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] Ghost entities


This patch provides partial support for ghost entities, in particular ghost
subprograms. Ghost entities are no longer categorized as such by an aspect,
instead one should use aspect/pragma Convention with convention_identifier
Ghost.

------------
-- Source --
------------

--  gen.ads

generic
   type Element is private;
   with function Formal_Func (Val : Element) return Element;

package Gen is
end Gen;

--  illegal_usage.ads

with Gen;

package Illegal_Usage is

   function Ghost_Func (Val : Integer) return Integer
     with Convention => Ghost;

   function Ren_GF_1 (Val : Integer) return Integer renames Ghost_Func;
   function Ren_GF_2 (Val : Integer) return Integer renames Ren_GF_1;

   subtype Nat_Subtype_1 is Natural
     with Dynamic_Predicate => Ghost_Func (Nat_Subtype_1) > 1;
   subtype Nat_Subtype_2 is Natural
     with Dynamic_Predicate => Ren_GF_1 (Nat_Subtype_2) > 1;
   subtype Nat_Subtype_3 is Natural
     with Dynamic_Predicate => Ren_GF_2 (Nat_Subtype_3) > 1;
   --  Ghost function cannot be called from subtype predicates

   package Inst is new Gen (Integer, Ghost_Func);
   --  Ghost function cannot act as generic actuals

   type Iface_1 is interface;
   function Func_1 return Iface_1 is abstract;
   function Func_2 return Iface_1 is abstract;

   type Impl_Type_1 is new Iface_1 with null record;
   overriding function Func_1 return Impl_Type_1
     with Convention => Ghost;
   function Func_2 return Impl_Type_1
     with Convention => Ghost;
   --  Ghost functions cannot override

   type Iface_2 is interface;
   function Func_3 return Iface_2 is abstract with Convention => Ghost;
   function Func_4 return Iface_2 is abstract with Convention => Ghost;

   type Impl_Type_2 is new Iface_2 with null record;
   overriding function Func_3 return Impl_Type_2
     with Convention => Ghost;
   function Func_4 return Impl_Type_2
     with Convention => Ghost;
   --  Ghost functions cannot override

   procedure Proc;
   --  An illegal context that does not allow the use of ghost functions

end Illegal_Usage;

--  illegal_usage.adb

package body Illegal_Usage is
   overriding function Func_1 return Impl_Type_1 is
      Result : Impl_Type_1;
   begin
      return Result;
   end Func_1;

   function Func_2 return Impl_Type_1 is
   begin
      return Func_1;
   end Func_2;

   overriding function Func_3 return Impl_Type_2 is
      Result : Impl_Type_2;
   begin
      return Result;
   end Func_3;

   function Func_4 return Impl_Type_2 is
   begin
      return Func_3;
   end Func_4;

   function Ghost_Func (Val : Integer) return Integer is
   begin
      return Val + 1;
   end Ghost_Func;

   procedure Proc is
      type Ghost_Func_Ptr is access function (Val : Integer) return Integer;
      Ptr : constant Ghost_Func_Ptr := Ghost_Func'Access;
      --  Cannot take 'Access of a ghost function

      Var : Integer;

   begin
      Var := Ghost_Func (1);
      Var := Ren_GF_1 (2);
      Var := Ren_GF_2 (3);
      --  Cannot call ghost functions from a non-ghost function context
   end Proc;
end Illegal_Usage;

----------------------------
-- Compilation and output --
----------------------------

$ gcc -c -gnat12 -gnata illegal_usage.adb
illegal_usage.adb:31:40: prefix of "Access" attribute cannot be a ghost
  subprogram
illegal_usage.adb:37:14: call to ghost subprogram must appear in assertion
  expression or another ghost subprogram
illegal_usage.adb:38:14: call to ghost subprogram must appear in assertion
  expression or another ghost subprogram
illegal_usage.adb:39:14: call to ghost subprogram must appear in assertion
  expression or another ghost subprogram
illegal_usage.ads:12:32: call to ghost subprogram must appear in assertion
  expression or another ghost subprogram
illegal_usage.ads:14:32: call to ghost subprogram must appear in assertion
  expression or another ghost subprogram
illegal_usage.ads:16:32: call to ghost subprogram must appear in assertion
  expression or another ghost subprogram
illegal_usage.ads:19:38: ghost subprogram "Ghost_Func" cannot act as generic
  actual
illegal_usage.ads:19:38: instantiation abandoned
illegal_usage.ads:27:24: ghost subprogram "Func_1" cannot be overriding
illegal_usage.ads:29:13: ghost subprogram "Func_2" cannot be overriding
illegal_usage.ads:38:24: ghost subprogram "Func_3" cannot be overriding
illegal_usage.ads:40:13: ghost subprogram "Func_4" cannot be overriding

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

2013-04-25  Hristian Kirtchev  <kirtchev@adacore.com>

	* aspects.ads, aspects.adb: Remove aspect Ghost from all relevant
	tables.
	* einfo.adb: Remove with and use clause for Aspects.
	(Is_Ghost_Function): Removed.
	(Is_Ghost_Entity): New routine.
	(Is_Ghost_Subprogram): New routine.
	* einfo.ads: Remove synthesized attribute Is_Ghost_Function
	along with its uses in entities.  Add synthesized attributes
	Is_Ghost_Entity and Is_Ghost_Subprogram along with uses in related
	entities.
	(Is_Ghost_Function): Removed.
	(Is_Ghost_Entity): New routine.
	(Is_Ghost_Subprogram): New routine.
	* par-prag.adb: Remove pragma Ghost from the processing machinery.
	* repinfo.adb (List_Mechanisms): Add a value for convention Ghost.
	* sem_attr.adb (Analyze_Access_Attribute): Update the check
	for ghost subprograms.
	* sem_ch4.adb (Analyze_Call): Update the check for calls
	to ghost subprograms.
	(Check_Ghost_Function_Call): Removed.
	(Check_Ghost_Subprogram_Call): New routine.
	* sem_ch6.adb (Check_Convention): Rewritten.
	(Check_Overriding_Indicator): Remove the check for overriding
	ghost functions.
	(Convention_Of): New routine.
	* sem_ch12.adb (Preanalyze_Actuals): Update the check for ghost
	generic actual subprograms.
	* sem_mech.adb (Set_Mechanisms): Add an entry for convention Ghost.
	* sem_prag.adb: Remove the value for pragma Ghost from
	table Sig_Flags.
	(Analyze_Pragma): Remove the processing for pragma Ghost.
	(Process_Convention): Emit an error when a ghost
	subprogram attempts to override.
	(Set_Convention_From_Pragma): Emit an error when a ghost subprogram
	attempts to override.
	* sinfo.ads: Clarify the usage of field Label_Construct.
	* snames.adb-tmpl (Get_Convention_Id): Add an entry for
	predefined name Ghost.
	(Get_Convention_Name): Add an entry for convention Ghost.
	* snames.ads-tmpl: Move predefined name Ghost to the sublist
	denoting conventions. Add convention id Ghost. Remove pragma
	id Ghost.

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]