FLANG
resolve-names-utils.h
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
14#include "flang/Evaluate/fold.h"
15#include "flang/Parser/message.h"
16#include "flang/Parser/tools.h"
17#include "flang/Semantics/expression.h"
18#include "flang/Semantics/scope.h"
19#include "flang/Semantics/semantics.h"
20#include "flang/Semantics/symbol.h"
21#include "flang/Semantics/type.h"
22#include "llvm/Support/raw_ostream.h"
23
24namespace Fortran::parser {
25class CharBlock;
26struct ArraySpec;
27struct CoarraySpec;
29struct DataRef;
30struct DefinedOpName;
31struct Designator;
32struct Expr;
33struct GenericSpec;
34struct Name;
35} // namespace Fortran::parser
36
37namespace Fortran::semantics {
38
39using SourceName = parser::CharBlock;
41
42// Record that a Name has been resolved to a Symbol
43Symbol &Resolve(const parser::Name &, Symbol &);
44Symbol *Resolve(const parser::Name &, Symbol *);
45
46// Create a copy of msg with a new severity.
47parser::MessageFixedText WithSeverity(
48 const parser::MessageFixedText &msg, parser::Severity);
49
50bool IsIntrinsicOperator(const SemanticsContext &, const SourceName &);
51bool IsLogicalConstant(const SemanticsContext &, const SourceName &);
52
53template <typename T>
54MaybeIntExpr EvaluateIntExpr(SemanticsContext &context, const T &expr) {
55 if (MaybeExpr maybeExpr{
56 Fold(context.foldingContext(), AnalyzeExpr(context, expr))}) {
57 if (auto *intExpr{evaluate::UnwrapExpr<SomeIntExpr>(*maybeExpr)}) {
58 return std::move(*intExpr);
59 }
60 }
61 return std::nullopt;
62}
63
64template <typename T>
65std::optional<std::int64_t> EvaluateInt64(
66 SemanticsContext &context, const T &expr) {
67 return evaluate::ToInt64(EvaluateIntExpr(context, expr));
68}
69
70// Analyze a generic-spec and generate a symbol name and GenericKind for it.
71class GenericSpecInfo {
72public:
73 explicit GenericSpecInfo(const parser::DefinedOpName &x) { Analyze(x); }
74 explicit GenericSpecInfo(const parser::GenericSpec &x) { Analyze(x); }
75
76 GenericKind kind() const { return kind_; }
77 const SourceName &symbolName() const { return symbolName_.value(); }
78 // Set the GenericKind in this symbol and resolve the corresponding
79 // name if there is one
80 void Resolve(Symbol *) const;
81 friend llvm::raw_ostream &operator<<(
82 llvm::raw_ostream &, const GenericSpecInfo &);
83
84private:
85 void Analyze(const parser::DefinedOpName &);
86 void Analyze(const parser::GenericSpec &);
87
88 GenericKind kind_;
89 const parser::Name *parseName_{nullptr};
90 std::optional<SourceName> symbolName_;
91};
92
93// Analyze a parser::ArraySpec or parser::CoarraySpec
94ArraySpec AnalyzeArraySpec(SemanticsContext &, const parser::ArraySpec &);
95ArraySpec AnalyzeArraySpec(
97ArraySpec AnalyzeDeferredShapeSpecList(
98 SemanticsContext &, const parser::DeferredShapeSpecList &);
99ArraySpec AnalyzeCoarraySpec(
100 SemanticsContext &context, const parser::CoarraySpec &);
101
102// Perform consistency checks on equivalence sets
103class EquivalenceSets {
104public:
105 EquivalenceSets(SemanticsContext &context) : context_{context} {}
106 std::vector<EquivalenceSet> &sets() { return sets_; };
107 // Resolve this designator and add to the current equivalence set
108 void AddToSet(const parser::Designator &);
109 // Finish the current equivalence set: determine if it overlaps
110 // with any of the others and perform necessary merges if it does.
111 void FinishSet(const parser::CharBlock &);
112
113private:
114 bool CheckCanEquivalence(
115 const parser::CharBlock &, const Symbol &, const Symbol &);
116 void MergeInto(const parser::CharBlock &, EquivalenceSet &, std::size_t);
117 const EquivalenceObject *Find(const EquivalenceSet &, const Symbol &);
118 bool CheckDesignator(const parser::Designator &);
119 bool CheckDataRef(const parser::CharBlock &, const parser::DataRef &);
120 bool CheckObject(const parser::Name &);
121 bool CheckArrayBound(const parser::Expr &);
122 bool CheckSubstringBound(const parser::Expr &, bool);
123 bool IsCharacterSequenceType(const DeclTypeSpec *);
124 bool IsDefaultKindNumericType(const IntrinsicTypeSpec &);
125 bool IsDefaultNumericSequenceType(const DeclTypeSpec *);
126 static bool IsAnyNumericSequenceType(const DeclTypeSpec *);
127 static bool IsSequenceType(
128 const DeclTypeSpec *, std::function<bool(const IntrinsicTypeSpec &)>);
129
130 SemanticsContext &context_;
131 std::vector<EquivalenceSet> sets_; // all equivalence sets in this scope
132 // Map object to index of set it is in
133 std::map<EquivalenceObject, std::size_t> objectToSet_;
134 EquivalenceSet currSet_; // equivalence set currently being constructed
135 struct {
136 Symbol *symbol{nullptr};
137 std::vector<ConstantSubscript> subscripts;
138 std::optional<ConstantSubscript> substringStart;
139 } currObject_; // equivalence object currently being constructed
140};
141
142// Duplicates a subprogram's dummy arguments and result, if any, and
143// maps all of the symbols in their expressions.
145void MapSubprogramToNewSymbols(const Symbol &oldSymbol, Symbol &newSymbol,
146 Scope &newScope, SymbolAndTypeMappings * = nullptr);
147
148parser::CharBlock MakeNameFromOperator(
149 const parser::DefinedOperator::IntrinsicOperator &op,
150 SemanticsContext &context);
151parser::CharBlock MangleSpecialFunctions(const parser::CharBlock &name);
152std::string MangleDefinedOperator(const parser::CharBlock &name);
153
154// Map a mangled declare reduction name (e.g., "op.+", "op.combine.",
155// "op.max") back to the Fortran identifier used as the scope key for the
156// corresponding operator or procedure (e.g., "operator(+)", ".combine.",
157// "max"). Non-mangled names (procedure designators) are returned as-is.
158std::string GetReductionFortranId(const parser::CharBlock &mangledName);
159
160} // namespace Fortran::semantics
161#endif // FORTRAN_SEMANTICS_RESOLVE_NAMES_H_
Definition char-block.h:26
Definition scope.h:68
Definition semantics.h:67
Definition symbol.h:893
Definition check-expression.h:19
Definition parse-tree.h:1371
Definition parse-tree.h:977
Definition parse-tree.h:988
Definition parse-tree.h:1848
Definition parse-tree.h:1887
Definition parse-tree.h:1735
Definition parse-tree.h:3074
Definition parse-tree.h:591
Definition type.h:239
Definition symbol.h:743
Definition resolve-names-utils.cpp:743