def visit_Call(self, node): if isinstance(node.func, ast.Name): # Catch bool, and interpret it as a set emptyness check. if node.func.id == 'bool': if len(node.args) != 1: raise PythonToDARhiError('Invalid call') iter = self.visit(node.args[0]) if self.objectdomain: return dha.Match(dha.PatMatch(dha.genUnboundVar(st.getFreshNSymbol()), iter), dka.Symtab()) else: dka.assertnodetype(iter, dha.Name) return dha.Match(dha.PatMatchName(dha.genUnboundVar(st.getFreshNSymbol()), iter.id), dka.Symtab()) # Catch hasattr. elif node.func.id == 'hasattr': if len(node.args) != 2: raise PythonToDARhiError('Invalid call') if not isinstance(node.args[1], ast.Str): raise PythonToDARhiError('Reflection not allowed in hasattr') return dha.HasAttr(self.visit(node.args[0]), node.args[1].s) elif isinstance(node.func, ast.Attribute): # Catch any. if node.func.attr == 'any': if len(node.args) != 0: raise PythonToDARhiError('Invalid call') return dha.UnaryOp(dha.Any(), self.visit(node.func.value)) return self.call_helper(node, isstmt=False)
def genUnionDiffCode(leftsym, rightsym, isunion, mod): v = st.getFreshNSymbol() simpleop = dha.UpAdd(mod) if isunion else dha.UpRemove(mod) update = [dha.SetUpdate(leftsym, simpleop, dha.Name(v))] code = [dha.For(dha.PatMatchName(dha.genUnboundVar(v), rightsym), dha.Block(update))] return code
def genClearCode(setsym, mod): """Generate code to empty a set.""" v = st.getFreshNSymbol() match = dha.PatMatchName(dha.genUnboundVar(v), setsym) update = [dha.SetUpdate(setsym, dha.UpRemove(mod), dha.Name(v))] code = [dha.PatWhile(match, dha.Block(update))] return code
def visit_PatVar(self, node): v = st.getFreshNSymbol() if dha.isBoundPatVar(node): self.bounds.append(dha.Name(v)) return dha.genUnboundVar(v) else: self.unbounds.append(dha.Name(v)) return dha.genUnboundVar(v)
def visit_Pick2nd(self, node): self.generic_visit(node) tup = dha.PatTuple([dha.PatExpr(node.key), dha.genUnboundVar(st.getFreshNSymbol())]) mask, bounds, unbounds = getPatInfo(tup) ssym = node.id auxsym = self.getAuxSym(ssym, mask, tup) code = dha.UnaryOp(dha.Any(), dha.Lookup(auxsym, bounds)) return code
def visit_PatIgnore(self, node): return dha.genUnboundVar(st.getFreshNSymbol())
def visit_PatExpr(self, node): v = st.getFreshNSymbol() self.bounds.append(dha.Name(v)) return dha.genUnboundVar(v)
def visit_HasAttr(self, node): self.generic_visit(node) fs = self.getFieldSym(node.attr, node.value._t) tup = dha.PatTuple([dha.PatExpr(node.value), dha.genUnboundVar(st.getFreshNSymbol())]) return dha.Match(dha.PatMatchName(tup, fs), dka.Symtab())