diff --git a/flang/lib/Lower/ConvertExpr.cpp b/flang/lib/Lower/ConvertExpr.cpp --- a/flang/lib/Lower/ConvertExpr.cpp +++ b/flang/lib/Lower/ConvertExpr.cpp @@ -19,6 +19,7 @@ #include "flang/Lower/IntrinsicCall.h" #include "flang/Lower/SymbolMap.h" #include "flang/Lower/Todo.h" +#include "flang/Optimizer/Builder/Complex.h" #include "flang/Semantics/expression.h" #include "flang/Semantics/symbol.h" #include "flang/Semantics/tools.h" @@ -277,7 +278,9 @@ template ExtValue genval(const Fortran::evaluate::ComplexConstructor &op) { - TODO(getLoc(), "genval ComplexConstructor"); + mlir::Value realPartValue = genunbox(op.left()); + return fir::factory::Complex{builder, getLoc()}.createComplex( + KIND, realPartValue, genunbox(op.right())); } template @@ -381,7 +384,14 @@ return genRealConstant(builder.getContext(), floatVal); } } else if constexpr (TC == Fortran::common::TypeCategory::Complex) { - TODO(getLoc(), "genval complex constant"); + using TR = + Fortran::evaluate::Type; + Fortran::evaluate::ComplexConstructor ctor( + Fortran::evaluate::Expr{ + Fortran::evaluate::Constant{value.REAL()}}, + Fortran::evaluate::Expr{ + Fortran::evaluate::Constant{value.AIMAG()}}); + return genunbox(ctor); } else /*constexpr*/ { llvm_unreachable("unhandled constant"); } diff --git a/flang/test/Lower/assignment.f90 b/flang/test/Lower/assignment.f90 --- a/flang/test/Lower/assignment.f90 +++ b/flang/test/Lower/assignment.f90 @@ -284,3 +284,17 @@ ! CHECK: fir.store %[[C10]] to %[[D]] : !fir.ref ! CHECK: %[[C16:.*]] = arith.constant 1.600000e+01 : f128 ! CHECK: fir.store %[[C16]] to %[[E]] : !fir.ref + +subroutine complex_constant() + complex(4) :: a + a = (0, 1) +end + +! CHECK-LABEL: func @_QPcomplex_constant() +! CHECK: %[[A:.*]] = fir.alloca !fir.complex<4> {bindc_name = "a", uniq_name = "_QFcomplex_constantEa"} +! CHECK: %[[C0:.*]] = arith.constant 0.000000e+00 : f32 +! CHECK: %[[C1:.*]] = arith.constant 1.000000e+00 : f32 +! CHECK: %[[UNDEF:.*]] = fir.undefined !fir.complex<4> +! CHECK: %[[INS0:.*]] = fir.insert_value %[[UNDEF]], %[[C0]], [0 : index] : (!fir.complex<4>, f32) -> !fir.complex<4> +! CHECK: %[[INS1:.*]] = fir.insert_value %[[INS0]], %[[C1]], [1 : index] : (!fir.complex<4>, f32) -> !fir.complex<4> +! CHECK: fir.store %[[INS1]] to %[[A]] : !fir.ref>