1------------------------------------------------------------------------------
2--                                                                          --
3--                         GNAT COMPILER COMPONENTS                         --
4--                                                                          --
5--                             S E M _ E L A B                              --
6--                                                                          --
7--                                 S p e c                                  --
8--                                                                          --
9--          Copyright (C) 1997-2015, Free Software Foundation, Inc.         --
10--                                                                          --
11-- GNAT is free software;  you can  redistribute it  and/or modify it under --
12-- terms of the  GNU General Public License as published  by the Free Soft- --
13-- ware  Foundation;  either version 3,  or (at your option) any later ver- --
14-- sion.  GNAT is distributed in the hope that it will be useful, but WITH- --
15-- OUT ANY WARRANTY;  without even the  implied warranty of MERCHANTABILITY --
16-- or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License --
17-- for  more details.  You should have  received  a copy of the GNU General --
18-- Public License  distributed with GNAT; see file COPYING3.  If not, go to --
19-- http://www.gnu.org/licenses for a complete copy of the license.          --
20--                                                                          --
21-- GNAT was originally developed  by the GNAT team at  New York University. --
22-- Extensive contributions were provided by Ada Core Technologies Inc.      --
23--                                                                          --
24------------------------------------------------------------------------------
25
26--  This package contains the routines used to deal with issuing warnings
27--  for cases of calls that may require warnings about possible access
28--  before elaboration.
29
30with Types; use Types;
31
32package Sem_Elab is
33
34   -----------------------------
35   -- Description of Approach --
36   -----------------------------
37
38   --  Every non-static call that is encountered by Sem_Res results in a call
39   --  to Check_Elab_Call, with N being the call node, and Outer set to its
40   --  default value of True. In addition X'Access is treated like a call
41   --  for the access-to-procedure case, and in SPARK mode only we also
42   --  check variable references.
43
44   --  The goal of Check_Elab_Call is to determine whether or not the reference
45   --  in question can generate an access before elaboration error (raising
46   --  Program_Error) either by directly calling a subprogram whose body
47   --  has not yet been elaborated, or indirectly, by calling a subprogram
48   --  whose body has been elaborated, but which contains a call to such a
49   --  subprogram.
50
51   --  In addition, in SPARK mode, we are checking for a variable reference in
52   --  another package, which requires an explicit Elaborate_All pragma.
53
54   --  The only references that we need to look at the outer level are
55   --  references that occur in elaboration code. There are two cases. The
56   --  reference can be at the outer level of elaboration code, or it can
57   --  be within another unit, e.g. the elaboration code of a subprogram.
58
59   --  In the case of an elaboration call at the outer level, we must trace
60   --  all calls to outer level routines either within the current unit or to
61   --  other units that are with'ed. For calls within the current unit, we can
62   --  determine if the body has been elaborated or not, and if it has not,
63   --  then a warning is generated.
64
65   --  Note that there are two subcases. If the original call directly calls a
66   --  subprogram whose body has not been elaborated, then we know that an ABE
67   --  will take place, and we replace the call by a raise of Program_Error.
68   --  If the call is indirect, then we don't know that the PE will be raised,
69   --  since the call might be guarded by a conditional. In this case we set
70   --  Do_Elab_Check on the call so that a dynamic check is generated, and
71   --  output a warning.
72
73   --  For calls to a subprogram in a with'ed unit or a 'Access or variable
74   --  refernece (SPARK mode case), we require that a pragma Elaborate_All
75   --  or pragma Elaborate be present, or that the referenced unit have a
76   --  pragma Preelaborate, pragma Pure, or pragma Elaborate_Body. If none
77   --  of these conditions is met, then a warning is generated that a pragma
78   --  Elaborate_All may be needed (error in the SPARK case), or an implicit
79   --  pragma is generated.
80
81   --  For the case of an elaboration call at some inner level, we are
82   --  interested in tracing only calls to subprograms at the same level,
83   --  i.e. those that can be called during elaboration. Any calls to
84   --  outer level routines cannot cause ABE's as a result of the original
85   --  call (there might be an outer level call to the subprogram from
86   --  outside that causes the ABE, but that gets analyzed separately).
87
88   --  Note that we never trace calls to inner level subprograms, since
89   --  these cannot result in ABE's unless there is an elaboration problem
90   --  at a lower level, which will be separately detected.
91
92   --  Note on pragma Elaborate. The checking here assumes that a pragma
93   --  Elaborate on a with'ed unit guarantees that subprograms within the
94   --  unit can be called without causing an ABE. This is not in fact the
95   --  case since pragma Elaborate does not guarantee the transitive
96   --  coverage guaranteed by Elaborate_All. However, we decide to trust
97   --  the user in this case.
98
99   --------------------------------------
100   -- Instantiation Elaboration Errors --
101   --------------------------------------
102
103   --  A special case arises when an instantiation appears in a context
104   --  that is known to be before the body is elaborated, e.g.
105
106   --       generic package x is ...
107   --       ...
108   --       package xx is new x;
109   --       ...
110   --       package body x is ...
111
112   --  In this situation it is certain that an elaboration error will
113   --  occur, and an unconditional raise Program_Error statement is
114   --  inserted before the instantiation, and a warning generated.
115
116   --  The problem is that in this case we have no place to put the
117   --  body of the instantiation. We can't put it in the normal place,
118   --  because it is too early, and will cause errors to occur as a
119   --  result of referencing entities before they are declared.
120
121   --  Our approach in this case is simply to avoid creating the body
122   --  of the instantiation in such a case. The instantiation spec is
123   --  modified to include dummy bodies for all subprograms, so that
124   --  the resulting code does not contain subprogram specs with no
125   --  corresponding bodies.
126
127   procedure Check_Elab_Call
128     (N            : Node_Id;
129      Outer_Scope  : Entity_Id := Empty;
130      In_Init_Proc : Boolean   := False);
131   --  Check a call for possible elaboration problems. The node N is either an
132   --  N_Function_Call or N_Procedure_Call_Statement node or an access
133   --  attribute reference whose prefix is a subprogram.
134   --
135   --  If SPARK_Mode is On, then N can also be a variablr reference, since
136   --  SPARK requires the use of Elaborate_All for references to variables
137   --  in other packages.
138
139   --  The Outer_Scope argument indicates whether this is an outer level
140   --  call from Sem_Res (Outer_Scope set to Empty), or an internal recursive
141   --  call (Outer_Scope set to entity of outermost call, see body). The flag
142   --  In_Init_Proc should be set whenever the current context is a type
143   --  init proc.
144
145   --  Note: this might better be called Check_Elab_Reference (to recognize
146   --  the SPARK case), but we prefer to keep the original name, since this
147   --  is primarily used for checking for calls that could generate an ABE).
148
149   procedure Check_Elab_Calls;
150   --  Not all the processing for Check_Elab_Call can be done at the time
151   --  of calls to Check_Elab_Call. This is because for internal calls, we
152   --  need to wait to complete the check until all generic bodies have been
153   --  instantiated. The Check_Elab_Calls procedure cleans up these waiting
154   --  checks. It is called once after the completion of instantiation.
155
156   procedure Check_Elab_Assign (N : Node_Id);
157   --  N is either the left side of an assignment, or a procedure argument for
158   --  a mode OUT or IN OUT formal. This procedure checks for a possible case
159   --  of access to an entity from elaboration code before the entity has been
160   --  initialized, and issues appropriate warnings.
161
162   procedure Check_Elab_Instantiation
163     (N           : Node_Id;
164      Outer_Scope : Entity_Id := Empty);
165   --  Check an instantiation for possible elaboration problems. N is an
166   --  instantiation node (N_Package_Instantiation, N_Function_Instantiation,
167   --  or N_Procedure_Instantiation), and Outer_Scope indicates if this is
168   --  an outer level call from Sem_Ch12 (Outer_Scope set to Empty), or an
169   --  internal recursive call (Outer_Scope set to scope of outermost call,
170   --  see body for further details). The returned value is relevant only
171   --  for an outer level call, and is set to False if an elaboration error
172   --  is bound to occur on the instantiation, and True otherwise. This is
173   --  used by the caller to signal that the body of the instance should
174   --  not be generated (see detailed description in body).
175
176   procedure Check_Task_Activation (N : Node_Id);
177   --  Tt the point at which tasks are activated in a package body, check
178   --  that the bodies of the tasks are elaborated.
179
180end Sem_Elab;
181