Skip to content

Commit

Permalink
Merge pull request #27 from sinha108/main
Browse files Browse the repository at this point in the history
Type resolution improvements
  • Loading branch information
rangeetpan authored Jun 11, 2024
2 parents 61190ff + a257866 commit 9dbae80
Showing 1 changed file with 24 additions and 17 deletions.
41 changes: 24 additions & 17 deletions src/main/java/com/ibm/northstar/SymbolTable.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@
public class SymbolTable {

private static JavaSymbolSolver javaSymbolSolver;
private static Set<String> unresolvedTypes = new HashSet<>();
private static Set<String> unresolvedExpressions = new HashSet<>();

/**
* Processes the given compilation unit to extract information about classes and
Expand Down Expand Up @@ -408,6 +410,7 @@ private static List<String> getAccessedFields(Optional<BlockStmt> callableBody,
// process field access expressions in the callable
callableBody.ifPresent(cb -> cb.findAll(FieldAccessExpr.class)
.stream()
.filter(faExpr -> faExpr.getParentNode().isPresent() && !(faExpr.getParentNode().get() instanceof FieldAccessExpr))
.map(faExpr -> {
String fieldDeclaringType = resolveExpression(faExpr.getScope());
if (!fieldDeclaringType.isEmpty()) {
Expand Down Expand Up @@ -474,12 +477,8 @@ private static List<CallSite> getCallSites(Optional<BlockStmt> callableBody) {

for (ObjectCreationExpr objectCreationExpr : callableBody.get().findAll(ObjectCreationExpr.class)) {
// resolve declaring type for called method
String instantiatedType = objectCreationExpr.getTypeAsString();
try {
instantiatedType = objectCreationExpr.getType().resolve().describe();
} catch (UnsolvedSymbolException | IllegalStateException e) {
Log.warn("Could not resolve "+instantiatedType+": "+e.getMessage());
}
String instantiatedType = resolveType(objectCreationExpr.getType());

// resolve arguments of the constructor call to types
List<String> arguments = objectCreationExpr.getArguments().stream()
.map(arg -> resolveExpression(arg)).collect(Collectors.toList());
Expand Down Expand Up @@ -538,13 +537,17 @@ private static CallSite createCallSite(Expression callExpr, String calleeName, S
* @return Resolved type name or empty string if type resolution fails
*/
private static String resolveExpression(Expression expression) {
try {
ResolvedType resolvedType = javaSymbolSolver.calculateType(expression);
if (resolvedType.isReferenceType() || resolvedType.isUnionType()) {
return resolvedType.describe();
// perform expression resolution if resolution of this expression did not fail previously
if (!unresolvedExpressions.contains(expression.toString())) {
try {
ResolvedType resolvedType = javaSymbolSolver.calculateType(expression);
if (resolvedType.isReferenceType() || resolvedType.isUnionType()) {
return resolvedType.describe();
}
} catch (RuntimeException exception) {
Log.debug("Could not resolve expression: " + expression + ": " + exception.getMessage());
unresolvedExpressions.add(expression.toString());
}
} catch (RuntimeException exception) {
Log.warn("Could not resolve expression: "+expression+"\n"+exception.getMessage());
}
return "";
}
Expand All @@ -556,12 +559,16 @@ private static String resolveExpression(Expression expression) {
* @return Resolved (qualified) type name
*/
private static String resolveType(Type type) {
try {
return type.resolve().describe();
} catch (UnsolvedSymbolException | IllegalStateException | MethodAmbiguityException e) {
Log.warn("Could not resolve "+type.asString()+": "+e.getMessage());
return type.asString();
// perform type resolution if resolution of this type did not fail previously
if (!unresolvedTypes.contains(type.asString())) {
try {
return type.resolve().describe();
} catch (RuntimeException e) {
Log.warn("Could not resolve type: " + type.asString() + ": " + e.getMessage());
unresolvedTypes.add(type.asString());
}
}
return type.asString();
}

/**
Expand Down

0 comments on commit 9dbae80

Please sign in to comment.