* gimplify.c (find_single_pointer_decl_1): New static function.
[official-gcc.git] / gcc / ada / sem_ch12.adb
blob05f89f65e204b54a69e103d32cfc25abf03809b5
1 ------------------------------------------------------------------------------
2 -- --
3 -- GNAT COMPILER COMPONENTS --
4 -- --
5 -- S E M _ C H 1 2 --
6 -- --
7 -- B o d y --
8 -- --
9 -- Copyright (C) 1992-2005 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 2, 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 COPYING. If not, write --
19 -- to the Free Software Foundation, 51 Franklin Street, Fifth Floor, --
20 -- Boston, MA 02110-1301, USA. --
21 -- --
22 -- GNAT was originally developed by the GNAT team at New York University. --
23 -- Extensive contributions were provided by Ada Core Technologies Inc. --
24 -- --
25 ------------------------------------------------------------------------------
27 with Atree; use Atree;
28 with Einfo; use Einfo;
29 with Elists; use Elists;
30 with Errout; use Errout;
31 with Expander; use Expander;
32 with Fname; use Fname;
33 with Fname.UF; use Fname.UF;
34 with Freeze; use Freeze;
35 with Hostparm;
36 with Lib; use Lib;
37 with Lib.Load; use Lib.Load;
38 with Lib.Xref; use Lib.Xref;
39 with Nlists; use Nlists;
40 with Nmake; use Nmake;
41 with Opt; use Opt;
42 with Rident; use Rident;
43 with Restrict; use Restrict;
44 with Rtsfind; use Rtsfind;
45 with Sem; use Sem;
46 with Sem_Cat; use Sem_Cat;
47 with Sem_Ch3; use Sem_Ch3;
48 with Sem_Ch6; use Sem_Ch6;
49 with Sem_Ch7; use Sem_Ch7;
50 with Sem_Ch8; use Sem_Ch8;
51 with Sem_Ch10; use Sem_Ch10;
52 with Sem_Ch13; use Sem_Ch13;
53 with Sem_Disp; use Sem_Disp;
54 with Sem_Elab; use Sem_Elab;
55 with Sem_Elim; use Sem_Elim;
56 with Sem_Eval; use Sem_Eval;
57 with Sem_Res; use Sem_Res;
58 with Sem_Type; use Sem_Type;
59 with Sem_Util; use Sem_Util;
60 with Sem_Warn; use Sem_Warn;
61 with Stand; use Stand;
62 with Sinfo; use Sinfo;
63 with Sinfo.CN; use Sinfo.CN;
64 with Sinput; use Sinput;
65 with Sinput.L; use Sinput.L;
66 with Snames; use Snames;
67 with Stringt; use Stringt;
68 with Uname; use Uname;
69 with Table;
70 with Tbuild; use Tbuild;
71 with Uintp; use Uintp;
72 with Urealp; use Urealp;
74 with GNAT.HTable;
76 package body Sem_Ch12 is
78 ----------------------------------------------------------
79 -- Implementation of Generic Analysis and Instantiation --
80 -----------------------------------------------------------
82 -- GNAT implements generics by macro expansion. No attempt is made to
83 -- share generic instantiations (for now). Analysis of a generic definition
84 -- does not perform any expansion action, but the expander must be called
85 -- on the tree for each instantiation, because the expansion may of course
86 -- depend on the generic actuals. All of this is best achieved as follows:
88 -- a) Semantic analysis of a generic unit is performed on a copy of the
89 -- tree for the generic unit. All tree modifications that follow analysis
90 -- do not affect the original tree. Links are kept between the original
91 -- tree and the copy, in order to recognize non-local references within
92 -- the generic, and propagate them to each instance (recall that name
93 -- resolution is done on the generic declaration: generics are not really
94 -- macros!). This is summarized in the following diagram:
96 -- .-----------. .----------.
97 -- | semantic |<--------------| generic |
98 -- | copy | | unit |
99 -- | |==============>| |
100 -- |___________| global |__________|
101 -- references | | |
102 -- | | |
103 -- .-----|--|.
104 -- | .-----|---.
105 -- | | .----------.
106 -- | | | generic |
107 -- |__| | |
108 -- |__| instance |
109 -- |__________|
111 -- b) Each instantiation copies the original tree, and inserts into it a
112 -- series of declarations that describe the mapping between generic formals
113 -- and actuals. For example, a generic In OUT parameter is an object
114 -- renaming of the corresponing actual, etc. Generic IN parameters are
115 -- constant declarations.
117 -- c) In order to give the right visibility for these renamings, we use
118 -- a different scheme for package and subprogram instantiations. For
119 -- packages, the list of renamings is inserted into the package
120 -- specification, before the visible declarations of the package. The
121 -- renamings are analyzed before any of the text of the instance, and are
122 -- thus visible at the right place. Furthermore, outside of the instance,
123 -- the generic parameters are visible and denote their corresponding
124 -- actuals.
126 -- For subprograms, we create a container package to hold the renamings
127 -- and the subprogram instance itself. Analysis of the package makes the
128 -- renaming declarations visible to the subprogram. After analyzing the
129 -- package, the defining entity for the subprogram is touched-up so that
130 -- it appears declared in the current scope, and not inside the container
131 -- package.
133 -- If the instantiation is a compilation unit, the container package is
134 -- given the same name as the subprogram instance. This ensures that
135 -- the elaboration procedure called by the binder, using the compilation
136 -- unit name, calls in fact the elaboration procedure for the package.
138 -- Not surprisingly, private types complicate this approach. By saving in
139 -- the original generic object the non-local references, we guarantee that
140 -- the proper entities are referenced at the point of instantiation.
141 -- However, for private types, this by itself does not insure that the
142 -- proper VIEW of the entity is used (the full type may be visible at the
143 -- point of generic definition, but not at instantiation, or vice-versa).
144 -- In order to reference the proper view, we special-case any reference
145 -- to private types in the generic object, by saving both views, one in
146 -- the generic and one in the semantic copy. At time of instantiation, we
147 -- check whether the two views are consistent, and exchange declarations if
148 -- necessary, in order to restore the correct visibility. Similarly, if
149 -- the instance view is private when the generic view was not, we perform
150 -- the exchange. After completing the instantiation, we restore the
151 -- current visibility. The flag Has_Private_View marks identifiers in the
152 -- the generic unit that require checking.
154 -- Visibility within nested generic units requires special handling.
155 -- Consider the following scheme:
157 -- type Global is ... -- outside of generic unit.
158 -- generic ...
159 -- package Outer is
160 -- ...
161 -- type Semi_Global is ... -- global to inner.
163 -- generic ... -- 1
164 -- procedure inner (X1 : Global; X2 : Semi_Global);
166 -- procedure in2 is new inner (...); -- 4
167 -- end Outer;
169 -- package New_Outer is new Outer (...); -- 2
170 -- procedure New_Inner is new New_Outer.Inner (...); -- 3
172 -- The semantic analysis of Outer captures all occurrences of Global.
173 -- The semantic analysis of Inner (at 1) captures both occurrences of
174 -- Global and Semi_Global.
176 -- At point 2 (instantiation of Outer), we also produce a generic copy
177 -- of Inner, even though Inner is, at that point, not being instantiated.
178 -- (This is just part of the semantic analysis of New_Outer).
180 -- Critically, references to Global within Inner must be preserved, while
181 -- references to Semi_Global should not preserved, because they must now
182 -- resolve to an entity within New_Outer. To distinguish between these, we
183 -- use a global variable, Current_Instantiated_Parent, which is set when
184 -- performing a generic copy during instantiation (at 2). This variable is
185 -- used when performing a generic copy that is not an instantiation, but
186 -- that is nested within one, as the occurrence of 1 within 2. The analysis
187 -- of a nested generic only preserves references that are global to the
188 -- enclosing Current_Instantiated_Parent. We use the Scope_Depth value to
189 -- determine whether a reference is external to the given parent.
191 -- The instantiation at point 3 requires no special treatment. The method
192 -- works as well for further nestings of generic units, but of course the
193 -- variable Current_Instantiated_Parent must be stacked because nested
194 -- instantiations can occur, e.g. the occurrence of 4 within 2.
196 -- The instantiation of package and subprogram bodies is handled in a
197 -- similar manner, except that it is delayed until after semantic
198 -- analysis is complete. In this fashion complex cross-dependencies
199 -- between several package declarations and bodies containing generics
200 -- can be compiled which otherwise would diagnose spurious circularities.
202 -- For example, it is possible to compile two packages A and B that
203 -- have the following structure:
205 -- package A is package B is
206 -- generic ... generic ...
207 -- package G_A is package G_B is
209 -- with B; with A;
210 -- package body A is package body B is
211 -- package N_B is new G_B (..) package N_A is new G_A (..)
213 -- The table Pending_Instantiations in package Inline is used to keep
214 -- track of body instantiations that are delayed in this manner. Inline
215 -- handles the actual calls to do the body instantiations. This activity
216 -- is part of Inline, since the processing occurs at the same point, and
217 -- for essentially the same reason, as the handling of inlined routines.
219 ----------------------------------------------
220 -- Detection of Instantiation Circularities --
221 ----------------------------------------------
223 -- If we have a chain of instantiations that is circular, this is a
224 -- static error which must be detected at compile time. The detection
225 -- of these circularities is carried out at the point that we insert
226 -- a generic instance spec or body. If there is a circularity, then
227 -- the analysis of the offending spec or body will eventually result
228 -- in trying to load the same unit again, and we detect this problem
229 -- as we analyze the package instantiation for the second time.
231 -- At least in some cases after we have detected the circularity, we
232 -- get into trouble if we try to keep going. The following flag is
233 -- set if a circularity is detected, and used to abandon compilation
234 -- after the messages have been posted.
236 Circularity_Detected : Boolean := False;
237 -- This should really be reset on encountering a new main unit, but in
238 -- practice we are not using multiple main units so it is not critical.
240 -----------------------
241 -- Local subprograms --
242 -----------------------
244 procedure Abandon_Instantiation (N : Node_Id);
245 pragma No_Return (Abandon_Instantiation);
246 -- Posts an error message "instantiation abandoned" at the indicated
247 -- node and then raises the exception Instantiation_Error to do it.
249 procedure Analyze_Formal_Array_Type
250 (T : in out Entity_Id;
251 Def : Node_Id);
252 -- A formal array type is treated like an array type declaration, and
253 -- invokes Array_Type_Declaration (sem_ch3) whose first parameter is
254 -- in-out, because in the case of an anonymous type the entity is
255 -- actually created in the procedure.
257 -- The following procedures treat other kinds of formal parameters
259 procedure Analyze_Formal_Derived_Type
260 (N : Node_Id;
261 T : Entity_Id;
262 Def : Node_Id);
264 -- The following subprograms create abbreviated declarations for formal
265 -- scalar types. We introduce an anonymous base of the proper class for
266 -- each of them, and define the formals as constrained first subtypes of
267 -- their bases. The bounds are expressions that are non-static in the
268 -- generic.
270 procedure Analyze_Formal_Decimal_Fixed_Point_Type
271 (T : Entity_Id; Def : Node_Id);
272 procedure Analyze_Formal_Discrete_Type (T : Entity_Id; Def : Node_Id);
273 procedure Analyze_Formal_Floating_Type (T : Entity_Id; Def : Node_Id);
274 procedure Analyze_Formal_Signed_Integer_Type (T : Entity_Id; Def : Node_Id);
275 procedure Analyze_Formal_Modular_Type (T : Entity_Id; Def : Node_Id);
276 procedure Analyze_Formal_Ordinary_Fixed_Point_Type
277 (T : Entity_Id; Def : Node_Id);
279 procedure Analyze_Formal_Private_Type
280 (N : Node_Id;
281 T : Entity_Id;
282 Def : Node_Id);
283 -- This needs comments???
285 procedure Analyze_Generic_Formal_Part (N : Node_Id);
287 procedure Analyze_Generic_Access_Type (T : Entity_Id; Def : Node_Id);
288 -- This needs comments ???
290 function Analyze_Associations
291 (I_Node : Node_Id;
292 Formals : List_Id;
293 F_Copy : List_Id) return List_Id;
294 -- At instantiation time, build the list of associations between formals
295 -- and actuals. Each association becomes a renaming declaration for the
296 -- formal entity. F_Copy is the analyzed list of formals in the generic
297 -- copy. It is used to apply legality checks to the actuals. I_Node is the
298 -- instantiation node itself.
300 procedure Analyze_Subprogram_Instantiation
301 (N : Node_Id;
302 K : Entity_Kind);
304 procedure Build_Instance_Compilation_Unit_Nodes
305 (N : Node_Id;
306 Act_Body : Node_Id;
307 Act_Decl : Node_Id);
308 -- This procedure is used in the case where the generic instance of a
309 -- subprogram body or package body is a library unit. In this case, the
310 -- original library unit node for the generic instantiation must be
311 -- replaced by the resulting generic body, and a link made to a new
312 -- compilation unit node for the generic declaration. The argument N is
313 -- the original generic instantiation. Act_Body and Act_Decl are the body
314 -- and declaration of the instance (either package body and declaration
315 -- nodes or subprogram body and declaration nodes depending on the case).
316 -- On return, the node N has been rewritten with the actual body.
318 procedure Check_Formal_Packages (P_Id : Entity_Id);
319 -- Apply the following to all formal packages in generic associations
321 procedure Check_Formal_Package_Instance
322 (Formal_Pack : Entity_Id;
323 Actual_Pack : Entity_Id);
324 -- Verify that the actuals of the actual instance match the actuals of
325 -- the template for a formal package that is not declared with a box.
327 procedure Check_Forward_Instantiation (Decl : Node_Id);
328 -- If the generic is a local entity and the corresponding body has not
329 -- been seen yet, flag enclosing packages to indicate that it will be
330 -- elaborated after the generic body. Subprograms declared in the same
331 -- package cannot be inlined by the front-end because front-end inlining
332 -- requires a strict linear order of elaboration.
334 procedure Check_Hidden_Child_Unit
335 (N : Node_Id;
336 Gen_Unit : Entity_Id;
337 Act_Decl_Id : Entity_Id);
338 -- If the generic unit is an implicit child instance within a parent
339 -- instance, we need to make an explicit test that it is not hidden by
340 -- a child instance of the same name and parent.
342 procedure Check_Private_View (N : Node_Id);
343 -- Check whether the type of a generic entity has a different view between
344 -- the point of generic analysis and the point of instantiation. If the
345 -- view has changed, then at the point of instantiation we restore the
346 -- correct view to perform semantic analysis of the instance, and reset
347 -- the current view after instantiation. The processing is driven by the
348 -- current private status of the type of the node, and Has_Private_View,
349 -- a flag that is set at the point of generic compilation. If view and
350 -- flag are inconsistent then the type is updated appropriately.
352 procedure Check_Generic_Actuals
353 (Instance : Entity_Id;
354 Is_Formal_Box : Boolean);
355 -- Similar to previous one. Check the actuals in the instantiation,
356 -- whose views can change between the point of instantiation and the point
357 -- of instantiation of the body. In addition, mark the generic renamings
358 -- as generic actuals, so that they are not compatible with other actuals.
359 -- Recurse on an actual that is a formal package whose declaration has
360 -- a box.
362 function Contains_Instance_Of
363 (Inner : Entity_Id;
364 Outer : Entity_Id;
365 N : Node_Id) return Boolean;
366 -- Inner is instantiated within the generic Outer. Check whether Inner
367 -- directly or indirectly contains an instance of Outer or of one of its
368 -- parents, in the case of a subunit. Each generic unit holds a list of
369 -- the entities instantiated within (at any depth). This procedure
370 -- determines whether the set of such lists contains a cycle, i.e. an
371 -- illegal circular instantiation.
373 function Denotes_Formal_Package
374 (Pack : Entity_Id;
375 On_Exit : Boolean := False) return Boolean;
376 -- Returns True if E is a formal package of an enclosing generic, or
377 -- the actual for such a formal in an enclosing instantiation. If such
378 -- a package is used as a formal in an nested generic, or as an actual
379 -- in a nested instantiation, the visibility of ITS formals should not
380 -- be modified. When called from within Restore_Private_Views, the flag
381 -- On_Exit is true, to indicate that the search for a possible enclosing
382 -- instance should ignore the current one.
384 function Find_Actual_Type
385 (Typ : Entity_Id;
386 Gen_Scope : Entity_Id) return Entity_Id;
387 -- When validating the actual types of a child instance, check whether
388 -- the formal is a formal type of the parent unit, and retrieve the current
389 -- actual for it. Typ is the entity in the analyzed formal type declaration
390 -- (component or index type of an array type) and Gen_Scope is the scope of
391 -- the analyzed formal array type.
393 function Get_Package_Instantiation_Node (A : Entity_Id) return Node_Id;
394 -- Given the entity of a unit that is an instantiation, retrieve the
395 -- original instance node. This is used when loading the instantiations
396 -- of the ancestors of a child generic that is being instantiated.
398 function In_Same_Declarative_Part
399 (F_Node : Node_Id;
400 Inst : Node_Id) return Boolean;
401 -- True if the instantiation Inst and the given freeze_node F_Node appear
402 -- within the same declarative part, ignoring subunits, but with no inter-
403 -- vening suprograms or concurrent units. If true, the freeze node
404 -- of the instance can be placed after the freeze node of the parent,
405 -- which it itself an instance.
407 function In_Main_Context (E : Entity_Id) return Boolean;
408 -- Check whether an instantiation is in the context of the main unit.
409 -- Used to determine whether its body should be elaborated to allow
410 -- front-end inlining.
412 procedure Set_Instance_Env
413 (Gen_Unit : Entity_Id;
414 Act_Unit : Entity_Id);
415 -- Save current instance on saved environment, to be used to determine
416 -- the global status of entities in nested instances. Part of Save_Env.
417 -- called after verifying that the generic unit is legal for the instance.
419 procedure Set_Instance_Of (A : Entity_Id; B : Entity_Id);
420 -- Associate analyzed generic parameter with corresponding
421 -- instance. Used for semantic checks at instantiation time.
423 function Has_Been_Exchanged (E : Entity_Id) return Boolean;
424 -- Traverse the Exchanged_Views list to see if a type was private
425 -- and has already been flipped during this phase of instantiation.
427 procedure Hide_Current_Scope;
428 -- When compiling a generic child unit, the parent context must be
429 -- present, but the instance and all entities that may be generated
430 -- must be inserted in the current scope. We leave the current scope
431 -- on the stack, but make its entities invisible to avoid visibility
432 -- problems. This is reversed at the end of instantiations. This is
433 -- not done for the instantiation of the bodies, which only require the
434 -- instances of the generic parents to be in scope.
436 procedure Install_Body
437 (Act_Body : Node_Id;
438 N : Node_Id;
439 Gen_Body : Node_Id;
440 Gen_Decl : Node_Id);
441 -- If the instantiation happens textually before the body of the generic,
442 -- the instantiation of the body must be analyzed after the generic body,
443 -- and not at the point of instantiation. Such early instantiations can
444 -- happen if the generic and the instance appear in a package declaration
445 -- because the generic body can only appear in the corresponding package
446 -- body. Early instantiations can also appear if generic, instance and
447 -- body are all in the declarative part of a subprogram or entry. Entities
448 -- of packages that are early instantiations are delayed, and their freeze
449 -- node appears after the generic body.
451 procedure Insert_After_Last_Decl (N : Node_Id; F_Node : Node_Id);
452 -- Insert freeze node at the end of the declarative part that includes the
453 -- instance node N. If N is in the visible part of an enclosing package
454 -- declaration, the freeze node has to be inserted at the end of the
455 -- private declarations, if any.
457 procedure Freeze_Subprogram_Body
458 (Inst_Node : Node_Id;
459 Gen_Body : Node_Id;
460 Pack_Id : Entity_Id);
461 -- The generic body may appear textually after the instance, including
462 -- in the proper body of a stub, or within a different package instance.
463 -- Given that the instance can only be elaborated after the generic, we
464 -- place freeze_nodes for the instance and/or for packages that may enclose
465 -- the instance and the generic, so that the back-end can establish the
466 -- proper order of elaboration.
468 procedure Init_Env;
469 -- Establish environment for subsequent instantiation. Separated from
470 -- Save_Env because data-structures for visibility handling must be
471 -- initialized before call to Check_Generic_Child_Unit.
473 procedure Install_Parent (P : Entity_Id; In_Body : Boolean := False);
474 -- When compiling an instance of a child unit the parent (which is
475 -- itself an instance) is an enclosing scope that must be made
476 -- immediately visible. This procedure is also used to install the non-
477 -- generic parent of a generic child unit when compiling its body, so
478 -- that full views of types in the parent are made visible.
480 procedure Remove_Parent (In_Body : Boolean := False);
481 -- Reverse effect after instantiation of child is complete
483 procedure Inline_Instance_Body
484 (N : Node_Id;
485 Gen_Unit : Entity_Id;
486 Act_Decl : Node_Id);
487 -- If front-end inlining is requested, instantiate the package body,
488 -- and preserve the visibility of its compilation unit, to insure
489 -- that successive instantiations succeed.
491 -- The functions Instantiate_XXX perform various legality checks and build
492 -- the declarations for instantiated generic parameters. In all of these
493 -- Formal is the entity in the generic unit, Actual is the entity of
494 -- expression in the generic associations, and Analyzed_Formal is the
495 -- formal in the generic copy, which contains the semantic information to
496 -- be used to validate the actual.
498 function Instantiate_Object
499 (Formal : Node_Id;
500 Actual : Node_Id;
501 Analyzed_Formal : Node_Id) return List_Id;
503 function Instantiate_Type
504 (Formal : Node_Id;
505 Actual : Node_Id;
506 Analyzed_Formal : Node_Id;
507 Actual_Decls : List_Id) return Node_Id;
509 function Instantiate_Formal_Subprogram
510 (Formal : Node_Id;
511 Actual : Node_Id;
512 Analyzed_Formal : Node_Id) return Node_Id;
514 function Instantiate_Formal_Package
515 (Formal : Node_Id;
516 Actual : Node_Id;
517 Analyzed_Formal : Node_Id) return List_Id;
518 -- If the formal package is declared with a box, special visibility rules
519 -- apply to its formals: they are in the visible part of the package. This
520 -- is true in the declarative region of the formal package, that is to say
521 -- in the enclosing generic or instantiation. For an instantiation, the
522 -- parameters of the formal package are made visible in an explicit step.
523 -- Furthermore, if the actual is a visible use_clause, these formals must
524 -- be made potentially use_visible as well. On exit from the enclosing
525 -- instantiation, the reverse must be done.
527 -- For a formal package declared without a box, there are conformance rules
528 -- that apply to the actuals in the generic declaration and the actuals of
529 -- the actual package in the enclosing instantiation. The simplest way to
530 -- apply these rules is to repeat the instantiation of the formal package
531 -- in the context of the enclosing instance, and compare the generic
532 -- associations of this instantiation with those of the actual package.
534 function Is_In_Main_Unit (N : Node_Id) return Boolean;
535 -- Test if given node is in the main unit
537 procedure Load_Parent_Of_Generic (N : Node_Id; Spec : Node_Id);
538 -- If the generic appears in a separate non-generic library unit,
539 -- load the corresponding body to retrieve the body of the generic.
540 -- N is the node for the generic instantiation, Spec is the generic
541 -- package declaration.
543 procedure Inherit_Context (Gen_Decl : Node_Id; Inst : Node_Id);
544 -- Add the context clause of the unit containing a generic unit to
545 -- an instantiation that is a compilation unit.
547 function Get_Associated_Node (N : Node_Id) return Node_Id;
548 -- In order to propagate semantic information back from the analyzed
549 -- copy to the original generic, we maintain links between selected nodes
550 -- in the generic and their corresponding copies. At the end of generic
551 -- analysis, the routine Save_Global_References traverses the generic
552 -- tree, examines the semantic information, and preserves the links to
553 -- those nodes that contain global information. At instantiation, the
554 -- information from the associated node is placed on the new copy, so
555 -- that name resolution is not repeated.
557 -- Three kinds of source nodes have associated nodes:
559 -- a) those that can reference (denote) entities, that is identifiers,
560 -- character literals, expanded_names, operator symbols, operators,
561 -- and attribute reference nodes. These nodes have an Entity field
562 -- and are the set of nodes that are in N_Has_Entity.
564 -- b) aggregates (N_Aggregate and N_Extension_Aggregate)
566 -- c) selected components (N_Selected_Component)
568 -- For the first class, the associated node preserves the entity if it is
569 -- global. If the generic contains nested instantiations, the associated
570 -- node itself has been recopied, and a chain of them must be followed.
572 -- For aggregates, the associated node allows retrieval of the type, which
573 -- may otherwise not appear in the generic. The view of this type may be
574 -- different between generic and instantiation, and the full view can be
575 -- installed before the instantiation is analyzed. For aggregates of
576 -- type extensions, the same view exchange may have to be performed for
577 -- some of the ancestor types, if their view is private at the point of
578 -- instantiation.
580 -- Nodes that are selected components in the parse tree may be rewritten
581 -- as expanded names after resolution, and must be treated as potential
582 -- entity holders. which is why they also have an Associated_Node.
584 -- Nodes that do not come from source, such as freeze nodes, do not appear
585 -- in the generic tree, and need not have an associated node.
587 -- The associated node is stored in the Associated_Node field. Note that
588 -- this field overlaps Entity, which is fine, because the whole point is
589 -- that we don't need or want the normal Entity field in this situation.
591 procedure Move_Freeze_Nodes
592 (Out_Of : Entity_Id;
593 After : Node_Id;
594 L : List_Id);
595 -- Freeze nodes can be generated in the analysis of a generic unit, but
596 -- will not be seen by the back-end. It is necessary to move those nodes
597 -- to the enclosing scope if they freeze an outer entity. We place them
598 -- at the end of the enclosing generic package, which is semantically
599 -- neutral.
601 procedure Pre_Analyze_Actuals (N : Node_Id);
602 -- Analyze actuals to perform name resolution. Full resolution is done
603 -- later, when the expected types are known, but names have to be captured
604 -- before installing parents of generics, that are not visible for the
605 -- actuals themselves.
607 procedure Valid_Default_Attribute (Nam : Entity_Id; Def : Node_Id);
608 -- Verify that an attribute that appears as the default for a formal
609 -- subprogram is a function or procedure with the correct profile.
611 -------------------------------------------
612 -- Data Structures for Generic Renamings --
613 -------------------------------------------
615 -- The map Generic_Renamings associates generic entities with their
616 -- corresponding actuals. Currently used to validate type instances.
617 -- It will eventually be used for all generic parameters to eliminate
618 -- the need for overload resolution in the instance.
620 type Assoc_Ptr is new Int;
622 Assoc_Null : constant Assoc_Ptr := -1;
624 type Assoc is record
625 Gen_Id : Entity_Id;
626 Act_Id : Entity_Id;
627 Next_In_HTable : Assoc_Ptr;
628 end record;
630 package Generic_Renamings is new Table.Table
631 (Table_Component_Type => Assoc,
632 Table_Index_Type => Assoc_Ptr,
633 Table_Low_Bound => 0,
634 Table_Initial => 10,
635 Table_Increment => 100,
636 Table_Name => "Generic_Renamings");
638 -- Variable to hold enclosing instantiation. When the environment is
639 -- saved for a subprogram inlining, the corresponding Act_Id is empty.
641 Current_Instantiated_Parent : Assoc := (Empty, Empty, Assoc_Null);
643 -- Hash table for associations
645 HTable_Size : constant := 37;
646 type HTable_Range is range 0 .. HTable_Size - 1;
648 procedure Set_Next_Assoc (E : Assoc_Ptr; Next : Assoc_Ptr);
649 function Next_Assoc (E : Assoc_Ptr) return Assoc_Ptr;
650 function Get_Gen_Id (E : Assoc_Ptr) return Entity_Id;
651 function Hash (F : Entity_Id) return HTable_Range;
653 package Generic_Renamings_HTable is new GNAT.HTable.Static_HTable (
654 Header_Num => HTable_Range,
655 Element => Assoc,
656 Elmt_Ptr => Assoc_Ptr,
657 Null_Ptr => Assoc_Null,
658 Set_Next => Set_Next_Assoc,
659 Next => Next_Assoc,
660 Key => Entity_Id,
661 Get_Key => Get_Gen_Id,
662 Hash => Hash,
663 Equal => "=");
665 Exchanged_Views : Elist_Id;
666 -- This list holds the private views that have been exchanged during
667 -- instantiation to restore the visibility of the generic declaration.
668 -- (see comments above). After instantiation, the current visibility is
669 -- reestablished by means of a traversal of this list.
671 Hidden_Entities : Elist_Id;
672 -- This list holds the entities of the current scope that are removed
673 -- from immediate visibility when instantiating a child unit. Their
674 -- visibility is restored in Remove_Parent.
676 -- Because instantiations can be recursive, the following must be saved
677 -- on entry and restored on exit from an instantiation (spec or body).
678 -- This is done by the two procedures Save_Env and Restore_Env. For
679 -- package and subprogram instantiations (but not for the body instances)
680 -- the action of Save_Env is done in two steps: Init_Env is called before
681 -- Check_Generic_Child_Unit, because setting the parent instances requires
682 -- that the visibility data structures be properly initialized. Once the
683 -- generic is unit is validated, Set_Instance_Env completes Save_Env.
685 Parent_Unit_Visible : Boolean := False;
686 -- Parent_Unit_Visible is used when the generic is a child unit, and
687 -- indicates whether the ultimate parent of the generic is visible in the
688 -- instantiation environment. It is used to reset the visiblity of the
689 -- parent at the end of the instantiation (see Remove_Parent).
691 type Instance_Env is record
692 Ada_Version : Ada_Version_Type;
693 Ada_Version_Explicit : Ada_Version_Type;
694 Instantiated_Parent : Assoc;
695 Exchanged_Views : Elist_Id;
696 Hidden_Entities : Elist_Id;
697 Current_Sem_Unit : Unit_Number_Type;
698 Parent_Unit_Visible : Boolean := False;
699 end record;
701 package Instance_Envs is new Table.Table (
702 Table_Component_Type => Instance_Env,
703 Table_Index_Type => Int,
704 Table_Low_Bound => 0,
705 Table_Initial => 32,
706 Table_Increment => 100,
707 Table_Name => "Instance_Envs");
709 procedure Restore_Private_Views
710 (Pack_Id : Entity_Id;
711 Is_Package : Boolean := True);
712 -- Restore the private views of external types, and unmark the generic
713 -- renamings of actuals, so that they become comptible subtypes again.
714 -- For subprograms, Pack_Id is the package constructed to hold the
715 -- renamings.
717 procedure Switch_View (T : Entity_Id);
718 -- Switch the partial and full views of a type and its private
719 -- dependents (i.e. its subtypes and derived types).
721 ------------------------------------
722 -- Structures for Error Reporting --
723 ------------------------------------
725 Instantiation_Node : Node_Id;
726 -- Used by subprograms that validate instantiation of formal parameters
727 -- where there might be no actual on which to place the error message.
728 -- Also used to locate the instantiation node for generic subunits.
730 Instantiation_Error : exception;
731 -- When there is a semantic error in the generic parameter matching,
732 -- there is no point in continuing the instantiation, because the
733 -- number of cascaded errors is unpredictable. This exception aborts
734 -- the instantiation process altogether.
736 S_Adjustment : Sloc_Adjustment;
737 -- Offset created for each node in an instantiation, in order to keep
738 -- track of the source position of the instantiation in each of its nodes.
739 -- A subsequent semantic error or warning on a construct of the instance
740 -- points to both places: the original generic node, and the point of
741 -- instantiation. See Sinput and Sinput.L for additional details.
743 ------------------------------------------------------------
744 -- Data structure for keeping track when inside a Generic --
745 ------------------------------------------------------------
747 -- The following table is used to save values of the Inside_A_Generic
748 -- flag (see spec of Sem) when they are saved by Start_Generic.
750 package Generic_Flags is new Table.Table (
751 Table_Component_Type => Boolean,
752 Table_Index_Type => Int,
753 Table_Low_Bound => 0,
754 Table_Initial => 32,
755 Table_Increment => 200,
756 Table_Name => "Generic_Flags");
758 ---------------------------
759 -- Abandon_Instantiation --
760 ---------------------------
762 procedure Abandon_Instantiation (N : Node_Id) is
763 begin
764 Error_Msg_N ("instantiation abandoned!", N);
765 raise Instantiation_Error;
766 end Abandon_Instantiation;
768 --------------------------
769 -- Analyze_Associations --
770 --------------------------
772 function Analyze_Associations
773 (I_Node : Node_Id;
774 Formals : List_Id;
775 F_Copy : List_Id) return List_Id
777 Actual_Types : constant Elist_Id := New_Elmt_List;
778 Assoc : constant List_Id := New_List;
779 Defaults : constant Elist_Id := New_Elmt_List;
780 Gen_Unit : constant Entity_Id := Defining_Entity (Parent (F_Copy));
781 Actuals : List_Id;
782 Actual : Node_Id;
783 Formal : Node_Id;
784 Next_Formal : Node_Id;
785 Temp_Formal : Node_Id;
786 Analyzed_Formal : Node_Id;
787 Match : Node_Id;
788 Named : Node_Id;
789 First_Named : Node_Id := Empty;
790 Found_Assoc : Node_Id;
791 Is_Named_Assoc : Boolean;
792 Num_Matched : Int := 0;
793 Num_Actuals : Int := 0;
795 function Matching_Actual
796 (F : Entity_Id;
797 A_F : Entity_Id) return Node_Id;
798 -- Find actual that corresponds to a given a formal parameter. If the
799 -- actuals are positional, return the next one, if any. If the actuals
800 -- are named, scan the parameter associations to find the right one.
801 -- A_F is the corresponding entity in the analyzed generic,which is
802 -- placed on the selector name for ASIS use.
804 procedure Set_Analyzed_Formal;
805 -- Find the node in the generic copy that corresponds to a given formal.
806 -- The semantic information on this node is used to perform legality
807 -- checks on the actuals. Because semantic analysis can introduce some
808 -- anonymous entities or modify the declaration node itself, the
809 -- correspondence between the two lists is not one-one. In addition to
810 -- anonymous types, the presence a formal equality will introduce an
811 -- implicit declaration for the corresponding inequality.
813 ---------------------
814 -- Matching_Actual --
815 ---------------------
817 function Matching_Actual
818 (F : Entity_Id;
819 A_F : Entity_Id) return Node_Id
821 Found : Node_Id;
822 Prev : Node_Id;
824 begin
825 Is_Named_Assoc := False;
827 -- End of list of purely positional parameters
829 if No (Actual) then
830 Found := Empty;
832 -- Case of positional parameter corresponding to current formal
834 elsif No (Selector_Name (Actual)) then
835 Found := Explicit_Generic_Actual_Parameter (Actual);
836 Found_Assoc := Actual;
837 Num_Matched := Num_Matched + 1;
838 Next (Actual);
840 -- Otherwise scan list of named actuals to find the one with the
841 -- desired name. All remaining actuals have explicit names.
843 else
844 Is_Named_Assoc := True;
845 Found := Empty;
846 Prev := Empty;
848 while Present (Actual) loop
849 if Chars (Selector_Name (Actual)) = Chars (F) then
850 Found := Explicit_Generic_Actual_Parameter (Actual);
851 Set_Entity (Selector_Name (Actual), A_F);
852 Set_Etype (Selector_Name (Actual), Etype (A_F));
853 Generate_Reference (A_F, Selector_Name (Actual));
854 Found_Assoc := Actual;
855 Num_Matched := Num_Matched + 1;
856 exit;
857 end if;
859 Prev := Actual;
860 Next (Actual);
861 end loop;
863 -- Reset for subsequent searches. In most cases the named
864 -- associations are in order. If they are not, we reorder them
865 -- to avoid scanning twice the same actual. This is not just a
866 -- question of efficiency: there may be multiple defaults with
867 -- boxes that have the same name. In a nested instantiation we
868 -- insert actuals for those defaults, and cannot rely on their
869 -- names to disambiguate them.
871 if Actual = First_Named then
872 Next (First_Named);
874 elsif Present (Actual) then
875 Insert_Before (First_Named, Remove_Next (Prev));
876 end if;
878 Actual := First_Named;
879 end if;
881 return Found;
882 end Matching_Actual;
884 -------------------------
885 -- Set_Analyzed_Formal --
886 -------------------------
888 procedure Set_Analyzed_Formal is
889 Kind : Node_Kind;
890 begin
891 while Present (Analyzed_Formal) loop
892 Kind := Nkind (Analyzed_Formal);
894 case Nkind (Formal) is
896 when N_Formal_Subprogram_Declaration =>
897 exit when Kind in N_Formal_Subprogram_Declaration
898 and then
899 Chars
900 (Defining_Unit_Name (Specification (Formal))) =
901 Chars
902 (Defining_Unit_Name (Specification (Analyzed_Formal)));
904 when N_Formal_Package_Declaration =>
905 exit when
906 Kind = N_Formal_Package_Declaration
907 or else
908 Kind = N_Generic_Package_Declaration;
910 when N_Use_Package_Clause | N_Use_Type_Clause => exit;
912 when others =>
914 -- Skip freeze nodes, and nodes inserted to replace
915 -- unrecognized pragmas.
917 exit when
918 Kind not in N_Formal_Subprogram_Declaration
919 and then Kind /= N_Subprogram_Declaration
920 and then Kind /= N_Freeze_Entity
921 and then Kind /= N_Null_Statement
922 and then Kind /= N_Itype_Reference
923 and then Chars (Defining_Identifier (Formal)) =
924 Chars (Defining_Identifier (Analyzed_Formal));
925 end case;
927 Next (Analyzed_Formal);
928 end loop;
930 end Set_Analyzed_Formal;
932 -- Start of processing for Analyze_Associations
934 begin
935 -- If named associations are present, save the first named association
936 -- (it may of course be Empty) to facilitate subsequent name search.
938 Actuals := Generic_Associations (I_Node);
940 if Present (Actuals) then
941 First_Named := First (Actuals);
943 while Present (First_Named)
944 and then No (Selector_Name (First_Named))
945 loop
946 Num_Actuals := Num_Actuals + 1;
947 Next (First_Named);
948 end loop;
949 end if;
951 Named := First_Named;
952 while Present (Named) loop
953 if No (Selector_Name (Named)) then
954 Error_Msg_N ("invalid positional actual after named one", Named);
955 Abandon_Instantiation (Named);
956 end if;
958 -- A named association may lack an actual parameter, if it was
959 -- introduced for a default subprogram that turns out to be local
960 -- to the outer instantiation.
962 if Present (Explicit_Generic_Actual_Parameter (Named)) then
963 Num_Actuals := Num_Actuals + 1;
964 end if;
966 Next (Named);
967 end loop;
969 if Present (Formals) then
970 Formal := First_Non_Pragma (Formals);
971 Analyzed_Formal := First_Non_Pragma (F_Copy);
973 if Present (Actuals) then
974 Actual := First (Actuals);
976 -- All formals should have default values
978 else
979 Actual := Empty;
980 end if;
982 while Present (Formal) loop
983 Set_Analyzed_Formal;
984 Next_Formal := Next_Non_Pragma (Formal);
986 case Nkind (Formal) is
987 when N_Formal_Object_Declaration =>
988 Match :=
989 Matching_Actual (
990 Defining_Identifier (Formal),
991 Defining_Identifier (Analyzed_Formal));
993 Append_List
994 (Instantiate_Object (Formal, Match, Analyzed_Formal),
995 Assoc);
997 when N_Formal_Type_Declaration =>
998 Match :=
999 Matching_Actual (
1000 Defining_Identifier (Formal),
1001 Defining_Identifier (Analyzed_Formal));
1003 if No (Match) then
1004 Error_Msg_Sloc := Sloc (Gen_Unit);
1005 Error_Msg_NE
1006 ("missing actual&",
1007 Instantiation_Node, Defining_Identifier (Formal));
1008 Error_Msg_NE ("\in instantiation of & declared#",
1009 Instantiation_Node, Gen_Unit);
1010 Abandon_Instantiation (Instantiation_Node);
1012 else
1013 Analyze (Match);
1014 Append_To (Assoc,
1015 Instantiate_Type
1016 (Formal, Match, Analyzed_Formal, Assoc));
1018 -- an instantiation is a freeze point for the actuals,
1019 -- unless this is a rewritten formal package.
1021 if Nkind (I_Node) /= N_Formal_Package_Declaration then
1022 Append_Elmt (Entity (Match), Actual_Types);
1023 end if;
1024 end if;
1026 -- A remote access-to-class-wide type must not be an
1027 -- actual parameter for a generic formal of an access
1028 -- type (E.2.2 (17)).
1030 if Nkind (Analyzed_Formal) = N_Formal_Type_Declaration
1031 and then
1032 Nkind (Formal_Type_Definition (Analyzed_Formal)) =
1033 N_Access_To_Object_Definition
1034 then
1035 Validate_Remote_Access_To_Class_Wide_Type (Match);
1036 end if;
1038 when N_Formal_Subprogram_Declaration =>
1039 Match :=
1040 Matching_Actual (
1041 Defining_Unit_Name (Specification (Formal)),
1042 Defining_Unit_Name (Specification (Analyzed_Formal)));
1044 -- If the formal subprogram has the same name as
1045 -- another formal subprogram of the generic, then
1046 -- a named association is illegal (12.3(9)). Exclude
1047 -- named associations that are generated for a nested
1048 -- instance.
1050 if Present (Match)
1051 and then Is_Named_Assoc
1052 and then Comes_From_Source (Found_Assoc)
1053 then
1054 Temp_Formal := First (Formals);
1055 while Present (Temp_Formal) loop
1056 if Nkind (Temp_Formal) in
1057 N_Formal_Subprogram_Declaration
1058 and then Temp_Formal /= Formal
1059 and then
1060 Chars (Selector_Name (Found_Assoc)) =
1061 Chars (Defining_Unit_Name
1062 (Specification (Temp_Formal)))
1063 then
1064 Error_Msg_N
1065 ("name not allowed for overloaded formal",
1066 Found_Assoc);
1067 Abandon_Instantiation (Instantiation_Node);
1068 end if;
1070 Next (Temp_Formal);
1071 end loop;
1072 end if;
1074 Append_To (Assoc,
1075 Instantiate_Formal_Subprogram
1076 (Formal, Match, Analyzed_Formal));
1078 if No (Match)
1079 and then Box_Present (Formal)
1080 then
1081 Append_Elmt
1082 (Defining_Unit_Name (Specification (Last (Assoc))),
1083 Defaults);
1084 end if;
1086 when N_Formal_Package_Declaration =>
1087 Match :=
1088 Matching_Actual (
1089 Defining_Identifier (Formal),
1090 Defining_Identifier (Original_Node (Analyzed_Formal)));
1092 if No (Match) then
1093 Error_Msg_Sloc := Sloc (Gen_Unit);
1094 Error_Msg_NE
1095 ("missing actual&",
1096 Instantiation_Node, Defining_Identifier (Formal));
1097 Error_Msg_NE ("\in instantiation of & declared#",
1098 Instantiation_Node, Gen_Unit);
1100 Abandon_Instantiation (Instantiation_Node);
1102 else
1103 Analyze (Match);
1104 Append_List
1105 (Instantiate_Formal_Package
1106 (Formal, Match, Analyzed_Formal),
1107 Assoc);
1108 end if;
1110 -- For use type and use package appearing in the context
1111 -- clause, we have already copied them, so we can just
1112 -- move them where they belong (we mustn't recopy them
1113 -- since this would mess up the Sloc values).
1115 when N_Use_Package_Clause |
1116 N_Use_Type_Clause =>
1117 Remove (Formal);
1118 Append (Formal, Assoc);
1120 when others =>
1121 raise Program_Error;
1123 end case;
1125 Formal := Next_Formal;
1126 Next_Non_Pragma (Analyzed_Formal);
1127 end loop;
1129 if Num_Actuals > Num_Matched then
1130 Error_Msg_Sloc := Sloc (Gen_Unit);
1132 if Present (Selector_Name (Actual)) then
1133 Error_Msg_NE
1134 ("unmatched actual&",
1135 Actual, Selector_Name (Actual));
1136 Error_Msg_NE ("\in instantiation of& declared#",
1137 Actual, Gen_Unit);
1138 else
1139 Error_Msg_NE
1140 ("unmatched actual in instantiation of& declared#",
1141 Actual, Gen_Unit);
1142 end if;
1143 end if;
1145 elsif Present (Actuals) then
1146 Error_Msg_N
1147 ("too many actuals in generic instantiation", Instantiation_Node);
1148 end if;
1150 declare
1151 Elmt : Elmt_Id := First_Elmt (Actual_Types);
1153 begin
1154 while Present (Elmt) loop
1155 Freeze_Before (I_Node, Node (Elmt));
1156 Next_Elmt (Elmt);
1157 end loop;
1158 end;
1160 -- If there are default subprograms, normalize the tree by adding
1161 -- explicit associations for them. This is required if the instance
1162 -- appears within a generic.
1164 declare
1165 Elmt : Elmt_Id;
1166 Subp : Entity_Id;
1167 New_D : Node_Id;
1169 begin
1170 Elmt := First_Elmt (Defaults);
1171 while Present (Elmt) loop
1172 if No (Actuals) then
1173 Actuals := New_List;
1174 Set_Generic_Associations (I_Node, Actuals);
1175 end if;
1177 Subp := Node (Elmt);
1178 New_D :=
1179 Make_Generic_Association (Sloc (Subp),
1180 Selector_Name => New_Occurrence_Of (Subp, Sloc (Subp)),
1181 Explicit_Generic_Actual_Parameter =>
1182 New_Occurrence_Of (Subp, Sloc (Subp)));
1183 Mark_Rewrite_Insertion (New_D);
1184 Append_To (Actuals, New_D);
1185 Next_Elmt (Elmt);
1186 end loop;
1187 end;
1189 return Assoc;
1190 end Analyze_Associations;
1192 -------------------------------
1193 -- Analyze_Formal_Array_Type --
1194 -------------------------------
1196 procedure Analyze_Formal_Array_Type
1197 (T : in out Entity_Id;
1198 Def : Node_Id)
1200 DSS : Node_Id;
1202 begin
1203 -- Treated like a non-generic array declaration, with
1204 -- additional semantic checks.
1206 Enter_Name (T);
1208 if Nkind (Def) = N_Constrained_Array_Definition then
1209 DSS := First (Discrete_Subtype_Definitions (Def));
1210 while Present (DSS) loop
1211 if Nkind (DSS) = N_Subtype_Indication
1212 or else Nkind (DSS) = N_Range
1213 or else Nkind (DSS) = N_Attribute_Reference
1214 then
1215 Error_Msg_N ("only a subtype mark is allowed in a formal", DSS);
1216 end if;
1218 Next (DSS);
1219 end loop;
1220 end if;
1222 Array_Type_Declaration (T, Def);
1223 Set_Is_Generic_Type (Base_Type (T));
1225 if Ekind (Component_Type (T)) = E_Incomplete_Type
1226 and then No (Full_View (Component_Type (T)))
1227 then
1228 Error_Msg_N ("premature usage of incomplete type", Def);
1230 -- Check that range constraint is not allowed on the component type
1231 -- of a generic formal array type (AARM 12.5.3(3))
1233 elsif Is_Internal (Component_Type (T))
1234 and then Present (Subtype_Indication (Component_Definition (Def)))
1235 and then Nkind (Original_Node
1236 (Subtype_Indication (Component_Definition (Def))))
1237 = N_Subtype_Indication
1238 then
1239 Error_Msg_N
1240 ("in a formal, a subtype indication can only be "
1241 & "a subtype mark ('R'M 12.5.3(3))",
1242 Subtype_Indication (Component_Definition (Def)));
1243 end if;
1245 end Analyze_Formal_Array_Type;
1247 ---------------------------------------------
1248 -- Analyze_Formal_Decimal_Fixed_Point_Type --
1249 ---------------------------------------------
1251 -- As for other generic types, we create a valid type representation
1252 -- with legal but arbitrary attributes, whose values are never considered
1253 -- static. For all scalar types we introduce an anonymous base type, with
1254 -- the same attributes. We choose the corresponding integer type to be
1255 -- Standard_Integer.
1257 procedure Analyze_Formal_Decimal_Fixed_Point_Type
1258 (T : Entity_Id;
1259 Def : Node_Id)
1261 Loc : constant Source_Ptr := Sloc (Def);
1262 Base : constant Entity_Id :=
1263 New_Internal_Entity
1264 (E_Decimal_Fixed_Point_Type,
1265 Current_Scope, Sloc (Def), 'G');
1266 Int_Base : constant Entity_Id := Standard_Integer;
1267 Delta_Val : constant Ureal := Ureal_1;
1268 Digs_Val : constant Uint := Uint_6;
1270 begin
1271 Enter_Name (T);
1273 Set_Etype (Base, Base);
1274 Set_Size_Info (Base, Int_Base);
1275 Set_RM_Size (Base, RM_Size (Int_Base));
1276 Set_First_Rep_Item (Base, First_Rep_Item (Int_Base));
1277 Set_Digits_Value (Base, Digs_Val);
1278 Set_Delta_Value (Base, Delta_Val);
1279 Set_Small_Value (Base, Delta_Val);
1280 Set_Scalar_Range (Base,
1281 Make_Range (Loc,
1282 Low_Bound => Make_Real_Literal (Loc, Ureal_1),
1283 High_Bound => Make_Real_Literal (Loc, Ureal_1)));
1285 Set_Is_Generic_Type (Base);
1286 Set_Parent (Base, Parent (Def));
1288 Set_Ekind (T, E_Decimal_Fixed_Point_Subtype);
1289 Set_Etype (T, Base);
1290 Set_Size_Info (T, Int_Base);
1291 Set_RM_Size (T, RM_Size (Int_Base));
1292 Set_First_Rep_Item (T, First_Rep_Item (Int_Base));
1293 Set_Digits_Value (T, Digs_Val);
1294 Set_Delta_Value (T, Delta_Val);
1295 Set_Small_Value (T, Delta_Val);
1296 Set_Scalar_Range (T, Scalar_Range (Base));
1297 Set_Is_Constrained (T);
1299 Check_Restriction (No_Fixed_Point, Def);
1300 end Analyze_Formal_Decimal_Fixed_Point_Type;
1302 ---------------------------------
1303 -- Analyze_Formal_Derived_Type --
1304 ---------------------------------
1306 procedure Analyze_Formal_Derived_Type
1307 (N : Node_Id;
1308 T : Entity_Id;
1309 Def : Node_Id)
1311 Loc : constant Source_Ptr := Sloc (Def);
1312 Unk_Disc : constant Boolean := Unknown_Discriminants_Present (N);
1313 New_N : Node_Id;
1315 begin
1316 Set_Is_Generic_Type (T);
1318 if Private_Present (Def) then
1319 New_N :=
1320 Make_Private_Extension_Declaration (Loc,
1321 Defining_Identifier => T,
1322 Discriminant_Specifications => Discriminant_Specifications (N),
1323 Unknown_Discriminants_Present => Unk_Disc,
1324 Subtype_Indication => Subtype_Mark (Def));
1326 Set_Abstract_Present (New_N, Abstract_Present (Def));
1328 else
1329 New_N :=
1330 Make_Full_Type_Declaration (Loc,
1331 Defining_Identifier => T,
1332 Discriminant_Specifications =>
1333 Discriminant_Specifications (Parent (T)),
1334 Type_Definition =>
1335 Make_Derived_Type_Definition (Loc,
1336 Subtype_Indication => Subtype_Mark (Def)));
1338 Set_Abstract_Present
1339 (Type_Definition (New_N), Abstract_Present (Def));
1340 end if;
1342 Rewrite (N, New_N);
1343 Analyze (N);
1345 if Unk_Disc then
1346 if not Is_Composite_Type (T) then
1347 Error_Msg_N
1348 ("unknown discriminants not allowed for elementary types", N);
1349 else
1350 Set_Has_Unknown_Discriminants (T);
1351 Set_Is_Constrained (T, False);
1352 end if;
1353 end if;
1355 -- If the parent type has a known size, so does the formal, which
1356 -- makes legal representation clauses that involve the formal.
1358 Set_Size_Known_At_Compile_Time
1359 (T, Size_Known_At_Compile_Time (Entity (Subtype_Mark (Def))));
1361 end Analyze_Formal_Derived_Type;
1363 ----------------------------------
1364 -- Analyze_Formal_Discrete_Type --
1365 ----------------------------------
1367 -- The operations defined for a discrete types are those of an
1368 -- enumeration type. The size is set to an arbitrary value, for use
1369 -- in analyzing the generic unit.
1371 procedure Analyze_Formal_Discrete_Type (T : Entity_Id; Def : Node_Id) is
1372 Loc : constant Source_Ptr := Sloc (Def);
1373 Lo : Node_Id;
1374 Hi : Node_Id;
1376 Base : constant Entity_Id :=
1377 New_Internal_Entity
1378 (E_Floating_Point_Type, Current_Scope, Sloc (Def), 'G');
1379 begin
1380 Enter_Name (T);
1381 Set_Ekind (T, E_Enumeration_Subtype);
1382 Set_Etype (T, Base);
1383 Init_Size (T, 8);
1384 Init_Alignment (T);
1385 Set_Is_Generic_Type (T);
1386 Set_Is_Constrained (T);
1388 -- For semantic analysis, the bounds of the type must be set to some
1389 -- non-static value. The simplest is to create attribute nodes for
1390 -- those bounds, that refer to the type itself. These bounds are never
1391 -- analyzed but serve as place-holders.
1393 Lo :=
1394 Make_Attribute_Reference (Loc,
1395 Attribute_Name => Name_First,
1396 Prefix => New_Reference_To (T, Loc));
1397 Set_Etype (Lo, T);
1399 Hi :=
1400 Make_Attribute_Reference (Loc,
1401 Attribute_Name => Name_Last,
1402 Prefix => New_Reference_To (T, Loc));
1403 Set_Etype (Hi, T);
1405 Set_Scalar_Range (T,
1406 Make_Range (Loc,
1407 Low_Bound => Lo,
1408 High_Bound => Hi));
1410 Set_Ekind (Base, E_Enumeration_Type);
1411 Set_Etype (Base, Base);
1412 Init_Size (Base, 8);
1413 Init_Alignment (Base);
1414 Set_Is_Generic_Type (Base);
1415 Set_Scalar_Range (Base, Scalar_Range (T));
1416 Set_Parent (Base, Parent (Def));
1418 end Analyze_Formal_Discrete_Type;
1420 ----------------------------------
1421 -- Analyze_Formal_Floating_Type --
1422 ---------------------------------
1424 procedure Analyze_Formal_Floating_Type (T : Entity_Id; Def : Node_Id) is
1425 Base : constant Entity_Id :=
1426 New_Internal_Entity
1427 (E_Floating_Point_Type, Current_Scope, Sloc (Def), 'G');
1429 begin
1430 -- The various semantic attributes are taken from the predefined type
1431 -- Float, just so that all of them are initialized. Their values are
1432 -- never used because no constant folding or expansion takes place in
1433 -- the generic itself.
1435 Enter_Name (T);
1436 Set_Ekind (T, E_Floating_Point_Subtype);
1437 Set_Etype (T, Base);
1438 Set_Size_Info (T, (Standard_Float));
1439 Set_RM_Size (T, RM_Size (Standard_Float));
1440 Set_Digits_Value (T, Digits_Value (Standard_Float));
1441 Set_Scalar_Range (T, Scalar_Range (Standard_Float));
1442 Set_Is_Constrained (T);
1444 Set_Is_Generic_Type (Base);
1445 Set_Etype (Base, Base);
1446 Set_Size_Info (Base, (Standard_Float));
1447 Set_RM_Size (Base, RM_Size (Standard_Float));
1448 Set_Digits_Value (Base, Digits_Value (Standard_Float));
1449 Set_Scalar_Range (Base, Scalar_Range (Standard_Float));
1450 Set_Parent (Base, Parent (Def));
1452 Check_Restriction (No_Floating_Point, Def);
1453 end Analyze_Formal_Floating_Type;
1455 ---------------------------------
1456 -- Analyze_Formal_Modular_Type --
1457 ---------------------------------
1459 procedure Analyze_Formal_Modular_Type (T : Entity_Id; Def : Node_Id) is
1460 begin
1461 -- Apart from their entity kind, generic modular types are treated
1462 -- like signed integer types, and have the same attributes.
1464 Analyze_Formal_Signed_Integer_Type (T, Def);
1465 Set_Ekind (T, E_Modular_Integer_Subtype);
1466 Set_Ekind (Etype (T), E_Modular_Integer_Type);
1468 end Analyze_Formal_Modular_Type;
1470 ---------------------------------------
1471 -- Analyze_Formal_Object_Declaration --
1472 ---------------------------------------
1474 procedure Analyze_Formal_Object_Declaration (N : Node_Id) is
1475 E : constant Node_Id := Expression (N);
1476 Id : constant Node_Id := Defining_Identifier (N);
1477 K : Entity_Kind;
1478 T : Node_Id;
1480 begin
1481 Enter_Name (Id);
1483 -- Determine the mode of the formal object
1485 if Out_Present (N) then
1486 K := E_Generic_In_Out_Parameter;
1488 if not In_Present (N) then
1489 Error_Msg_N ("formal generic objects cannot have mode OUT", N);
1490 end if;
1492 else
1493 K := E_Generic_In_Parameter;
1494 end if;
1496 Find_Type (Subtype_Mark (N));
1497 T := Entity (Subtype_Mark (N));
1499 if Ekind (T) = E_Incomplete_Type then
1500 Error_Msg_N ("premature usage of incomplete type", Subtype_Mark (N));
1501 end if;
1503 if K = E_Generic_In_Parameter then
1505 -- Ada 2005 (AI-287): Limited aggregates allowed in generic formals
1507 if Ada_Version < Ada_05 and then Is_Limited_Type (T) then
1508 Error_Msg_N
1509 ("generic formal of mode IN must not be of limited type", N);
1510 Explain_Limited_Type (T, N);
1511 end if;
1513 if Is_Abstract (T) then
1514 Error_Msg_N
1515 ("generic formal of mode IN must not be of abstract type", N);
1516 end if;
1518 if Present (E) then
1519 Analyze_Per_Use_Expression (E, T);
1520 end if;
1522 Set_Ekind (Id, K);
1523 Set_Etype (Id, T);
1525 -- Case of generic IN OUT parameter
1527 else
1528 -- If the formal has an unconstrained type, construct its
1529 -- actual subtype, as is done for subprogram formals. In this
1530 -- fashion, all its uses can refer to specific bounds.
1532 Set_Ekind (Id, K);
1533 Set_Etype (Id, T);
1535 if (Is_Array_Type (T)
1536 and then not Is_Constrained (T))
1537 or else
1538 (Ekind (T) = E_Record_Type
1539 and then Has_Discriminants (T))
1540 then
1541 declare
1542 Non_Freezing_Ref : constant Node_Id :=
1543 New_Reference_To (Id, Sloc (Id));
1544 Decl : Node_Id;
1546 begin
1547 -- Make sure that the actual subtype doesn't generate
1548 -- bogus freezing.
1550 Set_Must_Not_Freeze (Non_Freezing_Ref);
1551 Decl := Build_Actual_Subtype (T, Non_Freezing_Ref);
1552 Insert_Before_And_Analyze (N, Decl);
1553 Set_Actual_Subtype (Id, Defining_Identifier (Decl));
1554 end;
1555 else
1556 Set_Actual_Subtype (Id, T);
1557 end if;
1559 if Present (E) then
1560 Error_Msg_N
1561 ("initialization not allowed for `IN OUT` formals", N);
1562 end if;
1563 end if;
1565 end Analyze_Formal_Object_Declaration;
1567 ----------------------------------------------
1568 -- Analyze_Formal_Ordinary_Fixed_Point_Type --
1569 ----------------------------------------------
1571 procedure Analyze_Formal_Ordinary_Fixed_Point_Type
1572 (T : Entity_Id;
1573 Def : Node_Id)
1575 Loc : constant Source_Ptr := Sloc (Def);
1576 Base : constant Entity_Id :=
1577 New_Internal_Entity
1578 (E_Ordinary_Fixed_Point_Type, Current_Scope, Sloc (Def), 'G');
1579 begin
1580 -- The semantic attributes are set for completeness only, their
1581 -- values will never be used, because all properties of the type
1582 -- are non-static.
1584 Enter_Name (T);
1585 Set_Ekind (T, E_Ordinary_Fixed_Point_Subtype);
1586 Set_Etype (T, Base);
1587 Set_Size_Info (T, Standard_Integer);
1588 Set_RM_Size (T, RM_Size (Standard_Integer));
1589 Set_Small_Value (T, Ureal_1);
1590 Set_Delta_Value (T, Ureal_1);
1591 Set_Scalar_Range (T,
1592 Make_Range (Loc,
1593 Low_Bound => Make_Real_Literal (Loc, Ureal_1),
1594 High_Bound => Make_Real_Literal (Loc, Ureal_1)));
1595 Set_Is_Constrained (T);
1597 Set_Is_Generic_Type (Base);
1598 Set_Etype (Base, Base);
1599 Set_Size_Info (Base, Standard_Integer);
1600 Set_RM_Size (Base, RM_Size (Standard_Integer));
1601 Set_Small_Value (Base, Ureal_1);
1602 Set_Delta_Value (Base, Ureal_1);
1603 Set_Scalar_Range (Base, Scalar_Range (T));
1604 Set_Parent (Base, Parent (Def));
1606 Check_Restriction (No_Fixed_Point, Def);
1607 end Analyze_Formal_Ordinary_Fixed_Point_Type;
1609 ----------------------------
1610 -- Analyze_Formal_Package --
1611 ----------------------------
1613 procedure Analyze_Formal_Package (N : Node_Id) is
1614 Loc : constant Source_Ptr := Sloc (N);
1615 Pack_Id : constant Entity_Id := Defining_Identifier (N);
1616 Formal : Entity_Id;
1617 Gen_Id : constant Node_Id := Name (N);
1618 Gen_Decl : Node_Id;
1619 Gen_Unit : Entity_Id;
1620 New_N : Node_Id;
1621 Parent_Installed : Boolean := False;
1622 Renaming : Node_Id;
1623 Parent_Instance : Entity_Id;
1624 Renaming_In_Par : Entity_Id;
1626 begin
1627 Text_IO_Kludge (Gen_Id);
1629 Init_Env;
1630 Check_Generic_Child_Unit (Gen_Id, Parent_Installed);
1631 Gen_Unit := Entity (Gen_Id);
1633 if Ekind (Gen_Unit) /= E_Generic_Package then
1634 Error_Msg_N ("expect generic package name", Gen_Id);
1635 Restore_Env;
1636 return;
1638 elsif Gen_Unit = Current_Scope then
1639 Error_Msg_N
1640 ("generic package cannot be used as a formal package of itself",
1641 Gen_Id);
1642 Restore_Env;
1643 return;
1645 elsif In_Open_Scopes (Gen_Unit) then
1646 if Is_Compilation_Unit (Gen_Unit)
1647 and then Is_Child_Unit (Current_Scope)
1648 then
1649 -- Special-case the error when the formal is a parent, and
1650 -- continue analysis to minimize cascaded errors.
1652 Error_Msg_N
1653 ("generic parent cannot be used as formal package "
1654 & "of a child unit",
1655 Gen_Id);
1657 else
1658 Error_Msg_N
1659 ("generic package cannot be used as a formal package "
1660 & "within itself",
1661 Gen_Id);
1662 Restore_Env;
1663 return;
1664 end if;
1665 end if;
1667 -- Check for a formal package that is a package renaming
1669 if Present (Renamed_Object (Gen_Unit)) then
1670 Gen_Unit := Renamed_Object (Gen_Unit);
1671 end if;
1673 -- The formal package is treated like a regular instance, but only
1674 -- the specification needs to be instantiated, to make entities visible.
1676 if not Box_Present (N) then
1677 Hidden_Entities := New_Elmt_List;
1678 Analyze_Package_Instantiation (N);
1680 if Parent_Installed then
1681 Remove_Parent;
1682 end if;
1684 else
1685 -- If there are no generic associations, the generic parameters
1686 -- appear as local entities and are instantiated like them. We copy
1687 -- the generic package declaration as if it were an instantiation,
1688 -- and analyze it like a regular package, except that we treat the
1689 -- formals as additional visible components.
1691 Gen_Decl := Unit_Declaration_Node (Gen_Unit);
1693 if In_Extended_Main_Source_Unit (N) then
1694 Set_Is_Instantiated (Gen_Unit);
1695 Generate_Reference (Gen_Unit, N);
1696 end if;
1698 Formal := New_Copy (Pack_Id);
1699 Create_Instantiation_Source (N, Gen_Unit, False, S_Adjustment);
1701 New_N :=
1702 Copy_Generic_Node
1703 (Original_Node (Gen_Decl), Empty, Instantiating => True);
1704 Rewrite (N, New_N);
1705 Set_Defining_Unit_Name (Specification (New_N), Formal);
1706 Set_Instance_Env (Gen_Unit, Formal);
1708 Enter_Name (Formal);
1709 Set_Ekind (Formal, E_Generic_Package);
1710 Set_Etype (Formal, Standard_Void_Type);
1711 Set_Inner_Instances (Formal, New_Elmt_List);
1712 New_Scope (Formal);
1714 -- Within the formal, the name of the generic package is a renaming
1715 -- of the formal (as for a regular instantiation).
1717 Renaming := Make_Package_Renaming_Declaration (Loc,
1718 Defining_Unit_Name =>
1719 Make_Defining_Identifier (Loc, Chars (Gen_Unit)),
1720 Name => New_Reference_To (Formal, Loc));
1722 if Present (Visible_Declarations (Specification (N))) then
1723 Prepend (Renaming, To => Visible_Declarations (Specification (N)));
1724 elsif Present (Private_Declarations (Specification (N))) then
1725 Prepend (Renaming, To => Private_Declarations (Specification (N)));
1726 end if;
1728 if Is_Child_Unit (Gen_Unit)
1729 and then Parent_Installed
1730 then
1731 -- Similarly, we have to make the name of the formal visible in
1732 -- the parent instance, to resolve properly fully qualified names
1733 -- that may appear in the generic unit. The parent instance has
1734 -- been placed on the scope stack ahead of the current scope.
1736 Parent_Instance := Scope_Stack.Table (Scope_Stack.Last - 1).Entity;
1738 Renaming_In_Par :=
1739 Make_Defining_Identifier (Loc, Chars (Gen_Unit));
1740 Set_Ekind (Renaming_In_Par, E_Package);
1741 Set_Etype (Renaming_In_Par, Standard_Void_Type);
1742 Set_Scope (Renaming_In_Par, Parent_Instance);
1743 Set_Parent (Renaming_In_Par, Parent (Formal));
1744 Set_Renamed_Object (Renaming_In_Par, Formal);
1745 Append_Entity (Renaming_In_Par, Parent_Instance);
1746 end if;
1748 Analyze_Generic_Formal_Part (N);
1749 Analyze (Specification (N));
1750 End_Package_Scope (Formal);
1752 if Parent_Installed then
1753 Remove_Parent;
1754 end if;
1756 Restore_Env;
1758 -- Inside the generic unit, the formal package is a regular
1759 -- package, but no body is needed for it. Note that after
1760 -- instantiation, the defining_unit_name we need is in the
1761 -- new tree and not in the original. (see Package_Instantiation).
1762 -- A generic formal package is an instance, and can be used as
1763 -- an actual for an inner instance. Mark its generic parent.
1765 Set_Ekind (Formal, E_Package);
1766 Set_Generic_Parent (Specification (N), Gen_Unit);
1767 Set_Has_Completion (Formal, True);
1769 Set_Ekind (Pack_Id, E_Package);
1770 Set_Etype (Pack_Id, Standard_Void_Type);
1771 Set_Scope (Pack_Id, Scope (Formal));
1772 Set_Has_Completion (Pack_Id, True);
1773 end if;
1774 end Analyze_Formal_Package;
1776 ---------------------------------
1777 -- Analyze_Formal_Private_Type --
1778 ---------------------------------
1780 procedure Analyze_Formal_Private_Type
1781 (N : Node_Id;
1782 T : Entity_Id;
1783 Def : Node_Id)
1785 begin
1786 New_Private_Type (N, T, Def);
1788 -- Set the size to an arbitrary but legal value
1790 Set_Size_Info (T, Standard_Integer);
1791 Set_RM_Size (T, RM_Size (Standard_Integer));
1792 end Analyze_Formal_Private_Type;
1794 ----------------------------------------
1795 -- Analyze_Formal_Signed_Integer_Type --
1796 ----------------------------------------
1798 procedure Analyze_Formal_Signed_Integer_Type
1799 (T : Entity_Id;
1800 Def : Node_Id)
1802 Base : constant Entity_Id :=
1803 New_Internal_Entity
1804 (E_Signed_Integer_Type, Current_Scope, Sloc (Def), 'G');
1806 begin
1807 Enter_Name (T);
1809 Set_Ekind (T, E_Signed_Integer_Subtype);
1810 Set_Etype (T, Base);
1811 Set_Size_Info (T, Standard_Integer);
1812 Set_RM_Size (T, RM_Size (Standard_Integer));
1813 Set_Scalar_Range (T, Scalar_Range (Standard_Integer));
1814 Set_Is_Constrained (T);
1816 Set_Is_Generic_Type (Base);
1817 Set_Size_Info (Base, Standard_Integer);
1818 Set_RM_Size (Base, RM_Size (Standard_Integer));
1819 Set_Etype (Base, Base);
1820 Set_Scalar_Range (Base, Scalar_Range (Standard_Integer));
1821 Set_Parent (Base, Parent (Def));
1822 end Analyze_Formal_Signed_Integer_Type;
1824 -------------------------------
1825 -- Analyze_Formal_Subprogram --
1826 -------------------------------
1828 procedure Analyze_Formal_Subprogram (N : Node_Id) is
1829 Spec : constant Node_Id := Specification (N);
1830 Def : constant Node_Id := Default_Name (N);
1831 Nam : constant Entity_Id := Defining_Unit_Name (Spec);
1832 Subp : Entity_Id;
1834 begin
1835 if Nam = Error then
1836 return;
1837 end if;
1839 if Nkind (Nam) = N_Defining_Program_Unit_Name then
1840 Error_Msg_N ("name of formal subprogram must be a direct name", Nam);
1841 return;
1842 end if;
1844 Analyze_Subprogram_Declaration (N);
1845 Set_Is_Formal_Subprogram (Nam);
1846 Set_Has_Completion (Nam);
1848 if Nkind (N) = N_Formal_Abstract_Subprogram_Declaration then
1849 Set_Is_Abstract (Nam);
1850 Set_Is_Dispatching_Operation (Nam);
1852 declare
1853 Ctrl_Type : constant Entity_Id := Find_Dispatching_Type (Nam);
1855 begin
1856 if not Present (Ctrl_Type) then
1857 Error_Msg_N
1858 ("abstract formal subprogram must have a controlling type",
1861 else
1862 Check_Controlling_Formals (Ctrl_Type, Nam);
1863 end if;
1864 end;
1865 end if;
1867 -- Default name is resolved at the point of instantiation
1869 if Box_Present (N) then
1870 null;
1872 -- Else default is bound at the point of generic declaration
1874 elsif Present (Def) then
1875 if Nkind (Def) = N_Operator_Symbol then
1876 Find_Direct_Name (Def);
1878 elsif Nkind (Def) /= N_Attribute_Reference then
1879 Analyze (Def);
1881 else
1882 -- For an attribute reference, analyze the prefix and verify
1883 -- that it has the proper profile for the subprogram.
1885 Analyze (Prefix (Def));
1886 Valid_Default_Attribute (Nam, Def);
1887 return;
1888 end if;
1890 -- Default name may be overloaded, in which case the interpretation
1891 -- with the correct profile must be selected, as for a renaming.
1893 if Etype (Def) = Any_Type then
1894 return;
1896 elsif Nkind (Def) = N_Selected_Component then
1897 Subp := Entity (Selector_Name (Def));
1899 if Ekind (Subp) /= E_Entry then
1900 Error_Msg_N ("expect valid subprogram name as default", Def);
1901 return;
1902 end if;
1904 elsif Nkind (Def) = N_Indexed_Component then
1906 if Nkind (Prefix (Def)) /= N_Selected_Component then
1907 Error_Msg_N ("expect valid subprogram name as default", Def);
1908 return;
1910 else
1911 Subp := Entity (Selector_Name (Prefix (Def)));
1913 if Ekind (Subp) /= E_Entry_Family then
1914 Error_Msg_N ("expect valid subprogram name as default", Def);
1915 return;
1916 end if;
1917 end if;
1919 elsif Nkind (Def) = N_Character_Literal then
1921 -- Needs some type checks: subprogram should be parameterless???
1923 Resolve (Def, (Etype (Nam)));
1925 elsif not Is_Entity_Name (Def)
1926 or else not Is_Overloadable (Entity (Def))
1927 then
1928 Error_Msg_N ("expect valid subprogram name as default", Def);
1929 return;
1931 elsif not Is_Overloaded (Def) then
1932 Subp := Entity (Def);
1934 if Subp = Nam then
1935 Error_Msg_N ("premature usage of formal subprogram", Def);
1937 elsif not Entity_Matches_Spec (Subp, Nam) then
1938 Error_Msg_N ("no visible entity matches specification", Def);
1939 end if;
1941 else
1942 declare
1943 I : Interp_Index;
1944 I1 : Interp_Index := 0;
1945 It : Interp;
1946 It1 : Interp;
1948 begin
1949 Subp := Any_Id;
1950 Get_First_Interp (Def, I, It);
1951 while Present (It.Nam) loop
1953 if Entity_Matches_Spec (It.Nam, Nam) then
1954 if Subp /= Any_Id then
1955 It1 := Disambiguate (Def, I1, I, Etype (Subp));
1957 if It1 = No_Interp then
1958 Error_Msg_N ("ambiguous default subprogram", Def);
1959 else
1960 Subp := It1.Nam;
1961 end if;
1963 exit;
1965 else
1966 I1 := I;
1967 Subp := It.Nam;
1968 end if;
1969 end if;
1971 Get_Next_Interp (I, It);
1972 end loop;
1973 end;
1975 if Subp /= Any_Id then
1976 Set_Entity (Def, Subp);
1978 if Subp = Nam then
1979 Error_Msg_N ("premature usage of formal subprogram", Def);
1981 elsif Ekind (Subp) /= E_Operator then
1982 Check_Mode_Conformant (Subp, Nam);
1983 end if;
1985 else
1986 Error_Msg_N ("no visible subprogram matches specification", N);
1987 end if;
1988 end if;
1989 end if;
1990 end Analyze_Formal_Subprogram;
1992 -------------------------------------
1993 -- Analyze_Formal_Type_Declaration --
1994 -------------------------------------
1996 procedure Analyze_Formal_Type_Declaration (N : Node_Id) is
1997 Def : constant Node_Id := Formal_Type_Definition (N);
1998 T : Entity_Id;
2000 begin
2001 T := Defining_Identifier (N);
2003 if Present (Discriminant_Specifications (N))
2004 and then Nkind (Def) /= N_Formal_Private_Type_Definition
2005 then
2006 Error_Msg_N
2007 ("discriminants not allowed for this formal type",
2008 Defining_Identifier (First (Discriminant_Specifications (N))));
2009 end if;
2011 -- Enter the new name, and branch to specific routine
2013 case Nkind (Def) is
2014 when N_Formal_Private_Type_Definition =>
2015 Analyze_Formal_Private_Type (N, T, Def);
2017 when N_Formal_Derived_Type_Definition =>
2018 Analyze_Formal_Derived_Type (N, T, Def);
2020 when N_Formal_Discrete_Type_Definition =>
2021 Analyze_Formal_Discrete_Type (T, Def);
2023 when N_Formal_Signed_Integer_Type_Definition =>
2024 Analyze_Formal_Signed_Integer_Type (T, Def);
2026 when N_Formal_Modular_Type_Definition =>
2027 Analyze_Formal_Modular_Type (T, Def);
2029 when N_Formal_Floating_Point_Definition =>
2030 Analyze_Formal_Floating_Type (T, Def);
2032 when N_Formal_Ordinary_Fixed_Point_Definition =>
2033 Analyze_Formal_Ordinary_Fixed_Point_Type (T, Def);
2035 when N_Formal_Decimal_Fixed_Point_Definition =>
2036 Analyze_Formal_Decimal_Fixed_Point_Type (T, Def);
2038 when N_Array_Type_Definition =>
2039 Analyze_Formal_Array_Type (T, Def);
2041 when N_Access_To_Object_Definition |
2042 N_Access_Function_Definition |
2043 N_Access_Procedure_Definition =>
2044 Analyze_Generic_Access_Type (T, Def);
2046 when N_Error =>
2047 null;
2049 when others =>
2050 raise Program_Error;
2052 end case;
2054 Set_Is_Generic_Type (T);
2055 end Analyze_Formal_Type_Declaration;
2057 ------------------------------------
2058 -- Analyze_Function_Instantiation --
2059 ------------------------------------
2061 procedure Analyze_Function_Instantiation (N : Node_Id) is
2062 begin
2063 Analyze_Subprogram_Instantiation (N, E_Function);
2064 end Analyze_Function_Instantiation;
2066 ---------------------------------
2067 -- Analyze_Generic_Access_Type --
2068 ---------------------------------
2070 procedure Analyze_Generic_Access_Type (T : Entity_Id; Def : Node_Id) is
2071 begin
2072 Enter_Name (T);
2074 if Nkind (Def) = N_Access_To_Object_Definition then
2075 Access_Type_Declaration (T, Def);
2077 if Is_Incomplete_Or_Private_Type (Designated_Type (T))
2078 and then No (Full_View (Designated_Type (T)))
2079 and then not Is_Generic_Type (Designated_Type (T))
2080 then
2081 Error_Msg_N ("premature usage of incomplete type", Def);
2083 elsif Is_Internal (Designated_Type (T)) then
2084 Error_Msg_N
2085 ("only a subtype mark is allowed in a formal", Def);
2086 end if;
2088 else
2089 Access_Subprogram_Declaration (T, Def);
2090 end if;
2091 end Analyze_Generic_Access_Type;
2093 ---------------------------------
2094 -- Analyze_Generic_Formal_Part --
2095 ---------------------------------
2097 procedure Analyze_Generic_Formal_Part (N : Node_Id) is
2098 Gen_Parm_Decl : Node_Id;
2100 begin
2101 -- The generic formals are processed in the scope of the generic
2102 -- unit, where they are immediately visible. The scope is installed
2103 -- by the caller.
2105 Gen_Parm_Decl := First (Generic_Formal_Declarations (N));
2107 while Present (Gen_Parm_Decl) loop
2108 Analyze (Gen_Parm_Decl);
2109 Next (Gen_Parm_Decl);
2110 end loop;
2112 Generate_Reference_To_Generic_Formals (Current_Scope);
2113 end Analyze_Generic_Formal_Part;
2115 ------------------------------------------
2116 -- Analyze_Generic_Package_Declaration --
2117 ------------------------------------------
2119 procedure Analyze_Generic_Package_Declaration (N : Node_Id) is
2120 Loc : constant Source_Ptr := Sloc (N);
2121 Id : Entity_Id;
2122 New_N : Node_Id;
2123 Save_Parent : Node_Id;
2124 Renaming : Node_Id;
2125 Decls : constant List_Id :=
2126 Visible_Declarations (Specification (N));
2127 Decl : Node_Id;
2129 begin
2130 -- We introduce a renaming of the enclosing package, to have a usable
2131 -- entity as the prefix of an expanded name for a local entity of the
2132 -- form Par.P.Q, where P is the generic package. This is because a local
2133 -- entity named P may hide it, so that the usual visibility rules in
2134 -- the instance will not resolve properly.
2136 Renaming :=
2137 Make_Package_Renaming_Declaration (Loc,
2138 Defining_Unit_Name =>
2139 Make_Defining_Identifier (Loc,
2140 Chars => New_External_Name (Chars (Defining_Entity (N)), "GH")),
2141 Name => Make_Identifier (Loc, Chars (Defining_Entity (N))));
2143 if Present (Decls) then
2144 Decl := First (Decls);
2145 while Present (Decl)
2146 and then Nkind (Decl) = N_Pragma
2147 loop
2148 Next (Decl);
2149 end loop;
2151 if Present (Decl) then
2152 Insert_Before (Decl, Renaming);
2153 else
2154 Append (Renaming, Visible_Declarations (Specification (N)));
2155 end if;
2157 else
2158 Set_Visible_Declarations (Specification (N), New_List (Renaming));
2159 end if;
2161 -- Create copy of generic unit, and save for instantiation.
2162 -- If the unit is a child unit, do not copy the specifications
2163 -- for the parent, which are not part of the generic tree.
2165 Save_Parent := Parent_Spec (N);
2166 Set_Parent_Spec (N, Empty);
2168 New_N := Copy_Generic_Node (N, Empty, Instantiating => False);
2169 Set_Parent_Spec (New_N, Save_Parent);
2170 Rewrite (N, New_N);
2171 Id := Defining_Entity (N);
2172 Generate_Definition (Id);
2174 -- Expansion is not applied to generic units
2176 Start_Generic;
2178 Enter_Name (Id);
2179 Set_Ekind (Id, E_Generic_Package);
2180 Set_Etype (Id, Standard_Void_Type);
2181 New_Scope (Id);
2182 Enter_Generic_Scope (Id);
2183 Set_Inner_Instances (Id, New_Elmt_List);
2185 Set_Categorization_From_Pragmas (N);
2186 Set_Is_Pure (Id, Is_Pure (Current_Scope));
2188 -- Link the declaration of the generic homonym in the generic copy
2189 -- to the package it renames, so that it is always resolved properly.
2191 Set_Generic_Homonym (Id, Defining_Unit_Name (Renaming));
2192 Set_Entity (Associated_Node (Name (Renaming)), Id);
2194 -- For a library unit, we have reconstructed the entity for the
2195 -- unit, and must reset it in the library tables.
2197 if Nkind (Parent (N)) = N_Compilation_Unit then
2198 Set_Cunit_Entity (Current_Sem_Unit, Id);
2199 end if;
2201 Analyze_Generic_Formal_Part (N);
2203 -- After processing the generic formals, analysis proceeds
2204 -- as for a non-generic package.
2206 Analyze (Specification (N));
2208 Validate_Categorization_Dependency (N, Id);
2210 End_Generic;
2212 End_Package_Scope (Id);
2213 Exit_Generic_Scope (Id);
2215 if Nkind (Parent (N)) /= N_Compilation_Unit then
2216 Move_Freeze_Nodes (Id, N, Visible_Declarations (Specification (N)));
2217 Move_Freeze_Nodes (Id, N, Private_Declarations (Specification (N)));
2218 Move_Freeze_Nodes (Id, N, Generic_Formal_Declarations (N));
2220 else
2221 Set_Body_Required (Parent (N), Unit_Requires_Body (Id));
2222 Validate_RT_RAT_Component (N);
2224 -- If this is a spec without a body, check that generic parameters
2225 -- are referenced.
2227 if not Body_Required (Parent (N)) then
2228 Check_References (Id);
2229 end if;
2230 end if;
2231 end Analyze_Generic_Package_Declaration;
2233 --------------------------------------------
2234 -- Analyze_Generic_Subprogram_Declaration --
2235 --------------------------------------------
2237 procedure Analyze_Generic_Subprogram_Declaration (N : Node_Id) is
2238 Spec : Node_Id;
2239 Id : Entity_Id;
2240 Formals : List_Id;
2241 New_N : Node_Id;
2242 Result_Type : Entity_Id;
2243 Save_Parent : Node_Id;
2245 begin
2246 -- Create copy of generic unit,and save for instantiation.
2247 -- If the unit is a child unit, do not copy the specifications
2248 -- for the parent, which are not part of the generic tree.
2250 Save_Parent := Parent_Spec (N);
2251 Set_Parent_Spec (N, Empty);
2253 New_N := Copy_Generic_Node (N, Empty, Instantiating => False);
2254 Set_Parent_Spec (New_N, Save_Parent);
2255 Rewrite (N, New_N);
2257 Spec := Specification (N);
2258 Id := Defining_Entity (Spec);
2259 Generate_Definition (Id);
2261 if Nkind (Id) = N_Defining_Operator_Symbol then
2262 Error_Msg_N
2263 ("operator symbol not allowed for generic subprogram", Id);
2264 end if;
2266 Start_Generic;
2268 Enter_Name (Id);
2270 Set_Scope_Depth_Value (Id, Scope_Depth (Current_Scope) + 1);
2271 New_Scope (Id);
2272 Enter_Generic_Scope (Id);
2273 Set_Inner_Instances (Id, New_Elmt_List);
2274 Set_Is_Pure (Id, Is_Pure (Current_Scope));
2276 Analyze_Generic_Formal_Part (N);
2278 Formals := Parameter_Specifications (Spec);
2280 if Present (Formals) then
2281 Process_Formals (Formals, Spec);
2282 end if;
2284 if Nkind (Spec) = N_Function_Specification then
2285 Set_Ekind (Id, E_Generic_Function);
2287 if Nkind (Result_Definition (Spec)) = N_Access_Definition then
2288 Result_Type := Access_Definition (Spec, Result_Definition (Spec));
2289 Set_Etype (Id, Result_Type);
2290 else
2291 Find_Type (Result_Definition (Spec));
2292 Set_Etype (Id, Entity (Result_Definition (Spec)));
2293 end if;
2295 else
2296 Set_Ekind (Id, E_Generic_Procedure);
2297 Set_Etype (Id, Standard_Void_Type);
2298 end if;
2300 -- For a library unit, we have reconstructed the entity for the unit,
2301 -- and must reset it in the library tables. We also make sure that
2302 -- Body_Required is set properly in the original compilation unit node.
2304 if Nkind (Parent (N)) = N_Compilation_Unit then
2305 Set_Cunit_Entity (Current_Sem_Unit, Id);
2306 Set_Body_Required (Parent (N), Unit_Requires_Body (Id));
2307 end if;
2309 Set_Categorization_From_Pragmas (N);
2310 Validate_Categorization_Dependency (N, Id);
2312 Save_Global_References (Original_Node (N));
2314 End_Generic;
2315 End_Scope;
2316 Exit_Generic_Scope (Id);
2317 Generate_Reference_To_Formals (Id);
2318 end Analyze_Generic_Subprogram_Declaration;
2320 -----------------------------------
2321 -- Analyze_Package_Instantiation --
2322 -----------------------------------
2324 -- Note: this procedure is also used for formal package declarations, in
2325 -- which case the argument N is an N_Formal_Package_Declaration node.
2326 -- This should really be noted in the spec! ???
2328 procedure Analyze_Package_Instantiation (N : Node_Id) is
2329 Loc : constant Source_Ptr := Sloc (N);
2330 Gen_Id : constant Node_Id := Name (N);
2332 Act_Decl : Node_Id;
2333 Act_Decl_Name : Node_Id;
2334 Act_Decl_Id : Entity_Id;
2335 Act_Spec : Node_Id;
2336 Act_Tree : Node_Id;
2338 Gen_Decl : Node_Id;
2339 Gen_Unit : Entity_Id;
2341 Is_Actual_Pack : constant Boolean :=
2342 Is_Internal (Defining_Entity (N));
2344 Env_Installed : Boolean := False;
2345 Parent_Installed : Boolean := False;
2346 Renaming_List : List_Id;
2347 Unit_Renaming : Node_Id;
2348 Needs_Body : Boolean;
2349 Inline_Now : Boolean := False;
2351 procedure Delay_Descriptors (E : Entity_Id);
2352 -- Delay generation of subprogram descriptors for given entity
2354 function Might_Inline_Subp return Boolean;
2355 -- If inlining is active and the generic contains inlined subprograms,
2356 -- we instantiate the body. This may cause superfluous instantiations,
2357 -- but it is simpler than detecting the need for the body at the point
2358 -- of inlining, when the context of the instance is not available.
2360 -----------------------
2361 -- Delay_Descriptors --
2362 -----------------------
2364 procedure Delay_Descriptors (E : Entity_Id) is
2365 begin
2366 if not Delay_Subprogram_Descriptors (E) then
2367 Set_Delay_Subprogram_Descriptors (E);
2368 Pending_Descriptor.Increment_Last;
2369 Pending_Descriptor.Table (Pending_Descriptor.Last) := E;
2370 end if;
2371 end Delay_Descriptors;
2373 -----------------------
2374 -- Might_Inline_Subp --
2375 -----------------------
2377 function Might_Inline_Subp return Boolean is
2378 E : Entity_Id;
2380 begin
2381 if not Inline_Processing_Required then
2382 return False;
2384 else
2385 E := First_Entity (Gen_Unit);
2386 while Present (E) loop
2387 if Is_Subprogram (E)
2388 and then Is_Inlined (E)
2389 then
2390 return True;
2391 end if;
2393 Next_Entity (E);
2394 end loop;
2395 end if;
2397 return False;
2398 end Might_Inline_Subp;
2400 -- Start of processing for Analyze_Package_Instantiation
2402 begin
2403 -- Very first thing: apply the special kludge for Text_IO processing
2404 -- in case we are instantiating one of the children of [Wide_]Text_IO.
2406 Text_IO_Kludge (Name (N));
2408 -- Make node global for error reporting
2410 Instantiation_Node := N;
2412 -- Case of instantiation of a generic package
2414 if Nkind (N) = N_Package_Instantiation then
2415 Act_Decl_Id := New_Copy (Defining_Entity (N));
2416 Set_Comes_From_Source (Act_Decl_Id, True);
2418 if Nkind (Defining_Unit_Name (N)) = N_Defining_Program_Unit_Name then
2419 Act_Decl_Name :=
2420 Make_Defining_Program_Unit_Name (Loc,
2421 Name => New_Copy_Tree (Name (Defining_Unit_Name (N))),
2422 Defining_Identifier => Act_Decl_Id);
2423 else
2424 Act_Decl_Name := Act_Decl_Id;
2425 end if;
2427 -- Case of instantiation of a formal package
2429 else
2430 Act_Decl_Id := Defining_Identifier (N);
2431 Act_Decl_Name := Act_Decl_Id;
2432 end if;
2434 Generate_Definition (Act_Decl_Id);
2435 Pre_Analyze_Actuals (N);
2437 Init_Env;
2438 Env_Installed := True;
2439 Check_Generic_Child_Unit (Gen_Id, Parent_Installed);
2440 Gen_Unit := Entity (Gen_Id);
2442 -- Verify that it is the name of a generic package
2444 if Etype (Gen_Unit) = Any_Type then
2445 Restore_Env;
2446 return;
2448 elsif Ekind (Gen_Unit) /= E_Generic_Package then
2450 -- Ada 2005 (AI-50217): Cannot use instance in limited with_clause
2452 if From_With_Type (Gen_Unit) then
2453 Error_Msg_N
2454 ("cannot instantiate a limited withed package", Gen_Id);
2455 else
2456 Error_Msg_N
2457 ("expect name of generic package in instantiation", Gen_Id);
2458 end if;
2460 Restore_Env;
2461 return;
2462 end if;
2464 if In_Extended_Main_Source_Unit (N) then
2465 Set_Is_Instantiated (Gen_Unit);
2466 Generate_Reference (Gen_Unit, N);
2468 if Present (Renamed_Object (Gen_Unit)) then
2469 Set_Is_Instantiated (Renamed_Object (Gen_Unit));
2470 Generate_Reference (Renamed_Object (Gen_Unit), N);
2471 end if;
2472 end if;
2474 if Nkind (Gen_Id) = N_Identifier
2475 and then Chars (Gen_Unit) = Chars (Defining_Entity (N))
2476 then
2477 Error_Msg_NE
2478 ("& is hidden within declaration of instance", Gen_Id, Gen_Unit);
2480 elsif Nkind (Gen_Id) = N_Expanded_Name
2481 and then Is_Child_Unit (Gen_Unit)
2482 and then Nkind (Prefix (Gen_Id)) = N_Identifier
2483 and then Chars (Act_Decl_Id) = Chars (Prefix (Gen_Id))
2484 then
2485 Error_Msg_N
2486 ("& is hidden within declaration of instance ", Prefix (Gen_Id));
2487 end if;
2489 Set_Entity (Gen_Id, Gen_Unit);
2491 -- If generic is a renaming, get original generic unit
2493 if Present (Renamed_Object (Gen_Unit))
2494 and then Ekind (Renamed_Object (Gen_Unit)) = E_Generic_Package
2495 then
2496 Gen_Unit := Renamed_Object (Gen_Unit);
2497 end if;
2499 -- Verify that there are no circular instantiations
2501 if In_Open_Scopes (Gen_Unit) then
2502 Error_Msg_NE ("instantiation of & within itself", N, Gen_Unit);
2503 Restore_Env;
2504 return;
2506 elsif Contains_Instance_Of (Gen_Unit, Current_Scope, Gen_Id) then
2507 Error_Msg_Node_2 := Current_Scope;
2508 Error_Msg_NE
2509 ("circular Instantiation: & instantiated in &!", N, Gen_Unit);
2510 Circularity_Detected := True;
2511 Restore_Env;
2512 return;
2514 else
2515 Set_Instance_Env (Gen_Unit, Act_Decl_Id);
2516 Gen_Decl := Unit_Declaration_Node (Gen_Unit);
2518 -- Initialize renamings map, for error checking, and the list
2519 -- that holds private entities whose views have changed between
2520 -- generic definition and instantiation. If this is the instance
2521 -- created to validate an actual package, the instantiation
2522 -- environment is that of the enclosing instance.
2524 Generic_Renamings.Set_Last (0);
2525 Generic_Renamings_HTable.Reset;
2527 Create_Instantiation_Source (N, Gen_Unit, False, S_Adjustment);
2529 -- Copy original generic tree, to produce text for instantiation
2531 Act_Tree :=
2532 Copy_Generic_Node
2533 (Original_Node (Gen_Decl), Empty, Instantiating => True);
2535 Act_Spec := Specification (Act_Tree);
2537 -- If this is the instance created to validate an actual package,
2538 -- only the formals matter, do not examine the package spec itself.
2540 if Is_Actual_Pack then
2541 Set_Visible_Declarations (Act_Spec, New_List);
2542 Set_Private_Declarations (Act_Spec, New_List);
2543 end if;
2545 Renaming_List :=
2546 Analyze_Associations
2548 Generic_Formal_Declarations (Act_Tree),
2549 Generic_Formal_Declarations (Gen_Decl));
2551 Set_Defining_Unit_Name (Act_Spec, Act_Decl_Name);
2552 Set_Is_Generic_Instance (Act_Decl_Id);
2554 Set_Generic_Parent (Act_Spec, Gen_Unit);
2556 -- References to the generic in its own declaration or its body
2557 -- are references to the instance. Add a renaming declaration for
2558 -- the generic unit itself. This declaration, as well as the renaming
2559 -- declarations for the generic formals, must remain private to the
2560 -- unit: the formals, because this is the language semantics, and
2561 -- the unit because its use is an artifact of the implementation.
2563 Unit_Renaming :=
2564 Make_Package_Renaming_Declaration (Loc,
2565 Defining_Unit_Name =>
2566 Make_Defining_Identifier (Loc, Chars (Gen_Unit)),
2567 Name => New_Reference_To (Act_Decl_Id, Loc));
2569 Append (Unit_Renaming, Renaming_List);
2571 -- The renaming declarations are the first local declarations of
2572 -- the new unit.
2574 if Is_Non_Empty_List (Visible_Declarations (Act_Spec)) then
2575 Insert_List_Before
2576 (First (Visible_Declarations (Act_Spec)), Renaming_List);
2577 else
2578 Set_Visible_Declarations (Act_Spec, Renaming_List);
2579 end if;
2581 Act_Decl :=
2582 Make_Package_Declaration (Loc,
2583 Specification => Act_Spec);
2585 -- Save the instantiation node, for subsequent instantiation
2586 -- of the body, if there is one and we are generating code for
2587 -- the current unit. Mark the unit as having a body, to avoid
2588 -- a premature error message.
2590 -- We instantiate the body if we are generating code, if we are
2591 -- generating cross-reference information, or if we are building
2592 -- trees for ASIS use.
2594 declare
2595 Enclosing_Body_Present : Boolean := False;
2596 -- If the generic unit is not a compilation unit, then a body
2597 -- may be present in its parent even if none is required. We
2598 -- create a tentative pending instantiation for the body, which
2599 -- will be discarded if none is actually present.
2601 Scop : Entity_Id;
2603 begin
2604 if Scope (Gen_Unit) /= Standard_Standard
2605 and then not Is_Child_Unit (Gen_Unit)
2606 then
2607 Scop := Scope (Gen_Unit);
2609 while Present (Scop)
2610 and then Scop /= Standard_Standard
2611 loop
2612 if Unit_Requires_Body (Scop) then
2613 Enclosing_Body_Present := True;
2614 exit;
2616 elsif In_Open_Scopes (Scop)
2617 and then In_Package_Body (Scop)
2618 then
2619 Enclosing_Body_Present := True;
2620 exit;
2621 end if;
2623 exit when Is_Compilation_Unit (Scop);
2624 Scop := Scope (Scop);
2625 end loop;
2626 end if;
2628 -- If front-end inlining is enabled, and this is a unit for which
2629 -- code will be generated, we instantiate the body at once.
2630 -- This is done if the instance is not the main unit, and if the
2631 -- generic is not a child unit of another generic, to avoid scope
2632 -- problems and the reinstallation of parent instances.
2634 if Expander_Active
2635 and then (not Is_Child_Unit (Gen_Unit)
2636 or else not Is_Generic_Unit (Scope (Gen_Unit)))
2637 and then Might_Inline_Subp
2638 and then not Is_Actual_Pack
2639 then
2640 if Front_End_Inlining
2641 and then (Is_In_Main_Unit (N)
2642 or else In_Main_Context (Current_Scope))
2643 and then Nkind (Parent (N)) /= N_Compilation_Unit
2644 then
2645 Inline_Now := True;
2647 -- In configurable_run_time mode we force the inlining of
2648 -- predefined subprogram marked Inline_Always, to minimize
2649 -- the use of the run-time library.
2651 elsif Is_Predefined_File_Name
2652 (Unit_File_Name (Get_Source_Unit (Gen_Decl)))
2653 and then Configurable_Run_Time_Mode
2654 and then Nkind (Parent (N)) /= N_Compilation_Unit
2655 then
2656 Inline_Now := True;
2657 end if;
2658 end if;
2660 Needs_Body :=
2661 (Unit_Requires_Body (Gen_Unit)
2662 or else Enclosing_Body_Present
2663 or else Present (Corresponding_Body (Gen_Decl)))
2664 and then (Is_In_Main_Unit (N)
2665 or else Might_Inline_Subp)
2666 and then not Is_Actual_Pack
2667 and then not Inline_Now
2668 and then (Operating_Mode = Generate_Code
2669 or else (Operating_Mode = Check_Semantics
2670 and then ASIS_Mode));
2672 -- If front_end_inlining is enabled, do not instantiate a
2673 -- body if within a generic context.
2675 if (Front_End_Inlining
2676 and then not Expander_Active)
2677 or else Is_Generic_Unit (Cunit_Entity (Main_Unit))
2678 then
2679 Needs_Body := False;
2680 end if;
2682 -- If the current context is generic, and the package being
2683 -- instantiated is declared within a formal package, there is no
2684 -- body to instantiate until the enclosing generic is instantiated
2685 -- and there is an actual for the formal package. If the formal
2686 -- package has parameters, we build regular package instance for
2687 -- it, that preceeds the original formal package declaration.
2689 if In_Open_Scopes (Scope (Scope (Gen_Unit))) then
2690 declare
2691 Decl : constant Node_Id :=
2692 Original_Node
2693 (Unit_Declaration_Node (Scope (Gen_Unit)));
2694 begin
2695 if Nkind (Decl) = N_Formal_Package_Declaration
2696 or else (Nkind (Decl) = N_Package_Declaration
2697 and then Is_List_Member (Decl)
2698 and then Present (Next (Decl))
2699 and then
2700 Nkind (Next (Decl)) = N_Formal_Package_Declaration)
2701 then
2702 Needs_Body := False;
2703 end if;
2704 end;
2705 end if;
2706 end;
2708 -- If we are generating the calling stubs from the instantiation of
2709 -- a generic RCI package, we will not use the body of the generic
2710 -- package.
2712 if Distribution_Stub_Mode = Generate_Caller_Stub_Body
2713 and then Is_Compilation_Unit (Defining_Entity (N))
2714 then
2715 Needs_Body := False;
2716 end if;
2718 if Needs_Body then
2720 -- Here is a defence against a ludicrous number of instantiations
2721 -- caused by a circular set of instantiation attempts.
2723 if Pending_Instantiations.Last >
2724 Hostparm.Max_Instantiations
2725 then
2726 Error_Msg_N ("too many instantiations", N);
2727 raise Unrecoverable_Error;
2728 end if;
2730 -- Indicate that the enclosing scopes contain an instantiation,
2731 -- and that cleanup actions should be delayed until after the
2732 -- instance body is expanded.
2734 Check_Forward_Instantiation (Gen_Decl);
2735 if Nkind (N) = N_Package_Instantiation then
2736 declare
2737 Enclosing_Master : Entity_Id := Current_Scope;
2739 begin
2740 while Enclosing_Master /= Standard_Standard loop
2742 if Ekind (Enclosing_Master) = E_Package then
2743 if Is_Compilation_Unit (Enclosing_Master) then
2744 if In_Package_Body (Enclosing_Master) then
2745 Delay_Descriptors
2746 (Body_Entity (Enclosing_Master));
2747 else
2748 Delay_Descriptors
2749 (Enclosing_Master);
2750 end if;
2752 exit;
2754 else
2755 Enclosing_Master := Scope (Enclosing_Master);
2756 end if;
2758 elsif Ekind (Enclosing_Master) = E_Generic_Package then
2759 Enclosing_Master := Scope (Enclosing_Master);
2761 elsif Is_Generic_Subprogram (Enclosing_Master)
2762 or else Ekind (Enclosing_Master) = E_Void
2763 then
2764 -- Cleanup actions will eventually be performed on
2765 -- the enclosing instance, if any. enclosing scope
2766 -- is void in the formal part of a generic subp.
2768 exit;
2770 else
2771 if Ekind (Enclosing_Master) = E_Entry
2772 and then
2773 Ekind (Scope (Enclosing_Master)) = E_Protected_Type
2774 then
2775 Enclosing_Master :=
2776 Protected_Body_Subprogram (Enclosing_Master);
2777 end if;
2779 Set_Delay_Cleanups (Enclosing_Master);
2781 while Ekind (Enclosing_Master) = E_Block loop
2782 Enclosing_Master := Scope (Enclosing_Master);
2783 end loop;
2785 if Is_Subprogram (Enclosing_Master) then
2786 Delay_Descriptors (Enclosing_Master);
2788 elsif Is_Task_Type (Enclosing_Master) then
2789 declare
2790 TBP : constant Node_Id :=
2791 Get_Task_Body_Procedure
2792 (Enclosing_Master);
2794 begin
2795 if Present (TBP) then
2796 Delay_Descriptors (TBP);
2797 Set_Delay_Cleanups (TBP);
2798 end if;
2799 end;
2800 end if;
2802 exit;
2803 end if;
2804 end loop;
2805 end;
2807 -- Make entry in table
2809 Pending_Instantiations.Increment_Last;
2810 Pending_Instantiations.Table (Pending_Instantiations.Last) :=
2811 (N, Act_Decl, Expander_Active, Current_Sem_Unit);
2812 end if;
2813 end if;
2815 Set_Categorization_From_Pragmas (Act_Decl);
2817 if Parent_Installed then
2818 Hide_Current_Scope;
2819 end if;
2821 Set_Instance_Spec (N, Act_Decl);
2823 -- If not a compilation unit, insert the package declaration
2824 -- before the original instantiation node.
2826 if Nkind (Parent (N)) /= N_Compilation_Unit then
2827 Mark_Rewrite_Insertion (Act_Decl);
2828 Insert_Before (N, Act_Decl);
2829 Analyze (Act_Decl);
2831 -- For an instantiation that is a compilation unit, place
2832 -- declaration on current node so context is complete
2833 -- for analysis (including nested instantiations). It this
2834 -- is the main unit, the declaration eventually replaces the
2835 -- instantiation node. If the instance body is later created, it
2836 -- replaces the instance node, and the declation is attached to
2837 -- it (see Build_Instance_Compilation_Unit_Nodes).
2839 else
2840 if Cunit_Entity (Current_Sem_Unit) = Defining_Entity (N) then
2842 -- The entity for the current unit is the newly created one,
2843 -- and all semantic information is attached to it.
2845 Set_Cunit_Entity (Current_Sem_Unit, Act_Decl_Id);
2847 -- If this is the main unit, replace the main entity as well
2849 if Current_Sem_Unit = Main_Unit then
2850 Main_Unit_Entity := Act_Decl_Id;
2851 end if;
2852 end if;
2854 -- There is a problem with inlining here
2855 -- More comments needed??? what problem
2857 Set_Unit (Parent (N), Act_Decl);
2858 Set_Parent_Spec (Act_Decl, Parent_Spec (N));
2859 Analyze (Act_Decl);
2860 Set_Unit (Parent (N), N);
2861 Set_Body_Required (Parent (N), False);
2863 -- We never need elaboration checks on instantiations, since
2864 -- by definition, the body instantiation is elaborated at the
2865 -- same time as the spec instantiation.
2867 Set_Suppress_Elaboration_Warnings (Act_Decl_Id);
2868 Set_Kill_Elaboration_Checks (Act_Decl_Id);
2869 end if;
2871 Check_Elab_Instantiation (N);
2873 if ABE_Is_Certain (N) and then Needs_Body then
2874 Pending_Instantiations.Decrement_Last;
2875 end if;
2876 Check_Hidden_Child_Unit (N, Gen_Unit, Act_Decl_Id);
2878 Set_First_Private_Entity (Defining_Unit_Name (Unit_Renaming),
2879 First_Private_Entity (Act_Decl_Id));
2881 -- If the instantiation will receive a body, the unit will
2882 -- be transformed into a package body, and receive its own
2883 -- elaboration entity. Otherwise, the nature of the unit is
2884 -- now a package declaration.
2886 if Nkind (Parent (N)) = N_Compilation_Unit
2887 and then not Needs_Body
2888 then
2889 Rewrite (N, Act_Decl);
2890 end if;
2892 if Present (Corresponding_Body (Gen_Decl))
2893 or else Unit_Requires_Body (Gen_Unit)
2894 then
2895 Set_Has_Completion (Act_Decl_Id);
2896 end if;
2898 Check_Formal_Packages (Act_Decl_Id);
2900 Restore_Private_Views (Act_Decl_Id);
2902 if not Generic_Separately_Compiled (Gen_Unit) then
2903 Inherit_Context (Gen_Decl, N);
2904 end if;
2906 if Parent_Installed then
2907 Remove_Parent;
2908 end if;
2910 Restore_Env;
2911 Env_Installed := False;
2912 end if;
2914 Validate_Categorization_Dependency (N, Act_Decl_Id);
2916 -- Check restriction, but skip this if something went wrong in
2917 -- the above analysis, indicated by Act_Decl_Id being void.
2919 if Ekind (Act_Decl_Id) /= E_Void
2920 and then not Is_Library_Level_Entity (Act_Decl_Id)
2921 then
2922 Check_Restriction (No_Local_Allocators, N);
2923 end if;
2925 if Inline_Now then
2926 Inline_Instance_Body (N, Gen_Unit, Act_Decl);
2927 end if;
2929 -- The following is a tree patch for ASIS: ASIS needs separate nodes
2930 -- to be used as defining identifiers for a formal package and for the
2931 -- corresponding expanded package
2933 if Nkind (N) = N_Formal_Package_Declaration then
2934 Act_Decl_Id := New_Copy (Defining_Entity (N));
2935 Set_Comes_From_Source (Act_Decl_Id, True);
2936 Set_Is_Generic_Instance (Act_Decl_Id, False);
2937 Set_Defining_Identifier (N, Act_Decl_Id);
2938 end if;
2940 exception
2941 when Instantiation_Error =>
2942 if Parent_Installed then
2943 Remove_Parent;
2944 end if;
2946 if Env_Installed then
2947 Restore_Env;
2948 end if;
2949 end Analyze_Package_Instantiation;
2951 --------------------------
2952 -- Inline_Instance_Body --
2953 --------------------------
2955 procedure Inline_Instance_Body
2956 (N : Node_Id;
2957 Gen_Unit : Entity_Id;
2958 Act_Decl : Node_Id)
2960 Vis : Boolean;
2961 Gen_Comp : constant Entity_Id :=
2962 Cunit_Entity (Get_Source_Unit (Gen_Unit));
2963 Curr_Comp : constant Node_Id := Cunit (Current_Sem_Unit);
2964 Curr_Scope : Entity_Id := Empty;
2965 Curr_Unit : constant Entity_Id :=
2966 Cunit_Entity (Current_Sem_Unit);
2967 Removed : Boolean := False;
2968 Num_Scopes : Int := 0;
2969 Use_Clauses : array (1 .. Scope_Stack.Last) of Node_Id;
2970 Instances : array (1 .. Scope_Stack.Last) of Entity_Id;
2971 Inner_Scopes : array (1 .. Scope_Stack.Last) of Entity_Id;
2972 Num_Inner : Int := 0;
2973 N_Instances : Int := 0;
2974 S : Entity_Id;
2976 begin
2977 -- Case of generic unit defined in another unit. We must remove
2978 -- the complete context of the current unit to install that of
2979 -- the generic.
2981 if Gen_Comp /= Cunit_Entity (Current_Sem_Unit) then
2982 S := Current_Scope;
2984 while Present (S)
2985 and then S /= Standard_Standard
2986 loop
2987 Num_Scopes := Num_Scopes + 1;
2989 Use_Clauses (Num_Scopes) :=
2990 (Scope_Stack.Table
2991 (Scope_Stack.Last - Num_Scopes + 1).
2992 First_Use_Clause);
2993 End_Use_Clauses (Use_Clauses (Num_Scopes));
2995 exit when Is_Generic_Instance (S)
2996 and then (In_Package_Body (S)
2997 or else Ekind (S) = E_Procedure
2998 or else Ekind (S) = E_Function);
2999 S := Scope (S);
3000 end loop;
3002 Vis := Is_Immediately_Visible (Gen_Comp);
3004 -- Find and save all enclosing instances
3006 S := Current_Scope;
3008 while Present (S)
3009 and then S /= Standard_Standard
3010 loop
3011 if Is_Generic_Instance (S) then
3012 N_Instances := N_Instances + 1;
3013 Instances (N_Instances) := S;
3015 exit when In_Package_Body (S);
3016 end if;
3018 S := Scope (S);
3019 end loop;
3021 -- Remove context of current compilation unit, unless we
3022 -- are within a nested package instantiation, in which case
3023 -- the context has been removed previously.
3025 -- If current scope is the body of a child unit, remove context
3026 -- of spec as well.
3028 S := Current_Scope;
3030 while Present (S)
3031 and then S /= Standard_Standard
3032 loop
3033 exit when Is_Generic_Instance (S)
3034 and then (In_Package_Body (S)
3035 or else Ekind (S) = E_Procedure
3036 or else Ekind (S) = E_Function);
3038 if S = Curr_Unit
3039 or else (Ekind (Curr_Unit) = E_Package_Body
3040 and then S = Spec_Entity (Curr_Unit))
3041 or else (Ekind (Curr_Unit) = E_Subprogram_Body
3042 and then S =
3043 Corresponding_Spec
3044 (Unit_Declaration_Node (Curr_Unit)))
3045 then
3046 Removed := True;
3048 -- Remove entities in current scopes from visibility, so
3049 -- than instance body is compiled in a clean environment.
3051 Save_Scope_Stack (Handle_Use => False);
3053 if Is_Child_Unit (S) then
3055 -- Remove child unit from stack, as well as inner scopes.
3056 -- Removing the context of a child unit removes parent
3057 -- units as well.
3059 while Current_Scope /= S loop
3060 Num_Inner := Num_Inner + 1;
3061 Inner_Scopes (Num_Inner) := Current_Scope;
3062 Pop_Scope;
3063 end loop;
3065 Pop_Scope;
3066 Remove_Context (Curr_Comp);
3067 Curr_Scope := S;
3069 else
3070 Remove_Context (Curr_Comp);
3071 end if;
3073 if Ekind (Curr_Unit) = E_Package_Body then
3074 Remove_Context (Library_Unit (Curr_Comp));
3075 end if;
3076 end if;
3078 S := Scope (S);
3079 end loop;
3081 New_Scope (Standard_Standard);
3082 Scope_Stack.Table (Scope_Stack.Last).Is_Active_Stack_Base := True;
3083 Instantiate_Package_Body
3084 ((N, Act_Decl, Expander_Active, Current_Sem_Unit), True);
3085 Pop_Scope;
3087 -- Restore context
3089 Set_Is_Immediately_Visible (Gen_Comp, Vis);
3091 -- Reset Generic_Instance flag so that use clauses can be installed
3092 -- in the proper order. (See Use_One_Package for effect of enclosing
3093 -- instances on processing of use clauses).
3095 for J in 1 .. N_Instances loop
3096 Set_Is_Generic_Instance (Instances (J), False);
3097 end loop;
3099 if Removed then
3100 Install_Context (Curr_Comp);
3102 if Present (Curr_Scope)
3103 and then Is_Child_Unit (Curr_Scope)
3104 then
3105 New_Scope (Curr_Scope);
3106 Set_Is_Immediately_Visible (Curr_Scope);
3108 -- Finally, restore inner scopes as well
3110 for J in reverse 1 .. Num_Inner loop
3111 New_Scope (Inner_Scopes (J));
3112 end loop;
3113 end if;
3115 Restore_Scope_Stack (Handle_Use => False);
3117 if Present (Curr_Scope)
3118 and then
3119 (In_Private_Part (Curr_Scope)
3120 or else In_Package_Body (Curr_Scope))
3121 then
3122 -- Install private declaration of ancestor units, which
3123 -- are currently available. Restore_Scope_Stack and
3124 -- Install_Context only install the visible part of parents.
3126 declare
3127 Par : Entity_Id;
3128 begin
3129 Par := Scope (Curr_Scope);
3130 while (Present (Par))
3131 and then Par /= Standard_Standard
3132 loop
3133 Install_Private_Declarations (Par);
3134 Par := Scope (Par);
3135 end loop;
3136 end;
3137 end if;
3138 end if;
3140 -- Restore use clauses. For a child unit, use clauses in the parents
3141 -- are restored when installing the context, so only those in inner
3142 -- scopes (and those local to the child unit itself) need to be
3143 -- installed explicitly.
3145 if Is_Child_Unit (Curr_Unit)
3146 and then Removed
3147 then
3148 for J in reverse 1 .. Num_Inner + 1 loop
3149 Scope_Stack.Table (Scope_Stack.Last - J + 1).First_Use_Clause :=
3150 Use_Clauses (J);
3151 Install_Use_Clauses (Use_Clauses (J));
3152 end loop;
3154 else
3155 for J in reverse 1 .. Num_Scopes loop
3156 Scope_Stack.Table (Scope_Stack.Last - J + 1).First_Use_Clause :=
3157 Use_Clauses (J);
3158 Install_Use_Clauses (Use_Clauses (J));
3159 end loop;
3160 end if;
3162 for J in 1 .. N_Instances loop
3163 Set_Is_Generic_Instance (Instances (J), True);
3164 end loop;
3166 -- If generic unit is in current unit, current context is correct
3168 else
3169 Instantiate_Package_Body
3170 ((N, Act_Decl, Expander_Active, Current_Sem_Unit), True);
3171 end if;
3172 end Inline_Instance_Body;
3174 -------------------------------------
3175 -- Analyze_Procedure_Instantiation --
3176 -------------------------------------
3178 procedure Analyze_Procedure_Instantiation (N : Node_Id) is
3179 begin
3180 Analyze_Subprogram_Instantiation (N, E_Procedure);
3181 end Analyze_Procedure_Instantiation;
3183 --------------------------------------
3184 -- Analyze_Subprogram_Instantiation --
3185 --------------------------------------
3187 procedure Analyze_Subprogram_Instantiation
3188 (N : Node_Id;
3189 K : Entity_Kind)
3191 Loc : constant Source_Ptr := Sloc (N);
3192 Gen_Id : constant Node_Id := Name (N);
3194 Anon_Id : constant Entity_Id :=
3195 Make_Defining_Identifier (Sloc (Defining_Entity (N)),
3196 Chars => New_External_Name
3197 (Chars (Defining_Entity (N)), 'R'));
3199 Act_Decl_Id : Entity_Id;
3200 Act_Decl : Node_Id;
3201 Act_Spec : Node_Id;
3202 Act_Tree : Node_Id;
3204 Env_Installed : Boolean := False;
3205 Gen_Unit : Entity_Id;
3206 Gen_Decl : Node_Id;
3207 Pack_Id : Entity_Id;
3208 Parent_Installed : Boolean := False;
3209 Renaming_List : List_Id;
3211 procedure Analyze_Instance_And_Renamings;
3212 -- The instance must be analyzed in a context that includes the
3213 -- mappings of generic parameters into actuals. We create a package
3214 -- declaration for this purpose, and a subprogram with an internal
3215 -- name within the package. The subprogram instance is simply an
3216 -- alias for the internal subprogram, declared in the current scope.
3218 ------------------------------------
3219 -- Analyze_Instance_And_Renamings --
3220 ------------------------------------
3222 procedure Analyze_Instance_And_Renamings is
3223 Def_Ent : constant Entity_Id := Defining_Entity (N);
3224 Pack_Decl : Node_Id;
3226 begin
3227 if Nkind (Parent (N)) = N_Compilation_Unit then
3229 -- For the case of a compilation unit, the container package
3230 -- has the same name as the instantiation, to insure that the
3231 -- binder calls the elaboration procedure with the right name.
3232 -- Copy the entity of the instance, which may have compilation
3233 -- level flags (e.g. Is_Child_Unit) set.
3235 Pack_Id := New_Copy (Def_Ent);
3237 else
3238 -- Otherwise we use the name of the instantiation concatenated
3239 -- with its source position to ensure uniqueness if there are
3240 -- several instantiations with the same name.
3242 Pack_Id :=
3243 Make_Defining_Identifier (Loc,
3244 Chars => New_External_Name
3245 (Related_Id => Chars (Def_Ent),
3246 Suffix => "GP",
3247 Suffix_Index => Source_Offset (Sloc (Def_Ent))));
3248 end if;
3250 Pack_Decl := Make_Package_Declaration (Loc,
3251 Specification => Make_Package_Specification (Loc,
3252 Defining_Unit_Name => Pack_Id,
3253 Visible_Declarations => Renaming_List,
3254 End_Label => Empty));
3256 Set_Instance_Spec (N, Pack_Decl);
3257 Set_Is_Generic_Instance (Pack_Id);
3258 Set_Needs_Debug_Info (Pack_Id);
3260 -- Case of not a compilation unit
3262 if Nkind (Parent (N)) /= N_Compilation_Unit then
3263 Mark_Rewrite_Insertion (Pack_Decl);
3264 Insert_Before (N, Pack_Decl);
3265 Set_Has_Completion (Pack_Id);
3267 -- Case of an instantiation that is a compilation unit
3269 -- Place declaration on current node so context is complete
3270 -- for analysis (including nested instantiations), and for
3271 -- use in a context_clause (see Analyze_With_Clause).
3273 else
3274 Set_Unit (Parent (N), Pack_Decl);
3275 Set_Parent_Spec (Pack_Decl, Parent_Spec (N));
3276 end if;
3278 Analyze (Pack_Decl);
3279 Check_Formal_Packages (Pack_Id);
3280 Set_Is_Generic_Instance (Pack_Id, False);
3282 -- Body of the enclosing package is supplied when instantiating
3283 -- the subprogram body, after semantic analysis is completed.
3285 if Nkind (Parent (N)) = N_Compilation_Unit then
3287 -- Remove package itself from visibility, so it does not
3288 -- conflict with subprogram.
3290 Set_Name_Entity_Id (Chars (Pack_Id), Homonym (Pack_Id));
3292 -- Set name and scope of internal subprogram so that the
3293 -- proper external name will be generated. The proper scope
3294 -- is the scope of the wrapper package. We need to generate
3295 -- debugging information for the internal subprogram, so set
3296 -- flag accordingly.
3298 Set_Chars (Anon_Id, Chars (Defining_Entity (N)));
3299 Set_Scope (Anon_Id, Scope (Pack_Id));
3301 -- Mark wrapper package as referenced, to avoid spurious
3302 -- warnings if the instantiation appears in various with_
3303 -- clauses of subunits of the main unit.
3305 Set_Referenced (Pack_Id);
3306 end if;
3308 Set_Is_Generic_Instance (Anon_Id);
3309 Set_Needs_Debug_Info (Anon_Id);
3310 Act_Decl_Id := New_Copy (Anon_Id);
3312 Set_Parent (Act_Decl_Id, Parent (Anon_Id));
3313 Set_Chars (Act_Decl_Id, Chars (Defining_Entity (N)));
3314 Set_Sloc (Act_Decl_Id, Sloc (Defining_Entity (N)));
3315 Set_Comes_From_Source (Act_Decl_Id, True);
3317 -- The signature may involve types that are not frozen yet, but
3318 -- the subprogram will be frozen at the point the wrapper package
3319 -- is frozen, so it does not need its own freeze node. In fact, if
3320 -- one is created, it might conflict with the freezing actions from
3321 -- the wrapper package (see 7206-013).
3323 Set_Has_Delayed_Freeze (Anon_Id, False);
3325 -- If the instance is a child unit, mark the Id accordingly. Mark
3326 -- the anonymous entity as well, which is the real subprogram and
3327 -- which is used when the instance appears in a context clause.
3329 Set_Is_Child_Unit (Act_Decl_Id, Is_Child_Unit (Defining_Entity (N)));
3330 Set_Is_Child_Unit (Anon_Id, Is_Child_Unit (Defining_Entity (N)));
3331 New_Overloaded_Entity (Act_Decl_Id);
3332 Check_Eliminated (Act_Decl_Id);
3334 -- In compilation unit case, kill elaboration checks on the
3335 -- instantiation, since they are never needed -- the body is
3336 -- instantiated at the same point as the spec.
3338 if Nkind (Parent (N)) = N_Compilation_Unit then
3339 Set_Suppress_Elaboration_Warnings (Act_Decl_Id);
3340 Set_Kill_Elaboration_Checks (Act_Decl_Id);
3341 Set_Is_Compilation_Unit (Anon_Id);
3343 Set_Cunit_Entity (Current_Sem_Unit, Pack_Id);
3344 end if;
3346 -- The instance is not a freezing point for the new subprogram
3348 Set_Is_Frozen (Act_Decl_Id, False);
3350 if Nkind (Defining_Entity (N)) = N_Defining_Operator_Symbol then
3351 Valid_Operator_Definition (Act_Decl_Id);
3352 end if;
3354 Set_Alias (Act_Decl_Id, Anon_Id);
3355 Set_Parent (Act_Decl_Id, Parent (Anon_Id));
3356 Set_Has_Completion (Act_Decl_Id);
3357 Set_Related_Instance (Pack_Id, Act_Decl_Id);
3359 if Nkind (Parent (N)) = N_Compilation_Unit then
3360 Set_Body_Required (Parent (N), False);
3361 end if;
3363 end Analyze_Instance_And_Renamings;
3365 -- Start of processing for Analyze_Subprogram_Instantiation
3367 begin
3368 -- Very first thing: apply the special kludge for Text_IO processing
3369 -- in case we are instantiating one of the children of [Wide_]Text_IO.
3370 -- Of course such an instantiation is bogus (these are packages, not
3371 -- subprograms), but we get a better error message if we do this.
3373 Text_IO_Kludge (Gen_Id);
3375 -- Make node global for error reporting
3377 Instantiation_Node := N;
3378 Pre_Analyze_Actuals (N);
3380 Init_Env;
3381 Env_Installed := True;
3382 Check_Generic_Child_Unit (Gen_Id, Parent_Installed);
3383 Gen_Unit := Entity (Gen_Id);
3385 Generate_Reference (Gen_Unit, Gen_Id);
3387 if Nkind (Gen_Id) = N_Identifier
3388 and then Chars (Gen_Unit) = Chars (Defining_Entity (N))
3389 then
3390 Error_Msg_NE
3391 ("& is hidden within declaration of instance", Gen_Id, Gen_Unit);
3392 end if;
3394 if Etype (Gen_Unit) = Any_Type then
3395 Restore_Env;
3396 return;
3397 end if;
3399 -- Verify that it is a generic subprogram of the right kind, and that
3400 -- it does not lead to a circular instantiation.
3402 if Ekind (Gen_Unit) /= E_Generic_Procedure
3403 and then Ekind (Gen_Unit) /= E_Generic_Function
3404 then
3405 Error_Msg_N ("expect generic subprogram in instantiation", Gen_Id);
3407 elsif In_Open_Scopes (Gen_Unit) then
3408 Error_Msg_NE ("instantiation of & within itself", N, Gen_Unit);
3410 elsif K = E_Procedure
3411 and then Ekind (Gen_Unit) /= E_Generic_Procedure
3412 then
3413 if Ekind (Gen_Unit) = E_Generic_Function then
3414 Error_Msg_N
3415 ("cannot instantiate generic function as procedure", Gen_Id);
3416 else
3417 Error_Msg_N
3418 ("expect name of generic procedure in instantiation", Gen_Id);
3419 end if;
3421 elsif K = E_Function
3422 and then Ekind (Gen_Unit) /= E_Generic_Function
3423 then
3424 if Ekind (Gen_Unit) = E_Generic_Procedure then
3425 Error_Msg_N
3426 ("cannot instantiate generic procedure as function", Gen_Id);
3427 else
3428 Error_Msg_N
3429 ("expect name of generic function in instantiation", Gen_Id);
3430 end if;
3432 else
3433 Set_Entity (Gen_Id, Gen_Unit);
3434 Set_Is_Instantiated (Gen_Unit);
3436 if In_Extended_Main_Source_Unit (N) then
3437 Generate_Reference (Gen_Unit, N);
3438 end if;
3440 -- If renaming, get original unit
3442 if Present (Renamed_Object (Gen_Unit))
3443 and then (Ekind (Renamed_Object (Gen_Unit)) = E_Generic_Procedure
3444 or else
3445 Ekind (Renamed_Object (Gen_Unit)) = E_Generic_Function)
3446 then
3447 Gen_Unit := Renamed_Object (Gen_Unit);
3448 Set_Is_Instantiated (Gen_Unit);
3449 Generate_Reference (Gen_Unit, N);
3450 end if;
3452 if Contains_Instance_Of (Gen_Unit, Current_Scope, Gen_Id) then
3453 Error_Msg_Node_2 := Current_Scope;
3454 Error_Msg_NE
3455 ("circular Instantiation: & instantiated in &!", N, Gen_Unit);
3456 Circularity_Detected := True;
3457 return;
3458 end if;
3460 Gen_Decl := Unit_Declaration_Node (Gen_Unit);
3462 -- The subprogram itself cannot contain a nested instance, so
3463 -- the current parent is left empty.
3465 Set_Instance_Env (Gen_Unit, Empty);
3467 -- Initialize renamings map, for error checking
3469 Generic_Renamings.Set_Last (0);
3470 Generic_Renamings_HTable.Reset;
3472 Create_Instantiation_Source (N, Gen_Unit, False, S_Adjustment);
3474 -- Copy original generic tree, to produce text for instantiation
3476 Act_Tree :=
3477 Copy_Generic_Node
3478 (Original_Node (Gen_Decl), Empty, Instantiating => True);
3480 Act_Spec := Specification (Act_Tree);
3481 Renaming_List :=
3482 Analyze_Associations
3484 Generic_Formal_Declarations (Act_Tree),
3485 Generic_Formal_Declarations (Gen_Decl));
3487 -- Build the subprogram declaration, which does not appear
3488 -- in the generic template, and give it a sloc consistent
3489 -- with that of the template.
3491 Set_Defining_Unit_Name (Act_Spec, Anon_Id);
3492 Set_Generic_Parent (Act_Spec, Gen_Unit);
3493 Act_Decl :=
3494 Make_Subprogram_Declaration (Sloc (Act_Spec),
3495 Specification => Act_Spec);
3497 Set_Categorization_From_Pragmas (Act_Decl);
3499 if Parent_Installed then
3500 Hide_Current_Scope;
3501 end if;
3503 Append (Act_Decl, Renaming_List);
3504 Analyze_Instance_And_Renamings;
3506 -- If the generic is marked Import (Intrinsic), then so is the
3507 -- instance. This indicates that there is no body to instantiate.
3508 -- If generic is marked inline, so it the instance, and the
3509 -- anonymous subprogram it renames. If inlined, or else if inlining
3510 -- is enabled for the compilation, we generate the instance body
3511 -- even if it is not within the main unit.
3513 -- Any other pragmas might also be inherited ???
3515 if Is_Intrinsic_Subprogram (Gen_Unit) then
3516 Set_Is_Intrinsic_Subprogram (Anon_Id);
3517 Set_Is_Intrinsic_Subprogram (Act_Decl_Id);
3519 if Chars (Gen_Unit) = Name_Unchecked_Conversion then
3520 Validate_Unchecked_Conversion (N, Act_Decl_Id);
3521 end if;
3522 end if;
3524 Generate_Definition (Act_Decl_Id);
3526 Set_Is_Inlined (Act_Decl_Id, Is_Inlined (Gen_Unit));
3527 Set_Is_Inlined (Anon_Id, Is_Inlined (Gen_Unit));
3529 if not Is_Intrinsic_Subprogram (Gen_Unit) then
3530 Check_Elab_Instantiation (N);
3531 end if;
3533 if Is_Dispatching_Operation (Act_Decl_Id)
3534 and then Ada_Version >= Ada_05
3535 then
3536 declare
3537 Formal : Entity_Id;
3539 begin
3540 Formal := First_Formal (Act_Decl_Id);
3541 while Present (Formal) loop
3542 if Ekind (Etype (Formal)) = E_Anonymous_Access_Type
3543 and then Is_Controlling_Formal (Formal)
3544 and then not Can_Never_Be_Null (Formal)
3545 then
3546 Error_Msg_NE ("access parameter& is controlling,",
3547 N, Formal);
3548 Error_Msg_NE ("\corresponding parameter of & must be"
3549 & " explicitly null-excluding", N, Gen_Id);
3550 end if;
3552 Next_Formal (Formal);
3553 end loop;
3554 end;
3555 end if;
3557 Check_Hidden_Child_Unit (N, Gen_Unit, Act_Decl_Id);
3559 -- Subject to change, pending on if other pragmas are inherited ???
3561 Validate_Categorization_Dependency (N, Act_Decl_Id);
3563 if not Is_Intrinsic_Subprogram (Act_Decl_Id) then
3564 if not Generic_Separately_Compiled (Gen_Unit) then
3565 Inherit_Context (Gen_Decl, N);
3566 end if;
3568 Restore_Private_Views (Pack_Id, False);
3570 -- If the context requires a full instantiation, mark node for
3571 -- subsequent construction of the body.
3573 if (Is_In_Main_Unit (N)
3574 or else Is_Inlined (Act_Decl_Id))
3575 and then (Operating_Mode = Generate_Code
3576 or else (Operating_Mode = Check_Semantics
3577 and then ASIS_Mode))
3578 and then (Expander_Active or else ASIS_Mode)
3579 and then not ABE_Is_Certain (N)
3580 and then not Is_Eliminated (Act_Decl_Id)
3581 then
3582 Pending_Instantiations.Increment_Last;
3583 Pending_Instantiations.Table (Pending_Instantiations.Last) :=
3584 (N, Act_Decl, Expander_Active, Current_Sem_Unit);
3585 Check_Forward_Instantiation (Gen_Decl);
3587 -- The wrapper package is always delayed, because it does
3588 -- not constitute a freeze point, but to insure that the
3589 -- freeze node is placed properly, it is created directly
3590 -- when instantiating the body (otherwise the freeze node
3591 -- might appear to early for nested instantiations).
3593 elsif Nkind (Parent (N)) = N_Compilation_Unit then
3595 -- For ASIS purposes, indicate that the wrapper package has
3596 -- replaced the instantiation node.
3598 Rewrite (N, Unit (Parent (N)));
3599 Set_Unit (Parent (N), N);
3600 end if;
3602 elsif Nkind (Parent (N)) = N_Compilation_Unit then
3604 -- Replace instance node for library-level instantiations
3605 -- of intrinsic subprograms, for ASIS use.
3607 Rewrite (N, Unit (Parent (N)));
3608 Set_Unit (Parent (N), N);
3609 end if;
3611 if Parent_Installed then
3612 Remove_Parent;
3613 end if;
3615 Restore_Env;
3616 Env_Installed := False;
3617 Generic_Renamings.Set_Last (0);
3618 Generic_Renamings_HTable.Reset;
3619 end if;
3621 exception
3622 when Instantiation_Error =>
3623 if Parent_Installed then
3624 Remove_Parent;
3625 end if;
3627 if Env_Installed then
3628 Restore_Env;
3629 end if;
3630 end Analyze_Subprogram_Instantiation;
3632 -------------------------
3633 -- Get_Associated_Node --
3634 -------------------------
3636 function Get_Associated_Node (N : Node_Id) return Node_Id is
3637 Assoc : Node_Id := Associated_Node (N);
3639 begin
3640 if Nkind (Assoc) /= Nkind (N) then
3641 return Assoc;
3643 elsif Nkind (Assoc) = N_Aggregate
3644 or else Nkind (Assoc) = N_Extension_Aggregate
3645 then
3646 return Assoc;
3648 else
3649 -- If the node is part of an inner generic, it may itself have been
3650 -- remapped into a further generic copy. Associated_Node is otherwise
3651 -- used for the entity of the node, and will be of a different node
3652 -- kind, or else N has been rewritten as a literal or function call.
3654 while Present (Associated_Node (Assoc))
3655 and then Nkind (Associated_Node (Assoc)) = Nkind (Assoc)
3656 loop
3657 Assoc := Associated_Node (Assoc);
3658 end loop;
3660 -- Follow and additional link in case the final node was rewritten.
3661 -- This can only happen with nested generic units.
3663 if (Nkind (Assoc) = N_Identifier or else Nkind (Assoc) in N_Op)
3664 and then Present (Associated_Node (Assoc))
3665 and then (Nkind (Associated_Node (Assoc)) = N_Function_Call
3666 or else
3667 Nkind (Associated_Node (Assoc)) = N_Explicit_Dereference
3668 or else
3669 Nkind (Associated_Node (Assoc)) = N_Integer_Literal
3670 or else
3671 Nkind (Associated_Node (Assoc)) = N_Real_Literal
3672 or else
3673 Nkind (Associated_Node (Assoc)) = N_String_Literal)
3674 then
3675 Assoc := Associated_Node (Assoc);
3676 end if;
3678 return Assoc;
3679 end if;
3680 end Get_Associated_Node;
3682 -------------------------------------------
3683 -- Build_Instance_Compilation_Unit_Nodes --
3684 -------------------------------------------
3686 procedure Build_Instance_Compilation_Unit_Nodes
3687 (N : Node_Id;
3688 Act_Body : Node_Id;
3689 Act_Decl : Node_Id)
3691 Decl_Cunit : Node_Id;
3692 Body_Cunit : Node_Id;
3693 Citem : Node_Id;
3694 New_Main : constant Entity_Id := Defining_Entity (Act_Decl);
3695 Old_Main : constant Entity_Id := Cunit_Entity (Main_Unit);
3697 begin
3698 -- A new compilation unit node is built for the instance declaration
3700 Decl_Cunit :=
3701 Make_Compilation_Unit (Sloc (N),
3702 Context_Items => Empty_List,
3703 Unit => Act_Decl,
3704 Aux_Decls_Node =>
3705 Make_Compilation_Unit_Aux (Sloc (N)));
3707 Set_Parent_Spec (Act_Decl, Parent_Spec (N));
3708 Set_Body_Required (Decl_Cunit, True);
3710 -- We use the original instantiation compilation unit as the resulting
3711 -- compilation unit of the instance, since this is the main unit.
3713 Rewrite (N, Act_Body);
3714 Body_Cunit := Parent (N);
3716 -- The two compilation unit nodes are linked by the Library_Unit field
3718 Set_Library_Unit (Decl_Cunit, Body_Cunit);
3719 Set_Library_Unit (Body_Cunit, Decl_Cunit);
3721 -- Preserve the private nature of the package if needed
3723 Set_Private_Present (Decl_Cunit, Private_Present (Body_Cunit));
3725 -- If the instance is not the main unit, its context, categorization,
3726 -- and elaboration entity are not relevant to the compilation.
3728 if Parent (N) /= Cunit (Main_Unit) then
3729 return;
3730 end if;
3732 -- The context clause items on the instantiation, which are now
3733 -- attached to the body compilation unit (since the body overwrote
3734 -- the original instantiation node), semantically belong on the spec,
3735 -- so copy them there. It's harmless to leave them on the body as well.
3736 -- In fact one could argue that they belong in both places.
3738 Citem := First (Context_Items (Body_Cunit));
3739 while Present (Citem) loop
3740 Append (New_Copy (Citem), Context_Items (Decl_Cunit));
3741 Next (Citem);
3742 end loop;
3744 -- Propagate categorization flags on packages, so that they appear
3745 -- in ali file for the spec of the unit.
3747 if Ekind (New_Main) = E_Package then
3748 Set_Is_Pure (Old_Main, Is_Pure (New_Main));
3749 Set_Is_Preelaborated (Old_Main, Is_Preelaborated (New_Main));
3750 Set_Is_Remote_Types (Old_Main, Is_Remote_Types (New_Main));
3751 Set_Is_Shared_Passive (Old_Main, Is_Shared_Passive (New_Main));
3752 Set_Is_Remote_Call_Interface
3753 (Old_Main, Is_Remote_Call_Interface (New_Main));
3754 end if;
3756 -- Make entry in Units table, so that binder can generate call to
3757 -- elaboration procedure for body, if any.
3759 Make_Instance_Unit (Body_Cunit);
3760 Main_Unit_Entity := New_Main;
3761 Set_Cunit_Entity (Main_Unit, Main_Unit_Entity);
3763 -- Build elaboration entity, since the instance may certainly
3764 -- generate elaboration code requiring a flag for protection.
3766 Build_Elaboration_Entity (Decl_Cunit, New_Main);
3767 end Build_Instance_Compilation_Unit_Nodes;
3769 -----------------------------------
3770 -- Check_Formal_Package_Instance --
3771 -----------------------------------
3773 -- If the formal has specific parameters, they must match those of the
3774 -- actual. Both of them are instances, and the renaming declarations
3775 -- for their formal parameters appear in the same order in both. The
3776 -- analyzed formal has been analyzed in the context of the current
3777 -- instance.
3779 procedure Check_Formal_Package_Instance
3780 (Formal_Pack : Entity_Id;
3781 Actual_Pack : Entity_Id)
3783 E1 : Entity_Id := First_Entity (Actual_Pack);
3784 E2 : Entity_Id := First_Entity (Formal_Pack);
3786 Expr1 : Node_Id;
3787 Expr2 : Node_Id;
3789 procedure Check_Mismatch (B : Boolean);
3790 -- Common error routine for mismatch between the parameters of
3791 -- the actual instance and those of the formal package.
3793 function Same_Instantiated_Constant (E1, E2 : Entity_Id) return Boolean;
3794 -- The formal may come from a nested formal package, and the actual
3795 -- may have been constant-folded. To determine whether the two denote
3796 -- the same entity we may have to traverse several definitions to
3797 -- recover the ultimate entity that they refer to.
3799 function Same_Instantiated_Variable (E1, E2 : Entity_Id) return Boolean;
3800 -- Similarly, if the formal comes from a nested formal package, the
3801 -- actual may designate the formal through multiple renamings, which
3802 -- have to be followed to determine the original variable in question.
3804 --------------------
3805 -- Check_Mismatch --
3806 --------------------
3808 procedure Check_Mismatch (B : Boolean) is
3809 begin
3810 if B then
3811 Error_Msg_NE
3812 ("actual for & in actual instance does not match formal",
3813 Parent (Actual_Pack), E1);
3814 end if;
3815 end Check_Mismatch;
3817 --------------------------------
3818 -- Same_Instantiated_Constant --
3819 --------------------------------
3821 function Same_Instantiated_Constant
3822 (E1, E2 : Entity_Id) return Boolean
3824 Ent : Entity_Id;
3826 begin
3827 Ent := E2;
3828 while Present (Ent) loop
3829 if E1 = Ent then
3830 return True;
3832 elsif Ekind (Ent) /= E_Constant then
3833 return False;
3835 elsif Is_Entity_Name (Constant_Value (Ent)) then
3836 if Entity (Constant_Value (Ent)) = E1 then
3837 return True;
3838 else
3839 Ent := Entity (Constant_Value (Ent));
3840 end if;
3842 -- The actual may be a constant that has been folded. Recover
3843 -- original name.
3845 elsif Is_Entity_Name (Original_Node (Constant_Value (Ent))) then
3846 Ent := Entity (Original_Node (Constant_Value (Ent)));
3847 else
3848 return False;
3849 end if;
3850 end loop;
3852 return False;
3853 end Same_Instantiated_Constant;
3855 --------------------------------
3856 -- Same_Instantiated_Variable --
3857 --------------------------------
3859 function Same_Instantiated_Variable
3860 (E1, E2 : Entity_Id) return Boolean
3862 function Original_Entity (E : Entity_Id) return Entity_Id;
3863 -- Follow chain of renamings to the ultimate ancestor
3865 ---------------------
3866 -- Original_Entity --
3867 ---------------------
3869 function Original_Entity (E : Entity_Id) return Entity_Id is
3870 Orig : Entity_Id;
3872 begin
3873 Orig := E;
3874 while Nkind (Parent (Orig)) = N_Object_Renaming_Declaration
3875 and then Present (Renamed_Object (Orig))
3876 and then Is_Entity_Name (Renamed_Object (Orig))
3877 loop
3878 Orig := Entity (Renamed_Object (Orig));
3879 end loop;
3881 return Orig;
3882 end Original_Entity;
3884 -- Start of processing for Same_Instantiated_Variable
3886 begin
3887 return Ekind (E1) = Ekind (E2)
3888 and then Original_Entity (E1) = Original_Entity (E2);
3889 end Same_Instantiated_Variable;
3891 -- Start of processing for Check_Formal_Package_Instance
3893 begin
3894 while Present (E1)
3895 and then Present (E2)
3896 loop
3897 exit when Ekind (E1) = E_Package
3898 and then Renamed_Entity (E1) = Renamed_Entity (Actual_Pack);
3900 if Is_Type (E1) then
3902 -- Subtypes must statically match. E1 and E2 are the
3903 -- local entities that are subtypes of the actuals.
3904 -- Itypes generated for other parameters need not be checked,
3905 -- the check will be performed on the parameters themselves.
3907 if not Is_Itype (E1)
3908 and then not Is_Itype (E2)
3909 then
3910 Check_Mismatch
3911 (not Is_Type (E2)
3912 or else Etype (E1) /= Etype (E2)
3913 or else not Subtypes_Statically_Match (E1, E2));
3914 end if;
3916 elsif Ekind (E1) = E_Constant then
3918 -- IN parameters must denote the same static value, or
3919 -- the same constant, or the literal null.
3921 Expr1 := Expression (Parent (E1));
3923 if Ekind (E2) /= E_Constant then
3924 Check_Mismatch (True);
3925 goto Next_E;
3926 else
3927 Expr2 := Expression (Parent (E2));
3928 end if;
3930 if Is_Static_Expression (Expr1) then
3932 if not Is_Static_Expression (Expr2) then
3933 Check_Mismatch (True);
3935 elsif Is_Integer_Type (Etype (E1)) then
3937 declare
3938 V1 : constant Uint := Expr_Value (Expr1);
3939 V2 : constant Uint := Expr_Value (Expr2);
3940 begin
3941 Check_Mismatch (V1 /= V2);
3942 end;
3944 elsif Is_Real_Type (Etype (E1)) then
3945 declare
3946 V1 : constant Ureal := Expr_Value_R (Expr1);
3947 V2 : constant Ureal := Expr_Value_R (Expr2);
3948 begin
3949 Check_Mismatch (V1 /= V2);
3950 end;
3952 elsif Is_String_Type (Etype (E1))
3953 and then Nkind (Expr1) = N_String_Literal
3954 then
3956 if Nkind (Expr2) /= N_String_Literal then
3957 Check_Mismatch (True);
3958 else
3959 Check_Mismatch
3960 (not String_Equal (Strval (Expr1), Strval (Expr2)));
3961 end if;
3962 end if;
3964 elsif Is_Entity_Name (Expr1) then
3965 if Is_Entity_Name (Expr2) then
3966 if Entity (Expr1) = Entity (Expr2) then
3967 null;
3968 else
3969 Check_Mismatch
3970 (not Same_Instantiated_Constant
3971 (Entity (Expr1), Entity (Expr2)));
3972 end if;
3973 else
3974 Check_Mismatch (True);
3975 end if;
3977 elsif Is_Entity_Name (Original_Node (Expr1))
3978 and then Is_Entity_Name (Expr2)
3979 and then
3980 Same_Instantiated_Constant
3981 (Entity (Original_Node (Expr1)), Entity (Expr2))
3982 then
3983 null;
3985 elsif Nkind (Expr1) = N_Null then
3986 Check_Mismatch (Nkind (Expr1) /= N_Null);
3988 else
3989 Check_Mismatch (True);
3990 end if;
3992 elsif Ekind (E1) = E_Variable then
3993 Check_Mismatch (not Same_Instantiated_Variable (E1, E2));
3995 elsif Ekind (E1) = E_Package then
3996 Check_Mismatch
3997 (Ekind (E1) /= Ekind (E2)
3998 or else Renamed_Object (E1) /= Renamed_Object (E2));
4000 elsif Is_Overloadable (E1) then
4002 -- Verify that the names of the entities match.
4003 -- What if actual is an attribute ???
4005 Check_Mismatch
4006 (Ekind (E2) /= Ekind (E1) or else (Alias (E1)) /= Alias (E2));
4008 else
4009 raise Program_Error;
4010 end if;
4012 <<Next_E>>
4013 Next_Entity (E1);
4014 Next_Entity (E2);
4015 end loop;
4016 end Check_Formal_Package_Instance;
4018 ---------------------------
4019 -- Check_Formal_Packages --
4020 ---------------------------
4022 procedure Check_Formal_Packages (P_Id : Entity_Id) is
4023 E : Entity_Id;
4024 Formal_P : Entity_Id;
4026 begin
4027 -- Iterate through the declarations in the instance, looking for
4028 -- package renaming declarations that denote instances of formal
4029 -- packages. Stop when we find the renaming of the current package
4030 -- itself. The declaration for a formal package without a box is
4031 -- followed by an internal entity that repeats the instantiation.
4033 E := First_Entity (P_Id);
4034 while Present (E) loop
4035 if Ekind (E) = E_Package then
4036 if Renamed_Object (E) = P_Id then
4037 exit;
4039 elsif Nkind (Parent (E)) /= N_Package_Renaming_Declaration then
4040 null;
4042 elsif not Box_Present (Parent (Associated_Formal_Package (E))) then
4043 Formal_P := Next_Entity (E);
4044 Check_Formal_Package_Instance (Formal_P, E);
4045 end if;
4046 end if;
4048 Next_Entity (E);
4049 end loop;
4050 end Check_Formal_Packages;
4052 ---------------------------------
4053 -- Check_Forward_Instantiation --
4054 ---------------------------------
4056 procedure Check_Forward_Instantiation (Decl : Node_Id) is
4057 S : Entity_Id;
4058 Gen_Comp : Entity_Id := Cunit_Entity (Get_Source_Unit (Decl));
4060 begin
4061 -- The instantiation appears before the generic body if we are in the
4062 -- scope of the unit containing the generic, either in its spec or in
4063 -- the package body. and before the generic body.
4065 if Ekind (Gen_Comp) = E_Package_Body then
4066 Gen_Comp := Spec_Entity (Gen_Comp);
4067 end if;
4069 if In_Open_Scopes (Gen_Comp)
4070 and then No (Corresponding_Body (Decl))
4071 then
4072 S := Current_Scope;
4074 while Present (S)
4075 and then not Is_Compilation_Unit (S)
4076 and then not Is_Child_Unit (S)
4077 loop
4078 if Ekind (S) = E_Package then
4079 Set_Has_Forward_Instantiation (S);
4080 end if;
4082 S := Scope (S);
4083 end loop;
4084 end if;
4085 end Check_Forward_Instantiation;
4087 ---------------------------
4088 -- Check_Generic_Actuals --
4089 ---------------------------
4091 -- The visibility of the actuals may be different between the
4092 -- point of generic instantiation and the instantiation of the body.
4094 procedure Check_Generic_Actuals
4095 (Instance : Entity_Id;
4096 Is_Formal_Box : Boolean)
4098 E : Entity_Id;
4099 Astype : Entity_Id;
4101 function Denotes_Previous_Actual (Typ : Entity_Id) return Boolean;
4102 -- For a formal that is an array type, the component type is often
4103 -- a previous formal in the same unit. The privacy status of the
4104 -- component type will have been examined earlier in the traversal
4105 -- of the corresponding actuals, and this status should not be
4106 -- modified for the array type itself.
4107 -- To detect this case we have to rescan the list of formals, which
4108 -- is usually short enough to ignore the resulting inefficiency.
4110 function Denotes_Previous_Actual (Typ : Entity_Id) return Boolean is
4111 Prev : Entity_Id;
4112 begin
4113 Prev := First_Entity (Instance);
4114 while Present (Prev) loop
4115 if Is_Type (Prev)
4116 and then Nkind (Parent (Prev)) = N_Subtype_Declaration
4117 and then Is_Entity_Name (Subtype_Indication (Parent (Prev)))
4118 and then Entity (Subtype_Indication (Parent (Prev))) = Typ
4119 then
4120 return True;
4121 elsif Prev = E then
4122 return False;
4123 else
4124 Next_Entity (Prev);
4125 end if;
4126 end loop;
4127 return False;
4128 end Denotes_Previous_Actual;
4130 -- Start of processing for Check_Generic_Actuals
4132 begin
4133 E := First_Entity (Instance);
4134 while Present (E) loop
4135 if Is_Type (E)
4136 and then Nkind (Parent (E)) = N_Subtype_Declaration
4137 and then Scope (Etype (E)) /= Instance
4138 and then Is_Entity_Name (Subtype_Indication (Parent (E)))
4139 then
4140 if Is_Array_Type (E)
4141 and then Denotes_Previous_Actual (Component_Type (E))
4142 then
4143 null;
4144 else
4145 Check_Private_View (Subtype_Indication (Parent (E)));
4146 end if;
4147 Set_Is_Generic_Actual_Type (E, True);
4148 Set_Is_Hidden (E, False);
4149 Set_Is_Potentially_Use_Visible (E,
4150 In_Use (Instance));
4152 -- We constructed the generic actual type as a subtype of
4153 -- the supplied type. This means that it normally would not
4154 -- inherit subtype specific attributes of the actual, which
4155 -- is wrong for the generic case.
4157 Astype := Ancestor_Subtype (E);
4159 if No (Astype) then
4161 -- can happen when E is an itype that is the full view of
4162 -- a private type completed, e.g. with a constrained array.
4164 Astype := Base_Type (E);
4165 end if;
4167 Set_Size_Info (E, (Astype));
4168 Set_RM_Size (E, RM_Size (Astype));
4169 Set_First_Rep_Item (E, First_Rep_Item (Astype));
4171 if Is_Discrete_Or_Fixed_Point_Type (E) then
4172 Set_RM_Size (E, RM_Size (Astype));
4174 -- In nested instances, the base type of an access actual
4175 -- may itself be private, and need to be exchanged.
4177 elsif Is_Access_Type (E)
4178 and then Is_Private_Type (Etype (E))
4179 then
4180 Check_Private_View
4181 (New_Occurrence_Of (Etype (E), Sloc (Instance)));
4182 end if;
4184 elsif Ekind (E) = E_Package then
4186 -- If this is the renaming for the current instance, we're done.
4187 -- Otherwise it is a formal package. If the corresponding formal
4188 -- was declared with a box, the (instantiations of the) generic
4189 -- formal part are also visible. Otherwise, ignore the entity
4190 -- created to validate the actuals.
4192 if Renamed_Object (E) = Instance then
4193 exit;
4195 elsif Nkind (Parent (E)) /= N_Package_Renaming_Declaration then
4196 null;
4198 -- The visibility of a formal of an enclosing generic is already
4199 -- correct.
4201 elsif Denotes_Formal_Package (E) then
4202 null;
4204 elsif Present (Associated_Formal_Package (E)) then
4205 if Box_Present (Parent (Associated_Formal_Package (E))) then
4206 Check_Generic_Actuals (Renamed_Object (E), True);
4207 end if;
4209 Set_Is_Hidden (E, False);
4210 end if;
4212 -- If this is a subprogram instance (in a wrapper package) the
4213 -- actual is fully visible.
4215 elsif Is_Wrapper_Package (Instance) then
4216 Set_Is_Hidden (E, False);
4218 else
4219 Set_Is_Hidden (E, not Is_Formal_Box);
4220 end if;
4222 Next_Entity (E);
4223 end loop;
4224 end Check_Generic_Actuals;
4226 ------------------------------
4227 -- Check_Generic_Child_Unit --
4228 ------------------------------
4230 procedure Check_Generic_Child_Unit
4231 (Gen_Id : Node_Id;
4232 Parent_Installed : in out Boolean)
4234 Loc : constant Source_Ptr := Sloc (Gen_Id);
4235 Gen_Par : Entity_Id := Empty;
4236 Inst_Par : Entity_Id;
4237 E : Entity_Id;
4238 S : Node_Id;
4240 function Find_Generic_Child
4241 (Scop : Entity_Id;
4242 Id : Node_Id) return Entity_Id;
4243 -- Search generic parent for possible child unit with the given name
4245 function In_Enclosing_Instance return Boolean;
4246 -- Within an instance of the parent, the child unit may be denoted
4247 -- by a simple name, or an abbreviated expanded name. Examine enclosing
4248 -- scopes to locate a possible parent instantiation.
4250 ------------------------
4251 -- Find_Generic_Child --
4252 ------------------------
4254 function Find_Generic_Child
4255 (Scop : Entity_Id;
4256 Id : Node_Id) return Entity_Id
4258 E : Entity_Id;
4260 begin
4261 -- If entity of name is already set, instance has already been
4262 -- resolved, e.g. in an enclosing instantiation.
4264 if Present (Entity (Id)) then
4265 if Scope (Entity (Id)) = Scop then
4266 return Entity (Id);
4267 else
4268 return Empty;
4269 end if;
4271 else
4272 E := First_Entity (Scop);
4273 while Present (E) loop
4274 if Chars (E) = Chars (Id)
4275 and then Is_Child_Unit (E)
4276 then
4277 if Is_Child_Unit (E)
4278 and then not Is_Visible_Child_Unit (E)
4279 then
4280 Error_Msg_NE
4281 ("generic child unit& is not visible", Gen_Id, E);
4282 end if;
4284 Set_Entity (Id, E);
4285 return E;
4286 end if;
4288 Next_Entity (E);
4289 end loop;
4291 return Empty;
4292 end if;
4293 end Find_Generic_Child;
4295 ---------------------------
4296 -- In_Enclosing_Instance --
4297 ---------------------------
4299 function In_Enclosing_Instance return Boolean is
4300 Enclosing_Instance : Node_Id;
4301 Instance_Decl : Node_Id;
4303 begin
4304 Enclosing_Instance := Current_Scope;
4306 while Present (Enclosing_Instance) loop
4307 Instance_Decl := Unit_Declaration_Node (Enclosing_Instance);
4309 if Ekind (Enclosing_Instance) = E_Package
4310 and then Is_Generic_Instance (Enclosing_Instance)
4311 and then Present
4312 (Generic_Parent (Specification (Instance_Decl)))
4313 then
4314 -- Check whether the generic we are looking for is a child
4315 -- of this instance.
4317 E := Find_Generic_Child
4318 (Generic_Parent (Specification (Instance_Decl)), Gen_Id);
4319 exit when Present (E);
4321 else
4322 E := Empty;
4323 end if;
4325 Enclosing_Instance := Scope (Enclosing_Instance);
4326 end loop;
4328 if No (E) then
4330 -- Not a child unit
4332 Analyze (Gen_Id);
4333 return False;
4335 else
4336 Rewrite (Gen_Id,
4337 Make_Expanded_Name (Loc,
4338 Chars => Chars (E),
4339 Prefix => New_Occurrence_Of (Enclosing_Instance, Loc),
4340 Selector_Name => New_Occurrence_Of (E, Loc)));
4342 Set_Entity (Gen_Id, E);
4343 Set_Etype (Gen_Id, Etype (E));
4344 Parent_Installed := False; -- Already in scope.
4345 return True;
4346 end if;
4347 end In_Enclosing_Instance;
4349 -- Start of processing for Check_Generic_Child_Unit
4351 begin
4352 -- If the name of the generic is given by a selected component, it
4353 -- may be the name of a generic child unit, and the prefix is the name
4354 -- of an instance of the parent, in which case the child unit must be
4355 -- visible. If this instance is not in scope, it must be placed there
4356 -- and removed after instantiation, because what is being instantiated
4357 -- is not the original child, but the corresponding child present in
4358 -- the instance of the parent.
4360 -- If the child is instantiated within the parent, it can be given by
4361 -- a simple name. In this case the instance is already in scope, but
4362 -- the child generic must be recovered from the generic parent as well.
4364 if Nkind (Gen_Id) = N_Selected_Component then
4365 S := Selector_Name (Gen_Id);
4366 Analyze (Prefix (Gen_Id));
4367 Inst_Par := Entity (Prefix (Gen_Id));
4369 if Ekind (Inst_Par) = E_Package
4370 and then Present (Renamed_Object (Inst_Par))
4371 then
4372 Inst_Par := Renamed_Object (Inst_Par);
4373 end if;
4375 if Ekind (Inst_Par) = E_Package then
4376 if Nkind (Parent (Inst_Par)) = N_Package_Specification then
4377 Gen_Par := Generic_Parent (Parent (Inst_Par));
4379 elsif Nkind (Parent (Inst_Par)) = N_Defining_Program_Unit_Name
4380 and then
4381 Nkind (Parent (Parent (Inst_Par))) = N_Package_Specification
4382 then
4383 Gen_Par := Generic_Parent (Parent (Parent (Inst_Par)));
4384 end if;
4386 elsif Ekind (Inst_Par) = E_Generic_Package
4387 and then Nkind (Parent (Gen_Id)) = N_Formal_Package_Declaration
4388 then
4389 -- A formal package may be a real child package, and not the
4390 -- implicit instance within a parent. In this case the child is
4391 -- not visible and has to be retrieved explicitly as well.
4393 Gen_Par := Inst_Par;
4394 end if;
4396 if Present (Gen_Par) then
4398 -- The prefix denotes an instantiation. The entity itself
4399 -- may be a nested generic, or a child unit.
4401 E := Find_Generic_Child (Gen_Par, S);
4403 if Present (E) then
4404 Change_Selected_Component_To_Expanded_Name (Gen_Id);
4405 Set_Entity (Gen_Id, E);
4406 Set_Etype (Gen_Id, Etype (E));
4407 Set_Entity (S, E);
4408 Set_Etype (S, Etype (E));
4410 -- Indicate that this is a reference to the parent
4412 if In_Extended_Main_Source_Unit (Gen_Id) then
4413 Set_Is_Instantiated (Inst_Par);
4414 end if;
4416 -- A common mistake is to replicate the naming scheme of
4417 -- a hierarchy by instantiating a generic child directly,
4418 -- rather than the implicit child in a parent instance:
4420 -- generic .. package Gpar is ..
4421 -- generic .. package Gpar.Child is ..
4422 -- package Par is new Gpar ();
4424 -- with Gpar.Child;
4425 -- package Par.Child is new Gpar.Child ();
4426 -- rather than Par.Child
4428 -- In this case the instantiation is within Par, which is
4429 -- an instance, but Gpar does not denote Par because we are
4430 -- not IN the instance of Gpar, so this is illegal. The test
4431 -- below recognizes this particular case.
4433 if Is_Child_Unit (E)
4434 and then not Comes_From_Source (Entity (Prefix (Gen_Id)))
4435 and then (not In_Instance
4436 or else Nkind (Parent (Parent (Gen_Id))) =
4437 N_Compilation_Unit)
4438 then
4439 Error_Msg_N
4440 ("prefix of generic child unit must be instance of parent",
4441 Gen_Id);
4442 end if;
4444 if not In_Open_Scopes (Inst_Par)
4445 and then Nkind (Parent (Gen_Id)) not in
4446 N_Generic_Renaming_Declaration
4447 then
4448 Install_Parent (Inst_Par);
4449 Parent_Installed := True;
4450 end if;
4452 else
4453 -- If the generic parent does not contain an entity that
4454 -- corresponds to the selector, the instance doesn't either.
4455 -- Analyzing the node will yield the appropriate error message.
4456 -- If the entity is not a child unit, then it is an inner
4457 -- generic in the parent.
4459 Analyze (Gen_Id);
4460 end if;
4462 else
4463 Analyze (Gen_Id);
4465 if Is_Child_Unit (Entity (Gen_Id))
4466 and then
4467 Nkind (Parent (Gen_Id)) not in N_Generic_Renaming_Declaration
4468 and then not In_Open_Scopes (Inst_Par)
4469 then
4470 Install_Parent (Inst_Par);
4471 Parent_Installed := True;
4472 end if;
4473 end if;
4475 elsif Nkind (Gen_Id) = N_Expanded_Name then
4477 -- Entity already present, analyze prefix, whose meaning may be
4478 -- an instance in the current context. If it is an instance of
4479 -- a relative within another, the proper parent may still have
4480 -- to be installed, if they are not of the same generation.
4482 Analyze (Prefix (Gen_Id));
4483 Inst_Par := Entity (Prefix (Gen_Id));
4485 if In_Enclosing_Instance then
4486 null;
4488 elsif Present (Entity (Gen_Id))
4489 and then Is_Child_Unit (Entity (Gen_Id))
4490 and then not In_Open_Scopes (Inst_Par)
4491 then
4492 Install_Parent (Inst_Par);
4493 Parent_Installed := True;
4494 end if;
4496 elsif In_Enclosing_Instance then
4498 -- The child unit is found in some enclosing scope
4500 null;
4502 else
4503 Analyze (Gen_Id);
4505 -- If this is the renaming of the implicit child in a parent
4506 -- instance, recover the parent name and install it.
4508 if Is_Entity_Name (Gen_Id) then
4509 E := Entity (Gen_Id);
4511 if Is_Generic_Unit (E)
4512 and then Nkind (Parent (E)) in N_Generic_Renaming_Declaration
4513 and then Is_Child_Unit (Renamed_Object (E))
4514 and then Is_Generic_Unit (Scope (Renamed_Object (E)))
4515 and then Nkind (Name (Parent (E))) = N_Expanded_Name
4516 then
4517 Rewrite (Gen_Id,
4518 New_Copy_Tree (Name (Parent (E))));
4519 Inst_Par := Entity (Prefix (Gen_Id));
4521 if not In_Open_Scopes (Inst_Par) then
4522 Install_Parent (Inst_Par);
4523 Parent_Installed := True;
4524 end if;
4526 -- If it is a child unit of a non-generic parent, it may be
4527 -- use-visible and given by a direct name. Install parent as
4528 -- for other cases.
4530 elsif Is_Generic_Unit (E)
4531 and then Is_Child_Unit (E)
4532 and then
4533 Nkind (Parent (Gen_Id)) not in N_Generic_Renaming_Declaration
4534 and then not Is_Generic_Unit (Scope (E))
4535 then
4536 if not In_Open_Scopes (Scope (E)) then
4537 Install_Parent (Scope (E));
4538 Parent_Installed := True;
4539 end if;
4540 end if;
4541 end if;
4542 end if;
4543 end Check_Generic_Child_Unit;
4545 -----------------------------
4546 -- Check_Hidden_Child_Unit --
4547 -----------------------------
4549 procedure Check_Hidden_Child_Unit
4550 (N : Node_Id;
4551 Gen_Unit : Entity_Id;
4552 Act_Decl_Id : Entity_Id)
4554 Gen_Id : constant Node_Id := Name (N);
4556 begin
4557 if Is_Child_Unit (Gen_Unit)
4558 and then Is_Child_Unit (Act_Decl_Id)
4559 and then Nkind (Gen_Id) = N_Expanded_Name
4560 and then Entity (Prefix (Gen_Id)) = Scope (Act_Decl_Id)
4561 and then Chars (Gen_Unit) = Chars (Act_Decl_Id)
4562 then
4563 Error_Msg_Node_2 := Scope (Act_Decl_Id);
4564 Error_Msg_NE
4565 ("generic unit & is implicitly declared in &",
4566 Defining_Unit_Name (N), Gen_Unit);
4567 Error_Msg_N ("\instance must have different name",
4568 Defining_Unit_Name (N));
4569 end if;
4570 end Check_Hidden_Child_Unit;
4572 ------------------------
4573 -- Check_Private_View --
4574 ------------------------
4576 procedure Check_Private_View (N : Node_Id) is
4577 T : constant Entity_Id := Etype (N);
4578 BT : Entity_Id;
4580 begin
4581 -- Exchange views if the type was not private in the generic but is
4582 -- private at the point of instantiation. Do not exchange views if
4583 -- the scope of the type is in scope. This can happen if both generic
4584 -- and instance are sibling units, or if type is defined in a parent.
4585 -- In this case the visibility of the type will be correct for all
4586 -- semantic checks.
4588 if Present (T) then
4589 BT := Base_Type (T);
4591 if Is_Private_Type (T)
4592 and then not Has_Private_View (N)
4593 and then Present (Full_View (T))
4594 and then not In_Open_Scopes (Scope (T))
4595 then
4596 -- In the generic, the full type was visible. Save the
4597 -- private entity, for subsequent exchange.
4599 Switch_View (T);
4601 elsif Has_Private_View (N)
4602 and then not Is_Private_Type (T)
4603 and then not Has_Been_Exchanged (T)
4604 and then Etype (Get_Associated_Node (N)) /= T
4605 then
4606 -- Only the private declaration was visible in the generic. If
4607 -- the type appears in a subtype declaration, the subtype in the
4608 -- instance must have a view compatible with that of its parent,
4609 -- which must be exchanged (see corresponding code in Restore_
4610 -- Private_Views). Otherwise, if the type is defined in a parent
4611 -- unit, leave full visibility within instance, which is safe.
4613 if In_Open_Scopes (Scope (Base_Type (T)))
4614 and then not Is_Private_Type (Base_Type (T))
4615 and then Comes_From_Source (Base_Type (T))
4616 then
4617 null;
4619 elsif Nkind (Parent (N)) = N_Subtype_Declaration
4620 or else not In_Private_Part (Scope (Base_Type (T)))
4621 then
4622 Prepend_Elmt (T, Exchanged_Views);
4623 Exchange_Declarations (Etype (Get_Associated_Node (N)));
4624 end if;
4626 -- For composite types with inconsistent representation
4627 -- exchange component types accordingly.
4629 elsif Is_Access_Type (T)
4630 and then Is_Private_Type (Designated_Type (T))
4631 and then not Has_Private_View (N)
4632 and then Present (Full_View (Designated_Type (T)))
4633 then
4634 Switch_View (Designated_Type (T));
4636 elsif Is_Array_Type (T)
4637 and then Is_Private_Type (Component_Type (T))
4638 and then not Has_Private_View (N)
4639 and then Present (Full_View (Component_Type (T)))
4640 then
4641 Switch_View (Component_Type (T));
4643 elsif Is_Private_Type (T)
4644 and then Present (Full_View (T))
4645 and then Is_Array_Type (Full_View (T))
4646 and then Is_Private_Type (Component_Type (Full_View (T)))
4647 then
4648 Switch_View (T);
4650 -- Finally, a non-private subtype may have a private base type,
4651 -- which must be exchanged for consistency. This can happen when
4652 -- instantiating a package body, when the scope stack is empty
4653 -- but in fact the subtype and the base type are declared in an
4654 -- enclosing scope.
4656 elsif not Is_Private_Type (T)
4657 and then not Has_Private_View (N)
4658 and then Is_Private_Type (Base_Type (T))
4659 and then Present (Full_View (BT))
4660 and then not Is_Generic_Type (BT)
4661 and then not In_Open_Scopes (BT)
4662 then
4663 Prepend_Elmt (Full_View (BT), Exchanged_Views);
4664 Exchange_Declarations (BT);
4665 end if;
4666 end if;
4667 end Check_Private_View;
4669 --------------------------
4670 -- Contains_Instance_Of --
4671 --------------------------
4673 function Contains_Instance_Of
4674 (Inner : Entity_Id;
4675 Outer : Entity_Id;
4676 N : Node_Id) return Boolean
4678 Elmt : Elmt_Id;
4679 Scop : Entity_Id;
4681 begin
4682 Scop := Outer;
4684 -- Verify that there are no circular instantiations. We check whether
4685 -- the unit contains an instance of the current scope or some enclosing
4686 -- scope (in case one of the instances appears in a subunit). Longer
4687 -- circularities involving subunits might seem too pathological to
4688 -- consider, but they were not too pathological for the authors of
4689 -- DEC bc30vsq, so we loop over all enclosing scopes, and mark all
4690 -- enclosing generic scopes as containing an instance.
4692 loop
4693 -- Within a generic subprogram body, the scope is not generic, to
4694 -- allow for recursive subprograms. Use the declaration to determine
4695 -- whether this is a generic unit.
4697 if Ekind (Scop) = E_Generic_Package
4698 or else (Is_Subprogram (Scop)
4699 and then Nkind (Unit_Declaration_Node (Scop)) =
4700 N_Generic_Subprogram_Declaration)
4701 then
4702 Elmt := First_Elmt (Inner_Instances (Inner));
4704 while Present (Elmt) loop
4705 if Node (Elmt) = Scop then
4706 Error_Msg_Node_2 := Inner;
4707 Error_Msg_NE
4708 ("circular Instantiation: & instantiated within &!",
4709 N, Scop);
4710 return True;
4712 elsif Node (Elmt) = Inner then
4713 return True;
4715 elsif Contains_Instance_Of (Node (Elmt), Scop, N) then
4716 Error_Msg_Node_2 := Inner;
4717 Error_Msg_NE
4718 ("circular Instantiation: & instantiated within &!",
4719 N, Node (Elmt));
4720 return True;
4721 end if;
4723 Next_Elmt (Elmt);
4724 end loop;
4726 -- Indicate that Inner is being instantiated within Scop
4728 Append_Elmt (Inner, Inner_Instances (Scop));
4729 end if;
4731 if Scop = Standard_Standard then
4732 exit;
4733 else
4734 Scop := Scope (Scop);
4735 end if;
4736 end loop;
4738 return False;
4739 end Contains_Instance_Of;
4741 -----------------------
4742 -- Copy_Generic_Node --
4743 -----------------------
4745 function Copy_Generic_Node
4746 (N : Node_Id;
4747 Parent_Id : Node_Id;
4748 Instantiating : Boolean) return Node_Id
4750 Ent : Entity_Id;
4751 New_N : Node_Id;
4753 function Copy_Generic_Descendant (D : Union_Id) return Union_Id;
4754 -- Check the given value of one of the Fields referenced by the
4755 -- current node to determine whether to copy it recursively. The
4756 -- field may hold a Node_Id, a List_Id, or an Elist_Id, or a plain
4757 -- value (Sloc, Uint, Char) in which case it need not be copied.
4759 procedure Copy_Descendants;
4760 -- Common utility for various nodes
4762 function Copy_Generic_Elist (E : Elist_Id) return Elist_Id;
4763 -- Make copy of element list
4765 function Copy_Generic_List
4766 (L : List_Id;
4767 Parent_Id : Node_Id) return List_Id;
4768 -- Apply Copy_Node recursively to the members of a node list
4770 function In_Defining_Unit_Name (Nam : Node_Id) return Boolean;
4771 -- True if an identifier is part of the defining program unit name
4772 -- of a child unit. The entity of such an identifier must be kept
4773 -- (for ASIS use) even though as the name of an enclosing generic
4774 -- it would otherwise not be preserved in the generic tree.
4776 ----------------------
4777 -- Copy_Descendants --
4778 ----------------------
4780 procedure Copy_Descendants is
4782 use Atree.Unchecked_Access;
4783 -- This code section is part of the implementation of an untyped
4784 -- tree traversal, so it needs direct access to node fields.
4786 begin
4787 Set_Field1 (New_N, Copy_Generic_Descendant (Field1 (N)));
4788 Set_Field2 (New_N, Copy_Generic_Descendant (Field2 (N)));
4789 Set_Field3 (New_N, Copy_Generic_Descendant (Field3 (N)));
4790 Set_Field4 (New_N, Copy_Generic_Descendant (Field4 (N)));
4791 Set_Field5 (New_N, Copy_Generic_Descendant (Field5 (N)));
4792 end Copy_Descendants;
4794 -----------------------------
4795 -- Copy_Generic_Descendant --
4796 -----------------------------
4798 function Copy_Generic_Descendant (D : Union_Id) return Union_Id is
4799 begin
4800 if D = Union_Id (Empty) then
4801 return D;
4803 elsif D in Node_Range then
4804 return Union_Id
4805 (Copy_Generic_Node (Node_Id (D), New_N, Instantiating));
4807 elsif D in List_Range then
4808 return Union_Id (Copy_Generic_List (List_Id (D), New_N));
4810 elsif D in Elist_Range then
4811 return Union_Id (Copy_Generic_Elist (Elist_Id (D)));
4813 -- Nothing else is copyable (e.g. Uint values), return as is
4815 else
4816 return D;
4817 end if;
4818 end Copy_Generic_Descendant;
4820 ------------------------
4821 -- Copy_Generic_Elist --
4822 ------------------------
4824 function Copy_Generic_Elist (E : Elist_Id) return Elist_Id is
4825 M : Elmt_Id;
4826 L : Elist_Id;
4828 begin
4829 if Present (E) then
4830 L := New_Elmt_List;
4831 M := First_Elmt (E);
4832 while Present (M) loop
4833 Append_Elmt
4834 (Copy_Generic_Node (Node (M), Empty, Instantiating), L);
4835 Next_Elmt (M);
4836 end loop;
4838 return L;
4840 else
4841 return No_Elist;
4842 end if;
4843 end Copy_Generic_Elist;
4845 -----------------------
4846 -- Copy_Generic_List --
4847 -----------------------
4849 function Copy_Generic_List
4850 (L : List_Id;
4851 Parent_Id : Node_Id) return List_Id
4853 N : Node_Id;
4854 New_L : List_Id;
4856 begin
4857 if Present (L) then
4858 New_L := New_List;
4859 Set_Parent (New_L, Parent_Id);
4861 N := First (L);
4862 while Present (N) loop
4863 Append (Copy_Generic_Node (N, Empty, Instantiating), New_L);
4864 Next (N);
4865 end loop;
4867 return New_L;
4869 else
4870 return No_List;
4871 end if;
4872 end Copy_Generic_List;
4874 ---------------------------
4875 -- In_Defining_Unit_Name --
4876 ---------------------------
4878 function In_Defining_Unit_Name (Nam : Node_Id) return Boolean is
4879 begin
4880 return Present (Parent (Nam))
4881 and then (Nkind (Parent (Nam)) = N_Defining_Program_Unit_Name
4882 or else
4883 (Nkind (Parent (Nam)) = N_Expanded_Name
4884 and then In_Defining_Unit_Name (Parent (Nam))));
4885 end In_Defining_Unit_Name;
4887 -- Start of processing for Copy_Generic_Node
4889 begin
4890 if N = Empty then
4891 return N;
4892 end if;
4894 New_N := New_Copy (N);
4896 if Instantiating then
4897 Adjust_Instantiation_Sloc (New_N, S_Adjustment);
4898 end if;
4900 if not Is_List_Member (N) then
4901 Set_Parent (New_N, Parent_Id);
4902 end if;
4904 -- If defining identifier, then all fields have been copied already
4906 if Nkind (New_N) in N_Entity then
4907 null;
4909 -- Special casing for identifiers and other entity names and operators
4911 elsif Nkind (New_N) = N_Identifier
4912 or else Nkind (New_N) = N_Character_Literal
4913 or else Nkind (New_N) = N_Expanded_Name
4914 or else Nkind (New_N) = N_Operator_Symbol
4915 or else Nkind (New_N) in N_Op
4916 then
4917 if not Instantiating then
4919 -- Link both nodes in order to assign subsequently the
4920 -- entity of the copy to the original node, in case this
4921 -- is a global reference.
4923 Set_Associated_Node (N, New_N);
4925 -- If we are within an instantiation, this is a nested generic
4926 -- that has already been analyzed at the point of definition. We
4927 -- must preserve references that were global to the enclosing
4928 -- parent at that point. Other occurrences, whether global or
4929 -- local to the current generic, must be resolved anew, so we
4930 -- reset the entity in the generic copy. A global reference has
4931 -- a smaller depth than the parent, or else the same depth in
4932 -- case both are distinct compilation units.
4934 -- It is also possible for Current_Instantiated_Parent to be
4935 -- defined, and for this not to be a nested generic, namely
4936 -- if the unit is loaded through Rtsfind. In that case, the
4937 -- entity of New_N is only a link to the associated node, and
4938 -- not a defining occurrence.
4940 -- The entities for parent units in the defining_program_unit
4941 -- of a generic child unit are established when the context of
4942 -- the unit is first analyzed, before the generic copy is made.
4943 -- They are preserved in the copy for use in ASIS queries.
4945 Ent := Entity (New_N);
4947 if No (Current_Instantiated_Parent.Gen_Id) then
4948 if No (Ent)
4949 or else Nkind (Ent) /= N_Defining_Identifier
4950 or else not In_Defining_Unit_Name (N)
4951 then
4952 Set_Associated_Node (New_N, Empty);
4953 end if;
4955 elsif No (Ent)
4956 or else
4957 not (Nkind (Ent) = N_Defining_Identifier
4958 or else
4959 Nkind (Ent) = N_Defining_Character_Literal
4960 or else
4961 Nkind (Ent) = N_Defining_Operator_Symbol)
4962 or else No (Scope (Ent))
4963 or else Scope (Ent) = Current_Instantiated_Parent.Gen_Id
4964 or else (Scope_Depth (Scope (Ent)) >
4965 Scope_Depth (Current_Instantiated_Parent.Gen_Id)
4966 and then
4967 Get_Source_Unit (Ent) =
4968 Get_Source_Unit (Current_Instantiated_Parent.Gen_Id))
4969 then
4970 Set_Associated_Node (New_N, Empty);
4971 end if;
4973 -- Case of instantiating identifier or some other name or operator
4975 else
4976 -- If the associated node is still defined, the entity in
4977 -- it is global, and must be copied to the instance.
4978 -- If this copy is being made for a body to inline, it is
4979 -- applied to an instantiated tree, and the entity is already
4980 -- present and must be also preserved.
4982 declare
4983 Assoc : constant Node_Id := Get_Associated_Node (N);
4984 begin
4985 if Present (Assoc) then
4986 if Nkind (Assoc) = Nkind (N) then
4987 Set_Entity (New_N, Entity (Assoc));
4988 Check_Private_View (N);
4990 elsif Nkind (Assoc) = N_Function_Call then
4991 Set_Entity (New_N, Entity (Name (Assoc)));
4993 elsif (Nkind (Assoc) = N_Defining_Identifier
4994 or else Nkind (Assoc) = N_Defining_Character_Literal
4995 or else Nkind (Assoc) = N_Defining_Operator_Symbol)
4996 and then Expander_Active
4997 then
4998 -- Inlining case: we are copying a tree that contains
4999 -- global entities, which are preserved in the copy
5000 -- to be used for subsequent inlining.
5002 null;
5004 else
5005 Set_Entity (New_N, Empty);
5006 end if;
5007 end if;
5008 end;
5009 end if;
5011 -- For expanded name, we must copy the Prefix and Selector_Name
5013 if Nkind (N) = N_Expanded_Name then
5014 Set_Prefix
5015 (New_N, Copy_Generic_Node (Prefix (N), New_N, Instantiating));
5017 Set_Selector_Name (New_N,
5018 Copy_Generic_Node (Selector_Name (N), New_N, Instantiating));
5020 -- For operators, we must copy the right operand
5022 elsif Nkind (N) in N_Op then
5023 Set_Right_Opnd (New_N,
5024 Copy_Generic_Node (Right_Opnd (N), New_N, Instantiating));
5026 -- And for binary operators, the left operand as well
5028 if Nkind (N) in N_Binary_Op then
5029 Set_Left_Opnd (New_N,
5030 Copy_Generic_Node (Left_Opnd (N), New_N, Instantiating));
5031 end if;
5032 end if;
5034 -- Special casing for stubs
5036 elsif Nkind (N) in N_Body_Stub then
5038 -- In any case, we must copy the specification or defining
5039 -- identifier as appropriate.
5041 if Nkind (N) = N_Subprogram_Body_Stub then
5042 Set_Specification (New_N,
5043 Copy_Generic_Node (Specification (N), New_N, Instantiating));
5045 else
5046 Set_Defining_Identifier (New_N,
5047 Copy_Generic_Node
5048 (Defining_Identifier (N), New_N, Instantiating));
5049 end if;
5051 -- If we are not instantiating, then this is where we load and
5052 -- analyze subunits, i.e. at the point where the stub occurs. A
5053 -- more permissivle system might defer this analysis to the point
5054 -- of instantiation, but this seems to complicated for now.
5056 if not Instantiating then
5057 declare
5058 Subunit_Name : constant Unit_Name_Type := Get_Unit_Name (N);
5059 Subunit : Node_Id;
5060 Unum : Unit_Number_Type;
5061 New_Body : Node_Id;
5063 begin
5064 Unum :=
5065 Load_Unit
5066 (Load_Name => Subunit_Name,
5067 Required => False,
5068 Subunit => True,
5069 Error_Node => N);
5071 -- If the proper body is not found, a warning message will
5072 -- be emitted when analyzing the stub, or later at the the
5073 -- point of instantiation. Here we just leave the stub as is.
5075 if Unum = No_Unit then
5076 Subunits_Missing := True;
5077 goto Subunit_Not_Found;
5078 end if;
5080 Subunit := Cunit (Unum);
5082 if Nkind (Unit (Subunit)) /= N_Subunit then
5083 Error_Msg_Sloc := Sloc (N);
5084 Error_Msg_N
5085 ("expected SEPARATE subunit to complete stub at#,"
5086 & " found child unit", Subunit);
5087 goto Subunit_Not_Found;
5088 end if;
5090 -- We must create a generic copy of the subunit, in order
5091 -- to perform semantic analysis on it, and we must replace
5092 -- the stub in the original generic unit with the subunit,
5093 -- in order to preserve non-local references within.
5095 -- Only the proper body needs to be copied. Library_Unit and
5096 -- context clause are simply inherited by the generic copy.
5097 -- Note that the copy (which may be recursive if there are
5098 -- nested subunits) must be done first, before attaching it
5099 -- to the enclosing generic.
5101 New_Body :=
5102 Copy_Generic_Node
5103 (Proper_Body (Unit (Subunit)),
5104 Empty, Instantiating => False);
5106 -- Now place the original proper body in the original
5107 -- generic unit. This is a body, not a compilation unit.
5109 Rewrite (N, Proper_Body (Unit (Subunit)));
5110 Set_Is_Compilation_Unit (Defining_Entity (N), False);
5111 Set_Was_Originally_Stub (N);
5113 -- Finally replace the body of the subunit with its copy,
5114 -- and make this new subunit into the library unit of the
5115 -- generic copy, which does not have stubs any longer.
5117 Set_Proper_Body (Unit (Subunit), New_Body);
5118 Set_Library_Unit (New_N, Subunit);
5119 Inherit_Context (Unit (Subunit), N);
5120 end;
5122 -- If we are instantiating, this must be an error case, since
5123 -- otherwise we would have replaced the stub node by the proper
5124 -- body that corresponds. So just ignore it in the copy (i.e.
5125 -- we have copied it, and that is good enough).
5127 else
5128 null;
5129 end if;
5131 <<Subunit_Not_Found>> null;
5133 -- If the node is a compilation unit, it is the subunit of a stub,
5134 -- which has been loaded already (see code below). In this case,
5135 -- the library unit field of N points to the parent unit (which
5136 -- is a compilation unit) and need not (and cannot!) be copied.
5138 -- When the proper body of the stub is analyzed, thie library_unit
5139 -- link is used to establish the proper context (see sem_ch10).
5141 -- The other fields of a compilation unit are copied as usual
5143 elsif Nkind (N) = N_Compilation_Unit then
5145 -- This code can only be executed when not instantiating, because
5146 -- in the copy made for an instantiation, the compilation unit
5147 -- node has disappeared at the point that a stub is replaced by
5148 -- its proper body.
5150 pragma Assert (not Instantiating);
5152 Set_Context_Items (New_N,
5153 Copy_Generic_List (Context_Items (N), New_N));
5155 Set_Unit (New_N,
5156 Copy_Generic_Node (Unit (N), New_N, False));
5158 Set_First_Inlined_Subprogram (New_N,
5159 Copy_Generic_Node
5160 (First_Inlined_Subprogram (N), New_N, False));
5162 Set_Aux_Decls_Node (New_N,
5163 Copy_Generic_Node (Aux_Decls_Node (N), New_N, False));
5165 -- For an assignment node, the assignment is known to be semantically
5166 -- legal if we are instantiating the template. This avoids incorrect
5167 -- diagnostics in generated code.
5169 elsif Nkind (N) = N_Assignment_Statement then
5171 -- Copy name and expression fields in usual manner
5173 Set_Name (New_N,
5174 Copy_Generic_Node (Name (N), New_N, Instantiating));
5176 Set_Expression (New_N,
5177 Copy_Generic_Node (Expression (N), New_N, Instantiating));
5179 if Instantiating then
5180 Set_Assignment_OK (Name (New_N), True);
5181 end if;
5183 elsif Nkind (N) = N_Aggregate
5184 or else Nkind (N) = N_Extension_Aggregate
5185 then
5187 if not Instantiating then
5188 Set_Associated_Node (N, New_N);
5190 else
5191 if Present (Get_Associated_Node (N))
5192 and then Nkind (Get_Associated_Node (N)) = Nkind (N)
5193 then
5194 -- In the generic the aggregate has some composite type. If at
5195 -- the point of instantiation the type has a private view,
5196 -- install the full view (and that of its ancestors, if any).
5198 declare
5199 T : Entity_Id := (Etype (Get_Associated_Node (New_N)));
5200 Rt : Entity_Id;
5202 begin
5203 if Present (T)
5204 and then Is_Private_Type (T)
5205 then
5206 Switch_View (T);
5207 end if;
5209 if Present (T)
5210 and then Is_Tagged_Type (T)
5211 and then Is_Derived_Type (T)
5212 then
5213 Rt := Root_Type (T);
5215 loop
5216 T := Etype (T);
5218 if Is_Private_Type (T) then
5219 Switch_View (T);
5220 end if;
5222 exit when T = Rt;
5223 end loop;
5224 end if;
5225 end;
5226 end if;
5227 end if;
5229 -- Do not copy the associated node, which points to
5230 -- the generic copy of the aggregate.
5232 declare
5233 use Atree.Unchecked_Access;
5234 -- This code section is part of the implementation of an untyped
5235 -- tree traversal, so it needs direct access to node fields.
5237 begin
5238 Set_Field1 (New_N, Copy_Generic_Descendant (Field1 (N)));
5239 Set_Field2 (New_N, Copy_Generic_Descendant (Field2 (N)));
5240 Set_Field3 (New_N, Copy_Generic_Descendant (Field3 (N)));
5241 Set_Field5 (New_N, Copy_Generic_Descendant (Field5 (N)));
5242 end;
5244 -- Allocators do not have an identifier denoting the access type,
5245 -- so we must locate it through the expression to check whether
5246 -- the views are consistent.
5248 elsif Nkind (N) = N_Allocator
5249 and then Nkind (Expression (N)) = N_Qualified_Expression
5250 and then Is_Entity_Name (Subtype_Mark (Expression (N)))
5251 and then Instantiating
5252 then
5253 declare
5254 T : constant Node_Id :=
5255 Get_Associated_Node (Subtype_Mark (Expression (N)));
5256 Acc_T : Entity_Id;
5258 begin
5259 if Present (T) then
5260 -- Retrieve the allocator node in the generic copy
5262 Acc_T := Etype (Parent (Parent (T)));
5263 if Present (Acc_T)
5264 and then Is_Private_Type (Acc_T)
5265 then
5266 Switch_View (Acc_T);
5267 end if;
5268 end if;
5270 Copy_Descendants;
5271 end;
5273 -- For a proper body, we must catch the case of a proper body that
5274 -- replaces a stub. This represents the point at which a separate
5275 -- compilation unit, and hence template file, may be referenced, so
5276 -- we must make a new source instantiation entry for the template
5277 -- of the subunit, and ensure that all nodes in the subunit are
5278 -- adjusted using this new source instantiation entry.
5280 elsif Nkind (N) in N_Proper_Body then
5281 declare
5282 Save_Adjustment : constant Sloc_Adjustment := S_Adjustment;
5284 begin
5285 if Instantiating and then Was_Originally_Stub (N) then
5286 Create_Instantiation_Source
5287 (Instantiation_Node,
5288 Defining_Entity (N),
5289 False,
5290 S_Adjustment);
5291 end if;
5293 -- Now copy the fields of the proper body, using the new
5294 -- adjustment factor if one was needed as per test above.
5296 Copy_Descendants;
5298 -- Restore the original adjustment factor in case changed
5300 S_Adjustment := Save_Adjustment;
5301 end;
5303 -- Don't copy Ident or Comment pragmas, since the comment belongs
5304 -- to the generic unit, not to the instantiating unit.
5306 elsif Nkind (N) = N_Pragma
5307 and then Instantiating
5308 then
5309 declare
5310 Prag_Id : constant Pragma_Id := Get_Pragma_Id (Chars (N));
5312 begin
5313 if Prag_Id = Pragma_Ident
5314 or else Prag_Id = Pragma_Comment
5315 then
5316 New_N := Make_Null_Statement (Sloc (N));
5318 else
5319 Copy_Descendants;
5320 end if;
5321 end;
5323 elsif Nkind (N) = N_Integer_Literal
5324 or else Nkind (N) = N_Real_Literal
5325 then
5326 -- No descendant fields need traversing
5328 null;
5330 -- For the remaining nodes, copy recursively their descendants
5332 else
5333 Copy_Descendants;
5335 if Instantiating
5336 and then Nkind (N) = N_Subprogram_Body
5337 then
5338 Set_Generic_Parent (Specification (New_N), N);
5339 end if;
5340 end if;
5342 return New_N;
5343 end Copy_Generic_Node;
5345 ----------------------------
5346 -- Denotes_Formal_Package --
5347 ----------------------------
5349 function Denotes_Formal_Package
5350 (Pack : Entity_Id;
5351 On_Exit : Boolean := False) return Boolean
5353 Par : Entity_Id;
5354 Scop : constant Entity_Id := Scope (Pack);
5355 E : Entity_Id;
5357 begin
5358 if On_Exit then
5359 Par :=
5360 Instance_Envs.Table
5361 (Instance_Envs.Last).Instantiated_Parent.Act_Id;
5362 else
5363 Par := Current_Instantiated_Parent.Act_Id;
5364 end if;
5366 if Ekind (Scop) = E_Generic_Package
5367 or else Nkind (Unit_Declaration_Node (Scop)) =
5368 N_Generic_Subprogram_Declaration
5369 then
5370 return True;
5372 elsif Nkind (Parent (Pack)) = N_Formal_Package_Declaration then
5373 return True;
5375 elsif No (Par) then
5376 return False;
5378 else
5379 -- Check whether this package is associated with a formal
5380 -- package of the enclosing instantiation. Iterate over the
5381 -- list of renamings.
5383 E := First_Entity (Par);
5384 while Present (E) loop
5385 if Ekind (E) /= E_Package
5386 or else Nkind (Parent (E)) /= N_Package_Renaming_Declaration
5387 then
5388 null;
5389 elsif Renamed_Object (E) = Par then
5390 return False;
5392 elsif Renamed_Object (E) = Pack then
5393 return True;
5394 end if;
5396 Next_Entity (E);
5397 end loop;
5399 return False;
5400 end if;
5401 end Denotes_Formal_Package;
5403 -----------------
5404 -- End_Generic --
5405 -----------------
5407 procedure End_Generic is
5408 begin
5409 -- ??? More things could be factored out in this
5410 -- routine. Should probably be done at a later stage.
5412 Inside_A_Generic := Generic_Flags.Table (Generic_Flags.Last);
5413 Generic_Flags.Decrement_Last;
5415 Expander_Mode_Restore;
5416 end End_Generic;
5418 ----------------------
5419 -- Find_Actual_Type --
5420 ----------------------
5422 function Find_Actual_Type
5423 (Typ : Entity_Id;
5424 Gen_Scope : Entity_Id) return Entity_Id
5426 T : Entity_Id;
5428 begin
5429 if not Is_Child_Unit (Gen_Scope) then
5430 return Get_Instance_Of (Typ);
5432 elsif not Is_Generic_Type (Typ)
5433 or else Scope (Typ) = Gen_Scope
5434 then
5435 return Get_Instance_Of (Typ);
5437 else
5438 T := Current_Entity (Typ);
5439 while Present (T) loop
5440 if In_Open_Scopes (Scope (T)) then
5441 return T;
5442 end if;
5444 T := Homonym (T);
5445 end loop;
5447 return Typ;
5448 end if;
5449 end Find_Actual_Type;
5451 ----------------------------
5452 -- Freeze_Subprogram_Body --
5453 ----------------------------
5455 procedure Freeze_Subprogram_Body
5456 (Inst_Node : Node_Id;
5457 Gen_Body : Node_Id;
5458 Pack_Id : Entity_Id)
5460 F_Node : Node_Id;
5461 Gen_Unit : constant Entity_Id := Get_Generic_Entity (Inst_Node);
5462 Par : constant Entity_Id := Scope (Gen_Unit);
5463 Enc_G : Entity_Id;
5464 Enc_I : Node_Id;
5465 E_G_Id : Entity_Id;
5467 function Earlier (N1, N2 : Node_Id) return Boolean;
5468 -- Yields True if N1 and N2 appear in the same compilation unit,
5469 -- ignoring subunits, and if N1 is to the left of N2 in a left-to-right
5470 -- traversal of the tree for the unit.
5472 function Enclosing_Body (N : Node_Id) return Node_Id;
5473 -- Find innermost package body that encloses the given node, and which
5474 -- is not a compilation unit. Freeze nodes for the instance, or for its
5475 -- enclosing body, may be inserted after the enclosing_body of the
5476 -- generic unit.
5478 function Package_Freeze_Node (B : Node_Id) return Node_Id;
5479 -- Find entity for given package body, and locate or create a freeze
5480 -- node for it.
5482 function True_Parent (N : Node_Id) return Node_Id;
5483 -- For a subunit, return parent of corresponding stub
5485 -------------
5486 -- Earlier --
5487 -------------
5489 function Earlier (N1, N2 : Node_Id) return Boolean is
5490 D1 : Integer := 0;
5491 D2 : Integer := 0;
5492 P1 : Node_Id := N1;
5493 P2 : Node_Id := N2;
5495 procedure Find_Depth (P : in out Node_Id; D : in out Integer);
5496 -- Find distance from given node to enclosing compilation unit
5498 ----------------
5499 -- Find_Depth --
5500 ----------------
5502 procedure Find_Depth (P : in out Node_Id; D : in out Integer) is
5503 begin
5504 while Present (P)
5505 and then Nkind (P) /= N_Compilation_Unit
5506 loop
5507 P := True_Parent (P);
5508 D := D + 1;
5509 end loop;
5510 end Find_Depth;
5512 -- Start of procesing for Earlier
5514 begin
5515 Find_Depth (P1, D1);
5516 Find_Depth (P2, D2);
5518 if P1 /= P2 then
5519 return False;
5520 else
5521 P1 := N1;
5522 P2 := N2;
5523 end if;
5525 while D1 > D2 loop
5526 P1 := True_Parent (P1);
5527 D1 := D1 - 1;
5528 end loop;
5530 while D2 > D1 loop
5531 P2 := True_Parent (P2);
5532 D2 := D2 - 1;
5533 end loop;
5535 -- At this point P1 and P2 are at the same distance from the root.
5536 -- We examine their parents until we find a common declarative
5537 -- list, at which point we can establish their relative placement
5538 -- by comparing their ultimate slocs. If we reach the root,
5539 -- N1 and N2 do not descend from the same declarative list (e.g.
5540 -- one is nested in the declarative part and the other is in a block
5541 -- in the statement part) and the earlier one is already frozen.
5543 while not Is_List_Member (P1)
5544 or else not Is_List_Member (P2)
5545 or else List_Containing (P1) /= List_Containing (P2)
5546 loop
5547 P1 := True_Parent (P1);
5548 P2 := True_Parent (P2);
5550 if Nkind (Parent (P1)) = N_Subunit then
5551 P1 := Corresponding_Stub (Parent (P1));
5552 end if;
5554 if Nkind (Parent (P2)) = N_Subunit then
5555 P2 := Corresponding_Stub (Parent (P2));
5556 end if;
5558 if P1 = P2 then
5559 return False;
5560 end if;
5561 end loop;
5563 return
5564 Top_Level_Location (Sloc (P1)) < Top_Level_Location (Sloc (P2));
5565 end Earlier;
5567 --------------------
5568 -- Enclosing_Body --
5569 --------------------
5571 function Enclosing_Body (N : Node_Id) return Node_Id is
5572 P : Node_Id := Parent (N);
5574 begin
5575 while Present (P)
5576 and then Nkind (Parent (P)) /= N_Compilation_Unit
5577 loop
5578 if Nkind (P) = N_Package_Body then
5580 if Nkind (Parent (P)) = N_Subunit then
5581 return Corresponding_Stub (Parent (P));
5582 else
5583 return P;
5584 end if;
5585 end if;
5587 P := True_Parent (P);
5588 end loop;
5590 return Empty;
5591 end Enclosing_Body;
5593 -------------------------
5594 -- Package_Freeze_Node --
5595 -------------------------
5597 function Package_Freeze_Node (B : Node_Id) return Node_Id is
5598 Id : Entity_Id;
5600 begin
5601 if Nkind (B) = N_Package_Body then
5602 Id := Corresponding_Spec (B);
5604 else pragma Assert (Nkind (B) = N_Package_Body_Stub);
5605 Id := Corresponding_Spec (Proper_Body (Unit (Library_Unit (B))));
5606 end if;
5608 Ensure_Freeze_Node (Id);
5609 return Freeze_Node (Id);
5610 end Package_Freeze_Node;
5612 -----------------
5613 -- True_Parent --
5614 -----------------
5616 function True_Parent (N : Node_Id) return Node_Id is
5617 begin
5618 if Nkind (Parent (N)) = N_Subunit then
5619 return Parent (Corresponding_Stub (Parent (N)));
5620 else
5621 return Parent (N);
5622 end if;
5623 end True_Parent;
5625 -- Start of processing of Freeze_Subprogram_Body
5627 begin
5628 -- If the instance and the generic body appear within the same
5629 -- unit, and the instance preceeds the generic, the freeze node for
5630 -- the instance must appear after that of the generic. If the generic
5631 -- is nested within another instance I2, then current instance must
5632 -- be frozen after I2. In both cases, the freeze nodes are those of
5633 -- enclosing packages. Otherwise, the freeze node is placed at the end
5634 -- of the current declarative part.
5636 Enc_G := Enclosing_Body (Gen_Body);
5637 Enc_I := Enclosing_Body (Inst_Node);
5638 Ensure_Freeze_Node (Pack_Id);
5639 F_Node := Freeze_Node (Pack_Id);
5641 if Is_Generic_Instance (Par)
5642 and then Present (Freeze_Node (Par))
5643 and then
5644 In_Same_Declarative_Part (Freeze_Node (Par), Inst_Node)
5645 then
5646 if ABE_Is_Certain (Get_Package_Instantiation_Node (Par)) then
5648 -- The parent was a premature instantiation. Insert freeze
5649 -- node at the end the current declarative part.
5651 Insert_After_Last_Decl (Inst_Node, F_Node);
5653 else
5654 Insert_After (Freeze_Node (Par), F_Node);
5655 end if;
5657 -- The body enclosing the instance should be frozen after the body
5658 -- that includes the generic, because the body of the instance may
5659 -- make references to entities therein. If the two are not in the
5660 -- same declarative part, or if the one enclosing the instance is
5661 -- frozen already, freeze the instance at the end of the current
5662 -- declarative part.
5664 elsif Is_Generic_Instance (Par)
5665 and then Present (Freeze_Node (Par))
5666 and then Present (Enc_I)
5667 then
5668 if In_Same_Declarative_Part (Freeze_Node (Par), Enc_I)
5669 or else
5670 (Nkind (Enc_I) = N_Package_Body
5671 and then
5672 In_Same_Declarative_Part (Freeze_Node (Par), Parent (Enc_I)))
5673 then
5674 -- The enclosing package may contain several instances. Rather
5675 -- than computing the earliest point at which to insert its
5676 -- freeze node, we place it at the end of the declarative part
5677 -- of the parent of the generic.
5679 Insert_After_Last_Decl
5680 (Freeze_Node (Par), Package_Freeze_Node (Enc_I));
5681 end if;
5683 Insert_After_Last_Decl (Inst_Node, F_Node);
5685 elsif Present (Enc_G)
5686 and then Present (Enc_I)
5687 and then Enc_G /= Enc_I
5688 and then Earlier (Inst_Node, Gen_Body)
5689 then
5690 if Nkind (Enc_G) = N_Package_Body then
5691 E_G_Id := Corresponding_Spec (Enc_G);
5692 else pragma Assert (Nkind (Enc_G) = N_Package_Body_Stub);
5693 E_G_Id :=
5694 Corresponding_Spec (Proper_Body (Unit (Library_Unit (Enc_G))));
5695 end if;
5697 -- Freeze package that encloses instance, and place node after
5698 -- package that encloses generic. If enclosing package is already
5699 -- frozen we have to assume it is at the proper place. This may
5700 -- be a potential ABE that requires dynamic checking.
5702 Insert_After_Last_Decl (Enc_G, Package_Freeze_Node (Enc_I));
5704 -- Freeze enclosing subunit before instance
5706 Ensure_Freeze_Node (E_G_Id);
5708 if not Is_List_Member (Freeze_Node (E_G_Id)) then
5709 Insert_After (Enc_G, Freeze_Node (E_G_Id));
5710 end if;
5712 Insert_After_Last_Decl (Inst_Node, F_Node);
5714 else
5715 -- If none of the above, insert freeze node at the end of the
5716 -- current declarative part.
5718 Insert_After_Last_Decl (Inst_Node, F_Node);
5719 end if;
5720 end Freeze_Subprogram_Body;
5722 ----------------
5723 -- Get_Gen_Id --
5724 ----------------
5726 function Get_Gen_Id (E : Assoc_Ptr) return Entity_Id is
5727 begin
5728 return Generic_Renamings.Table (E).Gen_Id;
5729 end Get_Gen_Id;
5731 ---------------------
5732 -- Get_Instance_Of --
5733 ---------------------
5735 function Get_Instance_Of (A : Entity_Id) return Entity_Id is
5736 Res : constant Assoc_Ptr := Generic_Renamings_HTable.Get (A);
5738 begin
5739 if Res /= Assoc_Null then
5740 return Generic_Renamings.Table (Res).Act_Id;
5741 else
5742 -- On exit, entity is not instantiated: not a generic parameter,
5743 -- or else parameter of an inner generic unit.
5745 return A;
5746 end if;
5747 end Get_Instance_Of;
5749 ------------------------------------
5750 -- Get_Package_Instantiation_Node --
5751 ------------------------------------
5753 function Get_Package_Instantiation_Node (A : Entity_Id) return Node_Id is
5754 Decl : Node_Id := Unit_Declaration_Node (A);
5755 Inst : Node_Id;
5757 begin
5758 -- If the instantiation is a compilation unit that does not need a
5759 -- body then the instantiation node has been rewritten as a package
5760 -- declaration for the instance, and we return the original node.
5762 -- If it is a compilation unit and the instance node has not been
5763 -- rewritten, then it is still the unit of the compilation. Finally,
5764 -- if a body is present, this is a parent of the main unit whose body
5765 -- has been compiled for inlining purposes, and the instantiation node
5766 -- has been rewritten with the instance body.
5768 -- Otherwise the instantiation node appears after the declaration.
5769 -- If the entity is a formal package, the declaration may have been
5770 -- rewritten as a generic declaration (in the case of a formal with a
5771 -- box) or left as a formal package declaration if it has actuals, and
5772 -- is found with a forward search.
5774 if Nkind (Parent (Decl)) = N_Compilation_Unit then
5775 if Nkind (Decl) = N_Package_Declaration
5776 and then Present (Corresponding_Body (Decl))
5777 then
5778 Decl := Unit_Declaration_Node (Corresponding_Body (Decl));
5779 end if;
5781 if Nkind (Original_Node (Decl)) = N_Package_Instantiation then
5782 return Original_Node (Decl);
5783 else
5784 return Unit (Parent (Decl));
5785 end if;
5787 elsif Nkind (Decl) = N_Generic_Package_Declaration
5788 and then Nkind (Original_Node (Decl)) = N_Formal_Package_Declaration
5789 then
5790 return Original_Node (Decl);
5792 else
5793 Inst := Next (Decl);
5794 while Nkind (Inst) /= N_Package_Instantiation
5795 and then Nkind (Inst) /= N_Formal_Package_Declaration
5796 loop
5797 Next (Inst);
5798 end loop;
5800 return Inst;
5801 end if;
5802 end Get_Package_Instantiation_Node;
5804 ------------------------
5805 -- Has_Been_Exchanged --
5806 ------------------------
5808 function Has_Been_Exchanged (E : Entity_Id) return Boolean is
5809 Next : Elmt_Id := First_Elmt (Exchanged_Views);
5811 begin
5812 while Present (Next) loop
5813 if Full_View (Node (Next)) = E then
5814 return True;
5815 end if;
5817 Next_Elmt (Next);
5818 end loop;
5820 return False;
5821 end Has_Been_Exchanged;
5823 ----------
5824 -- Hash --
5825 ----------
5827 function Hash (F : Entity_Id) return HTable_Range is
5828 begin
5829 return HTable_Range (F mod HTable_Size);
5830 end Hash;
5832 ------------------------
5833 -- Hide_Current_Scope --
5834 ------------------------
5836 procedure Hide_Current_Scope is
5837 C : constant Entity_Id := Current_Scope;
5838 E : Entity_Id;
5840 begin
5841 Set_Is_Hidden_Open_Scope (C);
5842 E := First_Entity (C);
5844 while Present (E) loop
5845 if Is_Immediately_Visible (E) then
5846 Set_Is_Immediately_Visible (E, False);
5847 Append_Elmt (E, Hidden_Entities);
5848 end if;
5850 Next_Entity (E);
5851 end loop;
5853 -- Make the scope name invisible as well. This is necessary, but
5854 -- might conflict with calls to Rtsfind later on, in case the scope
5855 -- is a predefined one. There is no clean solution to this problem, so
5856 -- for now we depend on the user not redefining Standard itself in one
5857 -- of the parent units.
5859 if Is_Immediately_Visible (C)
5860 and then C /= Standard_Standard
5861 then
5862 Set_Is_Immediately_Visible (C, False);
5863 Append_Elmt (C, Hidden_Entities);
5864 end if;
5866 end Hide_Current_Scope;
5868 --------------
5869 -- Init_Env --
5870 --------------
5872 procedure Init_Env is
5873 Saved : Instance_Env;
5875 begin
5876 Saved.Ada_Version := Ada_Version;
5877 Saved.Ada_Version_Explicit := Ada_Version_Explicit;
5878 Saved.Instantiated_Parent := Current_Instantiated_Parent;
5879 Saved.Exchanged_Views := Exchanged_Views;
5880 Saved.Hidden_Entities := Hidden_Entities;
5881 Saved.Current_Sem_Unit := Current_Sem_Unit;
5882 Saved.Parent_Unit_Visible := Parent_Unit_Visible;
5883 Instance_Envs.Increment_Last;
5884 Instance_Envs.Table (Instance_Envs.Last) := Saved;
5886 Exchanged_Views := New_Elmt_List;
5887 Hidden_Entities := New_Elmt_List;
5889 -- Make dummy entry for Instantiated parent. If generic unit is
5890 -- legal, this is set properly in Set_Instance_Env.
5892 Current_Instantiated_Parent :=
5893 (Current_Scope, Current_Scope, Assoc_Null);
5894 end Init_Env;
5896 ------------------------------
5897 -- In_Same_Declarative_Part --
5898 ------------------------------
5900 function In_Same_Declarative_Part
5901 (F_Node : Node_Id;
5902 Inst : Node_Id) return Boolean
5904 Decls : constant Node_Id := Parent (F_Node);
5905 Nod : Node_Id := Parent (Inst);
5907 begin
5908 while Present (Nod) loop
5909 if Nod = Decls then
5910 return True;
5912 elsif Nkind (Nod) = N_Subprogram_Body
5913 or else Nkind (Nod) = N_Package_Body
5914 or else Nkind (Nod) = N_Task_Body
5915 or else Nkind (Nod) = N_Protected_Body
5916 or else Nkind (Nod) = N_Block_Statement
5917 then
5918 return False;
5920 elsif Nkind (Nod) = N_Subunit then
5921 Nod := Corresponding_Stub (Nod);
5923 elsif Nkind (Nod) = N_Compilation_Unit then
5924 return False;
5925 else
5926 Nod := Parent (Nod);
5927 end if;
5928 end loop;
5930 return False;
5931 end In_Same_Declarative_Part;
5933 ---------------------
5934 -- In_Main_Context --
5935 ---------------------
5937 function In_Main_Context (E : Entity_Id) return Boolean is
5938 Context : List_Id;
5939 Clause : Node_Id;
5940 Nam : Node_Id;
5942 begin
5943 if not Is_Compilation_Unit (E)
5944 or else Ekind (E) /= E_Package
5945 or else In_Private_Part (E)
5946 then
5947 return False;
5948 end if;
5950 Context := Context_Items (Cunit (Main_Unit));
5952 Clause := First (Context);
5953 while Present (Clause) loop
5954 if Nkind (Clause) = N_With_Clause then
5955 Nam := Name (Clause);
5957 -- If the current scope is part of the context of the main unit,
5958 -- analysis of the corresponding with_clause is not complete, and
5959 -- the entity is not set. We use the Chars field directly, which
5960 -- might produce false positives in rare cases, but guarantees
5961 -- that we produce all the instance bodies we will need.
5963 if (Nkind (Nam) = N_Identifier
5964 and then Chars (Nam) = Chars (E))
5965 or else (Nkind (Nam) = N_Selected_Component
5966 and then Chars (Selector_Name (Nam)) = Chars (E))
5967 then
5968 return True;
5969 end if;
5970 end if;
5972 Next (Clause);
5973 end loop;
5975 return False;
5976 end In_Main_Context;
5978 ---------------------
5979 -- Inherit_Context --
5980 ---------------------
5982 procedure Inherit_Context (Gen_Decl : Node_Id; Inst : Node_Id) is
5983 Current_Context : List_Id;
5984 Current_Unit : Node_Id;
5985 Item : Node_Id;
5986 New_I : Node_Id;
5988 begin
5989 if Nkind (Parent (Gen_Decl)) = N_Compilation_Unit then
5991 -- The inherited context is attached to the enclosing compilation
5992 -- unit. This is either the main unit, or the declaration for the
5993 -- main unit (in case the instantation appears within the package
5994 -- declaration and the main unit is its body).
5996 Current_Unit := Parent (Inst);
5997 while Present (Current_Unit)
5998 and then Nkind (Current_Unit) /= N_Compilation_Unit
5999 loop
6000 Current_Unit := Parent (Current_Unit);
6001 end loop;
6003 Current_Context := Context_Items (Current_Unit);
6005 Item := First (Context_Items (Parent (Gen_Decl)));
6006 while Present (Item) loop
6007 if Nkind (Item) = N_With_Clause then
6008 New_I := New_Copy (Item);
6009 Set_Implicit_With (New_I, True);
6010 Append (New_I, Current_Context);
6011 end if;
6013 Next (Item);
6014 end loop;
6015 end if;
6016 end Inherit_Context;
6018 ----------------
6019 -- Initialize --
6020 ----------------
6022 procedure Initialize is
6023 begin
6024 Generic_Renamings.Init;
6025 Instance_Envs.Init;
6026 Generic_Flags.Init;
6027 Generic_Renamings_HTable.Reset;
6028 Circularity_Detected := False;
6029 Exchanged_Views := No_Elist;
6030 Hidden_Entities := No_Elist;
6031 end Initialize;
6033 ----------------------------
6034 -- Insert_After_Last_Decl --
6035 ----------------------------
6037 procedure Insert_After_Last_Decl (N : Node_Id; F_Node : Node_Id) is
6038 L : List_Id := List_Containing (N);
6039 P : constant Node_Id := Parent (L);
6041 begin
6042 if not Is_List_Member (F_Node) then
6043 if Nkind (P) = N_Package_Specification
6044 and then L = Visible_Declarations (P)
6045 and then Present (Private_Declarations (P))
6046 and then not Is_Empty_List (Private_Declarations (P))
6047 then
6048 L := Private_Declarations (P);
6049 end if;
6051 Insert_After (Last (L), F_Node);
6052 end if;
6053 end Insert_After_Last_Decl;
6055 ------------------
6056 -- Install_Body --
6057 ------------------
6059 procedure Install_Body
6060 (Act_Body : Node_Id;
6061 N : Node_Id;
6062 Gen_Body : Node_Id;
6063 Gen_Decl : Node_Id)
6065 Act_Id : constant Entity_Id := Corresponding_Spec (Act_Body);
6066 Act_Unit : constant Node_Id := Unit (Cunit (Get_Source_Unit (N)));
6067 Gen_Id : constant Entity_Id := Corresponding_Spec (Gen_Body);
6068 Par : constant Entity_Id := Scope (Gen_Id);
6069 Gen_Unit : constant Node_Id :=
6070 Unit (Cunit (Get_Source_Unit (Gen_Decl)));
6071 Orig_Body : Node_Id := Gen_Body;
6072 F_Node : Node_Id;
6073 Body_Unit : Node_Id;
6075 Must_Delay : Boolean;
6077 function Enclosing_Subp (Id : Entity_Id) return Entity_Id;
6078 -- Find subprogram (if any) that encloses instance and/or generic body
6080 function True_Sloc (N : Node_Id) return Source_Ptr;
6081 -- If the instance is nested inside a generic unit, the Sloc of the
6082 -- instance indicates the place of the original definition, not the
6083 -- point of the current enclosing instance. Pending a better usage of
6084 -- Slocs to indicate instantiation places, we determine the place of
6085 -- origin of a node by finding the maximum sloc of any ancestor node.
6086 -- Why is this not equivalent to Top_Level_Location ???
6088 --------------------
6089 -- Enclosing_Subp --
6090 --------------------
6092 function Enclosing_Subp (Id : Entity_Id) return Entity_Id is
6093 Scop : Entity_Id := Scope (Id);
6095 begin
6096 while Scop /= Standard_Standard
6097 and then not Is_Overloadable (Scop)
6098 loop
6099 Scop := Scope (Scop);
6100 end loop;
6102 return Scop;
6103 end Enclosing_Subp;
6105 ---------------
6106 -- True_Sloc --
6107 ---------------
6109 function True_Sloc (N : Node_Id) return Source_Ptr is
6110 Res : Source_Ptr;
6111 N1 : Node_Id;
6113 begin
6114 Res := Sloc (N);
6115 N1 := N;
6116 while Present (N1) and then N1 /= Act_Unit loop
6117 if Sloc (N1) > Res then
6118 Res := Sloc (N1);
6119 end if;
6121 N1 := Parent (N1);
6122 end loop;
6124 return Res;
6125 end True_Sloc;
6127 -- Start of processing for Install_Body
6129 begin
6130 -- If the body is a subunit, the freeze point is the corresponding
6131 -- stub in the current compilation, not the subunit itself.
6133 if Nkind (Parent (Gen_Body)) = N_Subunit then
6134 Orig_Body := Corresponding_Stub (Parent (Gen_Body));
6135 else
6136 Orig_Body := Gen_Body;
6137 end if;
6139 Body_Unit := Unit (Cunit (Get_Source_Unit (Orig_Body)));
6141 -- If the instantiation and the generic definition appear in the
6142 -- same package declaration, this is an early instantiation.
6143 -- If they appear in the same declarative part, it is an early
6144 -- instantiation only if the generic body appears textually later,
6145 -- and the generic body is also in the main unit.
6147 -- If instance is nested within a subprogram, and the generic body is
6148 -- not, the instance is delayed because the enclosing body is. If
6149 -- instance and body are within the same scope, or the same sub-
6150 -- program body, indicate explicitly that the instance is delayed.
6152 Must_Delay :=
6153 (Gen_Unit = Act_Unit
6154 and then ((Nkind (Gen_Unit) = N_Package_Declaration)
6155 or else Nkind (Gen_Unit) = N_Generic_Package_Declaration
6156 or else (Gen_Unit = Body_Unit
6157 and then True_Sloc (N) < Sloc (Orig_Body)))
6158 and then Is_In_Main_Unit (Gen_Unit)
6159 and then (Scope (Act_Id) = Scope (Gen_Id)
6160 or else
6161 Enclosing_Subp (Act_Id) = Enclosing_Subp (Gen_Id)));
6163 -- If this is an early instantiation, the freeze node is placed after
6164 -- the generic body. Otherwise, if the generic appears in an instance,
6165 -- we cannot freeze the current instance until the outer one is frozen.
6166 -- This is only relevant if the current instance is nested within some
6167 -- inner scope not itself within the outer instance. If this scope is
6168 -- a package body in the same declarative part as the outer instance,
6169 -- then that body needs to be frozen after the outer instance. Finally,
6170 -- if no delay is needed, we place the freeze node at the end of the
6171 -- current declarative part.
6173 if Expander_Active then
6174 Ensure_Freeze_Node (Act_Id);
6175 F_Node := Freeze_Node (Act_Id);
6177 if Must_Delay then
6178 Insert_After (Orig_Body, F_Node);
6180 elsif Is_Generic_Instance (Par)
6181 and then Present (Freeze_Node (Par))
6182 and then Scope (Act_Id) /= Par
6183 then
6184 -- Freeze instance of inner generic after instance of enclosing
6185 -- generic.
6187 if In_Same_Declarative_Part (Freeze_Node (Par), N) then
6188 Insert_After (Freeze_Node (Par), F_Node);
6190 -- Freeze package enclosing instance of inner generic after
6191 -- instance of enclosing generic.
6193 elsif Nkind (Parent (N)) = N_Package_Body
6194 and then In_Same_Declarative_Part (Freeze_Node (Par), Parent (N))
6195 then
6197 declare
6198 Enclosing : constant Entity_Id :=
6199 Corresponding_Spec (Parent (N));
6201 begin
6202 Insert_After_Last_Decl (N, F_Node);
6203 Ensure_Freeze_Node (Enclosing);
6205 if not Is_List_Member (Freeze_Node (Enclosing)) then
6206 Insert_After (Freeze_Node (Par), Freeze_Node (Enclosing));
6207 end if;
6208 end;
6210 else
6211 Insert_After_Last_Decl (N, F_Node);
6212 end if;
6214 else
6215 Insert_After_Last_Decl (N, F_Node);
6216 end if;
6217 end if;
6219 Set_Is_Frozen (Act_Id);
6220 Insert_Before (N, Act_Body);
6221 Mark_Rewrite_Insertion (Act_Body);
6222 end Install_Body;
6224 --------------------
6225 -- Install_Parent --
6226 --------------------
6228 procedure Install_Parent (P : Entity_Id; In_Body : Boolean := False) is
6229 Ancestors : constant Elist_Id := New_Elmt_List;
6230 S : constant Entity_Id := Current_Scope;
6231 Inst_Par : Entity_Id;
6232 First_Par : Entity_Id;
6233 Inst_Node : Node_Id;
6234 Gen_Par : Entity_Id;
6235 First_Gen : Entity_Id;
6236 Elmt : Elmt_Id;
6238 procedure Install_Formal_Packages (Par : Entity_Id);
6239 -- If any of the formals of the parent are formal packages with box,
6240 -- their formal parts are visible in the parent and thus in the child
6241 -- unit as well. Analogous to what is done in Check_Generic_Actuals
6242 -- for the unit itself.
6244 procedure Install_Noninstance_Specs (Par : Entity_Id);
6245 -- Install the scopes of noninstance parent units ending with Par
6247 procedure Install_Spec (Par : Entity_Id);
6248 -- The child unit is within the declarative part of the parent, so
6249 -- the declarations within the parent are immediately visible.
6251 -----------------------------
6252 -- Install_Formal_Packages --
6253 -----------------------------
6255 procedure Install_Formal_Packages (Par : Entity_Id) is
6256 E : Entity_Id;
6258 begin
6259 E := First_Entity (Par);
6260 while Present (E) loop
6261 if Ekind (E) = E_Package
6262 and then Nkind (Parent (E)) = N_Package_Renaming_Declaration
6263 then
6264 -- If this is the renaming for the parent instance, done
6266 if Renamed_Object (E) = Par then
6267 exit;
6269 -- The visibility of a formal of an enclosing generic is
6270 -- already correct.
6272 elsif Denotes_Formal_Package (E) then
6273 null;
6275 elsif Present (Associated_Formal_Package (E))
6276 and then Box_Present (Parent (Associated_Formal_Package (E)))
6277 then
6278 Check_Generic_Actuals (Renamed_Object (E), True);
6279 Set_Is_Hidden (E, False);
6280 end if;
6281 end if;
6283 Next_Entity (E);
6284 end loop;
6285 end Install_Formal_Packages;
6287 -------------------------------
6288 -- Install_Noninstance_Specs --
6289 -------------------------------
6291 procedure Install_Noninstance_Specs (Par : Entity_Id) is
6292 begin
6293 if Present (Par)
6294 and then Par /= Standard_Standard
6295 and then not In_Open_Scopes (Par)
6296 then
6297 Install_Noninstance_Specs (Scope (Par));
6298 Install_Spec (Par);
6299 end if;
6300 end Install_Noninstance_Specs;
6302 ------------------
6303 -- Install_Spec --
6304 ------------------
6306 procedure Install_Spec (Par : Entity_Id) is
6307 Spec : constant Node_Id :=
6308 Specification (Unit_Declaration_Node (Par));
6310 begin
6311 if not Is_Child_Unit (Par) then
6312 Parent_Unit_Visible := Is_Immediately_Visible (Par);
6313 end if;
6315 New_Scope (Par);
6316 Set_Is_Immediately_Visible (Par);
6317 Install_Visible_Declarations (Par);
6318 Install_Private_Declarations (Par);
6319 Set_Use (Visible_Declarations (Spec));
6320 Set_Use (Private_Declarations (Spec));
6321 end Install_Spec;
6323 -- Start of processing for Install_Parent
6325 begin
6326 -- We need to install the parent instance to compile the instantiation
6327 -- of the child, but the child instance must appear in the current
6328 -- scope. Given that we cannot place the parent above the current
6329 -- scope in the scope stack, we duplicate the current scope and unstack
6330 -- both after the instantiation is complete.
6332 -- If the parent is itself the instantiation of a child unit, we must
6333 -- also stack the instantiation of its parent, and so on. Each such
6334 -- ancestor is the prefix of the name in a prior instantiation.
6336 -- If this is a nested instance, the parent unit itself resolves to
6337 -- a renaming of the parent instance, whose declaration we need.
6339 -- Finally, the parent may be a generic (not an instance) when the
6340 -- child unit appears as a formal package.
6342 Inst_Par := P;
6344 if Present (Renamed_Entity (Inst_Par)) then
6345 Inst_Par := Renamed_Entity (Inst_Par);
6346 end if;
6348 First_Par := Inst_Par;
6350 Gen_Par :=
6351 Generic_Parent (Specification (Unit_Declaration_Node (Inst_Par)));
6353 First_Gen := Gen_Par;
6355 while Present (Gen_Par)
6356 and then Is_Child_Unit (Gen_Par)
6357 loop
6358 -- Load grandparent instance as well
6360 Inst_Node := Get_Package_Instantiation_Node (Inst_Par);
6362 if Nkind (Name (Inst_Node)) = N_Expanded_Name then
6363 Inst_Par := Entity (Prefix (Name (Inst_Node)));
6365 if Present (Renamed_Entity (Inst_Par)) then
6366 Inst_Par := Renamed_Entity (Inst_Par);
6367 end if;
6369 Gen_Par :=
6370 Generic_Parent
6371 (Specification (Unit_Declaration_Node (Inst_Par)));
6373 if Present (Gen_Par) then
6374 Prepend_Elmt (Inst_Par, Ancestors);
6376 else
6377 -- Parent is not the name of an instantiation
6379 Install_Noninstance_Specs (Inst_Par);
6381 exit;
6382 end if;
6384 else
6385 -- Previous error
6387 exit;
6388 end if;
6389 end loop;
6391 if Present (First_Gen) then
6392 Append_Elmt (First_Par, Ancestors);
6394 else
6395 Install_Noninstance_Specs (First_Par);
6396 end if;
6398 if not Is_Empty_Elmt_List (Ancestors) then
6399 Elmt := First_Elmt (Ancestors);
6401 while Present (Elmt) loop
6402 Install_Spec (Node (Elmt));
6403 Install_Formal_Packages (Node (Elmt));
6405 Next_Elmt (Elmt);
6406 end loop;
6407 end if;
6409 if not In_Body then
6410 New_Scope (S);
6411 end if;
6412 end Install_Parent;
6414 --------------------------------
6415 -- Instantiate_Formal_Package --
6416 --------------------------------
6418 function Instantiate_Formal_Package
6419 (Formal : Node_Id;
6420 Actual : Node_Id;
6421 Analyzed_Formal : Node_Id) return List_Id
6423 Loc : constant Source_Ptr := Sloc (Actual);
6424 Actual_Pack : Entity_Id;
6425 Formal_Pack : Entity_Id;
6426 Gen_Parent : Entity_Id;
6427 Decls : List_Id;
6428 Nod : Node_Id;
6429 Parent_Spec : Node_Id;
6431 procedure Find_Matching_Actual
6432 (F : Node_Id;
6433 Act : in out Entity_Id);
6434 -- We need to associate each formal entity in the formal package
6435 -- with the corresponding entity in the actual package. The actual
6436 -- package has been analyzed and possibly expanded, and as a result
6437 -- there is no one-to-one correspondence between the two lists (for
6438 -- example, the actual may include subtypes, itypes, and inherited
6439 -- primitive operations, interspersed among the renaming declarations
6440 -- for the actuals) . We retrieve the corresponding actual by name
6441 -- because each actual has the same name as the formal, and they do
6442 -- appear in the same order.
6444 function Formal_Entity
6445 (F : Node_Id;
6446 Act_Ent : Entity_Id) return Entity_Id;
6447 -- Returns the entity associated with the given formal F. In the
6448 -- case where F is a formal package, this function will iterate
6449 -- through all of F's formals and enter map associations from the
6450 -- actuals occurring in the formal package's corresponding actual
6451 -- package (obtained via Act_Ent) to the formal package's formal
6452 -- parameters. This function is called recursively for arbitrary
6453 -- levels of formal packages.
6455 function Is_Instance_Of
6456 (Act_Spec : Entity_Id;
6457 Gen_Anc : Entity_Id) return Boolean;
6458 -- The actual can be an instantiation of a generic within another
6459 -- instance, in which case there is no direct link from it to the
6460 -- original generic ancestor. In that case, we recognize that the
6461 -- ultimate ancestor is the same by examining names and scopes.
6463 procedure Map_Entities (Form : Entity_Id; Act : Entity_Id);
6464 -- Within the generic part, entities in the formal package are
6465 -- visible. To validate subsequent type declarations, indicate
6466 -- the correspondence betwen the entities in the analyzed formal,
6467 -- and the entities in the actual package. There are three packages
6468 -- involved in the instantiation of a formal package: the parent
6469 -- generic P1 which appears in the generic declaration, the fake
6470 -- instantiation P2 which appears in the analyzed generic, and whose
6471 -- visible entities may be used in subsequent formals, and the actual
6472 -- P3 in the instance. To validate subsequent formals, me indicate
6473 -- that the entities in P2 are mapped into those of P3. The mapping of
6474 -- entities has to be done recursively for nested packages.
6476 procedure Process_Nested_Formal (Formal : Entity_Id);
6477 -- If the current formal is declared with a box, its own formals are
6478 -- visible in the instance, as they were in the generic, and their
6479 -- Hidden flag must be reset. If some of these formals are themselves
6480 -- packages declared with a box, the processing must be recursive.
6482 --------------------------
6483 -- Find_Matching_Actual --
6484 --------------------------
6486 procedure Find_Matching_Actual
6487 (F : Node_Id;
6488 Act : in out Entity_Id)
6490 Formal_Ent : Entity_Id;
6492 begin
6493 case Nkind (Original_Node (F)) is
6494 when N_Formal_Object_Declaration |
6495 N_Formal_Type_Declaration =>
6496 Formal_Ent := Defining_Identifier (F);
6498 while Chars (Act) /= Chars (Formal_Ent) loop
6499 Next_Entity (Act);
6500 end loop;
6502 when N_Formal_Subprogram_Declaration |
6503 N_Formal_Package_Declaration |
6504 N_Package_Declaration |
6505 N_Generic_Package_Declaration =>
6506 Formal_Ent := Defining_Entity (F);
6508 while Chars (Act) /= Chars (Formal_Ent) loop
6509 Next_Entity (Act);
6510 end loop;
6512 when others =>
6513 raise Program_Error;
6514 end case;
6515 end Find_Matching_Actual;
6517 -------------------
6518 -- Formal_Entity --
6519 -------------------
6521 function Formal_Entity
6522 (F : Node_Id;
6523 Act_Ent : Entity_Id) return Entity_Id
6525 Orig_Node : Node_Id := F;
6526 Act_Pkg : Entity_Id;
6528 begin
6529 case Nkind (Original_Node (F)) is
6530 when N_Formal_Object_Declaration =>
6531 return Defining_Identifier (F);
6533 when N_Formal_Type_Declaration =>
6534 return Defining_Identifier (F);
6536 when N_Formal_Subprogram_Declaration =>
6537 return Defining_Unit_Name (Specification (F));
6539 when N_Package_Declaration =>
6540 return Defining_Unit_Name (Specification (F));
6542 when N_Formal_Package_Declaration |
6543 N_Generic_Package_Declaration =>
6545 if Nkind (F) = N_Generic_Package_Declaration then
6546 Orig_Node := Original_Node (F);
6547 end if;
6549 Act_Pkg := Act_Ent;
6551 -- Find matching actual package, skipping over itypes and
6552 -- other entities generated when analyzing the formal. We
6553 -- know that if the instantiation is legal then there is
6554 -- a matching package for the formal.
6556 while Ekind (Act_Pkg) /= E_Package loop
6557 Act_Pkg := Next_Entity (Act_Pkg);
6558 end loop;
6560 declare
6561 Actual_Ent : Entity_Id := First_Entity (Act_Pkg);
6562 Formal_Node : Node_Id;
6563 Formal_Ent : Entity_Id;
6565 Gen_Decl : Node_Id;
6566 Formals : List_Id;
6568 begin
6569 -- The actual may be a renamed generic package, in which
6570 -- case we want to retrieve the original generic in order
6571 -- to traverse its formal part.
6573 if Present (Renamed_Entity (Entity (Name (Orig_Node)))) then
6574 Gen_Decl :=
6575 Unit_Declaration_Node (
6576 Renamed_Entity (Entity (Name (Orig_Node))));
6577 else
6578 Gen_Decl :=
6579 Unit_Declaration_Node (Entity (Name (Orig_Node)));
6580 end if;
6582 Formals := Generic_Formal_Declarations (Gen_Decl);
6584 if Present (Formals) then
6585 Formal_Node := First_Non_Pragma (Formals);
6586 else
6587 Formal_Node := Empty;
6588 end if;
6590 while Present (Actual_Ent)
6591 and then Present (Formal_Node)
6592 and then Actual_Ent /= First_Private_Entity (Act_Pkg)
6593 loop
6594 -- ??? Are the following calls also needed here:
6596 -- Set_Is_Hidden (Actual_Ent, False);
6597 -- Set_Is_Potentially_Use_Visible
6598 -- (Actual_Ent, In_Use (Act_Ent));
6600 Formal_Ent := Formal_Entity (Formal_Node, Actual_Ent);
6601 if Present (Formal_Ent) then
6602 Set_Instance_Of (Formal_Ent, Actual_Ent);
6603 end if;
6604 Next_Non_Pragma (Formal_Node);
6606 Next_Entity (Actual_Ent);
6607 end loop;
6608 end;
6610 return Defining_Identifier (Orig_Node);
6612 when N_Use_Package_Clause =>
6613 return Empty;
6615 when N_Use_Type_Clause =>
6616 return Empty;
6618 -- We return Empty for all other encountered forms of
6619 -- declarations because there are some cases of nonformal
6620 -- sorts of declaration that can show up (e.g., when array
6621 -- formals are present). Since it's not clear what kinds
6622 -- can appear among the formals, we won't raise failure here.
6624 when others =>
6625 return Empty;
6627 end case;
6628 end Formal_Entity;
6630 --------------------
6631 -- Is_Instance_Of --
6632 --------------------
6634 function Is_Instance_Of
6635 (Act_Spec : Entity_Id;
6636 Gen_Anc : Entity_Id) return Boolean
6638 Gen_Par : constant Entity_Id := Generic_Parent (Act_Spec);
6640 begin
6641 if No (Gen_Par) then
6642 return False;
6644 -- Simplest case: the generic parent of the actual is the formal
6646 elsif Gen_Par = Gen_Anc then
6647 return True;
6649 elsif Chars (Gen_Par) /= Chars (Gen_Anc) then
6650 return False;
6652 -- The actual may be obtained through several instantiations. Its
6653 -- scope must itself be an instance of a generic declared in the
6654 -- same scope as the formal. Any other case is detected above.
6656 elsif not Is_Generic_Instance (Scope (Gen_Par)) then
6657 return False;
6659 else
6660 return Generic_Parent (Parent (Scope (Gen_Par))) = Scope (Gen_Anc);
6661 end if;
6662 end Is_Instance_Of;
6664 ------------------
6665 -- Map_Entities --
6666 ------------------
6668 procedure Map_Entities (Form : Entity_Id; Act : Entity_Id) is
6669 E1 : Entity_Id;
6670 E2 : Entity_Id;
6672 begin
6673 Set_Instance_Of (Form, Act);
6675 -- Traverse formal and actual package to map the corresponding
6676 -- entities. We skip over internal entities that may be generated
6677 -- during semantic analysis, and find the matching entities by
6678 -- name, given that they must appear in the same order.
6680 E1 := First_Entity (Form);
6681 E2 := First_Entity (Act);
6682 while Present (E1)
6683 and then E1 /= First_Private_Entity (Form)
6684 loop
6685 if not Is_Internal (E1)
6686 and then not Is_Class_Wide_Type (E1)
6687 and then Present (Parent (E1))
6688 then
6689 while Present (E2)
6690 and then Chars (E2) /= Chars (E1)
6691 loop
6692 Next_Entity (E2);
6693 end loop;
6695 if No (E2) then
6696 exit;
6697 else
6698 Set_Instance_Of (E1, E2);
6700 if Is_Type (E1)
6701 and then Is_Tagged_Type (E2)
6702 then
6703 Set_Instance_Of
6704 (Class_Wide_Type (E1), Class_Wide_Type (E2));
6705 end if;
6707 if Ekind (E1) = E_Package
6708 and then No (Renamed_Object (E1))
6709 then
6710 Map_Entities (E1, E2);
6711 end if;
6712 end if;
6713 end if;
6715 Next_Entity (E1);
6716 end loop;
6717 end Map_Entities;
6719 ---------------------------
6720 -- Process_Nested_Formal --
6721 ---------------------------
6723 procedure Process_Nested_Formal (Formal : Entity_Id) is
6724 Ent : Entity_Id;
6726 begin
6727 if Present (Associated_Formal_Package (Formal))
6728 and then Box_Present (Parent (Associated_Formal_Package (Formal)))
6729 then
6730 Ent := First_Entity (Formal);
6731 while Present (Ent) loop
6732 Set_Is_Hidden (Ent, False);
6733 Set_Is_Potentially_Use_Visible
6734 (Ent, Is_Potentially_Use_Visible (Formal));
6736 if Ekind (Ent) = E_Package then
6737 exit when Renamed_Entity (Ent) = Renamed_Entity (Formal);
6738 Process_Nested_Formal (Ent);
6739 end if;
6741 Next_Entity (Ent);
6742 end loop;
6743 end if;
6744 end Process_Nested_Formal;
6746 -- Start of processing for Instantiate_Formal_Package
6748 begin
6749 Analyze (Actual);
6751 if not Is_Entity_Name (Actual)
6752 or else Ekind (Entity (Actual)) /= E_Package
6753 then
6754 Error_Msg_N
6755 ("expect package instance to instantiate formal", Actual);
6756 Abandon_Instantiation (Actual);
6757 raise Program_Error;
6759 else
6760 Actual_Pack := Entity (Actual);
6761 Set_Is_Instantiated (Actual_Pack);
6763 -- The actual may be a renamed package, or an outer generic
6764 -- formal package whose instantiation is converted into a renaming.
6766 if Present (Renamed_Object (Actual_Pack)) then
6767 Actual_Pack := Renamed_Object (Actual_Pack);
6768 end if;
6770 if Nkind (Analyzed_Formal) = N_Formal_Package_Declaration then
6771 Gen_Parent := Get_Instance_Of (Entity (Name (Analyzed_Formal)));
6772 Formal_Pack := Defining_Identifier (Analyzed_Formal);
6773 else
6774 Gen_Parent :=
6775 Generic_Parent (Specification (Analyzed_Formal));
6776 Formal_Pack :=
6777 Defining_Unit_Name (Specification (Analyzed_Formal));
6778 end if;
6780 if Nkind (Parent (Actual_Pack)) = N_Defining_Program_Unit_Name then
6781 Parent_Spec := Specification (Unit_Declaration_Node (Actual_Pack));
6782 else
6783 Parent_Spec := Parent (Actual_Pack);
6784 end if;
6786 if Gen_Parent = Any_Id then
6787 Error_Msg_N
6788 ("previous error in declaration of formal package", Actual);
6789 Abandon_Instantiation (Actual);
6791 elsif
6792 Is_Instance_Of (Parent_Spec, Get_Instance_Of (Gen_Parent))
6793 then
6794 null;
6796 else
6797 Error_Msg_NE
6798 ("actual parameter must be instance of&", Actual, Gen_Parent);
6799 Abandon_Instantiation (Actual);
6800 end if;
6802 Set_Instance_Of (Defining_Identifier (Formal), Actual_Pack);
6803 Map_Entities (Formal_Pack, Actual_Pack);
6805 Nod :=
6806 Make_Package_Renaming_Declaration (Loc,
6807 Defining_Unit_Name => New_Copy (Defining_Identifier (Formal)),
6808 Name => New_Reference_To (Actual_Pack, Loc));
6810 Set_Associated_Formal_Package (Defining_Unit_Name (Nod),
6811 Defining_Identifier (Formal));
6812 Decls := New_List (Nod);
6814 -- If the formal F has a box, then the generic declarations are
6815 -- visible in the generic G. In an instance of G, the corresponding
6816 -- entities in the actual for F (which are the actuals for the
6817 -- instantiation of the generic that F denotes) must also be made
6818 -- visible for analysis of the current instance. On exit from the
6819 -- current instance, those entities are made private again. If the
6820 -- actual is currently in use, these entities are also use-visible.
6822 -- The loop through the actual entities also steps through the
6823 -- formal entities and enters associations from formals to
6824 -- actuals into the renaming map. This is necessary to properly
6825 -- handle checking of actual parameter associations for later
6826 -- formals that depend on actuals declared in the formal package.
6828 if Box_Present (Formal) then
6829 declare
6830 Gen_Decl : constant Node_Id :=
6831 Unit_Declaration_Node (Gen_Parent);
6832 Formals : constant List_Id :=
6833 Generic_Formal_Declarations (Gen_Decl);
6834 Actual_Ent : Entity_Id;
6835 Formal_Node : Node_Id;
6836 Formal_Ent : Entity_Id;
6838 begin
6839 if Present (Formals) then
6840 Formal_Node := First_Non_Pragma (Formals);
6841 else
6842 Formal_Node := Empty;
6843 end if;
6845 Actual_Ent := First_Entity (Actual_Pack);
6847 while Present (Actual_Ent)
6848 and then Actual_Ent /= First_Private_Entity (Actual_Pack)
6849 loop
6850 Set_Is_Hidden (Actual_Ent, False);
6851 Set_Is_Potentially_Use_Visible
6852 (Actual_Ent, In_Use (Actual_Pack));
6854 if Ekind (Actual_Ent) = E_Package then
6855 Process_Nested_Formal (Actual_Ent);
6856 end if;
6858 if Present (Formal_Node) then
6859 Formal_Ent := Formal_Entity (Formal_Node, Actual_Ent);
6861 if Present (Formal_Ent) then
6862 Find_Matching_Actual (Formal_Node, Actual_Ent);
6863 Set_Instance_Of (Formal_Ent, Actual_Ent);
6864 end if;
6866 Next_Non_Pragma (Formal_Node);
6868 else
6869 -- No further formals to match, but the generic
6870 -- part may contain inherited operation that are
6871 -- not hidden in the enclosing instance.
6873 Next_Entity (Actual_Ent);
6874 end if;
6876 end loop;
6877 end;
6879 -- If the formal is not declared with a box, reanalyze it as
6880 -- an instantiation, to verify the matching rules of 12.7. The
6881 -- actual checks are performed after the generic associations
6882 -- been analyzed.
6884 else
6885 declare
6886 I_Pack : constant Entity_Id :=
6887 Make_Defining_Identifier (Sloc (Actual),
6888 Chars => New_Internal_Name ('P'));
6890 begin
6891 Set_Is_Internal (I_Pack);
6893 Append_To (Decls,
6894 Make_Package_Instantiation (Sloc (Actual),
6895 Defining_Unit_Name => I_Pack,
6896 Name => New_Occurrence_Of (Gen_Parent, Sloc (Actual)),
6897 Generic_Associations =>
6898 Generic_Associations (Formal)));
6899 end;
6900 end if;
6902 return Decls;
6903 end if;
6904 end Instantiate_Formal_Package;
6906 -----------------------------------
6907 -- Instantiate_Formal_Subprogram --
6908 -----------------------------------
6910 function Instantiate_Formal_Subprogram
6911 (Formal : Node_Id;
6912 Actual : Node_Id;
6913 Analyzed_Formal : Node_Id) return Node_Id
6915 Loc : Source_Ptr := Sloc (Instantiation_Node);
6916 Formal_Sub : constant Entity_Id :=
6917 Defining_Unit_Name (Specification (Formal));
6918 Analyzed_S : constant Entity_Id :=
6919 Defining_Unit_Name (Specification (Analyzed_Formal));
6920 Decl_Node : Node_Id;
6921 Nam : Node_Id;
6922 New_Spec : Node_Id;
6924 function From_Parent_Scope (Subp : Entity_Id) return Boolean;
6925 -- If the generic is a child unit, the parent has been installed on the
6926 -- scope stack, but a default subprogram cannot resolve to something on
6927 -- the parent because that parent is not really part of the visible
6928 -- context (it is there to resolve explicit local entities). If the
6929 -- default has resolved in this way, we remove the entity from
6930 -- immediate visibility and analyze the node again to emit an error
6931 -- message or find another visible candidate.
6933 procedure Valid_Actual_Subprogram (Act : Node_Id);
6934 -- Perform legality check and raise exception on failure
6936 -----------------------
6937 -- From_Parent_Scope --
6938 -----------------------
6940 function From_Parent_Scope (Subp : Entity_Id) return Boolean is
6941 Gen_Scope : Node_Id := Scope (Analyzed_S);
6943 begin
6944 while Present (Gen_Scope)
6945 and then Is_Child_Unit (Gen_Scope)
6946 loop
6947 if Scope (Subp) = Scope (Gen_Scope) then
6948 return True;
6949 end if;
6951 Gen_Scope := Scope (Gen_Scope);
6952 end loop;
6954 return False;
6955 end From_Parent_Scope;
6957 -----------------------------
6958 -- Valid_Actual_Subprogram --
6959 -----------------------------
6961 procedure Valid_Actual_Subprogram (Act : Node_Id) is
6962 Act_E : Entity_Id := Empty;
6964 begin
6965 if Is_Entity_Name (Act) then
6966 Act_E := Entity (Act);
6967 elsif Nkind (Act) = N_Selected_Component
6968 and then Is_Entity_Name (Selector_Name (Act))
6969 then
6970 Act_E := Entity (Selector_Name (Act));
6971 end if;
6973 if (Present (Act_E) and then Is_Overloadable (Act_E))
6974 or else Nkind (Act) = N_Attribute_Reference
6975 or else Nkind (Act) = N_Indexed_Component
6976 or else Nkind (Act) = N_Character_Literal
6977 or else Nkind (Act) = N_Explicit_Dereference
6978 then
6979 return;
6980 end if;
6982 Error_Msg_NE
6983 ("expect subprogram or entry name in instantiation of&",
6984 Instantiation_Node, Formal_Sub);
6985 Abandon_Instantiation (Instantiation_Node);
6987 end Valid_Actual_Subprogram;
6989 -- Start of processing for Instantiate_Formal_Subprogram
6991 begin
6992 New_Spec := New_Copy_Tree (Specification (Formal));
6994 -- Create new entity for the actual (New_Copy_Tree does not)
6996 Set_Defining_Unit_Name
6997 (New_Spec, Make_Defining_Identifier (Loc, Chars (Formal_Sub)));
6999 -- Find entity of actual. If the actual is an attribute reference, it
7000 -- cannot be resolved here (its formal is missing) but is handled
7001 -- instead in Attribute_Renaming. If the actual is overloaded, it is
7002 -- fully resolved subsequently, when the renaming declaration for the
7003 -- formal is analyzed. If it is an explicit dereference, resolve the
7004 -- prefix but not the actual itself, to prevent interpretation as a
7005 -- call.
7007 if Present (Actual) then
7008 Loc := Sloc (Actual);
7009 Set_Sloc (New_Spec, Loc);
7011 if Nkind (Actual) = N_Operator_Symbol then
7012 Find_Direct_Name (Actual);
7014 elsif Nkind (Actual) = N_Explicit_Dereference then
7015 Analyze (Prefix (Actual));
7017 elsif Nkind (Actual) /= N_Attribute_Reference then
7018 Analyze (Actual);
7019 end if;
7021 Valid_Actual_Subprogram (Actual);
7022 Nam := Actual;
7024 elsif Present (Default_Name (Formal)) then
7025 if Nkind (Default_Name (Formal)) /= N_Attribute_Reference
7026 and then Nkind (Default_Name (Formal)) /= N_Selected_Component
7027 and then Nkind (Default_Name (Formal)) /= N_Indexed_Component
7028 and then Nkind (Default_Name (Formal)) /= N_Character_Literal
7029 and then Present (Entity (Default_Name (Formal)))
7030 then
7031 Nam := New_Occurrence_Of (Entity (Default_Name (Formal)), Loc);
7032 else
7033 Nam := New_Copy (Default_Name (Formal));
7034 Set_Sloc (Nam, Loc);
7035 end if;
7037 elsif Box_Present (Formal) then
7039 -- Actual is resolved at the point of instantiation. Create
7040 -- an identifier or operator with the same name as the formal.
7042 if Nkind (Formal_Sub) = N_Defining_Operator_Symbol then
7043 Nam := Make_Operator_Symbol (Loc,
7044 Chars => Chars (Formal_Sub),
7045 Strval => No_String);
7046 else
7047 Nam := Make_Identifier (Loc, Chars (Formal_Sub));
7048 end if;
7050 elsif Nkind (Specification (Formal)) = N_Procedure_Specification
7051 and then Null_Present (Specification (Formal))
7052 then
7053 -- Generate null body for procedure, for use in the instance
7055 Decl_Node :=
7056 Make_Subprogram_Body (Loc,
7057 Specification => New_Spec,
7058 Declarations => New_List,
7059 Handled_Statement_Sequence =>
7060 Make_Handled_Sequence_Of_Statements (Loc,
7061 Statements => New_List (Make_Null_Statement (Loc))));
7063 Set_Is_Intrinsic_Subprogram (Defining_Unit_Name (New_Spec));
7064 return Decl_Node;
7066 else
7067 Error_Msg_Sloc := Sloc (Scope (Analyzed_S));
7068 Error_Msg_NE
7069 ("missing actual&", Instantiation_Node, Formal_Sub);
7070 Error_Msg_NE
7071 ("\in instantiation of & declared#",
7072 Instantiation_Node, Scope (Analyzed_S));
7073 Abandon_Instantiation (Instantiation_Node);
7074 end if;
7076 Decl_Node :=
7077 Make_Subprogram_Renaming_Declaration (Loc,
7078 Specification => New_Spec,
7079 Name => Nam);
7081 -- If we do not have an actual and the formal specified <> then
7082 -- set to get proper default.
7084 if No (Actual) and then Box_Present (Formal) then
7085 Set_From_Default (Decl_Node);
7086 end if;
7088 -- Gather possible interpretations for the actual before analyzing the
7089 -- instance. If overloaded, it will be resolved when analyzing the
7090 -- renaming declaration.
7092 if Box_Present (Formal)
7093 and then No (Actual)
7094 then
7095 Analyze (Nam);
7097 if Is_Child_Unit (Scope (Analyzed_S))
7098 and then Present (Entity (Nam))
7099 then
7100 if not Is_Overloaded (Nam) then
7102 if From_Parent_Scope (Entity (Nam)) then
7103 Set_Is_Immediately_Visible (Entity (Nam), False);
7104 Set_Entity (Nam, Empty);
7105 Set_Etype (Nam, Empty);
7107 Analyze (Nam);
7109 Set_Is_Immediately_Visible (Entity (Nam));
7110 end if;
7112 else
7113 declare
7114 I : Interp_Index;
7115 It : Interp;
7117 begin
7118 Get_First_Interp (Nam, I, It);
7120 while Present (It.Nam) loop
7121 if From_Parent_Scope (It.Nam) then
7122 Remove_Interp (I);
7123 end if;
7125 Get_Next_Interp (I, It);
7126 end loop;
7127 end;
7128 end if;
7129 end if;
7130 end if;
7132 -- The generic instantiation freezes the actual. This can only be
7133 -- done once the actual is resolved, in the analysis of the renaming
7134 -- declaration. To make the formal subprogram entity available, we set
7135 -- Corresponding_Formal_Spec to point to the formal subprogram entity.
7136 -- This is also needed in Analyze_Subprogram_Renaming for the processing
7137 -- of formal abstract subprograms.
7139 Set_Corresponding_Formal_Spec (Decl_Node, Analyzed_S);
7141 -- We cannot analyze the renaming declaration, and thus find the
7142 -- actual, until the all the actuals are assembled in the instance.
7143 -- For subsequent checks of other actuals, indicate the node that
7144 -- will hold the instance of this formal.
7146 Set_Instance_Of (Analyzed_S, Nam);
7148 if Nkind (Actual) = N_Selected_Component
7149 and then Is_Task_Type (Etype (Prefix (Actual)))
7150 and then not Is_Frozen (Etype (Prefix (Actual)))
7151 then
7152 -- The renaming declaration will create a body, which must appear
7153 -- outside of the instantiation, We move the renaming declaration
7154 -- out of the instance, and create an additional renaming inside,
7155 -- to prevent freezing anomalies.
7157 declare
7158 Anon_Id : constant Entity_Id :=
7159 Make_Defining_Identifier
7160 (Loc, New_Internal_Name ('E'));
7161 begin
7162 Set_Defining_Unit_Name (New_Spec, Anon_Id);
7163 Insert_Before (Instantiation_Node, Decl_Node);
7164 Analyze (Decl_Node);
7166 -- Now create renaming within the instance
7168 Decl_Node :=
7169 Make_Subprogram_Renaming_Declaration (Loc,
7170 Specification => New_Copy_Tree (New_Spec),
7171 Name => New_Occurrence_Of (Anon_Id, Loc));
7173 Set_Defining_Unit_Name (Specification (Decl_Node),
7174 Make_Defining_Identifier (Loc, Chars (Formal_Sub)));
7175 end;
7176 end if;
7178 return Decl_Node;
7179 end Instantiate_Formal_Subprogram;
7181 ------------------------
7182 -- Instantiate_Object --
7183 ------------------------
7185 function Instantiate_Object
7186 (Formal : Node_Id;
7187 Actual : Node_Id;
7188 Analyzed_Formal : Node_Id) return List_Id
7190 Formal_Id : constant Entity_Id := Defining_Identifier (Formal);
7191 Type_Id : constant Node_Id := Subtype_Mark (Formal);
7192 Loc : constant Source_Ptr := Sloc (Actual);
7193 Act_Assoc : constant Node_Id := Parent (Actual);
7194 Orig_Ftyp : constant Entity_Id :=
7195 Etype (Defining_Identifier (Analyzed_Formal));
7196 List : constant List_Id := New_List;
7197 Ftyp : Entity_Id;
7198 Decl_Node : Node_Id;
7199 Subt_Decl : Node_Id := Empty;
7201 begin
7202 -- Sloc for error message on missing actual
7204 Error_Msg_Sloc := Sloc (Scope (Defining_Identifier (Analyzed_Formal)));
7206 if Get_Instance_Of (Formal_Id) /= Formal_Id then
7207 Error_Msg_N ("duplicate instantiation of generic parameter", Actual);
7208 end if;
7210 Set_Parent (List, Parent (Actual));
7212 -- OUT present
7214 if Out_Present (Formal) then
7216 -- An IN OUT generic actual must be a name. The instantiation is a
7217 -- renaming declaration. The actual is the name being renamed. We
7218 -- use the actual directly, rather than a copy, because it is not
7219 -- used further in the list of actuals, and because a copy or a use
7220 -- of relocate_node is incorrect if the instance is nested within a
7221 -- generic. In order to simplify ASIS searches, the Generic_Parent
7222 -- field links the declaration to the generic association.
7224 if No (Actual) then
7225 Error_Msg_NE
7226 ("missing actual&",
7227 Instantiation_Node, Formal_Id);
7228 Error_Msg_NE
7229 ("\in instantiation of & declared#",
7230 Instantiation_Node,
7231 Scope (Defining_Identifier (Analyzed_Formal)));
7232 Abandon_Instantiation (Instantiation_Node);
7233 end if;
7235 Decl_Node :=
7236 Make_Object_Renaming_Declaration (Loc,
7237 Defining_Identifier => New_Copy (Formal_Id),
7238 Subtype_Mark => New_Copy_Tree (Type_Id),
7239 Name => Actual);
7241 Set_Corresponding_Generic_Association (Decl_Node, Act_Assoc);
7243 -- The analysis of the actual may produce insert_action nodes, so
7244 -- the declaration must have a context in which to attach them.
7246 Append (Decl_Node, List);
7247 Analyze (Actual);
7249 -- Return if the analysis of the actual reported some error
7251 if Etype (Actual) = Any_Type then
7252 return List;
7253 end if;
7255 -- This check is performed here because Analyze_Object_Renaming
7256 -- will not check it when Comes_From_Source is False. Note
7257 -- though that the check for the actual being the name of an
7258 -- object will be performed in Analyze_Object_Renaming.
7260 if Is_Object_Reference (Actual)
7261 and then Is_Dependent_Component_Of_Mutable_Object (Actual)
7262 then
7263 Error_Msg_N
7264 ("illegal discriminant-dependent component for in out parameter",
7265 Actual);
7266 end if;
7268 -- The actual has to be resolved in order to check that it is
7269 -- a variable (due to cases such as F(1), where F returns
7270 -- access to an array, and for overloaded prefixes).
7272 Ftyp :=
7273 Get_Instance_Of (Etype (Defining_Identifier (Analyzed_Formal)));
7275 if Is_Private_Type (Ftyp)
7276 and then not Is_Private_Type (Etype (Actual))
7277 and then (Base_Type (Full_View (Ftyp)) = Base_Type (Etype (Actual))
7278 or else Base_Type (Etype (Actual)) = Ftyp)
7279 then
7280 -- If the actual has the type of the full view of the formal,
7281 -- or else a non-private subtype of the formal, then
7282 -- the visibility of the formal type has changed. Add to the
7283 -- actuals a subtype declaration that will force the exchange
7284 -- of views in the body of the instance as well.
7286 Subt_Decl :=
7287 Make_Subtype_Declaration (Loc,
7288 Defining_Identifier =>
7289 Make_Defining_Identifier (Loc, New_Internal_Name ('P')),
7290 Subtype_Indication => New_Occurrence_Of (Ftyp, Loc));
7292 Prepend (Subt_Decl, List);
7294 Prepend_Elmt (Full_View (Ftyp), Exchanged_Views);
7295 Exchange_Declarations (Ftyp);
7296 end if;
7298 Resolve (Actual, Ftyp);
7300 if not Is_Variable (Actual) or else Paren_Count (Actual) > 0 then
7301 Error_Msg_NE
7302 ("actual for& must be a variable", Actual, Formal_Id);
7304 elsif Base_Type (Ftyp) /= Base_Type (Etype (Actual)) then
7305 Error_Msg_NE (
7306 "type of actual does not match type of&", Actual, Formal_Id);
7308 end if;
7310 Note_Possible_Modification (Actual);
7312 -- Check for instantiation of atomic/volatile actual for
7313 -- non-atomic/volatile formal (RM C.6 (12)).
7315 if Is_Atomic_Object (Actual)
7316 and then not Is_Atomic (Orig_Ftyp)
7317 then
7318 Error_Msg_N
7319 ("cannot instantiate non-atomic formal object " &
7320 "with atomic actual", Actual);
7322 elsif Is_Volatile_Object (Actual)
7323 and then not Is_Volatile (Orig_Ftyp)
7324 then
7325 Error_Msg_N
7326 ("cannot instantiate non-volatile formal object " &
7327 "with volatile actual", Actual);
7328 end if;
7330 -- OUT not present
7332 else
7333 -- The instantiation of a generic formal in-parameter
7334 -- is a constant declaration. The actual is the expression for
7335 -- that declaration.
7337 if Present (Actual) then
7339 Decl_Node := Make_Object_Declaration (Loc,
7340 Defining_Identifier => New_Copy (Formal_Id),
7341 Constant_Present => True,
7342 Object_Definition => New_Copy_Tree (Type_Id),
7343 Expression => Actual);
7345 Set_Corresponding_Generic_Association (Decl_Node, Act_Assoc);
7347 -- A generic formal object of a tagged type is defined
7348 -- to be aliased so the new constant must also be treated
7349 -- as aliased.
7351 if Is_Tagged_Type
7352 (Etype (Defining_Identifier (Analyzed_Formal)))
7353 then
7354 Set_Aliased_Present (Decl_Node);
7355 end if;
7357 Append (Decl_Node, List);
7359 -- No need to repeat (pre-)analysis of some expression nodes
7360 -- already handled in Pre_Analyze_Actuals.
7362 if Nkind (Actual) /= N_Allocator then
7363 Analyze (Actual);
7365 -- Return if the analysis of the actual reported some error
7367 if Etype (Actual) = Any_Type then
7368 return List;
7369 end if;
7370 end if;
7372 declare
7373 Typ : constant Entity_Id :=
7374 Get_Instance_Of
7375 (Etype (Defining_Identifier (Analyzed_Formal)));
7377 begin
7378 Freeze_Before (Instantiation_Node, Typ);
7380 -- If the actual is an aggregate, perform name resolution on
7381 -- its components (the analysis of an aggregate does not do
7382 -- it) to capture local names that may be hidden if the
7383 -- generic is a child unit.
7385 if Nkind (Actual) = N_Aggregate then
7386 Pre_Analyze_And_Resolve (Actual, Typ);
7387 end if;
7388 end;
7390 elsif Present (Expression (Formal)) then
7392 -- Use default to construct declaration
7394 Decl_Node :=
7395 Make_Object_Declaration (Sloc (Formal),
7396 Defining_Identifier => New_Copy (Formal_Id),
7397 Constant_Present => True,
7398 Object_Definition => New_Copy (Type_Id),
7399 Expression => New_Copy_Tree (Expression (Formal)));
7401 Append (Decl_Node, List);
7402 Set_Analyzed (Expression (Decl_Node), False);
7404 else
7405 Error_Msg_NE
7406 ("missing actual&",
7407 Instantiation_Node, Formal_Id);
7408 Error_Msg_NE ("\in instantiation of & declared#",
7409 Instantiation_Node,
7410 Scope (Defining_Identifier (Analyzed_Formal)));
7412 if Is_Scalar_Type
7413 (Etype (Defining_Identifier (Analyzed_Formal)))
7414 then
7415 -- Create dummy constant declaration so that instance can
7416 -- be analyzed, to minimize cascaded visibility errors.
7418 Decl_Node :=
7419 Make_Object_Declaration (Loc,
7420 Defining_Identifier => New_Copy (Formal_Id),
7421 Constant_Present => True,
7422 Object_Definition => New_Copy (Type_Id),
7423 Expression =>
7424 Make_Attribute_Reference (Sloc (Formal_Id),
7425 Attribute_Name => Name_First,
7426 Prefix => New_Copy (Type_Id)));
7428 Append (Decl_Node, List);
7430 else
7431 Abandon_Instantiation (Instantiation_Node);
7432 end if;
7433 end if;
7435 end if;
7437 return List;
7438 end Instantiate_Object;
7440 ------------------------------
7441 -- Instantiate_Package_Body --
7442 ------------------------------
7444 procedure Instantiate_Package_Body
7445 (Body_Info : Pending_Body_Info;
7446 Inlined_Body : Boolean := False)
7448 Act_Decl : constant Node_Id := Body_Info.Act_Decl;
7449 Inst_Node : constant Node_Id := Body_Info.Inst_Node;
7450 Loc : constant Source_Ptr := Sloc (Inst_Node);
7452 Gen_Id : constant Node_Id := Name (Inst_Node);
7453 Gen_Unit : constant Entity_Id := Get_Generic_Entity (Inst_Node);
7454 Gen_Decl : constant Node_Id := Unit_Declaration_Node (Gen_Unit);
7455 Act_Spec : constant Node_Id := Specification (Act_Decl);
7456 Act_Decl_Id : constant Entity_Id := Defining_Entity (Act_Spec);
7458 Act_Body_Name : Node_Id;
7459 Gen_Body : Node_Id;
7460 Gen_Body_Id : Node_Id;
7461 Act_Body : Node_Id;
7462 Act_Body_Id : Entity_Id;
7464 Parent_Installed : Boolean := False;
7465 Save_Style_Check : constant Boolean := Style_Check;
7467 begin
7468 Gen_Body_Id := Corresponding_Body (Gen_Decl);
7470 -- The instance body may already have been processed, as the parent
7471 -- of another instance that is inlined. (Load_Parent_Of_Generic).
7473 if Present (Corresponding_Body (Instance_Spec (Inst_Node))) then
7474 return;
7475 end if;
7477 Expander_Mode_Save_And_Set (Body_Info.Expander_Status);
7479 if No (Gen_Body_Id) then
7480 Load_Parent_Of_Generic (Inst_Node, Specification (Gen_Decl));
7481 Gen_Body_Id := Corresponding_Body (Gen_Decl);
7482 end if;
7484 -- Establish global variable for sloc adjustment and for error
7485 -- recovery.
7487 Instantiation_Node := Inst_Node;
7489 if Present (Gen_Body_Id) then
7490 Save_Env (Gen_Unit, Act_Decl_Id);
7491 Style_Check := False;
7492 Current_Sem_Unit := Body_Info.Current_Sem_Unit;
7494 Gen_Body := Unit_Declaration_Node (Gen_Body_Id);
7496 Create_Instantiation_Source
7497 (Inst_Node, Gen_Body_Id, False, S_Adjustment);
7499 Act_Body :=
7500 Copy_Generic_Node
7501 (Original_Node (Gen_Body), Empty, Instantiating => True);
7503 -- Build new name (possibly qualified) for body declaration
7505 Act_Body_Id := New_Copy (Act_Decl_Id);
7507 -- Some attributes of the spec entity are not inherited by the
7508 -- body entity.
7510 Set_Handler_Records (Act_Body_Id, No_List);
7512 if Nkind (Defining_Unit_Name (Act_Spec)) =
7513 N_Defining_Program_Unit_Name
7514 then
7515 Act_Body_Name :=
7516 Make_Defining_Program_Unit_Name (Loc,
7517 Name => New_Copy_Tree (Name (Defining_Unit_Name (Act_Spec))),
7518 Defining_Identifier => Act_Body_Id);
7519 else
7520 Act_Body_Name := Act_Body_Id;
7521 end if;
7523 Set_Defining_Unit_Name (Act_Body, Act_Body_Name);
7525 Set_Corresponding_Spec (Act_Body, Act_Decl_Id);
7526 Check_Generic_Actuals (Act_Decl_Id, False);
7528 -- If it is a child unit, make the parent instance (which is an
7529 -- instance of the parent of the generic) visible. The parent
7530 -- instance is the prefix of the name of the generic unit.
7532 if Ekind (Scope (Gen_Unit)) = E_Generic_Package
7533 and then Nkind (Gen_Id) = N_Expanded_Name
7534 then
7535 Install_Parent (Entity (Prefix (Gen_Id)), In_Body => True);
7536 Parent_Installed := True;
7538 elsif Is_Child_Unit (Gen_Unit) then
7539 Install_Parent (Scope (Gen_Unit), In_Body => True);
7540 Parent_Installed := True;
7541 end if;
7543 -- If the instantiation is a library unit, and this is the main
7544 -- unit, then build the resulting compilation unit nodes for the
7545 -- instance. If this is a compilation unit but it is not the main
7546 -- unit, then it is the body of a unit in the context, that is being
7547 -- compiled because it is encloses some inlined unit or another
7548 -- generic unit being instantiated. In that case, this body is not
7549 -- part of the current compilation, and is not attached to the tree,
7550 -- but its parent must be set for analysis.
7552 if Nkind (Parent (Inst_Node)) = N_Compilation_Unit then
7554 -- Replace instance node with body of instance, and create
7555 -- new node for corresponding instance declaration.
7557 Build_Instance_Compilation_Unit_Nodes
7558 (Inst_Node, Act_Body, Act_Decl);
7559 Analyze (Inst_Node);
7561 if Parent (Inst_Node) = Cunit (Main_Unit) then
7563 -- If the instance is a child unit itself, then set the
7564 -- scope of the expanded body to be the parent of the
7565 -- instantiation (ensuring that the fully qualified name
7566 -- will be generated for the elaboration subprogram).
7568 if Nkind (Defining_Unit_Name (Act_Spec)) =
7569 N_Defining_Program_Unit_Name
7570 then
7571 Set_Scope
7572 (Defining_Entity (Inst_Node), Scope (Act_Decl_Id));
7573 end if;
7574 end if;
7576 -- Case where instantiation is not a library unit
7578 else
7579 -- If this is an early instantiation, i.e. appears textually
7580 -- before the corresponding body and must be elaborated first,
7581 -- indicate that the body instance is to be delayed.
7583 Install_Body (Act_Body, Inst_Node, Gen_Body, Gen_Decl);
7585 -- Now analyze the body. We turn off all checks if this is
7586 -- an internal unit, since there is no reason to have checks
7587 -- on for any predefined run-time library code. All such
7588 -- code is designed to be compiled with checks off.
7590 -- Note that we do NOT apply this criterion to children of
7591 -- GNAT (or on VMS, children of DEC). The latter units must
7592 -- suppress checks explicitly if this is needed.
7594 if Is_Predefined_File_Name
7595 (Unit_File_Name (Get_Source_Unit (Gen_Decl)))
7596 then
7597 Analyze (Act_Body, Suppress => All_Checks);
7598 else
7599 Analyze (Act_Body);
7600 end if;
7601 end if;
7603 if not Generic_Separately_Compiled (Gen_Unit) then
7604 Inherit_Context (Gen_Body, Inst_Node);
7605 end if;
7607 -- Remove the parent instances if they have been placed on the
7608 -- scope stack to compile the body.
7610 if Parent_Installed then
7611 Remove_Parent (In_Body => True);
7612 end if;
7614 Restore_Private_Views (Act_Decl_Id);
7616 -- Remove the current unit from visibility if this is an instance
7617 -- that is not elaborated on the fly for inlining purposes.
7619 if not Inlined_Body then
7620 Set_Is_Immediately_Visible (Act_Decl_Id, False);
7621 end if;
7623 Restore_Env;
7624 Style_Check := Save_Style_Check;
7626 -- If we have no body, and the unit requires a body, then complain.
7627 -- This complaint is suppressed if we have detected other errors
7628 -- (since a common reason for missing the body is that it had errors).
7630 elsif Unit_Requires_Body (Gen_Unit) then
7631 if Serious_Errors_Detected = 0 then
7632 Error_Msg_NE
7633 ("cannot find body of generic package &", Inst_Node, Gen_Unit);
7635 -- Don't attempt to perform any cleanup actions if some other
7636 -- error was aready detected, since this can cause blowups.
7638 else
7639 return;
7640 end if;
7642 -- Case of package that does not need a body
7644 else
7645 -- If the instantiation of the declaration is a library unit,
7646 -- rewrite the original package instantiation as a package
7647 -- declaration in the compilation unit node.
7649 if Nkind (Parent (Inst_Node)) = N_Compilation_Unit then
7650 Set_Parent_Spec (Act_Decl, Parent_Spec (Inst_Node));
7651 Rewrite (Inst_Node, Act_Decl);
7653 -- Generate elaboration entity, in case spec has elaboration
7654 -- code. This cannot be done when the instance is analyzed,
7655 -- because it is not known yet whether the body exists.
7657 Set_Elaboration_Entity_Required (Act_Decl_Id, False);
7658 Build_Elaboration_Entity (Parent (Inst_Node), Act_Decl_Id);
7660 -- If the instantiation is not a library unit, then append the
7661 -- declaration to the list of implicitly generated entities.
7662 -- unless it is already a list member which means that it was
7663 -- already processed
7665 elsif not Is_List_Member (Act_Decl) then
7666 Mark_Rewrite_Insertion (Act_Decl);
7667 Insert_Before (Inst_Node, Act_Decl);
7668 end if;
7669 end if;
7671 Expander_Mode_Restore;
7672 end Instantiate_Package_Body;
7674 ---------------------------------
7675 -- Instantiate_Subprogram_Body --
7676 ---------------------------------
7678 procedure Instantiate_Subprogram_Body
7679 (Body_Info : Pending_Body_Info)
7681 Act_Decl : constant Node_Id := Body_Info.Act_Decl;
7682 Inst_Node : constant Node_Id := Body_Info.Inst_Node;
7683 Loc : constant Source_Ptr := Sloc (Inst_Node);
7684 Gen_Id : constant Node_Id := Name (Inst_Node);
7685 Gen_Unit : constant Entity_Id := Get_Generic_Entity (Inst_Node);
7686 Gen_Decl : constant Node_Id := Unit_Declaration_Node (Gen_Unit);
7687 Anon_Id : constant Entity_Id :=
7688 Defining_Unit_Name (Specification (Act_Decl));
7689 Pack_Id : constant Entity_Id :=
7690 Defining_Unit_Name (Parent (Act_Decl));
7691 Decls : List_Id;
7692 Gen_Body : Node_Id;
7693 Gen_Body_Id : Node_Id;
7694 Act_Body : Node_Id;
7695 Act_Body_Id : Entity_Id;
7696 Pack_Body : Node_Id;
7697 Prev_Formal : Entity_Id;
7698 Ret_Expr : Node_Id;
7699 Unit_Renaming : Node_Id;
7701 Parent_Installed : Boolean := False;
7702 Save_Style_Check : constant Boolean := Style_Check;
7704 begin
7705 Gen_Body_Id := Corresponding_Body (Gen_Decl);
7707 Expander_Mode_Save_And_Set (Body_Info.Expander_Status);
7709 if No (Gen_Body_Id) then
7710 Load_Parent_Of_Generic (Inst_Node, Specification (Gen_Decl));
7711 Gen_Body_Id := Corresponding_Body (Gen_Decl);
7712 end if;
7714 Instantiation_Node := Inst_Node;
7716 if Present (Gen_Body_Id) then
7717 Gen_Body := Unit_Declaration_Node (Gen_Body_Id);
7719 if Nkind (Gen_Body) = N_Subprogram_Body_Stub then
7721 -- Either body is not present, or context is non-expanding, as
7722 -- when compiling a subunit. Mark the instance as completed, and
7723 -- diagnose a missing body when needed.
7725 if Expander_Active
7726 and then Operating_Mode = Generate_Code
7727 then
7728 Error_Msg_N
7729 ("missing proper body for instantiation", Gen_Body);
7730 end if;
7732 Set_Has_Completion (Anon_Id);
7733 return;
7734 end if;
7736 Save_Env (Gen_Unit, Anon_Id);
7737 Style_Check := False;
7738 Current_Sem_Unit := Body_Info.Current_Sem_Unit;
7739 Create_Instantiation_Source
7740 (Inst_Node,
7741 Gen_Body_Id,
7742 False,
7743 S_Adjustment);
7745 Act_Body :=
7746 Copy_Generic_Node
7747 (Original_Node (Gen_Body), Empty, Instantiating => True);
7748 Act_Body_Id := Defining_Entity (Act_Body);
7749 Set_Chars (Act_Body_Id, Chars (Anon_Id));
7750 Set_Sloc (Act_Body_Id, Sloc (Defining_Entity (Inst_Node)));
7751 Set_Corresponding_Spec (Act_Body, Anon_Id);
7752 Set_Has_Completion (Anon_Id);
7753 Check_Generic_Actuals (Pack_Id, False);
7755 -- If it is a child unit, make the parent instance (which is an
7756 -- instance of the parent of the generic) visible. The parent
7757 -- instance is the prefix of the name of the generic unit.
7759 if Ekind (Scope (Gen_Unit)) = E_Generic_Package
7760 and then Nkind (Gen_Id) = N_Expanded_Name
7761 then
7762 Install_Parent (Entity (Prefix (Gen_Id)), In_Body => True);
7763 Parent_Installed := True;
7765 elsif Is_Child_Unit (Gen_Unit) then
7766 Install_Parent (Scope (Gen_Unit), In_Body => True);
7767 Parent_Installed := True;
7768 end if;
7770 -- Inside its body, a reference to the generic unit is a reference
7771 -- to the instance. The corresponding renaming is the first
7772 -- declaration in the body.
7774 Unit_Renaming :=
7775 Make_Subprogram_Renaming_Declaration (Loc,
7776 Specification =>
7777 Copy_Generic_Node (
7778 Specification (Original_Node (Gen_Body)),
7779 Empty,
7780 Instantiating => True),
7781 Name => New_Occurrence_Of (Anon_Id, Loc));
7783 -- If there is a formal subprogram with the same name as the
7784 -- unit itself, do not add this renaming declaration. This is
7785 -- a temporary fix for one ACVC test. ???
7787 Prev_Formal := First_Entity (Pack_Id);
7788 while Present (Prev_Formal) loop
7789 if Chars (Prev_Formal) = Chars (Gen_Unit)
7790 and then Is_Overloadable (Prev_Formal)
7791 then
7792 exit;
7793 end if;
7795 Next_Entity (Prev_Formal);
7796 end loop;
7798 if Present (Prev_Formal) then
7799 Decls := New_List (Act_Body);
7800 else
7801 Decls := New_List (Unit_Renaming, Act_Body);
7802 end if;
7804 -- The subprogram body is placed in the body of a dummy package
7805 -- body, whose spec contains the subprogram declaration as well
7806 -- as the renaming declarations for the generic parameters.
7808 Pack_Body := Make_Package_Body (Loc,
7809 Defining_Unit_Name => New_Copy (Pack_Id),
7810 Declarations => Decls);
7812 Set_Corresponding_Spec (Pack_Body, Pack_Id);
7814 -- If the instantiation is a library unit, then build resulting
7815 -- compilation unit nodes for the instance. The declaration of
7816 -- the enclosing package is the grandparent of the subprogram
7817 -- declaration. First replace the instantiation node as the unit
7818 -- of the corresponding compilation.
7820 if Nkind (Parent (Inst_Node)) = N_Compilation_Unit then
7821 if Parent (Inst_Node) = Cunit (Main_Unit) then
7822 Set_Unit (Parent (Inst_Node), Inst_Node);
7823 Build_Instance_Compilation_Unit_Nodes
7824 (Inst_Node, Pack_Body, Parent (Parent (Act_Decl)));
7825 Analyze (Inst_Node);
7826 else
7827 Set_Parent (Pack_Body, Parent (Inst_Node));
7828 Analyze (Pack_Body);
7829 end if;
7831 else
7832 Insert_Before (Inst_Node, Pack_Body);
7833 Mark_Rewrite_Insertion (Pack_Body);
7834 Analyze (Pack_Body);
7836 if Expander_Active then
7837 Freeze_Subprogram_Body (Inst_Node, Gen_Body, Pack_Id);
7838 end if;
7839 end if;
7841 if not Generic_Separately_Compiled (Gen_Unit) then
7842 Inherit_Context (Gen_Body, Inst_Node);
7843 end if;
7845 Restore_Private_Views (Pack_Id, False);
7847 if Parent_Installed then
7848 Remove_Parent (In_Body => True);
7849 end if;
7851 Restore_Env;
7852 Style_Check := Save_Style_Check;
7854 -- Body not found. Error was emitted already. If there were no
7855 -- previous errors, this may be an instance whose scope is a premature
7856 -- instance. In that case we must insure that the (legal) program does
7857 -- raise program error if executed. We generate a subprogram body for
7858 -- this purpose. See DEC ac30vso.
7860 elsif Serious_Errors_Detected = 0
7861 and then Nkind (Parent (Inst_Node)) /= N_Compilation_Unit
7862 then
7863 if Ekind (Anon_Id) = E_Procedure then
7864 Act_Body :=
7865 Make_Subprogram_Body (Loc,
7866 Specification =>
7867 Make_Procedure_Specification (Loc,
7868 Defining_Unit_Name =>
7869 Make_Defining_Identifier (Loc, Chars (Anon_Id)),
7870 Parameter_Specifications =>
7871 New_Copy_List
7872 (Parameter_Specifications (Parent (Anon_Id)))),
7874 Declarations => Empty_List,
7875 Handled_Statement_Sequence =>
7876 Make_Handled_Sequence_Of_Statements (Loc,
7877 Statements =>
7878 New_List (
7879 Make_Raise_Program_Error (Loc,
7880 Reason =>
7881 PE_Access_Before_Elaboration))));
7883 else
7884 Ret_Expr :=
7885 Make_Raise_Program_Error (Loc,
7886 Reason => PE_Access_Before_Elaboration);
7888 Set_Etype (Ret_Expr, (Etype (Anon_Id)));
7889 Set_Analyzed (Ret_Expr);
7891 Act_Body :=
7892 Make_Subprogram_Body (Loc,
7893 Specification =>
7894 Make_Function_Specification (Loc,
7895 Defining_Unit_Name =>
7896 Make_Defining_Identifier (Loc, Chars (Anon_Id)),
7897 Parameter_Specifications =>
7898 New_Copy_List
7899 (Parameter_Specifications (Parent (Anon_Id))),
7900 Result_Definition =>
7901 New_Occurrence_Of (Etype (Anon_Id), Loc)),
7903 Declarations => Empty_List,
7904 Handled_Statement_Sequence =>
7905 Make_Handled_Sequence_Of_Statements (Loc,
7906 Statements =>
7907 New_List (Make_Return_Statement (Loc, Ret_Expr))));
7908 end if;
7910 Pack_Body := Make_Package_Body (Loc,
7911 Defining_Unit_Name => New_Copy (Pack_Id),
7912 Declarations => New_List (Act_Body));
7914 Insert_After (Inst_Node, Pack_Body);
7915 Set_Corresponding_Spec (Pack_Body, Pack_Id);
7916 Analyze (Pack_Body);
7917 end if;
7919 Expander_Mode_Restore;
7920 end Instantiate_Subprogram_Body;
7922 ----------------------
7923 -- Instantiate_Type --
7924 ----------------------
7926 function Instantiate_Type
7927 (Formal : Node_Id;
7928 Actual : Node_Id;
7929 Analyzed_Formal : Node_Id;
7930 Actual_Decls : List_Id) return Node_Id
7932 Loc : constant Source_Ptr := Sloc (Actual);
7933 Gen_T : constant Entity_Id := Defining_Identifier (Formal);
7934 A_Gen_T : constant Entity_Id := Defining_Identifier (Analyzed_Formal);
7935 Ancestor : Entity_Id := Empty;
7936 Def : constant Node_Id := Formal_Type_Definition (Formal);
7937 Act_T : Entity_Id;
7938 Decl_Node : Node_Id;
7940 procedure Validate_Array_Type_Instance;
7941 procedure Validate_Access_Subprogram_Instance;
7942 procedure Validate_Access_Type_Instance;
7943 procedure Validate_Derived_Type_Instance;
7944 procedure Validate_Private_Type_Instance;
7945 -- These procedures perform validation tests for the named case
7947 function Subtypes_Match (Gen_T, Act_T : Entity_Id) return Boolean;
7948 -- Check that base types are the same and that the subtypes match
7949 -- statically. Used in several of the above.
7951 --------------------
7952 -- Subtypes_Match --
7953 --------------------
7955 function Subtypes_Match (Gen_T, Act_T : Entity_Id) return Boolean is
7956 T : constant Entity_Id := Get_Instance_Of (Gen_T);
7958 begin
7959 return (Base_Type (T) = Base_Type (Act_T)
7960 -- why is the and then commented out here???
7961 -- and then Is_Constrained (T) = Is_Constrained (Act_T)
7962 and then Subtypes_Statically_Match (T, Act_T))
7964 or else (Is_Class_Wide_Type (Gen_T)
7965 and then Is_Class_Wide_Type (Act_T)
7966 and then
7967 Subtypes_Match (
7968 Get_Instance_Of (Root_Type (Gen_T)),
7969 Root_Type (Act_T)));
7970 end Subtypes_Match;
7972 -----------------------------------------
7973 -- Validate_Access_Subprogram_Instance --
7974 -----------------------------------------
7976 procedure Validate_Access_Subprogram_Instance is
7977 begin
7978 if not Is_Access_Type (Act_T)
7979 or else Ekind (Designated_Type (Act_T)) /= E_Subprogram_Type
7980 then
7981 Error_Msg_NE
7982 ("expect access type in instantiation of &", Actual, Gen_T);
7983 Abandon_Instantiation (Actual);
7984 end if;
7986 Check_Mode_Conformant
7987 (Designated_Type (Act_T),
7988 Designated_Type (A_Gen_T),
7989 Actual,
7990 Get_Inst => True);
7992 if Ekind (Base_Type (Act_T)) = E_Access_Protected_Subprogram_Type then
7993 if Ekind (A_Gen_T) = E_Access_Subprogram_Type then
7994 Error_Msg_NE
7995 ("protected access type not allowed for formal &",
7996 Actual, Gen_T);
7997 end if;
7999 elsif Ekind (A_Gen_T) = E_Access_Protected_Subprogram_Type then
8000 Error_Msg_NE
8001 ("expect protected access type for formal &",
8002 Actual, Gen_T);
8003 end if;
8004 end Validate_Access_Subprogram_Instance;
8006 -----------------------------------
8007 -- Validate_Access_Type_Instance --
8008 -----------------------------------
8010 procedure Validate_Access_Type_Instance is
8011 Desig_Type : constant Entity_Id :=
8012 Find_Actual_Type
8013 (Designated_Type (A_Gen_T), Scope (A_Gen_T));
8015 begin
8016 if not Is_Access_Type (Act_T) then
8017 Error_Msg_NE
8018 ("expect access type in instantiation of &", Actual, Gen_T);
8019 Abandon_Instantiation (Actual);
8020 end if;
8022 if Is_Access_Constant (A_Gen_T) then
8023 if not Is_Access_Constant (Act_T) then
8024 Error_Msg_N
8025 ("actual type must be access-to-constant type", Actual);
8026 Abandon_Instantiation (Actual);
8027 end if;
8028 else
8029 if Is_Access_Constant (Act_T) then
8030 Error_Msg_N
8031 ("actual type must be access-to-variable type", Actual);
8032 Abandon_Instantiation (Actual);
8034 elsif Ekind (A_Gen_T) = E_General_Access_Type
8035 and then Ekind (Base_Type (Act_T)) /= E_General_Access_Type
8036 then
8037 Error_Msg_N ("actual must be general access type!", Actual);
8038 Error_Msg_NE ("add ALL to }!", Actual, Act_T);
8039 Abandon_Instantiation (Actual);
8040 end if;
8041 end if;
8043 -- The designated subtypes, that is to say the subtypes introduced
8044 -- by an access type declaration (and not by a subtype declaration)
8045 -- must match.
8047 if not Subtypes_Match
8048 (Desig_Type, Designated_Type (Base_Type (Act_T)))
8049 then
8050 Error_Msg_NE
8051 ("designated type of actual does not match that of formal &",
8052 Actual, Gen_T);
8053 Abandon_Instantiation (Actual);
8055 elsif Is_Access_Type (Designated_Type (Act_T))
8056 and then Is_Constrained (Designated_Type (Designated_Type (Act_T)))
8058 Is_Constrained (Designated_Type (Desig_Type))
8059 then
8060 Error_Msg_NE
8061 ("designated type of actual does not match that of formal &",
8062 Actual, Gen_T);
8063 Abandon_Instantiation (Actual);
8064 end if;
8065 end Validate_Access_Type_Instance;
8067 ----------------------------------
8068 -- Validate_Array_Type_Instance --
8069 ----------------------------------
8071 procedure Validate_Array_Type_Instance is
8072 I1 : Node_Id;
8073 I2 : Node_Id;
8074 T2 : Entity_Id;
8076 function Formal_Dimensions return Int;
8077 -- Count number of dimensions in array type formal
8079 -----------------------
8080 -- Formal_Dimensions --
8081 -----------------------
8083 function Formal_Dimensions return Int is
8084 Num : Int := 0;
8085 Index : Node_Id;
8087 begin
8088 if Nkind (Def) = N_Constrained_Array_Definition then
8089 Index := First (Discrete_Subtype_Definitions (Def));
8090 else
8091 Index := First (Subtype_Marks (Def));
8092 end if;
8094 while Present (Index) loop
8095 Num := Num + 1;
8096 Next_Index (Index);
8097 end loop;
8099 return Num;
8100 end Formal_Dimensions;
8102 -- Start of processing for Validate_Array_Type_Instance
8104 begin
8105 if not Is_Array_Type (Act_T) then
8106 Error_Msg_NE
8107 ("expect array type in instantiation of &", Actual, Gen_T);
8108 Abandon_Instantiation (Actual);
8110 elsif Nkind (Def) = N_Constrained_Array_Definition then
8111 if not (Is_Constrained (Act_T)) then
8112 Error_Msg_NE
8113 ("expect constrained array in instantiation of &",
8114 Actual, Gen_T);
8115 Abandon_Instantiation (Actual);
8116 end if;
8118 else
8119 if Is_Constrained (Act_T) then
8120 Error_Msg_NE
8121 ("expect unconstrained array in instantiation of &",
8122 Actual, Gen_T);
8123 Abandon_Instantiation (Actual);
8124 end if;
8125 end if;
8127 if Formal_Dimensions /= Number_Dimensions (Act_T) then
8128 Error_Msg_NE
8129 ("dimensions of actual do not match formal &", Actual, Gen_T);
8130 Abandon_Instantiation (Actual);
8131 end if;
8133 I1 := First_Index (A_Gen_T);
8134 I2 := First_Index (Act_T);
8135 for J in 1 .. Formal_Dimensions loop
8137 -- If the indices of the actual were given by a subtype_mark,
8138 -- the index was transformed into a range attribute. Retrieve
8139 -- the original type mark for checking.
8141 if Is_Entity_Name (Original_Node (I2)) then
8142 T2 := Entity (Original_Node (I2));
8143 else
8144 T2 := Etype (I2);
8145 end if;
8147 if not Subtypes_Match
8148 (Find_Actual_Type (Etype (I1), Scope (A_Gen_T)), T2)
8149 then
8150 Error_Msg_NE
8151 ("index types of actual do not match those of formal &",
8152 Actual, Gen_T);
8153 Abandon_Instantiation (Actual);
8154 end if;
8156 Next_Index (I1);
8157 Next_Index (I2);
8158 end loop;
8160 if not Subtypes_Match (
8161 Find_Actual_Type (Component_Type (A_Gen_T), Scope (A_Gen_T)),
8162 Component_Type (Act_T))
8163 then
8164 Error_Msg_NE
8165 ("component subtype of actual does not match that of formal &",
8166 Actual, Gen_T);
8167 Abandon_Instantiation (Actual);
8168 end if;
8170 if Has_Aliased_Components (A_Gen_T)
8171 and then not Has_Aliased_Components (Act_T)
8172 then
8173 Error_Msg_NE
8174 ("actual must have aliased components to match formal type &",
8175 Actual, Gen_T);
8176 end if;
8178 end Validate_Array_Type_Instance;
8180 ------------------------------------
8181 -- Validate_Derived_Type_Instance --
8182 ------------------------------------
8184 procedure Validate_Derived_Type_Instance is
8185 Actual_Discr : Entity_Id;
8186 Ancestor_Discr : Entity_Id;
8188 begin
8189 -- If the parent type in the generic declaration is itself
8190 -- a previous formal type, then it is local to the generic
8191 -- and absent from the analyzed generic definition. In that
8192 -- case the ancestor is the instance of the formal (which must
8193 -- have been instantiated previously), unless the ancestor is
8194 -- itself a formal derived type. In this latter case (which is the
8195 -- subject of Corrigendum 8652/0038 (AI-202) the ancestor of the
8196 -- formals is the ancestor of its parent. Otherwise, the analyzed
8197 -- generic carries the parent type. If the parent type is defined
8198 -- in a previous formal package, then the scope of that formal
8199 -- package is that of the generic type itself, and it has already
8200 -- been mapped into the corresponding type in the actual package.
8202 -- Common case: parent type defined outside of the generic
8204 if Is_Entity_Name (Subtype_Mark (Def))
8205 and then Present (Entity (Subtype_Mark (Def)))
8206 then
8207 Ancestor := Get_Instance_Of (Entity (Subtype_Mark (Def)));
8209 -- Check whether parent is defined in a previous formal package
8211 elsif
8212 Scope (Scope (Base_Type (Etype (A_Gen_T)))) = Scope (A_Gen_T)
8213 then
8214 Ancestor :=
8215 Get_Instance_Of (Base_Type (Etype (A_Gen_T)));
8217 -- The type may be a local derivation, or a type extension of
8218 -- a previous formal, or of a formal of a parent package.
8220 elsif Is_Derived_Type (Get_Instance_Of (A_Gen_T))
8221 or else
8222 Ekind (Get_Instance_Of (A_Gen_T)) = E_Record_Type_With_Private
8223 then
8224 -- Check whether the parent is another derived formal type
8225 -- in the same generic unit.
8227 if Etype (A_Gen_T) /= A_Gen_T
8228 and then Is_Generic_Type (Etype (A_Gen_T))
8229 and then Scope (Etype (A_Gen_T)) = Scope (A_Gen_T)
8230 and then Etype (Etype (A_Gen_T)) /= Etype (A_Gen_T)
8231 then
8232 -- Locate ancestor of parent from the subtype declaration
8233 -- created for the actual.
8235 declare
8236 Decl : Node_Id;
8238 begin
8239 Decl := First (Actual_Decls);
8240 while Present (Decl) loop
8241 if Nkind (Decl) = N_Subtype_Declaration
8242 and then Chars (Defining_Identifier (Decl)) =
8243 Chars (Etype (A_Gen_T))
8244 then
8245 Ancestor := Generic_Parent_Type (Decl);
8246 exit;
8247 else
8248 Next (Decl);
8249 end if;
8250 end loop;
8251 end;
8253 pragma Assert (Present (Ancestor));
8255 else
8256 Ancestor :=
8257 Get_Instance_Of (Base_Type (Get_Instance_Of (A_Gen_T)));
8258 end if;
8260 else
8261 Ancestor := Get_Instance_Of (Etype (Base_Type (A_Gen_T)));
8262 end if;
8264 -- Ada 2005 (AI-251)
8266 if Ada_Version >= Ada_05
8267 and then Is_Interface (Ancestor)
8268 then
8269 if not Interface_Present_In_Ancestor (Act_T, Ancestor) then
8270 Error_Msg_NE
8271 ("(Ada 2005) expected type implementing & in instantiation",
8272 Actual, Ancestor);
8273 end if;
8275 elsif not Is_Ancestor (Base_Type (Ancestor), Act_T) then
8276 Error_Msg_NE
8277 ("expect type derived from & in instantiation",
8278 Actual, First_Subtype (Ancestor));
8279 Abandon_Instantiation (Actual);
8280 end if;
8282 -- Perform atomic/volatile checks (RM C.6(12))
8284 if Is_Atomic (Act_T) and then not Is_Atomic (Ancestor) then
8285 Error_Msg_N
8286 ("cannot have atomic actual type for non-atomic formal type",
8287 Actual);
8289 elsif Is_Volatile (Act_T)
8290 and then not Is_Volatile (Ancestor)
8291 and then Is_By_Reference_Type (Ancestor)
8292 then
8293 Error_Msg_N
8294 ("cannot have volatile actual type for non-volatile formal type",
8295 Actual);
8296 end if;
8298 -- It should not be necessary to check for unknown discriminants
8299 -- on Formal, but for some reason Has_Unknown_Discriminants is
8300 -- false for A_Gen_T, so Is_Indefinite_Subtype incorrectly
8301 -- returns False. This needs fixing. ???
8303 if not Is_Indefinite_Subtype (A_Gen_T)
8304 and then not Unknown_Discriminants_Present (Formal)
8305 and then Is_Indefinite_Subtype (Act_T)
8306 then
8307 Error_Msg_N
8308 ("actual subtype must be constrained", Actual);
8309 Abandon_Instantiation (Actual);
8310 end if;
8312 if not Unknown_Discriminants_Present (Formal) then
8313 if Is_Constrained (Ancestor) then
8314 if not Is_Constrained (Act_T) then
8315 Error_Msg_N
8316 ("actual subtype must be constrained", Actual);
8317 Abandon_Instantiation (Actual);
8318 end if;
8320 -- Ancestor is unconstrained
8322 elsif Is_Constrained (Act_T) then
8323 if Ekind (Ancestor) = E_Access_Type
8324 or else Is_Composite_Type (Ancestor)
8325 then
8326 Error_Msg_N
8327 ("actual subtype must be unconstrained", Actual);
8328 Abandon_Instantiation (Actual);
8329 end if;
8331 -- A class-wide type is only allowed if the formal has
8332 -- unknown discriminants.
8334 elsif Is_Class_Wide_Type (Act_T)
8335 and then not Has_Unknown_Discriminants (Ancestor)
8336 then
8337 Error_Msg_NE
8338 ("actual for & cannot be a class-wide type", Actual, Gen_T);
8339 Abandon_Instantiation (Actual);
8341 -- Otherwise, the formal and actual shall have the same
8342 -- number of discriminants and each discriminant of the
8343 -- actual must correspond to a discriminant of the formal.
8345 elsif Has_Discriminants (Act_T)
8346 and then not Has_Unknown_Discriminants (Act_T)
8347 and then Has_Discriminants (Ancestor)
8348 then
8349 Actual_Discr := First_Discriminant (Act_T);
8350 Ancestor_Discr := First_Discriminant (Ancestor);
8351 while Present (Actual_Discr)
8352 and then Present (Ancestor_Discr)
8353 loop
8354 if Base_Type (Act_T) /= Base_Type (Ancestor) and then
8355 not Present (Corresponding_Discriminant (Actual_Discr))
8356 then
8357 Error_Msg_NE
8358 ("discriminant & does not correspond " &
8359 "to ancestor discriminant", Actual, Actual_Discr);
8360 Abandon_Instantiation (Actual);
8361 end if;
8363 Next_Discriminant (Actual_Discr);
8364 Next_Discriminant (Ancestor_Discr);
8365 end loop;
8367 if Present (Actual_Discr) or else Present (Ancestor_Discr) then
8368 Error_Msg_NE
8369 ("actual for & must have same number of discriminants",
8370 Actual, Gen_T);
8371 Abandon_Instantiation (Actual);
8372 end if;
8374 -- This case should be caught by the earlier check for
8375 -- for constrainedness, but the check here is added for
8376 -- completeness.
8378 elsif Has_Discriminants (Act_T)
8379 and then not Has_Unknown_Discriminants (Act_T)
8380 then
8381 Error_Msg_NE
8382 ("actual for & must not have discriminants", Actual, Gen_T);
8383 Abandon_Instantiation (Actual);
8385 elsif Has_Discriminants (Ancestor) then
8386 Error_Msg_NE
8387 ("actual for & must have known discriminants", Actual, Gen_T);
8388 Abandon_Instantiation (Actual);
8389 end if;
8391 if not Subtypes_Statically_Compatible (Act_T, Ancestor) then
8392 Error_Msg_N
8393 ("constraint on actual is incompatible with formal", Actual);
8394 Abandon_Instantiation (Actual);
8395 end if;
8396 end if;
8397 end Validate_Derived_Type_Instance;
8399 ------------------------------------
8400 -- Validate_Private_Type_Instance --
8401 ------------------------------------
8403 procedure Validate_Private_Type_Instance is
8404 Formal_Discr : Entity_Id;
8405 Actual_Discr : Entity_Id;
8406 Formal_Subt : Entity_Id;
8408 begin
8409 if Is_Limited_Type (Act_T)
8410 and then not Is_Limited_Type (A_Gen_T)
8411 then
8412 Error_Msg_NE
8413 ("actual for non-limited & cannot be a limited type", Actual,
8414 Gen_T);
8415 Explain_Limited_Type (Act_T, Actual);
8416 Abandon_Instantiation (Actual);
8418 elsif Is_Indefinite_Subtype (Act_T)
8419 and then not Is_Indefinite_Subtype (A_Gen_T)
8420 and then Ada_Version >= Ada_95
8421 then
8422 Error_Msg_NE
8423 ("actual for & must be a definite subtype", Actual, Gen_T);
8425 elsif not Is_Tagged_Type (Act_T)
8426 and then Is_Tagged_Type (A_Gen_T)
8427 then
8428 Error_Msg_NE
8429 ("actual for & must be a tagged type", Actual, Gen_T);
8431 elsif Has_Discriminants (A_Gen_T) then
8432 if not Has_Discriminants (Act_T) then
8433 Error_Msg_NE
8434 ("actual for & must have discriminants", Actual, Gen_T);
8435 Abandon_Instantiation (Actual);
8437 elsif Is_Constrained (Act_T) then
8438 Error_Msg_NE
8439 ("actual for & must be unconstrained", Actual, Gen_T);
8440 Abandon_Instantiation (Actual);
8442 else
8443 Formal_Discr := First_Discriminant (A_Gen_T);
8444 Actual_Discr := First_Discriminant (Act_T);
8445 while Formal_Discr /= Empty loop
8446 if Actual_Discr = Empty then
8447 Error_Msg_NE
8448 ("discriminants on actual do not match formal",
8449 Actual, Gen_T);
8450 Abandon_Instantiation (Actual);
8451 end if;
8453 Formal_Subt := Get_Instance_Of (Etype (Formal_Discr));
8455 -- Access discriminants match if designated types do
8457 if Ekind (Base_Type (Formal_Subt)) = E_Anonymous_Access_Type
8458 and then (Ekind (Base_Type (Etype (Actual_Discr)))) =
8459 E_Anonymous_Access_Type
8460 and then
8461 Get_Instance_Of
8462 (Designated_Type (Base_Type (Formal_Subt))) =
8463 Designated_Type (Base_Type (Etype (Actual_Discr)))
8464 then
8465 null;
8467 elsif Base_Type (Formal_Subt) /=
8468 Base_Type (Etype (Actual_Discr))
8469 then
8470 Error_Msg_NE
8471 ("types of actual discriminants must match formal",
8472 Actual, Gen_T);
8473 Abandon_Instantiation (Actual);
8475 elsif not Subtypes_Statically_Match
8476 (Formal_Subt, Etype (Actual_Discr))
8477 and then Ada_Version >= Ada_95
8478 then
8479 Error_Msg_NE
8480 ("subtypes of actual discriminants must match formal",
8481 Actual, Gen_T);
8482 Abandon_Instantiation (Actual);
8483 end if;
8485 Next_Discriminant (Formal_Discr);
8486 Next_Discriminant (Actual_Discr);
8487 end loop;
8489 if Actual_Discr /= Empty then
8490 Error_Msg_NE
8491 ("discriminants on actual do not match formal",
8492 Actual, Gen_T);
8493 Abandon_Instantiation (Actual);
8494 end if;
8495 end if;
8497 end if;
8499 Ancestor := Gen_T;
8500 end Validate_Private_Type_Instance;
8502 -- Start of processing for Instantiate_Type
8504 begin
8505 if Get_Instance_Of (A_Gen_T) /= A_Gen_T then
8506 Error_Msg_N ("duplicate instantiation of generic type", Actual);
8507 return Error;
8509 elsif not Is_Entity_Name (Actual)
8510 or else not Is_Type (Entity (Actual))
8511 then
8512 Error_Msg_NE
8513 ("expect valid subtype mark to instantiate &", Actual, Gen_T);
8514 Abandon_Instantiation (Actual);
8516 else
8517 Act_T := Entity (Actual);
8519 -- Ada 2005 (AI-216): An Unchecked_Union subtype shall only be passed
8520 -- as a generic actual parameter if the corresponding formal type
8521 -- does not have a known_discriminant_part, or is a formal derived
8522 -- type that is an Unchecked_Union type.
8524 if Is_Unchecked_Union (Base_Type (Act_T)) then
8525 if not Has_Discriminants (A_Gen_T)
8526 or else
8527 (Is_Derived_Type (A_Gen_T)
8528 and then
8529 Is_Unchecked_Union (A_Gen_T))
8530 then
8531 null;
8532 else
8533 Error_Msg_N ("Unchecked_Union cannot be the actual for a" &
8534 " discriminated formal type", Act_T);
8536 end if;
8537 end if;
8539 -- Deal with fixed/floating restrictions
8541 if Is_Floating_Point_Type (Act_T) then
8542 Check_Restriction (No_Floating_Point, Actual);
8543 elsif Is_Fixed_Point_Type (Act_T) then
8544 Check_Restriction (No_Fixed_Point, Actual);
8545 end if;
8547 -- Deal with error of using incomplete type as generic actual
8549 if Ekind (Act_T) = E_Incomplete_Type then
8550 if No (Underlying_Type (Act_T)) then
8551 Error_Msg_N ("premature use of incomplete type", Actual);
8552 Abandon_Instantiation (Actual);
8553 else
8554 Act_T := Full_View (Act_T);
8555 Set_Entity (Actual, Act_T);
8557 if Has_Private_Component (Act_T) then
8558 Error_Msg_N
8559 ("premature use of type with private component", Actual);
8560 end if;
8561 end if;
8563 -- Deal with error of premature use of private type as generic actual
8565 elsif Is_Private_Type (Act_T)
8566 and then Is_Private_Type (Base_Type (Act_T))
8567 and then not Is_Generic_Type (Act_T)
8568 and then not Is_Derived_Type (Act_T)
8569 and then No (Full_View (Root_Type (Act_T)))
8570 then
8571 Error_Msg_N ("premature use of private type", Actual);
8573 elsif Has_Private_Component (Act_T) then
8574 Error_Msg_N
8575 ("premature use of type with private component", Actual);
8576 end if;
8578 Set_Instance_Of (A_Gen_T, Act_T);
8580 -- If the type is generic, the class-wide type may also be used
8582 if Is_Tagged_Type (A_Gen_T)
8583 and then Is_Tagged_Type (Act_T)
8584 and then not Is_Class_Wide_Type (A_Gen_T)
8585 then
8586 Set_Instance_Of (Class_Wide_Type (A_Gen_T),
8587 Class_Wide_Type (Act_T));
8588 end if;
8590 if not Is_Abstract (A_Gen_T)
8591 and then Is_Abstract (Act_T)
8592 then
8593 Error_Msg_N
8594 ("actual of non-abstract formal cannot be abstract", Actual);
8595 end if;
8597 if Is_Scalar_Type (Gen_T) then
8598 Set_Instance_Of (Etype (A_Gen_T), Etype (Act_T));
8599 end if;
8600 end if;
8602 case Nkind (Def) is
8603 when N_Formal_Private_Type_Definition =>
8604 Validate_Private_Type_Instance;
8606 when N_Formal_Derived_Type_Definition =>
8607 Validate_Derived_Type_Instance;
8609 when N_Formal_Discrete_Type_Definition =>
8610 if not Is_Discrete_Type (Act_T) then
8611 Error_Msg_NE
8612 ("expect discrete type in instantiation of&", Actual, Gen_T);
8613 Abandon_Instantiation (Actual);
8614 end if;
8616 when N_Formal_Signed_Integer_Type_Definition =>
8617 if not Is_Signed_Integer_Type (Act_T) then
8618 Error_Msg_NE
8619 ("expect signed integer type in instantiation of&",
8620 Actual, Gen_T);
8621 Abandon_Instantiation (Actual);
8622 end if;
8624 when N_Formal_Modular_Type_Definition =>
8625 if not Is_Modular_Integer_Type (Act_T) then
8626 Error_Msg_NE
8627 ("expect modular type in instantiation of &", Actual, Gen_T);
8628 Abandon_Instantiation (Actual);
8629 end if;
8631 when N_Formal_Floating_Point_Definition =>
8632 if not Is_Floating_Point_Type (Act_T) then
8633 Error_Msg_NE
8634 ("expect float type in instantiation of &", Actual, Gen_T);
8635 Abandon_Instantiation (Actual);
8636 end if;
8638 when N_Formal_Ordinary_Fixed_Point_Definition =>
8639 if not Is_Ordinary_Fixed_Point_Type (Act_T) then
8640 Error_Msg_NE
8641 ("expect ordinary fixed point type in instantiation of &",
8642 Actual, Gen_T);
8643 Abandon_Instantiation (Actual);
8644 end if;
8646 when N_Formal_Decimal_Fixed_Point_Definition =>
8647 if not Is_Decimal_Fixed_Point_Type (Act_T) then
8648 Error_Msg_NE
8649 ("expect decimal type in instantiation of &",
8650 Actual, Gen_T);
8651 Abandon_Instantiation (Actual);
8652 end if;
8654 when N_Array_Type_Definition =>
8655 Validate_Array_Type_Instance;
8657 when N_Access_To_Object_Definition =>
8658 Validate_Access_Type_Instance;
8660 when N_Access_Function_Definition |
8661 N_Access_Procedure_Definition =>
8662 Validate_Access_Subprogram_Instance;
8664 when others =>
8665 raise Program_Error;
8667 end case;
8669 Decl_Node :=
8670 Make_Subtype_Declaration (Loc,
8671 Defining_Identifier => New_Copy (Gen_T),
8672 Subtype_Indication => New_Reference_To (Act_T, Loc));
8674 if Is_Private_Type (Act_T) then
8675 Set_Has_Private_View (Subtype_Indication (Decl_Node));
8677 elsif Is_Access_Type (Act_T)
8678 and then Is_Private_Type (Designated_Type (Act_T))
8679 then
8680 Set_Has_Private_View (Subtype_Indication (Decl_Node));
8681 end if;
8683 -- Flag actual derived types so their elaboration produces the
8684 -- appropriate renamings for the primitive operations of the ancestor.
8685 -- Flag actual for formal private types as well, to determine whether
8686 -- operations in the private part may override inherited operations.
8688 if Nkind (Def) = N_Formal_Derived_Type_Definition
8689 or else Nkind (Def) = N_Formal_Private_Type_Definition
8690 then
8691 Set_Generic_Parent_Type (Decl_Node, Ancestor);
8692 end if;
8694 return Decl_Node;
8695 end Instantiate_Type;
8697 ---------------------
8698 -- Is_In_Main_Unit --
8699 ---------------------
8701 function Is_In_Main_Unit (N : Node_Id) return Boolean is
8702 Unum : constant Unit_Number_Type := Get_Source_Unit (N);
8703 Current_Unit : Node_Id;
8705 begin
8706 if Unum = Main_Unit then
8707 return True;
8709 -- If the current unit is a subunit then it is either the main unit
8710 -- or is being compiled as part of the main unit.
8712 elsif Nkind (N) = N_Compilation_Unit then
8713 return Nkind (Unit (N)) = N_Subunit;
8714 end if;
8716 Current_Unit := Parent (N);
8717 while Present (Current_Unit)
8718 and then Nkind (Current_Unit) /= N_Compilation_Unit
8719 loop
8720 Current_Unit := Parent (Current_Unit);
8721 end loop;
8723 -- The instantiation node is in the main unit, or else the current
8724 -- node (perhaps as the result of nested instantiations) is in the
8725 -- main unit, or in the declaration of the main unit, which in this
8726 -- last case must be a body.
8728 return Unum = Main_Unit
8729 or else Current_Unit = Cunit (Main_Unit)
8730 or else Current_Unit = Library_Unit (Cunit (Main_Unit))
8731 or else (Present (Library_Unit (Current_Unit))
8732 and then Is_In_Main_Unit (Library_Unit (Current_Unit)));
8733 end Is_In_Main_Unit;
8735 ----------------------------
8736 -- Load_Parent_Of_Generic --
8737 ----------------------------
8739 procedure Load_Parent_Of_Generic (N : Node_Id; Spec : Node_Id) is
8740 Comp_Unit : constant Node_Id := Cunit (Get_Source_Unit (Spec));
8741 Save_Style_Check : constant Boolean := Style_Check;
8742 True_Parent : Node_Id;
8743 Inst_Node : Node_Id;
8744 OK : Boolean;
8746 begin
8747 if not In_Same_Source_Unit (N, Spec)
8748 or else Nkind (Unit (Comp_Unit)) = N_Package_Declaration
8749 or else (Nkind (Unit (Comp_Unit)) = N_Package_Body
8750 and then not Is_In_Main_Unit (Spec))
8751 then
8752 -- Find body of parent of spec, and analyze it. A special case
8753 -- arises when the parent is an instantiation, that is to say when
8754 -- we are currently instantiating a nested generic. In that case,
8755 -- there is no separate file for the body of the enclosing instance.
8756 -- Instead, the enclosing body must be instantiated as if it were
8757 -- a pending instantiation, in order to produce the body for the
8758 -- nested generic we require now. Note that in that case the
8759 -- generic may be defined in a package body, the instance defined
8760 -- in the same package body, and the original enclosing body may not
8761 -- be in the main unit.
8763 True_Parent := Parent (Spec);
8764 Inst_Node := Empty;
8766 while Present (True_Parent)
8767 and then Nkind (True_Parent) /= N_Compilation_Unit
8768 loop
8769 if Nkind (True_Parent) = N_Package_Declaration
8770 and then
8771 Nkind (Original_Node (True_Parent)) = N_Package_Instantiation
8772 then
8773 -- Parent is a compilation unit that is an instantiation.
8774 -- Instantiation node has been replaced with package decl.
8776 Inst_Node := Original_Node (True_Parent);
8777 exit;
8779 elsif Nkind (True_Parent) = N_Package_Declaration
8780 and then Present (Generic_Parent (Specification (True_Parent)))
8781 and then Nkind (Parent (True_Parent)) /= N_Compilation_Unit
8782 then
8783 -- Parent is an instantiation within another specification.
8784 -- Declaration for instance has been inserted before original
8785 -- instantiation node. A direct link would be preferable?
8787 Inst_Node := Next (True_Parent);
8789 while Present (Inst_Node)
8790 and then Nkind (Inst_Node) /= N_Package_Instantiation
8791 loop
8792 Next (Inst_Node);
8793 end loop;
8795 -- If the instance appears within a generic, and the generic
8796 -- unit is defined within a formal package of the enclosing
8797 -- generic, there is no generic body available, and none
8798 -- needed. A more precise test should be used ???
8800 if No (Inst_Node) then
8801 return;
8802 end if;
8804 exit;
8805 else
8806 True_Parent := Parent (True_Parent);
8807 end if;
8808 end loop;
8810 -- Case where we are currently instantiating a nested generic
8812 if Present (Inst_Node) then
8813 if Nkind (Parent (True_Parent)) = N_Compilation_Unit then
8815 -- Instantiation node and declaration of instantiated package
8816 -- were exchanged when only the declaration was needed.
8817 -- Restore instantiation node before proceeding with body.
8819 Set_Unit (Parent (True_Parent), Inst_Node);
8820 end if;
8822 -- Now complete instantiation of enclosing body, if it appears
8823 -- in some other unit. If it appears in the current unit, the
8824 -- body will have been instantiated already.
8826 if No (Corresponding_Body (Instance_Spec (Inst_Node))) then
8828 -- We need to determine the expander mode to instantiate
8829 -- the enclosing body. Because the generic body we need
8830 -- may use global entities declared in the enclosing package
8831 -- (including aggregates) it is in general necessary to
8832 -- compile this body with expansion enabled. The exception
8833 -- is if we are within a generic package, in which case
8834 -- the usual generic rule applies.
8836 declare
8837 Exp_Status : Boolean := True;
8838 Scop : Entity_Id;
8840 begin
8841 -- Loop through scopes looking for generic package
8843 Scop := Scope (Defining_Entity (Instance_Spec (Inst_Node)));
8844 while Present (Scop)
8845 and then Scop /= Standard_Standard
8846 loop
8847 if Ekind (Scop) = E_Generic_Package then
8848 Exp_Status := False;
8849 exit;
8850 end if;
8852 Scop := Scope (Scop);
8853 end loop;
8855 Instantiate_Package_Body
8856 (Pending_Body_Info'(
8857 Inst_Node, True_Parent, Exp_Status,
8858 Get_Code_Unit (Sloc (Inst_Node))));
8859 end;
8860 end if;
8862 -- Case where we are not instantiating a nested generic
8864 else
8865 Opt.Style_Check := False;
8866 Expander_Mode_Save_And_Set (True);
8867 Load_Needed_Body (Comp_Unit, OK);
8868 Opt.Style_Check := Save_Style_Check;
8869 Expander_Mode_Restore;
8871 if not OK
8872 and then Unit_Requires_Body (Defining_Entity (Spec))
8873 then
8874 declare
8875 Bname : constant Unit_Name_Type :=
8876 Get_Body_Name (Get_Unit_Name (Unit (Comp_Unit)));
8878 begin
8879 Error_Msg_Unit_1 := Bname;
8880 Error_Msg_N ("this instantiation requires$!", N);
8881 Error_Msg_Name_1 :=
8882 Get_File_Name (Bname, Subunit => False);
8883 Error_Msg_N ("\but file{ was not found!", N);
8884 raise Unrecoverable_Error;
8885 end;
8886 end if;
8887 end if;
8888 end if;
8890 -- If loading the parent of the generic caused an instantiation
8891 -- circularity, we abandon compilation at this point, because
8892 -- otherwise in some cases we get into trouble with infinite
8893 -- recursions after this point.
8895 if Circularity_Detected then
8896 raise Unrecoverable_Error;
8897 end if;
8898 end Load_Parent_Of_Generic;
8900 -----------------------
8901 -- Move_Freeze_Nodes --
8902 -----------------------
8904 procedure Move_Freeze_Nodes
8905 (Out_Of : Entity_Id;
8906 After : Node_Id;
8907 L : List_Id)
8909 Decl : Node_Id;
8910 Next_Decl : Node_Id;
8911 Next_Node : Node_Id := After;
8912 Spec : Node_Id;
8914 function Is_Outer_Type (T : Entity_Id) return Boolean;
8915 -- Check whether entity is declared in a scope external to that
8916 -- of the generic unit.
8918 -------------------
8919 -- Is_Outer_Type --
8920 -------------------
8922 function Is_Outer_Type (T : Entity_Id) return Boolean is
8923 Scop : Entity_Id := Scope (T);
8925 begin
8926 if Scope_Depth (Scop) < Scope_Depth (Out_Of) then
8927 return True;
8929 else
8930 while Scop /= Standard_Standard loop
8932 if Scop = Out_Of then
8933 return False;
8934 else
8935 Scop := Scope (Scop);
8936 end if;
8937 end loop;
8939 return True;
8940 end if;
8941 end Is_Outer_Type;
8943 -- Start of processing for Move_Freeze_Nodes
8945 begin
8946 if No (L) then
8947 return;
8948 end if;
8950 -- First remove the freeze nodes that may appear before all other
8951 -- declarations.
8953 Decl := First (L);
8954 while Present (Decl)
8955 and then Nkind (Decl) = N_Freeze_Entity
8956 and then Is_Outer_Type (Entity (Decl))
8957 loop
8958 Decl := Remove_Head (L);
8959 Insert_After (Next_Node, Decl);
8960 Set_Analyzed (Decl, False);
8961 Next_Node := Decl;
8962 Decl := First (L);
8963 end loop;
8965 -- Next scan the list of declarations and remove each freeze node that
8966 -- appears ahead of the current node.
8968 while Present (Decl) loop
8969 while Present (Next (Decl))
8970 and then Nkind (Next (Decl)) = N_Freeze_Entity
8971 and then Is_Outer_Type (Entity (Next (Decl)))
8972 loop
8973 Next_Decl := Remove_Next (Decl);
8974 Insert_After (Next_Node, Next_Decl);
8975 Set_Analyzed (Next_Decl, False);
8976 Next_Node := Next_Decl;
8977 end loop;
8979 -- If the declaration is a nested package or concurrent type, then
8980 -- recurse. Nested generic packages will have been processed from the
8981 -- inside out.
8983 if Nkind (Decl) = N_Package_Declaration then
8984 Spec := Specification (Decl);
8986 elsif Nkind (Decl) = N_Task_Type_Declaration then
8987 Spec := Task_Definition (Decl);
8989 elsif Nkind (Decl) = N_Protected_Type_Declaration then
8990 Spec := Protected_Definition (Decl);
8992 else
8993 Spec := Empty;
8994 end if;
8996 if Present (Spec) then
8997 Move_Freeze_Nodes (Out_Of, Next_Node,
8998 Visible_Declarations (Spec));
8999 Move_Freeze_Nodes (Out_Of, Next_Node,
9000 Private_Declarations (Spec));
9001 end if;
9003 Next (Decl);
9004 end loop;
9005 end Move_Freeze_Nodes;
9007 ----------------
9008 -- Next_Assoc --
9009 ----------------
9011 function Next_Assoc (E : Assoc_Ptr) return Assoc_Ptr is
9012 begin
9013 return Generic_Renamings.Table (E).Next_In_HTable;
9014 end Next_Assoc;
9016 ------------------------
9017 -- Preanalyze_Actuals --
9018 ------------------------
9020 procedure Pre_Analyze_Actuals (N : Node_Id) is
9021 Assoc : Node_Id;
9022 Act : Node_Id;
9023 Errs : constant Int := Serious_Errors_Detected;
9025 begin
9026 Assoc := First (Generic_Associations (N));
9028 while Present (Assoc) loop
9029 Act := Explicit_Generic_Actual_Parameter (Assoc);
9031 -- Within a nested instantiation, a defaulted actual is an
9032 -- empty association, so nothing to analyze. If the actual for
9033 -- a subprogram is an attribute, analyze prefix only, because
9034 -- actual is not a complete attribute reference.
9036 -- If actual is an allocator, analyze expression only. The full
9037 -- analysis can generate code, and if the instance is a compilation
9038 -- unit we have to wait until the package instance is installed to
9039 -- have a proper place to insert this code.
9041 -- String literals may be operators, but at this point we do not
9042 -- know whether the actual is a formal subprogram or a string.
9044 if No (Act) then
9045 null;
9047 elsif Nkind (Act) = N_Attribute_Reference then
9048 Analyze (Prefix (Act));
9050 elsif Nkind (Act) = N_Explicit_Dereference then
9051 Analyze (Prefix (Act));
9053 elsif Nkind (Act) = N_Allocator then
9054 declare
9055 Expr : constant Node_Id := Expression (Act);
9057 begin
9058 if Nkind (Expr) = N_Subtype_Indication then
9059 Analyze (Subtype_Mark (Expr));
9060 Analyze_List (Constraints (Constraint (Expr)));
9061 else
9062 Analyze (Expr);
9063 end if;
9064 end;
9066 elsif Nkind (Act) /= N_Operator_Symbol then
9067 Analyze (Act);
9068 end if;
9070 if Errs /= Serious_Errors_Detected then
9071 Abandon_Instantiation (Act);
9072 end if;
9074 Next (Assoc);
9075 end loop;
9076 end Pre_Analyze_Actuals;
9078 -------------------
9079 -- Remove_Parent --
9080 -------------------
9082 procedure Remove_Parent (In_Body : Boolean := False) is
9083 S : Entity_Id := Current_Scope;
9084 E : Entity_Id;
9085 P : Entity_Id;
9086 Hidden : Elmt_Id;
9088 begin
9089 -- After child instantiation is complete, remove from scope stack
9090 -- the extra copy of the current scope, and then remove parent
9091 -- instances.
9093 if not In_Body then
9094 Pop_Scope;
9096 while Current_Scope /= S loop
9097 P := Current_Scope;
9098 End_Package_Scope (Current_Scope);
9100 if In_Open_Scopes (P) then
9101 E := First_Entity (P);
9103 while Present (E) loop
9104 Set_Is_Immediately_Visible (E, True);
9105 Next_Entity (E);
9106 end loop;
9108 if Is_Generic_Instance (Current_Scope)
9109 and then P /= Current_Scope
9110 then
9111 -- We are within an instance of some sibling. Retain
9112 -- visibility of parent, for proper subsequent cleanup,
9113 -- and reinstall private declarations as well.
9115 Set_In_Private_Part (P);
9116 Install_Private_Declarations (P);
9117 end if;
9119 -- If the ultimate parent is a compilation unit, reset its
9120 -- visibility to what it was before instantiation.
9122 elsif not In_Open_Scopes (Scope (P))
9123 or else
9124 (not Is_Child_Unit (P) and then not Parent_Unit_Visible)
9125 then
9126 Set_Is_Immediately_Visible (P, False);
9127 end if;
9128 end loop;
9130 -- Reset visibility of entities in the enclosing scope
9132 Set_Is_Hidden_Open_Scope (Current_Scope, False);
9133 Hidden := First_Elmt (Hidden_Entities);
9135 while Present (Hidden) loop
9136 Set_Is_Immediately_Visible (Node (Hidden), True);
9137 Next_Elmt (Hidden);
9138 end loop;
9140 else
9141 -- Each body is analyzed separately, and there is no context
9142 -- that needs preserving from one body instance to the next,
9143 -- so remove all parent scopes that have been installed.
9145 while Present (S) loop
9146 End_Package_Scope (S);
9147 Set_Is_Immediately_Visible (S, False);
9148 S := Current_Scope;
9149 exit when S = Standard_Standard;
9150 end loop;
9151 end if;
9153 end Remove_Parent;
9155 -----------------
9156 -- Restore_Env --
9157 -----------------
9159 procedure Restore_Env is
9160 Saved : Instance_Env renames Instance_Envs.Table (Instance_Envs.Last);
9162 begin
9163 Ada_Version := Saved.Ada_Version;
9164 Ada_Version_Explicit := Saved.Ada_Version_Explicit;
9166 if No (Current_Instantiated_Parent.Act_Id) then
9168 -- Restore environment after subprogram inlining
9170 Restore_Private_Views (Empty);
9171 end if;
9173 Current_Instantiated_Parent := Saved.Instantiated_Parent;
9174 Exchanged_Views := Saved.Exchanged_Views;
9175 Hidden_Entities := Saved.Hidden_Entities;
9176 Current_Sem_Unit := Saved.Current_Sem_Unit;
9177 Parent_Unit_Visible := Saved.Parent_Unit_Visible;
9179 Instance_Envs.Decrement_Last;
9180 end Restore_Env;
9182 ---------------------------
9183 -- Restore_Private_Views --
9184 ---------------------------
9186 procedure Restore_Private_Views
9187 (Pack_Id : Entity_Id;
9188 Is_Package : Boolean := True)
9190 M : Elmt_Id;
9191 E : Entity_Id;
9192 Typ : Entity_Id;
9193 Dep_Elmt : Elmt_Id;
9194 Dep_Typ : Node_Id;
9196 procedure Restore_Nested_Formal (Formal : Entity_Id);
9197 -- Hide the generic formals of formal packages declared with box
9198 -- which were reachable in the current instantiation.
9200 procedure Restore_Nested_Formal (Formal : Entity_Id) is
9201 Ent : Entity_Id;
9202 begin
9203 if Present (Renamed_Object (Formal))
9204 and then Denotes_Formal_Package (Renamed_Object (Formal), True)
9205 then
9206 return;
9208 elsif Present (Associated_Formal_Package (Formal))
9209 and then Box_Present (Parent (Associated_Formal_Package (Formal)))
9210 then
9211 Ent := First_Entity (Formal);
9213 while Present (Ent) loop
9214 exit when Ekind (Ent) = E_Package
9215 and then Renamed_Entity (Ent) = Renamed_Entity (Formal);
9217 Set_Is_Hidden (Ent);
9218 Set_Is_Potentially_Use_Visible (Ent, False);
9220 -- If package, then recurse
9222 if Ekind (Ent) = E_Package then
9223 Restore_Nested_Formal (Ent);
9224 end if;
9226 Next_Entity (Ent);
9227 end loop;
9228 end if;
9229 end Restore_Nested_Formal;
9231 begin
9232 M := First_Elmt (Exchanged_Views);
9233 while Present (M) loop
9234 Typ := Node (M);
9236 -- Subtypes of types whose views have been exchanged, and that
9237 -- are defined within the instance, were not on the list of
9238 -- Private_Dependents on entry to the instance, so they have to
9239 -- be exchanged explicitly now, in order to remain consistent with
9240 -- the view of the parent type.
9242 if Ekind (Typ) = E_Private_Type
9243 or else Ekind (Typ) = E_Limited_Private_Type
9244 or else Ekind (Typ) = E_Record_Type_With_Private
9245 then
9246 Dep_Elmt := First_Elmt (Private_Dependents (Typ));
9248 while Present (Dep_Elmt) loop
9249 Dep_Typ := Node (Dep_Elmt);
9251 if Scope (Dep_Typ) = Pack_Id
9252 and then Present (Full_View (Dep_Typ))
9253 then
9254 Replace_Elmt (Dep_Elmt, Full_View (Dep_Typ));
9255 Exchange_Declarations (Dep_Typ);
9256 end if;
9258 Next_Elmt (Dep_Elmt);
9259 end loop;
9260 end if;
9262 Exchange_Declarations (Node (M));
9263 Next_Elmt (M);
9264 end loop;
9266 if No (Pack_Id) then
9267 return;
9268 end if;
9270 -- Make the generic formal parameters private, and make the formal
9271 -- types into subtypes of the actuals again.
9273 E := First_Entity (Pack_Id);
9275 while Present (E) loop
9276 Set_Is_Hidden (E, True);
9278 if Is_Type (E)
9279 and then Nkind (Parent (E)) = N_Subtype_Declaration
9280 then
9281 Set_Is_Generic_Actual_Type (E, False);
9283 -- An unusual case of aliasing: the actual may also be directly
9284 -- visible in the generic, and be private there, while it is
9285 -- fully visible in the context of the instance. The internal
9286 -- subtype is private in the instance, but has full visibility
9287 -- like its parent in the enclosing scope. This enforces the
9288 -- invariant that the privacy status of all private dependents of
9289 -- a type coincide with that of the parent type. This can only
9290 -- happen when a generic child unit is instantiated within a
9291 -- sibling.
9293 if Is_Private_Type (E)
9294 and then not Is_Private_Type (Etype (E))
9295 then
9296 Exchange_Declarations (E);
9297 end if;
9299 elsif Ekind (E) = E_Package then
9301 -- The end of the renaming list is the renaming of the generic
9302 -- package itself. If the instance is a subprogram, all entities
9303 -- in the corresponding package are renamings. If this entity is
9304 -- a formal package, make its own formals private as well. The
9305 -- actual in this case is itself the renaming of an instantation.
9306 -- If the entity is not a package renaming, it is the entity
9307 -- created to validate formal package actuals: ignore.
9309 -- If the actual is itself a formal package for the enclosing
9310 -- generic, or the actual for such a formal package, it remains
9311 -- visible on exit from the instance, and therefore nothing
9312 -- needs to be done either, except to keep it accessible.
9314 if Is_Package
9315 and then Renamed_Object (E) = Pack_Id
9316 then
9317 exit;
9319 elsif Nkind (Parent (E)) /= N_Package_Renaming_Declaration then
9320 null;
9322 elsif Denotes_Formal_Package (Renamed_Object (E), True) then
9323 Set_Is_Hidden (E, False);
9325 else
9326 declare
9327 Act_P : constant Entity_Id := Renamed_Object (E);
9328 Id : Entity_Id;
9330 begin
9331 Id := First_Entity (Act_P);
9332 while Present (Id)
9333 and then Id /= First_Private_Entity (Act_P)
9334 loop
9335 exit when Ekind (Id) = E_Package
9336 and then Renamed_Object (Id) = Act_P;
9338 Set_Is_Hidden (Id, True);
9339 Set_Is_Potentially_Use_Visible (Id, In_Use (Act_P));
9341 if Ekind (Id) = E_Package then
9342 Restore_Nested_Formal (Id);
9343 end if;
9345 Next_Entity (Id);
9346 end loop;
9347 end;
9348 end if;
9349 end if;
9351 Next_Entity (E);
9352 end loop;
9353 end Restore_Private_Views;
9355 --------------
9356 -- Save_Env --
9357 --------------
9359 procedure Save_Env
9360 (Gen_Unit : Entity_Id;
9361 Act_Unit : Entity_Id)
9363 begin
9364 Init_Env;
9365 Set_Instance_Env (Gen_Unit, Act_Unit);
9366 end Save_Env;
9368 ----------------------------
9369 -- Save_Global_References --
9370 ----------------------------
9372 procedure Save_Global_References (N : Node_Id) is
9373 Gen_Scope : Entity_Id;
9374 E : Entity_Id;
9375 N2 : Node_Id;
9377 function Is_Global (E : Entity_Id) return Boolean;
9378 -- Check whether entity is defined outside of generic unit.
9379 -- Examine the scope of an entity, and the scope of the scope,
9380 -- etc, until we find either Standard, in which case the entity
9381 -- is global, or the generic unit itself, which indicates that
9382 -- the entity is local. If the entity is the generic unit itself,
9383 -- as in the case of a recursive call, or the enclosing generic unit,
9384 -- if different from the current scope, then it is local as well,
9385 -- because it will be replaced at the point of instantiation. On
9386 -- the other hand, if it is a reference to a child unit of a common
9387 -- ancestor, which appears in an instantiation, it is global because
9388 -- it is used to denote a specific compilation unit at the time the
9389 -- instantiations will be analyzed.
9391 procedure Reset_Entity (N : Node_Id);
9392 -- Save semantic information on global entity, so that it is not
9393 -- resolved again at instantiation time.
9395 procedure Save_Entity_Descendants (N : Node_Id);
9396 -- Apply Save_Global_References to the two syntactic descendants of
9397 -- non-terminal nodes that carry an Associated_Node and are processed
9398 -- through Reset_Entity. Once the global entity (if any) has been
9399 -- captured together with its type, only two syntactic descendants
9400 -- need to be traversed to complete the processing of the tree rooted
9401 -- at N. This applies to Selected_Components, Expanded_Names, and to
9402 -- Operator nodes. N can also be a character literal, identifier, or
9403 -- operator symbol node, but the call has no effect in these cases.
9405 procedure Save_Global_Defaults (N1, N2 : Node_Id);
9406 -- Default actuals in nested instances must be handled specially
9407 -- because there is no link to them from the original tree. When an
9408 -- actual subprogram is given by a default, we add an explicit generic
9409 -- association for it in the instantiation node. When we save the
9410 -- global references on the name of the instance, we recover the list
9411 -- of generic associations, and add an explicit one to the original
9412 -- generic tree, through which a global actual can be preserved.
9413 -- Similarly, if a child unit is instantiated within a sibling, in the
9414 -- context of the parent, we must preserve the identifier of the parent
9415 -- so that it can be properly resolved in a subsequent instantiation.
9417 procedure Save_Global_Descendant (D : Union_Id);
9418 -- Apply Save_Global_References recursively to the descendents of
9419 -- current node.
9421 procedure Save_References (N : Node_Id);
9422 -- This is the recursive procedure that does the work, once the
9423 -- enclosing generic scope has been established.
9425 ---------------
9426 -- Is_Global --
9427 ---------------
9429 function Is_Global (E : Entity_Id) return Boolean is
9430 Se : Entity_Id := Scope (E);
9432 function Is_Instance_Node (Decl : Node_Id) return Boolean;
9433 -- Determine whether the parent node of a reference to a child unit
9434 -- denotes an instantiation or a formal package, in which case the
9435 -- reference to the child unit is global, even if it appears within
9436 -- the current scope (e.g. when the instance appears within the body
9437 -- of an ancestor).
9439 ----------------------
9440 -- Is_Instance_Node --
9441 ----------------------
9443 function Is_Instance_Node (Decl : Node_Id) return Boolean is
9444 begin
9445 return (Nkind (Decl) in N_Generic_Instantiation
9446 or else
9447 Nkind (Original_Node (Decl)) = N_Formal_Package_Declaration);
9448 end Is_Instance_Node;
9450 -- Start of processing for Is_Global
9452 begin
9453 if E = Gen_Scope then
9454 return False;
9456 elsif E = Standard_Standard then
9457 return True;
9459 elsif Is_Child_Unit (E)
9460 and then (Is_Instance_Node (Parent (N2))
9461 or else (Nkind (Parent (N2)) = N_Expanded_Name
9462 and then N2 = Selector_Name (Parent (N2))
9463 and then Is_Instance_Node (Parent (Parent (N2)))))
9464 then
9465 return True;
9467 else
9468 while Se /= Gen_Scope loop
9469 if Se = Standard_Standard then
9470 return True;
9471 else
9472 Se := Scope (Se);
9473 end if;
9474 end loop;
9476 return False;
9477 end if;
9478 end Is_Global;
9480 ------------------
9481 -- Reset_Entity --
9482 ------------------
9484 procedure Reset_Entity (N : Node_Id) is
9486 procedure Set_Global_Type (N : Node_Id; N2 : Node_Id);
9487 -- The type of N2 is global to the generic unit. Save the
9488 -- type in the generic node.
9490 function Top_Ancestor (E : Entity_Id) return Entity_Id;
9491 -- Find the ultimate ancestor of the current unit. If it is
9492 -- not a generic unit, then the name of the current unit
9493 -- in the prefix of an expanded name must be replaced with
9494 -- its generic homonym to ensure that it will be properly
9495 -- resolved in an instance.
9497 ---------------------
9498 -- Set_Global_Type --
9499 ---------------------
9501 procedure Set_Global_Type (N : Node_Id; N2 : Node_Id) is
9502 Typ : constant Entity_Id := Etype (N2);
9504 begin
9505 Set_Etype (N, Typ);
9507 if Entity (N) /= N2
9508 and then Has_Private_View (Entity (N))
9509 then
9510 -- If the entity of N is not the associated node, this is
9511 -- a nested generic and it has an associated node as well,
9512 -- whose type is already the full view (see below). Indicate
9513 -- that the original node has a private view.
9515 Set_Has_Private_View (N);
9516 end if;
9518 -- If not a private type, nothing else to do
9520 if not Is_Private_Type (Typ) then
9521 if Is_Array_Type (Typ)
9522 and then Is_Private_Type (Component_Type (Typ))
9523 then
9524 Set_Has_Private_View (N);
9525 end if;
9527 -- If it is a derivation of a private type in a context where
9528 -- no full view is needed, nothing to do either.
9530 elsif No (Full_View (Typ)) and then Typ /= Etype (Typ) then
9531 null;
9533 -- Otherwise mark the type for flipping and use the full_view
9534 -- when available.
9536 else
9537 Set_Has_Private_View (N);
9539 if Present (Full_View (Typ)) then
9540 Set_Etype (N2, Full_View (Typ));
9541 end if;
9542 end if;
9543 end Set_Global_Type;
9545 ------------------
9546 -- Top_Ancestor --
9547 ------------------
9549 function Top_Ancestor (E : Entity_Id) return Entity_Id is
9550 Par : Entity_Id := E;
9552 begin
9553 while Is_Child_Unit (Par) loop
9554 Par := Scope (Par);
9555 end loop;
9557 return Par;
9558 end Top_Ancestor;
9560 -- Start of processing for Reset_Entity
9562 begin
9563 N2 := Get_Associated_Node (N);
9564 E := Entity (N2);
9566 if Present (E) then
9567 if Is_Global (E) then
9568 Set_Global_Type (N, N2);
9570 elsif Nkind (N) = N_Op_Concat
9571 and then Is_Generic_Type (Etype (N2))
9572 and then
9573 (Base_Type (Etype (Right_Opnd (N2))) = Etype (N2)
9574 or else Base_Type (Etype (Left_Opnd (N2))) = Etype (N2))
9575 and then Is_Intrinsic_Subprogram (E)
9576 then
9577 null;
9579 else
9580 -- Entity is local. Mark generic node as unresolved.
9581 -- Note that now it does not have an entity.
9583 Set_Associated_Node (N, Empty);
9584 Set_Etype (N, Empty);
9585 end if;
9587 if (Nkind (Parent (N)) = N_Package_Instantiation
9588 or else Nkind (Parent (N)) = N_Function_Instantiation
9589 or else Nkind (Parent (N)) = N_Procedure_Instantiation)
9590 and then N = Name (Parent (N))
9591 then
9592 Save_Global_Defaults (Parent (N), Parent (N2));
9593 end if;
9595 elsif Nkind (Parent (N)) = N_Selected_Component
9596 and then Nkind (Parent (N2)) = N_Expanded_Name
9597 then
9599 if Is_Global (Entity (Parent (N2))) then
9600 Change_Selected_Component_To_Expanded_Name (Parent (N));
9601 Set_Associated_Node (Parent (N), Parent (N2));
9602 Set_Global_Type (Parent (N), Parent (N2));
9603 Save_Entity_Descendants (N);
9605 -- If this is a reference to the current generic entity,
9606 -- replace by the name of the generic homonym of the current
9607 -- package. This is because in an instantiation Par.P.Q will
9608 -- not resolve to the name of the instance, whose enclosing
9609 -- scope is not necessarily Par. We use the generic homonym
9610 -- rather that the name of the generic itself, because it may
9611 -- be hidden by a local declaration.
9613 elsif In_Open_Scopes (Entity (Parent (N2)))
9614 and then not
9615 Is_Generic_Unit (Top_Ancestor (Entity (Prefix (Parent (N2)))))
9616 then
9617 if Ekind (Entity (Parent (N2))) = E_Generic_Package then
9618 Rewrite (Parent (N),
9619 Make_Identifier (Sloc (N),
9620 Chars =>
9621 Chars (Generic_Homonym (Entity (Parent (N2))))));
9622 else
9623 Rewrite (Parent (N),
9624 Make_Identifier (Sloc (N),
9625 Chars => Chars (Selector_Name (Parent (N2)))));
9626 end if;
9627 end if;
9629 if (Nkind (Parent (Parent (N))) = N_Package_Instantiation
9630 or else Nkind (Parent (Parent (N)))
9631 = N_Function_Instantiation
9632 or else Nkind (Parent (Parent (N)))
9633 = N_Procedure_Instantiation)
9634 and then Parent (N) = Name (Parent (Parent (N)))
9635 then
9636 Save_Global_Defaults
9637 (Parent (Parent (N)), Parent (Parent ((N2))));
9638 end if;
9640 -- A selected component may denote a static constant that has
9641 -- been folded. Make the same replacement in original tree.
9643 elsif Nkind (Parent (N)) = N_Selected_Component
9644 and then (Nkind (Parent (N2)) = N_Integer_Literal
9645 or else Nkind (Parent (N2)) = N_Real_Literal)
9646 then
9647 Rewrite (Parent (N),
9648 New_Copy (Parent (N2)));
9649 Set_Analyzed (Parent (N), False);
9651 -- A selected component may be transformed into a parameterless
9652 -- function call. If the called entity is global, rewrite the
9653 -- node appropriately, i.e. as an extended name for the global
9654 -- entity.
9656 elsif Nkind (Parent (N)) = N_Selected_Component
9657 and then Nkind (Parent (N2)) = N_Function_Call
9658 and then Is_Global (Entity (Name (Parent (N2))))
9659 then
9660 Change_Selected_Component_To_Expanded_Name (Parent (N));
9661 Set_Associated_Node (Parent (N), Name (Parent (N2)));
9662 Set_Global_Type (Parent (N), Name (Parent (N2)));
9663 Save_Entity_Descendants (N);
9665 else
9666 -- Entity is local. Reset in generic unit, so that node
9667 -- is resolved anew at the point of instantiation.
9669 Set_Associated_Node (N, Empty);
9670 Set_Etype (N, Empty);
9671 end if;
9672 end Reset_Entity;
9674 -----------------------------
9675 -- Save_Entity_Descendants --
9676 -----------------------------
9678 procedure Save_Entity_Descendants (N : Node_Id) is
9679 begin
9680 case Nkind (N) is
9681 when N_Binary_Op =>
9682 Save_Global_Descendant (Union_Id (Left_Opnd (N)));
9683 Save_Global_Descendant (Union_Id (Right_Opnd (N)));
9685 when N_Unary_Op =>
9686 Save_Global_Descendant (Union_Id (Right_Opnd (N)));
9688 when N_Expanded_Name | N_Selected_Component =>
9689 Save_Global_Descendant (Union_Id (Prefix (N)));
9690 Save_Global_Descendant (Union_Id (Selector_Name (N)));
9692 when N_Identifier | N_Character_Literal | N_Operator_Symbol =>
9693 null;
9695 when others =>
9696 raise Program_Error;
9697 end case;
9698 end Save_Entity_Descendants;
9700 --------------------------
9701 -- Save_Global_Defaults --
9702 --------------------------
9704 procedure Save_Global_Defaults (N1, N2 : Node_Id) is
9705 Loc : constant Source_Ptr := Sloc (N1);
9706 Assoc2 : constant List_Id := Generic_Associations (N2);
9707 Gen_Id : constant Entity_Id := Get_Generic_Entity (N2);
9708 Assoc1 : List_Id;
9709 Act1 : Node_Id;
9710 Act2 : Node_Id;
9711 Def : Node_Id;
9712 Ndec : Node_Id;
9713 Subp : Entity_Id;
9714 Actual : Entity_Id;
9716 begin
9717 Assoc1 := Generic_Associations (N1);
9719 if Present (Assoc1) then
9720 Act1 := First (Assoc1);
9721 else
9722 Act1 := Empty;
9723 Set_Generic_Associations (N1, New_List);
9724 Assoc1 := Generic_Associations (N1);
9725 end if;
9727 if Present (Assoc2) then
9728 Act2 := First (Assoc2);
9729 else
9730 return;
9731 end if;
9733 while Present (Act1) and then Present (Act2) loop
9734 Next (Act1);
9735 Next (Act2);
9736 end loop;
9738 -- Find the associations added for default suprograms
9740 if Present (Act2) then
9741 while Nkind (Act2) /= N_Generic_Association
9742 or else No (Entity (Selector_Name (Act2)))
9743 or else not Is_Overloadable (Entity (Selector_Name (Act2)))
9744 loop
9745 Next (Act2);
9746 end loop;
9748 -- Add a similar association if the default is global. The
9749 -- renaming declaration for the actual has been analyzed, and
9750 -- its alias is the program it renames. Link the actual in the
9751 -- original generic tree with the node in the analyzed tree.
9753 while Present (Act2) loop
9754 Subp := Entity (Selector_Name (Act2));
9755 Def := Explicit_Generic_Actual_Parameter (Act2);
9757 -- Following test is defence against rubbish errors
9759 if No (Alias (Subp)) then
9760 return;
9761 end if;
9763 -- Retrieve the resolved actual from the renaming declaration
9764 -- created for the instantiated formal.
9766 Actual := Entity (Name (Parent (Parent (Subp))));
9767 Set_Entity (Def, Actual);
9768 Set_Etype (Def, Etype (Actual));
9770 if Is_Global (Actual) then
9771 Ndec :=
9772 Make_Generic_Association (Loc,
9773 Selector_Name => New_Occurrence_Of (Subp, Loc),
9774 Explicit_Generic_Actual_Parameter =>
9775 New_Occurrence_Of (Actual, Loc));
9777 Set_Associated_Node
9778 (Explicit_Generic_Actual_Parameter (Ndec), Def);
9780 Append (Ndec, Assoc1);
9782 -- If there are other defaults, add a dummy association
9783 -- in case there are other defaulted formals with the same
9784 -- name.
9786 elsif Present (Next (Act2)) then
9787 Ndec :=
9788 Make_Generic_Association (Loc,
9789 Selector_Name => New_Occurrence_Of (Subp, Loc),
9790 Explicit_Generic_Actual_Parameter => Empty);
9792 Append (Ndec, Assoc1);
9793 end if;
9795 Next (Act2);
9796 end loop;
9797 end if;
9799 if Nkind (Name (N1)) = N_Identifier
9800 and then Is_Child_Unit (Gen_Id)
9801 and then Is_Global (Gen_Id)
9802 and then Is_Generic_Unit (Scope (Gen_Id))
9803 and then In_Open_Scopes (Scope (Gen_Id))
9804 then
9805 -- This is an instantiation of a child unit within a sibling,
9806 -- so that the generic parent is in scope. An eventual instance
9807 -- must occur within the scope of an instance of the parent.
9808 -- Make name in instance into an expanded name, to preserve the
9809 -- identifier of the parent, so it can be resolved subsequently.
9811 Rewrite (Name (N2),
9812 Make_Expanded_Name (Loc,
9813 Chars => Chars (Gen_Id),
9814 Prefix => New_Occurrence_Of (Scope (Gen_Id), Loc),
9815 Selector_Name => New_Occurrence_Of (Gen_Id, Loc)));
9816 Set_Entity (Name (N2), Gen_Id);
9818 Rewrite (Name (N1),
9819 Make_Expanded_Name (Loc,
9820 Chars => Chars (Gen_Id),
9821 Prefix => New_Occurrence_Of (Scope (Gen_Id), Loc),
9822 Selector_Name => New_Occurrence_Of (Gen_Id, Loc)));
9824 Set_Associated_Node (Name (N1), Name (N2));
9825 Set_Associated_Node (Prefix (Name (N1)), Empty);
9826 Set_Associated_Node
9827 (Selector_Name (Name (N1)), Selector_Name (Name (N2)));
9828 Set_Etype (Name (N1), Etype (Gen_Id));
9829 end if;
9831 end Save_Global_Defaults;
9833 ----------------------------
9834 -- Save_Global_Descendant --
9835 ----------------------------
9837 procedure Save_Global_Descendant (D : Union_Id) is
9838 N1 : Node_Id;
9840 begin
9841 if D in Node_Range then
9842 if D = Union_Id (Empty) then
9843 null;
9845 elsif Nkind (Node_Id (D)) /= N_Compilation_Unit then
9846 Save_References (Node_Id (D));
9847 end if;
9849 elsif D in List_Range then
9850 if D = Union_Id (No_List)
9851 or else Is_Empty_List (List_Id (D))
9852 then
9853 null;
9855 else
9856 N1 := First (List_Id (D));
9857 while Present (N1) loop
9858 Save_References (N1);
9859 Next (N1);
9860 end loop;
9861 end if;
9863 -- Element list or other non-node field, nothing to do
9865 else
9866 null;
9867 end if;
9868 end Save_Global_Descendant;
9870 ---------------------
9871 -- Save_References --
9872 ---------------------
9874 -- This is the recursive procedure that does the work, once the
9875 -- enclosing generic scope has been established. We have to treat
9876 -- specially a number of node rewritings that are required by semantic
9877 -- processing and which change the kind of nodes in the generic copy:
9878 -- typically constant-folding, replacing an operator node by a string
9879 -- literal, or a selected component by an expanded name. In each of
9880 -- those cases, the transformation is propagated to the generic unit.
9882 procedure Save_References (N : Node_Id) is
9883 begin
9884 if N = Empty then
9885 null;
9887 elsif Nkind (N) = N_Character_Literal
9888 or else Nkind (N) = N_Operator_Symbol
9889 then
9890 if Nkind (N) = Nkind (Get_Associated_Node (N)) then
9891 Reset_Entity (N);
9893 elsif Nkind (N) = N_Operator_Symbol
9894 and then Nkind (Get_Associated_Node (N)) = N_String_Literal
9895 then
9896 Change_Operator_Symbol_To_String_Literal (N);
9897 end if;
9899 elsif Nkind (N) in N_Op then
9901 if Nkind (N) = Nkind (Get_Associated_Node (N)) then
9903 if Nkind (N) = N_Op_Concat then
9904 Set_Is_Component_Left_Opnd (N,
9905 Is_Component_Left_Opnd (Get_Associated_Node (N)));
9907 Set_Is_Component_Right_Opnd (N,
9908 Is_Component_Right_Opnd (Get_Associated_Node (N)));
9909 end if;
9911 Reset_Entity (N);
9912 else
9913 -- Node may be transformed into call to a user-defined operator
9915 N2 := Get_Associated_Node (N);
9917 if Nkind (N2) = N_Function_Call then
9918 E := Entity (Name (N2));
9920 if Present (E)
9921 and then Is_Global (E)
9922 then
9923 Set_Etype (N, Etype (N2));
9924 else
9925 Set_Associated_Node (N, Empty);
9926 Set_Etype (N, Empty);
9927 end if;
9929 elsif Nkind (N2) = N_Integer_Literal
9930 or else Nkind (N2) = N_Real_Literal
9931 or else Nkind (N2) = N_String_Literal
9932 then
9933 -- Operation was constant-folded, perform the same
9934 -- replacement in generic.
9936 Rewrite (N, New_Copy (N2));
9937 Set_Analyzed (N, False);
9939 elsif Nkind (N2) = N_Identifier
9940 and then Ekind (Entity (N2)) = E_Enumeration_Literal
9941 then
9942 -- Same if call was folded into a literal, but in this
9943 -- case retain the entity to avoid spurious ambiguities
9944 -- if id is overloaded at the point of instantiation or
9945 -- inlining.
9947 Rewrite (N, New_Copy (N2));
9948 Set_Analyzed (N, False);
9949 end if;
9950 end if;
9952 -- Complete the check on operands, if node has not been
9953 -- constant-folded.
9955 if Nkind (N) in N_Op then
9956 Save_Entity_Descendants (N);
9957 end if;
9959 elsif Nkind (N) = N_Identifier then
9960 if Nkind (N) = Nkind (Get_Associated_Node (N)) then
9962 -- If this is a discriminant reference, always save it.
9963 -- It is used in the instance to find the corresponding
9964 -- discriminant positionally rather than by name.
9966 Set_Original_Discriminant
9967 (N, Original_Discriminant (Get_Associated_Node (N)));
9968 Reset_Entity (N);
9970 else
9971 N2 := Get_Associated_Node (N);
9973 if Nkind (N2) = N_Function_Call then
9974 E := Entity (Name (N2));
9976 -- Name resolves to a call to parameterless function.
9977 -- If original entity is global, mark node as resolved.
9979 if Present (E)
9980 and then Is_Global (E)
9981 then
9982 Set_Etype (N, Etype (N2));
9983 else
9984 Set_Associated_Node (N, Empty);
9985 Set_Etype (N, Empty);
9986 end if;
9988 elsif
9989 Nkind (N2) = N_Integer_Literal or else
9990 Nkind (N2) = N_Real_Literal or else
9991 Nkind (N2) = N_String_Literal
9992 then
9993 -- Name resolves to named number that is constant-folded,
9994 -- or to string literal from concatenation.
9995 -- Perform the same replacement in generic.
9997 Rewrite (N, New_Copy (N2));
9998 Set_Analyzed (N, False);
10000 elsif Nkind (N2) = N_Explicit_Dereference then
10002 -- An identifier is rewritten as a dereference if it is
10003 -- the prefix in a selected component, and it denotes an
10004 -- access to a composite type, or a parameterless function
10005 -- call that returns an access type.
10007 -- Check whether corresponding entity in prefix is global
10009 if Is_Entity_Name (Prefix (N2))
10010 and then Present (Entity (Prefix (N2)))
10011 and then Is_Global (Entity (Prefix (N2)))
10012 then
10013 Rewrite (N,
10014 Make_Explicit_Dereference (Sloc (N),
10015 Prefix => Make_Identifier (Sloc (N),
10016 Chars => Chars (N))));
10017 Set_Associated_Node (Prefix (N), Prefix (N2));
10019 elsif Nkind (Prefix (N2)) = N_Function_Call
10020 and then Is_Global (Entity (Name (Prefix (N2))))
10021 then
10022 Rewrite (N,
10023 Make_Explicit_Dereference (Sloc (N),
10024 Prefix => Make_Function_Call (Sloc (N),
10025 Name =>
10026 Make_Identifier (Sloc (N),
10027 Chars => Chars (N)))));
10029 Set_Associated_Node
10030 (Name (Prefix (N)), Name (Prefix (N2)));
10032 else
10033 Set_Associated_Node (N, Empty);
10034 Set_Etype (N, Empty);
10035 end if;
10037 -- The subtype mark of a nominally unconstrained object
10038 -- is rewritten as a subtype indication using the bounds
10039 -- of the expression. Recover the original subtype mark.
10041 elsif Nkind (N2) = N_Subtype_Indication
10042 and then Is_Entity_Name (Original_Node (N2))
10043 then
10044 Set_Associated_Node (N, Original_Node (N2));
10045 Reset_Entity (N);
10047 else
10048 null;
10049 end if;
10050 end if;
10052 elsif Nkind (N) in N_Entity then
10053 null;
10055 else
10056 declare
10057 use Atree.Unchecked_Access;
10058 -- This code section is part of implementing an untyped tree
10059 -- traversal, so it needs direct access to node fields.
10061 begin
10062 if Nkind (N) = N_Aggregate
10063 or else
10064 Nkind (N) = N_Extension_Aggregate
10065 then
10066 N2 := Get_Associated_Node (N);
10068 if No (N2)
10069 or else No (Etype (N2))
10070 or else not Is_Global (Etype (N2))
10071 then
10072 Set_Associated_Node (N, Empty);
10073 end if;
10075 Save_Global_Descendant (Field1 (N));
10076 Save_Global_Descendant (Field2 (N));
10077 Save_Global_Descendant (Field3 (N));
10078 Save_Global_Descendant (Field5 (N));
10080 -- All other cases than aggregates
10082 else
10083 Save_Global_Descendant (Field1 (N));
10084 Save_Global_Descendant (Field2 (N));
10085 Save_Global_Descendant (Field3 (N));
10086 Save_Global_Descendant (Field4 (N));
10087 Save_Global_Descendant (Field5 (N));
10088 end if;
10089 end;
10090 end if;
10091 end Save_References;
10093 -- Start of processing for Save_Global_References
10095 begin
10096 Gen_Scope := Current_Scope;
10098 -- If the generic unit is a child unit, references to entities in
10099 -- the parent are treated as local, because they will be resolved
10100 -- anew in the context of the instance of the parent.
10102 while Is_Child_Unit (Gen_Scope)
10103 and then Ekind (Scope (Gen_Scope)) = E_Generic_Package
10104 loop
10105 Gen_Scope := Scope (Gen_Scope);
10106 end loop;
10108 Save_References (N);
10109 end Save_Global_References;
10111 --------------------------------------
10112 -- Set_Copied_Sloc_For_Inlined_Body --
10113 --------------------------------------
10115 procedure Set_Copied_Sloc_For_Inlined_Body (N : Node_Id; E : Entity_Id) is
10116 begin
10117 Create_Instantiation_Source (N, E, True, S_Adjustment);
10118 end Set_Copied_Sloc_For_Inlined_Body;
10120 ---------------------
10121 -- Set_Instance_Of --
10122 ---------------------
10124 procedure Set_Instance_Of (A : Entity_Id; B : Entity_Id) is
10125 begin
10126 Generic_Renamings.Table (Generic_Renamings.Last) := (A, B, Assoc_Null);
10127 Generic_Renamings_HTable.Set (Generic_Renamings.Last);
10128 Generic_Renamings.Increment_Last;
10129 end Set_Instance_Of;
10131 --------------------
10132 -- Set_Next_Assoc --
10133 --------------------
10135 procedure Set_Next_Assoc (E : Assoc_Ptr; Next : Assoc_Ptr) is
10136 begin
10137 Generic_Renamings.Table (E).Next_In_HTable := Next;
10138 end Set_Next_Assoc;
10140 -------------------
10141 -- Start_Generic --
10142 -------------------
10144 procedure Start_Generic is
10145 begin
10146 -- ??? I am sure more things could be factored out in this
10147 -- routine. Should probably be done at a later stage.
10149 Generic_Flags.Increment_Last;
10150 Generic_Flags.Table (Generic_Flags.Last) := Inside_A_Generic;
10151 Inside_A_Generic := True;
10153 Expander_Mode_Save_And_Set (False);
10154 end Start_Generic;
10156 ----------------------
10157 -- Set_Instance_Env --
10158 ----------------------
10160 procedure Set_Instance_Env
10161 (Gen_Unit : Entity_Id;
10162 Act_Unit : Entity_Id)
10164 begin
10165 -- Regardless of the current mode, predefined units are analyzed in
10166 -- the most current Ada mode, and earlier version Ada checks do not
10167 -- apply to predefined units.
10169 -- Why is this not using the routine Opt.Set_Opt_Config_Switches ???
10171 if Is_Internal_File_Name
10172 (Fname => Unit_File_Name (Get_Source_Unit (Gen_Unit)),
10173 Renamings_Included => True) then
10174 Ada_Version := Ada_Version_Type'Last;
10175 Ada_Version_Explicit := Ada_Version_Explicit_Config;
10176 end if;
10178 Current_Instantiated_Parent := (Gen_Unit, Act_Unit, Assoc_Null);
10179 end Set_Instance_Env;
10181 -----------------
10182 -- Switch_View --
10183 -----------------
10185 procedure Switch_View (T : Entity_Id) is
10186 BT : constant Entity_Id := Base_Type (T);
10187 Priv_Elmt : Elmt_Id := No_Elmt;
10188 Priv_Sub : Entity_Id;
10190 begin
10191 -- T may be private but its base type may have been exchanged through
10192 -- some other occurrence, in which case there is nothing to switch.
10194 if not Is_Private_Type (BT) then
10195 return;
10196 end if;
10198 Priv_Elmt := First_Elmt (Private_Dependents (BT));
10200 if Present (Full_View (BT)) then
10201 Prepend_Elmt (Full_View (BT), Exchanged_Views);
10202 Exchange_Declarations (BT);
10203 end if;
10205 while Present (Priv_Elmt) loop
10206 Priv_Sub := (Node (Priv_Elmt));
10208 -- We avoid flipping the subtype if the Etype of its full
10209 -- view is private because this would result in a malformed
10210 -- subtype. This occurs when the Etype of the subtype full
10211 -- view is the full view of the base type (and since the
10212 -- base types were just switched, the subtype is pointing
10213 -- to the wrong view). This is currently the case for
10214 -- tagged record types, access types (maybe more?) and
10215 -- needs to be resolved. ???
10217 if Present (Full_View (Priv_Sub))
10218 and then not Is_Private_Type (Etype (Full_View (Priv_Sub)))
10219 then
10220 Prepend_Elmt (Full_View (Priv_Sub), Exchanged_Views);
10221 Exchange_Declarations (Priv_Sub);
10222 end if;
10224 Next_Elmt (Priv_Elmt);
10225 end loop;
10226 end Switch_View;
10228 -----------------------------
10229 -- Valid_Default_Attribute --
10230 -----------------------------
10232 procedure Valid_Default_Attribute (Nam : Entity_Id; Def : Node_Id) is
10233 Attr_Id : constant Attribute_Id :=
10234 Get_Attribute_Id (Attribute_Name (Def));
10235 T : constant Entity_Id := Entity (Prefix (Def));
10236 Is_Fun : constant Boolean := (Ekind (Nam) = E_Function);
10237 F : Entity_Id;
10238 Num_F : Int;
10239 OK : Boolean;
10241 begin
10242 if No (T)
10243 or else T = Any_Id
10244 then
10245 return;
10246 end if;
10248 Num_F := 0;
10249 F := First_Formal (Nam);
10250 while Present (F) loop
10251 Num_F := Num_F + 1;
10252 Next_Formal (F);
10253 end loop;
10255 case Attr_Id is
10256 when Attribute_Adjacent | Attribute_Ceiling | Attribute_Copy_Sign |
10257 Attribute_Floor | Attribute_Fraction | Attribute_Machine |
10258 Attribute_Model | Attribute_Remainder | Attribute_Rounding |
10259 Attribute_Unbiased_Rounding =>
10260 OK := Is_Fun
10261 and then Num_F = 1
10262 and then Is_Floating_Point_Type (T);
10264 when Attribute_Image | Attribute_Pred | Attribute_Succ |
10265 Attribute_Value | Attribute_Wide_Image |
10266 Attribute_Wide_Value =>
10267 OK := (Is_Fun and then Num_F = 1 and then Is_Scalar_Type (T));
10269 when Attribute_Max | Attribute_Min =>
10270 OK := (Is_Fun and then Num_F = 2 and then Is_Scalar_Type (T));
10272 when Attribute_Input =>
10273 OK := (Is_Fun and then Num_F = 1);
10275 when Attribute_Output | Attribute_Read | Attribute_Write =>
10276 OK := (not Is_Fun and then Num_F = 2);
10278 when others =>
10279 OK := False;
10280 end case;
10282 if not OK then
10283 Error_Msg_N ("attribute reference has wrong profile for subprogram",
10284 Def);
10285 end if;
10286 end Valid_Default_Attribute;
10288 end Sem_Ch12;