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] Inlining of expression function returning controlled object


Pragma Inline_Always has been extended to support inlining of calls
to expression functions that return a controlled object if the
expression function fulfills all the following requirements:
  1. Has pragma/aspect Inline_Always
  2. Has no formals
  3. Has no contracts
  4. Has no dispatching primitive
  5. Its result subtype is controlled (or with controlled components)
  6. Its result subtype not subject to type-invariant checks
  7. Its result subtype not a class-wide type
  8. Its return expression naming an object global to the function
  9. The nominal subtype of the returned object statically compatible with
     the result subtype of the expression function.

After this enhancement, using the following sources, the call to the
expression function Ada_Exception_Trace is now inlined.

with Ada.Finalization;
package Param is
   type T is abstract tagged private;
private
   type T is abstract new Ada.Finalization.Controlled with null record;

   procedure Initialize (Obj : in out T);
   procedure Adjust     (Obj : in out T);
   procedure Finalize   (Obj : in out T);
end;

package Param.Debug is
   type T is private;

   function Value (Parameter : T) return Boolean with Inline_Always;
   function Ada_Exception_Trace return T with Inline_Always;
   procedure Do_Test;
private
   type Comp_T is new Param.T with record
      Value : Boolean := True;
   end record;

   type T is record
      Component : Comp_T;
   end record;

   function Value (Parameter : T) return Boolean
     is (Parameter.Component.Value);

   Private_Ada_Exception_Trace : T;

   function Ada_Exception_Trace return T      --  Test
     is (Private_Ada_Exception_Trace);
end Param.Debug;

with Ada.Text_IO; use Ada.Text_IO;
package body Param is
   procedure Initialize (Obj : in out T) is
   begin
      Put_Line ("Initialize()");
   end;

   procedure Adjust (Obj : in out T) is
   begin
      Put_Line ("Adjust()");
   end;

   procedure Finalize (Obj : in out T) is
   begin
      Put_Line ("Finalize()");
   end;
end;

with Ada.Text_IO; use Ada.Text_IO;
package body Param.Debug is
   procedure Do_Test is
   begin
      if Value (Ada_Exception_Trace) then    -- Test
         Put_Line ("Do_Test()");
      end if;
   end;
end;

with Param.Debug;
procedure Main is
begin
   Param.Debug.Do_Test;
end;

Command: gnatmake main.adb; ./main
Output:
  Initialize()
  Do_Test()
  Finalize()

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

2017-01-13  Javier Miranda  <miranda@adacore.com>

	* sem_res.adb (Resolve_Call): Do not establish a transient scope
	for a call to inlinable expression function (since the call will
	be replaced by its returned object).
	* exp_ch6.ads (Is_Inlinable_Expression_Function): New subprogram.
	* exp_ch6.adb (Expression_Of_Expression_Function): New subprogram.
	(Expand_Call): For inlinable expression function call replace the
	call by its returned object.
	(Is_Inlinable_Expression_Function): New subprogram.

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]