9#ifndef FORTRAN_SEMANTICS_TYPE_H_
10#define FORTRAN_SEMANTICS_TYPE_H_
12#include "flang/Common/idioms.h"
13#include "flang/Evaluate/expression.h"
14#include "flang/Parser/char-block.h"
15#include "flang/Support/Fortran.h"
33std::optional<bool> AreEquivalentInInterface(
const Expr<T> &,
const Expr<T> &);
34extern template std::optional<bool>
35AreEquivalentInInterface<Type<TypeCategory::Integer>>(
40namespace Fortran::semantics {
48using SourceName = parser::CharBlock;
49using TypeCategory = common::TypeCategory;
50using SomeExpr = evaluate::Expr<evaluate::SomeType>;
51using MaybeExpr = std::optional<SomeExpr>;
53 evaluate::Expr<evaluate::Type<common::TypeCategory::Integer>>;
54using MaybeIntExpr = std::optional<SomeIntExpr>;
55using SubscriptIntExpr = evaluate::Expr<evaluate::SubscriptInteger>;
56using MaybeSubscriptIntExpr = std::optional<SubscriptIntExpr>;
57using KindExpr = SubscriptIntExpr;
59KindExpr MakeKindExpr(
int v);
70 static Bound Star() {
return Bound(Category::Star); }
71 static Bound Colon() {
return Bound(Category::Colon); }
72 explicit Bound(MaybeSubscriptIntExpr &&expr) : expr_{std::move(expr)} {
73 CHECK(evaluate::IsSubscriptIntegerKind(expr_));
75 explicit Bound(common::ConstantSubscript bound);
76 Bound(
const Bound &) =
default;
77 Bound(Bound &&) =
default;
78 Bound &operator=(
const Bound &) =
default;
79 Bound &operator=(Bound &&) =
default;
80 bool isExplicit()
const {
return category_ == Category::Explicit; }
81 bool isStar()
const {
return category_ == Category::Star; }
82 bool isColon()
const {
return category_ == Category::Colon; }
83 MaybeSubscriptIntExpr &GetExplicit() {
return expr_; }
84 const MaybeSubscriptIntExpr &GetExplicit()
const {
return expr_; }
85 void SetExplicit(MaybeSubscriptIntExpr &&expr) {
87 CHECK(evaluate::IsSubscriptIntegerKind(expr));
88 expr_ = std::move(expr);
92 enum class Category { Explicit, Star, Colon };
93 Bound(Category category) : category_{category} {}
94 Bound(Category category, MaybeSubscriptIntExpr &&expr)
95 : category_{category}, expr_{std::move(expr)} {}
96 Category category_{Category::Explicit};
97 MaybeSubscriptIntExpr expr_;
98 friend llvm::raw_ostream &operator<<(llvm::raw_ostream &,
const Bound &);
105 static ParamValue Assumed(common::TypeParamAttr attr) {
106 return ParamValue{Category::Assumed, attr};
108 static ParamValue Deferred(common::TypeParamAttr attr) {
109 return ParamValue{Category::Deferred, attr};
111 ParamValue(
const ParamValue &) =
default;
112 explicit ParamValue(MaybeIntExpr &&, common::TypeParamAttr);
113 explicit ParamValue(SomeIntExpr &&, common::TypeParamAttr attr);
114 explicit ParamValue(common::ConstantSubscript, common::TypeParamAttr attr);
115 bool isExplicit()
const {
return category_ == Category::Explicit; }
116 bool isAssumed()
const {
return category_ == Category::Assumed; }
117 bool isDeferred()
const {
return category_ == Category::Deferred; }
118 const MaybeIntExpr &GetExplicit()
const {
return expr_; }
119 void SetExplicit(SomeIntExpr &&);
120 bool isKind()
const {
return attr_ == common::TypeParamAttr::Kind; }
121 bool isLen()
const {
return attr_ == common::TypeParamAttr::Len; }
122 void set_attr(common::TypeParamAttr attr) { attr_ = attr; }
123 bool operator==(
const ParamValue &that)
const {
124 return category_ == that.category_ && expr_ == that.expr_;
126 bool operator!=(
const ParamValue &that)
const {
return !(*
this == that); }
127 bool IsEquivalentInInterface(
const ParamValue &that)
const {
128 return (category_ == that.category_ &&
129 expr_.has_value() == that.expr_.has_value() &&
131 evaluate::AreEquivalentInInterface(*expr_, *that.expr_)
134 std::string AsFortran()
const;
137 enum class Category { Explicit, Deferred, Assumed };
138 ParamValue(Category category, common::TypeParamAttr attr)
139 : category_{category}, attr_{attr} {}
140 Category category_{Category::Explicit};
141 common::TypeParamAttr attr_{common::TypeParamAttr::Kind};
143 friend llvm::raw_ostream &operator<<(llvm::raw_ostream &,
const ParamValue &);
146class IntrinsicTypeSpec {
148 TypeCategory category()
const {
return category_; }
149 const KindExpr &kind()
const {
return kind_; }
150 bool operator==(
const IntrinsicTypeSpec &x)
const {
151 return category_ == x.category_ && kind_ == x.kind_;
153 bool operator!=(
const IntrinsicTypeSpec &x)
const {
return !operator==(x); }
154 std::string AsFortran()
const;
157 IntrinsicTypeSpec(TypeCategory, KindExpr &&);
160 TypeCategory category_;
162 friend llvm::raw_ostream &operator<<(
163 llvm::raw_ostream &os,
const IntrinsicTypeSpec &x);
166class NumericTypeSpec :
public IntrinsicTypeSpec {
168 NumericTypeSpec(TypeCategory category, KindExpr &&kind)
169 : IntrinsicTypeSpec(category, std::move(kind)) {
170 CHECK(common::IsNumericTypeCategory(category));
174class LogicalTypeSpec :
public IntrinsicTypeSpec {
176 explicit LogicalTypeSpec(KindExpr &&kind)
177 : IntrinsicTypeSpec(TypeCategory::Logical, std::move(kind)) {}
180class CharacterTypeSpec :
public IntrinsicTypeSpec {
182 CharacterTypeSpec(
ParamValue &&length, KindExpr &&kind)
183 : IntrinsicTypeSpec(TypeCategory::Character, std::move(kind)),
184 length_{std::move(length)} {}
185 const ParamValue &length()
const {
return length_; }
186 bool operator==(
const CharacterTypeSpec &that)
const {
187 return kind() == that.kind() && length_ == that.length_;
189 std::string AsFortran()
const;
193 friend llvm::raw_ostream &operator<<(
194 llvm::raw_ostream &os,
const CharacterTypeSpec &x);
200 static ShapeSpec MakeExplicit(
Bound &&lb,
Bound &&ub) {
201 return ShapeSpec(std::move(lb), std::move(ub));
204 static const ShapeSpec MakeExplicit(
Bound &&ub) {
205 return MakeExplicit(
Bound{1}, std::move(ub));
208 static ShapeSpec MakeAssumedShape() {
209 return ShapeSpec(
Bound{1}, Bound::Colon());
212 static ShapeSpec MakeAssumedShape(
Bound &&lb) {
213 return ShapeSpec(std::move(lb), Bound::Colon());
216 static ShapeSpec MakeDeferred() {
217 return ShapeSpec(Bound::Colon(), Bound::Colon());
220 static ShapeSpec MakeImplied() {
return ShapeSpec(
Bound{1}, Bound::Star()); }
222 static ShapeSpec MakeImplied(
Bound &&lb) {
223 return ShapeSpec(std::move(lb), Bound::Star());
226 static ShapeSpec MakeAssumedRank() {
227 return ShapeSpec(Bound::Star(), Bound::Star());
230 ShapeSpec(
const ShapeSpec &) =
default;
231 ShapeSpec(ShapeSpec &&) =
default;
232 ShapeSpec &operator=(
const ShapeSpec &) =
default;
233 ShapeSpec &operator=(ShapeSpec &&) =
default;
235 Bound &lbound() {
return lb_; }
236 const Bound &lbound()
const {
return lb_; }
237 Bound &ubound() {
return ub_; }
238 const Bound &ubound()
const {
return ub_; }
241 ShapeSpec(
Bound &&lb,
Bound &&ub) : lb_{std::move(lb)}, ub_{std::move(ub)} {}
244 friend llvm::raw_ostream &operator<<(llvm::raw_ostream &,
const ShapeSpec &);
247struct ArraySpec :
public std::vector<ShapeSpec> {
249 int Rank()
const {
return size(); }
254 inline bool IsExplicitShape()
const;
255 inline bool CanBeAssumedShape()
const;
256 inline bool CanBeDeferredShape()
const;
257 inline bool CanBeImpliedShape()
const;
258 inline bool CanBeAssumedSize()
const;
259 inline bool IsAssumedRank()
const;
265 bool isFromRankOneBounds()
const {
266 return lboundIsRankOne_ || uboundIsRankOne_;
268 bool lboundIsRankOne()
const {
return lboundIsRankOne_; }
269 bool uboundIsRankOne()
const {
return uboundIsRankOne_; }
270 void set_rankOneBounds(
bool lower,
bool upper) {
271 lboundIsRankOne_ =
lower;
272 uboundIsRankOne_ = upper;
275 std::vector<ShapeSpec>::clear();
276 lboundIsRankOne_ = uboundIsRankOne_ =
false;
280 bool lboundIsRankOne_{
false};
281 bool uboundIsRankOne_{
false};
283 template <
typename P>
bool CheckAll(P predicate)
const {
284 return !empty() && std::all_of(begin(), end(), predicate);
287llvm::raw_ostream &operator<<(llvm::raw_ostream &,
const ArraySpec &);
291class DerivedTypeSpec {
293 enum class Category {
301 using RawParameter = std::pair<const parser::Keyword *, ParamValue>;
302 using RawParameters = std::vector<RawParameter>;
303 using ParameterMapType = std::map<SourceName, ParamValue>;
304 DerivedTypeSpec(SourceName,
const Symbol &);
305 DerivedTypeSpec(
const DerivedTypeSpec &);
306 DerivedTypeSpec(DerivedTypeSpec &&);
308 const SourceName &name()
const {
return name_; }
309 const Symbol &originalTypeSymbol()
const {
return originalTypeSymbol_; }
310 const Symbol &typeSymbol()
const {
return typeSymbol_; }
311 const Scope *scope()
const {
return scope_; }
313 const Scope *GetScope()
const;
314 void set_scope(
const Scope &);
315 void ReplaceScope(
const Scope &);
316 const RawParameters &rawParameters()
const {
return rawParameters_; }
317 const ParameterMapType ¶meters()
const {
return parameters_; }
319 bool MightBeParameterized()
const;
320 bool IsForwardReferenced()
const;
321 bool HasDefaultInitialization(
322 bool ignoreAllocatable =
false,
bool ignorePointer =
true)
const;
323 std::optional<std::string>
324 ComponentWithDefaultInitialization(
325 bool ignoreAllocatable =
false,
bool ignorePointer =
true)
const;
326 bool HasDestruction()
const;
331 void AddRawParamValue(
const parser::Keyword *,
ParamValue &&);
338 void AddParamValue(SourceName,
ParamValue &&);
342 void Instantiate(
Scope &containingScope);
345 void PrepareForScopeClone();
348 const ParamValue *FindParameter(SourceName target)
const {
349 auto iter{parameters_.find(target)};
350 if (iter != parameters_.end()) {
351 return &iter->second;
356 bool operator==(
const DerivedTypeSpec &that)
const {
357 return RawEquals(that) && parameters_ == that.parameters_;
359 bool operator!=(
const DerivedTypeSpec &that)
const {
360 return !(*
this == that);
364 bool MatchesOrExtends(
const DerivedTypeSpec &)
const;
365 std::string AsFortran()
const;
366 std::string VectorTypeAsFortran()
const;
368 Category category()
const {
return category_; }
369 void set_category(Category category) { category_ = category; }
370 bool IsVectorType()
const {
371 return category_ == Category::IntrinsicVector ||
372 category_ == Category::PairVector || category_ == Category::QuadVector;
374 bool IsEnumerationType()
const {
375 return category_ == Category::EnumerationType;
380 const Symbol &originalTypeSymbol_;
381 const Symbol &typeSymbol_;
382 const Scope *scope_{
nullptr};
384 bool evaluated_{
false};
385 bool instantiated_{
false};
386 RawParameters rawParameters_;
387 ParameterMapType parameters_;
388 Category category_{Category::DerivedType};
389 bool RawEquals(
const DerivedTypeSpec &that)
const {
390 return &typeSymbol_ == &that.typeSymbol_ &&
391 &originalTypeSymbol_ == &that.originalTypeSymbol_ &&
392 cooked_ == that.cooked_ && rawParameters_ == that.rawParameters_;
394 friend llvm::raw_ostream &operator<<(
395 llvm::raw_ostream &,
const DerivedTypeSpec &);
420 DeclTypeSpec(Category);
422 bool operator==(
const DeclTypeSpec &)
const;
423 bool operator!=(
const DeclTypeSpec &that)
const {
return !operator==(that); }
425 Category category()
const {
return category_; }
426 void set_category(Category category) { category_ = category; }
427 bool IsPolymorphic()
const {
428 return category_ == ClassDerived || IsUnlimitedPolymorphic();
430 bool IsUnlimitedPolymorphic()
const {
431 return category_ == TypeStar || category_ == ClassStar;
433 bool IsAssumedType()
const {
return category_ == TypeStar; }
434 bool IsNumeric(TypeCategory)
const;
435 bool IsSequenceType()
const;
439 CHECK(category_ == Character);
440 return std::get<CharacterTypeSpec>(typeSpec_);
443 CHECK(category_ == TypeDerived || category_ == ClassDerived);
444 return std::get<DerivedTypeSpec>(typeSpec_);
447 CHECK(category_ == TypeDerived || category_ == ClassDerived);
448 return std::get<DerivedTypeSpec>(typeSpec_);
456 std::string AsFortran()
const;
464llvm::raw_ostream &operator<<(llvm::raw_ostream &,
const DeclTypeSpec &);
469inline bool ArraySpec::IsExplicitShape()
const {
470 return CheckAll([](
const ShapeSpec &x) {
return x.ubound().isExplicit(); });
472inline bool ArraySpec::CanBeAssumedShape()
const {
473 return CheckAll([](
const ShapeSpec &x) {
return x.ubound().isColon(); });
475inline bool ArraySpec::CanBeDeferredShape()
const {
476 return CheckAll([](
const ShapeSpec &x) {
477 return x.lbound().isColon() && x.ubound().isColon();
480inline bool ArraySpec::CanBeImpliedShape()
const {
481 return !IsAssumedRank() &&
482 CheckAll([](
const ShapeSpec &x) {
return x.ubound().isStar(); });
484inline bool ArraySpec::CanBeAssumedSize()
const {
485 return !empty() && !IsAssumedRank() && back().ubound().isStar() &&
486 std::all_of(begin(), end() - 1,
487 [](
const ShapeSpec &x) {
return x.ubound().isExplicit(); });
489inline bool ArraySpec::IsAssumedRank()
const {
490 return Rank() == 1 && front().lbound().isStar();
496 return &std::get<NumericTypeSpec>(typeSpec_);
498 return &std::get<LogicalTypeSpec>(typeSpec_);
500 return &std::get<CharacterTypeSpec>(typeSpec_);
506 return const_cast<DeclTypeSpec *
>(
this)->AsIntrinsic();
513 return &std::get<DerivedTypeSpec>(typeSpec_);
519 return const_cast<DeclTypeSpec *
>(
this)->AsDerived();
Definition semantics.h:71
Definition ParserActions.h:24
Definition check-expression.h:19