Skip to content

Commit 2838387

Browse files
committed
document EnumeralType; add 'values' attribute
1 parent a369bf5 commit 2838387

File tree

6 files changed

+80
-1
lines changed

6 files changed

+80
-1
lines changed

docs/tree.rst

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -542,6 +542,17 @@ Additional attributes for various :py:class:`gcc.Type` subclasses:
542542
The :py:class:`gcc.Type` that this type points to. In the above
543543
example (`int *`), this would be the `int` type.
544544

545+
.. py:class:: gcc.EnumeralType
546+
547+
Subclass of :py:class:`gcc.Type` representing an enumeral type.
548+
549+
.. py:attribute:: values
550+
551+
A list of tuple representing the constants defined in this
552+
enumeration. Each tuple consists of two elements; the first
553+
being the name of the constant, a :py:class:`gcc.IdentifierNode`;
554+
and the second being the value, a :py:class:`gcc.Constant`.
555+
545556
.. py:class:: gcc.ArrayType
546557
547558
Subclass of :py:class:`gcc.Type` representing an array type. For example,

gcc-python-tree.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1152,7 +1152,7 @@ PyGcc_TreeMakeListFromTreeList(tree t)
11521152
Extract a list of objects for the values
11531153
*/
11541154
PyObject *
1155-
gcc_tree_list_of_pairs_from_tree_list_chain(tree t)
1155+
PyGcc_TreeMakeListOfPairsFromTreeListChain(tree t)
11561156
{
11571157
PyObject *result = NULL;
11581158

generate-tree-c.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -485,6 +485,11 @@ def add_complex_getter(name, doc):
485485
'PyGcc_TreeListFromChain(TYPE_METHODS(self->t.inner))',
486486
"The methods of this type")
487487

488+
if tree_type.SYM == 'ENUMERAL_TYPE':
489+
add_simple_getter('values',
490+
'PyGcc_TreeMakeListOfPairsFromTreeListChain(TYPE_VALUES(self->t.inner))',
491+
"The values of this type")
492+
488493
if tree_type.SYM == 'IDENTIFIER_NODE':
489494
add_simple_getter('name',
490495
'PyGccStringOrNone(IDENTIFIER_POINTER(self->t.inner))',

tests/plugin/enum-type/input.c

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/*
2+
Copyright 2015 Tom Tromey <[email protected]>
3+
4+
This is free software: you can redistribute it and/or modify it
5+
under the terms of the GNU General Public License as published by
6+
the Free Software Foundation, either version 3 of the License, or
7+
(at your option) any later version.
8+
9+
This program is distributed in the hope that it will be useful, but
10+
WITHOUT ANY WARRANTY; without even the implied warranty of
11+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12+
General Public License for more details.
13+
14+
You should have received a copy of the GNU General Public License
15+
along with this program. If not, see
16+
<http://www.gnu.org/licenses/>.
17+
*/
18+
19+
enum the_enum
20+
{
21+
ONE = 1,
22+
TWO = 2,
23+
MINUS_ONE = -1
24+
};
25+
26+
/* We need a variable because some versions of gcc don't call
27+
PLUGIN_FINISH_TYPE for an enum. */
28+
enum the_enum variable;

tests/plugin/enum-type/script.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# -*- coding: utf-8 -*-
2+
# Copyright 2015 Tom Tromey <[email protected]>
3+
#
4+
# This is free software: you can redistribute it and/or modify it
5+
# under the terms of the GNU General Public License as published by
6+
# the Free Software Foundation, either version 3 of the License, or
7+
# (at your option) any later version.
8+
#
9+
# This program is distributed in the hope that it will be useful, but
10+
# WITHOUT ANY WARRANTY; without even the implied warranty of
11+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12+
# General Public License for more details.
13+
#
14+
# You should have received a copy of the GNU General Public License
15+
# along with this program. If not, see
16+
# <http://www.gnu.org/licenses/>.
17+
18+
import gcc
19+
20+
def on_decl(v, *args, **kwargs):
21+
if v.name != 'variable':
22+
return
23+
24+
t = v.type
25+
print(t.name)
26+
print('length = %d' % len(t.values))
27+
for (name, value) in t.values:
28+
print('%s = %s' % (name, value))
29+
30+
gcc.register_callback(gcc.PLUGIN_FINISH_DECL, on_decl)

tests/plugin/enum-type/stdout.txt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
the_enum
2+
length = 3
3+
ONE = 1
4+
TWO = 2
5+
MINUS_ONE = -1

0 commit comments

Comments
 (0)