1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
9 -- Copyright (C) 1992-2003, Free Software Foundation, Inc. --
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. --
22 -- GNAT was originally developed by the GNAT team at New York University. --
23 -- Extensive contributions were provided by Ada Core Technologies Inc. --
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;
36 with Inline; use Inline;
38 with Lib.Load; use Lib.Load;
39 with Lib.Xref; use Lib.Xref;
40 with Nlists; use Nlists;
41 with Nmake; use Nmake;
43 with Restrict; use Restrict;
44 with Rtsfind; use Rtsfind;
46 with Sem_Cat; use Sem_Cat;
47 with Sem_Ch3; use Sem_Ch3;
48 with Sem_Ch6; use Sem_Ch6;
49 with Sem_Ch7; use Sem_Ch7;
50 with Sem_Ch8; use Sem_Ch8;
51 with Sem_Ch10; use Sem_Ch10;
52 with Sem_Ch13; use Sem_Ch13;
53 with Sem_Elab; use Sem_Elab;
54 with Sem_Elim; use Sem_Elim;
55 with Sem_Eval; use Sem_Eval;
56 with Sem_Res; use Sem_Res;
57 with Sem_Type; use Sem_Type;
58 with Sem_Util; use Sem_Util;
59 with Sem_Warn; use Sem_Warn;
60 with Stand; use Stand;
61 with Sinfo; use Sinfo;
62 with Sinfo.CN; use Sinfo.CN;
63 with Sinput; use Sinput;
64 with Sinput.L; use Sinput.L;
65 with Snames; use Snames;
66 with Stringt; use Stringt;
67 with Uname; use Uname;
69 with Tbuild; use Tbuild;
70 with Uintp; use Uintp;
71 with Urealp; use Urealp;
75 package body Sem_Ch12 is
77 ----------------------------------------------------------
78 -- Implementation of Generic Analysis and Instantiation --
79 -----------------------------------------------------------
81 -- GNAT implements generics by macro expansion. No attempt is made to
82 -- share generic instantiations (for now). Analysis of a generic definition
83 -- does not perform any expansion action, but the expander must be called
84 -- on the tree for each instantiation, because the expansion may of course
85 -- depend on the generic actuals. All of this is best achieved as follows:
87 -- a) Semantic analysis of a generic unit is performed on a copy of the
88 -- tree for the generic unit. All tree modifications that follow analysis
89 -- do not affect the original tree. Links are kept between the original
90 -- tree and the copy, in order to recognize non-local references within
91 -- the generic, and propagate them to each instance (recall that name
92 -- resolution is done on the generic declaration: generics are not really
93 -- macros!). This is summarized in the following diagram:
95 -- .-----------. .----------.
96 -- | semantic |<--------------| generic |
98 -- | |==============>| |
99 -- |___________| global |__________|
110 -- b) Each instantiation copies the original tree, and inserts into it a
111 -- series of declarations that describe the mapping between generic formals
112 -- and actuals. For example, a generic In OUT parameter is an object
113 -- renaming of the corresponing actual, etc. Generic IN parameters are
114 -- constant declarations.
116 -- c) In order to give the right visibility for these renamings, we use
117 -- a different scheme for package and subprogram instantiations. For
118 -- packages, the list of renamings is inserted into the package
119 -- specification, before the visible declarations of the package. The
120 -- renamings are analyzed before any of the text of the instance, and are
121 -- thus visible at the right place. Furthermore, outside of the instance,
122 -- the generic parameters are visible and denote their corresponding
125 -- For subprograms, we create a container package to hold the renamings
126 -- and the subprogram instance itself. Analysis of the package makes the
127 -- renaming declarations visible to the subprogram. After analyzing the
128 -- package, the defining entity for the subprogram is touched-up so that
129 -- it appears declared in the current scope, and not inside the container
132 -- If the instantiation is a compilation unit, the container package is
133 -- given the same name as the subprogram instance. This ensures that
134 -- the elaboration procedure called by the binder, using the compilation
135 -- unit name, calls in fact the elaboration procedure for the package.
137 -- Not surprisingly, private types complicate this approach. By saving in
138 -- the original generic object the non-local references, we guarantee that
139 -- the proper entities are referenced at the point of instantiation.
140 -- However, for private types, this by itself does not insure that the
141 -- proper VIEW of the entity is used (the full type may be visible at the
142 -- point of generic definition, but not at instantiation, or vice-versa).
143 -- In order to reference the proper view, we special-case any reference
144 -- to private types in the generic object, by saving both views, one in
145 -- the generic and one in the semantic copy. At time of instantiation, we
146 -- check whether the two views are consistent, and exchange declarations if
147 -- necessary, in order to restore the correct visibility. Similarly, if
148 -- the instance view is private when the generic view was not, we perform
149 -- the exchange. After completing the instantiation, we restore the
150 -- current visibility. The flag Has_Private_View marks identifiers in the
151 -- the generic unit that require checking.
153 -- Visibility within nested generic units requires special handling.
154 -- Consider the following scheme:
156 -- type Global is ... -- outside of generic unit.
160 -- type Semi_Global is ... -- global to inner.
163 -- procedure inner (X1 : Global; X2 : Semi_Global);
165 -- procedure in2 is new inner (...); -- 4
168 -- package New_Outer is new Outer (...); -- 2
169 -- procedure New_Inner is new New_Outer.Inner (...); -- 3
171 -- The semantic analysis of Outer captures all occurrences of Global.
172 -- The semantic analysis of Inner (at 1) captures both occurrences of
173 -- Global and Semi_Global.
175 -- At point 2 (instantiation of Outer), we also produce a generic copy
176 -- of Inner, even though Inner is, at that point, not being instantiated.
177 -- (This is just part of the semantic analysis of New_Outer).
179 -- Critically, references to Global within Inner must be preserved, while
180 -- references to Semi_Global should not preserved, because they must now
181 -- resolve to an entity within New_Outer. To distinguish between these, we
182 -- use a global variable, Current_Instantiated_Parent, which is set when
183 -- performing a generic copy during instantiation (at 2). This variable is
184 -- used when performing a generic copy that is not an instantiation, but
185 -- that is nested within one, as the occurrence of 1 within 2. The analysis
186 -- of a nested generic only preserves references that are global to the
187 -- enclosing Current_Instantiated_Parent. We use the Scope_Depth value to
188 -- determine whether a reference is external to the given parent.
190 -- The instantiation at point 3 requires no special treatment. The method
191 -- works as well for further nestings of generic units, but of course the
192 -- variable Current_Instantiated_Parent must be stacked because nested
193 -- instantiations can occur, e.g. the occurrence of 4 within 2.
195 -- The instantiation of package and subprogram bodies is handled in a
196 -- similar manner, except that it is delayed until after semantic
197 -- analysis is complete. In this fashion complex cross-dependencies
198 -- between several package declarations and bodies containing generics
199 -- can be compiled which otherwise would diagnose spurious circularities.
201 -- For example, it is possible to compile two packages A and B that
202 -- have the following structure:
204 -- package A is package B is
205 -- generic ... generic ...
206 -- package G_A is package G_B is
209 -- package body A is package body B is
210 -- package N_B is new G_B (..) package N_A is new G_A (..)
212 -- The table Pending_Instantiations in package Inline is used to keep
213 -- track of body instantiations that are delayed in this manner. Inline
214 -- handles the actual calls to do the body instantiations. This activity
215 -- is part of Inline, since the processing occurs at the same point, and
216 -- for essentially the same reason, as the handling of inlined routines.
218 ----------------------------------------------
219 -- Detection of Instantiation Circularities --
220 ----------------------------------------------
222 -- If we have a chain of instantiations that is circular, this is a
223 -- static error which must be detected at compile time. The detection
224 -- of these circularities is carried out at the point that we insert
225 -- a generic instance spec or body. If there is a circularity, then
226 -- the analysis of the offending spec or body will eventually result
227 -- in trying to load the same unit again, and we detect this problem
228 -- as we analyze the package instantiation for the second time.
230 -- At least in some cases after we have detected the circularity, we
231 -- get into trouble if we try to keep going. The following flag is
232 -- set if a circularity is detected, and used to abandon compilation
233 -- after the messages have been posted.
235 Circularity_Detected : Boolean := False;
236 -- This should really be reset on encountering a new main unit, but in
237 -- practice we are not using multiple main units so it is not critical.
239 -----------------------
240 -- Local subprograms --
241 -----------------------
243 procedure Abandon_Instantiation (N : Node_Id);
244 pragma No_Return (Abandon_Instantiation);
245 -- Posts an error message "instantiation abandoned" at the indicated
246 -- node and then raises the exception Instantiation_Error to do it.
248 procedure Analyze_Formal_Array_Type
249 (T : in out Entity_Id;
251 -- A formal array type is treated like an array type declaration, and
252 -- invokes Array_Type_Declaration (sem_ch3) whose first parameter is
253 -- in-out, because in the case of an anonymous type the entity is
254 -- actually created in the procedure.
256 -- The following procedures treat other kinds of formal parameters.
258 procedure Analyze_Formal_Derived_Type
263 -- All the following need comments???
265 procedure Analyze_Formal_Decimal_Fixed_Point_Type
266 (T : Entity_Id; Def : Node_Id);
267 procedure Analyze_Formal_Discrete_Type (T : Entity_Id; Def : Node_Id);
268 procedure Analyze_Formal_Floating_Type (T : Entity_Id; Def : Node_Id);
269 procedure Analyze_Formal_Signed_Integer_Type (T : Entity_Id; Def : Node_Id);
270 procedure Analyze_Formal_Modular_Type (T : Entity_Id; Def : Node_Id);
271 procedure Analyze_Formal_Ordinary_Fixed_Point_Type
272 (T : Entity_Id; Def : Node_Id);
274 procedure Analyze_Formal_Private_Type
278 -- This needs comments???
280 procedure Analyze_Generic_Formal_Part (N : Node_Id);
282 procedure Analyze_Generic_Access_Type (T : Entity_Id; Def : Node_Id);
283 -- This needs comments ???
285 function Analyze_Associations
290 -- At instantiation time, build the list of associations between formals
291 -- and actuals. Each association becomes a renaming declaration for the
292 -- formal entity. F_Copy is the analyzed list of formals in the generic
293 -- copy. It is used to apply legality checks to the actuals. I_Node is the
294 -- instantiation node itself.
296 procedure Analyze_Subprogram_Instantiation
300 procedure Build_Instance_Compilation_Unit_Nodes
304 -- This procedure is used in the case where the generic instance of a
305 -- subprogram body or package body is a library unit. In this case, the
306 -- original library unit node for the generic instantiation must be
307 -- replaced by the resulting generic body, and a link made to a new
308 -- compilation unit node for the generic declaration. The argument N is
309 -- the original generic instantiation. Act_Body and Act_Decl are the body
310 -- and declaration of the instance (either package body and declaration
311 -- nodes or subprogram body and declaration nodes depending on the case).
312 -- On return, the node N has been rewritten with the actual body.
314 procedure Check_Formal_Packages (P_Id : Entity_Id);
315 -- Apply the following to all formal packages in generic associations.
317 procedure Check_Formal_Package_Instance
318 (Formal_Pack : Entity_Id;
319 Actual_Pack : Entity_Id);
320 -- Verify that the actuals of the actual instance match the actuals of
321 -- the template for a formal package that is not declared with a box.
323 procedure Check_Forward_Instantiation (Decl : Node_Id);
324 -- If the generic is a local entity and the corresponding body has not
325 -- been seen yet, flag enclosing packages to indicate that it will be
326 -- elaborated after the generic body. Subprograms declared in the same
327 -- package cannot be inlined by the front-end because front-end inlining
328 -- requires a strict linear order of elaboration.
330 procedure Check_Hidden_Child_Unit
332 Gen_Unit : Entity_Id;
333 Act_Decl_Id : Entity_Id);
334 -- If the generic unit is an implicit child instance within a parent
335 -- instance, we need to make an explicit test that it is not hidden by
336 -- a child instance of the same name and parent.
338 procedure Check_Private_View (N : Node_Id);
339 -- Check whether the type of a generic entity has a different view between
340 -- the point of generic analysis and the point of instantiation. If the
341 -- view has changed, then at the point of instantiation we restore the
342 -- correct view to perform semantic analysis of the instance, and reset
343 -- the current view after instantiation. The processing is driven by the
344 -- current private status of the type of the node, and Has_Private_View,
345 -- a flag that is set at the point of generic compilation. If view and
346 -- flag are inconsistent then the type is updated appropriately.
348 procedure Check_Generic_Actuals
349 (Instance : Entity_Id;
350 Is_Formal_Box : Boolean);
351 -- Similar to previous one. Check the actuals in the instantiation,
352 -- whose views can change between the point of instantiation and the point
353 -- of instantiation of the body. In addition, mark the generic renamings
354 -- as generic actuals, so that they are not compatible with other actuals.
355 -- Recurse on an actual that is a formal package whose declaration has
358 function Contains_Instance_Of
363 -- Inner is instantiated within the generic Outer. Check whether Inner
364 -- directly or indirectly contains an instance of Outer or of one of its
365 -- parents, in the case of a subunit. Each generic unit holds a list of
366 -- the entities instantiated within (at any depth). This procedure
367 -- determines whether the set of such lists contains a cycle, i.e. an
368 -- illegal circular instantiation.
370 function Denotes_Formal_Package (Pack : Entity_Id) return Boolean;
371 -- Returns True if E is a formal package of an enclosing generic, or
372 -- the actual for such a formal in an enclosing instantiation. Used in
373 -- Restore_Private_Views, to keep the formals of such a package visible
374 -- on exit from an inner instantiation.
376 function Find_Actual_Type
378 Gen_Scope : Entity_Id)
380 -- When validating the actual types of a child instance, check whether
381 -- the formal is a formal type of the parent unit, and retrieve the current
382 -- actual for it. Typ is the entity in the analyzed formal type declaration
383 -- (component or index type of an array type) and Gen_Scope is the scope of
384 -- the analyzed formal array type.
386 function Get_Package_Instantiation_Node (A : Entity_Id) return Node_Id;
387 -- Given the entity of a unit that is an instantiation, retrieve the
388 -- original instance node. This is used when loading the instantiations
389 -- of the ancestors of a child generic that is being instantiated.
391 function In_Same_Declarative_Part
395 -- True if the instantiation Inst and the given freeze_node F_Node appear
396 -- within the same declarative part, ignoring subunits, but with no inter-
397 -- vening suprograms or concurrent units. If true, the freeze node
398 -- of the instance can be placed after the freeze node of the parent,
399 -- which it itself an instance.
401 procedure Set_Instance_Env
402 (Gen_Unit : Entity_Id;
403 Act_Unit : Entity_Id);
404 -- Save current instance on saved environment, to be used to determine
405 -- the global status of entities in nested instances. Part of Save_Env.
406 -- called after verifying that the generic unit is legal for the instance.
408 procedure Set_Instance_Of (A : Entity_Id; B : Entity_Id);
409 -- Associate analyzed generic parameter with corresponding
410 -- instance. Used for semantic checks at instantiation time.
412 function Has_Been_Exchanged (E : Entity_Id) return Boolean;
413 -- Traverse the Exchanged_Views list to see if a type was private
414 -- and has already been flipped during this phase of instantiation.
416 procedure Hide_Current_Scope;
417 -- When compiling a generic child unit, the parent context must be
418 -- present, but the instance and all entities that may be generated
419 -- must be inserted in the current scope. We leave the current scope
420 -- on the stack, but make its entities invisible to avoid visibility
421 -- problems. This is reversed at the end of instantiations. This is
422 -- not done for the instantiation of the bodies, which only require the
423 -- instances of the generic parents to be in scope.
425 procedure Install_Body
430 -- If the instantiation happens textually before the body of the generic,
431 -- the instantiation of the body must be analyzed after the generic body,
432 -- and not at the point of instantiation. Such early instantiations can
433 -- happen if the generic and the instance appear in a package declaration
434 -- because the generic body can only appear in the corresponding package
435 -- body. Early instantiations can also appear if generic, instance and
436 -- body are all in the declarative part of a subprogram or entry. Entities
437 -- of packages that are early instantiations are delayed, and their freeze
438 -- node appears after the generic body.
440 procedure Insert_After_Last_Decl (N : Node_Id; F_Node : Node_Id);
441 -- Insert freeze node at the end of the declarative part that includes the
442 -- instance node N. If N is in the visible part of an enclosing package
443 -- declaration, the freeze node has to be inserted at the end of the
444 -- private declarations, if any.
446 procedure Freeze_Subprogram_Body
447 (Inst_Node : Node_Id;
449 Pack_Id : Entity_Id);
450 -- The generic body may appear textually after the instance, including
451 -- in the proper body of a stub, or within a different package instance.
452 -- Given that the instance can only be elaborated after the generic, we
453 -- place freeze_nodes for the instance and/or for packages that may enclose
454 -- the instance and the generic, so that the back-end can establish the
455 -- proper order of elaboration.
458 -- Establish environment for subsequent instantiation. Separated from
459 -- Save_Env because data-structures for visibility handling must be
460 -- initialized before call to Check_Generic_Child_Unit.
462 procedure Install_Parent (P : Entity_Id; In_Body : Boolean := False);
463 -- When compiling an instance of a child unit the parent (which is
464 -- itself an instance) is an enclosing scope that must be made
465 -- immediately visible. This procedure is also used to install the non-
466 -- generic parent of a generic child unit when compiling its body, so that
467 -- full views of types in the parent are made visible.
469 procedure Remove_Parent (In_Body : Boolean := False);
470 -- Reverse effect after instantiation of child is complete.
472 procedure Inline_Instance_Body
474 Gen_Unit : Entity_Id;
476 -- If front-end inlining is requested, instantiate the package body,
477 -- and preserve the visibility of its compilation unit, to insure
478 -- that successive instantiations succeed.
480 -- The functions Instantiate_XXX perform various legality checks and build
481 -- the declarations for instantiated generic parameters.
482 -- Need to describe what the parameters are ???
484 function Instantiate_Object
487 Analyzed_Formal : Node_Id)
490 function Instantiate_Type
493 Analyzed_Formal : Node_Id;
494 Actual_Decls : List_Id)
497 function Instantiate_Formal_Subprogram
500 Analyzed_Formal : Node_Id)
503 function Instantiate_Formal_Package
506 Analyzed_Formal : Node_Id)
508 -- If the formal package is declared with a box, special visibility rules
509 -- apply to its formals: they are in the visible part of the package. This
510 -- is true in the declarative region of the formal package, that is to say
511 -- in the enclosing generic or instantiation. For an instantiation, the
512 -- parameters of the formal package are made visible in an explicit step.
513 -- Furthermore, if the actual is a visible use_clause, these formals must
514 -- be made potentially use_visible as well. On exit from the enclosing
515 -- instantiation, the reverse must be done.
517 -- For a formal package declared without a box, there are conformance rules
518 -- that apply to the actuals in the generic declaration and the actuals of
519 -- the actual package in the enclosing instantiation. The simplest way to
520 -- apply these rules is to repeat the instantiation of the formal package
521 -- in the context of the enclosing instance, and compare the generic
522 -- associations of this instantiation with those of the actual package.
524 function Is_In_Main_Unit (N : Node_Id) return Boolean;
525 -- Test if given node is in the main unit
527 procedure Load_Parent_Of_Generic (N : Node_Id; Spec : Node_Id);
528 -- If the generic appears in a separate non-generic library unit,
529 -- load the corresponding body to retrieve the body of the generic.
530 -- N is the node for the generic instantiation, Spec is the generic
531 -- package declaration.
533 procedure Inherit_Context (Gen_Decl : Node_Id; Inst : Node_Id);
534 -- Add the context clause of the unit containing a generic unit to
535 -- an instantiation that is a compilation unit.
537 function Get_Associated_Node (N : Node_Id) return Node_Id;
538 -- In order to propagate semantic information back from the analyzed
539 -- copy to the original generic, we maintain links between selected nodes
540 -- in the generic and their corresponding copies. At the end of generic
541 -- analysis, the routine Save_Global_References traverses the generic
542 -- tree, examines the semantic information, and preserves the links to
543 -- those nodes that contain global information. At instantiation, the
544 -- information from the associated node is placed on the new copy, so
545 -- that name resolution is not repeated.
547 -- Three kinds of source nodes have associated nodes:
549 -- a) those that can reference (denote) entities, that is identifiers,
550 -- character literals, expanded_names, operator symbols, operators,
551 -- and attribute reference nodes. These nodes have an Entity field
552 -- and are the set of nodes that are in N_Has_Entity.
554 -- b) aggregates (N_Aggregate and N_Extension_Aggregate)
556 -- c) selected components (N_Selected_Component)
558 -- For the first class, the associated node preserves the entity if it is
559 -- global. If the generic contains nested instantiations, the associated
560 -- node itself has been recopied, and a chain of them must be followed.
562 -- For aggregates, the associated node allows retrieval of the type, which
563 -- may otherwise not appear in the generic. The view of this type may be
564 -- different between generic and instantiation, and the full view can be
565 -- installed before the instantiation is analyzed. For aggregates of
566 -- type extensions, the same view exchange may have to be performed for
567 -- some of the ancestor types, if their view is private at the point of
570 -- Nodes that are selected components in the parse tree may be rewritten
571 -- as expanded names after resolution, and must be treated as potential
572 -- entity holders. which is why they also have an Associated_Node.
574 -- Nodes that do not come from source, such as freeze nodes, do not appear
575 -- in the generic tree, and need not have an associated node.
577 -- The associated node is stored in the Associated_Node field. Note that
578 -- this field overlaps Entity, which is fine, because the whole point is
579 -- that we don't need or want the normal Entity field in this situation.
581 procedure Move_Freeze_Nodes
585 -- Freeze nodes can be generated in the analysis of a generic unit, but
586 -- will not be seen by the back-end. It is necessary to move those nodes
587 -- to the enclosing scope if they freeze an outer entity. We place them
588 -- at the end of the enclosing generic package, which is semantically
591 procedure Pre_Analyze_Actuals (N : Node_Id);
592 -- Analyze actuals to perform name resolution. Full resolution is done
593 -- later, when the expected types are known, but names have to be captured
594 -- before installing parents of generics, that are not visible for the
595 -- actuals themselves.
597 procedure Valid_Default_Attribute (Nam : Entity_Id; Def : Node_Id);
598 -- Verify that an attribute that appears as the default for a formal
599 -- subprogram is a function or procedure with the correct profile.
601 -------------------------------------------
602 -- Data Structures for Generic Renamings --
603 -------------------------------------------
605 -- The map Generic_Renamings associates generic entities with their
606 -- corresponding actuals. Currently used to validate type instances.
607 -- It will eventually be used for all generic parameters to eliminate
608 -- the need for overload resolution in the instance.
610 type Assoc_Ptr is new Int;
612 Assoc_Null : constant Assoc_Ptr := -1;
617 Next_In_HTable : Assoc_Ptr;
620 package Generic_Renamings is new Table.Table
621 (Table_Component_Type => Assoc,
622 Table_Index_Type => Assoc_Ptr,
623 Table_Low_Bound => 0,
625 Table_Increment => 100,
626 Table_Name => "Generic_Renamings");
628 -- Variable to hold enclosing instantiation. When the environment is
629 -- saved for a subprogram inlining, the corresponding Act_Id is empty.
631 Current_Instantiated_Parent : Assoc := (Empty, Empty, Assoc_Null);
633 -- Hash table for associations
635 HTable_Size : constant := 37;
636 type HTable_Range is range 0 .. HTable_Size - 1;
638 procedure Set_Next_Assoc (E : Assoc_Ptr; Next : Assoc_Ptr);
639 function Next_Assoc (E : Assoc_Ptr) return Assoc_Ptr;
640 function Get_Gen_Id (E : Assoc_Ptr) return Entity_Id;
641 function Hash (F : Entity_Id) return HTable_Range;
643 package Generic_Renamings_HTable is new GNAT.HTable.Static_HTable (
644 Header_Num => HTable_Range,
646 Elmt_Ptr => Assoc_Ptr,
647 Null_Ptr => Assoc_Null,
648 Set_Next => Set_Next_Assoc,
651 Get_Key => Get_Gen_Id,
655 Exchanged_Views : Elist_Id;
656 -- This list holds the private views that have been exchanged during
657 -- instantiation to restore the visibility of the generic declaration.
658 -- (see comments above). After instantiation, the current visibility is
659 -- reestablished by means of a traversal of this list.
661 Hidden_Entities : Elist_Id;
662 -- This list holds the entities of the current scope that are removed
663 -- from immediate visibility when instantiating a child unit. Their
664 -- visibility is restored in Remove_Parent.
666 -- Because instantiations can be recursive, the following must be saved
667 -- on entry and restored on exit from an instantiation (spec or body).
668 -- This is done by the two procedures Save_Env and Restore_Env. For
669 -- package and subprogram instantiations (but not for the body instances)
670 -- the action of Save_Env is done in two steps: Init_Env is called before
671 -- Check_Generic_Child_Unit, because setting the parent instances requires
672 -- that the visibility data structures be properly initialized. Once the
673 -- generic is unit is validated, Set_Instance_Env completes Save_Env.
675 type Instance_Env is record
677 Instantiated_Parent : Assoc;
678 Exchanged_Views : Elist_Id;
679 Hidden_Entities : Elist_Id;
680 Current_Sem_Unit : Unit_Number_Type;
683 package Instance_Envs is new Table.Table (
684 Table_Component_Type => Instance_Env,
685 Table_Index_Type => Int,
686 Table_Low_Bound => 0,
688 Table_Increment => 100,
689 Table_Name => "Instance_Envs");
691 procedure Restore_Private_Views
692 (Pack_Id : Entity_Id;
693 Is_Package : Boolean := True);
694 -- Restore the private views of external types, and unmark the generic
695 -- renamings of actuals, so that they become comptible subtypes again.
696 -- For subprograms, Pack_Id is the package constructed to hold the
699 procedure Switch_View (T : Entity_Id);
700 -- Switch the partial and full views of a type and its private
701 -- dependents (i.e. its subtypes and derived types).
703 ------------------------------------
704 -- Structures for Error Reporting --
705 ------------------------------------
707 Instantiation_Node : Node_Id;
708 -- Used by subprograms that validate instantiation of formal parameters
709 -- where there might be no actual on which to place the error message.
710 -- Also used to locate the instantiation node for generic subunits.
712 Instantiation_Error : exception;
713 -- When there is a semantic error in the generic parameter matching,
714 -- there is no point in continuing the instantiation, because the
715 -- number of cascaded errors is unpredictable. This exception aborts
716 -- the instantiation process altogether.
718 S_Adjustment : Sloc_Adjustment;
719 -- Offset created for each node in an instantiation, in order to keep
720 -- track of the source position of the instantiation in each of its nodes.
721 -- A subsequent semantic error or warning on a construct of the instance
722 -- points to both places: the original generic node, and the point of
723 -- instantiation. See Sinput and Sinput.L for additional details.
725 ------------------------------------------------------------
726 -- Data structure for keeping track when inside a Generic --
727 ------------------------------------------------------------
729 -- The following table is used to save values of the Inside_A_Generic
730 -- flag (see spec of Sem) when they are saved by Start_Generic.
732 package Generic_Flags is new Table.Table (
733 Table_Component_Type => Boolean,
734 Table_Index_Type => Int,
735 Table_Low_Bound => 0,
737 Table_Increment => 200,
738 Table_Name => "Generic_Flags");
740 ---------------------------
741 -- Abandon_Instantiation --
742 ---------------------------
744 procedure Abandon_Instantiation (N : Node_Id) is
746 Error_Msg_N ("instantiation abandoned!", N);
747 raise Instantiation_Error;
748 end Abandon_Instantiation;
750 --------------------------
751 -- Analyze_Associations --
752 --------------------------
754 function Analyze_Associations
760 Actual_Types : constant Elist_Id := New_Elmt_List;
761 Assoc : constant List_Id := New_List;
762 Defaults : constant Elist_Id := New_Elmt_List;
766 Next_Formal : Node_Id;
767 Temp_Formal : Node_Id;
768 Analyzed_Formal : Node_Id;
771 First_Named : Node_Id := Empty;
772 Found_Assoc : Node_Id;
773 Is_Named_Assoc : Boolean;
774 Num_Matched : Int := 0;
775 Num_Actuals : Int := 0;
777 function Matching_Actual
781 -- Find actual that corresponds to a given a formal parameter. If the
782 -- actuals are positional, return the next one, if any. If the actuals
783 -- are named, scan the parameter associations to find the right one.
784 -- A_F is the corresponding entity in the analyzed generic,which is
785 -- placed on the selector name for ASIS use.
787 procedure Set_Analyzed_Formal;
788 -- Find the node in the generic copy that corresponds to a given formal.
789 -- The semantic information on this node is used to perform legality
790 -- checks on the actuals. Because semantic analysis can introduce some
791 -- anonymous entities or modify the declaration node itself, the
792 -- correspondence between the two lists is not one-one. In addition to
793 -- anonymous types, the presence a formal equality will introduce an
794 -- implicit declaration for the corresponding inequality.
796 ---------------------
797 -- Matching_Actual --
798 ---------------------
800 function Matching_Actual
809 Is_Named_Assoc := False;
811 -- End of list of purely positional parameters
816 -- Case of positional parameter corresponding to current formal
818 elsif No (Selector_Name (Actual)) then
819 Found := Explicit_Generic_Actual_Parameter (Actual);
820 Found_Assoc := Actual;
821 Num_Matched := Num_Matched + 1;
824 -- Otherwise scan list of named actuals to find the one with the
825 -- desired name. All remaining actuals have explicit names.
828 Is_Named_Assoc := True;
832 while Present (Actual) loop
833 if Chars (Selector_Name (Actual)) = Chars (F) then
834 Found := Explicit_Generic_Actual_Parameter (Actual);
835 Set_Entity (Selector_Name (Actual), A_F);
836 Set_Etype (Selector_Name (Actual), Etype (A_F));
837 Generate_Reference (A_F, Selector_Name (Actual));
838 Found_Assoc := Actual;
839 Num_Matched := Num_Matched + 1;
847 -- Reset for subsequent searches. In most cases the named
848 -- associations are in order. If they are not, we reorder them
849 -- to avoid scanning twice the same actual. This is not just a
850 -- question of efficiency: there may be multiple defaults with
851 -- boxes that have the same name. In a nested instantiation we
852 -- insert actuals for those defaults, and cannot rely on their
853 -- names to disambiguate them.
855 if Actual = First_Named then
858 elsif Present (Actual) then
859 Insert_Before (First_Named, Remove_Next (Prev));
862 Actual := First_Named;
868 -------------------------
869 -- Set_Analyzed_Formal --
870 -------------------------
872 procedure Set_Analyzed_Formal is
875 while Present (Analyzed_Formal) loop
876 Kind := Nkind (Analyzed_Formal);
878 case Nkind (Formal) is
880 when N_Formal_Subprogram_Declaration =>
881 exit when Kind = N_Formal_Subprogram_Declaration
884 (Defining_Unit_Name (Specification (Formal))) =
886 (Defining_Unit_Name (Specification (Analyzed_Formal)));
888 when N_Formal_Package_Declaration =>
890 Kind = N_Formal_Package_Declaration
892 Kind = N_Generic_Package_Declaration;
894 when N_Use_Package_Clause | N_Use_Type_Clause => exit;
898 -- Skip freeze nodes, and nodes inserted to replace
899 -- unrecognized pragmas.
902 Kind /= N_Formal_Subprogram_Declaration
903 and then Kind /= N_Subprogram_Declaration
904 and then Kind /= N_Freeze_Entity
905 and then Kind /= N_Null_Statement
906 and then Kind /= N_Itype_Reference
907 and then Chars (Defining_Identifier (Formal)) =
908 Chars (Defining_Identifier (Analyzed_Formal));
911 Next (Analyzed_Formal);
914 end Set_Analyzed_Formal;
916 -- Start of processing for Analyze_Associations
919 -- If named associations are present, save the first named association
920 -- (it may of course be Empty) to facilitate subsequent name search.
922 Actuals := Generic_Associations (I_Node);
924 if Present (Actuals) then
925 First_Named := First (Actuals);
927 while Present (First_Named)
928 and then No (Selector_Name (First_Named))
930 Num_Actuals := Num_Actuals + 1;
935 Named := First_Named;
936 while Present (Named) loop
937 if No (Selector_Name (Named)) then
938 Error_Msg_N ("invalid positional actual after named one", Named);
939 Abandon_Instantiation (Named);
942 -- A named association may lack an actual parameter, if it was
943 -- introduced for a default subprogram that turns out to be local
944 -- to the outer instantiation.
946 if Present (Explicit_Generic_Actual_Parameter (Named)) then
947 Num_Actuals := Num_Actuals + 1;
953 if Present (Formals) then
954 Formal := First_Non_Pragma (Formals);
955 Analyzed_Formal := First_Non_Pragma (F_Copy);
957 if Present (Actuals) then
958 Actual := First (Actuals);
960 -- All formals should have default values
966 while Present (Formal) loop
968 Next_Formal := Next_Non_Pragma (Formal);
970 case Nkind (Formal) is
971 when N_Formal_Object_Declaration =>
974 Defining_Identifier (Formal),
975 Defining_Identifier (Analyzed_Formal));
978 (Instantiate_Object (Formal, Match, Analyzed_Formal),
981 when N_Formal_Type_Declaration =>
984 Defining_Identifier (Formal),
985 Defining_Identifier (Analyzed_Formal));
988 Error_Msg_NE ("missing actual for instantiation of &",
989 Instantiation_Node, Defining_Identifier (Formal));
990 Abandon_Instantiation (Instantiation_Node);
996 (Formal, Match, Analyzed_Formal, Assoc));
998 -- an instantiation is a freeze point for the actuals,
999 -- unless this is a rewritten formal package.
1001 if Nkind (I_Node) /= N_Formal_Package_Declaration then
1002 Append_Elmt (Entity (Match), Actual_Types);
1006 -- A remote access-to-class-wide type must not be an
1007 -- actual parameter for a generic formal of an access
1008 -- type (E.2.2 (17)).
1010 if Nkind (Analyzed_Formal) = N_Formal_Type_Declaration
1012 Nkind (Formal_Type_Definition (Analyzed_Formal)) =
1013 N_Access_To_Object_Definition
1015 Validate_Remote_Access_To_Class_Wide_Type (Match);
1018 when N_Formal_Subprogram_Declaration =>
1021 Defining_Unit_Name (Specification (Formal)),
1022 Defining_Unit_Name (Specification (Analyzed_Formal)));
1024 -- If the formal subprogram has the same name as
1025 -- another formal subprogram of the generic, then
1026 -- a named association is illegal (12.3(9)). Exclude
1027 -- named associations that are generated for a nested
1031 and then Is_Named_Assoc
1032 and then Comes_From_Source (Found_Assoc)
1034 Temp_Formal := First (Formals);
1035 while Present (Temp_Formal) loop
1036 if Nkind (Temp_Formal) =
1037 N_Formal_Subprogram_Declaration
1038 and then Temp_Formal /= Formal
1040 Chars (Selector_Name (Found_Assoc)) =
1041 Chars (Defining_Unit_Name
1042 (Specification (Temp_Formal)))
1045 ("name not allowed for overloaded formal",
1047 Abandon_Instantiation (Instantiation_Node);
1055 Instantiate_Formal_Subprogram
1056 (Formal, Match, Analyzed_Formal));
1059 and then Box_Present (Formal)
1062 (Defining_Unit_Name (Specification (Last (Assoc))),
1066 when N_Formal_Package_Declaration =>
1069 Defining_Identifier (Formal),
1070 Defining_Identifier (Original_Node (Analyzed_Formal)));
1074 ("missing actual for instantiation of&",
1076 Defining_Identifier (Formal));
1078 Abandon_Instantiation (Instantiation_Node);
1083 (Instantiate_Formal_Package
1084 (Formal, Match, Analyzed_Formal),
1088 -- For use type and use package appearing in the context
1089 -- clause, we have already copied them, so we can just
1090 -- move them where they belong (we mustn't recopy them
1091 -- since this would mess up the Sloc values).
1093 when N_Use_Package_Clause |
1094 N_Use_Type_Clause =>
1096 Append (Formal, Assoc);
1099 raise Program_Error;
1103 Formal := Next_Formal;
1104 Next_Non_Pragma (Analyzed_Formal);
1107 if Num_Actuals > Num_Matched then
1109 ("unmatched actuals in instantiation", Instantiation_Node);
1112 elsif Present (Actuals) then
1114 ("too many actuals in generic instantiation", Instantiation_Node);
1118 Elmt : Elmt_Id := First_Elmt (Actual_Types);
1121 while Present (Elmt) loop
1122 Freeze_Before (I_Node, Node (Elmt));
1127 -- If there are default subprograms, normalize the tree by adding
1128 -- explicit associations for them. This is required if the instance
1129 -- appears within a generic.
1137 Elmt := First_Elmt (Defaults);
1138 while Present (Elmt) loop
1139 if No (Actuals) then
1140 Actuals := New_List;
1141 Set_Generic_Associations (I_Node, Actuals);
1144 Subp := Node (Elmt);
1146 Make_Generic_Association (Sloc (Subp),
1147 Selector_Name => New_Occurrence_Of (Subp, Sloc (Subp)),
1148 Explicit_Generic_Actual_Parameter =>
1149 New_Occurrence_Of (Subp, Sloc (Subp)));
1150 Mark_Rewrite_Insertion (New_D);
1151 Append_To (Actuals, New_D);
1157 end Analyze_Associations;
1159 -------------------------------
1160 -- Analyze_Formal_Array_Type --
1161 -------------------------------
1163 procedure Analyze_Formal_Array_Type
1164 (T : in out Entity_Id;
1170 -- Treated like a non-generic array declaration, with
1171 -- additional semantic checks.
1175 if Nkind (Def) = N_Constrained_Array_Definition then
1176 DSS := First (Discrete_Subtype_Definitions (Def));
1177 while Present (DSS) loop
1178 if Nkind (DSS) = N_Subtype_Indication
1179 or else Nkind (DSS) = N_Range
1180 or else Nkind (DSS) = N_Attribute_Reference
1182 Error_Msg_N ("only a subtype mark is allowed in a formal", DSS);
1189 Array_Type_Declaration (T, Def);
1190 Set_Is_Generic_Type (Base_Type (T));
1192 if Ekind (Component_Type (T)) = E_Incomplete_Type
1193 and then No (Full_View (Component_Type (T)))
1195 Error_Msg_N ("premature usage of incomplete type", Def);
1197 elsif Is_Internal (Component_Type (T))
1198 and then Nkind (Original_Node (Subtype_Indication (Def)))
1199 /= N_Attribute_Reference
1202 ("only a subtype mark is allowed in a formal",
1203 Subtype_Indication (Def));
1206 end Analyze_Formal_Array_Type;
1208 ---------------------------------------------
1209 -- Analyze_Formal_Decimal_Fixed_Point_Type --
1210 ---------------------------------------------
1212 -- As for other generic types, we create a valid type representation
1213 -- with legal but arbitrary attributes, whose values are never considered
1214 -- static. For all scalar types we introduce an anonymous base type, with
1215 -- the same attributes. We choose the corresponding integer type to be
1216 -- Standard_Integer.
1218 procedure Analyze_Formal_Decimal_Fixed_Point_Type
1222 Loc : constant Source_Ptr := Sloc (Def);
1223 Base : constant Entity_Id :=
1225 (E_Decimal_Fixed_Point_Type,
1226 Current_Scope, Sloc (Def), 'G');
1227 Int_Base : constant Entity_Id := Standard_Integer;
1228 Delta_Val : constant Ureal := Ureal_1;
1229 Digs_Val : constant Uint := Uint_6;
1234 Set_Etype (Base, Base);
1235 Set_Size_Info (Base, Int_Base);
1236 Set_RM_Size (Base, RM_Size (Int_Base));
1237 Set_First_Rep_Item (Base, First_Rep_Item (Int_Base));
1238 Set_Digits_Value (Base, Digs_Val);
1239 Set_Delta_Value (Base, Delta_Val);
1240 Set_Small_Value (Base, Delta_Val);
1241 Set_Scalar_Range (Base,
1243 Low_Bound => Make_Real_Literal (Loc, Ureal_1),
1244 High_Bound => Make_Real_Literal (Loc, Ureal_1)));
1246 Set_Is_Generic_Type (Base);
1247 Set_Parent (Base, Parent (Def));
1249 Set_Ekind (T, E_Decimal_Fixed_Point_Subtype);
1250 Set_Etype (T, Base);
1251 Set_Size_Info (T, Int_Base);
1252 Set_RM_Size (T, RM_Size (Int_Base));
1253 Set_First_Rep_Item (T, First_Rep_Item (Int_Base));
1254 Set_Digits_Value (T, Digs_Val);
1255 Set_Delta_Value (T, Delta_Val);
1256 Set_Small_Value (T, Delta_Val);
1257 Set_Scalar_Range (T, Scalar_Range (Base));
1259 Check_Restriction (No_Fixed_Point, Def);
1260 end Analyze_Formal_Decimal_Fixed_Point_Type;
1262 ---------------------------------
1263 -- Analyze_Formal_Derived_Type --
1264 ---------------------------------
1266 procedure Analyze_Formal_Derived_Type
1271 Loc : constant Source_Ptr := Sloc (Def);
1272 Unk_Disc : constant Boolean := Unknown_Discriminants_Present (N);
1276 Set_Is_Generic_Type (T);
1278 if Private_Present (Def) then
1280 Make_Private_Extension_Declaration (Loc,
1281 Defining_Identifier => T,
1282 Discriminant_Specifications => Discriminant_Specifications (N),
1283 Unknown_Discriminants_Present => Unk_Disc,
1284 Subtype_Indication => Subtype_Mark (Def));
1286 Set_Abstract_Present (New_N, Abstract_Present (Def));
1290 Make_Full_Type_Declaration (Loc,
1291 Defining_Identifier => T,
1292 Discriminant_Specifications =>
1293 Discriminant_Specifications (Parent (T)),
1295 Make_Derived_Type_Definition (Loc,
1296 Subtype_Indication => Subtype_Mark (Def)));
1298 Set_Abstract_Present
1299 (Type_Definition (New_N), Abstract_Present (Def));
1306 if not Is_Composite_Type (T) then
1308 ("unknown discriminants not allowed for elementary types", N);
1310 Set_Has_Unknown_Discriminants (T);
1311 Set_Is_Constrained (T, False);
1315 -- If the parent type has a known size, so does the formal, which
1316 -- makes legal representation clauses that involve the formal.
1318 Set_Size_Known_At_Compile_Time
1319 (T, Size_Known_At_Compile_Time (Entity (Subtype_Mark (Def))));
1321 end Analyze_Formal_Derived_Type;
1323 ----------------------------------
1324 -- Analyze_Formal_Discrete_Type --
1325 ----------------------------------
1327 -- The operations defined for a discrete types are those of an
1328 -- enumeration type. The size is set to an arbitrary value, for use
1329 -- in analyzing the generic unit.
1331 procedure Analyze_Formal_Discrete_Type (T : Entity_Id; Def : Node_Id) is
1332 Loc : constant Source_Ptr := Sloc (Def);
1338 Set_Ekind (T, E_Enumeration_Type);
1343 -- For semantic analysis, the bounds of the type must be set to some
1344 -- non-static value. The simplest is to create attribute nodes for
1345 -- those bounds, that refer to the type itself. These bounds are never
1346 -- analyzed but serve as place-holders.
1349 Make_Attribute_Reference (Loc,
1350 Attribute_Name => Name_First,
1351 Prefix => New_Reference_To (T, Loc));
1355 Make_Attribute_Reference (Loc,
1356 Attribute_Name => Name_Last,
1357 Prefix => New_Reference_To (T, Loc));
1360 Set_Scalar_Range (T,
1365 end Analyze_Formal_Discrete_Type;
1367 ----------------------------------
1368 -- Analyze_Formal_Floating_Type --
1369 ---------------------------------
1371 procedure Analyze_Formal_Floating_Type (T : Entity_Id; Def : Node_Id) is
1372 Base : constant Entity_Id :=
1374 (E_Floating_Point_Type, Current_Scope, Sloc (Def), 'G');
1377 -- The various semantic attributes are taken from the predefined type
1378 -- Float, just so that all of them are initialized. Their values are
1379 -- never used because no constant folding or expansion takes place in
1380 -- the generic itself.
1383 Set_Ekind (T, E_Floating_Point_Subtype);
1384 Set_Etype (T, Base);
1385 Set_Size_Info (T, (Standard_Float));
1386 Set_RM_Size (T, RM_Size (Standard_Float));
1387 Set_Digits_Value (T, Digits_Value (Standard_Float));
1388 Set_Scalar_Range (T, Scalar_Range (Standard_Float));
1390 Set_Is_Generic_Type (Base);
1391 Set_Etype (Base, Base);
1392 Set_Size_Info (Base, (Standard_Float));
1393 Set_RM_Size (Base, RM_Size (Standard_Float));
1394 Set_Digits_Value (Base, Digits_Value (Standard_Float));
1395 Set_Scalar_Range (Base, Scalar_Range (Standard_Float));
1396 Set_Parent (Base, Parent (Def));
1398 Check_Restriction (No_Floating_Point, Def);
1399 end Analyze_Formal_Floating_Type;
1401 ---------------------------------
1402 -- Analyze_Formal_Modular_Type --
1403 ---------------------------------
1405 procedure Analyze_Formal_Modular_Type (T : Entity_Id; Def : Node_Id) is
1407 -- Apart from their entity kind, generic modular types are treated
1408 -- like signed integer types, and have the same attributes.
1410 Analyze_Formal_Signed_Integer_Type (T, Def);
1411 Set_Ekind (T, E_Modular_Integer_Subtype);
1412 Set_Ekind (Etype (T), E_Modular_Integer_Type);
1414 end Analyze_Formal_Modular_Type;
1416 ---------------------------------------
1417 -- Analyze_Formal_Object_Declaration --
1418 ---------------------------------------
1420 procedure Analyze_Formal_Object_Declaration (N : Node_Id) is
1421 E : constant Node_Id := Expression (N);
1422 Id : constant Node_Id := Defining_Identifier (N);
1429 -- Determine the mode of the formal object
1431 if Out_Present (N) then
1432 K := E_Generic_In_Out_Parameter;
1434 if not In_Present (N) then
1435 Error_Msg_N ("formal generic objects cannot have mode OUT", N);
1439 K := E_Generic_In_Parameter;
1442 Find_Type (Subtype_Mark (N));
1443 T := Entity (Subtype_Mark (N));
1445 if Ekind (T) = E_Incomplete_Type then
1446 Error_Msg_N ("premature usage of incomplete type", Subtype_Mark (N));
1449 if K = E_Generic_In_Parameter then
1450 if Is_Limited_Type (T) then
1452 ("generic formal of mode IN must not be of limited type", N);
1453 Explain_Limited_Type (T, N);
1456 if Is_Abstract (T) then
1458 ("generic formal of mode IN must not be of abstract type", N);
1462 Analyze_Per_Use_Expression (E, T);
1468 -- Case of generic IN OUT parameter.
1471 -- If the formal has an unconstrained type, construct its
1472 -- actual subtype, as is done for subprogram formals. In this
1473 -- fashion, all its uses can refer to specific bounds.
1478 if (Is_Array_Type (T)
1479 and then not Is_Constrained (T))
1481 (Ekind (T) = E_Record_Type
1482 and then Has_Discriminants (T))
1485 Non_Freezing_Ref : constant Node_Id :=
1486 New_Reference_To (Id, Sloc (Id));
1490 -- Make sure that the actual subtype doesn't generate
1493 Set_Must_Not_Freeze (Non_Freezing_Ref);
1494 Decl := Build_Actual_Subtype (T, Non_Freezing_Ref);
1495 Insert_Before_And_Analyze (N, Decl);
1496 Set_Actual_Subtype (Id, Defining_Identifier (Decl));
1499 Set_Actual_Subtype (Id, T);
1504 ("initialization not allowed for `IN OUT` formals", N);
1508 end Analyze_Formal_Object_Declaration;
1510 ----------------------------------------------
1511 -- Analyze_Formal_Ordinary_Fixed_Point_Type --
1512 ----------------------------------------------
1514 procedure Analyze_Formal_Ordinary_Fixed_Point_Type
1518 Loc : constant Source_Ptr := Sloc (Def);
1519 Base : constant Entity_Id :=
1521 (E_Ordinary_Fixed_Point_Type, Current_Scope, Sloc (Def), 'G');
1523 -- The semantic attributes are set for completeness only, their
1524 -- values will never be used, because all properties of the type
1528 Set_Ekind (T, E_Ordinary_Fixed_Point_Subtype);
1529 Set_Etype (T, Base);
1530 Set_Size_Info (T, Standard_Integer);
1531 Set_RM_Size (T, RM_Size (Standard_Integer));
1532 Set_Small_Value (T, Ureal_1);
1533 Set_Delta_Value (T, Ureal_1);
1534 Set_Scalar_Range (T,
1536 Low_Bound => Make_Real_Literal (Loc, Ureal_1),
1537 High_Bound => Make_Real_Literal (Loc, Ureal_1)));
1539 Set_Is_Generic_Type (Base);
1540 Set_Etype (Base, Base);
1541 Set_Size_Info (Base, Standard_Integer);
1542 Set_RM_Size (Base, RM_Size (Standard_Integer));
1543 Set_Small_Value (Base, Ureal_1);
1544 Set_Delta_Value (Base, Ureal_1);
1545 Set_Scalar_Range (Base, Scalar_Range (T));
1546 Set_Parent (Base, Parent (Def));
1548 Check_Restriction (No_Fixed_Point, Def);
1549 end Analyze_Formal_Ordinary_Fixed_Point_Type;
1551 ----------------------------
1552 -- Analyze_Formal_Package --
1553 ----------------------------
1555 procedure Analyze_Formal_Package (N : Node_Id) is
1556 Loc : constant Source_Ptr := Sloc (N);
1557 Formal : constant Entity_Id := Defining_Identifier (N);
1558 Gen_Id : constant Node_Id := Name (N);
1560 Gen_Unit : Entity_Id;
1562 Parent_Installed : Boolean := False;
1564 Parent_Instance : Entity_Id;
1565 Renaming_In_Par : Entity_Id;
1568 Text_IO_Kludge (Gen_Id);
1571 Check_Generic_Child_Unit (Gen_Id, Parent_Installed);
1572 Gen_Unit := Entity (Gen_Id);
1574 if Ekind (Gen_Unit) /= E_Generic_Package then
1575 Error_Msg_N ("expect generic package name", Gen_Id);
1579 elsif Gen_Unit = Current_Scope then
1581 ("generic package cannot be used as a formal package of itself",
1587 -- Check for a formal package that is a package renaming.
1589 if Present (Renamed_Object (Gen_Unit)) then
1590 Gen_Unit := Renamed_Object (Gen_Unit);
1593 -- The formal package is treated like a regular instance, but only
1594 -- the specification needs to be instantiated, to make entities visible.
1596 if not Box_Present (N) then
1597 Hidden_Entities := New_Elmt_List;
1598 Analyze_Package_Instantiation (N);
1600 if Parent_Installed then
1605 -- If there are no generic associations, the generic parameters
1606 -- appear as local entities and are instantiated like them. We copy
1607 -- the generic package declaration as if it were an instantiation,
1608 -- and analyze it like a regular package, except that we treat the
1609 -- formals as additional visible components.
1611 Set_Instance_Env (Gen_Unit, Formal);
1613 Gen_Decl := Unit_Declaration_Node (Gen_Unit);
1615 if In_Extended_Main_Source_Unit (N) then
1616 Set_Is_Instantiated (Gen_Unit);
1617 Generate_Reference (Gen_Unit, N);
1622 (Original_Node (Gen_Decl), Empty, Instantiating => True);
1623 Set_Defining_Unit_Name (Specification (New_N), Formal);
1626 Enter_Name (Formal);
1627 Set_Ekind (Formal, E_Generic_Package);
1628 Set_Etype (Formal, Standard_Void_Type);
1629 Set_Inner_Instances (Formal, New_Elmt_List);
1632 -- Within the formal, the name of the generic package is a renaming
1633 -- of the formal (as for a regular instantiation).
1635 Renaming := Make_Package_Renaming_Declaration (Loc,
1636 Defining_Unit_Name =>
1637 Make_Defining_Identifier (Loc, Chars (Gen_Unit)),
1638 Name => New_Reference_To (Formal, Loc));
1640 if Present (Visible_Declarations (Specification (N))) then
1641 Prepend (Renaming, To => Visible_Declarations (Specification (N)));
1642 elsif Present (Private_Declarations (Specification (N))) then
1643 Prepend (Renaming, To => Private_Declarations (Specification (N)));
1646 if Is_Child_Unit (Gen_Unit)
1647 and then Parent_Installed
1649 -- Similarly, we have to make the name of the formal visible in
1650 -- the parent instance, to resolve properly fully qualified names
1651 -- that may appear in the generic unit. The parent instance has
1652 -- been placed on the scope stack ahead of the current scope.
1654 Parent_Instance := Scope_Stack.Table (Scope_Stack.Last - 1).Entity;
1657 Make_Defining_Identifier (Loc, Chars (Gen_Unit));
1658 Set_Ekind (Renaming_In_Par, E_Package);
1659 Set_Etype (Renaming_In_Par, Standard_Void_Type);
1660 Set_Scope (Renaming_In_Par, Parent_Instance);
1661 Set_Parent (Renaming_In_Par, Parent (Formal));
1662 Set_Renamed_Object (Renaming_In_Par, Formal);
1663 Append_Entity (Renaming_In_Par, Parent_Instance);
1666 Analyze_Generic_Formal_Part (N);
1667 Analyze (Specification (N));
1668 End_Package_Scope (Formal);
1670 if Parent_Installed then
1676 -- Inside the generic unit, the formal package is a regular
1677 -- package, but no body is needed for it. Note that after
1678 -- instantiation, the defining_unit_name we need is in the
1679 -- new tree and not in the original. (see Package_Instantiation).
1680 -- A generic formal package is an instance, and can be used as
1681 -- an actual for an inner instance. Mark its generic parent.
1683 Set_Ekind (Formal, E_Package);
1684 Set_Generic_Parent (Specification (N), Gen_Unit);
1685 Set_Has_Completion (Formal, True);
1687 end Analyze_Formal_Package;
1689 ---------------------------------
1690 -- Analyze_Formal_Private_Type --
1691 ---------------------------------
1693 procedure Analyze_Formal_Private_Type
1699 New_Private_Type (N, T, Def);
1701 -- Set the size to an arbitrary but legal value.
1703 Set_Size_Info (T, Standard_Integer);
1704 Set_RM_Size (T, RM_Size (Standard_Integer));
1705 end Analyze_Formal_Private_Type;
1707 ----------------------------------------
1708 -- Analyze_Formal_Signed_Integer_Type --
1709 ----------------------------------------
1711 procedure Analyze_Formal_Signed_Integer_Type
1715 Base : constant Entity_Id :=
1717 (E_Signed_Integer_Type, Current_Scope, Sloc (Def), 'G');
1722 Set_Ekind (T, E_Signed_Integer_Subtype);
1723 Set_Etype (T, Base);
1724 Set_Size_Info (T, Standard_Integer);
1725 Set_RM_Size (T, RM_Size (Standard_Integer));
1726 Set_Scalar_Range (T, Scalar_Range (Standard_Integer));
1728 Set_Is_Generic_Type (Base);
1729 Set_Size_Info (Base, Standard_Integer);
1730 Set_RM_Size (Base, RM_Size (Standard_Integer));
1731 Set_Etype (Base, Base);
1732 Set_Scalar_Range (Base, Scalar_Range (Standard_Integer));
1733 Set_Parent (Base, Parent (Def));
1734 end Analyze_Formal_Signed_Integer_Type;
1736 -------------------------------
1737 -- Analyze_Formal_Subprogram --
1738 -------------------------------
1740 procedure Analyze_Formal_Subprogram (N : Node_Id) is
1741 Spec : constant Node_Id := Specification (N);
1742 Def : constant Node_Id := Default_Name (N);
1743 Nam : constant Entity_Id := Defining_Unit_Name (Spec);
1751 if Nkind (Nam) = N_Defining_Program_Unit_Name then
1752 Error_Msg_N ("name of formal subprogram must be a direct name", Nam);
1756 Analyze_Subprogram_Declaration (N);
1757 Set_Is_Formal_Subprogram (Nam);
1758 Set_Has_Completion (Nam);
1760 -- Default name is resolved at the point of instantiation
1762 if Box_Present (N) then
1765 -- Else default is bound at the point of generic declaration
1767 elsif Present (Def) then
1768 if Nkind (Def) = N_Operator_Symbol then
1769 Find_Direct_Name (Def);
1771 elsif Nkind (Def) /= N_Attribute_Reference then
1775 -- For an attribute reference, analyze the prefix and verify
1776 -- that it has the proper profile for the subprogram.
1778 Analyze (Prefix (Def));
1779 Valid_Default_Attribute (Nam, Def);
1783 -- Default name may be overloaded, in which case the interpretation
1784 -- with the correct profile must be selected, as for a renaming.
1786 if Etype (Def) = Any_Type then
1789 elsif Nkind (Def) = N_Selected_Component then
1790 Subp := Entity (Selector_Name (Def));
1792 if Ekind (Subp) /= E_Entry then
1793 Error_Msg_N ("expect valid subprogram name as default", Def);
1797 elsif Nkind (Def) = N_Indexed_Component then
1799 if Nkind (Prefix (Def)) /= N_Selected_Component then
1800 Error_Msg_N ("expect valid subprogram name as default", Def);
1804 Subp := Entity (Selector_Name (Prefix (Def)));
1806 if Ekind (Subp) /= E_Entry_Family then
1807 Error_Msg_N ("expect valid subprogram name as default", Def);
1812 elsif Nkind (Def) = N_Character_Literal then
1814 -- Needs some type checks: subprogram should be parameterless???
1816 Resolve (Def, (Etype (Nam)));
1818 elsif not Is_Entity_Name (Def)
1819 or else not Is_Overloadable (Entity (Def))
1821 Error_Msg_N ("expect valid subprogram name as default", Def);
1824 elsif not Is_Overloaded (Def) then
1825 Subp := Entity (Def);
1828 Error_Msg_N ("premature usage of formal subprogram", Def);
1830 elsif not Entity_Matches_Spec (Subp, Nam) then
1831 Error_Msg_N ("no visible entity matches specification", Def);
1837 I1 : Interp_Index := 0;
1843 Get_First_Interp (Def, I, It);
1844 while Present (It.Nam) loop
1846 if Entity_Matches_Spec (It.Nam, Nam) then
1847 if Subp /= Any_Id then
1848 It1 := Disambiguate (Def, I1, I, Etype (Subp));
1850 if It1 = No_Interp then
1851 Error_Msg_N ("ambiguous default subprogram", Def);
1864 Get_Next_Interp (I, It);
1868 if Subp /= Any_Id then
1869 Set_Entity (Def, Subp);
1872 Error_Msg_N ("premature usage of formal subprogram", Def);
1874 elsif Ekind (Subp) /= E_Operator then
1875 Check_Mode_Conformant (Subp, Nam);
1879 Error_Msg_N ("no visible subprogram matches specification", N);
1883 end Analyze_Formal_Subprogram;
1885 -------------------------------------
1886 -- Analyze_Formal_Type_Declaration --
1887 -------------------------------------
1889 procedure Analyze_Formal_Type_Declaration (N : Node_Id) is
1890 Def : constant Node_Id := Formal_Type_Definition (N);
1894 T := Defining_Identifier (N);
1896 if Present (Discriminant_Specifications (N))
1897 and then Nkind (Def) /= N_Formal_Private_Type_Definition
1900 ("discriminants not allowed for this formal type",
1901 Defining_Identifier (First (Discriminant_Specifications (N))));
1904 -- Enter the new name, and branch to specific routine.
1907 when N_Formal_Private_Type_Definition =>
1908 Analyze_Formal_Private_Type (N, T, Def);
1910 when N_Formal_Derived_Type_Definition =>
1911 Analyze_Formal_Derived_Type (N, T, Def);
1913 when N_Formal_Discrete_Type_Definition =>
1914 Analyze_Formal_Discrete_Type (T, Def);
1916 when N_Formal_Signed_Integer_Type_Definition =>
1917 Analyze_Formal_Signed_Integer_Type (T, Def);
1919 when N_Formal_Modular_Type_Definition =>
1920 Analyze_Formal_Modular_Type (T, Def);
1922 when N_Formal_Floating_Point_Definition =>
1923 Analyze_Formal_Floating_Type (T, Def);
1925 when N_Formal_Ordinary_Fixed_Point_Definition =>
1926 Analyze_Formal_Ordinary_Fixed_Point_Type (T, Def);
1928 when N_Formal_Decimal_Fixed_Point_Definition =>
1929 Analyze_Formal_Decimal_Fixed_Point_Type (T, Def);
1931 when N_Array_Type_Definition =>
1932 Analyze_Formal_Array_Type (T, Def);
1934 when N_Access_To_Object_Definition |
1935 N_Access_Function_Definition |
1936 N_Access_Procedure_Definition =>
1937 Analyze_Generic_Access_Type (T, Def);
1943 raise Program_Error;
1947 Set_Is_Generic_Type (T);
1948 end Analyze_Formal_Type_Declaration;
1950 ------------------------------------
1951 -- Analyze_Function_Instantiation --
1952 ------------------------------------
1954 procedure Analyze_Function_Instantiation (N : Node_Id) is
1956 Analyze_Subprogram_Instantiation (N, E_Function);
1957 end Analyze_Function_Instantiation;
1959 ---------------------------------
1960 -- Analyze_Generic_Access_Type --
1961 ---------------------------------
1963 procedure Analyze_Generic_Access_Type (T : Entity_Id; Def : Node_Id) is
1967 if Nkind (Def) = N_Access_To_Object_Definition then
1968 Access_Type_Declaration (T, Def);
1970 if Is_Incomplete_Or_Private_Type (Designated_Type (T))
1971 and then No (Full_View (Designated_Type (T)))
1972 and then not Is_Generic_Type (Designated_Type (T))
1974 Error_Msg_N ("premature usage of incomplete type", Def);
1976 elsif Is_Internal (Designated_Type (T)) then
1978 ("only a subtype mark is allowed in a formal", Def);
1982 Access_Subprogram_Declaration (T, Def);
1984 end Analyze_Generic_Access_Type;
1986 ---------------------------------
1987 -- Analyze_Generic_Formal_Part --
1988 ---------------------------------
1990 procedure Analyze_Generic_Formal_Part (N : Node_Id) is
1991 Gen_Parm_Decl : Node_Id;
1994 -- The generic formals are processed in the scope of the generic
1995 -- unit, where they are immediately visible. The scope is installed
1998 Gen_Parm_Decl := First (Generic_Formal_Declarations (N));
2000 while Present (Gen_Parm_Decl) loop
2001 Analyze (Gen_Parm_Decl);
2002 Next (Gen_Parm_Decl);
2005 Generate_Reference_To_Generic_Formals (Current_Scope);
2006 end Analyze_Generic_Formal_Part;
2008 ------------------------------------------
2009 -- Analyze_Generic_Package_Declaration --
2010 ------------------------------------------
2012 procedure Analyze_Generic_Package_Declaration (N : Node_Id) is
2013 Loc : constant Source_Ptr := Sloc (N);
2016 Save_Parent : Node_Id;
2018 Decls : constant List_Id :=
2019 Visible_Declarations (Specification (N));
2023 -- We introduce a renaming of the enclosing package, to have a usable
2024 -- entity as the prefix of an expanded name for a local entity of the
2025 -- form Par.P.Q, where P is the generic package. This is because a local
2026 -- entity named P may hide it, so that the usual visibility rules in
2027 -- the instance will not resolve properly.
2030 Make_Package_Renaming_Declaration (Loc,
2031 Defining_Unit_Name =>
2032 Make_Defining_Identifier (Loc,
2033 Chars => New_External_Name (Chars (Defining_Entity (N)), "GH")),
2034 Name => Make_Identifier (Loc, Chars (Defining_Entity (N))));
2036 if Present (Decls) then
2037 Decl := First (Decls);
2038 while Present (Decl)
2039 and then Nkind (Decl) = N_Pragma
2044 if Present (Decl) then
2045 Insert_Before (Decl, Renaming);
2047 Append (Renaming, Visible_Declarations (Specification (N)));
2051 Set_Visible_Declarations (Specification (N), New_List (Renaming));
2054 -- Create copy of generic unit, and save for instantiation.
2055 -- If the unit is a child unit, do not copy the specifications
2056 -- for the parent, which are not part of the generic tree.
2058 Save_Parent := Parent_Spec (N);
2059 Set_Parent_Spec (N, Empty);
2061 New_N := Copy_Generic_Node (N, Empty, Instantiating => False);
2062 Set_Parent_Spec (New_N, Save_Parent);
2064 Id := Defining_Entity (N);
2065 Generate_Definition (Id);
2067 -- Expansion is not applied to generic units.
2072 Set_Ekind (Id, E_Generic_Package);
2073 Set_Etype (Id, Standard_Void_Type);
2075 Enter_Generic_Scope (Id);
2076 Set_Inner_Instances (Id, New_Elmt_List);
2078 Set_Categorization_From_Pragmas (N);
2079 Set_Is_Pure (Id, Is_Pure (Current_Scope));
2081 -- Link the declaration of the generic homonym in the generic copy
2082 -- to the package it renames, so that it is always resolved properly.
2084 Set_Generic_Homonym (Id, Defining_Unit_Name (Renaming));
2085 Set_Entity (Associated_Node (Name (Renaming)), Id);
2087 -- For a library unit, we have reconstructed the entity for the
2088 -- unit, and must reset it in the library tables.
2090 if Nkind (Parent (N)) = N_Compilation_Unit then
2091 Set_Cunit_Entity (Current_Sem_Unit, Id);
2094 Analyze_Generic_Formal_Part (N);
2096 -- After processing the generic formals, analysis proceeds
2097 -- as for a non-generic package.
2099 Analyze (Specification (N));
2101 Validate_Categorization_Dependency (N, Id);
2105 End_Package_Scope (Id);
2106 Exit_Generic_Scope (Id);
2108 if Nkind (Parent (N)) /= N_Compilation_Unit then
2109 Move_Freeze_Nodes (Id, N, Visible_Declarations (Specification (N)));
2110 Move_Freeze_Nodes (Id, N, Private_Declarations (Specification (N)));
2111 Move_Freeze_Nodes (Id, N, Generic_Formal_Declarations (N));
2114 Set_Body_Required (Parent (N), Unit_Requires_Body (Id));
2115 Validate_RT_RAT_Component (N);
2117 -- If this is a spec without a body, check that generic parameters
2120 if not Body_Required (Parent (N)) then
2121 Check_References (Id);
2124 end Analyze_Generic_Package_Declaration;
2126 --------------------------------------------
2127 -- Analyze_Generic_Subprogram_Declaration --
2128 --------------------------------------------
2130 procedure Analyze_Generic_Subprogram_Declaration (N : Node_Id) is
2135 Save_Parent : Node_Id;
2138 -- Create copy of generic unit,and save for instantiation.
2139 -- If the unit is a child unit, do not copy the specifications
2140 -- for the parent, which are not part of the generic tree.
2142 Save_Parent := Parent_Spec (N);
2143 Set_Parent_Spec (N, Empty);
2145 New_N := Copy_Generic_Node (N, Empty, Instantiating => False);
2146 Set_Parent_Spec (New_N, Save_Parent);
2149 Spec := Specification (N);
2150 Id := Defining_Entity (Spec);
2151 Generate_Definition (Id);
2153 if Nkind (Id) = N_Defining_Operator_Symbol then
2155 ("operator symbol not allowed for generic subprogram", Id);
2162 Set_Scope_Depth_Value (Id, Scope_Depth (Current_Scope) + 1);
2164 Enter_Generic_Scope (Id);
2165 Set_Inner_Instances (Id, New_Elmt_List);
2166 Set_Is_Pure (Id, Is_Pure (Current_Scope));
2168 Analyze_Generic_Formal_Part (N);
2170 Formals := Parameter_Specifications (Spec);
2172 if Present (Formals) then
2173 Process_Formals (Formals, Spec);
2176 if Nkind (Spec) = N_Function_Specification then
2177 Set_Ekind (Id, E_Generic_Function);
2178 Find_Type (Subtype_Mark (Spec));
2179 Set_Etype (Id, Entity (Subtype_Mark (Spec)));
2181 Set_Ekind (Id, E_Generic_Procedure);
2182 Set_Etype (Id, Standard_Void_Type);
2185 -- For a library unit, we have reconstructed the entity for the
2186 -- unit, and must reset it in the library tables. We also need
2187 -- to make sure that Body_Required is set properly in the original
2188 -- compilation unit node.
2190 if Nkind (Parent (N)) = N_Compilation_Unit then
2191 Set_Cunit_Entity (Current_Sem_Unit, Id);
2192 Set_Body_Required (Parent (N), Unit_Requires_Body (Id));
2195 Set_Categorization_From_Pragmas (N);
2196 Validate_Categorization_Dependency (N, Id);
2198 Save_Global_References (Original_Node (N));
2202 Exit_Generic_Scope (Id);
2203 Generate_Reference_To_Formals (Id);
2204 end Analyze_Generic_Subprogram_Declaration;
2206 -----------------------------------
2207 -- Analyze_Package_Instantiation --
2208 -----------------------------------
2210 -- Note: this procedure is also used for formal package declarations,
2211 -- in which case the argument N is an N_Formal_Package_Declaration
2212 -- node. This should really be noted in the spec! ???
2214 procedure Analyze_Package_Instantiation (N : Node_Id) is
2215 Loc : constant Source_Ptr := Sloc (N);
2216 Gen_Id : constant Node_Id := Name (N);
2219 Act_Decl_Name : Node_Id;
2220 Act_Decl_Id : Entity_Id;
2225 Gen_Unit : Entity_Id;
2227 Is_Actual_Pack : constant Boolean :=
2228 Is_Internal (Defining_Entity (N));
2230 Parent_Installed : Boolean := False;
2231 Renaming_List : List_Id;
2232 Unit_Renaming : Node_Id;
2233 Needs_Body : Boolean;
2234 Inline_Now : Boolean := False;
2236 procedure Delay_Descriptors (E : Entity_Id);
2237 -- Delay generation of subprogram descriptors for given entity
2239 function Might_Inline_Subp return Boolean;
2240 -- If inlining is active and the generic contains inlined subprograms,
2241 -- we instantiate the body. This may cause superfluous instantiations,
2242 -- but it is simpler than detecting the need for the body at the point
2243 -- of inlining, when the context of the instance is not available.
2245 -----------------------
2246 -- Delay_Descriptors --
2247 -----------------------
2249 procedure Delay_Descriptors (E : Entity_Id) is
2251 if not Delay_Subprogram_Descriptors (E) then
2252 Set_Delay_Subprogram_Descriptors (E);
2253 Pending_Descriptor.Increment_Last;
2254 Pending_Descriptor.Table (Pending_Descriptor.Last) := E;
2256 end Delay_Descriptors;
2258 -----------------------
2259 -- Might_Inline_Subp --
2260 -----------------------
2262 function Might_Inline_Subp return Boolean is
2266 if not Inline_Processing_Required then
2270 E := First_Entity (Gen_Unit);
2272 while Present (E) loop
2274 if Is_Subprogram (E)
2275 and then Is_Inlined (E)
2285 end Might_Inline_Subp;
2287 -- Start of processing for Analyze_Package_Instantiation
2290 -- Very first thing: apply the special kludge for Text_IO processing
2291 -- in case we are instantiating one of the children of [Wide_]Text_IO.
2293 Text_IO_Kludge (Name (N));
2295 -- Make node global for error reporting.
2297 Instantiation_Node := N;
2299 -- Case of instantiation of a generic package
2301 if Nkind (N) = N_Package_Instantiation then
2302 Act_Decl_Id := New_Copy (Defining_Entity (N));
2303 Set_Comes_From_Source (Act_Decl_Id, True);
2305 if Nkind (Defining_Unit_Name (N)) = N_Defining_Program_Unit_Name then
2307 Make_Defining_Program_Unit_Name (Loc,
2308 Name => New_Copy_Tree (Name (Defining_Unit_Name (N))),
2309 Defining_Identifier => Act_Decl_Id);
2311 Act_Decl_Name := Act_Decl_Id;
2314 -- Case of instantiation of a formal package
2317 Act_Decl_Id := Defining_Identifier (N);
2318 Act_Decl_Name := Act_Decl_Id;
2321 Generate_Definition (Act_Decl_Id);
2322 Pre_Analyze_Actuals (N);
2325 Check_Generic_Child_Unit (Gen_Id, Parent_Installed);
2326 Gen_Unit := Entity (Gen_Id);
2328 -- Verify that it is the name of a generic package
2330 if Etype (Gen_Unit) = Any_Type then
2334 elsif Ekind (Gen_Unit) /= E_Generic_Package then
2336 if From_With_Type (Gen_Unit) then
2338 ("cannot instantiate a limited withed package", Gen_Id);
2341 ("expect name of generic package in instantiation", Gen_Id);
2348 if In_Extended_Main_Source_Unit (N) then
2349 Set_Is_Instantiated (Gen_Unit);
2350 Generate_Reference (Gen_Unit, N);
2352 if Present (Renamed_Object (Gen_Unit)) then
2353 Set_Is_Instantiated (Renamed_Object (Gen_Unit));
2354 Generate_Reference (Renamed_Object (Gen_Unit), N);
2358 if Nkind (Gen_Id) = N_Identifier
2359 and then Chars (Gen_Unit) = Chars (Defining_Entity (N))
2362 ("& is hidden within declaration of instance", Gen_Id, Gen_Unit);
2364 elsif Nkind (Gen_Id) = N_Expanded_Name
2365 and then Is_Child_Unit (Gen_Unit)
2366 and then Nkind (Prefix (Gen_Id)) = N_Identifier
2367 and then Chars (Act_Decl_Id) = Chars (Prefix (Gen_Id))
2370 ("& is hidden within declaration of instance ", Prefix (Gen_Id));
2373 Set_Entity (Gen_Id, Gen_Unit);
2375 -- If generic is a renaming, get original generic unit.
2377 if Present (Renamed_Object (Gen_Unit))
2378 and then Ekind (Renamed_Object (Gen_Unit)) = E_Generic_Package
2380 Gen_Unit := Renamed_Object (Gen_Unit);
2383 -- Verify that there are no circular instantiations.
2385 if In_Open_Scopes (Gen_Unit) then
2386 Error_Msg_NE ("instantiation of & within itself", N, Gen_Unit);
2390 elsif Contains_Instance_Of (Gen_Unit, Current_Scope, Gen_Id) then
2391 Error_Msg_Node_2 := Current_Scope;
2393 ("circular Instantiation: & instantiated in &!", N, Gen_Unit);
2394 Circularity_Detected := True;
2399 Set_Instance_Env (Gen_Unit, Act_Decl_Id);
2400 Gen_Decl := Unit_Declaration_Node (Gen_Unit);
2402 -- Initialize renamings map, for error checking, and the list
2403 -- that holds private entities whose views have changed between
2404 -- generic definition and instantiation. If this is the instance
2405 -- created to validate an actual package, the instantiation
2406 -- environment is that of the enclosing instance.
2408 Generic_Renamings.Set_Last (0);
2409 Generic_Renamings_HTable.Reset;
2411 Create_Instantiation_Source (N, Gen_Unit, False, S_Adjustment);
2413 -- Copy original generic tree, to produce text for instantiation.
2417 (Original_Node (Gen_Decl), Empty, Instantiating => True);
2419 Act_Spec := Specification (Act_Tree);
2421 -- If this is the instance created to validate an actual package,
2422 -- only the formals matter, do not examine the package spec itself.
2424 if Is_Actual_Pack then
2425 Set_Visible_Declarations (Act_Spec, New_List);
2426 Set_Private_Declarations (Act_Spec, New_List);
2430 Analyze_Associations
2432 Generic_Formal_Declarations (Act_Tree),
2433 Generic_Formal_Declarations (Gen_Decl));
2435 Set_Defining_Unit_Name (Act_Spec, Act_Decl_Name);
2436 Set_Is_Generic_Instance (Act_Decl_Id);
2438 Set_Generic_Parent (Act_Spec, Gen_Unit);
2440 -- References to the generic in its own declaration or its body
2441 -- are references to the instance. Add a renaming declaration for
2442 -- the generic unit itself. This declaration, as well as the renaming
2443 -- declarations for the generic formals, must remain private to the
2444 -- unit: the formals, because this is the language semantics, and
2445 -- the unit because its use is an artifact of the implementation.
2448 Make_Package_Renaming_Declaration (Loc,
2449 Defining_Unit_Name =>
2450 Make_Defining_Identifier (Loc, Chars (Gen_Unit)),
2451 Name => New_Reference_To (Act_Decl_Id, Loc));
2453 Append (Unit_Renaming, Renaming_List);
2455 -- The renaming declarations are the first local declarations of
2458 if Is_Non_Empty_List (Visible_Declarations (Act_Spec)) then
2460 (First (Visible_Declarations (Act_Spec)), Renaming_List);
2462 Set_Visible_Declarations (Act_Spec, Renaming_List);
2466 Make_Package_Declaration (Loc,
2467 Specification => Act_Spec);
2469 -- Save the instantiation node, for subsequent instantiation
2470 -- of the body, if there is one and we are generating code for
2471 -- the current unit. Mark the unit as having a body, to avoid
2472 -- a premature error message.
2474 -- We instantiate the body if we are generating code, if we are
2475 -- generating cross-reference information, or if we are building
2476 -- trees for ASIS use.
2479 Enclosing_Body_Present : Boolean := False;
2480 -- If the generic unit is not a compilation unit, then a body
2481 -- may be present in its parent even if none is required. We
2482 -- create a tentative pending instantiation for the body, which
2483 -- will be discarded if none is actually present.
2488 if Scope (Gen_Unit) /= Standard_Standard
2489 and then not Is_Child_Unit (Gen_Unit)
2491 Scop := Scope (Gen_Unit);
2493 while Present (Scop)
2494 and then Scop /= Standard_Standard
2496 if Unit_Requires_Body (Scop) then
2497 Enclosing_Body_Present := True;
2501 exit when Is_Compilation_Unit (Scop);
2502 Scop := Scope (Scop);
2506 -- If front-end inlining is enabled, and this is a unit for which
2507 -- code will be generated, we instantiate the body at once.
2508 -- This is done if the instance is not the main unit, and if the
2509 -- generic is not a child unit of another generic, to avoid scope
2510 -- problems and the reinstallation of parent instances.
2512 if Front_End_Inlining
2513 and then Expander_Active
2514 and then (not Is_Child_Unit (Gen_Unit)
2515 or else not Is_Generic_Unit (Scope (Gen_Unit)))
2516 and then Is_In_Main_Unit (N)
2517 and then Nkind (Parent (N)) /= N_Compilation_Unit
2518 and then Might_Inline_Subp
2519 and then not Is_Actual_Pack
2525 (Unit_Requires_Body (Gen_Unit)
2526 or else Enclosing_Body_Present
2527 or else Present (Corresponding_Body (Gen_Decl)))
2528 and then (Is_In_Main_Unit (N)
2529 or else Might_Inline_Subp)
2530 and then not Is_Actual_Pack
2531 and then not Inline_Now
2533 and then (Operating_Mode = Generate_Code
2534 or else (Operating_Mode = Check_Semantics
2535 and then ASIS_Mode));
2537 -- If front_end_inlining is enabled, do not instantiate a
2538 -- body if within a generic context.
2540 if Front_End_Inlining
2541 and then not Expander_Active
2543 Needs_Body := False;
2546 -- If the current context is generic, and the package being
2547 -- instantiated is declared within a formal package, there
2548 -- is no body to instantiate until the enclosing generic is
2549 -- instantiated, and there is an actual for the formal
2550 -- package. If the formal package has parameters, we build a
2551 -- regular package instance for it, that preceeds the original
2552 -- formal package declaration.
2554 if In_Open_Scopes (Scope (Scope (Gen_Unit))) then
2558 (Unit_Declaration_Node (Scope (Gen_Unit)));
2560 if Nkind (Decl) = N_Formal_Package_Declaration
2561 or else (Nkind (Decl) = N_Package_Declaration
2562 and then Is_List_Member (Decl)
2563 and then Present (Next (Decl))
2565 Nkind (Next (Decl)) = N_Formal_Package_Declaration)
2567 Needs_Body := False;
2573 -- If we are generating the calling stubs from the instantiation
2574 -- of a generic RCI package, we will not use the body of the
2577 if Distribution_Stub_Mode = Generate_Caller_Stub_Body
2578 and then Is_Compilation_Unit (Defining_Entity (N))
2580 Needs_Body := False;
2585 -- Here is a defence against a ludicrous number of instantiations
2586 -- caused by a circular set of instantiation attempts.
2588 if Pending_Instantiations.Last >
2589 Hostparm.Max_Instantiations
2591 Error_Msg_N ("too many instantiations", N);
2592 raise Unrecoverable_Error;
2595 -- Indicate that the enclosing scopes contain an instantiation,
2596 -- and that cleanup actions should be delayed until after the
2597 -- instance body is expanded.
2599 Check_Forward_Instantiation (Gen_Decl);
2600 if Nkind (N) = N_Package_Instantiation then
2602 Enclosing_Master : Entity_Id := Current_Scope;
2605 while Enclosing_Master /= Standard_Standard loop
2607 if Ekind (Enclosing_Master) = E_Package then
2608 if Is_Compilation_Unit (Enclosing_Master) then
2609 if In_Package_Body (Enclosing_Master) then
2611 (Body_Entity (Enclosing_Master));
2620 Enclosing_Master := Scope (Enclosing_Master);
2623 elsif Ekind (Enclosing_Master) = E_Generic_Package then
2624 Enclosing_Master := Scope (Enclosing_Master);
2626 elsif Is_Generic_Subprogram (Enclosing_Master)
2627 or else Ekind (Enclosing_Master) = E_Void
2629 -- Cleanup actions will eventually be performed on
2630 -- the enclosing instance, if any. enclosing scope
2631 -- is void in the formal part of a generic subp.
2636 if Ekind (Enclosing_Master) = E_Entry
2638 Ekind (Scope (Enclosing_Master)) = E_Protected_Type
2641 Protected_Body_Subprogram (Enclosing_Master);
2644 Set_Delay_Cleanups (Enclosing_Master);
2646 while Ekind (Enclosing_Master) = E_Block loop
2647 Enclosing_Master := Scope (Enclosing_Master);
2650 if Is_Subprogram (Enclosing_Master) then
2651 Delay_Descriptors (Enclosing_Master);
2653 elsif Is_Task_Type (Enclosing_Master) then
2655 TBP : constant Node_Id :=
2656 Get_Task_Body_Procedure
2660 if Present (TBP) then
2661 Delay_Descriptors (TBP);
2662 Set_Delay_Cleanups (TBP);
2672 -- Make entry in table
2674 Pending_Instantiations.Increment_Last;
2675 Pending_Instantiations.Table (Pending_Instantiations.Last) :=
2676 (N, Act_Decl, Expander_Active, Current_Sem_Unit);
2680 Set_Categorization_From_Pragmas (Act_Decl);
2682 if Parent_Installed then
2686 Set_Instance_Spec (N, Act_Decl);
2688 -- If not a compilation unit, insert the package declaration
2689 -- before the original instantiation node.
2691 if Nkind (Parent (N)) /= N_Compilation_Unit then
2692 Mark_Rewrite_Insertion (Act_Decl);
2693 Insert_Before (N, Act_Decl);
2696 -- For an instantiation that is a compilation unit, place
2697 -- declaration on current node so context is complete
2698 -- for analysis (including nested instantiations). It this
2699 -- is the main unit, the declaration eventually replaces the
2700 -- instantiation node. If the instance body is later created, it
2701 -- replaces the instance node, and the declation is attached to
2702 -- it (see Build_Instance_Compilation_Unit_Nodes).
2705 if Cunit_Entity (Current_Sem_Unit) = Defining_Entity (N) then
2707 -- The entity for the current unit is the newly created one,
2708 -- and all semantic information is attached to it.
2710 Set_Cunit_Entity (Current_Sem_Unit, Act_Decl_Id);
2712 -- If this is the main unit, replace the main entity as well.
2714 if Current_Sem_Unit = Main_Unit then
2715 Main_Unit_Entity := Act_Decl_Id;
2719 Set_Unit (Parent (N), Act_Decl);
2720 Set_Parent_Spec (Act_Decl, Parent_Spec (N));
2722 Set_Unit (Parent (N), N);
2723 Set_Body_Required (Parent (N), False);
2725 -- We never need elaboration checks on instantiations, since
2726 -- by definition, the body instantiation is elaborated at the
2727 -- same time as the spec instantiation.
2729 Set_Suppress_Elaboration_Warnings (Act_Decl_Id);
2730 Set_Kill_Elaboration_Checks (Act_Decl_Id);
2733 Check_Elab_Instantiation (N);
2735 if ABE_Is_Certain (N) and then Needs_Body then
2736 Pending_Instantiations.Decrement_Last;
2738 Check_Hidden_Child_Unit (N, Gen_Unit, Act_Decl_Id);
2740 Set_First_Private_Entity (Defining_Unit_Name (Unit_Renaming),
2741 First_Private_Entity (Act_Decl_Id));
2743 -- If the instantiation will receive a body, the unit will
2744 -- be transformed into a package body, and receive its own
2745 -- elaboration entity. Otherwise, the nature of the unit is
2746 -- now a package declaration.
2748 if Nkind (Parent (N)) = N_Compilation_Unit
2749 and then not Needs_Body
2751 Rewrite (N, Act_Decl);
2754 if Present (Corresponding_Body (Gen_Decl))
2755 or else Unit_Requires_Body (Gen_Unit)
2757 Set_Has_Completion (Act_Decl_Id);
2760 Check_Formal_Packages (Act_Decl_Id);
2762 Restore_Private_Views (Act_Decl_Id);
2764 if not Generic_Separately_Compiled (Gen_Unit) then
2765 Inherit_Context (Gen_Decl, N);
2768 if Parent_Installed then
2775 Validate_Categorization_Dependency (N, Act_Decl_Id);
2777 -- Check restriction, but skip this if something went wrong in
2778 -- the above analysis, indicated by Act_Decl_Id being void.
2780 if Ekind (Act_Decl_Id) /= E_Void
2781 and then not Is_Library_Level_Entity (Act_Decl_Id)
2783 Check_Restriction (No_Local_Allocators, N);
2787 Inline_Instance_Body (N, Gen_Unit, Act_Decl);
2791 when Instantiation_Error =>
2792 if Parent_Installed then
2795 end Analyze_Package_Instantiation;
2797 ---------------------------
2798 -- Inline_Instance_Body --
2799 ---------------------------
2801 procedure Inline_Instance_Body
2803 Gen_Unit : Entity_Id;
2807 Gen_Comp : constant Entity_Id :=
2808 Cunit_Entity (Get_Source_Unit (Gen_Unit));
2809 Curr_Comp : constant Node_Id := Cunit (Current_Sem_Unit);
2810 Curr_Scope : Entity_Id := Empty;
2811 Curr_Unit : constant Entity_Id :=
2812 Cunit_Entity (Current_Sem_Unit);
2813 Removed : Boolean := False;
2814 Num_Scopes : Int := 0;
2815 Use_Clauses : array (1 .. Scope_Stack.Last) of Node_Id;
2816 Instances : array (1 .. Scope_Stack.Last) of Entity_Id;
2817 Inner_Scopes : array (1 .. Scope_Stack.Last) of Entity_Id;
2818 Num_Inner : Int := 0;
2819 N_Instances : Int := 0;
2823 -- Case of generic unit defined in another unit. We must remove
2824 -- the complete context of the current unit to install that of
2827 if Gen_Comp /= Cunit_Entity (Current_Sem_Unit) then
2831 and then S /= Standard_Standard
2833 Num_Scopes := Num_Scopes + 1;
2835 Use_Clauses (Num_Scopes) :=
2837 (Scope_Stack.Last - Num_Scopes + 1).
2839 End_Use_Clauses (Use_Clauses (Num_Scopes));
2841 exit when Is_Generic_Instance (S)
2842 and then (In_Package_Body (S)
2843 or else Ekind (S) = E_Procedure
2844 or else Ekind (S) = E_Function);
2848 Vis := Is_Immediately_Visible (Gen_Comp);
2850 -- Find and save all enclosing instances
2855 and then S /= Standard_Standard
2857 if Is_Generic_Instance (S) then
2858 N_Instances := N_Instances + 1;
2859 Instances (N_Instances) := S;
2861 exit when In_Package_Body (S);
2867 -- Remove context of current compilation unit, unless we
2868 -- are within a nested package instantiation, in which case
2869 -- the context has been removed previously.
2871 -- If current scope is the body of a child unit, remove context
2877 and then S /= Standard_Standard
2879 exit when Is_Generic_Instance (S)
2880 and then (In_Package_Body (S)
2881 or else Ekind (S) = E_Procedure
2882 or else Ekind (S) = E_Function);
2885 or else (Ekind (Curr_Unit) = E_Package_Body
2886 and then S = Spec_Entity (Curr_Unit))
2887 or else (Ekind (Curr_Unit) = E_Subprogram_Body
2890 (Unit_Declaration_Node (Curr_Unit)))
2894 -- Remove entities in current scopes from visibility, so
2895 -- than instance body is compiled in a clean environment.
2897 Save_Scope_Stack (Handle_Use => False);
2899 if Is_Child_Unit (S) then
2901 -- Remove child unit from stack, as well as inner scopes.
2902 -- Removing the context of a child unit removes parent
2905 while Current_Scope /= S loop
2906 Num_Inner := Num_Inner + 1;
2907 Inner_Scopes (Num_Inner) := Current_Scope;
2912 Remove_Context (Curr_Comp);
2916 Remove_Context (Curr_Comp);
2919 if Ekind (Curr_Unit) = E_Package_Body then
2920 Remove_Context (Library_Unit (Curr_Comp));
2927 New_Scope (Standard_Standard);
2928 Scope_Stack.Table (Scope_Stack.Last).Is_Active_Stack_Base := True;
2929 Instantiate_Package_Body
2930 ((N, Act_Decl, Expander_Active, Current_Sem_Unit), True);
2935 Set_Is_Immediately_Visible (Gen_Comp, Vis);
2937 -- Reset Generic_Instance flag so that use clauses can be installed
2938 -- in the proper order. (See Use_One_Package for effect of enclosing
2939 -- instances on processing of use clauses).
2941 for J in 1 .. N_Instances loop
2942 Set_Is_Generic_Instance (Instances (J), False);
2946 Install_Context (Curr_Comp);
2948 if Present (Curr_Scope)
2949 and then Is_Child_Unit (Curr_Scope)
2951 New_Scope (Curr_Scope);
2952 Set_Is_Immediately_Visible (Curr_Scope);
2954 -- Finally, restore inner scopes as well.
2956 for J in reverse 1 .. Num_Inner loop
2957 New_Scope (Inner_Scopes (J));
2961 Restore_Scope_Stack (Handle_Use => False);
2964 -- Restore use clauses. For a child unit, use clauses in the
2965 -- parents are restored when installing the context, so only
2966 -- those in inner scopes (and those local to the child unit itself)
2967 -- need to be installed explicitly.
2969 if Is_Child_Unit (Curr_Unit)
2972 for J in reverse 1 .. Num_Inner + 1 loop
2973 Scope_Stack.Table (Scope_Stack.Last - J + 1).First_Use_Clause :=
2975 Install_Use_Clauses (Use_Clauses (J));
2979 for J in reverse 1 .. Num_Scopes loop
2980 Scope_Stack.Table (Scope_Stack.Last - J + 1).First_Use_Clause :=
2982 Install_Use_Clauses (Use_Clauses (J));
2986 for J in 1 .. N_Instances loop
2987 Set_Is_Generic_Instance (Instances (J), True);
2990 -- If generic unit is in current unit, current context is correct.
2993 Instantiate_Package_Body
2994 ((N, Act_Decl, Expander_Active, Current_Sem_Unit), True);
2996 end Inline_Instance_Body;
2998 -------------------------------------
2999 -- Analyze_Procedure_Instantiation --
3000 -------------------------------------
3002 procedure Analyze_Procedure_Instantiation (N : Node_Id) is
3004 Analyze_Subprogram_Instantiation (N, E_Procedure);
3005 end Analyze_Procedure_Instantiation;
3007 --------------------------------------
3008 -- Analyze_Subprogram_Instantiation --
3009 --------------------------------------
3011 procedure Analyze_Subprogram_Instantiation
3015 Loc : constant Source_Ptr := Sloc (N);
3016 Gen_Id : constant Node_Id := Name (N);
3018 Anon_Id : constant Entity_Id :=
3019 Make_Defining_Identifier (Sloc (Defining_Entity (N)),
3020 Chars => New_External_Name
3021 (Chars (Defining_Entity (N)), 'R'));
3023 Act_Decl_Id : Entity_Id;
3028 Gen_Unit : Entity_Id;
3030 Pack_Id : Entity_Id;
3031 Parent_Installed : Boolean := False;
3032 Renaming_List : List_Id;
3034 procedure Analyze_Instance_And_Renamings;
3035 -- The instance must be analyzed in a context that includes the
3036 -- mappings of generic parameters into actuals. We create a package
3037 -- declaration for this purpose, and a subprogram with an internal
3038 -- name within the package. The subprogram instance is simply an
3039 -- alias for the internal subprogram, declared in the current scope.
3041 ------------------------------------
3042 -- Analyze_Instance_And_Renamings --
3043 ------------------------------------
3045 procedure Analyze_Instance_And_Renamings is
3046 Def_Ent : constant Entity_Id := Defining_Entity (N);
3047 Pack_Decl : Node_Id;
3050 if Nkind (Parent (N)) = N_Compilation_Unit then
3052 -- For the case of a compilation unit, the container package
3053 -- has the same name as the instantiation, to insure that the
3054 -- binder calls the elaboration procedure with the right name.
3055 -- Copy the entity of the instance, which may have compilation
3056 -- level flags (e.g. Is_Child_Unit) set.
3058 Pack_Id := New_Copy (Def_Ent);
3061 -- Otherwise we use the name of the instantiation concatenated
3062 -- with its source position to ensure uniqueness if there are
3063 -- several instantiations with the same name.
3066 Make_Defining_Identifier (Loc,
3067 Chars => New_External_Name
3068 (Related_Id => Chars (Def_Ent),
3070 Suffix_Index => Source_Offset (Sloc (Def_Ent))));
3073 Pack_Decl := Make_Package_Declaration (Loc,
3074 Specification => Make_Package_Specification (Loc,
3075 Defining_Unit_Name => Pack_Id,
3076 Visible_Declarations => Renaming_List,
3077 End_Label => Empty));
3079 Set_Instance_Spec (N, Pack_Decl);
3080 Set_Is_Generic_Instance (Pack_Id);
3081 Set_Needs_Debug_Info (Pack_Id);
3083 -- Case of not a compilation unit
3085 if Nkind (Parent (N)) /= N_Compilation_Unit then
3086 Mark_Rewrite_Insertion (Pack_Decl);
3087 Insert_Before (N, Pack_Decl);
3088 Set_Has_Completion (Pack_Id);
3090 -- Case of an instantiation that is a compilation unit
3092 -- Place declaration on current node so context is complete
3093 -- for analysis (including nested instantiations), and for
3094 -- use in a context_clause (see Analyze_With_Clause).
3097 Set_Unit (Parent (N), Pack_Decl);
3098 Set_Parent_Spec (Pack_Decl, Parent_Spec (N));
3101 Analyze (Pack_Decl);
3102 Check_Formal_Packages (Pack_Id);
3103 Set_Is_Generic_Instance (Pack_Id, False);
3105 -- Body of the enclosing package is supplied when instantiating
3106 -- the subprogram body, after semantic analysis is completed.
3108 if Nkind (Parent (N)) = N_Compilation_Unit then
3110 -- Remove package itself from visibility, so it does not
3111 -- conflict with subprogram.
3113 Set_Name_Entity_Id (Chars (Pack_Id), Homonym (Pack_Id));
3115 -- Set name and scope of internal subprogram so that the
3116 -- proper external name will be generated. The proper scope
3117 -- is the scope of the wrapper package. We need to generate
3118 -- debugging information for the internal subprogram, so set
3119 -- flag accordingly.
3121 Set_Chars (Anon_Id, Chars (Defining_Entity (N)));
3122 Set_Scope (Anon_Id, Scope (Pack_Id));
3124 -- Mark wrapper package as referenced, to avoid spurious
3125 -- warnings if the instantiation appears in various with_
3126 -- clauses of subunits of the main unit.
3128 Set_Referenced (Pack_Id);
3131 Set_Is_Generic_Instance (Anon_Id);
3132 Set_Needs_Debug_Info (Anon_Id);
3133 Act_Decl_Id := New_Copy (Anon_Id);
3135 Set_Parent (Act_Decl_Id, Parent (Anon_Id));
3136 Set_Chars (Act_Decl_Id, Chars (Defining_Entity (N)));
3137 Set_Sloc (Act_Decl_Id, Sloc (Defining_Entity (N)));
3138 Set_Comes_From_Source (Act_Decl_Id, True);
3140 -- The signature may involve types that are not frozen yet, but
3141 -- the subprogram will be frozen at the point the wrapper package
3142 -- is frozen, so it does not need its own freeze node. In fact, if
3143 -- one is created, it might conflict with the freezing actions from
3144 -- the wrapper package (see 7206-013).
3146 Set_Has_Delayed_Freeze (Anon_Id, False);
3148 -- If the instance is a child unit, mark the Id accordingly. Mark
3149 -- the anonymous entity as well, which is the real subprogram and
3150 -- which is used when the instance appears in a context clause.
3152 Set_Is_Child_Unit (Act_Decl_Id, Is_Child_Unit (Defining_Entity (N)));
3153 Set_Is_Child_Unit (Anon_Id, Is_Child_Unit (Defining_Entity (N)));
3154 New_Overloaded_Entity (Act_Decl_Id);
3155 Check_Eliminated (Act_Decl_Id);
3157 -- In compilation unit case, kill elaboration checks on the
3158 -- instantiation, since they are never needed -- the body is
3159 -- instantiated at the same point as the spec.
3161 if Nkind (Parent (N)) = N_Compilation_Unit then
3162 Set_Suppress_Elaboration_Warnings (Act_Decl_Id);
3163 Set_Kill_Elaboration_Checks (Act_Decl_Id);
3164 Set_Is_Compilation_Unit (Anon_Id);
3166 Set_Cunit_Entity (Current_Sem_Unit, Pack_Id);
3169 -- The instance is not a freezing point for the new subprogram.
3171 Set_Is_Frozen (Act_Decl_Id, False);
3173 if Nkind (Defining_Entity (N)) = N_Defining_Operator_Symbol then
3174 Valid_Operator_Definition (Act_Decl_Id);
3177 Set_Alias (Act_Decl_Id, Anon_Id);
3178 Set_Parent (Act_Decl_Id, Parent (Anon_Id));
3179 Set_Has_Completion (Act_Decl_Id);
3180 Set_Related_Instance (Pack_Id, Act_Decl_Id);
3182 if Nkind (Parent (N)) = N_Compilation_Unit then
3183 Set_Body_Required (Parent (N), False);
3186 end Analyze_Instance_And_Renamings;
3188 -- Start of processing for Analyze_Subprogram_Instantiation
3191 -- Very first thing: apply the special kludge for Text_IO processing
3192 -- in case we are instantiating one of the children of [Wide_]Text_IO.
3193 -- Of course such an instantiation is bogus (these are packages, not
3194 -- subprograms), but we get a better error message if we do this.
3196 Text_IO_Kludge (Gen_Id);
3198 -- Make node global for error reporting.
3200 Instantiation_Node := N;
3201 Pre_Analyze_Actuals (N);
3204 Check_Generic_Child_Unit (Gen_Id, Parent_Installed);
3205 Gen_Unit := Entity (Gen_Id);
3207 Generate_Reference (Gen_Unit, Gen_Id);
3209 if Nkind (Gen_Id) = N_Identifier
3210 and then Chars (Gen_Unit) = Chars (Defining_Entity (N))
3213 ("& is hidden within declaration of instance", Gen_Id, Gen_Unit);
3216 if Etype (Gen_Unit) = Any_Type then
3221 -- Verify that it is a generic subprogram of the right kind, and that
3222 -- it does not lead to a circular instantiation.
3224 if Ekind (Gen_Unit) /= E_Generic_Procedure
3225 and then Ekind (Gen_Unit) /= E_Generic_Function
3227 Error_Msg_N ("expect generic subprogram in instantiation", Gen_Id);
3229 elsif In_Open_Scopes (Gen_Unit) then
3230 Error_Msg_NE ("instantiation of & within itself", N, Gen_Unit);
3232 elsif K = E_Procedure
3233 and then Ekind (Gen_Unit) /= E_Generic_Procedure
3235 if Ekind (Gen_Unit) = E_Generic_Function then
3237 ("cannot instantiate generic function as procedure", Gen_Id);
3240 ("expect name of generic procedure in instantiation", Gen_Id);
3243 elsif K = E_Function
3244 and then Ekind (Gen_Unit) /= E_Generic_Function
3246 if Ekind (Gen_Unit) = E_Generic_Procedure then
3248 ("cannot instantiate generic procedure as function", Gen_Id);
3251 ("expect name of generic function in instantiation", Gen_Id);
3255 Set_Entity (Gen_Id, Gen_Unit);
3256 Set_Is_Instantiated (Gen_Unit);
3258 if In_Extended_Main_Source_Unit (N) then
3259 Generate_Reference (Gen_Unit, N);
3262 -- If renaming, get original unit
3264 if Present (Renamed_Object (Gen_Unit))
3265 and then (Ekind (Renamed_Object (Gen_Unit)) = E_Generic_Procedure
3267 Ekind (Renamed_Object (Gen_Unit)) = E_Generic_Function)
3269 Gen_Unit := Renamed_Object (Gen_Unit);
3270 Set_Is_Instantiated (Gen_Unit);
3271 Generate_Reference (Gen_Unit, N);
3274 if Contains_Instance_Of (Gen_Unit, Current_Scope, Gen_Id) then
3275 Error_Msg_Node_2 := Current_Scope;
3277 ("circular Instantiation: & instantiated in &!", N, Gen_Unit);
3278 Circularity_Detected := True;
3282 Gen_Decl := Unit_Declaration_Node (Gen_Unit);
3284 -- The subprogram itself cannot contain a nested instance, so
3285 -- the current parent is left empty.
3287 Set_Instance_Env (Gen_Unit, Empty);
3289 -- Initialize renamings map, for error checking.
3291 Generic_Renamings.Set_Last (0);
3292 Generic_Renamings_HTable.Reset;
3294 Create_Instantiation_Source (N, Gen_Unit, False, S_Adjustment);
3296 -- Copy original generic tree, to produce text for instantiation.
3300 (Original_Node (Gen_Decl), Empty, Instantiating => True);
3302 Act_Spec := Specification (Act_Tree);
3304 Analyze_Associations
3306 Generic_Formal_Declarations (Act_Tree),
3307 Generic_Formal_Declarations (Gen_Decl));
3309 -- Build the subprogram declaration, which does not appear
3310 -- in the generic template, and give it a sloc consistent
3311 -- with that of the template.
3313 Set_Defining_Unit_Name (Act_Spec, Anon_Id);
3314 Set_Generic_Parent (Act_Spec, Gen_Unit);
3316 Make_Subprogram_Declaration (Sloc (Act_Spec),
3317 Specification => Act_Spec);
3319 Set_Categorization_From_Pragmas (Act_Decl);
3321 if Parent_Installed then
3325 Append (Act_Decl, Renaming_List);
3326 Analyze_Instance_And_Renamings;
3328 -- If the generic is marked Import (Intrinsic), then so is the
3329 -- instance. This indicates that there is no body to instantiate.
3330 -- If generic is marked inline, so it the instance, and the
3331 -- anonymous subprogram it renames. If inlined, or else if inlining
3332 -- is enabled for the compilation, we generate the instance body
3333 -- even if it is not within the main unit.
3335 -- Any other pragmas might also be inherited ???
3337 if Is_Intrinsic_Subprogram (Gen_Unit) then
3338 Set_Is_Intrinsic_Subprogram (Anon_Id);
3339 Set_Is_Intrinsic_Subprogram (Act_Decl_Id);
3341 if Chars (Gen_Unit) = Name_Unchecked_Conversion then
3342 Validate_Unchecked_Conversion (N, Act_Decl_Id);
3346 Generate_Definition (Act_Decl_Id);
3348 Set_Is_Inlined (Act_Decl_Id, Is_Inlined (Gen_Unit));
3349 Set_Is_Inlined (Anon_Id, Is_Inlined (Gen_Unit));
3351 if not Is_Intrinsic_Subprogram (Gen_Unit) then
3352 Check_Elab_Instantiation (N);
3355 Check_Hidden_Child_Unit (N, Gen_Unit, Act_Decl_Id);
3357 -- Subject to change, pending on if other pragmas are inherited ???
3359 Validate_Categorization_Dependency (N, Act_Decl_Id);
3361 if not Is_Intrinsic_Subprogram (Act_Decl_Id) then
3363 if not Generic_Separately_Compiled (Gen_Unit) then
3364 Inherit_Context (Gen_Decl, N);
3367 Restore_Private_Views (Pack_Id, False);
3369 -- If the context requires a full instantiation, mark node for
3370 -- subsequent construction of the body.
3372 if (Is_In_Main_Unit (N)
3373 or else Is_Inlined (Act_Decl_Id))
3374 and then (Operating_Mode = Generate_Code
3375 or else (Operating_Mode = Check_Semantics
3376 and then ASIS_Mode))
3377 and then (Expander_Active or else ASIS_Mode)
3378 and then not ABE_Is_Certain (N)
3379 and then not Is_Eliminated (Act_Decl_Id)
3381 Pending_Instantiations.Increment_Last;
3382 Pending_Instantiations.Table (Pending_Instantiations.Last) :=
3383 (N, Act_Decl, Expander_Active, Current_Sem_Unit);
3384 Check_Forward_Instantiation (Gen_Decl);
3386 -- The wrapper package is always delayed, because it does
3387 -- not constitute a freeze point, but to insure that the
3388 -- freeze node is placed properly, it is created directly
3389 -- when instantiating the body (otherwise the freeze node
3390 -- might appear to early for nested instantiations).
3392 elsif Nkind (Parent (N)) = N_Compilation_Unit then
3394 -- For ASIS purposes, indicate that the wrapper package has
3395 -- replaced the instantiation node.
3397 Rewrite (N, Unit (Parent (N)));
3398 Set_Unit (Parent (N), N);
3401 elsif Nkind (Parent (N)) = N_Compilation_Unit then
3403 -- Replace instance node for library-level instantiations
3404 -- of intrinsic subprograms, for ASIS use.
3406 Rewrite (N, Unit (Parent (N)));
3407 Set_Unit (Parent (N), N);
3410 if Parent_Installed then
3415 Generic_Renamings.Set_Last (0);
3416 Generic_Renamings_HTable.Reset;
3420 when Instantiation_Error =>
3421 if Parent_Installed then
3424 end Analyze_Subprogram_Instantiation;
3426 -------------------------
3427 -- Get_Associated_Node --
3428 -------------------------
3430 function Get_Associated_Node (N : Node_Id) return Node_Id is
3431 Assoc : Node_Id := Associated_Node (N);
3434 if Nkind (Assoc) /= Nkind (N) then
3437 elsif Nkind (Assoc) = N_Aggregate
3438 or else Nkind (Assoc) = N_Extension_Aggregate
3442 -- If the node is part of an inner generic, it may itself have been
3443 -- remapped into a further generic copy. Associated_Node is otherwise
3444 -- used for the entity of the node, and will be of a different node
3445 -- kind, or else N has been rewritten as a literal or function call.
3447 while Present (Associated_Node (Assoc))
3448 and then Nkind (Associated_Node (Assoc)) = Nkind (Assoc)
3450 Assoc := Associated_Node (Assoc);
3453 -- Follow and additional link in case the final node was rewritten.
3454 -- This can only happen with nested generic units.
3456 if (Nkind (Assoc) = N_Identifier or else Nkind (Assoc) in N_Op)
3457 and then Present (Associated_Node (Assoc))
3458 and then (Nkind (Associated_Node (Assoc)) = N_Function_Call
3460 Nkind (Associated_Node (Assoc)) = N_Explicit_Dereference
3462 Nkind (Associated_Node (Assoc)) = N_Integer_Literal
3464 Nkind (Associated_Node (Assoc)) = N_Real_Literal
3466 Nkind (Associated_Node (Assoc)) = N_String_Literal)
3468 Assoc := Associated_Node (Assoc);
3473 end Get_Associated_Node;
3475 -------------------------------------------
3476 -- Build_Instance_Compilation_Unit_Nodes --
3477 -------------------------------------------
3479 procedure Build_Instance_Compilation_Unit_Nodes
3484 Decl_Cunit : Node_Id;
3485 Body_Cunit : Node_Id;
3487 New_Main : constant Entity_Id := Defining_Entity (Act_Decl);
3488 Old_Main : constant Entity_Id := Cunit_Entity (Main_Unit);
3491 -- A new compilation unit node is built for the instance declaration
3494 Make_Compilation_Unit (Sloc (N),
3495 Context_Items => Empty_List,
3498 Make_Compilation_Unit_Aux (Sloc (N)));
3500 Set_Parent_Spec (Act_Decl, Parent_Spec (N));
3501 Set_Body_Required (Decl_Cunit, True);
3503 -- We use the original instantiation compilation unit as the resulting
3504 -- compilation unit of the instance, since this is the main unit.
3506 Rewrite (N, Act_Body);
3507 Body_Cunit := Parent (N);
3509 -- The two compilation unit nodes are linked by the Library_Unit field
3511 Set_Library_Unit (Decl_Cunit, Body_Cunit);
3512 Set_Library_Unit (Body_Cunit, Decl_Cunit);
3514 -- Preserve the private nature of the package if needed.
3516 Set_Private_Present (Decl_Cunit, Private_Present (Body_Cunit));
3518 -- If the instance is not the main unit, its context, categorization,
3519 -- and elaboration entity are not relevant to the compilation.
3521 if Parent (N) /= Cunit (Main_Unit) then
3525 -- The context clause items on the instantiation, which are now
3526 -- attached to the body compilation unit (since the body overwrote
3527 -- the original instantiation node), semantically belong on the spec,
3528 -- so copy them there. It's harmless to leave them on the body as well.
3529 -- In fact one could argue that they belong in both places.
3531 Citem := First (Context_Items (Body_Cunit));
3532 while Present (Citem) loop
3533 Append (New_Copy (Citem), Context_Items (Decl_Cunit));
3537 -- Propagate categorization flags on packages, so that they appear
3538 -- in ali file for the spec of the unit.
3540 if Ekind (New_Main) = E_Package then
3541 Set_Is_Pure (Old_Main, Is_Pure (New_Main));
3542 Set_Is_Preelaborated (Old_Main, Is_Preelaborated (New_Main));
3543 Set_Is_Remote_Types (Old_Main, Is_Remote_Types (New_Main));
3544 Set_Is_Shared_Passive (Old_Main, Is_Shared_Passive (New_Main));
3545 Set_Is_Remote_Call_Interface
3546 (Old_Main, Is_Remote_Call_Interface (New_Main));
3549 -- Make entry in Units table, so that binder can generate call to
3550 -- elaboration procedure for body, if any.
3552 Make_Instance_Unit (Body_Cunit);
3553 Main_Unit_Entity := New_Main;
3554 Set_Cunit_Entity (Main_Unit, Main_Unit_Entity);
3556 -- Build elaboration entity, since the instance may certainly
3557 -- generate elaboration code requiring a flag for protection.
3559 Build_Elaboration_Entity (Decl_Cunit, New_Main);
3560 end Build_Instance_Compilation_Unit_Nodes;
3562 -----------------------------------
3563 -- Check_Formal_Package_Instance --
3564 -----------------------------------
3566 -- If the formal has specific parameters, they must match those of the
3567 -- actual. Both of them are instances, and the renaming declarations
3568 -- for their formal parameters appear in the same order in both. The
3569 -- analyzed formal has been analyzed in the context of the current
3572 procedure Check_Formal_Package_Instance
3573 (Formal_Pack : Entity_Id;
3574 Actual_Pack : Entity_Id)
3576 E1 : Entity_Id := First_Entity (Actual_Pack);
3577 E2 : Entity_Id := First_Entity (Formal_Pack);
3582 procedure Check_Mismatch (B : Boolean);
3583 -- Common error routine for mismatch between the parameters of
3584 -- the actual instance and those of the formal package.
3586 procedure Check_Mismatch (B : Boolean) is
3590 ("actual for & in actual instance does not match formal",
3591 Parent (Actual_Pack), E1);
3595 -- Start of processing for Check_Formal_Package_Instance
3599 and then Present (E2)
3601 exit when Ekind (E1) = E_Package
3602 and then Renamed_Entity (E1) = Renamed_Entity (Actual_Pack);
3604 if Is_Type (E1) then
3606 -- Subtypes must statically match. E1 and E2 are the
3607 -- local entities that are subtypes of the actuals.
3608 -- Itypes generated for other parameters need not be checked,
3609 -- the check will be performed on the parameters themselves.
3611 if not Is_Itype (E1)
3612 and then not Is_Itype (E2)
3616 or else Etype (E1) /= Etype (E2)
3617 or else not Subtypes_Statically_Match (E1, E2));
3620 elsif Ekind (E1) = E_Constant then
3622 -- IN parameters must denote the same static value, or
3623 -- the same constant, or the literal null.
3625 Expr1 := Expression (Parent (E1));
3627 if Ekind (E2) /= E_Constant then
3628 Check_Mismatch (True);
3631 Expr2 := Expression (Parent (E2));
3634 if Is_Static_Expression (Expr1) then
3636 if not Is_Static_Expression (Expr2) then
3637 Check_Mismatch (True);
3639 elsif Is_Integer_Type (Etype (E1)) then
3642 V1 : constant Uint := Expr_Value (Expr1);
3643 V2 : constant Uint := Expr_Value (Expr2);
3645 Check_Mismatch (V1 /= V2);
3648 elsif Is_Real_Type (Etype (E1)) then
3650 V1 : constant Ureal := Expr_Value_R (Expr1);
3651 V2 : constant Ureal := Expr_Value_R (Expr2);
3653 Check_Mismatch (V1 /= V2);
3656 elsif Is_String_Type (Etype (E1))
3657 and then Nkind (Expr1) = N_String_Literal
3660 if Nkind (Expr2) /= N_String_Literal then
3661 Check_Mismatch (True);
3664 (not String_Equal (Strval (Expr1), Strval (Expr2)));
3668 elsif Is_Entity_Name (Expr1) then
3669 if Is_Entity_Name (Expr2) then
3670 if Entity (Expr1) = Entity (Expr2) then
3673 elsif Ekind (Entity (Expr2)) = E_Constant
3674 and then Is_Entity_Name (Constant_Value (Entity (Expr2)))
3676 Entity (Constant_Value (Entity (Expr2))) = Entity (Expr1)
3680 Check_Mismatch (True);
3683 Check_Mismatch (True);
3686 elsif Nkind (Expr1) = N_Null then
3687 Check_Mismatch (Nkind (Expr1) /= N_Null);
3690 Check_Mismatch (True);
3693 elsif Ekind (E1) = E_Variable
3694 or else Ekind (E1) = E_Package
3697 (Ekind (E1) /= Ekind (E2)
3698 or else Renamed_Object (E1) /= Renamed_Object (E2));
3700 elsif Is_Overloadable (E1) then
3702 -- Verify that the names of the entities match.
3703 -- What if actual is an attribute ???
3706 (Ekind (E2) /= Ekind (E1) or else (Alias (E1)) /= Alias (E2));
3709 raise Program_Error;
3716 end Check_Formal_Package_Instance;
3718 ---------------------------
3719 -- Check_Formal_Packages --
3720 ---------------------------
3722 procedure Check_Formal_Packages (P_Id : Entity_Id) is
3724 Formal_P : Entity_Id;
3727 -- Iterate through the declarations in the instance, looking for
3728 -- package renaming declarations that denote instances of formal
3729 -- packages. Stop when we find the renaming of the current package
3730 -- itself. The declaration for a formal package without a box is
3731 -- followed by an internal entity that repeats the instantiation.
3733 E := First_Entity (P_Id);
3734 while Present (E) loop
3735 if Ekind (E) = E_Package then
3736 if Renamed_Object (E) = P_Id then
3739 elsif Nkind (Parent (E)) /= N_Package_Renaming_Declaration then
3742 elsif not Box_Present (Parent (Associated_Formal_Package (E))) then
3743 Formal_P := Next_Entity (E);
3744 Check_Formal_Package_Instance (Formal_P, E);
3750 end Check_Formal_Packages;
3752 ---------------------------------
3753 -- Check_Forward_Instantiation --
3754 ---------------------------------
3756 procedure Check_Forward_Instantiation (Decl : Node_Id) is
3758 Gen_Comp : Entity_Id := Cunit_Entity (Get_Source_Unit (Decl));
3761 -- The instantiation appears before the generic body if we are in the
3762 -- scope of the unit containing the generic, either in its spec or in
3763 -- the package body. and before the generic body.
3765 if Ekind (Gen_Comp) = E_Package_Body then
3766 Gen_Comp := Spec_Entity (Gen_Comp);
3769 if In_Open_Scopes (Gen_Comp)
3770 and then No (Corresponding_Body (Decl))
3775 and then not Is_Compilation_Unit (S)
3776 and then not Is_Child_Unit (S)
3778 if Ekind (S) = E_Package then
3779 Set_Has_Forward_Instantiation (S);
3785 end Check_Forward_Instantiation;
3787 ---------------------------
3788 -- Check_Generic_Actuals --
3789 ---------------------------
3791 -- The visibility of the actuals may be different between the
3792 -- point of generic instantiation and the instantiation of the body.
3794 procedure Check_Generic_Actuals
3795 (Instance : Entity_Id;
3796 Is_Formal_Box : Boolean)
3802 E := First_Entity (Instance);
3803 while Present (E) loop
3805 and then Nkind (Parent (E)) = N_Subtype_Declaration
3806 and then Scope (Etype (E)) /= Instance
3807 and then Is_Entity_Name (Subtype_Indication (Parent (E)))
3809 Check_Private_View (Subtype_Indication (Parent (E)));
3810 Set_Is_Generic_Actual_Type (E, True);
3811 Set_Is_Hidden (E, False);
3813 -- We constructed the generic actual type as a subtype of
3814 -- the supplied type. This means that it normally would not
3815 -- inherit subtype specific attributes of the actual, which
3816 -- is wrong for the generic case.
3818 Astype := Ancestor_Subtype (E);
3822 -- can happen when E is an itype that is the full view of
3823 -- a private type completed, e.g. with a constrained array.
3825 Astype := Base_Type (E);
3828 Set_Size_Info (E, (Astype));
3829 Set_RM_Size (E, RM_Size (Astype));
3830 Set_First_Rep_Item (E, First_Rep_Item (Astype));
3832 if Is_Discrete_Or_Fixed_Point_Type (E) then
3833 Set_RM_Size (E, RM_Size (Astype));
3835 -- In nested instances, the base type of an access actual
3836 -- may itself be private, and need to be exchanged.
3838 elsif Is_Access_Type (E)
3839 and then Is_Private_Type (Etype (E))
3842 (New_Occurrence_Of (Etype (E), Sloc (Instance)));
3845 elsif Ekind (E) = E_Package then
3847 -- If this is the renaming for the current instance, we're done.
3848 -- Otherwise it is a formal package. If the corresponding formal
3849 -- was declared with a box, the (instantiations of the) generic
3850 -- formal part are also visible. Otherwise, ignore the entity
3851 -- created to validate the actuals.
3853 if Renamed_Object (E) = Instance then
3856 elsif Nkind (Parent (E)) /= N_Package_Renaming_Declaration then
3859 -- The visibility of a formal of an enclosing generic is already
3862 elsif Denotes_Formal_Package (E) then
3865 elsif Present (Associated_Formal_Package (E))
3866 and then Box_Present (Parent (Associated_Formal_Package (E)))
3868 Check_Generic_Actuals (Renamed_Object (E), True);
3869 Set_Is_Hidden (E, False);
3872 -- If this is a subprogram instance (in a wrapper package) the
3873 -- actual is fully visible.
3875 elsif Is_Wrapper_Package (Instance) then
3876 Set_Is_Hidden (E, False);
3879 Set_Is_Hidden (E, not Is_Formal_Box);
3884 end Check_Generic_Actuals;
3886 ------------------------------
3887 -- Check_Generic_Child_Unit --
3888 ------------------------------
3890 procedure Check_Generic_Child_Unit
3892 Parent_Installed : in out Boolean)
3894 Loc : constant Source_Ptr := Sloc (Gen_Id);
3895 Gen_Par : Entity_Id := Empty;
3896 Inst_Par : Entity_Id;
3900 function Find_Generic_Child
3904 -- Search generic parent for possible child unit with the given name.
3906 function In_Enclosing_Instance return Boolean;
3907 -- Within an instance of the parent, the child unit may be denoted
3908 -- by a simple name, or an abbreviated expanded name. Examine enclosing
3909 -- scopes to locate a possible parent instantiation.
3911 ------------------------
3912 -- Find_Generic_Child --
3913 ------------------------
3915 function Find_Generic_Child
3923 -- If entity of name is already set, instance has already been
3924 -- resolved, e.g. in an enclosing instantiation.
3926 if Present (Entity (Id)) then
3927 if Scope (Entity (Id)) = Scop then
3934 E := First_Entity (Scop);
3935 while Present (E) loop
3936 if Chars (E) = Chars (Id)
3937 and then Is_Child_Unit (E)
3939 if Is_Child_Unit (E)
3940 and then not Is_Visible_Child_Unit (E)
3943 ("generic child unit& is not visible", Gen_Id, E);
3955 end Find_Generic_Child;
3957 ---------------------------
3958 -- In_Enclosing_Instance --
3959 ---------------------------
3961 function In_Enclosing_Instance return Boolean is
3962 Enclosing_Instance : Node_Id;
3963 Instance_Decl : Node_Id;
3966 Enclosing_Instance := Current_Scope;
3968 while Present (Enclosing_Instance) loop
3969 Instance_Decl := Unit_Declaration_Node (Enclosing_Instance);
3971 if Ekind (Enclosing_Instance) = E_Package
3972 and then Is_Generic_Instance (Enclosing_Instance)
3974 (Generic_Parent (Specification (Instance_Decl)))
3976 -- Check whether the generic we are looking for is a child
3977 -- of this instance.
3979 E := Find_Generic_Child
3980 (Generic_Parent (Specification (Instance_Decl)), Gen_Id);
3981 exit when Present (E);
3987 Enclosing_Instance := Scope (Enclosing_Instance);
3999 Make_Expanded_Name (Loc,
4001 Prefix => New_Occurrence_Of (Enclosing_Instance, Loc),
4002 Selector_Name => New_Occurrence_Of (E, Loc)));
4004 Set_Entity (Gen_Id, E);
4005 Set_Etype (Gen_Id, Etype (E));
4006 Parent_Installed := False; -- Already in scope.
4009 end In_Enclosing_Instance;
4011 -- Start of processing for Check_Generic_Child_Unit
4014 -- If the name of the generic is given by a selected component, it
4015 -- may be the name of a generic child unit, and the prefix is the name
4016 -- of an instance of the parent, in which case the child unit must be
4017 -- visible. If this instance is not in scope, it must be placed there
4018 -- and removed after instantiation, because what is being instantiated
4019 -- is not the original child, but the corresponding child present in
4020 -- the instance of the parent.
4022 -- If the child is instantiated within the parent, it can be given by
4023 -- a simple name. In this case the instance is already in scope, but
4024 -- the child generic must be recovered from the generic parent as well.
4026 if Nkind (Gen_Id) = N_Selected_Component then
4027 S := Selector_Name (Gen_Id);
4028 Analyze (Prefix (Gen_Id));
4029 Inst_Par := Entity (Prefix (Gen_Id));
4031 if Ekind (Inst_Par) = E_Package
4032 and then Present (Renamed_Object (Inst_Par))
4034 Inst_Par := Renamed_Object (Inst_Par);
4037 if Ekind (Inst_Par) = E_Package then
4038 if Nkind (Parent (Inst_Par)) = N_Package_Specification then
4039 Gen_Par := Generic_Parent (Parent (Inst_Par));
4041 elsif Nkind (Parent (Inst_Par)) = N_Defining_Program_Unit_Name
4043 Nkind (Parent (Parent (Inst_Par))) = N_Package_Specification
4045 Gen_Par := Generic_Parent (Parent (Parent (Inst_Par)));
4048 elsif Ekind (Inst_Par) = E_Generic_Package
4049 and then Nkind (Parent (Gen_Id)) = N_Formal_Package_Declaration
4051 -- A formal package may be a real child package, and not the
4052 -- implicit instance within a parent. In this case the child is
4053 -- not visible and has to be retrieved explicitly as well.
4055 Gen_Par := Inst_Par;
4058 if Present (Gen_Par) then
4060 -- The prefix denotes an instantiation. The entity itself
4061 -- may be a nested generic, or a child unit.
4063 E := Find_Generic_Child (Gen_Par, S);
4066 Change_Selected_Component_To_Expanded_Name (Gen_Id);
4067 Set_Entity (Gen_Id, E);
4068 Set_Etype (Gen_Id, Etype (E));
4070 Set_Etype (S, Etype (E));
4072 -- Indicate that this is a reference to the parent.
4074 if In_Extended_Main_Source_Unit (Gen_Id) then
4075 Set_Is_Instantiated (Inst_Par);
4078 -- A common mistake is to replicate the naming scheme of
4079 -- a hierarchy by instantiating a generic child directly,
4080 -- rather than the implicit child in a parent instance:
4082 -- generic .. package Gpar is ..
4083 -- generic .. package Gpar.Child is ..
4084 -- package Par is new Gpar ();
4087 -- package Par.Child is new Gpar.Child ();
4088 -- rather than Par.Child
4090 -- In this case the instantiation is within Par, which is
4091 -- an instance, but Gpar does not denote Par because we are
4092 -- not IN the instance of Gpar, so this is illegal. The test
4093 -- below recognizes this particular case.
4095 if Is_Child_Unit (E)
4096 and then not Comes_From_Source (Entity (Prefix (Gen_Id)))
4097 and then (not In_Instance
4098 or else Nkind (Parent (Parent (Gen_Id))) =
4102 ("prefix of generic child unit must be instance of parent",
4106 if not In_Open_Scopes (Inst_Par)
4107 and then Nkind (Parent (Gen_Id)) not in
4108 N_Generic_Renaming_Declaration
4110 Install_Parent (Inst_Par);
4111 Parent_Installed := True;
4115 -- If the generic parent does not contain an entity that
4116 -- corresponds to the selector, the instance doesn't either.
4117 -- Analyzing the node will yield the appropriate error message.
4118 -- If the entity is not a child unit, then it is an inner
4119 -- generic in the parent.
4127 if Is_Child_Unit (Entity (Gen_Id))
4129 Nkind (Parent (Gen_Id)) not in N_Generic_Renaming_Declaration
4130 and then not In_Open_Scopes (Inst_Par)
4132 Install_Parent (Inst_Par);
4133 Parent_Installed := True;
4137 elsif Nkind (Gen_Id) = N_Expanded_Name then
4139 -- Entity already present, analyze prefix, whose meaning may be
4140 -- an instance in the current context. If it is an instance of
4141 -- a relative within another, the proper parent may still have
4142 -- to be installed, if they are not of the same generation.
4144 Analyze (Prefix (Gen_Id));
4145 Inst_Par := Entity (Prefix (Gen_Id));
4147 if In_Enclosing_Instance then
4150 elsif Present (Entity (Gen_Id))
4151 and then Is_Child_Unit (Entity (Gen_Id))
4152 and then not In_Open_Scopes (Inst_Par)
4154 Install_Parent (Inst_Par);
4155 Parent_Installed := True;
4158 elsif In_Enclosing_Instance then
4160 -- The child unit is found in some enclosing scope
4167 -- If this is the renaming of the implicit child in a parent
4168 -- instance, recover the parent name and install it.
4170 if Is_Entity_Name (Gen_Id) then
4171 E := Entity (Gen_Id);
4173 if Is_Generic_Unit (E)
4174 and then Nkind (Parent (E)) in N_Generic_Renaming_Declaration
4175 and then Is_Child_Unit (Renamed_Object (E))
4176 and then Is_Generic_Unit (Scope (Renamed_Object (E)))
4177 and then Nkind (Name (Parent (E))) = N_Expanded_Name
4180 New_Copy_Tree (Name (Parent (E))));
4181 Inst_Par := Entity (Prefix (Gen_Id));
4183 if not In_Open_Scopes (Inst_Par) then
4184 Install_Parent (Inst_Par);
4185 Parent_Installed := True;
4188 -- If it is a child unit of a non-generic parent, it may be
4189 -- use-visible and given by a direct name. Install parent as
4192 elsif Is_Generic_Unit (E)
4193 and then Is_Child_Unit (E)
4195 Nkind (Parent (Gen_Id)) not in N_Generic_Renaming_Declaration
4196 and then not Is_Generic_Unit (Scope (E))
4198 if not In_Open_Scopes (Scope (E)) then
4199 Install_Parent (Scope (E));
4200 Parent_Installed := True;
4205 end Check_Generic_Child_Unit;
4207 -----------------------------
4208 -- Check_Hidden_Child_Unit --
4209 -----------------------------
4211 procedure Check_Hidden_Child_Unit
4213 Gen_Unit : Entity_Id;
4214 Act_Decl_Id : Entity_Id)
4216 Gen_Id : constant Node_Id := Name (N);
4219 if Is_Child_Unit (Gen_Unit)
4220 and then Is_Child_Unit (Act_Decl_Id)
4221 and then Nkind (Gen_Id) = N_Expanded_Name
4222 and then Entity (Prefix (Gen_Id)) = Scope (Act_Decl_Id)
4223 and then Chars (Gen_Unit) = Chars (Act_Decl_Id)
4225 Error_Msg_Node_2 := Scope (Act_Decl_Id);
4227 ("generic unit & is implicitly declared in &",
4228 Defining_Unit_Name (N), Gen_Unit);
4229 Error_Msg_N ("\instance must have different name",
4230 Defining_Unit_Name (N));
4232 end Check_Hidden_Child_Unit;
4234 ------------------------
4235 -- Check_Private_View --
4236 ------------------------
4238 procedure Check_Private_View (N : Node_Id) is
4239 T : constant Entity_Id := Etype (N);
4243 -- Exchange views if the type was not private in the generic but is
4244 -- private at the point of instantiation. Do not exchange views if
4245 -- the scope of the type is in scope. This can happen if both generic
4246 -- and instance are sibling units, or if type is defined in a parent.
4247 -- In this case the visibility of the type will be correct for all
4251 BT := Base_Type (T);
4253 if Is_Private_Type (T)
4254 and then not Has_Private_View (N)
4255 and then Present (Full_View (T))
4256 and then not In_Open_Scopes (Scope (T))
4258 -- In the generic, the full type was visible. Save the
4259 -- private entity, for subsequent exchange.
4263 elsif Has_Private_View (N)
4264 and then not Is_Private_Type (T)
4265 and then not Has_Been_Exchanged (T)
4266 and then Etype (Get_Associated_Node (N)) /= T
4268 -- Only the private declaration was visible in the generic. If
4269 -- the type appears in a subtype declaration, the subtype in the
4270 -- instance must have a view compatible with that of its parent,
4271 -- which must be exchanged (see corresponding code in Restore_
4272 -- Private_Views). Otherwise, if the type is defined in a parent
4273 -- unit, leave full visibility within instance, which is safe.
4275 if In_Open_Scopes (Scope (Base_Type (T)))
4276 and then not Is_Private_Type (Base_Type (T))
4277 and then Comes_From_Source (Base_Type (T))
4281 elsif Nkind (Parent (N)) = N_Subtype_Declaration
4282 or else not In_Private_Part (Scope (Base_Type (T)))
4284 Append_Elmt (T, Exchanged_Views);
4285 Exchange_Declarations (Etype (Get_Associated_Node (N)));
4288 -- For composite types with inconsistent representation
4289 -- exchange component types accordingly.
4291 elsif Is_Access_Type (T)
4292 and then Is_Private_Type (Designated_Type (T))
4293 and then not Has_Private_View (N)
4294 and then Present (Full_View (Designated_Type (T)))
4296 Switch_View (Designated_Type (T));
4298 elsif Is_Array_Type (T)
4299 and then Is_Private_Type (Component_Type (T))
4300 and then not Has_Private_View (N)
4301 and then Present (Full_View (Component_Type (T)))
4303 Switch_View (Component_Type (T));
4305 elsif Is_Private_Type (T)
4306 and then Present (Full_View (T))
4307 and then Is_Array_Type (Full_View (T))
4308 and then Is_Private_Type (Component_Type (Full_View (T)))
4312 -- Finally, a non-private subtype may have a private base type,
4313 -- which must be exchanged for consistency. This can happen when
4314 -- instantiating a package body, when the scope stack is empty
4315 -- but in fact the subtype and the base type are declared in an
4318 elsif not Is_Private_Type (T)
4319 and then not Has_Private_View (N)
4320 and then Is_Private_Type (Base_Type (T))
4321 and then Present (Full_View (BT))
4322 and then not Is_Generic_Type (BT)
4323 and then not In_Open_Scopes (BT)
4325 Append_Elmt (Full_View (BT), Exchanged_Views);
4326 Exchange_Declarations (BT);
4329 end Check_Private_View;
4331 --------------------------
4332 -- Contains_Instance_Of --
4333 --------------------------
4335 function Contains_Instance_Of
4347 -- Verify that there are no circular instantiations. We check whether
4348 -- the unit contains an instance of the current scope or some enclosing
4349 -- scope (in case one of the instances appears in a subunit). Longer
4350 -- circularities involving subunits might seem too pathological to
4351 -- consider, but they were not too pathological for the authors of
4352 -- DEC bc30vsq, so we loop over all enclosing scopes, and mark all
4353 -- enclosing generic scopes as containing an instance.
4356 -- Within a generic subprogram body, the scope is not generic, to
4357 -- allow for recursive subprograms. Use the declaration to determine
4358 -- whether this is a generic unit.
4360 if Ekind (Scop) = E_Generic_Package
4361 or else (Is_Subprogram (Scop)
4362 and then Nkind (Unit_Declaration_Node (Scop)) =
4363 N_Generic_Subprogram_Declaration)
4365 Elmt := First_Elmt (Inner_Instances (Inner));
4367 while Present (Elmt) loop
4368 if Node (Elmt) = Scop then
4369 Error_Msg_Node_2 := Inner;
4371 ("circular Instantiation: & instantiated within &!",
4375 elsif Node (Elmt) = Inner then
4378 elsif Contains_Instance_Of (Node (Elmt), Scop, N) then
4379 Error_Msg_Node_2 := Inner;
4381 ("circular Instantiation: & instantiated within &!",
4389 -- Indicate that Inner is being instantiated within Scop.
4391 Append_Elmt (Inner, Inner_Instances (Scop));
4394 if Scop = Standard_Standard then
4397 Scop := Scope (Scop);
4402 end Contains_Instance_Of;
4404 -----------------------
4405 -- Copy_Generic_Node --
4406 -----------------------
4408 function Copy_Generic_Node
4410 Parent_Id : Node_Id;
4411 Instantiating : Boolean)
4417 function Copy_Generic_Descendant (D : Union_Id) return Union_Id;
4418 -- Check the given value of one of the Fields referenced by the
4419 -- current node to determine whether to copy it recursively. The
4420 -- field may hold a Node_Id, a List_Id, or an Elist_Id, or a plain
4421 -- value (Sloc, Uint, Char) in which case it need not be copied.
4423 procedure Copy_Descendants;
4424 -- Common utility for various nodes.
4426 function Copy_Generic_Elist (E : Elist_Id) return Elist_Id;
4427 -- Make copy of element list.
4429 function Copy_Generic_List
4431 Parent_Id : Node_Id)
4433 -- Apply Copy_Node recursively to the members of a node list.
4435 function In_Defining_Unit_Name (Nam : Node_Id) return Boolean;
4436 -- True if an identifier is part of the defining program unit name
4437 -- of a child unit. The entity of such an identifier must be kept
4438 -- (for ASIS use) even though as the name of an enclosing generic
4439 -- it would otherwise not be preserved in the generic tree.
4441 -----------------------
4442 -- Copy_Descendants --
4443 -----------------------
4445 procedure Copy_Descendants is
4447 use Atree.Unchecked_Access;
4448 -- This code section is part of the implementation of an untyped
4449 -- tree traversal, so it needs direct access to node fields.
4452 Set_Field1 (New_N, Copy_Generic_Descendant (Field1 (N)));
4453 Set_Field2 (New_N, Copy_Generic_Descendant (Field2 (N)));
4454 Set_Field3 (New_N, Copy_Generic_Descendant (Field3 (N)));
4455 Set_Field4 (New_N, Copy_Generic_Descendant (Field4 (N)));
4456 Set_Field5 (New_N, Copy_Generic_Descendant (Field5 (N)));
4457 end Copy_Descendants;
4459 -----------------------------
4460 -- Copy_Generic_Descendant --
4461 -----------------------------
4463 function Copy_Generic_Descendant (D : Union_Id) return Union_Id is
4465 if D = Union_Id (Empty) then
4468 elsif D in Node_Range then
4470 (Copy_Generic_Node (Node_Id (D), New_N, Instantiating));
4472 elsif D in List_Range then
4473 return Union_Id (Copy_Generic_List (List_Id (D), New_N));
4475 elsif D in Elist_Range then
4476 return Union_Id (Copy_Generic_Elist (Elist_Id (D)));
4478 -- Nothing else is copyable (e.g. Uint values), return as is
4483 end Copy_Generic_Descendant;
4485 ------------------------
4486 -- Copy_Generic_Elist --
4487 ------------------------
4489 function Copy_Generic_Elist (E : Elist_Id) return Elist_Id is
4496 M := First_Elmt (E);
4497 while Present (M) loop
4499 (Copy_Generic_Node (Node (M), Empty, Instantiating), L);
4508 end Copy_Generic_Elist;
4510 -----------------------
4511 -- Copy_Generic_List --
4512 -----------------------
4514 function Copy_Generic_List
4516 Parent_Id : Node_Id)
4525 Set_Parent (New_L, Parent_Id);
4528 while Present (N) loop
4529 Append (Copy_Generic_Node (N, Empty, Instantiating), New_L);
4538 end Copy_Generic_List;
4540 ---------------------------
4541 -- In_Defining_Unit_Name --
4542 ---------------------------
4544 function In_Defining_Unit_Name (Nam : Node_Id) return Boolean is
4546 return Present (Parent (Nam))
4547 and then (Nkind (Parent (Nam)) = N_Defining_Program_Unit_Name
4549 (Nkind (Parent (Nam)) = N_Expanded_Name
4550 and then In_Defining_Unit_Name (Parent (Nam))));
4551 end In_Defining_Unit_Name;
4553 -- Start of processing for Copy_Generic_Node
4560 New_N := New_Copy (N);
4562 if Instantiating then
4563 Adjust_Instantiation_Sloc (New_N, S_Adjustment);
4566 if not Is_List_Member (N) then
4567 Set_Parent (New_N, Parent_Id);
4570 -- If defining identifier, then all fields have been copied already
4572 if Nkind (New_N) in N_Entity then
4575 -- Special casing for identifiers and other entity names and operators
4577 elsif Nkind (New_N) = N_Identifier
4578 or else Nkind (New_N) = N_Character_Literal
4579 or else Nkind (New_N) = N_Expanded_Name
4580 or else Nkind (New_N) = N_Operator_Symbol
4581 or else Nkind (New_N) in N_Op
4583 if not Instantiating then
4585 -- Link both nodes in order to assign subsequently the
4586 -- entity of the copy to the original node, in case this
4587 -- is a global reference.
4589 Set_Associated_Node (N, New_N);
4591 -- If we are within an instantiation, this is a nested generic
4592 -- that has already been analyzed at the point of definition. We
4593 -- must preserve references that were global to the enclosing
4594 -- parent at that point. Other occurrences, whether global or
4595 -- local to the current generic, must be resolved anew, so we
4596 -- reset the entity in the generic copy. A global reference has
4597 -- a smaller depth than the parent, or else the same depth in
4598 -- case both are distinct compilation units.
4600 -- It is also possible for Current_Instantiated_Parent to be
4601 -- defined, and for this not to be a nested generic, namely
4602 -- if the unit is loaded through Rtsfind. In that case, the
4603 -- entity of New_N is only a link to the associated node, and
4604 -- not a defining occurrence.
4606 -- The entities for parent units in the defining_program_unit
4607 -- of a generic child unit are established when the context of
4608 -- the unit is first analyzed, before the generic copy is made.
4609 -- They are preserved in the copy for use in ASIS queries.
4611 Ent := Entity (New_N);
4613 if No (Current_Instantiated_Parent.Gen_Id) then
4615 or else Nkind (Ent) /= N_Defining_Identifier
4616 or else not In_Defining_Unit_Name (N)
4618 Set_Associated_Node (New_N, Empty);
4623 not (Nkind (Ent) = N_Defining_Identifier
4625 Nkind (Ent) = N_Defining_Character_Literal
4627 Nkind (Ent) = N_Defining_Operator_Symbol)
4628 or else No (Scope (Ent))
4629 or else Scope (Ent) = Current_Instantiated_Parent.Gen_Id
4630 or else (Scope_Depth (Scope (Ent)) >
4631 Scope_Depth (Current_Instantiated_Parent.Gen_Id)
4633 Get_Source_Unit (Ent) =
4634 Get_Source_Unit (Current_Instantiated_Parent.Gen_Id))
4636 Set_Associated_Node (New_N, Empty);
4639 -- Case of instantiating identifier or some other name or operator
4642 -- If the associated node is still defined, the entity in
4643 -- it is global, and must be copied to the instance.
4645 if Present (Get_Associated_Node (N)) then
4646 if Nkind (Get_Associated_Node (N)) = Nkind (N) then
4647 Set_Entity (New_N, Entity (Get_Associated_Node (N)));
4648 Check_Private_View (N);
4650 elsif Nkind (Get_Associated_Node (N)) = N_Function_Call then
4651 Set_Entity (New_N, Entity (Name (Get_Associated_Node (N))));
4654 Set_Entity (New_N, Empty);
4659 -- For expanded name, we must copy the Prefix and Selector_Name
4661 if Nkind (N) = N_Expanded_Name then
4663 (New_N, Copy_Generic_Node (Prefix (N), New_N, Instantiating));
4665 Set_Selector_Name (New_N,
4666 Copy_Generic_Node (Selector_Name (N), New_N, Instantiating));
4668 -- For operators, we must copy the right operand
4670 elsif Nkind (N) in N_Op then
4671 Set_Right_Opnd (New_N,
4672 Copy_Generic_Node (Right_Opnd (N), New_N, Instantiating));
4674 -- And for binary operators, the left operand as well
4676 if Nkind (N) in N_Binary_Op then
4677 Set_Left_Opnd (New_N,
4678 Copy_Generic_Node (Left_Opnd (N), New_N, Instantiating));
4682 -- Special casing for stubs
4684 elsif Nkind (N) in N_Body_Stub then
4686 -- In any case, we must copy the specification or defining
4687 -- identifier as appropriate.
4689 if Nkind (N) = N_Subprogram_Body_Stub then
4690 Set_Specification (New_N,
4691 Copy_Generic_Node (Specification (N), New_N, Instantiating));
4694 Set_Defining_Identifier (New_N,
4696 (Defining_Identifier (N), New_N, Instantiating));
4699 -- If we are not instantiating, then this is where we load and
4700 -- analyze subunits, i.e. at the point where the stub occurs. A
4701 -- more permissivle system might defer this analysis to the point
4702 -- of instantiation, but this seems to complicated for now.
4704 if not Instantiating then
4706 Subunit_Name : constant Unit_Name_Type := Get_Unit_Name (N);
4708 Unum : Unit_Number_Type;
4714 (Load_Name => Subunit_Name,
4719 -- If the proper body is not found, a warning message will
4720 -- be emitted when analyzing the stub, or later at the the
4721 -- point of instantiation. Here we just leave the stub as is.
4723 if Unum = No_Unit then
4724 Subunits_Missing := True;
4725 goto Subunit_Not_Found;
4728 Subunit := Cunit (Unum);
4730 if Nkind (Unit (Subunit)) /= N_Subunit then
4731 Error_Msg_Sloc := Sloc (N);
4733 ("expected SEPARATE subunit to complete stub at#,"
4734 & " found child unit", Subunit);
4735 goto Subunit_Not_Found;
4738 -- We must create a generic copy of the subunit, in order
4739 -- to perform semantic analysis on it, and we must replace
4740 -- the stub in the original generic unit with the subunit,
4741 -- in order to preserve non-local references within.
4743 -- Only the proper body needs to be copied. Library_Unit and
4744 -- context clause are simply inherited by the generic copy.
4745 -- Note that the copy (which may be recursive if there are
4746 -- nested subunits) must be done first, before attaching it
4747 -- to the enclosing generic.
4751 (Proper_Body (Unit (Subunit)),
4752 Empty, Instantiating => False);
4754 -- Now place the original proper body in the original
4755 -- generic unit. This is a body, not a compilation unit.
4757 Rewrite (N, Proper_Body (Unit (Subunit)));
4758 Set_Is_Compilation_Unit (Defining_Entity (N), False);
4759 Set_Was_Originally_Stub (N);
4761 -- Finally replace the body of the subunit with its copy,
4762 -- and make this new subunit into the library unit of the
4763 -- generic copy, which does not have stubs any longer.
4765 Set_Proper_Body (Unit (Subunit), New_Body);
4766 Set_Library_Unit (New_N, Subunit);
4767 Inherit_Context (Unit (Subunit), N);
4770 -- If we are instantiating, this must be an error case, since
4771 -- otherwise we would have replaced the stub node by the proper
4772 -- body that corresponds. So just ignore it in the copy (i.e.
4773 -- we have copied it, and that is good enough).
4779 <<Subunit_Not_Found>> null;
4781 -- If the node is a compilation unit, it is the subunit of a stub,
4782 -- which has been loaded already (see code below). In this case,
4783 -- the library unit field of N points to the parent unit (which
4784 -- is a compilation unit) and need not (and cannot!) be copied.
4786 -- When the proper body of the stub is analyzed, thie library_unit
4787 -- link is used to establish the proper context (see sem_ch10).
4789 -- The other fields of a compilation unit are copied as usual
4791 elsif Nkind (N) = N_Compilation_Unit then
4793 -- This code can only be executed when not instantiating, because
4794 -- in the copy made for an instantiation, the compilation unit
4795 -- node has disappeared at the point that a stub is replaced by
4798 pragma Assert (not Instantiating);
4800 Set_Context_Items (New_N,
4801 Copy_Generic_List (Context_Items (N), New_N));
4804 Copy_Generic_Node (Unit (N), New_N, False));
4806 Set_First_Inlined_Subprogram (New_N,
4808 (First_Inlined_Subprogram (N), New_N, False));
4810 Set_Aux_Decls_Node (New_N,
4811 Copy_Generic_Node (Aux_Decls_Node (N), New_N, False));
4813 -- For an assignment node, the assignment is known to be semantically
4814 -- legal if we are instantiating the template. This avoids incorrect
4815 -- diagnostics in generated code.
4817 elsif Nkind (N) = N_Assignment_Statement then
4819 -- Copy name and expression fields in usual manner
4822 Copy_Generic_Node (Name (N), New_N, Instantiating));
4824 Set_Expression (New_N,
4825 Copy_Generic_Node (Expression (N), New_N, Instantiating));
4827 if Instantiating then
4828 Set_Assignment_OK (Name (New_N), True);
4831 elsif Nkind (N) = N_Aggregate
4832 or else Nkind (N) = N_Extension_Aggregate
4835 if not Instantiating then
4836 Set_Associated_Node (N, New_N);
4839 if Present (Get_Associated_Node (N))
4840 and then Nkind (Get_Associated_Node (N)) = Nkind (N)
4842 -- In the generic the aggregate has some composite type. If at
4843 -- the point of instantiation the type has a private view,
4844 -- install the full view (and that of its ancestors, if any).
4847 T : Entity_Id := (Etype (Get_Associated_Node (New_N)));
4852 and then Is_Private_Type (T)
4858 and then Is_Tagged_Type (T)
4859 and then Is_Derived_Type (T)
4861 Rt := Root_Type (T);
4866 if Is_Private_Type (T) then
4877 -- Do not copy the associated node, which points to
4878 -- the generic copy of the aggregate.
4881 use Atree.Unchecked_Access;
4882 -- This code section is part of the implementation of an untyped
4883 -- tree traversal, so it needs direct access to node fields.
4886 Set_Field1 (New_N, Copy_Generic_Descendant (Field1 (N)));
4887 Set_Field2 (New_N, Copy_Generic_Descendant (Field2 (N)));
4888 Set_Field3 (New_N, Copy_Generic_Descendant (Field3 (N)));
4889 Set_Field5 (New_N, Copy_Generic_Descendant (Field5 (N)));
4892 -- Allocators do not have an identifier denoting the access type,
4893 -- so we must locate it through the expression to check whether
4894 -- the views are consistent.
4896 elsif Nkind (N) = N_Allocator
4897 and then Nkind (Expression (N)) = N_Qualified_Expression
4898 and then Is_Entity_Name (Subtype_Mark (Expression (N)))
4899 and then Instantiating
4902 T : constant Node_Id :=
4903 Get_Associated_Node (Subtype_Mark (Expression (N)));
4908 -- Retrieve the allocator node in the generic copy.
4910 Acc_T := Etype (Parent (Parent (T)));
4912 and then Is_Private_Type (Acc_T)
4914 Switch_View (Acc_T);
4921 -- For a proper body, we must catch the case of a proper body that
4922 -- replaces a stub. This represents the point at which a separate
4923 -- compilation unit, and hence template file, may be referenced, so
4924 -- we must make a new source instantiation entry for the template
4925 -- of the subunit, and ensure that all nodes in the subunit are
4926 -- adjusted using this new source instantiation entry.
4928 elsif Nkind (N) in N_Proper_Body then
4930 Save_Adjustment : constant Sloc_Adjustment := S_Adjustment;
4933 if Instantiating and then Was_Originally_Stub (N) then
4934 Create_Instantiation_Source
4935 (Instantiation_Node,
4936 Defining_Entity (N),
4941 -- Now copy the fields of the proper body, using the new
4942 -- adjustment factor if one was needed as per test above.
4946 -- Restore the original adjustment factor in case changed
4948 S_Adjustment := Save_Adjustment;
4951 -- Don't copy Ident or Comment pragmas, since the comment belongs
4952 -- to the generic unit, not to the instantiating unit.
4954 elsif Nkind (N) = N_Pragma
4955 and then Instantiating
4958 Prag_Id : constant Pragma_Id := Get_Pragma_Id (Chars (N));
4961 if Prag_Id = Pragma_Ident
4962 or else Prag_Id = Pragma_Comment
4964 New_N := Make_Null_Statement (Sloc (N));
4971 elsif Nkind (N) = N_Integer_Literal
4972 or else Nkind (N) = N_Real_Literal
4974 -- No descendant fields need traversing
4978 -- For the remaining nodes, copy recursively their descendants
4984 and then Nkind (N) = N_Subprogram_Body
4986 Set_Generic_Parent (Specification (New_N), N);
4991 end Copy_Generic_Node;
4993 ----------------------------
4994 -- Denotes_Formal_Package --
4995 ----------------------------
4997 function Denotes_Formal_Package (Pack : Entity_Id) return Boolean is
4998 Par : constant Entity_Id := Current_Instantiated_Parent.Act_Id;
4999 Scop : constant Entity_Id := Scope (Pack);
5003 if Ekind (Scop) = E_Generic_Package
5004 or else Nkind (Unit_Declaration_Node (Scop)) =
5005 N_Generic_Subprogram_Declaration
5009 elsif Nkind (Parent (Pack)) = N_Formal_Package_Declaration then
5016 -- Check whether this package is associated with a formal
5017 -- package of the enclosing instantiation. Iterate over the
5018 -- list of renamings.
5020 E := First_Entity (Par);
5021 while Present (E) loop
5022 if Ekind (E) /= E_Package
5023 or else Nkind (Parent (E)) /= N_Package_Renaming_Declaration
5026 elsif Renamed_Object (E) = Par then
5029 elsif Renamed_Object (E) = Pack then
5038 end Denotes_Formal_Package;
5044 procedure End_Generic is
5046 -- ??? More things could be factored out in this
5047 -- routine. Should probably be done at a later stage.
5049 Inside_A_Generic := Generic_Flags.Table (Generic_Flags.Last);
5050 Generic_Flags.Decrement_Last;
5052 Expander_Mode_Restore;
5055 ----------------------
5056 -- Find_Actual_Type --
5057 ----------------------
5059 function Find_Actual_Type
5061 Gen_Scope : Entity_Id)
5067 if not Is_Child_Unit (Gen_Scope) then
5068 return Get_Instance_Of (Typ);
5070 elsif not Is_Generic_Type (Typ)
5071 or else Scope (Typ) = Gen_Scope
5073 return Get_Instance_Of (Typ);
5076 T := Current_Entity (Typ);
5077 while Present (T) loop
5078 if In_Open_Scopes (Scope (T)) then
5087 end Find_Actual_Type;
5089 ----------------------------
5090 -- Freeze_Subprogram_Body --
5091 ----------------------------
5093 procedure Freeze_Subprogram_Body
5094 (Inst_Node : Node_Id;
5096 Pack_Id : Entity_Id)
5099 Gen_Unit : constant Entity_Id := Get_Generic_Entity (Inst_Node);
5100 Par : constant Entity_Id := Scope (Gen_Unit);
5105 function Earlier (N1, N2 : Node_Id) return Boolean;
5106 -- Yields True if N1 and N2 appear in the same compilation unit,
5107 -- ignoring subunits, and if N1 is to the left of N2 in a left-to-right
5108 -- traversal of the tree for the unit.
5110 function Enclosing_Body (N : Node_Id) return Node_Id;
5111 -- Find innermost package body that encloses the given node, and which
5112 -- is not a compilation unit. Freeze nodes for the instance, or for its
5113 -- enclosing body, may be inserted after the enclosing_body of the
5116 function Package_Freeze_Node (B : Node_Id) return Node_Id;
5117 -- Find entity for given package body, and locate or create a freeze
5120 function True_Parent (N : Node_Id) return Node_Id;
5121 -- For a subunit, return parent of corresponding stub.
5127 function Earlier (N1, N2 : Node_Id) return Boolean is
5133 procedure Find_Depth (P : in out Node_Id; D : in out Integer);
5134 -- Find distance from given node to enclosing compilation unit.
5140 procedure Find_Depth (P : in out Node_Id; D : in out Integer) is
5143 and then Nkind (P) /= N_Compilation_Unit
5145 P := True_Parent (P);
5150 -- Start of procesing for Earlier
5153 Find_Depth (P1, D1);
5154 Find_Depth (P2, D2);
5164 P1 := True_Parent (P1);
5169 P2 := True_Parent (P2);
5173 -- At this point P1 and P2 are at the same distance from the root.
5174 -- We examine their parents until we find a common declarative
5175 -- list, at which point we can establish their relative placement
5176 -- by comparing their ultimate slocs. If we reach the root,
5177 -- N1 and N2 do not descend from the same declarative list (e.g.
5178 -- one is nested in the declarative part and the other is in a block
5179 -- in the statement part) and the earlier one is already frozen.
5181 while not Is_List_Member (P1)
5182 or else not Is_List_Member (P2)
5183 or else List_Containing (P1) /= List_Containing (P2)
5185 P1 := True_Parent (P1);
5186 P2 := True_Parent (P2);
5188 if Nkind (Parent (P1)) = N_Subunit then
5189 P1 := Corresponding_Stub (Parent (P1));
5192 if Nkind (Parent (P2)) = N_Subunit then
5193 P2 := Corresponding_Stub (Parent (P2));
5202 Top_Level_Location (Sloc (P1)) < Top_Level_Location (Sloc (P2));
5205 --------------------
5206 -- Enclosing_Body --
5207 --------------------
5209 function Enclosing_Body (N : Node_Id) return Node_Id is
5210 P : Node_Id := Parent (N);
5214 and then Nkind (Parent (P)) /= N_Compilation_Unit
5216 if Nkind (P) = N_Package_Body then
5218 if Nkind (Parent (P)) = N_Subunit then
5219 return Corresponding_Stub (Parent (P));
5225 P := True_Parent (P);
5231 -------------------------
5232 -- Package_Freeze_Node --
5233 -------------------------
5235 function Package_Freeze_Node (B : Node_Id) return Node_Id is
5239 if Nkind (B) = N_Package_Body then
5240 Id := Corresponding_Spec (B);
5242 else pragma Assert (Nkind (B) = N_Package_Body_Stub);
5243 Id := Corresponding_Spec (Proper_Body (Unit (Library_Unit (B))));
5246 Ensure_Freeze_Node (Id);
5247 return Freeze_Node (Id);
5248 end Package_Freeze_Node;
5254 function True_Parent (N : Node_Id) return Node_Id is
5256 if Nkind (Parent (N)) = N_Subunit then
5257 return Parent (Corresponding_Stub (Parent (N)));
5263 -- Start of processing of Freeze_Subprogram_Body
5266 -- If the instance and the generic body appear within the same
5267 -- unit, and the instance preceeds the generic, the freeze node for
5268 -- the instance must appear after that of the generic. If the generic
5269 -- is nested within another instance I2, then current instance must
5270 -- be frozen after I2. In both cases, the freeze nodes are those of
5271 -- enclosing packages. Otherwise, the freeze node is placed at the end
5272 -- of the current declarative part.
5274 Enc_G := Enclosing_Body (Gen_Body);
5275 Enc_I := Enclosing_Body (Inst_Node);
5276 Ensure_Freeze_Node (Pack_Id);
5277 F_Node := Freeze_Node (Pack_Id);
5279 if Is_Generic_Instance (Par)
5280 and then Present (Freeze_Node (Par))
5282 In_Same_Declarative_Part (Freeze_Node (Par), Inst_Node)
5284 if ABE_Is_Certain (Get_Package_Instantiation_Node (Par)) then
5286 -- The parent was a premature instantiation. Insert freeze
5287 -- node at the end the current declarative part.
5289 Insert_After_Last_Decl (Inst_Node, F_Node);
5292 Insert_After (Freeze_Node (Par), F_Node);
5295 -- The body enclosing the instance should be frozen after the body
5296 -- that includes the generic, because the body of the instance may
5297 -- make references to entities therein. If the two are not in the
5298 -- same declarative part, or if the one enclosing the instance is
5299 -- frozen already, freeze the instance at the end of the current
5300 -- declarative part.
5302 elsif Is_Generic_Instance (Par)
5303 and then Present (Freeze_Node (Par))
5304 and then Present (Enc_I)
5306 if In_Same_Declarative_Part (Freeze_Node (Par), Enc_I)
5308 (Nkind (Enc_I) = N_Package_Body
5310 In_Same_Declarative_Part (Freeze_Node (Par), Parent (Enc_I)))
5312 -- The enclosing package may contain several instances. Rather
5313 -- than computing the earliest point at which to insert its
5314 -- freeze node, we place it at the end of the declarative part
5315 -- of the parent of the generic.
5317 Insert_After_Last_Decl
5318 (Freeze_Node (Par), Package_Freeze_Node (Enc_I));
5321 Insert_After_Last_Decl (Inst_Node, F_Node);
5323 elsif Present (Enc_G)
5324 and then Present (Enc_I)
5325 and then Enc_G /= Enc_I
5326 and then Earlier (Inst_Node, Gen_Body)
5328 if Nkind (Enc_G) = N_Package_Body then
5329 E_G_Id := Corresponding_Spec (Enc_G);
5330 else pragma Assert (Nkind (Enc_G) = N_Package_Body_Stub);
5332 Corresponding_Spec (Proper_Body (Unit (Library_Unit (Enc_G))));
5335 -- Freeze package that encloses instance, and place node after
5336 -- package that encloses generic. If enclosing package is already
5337 -- frozen we have to assume it is at the proper place. This may
5338 -- be a potential ABE that requires dynamic checking.
5340 Insert_After_Last_Decl (Enc_G, Package_Freeze_Node (Enc_I));
5342 -- Freeze enclosing subunit before instance
5344 Ensure_Freeze_Node (E_G_Id);
5346 if not Is_List_Member (Freeze_Node (E_G_Id)) then
5347 Insert_After (Enc_G, Freeze_Node (E_G_Id));
5350 Insert_After_Last_Decl (Inst_Node, F_Node);
5353 -- If none of the above, insert freeze node at the end of the
5354 -- current declarative part.
5356 Insert_After_Last_Decl (Inst_Node, F_Node);
5358 end Freeze_Subprogram_Body;
5364 function Get_Gen_Id (E : Assoc_Ptr) return Entity_Id is
5366 return Generic_Renamings.Table (E).Gen_Id;
5369 ---------------------
5370 -- Get_Instance_Of --
5371 ---------------------
5373 function Get_Instance_Of (A : Entity_Id) return Entity_Id is
5374 Res : constant Assoc_Ptr := Generic_Renamings_HTable.Get (A);
5377 if Res /= Assoc_Null then
5378 return Generic_Renamings.Table (Res).Act_Id;
5380 -- On exit, entity is not instantiated: not a generic parameter,
5381 -- or else parameter of an inner generic unit.
5385 end Get_Instance_Of;
5387 ------------------------------------
5388 -- Get_Package_Instantiation_Node --
5389 ------------------------------------
5391 function Get_Package_Instantiation_Node (A : Entity_Id) return Node_Id is
5392 Decl : Node_Id := Unit_Declaration_Node (A);
5396 -- If the instantiation is a compilation unit that does not need a
5397 -- body then the instantiation node has been rewritten as a package
5398 -- declaration for the instance, and we return the original node.
5400 -- If it is a compilation unit and the instance node has not been
5401 -- rewritten, then it is still the unit of the compilation. Finally,
5402 -- if a body is present, this is a parent of the main unit whose body
5403 -- has been compiled for inlining purposes, and the instantiation node
5404 -- has been rewritten with the instance body.
5406 -- Otherwise the instantiation node appears after the declaration.
5407 -- If the entity is a formal package, the declaration may have been
5408 -- rewritten as a generic declaration (in the case of a formal with a
5409 -- box) or left as a formal package declaration if it has actuals, and
5410 -- is found with a forward search.
5412 if Nkind (Parent (Decl)) = N_Compilation_Unit then
5413 if Nkind (Decl) = N_Package_Declaration
5414 and then Present (Corresponding_Body (Decl))
5416 Decl := Unit_Declaration_Node (Corresponding_Body (Decl));
5419 if Nkind (Original_Node (Decl)) = N_Package_Instantiation then
5420 return Original_Node (Decl);
5422 return Unit (Parent (Decl));
5425 elsif Nkind (Decl) = N_Generic_Package_Declaration
5426 and then Nkind (Original_Node (Decl)) = N_Formal_Package_Declaration
5428 return Original_Node (Decl);
5431 Inst := Next (Decl);
5432 while Nkind (Inst) /= N_Package_Instantiation
5433 and then Nkind (Inst) /= N_Formal_Package_Declaration
5440 end Get_Package_Instantiation_Node;
5442 ------------------------
5443 -- Has_Been_Exchanged --
5444 ------------------------
5446 function Has_Been_Exchanged (E : Entity_Id) return Boolean is
5447 Next : Elmt_Id := First_Elmt (Exchanged_Views);
5450 while Present (Next) loop
5451 if Full_View (Node (Next)) = E then
5459 end Has_Been_Exchanged;
5465 function Hash (F : Entity_Id) return HTable_Range is
5467 return HTable_Range (F mod HTable_Size);
5470 ------------------------
5471 -- Hide_Current_Scope --
5472 ------------------------
5474 procedure Hide_Current_Scope is
5475 C : constant Entity_Id := Current_Scope;
5479 Set_Is_Hidden_Open_Scope (C);
5480 E := First_Entity (C);
5482 while Present (E) loop
5483 if Is_Immediately_Visible (E) then
5484 Set_Is_Immediately_Visible (E, False);
5485 Append_Elmt (E, Hidden_Entities);
5491 -- Make the scope name invisible as well. This is necessary, but
5492 -- might conflict with calls to Rtsfind later on, in case the scope
5493 -- is a predefined one. There is no clean solution to this problem, so
5494 -- for now we depend on the user not redefining Standard itself in one
5495 -- of the parent units.
5497 if Is_Immediately_Visible (C)
5498 and then C /= Standard_Standard
5500 Set_Is_Immediately_Visible (C, False);
5501 Append_Elmt (C, Hidden_Entities);
5504 end Hide_Current_Scope;
5510 procedure Init_Env is
5511 Saved : Instance_Env;
5514 Saved.Ada_83 := Ada_83;
5515 Saved.Instantiated_Parent := Current_Instantiated_Parent;
5516 Saved.Exchanged_Views := Exchanged_Views;
5517 Saved.Hidden_Entities := Hidden_Entities;
5518 Saved.Current_Sem_Unit := Current_Sem_Unit;
5519 Instance_Envs.Increment_Last;
5520 Instance_Envs.Table (Instance_Envs.Last) := Saved;
5522 Exchanged_Views := New_Elmt_List;
5523 Hidden_Entities := New_Elmt_List;
5525 -- Make dummy entry for Instantiated parent. If generic unit is
5526 -- legal, this is set properly in Set_Instance_Env.
5528 Current_Instantiated_Parent :=
5529 (Current_Scope, Current_Scope, Assoc_Null);
5532 ------------------------------
5533 -- In_Same_Declarative_Part --
5534 ------------------------------
5536 function In_Same_Declarative_Part
5541 Decls : constant Node_Id := Parent (F_Node);
5542 Nod : Node_Id := Parent (Inst);
5545 while Present (Nod) loop
5549 elsif Nkind (Nod) = N_Subprogram_Body
5550 or else Nkind (Nod) = N_Package_Body
5551 or else Nkind (Nod) = N_Task_Body
5552 or else Nkind (Nod) = N_Protected_Body
5553 or else Nkind (Nod) = N_Block_Statement
5557 elsif Nkind (Nod) = N_Subunit then
5558 Nod := Corresponding_Stub (Nod);
5560 elsif Nkind (Nod) = N_Compilation_Unit then
5563 Nod := Parent (Nod);
5568 end In_Same_Declarative_Part;
5570 ---------------------
5571 -- Inherit_Context --
5572 ---------------------
5574 procedure Inherit_Context (Gen_Decl : Node_Id; Inst : Node_Id) is
5575 Current_Context : List_Id;
5576 Current_Unit : Node_Id;
5581 if Nkind (Parent (Gen_Decl)) = N_Compilation_Unit then
5583 -- The inherited context is attached to the enclosing compilation
5584 -- unit. This is either the main unit, or the declaration for the
5585 -- main unit (in case the instantation appears within the package
5586 -- declaration and the main unit is its body).
5588 Current_Unit := Parent (Inst);
5589 while Present (Current_Unit)
5590 and then Nkind (Current_Unit) /= N_Compilation_Unit
5592 Current_Unit := Parent (Current_Unit);
5595 Current_Context := Context_Items (Current_Unit);
5597 Item := First (Context_Items (Parent (Gen_Decl)));
5598 while Present (Item) loop
5599 if Nkind (Item) = N_With_Clause then
5600 New_I := New_Copy (Item);
5601 Set_Implicit_With (New_I, True);
5602 Append (New_I, Current_Context);
5608 end Inherit_Context;
5614 procedure Initialize is
5616 Generic_Renamings.Init;
5619 Generic_Renamings_HTable.Reset;
5620 Circularity_Detected := False;
5623 ----------------------------
5624 -- Insert_After_Last_Decl --
5625 ----------------------------
5627 procedure Insert_After_Last_Decl (N : Node_Id; F_Node : Node_Id) is
5628 L : List_Id := List_Containing (N);
5629 P : constant Node_Id := Parent (L);
5632 if not Is_List_Member (F_Node) then
5633 if Nkind (P) = N_Package_Specification
5634 and then L = Visible_Declarations (P)
5635 and then Present (Private_Declarations (P))
5636 and then not Is_Empty_List (Private_Declarations (P))
5638 L := Private_Declarations (P);
5641 Insert_After (Last (L), F_Node);
5643 end Insert_After_Last_Decl;
5649 procedure Install_Body
5650 (Act_Body : Node_Id;
5655 Act_Id : constant Entity_Id := Corresponding_Spec (Act_Body);
5656 Act_Unit : constant Node_Id := Unit (Cunit (Get_Source_Unit (N)));
5657 Gen_Id : constant Entity_Id := Corresponding_Spec (Gen_Body);
5658 Par : constant Entity_Id := Scope (Gen_Id);
5659 Gen_Unit : constant Node_Id :=
5660 Unit (Cunit (Get_Source_Unit (Gen_Decl)));
5661 Orig_Body : Node_Id := Gen_Body;
5663 Body_Unit : Node_Id;
5665 Must_Delay : Boolean;
5667 function Enclosing_Subp (Id : Entity_Id) return Entity_Id;
5668 -- Find subprogram (if any) that encloses instance and/or generic body.
5670 function True_Sloc (N : Node_Id) return Source_Ptr;
5671 -- If the instance is nested inside a generic unit, the Sloc of the
5672 -- instance indicates the place of the original definition, not the
5673 -- point of the current enclosing instance. Pending a better usage of
5674 -- Slocs to indicate instantiation places, we determine the place of
5675 -- origin of a node by finding the maximum sloc of any ancestor node.
5676 -- Why is this not equivalent fo Top_Level_Location ???
5678 function Enclosing_Subp (Id : Entity_Id) return Entity_Id is
5679 Scop : Entity_Id := Scope (Id);
5682 while Scop /= Standard_Standard
5683 and then not Is_Overloadable (Scop)
5685 Scop := Scope (Scop);
5691 function True_Sloc (N : Node_Id) return Source_Ptr is
5698 while Present (N1) and then N1 /= Act_Unit loop
5699 if Sloc (N1) > Res then
5709 -- Start of processing for Install_Body
5712 -- If the body is a subunit, the freeze point is the corresponding
5713 -- stub in the current compilation, not the subunit itself.
5715 if Nkind (Parent (Gen_Body)) = N_Subunit then
5716 Orig_Body := Corresponding_Stub (Parent (Gen_Body));
5718 Orig_Body := Gen_Body;
5721 Body_Unit := Unit (Cunit (Get_Source_Unit (Orig_Body)));
5723 -- If the instantiation and the generic definition appear in the
5724 -- same package declaration, this is an early instantiation.
5725 -- If they appear in the same declarative part, it is an early
5726 -- instantiation only if the generic body appears textually later,
5727 -- and the generic body is also in the main unit.
5729 -- If instance is nested within a subprogram, and the generic body is
5730 -- not, the instance is delayed because the enclosing body is. If
5731 -- instance and body are within the same scope, or the same sub-
5732 -- program body, indicate explicitly that the instance is delayed.
5735 (Gen_Unit = Act_Unit
5736 and then ((Nkind (Gen_Unit) = N_Package_Declaration)
5737 or else Nkind (Gen_Unit) = N_Generic_Package_Declaration
5738 or else (Gen_Unit = Body_Unit
5739 and then True_Sloc (N) < Sloc (Orig_Body)))
5740 and then Is_In_Main_Unit (Gen_Unit)
5741 and then (Scope (Act_Id) = Scope (Gen_Id)
5743 Enclosing_Subp (Act_Id) = Enclosing_Subp (Gen_Id)));
5745 -- If this is an early instantiation, the freeze node is placed after
5746 -- the generic body. Otherwise, if the generic appears in an instance,
5747 -- we cannot freeze the current instance until the outer one is frozen.
5748 -- This is only relevant if the current instance is nested within some
5749 -- inner scope not itself within the outer instance. If this scope is
5750 -- a package body in the same declarative part as the outer instance,
5751 -- then that body needs to be frozen after the outer instance. Finally,
5752 -- if no delay is needed, we place the freeze node at the end of the
5753 -- current declarative part.
5755 if Expander_Active then
5756 Ensure_Freeze_Node (Act_Id);
5757 F_Node := Freeze_Node (Act_Id);
5760 Insert_After (Orig_Body, F_Node);
5762 elsif Is_Generic_Instance (Par)
5763 and then Present (Freeze_Node (Par))
5764 and then Scope (Act_Id) /= Par
5766 -- Freeze instance of inner generic after instance of enclosing
5769 if In_Same_Declarative_Part (Freeze_Node (Par), N) then
5770 Insert_After (Freeze_Node (Par), F_Node);
5772 -- Freeze package enclosing instance of inner generic after
5773 -- instance of enclosing generic.
5775 elsif Nkind (Parent (N)) = N_Package_Body
5776 and then In_Same_Declarative_Part (Freeze_Node (Par), Parent (N))
5780 Enclosing : constant Entity_Id :=
5781 Corresponding_Spec (Parent (N));
5784 Insert_After_Last_Decl (N, F_Node);
5785 Ensure_Freeze_Node (Enclosing);
5787 if not Is_List_Member (Freeze_Node (Enclosing)) then
5788 Insert_After (Freeze_Node (Par), Freeze_Node (Enclosing));
5793 Insert_After_Last_Decl (N, F_Node);
5797 Insert_After_Last_Decl (N, F_Node);
5801 Set_Is_Frozen (Act_Id);
5802 Insert_Before (N, Act_Body);
5803 Mark_Rewrite_Insertion (Act_Body);
5806 --------------------
5807 -- Install_Parent --
5808 --------------------
5810 procedure Install_Parent (P : Entity_Id; In_Body : Boolean := False) is
5811 Ancestors : constant Elist_Id := New_Elmt_List;
5812 S : constant Entity_Id := Current_Scope;
5813 Inst_Par : Entity_Id;
5814 First_Par : Entity_Id;
5815 Inst_Node : Node_Id;
5816 Gen_Par : Entity_Id;
5817 First_Gen : Entity_Id;
5820 procedure Install_Formal_Packages (Par : Entity_Id);
5821 -- If any of the formals of the parent are formal packages with box,
5822 -- their formal parts are visible in the parent and thus in the child
5823 -- unit as well. Analogous to what is done in Check_Generic_Actuals
5824 -- for the unit itself.
5826 procedure Install_Noninstance_Specs (Par : Entity_Id);
5827 -- Install the scopes of noninstance parent units ending with Par.
5829 procedure Install_Spec (Par : Entity_Id);
5830 -- The child unit is within the declarative part of the parent, so
5831 -- the declarations within the parent are immediately visible.
5833 -----------------------------
5834 -- Install_Formal_Packages --
5835 -----------------------------
5837 procedure Install_Formal_Packages (Par : Entity_Id) is
5841 E := First_Entity (Par);
5843 while Present (E) loop
5845 if Ekind (E) = E_Package
5846 and then Nkind (Parent (E)) = N_Package_Renaming_Declaration
5848 -- If this is the renaming for the parent instance, done.
5850 if Renamed_Object (E) = Par then
5853 -- The visibility of a formal of an enclosing generic is
5856 elsif Denotes_Formal_Package (E) then
5859 elsif Present (Associated_Formal_Package (E))
5860 and then Box_Present (Parent (Associated_Formal_Package (E)))
5862 Check_Generic_Actuals (Renamed_Object (E), True);
5863 Set_Is_Hidden (E, False);
5869 end Install_Formal_Packages;
5871 -------------------------------
5872 -- Install_Noninstance_Specs --
5873 -------------------------------
5875 procedure Install_Noninstance_Specs (Par : Entity_Id) is
5878 and then Par /= Standard_Standard
5879 and then not In_Open_Scopes (Par)
5881 Install_Noninstance_Specs (Scope (Par));
5884 end Install_Noninstance_Specs;
5890 procedure Install_Spec (Par : Entity_Id) is
5891 Spec : constant Node_Id :=
5892 Specification (Unit_Declaration_Node (Par));
5896 Set_Is_Immediately_Visible (Par);
5897 Install_Visible_Declarations (Par);
5898 Install_Private_Declarations (Par);
5899 Set_Use (Visible_Declarations (Spec));
5900 Set_Use (Private_Declarations (Spec));
5903 -- Start of processing for Install_Parent
5906 -- We need to install the parent instance to compile the instantiation
5907 -- of the child, but the child instance must appear in the current
5908 -- scope. Given that we cannot place the parent above the current
5909 -- scope in the scope stack, we duplicate the current scope and unstack
5910 -- both after the instantiation is complete.
5912 -- If the parent is itself the instantiation of a child unit, we must
5913 -- also stack the instantiation of its parent, and so on. Each such
5914 -- ancestor is the prefix of the name in a prior instantiation.
5916 -- If this is a nested instance, the parent unit itself resolves to
5917 -- a renaming of the parent instance, whose declaration we need.
5919 -- Finally, the parent may be a generic (not an instance) when the
5920 -- child unit appears as a formal package.
5924 if Present (Renamed_Entity (Inst_Par)) then
5925 Inst_Par := Renamed_Entity (Inst_Par);
5928 First_Par := Inst_Par;
5931 Generic_Parent (Specification (Unit_Declaration_Node (Inst_Par)));
5933 First_Gen := Gen_Par;
5935 while Present (Gen_Par)
5936 and then Is_Child_Unit (Gen_Par)
5938 -- Load grandparent instance as well
5940 Inst_Node := Get_Package_Instantiation_Node (Inst_Par);
5942 if Nkind (Name (Inst_Node)) = N_Expanded_Name then
5943 Inst_Par := Entity (Prefix (Name (Inst_Node)));
5945 if Present (Renamed_Entity (Inst_Par)) then
5946 Inst_Par := Renamed_Entity (Inst_Par);
5951 (Specification (Unit_Declaration_Node (Inst_Par)));
5953 if Present (Gen_Par) then
5954 Prepend_Elmt (Inst_Par, Ancestors);
5957 -- Parent is not the name of an instantiation
5959 Install_Noninstance_Specs (Inst_Par);
5971 if Present (First_Gen) then
5972 Append_Elmt (First_Par, Ancestors);
5975 Install_Noninstance_Specs (First_Par);
5978 if not Is_Empty_Elmt_List (Ancestors) then
5979 Elmt := First_Elmt (Ancestors);
5981 while Present (Elmt) loop
5982 Install_Spec (Node (Elmt));
5983 Install_Formal_Packages (Node (Elmt));
5994 --------------------------------
5995 -- Instantiate_Formal_Package --
5996 --------------------------------
5998 function Instantiate_Formal_Package
6001 Analyzed_Formal : Node_Id)
6004 Loc : constant Source_Ptr := Sloc (Actual);
6005 Actual_Pack : Entity_Id;
6006 Formal_Pack : Entity_Id;
6007 Gen_Parent : Entity_Id;
6010 Parent_Spec : Node_Id;
6012 procedure Find_Matching_Actual
6014 Act : in out Entity_Id);
6015 -- We need to associate each formal entity in the formal package
6016 -- with the corresponding entity in the actual package. The actual
6017 -- package has been analyzed and possibly expanded, and as a result
6018 -- there is no one-to-one correspondence between the two lists (for
6019 -- example, the actual may include subtypes, itypes, and inherited
6020 -- primitive operations, interspersed among the renaming declarations
6021 -- for the actuals) . We retrieve the corresponding actual by name
6022 -- because each actual has the same name as the formal, and they do
6023 -- appear in the same order.
6025 function Formal_Entity
6027 Act_Ent : Entity_Id)
6029 -- Returns the entity associated with the given formal F. In the
6030 -- case where F is a formal package, this function will iterate
6031 -- through all of F's formals and enter map associations from the
6032 -- actuals occurring in the formal package's corresponding actual
6033 -- package (obtained via Act_Ent) to the formal package's formal
6034 -- parameters. This function is called recursively for arbitrary
6035 -- levels of formal packages.
6037 function Is_Instance_Of
6038 (Act_Spec : Entity_Id;
6039 Gen_Anc : Entity_Id)
6041 -- The actual can be an instantiation of a generic within another
6042 -- instance, in which case there is no direct link from it to the
6043 -- original generic ancestor. In that case, we recognize that the
6044 -- ultimate ancestor is the same by examining names and scopes.
6046 procedure Map_Entities (Form : Entity_Id; Act : Entity_Id);
6047 -- Within the generic part, entities in the formal package are
6048 -- visible. To validate subsequent type declarations, indicate
6049 -- the correspondence betwen the entities in the analyzed formal,
6050 -- and the entities in the actual package. There are three packages
6051 -- involved in the instantiation of a formal package: the parent
6052 -- generic P1 which appears in the generic declaration, the fake
6053 -- instantiation P2 which appears in the analyzed generic, and whose
6054 -- visible entities may be used in subsequent formals, and the actual
6055 -- P3 in the instance. To validate subsequent formals, me indicate
6056 -- that the entities in P2 are mapped into those of P3. The mapping of
6057 -- entities has to be done recursively for nested packages.
6059 --------------------------
6060 -- Find_Matching_Actual --
6061 --------------------------
6063 procedure Find_Matching_Actual
6065 Act : in out Entity_Id)
6067 Formal_Ent : Entity_Id;
6070 case Nkind (Original_Node (F)) is
6071 when N_Formal_Object_Declaration |
6072 N_Formal_Type_Declaration =>
6073 Formal_Ent := Defining_Identifier (F);
6075 while Chars (Act) /= Chars (Formal_Ent) loop
6079 when N_Formal_Subprogram_Declaration |
6080 N_Formal_Package_Declaration |
6081 N_Package_Declaration |
6082 N_Generic_Package_Declaration =>
6083 Formal_Ent := Defining_Entity (F);
6085 while Chars (Act) /= Chars (Formal_Ent) loop
6091 pragma Assert (False);
6093 end Find_Matching_Actual;
6099 function Formal_Entity
6101 Act_Ent : Entity_Id)
6104 Orig_Node : Node_Id := F;
6105 Act_Pkg : Entity_Id;
6108 case Nkind (Original_Node (F)) is
6109 when N_Formal_Object_Declaration =>
6110 return Defining_Identifier (F);
6112 when N_Formal_Type_Declaration =>
6113 return Defining_Identifier (F);
6115 when N_Formal_Subprogram_Declaration =>
6116 return Defining_Unit_Name (Specification (F));
6118 when N_Package_Declaration =>
6119 return Defining_Unit_Name (Specification (F));
6121 when N_Formal_Package_Declaration |
6122 N_Generic_Package_Declaration =>
6124 if Nkind (F) = N_Generic_Package_Declaration then
6125 Orig_Node := Original_Node (F);
6130 -- Find matching actual package, skipping over itypes and
6131 -- other entities generated when analyzing the formal. We
6132 -- know that if the instantiation is legal then there is
6133 -- a matching package for the formal.
6135 while Ekind (Act_Pkg) /= E_Package loop
6136 Act_Pkg := Next_Entity (Act_Pkg);
6140 Actual_Ent : Entity_Id := First_Entity (Act_Pkg);
6141 Formal_Node : Node_Id;
6142 Formal_Ent : Entity_Id;
6144 Gen_Decl : constant Node_Id :=
6145 Unit_Declaration_Node
6146 (Entity (Name (Orig_Node)));
6148 Formals : constant List_Id :=
6149 Generic_Formal_Declarations (Gen_Decl);
6152 if Present (Formals) then
6153 Formal_Node := First_Non_Pragma (Formals);
6155 Formal_Node := Empty;
6158 while Present (Actual_Ent)
6159 and then Present (Formal_Node)
6160 and then Actual_Ent /= First_Private_Entity (Act_Ent)
6162 -- ??? Are the following calls also needed here:
6164 -- Set_Is_Hidden (Actual_Ent, False);
6165 -- Set_Is_Potentially_Use_Visible
6166 -- (Actual_Ent, In_Use (Act_Ent));
6168 Formal_Ent := Formal_Entity (Formal_Node, Actual_Ent);
6169 if Present (Formal_Ent) then
6170 Set_Instance_Of (Formal_Ent, Actual_Ent);
6172 Next_Non_Pragma (Formal_Node);
6174 Next_Entity (Actual_Ent);
6178 return Defining_Identifier (Orig_Node);
6180 when N_Use_Package_Clause =>
6183 when N_Use_Type_Clause =>
6186 -- We return Empty for all other encountered forms of
6187 -- declarations because there are some cases of nonformal
6188 -- sorts of declaration that can show up (e.g., when array
6189 -- formals are present). Since it's not clear what kinds
6190 -- can appear among the formals, we won't raise failure here.
6198 --------------------
6199 -- Is_Instance_Of --
6200 --------------------
6202 function Is_Instance_Of
6203 (Act_Spec : Entity_Id;
6204 Gen_Anc : Entity_Id)
6207 Gen_Par : Entity_Id := Generic_Parent (Act_Spec);
6210 if No (Gen_Par) then
6213 -- Simplest case: the generic parent of the actual is the formal.
6215 elsif Gen_Par = Gen_Anc then
6218 elsif Chars (Gen_Par) /= Chars (Gen_Anc) then
6221 -- The actual may be obtained through several instantiations. Its
6222 -- scope must itself be an instance of a generic declared in the
6223 -- same scope as the formal. Any other case is detected above.
6225 elsif not Is_Generic_Instance (Scope (Gen_Par)) then
6229 return Generic_Parent (Parent (Scope (Gen_Par))) = Scope (Gen_Anc);
6237 procedure Map_Entities (Form : Entity_Id; Act : Entity_Id) is
6242 Set_Instance_Of (Form, Act);
6244 -- Traverse formal and actual package to map the corresponding
6245 -- entities. We skip over internal entities that may be generated
6246 -- during semantic analysis, and find the matching entities by
6247 -- name, given that they must appear in the same order.
6249 E1 := First_Entity (Form);
6250 E2 := First_Entity (Act);
6252 and then E1 /= First_Private_Entity (Form)
6254 if not Is_Internal (E1)
6255 and then not Is_Class_Wide_Type (E1)
6256 and then Present (Parent (E1))
6259 and then Chars (E2) /= Chars (E1)
6267 Set_Instance_Of (E1, E2);
6270 and then Is_Tagged_Type (E2)
6273 (Class_Wide_Type (E1), Class_Wide_Type (E2));
6276 if Ekind (E1) = E_Package
6277 and then No (Renamed_Object (E1))
6279 Map_Entities (E1, E2);
6288 -- Start of processing for Instantiate_Formal_Package
6293 if not Is_Entity_Name (Actual)
6294 or else Ekind (Entity (Actual)) /= E_Package
6297 ("expect package instance to instantiate formal", Actual);
6298 Abandon_Instantiation (Actual);
6299 raise Program_Error;
6302 Actual_Pack := Entity (Actual);
6303 Set_Is_Instantiated (Actual_Pack);
6305 -- The actual may be a renamed package, or an outer generic
6306 -- formal package whose instantiation is converted into a renaming.
6308 if Present (Renamed_Object (Actual_Pack)) then
6309 Actual_Pack := Renamed_Object (Actual_Pack);
6312 if Nkind (Analyzed_Formal) = N_Formal_Package_Declaration then
6313 Gen_Parent := Get_Instance_Of (Entity (Name (Analyzed_Formal)));
6314 Formal_Pack := Defining_Identifier (Analyzed_Formal);
6317 Generic_Parent (Specification (Analyzed_Formal));
6319 Defining_Unit_Name (Specification (Analyzed_Formal));
6322 if Nkind (Parent (Actual_Pack)) = N_Defining_Program_Unit_Name then
6323 Parent_Spec := Specification (Unit_Declaration_Node (Actual_Pack));
6325 Parent_Spec := Parent (Actual_Pack);
6328 if Gen_Parent = Any_Id then
6330 ("previous error in declaration of formal package", Actual);
6331 Abandon_Instantiation (Actual);
6334 Is_Instance_Of (Parent_Spec, Get_Instance_Of (Gen_Parent))
6340 ("actual parameter must be instance of&", Actual, Gen_Parent);
6341 Abandon_Instantiation (Actual);
6344 Set_Instance_Of (Defining_Identifier (Formal), Actual_Pack);
6345 Map_Entities (Formal_Pack, Actual_Pack);
6348 Make_Package_Renaming_Declaration (Loc,
6349 Defining_Unit_Name => New_Copy (Defining_Identifier (Formal)),
6350 Name => New_Reference_To (Actual_Pack, Loc));
6352 Set_Associated_Formal_Package (Defining_Unit_Name (Nod),
6353 Defining_Identifier (Formal));
6354 Decls := New_List (Nod);
6356 -- If the formal F has a box, then the generic declarations are
6357 -- visible in the generic G. In an instance of G, the corresponding
6358 -- entities in the actual for F (which are the actuals for the
6359 -- instantiation of the generic that F denotes) must also be made
6360 -- visible for analysis of the current instance. On exit from the
6361 -- current instance, those entities are made private again. If the
6362 -- actual is currently in use, these entities are also use-visible.
6364 -- The loop through the actual entities also steps through the
6365 -- formal entities and enters associations from formals to
6366 -- actuals into the renaming map. This is necessary to properly
6367 -- handle checking of actual parameter associations for later
6368 -- formals that depend on actuals declared in the formal package.
6370 if Box_Present (Formal) then
6372 Gen_Decl : constant Node_Id :=
6373 Unit_Declaration_Node (Gen_Parent);
6374 Formals : constant List_Id :=
6375 Generic_Formal_Declarations (Gen_Decl);
6376 Actual_Ent : Entity_Id;
6377 Formal_Node : Node_Id;
6378 Formal_Ent : Entity_Id;
6381 if Present (Formals) then
6382 Formal_Node := First_Non_Pragma (Formals);
6384 Formal_Node := Empty;
6387 Actual_Ent := First_Entity (Actual_Pack);
6389 while Present (Actual_Ent)
6390 and then Actual_Ent /= First_Private_Entity (Actual_Pack)
6392 Set_Is_Hidden (Actual_Ent, False);
6393 Set_Is_Potentially_Use_Visible
6394 (Actual_Ent, In_Use (Actual_Pack));
6396 if Present (Formal_Node) then
6397 Formal_Ent := Formal_Entity (Formal_Node, Actual_Ent);
6399 if Present (Formal_Ent) then
6400 Find_Matching_Actual (Formal_Node, Actual_Ent);
6401 Set_Instance_Of (Formal_Ent, Actual_Ent);
6404 Next_Non_Pragma (Formal_Node);
6407 -- No further formals to match.
6415 -- If the formal is not declared with a box, reanalyze it as
6416 -- an instantiation, to verify the matching rules of 12.7. The
6417 -- actual checks are performed after the generic associations
6422 I_Pack : constant Entity_Id :=
6423 Make_Defining_Identifier (Sloc (Actual),
6424 Chars => New_Internal_Name ('P'));
6427 Set_Is_Internal (I_Pack);
6430 Make_Package_Instantiation (Sloc (Actual),
6431 Defining_Unit_Name => I_Pack,
6432 Name => New_Occurrence_Of (Gen_Parent, Sloc (Actual)),
6433 Generic_Associations =>
6434 Generic_Associations (Formal)));
6440 end Instantiate_Formal_Package;
6442 -----------------------------------
6443 -- Instantiate_Formal_Subprogram --
6444 -----------------------------------
6446 function Instantiate_Formal_Subprogram
6449 Analyzed_Formal : Node_Id)
6452 Loc : Source_Ptr := Sloc (Instantiation_Node);
6453 Formal_Sub : constant Entity_Id :=
6454 Defining_Unit_Name (Specification (Formal));
6455 Analyzed_S : constant Entity_Id :=
6456 Defining_Unit_Name (Specification (Analyzed_Formal));
6457 Decl_Node : Node_Id;
6461 function From_Parent_Scope (Subp : Entity_Id) return Boolean;
6462 -- If the generic is a child unit, the parent has been installed
6463 -- on the scope stack, but a default subprogram cannot resolve to
6464 -- something on the parent because that parent is not really part
6465 -- of the visible context (it is there to resolve explicit local
6466 -- entities). If the default has resolved in this way, we remove
6467 -- the entity from immediate visibility and analyze the node again
6468 -- to emit an error message or find another visible candidate.
6470 procedure Valid_Actual_Subprogram (Act : Node_Id);
6471 -- Perform legality check and raise exception on failure.
6473 -----------------------
6474 -- From_Parent_Scope --
6475 -----------------------
6477 function From_Parent_Scope (Subp : Entity_Id) return Boolean is
6478 Gen_Scope : Node_Id := Scope (Analyzed_S);
6481 while Present (Gen_Scope)
6482 and then Is_Child_Unit (Gen_Scope)
6484 if Scope (Subp) = Scope (Gen_Scope) then
6488 Gen_Scope := Scope (Gen_Scope);
6492 end From_Parent_Scope;
6494 -----------------------------
6495 -- Valid_Actual_Subprogram --
6496 -----------------------------
6498 procedure Valid_Actual_Subprogram (Act : Node_Id) is
6499 Act_E : Entity_Id := Empty;
6502 if Is_Entity_Name (Act) then
6503 Act_E := Entity (Act);
6504 elsif Nkind (Act) = N_Selected_Component
6505 and then Is_Entity_Name (Selector_Name (Act))
6507 Act_E := Entity (Selector_Name (Act));
6510 if (Present (Act_E) and then Is_Overloadable (Act_E))
6511 or else Nkind (Act) = N_Attribute_Reference
6512 or else Nkind (Act) = N_Indexed_Component
6513 or else Nkind (Act) = N_Character_Literal
6514 or else Nkind (Act) = N_Explicit_Dereference
6520 ("expect subprogram or entry name in instantiation of&",
6521 Instantiation_Node, Formal_Sub);
6522 Abandon_Instantiation (Instantiation_Node);
6524 end Valid_Actual_Subprogram;
6526 -- Start of processing for Instantiate_Formal_Subprogram
6529 New_Spec := New_Copy_Tree (Specification (Formal));
6531 -- Create new entity for the actual (New_Copy_Tree does not).
6533 Set_Defining_Unit_Name
6534 (New_Spec, Make_Defining_Identifier (Loc, Chars (Formal_Sub)));
6536 -- Find entity of actual. If the actual is an attribute reference, it
6537 -- cannot be resolved here (its formal is missing) but is handled
6538 -- instead in Attribute_Renaming. If the actual is overloaded, it is
6539 -- fully resolved subsequently, when the renaming declaration for the
6540 -- formal is analyzed. If it is an explicit dereference, resolve the
6541 -- prefix but not the actual itself, to prevent interpretation as a
6544 if Present (Actual) then
6545 Loc := Sloc (Actual);
6546 Set_Sloc (New_Spec, Loc);
6548 if Nkind (Actual) = N_Operator_Symbol then
6549 Find_Direct_Name (Actual);
6551 elsif Nkind (Actual) = N_Explicit_Dereference then
6552 Analyze (Prefix (Actual));
6554 elsif Nkind (Actual) /= N_Attribute_Reference then
6558 Valid_Actual_Subprogram (Actual);
6561 elsif Present (Default_Name (Formal)) then
6562 if Nkind (Default_Name (Formal)) /= N_Attribute_Reference
6563 and then Nkind (Default_Name (Formal)) /= N_Selected_Component
6564 and then Nkind (Default_Name (Formal)) /= N_Indexed_Component
6565 and then Nkind (Default_Name (Formal)) /= N_Character_Literal
6566 and then Present (Entity (Default_Name (Formal)))
6568 Nam := New_Occurrence_Of (Entity (Default_Name (Formal)), Loc);
6570 Nam := New_Copy (Default_Name (Formal));
6571 Set_Sloc (Nam, Loc);
6574 elsif Box_Present (Formal) then
6576 -- Actual is resolved at the point of instantiation. Create
6577 -- an identifier or operator with the same name as the formal.
6579 if Nkind (Formal_Sub) = N_Defining_Operator_Symbol then
6580 Nam := Make_Operator_Symbol (Loc,
6581 Chars => Chars (Formal_Sub),
6582 Strval => No_String);
6584 Nam := Make_Identifier (Loc, Chars (Formal_Sub));
6589 ("missing actual for instantiation of &",
6590 Instantiation_Node, Formal_Sub);
6591 Abandon_Instantiation (Instantiation_Node);
6595 Make_Subprogram_Renaming_Declaration (Loc,
6596 Specification => New_Spec,
6599 -- Gather possible interpretations for the actual before analyzing the
6600 -- instance. If overloaded, it will be resolved when analyzing the
6601 -- renaming declaration.
6603 if Box_Present (Formal)
6604 and then No (Actual)
6608 if Is_Child_Unit (Scope (Analyzed_S))
6609 and then Present (Entity (Nam))
6611 if not Is_Overloaded (Nam) then
6613 if From_Parent_Scope (Entity (Nam)) then
6614 Set_Is_Immediately_Visible (Entity (Nam), False);
6615 Set_Entity (Nam, Empty);
6616 Set_Etype (Nam, Empty);
6620 Set_Is_Immediately_Visible (Entity (Nam));
6629 Get_First_Interp (Nam, I, It);
6631 while Present (It.Nam) loop
6632 if From_Parent_Scope (It.Nam) then
6636 Get_Next_Interp (I, It);
6643 -- The generic instantiation freezes the actual. This can only be
6644 -- done once the actual is resolved, in the analysis of the renaming
6645 -- declaration. To indicate that must be done, we set the corresponding
6646 -- spec of the node to point to the formal subprogram entity.
6648 Set_Corresponding_Spec (Decl_Node, Analyzed_S);
6650 -- We cannot analyze the renaming declaration, and thus find the
6651 -- actual, until the all the actuals are assembled in the instance.
6652 -- For subsequent checks of other actuals, indicate the node that
6653 -- will hold the instance of this formal.
6655 Set_Instance_Of (Analyzed_S, Nam);
6657 if Nkind (Actual) = N_Selected_Component
6658 and then Is_Task_Type (Etype (Prefix (Actual)))
6659 and then not Is_Frozen (Etype (Prefix (Actual)))
6661 -- The renaming declaration will create a body, which must appear
6662 -- outside of the instantiation, We move the renaming declaration
6663 -- out of the instance, and create an additional renaming inside,
6664 -- to prevent freezing anomalies.
6667 Anon_Id : constant Entity_Id :=
6668 Make_Defining_Identifier
6669 (Loc, New_Internal_Name ('E'));
6671 Set_Defining_Unit_Name (New_Spec, Anon_Id);
6672 Insert_Before (Instantiation_Node, Decl_Node);
6673 Analyze (Decl_Node);
6675 -- Now create renaming within the instance
6678 Make_Subprogram_Renaming_Declaration (Loc,
6679 Specification => New_Copy_Tree (New_Spec),
6680 Name => New_Occurrence_Of (Anon_Id, Loc));
6682 Set_Defining_Unit_Name (Specification (Decl_Node),
6683 Make_Defining_Identifier (Loc, Chars (Formal_Sub)));
6688 end Instantiate_Formal_Subprogram;
6690 ------------------------
6691 -- Instantiate_Object --
6692 ------------------------
6694 function Instantiate_Object
6697 Analyzed_Formal : Node_Id)
6700 Formal_Id : constant Entity_Id := Defining_Identifier (Formal);
6701 Type_Id : constant Node_Id := Subtype_Mark (Formal);
6702 Loc : constant Source_Ptr := Sloc (Actual);
6703 Act_Assoc : constant Node_Id := Parent (Actual);
6704 Orig_Ftyp : constant Entity_Id :=
6705 Etype (Defining_Identifier (Analyzed_Formal));
6706 List : constant List_Id := New_List;
6708 Decl_Node : Node_Id;
6709 Subt_Decl : Node_Id := Empty;
6712 if Get_Instance_Of (Formal_Id) /= Formal_Id then
6713 Error_Msg_N ("duplicate instantiation of generic parameter", Actual);
6716 Set_Parent (List, Parent (Actual));
6720 if Out_Present (Formal) then
6722 -- An IN OUT generic actual must be a name. The instantiation is
6723 -- a renaming declaration. The actual is the name being renamed.
6724 -- We use the actual directly, rather than a copy, because it is not
6725 -- used further in the list of actuals, and because a copy or a use
6726 -- of relocate_node is incorrect if the instance is nested within
6727 -- a generic. In order to simplify ASIS searches, the Generic_Parent
6728 -- field links the declaration to the generic association.
6732 ("missing actual for instantiation of &",
6733 Instantiation_Node, Formal_Id);
6734 Abandon_Instantiation (Instantiation_Node);
6738 Make_Object_Renaming_Declaration (Loc,
6739 Defining_Identifier => New_Copy (Formal_Id),
6740 Subtype_Mark => New_Copy_Tree (Type_Id),
6743 Set_Corresponding_Generic_Association (Decl_Node, Act_Assoc);
6745 -- The analysis of the actual may produce insert_action nodes, so
6746 -- the declaration must have a context in which to attach them.
6748 Append (Decl_Node, List);
6751 -- This check is performed here because Analyze_Object_Renaming
6752 -- will not check it when Comes_From_Source is False. Note
6753 -- though that the check for the actual being the name of an
6754 -- object will be performed in Analyze_Object_Renaming.
6756 if Is_Object_Reference (Actual)
6757 and then Is_Dependent_Component_Of_Mutable_Object (Actual)
6760 ("illegal discriminant-dependent component for in out parameter",
6764 -- The actual has to be resolved in order to check that it is
6765 -- a variable (due to cases such as F(1), where F returns
6766 -- access to an array, and for overloaded prefixes).
6769 Get_Instance_Of (Etype (Defining_Identifier (Analyzed_Formal)));
6771 if Is_Private_Type (Ftyp)
6772 and then not Is_Private_Type (Etype (Actual))
6773 and then (Base_Type (Full_View (Ftyp)) = Base_Type (Etype (Actual))
6774 or else Base_Type (Etype (Actual)) = Ftyp)
6776 -- If the actual has the type of the full view of the formal,
6777 -- or else a non-private subtype of the formal, then
6778 -- the visibility of the formal type has changed. Add to the
6779 -- actuals a subtype declaration that will force the exchange
6780 -- of views in the body of the instance as well.
6783 Make_Subtype_Declaration (Loc,
6784 Defining_Identifier =>
6785 Make_Defining_Identifier (Loc, New_Internal_Name ('P')),
6786 Subtype_Indication => New_Occurrence_Of (Ftyp, Loc));
6788 Prepend (Subt_Decl, List);
6790 Append_Elmt (Full_View (Ftyp), Exchanged_Views);
6791 Exchange_Declarations (Ftyp);
6794 Resolve (Actual, Ftyp);
6796 if not Is_Variable (Actual) or else Paren_Count (Actual) > 0 then
6798 ("actual for& must be a variable", Actual, Formal_Id);
6800 elsif Base_Type (Ftyp) /= Base_Type (Etype (Actual)) then
6802 "type of actual does not match type of&", Actual, Formal_Id);
6806 Note_Possible_Modification (Actual);
6808 -- Check for instantiation of atomic/volatile actual for
6809 -- non-atomic/volatile formal (RM C.6 (12)).
6811 if Is_Atomic_Object (Actual)
6812 and then not Is_Atomic (Orig_Ftyp)
6815 ("cannot instantiate non-atomic formal object " &
6816 "with atomic actual", Actual);
6818 elsif Is_Volatile_Object (Actual)
6819 and then not Is_Volatile (Orig_Ftyp)
6822 ("cannot instantiate non-volatile formal object " &
6823 "with volatile actual", Actual);
6829 -- The instantiation of a generic formal in-parameter
6830 -- is a constant declaration. The actual is the expression for
6831 -- that declaration.
6833 if Present (Actual) then
6835 Decl_Node := Make_Object_Declaration (Loc,
6836 Defining_Identifier => New_Copy (Formal_Id),
6837 Constant_Present => True,
6838 Object_Definition => New_Copy_Tree (Type_Id),
6839 Expression => Actual);
6841 Set_Corresponding_Generic_Association (Decl_Node, Act_Assoc);
6843 -- A generic formal object of a tagged type is defined
6844 -- to be aliased so the new constant must also be treated
6848 (Etype (Defining_Identifier (Analyzed_Formal)))
6850 Set_Aliased_Present (Decl_Node);
6853 Append (Decl_Node, List);
6855 -- No need to repeat (pre-)analysis of some expression nodes
6856 -- already handled in Pre_Analyze_Actuals.
6858 if Nkind (Actual) /= N_Allocator then
6863 Typ : constant Entity_Id :=
6865 (Etype (Defining_Identifier (Analyzed_Formal)));
6868 Freeze_Before (Instantiation_Node, Typ);
6870 -- If the actual is an aggregate, perform name resolution
6871 -- on its components (the analysis of an aggregate does not
6872 -- do it) to capture local names that may be hidden if the
6873 -- generic is a child unit.
6875 if Nkind (Actual) = N_Aggregate then
6876 Pre_Analyze_And_Resolve (Actual, Typ);
6880 elsif Present (Expression (Formal)) then
6882 -- Use default to construct declaration.
6885 Make_Object_Declaration (Sloc (Formal),
6886 Defining_Identifier => New_Copy (Formal_Id),
6887 Constant_Present => True,
6888 Object_Definition => New_Copy (Type_Id),
6889 Expression => New_Copy_Tree (Expression (Formal)));
6891 Append (Decl_Node, List);
6892 Set_Analyzed (Expression (Decl_Node), False);
6896 ("missing actual for instantiation of &",
6897 Instantiation_Node, Formal_Id);
6900 (Etype (Defining_Identifier (Analyzed_Formal)))
6902 -- Create dummy constant declaration so that instance can
6903 -- be analyzed, to minimize cascaded visibility errors.
6906 Make_Object_Declaration (Loc,
6907 Defining_Identifier => New_Copy (Formal_Id),
6908 Constant_Present => True,
6909 Object_Definition => New_Copy (Type_Id),
6911 Make_Attribute_Reference (Sloc (Formal_Id),
6912 Attribute_Name => Name_First,
6913 Prefix => New_Copy (Type_Id)));
6915 Append (Decl_Node, List);
6918 Abandon_Instantiation (Instantiation_Node);
6925 end Instantiate_Object;
6927 ------------------------------
6928 -- Instantiate_Package_Body --
6929 ------------------------------
6931 procedure Instantiate_Package_Body
6932 (Body_Info : Pending_Body_Info;
6933 Inlined_Body : Boolean := False)
6935 Act_Decl : constant Node_Id := Body_Info.Act_Decl;
6936 Inst_Node : constant Node_Id := Body_Info.Inst_Node;
6937 Loc : constant Source_Ptr := Sloc (Inst_Node);
6939 Gen_Id : constant Node_Id := Name (Inst_Node);
6940 Gen_Unit : constant Entity_Id := Get_Generic_Entity (Inst_Node);
6941 Gen_Decl : constant Node_Id := Unit_Declaration_Node (Gen_Unit);
6942 Act_Spec : constant Node_Id := Specification (Act_Decl);
6943 Act_Decl_Id : constant Entity_Id := Defining_Entity (Act_Spec);
6945 Act_Body_Name : Node_Id;
6947 Gen_Body_Id : Node_Id;
6949 Act_Body_Id : Entity_Id;
6951 Parent_Installed : Boolean := False;
6952 Save_Style_Check : constant Boolean := Style_Check;
6955 Gen_Body_Id := Corresponding_Body (Gen_Decl);
6957 -- The instance body may already have been processed, as the parent
6958 -- of another instance that is inlined. (Load_Parent_Of_Generic).
6960 if Present (Corresponding_Body (Instance_Spec (Inst_Node))) then
6964 Expander_Mode_Save_And_Set (Body_Info.Expander_Status);
6966 if No (Gen_Body_Id) then
6967 Load_Parent_Of_Generic (Inst_Node, Specification (Gen_Decl));
6968 Gen_Body_Id := Corresponding_Body (Gen_Decl);
6971 -- Establish global variable for sloc adjustment and for error
6974 Instantiation_Node := Inst_Node;
6976 if Present (Gen_Body_Id) then
6977 Save_Env (Gen_Unit, Act_Decl_Id);
6978 Style_Check := False;
6979 Current_Sem_Unit := Body_Info.Current_Sem_Unit;
6981 Gen_Body := Unit_Declaration_Node (Gen_Body_Id);
6983 Create_Instantiation_Source
6984 (Inst_Node, Gen_Body_Id, False, S_Adjustment);
6988 (Original_Node (Gen_Body), Empty, Instantiating => True);
6990 -- Build new name (possibly qualified) for body declaration
6992 Act_Body_Id := New_Copy (Act_Decl_Id);
6994 -- Some attributes of the spec entity are not inherited by the
6997 Set_Handler_Records (Act_Body_Id, No_List);
6999 if Nkind (Defining_Unit_Name (Act_Spec)) =
7000 N_Defining_Program_Unit_Name
7003 Make_Defining_Program_Unit_Name (Loc,
7004 Name => New_Copy_Tree (Name (Defining_Unit_Name (Act_Spec))),
7005 Defining_Identifier => Act_Body_Id);
7007 Act_Body_Name := Act_Body_Id;
7010 Set_Defining_Unit_Name (Act_Body, Act_Body_Name);
7012 Set_Corresponding_Spec (Act_Body, Act_Decl_Id);
7013 Check_Generic_Actuals (Act_Decl_Id, False);
7015 -- If it is a child unit, make the parent instance (which is an
7016 -- instance of the parent of the generic) visible. The parent
7017 -- instance is the prefix of the name of the generic unit.
7019 if Ekind (Scope (Gen_Unit)) = E_Generic_Package
7020 and then Nkind (Gen_Id) = N_Expanded_Name
7022 Install_Parent (Entity (Prefix (Gen_Id)), In_Body => True);
7023 Parent_Installed := True;
7025 elsif Is_Child_Unit (Gen_Unit) then
7026 Install_Parent (Scope (Gen_Unit), In_Body => True);
7027 Parent_Installed := True;
7030 -- If the instantiation is a library unit, and this is the main
7031 -- unit, then build the resulting compilation unit nodes for the
7032 -- instance. If this is a compilation unit but it is not the main
7033 -- unit, then it is the body of a unit in the context, that is being
7034 -- compiled because it is encloses some inlined unit or another
7035 -- generic unit being instantiated. In that case, this body is not
7036 -- part of the current compilation, and is not attached to the tree,
7037 -- but its parent must be set for analysis.
7039 if Nkind (Parent (Inst_Node)) = N_Compilation_Unit then
7041 -- Replace instance node with body of instance, and create
7042 -- new node for corresponding instance declaration.
7044 Build_Instance_Compilation_Unit_Nodes
7045 (Inst_Node, Act_Body, Act_Decl);
7046 Analyze (Inst_Node);
7048 if Parent (Inst_Node) = Cunit (Main_Unit) then
7050 -- If the instance is a child unit itself, then set the
7051 -- scope of the expanded body to be the parent of the
7052 -- instantiation (ensuring that the fully qualified name
7053 -- will be generated for the elaboration subprogram).
7055 if Nkind (Defining_Unit_Name (Act_Spec)) =
7056 N_Defining_Program_Unit_Name
7059 (Defining_Entity (Inst_Node), Scope (Act_Decl_Id));
7063 -- Case where instantiation is not a library unit
7066 -- If this is an early instantiation, i.e. appears textually
7067 -- before the corresponding body and must be elaborated first,
7068 -- indicate that the body instance is to be delayed.
7070 Install_Body (Act_Body, Inst_Node, Gen_Body, Gen_Decl);
7072 -- Now analyze the body. We turn off all checks if this is
7073 -- an internal unit, since there is no reason to have checks
7074 -- on for any predefined run-time library code. All such
7075 -- code is designed to be compiled with checks off.
7077 -- Note that we do NOT apply this criterion to children of
7078 -- GNAT (or on VMS, children of DEC). The latter units must
7079 -- suppress checks explicitly if this is needed.
7081 if Is_Predefined_File_Name
7082 (Unit_File_Name (Get_Source_Unit (Gen_Decl)))
7084 Analyze (Act_Body, Suppress => All_Checks);
7090 if not Generic_Separately_Compiled (Gen_Unit) then
7091 Inherit_Context (Gen_Body, Inst_Node);
7094 -- Remove the parent instances if they have been placed on the
7095 -- scope stack to compile the body.
7097 if Parent_Installed then
7098 Remove_Parent (In_Body => True);
7101 Restore_Private_Views (Act_Decl_Id);
7103 -- Remove the current unit from visibility if this is an instance
7104 -- that is not elaborated on the fly for inlining purposes.
7106 if not Inlined_Body then
7107 Set_Is_Immediately_Visible (Act_Decl_Id, False);
7111 Style_Check := Save_Style_Check;
7113 -- If we have no body, and the unit requires a body, then complain.
7114 -- This complaint is suppressed if we have detected other errors
7115 -- (since a common reason for missing the body is that it had errors).
7117 elsif Unit_Requires_Body (Gen_Unit) then
7118 if Serious_Errors_Detected = 0 then
7120 ("cannot find body of generic package &", Inst_Node, Gen_Unit);
7122 -- Don't attempt to perform any cleanup actions if some other
7123 -- error was aready detected, since this can cause blowups.
7129 -- Case of package that does not need a body
7132 -- If the instantiation of the declaration is a library unit,
7133 -- rewrite the original package instantiation as a package
7134 -- declaration in the compilation unit node.
7136 if Nkind (Parent (Inst_Node)) = N_Compilation_Unit then
7137 Set_Parent_Spec (Act_Decl, Parent_Spec (Inst_Node));
7138 Rewrite (Inst_Node, Act_Decl);
7140 -- Generate elaboration entity, in case spec has elaboration
7141 -- code. This cannot be done when the instance is analyzed,
7142 -- because it is not known yet whether the body exists.
7144 Set_Elaboration_Entity_Required (Act_Decl_Id, False);
7145 Build_Elaboration_Entity (Parent (Inst_Node), Act_Decl_Id);
7147 -- If the instantiation is not a library unit, then append the
7148 -- declaration to the list of implicitly generated entities.
7149 -- unless it is already a list member which means that it was
7150 -- already processed
7152 elsif not Is_List_Member (Act_Decl) then
7153 Mark_Rewrite_Insertion (Act_Decl);
7154 Insert_Before (Inst_Node, Act_Decl);
7158 Expander_Mode_Restore;
7159 end Instantiate_Package_Body;
7161 ---------------------------------
7162 -- Instantiate_Subprogram_Body --
7163 ---------------------------------
7165 procedure Instantiate_Subprogram_Body
7166 (Body_Info : Pending_Body_Info)
7168 Act_Decl : constant Node_Id := Body_Info.Act_Decl;
7169 Inst_Node : constant Node_Id := Body_Info.Inst_Node;
7170 Loc : constant Source_Ptr := Sloc (Inst_Node);
7171 Gen_Id : constant Node_Id := Name (Inst_Node);
7172 Gen_Unit : constant Entity_Id := Get_Generic_Entity (Inst_Node);
7173 Gen_Decl : constant Node_Id := Unit_Declaration_Node (Gen_Unit);
7174 Anon_Id : constant Entity_Id :=
7175 Defining_Unit_Name (Specification (Act_Decl));
7176 Pack_Id : constant Entity_Id :=
7177 Defining_Unit_Name (Parent (Act_Decl));
7180 Gen_Body_Id : Node_Id;
7182 Act_Body_Id : Entity_Id;
7183 Pack_Body : Node_Id;
7184 Prev_Formal : Entity_Id;
7186 Unit_Renaming : Node_Id;
7188 Parent_Installed : Boolean := False;
7189 Save_Style_Check : constant Boolean := Style_Check;
7192 Gen_Body_Id := Corresponding_Body (Gen_Decl);
7194 Expander_Mode_Save_And_Set (Body_Info.Expander_Status);
7196 if No (Gen_Body_Id) then
7197 Load_Parent_Of_Generic (Inst_Node, Specification (Gen_Decl));
7198 Gen_Body_Id := Corresponding_Body (Gen_Decl);
7201 Instantiation_Node := Inst_Node;
7203 if Present (Gen_Body_Id) then
7204 Gen_Body := Unit_Declaration_Node (Gen_Body_Id);
7206 if Nkind (Gen_Body) = N_Subprogram_Body_Stub then
7208 -- Either body is not present, or context is non-expanding, as
7209 -- when compiling a subunit. Mark the instance as completed.
7211 Set_Has_Completion (Anon_Id);
7215 Save_Env (Gen_Unit, Anon_Id);
7216 Style_Check := False;
7217 Current_Sem_Unit := Body_Info.Current_Sem_Unit;
7218 Create_Instantiation_Source
7226 (Original_Node (Gen_Body), Empty, Instantiating => True);
7227 Act_Body_Id := Defining_Entity (Act_Body);
7228 Set_Chars (Act_Body_Id, Chars (Anon_Id));
7229 Set_Sloc (Act_Body_Id, Sloc (Defining_Entity (Inst_Node)));
7230 Set_Corresponding_Spec (Act_Body, Anon_Id);
7231 Set_Has_Completion (Anon_Id);
7232 Check_Generic_Actuals (Pack_Id, False);
7234 -- If it is a child unit, make the parent instance (which is an
7235 -- instance of the parent of the generic) visible. The parent
7236 -- instance is the prefix of the name of the generic unit.
7238 if Ekind (Scope (Gen_Unit)) = E_Generic_Package
7239 and then Nkind (Gen_Id) = N_Expanded_Name
7241 Install_Parent (Entity (Prefix (Gen_Id)), In_Body => True);
7242 Parent_Installed := True;
7244 elsif Is_Child_Unit (Gen_Unit) then
7245 Install_Parent (Scope (Gen_Unit), In_Body => True);
7246 Parent_Installed := True;
7249 -- Inside its body, a reference to the generic unit is a reference
7250 -- to the instance. The corresponding renaming is the first
7251 -- declaration in the body.
7254 Make_Subprogram_Renaming_Declaration (Loc,
7257 Specification (Original_Node (Gen_Body)),
7259 Instantiating => True),
7260 Name => New_Occurrence_Of (Anon_Id, Loc));
7262 -- If there is a formal subprogram with the same name as the
7263 -- unit itself, do not add this renaming declaration. This is
7264 -- a temporary fix for one ACVC test. ???
7266 Prev_Formal := First_Entity (Pack_Id);
7267 while Present (Prev_Formal) loop
7268 if Chars (Prev_Formal) = Chars (Gen_Unit)
7269 and then Is_Overloadable (Prev_Formal)
7274 Next_Entity (Prev_Formal);
7277 if Present (Prev_Formal) then
7278 Decls := New_List (Act_Body);
7280 Decls := New_List (Unit_Renaming, Act_Body);
7283 -- The subprogram body is placed in the body of a dummy package
7284 -- body, whose spec contains the subprogram declaration as well
7285 -- as the renaming declarations for the generic parameters.
7287 Pack_Body := Make_Package_Body (Loc,
7288 Defining_Unit_Name => New_Copy (Pack_Id),
7289 Declarations => Decls);
7291 Set_Corresponding_Spec (Pack_Body, Pack_Id);
7293 -- If the instantiation is a library unit, then build resulting
7294 -- compilation unit nodes for the instance. The declaration of
7295 -- the enclosing package is the grandparent of the subprogram
7296 -- declaration. First replace the instantiation node as the unit
7297 -- of the corresponding compilation.
7299 if Nkind (Parent (Inst_Node)) = N_Compilation_Unit then
7300 if Parent (Inst_Node) = Cunit (Main_Unit) then
7301 Set_Unit (Parent (Inst_Node), Inst_Node);
7302 Build_Instance_Compilation_Unit_Nodes
7303 (Inst_Node, Pack_Body, Parent (Parent (Act_Decl)));
7304 Analyze (Inst_Node);
7306 Set_Parent (Pack_Body, Parent (Inst_Node));
7307 Analyze (Pack_Body);
7311 Insert_Before (Inst_Node, Pack_Body);
7312 Mark_Rewrite_Insertion (Pack_Body);
7313 Analyze (Pack_Body);
7315 if Expander_Active then
7316 Freeze_Subprogram_Body (Inst_Node, Gen_Body, Pack_Id);
7320 if not Generic_Separately_Compiled (Gen_Unit) then
7321 Inherit_Context (Gen_Body, Inst_Node);
7324 Restore_Private_Views (Pack_Id, False);
7326 if Parent_Installed then
7327 Remove_Parent (In_Body => True);
7331 Style_Check := Save_Style_Check;
7333 -- Body not found. Error was emitted already. If there were no
7334 -- previous errors, this may be an instance whose scope is a premature
7335 -- instance. In that case we must insure that the (legal) program does
7336 -- raise program error if executed. We generate a subprogram body for
7337 -- this purpose. See DEC ac30vso.
7339 elsif Serious_Errors_Detected = 0
7340 and then Nkind (Parent (Inst_Node)) /= N_Compilation_Unit
7342 if Ekind (Anon_Id) = E_Procedure then
7344 Make_Subprogram_Body (Loc,
7346 Make_Procedure_Specification (Loc,
7347 Defining_Unit_Name => New_Copy (Anon_Id),
7348 Parameter_Specifications =>
7350 (Parameter_Specifications (Parent (Anon_Id)))),
7352 Declarations => Empty_List,
7353 Handled_Statement_Sequence =>
7354 Make_Handled_Sequence_Of_Statements (Loc,
7357 Make_Raise_Program_Error (Loc,
7359 PE_Access_Before_Elaboration))));
7363 Make_Raise_Program_Error (Loc,
7364 Reason => PE_Access_Before_Elaboration);
7366 Set_Etype (Ret_Expr, (Etype (Anon_Id)));
7367 Set_Analyzed (Ret_Expr);
7370 Make_Subprogram_Body (Loc,
7372 Make_Function_Specification (Loc,
7373 Defining_Unit_Name => New_Copy (Anon_Id),
7374 Parameter_Specifications =>
7376 (Parameter_Specifications (Parent (Anon_Id))),
7378 New_Occurrence_Of (Etype (Anon_Id), Loc)),
7380 Declarations => Empty_List,
7381 Handled_Statement_Sequence =>
7382 Make_Handled_Sequence_Of_Statements (Loc,
7384 New_List (Make_Return_Statement (Loc, Ret_Expr))));
7387 Pack_Body := Make_Package_Body (Loc,
7388 Defining_Unit_Name => New_Copy (Pack_Id),
7389 Declarations => New_List (Act_Body));
7391 Insert_After (Inst_Node, Pack_Body);
7392 Set_Corresponding_Spec (Pack_Body, Pack_Id);
7393 Analyze (Pack_Body);
7396 Expander_Mode_Restore;
7397 end Instantiate_Subprogram_Body;
7399 ----------------------
7400 -- Instantiate_Type --
7401 ----------------------
7403 function Instantiate_Type
7406 Analyzed_Formal : Node_Id;
7407 Actual_Decls : List_Id)
7410 Loc : constant Source_Ptr := Sloc (Actual);
7411 Gen_T : constant Entity_Id := Defining_Identifier (Formal);
7412 A_Gen_T : constant Entity_Id := Defining_Identifier (Analyzed_Formal);
7413 Ancestor : Entity_Id := Empty;
7414 Def : constant Node_Id := Formal_Type_Definition (Formal);
7416 Decl_Node : Node_Id;
7418 procedure Validate_Array_Type_Instance;
7419 procedure Validate_Access_Subprogram_Instance;
7420 procedure Validate_Access_Type_Instance;
7421 procedure Validate_Derived_Type_Instance;
7422 procedure Validate_Private_Type_Instance;
7423 -- These procedures perform validation tests for the named case
7425 function Subtypes_Match (Gen_T, Act_T : Entity_Id) return Boolean;
7426 -- Check that base types are the same and that the subtypes match
7427 -- statically. Used in several of the above.
7429 --------------------
7430 -- Subtypes_Match --
7431 --------------------
7433 function Subtypes_Match (Gen_T, Act_T : Entity_Id) return Boolean is
7434 T : constant Entity_Id := Get_Instance_Of (Gen_T);
7437 return (Base_Type (T) = Base_Type (Act_T)
7438 -- why is the and then commented out here???
7439 -- and then Is_Constrained (T) = Is_Constrained (Act_T)
7440 and then Subtypes_Statically_Match (T, Act_T))
7442 or else (Is_Class_Wide_Type (Gen_T)
7443 and then Is_Class_Wide_Type (Act_T)
7446 Get_Instance_Of (Root_Type (Gen_T)),
7447 Root_Type (Act_T)));
7450 -----------------------------------------
7451 -- Validate_Access_Subprogram_Instance --
7452 -----------------------------------------
7454 procedure Validate_Access_Subprogram_Instance is
7456 if not Is_Access_Type (Act_T)
7457 or else Ekind (Designated_Type (Act_T)) /= E_Subprogram_Type
7460 ("expect access type in instantiation of &", Actual, Gen_T);
7461 Abandon_Instantiation (Actual);
7464 Check_Mode_Conformant
7465 (Designated_Type (Act_T),
7466 Designated_Type (A_Gen_T),
7470 if Ekind (Base_Type (Act_T)) = E_Access_Protected_Subprogram_Type then
7471 if Ekind (A_Gen_T) = E_Access_Subprogram_Type then
7473 ("protected access type not allowed for formal &",
7477 elsif Ekind (A_Gen_T) = E_Access_Protected_Subprogram_Type then
7479 ("expect protected access type for formal &",
7482 end Validate_Access_Subprogram_Instance;
7484 -----------------------------------
7485 -- Validate_Access_Type_Instance --
7486 -----------------------------------
7488 procedure Validate_Access_Type_Instance is
7489 Desig_Type : constant Entity_Id :=
7491 (Designated_Type (A_Gen_T), Scope (A_Gen_T));
7494 if not Is_Access_Type (Act_T) then
7496 ("expect access type in instantiation of &", Actual, Gen_T);
7497 Abandon_Instantiation (Actual);
7500 if Is_Access_Constant (A_Gen_T) then
7501 if not Is_Access_Constant (Act_T) then
7503 ("actual type must be access-to-constant type", Actual);
7504 Abandon_Instantiation (Actual);
7507 if Is_Access_Constant (Act_T) then
7509 ("actual type must be access-to-variable type", Actual);
7510 Abandon_Instantiation (Actual);
7512 elsif Ekind (A_Gen_T) = E_General_Access_Type
7513 and then Ekind (Base_Type (Act_T)) /= E_General_Access_Type
7515 Error_Msg_N ("actual must be general access type!", Actual);
7516 Error_Msg_NE ("add ALL to }!", Actual, Act_T);
7517 Abandon_Instantiation (Actual);
7521 -- The designated subtypes, that is to say the subtypes introduced
7522 -- by an access type declaration (and not by a subtype declaration)
7525 if not Subtypes_Match
7526 (Desig_Type, Designated_Type (Base_Type (Act_T)))
7529 ("designated type of actual does not match that of formal &",
7531 Abandon_Instantiation (Actual);
7533 elsif Is_Access_Type (Designated_Type (Act_T))
7534 and then Is_Constrained (Designated_Type (Designated_Type (Act_T)))
7536 Is_Constrained (Designated_Type (Desig_Type))
7539 ("designated type of actual does not match that of formal &",
7541 Abandon_Instantiation (Actual);
7543 end Validate_Access_Type_Instance;
7545 ----------------------------------
7546 -- Validate_Array_Type_Instance --
7547 ----------------------------------
7549 procedure Validate_Array_Type_Instance is
7554 function Formal_Dimensions return Int;
7555 -- Count number of dimensions in array type formal
7557 function Formal_Dimensions return Int is
7562 if Nkind (Def) = N_Constrained_Array_Definition then
7563 Index := First (Discrete_Subtype_Definitions (Def));
7565 Index := First (Subtype_Marks (Def));
7568 while Present (Index) loop
7574 end Formal_Dimensions;
7576 -- Start of processing for Validate_Array_Type_Instance
7579 if not Is_Array_Type (Act_T) then
7581 ("expect array type in instantiation of &", Actual, Gen_T);
7582 Abandon_Instantiation (Actual);
7584 elsif Nkind (Def) = N_Constrained_Array_Definition then
7585 if not (Is_Constrained (Act_T)) then
7587 ("expect constrained array in instantiation of &",
7589 Abandon_Instantiation (Actual);
7593 if Is_Constrained (Act_T) then
7595 ("expect unconstrained array in instantiation of &",
7597 Abandon_Instantiation (Actual);
7601 if Formal_Dimensions /= Number_Dimensions (Act_T) then
7603 ("dimensions of actual do not match formal &", Actual, Gen_T);
7604 Abandon_Instantiation (Actual);
7607 I1 := First_Index (A_Gen_T);
7608 I2 := First_Index (Act_T);
7609 for J in 1 .. Formal_Dimensions loop
7611 -- If the indices of the actual were given by a subtype_mark,
7612 -- the index was transformed into a range attribute. Retrieve
7613 -- the original type mark for checking.
7615 if Is_Entity_Name (Original_Node (I2)) then
7616 T2 := Entity (Original_Node (I2));
7621 if not Subtypes_Match
7622 (Find_Actual_Type (Etype (I1), Scope (A_Gen_T)), T2)
7625 ("index types of actual do not match those of formal &",
7627 Abandon_Instantiation (Actual);
7634 if not Subtypes_Match (
7635 Find_Actual_Type (Component_Type (A_Gen_T), Scope (A_Gen_T)),
7636 Component_Type (Act_T))
7639 ("component subtype of actual does not match that of formal &",
7641 Abandon_Instantiation (Actual);
7644 if Has_Aliased_Components (A_Gen_T)
7645 and then not Has_Aliased_Components (Act_T)
7648 ("actual must have aliased components to match formal type &",
7652 end Validate_Array_Type_Instance;
7654 ------------------------------------
7655 -- Validate_Derived_Type_Instance --
7656 ------------------------------------
7658 procedure Validate_Derived_Type_Instance is
7659 Actual_Discr : Entity_Id;
7660 Ancestor_Discr : Entity_Id;
7663 -- If the parent type in the generic declaration is itself
7664 -- a previous formal type, then it is local to the generic
7665 -- and absent from the analyzed generic definition. In that
7666 -- case the ancestor is the instance of the formal (which must
7667 -- have been instantiated previously), unless the ancestor is
7668 -- itself a formal derived type. In this latter case (which is the
7669 -- subject of Corrigendum 8652/0038 (AI-202) the ancestor of the
7670 -- formals is the ancestor of its parent. Otherwise, the analyzed
7671 -- generic carries the parent type. If the parent type is defined
7672 -- in a previous formal package, then the scope of that formal
7673 -- package is that of the generic type itself, and it has already
7674 -- been mapped into the corresponding type in the actual package.
7676 -- Common case: parent type defined outside of the generic
7678 if Is_Entity_Name (Subtype_Mark (Def))
7679 and then Present (Entity (Subtype_Mark (Def)))
7681 Ancestor := Get_Instance_Of (Entity (Subtype_Mark (Def)));
7683 -- Check whether parent is defined in a previous formal package
7686 Scope (Scope (Base_Type (Etype (A_Gen_T)))) = Scope (A_Gen_T)
7689 Get_Instance_Of (Base_Type (Etype (A_Gen_T)));
7691 -- The type may be a local derivation, or a type extension of
7692 -- a previous formal, or of a formal of a parent package.
7694 elsif Is_Derived_Type (Get_Instance_Of (A_Gen_T))
7696 Ekind (Get_Instance_Of (A_Gen_T)) = E_Record_Type_With_Private
7698 -- Check whether the parent is another derived formal type
7699 -- in the same generic unit.
7701 if Etype (A_Gen_T) /= A_Gen_T
7702 and then Is_Generic_Type (Etype (A_Gen_T))
7703 and then Scope (Etype (A_Gen_T)) = Scope (A_Gen_T)
7704 and then Etype (Etype (A_Gen_T)) /= Etype (A_Gen_T)
7706 -- Locate ancestor of parent from the subtype declaration
7707 -- created for the actual.
7713 Decl := First (Actual_Decls);
7715 while (Present (Decl)) loop
7716 if Nkind (Decl) = N_Subtype_Declaration
7717 and then Chars (Defining_Identifier (Decl)) =
7718 Chars (Etype (A_Gen_T))
7720 Ancestor := Generic_Parent_Type (Decl);
7728 pragma Assert (Present (Ancestor));
7732 Get_Instance_Of (Base_Type (Get_Instance_Of (A_Gen_T)));
7736 Ancestor := Get_Instance_Of (Etype (Base_Type (A_Gen_T)));
7739 if not Is_Ancestor (Base_Type (Ancestor), Act_T) then
7741 ("expect type derived from & in instantiation",
7742 Actual, First_Subtype (Ancestor));
7743 Abandon_Instantiation (Actual);
7746 -- Perform atomic/volatile checks (RM C.6(12))
7748 if Is_Atomic (Act_T) and then not Is_Atomic (Ancestor) then
7750 ("cannot have atomic actual type for non-atomic formal type",
7753 elsif Is_Volatile (Act_T)
7754 and then not Is_Volatile (Ancestor)
7755 and then Is_By_Reference_Type (Ancestor)
7758 ("cannot have volatile actual type for non-volatile formal type",
7762 -- It should not be necessary to check for unknown discriminants
7763 -- on Formal, but for some reason Has_Unknown_Discriminants is
7764 -- false for A_Gen_T, so Is_Indefinite_Subtype incorrectly
7765 -- returns False. This needs fixing. ???
7767 if not Is_Indefinite_Subtype (A_Gen_T)
7768 and then not Unknown_Discriminants_Present (Formal)
7769 and then Is_Indefinite_Subtype (Act_T)
7772 ("actual subtype must be constrained", Actual);
7773 Abandon_Instantiation (Actual);
7776 if not Unknown_Discriminants_Present (Formal) then
7777 if Is_Constrained (Ancestor) then
7778 if not Is_Constrained (Act_T) then
7780 ("actual subtype must be constrained", Actual);
7781 Abandon_Instantiation (Actual);
7784 -- Ancestor is unconstrained
7786 elsif Is_Constrained (Act_T) then
7787 if Ekind (Ancestor) = E_Access_Type
7788 or else Is_Composite_Type (Ancestor)
7791 ("actual subtype must be unconstrained", Actual);
7792 Abandon_Instantiation (Actual);
7795 -- A class-wide type is only allowed if the formal has
7796 -- unknown discriminants.
7798 elsif Is_Class_Wide_Type (Act_T)
7799 and then not Has_Unknown_Discriminants (Ancestor)
7802 ("actual for & cannot be a class-wide type", Actual, Gen_T);
7803 Abandon_Instantiation (Actual);
7805 -- Otherwise, the formal and actual shall have the same
7806 -- number of discriminants and each discriminant of the
7807 -- actual must correspond to a discriminant of the formal.
7809 elsif Has_Discriminants (Act_T)
7810 and then Has_Discriminants (Ancestor)
7812 Actual_Discr := First_Discriminant (Act_T);
7813 Ancestor_Discr := First_Discriminant (Ancestor);
7814 while Present (Actual_Discr)
7815 and then Present (Ancestor_Discr)
7817 if Base_Type (Act_T) /= Base_Type (Ancestor) and then
7818 not Present (Corresponding_Discriminant (Actual_Discr))
7821 ("discriminant & does not correspond " &
7822 "to ancestor discriminant", Actual, Actual_Discr);
7823 Abandon_Instantiation (Actual);
7826 Next_Discriminant (Actual_Discr);
7827 Next_Discriminant (Ancestor_Discr);
7830 if Present (Actual_Discr) or else Present (Ancestor_Discr) then
7832 ("actual for & must have same number of discriminants",
7834 Abandon_Instantiation (Actual);
7837 -- This case should be caught by the earlier check for
7838 -- for constrainedness, but the check here is added for
7841 elsif Has_Discriminants (Act_T) then
7843 ("actual for & must not have discriminants", Actual, Gen_T);
7844 Abandon_Instantiation (Actual);
7846 elsif Has_Discriminants (Ancestor) then
7848 ("actual for & must have known discriminants", Actual, Gen_T);
7849 Abandon_Instantiation (Actual);
7852 if not Subtypes_Statically_Compatible (Act_T, Ancestor) then
7854 ("constraint on actual is incompatible with formal", Actual);
7855 Abandon_Instantiation (Actual);
7858 end Validate_Derived_Type_Instance;
7860 ------------------------------------
7861 -- Validate_Private_Type_Instance --
7862 ------------------------------------
7864 procedure Validate_Private_Type_Instance is
7865 Formal_Discr : Entity_Id;
7866 Actual_Discr : Entity_Id;
7867 Formal_Subt : Entity_Id;
7870 if Is_Limited_Type (Act_T)
7871 and then not Is_Limited_Type (A_Gen_T)
7874 ("actual for non-limited & cannot be a limited type", Actual,
7876 Explain_Limited_Type (Act_T, Actual);
7877 Abandon_Instantiation (Actual);
7879 elsif Is_Indefinite_Subtype (Act_T)
7880 and then not Is_Indefinite_Subtype (A_Gen_T)
7884 ("actual for & must be a definite subtype", Actual, Gen_T);
7886 elsif not Is_Tagged_Type (Act_T)
7887 and then Is_Tagged_Type (A_Gen_T)
7890 ("actual for & must be a tagged type", Actual, Gen_T);
7892 elsif Has_Discriminants (A_Gen_T) then
7893 if not Has_Discriminants (Act_T) then
7895 ("actual for & must have discriminants", Actual, Gen_T);
7896 Abandon_Instantiation (Actual);
7898 elsif Is_Constrained (Act_T) then
7900 ("actual for & must be unconstrained", Actual, Gen_T);
7901 Abandon_Instantiation (Actual);
7904 Formal_Discr := First_Discriminant (A_Gen_T);
7905 Actual_Discr := First_Discriminant (Act_T);
7906 while Formal_Discr /= Empty loop
7907 if Actual_Discr = Empty then
7909 ("discriminants on actual do not match formal",
7911 Abandon_Instantiation (Actual);
7914 Formal_Subt := Get_Instance_Of (Etype (Formal_Discr));
7916 -- access discriminants match if designated types do.
7918 if Ekind (Base_Type (Formal_Subt)) = E_Anonymous_Access_Type
7919 and then (Ekind (Base_Type (Etype (Actual_Discr))))
7920 = E_Anonymous_Access_Type
7921 and then Get_Instance_Of (
7922 Designated_Type (Base_Type (Formal_Subt)))
7923 = Designated_Type (Base_Type (Etype (Actual_Discr)))
7927 elsif Base_Type (Formal_Subt) /=
7928 Base_Type (Etype (Actual_Discr))
7931 ("types of actual discriminants must match formal",
7933 Abandon_Instantiation (Actual);
7935 elsif not Subtypes_Statically_Match
7936 (Formal_Subt, Etype (Actual_Discr))
7940 ("subtypes of actual discriminants must match formal",
7942 Abandon_Instantiation (Actual);
7945 Next_Discriminant (Formal_Discr);
7946 Next_Discriminant (Actual_Discr);
7949 if Actual_Discr /= Empty then
7951 ("discriminants on actual do not match formal",
7953 Abandon_Instantiation (Actual);
7960 end Validate_Private_Type_Instance;
7962 -- Start of processing for Instantiate_Type
7965 if Get_Instance_Of (A_Gen_T) /= A_Gen_T then
7966 Error_Msg_N ("duplicate instantiation of generic type", Actual);
7969 elsif not Is_Entity_Name (Actual)
7970 or else not Is_Type (Entity (Actual))
7973 ("expect valid subtype mark to instantiate &", Actual, Gen_T);
7974 Abandon_Instantiation (Actual);
7977 Act_T := Entity (Actual);
7979 -- Deal with fixed/floating restrictions
7981 if Is_Floating_Point_Type (Act_T) then
7982 Check_Restriction (No_Floating_Point, Actual);
7983 elsif Is_Fixed_Point_Type (Act_T) then
7984 Check_Restriction (No_Fixed_Point, Actual);
7987 -- Deal with error of using incomplete type as generic actual
7989 if Ekind (Act_T) = E_Incomplete_Type then
7990 if No (Underlying_Type (Act_T)) then
7991 Error_Msg_N ("premature use of incomplete type", Actual);
7992 Abandon_Instantiation (Actual);
7994 Act_T := Full_View (Act_T);
7995 Set_Entity (Actual, Act_T);
7997 if Has_Private_Component (Act_T) then
7999 ("premature use of type with private component", Actual);
8003 -- Deal with error of premature use of private type as generic actual
8005 elsif Is_Private_Type (Act_T)
8006 and then Is_Private_Type (Base_Type (Act_T))
8007 and then not Is_Generic_Type (Act_T)
8008 and then not Is_Derived_Type (Act_T)
8009 and then No (Full_View (Root_Type (Act_T)))
8011 Error_Msg_N ("premature use of private type", Actual);
8013 elsif Has_Private_Component (Act_T) then
8015 ("premature use of type with private component", Actual);
8018 Set_Instance_Of (A_Gen_T, Act_T);
8020 -- If the type is generic, the class-wide type may also be used
8022 if Is_Tagged_Type (A_Gen_T)
8023 and then Is_Tagged_Type (Act_T)
8024 and then not Is_Class_Wide_Type (A_Gen_T)
8026 Set_Instance_Of (Class_Wide_Type (A_Gen_T),
8027 Class_Wide_Type (Act_T));
8030 if not Is_Abstract (A_Gen_T)
8031 and then Is_Abstract (Act_T)
8034 ("actual of non-abstract formal cannot be abstract", Actual);
8037 if Is_Scalar_Type (Gen_T) then
8038 Set_Instance_Of (Etype (A_Gen_T), Etype (Act_T));
8043 when N_Formal_Private_Type_Definition =>
8044 Validate_Private_Type_Instance;
8046 when N_Formal_Derived_Type_Definition =>
8047 Validate_Derived_Type_Instance;
8049 when N_Formal_Discrete_Type_Definition =>
8050 if not Is_Discrete_Type (Act_T) then
8052 ("expect discrete type in instantiation of&", Actual, Gen_T);
8053 Abandon_Instantiation (Actual);
8056 when N_Formal_Signed_Integer_Type_Definition =>
8057 if not Is_Signed_Integer_Type (Act_T) then
8059 ("expect signed integer type in instantiation of&",
8061 Abandon_Instantiation (Actual);
8064 when N_Formal_Modular_Type_Definition =>
8065 if not Is_Modular_Integer_Type (Act_T) then
8067 ("expect modular type in instantiation of &", Actual, Gen_T);
8068 Abandon_Instantiation (Actual);
8071 when N_Formal_Floating_Point_Definition =>
8072 if not Is_Floating_Point_Type (Act_T) then
8074 ("expect float type in instantiation of &", Actual, Gen_T);
8075 Abandon_Instantiation (Actual);
8078 when N_Formal_Ordinary_Fixed_Point_Definition =>
8079 if not Is_Ordinary_Fixed_Point_Type (Act_T) then
8081 ("expect ordinary fixed point type in instantiation of &",
8083 Abandon_Instantiation (Actual);
8086 when N_Formal_Decimal_Fixed_Point_Definition =>
8087 if not Is_Decimal_Fixed_Point_Type (Act_T) then
8089 ("expect decimal type in instantiation of &",
8091 Abandon_Instantiation (Actual);
8094 when N_Array_Type_Definition =>
8095 Validate_Array_Type_Instance;
8097 when N_Access_To_Object_Definition =>
8098 Validate_Access_Type_Instance;
8100 when N_Access_Function_Definition |
8101 N_Access_Procedure_Definition =>
8102 Validate_Access_Subprogram_Instance;
8105 raise Program_Error;
8110 Make_Subtype_Declaration (Loc,
8111 Defining_Identifier => New_Copy (Gen_T),
8112 Subtype_Indication => New_Reference_To (Act_T, Loc));
8114 if Is_Private_Type (Act_T) then
8115 Set_Has_Private_View (Subtype_Indication (Decl_Node));
8117 elsif Is_Access_Type (Act_T)
8118 and then Is_Private_Type (Designated_Type (Act_T))
8120 Set_Has_Private_View (Subtype_Indication (Decl_Node));
8123 -- Flag actual derived types so their elaboration produces the
8124 -- appropriate renamings for the primitive operations of the ancestor.
8125 -- Flag actual for formal private types as well, to determine whether
8126 -- operations in the private part may override inherited operations.
8128 if Nkind (Def) = N_Formal_Derived_Type_Definition
8129 or else Nkind (Def) = N_Formal_Private_Type_Definition
8131 Set_Generic_Parent_Type (Decl_Node, Ancestor);
8135 end Instantiate_Type;
8137 ---------------------
8138 -- Is_In_Main_Unit --
8139 ---------------------
8141 function Is_In_Main_Unit (N : Node_Id) return Boolean is
8142 Unum : constant Unit_Number_Type := Get_Source_Unit (N);
8144 Current_Unit : Node_Id;
8147 if Unum = Main_Unit then
8150 -- If the current unit is a subunit then it is either the main unit
8151 -- or is being compiled as part of the main unit.
8153 elsif Nkind (N) = N_Compilation_Unit then
8154 return Nkind (Unit (N)) = N_Subunit;
8157 Current_Unit := Parent (N);
8158 while Present (Current_Unit)
8159 and then Nkind (Current_Unit) /= N_Compilation_Unit
8161 Current_Unit := Parent (Current_Unit);
8164 -- The instantiation node is in the main unit, or else the current
8165 -- node (perhaps as the result of nested instantiations) is in the
8166 -- main unit, or in the declaration of the main unit, which in this
8167 -- last case must be a body.
8169 return Unum = Main_Unit
8170 or else Current_Unit = Cunit (Main_Unit)
8171 or else Current_Unit = Library_Unit (Cunit (Main_Unit))
8172 or else (Present (Library_Unit (Current_Unit))
8173 and then Is_In_Main_Unit (Library_Unit (Current_Unit)));
8174 end Is_In_Main_Unit;
8176 ----------------------------
8177 -- Load_Parent_Of_Generic --
8178 ----------------------------
8180 procedure Load_Parent_Of_Generic (N : Node_Id; Spec : Node_Id) is
8181 Comp_Unit : constant Node_Id := Cunit (Get_Source_Unit (Spec));
8182 Save_Style_Check : constant Boolean := Style_Check;
8183 True_Parent : Node_Id;
8184 Inst_Node : Node_Id;
8188 if not In_Same_Source_Unit (N, Spec)
8189 or else Nkind (Unit (Comp_Unit)) = N_Package_Declaration
8190 or else (Nkind (Unit (Comp_Unit)) = N_Package_Body
8191 and then not Is_In_Main_Unit (Spec))
8193 -- Find body of parent of spec, and analyze it. A special case
8194 -- arises when the parent is an instantiation, that is to say when
8195 -- we are currently instantiating a nested generic. In that case,
8196 -- there is no separate file for the body of the enclosing instance.
8197 -- Instead, the enclosing body must be instantiated as if it were
8198 -- a pending instantiation, in order to produce the body for the
8199 -- nested generic we require now. Note that in that case the
8200 -- generic may be defined in a package body, the instance defined
8201 -- in the same package body, and the original enclosing body may not
8202 -- be in the main unit.
8204 True_Parent := Parent (Spec);
8207 while Present (True_Parent)
8208 and then Nkind (True_Parent) /= N_Compilation_Unit
8210 if Nkind (True_Parent) = N_Package_Declaration
8212 Nkind (Original_Node (True_Parent)) = N_Package_Instantiation
8214 -- Parent is a compilation unit that is an instantiation.
8215 -- Instantiation node has been replaced with package decl.
8217 Inst_Node := Original_Node (True_Parent);
8220 elsif Nkind (True_Parent) = N_Package_Declaration
8221 and then Present (Generic_Parent (Specification (True_Parent)))
8222 and then Nkind (Parent (True_Parent)) /= N_Compilation_Unit
8224 -- Parent is an instantiation within another specification.
8225 -- Declaration for instance has been inserted before original
8226 -- instantiation node. A direct link would be preferable?
8228 Inst_Node := Next (True_Parent);
8230 while Present (Inst_Node)
8231 and then Nkind (Inst_Node) /= N_Package_Instantiation
8236 -- If the instance appears within a generic, and the generic
8237 -- unit is defined within a formal package of the enclosing
8238 -- generic, there is no generic body available, and none
8239 -- needed. A more precise test should be used ???
8241 if No (Inst_Node) then
8247 True_Parent := Parent (True_Parent);
8251 -- Case where we are currently instantiating a nested generic
8253 if Present (Inst_Node) then
8254 if Nkind (Parent (True_Parent)) = N_Compilation_Unit then
8256 -- Instantiation node and declaration of instantiated package
8257 -- were exchanged when only the declaration was needed.
8258 -- Restore instantiation node before proceeding with body.
8260 Set_Unit (Parent (True_Parent), Inst_Node);
8263 -- Now complete instantiation of enclosing body, if it appears
8264 -- in some other unit. If it appears in the current unit, the
8265 -- body will have been instantiated already.
8267 if No (Corresponding_Body (Instance_Spec (Inst_Node))) then
8269 -- We need to determine the expander mode to instantiate
8270 -- the enclosing body. Because the generic body we need
8271 -- may use global entities declared in the enclosing package
8272 -- (including aggregates) it is in general necessary to
8273 -- compile this body with expansion enabled. The exception
8274 -- is if we are within a generic package, in which case
8275 -- the usual generic rule applies.
8278 Exp_Status : Boolean := True;
8282 -- Loop through scopes looking for generic package
8284 Scop := Scope (Defining_Entity (Instance_Spec (Inst_Node)));
8285 while Present (Scop)
8286 and then Scop /= Standard_Standard
8288 if Ekind (Scop) = E_Generic_Package then
8289 Exp_Status := False;
8293 Scop := Scope (Scop);
8296 Instantiate_Package_Body
8297 (Pending_Body_Info'(
8298 Inst_Node, True_Parent, Exp_Status,
8299 Get_Code_Unit (Sloc (Inst_Node))));
8303 -- Case where we are not instantiating a nested generic
8306 Opt.Style_Check := False;
8307 Expander_Mode_Save_And_Set (True);
8308 Load_Needed_Body (Comp_Unit, OK);
8309 Opt.Style_Check := Save_Style_Check;
8310 Expander_Mode_Restore;
8313 and then Unit_Requires_Body (Defining_Entity (Spec))
8316 Bname : constant Unit_Name_Type :=
8317 Get_Body_Name (Get_Unit_Name (Unit (Comp_Unit)));
8320 Error_Msg_Unit_1 := Bname;
8321 Error_Msg_N ("this instantiation requires$!", N);
8323 Get_File_Name (Bname, Subunit => False);
8324 Error_Msg_N ("\but file{ was not found!", N);
8325 raise Unrecoverable_Error;
8331 -- If loading the parent of the generic caused an instantiation
8332 -- circularity, we abandon compilation at this point, because
8333 -- otherwise in some cases we get into trouble with infinite
8334 -- recursions after this point.
8336 if Circularity_Detected then
8337 raise Unrecoverable_Error;
8339 end Load_Parent_Of_Generic;
8341 -----------------------
8342 -- Move_Freeze_Nodes --
8343 -----------------------
8345 procedure Move_Freeze_Nodes
8346 (Out_Of : Entity_Id;
8351 Next_Decl : Node_Id;
8352 Next_Node : Node_Id := After;
8355 function Is_Outer_Type (T : Entity_Id) return Boolean;
8356 -- Check whether entity is declared in a scope external to that
8357 -- of the generic unit.
8363 function Is_Outer_Type (T : Entity_Id) return Boolean is
8364 Scop : Entity_Id := Scope (T);
8367 if Scope_Depth (Scop) < Scope_Depth (Out_Of) then
8371 while Scop /= Standard_Standard loop
8373 if Scop = Out_Of then
8376 Scop := Scope (Scop);
8384 -- Start of processing for Move_Freeze_Nodes
8391 -- First remove the freeze nodes that may appear before all other
8395 while Present (Decl)
8396 and then Nkind (Decl) = N_Freeze_Entity
8397 and then Is_Outer_Type (Entity (Decl))
8399 Decl := Remove_Head (L);
8400 Insert_After (Next_Node, Decl);
8401 Set_Analyzed (Decl, False);
8406 -- Next scan the list of declarations and remove each freeze node that
8407 -- appears ahead of the current node.
8409 while Present (Decl) loop
8410 while Present (Next (Decl))
8411 and then Nkind (Next (Decl)) = N_Freeze_Entity
8412 and then Is_Outer_Type (Entity (Next (Decl)))
8414 Next_Decl := Remove_Next (Decl);
8415 Insert_After (Next_Node, Next_Decl);
8416 Set_Analyzed (Next_Decl, False);
8417 Next_Node := Next_Decl;
8420 -- If the declaration is a nested package or concurrent type, then
8421 -- recurse. Nested generic packages will have been processed from the
8424 if Nkind (Decl) = N_Package_Declaration then
8425 Spec := Specification (Decl);
8427 elsif Nkind (Decl) = N_Task_Type_Declaration then
8428 Spec := Task_Definition (Decl);
8430 elsif Nkind (Decl) = N_Protected_Type_Declaration then
8431 Spec := Protected_Definition (Decl);
8437 if Present (Spec) then
8438 Move_Freeze_Nodes (Out_Of, Next_Node,
8439 Visible_Declarations (Spec));
8440 Move_Freeze_Nodes (Out_Of, Next_Node,
8441 Private_Declarations (Spec));
8446 end Move_Freeze_Nodes;
8452 function Next_Assoc (E : Assoc_Ptr) return Assoc_Ptr is
8454 return Generic_Renamings.Table (E).Next_In_HTable;
8457 ------------------------
8458 -- Preanalyze_Actuals --
8459 ------------------------
8461 procedure Pre_Analyze_Actuals (N : Node_Id) is
8464 Errs : constant Int := Serious_Errors_Detected;
8467 Assoc := First (Generic_Associations (N));
8469 while Present (Assoc) loop
8470 Act := Explicit_Generic_Actual_Parameter (Assoc);
8472 -- Within a nested instantiation, a defaulted actual is an
8473 -- empty association, so nothing to analyze. If the actual for
8474 -- a subprogram is an attribute, analyze prefix only, because
8475 -- actual is not a complete attribute reference.
8477 -- If actual is an allocator, analyze expression only. The full
8478 -- analysis can generate code, and if the instance is a compilation
8479 -- unit we have to wait until the package instance is installed to
8480 -- have a proper place to insert this code.
8482 -- String literals may be operators, but at this point we do not
8483 -- know whether the actual is a formal subprogram or a string.
8488 elsif Nkind (Act) = N_Attribute_Reference then
8489 Analyze (Prefix (Act));
8491 elsif Nkind (Act) = N_Explicit_Dereference then
8492 Analyze (Prefix (Act));
8494 elsif Nkind (Act) = N_Allocator then
8496 Expr : constant Node_Id := Expression (Act);
8499 if Nkind (Expr) = N_Subtype_Indication then
8500 Analyze (Subtype_Mark (Expr));
8501 Analyze_List (Constraints (Constraint (Expr)));
8507 elsif Nkind (Act) /= N_Operator_Symbol then
8511 if Errs /= Serious_Errors_Detected then
8512 Abandon_Instantiation (Act);
8517 end Pre_Analyze_Actuals;
8523 procedure Remove_Parent (In_Body : Boolean := False) is
8524 S : Entity_Id := Current_Scope;
8530 -- After child instantiation is complete, remove from scope stack
8531 -- the extra copy of the current scope, and then remove parent
8537 while Current_Scope /= S loop
8539 End_Package_Scope (Current_Scope);
8541 if In_Open_Scopes (P) then
8542 E := First_Entity (P);
8544 while Present (E) loop
8545 Set_Is_Immediately_Visible (E, True);
8549 if Is_Generic_Instance (Current_Scope)
8550 and then P /= Current_Scope
8552 -- We are within an instance of some sibling. Retain
8553 -- visibility of parent, for proper subsequent cleanup.
8555 Set_In_Private_Part (P);
8558 elsif not In_Open_Scopes (Scope (P)) then
8559 Set_Is_Immediately_Visible (P, False);
8563 -- Reset visibility of entities in the enclosing scope.
8565 Set_Is_Hidden_Open_Scope (Current_Scope, False);
8566 Hidden := First_Elmt (Hidden_Entities);
8568 while Present (Hidden) loop
8569 Set_Is_Immediately_Visible (Node (Hidden), True);
8574 -- Each body is analyzed separately, and there is no context
8575 -- that needs preserving from one body instance to the next,
8576 -- so remove all parent scopes that have been installed.
8578 while Present (S) loop
8579 End_Package_Scope (S);
8580 Set_Is_Immediately_Visible (S, False);
8582 exit when S = Standard_Standard;
8592 procedure Restore_Env is
8593 Saved : Instance_Env renames Instance_Envs.Table (Instance_Envs.Last);
8596 Ada_83 := Saved.Ada_83;
8598 if No (Current_Instantiated_Parent.Act_Id) then
8600 -- Restore environment after subprogram inlining
8602 Restore_Private_Views (Empty);
8605 Current_Instantiated_Parent := Saved.Instantiated_Parent;
8606 Exchanged_Views := Saved.Exchanged_Views;
8607 Hidden_Entities := Saved.Hidden_Entities;
8608 Current_Sem_Unit := Saved.Current_Sem_Unit;
8610 Instance_Envs.Decrement_Last;
8613 ---------------------------
8614 -- Restore_Private_Views --
8615 ---------------------------
8617 procedure Restore_Private_Views
8618 (Pack_Id : Entity_Id;
8619 Is_Package : Boolean := True)
8628 M := First_Elmt (Exchanged_Views);
8629 while Present (M) loop
8632 -- Subtypes of types whose views have been exchanged, and that
8633 -- are defined within the instance, were not on the list of
8634 -- Private_Dependents on entry to the instance, so they have to
8635 -- be exchanged explicitly now, in order to remain consistent with
8636 -- the view of the parent type.
8638 if Ekind (Typ) = E_Private_Type
8639 or else Ekind (Typ) = E_Limited_Private_Type
8640 or else Ekind (Typ) = E_Record_Type_With_Private
8642 Dep_Elmt := First_Elmt (Private_Dependents (Typ));
8644 while Present (Dep_Elmt) loop
8645 Dep_Typ := Node (Dep_Elmt);
8647 if Scope (Dep_Typ) = Pack_Id
8648 and then Present (Full_View (Dep_Typ))
8650 Replace_Elmt (Dep_Elmt, Full_View (Dep_Typ));
8651 Exchange_Declarations (Dep_Typ);
8654 Next_Elmt (Dep_Elmt);
8658 Exchange_Declarations (Node (M));
8662 if No (Pack_Id) then
8666 -- Make the generic formal parameters private, and make the formal
8667 -- types into subtypes of the actuals again.
8669 E := First_Entity (Pack_Id);
8671 while Present (E) loop
8672 Set_Is_Hidden (E, True);
8675 and then Nkind (Parent (E)) = N_Subtype_Declaration
8677 Set_Is_Generic_Actual_Type (E, False);
8679 -- An unusual case of aliasing: the actual may also be directly
8680 -- visible in the generic, and be private there, while it is
8681 -- fully visible in the context of the instance. The internal
8682 -- subtype is private in the instance, but has full visibility
8683 -- like its parent in the enclosing scope. This enforces the
8684 -- invariant that the privacy status of all private dependents of
8685 -- a type coincide with that of the parent type. This can only
8686 -- happen when a generic child unit is instantiated within a
8689 if Is_Private_Type (E)
8690 and then not Is_Private_Type (Etype (E))
8692 Exchange_Declarations (E);
8695 elsif Ekind (E) = E_Package then
8697 -- The end of the renaming list is the renaming of the generic
8698 -- package itself. If the instance is a subprogram, all entities
8699 -- in the corresponding package are renamings. If this entity is
8700 -- a formal package, make its own formals private as well. The
8701 -- actual in this case is itself the renaming of an instantation.
8702 -- If the entity is not a package renaming, it is the entity
8703 -- created to validate formal package actuals: ignore.
8705 -- If the actual is itself a formal package for the enclosing
8706 -- generic, or the actual for such a formal package, it remains
8707 -- visible after the current instance, and therefore nothing
8708 -- needs to be done either, except to keep it accessible.
8711 and then Renamed_Object (E) = Pack_Id
8715 elsif Nkind (Parent (E)) /= N_Package_Renaming_Declaration then
8718 elsif Denotes_Formal_Package (Renamed_Object (E)) then
8719 Set_Is_Hidden (E, False);
8723 Act_P : constant Entity_Id := Renamed_Object (E);
8727 Id := First_Entity (Act_P);
8729 and then Id /= First_Private_Entity (Act_P)
8731 Set_Is_Hidden (Id, True);
8732 Set_Is_Potentially_Use_Visible (Id, In_Use (Act_P));
8733 exit when Ekind (Id) = E_Package
8734 and then Renamed_Object (Id) = Act_P;
8745 end Restore_Private_Views;
8752 (Gen_Unit : Entity_Id;
8753 Act_Unit : Entity_Id)
8757 Set_Instance_Env (Gen_Unit, Act_Unit);
8760 ----------------------------
8761 -- Save_Global_References --
8762 ----------------------------
8764 procedure Save_Global_References (N : Node_Id) is
8765 Gen_Scope : Entity_Id;
8769 function Is_Global (E : Entity_Id) return Boolean;
8770 -- Check whether entity is defined outside of generic unit.
8771 -- Examine the scope of an entity, and the scope of the scope,
8772 -- etc, until we find either Standard, in which case the entity
8773 -- is global, or the generic unit itself, which indicates that
8774 -- the entity is local. If the entity is the generic unit itself,
8775 -- as in the case of a recursive call, or the enclosing generic unit,
8776 -- if different from the current scope, then it is local as well,
8777 -- because it will be replaced at the point of instantiation. On
8778 -- the other hand, if it is a reference to a child unit of a common
8779 -- ancestor, which appears in an instantiation, it is global because
8780 -- it is used to denote a specific compilation unit at the time the
8781 -- instantiations will be analyzed.
8783 procedure Reset_Entity (N : Node_Id);
8784 -- Save semantic information on global entity, so that it is not
8785 -- resolved again at instantiation time.
8787 procedure Save_Entity_Descendants (N : Node_Id);
8788 -- Apply Save_Global_References to the two syntactic descendants of
8789 -- non-terminal nodes that carry an Associated_Node and are processed
8790 -- through Reset_Entity. Once the global entity (if any) has been
8791 -- captured together with its type, only two syntactic descendants
8792 -- need to be traversed to complete the processing of the tree rooted
8793 -- at N. This applies to Selected_Components, Expanded_Names, and to
8794 -- Operator nodes. N can also be a character literal, identifier, or
8795 -- operator symbol node, but the call has no effect in these cases.
8797 procedure Save_Global_Defaults (N1, N2 : Node_Id);
8798 -- Default actuals in nested instances must be handled specially
8799 -- because there is no link to them from the original tree. When an
8800 -- actual subprogram is given by a default, we add an explicit generic
8801 -- association for it in the instantiation node. When we save the
8802 -- global references on the name of the instance, we recover the list
8803 -- of generic associations, and add an explicit one to the original
8804 -- generic tree, through which a global actual can be preserved.
8805 -- Similarly, if a child unit is instantiated within a sibling, in the
8806 -- context of the parent, we must preserve the identifier of the parent
8807 -- so that it can be properly resolved in a subsequent instantiation.
8809 procedure Save_Global_Descendant (D : Union_Id);
8810 -- Apply Save_Global_References recursively to the descendents of
8813 procedure Save_References (N : Node_Id);
8814 -- This is the recursive procedure that does the work, once the
8815 -- enclosing generic scope has been established.
8821 function Is_Global (E : Entity_Id) return Boolean is
8822 Se : Entity_Id := Scope (E);
8824 function Is_Instance_Node (Decl : Node_Id) return Boolean;
8825 -- Determine whether the parent node of a reference to a child unit
8826 -- denotes an instantiation or a formal package, in which case the
8827 -- reference to the child unit is global, even if it appears within
8828 -- the current scope (e.g. when the instance appears within the body
8831 function Is_Instance_Node (Decl : Node_Id) return Boolean is
8833 return (Nkind (Decl) in N_Generic_Instantiation
8835 Nkind (Original_Node (Decl)) = N_Formal_Package_Declaration);
8836 end Is_Instance_Node;
8838 -- Start of processing for Is_Global
8841 if E = Gen_Scope then
8844 elsif E = Standard_Standard then
8847 elsif Is_Child_Unit (E)
8848 and then (Is_Instance_Node (Parent (N2))
8849 or else (Nkind (Parent (N2)) = N_Expanded_Name
8850 and then N2 = Selector_Name (Parent (N2))
8851 and then Is_Instance_Node (Parent (Parent (N2)))))
8856 while Se /= Gen_Scope loop
8857 if Se = Standard_Standard then
8872 procedure Reset_Entity (N : Node_Id) is
8874 procedure Set_Global_Type (N : Node_Id; N2 : Node_Id);
8875 -- The type of N2 is global to the generic unit. Save the
8876 -- type in the generic node.
8878 function Top_Ancestor (E : Entity_Id) return Entity_Id;
8879 -- Find the ultimate ancestor of the current unit. If it is
8880 -- not a generic unit, then the name of the current unit
8881 -- in the prefix of an expanded name must be replaced with
8882 -- its generic homonym to ensure that it will be properly
8883 -- resolved in an instance.
8885 ---------------------
8886 -- Set_Global_Type --
8887 ---------------------
8889 procedure Set_Global_Type (N : Node_Id; N2 : Node_Id) is
8890 Typ : constant Entity_Id := Etype (N2);
8896 and then Has_Private_View (Entity (N))
8898 -- If the entity of N is not the associated node, this is
8899 -- a nested generic and it has an associated node as well,
8900 -- whose type is already the full view (see below). Indicate
8901 -- that the original node has a private view.
8903 Set_Has_Private_View (N);
8906 -- If not a private type, nothing else to do
8908 if not Is_Private_Type (Typ) then
8909 if Is_Array_Type (Typ)
8910 and then Is_Private_Type (Component_Type (Typ))
8912 Set_Has_Private_View (N);
8915 -- If it is a derivation of a private type in a context where
8916 -- no full view is needed, nothing to do either.
8918 elsif No (Full_View (Typ)) and then Typ /= Etype (Typ) then
8921 -- Otherwise mark the type for flipping and use the full_view
8925 Set_Has_Private_View (N);
8927 if Present (Full_View (Typ)) then
8928 Set_Etype (N2, Full_View (Typ));
8931 end Set_Global_Type;
8937 function Top_Ancestor (E : Entity_Id) return Entity_Id is
8938 Par : Entity_Id := E;
8941 while Is_Child_Unit (Par) loop
8948 -- Start of processing for Reset_Entity
8951 N2 := Get_Associated_Node (N);
8955 if Is_Global (E) then
8956 Set_Global_Type (N, N2);
8958 elsif Nkind (N) = N_Op_Concat
8959 and then Is_Generic_Type (Etype (N2))
8961 (Base_Type (Etype (Right_Opnd (N2))) = Etype (N2)
8962 or else Base_Type (Etype (Left_Opnd (N2))) = Etype (N2))
8963 and then Is_Intrinsic_Subprogram (E)
8968 -- Entity is local. Mark generic node as unresolved.
8969 -- Note that now it does not have an entity.
8971 Set_Associated_Node (N, Empty);
8972 Set_Etype (N, Empty);
8975 if (Nkind (Parent (N)) = N_Package_Instantiation
8976 or else Nkind (Parent (N)) = N_Function_Instantiation
8977 or else Nkind (Parent (N)) = N_Procedure_Instantiation)
8978 and then N = Name (Parent (N))
8980 Save_Global_Defaults (Parent (N), Parent (N2));
8983 elsif Nkind (Parent (N)) = N_Selected_Component
8984 and then Nkind (Parent (N2)) = N_Expanded_Name
8987 if Is_Global (Entity (Parent (N2))) then
8988 Change_Selected_Component_To_Expanded_Name (Parent (N));
8989 Set_Associated_Node (Parent (N), Parent (N2));
8990 Set_Global_Type (Parent (N), Parent (N2));
8991 Save_Entity_Descendants (N);
8993 -- If this is a reference to the current generic entity,
8994 -- replace by the name of the generic homonym of the current
8995 -- package. This is because in an instantiation Par.P.Q will
8996 -- not resolve to the name of the instance, whose enclosing
8997 -- scope is not necessarily Par. We use the generic homonym
8998 -- rather that the name of the generic itself, because it may
8999 -- be hidden by a local declaration.
9001 elsif In_Open_Scopes (Entity (Parent (N2)))
9003 Is_Generic_Unit (Top_Ancestor (Entity (Prefix (Parent (N2)))))
9005 if Ekind (Entity (Parent (N2))) = E_Generic_Package then
9006 Rewrite (Parent (N),
9007 Make_Identifier (Sloc (N),
9009 Chars (Generic_Homonym (Entity (Parent (N2))))));
9011 Rewrite (Parent (N),
9012 Make_Identifier (Sloc (N),
9013 Chars => Chars (Selector_Name (Parent (N2)))));
9017 if (Nkind (Parent (Parent (N))) = N_Package_Instantiation
9018 or else Nkind (Parent (Parent (N)))
9019 = N_Function_Instantiation
9020 or else Nkind (Parent (Parent (N)))
9021 = N_Procedure_Instantiation)
9022 and then Parent (N) = Name (Parent (Parent (N)))
9024 Save_Global_Defaults
9025 (Parent (Parent (N)), Parent (Parent ((N2))));
9028 -- A selected component may denote a static constant that has
9029 -- been folded. Make the same replacement in original tree.
9031 elsif Nkind (Parent (N)) = N_Selected_Component
9032 and then (Nkind (Parent (N2)) = N_Integer_Literal
9033 or else Nkind (Parent (N2)) = N_Real_Literal)
9035 Rewrite (Parent (N),
9036 New_Copy (Parent (N2)));
9037 Set_Analyzed (Parent (N), False);
9039 -- A selected component may be transformed into a parameterless
9040 -- function call. If the called entity is global, rewrite the
9041 -- node appropriately, i.e. as an extended name for the global
9044 elsif Nkind (Parent (N)) = N_Selected_Component
9045 and then Nkind (Parent (N2)) = N_Function_Call
9046 and then Is_Global (Entity (Name (Parent (N2))))
9048 Change_Selected_Component_To_Expanded_Name (Parent (N));
9049 Set_Associated_Node (Parent (N), Name (Parent (N2)));
9050 Set_Global_Type (Parent (N), Name (Parent (N2)));
9051 Save_Entity_Descendants (N);
9054 -- Entity is local. Reset in generic unit, so that node
9055 -- is resolved anew at the point of instantiation.
9057 Set_Associated_Node (N, Empty);
9058 Set_Etype (N, Empty);
9062 -----------------------------
9063 -- Save_Entity_Descendants --
9064 -----------------------------
9066 procedure Save_Entity_Descendants (N : Node_Id) is
9070 Save_Global_Descendant (Union_Id (Left_Opnd (N)));
9071 Save_Global_Descendant (Union_Id (Right_Opnd (N)));
9074 Save_Global_Descendant (Union_Id (Right_Opnd (N)));
9076 when N_Expanded_Name | N_Selected_Component =>
9077 Save_Global_Descendant (Union_Id (Prefix (N)));
9078 Save_Global_Descendant (Union_Id (Selector_Name (N)));
9080 when N_Identifier | N_Character_Literal | N_Operator_Symbol =>
9084 raise Program_Error;
9086 end Save_Entity_Descendants;
9088 --------------------------
9089 -- Save_Global_Defaults --
9090 --------------------------
9092 procedure Save_Global_Defaults (N1, N2 : Node_Id) is
9093 Loc : constant Source_Ptr := Sloc (N1);
9094 Assoc2 : constant List_Id := Generic_Associations (N2);
9095 Gen_Id : constant Entity_Id := Get_Generic_Entity (N2);
9105 Assoc1 := Generic_Associations (N1);
9107 if Present (Assoc1) then
9108 Act1 := First (Assoc1);
9111 Set_Generic_Associations (N1, New_List);
9112 Assoc1 := Generic_Associations (N1);
9115 if Present (Assoc2) then
9116 Act2 := First (Assoc2);
9121 while Present (Act1) and then Present (Act2) loop
9126 -- Find the associations added for default suprograms.
9128 if Present (Act2) then
9129 while Nkind (Act2) /= N_Generic_Association
9130 or else No (Entity (Selector_Name (Act2)))
9131 or else not Is_Overloadable (Entity (Selector_Name (Act2)))
9136 -- Add a similar association if the default is global. The
9137 -- renaming declaration for the actual has been analyzed, and
9138 -- its alias is the program it renames. Link the actual in the
9139 -- original generic tree with the node in the analyzed tree.
9141 while Present (Act2) loop
9142 Subp := Entity (Selector_Name (Act2));
9143 Def := Explicit_Generic_Actual_Parameter (Act2);
9145 -- Following test is defence against rubbish errors
9147 if No (Alias (Subp)) then
9151 -- Retrieve the resolved actual from the renaming declaration
9152 -- created for the instantiated formal.
9154 Actual := Entity (Name (Parent (Parent (Subp))));
9155 Set_Entity (Def, Actual);
9156 Set_Etype (Def, Etype (Actual));
9158 if Is_Global (Actual) then
9160 Make_Generic_Association (Loc,
9161 Selector_Name => New_Occurrence_Of (Subp, Loc),
9162 Explicit_Generic_Actual_Parameter =>
9163 New_Occurrence_Of (Actual, Loc));
9166 (Explicit_Generic_Actual_Parameter (Ndec), Def);
9168 Append (Ndec, Assoc1);
9170 -- If there are other defaults, add a dummy association
9171 -- in case there are other defaulted formals with the same
9174 elsif Present (Next (Act2)) then
9176 Make_Generic_Association (Loc,
9177 Selector_Name => New_Occurrence_Of (Subp, Loc),
9178 Explicit_Generic_Actual_Parameter => Empty);
9180 Append (Ndec, Assoc1);
9187 if Nkind (Name (N1)) = N_Identifier
9188 and then Is_Child_Unit (Gen_Id)
9189 and then Is_Global (Gen_Id)
9190 and then Is_Generic_Unit (Scope (Gen_Id))
9191 and then In_Open_Scopes (Scope (Gen_Id))
9193 -- This is an instantiation of a child unit within a sibling,
9194 -- so that the generic parent is in scope. An eventual instance
9195 -- must occur within the scope of an instance of the parent.
9196 -- Make name in instance into an expanded name, to preserve the
9197 -- identifier of the parent, so it can be resolved subsequently.
9200 Make_Expanded_Name (Loc,
9201 Chars => Chars (Gen_Id),
9202 Prefix => New_Occurrence_Of (Scope (Gen_Id), Loc),
9203 Selector_Name => New_Occurrence_Of (Gen_Id, Loc)));
9204 Set_Entity (Name (N2), Gen_Id);
9207 Make_Expanded_Name (Loc,
9208 Chars => Chars (Gen_Id),
9209 Prefix => New_Occurrence_Of (Scope (Gen_Id), Loc),
9210 Selector_Name => New_Occurrence_Of (Gen_Id, Loc)));
9212 Set_Associated_Node (Name (N1), Name (N2));
9213 Set_Associated_Node (Prefix (Name (N1)), Empty);
9215 (Selector_Name (Name (N1)), Selector_Name (Name (N2)));
9216 Set_Etype (Name (N1), Etype (Gen_Id));
9219 end Save_Global_Defaults;
9221 ----------------------------
9222 -- Save_Global_Descendant --
9223 ----------------------------
9225 procedure Save_Global_Descendant (D : Union_Id) is
9229 if D in Node_Range then
9230 if D = Union_Id (Empty) then
9233 elsif Nkind (Node_Id (D)) /= N_Compilation_Unit then
9234 Save_References (Node_Id (D));
9237 elsif D in List_Range then
9238 if D = Union_Id (No_List)
9239 or else Is_Empty_List (List_Id (D))
9244 N1 := First (List_Id (D));
9245 while Present (N1) loop
9246 Save_References (N1);
9251 -- Element list or other non-node field, nothing to do
9256 end Save_Global_Descendant;
9258 ---------------------
9259 -- Save_References --
9260 ---------------------
9262 -- This is the recursive procedure that does the work, once the
9263 -- enclosing generic scope has been established. We have to treat
9264 -- specially a number of node rewritings that are required by semantic
9265 -- processing and which change the kind of nodes in the generic copy:
9266 -- typically constant-folding, replacing an operator node by a string
9267 -- literal, or a selected component by an expanded name. In each of
9268 -- those cases, the transformation is propagated to the generic unit.
9270 procedure Save_References (N : Node_Id) is
9275 elsif Nkind (N) = N_Character_Literal
9276 or else Nkind (N) = N_Operator_Symbol
9278 if Nkind (N) = Nkind (Get_Associated_Node (N)) then
9281 elsif Nkind (N) = N_Operator_Symbol
9282 and then Nkind (Get_Associated_Node (N)) = N_String_Literal
9284 Change_Operator_Symbol_To_String_Literal (N);
9287 elsif Nkind (N) in N_Op then
9289 if Nkind (N) = Nkind (Get_Associated_Node (N)) then
9291 if Nkind (N) = N_Op_Concat then
9292 Set_Is_Component_Left_Opnd (N,
9293 Is_Component_Left_Opnd (Get_Associated_Node (N)));
9295 Set_Is_Component_Right_Opnd (N,
9296 Is_Component_Right_Opnd (Get_Associated_Node (N)));
9301 -- Node may be transformed into call to a user-defined operator
9303 N2 := Get_Associated_Node (N);
9305 if Nkind (N2) = N_Function_Call then
9306 E := Entity (Name (N2));
9309 and then Is_Global (E)
9311 Set_Etype (N, Etype (N2));
9313 Set_Associated_Node (N, Empty);
9314 Set_Etype (N, Empty);
9317 elsif Nkind (N2) = N_Integer_Literal
9318 or else Nkind (N2) = N_Real_Literal
9319 or else Nkind (N2) = N_String_Literal
9321 -- Operation was constant-folded, perform the same
9322 -- replacement in generic.
9324 Rewrite (N, New_Copy (N2));
9325 Set_Analyzed (N, False);
9327 elsif Nkind (N2) = N_Identifier
9328 and then Ekind (Entity (N2)) = E_Enumeration_Literal
9330 -- Same if call was folded into a literal, but in this
9331 -- case retain the entity to avoid spurious ambiguities
9332 -- if id is overloaded at the point of instantiation or
9335 Rewrite (N, New_Copy (N2));
9336 Set_Associated_Node (N, N2);
9337 Set_Analyzed (N, False);
9341 -- Complete the check on operands, if node has not been
9344 if Nkind (N) in N_Op then
9345 Save_Entity_Descendants (N);
9348 elsif Nkind (N) = N_Identifier then
9349 if Nkind (N) = Nkind (Get_Associated_Node (N)) then
9351 -- If this is a discriminant reference, always save it.
9352 -- It is used in the instance to find the corresponding
9353 -- discriminant positionally rather than by name.
9355 Set_Original_Discriminant
9356 (N, Original_Discriminant (Get_Associated_Node (N)));
9360 N2 := Get_Associated_Node (N);
9362 if Nkind (N2) = N_Function_Call then
9363 E := Entity (Name (N2));
9365 -- Name resolves to a call to parameterless function.
9366 -- If original entity is global, mark node as resolved.
9369 and then Is_Global (E)
9371 Set_Etype (N, Etype (N2));
9373 Set_Associated_Node (N, Empty);
9374 Set_Etype (N, Empty);
9378 Nkind (N2) = N_Integer_Literal or else
9379 Nkind (N2) = N_Real_Literal or else
9380 Nkind (N2) = N_String_Literal
9382 -- Name resolves to named number that is constant-folded,
9383 -- or to string literal from concatenation.
9384 -- Perform the same replacement in generic.
9386 Rewrite (N, New_Copy (N2));
9387 Set_Analyzed (N, False);
9389 elsif Nkind (N2) = N_Explicit_Dereference then
9391 -- An identifier is rewritten as a dereference if it is
9392 -- the prefix in a selected component, and it denotes an
9393 -- access to a composite type, or a parameterless function
9394 -- call that returns an access type.
9396 -- Check whether corresponding entity in prefix is global.
9398 if Is_Entity_Name (Prefix (N2))
9399 and then Present (Entity (Prefix (N2)))
9400 and then Is_Global (Entity (Prefix (N2)))
9403 Make_Explicit_Dereference (Sloc (N),
9404 Prefix => Make_Identifier (Sloc (N),
9405 Chars => Chars (N))));
9406 Set_Associated_Node (Prefix (N), Prefix (N2));
9408 elsif Nkind (Prefix (N2)) = N_Function_Call
9409 and then Is_Global (Entity (Name (Prefix (N2))))
9412 Make_Explicit_Dereference (Sloc (N),
9413 Prefix => Make_Function_Call (Sloc (N),
9415 Make_Identifier (Sloc (N),
9416 Chars => Chars (N)))));
9419 (Name (Prefix (N)), Name (Prefix (N2)));
9422 Set_Associated_Node (N, Empty);
9423 Set_Etype (N, Empty);
9426 -- The subtype mark of a nominally unconstrained object
9427 -- is rewritten as a subtype indication using the bounds
9428 -- of the expression. Recover the original subtype mark.
9430 elsif Nkind (N2) = N_Subtype_Indication
9431 and then Is_Entity_Name (Original_Node (N2))
9433 Set_Associated_Node (N, Original_Node (N2));
9441 elsif Nkind (N) in N_Entity then
9446 use Atree.Unchecked_Access;
9447 -- This code section is part of implementing an untyped tree
9448 -- traversal, so it needs direct access to node fields.
9451 if Nkind (N) = N_Aggregate
9453 Nkind (N) = N_Extension_Aggregate
9455 N2 := Get_Associated_Node (N);
9458 or else No (Etype (N2))
9459 or else not Is_Global (Etype (N2))
9461 Set_Associated_Node (N, Empty);
9464 Save_Global_Descendant (Field1 (N));
9465 Save_Global_Descendant (Field2 (N));
9466 Save_Global_Descendant (Field3 (N));
9467 Save_Global_Descendant (Field5 (N));
9469 -- All other cases than aggregates
9472 Save_Global_Descendant (Field1 (N));
9473 Save_Global_Descendant (Field2 (N));
9474 Save_Global_Descendant (Field3 (N));
9475 Save_Global_Descendant (Field4 (N));
9476 Save_Global_Descendant (Field5 (N));
9480 end Save_References;
9482 -- Start of processing for Save_Global_References
9485 Gen_Scope := Current_Scope;
9487 -- If the generic unit is a child unit, references to entities in
9488 -- the parent are treated as local, because they will be resolved
9489 -- anew in the context of the instance of the parent.
9491 while Is_Child_Unit (Gen_Scope)
9492 and then Ekind (Scope (Gen_Scope)) = E_Generic_Package
9494 Gen_Scope := Scope (Gen_Scope);
9497 Save_References (N);
9498 end Save_Global_References;
9500 --------------------------------------
9501 -- Set_Copied_Sloc_For_Inlined_Body --
9502 --------------------------------------
9504 procedure Set_Copied_Sloc_For_Inlined_Body (N : Node_Id; E : Entity_Id) is
9506 Create_Instantiation_Source (N, E, True, S_Adjustment);
9507 end Set_Copied_Sloc_For_Inlined_Body;
9509 ---------------------
9510 -- Set_Instance_Of --
9511 ---------------------
9513 procedure Set_Instance_Of (A : Entity_Id; B : Entity_Id) is
9515 Generic_Renamings.Table (Generic_Renamings.Last) := (A, B, Assoc_Null);
9516 Generic_Renamings_HTable.Set (Generic_Renamings.Last);
9517 Generic_Renamings.Increment_Last;
9518 end Set_Instance_Of;
9520 --------------------
9521 -- Set_Next_Assoc --
9522 --------------------
9524 procedure Set_Next_Assoc (E : Assoc_Ptr; Next : Assoc_Ptr) is
9526 Generic_Renamings.Table (E).Next_In_HTable := Next;
9533 procedure Start_Generic is
9535 -- ??? I am sure more things could be factored out in this
9536 -- routine. Should probably be done at a later stage.
9538 Generic_Flags.Increment_Last;
9539 Generic_Flags.Table (Generic_Flags.Last) := Inside_A_Generic;
9540 Inside_A_Generic := True;
9542 Expander_Mode_Save_And_Set (False);
9545 ----------------------
9546 -- Set_Instance_Env --
9547 ----------------------
9549 procedure Set_Instance_Env
9550 (Gen_Unit : Entity_Id;
9551 Act_Unit : Entity_Id)
9555 -- Regardless of the current mode, predefined units are analyzed in
9556 -- Ada95 mode, and Ada83 checks don't apply.
9558 if Is_Internal_File_Name
9559 (Fname => Unit_File_Name (Get_Source_Unit (Gen_Unit)),
9560 Renamings_Included => True) then
9564 Current_Instantiated_Parent := (Gen_Unit, Act_Unit, Assoc_Null);
9565 end Set_Instance_Env;
9571 procedure Switch_View (T : Entity_Id) is
9572 BT : constant Entity_Id := Base_Type (T);
9573 Priv_Elmt : Elmt_Id := No_Elmt;
9574 Priv_Sub : Entity_Id;
9577 -- T may be private but its base type may have been exchanged through
9578 -- some other occurrence, in which case there is nothing to switch.
9580 if not Is_Private_Type (BT) then
9584 Priv_Elmt := First_Elmt (Private_Dependents (BT));
9586 if Present (Full_View (BT)) then
9587 Append_Elmt (Full_View (BT), Exchanged_Views);
9588 Exchange_Declarations (BT);
9591 while Present (Priv_Elmt) loop
9592 Priv_Sub := (Node (Priv_Elmt));
9594 -- We avoid flipping the subtype if the Etype of its full
9595 -- view is private because this would result in a malformed
9596 -- subtype. This occurs when the Etype of the subtype full
9597 -- view is the full view of the base type (and since the
9598 -- base types were just switched, the subtype is pointing
9599 -- to the wrong view). This is currently the case for
9600 -- tagged record types, access types (maybe more?) and
9601 -- needs to be resolved. ???
9603 if Present (Full_View (Priv_Sub))
9604 and then not Is_Private_Type (Etype (Full_View (Priv_Sub)))
9606 Append_Elmt (Full_View (Priv_Sub), Exchanged_Views);
9607 Exchange_Declarations (Priv_Sub);
9610 Next_Elmt (Priv_Elmt);
9614 -----------------------------
9615 -- Valid_Default_Attribute --
9616 -----------------------------
9618 procedure Valid_Default_Attribute (Nam : Entity_Id; Def : Node_Id) is
9619 Attr_Id : constant Attribute_Id :=
9620 Get_Attribute_Id (Attribute_Name (Def));
9621 T : constant Entity_Id := Entity (Prefix (Def));
9622 Is_Fun : constant Boolean := (Ekind (Nam) = E_Function);
9635 F := First_Formal (Nam);
9636 while Present (F) loop
9642 when Attribute_Adjacent | Attribute_Ceiling | Attribute_Copy_Sign |
9643 Attribute_Floor | Attribute_Fraction | Attribute_Machine |
9644 Attribute_Model | Attribute_Remainder | Attribute_Rounding |
9645 Attribute_Unbiased_Rounding =>
9648 and then Is_Floating_Point_Type (T);
9650 when Attribute_Image | Attribute_Pred | Attribute_Succ |
9651 Attribute_Value | Attribute_Wide_Image |
9652 Attribute_Wide_Value =>
9653 OK := (Is_Fun and then Num_F = 1 and then Is_Scalar_Type (T));
9655 when Attribute_Max | Attribute_Min =>
9656 OK := (Is_Fun and then Num_F = 2 and then Is_Scalar_Type (T));
9658 when Attribute_Input =>
9659 OK := (Is_Fun and then Num_F = 1);
9661 when Attribute_Output | Attribute_Read | Attribute_Write =>
9662 OK := (not Is_Fun and then Num_F = 2);
9669 Error_Msg_N ("attribute reference has wrong profile for subprogram",
9672 end Valid_Default_Attribute;