Convert CEnumDefNode and CEnumDefItemNode to use explicit visibilities.
authorW. Trevor King <wking@drexel.edu>
Thu, 3 Mar 2011 01:43:36 +0000 (20:43 -0500)
committerW. Trevor King <wking@drexel.edu>
Thu, 3 Mar 2011 01:43:36 +0000 (20:43 -0500)
Cython/Compiler/Nodes.py
Cython/Compiler/Parsing.py

index c45f52605179c8629ca23d8ab0ba9a1b9758aae5..cfced8c6d0fe2e57b2e69268721605a9303c7e5f 100644 (file)
@@ -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)
 
 
index 4af0a542fd14f5f2fc03dd78dd4a583b0ed89501..f6c684225c36136abf85e2d65e911cbea51fc26a 100644 (file)
@@ -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'))