def visit_Attribute(self, n): if (isinstance(n.value, typed_ast.Call) and isinstance(n.value.func, typed_ast.Name) and n.value.func.id == 'super'): return SuperExpr(n.attr) return MemberExpr(self.visit(n.value), n.attr)
def visit_Attribute(self, n: ast35.Attribute) -> Node: if (isinstance(n.value, ast35.Call) and isinstance(n.value.func, ast35.Name) and n.value.func.id == 'super'): return SuperExpr(n.attr) return MemberExpr(self.visit(n.value), n.attr)
def visit_Attribute(self, n: ast27.Attribute) -> Expression: if (isinstance(n.value, ast27.Call) and isinstance(n.value.func, ast27.Name) and n.value.func.id == 'super'): return SuperExpr(n.attr, self.visit(n.value)) return MemberExpr(self.visit(n.value), n.attr)
def make_superclass_constructor_call( self, info: TypeInfo, callee_type: Callable) -> ExpressionStmt: """Construct a statement that calls the superclass constructor. In particular, it passes any type variables arguments as needed. """ callee = SuperExpr('__init__') callee.info = info # We do not handle generic constructors. Either pass runtime # type variables from the current scope or perhaps require # explicit constructor in this case. selftype = self_type(info) # FIX overloading # FIX default args / varargs # Map self type to the superclass context. base = info.mro[1] selftype = map_instance_to_supertype(selftype, base) super_init = cast(FuncDef, base.get_method('__init__')) # Add constructor arguments. args = [] # type: List[Node] for n in range(1, callee_type.min_args): args.append(NameExpr(super_init.args[n].name())) self.tf.set_type(args[-1], callee_type.arg_types[n]) # Store callee type after stripping away the 'self' type. self.tf.set_type(callee, nodes.method_callable(callee_type)) call = CallExpr(callee, args, [nodes.ARG_POS] * len(args)) return ExpressionStmt(call)
def visit_Attribute(self, n: Attribute) -> Union[MemberExpr, SuperExpr]: value = n.value member_expr = MemberExpr(self.visit(value), n.attr) obj = member_expr.expr if (isinstance(obj, CallExpr) and isinstance(obj.callee, NameExpr) and obj.callee.name == 'super'): e = SuperExpr(member_expr.name, obj) # type: Union[MemberExpr, SuperExpr] else: e = member_expr return self.set_line(e, n)
def visit_Attribute(self, n: Attribute) -> Expression: # First create MemberExpr and then potentially replace with a SuperExpr # to improve performance when compiled. The check for "super()" will be # faster with native AST nodes. Note also that super expressions are # less common than normal member expressions. member_expr = MemberExpr(self.visit(n.value), n.attr) obj = member_expr.expr if (isinstance(obj, CallExpr) and isinstance(obj.callee, NameExpr) and obj.callee.name == 'super'): e: Expression = SuperExpr(member_expr.name, obj) else: e = member_expr return self.set_line(e, n)
def make_generic_wrapper_init(self, info: TypeInfo) -> FuncDef: """Build constructor of a generic wrapper class.""" nslots = num_slots(info) cdefs = [] # type: List[Node] # Build superclass constructor call. base = info.mro[1] if base.fullname() != 'builtins.object' and self.tf.is_java: s = SuperExpr('__init__') cargs = [NameExpr('__o')] # type: List[Node] for n in range(num_slots(base)): cargs.append(NameExpr(tvar_arg_name(n + 1))) for n in range(num_slots(base)): cargs.append(NameExpr(tvar_arg_name(n + 1, BOUND_VAR))) c = CallExpr(s, cargs, [nodes.ARG_POS] * len(cargs)) cdefs.append(ExpressionStmt(c)) # Create initialization of the wrapped object. cdefs.append(AssignmentStmt([MemberExpr( self_expr(), self.object_member_name(info), direct=True)], NameExpr('__o'))) # Build constructor arguments. args = [Var('self'), Var('__o')] init = [None, None] # type: List[Node] for alt in [False, BOUND_VAR]: for n in range(nslots): args.append(Var(tvar_arg_name(n + 1, alt))) init.append(None) nargs = nslots * 2 + 2 fdef = FuncDef('__init__', args, [nodes.ARG_POS] * nargs, init, Block(cdefs), Callable( [AnyType()] * nargs, [nodes.ARG_POS] * nargs, [None] * nargs, Void(), is_type_obj=False)) fdef.info = info self.make_wrapper_slot_initializer(fdef) return fdef
def visit_super_expr(self, node: SuperExpr) -> Node: new = SuperExpr(node.name) new.info = node.info return new
def visit_super_expr(self, node: SuperExpr) -> SuperExpr: call = self.expr(node.call) assert isinstance(call, CallExpr) new = SuperExpr(node.name, call) new.info = node.info return new