Skip to content

document EnumeralType; add 'values' attribute #36

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 23, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions docs/tree.rst
Original file line number Diff line number Diff line change
Expand Up @@ -542,6 +542,17 @@ Additional attributes for various :py:class:`gcc.Type` subclasses:
The :py:class:`gcc.Type` that this type points to. In the above
example (`int *`), this would be the `int` type.

.. py:class:: gcc.EnumeralType

Subclass of :py:class:`gcc.Type` representing an enumeral type.

.. py:attribute:: values

A list of tuple representing the constants defined in this
enumeration. Each tuple consists of two elements; the first
being the name of the constant, a :py:class:`gcc.IdentifierNode`;
and the second being the value, a :py:class:`gcc.Constant`.

.. py:class:: gcc.ArrayType

Subclass of :py:class:`gcc.Type` representing an array type. For example,
Expand Down
2 changes: 1 addition & 1 deletion gcc-python-tree.c
Original file line number Diff line number Diff line change
Expand Up @@ -1152,7 +1152,7 @@ PyGcc_TreeMakeListFromTreeList(tree t)
Extract a list of objects for the values
*/
PyObject *
gcc_tree_list_of_pairs_from_tree_list_chain(tree t)
PyGcc_TreeMakeListOfPairsFromTreeListChain(tree t)
{
PyObject *result = NULL;

Expand Down
5 changes: 5 additions & 0 deletions generate-tree-c.py
Original file line number Diff line number Diff line change
Expand Up @@ -485,6 +485,11 @@ def add_complex_getter(name, doc):
'PyGcc_TreeListFromChain(TYPE_METHODS(self->t.inner))',
"The methods of this type")

if tree_type.SYM == 'ENUMERAL_TYPE':
add_simple_getter('values',
'PyGcc_TreeMakeListOfPairsFromTreeListChain(TYPE_VALUES(self->t.inner))',
"The values of this type")

if tree_type.SYM == 'IDENTIFIER_NODE':
add_simple_getter('name',
'PyGccStringOrNone(IDENTIFIER_POINTER(self->t.inner))',
Expand Down
28 changes: 28 additions & 0 deletions tests/plugin/enum-type/input.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/*
Copyright 2015 Tom Tromey <[email protected]>

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
<http://www.gnu.org/licenses/>.
*/

enum the_enum
{
ONE = 1,
TWO = 2,
MINUS_ONE = -1
};

/* We need a variable because some versions of gcc don't call
PLUGIN_FINISH_TYPE for an enum. */
enum the_enum variable;
30 changes: 30 additions & 0 deletions tests/plugin/enum-type/script.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# -*- coding: utf-8 -*-
# Copyright 2015 Tom Tromey <[email protected]>
#
# 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
# <http://www.gnu.org/licenses/>.

import gcc

def on_decl(v, *args, **kwargs):
if v.name != 'variable':
return

t = v.type
print(t.name)
print('length = %d' % len(t.values))
for (name, value) in t.values:
print('%s = %s' % (name, value))

gcc.register_callback(gcc.PLUGIN_FINISH_DECL, on_decl)
5 changes: 5 additions & 0 deletions tests/plugin/enum-type/stdout.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
the_enum
length = 3
ONE = 1
TWO = 2
MINUS_ONE = -1