|
| 1 | +//===-- ConvertExprToHLFIR.cpp --------------------------------------------===// |
| 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 | +#include "flang/Lower/ConvertExprToHLFIR.h" |
| 14 | +#include "flang/Lower/AbstractConverter.h" |
| 15 | +#include "flang/Lower/StatementContext.h" |
| 16 | +#include "flang/Lower/SymbolMap.h" |
| 17 | +#include "flang/Optimizer/Builder/Todo.h" |
| 18 | + |
| 19 | +namespace { |
| 20 | + |
| 21 | +/// Lower Designators to HLFIR. |
| 22 | +class HlfirDesignatorBuilder { |
| 23 | +public: |
| 24 | + HlfirDesignatorBuilder(mlir::Location loc, |
| 25 | + Fortran::lower::AbstractConverter &converter, |
| 26 | + Fortran::lower::SymMap &symMap, |
| 27 | + Fortran::lower::StatementContext &stmtCtx) |
| 28 | + : converter{converter}, symMap{symMap}, stmtCtx{stmtCtx}, loc{loc} {} |
| 29 | + |
| 30 | + // Character designators variant contains substrings |
| 31 | + using CharacterDesignators = |
| 32 | + decltype(Fortran::evaluate::Designator<Fortran::evaluate::Type< |
| 33 | + Fortran::evaluate::TypeCategory::Character, 1>>::u); |
| 34 | + hlfir::FortranEntity gen(const CharacterDesignators &designatorVariant) { |
| 35 | + return std::visit([&](const auto &x) { return gen(x); }, designatorVariant); |
| 36 | + } |
| 37 | + // Character designators variant contains complex parts |
| 38 | + using RealDesignators = |
| 39 | + decltype(Fortran::evaluate::Designator<Fortran::evaluate::Type< |
| 40 | + Fortran::evaluate::TypeCategory::Real, 4>>::u); |
| 41 | + hlfir::FortranEntity gen(const RealDesignators &designatorVariant) { |
| 42 | + return std::visit([&](const auto &x) { return gen(x); }, designatorVariant); |
| 43 | + } |
| 44 | + // All other designators are similar |
| 45 | + using OtherDesignators = |
| 46 | + decltype(Fortran::evaluate::Designator<Fortran::evaluate::Type< |
| 47 | + Fortran::evaluate::TypeCategory::Integer, 4>>::u); |
| 48 | + hlfir::FortranEntity gen(const OtherDesignators &designatorVariant) { |
| 49 | + return std::visit([&](const auto &x) { return gen(x); }, designatorVariant); |
| 50 | + } |
| 51 | + |
| 52 | +private: |
| 53 | + hlfir::FortranEntity gen(const Fortran::evaluate::SymbolRef &symbolRef) { |
| 54 | + if (llvm::Optional<fir::FortranVariableOpInterface> varDef = |
| 55 | + getSymMap().lookupVariableDefinition(symbolRef)) |
| 56 | + return *varDef; |
| 57 | + TODO(getLoc(), "lowering symbol to HLFIR"); |
| 58 | + } |
| 59 | + hlfir::FortranEntity gen(const Fortran::evaluate::Component &component) { |
| 60 | + TODO(getLoc(), "lowering component to HLFIR"); |
| 61 | + } |
| 62 | + hlfir::FortranEntity gen(const Fortran::evaluate::ArrayRef &arrayRef) { |
| 63 | + TODO(getLoc(), "lowering ArrayRef to HLFIR"); |
| 64 | + } |
| 65 | + hlfir::FortranEntity gen(const Fortran::evaluate::CoarrayRef &coarrayRef) { |
| 66 | + TODO(getLoc(), "lowering CoarrayRef to HLFIR"); |
| 67 | + } |
| 68 | + hlfir::FortranEntity gen(const Fortran::evaluate::ComplexPart &complexPart) { |
| 69 | + TODO(getLoc(), "lowering complex part to HLFIR"); |
| 70 | + } |
| 71 | + hlfir::FortranEntity gen(const Fortran::evaluate::Substring &substring) { |
| 72 | + TODO(getLoc(), "lowering substrings to HLFIR"); |
| 73 | + } |
| 74 | + |
| 75 | + mlir::Location getLoc() const { return loc; } |
| 76 | + Fortran::lower::AbstractConverter &getConverter() { return converter; } |
| 77 | + fir::FirOpBuilder &getBuilder() { return converter.getFirOpBuilder(); } |
| 78 | + Fortran::lower::SymMap &getSymMap() { return symMap; } |
| 79 | + Fortran::lower::StatementContext &getStmtCtx() { return stmtCtx; } |
| 80 | + |
| 81 | + Fortran::lower::AbstractConverter &converter; |
| 82 | + Fortran::lower::SymMap &symMap; |
| 83 | + Fortran::lower::StatementContext &stmtCtx; |
| 84 | + mlir::Location loc; |
| 85 | +}; |
| 86 | + |
| 87 | +/// Lower Expr to HLFIR. |
| 88 | +class HlfirBuilder { |
| 89 | +public: |
| 90 | + HlfirBuilder(mlir::Location loc, Fortran::lower::AbstractConverter &converter, |
| 91 | + Fortran::lower::SymMap &symMap, |
| 92 | + Fortran::lower::StatementContext &stmtCtx) |
| 93 | + : converter{converter}, symMap{symMap}, stmtCtx{stmtCtx}, loc{loc} {} |
| 94 | + |
| 95 | + template <typename T> |
| 96 | + hlfir::FortranEntity gen(const Fortran::evaluate::Expr<T> &expr) { |
| 97 | + return std::visit([&](const auto &x) { return gen(x); }, expr.u); |
| 98 | + } |
| 99 | + |
| 100 | +private: |
| 101 | + hlfir::FortranEntity gen(const Fortran::evaluate::BOZLiteralConstant &expr) { |
| 102 | + fir::emitFatalError(loc, "BOZ literal must be replaced by semantics"); |
| 103 | + } |
| 104 | + hlfir::FortranEntity gen(const Fortran::evaluate::NullPointer &expr) { |
| 105 | + TODO(getLoc(), "lowering NullPointer to HLFIR"); |
| 106 | + } |
| 107 | + hlfir::FortranEntity gen(const Fortran::evaluate::ProcedureDesignator &expr) { |
| 108 | + TODO(getLoc(), "lowering ProcDes to HLFIR"); |
| 109 | + } |
| 110 | + hlfir::FortranEntity gen(const Fortran::evaluate::ProcedureRef &expr) { |
| 111 | + TODO(getLoc(), "lowering ProcRef to HLFIR"); |
| 112 | + } |
| 113 | + |
| 114 | + template <typename T> |
| 115 | + hlfir::FortranEntity gen(const Fortran::evaluate::Designator<T> &designator) { |
| 116 | + return HlfirDesignatorBuilder(getLoc(), getConverter(), getSymMap(), |
| 117 | + getStmtCtx()) |
| 118 | + .gen(designator.u); |
| 119 | + } |
| 120 | + |
| 121 | + template <typename T> |
| 122 | + hlfir::FortranEntity gen(const Fortran::evaluate::FunctionRef<T> &expr) { |
| 123 | + TODO(getLoc(), "lowering funcRef to HLFIR"); |
| 124 | + } |
| 125 | + |
| 126 | + template <typename T> |
| 127 | + hlfir::FortranEntity gen(const Fortran::evaluate::Constant<T> &expr) { |
| 128 | + TODO(getLoc(), "lowering constant to HLFIR"); |
| 129 | + } |
| 130 | + |
| 131 | + template <typename T> |
| 132 | + hlfir::FortranEntity gen(const Fortran::evaluate::ArrayConstructor<T> &expr) { |
| 133 | + TODO(getLoc(), "lowering ArrayCtor to HLFIR"); |
| 134 | + } |
| 135 | + |
| 136 | + template <Fortran::common::TypeCategory TC1, int KIND, |
| 137 | + Fortran::common::TypeCategory TC2> |
| 138 | + hlfir::FortranEntity |
| 139 | + gen(const Fortran::evaluate::Convert<Fortran::evaluate::Type<TC1, KIND>, TC2> |
| 140 | + &convert) { |
| 141 | + TODO(getLoc(), "lowering convert to HLFIR"); |
| 142 | + } |
| 143 | + |
| 144 | + template <typename D, typename R, typename O> |
| 145 | + hlfir::FortranEntity gen(const Fortran::evaluate::Operation<D, R, O> &op) { |
| 146 | + TODO(getLoc(), "lowering unary op to HLFIR"); |
| 147 | + } |
| 148 | + |
| 149 | + template <typename D, typename R, typename LO, typename RO> |
| 150 | + hlfir::FortranEntity |
| 151 | + gen(const Fortran::evaluate::Operation<D, R, LO, RO> &op) { |
| 152 | + TODO(getLoc(), "lowering binary op to HLFIR"); |
| 153 | + } |
| 154 | + |
| 155 | + hlfir::FortranEntity |
| 156 | + gen(const Fortran::evaluate::Relational<Fortran::evaluate::SomeType> &op) { |
| 157 | + return std::visit([&](const auto &x) { return gen(x); }, op.u); |
| 158 | + } |
| 159 | + |
| 160 | + hlfir::FortranEntity gen(const Fortran::evaluate::TypeParamInquiry &) { |
| 161 | + TODO(getLoc(), "lowering type parameter inquiry to HLFIR"); |
| 162 | + } |
| 163 | + |
| 164 | + hlfir::FortranEntity gen(const Fortran::evaluate::DescriptorInquiry &desc) { |
| 165 | + TODO(getLoc(), "lowering descriptor inquiry to HLFIR"); |
| 166 | + } |
| 167 | + |
| 168 | + hlfir::FortranEntity gen(const Fortran::evaluate::ImpliedDoIndex &var) { |
| 169 | + TODO(getLoc(), "lowering implied do index to HLFIR"); |
| 170 | + } |
| 171 | + |
| 172 | + hlfir::FortranEntity gen(const Fortran::evaluate::StructureConstructor &var) { |
| 173 | + TODO(getLoc(), "lowering structure constructor to HLFIR"); |
| 174 | + } |
| 175 | + |
| 176 | + mlir::Location getLoc() const { return loc; } |
| 177 | + Fortran::lower::AbstractConverter &getConverter() { return converter; } |
| 178 | + fir::FirOpBuilder &getBuilder() { return converter.getFirOpBuilder(); } |
| 179 | + Fortran::lower::SymMap &getSymMap() { return symMap; } |
| 180 | + Fortran::lower::StatementContext &getStmtCtx() { return stmtCtx; } |
| 181 | + |
| 182 | + Fortran::lower::AbstractConverter &converter; |
| 183 | + Fortran::lower::SymMap &symMap; |
| 184 | + Fortran::lower::StatementContext &stmtCtx; |
| 185 | + mlir::Location loc; |
| 186 | +}; |
| 187 | + |
| 188 | +} // namespace |
| 189 | + |
| 190 | +hlfir::FortranEntity Fortran::lower::convertExprToHLFIR( |
| 191 | + mlir::Location loc, Fortran::lower::AbstractConverter &converter, |
| 192 | + const Fortran::lower::SomeExpr &expr, Fortran::lower::SymMap &symMap, |
| 193 | + Fortran::lower::StatementContext &stmtCtx) { |
| 194 | + return HlfirBuilder(loc, converter, symMap, stmtCtx).gen(expr); |
| 195 | +} |
0 commit comments