]> git.ipfire.org Git - thirdparty/gcc.git/blobdiff - gcc/gengtype.c
Merge from trunk.
[thirdparty/gcc.git] / gcc / gengtype.c
index 85be41bf0fc5a11a4351b278c6ab862d13df4e56..c2172c928fb402459b7c817f4ed17b38ed799d7f 100644 (file)
@@ -25,7 +25,6 @@
 #include "system.h"
 #include "errors.h"            /* for fatal */
 #include "getopt.h"
-#include "double-int.h"
 #include "version.h"           /* for version_string & pkgversion_string.  */
 #include "hashtab.h"
 #include "xregex.h"
@@ -137,6 +136,16 @@ xasprintf (const char *format, ...)
   return result;
 }
 \f
+/* Locate the ultimate base class of struct S.  */
+
+static const_type_p
+get_ultimate_base_class (const_type_p s)
+{
+  while (s->u.s.base_class)
+    s = s->u.s.base_class;
+  return s;
+}
+\f
 /* Input file handling. */
 
 /* Table of all input files.  */
@@ -525,7 +534,7 @@ do_typedef (const char *s, type_p t, struct fileloc *pos)
   for (p = typedefs; p != NULL; p = p->next)
     if (strcmp (p->name, s) == 0)
       {
-       if (p->type != t)
+       if (p->type != t && strcmp (s, "result_type") != 0)
          {
            error_at_line (pos, "type `%s' previously defined", s);
            error_at_line (&p->line, "previously defined here");
@@ -667,6 +676,16 @@ resolve_typedef (const char *s, struct fileloc *pos)
   return p;
 }
 
+/* Add SUBCLASS to head of linked list of BASE's subclasses.  */
+
+void add_subclass (type_p base, type_p subclass)
+{
+  gcc_assert (union_or_struct_p (base));
+  gcc_assert (union_or_struct_p (subclass));
+
+  subclass->u.s.next_sibling_class = base->u.s.first_subclass;
+  base->u.s.first_subclass = subclass;
+}
 
 /* Create and return a new structure with tag NAME at POS with fields
    FIELDS and options O.  The KIND of structure must be one of
@@ -674,7 +693,7 @@ resolve_typedef (const char *s, struct fileloc *pos)
 
 type_p
 new_structure (const char *name, enum typekind kind, struct fileloc *pos,
-              pair_p fields, options_p o)
+              pair_p fields, options_p o, type_p base_class)
 {
   type_p si;
   type_p s = NULL;
@@ -748,6 +767,9 @@ new_structure (const char *name, enum typekind kind, struct fileloc *pos,
   s->u.s.bitmap = bitmap;
   if (s->u.s.lang_struct)
     s->u.s.lang_struct->u.s.bitmap |= bitmap;
+  s->u.s.base_class = base_class;
+  if (base_class)
+    add_subclass (base_class, s);
 
   return s;
 }
@@ -976,7 +998,7 @@ create_optional_field_ (pair_p next, type_p type, const char *name,
     create_string_option (union_fields->opt, "tag", "1");
   union_type = 
     new_structure (xasprintf ("%s_%d", "fake_union", id++), TYPE_UNION,
-                   &lexer_line, union_fields, NULL);
+                   &lexer_line, union_fields, NULL, NULL);
 
   /* Create the field and give it the new fake union type.  Add a "desc"
      tag that specifies the condition under which the field is valid.  */
@@ -1167,7 +1189,7 @@ adjust_field_rtx_def (type_p t, options_p ARG_UNUSED (opt))
            create_string_option (nodot, "tag", note_insn_name[c]);
       }
     note_union_tp = new_structure ("rtx_def_note_subunion", TYPE_UNION,
-                                  &lexer_line, note_flds, NULL);
+                                  &lexer_line, note_flds, NULL, NULL);
   }
   /* Create a type to represent the various forms of SYMBOL_REF_DATA.  */
   {
@@ -1177,7 +1199,7 @@ adjust_field_rtx_def (type_p t, options_p ARG_UNUSED (opt))
     sym_flds = create_field (sym_flds, constant_tp, "rt_constant");
     sym_flds->opt = create_string_option (nodot, "tag", "1");
     symbol_union_tp = new_structure ("rtx_def_symbol_subunion", TYPE_UNION,
-                                    &lexer_line, sym_flds, NULL);
+                                    &lexer_line, sym_flds, NULL, NULL);
   }
   for (i = 0; i < NUM_RTX_CODE; i++)
     {
@@ -1319,7 +1341,7 @@ adjust_field_rtx_def (type_p t, options_p ARG_UNUSED (opt))
 
       sname = xasprintf ("rtx_def_%s", rtx_name[i]);
       substruct = new_structure (sname, TYPE_STRUCT, &lexer_line, subfields,
-                                NULL);
+                                NULL, NULL);
 
       ftag = xstrdup (rtx_name[i]);
       for (nmindex = 0; nmindex < strlen (ftag); nmindex++)
