@@ -96,6 +96,10 @@ struct CppEmitter {
96
96
LogicalResult emitVariableDeclaration (OpResult result,
97
97
bool trailingSemicolon);
98
98
99
+ // / Emits a declaration of a variable with the given type and name.
100
+ LogicalResult emitVariableDeclaration (Location loc, Type type,
101
+ StringRef name);
102
+
99
103
// / Emits the variable declaration and assignment prefix for 'op'.
100
104
// / - emits separate variable followed by std::tie for multi-valued operation;
101
105
// / - emits single type followed by variable for single result;
@@ -623,14 +627,12 @@ static LogicalResult printOperation(CppEmitter &emitter,
623
627
os << " " << functionOp.getName ();
624
628
625
629
os << " (" ;
626
- if (failed (interleaveCommaWithError (
627
- functionOp.getArguments (), os,
628
- [&](BlockArgument arg) -> LogicalResult {
629
- if (failed (emitter.emitType (functionOp.getLoc (), arg.getType ())))
630
- return failure ();
631
- os << " " << emitter.getOrCreateName (arg);
632
- return success ();
633
- })))
630
+ if (failed (interleaveCommaWithError (functionOp.getArguments (), os,
631
+ [&](BlockArgument arg) -> LogicalResult {
632
+ return emitter.emitVariableDeclaration (
633
+ functionOp.getLoc (), arg.getType (),
634
+ emitter.getOrCreateName (arg));
635
+ })))
634
636
return failure ();
635
637
os << " ) {\n " ;
636
638
os.indent ();
@@ -893,9 +895,10 @@ LogicalResult CppEmitter::emitVariableDeclaration(OpResult result,
893
895
return result.getDefiningOp ()->emitError (
894
896
" result variable for the operation already declared" );
895
897
}
896
- if (failed (emitType (result.getOwner ()->getLoc (), result.getType ())))
898
+ if (failed (emitVariableDeclaration (result.getOwner ()->getLoc (),
899
+ result.getType (),
900
+ getOrCreateName (result))))
897
901
return failure ();
898
- os << " " << getOrCreateName (result);
899
902
if (trailingSemicolon)
900
903
os << " ;\n " ;
901
904
return success ();
@@ -977,6 +980,23 @@ LogicalResult CppEmitter::emitOperation(Operation &op, bool trailingSemicolon) {
977
980
return success ();
978
981
}
979
982
983
+ LogicalResult CppEmitter::emitVariableDeclaration (Location loc, Type type,
984
+ StringRef name) {
985
+ if (auto arrType = dyn_cast<emitc::ArrayType>(type)) {
986
+ if (failed (emitType (loc, arrType.getElementType ())))
987
+ return failure ();
988
+ os << " " << name;
989
+ for (auto dim : arrType.getShape ()) {
990
+ os << " [" << dim << " ]" ;
991
+ }
992
+ return success ();
993
+ }
994
+ if (failed (emitType (loc, type)))
995
+ return failure ();
996
+ os << " " << name;
997
+ return success ();
998
+ }
999
+
980
1000
LogicalResult CppEmitter::emitType (Location loc, Type type) {
981
1001
if (auto iType = dyn_cast<IntegerType>(type)) {
982
1002
switch (iType.getWidth ()) {
0 commit comments