2008-05-30 Vladimir Makarov <vmakarov@redhat.com>
[official-gcc.git] / gcc / ada / sem_elab.ads
blob7b85b6f886fcac2d0c84c0221b965085bceac9c6
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-2008, 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 ------------------------------------------------------------------------------
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.
30 with Types; use Types;
32 package Sem_Elab is
34 -----------------------------
35 -- Description of Approach --
36 -----------------------------
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.
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.
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.
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.
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.
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.
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).
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.
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.
93 --------------------------------------
94 -- Instantiation Elaboration Errors --
95 --------------------------------------
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.
100 -- generic package x is ...
101 -- ...
102 -- package xx is new x;
103 -- ...
104 -- package body x is ...
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.
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.
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.
121 procedure Check_Elab_Call (N : Node_Id; Outer_Scope : Entity_Id := Empty);
122 -- Check a call for possible elaboration problems. The node N is either
123 -- an N_Function_Call or N_Procedure_Call_Statement node. The Outer_Scope
124 -- argument indicates whether this is an outer level call from Sem_Res
125 -- (Outer_Scope set to Empty), or an internal recursive call (Outer_Scope
126 -- set to entity of outermost call, see body).
128 procedure Check_Elab_Calls;
129 -- Not all the processing for Check_Elab_Call can be done at the time
130 -- of calls to Check_Elab_Call. This is because for internal calls, we
131 -- need to wait to complete the check until all generic bodies have been
132 -- instantiated. The Check_Elab_Calls procedure cleans up these waiting
133 -- checks. It is called once after the completion of instantiation.
135 procedure Check_Elab_Assign (N : Node_Id);
136 -- N is either the left side of an assignment, or a procedure argument for
137 -- a mode OUT or IN OUT formal. This procedure checks for a possible case
138 -- of access to an entity from elaboration code before the entity has been
139 -- initialized, and issues appropriate warnings.
141 procedure Check_Elab_Instantiation
142 (N : Node_Id;
143 Outer_Scope : Entity_Id := Empty);
144 -- Check an instantiation for possible elaboration problems. N is an
145 -- instantiation node (N_Package_Instantiation, N_Function_Instantiation,
146 -- or N_Procedure_Instantiation), and Outer_Scope indicates if this is
147 -- an outer level call from Sem_Ch12 (Outer_Scope set to Empty), or an
148 -- internal recursive call (Outer_Scope set to scope of outermost call,
149 -- see body for further details). The returned value is relevant only
150 -- for an outer level call, and is set to False if an elaboration error
151 -- is bound to occur on the instantiation, and True otherwise. This is
152 -- used by the caller to signal that the body of the instance should
153 -- not be generated (see detailed description in body).
155 procedure Check_Task_Activation (N : Node_Id);
156 -- at the point at which tasks are activated in a package body, check
157 -- that the bodies of the tasks are elaborated.
159 end Sem_Elab;