From de5401b5c5760c5384f24161373e80bc50fdd442 Mon Sep 17 00:00:00 2001 From: Johan Dahlin Date: Tue, 13 Sep 2011 11:57:28 -0300 Subject: 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 c99cf630..243b6afd 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 d2975afc..b228a561 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 8568fea3..5faaf197 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 f1e150df..bfe82a85 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 29d9229d..a921ad47 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 d3a056b3..cb785e73 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 eae5b7c9..4449604c 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. --> + + + + + + + + + This is a callback. diff --git a/tests/scanner/Foo-1.0-expected.gir b/tests/scanner/Foo-1.0-expected.gir index 371250f5..b8dc33f5 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. --> - + @@ -631,7 +633,7 @@ uses a C sugar return type. disguised="1" glib:is-gtype-struct-for="OtherObject"> - + @@ -680,7 +682,7 @@ it because it's not a boxed type. - + diff --git a/tests/scanner/Regress-1.0-expected.gir b/tests/scanner/Regress-1.0-expected.gir index 79ca1d31..7d106f66 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. --> Typedef'd va_list for additional reasons - + - + Like telepathy-glib's TpIntset. - + - + - +