[ClavaAst] Improves code generation of DeclStmt for same-type Decl list
@@ -24,9 +24,12 @@ import com.google.common.base.Preconditions;
import pt.up.fe.specs.clava.ClavaNode;
import pt.up.fe.specs.clava.ast.decl.Decl;
+import pt.up.fe.specs.clava.ast.decl.NamedDecl;
import pt.up.fe.specs.clava.ast.decl.RecordDecl;
import pt.up.fe.specs.clava.ast.decl.VarDecl;
import pt.up.fe.specs.clava.ast.stmt.enums.DeclStmtType;
+import pt.up.fe.specs.clava.ast.type.Type;
+import pt.up.fe.specs.clava.utils.Typable;
public class DeclStmt extends Stmt {
@@ -234,10 +237,18 @@ public class DeclStmt extends Stmt {
return getChildren(Decl.class).get(0).getCode();
}
- // All elements are Decls
+ // All elements are Decls with types
// List<NamedDecl> decls = getChildren(NamedDecl.class);
List<Decl> decls = getChildren(Decl.class);
+ // Check if all types of decls are the same
+ Type firstType = ((Typable) decls.get(0)).getType();
+ boolean typesAreDiff = decls.stream()
+ .map(decl -> ((Typable) decl).getType())
+ .filter(type -> !type.equals(firstType))
+ .findFirst()
+ .isPresent();
+
// Write code of first type, add code of next types without variable declaration
// System.out.println("DECLS:");
// for (Decl decl : decls) {
@@ -254,6 +265,8 @@ public class DeclStmt extends Stmt {
// if(!firstDecl.endsWith(";")) {
// code.append(";");
// }
+
+ if (typesAreDiff) {
for (int i = 1; i < decls.size(); i++) {
if (!code.trim().endsWith(";")) {
code += ";";
@@ -261,6 +274,12 @@ public class DeclStmt extends Stmt {
code += ln() + decls.get(i).getCode();
}
+ } else {
+ for (int i = 1; i < decls.size(); i++) {
+ code += ", ";
+ code += ((NamedDecl) decls.get(i)).getTypelessCode();
+ }
+ }
// String code = decls.stream()
// .map(decl -> decl.getCode())
@@ -273,6 +292,7 @@ public class DeclStmt extends Stmt {
// System.out.println("ADDING ;");
code += ";";
}
+
// System.out.println("FINAL CODE:" + code);
return code;
}
