commit 2855f528ecc3b21b5adec0eb6fae18222935200b
Author: David Malcolm <dmalcolm(a)redhat.com>
Date: Fri Jul 29 15:44:11 2011 -0400
Implement __repr__ for gcc.IntegerCst and gcc.StringCst
gcc-python-tree.c | 46 ++++++++++++++++++++++++++++++++++++++++++++++
gcc-python-wrappers.h | 6 ++++++
gcc-python.c | 40 ++++++++++++++++++++++++----------------
gcc-python.h | 4 ++++
generate-tree-c.py | 6 ++++++
5 files changed, 86 insertions(+), 16 deletions(-)
---
diff --git a/gcc-python-tree.c b/gcc-python-tree.c
index d8ce3e0..8c226d8 100644
--- a/gcc-python-tree.c
+++ b/gcc-python-tree.c
@@ -263,6 +263,52 @@ gcc_IntegerConstant_get_constant(struct PyGccTree * self, void *closure)
}
PyObject *
+gcc_IntegerConstant_repr(struct PyGccTree * self)
+{
+ tree type = TREE_TYPE(self->t);
+ char buf[512];
+
+ gcc_python_double_int_as_text(TREE_INT_CST(self->t),
+ TYPE_UNSIGNED(type),
+ buf, sizeof(buf));
+ return gcc_python_string_from_format("%s(%s)",
+ Py_TYPE(self)->tp_name,
+ buf);
+}
+
+PyObject *
+gcc_StringConstant_repr(struct PyGccTree * self)
+{
+ PyObject *str_obj;
+ PyObject *result = NULL;
+
+ str_obj = gcc_python_string_or_none(TREE_STRING_POINTER(self->t));
+ if (!str_obj) {
+ return NULL;
+ }
+#if PY_MAJOR_VERSION >= 3
+ result = gcc_python_string_from_format("%s(%R)",
+ Py_TYPE(self)->tp_name,
+ str_obj);
+#else
+ {
+ PyObject *str_repr;
+ str_repr = PyObject_Repr(str_obj);
+ if (!str_repr) {
+ Py_DECREF(str_obj);
+ return NULL;
+ }
+ result = gcc_python_string_from_format("%s(%s)",
+ Py_TYPE(self)->tp_name,
+ PyString_AsString(str_repr));
+ Py_DECREF(str_repr);
+ }
+#endif
+ Py_DECREF(str_obj);
+ return result;
+}
+
+PyObject *
gcc_TypeDecl_get_pointer(struct PyGccTree *self, void *closure)
{
tree decl_type = TREE_TYPE(self->t);
diff --git a/gcc-python-wrappers.h b/gcc-python-wrappers.h
index e33936f..05424b4 100644
--- a/gcc-python-wrappers.h
+++ b/gcc-python-wrappers.h
@@ -111,6 +111,12 @@ PyObject *
gcc_IntegerConstant_get_constant(struct PyGccTree * self, void *closure);
PyObject *
+gcc_IntegerConstant_repr(struct PyGccTree * self);
+
+PyObject *
+gcc_StringConstant_repr(struct PyGccTree * self);
+
+PyObject *
gcc_TypeDecl_get_pointer(struct PyGccTree *self, void *closure);
/* gcc-python-gimple.c: */
diff --git a/gcc-python.c b/gcc-python.c
index 27aedb6..0eefd53 100644
--- a/gcc-python.c
+++ b/gcc-python.c
@@ -1032,28 +1032,36 @@ gcc_python_string_or_none(const char *str_or_null)
}
}
-PyObject *
-gcc_python_int_from_double_int(double_int di, bool is_unsigned)
-{
- /*
- "double_int" is declared in gcc/double-int.h as a pair of HOST_WIDE_INT.
- These in turn are defined in gcc/hwint.h as a #define to one of "long",
- "long long", or "__int64".
+/*
+ "double_int" is declared in gcc/double-int.h as a pair of HOST_WIDE_INT.
+ These in turn are defined in gcc/hwint.h as a #define to one of "long",
+ "long long", or "__int64".
- It appears that they can be interpreted as either "unsigned" or "signed"
- */
+ It appears that they can be interpreted as either "unsigned" or "signed"
- /* How to convert this to a PyLong object?
- We "cheat", and take it through the decimal representation, then convert
- from decimal. This is probably slow, but is (I hope) at least correct.
- */
- char buf[512]; /* FIXME */
+ How to convert this to other types?
+ We "cheat", and take it through the decimal representation, then convert
+ from decimal. This is probably slow, but is (I hope) at least correct.
+*/
+void
+gcc_python_double_int_as_text(double_int di, bool is_unsigned,
+ char *out, int bufsize)
+{
FILE *f;
+ assert(out);
+ assert(bufsize > 256); /* FIXME */
- buf[0] = '\0';
- f = fmemopen(buf, sizeof(buf), "w");
+ out[0] = '\0';
+ f = fmemopen(out, bufsize, "w");
dump_double_int (f, di, is_unsigned);
fclose(f);
+}
+
+PyObject *
+gcc_python_int_from_double_int(double_int di, bool is_unsigned)
+{
+ char buf[512]; /* FIXME */
+ gcc_python_double_int_as_text(di, is_unsigned, buf, sizeof(buf));
return PyLong_FromString(buf, NULL, 10);
}
diff --git a/gcc-python.h b/gcc-python.h
index 9f15eb7..ce8265f 100644
--- a/gcc-python.h
+++ b/gcc-python.h
@@ -199,6 +199,10 @@ gcc_python_string_or_none(const char *str_or_null);
PyObject *
VEC_tree_as_PyList(VEC(tree,gc) *vec_nodes);
+void
+gcc_python_double_int_as_text(double_int di, bool is_unsigned,
+ char *out, int bufsize);
+
PyObject *
gcc_python_int_from_double_int(double_int di, bool is_unsigned);
diff --git a/generate-tree-c.py b/generate-tree-c.py
index dbc6d73..ebb35ac 100644
--- a/generate-tree-c.py
+++ b/generate-tree-c.py
@@ -293,6 +293,8 @@ def generate_tree_code_classes():
getsettable = PyGetSetDefTable('gcc_%s_getset_table' % cc, [])
tp_as_number = None
+ tp_repr = None
+ tp_str = None
def get_getter_identifier(name):
return 'gcc_%s_get_%s' % (cc, name)
@@ -320,6 +322,7 @@ def generate_tree_code_classes():
add_simple_getter('constant',
'gcc_python_string_from_string(TREE_STRING_POINTER(self->t))',
'The actual value of this constant, as a str')
+ tp_repr = '(reprfunc)gcc_StringConstant_repr'
if cc == 'IntegerCst':
getsettable.add_gsdef('constant',
@@ -330,6 +333,7 @@ def generate_tree_code_classes():
tp_as_number = number_methods.identifier
number_methods.nb_int = 'gcc_IntegerConstant_get_constant'
cu.add_defn(number_methods.c_defn())
+ tp_repr = '(reprfunc)gcc_IntegerConstant_repr'
# TYPE_QUALS for various foo_TYPE classes:
if tree_type.SYM in ('VOID_TYPE', 'INTEGER_TYPE', 'REAL_TYPE',
@@ -470,6 +474,8 @@ def generate_tree_code_classes():
tp_new = 'PyType_GenericNew',
tp_base = '&%s' % base_type,
tp_getset = getsettable.identifier,
+ tp_str = tp_str,
+ tp_repr = tp_repr,
)
if tp_as_number:
pytype.tp_as_number = '&%s' % tp_as_number