gobject-introspection r171 - in trunk: . giscanner tools



Author: johan
Date: Fri Apr 18 19:49:43 2008
New Revision: 171
URL: http://svn.gnome.org/viewvc/gobject-introspection?rev=171&view=rev

Log:
2008-04-18  Johan Dahlin  <johan gnome org>

    * tools/g-ir-scanner (Parameter.__init__): Start constructing
    a real node tree.

    * giscanner/giscannermodule.c: wrap GISourceType.child_list and
    fix the style



Modified:
   trunk/ChangeLog
   trunk/giscanner/giscannermodule.c
   trunk/tools/g-ir-scanner

Modified: trunk/giscanner/giscannermodule.c
==============================================================================
--- trunk/giscanner/giscannermodule.c	(original)
+++ trunk/giscanner/giscannermodule.c	Fri Apr 18 19:49:43 2008
@@ -1,6 +1,6 @@
 /* GObject introspection: scanner
  *
- * Copyright (C) 2008
+ * Copyright (C) 2008  Johan Dahlin <johan gnome org>
  *
  * This library is free software; you can redistribute it and/or
  * modify it under the terms of the GNU Lesser General Public
@@ -67,38 +67,45 @@
   GISourceScanner *scanner;
 } PyGISourceScanner;
 
-NEW_CLASS(PyGISourceSymbol, "SourceSymbol", GISourceSymbol);
-NEW_CLASS(PyGISourceType, "SourceType", GISourceType);
-NEW_CLASS(PyGISourceScanner, "SourceScanner", GISourceScanner);
+NEW_CLASS (PyGISourceSymbol, "SourceSymbol", GISourceSymbol);
+NEW_CLASS (PyGISourceType, "SourceType", GISourceType);
+NEW_CLASS (PyGISourceScanner, "SourceScanner", GISourceScanner);
 
 
 /* Symbol */
 
 static PyObject *
-symbol_get_type(PyGISourceSymbol *self,
-		void             *context)
+symbol_get_type (PyGISourceSymbol *self,
+		 void             *context)
 {
-  return PyInt_FromLong(self->symbol->type);
+  return PyInt_FromLong (self->symbol->type);
 }
 
 static PyObject *