@@ -1328,7 +1350,7 @@ adjust_field_rtx_def (type_p t, options_p ARG_UNUSED (opt))
       flds->opt = create_string_option (nodot, "tag", ftag);
     }
   return new_structure ("rtx_def_subunion", TYPE_UNION, &lexer_line, flds,
-                       nodot);
+                       nodot, NULL);
 }
 
 /* Handle `special("tree_exp")'.  This is a special case for
@@ -1358,7 +1380,7 @@ adjust_field_tree_exp (type_p t, options_p opt ATTRIBUTE_UNUSED)
   flds->opt = create_string_option (flds->opt, "default", "");
 
   return new_structure ("tree_exp_subunion", TYPE_UNION, &lexer_line, flds,
-                       nodot);
+                       nodot, NULL);
 }
 
 /* Perform any special processing on a type T, about to become the type
@@ -1531,7 +1553,17 @@ set_gc_used_type (type_p t, enum gc_used_enum level, type_p param[NUM_PARAM],
        process_gc_options (t->u.s.opt, level, &dummy, &dummy, &dummy, &dummy,
                            &dummy2);
 
-       for (f = t->u.s.fields; f; f = f->next)
+       if (t->u.s.base_class)
+         set_gc_used_type (t->u.s.base_class, level, param,
+                           allow_undefined_types);
+       /* Anything pointing to a base class might actually be pointing
+          to a subclass.  */
+       for (type_p subclass = t->u.s.first_subclass; subclass;
+            subclass = subclass->u.s.next_sibling_class)
+         set_gc_used_type (subclass, level, param,
+                           allow_undefined_types);
+
+       FOR_ALL_INHERITED_FIELDS(t, f)
          {
            int maybe_undef = 0;
            int pass_param = 0;
@@ -1733,12 +1765,13 @@ open_base_files (void)
     static const char *const ifiles[] = {
       "config.h", "system.h", "coretypes.h", "tm.h",
       "hashtab.h", "splay-tree.h", "obstack.h", "bitmap.h", "input.h",
-      "tree.h", "rtl.h", "function.h", "insn-config.h", "expr.h",
+      "tree.h", "rtl.h", "wide-int.h", "function.h", "insn-config.h", "expr.h",
       "hard-reg-set.h", "basic-block.h", "cselib.h", "insn-addr.h",
       "optabs.h", "libfuncs.h", "debug.h", "ggc.h", "cgraph.h",
-      "gimple.h", "gimple-ssa.h", "tree-cfg.h", "tree-phinodes.h",
-      "ssa-iterators.h", "tree-ssanames.h", "tree-ssa-loop.h",
-      "tree-into-ssa.h", "tree-dfa.h", 
+      "gimple.h", "gimple-iterator.h", "gimple-ssa.h", "tree-cfg.h",
+      "tree-phinodes.h", "ssa-iterators.h", "stringpool.h", "tree-ssanames.h",
+      "tree-ssa-loop.h", "tree-ssa-loop-ivopts.h", "tree-ssa-loop-manip.h",
+      "tree-ssa-loop-niter.h", "tree-into-ssa.h", "tree-dfa.h", 
       "tree-ssa.h", "reload.h", "cpp-id-data.h", "tree-chrec.h",
       "except.h", "output.h",  "cfgloop.h",
       "target.h", "ipa-prop.h", "lto-streamer.h", "target-globals.h",
@@ -2342,6 +2375,8 @@ is_file_equal (outf_p of)
          break;
        }
     }
+  if (equal && EOF != fgetc (newfile))
+    equal = false;
   fclose (newfile);
   return equal;
 }
@@ -2543,6 +2578,10 @@ output_mangled_typename (outf_p of, const_type_p t)
       case TYPE_LANG_STRUCT:
       case TYPE_USER_STRUCT:
        {
+         /* For references to classes within an inheritance hierarchy,
+            only ever reference the ultimate base class, since only
+            it will have gt_ functions.  */
+         t = get_ultimate_base_class (t);
          const char *id_for_tag = filter_type_name (t->u.s.tag);
          oprintf (of, "%lu%s", (unsigned long) strlen (id_for_tag),
                   id_for_tag);
@@ -2605,6 +2644,42 @@ output_escaped_param (struct walk_type_data *d, const char *param,
        }
 }
 
