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(
101     evaluate::FoldingContext &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 (!symbol.test(Symbol::Flag::Error)) {
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 (!symbol.test(Symbol::Flag::Error)) {
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   CookParameters(foldingContext);
224   EvaluateParameters(foldingContext);
225   const Scope &typeScope{DEREF(typeSymbol_.scope())};
226   if (!MightBeParameterized()) {
227     scope_ = &typeScope;
228     for (auto &pair : typeScope) {
229       Symbol &symbol{*pair.second};
230       if (DeclTypeSpec * type{symbol.GetType()}) {
231         if (DerivedTypeSpec * derived{type->AsDerived()}) {
232           if (!(derived->IsForwardReferenced() &&
233                   IsAllocatableOrPointer(symbol))) {
234             derived->Instantiate(containingScope, context);
235           }
236         }
237       }
238     }
239     return;
240   }
241   Scope &newScope{containingScope.MakeScope(Scope::Kind::DerivedType)};
242   newScope.set_derivedTypeSpec(*this);
243   ReplaceScope(newScope);
244   for (const Symbol &symbol : OrderParameterDeclarations(typeSymbol_)) {
245     const SourceName &name{symbol.name()};
246     if (typeScope.find(symbol.name()) != typeScope.end()) {
247       // This type parameter belongs to the derived type itself, not to
248       // one of its ancestors.  Put the type parameter expression value
249       // into the new scope as the initialization value for the parameter.
250       if (ParamValue * paramValue{FindParameter(name)}) {
251         const TypeParamDetails &details{symbol.get<TypeParamDetails>()};
252         paramValue->set_attr(details.attr());
253         if (MaybeIntExpr expr{paramValue->GetExplicit()}) {
254           // Ensure that any kind type parameters with values are
255           // constant by now.
256           if (details.attr() == common::TypeParamAttr::Kind) {
257             // Any errors in rank and type will have already elicited
258             // messages, so don't pile on by complaining further here.
259             if (auto maybeDynamicType{expr->GetType()}) {
260               if (expr->Rank() == 0 &&
261                   maybeDynamicType->category() == TypeCategory::Integer) {
262                 if (!evaluate::ToInt64(*expr)) {
263                   if (auto *msg{foldingContext.messages().Say(
264                           "Value of kind type parameter '%s' (%s) is not "
265                           "a scalar INTEGER constant"_err_en_US,
266                           name, expr->AsFortran())}) {
267                     msg->Attach(name, "declared here"_en_US);
268                   }
269                 }
270               }
271             }
272           }
273           TypeParamDetails instanceDetails{details.attr()};
274           if (const DeclTypeSpec * type{details.type()}) {
275             instanceDetails.set_type(*type);
276           }
277           instanceDetails.set_init(std::move(*expr));
278           newScope.try_emplace(name, std::move(instanceDetails));
279         }
280       }
281     }
282   }
283   // Instantiate every non-parameter symbol from the original derived
284   // type's scope into the new instance.
285   auto restorer{foldingContext.WithPDTInstance(*this)};
286   newScope.AddSourceRange(typeScope.sourceRange());
287   InstantiateHelper{context, newScope}.InstantiateComponents(typeScope);
288 }
289 
290 void InstantiateHelper::InstantiateComponents(const Scope &fromScope) {
291   for (const auto &pair : fromScope) {
292     InstantiateComponent(*pair.second);
293   }
294 }
295 
296 void InstantiateHelper::InstantiateComponent(const Symbol &oldSymbol) {
297   auto pair{scope_.try_emplace(
298       oldSymbol.name(), oldSymbol.attrs(), common::Clone(oldSymbol.details()))};
299   Symbol &newSymbol{*pair.first->second};
300   if (!pair.second) {
301     // Symbol was already present in the scope, which can only happen
302     // in the case of type parameters.
303     CHECK(oldSymbol.has<TypeParamDetails>());
304     return;
305   }
306   newSymbol.flags() = oldSymbol.flags();
307   if (auto *details{newSymbol.detailsIf<ObjectEntityDetails>()}) {
308     if (const DeclTypeSpec * newType{InstantiateType(newSymbol)}) {
309       details->ReplaceType(*newType);
310     }
311     details->set_init(Fold(std::move(details->init())));
312     for (ShapeSpec &dim : details->shape()) {
313       if (dim.lbound().isExplicit()) {
314         dim.lbound().SetExplicit(Fold(std::move(dim.lbound().GetExplicit())));
315       }
316       if (dim.ubound().isExplicit()) {
317         dim.ubound().SetExplicit(Fold(std::move(dim.ubound().GetExplicit())));
318       }
319     }
320     for (ShapeSpec &dim : details->coshape()) {
321       if (dim.lbound().isExplicit()) {
322         dim.lbound().SetExplicit(Fold(std::move(dim.lbound().GetExplicit())));
323       }
324       if (dim.ubound().isExplicit()) {
325         dim.ubound().SetExplicit(Fold(std::move(dim.ubound().GetExplicit())));
326       }
327     }
328   }
329 }
330 
331 const DeclTypeSpec *InstantiateHelper::InstantiateType(const Symbol &symbol) {
332   const DeclTypeSpec *type{symbol.GetType()};
333   if (!type) {
334     return nullptr; // error has occurred
335   } else if (const DerivedTypeSpec * spec{type->AsDerived()}) {
336     return &FindOrInstantiateDerivedType(scope_,
337         CreateDerivedTypeSpec(*spec, symbol.test(Symbol::Flag::ParentComp)),
338         context_, type->category());
339   } else if (type->AsIntrinsic()) {
340     return &InstantiateIntrinsicType(*type);
341   } else if (type->category() == DeclTypeSpec::ClassStar) {
342     return type;
343   } else {
344     common::die("InstantiateType: %s", type->AsFortran().c_str());
345   }
346 }
347 
348 // Apply type parameter values to an intrinsic type spec.
349 const DeclTypeSpec &InstantiateHelper::InstantiateIntrinsicType(
350     const DeclTypeSpec &spec) {
351   const IntrinsicTypeSpec &intrinsic{DEREF(spec.AsIntrinsic())};
352   if (evaluate::ToInt64(intrinsic.kind())) {
353     return spec; // KIND is already a known constant
354   }
355   // The expression was not originally constant, but now it must be so
356   // in the context of a parameterized derived type instantiation.
357   KindExpr copy{Fold(common::Clone(intrinsic.kind()))};
358   int kind{context_.GetDefaultKind(intrinsic.category())};
359   if (auto value{evaluate::ToInt64(copy)}) {
360     if (evaluate::IsValidKindOfIntrinsicType(intrinsic.category(), *value)) {
361       kind = *value;
362     } else {
363       foldingContext().messages().Say(
364           "KIND parameter value (%jd) of intrinsic type %s "
365           "did not resolve to a supported value"_err_en_US,
366           *value,
367           parser::ToUpperCaseLetters(EnumToString(intrinsic.category())));
368     }
369   }
370   switch (spec.category()) {
371   case DeclTypeSpec::Numeric:
372     return scope_.MakeNumericType(intrinsic.category(), KindExpr{kind});
373   case DeclTypeSpec::Logical:
374     return scope_.MakeLogicalType(KindExpr{kind});
375   case DeclTypeSpec::Character:
376     return scope_.MakeCharacterType(
377         ParamValue{spec.characterTypeSpec().length()}, KindExpr{kind});
378   default:
379     CRASH_NO_CASE;
380   }
381 }
382 
383 DerivedTypeSpec InstantiateHelper::CreateDerivedTypeSpec(
384     const DerivedTypeSpec &spec, bool isParentComp) {
385   DerivedTypeSpec result{spec};
386   result.CookParameters(foldingContext()); // enables AddParamValue()
387   if (isParentComp) {
388     // Forward any explicit type parameter values from the
389     // derived type spec under instantiation that define type parameters
390     // of the parent component to the derived type spec of the
391     // parent component.
392     const DerivedTypeSpec &instanceSpec{DEREF(foldingContext().pdtInstance())};
393     for (const auto &[name, value] : instanceSpec.parameters()) {
394       if (scope_.find(name) == scope_.end()) {
395         result.AddParamValue(name, ParamValue{value});
396       }
397     }
398   }
399   return result;
400 }
401 
402 std::string DerivedTypeSpec::AsFortran() const {
403   std::string buf;
404   llvm::raw_string_ostream ss{buf};
405   ss << name_;
406   if (!rawParameters_.empty()) {
407     CHECK(parameters_.empty());
408     ss << '(';
409     bool first = true;
410     for (const auto &[maybeKeyword, value] : rawParameters_) {
411       if (first) {
412         first = false;
413       } else {
414         ss << ',';
415       }
416       if (maybeKeyword) {
417         ss << maybeKeyword->v.source.ToString() << '=';
418       }
419       ss << value.AsFortran();
420     }
421     ss << ')';
422   } else if (!parameters_.empty()) {
423     ss << '(';
424     bool first = true;
425     for (const auto &[name, value] : parameters_) {
426       if (first) {
427         first = false;
428       } else {
429         ss << ',';
430       }
431       ss << name.ToString() << '=' << value.AsFortran();
432     }
433     ss << ')';
434   }
435   return ss.str();
436 }
437 
438 llvm::raw_ostream &operator<<(llvm::raw_ostream &o, const DerivedTypeSpec &x) {
439   return o << x.AsFortran();
440 }
441 
442 Bound::Bound(int bound) : expr_{bound} {}
443 
444 llvm::raw_ostream &operator<<(llvm::raw_ostream &o, const Bound &x) {
445   if (x.isAssumed()) {
446     o << '*';
447   } else if (x.isDeferred()) {
448     o << ':';
449   } else if (x.expr_) {
450     x.expr_->AsFortran(o);
451   } else {
452     o << "<no-expr>";
453   }
454   return o;
455 }
456 
457 llvm::raw_ostream &operator<<(llvm::raw_ostream &o, const ShapeSpec &x) {
458   if (x.lb_.isAssumed()) {
459     CHECK(x.ub_.isAssumed());
460     o << "..";
461   } else {
462     if (!x.lb_.isDeferred()) {
463       o << x.lb_;
464     }
465     o << ':';
466     if (!x.ub_.isDeferred()) {
467       o << x.ub_;
468     }
469   }
470   return o;
471 }
472 
473 llvm::raw_ostream &operator<<(
474     llvm::raw_ostream &os, const ArraySpec &arraySpec) {
475   char sep{'('};
476   for (auto &shape : arraySpec) {
477     os << sep << shape;
478     sep = ',';
479   }
480   if (sep == ',') {
481     os << ')';
482   }
483   return os;
484 }
485 
486 ParamValue::ParamValue(MaybeIntExpr &&expr, common::TypeParamAttr attr)
487     : attr_{attr}, expr_{std::move(expr)} {}
488 ParamValue::ParamValue(SomeIntExpr &&expr, common::TypeParamAttr attr)
489     : attr_{attr}, expr_{std::move(expr)} {}
490 ParamValue::ParamValue(
491     common::ConstantSubscript value, common::TypeParamAttr attr)
492     : ParamValue(SomeIntExpr{evaluate::Expr<evaluate::SubscriptInteger>{value}},
493           attr) {}
494 
495 void ParamValue::SetExplicit(SomeIntExpr &&x) {
496   category_ = Category::Explicit;
497   expr_ = std::move(x);
498 }
499 
500 std::string ParamValue::AsFortran() const {
501   switch (category_) {
502     SWITCH_COVERS_ALL_CASES
503   case Category::Assumed:
504     return "*";
505   case Category::Deferred:
506     return ":";
507   case Category::Explicit:
508     if (expr_) {
509       std::string buf;
510       llvm::raw_string_ostream ss{buf};
511       expr_->AsFortran(ss);
512       return ss.str();
513     } else {
514       return "";
515     }
516   }
517 }
518 
519 llvm::raw_ostream &operator<<(llvm::raw_ostream &o, const ParamValue &x) {
520   return o << x.AsFortran();
521 }
522 
523 IntrinsicTypeSpec::IntrinsicTypeSpec(TypeCategory category, KindExpr &&kind)
524     : category_{category}, kind_{std::move(kind)} {
525   CHECK(category != TypeCategory::Derived);
526 }
527 
528 static std::string KindAsFortran(const KindExpr &kind) {
529   std::string buf;
530   llvm::raw_string_ostream ss{buf};
531   if (auto k{evaluate::ToInt64(kind)}) {
532     ss << *k; // emit unsuffixed kind code
533   } else {
534     kind.AsFortran(ss);
535   }
536   return ss.str();
537 }
538 
539 std::string IntrinsicTypeSpec::AsFortran() const {
540   return parser::ToUpperCaseLetters(common::EnumToString(category_)) + '(' +
541       KindAsFortran(kind_) + ')';
542 }
543 
544 llvm::raw_ostream &operator<<(
545     llvm::raw_ostream &os, const IntrinsicTypeSpec &x) {
546   return os << x.AsFortran();
547 }
548 
549 std::string CharacterTypeSpec::AsFortran() const {
550   return "CHARACTER(" + length_.AsFortran() + ',' + KindAsFortran(kind()) + ')';
551 }
552 
553 llvm::raw_ostream &operator<<(
554     llvm::raw_ostream &os, const CharacterTypeSpec &x) {
555   return os << x.AsFortran();
556 }
557 
558 DeclTypeSpec::DeclTypeSpec(NumericTypeSpec &&typeSpec)
559     : category_{Numeric}, typeSpec_{std::move(typeSpec)} {}
560 DeclTypeSpec::DeclTypeSpec(LogicalTypeSpec &&typeSpec)
561     : category_{Logical}, typeSpec_{std::move(typeSpec)} {}
562 DeclTypeSpec::DeclTypeSpec(const CharacterTypeSpec &typeSpec)
563     : category_{Character}, typeSpec_{typeSpec} {}
564 DeclTypeSpec::DeclTypeSpec(CharacterTypeSpec &&typeSpec)
565     : category_{Character}, typeSpec_{std::move(typeSpec)} {}
566 DeclTypeSpec::DeclTypeSpec(Category category, const DerivedTypeSpec &typeSpec)
567     : category_{category}, typeSpec_{typeSpec} {
568   CHECK(category == TypeDerived || category == ClassDerived);
569 }
570 DeclTypeSpec::DeclTypeSpec(Category category, DerivedTypeSpec &&typeSpec)
571     : category_{category}, typeSpec_{std::move(typeSpec)} {
572   CHECK(category == TypeDerived || category == ClassDerived);
573 }
574 DeclTypeSpec::DeclTypeSpec(Category category) : category_{category} {
575   CHECK(category == TypeStar || category == ClassStar);
576 }
577 bool DeclTypeSpec::IsNumeric(TypeCategory tc) const {
578   return category_ == Numeric && numericTypeSpec().category() == tc;
579 }
580 bool DeclTypeSpec::IsSequenceType() const {
581   if (const DerivedTypeSpec * derivedType{AsDerived()}) {
582     const auto *typeDetails{
583         derivedType->typeSymbol().detailsIf<DerivedTypeDetails>()};
584     return typeDetails && typeDetails->sequence();
585   }
586   return false;
587 }
588 
589 const NumericTypeSpec &DeclTypeSpec::numericTypeSpec() const {
590   CHECK(category_ == Numeric);
591   return std::get<NumericTypeSpec>(typeSpec_);
592 }
593 const LogicalTypeSpec &DeclTypeSpec::logicalTypeSpec() const {
594   CHECK(category_ == Logical);
595   return std::get<LogicalTypeSpec>(typeSpec_);
596 }
597 bool DeclTypeSpec::operator==(const DeclTypeSpec &that) const {
598   return category_ == that.category_ && typeSpec_ == that.typeSpec_;
599 }
600 
601 std::string DeclTypeSpec::AsFortran() const {
602   switch (category_) {
603     SWITCH_COVERS_ALL_CASES
604   case Numeric:
605     return numericTypeSpec().AsFortran();
606   case Logical:
607     return logicalTypeSpec().AsFortran();
608   case Character:
609     return characterTypeSpec().AsFortran();
610   case TypeDerived:
611     return "TYPE(" + derivedTypeSpec().AsFortran() + ')';
612   case ClassDerived:
613     return "CLASS(" + derivedTypeSpec().AsFortran() + ')';
614   case TypeStar:
615     return "TYPE(*)";
616   case ClassStar:
617     return "CLASS(*)";
618   }
619 }
620 
621 llvm::raw_ostream &operator<<(llvm::raw_ostream &o, const DeclTypeSpec &x) {
622   return o << x.AsFortran();
623 }
624 
625 void ProcInterface::set_symbol(const Symbol &symbol) {
626   CHECK(!type_);
627   symbol_ = &symbol;
628 }
629 void ProcInterface::set_type(const DeclTypeSpec &type) {
630   CHECK(!symbol_);
631   type_ = &type;
632 }
633 } // namespace Fortran::semantics
634