From 2d9c4206c9d18b39f4d1c87ddbbda75238b7bf19 Mon Sep 17 00:00:00 2001 From: Javier Miranda Date: Wed, 26 Sep 2018 09:19:04 +0000 Subject: [PATCH] [Ada] Spurious error on interface conversion under ZFP The frontend reports an error under ZFP when performing the type conversion of a tagged object to one of its covered interface types. 2018-09-26 Javier Miranda gcc/ada/ * exp_disp.adb (Expand_Interface_Conversion): No displacement of the pointer needed when the type of the operand is an interface type that maches the target type and we are compiling under configurable runtime. Adding also documentation explaining why this cannot be done when compiling with the full runtime. * exp_intr.adb: Update comment. gcc/testsuite/ * gnat.dg/interface8.adb, gnat.dg/interface8.ads: New testcase. From-SVN: r264628 --- gcc/ada/ChangeLog | 9 +++++++++ gcc/ada/exp_disp.adb | 28 ++++++++++++++++++++++++++++ gcc/ada/exp_intr.adb | 5 ++++- gcc/testsuite/ChangeLog | 4 ++++ gcc/testsuite/gnat.dg/interface8.adb | 9 +++++++++ gcc/testsuite/gnat.dg/interface8.ads | 11 +++++++++++ 6 files changed, 65 insertions(+), 1 deletion(-) create mode 100644 gcc/testsuite/gnat.dg/interface8.adb create mode 100644 gcc/testsuite/gnat.dg/interface8.ads diff --git a/gcc/ada/ChangeLog b/gcc/ada/ChangeLog index d6ce7847c36..16855a7d23f 100644 --- a/gcc/ada/ChangeLog +++ b/gcc/ada/ChangeLog @@ -1,3 +1,12 @@ +2018-09-26 Javier Miranda + + * exp_disp.adb (Expand_Interface_Conversion): No displacement of + the pointer needed when the type of the operand is an interface + type that maches the target type and we are compiling under + configurable runtime. Adding also documentation explaining why + this cannot be done when compiling with the full runtime. + * exp_intr.adb: Update comment. + 2018-09-26 Hristian Kirtchev * sem_ch5.adb (Wrap_Loop_Statement): Annotate as No_Return. diff --git a/gcc/ada/exp_disp.adb b/gcc/ada/exp_disp.adb index cf7ce4986a8..2169b67419a 100644 --- a/gcc/ada/exp_disp.adb +++ b/gcc/ada/exp_disp.adb @@ -1339,11 +1339,39 @@ package body Exp_Disp is Opnd := Designated_Type (Opnd); end if; + Opnd := Underlying_Record_Type (Opnd); + if not Is_Interface (Opnd) and then Is_Ancestor (Iface_Typ, Opnd, Use_Full_View => True) then return; end if; + + -- When the type of the operand and the target interface type match, + -- it is generally safe to skip generating code to displace the + -- pointer to the object to reference the secondary dispatch table + -- associated with the target interface type. The exception to this + -- general rule is when the underlying object of the type conversion + -- is an object built by means of a dispatching constructor (since in + -- such case the expansion of the constructor call is a direct call + -- to an object primitive, i.e. without thunks, and the expansion of + -- the constructor call adds an explicit conversion to the target + -- interface type to force the displacement of the pointer to the + -- object to reference the corresponding secondary dispatch table + -- (cf. Make_DT and Expand_Dispatching_Constructor_Call)). + + -- At this stage we cannot identify whether the underlying object is + -- a BIP object and hence we cannot skip generating the code to try + -- displacing the pointer to the object. However, under configurable + -- runtime it is safe to skip generating code to displace the pointer + -- to the object, because generic dispatching constructors are not + -- supported. + + if Opnd = Iface_Typ + and then not RTE_Available (RE_Displace) + then + return; + end if; end; -- Evaluate if we can statically displace the pointer to the object diff --git a/gcc/ada/exp_intr.adb b/gcc/ada/exp_intr.adb index 73981faa4c0..4f4584bad6a 100644 --- a/gcc/ada/exp_intr.adb +++ b/gcc/ada/exp_intr.adb @@ -402,7 +402,10 @@ package body Exp_Intr is end if; -- Rewrite and analyze the call to the instance as a class-wide - -- conversion of the call to the actual constructor. + -- conversion of the call to the actual constructor. When the result + -- type is a class-wide interface type this conversion is required to + -- force the displacement of the pointer to the object to reference the + -- corresponding dispatch table. Rewrite (N, Convert_To (Result_Typ, Cnstr_Call)); diff --git a/gcc/testsuite/ChangeLog b/gcc/testsuite/ChangeLog index e285be64939..ccebb8a56be 100644 --- a/gcc/testsuite/ChangeLog +++ b/gcc/testsuite/ChangeLog @@ -1,3 +1,7 @@ +2018-09-26 Javier Miranda + + * gnat.dg/interface8.adb, gnat.dg/interface8.ads: New testcase. + 2018-09-26 Ed Schonberg * gnat.dg/predicate2-containers.ads, diff --git a/gcc/testsuite/gnat.dg/interface8.adb b/gcc/testsuite/gnat.dg/interface8.adb new file mode 100644 index 00000000000..5537ddb31e5 --- /dev/null +++ b/gcc/testsuite/gnat.dg/interface8.adb @@ -0,0 +1,9 @@ +-- { dg-do compile } + +package body Interface8 is + function Get_Iface (This : Child) return not null access Iface'Class + is + begin + return This.Interface_1; + end; +end; diff --git a/gcc/testsuite/gnat.dg/interface8.ads b/gcc/testsuite/gnat.dg/interface8.ads new file mode 100644 index 00000000000..d67b3751854 --- /dev/null +++ b/gcc/testsuite/gnat.dg/interface8.ads @@ -0,0 +1,11 @@ +package Interface8 is + type Iface is interface; + + type Root is abstract tagged null record; + + type Child is new Root and Iface with record + Interface_1 : access Iface'Class; + end record; + + function Get_Iface (This : Child) return not null access Iface'Class; +end; -- 2.30.2