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