+const char *
+get_string_option (options_p opt, const char *key)
+{
+  for (; opt; opt = opt->next)
+    if (strcmp (opt->name, key) == 0)
+      return opt->info.string;
+  return NULL;
+}
+
+static void
+walk_subclasses (type_p base, struct walk_type_data *d)
+{
+  for (type_p sub = base->u.s.first_subclass; sub != NULL;
+       sub = sub->u.s.next_sibling_class)
+    {
+      const char *type_tag = get_string_option (sub->u.s.opt, "tag");
+      if (type_tag)
+       {
+         oprintf (d->of, "%*scase %s:\n", d->indent, "", type_tag);
+         d->indent += 2;
+         oprintf (d->of, "%*s{\n", d->indent, "");
+         d->indent += 2;
+         oprintf (d->of, "%*s%s *sub = static_cast <%s *> (x);\n",
+                  d->indent, "", sub->u.s.tag, sub->u.s.tag);
+         const char *old_val = d->val;
+         d->val = "(*sub)";
+         walk_type (sub, d);
+         d->val = old_val;
+         d->indent -= 2;
+         oprintf (d->of, "%*s}\n", d->indent, "");
+         oprintf (d->of, "%*sbreak;\n", d->indent, "");
+         d->indent -= 2;
+       }
+      walk_subclasses (sub, d);
+    }
+}
 
 /* Call D->PROCESS_FIELD for every field (or subfield) of D->VAL,
    which is of type T.  Write code to D->OF to constrain execution (at
@@ -2622,6 +2697,7 @@ walk_type (type_p t, struct walk_type_data *d)
 {
   const char *length = NULL;
   const char *desc = NULL;
+  const char *type_tag = NULL;
   int maybe_undef_p = 0;
   int use_param_num = -1;
   int use_params_p = 0;
@@ -2650,7 +2726,7 @@ walk_type (type_p t, struct walk_type_data *d)
     else if (strcmp (oo->name, "dot") == 0)
       ;
     else if (strcmp (oo->name, "tag") == 0)
-      ;
+      type_tag = oo->info.string;
     else if (strcmp (oo->name, "special") == 0)
       ;
     else if (strcmp (oo->name, "skip") == 0)
@@ -2969,8 +3045,32 @@ walk_type (type_p t, struct walk_type_data *d)
            d->indent += 2;
            oprintf (d->of, "%*s{\n", d->indent, "");
          }
+       else if (desc)
+         {
+           /* We have a "desc" option on a struct, signifying the
+              base class within a GC-managed inheritance hierarchy.
+              The current code specialcases the base class, then walks
+              into subclasses, recursing into this routine to handle them.
+              This organization requires the base class to have a case in
+              the switch statement, and hence a tag value is mandatory
+              for the base class.   This restriction could be removed, but
+              it would require some restructing of this code.  */
+           if (!type_tag)
+             {
+               error_at_line (d->line,
+                              "missing `tag' option for type `%s'",
+                              t->u.s.tag);
+             }
+           oprintf (d->of, "%*sswitch (", d->indent, "");
+           output_escaped_param (d, desc, "desc");
+           oprintf (d->of, ")\n");
+           d->indent += 2;
+           oprintf (d->of, "%*s{\n", d->indent, "");
+           oprintf (d->of, "%*scase %s:\n", d->indent, "", type_tag);
+           d->indent += 2;
+         }
 
-       for (f = t->u.s.fields; f; f = f->next)
+       FOR_ALL_INHERITED_FIELDS (t, f)
          {
            options_p oo;
            int skip_p = 0;
@@ -3008,7 +3108,7 @@ walk_type (type_p t, struct walk_type_data *d)
          }
        endcounter = d->counter;
 
-       for (f = t->u.s.fields; f; f = f->next)
+       FOR_ALL_INHERITED_FIELDS (t, f)
          {
            options_p oo;
            const char *dot = ".";
@@ -3110,11 +3210,36 @@ walk_type (type_p t, struct walk_type_data *d)
            oprintf (d->of, "%*sdefault:\n", d->indent, "");
            oprintf (d->of, "%*s  break;\n", d->indent, "");
          }
+
+       if (desc && !union_p)
+         {
+               oprintf (d->of, "%*sbreak;\n", d->indent, "");
+               d->indent -= 2;
+          }
        if (union_p)
          {
            oprintf (d->of, "%*s}\n", d->indent, "");
            d->indent -= 2;
          }
