* breakpoint.c, buildsym.c, c-exp.y, coffread.c, command.c,
[deliverable/binutils-gdb.git] / gdb / gdbtypes.c
index aaf7d2775034b40a3800b22eb73f613b4963a054..28e8aa4a156c9a4a755a5267efdabecca70bac51 100644 (file)
@@ -23,11 +23,13 @@ Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
 #include "bfd.h"
 #include "symtab.h"
 #include "symfile.h"
+#include "objfiles.h"
 #include "gdbtypes.h"
 #include "expression.h"
 #include "language.h"
 #include "target.h"
 #include "value.h"
+#include "demangle.h"
 
 /* Alloc a new type structure and fill it with some defaults.  If
    OBJFILE is non-NULL, then allocate the space for the type structure
@@ -50,7 +52,7 @@ alloc_type (objfile)
       type  = (struct type *) obstack_alloc (&objfile -> type_obstack,
                                             sizeof (struct type));
     }
-  (void) memset ((char *)type, 0, sizeof (struct type));
+  memset ((char *)type, 0, sizeof (struct type));
 
   /* Initialize the fields that might not be zero. */
 
@@ -61,6 +63,61 @@ alloc_type (objfile)
   return (type);
 }
 
+/* Lookup a pointer to a type TYPE.  TYPEPTR, if nonzero, points
+   to a pointer to memory where the pointer type should be stored.
+   If *TYPEPTR is zero, update it to point to the pointer type we return.
+   We allocate new memory if needed.  */
+
+struct type *
+make_pointer_type (type, typeptr)
+     struct type *type;
+     struct type **typeptr;
+{
+  register struct type *ntype;         /* New type */
+  struct objfile *objfile;
+
+  ntype = TYPE_POINTER_TYPE (type);
+
+  if (ntype) 
+    if (typeptr == 0)          
+      return ntype;    /* Don't care about alloc, and have new type.  */
+    else if (*typeptr == 0)
+      {
+       *typeptr = ntype;       /* Tracking alloc, and we have new type.  */
+       return ntype;
+      }
+
+  if (typeptr == 0 || *typeptr == 0)   /* We'll need to allocate one.  */
+    {
+      ntype = alloc_type (TYPE_OBJFILE (type));
+      if (typeptr)
+       *typeptr = ntype;
+    }
+  else                         /* We have storage, but need to reset it.  */
+    {
+      ntype = *typeptr;
+      objfile = TYPE_OBJFILE (ntype);
+      memset ((char *)ntype, 0, sizeof (struct type));
+      TYPE_OBJFILE (ntype) = objfile;
+    }
+
+  TYPE_TARGET_TYPE (ntype) = type;
+  TYPE_POINTER_TYPE (type) = ntype;
+
+  /* FIXME!  Assume the machine has only one representation for pointers!  */
+
+  TYPE_LENGTH (ntype) = TARGET_PTR_BIT / TARGET_CHAR_BIT;
+  TYPE_CODE (ntype) = TYPE_CODE_PTR;
+
+  /* pointers are unsigned */
+  TYPE_FLAGS (ntype) |= TYPE_FLAG_UNSIGNED;
+  
+  if (!TYPE_POINTER_TYPE (type))       /* Remember it, if don't have one.  */
+    TYPE_POINTER_TYPE (type) = ntype;
+
+  return ntype;
+}
+
 /* Given a type TYPE, return a type of pointers to that type.
    May need to construct such a type if this is the first use.  */
 
