type.
@end defun
+@defun Type.function (@r{[}param_type@dots{}@r{]})
+Return a new @code{gdb.Type} object which represents a type of function
+returning this type. @code{param_type@dots{}} arguments specify parameter
+types. Use @code{None} as last parameter type to create a vararg function
+type. When invoked with single @code{None} argument or with no arguments at
+all it creates a vararg function taking zero or more parameters.
+@end defun
+
@defun Type.strip_typedefs ()
Return a new @code{gdb.Type} that represents the real type,
after removing all layers of typedefs.
return type_to_type_object (type);
}
+/* Return a function type. */
+static PyObject *
+typy_function (PyObject *self, PyObject *args)
+{
+ struct type *type = ((type_object *) self)->type;
+
+ gdb_assert (PySequence_Check (args));
+
+ std::vector<struct type *> param_types (PySequence_Length (args));
+
+ for (int i = 0; i < PySequence_Length (args); i++)
+ {
+ PyObject *param_type_obj = PySequence_GetItem (args, i);
+
+ if (param_type_obj == Py_None)
+ {
+ param_types[i] = nullptr;
+ if (i != (PySequence_Length (args) - 1))
+ {
+ PyErr_Format (PyExc_ValueError,
+ _("Argument at index %d is None but None can "
+ "only be the last type."), i);
+ return nullptr;
+ }
+ }
+ else
+ {
+ param_types[i] = type_object_to_type (param_type_obj);
+ if (!param_types[i])
+ {
+ PyErr_Format (PyExc_TypeError,
+ _("Argument at index %d is not a gdb.Type "
+ "object."), i);
+ return nullptr;
+ }
+ }
+ }
+
+ try
+ {
+ type = lookup_function_type_with_arguments (
+ type, param_types.size (), param_types.data ());
+ }
+ catch (const gdb_exception &except)
+ {
+ return gdbpy_handle_gdb_exception (nullptr, except);
+ }
+
+ return type_to_type_object (type);
+}
+
/* Return the size of the type represented by SELF, in bytes. */
static PyObject *
typy_get_sizeof (PyObject *self, void *closure)
{ "unqualified", typy_unqualified, METH_NOARGS,
"unqualified () -> Type\n\
Return a variant of this type without const or volatile attributes." },
+ { "function", typy_function, METH_VARARGS,
+ "function () -> Type\n\
+Return a function type returning value of this type." },
{ "values", typy_values, METH_NOARGS,
"values () -> list\n\
Return a list holding all the fields of this type.\n\
gdb_test "python print(gdb.lookup_type('int').optimized_out())" \
"<optimized out>"
+ gdb_test_no_output "python int_t = gdb.lookup_type('int')"
+
+ gdb_test "python print(repr(int_t.function()))" \
+ "<gdb.Type code=TYPE_CODE_FUNC name=int \\(\\)>"
+
+ gdb_test "python print(repr(int_t.function(int_t, int_t, int_t)))" \
+ "<gdb.Type code=TYPE_CODE_FUNC name=int \\(int, int, int\\)>"
+
+ gdb_test "python print(repr(int_t.function(int_t, None)))" \
+ "<gdb.Type code=TYPE_CODE_FUNC name=int \\(int, ...\\)>"
+
+ gdb_test "python print(repr(int_t.function(None)))" \
+ "<gdb.Type code=TYPE_CODE_FUNC name=int \\(\\)>"
+
+ gdb_test "python print(repr(int_t.function(123)))" \
+ "TypeError.*:.*"
+
+ gdb_test "python print(repr(int_t.function(int_t, None, int_t)))" \
+ "ValueError.*:.*"
+
set sint [get_sizeof int 0]
gdb_test "python print(gdb.parse_and_eval('aligncheck').type.alignof)" \
$sint