* gdbtypes.c (append_composite_type_field): New function.
authorAndrew Cagney <cagney@redhat.com>
Sat, 23 Mar 2002 01:24:54 +0000 (01:24 +0000)
committerAndrew Cagney <cagney@redhat.com>
Sat, 23 Mar 2002 01:24:54 +0000 (01:24 +0000)
(init_composite_type): New function.
* gdbtypes.h (append_composite_type_field): Declare.
(init_composite_type): Ditto.

gdb/ChangeLog
gdb/gdbtypes.c
gdb/gdbtypes.h

index 2f01146..3940517 100644 (file)
@@ -1,3 +1,10 @@
+2002-03-22  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.
+
 2002-03-22  Elena Zannoni  <ezannoni@redhat.com>
 
         * ppc-linux-tdep.c (ppc_sysv_abi_use_struct_convention): New
index a13847e..7b59d72 100644 (file)
@@ -1753,6 +1753,48 @@ init_type (enum type_code code, int length, int flags, char *name,
   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 74b521a..f4a2cf6 100644 (file)
@@ -1054,6 +1054,16 @@ extern struct type *alloc_type (struct objfile *);
 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 **);