Skip to content
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

Changes in imports and one minor fix for globals. #8

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import com.ibm.wala.cast.tree.CAstEntity;
import com.ibm.wala.cast.tree.CAstNode;
import com.ibm.wala.cast.tree.CAstSourcePositionMap.Position;
import com.ibm.wala.cast.tree.CAstSymbol;
import com.ibm.wala.cast.tree.CAstType;
import com.ibm.wala.cast.tree.impl.CAstOperator;
import com.ibm.wala.cast.tree.impl.CAstSymbolImpl;
Expand Down Expand Up @@ -229,7 +230,16 @@ public void doArrayWrite(WalkContext context, int arrayValue, CAstNode arrayRef,
protected void doFieldRead(WalkContext context, int result, int receiver, CAstNode elt, CAstNode parent) {
int currentInstruction = context.cfg().getCurrentInstruction();
if (elt.getKind() == CAstNode.CONSTANT && elt.getValue() instanceof String) {
FieldReference f = FieldReference.findOrCreate(PythonTypes.Root, Atom.findOrCreateUnicodeAtom((String)elt.getValue()), PythonTypes.Root);
FieldReference f;
if(parent.getChildCount() > 1
&& parent.getChild(0).getKind() == CAstNode.PRIMITIVE
&& parent.getChild(0).getChildCount() == 2
&& parent.getChild(0).getChild(0).getValue().equals("import")){
f = FieldReference.findOrCreate(PythonTypes.module, Atom.findOrCreateUnicodeAtom((String)elt.getValue()), PythonTypes.Root);
}
else
f = FieldReference.findOrCreate(PythonTypes.Root, Atom.findOrCreateUnicodeAtom((String)elt.getValue()), PythonTypes.Root);

context.cfg().addInstruction(Python.instructionFactory().GetInstruction(currentInstruction, result, receiver, f));
} else {
visit(elt, context, this);
Expand Down Expand Up @@ -267,8 +277,8 @@ protected void leaveFunctionEntity(CAstEntity n, WalkContext context, WalkContex
CAstVisitor<WalkContext> visitor) {
super.leaveFunctionEntity(n, context, codeContext, visitor);

String fnName = composeEntityName(context, n) + "_defaults";
if (n.getArgumentDefaults() != null) {
String fnName = composeEntityName(context, n) + "_defaults";
if (n.getArgumentDefaults() != null && n.getArgumentDefaults().length > 0) {
int first = n.getArgumentCount() - n.getArgumentDefaults().length;
for(int i = first; i < n.getArgumentCount(); i++) {
CAstNode dflt = n.getArgumentDefaults()[i - first];
Expand All @@ -278,6 +288,31 @@ protected void leaveFunctionEntity(CAstEntity n, WalkContext context, WalkContex
}
}

protected void leaveDeclStmt(CAstNode n, WalkContext c, CAstVisitor<WalkContext> visitor) {
CAstSymbol s = (CAstSymbol) n.getChild(0).getValue();
String nm = s.name();
CAstType t = s.type();
Scope scope = c.currentScope();
if (n.getChildCount() == 2) {
CAstNode v = n.getChild(1);
if(isGlobal(c, nm)){
scope.declare(s);
doGlobalWrite(c, nm, makeType(t), c.getValue(v));
}
else if (scope.contains(nm) && scope.lookup(nm).getDefiningScope() == scope) {
assert !s.isFinal();
doLocalWrite(c, nm, makeType(t), c.getValue(v));
} else if (v.getKind() != CAstNode.CONSTANT && v.getKind() != CAstNode.VAR && v.getKind() != CAstNode.THIS) {
scope.declare(s, c.getValue(v));
} else {
scope.declare(s);
doLocalWrite(c, nm, makeType(t), c.getValue(v));
}
} else {
c.currentScope().declare(s);
}
}

@Override
protected void leaveVar(CAstNode n, WalkContext c, CAstVisitor<WalkContext> visitor) {
WalkContext context = c;
Expand Down Expand Up @@ -596,7 +631,8 @@ protected boolean doVisit(CAstNode n, WalkContext context, CAstVisitor<WalkConte
context.setValue(n, result);
return true;

} else if(n.getKind() == CAstNode.GLOBAL_DECL){
}
else if(n.getKind() == CAstNode.GLOBAL_DECL){
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we revert these formatting changes so that conflicts could be more easily resolved?

int numOfChildren = n.getChildCount();
for(int i = 0;i < numOfChildren; i++){
String val = (String) n.getChild(i).getChild(0).getValue();
Expand All @@ -605,7 +641,8 @@ protected boolean doVisit(CAstNode n, WalkContext context, CAstVisitor<WalkConte
}
return true;

} else {
}
else {
return super.doVisit(n, context, visitor);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,7 @@
import com.ibm.wala.cast.python.cfg.PythonInducedCFG;
import com.ibm.wala.cast.python.modref.PythonModRef.PythonModVisitor;
import com.ibm.wala.cast.python.modref.PythonModRef.PythonRefVisitor;
import com.ibm.wala.cast.python.ssa.PythonInvokeInstruction;
import com.ibm.wala.cast.python.ssa.PythonPropertyRead;
import com.ibm.wala.cast.python.ssa.PythonPropertyWrite;
import com.ibm.wala.cast.python.ssa.PythonStoreProperty;
import com.ibm.wala.cast.python.ssa.*;
import com.ibm.wala.cast.python.types.PythonTypes;
import com.ibm.wala.cast.types.AstMethodReference;
import com.ibm.wala.cfg.InducedCFG;
Expand All @@ -53,10 +50,7 @@
import com.ibm.wala.ipa.modref.ModRef.RefVisitor;
import com.ibm.wala.shrikeCT.BootstrapMethodsReader.BootstrapMethod;
import com.ibm.wala.shrikeCT.InvalidClassFileException;
import com.ibm.wala.ssa.SSAAbstractInvokeInstruction;
import com.ibm.wala.ssa.SSAArrayStoreInstruction;
import com.ibm.wala.ssa.SSAInstruction;
import com.ibm.wala.ssa.SSAInvokeInstruction;
import com.ibm.wala.ssa.*;
import com.ibm.wala.types.FieldReference;
import com.ibm.wala.types.MethodReference;
import com.ibm.wala.types.TypeName;
Expand Down Expand Up @@ -248,6 +242,14 @@ public EachElementGetInstruction EachElementGetInstruction(int iindex, int value
public AstYieldInstruction YieldInstruction(int iindex, int[] rvals) {
return new AstYieldInstruction(iindex, rvals);
}

@Override
public SSAGetInstruction GetInstruction(int iindex, int result, int ref, FieldReference field){
if(field.getDeclaringClass().getName().toString().equals("Lmodule"))
return new PythonImportFromGetInstruction(iindex, result, ref, field);

return super.GetInstruction(iindex, result, ref, field);
}
};
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1021,7 +1021,8 @@ public CAstNode visitIfExp(IfExp arg0) throws Exception {
private String name(alias n) {
String s = n.getInternalAsname()==null? n.getInternalName(): n.getInternalAsname();
if (s.contains(".")) {
s = s.substring(0, s.indexOf('.'));
//s = s.substring(0, s.indexOf('.'));
s = s.replace(".","/");
}
return s;
}
Expand All @@ -1042,32 +1043,35 @@ public CAstNode visitImport(Import arg0) throws Exception {

@Override
public CAstNode visitImportFrom(ImportFrom arg0) throws Exception {
CAstNode[] elts = new CAstNode[ arg0.getInternalNames().size() ];
CAstNode[] elts = new CAstNode[ arg0.getInternalNames().size() *2];
int i = 0;
java.util.List<Name> names = arg0.getInternalModuleNames();

StringBuilder sb = new StringBuilder();
String prev = ".";
for(Name name: names){
if(!prev.equals("."))
sb.append(".");
sb.append(name.getInternalId());
prev = name.getInternalId();
}
String moduleName = sb.toString();
for(alias n : arg0.getInternalNames()) {
elts[i++] = Ast.makeNode(CAstNode.DECL_STMT,
Ast.makeConstant(new CAstSymbolImpl(name(n), PythonCAstToIRTranslator.Any)),
Ast.makeConstant(new CAstSymbolImpl(name(n), PythonCAstToIRTranslator.Any)));
CAstNode importAst = Ast.makeNode(CAstNode.PRIMITIVE,
Ast.makeConstant("import"),
Ast.makeConstant(moduleName.replace(".", "/")));
elts[i++] = Ast.makeNode(CAstNode.ASSIGN,
Ast.makeNode(CAstNode.VAR, Ast.makeConstant(name(n))),
Ast.makeNode(CAstNode.OBJECT_REF,
importAst(arg0),
importAst,
Ast.makeConstant(n.getInternalName())));
}

return Ast.makeNode(CAstNode.BLOCK_STMT, elts);
}

private CAstNode importAst(ImportFrom arg0) {
java.util.List<Name> names = arg0.getInternalModuleNames();
CAstNode importAst = Ast.makeNode(CAstNode.PRIMITIVE,
Ast.makeConstant("import"),
Ast.makeConstant(names.get(0).getInternalId()));
for(int i = 1; i < names.size(); i++) {
importAst = Ast.makeNode(CAstNode.OBJECT_REF,
importAst,
Ast.makeConstant(names.get(i).getInternalId()));
}
return importAst;
}

@Override
public CAstNode visitIndex(Index arg0) throws Exception {
return arg0.getInternalValue().accept(this);
Expand Down Expand Up @@ -1253,6 +1257,9 @@ private CAstNode doGenerators(java.util.List<comprehension> generators, CAstNode
result = Ast.makeNode(CAstNode.BLOCK_EXPR,
Ast.makeNode(CAstNode.DECL_STMT, Ast.makeConstant(new CAstSymbolImpl(tempName, PythonCAstToIRTranslator.Any)),
c.getInternalIter().accept(this)),
Ast.makeNode(CAstNode.ASSIGN,
c.getInternalTarget().accept(this),
Ast.makeConstant(null)),
Ast.makeNode(CAstNode.LOOP,
test,
Ast.makeNode(CAstNode.BLOCK_EXPR,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package com.ibm.wala.cast.python.ssa;

import com.ibm.wala.ssa.SSAGetInstruction;
import com.ibm.wala.ssa.SymbolTable;
import com.ibm.wala.types.FieldReference;

public class PythonImportFromGetInstruction extends SSAGetInstruction {
public PythonImportFromGetInstruction(int iindex, int result, int ref, FieldReference field){
super(iindex, result, ref, field);
}

public PythonImportFromGetInstruction(int iindex, int result, FieldReference field){
super(iindex, result, field);
}

public String toString(SymbolTable symbolTable){
return getValueString(symbolTable, getDef()) + " = importFromGet " + getDeclaredField() + " "
+ getValueString(symbolTable, getRef());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -52,4 +52,6 @@ public class PythonTypes extends AstTypeReference {

public static final TypeReference trampoline = TypeReference.findOrCreate(pythonLoader, TypeName.findOrCreate("Ltrampoline"));

public static final TypeReference module = TypeReference.findOrCreate(pythonLoader, TypeName.findOrCreate("Lmodule"));

}