[pypy-svn] r46713 - pypy/dist/pypy/translator/c

arigo at codespeak.net arigo at codespeak.net
Tue Sep 18 10:55:05 CEST 2007


Author: arigo
Date: Tue Sep 18 10:54:55 2007
New Revision: 46713

Modified:
   pypy/dist/pypy/translator/c/genc.py
   pypy/dist/pypy/translator/c/node.py
Log:
Reduce (a bit) code duplication in genc.py.
Allow DefNodes to specify exactly which forward declaration they would like
to have, if any.


Modified: pypy/dist/pypy/translator/c/genc.py
==============================================================================
--- pypy/dist/pypy/translator/c/genc.py	(original)
+++ pypy/dist/pypy/translator/c/genc.py	Tue Sep 18 10:54:55 2007
@@ -444,31 +444,12 @@
         name = 'structdef.h'
         fi = self.makefile(name)
         print >> f, '#include "%s"' % name
-        print >> fi, '/***********************************************************/'
-        print >> fi, '/***  Structure definitions                              ***/'
-        print >> fi
-        for node in structdeflist:
-            if getattr(node, 'is_external', False):
-                continue
-            print >> fi, '%s %s;' % (node.typetag, node.name)
-        print >> fi
-        for node in structdeflist:
-            for line in node.definition():
-                print >> fi, line
-        print >> fi
-        print >> fi, '/***********************************************************/'
+        gen_structdef(fi, database)
         fi.close()
         name = 'forwarddecl.h'
         fi = self.makefile(name)
         print >> f, '#include "%s"' % name
-        print >> fi, '/***********************************************************/'
-        print >> fi, '/***  Forward declarations                               ***/'
-        print >> fi
-        for node in database.globalcontainers():
-            for line in node.forward_declaration():
-                print >> fi, line
-        print >> fi
-        print >> fi, '/***********************************************************/'
+        gen_forwarddecl(fi, database)
         fi.close()
 
         #
@@ -552,27 +533,23 @@
         print >> f
 
 
-# this function acts as the fallback for small sources for now.
-# Maybe we drop this completely if source splitting is the way
-# to go. Currently, I'm quite fine with keeping a working fallback.
-
-def gen_readable_parts_of_main_c_file(f, database, preimplementationlines=[]):
-    #
-    # All declarations
-    #
+def gen_structdef(f, database):
     structdeflist = database.getstructdeflist()
-    print >> f
     print >> f, '/***********************************************************/'
     print >> f, '/***  Structure definitions                              ***/'
     print >> f
     for node in structdeflist:
-        if node.name and not getattr(node, 'is_external', False):
+        if hasattr(node, 'forward_decl'):
+            if node.forward_decl:
+                print >> f, node.forward_decl
+        else:
             print >> f, '%s %s;' % (node.typetag, node.name)
     print >> f
     for node in structdeflist:
         for line in node.definition():
             print >> f, line
-    print >> f
+
+def gen_forwarddecl(f, database):
     print >> f, '/***********************************************************/'
     print >> f, '/***  Forward declarations                               ***/'
     print >> f
@@ -580,6 +557,20 @@
         for line in node.forward_declaration():
             print >> f, line
 
+# this function acts as the fallback for small sources for now.
+# Maybe we drop this completely if source splitting is the way
+# to go. Currently, I'm quite fine with keeping a working fallback.
+# XXX but we need to reduce code duplication.
+
+def gen_readable_parts_of_main_c_file(f, database, preimplementationlines=[]):
+    #
+    # All declarations
+    #
+    print >> f
+    gen_structdef(f, database)
+    print >> f
+    gen_forwarddecl(f, database)
+
     #
     # Implementation of functions and global structures and arrays
     #

Modified: pypy/dist/pypy/translator/c/node.py
==============================================================================
--- pypy/dist/pypy/translator/c/node.py	(original)
+++ pypy/dist/pypy/translator/c/node.py	Tue Sep 18 10:54:55 2007
@@ -36,7 +36,6 @@
 
 class StructDefNode:
     typetag = 'struct'
-    is_external = False
     def __init__(self, db, STRUCT, varlength=1):
         self.db = db
         self.STRUCT = STRUCT
@@ -56,7 +55,7 @@
             self.typetag = ''
             assert STRUCT._hints.get('external')
         if self.STRUCT._hints.get('external'):      # XXX hack
-            self.is_external = True
+            self.forward_decl = None
         if STRUCT._hints.get('c_name'):
             self.barename = self.name = STRUCT._hints['c_name']
             self.c_struct_field_name = self.verbatim_field_name



More information about the Pypy-commit mailing list