Skip to content

Commit cdb09a7

Browse files
committed
Add get attribute TYPE_METHODS to record types
1 parent ba4fa6d commit cdb09a7

File tree

5 files changed

+94
-0
lines changed

5 files changed

+94
-0
lines changed

docs/tree.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -628,6 +628,10 @@ Additional attributes for various :py:class:`gcc.Type` subclasses:
628628
629629
The fields of this type, as a list of :py:class:`gcc.FieldDecl` instances
630630

631+
.. py:attribute:: methods
632+
633+
The methods of this type, as a list of :py:class:`gcc.MethodType` instances
634+
631635
You can look up C structures by looking within the top-level
632636
:py:class:`gcc.Block` within the current translation unit. For example,
633637
given this sample C code:

generate-tree-c.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -474,6 +474,9 @@ def add_complex_getter(name, doc):
474474
add_simple_getter('fields',
475475
'PyGcc_TreeListFromChain(TYPE_FIELDS(self->t.inner))',
476476
"The fields of this type")
477+
add_simple_getter('methods',
478+
'PyGcc_TreeListFromChain(TYPE_METHODS(self->t.inner))',
479+
"The methods of this type")
477480

478481
if tree_type.SYM == 'IDENTIFIER_NODE':
479482
add_simple_getter('name',
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/*
2+
Copyright 2014 Philip Herron <redbrain@gcc.gnu.org>
3+
Copyright 2011 Red Hat, Inc.
4+
5+
This is free software: you can redistribute it and/or modify it
6+
under the terms of the GNU General Public License as published by
7+
the Free Software Foundation, either version 3 of the License, or
8+
(at your option) any later version.
9+
10+
This program is distributed in the hope that it will be useful, but
11+
WITHOUT ANY WARRANTY; without even the implied warranty of
12+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13+
General Public License for more details.
14+
15+
You should have received a copy of the GNU General Public License
16+
along with this program. If not, see
17+
<http://www.gnu.org/licenses/>.
18+
*/
19+
20+
class test {
21+
int a, b;
22+
public:
23+
test (int, int);
24+
int somefunc (void);
25+
};
26+
27+
test::test (int x, int y)
28+
{
29+
a = x;
30+
b = y;
31+
}
32+
33+
int test::somefunc (void)
34+
{
35+
return a * b;
36+
}
37+
38+
39+
/*
40+
PEP-7
41+
Local variables:
42+
c-basic-offset: 4
43+
indent-tabs-mode: nil
44+
End:
45+
*/
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
# -*- coding: utf-8 -*-
2+
# Copyright 2014 Philip Herron <redbrain@gcc.gnu.org>
3+
# Copyright 2011 Red Hat, Inc.
4+
#
5+
# This is free software: you can redistribute it and/or modify it
6+
# under the terms of the GNU General Public License as published by
7+
# the Free Software Foundation, either version 3 of the License, or
8+
# (at your option) any later version.
9+
#
10+
# This program is distributed in the hope that it will be useful, but
11+
# WITHOUT ANY WARRANTY; without even the implied warranty of
12+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13+
# General Public License for more details.
14+
#
15+
# You should have received a copy of the GNU General Public License
16+
# along with this program. If not, see
17+
# <http://www.gnu.org/licenses/>.
18+
19+
import gcc
20+
import pprint
21+
22+
def gccPassHook(passname, _):
23+
if passname.name == '*free_lang_data':
24+
for i in gcc.get_translation_units ():
25+
gns = gcc.get_global_namespace ()
26+
for decl in gns.declarations:
27+
if decl.is_builtin is False:
28+
pp = pprint.PrettyPrinter(indent=4)
29+
pp.pprint (str (decl.type))
30+
pp.pprint (decl.type.fields)
31+
pp.pprint (decl.type.methods)
32+
33+
gcc.register_callback (gcc.PLUGIN_PASS_EXECUTION, gccPassHook)
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
'struct test'
2+
[gcc.FieldDecl('a'), gcc.FieldDecl('b'), gcc.TypeDecl('test')]
3+
[ gcc.FunctionDecl('test'),
4+
gcc.FunctionDecl('__base_ctor '),
5+
gcc.FunctionDecl('__comp_ctor '),
6+
gcc.FunctionDecl('test'),
7+
gcc.FunctionDecl('__base_ctor '),
8+
gcc.FunctionDecl('__comp_ctor '),
9+
gcc.FunctionDecl('somefunc')]

0 commit comments

Comments
 (0)