@@ -68,74 +125,130 @@ struct type *
 lookup_pointer_type (type)
      struct type *type;
 {
-  register struct type *ptype;
-
-  if ((ptype = TYPE_POINTER_TYPE (type)) == NULL)
-    {
-      /* This is the first time anyone wanted a pointer to a TYPE.  */
-      
-      ptype  = alloc_type (TYPE_OBJFILE (type));
-      TYPE_TARGET_TYPE (ptype) = type;
-      TYPE_POINTER_TYPE (type) = ptype;
-      
-      /* We assume the machine has only one representation for pointers!  */
-      
-      TYPE_LENGTH (ptype) = TARGET_PTR_BIT / TARGET_CHAR_BIT;
-      TYPE_CODE (ptype) = TYPE_CODE_PTR;
-      
-      /* pointers are unsigned */
-      TYPE_FLAGS(ptype) |= TYPE_FLAG_UNSIGNED;
-      
-    }
-  return (ptype);
+  return make_pointer_type (type, (struct type **)0);
 }
 
+/* Lookup a C++ `reference' to a type TYPE.  TYPEPTR, if nonzero, points
+   to a pointer to memory where the reference type should be stored.
+   If *TYPEPTR is zero, update it to point to the reference type we return.
+   We allocate new memory if needed.  */
+
+struct type *
+make_reference_type (type, typeptr)
+     struct type *type;
+     struct type **typeptr;
+{
+  register struct type *ntype;         /* New type */
+  struct objfile *objfile;
+
+  ntype = TYPE_REFERENCE_TYPE (type);
+
+  if (ntype) 
+    if (typeptr == 0)          
+      return ntype;    /* Don't care about alloc, and have new type.  */
+    else if (*typeptr == 0)
+      {
+       *typeptr = ntype;       /* Tracking alloc, and we have new type.  */
+       return ntype;
+      }
+
+  if (typeptr == 0 || *typeptr == 0)   /* We'll need to allocate one.  */
+    {
+      ntype = alloc_type (TYPE_OBJFILE (type));
+      if (typeptr)
+       *typeptr = ntype;
+    }
+  else                         /* We have storage, but need to reset it.  */
+    {
+      ntype = *typeptr;
+      objfile = TYPE_OBJFILE (ntype);
+      memset ((char *)ntype, 0, sizeof (struct type));
+      TYPE_OBJFILE (ntype) = objfile;
+    }
+
+  TYPE_TARGET_TYPE (ntype) = type;
+  TYPE_REFERENCE_TYPE (type) = ntype;
+
+  /* FIXME!  Assume the machine has only one representation for references,
+     and that it matches the (only) representation for pointers!  */
+
+  TYPE_LENGTH (ntype) = TARGET_PTR_BIT / TARGET_CHAR_BIT;
+  TYPE_CODE (ntype) = TYPE_CODE_REF;
+  
+  if (!TYPE_REFERENCE_TYPE (type))     /* Remember it, if don't have one.  */
+    TYPE_REFERENCE_TYPE (type) = ntype;
+
+  return ntype;
+}
+
+/* Same as above, but caller doesn't care about memory allocation details.  */
+
 struct type *
 lookup_reference_type (type)
      struct type *type;
 {
-  register struct type *rtype;
-
-  if ((rtype = TYPE_REFERENCE_TYPE (type)) == NULL)
-    {
-      /* This is the first time anyone wanted a pointer to a TYPE.  */
-      
-      rtype  = alloc_type (TYPE_OBJFILE (type));
-      TYPE_TARGET_TYPE (rtype) = type;
-      TYPE_REFERENCE_TYPE (type) = rtype;
-      
-      /* We assume the machine has only one representation for pointers!  */
-      /* FIXME:  This confuses host<->target data representations, and is a
-        poor assumption besides. */
-      
-      TYPE_LENGTH (rtype) = sizeof (char *);
-      TYPE_CODE (rtype) = TYPE_CODE_REF;
-      
-    }
-  return (rtype);
+  return make_reference_type (type, (struct type **)0);
 }
 
-/* Given a type TYPE, return a type of functions that return that type.
-   May need to construct such a type if this is the first use.  */
+/* Lookup a function type that returns type TYPE.  TYPEPTR, if nonzero, points
+   to a pointer to memory where the function type should be stored.
+   If *TYPEPTR is zero, update it to point to the function type we return.
+   We allocate new memory if needed.  */
 
 struct type *
-lookup_function_type (type)
+make_function_type (type, typeptr)
      struct type *type;
