Skip to content

Commit

Permalink
Merge pull request #13 from sinha108/var-decl-info
Browse files Browse the repository at this point in the history
Information about variable declarations in callables
  • Loading branch information
rangeetpan authored Apr 26, 2024
2 parents 6b45091 + df69040 commit 70a2fa6
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 1 deletion.
37 changes: 36 additions & 1 deletion src/main/java/com/ibm/northstar/SymbolTable.java
Original file line number Diff line number Diff line change
Expand Up @@ -301,6 +301,7 @@ private static Pair<String, Callable> processCallableDeclaration(CallableDeclara
callableNode.setCalledMethodDeclaringTypes(getCalledMethodDeclaringTypes(body));
callableNode.setAccessedFields(getAccessedFields(body, classFields, typeName));
callableNode.setCallSites(getCallSites(body));
callableNode.setVariableDeclarations(getVariableDeclarations(body));

String callableSignature = (callableDecl instanceof MethodDeclaration) ? callableDecl.getSignature().asString() : callableDecl.getSignature().asString().replace(callableDecl.getSignature().getName(), "<init>");
return Pair.of(callableSignature, callableNode);
Expand Down Expand Up @@ -359,7 +360,41 @@ private static List<String> getReferencedTypes(Optional<BlockStmt> blockStmt) {
}

/**
* Computes and returns the list if fields accessed in the given callable body. The returned values contain
* Returns information about variable declarations in the given callable. The information includes
* var name, var type, var initializer, and position.
*
* @param blockStmt Callable to compute var declaration information for
* @return list of variable declarations
*/
private static List<VariableDeclaration> getVariableDeclarations(Optional<BlockStmt> blockStmt) {
List<VariableDeclaration> varDeclarations = new ArrayList<>();
if (blockStmt.isEmpty()) {
return varDeclarations;
}
for (VariableDeclarator declarator : blockStmt.get().findAll(VariableDeclarator.class)) {
VariableDeclaration varDeclaration = new VariableDeclaration();
varDeclaration.setName(declarator.getNameAsString());
varDeclaration.setType(resolveType(declarator.getType()));
varDeclaration.setInitializer(declarator.getInitializer().isPresent() ?
declarator.getInitializer().get().toString() : "");
if (declarator.getRange().isPresent()) {
varDeclaration.setStartLine(declarator.getRange().get().begin.line);
varDeclaration.setStartColumn(declarator.getRange().get().begin.column);
varDeclaration.setEndLine(declarator.getRange().get().end.line);
varDeclaration.setEndColumn(declarator.getRange().get().end.column);
} else {
varDeclaration.setStartLine(-1);
varDeclaration.setStartColumn(-1);
varDeclaration.setEndLine(-1);
varDeclaration.setEndColumn(-1);
}
varDeclarations.add(varDeclaration);
}
return varDeclarations;
}

/**
* Computes and returns the list of fields accessed in the given callable body. The returned values contain
* field names qualified by names of the declaring types.
*
* @param callableBody Callable body to compute accessed fields for
Expand Down
1 change: 1 addition & 0 deletions src/main/java/com/ibm/northstar/entities/Callable.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,5 @@ public class Callable {
private List<String> accessedFields;
private List<String> calledMethodDeclaringTypes;
private List<CallSite> callSites;
private List<VariableDeclaration> variableDeclarations;
}
16 changes: 16 additions & 0 deletions src/main/java/com/ibm/northstar/entities/VariableDeclaration.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package com.ibm.northstar.entities;

import lombok.Data;

import java.util.List;

@Data
public class VariableDeclaration {
private String name;
private String type;
private String initializer;
private int startLine;
private int startColumn;
private int endLine;
private int endColumn;
}

0 comments on commit 70a2fa6

Please sign in to comment.