public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
From: Pierre-Marie de Rodat <derodat@adacore.com>
To: gcc-patches@gcc.gnu.org
Cc: Ed Schonberg <schonberg@adacore.com>
Subject: [Ada] Handle defaults in declare_expressions in postconditions
Date: Wed, 5 May 2021 04:20:05 -0400	[thread overview]
Message-ID: <20210505082005.GA31251@adacore.com> (raw)

[-- Attachment #1: Type: text/plain, Size: 1065 bytes --]

In Ada2020 the expression for a postcondition can be a
Declare_Expression containing object declarations with defaults. These
defaults may generate local subtypes, but as for other default
expressions their full expansion must be deferred, typically to the
freeze point of the object being declared, which for a postcondition
will be the analysis of the generated postcondition procedure.
Nevertheless the declarations for such subtypes must appear in the tree
to complete the semantic analysis of the default expression itself.
Given that Insert_Actions is disabled when In_Spec_Expression is set, we
place the generated declaration directly ahead of the local declaration,
and provide the needed semantic information for the subtype.

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

gcc/ada/

	* sem_ch3.adb (Find_Type_Of_Object):  When In_Spec_Expression is
	set and the object declaration generates a subtype indication,
	build the corresponding subtype declaration and place it in tree
	without the use of Insert_Actions, which is disabled in this
	context.

[-- Attachment #2: patch.diff --]
[-- Type: text/x-diff, Size: 1845 bytes --]

diff --git a/gcc/ada/sem_ch3.adb b/gcc/ada/sem_ch3.adb
--- a/gcc/ada/sem_ch3.adb
+++ b/gcc/ada/sem_ch3.adb
@@ -17827,6 +17827,44 @@ package body Sem_Ch3 is
 
          T := Make_Defining_Identifier (Sloc (P), Nam);
 
+         --  If In_Spec_Expression, for example within a pre/postcondition,
+         --  provide enough information for use of the subtype without
+         --  depending on full analysis and freezing, which will happen when
+         --  building the correspondiing subprogram.
+
+         if In_Spec_Expression then
+            Analyze (Subtype_Mark (Obj_Def));
+
+            declare
+               Base_T : constant Entity_Id := Entity (Subtype_Mark (Obj_Def));
+               Decl   : constant Node_Id :=
+                 Make_Subtype_Declaration (Sloc (P),
+                   Defining_Identifier => T,
+                   Subtype_Indication  => Relocate_Node (Obj_Def));
+            begin
+               Set_Etype  (T, Base_T);
+               Set_Ekind  (T, Subtype_Kind (Ekind (Base_T)));
+               Set_Parent (T, Obj_Def);
+
+               if Ekind (T) = E_Array_Subtype then
+                  Set_First_Index (T, First_Index (Base_T));
+                  Set_Is_Constrained (T);
+
+               elsif Ekind (T) = E_Record_Subtype then
+                  Set_First_Entity (T, First_Entity (Base_T));
+                  Set_Has_Discriminants (T, Has_Discriminants (Base_T));
+                  Set_Is_Constrained (T);
+               end if;
+
+               Insert_Before (Related_Nod, Decl);
+            end;
+
+            return T;
+         end if;
+
+         --  When generating code, insert subtype declaration ahead of
+         --  declaration that generated it.
+
          Insert_Action (Obj_Def,
            Make_Subtype_Declaration (Sloc (P),
              Defining_Identifier => T,



                 reply	other threads:[~2021-05-05  8:20 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20210505082005.GA31251@adacore.com \
    --to=derodat@adacore.com \
    --cc=gcc-patches@gcc.gnu.org \
    --cc=schonberg@adacore.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).