+     struct type **typeptr;
 {
-  register struct type *ptype;
+  register struct type *ntype;         /* New type */
+  struct objfile *objfile;
 
-  if ((ptype = TYPE_FUNCTION_TYPE (type)) == NULL)
+  ntype = TYPE_FUNCTION_TYPE (type);
+
+  if (ntype) 
+    if (typeptr == 0)          
+      return ntype;    /* Don't care about alloc, and have new type.  */
+    else if (*typeptr == 0)
+      {
+       *typeptr = ntype;       /* Tracking alloc, and we have new type.  */
+       return ntype;
+      }
+
+  if (typeptr == 0 || *typeptr == 0)   /* We'll need to allocate one.  */
     {
-      /* This is the first time anyone wanted a function returning a TYPE.  */
-      
-      ptype = alloc_type (TYPE_OBJFILE (type));
-      TYPE_TARGET_TYPE (ptype) = type;
-      TYPE_FUNCTION_TYPE (type) = ptype;
-      
-      TYPE_LENGTH (ptype) = 1;
-      TYPE_CODE (ptype) = TYPE_CODE_FUNC;
+      ntype = alloc_type (TYPE_OBJFILE (type));
+      if (typeptr)
+       *typeptr = ntype;
     }
-  return (ptype);
+  else                         /* We have storage, but need to reset it.  */
+    {
+      ntype = *typeptr;
+      objfile = TYPE_OBJFILE (ntype);
+      memset ((char *)ntype, 0, sizeof (struct type));
+      TYPE_OBJFILE (ntype) = objfile;
+    }
+
+  TYPE_TARGET_TYPE (ntype) = type;
+  TYPE_FUNCTION_TYPE (type) = ntype;
+
+  TYPE_LENGTH (ntype) = 1;
+  TYPE_CODE (ntype) = TYPE_CODE_FUNC;
+  
+  if (!TYPE_FUNCTION_TYPE (type))      /* Remember it, if don't have one.  */
+    TYPE_FUNCTION_TYPE (type) = ntype;
+
+  return ntype;
+}
+
+
+/* Given a type TYPE, return a type of functions that return that type.
+   May need to construct such a type if this is the first use.  */
+
+struct type *
+lookup_function_type (type)
+     struct type *type;
+{
+  return make_function_type (type, (struct type **)0);
 }
 
 /* Implement direct support for MEMBER_TYPE in GNU C++.
@@ -235,10 +348,9 @@ create_array_type (element_type, number)
    include the offset (that's the value of the MEMBER itself), but does
    include the structure type into which it points (for some reason).
 
-   FIXME:  When "smashing" the type, we preserve the objfile that the
+   When "smashing" the type, we preserve the objfile that the
    old type pointed to, since we aren't changing where the type is actually
-   allocated.  If the two types aren't associated with the same objfile,
-   then we are in deep-s**t anyway... */
+   allocated.  */
 
 void
 smash_to_member_type (type, domain, to_type)
@@ -250,7 +362,7 @@ smash_to_member_type (type, domain, to_type)
 
   objfile = TYPE_OBJFILE (type);
 
-  (void) memset ((char *)type, 0, sizeof (struct type));
+  memset ((char *)type, 0, sizeof (struct type));
   TYPE_OBJFILE (type) = objfile;
   TYPE_TARGET_TYPE (type) = to_type;
   TYPE_DOMAIN_TYPE (type) = domain;
@@ -261,10 +373,9 @@ smash_to_member_type (type, domain, to_type)
 /* Smash TYPE to be a type of method of DOMAIN with type TO_TYPE.
    METHOD just means `function that gets an extra "this" argument'.
 
-   FIXME:  When "smashing" the type, we preserve the objfile that the
+   When "smashing" the type, we preserve the objfile that the
    old type pointed to, since we aren't changing where the type is actually
-   allocated.  If the two types aren't associated with the same objfile,
-   then we are in deep-s**t anyway... */
+   allocated.  */
 
 void
 smash_to_method_type (type, domain, to_type, args)
