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