https://gcc.gnu.org/bugzilla/show_bug.cgi?id=65696

--- Comment #2 from yuta tomino <demoonlit at panathenaia dot halfmoon.jp> ---
Here is a proposing fix.
I has tried this patch with gcc from 4.8 to 7, and it seems to work fine with
all versions.

diff --git a/gcc/ada/exp_atag.adb b/gcc/ada/exp_atag.adb
index 587432c..4313446 100644
--- a/gcc/ada/exp_atag.adb
+++ b/gcc/ada/exp_atag.adb
@@ -744,7 +744,8 @@ package body Exp_Atag is
    function Build_Inherit_Predefined_Prims
      (Loc          : Source_Ptr;
       Old_Tag_Node : Node_Id;
-      New_Tag_Node : Node_Id) return Node_Id
+      New_Tag_Node : Node_Id;
+      Num_Predef_Prims : Int) return Node_Id
    is
    begin
       return
@@ -759,7 +760,7 @@ package body Exp_Atag is
                         New_Tag_Node)))),
               Discrete_Range => Make_Range (Loc,
                 Make_Integer_Literal (Loc, Uint_1),
-                New_Occurrence_Of (RTE (RE_Max_Predef_Prims), Loc))),
+                Make_Integer_Literal (Loc, Num_Predef_Prims))),

           Expression =>
             Make_Slice (Loc,
@@ -772,7 +773,7 @@ package body Exp_Atag is
               Discrete_Range =>
                 Make_Range (Loc,
                   Make_Integer_Literal (Loc, 1),
-                  New_Occurrence_Of (RTE (RE_Max_Predef_Prims), Loc))));
+                  Make_Integer_Literal (Loc, Num_Predef_Prims))));
    end Build_Inherit_Predefined_Prims;

    -------------------------
diff --git a/gcc/ada/exp_atag.ads b/gcc/ada/exp_atag.ads
index d53466f..eb4be55 100644
--- a/gcc/ada/exp_atag.ads
+++ b/gcc/ada/exp_atag.ads
@@ -111,7 +111,8 @@ package Exp_Atag is
    function Build_Inherit_Predefined_Prims
      (Loc          : Source_Ptr;
       Old_Tag_Node : Node_Id;
-      New_Tag_Node : Node_Id) return Node_Id;
+      New_Tag_Node : Node_Id;
+      Num_Predef_Prims : Int) return Node_Id;
    --  Build code that inherits the predefined primitives of the parent.
    --
    --  Generates: Predefined_DT (New_T).D (All_Predefined_Prims) :=
diff --git a/gcc/ada/exp_disp.adb b/gcc/ada/exp_disp.adb
index 2b63377..ef7440f 100644
--- a/gcc/ada/exp_disp.adb
+++ b/gcc/ada/exp_disp.adb
@@ -4524,6 +4524,7 @@ package body Exp_Disp is
       Iface_Table_Node   : Node_Id;
       Name_ITable        : Name_Id;
       Nb_Predef_Prims    : Nat := 0;
+      Cp_Predef_Prims    : Nat := 0;
       Nb_Prim            : Nat := 0;
       New_Node           : Node_Id;
       Num_Ifaces         : Nat := 0;
@@ -5690,10 +5691,6 @@ package body Exp_Disp is
             Pos : Nat;

          begin
-            if not Building_Static_DT (Typ) then
-               Nb_Predef_Prims := Max_Predef_Prims;
-
-            else
                Prim_Elmt := First_Elmt (Primitive_Operations (Typ));
                while Present (Prim_Elmt) loop
                   Prim := Node (Prim_Elmt);
@@ -5703,13 +5700,18 @@ package body Exp_Disp is
                   then
                      Pos := UI_To_Int (DT_Position (Prim));

-                     if Pos > Nb_Predef_Prims then
-                        Nb_Predef_Prims := Pos;
+                     if Pos > Cp_Predef_Prims then
+                        Cp_Predef_Prims := Pos;
                      end if;
                   end if;

                   Next_Elmt (Prim_Elmt);
                end loop;
+
+            if not Building_Static_DT (Typ) then
+               Nb_Predef_Prims := Max_Predef_Prims;
+            else
+               Nb_Predef_Prims := Cp_Predef_Prims;
             end if;

             declare
@@ -6054,7 +6056,8 @@ package body Exp_Disp is
                           (Node
                             (Next_Elmt
                               (First_Elmt
-                                (Access_Disp_Table (Typ)))), Loc)));
+                                (Access_Disp_Table (Typ)))), Loc),
+                      Num_Predef_Prims => Cp_Predef_Prims));

                   if Nb_Prims /= 0 then
                      Append_To (Elab_Code,
@@ -6143,7 +6146,8 @@ package body Exp_Disp is
                                           Unchecked_Convert_To (RTE (RE_Tag),
                                             New_Occurrence_Of
                                               (Node (Next_Elmt (Sec_DT_Typ)),
-                                               Loc))));
+                                               Loc)),
+                                        Num_Predef_Prims => Cp_Predef_Prims));

                                     if Num_Prims /= 0 then
                                        Append_To (Elab_Code,
@@ -6189,7 +6193,8 @@ package body Exp_Disp is
                                           Unchecked_Convert_To (RTE (RE_Tag),
                                             New_Occurrence_Of
                                               (Node (Next_Elmt (Sec_DT_Typ)),
-                                               Loc))));
+                                               Loc)),
+                                        Num_Predef_Prims => Cp_Predef_Prims));

                                     if Num_Prims /= 0 then
                                        Append_To (Elab_Code,

Reply via email to