Skip to content

Information about variable declarations in callables #13

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Apr 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
}