optimize dependent function parse

support merge importNodes
This commit is contained in:
CaiHQ 2023-07-24 11:35:51 +08:00
parent bc7a54e9ce
commit 374451dd5b
3 changed files with 52 additions and 11 deletions

View File

@ -55,7 +55,7 @@ dependencies {
} }
group = "org.bdware.sc" group = "org.bdware.sc"
version = "1.7.6" version = "1.7.8"
tasks.processResources.setDuplicatesStrategy(DuplicatesStrategy.INCLUDE) tasks.processResources.setDuplicatesStrategy(DuplicatesStrategy.INCLUDE)
task copyLibs(type: Copy) { task copyLibs(type: Copy) {

View File

@ -130,6 +130,7 @@ public class ContractNode {
public void merge(ContractNode contract) { public void merge(ContractNode contract) {
sigRequired |= contract.sigRequired; sigRequired |= contract.sigRequired;
instrumentBranch |= contract.instrumentBranch; instrumentBranch |= contract.instrumentBranch;
imports.addAll(contract.getImports());
for (FunctionNode fn : contract.functions) { for (FunctionNode fn : contract.functions) {
functions.add(fn); functions.add(fn);
functionMap.put(fn.functionName, fn); functionMap.put(fn.functionName, fn);
@ -237,7 +238,7 @@ public class ContractNode {
} }
public void maintainRouteJoinInfo(JsonObject methodRouteInfoMap, JsonObject methodJoinInfoMap, public void maintainRouteJoinInfo(JsonObject methodRouteInfoMap, JsonObject methodJoinInfoMap,
JsonObject dependentFunctions) { JsonObject dependentFunctions) {
// all functions存了ContractNode中所有的FunctionNode // all functions存了ContractNode中所有的FunctionNode
List<FunctionNode> allFunctions = getFunctions(); List<FunctionNode> allFunctions = getFunctions();
for (FunctionNode functionNode : allFunctions) { for (FunctionNode functionNode : allFunctions) {
@ -262,10 +263,33 @@ public class ContractNode {
} }
} }
} }
for (InterfaceNode interfaceNode : interfaceMap.values()) {
AnnotationNode doopAnnotation = interfaceNode.getAnnotation("DOOP");
DoipOperationInfo doipOperationInfo = interfaceNode.getDoipOperationInfo();
if (doopAnnotation != null && doipOperationInfo != null) {
RouteInfo routeInfo = interfaceNode.getRouteInfo();
JoinInfo joinInfo = interfaceNode.getJoinInfo();
if (routeInfo != null) {
packSourceFunctionAndDependentFunctions(getFunction(routeInfo.funcName),
dependentFunctions);
methodRouteInfoMap.add(doipOperationInfo.operationName,
JsonUtil.parseObjectAsJsonObject(routeInfo));
}
if (joinInfo != null) {
packSourceFunctionAndDependentFunctions(getFunction(joinInfo.joinCountFuncName),
dependentFunctions);
packSourceFunctionAndDependentFunctions(getFunction(joinInfo.joinFuncName),
dependentFunctions);
methodJoinInfoMap.add(doipOperationInfo.operationName,
JsonUtil.parseObjectAsJsonObject(joinInfo));
}
}
}
} }
public void packSourceFunctionAndDependentFunctions(FunctionNode sourceFunctionNode, public void packSourceFunctionAndDependentFunctions(FunctionNode sourceFunctionNode,
JsonObject functions) { JsonObject functions) {
if (sourceFunctionNode == null) if (sourceFunctionNode == null)
return; return;
functions.addProperty(sourceFunctionNode.functionName, sourceFunctionNode.plainText()); functions.addProperty(sourceFunctionNode.functionName, sourceFunctionNode.plainText());
@ -291,8 +315,16 @@ public class ContractNode {
+ " -> " + annotationNode.getType()); + " -> " + annotationNode.getType());
} }
functionNode.annotations.addAll(node.annotations); functionNode.annotations.addAll(node.annotations);
} else } else {
throw new RuntimeException("unimplemented functions:" + node.functionName); //now we accept only interfaces.
//just ignore!
// throw new RuntimeException("unimplemented functions:" + node.functionName);
}
} }
} }
public Collection<InterfaceNode> getInterfaces() {
return interfaceMap.values();
}
} }

View File

@ -5,6 +5,8 @@ import org.bdware.sc.node.FunctionNode;
import org.bdware.sc.parser.YJSParser; import org.bdware.sc.parser.YJSParser;
import org.bdware.sc.parser.YJSParserBaseVisitor; import org.bdware.sc.parser.YJSParserBaseVisitor;
import java.util.List;
public class FunctionDependencyVisitor extends YJSParserBaseVisitor<FunctionNode> { public class FunctionDependencyVisitor extends YJSParserBaseVisitor<FunctionNode> {
public static String APPLY = ".apply"; public static String APPLY = ".apply";
@ -21,12 +23,19 @@ public class FunctionDependencyVisitor extends YJSParserBaseVisitor<FunctionNode
@Override @Override
public FunctionNode visitArgumentsExpression(YJSParser.ArgumentsExpressionContext ctx) { public FunctionNode visitArgumentsExpression(YJSParser.ArgumentsExpressionContext ctx) {
super.visitArgumentsExpression(ctx); super.visitArgumentsExpression(ctx);
for (FunctionNode f : cn.getFunctions()) { String funText = ctx.singleExpression().getText();
if (ctx.singleExpression().getText().equals(f.functionName) if (funText.endsWith(APPLY)) funText = funText.replaceAll(APPLY+"$","");
|| ctx.singleExpression().getText().equals(f.functionName + APPLY) if (funText.endsWith(CALL)) funText = funText.replaceAll(CALL+"$","");
|| ctx.singleExpression().getText().equals(f.functionName + CALL)) { if (cn.getFunction(funText)!=null)
fn.addDependentFunctions(f.functionName); fn.addDependentFunctions(funText);
break; if (ctx.arguments() != null) {
List<YJSParser.SingleExpressionContext> singleExpress = ctx.arguments().singleExpression();
if (singleExpress != null && singleExpress.size() > 0) {
for (YJSParser.SingleExpressionContext sin : singleExpress) {
funText = sin.getText();
if (cn.getFunction(funText)!=null)
fn.addDependentFunctions(funText);
}
} }
} }
return fn; return fn;