@@ -277,7 +388,7 @@ smash_to_method_type (type, domain, to_type, args)
 
   objfile = TYPE_OBJFILE (type);
 
-  (void) memset ((char *)type, 0, sizeof (struct type));
+  memset ((char *)type, 0, sizeof (struct type));
   TYPE_OBJFILE (type) = objfile;
   TYPE_TARGET_TYPE (type) = to_type;
   TYPE_DOMAIN_TYPE (type) = domain;
@@ -318,6 +429,8 @@ type_name_no_tag (type)
                name += 5;
              }
            break;
+         default:              /* To avoid -Wall warnings */
+           break;
        }
     }
   return (name);
@@ -627,7 +740,7 @@ check_stub_method (type, i, j)
 {
   struct fn_field *f;
   char *mangled_name = gdb_mangle_name (type, i, j);
-  char *demangled_name = cplus_demangle (mangled_name, 0);
+  char *demangled_name = cplus_demangle (mangled_name, DMGL_PARAMS);
   char *argtypetext, *p;
   int depth = 0, argcount = 1;
   struct type **argtypes;
@@ -732,7 +845,12 @@ allocate_cplus_struct_type (type)
     }
 }
 
-/* Helper function to initialize the standard scalar types.  */
+/* Helper function to initialize the standard scalar types.
+
+   If NAME is non-NULL and OBJFILE is non-NULL, then we make a copy
+   of the string pointed to by name in the type_obstack for that objfile,
+   and initialize the type name to that copy.  There are places (mipsread.c
+   in particular, where init_type is called with a NULL value for NAME). */
 
 struct type *
 init_type (code, length, flags, name, objfile)
@@ -748,7 +866,15 @@ init_type (code, length, flags, name, objfile)
   TYPE_CODE (type) = code;
   TYPE_LENGTH (type) = length;
   TYPE_FLAGS (type) |= flags;
-  TYPE_NAME (type) = name;
+  if ((name != NULL) && (objfile != NULL))
+    {
+      TYPE_NAME (type) =
+       obsavestring (name, strlen (name), &objfile -> type_obstack);
+    }
+  else
+    {
+      TYPE_NAME (type) = name;
+    }
 
   /* C++ fancies.  */
 
@@ -767,7 +893,21 @@ init_type (code, length, flags, name, objfile)
    define fundamental types.
 
    For the formats which don't provide fundamental types, gdb can create
-   such types, using defaults reasonable for the current target machine. */
+   such types, using defaults reasonable for the current target machine.
+
+   FIXME:  Some compilers distinguish explicitly signed integral types
+   (signed short, signed int, signed long) from "regular" integral types
+   (short, int, long) in the debugging information.  There is some dis-
+   agreement as to how useful this feature is.  In particular, gcc does
+   not support this.  Also, only some debugging formats allow the
+   distinction to be passed on to a debugger.  For now, we always just
+   use "short", "int", or "long" as the type name, for both the implicit
+   and explicitly signed types.  This also makes life easier for the
+   gdb test suite since we don't have to account for the differences
+   in output depending upon what the compiler and debugging format
+   support.  We will probably have to re-examine the issue when gdb
+   starts taking it's fundamental type information directly from the
+   debugging information supplied by the compiler.  fnf@cygnus.com */
 
 struct type *
 lookup_fundamental_type (objfile, typeid)
@@ -790,7 +930,7 @@ lookup_fundamental_type (objfile, typeid)
          nbytes = FT_NUM_MEMBERS * sizeof (struct type *);
          objfile -> fundamental_types = (struct type **)
            obstack_alloc (&objfile -> type_obstack, nbytes);
-         (void) memset ((char *)objfile -> fundamental_types, 0, nbytes);
+         memset ((char *)objfile -> fundamental_types, 0, nbytes);
        }
       typep = objfile -> fundamental_types + typeid;
       if ((type = *typep) == NULL)