-symbol_get_ident(PyGISourceSymbol *self,
+symbol_get_ident (PyGISourceSymbol *self,
 		  void            *context)
 {
-  return PyString_FromString(self->symbol->ident);
+  return PyString_FromString (self->symbol->ident);
 }
 
 static PyObject *
-symbol_get_base_type(PyGISourceSymbol *self,
-		     void             *context)
+symbol_get_base_type (PyGISourceSymbol *self,
+		      void             *context)
 {
   PyGISourceType *item;
-  item = (PyGISourceType *)PyObject_GC_New(PyGISourceType,
-					   &PyGISourceType_Type);
+  item = (PyGISourceType *)PyObject_New (PyGISourceType,
+					 &PyGISourceType_Type);
   item->type = self->symbol->base_type;
   return (PyObject*)item;
 }
 
+static PyObject *
+symbol_get_const_int (PyGISourceSymbol *self,
+		      void             *context)
+{
+  return PyInt_FromLong (self->symbol->const_int);
+}
+
 static PyGetSetDef _PyGISourceSymbol_getsets[] = {
   /* int ref_count; */
   { "type", (getter)symbol_get_type, NULL, NULL},
@@ -106,7 +113,7 @@
   { "ident", (getter)symbol_get_ident, NULL, NULL},
   { "base_type", (getter)symbol_get_base_type, NULL, NULL},
   /* gboolean const_int_set; */
-  /* int const_int; */
+  { "const_int", (getter)symbol_get_const_int, NULL, NULL},
   /* char *const_string; */
   /* GSList *directives; */
   { 0 }
@@ -117,57 +124,84 @@
 /* Type */
 
 static PyObject *
-type_get_type(PyGISourceType *self,
-	      void           *context)
+type_get_type (PyGISourceType *self,
+	       void           *context)
 {
-  return PyInt_FromLong(self->type->type);
+  return PyInt_FromLong (self->type->type);
 }
 
 static PyObject *
-type_get_storage_class_specifier(PyGISourceType *self,
-				 void           *context)
+type_get_storage_class_specifier (PyGISourceType *self,
+				  void           *context)
 {
-  return PyInt_FromLong(self->type->storage_class_specifier);
+  return PyInt_FromLong (self->type->storage_class_specifier);
 }
 
 static PyObject *
-type_get_type_qualifier(PyGISourceType *self,
-			void           *context)
+type_get_type_qualifier (PyGISourceType *self,
+			 void           *context)
 {
-  return PyInt_FromLong(self->type->type_qualifier);
+  return PyInt_FromLong (self->type->type_qualifier);
 }
 
 static PyObject *
-type_get_function_specifier(PyGISourceType *self,
-			    void           *context)
+type_get_function_specifier (PyGISourceType *self,
+			     void           *context)
 {
-  return PyInt_FromLong(self->type->function_specifier);
+  return PyInt_FromLong (self->type->function_specifier);
 }
 
 static PyObject *
-type_get_name(PyGISourceType *self,
-	      void           *context)
+type_get_name (PyGISourceType *self,
+	       void           *context)
 {
   if (!self->type->name)
     {
-      Py_INCREF(Py_None);
+      Py_INCREF (Py_None);
       return Py_None;
     }
     
-  return PyString_FromString(self->type->name);
+  return PyString_FromString (self->type->name);
 }
 
 static PyObject *
-type_get_base_type(PyGISourceType *self,
-		   void             *context)
+type_get_base_type (PyGISourceType *self,
+		    void           *context)
 {
   PyGISourceType *item;
-  item = (PyGISourceType *)PyObject_GC_New(PyGISourceType,
-					   &PyGISourceType_Type);
+  item = (PyGISourceType *)PyObject_New (PyGISourceType,
+					 &PyGISourceType_Type);
   item->type = self->type->base_type;
   return (PyObject*)item;
 }
 
+static PyObject *
+type_get_child_list (PyGISourceType *self,
+		     void           *context)
+{
+  GList *l, *symbols;
+  PyObject *list;
+  int i = 0;
+
+  if (!self->type)
+    return Py_BuildValue("[]");
+  
+  list = PyList_New (g_list_length (self->type->child_list));
+  
+  for (l = self->type->child_list; l; l = l->next)
+    {
+      PyGISourceSymbol *item;
+      item = (PyGISourceSymbol *)PyObject_New (PyGISourceSymbol,
+					       &PyGISourceSymbol_Type);
+      item->symbol = l->data;
+      PyList_SetItem (list, i++, (PyObject*)item);
+      Py_INCREF (item);
+    }
+
+  Py_INCREF (list);
+  return list;
+}
+
 static PyGetSetDef _PyGISourceType_getsets[] = {
   { "type", (getter)type_get_type, NULL, NULL},
   { "storage_class_specifier", (getter)type_get_storage_class_specifier, NULL, NULL},
@@ -175,7 +209,7 @@
   { "function_specifier", (getter)type_get_function_specifier, NULL, NULL},
   { "name", (getter)type_get_name, NULL, NULL},
   { "base_type", (getter)type_get_base_type, NULL, NULL},
-  /* { "child_list", (getter)type_get_child_list, NULL, NULL}, */
+  { "child_list", (getter)type_get_child_list, NULL, NULL},
   { 0 }
 };
 
@@ -256,12 +290,14 @@
   for (l = symbols; l; l = l->next)
     {
       PyGISourceSymbol *item;
-      item = (PyGISourceSymbol *)PyObject_GC_New(PyGISourceSymbol,
-						 &PyGISourceSymbol_Type);
+      item = (PyGISourceSymbol *)PyObject_New (PyGISourceSymbol,
+					       &PyGISourceSymbol_Type);
       item->symbol = l->data;
-      PyList_SetItem(list, i++, (PyObject*)item);
+      PyList_SetItem (list, i++, (PyObject*)item);
+      Py_INCREF (item);
     }
-  
+
+  Py_INCREF (list);
   return list;
 }
 

Modified: trunk/tools/g-ir-scanner
==============================================================================
--- trunk/tools/g-ir-scanner	(original)
+++ trunk/tools/g-ir-scanner	Fri Apr 18 19:49:43 2008
@@ -1,4 +1,5 @@
 #!/usr/bin/env python
+import optparse
 import os
 import subprocess
 import sys
@@ -7,35 +8,62 @@
 
 import giscanner
 
-class Symbol(object):
-    def __init__(self, symbol):
-        self.symbol = symbol
-        self.nodes = []
+
+class Node(object):
+    pass
+
+
+class Function(Node):
+    def __init__(self, name, retval, parameters):
+        self.name = name
+        self.retval = retval
+        self.parameters = parameters
 
     def __repr__(self):
-        return '%s(%s, %s)>' % (self.__class__.__name__,
-                                self.symbol.ident,
-                                self.nodes)
+        return 'Function(%r, %r, %r)' % (self.name, self.retval, self.parameters)
 
-class Function(Symbol):
-    pass
 
-class Struct(Symbol):
-    pass
+class Parameter(Node):
+    def __init__(self, name, type):
+        self.name = name
+        self.type = type
+
+    def __repr__(self):
+        return 'Parameter(%r, %r)' % (self.name, self.type)
+
+
+class Enum(Node):
+    def __init__(self, name, members):
+        self.name = name
+        self.members = members
+
+    def __repr__(self):
+        return 'Enum(%r, %r)' % (self.name, self.members)
+
+
+class Member(Node):
+    def __init__(self, name, value):
+        self.name = name
+        self.value = value
+
+    def __repr__(self):
+        return 'Member(%r, %r)' % (self.name, self.value)
 
-class Typedef(Symbol):
-    pass
 
 class Generator(object):
     def __init__(self):
         self._scanner = giscanner.SourceScanner()
         self._filenames = []
-        self._cpp_options = None
+        self._cpp_options = []
 
     # Public API
 
-    def set_cpp_options(self, cpp_options):
-        self._cpp_options = cpp_options
+    def set_cpp_options(self, includes, defines, undefines):
+        for prefix, args in [('-I', includes),
+                             ('-D', defines),
+                             ('-U', undefines)]:
+            for arg in (args or []):
+                self._cpp_options.append(prefix + arg)
 
     def parse_file(self, filename):
         self._parse_one(filename)
@@ -98,47 +126,85 @@
 
         self._traverse()
 
+    def get_nodes(self):
+        for node in self.nodes:
+            yield node
+
     def _traverse(self):
         for symbol in self.generator.get_symbols():
             node = self._traverse_one(symbol)
             if node is not None:
                 self.nodes.append(node)
 
-    def _traverse_one(self, symbol):
-        if symbol.type == giscanner.CSYMBOL_TYPE_FUNCTION:
-            return Function(symbol)
-        elif symbol.type == giscanner.CSYMBOL_TYPE_TYPEDEF:
-            return Typedef(symbol)
-        elif symbol.type == giscanner.CSYMBOL_TYPE_STRUCT:
-            return Struct(symbol)
+    def _traverse_one(self, symbol, stype=None):
+        if stype is None:
+            stype = symbol.type
+        if stype == giscanner.CSYMBOL_TYPE_FUNCTION:
+            return self._create_function(symbol)
+        elif stype == giscanner.CSYMBOL_TYPE_TYPEDEF:
+            return self._traverse_one(symbol, symbol.base_type.type)
+        #elif stype == giscanner.CSYMBOL_TYPE_STRUCT:
+        #    return Struct(symbol)
+        elif stype == giscanner.CSYMBOL_TYPE_ENUM:
+            return self._create_enum(symbol)
         else:
             print 'unhandled', symbol.type
 
+    def _create_enum(self, symbol):
+        members = []
+        for child in symbol.base_type.child_list:
+            members.append(Member(child.ident,
+                                  child.const_int))
+
+        return Enum(symbol.ident, members)
+
+    def _create_function(self, symbol):
+        parameters = []
+        retval = None
+        for child in symbol.base_type.child_list:
+            print child.base_type.type
+            parameters.append(Parameter(child.ident, '??'))
+
+        return Function(symbol.ident, retval, parameters)
+
 def main(args):
-    if len(args) == 1:
+    parser = optparse.OptionParser('%prog [options] sources')
+    parser.add_option("-v", "--verbose",
+                      action="store_true", dest="verbose",
+                      help="be verbose")
+
+    group = optparse.OptionGroup(parser, "Preprocessor options")
+    group.add_option("-I", help="Pre-processor include file",
+                     action="append", dest="cpp_includes")
+    group.add_option("-D", help="Pre-processor define",
+                     action="append", dest="cpp_defines")
+    group.add_option("-U", help="Pre-processor undefine",
+                     action="append", dest="cpp_undefines")
+    group.add_option("-p", dest="", help="Ignored")
+    parser.add_option_group(group)
+
+    (options, args) = parser.parse_args()
+
+    if not args:
         print 'ERROR: Needs at least one filename.'
         return 0
 
-    filenames = []
-    cpp_options = []
-
     gen = Generator()
-    for arg in args[1:]:
+    gen.set_cpp_options(options.cpp_includes,
+                        options.cpp_defines,
+                        options.cpp_undefines)
+    filenames = []
+    for arg in args:
         if (arg.endswith('.c') or
             arg.endswith('.h')):
             filenames.append(arg)
-        elif arg.startswith('-') and len(arg) >= 2:
-            if arg[1] in ['I', 'U', 'D']:
-                cpp_options.append(arg)
-        else:
-            print 'Unhandled argument:', arg
-    gen.set_cpp_options(cpp_options)
 
     for filename in filenames:
         gen.parse_file(filename)
     gen.parse_macros()
 
     builder = TreeBuilder(gen)
-    print builder.nodes
+    import pprint
+    pprint.pprint(list(builder.get_nodes()))
 
 sys.exit(main(sys.argv))



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]