Skip to content

Commit f5dc99e

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

File tree

5 files changed

+115
-0
lines changed

5 files changed

+115
-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: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
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+
from gccutils 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+
pprint (decl.type)
29+
30+
gcc.register_callback (gcc.PLUGIN_PASS_EXECUTION, gccPassHook)
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<RecordType
2+
repr() = <gcc.RecordType object at 0x2be4738>
3+
str() = 'struct test'
4+
superclasses = (<type 'gcc.Type'>, <type 'gcc.Tree'>)
5+
.attributes = {}
6+
.fields = [gcc.FieldDecl('a'), gcc.FieldDecl('b'), gcc.TypeDecl('test')]
7+
.methods = [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')]
8+
.name = <TypeDecl
9+
repr() = gcc.TypeDecl('test')
10+
str() = 'test'
11+
superclasses = (<type 'gcc.Declaration'>, <type 'gcc.Tree'>)
12+
.is_artificial = True
13+
.is_builtin = False
14+
.location = tests/examples/cplusplus/methods/input.cc:20
15+
.name = 'test'
16+
.pointer = <PointerType
17+
repr() = gcc.PointerType(dereference=<gcc.RecordType object at 0x2be4738>)
18+
str() = 'struct test *'
19+
superclasses = (<type 'gcc.Type'>, <type 'gcc.Tree'>)
20+
.attributes = {}
21+
.dereference = ... ('<gcc.RecordType object at 0x2be4738>')
22+
.name = None
23+
.sizeof = 8
24+
.str_no_uid = 'struct test *'
25+
.type = ... ('<gcc.RecordType object at 0x2be4738>')
26+
>
27+
.str_no_uid = 'test'
28+
.type = ... ('<gcc.RecordType object at 0x2be4738>')
29+
>
30+
.sizeof = 8
31+
.str_no_uid = 'struct test'
32+
.type = None
33+
>

0 commit comments

Comments
 (0)