[gobject-introspection] Add constant value annotation



commit de5401b5c5760c5384f24161373e80bc50fdd442
Author: Johan Dahlin <johan gnome org>
Date:   Tue Sep 13 11:57:28 2011 -0300

    Add constant value annotation
    
    Add an annotation tag "Value:" which can be used on
    constants to override the value.

 giscanner/annotationparser.py             |    4 +++-
 giscanner/ast.py                          |    3 ++-
 giscanner/girparser.py                    |    5 +++--
 giscanner/girwriter.py                    |    4 +++-
 giscanner/maintransformer.py              |   12 +++++++++++-
 giscanner/transformer.py                  |    3 ++-
 tests/scanner/Annotation-1.0-expected.gir |   15 +++++++++++++++
 tests/scanner/Foo-1.0-expected.gir        |    8 +++++---
 tests/scanner/Regress-1.0-expected.gir    |   18 +++++++++++++-----
 tests/scanner/annotation.h                |   21 +++++++++++++++++++++
 10 files changed, 78 insertions(+), 15 deletions(-)
---
diff --git a/giscanner/annotationparser.py b/giscanner/annotationparser.py
index c99cf63..243b6af 100644
--- a/giscanner/annotationparser.py
+++ b/giscanner/annotationparser.py
@@ -39,6 +39,7 @@ TAG_REF_FUNC = 'ref func'
 TAG_SET_VALUE_FUNC = 'set value func'
 TAG_GET_VALUE_FUNC = 'get value func'
 TAG_TRANSFER = 'transfer'
+TAG_VALUE = 'value'
 _ALL_TAGS = [TAG_VFUNC,
              TAG_SINCE,
              TAG_STABILITY,
@@ -51,7 +52,8 @@ _ALL_TAGS = [TAG_VFUNC,
              TAG_REF_FUNC,
              TAG_SET_VALUE_FUNC,
              TAG_GET_VALUE_FUNC,
-             TAG_TRANSFER]
+             TAG_TRANSFER,
+             TAG_VALUE]
 
 # Options - annotations for parameters and return values
 OPT_ALLOW_NONE = 'allow-none'
diff --git a/giscanner/ast.py b/giscanner/ast.py
index d2975af..b228a56 100644
--- a/giscanner/ast.py
+++ b/giscanner/ast.py
@@ -993,10 +993,11 @@ class Interface(Node, Registered):
 
 class Constant(Node):
 
-    def __init__(self, name, value_type, value):
+    def __init__(self, name, value_type, value, ctype):
         Node.__init__(self, name)
         self.value_type = value_type
         self.value = value
+        self.ctype = ctype
 
 
 class Property(Node):
diff --git a/giscanner/girparser.py b/giscanner/girparser.py
index 8568fea..5faaf19 100644
--- a/giscanner/girparser.py
+++ b/giscanner/girparser.py
@@ -543,8 +543,9 @@ class GIRParser(object):
     def _parse_constant(self, node):
         type_node = self._parse_type(node)
         constant = ast.Constant(node.attrib['name'],
-                            type_node,
-                            node.attrib['value'])
+                                type_node,
+                                node.attrib['value'],
+                                node.attrib.get(_cns('type')))
         self._parse_generic_attribs(node, constant)
         self._namespace.append(constant)
 
