[Ada] Finding proper scope when inside entry body

Message ID 20180111090519.GA102950@adacore.com
State New
Headers show
Series
  • [Ada] Finding proper scope when inside entry body
Related show

Commit Message

Pierre-Marie de Rodat Jan. 11, 2018, 9:05 a.m.
This patch modifies routine Find_Enclosing_Scope which obtains the scope of an
arbitrary node to return the unique defining entity of an enclosing body. This
automatically takes care of the following corner cases:

   * The body is a subprogram body which does not complete a previous
     declaration. In this case the proper scope is the entity of the
     body.

   * The body is an entry body. Due to a limitation in the AST, the
     entry body does not store its correcponsing spec, but utilizes a
     roundabout way of obtaining it. Regardless of the limitation, the
     proper scope is the entity of the entry declaration.

The issue was discovered during the development of the GNATprove tool and
is not visible to end users. No simple test is available because this would
require a debug session.

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

2018-01-11  Hristian Kirtchev  <kirtchev@adacore.com>

gcc/ada/

	* sem_util.adb (Find_Enclosing_Scope): Return the unique defining
	entity when the enclosing construct is a body.

Patch

--- gcc/ada/sem_util.adb
+++ gcc/ada/sem_util.adb
@@ -7847,8 +7847,7 @@  package body Sem_Util is
    --------------------------
 
    function Find_Enclosing_Scope (N : Node_Id) return Entity_Id is
-      Par     : Node_Id;
-      Spec_Id : Entity_Id;
+      Par : Node_Id;
 
    begin
       --  Examine the parent chain looking for a construct which defines a
@@ -7877,7 +7876,8 @@  package body Sem_Util is
                return Defining_Entity (Par);
 
             --  The construct denotes a body, the proper scope is the entity of
-            --  the corresponding spec.
+            --  the corresponding spec or that of the body if the body does not
+            --  complete a previous declaration.
 
             when N_Entry_Body
                | N_Package_Body
@@ -7885,22 +7885,7 @@  package body Sem_Util is
                | N_Subprogram_Body
                | N_Task_Body
             =>
-               Spec_Id := Corresponding_Spec (Par);
-
-               --  The defining entity of a stand-alone subprogram body defines
-               --  a scope.
-
-               if Nkind (Par) = N_Subprogram_Body and then No (Spec_Id) then
-                  return Defining_Entity (Par);
-
-               --  Otherwise there should be corresponding spec which defines a
-               --  scope.
-
-               else
-                  pragma Assert (Present (Spec_Id));
-
-                  return Spec_Id;
-               end if;
+               return Unique_Defining_Entity (Par);
 
             --  Special cases