diff --git a/com.ibm.wala.cast.python/source/com/ibm/wala/cast/python/ir/PythonCAstToIRTranslator.java b/com.ibm.wala.cast.python/source/com/ibm/wala/cast/python/ir/PythonCAstToIRTranslator.java index 7e4a006e6..c7dadfbae 100644 --- a/com.ibm.wala.cast.python/source/com/ibm/wala/cast/python/ir/PythonCAstToIRTranslator.java +++ b/com.ibm.wala.cast.python/source/com/ibm/wala/cast/python/ir/PythonCAstToIRTranslator.java @@ -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; @@ -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); @@ -267,8 +277,8 @@ protected void leaveFunctionEntity(CAstEntity n, WalkContext context, WalkContex CAstVisitor 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]; @@ -278,6 +288,31 @@ protected void leaveFunctionEntity(CAstEntity n, WalkContext context, WalkContex } } + protected void leaveDeclStmt(CAstNode n, WalkContext c, CAstVisitor 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 visitor) { WalkContext context = c; @@ -596,7 +631,8 @@ protected boolean doVisit(CAstNode n, WalkContext context, CAstVisitor 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 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); @@ -1253,6 +1257,9 @@ private CAstNode doGenerators(java.util.List 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, diff --git a/com.ibm.wala.cast.python/source/com/ibm/wala/cast/python/ssa/PythonImportFromGetInstruction.java b/com.ibm.wala.cast.python/source/com/ibm/wala/cast/python/ssa/PythonImportFromGetInstruction.java new file mode 100644 index 000000000..25ff17f17 --- /dev/null +++ b/com.ibm.wala.cast.python/source/com/ibm/wala/cast/python/ssa/PythonImportFromGetInstruction.java @@ -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()); + } +} diff --git a/com.ibm.wala.cast.python/source/com/ibm/wala/cast/python/types/PythonTypes.java b/com.ibm.wala.cast.python/source/com/ibm/wala/cast/python/types/PythonTypes.java index 9fda2be88..cde2e1909 100644 --- a/com.ibm.wala.cast.python/source/com/ibm/wala/cast/python/types/PythonTypes.java +++ b/com.ibm.wala.cast.python/source/com/ibm/wala/cast/python/types/PythonTypes.java @@ -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")); + }