@@ -997,6 +997,72 @@ static LogicalResult printOperation(CppEmitter &emitter, ModuleOp moduleOp) {
997
997
return success ();
998
998
}
999
999
1000
+ static LogicalResult printOperation (CppEmitter &emitter, ClassOp classOp) {
1001
+ CppEmitter::Scope classScope (emitter);
1002
+ raw_indented_ostream &os = emitter.ostream ();
1003
+ os << " class " << classOp.getSymName () << " final {\n " ;
1004
+ os << " public:\n " ;
1005
+
1006
+ os.indent ();
1007
+ for (Operation &op : classOp) {
1008
+ if (isa<FieldOp>(op)) {
1009
+ if (failed (emitter.emitOperation (op, /* trailingSemicolon=*/ true )))
1010
+ return failure ();
1011
+ }
1012
+ }
1013
+ os << " \n const std::map<std::string, char*> _buffer_map {\n " ;
1014
+ for (Operation &op : classOp) {
1015
+ if (auto fieldOp = dyn_cast<FieldOp>(op)) {
1016
+ os << " { \" " << fieldOp.getSymName () << " \" , reinterpret_cast<char*>(&"
1017
+ << fieldOp.getSymName () << " ) },\n " ;
1018
+ }
1019
+ }
1020
+ os << " };\n " ;
1021
+
1022
+ os << " char* getBufferForName(const std::string& name) const {\n " ;
1023
+ os << " auto it = _buffer_map.find(name);\n " ;
1024
+ os << " return (it == _buffer_map.end()) ? nullptr : it->second;\n " ;
1025
+ os << " }\n\n " ;
1026
+ for (Operation &op : classOp) {
1027
+ if (!isa<FieldOp>(op)) {
1028
+ if (failed (emitter.emitOperation (op, /* trailingSemicolon=*/ false )))
1029
+ return failure ();
1030
+ }
1031
+ }
1032
+
1033
+ os.unindent ();
1034
+ os << " };" ;
1035
+ return success ();
1036
+ }
1037
+
1038
+ static LogicalResult printOperation (CppEmitter &emitter, FieldOp fieldOp) {
1039
+ raw_ostream &os = emitter.ostream ();
1040
+ Location loc = fieldOp->getLoc ();
1041
+ Type type = fieldOp.getType ();
1042
+ if (failed (emitter.emitType (loc, type)))
1043
+ return failure ();
1044
+ os << " " << fieldOp.getSymName ();
1045
+ return success ();
1046
+ }
1047
+
1048
+ static LogicalResult printOperation (CppEmitter &emitter,
1049
+ GetFieldOp getFieldOp) {
1050
+ raw_indented_ostream &os = emitter.ostream ();
1051
+ Location loc = getFieldOp->getLoc ();
1052
+
1053
+ if (getFieldOp->getNumResults () > 0 ) {
1054
+ Value result = getFieldOp->getResult (0 );
1055
+ if (failed (emitter.emitType (loc, result.getType ())))
1056
+ return failure ();
1057
+ os << " " ;
1058
+ if (failed (emitter.emitOperand (result)))
1059
+ return failure ();
1060
+ os << " = " ;
1061
+ }
1062
+ os << getFieldOp.getFieldName ().str ();
1063
+ return success ();
1064
+ }
1065
+
1000
1066
static LogicalResult printOperation (CppEmitter &emitter, FileOp file) {
1001
1067
if (!emitter.shouldEmitFile (file))
1002
1068
return success ();
@@ -1612,7 +1678,8 @@ LogicalResult CppEmitter::emitOperation(Operation &op, bool trailingSemicolon) {
1612
1678
emitc::LoadOp, emitc::LogicalAndOp, emitc::LogicalNotOp,
1613
1679
emitc::LogicalOrOp, emitc::MulOp, emitc::RemOp, emitc::ReturnOp,
1614
1680
emitc::SubOp, emitc::SwitchOp, emitc::UnaryMinusOp,
1615
- emitc::UnaryPlusOp, emitc::VariableOp, emitc::VerbatimOp>(
1681
+ emitc::UnaryPlusOp, emitc::VariableOp, emitc::VerbatimOp,
1682
+ emitc::ClassOp, emitc::FieldOp, emitc::GetFieldOp>(
1616
1683
1617
1684
[&](auto op) { return printOperation (*this , op); })
1618
1685
// Func ops.
0 commit comments