def visit_cython_attribute(self, node): attribute = node.as_cython_attribute() if attribute: if attribute == u'compiled': node = BoolNode(node.pos, value=True) elif attribute == u'NULL': node = NullNode(node.pos) elif not PyrexTypes.parse_basic_type(attribute): error(node.pos, u"'%s' not a valid cython attribute or is being used incorrectly" % attribute) return node
def visit_PyClassDefNode(self, node): pxd_def = self.scope.lookup(node.name) if pxd_def: if pxd_def.is_cclass: return self.visit_CClassDefNode(node.as_cclass(), pxd_def) else: error(node.pos, "'%s' redeclared" % node.name) error(pxd_def.pos, "previous declaration here") return None else: return node
def visit_cython_attribute(self, node): attribute = node.as_cython_attribute() if attribute: if attribute == u'compiled': node = BoolNode(node.pos, value=True) elif attribute == u'NULL': node = NullNode(node.pos) elif not PyrexTypes.parse_basic_type(attribute): error( node.pos, u"'%s' not a valid cython attribute or is being used incorrectly" % attribute) return node
def visit_DefNode(self, node): pxd_def = self.scope.lookup(node.name) if pxd_def: if self.scope.is_c_class_scope and len(pxd_def.type.args) > 0: # The self parameter type needs adjusting. pxd_def.type.args[0].type = self.scope.parent_type if pxd_def.is_cfunction: node = node.as_cfunction(pxd_def) else: error(node.pos, "'%s' redeclared" % node.name) error(pxd_def.pos, "previous declaration here") return None elif self.scope.is_module_scope and self.directives['auto_cpdef']: node = node.as_cfunction(scope=self.scope) # Enable this when internal def functions are allowed. # self.visitchildren(node) return node
def visit_FuncDefNode(self, node): self.seen_vars_stack.append(set()) lenv = node.local_scope node.body.analyse_control_flow(lenv) # this will be totally refactored node.declare_arguments(lenv) for var, type_node in node.directive_locals.items(): if not lenv.lookup_here(var): # don't redeclare args type = type_node.analyse_as_type(lenv) if type: lenv.declare_var(var, type, type_node.pos) else: error(type_node.pos, "Not a type") node.body.analyse_declarations(lenv) self.env_stack.append(lenv) self.visitchildren(node) self.env_stack.pop() self.seen_vars_stack.pop() return node
def visit_SimpleCallNode(self, node): # locals builtin if isinstance(node.function, ExprNodes.NameNode): if node.function.name == 'locals': lenv = self.env_stack[-1] entry = lenv.lookup_here('locals') if entry: # not the builtin 'locals' return node if len(node.args) > 0: error( self.pos, "Builtin 'locals()' called with wrong number of args, expected 0, got %d" % len(node.args)) return node pos = node.pos items = [ ExprNodes.DictItemNode(pos, key=ExprNodes.StringNode(pos, value=var), value=ExprNodes.NameNode(pos, name=var)) for var in lenv.entries ] return ExprNodes.DictNode(pos, key_value_pairs=items) # cython.foo function = node.function.as_cython_attribute() if function: if function == u'cast': if len(node.args) != 2: error(node.function.pos, u"cast() takes exactly two arguments") else: type = node.args[0].analyse_as_type(self.env_stack[-1]) if type: node = TypecastNode(node.function.pos, type=type, operand=node.args[1]) else: error(node.args[0].pos, "Not a type") elif function == u'sizeof': if len(node.args) != 1: error(node.function.pos, u"sizeof() takes exactly one argument") else: type = node.args[0].analyse_as_type(self.env_stack[-1]) if type: node = SizeofTypeNode(node.function.pos, arg_type=type) else: node = SizeofVarNode(node.function.pos, operand=node.args[0]) elif function == 'typeof': if len(node.args) != 1: error(node.function.pos, u"typeof() takes exactly one argument") else: node = TypeofNode(node.function.pos, operand=node.args[0]) elif function == 'address': if len(node.args) != 1: error(node.function.pos, u"address() takes exactly one argument") else: node = AmpersandNode(node.function.pos, operand=node.args[0]) elif function == 'cmod': if len(node.args) != 2: error(node.function.pos, u"cmod() takes exactly two arguments") else: node = binop_node(node.function.pos, '%', node.args[0], node.args[1]) node.cdivision = True elif function == 'cdiv': if len(node.args) != 2: error(node.function.pos, u"cdiv() takes exactly two arguments") else: node = binop_node(node.function.pos, '/', node.args[0], node.args[1]) node.cdivision = True else: error(node.function.pos, u"'%s' not a valid cython language construct" % function) self.visitchildren(node) return node
def visit_SimpleCallNode(self, node): # locals builtin if isinstance(node.function, ExprNodes.NameNode): if node.function.name == 'locals': lenv = self.env_stack[-1] entry = lenv.lookup_here('locals') if entry: # not the builtin 'locals' return node if len(node.args) > 0: error(self.pos, "Builtin 'locals()' called with wrong number of args, expected 0, got %d" % len(node.args)) return node pos = node.pos items = [ExprNodes.DictItemNode(pos, key=ExprNodes.StringNode(pos, value=var), value=ExprNodes.NameNode(pos, name=var)) for var in lenv.entries] return ExprNodes.DictNode(pos, key_value_pairs=items) # cython.foo function = node.function.as_cython_attribute() if function: if function == u'cast': if len(node.args) != 2: error(node.function.pos, u"cast() takes exactly two arguments") else: type = node.args[0].analyse_as_type(self.env_stack[-1]) if type: node = TypecastNode(node.function.pos, type=type, operand=node.args[1]) else: error(node.args[0].pos, "Not a type") elif function == u'sizeof': if len(node.args) != 1: error(node.function.pos, u"sizeof() takes exactly one argument") else: type = node.args[0].analyse_as_type(self.env_stack[-1]) if type: node = SizeofTypeNode(node.function.pos, arg_type=type) else: node = SizeofVarNode(node.function.pos, operand=node.args[0]) elif function == 'typeof': if len(node.args) != 1: error(node.function.pos, u"typeof() takes exactly one argument") else: node = TypeofNode(node.function.pos, operand=node.args[0]) elif function == 'address': if len(node.args) != 1: error(node.function.pos, u"address() takes exactly one argument") else: node = AmpersandNode(node.function.pos, operand=node.args[0]) elif function == 'cmod': if len(node.args) != 2: error(node.function.pos, u"cmod() takes exactly two arguments") else: node = binop_node(node.function.pos, '%', node.args[0], node.args[1]) node.cdivision = True elif function == 'cdiv': if len(node.args) != 2: error(node.function.pos, u"cdiv() takes exactly two arguments") else: node = binop_node(node.function.pos, '/', node.args[0], node.args[1]) node.cdivision = True else: error(node.function.pos, u"'%s' not a valid cython language construct" % function) self.visitchildren(node) return node