From a918d1466928ea712817df18c3bc66d9c4c5c53f Mon Sep 17 00:00:00 2001 From: "W. Trevor King" Date: Wed, 2 Mar 2011 20:43:36 -0500 Subject: [PATCH] Convert CEnumDefNode and CEnumDefItemNode to use explicit visibilities. --- Cython/Compiler/Nodes.py | 34 ++++++++++++++++++---------------- Cython/Compiler/Parsing.py | 28 ++++++++++++++-------------- 2 files changed, 32 insertions(+), 30 deletions(-) diff --git a/Cython/Compiler/Nodes.py b/Cython/Compiler/Nodes.py index c45f5260..cfced8c6 100644 --- a/Cython/Compiler/Nodes.py +++ b/Cython/Compiler/Nodes.py @@ -1079,20 +1079,23 @@ class CppClassNode(CStructOrUnionDefNode): attr.analyse_declarations(scope) class CEnumDefNode(StatNode): - # name string or None - # cname string or None + # name (same as Binding.name) + # cname (same as Binding.cname) # items [CEnumDefItemNode] # typedef_flag boolean - # visibility "public" or "private" + # extern (same meaning as Binding.extern) + # c_visibility (same as Binding.c_visibility) + # visibility (same as Binding.visibility) # in_pxd boolean # entry Entry child_attrs = ["items"] def analyse_declarations(self, env): - self.entry = env.declare_enum(self.name, self.pos, - cname = self.cname, typedef_flag = self.typedef_flag, - visibility = self.visibility) + binding = Binding() + binding.pull(self) + self.entry = env.WTK_declare_enum( + binding, typedef_flag = self.typedef_flag, pos = self.pos) if self.items is not None: if self.in_pxd and not env.in_cinclude: self.entry.defined_in_pxd = 1 @@ -1106,6 +1109,8 @@ class CEnumDefNode(StatNode): if self.visibility == 'public': temp = code.funcstate.allocate_temp(PyrexTypes.py_object_type, manage_ref=True) for item in self.entry.enum_values: + if item.visibility == 'private': + continue code.putln("%s = PyInt_FromLong(%s); %s" % ( temp, item.cname, @@ -1121,8 +1126,8 @@ class CEnumDefNode(StatNode): class CEnumDefItemNode(StatNode): - # name string - # cname string or None + # name (same as Binding.name) + # cname (same as Binding.cname) # value ExprNode or None child_attrs = ["value"] @@ -1133,14 +1138,11 @@ class CEnumDefItemNode(StatNode): if not self.value.type.is_int: self.value = self.value.coerce_to(PyrexTypes.c_int_type, env) self.value.analyse_const_expression(env) - visibility = 'private' - if enum_entry.extern: - visibility = 'extern' - elif enum_entry.c_visibility != 'private': - visibility = enum_entry.c_visibility - entry = env.declare_const(self.name, enum_entry.type, - self.value, self.pos, cname = self.cname, - visibility = visibility) + binding = Binding() + binding.pull(self) + entry = env.WTK_declare_const( + binding, type = enum_entry.type, + value = self.value, pos = self.pos) enum_entry.enum_values.append(entry) diff --git a/Cython/Compiler/Parsing.py b/Cython/Compiler/Parsing.py index 4af0a542..f6c68422 100644 --- a/Cython/Compiler/Parsing.py +++ b/Cython/Compiler/Parsing.py @@ -2549,22 +2549,26 @@ def p_c_enum_definition(s, pos, ctx): items = None s.expect(':') items = [] + # Work around overloading of the 'public' keyword. + item_ctx = ctx() + if item_ctx.c_visibility == 'public': + item_ctx.c_visibility = 'public' + item_ctx.visibility = 'public' + else: + item_ctx.c_visibility = 'public' + item_ctx.visibility = 'private' if s.sy != 'NEWLINE': - p_c_enum_line(s, ctx, items) + p_c_enum_line(s, item_ctx, items) else: s.next() # 'NEWLINE' s.expect_indent() while s.sy not in ('DEDENT', 'EOF'): - p_c_enum_line(s, ctx, items) + p_c_enum_line(s, item_ctx, items) s.expect_dedent() - visibility = 'private' - if ctx.extern: - visibility = 'extern' - elif ctx.c_visibility != 'private': - visibility = ctx.c_visibility return Nodes.CEnumDefNode( pos, name = name, cname = cname, items = items, - typedef_flag = ctx.typedef_flag, visibility = visibility, + typedef_flag = ctx.typedef_flag, extern = ctx.extern, + c_visibility = ctx.c_visibility, visibility = ctx.visibility, in_pxd = ctx.level == 'module_pxd') def p_c_enum_line(s, ctx, items): @@ -2594,14 +2598,10 @@ def p_c_enum_item(s, ctx, items): if s.sy == '=': s.next() value = p_test(s) - visibility = 'private' - if ctx.extern: - visibility = 'extern' - elif ctx.c_visibility != 'private': - visibility = ctx.c_visibility items.append(Nodes.CEnumDefItemNode(pos, name = name, cname = cname, value = value, - visibility = visibility, + extern = ctx.extern, visibility = ctx.visibility, + c_visibility = ctx.c_visibility, in_pxd = ctx.level == 'module_pxd')) -- 2.26.2