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-2012, 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
39   --  a call to Check_Elab_Call, with N being the call node, and Outer
40   --  set to its default value of True.
41
42   --  The goal of Check_Elab_Call is to determine whether or not the
43   --  call in question can generate an access before elaboration
44   --  error (raising Program_Error) either by directly calling a
45   --  subprogram whose body has not yet been elaborated, or indirectly,
46   --  by calling a subprogram whose body has been elaborated, but which
47   --  contains a call to such a subprogram.
48
49   --  The only calls that we need to look at at the outer level are
50   --  calls that occur in elaboration code. There are two cases. The
51   --  call can be at the outer level of elaboration code, or it can
52   --  be within another unit, e.g. the elaboration code of a subprogram.
53
54   --  In the case of an elaboration call at the outer level, we must
55   --  trace all calls to outer level routines either within the current
56   --  unit or to other units that are with'ed. For calls within the
57   --  current unit, we can determine if the body has been elaborated
58   --  or not, and if it has not, then a warning is generated.
59
60   --  Note that there are two subcases. If the original call directly
61   --  calls a subprogram whose body has not been elaborated, then we
62   --  know that an ABE will take place, and we replace the call by
63   --  a raise of Program_Error. If the call is indirect, then we don't
64   --  know that the PE will be raised, since the call might be guarded
65   --  by a conditional. In this case we set Do_Elab_Check on the call
66   --  so that a dynamic check is generated, and output a warning.
67
68   --  For calls to a subprogram in a with'ed unit, we require that
69   --  a pragma Elaborate_All or pragma Elaborate be present, or that
70   --  the referenced unit have a pragma Preelaborate, pragma Pure, or
71   --  pragma Elaborate_Body. If none of these conditions is met, then
72   --  a warning is generated that a pragma Elaborate_All may be needed.
73
74   --  For the case of an elaboration call at some inner level, we are
75   --  interested in tracing only calls to subprograms at the same level,
76   --  i.e. those that can be called during elaboration. Any calls to
77   --  outer level routines cannot cause ABE's as a result of the original
78   --  call (there might be an outer level call to the subprogram from
79   --  outside that causes the ABE, but that gets analyzed separately).
80
81   --  Note that we never trace calls to inner level subprograms, since
82   --  these cannot result in ABE's unless there is an elaboration problem
83   --  at a lower level, which will be separately detected.
84
85   --  Note on pragma Elaborate. The checking here assumes that a pragma
86   --  Elaborate on a with'ed unit guarantees that subprograms within the
87   --  unit can be called without causing an ABE. This is not in fact the
88   --  case since pragma Elaborate does not guarantee the transitive
89   --  coverage guaranteed by Elaborate_All. However, we leave this issue
90   --  up to the binder, which has generates warnings if there are possible
91   --  problems in the use of pragma Elaborate.
92
93   --------------------------------------
94   -- Instantiation Elaboration Errors --
95   --------------------------------------
96
97   --  A special case arises when an instantiation appears in a context
98   --  that is known to be before the body is elaborated, e.g.
99
100   --       generic package x is ...
101   --       ...
102   --       package xx is new x;
103   --       ...
104   --       package body x is ...
105
106   --  In this situation it is certain that an elaboration error will
107   --  occur, and an unconditional raise Program_Error statement is
108   --  inserted before the instantiation, and a warning generated.
109
110   --  The problem is that in this case we have no place to put the
111   --  body of the instantiation. We can't put it in the normal place,
112   --  because it is too early, and will cause errors to occur as a
113   --  result of referencing entities before they are declared.
114
115   --  Our approach in this case is simply to avoid creating the body
116   --  of the instantiation in such a case. The instantiation spec is
117   --  modified to include dummy bodies for all subprograms, so that
118   --  the resulting code does not contain subprogram specs with no
119   --  corresponding bodies.
120
121   procedure Check_Elab_Call
122     (N            : Node_Id;
123      Outer_Scope  : Entity_Id := Empty;
124      In_Init_Proc : Boolean   := False);
125   --  Check a call for possible elaboration problems. The node N is either an
126   --  N_Function_Call or N_Procedure_Call_Statement node or an access
127   --  attribute reference whose prefix is a subprogram. The Outer_Scope
128   --  argument indicates whether this is an outer level call from Sem_Res
129   --  (Outer_Scope set to Empty), or an internal recursive call (Outer_Scope
130   --  set to entity of outermost call, see body). Flag In_Init_Proc should be
131   --  set whenever the current context is a type init proc.
132
133   procedure Check_Elab_Calls;
134   --  Not all the processing for Check_Elab_Call can be done at the time
135   --  of calls to Check_Elab_Call. This is because for internal calls, we
136   --  need to wait to complete the check until all generic bodies have been
137   --  instantiated. The Check_Elab_Calls procedure cleans up these waiting
138   --  checks. It is called once after the completion of instantiation.
139
140   procedure Check_Elab_Assign (N : Node_Id);
141   --  N is either the left side of an assignment, or a procedure argument for
142   --  a mode OUT or IN OUT formal. This procedure checks for a possible case
143   --  of access to an entity from elaboration code before the entity has been
144   --  initialized, and issues appropriate warnings.
145
146   procedure Check_Elab_Instantiation
147     (N           : Node_Id;
148      Outer_Scope : Entity_Id := Empty);
149   --  Check an instantiation for possible elaboration problems. N is an
150   --  instantiation node (N_Package_Instantiation, N_Function_Instantiation,
151   --  or N_Procedure_Instantiation), and Outer_Scope indicates if this is
152   --  an outer level call from Sem_Ch12 (Outer_Scope set to Empty), or an
153   --  internal recursive call (Outer_Scope set to scope of outermost call,
154   --  see body for further details). The returned value is relevant only
155   --  for an outer level call, and is set to False if an elaboration error
156   --  is bound to occur on the instantiation, and True otherwise. This is
157   --  used by the caller to signal that the body of the instance should
158   --  not be generated (see detailed description in body).
159
160   procedure Check_Task_Activation (N : Node_Id);
161   --  at the point at which tasks are activated in a package body, check
162   --  that the bodies of the tasks are elaborated.
163
164end Sem_Elab;
165