FLANG
AbstractConverter.h
1//===-- Lower/AbstractConverter.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// Coding style: https://mlir.llvm.org/getting_started/DeveloperGuide/
10//
11//===----------------------------------------------------------------------===//
12
13#ifndef FORTRAN_LOWER_ABSTRACTCONVERTER_H
14#define FORTRAN_LOWER_ABSTRACTCONVERTER_H
15
16#include "flang/Common/Fortran.h"
18#include "flang/Lower/PFTDefs.h"
19#include "flang/Optimizer/Builder/BoxValue.h"
20#include "flang/Optimizer/Dialect/FIRAttr.h"
21#include "flang/Semantics/symbol.h"
22#include "mlir/IR/Builders.h"
23#include "mlir/IR/BuiltinOps.h"
24#include "mlir/IR/Operation.h"
25#include "llvm/ADT/ArrayRef.h"
26
27namespace mlir {
28class SymbolTable;
29}
30
31namespace fir {
32class KindMapping;
33class FirOpBuilder;
34} // namespace fir
35
36namespace Fortran {
37namespace common {
38template <typename>
39class Reference;
40}
41
42namespace evaluate {
43struct DataRef;
44template <typename>
45class Expr;
46class FoldingContext;
47struct SomeType;
48} // namespace evaluate
49
50namespace parser {
51class CharBlock;
52}
53namespace semantics {
54class Symbol;
55class Scope;
56class DerivedTypeSpec;
57} // namespace semantics
58
59namespace lower {
60class SymMap;
61struct SymbolBox;
62namespace pft {
63struct Variable;
64struct FunctionLikeUnit;
65} // namespace pft
66
69using TypeConstructionStack =
70 llvm::DenseMap<const Fortran::semantics::Scope *, mlir::Type>;
72
73using ExprToValueMap = llvm::DenseMap<const SomeExpr *, mlir::Value>;
74
75//===----------------------------------------------------------------------===//
76// AbstractConverter interface
77//===----------------------------------------------------------------------===//
78
83public:
84 //===--------------------------------------------------------------------===//
85 // Symbols
86 //===--------------------------------------------------------------------===//
87
89 virtual mlir::Value getSymbolAddress(SymbolRef sym) = 0;
90
91 virtual fir::ExtendedValue
92 symBoxToExtendedValue(const Fortran::lower::SymbolBox &symBox) = 0;
93
94 virtual fir::ExtendedValue
95 getSymbolExtendedValue(const Fortran::semantics::Symbol &sym,
96 Fortran::lower::SymMap *symMap = nullptr) = 0;
97
99 virtual mlir::Value impliedDoBinding(llvm::StringRef name) = 0;
100
102 virtual void copySymbolBinding(SymbolRef src, SymbolRef target) = 0;
103
106 virtual void bindSymbol(SymbolRef sym, const fir::ExtendedValue &exval) = 0;
107
112 virtual void overrideExprValues(const ExprToValueMap *) = 0;
113 void resetExprOverrides() { overrideExprValues(nullptr); }
114 virtual const ExprToValueMap *getExprOverrides() = 0;
115
117 virtual bool lookupLabelSet(SymbolRef sym, pft::LabelSet &labelSet) = 0;
118
120 virtual pft::Evaluation *lookupLabel(pft::Label label) = 0;
121
126 virtual bool
128 bool skipDefaultInit) = 0;
129
130 virtual void
131 createHostAssociateVarCloneDealloc(const Fortran::semantics::Symbol &sym) = 0;
132
141 virtual void
143 mlir::OpBuilder::InsertPoint *copyAssignIP = nullptr,
144 bool hostIsSource = true) = 0;
145
146 virtual void copyVar(mlir::Location loc, mlir::Value dst, mlir::Value src,
147 fir::FortranVariableFlagsEnum attrs) = 0;
148
151 virtual bool
153
161 virtual void collectSymbolSet(
162 pft::Evaluation &eval,
163 llvm::SetVector<const Fortran::semantics::Symbol *> &symbolSet,
164 Fortran::semantics::Symbol::Flag flag, bool collectSymbols = true,
165 bool collectHostAssociatedSymbols = false) = 0;
166
173 virtual llvm::StringRef
174 getUniqueLitName(mlir::Location loc,
175 std::unique_ptr<Fortran::lower::SomeExpr> expression,
176 mlir::Type eleTy) = 0;
177
178 //===--------------------------------------------------------------------===//
179 // Expressions
180 //===--------------------------------------------------------------------===//
181
187 StatementContext &context,
188 mlir::Location *locPtr = nullptr) = 0;
189
191 fir::ExtendedValue genExprAddr(mlir::Location loc, const SomeExpr *expr,
192 StatementContext &stmtCtx) {
193 return genExprAddr(*expr, stmtCtx, &loc);
194 }
195 fir::ExtendedValue genExprAddr(mlir::Location loc, const SomeExpr &expr,
196 StatementContext &stmtCtx) {
197 return genExprAddr(expr, stmtCtx, &loc);
198 }
199
202 StatementContext &context,
203 mlir::Location *locPtr = nullptr) = 0;
204
206 fir::ExtendedValue genExprValue(mlir::Location loc, const SomeExpr *expr,
207 StatementContext &stmtCtx) {
208 return genExprValue(*expr, stmtCtx, &loc);
209 }
210 fir::ExtendedValue genExprValue(mlir::Location loc, const SomeExpr &expr,
211 StatementContext &stmtCtx) {
212 return genExprValue(expr, stmtCtx, &loc);
213 }
214
218 virtual fir::ExtendedValue genExprBox(mlir::Location loc,
219 const SomeExpr &expr,
220 StatementContext &stmtCtx) = 0;
221
225 virtual fir::MutableBoxValue genExprMutableBox(mlir::Location loc,
226 const SomeExpr &expr) = 0;
227
231
234 virtual mlir::Value hostAssocTupleValue() = 0;
235
238 virtual void bindHostAssocTuple(mlir::Value val) = 0;
239
243 virtual mlir::Value dummyArgsScopeValue() const = 0;
244
249 virtual bool
251
255
256 //===--------------------------------------------------------------------===//
257 // Types
258 //===--------------------------------------------------------------------===//
259
261 virtual mlir::Type genType(const SomeExpr &) = 0;
263 virtual mlir::Type genType(SymbolRef) = 0;
265 virtual mlir::Type genType(Fortran::common::TypeCategory tc) = 0;
267 virtual mlir::Type
268 genType(Fortran::common::TypeCategory tc, int kind,
269 llvm::ArrayRef<std::int64_t> lenParameters = std::nullopt) = 0;
271 virtual mlir::Type genType(const Fortran::semantics::DerivedTypeSpec &) = 0;
273 virtual mlir::Type genType(const pft::Variable &) = 0;
274
277 virtual void
278 registerTypeInfo(mlir::Location loc, SymbolRef typeInfoSym,
280 fir::RecordType type) = 0;
281
284 virtual TypeConstructionStack &getTypeConstructionStack() = 0;
285
286 //===--------------------------------------------------------------------===//
287 // Locations
288 //===--------------------------------------------------------------------===//
289
291 virtual mlir::Location getCurrentLocation() = 0;
293 virtual mlir::Location genUnknownLocation() = 0;
295 virtual mlir::Location genLocation(const Fortran::parser::CharBlock &) = 0;
296
299
300 //===--------------------------------------------------------------------===//
301 // FIR/MLIR
302 //===--------------------------------------------------------------------===//
303
307 virtual mlir::ModuleOp getModuleOp() = 0;
309 virtual mlir::MLIRContext &getMLIRContext() = 0;
311 virtual std::string mangleName(const Fortran::semantics::Symbol &) = 0;
313 virtual std::string
316 virtual std::string mangleName(std::string &) = 0;
319 virtual std::string
321
323 virtual const fir::KindMapping &getKindMap() = 0;
324
325 virtual Fortran::lower::StatementContext &getFctCtx() = 0;
326
328 : loweringOptions(loweringOptions) {}
329 virtual ~AbstractConverter() = default;
330
331 //===--------------------------------------------------------------------===//
332 // Miscellaneous
333 //===--------------------------------------------------------------------===//
334
336 virtual void genEval(pft::Evaluation &eval,
337 bool unstructuredContext = true) = 0;
338
341 return loweringOptions;
342 }
343
348
356 virtual mlir::SymbolTable *getMLIRSymbolTable() = 0;
357
358private:
360 const Fortran::lower::LoweringOptions &loweringOptions;
361};
362
363} // namespace lower
364} // namespace Fortran
365
366#endif // FORTRAN_LOWER_ABSTRACTCONVERTER_H
Definition: common.h:215
Definition: AbstractConverter.h:82
virtual mlir::Value impliedDoBinding(llvm::StringRef name)=0
Get the binding of an implied do variable by name.
virtual void copyHostAssociateVar(const Fortran::semantics::Symbol &sym, mlir::OpBuilder::InsertPoint *copyAssignIP=nullptr, bool hostIsSource=true)=0
virtual mlir::Location genLocation(const Fortran::parser::CharBlock &)=0
Generate the location as converted from a CharBlock.
virtual pft::Evaluation * lookupLabel(pft::Label label)=0
Get the code defined by a label.
virtual mlir::MLIRContext & getMLIRContext()=0
Get the MLIRContext.
virtual fir::MutableBoxValue genExprMutableBox(mlir::Location loc, const SomeExpr &expr)=0
fir::ExtendedValue genExprAddr(mlir::Location loc, const SomeExpr *expr, StatementContext &stmtCtx)
Generate the address of the location holding the expression, expr.
Definition: AbstractConverter.h:191
virtual mlir::Value getSymbolAddress(SymbolRef sym)=0
Get the mlir instance of a symbol.
virtual mlir::Location getCurrentLocation()=0
Get the converter's current location.
virtual Fortran::lower::SymbolBox lookupOneLevelUpSymbol(const Fortran::semantics::Symbol &sym)=0
virtual llvm::StringRef getUniqueLitName(mlir::Location loc, std::unique_ptr< Fortran::lower::SomeExpr > expression, mlir::Type eleTy)=0
virtual std::string mangleName(std::string &)=0
Unique a compiler generated name (add a containing scope specific prefix)
virtual mlir::Location genUnknownLocation()=0
Generate a dummy location.
virtual mlir::ModuleOp getModuleOp()=0
Get the ModuleOp.
virtual bool createHostAssociateVarClone(const Fortran::semantics::Symbol &sym, bool skipDefaultInit)=0
virtual mlir::Type genType(const SomeExpr &)=0
Generate the type of an Expr.
virtual bool isPresentShallowLookup(const Fortran::semantics::Symbol &sym)=0
virtual fir::ExtendedValue genExprValue(const SomeExpr &expr, StatementContext &context, mlir::Location *locPtr=nullptr)=0
Generate the computations of the expression to produce a value.
virtual fir::ExtendedValue genExprBox(mlir::Location loc, const SomeExpr &expr, StatementContext &stmtCtx)=0
virtual void overrideExprValues(const ExprToValueMap *)=0
virtual fir::FirOpBuilder & getFirOpBuilder()=0
Get the OpBuilder.
virtual mlir::Type genType(SymbolRef)=0
Generate the type of a Symbol.
virtual void registerTypeInfo(mlir::Location loc, SymbolRef typeInfoSym, const Fortran::semantics::DerivedTypeSpec &typeSpec, fir::RecordType type)=0
virtual mlir::Type genType(Fortran::common::TypeCategory tc, int kind, llvm::ArrayRef< std::int64_t > lenParameters=std::nullopt)=0
Generate the type from a category and kind and length parameters.
virtual mlir::Type genType(const pft::Variable &)=0
Generate the type from a Variable.
virtual const Fortran::semantics::Scope & getCurrentScope()=0
Get the converter's current scope.
virtual std::string getRecordTypeFieldName(const Fortran::semantics::Symbol &component)=0
virtual void genEval(pft::Evaluation &eval, bool unstructuredContext=true)=0
Generate IR for Evaluation eval.
virtual TypeConstructionStack & getTypeConstructionStack()=0
virtual void bindHostAssocTuple(mlir::Value val)=0
virtual const Fortran::lower::pft::FunctionLikeUnit * getCurrentFunctionUnit() const =0
Returns the FunctionLikeUnit being lowered, if any.
virtual mlir::Value hostAssocTupleValue()=0
virtual void copySymbolBinding(SymbolRef src, SymbolRef target)=0
Copy the binding of src to target symbol.
virtual Fortran::evaluate::FoldingContext & getFoldingContext()=0
virtual void bindSymbol(SymbolRef sym, const fir::ExtendedValue &exval)=0
virtual mlir::SymbolTable * getMLIRSymbolTable()=0
virtual mlir::Type genType(const Fortran::semantics::DerivedTypeSpec &)=0
Generate the type from a DerivedTypeSpec.
virtual mlir::Type genType(Fortran::common::TypeCategory tc)=0
Generate the type from a category.
virtual mlir::Value dummyArgsScopeValue() const =0
virtual void collectSymbolSet(pft::Evaluation &eval, llvm::SetVector< const Fortran::semantics::Symbol * > &symbolSet, Fortran::semantics::Symbol::Flag flag, bool collectSymbols=true, bool collectHostAssociatedSymbols=false)=0
virtual fir::ExtendedValue genExprAddr(const SomeExpr &expr, StatementContext &context, mlir::Location *locPtr=nullptr)=0
virtual bool isRegisteredDummySymbol(Fortran::semantics::SymbolRef symRef) const =0
fir::ExtendedValue genExprValue(mlir::Location loc, const SomeExpr *expr, StatementContext &stmtCtx)
Generate the computations of the expression, expr, to produce a value.
Definition: AbstractConverter.h:206
virtual std::string mangleName(const Fortran::semantics::DerivedTypeSpec &)=0
Unique a derived type (add a containing scope specific prefix)
const Fortran::lower::LoweringOptions & getLoweringOptions() const
Return options controlling lowering behavior.
Definition: AbstractConverter.h:340
virtual bool lookupLabelSet(SymbolRef sym, pft::LabelSet &labelSet)=0
Get the label set associated with a symbol.
virtual std::string mangleName(const Fortran::semantics::Symbol &)=0
Unique a symbol (add a containing scope specific prefix)
virtual const fir::KindMapping & getKindMap()=0
Get the KindMap.
Definition: LoweringOptions.h:34
Definition: StatementContext.h:46
Definition: SymbolMap.h:146
Definition: char-block.h:28
Definition: scope.h:58
Definition: symbol.h:712
Definition: BoxValue.h:478
Definition: FIRBuilder.h:55
Definition: KindMapping.h:48
Definition: BoxValue.h:360
Definition: FIRType.h:77
Definition: bit-population-count.h:20
Definition: AbstractConverter.h:31
Definition: AbstractConverter.h:27
Definition: variable.h:47
Definition: SymbolMap.h:52
Definition: PFTBuilder.h:216
Definition: PFTBuilder.h:605
Definition: PFTBuilder.h:400