* arm.c (FL_WBUF): Define.
[official-gcc.git] / gcc / ada / sem_ch12.adb
blob661ac7651bc45d0d1dbcc8ba4dd1e08a77ab4580
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, 59 Temple Place - Suite 330, Boston, --
20 -- MA 02111-1307, 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 Inline; use Inline;
37 with Lib; use Lib;
38 with Lib.Load; use Lib.Load;
39 with Lib.Xref; use Lib.Xref;
40 with Nlists; use Nlists;
41 with Nmake; use Nmake;
42 with Opt; use Opt;
43 with Rident; use Rident;
44 with Restrict; use Restrict;
45 with Rtsfind; use Rtsfind;
46 with Sem; use Sem;
47 with Sem_Cat; use Sem_Cat;
48 with Sem_Ch3; use Sem_Ch3;
49 with Sem_Ch6; use Sem_Ch6;
50 with Sem_Ch7; use Sem_Ch7;
51 with Sem_Ch8; use Sem_Ch8;
52 with Sem_Ch10; use Sem_Ch10;
53 with Sem_Ch13; use Sem_Ch13;
54 with Sem_Disp; use Sem_Disp;
55 with Sem_Elab; use Sem_Elab;
56 with Sem_Elim; use Sem_Elim;
57 with Sem_Eval; use Sem_Eval;
58 with Sem_Res; use Sem_Res;
59 with Sem_Type; use Sem_Type;
60 with Sem_Util; use Sem_Util;
61 with Sem_Warn; use Sem_Warn;
62 with Stand; use Stand;
63 with Sinfo; use Sinfo;
64 with Sinfo.CN; use Sinfo.CN;
65 with Sinput; use Sinput;
66 with Sinput.L; use Sinput.L;
67 with Snames; use Snames;
68 with Stringt; use Stringt;
69 with Uname; use Uname;
70 with Table;
71 with Tbuild; use Tbuild;
72 with Uintp; use Uintp;
73 with Urealp; use Urealp;
75 with GNAT.HTable;
77 package body Sem_Ch12 is
79 ----------------------------------------------------------
80 -- Implementation of Generic Analysis and Instantiation --
81 -----------------------------------------------------------
83 -- GNAT implements generics by macro expansion. No attempt is made to
84 -- share generic instantiations (for now). Analysis of a generic definition
85 -- does not perform any expansion action, but the expander must be called
86 -- on the tree for each instantiation, because the expansion may of course
87 -- depend on the generic actuals. All of this is best achieved as follows:
89 -- a) Semantic analysis of a generic unit is performed on a copy of the
90 -- tree for the generic unit. All tree modifications that follow analysis
91 -- do not affect the original tree. Links are kept between the original
92 -- tree and the copy, in order to recognize non-local references within
93 -- the generic, and propagate them to each instance (recall that name
94 -- resolution is done on the generic declaration: generics are not really
95 -- macros!). This is summarized in the following diagram:
97 -- .-----------. .----------.
98 -- | semantic |<--------------| generic |
99 -- | copy | | unit |
100 -- | |==============>| |
101 -- |___________| global |__________|
102 -- references | | |
103 -- | | |
104 -- .-----|--|.
105 -- | .-----|---.
106 -- | | .----------.
107 -- | | | generic |
108 -- |__| | |
109 -- |__| instance |
110 -- |__________|
112 -- b) Each instantiation copies the original tree, and inserts into it a
113 -- series of declarations that describe the mapping between generic formals
114 -- and actuals. For example, a generic In OUT parameter is an object
115 -- renaming of the corresponing actual, etc. Generic IN parameters are
116 -- constant declarations.
118 -- c) In order to give the right visibility for these renamings, we use
119 -- a different scheme for package and subprogram instantiations. For
120 -- packages, the list of renamings is inserted into the package
121 -- specification, before the visible declarations of the package. The
122 -- renamings are analyzed before any of the text of the instance, and are
123 -- thus visible at the right place. Furthermore, outside of the instance,
124 -- the generic parameters are visible and denote their corresponding
125 -- actuals.
127 -- For subprograms, we create a container package to hold the renamings
128 -- and the subprogram instance itself. Analysis of the package makes the
129 -- renaming declarations visible to the subprogram. After analyzing the
130 -- package, the defining entity for the subprogram is touched-up so that
131 -- it appears declared in the current scope, and not inside the container
132 -- package.
134 -- If the instantiation is a compilation unit, the container package is
135 -- given the same name as the subprogram instance. This ensures that
136 -- the elaboration procedure called by the binder, using the compilation
137 -- unit name, calls in fact the elaboration procedure for the package.
139 -- Not surprisingly, private types complicate this approach. By saving in
140 -- the original generic object the non-local references, we guarantee that
141 -- the proper entities are referenced at the point of instantiation.
142 -- However, for private types, this by itself does not insure that the
143 -- proper VIEW of the entity is used (the full type may be visible at the
144 -- point of generic definition, but not at instantiation, or vice-versa).
145 -- In order to reference the proper view, we special-case any reference
146 -- to private types in the generic object, by saving both views, one in
147 -- the generic and one in the semantic copy. At time of instantiation, we
148 -- check whether the two views are consistent, and exchange declarations if
149 -- necessary, in order to restore the correct visibility. Similarly, if
150 -- the instance view is private when the generic view was not, we perform
151 -- the exchange. After completing the instantiation, we restore the
152 -- current visibility. The flag Has_Private_View marks identifiers in the
153 -- the generic unit that require checking.
155 -- Visibility within nested generic units requires special handling.
156 -- Consider the following scheme:
158 -- type Global is ... -- outside of generic unit.
159 -- generic ...
160 -- package Outer is
161 -- ...
162 -- type Semi_Global is ... -- global to inner.
164 -- generic ... -- 1
165 -- procedure inner (X1 : Global; X2 : Semi_Global);
167 -- procedure in2 is new inner (...); -- 4
168 -- end Outer;
170 -- package New_Outer is new Outer (...); -- 2
171 -- procedure New_Inner is new New_Outer.Inner (...); -- 3
173 -- The semantic analysis of Outer captures all occurrences of Global.
174 -- The semantic analysis of Inner (at 1) captures both occurrences of
175 -- Global and Semi_Global.
177 -- At point 2 (instantiation of Outer), we also produce a generic copy
178 -- of Inner, even though Inner is, at that point, not being instantiated.
179 -- (This is just part of the semantic analysis of New_Outer).
181 -- Critically, references to Global within Inner must be preserved, while
182 -- references to Semi_Global should not preserved, because they must now
183 -- resolve to an entity within New_Outer. To distinguish between these, we
184 -- use a global variable, Current_Instantiated_Parent, which is set when
185 -- performing a generic copy during instantiation (at 2). This variable is
186 -- used when performing a generic copy that is not an instantiation, but
187 -- that is nested within one, as the occurrence of 1 within 2. The analysis
188 -- of a nested generic only preserves references that are global to the
189 -- enclosing Current_Instantiated_Parent. We use the Scope_Depth value to
190 -- determine whether a reference is external to the given parent.
192 -- The instantiation at point 3 requires no special treatment. The method
193 -- works as well for further nestings of generic units, but of course the
194 -- variable Current_Instantiated_Parent must be stacked because nested
195 -- instantiations can occur, e.g. the occurrence of 4 within 2.
197 -- The instantiation of package and subprogram bodies is handled in a
198 -- similar manner, except that it is delayed until after semantic
199 -- analysis is complete. In this fashion complex cross-dependencies
200 -- between several package declarations and bodies containing generics
201 -- can be compiled which otherwise would diagnose spurious circularities.
203 -- For example, it is possible to compile two packages A and B that
204 -- have the following structure:
206 -- package A is package B is
207 -- generic ... generic ...
208 -- package G_A is package G_B is
210 -- with B; with A;
211 -- package body A is package body B is
212 -- package N_B is new G_B (..) package N_A is new G_A (..)
214 -- The table Pending_Instantiations in package Inline is used to keep
215 -- track of body instantiations that are delayed in this manner. Inline
216 -- handles the actual calls to do the body instantiations. This activity
217 -- is part of Inline, since the processing occurs at the same point, and
218 -- for essentially the same reason, as the handling of inlined routines.
220 ----------------------------------------------
221 -- Detection of Instantiation Circularities --
222 ----------------------------------------------
224 -- If we have a chain of instantiations that is circular, this is a
225 -- static error which must be detected at compile time. The detection
226 -- of these circularities is carried out at the point that we insert
227 -- a generic instance spec or body. If there is a circularity, then
228 -- the analysis of the offending spec or body will eventually result
229 -- in trying to load the same unit again, and we detect this problem
230 -- as we analyze the package instantiation for the second time.
232 -- At least in some cases after we have detected the circularity, we
233 -- get into trouble if we try to keep going. The following flag is
234 -- set if a circularity is detected, and used to abandon compilation
235 -- after the messages have been posted.
237 Circularity_Detected : Boolean := False;
238 -- This should really be reset on encountering a new main unit, but in
239 -- practice we are not using multiple main units so it is not critical.
241 -----------------------
242 -- Local subprograms --
243 -----------------------
245 procedure Abandon_Instantiation (N : Node_Id);
246 pragma No_Return (Abandon_Instantiation);
247 -- Posts an error message "instantiation abandoned" at the indicated
248 -- node and then raises the exception Instantiation_Error to do it.
250 procedure Analyze_Formal_Array_Type
251 (T : in out Entity_Id;
252 Def : Node_Id);
253 -- A formal array type is treated like an array type declaration, and
254 -- invokes Array_Type_Declaration (sem_ch3) whose first parameter is
255 -- in-out, because in the case of an anonymous type the entity is
256 -- actually created in the procedure.
258 -- The following procedures treat other kinds of formal parameters
260 procedure Analyze_Formal_Derived_Type
261 (N : Node_Id;
262 T : Entity_Id;
263 Def : Node_Id);
265 -- The following subprograms create abbreviated declarations for formal
266 -- scalar types. We introduce an anonymous base of the proper class for
267 -- each of them, and define the formals as constrained first subtypes of
268 -- their bases. The bounds are expressions that are non-static in the
269 -- generic.
271 procedure Analyze_Formal_Decimal_Fixed_Point_Type
272 (T : Entity_Id; Def : Node_Id);
273 procedure Analyze_Formal_Discrete_Type (T : Entity_Id; Def : Node_Id);
274 procedure Analyze_Formal_Floating_Type (T : Entity_Id; Def : Node_Id);
275 procedure Analyze_Formal_Signed_Integer_Type (T : Entity_Id; Def : Node_Id);
276 procedure Analyze_Formal_Modular_Type (T : Entity_Id; Def : Node_Id);
277 procedure Analyze_Formal_Ordinary_Fixed_Point_Type
278 (T : Entity_Id; Def : Node_Id);
280 procedure Analyze_Formal_Private_Type
281 (N : Node_Id;
282 T : Entity_Id;
283 Def : Node_Id);
284 -- This needs comments???
286 procedure Analyze_Generic_Formal_Part (N : Node_Id);
288 procedure Analyze_Generic_Access_Type (T : Entity_Id; Def : Node_Id);
289 -- This needs comments ???
291 function Analyze_Associations
292 (I_Node : Node_Id;
293 Formals : List_Id;
294 F_Copy : List_Id) return List_Id;
295 -- At instantiation time, build the list of associations between formals
296 -- and actuals. Each association becomes a renaming declaration for the
297 -- formal entity. F_Copy is the analyzed list of formals in the generic
298 -- copy. It is used to apply legality checks to the actuals. I_Node is the
299 -- instantiation node itself.
301 procedure Analyze_Subprogram_Instantiation
302 (N : Node_Id;
303 K : Entity_Kind);
305 procedure Build_Instance_Compilation_Unit_Nodes
306 (N : Node_Id;
307 Act_Body : Node_Id;
308 Act_Decl : Node_Id);
309 -- This procedure is used in the case where the generic instance of a
310 -- subprogram body or package body is a library unit. In this case, the
311 -- original library unit node for the generic instantiation must be
312 -- replaced by the resulting generic body, and a link made to a new
313 -- compilation unit node for the generic declaration. The argument N is
314 -- the original generic instantiation. Act_Body and Act_Decl are the body
315 -- and declaration of the instance (either package body and declaration
316 -- nodes or subprogram body and declaration nodes depending on the case).
317 -- On return, the node N has been rewritten with the actual body.
319 procedure Check_Formal_Packages (P_Id : Entity_Id);
320 -- Apply the following to all formal packages in generic associations
322 procedure Check_Formal_Package_Instance
323 (Formal_Pack : Entity_Id;
324 Actual_Pack : Entity_Id);
325 -- Verify that the actuals of the actual instance match the actuals of
326 -- the template for a formal package that is not declared with a box.
328 procedure Check_Forward_Instantiation (Decl : Node_Id);
329 -- If the generic is a local entity and the corresponding body has not
330 -- been seen yet, flag enclosing packages to indicate that it will be
331 -- elaborated after the generic body. Subprograms declared in the same
332 -- package cannot be inlined by the front-end because front-end inlining
333 -- requires a strict linear order of elaboration.
335 procedure Check_Hidden_Child_Unit
336 (N : Node_Id;
337 Gen_Unit : Entity_Id;
338 Act_Decl_Id : Entity_Id);
339 -- If the generic unit is an implicit child instance within a parent
340 -- instance, we need to make an explicit test that it is not hidden by
341 -- a child instance of the same name and parent.
343 procedure Check_Private_View (N : Node_Id);
344 -- Check whether the type of a generic entity has a different view between
345 -- the point of generic analysis and the point of instantiation. If the
346 -- view has changed, then at the point of instantiation we restore the
347 -- correct view to perform semantic analysis of the instance, and reset
348 -- the current view after instantiation. The processing is driven by the
349 -- current private status of the type of the node, and Has_Private_View,
350 -- a flag that is set at the point of generic compilation. If view and
351 -- flag are inconsistent then the type is updated appropriately.
353 procedure Check_Generic_Actuals
354 (Instance : Entity_Id;
355 Is_Formal_Box : Boolean);
356 -- Similar to previous one. Check the actuals in the instantiation,
357 -- whose views can change between the point of instantiation and the point
358 -- of instantiation of the body. In addition, mark the generic renamings
359 -- as generic actuals, so that they are not compatible with other actuals.
360 -- Recurse on an actual that is a formal package whose declaration has
361 -- a box.
363 function Contains_Instance_Of
364 (Inner : Entity_Id;
365 Outer : Entity_Id;
366 N : Node_Id) return Boolean;
367 -- Inner is instantiated within the generic Outer. Check whether Inner
368 -- directly or indirectly contains an instance of Outer or of one of its
369 -- parents, in the case of a subunit. Each generic unit holds a list of
370 -- the entities instantiated within (at any depth). This procedure
371 -- determines whether the set of such lists contains a cycle, i.e. an
372 -- illegal circular instantiation.
374 function Denotes_Formal_Package
375 (Pack : Entity_Id;
376 On_Exit : Boolean := False) return Boolean;
377 -- Returns True if E is a formal package of an enclosing generic, or
378 -- the actual for such a formal in an enclosing instantiation. If such
379 -- a package is used as a formal in an nested generic, or as an actual
380 -- in a nested instantiation, the visibility of ITS formals should not
381 -- be modified. When called from within Restore_Private_Views, the flag
382 -- On_Exit is true, to indicate that the search for a possible enclosing
383 -- instance should ignore the current one.
385 function Find_Actual_Type
386 (Typ : Entity_Id;
387 Gen_Scope : Entity_Id) return Entity_Id;
388 -- When validating the actual types of a child instance, check whether
389 -- the formal is a formal type of the parent unit, and retrieve the current
390 -- actual for it. Typ is the entity in the analyzed formal type declaration
391 -- (component or index type of an array type) and Gen_Scope is the scope of
392 -- the analyzed formal array type.
394 function Get_Package_Instantiation_Node (A : Entity_Id) return Node_Id;
395 -- Given the entity of a unit that is an instantiation, retrieve the
396 -- original instance node. This is used when loading the instantiations
397 -- of the ancestors of a child generic that is being instantiated.
399 function In_Same_Declarative_Part
400 (F_Node : Node_Id;
401 Inst : Node_Id) return Boolean;
402 -- True if the instantiation Inst and the given freeze_node F_Node appear
403 -- within the same declarative part, ignoring subunits, but with no inter-
404 -- vening suprograms or concurrent units. If true, the freeze node
405 -- of the instance can be placed after the freeze node of the parent,
406 -- which it itself an instance.
408 function In_Main_Context (E : Entity_Id) return Boolean;
409 -- Check whether an instantiation is in the context of the main unit.
410 -- Used to determine whether its body should be elaborated to allow
411 -- front-end inlining.
413 procedure Set_Instance_Env
414 (Gen_Unit : Entity_Id;
415 Act_Unit : Entity_Id);
416 -- Save current instance on saved environment, to be used to determine
417 -- the global status of entities in nested instances. Part of Save_Env.
418 -- called after verifying that the generic unit is legal for the instance.
420 procedure Set_Instance_Of (A : Entity_Id; B : Entity_Id);
421 -- Associate analyzed generic parameter with corresponding
422 -- instance. Used for semantic checks at instantiation time.
424 function Has_Been_Exchanged (E : Entity_Id) return Boolean;
425 -- Traverse the Exchanged_Views list to see if a type was private
426 -- and has already been flipped during this phase of instantiation.
428 procedure Hide_Current_Scope;
429 -- When compiling a generic child unit, the parent context must be
430 -- present, but the instance and all entities that may be generated
431 -- must be inserted in the current scope. We leave the current scope
432 -- on the stack, but make its entities invisible to avoid visibility
433 -- problems. This is reversed at the end of instantiations. This is
434 -- not done for the instantiation of the bodies, which only require the
435 -- instances of the generic parents to be in scope.
437 procedure Install_Body
438 (Act_Body : Node_Id;
439 N : Node_Id;
440 Gen_Body : Node_Id;
441 Gen_Decl : Node_Id);
442 -- If the instantiation happens textually before the body of the generic,
443 -- the instantiation of the body must be analyzed after the generic body,
444 -- and not at the point of instantiation. Such early instantiations can
445 -- happen if the generic and the instance appear in a package declaration
446 -- because the generic body can only appear in the corresponding package
447 -- body. Early instantiations can also appear if generic, instance and
448 -- body are all in the declarative part of a subprogram or entry. Entities
449 -- of packages that are early instantiations are delayed, and their freeze
450 -- node appears after the generic body.
452 procedure Insert_After_Last_Decl (N : Node_Id; F_Node : Node_Id);
453 -- Insert freeze node at the end of the declarative part that includes the
454 -- instance node N. If N is in the visible part of an enclosing package
455 -- declaration, the freeze node has to be inserted at the end of the
456 -- private declarations, if any.
458 procedure Freeze_Subprogram_Body
459 (Inst_Node : Node_Id;
460 Gen_Body : Node_Id;
461 Pack_Id : Entity_Id);
462 -- The generic body may appear textually after the instance, including
463 -- in the proper body of a stub, or within a different package instance.
464 -- Given that the instance can only be elaborated after the generic, we
465 -- place freeze_nodes for the instance and/or for packages that may enclose
466 -- the instance and the generic, so that the back-end can establish the
467 -- proper order of elaboration.
469 procedure Init_Env;
470 -- Establish environment for subsequent instantiation. Separated from
471 -- Save_Env because data-structures for visibility handling must be
472 -- initialized before call to Check_Generic_Child_Unit.
474 procedure Install_Parent (P : Entity_Id; In_Body : Boolean := False);
475 -- When compiling an instance of a child unit the parent (which is
476 -- itself an instance) is an enclosing scope that must be made
477 -- immediately visible. This procedure is also used to install the non-
478 -- generic parent of a generic child unit when compiling its body, so
479 -- that full views of types in the parent are made visible.
481 procedure Remove_Parent (In_Body : Boolean := False);
482 -- Reverse effect after instantiation of child is complete
484 procedure Inline_Instance_Body
485 (N : Node_Id;
486 Gen_Unit : Entity_Id;
487 Act_Decl : Node_Id);
488 -- If front-end inlining is requested, instantiate the package body,
489 -- and preserve the visibility of its compilation unit, to insure
490 -- that successive instantiations succeed.
492 -- The functions Instantiate_XXX perform various legality checks and build
493 -- the declarations for instantiated generic parameters. In all of these
494 -- Formal is the entity in the generic unit, Actual is the entity of
495 -- expression in the generic associations, and Analyzed_Formal is the
496 -- formal in the generic copy, which contains the semantic information to
497 -- be used to validate the actual.
499 function Instantiate_Object
500 (Formal : Node_Id;
501 Actual : Node_Id;
502 Analyzed_Formal : Node_Id) return List_Id;
504 function Instantiate_Type
505 (Formal : Node_Id;
506 Actual : Node_Id;
507 Analyzed_Formal : Node_Id;
508 Actual_Decls : List_Id) return Node_Id;
510 function Instantiate_Formal_Subprogram
511 (Formal : Node_Id;
512 Actual : Node_Id;
513 Analyzed_Formal : Node_Id) return Node_Id;
515 function Instantiate_Formal_Package
516 (Formal : Node_Id;
517 Actual : Node_Id;
518 Analyzed_Formal : Node_Id) return List_Id;
519 -- If the formal package is declared with a box, special visibility rules
520 -- apply to its formals: they are in the visible part of the package. This
521 -- is true in the declarative region of the formal package, that is to say
522 -- in the enclosing generic or instantiation. For an instantiation, the
523 -- parameters of the formal package are made visible in an explicit step.
524 -- Furthermore, if the actual is a visible use_clause, these formals must
525 -- be made potentially use_visible as well. On exit from the enclosing
526 -- instantiation, the reverse must be done.
528 -- For a formal package declared without a box, there are conformance rules
529 -- that apply to the actuals in the generic declaration and the actuals of
530 -- the actual package in the enclosing instantiation. The simplest way to
531 -- apply these rules is to repeat the instantiation of the formal package
532 -- in the context of the enclosing instance, and compare the generic
533 -- associations of this instantiation with those of the actual package.
535 function Is_In_Main_Unit (N : Node_Id) return Boolean;
536 -- Test if given node is in the main unit
538 procedure Load_Parent_Of_Generic (N : Node_Id; Spec : Node_Id);
539 -- If the generic appears in a separate non-generic library unit,
540 -- load the corresponding body to retrieve the body of the generic.
541 -- N is the node for the generic instantiation, Spec is the generic
542 -- package declaration.
544 procedure Inherit_Context (Gen_Decl : Node_Id; Inst : Node_Id);
545 -- Add the context clause of the unit containing a generic unit to
546 -- an instantiation that is a compilation unit.
548 function Get_Associated_Node (N : Node_Id) return Node_Id;
549 -- In order to propagate semantic information back from the analyzed
550 -- copy to the original generic, we maintain links between selected nodes
551 -- in the generic and their corresponding copies. At the end of generic
552 -- analysis, the routine Save_Global_References traverses the generic
553 -- tree, examines the semantic information, and preserves the links to
554 -- those nodes that contain global information. At instantiation, the
555 -- information from the associated node is placed on the new copy, so
556 -- that name resolution is not repeated.
558 -- Three kinds of source nodes have associated nodes:
560 -- a) those that can reference (denote) entities, that is identifiers,
561 -- character literals, expanded_names, operator symbols, operators,
562 -- and attribute reference nodes. These nodes have an Entity field
563 -- and are the set of nodes that are in N_Has_Entity.
565 -- b) aggregates (N_Aggregate and N_Extension_Aggregate)
567 -- c) selected components (N_Selected_Component)
569 -- For the first class, the associated node preserves the entity if it is
570 -- global. If the generic contains nested instantiations, the associated
571 -- node itself has been recopied, and a chain of them must be followed.
573 -- For aggregates, the associated node allows retrieval of the type, which
574 -- may otherwise not appear in the generic. The view of this type may be
575 -- different between generic and instantiation, and the full view can be
576 -- installed before the instantiation is analyzed. For aggregates of
577 -- type extensions, the same view exchange may have to be performed for
578 -- some of the ancestor types, if their view is private at the point of
579 -- instantiation.
581 -- Nodes that are selected components in the parse tree may be rewritten
582 -- as expanded names after resolution, and must be treated as potential
583 -- entity holders. which is why they also have an Associated_Node.
585 -- Nodes that do not come from source, such as freeze nodes, do not appear
586 -- in the generic tree, and need not have an associated node.
588 -- The associated node is stored in the Associated_Node field. Note that
589 -- this field overlaps Entity, which is fine, because the whole point is
590 -- that we don't need or want the normal Entity field in this situation.
592 procedure Move_Freeze_Nodes
593 (Out_Of : Entity_Id;
594 After : Node_Id;
595 L : List_Id);
596 -- Freeze nodes can be generated in the analysis of a generic unit, but
597 -- will not be seen by the back-end. It is necessary to move those nodes
598 -- to the enclosing scope if they freeze an outer entity. We place them
599 -- at the end of the enclosing generic package, which is semantically
600 -- neutral.
602 procedure Pre_Analyze_Actuals (N : Node_Id);
603 -- Analyze actuals to perform name resolution. Full resolution is done
604 -- later, when the expected types are known, but names have to be captured
605 -- before installing parents of generics, that are not visible for the
606 -- actuals themselves.
608 procedure Valid_Default_Attribute (Nam : Entity_Id; Def : Node_Id);
609 -- Verify that an attribute that appears as the default for a formal
610 -- subprogram is a function or procedure with the correct profile.
612 -------------------------------------------
613 -- Data Structures for Generic Renamings --
614 -------------------------------------------
616 -- The map Generic_Renamings associates generic entities with their
617 -- corresponding actuals. Currently used to validate type instances.
618 -- It will eventually be used for all generic parameters to eliminate
619 -- the need for overload resolution in the instance.
621 type Assoc_Ptr is new Int;
623 Assoc_Null : constant Assoc_Ptr := -1;
625 type Assoc is record
626 Gen_Id : Entity_Id;
627 Act_Id : Entity_Id;
628 Next_In_HTable : Assoc_Ptr;
629 end record;
631 package Generic_Renamings is new Table.Table
632 (Table_Component_Type => Assoc,
633 Table_Index_Type => Assoc_Ptr,
634 Table_Low_Bound => 0,
635 Table_Initial => 10,
636 Table_Increment => 100,
637 Table_Name => "Generic_Renamings");
639 -- Variable to hold enclosing instantiation. When the environment is
640 -- saved for a subprogram inlining, the corresponding Act_Id is empty.
642 Current_Instantiated_Parent : Assoc := (Empty, Empty, Assoc_Null);
644 -- Hash table for associations
646 HTable_Size : constant := 37;
647 type HTable_Range is range 0 .. HTable_Size - 1;
649 procedure Set_Next_Assoc (E : Assoc_Ptr; Next : Assoc_Ptr);
650 function Next_Assoc (E : Assoc_Ptr) return Assoc_Ptr;
651 function Get_Gen_Id (E : Assoc_Ptr) return Entity_Id;
652 function Hash (F : Entity_Id) return HTable_Range;
654 package Generic_Renamings_HTable is new GNAT.HTable.Static_HTable (
655 Header_Num => HTable_Range,
656 Element => Assoc,
657 Elmt_Ptr => Assoc_Ptr,
658 Null_Ptr => Assoc_Null,
659 Set_Next => Set_Next_Assoc,
660 Next => Next_Assoc,
661 Key => Entity_Id,
662 Get_Key => Get_Gen_Id,
663 Hash => Hash,
664 Equal => "=");
666 Exchanged_Views : Elist_Id;
667 -- This list holds the private views that have been exchanged during
668 -- instantiation to restore the visibility of the generic declaration.
669 -- (see comments above). After instantiation, the current visibility is
670 -- reestablished by means of a traversal of this list.
672 Hidden_Entities : Elist_Id;
673 -- This list holds the entities of the current scope that are removed
674 -- from immediate visibility when instantiating a child unit. Their
675 -- visibility is restored in Remove_Parent.
677 -- Because instantiations can be recursive, the following must be saved
678 -- on entry and restored on exit from an instantiation (spec or body).
679 -- This is done by the two procedures Save_Env and Restore_Env. For
680 -- package and subprogram instantiations (but not for the body instances)
681 -- the action of Save_Env is done in two steps: Init_Env is called before
682 -- Check_Generic_Child_Unit, because setting the parent instances requires
683 -- that the visibility data structures be properly initialized. Once the
684 -- generic is unit is validated, Set_Instance_Env completes Save_Env.
686 Parent_Unit_Visible : Boolean := False;
687 -- Parent_Unit_Visible is used when the generic is a child unit, and
688 -- indicates whether the ultimate parent of the generic is visible in the
689 -- instantiation environment. It is used to reset the visiblity of the
690 -- parent at the end of the instantiation (see Remove_Parent).
692 type Instance_Env is record
693 Ada_Version : 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 New_N :=
1700 Copy_Generic_Node
1701 (Original_Node (Gen_Decl), Empty, Instantiating => True);
1702 Rewrite (N, New_N);
1703 Set_Defining_Unit_Name (Specification (New_N), Formal);
1704 Set_Instance_Env (Gen_Unit, Formal);
1706 Enter_Name (Formal);
1707 Set_Ekind (Formal, E_Generic_Package);
1708 Set_Etype (Formal, Standard_Void_Type);
1709 Set_Inner_Instances (Formal, New_Elmt_List);
1710 New_Scope (Formal);
1712 -- Within the formal, the name of the generic package is a renaming
1713 -- of the formal (as for a regular instantiation).
1715 Renaming := Make_Package_Renaming_Declaration (Loc,
1716 Defining_Unit_Name =>
1717 Make_Defining_Identifier (Loc, Chars (Gen_Unit)),
1718 Name => New_Reference_To (Formal, Loc));
1720 if Present (Visible_Declarations (Specification (N))) then
1721 Prepend (Renaming, To => Visible_Declarations (Specification (N)));
1722 elsif Present (Private_Declarations (Specification (N))) then
1723 Prepend (Renaming, To => Private_Declarations (Specification (N)));
1724 end if;
1726 if Is_Child_Unit (Gen_Unit)
1727 and then Parent_Installed
1728 then
1729 -- Similarly, we have to make the name of the formal visible in
1730 -- the parent instance, to resolve properly fully qualified names
1731 -- that may appear in the generic unit. The parent instance has
1732 -- been placed on the scope stack ahead of the current scope.
1734 Parent_Instance := Scope_Stack.Table (Scope_Stack.Last - 1).Entity;
1736 Renaming_In_Par :=
1737 Make_Defining_Identifier (Loc, Chars (Gen_Unit));
1738 Set_Ekind (Renaming_In_Par, E_Package);
1739 Set_Etype (Renaming_In_Par, Standard_Void_Type);
1740 Set_Scope (Renaming_In_Par, Parent_Instance);
1741 Set_Parent (Renaming_In_Par, Parent (Formal));
1742 Set_Renamed_Object (Renaming_In_Par, Formal);
1743 Append_Entity (Renaming_In_Par, Parent_Instance);
1744 end if;
1746 Analyze_Generic_Formal_Part (N);
1747 Analyze (Specification (N));
1748 End_Package_Scope (Formal);
1750 if Parent_Installed then
1751 Remove_Parent;
1752 end if;
1754 Restore_Env;
1756 -- Inside the generic unit, the formal package is a regular
1757 -- package, but no body is needed for it. Note that after
1758 -- instantiation, the defining_unit_name we need is in the
1759 -- new tree and not in the original. (see Package_Instantiation).
1760 -- A generic formal package is an instance, and can be used as
1761 -- an actual for an inner instance. Mark its generic parent.
1763 Set_Ekind (Formal, E_Package);
1764 Set_Generic_Parent (Specification (N), Gen_Unit);
1765 Set_Has_Completion (Formal, True);
1767 Set_Ekind (Pack_Id, E_Package);
1768 Set_Etype (Pack_Id, Standard_Void_Type);
1769 Set_Scope (Pack_Id, Scope (Formal));
1770 Set_Has_Completion (Pack_Id, True);
1771 end if;
1772 end Analyze_Formal_Package;
1774 ---------------------------------
1775 -- Analyze_Formal_Private_Type --
1776 ---------------------------------
1778 procedure Analyze_Formal_Private_Type
1779 (N : Node_Id;
1780 T : Entity_Id;
1781 Def : Node_Id)
1783 begin
1784 New_Private_Type (N, T, Def);
1786 -- Set the size to an arbitrary but legal value
1788 Set_Size_Info (T, Standard_Integer);
1789 Set_RM_Size (T, RM_Size (Standard_Integer));
1790 end Analyze_Formal_Private_Type;
1792 ----------------------------------------
1793 -- Analyze_Formal_Signed_Integer_Type --
1794 ----------------------------------------
1796 procedure Analyze_Formal_Signed_Integer_Type
1797 (T : Entity_Id;
1798 Def : Node_Id)
1800 Base : constant Entity_Id :=
1801 New_Internal_Entity
1802 (E_Signed_Integer_Type, Current_Scope, Sloc (Def), 'G');
1804 begin
1805 Enter_Name (T);
1807 Set_Ekind (T, E_Signed_Integer_Subtype);
1808 Set_Etype (T, Base);
1809 Set_Size_Info (T, Standard_Integer);
1810 Set_RM_Size (T, RM_Size (Standard_Integer));
1811 Set_Scalar_Range (T, Scalar_Range (Standard_Integer));
1812 Set_Is_Constrained (T);
1814 Set_Is_Generic_Type (Base);
1815 Set_Size_Info (Base, Standard_Integer);
1816 Set_RM_Size (Base, RM_Size (Standard_Integer));
1817 Set_Etype (Base, Base);
1818 Set_Scalar_Range (Base, Scalar_Range (Standard_Integer));
1819 Set_Parent (Base, Parent (Def));
1820 end Analyze_Formal_Signed_Integer_Type;
1822 -------------------------------
1823 -- Analyze_Formal_Subprogram --
1824 -------------------------------
1826 procedure Analyze_Formal_Subprogram (N : Node_Id) is
1827 Spec : constant Node_Id := Specification (N);
1828 Def : constant Node_Id := Default_Name (N);
1829 Nam : constant Entity_Id := Defining_Unit_Name (Spec);
1830 Subp : Entity_Id;
1832 begin
1833 if Nam = Error then
1834 return;
1835 end if;
1837 if Nkind (Nam) = N_Defining_Program_Unit_Name then
1838 Error_Msg_N ("name of formal subprogram must be a direct name", Nam);
1839 return;
1840 end if;
1842 Analyze_Subprogram_Declaration (N);
1843 Set_Is_Formal_Subprogram (Nam);
1844 Set_Has_Completion (Nam);
1846 if Nkind (N) = N_Formal_Abstract_Subprogram_Declaration then
1847 Set_Is_Abstract (Nam);
1848 Set_Is_Dispatching_Operation (Nam);
1850 declare
1851 Ctrl_Type : constant Entity_Id := Find_Dispatching_Type (Nam);
1853 begin
1854 if not Present (Ctrl_Type) then
1855 Error_Msg_N
1856 ("abstract formal subprogram must have a controlling type",
1859 else
1860 Check_Controlling_Formals (Ctrl_Type, Nam);
1861 end if;
1862 end;
1863 end if;
1865 -- Default name is resolved at the point of instantiation
1867 if Box_Present (N) then
1868 null;
1870 -- Else default is bound at the point of generic declaration
1872 elsif Present (Def) then
1873 if Nkind (Def) = N_Operator_Symbol then
1874 Find_Direct_Name (Def);
1876 elsif Nkind (Def) /= N_Attribute_Reference then
1877 Analyze (Def);
1879 else
1880 -- For an attribute reference, analyze the prefix and verify
1881 -- that it has the proper profile for the subprogram.
1883 Analyze (Prefix (Def));
1884 Valid_Default_Attribute (Nam, Def);
1885 return;
1886 end if;
1888 -- Default name may be overloaded, in which case the interpretation
1889 -- with the correct profile must be selected, as for a renaming.
1891 if Etype (Def) = Any_Type then
1892 return;
1894 elsif Nkind (Def) = N_Selected_Component then
1895 Subp := Entity (Selector_Name (Def));
1897 if Ekind (Subp) /= E_Entry then
1898 Error_Msg_N ("expect valid subprogram name as default", Def);
1899 return;
1900 end if;
1902 elsif Nkind (Def) = N_Indexed_Component then
1904 if Nkind (Prefix (Def)) /= N_Selected_Component then
1905 Error_Msg_N ("expect valid subprogram name as default", Def);
1906 return;
1908 else
1909 Subp := Entity (Selector_Name (Prefix (Def)));
1911 if Ekind (Subp) /= E_Entry_Family then
1912 Error_Msg_N ("expect valid subprogram name as default", Def);
1913 return;
1914 end if;
1915 end if;
1917 elsif Nkind (Def) = N_Character_Literal then
1919 -- Needs some type checks: subprogram should be parameterless???
1921 Resolve (Def, (Etype (Nam)));
1923 elsif not Is_Entity_Name (Def)
1924 or else not Is_Overloadable (Entity (Def))
1925 then
1926 Error_Msg_N ("expect valid subprogram name as default", Def);
1927 return;
1929 elsif not Is_Overloaded (Def) then
1930 Subp := Entity (Def);
1932 if Subp = Nam then
1933 Error_Msg_N ("premature usage of formal subprogram", Def);
1935 elsif not Entity_Matches_Spec (Subp, Nam) then
1936 Error_Msg_N ("no visible entity matches specification", Def);
1937 end if;
1939 else
1940 declare
1941 I : Interp_Index;
1942 I1 : Interp_Index := 0;
1943 It : Interp;
1944 It1 : Interp;
1946 begin
1947 Subp := Any_Id;
1948 Get_First_Interp (Def, I, It);
1949 while Present (It.Nam) loop
1951 if Entity_Matches_Spec (It.Nam, Nam) then
1952 if Subp /= Any_Id then
1953 It1 := Disambiguate (Def, I1, I, Etype (Subp));
1955 if It1 = No_Interp then
1956 Error_Msg_N ("ambiguous default subprogram", Def);
1957 else
1958 Subp := It1.Nam;
1959 end if;
1961 exit;
1963 else
1964 I1 := I;
1965 Subp := It.Nam;
1966 end if;
1967 end if;
1969 Get_Next_Interp (I, It);
1970 end loop;
1971 end;
1973 if Subp /= Any_Id then
1974 Set_Entity (Def, Subp);
1976 if Subp = Nam then
1977 Error_Msg_N ("premature usage of formal subprogram", Def);
1979 elsif Ekind (Subp) /= E_Operator then
1980 Check_Mode_Conformant (Subp, Nam);
1981 end if;
1983 else
1984 Error_Msg_N ("no visible subprogram matches specification", N);
1985 end if;
1986 end if;
1987 end if;
1988 end Analyze_Formal_Subprogram;
1990 -------------------------------------
1991 -- Analyze_Formal_Type_Declaration --
1992 -------------------------------------
1994 procedure Analyze_Formal_Type_Declaration (N : Node_Id) is
1995 Def : constant Node_Id := Formal_Type_Definition (N);
1996 T : Entity_Id;
1998 begin
1999 T := Defining_Identifier (N);
2001 if Present (Discriminant_Specifications (N))
2002 and then Nkind (Def) /= N_Formal_Private_Type_Definition
2003 then
2004 Error_Msg_N
2005 ("discriminants not allowed for this formal type",
2006 Defining_Identifier (First (Discriminant_Specifications (N))));
2007 end if;
2009 -- Enter the new name, and branch to specific routine
2011 case Nkind (Def) is
2012 when N_Formal_Private_Type_Definition =>
2013 Analyze_Formal_Private_Type (N, T, Def);
2015 when N_Formal_Derived_Type_Definition =>
2016 Analyze_Formal_Derived_Type (N, T, Def);
2018 when N_Formal_Discrete_Type_Definition =>
2019 Analyze_Formal_Discrete_Type (T, Def);
2021 when N_Formal_Signed_Integer_Type_Definition =>
2022 Analyze_Formal_Signed_Integer_Type (T, Def);
2024 when N_Formal_Modular_Type_Definition =>
2025 Analyze_Formal_Modular_Type (T, Def);
2027 when N_Formal_Floating_Point_Definition =>
2028 Analyze_Formal_Floating_Type (T, Def);
2030 when N_Formal_Ordinary_Fixed_Point_Definition =>
2031 Analyze_Formal_Ordinary_Fixed_Point_Type (T, Def);
2033 when N_Formal_Decimal_Fixed_Point_Definition =>
2034 Analyze_Formal_Decimal_Fixed_Point_Type (T, Def);
2036 when N_Array_Type_Definition =>
2037 Analyze_Formal_Array_Type (T, Def);
2039 when N_Access_To_Object_Definition |
2040 N_Access_Function_Definition |
2041 N_Access_Procedure_Definition =>
2042 Analyze_Generic_Access_Type (T, Def);
2044 when N_Error =>
2045 null;
2047 when others =>
2048 raise Program_Error;
2050 end case;
2052 Set_Is_Generic_Type (T);
2053 end Analyze_Formal_Type_Declaration;
2055 ------------------------------------
2056 -- Analyze_Function_Instantiation --
2057 ------------------------------------
2059 procedure Analyze_Function_Instantiation (N : Node_Id) is
2060 begin
2061 Analyze_Subprogram_Instantiation (N, E_Function);
2062 end Analyze_Function_Instantiation;
2064 ---------------------------------
2065 -- Analyze_Generic_Access_Type --
2066 ---------------------------------
2068 procedure Analyze_Generic_Access_Type (T : Entity_Id; Def : Node_Id) is
2069 begin
2070 Enter_Name (T);
2072 if Nkind (Def) = N_Access_To_Object_Definition then
2073 Access_Type_Declaration (T, Def);
2075 if Is_Incomplete_Or_Private_Type (Designated_Type (T))
2076 and then No (Full_View (Designated_Type (T)))
2077 and then not Is_Generic_Type (Designated_Type (T))
2078 then
2079 Error_Msg_N ("premature usage of incomplete type", Def);
2081 elsif Is_Internal (Designated_Type (T)) then
2082 Error_Msg_N
2083 ("only a subtype mark is allowed in a formal", Def);
2084 end if;
2086 else
2087 Access_Subprogram_Declaration (T, Def);
2088 end if;
2089 end Analyze_Generic_Access_Type;
2091 ---------------------------------
2092 -- Analyze_Generic_Formal_Part --
2093 ---------------------------------
2095 procedure Analyze_Generic_Formal_Part (N : Node_Id) is
2096 Gen_Parm_Decl : Node_Id;
2098 begin
2099 -- The generic formals are processed in the scope of the generic
2100 -- unit, where they are immediately visible. The scope is installed
2101 -- by the caller.
2103 Gen_Parm_Decl := First (Generic_Formal_Declarations (N));
2105 while Present (Gen_Parm_Decl) loop
2106 Analyze (Gen_Parm_Decl);
2107 Next (Gen_Parm_Decl);
2108 end loop;
2110 Generate_Reference_To_Generic_Formals (Current_Scope);
2111 end Analyze_Generic_Formal_Part;
2113 ------------------------------------------
2114 -- Analyze_Generic_Package_Declaration --
2115 ------------------------------------------
2117 procedure Analyze_Generic_Package_Declaration (N : Node_Id) is
2118 Loc : constant Source_Ptr := Sloc (N);
2119 Id : Entity_Id;
2120 New_N : Node_Id;
2121 Save_Parent : Node_Id;
2122 Renaming : Node_Id;
2123 Decls : constant List_Id :=
2124 Visible_Declarations (Specification (N));
2125 Decl : Node_Id;
2127 begin
2128 -- We introduce a renaming of the enclosing package, to have a usable
2129 -- entity as the prefix of an expanded name for a local entity of the
2130 -- form Par.P.Q, where P is the generic package. This is because a local
2131 -- entity named P may hide it, so that the usual visibility rules in
2132 -- the instance will not resolve properly.
2134 Renaming :=
2135 Make_Package_Renaming_Declaration (Loc,
2136 Defining_Unit_Name =>
2137 Make_Defining_Identifier (Loc,
2138 Chars => New_External_Name (Chars (Defining_Entity (N)), "GH")),
2139 Name => Make_Identifier (Loc, Chars (Defining_Entity (N))));
2141 if Present (Decls) then
2142 Decl := First (Decls);
2143 while Present (Decl)
2144 and then Nkind (Decl) = N_Pragma
2145 loop
2146 Next (Decl);
2147 end loop;
2149 if Present (Decl) then
2150 Insert_Before (Decl, Renaming);
2151 else
2152 Append (Renaming, Visible_Declarations (Specification (N)));
2153 end if;
2155 else
2156 Set_Visible_Declarations (Specification (N), New_List (Renaming));
2157 end if;
2159 -- Create copy of generic unit, and save for instantiation.
2160 -- If the unit is a child unit, do not copy the specifications
2161 -- for the parent, which are not part of the generic tree.
2163 Save_Parent := Parent_Spec (N);
2164 Set_Parent_Spec (N, Empty);
2166 New_N := Copy_Generic_Node (N, Empty, Instantiating => False);
2167 Set_Parent_Spec (New_N, Save_Parent);
2168 Rewrite (N, New_N);
2169 Id := Defining_Entity (N);
2170 Generate_Definition (Id);
2172 -- Expansion is not applied to generic units
2174 Start_Generic;
2176 Enter_Name (Id);
2177 Set_Ekind (Id, E_Generic_Package);
2178 Set_Etype (Id, Standard_Void_Type);
2179 New_Scope (Id);
2180 Enter_Generic_Scope (Id);
2181 Set_Inner_Instances (Id, New_Elmt_List);
2183 Set_Categorization_From_Pragmas (N);
2184 Set_Is_Pure (Id, Is_Pure (Current_Scope));
2186 -- Link the declaration of the generic homonym in the generic copy
2187 -- to the package it renames, so that it is always resolved properly.
2189 Set_Generic_Homonym (Id, Defining_Unit_Name (Renaming));
2190 Set_Entity (Associated_Node (Name (Renaming)), Id);
2192 -- For a library unit, we have reconstructed the entity for the
2193 -- unit, and must reset it in the library tables.
2195 if Nkind (Parent (N)) = N_Compilation_Unit then
2196 Set_Cunit_Entity (Current_Sem_Unit, Id);
2197 end if;
2199 Analyze_Generic_Formal_Part (N);
2201 -- After processing the generic formals, analysis proceeds
2202 -- as for a non-generic package.
2204 Analyze (Specification (N));
2206 Validate_Categorization_Dependency (N, Id);
2208 End_Generic;
2210 End_Package_Scope (Id);
2211 Exit_Generic_Scope (Id);
2213 if Nkind (Parent (N)) /= N_Compilation_Unit then
2214 Move_Freeze_Nodes (Id, N, Visible_Declarations (Specification (N)));
2215 Move_Freeze_Nodes (Id, N, Private_Declarations (Specification (N)));
2216 Move_Freeze_Nodes (Id, N, Generic_Formal_Declarations (N));
2218 else
2219 Set_Body_Required (Parent (N), Unit_Requires_Body (Id));
2220 Validate_RT_RAT_Component (N);
2222 -- If this is a spec without a body, check that generic parameters
2223 -- are referenced.
2225 if not Body_Required (Parent (N)) then
2226 Check_References (Id);
2227 end if;
2228 end if;
2229 end Analyze_Generic_Package_Declaration;
2231 --------------------------------------------
2232 -- Analyze_Generic_Subprogram_Declaration --
2233 --------------------------------------------
2235 procedure Analyze_Generic_Subprogram_Declaration (N : Node_Id) is
2236 Spec : Node_Id;
2237 Id : Entity_Id;
2238 Formals : List_Id;
2239 New_N : Node_Id;
2240 Save_Parent : Node_Id;
2242 begin
2243 -- Create copy of generic unit,and save for instantiation.
2244 -- If the unit is a child unit, do not copy the specifications
2245 -- for the parent, which are not part of the generic tree.
2247 Save_Parent := Parent_Spec (N);
2248 Set_Parent_Spec (N, Empty);
2250 New_N := Copy_Generic_Node (N, Empty, Instantiating => False);
2251 Set_Parent_Spec (New_N, Save_Parent);
2252 Rewrite (N, New_N);
2254 Spec := Specification (N);
2255 Id := Defining_Entity (Spec);
2256 Generate_Definition (Id);
2258 if Nkind (Id) = N_Defining_Operator_Symbol then
2259 Error_Msg_N
2260 ("operator symbol not allowed for generic subprogram", Id);
2261 end if;
2263 Start_Generic;
2265 Enter_Name (Id);
2267 Set_Scope_Depth_Value (Id, Scope_Depth (Current_Scope) + 1);
2268 New_Scope (Id);
2269 Enter_Generic_Scope (Id);
2270 Set_Inner_Instances (Id, New_Elmt_List);
2271 Set_Is_Pure (Id, Is_Pure (Current_Scope));
2273 Analyze_Generic_Formal_Part (N);
2275 Formals := Parameter_Specifications (Spec);
2277 if Present (Formals) then
2278 Process_Formals (Formals, Spec);
2279 end if;
2281 if Nkind (Spec) = N_Function_Specification then
2282 Set_Ekind (Id, E_Generic_Function);
2283 Find_Type (Subtype_Mark (Spec));
2284 Set_Etype (Id, Entity (Subtype_Mark (Spec)));
2285 else
2286 Set_Ekind (Id, E_Generic_Procedure);
2287 Set_Etype (Id, Standard_Void_Type);
2288 end if;
2290 -- For a library unit, we have reconstructed the entity for the
2291 -- unit, and must reset it in the library tables. We also need
2292 -- to make sure that Body_Required is set properly in the original
2293 -- compilation unit node.
2295 if Nkind (Parent (N)) = N_Compilation_Unit then
2296 Set_Cunit_Entity (Current_Sem_Unit, Id);
2297 Set_Body_Required (Parent (N), Unit_Requires_Body (Id));
2298 end if;
2300 Set_Categorization_From_Pragmas (N);
2301 Validate_Categorization_Dependency (N, Id);
2303 Save_Global_References (Original_Node (N));
2305 End_Generic;
2306 End_Scope;
2307 Exit_Generic_Scope (Id);
2308 Generate_Reference_To_Formals (Id);
2309 end Analyze_Generic_Subprogram_Declaration;
2311 -----------------------------------
2312 -- Analyze_Package_Instantiation --
2313 -----------------------------------
2315 -- Note: this procedure is also used for formal package declarations,
2316 -- in which case the argument N is an N_Formal_Package_Declaration
2317 -- node. This should really be noted in the spec! ???
2319 procedure Analyze_Package_Instantiation (N : Node_Id) is
2320 Loc : constant Source_Ptr := Sloc (N);
2321 Gen_Id : constant Node_Id := Name (N);
2323 Act_Decl : Node_Id;
2324 Act_Decl_Name : Node_Id;
2325 Act_Decl_Id : Entity_Id;
2326 Act_Spec : Node_Id;
2327 Act_Tree : Node_Id;
2329 Gen_Decl : Node_Id;
2330 Gen_Unit : Entity_Id;
2332 Is_Actual_Pack : constant Boolean :=
2333 Is_Internal (Defining_Entity (N));
2335 Parent_Installed : Boolean := False;
2336 Renaming_List : List_Id;
2337 Unit_Renaming : Node_Id;
2338 Needs_Body : Boolean;
2339 Inline_Now : Boolean := False;
2341 procedure Delay_Descriptors (E : Entity_Id);
2342 -- Delay generation of subprogram descriptors for given entity
2344 function Might_Inline_Subp return Boolean;
2345 -- If inlining is active and the generic contains inlined subprograms,
2346 -- we instantiate the body. This may cause superfluous instantiations,
2347 -- but it is simpler than detecting the need for the body at the point
2348 -- of inlining, when the context of the instance is not available.
2350 -----------------------
2351 -- Delay_Descriptors --
2352 -----------------------
2354 procedure Delay_Descriptors (E : Entity_Id) is
2355 begin
2356 if not Delay_Subprogram_Descriptors (E) then
2357 Set_Delay_Subprogram_Descriptors (E);
2358 Pending_Descriptor.Increment_Last;
2359 Pending_Descriptor.Table (Pending_Descriptor.Last) := E;
2360 end if;
2361 end Delay_Descriptors;
2363 -----------------------
2364 -- Might_Inline_Subp --
2365 -----------------------
2367 function Might_Inline_Subp return Boolean is
2368 E : Entity_Id;
2370 begin
2371 if not Inline_Processing_Required then
2372 return False;
2374 else
2375 E := First_Entity (Gen_Unit);
2376 while Present (E) loop
2377 if Is_Subprogram (E)
2378 and then Is_Inlined (E)
2379 then
2380 return True;
2381 end if;
2383 Next_Entity (E);
2384 end loop;
2385 end if;
2387 return False;
2388 end Might_Inline_Subp;
2390 -- Start of processing for Analyze_Package_Instantiation
2392 begin
2393 -- Very first thing: apply the special kludge for Text_IO processing
2394 -- in case we are instantiating one of the children of [Wide_]Text_IO.
2396 Text_IO_Kludge (Name (N));
2398 -- Make node global for error reporting
2400 Instantiation_Node := N;
2402 -- Case of instantiation of a generic package
2404 if Nkind (N) = N_Package_Instantiation then
2405 Act_Decl_Id := New_Copy (Defining_Entity (N));
2406 Set_Comes_From_Source (Act_Decl_Id, True);
2408 if Nkind (Defining_Unit_Name (N)) = N_Defining_Program_Unit_Name then
2409 Act_Decl_Name :=
2410 Make_Defining_Program_Unit_Name (Loc,
2411 Name => New_Copy_Tree (Name (Defining_Unit_Name (N))),
2412 Defining_Identifier => Act_Decl_Id);
2413 else
2414 Act_Decl_Name := Act_Decl_Id;
2415 end if;
2417 -- Case of instantiation of a formal package
2419 else
2420 Act_Decl_Id := Defining_Identifier (N);
2421 Act_Decl_Name := Act_Decl_Id;
2422 end if;
2424 Generate_Definition (Act_Decl_Id);
2425 Pre_Analyze_Actuals (N);
2427 Init_Env;
2428 Check_Generic_Child_Unit (Gen_Id, Parent_Installed);
2429 Gen_Unit := Entity (Gen_Id);
2431 -- Verify that it is the name of a generic package
2433 if Etype (Gen_Unit) = Any_Type then
2434 Restore_Env;
2435 return;
2437 elsif Ekind (Gen_Unit) /= E_Generic_Package then
2439 -- Ada 2005 (AI-50217): Cannot use instance in limited with_clause
2441 if From_With_Type (Gen_Unit) then
2442 Error_Msg_N
2443 ("cannot instantiate a limited withed package", Gen_Id);
2444 else
2445 Error_Msg_N
2446 ("expect name of generic package in instantiation", Gen_Id);
2447 end if;
2449 Restore_Env;
2450 return;
2451 end if;
2453 if In_Extended_Main_Source_Unit (N) then
2454 Set_Is_Instantiated (Gen_Unit);
2455 Generate_Reference (Gen_Unit, N);
2457 if Present (Renamed_Object (Gen_Unit)) then
2458 Set_Is_Instantiated (Renamed_Object (Gen_Unit));
2459 Generate_Reference (Renamed_Object (Gen_Unit), N);
2460 end if;
2461 end if;
2463 if Nkind (Gen_Id) = N_Identifier
2464 and then Chars (Gen_Unit) = Chars (Defining_Entity (N))
2465 then
2466 Error_Msg_NE
2467 ("& is hidden within declaration of instance", Gen_Id, Gen_Unit);
2469 elsif Nkind (Gen_Id) = N_Expanded_Name
2470 and then Is_Child_Unit (Gen_Unit)
2471 and then Nkind (Prefix (Gen_Id)) = N_Identifier
2472 and then Chars (Act_Decl_Id) = Chars (Prefix (Gen_Id))
2473 then
2474 Error_Msg_N
2475 ("& is hidden within declaration of instance ", Prefix (Gen_Id));
2476 end if;
2478 Set_Entity (Gen_Id, Gen_Unit);
2480 -- If generic is a renaming, get original generic unit
2482 if Present (Renamed_Object (Gen_Unit))
2483 and then Ekind (Renamed_Object (Gen_Unit)) = E_Generic_Package
2484 then
2485 Gen_Unit := Renamed_Object (Gen_Unit);
2486 end if;
2488 -- Verify that there are no circular instantiations
2490 if In_Open_Scopes (Gen_Unit) then
2491 Error_Msg_NE ("instantiation of & within itself", N, Gen_Unit);
2492 Restore_Env;
2493 return;
2495 elsif Contains_Instance_Of (Gen_Unit, Current_Scope, Gen_Id) then
2496 Error_Msg_Node_2 := Current_Scope;
2497 Error_Msg_NE
2498 ("circular Instantiation: & instantiated in &!", N, Gen_Unit);
2499 Circularity_Detected := True;
2500 Restore_Env;
2501 return;
2503 else
2504 Set_Instance_Env (Gen_Unit, Act_Decl_Id);
2505 Gen_Decl := Unit_Declaration_Node (Gen_Unit);
2507 -- Initialize renamings map, for error checking, and the list
2508 -- that holds private entities whose views have changed between
2509 -- generic definition and instantiation. If this is the instance
2510 -- created to validate an actual package, the instantiation
2511 -- environment is that of the enclosing instance.
2513 Generic_Renamings.Set_Last (0);
2514 Generic_Renamings_HTable.Reset;
2516 Create_Instantiation_Source (N, Gen_Unit, False, S_Adjustment);
2518 -- Copy original generic tree, to produce text for instantiation
2520 Act_Tree :=
2521 Copy_Generic_Node
2522 (Original_Node (Gen_Decl), Empty, Instantiating => True);
2524 Act_Spec := Specification (Act_Tree);
2526 -- If this is the instance created to validate an actual package,
2527 -- only the formals matter, do not examine the package spec itself.
2529 if Is_Actual_Pack then
2530 Set_Visible_Declarations (Act_Spec, New_List);
2531 Set_Private_Declarations (Act_Spec, New_List);
2532 end if;
2534 Renaming_List :=
2535 Analyze_Associations
2537 Generic_Formal_Declarations (Act_Tree),
2538 Generic_Formal_Declarations (Gen_Decl));
2540 Set_Defining_Unit_Name (Act_Spec, Act_Decl_Name);
2541 Set_Is_Generic_Instance (Act_Decl_Id);
2543 Set_Generic_Parent (Act_Spec, Gen_Unit);
2545 -- References to the generic in its own declaration or its body
2546 -- are references to the instance. Add a renaming declaration for
2547 -- the generic unit itself. This declaration, as well as the renaming
2548 -- declarations for the generic formals, must remain private to the
2549 -- unit: the formals, because this is the language semantics, and
2550 -- the unit because its use is an artifact of the implementation.
2552 Unit_Renaming :=
2553 Make_Package_Renaming_Declaration (Loc,
2554 Defining_Unit_Name =>
2555 Make_Defining_Identifier (Loc, Chars (Gen_Unit)),
2556 Name => New_Reference_To (Act_Decl_Id, Loc));
2558 Append (Unit_Renaming, Renaming_List);
2560 -- The renaming declarations are the first local declarations of
2561 -- the new unit.
2563 if Is_Non_Empty_List (Visible_Declarations (Act_Spec)) then
2564 Insert_List_Before
2565 (First (Visible_Declarations (Act_Spec)), Renaming_List);
2566 else
2567 Set_Visible_Declarations (Act_Spec, Renaming_List);
2568 end if;
2570 Act_Decl :=
2571 Make_Package_Declaration (Loc,
2572 Specification => Act_Spec);
2574 -- Save the instantiation node, for subsequent instantiation
2575 -- of the body, if there is one and we are generating code for
2576 -- the current unit. Mark the unit as having a body, to avoid
2577 -- a premature error message.
2579 -- We instantiate the body if we are generating code, if we are
2580 -- generating cross-reference information, or if we are building
2581 -- trees for ASIS use.
2583 declare
2584 Enclosing_Body_Present : Boolean := False;
2585 -- If the generic unit is not a compilation unit, then a body
2586 -- may be present in its parent even if none is required. We
2587 -- create a tentative pending instantiation for the body, which
2588 -- will be discarded if none is actually present.
2590 Scop : Entity_Id;
2592 begin
2593 if Scope (Gen_Unit) /= Standard_Standard
2594 and then not Is_Child_Unit (Gen_Unit)
2595 then
2596 Scop := Scope (Gen_Unit);
2598 while Present (Scop)
2599 and then Scop /= Standard_Standard
2600 loop
2601 if Unit_Requires_Body (Scop) then
2602 Enclosing_Body_Present := True;
2603 exit;
2605 elsif In_Open_Scopes (Scop)
2606 and then In_Package_Body (Scop)
2607 then
2608 Enclosing_Body_Present := True;
2609 exit;
2610 end if;
2612 exit when Is_Compilation_Unit (Scop);
2613 Scop := Scope (Scop);
2614 end loop;
2615 end if;
2617 -- If front-end inlining is enabled, and this is a unit for which
2618 -- code will be generated, we instantiate the body at once.
2619 -- This is done if the instance is not the main unit, and if the
2620 -- generic is not a child unit of another generic, to avoid scope
2621 -- problems and the reinstallation of parent instances.
2623 if Front_End_Inlining
2624 and then Expander_Active
2625 and then (not Is_Child_Unit (Gen_Unit)
2626 or else not Is_Generic_Unit (Scope (Gen_Unit)))
2627 and then (Is_In_Main_Unit (N)
2628 or else In_Main_Context (Current_Scope))
2629 and then Nkind (Parent (N)) /= N_Compilation_Unit
2630 and then Might_Inline_Subp
2631 and then not Is_Actual_Pack
2632 then
2633 Inline_Now := True;
2634 end if;
2636 Needs_Body :=
2637 (Unit_Requires_Body (Gen_Unit)
2638 or else Enclosing_Body_Present
2639 or else Present (Corresponding_Body (Gen_Decl)))
2640 and then (Is_In_Main_Unit (N)
2641 or else Might_Inline_Subp)
2642 and then not Is_Actual_Pack
2643 and then not Inline_Now
2645 and then (Operating_Mode = Generate_Code
2646 or else (Operating_Mode = Check_Semantics
2647 and then ASIS_Mode));
2649 -- If front_end_inlining is enabled, do not instantiate a
2650 -- body if within a generic context.
2652 if (Front_End_Inlining
2653 and then not Expander_Active)
2654 or else Is_Generic_Unit (Cunit_Entity (Main_Unit))
2655 then
2656 Needs_Body := False;
2657 end if;
2659 -- If the current context is generic, and the package being
2660 -- instantiated is declared within a formal package, there
2661 -- is no body to instantiate until the enclosing generic is
2662 -- instantiated, and there is an actual for the formal
2663 -- package. If the formal package has parameters, we build a
2664 -- regular package instance for it, that preceeds the original
2665 -- formal package declaration.
2667 if In_Open_Scopes (Scope (Scope (Gen_Unit))) then
2668 declare
2669 Decl : constant Node_Id :=
2670 Original_Node
2671 (Unit_Declaration_Node (Scope (Gen_Unit)));
2672 begin
2673 if Nkind (Decl) = N_Formal_Package_Declaration
2674 or else (Nkind (Decl) = N_Package_Declaration
2675 and then Is_List_Member (Decl)
2676 and then Present (Next (Decl))
2677 and then
2678 Nkind (Next (Decl)) = N_Formal_Package_Declaration)
2679 then
2680 Needs_Body := False;
2681 end if;
2682 end;
2683 end if;
2684 end;
2686 -- If we are generating the calling stubs from the instantiation
2687 -- of a generic RCI package, we will not use the body of the
2688 -- generic package.
2690 if Distribution_Stub_Mode = Generate_Caller_Stub_Body
2691 and then Is_Compilation_Unit (Defining_Entity (N))
2692 then
2693 Needs_Body := False;
2694 end if;
2696 if Needs_Body then
2698 -- Here is a defence against a ludicrous number of instantiations
2699 -- caused by a circular set of instantiation attempts.
2701 if Pending_Instantiations.Last >
2702 Hostparm.Max_Instantiations
2703 then
2704 Error_Msg_N ("too many instantiations", N);
2705 raise Unrecoverable_Error;
2706 end if;
2708 -- Indicate that the enclosing scopes contain an instantiation,
2709 -- and that cleanup actions should be delayed until after the
2710 -- instance body is expanded.
2712 Check_Forward_Instantiation (Gen_Decl);
2713 if Nkind (N) = N_Package_Instantiation then
2714 declare
2715 Enclosing_Master : Entity_Id := Current_Scope;
2717 begin
2718 while Enclosing_Master /= Standard_Standard loop
2720 if Ekind (Enclosing_Master) = E_Package then
2721 if Is_Compilation_Unit (Enclosing_Master) then
2722 if In_Package_Body (Enclosing_Master) then
2723 Delay_Descriptors
2724 (Body_Entity (Enclosing_Master));
2725 else
2726 Delay_Descriptors
2727 (Enclosing_Master);
2728 end if;
2730 exit;
2732 else
2733 Enclosing_Master := Scope (Enclosing_Master);
2734 end if;
2736 elsif Ekind (Enclosing_Master) = E_Generic_Package then
2737 Enclosing_Master := Scope (Enclosing_Master);
2739 elsif Is_Generic_Subprogram (Enclosing_Master)
2740 or else Ekind (Enclosing_Master) = E_Void
2741 then
2742 -- Cleanup actions will eventually be performed on
2743 -- the enclosing instance, if any. enclosing scope
2744 -- is void in the formal part of a generic subp.
2746 exit;
2748 else
2749 if Ekind (Enclosing_Master) = E_Entry
2750 and then
2751 Ekind (Scope (Enclosing_Master)) = E_Protected_Type
2752 then
2753 Enclosing_Master :=
2754 Protected_Body_Subprogram (Enclosing_Master);
2755 end if;
2757 Set_Delay_Cleanups (Enclosing_Master);
2759 while Ekind (Enclosing_Master) = E_Block loop
2760 Enclosing_Master := Scope (Enclosing_Master);
2761 end loop;
2763 if Is_Subprogram (Enclosing_Master) then
2764 Delay_Descriptors (Enclosing_Master);
2766 elsif Is_Task_Type (Enclosing_Master) then
2767 declare
2768 TBP : constant Node_Id :=
2769 Get_Task_Body_Procedure
2770 (Enclosing_Master);
2772 begin
2773 if Present (TBP) then
2774 Delay_Descriptors (TBP);
2775 Set_Delay_Cleanups (TBP);
2776 end if;
2777 end;
2778 end if;
2780 exit;
2781 end if;
2782 end loop;
2783 end;
2785 -- Make entry in table
2787 Pending_Instantiations.Increment_Last;
2788 Pending_Instantiations.Table (Pending_Instantiations.Last) :=
2789 (N, Act_Decl, Expander_Active, Current_Sem_Unit);
2790 end if;
2791 end if;
2793 Set_Categorization_From_Pragmas (Act_Decl);
2795 if Parent_Installed then
2796 Hide_Current_Scope;
2797 end if;
2799 Set_Instance_Spec (N, Act_Decl);
2801 -- If not a compilation unit, insert the package declaration
2802 -- before the original instantiation node.
2804 if Nkind (Parent (N)) /= N_Compilation_Unit then
2805 Mark_Rewrite_Insertion (Act_Decl);
2806 Insert_Before (N, Act_Decl);
2807 Analyze (Act_Decl);
2809 -- For an instantiation that is a compilation unit, place
2810 -- declaration on current node so context is complete
2811 -- for analysis (including nested instantiations). It this
2812 -- is the main unit, the declaration eventually replaces the
2813 -- instantiation node. If the instance body is later created, it
2814 -- replaces the instance node, and the declation is attached to
2815 -- it (see Build_Instance_Compilation_Unit_Nodes).
2817 else
2818 if Cunit_Entity (Current_Sem_Unit) = Defining_Entity (N) then
2820 -- The entity for the current unit is the newly created one,
2821 -- and all semantic information is attached to it.
2823 Set_Cunit_Entity (Current_Sem_Unit, Act_Decl_Id);
2825 -- If this is the main unit, replace the main entity as well
2827 if Current_Sem_Unit = Main_Unit then
2828 Main_Unit_Entity := Act_Decl_Id;
2829 end if;
2830 end if;
2832 -- There is a problem with inlining here.
2834 Set_Unit (Parent (N), Act_Decl);
2835 Set_Parent_Spec (Act_Decl, Parent_Spec (N));
2836 Analyze (Act_Decl);
2837 Set_Unit (Parent (N), N);
2838 Set_Body_Required (Parent (N), False);
2840 -- We never need elaboration checks on instantiations, since
2841 -- by definition, the body instantiation is elaborated at the
2842 -- same time as the spec instantiation.
2844 Set_Suppress_Elaboration_Warnings (Act_Decl_Id);
2845 Set_Kill_Elaboration_Checks (Act_Decl_Id);
2846 end if;
2848 Check_Elab_Instantiation (N);
2850 if ABE_Is_Certain (N) and then Needs_Body then
2851 Pending_Instantiations.Decrement_Last;
2852 end if;
2853 Check_Hidden_Child_Unit (N, Gen_Unit, Act_Decl_Id);
2855 Set_First_Private_Entity (Defining_Unit_Name (Unit_Renaming),
2856 First_Private_Entity (Act_Decl_Id));
2858 -- If the instantiation will receive a body, the unit will
2859 -- be transformed into a package body, and receive its own
2860 -- elaboration entity. Otherwise, the nature of the unit is
2861 -- now a package declaration.
2863 if Nkind (Parent (N)) = N_Compilation_Unit
2864 and then not Needs_Body
2865 then
2866 Rewrite (N, Act_Decl);
2867 end if;
2869 if Present (Corresponding_Body (Gen_Decl))
2870 or else Unit_Requires_Body (Gen_Unit)
2871 then
2872 Set_Has_Completion (Act_Decl_Id);
2873 end if;
2875 Check_Formal_Packages (Act_Decl_Id);
2877 Restore_Private_Views (Act_Decl_Id);
2879 if not Generic_Separately_Compiled (Gen_Unit) then
2880 Inherit_Context (Gen_Decl, N);
2881 end if;
2883 if Parent_Installed then
2884 Remove_Parent;
2885 end if;
2887 Restore_Env;
2888 end if;
2890 Validate_Categorization_Dependency (N, Act_Decl_Id);
2892 -- Check restriction, but skip this if something went wrong in
2893 -- the above analysis, indicated by Act_Decl_Id being void.
2895 if Ekind (Act_Decl_Id) /= E_Void
2896 and then not Is_Library_Level_Entity (Act_Decl_Id)
2897 then
2898 Check_Restriction (No_Local_Allocators, N);
2899 end if;
2901 if Inline_Now then
2902 Inline_Instance_Body (N, Gen_Unit, Act_Decl);
2903 end if;
2905 -- The following is a tree patch for ASIS: ASIS needs separate nodes
2906 -- to be used as defining identifiers for a formal package and for the
2907 -- corresponding expanded package
2909 if Nkind (N) = N_Formal_Package_Declaration then
2910 Act_Decl_Id := New_Copy (Defining_Entity (N));
2911 Set_Comes_From_Source (Act_Decl_Id, True);
2912 Set_Is_Generic_Instance (Act_Decl_Id, False);
2913 Set_Defining_Identifier (N, Act_Decl_Id);
2914 end if;
2916 exception
2917 when Instantiation_Error =>
2918 if Parent_Installed then
2919 Remove_Parent;
2920 end if;
2921 end Analyze_Package_Instantiation;
2923 --------------------------
2924 -- Inline_Instance_Body --
2925 --------------------------
2927 procedure Inline_Instance_Body
2928 (N : Node_Id;
2929 Gen_Unit : Entity_Id;
2930 Act_Decl : Node_Id)
2932 Vis : Boolean;
2933 Gen_Comp : constant Entity_Id :=
2934 Cunit_Entity (Get_Source_Unit (Gen_Unit));
2935 Curr_Comp : constant Node_Id := Cunit (Current_Sem_Unit);
2936 Curr_Scope : Entity_Id := Empty;
2937 Curr_Unit : constant Entity_Id :=
2938 Cunit_Entity (Current_Sem_Unit);
2939 Removed : Boolean := False;
2940 Num_Scopes : Int := 0;
2941 Use_Clauses : array (1 .. Scope_Stack.Last) of Node_Id;
2942 Instances : array (1 .. Scope_Stack.Last) of Entity_Id;
2943 Inner_Scopes : array (1 .. Scope_Stack.Last) of Entity_Id;
2944 Num_Inner : Int := 0;
2945 N_Instances : Int := 0;
2946 S : Entity_Id;
2948 begin
2949 -- Case of generic unit defined in another unit. We must remove
2950 -- the complete context of the current unit to install that of
2951 -- the generic.
2953 if Gen_Comp /= Cunit_Entity (Current_Sem_Unit) then
2954 S := Current_Scope;
2956 while Present (S)
2957 and then S /= Standard_Standard
2958 loop
2959 Num_Scopes := Num_Scopes + 1;
2961 Use_Clauses (Num_Scopes) :=
2962 (Scope_Stack.Table
2963 (Scope_Stack.Last - Num_Scopes + 1).
2964 First_Use_Clause);
2965 End_Use_Clauses (Use_Clauses (Num_Scopes));
2967 exit when Is_Generic_Instance (S)
2968 and then (In_Package_Body (S)
2969 or else Ekind (S) = E_Procedure
2970 or else Ekind (S) = E_Function);
2971 S := Scope (S);
2972 end loop;
2974 Vis := Is_Immediately_Visible (Gen_Comp);
2976 -- Find and save all enclosing instances
2978 S := Current_Scope;
2980 while Present (S)
2981 and then S /= Standard_Standard
2982 loop
2983 if Is_Generic_Instance (S) then
2984 N_Instances := N_Instances + 1;
2985 Instances (N_Instances) := S;
2987 exit when In_Package_Body (S);
2988 end if;
2990 S := Scope (S);
2991 end loop;
2993 -- Remove context of current compilation unit, unless we
2994 -- are within a nested package instantiation, in which case
2995 -- the context has been removed previously.
2997 -- If current scope is the body of a child unit, remove context
2998 -- of spec as well.
3000 S := Current_Scope;
3002 while Present (S)
3003 and then S /= Standard_Standard
3004 loop
3005 exit when Is_Generic_Instance (S)
3006 and then (In_Package_Body (S)
3007 or else Ekind (S) = E_Procedure
3008 or else Ekind (S) = E_Function);
3010 if S = Curr_Unit
3011 or else (Ekind (Curr_Unit) = E_Package_Body
3012 and then S = Spec_Entity (Curr_Unit))
3013 or else (Ekind (Curr_Unit) = E_Subprogram_Body
3014 and then S =
3015 Corresponding_Spec
3016 (Unit_Declaration_Node (Curr_Unit)))
3017 then
3018 Removed := True;
3020 -- Remove entities in current scopes from visibility, so
3021 -- than instance body is compiled in a clean environment.
3023 Save_Scope_Stack (Handle_Use => False);
3025 if Is_Child_Unit (S) then
3027 -- Remove child unit from stack, as well as inner scopes.
3028 -- Removing the context of a child unit removes parent
3029 -- units as well.
3031 while Current_Scope /= S loop
3032 Num_Inner := Num_Inner + 1;
3033 Inner_Scopes (Num_Inner) := Current_Scope;
3034 Pop_Scope;
3035 end loop;
3037 Pop_Scope;
3038 Remove_Context (Curr_Comp);
3039 Curr_Scope := S;
3041 else
3042 Remove_Context (Curr_Comp);
3043 end if;
3045 if Ekind (Curr_Unit) = E_Package_Body then
3046 Remove_Context (Library_Unit (Curr_Comp));
3047 end if;
3048 end if;
3050 S := Scope (S);
3051 end loop;
3053 New_Scope (Standard_Standard);
3054 Scope_Stack.Table (Scope_Stack.Last).Is_Active_Stack_Base := True;
3055 Instantiate_Package_Body
3056 ((N, Act_Decl, Expander_Active, Current_Sem_Unit), True);
3057 Pop_Scope;
3059 -- Restore context
3061 Set_Is_Immediately_Visible (Gen_Comp, Vis);
3063 -- Reset Generic_Instance flag so that use clauses can be installed
3064 -- in the proper order. (See Use_One_Package for effect of enclosing
3065 -- instances on processing of use clauses).
3067 for J in 1 .. N_Instances loop
3068 Set_Is_Generic_Instance (Instances (J), False);
3069 end loop;
3071 if Removed then
3072 Install_Context (Curr_Comp);
3074 if Present (Curr_Scope)
3075 and then Is_Child_Unit (Curr_Scope)
3076 then
3077 New_Scope (Curr_Scope);
3078 Set_Is_Immediately_Visible (Curr_Scope);
3080 -- Finally, restore inner scopes as well
3082 for J in reverse 1 .. Num_Inner loop
3083 New_Scope (Inner_Scopes (J));
3084 end loop;
3085 end if;
3087 Restore_Scope_Stack (Handle_Use => False);
3089 if Present (Curr_Scope)
3090 and then
3091 (In_Private_Part (Curr_Scope)
3092 or else In_Package_Body (Curr_Scope))
3093 then
3094 -- Install private declaration of ancestor units, which
3095 -- are currently available. Restore_Scope_Stack and
3096 -- Install_Context only install the visible part of parents.
3098 declare
3099 Par : Entity_Id;
3100 begin
3101 Par := Scope (Curr_Scope);
3102 while (Present (Par))
3103 and then Par /= Standard_Standard
3104 loop
3105 Install_Private_Declarations (Par);
3106 Par := Scope (Par);
3107 end loop;
3108 end;
3109 end if;
3110 end if;
3112 -- Restore use clauses. For a child unit, use clauses in the parents
3113 -- are restored when installing the context, so only those in inner
3114 -- scopes (and those local to the child unit itself) need to be
3115 -- installed explicitly.
3117 if Is_Child_Unit (Curr_Unit)
3118 and then Removed
3119 then
3120 for J in reverse 1 .. Num_Inner + 1 loop
3121 Scope_Stack.Table (Scope_Stack.Last - J + 1).First_Use_Clause :=
3122 Use_Clauses (J);
3123 Install_Use_Clauses (Use_Clauses (J));
3124 end loop;
3126 else
3127 for J in reverse 1 .. Num_Scopes loop
3128 Scope_Stack.Table (Scope_Stack.Last - J + 1).First_Use_Clause :=
3129 Use_Clauses (J);
3130 Install_Use_Clauses (Use_Clauses (J));
3131 end loop;
3132 end if;
3134 for J in 1 .. N_Instances loop
3135 Set_Is_Generic_Instance (Instances (J), True);
3136 end loop;
3138 -- If generic unit is in current unit, current context is correct
3140 else
3141 Instantiate_Package_Body
3142 ((N, Act_Decl, Expander_Active, Current_Sem_Unit), True);
3143 end if;
3144 end Inline_Instance_Body;
3146 -------------------------------------
3147 -- Analyze_Procedure_Instantiation --
3148 -------------------------------------
3150 procedure Analyze_Procedure_Instantiation (N : Node_Id) is
3151 begin
3152 Analyze_Subprogram_Instantiation (N, E_Procedure);
3153 end Analyze_Procedure_Instantiation;
3155 --------------------------------------
3156 -- Analyze_Subprogram_Instantiation --
3157 --------------------------------------
3159 procedure Analyze_Subprogram_Instantiation
3160 (N : Node_Id;
3161 K : Entity_Kind)
3163 Loc : constant Source_Ptr := Sloc (N);
3164 Gen_Id : constant Node_Id := Name (N);
3166 Anon_Id : constant Entity_Id :=
3167 Make_Defining_Identifier (Sloc (Defining_Entity (N)),
3168 Chars => New_External_Name
3169 (Chars (Defining_Entity (N)), 'R'));
3171 Act_Decl_Id : Entity_Id;
3172 Act_Decl : Node_Id;
3173 Act_Spec : Node_Id;
3174 Act_Tree : Node_Id;
3176 Gen_Unit : Entity_Id;
3177 Gen_Decl : Node_Id;
3178 Pack_Id : Entity_Id;
3179 Parent_Installed : Boolean := False;
3180 Renaming_List : List_Id;
3182 procedure Analyze_Instance_And_Renamings;
3183 -- The instance must be analyzed in a context that includes the
3184 -- mappings of generic parameters into actuals. We create a package
3185 -- declaration for this purpose, and a subprogram with an internal
3186 -- name within the package. The subprogram instance is simply an
3187 -- alias for the internal subprogram, declared in the current scope.
3189 ------------------------------------
3190 -- Analyze_Instance_And_Renamings --
3191 ------------------------------------
3193 procedure Analyze_Instance_And_Renamings is
3194 Def_Ent : constant Entity_Id := Defining_Entity (N);
3195 Pack_Decl : Node_Id;
3197 begin
3198 if Nkind (Parent (N)) = N_Compilation_Unit then
3200 -- For the case of a compilation unit, the container package
3201 -- has the same name as the instantiation, to insure that the
3202 -- binder calls the elaboration procedure with the right name.
3203 -- Copy the entity of the instance, which may have compilation
3204 -- level flags (e.g. Is_Child_Unit) set.
3206 Pack_Id := New_Copy (Def_Ent);
3208 else
3209 -- Otherwise we use the name of the instantiation concatenated
3210 -- with its source position to ensure uniqueness if there are
3211 -- several instantiations with the same name.
3213 Pack_Id :=
3214 Make_Defining_Identifier (Loc,
3215 Chars => New_External_Name
3216 (Related_Id => Chars (Def_Ent),
3217 Suffix => "GP",
3218 Suffix_Index => Source_Offset (Sloc (Def_Ent))));
3219 end if;
3221 Pack_Decl := Make_Package_Declaration (Loc,
3222 Specification => Make_Package_Specification (Loc,
3223 Defining_Unit_Name => Pack_Id,
3224 Visible_Declarations => Renaming_List,
3225 End_Label => Empty));
3227 Set_Instance_Spec (N, Pack_Decl);
3228 Set_Is_Generic_Instance (Pack_Id);
3229 Set_Needs_Debug_Info (Pack_Id);
3231 -- Case of not a compilation unit
3233 if Nkind (Parent (N)) /= N_Compilation_Unit then
3234 Mark_Rewrite_Insertion (Pack_Decl);
3235 Insert_Before (N, Pack_Decl);
3236 Set_Has_Completion (Pack_Id);
3238 -- Case of an instantiation that is a compilation unit
3240 -- Place declaration on current node so context is complete
3241 -- for analysis (including nested instantiations), and for
3242 -- use in a context_clause (see Analyze_With_Clause).
3244 else
3245 Set_Unit (Parent (N), Pack_Decl);
3246 Set_Parent_Spec (Pack_Decl, Parent_Spec (N));
3247 end if;
3249 Analyze (Pack_Decl);
3250 Check_Formal_Packages (Pack_Id);
3251 Set_Is_Generic_Instance (Pack_Id, False);
3253 -- Body of the enclosing package is supplied when instantiating
3254 -- the subprogram body, after semantic analysis is completed.
3256 if Nkind (Parent (N)) = N_Compilation_Unit then
3258 -- Remove package itself from visibility, so it does not
3259 -- conflict with subprogram.
3261 Set_Name_Entity_Id (Chars (Pack_Id), Homonym (Pack_Id));
3263 -- Set name and scope of internal subprogram so that the
3264 -- proper external name will be generated. The proper scope
3265 -- is the scope of the wrapper package. We need to generate
3266 -- debugging information for the internal subprogram, so set
3267 -- flag accordingly.
3269 Set_Chars (Anon_Id, Chars (Defining_Entity (N)));
3270 Set_Scope (Anon_Id, Scope (Pack_Id));
3272 -- Mark wrapper package as referenced, to avoid spurious
3273 -- warnings if the instantiation appears in various with_
3274 -- clauses of subunits of the main unit.
3276 Set_Referenced (Pack_Id);
3277 end if;
3279 Set_Is_Generic_Instance (Anon_Id);
3280 Set_Needs_Debug_Info (Anon_Id);
3281 Act_Decl_Id := New_Copy (Anon_Id);
3283 Set_Parent (Act_Decl_Id, Parent (Anon_Id));
3284 Set_Chars (Act_Decl_Id, Chars (Defining_Entity (N)));
3285 Set_Sloc (Act_Decl_Id, Sloc (Defining_Entity (N)));
3286 Set_Comes_From_Source (Act_Decl_Id, True);
3288 -- The signature may involve types that are not frozen yet, but
3289 -- the subprogram will be frozen at the point the wrapper package
3290 -- is frozen, so it does not need its own freeze node. In fact, if
3291 -- one is created, it might conflict with the freezing actions from
3292 -- the wrapper package (see 7206-013).
3294 Set_Has_Delayed_Freeze (Anon_Id, False);
3296 -- If the instance is a child unit, mark the Id accordingly. Mark
3297 -- the anonymous entity as well, which is the real subprogram and
3298 -- which is used when the instance appears in a context clause.
3300 Set_Is_Child_Unit (Act_Decl_Id, Is_Child_Unit (Defining_Entity (N)));
3301 Set_Is_Child_Unit (Anon_Id, Is_Child_Unit (Defining_Entity (N)));
3302 New_Overloaded_Entity (Act_Decl_Id);
3303 Check_Eliminated (Act_Decl_Id);
3305 -- In compilation unit case, kill elaboration checks on the
3306 -- instantiation, since they are never needed -- the body is
3307 -- instantiated at the same point as the spec.
3309 if Nkind (Parent (N)) = N_Compilation_Unit then
3310 Set_Suppress_Elaboration_Warnings (Act_Decl_Id);
3311 Set_Kill_Elaboration_Checks (Act_Decl_Id);
3312 Set_Is_Compilation_Unit (Anon_Id);
3314 Set_Cunit_Entity (Current_Sem_Unit, Pack_Id);
3315 end if;
3317 -- The instance is not a freezing point for the new subprogram
3319 Set_Is_Frozen (Act_Decl_Id, False);
3321 if Nkind (Defining_Entity (N)) = N_Defining_Operator_Symbol then
3322 Valid_Operator_Definition (Act_Decl_Id);
3323 end if;
3325 Set_Alias (Act_Decl_Id, Anon_Id);
3326 Set_Parent (Act_Decl_Id, Parent (Anon_Id));
3327 Set_Has_Completion (Act_Decl_Id);
3328 Set_Related_Instance (Pack_Id, Act_Decl_Id);
3330 if Nkind (Parent (N)) = N_Compilation_Unit then
3331 Set_Body_Required (Parent (N), False);
3332 end if;
3334 end Analyze_Instance_And_Renamings;
3336 -- Start of processing for Analyze_Subprogram_Instantiation
3338 begin
3339 -- Very first thing: apply the special kludge for Text_IO processing
3340 -- in case we are instantiating one of the children of [Wide_]Text_IO.
3341 -- Of course such an instantiation is bogus (these are packages, not
3342 -- subprograms), but we get a better error message if we do this.
3344 Text_IO_Kludge (Gen_Id);
3346 -- Make node global for error reporting
3348 Instantiation_Node := N;
3349 Pre_Analyze_Actuals (N);
3351 Init_Env;
3352 Check_Generic_Child_Unit (Gen_Id, Parent_Installed);
3353 Gen_Unit := Entity (Gen_Id);
3355 Generate_Reference (Gen_Unit, Gen_Id);
3357 if Nkind (Gen_Id) = N_Identifier
3358 and then Chars (Gen_Unit) = Chars (Defining_Entity (N))
3359 then
3360 Error_Msg_NE
3361 ("& is hidden within declaration of instance", Gen_Id, Gen_Unit);
3362 end if;
3364 if Etype (Gen_Unit) = Any_Type then
3365 Restore_Env;
3366 return;
3367 end if;
3369 -- Verify that it is a generic subprogram of the right kind, and that
3370 -- it does not lead to a circular instantiation.
3372 if Ekind (Gen_Unit) /= E_Generic_Procedure
3373 and then Ekind (Gen_Unit) /= E_Generic_Function
3374 then
3375 Error_Msg_N ("expect generic subprogram in instantiation", Gen_Id);
3377 elsif In_Open_Scopes (Gen_Unit) then
3378 Error_Msg_NE ("instantiation of & within itself", N, Gen_Unit);
3380 elsif K = E_Procedure
3381 and then Ekind (Gen_Unit) /= E_Generic_Procedure
3382 then
3383 if Ekind (Gen_Unit) = E_Generic_Function then
3384 Error_Msg_N
3385 ("cannot instantiate generic function as procedure", Gen_Id);
3386 else
3387 Error_Msg_N
3388 ("expect name of generic procedure in instantiation", Gen_Id);
3389 end if;
3391 elsif K = E_Function
3392 and then Ekind (Gen_Unit) /= E_Generic_Function
3393 then
3394 if Ekind (Gen_Unit) = E_Generic_Procedure then
3395 Error_Msg_N
3396 ("cannot instantiate generic procedure as function", Gen_Id);
3397 else
3398 Error_Msg_N
3399 ("expect name of generic function in instantiation", Gen_Id);
3400 end if;
3402 else
3403 Set_Entity (Gen_Id, Gen_Unit);
3404 Set_Is_Instantiated (Gen_Unit);
3406 if In_Extended_Main_Source_Unit (N) then
3407 Generate_Reference (Gen_Unit, N);
3408 end if;
3410 -- If renaming, get original unit
3412 if Present (Renamed_Object (Gen_Unit))
3413 and then (Ekind (Renamed_Object (Gen_Unit)) = E_Generic_Procedure
3414 or else
3415 Ekind (Renamed_Object (Gen_Unit)) = E_Generic_Function)
3416 then
3417 Gen_Unit := Renamed_Object (Gen_Unit);
3418 Set_Is_Instantiated (Gen_Unit);
3419 Generate_Reference (Gen_Unit, N);
3420 end if;
3422 if Contains_Instance_Of (Gen_Unit, Current_Scope, Gen_Id) then
3423 Error_Msg_Node_2 := Current_Scope;
3424 Error_Msg_NE
3425 ("circular Instantiation: & instantiated in &!", N, Gen_Unit);
3426 Circularity_Detected := True;
3427 return;
3428 end if;
3430 Gen_Decl := Unit_Declaration_Node (Gen_Unit);
3432 -- The subprogram itself cannot contain a nested instance, so
3433 -- the current parent is left empty.
3435 Set_Instance_Env (Gen_Unit, Empty);
3437 -- Initialize renamings map, for error checking
3439 Generic_Renamings.Set_Last (0);
3440 Generic_Renamings_HTable.Reset;
3442 Create_Instantiation_Source (N, Gen_Unit, False, S_Adjustment);
3444 -- Copy original generic tree, to produce text for instantiation
3446 Act_Tree :=
3447 Copy_Generic_Node
3448 (Original_Node (Gen_Decl), Empty, Instantiating => True);
3450 Act_Spec := Specification (Act_Tree);
3451 Renaming_List :=
3452 Analyze_Associations
3454 Generic_Formal_Declarations (Act_Tree),
3455 Generic_Formal_Declarations (Gen_Decl));
3457 -- Build the subprogram declaration, which does not appear
3458 -- in the generic template, and give it a sloc consistent
3459 -- with that of the template.
3461 Set_Defining_Unit_Name (Act_Spec, Anon_Id);
3462 Set_Generic_Parent (Act_Spec, Gen_Unit);
3463 Act_Decl :=
3464 Make_Subprogram_Declaration (Sloc (Act_Spec),
3465 Specification => Act_Spec);
3467 Set_Categorization_From_Pragmas (Act_Decl);
3469 if Parent_Installed then
3470 Hide_Current_Scope;
3471 end if;
3473 Append (Act_Decl, Renaming_List);
3474 Analyze_Instance_And_Renamings;
3476 -- If the generic is marked Import (Intrinsic), then so is the
3477 -- instance. This indicates that there is no body to instantiate.
3478 -- If generic is marked inline, so it the instance, and the
3479 -- anonymous subprogram it renames. If inlined, or else if inlining
3480 -- is enabled for the compilation, we generate the instance body
3481 -- even if it is not within the main unit.
3483 -- Any other pragmas might also be inherited ???
3485 if Is_Intrinsic_Subprogram (Gen_Unit) then
3486 Set_Is_Intrinsic_Subprogram (Anon_Id);
3487 Set_Is_Intrinsic_Subprogram (Act_Decl_Id);
3489 if Chars (Gen_Unit) = Name_Unchecked_Conversion then
3490 Validate_Unchecked_Conversion (N, Act_Decl_Id);
3491 end if;
3492 end if;
3494 Generate_Definition (Act_Decl_Id);
3496 Set_Is_Inlined (Act_Decl_Id, Is_Inlined (Gen_Unit));
3497 Set_Is_Inlined (Anon_Id, Is_Inlined (Gen_Unit));
3499 if not Is_Intrinsic_Subprogram (Gen_Unit) then
3500 Check_Elab_Instantiation (N);
3501 end if;
3503 Check_Hidden_Child_Unit (N, Gen_Unit, Act_Decl_Id);
3505 -- Subject to change, pending on if other pragmas are inherited ???
3507 Validate_Categorization_Dependency (N, Act_Decl_Id);
3509 if not Is_Intrinsic_Subprogram (Act_Decl_Id) then
3511 if not Generic_Separately_Compiled (Gen_Unit) then
3512 Inherit_Context (Gen_Decl, N);
3513 end if;
3515 Restore_Private_Views (Pack_Id, False);
3517 -- If the context requires a full instantiation, mark node for
3518 -- subsequent construction of the body.
3520 if (Is_In_Main_Unit (N)
3521 or else Is_Inlined (Act_Decl_Id))
3522 and then (Operating_Mode = Generate_Code
3523 or else (Operating_Mode = Check_Semantics
3524 and then ASIS_Mode))
3525 and then (Expander_Active or else ASIS_Mode)
3526 and then not ABE_Is_Certain (N)
3527 and then not Is_Eliminated (Act_Decl_Id)
3528 then
3529 Pending_Instantiations.Increment_Last;
3530 Pending_Instantiations.Table (Pending_Instantiations.Last) :=
3531 (N, Act_Decl, Expander_Active, Current_Sem_Unit);
3532 Check_Forward_Instantiation (Gen_Decl);
3534 -- The wrapper package is always delayed, because it does
3535 -- not constitute a freeze point, but to insure that the
3536 -- freeze node is placed properly, it is created directly
3537 -- when instantiating the body (otherwise the freeze node
3538 -- might appear to early for nested instantiations).
3540 elsif Nkind (Parent (N)) = N_Compilation_Unit then
3542 -- For ASIS purposes, indicate that the wrapper package has
3543 -- replaced the instantiation node.
3545 Rewrite (N, Unit (Parent (N)));
3546 Set_Unit (Parent (N), N);
3547 end if;
3549 elsif Nkind (Parent (N)) = N_Compilation_Unit then
3551 -- Replace instance node for library-level instantiations
3552 -- of intrinsic subprograms, for ASIS use.
3554 Rewrite (N, Unit (Parent (N)));
3555 Set_Unit (Parent (N), N);
3556 end if;
3558 if Parent_Installed then
3559 Remove_Parent;
3560 end if;
3562 Restore_Env;
3563 Generic_Renamings.Set_Last (0);
3564 Generic_Renamings_HTable.Reset;
3565 end if;
3567 exception
3568 when Instantiation_Error =>
3569 if Parent_Installed then
3570 Remove_Parent;
3571 end if;
3572 end Analyze_Subprogram_Instantiation;
3574 -------------------------
3575 -- Get_Associated_Node --
3576 -------------------------
3578 function Get_Associated_Node (N : Node_Id) return Node_Id is
3579 Assoc : Node_Id := Associated_Node (N);
3581 begin
3582 if Nkind (Assoc) /= Nkind (N) then
3583 return Assoc;
3585 elsif Nkind (Assoc) = N_Aggregate
3586 or else Nkind (Assoc) = N_Extension_Aggregate
3587 then
3588 return Assoc;
3590 else
3591 -- If the node is part of an inner generic, it may itself have been
3592 -- remapped into a further generic copy. Associated_Node is otherwise
3593 -- used for the entity of the node, and will be of a different node
3594 -- kind, or else N has been rewritten as a literal or function call.
3596 while Present (Associated_Node (Assoc))
3597 and then Nkind (Associated_Node (Assoc)) = Nkind (Assoc)
3598 loop
3599 Assoc := Associated_Node (Assoc);
3600 end loop;
3602 -- Follow and additional link in case the final node was rewritten.
3603 -- This can only happen with nested generic units.
3605 if (Nkind (Assoc) = N_Identifier or else Nkind (Assoc) in N_Op)
3606 and then Present (Associated_Node (Assoc))
3607 and then (Nkind (Associated_Node (Assoc)) = N_Function_Call
3608 or else
3609 Nkind (Associated_Node (Assoc)) = N_Explicit_Dereference
3610 or else
3611 Nkind (Associated_Node (Assoc)) = N_Integer_Literal
3612 or else
3613 Nkind (Associated_Node (Assoc)) = N_Real_Literal
3614 or else
3615 Nkind (Associated_Node (Assoc)) = N_String_Literal)
3616 then
3617 Assoc := Associated_Node (Assoc);
3618 end if;
3620 return Assoc;
3621 end if;
3622 end Get_Associated_Node;
3624 -------------------------------------------
3625 -- Build_Instance_Compilation_Unit_Nodes --
3626 -------------------------------------------
3628 procedure Build_Instance_Compilation_Unit_Nodes
3629 (N : Node_Id;
3630 Act_Body : Node_Id;
3631 Act_Decl : Node_Id)
3633 Decl_Cunit : Node_Id;
3634 Body_Cunit : Node_Id;
3635 Citem : Node_Id;
3636 New_Main : constant Entity_Id := Defining_Entity (Act_Decl);
3637 Old_Main : constant Entity_Id := Cunit_Entity (Main_Unit);
3639 begin
3640 -- A new compilation unit node is built for the instance declaration
3642 Decl_Cunit :=
3643 Make_Compilation_Unit (Sloc (N),
3644 Context_Items => Empty_List,
3645 Unit => Act_Decl,
3646 Aux_Decls_Node =>
3647 Make_Compilation_Unit_Aux (Sloc (N)));
3649 Set_Parent_Spec (Act_Decl, Parent_Spec (N));
3650 Set_Body_Required (Decl_Cunit, True);
3652 -- We use the original instantiation compilation unit as the resulting
3653 -- compilation unit of the instance, since this is the main unit.
3655 Rewrite (N, Act_Body);
3656 Body_Cunit := Parent (N);
3658 -- The two compilation unit nodes are linked by the Library_Unit field
3660 Set_Library_Unit (Decl_Cunit, Body_Cunit);
3661 Set_Library_Unit (Body_Cunit, Decl_Cunit);
3663 -- Preserve the private nature of the package if needed
3665 Set_Private_Present (Decl_Cunit, Private_Present (Body_Cunit));
3667 -- If the instance is not the main unit, its context, categorization,
3668 -- and elaboration entity are not relevant to the compilation.
3670 if Parent (N) /= Cunit (Main_Unit) then
3671 return;
3672 end if;
3674 -- The context clause items on the instantiation, which are now
3675 -- attached to the body compilation unit (since the body overwrote
3676 -- the original instantiation node), semantically belong on the spec,
3677 -- so copy them there. It's harmless to leave them on the body as well.
3678 -- In fact one could argue that they belong in both places.
3680 Citem := First (Context_Items (Body_Cunit));
3681 while Present (Citem) loop
3682 Append (New_Copy (Citem), Context_Items (Decl_Cunit));
3683 Next (Citem);
3684 end loop;
3686 -- Propagate categorization flags on packages, so that they appear
3687 -- in ali file for the spec of the unit.
3689 if Ekind (New_Main) = E_Package then
3690 Set_Is_Pure (Old_Main, Is_Pure (New_Main));
3691 Set_Is_Preelaborated (Old_Main, Is_Preelaborated (New_Main));
3692 Set_Is_Remote_Types (Old_Main, Is_Remote_Types (New_Main));
3693 Set_Is_Shared_Passive (Old_Main, Is_Shared_Passive (New_Main));
3694 Set_Is_Remote_Call_Interface
3695 (Old_Main, Is_Remote_Call_Interface (New_Main));
3696 end if;
3698 -- Make entry in Units table, so that binder can generate call to
3699 -- elaboration procedure for body, if any.
3701 Make_Instance_Unit (Body_Cunit);
3702 Main_Unit_Entity := New_Main;
3703 Set_Cunit_Entity (Main_Unit, Main_Unit_Entity);
3705 -- Build elaboration entity, since the instance may certainly
3706 -- generate elaboration code requiring a flag for protection.
3708 Build_Elaboration_Entity (Decl_Cunit, New_Main);
3709 end Build_Instance_Compilation_Unit_Nodes;
3711 -----------------------------------
3712 -- Check_Formal_Package_Instance --
3713 -----------------------------------
3715 -- If the formal has specific parameters, they must match those of the
3716 -- actual. Both of them are instances, and the renaming declarations
3717 -- for their formal parameters appear in the same order in both. The
3718 -- analyzed formal has been analyzed in the context of the current
3719 -- instance.
3721 procedure Check_Formal_Package_Instance
3722 (Formal_Pack : Entity_Id;
3723 Actual_Pack : Entity_Id)
3725 E1 : Entity_Id := First_Entity (Actual_Pack);
3726 E2 : Entity_Id := First_Entity (Formal_Pack);
3728 Expr1 : Node_Id;
3729 Expr2 : Node_Id;
3731 procedure Check_Mismatch (B : Boolean);
3732 -- Common error routine for mismatch between the parameters of
3733 -- the actual instance and those of the formal package.
3735 function Same_Instantiated_Constant (E1, E2 : Entity_Id) return Boolean;
3736 -- The formal may come from a nested formal package, and the actual
3737 -- may have been constant-folded. To determine whether the two denote
3738 -- the same entity we may have to traverse several definitions to
3739 -- recover the ultimate entity that they refer to.
3741 function Same_Instantiated_Variable (E1, E2 : Entity_Id) return Boolean;
3742 -- Similarly, if the formal comes from a nested formal package, the
3743 -- actual may designate the formal through multiple renamings, which
3744 -- have to be followed to determine the original variable in question.
3746 --------------------
3747 -- Check_Mismatch --
3748 --------------------
3750 procedure Check_Mismatch (B : Boolean) is
3751 begin
3752 if B then
3753 Error_Msg_NE
3754 ("actual for & in actual instance does not match formal",
3755 Parent (Actual_Pack), E1);
3756 end if;
3757 end Check_Mismatch;
3759 --------------------------------
3760 -- Same_Instantiated_Constant --
3761 --------------------------------
3763 function Same_Instantiated_Constant
3764 (E1, E2 : Entity_Id) return Boolean
3766 Ent : Entity_Id;
3767 begin
3768 Ent := E2;
3769 while Present (Ent) loop
3770 if E1 = Ent then
3771 return True;
3773 elsif Ekind (Ent) /= E_Constant then
3774 return False;
3776 elsif Is_Entity_Name (Constant_Value (Ent)) then
3777 if Entity (Constant_Value (Ent)) = E1 then
3778 return True;
3779 else
3780 Ent := Entity (Constant_Value (Ent));
3781 end if;
3783 -- The actual may be a constant that has been folded. Recover
3784 -- original name.
3786 elsif Is_Entity_Name (Original_Node (Constant_Value (Ent))) then
3787 Ent := Entity (Original_Node (Constant_Value (Ent)));
3788 else
3789 return False;
3790 end if;
3791 end loop;
3793 return False;
3794 end Same_Instantiated_Constant;
3796 --------------------------------
3797 -- Same_Instantiated_Variable --
3798 --------------------------------
3800 function Same_Instantiated_Variable
3801 (E1, E2 : Entity_Id) return Boolean
3803 function Original_Entity (E : Entity_Id) return Entity_Id;
3804 -- Follow chain of renamings to the ultimate ancestor
3806 ---------------------
3807 -- Original_Entity --
3808 ---------------------
3810 function Original_Entity (E : Entity_Id) return Entity_Id is
3811 Orig : Entity_Id;
3813 begin
3814 Orig := E;
3815 while Nkind (Parent (Orig)) = N_Object_Renaming_Declaration
3816 and then Present (Renamed_Object (Orig))
3817 and then Is_Entity_Name (Renamed_Object (Orig))
3818 loop
3819 Orig := Entity (Renamed_Object (Orig));
3820 end loop;
3822 return Orig;
3823 end Original_Entity;
3825 -- Start of processing for Same_Instantiated_Variable
3827 begin
3828 return Ekind (E1) = Ekind (E2)
3829 and then Original_Entity (E1) = Original_Entity (E2);
3830 end Same_Instantiated_Variable;
3832 -- Start of processing for Check_Formal_Package_Instance
3834 begin
3835 while Present (E1)
3836 and then Present (E2)
3837 loop
3838 exit when Ekind (E1) = E_Package
3839 and then Renamed_Entity (E1) = Renamed_Entity (Actual_Pack);
3841 if Is_Type (E1) then
3843 -- Subtypes must statically match. E1 and E2 are the
3844 -- local entities that are subtypes of the actuals.
3845 -- Itypes generated for other parameters need not be checked,
3846 -- the check will be performed on the parameters themselves.
3848 if not Is_Itype (E1)
3849 and then not Is_Itype (E2)
3850 then
3851 Check_Mismatch
3852 (not Is_Type (E2)
3853 or else Etype (E1) /= Etype (E2)
3854 or else not Subtypes_Statically_Match (E1, E2));
3855 end if;
3857 elsif Ekind (E1) = E_Constant then
3859 -- IN parameters must denote the same static value, or
3860 -- the same constant, or the literal null.
3862 Expr1 := Expression (Parent (E1));
3864 if Ekind (E2) /= E_Constant then
3865 Check_Mismatch (True);
3866 goto Next_E;
3867 else
3868 Expr2 := Expression (Parent (E2));
3869 end if;
3871 if Is_Static_Expression (Expr1) then
3873 if not Is_Static_Expression (Expr2) then
3874 Check_Mismatch (True);
3876 elsif Is_Integer_Type (Etype (E1)) then
3878 declare
3879 V1 : constant Uint := Expr_Value (Expr1);
3880 V2 : constant Uint := Expr_Value (Expr2);
3881 begin
3882 Check_Mismatch (V1 /= V2);
3883 end;
3885 elsif Is_Real_Type (Etype (E1)) then
3886 declare
3887 V1 : constant Ureal := Expr_Value_R (Expr1);
3888 V2 : constant Ureal := Expr_Value_R (Expr2);
3889 begin
3890 Check_Mismatch (V1 /= V2);
3891 end;
3893 elsif Is_String_Type (Etype (E1))
3894 and then Nkind (Expr1) = N_String_Literal
3895 then
3897 if Nkind (Expr2) /= N_String_Literal then
3898 Check_Mismatch (True);
3899 else
3900 Check_Mismatch
3901 (not String_Equal (Strval (Expr1), Strval (Expr2)));
3902 end if;
3903 end if;
3905 elsif Is_Entity_Name (Expr1) then
3906 if Is_Entity_Name (Expr2) then
3907 if Entity (Expr1) = Entity (Expr2) then
3908 null;
3909 else
3910 Check_Mismatch
3911 (not Same_Instantiated_Constant
3912 (Entity (Expr1), Entity (Expr2)));
3913 end if;
3914 else
3915 Check_Mismatch (True);
3916 end if;
3918 elsif Is_Entity_Name (Original_Node (Expr1))
3919 and then Is_Entity_Name (Expr2)
3920 and then
3921 Same_Instantiated_Constant
3922 (Entity (Original_Node (Expr1)), Entity (Expr2))
3923 then
3924 null;
3926 elsif Nkind (Expr1) = N_Null then
3927 Check_Mismatch (Nkind (Expr1) /= N_Null);
3929 else
3930 Check_Mismatch (True);
3931 end if;
3933 elsif Ekind (E1) = E_Variable then
3934 Check_Mismatch (not Same_Instantiated_Variable (E1, E2));
3936 elsif Ekind (E1) = E_Package then
3937 Check_Mismatch
3938 (Ekind (E1) /= Ekind (E2)
3939 or else Renamed_Object (E1) /= Renamed_Object (E2));
3941 elsif Is_Overloadable (E1) then
3943 -- Verify that the names of the entities match.
3944 -- What if actual is an attribute ???
3946 Check_Mismatch
3947 (Ekind (E2) /= Ekind (E1) or else (Alias (E1)) /= Alias (E2));
3949 else
3950 raise Program_Error;
3951 end if;
3953 <<Next_E>>
3954 Next_Entity (E1);
3955 Next_Entity (E2);
3956 end loop;
3957 end Check_Formal_Package_Instance;
3959 ---------------------------
3960 -- Check_Formal_Packages --
3961 ---------------------------
3963 procedure Check_Formal_Packages (P_Id : Entity_Id) is
3964 E : Entity_Id;
3965 Formal_P : Entity_Id;
3967 begin
3968 -- Iterate through the declarations in the instance, looking for
3969 -- package renaming declarations that denote instances of formal
3970 -- packages. Stop when we find the renaming of the current package
3971 -- itself. The declaration for a formal package without a box is
3972 -- followed by an internal entity that repeats the instantiation.
3974 E := First_Entity (P_Id);
3975 while Present (E) loop
3976 if Ekind (E) = E_Package then
3977 if Renamed_Object (E) = P_Id then
3978 exit;
3980 elsif Nkind (Parent (E)) /= N_Package_Renaming_Declaration then
3981 null;
3983 elsif not Box_Present (Parent (Associated_Formal_Package (E))) then
3984 Formal_P := Next_Entity (E);
3985 Check_Formal_Package_Instance (Formal_P, E);
3986 end if;
3987 end if;
3989 Next_Entity (E);
3990 end loop;
3991 end Check_Formal_Packages;
3993 ---------------------------------
3994 -- Check_Forward_Instantiation --
3995 ---------------------------------
3997 procedure Check_Forward_Instantiation (Decl : Node_Id) is
3998 S : Entity_Id;
3999 Gen_Comp : Entity_Id := Cunit_Entity (Get_Source_Unit (Decl));
4001 begin
4002 -- The instantiation appears before the generic body if we are in the
4003 -- scope of the unit containing the generic, either in its spec or in
4004 -- the package body. and before the generic body.
4006 if Ekind (Gen_Comp) = E_Package_Body then
4007 Gen_Comp := Spec_Entity (Gen_Comp);
4008 end if;
4010 if In_Open_Scopes (Gen_Comp)
4011 and then No (Corresponding_Body (Decl))
4012 then
4013 S := Current_Scope;
4015 while Present (S)
4016 and then not Is_Compilation_Unit (S)
4017 and then not Is_Child_Unit (S)
4018 loop
4019 if Ekind (S) = E_Package then
4020 Set_Has_Forward_Instantiation (S);
4021 end if;
4023 S := Scope (S);
4024 end loop;
4025 end if;
4026 end Check_Forward_Instantiation;
4028 ---------------------------
4029 -- Check_Generic_Actuals --
4030 ---------------------------
4032 -- The visibility of the actuals may be different between the
4033 -- point of generic instantiation and the instantiation of the body.
4035 procedure Check_Generic_Actuals
4036 (Instance : Entity_Id;
4037 Is_Formal_Box : Boolean)
4039 E : Entity_Id;
4040 Astype : Entity_Id;
4042 function Denotes_Previous_Actual (Typ : Entity_Id) return Boolean;
4043 -- For a formal that is an array type, the component type is often
4044 -- a previous formal in the same unit. The privacy status of the
4045 -- component type will have been examined earlier in the traversal
4046 -- of the corresponding actuals, and this status should not be
4047 -- modified for the array type itself.
4048 -- To detect this case we have to rescan the list of formals, which
4049 -- is usually short enough to ignore the resulting inefficiency.
4051 function Denotes_Previous_Actual (Typ : Entity_Id) return Boolean is
4052 Prev : Entity_Id;
4053 begin
4054 Prev := First_Entity (Instance);
4055 while Present (Prev) loop
4056 if Is_Type (Prev)
4057 and then Nkind (Parent (Prev)) = N_Subtype_Declaration
4058 and then Is_Entity_Name (Subtype_Indication (Parent (Prev)))
4059 and then Entity (Subtype_Indication (Parent (Prev))) = Typ
4060 then
4061 return True;
4062 elsif Prev = E then
4063 return False;
4064 else
4065 Next_Entity (Prev);
4066 end if;
4067 end loop;
4068 return False;
4069 end Denotes_Previous_Actual;
4071 -- Start of processing for Check_Generic_Actuals
4073 begin
4074 E := First_Entity (Instance);
4075 while Present (E) loop
4076 if Is_Type (E)
4077 and then Nkind (Parent (E)) = N_Subtype_Declaration
4078 and then Scope (Etype (E)) /= Instance
4079 and then Is_Entity_Name (Subtype_Indication (Parent (E)))
4080 then
4081 if Is_Array_Type (E)
4082 and then Denotes_Previous_Actual (Component_Type (E))
4083 then
4084 null;
4085 else
4086 Check_Private_View (Subtype_Indication (Parent (E)));
4087 end if;
4088 Set_Is_Generic_Actual_Type (E, True);
4089 Set_Is_Hidden (E, False);
4090 Set_Is_Potentially_Use_Visible (E,
4091 In_Use (Instance));
4093 -- We constructed the generic actual type as a subtype of
4094 -- the supplied type. This means that it normally would not
4095 -- inherit subtype specific attributes of the actual, which
4096 -- is wrong for the generic case.
4098 Astype := Ancestor_Subtype (E);
4100 if No (Astype) then
4102 -- can happen when E is an itype that is the full view of
4103 -- a private type completed, e.g. with a constrained array.
4105 Astype := Base_Type (E);
4106 end if;
4108 Set_Size_Info (E, (Astype));
4109 Set_RM_Size (E, RM_Size (Astype));
4110 Set_First_Rep_Item (E, First_Rep_Item (Astype));
4112 if Is_Discrete_Or_Fixed_Point_Type (E) then
4113 Set_RM_Size (E, RM_Size (Astype));
4115 -- In nested instances, the base type of an access actual
4116 -- may itself be private, and need to be exchanged.
4118 elsif Is_Access_Type (E)
4119 and then Is_Private_Type (Etype (E))
4120 then
4121 Check_Private_View
4122 (New_Occurrence_Of (Etype (E), Sloc (Instance)));
4123 end if;
4125 elsif Ekind (E) = E_Package then
4127 -- If this is the renaming for the current instance, we're done.
4128 -- Otherwise it is a formal package. If the corresponding formal
4129 -- was declared with a box, the (instantiations of the) generic
4130 -- formal part are also visible. Otherwise, ignore the entity
4131 -- created to validate the actuals.
4133 if Renamed_Object (E) = Instance then
4134 exit;
4136 elsif Nkind (Parent (E)) /= N_Package_Renaming_Declaration then
4137 null;
4139 -- The visibility of a formal of an enclosing generic is already
4140 -- correct.
4142 elsif Denotes_Formal_Package (E) then
4143 null;
4145 elsif Present (Associated_Formal_Package (E)) then
4146 if Box_Present (Parent (Associated_Formal_Package (E))) then
4147 Check_Generic_Actuals (Renamed_Object (E), True);
4148 end if;
4150 Set_Is_Hidden (E, False);
4151 end if;
4153 -- If this is a subprogram instance (in a wrapper package) the
4154 -- actual is fully visible.
4156 elsif Is_Wrapper_Package (Instance) then
4157 Set_Is_Hidden (E, False);
4159 else
4160 Set_Is_Hidden (E, not Is_Formal_Box);
4161 end if;
4163 Next_Entity (E);
4164 end loop;
4165 end Check_Generic_Actuals;
4167 ------------------------------
4168 -- Check_Generic_Child_Unit --
4169 ------------------------------
4171 procedure Check_Generic_Child_Unit
4172 (Gen_Id : Node_Id;
4173 Parent_Installed : in out Boolean)
4175 Loc : constant Source_Ptr := Sloc (Gen_Id);
4176 Gen_Par : Entity_Id := Empty;
4177 Inst_Par : Entity_Id;
4178 E : Entity_Id;
4179 S : Node_Id;
4181 function Find_Generic_Child
4182 (Scop : Entity_Id;
4183 Id : Node_Id) return Entity_Id;
4184 -- Search generic parent for possible child unit with the given name
4186 function In_Enclosing_Instance return Boolean;
4187 -- Within an instance of the parent, the child unit may be denoted
4188 -- by a simple name, or an abbreviated expanded name. Examine enclosing
4189 -- scopes to locate a possible parent instantiation.
4191 ------------------------
4192 -- Find_Generic_Child --
4193 ------------------------
4195 function Find_Generic_Child
4196 (Scop : Entity_Id;
4197 Id : Node_Id) return Entity_Id
4199 E : Entity_Id;
4201 begin
4202 -- If entity of name is already set, instance has already been
4203 -- resolved, e.g. in an enclosing instantiation.
4205 if Present (Entity (Id)) then
4206 if Scope (Entity (Id)) = Scop then
4207 return Entity (Id);
4208 else
4209 return Empty;
4210 end if;
4212 else
4213 E := First_Entity (Scop);
4214 while Present (E) loop
4215 if Chars (E) = Chars (Id)
4216 and then Is_Child_Unit (E)
4217 then
4218 if Is_Child_Unit (E)
4219 and then not Is_Visible_Child_Unit (E)
4220 then
4221 Error_Msg_NE
4222 ("generic child unit& is not visible", Gen_Id, E);
4223 end if;
4225 Set_Entity (Id, E);
4226 return E;
4227 end if;
4229 Next_Entity (E);
4230 end loop;
4232 return Empty;
4233 end if;
4234 end Find_Generic_Child;
4236 ---------------------------
4237 -- In_Enclosing_Instance --
4238 ---------------------------
4240 function In_Enclosing_Instance return Boolean is
4241 Enclosing_Instance : Node_Id;
4242 Instance_Decl : Node_Id;
4244 begin
4245 Enclosing_Instance := Current_Scope;
4247 while Present (Enclosing_Instance) loop
4248 Instance_Decl := Unit_Declaration_Node (Enclosing_Instance);
4250 if Ekind (Enclosing_Instance) = E_Package
4251 and then Is_Generic_Instance (Enclosing_Instance)
4252 and then Present
4253 (Generic_Parent (Specification (Instance_Decl)))
4254 then
4255 -- Check whether the generic we are looking for is a child
4256 -- of this instance.
4258 E := Find_Generic_Child
4259 (Generic_Parent (Specification (Instance_Decl)), Gen_Id);
4260 exit when Present (E);
4262 else
4263 E := Empty;
4264 end if;
4266 Enclosing_Instance := Scope (Enclosing_Instance);
4267 end loop;
4269 if No (E) then
4271 -- Not a child unit
4273 Analyze (Gen_Id);
4274 return False;
4276 else
4277 Rewrite (Gen_Id,
4278 Make_Expanded_Name (Loc,
4279 Chars => Chars (E),
4280 Prefix => New_Occurrence_Of (Enclosing_Instance, Loc),
4281 Selector_Name => New_Occurrence_Of (E, Loc)));
4283 Set_Entity (Gen_Id, E);
4284 Set_Etype (Gen_Id, Etype (E));
4285 Parent_Installed := False; -- Already in scope.
4286 return True;
4287 end if;
4288 end In_Enclosing_Instance;
4290 -- Start of processing for Check_Generic_Child_Unit
4292 begin
4293 -- If the name of the generic is given by a selected component, it
4294 -- may be the name of a generic child unit, and the prefix is the name
4295 -- of an instance of the parent, in which case the child unit must be
4296 -- visible. If this instance is not in scope, it must be placed there
4297 -- and removed after instantiation, because what is being instantiated
4298 -- is not the original child, but the corresponding child present in
4299 -- the instance of the parent.
4301 -- If the child is instantiated within the parent, it can be given by
4302 -- a simple name. In this case the instance is already in scope, but
4303 -- the child generic must be recovered from the generic parent as well.
4305 if Nkind (Gen_Id) = N_Selected_Component then
4306 S := Selector_Name (Gen_Id);
4307 Analyze (Prefix (Gen_Id));
4308 Inst_Par := Entity (Prefix (Gen_Id));
4310 if Ekind (Inst_Par) = E_Package
4311 and then Present (Renamed_Object (Inst_Par))
4312 then
4313 Inst_Par := Renamed_Object (Inst_Par);
4314 end if;
4316 if Ekind (Inst_Par) = E_Package then
4317 if Nkind (Parent (Inst_Par)) = N_Package_Specification then
4318 Gen_Par := Generic_Parent (Parent (Inst_Par));
4320 elsif Nkind (Parent (Inst_Par)) = N_Defining_Program_Unit_Name
4321 and then
4322 Nkind (Parent (Parent (Inst_Par))) = N_Package_Specification
4323 then
4324 Gen_Par := Generic_Parent (Parent (Parent (Inst_Par)));
4325 end if;
4327 elsif Ekind (Inst_Par) = E_Generic_Package
4328 and then Nkind (Parent (Gen_Id)) = N_Formal_Package_Declaration
4329 then
4330 -- A formal package may be a real child package, and not the
4331 -- implicit instance within a parent. In this case the child is
4332 -- not visible and has to be retrieved explicitly as well.
4334 Gen_Par := Inst_Par;
4335 end if;
4337 if Present (Gen_Par) then
4339 -- The prefix denotes an instantiation. The entity itself
4340 -- may be a nested generic, or a child unit.
4342 E := Find_Generic_Child (Gen_Par, S);
4344 if Present (E) then
4345 Change_Selected_Component_To_Expanded_Name (Gen_Id);
4346 Set_Entity (Gen_Id, E);
4347 Set_Etype (Gen_Id, Etype (E));
4348 Set_Entity (S, E);
4349 Set_Etype (S, Etype (E));
4351 -- Indicate that this is a reference to the parent
4353 if In_Extended_Main_Source_Unit (Gen_Id) then
4354 Set_Is_Instantiated (Inst_Par);
4355 end if;
4357 -- A common mistake is to replicate the naming scheme of
4358 -- a hierarchy by instantiating a generic child directly,
4359 -- rather than the implicit child in a parent instance:
4361 -- generic .. package Gpar is ..
4362 -- generic .. package Gpar.Child is ..
4363 -- package Par is new Gpar ();
4365 -- with Gpar.Child;
4366 -- package Par.Child is new Gpar.Child ();
4367 -- rather than Par.Child
4369 -- In this case the instantiation is within Par, which is
4370 -- an instance, but Gpar does not denote Par because we are
4371 -- not IN the instance of Gpar, so this is illegal. The test
4372 -- below recognizes this particular case.
4374 if Is_Child_Unit (E)
4375 and then not Comes_From_Source (Entity (Prefix (Gen_Id)))
4376 and then (not In_Instance
4377 or else Nkind (Parent (Parent (Gen_Id))) =
4378 N_Compilation_Unit)
4379 then
4380 Error_Msg_N
4381 ("prefix of generic child unit must be instance of parent",
4382 Gen_Id);
4383 end if;
4385 if not In_Open_Scopes (Inst_Par)
4386 and then Nkind (Parent (Gen_Id)) not in
4387 N_Generic_Renaming_Declaration
4388 then
4389 Install_Parent (Inst_Par);
4390 Parent_Installed := True;
4391 end if;
4393 else
4394 -- If the generic parent does not contain an entity that
4395 -- corresponds to the selector, the instance doesn't either.
4396 -- Analyzing the node will yield the appropriate error message.
4397 -- If the entity is not a child unit, then it is an inner
4398 -- generic in the parent.
4400 Analyze (Gen_Id);
4401 end if;
4403 else
4404 Analyze (Gen_Id);
4406 if Is_Child_Unit (Entity (Gen_Id))
4407 and then
4408 Nkind (Parent (Gen_Id)) not in N_Generic_Renaming_Declaration
4409 and then not In_Open_Scopes (Inst_Par)
4410 then
4411 Install_Parent (Inst_Par);
4412 Parent_Installed := True;
4413 end if;
4414 end if;
4416 elsif Nkind (Gen_Id) = N_Expanded_Name then
4418 -- Entity already present, analyze prefix, whose meaning may be
4419 -- an instance in the current context. If it is an instance of
4420 -- a relative within another, the proper parent may still have
4421 -- to be installed, if they are not of the same generation.
4423 Analyze (Prefix (Gen_Id));
4424 Inst_Par := Entity (Prefix (Gen_Id));
4426 if In_Enclosing_Instance then
4427 null;
4429 elsif Present (Entity (Gen_Id))
4430 and then Is_Child_Unit (Entity (Gen_Id))
4431 and then not In_Open_Scopes (Inst_Par)
4432 then
4433 Install_Parent (Inst_Par);
4434 Parent_Installed := True;
4435 end if;
4437 elsif In_Enclosing_Instance then
4439 -- The child unit is found in some enclosing scope
4441 null;
4443 else
4444 Analyze (Gen_Id);
4446 -- If this is the renaming of the implicit child in a parent
4447 -- instance, recover the parent name and install it.
4449 if Is_Entity_Name (Gen_Id) then
4450 E := Entity (Gen_Id);
4452 if Is_Generic_Unit (E)
4453 and then Nkind (Parent (E)) in N_Generic_Renaming_Declaration
4454 and then Is_Child_Unit (Renamed_Object (E))
4455 and then Is_Generic_Unit (Scope (Renamed_Object (E)))
4456 and then Nkind (Name (Parent (E))) = N_Expanded_Name
4457 then
4458 Rewrite (Gen_Id,
4459 New_Copy_Tree (Name (Parent (E))));
4460 Inst_Par := Entity (Prefix (Gen_Id));
4462 if not In_Open_Scopes (Inst_Par) then
4463 Install_Parent (Inst_Par);
4464 Parent_Installed := True;
4465 end if;
4467 -- If it is a child unit of a non-generic parent, it may be
4468 -- use-visible and given by a direct name. Install parent as
4469 -- for other cases.
4471 elsif Is_Generic_Unit (E)
4472 and then Is_Child_Unit (E)
4473 and then
4474 Nkind (Parent (Gen_Id)) not in N_Generic_Renaming_Declaration
4475 and then not Is_Generic_Unit (Scope (E))
4476 then
4477 if not In_Open_Scopes (Scope (E)) then
4478 Install_Parent (Scope (E));
4479 Parent_Installed := True;
4480 end if;
4481 end if;
4482 end if;
4483 end if;
4484 end Check_Generic_Child_Unit;
4486 -----------------------------
4487 -- Check_Hidden_Child_Unit --
4488 -----------------------------
4490 procedure Check_Hidden_Child_Unit
4491 (N : Node_Id;
4492 Gen_Unit : Entity_Id;
4493 Act_Decl_Id : Entity_Id)
4495 Gen_Id : constant Node_Id := Name (N);
4497 begin
4498 if Is_Child_Unit (Gen_Unit)
4499 and then Is_Child_Unit (Act_Decl_Id)
4500 and then Nkind (Gen_Id) = N_Expanded_Name
4501 and then Entity (Prefix (Gen_Id)) = Scope (Act_Decl_Id)
4502 and then Chars (Gen_Unit) = Chars (Act_Decl_Id)
4503 then
4504 Error_Msg_Node_2 := Scope (Act_Decl_Id);
4505 Error_Msg_NE
4506 ("generic unit & is implicitly declared in &",
4507 Defining_Unit_Name (N), Gen_Unit);
4508 Error_Msg_N ("\instance must have different name",
4509 Defining_Unit_Name (N));
4510 end if;
4511 end Check_Hidden_Child_Unit;
4513 ------------------------
4514 -- Check_Private_View --
4515 ------------------------
4517 procedure Check_Private_View (N : Node_Id) is
4518 T : constant Entity_Id := Etype (N);
4519 BT : Entity_Id;
4521 begin
4522 -- Exchange views if the type was not private in the generic but is
4523 -- private at the point of instantiation. Do not exchange views if
4524 -- the scope of the type is in scope. This can happen if both generic
4525 -- and instance are sibling units, or if type is defined in a parent.
4526 -- In this case the visibility of the type will be correct for all
4527 -- semantic checks.
4529 if Present (T) then
4530 BT := Base_Type (T);
4532 if Is_Private_Type (T)
4533 and then not Has_Private_View (N)
4534 and then Present (Full_View (T))
4535 and then not In_Open_Scopes (Scope (T))
4536 then
4537 -- In the generic, the full type was visible. Save the
4538 -- private entity, for subsequent exchange.
4540 Switch_View (T);
4542 elsif Has_Private_View (N)
4543 and then not Is_Private_Type (T)
4544 and then not Has_Been_Exchanged (T)
4545 and then Etype (Get_Associated_Node (N)) /= T
4546 then
4547 -- Only the private declaration was visible in the generic. If
4548 -- the type appears in a subtype declaration, the subtype in the
4549 -- instance must have a view compatible with that of its parent,
4550 -- which must be exchanged (see corresponding code in Restore_
4551 -- Private_Views). Otherwise, if the type is defined in a parent
4552 -- unit, leave full visibility within instance, which is safe.
4554 if In_Open_Scopes (Scope (Base_Type (T)))
4555 and then not Is_Private_Type (Base_Type (T))
4556 and then Comes_From_Source (Base_Type (T))
4557 then
4558 null;
4560 elsif Nkind (Parent (N)) = N_Subtype_Declaration
4561 or else not In_Private_Part (Scope (Base_Type (T)))
4562 then
4563 Append_Elmt (T, Exchanged_Views);
4564 Exchange_Declarations (Etype (Get_Associated_Node (N)));
4565 end if;
4567 -- For composite types with inconsistent representation
4568 -- exchange component types accordingly.
4570 elsif Is_Access_Type (T)
4571 and then Is_Private_Type (Designated_Type (T))
4572 and then not Has_Private_View (N)
4573 and then Present (Full_View (Designated_Type (T)))
4574 then
4575 Switch_View (Designated_Type (T));
4577 elsif Is_Array_Type (T)
4578 and then Is_Private_Type (Component_Type (T))
4579 and then not Has_Private_View (N)
4580 and then Present (Full_View (Component_Type (T)))
4581 then
4582 Switch_View (Component_Type (T));
4584 elsif Is_Private_Type (T)
4585 and then Present (Full_View (T))
4586 and then Is_Array_Type (Full_View (T))
4587 and then Is_Private_Type (Component_Type (Full_View (T)))
4588 then
4589 Switch_View (T);
4591 -- Finally, a non-private subtype may have a private base type,
4592 -- which must be exchanged for consistency. This can happen when
4593 -- instantiating a package body, when the scope stack is empty
4594 -- but in fact the subtype and the base type are declared in an
4595 -- enclosing scope.
4597 elsif not Is_Private_Type (T)
4598 and then not Has_Private_View (N)
4599 and then Is_Private_Type (Base_Type (T))
4600 and then Present (Full_View (BT))
4601 and then not Is_Generic_Type (BT)
4602 and then not In_Open_Scopes (BT)
4603 then
4604 Append_Elmt (Full_View (BT), Exchanged_Views);
4605 Exchange_Declarations (BT);
4606 end if;
4607 end if;
4608 end Check_Private_View;
4610 --------------------------
4611 -- Contains_Instance_Of --
4612 --------------------------
4614 function Contains_Instance_Of
4615 (Inner : Entity_Id;
4616 Outer : Entity_Id;
4617 N : Node_Id) return Boolean
4619 Elmt : Elmt_Id;
4620 Scop : Entity_Id;
4622 begin
4623 Scop := Outer;
4625 -- Verify that there are no circular instantiations. We check whether
4626 -- the unit contains an instance of the current scope or some enclosing
4627 -- scope (in case one of the instances appears in a subunit). Longer
4628 -- circularities involving subunits might seem too pathological to
4629 -- consider, but they were not too pathological for the authors of
4630 -- DEC bc30vsq, so we loop over all enclosing scopes, and mark all
4631 -- enclosing generic scopes as containing an instance.
4633 loop
4634 -- Within a generic subprogram body, the scope is not generic, to
4635 -- allow for recursive subprograms. Use the declaration to determine
4636 -- whether this is a generic unit.
4638 if Ekind (Scop) = E_Generic_Package
4639 or else (Is_Subprogram (Scop)
4640 and then Nkind (Unit_Declaration_Node (Scop)) =
4641 N_Generic_Subprogram_Declaration)
4642 then
4643 Elmt := First_Elmt (Inner_Instances (Inner));
4645 while Present (Elmt) loop
4646 if Node (Elmt) = Scop then
4647 Error_Msg_Node_2 := Inner;
4648 Error_Msg_NE
4649 ("circular Instantiation: & instantiated within &!",
4650 N, Scop);
4651 return True;
4653 elsif Node (Elmt) = Inner then
4654 return True;
4656 elsif Contains_Instance_Of (Node (Elmt), Scop, N) then
4657 Error_Msg_Node_2 := Inner;
4658 Error_Msg_NE
4659 ("circular Instantiation: & instantiated within &!",
4660 N, Node (Elmt));
4661 return True;
4662 end if;
4664 Next_Elmt (Elmt);
4665 end loop;
4667 -- Indicate that Inner is being instantiated within Scop
4669 Append_Elmt (Inner, Inner_Instances (Scop));
4670 end if;
4672 if Scop = Standard_Standard then
4673 exit;
4674 else
4675 Scop := Scope (Scop);
4676 end if;
4677 end loop;
4679 return False;
4680 end Contains_Instance_Of;
4682 -----------------------
4683 -- Copy_Generic_Node --
4684 -----------------------
4686 function Copy_Generic_Node
4687 (N : Node_Id;
4688 Parent_Id : Node_Id;
4689 Instantiating : Boolean) return Node_Id
4691 Ent : Entity_Id;
4692 New_N : Node_Id;
4694 function Copy_Generic_Descendant (D : Union_Id) return Union_Id;
4695 -- Check the given value of one of the Fields referenced by the
4696 -- current node to determine whether to copy it recursively. The
4697 -- field may hold a Node_Id, a List_Id, or an Elist_Id, or a plain
4698 -- value (Sloc, Uint, Char) in which case it need not be copied.
4700 procedure Copy_Descendants;
4701 -- Common utility for various nodes
4703 function Copy_Generic_Elist (E : Elist_Id) return Elist_Id;
4704 -- Make copy of element list
4706 function Copy_Generic_List
4707 (L : List_Id;
4708 Parent_Id : Node_Id) return List_Id;
4709 -- Apply Copy_Node recursively to the members of a node list
4711 function In_Defining_Unit_Name (Nam : Node_Id) return Boolean;
4712 -- True if an identifier is part of the defining program unit name
4713 -- of a child unit. The entity of such an identifier must be kept
4714 -- (for ASIS use) even though as the name of an enclosing generic
4715 -- it would otherwise not be preserved in the generic tree.
4717 ----------------------
4718 -- Copy_Descendants --
4719 ----------------------
4721 procedure Copy_Descendants is
4723 use Atree.Unchecked_Access;
4724 -- This code section is part of the implementation of an untyped
4725 -- tree traversal, so it needs direct access to node fields.
4727 begin
4728 Set_Field1 (New_N, Copy_Generic_Descendant (Field1 (N)));
4729 Set_Field2 (New_N, Copy_Generic_Descendant (Field2 (N)));
4730 Set_Field3 (New_N, Copy_Generic_Descendant (Field3 (N)));
4731 Set_Field4 (New_N, Copy_Generic_Descendant (Field4 (N)));
4732 Set_Field5 (New_N, Copy_Generic_Descendant (Field5 (N)));
4733 end Copy_Descendants;
4735 -----------------------------
4736 -- Copy_Generic_Descendant --
4737 -----------------------------
4739 function Copy_Generic_Descendant (D : Union_Id) return Union_Id is
4740 begin
4741 if D = Union_Id (Empty) then
4742 return D;
4744 elsif D in Node_Range then
4745 return Union_Id
4746 (Copy_Generic_Node (Node_Id (D), New_N, Instantiating));
4748 elsif D in List_Range then
4749 return Union_Id (Copy_Generic_List (List_Id (D), New_N));
4751 elsif D in Elist_Range then
4752 return Union_Id (Copy_Generic_Elist (Elist_Id (D)));
4754 -- Nothing else is copyable (e.g. Uint values), return as is
4756 else
4757 return D;
4758 end if;
4759 end Copy_Generic_Descendant;
4761 ------------------------
4762 -- Copy_Generic_Elist --
4763 ------------------------
4765 function Copy_Generic_Elist (E : Elist_Id) return Elist_Id is
4766 M : Elmt_Id;
4767 L : Elist_Id;
4769 begin
4770 if Present (E) then
4771 L := New_Elmt_List;
4772 M := First_Elmt (E);
4773 while Present (M) loop
4774 Append_Elmt
4775 (Copy_Generic_Node (Node (M), Empty, Instantiating), L);
4776 Next_Elmt (M);
4777 end loop;
4779 return L;
4781 else
4782 return No_Elist;
4783 end if;
4784 end Copy_Generic_Elist;
4786 -----------------------
4787 -- Copy_Generic_List --
4788 -----------------------
4790 function Copy_Generic_List
4791 (L : List_Id;
4792 Parent_Id : Node_Id) return List_Id
4794 N : Node_Id;
4795 New_L : List_Id;
4797 begin
4798 if Present (L) then
4799 New_L := New_List;
4800 Set_Parent (New_L, Parent_Id);
4802 N := First (L);
4803 while Present (N) loop
4804 Append (Copy_Generic_Node (N, Empty, Instantiating), New_L);
4805 Next (N);
4806 end loop;
4808 return New_L;
4810 else
4811 return No_List;
4812 end if;
4813 end Copy_Generic_List;
4815 ---------------------------
4816 -- In_Defining_Unit_Name --
4817 ---------------------------
4819 function In_Defining_Unit_Name (Nam : Node_Id) return Boolean is
4820 begin
4821 return Present (Parent (Nam))
4822 and then (Nkind (Parent (Nam)) = N_Defining_Program_Unit_Name
4823 or else
4824 (Nkind (Parent (Nam)) = N_Expanded_Name
4825 and then In_Defining_Unit_Name (Parent (Nam))));
4826 end In_Defining_Unit_Name;
4828 -- Start of processing for Copy_Generic_Node
4830 begin
4831 if N = Empty then
4832 return N;
4833 end if;
4835 New_N := New_Copy (N);
4837 if Instantiating then
4838 Adjust_Instantiation_Sloc (New_N, S_Adjustment);
4839 end if;
4841 if not Is_List_Member (N) then
4842 Set_Parent (New_N, Parent_Id);
4843 end if;
4845 -- If defining identifier, then all fields have been copied already
4847 if Nkind (New_N) in N_Entity then
4848 null;
4850 -- Special casing for identifiers and other entity names and operators
4852 elsif Nkind (New_N) = N_Identifier
4853 or else Nkind (New_N) = N_Character_Literal
4854 or else Nkind (New_N) = N_Expanded_Name
4855 or else Nkind (New_N) = N_Operator_Symbol
4856 or else Nkind (New_N) in N_Op
4857 then
4858 if not Instantiating then
4860 -- Link both nodes in order to assign subsequently the
4861 -- entity of the copy to the original node, in case this
4862 -- is a global reference.
4864 Set_Associated_Node (N, New_N);
4866 -- If we are within an instantiation, this is a nested generic
4867 -- that has already been analyzed at the point of definition. We
4868 -- must preserve references that were global to the enclosing
4869 -- parent at that point. Other occurrences, whether global or
4870 -- local to the current generic, must be resolved anew, so we
4871 -- reset the entity in the generic copy. A global reference has
4872 -- a smaller depth than the parent, or else the same depth in
4873 -- case both are distinct compilation units.
4875 -- It is also possible for Current_Instantiated_Parent to be
4876 -- defined, and for this not to be a nested generic, namely
4877 -- if the unit is loaded through Rtsfind. In that case, the
4878 -- entity of New_N is only a link to the associated node, and
4879 -- not a defining occurrence.
4881 -- The entities for parent units in the defining_program_unit
4882 -- of a generic child unit are established when the context of
4883 -- the unit is first analyzed, before the generic copy is made.
4884 -- They are preserved in the copy for use in ASIS queries.
4886 Ent := Entity (New_N);
4888 if No (Current_Instantiated_Parent.Gen_Id) then
4889 if No (Ent)
4890 or else Nkind (Ent) /= N_Defining_Identifier
4891 or else not In_Defining_Unit_Name (N)
4892 then
4893 Set_Associated_Node (New_N, Empty);
4894 end if;
4896 elsif No (Ent)
4897 or else
4898 not (Nkind (Ent) = N_Defining_Identifier
4899 or else
4900 Nkind (Ent) = N_Defining_Character_Literal
4901 or else
4902 Nkind (Ent) = N_Defining_Operator_Symbol)
4903 or else No (Scope (Ent))
4904 or else Scope (Ent) = Current_Instantiated_Parent.Gen_Id
4905 or else (Scope_Depth (Scope (Ent)) >
4906 Scope_Depth (Current_Instantiated_Parent.Gen_Id)
4907 and then
4908 Get_Source_Unit (Ent) =
4909 Get_Source_Unit (Current_Instantiated_Parent.Gen_Id))
4910 then
4911 Set_Associated_Node (New_N, Empty);
4912 end if;
4914 -- Case of instantiating identifier or some other name or operator
4916 else
4917 -- If the associated node is still defined, the entity in
4918 -- it is global, and must be copied to the instance.
4919 -- If this copy is being made for a body to inline, it is
4920 -- applied to an instantiated tree, and the entity is already
4921 -- present and must be also preserved.
4923 declare
4924 Assoc : constant Node_Id := Get_Associated_Node (N);
4925 begin
4926 if Present (Assoc) then
4927 if Nkind (Assoc) = Nkind (N) then
4928 Set_Entity (New_N, Entity (Assoc));
4929 Check_Private_View (N);
4931 elsif Nkind (Assoc) = N_Function_Call then
4932 Set_Entity (New_N, Entity (Name (Assoc)));
4934 elsif (Nkind (Assoc) = N_Defining_Identifier
4935 or else Nkind (Assoc) = N_Defining_Character_Literal
4936 or else Nkind (Assoc) = N_Defining_Operator_Symbol)
4937 and then Expander_Active
4938 then
4939 -- Inlining case: we are copying a tree that contains
4940 -- global entities, which are preserved in the copy
4941 -- to be used for subsequent inlining.
4943 null;
4945 else
4946 Set_Entity (New_N, Empty);
4947 end if;
4948 end if;
4949 end;
4950 end if;
4952 -- For expanded name, we must copy the Prefix and Selector_Name
4954 if Nkind (N) = N_Expanded_Name then
4955 Set_Prefix
4956 (New_N, Copy_Generic_Node (Prefix (N), New_N, Instantiating));
4958 Set_Selector_Name (New_N,
4959 Copy_Generic_Node (Selector_Name (N), New_N, Instantiating));
4961 -- For operators, we must copy the right operand
4963 elsif Nkind (N) in N_Op then
4964 Set_Right_Opnd (New_N,
4965 Copy_Generic_Node (Right_Opnd (N), New_N, Instantiating));
4967 -- And for binary operators, the left operand as well
4969 if Nkind (N) in N_Binary_Op then
4970 Set_Left_Opnd (New_N,
4971 Copy_Generic_Node (Left_Opnd (N), New_N, Instantiating));
4972 end if;
4973 end if;
4975 -- Special casing for stubs
4977 elsif Nkind (N) in N_Body_Stub then
4979 -- In any case, we must copy the specification or defining
4980 -- identifier as appropriate.
4982 if Nkind (N) = N_Subprogram_Body_Stub then
4983 Set_Specification (New_N,
4984 Copy_Generic_Node (Specification (N), New_N, Instantiating));
4986 else
4987 Set_Defining_Identifier (New_N,
4988 Copy_Generic_Node
4989 (Defining_Identifier (N), New_N, Instantiating));
4990 end if;
4992 -- If we are not instantiating, then this is where we load and
4993 -- analyze subunits, i.e. at the point where the stub occurs. A
4994 -- more permissivle system might defer this analysis to the point
4995 -- of instantiation, but this seems to complicated for now.
4997 if not Instantiating then
4998 declare
4999 Subunit_Name : constant Unit_Name_Type := Get_Unit_Name (N);
5000 Subunit : Node_Id;
5001 Unum : Unit_Number_Type;
5002 New_Body : Node_Id;
5004 begin
5005 Unum :=
5006 Load_Unit
5007 (Load_Name => Subunit_Name,
5008 Required => False,
5009 Subunit => True,
5010 Error_Node => N);
5012 -- If the proper body is not found, a warning message will
5013 -- be emitted when analyzing the stub, or later at the the
5014 -- point of instantiation. Here we just leave the stub as is.
5016 if Unum = No_Unit then
5017 Subunits_Missing := True;
5018 goto Subunit_Not_Found;
5019 end if;
5021 Subunit := Cunit (Unum);
5023 if Nkind (Unit (Subunit)) /= N_Subunit then
5024 Error_Msg_Sloc := Sloc (N);
5025 Error_Msg_N
5026 ("expected SEPARATE subunit to complete stub at#,"
5027 & " found child unit", Subunit);
5028 goto Subunit_Not_Found;
5029 end if;
5031 -- We must create a generic copy of the subunit, in order
5032 -- to perform semantic analysis on it, and we must replace
5033 -- the stub in the original generic unit with the subunit,
5034 -- in order to preserve non-local references within.
5036 -- Only the proper body needs to be copied. Library_Unit and
5037 -- context clause are simply inherited by the generic copy.
5038 -- Note that the copy (which may be recursive if there are
5039 -- nested subunits) must be done first, before attaching it
5040 -- to the enclosing generic.
5042 New_Body :=
5043 Copy_Generic_Node
5044 (Proper_Body (Unit (Subunit)),
5045 Empty, Instantiating => False);
5047 -- Now place the original proper body in the original
5048 -- generic unit. This is a body, not a compilation unit.
5050 Rewrite (N, Proper_Body (Unit (Subunit)));
5051 Set_Is_Compilation_Unit (Defining_Entity (N), False);
5052 Set_Was_Originally_Stub (N);
5054 -- Finally replace the body of the subunit with its copy,
5055 -- and make this new subunit into the library unit of the
5056 -- generic copy, which does not have stubs any longer.
5058 Set_Proper_Body (Unit (Subunit), New_Body);
5059 Set_Library_Unit (New_N, Subunit);
5060 Inherit_Context (Unit (Subunit), N);
5061 end;
5063 -- If we are instantiating, this must be an error case, since
5064 -- otherwise we would have replaced the stub node by the proper
5065 -- body that corresponds. So just ignore it in the copy (i.e.
5066 -- we have copied it, and that is good enough).
5068 else
5069 null;
5070 end if;
5072 <<Subunit_Not_Found>> null;
5074 -- If the node is a compilation unit, it is the subunit of a stub,
5075 -- which has been loaded already (see code below). In this case,
5076 -- the library unit field of N points to the parent unit (which
5077 -- is a compilation unit) and need not (and cannot!) be copied.
5079 -- When the proper body of the stub is analyzed, thie library_unit
5080 -- link is used to establish the proper context (see sem_ch10).
5082 -- The other fields of a compilation unit are copied as usual
5084 elsif Nkind (N) = N_Compilation_Unit then
5086 -- This code can only be executed when not instantiating, because
5087 -- in the copy made for an instantiation, the compilation unit
5088 -- node has disappeared at the point that a stub is replaced by
5089 -- its proper body.
5091 pragma Assert (not Instantiating);
5093 Set_Context_Items (New_N,
5094 Copy_Generic_List (Context_Items (N), New_N));
5096 Set_Unit (New_N,
5097 Copy_Generic_Node (Unit (N), New_N, False));
5099 Set_First_Inlined_Subprogram (New_N,
5100 Copy_Generic_Node
5101 (First_Inlined_Subprogram (N), New_N, False));
5103 Set_Aux_Decls_Node (New_N,
5104 Copy_Generic_Node (Aux_Decls_Node (N), New_N, False));
5106 -- For an assignment node, the assignment is known to be semantically
5107 -- legal if we are instantiating the template. This avoids incorrect
5108 -- diagnostics in generated code.
5110 elsif Nkind (N) = N_Assignment_Statement then
5112 -- Copy name and expression fields in usual manner
5114 Set_Name (New_N,
5115 Copy_Generic_Node (Name (N), New_N, Instantiating));
5117 Set_Expression (New_N,
5118 Copy_Generic_Node (Expression (N), New_N, Instantiating));
5120 if Instantiating then
5121 Set_Assignment_OK (Name (New_N), True);
5122 end if;
5124 elsif Nkind (N) = N_Aggregate
5125 or else Nkind (N) = N_Extension_Aggregate
5126 then
5128 if not Instantiating then
5129 Set_Associated_Node (N, New_N);
5131 else
5132 if Present (Get_Associated_Node (N))
5133 and then Nkind (Get_Associated_Node (N)) = Nkind (N)
5134 then
5135 -- In the generic the aggregate has some composite type. If at
5136 -- the point of instantiation the type has a private view,
5137 -- install the full view (and that of its ancestors, if any).
5139 declare
5140 T : Entity_Id := (Etype (Get_Associated_Node (New_N)));
5141 Rt : Entity_Id;
5143 begin
5144 if Present (T)
5145 and then Is_Private_Type (T)
5146 then
5147 Switch_View (T);
5148 end if;
5150 if Present (T)
5151 and then Is_Tagged_Type (T)
5152 and then Is_Derived_Type (T)
5153 then
5154 Rt := Root_Type (T);
5156 loop
5157 T := Etype (T);
5159 if Is_Private_Type (T) then
5160 Switch_View (T);
5161 end if;
5163 exit when T = Rt;
5164 end loop;
5165 end if;
5166 end;
5167 end if;
5168 end if;
5170 -- Do not copy the associated node, which points to
5171 -- the generic copy of the aggregate.
5173 declare
5174 use Atree.Unchecked_Access;
5175 -- This code section is part of the implementation of an untyped
5176 -- tree traversal, so it needs direct access to node fields.
5178 begin
5179 Set_Field1 (New_N, Copy_Generic_Descendant (Field1 (N)));
5180 Set_Field2 (New_N, Copy_Generic_Descendant (Field2 (N)));
5181 Set_Field3 (New_N, Copy_Generic_Descendant (Field3 (N)));
5182 Set_Field5 (New_N, Copy_Generic_Descendant (Field5 (N)));
5183 end;
5185 -- Allocators do not have an identifier denoting the access type,
5186 -- so we must locate it through the expression to check whether
5187 -- the views are consistent.
5189 elsif Nkind (N) = N_Allocator
5190 and then Nkind (Expression (N)) = N_Qualified_Expression
5191 and then Is_Entity_Name (Subtype_Mark (Expression (N)))
5192 and then Instantiating
5193 then
5194 declare
5195 T : constant Node_Id :=
5196 Get_Associated_Node (Subtype_Mark (Expression (N)));
5197 Acc_T : Entity_Id;
5199 begin
5200 if Present (T) then
5201 -- Retrieve the allocator node in the generic copy
5203 Acc_T := Etype (Parent (Parent (T)));
5204 if Present (Acc_T)
5205 and then Is_Private_Type (Acc_T)
5206 then
5207 Switch_View (Acc_T);
5208 end if;
5209 end if;
5211 Copy_Descendants;
5212 end;
5214 -- For a proper body, we must catch the case of a proper body that
5215 -- replaces a stub. This represents the point at which a separate
5216 -- compilation unit, and hence template file, may be referenced, so
5217 -- we must make a new source instantiation entry for the template
5218 -- of the subunit, and ensure that all nodes in the subunit are
5219 -- adjusted using this new source instantiation entry.
5221 elsif Nkind (N) in N_Proper_Body then
5222 declare
5223 Save_Adjustment : constant Sloc_Adjustment := S_Adjustment;
5225 begin
5226 if Instantiating and then Was_Originally_Stub (N) then
5227 Create_Instantiation_Source
5228 (Instantiation_Node,
5229 Defining_Entity (N),
5230 False,
5231 S_Adjustment);
5232 end if;
5234 -- Now copy the fields of the proper body, using the new
5235 -- adjustment factor if one was needed as per test above.
5237 Copy_Descendants;
5239 -- Restore the original adjustment factor in case changed
5241 S_Adjustment := Save_Adjustment;
5242 end;
5244 -- Don't copy Ident or Comment pragmas, since the comment belongs
5245 -- to the generic unit, not to the instantiating unit.
5247 elsif Nkind (N) = N_Pragma
5248 and then Instantiating
5249 then
5250 declare
5251 Prag_Id : constant Pragma_Id := Get_Pragma_Id (Chars (N));
5253 begin
5254 if Prag_Id = Pragma_Ident
5255 or else Prag_Id = Pragma_Comment
5256 then
5257 New_N := Make_Null_Statement (Sloc (N));
5259 else
5260 Copy_Descendants;
5261 end if;
5262 end;
5264 elsif Nkind (N) = N_Integer_Literal
5265 or else Nkind (N) = N_Real_Literal
5266 then
5267 -- No descendant fields need traversing
5269 null;
5271 -- For the remaining nodes, copy recursively their descendants
5273 else
5274 Copy_Descendants;
5276 if Instantiating
5277 and then Nkind (N) = N_Subprogram_Body
5278 then
5279 Set_Generic_Parent (Specification (New_N), N);
5280 end if;
5281 end if;
5283 return New_N;
5284 end Copy_Generic_Node;
5286 ----------------------------
5287 -- Denotes_Formal_Package --
5288 ----------------------------
5290 function Denotes_Formal_Package
5291 (Pack : Entity_Id;
5292 On_Exit : Boolean := False) return Boolean
5294 Par : Entity_Id;
5295 Scop : constant Entity_Id := Scope (Pack);
5296 E : Entity_Id;
5298 begin
5299 if On_Exit then
5300 Par :=
5301 Instance_Envs.Table
5302 (Instance_Envs.Last).Instantiated_Parent.Act_Id;
5303 else
5304 Par := Current_Instantiated_Parent.Act_Id;
5305 end if;
5307 if Ekind (Scop) = E_Generic_Package
5308 or else Nkind (Unit_Declaration_Node (Scop)) =
5309 N_Generic_Subprogram_Declaration
5310 then
5311 return True;
5313 elsif Nkind (Parent (Pack)) = N_Formal_Package_Declaration then
5314 return True;
5316 elsif No (Par) then
5317 return False;
5319 else
5320 -- Check whether this package is associated with a formal
5321 -- package of the enclosing instantiation. Iterate over the
5322 -- list of renamings.
5324 E := First_Entity (Par);
5325 while Present (E) loop
5326 if Ekind (E) /= E_Package
5327 or else Nkind (Parent (E)) /= N_Package_Renaming_Declaration
5328 then
5329 null;
5330 elsif Renamed_Object (E) = Par then
5331 return False;
5333 elsif Renamed_Object (E) = Pack then
5334 return True;
5335 end if;
5337 Next_Entity (E);
5338 end loop;
5340 return False;
5341 end if;
5342 end Denotes_Formal_Package;
5344 -----------------
5345 -- End_Generic --
5346 -----------------
5348 procedure End_Generic is
5349 begin
5350 -- ??? More things could be factored out in this
5351 -- routine. Should probably be done at a later stage.
5353 Inside_A_Generic := Generic_Flags.Table (Generic_Flags.Last);
5354 Generic_Flags.Decrement_Last;
5356 Expander_Mode_Restore;
5357 end End_Generic;
5359 ----------------------
5360 -- Find_Actual_Type --
5361 ----------------------
5363 function Find_Actual_Type
5364 (Typ : Entity_Id;
5365 Gen_Scope : Entity_Id) return Entity_Id
5367 T : Entity_Id;
5369 begin
5370 if not Is_Child_Unit (Gen_Scope) then
5371 return Get_Instance_Of (Typ);
5373 elsif not Is_Generic_Type (Typ)
5374 or else Scope (Typ) = Gen_Scope
5375 then
5376 return Get_Instance_Of (Typ);
5378 else
5379 T := Current_Entity (Typ);
5380 while Present (T) loop
5381 if In_Open_Scopes (Scope (T)) then
5382 return T;
5383 end if;
5385 T := Homonym (T);
5386 end loop;
5388 return Typ;
5389 end if;
5390 end Find_Actual_Type;
5392 ----------------------------
5393 -- Freeze_Subprogram_Body --
5394 ----------------------------
5396 procedure Freeze_Subprogram_Body
5397 (Inst_Node : Node_Id;
5398 Gen_Body : Node_Id;
5399 Pack_Id : Entity_Id)
5401 F_Node : Node_Id;
5402 Gen_Unit : constant Entity_Id := Get_Generic_Entity (Inst_Node);
5403 Par : constant Entity_Id := Scope (Gen_Unit);
5404 Enc_G : Entity_Id;
5405 Enc_I : Node_Id;
5406 E_G_Id : Entity_Id;
5408 function Earlier (N1, N2 : Node_Id) return Boolean;
5409 -- Yields True if N1 and N2 appear in the same compilation unit,
5410 -- ignoring subunits, and if N1 is to the left of N2 in a left-to-right
5411 -- traversal of the tree for the unit.
5413 function Enclosing_Body (N : Node_Id) return Node_Id;
5414 -- Find innermost package body that encloses the given node, and which
5415 -- is not a compilation unit. Freeze nodes for the instance, or for its
5416 -- enclosing body, may be inserted after the enclosing_body of the
5417 -- generic unit.
5419 function Package_Freeze_Node (B : Node_Id) return Node_Id;
5420 -- Find entity for given package body, and locate or create a freeze
5421 -- node for it.
5423 function True_Parent (N : Node_Id) return Node_Id;
5424 -- For a subunit, return parent of corresponding stub
5426 -------------
5427 -- Earlier --
5428 -------------
5430 function Earlier (N1, N2 : Node_Id) return Boolean is
5431 D1 : Integer := 0;
5432 D2 : Integer := 0;
5433 P1 : Node_Id := N1;
5434 P2 : Node_Id := N2;
5436 procedure Find_Depth (P : in out Node_Id; D : in out Integer);
5437 -- Find distance from given node to enclosing compilation unit
5439 ----------------
5440 -- Find_Depth --
5441 ----------------
5443 procedure Find_Depth (P : in out Node_Id; D : in out Integer) is
5444 begin
5445 while Present (P)
5446 and then Nkind (P) /= N_Compilation_Unit
5447 loop
5448 P := True_Parent (P);
5449 D := D + 1;
5450 end loop;
5451 end Find_Depth;
5453 -- Start of procesing for Earlier
5455 begin
5456 Find_Depth (P1, D1);
5457 Find_Depth (P2, D2);
5459 if P1 /= P2 then
5460 return False;
5461 else
5462 P1 := N1;
5463 P2 := N2;
5464 end if;
5466 while D1 > D2 loop
5467 P1 := True_Parent (P1);
5468 D1 := D1 - 1;
5469 end loop;
5471 while D2 > D1 loop
5472 P2 := True_Parent (P2);
5473 D2 := D2 - 1;
5474 end loop;
5476 -- At this point P1 and P2 are at the same distance from the root.
5477 -- We examine their parents until we find a common declarative
5478 -- list, at which point we can establish their relative placement
5479 -- by comparing their ultimate slocs. If we reach the root,
5480 -- N1 and N2 do not descend from the same declarative list (e.g.
5481 -- one is nested in the declarative part and the other is in a block
5482 -- in the statement part) and the earlier one is already frozen.
5484 while not Is_List_Member (P1)
5485 or else not Is_List_Member (P2)
5486 or else List_Containing (P1) /= List_Containing (P2)
5487 loop
5488 P1 := True_Parent (P1);
5489 P2 := True_Parent (P2);
5491 if Nkind (Parent (P1)) = N_Subunit then
5492 P1 := Corresponding_Stub (Parent (P1));
5493 end if;
5495 if Nkind (Parent (P2)) = N_Subunit then
5496 P2 := Corresponding_Stub (Parent (P2));
5497 end if;
5499 if P1 = P2 then
5500 return False;
5501 end if;
5502 end loop;
5504 return
5505 Top_Level_Location (Sloc (P1)) < Top_Level_Location (Sloc (P2));
5506 end Earlier;
5508 --------------------
5509 -- Enclosing_Body --
5510 --------------------
5512 function Enclosing_Body (N : Node_Id) return Node_Id is
5513 P : Node_Id := Parent (N);
5515 begin
5516 while Present (P)
5517 and then Nkind (Parent (P)) /= N_Compilation_Unit
5518 loop
5519 if Nkind (P) = N_Package_Body then
5521 if Nkind (Parent (P)) = N_Subunit then
5522 return Corresponding_Stub (Parent (P));
5523 else
5524 return P;
5525 end if;
5526 end if;
5528 P := True_Parent (P);
5529 end loop;
5531 return Empty;
5532 end Enclosing_Body;
5534 -------------------------
5535 -- Package_Freeze_Node --
5536 -------------------------
5538 function Package_Freeze_Node (B : Node_Id) return Node_Id is
5539 Id : Entity_Id;
5541 begin
5542 if Nkind (B) = N_Package_Body then
5543 Id := Corresponding_Spec (B);
5545 else pragma Assert (Nkind (B) = N_Package_Body_Stub);
5546 Id := Corresponding_Spec (Proper_Body (Unit (Library_Unit (B))));
5547 end if;
5549 Ensure_Freeze_Node (Id);
5550 return Freeze_Node (Id);
5551 end Package_Freeze_Node;
5553 -----------------
5554 -- True_Parent --
5555 -----------------
5557 function True_Parent (N : Node_Id) return Node_Id is
5558 begin
5559 if Nkind (Parent (N)) = N_Subunit then
5560 return Parent (Corresponding_Stub (Parent (N)));
5561 else
5562 return Parent (N);
5563 end if;
5564 end True_Parent;
5566 -- Start of processing of Freeze_Subprogram_Body
5568 begin
5569 -- If the instance and the generic body appear within the same
5570 -- unit, and the instance preceeds the generic, the freeze node for
5571 -- the instance must appear after that of the generic. If the generic
5572 -- is nested within another instance I2, then current instance must
5573 -- be frozen after I2. In both cases, the freeze nodes are those of
5574 -- enclosing packages. Otherwise, the freeze node is placed at the end
5575 -- of the current declarative part.
5577 Enc_G := Enclosing_Body (Gen_Body);
5578 Enc_I := Enclosing_Body (Inst_Node);
5579 Ensure_Freeze_Node (Pack_Id);
5580 F_Node := Freeze_Node (Pack_Id);
5582 if Is_Generic_Instance (Par)
5583 and then Present (Freeze_Node (Par))
5584 and then
5585 In_Same_Declarative_Part (Freeze_Node (Par), Inst_Node)
5586 then
5587 if ABE_Is_Certain (Get_Package_Instantiation_Node (Par)) then
5589 -- The parent was a premature instantiation. Insert freeze
5590 -- node at the end the current declarative part.
5592 Insert_After_Last_Decl (Inst_Node, F_Node);
5594 else
5595 Insert_After (Freeze_Node (Par), F_Node);
5596 end if;
5598 -- The body enclosing the instance should be frozen after the body
5599 -- that includes the generic, because the body of the instance may
5600 -- make references to entities therein. If the two are not in the
5601 -- same declarative part, or if the one enclosing the instance is
5602 -- frozen already, freeze the instance at the end of the current
5603 -- declarative part.
5605 elsif Is_Generic_Instance (Par)
5606 and then Present (Freeze_Node (Par))
5607 and then Present (Enc_I)
5608 then
5609 if In_Same_Declarative_Part (Freeze_Node (Par), Enc_I)
5610 or else
5611 (Nkind (Enc_I) = N_Package_Body
5612 and then
5613 In_Same_Declarative_Part (Freeze_Node (Par), Parent (Enc_I)))
5614 then
5615 -- The enclosing package may contain several instances. Rather
5616 -- than computing the earliest point at which to insert its
5617 -- freeze node, we place it at the end of the declarative part
5618 -- of the parent of the generic.
5620 Insert_After_Last_Decl
5621 (Freeze_Node (Par), Package_Freeze_Node (Enc_I));
5622 end if;
5624 Insert_After_Last_Decl (Inst_Node, F_Node);
5626 elsif Present (Enc_G)
5627 and then Present (Enc_I)
5628 and then Enc_G /= Enc_I
5629 and then Earlier (Inst_Node, Gen_Body)
5630 then
5631 if Nkind (Enc_G) = N_Package_Body then
5632 E_G_Id := Corresponding_Spec (Enc_G);
5633 else pragma Assert (Nkind (Enc_G) = N_Package_Body_Stub);
5634 E_G_Id :=
5635 Corresponding_Spec (Proper_Body (Unit (Library_Unit (Enc_G))));
5636 end if;
5638 -- Freeze package that encloses instance, and place node after
5639 -- package that encloses generic. If enclosing package is already
5640 -- frozen we have to assume it is at the proper place. This may
5641 -- be a potential ABE that requires dynamic checking.
5643 Insert_After_Last_Decl (Enc_G, Package_Freeze_Node (Enc_I));
5645 -- Freeze enclosing subunit before instance
5647 Ensure_Freeze_Node (E_G_Id);
5649 if not Is_List_Member (Freeze_Node (E_G_Id)) then
5650 Insert_After (Enc_G, Freeze_Node (E_G_Id));
5651 end if;
5653 Insert_After_Last_Decl (Inst_Node, F_Node);
5655 else
5656 -- If none of the above, insert freeze node at the end of the
5657 -- current declarative part.
5659 Insert_After_Last_Decl (Inst_Node, F_Node);
5660 end if;
5661 end Freeze_Subprogram_Body;
5663 ----------------
5664 -- Get_Gen_Id --
5665 ----------------
5667 function Get_Gen_Id (E : Assoc_Ptr) return Entity_Id is
5668 begin
5669 return Generic_Renamings.Table (E).Gen_Id;
5670 end Get_Gen_Id;
5672 ---------------------
5673 -- Get_Instance_Of --
5674 ---------------------
5676 function Get_Instance_Of (A : Entity_Id) return Entity_Id is
5677 Res : constant Assoc_Ptr := Generic_Renamings_HTable.Get (A);
5679 begin
5680 if Res /= Assoc_Null then
5681 return Generic_Renamings.Table (Res).Act_Id;
5682 else
5683 -- On exit, entity is not instantiated: not a generic parameter,
5684 -- or else parameter of an inner generic unit.
5686 return A;
5687 end if;
5688 end Get_Instance_Of;
5690 ------------------------------------
5691 -- Get_Package_Instantiation_Node --
5692 ------------------------------------
5694 function Get_Package_Instantiation_Node (A : Entity_Id) return Node_Id is
5695 Decl : Node_Id := Unit_Declaration_Node (A);
5696 Inst : Node_Id;
5698 begin
5699 -- If the instantiation is a compilation unit that does not need a
5700 -- body then the instantiation node has been rewritten as a package
5701 -- declaration for the instance, and we return the original node.
5703 -- If it is a compilation unit and the instance node has not been
5704 -- rewritten, then it is still the unit of the compilation. Finally,
5705 -- if a body is present, this is a parent of the main unit whose body
5706 -- has been compiled for inlining purposes, and the instantiation node
5707 -- has been rewritten with the instance body.
5709 -- Otherwise the instantiation node appears after the declaration.
5710 -- If the entity is a formal package, the declaration may have been
5711 -- rewritten as a generic declaration (in the case of a formal with a
5712 -- box) or left as a formal package declaration if it has actuals, and
5713 -- is found with a forward search.
5715 if Nkind (Parent (Decl)) = N_Compilation_Unit then
5716 if Nkind (Decl) = N_Package_Declaration
5717 and then Present (Corresponding_Body (Decl))
5718 then
5719 Decl := Unit_Declaration_Node (Corresponding_Body (Decl));
5720 end if;
5722 if Nkind (Original_Node (Decl)) = N_Package_Instantiation then
5723 return Original_Node (Decl);
5724 else
5725 return Unit (Parent (Decl));
5726 end if;
5728 elsif Nkind (Decl) = N_Generic_Package_Declaration
5729 and then Nkind (Original_Node (Decl)) = N_Formal_Package_Declaration
5730 then
5731 return Original_Node (Decl);
5733 else
5734 Inst := Next (Decl);
5735 while Nkind (Inst) /= N_Package_Instantiation
5736 and then Nkind (Inst) /= N_Formal_Package_Declaration
5737 loop
5738 Next (Inst);
5739 end loop;
5741 return Inst;
5742 end if;
5743 end Get_Package_Instantiation_Node;
5745 ------------------------
5746 -- Has_Been_Exchanged --
5747 ------------------------
5749 function Has_Been_Exchanged (E : Entity_Id) return Boolean is
5750 Next : Elmt_Id := First_Elmt (Exchanged_Views);
5752 begin
5753 while Present (Next) loop
5754 if Full_View (Node (Next)) = E then
5755 return True;
5756 end if;
5758 Next_Elmt (Next);
5759 end loop;
5761 return False;
5762 end Has_Been_Exchanged;
5764 ----------
5765 -- Hash --
5766 ----------
5768 function Hash (F : Entity_Id) return HTable_Range is
5769 begin
5770 return HTable_Range (F mod HTable_Size);
5771 end Hash;
5773 ------------------------
5774 -- Hide_Current_Scope --
5775 ------------------------
5777 procedure Hide_Current_Scope is
5778 C : constant Entity_Id := Current_Scope;
5779 E : Entity_Id;
5781 begin
5782 Set_Is_Hidden_Open_Scope (C);
5783 E := First_Entity (C);
5785 while Present (E) loop
5786 if Is_Immediately_Visible (E) then
5787 Set_Is_Immediately_Visible (E, False);
5788 Append_Elmt (E, Hidden_Entities);
5789 end if;
5791 Next_Entity (E);
5792 end loop;
5794 -- Make the scope name invisible as well. This is necessary, but
5795 -- might conflict with calls to Rtsfind later on, in case the scope
5796 -- is a predefined one. There is no clean solution to this problem, so
5797 -- for now we depend on the user not redefining Standard itself in one
5798 -- of the parent units.
5800 if Is_Immediately_Visible (C)
5801 and then C /= Standard_Standard
5802 then
5803 Set_Is_Immediately_Visible (C, False);
5804 Append_Elmt (C, Hidden_Entities);
5805 end if;
5807 end Hide_Current_Scope;
5809 --------------
5810 -- Init_Env --
5811 --------------
5813 procedure Init_Env is
5814 Saved : Instance_Env;
5816 begin
5817 Saved.Ada_Version := Ada_Version;
5818 Saved.Instantiated_Parent := Current_Instantiated_Parent;
5819 Saved.Exchanged_Views := Exchanged_Views;
5820 Saved.Hidden_Entities := Hidden_Entities;
5821 Saved.Current_Sem_Unit := Current_Sem_Unit;
5822 Saved.Parent_Unit_Visible := Parent_Unit_Visible;
5823 Instance_Envs.Increment_Last;
5824 Instance_Envs.Table (Instance_Envs.Last) := Saved;
5826 Exchanged_Views := New_Elmt_List;
5827 Hidden_Entities := New_Elmt_List;
5829 -- Make dummy entry for Instantiated parent. If generic unit is
5830 -- legal, this is set properly in Set_Instance_Env.
5832 Current_Instantiated_Parent :=
5833 (Current_Scope, Current_Scope, Assoc_Null);
5834 end Init_Env;
5836 ------------------------------
5837 -- In_Same_Declarative_Part --
5838 ------------------------------
5840 function In_Same_Declarative_Part
5841 (F_Node : Node_Id;
5842 Inst : Node_Id) return Boolean
5844 Decls : constant Node_Id := Parent (F_Node);
5845 Nod : Node_Id := Parent (Inst);
5847 begin
5848 while Present (Nod) loop
5849 if Nod = Decls then
5850 return True;
5852 elsif Nkind (Nod) = N_Subprogram_Body
5853 or else Nkind (Nod) = N_Package_Body
5854 or else Nkind (Nod) = N_Task_Body
5855 or else Nkind (Nod) = N_Protected_Body
5856 or else Nkind (Nod) = N_Block_Statement
5857 then
5858 return False;
5860 elsif Nkind (Nod) = N_Subunit then
5861 Nod := Corresponding_Stub (Nod);
5863 elsif Nkind (Nod) = N_Compilation_Unit then
5864 return False;
5865 else
5866 Nod := Parent (Nod);
5867 end if;
5868 end loop;
5870 return False;
5871 end In_Same_Declarative_Part;
5873 ---------------------
5874 -- In_Main_Context --
5875 ---------------------
5877 function In_Main_Context (E : Entity_Id) return Boolean is
5878 Context : List_Id;
5879 Clause : Node_Id;
5880 Nam : Node_Id;
5882 begin
5883 if not Is_Compilation_Unit (E)
5884 or else Ekind (E) /= E_Package
5885 or else In_Private_Part (E)
5886 then
5887 return False;
5888 end if;
5890 Context := Context_Items (Cunit (Main_Unit));
5892 Clause := First (Context);
5893 while Present (Clause) loop
5894 if Nkind (Clause) = N_With_Clause then
5895 Nam := Name (Clause);
5897 -- If the current scope is part of the context of the main unit,
5898 -- analysis of the corresponding with_clause is not complete, and
5899 -- the entity is not set. We use the Chars field directly, which
5900 -- might produce false positives in rare cases, but guarantees
5901 -- that we produce all the instance bodies we will need.
5903 if (Nkind (Nam) = N_Identifier
5904 and then Chars (Nam) = Chars (E))
5905 or else (Nkind (Nam) = N_Selected_Component
5906 and then Chars (Selector_Name (Nam)) = Chars (E))
5907 then
5908 return True;
5909 end if;
5910 end if;
5912 Next (Clause);
5913 end loop;
5915 return False;
5916 end In_Main_Context;
5918 ---------------------
5919 -- Inherit_Context --
5920 ---------------------
5922 procedure Inherit_Context (Gen_Decl : Node_Id; Inst : Node_Id) is
5923 Current_Context : List_Id;
5924 Current_Unit : Node_Id;
5925 Item : Node_Id;
5926 New_I : Node_Id;
5928 begin
5929 if Nkind (Parent (Gen_Decl)) = N_Compilation_Unit then
5931 -- The inherited context is attached to the enclosing compilation
5932 -- unit. This is either the main unit, or the declaration for the
5933 -- main unit (in case the instantation appears within the package
5934 -- declaration and the main unit is its body).
5936 Current_Unit := Parent (Inst);
5937 while Present (Current_Unit)
5938 and then Nkind (Current_Unit) /= N_Compilation_Unit
5939 loop
5940 Current_Unit := Parent (Current_Unit);
5941 end loop;
5943 Current_Context := Context_Items (Current_Unit);
5945 Item := First (Context_Items (Parent (Gen_Decl)));
5946 while Present (Item) loop
5947 if Nkind (Item) = N_With_Clause then
5948 New_I := New_Copy (Item);
5949 Set_Implicit_With (New_I, True);
5950 Append (New_I, Current_Context);
5951 end if;
5953 Next (Item);
5954 end loop;
5955 end if;
5956 end Inherit_Context;
5958 ----------------
5959 -- Initialize --
5960 ----------------
5962 procedure Initialize is
5963 begin
5964 Generic_Renamings.Init;
5965 Instance_Envs.Init;
5966 Generic_Flags.Init;
5967 Generic_Renamings_HTable.Reset;
5968 Circularity_Detected := False;
5969 Exchanged_Views := No_Elist;
5970 Hidden_Entities := No_Elist;
5971 end Initialize;
5973 ----------------------------
5974 -- Insert_After_Last_Decl --
5975 ----------------------------
5977 procedure Insert_After_Last_Decl (N : Node_Id; F_Node : Node_Id) is
5978 L : List_Id := List_Containing (N);
5979 P : constant Node_Id := Parent (L);
5981 begin
5982 if not Is_List_Member (F_Node) then
5983 if Nkind (P) = N_Package_Specification
5984 and then L = Visible_Declarations (P)
5985 and then Present (Private_Declarations (P))
5986 and then not Is_Empty_List (Private_Declarations (P))
5987 then
5988 L := Private_Declarations (P);
5989 end if;
5991 Insert_After (Last (L), F_Node);
5992 end if;
5993 end Insert_After_Last_Decl;
5995 ------------------
5996 -- Install_Body --
5997 ------------------
5999 procedure Install_Body
6000 (Act_Body : Node_Id;
6001 N : Node_Id;
6002 Gen_Body : Node_Id;
6003 Gen_Decl : Node_Id)
6005 Act_Id : constant Entity_Id := Corresponding_Spec (Act_Body);
6006 Act_Unit : constant Node_Id := Unit (Cunit (Get_Source_Unit (N)));
6007 Gen_Id : constant Entity_Id := Corresponding_Spec (Gen_Body);
6008 Par : constant Entity_Id := Scope (Gen_Id);
6009 Gen_Unit : constant Node_Id :=
6010 Unit (Cunit (Get_Source_Unit (Gen_Decl)));
6011 Orig_Body : Node_Id := Gen_Body;
6012 F_Node : Node_Id;
6013 Body_Unit : Node_Id;
6015 Must_Delay : Boolean;
6017 function Enclosing_Subp (Id : Entity_Id) return Entity_Id;
6018 -- Find subprogram (if any) that encloses instance and/or generic body
6020 function True_Sloc (N : Node_Id) return Source_Ptr;
6021 -- If the instance is nested inside a generic unit, the Sloc of the
6022 -- instance indicates the place of the original definition, not the
6023 -- point of the current enclosing instance. Pending a better usage of
6024 -- Slocs to indicate instantiation places, we determine the place of
6025 -- origin of a node by finding the maximum sloc of any ancestor node.
6026 -- Why is this not equivalent to Top_Level_Location ???
6028 --------------------
6029 -- Enclosing_Subp --
6030 --------------------
6032 function Enclosing_Subp (Id : Entity_Id) return Entity_Id is
6033 Scop : Entity_Id := Scope (Id);
6035 begin
6036 while Scop /= Standard_Standard
6037 and then not Is_Overloadable (Scop)
6038 loop
6039 Scop := Scope (Scop);
6040 end loop;
6042 return Scop;
6043 end Enclosing_Subp;
6045 ---------------
6046 -- True_Sloc --
6047 ---------------
6049 function True_Sloc (N : Node_Id) return Source_Ptr is
6050 Res : Source_Ptr;
6051 N1 : Node_Id;
6053 begin
6054 Res := Sloc (N);
6055 N1 := N;
6056 while Present (N1) and then N1 /= Act_Unit loop
6057 if Sloc (N1) > Res then
6058 Res := Sloc (N1);
6059 end if;
6061 N1 := Parent (N1);
6062 end loop;
6064 return Res;
6065 end True_Sloc;
6067 -- Start of processing for Install_Body
6069 begin
6070 -- If the body is a subunit, the freeze point is the corresponding
6071 -- stub in the current compilation, not the subunit itself.
6073 if Nkind (Parent (Gen_Body)) = N_Subunit then
6074 Orig_Body := Corresponding_Stub (Parent (Gen_Body));
6075 else
6076 Orig_Body := Gen_Body;
6077 end if;
6079 Body_Unit := Unit (Cunit (Get_Source_Unit (Orig_Body)));
6081 -- If the instantiation and the generic definition appear in the
6082 -- same package declaration, this is an early instantiation.
6083 -- If they appear in the same declarative part, it is an early
6084 -- instantiation only if the generic body appears textually later,
6085 -- and the generic body is also in the main unit.
6087 -- If instance is nested within a subprogram, and the generic body is
6088 -- not, the instance is delayed because the enclosing body is. If
6089 -- instance and body are within the same scope, or the same sub-
6090 -- program body, indicate explicitly that the instance is delayed.
6092 Must_Delay :=
6093 (Gen_Unit = Act_Unit
6094 and then ((Nkind (Gen_Unit) = N_Package_Declaration)
6095 or else Nkind (Gen_Unit) = N_Generic_Package_Declaration
6096 or else (Gen_Unit = Body_Unit
6097 and then True_Sloc (N) < Sloc (Orig_Body)))
6098 and then Is_In_Main_Unit (Gen_Unit)
6099 and then (Scope (Act_Id) = Scope (Gen_Id)
6100 or else
6101 Enclosing_Subp (Act_Id) = Enclosing_Subp (Gen_Id)));
6103 -- If this is an early instantiation, the freeze node is placed after
6104 -- the generic body. Otherwise, if the generic appears in an instance,
6105 -- we cannot freeze the current instance until the outer one is frozen.
6106 -- This is only relevant if the current instance is nested within some
6107 -- inner scope not itself within the outer instance. If this scope is
6108 -- a package body in the same declarative part as the outer instance,
6109 -- then that body needs to be frozen after the outer instance. Finally,
6110 -- if no delay is needed, we place the freeze node at the end of the
6111 -- current declarative part.
6113 if Expander_Active then
6114 Ensure_Freeze_Node (Act_Id);
6115 F_Node := Freeze_Node (Act_Id);
6117 if Must_Delay then
6118 Insert_After (Orig_Body, F_Node);
6120 elsif Is_Generic_Instance (Par)
6121 and then Present (Freeze_Node (Par))
6122 and then Scope (Act_Id) /= Par
6123 then
6124 -- Freeze instance of inner generic after instance of enclosing
6125 -- generic.
6127 if In_Same_Declarative_Part (Freeze_Node (Par), N) then
6128 Insert_After (Freeze_Node (Par), F_Node);
6130 -- Freeze package enclosing instance of inner generic after
6131 -- instance of enclosing generic.
6133 elsif Nkind (Parent (N)) = N_Package_Body
6134 and then In_Same_Declarative_Part (Freeze_Node (Par), Parent (N))
6135 then
6137 declare
6138 Enclosing : constant Entity_Id :=
6139 Corresponding_Spec (Parent (N));
6141 begin
6142 Insert_After_Last_Decl (N, F_Node);
6143 Ensure_Freeze_Node (Enclosing);
6145 if not Is_List_Member (Freeze_Node (Enclosing)) then
6146 Insert_After (Freeze_Node (Par), Freeze_Node (Enclosing));
6147 end if;
6148 end;
6150 else
6151 Insert_After_Last_Decl (N, F_Node);
6152 end if;
6154 else
6155 Insert_After_Last_Decl (N, F_Node);
6156 end if;
6157 end if;
6159 Set_Is_Frozen (Act_Id);
6160 Insert_Before (N, Act_Body);
6161 Mark_Rewrite_Insertion (Act_Body);
6162 end Install_Body;
6164 --------------------
6165 -- Install_Parent --
6166 --------------------
6168 procedure Install_Parent (P : Entity_Id; In_Body : Boolean := False) is
6169 Ancestors : constant Elist_Id := New_Elmt_List;
6170 S : constant Entity_Id := Current_Scope;
6171 Inst_Par : Entity_Id;
6172 First_Par : Entity_Id;
6173 Inst_Node : Node_Id;
6174 Gen_Par : Entity_Id;
6175 First_Gen : Entity_Id;
6176 Elmt : Elmt_Id;
6178 procedure Install_Formal_Packages (Par : Entity_Id);
6179 -- If any of the formals of the parent are formal packages with box,
6180 -- their formal parts are visible in the parent and thus in the child
6181 -- unit as well. Analogous to what is done in Check_Generic_Actuals
6182 -- for the unit itself.
6184 procedure Install_Noninstance_Specs (Par : Entity_Id);
6185 -- Install the scopes of noninstance parent units ending with Par
6187 procedure Install_Spec (Par : Entity_Id);
6188 -- The child unit is within the declarative part of the parent, so
6189 -- the declarations within the parent are immediately visible.
6191 -----------------------------
6192 -- Install_Formal_Packages --
6193 -----------------------------
6195 procedure Install_Formal_Packages (Par : Entity_Id) is
6196 E : Entity_Id;
6198 begin
6199 E := First_Entity (Par);
6200 while Present (E) loop
6201 if Ekind (E) = E_Package
6202 and then Nkind (Parent (E)) = N_Package_Renaming_Declaration
6203 then
6204 -- If this is the renaming for the parent instance, done
6206 if Renamed_Object (E) = Par then
6207 exit;
6209 -- The visibility of a formal of an enclosing generic is
6210 -- already correct.
6212 elsif Denotes_Formal_Package (E) then
6213 null;
6215 elsif Present (Associated_Formal_Package (E))
6216 and then Box_Present (Parent (Associated_Formal_Package (E)))
6217 then
6218 Check_Generic_Actuals (Renamed_Object (E), True);
6219 Set_Is_Hidden (E, False);
6220 end if;
6221 end if;
6223 Next_Entity (E);
6224 end loop;
6225 end Install_Formal_Packages;
6227 -------------------------------
6228 -- Install_Noninstance_Specs --
6229 -------------------------------
6231 procedure Install_Noninstance_Specs (Par : Entity_Id) is
6232 begin
6233 if Present (Par)
6234 and then Par /= Standard_Standard
6235 and then not In_Open_Scopes (Par)
6236 then
6237 Install_Noninstance_Specs (Scope (Par));
6238 Install_Spec (Par);
6239 end if;
6240 end Install_Noninstance_Specs;
6242 ------------------
6243 -- Install_Spec --
6244 ------------------
6246 procedure Install_Spec (Par : Entity_Id) is
6247 Spec : constant Node_Id :=
6248 Specification (Unit_Declaration_Node (Par));
6250 begin
6251 if not Is_Child_Unit (Par) then
6252 Parent_Unit_Visible := Is_Immediately_Visible (Par);
6253 end if;
6255 New_Scope (Par);
6256 Set_Is_Immediately_Visible (Par);
6257 Install_Visible_Declarations (Par);
6258 Install_Private_Declarations (Par);
6259 Set_Use (Visible_Declarations (Spec));
6260 Set_Use (Private_Declarations (Spec));
6261 end Install_Spec;
6263 -- Start of processing for Install_Parent
6265 begin
6266 -- We need to install the parent instance to compile the instantiation
6267 -- of the child, but the child instance must appear in the current
6268 -- scope. Given that we cannot place the parent above the current
6269 -- scope in the scope stack, we duplicate the current scope and unstack
6270 -- both after the instantiation is complete.
6272 -- If the parent is itself the instantiation of a child unit, we must
6273 -- also stack the instantiation of its parent, and so on. Each such
6274 -- ancestor is the prefix of the name in a prior instantiation.
6276 -- If this is a nested instance, the parent unit itself resolves to
6277 -- a renaming of the parent instance, whose declaration we need.
6279 -- Finally, the parent may be a generic (not an instance) when the
6280 -- child unit appears as a formal package.
6282 Inst_Par := P;
6284 if Present (Renamed_Entity (Inst_Par)) then
6285 Inst_Par := Renamed_Entity (Inst_Par);
6286 end if;
6288 First_Par := Inst_Par;
6290 Gen_Par :=
6291 Generic_Parent (Specification (Unit_Declaration_Node (Inst_Par)));
6293 First_Gen := Gen_Par;
6295 while Present (Gen_Par)
6296 and then Is_Child_Unit (Gen_Par)
6297 loop
6298 -- Load grandparent instance as well
6300 Inst_Node := Get_Package_Instantiation_Node (Inst_Par);
6302 if Nkind (Name (Inst_Node)) = N_Expanded_Name then
6303 Inst_Par := Entity (Prefix (Name (Inst_Node)));
6305 if Present (Renamed_Entity (Inst_Par)) then
6306 Inst_Par := Renamed_Entity (Inst_Par);
6307 end if;
6309 Gen_Par :=
6310 Generic_Parent
6311 (Specification (Unit_Declaration_Node (Inst_Par)));
6313 if Present (Gen_Par) then
6314 Prepend_Elmt (Inst_Par, Ancestors);
6316 else
6317 -- Parent is not the name of an instantiation
6319 Install_Noninstance_Specs (Inst_Par);
6321 exit;
6322 end if;
6324 else
6325 -- Previous error
6327 exit;
6328 end if;
6329 end loop;
6331 if Present (First_Gen) then
6332 Append_Elmt (First_Par, Ancestors);
6334 else
6335 Install_Noninstance_Specs (First_Par);
6336 end if;
6338 if not Is_Empty_Elmt_List (Ancestors) then
6339 Elmt := First_Elmt (Ancestors);
6341 while Present (Elmt) loop
6342 Install_Spec (Node (Elmt));
6343 Install_Formal_Packages (Node (Elmt));
6345 Next_Elmt (Elmt);
6346 end loop;
6347 end if;
6349 if not In_Body then
6350 New_Scope (S);
6351 end if;
6352 end Install_Parent;
6354 --------------------------------
6355 -- Instantiate_Formal_Package --
6356 --------------------------------
6358 function Instantiate_Formal_Package
6359 (Formal : Node_Id;
6360 Actual : Node_Id;
6361 Analyzed_Formal : Node_Id) return List_Id
6363 Loc : constant Source_Ptr := Sloc (Actual);
6364 Actual_Pack : Entity_Id;
6365 Formal_Pack : Entity_Id;
6366 Gen_Parent : Entity_Id;
6367 Decls : List_Id;
6368 Nod : Node_Id;
6369 Parent_Spec : Node_Id;
6371 procedure Find_Matching_Actual
6372 (F : Node_Id;
6373 Act : in out Entity_Id);
6374 -- We need to associate each formal entity in the formal package
6375 -- with the corresponding entity in the actual package. The actual
6376 -- package has been analyzed and possibly expanded, and as a result
6377 -- there is no one-to-one correspondence between the two lists (for
6378 -- example, the actual may include subtypes, itypes, and inherited
6379 -- primitive operations, interspersed among the renaming declarations
6380 -- for the actuals) . We retrieve the corresponding actual by name
6381 -- because each actual has the same name as the formal, and they do
6382 -- appear in the same order.
6384 function Formal_Entity
6385 (F : Node_Id;
6386 Act_Ent : Entity_Id) return Entity_Id;
6387 -- Returns the entity associated with the given formal F. In the
6388 -- case where F is a formal package, this function will iterate
6389 -- through all of F's formals and enter map associations from the
6390 -- actuals occurring in the formal package's corresponding actual
6391 -- package (obtained via Act_Ent) to the formal package's formal
6392 -- parameters. This function is called recursively for arbitrary
6393 -- levels of formal packages.
6395 function Is_Instance_Of
6396 (Act_Spec : Entity_Id;
6397 Gen_Anc : Entity_Id) return Boolean;
6398 -- The actual can be an instantiation of a generic within another
6399 -- instance, in which case there is no direct link from it to the
6400 -- original generic ancestor. In that case, we recognize that the
6401 -- ultimate ancestor is the same by examining names and scopes.
6403 procedure Map_Entities (Form : Entity_Id; Act : Entity_Id);
6404 -- Within the generic part, entities in the formal package are
6405 -- visible. To validate subsequent type declarations, indicate
6406 -- the correspondence betwen the entities in the analyzed formal,
6407 -- and the entities in the actual package. There are three packages
6408 -- involved in the instantiation of a formal package: the parent
6409 -- generic P1 which appears in the generic declaration, the fake
6410 -- instantiation P2 which appears in the analyzed generic, and whose
6411 -- visible entities may be used in subsequent formals, and the actual
6412 -- P3 in the instance. To validate subsequent formals, me indicate
6413 -- that the entities in P2 are mapped into those of P3. The mapping of
6414 -- entities has to be done recursively for nested packages.
6416 procedure Process_Nested_Formal (Formal : Entity_Id);
6417 -- If the current formal is declared with a box, its own formals are
6418 -- visible in the instance, as they were in the generic, and their
6419 -- Hidden flag must be reset. If some of these formals are themselves
6420 -- packages declared with a box, the processing must be recursive.
6422 --------------------------
6423 -- Find_Matching_Actual --
6424 --------------------------
6426 procedure Find_Matching_Actual
6427 (F : Node_Id;
6428 Act : in out Entity_Id)
6430 Formal_Ent : Entity_Id;
6432 begin
6433 case Nkind (Original_Node (F)) is
6434 when N_Formal_Object_Declaration |
6435 N_Formal_Type_Declaration =>
6436 Formal_Ent := Defining_Identifier (F);
6438 while Chars (Act) /= Chars (Formal_Ent) loop
6439 Next_Entity (Act);
6440 end loop;
6442 when N_Formal_Subprogram_Declaration |
6443 N_Formal_Package_Declaration |
6444 N_Package_Declaration |
6445 N_Generic_Package_Declaration =>
6446 Formal_Ent := Defining_Entity (F);
6448 while Chars (Act) /= Chars (Formal_Ent) loop
6449 Next_Entity (Act);
6450 end loop;
6452 when others =>
6453 raise Program_Error;
6454 end case;
6455 end Find_Matching_Actual;
6457 -------------------
6458 -- Formal_Entity --
6459 -------------------
6461 function Formal_Entity
6462 (F : Node_Id;
6463 Act_Ent : Entity_Id) return Entity_Id
6465 Orig_Node : Node_Id := F;
6466 Act_Pkg : Entity_Id;
6468 begin
6469 case Nkind (Original_Node (F)) is
6470 when N_Formal_Object_Declaration =>
6471 return Defining_Identifier (F);
6473 when N_Formal_Type_Declaration =>
6474 return Defining_Identifier (F);
6476 when N_Formal_Subprogram_Declaration =>
6477 return Defining_Unit_Name (Specification (F));
6479 when N_Package_Declaration =>
6480 return Defining_Unit_Name (Specification (F));
6482 when N_Formal_Package_Declaration |
6483 N_Generic_Package_Declaration =>
6485 if Nkind (F) = N_Generic_Package_Declaration then
6486 Orig_Node := Original_Node (F);
6487 end if;
6489 Act_Pkg := Act_Ent;
6491 -- Find matching actual package, skipping over itypes and
6492 -- other entities generated when analyzing the formal. We
6493 -- know that if the instantiation is legal then there is
6494 -- a matching package for the formal.
6496 while Ekind (Act_Pkg) /= E_Package loop
6497 Act_Pkg := Next_Entity (Act_Pkg);
6498 end loop;
6500 declare
6501 Actual_Ent : Entity_Id := First_Entity (Act_Pkg);
6502 Formal_Node : Node_Id;
6503 Formal_Ent : Entity_Id;
6505 Gen_Decl : constant Node_Id :=
6506 Unit_Declaration_Node
6507 (Entity (Name (Orig_Node)));
6509 Formals : constant List_Id :=
6510 Generic_Formal_Declarations (Gen_Decl);
6512 begin
6513 if Present (Formals) then
6514 Formal_Node := First_Non_Pragma (Formals);
6515 else
6516 Formal_Node := Empty;
6517 end if;
6519 while Present (Actual_Ent)
6520 and then Present (Formal_Node)
6521 and then Actual_Ent /= First_Private_Entity (Act_Pkg)
6522 loop
6523 -- ??? Are the following calls also needed here:
6525 -- Set_Is_Hidden (Actual_Ent, False);
6526 -- Set_Is_Potentially_Use_Visible
6527 -- (Actual_Ent, In_Use (Act_Ent));
6529 Formal_Ent := Formal_Entity (Formal_Node, Actual_Ent);
6530 if Present (Formal_Ent) then
6531 Set_Instance_Of (Formal_Ent, Actual_Ent);
6532 end if;
6533 Next_Non_Pragma (Formal_Node);
6535 Next_Entity (Actual_Ent);
6536 end loop;
6537 end;
6539 return Defining_Identifier (Orig_Node);
6541 when N_Use_Package_Clause =>
6542 return Empty;
6544 when N_Use_Type_Clause =>
6545 return Empty;
6547 -- We return Empty for all other encountered forms of
6548 -- declarations because there are some cases of nonformal
6549 -- sorts of declaration that can show up (e.g., when array
6550 -- formals are present). Since it's not clear what kinds
6551 -- can appear among the formals, we won't raise failure here.
6553 when others =>
6554 return Empty;
6556 end case;
6557 end Formal_Entity;
6559 --------------------
6560 -- Is_Instance_Of --
6561 --------------------
6563 function Is_Instance_Of
6564 (Act_Spec : Entity_Id;
6565 Gen_Anc : Entity_Id) return Boolean
6567 Gen_Par : constant Entity_Id := Generic_Parent (Act_Spec);
6569 begin
6570 if No (Gen_Par) then
6571 return False;
6573 -- Simplest case: the generic parent of the actual is the formal
6575 elsif Gen_Par = Gen_Anc then
6576 return True;
6578 elsif Chars (Gen_Par) /= Chars (Gen_Anc) then
6579 return False;
6581 -- The actual may be obtained through several instantiations. Its
6582 -- scope must itself be an instance of a generic declared in the
6583 -- same scope as the formal. Any other case is detected above.
6585 elsif not Is_Generic_Instance (Scope (Gen_Par)) then
6586 return False;
6588 else
6589 return Generic_Parent (Parent (Scope (Gen_Par))) = Scope (Gen_Anc);
6590 end if;
6591 end Is_Instance_Of;
6593 ------------------
6594 -- Map_Entities --
6595 ------------------
6597 procedure Map_Entities (Form : Entity_Id; Act : Entity_Id) is
6598 E1 : Entity_Id;
6599 E2 : Entity_Id;
6601 begin
6602 Set_Instance_Of (Form, Act);
6604 -- Traverse formal and actual package to map the corresponding
6605 -- entities. We skip over internal entities that may be generated
6606 -- during semantic analysis, and find the matching entities by
6607 -- name, given that they must appear in the same order.
6609 E1 := First_Entity (Form);
6610 E2 := First_Entity (Act);
6611 while Present (E1)
6612 and then E1 /= First_Private_Entity (Form)
6613 loop
6614 if not Is_Internal (E1)
6615 and then not Is_Class_Wide_Type (E1)
6616 and then Present (Parent (E1))
6617 then
6618 while Present (E2)
6619 and then Chars (E2) /= Chars (E1)
6620 loop
6621 Next_Entity (E2);
6622 end loop;
6624 if No (E2) then
6625 exit;
6626 else
6627 Set_Instance_Of (E1, E2);
6629 if Is_Type (E1)
6630 and then Is_Tagged_Type (E2)
6631 then
6632 Set_Instance_Of
6633 (Class_Wide_Type (E1), Class_Wide_Type (E2));
6634 end if;
6636 if Ekind (E1) = E_Package
6637 and then No (Renamed_Object (E1))
6638 then
6639 Map_Entities (E1, E2);
6640 end if;
6641 end if;
6642 end if;
6644 Next_Entity (E1);
6645 end loop;
6646 end Map_Entities;
6648 ---------------------------
6649 -- Process_Nested_Formal --
6650 ---------------------------
6652 procedure Process_Nested_Formal (Formal : Entity_Id) is
6653 Ent : Entity_Id;
6655 begin
6656 if Present (Associated_Formal_Package (Formal))
6657 and then Box_Present (Parent (Associated_Formal_Package (Formal)))
6658 then
6659 Ent := First_Entity (Formal);
6660 while Present (Ent) loop
6661 Set_Is_Hidden (Ent, False);
6662 Set_Is_Potentially_Use_Visible
6663 (Ent, Is_Potentially_Use_Visible (Formal));
6665 if Ekind (Ent) = E_Package then
6666 exit when Renamed_Entity (Ent) = Renamed_Entity (Formal);
6667 Process_Nested_Formal (Ent);
6668 end if;
6670 Next_Entity (Ent);
6671 end loop;
6672 end if;
6673 end Process_Nested_Formal;
6675 -- Start of processing for Instantiate_Formal_Package
6677 begin
6678 Analyze (Actual);
6680 if not Is_Entity_Name (Actual)
6681 or else Ekind (Entity (Actual)) /= E_Package
6682 then
6683 Error_Msg_N
6684 ("expect package instance to instantiate formal", Actual);
6685 Abandon_Instantiation (Actual);
6686 raise Program_Error;
6688 else
6689 Actual_Pack := Entity (Actual);
6690 Set_Is_Instantiated (Actual_Pack);
6692 -- The actual may be a renamed package, or an outer generic
6693 -- formal package whose instantiation is converted into a renaming.
6695 if Present (Renamed_Object (Actual_Pack)) then
6696 Actual_Pack := Renamed_Object (Actual_Pack);
6697 end if;
6699 if Nkind (Analyzed_Formal) = N_Formal_Package_Declaration then
6700 Gen_Parent := Get_Instance_Of (Entity (Name (Analyzed_Formal)));
6701 Formal_Pack := Defining_Identifier (Analyzed_Formal);
6702 else
6703 Gen_Parent :=
6704 Generic_Parent (Specification (Analyzed_Formal));
6705 Formal_Pack :=
6706 Defining_Unit_Name (Specification (Analyzed_Formal));
6707 end if;
6709 if Nkind (Parent (Actual_Pack)) = N_Defining_Program_Unit_Name then
6710 Parent_Spec := Specification (Unit_Declaration_Node (Actual_Pack));
6711 else
6712 Parent_Spec := Parent (Actual_Pack);
6713 end if;
6715 if Gen_Parent = Any_Id then
6716 Error_Msg_N
6717 ("previous error in declaration of formal package", Actual);
6718 Abandon_Instantiation (Actual);
6720 elsif
6721 Is_Instance_Of (Parent_Spec, Get_Instance_Of (Gen_Parent))
6722 then
6723 null;
6725 else
6726 Error_Msg_NE
6727 ("actual parameter must be instance of&", Actual, Gen_Parent);
6728 Abandon_Instantiation (Actual);
6729 end if;
6731 Set_Instance_Of (Defining_Identifier (Formal), Actual_Pack);
6732 Map_Entities (Formal_Pack, Actual_Pack);
6734 Nod :=
6735 Make_Package_Renaming_Declaration (Loc,
6736 Defining_Unit_Name => New_Copy (Defining_Identifier (Formal)),
6737 Name => New_Reference_To (Actual_Pack, Loc));
6739 Set_Associated_Formal_Package (Defining_Unit_Name (Nod),
6740 Defining_Identifier (Formal));
6741 Decls := New_List (Nod);
6743 -- If the formal F has a box, then the generic declarations are
6744 -- visible in the generic G. In an instance of G, the corresponding
6745 -- entities in the actual for F (which are the actuals for the
6746 -- instantiation of the generic that F denotes) must also be made
6747 -- visible for analysis of the current instance. On exit from the
6748 -- current instance, those entities are made private again. If the
6749 -- actual is currently in use, these entities are also use-visible.
6751 -- The loop through the actual entities also steps through the
6752 -- formal entities and enters associations from formals to
6753 -- actuals into the renaming map. This is necessary to properly
6754 -- handle checking of actual parameter associations for later
6755 -- formals that depend on actuals declared in the formal package.
6757 if Box_Present (Formal) then
6758 declare
6759 Gen_Decl : constant Node_Id :=
6760 Unit_Declaration_Node (Gen_Parent);
6761 Formals : constant List_Id :=
6762 Generic_Formal_Declarations (Gen_Decl);
6763 Actual_Ent : Entity_Id;
6764 Formal_Node : Node_Id;
6765 Formal_Ent : Entity_Id;
6767 begin
6768 if Present (Formals) then
6769 Formal_Node := First_Non_Pragma (Formals);
6770 else
6771 Formal_Node := Empty;
6772 end if;
6774 Actual_Ent := First_Entity (Actual_Pack);
6776 while Present (Actual_Ent)
6777 and then Actual_Ent /= First_Private_Entity (Actual_Pack)
6778 loop
6779 Set_Is_Hidden (Actual_Ent, False);
6780 Set_Is_Potentially_Use_Visible
6781 (Actual_Ent, In_Use (Actual_Pack));
6783 if Ekind (Actual_Ent) = E_Package then
6784 Process_Nested_Formal (Actual_Ent);
6785 end if;
6787 if Present (Formal_Node) then
6788 Formal_Ent := Formal_Entity (Formal_Node, Actual_Ent);
6790 if Present (Formal_Ent) then
6791 Find_Matching_Actual (Formal_Node, Actual_Ent);
6792 Set_Instance_Of (Formal_Ent, Actual_Ent);
6793 end if;
6795 Next_Non_Pragma (Formal_Node);
6797 else
6798 -- No further formals to match, but the generic
6799 -- part may contain inherited operation that are
6800 -- not hidden in the enclosing instance.
6802 Next_Entity (Actual_Ent);
6803 end if;
6805 end loop;
6806 end;
6808 -- If the formal is not declared with a box, reanalyze it as
6809 -- an instantiation, to verify the matching rules of 12.7. The
6810 -- actual checks are performed after the generic associations
6811 -- been analyzed.
6813 else
6814 declare
6815 I_Pack : constant Entity_Id :=
6816 Make_Defining_Identifier (Sloc (Actual),
6817 Chars => New_Internal_Name ('P'));
6819 begin
6820 Set_Is_Internal (I_Pack);
6822 Append_To (Decls,
6823 Make_Package_Instantiation (Sloc (Actual),
6824 Defining_Unit_Name => I_Pack,
6825 Name => New_Occurrence_Of (Gen_Parent, Sloc (Actual)),
6826 Generic_Associations =>
6827 Generic_Associations (Formal)));
6828 end;
6829 end if;
6831 return Decls;
6832 end if;
6833 end Instantiate_Formal_Package;
6835 -----------------------------------
6836 -- Instantiate_Formal_Subprogram --
6837 -----------------------------------
6839 function Instantiate_Formal_Subprogram
6840 (Formal : Node_Id;
6841 Actual : Node_Id;
6842 Analyzed_Formal : Node_Id) return Node_Id
6844 Loc : Source_Ptr := Sloc (Instantiation_Node);
6845 Formal_Sub : constant Entity_Id :=
6846 Defining_Unit_Name (Specification (Formal));
6847 Analyzed_S : constant Entity_Id :=
6848 Defining_Unit_Name (Specification (Analyzed_Formal));
6849 Decl_Node : Node_Id;
6850 Nam : Node_Id;
6851 New_Spec : Node_Id;
6853 function From_Parent_Scope (Subp : Entity_Id) return Boolean;
6854 -- If the generic is a child unit, the parent has been installed on the
6855 -- scope stack, but a default subprogram cannot resolve to something on
6856 -- the parent because that parent is not really part of the visible
6857 -- context (it is there to resolve explicit local entities). If the
6858 -- default has resolved in this way, we remove the entity from
6859 -- immediate visibility and analyze the node again to emit an error
6860 -- message or find another visible candidate.
6862 procedure Valid_Actual_Subprogram (Act : Node_Id);
6863 -- Perform legality check and raise exception on failure
6865 -----------------------
6866 -- From_Parent_Scope --
6867 -----------------------
6869 function From_Parent_Scope (Subp : Entity_Id) return Boolean is
6870 Gen_Scope : Node_Id := Scope (Analyzed_S);
6872 begin
6873 while Present (Gen_Scope)
6874 and then Is_Child_Unit (Gen_Scope)
6875 loop
6876 if Scope (Subp) = Scope (Gen_Scope) then
6877 return True;
6878 end if;
6880 Gen_Scope := Scope (Gen_Scope);
6881 end loop;
6883 return False;
6884 end From_Parent_Scope;
6886 -----------------------------
6887 -- Valid_Actual_Subprogram --
6888 -----------------------------
6890 procedure Valid_Actual_Subprogram (Act : Node_Id) is
6891 Act_E : Entity_Id := Empty;
6893 begin
6894 if Is_Entity_Name (Act) then
6895 Act_E := Entity (Act);
6896 elsif Nkind (Act) = N_Selected_Component
6897 and then Is_Entity_Name (Selector_Name (Act))
6898 then
6899 Act_E := Entity (Selector_Name (Act));
6900 end if;
6902 if (Present (Act_E) and then Is_Overloadable (Act_E))
6903 or else Nkind (Act) = N_Attribute_Reference
6904 or else Nkind (Act) = N_Indexed_Component
6905 or else Nkind (Act) = N_Character_Literal
6906 or else Nkind (Act) = N_Explicit_Dereference
6907 then
6908 return;
6909 end if;
6911 Error_Msg_NE
6912 ("expect subprogram or entry name in instantiation of&",
6913 Instantiation_Node, Formal_Sub);
6914 Abandon_Instantiation (Instantiation_Node);
6916 end Valid_Actual_Subprogram;
6918 -- Start of processing for Instantiate_Formal_Subprogram
6920 begin
6921 New_Spec := New_Copy_Tree (Specification (Formal));
6923 -- Create new entity for the actual (New_Copy_Tree does not)
6925 Set_Defining_Unit_Name
6926 (New_Spec, Make_Defining_Identifier (Loc, Chars (Formal_Sub)));
6928 -- Find entity of actual. If the actual is an attribute reference, it
6929 -- cannot be resolved here (its formal is missing) but is handled
6930 -- instead in Attribute_Renaming. If the actual is overloaded, it is
6931 -- fully resolved subsequently, when the renaming declaration for the
6932 -- formal is analyzed. If it is an explicit dereference, resolve the
6933 -- prefix but not the actual itself, to prevent interpretation as a
6934 -- call.
6936 if Present (Actual) then
6937 Loc := Sloc (Actual);
6938 Set_Sloc (New_Spec, Loc);
6940 if Nkind (Actual) = N_Operator_Symbol then
6941 Find_Direct_Name (Actual);
6943 elsif Nkind (Actual) = N_Explicit_Dereference then
6944 Analyze (Prefix (Actual));
6946 elsif Nkind (Actual) /= N_Attribute_Reference then
6947 Analyze (Actual);
6948 end if;
6950 Valid_Actual_Subprogram (Actual);
6951 Nam := Actual;
6953 elsif Present (Default_Name (Formal)) then
6954 if Nkind (Default_Name (Formal)) /= N_Attribute_Reference
6955 and then Nkind (Default_Name (Formal)) /= N_Selected_Component
6956 and then Nkind (Default_Name (Formal)) /= N_Indexed_Component
6957 and then Nkind (Default_Name (Formal)) /= N_Character_Literal
6958 and then Present (Entity (Default_Name (Formal)))
6959 then
6960 Nam := New_Occurrence_Of (Entity (Default_Name (Formal)), Loc);
6961 else
6962 Nam := New_Copy (Default_Name (Formal));
6963 Set_Sloc (Nam, Loc);
6964 end if;
6966 elsif Box_Present (Formal) then
6968 -- Actual is resolved at the point of instantiation. Create
6969 -- an identifier or operator with the same name as the formal.
6971 if Nkind (Formal_Sub) = N_Defining_Operator_Symbol then
6972 Nam := Make_Operator_Symbol (Loc,
6973 Chars => Chars (Formal_Sub),
6974 Strval => No_String);
6975 else
6976 Nam := Make_Identifier (Loc, Chars (Formal_Sub));
6977 end if;
6979 else
6980 Error_Msg_Sloc := Sloc (Scope (Analyzed_S));
6981 Error_Msg_NE
6982 ("missing actual&", Instantiation_Node, Formal_Sub);
6983 Error_Msg_NE
6984 ("\in instantiation of & declared#",
6985 Instantiation_Node, Scope (Analyzed_S));
6986 Abandon_Instantiation (Instantiation_Node);
6987 end if;
6989 Decl_Node :=
6990 Make_Subprogram_Renaming_Declaration (Loc,
6991 Specification => New_Spec,
6992 Name => Nam);
6994 -- If we do not have an actual and the formal specified <> then
6995 -- set to get proper default.
6997 if No (Actual) and then Box_Present (Formal) then
6998 Set_From_Default (Decl_Node);
6999 end if;
7001 -- Gather possible interpretations for the actual before analyzing the
7002 -- instance. If overloaded, it will be resolved when analyzing the
7003 -- renaming declaration.
7005 if Box_Present (Formal)
7006 and then No (Actual)
7007 then
7008 Analyze (Nam);
7010 if Is_Child_Unit (Scope (Analyzed_S))
7011 and then Present (Entity (Nam))
7012 then
7013 if not Is_Overloaded (Nam) then
7015 if From_Parent_Scope (Entity (Nam)) then
7016 Set_Is_Immediately_Visible (Entity (Nam), False);
7017 Set_Entity (Nam, Empty);
7018 Set_Etype (Nam, Empty);
7020 Analyze (Nam);
7022 Set_Is_Immediately_Visible (Entity (Nam));
7023 end if;
7025 else
7026 declare
7027 I : Interp_Index;
7028 It : Interp;
7030 begin
7031 Get_First_Interp (Nam, I, It);
7033 while Present (It.Nam) loop
7034 if From_Parent_Scope (It.Nam) then
7035 Remove_Interp (I);
7036 end if;
7038 Get_Next_Interp (I, It);
7039 end loop;
7040 end;
7041 end if;
7042 end if;
7043 end if;
7045 -- The generic instantiation freezes the actual. This can only be
7046 -- done once the actual is resolved, in the analysis of the renaming
7047 -- declaration. To make the formal subprogram entity available, we set
7048 -- Corresponding_Formal_Spec to point to the formal subprogram entity.
7049 -- This is also needed in Analyze_Subprogram_Renaming for the processing
7050 -- of formal abstract subprograms.
7052 Set_Corresponding_Formal_Spec (Decl_Node, Analyzed_S);
7054 -- We cannot analyze the renaming declaration, and thus find the
7055 -- actual, until the all the actuals are assembled in the instance.
7056 -- For subsequent checks of other actuals, indicate the node that
7057 -- will hold the instance of this formal.
7059 Set_Instance_Of (Analyzed_S, Nam);
7061 if Nkind (Actual) = N_Selected_Component
7062 and then Is_Task_Type (Etype (Prefix (Actual)))
7063 and then not Is_Frozen (Etype (Prefix (Actual)))
7064 then
7065 -- The renaming declaration will create a body, which must appear
7066 -- outside of the instantiation, We move the renaming declaration
7067 -- out of the instance, and create an additional renaming inside,
7068 -- to prevent freezing anomalies.
7070 declare
7071 Anon_Id : constant Entity_Id :=
7072 Make_Defining_Identifier
7073 (Loc, New_Internal_Name ('E'));
7074 begin
7075 Set_Defining_Unit_Name (New_Spec, Anon_Id);
7076 Insert_Before (Instantiation_Node, Decl_Node);
7077 Analyze (Decl_Node);
7079 -- Now create renaming within the instance
7081 Decl_Node :=
7082 Make_Subprogram_Renaming_Declaration (Loc,
7083 Specification => New_Copy_Tree (New_Spec),
7084 Name => New_Occurrence_Of (Anon_Id, Loc));
7086 Set_Defining_Unit_Name (Specification (Decl_Node),
7087 Make_Defining_Identifier (Loc, Chars (Formal_Sub)));
7088 end;
7089 end if;
7091 return Decl_Node;
7092 end Instantiate_Formal_Subprogram;
7094 ------------------------
7095 -- Instantiate_Object --
7096 ------------------------
7098 function Instantiate_Object
7099 (Formal : Node_Id;
7100 Actual : Node_Id;
7101 Analyzed_Formal : Node_Id) return List_Id
7103 Formal_Id : constant Entity_Id := Defining_Identifier (Formal);
7104 Type_Id : constant Node_Id := Subtype_Mark (Formal);
7105 Loc : constant Source_Ptr := Sloc (Actual);
7106 Act_Assoc : constant Node_Id := Parent (Actual);
7107 Orig_Ftyp : constant Entity_Id :=
7108 Etype (Defining_Identifier (Analyzed_Formal));
7109 List : constant List_Id := New_List;
7110 Ftyp : Entity_Id;
7111 Decl_Node : Node_Id;
7112 Subt_Decl : Node_Id := Empty;
7114 begin
7115 -- Sloc for error message on missing actual
7117 Error_Msg_Sloc := Sloc (Scope (Defining_Identifier (Analyzed_Formal)));
7119 if Get_Instance_Of (Formal_Id) /= Formal_Id then
7120 Error_Msg_N ("duplicate instantiation of generic parameter", Actual);
7121 end if;
7123 Set_Parent (List, Parent (Actual));
7125 -- OUT present
7127 if Out_Present (Formal) then
7129 -- An IN OUT generic actual must be a name. The instantiation is a
7130 -- renaming declaration. The actual is the name being renamed. We
7131 -- use the actual directly, rather than a copy, because it is not
7132 -- used further in the list of actuals, and because a copy or a use
7133 -- of relocate_node is incorrect if the instance is nested within a
7134 -- generic. In order to simplify ASIS searches, the Generic_Parent
7135 -- field links the declaration to the generic association.
7137 if No (Actual) then
7138 Error_Msg_NE
7139 ("missing actual&",
7140 Instantiation_Node, Formal_Id);
7141 Error_Msg_NE
7142 ("\in instantiation of & declared#",
7143 Instantiation_Node,
7144 Scope (Defining_Identifier (Analyzed_Formal)));
7145 Abandon_Instantiation (Instantiation_Node);
7146 end if;
7148 Decl_Node :=
7149 Make_Object_Renaming_Declaration (Loc,
7150 Defining_Identifier => New_Copy (Formal_Id),
7151 Subtype_Mark => New_Copy_Tree (Type_Id),
7152 Name => Actual);
7154 Set_Corresponding_Generic_Association (Decl_Node, Act_Assoc);
7156 -- The analysis of the actual may produce insert_action nodes, so
7157 -- the declaration must have a context in which to attach them.
7159 Append (Decl_Node, List);
7160 Analyze (Actual);
7162 -- Return if the analysis of the actual reported some error
7164 if Etype (Actual) = Any_Type then
7165 return List;
7166 end if;
7168 -- This check is performed here because Analyze_Object_Renaming
7169 -- will not check it when Comes_From_Source is False. Note
7170 -- though that the check for the actual being the name of an
7171 -- object will be performed in Analyze_Object_Renaming.
7173 if Is_Object_Reference (Actual)
7174 and then Is_Dependent_Component_Of_Mutable_Object (Actual)
7175 then
7176 Error_Msg_N
7177 ("illegal discriminant-dependent component for in out parameter",
7178 Actual);
7179 end if;
7181 -- The actual has to be resolved in order to check that it is
7182 -- a variable (due to cases such as F(1), where F returns
7183 -- access to an array, and for overloaded prefixes).
7185 Ftyp :=
7186 Get_Instance_Of (Etype (Defining_Identifier (Analyzed_Formal)));
7188 if Is_Private_Type (Ftyp)
7189 and then not Is_Private_Type (Etype (Actual))
7190 and then (Base_Type (Full_View (Ftyp)) = Base_Type (Etype (Actual))
7191 or else Base_Type (Etype (Actual)) = Ftyp)
7192 then
7193 -- If the actual has the type of the full view of the formal,
7194 -- or else a non-private subtype of the formal, then
7195 -- the visibility of the formal type has changed. Add to the
7196 -- actuals a subtype declaration that will force the exchange
7197 -- of views in the body of the instance as well.
7199 Subt_Decl :=
7200 Make_Subtype_Declaration (Loc,
7201 Defining_Identifier =>
7202 Make_Defining_Identifier (Loc, New_Internal_Name ('P')),
7203 Subtype_Indication => New_Occurrence_Of (Ftyp, Loc));
7205 Prepend (Subt_Decl, List);
7207 Append_Elmt (Full_View (Ftyp), Exchanged_Views);
7208 Exchange_Declarations (Ftyp);
7209 end if;
7211 Resolve (Actual, Ftyp);
7213 if not Is_Variable (Actual) or else Paren_Count (Actual) > 0 then
7214 Error_Msg_NE
7215 ("actual for& must be a variable", Actual, Formal_Id);
7217 elsif Base_Type (Ftyp) /= Base_Type (Etype (Actual)) then
7218 Error_Msg_NE (
7219 "type of actual does not match type of&", Actual, Formal_Id);
7221 end if;
7223 Note_Possible_Modification (Actual);
7225 -- Check for instantiation of atomic/volatile actual for
7226 -- non-atomic/volatile formal (RM C.6 (12)).
7228 if Is_Atomic_Object (Actual)
7229 and then not Is_Atomic (Orig_Ftyp)
7230 then
7231 Error_Msg_N
7232 ("cannot instantiate non-atomic formal object " &
7233 "with atomic actual", Actual);
7235 elsif Is_Volatile_Object (Actual)
7236 and then not Is_Volatile (Orig_Ftyp)
7237 then
7238 Error_Msg_N
7239 ("cannot instantiate non-volatile formal object " &
7240 "with volatile actual", Actual);
7241 end if;
7243 -- OUT not present
7245 else
7246 -- The instantiation of a generic formal in-parameter
7247 -- is a constant declaration. The actual is the expression for
7248 -- that declaration.
7250 if Present (Actual) then
7252 Decl_Node := Make_Object_Declaration (Loc,
7253 Defining_Identifier => New_Copy (Formal_Id),
7254 Constant_Present => True,
7255 Object_Definition => New_Copy_Tree (Type_Id),
7256 Expression => Actual);
7258 Set_Corresponding_Generic_Association (Decl_Node, Act_Assoc);
7260 -- A generic formal object of a tagged type is defined
7261 -- to be aliased so the new constant must also be treated
7262 -- as aliased.
7264 if Is_Tagged_Type
7265 (Etype (Defining_Identifier (Analyzed_Formal)))
7266 then
7267 Set_Aliased_Present (Decl_Node);
7268 end if;
7270 Append (Decl_Node, List);
7272 -- No need to repeat (pre-)analysis of some expression nodes
7273 -- already handled in Pre_Analyze_Actuals.
7275 if Nkind (Actual) /= N_Allocator then
7276 Analyze (Actual);
7278 -- Return if the analysis of the actual reported some error
7280 if Etype (Actual) = Any_Type then
7281 return List;
7282 end if;
7283 end if;
7285 declare
7286 Typ : constant Entity_Id :=
7287 Get_Instance_Of
7288 (Etype (Defining_Identifier (Analyzed_Formal)));
7290 begin
7291 Freeze_Before (Instantiation_Node, Typ);
7293 -- If the actual is an aggregate, perform name resolution on
7294 -- its components (the analysis of an aggregate does not do
7295 -- it) to capture local names that may be hidden if the
7296 -- generic is a child unit.
7298 if Nkind (Actual) = N_Aggregate then
7299 Pre_Analyze_And_Resolve (Actual, Typ);
7300 end if;
7301 end;
7303 elsif Present (Expression (Formal)) then
7305 -- Use default to construct declaration
7307 Decl_Node :=
7308 Make_Object_Declaration (Sloc (Formal),
7309 Defining_Identifier => New_Copy (Formal_Id),
7310 Constant_Present => True,
7311 Object_Definition => New_Copy (Type_Id),
7312 Expression => New_Copy_Tree (Expression (Formal)));
7314 Append (Decl_Node, List);
7315 Set_Analyzed (Expression (Decl_Node), False);
7317 else
7318 Error_Msg_NE
7319 ("missing actual&",
7320 Instantiation_Node, Formal_Id);
7321 Error_Msg_NE ("\in instantiation of & declared#",
7322 Instantiation_Node,
7323 Scope (Defining_Identifier (Analyzed_Formal)));
7325 if Is_Scalar_Type
7326 (Etype (Defining_Identifier (Analyzed_Formal)))
7327 then
7328 -- Create dummy constant declaration so that instance can
7329 -- be analyzed, to minimize cascaded visibility errors.
7331 Decl_Node :=
7332 Make_Object_Declaration (Loc,
7333 Defining_Identifier => New_Copy (Formal_Id),
7334 Constant_Present => True,
7335 Object_Definition => New_Copy (Type_Id),
7336 Expression =>
7337 Make_Attribute_Reference (Sloc (Formal_Id),
7338 Attribute_Name => Name_First,
7339 Prefix => New_Copy (Type_Id)));
7341 Append (Decl_Node, List);
7343 else
7344 Abandon_Instantiation (Instantiation_Node);
7345 end if;
7346 end if;
7348 end if;
7350 return List;
7351 end Instantiate_Object;
7353 ------------------------------
7354 -- Instantiate_Package_Body --
7355 ------------------------------
7357 procedure Instantiate_Package_Body
7358 (Body_Info : Pending_Body_Info;
7359 Inlined_Body : Boolean := False)
7361 Act_Decl : constant Node_Id := Body_Info.Act_Decl;
7362 Inst_Node : constant Node_Id := Body_Info.Inst_Node;
7363 Loc : constant Source_Ptr := Sloc (Inst_Node);
7365 Gen_Id : constant Node_Id := Name (Inst_Node);
7366 Gen_Unit : constant Entity_Id := Get_Generic_Entity (Inst_Node);
7367 Gen_Decl : constant Node_Id := Unit_Declaration_Node (Gen_Unit);
7368 Act_Spec : constant Node_Id := Specification (Act_Decl);
7369 Act_Decl_Id : constant Entity_Id := Defining_Entity (Act_Spec);
7371 Act_Body_Name : Node_Id;
7372 Gen_Body : Node_Id;
7373 Gen_Body_Id : Node_Id;
7374 Act_Body : Node_Id;
7375 Act_Body_Id : Entity_Id;
7377 Parent_Installed : Boolean := False;
7378 Save_Style_Check : constant Boolean := Style_Check;
7380 begin
7381 Gen_Body_Id := Corresponding_Body (Gen_Decl);
7383 -- The instance body may already have been processed, as the parent
7384 -- of another instance that is inlined. (Load_Parent_Of_Generic).
7386 if Present (Corresponding_Body (Instance_Spec (Inst_Node))) then
7387 return;
7388 end if;
7390 Expander_Mode_Save_And_Set (Body_Info.Expander_Status);
7392 if No (Gen_Body_Id) then
7393 Load_Parent_Of_Generic (Inst_Node, Specification (Gen_Decl));
7394 Gen_Body_Id := Corresponding_Body (Gen_Decl);
7395 end if;
7397 -- Establish global variable for sloc adjustment and for error
7398 -- recovery.
7400 Instantiation_Node := Inst_Node;
7402 if Present (Gen_Body_Id) then
7403 Save_Env (Gen_Unit, Act_Decl_Id);
7404 Style_Check := False;
7405 Current_Sem_Unit := Body_Info.Current_Sem_Unit;
7407 Gen_Body := Unit_Declaration_Node (Gen_Body_Id);
7409 Create_Instantiation_Source
7410 (Inst_Node, Gen_Body_Id, False, S_Adjustment);
7412 Act_Body :=
7413 Copy_Generic_Node
7414 (Original_Node (Gen_Body), Empty, Instantiating => True);
7416 -- Build new name (possibly qualified) for body declaration
7418 Act_Body_Id := New_Copy (Act_Decl_Id);
7420 -- Some attributes of the spec entity are not inherited by the
7421 -- body entity.
7423 Set_Handler_Records (Act_Body_Id, No_List);
7425 if Nkind (Defining_Unit_Name (Act_Spec)) =
7426 N_Defining_Program_Unit_Name
7427 then
7428 Act_Body_Name :=
7429 Make_Defining_Program_Unit_Name (Loc,
7430 Name => New_Copy_Tree (Name (Defining_Unit_Name (Act_Spec))),
7431 Defining_Identifier => Act_Body_Id);
7432 else
7433 Act_Body_Name := Act_Body_Id;
7434 end if;
7436 Set_Defining_Unit_Name (Act_Body, Act_Body_Name);
7438 Set_Corresponding_Spec (Act_Body, Act_Decl_Id);
7439 Check_Generic_Actuals (Act_Decl_Id, False);
7441 -- If it is a child unit, make the parent instance (which is an
7442 -- instance of the parent of the generic) visible. The parent
7443 -- instance is the prefix of the name of the generic unit.
7445 if Ekind (Scope (Gen_Unit)) = E_Generic_Package
7446 and then Nkind (Gen_Id) = N_Expanded_Name
7447 then
7448 Install_Parent (Entity (Prefix (Gen_Id)), In_Body => True);
7449 Parent_Installed := True;
7451 elsif Is_Child_Unit (Gen_Unit) then
7452 Install_Parent (Scope (Gen_Unit), In_Body => True);
7453 Parent_Installed := True;
7454 end if;
7456 -- If the instantiation is a library unit, and this is the main
7457 -- unit, then build the resulting compilation unit nodes for the
7458 -- instance. If this is a compilation unit but it is not the main
7459 -- unit, then it is the body of a unit in the context, that is being
7460 -- compiled because it is encloses some inlined unit or another
7461 -- generic unit being instantiated. In that case, this body is not
7462 -- part of the current compilation, and is not attached to the tree,
7463 -- but its parent must be set for analysis.
7465 if Nkind (Parent (Inst_Node)) = N_Compilation_Unit then
7467 -- Replace instance node with body of instance, and create
7468 -- new node for corresponding instance declaration.
7470 Build_Instance_Compilation_Unit_Nodes
7471 (Inst_Node, Act_Body, Act_Decl);
7472 Analyze (Inst_Node);
7474 if Parent (Inst_Node) = Cunit (Main_Unit) then
7476 -- If the instance is a child unit itself, then set the
7477 -- scope of the expanded body to be the parent of the
7478 -- instantiation (ensuring that the fully qualified name
7479 -- will be generated for the elaboration subprogram).
7481 if Nkind (Defining_Unit_Name (Act_Spec)) =
7482 N_Defining_Program_Unit_Name
7483 then
7484 Set_Scope
7485 (Defining_Entity (Inst_Node), Scope (Act_Decl_Id));
7486 end if;
7487 end if;
7489 -- Case where instantiation is not a library unit
7491 else
7492 -- If this is an early instantiation, i.e. appears textually
7493 -- before the corresponding body and must be elaborated first,
7494 -- indicate that the body instance is to be delayed.
7496 Install_Body (Act_Body, Inst_Node, Gen_Body, Gen_Decl);
7498 -- Now analyze the body. We turn off all checks if this is
7499 -- an internal unit, since there is no reason to have checks
7500 -- on for any predefined run-time library code. All such
7501 -- code is designed to be compiled with checks off.
7503 -- Note that we do NOT apply this criterion to children of
7504 -- GNAT (or on VMS, children of DEC). The latter units must
7505 -- suppress checks explicitly if this is needed.
7507 if Is_Predefined_File_Name
7508 (Unit_File_Name (Get_Source_Unit (Gen_Decl)))
7509 then
7510 Analyze (Act_Body, Suppress => All_Checks);
7511 else
7512 Analyze (Act_Body);
7513 end if;
7514 end if;
7516 if not Generic_Separately_Compiled (Gen_Unit) then
7517 Inherit_Context (Gen_Body, Inst_Node);
7518 end if;
7520 -- Remove the parent instances if they have been placed on the
7521 -- scope stack to compile the body.
7523 if Parent_Installed then
7524 Remove_Parent (In_Body => True);
7525 end if;
7527 Restore_Private_Views (Act_Decl_Id);
7529 -- Remove the current unit from visibility if this is an instance
7530 -- that is not elaborated on the fly for inlining purposes.
7532 if not Inlined_Body then
7533 Set_Is_Immediately_Visible (Act_Decl_Id, False);
7534 end if;
7536 Restore_Env;
7537 Style_Check := Save_Style_Check;
7539 -- If we have no body, and the unit requires a body, then complain.
7540 -- This complaint is suppressed if we have detected other errors
7541 -- (since a common reason for missing the body is that it had errors).
7543 elsif Unit_Requires_Body (Gen_Unit) then
7544 if Serious_Errors_Detected = 0 then
7545 Error_Msg_NE
7546 ("cannot find body of generic package &", Inst_Node, Gen_Unit);
7548 -- Don't attempt to perform any cleanup actions if some other
7549 -- error was aready detected, since this can cause blowups.
7551 else
7552 return;
7553 end if;
7555 -- Case of package that does not need a body
7557 else
7558 -- If the instantiation of the declaration is a library unit,
7559 -- rewrite the original package instantiation as a package
7560 -- declaration in the compilation unit node.
7562 if Nkind (Parent (Inst_Node)) = N_Compilation_Unit then
7563 Set_Parent_Spec (Act_Decl, Parent_Spec (Inst_Node));
7564 Rewrite (Inst_Node, Act_Decl);
7566 -- Generate elaboration entity, in case spec has elaboration
7567 -- code. This cannot be done when the instance is analyzed,
7568 -- because it is not known yet whether the body exists.
7570 Set_Elaboration_Entity_Required (Act_Decl_Id, False);
7571 Build_Elaboration_Entity (Parent (Inst_Node), Act_Decl_Id);
7573 -- If the instantiation is not a library unit, then append the
7574 -- declaration to the list of implicitly generated entities.
7575 -- unless it is already a list member which means that it was
7576 -- already processed
7578 elsif not Is_List_Member (Act_Decl) then
7579 Mark_Rewrite_Insertion (Act_Decl);
7580 Insert_Before (Inst_Node, Act_Decl);
7581 end if;
7582 end if;
7584 Expander_Mode_Restore;
7585 end Instantiate_Package_Body;
7587 ---------------------------------
7588 -- Instantiate_Subprogram_Body --
7589 ---------------------------------
7591 procedure Instantiate_Subprogram_Body
7592 (Body_Info : Pending_Body_Info)
7594 Act_Decl : constant Node_Id := Body_Info.Act_Decl;
7595 Inst_Node : constant Node_Id := Body_Info.Inst_Node;
7596 Loc : constant Source_Ptr := Sloc (Inst_Node);
7597 Gen_Id : constant Node_Id := Name (Inst_Node);
7598 Gen_Unit : constant Entity_Id := Get_Generic_Entity (Inst_Node);
7599 Gen_Decl : constant Node_Id := Unit_Declaration_Node (Gen_Unit);
7600 Anon_Id : constant Entity_Id :=
7601 Defining_Unit_Name (Specification (Act_Decl));
7602 Pack_Id : constant Entity_Id :=
7603 Defining_Unit_Name (Parent (Act_Decl));
7604 Decls : List_Id;
7605 Gen_Body : Node_Id;
7606 Gen_Body_Id : Node_Id;
7607 Act_Body : Node_Id;
7608 Act_Body_Id : Entity_Id;
7609 Pack_Body : Node_Id;
7610 Prev_Formal : Entity_Id;
7611 Ret_Expr : Node_Id;
7612 Unit_Renaming : Node_Id;
7614 Parent_Installed : Boolean := False;
7615 Save_Style_Check : constant Boolean := Style_Check;
7617 begin
7618 Gen_Body_Id := Corresponding_Body (Gen_Decl);
7620 Expander_Mode_Save_And_Set (Body_Info.Expander_Status);
7622 if No (Gen_Body_Id) then
7623 Load_Parent_Of_Generic (Inst_Node, Specification (Gen_Decl));
7624 Gen_Body_Id := Corresponding_Body (Gen_Decl);
7625 end if;
7627 Instantiation_Node := Inst_Node;
7629 if Present (Gen_Body_Id) then
7630 Gen_Body := Unit_Declaration_Node (Gen_Body_Id);
7632 if Nkind (Gen_Body) = N_Subprogram_Body_Stub then
7634 -- Either body is not present, or context is non-expanding, as
7635 -- when compiling a subunit. Mark the instance as completed, and
7636 -- diagnose a missing body when needed.
7638 if Expander_Active
7639 and then Operating_Mode = Generate_Code
7640 then
7641 Error_Msg_N
7642 ("missing proper body for instantiation", Gen_Body);
7643 end if;
7645 Set_Has_Completion (Anon_Id);
7646 return;
7647 end if;
7649 Save_Env (Gen_Unit, Anon_Id);
7650 Style_Check := False;
7651 Current_Sem_Unit := Body_Info.Current_Sem_Unit;
7652 Create_Instantiation_Source
7653 (Inst_Node,
7654 Gen_Body_Id,
7655 False,
7656 S_Adjustment);
7658 Act_Body :=
7659 Copy_Generic_Node
7660 (Original_Node (Gen_Body), Empty, Instantiating => True);
7661 Act_Body_Id := Defining_Entity (Act_Body);
7662 Set_Chars (Act_Body_Id, Chars (Anon_Id));
7663 Set_Sloc (Act_Body_Id, Sloc (Defining_Entity (Inst_Node)));
7664 Set_Corresponding_Spec (Act_Body, Anon_Id);
7665 Set_Has_Completion (Anon_Id);
7666 Check_Generic_Actuals (Pack_Id, False);
7668 -- If it is a child unit, make the parent instance (which is an
7669 -- instance of the parent of the generic) visible. The parent
7670 -- instance is the prefix of the name of the generic unit.
7672 if Ekind (Scope (Gen_Unit)) = E_Generic_Package
7673 and then Nkind (Gen_Id) = N_Expanded_Name
7674 then
7675 Install_Parent (Entity (Prefix (Gen_Id)), In_Body => True);
7676 Parent_Installed := True;
7678 elsif Is_Child_Unit (Gen_Unit) then
7679 Install_Parent (Scope (Gen_Unit), In_Body => True);
7680 Parent_Installed := True;
7681 end if;
7683 -- Inside its body, a reference to the generic unit is a reference
7684 -- to the instance. The corresponding renaming is the first
7685 -- declaration in the body.
7687 Unit_Renaming :=
7688 Make_Subprogram_Renaming_Declaration (Loc,
7689 Specification =>
7690 Copy_Generic_Node (
7691 Specification (Original_Node (Gen_Body)),
7692 Empty,
7693 Instantiating => True),
7694 Name => New_Occurrence_Of (Anon_Id, Loc));
7696 -- If there is a formal subprogram with the same name as the
7697 -- unit itself, do not add this renaming declaration. This is
7698 -- a temporary fix for one ACVC test. ???
7700 Prev_Formal := First_Entity (Pack_Id);
7701 while Present (Prev_Formal) loop
7702 if Chars (Prev_Formal) = Chars (Gen_Unit)
7703 and then Is_Overloadable (Prev_Formal)
7704 then
7705 exit;
7706 end if;
7708 Next_Entity (Prev_Formal);
7709 end loop;
7711 if Present (Prev_Formal) then
7712 Decls := New_List (Act_Body);
7713 else
7714 Decls := New_List (Unit_Renaming, Act_Body);
7715 end if;
7717 -- The subprogram body is placed in the body of a dummy package
7718 -- body, whose spec contains the subprogram declaration as well
7719 -- as the renaming declarations for the generic parameters.
7721 Pack_Body := Make_Package_Body (Loc,
7722 Defining_Unit_Name => New_Copy (Pack_Id),
7723 Declarations => Decls);
7725 Set_Corresponding_Spec (Pack_Body, Pack_Id);
7727 -- If the instantiation is a library unit, then build resulting
7728 -- compilation unit nodes for the instance. The declaration of
7729 -- the enclosing package is the grandparent of the subprogram
7730 -- declaration. First replace the instantiation node as the unit
7731 -- of the corresponding compilation.
7733 if Nkind (Parent (Inst_Node)) = N_Compilation_Unit then
7734 if Parent (Inst_Node) = Cunit (Main_Unit) then
7735 Set_Unit (Parent (Inst_Node), Inst_Node);
7736 Build_Instance_Compilation_Unit_Nodes
7737 (Inst_Node, Pack_Body, Parent (Parent (Act_Decl)));
7738 Analyze (Inst_Node);
7739 else
7740 Set_Parent (Pack_Body, Parent (Inst_Node));
7741 Analyze (Pack_Body);
7742 end if;
7744 else
7745 Insert_Before (Inst_Node, Pack_Body);
7746 Mark_Rewrite_Insertion (Pack_Body);
7747 Analyze (Pack_Body);
7749 if Expander_Active then
7750 Freeze_Subprogram_Body (Inst_Node, Gen_Body, Pack_Id);
7751 end if;
7752 end if;
7754 if not Generic_Separately_Compiled (Gen_Unit) then
7755 Inherit_Context (Gen_Body, Inst_Node);
7756 end if;
7758 Restore_Private_Views (Pack_Id, False);
7760 if Parent_Installed then
7761 Remove_Parent (In_Body => True);
7762 end if;
7764 Restore_Env;
7765 Style_Check := Save_Style_Check;
7767 -- Body not found. Error was emitted already. If there were no
7768 -- previous errors, this may be an instance whose scope is a premature
7769 -- instance. In that case we must insure that the (legal) program does
7770 -- raise program error if executed. We generate a subprogram body for
7771 -- this purpose. See DEC ac30vso.
7773 elsif Serious_Errors_Detected = 0
7774 and then Nkind (Parent (Inst_Node)) /= N_Compilation_Unit
7775 then
7776 if Ekind (Anon_Id) = E_Procedure then
7777 Act_Body :=
7778 Make_Subprogram_Body (Loc,
7779 Specification =>
7780 Make_Procedure_Specification (Loc,
7781 Defining_Unit_Name => New_Copy (Anon_Id),
7782 Parameter_Specifications =>
7783 New_Copy_List
7784 (Parameter_Specifications (Parent (Anon_Id)))),
7786 Declarations => Empty_List,
7787 Handled_Statement_Sequence =>
7788 Make_Handled_Sequence_Of_Statements (Loc,
7789 Statements =>
7790 New_List (
7791 Make_Raise_Program_Error (Loc,
7792 Reason =>
7793 PE_Access_Before_Elaboration))));
7795 else
7796 Ret_Expr :=
7797 Make_Raise_Program_Error (Loc,
7798 Reason => PE_Access_Before_Elaboration);
7800 Set_Etype (Ret_Expr, (Etype (Anon_Id)));
7801 Set_Analyzed (Ret_Expr);
7803 Act_Body :=
7804 Make_Subprogram_Body (Loc,
7805 Specification =>
7806 Make_Function_Specification (Loc,
7807 Defining_Unit_Name => New_Copy (Anon_Id),
7808 Parameter_Specifications =>
7809 New_Copy_List
7810 (Parameter_Specifications (Parent (Anon_Id))),
7811 Subtype_Mark =>
7812 New_Occurrence_Of (Etype (Anon_Id), Loc)),
7814 Declarations => Empty_List,
7815 Handled_Statement_Sequence =>
7816 Make_Handled_Sequence_Of_Statements (Loc,
7817 Statements =>
7818 New_List (Make_Return_Statement (Loc, Ret_Expr))));
7819 end if;
7821 Pack_Body := Make_Package_Body (Loc,
7822 Defining_Unit_Name => New_Copy (Pack_Id),
7823 Declarations => New_List (Act_Body));
7825 Insert_After (Inst_Node, Pack_Body);
7826 Set_Corresponding_Spec (Pack_Body, Pack_Id);
7827 Analyze (Pack_Body);
7828 end if;
7830 Expander_Mode_Restore;
7831 end Instantiate_Subprogram_Body;
7833 ----------------------
7834 -- Instantiate_Type --
7835 ----------------------
7837 function Instantiate_Type
7838 (Formal : Node_Id;
7839 Actual : Node_Id;
7840 Analyzed_Formal : Node_Id;
7841 Actual_Decls : List_Id) return Node_Id
7843 Loc : constant Source_Ptr := Sloc (Actual);
7844 Gen_T : constant Entity_Id := Defining_Identifier (Formal);
7845 A_Gen_T : constant Entity_Id := Defining_Identifier (Analyzed_Formal);
7846 Ancestor : Entity_Id := Empty;
7847 Def : constant Node_Id := Formal_Type_Definition (Formal);
7848 Act_T : Entity_Id;
7849 Decl_Node : Node_Id;
7851 procedure Validate_Array_Type_Instance;
7852 procedure Validate_Access_Subprogram_Instance;
7853 procedure Validate_Access_Type_Instance;
7854 procedure Validate_Derived_Type_Instance;
7855 procedure Validate_Private_Type_Instance;
7856 -- These procedures perform validation tests for the named case
7858 function Subtypes_Match (Gen_T, Act_T : Entity_Id) return Boolean;
7859 -- Check that base types are the same and that the subtypes match
7860 -- statically. Used in several of the above.
7862 --------------------
7863 -- Subtypes_Match --
7864 --------------------
7866 function Subtypes_Match (Gen_T, Act_T : Entity_Id) return Boolean is
7867 T : constant Entity_Id := Get_Instance_Of (Gen_T);
7869 begin
7870 return (Base_Type (T) = Base_Type (Act_T)
7871 -- why is the and then commented out here???
7872 -- and then Is_Constrained (T) = Is_Constrained (Act_T)
7873 and then Subtypes_Statically_Match (T, Act_T))
7875 or else (Is_Class_Wide_Type (Gen_T)
7876 and then Is_Class_Wide_Type (Act_T)
7877 and then
7878 Subtypes_Match (
7879 Get_Instance_Of (Root_Type (Gen_T)),
7880 Root_Type (Act_T)));
7881 end Subtypes_Match;
7883 -----------------------------------------
7884 -- Validate_Access_Subprogram_Instance --
7885 -----------------------------------------
7887 procedure Validate_Access_Subprogram_Instance is
7888 begin
7889 if not Is_Access_Type (Act_T)
7890 or else Ekind (Designated_Type (Act_T)) /= E_Subprogram_Type
7891 then
7892 Error_Msg_NE
7893 ("expect access type in instantiation of &", Actual, Gen_T);
7894 Abandon_Instantiation (Actual);
7895 end if;
7897 Check_Mode_Conformant
7898 (Designated_Type (Act_T),
7899 Designated_Type (A_Gen_T),
7900 Actual,
7901 Get_Inst => True);
7903 if Ekind (Base_Type (Act_T)) = E_Access_Protected_Subprogram_Type then
7904 if Ekind (A_Gen_T) = E_Access_Subprogram_Type then
7905 Error_Msg_NE
7906 ("protected access type not allowed for formal &",
7907 Actual, Gen_T);
7908 end if;
7910 elsif Ekind (A_Gen_T) = E_Access_Protected_Subprogram_Type then
7911 Error_Msg_NE
7912 ("expect protected access type for formal &",
7913 Actual, Gen_T);
7914 end if;
7915 end Validate_Access_Subprogram_Instance;
7917 -----------------------------------
7918 -- Validate_Access_Type_Instance --
7919 -----------------------------------
7921 procedure Validate_Access_Type_Instance is
7922 Desig_Type : constant Entity_Id :=
7923 Find_Actual_Type
7924 (Designated_Type (A_Gen_T), Scope (A_Gen_T));
7926 begin
7927 if not Is_Access_Type (Act_T) then
7928 Error_Msg_NE
7929 ("expect access type in instantiation of &", Actual, Gen_T);
7930 Abandon_Instantiation (Actual);
7931 end if;
7933 if Is_Access_Constant (A_Gen_T) then
7934 if not Is_Access_Constant (Act_T) then
7935 Error_Msg_N
7936 ("actual type must be access-to-constant type", Actual);
7937 Abandon_Instantiation (Actual);
7938 end if;
7939 else
7940 if Is_Access_Constant (Act_T) then
7941 Error_Msg_N
7942 ("actual type must be access-to-variable type", Actual);
7943 Abandon_Instantiation (Actual);
7945 elsif Ekind (A_Gen_T) = E_General_Access_Type
7946 and then Ekind (Base_Type (Act_T)) /= E_General_Access_Type
7947 then
7948 Error_Msg_N ("actual must be general access type!", Actual);
7949 Error_Msg_NE ("add ALL to }!", Actual, Act_T);
7950 Abandon_Instantiation (Actual);
7951 end if;
7952 end if;
7954 -- The designated subtypes, that is to say the subtypes introduced
7955 -- by an access type declaration (and not by a subtype declaration)
7956 -- must match.
7958 if not Subtypes_Match
7959 (Desig_Type, Designated_Type (Base_Type (Act_T)))
7960 then
7961 Error_Msg_NE
7962 ("designated type of actual does not match that of formal &",
7963 Actual, Gen_T);
7964 Abandon_Instantiation (Actual);
7966 elsif Is_Access_Type (Designated_Type (Act_T))
7967 and then Is_Constrained (Designated_Type (Designated_Type (Act_T)))
7969 Is_Constrained (Designated_Type (Desig_Type))
7970 then
7971 Error_Msg_NE
7972 ("designated type of actual does not match that of formal &",
7973 Actual, Gen_T);
7974 Abandon_Instantiation (Actual);
7975 end if;
7976 end Validate_Access_Type_Instance;
7978 ----------------------------------
7979 -- Validate_Array_Type_Instance --
7980 ----------------------------------
7982 procedure Validate_Array_Type_Instance is
7983 I1 : Node_Id;
7984 I2 : Node_Id;
7985 T2 : Entity_Id;
7987 function Formal_Dimensions return Int;
7988 -- Count number of dimensions in array type formal
7990 -----------------------
7991 -- Formal_Dimensions --
7992 -----------------------
7994 function Formal_Dimensions return Int is
7995 Num : Int := 0;
7996 Index : Node_Id;
7998 begin
7999 if Nkind (Def) = N_Constrained_Array_Definition then
8000 Index := First (Discrete_Subtype_Definitions (Def));
8001 else
8002 Index := First (Subtype_Marks (Def));
8003 end if;
8005 while Present (Index) loop
8006 Num := Num + 1;
8007 Next_Index (Index);
8008 end loop;
8010 return Num;
8011 end Formal_Dimensions;
8013 -- Start of processing for Validate_Array_Type_Instance
8015 begin
8016 if not Is_Array_Type (Act_T) then
8017 Error_Msg_NE
8018 ("expect array type in instantiation of &", Actual, Gen_T);
8019 Abandon_Instantiation (Actual);
8021 elsif Nkind (Def) = N_Constrained_Array_Definition then
8022 if not (Is_Constrained (Act_T)) then
8023 Error_Msg_NE
8024 ("expect constrained array in instantiation of &",
8025 Actual, Gen_T);
8026 Abandon_Instantiation (Actual);
8027 end if;
8029 else
8030 if Is_Constrained (Act_T) then
8031 Error_Msg_NE
8032 ("expect unconstrained array in instantiation of &",
8033 Actual, Gen_T);
8034 Abandon_Instantiation (Actual);
8035 end if;
8036 end if;
8038 if Formal_Dimensions /= Number_Dimensions (Act_T) then
8039 Error_Msg_NE
8040 ("dimensions of actual do not match formal &", Actual, Gen_T);
8041 Abandon_Instantiation (Actual);
8042 end if;
8044 I1 := First_Index (A_Gen_T);
8045 I2 := First_Index (Act_T);
8046 for J in 1 .. Formal_Dimensions loop
8048 -- If the indices of the actual were given by a subtype_mark,
8049 -- the index was transformed into a range attribute. Retrieve
8050 -- the original type mark for checking.
8052 if Is_Entity_Name (Original_Node (I2)) then
8053 T2 := Entity (Original_Node (I2));
8054 else
8055 T2 := Etype (I2);
8056 end if;
8058 if not Subtypes_Match
8059 (Find_Actual_Type (Etype (I1), Scope (A_Gen_T)), T2)
8060 then
8061 Error_Msg_NE
8062 ("index types of actual do not match those of formal &",
8063 Actual, Gen_T);
8064 Abandon_Instantiation (Actual);
8065 end if;
8067 Next_Index (I1);
8068 Next_Index (I2);
8069 end loop;
8071 if not Subtypes_Match (
8072 Find_Actual_Type (Component_Type (A_Gen_T), Scope (A_Gen_T)),
8073 Component_Type (Act_T))
8074 then
8075 Error_Msg_NE
8076 ("component subtype of actual does not match that of formal &",
8077 Actual, Gen_T);
8078 Abandon_Instantiation (Actual);
8079 end if;
8081 if Has_Aliased_Components (A_Gen_T)
8082 and then not Has_Aliased_Components (Act_T)
8083 then
8084 Error_Msg_NE
8085 ("actual must have aliased components to match formal type &",
8086 Actual, Gen_T);
8087 end if;
8089 end Validate_Array_Type_Instance;
8091 ------------------------------------
8092 -- Validate_Derived_Type_Instance --
8093 ------------------------------------
8095 procedure Validate_Derived_Type_Instance is
8096 Actual_Discr : Entity_Id;
8097 Ancestor_Discr : Entity_Id;
8099 begin
8100 -- If the parent type in the generic declaration is itself
8101 -- a previous formal type, then it is local to the generic
8102 -- and absent from the analyzed generic definition. In that
8103 -- case the ancestor is the instance of the formal (which must
8104 -- have been instantiated previously), unless the ancestor is
8105 -- itself a formal derived type. In this latter case (which is the
8106 -- subject of Corrigendum 8652/0038 (AI-202) the ancestor of the
8107 -- formals is the ancestor of its parent. Otherwise, the analyzed
8108 -- generic carries the parent type. If the parent type is defined
8109 -- in a previous formal package, then the scope of that formal
8110 -- package is that of the generic type itself, and it has already
8111 -- been mapped into the corresponding type in the actual package.
8113 -- Common case: parent type defined outside of the generic
8115 if Is_Entity_Name (Subtype_Mark (Def))
8116 and then Present (Entity (Subtype_Mark (Def)))
8117 then
8118 Ancestor := Get_Instance_Of (Entity (Subtype_Mark (Def)));
8120 -- Check whether parent is defined in a previous formal package
8122 elsif
8123 Scope (Scope (Base_Type (Etype (A_Gen_T)))) = Scope (A_Gen_T)
8124 then
8125 Ancestor :=
8126 Get_Instance_Of (Base_Type (Etype (A_Gen_T)));
8128 -- The type may be a local derivation, or a type extension of
8129 -- a previous formal, or of a formal of a parent package.
8131 elsif Is_Derived_Type (Get_Instance_Of (A_Gen_T))
8132 or else
8133 Ekind (Get_Instance_Of (A_Gen_T)) = E_Record_Type_With_Private
8134 then
8135 -- Check whether the parent is another derived formal type
8136 -- in the same generic unit.
8138 if Etype (A_Gen_T) /= A_Gen_T
8139 and then Is_Generic_Type (Etype (A_Gen_T))
8140 and then Scope (Etype (A_Gen_T)) = Scope (A_Gen_T)
8141 and then Etype (Etype (A_Gen_T)) /= Etype (A_Gen_T)
8142 then
8143 -- Locate ancestor of parent from the subtype declaration
8144 -- created for the actual.
8146 declare
8147 Decl : Node_Id;
8149 begin
8150 Decl := First (Actual_Decls);
8151 while Present (Decl) loop
8152 if Nkind (Decl) = N_Subtype_Declaration
8153 and then Chars (Defining_Identifier (Decl)) =
8154 Chars (Etype (A_Gen_T))
8155 then
8156 Ancestor := Generic_Parent_Type (Decl);
8157 exit;
8158 else
8159 Next (Decl);
8160 end if;
8161 end loop;
8162 end;
8164 pragma Assert (Present (Ancestor));
8166 else
8167 Ancestor :=
8168 Get_Instance_Of (Base_Type (Get_Instance_Of (A_Gen_T)));
8169 end if;
8171 else
8172 Ancestor := Get_Instance_Of (Etype (Base_Type (A_Gen_T)));
8173 end if;
8175 if not Is_Ancestor (Base_Type (Ancestor), Act_T) then
8176 Error_Msg_NE
8177 ("expect type derived from & in instantiation",
8178 Actual, First_Subtype (Ancestor));
8179 Abandon_Instantiation (Actual);
8180 end if;
8182 -- Perform atomic/volatile checks (RM C.6(12))
8184 if Is_Atomic (Act_T) and then not Is_Atomic (Ancestor) then
8185 Error_Msg_N
8186 ("cannot have atomic actual type for non-atomic formal type",
8187 Actual);
8189 elsif Is_Volatile (Act_T)
8190 and then not Is_Volatile (Ancestor)
8191 and then Is_By_Reference_Type (Ancestor)
8192 then
8193 Error_Msg_N
8194 ("cannot have volatile actual type for non-volatile formal type",
8195 Actual);
8196 end if;
8198 -- It should not be necessary to check for unknown discriminants
8199 -- on Formal, but for some reason Has_Unknown_Discriminants is
8200 -- false for A_Gen_T, so Is_Indefinite_Subtype incorrectly
8201 -- returns False. This needs fixing. ???
8203 if not Is_Indefinite_Subtype (A_Gen_T)
8204 and then not Unknown_Discriminants_Present (Formal)
8205 and then Is_Indefinite_Subtype (Act_T)
8206 then
8207 Error_Msg_N
8208 ("actual subtype must be constrained", Actual);
8209 Abandon_Instantiation (Actual);
8210 end if;
8212 if not Unknown_Discriminants_Present (Formal) then
8213 if Is_Constrained (Ancestor) then
8214 if not Is_Constrained (Act_T) then
8215 Error_Msg_N
8216 ("actual subtype must be constrained", Actual);
8217 Abandon_Instantiation (Actual);
8218 end if;
8220 -- Ancestor is unconstrained
8222 elsif Is_Constrained (Act_T) then
8223 if Ekind (Ancestor) = E_Access_Type
8224 or else Is_Composite_Type (Ancestor)
8225 then
8226 Error_Msg_N
8227 ("actual subtype must be unconstrained", Actual);
8228 Abandon_Instantiation (Actual);
8229 end if;
8231 -- A class-wide type is only allowed if the formal has
8232 -- unknown discriminants.
8234 elsif Is_Class_Wide_Type (Act_T)
8235 and then not Has_Unknown_Discriminants (Ancestor)
8236 then
8237 Error_Msg_NE
8238 ("actual for & cannot be a class-wide type", Actual, Gen_T);
8239 Abandon_Instantiation (Actual);
8241 -- Otherwise, the formal and actual shall have the same
8242 -- number of discriminants and each discriminant of the
8243 -- actual must correspond to a discriminant of the formal.
8245 elsif Has_Discriminants (Act_T)
8246 and then not Has_Unknown_Discriminants (Act_T)
8247 and then Has_Discriminants (Ancestor)
8248 then
8249 Actual_Discr := First_Discriminant (Act_T);
8250 Ancestor_Discr := First_Discriminant (Ancestor);
8251 while Present (Actual_Discr)
8252 and then Present (Ancestor_Discr)
8253 loop
8254 if Base_Type (Act_T) /= Base_Type (Ancestor) and then
8255 not Present (Corresponding_Discriminant (Actual_Discr))
8256 then
8257 Error_Msg_NE
8258 ("discriminant & does not correspond " &
8259 "to ancestor discriminant", Actual, Actual_Discr);
8260 Abandon_Instantiation (Actual);
8261 end if;
8263 Next_Discriminant (Actual_Discr);
8264 Next_Discriminant (Ancestor_Discr);
8265 end loop;
8267 if Present (Actual_Discr) or else Present (Ancestor_Discr) then
8268 Error_Msg_NE
8269 ("actual for & must have same number of discriminants",
8270 Actual, Gen_T);
8271 Abandon_Instantiation (Actual);
8272 end if;
8274 -- This case should be caught by the earlier check for
8275 -- for constrainedness, but the check here is added for
8276 -- completeness.
8278 elsif Has_Discriminants (Act_T)
8279 and then not Has_Unknown_Discriminants (Act_T)
8280 then
8281 Error_Msg_NE
8282 ("actual for & must not have discriminants", Actual, Gen_T);
8283 Abandon_Instantiation (Actual);
8285 elsif Has_Discriminants (Ancestor) then
8286 Error_Msg_NE
8287 ("actual for & must have known discriminants", Actual, Gen_T);
8288 Abandon_Instantiation (Actual);
8289 end if;
8291 if not Subtypes_Statically_Compatible (Act_T, Ancestor) then
8292 Error_Msg_N
8293 ("constraint on actual is incompatible with formal", Actual);
8294 Abandon_Instantiation (Actual);
8295 end if;
8296 end if;
8297 end Validate_Derived_Type_Instance;
8299 ------------------------------------
8300 -- Validate_Private_Type_Instance --
8301 ------------------------------------
8303 procedure Validate_Private_Type_Instance is
8304 Formal_Discr : Entity_Id;
8305 Actual_Discr : Entity_Id;
8306 Formal_Subt : Entity_Id;
8308 begin
8309 if Is_Limited_Type (Act_T)
8310 and then not Is_Limited_Type (A_Gen_T)
8311 then
8312 Error_Msg_NE
8313 ("actual for non-limited & cannot be a limited type", Actual,
8314 Gen_T);
8315 Explain_Limited_Type (Act_T, Actual);
8316 Abandon_Instantiation (Actual);
8318 elsif Is_Indefinite_Subtype (Act_T)
8319 and then not Is_Indefinite_Subtype (A_Gen_T)
8320 and then Ada_Version >= Ada_95
8321 then
8322 Error_Msg_NE
8323 ("actual for & must be a definite subtype", Actual, Gen_T);
8325 elsif not Is_Tagged_Type (Act_T)
8326 and then Is_Tagged_Type (A_Gen_T)
8327 then
8328 Error_Msg_NE
8329 ("actual for & must be a tagged type", Actual, Gen_T);
8331 elsif Has_Discriminants (A_Gen_T) then
8332 if not Has_Discriminants (Act_T) then
8333 Error_Msg_NE
8334 ("actual for & must have discriminants", Actual, Gen_T);
8335 Abandon_Instantiation (Actual);
8337 elsif Is_Constrained (Act_T) then
8338 Error_Msg_NE
8339 ("actual for & must be unconstrained", Actual, Gen_T);
8340 Abandon_Instantiation (Actual);
8342 else
8343 Formal_Discr := First_Discriminant (A_Gen_T);
8344 Actual_Discr := First_Discriminant (Act_T);
8345 while Formal_Discr /= Empty loop
8346 if Actual_Discr = Empty then
8347 Error_Msg_NE
8348 ("discriminants on actual do not match formal",
8349 Actual, Gen_T);
8350 Abandon_Instantiation (Actual);
8351 end if;
8353 Formal_Subt := Get_Instance_Of (Etype (Formal_Discr));
8355 -- Access discriminants match if designated types do
8357 if Ekind (Base_Type (Formal_Subt)) = E_Anonymous_Access_Type
8358 and then (Ekind (Base_Type (Etype (Actual_Discr)))) =
8359 E_Anonymous_Access_Type
8360 and then
8361 Get_Instance_Of
8362 (Designated_Type (Base_Type (Formal_Subt))) =
8363 Designated_Type (Base_Type (Etype (Actual_Discr)))
8364 then
8365 null;
8367 elsif Base_Type (Formal_Subt) /=
8368 Base_Type (Etype (Actual_Discr))
8369 then
8370 Error_Msg_NE
8371 ("types of actual discriminants must match formal",
8372 Actual, Gen_T);
8373 Abandon_Instantiation (Actual);
8375 elsif not Subtypes_Statically_Match
8376 (Formal_Subt, Etype (Actual_Discr))
8377 and then Ada_Version >= Ada_95
8378 then
8379 Error_Msg_NE
8380 ("subtypes of actual discriminants must match formal",
8381 Actual, Gen_T);
8382 Abandon_Instantiation (Actual);
8383 end if;
8385 Next_Discriminant (Formal_Discr);
8386 Next_Discriminant (Actual_Discr);
8387 end loop;
8389 if Actual_Discr /= Empty then
8390 Error_Msg_NE
8391 ("discriminants on actual do not match formal",
8392 Actual, Gen_T);
8393 Abandon_Instantiation (Actual);
8394 end if;
8395 end if;
8397 end if;
8399 Ancestor := Gen_T;
8400 end Validate_Private_Type_Instance;
8402 -- Start of processing for Instantiate_Type
8404 begin
8405 if Get_Instance_Of (A_Gen_T) /= A_Gen_T then
8406 Error_Msg_N ("duplicate instantiation of generic type", Actual);
8407 return Error;
8409 elsif not Is_Entity_Name (Actual)
8410 or else not Is_Type (Entity (Actual))
8411 then
8412 Error_Msg_NE
8413 ("expect valid subtype mark to instantiate &", Actual, Gen_T);
8414 Abandon_Instantiation (Actual);
8416 else
8417 Act_T := Entity (Actual);
8419 -- Ada 2005 (AI-216): An Unchecked_Union subtype shall only be passed
8420 -- as a generic actual parameter if the corresponding formal type
8421 -- does not have a known_discriminant_part, or is a formal derived
8422 -- type that is an Unchecked_Union type.
8424 if Is_Unchecked_Union (Base_Type (Act_T)) then
8425 if not Has_Discriminants (A_Gen_T)
8426 or else
8427 (Is_Derived_Type (A_Gen_T)
8428 and then
8429 Is_Unchecked_Union (A_Gen_T))
8430 then
8431 null;
8432 else
8433 Error_Msg_N ("Unchecked_Union cannot be the actual for a" &
8434 " discriminated formal type", Act_T);
8436 end if;
8437 end if;
8439 -- Deal with fixed/floating restrictions
8441 if Is_Floating_Point_Type (Act_T) then
8442 Check_Restriction (No_Floating_Point, Actual);
8443 elsif Is_Fixed_Point_Type (Act_T) then
8444 Check_Restriction (No_Fixed_Point, Actual);
8445 end if;
8447 -- Deal with error of using incomplete type as generic actual
8449 if Ekind (Act_T) = E_Incomplete_Type then
8450 if No (Underlying_Type (Act_T)) then
8451 Error_Msg_N ("premature use of incomplete type", Actual);
8452 Abandon_Instantiation (Actual);
8453 else
8454 Act_T := Full_View (Act_T);
8455 Set_Entity (Actual, Act_T);
8457 if Has_Private_Component (Act_T) then
8458 Error_Msg_N
8459 ("premature use of type with private component", Actual);
8460 end if;
8461 end if;
8463 -- Deal with error of premature use of private type as generic actual
8465 elsif Is_Private_Type (Act_T)
8466 and then Is_Private_Type (Base_Type (Act_T))
8467 and then not Is_Generic_Type (Act_T)
8468 and then not Is_Derived_Type (Act_T)
8469 and then No (Full_View (Root_Type (Act_T)))
8470 then
8471 Error_Msg_N ("premature use of private type", Actual);
8473 elsif Has_Private_Component (Act_T) then
8474 Error_Msg_N
8475 ("premature use of type with private component", Actual);
8476 end if;
8478 Set_Instance_Of (A_Gen_T, Act_T);
8480 -- If the type is generic, the class-wide type may also be used
8482 if Is_Tagged_Type (A_Gen_T)
8483 and then Is_Tagged_Type (Act_T)
8484 and then not Is_Class_Wide_Type (A_Gen_T)
8485 then
8486 Set_Instance_Of (Class_Wide_Type (A_Gen_T),
8487 Class_Wide_Type (Act_T));
8488 end if;
8490 if not Is_Abstract (A_Gen_T)
8491 and then Is_Abstract (Act_T)
8492 then
8493 Error_Msg_N
8494 ("actual of non-abstract formal cannot be abstract", Actual);
8495 end if;
8497 if Is_Scalar_Type (Gen_T) then
8498 Set_Instance_Of (Etype (A_Gen_T), Etype (Act_T));
8499 end if;
8500 end if;
8502 case Nkind (Def) is
8503 when N_Formal_Private_Type_Definition =>
8504 Validate_Private_Type_Instance;
8506 when N_Formal_Derived_Type_Definition =>
8507 Validate_Derived_Type_Instance;
8509 when N_Formal_Discrete_Type_Definition =>
8510 if not Is_Discrete_Type (Act_T) then
8511 Error_Msg_NE
8512 ("expect discrete type in instantiation of&", Actual, Gen_T);
8513 Abandon_Instantiation (Actual);
8514 end if;
8516 when N_Formal_Signed_Integer_Type_Definition =>
8517 if not Is_Signed_Integer_Type (Act_T) then
8518 Error_Msg_NE
8519 ("expect signed integer type in instantiation of&",
8520 Actual, Gen_T);
8521 Abandon_Instantiation (Actual);
8522 end if;
8524 when N_Formal_Modular_Type_Definition =>
8525 if not Is_Modular_Integer_Type (Act_T) then
8526 Error_Msg_NE
8527 ("expect modular type in instantiation of &", Actual, Gen_T);
8528 Abandon_Instantiation (Actual);
8529 end if;
8531 when N_Formal_Floating_Point_Definition =>
8532 if not Is_Floating_Point_Type (Act_T) then
8533 Error_Msg_NE
8534 ("expect float type in instantiation of &", Actual, Gen_T);
8535 Abandon_Instantiation (Actual);
8536 end if;
8538 when N_Formal_Ordinary_Fixed_Point_Definition =>
8539 if not Is_Ordinary_Fixed_Point_Type (Act_T) then
8540 Error_Msg_NE
8541 ("expect ordinary fixed point type in instantiation of &",
8542 Actual, Gen_T);
8543 Abandon_Instantiation (Actual);
8544 end if;
8546 when N_Formal_Decimal_Fixed_Point_Definition =>
8547 if not Is_Decimal_Fixed_Point_Type (Act_T) then
8548 Error_Msg_NE
8549 ("expect decimal type in instantiation of &",
8550 Actual, Gen_T);
8551 Abandon_Instantiation (Actual);
8552 end if;
8554 when N_Array_Type_Definition =>
8555 Validate_Array_Type_Instance;
8557 when N_Access_To_Object_Definition =>
8558 Validate_Access_Type_Instance;
8560 when N_Access_Function_Definition |
8561 N_Access_Procedure_Definition =>
8562 Validate_Access_Subprogram_Instance;
8564 when others =>
8565 raise Program_Error;
8567 end case;
8569 Decl_Node :=
8570 Make_Subtype_Declaration (Loc,
8571 Defining_Identifier => New_Copy (Gen_T),
8572 Subtype_Indication => New_Reference_To (Act_T, Loc));
8574 if Is_Private_Type (Act_T) then
8575 Set_Has_Private_View (Subtype_Indication (Decl_Node));
8577 elsif Is_Access_Type (Act_T)
8578 and then Is_Private_Type (Designated_Type (Act_T))
8579 then
8580 Set_Has_Private_View (Subtype_Indication (Decl_Node));
8581 end if;
8583 -- Flag actual derived types so their elaboration produces the
8584 -- appropriate renamings for the primitive operations of the ancestor.
8585 -- Flag actual for formal private types as well, to determine whether
8586 -- operations in the private part may override inherited operations.
8588 if Nkind (Def) = N_Formal_Derived_Type_Definition
8589 or else Nkind (Def) = N_Formal_Private_Type_Definition
8590 then
8591 Set_Generic_Parent_Type (Decl_Node, Ancestor);
8592 end if;
8594 return Decl_Node;
8595 end Instantiate_Type;
8597 ---------------------
8598 -- Is_In_Main_Unit --
8599 ---------------------
8601 function Is_In_Main_Unit (N : Node_Id) return Boolean is
8602 Unum : constant Unit_Number_Type := Get_Source_Unit (N);
8603 Current_Unit : Node_Id;
8605 begin
8606 if Unum = Main_Unit then
8607 return True;
8609 -- If the current unit is a subunit then it is either the main unit
8610 -- or is being compiled as part of the main unit.
8612 elsif Nkind (N) = N_Compilation_Unit then
8613 return Nkind (Unit (N)) = N_Subunit;
8614 end if;
8616 Current_Unit := Parent (N);
8617 while Present (Current_Unit)
8618 and then Nkind (Current_Unit) /= N_Compilation_Unit
8619 loop
8620 Current_Unit := Parent (Current_Unit);
8621 end loop;
8623 -- The instantiation node is in the main unit, or else the current
8624 -- node (perhaps as the result of nested instantiations) is in the
8625 -- main unit, or in the declaration of the main unit, which in this
8626 -- last case must be a body.
8628 return Unum = Main_Unit
8629 or else Current_Unit = Cunit (Main_Unit)
8630 or else Current_Unit = Library_Unit (Cunit (Main_Unit))
8631 or else (Present (Library_Unit (Current_Unit))
8632 and then Is_In_Main_Unit (Library_Unit (Current_Unit)));
8633 end Is_In_Main_Unit;
8635 ----------------------------
8636 -- Load_Parent_Of_Generic --
8637 ----------------------------
8639 procedure Load_Parent_Of_Generic (N : Node_Id; Spec : Node_Id) is
8640 Comp_Unit : constant Node_Id := Cunit (Get_Source_Unit (Spec));
8641 Save_Style_Check : constant Boolean := Style_Check;
8642 True_Parent : Node_Id;
8643 Inst_Node : Node_Id;
8644 OK : Boolean;
8646 begin
8647 if not In_Same_Source_Unit (N, Spec)
8648 or else Nkind (Unit (Comp_Unit)) = N_Package_Declaration
8649 or else (Nkind (Unit (Comp_Unit)) = N_Package_Body
8650 and then not Is_In_Main_Unit (Spec))
8651 then
8652 -- Find body of parent of spec, and analyze it. A special case
8653 -- arises when the parent is an instantiation, that is to say when
8654 -- we are currently instantiating a nested generic. In that case,
8655 -- there is no separate file for the body of the enclosing instance.
8656 -- Instead, the enclosing body must be instantiated as if it were
8657 -- a pending instantiation, in order to produce the body for the
8658 -- nested generic we require now. Note that in that case the
8659 -- generic may be defined in a package body, the instance defined
8660 -- in the same package body, and the original enclosing body may not
8661 -- be in the main unit.
8663 True_Parent := Parent (Spec);
8664 Inst_Node := Empty;
8666 while Present (True_Parent)
8667 and then Nkind (True_Parent) /= N_Compilation_Unit
8668 loop
8669 if Nkind (True_Parent) = N_Package_Declaration
8670 and then
8671 Nkind (Original_Node (True_Parent)) = N_Package_Instantiation
8672 then
8673 -- Parent is a compilation unit that is an instantiation.
8674 -- Instantiation node has been replaced with package decl.
8676 Inst_Node := Original_Node (True_Parent);
8677 exit;
8679 elsif Nkind (True_Parent) = N_Package_Declaration
8680 and then Present (Generic_Parent (Specification (True_Parent)))
8681 and then Nkind (Parent (True_Parent)) /= N_Compilation_Unit
8682 then
8683 -- Parent is an instantiation within another specification.
8684 -- Declaration for instance has been inserted before original
8685 -- instantiation node. A direct link would be preferable?
8687 Inst_Node := Next (True_Parent);
8689 while Present (Inst_Node)
8690 and then Nkind (Inst_Node) /= N_Package_Instantiation
8691 loop
8692 Next (Inst_Node);
8693 end loop;
8695 -- If the instance appears within a generic, and the generic
8696 -- unit is defined within a formal package of the enclosing
8697 -- generic, there is no generic body available, and none
8698 -- needed. A more precise test should be used ???
8700 if No (Inst_Node) then
8701 return;
8702 end if;
8704 exit;
8705 else
8706 True_Parent := Parent (True_Parent);
8707 end if;
8708 end loop;
8710 -- Case where we are currently instantiating a nested generic
8712 if Present (Inst_Node) then
8713 if Nkind (Parent (True_Parent)) = N_Compilation_Unit then
8715 -- Instantiation node and declaration of instantiated package
8716 -- were exchanged when only the declaration was needed.
8717 -- Restore instantiation node before proceeding with body.
8719 Set_Unit (Parent (True_Parent), Inst_Node);
8720 end if;
8722 -- Now complete instantiation of enclosing body, if it appears
8723 -- in some other unit. If it appears in the current unit, the
8724 -- body will have been instantiated already.
8726 if No (Corresponding_Body (Instance_Spec (Inst_Node))) then
8728 -- We need to determine the expander mode to instantiate
8729 -- the enclosing body. Because the generic body we need
8730 -- may use global entities declared in the enclosing package
8731 -- (including aggregates) it is in general necessary to
8732 -- compile this body with expansion enabled. The exception
8733 -- is if we are within a generic package, in which case
8734 -- the usual generic rule applies.
8736 declare
8737 Exp_Status : Boolean := True;
8738 Scop : Entity_Id;
8740 begin
8741 -- Loop through scopes looking for generic package
8743 Scop := Scope (Defining_Entity (Instance_Spec (Inst_Node)));
8744 while Present (Scop)
8745 and then Scop /= Standard_Standard
8746 loop
8747 if Ekind (Scop) = E_Generic_Package then
8748 Exp_Status := False;
8749 exit;
8750 end if;
8752 Scop := Scope (Scop);
8753 end loop;
8755 Instantiate_Package_Body
8756 (Pending_Body_Info'(
8757 Inst_Node, True_Parent, Exp_Status,
8758 Get_Code_Unit (Sloc (Inst_Node))));
8759 end;
8760 end if;
8762 -- Case where we are not instantiating a nested generic
8764 else
8765 Opt.Style_Check := False;
8766 Expander_Mode_Save_And_Set (True);
8767 Load_Needed_Body (Comp_Unit, OK);
8768 Opt.Style_Check := Save_Style_Check;
8769 Expander_Mode_Restore;
8771 if not OK
8772 and then Unit_Requires_Body (Defining_Entity (Spec))
8773 then
8774 declare
8775 Bname : constant Unit_Name_Type :=
8776 Get_Body_Name (Get_Unit_Name (Unit (Comp_Unit)));
8778 begin
8779 Error_Msg_Unit_1 := Bname;
8780 Error_Msg_N ("this instantiation requires$!", N);
8781 Error_Msg_Name_1 :=
8782 Get_File_Name (Bname, Subunit => False);
8783 Error_Msg_N ("\but file{ was not found!", N);
8784 raise Unrecoverable_Error;
8785 end;
8786 end if;
8787 end if;
8788 end if;
8790 -- If loading the parent of the generic caused an instantiation
8791 -- circularity, we abandon compilation at this point, because
8792 -- otherwise in some cases we get into trouble with infinite
8793 -- recursions after this point.
8795 if Circularity_Detected then
8796 raise Unrecoverable_Error;
8797 end if;
8798 end Load_Parent_Of_Generic;
8800 -----------------------
8801 -- Move_Freeze_Nodes --
8802 -----------------------
8804 procedure Move_Freeze_Nodes
8805 (Out_Of : Entity_Id;
8806 After : Node_Id;
8807 L : List_Id)
8809 Decl : Node_Id;
8810 Next_Decl : Node_Id;
8811 Next_Node : Node_Id := After;
8812 Spec : Node_Id;
8814 function Is_Outer_Type (T : Entity_Id) return Boolean;
8815 -- Check whether entity is declared in a scope external to that
8816 -- of the generic unit.
8818 -------------------
8819 -- Is_Outer_Type --
8820 -------------------
8822 function Is_Outer_Type (T : Entity_Id) return Boolean is
8823 Scop : Entity_Id := Scope (T);
8825 begin
8826 if Scope_Depth (Scop) < Scope_Depth (Out_Of) then
8827 return True;
8829 else
8830 while Scop /= Standard_Standard loop
8832 if Scop = Out_Of then
8833 return False;
8834 else
8835 Scop := Scope (Scop);
8836 end if;
8837 end loop;
8839 return True;
8840 end if;
8841 end Is_Outer_Type;
8843 -- Start of processing for Move_Freeze_Nodes
8845 begin
8846 if No (L) then
8847 return;
8848 end if;
8850 -- First remove the freeze nodes that may appear before all other
8851 -- declarations.
8853 Decl := First (L);
8854 while Present (Decl)
8855 and then Nkind (Decl) = N_Freeze_Entity
8856 and then Is_Outer_Type (Entity (Decl))
8857 loop
8858 Decl := Remove_Head (L);
8859 Insert_After (Next_Node, Decl);
8860 Set_Analyzed (Decl, False);
8861 Next_Node := Decl;
8862 Decl := First (L);
8863 end loop;
8865 -- Next scan the list of declarations and remove each freeze node that
8866 -- appears ahead of the current node.
8868 while Present (Decl) loop
8869 while Present (Next (Decl))
8870 and then Nkind (Next (Decl)) = N_Freeze_Entity
8871 and then Is_Outer_Type (Entity (Next (Decl)))
8872 loop
8873 Next_Decl := Remove_Next (Decl);
8874 Insert_After (Next_Node, Next_Decl);
8875 Set_Analyzed (Next_Decl, False);
8876 Next_Node := Next_Decl;
8877 end loop;
8879 -- If the declaration is a nested package or concurrent type, then
8880 -- recurse. Nested generic packages will have been processed from the
8881 -- inside out.
8883 if Nkind (Decl) = N_Package_Declaration then
8884 Spec := Specification (Decl);
8886 elsif Nkind (Decl) = N_Task_Type_Declaration then
8887 Spec := Task_Definition (Decl);
8889 elsif Nkind (Decl) = N_Protected_Type_Declaration then
8890 Spec := Protected_Definition (Decl);
8892 else
8893 Spec := Empty;
8894 end if;
8896 if Present (Spec) then
8897 Move_Freeze_Nodes (Out_Of, Next_Node,
8898 Visible_Declarations (Spec));
8899 Move_Freeze_Nodes (Out_Of, Next_Node,
8900 Private_Declarations (Spec));
8901 end if;
8903 Next (Decl);
8904 end loop;
8905 end Move_Freeze_Nodes;
8907 ----------------
8908 -- Next_Assoc --
8909 ----------------
8911 function Next_Assoc (E : Assoc_Ptr) return Assoc_Ptr is
8912 begin
8913 return Generic_Renamings.Table (E).Next_In_HTable;
8914 end Next_Assoc;
8916 ------------------------
8917 -- Preanalyze_Actuals --
8918 ------------------------
8920 procedure Pre_Analyze_Actuals (N : Node_Id) is
8921 Assoc : Node_Id;
8922 Act : Node_Id;
8923 Errs : constant Int := Serious_Errors_Detected;
8925 begin
8926 Assoc := First (Generic_Associations (N));
8928 while Present (Assoc) loop
8929 Act := Explicit_Generic_Actual_Parameter (Assoc);
8931 -- Within a nested instantiation, a defaulted actual is an
8932 -- empty association, so nothing to analyze. If the actual for
8933 -- a subprogram is an attribute, analyze prefix only, because
8934 -- actual is not a complete attribute reference.
8936 -- If actual is an allocator, analyze expression only. The full
8937 -- analysis can generate code, and if the instance is a compilation
8938 -- unit we have to wait until the package instance is installed to
8939 -- have a proper place to insert this code.
8941 -- String literals may be operators, but at this point we do not
8942 -- know whether the actual is a formal subprogram or a string.
8944 if No (Act) then
8945 null;
8947 elsif Nkind (Act) = N_Attribute_Reference then
8948 Analyze (Prefix (Act));
8950 elsif Nkind (Act) = N_Explicit_Dereference then
8951 Analyze (Prefix (Act));
8953 elsif Nkind (Act) = N_Allocator then
8954 declare
8955 Expr : constant Node_Id := Expression (Act);
8957 begin
8958 if Nkind (Expr) = N_Subtype_Indication then
8959 Analyze (Subtype_Mark (Expr));
8960 Analyze_List (Constraints (Constraint (Expr)));
8961 else
8962 Analyze (Expr);
8963 end if;
8964 end;
8966 elsif Nkind (Act) /= N_Operator_Symbol then
8967 Analyze (Act);
8968 end if;
8970 if Errs /= Serious_Errors_Detected then
8971 Abandon_Instantiation (Act);
8972 end if;
8974 Next (Assoc);
8975 end loop;
8976 end Pre_Analyze_Actuals;
8978 -------------------
8979 -- Remove_Parent --
8980 -------------------
8982 procedure Remove_Parent (In_Body : Boolean := False) is
8983 S : Entity_Id := Current_Scope;
8984 E : Entity_Id;
8985 P : Entity_Id;
8986 Hidden : Elmt_Id;
8988 begin
8989 -- After child instantiation is complete, remove from scope stack
8990 -- the extra copy of the current scope, and then remove parent
8991 -- instances.
8993 if not In_Body then
8994 Pop_Scope;
8996 while Current_Scope /= S loop
8997 P := Current_Scope;
8998 End_Package_Scope (Current_Scope);
9000 if In_Open_Scopes (P) then
9001 E := First_Entity (P);
9003 while Present (E) loop
9004 Set_Is_Immediately_Visible (E, True);
9005 Next_Entity (E);
9006 end loop;
9008 if Is_Generic_Instance (Current_Scope)
9009 and then P /= Current_Scope
9010 then
9011 -- We are within an instance of some sibling. Retain
9012 -- visibility of parent, for proper subsequent cleanup,
9013 -- and reinstall private declarations as well.
9015 Set_In_Private_Part (P);
9016 Install_Private_Declarations (P);
9017 end if;
9019 -- If the ultimate parent is a compilation unit, reset its
9020 -- visibility to what it was before instantiation.
9022 elsif not In_Open_Scopes (Scope (P))
9023 or else
9024 (not Is_Child_Unit (P) and then not Parent_Unit_Visible)
9025 then
9026 Set_Is_Immediately_Visible (P, False);
9027 end if;
9028 end loop;
9030 -- Reset visibility of entities in the enclosing scope
9032 Set_Is_Hidden_Open_Scope (Current_Scope, False);
9033 Hidden := First_Elmt (Hidden_Entities);
9035 while Present (Hidden) loop
9036 Set_Is_Immediately_Visible (Node (Hidden), True);
9037 Next_Elmt (Hidden);
9038 end loop;
9040 else
9041 -- Each body is analyzed separately, and there is no context
9042 -- that needs preserving from one body instance to the next,
9043 -- so remove all parent scopes that have been installed.
9045 while Present (S) loop
9046 End_Package_Scope (S);
9047 Set_Is_Immediately_Visible (S, False);
9048 S := Current_Scope;
9049 exit when S = Standard_Standard;
9050 end loop;
9051 end if;
9053 end Remove_Parent;
9055 -----------------
9056 -- Restore_Env --
9057 -----------------
9059 procedure Restore_Env is
9060 Saved : Instance_Env renames Instance_Envs.Table (Instance_Envs.Last);
9062 begin
9063 Ada_Version := Saved.Ada_Version;
9065 if No (Current_Instantiated_Parent.Act_Id) then
9067 -- Restore environment after subprogram inlining
9069 Restore_Private_Views (Empty);
9070 end if;
9072 Current_Instantiated_Parent := Saved.Instantiated_Parent;
9073 Exchanged_Views := Saved.Exchanged_Views;
9074 Hidden_Entities := Saved.Hidden_Entities;
9075 Current_Sem_Unit := Saved.Current_Sem_Unit;
9076 Parent_Unit_Visible := Saved.Parent_Unit_Visible;
9078 Instance_Envs.Decrement_Last;
9079 end Restore_Env;
9081 ---------------------------
9082 -- Restore_Private_Views --
9083 ---------------------------
9085 procedure Restore_Private_Views
9086 (Pack_Id : Entity_Id;
9087 Is_Package : Boolean := True)
9089 M : Elmt_Id;
9090 E : Entity_Id;
9091 Typ : Entity_Id;
9092 Dep_Elmt : Elmt_Id;
9093 Dep_Typ : Node_Id;
9095 procedure Restore_Nested_Formal (Formal : Entity_Id);
9096 -- Hide the generic formals of formal packages declared with box
9097 -- which were reachable in the current instantiation.
9099 procedure Restore_Nested_Formal (Formal : Entity_Id) is
9100 Ent : Entity_Id;
9101 begin
9102 if Present (Renamed_Object (Formal))
9103 and then Denotes_Formal_Package (Renamed_Object (Formal), True)
9104 then
9105 return;
9107 elsif Present (Associated_Formal_Package (Formal))
9108 and then Box_Present (Parent (Associated_Formal_Package (Formal)))
9109 then
9110 Ent := First_Entity (Formal);
9112 while Present (Ent) loop
9113 exit when Ekind (Ent) = E_Package
9114 and then Renamed_Entity (Ent) = Renamed_Entity (Formal);
9116 Set_Is_Hidden (Ent);
9117 Set_Is_Potentially_Use_Visible (Ent, False);
9119 -- If package, then recurse
9121 if Ekind (Ent) = E_Package then
9122 Restore_Nested_Formal (Ent);
9123 end if;
9125 Next_Entity (Ent);
9126 end loop;
9127 end if;
9128 end Restore_Nested_Formal;
9130 begin
9131 M := First_Elmt (Exchanged_Views);
9132 while Present (M) loop
9133 Typ := Node (M);
9135 -- Subtypes of types whose views have been exchanged, and that
9136 -- are defined within the instance, were not on the list of
9137 -- Private_Dependents on entry to the instance, so they have to
9138 -- be exchanged explicitly now, in order to remain consistent with
9139 -- the view of the parent type.
9141 if Ekind (Typ) = E_Private_Type
9142 or else Ekind (Typ) = E_Limited_Private_Type
9143 or else Ekind (Typ) = E_Record_Type_With_Private
9144 then
9145 Dep_Elmt := First_Elmt (Private_Dependents (Typ));
9147 while Present (Dep_Elmt) loop
9148 Dep_Typ := Node (Dep_Elmt);
9150 if Scope (Dep_Typ) = Pack_Id
9151 and then Present (Full_View (Dep_Typ))
9152 then
9153 Replace_Elmt (Dep_Elmt, Full_View (Dep_Typ));
9154 Exchange_Declarations (Dep_Typ);
9155 end if;
9157 Next_Elmt (Dep_Elmt);
9158 end loop;
9159 end if;
9161 Exchange_Declarations (Node (M));
9162 Next_Elmt (M);
9163 end loop;
9165 if No (Pack_Id) then
9166 return;
9167 end if;
9169 -- Make the generic formal parameters private, and make the formal
9170 -- types into subtypes of the actuals again.
9172 E := First_Entity (Pack_Id);
9174 while Present (E) loop
9175 Set_Is_Hidden (E, True);
9177 if Is_Type (E)
9178 and then Nkind (Parent (E)) = N_Subtype_Declaration
9179 then
9180 Set_Is_Generic_Actual_Type (E, False);
9182 -- An unusual case of aliasing: the actual may also be directly
9183 -- visible in the generic, and be private there, while it is
9184 -- fully visible in the context of the instance. The internal
9185 -- subtype is private in the instance, but has full visibility
9186 -- like its parent in the enclosing scope. This enforces the
9187 -- invariant that the privacy status of all private dependents of
9188 -- a type coincide with that of the parent type. This can only
9189 -- happen when a generic child unit is instantiated within a
9190 -- sibling.
9192 if Is_Private_Type (E)
9193 and then not Is_Private_Type (Etype (E))
9194 then
9195 Exchange_Declarations (E);
9196 end if;
9198 elsif Ekind (E) = E_Package then
9200 -- The end of the renaming list is the renaming of the generic
9201 -- package itself. If the instance is a subprogram, all entities
9202 -- in the corresponding package are renamings. If this entity is
9203 -- a formal package, make its own formals private as well. The
9204 -- actual in this case is itself the renaming of an instantation.
9205 -- If the entity is not a package renaming, it is the entity
9206 -- created to validate formal package actuals: ignore.
9208 -- If the actual is itself a formal package for the enclosing
9209 -- generic, or the actual for such a formal package, it remains
9210 -- visible on exit from the instance, and therefore nothing
9211 -- needs to be done either, except to keep it accessible.
9213 if Is_Package
9214 and then Renamed_Object (E) = Pack_Id
9215 then
9216 exit;
9218 elsif Nkind (Parent (E)) /= N_Package_Renaming_Declaration then
9219 null;
9221 elsif Denotes_Formal_Package (Renamed_Object (E), True) then
9222 Set_Is_Hidden (E, False);
9224 else
9225 declare
9226 Act_P : constant Entity_Id := Renamed_Object (E);
9227 Id : Entity_Id;
9229 begin
9230 Id := First_Entity (Act_P);
9231 while Present (Id)
9232 and then Id /= First_Private_Entity (Act_P)
9233 loop
9234 exit when Ekind (Id) = E_Package
9235 and then Renamed_Object (Id) = Act_P;
9237 Set_Is_Hidden (Id, True);
9238 Set_Is_Potentially_Use_Visible (Id, In_Use (Act_P));
9240 if Ekind (Id) = E_Package then
9241 Restore_Nested_Formal (Id);
9242 end if;
9244 Next_Entity (Id);
9245 end loop;
9246 end;
9247 end if;
9248 end if;
9250 Next_Entity (E);
9251 end loop;
9252 end Restore_Private_Views;
9254 --------------
9255 -- Save_Env --
9256 --------------
9258 procedure Save_Env
9259 (Gen_Unit : Entity_Id;
9260 Act_Unit : Entity_Id)
9262 begin
9263 Init_Env;
9264 Set_Instance_Env (Gen_Unit, Act_Unit);
9265 end Save_Env;
9267 ----------------------------
9268 -- Save_Global_References --
9269 ----------------------------
9271 procedure Save_Global_References (N : Node_Id) is
9272 Gen_Scope : Entity_Id;
9273 E : Entity_Id;
9274 N2 : Node_Id;
9276 function Is_Global (E : Entity_Id) return Boolean;
9277 -- Check whether entity is defined outside of generic unit.
9278 -- Examine the scope of an entity, and the scope of the scope,
9279 -- etc, until we find either Standard, in which case the entity
9280 -- is global, or the generic unit itself, which indicates that
9281 -- the entity is local. If the entity is the generic unit itself,
9282 -- as in the case of a recursive call, or the enclosing generic unit,
9283 -- if different from the current scope, then it is local as well,
9284 -- because it will be replaced at the point of instantiation. On
9285 -- the other hand, if it is a reference to a child unit of a common
9286 -- ancestor, which appears in an instantiation, it is global because
9287 -- it is used to denote a specific compilation unit at the time the
9288 -- instantiations will be analyzed.
9290 procedure Reset_Entity (N : Node_Id);
9291 -- Save semantic information on global entity, so that it is not
9292 -- resolved again at instantiation time.
9294 procedure Save_Entity_Descendants (N : Node_Id);
9295 -- Apply Save_Global_References to the two syntactic descendants of
9296 -- non-terminal nodes that carry an Associated_Node and are processed
9297 -- through Reset_Entity. Once the global entity (if any) has been
9298 -- captured together with its type, only two syntactic descendants
9299 -- need to be traversed to complete the processing of the tree rooted
9300 -- at N. This applies to Selected_Components, Expanded_Names, and to
9301 -- Operator nodes. N can also be a character literal, identifier, or
9302 -- operator symbol node, but the call has no effect in these cases.
9304 procedure Save_Global_Defaults (N1, N2 : Node_Id);
9305 -- Default actuals in nested instances must be handled specially
9306 -- because there is no link to them from the original tree. When an
9307 -- actual subprogram is given by a default, we add an explicit generic
9308 -- association for it in the instantiation node. When we save the
9309 -- global references on the name of the instance, we recover the list
9310 -- of generic associations, and add an explicit one to the original
9311 -- generic tree, through which a global actual can be preserved.
9312 -- Similarly, if a child unit is instantiated within a sibling, in the
9313 -- context of the parent, we must preserve the identifier of the parent
9314 -- so that it can be properly resolved in a subsequent instantiation.
9316 procedure Save_Global_Descendant (D : Union_Id);
9317 -- Apply Save_Global_References recursively to the descendents of
9318 -- current node.
9320 procedure Save_References (N : Node_Id);
9321 -- This is the recursive procedure that does the work, once the
9322 -- enclosing generic scope has been established.
9324 ---------------
9325 -- Is_Global --
9326 ---------------
9328 function Is_Global (E : Entity_Id) return Boolean is
9329 Se : Entity_Id := Scope (E);
9331 function Is_Instance_Node (Decl : Node_Id) return Boolean;
9332 -- Determine whether the parent node of a reference to a child unit
9333 -- denotes an instantiation or a formal package, in which case the
9334 -- reference to the child unit is global, even if it appears within
9335 -- the current scope (e.g. when the instance appears within the body
9336 -- of an ancestor).
9338 ----------------------
9339 -- Is_Instance_Node --
9340 ----------------------
9342 function Is_Instance_Node (Decl : Node_Id) return Boolean is
9343 begin
9344 return (Nkind (Decl) in N_Generic_Instantiation
9345 or else
9346 Nkind (Original_Node (Decl)) = N_Formal_Package_Declaration);
9347 end Is_Instance_Node;
9349 -- Start of processing for Is_Global
9351 begin
9352 if E = Gen_Scope then
9353 return False;
9355 elsif E = Standard_Standard then
9356 return True;
9358 elsif Is_Child_Unit (E)
9359 and then (Is_Instance_Node (Parent (N2))
9360 or else (Nkind (Parent (N2)) = N_Expanded_Name
9361 and then N2 = Selector_Name (Parent (N2))
9362 and then Is_Instance_Node (Parent (Parent (N2)))))
9363 then
9364 return True;
9366 else
9367 while Se /= Gen_Scope loop
9368 if Se = Standard_Standard then
9369 return True;
9370 else
9371 Se := Scope (Se);
9372 end if;
9373 end loop;
9375 return False;
9376 end if;
9377 end Is_Global;
9379 ------------------
9380 -- Reset_Entity --
9381 ------------------
9383 procedure Reset_Entity (N : Node_Id) is
9385 procedure Set_Global_Type (N : Node_Id; N2 : Node_Id);
9386 -- The type of N2 is global to the generic unit. Save the
9387 -- type in the generic node.
9389 function Top_Ancestor (E : Entity_Id) return Entity_Id;
9390 -- Find the ultimate ancestor of the current unit. If it is
9391 -- not a generic unit, then the name of the current unit
9392 -- in the prefix of an expanded name must be replaced with
9393 -- its generic homonym to ensure that it will be properly
9394 -- resolved in an instance.
9396 ---------------------
9397 -- Set_Global_Type --
9398 ---------------------
9400 procedure Set_Global_Type (N : Node_Id; N2 : Node_Id) is
9401 Typ : constant Entity_Id := Etype (N2);
9403 begin
9404 Set_Etype (N, Typ);
9406 if Entity (N) /= N2
9407 and then Has_Private_View (Entity (N))
9408 then
9409 -- If the entity of N is not the associated node, this is
9410 -- a nested generic and it has an associated node as well,
9411 -- whose type is already the full view (see below). Indicate
9412 -- that the original node has a private view.
9414 Set_Has_Private_View (N);
9415 end if;
9417 -- If not a private type, nothing else to do
9419 if not Is_Private_Type (Typ) then
9420 if Is_Array_Type (Typ)
9421 and then Is_Private_Type (Component_Type (Typ))
9422 then
9423 Set_Has_Private_View (N);
9424 end if;
9426 -- If it is a derivation of a private type in a context where
9427 -- no full view is needed, nothing to do either.
9429 elsif No (Full_View (Typ)) and then Typ /= Etype (Typ) then
9430 null;
9432 -- Otherwise mark the type for flipping and use the full_view
9433 -- when available.
9435 else
9436 Set_Has_Private_View (N);
9438 if Present (Full_View (Typ)) then
9439 Set_Etype (N2, Full_View (Typ));
9440 end if;
9441 end if;
9442 end Set_Global_Type;
9444 ------------------
9445 -- Top_Ancestor --
9446 ------------------
9448 function Top_Ancestor (E : Entity_Id) return Entity_Id is
9449 Par : Entity_Id := E;
9451 begin
9452 while Is_Child_Unit (Par) loop
9453 Par := Scope (Par);
9454 end loop;
9456 return Par;
9457 end Top_Ancestor;
9459 -- Start of processing for Reset_Entity
9461 begin
9462 N2 := Get_Associated_Node (N);
9463 E := Entity (N2);
9465 if Present (E) then
9466 if Is_Global (E) then
9467 Set_Global_Type (N, N2);
9469 elsif Nkind (N) = N_Op_Concat
9470 and then Is_Generic_Type (Etype (N2))
9471 and then
9472 (Base_Type (Etype (Right_Opnd (N2))) = Etype (N2)
9473 or else Base_Type (Etype (Left_Opnd (N2))) = Etype (N2))
9474 and then Is_Intrinsic_Subprogram (E)
9475 then
9476 null;
9478 else
9479 -- Entity is local. Mark generic node as unresolved.
9480 -- Note that now it does not have an entity.
9482 Set_Associated_Node (N, Empty);
9483 Set_Etype (N, Empty);
9484 end if;
9486 if (Nkind (Parent (N)) = N_Package_Instantiation
9487 or else Nkind (Parent (N)) = N_Function_Instantiation
9488 or else Nkind (Parent (N)) = N_Procedure_Instantiation)
9489 and then N = Name (Parent (N))
9490 then
9491 Save_Global_Defaults (Parent (N), Parent (N2));
9492 end if;
9494 elsif Nkind (Parent (N)) = N_Selected_Component
9495 and then Nkind (Parent (N2)) = N_Expanded_Name
9496 then
9498 if Is_Global (Entity (Parent (N2))) then
9499 Change_Selected_Component_To_Expanded_Name (Parent (N));
9500 Set_Associated_Node (Parent (N), Parent (N2));
9501 Set_Global_Type (Parent (N), Parent (N2));
9502 Save_Entity_Descendants (N);
9504 -- If this is a reference to the current generic entity,
9505 -- replace by the name of the generic homonym of the current
9506 -- package. This is because in an instantiation Par.P.Q will
9507 -- not resolve to the name of the instance, whose enclosing
9508 -- scope is not necessarily Par. We use the generic homonym
9509 -- rather that the name of the generic itself, because it may
9510 -- be hidden by a local declaration.
9512 elsif In_Open_Scopes (Entity (Parent (N2)))
9513 and then not
9514 Is_Generic_Unit (Top_Ancestor (Entity (Prefix (Parent (N2)))))
9515 then
9516 if Ekind (Entity (Parent (N2))) = E_Generic_Package then
9517 Rewrite (Parent (N),
9518 Make_Identifier (Sloc (N),
9519 Chars =>
9520 Chars (Generic_Homonym (Entity (Parent (N2))))));
9521 else
9522 Rewrite (Parent (N),
9523 Make_Identifier (Sloc (N),
9524 Chars => Chars (Selector_Name (Parent (N2)))));
9525 end if;
9526 end if;
9528 if (Nkind (Parent (Parent (N))) = N_Package_Instantiation
9529 or else Nkind (Parent (Parent (N)))
9530 = N_Function_Instantiation
9531 or else Nkind (Parent (Parent (N)))
9532 = N_Procedure_Instantiation)
9533 and then Parent (N) = Name (Parent (Parent (N)))
9534 then
9535 Save_Global_Defaults
9536 (Parent (Parent (N)), Parent (Parent ((N2))));
9537 end if;
9539 -- A selected component may denote a static constant that has
9540 -- been folded. Make the same replacement in original tree.
9542 elsif Nkind (Parent (N)) = N_Selected_Component
9543 and then (Nkind (Parent (N2)) = N_Integer_Literal
9544 or else Nkind (Parent (N2)) = N_Real_Literal)
9545 then
9546 Rewrite (Parent (N),
9547 New_Copy (Parent (N2)));
9548 Set_Analyzed (Parent (N), False);
9550 -- A selected component may be transformed into a parameterless
9551 -- function call. If the called entity is global, rewrite the
9552 -- node appropriately, i.e. as an extended name for the global
9553 -- entity.
9555 elsif Nkind (Parent (N)) = N_Selected_Component
9556 and then Nkind (Parent (N2)) = N_Function_Call
9557 and then Is_Global (Entity (Name (Parent (N2))))
9558 then
9559 Change_Selected_Component_To_Expanded_Name (Parent (N));
9560 Set_Associated_Node (Parent (N), Name (Parent (N2)));
9561 Set_Global_Type (Parent (N), Name (Parent (N2)));
9562 Save_Entity_Descendants (N);
9564 else
9565 -- Entity is local. Reset in generic unit, so that node
9566 -- is resolved anew at the point of instantiation.
9568 Set_Associated_Node (N, Empty);
9569 Set_Etype (N, Empty);
9570 end if;
9571 end Reset_Entity;
9573 -----------------------------
9574 -- Save_Entity_Descendants --
9575 -----------------------------
9577 procedure Save_Entity_Descendants (N : Node_Id) is
9578 begin
9579 case Nkind (N) is
9580 when N_Binary_Op =>
9581 Save_Global_Descendant (Union_Id (Left_Opnd (N)));
9582 Save_Global_Descendant (Union_Id (Right_Opnd (N)));
9584 when N_Unary_Op =>
9585 Save_Global_Descendant (Union_Id (Right_Opnd (N)));
9587 when N_Expanded_Name | N_Selected_Component =>
9588 Save_Global_Descendant (Union_Id (Prefix (N)));
9589 Save_Global_Descendant (Union_Id (Selector_Name (N)));
9591 when N_Identifier | N_Character_Literal | N_Operator_Symbol =>
9592 null;
9594 when others =>
9595 raise Program_Error;
9596 end case;
9597 end Save_Entity_Descendants;
9599 --------------------------
9600 -- Save_Global_Defaults --
9601 --------------------------
9603 procedure Save_Global_Defaults (N1, N2 : Node_Id) is
9604 Loc : constant Source_Ptr := Sloc (N1);
9605 Assoc2 : constant List_Id := Generic_Associations (N2);
9606 Gen_Id : constant Entity_Id := Get_Generic_Entity (N2);
9607 Assoc1 : List_Id;
9608 Act1 : Node_Id;
9609 Act2 : Node_Id;
9610 Def : Node_Id;
9611 Ndec : Node_Id;
9612 Subp : Entity_Id;
9613 Actual : Entity_Id;
9615 begin
9616 Assoc1 := Generic_Associations (N1);
9618 if Present (Assoc1) then
9619 Act1 := First (Assoc1);
9620 else
9621 Act1 := Empty;
9622 Set_Generic_Associations (N1, New_List);
9623 Assoc1 := Generic_Associations (N1);
9624 end if;
9626 if Present (Assoc2) then
9627 Act2 := First (Assoc2);
9628 else
9629 return;
9630 end if;
9632 while Present (Act1) and then Present (Act2) loop
9633 Next (Act1);
9634 Next (Act2);
9635 end loop;
9637 -- Find the associations added for default suprograms
9639 if Present (Act2) then
9640 while Nkind (Act2) /= N_Generic_Association
9641 or else No (Entity (Selector_Name (Act2)))
9642 or else not Is_Overloadable (Entity (Selector_Name (Act2)))
9643 loop
9644 Next (Act2);
9645 end loop;
9647 -- Add a similar association if the default is global. The
9648 -- renaming declaration for the actual has been analyzed, and
9649 -- its alias is the program it renames. Link the actual in the
9650 -- original generic tree with the node in the analyzed tree.
9652 while Present (Act2) loop
9653 Subp := Entity (Selector_Name (Act2));
9654 Def := Explicit_Generic_Actual_Parameter (Act2);
9656 -- Following test is defence against rubbish errors
9658 if No (Alias (Subp)) then
9659 return;
9660 end if;
9662 -- Retrieve the resolved actual from the renaming declaration
9663 -- created for the instantiated formal.
9665 Actual := Entity (Name (Parent (Parent (Subp))));
9666 Set_Entity (Def, Actual);
9667 Set_Etype (Def, Etype (Actual));
9669 if Is_Global (Actual) then
9670 Ndec :=
9671 Make_Generic_Association (Loc,
9672 Selector_Name => New_Occurrence_Of (Subp, Loc),
9673 Explicit_Generic_Actual_Parameter =>
9674 New_Occurrence_Of (Actual, Loc));
9676 Set_Associated_Node
9677 (Explicit_Generic_Actual_Parameter (Ndec), Def);
9679 Append (Ndec, Assoc1);
9681 -- If there are other defaults, add a dummy association
9682 -- in case there are other defaulted formals with the same
9683 -- name.
9685 elsif Present (Next (Act2)) then
9686 Ndec :=
9687 Make_Generic_Association (Loc,
9688 Selector_Name => New_Occurrence_Of (Subp, Loc),
9689 Explicit_Generic_Actual_Parameter => Empty);
9691 Append (Ndec, Assoc1);
9692 end if;
9694 Next (Act2);
9695 end loop;
9696 end if;
9698 if Nkind (Name (N1)) = N_Identifier
9699 and then Is_Child_Unit (Gen_Id)
9700 and then Is_Global (Gen_Id)
9701 and then Is_Generic_Unit (Scope (Gen_Id))
9702 and then In_Open_Scopes (Scope (Gen_Id))
9703 then
9704 -- This is an instantiation of a child unit within a sibling,
9705 -- so that the generic parent is in scope. An eventual instance
9706 -- must occur within the scope of an instance of the parent.
9707 -- Make name in instance into an expanded name, to preserve the
9708 -- identifier of the parent, so it can be resolved subsequently.
9710 Rewrite (Name (N2),
9711 Make_Expanded_Name (Loc,
9712 Chars => Chars (Gen_Id),
9713 Prefix => New_Occurrence_Of (Scope (Gen_Id), Loc),
9714 Selector_Name => New_Occurrence_Of (Gen_Id, Loc)));
9715 Set_Entity (Name (N2), Gen_Id);
9717 Rewrite (Name (N1),
9718 Make_Expanded_Name (Loc,
9719 Chars => Chars (Gen_Id),
9720 Prefix => New_Occurrence_Of (Scope (Gen_Id), Loc),
9721 Selector_Name => New_Occurrence_Of (Gen_Id, Loc)));
9723 Set_Associated_Node (Name (N1), Name (N2));
9724 Set_Associated_Node (Prefix (Name (N1)), Empty);
9725 Set_Associated_Node
9726 (Selector_Name (Name (N1)), Selector_Name (Name (N2)));
9727 Set_Etype (Name (N1), Etype (Gen_Id));
9728 end if;
9730 end Save_Global_Defaults;
9732 ----------------------------
9733 -- Save_Global_Descendant --
9734 ----------------------------
9736 procedure Save_Global_Descendant (D : Union_Id) is
9737 N1 : Node_Id;
9739 begin
9740 if D in Node_Range then
9741 if D = Union_Id (Empty) then
9742 null;
9744 elsif Nkind (Node_Id (D)) /= N_Compilation_Unit then
9745 Save_References (Node_Id (D));
9746 end if;
9748 elsif D in List_Range then
9749 if D = Union_Id (No_List)
9750 or else Is_Empty_List (List_Id (D))
9751 then
9752 null;
9754 else
9755 N1 := First (List_Id (D));
9756 while Present (N1) loop
9757 Save_References (N1);
9758 Next (N1);
9759 end loop;
9760 end if;
9762 -- Element list or other non-node field, nothing to do
9764 else
9765 null;
9766 end if;
9767 end Save_Global_Descendant;
9769 ---------------------
9770 -- Save_References --
9771 ---------------------
9773 -- This is the recursive procedure that does the work, once the
9774 -- enclosing generic scope has been established. We have to treat
9775 -- specially a number of node rewritings that are required by semantic
9776 -- processing and which change the kind of nodes in the generic copy:
9777 -- typically constant-folding, replacing an operator node by a string
9778 -- literal, or a selected component by an expanded name. In each of
9779 -- those cases, the transformation is propagated to the generic unit.
9781 procedure Save_References (N : Node_Id) is
9782 begin
9783 if N = Empty then
9784 null;
9786 elsif Nkind (N) = N_Character_Literal
9787 or else Nkind (N) = N_Operator_Symbol
9788 then
9789 if Nkind (N) = Nkind (Get_Associated_Node (N)) then
9790 Reset_Entity (N);
9792 elsif Nkind (N) = N_Operator_Symbol
9793 and then Nkind (Get_Associated_Node (N)) = N_String_Literal
9794 then
9795 Change_Operator_Symbol_To_String_Literal (N);
9796 end if;
9798 elsif Nkind (N) in N_Op then
9800 if Nkind (N) = Nkind (Get_Associated_Node (N)) then
9802 if Nkind (N) = N_Op_Concat then
9803 Set_Is_Component_Left_Opnd (N,
9804 Is_Component_Left_Opnd (Get_Associated_Node (N)));
9806 Set_Is_Component_Right_Opnd (N,
9807 Is_Component_Right_Opnd (Get_Associated_Node (N)));
9808 end if;
9810 Reset_Entity (N);
9811 else
9812 -- Node may be transformed into call to a user-defined operator
9814 N2 := Get_Associated_Node (N);
9816 if Nkind (N2) = N_Function_Call then
9817 E := Entity (Name (N2));
9819 if Present (E)
9820 and then Is_Global (E)
9821 then
9822 Set_Etype (N, Etype (N2));
9823 else
9824 Set_Associated_Node (N, Empty);
9825 Set_Etype (N, Empty);
9826 end if;
9828 elsif Nkind (N2) = N_Integer_Literal
9829 or else Nkind (N2) = N_Real_Literal
9830 or else Nkind (N2) = N_String_Literal
9831 then
9832 -- Operation was constant-folded, perform the same
9833 -- replacement in generic.
9835 Rewrite (N, New_Copy (N2));
9836 Set_Analyzed (N, False);
9838 elsif Nkind (N2) = N_Identifier
9839 and then Ekind (Entity (N2)) = E_Enumeration_Literal
9840 then
9841 -- Same if call was folded into a literal, but in this
9842 -- case retain the entity to avoid spurious ambiguities
9843 -- if id is overloaded at the point of instantiation or
9844 -- inlining.
9846 Rewrite (N, New_Copy (N2));
9847 Set_Analyzed (N, False);
9848 end if;
9849 end if;
9851 -- Complete the check on operands, if node has not been
9852 -- constant-folded.
9854 if Nkind (N) in N_Op then
9855 Save_Entity_Descendants (N);
9856 end if;
9858 elsif Nkind (N) = N_Identifier then
9859 if Nkind (N) = Nkind (Get_Associated_Node (N)) then
9861 -- If this is a discriminant reference, always save it.
9862 -- It is used in the instance to find the corresponding
9863 -- discriminant positionally rather than by name.
9865 Set_Original_Discriminant
9866 (N, Original_Discriminant (Get_Associated_Node (N)));
9867 Reset_Entity (N);
9869 else
9870 N2 := Get_Associated_Node (N);
9872 if Nkind (N2) = N_Function_Call then
9873 E := Entity (Name (N2));
9875 -- Name resolves to a call to parameterless function.
9876 -- If original entity is global, mark node as resolved.
9878 if Present (E)
9879 and then Is_Global (E)
9880 then
9881 Set_Etype (N, Etype (N2));
9882 else
9883 Set_Associated_Node (N, Empty);
9884 Set_Etype (N, Empty);
9885 end if;
9887 elsif
9888 Nkind (N2) = N_Integer_Literal or else
9889 Nkind (N2) = N_Real_Literal or else
9890 Nkind (N2) = N_String_Literal
9891 then
9892 -- Name resolves to named number that is constant-folded,
9893 -- or to string literal from concatenation.
9894 -- Perform the same replacement in generic.
9896 Rewrite (N, New_Copy (N2));
9897 Set_Analyzed (N, False);
9899 elsif Nkind (N2) = N_Explicit_Dereference then
9901 -- An identifier is rewritten as a dereference if it is
9902 -- the prefix in a selected component, and it denotes an
9903 -- access to a composite type, or a parameterless function
9904 -- call that returns an access type.
9906 -- Check whether corresponding entity in prefix is global
9908 if Is_Entity_Name (Prefix (N2))
9909 and then Present (Entity (Prefix (N2)))
9910 and then Is_Global (Entity (Prefix (N2)))
9911 then
9912 Rewrite (N,
9913 Make_Explicit_Dereference (Sloc (N),
9914 Prefix => Make_Identifier (Sloc (N),
9915 Chars => Chars (N))));
9916 Set_Associated_Node (Prefix (N), Prefix (N2));
9918 elsif Nkind (Prefix (N2)) = N_Function_Call
9919 and then Is_Global (Entity (Name (Prefix (N2))))
9920 then
9921 Rewrite (N,
9922 Make_Explicit_Dereference (Sloc (N),
9923 Prefix => Make_Function_Call (Sloc (N),
9924 Name =>
9925 Make_Identifier (Sloc (N),
9926 Chars => Chars (N)))));
9928 Set_Associated_Node
9929 (Name (Prefix (N)), Name (Prefix (N2)));
9931 else
9932 Set_Associated_Node (N, Empty);
9933 Set_Etype (N, Empty);
9934 end if;
9936 -- The subtype mark of a nominally unconstrained object
9937 -- is rewritten as a subtype indication using the bounds
9938 -- of the expression. Recover the original subtype mark.
9940 elsif Nkind (N2) = N_Subtype_Indication
9941 and then Is_Entity_Name (Original_Node (N2))
9942 then
9943 Set_Associated_Node (N, Original_Node (N2));
9944 Reset_Entity (N);
9946 else
9947 null;
9948 end if;
9949 end if;
9951 elsif Nkind (N) in N_Entity then
9952 null;
9954 else
9955 declare
9956 use Atree.Unchecked_Access;
9957 -- This code section is part of implementing an untyped tree
9958 -- traversal, so it needs direct access to node fields.
9960 begin
9961 if Nkind (N) = N_Aggregate
9962 or else
9963 Nkind (N) = N_Extension_Aggregate
9964 then
9965 N2 := Get_Associated_Node (N);
9967 if No (N2)
9968 or else No (Etype (N2))
9969 or else not Is_Global (Etype (N2))
9970 then
9971 Set_Associated_Node (N, Empty);
9972 end if;
9974 Save_Global_Descendant (Field1 (N));
9975 Save_Global_Descendant (Field2 (N));
9976 Save_Global_Descendant (Field3 (N));
9977 Save_Global_Descendant (Field5 (N));
9979 -- All other cases than aggregates
9981 else
9982 Save_Global_Descendant (Field1 (N));
9983 Save_Global_Descendant (Field2 (N));
9984 Save_Global_Descendant (Field3 (N));
9985 Save_Global_Descendant (Field4 (N));
9986 Save_Global_Descendant (Field5 (N));
9987 end if;
9988 end;
9989 end if;
9990 end Save_References;
9992 -- Start of processing for Save_Global_References
9994 begin
9995 Gen_Scope := Current_Scope;
9997 -- If the generic unit is a child unit, references to entities in
9998 -- the parent are treated as local, because they will be resolved
9999 -- anew in the context of the instance of the parent.
10001 while Is_Child_Unit (Gen_Scope)
10002 and then Ekind (Scope (Gen_Scope)) = E_Generic_Package
10003 loop
10004 Gen_Scope := Scope (Gen_Scope);
10005 end loop;
10007 Save_References (N);
10008 end Save_Global_References;
10010 --------------------------------------
10011 -- Set_Copied_Sloc_For_Inlined_Body --
10012 --------------------------------------
10014 procedure Set_Copied_Sloc_For_Inlined_Body (N : Node_Id; E : Entity_Id) is
10015 begin
10016 Create_Instantiation_Source (N, E, True, S_Adjustment);
10017 end Set_Copied_Sloc_For_Inlined_Body;
10019 ---------------------
10020 -- Set_Instance_Of --
10021 ---------------------
10023 procedure Set_Instance_Of (A : Entity_Id; B : Entity_Id) is
10024 begin
10025 Generic_Renamings.Table (Generic_Renamings.Last) := (A, B, Assoc_Null);
10026 Generic_Renamings_HTable.Set (Generic_Renamings.Last);
10027 Generic_Renamings.Increment_Last;
10028 end Set_Instance_Of;
10030 --------------------
10031 -- Set_Next_Assoc --
10032 --------------------
10034 procedure Set_Next_Assoc (E : Assoc_Ptr; Next : Assoc_Ptr) is
10035 begin
10036 Generic_Renamings.Table (E).Next_In_HTable := Next;
10037 end Set_Next_Assoc;
10039 -------------------
10040 -- Start_Generic --
10041 -------------------
10043 procedure Start_Generic is
10044 begin
10045 -- ??? I am sure more things could be factored out in this
10046 -- routine. Should probably be done at a later stage.
10048 Generic_Flags.Increment_Last;
10049 Generic_Flags.Table (Generic_Flags.Last) := Inside_A_Generic;
10050 Inside_A_Generic := True;
10052 Expander_Mode_Save_And_Set (False);
10053 end Start_Generic;
10055 ----------------------
10056 -- Set_Instance_Env --
10057 ----------------------
10059 procedure Set_Instance_Env
10060 (Gen_Unit : Entity_Id;
10061 Act_Unit : Entity_Id)
10064 begin
10065 -- Regardless of the current mode, predefined units are analyzed in
10066 -- the most current Ada mode, and earlier version Ada checks do not
10067 -- apply to predefined units.
10069 if Is_Internal_File_Name
10070 (Fname => Unit_File_Name (Get_Source_Unit (Gen_Unit)),
10071 Renamings_Included => True) then
10072 Ada_Version := Ada_Version_Type'Last;
10073 end if;
10075 Current_Instantiated_Parent := (Gen_Unit, Act_Unit, Assoc_Null);
10076 end Set_Instance_Env;
10078 -----------------
10079 -- Switch_View --
10080 -----------------
10082 procedure Switch_View (T : Entity_Id) is
10083 BT : constant Entity_Id := Base_Type (T);
10084 Priv_Elmt : Elmt_Id := No_Elmt;
10085 Priv_Sub : Entity_Id;
10087 begin
10088 -- T may be private but its base type may have been exchanged through
10089 -- some other occurrence, in which case there is nothing to switch.
10091 if not Is_Private_Type (BT) then
10092 return;
10093 end if;
10095 Priv_Elmt := First_Elmt (Private_Dependents (BT));
10097 if Present (Full_View (BT)) then
10098 Append_Elmt (Full_View (BT), Exchanged_Views);
10099 Exchange_Declarations (BT);
10100 end if;
10102 while Present (Priv_Elmt) loop
10103 Priv_Sub := (Node (Priv_Elmt));
10105 -- We avoid flipping the subtype if the Etype of its full
10106 -- view is private because this would result in a malformed
10107 -- subtype. This occurs when the Etype of the subtype full
10108 -- view is the full view of the base type (and since the
10109 -- base types were just switched, the subtype is pointing
10110 -- to the wrong view). This is currently the case for
10111 -- tagged record types, access types (maybe more?) and
10112 -- needs to be resolved. ???
10114 if Present (Full_View (Priv_Sub))
10115 and then not Is_Private_Type (Etype (Full_View (Priv_Sub)))
10116 then
10117 Append_Elmt (Full_View (Priv_Sub), Exchanged_Views);
10118 Exchange_Declarations (Priv_Sub);
10119 end if;
10121 Next_Elmt (Priv_Elmt);
10122 end loop;
10123 end Switch_View;
10125 -----------------------------
10126 -- Valid_Default_Attribute --
10127 -----------------------------
10129 procedure Valid_Default_Attribute (Nam : Entity_Id; Def : Node_Id) is
10130 Attr_Id : constant Attribute_Id :=
10131 Get_Attribute_Id (Attribute_Name (Def));
10132 T : constant Entity_Id := Entity (Prefix (Def));
10133 Is_Fun : constant Boolean := (Ekind (Nam) = E_Function);
10134 F : Entity_Id;
10135 Num_F : Int;
10136 OK : Boolean;
10138 begin
10139 if No (T)
10140 or else T = Any_Id
10141 then
10142 return;
10143 end if;
10145 Num_F := 0;
10146 F := First_Formal (Nam);
10147 while Present (F) loop
10148 Num_F := Num_F + 1;
10149 Next_Formal (F);
10150 end loop;
10152 case Attr_Id is
10153 when Attribute_Adjacent | Attribute_Ceiling | Attribute_Copy_Sign |
10154 Attribute_Floor | Attribute_Fraction | Attribute_Machine |
10155 Attribute_Model | Attribute_Remainder | Attribute_Rounding |
10156 Attribute_Unbiased_Rounding =>
10157 OK := Is_Fun
10158 and then Num_F = 1
10159 and then Is_Floating_Point_Type (T);
10161 when Attribute_Image | Attribute_Pred | Attribute_Succ |
10162 Attribute_Value | Attribute_Wide_Image |
10163 Attribute_Wide_Value =>
10164 OK := (Is_Fun and then Num_F = 1 and then Is_Scalar_Type (T));
10166 when Attribute_Max | Attribute_Min =>
10167 OK := (Is_Fun and then Num_F = 2 and then Is_Scalar_Type (T));
10169 when Attribute_Input =>
10170 OK := (Is_Fun and then Num_F = 1);
10172 when Attribute_Output | Attribute_Read | Attribute_Write =>
10173 OK := (not Is_Fun and then Num_F = 2);
10175 when others =>
10176 OK := False;
10177 end case;
10179 if not OK then
10180 Error_Msg_N ("attribute reference has wrong profile for subprogram",
10181 Def);
10182 end if;
10183 end Valid_Default_Attribute;
10185 end Sem_Ch12;