This is the mail archive of the
gdb-patches@sources.redhat.com
mailing list for the GDB project.
[patch/rfc] Add functions to construct struct/union builtin_types.
- From: Andrew Cagney <ac131313 at cygnus dot com>
- To: gdb-patches at sources dot redhat dot com
- Date: Sat, 16 Mar 2002 20:44:50 -0500
- Subject: [patch/rfc] Add functions to construct struct/union builtin_types.
Hello,
The attached adds two functions:
init_composite_type()
Creates an empty struc/union type.
append_composite_type_field()
Appends an extra field to a struct/union type
to gdbtypes.[hc]. I got anoyed at the (I think is) cumbersom way that
composite types are created in builtin types (Look for v4si et.al.).
I've two bits of code pending that will use it:
- $gdbframe
- code for the MMX registers
(the original was written by either JimB or FernandoN)
thoughts?
If no one says ``boo'' I'll check it in in a few days.
Andrew
2002-03-16 Andrew Cagney <ac131313@redhat.com>
* gdbtypes.c (append_composite_type_field): New function.
(init_composite_type): New function.
* gdbtypes.h (append_composite_type_field): Declare.
(init_composite_type): Ditto.
Index: gdbtypes.c
===================================================================
RCS file: /cvs/src/src/gdb/gdbtypes.c,v
retrieving revision 1.41
diff -u -r1.41 gdbtypes.c
--- gdbtypes.c 2002/02/08 17:34:33 1.41
+++ gdbtypes.c 2002/03/17 01:08:19
@@ -1753,6 +1753,48 @@
return (type);
}
+/* Helper function. Create an empty composite type. */
+
+struct type *
+init_composite_type (char *name, enum type_code code)
+{
+ struct type *t;
+ gdb_assert (code == TYPE_CODE_STRUCT
+ || code == TYPE_CODE_UNION);
+ t = init_type (code, 0, 0, NULL, NULL);
+ TYPE_TAG_NAME (t) = name;
+ return t;
+}
+
+/* Helper function. Append a field to a composite type. */
+
+void
+append_composite_type_field (struct type *t, char *name, struct type *field)
+{
+ struct field *f;
+ TYPE_NFIELDS (t) = TYPE_NFIELDS (t) + 1;
+ TYPE_FIELDS (t) = xrealloc (TYPE_FIELDS (t),
+ sizeof (struct field) * TYPE_NFIELDS (t));
+ f = &(TYPE_FIELDS (t)[TYPE_NFIELDS (t) - 1]);
+ memset (f, 0, sizeof f[0]);
+ FIELD_TYPE (f[0]) = field;
+ FIELD_NAME (f[0]) = name;
+ if (TYPE_CODE (t) == TYPE_CODE_UNION)
+ {
+ if (TYPE_LENGTH (t) > TYPE_LENGTH (field))
+ TYPE_LENGTH (t) = TYPE_LENGTH (field);
+ }
+ else if (TYPE_CODE (t) == TYPE_CODE_STRUCT)
+ {
+ TYPE_LENGTH (t) = TYPE_LENGTH (t) + TYPE_LENGTH (field);
+ if (TYPE_NFIELDS (t) > 1)
+ {
+ FIELD_BITPOS (f[0]) = (FIELD_BITPOS (f[-1])
+ + TYPE_LENGTH (field) * TARGET_CHAR_BIT);
+ }
+ }
+}
+
/* Look up a fundamental type for the specified objfile.
May need to construct such a type if this is the first use.
Index: gdbtypes.h
===================================================================
RCS file: /cvs/src/src/gdb/gdbtypes.h,v
retrieving revision 1.26
diff -u -r1.26 gdbtypes.h
--- gdbtypes.h 2002/02/03 22:57:56 1.26
+++ gdbtypes.h 2002/03/17 01:08:27
@@ -1054,6 +1054,16 @@
extern struct type *init_type (enum type_code, int, int, char *,
struct objfile *);
+/* Helper functions to construct a struct or record type. An
+ initially empty type is created using init_composite_type().
+ Fields are then added using append_struct_type_field(). A union
+ type has its size set to the largest field. A struct type has each
+ field packed against the previous. */
+
+extern struct type *init_composite_type (char *name, enum type_code code);
+extern void append_composite_type_field (struct type *t, char *name,
+ struct type *field);
+
extern struct type *lookup_reference_type (struct type *);
extern struct type *make_reference_type (struct type *, struct type **);