Add hppa-openbsd target
[official-gcc.git] / gcc / ada / sem.ads
blobbc809524b9d6206436e5b1eeb9b700da14874408
1 ------------------------------------------------------------------------------
2 -- --
3 -- GNAT COMPILER COMPONENTS --
4 -- --
5 -- S E M --
6 -- --
7 -- S p e c --
8 -- --
9 -- --
10 -- Copyright (C) 1992-2001 Free Software Foundation, Inc. --
11 -- --
12 -- GNAT is free software; you can redistribute it and/or modify it under --
13 -- terms of the GNU General Public License as published by the Free Soft- --
14 -- ware Foundation; either version 2, or (at your option) any later ver- --
15 -- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
16 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
17 -- or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License --
18 -- for more details. You should have received a copy of the GNU General --
19 -- Public License distributed with GNAT; see file COPYING. If not, write --
20 -- to the Free Software Foundation, 59 Temple Place - Suite 330, Boston, --
21 -- MA 02111-1307, USA. --
22 -- --
23 -- GNAT was originally developed by the GNAT team at New York University. --
24 -- It is now maintained by Ada Core Technologies Inc (http://www.gnat.com). --
25 -- --
26 ------------------------------------------------------------------------------
28 --------------------------------------
29 -- Semantic Analysis: General Model --
30 --------------------------------------
32 -- Semantic processing involves 3 phases which are highly interwined
33 -- (ie mutually recursive):
35 -- Analysis implements the bulk of semantic analysis such as
36 -- name analysis and type resolution for declarations,
37 -- instructions and expressions. The main routine
38 -- driving this process is procedure Analyze given below.
39 -- This analysis phase is really a bottom up pass that is
40 -- achieved during the recursive traversal performed by the
41 -- Analyze_... procedures implemented in the sem_* packages.
42 -- For expressions this phase determines unambiguous types
43 -- and collects sets of possible types where the
44 -- interpretation is potentially ambiguous.
46 -- Resolution is carried out only for expressions to finish type
47 -- resolution that was initiated but not necessarily
48 -- completed during analysis (because of overloading
49 -- ambiguities). Specifically, after completing the bottom
50 -- up pass carried out during analysis for expressions, the
51 -- Resolve routine (see the spec of sem_res for more info)
52 -- is called to perform a top down resolution with
53 -- recursive calls to itself to resolve operands.
55 -- Expansion if we are not generating code this phase is a no-op.
56 -- otherwise this phase expands, ie transforms, original
57 -- declaration, expressions or instructions into simpler
58 -- structures that can be handled by the back-end. This
59 -- phase is also in charge of generating code which is
60 -- implicit in the original source (for instance for
61 -- default initializations, controlled types, etc.)
62 -- There are two separate instances where expansion is
63 -- invoked. For declarations and instructions, expansion is
64 -- invoked just after analysis since no resolution needs
65 -- to be performed. For expressions, expansion is done just
66 -- after resolution. In both cases expansion is done from the
67 -- bottom up just before the end of Analyze for instructions
68 -- and declarations or the call to Resolve for expressions.
69 -- The main routine driving expansion is Expand.
70 -- See the spec of Expander for more details.
72 -- To summarize, in normal code generation mode we recursively traverse the
73 -- abstract syntax tree top-down performing semantic analysis bottom
74 -- up. For instructions and declarations, before the call to the Analyze
75 -- routine completes we perform expansion since at that point we have all
76 -- semantic information needed. For expression nodes, after the call to
77 -- Analysis terminates we invoke the Resolve routine to transmit top-down
78 -- the type that was gathered by Analyze which will resolve possible
79 -- ambiguities in the expression. Just before the call to Resolve
80 -- terminates, the expression can be expanded since all the semantic
81 -- information is available at that point.
83 -- If we are not generating code then the expansion phase is a no-op.
85 -- When generating code there are a number of exceptions to the basic
86 -- Analysis-Resolution-Expansion model for expressions. The most prominent
87 -- examples are the handling of default expressions and aggregates.
89 -------------------------------------
90 -- Handling of Default Expressions --
91 -------------------------------------
93 -- The default expressions in component declarations and in procedure
94 -- specifications (but not the ones in object declarations) are quite
95 -- tricky to handle. The problem is that some processing is required
96 -- at the point where the expression appears:
98 -- visibility analysis (including user defined operators)
99 -- freezing of static expressions
101 -- but other processing must be deferred until the enclosing entity
102 -- (record or procedure specification) is frozen:
104 -- freezing of any other types in the expression
105 -- expansion
107 -- Expansion has to be deferred since you can't generate code for
108 -- expressions that refernce types that have not been frozen yet. As an
109 -- example, consider the following:
111 -- type x is delta 0.5 range -10.0 .. +10.0;
112 -- ...
113 -- type q is record
114 -- xx : x := y * z;
115 -- end record;
117 -- for x'small use 0.25
119 -- The expander is in charge of dealing with fixed-point, and of course
120 -- the small declaration, which is not too late, since the declaration of
121 -- type q does *not* freeze type x, definitely affects the expanded code.
123 -- Generally our model is to combine analysis resolution and expansion, but
124 -- this is the one case where this model falls down. Here is how we patch
125 -- it up without causing too much distortion to our basic model.
127 -- A switch (sede below) is set to indicate that we are in the initial
128 -- occurrence of a default expression. The analyzer is then called on this
129 -- expression with the switch set true. Analysis and resolution proceed
130 -- almost as usual, except that Freeze_Expression will not freeze
131 -- non-static expressions if this switch is set, and the call to Expand at
132 -- the end of resolution is skipped. This also skips the code that normally
133 -- sets the Analyzed flag to True). The result is that when we are done the
134 -- tree is still marked as unanalyzed, but all types for static expressions
135 -- are frozen as required, and all entities of variables have been
136 -- recorded. We then turn off the switch, and later on reanalyze the
137 -- expression with the switch off. The effect is that this second analysis
138 -- freezes the rest of the types as required, and generates code but
139 -- visibility analysis is not repeated since all the entities are marked.
141 -- The second analysis (the one that generates code) is in the context
142 -- where the code is required. For a record field default, this is in
143 -- the initialization procedure for the record and for a subprogram
144 -- default parameter, it is at the point the subprogram is frozen.
146 ------------------
147 -- Pre-Analysis --
148 ------------------
150 -- For certain kind of expressions, such as aggregates, we need to defer
151 -- expansion of the aggregate and its inner expressions after the whole
152 -- set of expressions appearing inside the aggregate have been analyzed.
153 -- Consider, for instance the following example:
155 -- (1 .. 100 => new Thing (Function_Call))
157 -- The normal Analysis-Resolution-Expansion mechanism where expansion
158 -- of the children is performed before expansion of the parent does not
159 -- work if the code generated for the children by the expander needs
160 -- to be evaluated repeatdly (for instance in the above aggregate
161 -- "new Thing (Function_Call)" needs to be called 100 times.)
162 -- The reason why this mecanism does not work is that, the expanded code
163 -- for the children is typically inserted above the parent and thus
164 -- when the father gets expanded no re-evaluation takes place. For instance
165 -- in the case of aggregates if "new Thing (Function_Call)" is expanded
166 -- before of the aggregate the expanded code will be placed outside
167 -- of the aggregate and when expanding the aggregate the loop from 1 to 100
168 -- will not surround the expanded code for "new Thing (Function_Call)".
170 -- To remedy this situation we introduce a new flag which signals whether
171 -- we want a full analysis (ie expansion is enabled) or a pre-analysis
172 -- which performs Analysis and Resolution but no expansion.
174 -- After the complete pre-analysis of an expression has been carried out
175 -- we can transform the expression and then carry out the full
176 -- Analyze-Resolve-Expand cycle on the transformed expression top-down
177 -- so that the expansion of inner expressions happens inside the newly
178 -- generated node for the parent expression.
180 -- Note that the difference between processing of default expressions and
181 -- pre-analysis of other expressions is that we do carry out freezing in
182 -- the latter but not in the former (except for static scalar expressions).
183 -- The routine that performs pre-analysis is called Pre_Analyze_And_Resolve
184 -- and is in Sem_Res.
186 with Alloc;
187 with Einfo; use Einfo;
188 with Opt; use Opt;
189 with Snames; use Snames;
190 with Table;
191 with Types; use Types;
193 package Sem is
195 New_Nodes_OK : Int := 1;
196 -- Temporary flag for use in checking out HLO. Set non-zero if it is
197 -- OK to generate new nodes.
199 -----------------------------
200 -- Semantic Analysis Flags --
201 -----------------------------
203 Full_Analysis : Boolean := True;
204 -- Switch to indicate whether we are doing a full analysis or a
205 -- pre-analysis. In normal analysis mode (Analysis-Expansion for
206 -- instructions or declarations) or (Analysis-Resolution-Expansion for
207 -- expressions) this flag is set. Note that if we are not generating
208 -- code the expansion phase merely sets the Analyzed flag to True in
209 -- this case. If we are in Pre-Analysis mode (see above) this flag is
210 -- set to False then the expansion phase is skipped.
211 -- When this flag is False the flag Expander_Active is also False
212 -- (the Expander_Activer flag defined in the spec of package Expander
213 -- tells you whether expansion is currently enabled).
214 -- You should really regard this as a read only flag.
216 In_Default_Expression : Boolean := False;
217 -- Switch to indicate that we are in a default expression, as described
218 -- above. Note that this must be recursively saved on a Semantics call
219 -- since it is possible for the analysis of an expression to result in
220 -- a recursive call (e.g. to get the entity for System.Address as part
221 -- of the processing of an Address attribute reference).
222 -- When this switch is True then Full_Analysis above must be False.
223 -- You should really regard this as a read only flag.
225 In_Inlined_Body : Boolean := False;
226 -- Switch to indicate that we are analyzing and resolving an inlined
227 -- body. Type checking is disabled in this context, because types are
228 -- known to be compatible. This avoids problems with private types whose
229 -- full view is derived from private types.
231 Inside_A_Generic : Boolean := False;
232 -- This flag is set if we are processing a generic specification,
233 -- generic definition, or generic body. When this flag is True the
234 -- Expander_Active flag is False to disable any code expansion (see
235 -- package Expander). Only the generic processing can modify the
236 -- status of this flag, any other client should regard it as read-only.
238 Unloaded_Subunits : Boolean := False;
239 -- This flag is set True if we have subunits that are not loaded. This
240 -- occurs when the main unit is a subunit, and contains lower level
241 -- subunits that are not loaded. We use this flag to suppress warnings
242 -- about unused variables, since these warnings are unreliable in this
243 -- case. We could perhaps do a more accurate job and retain some of the
244 -- warnings, but it is quite a tricky job. See test 4323-002.
246 -----------------
247 -- Scope Stack --
248 -----------------
250 Scope_Suppress : Suppress_Record := Suppress_Options;
251 -- This record contains the current scope based settings of the suppress
252 -- switches. It is initialized from the options as shown, and then modified
253 -- by pragma Suppress. On entry to each scope, the current setting is saved
254 -- the scope stack, and then restored on exit from the scope.
256 -- The scope stack holds all entries of the scope table. As in the parser,
257 -- we use Last as the stack pointer, so that we can always find the scope
258 -- that is currently open in Scope_Stack.Table (Scope_Stack.Last). The
259 -- oldest entry, at Scope_Stack (0) is Standard. The entries in the table
260 -- include the entity for the referenced scope, together with information
261 -- used to restore the proper setting of check suppressions on scope exit.
263 -- There are two kinds of suppress checks, scope based suppress checks
264 -- (from initial command line arguments, or from Suppress pragmas not
265 -- including an entity name). The scope based suppress checks are recorded
266 -- in the Sem.Supress variable, and all that is necessary is to save the
267 -- state of this variable on scope entry, and restore it on scope exit.
269 -- The other kind of suppress check is entity based suppress checks, from
270 -- Suppress pragmas giving an Entity_Id. These checks are reflected by the
271 -- appropriate bit being set in the corresponding entity, and restoring the
272 -- setting of these bits is a little trickier. In particular a given pragma
273 -- Suppress may or may not affect the current state. If it sets a check for
274 -- an entity that is already checked, then it is important that this check
275 -- not be restored on scope exit. The situation is made more complicated
276 -- by the fact that a given suppress pragma can specify multiple entities
277 -- (in the overloaded case), and multiple checks (by using All_Checks), so
278 -- that it may be partially effective. On exit only checks that were in
279 -- fact effective must be removed. Logically we could do this by saving
280 -- the entire state of the entity flags on scope entry and restoring them
281 -- on scope exit, but that would be ludicrous, so what we do instead is to
282 -- maintain the following differential structure that shows what checks
283 -- were installed for the current scope.
285 -- Note: Suppress pragmas that specify entities defined in a package
286 -- spec do not make entries in this table, since such checks suppress
287 -- requests are valid for the entire life of the entity.
289 type Entity_Check_Suppress_Record is record
290 Entity : Entity_Id;
291 -- Entity to which the check applies
293 Check : Check_Id;
294 -- Check which is set (note this cannot be All_Checks, if the All_Checks
295 -- case, a sequence of eentries appears for the individual checks.
296 end record;
298 -- Entity_Suppress is a stack, to which new entries are added as they
299 -- are processed (see pragma Suppress circuit in Sem_Prag). The scope
300 -- stack entry simply saves the stack pointer on entry, and restores
301 -- it on exit by reversing the checks one by one.
303 package Entity_Suppress is new Table.Table (
304 Table_Component_Type => Entity_Check_Suppress_Record,
305 Table_Index_Type => Int,
306 Table_Low_Bound => 0,
307 Table_Initial => Alloc.Entity_Suppress_Initial,
308 Table_Increment => Alloc.Entity_Suppress_Increment,
309 Table_Name => "Entity_Suppress");
311 -- Here is the scope stack itself
313 type Scope_Stack_Entry is record
314 Entity : Entity_Id;
315 -- Entity representing the scope
317 Last_Subprogram_Name : String_Ptr;
318 -- Pointer to name of last subprogram body in this scope. Used for
319 -- testing proper alpha ordering of subprogram bodies in scope.
321 Save_Scope_Suppress : Suppress_Record;
322 -- Save contents of Scope_Suppress on entry
324 Save_Entity_Suppress : Int;
325 -- Save contents of Entity_Suppress.Last on entry
327 Is_Transient : Boolean;
328 -- Marks Transient Scopes (See Exp_Ch7 body for details)
330 Previous_Visibility : Boolean;
331 -- Used when installing the parent (s) of the current compilation
332 -- unit. The parent may already be visible because of an ongoing
333 -- compilation, and the proper visibility must be restored on exit.
335 Node_To_Be_Wrapped : Node_Id;
336 -- Only used in transient scopes. Records the node which will
337 -- be wrapped by the transient block.
339 Actions_To_Be_Wrapped_Before : List_Id;
340 Actions_To_Be_Wrapped_After : List_Id;
341 -- Actions that have to be inserted at the start or at the end of a
342 -- transient block. Used to temporarily hold these actions until the
343 -- block is created, at which time the actions are moved to the
344 -- block.
346 Pending_Freeze_Actions : List_Id;
347 -- Used to collect freeze entity nodes and associated actions that
348 -- are generated in a inner context but need to be analyzed outside,
349 -- such as records and initialization procedures. On exit from the
350 -- scope, this list of actions is inserted before the scope construct
351 -- and analyzed to generate the corresponding freeze processing and
352 -- elaboration of other associated actions.
354 First_Use_Clause : Node_Id;
355 -- Head of list of Use_Clauses in current scope. The list is built
356 -- when the declarations in the scope are processed. The list is
357 -- traversed on scope exit to undo the effect of the use clauses.
359 Component_Alignment_Default : Component_Alignment_Kind;
360 -- Component alignment to be applied to any record or array types
361 -- that are declared for which a specific component alignment pragma
362 -- does not set the alignment.
364 Is_Active_Stack_Base : Boolean;
365 -- Set to true only when entering the scope for Standard_Standard from
366 -- from within procedure Semantics. Indicates the base of the current
367 -- active set of scopes. Needed by In_Open_Scopes to handle cases
368 -- where Standard_Standard can be pushed in the middle of the active
369 -- set of scopes (occurs for instantiations of generic child units).
370 end record;
372 package Scope_Stack is new Table.Table (
373 Table_Component_Type => Scope_Stack_Entry,
374 Table_Index_Type => Int,
375 Table_Low_Bound => 0,
376 Table_Initial => Alloc.Scope_Stack_Initial,
377 Table_Increment => Alloc.Scope_Stack_Increment,
378 Table_Name => "Sem.Scope_Stack");
380 function Get_Scope_Suppress (C : Check_Id) return Boolean;
381 -- Get suppress status of check C for the current scope
383 procedure Set_Scope_Suppress (C : Check_Id; B : Boolean);
384 -- Set suppress status of check C for the current scope
386 -----------------
387 -- Subprograms --
388 -----------------
390 procedure Initialize;
391 -- Initialize internal tables
393 procedure Lock;
394 -- Lock internal tables before calling back end
396 procedure Semantics (Comp_Unit : Node_Id);
397 -- This procedure is called to perform semantic analysis on the specified
398 -- node which is the N_Compilation_Unit node for the unit.
400 procedure Analyze (N : Node_Id);
401 procedure Analyze (N : Node_Id; Suppress : Check_Id);
402 -- This is the recursive procedure which is applied to individual nodes
403 -- of the tree, starting at the top level node (compilation unit node)
404 -- and then moving down the tree in a top down traversal. It calls
405 -- individual routines with names Analyze_xxx to analyze node xxx. Each
406 -- of these routines is responsible for calling Analyze on the components
407 -- of the subtree.
409 -- Note: In the case of expression components (nodes whose Nkind is in
410 -- N_Subexpr), the call to Analyze does not complete the semantic analysis
411 -- of the node, since the type resolution cannot be completed until the
412 -- complete context is analyzed. The completion of the type analysis occurs
413 -- in the corresponding Resolve routine (see Sem_Res).
415 -- Note: for integer and real literals, the analyzer sets the flag to
416 -- indicate that the result is a static expression. If the expander
417 -- generates a literal that does NOT correspond to a static expression,
418 -- e.g. by folding an expression whose value is known at compile-time,
419 -- but is not technically static, then the caller should reset the
420 -- Is_Static_Expression flag after analyzing but before resolving.
422 -- If the Suppress argument is present, then the analysis is done
423 -- with the specified check suppressed (can be All_Checks to suppress
424 -- all checks).
426 procedure Analyze_List (L : List_Id);
427 procedure Analyze_List (L : List_Id; Suppress : Check_Id);
428 -- Analyzes each element of a list. If the Suppress argument is present,
429 -- then the analysis is done with the specified check suppressed (can
430 -- be All_Checks to suppress all checks).
432 procedure Insert_List_After_And_Analyze
433 (N : Node_Id; L : List_Id);
434 procedure Insert_List_After_And_Analyze
435 (N : Node_Id; L : List_Id; Suppress : Check_Id);
436 -- Inserts list L after node N using Nlists.Insert_List_After, and then,
437 -- after this insertion is complete, analyzes all the nodes in the list,
438 -- including any additional nodes generated by this analysis. If the list
439 -- is empty or be No_List, the call has no effect. If the Suppress
440 -- argument is present, then the analysis is done with the specified
441 -- check suppressed (can be All_Checks to suppress all checks).
443 procedure Insert_List_Before_And_Analyze
444 (N : Node_Id; L : List_Id);
445 procedure Insert_List_Before_And_Analyze
446 (N : Node_Id; L : List_Id; Suppress : Check_Id);
447 -- Inserts list L before node N using Nlists.Insert_List_Before, and then,
448 -- after this insertion is complete, analyzes all the nodes in the list,
449 -- including any additional nodes generated by this analysis. If the list
450 -- is empty or be No_List, the call has no effect. If the Suppress
451 -- argument is present, then the analysis is done with the specified
452 -- check suppressed (can be All_Checks to suppress all checks).
454 procedure Insert_After_And_Analyze
455 (N : Node_Id; M : Node_Id);
456 procedure Insert_After_And_Analyze
457 (N : Node_Id; M : Node_Id; Suppress : Check_Id);
458 -- Inserts node M after node N and then after the insertion is complete,
459 -- analyzes the inserted node and all nodes that are generated by
460 -- this analysis. If the node is empty, the call has no effect. If the
461 -- Suppress argument is present, then the analysis is done with the
462 -- specified check suppressed (can be All_Checks to suppress all checks).
464 procedure Insert_Before_And_Analyze
465 (N : Node_Id; M : Node_Id);
466 procedure Insert_Before_And_Analyze
467 (N : Node_Id; M : Node_Id; Suppress : Check_Id);
468 -- Inserts node M before node N and then after the insertion is complete,
469 -- analyzes the inserted node and all nodes that could be generated by
470 -- this analysis. If the node is empty, the call has no effect. If the
471 -- Suppress argument is present, then the analysis is done with the
472 -- specified check suppressed (can be All_Checks to suppress all checks).
474 function External_Ref_In_Generic (E : Entity_Id) return Boolean;
475 -- Return True if we are in the context of a generic and E is
476 -- external (more global) to it.
478 procedure Enter_Generic_Scope (S : Entity_Id);
479 -- Shall be called each time a Generic subprogram or package scope is
480 -- entered. S is the entity of the scope.
481 -- ??? At the moment, only called for package specs because this mechanism
482 -- is only used for avoiding freezing of external references in generics
483 -- and this can only be an issue if the outer generic scope is a package
484 -- spec (otherwise all external entities are already frozen)
486 procedure Exit_Generic_Scope (S : Entity_Id);
487 -- Shall be called each time a Generic subprogram or package scope is
488 -- exited. S is the entity of the scope.
489 -- ??? At the moment, only called for package specs exit.
491 end Sem;