Introduce compiled_regex, eliminate make_regfree_cleanup
[deliverable/binutils-gdb.git] / gdb / c-typeprint.c
index 161b3fe4b3a0cb619545f55f0ed954c5c7dc814e..9e197f5721fcebdfe3bb7c7a9fbda486b517ea76 100644 (file)
@@ -1,6 +1,5 @@
 /* Support for printing C and C++ types for GDB, the GNU debugger.
-   Copyright (C) 1986, 1988-1989, 1991-1996, 1998-2003, 2006-2012 Free
-   Software Foundation, Inc.
+   Copyright (C) 1986-2017 Free Software Foundation, Inc.
 
    This file is part of GDB.
 
@@ -31,9 +30,6 @@
 #include "c-lang.h"
 #include "typeprint.h"
 #include "cp-abi.h"
-#include "jv-lang.h"
-#include "gdb_string.h"
-#include <errno.h>
 #include "cp-support.h"
 
 static void c_type_print_varspec_prefix (struct type *,
@@ -53,7 +49,7 @@ static void c_type_print_modifier (struct type *,
 static const char *
 find_typedef_for_canonicalize (struct type *t, void *data)
 {
-  return find_typedef_in_hash (data, t);
+  return find_typedef_in_hash ((const struct type_print_options *) data, t);
 }
 
 /* Print NAME on STREAM.  If the 'raw' field of FLAGS is not set,
@@ -64,15 +60,14 @@ print_name_maybe_canonical (const char *name,
                            const struct type_print_options *flags,
                            struct ui_file *stream)
 {
-  char *s = NULL;
+  std::string s;
 
   if (!flags->raw)
     s = cp_canonicalize_string_full (name,
                                     find_typedef_for_canonicalize,
                                     (void *) flags);
 
-  fputs_filtered (s ? s : name, stream);
-  xfree (s);
+  fputs_filtered (!s.empty () ? s.c_str () : name, stream);
 }
 
 \f
@@ -92,7 +87,7 @@ c_print_type (struct type *type,
   const char *local_name;
 
   if (show > 0)
-    CHECK_TYPEDEF (type);
+    type = check_typedef (type);
 
   local_name = find_typedef_in_hash (flags, type);
   if (local_name != NULL)
@@ -115,7 +110,7 @@ c_print_type (struct type *type,
                      && !TYPE_VECTOR (type))
                  || code == TYPE_CODE_MEMBERPTR
                  || code == TYPE_CODE_METHODPTR
-                 || code == TYPE_CODE_REF)))
+                 || TYPE_IS_REFERENCE (type))))
        fputs_filtered (" ", stream);
       need_post_space = (varstring != NULL && strcmp (varstring, "") != 0);
       c_type_print_varspec_prefix (type, stream, show, 0, need_post_space,
@@ -147,7 +142,7 @@ c_print_typedef (struct type *type,
                 struct symbol *new_symbol,
                 struct ui_file *stream)
 {
-  CHECK_TYPEDEF (type);
+  type = check_typedef (type);
   fprintf_filtered (stream, "typedef ");
   type_print (type, "", stream, 0);
   if (TYPE_NAME ((SYMBOL_TYPE (new_symbol))) == 0
@@ -231,13 +226,21 @@ cp_type_print_method_args (struct type *mtype, const char *prefix,
                           language_cplus, DMGL_ANSI);
   fputs_filtered ("(", stream);
 
-  /* Skip the class variable.  */
+  /* Skip the class variable.  We keep this here to accommodate older
+     compilers and debug formats which may not support artificial
+     parameters.  */
   i = staticp ? 0 : 1;
   if (nargs > i)
     {
       while (i < nargs)
        {
-         c_print_type (args[i++].type, "", stream, 0, 0, flags);
+         struct field arg = args[i++];
+
+         /* Skip any artificial arguments.  */
+         if (FIELD_ARTIFICIAL (arg))
+           continue;
+
+         c_print_type (arg.type, "", stream, 0, 0, flags);
 
          if (i == nargs && varargs)
            fprintf_filtered (stream, ", ...");
@@ -270,6 +273,12 @@ cp_type_print_method_args (struct type *mtype, const char *prefix,
 
       if (TYPE_VOLATILE (domain))
        fprintf_filtered (stream, " volatile");
+
+      if (TYPE_RESTRICT (domain))
+       fprintf_filtered (stream, " restrict");
+
+      if (TYPE_ATOMIC (domain))
+       fprintf_filtered (stream, " _Atomic");
     }
 }
 
@@ -315,11 +324,11 @@ c_type_print_varspec_prefix (struct type *type,
     case TYPE_CODE_MEMBERPTR:
       c_type_print_varspec_prefix (TYPE_TARGET_TYPE (type),
                                   stream, show, 0, 0, flags);
-      name = type_name_no_tag (TYPE_DOMAIN_TYPE (type));
+      name = type_name_no_tag (TYPE_SELF_TYPE (type));
       if (name)
        print_name_maybe_canonical (name, flags, stream);
       else
-       c_type_print_base (TYPE_DOMAIN_TYPE (type),
+       c_type_print_base (TYPE_SELF_TYPE (type),
                           stream, -1, passed_a_ptr, flags);
       fprintf_filtered (stream, "::*");
       break;
@@ -328,19 +337,20 @@ c_type_print_varspec_prefix (struct type *type,
       c_type_print_varspec_prefix (TYPE_TARGET_TYPE (type),
                                   stream, show, 0, 0, flags);
       fprintf_filtered (stream, "(");
-      name = type_name_no_tag (TYPE_DOMAIN_TYPE (type));
+      name = type_name_no_tag (TYPE_SELF_TYPE (type));
       if (name)
        print_name_maybe_canonical (name, flags, stream);
       else
-       c_type_print_base (TYPE_DOMAIN_TYPE (type),
+       c_type_print_base (TYPE_SELF_TYPE (type),
                           stream, -1, passed_a_ptr, flags);
       fprintf_filtered (stream, "::*");
       break;
 
     case TYPE_CODE_REF:
+    case TYPE_CODE_RVALUE_REF:
       c_type_print_varspec_prefix (TYPE_TARGET_TYPE (type),
                                   stream, show, 1, 0, flags);
-      fprintf_filtered (stream, "&");
+      fprintf_filtered (stream, TYPE_CODE(type) == TYPE_CODE_REF ? "&" : "&&");
       c_type_print_modifier (type, stream, 1, need_post_space);
       break;
 
@@ -368,6 +378,7 @@ c_type_print_varspec_prefix (struct type *type,
     case TYPE_CODE_STRUCT:
     case TYPE_CODE_UNION:
     case TYPE_CODE_ENUM:
+    case TYPE_CODE_FLAGS:
     case TYPE_CODE_INT:
     case TYPE_CODE_FLT:
     case TYPE_CODE_VOID:
@@ -406,8 +417,7 @@ c_type_print_modifier (struct type *type, struct ui_file *stream,
   /* We don't print `const' qualifiers for references --- since all
      operators affect the thing referenced, not the reference itself,
      every reference is `const'.  */
-  if (TYPE_CONST (type)
-      && TYPE_CODE (type) != TYPE_CODE_REF)
+  if (TYPE_CONST (type) && !TYPE_IS_REFERENCE (type))
     {
       if (need_pre_space)
        fprintf_filtered (stream, " ");
@@ -423,6 +433,22 @@ c_type_print_modifier (struct type *type, struct ui_file *stream,
       did_print_modifier = 1;
     }
 
+  if (TYPE_RESTRICT (type))
+    {
+      if (did_print_modifier || need_pre_space)
+       fprintf_filtered (stream, " ");
+      fprintf_filtered (stream, "restrict");
+      did_print_modifier = 1;
+    }
+
+  if (TYPE_ATOMIC (type))
+    {
+      if (did_print_modifier || need_pre_space)
+       fprintf_filtered (stream, " ");
+      fprintf_filtered (stream, "_Atomic");
+      did_print_modifier = 1;
+    }
+
   address_space_id = address_space_int_to_name (get_type_arch (type),
                                                TYPE_INSTANCE_FLAGS (type));
   if (address_space_id)
@@ -445,20 +471,17 @@ c_type_print_modifier (struct type *type, struct ui_file *stream,
    parameter types get removed their possible const and volatile qualifiers to
    match demangled linkage name parameters part of such function type.
    LANGUAGE is the language in which TYPE was defined.  This is a necessary
-   evil since this code is used by the C, C++, and Java backends.  */
+   evil since this code is used by the C and C++.  */
 
 void
 c_type_print_args (struct type *type, struct ui_file *stream,
                   int linkage_name, enum language language,
                   const struct type_print_options *flags)
 {
-  int i, len;
-  struct field *args;
+  int i;
   int printed_any = 0;
 
   fprintf_filtered (stream, "(");
-  args = TYPE_FIELDS (type);
-  len = TYPE_NFIELDS (type);
 
   for (i = 0; i < TYPE_NFIELDS (type); i++)
     {
@@ -487,10 +510,7 @@ c_type_print_args (struct type *type, struct ui_file *stream,
          param_type = make_cv_type (0, 0, param_type, NULL);
        }
 
-      if (language == language_java)
-       java_print_type (param_type, "", stream, -1, 0, flags);
-      else
-       c_print_type (param_type, "", stream, -1, 0, flags);
+      c_print_type (param_type, "", stream, -1, 0, flags);
       printed_any = 1;
     }
 
@@ -507,8 +527,7 @@ c_type_print_args (struct type *type, struct ui_file *stream,
        }
     }
   else if (!printed_any
-          && ((TYPE_PROTOTYPED (type) && language != language_java)
-              || language == language_cplus))
+          && (TYPE_PROTOTYPED (type) || language == language_cplus))
     fprintf_filtered (stream, "void");
 
   fprintf_filtered (stream, ")");
@@ -528,7 +547,7 @@ is_type_conversion_operator (struct type *type, int i, int j)
      some other way, feel free to rewrite this function.  */
   const char *name = TYPE_FN_FIELDLIST_NAME (type, i);
 
-  if (strncmp (name, "operator", 8) != 0)
+  if (!startswith (name, "operator"))
     return 0;
 
   name += 8;
@@ -544,9 +563,9 @@ is_type_conversion_operator (struct type *type, int i, int j)
     /* If this doesn't look like the start of an identifier, then it
        isn't a type conversion operator.  */
     return 0;
-  else if (strncmp (name, "new", 3) == 0)
+  else if (startswith (name, "new"))
     name += 3;
-  else if (strncmp (name, "delete", 6) == 0)
+  else if (startswith (name, "delete"))
     name += 6;
   else
     /* If it doesn't look like new or delete, it's a type conversion
@@ -682,7 +701,11 @@ c_type_print_varspec_suffix (struct type *type,
 
        fprintf_filtered (stream, (is_vector ?
                                   " __attribute__ ((vector_size(" : "["));
-       if (get_array_bounds (type, &low_bound, &high_bound))
+       /* Bounds are not yet resolved, print a bounds placeholder instead.  */
+       if (TYPE_HIGH_BOUND_KIND (TYPE_INDEX_TYPE (type)) == PROP_LOCEXPR
+           || TYPE_HIGH_BOUND_KIND (TYPE_INDEX_TYPE (type)) == PROP_LOCLIST)
+         fprintf_filtered (stream, "variable length");
+       else if (get_array_bounds (type, &low_bound, &high_bound))
          fprintf_filtered (stream, "%s", 
                            plongest (high_bound - low_bound + 1));
        fprintf_filtered (stream, (is_vector ? ")))" : "]"));
@@ -705,6 +728,7 @@ c_type_print_varspec_suffix (struct type *type,
 
     case TYPE_CODE_PTR:
     case TYPE_CODE_REF:
+    case TYPE_CODE_RVALUE_REF:
       c_type_print_varspec_suffix (TYPE_TARGET_TYPE (type), stream,
                                   show, 1, 0, flags);
       break;
@@ -728,6 +752,7 @@ c_type_print_varspec_suffix (struct type *type,
     case TYPE_CODE_UNDEF:
     case TYPE_CODE_STRUCT:
     case TYPE_CODE_UNION:
+    case TYPE_CODE_FLAGS:
     case TYPE_CODE_ENUM:
     case TYPE_CODE_INT:
     case TYPE_CODE_FLT:
@@ -856,7 +881,7 @@ c_type_print_base (struct type *type, struct ui_file *stream,
       return;
     }
 
-  CHECK_TYPEDEF (type);
+  type = check_typedef (type);
 
   switch (TYPE_CODE (type))
     {
@@ -872,6 +897,7 @@ c_type_print_base (struct type *type, struct ui_file *stream,
     case TYPE_CODE_PTR:
     case TYPE_CODE_MEMBERPTR:
     case TYPE_CODE_REF:
+    case TYPE_CODE_RVALUE_REF:
     case TYPE_CODE_FUNC:
     case TYPE_CODE_METHOD:
     case TYPE_CODE_METHODPTR:
@@ -913,7 +939,7 @@ c_type_print_base (struct type *type, struct ui_file *stream,
           enum}" tag for unnamed struct/union/enum's, which we don't
           want to print.  */
        if (TYPE_TAG_NAME (type) != NULL
-           && strncmp (TYPE_TAG_NAME (type), "{unnamed", 8))
+           && !startswith (TYPE_TAG_NAME (type), "{unnamed"))
          {
            /* When printing the tag name, we are still effectively
               printing in the outer context, hence the use of FLAGS
@@ -1143,7 +1169,8 @@ c_type_print_base (struct type *type, struct ui_file *stream,
                  struct cleanup *inner_cleanup;
                  const char *physname = TYPE_FN_FIELD_PHYSNAME (f, j);
                  int is_full_physname_constructor =
-                   is_constructor_name (physname) 
+                   TYPE_FN_FIELD_CONSTRUCTOR (f, j)
+                   || is_constructor_name (physname)
                    || is_destructor_name (physname)
                    || method_name[0] == '~';
 
@@ -1219,8 +1246,8 @@ c_type_print_base (struct type *type, struct ui_file *stream,
                    mangled_name = TYPE_FN_FIELD_PHYSNAME (f, j);
 
                  demangled_name =
-                   cplus_demangle (mangled_name,
-                                   DMGL_ANSI | DMGL_PARAMS);
+                   gdb_demangle (mangled_name,
+                                 DMGL_ANSI | DMGL_PARAMS);
                  if (demangled_name == NULL)
                    {
                      /* In some cases (for instance with the HP
@@ -1284,28 +1311,27 @@ c_type_print_base (struct type *type, struct ui_file *stream,
              if (TYPE_NFIELDS (type) != 0 || TYPE_NFN_FIELDS (type) != 0)
                fprintf_filtered (stream, "\n");
 
-               for (i = 0; i < TYPE_TYPEDEF_FIELD_COUNT (type); i++)
-                 {
-                   struct type *target = TYPE_TYPEDEF_FIELD_TYPE (type, i);
-                   struct typedef_hash_table *table2;
-
-                   /* Dereference the typedef declaration itself.  */
-                   gdb_assert (TYPE_CODE (target) == TYPE_CODE_TYPEDEF);
-                   target = TYPE_TARGET_TYPE (target);
-
-                   print_spaces_filtered (level + 4, stream);
-                   fprintf_filtered (stream, "typedef ");
-
-                   /* We want to print typedefs with substitutions
-                      from the template parameters or globally-known
-                      typedefs but not local typedefs.  */
-                   c_print_type (target,
-                                 TYPE_TYPEDEF_FIELD_NAME (type, i),
-                                 stream, show - 1, level + 4,
-                                 &semi_local_flags);
-                   fprintf_filtered (stream, ";\n");
-                 }
-             }
+             for (i = 0; i < TYPE_TYPEDEF_FIELD_COUNT (type); i++)
+               {
+                 struct type *target = TYPE_TYPEDEF_FIELD_TYPE (type, i);
+
+                 /* Dereference the typedef declaration itself.  */
+                 gdb_assert (TYPE_CODE (target) == TYPE_CODE_TYPEDEF);
+                 target = TYPE_TARGET_TYPE (target);
+
+                 print_spaces_filtered (level + 4, stream);
+                 fprintf_filtered (stream, "typedef ");
+
+                 /* We want to print typedefs with substitutions
+                    from the template parameters or globally-known
+                    typedefs but not local typedefs.  */
+                 c_print_type (target,
+                               TYPE_TYPEDEF_FIELD_NAME (type, i),
+                               stream, show - 1, level + 4,
+                               &semi_local_flags);
+                 fprintf_filtered (stream, ";\n");
+               }
+           }
 
            fprintfi_filtered (level, stream, "}");
          }
@@ -1317,13 +1343,15 @@ c_type_print_base (struct type *type, struct ui_file *stream,
     case TYPE_CODE_ENUM:
       c_type_print_modifier (type, stream, 0, 1);
       fprintf_filtered (stream, "enum ");
+      if (TYPE_DECLARED_CLASS (type))
+       fprintf_filtered (stream, "class ");
       /* Print the tag name if it exists.
          The aCC compiler emits a spurious 
          "{unnamed struct}"/"{unnamed union}"/"{unnamed enum}"
          tag for unnamed struct/union/enum's, which we don't
          want to print.  */
       if (TYPE_TAG_NAME (type) != NULL
-         && strncmp (TYPE_TAG_NAME (type), "{unnamed", 8))
+         && !startswith (TYPE_TAG_NAME (type), "{unnamed"))
        {
          print_name_maybe_canonical (TYPE_TAG_NAME (type), flags, stream);
          if (show > 0)
@@ -1342,6 +1370,23 @@ c_type_print_base (struct type *type, struct ui_file *stream,
        {
          LONGEST lastval = 0;
 
+         /* We can't handle this case perfectly, as DWARF does not
+            tell us whether or not the underlying type was specified
+            in the source (and other debug formats don't provide this
+            at all).  We choose to print the underlying type, if it
+            has a name, when in C++ on the theory that it's better to
+            print too much than too little; but conversely not to
+            print something egregiously outside the current
+            language's syntax.  */
+         if (current_language->la_language == language_cplus
+             && TYPE_TARGET_TYPE (type) != NULL)
+           {
+             struct type *underlying = check_typedef (TYPE_TARGET_TYPE (type));
+
+             if (TYPE_NAME (underlying) != NULL)
+               fprintf_filtered (stream, ": %s ", TYPE_NAME (underlying));
+           }
+
          fprintf_filtered (stream, "{");
          len = TYPE_NFIELDS (type);
          for (i = 0; i < len; i++)
@@ -1363,6 +1408,55 @@ c_type_print_base (struct type *type, struct ui_file *stream,
        }
       break;
 
+    case TYPE_CODE_FLAGS:
+      {
+       struct type_print_options local_flags = *flags;
+
+       local_flags.local_typedefs = NULL;
+
+       c_type_print_modifier (type, stream, 0, 1);
+       fprintf_filtered (stream, "flag ");
+       print_name_maybe_canonical (TYPE_NAME (type), flags, stream);
+       if (show > 0)
+         {
+           fputs_filtered (" ", stream);
+           fprintf_filtered (stream, "{\n");
+           if (TYPE_NFIELDS (type) == 0)
+             {
+               if (TYPE_STUB (type))
+                 fprintfi_filtered (level + 4, stream,
+                                    _("<incomplete type>\n"));
+               else
+                 fprintfi_filtered (level + 4, stream,
+                                    _("<no data fields>\n"));
+             }
+           len = TYPE_NFIELDS (type);
+           for (i = 0; i < len; i++)
+             {
+               QUIT;
+               print_spaces_filtered (level + 4, stream);
+               /* We pass "show" here and not "show - 1" to get enum types
+                  printed.  There's no other way to see them.  */
+               c_print_type (TYPE_FIELD_TYPE (type, i),
+                             TYPE_FIELD_NAME (type, i),
+                             stream, show, level + 4,
+                             &local_flags);
+               fprintf_filtered (stream, " @%s",
+                                 plongest (TYPE_FIELD_BITPOS (type, i)));
+               if (TYPE_FIELD_BITSIZE (type, i) > 1)
+                 {
+                   fprintf_filtered (stream, "-%s",
+                                     plongest (TYPE_FIELD_BITPOS (type, i)
+                                               + TYPE_FIELD_BITSIZE (type, i)
+                                               - 1));
+                 }
+               fprintf_filtered (stream, ";\n");
+             }
+           fprintfi_filtered (level, stream, "}");
+         }
+      }
+      break;
+
     case TYPE_CODE_VOID:
       fprintf_filtered (stream, "void");
       break;
This page took 0.091206 seconds and 4 git commands to generate.