[ClavaWeaver] Implements wrap for definition-only functions
The wrapper is generated 'in-place', right after the original function
on which it is based.
@@ -72,7 +72,7 @@ public class CallWrap {
public CallWrap(CxxCall cxxCall) {
this.cxxCall = cxxCall;
- this.app = cxxCall.getRootImpl();
+ app = cxxCall.getRootImpl();
}
public void addWrapper(String name) {
@@ -96,9 +96,12 @@ public class CallWrap {
createUserIncludeWrapper(name);
break;
case NO_INCLUDE:
- SpecsLogs.msgInfo(
- "action 'wrap' is not supported yet when the call refers to a function that has no declaration in a header file: "
- + cxxCall.getNode().getLocation());
+ // SpecsLogs.msgInfo(
+ // "action 'wrap' is not supported yet when the call refers to a function that has no declaration in a
+ // header file: "
+ // + cxxCall.getNode().getLocation());
+ createInPlaceWrapper(name);
+ cxxCall.setName(name); // nned to call this here before returning
return;
case DECLARATION_IN_IMPLEMENTATION:
SpecsLogs.msgInfo(
@@ -125,6 +128,17 @@ public class CallWrap {
}
+ /**
+ * Creates a wrapper below the found implementation of the original function.
+ *
+ * @param name
+ */
+ private void createInPlaceWrapper(String name) {
+
+ FunctionDecl declaration = cxxCall.getDefinitionImpl().getNode();
+ addWrapperFunctionInPlace(name, declaration);
+ }
+
/**
* Creates a wrapper function based on the declaration of the function call.
*
@@ -177,6 +191,29 @@ public class CallWrap {
app.getAppData().get(WRAPPER_CALLS).add(name);
}
+ private void addWrapperFunctionInPlace(String name, FunctionDecl declaration) {
+
+ FunctionDecl wrapperFunctionDeclImpl = (FunctionDecl) declaration.copy();
+ wrapperFunctionDeclImpl.setDeclName(name);
+
+ // Create code that calls previous function
+ List<String> paramNames = wrapperFunctionDeclImpl.getParameters().stream()
+ .map(param -> param.getDeclName())
+ .collect(Collectors.toList());
+
+ List<Stmt> functionCallCode = createFunctionCallCode(paramNames);
+
+ wrapperFunctionDeclImpl.setBody(ClavaNodeFactory.compoundStmt(null, functionCallCode));
+
+ // add to original file
+ TranslationUnit originalFile = declaration.getAncestor(TranslationUnit.class);
+ int indexOfOriginal = declaration.indexOfSelf();
+ originalFile.addChild(indexOfOriginal + 1, wrapperFunctionDeclImpl);
+
+ // Save function implementation declaration
+ app.getAppData().get(WRAPPER_CALLS).add(name);
+ }
+
private void createSystemIncludeWrapper(String name) {
// Get wrapper implementation file
