1 //===-- Lower/ConvertType.h -- lowering of types ----------------*- 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 /// Conversion of front-end TYPE, KIND, ATTRIBUTE (TKA) information to FIR/MLIR.
10 /// This is meant to be the single point of truth (SPOT) for all type
11 /// conversions when lowering to FIR.  This implements all lowering of parse
12 /// tree TKA to the FIR type system. If one is converting front-end types and
13 /// not using one of the routines provided here, it's being done wrong.
14 ///
15 /// [Coding style](https://llvm.org/docs/CodingStandards.html)
16 ///
17 //----------------------------------------------------------------------------//
18 
19 #ifndef FORTRAN_LOWER_CONVERT_TYPE_H
20 #define FORTRAN_LOWER_CONVERT_TYPE_H
21 
22 #include "flang/Common/Fortran.h"
23 #include "mlir/IR/Types.h"
24 
25 namespace mlir {
26 class Location;
27 class MLIRContext;
28 class Type;
29 } // namespace mlir
30 
31 namespace Fortran {
32 namespace common {
33 class IntrinsicTypeDefaultKinds;
34 template <typename>
35 class Reference;
36 } // namespace common
37 
38 namespace evaluate {
39 struct DataRef;
40 template <typename>
41 class Designator;
42 template <typename>
43 class Expr;
44 template <common::TypeCategory>
45 struct SomeKind;
46 struct SomeType;
47 template <common::TypeCategory, int>
48 class Type;
49 } // namespace evaluate
50 
51 namespace semantics {
52 class Symbol;
53 } // namespace semantics
54 
55 namespace lower {
56 namespace pft {
57 struct Variable;
58 }
59 
60 using SomeExpr = evaluate::Expr<evaluate::SomeType>;
61 using SymbolRef = common::Reference<const semantics::Symbol>;
62 
63 /// Get a FIR type based on a category and kind.
64 mlir::Type getFIRType(mlir::MLIRContext *ctxt,
65                       common::IntrinsicTypeDefaultKinds const &defaults,
66                       common::TypeCategory tc, int kind);
67 
68 /// Get a FIR type based on a category.
69 mlir::Type getFIRType(mlir::MLIRContext *ctxt,
70                       common::IntrinsicTypeDefaultKinds const &defaults,
71                       common::TypeCategory tc);
72 
73 /// Translate a Fortran::evaluate::DataRef to an mlir::Type.
74 mlir::Type
75 translateDataRefToFIRType(mlir::MLIRContext *ctxt,
76                           common::IntrinsicTypeDefaultKinds const &defaults,
77                           const evaluate::DataRef &dataRef);
78 
79 /// Translate a Fortran::evaluate::Designator<> to an mlir::Type.
80 template <common::TypeCategory TC, int KIND>
translateDesignatorToFIRType(mlir::MLIRContext * ctxt,common::IntrinsicTypeDefaultKinds const & defaults,const evaluate::Designator<evaluate::Type<TC,KIND>> &)81 inline mlir::Type translateDesignatorToFIRType(
82     mlir::MLIRContext *ctxt, common::IntrinsicTypeDefaultKinds const &defaults,
83     const evaluate::Designator<evaluate::Type<TC, KIND>> &) {
84   return getFIRType(ctxt, defaults, TC, KIND);
85 }
86 
87 /// Translate a Fortran::evaluate::Designator<> to an mlir::Type.
88 template <common::TypeCategory TC>
translateDesignatorToFIRType(mlir::MLIRContext * ctxt,common::IntrinsicTypeDefaultKinds const & defaults,const evaluate::Designator<evaluate::SomeKind<TC>> &)89 inline mlir::Type translateDesignatorToFIRType(
90     mlir::MLIRContext *ctxt, common::IntrinsicTypeDefaultKinds const &defaults,
91     const evaluate::Designator<evaluate::SomeKind<TC>> &) {
92   return getFIRType(ctxt, defaults, TC);
93 }
94 
95 /// Translate a SomeExpr to an mlir::Type.
96 mlir::Type
97 translateSomeExprToFIRType(mlir::MLIRContext *ctxt,
98                            common::IntrinsicTypeDefaultKinds const &defaults,
99                            const SomeExpr *expr);
100 
101 /// Translate a Fortran::semantics::Symbol to an mlir::Type.
102 mlir::Type
103 translateSymbolToFIRType(mlir::MLIRContext *ctxt,
104                          common::IntrinsicTypeDefaultKinds const &defaults,
105                          const SymbolRef symbol);
106 
107 /// Translate a Fortran::lower::pft::Variable to an mlir::Type.
108 mlir::Type
109 translateVariableToFIRType(mlir::MLIRContext *ctxt,
110                            common::IntrinsicTypeDefaultKinds const &defaults,
111                            const pft::Variable &variable);
112 
113 /// Translate a REAL of KIND to the mlir::Type.
114 mlir::Type convertReal(mlir::MLIRContext *ctxt, int KIND);
115 
116 // Given a ReferenceType of a base type, returns the ReferenceType to
117 // the SequenceType of this base type.
118 // The created SequenceType has one dimension of unknown extent.
119 // This is useful to do pointer arithmetic using fir::CoordinateOp that requires
120 // a memory reference to a sequence type.
121 mlir::Type getSequenceRefType(mlir::Type referenceType);
122 
123 } // namespace lower
124 } // namespace Fortran
125 
126 #endif // FORTRAN_LOWER_CONVERT_TYPE_H
127