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 #ifndef FORTRAN_LOWER_ABSTRACTCONVERTER_H
10 #define FORTRAN_LOWER_ABSTRACTCONVERTER_H
11 
12 #include "flang/Common/Fortran.h"
13 #include "mlir/IR/BuiltinOps.h"
14 
15 namespace Fortran {
16 namespace common {
17 template <typename>
18 class Reference;
19 }
20 namespace evaluate {
21 struct DataRef;
22 template <typename>
23 class Expr;
24 class FoldingContext;
25 struct SomeType;
26 } // namespace evaluate
27 
28 namespace parser {
29 class CharBlock;
30 }
31 namespace semantics {
32 class Symbol;
33 }
34 
35 namespace lower {
36 namespace pft {
37 struct Variable;
38 }
39 
40 using SomeExpr = Fortran::evaluate::Expr<Fortran::evaluate::SomeType>;
41 using SymbolRef = Fortran::common::Reference<const Fortran::semantics::Symbol>;
42 class FirOpBuilder;
43 
44 //===----------------------------------------------------------------------===//
45 // AbstractConverter interface
46 //===----------------------------------------------------------------------===//
47 
48 /// The abstract interface for converter implementations to lower Fortran
49 /// front-end fragments such as expressions, types, etc. to the FIR dialect of
50 /// MLIR.
51 class AbstractConverter {
52 public:
53   //===--------------------------------------------------------------------===//
54   // Symbols
55   //===--------------------------------------------------------------------===//
56 
57   /// Get the mlir instance of a symbol.
58   virtual mlir::Value getSymbolAddress(SymbolRef sym) = 0;
59 
60   //===--------------------------------------------------------------------===//
61   // Expressions
62   //===--------------------------------------------------------------------===//
63 
64   /// Generate the address of the location holding the expression, someExpr
65   virtual mlir::Value genExprAddr(const SomeExpr &,
66                                   mlir::Location *loc = nullptr) = 0;
67   /// Generate the address of the location holding the expression, someExpr
genExprAddr(const SomeExpr * someExpr,mlir::Location loc)68   mlir::Value genExprAddr(const SomeExpr *someExpr, mlir::Location loc) {
69     return genExprAddr(*someExpr, &loc);
70   }
71 
72   /// Generate the computations of the expression to produce a value
73   virtual mlir::Value genExprValue(const SomeExpr &,
74                                    mlir::Location *loc = nullptr) = 0;
75   /// Generate the computations of the expression, someExpr, to produce a value
genExprValue(const SomeExpr * someExpr,mlir::Location loc)76   mlir::Value genExprValue(const SomeExpr *someExpr, mlir::Location loc) {
77     return genExprValue(*someExpr, &loc);
78   }
79 
80   /// Get FoldingContext that is required for some expression
81   /// analysis.
82   virtual Fortran::evaluate::FoldingContext &getFoldingContext() = 0;
83 
84   //===--------------------------------------------------------------------===//
85   // Types
86   //===--------------------------------------------------------------------===//
87 
88   /// Generate the type of a DataRef
89   virtual mlir::Type genType(const Fortran::evaluate::DataRef &) = 0;
90   /// Generate the type of an Expr
91   virtual mlir::Type genType(const SomeExpr &) = 0;
92   /// Generate the type of a Symbol
93   virtual mlir::Type genType(SymbolRef) = 0;
94   /// Generate the type from a category
95   virtual mlir::Type genType(Fortran::common::TypeCategory tc) = 0;
96   /// Generate the type from a category and kind
97   virtual mlir::Type genType(Fortran::common::TypeCategory tc, int kind) = 0;
98   /// Generate the type from a Variable
99   virtual mlir::Type genType(const pft::Variable &) = 0;
100 
101   //===--------------------------------------------------------------------===//
102   // Locations
103   //===--------------------------------------------------------------------===//
104 
105   /// Get the converter's current location
106   virtual mlir::Location getCurrentLocation() = 0;
107   /// Generate a dummy location
108   virtual mlir::Location genLocation() = 0;
109   /// Generate the location as converted from a CharBlock
110   virtual mlir::Location genLocation(const Fortran::parser::CharBlock &) = 0;
111 
112   //===--------------------------------------------------------------------===//
113   // FIR/MLIR
114   //===--------------------------------------------------------------------===//
115 
116   /// Get the OpBuilder
117   virtual Fortran::lower::FirOpBuilder &getFirOpBuilder() = 0;
118   /// Get the ModuleOp
119   virtual mlir::ModuleOp &getModuleOp() = 0;
120   /// Get the MLIRContext
121   virtual mlir::MLIRContext &getMLIRContext() = 0;
122   /// Unique a symbol
123   virtual std::string mangleName(const Fortran::semantics::Symbol &) = 0;
124   /// Unique a compiler generated identifier. A short prefix should be provided
125   /// to hint at the origin of the identifier.
126   virtual std::string uniqueCGIdent(llvm::StringRef prefix,
127                                     llvm::StringRef name) = 0;
128 
129   virtual ~AbstractConverter() = default;
130 };
131 
132 } // namespace lower
133 } // namespace Fortran
134 
135 #endif // FORTRAN_LOWER_ABSTRACTCONVERTER_H
136