@@ -114,7 +114,8 @@ static FailureOr<int> getOperatorPrecedence(Operation *operation) {
114
114
namespace {
115
115
// / Emitter that uses dialect specific emitters to emit C++ code.
116
116
struct CppEmitter {
117
- explicit CppEmitter (raw_ostream &os, bool declareVariablesAtTop);
117
+ explicit CppEmitter (raw_ostream &os, bool declareVariablesAtTop,
118
+ StringRef fileId);
118
119
119
120
// / Emits attribute or returns failure.
120
121
LogicalResult emitAttribute (Location loc, Attribute attr);
@@ -231,6 +232,11 @@ struct CppEmitter {
231
232
// / be declared at the beginning of a function.
232
233
bool shouldDeclareVariablesAtTop () { return declareVariablesAtTop; };
233
234
235
+ // / Returns whether this file op should be emitted
236
+ bool shouldEmitFile (FileOp file) {
237
+ return !fileId.empty () && file.getId () == fileId;
238
+ }
239
+
234
240
// / Get expression currently being emitted.
235
241
ExpressionOp getEmittedExpression () { return emittedExpression; }
236
242
@@ -258,6 +264,9 @@ struct CppEmitter {
258
264
// / includes results from ops located in nested regions.
259
265
bool declareVariablesAtTop;
260
266
267
+ // / Only emit file ops whos id matches this value.
268
+ std::string fileId;
269
+
261
270
// / Map from value to name of C++ variable that contain the name.
262
271
ValueMapper valueMapper;
263
272
@@ -963,6 +972,19 @@ static LogicalResult printOperation(CppEmitter &emitter, ModuleOp moduleOp) {
963
972
return success ();
964
973
}
965
974
975
+ static LogicalResult printOperation (CppEmitter &emitter, FileOp file) {
976
+ if (!emitter.shouldEmitFile (file))
977
+ return success ();
978
+
979
+ CppEmitter::Scope scope (emitter);
980
+
981
+ for (Operation &op : file) {
982
+ if (failed (emitter.emitOperation (op, /* trailingSemicolon=*/ false )))
983
+ return failure ();
984
+ }
985
+ return success ();
986
+ }
987
+
966
988
static LogicalResult printFunctionArgs (CppEmitter &emitter,
967
989
Operation *functionOp,
968
990
ArrayRef<Type> arguments) {
@@ -1162,8 +1184,10 @@ static LogicalResult printOperation(CppEmitter &emitter,
1162
1184
return success ();
1163
1185
}
1164
1186
1165
- CppEmitter::CppEmitter (raw_ostream &os, bool declareVariablesAtTop)
1166
- : os(os), declareVariablesAtTop(declareVariablesAtTop) {
1187
+ CppEmitter::CppEmitter (raw_ostream &os, bool declareVariablesAtTop,
1188
+ StringRef fileId)
1189
+ : os(os), declareVariablesAtTop(declareVariablesAtTop),
1190
+ fileId(fileId.str()) {
1167
1191
valueInScopeCount.push (0 );
1168
1192
labelInScopeCount.push (0 );
1169
1193
}
@@ -1558,12 +1582,13 @@ LogicalResult CppEmitter::emitOperation(Operation &op, bool trailingSemicolon) {
1558
1582
emitc::BitwiseRightShiftOp, emitc::BitwiseXorOp, emitc::CallOp,
1559
1583
emitc::CallOpaqueOp, emitc::CastOp, emitc::CmpOp,
1560
1584
emitc::ConditionalOp, emitc::ConstantOp, emitc::DeclareFuncOp,
1561
- emitc::DivOp, emitc::ExpressionOp, emitc::ForOp, emitc::FuncOp,
1562
- emitc::GlobalOp, emitc::IfOp, emitc::IncludeOp, emitc::LoadOp,
1563
- emitc::LogicalAndOp, emitc::LogicalNotOp, emitc::LogicalOrOp,
1564
- emitc::MulOp, emitc::RemOp, emitc::ReturnOp, emitc::SubOp,
1565
- emitc::SwitchOp, emitc::UnaryMinusOp, emitc::UnaryPlusOp,
1566
- emitc::VariableOp, emitc::VerbatimOp>(
1585
+ emitc::DivOp, emitc::ExpressionOp, emitc::FileOp, emitc::ForOp,
1586
+ emitc::FuncOp, emitc::GlobalOp, emitc::IfOp, emitc::IncludeOp,
1587
+ emitc::LoadOp, emitc::LogicalAndOp, emitc::LogicalNotOp,
1588
+ emitc::LogicalOrOp, emitc::MulOp, emitc::RemOp, emitc::ReturnOp,
1589
+ emitc::SubOp, emitc::SwitchOp, emitc::UnaryMinusOp,
1590
+ emitc::UnaryPlusOp, emitc::VariableOp, emitc::VerbatimOp>(
1591
+
1567
1592
[&](auto op) { return printOperation (*this , op); })
1568
1593
// Func ops.
1569
1594
.Case <func::CallOp, func::FuncOp, func::ReturnOp>(
@@ -1606,8 +1631,9 @@ LogicalResult CppEmitter::emitOperation(Operation &op, bool trailingSemicolon) {
1606
1631
// Never emit a semicolon for some operations, especially if endening with
1607
1632
// `}`.
1608
1633
trailingSemicolon &=
1609
- !isa<cf::CondBranchOp, emitc::DeclareFuncOp, emitc::ForOp, emitc::IfOp,
1610
- emitc::IncludeOp, emitc::SwitchOp, emitc::VerbatimOp>(op);
1634
+ !isa<cf::CondBranchOp, emitc::DeclareFuncOp, emitc::FileOp, emitc::ForOp,
1635
+ emitc::IfOp, emitc::IncludeOp, emitc::SwitchOp, emitc::VerbatimOp>(
1636
+ op);
1611
1637
1612
1638
os << (trailingSemicolon ? " ;\n " : " \n " );
1613
1639
@@ -1743,7 +1769,8 @@ LogicalResult CppEmitter::emitTupleType(Location loc, ArrayRef<Type> types) {
1743
1769
}
1744
1770
1745
1771
LogicalResult emitc::translateToCpp (Operation *op, raw_ostream &os,
1746
- bool declareVariablesAtTop) {
1747
- CppEmitter emitter (os, declareVariablesAtTop);
1772
+ bool declareVariablesAtTop,
1773
+ StringRef fileId) {
1774
+ CppEmitter emitter (os, declareVariablesAtTop, fileId);
1748
1775
return emitter.emitOperation (*op, /* trailingSemicolon=*/ false );
1749
1776
}
0 commit comments