diff --git a/flang/include/flang/Lower/ConvertType.h b/flang/include/flang/Lower/ConvertType.h --- a/flang/include/flang/Lower/ConvertType.h +++ b/flang/include/flang/Lower/ConvertType.h @@ -4,7 +4,11 @@ // 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/ +// +//===----------------------------------------------------------------------===// /// /// Conversion of front-end TYPE, KIND, ATTRIBUTE (TKA) information to FIR/MLIR. /// This is meant to be the single point of truth (SPOT) for all type @@ -12,15 +16,13 @@ /// tree TKA to the FIR type system. If one is converting front-end types and /// not using one of the routines provided here, it's being done wrong. /// -/// [Coding style](https://llvm.org/docs/CodingStandards.html) -/// -//----------------------------------------------------------------------------// +//===----------------------------------------------------------------------===// #ifndef FORTRAN_LOWER_CONVERT_TYPE_H #define FORTRAN_LOWER_CONVERT_TYPE_H #include "flang/Common/Fortran.h" -#include "mlir/IR/Types.h" +#include "mlir/IR/BuiltinTypes.h" namespace mlir { class Location; @@ -30,22 +32,14 @@ namespace Fortran { namespace common { -class IntrinsicTypeDefaultKinds; template class Reference; } // namespace common namespace evaluate { -struct DataRef; -template -class Designator; template class Expr; -template -struct SomeKind; struct SomeType; -template -class Type; } // namespace evaluate namespace semantics { @@ -68,14 +62,6 @@ mlir::Type getFIRType(mlir::MLIRContext *ctxt, common::TypeCategory tc, int kind); -/// Get a FIR type based on a category. -mlir::Type getFIRType(Fortran::lower::AbstractConverter &, - common::TypeCategory tc); - -/// Translate a Fortran::evaluate::DataRef to an mlir::Type. -mlir::Type translateDataRefToFIRType(Fortran::lower::AbstractConverter &, - const evaluate::DataRef &dataRef); - /// Translate a SomeExpr to an mlir::Type. mlir::Type translateSomeExprToFIRType(Fortran::lower::AbstractConverter &, const SomeExpr &expr); @@ -91,13 +77,6 @@ /// Translate a REAL of KIND to the mlir::Type. mlir::Type convertReal(mlir::MLIRContext *ctxt, int KIND); -// Given a ReferenceType of a base type, returns the ReferenceType to -// the SequenceType of this base type. -// The created SequenceType has one dimension of unknown extent. -// This is useful to do pointer arithmetic using fir::CoordinateOp that requires -// a memory reference to a sequence type. -mlir::Type getSequenceRefType(mlir::Type referenceType); - } // namespace lower } // namespace Fortran diff --git a/flang/lib/Lower/ConvertType.cpp b/flang/lib/Lower/ConvertType.cpp --- a/flang/lib/Lower/ConvertType.cpp +++ b/flang/lib/Lower/ConvertType.cpp @@ -101,54 +101,18 @@ llvm_unreachable("unhandled type category"); } -template -bool isConstant(const Fortran::evaluate::Expr &e) { - return Fortran::evaluate::IsConstantExpr(Fortran::lower::SomeExpr{e}); -} - -template -int64_t toConstant(const Fortran::evaluate::Expr &e) { - auto opt = Fortran::evaluate::ToInt64(e); - assert(opt.has_value() && "expression didn't resolve to a constant"); - return opt.value(); -} - -// one argument template, must be specialized -template -mlir::Type genFIRType(mlir::MLIRContext *, int) { - return {}; -} - -// two argument template -template -mlir::Type genFIRType(mlir::MLIRContext *context) { - if constexpr (TC == Fortran::common::TypeCategory::Integer) { - auto bits{Fortran::evaluate::Type::Scalar::bits}; - return mlir::IntegerType::get(context, bits); - } else if constexpr (TC == Fortran::common::TypeCategory::Logical || - TC == Fortran::common::TypeCategory::Character || - TC == Fortran::common::TypeCategory::Complex) { - return genFIRType(context, KIND); - } else { - return {}; - } -} - -template <> -mlir::Type -genFIRType(mlir::MLIRContext *context, - int KIND) { - if (Fortran::evaluate::IsValidKindOfIntrinsicType( - Fortran::common::TypeCategory::Character, KIND)) - return fir::CharacterType::get(context, KIND, 1); - return {}; -} +//===--------------------------------------------------------------------===// +// Symbol and expression type translation +//===--------------------------------------------------------------------===// +/// TypeBuilder translates expression and symbol type taking into account +/// their shape and length parameters. For symbols, attributes such as +/// ALLOCATABLE or POINTER are reflected in the fir type. +/// It uses evaluate::DynamicType and evaluate::Shape when possible to +/// avoid re-implementing type/shape analysis here. +/// Do not use the FirOpBuilder from the AbstractConverter to get fir/mlir types +/// since it is not guaranteed to exist yet when we lower types. namespace { - -/// Discover the type of an Fortran::evaluate::Expr and convert it to an -/// mlir::Type. The type returned may be an MLIR standard or FIR type. class TypeBuilder { public: TypeBuilder(Fortran::lower::AbstractConverter &converter) @@ -282,203 +246,11 @@ return ty; } - //===--------------------------------------------------------------------===// - // Generate type entry points - //===--------------------------------------------------------------------===// - - template