+       else if (desc)
+         {
+           /* Add cases to handle subclasses.  */
+           walk_subclasses (t, d);
+
+           /* Ensure that if someone forgets a "tag" option that we don't
+              silent fail to traverse that subclass's fields.  */
+           if (!seen_default_p)
+             {
+               oprintf (d->of, "%*s/* Unrecognized tag value.  */\n",
+                        d->indent, "");
+               oprintf (d->of, "%*sdefault: gcc_unreachable (); \n",
+                        d->indent, "");
+             }
+
+           /* End of the switch statement */
+           oprintf (d->of, "%*s}\n", d->indent, "");
+           d->indent -= 2;
+         }
        if (any_length_seen)
          {
            d->indent -= 2;
@@ -3460,6 +3585,24 @@ write_func_for_structure (type_p orig_s, type_p s, type_p *param,
   options_p opt;
   struct walk_type_data d;
 
+  if (s->u.s.base_class)
+    {
+      /* Verify that the base class has a "desc", since otherwise
+        the traversal hooks there won't attempt to visit fields of
+        subclasses such as this one.  */
+      const_type_p ubc = get_ultimate_base_class (s);
+      if ((!opts_have (ubc->u.s.opt, "user")
+          && !opts_have (ubc->u.s.opt, "desc")))
+       error_at_line (&s->u.s.line,
+                      ("'%s' is a subclass of non-GTY(user) GTY class '%s'"
+                       ", but '%s' lacks a discriminator 'desc' option"),
+                      s->u.s.tag, ubc->u.s.tag, ubc->u.s.tag);
+
+      /* Don't write fns for subclasses, only for the ultimate base class
+        within an inheritance hierarchy.  */
+      return;
+    }
+
   memset (&d, 0, sizeof (d));
   d.of = get_output_file_for_structure (s, param);
   for (opt = s->u.s.opt; opt; opt = opt->next)
@@ -3636,7 +3779,10 @@ write_types (outf_p output_header, type_p structures, type_p param_structs,
      emitted afterwards.  This is needed in plugin mode.  */
   oprintf (output_header, "/* Macros and declarations.  */\n");
   for (s = structures; s; s = s->next)
-    if (s->gc_used == GC_POINTED_TO || s->gc_used == GC_MAYBE_POINTED_TO)
+    /* Do not emit handlers for derived classes; we only ever deal with
+       the ultimate base class within an inheritance hierarchy.  */
+    if ((s->gc_used == GC_POINTED_TO || s->gc_used == GC_MAYBE_POINTED_TO)
+        && !s->u.s.base_class)
       {
        options_p opt;
 
@@ -3941,6 +4087,11 @@ write_local_func_for_structure (const_type_p orig_s, type_p s, type_p *param)
 {
   struct walk_type_data d;
 
+  /* Don't write fns for subclasses, only for the ultimate base class
+     within an inheritance hierarchy.  */
+  if (s->u.s.base_class)
+    return;
+
   memset (&d, 0, sizeof (d));
   d.of = get_output_file_for_structure (s, param);
   d.process_field = write_types_local_process_field;
@@ -4085,7 +4236,9 @@ write_local (outf_p output_header, type_p structures, type_p param_structs)
           || ((s)->gc_used == GC_MAYBE_POINTED_TO                      \
               && s->u.s.line.file != NULL)                             \
           || ((s)->gc_used == GC_USED                                  \
-              && strncmp (s->u.s.tag, "anonymous", strlen ("anonymous"))))))
+              && strncmp (s->u.s.tag, "anonymous", strlen ("anonymous"))) \
+          || (s->u.s.base_class && opts_have (s->u.s.opt, "tag")))))
+
 
 
 /* Might T contain any non-pointer elements?  */
@@ -4360,7 +4513,7 @@ write_root (outf_p f, pair_p v, type_p type, const char *name, int has_length,
 
     case TYPE_POINTER:
       {
-       type_p tp;
+       const_type_p tp;
 
        if (!start_root_entry (f, v, name, line))
          return;
@@ -4369,6 +4522,7 @@ write_root (outf_p f, pair_p v, type_p type, const char *name, int has_length,
 
        if (!has_length && union_or_struct_p (tp))
          {
+           tp = get_ultimate_base_class (tp);
            const char *id_for_tag = filter_type_name (tp->u.s.tag);
            oprintf (f, "    &gt_ggc_mx_%s,\n", id_for_tag);
            if (emit_pch)
@@ -5478,6 +5632,9 @@ main (int argc, char **argv)
       POS_HERE (do_scalar_typedef ("REAL_VALUE_TYPE", &pos));
       POS_HERE (do_scalar_typedef ("FIXED_VALUE_TYPE", &pos));
       POS_HERE (do_scalar_typedef ("double_int", &pos));
+      POS_HERE (do_scalar_typedef ("double_int_storage", &pos));
+      POS_HERE (do_scalar_typedef ("offset_int", &pos));
+      POS_HERE (do_scalar_typedef ("widest_int", &pos));
       POS_HERE (do_scalar_typedef ("uint64_t", &pos));
       POS_HERE (do_scalar_typedef ("uint8", &pos));
       POS_HERE (do_scalar_typedef ("uintptr_t", &pos));