Skip to content

Commit ab2dcf3

Browse files
committed
Handle gcc 8's changes to TYPE_FIELDS and TYPE_METHODS
1 parent c3d0f4b commit ab2dcf3

File tree

3 files changed

+92
-2
lines changed

3 files changed

+92
-2
lines changed

gcc-python-tree.c

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1013,6 +1013,48 @@ PyGccNamespaceDecl_namespaces(tree t)
10131013
return PyGcc_TreeListFromChain(NAMESPACE_LEVEL(t)->namespaces);
10141014
}
10151015

1016+
/* Accessing the fields and methods of compound types.
1017+
GCC 8's r250413 (aka ab87ee8f509c0b600102195704105d4d98ec59d9)
1018+
eliminated TYPE_METHODS, instead putting both fields and methods
1019+
on the TYPE_FIELDS chain, using DECL_DECLARES_FUNCTION_P to
1020+
distinguish them. */
1021+
1022+
#if (GCC_VERSION >= 8000)
1023+
1024+
static int is_field (tree t, void *)
1025+
{
1026+
return !DECL_DECLARES_FUNCTION_P (t);
1027+
}
1028+
1029+
static int is_method (tree t, void *)
1030+
{
1031+
return DECL_DECLARES_FUNCTION_P (t);
1032+
}
1033+
1034+
#endif /* #if (GCC_VERSION >= 8000) */
1035+
1036+
PyObject *
1037+
PyGcc_GetFields(struct PyGccTree *self)
1038+
{
1039+
#if (GCC_VERSION >= 8000)
1040+
return PyGcc_TreeListFromChainWithFilter(TYPE_FIELDS(self->t.inner),
1041+
is_field, NULL);
1042+
#else
1043+
return PyGcc_TreeListFromChain(TYPE_FIELDS(self->t.inner));
1044+
#endif
1045+
}
1046+
1047+
PyObject *
1048+
PyGcc_GetMethods(struct PyGccTree *self)
1049+
{
1050+
#if (GCC_VERSION >= 8000)
1051+
return PyGcc_TreeListFromChainWithFilter(TYPE_FIELDS(self->t.inner),
1052+
is_method, NULL);
1053+
#else
1054+
return PyGcc_TreeListFromChain(TYPE_METHODS(self->t.inner));
1055+
#endif
1056+
}
1057+
10161058
/*
10171059
GCC's debug_tree is implemented in:
10181060
gcc/print-tree.c
@@ -1121,6 +1163,44 @@ PyGcc_TreeListFromChain(tree t)
11211163
return NULL;
11221164
}
11231165

1166+
/* As above, but only add nodes for which "filter" returns true. */
1167+
PyObject *
1168+
PyGcc_TreeListFromChainWithFilter(tree t,
1169+
int (*filter) (tree, void *),
1170+
void *user_data)
1171+
{
1172+
PyObject *result = NULL;
1173+
1174+
result = PyList_New(0);
1175+
if (!result) {
1176+
goto error;
1177+
}
1178+
1179+
while (t) {
1180+
if (filter (t, user_data)) {
1181+
PyObject *item;
1182+
1183+
item = PyGccTree_New(gcc_private_make_tree(t));
1184+
if (!item) {
1185+
goto error;
1186+
}
1187+
if (-1 == PyList_Append(result, item)) {
1188+
Py_DECREF(item);
1189+
goto error;
1190+
}
1191+
Py_DECREF(item);
1192+
}
1193+
1194+
t = TREE_CHAIN(t);
1195+
}
1196+
1197+
return result;
1198+
1199+
error:
1200+
Py_XDECREF(result);
1201+
return NULL;
1202+
}
1203+
11241204
/*
11251205
As above, but expect nodes of the form:
11261206
tree_list ---> value

gcc-python-wrappers.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -346,6 +346,11 @@ PyGccNamespaceDecl_declarations(tree t);
346346
PyObject *
347347
PyGccNamespaceDecl_namespaces(tree t);
348348

349+
PyObject *
350+
PyGcc_GetFields(struct PyGccTree *self);
351+
352+
PyObject *
353+
PyGcc_GetMethods(struct PyGccTree *self);
349354

350355
/* gcc-python-gimple.c: */
351356
extern gcc_gimple_asm
@@ -499,6 +504,11 @@ PyGccRtl_str(struct PyGccRtl * self);
499504
PyObject *
500505
PyGcc_TreeListFromChain(tree t);
501506

507+
PyObject *
508+
PyGcc_TreeListFromChainWithFilter(tree t,
509+
int (*filter) (tree, void *),
510+
void *user_data);
511+
502512
PyObject *
503513
PyGcc_TreeMakeListFromTreeList(tree t);
504514

generate-tree-c.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -479,10 +479,10 @@ def add_complex_getter(name, doc):
479479

480480
if tree_type.SYM in ('RECORD_TYPE', 'UNION_TYPE', 'QUAL_UNION_TYPE'):
481481
add_simple_getter('fields',
482-
'PyGcc_TreeListFromChain(TYPE_FIELDS(self->t.inner))',
482+
'PyGcc_GetFields(self)',
483483
"The fields of this type")
484484
add_simple_getter('methods',
485-
'PyGcc_TreeListFromChain(TYPE_METHODS(self->t.inner))',
485+
'PyGcc_GetMethods(self)',
486486
"The methods of this type")
487487

488488
if tree_type.SYM == 'ENUMERAL_TYPE':

0 commit comments

Comments
 (0)