From: W. Trevor King Date: Sun, 6 Mar 2011 17:30:46 +0000 (-0500) Subject: Normalize WTK_declare_var -> declare_var. X-Git-Url: http://git.tremily.us/?a=commitdiff_plain;h=3e8b76a627fd2096e6b48d006b97945e73e538e4;p=cython.git Normalize WTK_declare_var -> declare_var. --- diff --git a/Cython/Compiler/AnalysedTreeTransforms.py b/Cython/Compiler/AnalysedTreeTransforms.py index de527e00..b87b0fa2 100644 --- a/Cython/Compiler/AnalysedTreeTransforms.py +++ b/Cython/Compiler/AnalysedTreeTransforms.py @@ -1,3 +1,4 @@ +from Binding import Binding from Cython.Compiler.Visitor import VisitorTransform, ScopeTrackingTransform, TreeVisitor from Nodes import StatListNode, SingleAssignmentNode, CFuncDefNode from ExprNodes import DictNode, DictItemNode, NameNode, UnicodeNode, NoneNode, \ @@ -38,10 +39,10 @@ class AutoTestDictTransform(ScopeTrackingTransform): self.tests = [] self.testspos = node.pos - test_dict_entry = node.scope.declare_var(EncodedString(u'__test__'), - py_object_type, - pos, - visibility='public') + binding = Binding( + name = EncodedString(u'__test__'), c_visibility = 'public') + test_dict_entry = node.scope.declare_var( + binding, type = py_object_type, pos = pos) create_test_dict_assignment = SingleAssignmentNode(pos, lhs=NameNode(pos, name=EncodedString(u'__test__'), entry=test_dict_entry), diff --git a/Cython/Compiler/Buffer.py b/Cython/Compiler/Buffer.py index 86eb69c7..46b3f195 100644 --- a/Cython/Compiler/Buffer.py +++ b/Cython/Compiler/Buffer.py @@ -1,3 +1,4 @@ +from Binding import Binding from Visitor import VisitorTransform, CythonTransform from ModuleNode import ModuleNode from Nodes import * @@ -70,15 +71,18 @@ class IntroduceBufferAuxiliaryVars(CythonTransform): # Declare auxiliary vars cname = scope.mangle(Naming.bufstruct_prefix, name) - bufinfo = scope.declare_var(name="$%s" % cname, cname=cname, - type=PyrexTypes.c_py_buffer_type, pos=node.pos) + binding = Binding(name="$%s" % cname, cname=cname) + bufinfo = scope.declare_var( + binding, type=PyrexTypes.c_py_buffer_type, pos=node.pos) if entry.is_arg: bufinfo.used = True # otherwise, NameNode will mark whether it is used def var(prefix, idx, initval): cname = scope.mangle(prefix, "%d_%s" % (idx, name)) - result = scope.declare_var("$%s" % cname, PyrexTypes.c_py_ssize_t_type, - node.pos, cname=cname, is_cdef=True) + binding = Binding(name="$%s" % cname, cname=cname) + result = scope.declare_var( + binding, type=PyrexTypes.c_py_ssize_t_type, is_cdef=True, + pos=node.pos) result.init = initval if entry.is_arg: diff --git a/Cython/Compiler/Builtin.py b/Cython/Compiler/Builtin.py index 94561920..fded9539 100644 --- a/Cython/Compiler/Builtin.py +++ b/Cython/Compiler/Builtin.py @@ -568,8 +568,9 @@ def init_builtin_structs(): for name, cname, attribute_types in builtin_structs_table: scope = StructOrUnionScope(name) for attribute_name, attribute_type in attribute_types: - scope.declare_var(attribute_name, attribute_type, None, - attribute_name, allow_pyobject=True) + binding = Binding(name = attribute_name, cname = attribute_name) + scope.declare_var( + binding, type = attribute_type, allow_pyobject=True) binding = Binding(name = name, cname = cname) builtin_scope.declare_struct_or_union( binding, kind = 'struct', scope = scope, typedef_flag = True) diff --git a/Cython/Compiler/ExprNodes.py b/Cython/Compiler/ExprNodes.py index 72ed2ea1..6e3de505 100755 --- a/Cython/Compiler/ExprNodes.py +++ b/Cython/Compiler/ExprNodes.py @@ -1394,7 +1394,8 @@ class NameNode(AtomicExprNode): type = unspecified_type else: type = py_object_type - self.entry = env.declare_var(self.name, type, self.pos) + binding = Binding(name = self.name) + self.entry = env.declare_var(binding, type = type, pos = self.pos) env.control_flow.set_state(self.pos, (self.name, 'initialized'), True) env.control_flow.set_state(self.pos, (self.name, 'source'), 'assignment') if self.entry.is_declared_generic: diff --git a/Cython/Compiler/Nodes.py b/Cython/Compiler/Nodes.py index 5fe4ad7f..1184030b 100644 --- a/Cython/Compiler/Nodes.py +++ b/Cython/Compiler/Nodes.py @@ -563,9 +563,14 @@ class CFuncDeclaratorNode(CDeclaratorNode): if self.optional_arg_count: scope = StructOrUnionScope() arg_count_member = '%sn' % Naming.pyrex_prefix - scope.declare_var(arg_count_member, PyrexTypes.c_int_type, self.pos) + binding = Binding(name = arg_count_member) + scope.declare_var( + binding, type = PyrexTypes.c_int_type, pos = self.pos) for arg in func_type_args[len(func_type_args)-self.optional_arg_count:]: - scope.declare_var(arg.name, arg.type, arg.pos, allow_pyobject = 1) + binding = Binding(name = arg.name) + scope.declare_var( + binding, type = arg.type, allow_pyobject = 1, + pos = arg.pos) struct_cname = env.mangle(Naming.opt_arg_prefix, self.base.name) binding = Binding(name = struct_cname, cname = struct_cname) self.op_args_struct = env.global_scope().declare_struct_or_union( @@ -969,7 +974,7 @@ class CVarDefNode(StatNode): if self.in_pxd and not self.extern: error(self.pos, "Only 'extern' C variable declaration allowed in .pxd file") - entry = dest_scope.WTK_declare_var( + entry = dest_scope.declare_var( binding, type = type, is_cdef = 1, pos = declarator.pos) entry.needs_property = need_property @@ -2188,6 +2193,8 @@ class DefNode(FuncDefNode): if entry and entry.type.is_cfunction and not self.is_wrapper: warning(self.pos, "Overriding cdef method with def method.", 5) binding = Binding(name = name) + if env.is_c_class_scope: + binding.extern = True entry = env.declare_pyfunction( binding, allow_redefine=not self.is_wrapper, pos = self.pos) self.entry = entry @@ -2230,7 +2237,9 @@ class DefNode(FuncDefNode): env.control_flow.set_state((), (arg.name, 'source'), 'arg') env.control_flow.set_state((), (arg.name, 'initialized'), True) if arg.needs_conversion: - arg.entry = env.declare_var(arg.name, arg.type, arg.pos) + binding = Binding(name = arg.name) + arg.entry = env.declare_var( + binding, type = arg.type, pos = arg.pos) if arg.type.is_pyobject: arg.entry.init = "0" arg.entry.init_to_none = 0 @@ -2251,7 +2260,8 @@ class DefNode(FuncDefNode): type = PyrexTypes.unspecified_type else: type = py_object_type - entry = env.declare_var(arg.name, type, arg.pos) + binding = Binding(name = arg.name) + entry = env.declare_var(binding, type = type, pos = arg.pos) entry.used = 1 entry.init = "0" entry.init_to_none = 0 @@ -3395,7 +3405,10 @@ class ExprStatNode(StatNode): if type is None: error(type_node.pos, "Unknown type") else: - env.declare_var(var.value, type, var.pos, is_cdef = True) + binding = Binding(name = var.value) + env.declare_var( + binding, type = type, is_cdef = True, + pos = var.pos) self.__class__ = PassStatNode def analyse_expressions(self, env): @@ -3482,7 +3495,10 @@ class SingleAssignmentNode(AssignmentNode): error(lhs.pos, "Invalid declaration") return for var, pos in vars: - env.declare_var(var, type, pos, is_cdef = True) + binding = Binding(name = var) + env.declare_var( + binding, type = type, is_cdef = True, + pos = pos) if len(args) == 2: # we have a value self.rhs = args[1] @@ -3521,7 +3537,8 @@ class SingleAssignmentNode(AssignmentNode): binding, kind = func_name, scope = scope, pos = self.rhs.pos) for member, type, pos in members: - scope.declare_var(member, type, pos) + binding = Binding(name = member) + scope.declare_var(binding, type = type, pos = pos) if self.declaration_only: return diff --git a/Cython/Compiler/ParseTreeTransforms.py b/Cython/Compiler/ParseTreeTransforms.py index 57ce935c..03e67709 100644 --- a/Cython/Compiler/ParseTreeTransforms.py +++ b/Cython/Compiler/ParseTreeTransforms.py @@ -1091,7 +1091,8 @@ if VALUE is not None: 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) + binding = Binding(name = var) + lenv.declare_var(binding, type = type, pos = type_node.pos) else: error(type_node.pos, "Not a type") node.body.analyse_declarations(lenv) @@ -1496,18 +1497,16 @@ class CreateClosureClasses(CythonTransform): if from_closure: assert cscope.is_closure_scope - class_scope.declare_var(pos=node.pos, - name=Naming.outer_scope_cname, - cname=Naming.outer_scope_cname, - type=cscope.scope_class.type, - is_cdef=True) + binding = Binding( + name=Naming.outer_scope_cname, cname=Naming.outer_scope_cname) + class_scope.declare_var( + binding, type=cscope.scope_class.type, is_cdef=True, + pos=node.pos) node.needs_outer_scope = True for name, entry in in_closure: - class_scope.declare_var(pos=entry.pos, - name=entry.name, - cname=entry.cname, - type=entry.type, - is_cdef=True) + binding = Binding(name=entry.name, cname=entry.cname) + class_scope.declare_var( + binding, type=entry.type, is_cdef=True, pos=entry.pos) node.needs_closure = True # Do it here because other classes are already checked target_module_scope.check_c_class(func_scope.scope_class) diff --git a/Cython/Compiler/PyrexTypes.py b/Cython/Compiler/PyrexTypes.py index 631e0f6b..d7443c7f 100755 --- a/Cython/Compiler/PyrexTypes.py +++ b/Cython/Compiler/PyrexTypes.py @@ -1190,8 +1190,10 @@ class CComplexType(CNumericType): extern=1) scope.parent_type = self scope.directives = {} - scope.declare_var("real", self.real_type, None, "real", is_cdef=True) - scope.declare_var("imag", self.real_type, None, "imag", is_cdef=True) + binding = Binding(name = 'real', cname = 'real') + scope.declare_var(binding, type = self.real_type, is_cdef=True) + binding = Binding(name = 'imag', cname = 'imag') + scope.declare_var(binding, type = self.real_type, is_cdef=True) binding = Binding( name = 'conjugate', cname = '__Pyx_c_conj%s' % self.funcsuffix) func_type = CFuncType( diff --git a/Cython/Compiler/Symtab.py b/Cython/Compiler/Symtab.py index 2cfe48bf..62b49aba 100644 --- a/Cython/Compiler/Symtab.py +++ b/Cython/Compiler/Symtab.py @@ -557,14 +557,7 @@ class Scope(object): self.sue_entries.append(entry) return entry - def declare_var(self, name, type, pos, - cname = None, visibility = 'private', is_cdef = 0): - binding = self._WTK_setup(name, cname, visibility) - return self.WTK_declare_var( - binding, type, is_cdef, pos = pos) - - def WTK_declare_var(self, binding, type, - is_cdef = 0, pos = None): + def declare_var(self, binding, type, is_cdef = 0, pos = None): # Add an entry for a variable. if not binding.cname: if binding.c_visibility != 'private': @@ -589,7 +582,7 @@ class Scope(object): if entry and not entry.type.is_cfunction: error(pos, "'%s' already declared" % binding.name) error(entry.pos, "Previous declaration is here") - entry = self.WTK_declare_var(binding, py_object_type, pos = pos) + entry = self.declare_var(binding, py_object_type, pos = pos) entry.signature = pyfunction_signature self.pyfunc_entries.append(entry) return entry @@ -610,10 +603,10 @@ class Scope(object): return self._declare_pyfunction( binding, entry = entry, pos = pos) else: # declare entry stub - self.WTK_declare_var( - binding, py_object_type, pos = pos) - entry = self.WTK_declare_var(Binding(name=None, cname=binding.name), - py_object_type, pos = pos) + self.declare_var(binding, py_object_type, pos = pos) + entry = self.declare_var( + Binding(name=None, cname=binding.name), + py_object_type, pos = pos) entry.name = EncodedString(binding.name) entry.qualified_name = self.qualify_name(binding.name) entry.signature = pyfunction_signature @@ -622,7 +615,7 @@ class Scope(object): def declare_lambda_function(self, binding, pos = None): # Add an entry for an anonymous Python function. - entry = self.WTK_declare_var(binding, py_object_type, pos = pos) + entry = self.declare_var(binding, py_object_type, pos = pos) entry.name = EncodedString(binding.cname) entry.func_cname = binding.cname entry.signature = pyfunction_signature @@ -739,7 +732,7 @@ class Scope(object): entry = self.lookup_here(name) if not entry: binding = self._WTK_setup(name, None, 'private') - entry = self.WTK_declare_var(binding, py_object_type) + entry = self.declare_var(binding, py_object_type) return entry def lookup_type(self, name): @@ -813,7 +806,7 @@ class BuiltinScope(Scope): for name, definition in self.builtin_entries.iteritems(): cname, type = definition binding = self._WTK_setup(name, cname, 'private') - self.WTK_declare_var(binding, type) + self.declare_var(binding, type) def lookup(self, name, language_level=None): # 'language_level' is passed by ModuleScope @@ -969,7 +962,8 @@ class ModuleScope(Scope): self.undeclared_cached_builtins = [] self.namespace_cname = self.module_cname for name in ['__builtins__', '__name__', '__file__', '__doc__']: - self.declare_var(EncodedString(name), py_object_type, None) + binding = Binding(name = EncodedString(name)) + self.declare_var(binding, type = py_object_type) def qualifying_scope(self): return self.parent_module @@ -988,8 +982,7 @@ class ModuleScope(Scope): and binding.name != 'xrange'): # 'xrange' is special cased in Code.py if self.has_import_star: - entry = self.WTK_declare_var( - binding, py_object_type, pos = pos) + entry = self.declare_var(binding, py_object_type, pos = pos) return entry elif self.outer_scope is not None: return self.outer_scope.declare_builtin(binding, pos = pos) @@ -1078,25 +1071,19 @@ class ModuleScope(Scope): warning(pos, "'%s' redeclared " % scope.name, 0) return None else: - entry = self.declare_var(as_name, py_object_type, pos) + binding = Binding(name=as_name) + entry = self.declare_var(binding, type =py_object_type, pos = pos) entry.as_module = scope self.add_imported_module(scope) return entry - def declare_var(self, name, type, pos, - cname = None, visibility = 'private', is_cdef = 0): - binding = self._WTK_setup(name, cname, visibility) - return self.WTK_declare_var( - binding, type, is_cdef, pos = pos) - - def WTK_declare_var(self, binding, type, - is_cdef = 0, pos = None): + def declare_var(self, binding, type, is_cdef = 0, pos = None): # Add an entry for a global variable. If it is a Python # object type, and not declared with cdef, it will live # in the module dictionary, otherwise it will be a C # global variable. - entry = Scope.WTK_declare_var( - self, binding, type, is_cdef, pos = pos) + entry = Scope.declare_var( + self, binding, type, is_cdef = is_cdef, pos = pos) if binding.c_visibility not in ('private', 'public'): error(pos, "Module-level variable cannot be declared %s" % binding.c_visibility) @@ -1118,7 +1105,7 @@ class ModuleScope(Scope): entry = self.lookup_here(name) if not entry: binding = self._WTK_setup(name, name, 'private') - self.WTK_declare_var(binding, py_object_type, pos = pos) + self.declare_var(binding, type = py_object_type, pos = pos) def use_utility_code(self, new_code): if new_code is not None: @@ -1395,20 +1382,11 @@ class LocalScope(Scope): self.control_flow.set_state((), (binding.name, 'source'), 'arg') return entry - def declare_var(self, name, type, pos, - cname = None, visibility = 'private', is_cdef = 0): - binding = self._WTK_setup(name, cname, visibility) - return self.WTK_declare_var( - binding, type, is_cdef, pos = pos) - - def WTK_declare_var(self, binding, type, - is_cdef = 0, pos = None): + def declare_var(self, binding, type, is_cdef = 0, pos = None): # Add an entry for a local variable. if binding.c_visibility != 'private': #visibility in ('public', 'readonly'): error(pos, "Local variable cannot be declared %s" % binding.c_visibility) - entry = Scope.WTK_declare_var( - self, binding, type, is_cdef, - pos = pos) + entry = Scope.declare_var(self, binding, type, is_cdef, pos = pos) if type.is_pyobject and not Options.init_local_none: entry.init = "0" entry.init_to_none = (type.is_pyobject or type.is_unspecified) and Options.init_local_none @@ -1477,14 +1455,7 @@ class GeneratorExpressionScope(Scope): def mangle(self, prefix, name): return '%s%s' % (self.genexp_prefix, self.parent_scope.mangle(self, prefix, name)) - def declare_var(self, name, type, pos, - cname = None, visibility = 'private', is_cdef = True): - binding = self._WTK_setup(name, cname, visibility) - return self.WTK_declare_var( - binding, type, is_cdef, pos = pos) - - def WTK_declare_var(self, binding, type, - is_cdef = 0, pos = None): + def declare_var(self, binding, type, is_cdef = 0, pos = None): if type is unspecified_type: # if the outer scope defines a type for this variable, inherit it outer_entry = self.outer_scope.lookup(binding.name) @@ -1529,14 +1500,8 @@ class StructOrUnionScope(Scope): def __init__(self, name="?"): Scope.__init__(self, name, None, None) - def declare_var(self, name, type, pos, - cname = None, visibility = 'private', is_cdef = 0, allow_pyobject = 0): - binding = self._WTK_setup(name, cname, visibility) - return self.WTK_declare_var( - binding, type, is_cdef, allow_pyobject, pos = pos) - - def WTK_declare_var(self, binding, type, - is_cdef = 0, allow_pyobject = 0, pos = None): + def declare_var(self, binding, type, is_cdef = 0, allow_pyobject = 0, + pos = None): # Add an entry for an attribute. if not binding.cname: binding.cname = binding.name @@ -1554,7 +1519,7 @@ class StructOrUnionScope(Scope): def declare_cfunction(self, binding, type, defining = 0, in_pxd = 0, modifiers = (), utility_code = None, pos = None): - return self.WTK_declare_var(binding, type, pos=pos) + return self.declare_var(binding, type = type, pos = pos) class ClassScope(Scope): @@ -1598,20 +1563,12 @@ class PyClassScope(ClassScope): is_py_class_scope = 1 - def declare_var(self, name, type, pos, - cname = None, visibility = 'private', is_cdef = 0): - binding = self._WTK_setup(name, cname, visibility) - return self.WTK_declare_var( - binding, type, is_cdef, pos = pos) - - def WTK_declare_var(self, binding, type, - is_cdef = 0, pos = None): + def declare_var(self, binding, type, is_cdef = 0, pos = None): if type is unspecified_type: type = py_object_type # Add an entry for a class attribute. - entry = Scope.WTK_declare_var( - self, binding, type, is_cdef, - pos = pos) + entry = Scope.declare_var( + self, binding, type = type, is_cdef = is_cdef, pos = pos) entry.is_pyglobal = 1 entry.is_pyclass_attr = 1 return entry @@ -1655,16 +1612,7 @@ class CClassScope(ClassScope): self.parent_type.base_type.scope is not None and self.parent_type.base_type.scope.needs_gc()) - def declare_var(self, name, type, pos, - cname = None, visibility = 'private', is_cdef = 0): - binding = self._WTK_setup(name, cname, visibility) - if visibility == 'private': - binding.visibility = 'private' - return self.WTK_declare_var( - binding, type, is_cdef, pos = pos) - - def WTK_declare_var(self, binding, type, - is_cdef = 0, pos = None): + def declare_var(self, binding, type, is_cdef = 0, pos = None): if is_cdef: # Add an entry for an attribute. if self.defined: @@ -1704,8 +1652,8 @@ class CClassScope(ClassScope): if type is unspecified_type: type = py_object_type # Add an entry for a class attribute. - entry = Scope.WTK_declare_var( - self, binding, type, is_cdef, pos = pos) + entry = Scope.declare_var( + self, binding, type, is_cdef = is_cdef, pos = pos) entry.is_member = 1 entry.is_pyglobal = 1 # xxx: is_pyglobal changes behaviour in so many places that # I keep it in for now. is_member should be enough @@ -1722,12 +1670,10 @@ class CClassScope(ClassScope): if binding.name == "__new__": error(pos, "__new__ method of extension type will change semantics " "in a future version of Pyrex and Cython. Use __cinit__ instead.") - name = binding.name - entry = self.declare_var(name, py_object_type, pos, visibility='extern') - special_sig = get_special_method_signature(name) - #entry = self.WTK_declare_var( - # binding, py_object_type, pos = pos) - #special_sig = get_special_method_signature(binding.name) + if not binding.extern: + error(pos, "C class pyfunctions may only be extern") + entry = self.declare_var(binding, type = py_object_type, pos = pos) + special_sig = get_special_method_signature(binding.name) if special_sig: # Special methods get put in the method table with a particular # signature declared in advance. @@ -1863,14 +1809,10 @@ class CppClassScope(Scope): self.directives = outer_scope.directives self.inherited_var_entries = [] - def declare_var(self, name, type, pos, - cname = None, visibility = 'extern', is_cdef = 0, allow_pyobject = 0): - binding = self._WTK_setup(name, cname, visibility) - return self.WTK_declare_var( - binding, type, is_cdef, allow_pyobject, pos = pos) - - def WTK_declare_var(self, binding, type, - is_cdef = 0, allow_pyobject = 0, pos = None): + def declare_var(self, binding, type, is_cdef = 0, allow_pyobject = 0, + pos = None): + if not binding.extern: + error(pos, "c++ variables must be 'extern'") # Add an entry for an attribute. if not binding.cname: binding.cname = binding.name @@ -1920,7 +1862,7 @@ class CppClassScope(Scope): binding.name = '' type.return_type = self.lookup(self.name).type prev_entry = self.lookup_here(binding.name) - entry = self.WTK_declare_var(binding, type, pos = pos) + entry = self.declare_var(binding, type = type, pos = pos) if prev_entry: entry.overloaded_alternatives = prev_entry.all_alternatives() entry.utility_code = utility_code @@ -1966,9 +1908,9 @@ class CppClassScope(Scope): else: # binding = Binding() # binding.pull(entry) -# scope.WTK_declare_var( -# binding, -# type = entry.type.specialize(values), pos = entry.pos) +# scope.declare_var( +# binding, type = entry.type.specialize(values), +# pos = entry.pos) for e in entry.all_alternatives(): binding = Binding() binding.pull(e)