@@ -810,141 +950,151 @@ lookup_fundamental_type (objfile, typeid)
                type = init_type (TYPE_CODE_INT,
                                  TARGET_INT_BIT / TARGET_CHAR_BIT,
                                  TYPE_FLAG_UNSIGNED,
-                                 "boolean", (struct objfile *) NULL);
+                                 "boolean", objfile);
                break;
              case FT_STRING:
                type = init_type (TYPE_CODE_PASCAL_ARRAY,
                                  TARGET_CHAR_BIT / TARGET_CHAR_BIT,
                                  0,
-                                 "string", (struct objfile *) NULL);
+                                 "string", objfile);
                break;
              case FT_CHAR:
                type = init_type (TYPE_CODE_INT,
                                  TARGET_CHAR_BIT / TARGET_CHAR_BIT,
                                  0,
-                                 "char", (struct objfile *) NULL);
+                                 "char", objfile);
                break;
              case FT_SIGNED_CHAR:
                type = init_type (TYPE_CODE_INT,
                                  TARGET_CHAR_BIT / TARGET_CHAR_BIT,
                                  TYPE_FLAG_SIGNED,
-                                 "signed char", (struct objfile *) NULL);
+                                 "signed char", objfile);
                break;
              case FT_UNSIGNED_CHAR:
                type = init_type (TYPE_CODE_INT,
                                  TARGET_CHAR_BIT / TARGET_CHAR_BIT,
                                  TYPE_FLAG_UNSIGNED,
-                                 "unsigned char", (struct objfile *) NULL);
+                                 "unsigned char", objfile);
                break;
              case FT_SHORT:
                type = init_type (TYPE_CODE_INT,
                                  TARGET_SHORT_BIT / TARGET_CHAR_BIT,
                                  0,
-                                 "short", (struct objfile *) NULL);
+                                 "short", objfile);
                break;
              case FT_SIGNED_SHORT:
                type = init_type (TYPE_CODE_INT,
                                  TARGET_SHORT_BIT / TARGET_CHAR_BIT,
                                  TYPE_FLAG_SIGNED,
-                                 "signed short", (struct objfile *) NULL);
+                                 "short", objfile);    /* FIXME -fnf */
                break;
              case FT_UNSIGNED_SHORT:
                type = init_type (TYPE_CODE_INT,
                                  TARGET_SHORT_BIT / TARGET_CHAR_BIT,
                                  TYPE_FLAG_UNSIGNED,
-                                 "unsigned short", (struct objfile *) NULL);
+                                 "unsigned short", objfile);
                break;
              case FT_INTEGER:
                type = init_type (TYPE_CODE_INT,
                                  TARGET_INT_BIT / TARGET_CHAR_BIT,
                                  0,
-                                 "int", (struct objfile *) NULL);
+                                 "int", objfile);
                break;
              case FT_SIGNED_INTEGER:
                type = init_type (TYPE_CODE_INT,
                                  TARGET_INT_BIT / TARGET_CHAR_BIT,
                                  TYPE_FLAG_SIGNED,
-                                 "signed int", (struct objfile *) NULL);
+                                 "int", objfile);      /* FIXME -fnf */
                break;
              case FT_UNSIGNED_INTEGER:
                type = init_type (TYPE_CODE_INT,
                                  TARGET_INT_BIT / TARGET_CHAR_BIT,
                                  TYPE_FLAG_UNSIGNED,
-                                 "unsigned int", (struct objfile *) NULL);
+                                 "unsigned int", objfile);
+               break;
+             case FT_FIXED_DECIMAL:
+               type = init_type (TYPE_CODE_INT,
+                                 TARGET_INT_BIT / TARGET_CHAR_BIT,
+                                 0,
+                                 "fixed decimal", objfile);
                break;
              case FT_LONG:
                type = init_type (TYPE_CODE_INT,
                                  TARGET_LONG_BIT / TARGET_CHAR_BIT,
                                  0,
-                                 "long", (struct objfile *) NULL);
+                                 "long", objfile);
                break;
              case FT_SIGNED_LONG:
                type = init_type (TYPE_CODE_INT,
                                  TARGET_LONG_BIT / TARGET_CHAR_BIT,
                                  TYPE_FLAG_SIGNED,
-                                 "signed long", (struct objfile *) NULL);
+                                 "long", objfile);     /* FIXME -fnf */
                break;
              case FT_UNSIGNED_LONG:
                type = init_type (TYPE_CODE_INT,
                                  TARGET_LONG_BIT / TARGET_CHAR_BIT,
                                  TYPE_FLAG_UNSIGNED,
-                                 "unsigned long", (struct objfile *) NULL);
+                                 "unsigned long", objfile);
                break;
              case FT_LONG_LONG:
                type = init_type (TYPE_CODE_INT,
                                  TARGET_LONG_LONG_BIT / TARGET_CHAR_BIT,
                                  0,
-                                 "long long", (struct objfile *) NULL);
+                                 "long long", objfile);
                break;
              case FT_SIGNED_LONG_LONG:
                type = init_type (TYPE_CODE_INT,
                                  TARGET_LONG_LONG_BIT / TARGET_CHAR_BIT,
                                  TYPE_FLAG_SIGNED,
-                                 "signed long long", (struct objfile *) NULL);
+                                 "signed long long", objfile);
                break;
              case FT_UNSIGNED_LONG_LONG:
                type = init_type (TYPE_CODE_INT,
                                  TARGET_LONG_LONG_BIT / TARGET_CHAR_BIT,
                                  TYPE_FLAG_UNSIGNED,
-                                 "unsigned long long",
-                                 (struct objfile *) NULL);
+                                 "unsigned long long", objfile);
                break;
              case FT_FLOAT:
                type = init_type (TYPE_CODE_FLT,
                                  TARGET_FLOAT_BIT / TARGET_CHAR_BIT,
                                  0,
-                                 "float", (struct objfile *) NULL);
+                                 "float", objfile);
                break;
              case FT_DBL_PREC_FLOAT:
                type = init_type (TYPE_CODE_FLT,
                                  TARGET_DOUBLE_BIT / TARGET_CHAR_BIT,
                                  0,
-                                 "double", (struct objfile *) NULL);
+                                 "double", objfile);
+               break;
+             case FT_FLOAT_DECIMAL:
+               type = init_type (TYPE_CODE_FLT,
+                                 TARGET_DOUBLE_BIT / TARGET_CHAR_BIT,
+                                 0,
+                                 "floating decimal", objfile);
                break;
              case FT_EXT_PREC_FLOAT:
                type = init_type (TYPE_CODE_FLT,
                                  TARGET_LONG_DOUBLE_BIT / TARGET_CHAR_BIT,
                                  0,
-                                 "long double", (struct objfile *) NULL);
+                                 "long double", objfile);
                break;
              case FT_COMPLEX:
                type = init_type (TYPE_CODE_FLT,
                                  TARGET_COMPLEX_BIT / TARGET_CHAR_BIT,
                                  0,
-                                 "complex", (struct objfile *) NULL);
+                                 "complex", objfile);
                break;
              case FT_DBL_PREC_COMPLEX:
                type = init_type (TYPE_CODE_FLT,
                                  TARGET_DOUBLE_COMPLEX_BIT / TARGET_CHAR_BIT,
                                  0,
-                                 "double complex", (struct objfile *) NULL);
+                                 "double complex", objfile);
                break;
              case FT_EXT_PREC_COMPLEX:
                type = init_type (TYPE_CODE_FLT,
                                  TARGET_DOUBLE_COMPLEX_BIT / TARGET_CHAR_BIT,
                                  0,
-                                 "long double complex",
-                                 (struct objfile *) NULL);
+                                 "long double complex", objfile);
                break;
            }
          /* Install the newly created type in the objfile's fundamental_types
This page took 0.029812 seconds and 4 git commands to generate.