1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
9 -- Copyright (C) 1992-2005, 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 Rident; use Rident;
44 with Restrict; use Restrict;
45 with Rtsfind; use Rtsfind;
47 with Sem_Cat; use Sem_Cat;
48 with Sem_Ch3; use Sem_Ch3;
49 with Sem_Ch6; use Sem_Ch6;
50 with Sem_Ch7; use Sem_Ch7;
51 with Sem_Ch8; use Sem_Ch8;
52 with Sem_Ch10; use Sem_Ch10;
53 with Sem_Ch13; use Sem_Ch13;
54 with Sem_Disp; use Sem_Disp;
55 with Sem_Elab; use Sem_Elab;
56 with Sem_Elim; use Sem_Elim;
57 with Sem_Eval; use Sem_Eval;
58 with Sem_Res; use Sem_Res;
59 with Sem_Type; use Sem_Type;
60 with Sem_Util; use Sem_Util;
61 with Sem_Warn; use Sem_Warn;
62 with Stand; use Stand;
63 with Sinfo; use Sinfo;
64 with Sinfo.CN; use Sinfo.CN;
65 with Sinput; use Sinput;
66 with Sinput.L; use Sinput.L;
67 with Snames; use Snames;
68 with Stringt; use Stringt;
69 with Uname; use Uname;
71 with Tbuild; use Tbuild;
72 with Uintp; use Uintp;
73 with Urealp; use Urealp;
77 package body Sem_Ch12 is
79 ----------------------------------------------------------
80 -- Implementation of Generic Analysis and Instantiation --
81 -----------------------------------------------------------
83 -- GNAT implements generics by macro expansion. No attempt is made to
84 -- share generic instantiations (for now). Analysis of a generic definition
85 -- does not perform any expansion action, but the expander must be called
86 -- on the tree for each instantiation, because the expansion may of course
87 -- depend on the generic actuals. All of this is best achieved as follows:
89 -- a) Semantic analysis of a generic unit is performed on a copy of the
90 -- tree for the generic unit. All tree modifications that follow analysis
91 -- do not affect the original tree. Links are kept between the original
92 -- tree and the copy, in order to recognize non-local references within
93 -- the generic, and propagate them to each instance (recall that name
94 -- resolution is done on the generic declaration: generics are not really
95 -- macros!). This is summarized in the following diagram:
97 -- .-----------. .----------.
98 -- | semantic |<--------------| generic |
100 -- | |==============>| |
101 -- |___________| global |__________|
112 -- b) Each instantiation copies the original tree, and inserts into it a
113 -- series of declarations that describe the mapping between generic formals
114 -- and actuals. For example, a generic In OUT parameter is an object
115 -- renaming of the corresponing actual, etc. Generic IN parameters are
116 -- constant declarations.
118 -- c) In order to give the right visibility for these renamings, we use
119 -- a different scheme for package and subprogram instantiations. For
120 -- packages, the list of renamings is inserted into the package
121 -- specification, before the visible declarations of the package. The
122 -- renamings are analyzed before any of the text of the instance, and are
123 -- thus visible at the right place. Furthermore, outside of the instance,
124 -- the generic parameters are visible and denote their corresponding
127 -- For subprograms, we create a container package to hold the renamings
128 -- and the subprogram instance itself. Analysis of the package makes the
129 -- renaming declarations visible to the subprogram. After analyzing the
130 -- package, the defining entity for the subprogram is touched-up so that
131 -- it appears declared in the current scope, and not inside the container
134 -- If the instantiation is a compilation unit, the container package is
135 -- given the same name as the subprogram instance. This ensures that
136 -- the elaboration procedure called by the binder, using the compilation
137 -- unit name, calls in fact the elaboration procedure for the package.
139 -- Not surprisingly, private types complicate this approach. By saving in
140 -- the original generic object the non-local references, we guarantee that
141 -- the proper entities are referenced at the point of instantiation.
142 -- However, for private types, this by itself does not insure that the
143 -- proper VIEW of the entity is used (the full type may be visible at the
144 -- point of generic definition, but not at instantiation, or vice-versa).
145 -- In order to reference the proper view, we special-case any reference
146 -- to private types in the generic object, by saving both views, one in
147 -- the generic and one in the semantic copy. At time of instantiation, we
148 -- check whether the two views are consistent, and exchange declarations if
149 -- necessary, in order to restore the correct visibility. Similarly, if
150 -- the instance view is private when the generic view was not, we perform
151 -- the exchange. After completing the instantiation, we restore the
152 -- current visibility. The flag Has_Private_View marks identifiers in the
153 -- the generic unit that require checking.
155 -- Visibility within nested generic units requires special handling.
156 -- Consider the following scheme:
158 -- type Global is ... -- outside of generic unit.
162 -- type Semi_Global is ... -- global to inner.
165 -- procedure inner (X1 : Global; X2 : Semi_Global);
167 -- procedure in2 is new inner (...); -- 4
170 -- package New_Outer is new Outer (...); -- 2
171 -- procedure New_Inner is new New_Outer.Inner (...); -- 3
173 -- The semantic analysis of Outer captures all occurrences of Global.
174 -- The semantic analysis of Inner (at 1) captures both occurrences of
175 -- Global and Semi_Global.
177 -- At point 2 (instantiation of Outer), we also produce a generic copy
178 -- of Inner, even though Inner is, at that point, not being instantiated.
179 -- (This is just part of the semantic analysis of New_Outer).
181 -- Critically, references to Global within Inner must be preserved, while
182 -- references to Semi_Global should not preserved, because they must now
183 -- resolve to an entity within New_Outer. To distinguish between these, we
184 -- use a global variable, Current_Instantiated_Parent, which is set when
185 -- performing a generic copy during instantiation (at 2). This variable is
186 -- used when performing a generic copy that is not an instantiation, but
187 -- that is nested within one, as the occurrence of 1 within 2. The analysis
188 -- of a nested generic only preserves references that are global to the
189 -- enclosing Current_Instantiated_Parent. We use the Scope_Depth value to
190 -- determine whether a reference is external to the given parent.
192 -- The instantiation at point 3 requires no special treatment. The method
193 -- works as well for further nestings of generic units, but of course the
194 -- variable Current_Instantiated_Parent must be stacked because nested
195 -- instantiations can occur, e.g. the occurrence of 4 within 2.
197 -- The instantiation of package and subprogram bodies is handled in a
198 -- similar manner, except that it is delayed until after semantic
199 -- analysis is complete. In this fashion complex cross-dependencies
200 -- between several package declarations and bodies containing generics
201 -- can be compiled which otherwise would diagnose spurious circularities.
203 -- For example, it is possible to compile two packages A and B that
204 -- have the following structure:
206 -- package A is package B is
207 -- generic ... generic ...
208 -- package G_A is package G_B is
211 -- package body A is package body B is
212 -- package N_B is new G_B (..) package N_A is new G_A (..)
214 -- The table Pending_Instantiations in package Inline is used to keep
215 -- track of body instantiations that are delayed in this manner. Inline
216 -- handles the actual calls to do the body instantiations. This activity
217 -- is part of Inline, since the processing occurs at the same point, and
218 -- for essentially the same reason, as the handling of inlined routines.
220 ----------------------------------------------
221 -- Detection of Instantiation Circularities --
222 ----------------------------------------------
224 -- If we have a chain of instantiations that is circular, this is a
225 -- static error which must be detected at compile time. The detection
226 -- of these circularities is carried out at the point that we insert
227 -- a generic instance spec or body. If there is a circularity, then
228 -- the analysis of the offending spec or body will eventually result
229 -- in trying to load the same unit again, and we detect this problem
230 -- as we analyze the package instantiation for the second time.
232 -- At least in some cases after we have detected the circularity, we
233 -- get into trouble if we try to keep going. The following flag is
234 -- set if a circularity is detected, and used to abandon compilation
235 -- after the messages have been posted.
237 Circularity_Detected : Boolean := False;
238 -- This should really be reset on encountering a new main unit, but in
239 -- practice we are not using multiple main units so it is not critical.
241 -----------------------
242 -- Local subprograms --
243 -----------------------
245 procedure Abandon_Instantiation (N : Node_Id);
246 pragma No_Return (Abandon_Instantiation);
247 -- Posts an error message "instantiation abandoned" at the indicated
248 -- node and then raises the exception Instantiation_Error to do it.
250 procedure Analyze_Formal_Array_Type
251 (T : in out Entity_Id;
253 -- A formal array type is treated like an array type declaration, and
254 -- invokes Array_Type_Declaration (sem_ch3) whose first parameter is
255 -- in-out, because in the case of an anonymous type the entity is
256 -- actually created in the procedure.
258 -- The following procedures treat other kinds of formal parameters
260 procedure Analyze_Formal_Derived_Type
265 -- The following subprograms create abbreviated declarations for formal
266 -- scalar types. We introduce an anonymous base of the proper class for
267 -- each of them, and define the formals as constrained first subtypes of
268 -- their bases. The bounds are expressions that are non-static in the
271 procedure Analyze_Formal_Decimal_Fixed_Point_Type
272 (T : Entity_Id; Def : Node_Id);
273 procedure Analyze_Formal_Discrete_Type (T : Entity_Id; Def : Node_Id);
274 procedure Analyze_Formal_Floating_Type (T : Entity_Id; Def : Node_Id);
275 procedure Analyze_Formal_Signed_Integer_Type (T : Entity_Id; Def : Node_Id);
276 procedure Analyze_Formal_Modular_Type (T : Entity_Id; Def : Node_Id);
277 procedure Analyze_Formal_Ordinary_Fixed_Point_Type
278 (T : Entity_Id; Def : Node_Id);
280 procedure Analyze_Formal_Private_Type
284 -- This needs comments???
286 procedure Analyze_Generic_Formal_Part (N : Node_Id);
288 procedure Analyze_Generic_Access_Type (T : Entity_Id; Def : Node_Id);
289 -- This needs comments ???
291 function Analyze_Associations
294 F_Copy : List_Id) return List_Id;
295 -- At instantiation time, build the list of associations between formals
296 -- and actuals. Each association becomes a renaming declaration for the
297 -- formal entity. F_Copy is the analyzed list of formals in the generic
298 -- copy. It is used to apply legality checks to the actuals. I_Node is the
299 -- instantiation node itself.
301 procedure Analyze_Subprogram_Instantiation
305 procedure Build_Instance_Compilation_Unit_Nodes
309 -- This procedure is used in the case where the generic instance of a
310 -- subprogram body or package body is a library unit. In this case, the
311 -- original library unit node for the generic instantiation must be
312 -- replaced by the resulting generic body, and a link made to a new
313 -- compilation unit node for the generic declaration. The argument N is
314 -- the original generic instantiation. Act_Body and Act_Decl are the body
315 -- and declaration of the instance (either package body and declaration
316 -- nodes or subprogram body and declaration nodes depending on the case).
317 -- On return, the node N has been rewritten with the actual body.
319 procedure Check_Formal_Packages (P_Id : Entity_Id);
320 -- Apply the following to all formal packages in generic associations
322 procedure Check_Formal_Package_Instance
323 (Formal_Pack : Entity_Id;
324 Actual_Pack : Entity_Id);
325 -- Verify that the actuals of the actual instance match the actuals of
326 -- the template for a formal package that is not declared with a box.
328 procedure Check_Forward_Instantiation (Decl : Node_Id);
329 -- If the generic is a local entity and the corresponding body has not
330 -- been seen yet, flag enclosing packages to indicate that it will be
331 -- elaborated after the generic body. Subprograms declared in the same
332 -- package cannot be inlined by the front-end because front-end inlining
333 -- requires a strict linear order of elaboration.
335 procedure Check_Hidden_Child_Unit
337 Gen_Unit : Entity_Id;
338 Act_Decl_Id : Entity_Id);
339 -- If the generic unit is an implicit child instance within a parent
340 -- instance, we need to make an explicit test that it is not hidden by
341 -- a child instance of the same name and parent.
343 procedure Check_Private_View (N : Node_Id);
344 -- Check whether the type of a generic entity has a different view between
345 -- the point of generic analysis and the point of instantiation. If the
346 -- view has changed, then at the point of instantiation we restore the
347 -- correct view to perform semantic analysis of the instance, and reset
348 -- the current view after instantiation. The processing is driven by the
349 -- current private status of the type of the node, and Has_Private_View,
350 -- a flag that is set at the point of generic compilation. If view and
351 -- flag are inconsistent then the type is updated appropriately.
353 procedure Check_Generic_Actuals
354 (Instance : Entity_Id;
355 Is_Formal_Box : Boolean);
356 -- Similar to previous one. Check the actuals in the instantiation,
357 -- whose views can change between the point of instantiation and the point
358 -- of instantiation of the body. In addition, mark the generic renamings
359 -- as generic actuals, so that they are not compatible with other actuals.
360 -- Recurse on an actual that is a formal package whose declaration has
363 function Contains_Instance_Of
366 N : Node_Id) return Boolean;
367 -- Inner is instantiated within the generic Outer. Check whether Inner
368 -- directly or indirectly contains an instance of Outer or of one of its
369 -- parents, in the case of a subunit. Each generic unit holds a list of
370 -- the entities instantiated within (at any depth). This procedure
371 -- determines whether the set of such lists contains a cycle, i.e. an
372 -- illegal circular instantiation.
374 function Denotes_Formal_Package
376 On_Exit : Boolean := False) return Boolean;
377 -- Returns True if E is a formal package of an enclosing generic, or
378 -- the actual for such a formal in an enclosing instantiation. If such
379 -- a package is used as a formal in an nested generic, or as an actual
380 -- in a nested instantiation, the visibility of ITS formals should not
381 -- be modified. When called from within Restore_Private_Views, the flag
382 -- On_Exit is true, to indicate that the search for a possible enclosing
383 -- instance should ignore the current one.
385 function Find_Actual_Type
387 Gen_Scope : Entity_Id) return Entity_Id;
388 -- When validating the actual types of a child instance, check whether
389 -- the formal is a formal type of the parent unit, and retrieve the current
390 -- actual for it. Typ is the entity in the analyzed formal type declaration
391 -- (component or index type of an array type) and Gen_Scope is the scope of
392 -- the analyzed formal array type.
394 function Get_Package_Instantiation_Node (A : Entity_Id) return Node_Id;
395 -- Given the entity of a unit that is an instantiation, retrieve the
396 -- original instance node. This is used when loading the instantiations
397 -- of the ancestors of a child generic that is being instantiated.
399 function In_Same_Declarative_Part
401 Inst : Node_Id) return Boolean;
402 -- True if the instantiation Inst and the given freeze_node F_Node appear
403 -- within the same declarative part, ignoring subunits, but with no inter-
404 -- vening suprograms or concurrent units. If true, the freeze node
405 -- of the instance can be placed after the freeze node of the parent,
406 -- which it itself an instance.
408 function In_Main_Context (E : Entity_Id) return Boolean;
409 -- Check whether an instantiation is in the context of the main unit.
410 -- Used to determine whether its body should be elaborated to allow
411 -- front-end inlining.
413 procedure Set_Instance_Env
414 (Gen_Unit : Entity_Id;
415 Act_Unit : Entity_Id);
416 -- Save current instance on saved environment, to be used to determine
417 -- the global status of entities in nested instances. Part of Save_Env.
418 -- called after verifying that the generic unit is legal for the instance.
420 procedure Set_Instance_Of (A : Entity_Id; B : Entity_Id);
421 -- Associate analyzed generic parameter with corresponding
422 -- instance. Used for semantic checks at instantiation time.
424 function Has_Been_Exchanged (E : Entity_Id) return Boolean;
425 -- Traverse the Exchanged_Views list to see if a type was private
426 -- and has already been flipped during this phase of instantiation.
428 procedure Hide_Current_Scope;
429 -- When compiling a generic child unit, the parent context must be
430 -- present, but the instance and all entities that may be generated
431 -- must be inserted in the current scope. We leave the current scope
432 -- on the stack, but make its entities invisible to avoid visibility
433 -- problems. This is reversed at the end of instantiations. This is
434 -- not done for the instantiation of the bodies, which only require the
435 -- instances of the generic parents to be in scope.
437 procedure Install_Body
442 -- If the instantiation happens textually before the body of the generic,
443 -- the instantiation of the body must be analyzed after the generic body,
444 -- and not at the point of instantiation. Such early instantiations can
445 -- happen if the generic and the instance appear in a package declaration
446 -- because the generic body can only appear in the corresponding package
447 -- body. Early instantiations can also appear if generic, instance and
448 -- body are all in the declarative part of a subprogram or entry. Entities
449 -- of packages that are early instantiations are delayed, and their freeze
450 -- node appears after the generic body.
452 procedure Insert_After_Last_Decl (N : Node_Id; F_Node : Node_Id);
453 -- Insert freeze node at the end of the declarative part that includes the
454 -- instance node N. If N is in the visible part of an enclosing package
455 -- declaration, the freeze node has to be inserted at the end of the
456 -- private declarations, if any.
458 procedure Freeze_Subprogram_Body
459 (Inst_Node : Node_Id;
461 Pack_Id : Entity_Id);
462 -- The generic body may appear textually after the instance, including
463 -- in the proper body of a stub, or within a different package instance.
464 -- Given that the instance can only be elaborated after the generic, we
465 -- place freeze_nodes for the instance and/or for packages that may enclose
466 -- the instance and the generic, so that the back-end can establish the
467 -- proper order of elaboration.
470 -- Establish environment for subsequent instantiation. Separated from
471 -- Save_Env because data-structures for visibility handling must be
472 -- initialized before call to Check_Generic_Child_Unit.
474 procedure Install_Parent (P : Entity_Id; In_Body : Boolean := False);
475 -- When compiling an instance of a child unit the parent (which is
476 -- itself an instance) is an enclosing scope that must be made
477 -- immediately visible. This procedure is also used to install the non-
478 -- generic parent of a generic child unit when compiling its body, so
479 -- that full views of types in the parent are made visible.
481 procedure Remove_Parent (In_Body : Boolean := False);
482 -- Reverse effect after instantiation of child is complete
484 procedure Inline_Instance_Body
486 Gen_Unit : Entity_Id;
488 -- If front-end inlining is requested, instantiate the package body,
489 -- and preserve the visibility of its compilation unit, to insure
490 -- that successive instantiations succeed.
492 -- The functions Instantiate_XXX perform various legality checks and build
493 -- the declarations for instantiated generic parameters. In all of these
494 -- Formal is the entity in the generic unit, Actual is the entity of
495 -- expression in the generic associations, and Analyzed_Formal is the
496 -- formal in the generic copy, which contains the semantic information to
497 -- be used to validate the actual.
499 function Instantiate_Object
502 Analyzed_Formal : Node_Id) return List_Id;
504 function Instantiate_Type
507 Analyzed_Formal : Node_Id;
508 Actual_Decls : List_Id) return Node_Id;
510 function Instantiate_Formal_Subprogram
513 Analyzed_Formal : Node_Id) return Node_Id;
515 function Instantiate_Formal_Package
518 Analyzed_Formal : Node_Id) return List_Id;
519 -- If the formal package is declared with a box, special visibility rules
520 -- apply to its formals: they are in the visible part of the package. This
521 -- is true in the declarative region of the formal package, that is to say
522 -- in the enclosing generic or instantiation. For an instantiation, the
523 -- parameters of the formal package are made visible in an explicit step.
524 -- Furthermore, if the actual is a visible use_clause, these formals must
525 -- be made potentially use_visible as well. On exit from the enclosing
526 -- instantiation, the reverse must be done.
528 -- For a formal package declared without a box, there are conformance rules
529 -- that apply to the actuals in the generic declaration and the actuals of
530 -- the actual package in the enclosing instantiation. The simplest way to
531 -- apply these rules is to repeat the instantiation of the formal package
532 -- in the context of the enclosing instance, and compare the generic
533 -- associations of this instantiation with those of the actual package.
535 function Is_In_Main_Unit (N : Node_Id) return Boolean;
536 -- Test if given node is in the main unit
538 procedure Load_Parent_Of_Generic (N : Node_Id; Spec : Node_Id);
539 -- If the generic appears in a separate non-generic library unit,
540 -- load the corresponding body to retrieve the body of the generic.
541 -- N is the node for the generic instantiation, Spec is the generic
542 -- package declaration.
544 procedure Inherit_Context (Gen_Decl : Node_Id; Inst : Node_Id);
545 -- Add the context clause of the unit containing a generic unit to
546 -- an instantiation that is a compilation unit.
548 function Get_Associated_Node (N : Node_Id) return Node_Id;
549 -- In order to propagate semantic information back from the analyzed
550 -- copy to the original generic, we maintain links between selected nodes
551 -- in the generic and their corresponding copies. At the end of generic
552 -- analysis, the routine Save_Global_References traverses the generic
553 -- tree, examines the semantic information, and preserves the links to
554 -- those nodes that contain global information. At instantiation, the
555 -- information from the associated node is placed on the new copy, so
556 -- that name resolution is not repeated.
558 -- Three kinds of source nodes have associated nodes:
560 -- a) those that can reference (denote) entities, that is identifiers,
561 -- character literals, expanded_names, operator symbols, operators,
562 -- and attribute reference nodes. These nodes have an Entity field
563 -- and are the set of nodes that are in N_Has_Entity.
565 -- b) aggregates (N_Aggregate and N_Extension_Aggregate)
567 -- c) selected components (N_Selected_Component)
569 -- For the first class, the associated node preserves the entity if it is
570 -- global. If the generic contains nested instantiations, the associated
571 -- node itself has been recopied, and a chain of them must be followed.
573 -- For aggregates, the associated node allows retrieval of the type, which
574 -- may otherwise not appear in the generic. The view of this type may be
575 -- different between generic and instantiation, and the full view can be
576 -- installed before the instantiation is analyzed. For aggregates of
577 -- type extensions, the same view exchange may have to be performed for
578 -- some of the ancestor types, if their view is private at the point of
581 -- Nodes that are selected components in the parse tree may be rewritten
582 -- as expanded names after resolution, and must be treated as potential
583 -- entity holders. which is why they also have an Associated_Node.
585 -- Nodes that do not come from source, such as freeze nodes, do not appear
586 -- in the generic tree, and need not have an associated node.
588 -- The associated node is stored in the Associated_Node field. Note that
589 -- this field overlaps Entity, which is fine, because the whole point is
590 -- that we don't need or want the normal Entity field in this situation.
592 procedure Move_Freeze_Nodes
596 -- Freeze nodes can be generated in the analysis of a generic unit, but
597 -- will not be seen by the back-end. It is necessary to move those nodes
598 -- to the enclosing scope if they freeze an outer entity. We place them
599 -- at the end of the enclosing generic package, which is semantically
602 procedure Pre_Analyze_Actuals (N : Node_Id);
603 -- Analyze actuals to perform name resolution. Full resolution is done
604 -- later, when the expected types are known, but names have to be captured
605 -- before installing parents of generics, that are not visible for the
606 -- actuals themselves.
608 procedure Valid_Default_Attribute (Nam : Entity_Id; Def : Node_Id);
609 -- Verify that an attribute that appears as the default for a formal
610 -- subprogram is a function or procedure with the correct profile.
612 -------------------------------------------
613 -- Data Structures for Generic Renamings --
614 -------------------------------------------
616 -- The map Generic_Renamings associates generic entities with their
617 -- corresponding actuals. Currently used to validate type instances.
618 -- It will eventually be used for all generic parameters to eliminate
619 -- the need for overload resolution in the instance.
621 type Assoc_Ptr is new Int;
623 Assoc_Null : constant Assoc_Ptr := -1;
628 Next_In_HTable : Assoc_Ptr;
631 package Generic_Renamings is new Table.Table
632 (Table_Component_Type => Assoc,
633 Table_Index_Type => Assoc_Ptr,
634 Table_Low_Bound => 0,
636 Table_Increment => 100,
637 Table_Name => "Generic_Renamings");
639 -- Variable to hold enclosing instantiation. When the environment is
640 -- saved for a subprogram inlining, the corresponding Act_Id is empty.
642 Current_Instantiated_Parent : Assoc := (Empty, Empty, Assoc_Null);
644 -- Hash table for associations
646 HTable_Size : constant := 37;
647 type HTable_Range is range 0 .. HTable_Size - 1;
649 procedure Set_Next_Assoc (E : Assoc_Ptr; Next : Assoc_Ptr);
650 function Next_Assoc (E : Assoc_Ptr) return Assoc_Ptr;
651 function Get_Gen_Id (E : Assoc_Ptr) return Entity_Id;
652 function Hash (F : Entity_Id) return HTable_Range;
654 package Generic_Renamings_HTable is new GNAT.HTable.Static_HTable (
655 Header_Num => HTable_Range,
657 Elmt_Ptr => Assoc_Ptr,
658 Null_Ptr => Assoc_Null,
659 Set_Next => Set_Next_Assoc,
662 Get_Key => Get_Gen_Id,
666 Exchanged_Views : Elist_Id;
667 -- This list holds the private views that have been exchanged during
668 -- instantiation to restore the visibility of the generic declaration.
669 -- (see comments above). After instantiation, the current visibility is
670 -- reestablished by means of a traversal of this list.
672 Hidden_Entities : Elist_Id;
673 -- This list holds the entities of the current scope that are removed
674 -- from immediate visibility when instantiating a child unit. Their
675 -- visibility is restored in Remove_Parent.
677 -- Because instantiations can be recursive, the following must be saved
678 -- on entry and restored on exit from an instantiation (spec or body).
679 -- This is done by the two procedures Save_Env and Restore_Env. For
680 -- package and subprogram instantiations (but not for the body instances)
681 -- the action of Save_Env is done in two steps: Init_Env is called before
682 -- Check_Generic_Child_Unit, because setting the parent instances requires
683 -- that the visibility data structures be properly initialized. Once the
684 -- generic is unit is validated, Set_Instance_Env completes Save_Env.
686 Parent_Unit_Visible : Boolean := False;
687 -- Parent_Unit_Visible is used when the generic is a child unit, and
688 -- indicates whether the ultimate parent of the generic is visible in the
689 -- instantiation environment. It is used to reset the visiblity of the
690 -- parent at the end of the instantiation (see Remove_Parent).
692 type Instance_Env is record
693 Ada_Version : Ada_Version_Type;
694 Instantiated_Parent : Assoc;
695 Exchanged_Views : Elist_Id;
696 Hidden_Entities : Elist_Id;
697 Current_Sem_Unit : Unit_Number_Type;
698 Parent_Unit_Visible : Boolean := False;
701 package Instance_Envs is new Table.Table (
702 Table_Component_Type => Instance_Env,
703 Table_Index_Type => Int,
704 Table_Low_Bound => 0,
706 Table_Increment => 100,
707 Table_Name => "Instance_Envs");
709 procedure Restore_Private_Views
710 (Pack_Id : Entity_Id;
711 Is_Package : Boolean := True);
712 -- Restore the private views of external types, and unmark the generic
713 -- renamings of actuals, so that they become comptible subtypes again.
714 -- For subprograms, Pack_Id is the package constructed to hold the
717 procedure Switch_View (T : Entity_Id);
718 -- Switch the partial and full views of a type and its private
719 -- dependents (i.e. its subtypes and derived types).
721 ------------------------------------
722 -- Structures for Error Reporting --
723 ------------------------------------
725 Instantiation_Node : Node_Id;
726 -- Used by subprograms that validate instantiation of formal parameters
727 -- where there might be no actual on which to place the error message.
728 -- Also used to locate the instantiation node for generic subunits.
730 Instantiation_Error : exception;
731 -- When there is a semantic error in the generic parameter matching,
732 -- there is no point in continuing the instantiation, because the
733 -- number of cascaded errors is unpredictable. This exception aborts
734 -- the instantiation process altogether.
736 S_Adjustment : Sloc_Adjustment;
737 -- Offset created for each node in an instantiation, in order to keep
738 -- track of the source position of the instantiation in each of its nodes.
739 -- A subsequent semantic error or warning on a construct of the instance
740 -- points to both places: the original generic node, and the point of
741 -- instantiation. See Sinput and Sinput.L for additional details.
743 ------------------------------------------------------------
744 -- Data structure for keeping track when inside a Generic --
745 ------------------------------------------------------------
747 -- The following table is used to save values of the Inside_A_Generic
748 -- flag (see spec of Sem) when they are saved by Start_Generic.
750 package Generic_Flags is new Table.Table (
751 Table_Component_Type => Boolean,
752 Table_Index_Type => Int,
753 Table_Low_Bound => 0,
755 Table_Increment => 200,
756 Table_Name => "Generic_Flags");
758 ---------------------------
759 -- Abandon_Instantiation --
760 ---------------------------
762 procedure Abandon_Instantiation (N : Node_Id) is
764 Error_Msg_N ("instantiation abandoned!", N);
765 raise Instantiation_Error;
766 end Abandon_Instantiation;
768 --------------------------
769 -- Analyze_Associations --
770 --------------------------
772 function Analyze_Associations
775 F_Copy : List_Id) return List_Id
777 Actual_Types : constant Elist_Id := New_Elmt_List;
778 Assoc : constant List_Id := New_List;
779 Defaults : constant Elist_Id := New_Elmt_List;
780 Gen_Unit : constant Entity_Id := Defining_Entity (Parent (F_Copy));
784 Next_Formal : Node_Id;
785 Temp_Formal : Node_Id;
786 Analyzed_Formal : Node_Id;
789 First_Named : Node_Id := Empty;
790 Found_Assoc : Node_Id;
791 Is_Named_Assoc : Boolean;
792 Num_Matched : Int := 0;
793 Num_Actuals : Int := 0;
795 function Matching_Actual
797 A_F : Entity_Id) return Node_Id;
798 -- Find actual that corresponds to a given a formal parameter. If the
799 -- actuals are positional, return the next one, if any. If the actuals
800 -- are named, scan the parameter associations to find the right one.
801 -- A_F is the corresponding entity in the analyzed generic,which is
802 -- placed on the selector name for ASIS use.
804 procedure Set_Analyzed_Formal;
805 -- Find the node in the generic copy that corresponds to a given formal.
806 -- The semantic information on this node is used to perform legality
807 -- checks on the actuals. Because semantic analysis can introduce some
808 -- anonymous entities or modify the declaration node itself, the
809 -- correspondence between the two lists is not one-one. In addition to
810 -- anonymous types, the presence a formal equality will introduce an
811 -- implicit declaration for the corresponding inequality.
813 ---------------------
814 -- Matching_Actual --
815 ---------------------
817 function Matching_Actual
819 A_F : Entity_Id) return Node_Id
825 Is_Named_Assoc := False;
827 -- End of list of purely positional parameters
832 -- Case of positional parameter corresponding to current formal
834 elsif No (Selector_Name (Actual)) then
835 Found := Explicit_Generic_Actual_Parameter (Actual);
836 Found_Assoc := Actual;
837 Num_Matched := Num_Matched + 1;
840 -- Otherwise scan list of named actuals to find the one with the
841 -- desired name. All remaining actuals have explicit names.
844 Is_Named_Assoc := True;
848 while Present (Actual) loop
849 if Chars (Selector_Name (Actual)) = Chars (F) then
850 Found := Explicit_Generic_Actual_Parameter (Actual);
851 Set_Entity (Selector_Name (Actual), A_F);
852 Set_Etype (Selector_Name (Actual), Etype (A_F));
853 Generate_Reference (A_F, Selector_Name (Actual));
854 Found_Assoc := Actual;
855 Num_Matched := Num_Matched + 1;
863 -- Reset for subsequent searches. In most cases the named
864 -- associations are in order. If they are not, we reorder them
865 -- to avoid scanning twice the same actual. This is not just a
866 -- question of efficiency: there may be multiple defaults with
867 -- boxes that have the same name. In a nested instantiation we
868 -- insert actuals for those defaults, and cannot rely on their
869 -- names to disambiguate them.
871 if Actual = First_Named then
874 elsif Present (Actual) then
875 Insert_Before (First_Named, Remove_Next (Prev));
878 Actual := First_Named;
884 -------------------------
885 -- Set_Analyzed_Formal --
886 -------------------------
888 procedure Set_Analyzed_Formal is
891 while Present (Analyzed_Formal) loop
892 Kind := Nkind (Analyzed_Formal);
894 case Nkind (Formal) is
896 when N_Formal_Subprogram_Declaration =>
897 exit when Kind in N_Formal_Subprogram_Declaration
900 (Defining_Unit_Name (Specification (Formal))) =
902 (Defining_Unit_Name (Specification (Analyzed_Formal)));
904 when N_Formal_Package_Declaration =>
906 Kind = N_Formal_Package_Declaration
908 Kind = N_Generic_Package_Declaration;
910 when N_Use_Package_Clause | N_Use_Type_Clause => exit;
914 -- Skip freeze nodes, and nodes inserted to replace
915 -- unrecognized pragmas.
918 Kind not in N_Formal_Subprogram_Declaration
919 and then Kind /= N_Subprogram_Declaration
920 and then Kind /= N_Freeze_Entity
921 and then Kind /= N_Null_Statement
922 and then Kind /= N_Itype_Reference
923 and then Chars (Defining_Identifier (Formal)) =
924 Chars (Defining_Identifier (Analyzed_Formal));
927 Next (Analyzed_Formal);
930 end Set_Analyzed_Formal;
932 -- Start of processing for Analyze_Associations
935 -- If named associations are present, save the first named association
936 -- (it may of course be Empty) to facilitate subsequent name search.
938 Actuals := Generic_Associations (I_Node);
940 if Present (Actuals) then
941 First_Named := First (Actuals);
943 while Present (First_Named)
944 and then No (Selector_Name (First_Named))
946 Num_Actuals := Num_Actuals + 1;
951 Named := First_Named;
952 while Present (Named) loop
953 if No (Selector_Name (Named)) then
954 Error_Msg_N ("invalid positional actual after named one", Named);
955 Abandon_Instantiation (Named);
958 -- A named association may lack an actual parameter, if it was
959 -- introduced for a default subprogram that turns out to be local
960 -- to the outer instantiation.
962 if Present (Explicit_Generic_Actual_Parameter (Named)) then
963 Num_Actuals := Num_Actuals + 1;
969 if Present (Formals) then
970 Formal := First_Non_Pragma (Formals);
971 Analyzed_Formal := First_Non_Pragma (F_Copy);
973 if Present (Actuals) then
974 Actual := First (Actuals);
976 -- All formals should have default values
982 while Present (Formal) loop
984 Next_Formal := Next_Non_Pragma (Formal);
986 case Nkind (Formal) is
987 when N_Formal_Object_Declaration =>
990 Defining_Identifier (Formal),
991 Defining_Identifier (Analyzed_Formal));
994 (Instantiate_Object (Formal, Match, Analyzed_Formal),
997 when N_Formal_Type_Declaration =>
1000 Defining_Identifier (Formal),
1001 Defining_Identifier (Analyzed_Formal));
1004 Error_Msg_Sloc := Sloc (Gen_Unit);
1007 Instantiation_Node, Defining_Identifier (Formal));
1008 Error_Msg_NE ("\in instantiation of & declared#",
1009 Instantiation_Node, Gen_Unit);
1010 Abandon_Instantiation (Instantiation_Node);
1016 (Formal, Match, Analyzed_Formal, Assoc));
1018 -- an instantiation is a freeze point for the actuals,
1019 -- unless this is a rewritten formal package.
1021 if Nkind (I_Node) /= N_Formal_Package_Declaration then
1022 Append_Elmt (Entity (Match), Actual_Types);
1026 -- A remote access-to-class-wide type must not be an
1027 -- actual parameter for a generic formal of an access
1028 -- type (E.2.2 (17)).
1030 if Nkind (Analyzed_Formal) = N_Formal_Type_Declaration
1032 Nkind (Formal_Type_Definition (Analyzed_Formal)) =
1033 N_Access_To_Object_Definition
1035 Validate_Remote_Access_To_Class_Wide_Type (Match);
1038 when N_Formal_Subprogram_Declaration =>
1041 Defining_Unit_Name (Specification (Formal)),
1042 Defining_Unit_Name (Specification (Analyzed_Formal)));
1044 -- If the formal subprogram has the same name as
1045 -- another formal subprogram of the generic, then
1046 -- a named association is illegal (12.3(9)). Exclude
1047 -- named associations that are generated for a nested
1051 and then Is_Named_Assoc
1052 and then Comes_From_Source (Found_Assoc)
1054 Temp_Formal := First (Formals);
1055 while Present (Temp_Formal) loop
1056 if Nkind (Temp_Formal) in
1057 N_Formal_Subprogram_Declaration
1058 and then Temp_Formal /= Formal
1060 Chars (Selector_Name (Found_Assoc)) =
1061 Chars (Defining_Unit_Name
1062 (Specification (Temp_Formal)))
1065 ("name not allowed for overloaded formal",
1067 Abandon_Instantiation (Instantiation_Node);
1075 Instantiate_Formal_Subprogram
1076 (Formal, Match, Analyzed_Formal));
1079 and then Box_Present (Formal)
1082 (Defining_Unit_Name (Specification (Last (Assoc))),
1086 when N_Formal_Package_Declaration =>
1089 Defining_Identifier (Formal),
1090 Defining_Identifier (Original_Node (Analyzed_Formal)));
1093 Error_Msg_Sloc := Sloc (Gen_Unit);
1096 Instantiation_Node, Defining_Identifier (Formal));
1097 Error_Msg_NE ("\in instantiation of & declared#",
1098 Instantiation_Node, Gen_Unit);
1100 Abandon_Instantiation (Instantiation_Node);
1105 (Instantiate_Formal_Package
1106 (Formal, Match, Analyzed_Formal),
1110 -- For use type and use package appearing in the context
1111 -- clause, we have already copied them, so we can just
1112 -- move them where they belong (we mustn't recopy them
1113 -- since this would mess up the Sloc values).
1115 when N_Use_Package_Clause |
1116 N_Use_Type_Clause =>
1118 Append (Formal, Assoc);
1121 raise Program_Error;
1125 Formal := Next_Formal;
1126 Next_Non_Pragma (Analyzed_Formal);
1129 if Num_Actuals > Num_Matched then
1130 Error_Msg_Sloc := Sloc (Gen_Unit);
1132 if Present (Selector_Name (Actual)) then
1134 ("unmatched actual&",
1135 Actual, Selector_Name (Actual));
1136 Error_Msg_NE ("\in instantiation of& declared#",
1140 ("unmatched actual in instantiation of& declared#",
1145 elsif Present (Actuals) then
1147 ("too many actuals in generic instantiation", Instantiation_Node);
1151 Elmt : Elmt_Id := First_Elmt (Actual_Types);
1154 while Present (Elmt) loop
1155 Freeze_Before (I_Node, Node (Elmt));
1160 -- If there are default subprograms, normalize the tree by adding
1161 -- explicit associations for them. This is required if the instance
1162 -- appears within a generic.
1170 Elmt := First_Elmt (Defaults);
1171 while Present (Elmt) loop
1172 if No (Actuals) then
1173 Actuals := New_List;
1174 Set_Generic_Associations (I_Node, Actuals);
1177 Subp := Node (Elmt);
1179 Make_Generic_Association (Sloc (Subp),
1180 Selector_Name => New_Occurrence_Of (Subp, Sloc (Subp)),
1181 Explicit_Generic_Actual_Parameter =>
1182 New_Occurrence_Of (Subp, Sloc (Subp)));
1183 Mark_Rewrite_Insertion (New_D);
1184 Append_To (Actuals, New_D);
1190 end Analyze_Associations;
1192 -------------------------------
1193 -- Analyze_Formal_Array_Type --
1194 -------------------------------
1196 procedure Analyze_Formal_Array_Type
1197 (T : in out Entity_Id;
1203 -- Treated like a non-generic array declaration, with
1204 -- additional semantic checks.
1208 if Nkind (Def) = N_Constrained_Array_Definition then
1209 DSS := First (Discrete_Subtype_Definitions (Def));
1210 while Present (DSS) loop
1211 if Nkind (DSS) = N_Subtype_Indication
1212 or else Nkind (DSS) = N_Range
1213 or else Nkind (DSS) = N_Attribute_Reference
1215 Error_Msg_N ("only a subtype mark is allowed in a formal", DSS);
1222 Array_Type_Declaration (T, Def);
1223 Set_Is_Generic_Type (Base_Type (T));
1225 if Ekind (Component_Type (T)) = E_Incomplete_Type
1226 and then No (Full_View (Component_Type (T)))
1228 Error_Msg_N ("premature usage of incomplete type", Def);
1230 -- Check that range constraint is not allowed on the component type
1231 -- of a generic formal array type (AARM 12.5.3(3))
1233 elsif Is_Internal (Component_Type (T))
1234 and then Present (Subtype_Indication (Component_Definition (Def)))
1235 and then Nkind (Original_Node
1236 (Subtype_Indication (Component_Definition (Def))))
1237 = N_Subtype_Indication
1240 ("in a formal, a subtype indication can only be "
1241 & "a subtype mark ('R'M 12.5.3(3))",
1242 Subtype_Indication (Component_Definition (Def)));
1245 end Analyze_Formal_Array_Type;
1247 ---------------------------------------------
1248 -- Analyze_Formal_Decimal_Fixed_Point_Type --
1249 ---------------------------------------------
1251 -- As for other generic types, we create a valid type representation
1252 -- with legal but arbitrary attributes, whose values are never considered
1253 -- static. For all scalar types we introduce an anonymous base type, with
1254 -- the same attributes. We choose the corresponding integer type to be
1255 -- Standard_Integer.
1257 procedure Analyze_Formal_Decimal_Fixed_Point_Type
1261 Loc : constant Source_Ptr := Sloc (Def);
1262 Base : constant Entity_Id :=
1264 (E_Decimal_Fixed_Point_Type,
1265 Current_Scope, Sloc (Def), 'G');
1266 Int_Base : constant Entity_Id := Standard_Integer;
1267 Delta_Val : constant Ureal := Ureal_1;
1268 Digs_Val : constant Uint := Uint_6;
1273 Set_Etype (Base, Base);
1274 Set_Size_Info (Base, Int_Base);
1275 Set_RM_Size (Base, RM_Size (Int_Base));
1276 Set_First_Rep_Item (Base, First_Rep_Item (Int_Base));
1277 Set_Digits_Value (Base, Digs_Val);
1278 Set_Delta_Value (Base, Delta_Val);
1279 Set_Small_Value (Base, Delta_Val);
1280 Set_Scalar_Range (Base,
1282 Low_Bound => Make_Real_Literal (Loc, Ureal_1),
1283 High_Bound => Make_Real_Literal (Loc, Ureal_1)));
1285 Set_Is_Generic_Type (Base);
1286 Set_Parent (Base, Parent (Def));
1288 Set_Ekind (T, E_Decimal_Fixed_Point_Subtype);
1289 Set_Etype (T, Base);
1290 Set_Size_Info (T, Int_Base);
1291 Set_RM_Size (T, RM_Size (Int_Base));
1292 Set_First_Rep_Item (T, First_Rep_Item (Int_Base));
1293 Set_Digits_Value (T, Digs_Val);
1294 Set_Delta_Value (T, Delta_Val);
1295 Set_Small_Value (T, Delta_Val);
1296 Set_Scalar_Range (T, Scalar_Range (Base));
1297 Set_Is_Constrained (T);
1299 Check_Restriction (No_Fixed_Point, Def);
1300 end Analyze_Formal_Decimal_Fixed_Point_Type;
1302 ---------------------------------
1303 -- Analyze_Formal_Derived_Type --
1304 ---------------------------------
1306 procedure Analyze_Formal_Derived_Type
1311 Loc : constant Source_Ptr := Sloc (Def);
1312 Unk_Disc : constant Boolean := Unknown_Discriminants_Present (N);
1316 Set_Is_Generic_Type (T);
1318 if Private_Present (Def) then
1320 Make_Private_Extension_Declaration (Loc,
1321 Defining_Identifier => T,
1322 Discriminant_Specifications => Discriminant_Specifications (N),
1323 Unknown_Discriminants_Present => Unk_Disc,
1324 Subtype_Indication => Subtype_Mark (Def));
1326 Set_Abstract_Present (New_N, Abstract_Present (Def));
1330 Make_Full_Type_Declaration (Loc,
1331 Defining_Identifier => T,
1332 Discriminant_Specifications =>
1333 Discriminant_Specifications (Parent (T)),
1335 Make_Derived_Type_Definition (Loc,
1336 Subtype_Indication => Subtype_Mark (Def)));
1338 Set_Abstract_Present
1339 (Type_Definition (New_N), Abstract_Present (Def));
1346 if not Is_Composite_Type (T) then
1348 ("unknown discriminants not allowed for elementary types", N);
1350 Set_Has_Unknown_Discriminants (T);
1351 Set_Is_Constrained (T, False);
1355 -- If the parent type has a known size, so does the formal, which
1356 -- makes legal representation clauses that involve the formal.
1358 Set_Size_Known_At_Compile_Time
1359 (T, Size_Known_At_Compile_Time (Entity (Subtype_Mark (Def))));
1361 end Analyze_Formal_Derived_Type;
1363 ----------------------------------
1364 -- Analyze_Formal_Discrete_Type --
1365 ----------------------------------
1367 -- The operations defined for a discrete types are those of an
1368 -- enumeration type. The size is set to an arbitrary value, for use
1369 -- in analyzing the generic unit.
1371 procedure Analyze_Formal_Discrete_Type (T : Entity_Id; Def : Node_Id) is
1372 Loc : constant Source_Ptr := Sloc (Def);
1376 Base : constant Entity_Id :=
1378 (E_Floating_Point_Type, Current_Scope, Sloc (Def), 'G');
1381 Set_Ekind (T, E_Enumeration_Subtype);
1382 Set_Etype (T, Base);
1385 Set_Is_Generic_Type (T);
1386 Set_Is_Constrained (T);
1388 -- For semantic analysis, the bounds of the type must be set to some
1389 -- non-static value. The simplest is to create attribute nodes for
1390 -- those bounds, that refer to the type itself. These bounds are never
1391 -- analyzed but serve as place-holders.
1394 Make_Attribute_Reference (Loc,
1395 Attribute_Name => Name_First,
1396 Prefix => New_Reference_To (T, Loc));
1400 Make_Attribute_Reference (Loc,
1401 Attribute_Name => Name_Last,
1402 Prefix => New_Reference_To (T, Loc));
1405 Set_Scalar_Range (T,
1410 Set_Ekind (Base, E_Enumeration_Type);
1411 Set_Etype (Base, Base);
1412 Init_Size (Base, 8);
1413 Init_Alignment (Base);
1414 Set_Is_Generic_Type (Base);
1415 Set_Scalar_Range (Base, Scalar_Range (T));
1416 Set_Parent (Base, Parent (Def));
1418 end Analyze_Formal_Discrete_Type;
1420 ----------------------------------
1421 -- Analyze_Formal_Floating_Type --
1422 ---------------------------------
1424 procedure Analyze_Formal_Floating_Type (T : Entity_Id; Def : Node_Id) is
1425 Base : constant Entity_Id :=
1427 (E_Floating_Point_Type, Current_Scope, Sloc (Def), 'G');
1430 -- The various semantic attributes are taken from the predefined type
1431 -- Float, just so that all of them are initialized. Their values are
1432 -- never used because no constant folding or expansion takes place in
1433 -- the generic itself.
1436 Set_Ekind (T, E_Floating_Point_Subtype);
1437 Set_Etype (T, Base);
1438 Set_Size_Info (T, (Standard_Float));
1439 Set_RM_Size (T, RM_Size (Standard_Float));
1440 Set_Digits_Value (T, Digits_Value (Standard_Float));
1441 Set_Scalar_Range (T, Scalar_Range (Standard_Float));
1442 Set_Is_Constrained (T);
1444 Set_Is_Generic_Type (Base);
1445 Set_Etype (Base, Base);
1446 Set_Size_Info (Base, (Standard_Float));
1447 Set_RM_Size (Base, RM_Size (Standard_Float));
1448 Set_Digits_Value (Base, Digits_Value (Standard_Float));
1449 Set_Scalar_Range (Base, Scalar_Range (Standard_Float));
1450 Set_Parent (Base, Parent (Def));
1452 Check_Restriction (No_Floating_Point, Def);
1453 end Analyze_Formal_Floating_Type;
1455 ---------------------------------
1456 -- Analyze_Formal_Modular_Type --
1457 ---------------------------------
1459 procedure Analyze_Formal_Modular_Type (T : Entity_Id; Def : Node_Id) is
1461 -- Apart from their entity kind, generic modular types are treated
1462 -- like signed integer types, and have the same attributes.
1464 Analyze_Formal_Signed_Integer_Type (T, Def);
1465 Set_Ekind (T, E_Modular_Integer_Subtype);
1466 Set_Ekind (Etype (T), E_Modular_Integer_Type);
1468 end Analyze_Formal_Modular_Type;
1470 ---------------------------------------
1471 -- Analyze_Formal_Object_Declaration --
1472 ---------------------------------------
1474 procedure Analyze_Formal_Object_Declaration (N : Node_Id) is
1475 E : constant Node_Id := Expression (N);
1476 Id : constant Node_Id := Defining_Identifier (N);
1483 -- Determine the mode of the formal object
1485 if Out_Present (N) then
1486 K := E_Generic_In_Out_Parameter;
1488 if not In_Present (N) then
1489 Error_Msg_N ("formal generic objects cannot have mode OUT", N);
1493 K := E_Generic_In_Parameter;
1496 Find_Type (Subtype_Mark (N));
1497 T := Entity (Subtype_Mark (N));
1499 if Ekind (T) = E_Incomplete_Type then
1500 Error_Msg_N ("premature usage of incomplete type", Subtype_Mark (N));
1503 if K = E_Generic_In_Parameter then
1505 -- Ada 2005 (AI-287): Limited aggregates allowed in generic formals
1507 if Ada_Version < Ada_05 and then Is_Limited_Type (T) then
1509 ("generic formal of mode IN must not be of limited type", N);
1510 Explain_Limited_Type (T, N);
1513 if Is_Abstract (T) then
1515 ("generic formal of mode IN must not be of abstract type", N);
1519 Analyze_Per_Use_Expression (E, T);
1525 -- Case of generic IN OUT parameter
1528 -- If the formal has an unconstrained type, construct its
1529 -- actual subtype, as is done for subprogram formals. In this
1530 -- fashion, all its uses can refer to specific bounds.
1535 if (Is_Array_Type (T)
1536 and then not Is_Constrained (T))
1538 (Ekind (T) = E_Record_Type
1539 and then Has_Discriminants (T))
1542 Non_Freezing_Ref : constant Node_Id :=
1543 New_Reference_To (Id, Sloc (Id));
1547 -- Make sure that the actual subtype doesn't generate
1550 Set_Must_Not_Freeze (Non_Freezing_Ref);
1551 Decl := Build_Actual_Subtype (T, Non_Freezing_Ref);
1552 Insert_Before_And_Analyze (N, Decl);
1553 Set_Actual_Subtype (Id, Defining_Identifier (Decl));
1556 Set_Actual_Subtype (Id, T);
1561 ("initialization not allowed for `IN OUT` formals", N);
1565 end Analyze_Formal_Object_Declaration;
1567 ----------------------------------------------
1568 -- Analyze_Formal_Ordinary_Fixed_Point_Type --
1569 ----------------------------------------------
1571 procedure Analyze_Formal_Ordinary_Fixed_Point_Type
1575 Loc : constant Source_Ptr := Sloc (Def);
1576 Base : constant Entity_Id :=
1578 (E_Ordinary_Fixed_Point_Type, Current_Scope, Sloc (Def), 'G');
1580 -- The semantic attributes are set for completeness only, their
1581 -- values will never be used, because all properties of the type
1585 Set_Ekind (T, E_Ordinary_Fixed_Point_Subtype);
1586 Set_Etype (T, Base);
1587 Set_Size_Info (T, Standard_Integer);
1588 Set_RM_Size (T, RM_Size (Standard_Integer));
1589 Set_Small_Value (T, Ureal_1);
1590 Set_Delta_Value (T, Ureal_1);
1591 Set_Scalar_Range (T,
1593 Low_Bound => Make_Real_Literal (Loc, Ureal_1),
1594 High_Bound => Make_Real_Literal (Loc, Ureal_1)));
1595 Set_Is_Constrained (T);
1597 Set_Is_Generic_Type (Base);
1598 Set_Etype (Base, Base);
1599 Set_Size_Info (Base, Standard_Integer);
1600 Set_RM_Size (Base, RM_Size (Standard_Integer));
1601 Set_Small_Value (Base, Ureal_1);
1602 Set_Delta_Value (Base, Ureal_1);
1603 Set_Scalar_Range (Base, Scalar_Range (T));
1604 Set_Parent (Base, Parent (Def));
1606 Check_Restriction (No_Fixed_Point, Def);
1607 end Analyze_Formal_Ordinary_Fixed_Point_Type;
1609 ----------------------------
1610 -- Analyze_Formal_Package --
1611 ----------------------------
1613 procedure Analyze_Formal_Package (N : Node_Id) is
1614 Loc : constant Source_Ptr := Sloc (N);
1615 Pack_Id : constant Entity_Id := Defining_Identifier (N);
1617 Gen_Id : constant Node_Id := Name (N);
1619 Gen_Unit : Entity_Id;
1621 Parent_Installed : Boolean := False;
1623 Parent_Instance : Entity_Id;
1624 Renaming_In_Par : Entity_Id;
1627 Text_IO_Kludge (Gen_Id);
1630 Check_Generic_Child_Unit (Gen_Id, Parent_Installed);
1631 Gen_Unit := Entity (Gen_Id);
1633 if Ekind (Gen_Unit) /= E_Generic_Package then
1634 Error_Msg_N ("expect generic package name", Gen_Id);
1638 elsif Gen_Unit = Current_Scope then
1640 ("generic package cannot be used as a formal package of itself",
1645 elsif In_Open_Scopes (Gen_Unit) then
1646 if Is_Compilation_Unit (Gen_Unit)
1647 and then Is_Child_Unit (Current_Scope)
1649 -- Special-case the error when the formal is a parent, and
1650 -- continue analysis to minimize cascaded errors.
1653 ("generic parent cannot be used as formal package "
1654 & "of a child unit",
1659 ("generic package cannot be used as a formal package "
1667 -- Check for a formal package that is a package renaming
1669 if Present (Renamed_Object (Gen_Unit)) then
1670 Gen_Unit := Renamed_Object (Gen_Unit);
1673 -- The formal package is treated like a regular instance, but only
1674 -- the specification needs to be instantiated, to make entities visible.
1676 if not Box_Present (N) then
1677 Hidden_Entities := New_Elmt_List;
1678 Analyze_Package_Instantiation (N);
1680 if Parent_Installed then
1685 -- If there are no generic associations, the generic parameters
1686 -- appear as local entities and are instantiated like them. We copy
1687 -- the generic package declaration as if it were an instantiation,
1688 -- and analyze it like a regular package, except that we treat the
1689 -- formals as additional visible components.
1691 Gen_Decl := Unit_Declaration_Node (Gen_Unit);
1693 if In_Extended_Main_Source_Unit (N) then
1694 Set_Is_Instantiated (Gen_Unit);
1695 Generate_Reference (Gen_Unit, N);
1698 Formal := New_Copy (Pack_Id);
1701 (Original_Node (Gen_Decl), Empty, Instantiating => True);
1703 Set_Defining_Unit_Name (Specification (New_N), Formal);
1704 Set_Instance_Env (Gen_Unit, Formal);
1706 Enter_Name (Formal);
1707 Set_Ekind (Formal, E_Generic_Package);
1708 Set_Etype (Formal, Standard_Void_Type);
1709 Set_Inner_Instances (Formal, New_Elmt_List);
1712 -- Within the formal, the name of the generic package is a renaming
1713 -- of the formal (as for a regular instantiation).
1715 Renaming := Make_Package_Renaming_Declaration (Loc,
1716 Defining_Unit_Name =>
1717 Make_Defining_Identifier (Loc, Chars (Gen_Unit)),
1718 Name => New_Reference_To (Formal, Loc));
1720 if Present (Visible_Declarations (Specification (N))) then
1721 Prepend (Renaming, To => Visible_Declarations (Specification (N)));
1722 elsif Present (Private_Declarations (Specification (N))) then
1723 Prepend (Renaming, To => Private_Declarations (Specification (N)));
1726 if Is_Child_Unit (Gen_Unit)
1727 and then Parent_Installed
1729 -- Similarly, we have to make the name of the formal visible in
1730 -- the parent instance, to resolve properly fully qualified names
1731 -- that may appear in the generic unit. The parent instance has
1732 -- been placed on the scope stack ahead of the current scope.
1734 Parent_Instance := Scope_Stack.Table (Scope_Stack.Last - 1).Entity;
1737 Make_Defining_Identifier (Loc, Chars (Gen_Unit));
1738 Set_Ekind (Renaming_In_Par, E_Package);
1739 Set_Etype (Renaming_In_Par, Standard_Void_Type);
1740 Set_Scope (Renaming_In_Par, Parent_Instance);
1741 Set_Parent (Renaming_In_Par, Parent (Formal));
1742 Set_Renamed_Object (Renaming_In_Par, Formal);
1743 Append_Entity (Renaming_In_Par, Parent_Instance);
1746 Analyze_Generic_Formal_Part (N);
1747 Analyze (Specification (N));
1748 End_Package_Scope (Formal);
1750 if Parent_Installed then
1756 -- Inside the generic unit, the formal package is a regular
1757 -- package, but no body is needed for it. Note that after
1758 -- instantiation, the defining_unit_name we need is in the
1759 -- new tree and not in the original. (see Package_Instantiation).
1760 -- A generic formal package is an instance, and can be used as
1761 -- an actual for an inner instance. Mark its generic parent.
1763 Set_Ekind (Formal, E_Package);
1764 Set_Generic_Parent (Specification (N), Gen_Unit);
1765 Set_Has_Completion (Formal, True);
1767 Set_Ekind (Pack_Id, E_Package);
1768 Set_Etype (Pack_Id, Standard_Void_Type);
1769 Set_Scope (Pack_Id, Scope (Formal));
1770 Set_Has_Completion (Pack_Id, True);
1772 end Analyze_Formal_Package;
1774 ---------------------------------
1775 -- Analyze_Formal_Private_Type --
1776 ---------------------------------
1778 procedure Analyze_Formal_Private_Type
1784 New_Private_Type (N, T, Def);
1786 -- Set the size to an arbitrary but legal value
1788 Set_Size_Info (T, Standard_Integer);
1789 Set_RM_Size (T, RM_Size (Standard_Integer));
1790 end Analyze_Formal_Private_Type;
1792 ----------------------------------------
1793 -- Analyze_Formal_Signed_Integer_Type --
1794 ----------------------------------------
1796 procedure Analyze_Formal_Signed_Integer_Type
1800 Base : constant Entity_Id :=
1802 (E_Signed_Integer_Type, Current_Scope, Sloc (Def), 'G');
1807 Set_Ekind (T, E_Signed_Integer_Subtype);
1808 Set_Etype (T, Base);
1809 Set_Size_Info (T, Standard_Integer);
1810 Set_RM_Size (T, RM_Size (Standard_Integer));
1811 Set_Scalar_Range (T, Scalar_Range (Standard_Integer));
1812 Set_Is_Constrained (T);
1814 Set_Is_Generic_Type (Base);
1815 Set_Size_Info (Base, Standard_Integer);
1816 Set_RM_Size (Base, RM_Size (Standard_Integer));
1817 Set_Etype (Base, Base);
1818 Set_Scalar_Range (Base, Scalar_Range (Standard_Integer));
1819 Set_Parent (Base, Parent (Def));
1820 end Analyze_Formal_Signed_Integer_Type;
1822 -------------------------------
1823 -- Analyze_Formal_Subprogram --
1824 -------------------------------
1826 procedure Analyze_Formal_Subprogram (N : Node_Id) is
1827 Spec : constant Node_Id := Specification (N);
1828 Def : constant Node_Id := Default_Name (N);
1829 Nam : constant Entity_Id := Defining_Unit_Name (Spec);
1837 if Nkind (Nam) = N_Defining_Program_Unit_Name then
1838 Error_Msg_N ("name of formal subprogram must be a direct name", Nam);
1842 Analyze_Subprogram_Declaration (N);
1843 Set_Is_Formal_Subprogram (Nam);
1844 Set_Has_Completion (Nam);
1846 if Nkind (N) = N_Formal_Abstract_Subprogram_Declaration then
1847 Set_Is_Abstract (Nam);
1848 Set_Is_Dispatching_Operation (Nam);
1851 Ctrl_Type : constant Entity_Id := Find_Dispatching_Type (Nam);
1854 if not Present (Ctrl_Type) then
1856 ("abstract formal subprogram must have a controlling type",
1860 Check_Controlling_Formals (Ctrl_Type, Nam);
1865 -- Default name is resolved at the point of instantiation
1867 if Box_Present (N) then
1870 -- Else default is bound at the point of generic declaration
1872 elsif Present (Def) then
1873 if Nkind (Def) = N_Operator_Symbol then
1874 Find_Direct_Name (Def);
1876 elsif Nkind (Def) /= N_Attribute_Reference then
1880 -- For an attribute reference, analyze the prefix and verify
1881 -- that it has the proper profile for the subprogram.
1883 Analyze (Prefix (Def));
1884 Valid_Default_Attribute (Nam, Def);
1888 -- Default name may be overloaded, in which case the interpretation
1889 -- with the correct profile must be selected, as for a renaming.
1891 if Etype (Def) = Any_Type then
1894 elsif Nkind (Def) = N_Selected_Component then
1895 Subp := Entity (Selector_Name (Def));
1897 if Ekind (Subp) /= E_Entry then
1898 Error_Msg_N ("expect valid subprogram name as default", Def);
1902 elsif Nkind (Def) = N_Indexed_Component then
1904 if Nkind (Prefix (Def)) /= N_Selected_Component then
1905 Error_Msg_N ("expect valid subprogram name as default", Def);
1909 Subp := Entity (Selector_Name (Prefix (Def)));
1911 if Ekind (Subp) /= E_Entry_Family then
1912 Error_Msg_N ("expect valid subprogram name as default", Def);
1917 elsif Nkind (Def) = N_Character_Literal then
1919 -- Needs some type checks: subprogram should be parameterless???
1921 Resolve (Def, (Etype (Nam)));
1923 elsif not Is_Entity_Name (Def)
1924 or else not Is_Overloadable (Entity (Def))
1926 Error_Msg_N ("expect valid subprogram name as default", Def);
1929 elsif not Is_Overloaded (Def) then
1930 Subp := Entity (Def);
1933 Error_Msg_N ("premature usage of formal subprogram", Def);
1935 elsif not Entity_Matches_Spec (Subp, Nam) then
1936 Error_Msg_N ("no visible entity matches specification", Def);
1942 I1 : Interp_Index := 0;
1948 Get_First_Interp (Def, I, It);
1949 while Present (It.Nam) loop
1951 if Entity_Matches_Spec (It.Nam, Nam) then
1952 if Subp /= Any_Id then
1953 It1 := Disambiguate (Def, I1, I, Etype (Subp));
1955 if It1 = No_Interp then
1956 Error_Msg_N ("ambiguous default subprogram", Def);
1969 Get_Next_Interp (I, It);
1973 if Subp /= Any_Id then
1974 Set_Entity (Def, Subp);
1977 Error_Msg_N ("premature usage of formal subprogram", Def);
1979 elsif Ekind (Subp) /= E_Operator then
1980 Check_Mode_Conformant (Subp, Nam);
1984 Error_Msg_N ("no visible subprogram matches specification", N);
1988 end Analyze_Formal_Subprogram;
1990 -------------------------------------
1991 -- Analyze_Formal_Type_Declaration --
1992 -------------------------------------
1994 procedure Analyze_Formal_Type_Declaration (N : Node_Id) is
1995 Def : constant Node_Id := Formal_Type_Definition (N);
1999 T := Defining_Identifier (N);
2001 if Present (Discriminant_Specifications (N))
2002 and then Nkind (Def) /= N_Formal_Private_Type_Definition
2005 ("discriminants not allowed for this formal type",
2006 Defining_Identifier (First (Discriminant_Specifications (N))));
2009 -- Enter the new name, and branch to specific routine
2012 when N_Formal_Private_Type_Definition =>
2013 Analyze_Formal_Private_Type (N, T, Def);
2015 when N_Formal_Derived_Type_Definition =>
2016 Analyze_Formal_Derived_Type (N, T, Def);
2018 when N_Formal_Discrete_Type_Definition =>
2019 Analyze_Formal_Discrete_Type (T, Def);
2021 when N_Formal_Signed_Integer_Type_Definition =>
2022 Analyze_Formal_Signed_Integer_Type (T, Def);
2024 when N_Formal_Modular_Type_Definition =>
2025 Analyze_Formal_Modular_Type (T, Def);
2027 when N_Formal_Floating_Point_Definition =>
2028 Analyze_Formal_Floating_Type (T, Def);
2030 when N_Formal_Ordinary_Fixed_Point_Definition =>
2031 Analyze_Formal_Ordinary_Fixed_Point_Type (T, Def);
2033 when N_Formal_Decimal_Fixed_Point_Definition =>
2034 Analyze_Formal_Decimal_Fixed_Point_Type (T, Def);
2036 when N_Array_Type_Definition =>
2037 Analyze_Formal_Array_Type (T, Def);
2039 when N_Access_To_Object_Definition |
2040 N_Access_Function_Definition |
2041 N_Access_Procedure_Definition =>
2042 Analyze_Generic_Access_Type (T, Def);
2048 raise Program_Error;
2052 Set_Is_Generic_Type (T);
2053 end Analyze_Formal_Type_Declaration;
2055 ------------------------------------
2056 -- Analyze_Function_Instantiation --
2057 ------------------------------------
2059 procedure Analyze_Function_Instantiation (N : Node_Id) is
2061 Analyze_Subprogram_Instantiation (N, E_Function);
2062 end Analyze_Function_Instantiation;
2064 ---------------------------------
2065 -- Analyze_Generic_Access_Type --
2066 ---------------------------------
2068 procedure Analyze_Generic_Access_Type (T : Entity_Id; Def : Node_Id) is
2072 if Nkind (Def) = N_Access_To_Object_Definition then
2073 Access_Type_Declaration (T, Def);
2075 if Is_Incomplete_Or_Private_Type (Designated_Type (T))
2076 and then No (Full_View (Designated_Type (T)))
2077 and then not Is_Generic_Type (Designated_Type (T))
2079 Error_Msg_N ("premature usage of incomplete type", Def);
2081 elsif Is_Internal (Designated_Type (T)) then
2083 ("only a subtype mark is allowed in a formal", Def);
2087 Access_Subprogram_Declaration (T, Def);
2089 end Analyze_Generic_Access_Type;
2091 ---------------------------------
2092 -- Analyze_Generic_Formal_Part --
2093 ---------------------------------
2095 procedure Analyze_Generic_Formal_Part (N : Node_Id) is
2096 Gen_Parm_Decl : Node_Id;
2099 -- The generic formals are processed in the scope of the generic
2100 -- unit, where they are immediately visible. The scope is installed
2103 Gen_Parm_Decl := First (Generic_Formal_Declarations (N));
2105 while Present (Gen_Parm_Decl) loop
2106 Analyze (Gen_Parm_Decl);
2107 Next (Gen_Parm_Decl);
2110 Generate_Reference_To_Generic_Formals (Current_Scope);
2111 end Analyze_Generic_Formal_Part;
2113 ------------------------------------------
2114 -- Analyze_Generic_Package_Declaration --
2115 ------------------------------------------
2117 procedure Analyze_Generic_Package_Declaration (N : Node_Id) is
2118 Loc : constant Source_Ptr := Sloc (N);
2121 Save_Parent : Node_Id;
2123 Decls : constant List_Id :=
2124 Visible_Declarations (Specification (N));
2128 -- We introduce a renaming of the enclosing package, to have a usable
2129 -- entity as the prefix of an expanded name for a local entity of the
2130 -- form Par.P.Q, where P is the generic package. This is because a local
2131 -- entity named P may hide it, so that the usual visibility rules in
2132 -- the instance will not resolve properly.
2135 Make_Package_Renaming_Declaration (Loc,
2136 Defining_Unit_Name =>
2137 Make_Defining_Identifier (Loc,
2138 Chars => New_External_Name (Chars (Defining_Entity (N)), "GH")),
2139 Name => Make_Identifier (Loc, Chars (Defining_Entity (N))));
2141 if Present (Decls) then
2142 Decl := First (Decls);
2143 while Present (Decl)
2144 and then Nkind (Decl) = N_Pragma
2149 if Present (Decl) then
2150 Insert_Before (Decl, Renaming);
2152 Append (Renaming, Visible_Declarations (Specification (N)));
2156 Set_Visible_Declarations (Specification (N), New_List (Renaming));
2159 -- Create copy of generic unit, and save for instantiation.
2160 -- If the unit is a child unit, do not copy the specifications
2161 -- for the parent, which are not part of the generic tree.
2163 Save_Parent := Parent_Spec (N);
2164 Set_Parent_Spec (N, Empty);
2166 New_N := Copy_Generic_Node (N, Empty, Instantiating => False);
2167 Set_Parent_Spec (New_N, Save_Parent);
2169 Id := Defining_Entity (N);
2170 Generate_Definition (Id);
2172 -- Expansion is not applied to generic units
2177 Set_Ekind (Id, E_Generic_Package);
2178 Set_Etype (Id, Standard_Void_Type);
2180 Enter_Generic_Scope (Id);
2181 Set_Inner_Instances (Id, New_Elmt_List);
2183 Set_Categorization_From_Pragmas (N);
2184 Set_Is_Pure (Id, Is_Pure (Current_Scope));
2186 -- Link the declaration of the generic homonym in the generic copy
2187 -- to the package it renames, so that it is always resolved properly.
2189 Set_Generic_Homonym (Id, Defining_Unit_Name (Renaming));
2190 Set_Entity (Associated_Node (Name (Renaming)), Id);
2192 -- For a library unit, we have reconstructed the entity for the
2193 -- unit, and must reset it in the library tables.
2195 if Nkind (Parent (N)) = N_Compilation_Unit then
2196 Set_Cunit_Entity (Current_Sem_Unit, Id);
2199 Analyze_Generic_Formal_Part (N);
2201 -- After processing the generic formals, analysis proceeds
2202 -- as for a non-generic package.
2204 Analyze (Specification (N));
2206 Validate_Categorization_Dependency (N, Id);
2210 End_Package_Scope (Id);
2211 Exit_Generic_Scope (Id);
2213 if Nkind (Parent (N)) /= N_Compilation_Unit then
2214 Move_Freeze_Nodes (Id, N, Visible_Declarations (Specification (N)));
2215 Move_Freeze_Nodes (Id, N, Private_Declarations (Specification (N)));
2216 Move_Freeze_Nodes (Id, N, Generic_Formal_Declarations (N));
2219 Set_Body_Required (Parent (N), Unit_Requires_Body (Id));
2220 Validate_RT_RAT_Component (N);
2222 -- If this is a spec without a body, check that generic parameters
2225 if not Body_Required (Parent (N)) then
2226 Check_References (Id);
2229 end Analyze_Generic_Package_Declaration;
2231 --------------------------------------------
2232 -- Analyze_Generic_Subprogram_Declaration --
2233 --------------------------------------------
2235 procedure Analyze_Generic_Subprogram_Declaration (N : Node_Id) is
2240 Save_Parent : Node_Id;
2243 -- Create copy of generic unit,and save for instantiation.
2244 -- If the unit is a child unit, do not copy the specifications
2245 -- for the parent, which are not part of the generic tree.
2247 Save_Parent := Parent_Spec (N);
2248 Set_Parent_Spec (N, Empty);
2250 New_N := Copy_Generic_Node (N, Empty, Instantiating => False);
2251 Set_Parent_Spec (New_N, Save_Parent);
2254 Spec := Specification (N);
2255 Id := Defining_Entity (Spec);
2256 Generate_Definition (Id);
2258 if Nkind (Id) = N_Defining_Operator_Symbol then
2260 ("operator symbol not allowed for generic subprogram", Id);
2267 Set_Scope_Depth_Value (Id, Scope_Depth (Current_Scope) + 1);
2269 Enter_Generic_Scope (Id);
2270 Set_Inner_Instances (Id, New_Elmt_List);
2271 Set_Is_Pure (Id, Is_Pure (Current_Scope));
2273 Analyze_Generic_Formal_Part (N);
2275 Formals := Parameter_Specifications (Spec);
2277 if Present (Formals) then
2278 Process_Formals (Formals, Spec);
2281 if Nkind (Spec) = N_Function_Specification then
2282 Set_Ekind (Id, E_Generic_Function);
2283 Find_Type (Subtype_Mark (Spec));
2284 Set_Etype (Id, Entity (Subtype_Mark (Spec)));
2286 Set_Ekind (Id, E_Generic_Procedure);
2287 Set_Etype (Id, Standard_Void_Type);
2290 -- For a library unit, we have reconstructed the entity for the
2291 -- unit, and must reset it in the library tables. We also need
2292 -- to make sure that Body_Required is set properly in the original
2293 -- compilation unit node.
2295 if Nkind (Parent (N)) = N_Compilation_Unit then
2296 Set_Cunit_Entity (Current_Sem_Unit, Id);
2297 Set_Body_Required (Parent (N), Unit_Requires_Body (Id));
2300 Set_Categorization_From_Pragmas (N);
2301 Validate_Categorization_Dependency (N, Id);
2303 Save_Global_References (Original_Node (N));
2307 Exit_Generic_Scope (Id);
2308 Generate_Reference_To_Formals (Id);
2309 end Analyze_Generic_Subprogram_Declaration;
2311 -----------------------------------
2312 -- Analyze_Package_Instantiation --
2313 -----------------------------------
2315 -- Note: this procedure is also used for formal package declarations,
2316 -- in which case the argument N is an N_Formal_Package_Declaration
2317 -- node. This should really be noted in the spec! ???
2319 procedure Analyze_Package_Instantiation (N : Node_Id) is
2320 Loc : constant Source_Ptr := Sloc (N);
2321 Gen_Id : constant Node_Id := Name (N);
2324 Act_Decl_Name : Node_Id;
2325 Act_Decl_Id : Entity_Id;
2330 Gen_Unit : Entity_Id;
2332 Is_Actual_Pack : constant Boolean :=
2333 Is_Internal (Defining_Entity (N));
2335 Parent_Installed : Boolean := False;
2336 Renaming_List : List_Id;
2337 Unit_Renaming : Node_Id;
2338 Needs_Body : Boolean;
2339 Inline_Now : Boolean := False;
2341 procedure Delay_Descriptors (E : Entity_Id);
2342 -- Delay generation of subprogram descriptors for given entity
2344 function Might_Inline_Subp return Boolean;
2345 -- If inlining is active and the generic contains inlined subprograms,
2346 -- we instantiate the body. This may cause superfluous instantiations,
2347 -- but it is simpler than detecting the need for the body at the point
2348 -- of inlining, when the context of the instance is not available.
2350 -----------------------
2351 -- Delay_Descriptors --
2352 -----------------------
2354 procedure Delay_Descriptors (E : Entity_Id) is
2356 if not Delay_Subprogram_Descriptors (E) then
2357 Set_Delay_Subprogram_Descriptors (E);
2358 Pending_Descriptor.Increment_Last;
2359 Pending_Descriptor.Table (Pending_Descriptor.Last) := E;
2361 end Delay_Descriptors;
2363 -----------------------
2364 -- Might_Inline_Subp --
2365 -----------------------
2367 function Might_Inline_Subp return Boolean is
2371 if not Inline_Processing_Required then
2375 E := First_Entity (Gen_Unit);
2376 while Present (E) loop
2377 if Is_Subprogram (E)
2378 and then Is_Inlined (E)
2388 end Might_Inline_Subp;
2390 -- Start of processing for Analyze_Package_Instantiation
2393 -- Very first thing: apply the special kludge for Text_IO processing
2394 -- in case we are instantiating one of the children of [Wide_]Text_IO.
2396 Text_IO_Kludge (Name (N));
2398 -- Make node global for error reporting
2400 Instantiation_Node := N;
2402 -- Case of instantiation of a generic package
2404 if Nkind (N) = N_Package_Instantiation then
2405 Act_Decl_Id := New_Copy (Defining_Entity (N));
2406 Set_Comes_From_Source (Act_Decl_Id, True);
2408 if Nkind (Defining_Unit_Name (N)) = N_Defining_Program_Unit_Name then
2410 Make_Defining_Program_Unit_Name (Loc,
2411 Name => New_Copy_Tree (Name (Defining_Unit_Name (N))),
2412 Defining_Identifier => Act_Decl_Id);
2414 Act_Decl_Name := Act_Decl_Id;
2417 -- Case of instantiation of a formal package
2420 Act_Decl_Id := Defining_Identifier (N);
2421 Act_Decl_Name := Act_Decl_Id;
2424 Generate_Definition (Act_Decl_Id);
2425 Pre_Analyze_Actuals (N);
2428 Check_Generic_Child_Unit (Gen_Id, Parent_Installed);
2429 Gen_Unit := Entity (Gen_Id);
2431 -- Verify that it is the name of a generic package
2433 if Etype (Gen_Unit) = Any_Type then
2437 elsif Ekind (Gen_Unit) /= E_Generic_Package then
2439 -- Ada 2005 (AI-50217): Cannot use instance in limited with_clause
2441 if From_With_Type (Gen_Unit) then
2443 ("cannot instantiate a limited withed package", Gen_Id);
2446 ("expect name of generic package in instantiation", Gen_Id);
2453 if In_Extended_Main_Source_Unit (N) then
2454 Set_Is_Instantiated (Gen_Unit);
2455 Generate_Reference (Gen_Unit, N);
2457 if Present (Renamed_Object (Gen_Unit)) then
2458 Set_Is_Instantiated (Renamed_Object (Gen_Unit));
2459 Generate_Reference (Renamed_Object (Gen_Unit), N);
2463 if Nkind (Gen_Id) = N_Identifier
2464 and then Chars (Gen_Unit) = Chars (Defining_Entity (N))
2467 ("& is hidden within declaration of instance", Gen_Id, Gen_Unit);
2469 elsif Nkind (Gen_Id) = N_Expanded_Name
2470 and then Is_Child_Unit (Gen_Unit)
2471 and then Nkind (Prefix (Gen_Id)) = N_Identifier
2472 and then Chars (Act_Decl_Id) = Chars (Prefix (Gen_Id))
2475 ("& is hidden within declaration of instance ", Prefix (Gen_Id));
2478 Set_Entity (Gen_Id, Gen_Unit);
2480 -- If generic is a renaming, get original generic unit
2482 if Present (Renamed_Object (Gen_Unit))
2483 and then Ekind (Renamed_Object (Gen_Unit)) = E_Generic_Package
2485 Gen_Unit := Renamed_Object (Gen_Unit);
2488 -- Verify that there are no circular instantiations
2490 if In_Open_Scopes (Gen_Unit) then
2491 Error_Msg_NE ("instantiation of & within itself", N, Gen_Unit);
2495 elsif Contains_Instance_Of (Gen_Unit, Current_Scope, Gen_Id) then
2496 Error_Msg_Node_2 := Current_Scope;
2498 ("circular Instantiation: & instantiated in &!", N, Gen_Unit);
2499 Circularity_Detected := True;
2504 Set_Instance_Env (Gen_Unit, Act_Decl_Id);
2505 Gen_Decl := Unit_Declaration_Node (Gen_Unit);
2507 -- Initialize renamings map, for error checking, and the list
2508 -- that holds private entities whose views have changed between
2509 -- generic definition and instantiation. If this is the instance
2510 -- created to validate an actual package, the instantiation
2511 -- environment is that of the enclosing instance.
2513 Generic_Renamings.Set_Last (0);
2514 Generic_Renamings_HTable.Reset;
2516 Create_Instantiation_Source (N, Gen_Unit, False, S_Adjustment);
2518 -- Copy original generic tree, to produce text for instantiation
2522 (Original_Node (Gen_Decl), Empty, Instantiating => True);
2524 Act_Spec := Specification (Act_Tree);
2526 -- If this is the instance created to validate an actual package,
2527 -- only the formals matter, do not examine the package spec itself.
2529 if Is_Actual_Pack then
2530 Set_Visible_Declarations (Act_Spec, New_List);
2531 Set_Private_Declarations (Act_Spec, New_List);
2535 Analyze_Associations
2537 Generic_Formal_Declarations (Act_Tree),
2538 Generic_Formal_Declarations (Gen_Decl));
2540 Set_Defining_Unit_Name (Act_Spec, Act_Decl_Name);
2541 Set_Is_Generic_Instance (Act_Decl_Id);
2543 Set_Generic_Parent (Act_Spec, Gen_Unit);
2545 -- References to the generic in its own declaration or its body
2546 -- are references to the instance. Add a renaming declaration for
2547 -- the generic unit itself. This declaration, as well as the renaming
2548 -- declarations for the generic formals, must remain private to the
2549 -- unit: the formals, because this is the language semantics, and
2550 -- the unit because its use is an artifact of the implementation.
2553 Make_Package_Renaming_Declaration (Loc,
2554 Defining_Unit_Name =>
2555 Make_Defining_Identifier (Loc, Chars (Gen_Unit)),
2556 Name => New_Reference_To (Act_Decl_Id, Loc));
2558 Append (Unit_Renaming, Renaming_List);
2560 -- The renaming declarations are the first local declarations of
2563 if Is_Non_Empty_List (Visible_Declarations (Act_Spec)) then
2565 (First (Visible_Declarations (Act_Spec)), Renaming_List);
2567 Set_Visible_Declarations (Act_Spec, Renaming_List);
2571 Make_Package_Declaration (Loc,
2572 Specification => Act_Spec);
2574 -- Save the instantiation node, for subsequent instantiation
2575 -- of the body, if there is one and we are generating code for
2576 -- the current unit. Mark the unit as having a body, to avoid
2577 -- a premature error message.
2579 -- We instantiate the body if we are generating code, if we are
2580 -- generating cross-reference information, or if we are building
2581 -- trees for ASIS use.
2584 Enclosing_Body_Present : Boolean := False;
2585 -- If the generic unit is not a compilation unit, then a body
2586 -- may be present in its parent even if none is required. We
2587 -- create a tentative pending instantiation for the body, which
2588 -- will be discarded if none is actually present.
2593 if Scope (Gen_Unit) /= Standard_Standard
2594 and then not Is_Child_Unit (Gen_Unit)
2596 Scop := Scope (Gen_Unit);
2598 while Present (Scop)
2599 and then Scop /= Standard_Standard
2601 if Unit_Requires_Body (Scop) then
2602 Enclosing_Body_Present := True;
2605 elsif In_Open_Scopes (Scop)
2606 and then In_Package_Body (Scop)
2608 Enclosing_Body_Present := True;
2612 exit when Is_Compilation_Unit (Scop);
2613 Scop := Scope (Scop);
2617 -- If front-end inlining is enabled, and this is a unit for which
2618 -- code will be generated, we instantiate the body at once.
2619 -- This is done if the instance is not the main unit, and if the
2620 -- generic is not a child unit of another generic, to avoid scope
2621 -- problems and the reinstallation of parent instances.
2623 if Front_End_Inlining
2624 and then Expander_Active
2625 and then (not Is_Child_Unit (Gen_Unit)
2626 or else not Is_Generic_Unit (Scope (Gen_Unit)))
2627 and then (Is_In_Main_Unit (N)
2628 or else In_Main_Context (Current_Scope))
2629 and then Nkind (Parent (N)) /= N_Compilation_Unit
2630 and then Might_Inline_Subp
2631 and then not Is_Actual_Pack
2637 (Unit_Requires_Body (Gen_Unit)
2638 or else Enclosing_Body_Present
2639 or else Present (Corresponding_Body (Gen_Decl)))
2640 and then (Is_In_Main_Unit (N)
2641 or else Might_Inline_Subp)
2642 and then not Is_Actual_Pack
2643 and then not Inline_Now
2645 and then (Operating_Mode = Generate_Code
2646 or else (Operating_Mode = Check_Semantics
2647 and then ASIS_Mode));
2649 -- If front_end_inlining is enabled, do not instantiate a
2650 -- body if within a generic context.
2652 if (Front_End_Inlining
2653 and then not Expander_Active)
2654 or else Is_Generic_Unit (Cunit_Entity (Main_Unit))
2656 Needs_Body := False;
2659 -- If the current context is generic, and the package being
2660 -- instantiated is declared within a formal package, there
2661 -- is no body to instantiate until the enclosing generic is
2662 -- instantiated, and there is an actual for the formal
2663 -- package. If the formal package has parameters, we build a
2664 -- regular package instance for it, that preceeds the original
2665 -- formal package declaration.
2667 if In_Open_Scopes (Scope (Scope (Gen_Unit))) then
2669 Decl : constant Node_Id :=
2671 (Unit_Declaration_Node (Scope (Gen_Unit)));
2673 if Nkind (Decl) = N_Formal_Package_Declaration
2674 or else (Nkind (Decl) = N_Package_Declaration
2675 and then Is_List_Member (Decl)
2676 and then Present (Next (Decl))
2678 Nkind (Next (Decl)) = N_Formal_Package_Declaration)
2680 Needs_Body := False;
2686 -- If we are generating the calling stubs from the instantiation
2687 -- of a generic RCI package, we will not use the body of the
2690 if Distribution_Stub_Mode = Generate_Caller_Stub_Body
2691 and then Is_Compilation_Unit (Defining_Entity (N))
2693 Needs_Body := False;
2698 -- Here is a defence against a ludicrous number of instantiations
2699 -- caused by a circular set of instantiation attempts.
2701 if Pending_Instantiations.Last >
2702 Hostparm.Max_Instantiations
2704 Error_Msg_N ("too many instantiations", N);
2705 raise Unrecoverable_Error;
2708 -- Indicate that the enclosing scopes contain an instantiation,
2709 -- and that cleanup actions should be delayed until after the
2710 -- instance body is expanded.
2712 Check_Forward_Instantiation (Gen_Decl);
2713 if Nkind (N) = N_Package_Instantiation then
2715 Enclosing_Master : Entity_Id := Current_Scope;
2718 while Enclosing_Master /= Standard_Standard loop
2720 if Ekind (Enclosing_Master) = E_Package then
2721 if Is_Compilation_Unit (Enclosing_Master) then
2722 if In_Package_Body (Enclosing_Master) then
2724 (Body_Entity (Enclosing_Master));
2733 Enclosing_Master := Scope (Enclosing_Master);
2736 elsif Ekind (Enclosing_Master) = E_Generic_Package then
2737 Enclosing_Master := Scope (Enclosing_Master);
2739 elsif Is_Generic_Subprogram (Enclosing_Master)
2740 or else Ekind (Enclosing_Master) = E_Void
2742 -- Cleanup actions will eventually be performed on
2743 -- the enclosing instance, if any. enclosing scope
2744 -- is void in the formal part of a generic subp.
2749 if Ekind (Enclosing_Master) = E_Entry
2751 Ekind (Scope (Enclosing_Master)) = E_Protected_Type
2754 Protected_Body_Subprogram (Enclosing_Master);
2757 Set_Delay_Cleanups (Enclosing_Master);
2759 while Ekind (Enclosing_Master) = E_Block loop
2760 Enclosing_Master := Scope (Enclosing_Master);
2763 if Is_Subprogram (Enclosing_Master) then
2764 Delay_Descriptors (Enclosing_Master);
2766 elsif Is_Task_Type (Enclosing_Master) then
2768 TBP : constant Node_Id :=
2769 Get_Task_Body_Procedure
2773 if Present (TBP) then
2774 Delay_Descriptors (TBP);
2775 Set_Delay_Cleanups (TBP);
2785 -- Make entry in table
2787 Pending_Instantiations.Increment_Last;
2788 Pending_Instantiations.Table (Pending_Instantiations.Last) :=
2789 (N, Act_Decl, Expander_Active, Current_Sem_Unit);
2793 Set_Categorization_From_Pragmas (Act_Decl);
2795 if Parent_Installed then
2799 Set_Instance_Spec (N, Act_Decl);
2801 -- If not a compilation unit, insert the package declaration
2802 -- before the original instantiation node.
2804 if Nkind (Parent (N)) /= N_Compilation_Unit then
2805 Mark_Rewrite_Insertion (Act_Decl);
2806 Insert_Before (N, Act_Decl);
2809 -- For an instantiation that is a compilation unit, place
2810 -- declaration on current node so context is complete
2811 -- for analysis (including nested instantiations). It this
2812 -- is the main unit, the declaration eventually replaces the
2813 -- instantiation node. If the instance body is later created, it
2814 -- replaces the instance node, and the declation is attached to
2815 -- it (see Build_Instance_Compilation_Unit_Nodes).
2818 if Cunit_Entity (Current_Sem_Unit) = Defining_Entity (N) then
2820 -- The entity for the current unit is the newly created one,
2821 -- and all semantic information is attached to it.
2823 Set_Cunit_Entity (Current_Sem_Unit, Act_Decl_Id);
2825 -- If this is the main unit, replace the main entity as well
2827 if Current_Sem_Unit = Main_Unit then
2828 Main_Unit_Entity := Act_Decl_Id;
2832 -- There is a problem with inlining here.
2834 Set_Unit (Parent (N), Act_Decl);
2835 Set_Parent_Spec (Act_Decl, Parent_Spec (N));
2837 Set_Unit (Parent (N), N);
2838 Set_Body_Required (Parent (N), False);
2840 -- We never need elaboration checks on instantiations, since
2841 -- by definition, the body instantiation is elaborated at the
2842 -- same time as the spec instantiation.
2844 Set_Suppress_Elaboration_Warnings (Act_Decl_Id);
2845 Set_Kill_Elaboration_Checks (Act_Decl_Id);
2848 Check_Elab_Instantiation (N);
2850 if ABE_Is_Certain (N) and then Needs_Body then
2851 Pending_Instantiations.Decrement_Last;
2853 Check_Hidden_Child_Unit (N, Gen_Unit, Act_Decl_Id);
2855 Set_First_Private_Entity (Defining_Unit_Name (Unit_Renaming),
2856 First_Private_Entity (Act_Decl_Id));
2858 -- If the instantiation will receive a body, the unit will
2859 -- be transformed into a package body, and receive its own
2860 -- elaboration entity. Otherwise, the nature of the unit is
2861 -- now a package declaration.
2863 if Nkind (Parent (N)) = N_Compilation_Unit
2864 and then not Needs_Body
2866 Rewrite (N, Act_Decl);
2869 if Present (Corresponding_Body (Gen_Decl))
2870 or else Unit_Requires_Body (Gen_Unit)
2872 Set_Has_Completion (Act_Decl_Id);
2875 Check_Formal_Packages (Act_Decl_Id);
2877 Restore_Private_Views (Act_Decl_Id);
2879 if not Generic_Separately_Compiled (Gen_Unit) then
2880 Inherit_Context (Gen_Decl, N);
2883 if Parent_Installed then
2890 Validate_Categorization_Dependency (N, Act_Decl_Id);
2892 -- Check restriction, but skip this if something went wrong in
2893 -- the above analysis, indicated by Act_Decl_Id being void.
2895 if Ekind (Act_Decl_Id) /= E_Void
2896 and then not Is_Library_Level_Entity (Act_Decl_Id)
2898 Check_Restriction (No_Local_Allocators, N);
2902 Inline_Instance_Body (N, Gen_Unit, Act_Decl);
2905 -- The following is a tree patch for ASIS: ASIS needs separate nodes
2906 -- to be used as defining identifiers for a formal package and for the
2907 -- corresponding expanded package
2909 if Nkind (N) = N_Formal_Package_Declaration then
2910 Act_Decl_Id := New_Copy (Defining_Entity (N));
2911 Set_Comes_From_Source (Act_Decl_Id, True);
2912 Set_Is_Generic_Instance (Act_Decl_Id, False);
2913 Set_Defining_Identifier (N, Act_Decl_Id);
2917 when Instantiation_Error =>
2918 if Parent_Installed then
2921 end Analyze_Package_Instantiation;
2923 --------------------------
2924 -- Inline_Instance_Body --
2925 --------------------------
2927 procedure Inline_Instance_Body
2929 Gen_Unit : Entity_Id;
2933 Gen_Comp : constant Entity_Id :=
2934 Cunit_Entity (Get_Source_Unit (Gen_Unit));
2935 Curr_Comp : constant Node_Id := Cunit (Current_Sem_Unit);
2936 Curr_Scope : Entity_Id := Empty;
2937 Curr_Unit : constant Entity_Id :=
2938 Cunit_Entity (Current_Sem_Unit);
2939 Removed : Boolean := False;
2940 Num_Scopes : Int := 0;
2941 Use_Clauses : array (1 .. Scope_Stack.Last) of Node_Id;
2942 Instances : array (1 .. Scope_Stack.Last) of Entity_Id;
2943 Inner_Scopes : array (1 .. Scope_Stack.Last) of Entity_Id;
2944 Num_Inner : Int := 0;
2945 N_Instances : Int := 0;
2949 -- Case of generic unit defined in another unit. We must remove
2950 -- the complete context of the current unit to install that of
2953 if Gen_Comp /= Cunit_Entity (Current_Sem_Unit) then
2957 and then S /= Standard_Standard
2959 Num_Scopes := Num_Scopes + 1;
2961 Use_Clauses (Num_Scopes) :=
2963 (Scope_Stack.Last - Num_Scopes + 1).
2965 End_Use_Clauses (Use_Clauses (Num_Scopes));
2967 exit when Is_Generic_Instance (S)
2968 and then (In_Package_Body (S)
2969 or else Ekind (S) = E_Procedure
2970 or else Ekind (S) = E_Function);
2974 Vis := Is_Immediately_Visible (Gen_Comp);
2976 -- Find and save all enclosing instances
2981 and then S /= Standard_Standard
2983 if Is_Generic_Instance (S) then
2984 N_Instances := N_Instances + 1;
2985 Instances (N_Instances) := S;
2987 exit when In_Package_Body (S);
2993 -- Remove context of current compilation unit, unless we
2994 -- are within a nested package instantiation, in which case
2995 -- the context has been removed previously.
2997 -- If current scope is the body of a child unit, remove context
3003 and then S /= Standard_Standard
3005 exit when Is_Generic_Instance (S)
3006 and then (In_Package_Body (S)
3007 or else Ekind (S) = E_Procedure
3008 or else Ekind (S) = E_Function);
3011 or else (Ekind (Curr_Unit) = E_Package_Body
3012 and then S = Spec_Entity (Curr_Unit))
3013 or else (Ekind (Curr_Unit) = E_Subprogram_Body
3016 (Unit_Declaration_Node (Curr_Unit)))
3020 -- Remove entities in current scopes from visibility, so
3021 -- than instance body is compiled in a clean environment.
3023 Save_Scope_Stack (Handle_Use => False);
3025 if Is_Child_Unit (S) then
3027 -- Remove child unit from stack, as well as inner scopes.
3028 -- Removing the context of a child unit removes parent
3031 while Current_Scope /= S loop
3032 Num_Inner := Num_Inner + 1;
3033 Inner_Scopes (Num_Inner) := Current_Scope;
3038 Remove_Context (Curr_Comp);
3042 Remove_Context (Curr_Comp);
3045 if Ekind (Curr_Unit) = E_Package_Body then
3046 Remove_Context (Library_Unit (Curr_Comp));
3053 New_Scope (Standard_Standard);
3054 Scope_Stack.Table (Scope_Stack.Last).Is_Active_Stack_Base := True;
3055 Instantiate_Package_Body
3056 ((N, Act_Decl, Expander_Active, Current_Sem_Unit), True);
3061 Set_Is_Immediately_Visible (Gen_Comp, Vis);
3063 -- Reset Generic_Instance flag so that use clauses can be installed
3064 -- in the proper order. (See Use_One_Package for effect of enclosing
3065 -- instances on processing of use clauses).
3067 for J in 1 .. N_Instances loop
3068 Set_Is_Generic_Instance (Instances (J), False);
3072 Install_Context (Curr_Comp);
3074 if Present (Curr_Scope)
3075 and then Is_Child_Unit (Curr_Scope)
3077 New_Scope (Curr_Scope);
3078 Set_Is_Immediately_Visible (Curr_Scope);
3080 -- Finally, restore inner scopes as well
3082 for J in reverse 1 .. Num_Inner loop
3083 New_Scope (Inner_Scopes (J));
3087 Restore_Scope_Stack (Handle_Use => False);
3089 if Present (Curr_Scope)
3091 (In_Private_Part (Curr_Scope)
3092 or else In_Package_Body (Curr_Scope))
3094 -- Install private declaration of ancestor units, which
3095 -- are currently available. Restore_Scope_Stack and
3096 -- Install_Context only install the visible part of parents.
3101 Par := Scope (Curr_Scope);
3102 while (Present (Par))
3103 and then Par /= Standard_Standard
3105 Install_Private_Declarations (Par);
3112 -- Restore use clauses. For a child unit, use clauses in the parents
3113 -- are restored when installing the context, so only those in inner
3114 -- scopes (and those local to the child unit itself) need to be
3115 -- installed explicitly.
3117 if Is_Child_Unit (Curr_Unit)
3120 for J in reverse 1 .. Num_Inner + 1 loop
3121 Scope_Stack.Table (Scope_Stack.Last - J + 1).First_Use_Clause :=
3123 Install_Use_Clauses (Use_Clauses (J));
3127 for J in reverse 1 .. Num_Scopes loop
3128 Scope_Stack.Table (Scope_Stack.Last - J + 1).First_Use_Clause :=
3130 Install_Use_Clauses (Use_Clauses (J));
3134 for J in 1 .. N_Instances loop
3135 Set_Is_Generic_Instance (Instances (J), True);
3138 -- If generic unit is in current unit, current context is correct
3141 Instantiate_Package_Body
3142 ((N, Act_Decl, Expander_Active, Current_Sem_Unit), True);
3144 end Inline_Instance_Body;
3146 -------------------------------------
3147 -- Analyze_Procedure_Instantiation --
3148 -------------------------------------
3150 procedure Analyze_Procedure_Instantiation (N : Node_Id) is
3152 Analyze_Subprogram_Instantiation (N, E_Procedure);
3153 end Analyze_Procedure_Instantiation;
3155 --------------------------------------
3156 -- Analyze_Subprogram_Instantiation --
3157 --------------------------------------
3159 procedure Analyze_Subprogram_Instantiation
3163 Loc : constant Source_Ptr := Sloc (N);
3164 Gen_Id : constant Node_Id := Name (N);
3166 Anon_Id : constant Entity_Id :=
3167 Make_Defining_Identifier (Sloc (Defining_Entity (N)),
3168 Chars => New_External_Name
3169 (Chars (Defining_Entity (N)), 'R'));
3171 Act_Decl_Id : Entity_Id;
3176 Gen_Unit : Entity_Id;
3178 Pack_Id : Entity_Id;
3179 Parent_Installed : Boolean := False;
3180 Renaming_List : List_Id;
3182 procedure Analyze_Instance_And_Renamings;
3183 -- The instance must be analyzed in a context that includes the
3184 -- mappings of generic parameters into actuals. We create a package
3185 -- declaration for this purpose, and a subprogram with an internal
3186 -- name within the package. The subprogram instance is simply an
3187 -- alias for the internal subprogram, declared in the current scope.
3189 ------------------------------------
3190 -- Analyze_Instance_And_Renamings --
3191 ------------------------------------
3193 procedure Analyze_Instance_And_Renamings is
3194 Def_Ent : constant Entity_Id := Defining_Entity (N);
3195 Pack_Decl : Node_Id;
3198 if Nkind (Parent (N)) = N_Compilation_Unit then
3200 -- For the case of a compilation unit, the container package
3201 -- has the same name as the instantiation, to insure that the
3202 -- binder calls the elaboration procedure with the right name.
3203 -- Copy the entity of the instance, which may have compilation
3204 -- level flags (e.g. Is_Child_Unit) set.
3206 Pack_Id := New_Copy (Def_Ent);
3209 -- Otherwise we use the name of the instantiation concatenated
3210 -- with its source position to ensure uniqueness if there are
3211 -- several instantiations with the same name.
3214 Make_Defining_Identifier (Loc,
3215 Chars => New_External_Name
3216 (Related_Id => Chars (Def_Ent),
3218 Suffix_Index => Source_Offset (Sloc (Def_Ent))));
3221 Pack_Decl := Make_Package_Declaration (Loc,
3222 Specification => Make_Package_Specification (Loc,
3223 Defining_Unit_Name => Pack_Id,
3224 Visible_Declarations => Renaming_List,
3225 End_Label => Empty));
3227 Set_Instance_Spec (N, Pack_Decl);
3228 Set_Is_Generic_Instance (Pack_Id);
3229 Set_Needs_Debug_Info (Pack_Id);
3231 -- Case of not a compilation unit
3233 if Nkind (Parent (N)) /= N_Compilation_Unit then
3234 Mark_Rewrite_Insertion (Pack_Decl);
3235 Insert_Before (N, Pack_Decl);
3236 Set_Has_Completion (Pack_Id);
3238 -- Case of an instantiation that is a compilation unit
3240 -- Place declaration on current node so context is complete
3241 -- for analysis (including nested instantiations), and for
3242 -- use in a context_clause (see Analyze_With_Clause).
3245 Set_Unit (Parent (N), Pack_Decl);
3246 Set_Parent_Spec (Pack_Decl, Parent_Spec (N));
3249 Analyze (Pack_Decl);
3250 Check_Formal_Packages (Pack_Id);
3251 Set_Is_Generic_Instance (Pack_Id, False);
3253 -- Body of the enclosing package is supplied when instantiating
3254 -- the subprogram body, after semantic analysis is completed.
3256 if Nkind (Parent (N)) = N_Compilation_Unit then
3258 -- Remove package itself from visibility, so it does not
3259 -- conflict with subprogram.
3261 Set_Name_Entity_Id (Chars (Pack_Id), Homonym (Pack_Id));
3263 -- Set name and scope of internal subprogram so that the
3264 -- proper external name will be generated. The proper scope
3265 -- is the scope of the wrapper package. We need to generate
3266 -- debugging information for the internal subprogram, so set
3267 -- flag accordingly.
3269 Set_Chars (Anon_Id, Chars (Defining_Entity (N)));
3270 Set_Scope (Anon_Id, Scope (Pack_Id));
3272 -- Mark wrapper package as referenced, to avoid spurious
3273 -- warnings if the instantiation appears in various with_
3274 -- clauses of subunits of the main unit.
3276 Set_Referenced (Pack_Id);
3279 Set_Is_Generic_Instance (Anon_Id);
3280 Set_Needs_Debug_Info (Anon_Id);
3281 Act_Decl_Id := New_Copy (Anon_Id);
3283 Set_Parent (Act_Decl_Id, Parent (Anon_Id));
3284 Set_Chars (Act_Decl_Id, Chars (Defining_Entity (N)));
3285 Set_Sloc (Act_Decl_Id, Sloc (Defining_Entity (N)));
3286 Set_Comes_From_Source (Act_Decl_Id, True);
3288 -- The signature may involve types that are not frozen yet, but
3289 -- the subprogram will be frozen at the point the wrapper package
3290 -- is frozen, so it does not need its own freeze node. In fact, if
3291 -- one is created, it might conflict with the freezing actions from
3292 -- the wrapper package (see 7206-013).
3294 Set_Has_Delayed_Freeze (Anon_Id, False);
3296 -- If the instance is a child unit, mark the Id accordingly. Mark
3297 -- the anonymous entity as well, which is the real subprogram and
3298 -- which is used when the instance appears in a context clause.
3300 Set_Is_Child_Unit (Act_Decl_Id, Is_Child_Unit (Defining_Entity (N)));
3301 Set_Is_Child_Unit (Anon_Id, Is_Child_Unit (Defining_Entity (N)));
3302 New_Overloaded_Entity (Act_Decl_Id);
3303 Check_Eliminated (Act_Decl_Id);
3305 -- In compilation unit case, kill elaboration checks on the
3306 -- instantiation, since they are never needed -- the body is
3307 -- instantiated at the same point as the spec.
3309 if Nkind (Parent (N)) = N_Compilation_Unit then
3310 Set_Suppress_Elaboration_Warnings (Act_Decl_Id);
3311 Set_Kill_Elaboration_Checks (Act_Decl_Id);
3312 Set_Is_Compilation_Unit (Anon_Id);
3314 Set_Cunit_Entity (Current_Sem_Unit, Pack_Id);
3317 -- The instance is not a freezing point for the new subprogram
3319 Set_Is_Frozen (Act_Decl_Id, False);
3321 if Nkind (Defining_Entity (N)) = N_Defining_Operator_Symbol then
3322 Valid_Operator_Definition (Act_Decl_Id);
3325 Set_Alias (Act_Decl_Id, Anon_Id);
3326 Set_Parent (Act_Decl_Id, Parent (Anon_Id));
3327 Set_Has_Completion (Act_Decl_Id);
3328 Set_Related_Instance (Pack_Id, Act_Decl_Id);
3330 if Nkind (Parent (N)) = N_Compilation_Unit then
3331 Set_Body_Required (Parent (N), False);
3334 end Analyze_Instance_And_Renamings;
3336 -- Start of processing for Analyze_Subprogram_Instantiation
3339 -- Very first thing: apply the special kludge for Text_IO processing
3340 -- in case we are instantiating one of the children of [Wide_]Text_IO.
3341 -- Of course such an instantiation is bogus (these are packages, not
3342 -- subprograms), but we get a better error message if we do this.
3344 Text_IO_Kludge (Gen_Id);
3346 -- Make node global for error reporting
3348 Instantiation_Node := N;
3349 Pre_Analyze_Actuals (N);
3352 Check_Generic_Child_Unit (Gen_Id, Parent_Installed);
3353 Gen_Unit := Entity (Gen_Id);
3355 Generate_Reference (Gen_Unit, Gen_Id);
3357 if Nkind (Gen_Id) = N_Identifier
3358 and then Chars (Gen_Unit) = Chars (Defining_Entity (N))
3361 ("& is hidden within declaration of instance", Gen_Id, Gen_Unit);
3364 if Etype (Gen_Unit) = Any_Type then
3369 -- Verify that it is a generic subprogram of the right kind, and that
3370 -- it does not lead to a circular instantiation.
3372 if Ekind (Gen_Unit) /= E_Generic_Procedure
3373 and then Ekind (Gen_Unit) /= E_Generic_Function
3375 Error_Msg_N ("expect generic subprogram in instantiation", Gen_Id);
3377 elsif In_Open_Scopes (Gen_Unit) then
3378 Error_Msg_NE ("instantiation of & within itself", N, Gen_Unit);
3380 elsif K = E_Procedure
3381 and then Ekind (Gen_Unit) /= E_Generic_Procedure
3383 if Ekind (Gen_Unit) = E_Generic_Function then
3385 ("cannot instantiate generic function as procedure", Gen_Id);
3388 ("expect name of generic procedure in instantiation", Gen_Id);
3391 elsif K = E_Function
3392 and then Ekind (Gen_Unit) /= E_Generic_Function
3394 if Ekind (Gen_Unit) = E_Generic_Procedure then
3396 ("cannot instantiate generic procedure as function", Gen_Id);
3399 ("expect name of generic function in instantiation", Gen_Id);
3403 Set_Entity (Gen_Id, Gen_Unit);
3404 Set_Is_Instantiated (Gen_Unit);
3406 if In_Extended_Main_Source_Unit (N) then
3407 Generate_Reference (Gen_Unit, N);
3410 -- If renaming, get original unit
3412 if Present (Renamed_Object (Gen_Unit))
3413 and then (Ekind (Renamed_Object (Gen_Unit)) = E_Generic_Procedure
3415 Ekind (Renamed_Object (Gen_Unit)) = E_Generic_Function)
3417 Gen_Unit := Renamed_Object (Gen_Unit);
3418 Set_Is_Instantiated (Gen_Unit);
3419 Generate_Reference (Gen_Unit, N);
3422 if Contains_Instance_Of (Gen_Unit, Current_Scope, Gen_Id) then
3423 Error_Msg_Node_2 := Current_Scope;
3425 ("circular Instantiation: & instantiated in &!", N, Gen_Unit);
3426 Circularity_Detected := True;
3430 Gen_Decl := Unit_Declaration_Node (Gen_Unit);
3432 -- The subprogram itself cannot contain a nested instance, so
3433 -- the current parent is left empty.
3435 Set_Instance_Env (Gen_Unit, Empty);
3437 -- Initialize renamings map, for error checking
3439 Generic_Renamings.Set_Last (0);
3440 Generic_Renamings_HTable.Reset;
3442 Create_Instantiation_Source (N, Gen_Unit, False, S_Adjustment);
3444 -- Copy original generic tree, to produce text for instantiation
3448 (Original_Node (Gen_Decl), Empty, Instantiating => True);
3450 Act_Spec := Specification (Act_Tree);
3452 Analyze_Associations
3454 Generic_Formal_Declarations (Act_Tree),
3455 Generic_Formal_Declarations (Gen_Decl));
3457 -- Build the subprogram declaration, which does not appear
3458 -- in the generic template, and give it a sloc consistent
3459 -- with that of the template.
3461 Set_Defining_Unit_Name (Act_Spec, Anon_Id);
3462 Set_Generic_Parent (Act_Spec, Gen_Unit);
3464 Make_Subprogram_Declaration (Sloc (Act_Spec),
3465 Specification => Act_Spec);
3467 Set_Categorization_From_Pragmas (Act_Decl);
3469 if Parent_Installed then
3473 Append (Act_Decl, Renaming_List);
3474 Analyze_Instance_And_Renamings;
3476 -- If the generic is marked Import (Intrinsic), then so is the
3477 -- instance. This indicates that there is no body to instantiate.
3478 -- If generic is marked inline, so it the instance, and the
3479 -- anonymous subprogram it renames. If inlined, or else if inlining
3480 -- is enabled for the compilation, we generate the instance body
3481 -- even if it is not within the main unit.
3483 -- Any other pragmas might also be inherited ???
3485 if Is_Intrinsic_Subprogram (Gen_Unit) then
3486 Set_Is_Intrinsic_Subprogram (Anon_Id);
3487 Set_Is_Intrinsic_Subprogram (Act_Decl_Id);
3489 if Chars (Gen_Unit) = Name_Unchecked_Conversion then
3490 Validate_Unchecked_Conversion (N, Act_Decl_Id);
3494 Generate_Definition (Act_Decl_Id);
3496 Set_Is_Inlined (Act_Decl_Id, Is_Inlined (Gen_Unit));
3497 Set_Is_Inlined (Anon_Id, Is_Inlined (Gen_Unit));
3499 if not Is_Intrinsic_Subprogram (Gen_Unit) then
3500 Check_Elab_Instantiation (N);
3503 Check_Hidden_Child_Unit (N, Gen_Unit, Act_Decl_Id);
3505 -- Subject to change, pending on if other pragmas are inherited ???
3507 Validate_Categorization_Dependency (N, Act_Decl_Id);
3509 if not Is_Intrinsic_Subprogram (Act_Decl_Id) then
3511 if not Generic_Separately_Compiled (Gen_Unit) then
3512 Inherit_Context (Gen_Decl, N);
3515 Restore_Private_Views (Pack_Id, False);
3517 -- If the context requires a full instantiation, mark node for
3518 -- subsequent construction of the body.
3520 if (Is_In_Main_Unit (N)
3521 or else Is_Inlined (Act_Decl_Id))
3522 and then (Operating_Mode = Generate_Code
3523 or else (Operating_Mode = Check_Semantics
3524 and then ASIS_Mode))
3525 and then (Expander_Active or else ASIS_Mode)
3526 and then not ABE_Is_Certain (N)
3527 and then not Is_Eliminated (Act_Decl_Id)
3529 Pending_Instantiations.Increment_Last;
3530 Pending_Instantiations.Table (Pending_Instantiations.Last) :=
3531 (N, Act_Decl, Expander_Active, Current_Sem_Unit);
3532 Check_Forward_Instantiation (Gen_Decl);
3534 -- The wrapper package is always delayed, because it does
3535 -- not constitute a freeze point, but to insure that the
3536 -- freeze node is placed properly, it is created directly
3537 -- when instantiating the body (otherwise the freeze node
3538 -- might appear to early for nested instantiations).
3540 elsif Nkind (Parent (N)) = N_Compilation_Unit then
3542 -- For ASIS purposes, indicate that the wrapper package has
3543 -- replaced the instantiation node.
3545 Rewrite (N, Unit (Parent (N)));
3546 Set_Unit (Parent (N), N);
3549 elsif Nkind (Parent (N)) = N_Compilation_Unit then
3551 -- Replace instance node for library-level instantiations
3552 -- of intrinsic subprograms, for ASIS use.
3554 Rewrite (N, Unit (Parent (N)));
3555 Set_Unit (Parent (N), N);
3558 if Parent_Installed then
3563 Generic_Renamings.Set_Last (0);
3564 Generic_Renamings_HTable.Reset;
3568 when Instantiation_Error =>
3569 if Parent_Installed then
3572 end Analyze_Subprogram_Instantiation;
3574 -------------------------
3575 -- Get_Associated_Node --
3576 -------------------------
3578 function Get_Associated_Node (N : Node_Id) return Node_Id is
3579 Assoc : Node_Id := Associated_Node (N);
3582 if Nkind (Assoc) /= Nkind (N) then
3585 elsif Nkind (Assoc) = N_Aggregate
3586 or else Nkind (Assoc) = N_Extension_Aggregate
3591 -- If the node is part of an inner generic, it may itself have been
3592 -- remapped into a further generic copy. Associated_Node is otherwise
3593 -- used for the entity of the node, and will be of a different node
3594 -- kind, or else N has been rewritten as a literal or function call.
3596 while Present (Associated_Node (Assoc))
3597 and then Nkind (Associated_Node (Assoc)) = Nkind (Assoc)
3599 Assoc := Associated_Node (Assoc);
3602 -- Follow and additional link in case the final node was rewritten.
3603 -- This can only happen with nested generic units.
3605 if (Nkind (Assoc) = N_Identifier or else Nkind (Assoc) in N_Op)
3606 and then Present (Associated_Node (Assoc))
3607 and then (Nkind (Associated_Node (Assoc)) = N_Function_Call
3609 Nkind (Associated_Node (Assoc)) = N_Explicit_Dereference
3611 Nkind (Associated_Node (Assoc)) = N_Integer_Literal
3613 Nkind (Associated_Node (Assoc)) = N_Real_Literal
3615 Nkind (Associated_Node (Assoc)) = N_String_Literal)
3617 Assoc := Associated_Node (Assoc);
3622 end Get_Associated_Node;
3624 -------------------------------------------
3625 -- Build_Instance_Compilation_Unit_Nodes --
3626 -------------------------------------------
3628 procedure Build_Instance_Compilation_Unit_Nodes
3633 Decl_Cunit : Node_Id;
3634 Body_Cunit : Node_Id;
3636 New_Main : constant Entity_Id := Defining_Entity (Act_Decl);
3637 Old_Main : constant Entity_Id := Cunit_Entity (Main_Unit);
3640 -- A new compilation unit node is built for the instance declaration
3643 Make_Compilation_Unit (Sloc (N),
3644 Context_Items => Empty_List,
3647 Make_Compilation_Unit_Aux (Sloc (N)));
3649 Set_Parent_Spec (Act_Decl, Parent_Spec (N));
3650 Set_Body_Required (Decl_Cunit, True);
3652 -- We use the original instantiation compilation unit as the resulting
3653 -- compilation unit of the instance, since this is the main unit.
3655 Rewrite (N, Act_Body);
3656 Body_Cunit := Parent (N);
3658 -- The two compilation unit nodes are linked by the Library_Unit field
3660 Set_Library_Unit (Decl_Cunit, Body_Cunit);
3661 Set_Library_Unit (Body_Cunit, Decl_Cunit);
3663 -- Preserve the private nature of the package if needed
3665 Set_Private_Present (Decl_Cunit, Private_Present (Body_Cunit));
3667 -- If the instance is not the main unit, its context, categorization,
3668 -- and elaboration entity are not relevant to the compilation.
3670 if Parent (N) /= Cunit (Main_Unit) then
3674 -- The context clause items on the instantiation, which are now
3675 -- attached to the body compilation unit (since the body overwrote
3676 -- the original instantiation node), semantically belong on the spec,
3677 -- so copy them there. It's harmless to leave them on the body as well.
3678 -- In fact one could argue that they belong in both places.
3680 Citem := First (Context_Items (Body_Cunit));
3681 while Present (Citem) loop
3682 Append (New_Copy (Citem), Context_Items (Decl_Cunit));
3686 -- Propagate categorization flags on packages, so that they appear
3687 -- in ali file for the spec of the unit.
3689 if Ekind (New_Main) = E_Package then
3690 Set_Is_Pure (Old_Main, Is_Pure (New_Main));
3691 Set_Is_Preelaborated (Old_Main, Is_Preelaborated (New_Main));
3692 Set_Is_Remote_Types (Old_Main, Is_Remote_Types (New_Main));
3693 Set_Is_Shared_Passive (Old_Main, Is_Shared_Passive (New_Main));
3694 Set_Is_Remote_Call_Interface
3695 (Old_Main, Is_Remote_Call_Interface (New_Main));
3698 -- Make entry in Units table, so that binder can generate call to
3699 -- elaboration procedure for body, if any.
3701 Make_Instance_Unit (Body_Cunit);
3702 Main_Unit_Entity := New_Main;
3703 Set_Cunit_Entity (Main_Unit, Main_Unit_Entity);
3705 -- Build elaboration entity, since the instance may certainly
3706 -- generate elaboration code requiring a flag for protection.
3708 Build_Elaboration_Entity (Decl_Cunit, New_Main);
3709 end Build_Instance_Compilation_Unit_Nodes;
3711 -----------------------------------
3712 -- Check_Formal_Package_Instance --
3713 -----------------------------------
3715 -- If the formal has specific parameters, they must match those of the
3716 -- actual. Both of them are instances, and the renaming declarations
3717 -- for their formal parameters appear in the same order in both. The
3718 -- analyzed formal has been analyzed in the context of the current
3721 procedure Check_Formal_Package_Instance
3722 (Formal_Pack : Entity_Id;
3723 Actual_Pack : Entity_Id)
3725 E1 : Entity_Id := First_Entity (Actual_Pack);
3726 E2 : Entity_Id := First_Entity (Formal_Pack);
3731 procedure Check_Mismatch (B : Boolean);
3732 -- Common error routine for mismatch between the parameters of
3733 -- the actual instance and those of the formal package.
3735 function Same_Instantiated_Constant (E1, E2 : Entity_Id) return Boolean;
3736 -- The formal may come from a nested formal package, and the actual
3737 -- may have been constant-folded. To determine whether the two denote
3738 -- the same entity we may have to traverse several definitions to
3739 -- recover the ultimate entity that they refer to.
3741 function Same_Instantiated_Variable (E1, E2 : Entity_Id) return Boolean;
3742 -- Similarly, if the formal comes from a nested formal package, the
3743 -- actual may designate the formal through multiple renamings, which
3744 -- have to be followed to determine the original variable in question.
3746 --------------------
3747 -- Check_Mismatch --
3748 --------------------
3750 procedure Check_Mismatch (B : Boolean) is
3754 ("actual for & in actual instance does not match formal",
3755 Parent (Actual_Pack), E1);
3759 --------------------------------
3760 -- Same_Instantiated_Constant --
3761 --------------------------------
3763 function Same_Instantiated_Constant
3764 (E1, E2 : Entity_Id) return Boolean
3769 while Present (Ent) loop
3773 elsif Ekind (Ent) /= E_Constant then
3776 elsif Is_Entity_Name (Constant_Value (Ent)) then
3777 if Entity (Constant_Value (Ent)) = E1 then
3780 Ent := Entity (Constant_Value (Ent));
3783 -- The actual may be a constant that has been folded. Recover
3786 elsif Is_Entity_Name (Original_Node (Constant_Value (Ent))) then
3787 Ent := Entity (Original_Node (Constant_Value (Ent)));
3794 end Same_Instantiated_Constant;
3796 --------------------------------
3797 -- Same_Instantiated_Variable --
3798 --------------------------------
3800 function Same_Instantiated_Variable
3801 (E1, E2 : Entity_Id) return Boolean
3803 function Original_Entity (E : Entity_Id) return Entity_Id;
3804 -- Follow chain of renamings to the ultimate ancestor
3806 ---------------------
3807 -- Original_Entity --
3808 ---------------------
3810 function Original_Entity (E : Entity_Id) return Entity_Id is
3815 while Nkind (Parent (Orig)) = N_Object_Renaming_Declaration
3816 and then Present (Renamed_Object (Orig))
3817 and then Is_Entity_Name (Renamed_Object (Orig))
3819 Orig := Entity (Renamed_Object (Orig));
3823 end Original_Entity;
3825 -- Start of processing for Same_Instantiated_Variable
3828 return Ekind (E1) = Ekind (E2)
3829 and then Original_Entity (E1) = Original_Entity (E2);
3830 end Same_Instantiated_Variable;
3832 -- Start of processing for Check_Formal_Package_Instance
3836 and then Present (E2)
3838 exit when Ekind (E1) = E_Package
3839 and then Renamed_Entity (E1) = Renamed_Entity (Actual_Pack);
3841 if Is_Type (E1) then
3843 -- Subtypes must statically match. E1 and E2 are the
3844 -- local entities that are subtypes of the actuals.
3845 -- Itypes generated for other parameters need not be checked,
3846 -- the check will be performed on the parameters themselves.
3848 if not Is_Itype (E1)
3849 and then not Is_Itype (E2)
3853 or else Etype (E1) /= Etype (E2)
3854 or else not Subtypes_Statically_Match (E1, E2));
3857 elsif Ekind (E1) = E_Constant then
3859 -- IN parameters must denote the same static value, or
3860 -- the same constant, or the literal null.
3862 Expr1 := Expression (Parent (E1));
3864 if Ekind (E2) /= E_Constant then
3865 Check_Mismatch (True);
3868 Expr2 := Expression (Parent (E2));
3871 if Is_Static_Expression (Expr1) then
3873 if not Is_Static_Expression (Expr2) then
3874 Check_Mismatch (True);
3876 elsif Is_Integer_Type (Etype (E1)) then
3879 V1 : constant Uint := Expr_Value (Expr1);
3880 V2 : constant Uint := Expr_Value (Expr2);
3882 Check_Mismatch (V1 /= V2);
3885 elsif Is_Real_Type (Etype (E1)) then
3887 V1 : constant Ureal := Expr_Value_R (Expr1);
3888 V2 : constant Ureal := Expr_Value_R (Expr2);
3890 Check_Mismatch (V1 /= V2);
3893 elsif Is_String_Type (Etype (E1))
3894 and then Nkind (Expr1) = N_String_Literal
3897 if Nkind (Expr2) /= N_String_Literal then
3898 Check_Mismatch (True);
3901 (not String_Equal (Strval (Expr1), Strval (Expr2)));
3905 elsif Is_Entity_Name (Expr1) then
3906 if Is_Entity_Name (Expr2) then
3907 if Entity (Expr1) = Entity (Expr2) then
3911 (not Same_Instantiated_Constant
3912 (Entity (Expr1), Entity (Expr2)));
3915 Check_Mismatch (True);
3918 elsif Is_Entity_Name (Original_Node (Expr1))
3919 and then Is_Entity_Name (Expr2)
3921 Same_Instantiated_Constant
3922 (Entity (Original_Node (Expr1)), Entity (Expr2))
3926 elsif Nkind (Expr1) = N_Null then
3927 Check_Mismatch (Nkind (Expr1) /= N_Null);
3930 Check_Mismatch (True);
3933 elsif Ekind (E1) = E_Variable then
3934 Check_Mismatch (not Same_Instantiated_Variable (E1, E2));
3936 elsif Ekind (E1) = E_Package then
3938 (Ekind (E1) /= Ekind (E2)
3939 or else Renamed_Object (E1) /= Renamed_Object (E2));
3941 elsif Is_Overloadable (E1) then
3943 -- Verify that the names of the entities match.
3944 -- What if actual is an attribute ???
3947 (Ekind (E2) /= Ekind (E1) or else (Alias (E1)) /= Alias (E2));
3950 raise Program_Error;
3957 end Check_Formal_Package_Instance;
3959 ---------------------------
3960 -- Check_Formal_Packages --
3961 ---------------------------
3963 procedure Check_Formal_Packages (P_Id : Entity_Id) is
3965 Formal_P : Entity_Id;
3968 -- Iterate through the declarations in the instance, looking for
3969 -- package renaming declarations that denote instances of formal
3970 -- packages. Stop when we find the renaming of the current package
3971 -- itself. The declaration for a formal package without a box is
3972 -- followed by an internal entity that repeats the instantiation.
3974 E := First_Entity (P_Id);
3975 while Present (E) loop
3976 if Ekind (E) = E_Package then
3977 if Renamed_Object (E) = P_Id then
3980 elsif Nkind (Parent (E)) /= N_Package_Renaming_Declaration then
3983 elsif not Box_Present (Parent (Associated_Formal_Package (E))) then
3984 Formal_P := Next_Entity (E);
3985 Check_Formal_Package_Instance (Formal_P, E);
3991 end Check_Formal_Packages;
3993 ---------------------------------
3994 -- Check_Forward_Instantiation --
3995 ---------------------------------
3997 procedure Check_Forward_Instantiation (Decl : Node_Id) is
3999 Gen_Comp : Entity_Id := Cunit_Entity (Get_Source_Unit (Decl));
4002 -- The instantiation appears before the generic body if we are in the
4003 -- scope of the unit containing the generic, either in its spec or in
4004 -- the package body. and before the generic body.
4006 if Ekind (Gen_Comp) = E_Package_Body then
4007 Gen_Comp := Spec_Entity (Gen_Comp);
4010 if In_Open_Scopes (Gen_Comp)
4011 and then No (Corresponding_Body (Decl))
4016 and then not Is_Compilation_Unit (S)
4017 and then not Is_Child_Unit (S)
4019 if Ekind (S) = E_Package then
4020 Set_Has_Forward_Instantiation (S);
4026 end Check_Forward_Instantiation;
4028 ---------------------------
4029 -- Check_Generic_Actuals --
4030 ---------------------------
4032 -- The visibility of the actuals may be different between the
4033 -- point of generic instantiation and the instantiation of the body.
4035 procedure Check_Generic_Actuals
4036 (Instance : Entity_Id;
4037 Is_Formal_Box : Boolean)
4042 function Denotes_Previous_Actual (Typ : Entity_Id) return Boolean;
4043 -- For a formal that is an array type, the component type is often
4044 -- a previous formal in the same unit. The privacy status of the
4045 -- component type will have been examined earlier in the traversal
4046 -- of the corresponding actuals, and this status should not be
4047 -- modified for the array type itself.
4048 -- To detect this case we have to rescan the list of formals, which
4049 -- is usually short enough to ignore the resulting inefficiency.
4051 function Denotes_Previous_Actual (Typ : Entity_Id) return Boolean is
4054 Prev := First_Entity (Instance);
4055 while Present (Prev) loop
4057 and then Nkind (Parent (Prev)) = N_Subtype_Declaration
4058 and then Is_Entity_Name (Subtype_Indication (Parent (Prev)))
4059 and then Entity (Subtype_Indication (Parent (Prev))) = Typ
4069 end Denotes_Previous_Actual;
4071 -- Start of processing for Check_Generic_Actuals
4074 E := First_Entity (Instance);
4075 while Present (E) loop
4077 and then Nkind (Parent (E)) = N_Subtype_Declaration
4078 and then Scope (Etype (E)) /= Instance
4079 and then Is_Entity_Name (Subtype_Indication (Parent (E)))
4081 if Is_Array_Type (E)
4082 and then Denotes_Previous_Actual (Component_Type (E))
4086 Check_Private_View (Subtype_Indication (Parent (E)));
4088 Set_Is_Generic_Actual_Type (E, True);
4089 Set_Is_Hidden (E, False);
4090 Set_Is_Potentially_Use_Visible (E,
4093 -- We constructed the generic actual type as a subtype of
4094 -- the supplied type. This means that it normally would not
4095 -- inherit subtype specific attributes of the actual, which
4096 -- is wrong for the generic case.
4098 Astype := Ancestor_Subtype (E);
4102 -- can happen when E is an itype that is the full view of
4103 -- a private type completed, e.g. with a constrained array.
4105 Astype := Base_Type (E);
4108 Set_Size_Info (E, (Astype));
4109 Set_RM_Size (E, RM_Size (Astype));
4110 Set_First_Rep_Item (E, First_Rep_Item (Astype));
4112 if Is_Discrete_Or_Fixed_Point_Type (E) then
4113 Set_RM_Size (E, RM_Size (Astype));
4115 -- In nested instances, the base type of an access actual
4116 -- may itself be private, and need to be exchanged.
4118 elsif Is_Access_Type (E)
4119 and then Is_Private_Type (Etype (E))
4122 (New_Occurrence_Of (Etype (E), Sloc (Instance)));
4125 elsif Ekind (E) = E_Package then
4127 -- If this is the renaming for the current instance, we're done.
4128 -- Otherwise it is a formal package. If the corresponding formal
4129 -- was declared with a box, the (instantiations of the) generic
4130 -- formal part are also visible. Otherwise, ignore the entity
4131 -- created to validate the actuals.
4133 if Renamed_Object (E) = Instance then
4136 elsif Nkind (Parent (E)) /= N_Package_Renaming_Declaration then
4139 -- The visibility of a formal of an enclosing generic is already
4142 elsif Denotes_Formal_Package (E) then
4145 elsif Present (Associated_Formal_Package (E)) then
4146 if Box_Present (Parent (Associated_Formal_Package (E))) then
4147 Check_Generic_Actuals (Renamed_Object (E), True);
4150 Set_Is_Hidden (E, False);
4153 -- If this is a subprogram instance (in a wrapper package) the
4154 -- actual is fully visible.
4156 elsif Is_Wrapper_Package (Instance) then
4157 Set_Is_Hidden (E, False);
4160 Set_Is_Hidden (E, not Is_Formal_Box);
4165 end Check_Generic_Actuals;
4167 ------------------------------
4168 -- Check_Generic_Child_Unit --
4169 ------------------------------
4171 procedure Check_Generic_Child_Unit
4173 Parent_Installed : in out Boolean)
4175 Loc : constant Source_Ptr := Sloc (Gen_Id);
4176 Gen_Par : Entity_Id := Empty;
4177 Inst_Par : Entity_Id;
4181 function Find_Generic_Child
4183 Id : Node_Id) return Entity_Id;
4184 -- Search generic parent for possible child unit with the given name
4186 function In_Enclosing_Instance return Boolean;
4187 -- Within an instance of the parent, the child unit may be denoted
4188 -- by a simple name, or an abbreviated expanded name. Examine enclosing
4189 -- scopes to locate a possible parent instantiation.
4191 ------------------------
4192 -- Find_Generic_Child --
4193 ------------------------
4195 function Find_Generic_Child
4197 Id : Node_Id) return Entity_Id
4202 -- If entity of name is already set, instance has already been
4203 -- resolved, e.g. in an enclosing instantiation.
4205 if Present (Entity (Id)) then
4206 if Scope (Entity (Id)) = Scop then
4213 E := First_Entity (Scop);
4214 while Present (E) loop
4215 if Chars (E) = Chars (Id)
4216 and then Is_Child_Unit (E)
4218 if Is_Child_Unit (E)
4219 and then not Is_Visible_Child_Unit (E)
4222 ("generic child unit& is not visible", Gen_Id, E);
4234 end Find_Generic_Child;
4236 ---------------------------
4237 -- In_Enclosing_Instance --
4238 ---------------------------
4240 function In_Enclosing_Instance return Boolean is
4241 Enclosing_Instance : Node_Id;
4242 Instance_Decl : Node_Id;
4245 Enclosing_Instance := Current_Scope;
4247 while Present (Enclosing_Instance) loop
4248 Instance_Decl := Unit_Declaration_Node (Enclosing_Instance);
4250 if Ekind (Enclosing_Instance) = E_Package
4251 and then Is_Generic_Instance (Enclosing_Instance)
4253 (Generic_Parent (Specification (Instance_Decl)))
4255 -- Check whether the generic we are looking for is a child
4256 -- of this instance.
4258 E := Find_Generic_Child
4259 (Generic_Parent (Specification (Instance_Decl)), Gen_Id);
4260 exit when Present (E);
4266 Enclosing_Instance := Scope (Enclosing_Instance);
4278 Make_Expanded_Name (Loc,
4280 Prefix => New_Occurrence_Of (Enclosing_Instance, Loc),
4281 Selector_Name => New_Occurrence_Of (E, Loc)));
4283 Set_Entity (Gen_Id, E);
4284 Set_Etype (Gen_Id, Etype (E));
4285 Parent_Installed := False; -- Already in scope.
4288 end In_Enclosing_Instance;
4290 -- Start of processing for Check_Generic_Child_Unit
4293 -- If the name of the generic is given by a selected component, it
4294 -- may be the name of a generic child unit, and the prefix is the name
4295 -- of an instance of the parent, in which case the child unit must be
4296 -- visible. If this instance is not in scope, it must be placed there
4297 -- and removed after instantiation, because what is being instantiated
4298 -- is not the original child, but the corresponding child present in
4299 -- the instance of the parent.
4301 -- If the child is instantiated within the parent, it can be given by
4302 -- a simple name. In this case the instance is already in scope, but
4303 -- the child generic must be recovered from the generic parent as well.
4305 if Nkind (Gen_Id) = N_Selected_Component then
4306 S := Selector_Name (Gen_Id);
4307 Analyze (Prefix (Gen_Id));
4308 Inst_Par := Entity (Prefix (Gen_Id));
4310 if Ekind (Inst_Par) = E_Package
4311 and then Present (Renamed_Object (Inst_Par))
4313 Inst_Par := Renamed_Object (Inst_Par);
4316 if Ekind (Inst_Par) = E_Package then
4317 if Nkind (Parent (Inst_Par)) = N_Package_Specification then
4318 Gen_Par := Generic_Parent (Parent (Inst_Par));
4320 elsif Nkind (Parent (Inst_Par)) = N_Defining_Program_Unit_Name
4322 Nkind (Parent (Parent (Inst_Par))) = N_Package_Specification
4324 Gen_Par := Generic_Parent (Parent (Parent (Inst_Par)));
4327 elsif Ekind (Inst_Par) = E_Generic_Package
4328 and then Nkind (Parent (Gen_Id)) = N_Formal_Package_Declaration
4330 -- A formal package may be a real child package, and not the
4331 -- implicit instance within a parent. In this case the child is
4332 -- not visible and has to be retrieved explicitly as well.
4334 Gen_Par := Inst_Par;
4337 if Present (Gen_Par) then
4339 -- The prefix denotes an instantiation. The entity itself
4340 -- may be a nested generic, or a child unit.
4342 E := Find_Generic_Child (Gen_Par, S);
4345 Change_Selected_Component_To_Expanded_Name (Gen_Id);
4346 Set_Entity (Gen_Id, E);
4347 Set_Etype (Gen_Id, Etype (E));
4349 Set_Etype (S, Etype (E));
4351 -- Indicate that this is a reference to the parent
4353 if In_Extended_Main_Source_Unit (Gen_Id) then
4354 Set_Is_Instantiated (Inst_Par);
4357 -- A common mistake is to replicate the naming scheme of
4358 -- a hierarchy by instantiating a generic child directly,
4359 -- rather than the implicit child in a parent instance:
4361 -- generic .. package Gpar is ..
4362 -- generic .. package Gpar.Child is ..
4363 -- package Par is new Gpar ();
4366 -- package Par.Child is new Gpar.Child ();
4367 -- rather than Par.Child
4369 -- In this case the instantiation is within Par, which is
4370 -- an instance, but Gpar does not denote Par because we are
4371 -- not IN the instance of Gpar, so this is illegal. The test
4372 -- below recognizes this particular case.
4374 if Is_Child_Unit (E)
4375 and then not Comes_From_Source (Entity (Prefix (Gen_Id)))
4376 and then (not In_Instance
4377 or else Nkind (Parent (Parent (Gen_Id))) =
4381 ("prefix of generic child unit must be instance of parent",
4385 if not In_Open_Scopes (Inst_Par)
4386 and then Nkind (Parent (Gen_Id)) not in
4387 N_Generic_Renaming_Declaration
4389 Install_Parent (Inst_Par);
4390 Parent_Installed := True;
4394 -- If the generic parent does not contain an entity that
4395 -- corresponds to the selector, the instance doesn't either.
4396 -- Analyzing the node will yield the appropriate error message.
4397 -- If the entity is not a child unit, then it is an inner
4398 -- generic in the parent.
4406 if Is_Child_Unit (Entity (Gen_Id))
4408 Nkind (Parent (Gen_Id)) not in N_Generic_Renaming_Declaration
4409 and then not In_Open_Scopes (Inst_Par)
4411 Install_Parent (Inst_Par);
4412 Parent_Installed := True;
4416 elsif Nkind (Gen_Id) = N_Expanded_Name then
4418 -- Entity already present, analyze prefix, whose meaning may be
4419 -- an instance in the current context. If it is an instance of
4420 -- a relative within another, the proper parent may still have
4421 -- to be installed, if they are not of the same generation.
4423 Analyze (Prefix (Gen_Id));
4424 Inst_Par := Entity (Prefix (Gen_Id));
4426 if In_Enclosing_Instance then
4429 elsif Present (Entity (Gen_Id))
4430 and then Is_Child_Unit (Entity (Gen_Id))
4431 and then not In_Open_Scopes (Inst_Par)
4433 Install_Parent (Inst_Par);
4434 Parent_Installed := True;
4437 elsif In_Enclosing_Instance then
4439 -- The child unit is found in some enclosing scope
4446 -- If this is the renaming of the implicit child in a parent
4447 -- instance, recover the parent name and install it.
4449 if Is_Entity_Name (Gen_Id) then
4450 E := Entity (Gen_Id);
4452 if Is_Generic_Unit (E)
4453 and then Nkind (Parent (E)) in N_Generic_Renaming_Declaration
4454 and then Is_Child_Unit (Renamed_Object (E))
4455 and then Is_Generic_Unit (Scope (Renamed_Object (E)))
4456 and then Nkind (Name (Parent (E))) = N_Expanded_Name
4459 New_Copy_Tree (Name (Parent (E))));
4460 Inst_Par := Entity (Prefix (Gen_Id));
4462 if not In_Open_Scopes (Inst_Par) then
4463 Install_Parent (Inst_Par);
4464 Parent_Installed := True;
4467 -- If it is a child unit of a non-generic parent, it may be
4468 -- use-visible and given by a direct name. Install parent as
4471 elsif Is_Generic_Unit (E)
4472 and then Is_Child_Unit (E)
4474 Nkind (Parent (Gen_Id)) not in N_Generic_Renaming_Declaration
4475 and then not Is_Generic_Unit (Scope (E))
4477 if not In_Open_Scopes (Scope (E)) then
4478 Install_Parent (Scope (E));
4479 Parent_Installed := True;
4484 end Check_Generic_Child_Unit;
4486 -----------------------------
4487 -- Check_Hidden_Child_Unit --
4488 -----------------------------
4490 procedure Check_Hidden_Child_Unit
4492 Gen_Unit : Entity_Id;
4493 Act_Decl_Id : Entity_Id)
4495 Gen_Id : constant Node_Id := Name (N);
4498 if Is_Child_Unit (Gen_Unit)
4499 and then Is_Child_Unit (Act_Decl_Id)
4500 and then Nkind (Gen_Id) = N_Expanded_Name
4501 and then Entity (Prefix (Gen_Id)) = Scope (Act_Decl_Id)
4502 and then Chars (Gen_Unit) = Chars (Act_Decl_Id)
4504 Error_Msg_Node_2 := Scope (Act_Decl_Id);
4506 ("generic unit & is implicitly declared in &",
4507 Defining_Unit_Name (N), Gen_Unit);
4508 Error_Msg_N ("\instance must have different name",
4509 Defining_Unit_Name (N));
4511 end Check_Hidden_Child_Unit;
4513 ------------------------
4514 -- Check_Private_View --
4515 ------------------------
4517 procedure Check_Private_View (N : Node_Id) is
4518 T : constant Entity_Id := Etype (N);
4522 -- Exchange views if the type was not private in the generic but is
4523 -- private at the point of instantiation. Do not exchange views if
4524 -- the scope of the type is in scope. This can happen if both generic
4525 -- and instance are sibling units, or if type is defined in a parent.
4526 -- In this case the visibility of the type will be correct for all
4530 BT := Base_Type (T);
4532 if Is_Private_Type (T)
4533 and then not Has_Private_View (N)
4534 and then Present (Full_View (T))
4535 and then not In_Open_Scopes (Scope (T))
4537 -- In the generic, the full type was visible. Save the
4538 -- private entity, for subsequent exchange.
4542 elsif Has_Private_View (N)
4543 and then not Is_Private_Type (T)
4544 and then not Has_Been_Exchanged (T)
4545 and then Etype (Get_Associated_Node (N)) /= T
4547 -- Only the private declaration was visible in the generic. If
4548 -- the type appears in a subtype declaration, the subtype in the
4549 -- instance must have a view compatible with that of its parent,
4550 -- which must be exchanged (see corresponding code in Restore_
4551 -- Private_Views). Otherwise, if the type is defined in a parent
4552 -- unit, leave full visibility within instance, which is safe.
4554 if In_Open_Scopes (Scope (Base_Type (T)))
4555 and then not Is_Private_Type (Base_Type (T))
4556 and then Comes_From_Source (Base_Type (T))
4560 elsif Nkind (Parent (N)) = N_Subtype_Declaration
4561 or else not In_Private_Part (Scope (Base_Type (T)))
4563 Append_Elmt (T, Exchanged_Views);
4564 Exchange_Declarations (Etype (Get_Associated_Node (N)));
4567 -- For composite types with inconsistent representation
4568 -- exchange component types accordingly.
4570 elsif Is_Access_Type (T)
4571 and then Is_Private_Type (Designated_Type (T))
4572 and then not Has_Private_View (N)
4573 and then Present (Full_View (Designated_Type (T)))
4575 Switch_View (Designated_Type (T));
4577 elsif Is_Array_Type (T)
4578 and then Is_Private_Type (Component_Type (T))
4579 and then not Has_Private_View (N)
4580 and then Present (Full_View (Component_Type (T)))
4582 Switch_View (Component_Type (T));
4584 elsif Is_Private_Type (T)
4585 and then Present (Full_View (T))
4586 and then Is_Array_Type (Full_View (T))
4587 and then Is_Private_Type (Component_Type (Full_View (T)))
4591 -- Finally, a non-private subtype may have a private base type,
4592 -- which must be exchanged for consistency. This can happen when
4593 -- instantiating a package body, when the scope stack is empty
4594 -- but in fact the subtype and the base type are declared in an
4597 elsif not Is_Private_Type (T)
4598 and then not Has_Private_View (N)
4599 and then Is_Private_Type (Base_Type (T))
4600 and then Present (Full_View (BT))
4601 and then not Is_Generic_Type (BT)
4602 and then not In_Open_Scopes (BT)
4604 Append_Elmt (Full_View (BT), Exchanged_Views);
4605 Exchange_Declarations (BT);
4608 end Check_Private_View;
4610 --------------------------
4611 -- Contains_Instance_Of --
4612 --------------------------
4614 function Contains_Instance_Of
4617 N : Node_Id) return Boolean
4625 -- Verify that there are no circular instantiations. We check whether
4626 -- the unit contains an instance of the current scope or some enclosing
4627 -- scope (in case one of the instances appears in a subunit). Longer
4628 -- circularities involving subunits might seem too pathological to
4629 -- consider, but they were not too pathological for the authors of
4630 -- DEC bc30vsq, so we loop over all enclosing scopes, and mark all
4631 -- enclosing generic scopes as containing an instance.
4634 -- Within a generic subprogram body, the scope is not generic, to
4635 -- allow for recursive subprograms. Use the declaration to determine
4636 -- whether this is a generic unit.
4638 if Ekind (Scop) = E_Generic_Package
4639 or else (Is_Subprogram (Scop)
4640 and then Nkind (Unit_Declaration_Node (Scop)) =
4641 N_Generic_Subprogram_Declaration)
4643 Elmt := First_Elmt (Inner_Instances (Inner));
4645 while Present (Elmt) loop
4646 if Node (Elmt) = Scop then
4647 Error_Msg_Node_2 := Inner;
4649 ("circular Instantiation: & instantiated within &!",
4653 elsif Node (Elmt) = Inner then
4656 elsif Contains_Instance_Of (Node (Elmt), Scop, N) then
4657 Error_Msg_Node_2 := Inner;
4659 ("circular Instantiation: & instantiated within &!",
4667 -- Indicate that Inner is being instantiated within Scop
4669 Append_Elmt (Inner, Inner_Instances (Scop));
4672 if Scop = Standard_Standard then
4675 Scop := Scope (Scop);
4680 end Contains_Instance_Of;
4682 -----------------------
4683 -- Copy_Generic_Node --
4684 -----------------------
4686 function Copy_Generic_Node
4688 Parent_Id : Node_Id;
4689 Instantiating : Boolean) return Node_Id
4694 function Copy_Generic_Descendant (D : Union_Id) return Union_Id;
4695 -- Check the given value of one of the Fields referenced by the
4696 -- current node to determine whether to copy it recursively. The
4697 -- field may hold a Node_Id, a List_Id, or an Elist_Id, or a plain
4698 -- value (Sloc, Uint, Char) in which case it need not be copied.
4700 procedure Copy_Descendants;
4701 -- Common utility for various nodes
4703 function Copy_Generic_Elist (E : Elist_Id) return Elist_Id;
4704 -- Make copy of element list
4706 function Copy_Generic_List
4708 Parent_Id : Node_Id) return List_Id;
4709 -- Apply Copy_Node recursively to the members of a node list
4711 function In_Defining_Unit_Name (Nam : Node_Id) return Boolean;
4712 -- True if an identifier is part of the defining program unit name
4713 -- of a child unit. The entity of such an identifier must be kept
4714 -- (for ASIS use) even though as the name of an enclosing generic
4715 -- it would otherwise not be preserved in the generic tree.
4717 ----------------------
4718 -- Copy_Descendants --
4719 ----------------------
4721 procedure Copy_Descendants is
4723 use Atree.Unchecked_Access;
4724 -- This code section is part of the implementation of an untyped
4725 -- tree traversal, so it needs direct access to node fields.
4728 Set_Field1 (New_N, Copy_Generic_Descendant (Field1 (N)));
4729 Set_Field2 (New_N, Copy_Generic_Descendant (Field2 (N)));
4730 Set_Field3 (New_N, Copy_Generic_Descendant (Field3 (N)));
4731 Set_Field4 (New_N, Copy_Generic_Descendant (Field4 (N)));
4732 Set_Field5 (New_N, Copy_Generic_Descendant (Field5 (N)));
4733 end Copy_Descendants;
4735 -----------------------------
4736 -- Copy_Generic_Descendant --
4737 -----------------------------
4739 function Copy_Generic_Descendant (D : Union_Id) return Union_Id is
4741 if D = Union_Id (Empty) then
4744 elsif D in Node_Range then
4746 (Copy_Generic_Node (Node_Id (D), New_N, Instantiating));
4748 elsif D in List_Range then
4749 return Union_Id (Copy_Generic_List (List_Id (D), New_N));
4751 elsif D in Elist_Range then
4752 return Union_Id (Copy_Generic_Elist (Elist_Id (D)));
4754 -- Nothing else is copyable (e.g. Uint values), return as is
4759 end Copy_Generic_Descendant;
4761 ------------------------
4762 -- Copy_Generic_Elist --
4763 ------------------------
4765 function Copy_Generic_Elist (E : Elist_Id) return Elist_Id is
4772 M := First_Elmt (E);
4773 while Present (M) loop
4775 (Copy_Generic_Node (Node (M), Empty, Instantiating), L);
4784 end Copy_Generic_Elist;
4786 -----------------------
4787 -- Copy_Generic_List --
4788 -----------------------
4790 function Copy_Generic_List
4792 Parent_Id : Node_Id) return List_Id
4800 Set_Parent (New_L, Parent_Id);
4803 while Present (N) loop
4804 Append (Copy_Generic_Node (N, Empty, Instantiating), New_L);
4813 end Copy_Generic_List;
4815 ---------------------------
4816 -- In_Defining_Unit_Name --
4817 ---------------------------
4819 function In_Defining_Unit_Name (Nam : Node_Id) return Boolean is
4821 return Present (Parent (Nam))
4822 and then (Nkind (Parent (Nam)) = N_Defining_Program_Unit_Name
4824 (Nkind (Parent (Nam)) = N_Expanded_Name
4825 and then In_Defining_Unit_Name (Parent (Nam))));
4826 end In_Defining_Unit_Name;
4828 -- Start of processing for Copy_Generic_Node
4835 New_N := New_Copy (N);
4837 if Instantiating then
4838 Adjust_Instantiation_Sloc (New_N, S_Adjustment);
4841 if not Is_List_Member (N) then
4842 Set_Parent (New_N, Parent_Id);
4845 -- If defining identifier, then all fields have been copied already
4847 if Nkind (New_N) in N_Entity then
4850 -- Special casing for identifiers and other entity names and operators
4852 elsif Nkind (New_N) = N_Identifier
4853 or else Nkind (New_N) = N_Character_Literal
4854 or else Nkind (New_N) = N_Expanded_Name
4855 or else Nkind (New_N) = N_Operator_Symbol
4856 or else Nkind (New_N) in N_Op
4858 if not Instantiating then
4860 -- Link both nodes in order to assign subsequently the
4861 -- entity of the copy to the original node, in case this
4862 -- is a global reference.
4864 Set_Associated_Node (N, New_N);
4866 -- If we are within an instantiation, this is a nested generic
4867 -- that has already been analyzed at the point of definition. We
4868 -- must preserve references that were global to the enclosing
4869 -- parent at that point. Other occurrences, whether global or
4870 -- local to the current generic, must be resolved anew, so we
4871 -- reset the entity in the generic copy. A global reference has
4872 -- a smaller depth than the parent, or else the same depth in
4873 -- case both are distinct compilation units.
4875 -- It is also possible for Current_Instantiated_Parent to be
4876 -- defined, and for this not to be a nested generic, namely
4877 -- if the unit is loaded through Rtsfind. In that case, the
4878 -- entity of New_N is only a link to the associated node, and
4879 -- not a defining occurrence.
4881 -- The entities for parent units in the defining_program_unit
4882 -- of a generic child unit are established when the context of
4883 -- the unit is first analyzed, before the generic copy is made.
4884 -- They are preserved in the copy for use in ASIS queries.
4886 Ent := Entity (New_N);
4888 if No (Current_Instantiated_Parent.Gen_Id) then
4890 or else Nkind (Ent) /= N_Defining_Identifier
4891 or else not In_Defining_Unit_Name (N)
4893 Set_Associated_Node (New_N, Empty);
4898 not (Nkind (Ent) = N_Defining_Identifier
4900 Nkind (Ent) = N_Defining_Character_Literal
4902 Nkind (Ent) = N_Defining_Operator_Symbol)
4903 or else No (Scope (Ent))
4904 or else Scope (Ent) = Current_Instantiated_Parent.Gen_Id
4905 or else (Scope_Depth (Scope (Ent)) >
4906 Scope_Depth (Current_Instantiated_Parent.Gen_Id)
4908 Get_Source_Unit (Ent) =
4909 Get_Source_Unit (Current_Instantiated_Parent.Gen_Id))
4911 Set_Associated_Node (New_N, Empty);
4914 -- Case of instantiating identifier or some other name or operator
4917 -- If the associated node is still defined, the entity in
4918 -- it is global, and must be copied to the instance.
4919 -- If this copy is being made for a body to inline, it is
4920 -- applied to an instantiated tree, and the entity is already
4921 -- present and must be also preserved.
4924 Assoc : constant Node_Id := Get_Associated_Node (N);
4926 if Present (Assoc) then
4927 if Nkind (Assoc) = Nkind (N) then
4928 Set_Entity (New_N, Entity (Assoc));
4929 Check_Private_View (N);
4931 elsif Nkind (Assoc) = N_Function_Call then
4932 Set_Entity (New_N, Entity (Name (Assoc)));
4934 elsif (Nkind (Assoc) = N_Defining_Identifier
4935 or else Nkind (Assoc) = N_Defining_Character_Literal
4936 or else Nkind (Assoc) = N_Defining_Operator_Symbol)
4937 and then Expander_Active
4939 -- Inlining case: we are copying a tree that contains
4940 -- global entities, which are preserved in the copy
4941 -- to be used for subsequent inlining.
4946 Set_Entity (New_N, Empty);
4952 -- For expanded name, we must copy the Prefix and Selector_Name
4954 if Nkind (N) = N_Expanded_Name then
4956 (New_N, Copy_Generic_Node (Prefix (N), New_N, Instantiating));
4958 Set_Selector_Name (New_N,
4959 Copy_Generic_Node (Selector_Name (N), New_N, Instantiating));
4961 -- For operators, we must copy the right operand
4963 elsif Nkind (N) in N_Op then
4964 Set_Right_Opnd (New_N,
4965 Copy_Generic_Node (Right_Opnd (N), New_N, Instantiating));
4967 -- And for binary operators, the left operand as well
4969 if Nkind (N) in N_Binary_Op then
4970 Set_Left_Opnd (New_N,
4971 Copy_Generic_Node (Left_Opnd (N), New_N, Instantiating));
4975 -- Special casing for stubs
4977 elsif Nkind (N) in N_Body_Stub then
4979 -- In any case, we must copy the specification or defining
4980 -- identifier as appropriate.
4982 if Nkind (N) = N_Subprogram_Body_Stub then
4983 Set_Specification (New_N,
4984 Copy_Generic_Node (Specification (N), New_N, Instantiating));
4987 Set_Defining_Identifier (New_N,
4989 (Defining_Identifier (N), New_N, Instantiating));
4992 -- If we are not instantiating, then this is where we load and
4993 -- analyze subunits, i.e. at the point where the stub occurs. A
4994 -- more permissivle system might defer this analysis to the point
4995 -- of instantiation, but this seems to complicated for now.
4997 if not Instantiating then
4999 Subunit_Name : constant Unit_Name_Type := Get_Unit_Name (N);
5001 Unum : Unit_Number_Type;
5007 (Load_Name => Subunit_Name,
5012 -- If the proper body is not found, a warning message will
5013 -- be emitted when analyzing the stub, or later at the the
5014 -- point of instantiation. Here we just leave the stub as is.
5016 if Unum = No_Unit then
5017 Subunits_Missing := True;
5018 goto Subunit_Not_Found;
5021 Subunit := Cunit (Unum);
5023 if Nkind (Unit (Subunit)) /= N_Subunit then
5024 Error_Msg_Sloc := Sloc (N);
5026 ("expected SEPARATE subunit to complete stub at#,"
5027 & " found child unit", Subunit);
5028 goto Subunit_Not_Found;
5031 -- We must create a generic copy of the subunit, in order
5032 -- to perform semantic analysis on it, and we must replace
5033 -- the stub in the original generic unit with the subunit,
5034 -- in order to preserve non-local references within.
5036 -- Only the proper body needs to be copied. Library_Unit and
5037 -- context clause are simply inherited by the generic copy.
5038 -- Note that the copy (which may be recursive if there are
5039 -- nested subunits) must be done first, before attaching it
5040 -- to the enclosing generic.
5044 (Proper_Body (Unit (Subunit)),
5045 Empty, Instantiating => False);
5047 -- Now place the original proper body in the original
5048 -- generic unit. This is a body, not a compilation unit.
5050 Rewrite (N, Proper_Body (Unit (Subunit)));
5051 Set_Is_Compilation_Unit (Defining_Entity (N), False);
5052 Set_Was_Originally_Stub (N);
5054 -- Finally replace the body of the subunit with its copy,
5055 -- and make this new subunit into the library unit of the
5056 -- generic copy, which does not have stubs any longer.
5058 Set_Proper_Body (Unit (Subunit), New_Body);
5059 Set_Library_Unit (New_N, Subunit);
5060 Inherit_Context (Unit (Subunit), N);
5063 -- If we are instantiating, this must be an error case, since
5064 -- otherwise we would have replaced the stub node by the proper
5065 -- body that corresponds. So just ignore it in the copy (i.e.
5066 -- we have copied it, and that is good enough).
5072 <<Subunit_Not_Found>> null;
5074 -- If the node is a compilation unit, it is the subunit of a stub,
5075 -- which has been loaded already (see code below). In this case,
5076 -- the library unit field of N points to the parent unit (which
5077 -- is a compilation unit) and need not (and cannot!) be copied.
5079 -- When the proper body of the stub is analyzed, thie library_unit
5080 -- link is used to establish the proper context (see sem_ch10).
5082 -- The other fields of a compilation unit are copied as usual
5084 elsif Nkind (N) = N_Compilation_Unit then
5086 -- This code can only be executed when not instantiating, because
5087 -- in the copy made for an instantiation, the compilation unit
5088 -- node has disappeared at the point that a stub is replaced by
5091 pragma Assert (not Instantiating);
5093 Set_Context_Items (New_N,
5094 Copy_Generic_List (Context_Items (N), New_N));
5097 Copy_Generic_Node (Unit (N), New_N, False));
5099 Set_First_Inlined_Subprogram (New_N,
5101 (First_Inlined_Subprogram (N), New_N, False));
5103 Set_Aux_Decls_Node (New_N,
5104 Copy_Generic_Node (Aux_Decls_Node (N), New_N, False));
5106 -- For an assignment node, the assignment is known to be semantically
5107 -- legal if we are instantiating the template. This avoids incorrect
5108 -- diagnostics in generated code.
5110 elsif Nkind (N) = N_Assignment_Statement then
5112 -- Copy name and expression fields in usual manner
5115 Copy_Generic_Node (Name (N), New_N, Instantiating));
5117 Set_Expression (New_N,
5118 Copy_Generic_Node (Expression (N), New_N, Instantiating));
5120 if Instantiating then
5121 Set_Assignment_OK (Name (New_N), True);
5124 elsif Nkind (N) = N_Aggregate
5125 or else Nkind (N) = N_Extension_Aggregate
5128 if not Instantiating then
5129 Set_Associated_Node (N, New_N);
5132 if Present (Get_Associated_Node (N))
5133 and then Nkind (Get_Associated_Node (N)) = Nkind (N)
5135 -- In the generic the aggregate has some composite type. If at
5136 -- the point of instantiation the type has a private view,
5137 -- install the full view (and that of its ancestors, if any).
5140 T : Entity_Id := (Etype (Get_Associated_Node (New_N)));
5145 and then Is_Private_Type (T)
5151 and then Is_Tagged_Type (T)
5152 and then Is_Derived_Type (T)
5154 Rt := Root_Type (T);
5159 if Is_Private_Type (T) then
5170 -- Do not copy the associated node, which points to
5171 -- the generic copy of the aggregate.
5174 use Atree.Unchecked_Access;
5175 -- This code section is part of the implementation of an untyped
5176 -- tree traversal, so it needs direct access to node fields.
5179 Set_Field1 (New_N, Copy_Generic_Descendant (Field1 (N)));
5180 Set_Field2 (New_N, Copy_Generic_Descendant (Field2 (N)));
5181 Set_Field3 (New_N, Copy_Generic_Descendant (Field3 (N)));
5182 Set_Field5 (New_N, Copy_Generic_Descendant (Field5 (N)));
5185 -- Allocators do not have an identifier denoting the access type,
5186 -- so we must locate it through the expression to check whether
5187 -- the views are consistent.
5189 elsif Nkind (N) = N_Allocator
5190 and then Nkind (Expression (N)) = N_Qualified_Expression
5191 and then Is_Entity_Name (Subtype_Mark (Expression (N)))
5192 and then Instantiating
5195 T : constant Node_Id :=
5196 Get_Associated_Node (Subtype_Mark (Expression (N)));
5201 -- Retrieve the allocator node in the generic copy
5203 Acc_T := Etype (Parent (Parent (T)));
5205 and then Is_Private_Type (Acc_T)
5207 Switch_View (Acc_T);
5214 -- For a proper body, we must catch the case of a proper body that
5215 -- replaces a stub. This represents the point at which a separate
5216 -- compilation unit, and hence template file, may be referenced, so
5217 -- we must make a new source instantiation entry for the template
5218 -- of the subunit, and ensure that all nodes in the subunit are
5219 -- adjusted using this new source instantiation entry.
5221 elsif Nkind (N) in N_Proper_Body then
5223 Save_Adjustment : constant Sloc_Adjustment := S_Adjustment;
5226 if Instantiating and then Was_Originally_Stub (N) then
5227 Create_Instantiation_Source
5228 (Instantiation_Node,
5229 Defining_Entity (N),
5234 -- Now copy the fields of the proper body, using the new
5235 -- adjustment factor if one was needed as per test above.
5239 -- Restore the original adjustment factor in case changed
5241 S_Adjustment := Save_Adjustment;
5244 -- Don't copy Ident or Comment pragmas, since the comment belongs
5245 -- to the generic unit, not to the instantiating unit.
5247 elsif Nkind (N) = N_Pragma
5248 and then Instantiating
5251 Prag_Id : constant Pragma_Id := Get_Pragma_Id (Chars (N));
5254 if Prag_Id = Pragma_Ident
5255 or else Prag_Id = Pragma_Comment
5257 New_N := Make_Null_Statement (Sloc (N));
5264 elsif Nkind (N) = N_Integer_Literal
5265 or else Nkind (N) = N_Real_Literal
5267 -- No descendant fields need traversing
5271 -- For the remaining nodes, copy recursively their descendants
5277 and then Nkind (N) = N_Subprogram_Body
5279 Set_Generic_Parent (Specification (New_N), N);
5284 end Copy_Generic_Node;
5286 ----------------------------
5287 -- Denotes_Formal_Package --
5288 ----------------------------
5290 function Denotes_Formal_Package
5292 On_Exit : Boolean := False) return Boolean
5295 Scop : constant Entity_Id := Scope (Pack);
5302 (Instance_Envs.Last).Instantiated_Parent.Act_Id;
5304 Par := Current_Instantiated_Parent.Act_Id;
5307 if Ekind (Scop) = E_Generic_Package
5308 or else Nkind (Unit_Declaration_Node (Scop)) =
5309 N_Generic_Subprogram_Declaration
5313 elsif Nkind (Parent (Pack)) = N_Formal_Package_Declaration then
5320 -- Check whether this package is associated with a formal
5321 -- package of the enclosing instantiation. Iterate over the
5322 -- list of renamings.
5324 E := First_Entity (Par);
5325 while Present (E) loop
5326 if Ekind (E) /= E_Package
5327 or else Nkind (Parent (E)) /= N_Package_Renaming_Declaration
5330 elsif Renamed_Object (E) = Par then
5333 elsif Renamed_Object (E) = Pack then
5342 end Denotes_Formal_Package;
5348 procedure End_Generic is
5350 -- ??? More things could be factored out in this
5351 -- routine. Should probably be done at a later stage.
5353 Inside_A_Generic := Generic_Flags.Table (Generic_Flags.Last);
5354 Generic_Flags.Decrement_Last;
5356 Expander_Mode_Restore;
5359 ----------------------
5360 -- Find_Actual_Type --
5361 ----------------------
5363 function Find_Actual_Type
5365 Gen_Scope : Entity_Id) return Entity_Id
5370 if not Is_Child_Unit (Gen_Scope) then
5371 return Get_Instance_Of (Typ);
5373 elsif not Is_Generic_Type (Typ)
5374 or else Scope (Typ) = Gen_Scope
5376 return Get_Instance_Of (Typ);
5379 T := Current_Entity (Typ);
5380 while Present (T) loop
5381 if In_Open_Scopes (Scope (T)) then
5390 end Find_Actual_Type;
5392 ----------------------------
5393 -- Freeze_Subprogram_Body --
5394 ----------------------------
5396 procedure Freeze_Subprogram_Body
5397 (Inst_Node : Node_Id;
5399 Pack_Id : Entity_Id)
5402 Gen_Unit : constant Entity_Id := Get_Generic_Entity (Inst_Node);
5403 Par : constant Entity_Id := Scope (Gen_Unit);
5408 function Earlier (N1, N2 : Node_Id) return Boolean;
5409 -- Yields True if N1 and N2 appear in the same compilation unit,
5410 -- ignoring subunits, and if N1 is to the left of N2 in a left-to-right
5411 -- traversal of the tree for the unit.
5413 function Enclosing_Body (N : Node_Id) return Node_Id;
5414 -- Find innermost package body that encloses the given node, and which
5415 -- is not a compilation unit. Freeze nodes for the instance, or for its
5416 -- enclosing body, may be inserted after the enclosing_body of the
5419 function Package_Freeze_Node (B : Node_Id) return Node_Id;
5420 -- Find entity for given package body, and locate or create a freeze
5423 function True_Parent (N : Node_Id) return Node_Id;
5424 -- For a subunit, return parent of corresponding stub
5430 function Earlier (N1, N2 : Node_Id) return Boolean is
5436 procedure Find_Depth (P : in out Node_Id; D : in out Integer);
5437 -- Find distance from given node to enclosing compilation unit
5443 procedure Find_Depth (P : in out Node_Id; D : in out Integer) is
5446 and then Nkind (P) /= N_Compilation_Unit
5448 P := True_Parent (P);
5453 -- Start of procesing for Earlier
5456 Find_Depth (P1, D1);
5457 Find_Depth (P2, D2);
5467 P1 := True_Parent (P1);
5472 P2 := True_Parent (P2);
5476 -- At this point P1 and P2 are at the same distance from the root.
5477 -- We examine their parents until we find a common declarative
5478 -- list, at which point we can establish their relative placement
5479 -- by comparing their ultimate slocs. If we reach the root,
5480 -- N1 and N2 do not descend from the same declarative list (e.g.
5481 -- one is nested in the declarative part and the other is in a block
5482 -- in the statement part) and the earlier one is already frozen.
5484 while not Is_List_Member (P1)
5485 or else not Is_List_Member (P2)
5486 or else List_Containing (P1) /= List_Containing (P2)
5488 P1 := True_Parent (P1);
5489 P2 := True_Parent (P2);
5491 if Nkind (Parent (P1)) = N_Subunit then
5492 P1 := Corresponding_Stub (Parent (P1));
5495 if Nkind (Parent (P2)) = N_Subunit then
5496 P2 := Corresponding_Stub (Parent (P2));
5505 Top_Level_Location (Sloc (P1)) < Top_Level_Location (Sloc (P2));
5508 --------------------
5509 -- Enclosing_Body --
5510 --------------------
5512 function Enclosing_Body (N : Node_Id) return Node_Id is
5513 P : Node_Id := Parent (N);
5517 and then Nkind (Parent (P)) /= N_Compilation_Unit
5519 if Nkind (P) = N_Package_Body then
5521 if Nkind (Parent (P)) = N_Subunit then
5522 return Corresponding_Stub (Parent (P));
5528 P := True_Parent (P);
5534 -------------------------
5535 -- Package_Freeze_Node --
5536 -------------------------
5538 function Package_Freeze_Node (B : Node_Id) return Node_Id is
5542 if Nkind (B) = N_Package_Body then
5543 Id := Corresponding_Spec (B);
5545 else pragma Assert (Nkind (B) = N_Package_Body_Stub);
5546 Id := Corresponding_Spec (Proper_Body (Unit (Library_Unit (B))));
5549 Ensure_Freeze_Node (Id);
5550 return Freeze_Node (Id);
5551 end Package_Freeze_Node;
5557 function True_Parent (N : Node_Id) return Node_Id is
5559 if Nkind (Parent (N)) = N_Subunit then
5560 return Parent (Corresponding_Stub (Parent (N)));
5566 -- Start of processing of Freeze_Subprogram_Body
5569 -- If the instance and the generic body appear within the same
5570 -- unit, and the instance preceeds the generic, the freeze node for
5571 -- the instance must appear after that of the generic. If the generic
5572 -- is nested within another instance I2, then current instance must
5573 -- be frozen after I2. In both cases, the freeze nodes are those of
5574 -- enclosing packages. Otherwise, the freeze node is placed at the end
5575 -- of the current declarative part.
5577 Enc_G := Enclosing_Body (Gen_Body);
5578 Enc_I := Enclosing_Body (Inst_Node);
5579 Ensure_Freeze_Node (Pack_Id);
5580 F_Node := Freeze_Node (Pack_Id);
5582 if Is_Generic_Instance (Par)
5583 and then Present (Freeze_Node (Par))
5585 In_Same_Declarative_Part (Freeze_Node (Par), Inst_Node)
5587 if ABE_Is_Certain (Get_Package_Instantiation_Node (Par)) then
5589 -- The parent was a premature instantiation. Insert freeze
5590 -- node at the end the current declarative part.
5592 Insert_After_Last_Decl (Inst_Node, F_Node);
5595 Insert_After (Freeze_Node (Par), F_Node);
5598 -- The body enclosing the instance should be frozen after the body
5599 -- that includes the generic, because the body of the instance may
5600 -- make references to entities therein. If the two are not in the
5601 -- same declarative part, or if the one enclosing the instance is
5602 -- frozen already, freeze the instance at the end of the current
5603 -- declarative part.
5605 elsif Is_Generic_Instance (Par)
5606 and then Present (Freeze_Node (Par))
5607 and then Present (Enc_I)
5609 if In_Same_Declarative_Part (Freeze_Node (Par), Enc_I)
5611 (Nkind (Enc_I) = N_Package_Body
5613 In_Same_Declarative_Part (Freeze_Node (Par), Parent (Enc_I)))
5615 -- The enclosing package may contain several instances. Rather
5616 -- than computing the earliest point at which to insert its
5617 -- freeze node, we place it at the end of the declarative part
5618 -- of the parent of the generic.
5620 Insert_After_Last_Decl
5621 (Freeze_Node (Par), Package_Freeze_Node (Enc_I));
5624 Insert_After_Last_Decl (Inst_Node, F_Node);
5626 elsif Present (Enc_G)
5627 and then Present (Enc_I)
5628 and then Enc_G /= Enc_I
5629 and then Earlier (Inst_Node, Gen_Body)
5631 if Nkind (Enc_G) = N_Package_Body then
5632 E_G_Id := Corresponding_Spec (Enc_G);
5633 else pragma Assert (Nkind (Enc_G) = N_Package_Body_Stub);
5635 Corresponding_Spec (Proper_Body (Unit (Library_Unit (Enc_G))));
5638 -- Freeze package that encloses instance, and place node after
5639 -- package that encloses generic. If enclosing package is already
5640 -- frozen we have to assume it is at the proper place. This may
5641 -- be a potential ABE that requires dynamic checking.
5643 Insert_After_Last_Decl (Enc_G, Package_Freeze_Node (Enc_I));
5645 -- Freeze enclosing subunit before instance
5647 Ensure_Freeze_Node (E_G_Id);
5649 if not Is_List_Member (Freeze_Node (E_G_Id)) then
5650 Insert_After (Enc_G, Freeze_Node (E_G_Id));
5653 Insert_After_Last_Decl (Inst_Node, F_Node);
5656 -- If none of the above, insert freeze node at the end of the
5657 -- current declarative part.
5659 Insert_After_Last_Decl (Inst_Node, F_Node);
5661 end Freeze_Subprogram_Body;
5667 function Get_Gen_Id (E : Assoc_Ptr) return Entity_Id is
5669 return Generic_Renamings.Table (E).Gen_Id;
5672 ---------------------
5673 -- Get_Instance_Of --
5674 ---------------------
5676 function Get_Instance_Of (A : Entity_Id) return Entity_Id is
5677 Res : constant Assoc_Ptr := Generic_Renamings_HTable.Get (A);
5680 if Res /= Assoc_Null then
5681 return Generic_Renamings.Table (Res).Act_Id;
5683 -- On exit, entity is not instantiated: not a generic parameter,
5684 -- or else parameter of an inner generic unit.
5688 end Get_Instance_Of;
5690 ------------------------------------
5691 -- Get_Package_Instantiation_Node --
5692 ------------------------------------
5694 function Get_Package_Instantiation_Node (A : Entity_Id) return Node_Id is
5695 Decl : Node_Id := Unit_Declaration_Node (A);
5699 -- If the instantiation is a compilation unit that does not need a
5700 -- body then the instantiation node has been rewritten as a package
5701 -- declaration for the instance, and we return the original node.
5703 -- If it is a compilation unit and the instance node has not been
5704 -- rewritten, then it is still the unit of the compilation. Finally,
5705 -- if a body is present, this is a parent of the main unit whose body
5706 -- has been compiled for inlining purposes, and the instantiation node
5707 -- has been rewritten with the instance body.
5709 -- Otherwise the instantiation node appears after the declaration.
5710 -- If the entity is a formal package, the declaration may have been
5711 -- rewritten as a generic declaration (in the case of a formal with a
5712 -- box) or left as a formal package declaration if it has actuals, and
5713 -- is found with a forward search.
5715 if Nkind (Parent (Decl)) = N_Compilation_Unit then
5716 if Nkind (Decl) = N_Package_Declaration
5717 and then Present (Corresponding_Body (Decl))
5719 Decl := Unit_Declaration_Node (Corresponding_Body (Decl));
5722 if Nkind (Original_Node (Decl)) = N_Package_Instantiation then
5723 return Original_Node (Decl);
5725 return Unit (Parent (Decl));
5728 elsif Nkind (Decl) = N_Generic_Package_Declaration
5729 and then Nkind (Original_Node (Decl)) = N_Formal_Package_Declaration
5731 return Original_Node (Decl);
5734 Inst := Next (Decl);
5735 while Nkind (Inst) /= N_Package_Instantiation
5736 and then Nkind (Inst) /= N_Formal_Package_Declaration
5743 end Get_Package_Instantiation_Node;
5745 ------------------------
5746 -- Has_Been_Exchanged --
5747 ------------------------
5749 function Has_Been_Exchanged (E : Entity_Id) return Boolean is
5750 Next : Elmt_Id := First_Elmt (Exchanged_Views);
5753 while Present (Next) loop
5754 if Full_View (Node (Next)) = E then
5762 end Has_Been_Exchanged;
5768 function Hash (F : Entity_Id) return HTable_Range is
5770 return HTable_Range (F mod HTable_Size);
5773 ------------------------
5774 -- Hide_Current_Scope --
5775 ------------------------
5777 procedure Hide_Current_Scope is
5778 C : constant Entity_Id := Current_Scope;
5782 Set_Is_Hidden_Open_Scope (C);
5783 E := First_Entity (C);
5785 while Present (E) loop
5786 if Is_Immediately_Visible (E) then
5787 Set_Is_Immediately_Visible (E, False);
5788 Append_Elmt (E, Hidden_Entities);
5794 -- Make the scope name invisible as well. This is necessary, but
5795 -- might conflict with calls to Rtsfind later on, in case the scope
5796 -- is a predefined one. There is no clean solution to this problem, so
5797 -- for now we depend on the user not redefining Standard itself in one
5798 -- of the parent units.
5800 if Is_Immediately_Visible (C)
5801 and then C /= Standard_Standard
5803 Set_Is_Immediately_Visible (C, False);
5804 Append_Elmt (C, Hidden_Entities);
5807 end Hide_Current_Scope;
5813 procedure Init_Env is
5814 Saved : Instance_Env;
5817 Saved.Ada_Version := Ada_Version;
5818 Saved.Instantiated_Parent := Current_Instantiated_Parent;
5819 Saved.Exchanged_Views := Exchanged_Views;
5820 Saved.Hidden_Entities := Hidden_Entities;
5821 Saved.Current_Sem_Unit := Current_Sem_Unit;
5822 Saved.Parent_Unit_Visible := Parent_Unit_Visible;
5823 Instance_Envs.Increment_Last;
5824 Instance_Envs.Table (Instance_Envs.Last) := Saved;
5826 Exchanged_Views := New_Elmt_List;
5827 Hidden_Entities := New_Elmt_List;
5829 -- Make dummy entry for Instantiated parent. If generic unit is
5830 -- legal, this is set properly in Set_Instance_Env.
5832 Current_Instantiated_Parent :=
5833 (Current_Scope, Current_Scope, Assoc_Null);
5836 ------------------------------
5837 -- In_Same_Declarative_Part --
5838 ------------------------------
5840 function In_Same_Declarative_Part
5842 Inst : Node_Id) return Boolean
5844 Decls : constant Node_Id := Parent (F_Node);
5845 Nod : Node_Id := Parent (Inst);
5848 while Present (Nod) loop
5852 elsif Nkind (Nod) = N_Subprogram_Body
5853 or else Nkind (Nod) = N_Package_Body
5854 or else Nkind (Nod) = N_Task_Body
5855 or else Nkind (Nod) = N_Protected_Body
5856 or else Nkind (Nod) = N_Block_Statement
5860 elsif Nkind (Nod) = N_Subunit then
5861 Nod := Corresponding_Stub (Nod);
5863 elsif Nkind (Nod) = N_Compilation_Unit then
5866 Nod := Parent (Nod);
5871 end In_Same_Declarative_Part;
5873 ---------------------
5874 -- In_Main_Context --
5875 ---------------------
5877 function In_Main_Context (E : Entity_Id) return Boolean is
5883 if not Is_Compilation_Unit (E)
5884 or else Ekind (E) /= E_Package
5885 or else In_Private_Part (E)
5890 Context := Context_Items (Cunit (Main_Unit));
5892 Clause := First (Context);
5893 while Present (Clause) loop
5894 if Nkind (Clause) = N_With_Clause then
5895 Nam := Name (Clause);
5897 -- If the current scope is part of the context of the main unit,
5898 -- analysis of the corresponding with_clause is not complete, and
5899 -- the entity is not set. We use the Chars field directly, which
5900 -- might produce false positives in rare cases, but guarantees
5901 -- that we produce all the instance bodies we will need.
5903 if (Nkind (Nam) = N_Identifier
5904 and then Chars (Nam) = Chars (E))
5905 or else (Nkind (Nam) = N_Selected_Component
5906 and then Chars (Selector_Name (Nam)) = Chars (E))
5916 end In_Main_Context;
5918 ---------------------
5919 -- Inherit_Context --
5920 ---------------------
5922 procedure Inherit_Context (Gen_Decl : Node_Id; Inst : Node_Id) is
5923 Current_Context : List_Id;
5924 Current_Unit : Node_Id;
5929 if Nkind (Parent (Gen_Decl)) = N_Compilation_Unit then
5931 -- The inherited context is attached to the enclosing compilation
5932 -- unit. This is either the main unit, or the declaration for the
5933 -- main unit (in case the instantation appears within the package
5934 -- declaration and the main unit is its body).
5936 Current_Unit := Parent (Inst);
5937 while Present (Current_Unit)
5938 and then Nkind (Current_Unit) /= N_Compilation_Unit
5940 Current_Unit := Parent (Current_Unit);
5943 Current_Context := Context_Items (Current_Unit);
5945 Item := First (Context_Items (Parent (Gen_Decl)));
5946 while Present (Item) loop
5947 if Nkind (Item) = N_With_Clause then
5948 New_I := New_Copy (Item);
5949 Set_Implicit_With (New_I, True);
5950 Append (New_I, Current_Context);
5956 end Inherit_Context;
5962 procedure Initialize is
5964 Generic_Renamings.Init;
5967 Generic_Renamings_HTable.Reset;
5968 Circularity_Detected := False;
5969 Exchanged_Views := No_Elist;
5970 Hidden_Entities := No_Elist;
5973 ----------------------------
5974 -- Insert_After_Last_Decl --
5975 ----------------------------
5977 procedure Insert_After_Last_Decl (N : Node_Id; F_Node : Node_Id) is
5978 L : List_Id := List_Containing (N);
5979 P : constant Node_Id := Parent (L);
5982 if not Is_List_Member (F_Node) then
5983 if Nkind (P) = N_Package_Specification
5984 and then L = Visible_Declarations (P)
5985 and then Present (Private_Declarations (P))
5986 and then not Is_Empty_List (Private_Declarations (P))
5988 L := Private_Declarations (P);
5991 Insert_After (Last (L), F_Node);
5993 end Insert_After_Last_Decl;
5999 procedure Install_Body
6000 (Act_Body : Node_Id;
6005 Act_Id : constant Entity_Id := Corresponding_Spec (Act_Body);
6006 Act_Unit : constant Node_Id := Unit (Cunit (Get_Source_Unit (N)));
6007 Gen_Id : constant Entity_Id := Corresponding_Spec (Gen_Body);
6008 Par : constant Entity_Id := Scope (Gen_Id);
6009 Gen_Unit : constant Node_Id :=
6010 Unit (Cunit (Get_Source_Unit (Gen_Decl)));
6011 Orig_Body : Node_Id := Gen_Body;
6013 Body_Unit : Node_Id;
6015 Must_Delay : Boolean;
6017 function Enclosing_Subp (Id : Entity_Id) return Entity_Id;
6018 -- Find subprogram (if any) that encloses instance and/or generic body
6020 function True_Sloc (N : Node_Id) return Source_Ptr;
6021 -- If the instance is nested inside a generic unit, the Sloc of the
6022 -- instance indicates the place of the original definition, not the
6023 -- point of the current enclosing instance. Pending a better usage of
6024 -- Slocs to indicate instantiation places, we determine the place of
6025 -- origin of a node by finding the maximum sloc of any ancestor node.
6026 -- Why is this not equivalent to Top_Level_Location ???
6028 --------------------
6029 -- Enclosing_Subp --
6030 --------------------
6032 function Enclosing_Subp (Id : Entity_Id) return Entity_Id is
6033 Scop : Entity_Id := Scope (Id);
6036 while Scop /= Standard_Standard
6037 and then not Is_Overloadable (Scop)
6039 Scop := Scope (Scop);
6049 function True_Sloc (N : Node_Id) return Source_Ptr is
6056 while Present (N1) and then N1 /= Act_Unit loop
6057 if Sloc (N1) > Res then
6067 -- Start of processing for Install_Body
6070 -- If the body is a subunit, the freeze point is the corresponding
6071 -- stub in the current compilation, not the subunit itself.
6073 if Nkind (Parent (Gen_Body)) = N_Subunit then
6074 Orig_Body := Corresponding_Stub (Parent (Gen_Body));
6076 Orig_Body := Gen_Body;
6079 Body_Unit := Unit (Cunit (Get_Source_Unit (Orig_Body)));
6081 -- If the instantiation and the generic definition appear in the
6082 -- same package declaration, this is an early instantiation.
6083 -- If they appear in the same declarative part, it is an early
6084 -- instantiation only if the generic body appears textually later,
6085 -- and the generic body is also in the main unit.
6087 -- If instance is nested within a subprogram, and the generic body is
6088 -- not, the instance is delayed because the enclosing body is. If
6089 -- instance and body are within the same scope, or the same sub-
6090 -- program body, indicate explicitly that the instance is delayed.
6093 (Gen_Unit = Act_Unit
6094 and then ((Nkind (Gen_Unit) = N_Package_Declaration)
6095 or else Nkind (Gen_Unit) = N_Generic_Package_Declaration
6096 or else (Gen_Unit = Body_Unit
6097 and then True_Sloc (N) < Sloc (Orig_Body)))
6098 and then Is_In_Main_Unit (Gen_Unit)
6099 and then (Scope (Act_Id) = Scope (Gen_Id)
6101 Enclosing_Subp (Act_Id) = Enclosing_Subp (Gen_Id)));
6103 -- If this is an early instantiation, the freeze node is placed after
6104 -- the generic body. Otherwise, if the generic appears in an instance,
6105 -- we cannot freeze the current instance until the outer one is frozen.
6106 -- This is only relevant if the current instance is nested within some
6107 -- inner scope not itself within the outer instance. If this scope is
6108 -- a package body in the same declarative part as the outer instance,
6109 -- then that body needs to be frozen after the outer instance. Finally,
6110 -- if no delay is needed, we place the freeze node at the end of the
6111 -- current declarative part.
6113 if Expander_Active then
6114 Ensure_Freeze_Node (Act_Id);
6115 F_Node := Freeze_Node (Act_Id);
6118 Insert_After (Orig_Body, F_Node);
6120 elsif Is_Generic_Instance (Par)
6121 and then Present (Freeze_Node (Par))
6122 and then Scope (Act_Id) /= Par
6124 -- Freeze instance of inner generic after instance of enclosing
6127 if In_Same_Declarative_Part (Freeze_Node (Par), N) then
6128 Insert_After (Freeze_Node (Par), F_Node);
6130 -- Freeze package enclosing instance of inner generic after
6131 -- instance of enclosing generic.
6133 elsif Nkind (Parent (N)) = N_Package_Body
6134 and then In_Same_Declarative_Part (Freeze_Node (Par), Parent (N))
6138 Enclosing : constant Entity_Id :=
6139 Corresponding_Spec (Parent (N));
6142 Insert_After_Last_Decl (N, F_Node);
6143 Ensure_Freeze_Node (Enclosing);
6145 if not Is_List_Member (Freeze_Node (Enclosing)) then
6146 Insert_After (Freeze_Node (Par), Freeze_Node (Enclosing));
6151 Insert_After_Last_Decl (N, F_Node);
6155 Insert_After_Last_Decl (N, F_Node);
6159 Set_Is_Frozen (Act_Id);
6160 Insert_Before (N, Act_Body);
6161 Mark_Rewrite_Insertion (Act_Body);
6164 --------------------
6165 -- Install_Parent --
6166 --------------------
6168 procedure Install_Parent (P : Entity_Id; In_Body : Boolean := False) is
6169 Ancestors : constant Elist_Id := New_Elmt_List;
6170 S : constant Entity_Id := Current_Scope;
6171 Inst_Par : Entity_Id;
6172 First_Par : Entity_Id;
6173 Inst_Node : Node_Id;
6174 Gen_Par : Entity_Id;
6175 First_Gen : Entity_Id;
6178 procedure Install_Formal_Packages (Par : Entity_Id);
6179 -- If any of the formals of the parent are formal packages with box,
6180 -- their formal parts are visible in the parent and thus in the child
6181 -- unit as well. Analogous to what is done in Check_Generic_Actuals
6182 -- for the unit itself.
6184 procedure Install_Noninstance_Specs (Par : Entity_Id);
6185 -- Install the scopes of noninstance parent units ending with Par
6187 procedure Install_Spec (Par : Entity_Id);
6188 -- The child unit is within the declarative part of the parent, so
6189 -- the declarations within the parent are immediately visible.
6191 -----------------------------
6192 -- Install_Formal_Packages --
6193 -----------------------------
6195 procedure Install_Formal_Packages (Par : Entity_Id) is
6199 E := First_Entity (Par);
6200 while Present (E) loop
6201 if Ekind (E) = E_Package
6202 and then Nkind (Parent (E)) = N_Package_Renaming_Declaration
6204 -- If this is the renaming for the parent instance, done
6206 if Renamed_Object (E) = Par then
6209 -- The visibility of a formal of an enclosing generic is
6212 elsif Denotes_Formal_Package (E) then
6215 elsif Present (Associated_Formal_Package (E))
6216 and then Box_Present (Parent (Associated_Formal_Package (E)))
6218 Check_Generic_Actuals (Renamed_Object (E), True);
6219 Set_Is_Hidden (E, False);
6225 end Install_Formal_Packages;
6227 -------------------------------
6228 -- Install_Noninstance_Specs --
6229 -------------------------------
6231 procedure Install_Noninstance_Specs (Par : Entity_Id) is
6234 and then Par /= Standard_Standard
6235 and then not In_Open_Scopes (Par)
6237 Install_Noninstance_Specs (Scope (Par));
6240 end Install_Noninstance_Specs;
6246 procedure Install_Spec (Par : Entity_Id) is
6247 Spec : constant Node_Id :=
6248 Specification (Unit_Declaration_Node (Par));
6251 if not Is_Child_Unit (Par) then
6252 Parent_Unit_Visible := Is_Immediately_Visible (Par);
6256 Set_Is_Immediately_Visible (Par);
6257 Install_Visible_Declarations (Par);
6258 Install_Private_Declarations (Par);
6259 Set_Use (Visible_Declarations (Spec));
6260 Set_Use (Private_Declarations (Spec));
6263 -- Start of processing for Install_Parent
6266 -- We need to install the parent instance to compile the instantiation
6267 -- of the child, but the child instance must appear in the current
6268 -- scope. Given that we cannot place the parent above the current
6269 -- scope in the scope stack, we duplicate the current scope and unstack
6270 -- both after the instantiation is complete.
6272 -- If the parent is itself the instantiation of a child unit, we must
6273 -- also stack the instantiation of its parent, and so on. Each such
6274 -- ancestor is the prefix of the name in a prior instantiation.
6276 -- If this is a nested instance, the parent unit itself resolves to
6277 -- a renaming of the parent instance, whose declaration we need.
6279 -- Finally, the parent may be a generic (not an instance) when the
6280 -- child unit appears as a formal package.
6284 if Present (Renamed_Entity (Inst_Par)) then
6285 Inst_Par := Renamed_Entity (Inst_Par);
6288 First_Par := Inst_Par;
6291 Generic_Parent (Specification (Unit_Declaration_Node (Inst_Par)));
6293 First_Gen := Gen_Par;
6295 while Present (Gen_Par)
6296 and then Is_Child_Unit (Gen_Par)
6298 -- Load grandparent instance as well
6300 Inst_Node := Get_Package_Instantiation_Node (Inst_Par);
6302 if Nkind (Name (Inst_Node)) = N_Expanded_Name then
6303 Inst_Par := Entity (Prefix (Name (Inst_Node)));
6305 if Present (Renamed_Entity (Inst_Par)) then
6306 Inst_Par := Renamed_Entity (Inst_Par);
6311 (Specification (Unit_Declaration_Node (Inst_Par)));
6313 if Present (Gen_Par) then
6314 Prepend_Elmt (Inst_Par, Ancestors);
6317 -- Parent is not the name of an instantiation
6319 Install_Noninstance_Specs (Inst_Par);
6331 if Present (First_Gen) then
6332 Append_Elmt (First_Par, Ancestors);
6335 Install_Noninstance_Specs (First_Par);
6338 if not Is_Empty_Elmt_List (Ancestors) then
6339 Elmt := First_Elmt (Ancestors);
6341 while Present (Elmt) loop
6342 Install_Spec (Node (Elmt));
6343 Install_Formal_Packages (Node (Elmt));
6354 --------------------------------
6355 -- Instantiate_Formal_Package --
6356 --------------------------------
6358 function Instantiate_Formal_Package
6361 Analyzed_Formal : Node_Id) return List_Id
6363 Loc : constant Source_Ptr := Sloc (Actual);
6364 Actual_Pack : Entity_Id;
6365 Formal_Pack : Entity_Id;
6366 Gen_Parent : Entity_Id;
6369 Parent_Spec : Node_Id;
6371 procedure Find_Matching_Actual
6373 Act : in out Entity_Id);
6374 -- We need to associate each formal entity in the formal package
6375 -- with the corresponding entity in the actual package. The actual
6376 -- package has been analyzed and possibly expanded, and as a result
6377 -- there is no one-to-one correspondence between the two lists (for
6378 -- example, the actual may include subtypes, itypes, and inherited
6379 -- primitive operations, interspersed among the renaming declarations
6380 -- for the actuals) . We retrieve the corresponding actual by name
6381 -- because each actual has the same name as the formal, and they do
6382 -- appear in the same order.
6384 function Formal_Entity
6386 Act_Ent : Entity_Id) return Entity_Id;
6387 -- Returns the entity associated with the given formal F. In the
6388 -- case where F is a formal package, this function will iterate
6389 -- through all of F's formals and enter map associations from the
6390 -- actuals occurring in the formal package's corresponding actual
6391 -- package (obtained via Act_Ent) to the formal package's formal
6392 -- parameters. This function is called recursively for arbitrary
6393 -- levels of formal packages.
6395 function Is_Instance_Of
6396 (Act_Spec : Entity_Id;
6397 Gen_Anc : Entity_Id) return Boolean;
6398 -- The actual can be an instantiation of a generic within another
6399 -- instance, in which case there is no direct link from it to the
6400 -- original generic ancestor. In that case, we recognize that the
6401 -- ultimate ancestor is the same by examining names and scopes.
6403 procedure Map_Entities (Form : Entity_Id; Act : Entity_Id);
6404 -- Within the generic part, entities in the formal package are
6405 -- visible. To validate subsequent type declarations, indicate
6406 -- the correspondence betwen the entities in the analyzed formal,
6407 -- and the entities in the actual package. There are three packages
6408 -- involved in the instantiation of a formal package: the parent
6409 -- generic P1 which appears in the generic declaration, the fake
6410 -- instantiation P2 which appears in the analyzed generic, and whose
6411 -- visible entities may be used in subsequent formals, and the actual
6412 -- P3 in the instance. To validate subsequent formals, me indicate
6413 -- that the entities in P2 are mapped into those of P3. The mapping of
6414 -- entities has to be done recursively for nested packages.
6416 procedure Process_Nested_Formal (Formal : Entity_Id);
6417 -- If the current formal is declared with a box, its own formals are
6418 -- visible in the instance, as they were in the generic, and their
6419 -- Hidden flag must be reset. If some of these formals are themselves
6420 -- packages declared with a box, the processing must be recursive.
6422 --------------------------
6423 -- Find_Matching_Actual --
6424 --------------------------
6426 procedure Find_Matching_Actual
6428 Act : in out Entity_Id)
6430 Formal_Ent : Entity_Id;
6433 case Nkind (Original_Node (F)) is
6434 when N_Formal_Object_Declaration |
6435 N_Formal_Type_Declaration =>
6436 Formal_Ent := Defining_Identifier (F);
6438 while Chars (Act) /= Chars (Formal_Ent) loop
6442 when N_Formal_Subprogram_Declaration |
6443 N_Formal_Package_Declaration |
6444 N_Package_Declaration |
6445 N_Generic_Package_Declaration =>
6446 Formal_Ent := Defining_Entity (F);
6448 while Chars (Act) /= Chars (Formal_Ent) loop
6453 raise Program_Error;
6455 end Find_Matching_Actual;
6461 function Formal_Entity
6463 Act_Ent : Entity_Id) return Entity_Id
6465 Orig_Node : Node_Id := F;
6466 Act_Pkg : Entity_Id;
6469 case Nkind (Original_Node (F)) is
6470 when N_Formal_Object_Declaration =>
6471 return Defining_Identifier (F);
6473 when N_Formal_Type_Declaration =>
6474 return Defining_Identifier (F);
6476 when N_Formal_Subprogram_Declaration =>
6477 return Defining_Unit_Name (Specification (F));
6479 when N_Package_Declaration =>
6480 return Defining_Unit_Name (Specification (F));
6482 when N_Formal_Package_Declaration |
6483 N_Generic_Package_Declaration =>
6485 if Nkind (F) = N_Generic_Package_Declaration then
6486 Orig_Node := Original_Node (F);
6491 -- Find matching actual package, skipping over itypes and
6492 -- other entities generated when analyzing the formal. We
6493 -- know that if the instantiation is legal then there is
6494 -- a matching package for the formal.
6496 while Ekind (Act_Pkg) /= E_Package loop
6497 Act_Pkg := Next_Entity (Act_Pkg);
6501 Actual_Ent : Entity_Id := First_Entity (Act_Pkg);
6502 Formal_Node : Node_Id;
6503 Formal_Ent : Entity_Id;
6505 Gen_Decl : constant Node_Id :=
6506 Unit_Declaration_Node
6507 (Entity (Name (Orig_Node)));
6509 Formals : constant List_Id :=
6510 Generic_Formal_Declarations (Gen_Decl);
6513 if Present (Formals) then
6514 Formal_Node := First_Non_Pragma (Formals);
6516 Formal_Node := Empty;
6519 while Present (Actual_Ent)
6520 and then Present (Formal_Node)
6521 and then Actual_Ent /= First_Private_Entity (Act_Ent)
6523 -- ??? Are the following calls also needed here:
6525 -- Set_Is_Hidden (Actual_Ent, False);
6526 -- Set_Is_Potentially_Use_Visible
6527 -- (Actual_Ent, In_Use (Act_Ent));
6529 Formal_Ent := Formal_Entity (Formal_Node, Actual_Ent);
6530 if Present (Formal_Ent) then
6531 Set_Instance_Of (Formal_Ent, Actual_Ent);
6533 Next_Non_Pragma (Formal_Node);
6535 Next_Entity (Actual_Ent);
6539 return Defining_Identifier (Orig_Node);
6541 when N_Use_Package_Clause =>
6544 when N_Use_Type_Clause =>
6547 -- We return Empty for all other encountered forms of
6548 -- declarations because there are some cases of nonformal
6549 -- sorts of declaration that can show up (e.g., when array
6550 -- formals are present). Since it's not clear what kinds
6551 -- can appear among the formals, we won't raise failure here.
6559 --------------------
6560 -- Is_Instance_Of --
6561 --------------------
6563 function Is_Instance_Of
6564 (Act_Spec : Entity_Id;
6565 Gen_Anc : Entity_Id) return Boolean
6567 Gen_Par : constant Entity_Id := Generic_Parent (Act_Spec);
6570 if No (Gen_Par) then
6573 -- Simplest case: the generic parent of the actual is the formal
6575 elsif Gen_Par = Gen_Anc then
6578 elsif Chars (Gen_Par) /= Chars (Gen_Anc) then
6581 -- The actual may be obtained through several instantiations. Its
6582 -- scope must itself be an instance of a generic declared in the
6583 -- same scope as the formal. Any other case is detected above.
6585 elsif not Is_Generic_Instance (Scope (Gen_Par)) then
6589 return Generic_Parent (Parent (Scope (Gen_Par))) = Scope (Gen_Anc);
6597 procedure Map_Entities (Form : Entity_Id; Act : Entity_Id) is
6602 Set_Instance_Of (Form, Act);
6604 -- Traverse formal and actual package to map the corresponding
6605 -- entities. We skip over internal entities that may be generated
6606 -- during semantic analysis, and find the matching entities by
6607 -- name, given that they must appear in the same order.
6609 E1 := First_Entity (Form);
6610 E2 := First_Entity (Act);
6612 and then E1 /= First_Private_Entity (Form)
6614 if not Is_Internal (E1)
6615 and then not Is_Class_Wide_Type (E1)
6616 and then Present (Parent (E1))
6619 and then Chars (E2) /= Chars (E1)
6627 Set_Instance_Of (E1, E2);
6630 and then Is_Tagged_Type (E2)
6633 (Class_Wide_Type (E1), Class_Wide_Type (E2));
6636 if Ekind (E1) = E_Package
6637 and then No (Renamed_Object (E1))
6639 Map_Entities (E1, E2);
6648 ---------------------------
6649 -- Process_Nested_Formal --
6650 ---------------------------
6652 procedure Process_Nested_Formal (Formal : Entity_Id) is
6656 if Present (Associated_Formal_Package (Formal))
6657 and then Box_Present (Parent (Associated_Formal_Package (Formal)))
6659 Ent := First_Entity (Formal);
6660 while Present (Ent) loop
6661 Set_Is_Hidden (Ent, False);
6662 Set_Is_Potentially_Use_Visible
6663 (Ent, Is_Potentially_Use_Visible (Formal));
6665 if Ekind (Ent) = E_Package then
6666 exit when Renamed_Entity (Ent) = Renamed_Entity (Formal);
6667 Process_Nested_Formal (Ent);
6673 end Process_Nested_Formal;
6675 -- Start of processing for Instantiate_Formal_Package
6680 if not Is_Entity_Name (Actual)
6681 or else Ekind (Entity (Actual)) /= E_Package
6684 ("expect package instance to instantiate formal", Actual);
6685 Abandon_Instantiation (Actual);
6686 raise Program_Error;
6689 Actual_Pack := Entity (Actual);
6690 Set_Is_Instantiated (Actual_Pack);
6692 -- The actual may be a renamed package, or an outer generic
6693 -- formal package whose instantiation is converted into a renaming.
6695 if Present (Renamed_Object (Actual_Pack)) then
6696 Actual_Pack := Renamed_Object (Actual_Pack);
6699 if Nkind (Analyzed_Formal) = N_Formal_Package_Declaration then
6700 Gen_Parent := Get_Instance_Of (Entity (Name (Analyzed_Formal)));
6701 Formal_Pack := Defining_Identifier (Analyzed_Formal);
6704 Generic_Parent (Specification (Analyzed_Formal));
6706 Defining_Unit_Name (Specification (Analyzed_Formal));
6709 if Nkind (Parent (Actual_Pack)) = N_Defining_Program_Unit_Name then
6710 Parent_Spec := Specification (Unit_Declaration_Node (Actual_Pack));
6712 Parent_Spec := Parent (Actual_Pack);
6715 if Gen_Parent = Any_Id then
6717 ("previous error in declaration of formal package", Actual);
6718 Abandon_Instantiation (Actual);
6721 Is_Instance_Of (Parent_Spec, Get_Instance_Of (Gen_Parent))
6727 ("actual parameter must be instance of&", Actual, Gen_Parent);
6728 Abandon_Instantiation (Actual);
6731 Set_Instance_Of (Defining_Identifier (Formal), Actual_Pack);
6732 Map_Entities (Formal_Pack, Actual_Pack);
6735 Make_Package_Renaming_Declaration (Loc,
6736 Defining_Unit_Name => New_Copy (Defining_Identifier (Formal)),
6737 Name => New_Reference_To (Actual_Pack, Loc));
6739 Set_Associated_Formal_Package (Defining_Unit_Name (Nod),
6740 Defining_Identifier (Formal));
6741 Decls := New_List (Nod);
6743 -- If the formal F has a box, then the generic declarations are
6744 -- visible in the generic G. In an instance of G, the corresponding
6745 -- entities in the actual for F (which are the actuals for the
6746 -- instantiation of the generic that F denotes) must also be made
6747 -- visible for analysis of the current instance. On exit from the
6748 -- current instance, those entities are made private again. If the
6749 -- actual is currently in use, these entities are also use-visible.
6751 -- The loop through the actual entities also steps through the
6752 -- formal entities and enters associations from formals to
6753 -- actuals into the renaming map. This is necessary to properly
6754 -- handle checking of actual parameter associations for later
6755 -- formals that depend on actuals declared in the formal package.
6757 if Box_Present (Formal) then
6759 Gen_Decl : constant Node_Id :=
6760 Unit_Declaration_Node (Gen_Parent);
6761 Formals : constant List_Id :=
6762 Generic_Formal_Declarations (Gen_Decl);
6763 Actual_Ent : Entity_Id;
6764 Formal_Node : Node_Id;
6765 Formal_Ent : Entity_Id;
6768 if Present (Formals) then
6769 Formal_Node := First_Non_Pragma (Formals);
6771 Formal_Node := Empty;
6774 Actual_Ent := First_Entity (Actual_Pack);
6776 while Present (Actual_Ent)
6777 and then Actual_Ent /= First_Private_Entity (Actual_Pack)
6779 Set_Is_Hidden (Actual_Ent, False);
6780 Set_Is_Potentially_Use_Visible
6781 (Actual_Ent, In_Use (Actual_Pack));
6783 if Ekind (Actual_Ent) = E_Package then
6784 Process_Nested_Formal (Actual_Ent);
6787 if Present (Formal_Node) then
6788 Formal_Ent := Formal_Entity (Formal_Node, Actual_Ent);
6790 if Present (Formal_Ent) then
6791 Find_Matching_Actual (Formal_Node, Actual_Ent);
6792 Set_Instance_Of (Formal_Ent, Actual_Ent);
6795 Next_Non_Pragma (Formal_Node);
6798 -- No further formals to match, but the generic
6799 -- part may contain inherited operation that are
6800 -- not hidden in the enclosing instance.
6802 Next_Entity (Actual_Ent);
6808 -- If the formal is not declared with a box, reanalyze it as
6809 -- an instantiation, to verify the matching rules of 12.7. The
6810 -- actual checks are performed after the generic associations
6815 I_Pack : constant Entity_Id :=
6816 Make_Defining_Identifier (Sloc (Actual),
6817 Chars => New_Internal_Name ('P'));
6820 Set_Is_Internal (I_Pack);
6823 Make_Package_Instantiation (Sloc (Actual),
6824 Defining_Unit_Name => I_Pack,
6825 Name => New_Occurrence_Of (Gen_Parent, Sloc (Actual)),
6826 Generic_Associations =>
6827 Generic_Associations (Formal)));
6833 end Instantiate_Formal_Package;
6835 -----------------------------------
6836 -- Instantiate_Formal_Subprogram --
6837 -----------------------------------
6839 function Instantiate_Formal_Subprogram
6842 Analyzed_Formal : Node_Id) return Node_Id
6844 Loc : Source_Ptr := Sloc (Instantiation_Node);
6845 Formal_Sub : constant Entity_Id :=
6846 Defining_Unit_Name (Specification (Formal));
6847 Analyzed_S : constant Entity_Id :=
6848 Defining_Unit_Name (Specification (Analyzed_Formal));
6849 Decl_Node : Node_Id;
6853 function From_Parent_Scope (Subp : Entity_Id) return Boolean;
6854 -- If the generic is a child unit, the parent has been installed on the
6855 -- scope stack, but a default subprogram cannot resolve to something on
6856 -- the parent because that parent is not really part of the visible
6857 -- context (it is there to resolve explicit local entities). If the
6858 -- default has resolved in this way, we remove the entity from
6859 -- immediate visibility and analyze the node again to emit an error
6860 -- message or find another visible candidate.
6862 procedure Valid_Actual_Subprogram (Act : Node_Id);
6863 -- Perform legality check and raise exception on failure
6865 -----------------------
6866 -- From_Parent_Scope --
6867 -----------------------
6869 function From_Parent_Scope (Subp : Entity_Id) return Boolean is
6870 Gen_Scope : Node_Id := Scope (Analyzed_S);
6873 while Present (Gen_Scope)
6874 and then Is_Child_Unit (Gen_Scope)
6876 if Scope (Subp) = Scope (Gen_Scope) then
6880 Gen_Scope := Scope (Gen_Scope);
6884 end From_Parent_Scope;
6886 -----------------------------
6887 -- Valid_Actual_Subprogram --
6888 -----------------------------
6890 procedure Valid_Actual_Subprogram (Act : Node_Id) is
6891 Act_E : Entity_Id := Empty;
6894 if Is_Entity_Name (Act) then
6895 Act_E := Entity (Act);
6896 elsif Nkind (Act) = N_Selected_Component
6897 and then Is_Entity_Name (Selector_Name (Act))
6899 Act_E := Entity (Selector_Name (Act));
6902 if (Present (Act_E) and then Is_Overloadable (Act_E))
6903 or else Nkind (Act) = N_Attribute_Reference
6904 or else Nkind (Act) = N_Indexed_Component
6905 or else Nkind (Act) = N_Character_Literal
6906 or else Nkind (Act) = N_Explicit_Dereference
6912 ("expect subprogram or entry name in instantiation of&",
6913 Instantiation_Node, Formal_Sub);
6914 Abandon_Instantiation (Instantiation_Node);
6916 end Valid_Actual_Subprogram;
6918 -- Start of processing for Instantiate_Formal_Subprogram
6921 New_Spec := New_Copy_Tree (Specification (Formal));
6923 -- Create new entity for the actual (New_Copy_Tree does not)
6925 Set_Defining_Unit_Name
6926 (New_Spec, Make_Defining_Identifier (Loc, Chars (Formal_Sub)));
6928 -- Find entity of actual. If the actual is an attribute reference, it
6929 -- cannot be resolved here (its formal is missing) but is handled
6930 -- instead in Attribute_Renaming. If the actual is overloaded, it is
6931 -- fully resolved subsequently, when the renaming declaration for the
6932 -- formal is analyzed. If it is an explicit dereference, resolve the
6933 -- prefix but not the actual itself, to prevent interpretation as a
6936 if Present (Actual) then
6937 Loc := Sloc (Actual);
6938 Set_Sloc (New_Spec, Loc);
6940 if Nkind (Actual) = N_Operator_Symbol then
6941 Find_Direct_Name (Actual);
6943 elsif Nkind (Actual) = N_Explicit_Dereference then
6944 Analyze (Prefix (Actual));
6946 elsif Nkind (Actual) /= N_Attribute_Reference then
6950 Valid_Actual_Subprogram (Actual);
6953 elsif Present (Default_Name (Formal)) then
6954 if Nkind (Default_Name (Formal)) /= N_Attribute_Reference
6955 and then Nkind (Default_Name (Formal)) /= N_Selected_Component
6956 and then Nkind (Default_Name (Formal)) /= N_Indexed_Component
6957 and then Nkind (Default_Name (Formal)) /= N_Character_Literal
6958 and then Present (Entity (Default_Name (Formal)))
6960 Nam := New_Occurrence_Of (Entity (Default_Name (Formal)), Loc);
6962 Nam := New_Copy (Default_Name (Formal));
6963 Set_Sloc (Nam, Loc);
6966 elsif Box_Present (Formal) then
6968 -- Actual is resolved at the point of instantiation. Create
6969 -- an identifier or operator with the same name as the formal.
6971 if Nkind (Formal_Sub) = N_Defining_Operator_Symbol then
6972 Nam := Make_Operator_Symbol (Loc,
6973 Chars => Chars (Formal_Sub),
6974 Strval => No_String);
6976 Nam := Make_Identifier (Loc, Chars (Formal_Sub));
6980 Error_Msg_Sloc := Sloc (Scope (Analyzed_S));
6982 ("missing actual&", Instantiation_Node, Formal_Sub);
6984 ("\in instantiation of & declared#",
6985 Instantiation_Node, Scope (Analyzed_S));
6986 Abandon_Instantiation (Instantiation_Node);
6990 Make_Subprogram_Renaming_Declaration (Loc,
6991 Specification => New_Spec,
6994 -- If we do not have an actual and the formal specified <> then
6995 -- set to get proper default.
6997 if No (Actual) and then Box_Present (Formal) then
6998 Set_From_Default (Decl_Node);
7001 -- Gather possible interpretations for the actual before analyzing the
7002 -- instance. If overloaded, it will be resolved when analyzing the
7003 -- renaming declaration.
7005 if Box_Present (Formal)
7006 and then No (Actual)
7010 if Is_Child_Unit (Scope (Analyzed_S))
7011 and then Present (Entity (Nam))
7013 if not Is_Overloaded (Nam) then
7015 if From_Parent_Scope (Entity (Nam)) then
7016 Set_Is_Immediately_Visible (Entity (Nam), False);
7017 Set_Entity (Nam, Empty);
7018 Set_Etype (Nam, Empty);
7022 Set_Is_Immediately_Visible (Entity (Nam));
7031 Get_First_Interp (Nam, I, It);
7033 while Present (It.Nam) loop
7034 if From_Parent_Scope (It.Nam) then
7038 Get_Next_Interp (I, It);
7045 -- The generic instantiation freezes the actual. This can only be
7046 -- done once the actual is resolved, in the analysis of the renaming
7047 -- declaration. To make the formal subprogram entity available, we set
7048 -- Corresponding_Formal_Spec to point to the formal subprogram entity.
7049 -- This is also needed in Analyze_Subprogram_Renaming for the processing
7050 -- of formal abstract subprograms.
7052 Set_Corresponding_Formal_Spec (Decl_Node, Analyzed_S);
7054 -- We cannot analyze the renaming declaration, and thus find the
7055 -- actual, until the all the actuals are assembled in the instance.
7056 -- For subsequent checks of other actuals, indicate the node that
7057 -- will hold the instance of this formal.
7059 Set_Instance_Of (Analyzed_S, Nam);
7061 if Nkind (Actual) = N_Selected_Component
7062 and then Is_Task_Type (Etype (Prefix (Actual)))
7063 and then not Is_Frozen (Etype (Prefix (Actual)))
7065 -- The renaming declaration will create a body, which must appear
7066 -- outside of the instantiation, We move the renaming declaration
7067 -- out of the instance, and create an additional renaming inside,
7068 -- to prevent freezing anomalies.
7071 Anon_Id : constant Entity_Id :=
7072 Make_Defining_Identifier
7073 (Loc, New_Internal_Name ('E'));
7075 Set_Defining_Unit_Name (New_Spec, Anon_Id);
7076 Insert_Before (Instantiation_Node, Decl_Node);
7077 Analyze (Decl_Node);
7079 -- Now create renaming within the instance
7082 Make_Subprogram_Renaming_Declaration (Loc,
7083 Specification => New_Copy_Tree (New_Spec),
7084 Name => New_Occurrence_Of (Anon_Id, Loc));
7086 Set_Defining_Unit_Name (Specification (Decl_Node),
7087 Make_Defining_Identifier (Loc, Chars (Formal_Sub)));
7092 end Instantiate_Formal_Subprogram;
7094 ------------------------
7095 -- Instantiate_Object --
7096 ------------------------
7098 function Instantiate_Object
7101 Analyzed_Formal : Node_Id) return List_Id
7103 Formal_Id : constant Entity_Id := Defining_Identifier (Formal);
7104 Type_Id : constant Node_Id := Subtype_Mark (Formal);
7105 Loc : constant Source_Ptr := Sloc (Actual);
7106 Act_Assoc : constant Node_Id := Parent (Actual);
7107 Orig_Ftyp : constant Entity_Id :=
7108 Etype (Defining_Identifier (Analyzed_Formal));
7109 List : constant List_Id := New_List;
7111 Decl_Node : Node_Id;
7112 Subt_Decl : Node_Id := Empty;
7115 -- Sloc for error message on missing actual
7117 Error_Msg_Sloc := Sloc (Scope (Defining_Identifier (Analyzed_Formal)));
7119 if Get_Instance_Of (Formal_Id) /= Formal_Id then
7120 Error_Msg_N ("duplicate instantiation of generic parameter", Actual);
7123 Set_Parent (List, Parent (Actual));
7127 if Out_Present (Formal) then
7129 -- An IN OUT generic actual must be a name. The instantiation is a
7130 -- renaming declaration. The actual is the name being renamed. We
7131 -- use the actual directly, rather than a copy, because it is not
7132 -- used further in the list of actuals, and because a copy or a use
7133 -- of relocate_node is incorrect if the instance is nested within a
7134 -- generic. In order to simplify ASIS searches, the Generic_Parent
7135 -- field links the declaration to the generic association.
7140 Instantiation_Node, Formal_Id);
7142 ("\in instantiation of & declared#",
7144 Scope (Defining_Identifier (Analyzed_Formal)));
7145 Abandon_Instantiation (Instantiation_Node);
7149 Make_Object_Renaming_Declaration (Loc,
7150 Defining_Identifier => New_Copy (Formal_Id),
7151 Subtype_Mark => New_Copy_Tree (Type_Id),
7154 Set_Corresponding_Generic_Association (Decl_Node, Act_Assoc);
7156 -- The analysis of the actual may produce insert_action nodes, so
7157 -- the declaration must have a context in which to attach them.
7159 Append (Decl_Node, List);
7162 -- Return if the analysis of the actual reported some error
7164 if Etype (Actual) = Any_Type then
7168 -- This check is performed here because Analyze_Object_Renaming
7169 -- will not check it when Comes_From_Source is False. Note
7170 -- though that the check for the actual being the name of an
7171 -- object will be performed in Analyze_Object_Renaming.
7173 if Is_Object_Reference (Actual)
7174 and then Is_Dependent_Component_Of_Mutable_Object (Actual)
7177 ("illegal discriminant-dependent component for in out parameter",
7181 -- The actual has to be resolved in order to check that it is
7182 -- a variable (due to cases such as F(1), where F returns
7183 -- access to an array, and for overloaded prefixes).
7186 Get_Instance_Of (Etype (Defining_Identifier (Analyzed_Formal)));
7188 if Is_Private_Type (Ftyp)
7189 and then not Is_Private_Type (Etype (Actual))
7190 and then (Base_Type (Full_View (Ftyp)) = Base_Type (Etype (Actual))
7191 or else Base_Type (Etype (Actual)) = Ftyp)
7193 -- If the actual has the type of the full view of the formal,
7194 -- or else a non-private subtype of the formal, then
7195 -- the visibility of the formal type has changed. Add to the
7196 -- actuals a subtype declaration that will force the exchange
7197 -- of views in the body of the instance as well.
7200 Make_Subtype_Declaration (Loc,
7201 Defining_Identifier =>
7202 Make_Defining_Identifier (Loc, New_Internal_Name ('P')),
7203 Subtype_Indication => New_Occurrence_Of (Ftyp, Loc));
7205 Prepend (Subt_Decl, List);
7207 Append_Elmt (Full_View (Ftyp), Exchanged_Views);
7208 Exchange_Declarations (Ftyp);
7211 Resolve (Actual, Ftyp);
7213 if not Is_Variable (Actual) or else Paren_Count (Actual) > 0 then
7215 ("actual for& must be a variable", Actual, Formal_Id);
7217 elsif Base_Type (Ftyp) /= Base_Type (Etype (Actual)) then
7219 "type of actual does not match type of&", Actual, Formal_Id);
7223 Note_Possible_Modification (Actual);
7225 -- Check for instantiation of atomic/volatile actual for
7226 -- non-atomic/volatile formal (RM C.6 (12)).
7228 if Is_Atomic_Object (Actual)
7229 and then not Is_Atomic (Orig_Ftyp)
7232 ("cannot instantiate non-atomic formal object " &
7233 "with atomic actual", Actual);
7235 elsif Is_Volatile_Object (Actual)
7236 and then not Is_Volatile (Orig_Ftyp)
7239 ("cannot instantiate non-volatile formal object " &
7240 "with volatile actual", Actual);
7246 -- The instantiation of a generic formal in-parameter
7247 -- is a constant declaration. The actual is the expression for
7248 -- that declaration.
7250 if Present (Actual) then
7252 Decl_Node := Make_Object_Declaration (Loc,
7253 Defining_Identifier => New_Copy (Formal_Id),
7254 Constant_Present => True,
7255 Object_Definition => New_Copy_Tree (Type_Id),
7256 Expression => Actual);
7258 Set_Corresponding_Generic_Association (Decl_Node, Act_Assoc);
7260 -- A generic formal object of a tagged type is defined
7261 -- to be aliased so the new constant must also be treated
7265 (Etype (Defining_Identifier (Analyzed_Formal)))
7267 Set_Aliased_Present (Decl_Node);
7270 Append (Decl_Node, List);
7272 -- No need to repeat (pre-)analysis of some expression nodes
7273 -- already handled in Pre_Analyze_Actuals.
7275 if Nkind (Actual) /= N_Allocator then
7278 -- Return if the analysis of the actual reported some error
7280 if Etype (Actual) = Any_Type then
7286 Typ : constant Entity_Id :=
7288 (Etype (Defining_Identifier (Analyzed_Formal)));
7291 Freeze_Before (Instantiation_Node, Typ);
7293 -- If the actual is an aggregate, perform name resolution on
7294 -- its components (the analysis of an aggregate does not do
7295 -- it) to capture local names that may be hidden if the
7296 -- generic is a child unit.
7298 if Nkind (Actual) = N_Aggregate then
7299 Pre_Analyze_And_Resolve (Actual, Typ);
7303 elsif Present (Expression (Formal)) then
7305 -- Use default to construct declaration
7308 Make_Object_Declaration (Sloc (Formal),
7309 Defining_Identifier => New_Copy (Formal_Id),
7310 Constant_Present => True,
7311 Object_Definition => New_Copy (Type_Id),
7312 Expression => New_Copy_Tree (Expression (Formal)));
7314 Append (Decl_Node, List);
7315 Set_Analyzed (Expression (Decl_Node), False);
7320 Instantiation_Node, Formal_Id);
7321 Error_Msg_NE ("\in instantiation of & declared#",
7323 Scope (Defining_Identifier (Analyzed_Formal)));
7326 (Etype (Defining_Identifier (Analyzed_Formal)))
7328 -- Create dummy constant declaration so that instance can
7329 -- be analyzed, to minimize cascaded visibility errors.
7332 Make_Object_Declaration (Loc,
7333 Defining_Identifier => New_Copy (Formal_Id),
7334 Constant_Present => True,
7335 Object_Definition => New_Copy (Type_Id),
7337 Make_Attribute_Reference (Sloc (Formal_Id),
7338 Attribute_Name => Name_First,
7339 Prefix => New_Copy (Type_Id)));
7341 Append (Decl_Node, List);
7344 Abandon_Instantiation (Instantiation_Node);
7351 end Instantiate_Object;
7353 ------------------------------
7354 -- Instantiate_Package_Body --
7355 ------------------------------
7357 procedure Instantiate_Package_Body
7358 (Body_Info : Pending_Body_Info;
7359 Inlined_Body : Boolean := False)
7361 Act_Decl : constant Node_Id := Body_Info.Act_Decl;
7362 Inst_Node : constant Node_Id := Body_Info.Inst_Node;
7363 Loc : constant Source_Ptr := Sloc (Inst_Node);
7365 Gen_Id : constant Node_Id := Name (Inst_Node);
7366 Gen_Unit : constant Entity_Id := Get_Generic_Entity (Inst_Node);
7367 Gen_Decl : constant Node_Id := Unit_Declaration_Node (Gen_Unit);
7368 Act_Spec : constant Node_Id := Specification (Act_Decl);
7369 Act_Decl_Id : constant Entity_Id := Defining_Entity (Act_Spec);
7371 Act_Body_Name : Node_Id;
7373 Gen_Body_Id : Node_Id;
7375 Act_Body_Id : Entity_Id;
7377 Parent_Installed : Boolean := False;
7378 Save_Style_Check : constant Boolean := Style_Check;
7381 Gen_Body_Id := Corresponding_Body (Gen_Decl);
7383 -- The instance body may already have been processed, as the parent
7384 -- of another instance that is inlined. (Load_Parent_Of_Generic).
7386 if Present (Corresponding_Body (Instance_Spec (Inst_Node))) then
7390 Expander_Mode_Save_And_Set (Body_Info.Expander_Status);
7392 if No (Gen_Body_Id) then
7393 Load_Parent_Of_Generic (Inst_Node, Specification (Gen_Decl));
7394 Gen_Body_Id := Corresponding_Body (Gen_Decl);
7397 -- Establish global variable for sloc adjustment and for error
7400 Instantiation_Node := Inst_Node;
7402 if Present (Gen_Body_Id) then
7403 Save_Env (Gen_Unit, Act_Decl_Id);
7404 Style_Check := False;
7405 Current_Sem_Unit := Body_Info.Current_Sem_Unit;
7407 Gen_Body := Unit_Declaration_Node (Gen_Body_Id);
7409 Create_Instantiation_Source
7410 (Inst_Node, Gen_Body_Id, False, S_Adjustment);
7414 (Original_Node (Gen_Body), Empty, Instantiating => True);
7416 -- Build new name (possibly qualified) for body declaration
7418 Act_Body_Id := New_Copy (Act_Decl_Id);
7420 -- Some attributes of the spec entity are not inherited by the
7423 Set_Handler_Records (Act_Body_Id, No_List);
7425 if Nkind (Defining_Unit_Name (Act_Spec)) =
7426 N_Defining_Program_Unit_Name
7429 Make_Defining_Program_Unit_Name (Loc,
7430 Name => New_Copy_Tree (Name (Defining_Unit_Name (Act_Spec))),
7431 Defining_Identifier => Act_Body_Id);
7433 Act_Body_Name := Act_Body_Id;
7436 Set_Defining_Unit_Name (Act_Body, Act_Body_Name);
7438 Set_Corresponding_Spec (Act_Body, Act_Decl_Id);
7439 Check_Generic_Actuals (Act_Decl_Id, False);
7441 -- If it is a child unit, make the parent instance (which is an
7442 -- instance of the parent of the generic) visible. The parent
7443 -- instance is the prefix of the name of the generic unit.
7445 if Ekind (Scope (Gen_Unit)) = E_Generic_Package
7446 and then Nkind (Gen_Id) = N_Expanded_Name
7448 Install_Parent (Entity (Prefix (Gen_Id)), In_Body => True);
7449 Parent_Installed := True;
7451 elsif Is_Child_Unit (Gen_Unit) then
7452 Install_Parent (Scope (Gen_Unit), In_Body => True);
7453 Parent_Installed := True;
7456 -- If the instantiation is a library unit, and this is the main
7457 -- unit, then build the resulting compilation unit nodes for the
7458 -- instance. If this is a compilation unit but it is not the main
7459 -- unit, then it is the body of a unit in the context, that is being
7460 -- compiled because it is encloses some inlined unit or another
7461 -- generic unit being instantiated. In that case, this body is not
7462 -- part of the current compilation, and is not attached to the tree,
7463 -- but its parent must be set for analysis.
7465 if Nkind (Parent (Inst_Node)) = N_Compilation_Unit then
7467 -- Replace instance node with body of instance, and create
7468 -- new node for corresponding instance declaration.
7470 Build_Instance_Compilation_Unit_Nodes
7471 (Inst_Node, Act_Body, Act_Decl);
7472 Analyze (Inst_Node);
7474 if Parent (Inst_Node) = Cunit (Main_Unit) then
7476 -- If the instance is a child unit itself, then set the
7477 -- scope of the expanded body to be the parent of the
7478 -- instantiation (ensuring that the fully qualified name
7479 -- will be generated for the elaboration subprogram).
7481 if Nkind (Defining_Unit_Name (Act_Spec)) =
7482 N_Defining_Program_Unit_Name
7485 (Defining_Entity (Inst_Node), Scope (Act_Decl_Id));
7489 -- Case where instantiation is not a library unit
7492 -- If this is an early instantiation, i.e. appears textually
7493 -- before the corresponding body and must be elaborated first,
7494 -- indicate that the body instance is to be delayed.
7496 Install_Body (Act_Body, Inst_Node, Gen_Body, Gen_Decl);
7498 -- Now analyze the body. We turn off all checks if this is
7499 -- an internal unit, since there is no reason to have checks
7500 -- on for any predefined run-time library code. All such
7501 -- code is designed to be compiled with checks off.
7503 -- Note that we do NOT apply this criterion to children of
7504 -- GNAT (or on VMS, children of DEC). The latter units must
7505 -- suppress checks explicitly if this is needed.
7507 if Is_Predefined_File_Name
7508 (Unit_File_Name (Get_Source_Unit (Gen_Decl)))
7510 Analyze (Act_Body, Suppress => All_Checks);
7516 if not Generic_Separately_Compiled (Gen_Unit) then
7517 Inherit_Context (Gen_Body, Inst_Node);
7520 -- Remove the parent instances if they have been placed on the
7521 -- scope stack to compile the body.
7523 if Parent_Installed then
7524 Remove_Parent (In_Body => True);
7527 Restore_Private_Views (Act_Decl_Id);
7529 -- Remove the current unit from visibility if this is an instance
7530 -- that is not elaborated on the fly for inlining purposes.
7532 if not Inlined_Body then
7533 Set_Is_Immediately_Visible (Act_Decl_Id, False);
7537 Style_Check := Save_Style_Check;
7539 -- If we have no body, and the unit requires a body, then complain.
7540 -- This complaint is suppressed if we have detected other errors
7541 -- (since a common reason for missing the body is that it had errors).
7543 elsif Unit_Requires_Body (Gen_Unit) then
7544 if Serious_Errors_Detected = 0 then
7546 ("cannot find body of generic package &", Inst_Node, Gen_Unit);
7548 -- Don't attempt to perform any cleanup actions if some other
7549 -- error was aready detected, since this can cause blowups.
7555 -- Case of package that does not need a body
7558 -- If the instantiation of the declaration is a library unit,
7559 -- rewrite the original package instantiation as a package
7560 -- declaration in the compilation unit node.
7562 if Nkind (Parent (Inst_Node)) = N_Compilation_Unit then
7563 Set_Parent_Spec (Act_Decl, Parent_Spec (Inst_Node));
7564 Rewrite (Inst_Node, Act_Decl);
7566 -- Generate elaboration entity, in case spec has elaboration
7567 -- code. This cannot be done when the instance is analyzed,
7568 -- because it is not known yet whether the body exists.
7570 Set_Elaboration_Entity_Required (Act_Decl_Id, False);
7571 Build_Elaboration_Entity (Parent (Inst_Node), Act_Decl_Id);
7573 -- If the instantiation is not a library unit, then append the
7574 -- declaration to the list of implicitly generated entities.
7575 -- unless it is already a list member which means that it was
7576 -- already processed
7578 elsif not Is_List_Member (Act_Decl) then
7579 Mark_Rewrite_Insertion (Act_Decl);
7580 Insert_Before (Inst_Node, Act_Decl);
7584 Expander_Mode_Restore;
7585 end Instantiate_Package_Body;
7587 ---------------------------------
7588 -- Instantiate_Subprogram_Body --
7589 ---------------------------------
7591 procedure Instantiate_Subprogram_Body
7592 (Body_Info : Pending_Body_Info)
7594 Act_Decl : constant Node_Id := Body_Info.Act_Decl;
7595 Inst_Node : constant Node_Id := Body_Info.Inst_Node;
7596 Loc : constant Source_Ptr := Sloc (Inst_Node);
7597 Gen_Id : constant Node_Id := Name (Inst_Node);
7598 Gen_Unit : constant Entity_Id := Get_Generic_Entity (Inst_Node);
7599 Gen_Decl : constant Node_Id := Unit_Declaration_Node (Gen_Unit);
7600 Anon_Id : constant Entity_Id :=
7601 Defining_Unit_Name (Specification (Act_Decl));
7602 Pack_Id : constant Entity_Id :=
7603 Defining_Unit_Name (Parent (Act_Decl));
7606 Gen_Body_Id : Node_Id;
7608 Act_Body_Id : Entity_Id;
7609 Pack_Body : Node_Id;
7610 Prev_Formal : Entity_Id;
7612 Unit_Renaming : Node_Id;
7614 Parent_Installed : Boolean := False;
7615 Save_Style_Check : constant Boolean := Style_Check;
7618 Gen_Body_Id := Corresponding_Body (Gen_Decl);
7620 Expander_Mode_Save_And_Set (Body_Info.Expander_Status);
7622 if No (Gen_Body_Id) then
7623 Load_Parent_Of_Generic (Inst_Node, Specification (Gen_Decl));
7624 Gen_Body_Id := Corresponding_Body (Gen_Decl);
7627 Instantiation_Node := Inst_Node;
7629 if Present (Gen_Body_Id) then
7630 Gen_Body := Unit_Declaration_Node (Gen_Body_Id);
7632 if Nkind (Gen_Body) = N_Subprogram_Body_Stub then
7634 -- Either body is not present, or context is non-expanding, as
7635 -- when compiling a subunit. Mark the instance as completed, and
7636 -- diagnose a missing body when needed.
7639 and then Operating_Mode = Generate_Code
7642 ("missing proper body for instantiation", Gen_Body);
7645 Set_Has_Completion (Anon_Id);
7649 Save_Env (Gen_Unit, Anon_Id);
7650 Style_Check := False;
7651 Current_Sem_Unit := Body_Info.Current_Sem_Unit;
7652 Create_Instantiation_Source
7660 (Original_Node (Gen_Body), Empty, Instantiating => True);
7661 Act_Body_Id := Defining_Entity (Act_Body);
7662 Set_Chars (Act_Body_Id, Chars (Anon_Id));
7663 Set_Sloc (Act_Body_Id, Sloc (Defining_Entity (Inst_Node)));
7664 Set_Corresponding_Spec (Act_Body, Anon_Id);
7665 Set_Has_Completion (Anon_Id);
7666 Check_Generic_Actuals (Pack_Id, False);
7668 -- If it is a child unit, make the parent instance (which is an
7669 -- instance of the parent of the generic) visible. The parent
7670 -- instance is the prefix of the name of the generic unit.
7672 if Ekind (Scope (Gen_Unit)) = E_Generic_Package
7673 and then Nkind (Gen_Id) = N_Expanded_Name
7675 Install_Parent (Entity (Prefix (Gen_Id)), In_Body => True);
7676 Parent_Installed := True;
7678 elsif Is_Child_Unit (Gen_Unit) then
7679 Install_Parent (Scope (Gen_Unit), In_Body => True);
7680 Parent_Installed := True;
7683 -- Inside its body, a reference to the generic unit is a reference
7684 -- to the instance. The corresponding renaming is the first
7685 -- declaration in the body.
7688 Make_Subprogram_Renaming_Declaration (Loc,
7691 Specification (Original_Node (Gen_Body)),
7693 Instantiating => True),
7694 Name => New_Occurrence_Of (Anon_Id, Loc));
7696 -- If there is a formal subprogram with the same name as the
7697 -- unit itself, do not add this renaming declaration. This is
7698 -- a temporary fix for one ACVC test. ???
7700 Prev_Formal := First_Entity (Pack_Id);
7701 while Present (Prev_Formal) loop
7702 if Chars (Prev_Formal) = Chars (Gen_Unit)
7703 and then Is_Overloadable (Prev_Formal)
7708 Next_Entity (Prev_Formal);
7711 if Present (Prev_Formal) then
7712 Decls := New_List (Act_Body);
7714 Decls := New_List (Unit_Renaming, Act_Body);
7717 -- The subprogram body is placed in the body of a dummy package
7718 -- body, whose spec contains the subprogram declaration as well
7719 -- as the renaming declarations for the generic parameters.
7721 Pack_Body := Make_Package_Body (Loc,
7722 Defining_Unit_Name => New_Copy (Pack_Id),
7723 Declarations => Decls);
7725 Set_Corresponding_Spec (Pack_Body, Pack_Id);
7727 -- If the instantiation is a library unit, then build resulting
7728 -- compilation unit nodes for the instance. The declaration of
7729 -- the enclosing package is the grandparent of the subprogram
7730 -- declaration. First replace the instantiation node as the unit
7731 -- of the corresponding compilation.
7733 if Nkind (Parent (Inst_Node)) = N_Compilation_Unit then
7734 if Parent (Inst_Node) = Cunit (Main_Unit) then
7735 Set_Unit (Parent (Inst_Node), Inst_Node);
7736 Build_Instance_Compilation_Unit_Nodes
7737 (Inst_Node, Pack_Body, Parent (Parent (Act_Decl)));
7738 Analyze (Inst_Node);
7740 Set_Parent (Pack_Body, Parent (Inst_Node));
7741 Analyze (Pack_Body);
7745 Insert_Before (Inst_Node, Pack_Body);
7746 Mark_Rewrite_Insertion (Pack_Body);
7747 Analyze (Pack_Body);
7749 if Expander_Active then
7750 Freeze_Subprogram_Body (Inst_Node, Gen_Body, Pack_Id);
7754 if not Generic_Separately_Compiled (Gen_Unit) then
7755 Inherit_Context (Gen_Body, Inst_Node);
7758 Restore_Private_Views (Pack_Id, False);
7760 if Parent_Installed then
7761 Remove_Parent (In_Body => True);
7765 Style_Check := Save_Style_Check;
7767 -- Body not found. Error was emitted already. If there were no
7768 -- previous errors, this may be an instance whose scope is a premature
7769 -- instance. In that case we must insure that the (legal) program does
7770 -- raise program error if executed. We generate a subprogram body for
7771 -- this purpose. See DEC ac30vso.
7773 elsif Serious_Errors_Detected = 0
7774 and then Nkind (Parent (Inst_Node)) /= N_Compilation_Unit
7776 if Ekind (Anon_Id) = E_Procedure then
7778 Make_Subprogram_Body (Loc,
7780 Make_Procedure_Specification (Loc,
7781 Defining_Unit_Name => New_Copy (Anon_Id),
7782 Parameter_Specifications =>
7784 (Parameter_Specifications (Parent (Anon_Id)))),
7786 Declarations => Empty_List,
7787 Handled_Statement_Sequence =>
7788 Make_Handled_Sequence_Of_Statements (Loc,
7791 Make_Raise_Program_Error (Loc,
7793 PE_Access_Before_Elaboration))));
7797 Make_Raise_Program_Error (Loc,
7798 Reason => PE_Access_Before_Elaboration);
7800 Set_Etype (Ret_Expr, (Etype (Anon_Id)));
7801 Set_Analyzed (Ret_Expr);
7804 Make_Subprogram_Body (Loc,
7806 Make_Function_Specification (Loc,
7807 Defining_Unit_Name => New_Copy (Anon_Id),
7808 Parameter_Specifications =>
7810 (Parameter_Specifications (Parent (Anon_Id))),
7812 New_Occurrence_Of (Etype (Anon_Id), Loc)),
7814 Declarations => Empty_List,
7815 Handled_Statement_Sequence =>
7816 Make_Handled_Sequence_Of_Statements (Loc,
7818 New_List (Make_Return_Statement (Loc, Ret_Expr))));
7821 Pack_Body := Make_Package_Body (Loc,
7822 Defining_Unit_Name => New_Copy (Pack_Id),
7823 Declarations => New_List (Act_Body));
7825 Insert_After (Inst_Node, Pack_Body);
7826 Set_Corresponding_Spec (Pack_Body, Pack_Id);
7827 Analyze (Pack_Body);
7830 Expander_Mode_Restore;
7831 end Instantiate_Subprogram_Body;
7833 ----------------------
7834 -- Instantiate_Type --
7835 ----------------------
7837 function Instantiate_Type
7840 Analyzed_Formal : Node_Id;
7841 Actual_Decls : List_Id) return Node_Id
7843 Loc : constant Source_Ptr := Sloc (Actual);
7844 Gen_T : constant Entity_Id := Defining_Identifier (Formal);
7845 A_Gen_T : constant Entity_Id := Defining_Identifier (Analyzed_Formal);
7846 Ancestor : Entity_Id := Empty;
7847 Def : constant Node_Id := Formal_Type_Definition (Formal);
7849 Decl_Node : Node_Id;
7851 procedure Validate_Array_Type_Instance;
7852 procedure Validate_Access_Subprogram_Instance;
7853 procedure Validate_Access_Type_Instance;
7854 procedure Validate_Derived_Type_Instance;
7855 procedure Validate_Private_Type_Instance;
7856 -- These procedures perform validation tests for the named case
7858 function Subtypes_Match (Gen_T, Act_T : Entity_Id) return Boolean;
7859 -- Check that base types are the same and that the subtypes match
7860 -- statically. Used in several of the above.
7862 --------------------
7863 -- Subtypes_Match --
7864 --------------------
7866 function Subtypes_Match (Gen_T, Act_T : Entity_Id) return Boolean is
7867 T : constant Entity_Id := Get_Instance_Of (Gen_T);
7870 return (Base_Type (T) = Base_Type (Act_T)
7871 -- why is the and then commented out here???
7872 -- and then Is_Constrained (T) = Is_Constrained (Act_T)
7873 and then Subtypes_Statically_Match (T, Act_T))
7875 or else (Is_Class_Wide_Type (Gen_T)
7876 and then Is_Class_Wide_Type (Act_T)
7879 Get_Instance_Of (Root_Type (Gen_T)),
7880 Root_Type (Act_T)));
7883 -----------------------------------------
7884 -- Validate_Access_Subprogram_Instance --
7885 -----------------------------------------
7887 procedure Validate_Access_Subprogram_Instance is
7889 if not Is_Access_Type (Act_T)
7890 or else Ekind (Designated_Type (Act_T)) /= E_Subprogram_Type
7893 ("expect access type in instantiation of &", Actual, Gen_T);
7894 Abandon_Instantiation (Actual);
7897 Check_Mode_Conformant
7898 (Designated_Type (Act_T),
7899 Designated_Type (A_Gen_T),
7903 if Ekind (Base_Type (Act_T)) = E_Access_Protected_Subprogram_Type then
7904 if Ekind (A_Gen_T) = E_Access_Subprogram_Type then
7906 ("protected access type not allowed for formal &",
7910 elsif Ekind (A_Gen_T) = E_Access_Protected_Subprogram_Type then
7912 ("expect protected access type for formal &",
7915 end Validate_Access_Subprogram_Instance;
7917 -----------------------------------
7918 -- Validate_Access_Type_Instance --
7919 -----------------------------------
7921 procedure Validate_Access_Type_Instance is
7922 Desig_Type : constant Entity_Id :=
7924 (Designated_Type (A_Gen_T), Scope (A_Gen_T));
7927 if not Is_Access_Type (Act_T) then
7929 ("expect access type in instantiation of &", Actual, Gen_T);
7930 Abandon_Instantiation (Actual);
7933 if Is_Access_Constant (A_Gen_T) then
7934 if not Is_Access_Constant (Act_T) then
7936 ("actual type must be access-to-constant type", Actual);
7937 Abandon_Instantiation (Actual);
7940 if Is_Access_Constant (Act_T) then
7942 ("actual type must be access-to-variable type", Actual);
7943 Abandon_Instantiation (Actual);
7945 elsif Ekind (A_Gen_T) = E_General_Access_Type
7946 and then Ekind (Base_Type (Act_T)) /= E_General_Access_Type
7948 Error_Msg_N ("actual must be general access type!", Actual);
7949 Error_Msg_NE ("add ALL to }!", Actual, Act_T);
7950 Abandon_Instantiation (Actual);
7954 -- The designated subtypes, that is to say the subtypes introduced
7955 -- by an access type declaration (and not by a subtype declaration)
7958 if not Subtypes_Match
7959 (Desig_Type, Designated_Type (Base_Type (Act_T)))
7962 ("designated type of actual does not match that of formal &",
7964 Abandon_Instantiation (Actual);
7966 elsif Is_Access_Type (Designated_Type (Act_T))
7967 and then Is_Constrained (Designated_Type (Designated_Type (Act_T)))
7969 Is_Constrained (Designated_Type (Desig_Type))
7972 ("designated type of actual does not match that of formal &",
7974 Abandon_Instantiation (Actual);
7976 end Validate_Access_Type_Instance;
7978 ----------------------------------
7979 -- Validate_Array_Type_Instance --
7980 ----------------------------------
7982 procedure Validate_Array_Type_Instance is
7987 function Formal_Dimensions return Int;
7988 -- Count number of dimensions in array type formal
7990 -----------------------
7991 -- Formal_Dimensions --
7992 -----------------------
7994 function Formal_Dimensions return Int is
7999 if Nkind (Def) = N_Constrained_Array_Definition then
8000 Index := First (Discrete_Subtype_Definitions (Def));
8002 Index := First (Subtype_Marks (Def));
8005 while Present (Index) loop
8011 end Formal_Dimensions;
8013 -- Start of processing for Validate_Array_Type_Instance
8016 if not Is_Array_Type (Act_T) then
8018 ("expect array type in instantiation of &", Actual, Gen_T);
8019 Abandon_Instantiation (Actual);
8021 elsif Nkind (Def) = N_Constrained_Array_Definition then
8022 if not (Is_Constrained (Act_T)) then
8024 ("expect constrained array in instantiation of &",
8026 Abandon_Instantiation (Actual);
8030 if Is_Constrained (Act_T) then
8032 ("expect unconstrained array in instantiation of &",
8034 Abandon_Instantiation (Actual);
8038 if Formal_Dimensions /= Number_Dimensions (Act_T) then
8040 ("dimensions of actual do not match formal &", Actual, Gen_T);
8041 Abandon_Instantiation (Actual);
8044 I1 := First_Index (A_Gen_T);
8045 I2 := First_Index (Act_T);
8046 for J in 1 .. Formal_Dimensions loop
8048 -- If the indices of the actual were given by a subtype_mark,
8049 -- the index was transformed into a range attribute. Retrieve
8050 -- the original type mark for checking.
8052 if Is_Entity_Name (Original_Node (I2)) then
8053 T2 := Entity (Original_Node (I2));
8058 if not Subtypes_Match
8059 (Find_Actual_Type (Etype (I1), Scope (A_Gen_T)), T2)
8062 ("index types of actual do not match those of formal &",
8064 Abandon_Instantiation (Actual);
8071 if not Subtypes_Match (
8072 Find_Actual_Type (Component_Type (A_Gen_T), Scope (A_Gen_T)),
8073 Component_Type (Act_T))
8076 ("component subtype of actual does not match that of formal &",
8078 Abandon_Instantiation (Actual);
8081 if Has_Aliased_Components (A_Gen_T)
8082 and then not Has_Aliased_Components (Act_T)
8085 ("actual must have aliased components to match formal type &",
8089 end Validate_Array_Type_Instance;
8091 ------------------------------------
8092 -- Validate_Derived_Type_Instance --
8093 ------------------------------------
8095 procedure Validate_Derived_Type_Instance is
8096 Actual_Discr : Entity_Id;
8097 Ancestor_Discr : Entity_Id;
8100 -- If the parent type in the generic declaration is itself
8101 -- a previous formal type, then it is local to the generic
8102 -- and absent from the analyzed generic definition. In that
8103 -- case the ancestor is the instance of the formal (which must
8104 -- have been instantiated previously), unless the ancestor is
8105 -- itself a formal derived type. In this latter case (which is the
8106 -- subject of Corrigendum 8652/0038 (AI-202) the ancestor of the
8107 -- formals is the ancestor of its parent. Otherwise, the analyzed
8108 -- generic carries the parent type. If the parent type is defined
8109 -- in a previous formal package, then the scope of that formal
8110 -- package is that of the generic type itself, and it has already
8111 -- been mapped into the corresponding type in the actual package.
8113 -- Common case: parent type defined outside of the generic
8115 if Is_Entity_Name (Subtype_Mark (Def))
8116 and then Present (Entity (Subtype_Mark (Def)))
8118 Ancestor := Get_Instance_Of (Entity (Subtype_Mark (Def)));
8120 -- Check whether parent is defined in a previous formal package
8123 Scope (Scope (Base_Type (Etype (A_Gen_T)))) = Scope (A_Gen_T)
8126 Get_Instance_Of (Base_Type (Etype (A_Gen_T)));
8128 -- The type may be a local derivation, or a type extension of
8129 -- a previous formal, or of a formal of a parent package.
8131 elsif Is_Derived_Type (Get_Instance_Of (A_Gen_T))
8133 Ekind (Get_Instance_Of (A_Gen_T)) = E_Record_Type_With_Private
8135 -- Check whether the parent is another derived formal type
8136 -- in the same generic unit.
8138 if Etype (A_Gen_T) /= A_Gen_T
8139 and then Is_Generic_Type (Etype (A_Gen_T))
8140 and then Scope (Etype (A_Gen_T)) = Scope (A_Gen_T)
8141 and then Etype (Etype (A_Gen_T)) /= Etype (A_Gen_T)
8143 -- Locate ancestor of parent from the subtype declaration
8144 -- created for the actual.
8150 Decl := First (Actual_Decls);
8151 while Present (Decl) loop
8152 if Nkind (Decl) = N_Subtype_Declaration
8153 and then Chars (Defining_Identifier (Decl)) =
8154 Chars (Etype (A_Gen_T))
8156 Ancestor := Generic_Parent_Type (Decl);
8164 pragma Assert (Present (Ancestor));
8168 Get_Instance_Of (Base_Type (Get_Instance_Of (A_Gen_T)));
8172 Ancestor := Get_Instance_Of (Etype (Base_Type (A_Gen_T)));
8175 if not Is_Ancestor (Base_Type (Ancestor), Act_T) then
8177 ("expect type derived from & in instantiation",
8178 Actual, First_Subtype (Ancestor));
8179 Abandon_Instantiation (Actual);
8182 -- Perform atomic/volatile checks (RM C.6(12))
8184 if Is_Atomic (Act_T) and then not Is_Atomic (Ancestor) then
8186 ("cannot have atomic actual type for non-atomic formal type",
8189 elsif Is_Volatile (Act_T)
8190 and then not Is_Volatile (Ancestor)
8191 and then Is_By_Reference_Type (Ancestor)
8194 ("cannot have volatile actual type for non-volatile formal type",
8198 -- It should not be necessary to check for unknown discriminants
8199 -- on Formal, but for some reason Has_Unknown_Discriminants is
8200 -- false for A_Gen_T, so Is_Indefinite_Subtype incorrectly
8201 -- returns False. This needs fixing. ???
8203 if not Is_Indefinite_Subtype (A_Gen_T)
8204 and then not Unknown_Discriminants_Present (Formal)
8205 and then Is_Indefinite_Subtype (Act_T)
8208 ("actual subtype must be constrained", Actual);
8209 Abandon_Instantiation (Actual);
8212 if not Unknown_Discriminants_Present (Formal) then
8213 if Is_Constrained (Ancestor) then
8214 if not Is_Constrained (Act_T) then
8216 ("actual subtype must be constrained", Actual);
8217 Abandon_Instantiation (Actual);
8220 -- Ancestor is unconstrained
8222 elsif Is_Constrained (Act_T) then
8223 if Ekind (Ancestor) = E_Access_Type
8224 or else Is_Composite_Type (Ancestor)
8227 ("actual subtype must be unconstrained", Actual);
8228 Abandon_Instantiation (Actual);
8231 -- A class-wide type is only allowed if the formal has
8232 -- unknown discriminants.
8234 elsif Is_Class_Wide_Type (Act_T)
8235 and then not Has_Unknown_Discriminants (Ancestor)
8238 ("actual for & cannot be a class-wide type", Actual, Gen_T);
8239 Abandon_Instantiation (Actual);
8241 -- Otherwise, the formal and actual shall have the same
8242 -- number of discriminants and each discriminant of the
8243 -- actual must correspond to a discriminant of the formal.
8245 elsif Has_Discriminants (Act_T)
8246 and then not Has_Unknown_Discriminants (Act_T)
8247 and then Has_Discriminants (Ancestor)
8249 Actual_Discr := First_Discriminant (Act_T);
8250 Ancestor_Discr := First_Discriminant (Ancestor);
8251 while Present (Actual_Discr)
8252 and then Present (Ancestor_Discr)
8254 if Base_Type (Act_T) /= Base_Type (Ancestor) and then
8255 not Present (Corresponding_Discriminant (Actual_Discr))
8258 ("discriminant & does not correspond " &
8259 "to ancestor discriminant", Actual, Actual_Discr);
8260 Abandon_Instantiation (Actual);
8263 Next_Discriminant (Actual_Discr);
8264 Next_Discriminant (Ancestor_Discr);
8267 if Present (Actual_Discr) or else Present (Ancestor_Discr) then
8269 ("actual for & must have same number of discriminants",
8271 Abandon_Instantiation (Actual);
8274 -- This case should be caught by the earlier check for
8275 -- for constrainedness, but the check here is added for
8278 elsif Has_Discriminants (Act_T)
8279 and then not Has_Unknown_Discriminants (Act_T)
8282 ("actual for & must not have discriminants", Actual, Gen_T);
8283 Abandon_Instantiation (Actual);
8285 elsif Has_Discriminants (Ancestor) then
8287 ("actual for & must have known discriminants", Actual, Gen_T);
8288 Abandon_Instantiation (Actual);
8291 if not Subtypes_Statically_Compatible (Act_T, Ancestor) then
8293 ("constraint on actual is incompatible with formal", Actual);
8294 Abandon_Instantiation (Actual);
8297 end Validate_Derived_Type_Instance;
8299 ------------------------------------
8300 -- Validate_Private_Type_Instance --
8301 ------------------------------------
8303 procedure Validate_Private_Type_Instance is
8304 Formal_Discr : Entity_Id;
8305 Actual_Discr : Entity_Id;
8306 Formal_Subt : Entity_Id;
8309 if Is_Limited_Type (Act_T)
8310 and then not Is_Limited_Type (A_Gen_T)
8313 ("actual for non-limited & cannot be a limited type", Actual,
8315 Explain_Limited_Type (Act_T, Actual);
8316 Abandon_Instantiation (Actual);
8318 elsif Is_Indefinite_Subtype (Act_T)
8319 and then not Is_Indefinite_Subtype (A_Gen_T)
8320 and then Ada_Version >= Ada_95
8323 ("actual for & must be a definite subtype", Actual, Gen_T);
8325 elsif not Is_Tagged_Type (Act_T)
8326 and then Is_Tagged_Type (A_Gen_T)
8329 ("actual for & must be a tagged type", Actual, Gen_T);
8331 elsif Has_Discriminants (A_Gen_T) then
8332 if not Has_Discriminants (Act_T) then
8334 ("actual for & must have discriminants", Actual, Gen_T);
8335 Abandon_Instantiation (Actual);
8337 elsif Is_Constrained (Act_T) then
8339 ("actual for & must be unconstrained", Actual, Gen_T);
8340 Abandon_Instantiation (Actual);
8343 Formal_Discr := First_Discriminant (A_Gen_T);
8344 Actual_Discr := First_Discriminant (Act_T);
8345 while Formal_Discr /= Empty loop
8346 if Actual_Discr = Empty then
8348 ("discriminants on actual do not match formal",
8350 Abandon_Instantiation (Actual);
8353 Formal_Subt := Get_Instance_Of (Etype (Formal_Discr));
8355 -- Access discriminants match if designated types do
8357 if Ekind (Base_Type (Formal_Subt)) = E_Anonymous_Access_Type
8358 and then (Ekind (Base_Type (Etype (Actual_Discr)))) =
8359 E_Anonymous_Access_Type
8362 (Designated_Type (Base_Type (Formal_Subt))) =
8363 Designated_Type (Base_Type (Etype (Actual_Discr)))
8367 elsif Base_Type (Formal_Subt) /=
8368 Base_Type (Etype (Actual_Discr))
8371 ("types of actual discriminants must match formal",
8373 Abandon_Instantiation (Actual);
8375 elsif not Subtypes_Statically_Match
8376 (Formal_Subt, Etype (Actual_Discr))
8377 and then Ada_Version >= Ada_95
8380 ("subtypes of actual discriminants must match formal",
8382 Abandon_Instantiation (Actual);
8385 Next_Discriminant (Formal_Discr);
8386 Next_Discriminant (Actual_Discr);
8389 if Actual_Discr /= Empty then
8391 ("discriminants on actual do not match formal",
8393 Abandon_Instantiation (Actual);
8400 end Validate_Private_Type_Instance;
8402 -- Start of processing for Instantiate_Type
8405 if Get_Instance_Of (A_Gen_T) /= A_Gen_T then
8406 Error_Msg_N ("duplicate instantiation of generic type", Actual);
8409 elsif not Is_Entity_Name (Actual)
8410 or else not Is_Type (Entity (Actual))
8413 ("expect valid subtype mark to instantiate &", Actual, Gen_T);
8414 Abandon_Instantiation (Actual);
8417 Act_T := Entity (Actual);
8419 -- Ada 2005 (AI-216): An Unchecked_Union subtype shall only be passed
8420 -- as a generic actual parameter if the corresponding formal type
8421 -- does not have a known_discriminant_part, or is a formal derived
8422 -- type that is an Unchecked_Union type.
8424 if Is_Unchecked_Union (Base_Type (Act_T)) then
8425 if not Has_Discriminants (A_Gen_T)
8427 (Is_Derived_Type (A_Gen_T)
8429 Is_Unchecked_Union (A_Gen_T))
8433 Error_Msg_N ("Unchecked_Union cannot be the actual for a" &
8434 " discriminated formal type", Act_T);
8439 -- Deal with fixed/floating restrictions
8441 if Is_Floating_Point_Type (Act_T) then
8442 Check_Restriction (No_Floating_Point, Actual);
8443 elsif Is_Fixed_Point_Type (Act_T) then
8444 Check_Restriction (No_Fixed_Point, Actual);
8447 -- Deal with error of using incomplete type as generic actual
8449 if Ekind (Act_T) = E_Incomplete_Type then
8450 if No (Underlying_Type (Act_T)) then
8451 Error_Msg_N ("premature use of incomplete type", Actual);
8452 Abandon_Instantiation (Actual);
8454 Act_T := Full_View (Act_T);
8455 Set_Entity (Actual, Act_T);
8457 if Has_Private_Component (Act_T) then
8459 ("premature use of type with private component", Actual);
8463 -- Deal with error of premature use of private type as generic actual
8465 elsif Is_Private_Type (Act_T)
8466 and then Is_Private_Type (Base_Type (Act_T))
8467 and then not Is_Generic_Type (Act_T)
8468 and then not Is_Derived_Type (Act_T)
8469 and then No (Full_View (Root_Type (Act_T)))
8471 Error_Msg_N ("premature use of private type", Actual);
8473 elsif Has_Private_Component (Act_T) then
8475 ("premature use of type with private component", Actual);
8478 Set_Instance_Of (A_Gen_T, Act_T);
8480 -- If the type is generic, the class-wide type may also be used
8482 if Is_Tagged_Type (A_Gen_T)
8483 and then Is_Tagged_Type (Act_T)
8484 and then not Is_Class_Wide_Type (A_Gen_T)
8486 Set_Instance_Of (Class_Wide_Type (A_Gen_T),
8487 Class_Wide_Type (Act_T));
8490 if not Is_Abstract (A_Gen_T)
8491 and then Is_Abstract (Act_T)
8494 ("actual of non-abstract formal cannot be abstract", Actual);
8497 if Is_Scalar_Type (Gen_T) then
8498 Set_Instance_Of (Etype (A_Gen_T), Etype (Act_T));
8503 when N_Formal_Private_Type_Definition =>
8504 Validate_Private_Type_Instance;
8506 when N_Formal_Derived_Type_Definition =>
8507 Validate_Derived_Type_Instance;
8509 when N_Formal_Discrete_Type_Definition =>
8510 if not Is_Discrete_Type (Act_T) then
8512 ("expect discrete type in instantiation of&", Actual, Gen_T);
8513 Abandon_Instantiation (Actual);
8516 when N_Formal_Signed_Integer_Type_Definition =>
8517 if not Is_Signed_Integer_Type (Act_T) then
8519 ("expect signed integer type in instantiation of&",
8521 Abandon_Instantiation (Actual);
8524 when N_Formal_Modular_Type_Definition =>
8525 if not Is_Modular_Integer_Type (Act_T) then
8527 ("expect modular type in instantiation of &", Actual, Gen_T);
8528 Abandon_Instantiation (Actual);
8531 when N_Formal_Floating_Point_Definition =>
8532 if not Is_Floating_Point_Type (Act_T) then
8534 ("expect float type in instantiation of &", Actual, Gen_T);
8535 Abandon_Instantiation (Actual);
8538 when N_Formal_Ordinary_Fixed_Point_Definition =>
8539 if not Is_Ordinary_Fixed_Point_Type (Act_T) then
8541 ("expect ordinary fixed point type in instantiation of &",
8543 Abandon_Instantiation (Actual);
8546 when N_Formal_Decimal_Fixed_Point_Definition =>
8547 if not Is_Decimal_Fixed_Point_Type (Act_T) then
8549 ("expect decimal type in instantiation of &",
8551 Abandon_Instantiation (Actual);
8554 when N_Array_Type_Definition =>
8555 Validate_Array_Type_Instance;
8557 when N_Access_To_Object_Definition =>
8558 Validate_Access_Type_Instance;
8560 when N_Access_Function_Definition |
8561 N_Access_Procedure_Definition =>
8562 Validate_Access_Subprogram_Instance;
8565 raise Program_Error;
8570 Make_Subtype_Declaration (Loc,
8571 Defining_Identifier => New_Copy (Gen_T),
8572 Subtype_Indication => New_Reference_To (Act_T, Loc));
8574 if Is_Private_Type (Act_T) then
8575 Set_Has_Private_View (Subtype_Indication (Decl_Node));
8577 elsif Is_Access_Type (Act_T)
8578 and then Is_Private_Type (Designated_Type (Act_T))
8580 Set_Has_Private_View (Subtype_Indication (Decl_Node));
8583 -- Flag actual derived types so their elaboration produces the
8584 -- appropriate renamings for the primitive operations of the ancestor.
8585 -- Flag actual for formal private types as well, to determine whether
8586 -- operations in the private part may override inherited operations.
8588 if Nkind (Def) = N_Formal_Derived_Type_Definition
8589 or else Nkind (Def) = N_Formal_Private_Type_Definition
8591 Set_Generic_Parent_Type (Decl_Node, Ancestor);
8595 end Instantiate_Type;
8597 ---------------------
8598 -- Is_In_Main_Unit --
8599 ---------------------
8601 function Is_In_Main_Unit (N : Node_Id) return Boolean is
8602 Unum : constant Unit_Number_Type := Get_Source_Unit (N);
8603 Current_Unit : Node_Id;
8606 if Unum = Main_Unit then
8609 -- If the current unit is a subunit then it is either the main unit
8610 -- or is being compiled as part of the main unit.
8612 elsif Nkind (N) = N_Compilation_Unit then
8613 return Nkind (Unit (N)) = N_Subunit;
8616 Current_Unit := Parent (N);
8617 while Present (Current_Unit)
8618 and then Nkind (Current_Unit) /= N_Compilation_Unit
8620 Current_Unit := Parent (Current_Unit);
8623 -- The instantiation node is in the main unit, or else the current
8624 -- node (perhaps as the result of nested instantiations) is in the
8625 -- main unit, or in the declaration of the main unit, which in this
8626 -- last case must be a body.
8628 return Unum = Main_Unit
8629 or else Current_Unit = Cunit (Main_Unit)
8630 or else Current_Unit = Library_Unit (Cunit (Main_Unit))
8631 or else (Present (Library_Unit (Current_Unit))
8632 and then Is_In_Main_Unit (Library_Unit (Current_Unit)));
8633 end Is_In_Main_Unit;
8635 ----------------------------
8636 -- Load_Parent_Of_Generic --
8637 ----------------------------
8639 procedure Load_Parent_Of_Generic (N : Node_Id; Spec : Node_Id) is
8640 Comp_Unit : constant Node_Id := Cunit (Get_Source_Unit (Spec));
8641 Save_Style_Check : constant Boolean := Style_Check;
8642 True_Parent : Node_Id;
8643 Inst_Node : Node_Id;
8647 if not In_Same_Source_Unit (N, Spec)
8648 or else Nkind (Unit (Comp_Unit)) = N_Package_Declaration
8649 or else (Nkind (Unit (Comp_Unit)) = N_Package_Body
8650 and then not Is_In_Main_Unit (Spec))
8652 -- Find body of parent of spec, and analyze it. A special case
8653 -- arises when the parent is an instantiation, that is to say when
8654 -- we are currently instantiating a nested generic. In that case,
8655 -- there is no separate file for the body of the enclosing instance.
8656 -- Instead, the enclosing body must be instantiated as if it were
8657 -- a pending instantiation, in order to produce the body for the
8658 -- nested generic we require now. Note that in that case the
8659 -- generic may be defined in a package body, the instance defined
8660 -- in the same package body, and the original enclosing body may not
8661 -- be in the main unit.
8663 True_Parent := Parent (Spec);
8666 while Present (True_Parent)
8667 and then Nkind (True_Parent) /= N_Compilation_Unit
8669 if Nkind (True_Parent) = N_Package_Declaration
8671 Nkind (Original_Node (True_Parent)) = N_Package_Instantiation
8673 -- Parent is a compilation unit that is an instantiation.
8674 -- Instantiation node has been replaced with package decl.
8676 Inst_Node := Original_Node (True_Parent);
8679 elsif Nkind (True_Parent) = N_Package_Declaration
8680 and then Present (Generic_Parent (Specification (True_Parent)))
8681 and then Nkind (Parent (True_Parent)) /= N_Compilation_Unit
8683 -- Parent is an instantiation within another specification.
8684 -- Declaration for instance has been inserted before original
8685 -- instantiation node. A direct link would be preferable?
8687 Inst_Node := Next (True_Parent);
8689 while Present (Inst_Node)
8690 and then Nkind (Inst_Node) /= N_Package_Instantiation
8695 -- If the instance appears within a generic, and the generic
8696 -- unit is defined within a formal package of the enclosing
8697 -- generic, there is no generic body available, and none
8698 -- needed. A more precise test should be used ???
8700 if No (Inst_Node) then
8706 True_Parent := Parent (True_Parent);
8710 -- Case where we are currently instantiating a nested generic
8712 if Present (Inst_Node) then
8713 if Nkind (Parent (True_Parent)) = N_Compilation_Unit then
8715 -- Instantiation node and declaration of instantiated package
8716 -- were exchanged when only the declaration was needed.
8717 -- Restore instantiation node before proceeding with body.
8719 Set_Unit (Parent (True_Parent), Inst_Node);
8722 -- Now complete instantiation of enclosing body, if it appears
8723 -- in some other unit. If it appears in the current unit, the
8724 -- body will have been instantiated already.
8726 if No (Corresponding_Body (Instance_Spec (Inst_Node))) then
8728 -- We need to determine the expander mode to instantiate
8729 -- the enclosing body. Because the generic body we need
8730 -- may use global entities declared in the enclosing package
8731 -- (including aggregates) it is in general necessary to
8732 -- compile this body with expansion enabled. The exception
8733 -- is if we are within a generic package, in which case
8734 -- the usual generic rule applies.
8737 Exp_Status : Boolean := True;
8741 -- Loop through scopes looking for generic package
8743 Scop := Scope (Defining_Entity (Instance_Spec (Inst_Node)));
8744 while Present (Scop)
8745 and then Scop /= Standard_Standard
8747 if Ekind (Scop) = E_Generic_Package then
8748 Exp_Status := False;
8752 Scop := Scope (Scop);
8755 Instantiate_Package_Body
8756 (Pending_Body_Info'(
8757 Inst_Node, True_Parent, Exp_Status,
8758 Get_Code_Unit (Sloc (Inst_Node))));
8762 -- Case where we are not instantiating a nested generic
8765 Opt.Style_Check := False;
8766 Expander_Mode_Save_And_Set (True);
8767 Load_Needed_Body (Comp_Unit, OK);
8768 Opt.Style_Check := Save_Style_Check;
8769 Expander_Mode_Restore;
8772 and then Unit_Requires_Body (Defining_Entity (Spec))
8775 Bname : constant Unit_Name_Type :=
8776 Get_Body_Name (Get_Unit_Name (Unit (Comp_Unit)));
8779 Error_Msg_Unit_1 := Bname;
8780 Error_Msg_N ("this instantiation requires$!", N);
8782 Get_File_Name (Bname, Subunit => False);
8783 Error_Msg_N ("\but file{ was not found!", N);
8784 raise Unrecoverable_Error;
8790 -- If loading the parent of the generic caused an instantiation
8791 -- circularity, we abandon compilation at this point, because
8792 -- otherwise in some cases we get into trouble with infinite
8793 -- recursions after this point.
8795 if Circularity_Detected then
8796 raise Unrecoverable_Error;
8798 end Load_Parent_Of_Generic;
8800 -----------------------
8801 -- Move_Freeze_Nodes --
8802 -----------------------
8804 procedure Move_Freeze_Nodes
8805 (Out_Of : Entity_Id;
8810 Next_Decl : Node_Id;
8811 Next_Node : Node_Id := After;
8814 function Is_Outer_Type (T : Entity_Id) return Boolean;
8815 -- Check whether entity is declared in a scope external to that
8816 -- of the generic unit.
8822 function Is_Outer_Type (T : Entity_Id) return Boolean is
8823 Scop : Entity_Id := Scope (T);
8826 if Scope_Depth (Scop) < Scope_Depth (Out_Of) then
8830 while Scop /= Standard_Standard loop
8832 if Scop = Out_Of then
8835 Scop := Scope (Scop);
8843 -- Start of processing for Move_Freeze_Nodes
8850 -- First remove the freeze nodes that may appear before all other
8854 while Present (Decl)
8855 and then Nkind (Decl) = N_Freeze_Entity
8856 and then Is_Outer_Type (Entity (Decl))
8858 Decl := Remove_Head (L);
8859 Insert_After (Next_Node, Decl);
8860 Set_Analyzed (Decl, False);
8865 -- Next scan the list of declarations and remove each freeze node that
8866 -- appears ahead of the current node.
8868 while Present (Decl) loop
8869 while Present (Next (Decl))
8870 and then Nkind (Next (Decl)) = N_Freeze_Entity
8871 and then Is_Outer_Type (Entity (Next (Decl)))
8873 Next_Decl := Remove_Next (Decl);
8874 Insert_After (Next_Node, Next_Decl);
8875 Set_Analyzed (Next_Decl, False);
8876 Next_Node := Next_Decl;
8879 -- If the declaration is a nested package or concurrent type, then
8880 -- recurse. Nested generic packages will have been processed from the
8883 if Nkind (Decl) = N_Package_Declaration then
8884 Spec := Specification (Decl);
8886 elsif Nkind (Decl) = N_Task_Type_Declaration then
8887 Spec := Task_Definition (Decl);
8889 elsif Nkind (Decl) = N_Protected_Type_Declaration then
8890 Spec := Protected_Definition (Decl);
8896 if Present (Spec) then
8897 Move_Freeze_Nodes (Out_Of, Next_Node,
8898 Visible_Declarations (Spec));
8899 Move_Freeze_Nodes (Out_Of, Next_Node,
8900 Private_Declarations (Spec));
8905 end Move_Freeze_Nodes;
8911 function Next_Assoc (E : Assoc_Ptr) return Assoc_Ptr is
8913 return Generic_Renamings.Table (E).Next_In_HTable;
8916 ------------------------
8917 -- Preanalyze_Actuals --
8918 ------------------------
8920 procedure Pre_Analyze_Actuals (N : Node_Id) is
8923 Errs : constant Int := Serious_Errors_Detected;
8926 Assoc := First (Generic_Associations (N));
8928 while Present (Assoc) loop
8929 Act := Explicit_Generic_Actual_Parameter (Assoc);
8931 -- Within a nested instantiation, a defaulted actual is an
8932 -- empty association, so nothing to analyze. If the actual for
8933 -- a subprogram is an attribute, analyze prefix only, because
8934 -- actual is not a complete attribute reference.
8936 -- If actual is an allocator, analyze expression only. The full
8937 -- analysis can generate code, and if the instance is a compilation
8938 -- unit we have to wait until the package instance is installed to
8939 -- have a proper place to insert this code.
8941 -- String literals may be operators, but at this point we do not
8942 -- know whether the actual is a formal subprogram or a string.
8947 elsif Nkind (Act) = N_Attribute_Reference then
8948 Analyze (Prefix (Act));
8950 elsif Nkind (Act) = N_Explicit_Dereference then
8951 Analyze (Prefix (Act));
8953 elsif Nkind (Act) = N_Allocator then
8955 Expr : constant Node_Id := Expression (Act);
8958 if Nkind (Expr) = N_Subtype_Indication then
8959 Analyze (Subtype_Mark (Expr));
8960 Analyze_List (Constraints (Constraint (Expr)));
8966 elsif Nkind (Act) /= N_Operator_Symbol then
8970 if Errs /= Serious_Errors_Detected then
8971 Abandon_Instantiation (Act);
8976 end Pre_Analyze_Actuals;
8982 procedure Remove_Parent (In_Body : Boolean := False) is
8983 S : Entity_Id := Current_Scope;
8989 -- After child instantiation is complete, remove from scope stack
8990 -- the extra copy of the current scope, and then remove parent
8996 while Current_Scope /= S loop
8998 End_Package_Scope (Current_Scope);
9000 if In_Open_Scopes (P) then
9001 E := First_Entity (P);
9003 while Present (E) loop
9004 Set_Is_Immediately_Visible (E, True);
9008 if Is_Generic_Instance (Current_Scope)
9009 and then P /= Current_Scope
9011 -- We are within an instance of some sibling. Retain
9012 -- visibility of parent, for proper subsequent cleanup,
9013 -- and reinstall private declarations as well.
9015 Set_In_Private_Part (P);
9016 Install_Private_Declarations (P);
9019 -- If the ultimate parent is a compilation unit, reset its
9020 -- visibility to what it was before instantiation.
9022 elsif not In_Open_Scopes (Scope (P))
9024 (not Is_Child_Unit (P) and then not Parent_Unit_Visible)
9026 Set_Is_Immediately_Visible (P, False);
9030 -- Reset visibility of entities in the enclosing scope
9032 Set_Is_Hidden_Open_Scope (Current_Scope, False);
9033 Hidden := First_Elmt (Hidden_Entities);
9035 while Present (Hidden) loop
9036 Set_Is_Immediately_Visible (Node (Hidden), True);
9041 -- Each body is analyzed separately, and there is no context
9042 -- that needs preserving from one body instance to the next,
9043 -- so remove all parent scopes that have been installed.
9045 while Present (S) loop
9046 End_Package_Scope (S);
9047 Set_Is_Immediately_Visible (S, False);
9049 exit when S = Standard_Standard;
9059 procedure Restore_Env is
9060 Saved : Instance_Env renames Instance_Envs.Table (Instance_Envs.Last);
9063 Ada_Version := Saved.Ada_Version;
9065 if No (Current_Instantiated_Parent.Act_Id) then
9067 -- Restore environment after subprogram inlining
9069 Restore_Private_Views (Empty);
9072 Current_Instantiated_Parent := Saved.Instantiated_Parent;
9073 Exchanged_Views := Saved.Exchanged_Views;
9074 Hidden_Entities := Saved.Hidden_Entities;
9075 Current_Sem_Unit := Saved.Current_Sem_Unit;
9076 Parent_Unit_Visible := Saved.Parent_Unit_Visible;
9078 Instance_Envs.Decrement_Last;
9081 ---------------------------
9082 -- Restore_Private_Views --
9083 ---------------------------
9085 procedure Restore_Private_Views
9086 (Pack_Id : Entity_Id;
9087 Is_Package : Boolean := True)
9095 procedure Restore_Nested_Formal (Formal : Entity_Id);
9096 -- Hide the generic formals of formal packages declared with box
9097 -- which were reachable in the current instantiation.
9099 procedure Restore_Nested_Formal (Formal : Entity_Id) is
9102 if Present (Renamed_Object (Formal))
9103 and then Denotes_Formal_Package (Renamed_Object (Formal), True)
9107 elsif Present (Associated_Formal_Package (Formal))
9108 and then Box_Present (Parent (Associated_Formal_Package (Formal)))
9110 Ent := First_Entity (Formal);
9112 while Present (Ent) loop
9113 exit when Ekind (Ent) = E_Package
9114 and then Renamed_Entity (Ent) = Renamed_Entity (Formal);
9116 Set_Is_Hidden (Ent);
9117 Set_Is_Potentially_Use_Visible (Ent, False);
9119 -- If package, then recurse
9121 if Ekind (Ent) = E_Package then
9122 Restore_Nested_Formal (Ent);
9128 end Restore_Nested_Formal;
9131 M := First_Elmt (Exchanged_Views);
9132 while Present (M) loop
9135 -- Subtypes of types whose views have been exchanged, and that
9136 -- are defined within the instance, were not on the list of
9137 -- Private_Dependents on entry to the instance, so they have to
9138 -- be exchanged explicitly now, in order to remain consistent with
9139 -- the view of the parent type.
9141 if Ekind (Typ) = E_Private_Type
9142 or else Ekind (Typ) = E_Limited_Private_Type
9143 or else Ekind (Typ) = E_Record_Type_With_Private
9145 Dep_Elmt := First_Elmt (Private_Dependents (Typ));
9147 while Present (Dep_Elmt) loop
9148 Dep_Typ := Node (Dep_Elmt);
9150 if Scope (Dep_Typ) = Pack_Id
9151 and then Present (Full_View (Dep_Typ))
9153 Replace_Elmt (Dep_Elmt, Full_View (Dep_Typ));
9154 Exchange_Declarations (Dep_Typ);
9157 Next_Elmt (Dep_Elmt);
9161 Exchange_Declarations (Node (M));
9165 if No (Pack_Id) then
9169 -- Make the generic formal parameters private, and make the formal
9170 -- types into subtypes of the actuals again.
9172 E := First_Entity (Pack_Id);
9174 while Present (E) loop
9175 Set_Is_Hidden (E, True);
9178 and then Nkind (Parent (E)) = N_Subtype_Declaration
9180 Set_Is_Generic_Actual_Type (E, False);
9182 -- An unusual case of aliasing: the actual may also be directly
9183 -- visible in the generic, and be private there, while it is
9184 -- fully visible in the context of the instance. The internal
9185 -- subtype is private in the instance, but has full visibility
9186 -- like its parent in the enclosing scope. This enforces the
9187 -- invariant that the privacy status of all private dependents of
9188 -- a type coincide with that of the parent type. This can only
9189 -- happen when a generic child unit is instantiated within a
9192 if Is_Private_Type (E)
9193 and then not Is_Private_Type (Etype (E))
9195 Exchange_Declarations (E);
9198 elsif Ekind (E) = E_Package then
9200 -- The end of the renaming list is the renaming of the generic
9201 -- package itself. If the instance is a subprogram, all entities
9202 -- in the corresponding package are renamings. If this entity is
9203 -- a formal package, make its own formals private as well. The
9204 -- actual in this case is itself the renaming of an instantation.
9205 -- If the entity is not a package renaming, it is the entity
9206 -- created to validate formal package actuals: ignore.
9208 -- If the actual is itself a formal package for the enclosing
9209 -- generic, or the actual for such a formal package, it remains
9210 -- visible on exit from the instance, and therefore nothing
9211 -- needs to be done either, except to keep it accessible.
9214 and then Renamed_Object (E) = Pack_Id
9218 elsif Nkind (Parent (E)) /= N_Package_Renaming_Declaration then
9221 elsif Denotes_Formal_Package (Renamed_Object (E), True) then
9222 Set_Is_Hidden (E, False);
9226 Act_P : constant Entity_Id := Renamed_Object (E);
9230 Id := First_Entity (Act_P);
9232 and then Id /= First_Private_Entity (Act_P)
9234 exit when Ekind (Id) = E_Package
9235 and then Renamed_Object (Id) = Act_P;
9237 Set_Is_Hidden (Id, True);
9238 Set_Is_Potentially_Use_Visible (Id, In_Use (Act_P));
9240 if Ekind (Id) = E_Package then
9241 Restore_Nested_Formal (Id);
9252 end Restore_Private_Views;
9259 (Gen_Unit : Entity_Id;
9260 Act_Unit : Entity_Id)
9264 Set_Instance_Env (Gen_Unit, Act_Unit);
9267 ----------------------------
9268 -- Save_Global_References --
9269 ----------------------------
9271 procedure Save_Global_References (N : Node_Id) is
9272 Gen_Scope : Entity_Id;
9276 function Is_Global (E : Entity_Id) return Boolean;
9277 -- Check whether entity is defined outside of generic unit.
9278 -- Examine the scope of an entity, and the scope of the scope,
9279 -- etc, until we find either Standard, in which case the entity
9280 -- is global, or the generic unit itself, which indicates that
9281 -- the entity is local. If the entity is the generic unit itself,
9282 -- as in the case of a recursive call, or the enclosing generic unit,
9283 -- if different from the current scope, then it is local as well,
9284 -- because it will be replaced at the point of instantiation. On
9285 -- the other hand, if it is a reference to a child unit of a common
9286 -- ancestor, which appears in an instantiation, it is global because
9287 -- it is used to denote a specific compilation unit at the time the
9288 -- instantiations will be analyzed.
9290 procedure Reset_Entity (N : Node_Id);
9291 -- Save semantic information on global entity, so that it is not
9292 -- resolved again at instantiation time.
9294 procedure Save_Entity_Descendants (N : Node_Id);
9295 -- Apply Save_Global_References to the two syntactic descendants of
9296 -- non-terminal nodes that carry an Associated_Node and are processed
9297 -- through Reset_Entity. Once the global entity (if any) has been
9298 -- captured together with its type, only two syntactic descendants
9299 -- need to be traversed to complete the processing of the tree rooted
9300 -- at N. This applies to Selected_Components, Expanded_Names, and to
9301 -- Operator nodes. N can also be a character literal, identifier, or
9302 -- operator symbol node, but the call has no effect in these cases.
9304 procedure Save_Global_Defaults (N1, N2 : Node_Id);
9305 -- Default actuals in nested instances must be handled specially
9306 -- because there is no link to them from the original tree. When an
9307 -- actual subprogram is given by a default, we add an explicit generic
9308 -- association for it in the instantiation node. When we save the
9309 -- global references on the name of the instance, we recover the list
9310 -- of generic associations, and add an explicit one to the original
9311 -- generic tree, through which a global actual can be preserved.
9312 -- Similarly, if a child unit is instantiated within a sibling, in the
9313 -- context of the parent, we must preserve the identifier of the parent
9314 -- so that it can be properly resolved in a subsequent instantiation.
9316 procedure Save_Global_Descendant (D : Union_Id);
9317 -- Apply Save_Global_References recursively to the descendents of
9320 procedure Save_References (N : Node_Id);
9321 -- This is the recursive procedure that does the work, once the
9322 -- enclosing generic scope has been established.
9328 function Is_Global (E : Entity_Id) return Boolean is
9329 Se : Entity_Id := Scope (E);
9331 function Is_Instance_Node (Decl : Node_Id) return Boolean;
9332 -- Determine whether the parent node of a reference to a child unit
9333 -- denotes an instantiation or a formal package, in which case the
9334 -- reference to the child unit is global, even if it appears within
9335 -- the current scope (e.g. when the instance appears within the body
9338 ----------------------
9339 -- Is_Instance_Node --
9340 ----------------------
9342 function Is_Instance_Node (Decl : Node_Id) return Boolean is
9344 return (Nkind (Decl) in N_Generic_Instantiation
9346 Nkind (Original_Node (Decl)) = N_Formal_Package_Declaration);
9347 end Is_Instance_Node;
9349 -- Start of processing for Is_Global
9352 if E = Gen_Scope then
9355 elsif E = Standard_Standard then
9358 elsif Is_Child_Unit (E)
9359 and then (Is_Instance_Node (Parent (N2))
9360 or else (Nkind (Parent (N2)) = N_Expanded_Name
9361 and then N2 = Selector_Name (Parent (N2))
9362 and then Is_Instance_Node (Parent (Parent (N2)))))
9367 while Se /= Gen_Scope loop
9368 if Se = Standard_Standard then
9383 procedure Reset_Entity (N : Node_Id) is
9385 procedure Set_Global_Type (N : Node_Id; N2 : Node_Id);
9386 -- The type of N2 is global to the generic unit. Save the
9387 -- type in the generic node.
9389 function Top_Ancestor (E : Entity_Id) return Entity_Id;
9390 -- Find the ultimate ancestor of the current unit. If it is
9391 -- not a generic unit, then the name of the current unit
9392 -- in the prefix of an expanded name must be replaced with
9393 -- its generic homonym to ensure that it will be properly
9394 -- resolved in an instance.
9396 ---------------------
9397 -- Set_Global_Type --
9398 ---------------------
9400 procedure Set_Global_Type (N : Node_Id; N2 : Node_Id) is
9401 Typ : constant Entity_Id := Etype (N2);
9407 and then Has_Private_View (Entity (N))
9409 -- If the entity of N is not the associated node, this is
9410 -- a nested generic and it has an associated node as well,
9411 -- whose type is already the full view (see below). Indicate
9412 -- that the original node has a private view.
9414 Set_Has_Private_View (N);
9417 -- If not a private type, nothing else to do
9419 if not Is_Private_Type (Typ) then
9420 if Is_Array_Type (Typ)
9421 and then Is_Private_Type (Component_Type (Typ))
9423 Set_Has_Private_View (N);
9426 -- If it is a derivation of a private type in a context where
9427 -- no full view is needed, nothing to do either.
9429 elsif No (Full_View (Typ)) and then Typ /= Etype (Typ) then
9432 -- Otherwise mark the type for flipping and use the full_view
9436 Set_Has_Private_View (N);
9438 if Present (Full_View (Typ)) then
9439 Set_Etype (N2, Full_View (Typ));
9442 end Set_Global_Type;
9448 function Top_Ancestor (E : Entity_Id) return Entity_Id is
9449 Par : Entity_Id := E;
9452 while Is_Child_Unit (Par) loop
9459 -- Start of processing for Reset_Entity
9462 N2 := Get_Associated_Node (N);
9466 if Is_Global (E) then
9467 Set_Global_Type (N, N2);
9469 elsif Nkind (N) = N_Op_Concat
9470 and then Is_Generic_Type (Etype (N2))
9472 (Base_Type (Etype (Right_Opnd (N2))) = Etype (N2)
9473 or else Base_Type (Etype (Left_Opnd (N2))) = Etype (N2))
9474 and then Is_Intrinsic_Subprogram (E)
9479 -- Entity is local. Mark generic node as unresolved.
9480 -- Note that now it does not have an entity.
9482 Set_Associated_Node (N, Empty);
9483 Set_Etype (N, Empty);
9486 if (Nkind (Parent (N)) = N_Package_Instantiation
9487 or else Nkind (Parent (N)) = N_Function_Instantiation
9488 or else Nkind (Parent (N)) = N_Procedure_Instantiation)
9489 and then N = Name (Parent (N))
9491 Save_Global_Defaults (Parent (N), Parent (N2));
9494 elsif Nkind (Parent (N)) = N_Selected_Component
9495 and then Nkind (Parent (N2)) = N_Expanded_Name
9498 if Is_Global (Entity (Parent (N2))) then
9499 Change_Selected_Component_To_Expanded_Name (Parent (N));
9500 Set_Associated_Node (Parent (N), Parent (N2));
9501 Set_Global_Type (Parent (N), Parent (N2));
9502 Save_Entity_Descendants (N);
9504 -- If this is a reference to the current generic entity,
9505 -- replace by the name of the generic homonym of the current
9506 -- package. This is because in an instantiation Par.P.Q will
9507 -- not resolve to the name of the instance, whose enclosing
9508 -- scope is not necessarily Par. We use the generic homonym
9509 -- rather that the name of the generic itself, because it may
9510 -- be hidden by a local declaration.
9512 elsif In_Open_Scopes (Entity (Parent (N2)))
9514 Is_Generic_Unit (Top_Ancestor (Entity (Prefix (Parent (N2)))))
9516 if Ekind (Entity (Parent (N2))) = E_Generic_Package then
9517 Rewrite (Parent (N),
9518 Make_Identifier (Sloc (N),
9520 Chars (Generic_Homonym (Entity (Parent (N2))))));
9522 Rewrite (Parent (N),
9523 Make_Identifier (Sloc (N),
9524 Chars => Chars (Selector_Name (Parent (N2)))));
9528 if (Nkind (Parent (Parent (N))) = N_Package_Instantiation
9529 or else Nkind (Parent (Parent (N)))
9530 = N_Function_Instantiation
9531 or else Nkind (Parent (Parent (N)))
9532 = N_Procedure_Instantiation)
9533 and then Parent (N) = Name (Parent (Parent (N)))
9535 Save_Global_Defaults
9536 (Parent (Parent (N)), Parent (Parent ((N2))));
9539 -- A selected component may denote a static constant that has
9540 -- been folded. Make the same replacement in original tree.
9542 elsif Nkind (Parent (N)) = N_Selected_Component
9543 and then (Nkind (Parent (N2)) = N_Integer_Literal
9544 or else Nkind (Parent (N2)) = N_Real_Literal)
9546 Rewrite (Parent (N),
9547 New_Copy (Parent (N2)));
9548 Set_Analyzed (Parent (N), False);
9550 -- A selected component may be transformed into a parameterless
9551 -- function call. If the called entity is global, rewrite the
9552 -- node appropriately, i.e. as an extended name for the global
9555 elsif Nkind (Parent (N)) = N_Selected_Component
9556 and then Nkind (Parent (N2)) = N_Function_Call
9557 and then Is_Global (Entity (Name (Parent (N2))))
9559 Change_Selected_Component_To_Expanded_Name (Parent (N));
9560 Set_Associated_Node (Parent (N), Name (Parent (N2)));
9561 Set_Global_Type (Parent (N), Name (Parent (N2)));
9562 Save_Entity_Descendants (N);
9565 -- Entity is local. Reset in generic unit, so that node
9566 -- is resolved anew at the point of instantiation.
9568 Set_Associated_Node (N, Empty);
9569 Set_Etype (N, Empty);
9573 -----------------------------
9574 -- Save_Entity_Descendants --
9575 -----------------------------
9577 procedure Save_Entity_Descendants (N : Node_Id) is
9581 Save_Global_Descendant (Union_Id (Left_Opnd (N)));
9582 Save_Global_Descendant (Union_Id (Right_Opnd (N)));
9585 Save_Global_Descendant (Union_Id (Right_Opnd (N)));
9587 when N_Expanded_Name | N_Selected_Component =>
9588 Save_Global_Descendant (Union_Id (Prefix (N)));
9589 Save_Global_Descendant (Union_Id (Selector_Name (N)));
9591 when N_Identifier | N_Character_Literal | N_Operator_Symbol =>
9595 raise Program_Error;
9597 end Save_Entity_Descendants;
9599 --------------------------
9600 -- Save_Global_Defaults --
9601 --------------------------
9603 procedure Save_Global_Defaults (N1, N2 : Node_Id) is
9604 Loc : constant Source_Ptr := Sloc (N1);
9605 Assoc2 : constant List_Id := Generic_Associations (N2);
9606 Gen_Id : constant Entity_Id := Get_Generic_Entity (N2);
9616 Assoc1 := Generic_Associations (N1);
9618 if Present (Assoc1) then
9619 Act1 := First (Assoc1);
9622 Set_Generic_Associations (N1, New_List);
9623 Assoc1 := Generic_Associations (N1);
9626 if Present (Assoc2) then
9627 Act2 := First (Assoc2);
9632 while Present (Act1) and then Present (Act2) loop
9637 -- Find the associations added for default suprograms
9639 if Present (Act2) then
9640 while Nkind (Act2) /= N_Generic_Association
9641 or else No (Entity (Selector_Name (Act2)))
9642 or else not Is_Overloadable (Entity (Selector_Name (Act2)))
9647 -- Add a similar association if the default is global. The
9648 -- renaming declaration for the actual has been analyzed, and
9649 -- its alias is the program it renames. Link the actual in the
9650 -- original generic tree with the node in the analyzed tree.
9652 while Present (Act2) loop
9653 Subp := Entity (Selector_Name (Act2));
9654 Def := Explicit_Generic_Actual_Parameter (Act2);
9656 -- Following test is defence against rubbish errors
9658 if No (Alias (Subp)) then
9662 -- Retrieve the resolved actual from the renaming declaration
9663 -- created for the instantiated formal.
9665 Actual := Entity (Name (Parent (Parent (Subp))));
9666 Set_Entity (Def, Actual);
9667 Set_Etype (Def, Etype (Actual));
9669 if Is_Global (Actual) then
9671 Make_Generic_Association (Loc,
9672 Selector_Name => New_Occurrence_Of (Subp, Loc),
9673 Explicit_Generic_Actual_Parameter =>
9674 New_Occurrence_Of (Actual, Loc));
9677 (Explicit_Generic_Actual_Parameter (Ndec), Def);
9679 Append (Ndec, Assoc1);
9681 -- If there are other defaults, add a dummy association
9682 -- in case there are other defaulted formals with the same
9685 elsif Present (Next (Act2)) then
9687 Make_Generic_Association (Loc,
9688 Selector_Name => New_Occurrence_Of (Subp, Loc),
9689 Explicit_Generic_Actual_Parameter => Empty);
9691 Append (Ndec, Assoc1);
9698 if Nkind (Name (N1)) = N_Identifier
9699 and then Is_Child_Unit (Gen_Id)
9700 and then Is_Global (Gen_Id)
9701 and then Is_Generic_Unit (Scope (Gen_Id))
9702 and then In_Open_Scopes (Scope (Gen_Id))
9704 -- This is an instantiation of a child unit within a sibling,
9705 -- so that the generic parent is in scope. An eventual instance
9706 -- must occur within the scope of an instance of the parent.
9707 -- Make name in instance into an expanded name, to preserve the
9708 -- identifier of the parent, so it can be resolved subsequently.
9711 Make_Expanded_Name (Loc,
9712 Chars => Chars (Gen_Id),
9713 Prefix => New_Occurrence_Of (Scope (Gen_Id), Loc),
9714 Selector_Name => New_Occurrence_Of (Gen_Id, Loc)));
9715 Set_Entity (Name (N2), Gen_Id);
9718 Make_Expanded_Name (Loc,
9719 Chars => Chars (Gen_Id),
9720 Prefix => New_Occurrence_Of (Scope (Gen_Id), Loc),
9721 Selector_Name => New_Occurrence_Of (Gen_Id, Loc)));
9723 Set_Associated_Node (Name (N1), Name (N2));
9724 Set_Associated_Node (Prefix (Name (N1)), Empty);
9726 (Selector_Name (Name (N1)), Selector_Name (Name (N2)));
9727 Set_Etype (Name (N1), Etype (Gen_Id));
9730 end Save_Global_Defaults;
9732 ----------------------------
9733 -- Save_Global_Descendant --
9734 ----------------------------
9736 procedure Save_Global_Descendant (D : Union_Id) is
9740 if D in Node_Range then
9741 if D = Union_Id (Empty) then
9744 elsif Nkind (Node_Id (D)) /= N_Compilation_Unit then
9745 Save_References (Node_Id (D));
9748 elsif D in List_Range then
9749 if D = Union_Id (No_List)
9750 or else Is_Empty_List (List_Id (D))
9755 N1 := First (List_Id (D));
9756 while Present (N1) loop
9757 Save_References (N1);
9762 -- Element list or other non-node field, nothing to do
9767 end Save_Global_Descendant;
9769 ---------------------
9770 -- Save_References --
9771 ---------------------
9773 -- This is the recursive procedure that does the work, once the
9774 -- enclosing generic scope has been established. We have to treat
9775 -- specially a number of node rewritings that are required by semantic
9776 -- processing and which change the kind of nodes in the generic copy:
9777 -- typically constant-folding, replacing an operator node by a string
9778 -- literal, or a selected component by an expanded name. In each of
9779 -- those cases, the transformation is propagated to the generic unit.
9781 procedure Save_References (N : Node_Id) is
9786 elsif Nkind (N) = N_Character_Literal
9787 or else Nkind (N) = N_Operator_Symbol
9789 if Nkind (N) = Nkind (Get_Associated_Node (N)) then
9792 elsif Nkind (N) = N_Operator_Symbol
9793 and then Nkind (Get_Associated_Node (N)) = N_String_Literal
9795 Change_Operator_Symbol_To_String_Literal (N);
9798 elsif Nkind (N) in N_Op then
9800 if Nkind (N) = Nkind (Get_Associated_Node (N)) then
9802 if Nkind (N) = N_Op_Concat then
9803 Set_Is_Component_Left_Opnd (N,
9804 Is_Component_Left_Opnd (Get_Associated_Node (N)));
9806 Set_Is_Component_Right_Opnd (N,
9807 Is_Component_Right_Opnd (Get_Associated_Node (N)));
9812 -- Node may be transformed into call to a user-defined operator
9814 N2 := Get_Associated_Node (N);
9816 if Nkind (N2) = N_Function_Call then
9817 E := Entity (Name (N2));
9820 and then Is_Global (E)
9822 Set_Etype (N, Etype (N2));
9824 Set_Associated_Node (N, Empty);
9825 Set_Etype (N, Empty);
9828 elsif Nkind (N2) = N_Integer_Literal
9829 or else Nkind (N2) = N_Real_Literal
9830 or else Nkind (N2) = N_String_Literal
9832 -- Operation was constant-folded, perform the same
9833 -- replacement in generic.
9835 Rewrite (N, New_Copy (N2));
9836 Set_Analyzed (N, False);
9838 elsif Nkind (N2) = N_Identifier
9839 and then Ekind (Entity (N2)) = E_Enumeration_Literal
9841 -- Same if call was folded into a literal, but in this
9842 -- case retain the entity to avoid spurious ambiguities
9843 -- if id is overloaded at the point of instantiation or
9846 Rewrite (N, New_Copy (N2));
9847 Set_Analyzed (N, False);
9851 -- Complete the check on operands, if node has not been
9854 if Nkind (N) in N_Op then
9855 Save_Entity_Descendants (N);
9858 elsif Nkind (N) = N_Identifier then
9859 if Nkind (N) = Nkind (Get_Associated_Node (N)) then
9861 -- If this is a discriminant reference, always save it.
9862 -- It is used in the instance to find the corresponding
9863 -- discriminant positionally rather than by name.
9865 Set_Original_Discriminant
9866 (N, Original_Discriminant (Get_Associated_Node (N)));
9870 N2 := Get_Associated_Node (N);
9872 if Nkind (N2) = N_Function_Call then
9873 E := Entity (Name (N2));
9875 -- Name resolves to a call to parameterless function.
9876 -- If original entity is global, mark node as resolved.
9879 and then Is_Global (E)
9881 Set_Etype (N, Etype (N2));
9883 Set_Associated_Node (N, Empty);
9884 Set_Etype (N, Empty);
9888 Nkind (N2) = N_Integer_Literal or else
9889 Nkind (N2) = N_Real_Literal or else
9890 Nkind (N2) = N_String_Literal
9892 -- Name resolves to named number that is constant-folded,
9893 -- or to string literal from concatenation.
9894 -- Perform the same replacement in generic.
9896 Rewrite (N, New_Copy (N2));
9897 Set_Analyzed (N, False);
9899 elsif Nkind (N2) = N_Explicit_Dereference then
9901 -- An identifier is rewritten as a dereference if it is
9902 -- the prefix in a selected component, and it denotes an
9903 -- access to a composite type, or a parameterless function
9904 -- call that returns an access type.
9906 -- Check whether corresponding entity in prefix is global
9908 if Is_Entity_Name (Prefix (N2))
9909 and then Present (Entity (Prefix (N2)))
9910 and then Is_Global (Entity (Prefix (N2)))
9913 Make_Explicit_Dereference (Sloc (N),
9914 Prefix => Make_Identifier (Sloc (N),
9915 Chars => Chars (N))));
9916 Set_Associated_Node (Prefix (N), Prefix (N2));
9918 elsif Nkind (Prefix (N2)) = N_Function_Call
9919 and then Is_Global (Entity (Name (Prefix (N2))))
9922 Make_Explicit_Dereference (Sloc (N),
9923 Prefix => Make_Function_Call (Sloc (N),
9925 Make_Identifier (Sloc (N),
9926 Chars => Chars (N)))));
9929 (Name (Prefix (N)), Name (Prefix (N2)));
9932 Set_Associated_Node (N, Empty);
9933 Set_Etype (N, Empty);
9936 -- The subtype mark of a nominally unconstrained object
9937 -- is rewritten as a subtype indication using the bounds
9938 -- of the expression. Recover the original subtype mark.
9940 elsif Nkind (N2) = N_Subtype_Indication
9941 and then Is_Entity_Name (Original_Node (N2))
9943 Set_Associated_Node (N, Original_Node (N2));
9951 elsif Nkind (N) in N_Entity then
9956 use Atree.Unchecked_Access;
9957 -- This code section is part of implementing an untyped tree
9958 -- traversal, so it needs direct access to node fields.
9961 if Nkind (N) = N_Aggregate
9963 Nkind (N) = N_Extension_Aggregate
9965 N2 := Get_Associated_Node (N);
9968 or else No (Etype (N2))
9969 or else not Is_Global (Etype (N2))
9971 Set_Associated_Node (N, Empty);
9974 Save_Global_Descendant (Field1 (N));
9975 Save_Global_Descendant (Field2 (N));
9976 Save_Global_Descendant (Field3 (N));
9977 Save_Global_Descendant (Field5 (N));
9979 -- All other cases than aggregates
9982 Save_Global_Descendant (Field1 (N));
9983 Save_Global_Descendant (Field2 (N));
9984 Save_Global_Descendant (Field3 (N));
9985 Save_Global_Descendant (Field4 (N));
9986 Save_Global_Descendant (Field5 (N));
9990 end Save_References;
9992 -- Start of processing for Save_Global_References
9995 Gen_Scope := Current_Scope;
9997 -- If the generic unit is a child unit, references to entities in
9998 -- the parent are treated as local, because they will be resolved
9999 -- anew in the context of the instance of the parent.
10001 while Is_Child_Unit (Gen_Scope)
10002 and then Ekind (Scope (Gen_Scope)) = E_Generic_Package
10004 Gen_Scope := Scope (Gen_Scope);
10007 Save_References (N);
10008 end Save_Global_References;
10010 --------------------------------------
10011 -- Set_Copied_Sloc_For_Inlined_Body --
10012 --------------------------------------
10014 procedure Set_Copied_Sloc_For_Inlined_Body (N : Node_Id; E : Entity_Id) is
10016 Create_Instantiation_Source (N, E, True, S_Adjustment);
10017 end Set_Copied_Sloc_For_Inlined_Body;
10019 ---------------------
10020 -- Set_Instance_Of --
10021 ---------------------
10023 procedure Set_Instance_Of (A : Entity_Id; B : Entity_Id) is
10025 Generic_Renamings.Table (Generic_Renamings.Last) := (A, B, Assoc_Null);
10026 Generic_Renamings_HTable.Set (Generic_Renamings.Last);
10027 Generic_Renamings.Increment_Last;
10028 end Set_Instance_Of;
10030 --------------------
10031 -- Set_Next_Assoc --
10032 --------------------
10034 procedure Set_Next_Assoc (E : Assoc_Ptr; Next : Assoc_Ptr) is
10036 Generic_Renamings.Table (E).Next_In_HTable := Next;
10037 end Set_Next_Assoc;
10039 -------------------
10040 -- Start_Generic --
10041 -------------------
10043 procedure Start_Generic is
10045 -- ??? I am sure more things could be factored out in this
10046 -- routine. Should probably be done at a later stage.
10048 Generic_Flags.Increment_Last;
10049 Generic_Flags.Table (Generic_Flags.Last) := Inside_A_Generic;
10050 Inside_A_Generic := True;
10052 Expander_Mode_Save_And_Set (False);
10055 ----------------------
10056 -- Set_Instance_Env --
10057 ----------------------
10059 procedure Set_Instance_Env
10060 (Gen_Unit : Entity_Id;
10061 Act_Unit : Entity_Id)
10065 -- Regardless of the current mode, predefined units are analyzed in
10066 -- the most current Ada mode, and earlier version Ada checks do not
10067 -- apply to predefined units.
10069 if Is_Internal_File_Name
10070 (Fname => Unit_File_Name (Get_Source_Unit (Gen_Unit)),
10071 Renamings_Included => True) then
10072 Ada_Version := Ada_Version_Type'Last;
10075 Current_Instantiated_Parent := (Gen_Unit, Act_Unit, Assoc_Null);
10076 end Set_Instance_Env;
10082 procedure Switch_View (T : Entity_Id) is
10083 BT : constant Entity_Id := Base_Type (T);
10084 Priv_Elmt : Elmt_Id := No_Elmt;
10085 Priv_Sub : Entity_Id;
10088 -- T may be private but its base type may have been exchanged through
10089 -- some other occurrence, in which case there is nothing to switch.
10091 if not Is_Private_Type (BT) then
10095 Priv_Elmt := First_Elmt (Private_Dependents (BT));
10097 if Present (Full_View (BT)) then
10098 Append_Elmt (Full_View (BT), Exchanged_Views);
10099 Exchange_Declarations (BT);
10102 while Present (Priv_Elmt) loop
10103 Priv_Sub := (Node (Priv_Elmt));
10105 -- We avoid flipping the subtype if the Etype of its full
10106 -- view is private because this would result in a malformed
10107 -- subtype. This occurs when the Etype of the subtype full
10108 -- view is the full view of the base type (and since the
10109 -- base types were just switched, the subtype is pointing
10110 -- to the wrong view). This is currently the case for
10111 -- tagged record types, access types (maybe more?) and
10112 -- needs to be resolved. ???
10114 if Present (Full_View (Priv_Sub))
10115 and then not Is_Private_Type (Etype (Full_View (Priv_Sub)))
10117 Append_Elmt (Full_View (Priv_Sub), Exchanged_Views);
10118 Exchange_Declarations (Priv_Sub);
10121 Next_Elmt (Priv_Elmt);
10125 -----------------------------
10126 -- Valid_Default_Attribute --
10127 -----------------------------
10129 procedure Valid_Default_Attribute (Nam : Entity_Id; Def : Node_Id) is
10130 Attr_Id : constant Attribute_Id :=
10131 Get_Attribute_Id (Attribute_Name (Def));
10132 T : constant Entity_Id := Entity (Prefix (Def));
10133 Is_Fun : constant Boolean := (Ekind (Nam) = E_Function);
10146 F := First_Formal (Nam);
10147 while Present (F) loop
10148 Num_F := Num_F + 1;
10153 when Attribute_Adjacent | Attribute_Ceiling | Attribute_Copy_Sign |
10154 Attribute_Floor | Attribute_Fraction | Attribute_Machine |
10155 Attribute_Model | Attribute_Remainder | Attribute_Rounding |
10156 Attribute_Unbiased_Rounding =>
10159 and then Is_Floating_Point_Type (T);
10161 when Attribute_Image | Attribute_Pred | Attribute_Succ |
10162 Attribute_Value | Attribute_Wide_Image |
10163 Attribute_Wide_Value =>
10164 OK := (Is_Fun and then Num_F = 1 and then Is_Scalar_Type (T));
10166 when Attribute_Max | Attribute_Min =>
10167 OK := (Is_Fun and then Num_F = 2 and then Is_Scalar_Type (T));
10169 when Attribute_Input =>
10170 OK := (Is_Fun and then Num_F = 1);
10172 when Attribute_Output | Attribute_Read | Attribute_Write =>
10173 OK := (not Is_Fun and then Num_F = 2);
10180 Error_Msg_N ("attribute reference has wrong profile for subprogram",
10183 end Valid_Default_Attribute;