[ClavaAst] Improves FunctionDecl support for setting param types
@@ -24,6 +24,7 @@ import org.suikasoft.jOptions.Datakey.DataKey;
import org.suikasoft.jOptions.Datakey.KeyFactory;
import org.suikasoft.jOptions.Interfaces.DataStore;
+import pt.up.fe.specs.clava.ClavaLog;
import pt.up.fe.specs.clava.ClavaNode;
import pt.up.fe.specs.clava.Types;
import pt.up.fe.specs.clava.ast.attr.Attribute;
@@ -228,6 +229,10 @@ public class FunctionDecl extends DeclaratorDecl {
return getFunctionType().getReturnType();
}
+ public void setReturnType(Type returnType) {
+ getFunctionType().setReturnType(returnType);
+ }
+
// @Deprecated
// protected FunctionDeclData getFunctionDeclData() {
// return DataStoreToLegacy.getFunctionDecl(getData());
@@ -787,6 +792,31 @@ public class FunctionDecl extends DeclaratorDecl {
for (int i = 0; i < params.size(); i++) {
addChild(i, params.get(i));
}
+
+ // Update FunctionType
+ var newFunctionType = (FunctionType) getFunctionType().copy();
+ for (int i = 0; i < params.size(); i++) {
+ newFunctionType.setParamType(i, params.get(i).getType());
+ }
+ set(TYPE, newFunctionType);
+ }
+
+ public void setParamType(Integer index, Type type) {
+ int numParams = getNumParameters();
+ if (index >= numParams) {
+ ClavaLog.info(
+ "Cannot set type for param at index '" + index + "', function has only '" + numParams + "' params");
+ return;
+ }
+
+ // Set type of parameter
+ var param = getChild(ParmVarDecl.class, index);
+ param.setType(type);
+
+ // Set type of the function type
+ var newFunctionType = ((FunctionType) getFunctionType().copy());
+ newFunctionType.setParamType(index, type);
+ setFunctionType(newFunctionType);
}
public String getSignature() {
@@ -872,4 +902,5 @@ public class FunctionDecl extends DeclaratorDecl {
getDeclaration().ifPresent(decl -> decl.setType(type));
getDefinition().ifPresent(decl -> decl.setType(type));
}
+
}
