173
|
1 //===-- lib/Semantics/resolve-names-utils.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_RESOLVE_NAMES_UTILS_H_
|
|
10 #define FORTRAN_SEMANTICS_RESOLVE_NAMES_UTILS_H_
|
|
11
|
|
12 // Utility functions and class for use in resolve-names.cpp.
|
|
13
|
207
|
14 #include "flang/Evaluate/fold.h"
|
173
|
15 #include "flang/Parser/message.h"
|
207
|
16 #include "flang/Parser/tools.h"
|
|
17 #include "flang/Semantics/expression.h"
|
173
|
18 #include "flang/Semantics/scope.h"
|
207
|
19 #include "flang/Semantics/semantics.h"
|
173
|
20 #include "flang/Semantics/symbol.h"
|
|
21 #include "flang/Semantics/type.h"
|
207
|
22 #include "llvm/Support/raw_ostream.h"
|
173
|
23 #include <forward_list>
|
|
24
|
|
25 namespace Fortran::parser {
|
|
26 class CharBlock;
|
|
27 struct ArraySpec;
|
|
28 struct CoarraySpec;
|
|
29 struct ComponentArraySpec;
|
|
30 struct DataRef;
|
|
31 struct DefinedOpName;
|
|
32 struct Designator;
|
|
33 struct Expr;
|
|
34 struct GenericSpec;
|
|
35 struct Name;
|
|
36 } // namespace Fortran::parser
|
|
37
|
|
38 namespace Fortran::semantics {
|
|
39
|
|
40 using SourceName = parser::CharBlock;
|
|
41 class SemanticsContext;
|
|
42
|
|
43 // Record that a Name has been resolved to a Symbol
|
|
44 Symbol &Resolve(const parser::Name &, Symbol &);
|
|
45 Symbol *Resolve(const parser::Name &, Symbol *);
|
|
46
|
|
47 // Create a copy of msg with a new isFatal value.
|
|
48 parser::MessageFixedText WithIsFatal(
|
|
49 const parser::MessageFixedText &msg, bool isFatal);
|
|
50
|
|
51 bool IsIntrinsicOperator(const SemanticsContext &, const SourceName &);
|
|
52 bool IsLogicalConstant(const SemanticsContext &, const SourceName &);
|
|
53
|
207
|
54 // Some intrinsic operators have more than one name (e.g. `operator(.eq.)` and
|
|
55 // `operator(==)`). GetAllNames() returns them all, including symbolName.
|
|
56 std::forward_list<std::string> GetAllNames(
|
|
57 const SemanticsContext &, const SourceName &);
|
|
58
|
|
59 template <typename T>
|
|
60 MaybeIntExpr EvaluateIntExpr(SemanticsContext &context, const T &expr) {
|
|
61 if (MaybeExpr maybeExpr{
|
|
62 Fold(context.foldingContext(), AnalyzeExpr(context, expr))}) {
|
|
63 if (auto *intExpr{evaluate::UnwrapExpr<SomeIntExpr>(*maybeExpr)}) {
|
|
64 return std::move(*intExpr);
|
|
65 }
|
|
66 }
|
|
67 return std::nullopt;
|
|
68 }
|
|
69
|
|
70 template <typename T>
|
|
71 std::optional<std::int64_t> EvaluateInt64(
|
|
72 SemanticsContext &context, const T &expr) {
|
|
73 return evaluate::ToInt64(EvaluateIntExpr(context, expr));
|
|
74 }
|
|
75
|
173
|
76 // Analyze a generic-spec and generate a symbol name and GenericKind for it.
|
|
77 class GenericSpecInfo {
|
|
78 public:
|
|
79 GenericSpecInfo(const parser::DefinedOpName &x) { Analyze(x); }
|
|
80 GenericSpecInfo(const parser::GenericSpec &x) { Analyze(x); }
|
|
81
|
|
82 GenericKind kind() const { return kind_; }
|
|
83 const SourceName &symbolName() const { return symbolName_.value(); }
|
|
84 // Set the GenericKind in this symbol and resolve the corresponding
|
|
85 // name if there is one
|
|
86 void Resolve(Symbol *) const;
|
207
|
87 friend llvm::raw_ostream &operator<<(
|
|
88 llvm::raw_ostream &, const GenericSpecInfo &);
|
173
|
89
|
|
90 private:
|
|
91 GenericKind kind_;
|
|
92 const parser::Name *parseName_{nullptr};
|
|
93 std::optional<SourceName> symbolName_;
|
|
94
|
|
95 void Analyze(const parser::DefinedOpName &);
|
|
96 void Analyze(const parser::GenericSpec &);
|
|
97 };
|
|
98
|
|
99 // Analyze a parser::ArraySpec or parser::CoarraySpec
|
|
100 ArraySpec AnalyzeArraySpec(SemanticsContext &, const parser::ArraySpec &);
|
|
101 ArraySpec AnalyzeArraySpec(
|
|
102 SemanticsContext &, const parser::ComponentArraySpec &);
|
207
|
103 ArraySpec AnalyzeDeferredShapeSpecList(
|
|
104 SemanticsContext &, const parser::DeferredShapeSpecList &);
|
173
|
105 ArraySpec AnalyzeCoarraySpec(
|
|
106 SemanticsContext &context, const parser::CoarraySpec &);
|
|
107
|
|
108 // Perform consistency checks on equivalence sets
|
|
109 class EquivalenceSets {
|
|
110 public:
|
|
111 EquivalenceSets(SemanticsContext &context) : context_{context} {}
|
|
112 std::vector<EquivalenceSet> &sets() { return sets_; };
|
|
113 // Resolve this designator and add to the current equivalence set
|
|
114 void AddToSet(const parser::Designator &);
|
|
115 // Finish the current equivalence set: determine if it overlaps
|
|
116 // with any of the others and perform necessary merges if it does.
|
|
117 void FinishSet(const parser::CharBlock &);
|
|
118
|
|
119 private:
|
|
120 bool CheckCanEquivalence(
|
|
121 const parser::CharBlock &, const Symbol &, const Symbol &);
|
|
122 void MergeInto(const parser::CharBlock &, EquivalenceSet &, std::size_t);
|
|
123 const EquivalenceObject *Find(const EquivalenceSet &, const Symbol &);
|
|
124 bool CheckDesignator(const parser::Designator &);
|
|
125 bool CheckDataRef(const parser::CharBlock &, const parser::DataRef &);
|
|
126 bool CheckObject(const parser::Name &);
|
|
127 bool CheckArrayBound(const parser::Expr &);
|
|
128 bool CheckSubstringBound(const parser::Expr &, bool);
|
|
129 bool IsCharacterSequenceType(const DeclTypeSpec *);
|
|
130 bool IsDefaultKindNumericType(const IntrinsicTypeSpec &);
|
|
131 bool IsNumericSequenceType(const DeclTypeSpec *);
|
|
132 bool IsSequenceType(
|
|
133 const DeclTypeSpec *, std::function<bool(const IntrinsicTypeSpec &)>);
|
|
134
|
|
135 SemanticsContext &context_;
|
|
136 std::vector<EquivalenceSet> sets_; // all equivalence sets in this scope
|
|
137 // Map object to index of set it is in
|
|
138 std::map<EquivalenceObject, std::size_t> objectToSet_;
|
|
139 EquivalenceSet currSet_; // equivalence set currently being constructed
|
|
140 struct {
|
|
141 Symbol *symbol{nullptr};
|
|
142 std::vector<ConstantSubscript> subscripts;
|
|
143 std::optional<ConstantSubscript> substringStart;
|
|
144 } currObject_; // equivalence object currently being constructed
|
|
145 };
|
|
146
|
|
147 } // namespace Fortran::semantics
|
|
148 #endif // FORTRAN_SEMANTICS_RESOLVE_NAMES_H_
|