@@ -333,7 +333,8 @@ struct CppEmitter {
333333// / Determine whether expression \p op should be emitted in a deferred way.
334334static bool hasDeferredEmission (Operation *op) {
335335 return isa_and_nonnull<emitc::GetGlobalOp, emitc::LiteralOp, emitc::MemberOp,
336- emitc::MemberOfPtrOp, emitc::SubscriptOp>(op);
336+ emitc::MemberOfPtrOp, emitc::SubscriptOp,
337+ emitc::GetFieldOp>(op);
337338}
338339
339340// / Determine whether expression \p expressionOp should be emitted inline, i.e.
@@ -1049,25 +1050,17 @@ static LogicalResult printOperation(CppEmitter &emitter, ClassOp classOp) {
10491050
10501051static LogicalResult printOperation (CppEmitter &emitter, FieldOp fieldOp) {
10511052 raw_ostream &os = emitter.ostream ();
1052- if (failed (emitter.emitType (fieldOp->getLoc (), fieldOp.getType ())))
1053+ if (failed (emitter.emitVariableDeclaration (
1054+ fieldOp->getLoc (), fieldOp.getType (), fieldOp.getSymName ())))
10531055 return failure ();
1054- os << " " << fieldOp.getSymName () << " ;" ;
1055- return success ();
1056- }
1057-
1058- static LogicalResult printOperation (CppEmitter &emitter,
1059- GetFieldOp getFieldOp) {
1060- raw_indented_ostream &os = emitter.ostream ();
1061-
1062- Value result = getFieldOp.getResult ();
1063- if (failed (emitter.emitType (getFieldOp->getLoc (), result.getType ())))
1064- return failure ();
1065- os << " " ;
1066- if (failed (emitter.emitOperand (result)))
1067- return failure ();
1068- os << " = " ;
1056+ std::optional<Attribute> initialValue = fieldOp.getInitialValue ();
1057+ if (initialValue) {
1058+ os << " = " ;
1059+ if (failed (emitter.emitAttribute (fieldOp->getLoc (), *initialValue)))
1060+ return failure ();
1061+ }
10691062
1070- os << getFieldOp. getFieldName (). str () ;
1063+ os << " ; " ;
10711064 return success ();
10721065}
10731066
@@ -1204,7 +1197,7 @@ static LogicalResult printOperation(CppEmitter &emitter,
12041197 os << " ) {\n " ;
12051198 if (failed (printFunctionBody (emitter, operation, functionOp.getBlocks ())))
12061199 return failure ();
1207- os << " }\n " ;
1200+ os << " }" ;
12081201
12091202 return success ();
12101203}
@@ -1245,7 +1238,7 @@ static LogicalResult printOperation(CppEmitter &emitter,
12451238 os << " ) {\n " ;
12461239 if (failed (printFunctionBody (emitter, operation, functionOp.getBlocks ())))
12471240 return failure ();
1248- os << " }\n " ;
1241+ os << " }" ;
12491242
12501243 return success ();
12511244}
@@ -1700,12 +1693,11 @@ LogicalResult CppEmitter::emitOperation(Operation &op, bool trailingSemicolon) {
17001693 emitc::CmpOp, emitc::ConditionalOp, emitc::ConstantOp,
17011694 emitc::DeclareFuncOp, emitc::DivOp, emitc::ExpressionOp,
17021695 emitc::FieldOp, emitc::FileOp, emitc::ForOp, emitc::FuncOp,
1703- emitc::GetFieldOp, emitc::GlobalOp, emitc::IfOp,
1704- emitc::IncludeOp, emitc::LoadOp, emitc::LogicalAndOp,
1705- emitc::LogicalNotOp, emitc::LogicalOrOp, emitc::MulOp,
1706- emitc::RemOp, emitc::ReturnOp, emitc::SubOp, emitc::SwitchOp,
1707- emitc::UnaryMinusOp, emitc::UnaryPlusOp, emitc::VariableOp,
1708- emitc::VerbatimOp>(
1696+ emitc::GlobalOp, emitc::IfOp, emitc::IncludeOp, emitc::LoadOp,
1697+ emitc::LogicalAndOp, emitc::LogicalNotOp, emitc::LogicalOrOp,
1698+ emitc::MulOp, emitc::RemOp, emitc::ReturnOp, emitc::SubOp,
1699+ emitc::SwitchOp, emitc::UnaryMinusOp, emitc::UnaryPlusOp,
1700+ emitc::VariableOp, emitc::VerbatimOp>(
17091701
17101702 [&](auto op) { return printOperation (*this , op); })
17111703 // Func ops.
@@ -1715,6 +1707,10 @@ LogicalResult CppEmitter::emitOperation(Operation &op, bool trailingSemicolon) {
17151707 cacheDeferredOpResult (op.getResult (), op.getName ());
17161708 return success ();
17171709 })
1710+ .Case <emitc::GetFieldOp>([&](auto op) {
1711+ cacheDeferredOpResult (op.getResult (), op.getFieldName ());
1712+ return success ();
1713+ })
17181714 .Case <emitc::LiteralOp>([&](auto op) {
17191715 cacheDeferredOpResult (op.getResult (), op.getValue ());
17201716 return success ();
0 commit comments