FLANG
type.h
1//===-- include/flang/Semantics/type.h --------------------------*- C++ -*-===//
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#ifndef FORTRAN_SEMANTICS_TYPE_H_
10#define FORTRAN_SEMANTICS_TYPE_H_
11
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"
16#include <algorithm>
17#include <iosfwd>
18#include <map>
19#include <optional>
20#include <string>
21#include <variant>
22#include <vector>
23
24namespace llvm {
25class raw_ostream;
26}
27
28namespace Fortran::parser {
29struct Keyword;
30}
31
32namespace Fortran::evaluate { // avoid including all of Evaluate/tools.h
33template <typename T>
34std::optional<bool> AreEquivalentInInterface(const Expr<T> &, const Expr<T> &);
35extern template std::optional<bool> AreEquivalentInInterface<SomeInteger>(
36 const Expr<SomeInteger> &, const Expr<SomeInteger> &);
37} // namespace Fortran::evaluate
38
39namespace Fortran::semantics {
40
41class Scope;
42class SemanticsContext;
43class Symbol;
44
47using SourceName = parser::CharBlock;
48using TypeCategory = common::TypeCategory;
49using SomeExpr = evaluate::Expr<evaluate::SomeType>;
50using MaybeExpr = std::optional<SomeExpr>;
51using SomeIntExpr = evaluate::Expr<evaluate::SomeInteger>;
52using MaybeIntExpr = std::optional<SomeIntExpr>;
53using SubscriptIntExpr = evaluate::Expr<evaluate::SubscriptInteger>;
54using MaybeSubscriptIntExpr = std::optional<SubscriptIntExpr>;
55using KindExpr = SubscriptIntExpr;
56
57// An array spec bound: an explicit integer expression, assumed size
58// or implied shape(*), or assumed or deferred shape(:). In the absence
59// of explicit lower bounds it is not possible to distinguish assumed
60// shape bounds from deferred shape bounds without knowing whether the
61// particular symbol is an allocatable/pointer or a non-allocatable
62// non-pointer dummy; use the symbol-based predicates for those
63// determinations.
64class Bound {
65public:
66 static Bound Star() { return Bound(Category::Star); }
67 static Bound Colon() { return Bound(Category::Colon); }
68 explicit Bound(MaybeSubscriptIntExpr &&expr) : expr_{std::move(expr)} {}
69 explicit Bound(common::ConstantSubscript bound);
70 Bound(const Bound &) = default;
71 Bound(Bound &&) = default;
72 Bound &operator=(const Bound &) = default;
73 Bound &operator=(Bound &&) = default;
74 bool isExplicit() const { return category_ == Category::Explicit; }
75 bool isStar() const { return category_ == Category::Star; }
76 bool isColon() const { return category_ == Category::Colon; }
77 MaybeSubscriptIntExpr &GetExplicit() { return expr_; }
78 const MaybeSubscriptIntExpr &GetExplicit() const { return expr_; }
79 void SetExplicit(MaybeSubscriptIntExpr &&expr) {
80 CHECK(isExplicit());
81 expr_ = std::move(expr);
82 }
83
84private:
85 enum class Category { Explicit, Star, Colon };
86 Bound(Category category) : category_{category} {}
87 Bound(Category category, MaybeSubscriptIntExpr &&expr)
88 : category_{category}, expr_{std::move(expr)} {}
89 Category category_{Category::Explicit};
90 MaybeSubscriptIntExpr expr_;
91 friend llvm::raw_ostream &operator<<(llvm::raw_ostream &, const Bound &);
92};
93
94// A type parameter value: integer expression, assumed/implied(*),
95// or deferred(:).
96class ParamValue {
97public:
98 static ParamValue Assumed(common::TypeParamAttr attr) {
99 return ParamValue{Category::Assumed, attr};
100 }
101 static ParamValue Deferred(common::TypeParamAttr attr) {
102 return ParamValue{Category::Deferred, attr};
103 }
104 ParamValue(const ParamValue &) = default;
105 explicit ParamValue(MaybeIntExpr &&, common::TypeParamAttr);
106 explicit ParamValue(SomeIntExpr &&, common::TypeParamAttr attr);
107 explicit ParamValue(common::ConstantSubscript, common::TypeParamAttr attr);
108 bool isExplicit() const { return category_ == Category::Explicit; }
109 bool isAssumed() const { return category_ == Category::Assumed; }
110 bool isDeferred() const { return category_ == Category::Deferred; }
111 const MaybeIntExpr &GetExplicit() const { return expr_; }
112 void SetExplicit(SomeIntExpr &&);
113 bool isKind() const { return attr_ == common::TypeParamAttr::Kind; }
114 bool isLen() const { return attr_ == common::TypeParamAttr::Len; }
115 void set_attr(common::TypeParamAttr attr) { attr_ = attr; }
116 bool operator==(const ParamValue &that) const {
117 return category_ == that.category_ && expr_ == that.expr_;
118 }
119 bool operator!=(const ParamValue &that) const { return !(*this == that); }
120 bool IsEquivalentInInterface(const ParamValue &that) const {
121 return (category_ == that.category_ &&
122 expr_.has_value() == that.expr_.has_value() &&
123 (!expr_ || evaluate::AreEquivalentInInterface(*expr_, *that.expr_)));
124 }
125 std::string AsFortran() const;
126
127private:
128 enum class Category { Explicit, Deferred, Assumed };
129 ParamValue(Category category, common::TypeParamAttr attr)
130 : category_{category}, attr_{attr} {}
131 Category category_{Category::Explicit};
132 common::TypeParamAttr attr_{common::TypeParamAttr::Kind};
133 MaybeIntExpr expr_;
134 friend llvm::raw_ostream &operator<<(llvm::raw_ostream &, const ParamValue &);
135};
136
137class IntrinsicTypeSpec {
138public:
139 TypeCategory category() const { return category_; }
140 const KindExpr &kind() const { return kind_; }
141 bool operator==(const IntrinsicTypeSpec &x) const {
142 return category_ == x.category_ && kind_ == x.kind_;
143 }
144 bool operator!=(const IntrinsicTypeSpec &x) const { return !operator==(x); }
145 std::string AsFortran() const;
146
147protected:
148 IntrinsicTypeSpec(TypeCategory, KindExpr &&);
149
150private:
151 TypeCategory category_;
152 KindExpr kind_;
153 friend llvm::raw_ostream &operator<<(
154 llvm::raw_ostream &os, const IntrinsicTypeSpec &x);
155};
156
157class NumericTypeSpec : public IntrinsicTypeSpec {
158public:
159 NumericTypeSpec(TypeCategory category, KindExpr &&kind)
160 : IntrinsicTypeSpec(category, std::move(kind)) {
161 CHECK(common::IsNumericTypeCategory(category));
162 }
163};
164
165class LogicalTypeSpec : public IntrinsicTypeSpec {
166public:
167 explicit LogicalTypeSpec(KindExpr &&kind)
168 : IntrinsicTypeSpec(TypeCategory::Logical, std::move(kind)) {}
169};
170
171class CharacterTypeSpec : public IntrinsicTypeSpec {
172public:
173 CharacterTypeSpec(ParamValue &&length, KindExpr &&kind)
174 : IntrinsicTypeSpec(TypeCategory::Character, std::move(kind)),
175 length_{std::move(length)} {}
176 const ParamValue &length() const { return length_; }
177 bool operator==(const CharacterTypeSpec &that) const {
178 return kind() == that.kind() && length_ == that.length_;
179 }
180 std::string AsFortran() const;
181
182private:
183 ParamValue length_;
184 friend llvm::raw_ostream &operator<<(
185 llvm::raw_ostream &os, const CharacterTypeSpec &x);
186};
187
188class ShapeSpec {
189public:
190 // lb:ub
191 static ShapeSpec MakeExplicit(Bound &&lb, Bound &&ub) {
192 return ShapeSpec(std::move(lb), std::move(ub));
193 }
194 // 1:ub
195 static const ShapeSpec MakeExplicit(Bound &&ub) {
196 return MakeExplicit(Bound{1}, std::move(ub));
197 }
198 // 1:
199 static ShapeSpec MakeAssumedShape() {
200 return ShapeSpec(Bound{1}, Bound::Colon());
201 }
202 // lb:
203 static ShapeSpec MakeAssumedShape(Bound &&lb) {
204 return ShapeSpec(std::move(lb), Bound::Colon());
205 }
206 // :
207 static ShapeSpec MakeDeferred() {
208 return ShapeSpec(Bound::Colon(), Bound::Colon());
209 }
210 // 1:*
211 static ShapeSpec MakeImplied() { return ShapeSpec(Bound{1}, Bound::Star()); }
212 // lb:*
213 static ShapeSpec MakeImplied(Bound &&lb) {
214 return ShapeSpec(std::move(lb), Bound::Star());
215 }
216 // ..
217 static ShapeSpec MakeAssumedRank() {
218 return ShapeSpec(Bound::Star(), Bound::Star());
219 }
220
221 ShapeSpec(const ShapeSpec &) = default;
222 ShapeSpec(ShapeSpec &&) = default;
223 ShapeSpec &operator=(const ShapeSpec &) = default;
224 ShapeSpec &operator=(ShapeSpec &&) = default;
225
226 Bound &lbound() { return lb_; }
227 const Bound &lbound() const { return lb_; }
228 Bound &ubound() { return ub_; }
229 const Bound &ubound() const { return ub_; }
230
231private:
232 ShapeSpec(Bound &&lb, Bound &&ub) : lb_{std::move(lb)}, ub_{std::move(ub)} {}
233 Bound lb_;
234 Bound ub_;
235 friend llvm::raw_ostream &operator<<(llvm::raw_ostream &, const ShapeSpec &);
236};
237
238struct ArraySpec : public std::vector<ShapeSpec> {
239 ArraySpec() {}
240 int Rank() const { return size(); }
241 // These names are not exclusive, as some categories cannot be
242 // distinguished without knowing whether the particular symbol
243 // is allocatable, pointer, or a non-allocatable non-pointer dummy.
244 // Use the symbol-based predicates for exact results.
245 inline bool IsExplicitShape() const;
246 inline bool CanBeAssumedShape() const;
247 inline bool CanBeDeferredShape() const;
248 inline bool CanBeImpliedShape() const;
249 inline bool CanBeAssumedSize() const;
250 inline bool IsAssumedRank() const;
251
252private:
253 // Check non-empty and predicate is true for each element.
254 template <typename P> bool CheckAll(P predicate) const {
255 return !empty() && std::all_of(begin(), end(), predicate);
256 }
257};
258llvm::raw_ostream &operator<<(llvm::raw_ostream &, const ArraySpec &);
259
260// Each DerivedTypeSpec has a typeSymbol that has DerivedTypeDetails.
261// The name may not match the symbol's name in case of a USE rename.
262class DerivedTypeSpec {
263public:
264 enum class Category { DerivedType, IntrinsicVector, PairVector, QuadVector };
265
266 using RawParameter = std::pair<const parser::Keyword *, ParamValue>;
267 using RawParameters = std::vector<RawParameter>;
268 using ParameterMapType = std::map<SourceName, ParamValue>;
269 DerivedTypeSpec(SourceName, const Symbol &);
270 DerivedTypeSpec(const DerivedTypeSpec &);
271 DerivedTypeSpec(DerivedTypeSpec &&);
272
273 const SourceName &name() const { return name_; }
274 const Symbol &originalTypeSymbol() const { return originalTypeSymbol_; }
275 const Symbol &typeSymbol() const { return typeSymbol_; }
276 const Scope *scope() const { return scope_; }
277 // Return scope_ if it is set, or the typeSymbol_ scope otherwise.
278 const Scope *GetScope() const;
279 void set_scope(const Scope &);
280 void ReplaceScope(const Scope &);
281 const RawParameters &rawParameters() const { return rawParameters_; }
282 const ParameterMapType &parameters() const { return parameters_; }
283
284 bool MightBeParameterized() const;
285 bool IsForwardReferenced() const;
286 bool HasDefaultInitialization(
287 bool ignoreAllocatable = false, bool ignorePointer = true) const;
288 std::optional<std::string> // component path suitable for error messages
289 ComponentWithDefaultInitialization(
290 bool ignoreAllocatable = false, bool ignorePointer = true) const;
291 bool HasDestruction() const;
292
293 // The "raw" type parameter list is a simple transcription from the
294 // parameter list in the parse tree, built by calling AddRawParamValue().
295 // It can be used with forward-referenced derived types.
296 void AddRawParamValue(const parser::Keyword *, ParamValue &&);
297 // Checks the raw parameter list against the definition of a derived type.
298 // Converts the raw parameter list to a map, naming each actual parameter.
299 void CookParameters(evaluate::FoldingContext &);
300 // Evaluates type parameter expressions.
301 void EvaluateParameters(SemanticsContext &);
302 void AddParamValue(SourceName, ParamValue &&);
303 // Creates a Scope for the type and populates it with component
304 // instantiations that have been specialized with actual type parameter
305 // values, which are cooked &/or evaluated if necessary.
306 void Instantiate(Scope &containingScope);
307
308 ParamValue *FindParameter(SourceName);
309 const ParamValue *FindParameter(SourceName target) const {
310 auto iter{parameters_.find(target)};
311 if (iter != parameters_.end()) {
312 return &iter->second;
313 } else {
314 return nullptr;
315 }
316 }
317 bool operator==(const DerivedTypeSpec &that) const {
318 return RawEquals(that) && parameters_ == that.parameters_;
319 }
320 bool operator!=(const DerivedTypeSpec &that) const {
321 return !(*this == that);
322 }
323 // For TYPE IS & CLASS IS: kind type parameters must be
324 // explicit and equal, len type parameters are ignored.
325 bool MatchesOrExtends(const DerivedTypeSpec &) const;
326 std::string AsFortran() const;
327 std::string VectorTypeAsFortran() const;
328
329 Category category() const { return category_; }
330 void set_category(Category category) { category_ = category; }
331 bool IsVectorType() const {
332 return category_ == Category::IntrinsicVector ||
333 category_ == Category::PairVector || category_ == Category::QuadVector;
334 }
335
336private:
337 SourceName name_;
338 const Symbol &originalTypeSymbol_;
339 const Symbol &typeSymbol_; // == originalTypeSymbol_.GetUltimate()
340 const Scope *scope_{nullptr}; // same as typeSymbol_.scope() unless PDT
341 bool cooked_{false};
342 bool evaluated_{false};
343 bool instantiated_{false};
344 RawParameters rawParameters_;
345 ParameterMapType parameters_;
346 Category category_{Category::DerivedType};
347 bool RawEquals(const DerivedTypeSpec &that) const {
348 return &typeSymbol_ == &that.typeSymbol_ &&
349 &originalTypeSymbol_ == &that.originalTypeSymbol_ &&
350 cooked_ == that.cooked_ && rawParameters_ == that.rawParameters_;
351 }
352 friend llvm::raw_ostream &operator<<(
353 llvm::raw_ostream &, const DerivedTypeSpec &);
354};
355
356class DeclTypeSpec {
357public:
358 enum Category {
359 Numeric,
360 Logical,
361 Character,
362 TypeDerived,
363 ClassDerived,
364 TypeStar,
365 ClassStar
366 };
367
368 // intrinsic-type-spec or TYPE(intrinsic-type-spec), not character
369 DeclTypeSpec(NumericTypeSpec &&);
370 DeclTypeSpec(LogicalTypeSpec &&);
371 // character
372 DeclTypeSpec(const CharacterTypeSpec &);
373 DeclTypeSpec(CharacterTypeSpec &&);
374 // TYPE(derived-type-spec) or CLASS(derived-type-spec)
375 DeclTypeSpec(Category, const DerivedTypeSpec &);
376 DeclTypeSpec(Category, DerivedTypeSpec &&);
377 // TYPE(*) or CLASS(*)
378 DeclTypeSpec(Category);
379
380 bool operator==(const DeclTypeSpec &) const;
381 bool operator!=(const DeclTypeSpec &that) const { return !operator==(that); }
382
383 Category category() const { return category_; }
384 void set_category(Category category) { category_ = category; }
385 bool IsPolymorphic() const {
386 return category_ == ClassDerived || IsUnlimitedPolymorphic();
387 }
388 bool IsUnlimitedPolymorphic() const {
389 return category_ == TypeStar || category_ == ClassStar;
390 }
391 bool IsAssumedType() const { return category_ == TypeStar; }
392 bool IsNumeric(TypeCategory) const;
393 bool IsSequenceType() const;
394 const NumericTypeSpec &numericTypeSpec() const;
395 const LogicalTypeSpec &logicalTypeSpec() const;
396 const CharacterTypeSpec &characterTypeSpec() const {
397 CHECK(category_ == Character);
398 return std::get<CharacterTypeSpec>(typeSpec_);
399 }
400 const DerivedTypeSpec &derivedTypeSpec() const {
401 CHECK(category_ == TypeDerived || category_ == ClassDerived);
402 return std::get<DerivedTypeSpec>(typeSpec_);
403 }
404 DerivedTypeSpec &derivedTypeSpec() {
405 CHECK(category_ == TypeDerived || category_ == ClassDerived);
406 return std::get<DerivedTypeSpec>(typeSpec_);
407 }
408
409 inline IntrinsicTypeSpec *AsIntrinsic();
410 inline const IntrinsicTypeSpec *AsIntrinsic() const;
411 inline DerivedTypeSpec *AsDerived();
412 inline const DerivedTypeSpec *AsDerived() const;
413
414 std::string AsFortran() const;
415
416private:
417 Category category_;
418 std::variant<std::monostate, NumericTypeSpec, LogicalTypeSpec,
420 typeSpec_;
421};
422llvm::raw_ostream &operator<<(llvm::raw_ostream &, const DeclTypeSpec &);
423
424// Define some member functions here in the header so that they can be used by
425// lib/Evaluate without link-time dependency on Semantics.
426
427inline bool ArraySpec::IsExplicitShape() const {
428 return CheckAll([](const ShapeSpec &x) { return x.ubound().isExplicit(); });
429}
430inline bool ArraySpec::CanBeAssumedShape() const {
431 return CheckAll([](const ShapeSpec &x) { return x.ubound().isColon(); });
432}
433inline bool ArraySpec::CanBeDeferredShape() const {
434 return CheckAll([](const ShapeSpec &x) {
435 return x.lbound().isColon() && x.ubound().isColon();
436 });
437}
438inline bool ArraySpec::CanBeImpliedShape() const {
439 return !IsAssumedRank() &&
440 CheckAll([](const ShapeSpec &x) { return x.ubound().isStar(); });
441}
442inline bool ArraySpec::CanBeAssumedSize() const {
443 return !empty() && !IsAssumedRank() && back().ubound().isStar() &&
444 std::all_of(begin(), end() - 1,
445 [](const ShapeSpec &x) { return x.ubound().isExplicit(); });
446}
447inline bool ArraySpec::IsAssumedRank() const {
448 return Rank() == 1 && front().lbound().isStar();
449}
450
451inline IntrinsicTypeSpec *DeclTypeSpec::AsIntrinsic() {
452 switch (category_) {
453 case Numeric:
454 return &std::get<NumericTypeSpec>(typeSpec_);
455 case Logical:
456 return &std::get<LogicalTypeSpec>(typeSpec_);
457 case Character:
458 return &std::get<CharacterTypeSpec>(typeSpec_);
459 default:
460 return nullptr;
461 }
462}
463inline const IntrinsicTypeSpec *DeclTypeSpec::AsIntrinsic() const {
464 return const_cast<DeclTypeSpec *>(this)->AsIntrinsic();
465}
466
467inline DerivedTypeSpec *DeclTypeSpec::AsDerived() {
468 switch (category_) {
469 case TypeDerived:
470 case ClassDerived:
471 return &std::get<DerivedTypeSpec>(typeSpec_);
472 default:
473 return nullptr;
474 }
475}
476inline const DerivedTypeSpec *DeclTypeSpec::AsDerived() const {
477 return const_cast<DeclTypeSpec *>(this)->AsDerived();
478}
479
480} // namespace Fortran::semantics
481#endif // FORTRAN_SEMANTICS_TYPE_H_
Definition common.h:214
Definition common.h:216
Definition type.h:64
Definition type.h:96
Definition scope.h:58
Definition semantics.h:67
Definition type.h:188
Definition symbol.h:781
Definition call.h:34
Definition check-expression.h:19
Definition type.h:238