1 //===-- lib/Semantics/type.cpp --------------------------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #include "flang/Semantics/type.h"
10 #include "flang/Evaluate/fold.h"
11 #include "flang/Parser/characters.h"
12 #include "flang/Semantics/scope.h"
13 #include "flang/Semantics/symbol.h"
14 #include "flang/Semantics/tools.h"
15 #include "llvm/Support/raw_ostream.h"
16 
17 namespace Fortran::semantics {
18 
19 DerivedTypeSpec::DerivedTypeSpec(SourceName name, const Symbol &typeSymbol)
20     : name_{name}, typeSymbol_{typeSymbol} {
21   CHECK(typeSymbol.has<DerivedTypeDetails>());
22 }
23 DerivedTypeSpec::DerivedTypeSpec(const DerivedTypeSpec &that) = default;
24 DerivedTypeSpec::DerivedTypeSpec(DerivedTypeSpec &&that) = default;
25 
26 void DerivedTypeSpec::set_scope(const Scope &scope) {
27   CHECK(!scope_);
28   ReplaceScope(scope);
29 }
30 void DerivedTypeSpec::ReplaceScope(const Scope &scope) {
31   CHECK(scope.IsDerivedType());
32   scope_ = &scope;
33 }
34 
35 void DerivedTypeSpec::AddRawParamValue(
36     const std::optional<parser::Keyword> &keyword, ParamValue &&value) {
37   CHECK(parameters_.empty());
38   rawParameters_.emplace_back(keyword ? &*keyword : nullptr, std::move(value));
39 }
40 
41 void DerivedTypeSpec::CookParameters(evaluate::FoldingContext &foldingContext) {
42   if (cooked_) {
43     return;
44   }
45   cooked_ = true;
46   auto &messages{foldingContext.messages()};
47   if (IsForwardReferenced()) {
48     messages.Say(typeSymbol_.name(),
49         "Derived type '%s' was used but never defined"_err_en_US,
50         typeSymbol_.name());
51     return;
52   }
53 
54   // Parameters of the most deeply nested "base class" come first when the
55   // derived type is an extension.
56   auto parameterNames{OrderParameterNames(typeSymbol_)};
57   auto parameterDecls{OrderParameterDeclarations(typeSymbol_)};
58   auto nextNameIter{parameterNames.begin()};
59   RawParameters raw{std::move(rawParameters_)};
60   for (auto &[maybeKeyword, value] : raw) {
61     SourceName name;
62     common::TypeParamAttr attr{common::TypeParamAttr::Kind};
63     if (maybeKeyword) {
64       name = maybeKeyword->v.source;
65       auto it{std::find_if(parameterDecls.begin(), parameterDecls.end(),
66           [&](const Symbol &symbol) { return symbol.name() == name; })};
67       if (it == parameterDecls.end()) {
68         messages.Say(name,
69             "'%s' is not the name of a parameter for derived type '%s'"_err_en_US,
70             name, typeSymbol_.name());
71       } else {
72         // Resolve the keyword's symbol
73         maybeKeyword->v.symbol = const_cast<Symbol *>(&it->get());
74         attr = it->get().get<TypeParamDetails>().attr();
75       }
76     } else if (nextNameIter != parameterNames.end()) {
77       name = *nextNameIter++;
78       auto it{std::find_if(parameterDecls.begin(), parameterDecls.end(),
79           [&](const Symbol &symbol) { return symbol.name() == name; })};
80       if (it == parameterDecls.end()) {
81         break;
82       }
83       attr = it->get().get<TypeParamDetails>().attr();
84     } else {
85       messages.Say(name_,
86           "Too many type parameters given for derived type '%s'"_err_en_US,
87           typeSymbol_.name());
88       break;
89     }
90     if (FindParameter(name)) {
91       messages.Say(name_,
92           "Multiple values given for type parameter '%s'"_err_en_US, name);
93     } else {
94       value.set_attr(attr);
95       AddParamValue(name, std::move(value));
96     }
97   }
98 }
99 
100 void DerivedTypeSpec::EvaluateParameters(SemanticsContext &context) {
101   evaluate::FoldingContext &foldingContext{context.foldingContext()};
102   CookParameters(foldingContext);
103   if (evaluated_) {
104     return;
105   }
106   evaluated_ = true;
107   auto &messages{foldingContext.messages()};
108 
109   // Fold the explicit type parameter value expressions first.  Do not
110   // fold them within the scope of the derived type being instantiated;
111   // these expressions cannot use its type parameters.  Convert the values
112   // of the expressions to the declared types of the type parameters.
113   auto parameterDecls{OrderParameterDeclarations(typeSymbol_)};
114   for (const Symbol &symbol : parameterDecls) {
115     const SourceName &name{symbol.name()};
116     if (ParamValue * paramValue{FindParameter(name)}) {
117       if (const MaybeIntExpr & expr{paramValue->GetExplicit()}) {
118         if (auto converted{evaluate::ConvertToType(symbol, SomeExpr{*expr})}) {
119           SomeExpr folded{
120               evaluate::Fold(foldingContext, std::move(*converted))};
121           if (auto *intExpr{std::get_if<SomeIntExpr>(&folded.u)}) {
122             paramValue->SetExplicit(std::move(*intExpr));
123             continue;
124           }
125         }
126         if (!context.HasError(symbol)) {
127           evaluate::SayWithDeclaration(messages, symbol,
128               "Value of type parameter '%s' (%s) is not convertible to its"
129               " type"_err_en_US,
130               name, expr->AsFortran());
131         }
132       }
133     }
134   }
135 
136   // Default initialization expressions for the derived type's parameters
137   // may reference other parameters so long as the declaration precedes the
138   // use in the expression (10.1.12).  This is not necessarily the same
139   // order as "type parameter order" (7.5.3.2).
140   // Type parameter default value expressions are folded in declaration order
141   // within the scope of the derived type so that the values of earlier type
142   // parameters are available for use in the default initialization
143   // expressions of later parameters.
144   auto restorer{foldingContext.WithPDTInstance(*this)};
145   for (const Symbol &symbol : parameterDecls) {
146     const SourceName &name{symbol.name()};
147     if (!FindParameter(name)) {
148       const TypeParamDetails &details{symbol.get<TypeParamDetails>()};
149       if (details.init()) {
150         auto expr{
151             evaluate::Fold(foldingContext, common::Clone(details.init()))};
152         AddParamValue(name, ParamValue{std::move(*expr), details.attr()});
153       } else if (!context.HasError(symbol)) {
154         messages.Say(name_,
155             "Type parameter '%s' lacks a value and has no default"_err_en_US,
156             name);
157       }
158     }
159   }
160 }
161 
162 void DerivedTypeSpec::AddParamValue(SourceName name, ParamValue &&value) {
163   CHECK(cooked_);
164   auto pair{parameters_.insert(std::make_pair(name, std::move(value)))};
165   CHECK(pair.second); // name was not already present
166 }
167 
168 bool DerivedTypeSpec::MightBeParameterized() const {
169   return !cooked_ || !parameters_.empty();
170 }
171 
172 bool DerivedTypeSpec::IsForwardReferenced() const {
173   return typeSymbol_.get<DerivedTypeDetails>().isForwardReferenced();
174 }
175 
176 bool DerivedTypeSpec::HasDefaultInitialization() const {
177   DirectComponentIterator components{*this};
178   return bool{std::find_if(components.begin(), components.end(),
179       [](const Symbol &component) { return IsInitialized(component); })};
180 }
181 
182 ParamValue *DerivedTypeSpec::FindParameter(SourceName target) {
183   return const_cast<ParamValue *>(
184       const_cast<const DerivedTypeSpec *>(this)->FindParameter(target));
185 }
186 
187 class InstantiateHelper {
188 public:
189   InstantiateHelper(SemanticsContext &context, Scope &scope)
190       : context_{context}, scope_{scope} {}
191   // Instantiate components from fromScope into scope_
192   void InstantiateComponents(const Scope &);
193 
194 private:
195   evaluate::FoldingContext &foldingContext() {
196     return context_.foldingContext();
197   }
198   template <typename T> T Fold(T &&expr) {
199     return evaluate::Fold(foldingContext(), std::move(expr));
200   }
201   void InstantiateComponent(const Symbol &);
202   const DeclTypeSpec *InstantiateType(const Symbol &);
203   const DeclTypeSpec &InstantiateIntrinsicType(const DeclTypeSpec &);
204   DerivedTypeSpec CreateDerivedTypeSpec(const DerivedTypeSpec &, bool);
205 
206   SemanticsContext &context_;
207   Scope &scope_;
208 };
209 
210 void DerivedTypeSpec::Instantiate(
211     Scope &containingScope, SemanticsContext &context) {
212   if (instantiated_) {
213     return;
214   }
215   instantiated_ = true;
216   auto &foldingContext{context.foldingContext()};
217   if (IsForwardReferenced()) {
218     foldingContext.messages().Say(typeSymbol_.name(),
219         "The derived type '%s' was forward-referenced but not defined"_err_en_US,
220         typeSymbol_.name());
221     return;
222   }
223   EvaluateParameters(context);
224   const Scope &typeScope{DEREF(typeSymbol_.scope())};
225   if (!MightBeParameterized()) {
226     scope_ = &typeScope;
227     for (auto &pair : typeScope) {
228       Symbol &symbol{*pair.second};
229       if (DeclTypeSpec * type{symbol.GetType()}) {
230         if (DerivedTypeSpec * derived{type->AsDerived()}) {
231           if (!(derived->IsForwardReferenced() &&
232                   IsAllocatableOrPointer(symbol))) {
233             derived->Instantiate(containingScope, context);
234           }
235         }
236       }
237     }
238     return;
239   }
240   Scope &newScope{containingScope.MakeScope(Scope::Kind::DerivedType)};
241   newScope.set_derivedTypeSpec(*this);
242   ReplaceScope(newScope);
243   for (const Symbol &symbol : OrderParameterDeclarations(typeSymbol_)) {
244     const SourceName &name{symbol.name()};
245     if (typeScope.find(symbol.name()) != typeScope.end()) {
246       // This type parameter belongs to the derived type itself, not to
247       // one of its ancestors.  Put the type parameter expression value
248       // into the new scope as the initialization value for the parameter.
249       if (ParamValue * paramValue{FindParameter(name)}) {
250         const TypeParamDetails &details{symbol.get<TypeParamDetails>()};
251         paramValue->set_attr(details.attr());
252         if (MaybeIntExpr expr{paramValue->GetExplicit()}) {
253           // Ensure that any kind type parameters with values are
254           // constant by now.
255           if (details.attr() == common::TypeParamAttr::Kind) {
256             // Any errors in rank and type will have already elicited
257             // messages, so don't pile on by complaining further here.
258             if (auto maybeDynamicType{expr->GetType()}) {
259               if (expr->Rank() == 0 &&
260                   maybeDynamicType->category() == TypeCategory::Integer) {
261                 if (!evaluate::ToInt64(*expr)) {
262                   if (auto *msg{foldingContext.messages().Say(
263                           "Value of kind type parameter '%s' (%s) is not "
264                           "a scalar INTEGER constant"_err_en_US,
265                           name, expr->AsFortran())}) {
266                     msg->Attach(name, "declared here"_en_US);
267                   }
268                 }
269               }
270             }
271           }
272           TypeParamDetails instanceDetails{details.attr()};
273           if (const DeclTypeSpec * type{details.type()}) {
274             instanceDetails.set_type(*type);
275           }
276           instanceDetails.set_init(std::move(*expr));
277           newScope.try_emplace(name, std::move(instanceDetails));
278         }
279       }
280     }
281   }
282   // Instantiate every non-parameter symbol from the original derived
283   // type's scope into the new instance.
284   auto restorer{foldingContext.WithPDTInstance(*this)};
285   newScope.AddSourceRange(typeScope.sourceRange());
286   InstantiateHelper{context, newScope}.InstantiateComponents(typeScope);
287 }
288 
289 void InstantiateHelper::InstantiateComponents(const Scope &fromScope) {
290   for (const auto &pair : fromScope) {
291     InstantiateComponent(*pair.second);
292   }
293 }
294 
295 void InstantiateHelper::InstantiateComponent(const Symbol &oldSymbol) {
296   auto pair{scope_.try_emplace(
297       oldSymbol.name(), oldSymbol.attrs(), common::Clone(oldSymbol.details()))};
298   Symbol &newSymbol{*pair.first->second};
299   if (!pair.second) {
300     // Symbol was already present in the scope, which can only happen
301     // in the case of type parameters.
302     CHECK(oldSymbol.has<TypeParamDetails>());
303     return;
304   }
305   newSymbol.flags() = oldSymbol.flags();
306   if (auto *details{newSymbol.detailsIf<ObjectEntityDetails>()}) {
307     if (const DeclTypeSpec * newType{InstantiateType(newSymbol)}) {
308       details->ReplaceType(*newType);
309     }
310     details->set_init(Fold(std::move(details->init())));
311     for (ShapeSpec &dim : details->shape()) {
312       if (dim.lbound().isExplicit()) {
313         dim.lbound().SetExplicit(Fold(std::move(dim.lbound().GetExplicit())));
314       }
315       if (dim.ubound().isExplicit()) {
316         dim.ubound().SetExplicit(Fold(std::move(dim.ubound().GetExplicit())));
317       }
318     }
319     for (ShapeSpec &dim : details->coshape()) {
320       if (dim.lbound().isExplicit()) {
321         dim.lbound().SetExplicit(Fold(std::move(dim.lbound().GetExplicit())));
322       }
323       if (dim.ubound().isExplicit()) {
324         dim.ubound().SetExplicit(Fold(std::move(dim.ubound().GetExplicit())));
325       }
326     }
327   }
328 }
329 
330 const DeclTypeSpec *InstantiateHelper::InstantiateType(const Symbol &symbol) {
331   const DeclTypeSpec *type{symbol.GetType()};
332   if (!type) {
333     return nullptr; // error has occurred
334   } else if (const DerivedTypeSpec * spec{type->AsDerived()}) {
335     return &FindOrInstantiateDerivedType(scope_,
336         CreateDerivedTypeSpec(*spec, symbol.test(Symbol::Flag::ParentComp)),
337         context_, type->category());
338   } else if (type->AsIntrinsic()) {
339     return &InstantiateIntrinsicType(*type);
340   } else if (type->category() == DeclTypeSpec::ClassStar) {
341     return type;
342   } else {
343     common::die("InstantiateType: %s", type->AsFortran().c_str());
344   }
345 }
346 
347 // Apply type parameter values to an intrinsic type spec.
348 const DeclTypeSpec &InstantiateHelper::InstantiateIntrinsicType(
349     const DeclTypeSpec &spec) {
350   const IntrinsicTypeSpec &intrinsic{DEREF(spec.AsIntrinsic())};
351   if (evaluate::ToInt64(intrinsic.kind())) {
352     return spec; // KIND is already a known constant
353   }
354   // The expression was not originally constant, but now it must be so
355   // in the context of a parameterized derived type instantiation.
356   KindExpr copy{Fold(common::Clone(intrinsic.kind()))};
357   int kind{context_.GetDefaultKind(intrinsic.category())};
358   if (auto value{evaluate::ToInt64(copy)}) {
359     if (evaluate::IsValidKindOfIntrinsicType(intrinsic.category(), *value)) {
360       kind = *value;
361     } else {
362       foldingContext().messages().Say(
363           "KIND parameter value (%jd) of intrinsic type %s "
364           "did not resolve to a supported value"_err_en_US,
365           *value,
366           parser::ToUpperCaseLetters(EnumToString(intrinsic.category())));
367     }
368   }
369   switch (spec.category()) {
370   case DeclTypeSpec::Numeric:
371     return scope_.MakeNumericType(intrinsic.category(), KindExpr{kind});
372   case DeclTypeSpec::Logical:
373     return scope_.MakeLogicalType(KindExpr{kind});
374   case DeclTypeSpec::Character:
375     return scope_.MakeCharacterType(
376         ParamValue{spec.characterTypeSpec().length()}, KindExpr{kind});
377   default:
378     CRASH_NO_CASE;
379   }
380 }
381 
382 DerivedTypeSpec InstantiateHelper::CreateDerivedTypeSpec(
383     const DerivedTypeSpec &spec, bool isParentComp) {
384   DerivedTypeSpec result{spec};
385   result.CookParameters(foldingContext()); // enables AddParamValue()
386   if (isParentComp) {
387     // Forward any explicit type parameter values from the
388     // derived type spec under instantiation that define type parameters
389     // of the parent component to the derived type spec of the
390     // parent component.
391     const DerivedTypeSpec &instanceSpec{DEREF(foldingContext().pdtInstance())};
392     for (const auto &[name, value] : instanceSpec.parameters()) {
393       if (scope_.find(name) == scope_.end()) {
394         result.AddParamValue(name, ParamValue{value});
395       }
396     }
397   }
398   return result;
399 }
400 
401 std::string DerivedTypeSpec::AsFortran() const {
402   std::string buf;
403   llvm::raw_string_ostream ss{buf};
404   ss << name_;
405   if (!rawParameters_.empty()) {
406     CHECK(parameters_.empty());
407     ss << '(';
408     bool first = true;
409     for (const auto &[maybeKeyword, value] : rawParameters_) {
410       if (first) {
411         first = false;
412       } else {
413         ss << ',';
414       }
415       if (maybeKeyword) {
416         ss << maybeKeyword->v.source.ToString() << '=';
417       }
418       ss << value.AsFortran();
419     }
420     ss << ')';
421   } else if (!parameters_.empty()) {
422     ss << '(';
423     bool first = true;
424     for (const auto &[name, value] : parameters_) {
425       if (first) {
426         first = false;
427       } else {
428         ss << ',';
429       }
430       ss << name.ToString() << '=' << value.AsFortran();
431     }
432     ss << ')';
433   }
434   return ss.str();
435 }
436 
437 llvm::raw_ostream &operator<<(llvm::raw_ostream &o, const DerivedTypeSpec &x) {
438   return o << x.AsFortran();
439 }
440 
441 Bound::Bound(common::ConstantSubscript bound) : expr_{bound} {}
442 
443 llvm::raw_ostream &operator<<(llvm::raw_ostream &o, const Bound &x) {
444   if (x.isAssumed()) {
445     o << '*';
446   } else if (x.isDeferred()) {
447     o << ':';
448   } else if (x.expr_) {
449     x.expr_->AsFortran(o);
450   } else {
451     o << "<no-expr>";
452   }
453   return o;
454 }
455 
456 llvm::raw_ostream &operator<<(llvm::raw_ostream &o, const ShapeSpec &x) {
457   if (x.lb_.isAssumed()) {
458     CHECK(x.ub_.isAssumed());
459     o << "..";
460   } else {
461     if (!x.lb_.isDeferred()) {
462       o << x.lb_;
463     }
464     o << ':';
465     if (!x.ub_.isDeferred()) {
466       o << x.ub_;
467     }
468   }
469   return o;
470 }
471 
472 llvm::raw_ostream &operator<<(
473     llvm::raw_ostream &os, const ArraySpec &arraySpec) {
474   char sep{'('};
475   for (auto &shape : arraySpec) {
476     os << sep << shape;
477     sep = ',';
478   }
479   if (sep == ',') {
480     os << ')';
481   }
482   return os;
483 }
484 
485 ParamValue::ParamValue(MaybeIntExpr &&expr, common::TypeParamAttr attr)
486     : attr_{attr}, expr_{std::move(expr)} {}
487 ParamValue::ParamValue(SomeIntExpr &&expr, common::TypeParamAttr attr)
488     : attr_{attr}, expr_{std::move(expr)} {}
489 ParamValue::ParamValue(
490     common::ConstantSubscript value, common::TypeParamAttr attr)
491     : ParamValue(SomeIntExpr{evaluate::Expr<evaluate::SubscriptInteger>{value}},
492           attr) {}
493 
494 void ParamValue::SetExplicit(SomeIntExpr &&x) {
495   category_ = Category::Explicit;
496   expr_ = std::move(x);
497 }
498 
499 std::string ParamValue::AsFortran() const {
500   switch (category_) {
501     SWITCH_COVERS_ALL_CASES
502   case Category::Assumed:
503     return "*";
504   case Category::Deferred:
505     return ":";
506   case Category::Explicit:
507     if (expr_) {
508       std::string buf;
509       llvm::raw_string_ostream ss{buf};
510       expr_->AsFortran(ss);
511       return ss.str();
512     } else {
513       return "";
514     }
515   }
516 }
517 
518 llvm::raw_ostream &operator<<(llvm::raw_ostream &o, const ParamValue &x) {
519   return o << x.AsFortran();
520 }
521 
522 IntrinsicTypeSpec::IntrinsicTypeSpec(TypeCategory category, KindExpr &&kind)
523     : category_{category}, kind_{std::move(kind)} {
524   CHECK(category != TypeCategory::Derived);
525 }
526 
527 static std::string KindAsFortran(const KindExpr &kind) {
528   std::string buf;
529   llvm::raw_string_ostream ss{buf};
530   if (auto k{evaluate::ToInt64(kind)}) {
531     ss << *k; // emit unsuffixed kind code
532   } else {
533     kind.AsFortran(ss);
534   }
535   return ss.str();
536 }
537 
538 std::string IntrinsicTypeSpec::AsFortran() const {
539   return parser::ToUpperCaseLetters(common::EnumToString(category_)) + '(' +
540       KindAsFortran(kind_) + ')';
541 }
542 
543 llvm::raw_ostream &operator<<(
544     llvm::raw_ostream &os, const IntrinsicTypeSpec &x) {
545   return os << x.AsFortran();
546 }
547 
548 std::string CharacterTypeSpec::AsFortran() const {
549   return "CHARACTER(" + length_.AsFortran() + ',' + KindAsFortran(kind()) + ')';
550 }
551 
552 llvm::raw_ostream &operator<<(
553     llvm::raw_ostream &os, const CharacterTypeSpec &x) {
554   return os << x.AsFortran();
555 }
556 
557 DeclTypeSpec::DeclTypeSpec(NumericTypeSpec &&typeSpec)
558     : category_{Numeric}, typeSpec_{std::move(typeSpec)} {}
559 DeclTypeSpec::DeclTypeSpec(LogicalTypeSpec &&typeSpec)
560     : category_{Logical}, typeSpec_{std::move(typeSpec)} {}
561 DeclTypeSpec::DeclTypeSpec(const CharacterTypeSpec &typeSpec)
562     : category_{Character}, typeSpec_{typeSpec} {}
563 DeclTypeSpec::DeclTypeSpec(CharacterTypeSpec &&typeSpec)
564     : category_{Character}, typeSpec_{std::move(typeSpec)} {}
565 DeclTypeSpec::DeclTypeSpec(Category category, const DerivedTypeSpec &typeSpec)
566     : category_{category}, typeSpec_{typeSpec} {
567   CHECK(category == TypeDerived || category == ClassDerived);
568 }
569 DeclTypeSpec::DeclTypeSpec(Category category, DerivedTypeSpec &&typeSpec)
570     : category_{category}, typeSpec_{std::move(typeSpec)} {
571   CHECK(category == TypeDerived || category == ClassDerived);
572 }
573 DeclTypeSpec::DeclTypeSpec(Category category) : category_{category} {
574   CHECK(category == TypeStar || category == ClassStar);
575 }
576 bool DeclTypeSpec::IsNumeric(TypeCategory tc) const {
577   return category_ == Numeric && numericTypeSpec().category() == tc;
578 }
579 bool DeclTypeSpec::IsSequenceType() const {
580   if (const DerivedTypeSpec * derivedType{AsDerived()}) {
581     const auto *typeDetails{
582         derivedType->typeSymbol().detailsIf<DerivedTypeDetails>()};
583     return typeDetails && typeDetails->sequence();
584   }
585   return false;
586 }
587 
588 const NumericTypeSpec &DeclTypeSpec::numericTypeSpec() const {
589   CHECK(category_ == Numeric);
590   return std::get<NumericTypeSpec>(typeSpec_);
591 }
592 const LogicalTypeSpec &DeclTypeSpec::logicalTypeSpec() const {
593   CHECK(category_ == Logical);
594   return std::get<LogicalTypeSpec>(typeSpec_);
595 }
596 bool DeclTypeSpec::operator==(const DeclTypeSpec &that) const {
597   return category_ == that.category_ && typeSpec_ == that.typeSpec_;
598 }
599 
600 std::string DeclTypeSpec::AsFortran() const {
601   switch (category_) {
602     SWITCH_COVERS_ALL_CASES
603   case Numeric:
604     return numericTypeSpec().AsFortran();
605   case Logical:
606     return logicalTypeSpec().AsFortran();
607   case Character:
608     return characterTypeSpec().AsFortran();
609   case TypeDerived:
610     return "TYPE(" + derivedTypeSpec().AsFortran() + ')';
611   case ClassDerived:
612     return "CLASS(" + derivedTypeSpec().AsFortran() + ')';
613   case TypeStar:
614     return "TYPE(*)";
615   case ClassStar:
616     return "CLASS(*)";
617   }
618 }
619 
620 llvm::raw_ostream &operator<<(llvm::raw_ostream &o, const DeclTypeSpec &x) {
621   return o << x.AsFortran();
622 }
623 
624 void ProcInterface::set_symbol(const Symbol &symbol) {
625   CHECK(!type_);
626   symbol_ = &symbol;
627 }
628 void ProcInterface::set_type(const DeclTypeSpec &type) {
629   CHECK(!symbol_);
630   type_ = &type;
631 }
632 } // namespace Fortran::semantics
633