@@ -19,6 +19,13 @@ class ComplexExprEmitter : public StmtVisitor<ComplexExprEmitter, mlir::Value> {
1919 // Utilities
2020 // ===--------------------------------------------------------------------===//
2121
22+ LValue emitBinAssignLValue (const BinaryOperator *e, mlir::Value &val);
23+
24+ mlir::Value emitCast (CastKind ck, Expr *op, QualType destTy);
25+
26+ mlir::Value emitConstant (const CIRGenFunction::ConstantEmission &constant,
27+ Expr *e);
28+
2229 // / Given an expression with complex type that represents a value l-value,
2330 // / this method emits the address of the l-value, then loads and returns the
2431 // / result.
@@ -27,18 +34,18 @@ class ComplexExprEmitter : public StmtVisitor<ComplexExprEmitter, mlir::Value> {
2734 }
2835
2936 mlir::Value emitLoadOfLValue (LValue lv, SourceLocation loc);
30-
3137 // / Store the specified real/imag parts into the
3238 // / specified value pointer.
3339 void emitStoreOfComplex (mlir::Location loc, mlir::Value val, LValue lv,
3440 bool isInit);
3541
42+ mlir::Value VisitBinAssign (const BinaryOperator *e);
3643 mlir::Value VisitCallExpr (const CallExpr *e);
37- mlir::Value VisitInitListExpr (InitListExpr *e);
38-
44+ mlir::Value VisitDeclRefExpr (DeclRefExpr *e);
45+ mlir::Value VisitImplicitCastExpr (ImplicitCastExpr *e);
46+ mlir::Value VisitInitListExpr (const InitListExpr *e);
3947 mlir::Value VisitImaginaryLiteral (const ImaginaryLiteral *il);
4048};
41-
4249} // namespace
4350
4451static const ComplexType *getComplexType (QualType type) {
@@ -48,6 +55,48 @@ static const ComplexType *getComplexType(QualType type) {
4855 return cast<ComplexType>(cast<AtomicType>(type)->getValueType ());
4956}
5057
58+ LValue ComplexExprEmitter::emitBinAssignLValue (const BinaryOperator *e,
59+ mlir::Value &value) {
60+ assert (cgf.getContext ().hasSameUnqualifiedType (e->getLHS ()->getType (),
61+ e->getRHS ()->getType ()) &&
62+ " Invalid assignment" );
63+
64+ // Emit the RHS. __block variables need the RHS evaluated first.
65+ value = Visit (e->getRHS ());
66+
67+ // Compute the address to store into.
68+ LValue lhs = cgf.emitLValue (e->getLHS ());
69+
70+ // Store the result value into the LHS lvalue.
71+ emitStoreOfComplex (cgf.getLoc (e->getExprLoc ()), value, lhs, /* isInit*/ false );
72+ return lhs;
73+ }
74+
75+ mlir::Value ComplexExprEmitter::emitCast (CastKind ck, Expr *op,
76+ QualType destTy) {
77+ switch (ck) {
78+ case CK_LValueToRValue: {
79+ return Visit (op);
80+ }
81+ default :
82+ cgf.cgm .errorNYI (" ComplexType Cast" );
83+ break ;
84+ }
85+
86+ return {};
87+ }
88+
89+ mlir::Value ComplexExprEmitter::emitConstant (
90+ const CIRGenFunction::ConstantEmission &constant, Expr *e) {
91+ assert (constant && " not a constant" );
92+ if (constant.isReference ())
93+ return emitLoadOfLValue (constant.getReferenceLValue (cgf, e),
94+ e->getExprLoc ());
95+
96+ mlir::TypedAttr valueAttr = constant.getValue ();
97+ return builder.getConstant (cgf.getLoc (e->getSourceRange ()), valueAttr);
98+ }
99+
51100mlir::Value ComplexExprEmitter::emitLoadOfLValue (LValue lv,
52101 SourceLocation loc) {
53102 assert (lv.isSimple () && " non-simple complex l-value?" );
@@ -70,14 +119,44 @@ void ComplexExprEmitter::emitStoreOfComplex(mlir::Location loc, mlir::Value val,
70119 builder.createStore (loc, val, destAddr);
71120}
72121
122+ mlir::Value ComplexExprEmitter::VisitBinAssign (const BinaryOperator *e) {
123+ mlir::Value value;
124+ LValue lv = emitBinAssignLValue (e, value);
125+
126+ // The result of an assignment in C is the assigned r-value.
127+ if (!cgf.getLangOpts ().CPlusPlus )
128+ return value;
129+
130+ // If the lvalue is non-volatile, return the computed value of the
131+ // assignment.
132+ if (!lv.isVolatile ())
133+ return value;
134+
135+ return emitLoadOfLValue (lv, e->getExprLoc ());
136+ }
137+
73138mlir::Value ComplexExprEmitter::VisitCallExpr (const CallExpr *e) {
74139 if (e->getCallReturnType (cgf.getContext ())->isReferenceType ())
75140 return emitLoadOfLValue (e);
76141
77142 return cgf.emitCallExpr (e).getValue ();
78143}
79144
80- mlir::Value ComplexExprEmitter::VisitInitListExpr (InitListExpr *e) {
145+ mlir::Value ComplexExprEmitter::VisitDeclRefExpr (DeclRefExpr *e) {
146+ if (CIRGenFunction::ConstantEmission constant = cgf.tryEmitAsConstant (e))
147+ return emitConstant (constant, e);
148+ return emitLoadOfLValue (e);
149+ }
150+
151+ mlir::Value ComplexExprEmitter::VisitImplicitCastExpr (ImplicitCastExpr *e) {
152+ // Unlike for scalars, we don't have to worry about function->ptr demotion
153+ // here.
154+ if (e->changesVolatileQualification ())
155+ return emitLoadOfLValue (e);
156+ return emitCast (e->getCastKind (), e->getSubExpr (), e->getType ());
157+ }
158+
159+ mlir::Value ComplexExprEmitter::VisitInitListExpr (const InitListExpr *e) {
81160 mlir::Location loc = cgf.getLoc (e->getExprLoc ());
82161 if (e->getNumInits () == 2 ) {
83162 mlir::Value real = cgf.emitScalarExpr (e->getInit (0 ));
@@ -127,6 +206,17 @@ ComplexExprEmitter::VisitImaginaryLiteral(const ImaginaryLiteral *il) {
127206 return builder.create <cir::ConstantOp>(loc, complexAttr);
128207}
129208
209+ LValue CIRGenFunction::emitComplexAssignmentLValue (const BinaryOperator *e) {
210+ assert (e->getOpcode () == BO_Assign && " Expected assign op" );
211+
212+ mlir::Value value; // ignored
213+ LValue lvalue = ComplexExprEmitter (*this ).emitBinAssignLValue (e, value);
214+ if (getLangOpts ().OpenMP )
215+ cgm.errorNYI (" emitComplexAssignmentLValue OpenMP" );
216+
217+ return lvalue;
218+ }
219+
130220mlir::Value CIRGenFunction::emitComplexExpr (const Expr *e) {
131221 assert (e && getComplexType (e->getType ()) &&
132222 " Invalid complex expression to emit" );
0 commit comments