//===-- ConvertExprToHLFIR.cpp --------------------------------------------===// // // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. // See https://llvm.org/LICENSE.txt for license information. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception // //===----------------------------------------------------------------------===// // // Coding style: https://mlir.llvm.org/getting_started/DeveloperGuide/ // //===----------------------------------------------------------------------===// #include "flang/Lower/ConvertExprToHLFIR.h" #include "flang/Lower/AbstractConverter.h" #include "flang/Lower/StatementContext.h" #include "flang/Lower/SymbolMap.h" #include "flang/Optimizer/Builder/Todo.h" namespace { /// Lower Designators to HLFIR. class HlfirDesignatorBuilder { public: HlfirDesignatorBuilder(mlir::Location loc, Fortran::lower::AbstractConverter &converter, Fortran::lower::SymMap &symMap, Fortran::lower::StatementContext &stmtCtx) : converter{converter}, symMap{symMap}, stmtCtx{stmtCtx}, loc{loc} {} // Character designators variant contains substrings using CharacterDesignators = decltype(Fortran::evaluate::Designator>::u); hlfir::FortranEntity gen(const CharacterDesignators &designatorVariant) { return std::visit([&](const auto &x) { return gen(x); }, designatorVariant); } // Character designators variant contains complex parts using RealDesignators = decltype(Fortran::evaluate::Designator>::u); hlfir::FortranEntity gen(const RealDesignators &designatorVariant) { return std::visit([&](const auto &x) { return gen(x); }, designatorVariant); } // All other designators are similar using OtherDesignators = decltype(Fortran::evaluate::Designator>::u); hlfir::FortranEntity gen(const OtherDesignators &designatorVariant) { return std::visit([&](const auto &x) { return gen(x); }, designatorVariant); } private: hlfir::FortranEntity gen(const Fortran::evaluate::SymbolRef &symbolRef) { if (llvm::Optional varDef = getSymMap().lookupVariableDefinition(symbolRef)) return *varDef; TODO(getLoc(), "lowering symbol to HLFIR"); } hlfir::FortranEntity gen(const Fortran::evaluate::Component &component) { TODO(getLoc(), "lowering component to HLFIR"); } hlfir::FortranEntity gen(const Fortran::evaluate::ArrayRef &arrayRef) { TODO(getLoc(), "lowering ArrayRef to HLFIR"); } hlfir::FortranEntity gen(const Fortran::evaluate::CoarrayRef &coarrayRef) { TODO(getLoc(), "lowering CoarrayRef to HLFIR"); } hlfir::FortranEntity gen(const Fortran::evaluate::ComplexPart &complexPart) { TODO(getLoc(), "lowering complex part to HLFIR"); } hlfir::FortranEntity gen(const Fortran::evaluate::Substring &substring) { TODO(getLoc(), "lowering substrings to HLFIR"); } mlir::Location getLoc() const { return loc; } Fortran::lower::AbstractConverter &getConverter() { return converter; } fir::FirOpBuilder &getBuilder() { return converter.getFirOpBuilder(); } Fortran::lower::SymMap &getSymMap() { return symMap; } Fortran::lower::StatementContext &getStmtCtx() { return stmtCtx; } Fortran::lower::AbstractConverter &converter; Fortran::lower::SymMap &symMap; Fortran::lower::StatementContext &stmtCtx; mlir::Location loc; }; /// Lower Expr to HLFIR. class HlfirBuilder { public: HlfirBuilder(mlir::Location loc, Fortran::lower::AbstractConverter &converter, Fortran::lower::SymMap &symMap, Fortran::lower::StatementContext &stmtCtx) : converter{converter}, symMap{symMap}, stmtCtx{stmtCtx}, loc{loc} {} template hlfir::FortranEntity gen(const Fortran::evaluate::Expr &expr) { return std::visit([&](const auto &x) { return gen(x); }, expr.u); } private: hlfir::FortranEntity gen(const Fortran::evaluate::BOZLiteralConstant &expr) { fir::emitFatalError(loc, "BOZ literal must be replaced by semantics"); } hlfir::FortranEntity gen(const Fortran::evaluate::NullPointer &expr) { TODO(getLoc(), "lowering NullPointer to HLFIR"); } hlfir::FortranEntity gen(const Fortran::evaluate::ProcedureDesignator &expr) { TODO(getLoc(), "lowering ProcDes to HLFIR"); } hlfir::FortranEntity gen(const Fortran::evaluate::ProcedureRef &expr) { TODO(getLoc(), "lowering ProcRef to HLFIR"); } template hlfir::FortranEntity gen(const Fortran::evaluate::Designator &designator) { return HlfirDesignatorBuilder(getLoc(), getConverter(), getSymMap(), getStmtCtx()) .gen(designator.u); } template hlfir::FortranEntity gen(const Fortran::evaluate::FunctionRef &expr) { TODO(getLoc(), "lowering funcRef to HLFIR"); } template hlfir::FortranEntity gen(const Fortran::evaluate::Constant &expr) { TODO(getLoc(), "lowering constant to HLFIR"); } template hlfir::FortranEntity gen(const Fortran::evaluate::ArrayConstructor &expr) { TODO(getLoc(), "lowering ArrayCtor to HLFIR"); } template hlfir::FortranEntity gen(const Fortran::evaluate::Convert, TC2> &convert) { TODO(getLoc(), "lowering convert to HLFIR"); } template hlfir::FortranEntity gen(const Fortran::evaluate::Operation &op) { TODO(getLoc(), "lowering unary op to HLFIR"); } template hlfir::FortranEntity gen(const Fortran::evaluate::Operation &op) { TODO(getLoc(), "lowering binary op to HLFIR"); } hlfir::FortranEntity gen(const Fortran::evaluate::Relational &op) { return std::visit([&](const auto &x) { return gen(x); }, op.u); } hlfir::FortranEntity gen(const Fortran::evaluate::TypeParamInquiry &) { TODO(getLoc(), "lowering type parameter inquiry to HLFIR"); } hlfir::FortranEntity gen(const Fortran::evaluate::DescriptorInquiry &desc) { TODO(getLoc(), "lowering descriptor inquiry to HLFIR"); } hlfir::FortranEntity gen(const Fortran::evaluate::ImpliedDoIndex &var) { TODO(getLoc(), "lowering implied do index to HLFIR"); } hlfir::FortranEntity gen(const Fortran::evaluate::StructureConstructor &var) { TODO(getLoc(), "lowering structure constructor to HLFIR"); } mlir::Location getLoc() const { return loc; } Fortran::lower::AbstractConverter &getConverter() { return converter; } fir::FirOpBuilder &getBuilder() { return converter.getFirOpBuilder(); } Fortran::lower::SymMap &getSymMap() { return symMap; } Fortran::lower::StatementContext &getStmtCtx() { return stmtCtx; } Fortran::lower::AbstractConverter &converter; Fortran::lower::SymMap &symMap; Fortran::lower::StatementContext &stmtCtx; mlir::Location loc; }; } // namespace hlfir::FortranEntity Fortran::lower::convertExprToHLFIR( mlir::Location loc, Fortran::lower::AbstractConverter &converter, const Fortran::lower::SomeExpr &expr, Fortran::lower::SymMap &symMap, Fortran::lower::StatementContext &stmtCtx) { return HlfirBuilder(loc, converter, symMap, stmtCtx).gen(expr); }