diff --git a/giscanner/girwriter.py b/giscanner/girwriter.py
index f1e150d..bfe82a8 100644
--- a/giscanner/girwriter.py
+++ b/giscanner/girwriter.py
@@ -362,7 +362,9 @@ and/or use gtk-doc annotations. ''')
         self.write_tag('member', attrs)
 
     def _write_constant(self, constant):
-        attrs = [('name', constant.name), ('value', constant.value)]
+        attrs = [('name', constant.name),
+                 ('value', constant.value),
+                 ('c:type', constant.ctype)]
         with self.tagcontext('constant', attrs):
             self._write_type(constant.value_type)
 
diff --git a/giscanner/maintransformer.py b/giscanner/maintransformer.py
index 29d9229..a921ad4 100644
--- a/giscanner/maintransformer.py
+++ b/giscanner/maintransformer.py
@@ -24,7 +24,7 @@ from . import message
 from .annotationparser import (TAG_VFUNC, TAG_SINCE, TAG_DEPRECATED, TAG_RETURNS,
                                TAG_ATTRIBUTES, TAG_RENAME_TO, TAG_TYPE,
                                TAG_UNREF_FUNC, TAG_REF_FUNC, TAG_SET_VALUE_FUNC,
-                               TAG_GET_VALUE_FUNC)
+                               TAG_GET_VALUE_FUNC, TAG_VALUE)
 from .annotationparser import (OPT_ALLOW_NONE, OPT_ARRAY, OPT_ATTRIBUTE,
                                OPT_ELEMENT_TYPE, OPT_IN, OPT_INOUT,
                                OPT_INOUT_ALT, OPT_OUT, OPT_SCOPE,
@@ -238,6 +238,8 @@ usage is void (*_gtk_reserved1)(void);"""
                 node.set_value_func = tag.value if tag else None
                 tag = block.get(TAG_GET_VALUE_FUNC)
                 node.get_value_func = tag.value if tag else None
+        if isinstance(node, ast.Constant):
+            self._apply_annotations_constant(node)
         return True
 
     def _adjust_container_type(self, parent, node, options):
@@ -788,6 +790,14 @@ usage is void (*_gtk_reserved1)(void);"""
             self._apply_annotations_param(signal, param, tag)
         self._apply_annotations_return(signal, signal.retval, block)
 
+    def _apply_annotations_constant(self, node):
+        block = self._blocks.get(node.ctype)
+        if not block:
+            return
+        tag = block.get(TAG_VALUE)
+        if tag:
+            node.value = tag.value
+
     def _pass_read_annotations2(self, node, chain):
         if isinstance(node, ast.Function):
             self._apply_annotations2_function(node, chain)
diff --git a/giscanner/transformer.py b/giscanner/transformer.py
index d3a056b..cb785e7 100644
--- a/giscanner/transformer.py
+++ b/giscanner/transformer.py
@@ -672,7 +672,8 @@ raise ValueError."""
         else:
             raise AssertionError()
 
-        const = ast.Constant(name, typeval, value)
+        const = ast.Constant(name, typeval, value,
+                             symbol.ident)
         const.add_symbol_reference(symbol)
         return const
 
diff --git a/tests/scanner/Annotation-1.0-expected.gir b/tests/scanner/Annotation-1.0-expected.gir
index eae5b7c..4449604 100644
--- a/tests/scanner/Annotation-1.0-expected.gir
+++ b/tests/scanner/Annotation-1.0-expected.gir
@@ -20,6 +20,21 @@ and/or use gtk-doc annotations.  -->
       <member name="foo" value="1" c:identifier="ANN_FLAG_FOO"/>
       <member name="bar" value="2" c:identifier="ANN_FLAG_BAR"/>
     </bitfield>
+    <constant name="CALCULATED_DEFINE"
+              value="100"
+              c:type="ANNOTATION_CALCULATED_DEFINE">
+      <type name="gint" c:type="gint"/>
+    </constant>
+    <constant name="CALCULATED_LARGE"
+              value="10000000000UL"
+              c:type="ANNOTATION_CALCULATED_LARGE">
+      <type name="gint" c:type="gint"/>
+    </constant>
+    <constant name="CALCULATED_LARGE_DIV"
+              value="1000000UL"
+              c:type="ANNOTATION_CALCULATED_LARGE_DIV">
+      <type name="gint" c:type="gint"/>
+    </constant>
     <callback name="Callback" c:type="AnnotationCallback">
       <doc xml:whitespace="preserve">This is a callback.</doc>
       <return-value transfer-ownership="none">
diff --git a/tests/scanner/Foo-1.0-expected.gir b/tests/scanner/Foo-1.0-expected.gir
index 371250f..b8dc33f 100644
--- a/tests/scanner/Foo-1.0-expected.gir
+++ b/tests/scanner/Foo-1.0-expected.gir
@@ -156,7 +156,9 @@ and/or use gtk-doc annotations.  -->
         </return-value>
       </method>
     </record>
-    <constant name="DEFINE_SHOULD_BE_EXPOSED" value="should be exposed">
+    <constant name="DEFINE_SHOULD_BE_EXPOSED"
+              value="should be exposed"
+              c:type="FOO_DEFINE_SHOULD_BE_EXPOSED">
       <type name="utf8" c:type="gchar*"/>
     </constant>
     <enumeration name="EnumFullname" c:type="FooEnumFullname">
@@ -631,7 +633,7 @@ uses a C sugar return type.</doc>
             disguised="1"
             glib:is-gtype-struct-for="OtherObject">
     </record>
-    <constant name="PIE_IS_TASTY" value="3.141590">
+    <constant name="PIE_IS_TASTY" value="3.141590" c:type="FOO_PIE_IS_TASTY">
       <type name="gdouble" c:type="gdouble"/>
     </constant>
     <record name="Rectangle" c:type="FooRectangle">
@@ -680,7 +682,7 @@ it because it's not a boxed type.</doc>
         </parameters>
       </function>
     </record>
-    <constant name="SUCCESS_INT" value="4408">
+    <constant name="SUCCESS_INT" value="4408" c:type="FOO_SUCCESS_INT">
       <type name="gint" c:type="gint"/>
     </constant>
     <enumeration name="Skippable" introspectable="0" c:type="FooSkippable">
diff --git a/tests/scanner/Regress-1.0-expected.gir b/tests/scanner/Regress-1.0-expected.gir
index 79ca1d3..7d106f6 100644
--- a/tests/scanner/Regress-1.0-expected.gir
+++ b/tests/scanner/Regress-1.0-expected.gir
@@ -32,19 +32,25 @@ and/or use gtk-doc annotations.  -->
       <doc xml:whitespace="preserve">Typedef'd va_list for additional reasons</doc>
       <type name="va_list" c:type="va_list"/>
     </alias>
-    <constant name="DOUBLE_CONSTANT" value="44.220000">
+    <constant name="DOUBLE_CONSTANT"
+              value="44.220000"
+              c:type="REGRESS_DOUBLE_CONSTANT">
       <type name="gdouble" c:type="gdouble"/>
     </constant>
-    <constant name="INT_CONSTANT" value="4422">
+    <constant name="INT_CONSTANT" value="4422" c:type="REGRESS_INT_CONSTANT">
       <type name="gint" c:type="gint"/>
     </constant>
     <record name="Intset" c:type="RegressIntset" disguised="1">
       <doc xml:whitespace="preserve">Like telepathy-glib's TpIntset.</doc>
     </record>
-    <constant name="Mixed_Case_Constant" value="4423">
+    <constant name="Mixed_Case_Constant"
+              value="4423"
+              c:type="REGRESS_Mixed_Case_Constant">
       <type name="gint" c:type="gint"/>
     </constant>
-    <constant name="STRING_CONSTANT" value="Some String">
+    <constant name="STRING_CONSTANT"
+              value="Some String"
+              c:type="REGRESS_STRING_CONSTANT">
       <type name="utf8" c:type="gchar*"/>
     </constant>
     <record name="SkippedStructure"
@@ -1307,7 +1313,9 @@ TpAccount::status-changed</doc>
         <type name="GObject.ObjectClass" c:type="GObjectClass"/>
       </field>
     </record>
-    <constant name="UTF8_CONSTANT" value="const â utf8">
+    <constant name="UTF8_CONSTANT"
+              value="const â utf8"
+              c:type="REGRESS_UTF8_CONSTANT">
       <type name="utf8" c:type="gchar*"/>
     </constant>
     <function name="aliased_caller_alloc"
diff --git a/tests/scanner/annotation.h b/tests/scanner/annotation.h
index c7de0f1..8bfbfd2 100644
--- a/tests/scanner/annotation.h
+++ b/tests/scanner/annotation.h
@@ -169,5 +169,26 @@ void annotation_space_after_comment_bug631690 (void);
 
 gchar* annotation_return_filename (void);
 
+/* This one we can handle properly */
+#define ANNOTATION_CALCULATED_DEFINE (10 * 10)
+
+/**
+ * ANNOTATION_CALCULATED_LARGE:
+ *
+ * Constant to define a calculated large value
+ *
+ * Value: 10000000000UL
+ */
+#define ANNOTATION_CALCULATED_LARGE (1000 * G_GINT64_CONSTANT (10000000))
+
+/**
+ * ANNOTATION_CALCULATED_LARGE_DIV:
+ *
+ * Constant to define a calculated large value
+ *
+ * Value: 1000000UL
+ */
+#define ANNOTATION_CALCULATED_LARGE_DIV (1000 / G_GINT64_CONSTANT (10000000))
+
 #endif /* __ANNOTATION_OBJECT_H__ */
 



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