diff --git a/docs/tree.rst b/docs/tree.rst index 167a796d..1512e976 100644 --- a/docs/tree.rst +++ b/docs/tree.rst @@ -628,6 +628,10 @@ Additional attributes for various :py:class:`gcc.Type` subclasses: The fields of this type, as a list of :py:class:`gcc.FieldDecl` instances + .. py:attribute:: methods + + The methods of this type, as a list of :py:class:`gcc.MethodType` instances + You can look up C structures by looking within the top-level :py:class:`gcc.Block` within the current translation unit. For example, given this sample C code: diff --git a/generate-tree-c.py b/generate-tree-c.py index a822d1bd..1c6df3c5 100644 --- a/generate-tree-c.py +++ b/generate-tree-c.py @@ -474,6 +474,9 @@ def add_complex_getter(name, doc): add_simple_getter('fields', 'PyGcc_TreeListFromChain(TYPE_FIELDS(self->t.inner))', "The fields of this type") + add_simple_getter('methods', + 'PyGcc_TreeListFromChain(TYPE_METHODS(self->t.inner))', + "The methods of this type") if tree_type.SYM == 'IDENTIFIER_NODE': add_simple_getter('name', diff --git a/tests/examples/cplusplus/methods/input.cc b/tests/examples/cplusplus/methods/input.cc new file mode 100644 index 00000000..2203a03d --- /dev/null +++ b/tests/examples/cplusplus/methods/input.cc @@ -0,0 +1,45 @@ +/* + Copyright 2014 Philip Herron + Copyright 2011 Red Hat, Inc. + + This is free software: you can redistribute it and/or modify it + under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, but + WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see + . +*/ + +class test { + int a, b; +public: + test (int, int); + int somefunc (void); +}; + +test::test (int x, int y) +{ + a = x; + b = y; +} + +int test::somefunc (void) +{ + return a * b; +} + + +/* + PEP-7 +Local variables: +c-basic-offset: 4 +indent-tabs-mode: nil +End: +*/ diff --git a/tests/examples/cplusplus/methods/script.py b/tests/examples/cplusplus/methods/script.py new file mode 100644 index 00000000..24cffbc8 --- /dev/null +++ b/tests/examples/cplusplus/methods/script.py @@ -0,0 +1,33 @@ +# -*- coding: utf-8 -*- +# Copyright 2014 Philip Herron +# Copyright 2011 Red Hat, Inc. +# +# This is free software: you can redistribute it and/or modify it +# under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, but +# WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +# General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see +# . + +import gcc +import pprint + +def gccPassHook(passname, _): + if passname.name == '*free_lang_data': + for i in gcc.get_translation_units (): + gns = gcc.get_global_namespace () + for decl in gns.declarations: + if decl.is_builtin is False: + pp = pprint.PrettyPrinter(indent=4) + pp.pprint (str (decl.type)) + pp.pprint (decl.type.fields) + pp.pprint (decl.type.methods) + +gcc.register_callback (gcc.PLUGIN_PASS_EXECUTION, gccPassHook) diff --git a/tests/examples/cplusplus/methods/stdout.txt b/tests/examples/cplusplus/methods/stdout.txt new file mode 100644 index 00000000..ad43b195 --- /dev/null +++ b/tests/examples/cplusplus/methods/stdout.txt @@ -0,0 +1,9 @@ +'struct test' +[gcc.FieldDecl('a'), gcc.FieldDecl('b'), gcc.TypeDecl('test')] +[ gcc.FunctionDecl('test'), + gcc.FunctionDecl('__base_ctor '), + gcc.FunctionDecl('__comp_ctor '), + gcc.FunctionDecl('test'), + gcc.FunctionDecl('__base_ctor '), + gcc.FunctionDecl('__comp_ctor '), + gcc.FunctionDecl('somefunc')]