This is the mail archive of the gcc-patches@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

[Ada] Semantics of equality renaming in Ada2012


A renaming of equality on an untagged record type captures the meaning of
equality at the point of the renaming. A later user-defined equality on
the type of some component may affect primitive equality for the type, so
a body for the renaming must be built at the point of the renaming declaration
rather than waiting for an appropriate freeze point for the type. This is
technically known as squirelling semantics.

The following must compile and execute quietly:

    gnatmake -gnat12 -q ai05_123_3

package Ren is
   type Rec is record
      Name : String (1..5) := "heh??";
   end record;

   type Wrap is record
     Contents : Rec;
   end record;

   function Egal (First, Second : Wrap) return Boolean;
   function Equals (First, Second : Wrap) return Boolean renames "=";

   function "=" (X, Y : Rec) return Boolean;
   function Igual (Un, Deux : Wrap) return Boolean renames "=";
end Ren;
---
package body Ren is

  function Egal (first : wrap; second : wrap) return boolean is
  begin
     return first.contents.name = second.contents.name;
  end Egal;

   function "=" (X, Y : Rec) return Boolean is
   begin
      return X.Name (1) = Y.Name (1);
   end "=";
end Ren;
---
with Ren; use Ren;
procedure AI05_123_3 is
  Obj1 : Rec := (Name => "wow!!");
  Obj2 : Rec := (Name => "whew!");

  W1   : Wrap := (Contents => Obj1);
  W2   : Wrap := (Contents => Obj2);
begin

--  early renaming uses predefined equality
  if Equals (W1, W2) then
      raise Program_Error;
  end if;

  if Egal (W1, W2) then
      raise Program_Error;
  end if;

  --  user defined equality
  if W1 /= W2  then
     raise Program_Error;
  end if;

-- renamed user-defined equality.
 if not Igual (W1, W2)  then
    raise Program_Error;
 end if;
end;

Tested on x86_64-pc-linux-gnu, committed on trunk

2010-09-09  Ed Schonberg  <schonberg@adacore.com>

	* exp_ch3.adb (Build_Untagged_Equality): Do not set alias of implicit
	inequality, it is always rewritten as the negation of the corresponding
	equality operation.
	* exp_ch8.adb (Expand_N_Subprogram_Renaming): If the subprogram renames
	the predefined equality of an untagged record, create a body at the
	point of the renaming, to capture the current meaning of equality for
	the type.

Attachment: difs
Description: Text document


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]