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, 51 Franklin Street, Fifth Floor, --
20 -- Boston, MA 02110-1301, 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 Ada_Version_Explicit : Ada_Version_Type;
695 Instantiated_Parent : Assoc;
696 Exchanged_Views : Elist_Id;
697 Hidden_Entities : Elist_Id;
698 Current_Sem_Unit : Unit_Number_Type;
699 Parent_Unit_Visible : Boolean := False;
702 package Instance_Envs is new Table.Table (
703 Table_Component_Type => Instance_Env,
704 Table_Index_Type => Int,
705 Table_Low_Bound => 0,
707 Table_Increment => 100,
708 Table_Name => "Instance_Envs");
710 procedure Restore_Private_Views
711 (Pack_Id : Entity_Id;
712 Is_Package : Boolean := True);
713 -- Restore the private views of external types, and unmark the generic
714 -- renamings of actuals, so that they become comptible subtypes again.
715 -- For subprograms, Pack_Id is the package constructed to hold the
718 procedure Switch_View (T : Entity_Id);
719 -- Switch the partial and full views of a type and its private
720 -- dependents (i.e. its subtypes and derived types).
722 ------------------------------------
723 -- Structures for Error Reporting --
724 ------------------------------------
726 Instantiation_Node : Node_Id;
727 -- Used by subprograms that validate instantiation of formal parameters
728 -- where there might be no actual on which to place the error message.
729 -- Also used to locate the instantiation node for generic subunits.
731 Instantiation_Error : exception;
732 -- When there is a semantic error in the generic parameter matching,
733 -- there is no point in continuing the instantiation, because the
734 -- number of cascaded errors is unpredictable. This exception aborts
735 -- the instantiation process altogether.
737 S_Adjustment : Sloc_Adjustment;
738 -- Offset created for each node in an instantiation, in order to keep
739 -- track of the source position of the instantiation in each of its nodes.
740 -- A subsequent semantic error or warning on a construct of the instance
741 -- points to both places: the original generic node, and the point of
742 -- instantiation. See Sinput and Sinput.L for additional details.
744 ------------------------------------------------------------
745 -- Data structure for keeping track when inside a Generic --
746 ------------------------------------------------------------
748 -- The following table is used to save values of the Inside_A_Generic
749 -- flag (see spec of Sem) when they are saved by Start_Generic.
751 package Generic_Flags is new Table.Table (
752 Table_Component_Type => Boolean,
753 Table_Index_Type => Int,
754 Table_Low_Bound => 0,
756 Table_Increment => 200,
757 Table_Name => "Generic_Flags");
759 ---------------------------
760 -- Abandon_Instantiation --
761 ---------------------------
763 procedure Abandon_Instantiation (N : Node_Id) is
765 Error_Msg_N ("instantiation abandoned!", N);
766 raise Instantiation_Error;
767 end Abandon_Instantiation;
769 --------------------------
770 -- Analyze_Associations --
771 --------------------------
773 function Analyze_Associations
776 F_Copy : List_Id) return List_Id
778 Actual_Types : constant Elist_Id := New_Elmt_List;
779 Assoc : constant List_Id := New_List;
780 Defaults : constant Elist_Id := New_Elmt_List;
781 Gen_Unit : constant Entity_Id := Defining_Entity (Parent (F_Copy));
785 Next_Formal : Node_Id;
786 Temp_Formal : Node_Id;
787 Analyzed_Formal : Node_Id;
790 First_Named : Node_Id := Empty;
791 Found_Assoc : Node_Id;
792 Is_Named_Assoc : Boolean;
793 Num_Matched : Int := 0;
794 Num_Actuals : Int := 0;
796 function Matching_Actual
798 A_F : Entity_Id) return Node_Id;
799 -- Find actual that corresponds to a given a formal parameter. If the
800 -- actuals are positional, return the next one, if any. If the actuals
801 -- are named, scan the parameter associations to find the right one.
802 -- A_F is the corresponding entity in the analyzed generic,which is
803 -- placed on the selector name for ASIS use.
805 procedure Set_Analyzed_Formal;
806 -- Find the node in the generic copy that corresponds to a given formal.
807 -- The semantic information on this node is used to perform legality
808 -- checks on the actuals. Because semantic analysis can introduce some
809 -- anonymous entities or modify the declaration node itself, the
810 -- correspondence between the two lists is not one-one. In addition to
811 -- anonymous types, the presence a formal equality will introduce an
812 -- implicit declaration for the corresponding inequality.
814 ---------------------
815 -- Matching_Actual --
816 ---------------------
818 function Matching_Actual
820 A_F : Entity_Id) return Node_Id
826 Is_Named_Assoc := False;
828 -- End of list of purely positional parameters
833 -- Case of positional parameter corresponding to current formal
835 elsif No (Selector_Name (Actual)) then
836 Found := Explicit_Generic_Actual_Parameter (Actual);
837 Found_Assoc := Actual;
838 Num_Matched := Num_Matched + 1;
841 -- Otherwise scan list of named actuals to find the one with the
842 -- desired name. All remaining actuals have explicit names.
845 Is_Named_Assoc := True;
849 while Present (Actual) loop
850 if Chars (Selector_Name (Actual)) = Chars (F) then
851 Found := Explicit_Generic_Actual_Parameter (Actual);
852 Set_Entity (Selector_Name (Actual), A_F);
853 Set_Etype (Selector_Name (Actual), Etype (A_F));
854 Generate_Reference (A_F, Selector_Name (Actual));
855 Found_Assoc := Actual;
856 Num_Matched := Num_Matched + 1;
864 -- Reset for subsequent searches. In most cases the named
865 -- associations are in order. If they are not, we reorder them
866 -- to avoid scanning twice the same actual. This is not just a
867 -- question of efficiency: there may be multiple defaults with
868 -- boxes that have the same name. In a nested instantiation we
869 -- insert actuals for those defaults, and cannot rely on their
870 -- names to disambiguate them.
872 if Actual = First_Named then
875 elsif Present (Actual) then
876 Insert_Before (First_Named, Remove_Next (Prev));
879 Actual := First_Named;
885 -------------------------
886 -- Set_Analyzed_Formal --
887 -------------------------
889 procedure Set_Analyzed_Formal is
892 while Present (Analyzed_Formal) loop
893 Kind := Nkind (Analyzed_Formal);
895 case Nkind (Formal) is
897 when N_Formal_Subprogram_Declaration =>
898 exit when Kind in N_Formal_Subprogram_Declaration
901 (Defining_Unit_Name (Specification (Formal))) =
903 (Defining_Unit_Name (Specification (Analyzed_Formal)));
905 when N_Formal_Package_Declaration =>
907 Kind = N_Formal_Package_Declaration
909 Kind = N_Generic_Package_Declaration;
911 when N_Use_Package_Clause | N_Use_Type_Clause => exit;
915 -- Skip freeze nodes, and nodes inserted to replace
916 -- unrecognized pragmas.
919 Kind not in N_Formal_Subprogram_Declaration
920 and then Kind /= N_Subprogram_Declaration
921 and then Kind /= N_Freeze_Entity
922 and then Kind /= N_Null_Statement
923 and then Kind /= N_Itype_Reference
924 and then Chars (Defining_Identifier (Formal)) =
925 Chars (Defining_Identifier (Analyzed_Formal));
928 Next (Analyzed_Formal);
931 end Set_Analyzed_Formal;
933 -- Start of processing for Analyze_Associations
936 -- If named associations are present, save the first named association
937 -- (it may of course be Empty) to facilitate subsequent name search.
939 Actuals := Generic_Associations (I_Node);
941 if Present (Actuals) then
942 First_Named := First (Actuals);
944 while Present (First_Named)
945 and then No (Selector_Name (First_Named))
947 Num_Actuals := Num_Actuals + 1;
952 Named := First_Named;
953 while Present (Named) loop
954 if No (Selector_Name (Named)) then
955 Error_Msg_N ("invalid positional actual after named one", Named);
956 Abandon_Instantiation (Named);
959 -- A named association may lack an actual parameter, if it was
960 -- introduced for a default subprogram that turns out to be local
961 -- to the outer instantiation.
963 if Present (Explicit_Generic_Actual_Parameter (Named)) then
964 Num_Actuals := Num_Actuals + 1;
970 if Present (Formals) then
971 Formal := First_Non_Pragma (Formals);
972 Analyzed_Formal := First_Non_Pragma (F_Copy);
974 if Present (Actuals) then
975 Actual := First (Actuals);
977 -- All formals should have default values
983 while Present (Formal) loop
985 Next_Formal := Next_Non_Pragma (Formal);
987 case Nkind (Formal) is
988 when N_Formal_Object_Declaration =>
991 Defining_Identifier (Formal),
992 Defining_Identifier (Analyzed_Formal));
995 (Instantiate_Object (Formal, Match, Analyzed_Formal),
998 when N_Formal_Type_Declaration =>
1001 Defining_Identifier (Formal),
1002 Defining_Identifier (Analyzed_Formal));
1005 Error_Msg_Sloc := Sloc (Gen_Unit);
1008 Instantiation_Node, Defining_Identifier (Formal));
1009 Error_Msg_NE ("\in instantiation of & declared#",
1010 Instantiation_Node, Gen_Unit);
1011 Abandon_Instantiation (Instantiation_Node);
1017 (Formal, Match, Analyzed_Formal, Assoc));
1019 -- an instantiation is a freeze point for the actuals,
1020 -- unless this is a rewritten formal package.
1022 if Nkind (I_Node) /= N_Formal_Package_Declaration then
1023 Append_Elmt (Entity (Match), Actual_Types);
1027 -- A remote access-to-class-wide type must not be an
1028 -- actual parameter for a generic formal of an access
1029 -- type (E.2.2 (17)).
1031 if Nkind (Analyzed_Formal) = N_Formal_Type_Declaration
1033 Nkind (Formal_Type_Definition (Analyzed_Formal)) =
1034 N_Access_To_Object_Definition
1036 Validate_Remote_Access_To_Class_Wide_Type (Match);
1039 when N_Formal_Subprogram_Declaration =>
1042 Defining_Unit_Name (Specification (Formal)),
1043 Defining_Unit_Name (Specification (Analyzed_Formal)));
1045 -- If the formal subprogram has the same name as
1046 -- another formal subprogram of the generic, then
1047 -- a named association is illegal (12.3(9)). Exclude
1048 -- named associations that are generated for a nested
1052 and then Is_Named_Assoc
1053 and then Comes_From_Source (Found_Assoc)
1055 Temp_Formal := First (Formals);
1056 while Present (Temp_Formal) loop
1057 if Nkind (Temp_Formal) in
1058 N_Formal_Subprogram_Declaration
1059 and then Temp_Formal /= Formal
1061 Chars (Selector_Name (Found_Assoc)) =
1062 Chars (Defining_Unit_Name
1063 (Specification (Temp_Formal)))
1066 ("name not allowed for overloaded formal",
1068 Abandon_Instantiation (Instantiation_Node);
1076 Instantiate_Formal_Subprogram
1077 (Formal, Match, Analyzed_Formal));
1080 and then Box_Present (Formal)
1083 (Defining_Unit_Name (Specification (Last (Assoc))),
1087 when N_Formal_Package_Declaration =>
1090 Defining_Identifier (Formal),
1091 Defining_Identifier (Original_Node (Analyzed_Formal)));
1094 Error_Msg_Sloc := Sloc (Gen_Unit);
1097 Instantiation_Node, Defining_Identifier (Formal));
1098 Error_Msg_NE ("\in instantiation of & declared#",
1099 Instantiation_Node, Gen_Unit);
1101 Abandon_Instantiation (Instantiation_Node);
1106 (Instantiate_Formal_Package
1107 (Formal, Match, Analyzed_Formal),
1111 -- For use type and use package appearing in the context
1112 -- clause, we have already copied them, so we can just
1113 -- move them where they belong (we mustn't recopy them
1114 -- since this would mess up the Sloc values).
1116 when N_Use_Package_Clause |
1117 N_Use_Type_Clause =>
1119 Append (Formal, Assoc);
1122 raise Program_Error;
1126 Formal := Next_Formal;
1127 Next_Non_Pragma (Analyzed_Formal);
1130 if Num_Actuals > Num_Matched then
1131 Error_Msg_Sloc := Sloc (Gen_Unit);
1133 if Present (Selector_Name (Actual)) then
1135 ("unmatched actual&",
1136 Actual, Selector_Name (Actual));
1137 Error_Msg_NE ("\in instantiation of& declared#",
1141 ("unmatched actual in instantiation of& declared#",
1146 elsif Present (Actuals) then
1148 ("too many actuals in generic instantiation", Instantiation_Node);
1152 Elmt : Elmt_Id := First_Elmt (Actual_Types);
1155 while Present (Elmt) loop
1156 Freeze_Before (I_Node, Node (Elmt));
1161 -- If there are default subprograms, normalize the tree by adding
1162 -- explicit associations for them. This is required if the instance
1163 -- appears within a generic.
1171 Elmt := First_Elmt (Defaults);
1172 while Present (Elmt) loop
1173 if No (Actuals) then
1174 Actuals := New_List;
1175 Set_Generic_Associations (I_Node, Actuals);
1178 Subp := Node (Elmt);
1180 Make_Generic_Association (Sloc (Subp),
1181 Selector_Name => New_Occurrence_Of (Subp, Sloc (Subp)),
1182 Explicit_Generic_Actual_Parameter =>
1183 New_Occurrence_Of (Subp, Sloc (Subp)));
1184 Mark_Rewrite_Insertion (New_D);
1185 Append_To (Actuals, New_D);
1191 end Analyze_Associations;
1193 -------------------------------
1194 -- Analyze_Formal_Array_Type --
1195 -------------------------------
1197 procedure Analyze_Formal_Array_Type
1198 (T : in out Entity_Id;
1204 -- Treated like a non-generic array declaration, with
1205 -- additional semantic checks.
1209 if Nkind (Def) = N_Constrained_Array_Definition then
1210 DSS := First (Discrete_Subtype_Definitions (Def));
1211 while Present (DSS) loop
1212 if Nkind (DSS) = N_Subtype_Indication
1213 or else Nkind (DSS) = N_Range
1214 or else Nkind (DSS) = N_Attribute_Reference
1216 Error_Msg_N ("only a subtype mark is allowed in a formal", DSS);
1223 Array_Type_Declaration (T, Def);
1224 Set_Is_Generic_Type (Base_Type (T));
1226 if Ekind (Component_Type (T)) = E_Incomplete_Type
1227 and then No (Full_View (Component_Type (T)))
1229 Error_Msg_N ("premature usage of incomplete type", Def);
1231 -- Check that range constraint is not allowed on the component type
1232 -- of a generic formal array type (AARM 12.5.3(3))
1234 elsif Is_Internal (Component_Type (T))
1235 and then Present (Subtype_Indication (Component_Definition (Def)))
1236 and then Nkind (Original_Node
1237 (Subtype_Indication (Component_Definition (Def))))
1238 = N_Subtype_Indication
1241 ("in a formal, a subtype indication can only be "
1242 & "a subtype mark ('R'M 12.5.3(3))",
1243 Subtype_Indication (Component_Definition (Def)));
1246 end Analyze_Formal_Array_Type;
1248 ---------------------------------------------
1249 -- Analyze_Formal_Decimal_Fixed_Point_Type --
1250 ---------------------------------------------
1252 -- As for other generic types, we create a valid type representation
1253 -- with legal but arbitrary attributes, whose values are never considered
1254 -- static. For all scalar types we introduce an anonymous base type, with
1255 -- the same attributes. We choose the corresponding integer type to be
1256 -- Standard_Integer.
1258 procedure Analyze_Formal_Decimal_Fixed_Point_Type
1262 Loc : constant Source_Ptr := Sloc (Def);
1263 Base : constant Entity_Id :=
1265 (E_Decimal_Fixed_Point_Type,
1266 Current_Scope, Sloc (Def), 'G');
1267 Int_Base : constant Entity_Id := Standard_Integer;
1268 Delta_Val : constant Ureal := Ureal_1;
1269 Digs_Val : constant Uint := Uint_6;
1274 Set_Etype (Base, Base);
1275 Set_Size_Info (Base, Int_Base);
1276 Set_RM_Size (Base, RM_Size (Int_Base));
1277 Set_First_Rep_Item (Base, First_Rep_Item (Int_Base));
1278 Set_Digits_Value (Base, Digs_Val);
1279 Set_Delta_Value (Base, Delta_Val);
1280 Set_Small_Value (Base, Delta_Val);
1281 Set_Scalar_Range (Base,
1283 Low_Bound => Make_Real_Literal (Loc, Ureal_1),
1284 High_Bound => Make_Real_Literal (Loc, Ureal_1)));
1286 Set_Is_Generic_Type (Base);
1287 Set_Parent (Base, Parent (Def));
1289 Set_Ekind (T, E_Decimal_Fixed_Point_Subtype);
1290 Set_Etype (T, Base);
1291 Set_Size_Info (T, Int_Base);
1292 Set_RM_Size (T, RM_Size (Int_Base));
1293 Set_First_Rep_Item (T, First_Rep_Item (Int_Base));
1294 Set_Digits_Value (T, Digs_Val);
1295 Set_Delta_Value (T, Delta_Val);
1296 Set_Small_Value (T, Delta_Val);
1297 Set_Scalar_Range (T, Scalar_Range (Base));
1298 Set_Is_Constrained (T);
1300 Check_Restriction (No_Fixed_Point, Def);
1301 end Analyze_Formal_Decimal_Fixed_Point_Type;
1303 ---------------------------------
1304 -- Analyze_Formal_Derived_Type --
1305 ---------------------------------
1307 procedure Analyze_Formal_Derived_Type
1312 Loc : constant Source_Ptr := Sloc (Def);
1313 Unk_Disc : constant Boolean := Unknown_Discriminants_Present (N);
1317 Set_Is_Generic_Type (T);
1319 if Private_Present (Def) then
1321 Make_Private_Extension_Declaration (Loc,
1322 Defining_Identifier => T,
1323 Discriminant_Specifications => Discriminant_Specifications (N),
1324 Unknown_Discriminants_Present => Unk_Disc,
1325 Subtype_Indication => Subtype_Mark (Def));
1327 Set_Abstract_Present (New_N, Abstract_Present (Def));
1331 Make_Full_Type_Declaration (Loc,
1332 Defining_Identifier => T,
1333 Discriminant_Specifications =>
1334 Discriminant_Specifications (Parent (T)),
1336 Make_Derived_Type_Definition (Loc,
1337 Subtype_Indication => Subtype_Mark (Def)));
1339 Set_Abstract_Present
1340 (Type_Definition (New_N), Abstract_Present (Def));
1347 if not Is_Composite_Type (T) then
1349 ("unknown discriminants not allowed for elementary types", N);
1351 Set_Has_Unknown_Discriminants (T);
1352 Set_Is_Constrained (T, False);
1356 -- If the parent type has a known size, so does the formal, which
1357 -- makes legal representation clauses that involve the formal.
1359 Set_Size_Known_At_Compile_Time
1360 (T, Size_Known_At_Compile_Time (Entity (Subtype_Mark (Def))));
1362 end Analyze_Formal_Derived_Type;
1364 ----------------------------------
1365 -- Analyze_Formal_Discrete_Type --
1366 ----------------------------------
1368 -- The operations defined for a discrete types are those of an
1369 -- enumeration type. The size is set to an arbitrary value, for use
1370 -- in analyzing the generic unit.
1372 procedure Analyze_Formal_Discrete_Type (T : Entity_Id; Def : Node_Id) is
1373 Loc : constant Source_Ptr := Sloc (Def);
1377 Base : constant Entity_Id :=
1379 (E_Floating_Point_Type, Current_Scope, Sloc (Def), 'G');
1382 Set_Ekind (T, E_Enumeration_Subtype);
1383 Set_Etype (T, Base);
1386 Set_Is_Generic_Type (T);
1387 Set_Is_Constrained (T);
1389 -- For semantic analysis, the bounds of the type must be set to some
1390 -- non-static value. The simplest is to create attribute nodes for
1391 -- those bounds, that refer to the type itself. These bounds are never
1392 -- analyzed but serve as place-holders.
1395 Make_Attribute_Reference (Loc,
1396 Attribute_Name => Name_First,
1397 Prefix => New_Reference_To (T, Loc));
1401 Make_Attribute_Reference (Loc,
1402 Attribute_Name => Name_Last,
1403 Prefix => New_Reference_To (T, Loc));
1406 Set_Scalar_Range (T,
1411 Set_Ekind (Base, E_Enumeration_Type);
1412 Set_Etype (Base, Base);
1413 Init_Size (Base, 8);
1414 Init_Alignment (Base);
1415 Set_Is_Generic_Type (Base);
1416 Set_Scalar_Range (Base, Scalar_Range (T));
1417 Set_Parent (Base, Parent (Def));
1419 end Analyze_Formal_Discrete_Type;
1421 ----------------------------------
1422 -- Analyze_Formal_Floating_Type --
1423 ---------------------------------
1425 procedure Analyze_Formal_Floating_Type (T : Entity_Id; Def : Node_Id) is
1426 Base : constant Entity_Id :=
1428 (E_Floating_Point_Type, Current_Scope, Sloc (Def), 'G');
1431 -- The various semantic attributes are taken from the predefined type
1432 -- Float, just so that all of them are initialized. Their values are
1433 -- never used because no constant folding or expansion takes place in
1434 -- the generic itself.
1437 Set_Ekind (T, E_Floating_Point_Subtype);
1438 Set_Etype (T, Base);
1439 Set_Size_Info (T, (Standard_Float));
1440 Set_RM_Size (T, RM_Size (Standard_Float));
1441 Set_Digits_Value (T, Digits_Value (Standard_Float));
1442 Set_Scalar_Range (T, Scalar_Range (Standard_Float));
1443 Set_Is_Constrained (T);
1445 Set_Is_Generic_Type (Base);
1446 Set_Etype (Base, Base);
1447 Set_Size_Info (Base, (Standard_Float));
1448 Set_RM_Size (Base, RM_Size (Standard_Float));
1449 Set_Digits_Value (Base, Digits_Value (Standard_Float));
1450 Set_Scalar_Range (Base, Scalar_Range (Standard_Float));
1451 Set_Parent (Base, Parent (Def));
1453 Check_Restriction (No_Floating_Point, Def);
1454 end Analyze_Formal_Floating_Type;
1456 ---------------------------------
1457 -- Analyze_Formal_Modular_Type --
1458 ---------------------------------
1460 procedure Analyze_Formal_Modular_Type (T : Entity_Id; Def : Node_Id) is
1462 -- Apart from their entity kind, generic modular types are treated
1463 -- like signed integer types, and have the same attributes.
1465 Analyze_Formal_Signed_Integer_Type (T, Def);
1466 Set_Ekind (T, E_Modular_Integer_Subtype);
1467 Set_Ekind (Etype (T), E_Modular_Integer_Type);
1469 end Analyze_Formal_Modular_Type;
1471 ---------------------------------------
1472 -- Analyze_Formal_Object_Declaration --
1473 ---------------------------------------
1475 procedure Analyze_Formal_Object_Declaration (N : Node_Id) is
1476 E : constant Node_Id := Expression (N);
1477 Id : constant Node_Id := Defining_Identifier (N);
1484 -- Determine the mode of the formal object
1486 if Out_Present (N) then
1487 K := E_Generic_In_Out_Parameter;
1489 if not In_Present (N) then
1490 Error_Msg_N ("formal generic objects cannot have mode OUT", N);
1494 K := E_Generic_In_Parameter;
1497 Find_Type (Subtype_Mark (N));
1498 T := Entity (Subtype_Mark (N));
1500 if Ekind (T) = E_Incomplete_Type then
1501 Error_Msg_N ("premature usage of incomplete type", Subtype_Mark (N));
1504 if K = E_Generic_In_Parameter then
1506 -- Ada 2005 (AI-287): Limited aggregates allowed in generic formals
1508 if Ada_Version < Ada_05 and then Is_Limited_Type (T) then
1510 ("generic formal of mode IN must not be of limited type", N);
1511 Explain_Limited_Type (T, N);
1514 if Is_Abstract (T) then
1516 ("generic formal of mode IN must not be of abstract type", N);
1520 Analyze_Per_Use_Expression (E, T);
1526 -- Case of generic IN OUT parameter
1529 -- If the formal has an unconstrained type, construct its
1530 -- actual subtype, as is done for subprogram formals. In this
1531 -- fashion, all its uses can refer to specific bounds.
1536 if (Is_Array_Type (T)
1537 and then not Is_Constrained (T))
1539 (Ekind (T) = E_Record_Type
1540 and then Has_Discriminants (T))
1543 Non_Freezing_Ref : constant Node_Id :=
1544 New_Reference_To (Id, Sloc (Id));
1548 -- Make sure that the actual subtype doesn't generate
1551 Set_Must_Not_Freeze (Non_Freezing_Ref);
1552 Decl := Build_Actual_Subtype (T, Non_Freezing_Ref);
1553 Insert_Before_And_Analyze (N, Decl);
1554 Set_Actual_Subtype (Id, Defining_Identifier (Decl));
1557 Set_Actual_Subtype (Id, T);
1562 ("initialization not allowed for `IN OUT` formals", N);
1566 end Analyze_Formal_Object_Declaration;
1568 ----------------------------------------------
1569 -- Analyze_Formal_Ordinary_Fixed_Point_Type --
1570 ----------------------------------------------
1572 procedure Analyze_Formal_Ordinary_Fixed_Point_Type
1576 Loc : constant Source_Ptr := Sloc (Def);
1577 Base : constant Entity_Id :=
1579 (E_Ordinary_Fixed_Point_Type, Current_Scope, Sloc (Def), 'G');
1581 -- The semantic attributes are set for completeness only, their
1582 -- values will never be used, because all properties of the type
1586 Set_Ekind (T, E_Ordinary_Fixed_Point_Subtype);
1587 Set_Etype (T, Base);
1588 Set_Size_Info (T, Standard_Integer);
1589 Set_RM_Size (T, RM_Size (Standard_Integer));
1590 Set_Small_Value (T, Ureal_1);
1591 Set_Delta_Value (T, Ureal_1);
1592 Set_Scalar_Range (T,
1594 Low_Bound => Make_Real_Literal (Loc, Ureal_1),
1595 High_Bound => Make_Real_Literal (Loc, Ureal_1)));
1596 Set_Is_Constrained (T);
1598 Set_Is_Generic_Type (Base);
1599 Set_Etype (Base, Base);
1600 Set_Size_Info (Base, Standard_Integer);
1601 Set_RM_Size (Base, RM_Size (Standard_Integer));
1602 Set_Small_Value (Base, Ureal_1);
1603 Set_Delta_Value (Base, Ureal_1);
1604 Set_Scalar_Range (Base, Scalar_Range (T));
1605 Set_Parent (Base, Parent (Def));
1607 Check_Restriction (No_Fixed_Point, Def);
1608 end Analyze_Formal_Ordinary_Fixed_Point_Type;
1610 ----------------------------
1611 -- Analyze_Formal_Package --
1612 ----------------------------
1614 procedure Analyze_Formal_Package (N : Node_Id) is
1615 Loc : constant Source_Ptr := Sloc (N);
1616 Pack_Id : constant Entity_Id := Defining_Identifier (N);
1618 Gen_Id : constant Node_Id := Name (N);
1620 Gen_Unit : Entity_Id;
1622 Parent_Installed : Boolean := False;
1624 Parent_Instance : Entity_Id;
1625 Renaming_In_Par : Entity_Id;
1628 Text_IO_Kludge (Gen_Id);
1631 Check_Generic_Child_Unit (Gen_Id, Parent_Installed);
1632 Gen_Unit := Entity (Gen_Id);
1634 if Ekind (Gen_Unit) /= E_Generic_Package then
1635 Error_Msg_N ("expect generic package name", Gen_Id);
1639 elsif Gen_Unit = Current_Scope then
1641 ("generic package cannot be used as a formal package of itself",
1646 elsif In_Open_Scopes (Gen_Unit) then
1647 if Is_Compilation_Unit (Gen_Unit)
1648 and then Is_Child_Unit (Current_Scope)
1650 -- Special-case the error when the formal is a parent, and
1651 -- continue analysis to minimize cascaded errors.
1654 ("generic parent cannot be used as formal package "
1655 & "of a child unit",
1660 ("generic package cannot be used as a formal package "
1668 -- Check for a formal package that is a package renaming
1670 if Present (Renamed_Object (Gen_Unit)) then
1671 Gen_Unit := Renamed_Object (Gen_Unit);
1674 -- The formal package is treated like a regular instance, but only
1675 -- the specification needs to be instantiated, to make entities visible.
1677 if not Box_Present (N) then
1678 Hidden_Entities := New_Elmt_List;
1679 Analyze_Package_Instantiation (N);
1681 if Parent_Installed then
1686 -- If there are no generic associations, the generic parameters
1687 -- appear as local entities and are instantiated like them. We copy
1688 -- the generic package declaration as if it were an instantiation,
1689 -- and analyze it like a regular package, except that we treat the
1690 -- formals as additional visible components.
1692 Gen_Decl := Unit_Declaration_Node (Gen_Unit);
1694 if In_Extended_Main_Source_Unit (N) then
1695 Set_Is_Instantiated (Gen_Unit);
1696 Generate_Reference (Gen_Unit, N);
1699 Formal := New_Copy (Pack_Id);
1700 Create_Instantiation_Source (N, Gen_Unit, False, S_Adjustment);
1704 (Original_Node (Gen_Decl), Empty, Instantiating => True);
1706 Set_Defining_Unit_Name (Specification (New_N), Formal);
1707 Set_Instance_Env (Gen_Unit, Formal);
1709 Enter_Name (Formal);
1710 Set_Ekind (Formal, E_Generic_Package);
1711 Set_Etype (Formal, Standard_Void_Type);
1712 Set_Inner_Instances (Formal, New_Elmt_List);
1715 -- Within the formal, the name of the generic package is a renaming
1716 -- of the formal (as for a regular instantiation).
1718 Renaming := Make_Package_Renaming_Declaration (Loc,
1719 Defining_Unit_Name =>
1720 Make_Defining_Identifier (Loc, Chars (Gen_Unit)),
1721 Name => New_Reference_To (Formal, Loc));
1723 if Present (Visible_Declarations (Specification (N))) then
1724 Prepend (Renaming, To => Visible_Declarations (Specification (N)));
1725 elsif Present (Private_Declarations (Specification (N))) then
1726 Prepend (Renaming, To => Private_Declarations (Specification (N)));
1729 if Is_Child_Unit (Gen_Unit)
1730 and then Parent_Installed
1732 -- Similarly, we have to make the name of the formal visible in
1733 -- the parent instance, to resolve properly fully qualified names
1734 -- that may appear in the generic unit. The parent instance has
1735 -- been placed on the scope stack ahead of the current scope.
1737 Parent_Instance := Scope_Stack.Table (Scope_Stack.Last - 1).Entity;
1740 Make_Defining_Identifier (Loc, Chars (Gen_Unit));
1741 Set_Ekind (Renaming_In_Par, E_Package);
1742 Set_Etype (Renaming_In_Par, Standard_Void_Type);
1743 Set_Scope (Renaming_In_Par, Parent_Instance);
1744 Set_Parent (Renaming_In_Par, Parent (Formal));
1745 Set_Renamed_Object (Renaming_In_Par, Formal);
1746 Append_Entity (Renaming_In_Par, Parent_Instance);
1749 Analyze_Generic_Formal_Part (N);
1750 Analyze (Specification (N));
1751 End_Package_Scope (Formal);
1753 if Parent_Installed then
1759 -- Inside the generic unit, the formal package is a regular
1760 -- package, but no body is needed for it. Note that after
1761 -- instantiation, the defining_unit_name we need is in the
1762 -- new tree and not in the original. (see Package_Instantiation).
1763 -- A generic formal package is an instance, and can be used as
1764 -- an actual for an inner instance. Mark its generic parent.
1766 Set_Ekind (Formal, E_Package);
1767 Set_Generic_Parent (Specification (N), Gen_Unit);
1768 Set_Has_Completion (Formal, True);
1770 Set_Ekind (Pack_Id, E_Package);
1771 Set_Etype (Pack_Id, Standard_Void_Type);
1772 Set_Scope (Pack_Id, Scope (Formal));
1773 Set_Has_Completion (Pack_Id, True);
1775 end Analyze_Formal_Package;
1777 ---------------------------------
1778 -- Analyze_Formal_Private_Type --
1779 ---------------------------------
1781 procedure Analyze_Formal_Private_Type
1787 New_Private_Type (N, T, Def);
1789 -- Set the size to an arbitrary but legal value
1791 Set_Size_Info (T, Standard_Integer);
1792 Set_RM_Size (T, RM_Size (Standard_Integer));
1793 end Analyze_Formal_Private_Type;
1795 ----------------------------------------
1796 -- Analyze_Formal_Signed_Integer_Type --
1797 ----------------------------------------
1799 procedure Analyze_Formal_Signed_Integer_Type
1803 Base : constant Entity_Id :=
1805 (E_Signed_Integer_Type, Current_Scope, Sloc (Def), 'G');
1810 Set_Ekind (T, E_Signed_Integer_Subtype);
1811 Set_Etype (T, Base);
1812 Set_Size_Info (T, Standard_Integer);
1813 Set_RM_Size (T, RM_Size (Standard_Integer));
1814 Set_Scalar_Range (T, Scalar_Range (Standard_Integer));
1815 Set_Is_Constrained (T);
1817 Set_Is_Generic_Type (Base);
1818 Set_Size_Info (Base, Standard_Integer);
1819 Set_RM_Size (Base, RM_Size (Standard_Integer));
1820 Set_Etype (Base, Base);
1821 Set_Scalar_Range (Base, Scalar_Range (Standard_Integer));
1822 Set_Parent (Base, Parent (Def));
1823 end Analyze_Formal_Signed_Integer_Type;
1825 -------------------------------
1826 -- Analyze_Formal_Subprogram --
1827 -------------------------------
1829 procedure Analyze_Formal_Subprogram (N : Node_Id) is
1830 Spec : constant Node_Id := Specification (N);
1831 Def : constant Node_Id := Default_Name (N);
1832 Nam : constant Entity_Id := Defining_Unit_Name (Spec);
1840 if Nkind (Nam) = N_Defining_Program_Unit_Name then
1841 Error_Msg_N ("name of formal subprogram must be a direct name", Nam);
1845 Analyze_Subprogram_Declaration (N);
1846 Set_Is_Formal_Subprogram (Nam);
1847 Set_Has_Completion (Nam);
1849 if Nkind (N) = N_Formal_Abstract_Subprogram_Declaration then
1850 Set_Is_Abstract (Nam);
1851 Set_Is_Dispatching_Operation (Nam);
1854 Ctrl_Type : constant Entity_Id := Find_Dispatching_Type (Nam);
1857 if not Present (Ctrl_Type) then
1859 ("abstract formal subprogram must have a controlling type",
1863 Check_Controlling_Formals (Ctrl_Type, Nam);
1868 -- Default name is resolved at the point of instantiation
1870 if Box_Present (N) then
1873 -- Else default is bound at the point of generic declaration
1875 elsif Present (Def) then
1876 if Nkind (Def) = N_Operator_Symbol then
1877 Find_Direct_Name (Def);
1879 elsif Nkind (Def) /= N_Attribute_Reference then
1883 -- For an attribute reference, analyze the prefix and verify
1884 -- that it has the proper profile for the subprogram.
1886 Analyze (Prefix (Def));
1887 Valid_Default_Attribute (Nam, Def);
1891 -- Default name may be overloaded, in which case the interpretation
1892 -- with the correct profile must be selected, as for a renaming.
1894 if Etype (Def) = Any_Type then
1897 elsif Nkind (Def) = N_Selected_Component then
1898 Subp := Entity (Selector_Name (Def));
1900 if Ekind (Subp) /= E_Entry then
1901 Error_Msg_N ("expect valid subprogram name as default", Def);
1905 elsif Nkind (Def) = N_Indexed_Component then
1907 if Nkind (Prefix (Def)) /= N_Selected_Component then
1908 Error_Msg_N ("expect valid subprogram name as default", Def);
1912 Subp := Entity (Selector_Name (Prefix (Def)));
1914 if Ekind (Subp) /= E_Entry_Family then
1915 Error_Msg_N ("expect valid subprogram name as default", Def);
1920 elsif Nkind (Def) = N_Character_Literal then
1922 -- Needs some type checks: subprogram should be parameterless???
1924 Resolve (Def, (Etype (Nam)));
1926 elsif not Is_Entity_Name (Def)
1927 or else not Is_Overloadable (Entity (Def))
1929 Error_Msg_N ("expect valid subprogram name as default", Def);
1932 elsif not Is_Overloaded (Def) then
1933 Subp := Entity (Def);
1936 Error_Msg_N ("premature usage of formal subprogram", Def);
1938 elsif not Entity_Matches_Spec (Subp, Nam) then
1939 Error_Msg_N ("no visible entity matches specification", Def);
1945 I1 : Interp_Index := 0;
1951 Get_First_Interp (Def, I, It);
1952 while Present (It.Nam) loop
1954 if Entity_Matches_Spec (It.Nam, Nam) then
1955 if Subp /= Any_Id then
1956 It1 := Disambiguate (Def, I1, I, Etype (Subp));
1958 if It1 = No_Interp then
1959 Error_Msg_N ("ambiguous default subprogram", Def);
1972 Get_Next_Interp (I, It);
1976 if Subp /= Any_Id then
1977 Set_Entity (Def, Subp);
1980 Error_Msg_N ("premature usage of formal subprogram", Def);
1982 elsif Ekind (Subp) /= E_Operator then
1983 Check_Mode_Conformant (Subp, Nam);
1987 Error_Msg_N ("no visible subprogram matches specification", N);
1991 end Analyze_Formal_Subprogram;
1993 -------------------------------------
1994 -- Analyze_Formal_Type_Declaration --
1995 -------------------------------------
1997 procedure Analyze_Formal_Type_Declaration (N : Node_Id) is
1998 Def : constant Node_Id := Formal_Type_Definition (N);
2002 T := Defining_Identifier (N);
2004 if Present (Discriminant_Specifications (N))
2005 and then Nkind (Def) /= N_Formal_Private_Type_Definition
2008 ("discriminants not allowed for this formal type",
2009 Defining_Identifier (First (Discriminant_Specifications (N))));
2012 -- Enter the new name, and branch to specific routine
2015 when N_Formal_Private_Type_Definition =>
2016 Analyze_Formal_Private_Type (N, T, Def);
2018 when N_Formal_Derived_Type_Definition =>
2019 Analyze_Formal_Derived_Type (N, T, Def);
2021 when N_Formal_Discrete_Type_Definition =>
2022 Analyze_Formal_Discrete_Type (T, Def);
2024 when N_Formal_Signed_Integer_Type_Definition =>
2025 Analyze_Formal_Signed_Integer_Type (T, Def);
2027 when N_Formal_Modular_Type_Definition =>
2028 Analyze_Formal_Modular_Type (T, Def);
2030 when N_Formal_Floating_Point_Definition =>
2031 Analyze_Formal_Floating_Type (T, Def);
2033 when N_Formal_Ordinary_Fixed_Point_Definition =>
2034 Analyze_Formal_Ordinary_Fixed_Point_Type (T, Def);
2036 when N_Formal_Decimal_Fixed_Point_Definition =>
2037 Analyze_Formal_Decimal_Fixed_Point_Type (T, Def);
2039 when N_Array_Type_Definition =>
2040 Analyze_Formal_Array_Type (T, Def);
2042 when N_Access_To_Object_Definition |
2043 N_Access_Function_Definition |
2044 N_Access_Procedure_Definition =>
2045 Analyze_Generic_Access_Type (T, Def);
2051 raise Program_Error;
2055 Set_Is_Generic_Type (T);
2056 end Analyze_Formal_Type_Declaration;
2058 ------------------------------------
2059 -- Analyze_Function_Instantiation --
2060 ------------------------------------
2062 procedure Analyze_Function_Instantiation (N : Node_Id) is
2064 Analyze_Subprogram_Instantiation (N, E_Function);
2065 end Analyze_Function_Instantiation;
2067 ---------------------------------
2068 -- Analyze_Generic_Access_Type --
2069 ---------------------------------
2071 procedure Analyze_Generic_Access_Type (T : Entity_Id; Def : Node_Id) is
2075 if Nkind (Def) = N_Access_To_Object_Definition then
2076 Access_Type_Declaration (T, Def);
2078 if Is_Incomplete_Or_Private_Type (Designated_Type (T))
2079 and then No (Full_View (Designated_Type (T)))
2080 and then not Is_Generic_Type (Designated_Type (T))
2082 Error_Msg_N ("premature usage of incomplete type", Def);
2084 elsif Is_Internal (Designated_Type (T)) then
2086 ("only a subtype mark is allowed in a formal", Def);
2090 Access_Subprogram_Declaration (T, Def);
2092 end Analyze_Generic_Access_Type;
2094 ---------------------------------
2095 -- Analyze_Generic_Formal_Part --
2096 ---------------------------------
2098 procedure Analyze_Generic_Formal_Part (N : Node_Id) is
2099 Gen_Parm_Decl : Node_Id;
2102 -- The generic formals are processed in the scope of the generic
2103 -- unit, where they are immediately visible. The scope is installed
2106 Gen_Parm_Decl := First (Generic_Formal_Declarations (N));
2108 while Present (Gen_Parm_Decl) loop
2109 Analyze (Gen_Parm_Decl);
2110 Next (Gen_Parm_Decl);
2113 Generate_Reference_To_Generic_Formals (Current_Scope);
2114 end Analyze_Generic_Formal_Part;
2116 ------------------------------------------
2117 -- Analyze_Generic_Package_Declaration --
2118 ------------------------------------------
2120 procedure Analyze_Generic_Package_Declaration (N : Node_Id) is
2121 Loc : constant Source_Ptr := Sloc (N);
2124 Save_Parent : Node_Id;
2126 Decls : constant List_Id :=
2127 Visible_Declarations (Specification (N));
2131 -- We introduce a renaming of the enclosing package, to have a usable
2132 -- entity as the prefix of an expanded name for a local entity of the
2133 -- form Par.P.Q, where P is the generic package. This is because a local
2134 -- entity named P may hide it, so that the usual visibility rules in
2135 -- the instance will not resolve properly.
2138 Make_Package_Renaming_Declaration (Loc,
2139 Defining_Unit_Name =>
2140 Make_Defining_Identifier (Loc,
2141 Chars => New_External_Name (Chars (Defining_Entity (N)), "GH")),
2142 Name => Make_Identifier (Loc, Chars (Defining_Entity (N))));
2144 if Present (Decls) then
2145 Decl := First (Decls);
2146 while Present (Decl)
2147 and then Nkind (Decl) = N_Pragma
2152 if Present (Decl) then
2153 Insert_Before (Decl, Renaming);
2155 Append (Renaming, Visible_Declarations (Specification (N)));
2159 Set_Visible_Declarations (Specification (N), New_List (Renaming));
2162 -- Create copy of generic unit, and save for instantiation.
2163 -- If the unit is a child unit, do not copy the specifications
2164 -- for the parent, which are not part of the generic tree.
2166 Save_Parent := Parent_Spec (N);
2167 Set_Parent_Spec (N, Empty);
2169 New_N := Copy_Generic_Node (N, Empty, Instantiating => False);
2170 Set_Parent_Spec (New_N, Save_Parent);
2172 Id := Defining_Entity (N);
2173 Generate_Definition (Id);
2175 -- Expansion is not applied to generic units
2180 Set_Ekind (Id, E_Generic_Package);
2181 Set_Etype (Id, Standard_Void_Type);
2183 Enter_Generic_Scope (Id);
2184 Set_Inner_Instances (Id, New_Elmt_List);
2186 Set_Categorization_From_Pragmas (N);
2187 Set_Is_Pure (Id, Is_Pure (Current_Scope));
2189 -- Link the declaration of the generic homonym in the generic copy
2190 -- to the package it renames, so that it is always resolved properly.
2192 Set_Generic_Homonym (Id, Defining_Unit_Name (Renaming));
2193 Set_Entity (Associated_Node (Name (Renaming)), Id);
2195 -- For a library unit, we have reconstructed the entity for the
2196 -- unit, and must reset it in the library tables.
2198 if Nkind (Parent (N)) = N_Compilation_Unit then
2199 Set_Cunit_Entity (Current_Sem_Unit, Id);
2202 Analyze_Generic_Formal_Part (N);
2204 -- After processing the generic formals, analysis proceeds
2205 -- as for a non-generic package.
2207 Analyze (Specification (N));
2209 Validate_Categorization_Dependency (N, Id);
2213 End_Package_Scope (Id);
2214 Exit_Generic_Scope (Id);
2216 if Nkind (Parent (N)) /= N_Compilation_Unit then
2217 Move_Freeze_Nodes (Id, N, Visible_Declarations (Specification (N)));
2218 Move_Freeze_Nodes (Id, N, Private_Declarations (Specification (N)));
2219 Move_Freeze_Nodes (Id, N, Generic_Formal_Declarations (N));
2222 Set_Body_Required (Parent (N), Unit_Requires_Body (Id));
2223 Validate_RT_RAT_Component (N);
2225 -- If this is a spec without a body, check that generic parameters
2228 if not Body_Required (Parent (N)) then
2229 Check_References (Id);
2232 end Analyze_Generic_Package_Declaration;
2234 --------------------------------------------
2235 -- Analyze_Generic_Subprogram_Declaration --
2236 --------------------------------------------
2238 procedure Analyze_Generic_Subprogram_Declaration (N : Node_Id) is
2243 Save_Parent : Node_Id;
2246 -- Create copy of generic unit,and save for instantiation.
2247 -- If the unit is a child unit, do not copy the specifications
2248 -- for the parent, which are not part of the generic tree.
2250 Save_Parent := Parent_Spec (N);
2251 Set_Parent_Spec (N, Empty);
2253 New_N := Copy_Generic_Node (N, Empty, Instantiating => False);
2254 Set_Parent_Spec (New_N, Save_Parent);
2257 Spec := Specification (N);
2258 Id := Defining_Entity (Spec);
2259 Generate_Definition (Id);
2261 if Nkind (Id) = N_Defining_Operator_Symbol then
2263 ("operator symbol not allowed for generic subprogram", Id);
2270 Set_Scope_Depth_Value (Id, Scope_Depth (Current_Scope) + 1);
2272 Enter_Generic_Scope (Id);
2273 Set_Inner_Instances (Id, New_Elmt_List);
2274 Set_Is_Pure (Id, Is_Pure (Current_Scope));
2276 Analyze_Generic_Formal_Part (N);
2278 Formals := Parameter_Specifications (Spec);
2280 if Present (Formals) then
2281 Process_Formals (Formals, Spec);
2284 if Nkind (Spec) = N_Function_Specification then
2285 Set_Ekind (Id, E_Generic_Function);
2286 Find_Type (Subtype_Mark (Spec));
2287 Set_Etype (Id, Entity (Subtype_Mark (Spec)));
2289 Set_Ekind (Id, E_Generic_Procedure);
2290 Set_Etype (Id, Standard_Void_Type);
2293 -- For a library unit, we have reconstructed the entity for the
2294 -- unit, and must reset it in the library tables. We also need
2295 -- to make sure that Body_Required is set properly in the original
2296 -- compilation unit node.
2298 if Nkind (Parent (N)) = N_Compilation_Unit then
2299 Set_Cunit_Entity (Current_Sem_Unit, Id);
2300 Set_Body_Required (Parent (N), Unit_Requires_Body (Id));
2303 Set_Categorization_From_Pragmas (N);
2304 Validate_Categorization_Dependency (N, Id);
2306 Save_Global_References (Original_Node (N));
2310 Exit_Generic_Scope (Id);
2311 Generate_Reference_To_Formals (Id);
2312 end Analyze_Generic_Subprogram_Declaration;
2314 -----------------------------------
2315 -- Analyze_Package_Instantiation --
2316 -----------------------------------
2318 -- Note: this procedure is also used for formal package declarations,
2319 -- in which case the argument N is an N_Formal_Package_Declaration
2320 -- node. This should really be noted in the spec! ???
2322 procedure Analyze_Package_Instantiation (N : Node_Id) is
2323 Loc : constant Source_Ptr := Sloc (N);
2324 Gen_Id : constant Node_Id := Name (N);
2327 Act_Decl_Name : Node_Id;
2328 Act_Decl_Id : Entity_Id;
2333 Gen_Unit : Entity_Id;
2335 Is_Actual_Pack : constant Boolean :=
2336 Is_Internal (Defining_Entity (N));
2338 Parent_Installed : Boolean := False;
2339 Renaming_List : List_Id;
2340 Unit_Renaming : Node_Id;
2341 Needs_Body : Boolean;
2342 Inline_Now : Boolean := False;
2344 procedure Delay_Descriptors (E : Entity_Id);
2345 -- Delay generation of subprogram descriptors for given entity
2347 function Might_Inline_Subp return Boolean;
2348 -- If inlining is active and the generic contains inlined subprograms,
2349 -- we instantiate the body. This may cause superfluous instantiations,
2350 -- but it is simpler than detecting the need for the body at the point
2351 -- of inlining, when the context of the instance is not available.
2353 -----------------------
2354 -- Delay_Descriptors --
2355 -----------------------
2357 procedure Delay_Descriptors (E : Entity_Id) is
2359 if not Delay_Subprogram_Descriptors (E) then
2360 Set_Delay_Subprogram_Descriptors (E);
2361 Pending_Descriptor.Increment_Last;
2362 Pending_Descriptor.Table (Pending_Descriptor.Last) := E;
2364 end Delay_Descriptors;
2366 -----------------------
2367 -- Might_Inline_Subp --
2368 -----------------------
2370 function Might_Inline_Subp return Boolean is
2374 if not Inline_Processing_Required then
2378 E := First_Entity (Gen_Unit);
2379 while Present (E) loop
2380 if Is_Subprogram (E)
2381 and then Is_Inlined (E)
2391 end Might_Inline_Subp;
2393 -- Start of processing for Analyze_Package_Instantiation
2396 -- Very first thing: apply the special kludge for Text_IO processing
2397 -- in case we are instantiating one of the children of [Wide_]Text_IO.
2399 Text_IO_Kludge (Name (N));
2401 -- Make node global for error reporting
2403 Instantiation_Node := N;
2405 -- Case of instantiation of a generic package
2407 if Nkind (N) = N_Package_Instantiation then
2408 Act_Decl_Id := New_Copy (Defining_Entity (N));
2409 Set_Comes_From_Source (Act_Decl_Id, True);
2411 if Nkind (Defining_Unit_Name (N)) = N_Defining_Program_Unit_Name then
2413 Make_Defining_Program_Unit_Name (Loc,
2414 Name => New_Copy_Tree (Name (Defining_Unit_Name (N))),
2415 Defining_Identifier => Act_Decl_Id);
2417 Act_Decl_Name := Act_Decl_Id;
2420 -- Case of instantiation of a formal package
2423 Act_Decl_Id := Defining_Identifier (N);
2424 Act_Decl_Name := Act_Decl_Id;
2427 Generate_Definition (Act_Decl_Id);
2428 Pre_Analyze_Actuals (N);
2431 Check_Generic_Child_Unit (Gen_Id, Parent_Installed);
2432 Gen_Unit := Entity (Gen_Id);
2434 -- Verify that it is the name of a generic package
2436 if Etype (Gen_Unit) = Any_Type then
2440 elsif Ekind (Gen_Unit) /= E_Generic_Package then
2442 -- Ada 2005 (AI-50217): Cannot use instance in limited with_clause
2444 if From_With_Type (Gen_Unit) then
2446 ("cannot instantiate a limited withed package", Gen_Id);
2449 ("expect name of generic package in instantiation", Gen_Id);
2456 if In_Extended_Main_Source_Unit (N) then
2457 Set_Is_Instantiated (Gen_Unit);
2458 Generate_Reference (Gen_Unit, N);
2460 if Present (Renamed_Object (Gen_Unit)) then
2461 Set_Is_Instantiated (Renamed_Object (Gen_Unit));
2462 Generate_Reference (Renamed_Object (Gen_Unit), N);
2466 if Nkind (Gen_Id) = N_Identifier
2467 and then Chars (Gen_Unit) = Chars (Defining_Entity (N))
2470 ("& is hidden within declaration of instance", Gen_Id, Gen_Unit);
2472 elsif Nkind (Gen_Id) = N_Expanded_Name
2473 and then Is_Child_Unit (Gen_Unit)
2474 and then Nkind (Prefix (Gen_Id)) = N_Identifier
2475 and then Chars (Act_Decl_Id) = Chars (Prefix (Gen_Id))
2478 ("& is hidden within declaration of instance ", Prefix (Gen_Id));
2481 Set_Entity (Gen_Id, Gen_Unit);
2483 -- If generic is a renaming, get original generic unit
2485 if Present (Renamed_Object (Gen_Unit))
2486 and then Ekind (Renamed_Object (Gen_Unit)) = E_Generic_Package
2488 Gen_Unit := Renamed_Object (Gen_Unit);
2491 -- Verify that there are no circular instantiations
2493 if In_Open_Scopes (Gen_Unit) then
2494 Error_Msg_NE ("instantiation of & within itself", N, Gen_Unit);
2498 elsif Contains_Instance_Of (Gen_Unit, Current_Scope, Gen_Id) then
2499 Error_Msg_Node_2 := Current_Scope;
2501 ("circular Instantiation: & instantiated in &!", N, Gen_Unit);
2502 Circularity_Detected := True;
2507 Set_Instance_Env (Gen_Unit, Act_Decl_Id);
2508 Gen_Decl := Unit_Declaration_Node (Gen_Unit);
2510 -- Initialize renamings map, for error checking, and the list
2511 -- that holds private entities whose views have changed between
2512 -- generic definition and instantiation. If this is the instance
2513 -- created to validate an actual package, the instantiation
2514 -- environment is that of the enclosing instance.
2516 Generic_Renamings.Set_Last (0);
2517 Generic_Renamings_HTable.Reset;
2519 Create_Instantiation_Source (N, Gen_Unit, False, S_Adjustment);
2521 -- Copy original generic tree, to produce text for instantiation
2525 (Original_Node (Gen_Decl), Empty, Instantiating => True);
2527 Act_Spec := Specification (Act_Tree);
2529 -- If this is the instance created to validate an actual package,
2530 -- only the formals matter, do not examine the package spec itself.
2532 if Is_Actual_Pack then
2533 Set_Visible_Declarations (Act_Spec, New_List);
2534 Set_Private_Declarations (Act_Spec, New_List);
2538 Analyze_Associations
2540 Generic_Formal_Declarations (Act_Tree),
2541 Generic_Formal_Declarations (Gen_Decl));
2543 Set_Defining_Unit_Name (Act_Spec, Act_Decl_Name);
2544 Set_Is_Generic_Instance (Act_Decl_Id);
2546 Set_Generic_Parent (Act_Spec, Gen_Unit);
2548 -- References to the generic in its own declaration or its body
2549 -- are references to the instance. Add a renaming declaration for
2550 -- the generic unit itself. This declaration, as well as the renaming
2551 -- declarations for the generic formals, must remain private to the
2552 -- unit: the formals, because this is the language semantics, and
2553 -- the unit because its use is an artifact of the implementation.
2556 Make_Package_Renaming_Declaration (Loc,
2557 Defining_Unit_Name =>
2558 Make_Defining_Identifier (Loc, Chars (Gen_Unit)),
2559 Name => New_Reference_To (Act_Decl_Id, Loc));
2561 Append (Unit_Renaming, Renaming_List);
2563 -- The renaming declarations are the first local declarations of
2566 if Is_Non_Empty_List (Visible_Declarations (Act_Spec)) then
2568 (First (Visible_Declarations (Act_Spec)), Renaming_List);
2570 Set_Visible_Declarations (Act_Spec, Renaming_List);
2574 Make_Package_Declaration (Loc,
2575 Specification => Act_Spec);
2577 -- Save the instantiation node, for subsequent instantiation
2578 -- of the body, if there is one and we are generating code for
2579 -- the current unit. Mark the unit as having a body, to avoid
2580 -- a premature error message.
2582 -- We instantiate the body if we are generating code, if we are
2583 -- generating cross-reference information, or if we are building
2584 -- trees for ASIS use.
2587 Enclosing_Body_Present : Boolean := False;
2588 -- If the generic unit is not a compilation unit, then a body
2589 -- may be present in its parent even if none is required. We
2590 -- create a tentative pending instantiation for the body, which
2591 -- will be discarded if none is actually present.
2596 if Scope (Gen_Unit) /= Standard_Standard
2597 and then not Is_Child_Unit (Gen_Unit)
2599 Scop := Scope (Gen_Unit);
2601 while Present (Scop)
2602 and then Scop /= Standard_Standard
2604 if Unit_Requires_Body (Scop) then
2605 Enclosing_Body_Present := True;
2608 elsif In_Open_Scopes (Scop)
2609 and then In_Package_Body (Scop)
2611 Enclosing_Body_Present := True;
2615 exit when Is_Compilation_Unit (Scop);
2616 Scop := Scope (Scop);
2620 -- If front-end inlining is enabled, and this is a unit for which
2621 -- code will be generated, we instantiate the body at once.
2622 -- This is done if the instance is not the main unit, and if the
2623 -- generic is not a child unit of another generic, to avoid scope
2624 -- problems and the reinstallation of parent instances.
2627 and then (not Is_Child_Unit (Gen_Unit)
2628 or else not Is_Generic_Unit (Scope (Gen_Unit)))
2629 and then Might_Inline_Subp
2630 and then not Is_Actual_Pack
2632 if Front_End_Inlining
2633 and then (Is_In_Main_Unit (N)
2634 or else In_Main_Context (Current_Scope))
2635 and then Nkind (Parent (N)) /= N_Compilation_Unit
2639 -- In configurable_run_time mode we force the inlining of
2640 -- predefined subprogram marked Inline_Always, to minimize
2641 -- the use of the run-time library.
2643 elsif Is_Predefined_File_Name
2644 (Unit_File_Name (Get_Source_Unit (Gen_Decl)))
2645 and then Configurable_Run_Time_Mode
2646 and then Nkind (Parent (N)) /= N_Compilation_Unit
2653 (Unit_Requires_Body (Gen_Unit)
2654 or else Enclosing_Body_Present
2655 or else Present (Corresponding_Body (Gen_Decl)))
2656 and then (Is_In_Main_Unit (N)
2657 or else Might_Inline_Subp)
2658 and then not Is_Actual_Pack
2659 and then not Inline_Now
2660 and then (Operating_Mode = Generate_Code
2661 or else (Operating_Mode = Check_Semantics
2662 and then ASIS_Mode));
2664 -- If front_end_inlining is enabled, do not instantiate a
2665 -- body if within a generic context.
2667 if (Front_End_Inlining
2668 and then not Expander_Active)
2669 or else Is_Generic_Unit (Cunit_Entity (Main_Unit))
2671 Needs_Body := False;
2674 -- If the current context is generic, and the package being
2675 -- instantiated is declared within a formal package, there is no
2676 -- body to instantiate until the enclosing generic is instantiated
2677 -- and there is an actual for the formal package. If the formal
2678 -- package has parameters, we build regular package instance for
2679 -- it, that preceeds the original formal package declaration.
2681 if In_Open_Scopes (Scope (Scope (Gen_Unit))) then
2683 Decl : constant Node_Id :=
2685 (Unit_Declaration_Node (Scope (Gen_Unit)));
2687 if Nkind (Decl) = N_Formal_Package_Declaration
2688 or else (Nkind (Decl) = N_Package_Declaration
2689 and then Is_List_Member (Decl)
2690 and then Present (Next (Decl))
2692 Nkind (Next (Decl)) = N_Formal_Package_Declaration)
2694 Needs_Body := False;
2700 -- If we are generating the calling stubs from the instantiation of
2701 -- a generic RCI package, we will not use the body of the generic
2704 if Distribution_Stub_Mode = Generate_Caller_Stub_Body
2705 and then Is_Compilation_Unit (Defining_Entity (N))
2707 Needs_Body := False;
2712 -- Here is a defence against a ludicrous number of instantiations
2713 -- caused by a circular set of instantiation attempts.
2715 if Pending_Instantiations.Last >
2716 Hostparm.Max_Instantiations
2718 Error_Msg_N ("too many instantiations", N);
2719 raise Unrecoverable_Error;
2722 -- Indicate that the enclosing scopes contain an instantiation,
2723 -- and that cleanup actions should be delayed until after the
2724 -- instance body is expanded.
2726 Check_Forward_Instantiation (Gen_Decl);
2727 if Nkind (N) = N_Package_Instantiation then
2729 Enclosing_Master : Entity_Id := Current_Scope;
2732 while Enclosing_Master /= Standard_Standard loop
2734 if Ekind (Enclosing_Master) = E_Package then
2735 if Is_Compilation_Unit (Enclosing_Master) then
2736 if In_Package_Body (Enclosing_Master) then
2738 (Body_Entity (Enclosing_Master));
2747 Enclosing_Master := Scope (Enclosing_Master);
2750 elsif Ekind (Enclosing_Master) = E_Generic_Package then
2751 Enclosing_Master := Scope (Enclosing_Master);
2753 elsif Is_Generic_Subprogram (Enclosing_Master)
2754 or else Ekind (Enclosing_Master) = E_Void
2756 -- Cleanup actions will eventually be performed on
2757 -- the enclosing instance, if any. enclosing scope
2758 -- is void in the formal part of a generic subp.
2763 if Ekind (Enclosing_Master) = E_Entry
2765 Ekind (Scope (Enclosing_Master)) = E_Protected_Type
2768 Protected_Body_Subprogram (Enclosing_Master);
2771 Set_Delay_Cleanups (Enclosing_Master);
2773 while Ekind (Enclosing_Master) = E_Block loop
2774 Enclosing_Master := Scope (Enclosing_Master);
2777 if Is_Subprogram (Enclosing_Master) then
2778 Delay_Descriptors (Enclosing_Master);
2780 elsif Is_Task_Type (Enclosing_Master) then
2782 TBP : constant Node_Id :=
2783 Get_Task_Body_Procedure
2787 if Present (TBP) then
2788 Delay_Descriptors (TBP);
2789 Set_Delay_Cleanups (TBP);
2799 -- Make entry in table
2801 Pending_Instantiations.Increment_Last;
2802 Pending_Instantiations.Table (Pending_Instantiations.Last) :=
2803 (N, Act_Decl, Expander_Active, Current_Sem_Unit);
2807 Set_Categorization_From_Pragmas (Act_Decl);
2809 if Parent_Installed then
2813 Set_Instance_Spec (N, Act_Decl);
2815 -- If not a compilation unit, insert the package declaration
2816 -- before the original instantiation node.
2818 if Nkind (Parent (N)) /= N_Compilation_Unit then
2819 Mark_Rewrite_Insertion (Act_Decl);
2820 Insert_Before (N, Act_Decl);
2823 -- For an instantiation that is a compilation unit, place
2824 -- declaration on current node so context is complete
2825 -- for analysis (including nested instantiations). It this
2826 -- is the main unit, the declaration eventually replaces the
2827 -- instantiation node. If the instance body is later created, it
2828 -- replaces the instance node, and the declation is attached to
2829 -- it (see Build_Instance_Compilation_Unit_Nodes).
2832 if Cunit_Entity (Current_Sem_Unit) = Defining_Entity (N) then
2834 -- The entity for the current unit is the newly created one,
2835 -- and all semantic information is attached to it.
2837 Set_Cunit_Entity (Current_Sem_Unit, Act_Decl_Id);
2839 -- If this is the main unit, replace the main entity as well
2841 if Current_Sem_Unit = Main_Unit then
2842 Main_Unit_Entity := Act_Decl_Id;
2846 -- There is a problem with inlining here
2847 -- More comments needed??? what problem
2849 Set_Unit (Parent (N), Act_Decl);
2850 Set_Parent_Spec (Act_Decl, Parent_Spec (N));
2852 Set_Unit (Parent (N), N);
2853 Set_Body_Required (Parent (N), False);
2855 -- We never need elaboration checks on instantiations, since
2856 -- by definition, the body instantiation is elaborated at the
2857 -- same time as the spec instantiation.
2859 Set_Suppress_Elaboration_Warnings (Act_Decl_Id);
2860 Set_Kill_Elaboration_Checks (Act_Decl_Id);
2863 Check_Elab_Instantiation (N);
2865 if ABE_Is_Certain (N) and then Needs_Body then
2866 Pending_Instantiations.Decrement_Last;
2868 Check_Hidden_Child_Unit (N, Gen_Unit, Act_Decl_Id);
2870 Set_First_Private_Entity (Defining_Unit_Name (Unit_Renaming),
2871 First_Private_Entity (Act_Decl_Id));
2873 -- If the instantiation will receive a body, the unit will
2874 -- be transformed into a package body, and receive its own
2875 -- elaboration entity. Otherwise, the nature of the unit is
2876 -- now a package declaration.
2878 if Nkind (Parent (N)) = N_Compilation_Unit
2879 and then not Needs_Body
2881 Rewrite (N, Act_Decl);
2884 if Present (Corresponding_Body (Gen_Decl))
2885 or else Unit_Requires_Body (Gen_Unit)
2887 Set_Has_Completion (Act_Decl_Id);
2890 Check_Formal_Packages (Act_Decl_Id);
2892 Restore_Private_Views (Act_Decl_Id);
2894 if not Generic_Separately_Compiled (Gen_Unit) then
2895 Inherit_Context (Gen_Decl, N);
2898 if Parent_Installed then
2905 Validate_Categorization_Dependency (N, Act_Decl_Id);
2907 -- Check restriction, but skip this if something went wrong in
2908 -- the above analysis, indicated by Act_Decl_Id being void.
2910 if Ekind (Act_Decl_Id) /= E_Void
2911 and then not Is_Library_Level_Entity (Act_Decl_Id)
2913 Check_Restriction (No_Local_Allocators, N);
2917 Inline_Instance_Body (N, Gen_Unit, Act_Decl);
2920 -- The following is a tree patch for ASIS: ASIS needs separate nodes
2921 -- to be used as defining identifiers for a formal package and for the
2922 -- corresponding expanded package
2924 if Nkind (N) = N_Formal_Package_Declaration then
2925 Act_Decl_Id := New_Copy (Defining_Entity (N));
2926 Set_Comes_From_Source (Act_Decl_Id, True);
2927 Set_Is_Generic_Instance (Act_Decl_Id, False);
2928 Set_Defining_Identifier (N, Act_Decl_Id);
2932 when Instantiation_Error =>
2933 if Parent_Installed then
2936 end Analyze_Package_Instantiation;
2938 --------------------------
2939 -- Inline_Instance_Body --
2940 --------------------------
2942 procedure Inline_Instance_Body
2944 Gen_Unit : Entity_Id;
2948 Gen_Comp : constant Entity_Id :=
2949 Cunit_Entity (Get_Source_Unit (Gen_Unit));
2950 Curr_Comp : constant Node_Id := Cunit (Current_Sem_Unit);
2951 Curr_Scope : Entity_Id := Empty;
2952 Curr_Unit : constant Entity_Id :=
2953 Cunit_Entity (Current_Sem_Unit);
2954 Removed : Boolean := False;
2955 Num_Scopes : Int := 0;
2956 Use_Clauses : array (1 .. Scope_Stack.Last) of Node_Id;
2957 Instances : array (1 .. Scope_Stack.Last) of Entity_Id;
2958 Inner_Scopes : array (1 .. Scope_Stack.Last) of Entity_Id;
2959 Num_Inner : Int := 0;
2960 N_Instances : Int := 0;
2964 -- Case of generic unit defined in another unit. We must remove
2965 -- the complete context of the current unit to install that of
2968 if Gen_Comp /= Cunit_Entity (Current_Sem_Unit) then
2972 and then S /= Standard_Standard
2974 Num_Scopes := Num_Scopes + 1;
2976 Use_Clauses (Num_Scopes) :=
2978 (Scope_Stack.Last - Num_Scopes + 1).
2980 End_Use_Clauses (Use_Clauses (Num_Scopes));
2982 exit when Is_Generic_Instance (S)
2983 and then (In_Package_Body (S)
2984 or else Ekind (S) = E_Procedure
2985 or else Ekind (S) = E_Function);
2989 Vis := Is_Immediately_Visible (Gen_Comp);
2991 -- Find and save all enclosing instances
2996 and then S /= Standard_Standard
2998 if Is_Generic_Instance (S) then
2999 N_Instances := N_Instances + 1;
3000 Instances (N_Instances) := S;
3002 exit when In_Package_Body (S);
3008 -- Remove context of current compilation unit, unless we
3009 -- are within a nested package instantiation, in which case
3010 -- the context has been removed previously.
3012 -- If current scope is the body of a child unit, remove context
3018 and then S /= Standard_Standard
3020 exit when Is_Generic_Instance (S)
3021 and then (In_Package_Body (S)
3022 or else Ekind (S) = E_Procedure
3023 or else Ekind (S) = E_Function);
3026 or else (Ekind (Curr_Unit) = E_Package_Body
3027 and then S = Spec_Entity (Curr_Unit))
3028 or else (Ekind (Curr_Unit) = E_Subprogram_Body
3031 (Unit_Declaration_Node (Curr_Unit)))
3035 -- Remove entities in current scopes from visibility, so
3036 -- than instance body is compiled in a clean environment.
3038 Save_Scope_Stack (Handle_Use => False);
3040 if Is_Child_Unit (S) then
3042 -- Remove child unit from stack, as well as inner scopes.
3043 -- Removing the context of a child unit removes parent
3046 while Current_Scope /= S loop
3047 Num_Inner := Num_Inner + 1;
3048 Inner_Scopes (Num_Inner) := Current_Scope;
3053 Remove_Context (Curr_Comp);
3057 Remove_Context (Curr_Comp);
3060 if Ekind (Curr_Unit) = E_Package_Body then
3061 Remove_Context (Library_Unit (Curr_Comp));
3068 New_Scope (Standard_Standard);
3069 Scope_Stack.Table (Scope_Stack.Last).Is_Active_Stack_Base := True;
3070 Instantiate_Package_Body
3071 ((N, Act_Decl, Expander_Active, Current_Sem_Unit), True);
3076 Set_Is_Immediately_Visible (Gen_Comp, Vis);
3078 -- Reset Generic_Instance flag so that use clauses can be installed
3079 -- in the proper order. (See Use_One_Package for effect of enclosing
3080 -- instances on processing of use clauses).
3082 for J in 1 .. N_Instances loop
3083 Set_Is_Generic_Instance (Instances (J), False);
3087 Install_Context (Curr_Comp);
3089 if Present (Curr_Scope)
3090 and then Is_Child_Unit (Curr_Scope)
3092 New_Scope (Curr_Scope);
3093 Set_Is_Immediately_Visible (Curr_Scope);
3095 -- Finally, restore inner scopes as well
3097 for J in reverse 1 .. Num_Inner loop
3098 New_Scope (Inner_Scopes (J));
3102 Restore_Scope_Stack (Handle_Use => False);
3104 if Present (Curr_Scope)
3106 (In_Private_Part (Curr_Scope)
3107 or else In_Package_Body (Curr_Scope))
3109 -- Install private declaration of ancestor units, which
3110 -- are currently available. Restore_Scope_Stack and
3111 -- Install_Context only install the visible part of parents.
3116 Par := Scope (Curr_Scope);
3117 while (Present (Par))
3118 and then Par /= Standard_Standard
3120 Install_Private_Declarations (Par);
3127 -- Restore use clauses. For a child unit, use clauses in the parents
3128 -- are restored when installing the context, so only those in inner
3129 -- scopes (and those local to the child unit itself) need to be
3130 -- installed explicitly.
3132 if Is_Child_Unit (Curr_Unit)
3135 for J in reverse 1 .. Num_Inner + 1 loop
3136 Scope_Stack.Table (Scope_Stack.Last - J + 1).First_Use_Clause :=
3138 Install_Use_Clauses (Use_Clauses (J));
3142 for J in reverse 1 .. Num_Scopes loop
3143 Scope_Stack.Table (Scope_Stack.Last - J + 1).First_Use_Clause :=
3145 Install_Use_Clauses (Use_Clauses (J));
3149 for J in 1 .. N_Instances loop
3150 Set_Is_Generic_Instance (Instances (J), True);
3153 -- If generic unit is in current unit, current context is correct
3156 Instantiate_Package_Body
3157 ((N, Act_Decl, Expander_Active, Current_Sem_Unit), True);
3159 end Inline_Instance_Body;
3161 -------------------------------------
3162 -- Analyze_Procedure_Instantiation --
3163 -------------------------------------
3165 procedure Analyze_Procedure_Instantiation (N : Node_Id) is
3167 Analyze_Subprogram_Instantiation (N, E_Procedure);
3168 end Analyze_Procedure_Instantiation;
3170 --------------------------------------
3171 -- Analyze_Subprogram_Instantiation --
3172 --------------------------------------
3174 procedure Analyze_Subprogram_Instantiation
3178 Loc : constant Source_Ptr := Sloc (N);
3179 Gen_Id : constant Node_Id := Name (N);
3181 Anon_Id : constant Entity_Id :=
3182 Make_Defining_Identifier (Sloc (Defining_Entity (N)),
3183 Chars => New_External_Name
3184 (Chars (Defining_Entity (N)), 'R'));
3186 Act_Decl_Id : Entity_Id;
3191 Gen_Unit : Entity_Id;
3193 Pack_Id : Entity_Id;
3194 Parent_Installed : Boolean := False;
3195 Renaming_List : List_Id;
3197 procedure Analyze_Instance_And_Renamings;
3198 -- The instance must be analyzed in a context that includes the
3199 -- mappings of generic parameters into actuals. We create a package
3200 -- declaration for this purpose, and a subprogram with an internal
3201 -- name within the package. The subprogram instance is simply an
3202 -- alias for the internal subprogram, declared in the current scope.
3204 ------------------------------------
3205 -- Analyze_Instance_And_Renamings --
3206 ------------------------------------
3208 procedure Analyze_Instance_And_Renamings is
3209 Def_Ent : constant Entity_Id := Defining_Entity (N);
3210 Pack_Decl : Node_Id;
3213 if Nkind (Parent (N)) = N_Compilation_Unit then
3215 -- For the case of a compilation unit, the container package
3216 -- has the same name as the instantiation, to insure that the
3217 -- binder calls the elaboration procedure with the right name.
3218 -- Copy the entity of the instance, which may have compilation
3219 -- level flags (e.g. Is_Child_Unit) set.
3221 Pack_Id := New_Copy (Def_Ent);
3224 -- Otherwise we use the name of the instantiation concatenated
3225 -- with its source position to ensure uniqueness if there are
3226 -- several instantiations with the same name.
3229 Make_Defining_Identifier (Loc,
3230 Chars => New_External_Name
3231 (Related_Id => Chars (Def_Ent),
3233 Suffix_Index => Source_Offset (Sloc (Def_Ent))));
3236 Pack_Decl := Make_Package_Declaration (Loc,
3237 Specification => Make_Package_Specification (Loc,
3238 Defining_Unit_Name => Pack_Id,
3239 Visible_Declarations => Renaming_List,
3240 End_Label => Empty));
3242 Set_Instance_Spec (N, Pack_Decl);
3243 Set_Is_Generic_Instance (Pack_Id);
3244 Set_Needs_Debug_Info (Pack_Id);
3246 -- Case of not a compilation unit
3248 if Nkind (Parent (N)) /= N_Compilation_Unit then
3249 Mark_Rewrite_Insertion (Pack_Decl);
3250 Insert_Before (N, Pack_Decl);
3251 Set_Has_Completion (Pack_Id);
3253 -- Case of an instantiation that is a compilation unit
3255 -- Place declaration on current node so context is complete
3256 -- for analysis (including nested instantiations), and for
3257 -- use in a context_clause (see Analyze_With_Clause).
3260 Set_Unit (Parent (N), Pack_Decl);
3261 Set_Parent_Spec (Pack_Decl, Parent_Spec (N));
3264 Analyze (Pack_Decl);
3265 Check_Formal_Packages (Pack_Id);
3266 Set_Is_Generic_Instance (Pack_Id, False);
3268 -- Body of the enclosing package is supplied when instantiating
3269 -- the subprogram body, after semantic analysis is completed.
3271 if Nkind (Parent (N)) = N_Compilation_Unit then
3273 -- Remove package itself from visibility, so it does not
3274 -- conflict with subprogram.
3276 Set_Name_Entity_Id (Chars (Pack_Id), Homonym (Pack_Id));
3278 -- Set name and scope of internal subprogram so that the
3279 -- proper external name will be generated. The proper scope
3280 -- is the scope of the wrapper package. We need to generate
3281 -- debugging information for the internal subprogram, so set
3282 -- flag accordingly.
3284 Set_Chars (Anon_Id, Chars (Defining_Entity (N)));
3285 Set_Scope (Anon_Id, Scope (Pack_Id));
3287 -- Mark wrapper package as referenced, to avoid spurious
3288 -- warnings if the instantiation appears in various with_
3289 -- clauses of subunits of the main unit.
3291 Set_Referenced (Pack_Id);
3294 Set_Is_Generic_Instance (Anon_Id);
3295 Set_Needs_Debug_Info (Anon_Id);
3296 Act_Decl_Id := New_Copy (Anon_Id);
3298 Set_Parent (Act_Decl_Id, Parent (Anon_Id));
3299 Set_Chars (Act_Decl_Id, Chars (Defining_Entity (N)));
3300 Set_Sloc (Act_Decl_Id, Sloc (Defining_Entity (N)));
3301 Set_Comes_From_Source (Act_Decl_Id, True);
3303 -- The signature may involve types that are not frozen yet, but
3304 -- the subprogram will be frozen at the point the wrapper package
3305 -- is frozen, so it does not need its own freeze node. In fact, if
3306 -- one is created, it might conflict with the freezing actions from
3307 -- the wrapper package (see 7206-013).
3309 Set_Has_Delayed_Freeze (Anon_Id, False);
3311 -- If the instance is a child unit, mark the Id accordingly. Mark
3312 -- the anonymous entity as well, which is the real subprogram and
3313 -- which is used when the instance appears in a context clause.
3315 Set_Is_Child_Unit (Act_Decl_Id, Is_Child_Unit (Defining_Entity (N)));
3316 Set_Is_Child_Unit (Anon_Id, Is_Child_Unit (Defining_Entity (N)));
3317 New_Overloaded_Entity (Act_Decl_Id);
3318 Check_Eliminated (Act_Decl_Id);
3320 -- In compilation unit case, kill elaboration checks on the
3321 -- instantiation, since they are never needed -- the body is
3322 -- instantiated at the same point as the spec.
3324 if Nkind (Parent (N)) = N_Compilation_Unit then
3325 Set_Suppress_Elaboration_Warnings (Act_Decl_Id);
3326 Set_Kill_Elaboration_Checks (Act_Decl_Id);
3327 Set_Is_Compilation_Unit (Anon_Id);
3329 Set_Cunit_Entity (Current_Sem_Unit, Pack_Id);
3332 -- The instance is not a freezing point for the new subprogram
3334 Set_Is_Frozen (Act_Decl_Id, False);
3336 if Nkind (Defining_Entity (N)) = N_Defining_Operator_Symbol then
3337 Valid_Operator_Definition (Act_Decl_Id);
3340 Set_Alias (Act_Decl_Id, Anon_Id);
3341 Set_Parent (Act_Decl_Id, Parent (Anon_Id));
3342 Set_Has_Completion (Act_Decl_Id);
3343 Set_Related_Instance (Pack_Id, Act_Decl_Id);
3345 if Nkind (Parent (N)) = N_Compilation_Unit then
3346 Set_Body_Required (Parent (N), False);
3349 end Analyze_Instance_And_Renamings;
3351 -- Start of processing for Analyze_Subprogram_Instantiation
3354 -- Very first thing: apply the special kludge for Text_IO processing
3355 -- in case we are instantiating one of the children of [Wide_]Text_IO.
3356 -- Of course such an instantiation is bogus (these are packages, not
3357 -- subprograms), but we get a better error message if we do this.
3359 Text_IO_Kludge (Gen_Id);
3361 -- Make node global for error reporting
3363 Instantiation_Node := N;
3364 Pre_Analyze_Actuals (N);
3367 Check_Generic_Child_Unit (Gen_Id, Parent_Installed);
3368 Gen_Unit := Entity (Gen_Id);
3370 Generate_Reference (Gen_Unit, Gen_Id);
3372 if Nkind (Gen_Id) = N_Identifier
3373 and then Chars (Gen_Unit) = Chars (Defining_Entity (N))
3376 ("& is hidden within declaration of instance", Gen_Id, Gen_Unit);
3379 if Etype (Gen_Unit) = Any_Type then
3384 -- Verify that it is a generic subprogram of the right kind, and that
3385 -- it does not lead to a circular instantiation.
3387 if Ekind (Gen_Unit) /= E_Generic_Procedure
3388 and then Ekind (Gen_Unit) /= E_Generic_Function
3390 Error_Msg_N ("expect generic subprogram in instantiation", Gen_Id);
3392 elsif In_Open_Scopes (Gen_Unit) then
3393 Error_Msg_NE ("instantiation of & within itself", N, Gen_Unit);
3395 elsif K = E_Procedure
3396 and then Ekind (Gen_Unit) /= E_Generic_Procedure
3398 if Ekind (Gen_Unit) = E_Generic_Function then
3400 ("cannot instantiate generic function as procedure", Gen_Id);
3403 ("expect name of generic procedure in instantiation", Gen_Id);
3406 elsif K = E_Function
3407 and then Ekind (Gen_Unit) /= E_Generic_Function
3409 if Ekind (Gen_Unit) = E_Generic_Procedure then
3411 ("cannot instantiate generic procedure as function", Gen_Id);
3414 ("expect name of generic function in instantiation", Gen_Id);
3418 Set_Entity (Gen_Id, Gen_Unit);
3419 Set_Is_Instantiated (Gen_Unit);
3421 if In_Extended_Main_Source_Unit (N) then
3422 Generate_Reference (Gen_Unit, N);
3425 -- If renaming, get original unit
3427 if Present (Renamed_Object (Gen_Unit))
3428 and then (Ekind (Renamed_Object (Gen_Unit)) = E_Generic_Procedure
3430 Ekind (Renamed_Object (Gen_Unit)) = E_Generic_Function)
3432 Gen_Unit := Renamed_Object (Gen_Unit);
3433 Set_Is_Instantiated (Gen_Unit);
3434 Generate_Reference (Gen_Unit, N);
3437 if Contains_Instance_Of (Gen_Unit, Current_Scope, Gen_Id) then
3438 Error_Msg_Node_2 := Current_Scope;
3440 ("circular Instantiation: & instantiated in &!", N, Gen_Unit);
3441 Circularity_Detected := True;
3445 Gen_Decl := Unit_Declaration_Node (Gen_Unit);
3447 -- The subprogram itself cannot contain a nested instance, so
3448 -- the current parent is left empty.
3450 Set_Instance_Env (Gen_Unit, Empty);
3452 -- Initialize renamings map, for error checking
3454 Generic_Renamings.Set_Last (0);
3455 Generic_Renamings_HTable.Reset;
3457 Create_Instantiation_Source (N, Gen_Unit, False, S_Adjustment);
3459 -- Copy original generic tree, to produce text for instantiation
3463 (Original_Node (Gen_Decl), Empty, Instantiating => True);
3465 Act_Spec := Specification (Act_Tree);
3467 Analyze_Associations
3469 Generic_Formal_Declarations (Act_Tree),
3470 Generic_Formal_Declarations (Gen_Decl));
3472 -- Build the subprogram declaration, which does not appear
3473 -- in the generic template, and give it a sloc consistent
3474 -- with that of the template.
3476 Set_Defining_Unit_Name (Act_Spec, Anon_Id);
3477 Set_Generic_Parent (Act_Spec, Gen_Unit);
3479 Make_Subprogram_Declaration (Sloc (Act_Spec),
3480 Specification => Act_Spec);
3482 Set_Categorization_From_Pragmas (Act_Decl);
3484 if Parent_Installed then
3488 Append (Act_Decl, Renaming_List);
3489 Analyze_Instance_And_Renamings;
3491 -- If the generic is marked Import (Intrinsic), then so is the
3492 -- instance. This indicates that there is no body to instantiate.
3493 -- If generic is marked inline, so it the instance, and the
3494 -- anonymous subprogram it renames. If inlined, or else if inlining
3495 -- is enabled for the compilation, we generate the instance body
3496 -- even if it is not within the main unit.
3498 -- Any other pragmas might also be inherited ???
3500 if Is_Intrinsic_Subprogram (Gen_Unit) then
3501 Set_Is_Intrinsic_Subprogram (Anon_Id);
3502 Set_Is_Intrinsic_Subprogram (Act_Decl_Id);
3504 if Chars (Gen_Unit) = Name_Unchecked_Conversion then
3505 Validate_Unchecked_Conversion (N, Act_Decl_Id);
3509 Generate_Definition (Act_Decl_Id);
3511 Set_Is_Inlined (Act_Decl_Id, Is_Inlined (Gen_Unit));
3512 Set_Is_Inlined (Anon_Id, Is_Inlined (Gen_Unit));
3514 if not Is_Intrinsic_Subprogram (Gen_Unit) then
3515 Check_Elab_Instantiation (N);
3518 if Is_Dispatching_Operation (Act_Decl_Id)
3519 and then Ada_Version >= Ada_05
3525 Formal := First_Formal (Act_Decl_Id);
3526 while Present (Formal) loop
3527 if Ekind (Etype (Formal)) = E_Anonymous_Access_Type
3528 and then Is_Controlling_Formal (Formal)
3529 and then not Can_Never_Be_Null (Formal)
3531 Error_Msg_NE ("access parameter& is controlling,",
3533 Error_Msg_NE ("\corresponding parameter of & must be"
3534 & " explicitly null-excluding", N, Gen_Id);
3537 Next_Formal (Formal);
3542 Check_Hidden_Child_Unit (N, Gen_Unit, Act_Decl_Id);
3544 -- Subject to change, pending on if other pragmas are inherited ???
3546 Validate_Categorization_Dependency (N, Act_Decl_Id);
3548 if not Is_Intrinsic_Subprogram (Act_Decl_Id) then
3549 if not Generic_Separately_Compiled (Gen_Unit) then
3550 Inherit_Context (Gen_Decl, N);
3553 Restore_Private_Views (Pack_Id, False);
3555 -- If the context requires a full instantiation, mark node for
3556 -- subsequent construction of the body.
3558 if (Is_In_Main_Unit (N)
3559 or else Is_Inlined (Act_Decl_Id))
3560 and then (Operating_Mode = Generate_Code
3561 or else (Operating_Mode = Check_Semantics
3562 and then ASIS_Mode))
3563 and then (Expander_Active or else ASIS_Mode)
3564 and then not ABE_Is_Certain (N)
3565 and then not Is_Eliminated (Act_Decl_Id)
3567 Pending_Instantiations.Increment_Last;
3568 Pending_Instantiations.Table (Pending_Instantiations.Last) :=
3569 (N, Act_Decl, Expander_Active, Current_Sem_Unit);
3570 Check_Forward_Instantiation (Gen_Decl);
3572 -- The wrapper package is always delayed, because it does
3573 -- not constitute a freeze point, but to insure that the
3574 -- freeze node is placed properly, it is created directly
3575 -- when instantiating the body (otherwise the freeze node
3576 -- might appear to early for nested instantiations).
3578 elsif Nkind (Parent (N)) = N_Compilation_Unit then
3580 -- For ASIS purposes, indicate that the wrapper package has
3581 -- replaced the instantiation node.
3583 Rewrite (N, Unit (Parent (N)));
3584 Set_Unit (Parent (N), N);
3587 elsif Nkind (Parent (N)) = N_Compilation_Unit then
3589 -- Replace instance node for library-level instantiations
3590 -- of intrinsic subprograms, for ASIS use.
3592 Rewrite (N, Unit (Parent (N)));
3593 Set_Unit (Parent (N), N);
3596 if Parent_Installed then
3601 Generic_Renamings.Set_Last (0);
3602 Generic_Renamings_HTable.Reset;
3606 when Instantiation_Error =>
3607 if Parent_Installed then
3610 end Analyze_Subprogram_Instantiation;
3612 -------------------------
3613 -- Get_Associated_Node --
3614 -------------------------
3616 function Get_Associated_Node (N : Node_Id) return Node_Id is
3617 Assoc : Node_Id := Associated_Node (N);
3620 if Nkind (Assoc) /= Nkind (N) then
3623 elsif Nkind (Assoc) = N_Aggregate
3624 or else Nkind (Assoc) = N_Extension_Aggregate
3629 -- If the node is part of an inner generic, it may itself have been
3630 -- remapped into a further generic copy. Associated_Node is otherwise
3631 -- used for the entity of the node, and will be of a different node
3632 -- kind, or else N has been rewritten as a literal or function call.
3634 while Present (Associated_Node (Assoc))
3635 and then Nkind (Associated_Node (Assoc)) = Nkind (Assoc)
3637 Assoc := Associated_Node (Assoc);
3640 -- Follow and additional link in case the final node was rewritten.
3641 -- This can only happen with nested generic units.
3643 if (Nkind (Assoc) = N_Identifier or else Nkind (Assoc) in N_Op)
3644 and then Present (Associated_Node (Assoc))
3645 and then (Nkind (Associated_Node (Assoc)) = N_Function_Call
3647 Nkind (Associated_Node (Assoc)) = N_Explicit_Dereference
3649 Nkind (Associated_Node (Assoc)) = N_Integer_Literal
3651 Nkind (Associated_Node (Assoc)) = N_Real_Literal
3653 Nkind (Associated_Node (Assoc)) = N_String_Literal)
3655 Assoc := Associated_Node (Assoc);
3660 end Get_Associated_Node;
3662 -------------------------------------------
3663 -- Build_Instance_Compilation_Unit_Nodes --
3664 -------------------------------------------
3666 procedure Build_Instance_Compilation_Unit_Nodes
3671 Decl_Cunit : Node_Id;
3672 Body_Cunit : Node_Id;
3674 New_Main : constant Entity_Id := Defining_Entity (Act_Decl);
3675 Old_Main : constant Entity_Id := Cunit_Entity (Main_Unit);
3678 -- A new compilation unit node is built for the instance declaration
3681 Make_Compilation_Unit (Sloc (N),
3682 Context_Items => Empty_List,
3685 Make_Compilation_Unit_Aux (Sloc (N)));
3687 Set_Parent_Spec (Act_Decl, Parent_Spec (N));
3688 Set_Body_Required (Decl_Cunit, True);
3690 -- We use the original instantiation compilation unit as the resulting
3691 -- compilation unit of the instance, since this is the main unit.
3693 Rewrite (N, Act_Body);
3694 Body_Cunit := Parent (N);
3696 -- The two compilation unit nodes are linked by the Library_Unit field
3698 Set_Library_Unit (Decl_Cunit, Body_Cunit);
3699 Set_Library_Unit (Body_Cunit, Decl_Cunit);
3701 -- Preserve the private nature of the package if needed
3703 Set_Private_Present (Decl_Cunit, Private_Present (Body_Cunit));
3705 -- If the instance is not the main unit, its context, categorization,
3706 -- and elaboration entity are not relevant to the compilation.
3708 if Parent (N) /= Cunit (Main_Unit) then
3712 -- The context clause items on the instantiation, which are now
3713 -- attached to the body compilation unit (since the body overwrote
3714 -- the original instantiation node), semantically belong on the spec,
3715 -- so copy them there. It's harmless to leave them on the body as well.
3716 -- In fact one could argue that they belong in both places.
3718 Citem := First (Context_Items (Body_Cunit));
3719 while Present (Citem) loop
3720 Append (New_Copy (Citem), Context_Items (Decl_Cunit));
3724 -- Propagate categorization flags on packages, so that they appear
3725 -- in ali file for the spec of the unit.
3727 if Ekind (New_Main) = E_Package then
3728 Set_Is_Pure (Old_Main, Is_Pure (New_Main));
3729 Set_Is_Preelaborated (Old_Main, Is_Preelaborated (New_Main));
3730 Set_Is_Remote_Types (Old_Main, Is_Remote_Types (New_Main));
3731 Set_Is_Shared_Passive (Old_Main, Is_Shared_Passive (New_Main));
3732 Set_Is_Remote_Call_Interface
3733 (Old_Main, Is_Remote_Call_Interface (New_Main));
3736 -- Make entry in Units table, so that binder can generate call to
3737 -- elaboration procedure for body, if any.
3739 Make_Instance_Unit (Body_Cunit);
3740 Main_Unit_Entity := New_Main;
3741 Set_Cunit_Entity (Main_Unit, Main_Unit_Entity);
3743 -- Build elaboration entity, since the instance may certainly
3744 -- generate elaboration code requiring a flag for protection.
3746 Build_Elaboration_Entity (Decl_Cunit, New_Main);
3747 end Build_Instance_Compilation_Unit_Nodes;
3749 -----------------------------------
3750 -- Check_Formal_Package_Instance --
3751 -----------------------------------
3753 -- If the formal has specific parameters, they must match those of the
3754 -- actual. Both of them are instances, and the renaming declarations
3755 -- for their formal parameters appear in the same order in both. The
3756 -- analyzed formal has been analyzed in the context of the current
3759 procedure Check_Formal_Package_Instance
3760 (Formal_Pack : Entity_Id;
3761 Actual_Pack : Entity_Id)
3763 E1 : Entity_Id := First_Entity (Actual_Pack);
3764 E2 : Entity_Id := First_Entity (Formal_Pack);
3769 procedure Check_Mismatch (B : Boolean);
3770 -- Common error routine for mismatch between the parameters of
3771 -- the actual instance and those of the formal package.
3773 function Same_Instantiated_Constant (E1, E2 : Entity_Id) return Boolean;
3774 -- The formal may come from a nested formal package, and the actual
3775 -- may have been constant-folded. To determine whether the two denote
3776 -- the same entity we may have to traverse several definitions to
3777 -- recover the ultimate entity that they refer to.
3779 function Same_Instantiated_Variable (E1, E2 : Entity_Id) return Boolean;
3780 -- Similarly, if the formal comes from a nested formal package, the
3781 -- actual may designate the formal through multiple renamings, which
3782 -- have to be followed to determine the original variable in question.
3784 --------------------
3785 -- Check_Mismatch --
3786 --------------------
3788 procedure Check_Mismatch (B : Boolean) is
3792 ("actual for & in actual instance does not match formal",
3793 Parent (Actual_Pack), E1);
3797 --------------------------------
3798 -- Same_Instantiated_Constant --
3799 --------------------------------
3801 function Same_Instantiated_Constant
3802 (E1, E2 : Entity_Id) return Boolean
3808 while Present (Ent) loop
3812 elsif Ekind (Ent) /= E_Constant then
3815 elsif Is_Entity_Name (Constant_Value (Ent)) then
3816 if Entity (Constant_Value (Ent)) = E1 then
3819 Ent := Entity (Constant_Value (Ent));
3822 -- The actual may be a constant that has been folded. Recover
3825 elsif Is_Entity_Name (Original_Node (Constant_Value (Ent))) then
3826 Ent := Entity (Original_Node (Constant_Value (Ent)));
3833 end Same_Instantiated_Constant;
3835 --------------------------------
3836 -- Same_Instantiated_Variable --
3837 --------------------------------
3839 function Same_Instantiated_Variable
3840 (E1, E2 : Entity_Id) return Boolean
3842 function Original_Entity (E : Entity_Id) return Entity_Id;
3843 -- Follow chain of renamings to the ultimate ancestor
3845 ---------------------
3846 -- Original_Entity --
3847 ---------------------
3849 function Original_Entity (E : Entity_Id) return Entity_Id is
3854 while Nkind (Parent (Orig)) = N_Object_Renaming_Declaration
3855 and then Present (Renamed_Object (Orig))
3856 and then Is_Entity_Name (Renamed_Object (Orig))
3858 Orig := Entity (Renamed_Object (Orig));
3862 end Original_Entity;
3864 -- Start of processing for Same_Instantiated_Variable
3867 return Ekind (E1) = Ekind (E2)
3868 and then Original_Entity (E1) = Original_Entity (E2);
3869 end Same_Instantiated_Variable;
3871 -- Start of processing for Check_Formal_Package_Instance
3875 and then Present (E2)
3877 exit when Ekind (E1) = E_Package
3878 and then Renamed_Entity (E1) = Renamed_Entity (Actual_Pack);
3880 if Is_Type (E1) then
3882 -- Subtypes must statically match. E1 and E2 are the
3883 -- local entities that are subtypes of the actuals.
3884 -- Itypes generated for other parameters need not be checked,
3885 -- the check will be performed on the parameters themselves.
3887 if not Is_Itype (E1)
3888 and then not Is_Itype (E2)
3892 or else Etype (E1) /= Etype (E2)
3893 or else not Subtypes_Statically_Match (E1, E2));
3896 elsif Ekind (E1) = E_Constant then
3898 -- IN parameters must denote the same static value, or
3899 -- the same constant, or the literal null.
3901 Expr1 := Expression (Parent (E1));
3903 if Ekind (E2) /= E_Constant then
3904 Check_Mismatch (True);
3907 Expr2 := Expression (Parent (E2));
3910 if Is_Static_Expression (Expr1) then
3912 if not Is_Static_Expression (Expr2) then
3913 Check_Mismatch (True);
3915 elsif Is_Integer_Type (Etype (E1)) then
3918 V1 : constant Uint := Expr_Value (Expr1);
3919 V2 : constant Uint := Expr_Value (Expr2);
3921 Check_Mismatch (V1 /= V2);
3924 elsif Is_Real_Type (Etype (E1)) then
3926 V1 : constant Ureal := Expr_Value_R (Expr1);
3927 V2 : constant Ureal := Expr_Value_R (Expr2);
3929 Check_Mismatch (V1 /= V2);
3932 elsif Is_String_Type (Etype (E1))
3933 and then Nkind (Expr1) = N_String_Literal
3936 if Nkind (Expr2) /= N_String_Literal then
3937 Check_Mismatch (True);
3940 (not String_Equal (Strval (Expr1), Strval (Expr2)));
3944 elsif Is_Entity_Name (Expr1) then
3945 if Is_Entity_Name (Expr2) then
3946 if Entity (Expr1) = Entity (Expr2) then
3950 (not Same_Instantiated_Constant
3951 (Entity (Expr1), Entity (Expr2)));
3954 Check_Mismatch (True);
3957 elsif Is_Entity_Name (Original_Node (Expr1))
3958 and then Is_Entity_Name (Expr2)
3960 Same_Instantiated_Constant
3961 (Entity (Original_Node (Expr1)), Entity (Expr2))
3965 elsif Nkind (Expr1) = N_Null then
3966 Check_Mismatch (Nkind (Expr1) /= N_Null);
3969 Check_Mismatch (True);
3972 elsif Ekind (E1) = E_Variable then
3973 Check_Mismatch (not Same_Instantiated_Variable (E1, E2));
3975 elsif Ekind (E1) = E_Package then
3977 (Ekind (E1) /= Ekind (E2)
3978 or else Renamed_Object (E1) /= Renamed_Object (E2));
3980 elsif Is_Overloadable (E1) then
3982 -- Verify that the names of the entities match.
3983 -- What if actual is an attribute ???
3986 (Ekind (E2) /= Ekind (E1) or else (Alias (E1)) /= Alias (E2));
3989 raise Program_Error;
3996 end Check_Formal_Package_Instance;
3998 ---------------------------
3999 -- Check_Formal_Packages --
4000 ---------------------------
4002 procedure Check_Formal_Packages (P_Id : Entity_Id) is
4004 Formal_P : Entity_Id;
4007 -- Iterate through the declarations in the instance, looking for
4008 -- package renaming declarations that denote instances of formal
4009 -- packages. Stop when we find the renaming of the current package
4010 -- itself. The declaration for a formal package without a box is
4011 -- followed by an internal entity that repeats the instantiation.
4013 E := First_Entity (P_Id);
4014 while Present (E) loop
4015 if Ekind (E) = E_Package then
4016 if Renamed_Object (E) = P_Id then
4019 elsif Nkind (Parent (E)) /= N_Package_Renaming_Declaration then
4022 elsif not Box_Present (Parent (Associated_Formal_Package (E))) then
4023 Formal_P := Next_Entity (E);
4024 Check_Formal_Package_Instance (Formal_P, E);
4030 end Check_Formal_Packages;
4032 ---------------------------------
4033 -- Check_Forward_Instantiation --
4034 ---------------------------------
4036 procedure Check_Forward_Instantiation (Decl : Node_Id) is
4038 Gen_Comp : Entity_Id := Cunit_Entity (Get_Source_Unit (Decl));
4041 -- The instantiation appears before the generic body if we are in the
4042 -- scope of the unit containing the generic, either in its spec or in
4043 -- the package body. and before the generic body.
4045 if Ekind (Gen_Comp) = E_Package_Body then
4046 Gen_Comp := Spec_Entity (Gen_Comp);
4049 if In_Open_Scopes (Gen_Comp)
4050 and then No (Corresponding_Body (Decl))
4055 and then not Is_Compilation_Unit (S)
4056 and then not Is_Child_Unit (S)
4058 if Ekind (S) = E_Package then
4059 Set_Has_Forward_Instantiation (S);
4065 end Check_Forward_Instantiation;
4067 ---------------------------
4068 -- Check_Generic_Actuals --
4069 ---------------------------
4071 -- The visibility of the actuals may be different between the
4072 -- point of generic instantiation and the instantiation of the body.
4074 procedure Check_Generic_Actuals
4075 (Instance : Entity_Id;
4076 Is_Formal_Box : Boolean)
4081 function Denotes_Previous_Actual (Typ : Entity_Id) return Boolean;
4082 -- For a formal that is an array type, the component type is often
4083 -- a previous formal in the same unit. The privacy status of the
4084 -- component type will have been examined earlier in the traversal
4085 -- of the corresponding actuals, and this status should not be
4086 -- modified for the array type itself.
4087 -- To detect this case we have to rescan the list of formals, which
4088 -- is usually short enough to ignore the resulting inefficiency.
4090 function Denotes_Previous_Actual (Typ : Entity_Id) return Boolean is
4093 Prev := First_Entity (Instance);
4094 while Present (Prev) loop
4096 and then Nkind (Parent (Prev)) = N_Subtype_Declaration
4097 and then Is_Entity_Name (Subtype_Indication (Parent (Prev)))
4098 and then Entity (Subtype_Indication (Parent (Prev))) = Typ
4108 end Denotes_Previous_Actual;
4110 -- Start of processing for Check_Generic_Actuals
4113 E := First_Entity (Instance);
4114 while Present (E) loop
4116 and then Nkind (Parent (E)) = N_Subtype_Declaration
4117 and then Scope (Etype (E)) /= Instance
4118 and then Is_Entity_Name (Subtype_Indication (Parent (E)))
4120 if Is_Array_Type (E)
4121 and then Denotes_Previous_Actual (Component_Type (E))
4125 Check_Private_View (Subtype_Indication (Parent (E)));
4127 Set_Is_Generic_Actual_Type (E, True);
4128 Set_Is_Hidden (E, False);
4129 Set_Is_Potentially_Use_Visible (E,
4132 -- We constructed the generic actual type as a subtype of
4133 -- the supplied type. This means that it normally would not
4134 -- inherit subtype specific attributes of the actual, which
4135 -- is wrong for the generic case.
4137 Astype := Ancestor_Subtype (E);
4141 -- can happen when E is an itype that is the full view of
4142 -- a private type completed, e.g. with a constrained array.
4144 Astype := Base_Type (E);
4147 Set_Size_Info (E, (Astype));
4148 Set_RM_Size (E, RM_Size (Astype));
4149 Set_First_Rep_Item (E, First_Rep_Item (Astype));
4151 if Is_Discrete_Or_Fixed_Point_Type (E) then
4152 Set_RM_Size (E, RM_Size (Astype));
4154 -- In nested instances, the base type of an access actual
4155 -- may itself be private, and need to be exchanged.
4157 elsif Is_Access_Type (E)
4158 and then Is_Private_Type (Etype (E))
4161 (New_Occurrence_Of (Etype (E), Sloc (Instance)));
4164 elsif Ekind (E) = E_Package then
4166 -- If this is the renaming for the current instance, we're done.
4167 -- Otherwise it is a formal package. If the corresponding formal
4168 -- was declared with a box, the (instantiations of the) generic
4169 -- formal part are also visible. Otherwise, ignore the entity
4170 -- created to validate the actuals.
4172 if Renamed_Object (E) = Instance then
4175 elsif Nkind (Parent (E)) /= N_Package_Renaming_Declaration then
4178 -- The visibility of a formal of an enclosing generic is already
4181 elsif Denotes_Formal_Package (E) then
4184 elsif Present (Associated_Formal_Package (E)) then
4185 if Box_Present (Parent (Associated_Formal_Package (E))) then
4186 Check_Generic_Actuals (Renamed_Object (E), True);
4189 Set_Is_Hidden (E, False);
4192 -- If this is a subprogram instance (in a wrapper package) the
4193 -- actual is fully visible.
4195 elsif Is_Wrapper_Package (Instance) then
4196 Set_Is_Hidden (E, False);
4199 Set_Is_Hidden (E, not Is_Formal_Box);
4204 end Check_Generic_Actuals;
4206 ------------------------------
4207 -- Check_Generic_Child_Unit --
4208 ------------------------------
4210 procedure Check_Generic_Child_Unit
4212 Parent_Installed : in out Boolean)
4214 Loc : constant Source_Ptr := Sloc (Gen_Id);
4215 Gen_Par : Entity_Id := Empty;
4216 Inst_Par : Entity_Id;
4220 function Find_Generic_Child
4222 Id : Node_Id) return Entity_Id;
4223 -- Search generic parent for possible child unit with the given name
4225 function In_Enclosing_Instance return Boolean;
4226 -- Within an instance of the parent, the child unit may be denoted
4227 -- by a simple name, or an abbreviated expanded name. Examine enclosing
4228 -- scopes to locate a possible parent instantiation.
4230 ------------------------
4231 -- Find_Generic_Child --
4232 ------------------------
4234 function Find_Generic_Child
4236 Id : Node_Id) return Entity_Id
4241 -- If entity of name is already set, instance has already been
4242 -- resolved, e.g. in an enclosing instantiation.
4244 if Present (Entity (Id)) then
4245 if Scope (Entity (Id)) = Scop then
4252 E := First_Entity (Scop);
4253 while Present (E) loop
4254 if Chars (E) = Chars (Id)
4255 and then Is_Child_Unit (E)
4257 if Is_Child_Unit (E)
4258 and then not Is_Visible_Child_Unit (E)
4261 ("generic child unit& is not visible", Gen_Id, E);
4273 end Find_Generic_Child;
4275 ---------------------------
4276 -- In_Enclosing_Instance --
4277 ---------------------------
4279 function In_Enclosing_Instance return Boolean is
4280 Enclosing_Instance : Node_Id;
4281 Instance_Decl : Node_Id;
4284 Enclosing_Instance := Current_Scope;
4286 while Present (Enclosing_Instance) loop
4287 Instance_Decl := Unit_Declaration_Node (Enclosing_Instance);
4289 if Ekind (Enclosing_Instance) = E_Package
4290 and then Is_Generic_Instance (Enclosing_Instance)
4292 (Generic_Parent (Specification (Instance_Decl)))
4294 -- Check whether the generic we are looking for is a child
4295 -- of this instance.
4297 E := Find_Generic_Child
4298 (Generic_Parent (Specification (Instance_Decl)), Gen_Id);
4299 exit when Present (E);
4305 Enclosing_Instance := Scope (Enclosing_Instance);
4317 Make_Expanded_Name (Loc,
4319 Prefix => New_Occurrence_Of (Enclosing_Instance, Loc),
4320 Selector_Name => New_Occurrence_Of (E, Loc)));
4322 Set_Entity (Gen_Id, E);
4323 Set_Etype (Gen_Id, Etype (E));
4324 Parent_Installed := False; -- Already in scope.
4327 end In_Enclosing_Instance;
4329 -- Start of processing for Check_Generic_Child_Unit
4332 -- If the name of the generic is given by a selected component, it
4333 -- may be the name of a generic child unit, and the prefix is the name
4334 -- of an instance of the parent, in which case the child unit must be
4335 -- visible. If this instance is not in scope, it must be placed there
4336 -- and removed after instantiation, because what is being instantiated
4337 -- is not the original child, but the corresponding child present in
4338 -- the instance of the parent.
4340 -- If the child is instantiated within the parent, it can be given by
4341 -- a simple name. In this case the instance is already in scope, but
4342 -- the child generic must be recovered from the generic parent as well.
4344 if Nkind (Gen_Id) = N_Selected_Component then
4345 S := Selector_Name (Gen_Id);
4346 Analyze (Prefix (Gen_Id));
4347 Inst_Par := Entity (Prefix (Gen_Id));
4349 if Ekind (Inst_Par) = E_Package
4350 and then Present (Renamed_Object (Inst_Par))
4352 Inst_Par := Renamed_Object (Inst_Par);
4355 if Ekind (Inst_Par) = E_Package then
4356 if Nkind (Parent (Inst_Par)) = N_Package_Specification then
4357 Gen_Par := Generic_Parent (Parent (Inst_Par));
4359 elsif Nkind (Parent (Inst_Par)) = N_Defining_Program_Unit_Name
4361 Nkind (Parent (Parent (Inst_Par))) = N_Package_Specification
4363 Gen_Par := Generic_Parent (Parent (Parent (Inst_Par)));
4366 elsif Ekind (Inst_Par) = E_Generic_Package
4367 and then Nkind (Parent (Gen_Id)) = N_Formal_Package_Declaration
4369 -- A formal package may be a real child package, and not the
4370 -- implicit instance within a parent. In this case the child is
4371 -- not visible and has to be retrieved explicitly as well.
4373 Gen_Par := Inst_Par;
4376 if Present (Gen_Par) then
4378 -- The prefix denotes an instantiation. The entity itself
4379 -- may be a nested generic, or a child unit.
4381 E := Find_Generic_Child (Gen_Par, S);
4384 Change_Selected_Component_To_Expanded_Name (Gen_Id);
4385 Set_Entity (Gen_Id, E);
4386 Set_Etype (Gen_Id, Etype (E));
4388 Set_Etype (S, Etype (E));
4390 -- Indicate that this is a reference to the parent
4392 if In_Extended_Main_Source_Unit (Gen_Id) then
4393 Set_Is_Instantiated (Inst_Par);
4396 -- A common mistake is to replicate the naming scheme of
4397 -- a hierarchy by instantiating a generic child directly,
4398 -- rather than the implicit child in a parent instance:
4400 -- generic .. package Gpar is ..
4401 -- generic .. package Gpar.Child is ..
4402 -- package Par is new Gpar ();
4405 -- package Par.Child is new Gpar.Child ();
4406 -- rather than Par.Child
4408 -- In this case the instantiation is within Par, which is
4409 -- an instance, but Gpar does not denote Par because we are
4410 -- not IN the instance of Gpar, so this is illegal. The test
4411 -- below recognizes this particular case.
4413 if Is_Child_Unit (E)
4414 and then not Comes_From_Source (Entity (Prefix (Gen_Id)))
4415 and then (not In_Instance
4416 or else Nkind (Parent (Parent (Gen_Id))) =
4420 ("prefix of generic child unit must be instance of parent",
4424 if not In_Open_Scopes (Inst_Par)
4425 and then Nkind (Parent (Gen_Id)) not in
4426 N_Generic_Renaming_Declaration
4428 Install_Parent (Inst_Par);
4429 Parent_Installed := True;
4433 -- If the generic parent does not contain an entity that
4434 -- corresponds to the selector, the instance doesn't either.
4435 -- Analyzing the node will yield the appropriate error message.
4436 -- If the entity is not a child unit, then it is an inner
4437 -- generic in the parent.
4445 if Is_Child_Unit (Entity (Gen_Id))
4447 Nkind (Parent (Gen_Id)) not in N_Generic_Renaming_Declaration
4448 and then not In_Open_Scopes (Inst_Par)
4450 Install_Parent (Inst_Par);
4451 Parent_Installed := True;
4455 elsif Nkind (Gen_Id) = N_Expanded_Name then
4457 -- Entity already present, analyze prefix, whose meaning may be
4458 -- an instance in the current context. If it is an instance of
4459 -- a relative within another, the proper parent may still have
4460 -- to be installed, if they are not of the same generation.
4462 Analyze (Prefix (Gen_Id));
4463 Inst_Par := Entity (Prefix (Gen_Id));
4465 if In_Enclosing_Instance then
4468 elsif Present (Entity (Gen_Id))
4469 and then Is_Child_Unit (Entity (Gen_Id))
4470 and then not In_Open_Scopes (Inst_Par)
4472 Install_Parent (Inst_Par);
4473 Parent_Installed := True;
4476 elsif In_Enclosing_Instance then
4478 -- The child unit is found in some enclosing scope
4485 -- If this is the renaming of the implicit child in a parent
4486 -- instance, recover the parent name and install it.
4488 if Is_Entity_Name (Gen_Id) then
4489 E := Entity (Gen_Id);
4491 if Is_Generic_Unit (E)
4492 and then Nkind (Parent (E)) in N_Generic_Renaming_Declaration
4493 and then Is_Child_Unit (Renamed_Object (E))
4494 and then Is_Generic_Unit (Scope (Renamed_Object (E)))
4495 and then Nkind (Name (Parent (E))) = N_Expanded_Name
4498 New_Copy_Tree (Name (Parent (E))));
4499 Inst_Par := Entity (Prefix (Gen_Id));
4501 if not In_Open_Scopes (Inst_Par) then
4502 Install_Parent (Inst_Par);
4503 Parent_Installed := True;
4506 -- If it is a child unit of a non-generic parent, it may be
4507 -- use-visible and given by a direct name. Install parent as
4510 elsif Is_Generic_Unit (E)
4511 and then Is_Child_Unit (E)
4513 Nkind (Parent (Gen_Id)) not in N_Generic_Renaming_Declaration
4514 and then not Is_Generic_Unit (Scope (E))
4516 if not In_Open_Scopes (Scope (E)) then
4517 Install_Parent (Scope (E));
4518 Parent_Installed := True;
4523 end Check_Generic_Child_Unit;
4525 -----------------------------
4526 -- Check_Hidden_Child_Unit --
4527 -----------------------------
4529 procedure Check_Hidden_Child_Unit
4531 Gen_Unit : Entity_Id;
4532 Act_Decl_Id : Entity_Id)
4534 Gen_Id : constant Node_Id := Name (N);
4537 if Is_Child_Unit (Gen_Unit)
4538 and then Is_Child_Unit (Act_Decl_Id)
4539 and then Nkind (Gen_Id) = N_Expanded_Name
4540 and then Entity (Prefix (Gen_Id)) = Scope (Act_Decl_Id)
4541 and then Chars (Gen_Unit) = Chars (Act_Decl_Id)
4543 Error_Msg_Node_2 := Scope (Act_Decl_Id);
4545 ("generic unit & is implicitly declared in &",
4546 Defining_Unit_Name (N), Gen_Unit);
4547 Error_Msg_N ("\instance must have different name",
4548 Defining_Unit_Name (N));
4550 end Check_Hidden_Child_Unit;
4552 ------------------------
4553 -- Check_Private_View --
4554 ------------------------
4556 procedure Check_Private_View (N : Node_Id) is
4557 T : constant Entity_Id := Etype (N);
4561 -- Exchange views if the type was not private in the generic but is
4562 -- private at the point of instantiation. Do not exchange views if
4563 -- the scope of the type is in scope. This can happen if both generic
4564 -- and instance are sibling units, or if type is defined in a parent.
4565 -- In this case the visibility of the type will be correct for all
4569 BT := Base_Type (T);
4571 if Is_Private_Type (T)
4572 and then not Has_Private_View (N)
4573 and then Present (Full_View (T))
4574 and then not In_Open_Scopes (Scope (T))
4576 -- In the generic, the full type was visible. Save the
4577 -- private entity, for subsequent exchange.
4581 elsif Has_Private_View (N)
4582 and then not Is_Private_Type (T)
4583 and then not Has_Been_Exchanged (T)
4584 and then Etype (Get_Associated_Node (N)) /= T
4586 -- Only the private declaration was visible in the generic. If
4587 -- the type appears in a subtype declaration, the subtype in the
4588 -- instance must have a view compatible with that of its parent,
4589 -- which must be exchanged (see corresponding code in Restore_
4590 -- Private_Views). Otherwise, if the type is defined in a parent
4591 -- unit, leave full visibility within instance, which is safe.
4593 if In_Open_Scopes (Scope (Base_Type (T)))
4594 and then not Is_Private_Type (Base_Type (T))
4595 and then Comes_From_Source (Base_Type (T))
4599 elsif Nkind (Parent (N)) = N_Subtype_Declaration
4600 or else not In_Private_Part (Scope (Base_Type (T)))
4602 Append_Elmt (T, Exchanged_Views);
4603 Exchange_Declarations (Etype (Get_Associated_Node (N)));
4606 -- For composite types with inconsistent representation
4607 -- exchange component types accordingly.
4609 elsif Is_Access_Type (T)
4610 and then Is_Private_Type (Designated_Type (T))
4611 and then not Has_Private_View (N)
4612 and then Present (Full_View (Designated_Type (T)))
4614 Switch_View (Designated_Type (T));
4616 elsif Is_Array_Type (T)
4617 and then Is_Private_Type (Component_Type (T))
4618 and then not Has_Private_View (N)
4619 and then Present (Full_View (Component_Type (T)))
4621 Switch_View (Component_Type (T));
4623 elsif Is_Private_Type (T)
4624 and then Present (Full_View (T))
4625 and then Is_Array_Type (Full_View (T))
4626 and then Is_Private_Type (Component_Type (Full_View (T)))
4630 -- Finally, a non-private subtype may have a private base type,
4631 -- which must be exchanged for consistency. This can happen when
4632 -- instantiating a package body, when the scope stack is empty
4633 -- but in fact the subtype and the base type are declared in an
4636 elsif not Is_Private_Type (T)
4637 and then not Has_Private_View (N)
4638 and then Is_Private_Type (Base_Type (T))
4639 and then Present (Full_View (BT))
4640 and then not Is_Generic_Type (BT)
4641 and then not In_Open_Scopes (BT)
4643 Append_Elmt (Full_View (BT), Exchanged_Views);
4644 Exchange_Declarations (BT);
4647 end Check_Private_View;
4649 --------------------------
4650 -- Contains_Instance_Of --
4651 --------------------------
4653 function Contains_Instance_Of
4656 N : Node_Id) return Boolean
4664 -- Verify that there are no circular instantiations. We check whether
4665 -- the unit contains an instance of the current scope or some enclosing
4666 -- scope (in case one of the instances appears in a subunit). Longer
4667 -- circularities involving subunits might seem too pathological to
4668 -- consider, but they were not too pathological for the authors of
4669 -- DEC bc30vsq, so we loop over all enclosing scopes, and mark all
4670 -- enclosing generic scopes as containing an instance.
4673 -- Within a generic subprogram body, the scope is not generic, to
4674 -- allow for recursive subprograms. Use the declaration to determine
4675 -- whether this is a generic unit.
4677 if Ekind (Scop) = E_Generic_Package
4678 or else (Is_Subprogram (Scop)
4679 and then Nkind (Unit_Declaration_Node (Scop)) =
4680 N_Generic_Subprogram_Declaration)
4682 Elmt := First_Elmt (Inner_Instances (Inner));
4684 while Present (Elmt) loop
4685 if Node (Elmt) = Scop then
4686 Error_Msg_Node_2 := Inner;
4688 ("circular Instantiation: & instantiated within &!",
4692 elsif Node (Elmt) = Inner then
4695 elsif Contains_Instance_Of (Node (Elmt), Scop, N) then
4696 Error_Msg_Node_2 := Inner;
4698 ("circular Instantiation: & instantiated within &!",
4706 -- Indicate that Inner is being instantiated within Scop
4708 Append_Elmt (Inner, Inner_Instances (Scop));
4711 if Scop = Standard_Standard then
4714 Scop := Scope (Scop);
4719 end Contains_Instance_Of;
4721 -----------------------
4722 -- Copy_Generic_Node --
4723 -----------------------
4725 function Copy_Generic_Node
4727 Parent_Id : Node_Id;
4728 Instantiating : Boolean) return Node_Id
4733 function Copy_Generic_Descendant (D : Union_Id) return Union_Id;
4734 -- Check the given value of one of the Fields referenced by the
4735 -- current node to determine whether to copy it recursively. The
4736 -- field may hold a Node_Id, a List_Id, or an Elist_Id, or a plain
4737 -- value (Sloc, Uint, Char) in which case it need not be copied.
4739 procedure Copy_Descendants;
4740 -- Common utility for various nodes
4742 function Copy_Generic_Elist (E : Elist_Id) return Elist_Id;
4743 -- Make copy of element list
4745 function Copy_Generic_List
4747 Parent_Id : Node_Id) return List_Id;
4748 -- Apply Copy_Node recursively to the members of a node list
4750 function In_Defining_Unit_Name (Nam : Node_Id) return Boolean;
4751 -- True if an identifier is part of the defining program unit name
4752 -- of a child unit. The entity of such an identifier must be kept
4753 -- (for ASIS use) even though as the name of an enclosing generic
4754 -- it would otherwise not be preserved in the generic tree.
4756 ----------------------
4757 -- Copy_Descendants --
4758 ----------------------
4760 procedure Copy_Descendants is
4762 use Atree.Unchecked_Access;
4763 -- This code section is part of the implementation of an untyped
4764 -- tree traversal, so it needs direct access to node fields.
4767 Set_Field1 (New_N, Copy_Generic_Descendant (Field1 (N)));
4768 Set_Field2 (New_N, Copy_Generic_Descendant (Field2 (N)));
4769 Set_Field3 (New_N, Copy_Generic_Descendant (Field3 (N)));
4770 Set_Field4 (New_N, Copy_Generic_Descendant (Field4 (N)));
4771 Set_Field5 (New_N, Copy_Generic_Descendant (Field5 (N)));
4772 end Copy_Descendants;
4774 -----------------------------
4775 -- Copy_Generic_Descendant --
4776 -----------------------------
4778 function Copy_Generic_Descendant (D : Union_Id) return Union_Id is
4780 if D = Union_Id (Empty) then
4783 elsif D in Node_Range then
4785 (Copy_Generic_Node (Node_Id (D), New_N, Instantiating));
4787 elsif D in List_Range then
4788 return Union_Id (Copy_Generic_List (List_Id (D), New_N));
4790 elsif D in Elist_Range then
4791 return Union_Id (Copy_Generic_Elist (Elist_Id (D)));
4793 -- Nothing else is copyable (e.g. Uint values), return as is
4798 end Copy_Generic_Descendant;
4800 ------------------------
4801 -- Copy_Generic_Elist --
4802 ------------------------
4804 function Copy_Generic_Elist (E : Elist_Id) return Elist_Id is
4811 M := First_Elmt (E);
4812 while Present (M) loop
4814 (Copy_Generic_Node (Node (M), Empty, Instantiating), L);
4823 end Copy_Generic_Elist;
4825 -----------------------
4826 -- Copy_Generic_List --
4827 -----------------------
4829 function Copy_Generic_List
4831 Parent_Id : Node_Id) return List_Id
4839 Set_Parent (New_L, Parent_Id);
4842 while Present (N) loop
4843 Append (Copy_Generic_Node (N, Empty, Instantiating), New_L);
4852 end Copy_Generic_List;
4854 ---------------------------
4855 -- In_Defining_Unit_Name --
4856 ---------------------------
4858 function In_Defining_Unit_Name (Nam : Node_Id) return Boolean is
4860 return Present (Parent (Nam))
4861 and then (Nkind (Parent (Nam)) = N_Defining_Program_Unit_Name
4863 (Nkind (Parent (Nam)) = N_Expanded_Name
4864 and then In_Defining_Unit_Name (Parent (Nam))));
4865 end In_Defining_Unit_Name;
4867 -- Start of processing for Copy_Generic_Node
4874 New_N := New_Copy (N);
4876 if Instantiating then
4877 Adjust_Instantiation_Sloc (New_N, S_Adjustment);
4880 if not Is_List_Member (N) then
4881 Set_Parent (New_N, Parent_Id);
4884 -- If defining identifier, then all fields have been copied already
4886 if Nkind (New_N) in N_Entity then
4889 -- Special casing for identifiers and other entity names and operators
4891 elsif Nkind (New_N) = N_Identifier
4892 or else Nkind (New_N) = N_Character_Literal
4893 or else Nkind (New_N) = N_Expanded_Name
4894 or else Nkind (New_N) = N_Operator_Symbol
4895 or else Nkind (New_N) in N_Op
4897 if not Instantiating then
4899 -- Link both nodes in order to assign subsequently the
4900 -- entity of the copy to the original node, in case this
4901 -- is a global reference.
4903 Set_Associated_Node (N, New_N);
4905 -- If we are within an instantiation, this is a nested generic
4906 -- that has already been analyzed at the point of definition. We
4907 -- must preserve references that were global to the enclosing
4908 -- parent at that point. Other occurrences, whether global or
4909 -- local to the current generic, must be resolved anew, so we
4910 -- reset the entity in the generic copy. A global reference has
4911 -- a smaller depth than the parent, or else the same depth in
4912 -- case both are distinct compilation units.
4914 -- It is also possible for Current_Instantiated_Parent to be
4915 -- defined, and for this not to be a nested generic, namely
4916 -- if the unit is loaded through Rtsfind. In that case, the
4917 -- entity of New_N is only a link to the associated node, and
4918 -- not a defining occurrence.
4920 -- The entities for parent units in the defining_program_unit
4921 -- of a generic child unit are established when the context of
4922 -- the unit is first analyzed, before the generic copy is made.
4923 -- They are preserved in the copy for use in ASIS queries.
4925 Ent := Entity (New_N);
4927 if No (Current_Instantiated_Parent.Gen_Id) then
4929 or else Nkind (Ent) /= N_Defining_Identifier
4930 or else not In_Defining_Unit_Name (N)
4932 Set_Associated_Node (New_N, Empty);
4937 not (Nkind (Ent) = N_Defining_Identifier
4939 Nkind (Ent) = N_Defining_Character_Literal
4941 Nkind (Ent) = N_Defining_Operator_Symbol)
4942 or else No (Scope (Ent))
4943 or else Scope (Ent) = Current_Instantiated_Parent.Gen_Id
4944 or else (Scope_Depth (Scope (Ent)) >
4945 Scope_Depth (Current_Instantiated_Parent.Gen_Id)
4947 Get_Source_Unit (Ent) =
4948 Get_Source_Unit (Current_Instantiated_Parent.Gen_Id))
4950 Set_Associated_Node (New_N, Empty);
4953 -- Case of instantiating identifier or some other name or operator
4956 -- If the associated node is still defined, the entity in
4957 -- it is global, and must be copied to the instance.
4958 -- If this copy is being made for a body to inline, it is
4959 -- applied to an instantiated tree, and the entity is already
4960 -- present and must be also preserved.
4963 Assoc : constant Node_Id := Get_Associated_Node (N);
4965 if Present (Assoc) then
4966 if Nkind (Assoc) = Nkind (N) then
4967 Set_Entity (New_N, Entity (Assoc));
4968 Check_Private_View (N);
4970 elsif Nkind (Assoc) = N_Function_Call then
4971 Set_Entity (New_N, Entity (Name (Assoc)));
4973 elsif (Nkind (Assoc) = N_Defining_Identifier
4974 or else Nkind (Assoc) = N_Defining_Character_Literal
4975 or else Nkind (Assoc) = N_Defining_Operator_Symbol)
4976 and then Expander_Active
4978 -- Inlining case: we are copying a tree that contains
4979 -- global entities, which are preserved in the copy
4980 -- to be used for subsequent inlining.
4985 Set_Entity (New_N, Empty);
4991 -- For expanded name, we must copy the Prefix and Selector_Name
4993 if Nkind (N) = N_Expanded_Name then
4995 (New_N, Copy_Generic_Node (Prefix (N), New_N, Instantiating));
4997 Set_Selector_Name (New_N,
4998 Copy_Generic_Node (Selector_Name (N), New_N, Instantiating));
5000 -- For operators, we must copy the right operand
5002 elsif Nkind (N) in N_Op then
5003 Set_Right_Opnd (New_N,
5004 Copy_Generic_Node (Right_Opnd (N), New_N, Instantiating));
5006 -- And for binary operators, the left operand as well
5008 if Nkind (N) in N_Binary_Op then
5009 Set_Left_Opnd (New_N,
5010 Copy_Generic_Node (Left_Opnd (N), New_N, Instantiating));
5014 -- Special casing for stubs
5016 elsif Nkind (N) in N_Body_Stub then
5018 -- In any case, we must copy the specification or defining
5019 -- identifier as appropriate.
5021 if Nkind (N) = N_Subprogram_Body_Stub then
5022 Set_Specification (New_N,
5023 Copy_Generic_Node (Specification (N), New_N, Instantiating));
5026 Set_Defining_Identifier (New_N,
5028 (Defining_Identifier (N), New_N, Instantiating));
5031 -- If we are not instantiating, then this is where we load and
5032 -- analyze subunits, i.e. at the point where the stub occurs. A
5033 -- more permissivle system might defer this analysis to the point
5034 -- of instantiation, but this seems to complicated for now.
5036 if not Instantiating then
5038 Subunit_Name : constant Unit_Name_Type := Get_Unit_Name (N);
5040 Unum : Unit_Number_Type;
5046 (Load_Name => Subunit_Name,
5051 -- If the proper body is not found, a warning message will
5052 -- be emitted when analyzing the stub, or later at the the
5053 -- point of instantiation. Here we just leave the stub as is.
5055 if Unum = No_Unit then
5056 Subunits_Missing := True;
5057 goto Subunit_Not_Found;
5060 Subunit := Cunit (Unum);
5062 if Nkind (Unit (Subunit)) /= N_Subunit then
5063 Error_Msg_Sloc := Sloc (N);
5065 ("expected SEPARATE subunit to complete stub at#,"
5066 & " found child unit", Subunit);
5067 goto Subunit_Not_Found;
5070 -- We must create a generic copy of the subunit, in order
5071 -- to perform semantic analysis on it, and we must replace
5072 -- the stub in the original generic unit with the subunit,
5073 -- in order to preserve non-local references within.
5075 -- Only the proper body needs to be copied. Library_Unit and
5076 -- context clause are simply inherited by the generic copy.
5077 -- Note that the copy (which may be recursive if there are
5078 -- nested subunits) must be done first, before attaching it
5079 -- to the enclosing generic.
5083 (Proper_Body (Unit (Subunit)),
5084 Empty, Instantiating => False);
5086 -- Now place the original proper body in the original
5087 -- generic unit. This is a body, not a compilation unit.
5089 Rewrite (N, Proper_Body (Unit (Subunit)));
5090 Set_Is_Compilation_Unit (Defining_Entity (N), False);
5091 Set_Was_Originally_Stub (N);
5093 -- Finally replace the body of the subunit with its copy,
5094 -- and make this new subunit into the library unit of the
5095 -- generic copy, which does not have stubs any longer.
5097 Set_Proper_Body (Unit (Subunit), New_Body);
5098 Set_Library_Unit (New_N, Subunit);
5099 Inherit_Context (Unit (Subunit), N);
5102 -- If we are instantiating, this must be an error case, since
5103 -- otherwise we would have replaced the stub node by the proper
5104 -- body that corresponds. So just ignore it in the copy (i.e.
5105 -- we have copied it, and that is good enough).
5111 <<Subunit_Not_Found>> null;
5113 -- If the node is a compilation unit, it is the subunit of a stub,
5114 -- which has been loaded already (see code below). In this case,
5115 -- the library unit field of N points to the parent unit (which
5116 -- is a compilation unit) and need not (and cannot!) be copied.
5118 -- When the proper body of the stub is analyzed, thie library_unit
5119 -- link is used to establish the proper context (see sem_ch10).
5121 -- The other fields of a compilation unit are copied as usual
5123 elsif Nkind (N) = N_Compilation_Unit then
5125 -- This code can only be executed when not instantiating, because
5126 -- in the copy made for an instantiation, the compilation unit
5127 -- node has disappeared at the point that a stub is replaced by
5130 pragma Assert (not Instantiating);
5132 Set_Context_Items (New_N,
5133 Copy_Generic_List (Context_Items (N), New_N));
5136 Copy_Generic_Node (Unit (N), New_N, False));
5138 Set_First_Inlined_Subprogram (New_N,
5140 (First_Inlined_Subprogram (N), New_N, False));
5142 Set_Aux_Decls_Node (New_N,
5143 Copy_Generic_Node (Aux_Decls_Node (N), New_N, False));
5145 -- For an assignment node, the assignment is known to be semantically
5146 -- legal if we are instantiating the template. This avoids incorrect
5147 -- diagnostics in generated code.
5149 elsif Nkind (N) = N_Assignment_Statement then
5151 -- Copy name and expression fields in usual manner
5154 Copy_Generic_Node (Name (N), New_N, Instantiating));
5156 Set_Expression (New_N,
5157 Copy_Generic_Node (Expression (N), New_N, Instantiating));
5159 if Instantiating then
5160 Set_Assignment_OK (Name (New_N), True);
5163 elsif Nkind (N) = N_Aggregate
5164 or else Nkind (N) = N_Extension_Aggregate
5167 if not Instantiating then
5168 Set_Associated_Node (N, New_N);
5171 if Present (Get_Associated_Node (N))
5172 and then Nkind (Get_Associated_Node (N)) = Nkind (N)
5174 -- In the generic the aggregate has some composite type. If at
5175 -- the point of instantiation the type has a private view,
5176 -- install the full view (and that of its ancestors, if any).
5179 T : Entity_Id := (Etype (Get_Associated_Node (New_N)));
5184 and then Is_Private_Type (T)
5190 and then Is_Tagged_Type (T)
5191 and then Is_Derived_Type (T)
5193 Rt := Root_Type (T);
5198 if Is_Private_Type (T) then
5209 -- Do not copy the associated node, which points to
5210 -- the generic copy of the aggregate.
5213 use Atree.Unchecked_Access;
5214 -- This code section is part of the implementation of an untyped
5215 -- tree traversal, so it needs direct access to node fields.
5218 Set_Field1 (New_N, Copy_Generic_Descendant (Field1 (N)));
5219 Set_Field2 (New_N, Copy_Generic_Descendant (Field2 (N)));
5220 Set_Field3 (New_N, Copy_Generic_Descendant (Field3 (N)));
5221 Set_Field5 (New_N, Copy_Generic_Descendant (Field5 (N)));
5224 -- Allocators do not have an identifier denoting the access type,
5225 -- so we must locate it through the expression to check whether
5226 -- the views are consistent.
5228 elsif Nkind (N) = N_Allocator
5229 and then Nkind (Expression (N)) = N_Qualified_Expression
5230 and then Is_Entity_Name (Subtype_Mark (Expression (N)))
5231 and then Instantiating
5234 T : constant Node_Id :=
5235 Get_Associated_Node (Subtype_Mark (Expression (N)));
5240 -- Retrieve the allocator node in the generic copy
5242 Acc_T := Etype (Parent (Parent (T)));
5244 and then Is_Private_Type (Acc_T)
5246 Switch_View (Acc_T);
5253 -- For a proper body, we must catch the case of a proper body that
5254 -- replaces a stub. This represents the point at which a separate
5255 -- compilation unit, and hence template file, may be referenced, so
5256 -- we must make a new source instantiation entry for the template
5257 -- of the subunit, and ensure that all nodes in the subunit are
5258 -- adjusted using this new source instantiation entry.
5260 elsif Nkind (N) in N_Proper_Body then
5262 Save_Adjustment : constant Sloc_Adjustment := S_Adjustment;
5265 if Instantiating and then Was_Originally_Stub (N) then
5266 Create_Instantiation_Source
5267 (Instantiation_Node,
5268 Defining_Entity (N),
5273 -- Now copy the fields of the proper body, using the new
5274 -- adjustment factor if one was needed as per test above.
5278 -- Restore the original adjustment factor in case changed
5280 S_Adjustment := Save_Adjustment;
5283 -- Don't copy Ident or Comment pragmas, since the comment belongs
5284 -- to the generic unit, not to the instantiating unit.
5286 elsif Nkind (N) = N_Pragma
5287 and then Instantiating
5290 Prag_Id : constant Pragma_Id := Get_Pragma_Id (Chars (N));
5293 if Prag_Id = Pragma_Ident
5294 or else Prag_Id = Pragma_Comment
5296 New_N := Make_Null_Statement (Sloc (N));
5303 elsif Nkind (N) = N_Integer_Literal
5304 or else Nkind (N) = N_Real_Literal
5306 -- No descendant fields need traversing
5310 -- For the remaining nodes, copy recursively their descendants
5316 and then Nkind (N) = N_Subprogram_Body
5318 Set_Generic_Parent (Specification (New_N), N);
5323 end Copy_Generic_Node;
5325 ----------------------------
5326 -- Denotes_Formal_Package --
5327 ----------------------------
5329 function Denotes_Formal_Package
5331 On_Exit : Boolean := False) return Boolean
5334 Scop : constant Entity_Id := Scope (Pack);
5341 (Instance_Envs.Last).Instantiated_Parent.Act_Id;
5343 Par := Current_Instantiated_Parent.Act_Id;
5346 if Ekind (Scop) = E_Generic_Package
5347 or else Nkind (Unit_Declaration_Node (Scop)) =
5348 N_Generic_Subprogram_Declaration
5352 elsif Nkind (Parent (Pack)) = N_Formal_Package_Declaration then
5359 -- Check whether this package is associated with a formal
5360 -- package of the enclosing instantiation. Iterate over the
5361 -- list of renamings.
5363 E := First_Entity (Par);
5364 while Present (E) loop
5365 if Ekind (E) /= E_Package
5366 or else Nkind (Parent (E)) /= N_Package_Renaming_Declaration
5369 elsif Renamed_Object (E) = Par then
5372 elsif Renamed_Object (E) = Pack then
5381 end Denotes_Formal_Package;
5387 procedure End_Generic is
5389 -- ??? More things could be factored out in this
5390 -- routine. Should probably be done at a later stage.
5392 Inside_A_Generic := Generic_Flags.Table (Generic_Flags.Last);
5393 Generic_Flags.Decrement_Last;
5395 Expander_Mode_Restore;
5398 ----------------------
5399 -- Find_Actual_Type --
5400 ----------------------
5402 function Find_Actual_Type
5404 Gen_Scope : Entity_Id) return Entity_Id
5409 if not Is_Child_Unit (Gen_Scope) then
5410 return Get_Instance_Of (Typ);
5412 elsif not Is_Generic_Type (Typ)
5413 or else Scope (Typ) = Gen_Scope
5415 return Get_Instance_Of (Typ);
5418 T := Current_Entity (Typ);
5419 while Present (T) loop
5420 if In_Open_Scopes (Scope (T)) then
5429 end Find_Actual_Type;
5431 ----------------------------
5432 -- Freeze_Subprogram_Body --
5433 ----------------------------
5435 procedure Freeze_Subprogram_Body
5436 (Inst_Node : Node_Id;
5438 Pack_Id : Entity_Id)
5441 Gen_Unit : constant Entity_Id := Get_Generic_Entity (Inst_Node);
5442 Par : constant Entity_Id := Scope (Gen_Unit);
5447 function Earlier (N1, N2 : Node_Id) return Boolean;
5448 -- Yields True if N1 and N2 appear in the same compilation unit,
5449 -- ignoring subunits, and if N1 is to the left of N2 in a left-to-right
5450 -- traversal of the tree for the unit.
5452 function Enclosing_Body (N : Node_Id) return Node_Id;
5453 -- Find innermost package body that encloses the given node, and which
5454 -- is not a compilation unit. Freeze nodes for the instance, or for its
5455 -- enclosing body, may be inserted after the enclosing_body of the
5458 function Package_Freeze_Node (B : Node_Id) return Node_Id;
5459 -- Find entity for given package body, and locate or create a freeze
5462 function True_Parent (N : Node_Id) return Node_Id;
5463 -- For a subunit, return parent of corresponding stub
5469 function Earlier (N1, N2 : Node_Id) return Boolean is
5475 procedure Find_Depth (P : in out Node_Id; D : in out Integer);
5476 -- Find distance from given node to enclosing compilation unit
5482 procedure Find_Depth (P : in out Node_Id; D : in out Integer) is
5485 and then Nkind (P) /= N_Compilation_Unit
5487 P := True_Parent (P);
5492 -- Start of procesing for Earlier
5495 Find_Depth (P1, D1);
5496 Find_Depth (P2, D2);
5506 P1 := True_Parent (P1);
5511 P2 := True_Parent (P2);
5515 -- At this point P1 and P2 are at the same distance from the root.
5516 -- We examine their parents until we find a common declarative
5517 -- list, at which point we can establish their relative placement
5518 -- by comparing their ultimate slocs. If we reach the root,
5519 -- N1 and N2 do not descend from the same declarative list (e.g.
5520 -- one is nested in the declarative part and the other is in a block
5521 -- in the statement part) and the earlier one is already frozen.
5523 while not Is_List_Member (P1)
5524 or else not Is_List_Member (P2)
5525 or else List_Containing (P1) /= List_Containing (P2)
5527 P1 := True_Parent (P1);
5528 P2 := True_Parent (P2);
5530 if Nkind (Parent (P1)) = N_Subunit then
5531 P1 := Corresponding_Stub (Parent (P1));
5534 if Nkind (Parent (P2)) = N_Subunit then
5535 P2 := Corresponding_Stub (Parent (P2));
5544 Top_Level_Location (Sloc (P1)) < Top_Level_Location (Sloc (P2));
5547 --------------------
5548 -- Enclosing_Body --
5549 --------------------
5551 function Enclosing_Body (N : Node_Id) return Node_Id is
5552 P : Node_Id := Parent (N);
5556 and then Nkind (Parent (P)) /= N_Compilation_Unit
5558 if Nkind (P) = N_Package_Body then
5560 if Nkind (Parent (P)) = N_Subunit then
5561 return Corresponding_Stub (Parent (P));
5567 P := True_Parent (P);
5573 -------------------------
5574 -- Package_Freeze_Node --
5575 -------------------------
5577 function Package_Freeze_Node (B : Node_Id) return Node_Id is
5581 if Nkind (B) = N_Package_Body then
5582 Id := Corresponding_Spec (B);
5584 else pragma Assert (Nkind (B) = N_Package_Body_Stub);
5585 Id := Corresponding_Spec (Proper_Body (Unit (Library_Unit (B))));
5588 Ensure_Freeze_Node (Id);
5589 return Freeze_Node (Id);
5590 end Package_Freeze_Node;
5596 function True_Parent (N : Node_Id) return Node_Id is
5598 if Nkind (Parent (N)) = N_Subunit then
5599 return Parent (Corresponding_Stub (Parent (N)));
5605 -- Start of processing of Freeze_Subprogram_Body
5608 -- If the instance and the generic body appear within the same
5609 -- unit, and the instance preceeds the generic, the freeze node for
5610 -- the instance must appear after that of the generic. If the generic
5611 -- is nested within another instance I2, then current instance must
5612 -- be frozen after I2. In both cases, the freeze nodes are those of
5613 -- enclosing packages. Otherwise, the freeze node is placed at the end
5614 -- of the current declarative part.
5616 Enc_G := Enclosing_Body (Gen_Body);
5617 Enc_I := Enclosing_Body (Inst_Node);
5618 Ensure_Freeze_Node (Pack_Id);
5619 F_Node := Freeze_Node (Pack_Id);
5621 if Is_Generic_Instance (Par)
5622 and then Present (Freeze_Node (Par))
5624 In_Same_Declarative_Part (Freeze_Node (Par), Inst_Node)
5626 if ABE_Is_Certain (Get_Package_Instantiation_Node (Par)) then
5628 -- The parent was a premature instantiation. Insert freeze
5629 -- node at the end the current declarative part.
5631 Insert_After_Last_Decl (Inst_Node, F_Node);
5634 Insert_After (Freeze_Node (Par), F_Node);
5637 -- The body enclosing the instance should be frozen after the body
5638 -- that includes the generic, because the body of the instance may
5639 -- make references to entities therein. If the two are not in the
5640 -- same declarative part, or if the one enclosing the instance is
5641 -- frozen already, freeze the instance at the end of the current
5642 -- declarative part.
5644 elsif Is_Generic_Instance (Par)
5645 and then Present (Freeze_Node (Par))
5646 and then Present (Enc_I)
5648 if In_Same_Declarative_Part (Freeze_Node (Par), Enc_I)
5650 (Nkind (Enc_I) = N_Package_Body
5652 In_Same_Declarative_Part (Freeze_Node (Par), Parent (Enc_I)))
5654 -- The enclosing package may contain several instances. Rather
5655 -- than computing the earliest point at which to insert its
5656 -- freeze node, we place it at the end of the declarative part
5657 -- of the parent of the generic.
5659 Insert_After_Last_Decl
5660 (Freeze_Node (Par), Package_Freeze_Node (Enc_I));
5663 Insert_After_Last_Decl (Inst_Node, F_Node);
5665 elsif Present (Enc_G)
5666 and then Present (Enc_I)
5667 and then Enc_G /= Enc_I
5668 and then Earlier (Inst_Node, Gen_Body)
5670 if Nkind (Enc_G) = N_Package_Body then
5671 E_G_Id := Corresponding_Spec (Enc_G);
5672 else pragma Assert (Nkind (Enc_G) = N_Package_Body_Stub);
5674 Corresponding_Spec (Proper_Body (Unit (Library_Unit (Enc_G))));
5677 -- Freeze package that encloses instance, and place node after
5678 -- package that encloses generic. If enclosing package is already
5679 -- frozen we have to assume it is at the proper place. This may
5680 -- be a potential ABE that requires dynamic checking.
5682 Insert_After_Last_Decl (Enc_G, Package_Freeze_Node (Enc_I));
5684 -- Freeze enclosing subunit before instance
5686 Ensure_Freeze_Node (E_G_Id);
5688 if not Is_List_Member (Freeze_Node (E_G_Id)) then
5689 Insert_After (Enc_G, Freeze_Node (E_G_Id));
5692 Insert_After_Last_Decl (Inst_Node, F_Node);
5695 -- If none of the above, insert freeze node at the end of the
5696 -- current declarative part.
5698 Insert_After_Last_Decl (Inst_Node, F_Node);
5700 end Freeze_Subprogram_Body;
5706 function Get_Gen_Id (E : Assoc_Ptr) return Entity_Id is
5708 return Generic_Renamings.Table (E).Gen_Id;
5711 ---------------------
5712 -- Get_Instance_Of --
5713 ---------------------
5715 function Get_Instance_Of (A : Entity_Id) return Entity_Id is
5716 Res : constant Assoc_Ptr := Generic_Renamings_HTable.Get (A);
5719 if Res /= Assoc_Null then
5720 return Generic_Renamings.Table (Res).Act_Id;
5722 -- On exit, entity is not instantiated: not a generic parameter,
5723 -- or else parameter of an inner generic unit.
5727 end Get_Instance_Of;
5729 ------------------------------------
5730 -- Get_Package_Instantiation_Node --
5731 ------------------------------------
5733 function Get_Package_Instantiation_Node (A : Entity_Id) return Node_Id is
5734 Decl : Node_Id := Unit_Declaration_Node (A);
5738 -- If the instantiation is a compilation unit that does not need a
5739 -- body then the instantiation node has been rewritten as a package
5740 -- declaration for the instance, and we return the original node.
5742 -- If it is a compilation unit and the instance node has not been
5743 -- rewritten, then it is still the unit of the compilation. Finally,
5744 -- if a body is present, this is a parent of the main unit whose body
5745 -- has been compiled for inlining purposes, and the instantiation node
5746 -- has been rewritten with the instance body.
5748 -- Otherwise the instantiation node appears after the declaration.
5749 -- If the entity is a formal package, the declaration may have been
5750 -- rewritten as a generic declaration (in the case of a formal with a
5751 -- box) or left as a formal package declaration if it has actuals, and
5752 -- is found with a forward search.
5754 if Nkind (Parent (Decl)) = N_Compilation_Unit then
5755 if Nkind (Decl) = N_Package_Declaration
5756 and then Present (Corresponding_Body (Decl))
5758 Decl := Unit_Declaration_Node (Corresponding_Body (Decl));
5761 if Nkind (Original_Node (Decl)) = N_Package_Instantiation then
5762 return Original_Node (Decl);
5764 return Unit (Parent (Decl));
5767 elsif Nkind (Decl) = N_Generic_Package_Declaration
5768 and then Nkind (Original_Node (Decl)) = N_Formal_Package_Declaration
5770 return Original_Node (Decl);
5773 Inst := Next (Decl);
5774 while Nkind (Inst) /= N_Package_Instantiation
5775 and then Nkind (Inst) /= N_Formal_Package_Declaration
5782 end Get_Package_Instantiation_Node;
5784 ------------------------
5785 -- Has_Been_Exchanged --
5786 ------------------------
5788 function Has_Been_Exchanged (E : Entity_Id) return Boolean is
5789 Next : Elmt_Id := First_Elmt (Exchanged_Views);
5792 while Present (Next) loop
5793 if Full_View (Node (Next)) = E then
5801 end Has_Been_Exchanged;
5807 function Hash (F : Entity_Id) return HTable_Range is
5809 return HTable_Range (F mod HTable_Size);
5812 ------------------------
5813 -- Hide_Current_Scope --
5814 ------------------------
5816 procedure Hide_Current_Scope is
5817 C : constant Entity_Id := Current_Scope;
5821 Set_Is_Hidden_Open_Scope (C);
5822 E := First_Entity (C);
5824 while Present (E) loop
5825 if Is_Immediately_Visible (E) then
5826 Set_Is_Immediately_Visible (E, False);
5827 Append_Elmt (E, Hidden_Entities);
5833 -- Make the scope name invisible as well. This is necessary, but
5834 -- might conflict with calls to Rtsfind later on, in case the scope
5835 -- is a predefined one. There is no clean solution to this problem, so
5836 -- for now we depend on the user not redefining Standard itself in one
5837 -- of the parent units.
5839 if Is_Immediately_Visible (C)
5840 and then C /= Standard_Standard
5842 Set_Is_Immediately_Visible (C, False);
5843 Append_Elmt (C, Hidden_Entities);
5846 end Hide_Current_Scope;
5852 procedure Init_Env is
5853 Saved : Instance_Env;
5856 Saved.Ada_Version := Ada_Version;
5857 Saved.Ada_Version_Explicit := Ada_Version_Explicit;
5858 Saved.Instantiated_Parent := Current_Instantiated_Parent;
5859 Saved.Exchanged_Views := Exchanged_Views;
5860 Saved.Hidden_Entities := Hidden_Entities;
5861 Saved.Current_Sem_Unit := Current_Sem_Unit;
5862 Saved.Parent_Unit_Visible := Parent_Unit_Visible;
5863 Instance_Envs.Increment_Last;
5864 Instance_Envs.Table (Instance_Envs.Last) := Saved;
5866 Exchanged_Views := New_Elmt_List;
5867 Hidden_Entities := New_Elmt_List;
5869 -- Make dummy entry for Instantiated parent. If generic unit is
5870 -- legal, this is set properly in Set_Instance_Env.
5872 Current_Instantiated_Parent :=
5873 (Current_Scope, Current_Scope, Assoc_Null);
5876 ------------------------------
5877 -- In_Same_Declarative_Part --
5878 ------------------------------
5880 function In_Same_Declarative_Part
5882 Inst : Node_Id) return Boolean
5884 Decls : constant Node_Id := Parent (F_Node);
5885 Nod : Node_Id := Parent (Inst);
5888 while Present (Nod) loop
5892 elsif Nkind (Nod) = N_Subprogram_Body
5893 or else Nkind (Nod) = N_Package_Body
5894 or else Nkind (Nod) = N_Task_Body
5895 or else Nkind (Nod) = N_Protected_Body
5896 or else Nkind (Nod) = N_Block_Statement
5900 elsif Nkind (Nod) = N_Subunit then
5901 Nod := Corresponding_Stub (Nod);
5903 elsif Nkind (Nod) = N_Compilation_Unit then
5906 Nod := Parent (Nod);
5911 end In_Same_Declarative_Part;
5913 ---------------------
5914 -- In_Main_Context --
5915 ---------------------
5917 function In_Main_Context (E : Entity_Id) return Boolean is
5923 if not Is_Compilation_Unit (E)
5924 or else Ekind (E) /= E_Package
5925 or else In_Private_Part (E)
5930 Context := Context_Items (Cunit (Main_Unit));
5932 Clause := First (Context);
5933 while Present (Clause) loop
5934 if Nkind (Clause) = N_With_Clause then
5935 Nam := Name (Clause);
5937 -- If the current scope is part of the context of the main unit,
5938 -- analysis of the corresponding with_clause is not complete, and
5939 -- the entity is not set. We use the Chars field directly, which
5940 -- might produce false positives in rare cases, but guarantees
5941 -- that we produce all the instance bodies we will need.
5943 if (Nkind (Nam) = N_Identifier
5944 and then Chars (Nam) = Chars (E))
5945 or else (Nkind (Nam) = N_Selected_Component
5946 and then Chars (Selector_Name (Nam)) = Chars (E))
5956 end In_Main_Context;
5958 ---------------------
5959 -- Inherit_Context --
5960 ---------------------
5962 procedure Inherit_Context (Gen_Decl : Node_Id; Inst : Node_Id) is
5963 Current_Context : List_Id;
5964 Current_Unit : Node_Id;
5969 if Nkind (Parent (Gen_Decl)) = N_Compilation_Unit then
5971 -- The inherited context is attached to the enclosing compilation
5972 -- unit. This is either the main unit, or the declaration for the
5973 -- main unit (in case the instantation appears within the package
5974 -- declaration and the main unit is its body).
5976 Current_Unit := Parent (Inst);
5977 while Present (Current_Unit)
5978 and then Nkind (Current_Unit) /= N_Compilation_Unit
5980 Current_Unit := Parent (Current_Unit);
5983 Current_Context := Context_Items (Current_Unit);
5985 Item := First (Context_Items (Parent (Gen_Decl)));
5986 while Present (Item) loop
5987 if Nkind (Item) = N_With_Clause then
5988 New_I := New_Copy (Item);
5989 Set_Implicit_With (New_I, True);
5990 Append (New_I, Current_Context);
5996 end Inherit_Context;
6002 procedure Initialize is
6004 Generic_Renamings.Init;
6007 Generic_Renamings_HTable.Reset;
6008 Circularity_Detected := False;
6009 Exchanged_Views := No_Elist;
6010 Hidden_Entities := No_Elist;
6013 ----------------------------
6014 -- Insert_After_Last_Decl --
6015 ----------------------------
6017 procedure Insert_After_Last_Decl (N : Node_Id; F_Node : Node_Id) is
6018 L : List_Id := List_Containing (N);
6019 P : constant Node_Id := Parent (L);
6022 if not Is_List_Member (F_Node) then
6023 if Nkind (P) = N_Package_Specification
6024 and then L = Visible_Declarations (P)
6025 and then Present (Private_Declarations (P))
6026 and then not Is_Empty_List (Private_Declarations (P))
6028 L := Private_Declarations (P);
6031 Insert_After (Last (L), F_Node);
6033 end Insert_After_Last_Decl;
6039 procedure Install_Body
6040 (Act_Body : Node_Id;
6045 Act_Id : constant Entity_Id := Corresponding_Spec (Act_Body);
6046 Act_Unit : constant Node_Id := Unit (Cunit (Get_Source_Unit (N)));
6047 Gen_Id : constant Entity_Id := Corresponding_Spec (Gen_Body);
6048 Par : constant Entity_Id := Scope (Gen_Id);
6049 Gen_Unit : constant Node_Id :=
6050 Unit (Cunit (Get_Source_Unit (Gen_Decl)));
6051 Orig_Body : Node_Id := Gen_Body;
6053 Body_Unit : Node_Id;
6055 Must_Delay : Boolean;
6057 function Enclosing_Subp (Id : Entity_Id) return Entity_Id;
6058 -- Find subprogram (if any) that encloses instance and/or generic body
6060 function True_Sloc (N : Node_Id) return Source_Ptr;
6061 -- If the instance is nested inside a generic unit, the Sloc of the
6062 -- instance indicates the place of the original definition, not the
6063 -- point of the current enclosing instance. Pending a better usage of
6064 -- Slocs to indicate instantiation places, we determine the place of
6065 -- origin of a node by finding the maximum sloc of any ancestor node.
6066 -- Why is this not equivalent to Top_Level_Location ???
6068 --------------------
6069 -- Enclosing_Subp --
6070 --------------------
6072 function Enclosing_Subp (Id : Entity_Id) return Entity_Id is
6073 Scop : Entity_Id := Scope (Id);
6076 while Scop /= Standard_Standard
6077 and then not Is_Overloadable (Scop)
6079 Scop := Scope (Scop);
6089 function True_Sloc (N : Node_Id) return Source_Ptr is
6096 while Present (N1) and then N1 /= Act_Unit loop
6097 if Sloc (N1) > Res then
6107 -- Start of processing for Install_Body
6110 -- If the body is a subunit, the freeze point is the corresponding
6111 -- stub in the current compilation, not the subunit itself.
6113 if Nkind (Parent (Gen_Body)) = N_Subunit then
6114 Orig_Body := Corresponding_Stub (Parent (Gen_Body));
6116 Orig_Body := Gen_Body;
6119 Body_Unit := Unit (Cunit (Get_Source_Unit (Orig_Body)));
6121 -- If the instantiation and the generic definition appear in the
6122 -- same package declaration, this is an early instantiation.
6123 -- If they appear in the same declarative part, it is an early
6124 -- instantiation only if the generic body appears textually later,
6125 -- and the generic body is also in the main unit.
6127 -- If instance is nested within a subprogram, and the generic body is
6128 -- not, the instance is delayed because the enclosing body is. If
6129 -- instance and body are within the same scope, or the same sub-
6130 -- program body, indicate explicitly that the instance is delayed.
6133 (Gen_Unit = Act_Unit
6134 and then ((Nkind (Gen_Unit) = N_Package_Declaration)
6135 or else Nkind (Gen_Unit) = N_Generic_Package_Declaration
6136 or else (Gen_Unit = Body_Unit
6137 and then True_Sloc (N) < Sloc (Orig_Body)))
6138 and then Is_In_Main_Unit (Gen_Unit)
6139 and then (Scope (Act_Id) = Scope (Gen_Id)
6141 Enclosing_Subp (Act_Id) = Enclosing_Subp (Gen_Id)));
6143 -- If this is an early instantiation, the freeze node is placed after
6144 -- the generic body. Otherwise, if the generic appears in an instance,
6145 -- we cannot freeze the current instance until the outer one is frozen.
6146 -- This is only relevant if the current instance is nested within some
6147 -- inner scope not itself within the outer instance. If this scope is
6148 -- a package body in the same declarative part as the outer instance,
6149 -- then that body needs to be frozen after the outer instance. Finally,
6150 -- if no delay is needed, we place the freeze node at the end of the
6151 -- current declarative part.
6153 if Expander_Active then
6154 Ensure_Freeze_Node (Act_Id);
6155 F_Node := Freeze_Node (Act_Id);
6158 Insert_After (Orig_Body, F_Node);
6160 elsif Is_Generic_Instance (Par)
6161 and then Present (Freeze_Node (Par))
6162 and then Scope (Act_Id) /= Par
6164 -- Freeze instance of inner generic after instance of enclosing
6167 if In_Same_Declarative_Part (Freeze_Node (Par), N) then
6168 Insert_After (Freeze_Node (Par), F_Node);
6170 -- Freeze package enclosing instance of inner generic after
6171 -- instance of enclosing generic.
6173 elsif Nkind (Parent (N)) = N_Package_Body
6174 and then In_Same_Declarative_Part (Freeze_Node (Par), Parent (N))
6178 Enclosing : constant Entity_Id :=
6179 Corresponding_Spec (Parent (N));
6182 Insert_After_Last_Decl (N, F_Node);
6183 Ensure_Freeze_Node (Enclosing);
6185 if not Is_List_Member (Freeze_Node (Enclosing)) then
6186 Insert_After (Freeze_Node (Par), Freeze_Node (Enclosing));
6191 Insert_After_Last_Decl (N, F_Node);
6195 Insert_After_Last_Decl (N, F_Node);
6199 Set_Is_Frozen (Act_Id);
6200 Insert_Before (N, Act_Body);
6201 Mark_Rewrite_Insertion (Act_Body);
6204 --------------------
6205 -- Install_Parent --
6206 --------------------
6208 procedure Install_Parent (P : Entity_Id; In_Body : Boolean := False) is
6209 Ancestors : constant Elist_Id := New_Elmt_List;
6210 S : constant Entity_Id := Current_Scope;
6211 Inst_Par : Entity_Id;
6212 First_Par : Entity_Id;
6213 Inst_Node : Node_Id;
6214 Gen_Par : Entity_Id;
6215 First_Gen : Entity_Id;
6218 procedure Install_Formal_Packages (Par : Entity_Id);
6219 -- If any of the formals of the parent are formal packages with box,
6220 -- their formal parts are visible in the parent and thus in the child
6221 -- unit as well. Analogous to what is done in Check_Generic_Actuals
6222 -- for the unit itself.
6224 procedure Install_Noninstance_Specs (Par : Entity_Id);
6225 -- Install the scopes of noninstance parent units ending with Par
6227 procedure Install_Spec (Par : Entity_Id);
6228 -- The child unit is within the declarative part of the parent, so
6229 -- the declarations within the parent are immediately visible.
6231 -----------------------------
6232 -- Install_Formal_Packages --
6233 -----------------------------
6235 procedure Install_Formal_Packages (Par : Entity_Id) is
6239 E := First_Entity (Par);
6240 while Present (E) loop
6241 if Ekind (E) = E_Package
6242 and then Nkind (Parent (E)) = N_Package_Renaming_Declaration
6244 -- If this is the renaming for the parent instance, done
6246 if Renamed_Object (E) = Par then
6249 -- The visibility of a formal of an enclosing generic is
6252 elsif Denotes_Formal_Package (E) then
6255 elsif Present (Associated_Formal_Package (E))
6256 and then Box_Present (Parent (Associated_Formal_Package (E)))
6258 Check_Generic_Actuals (Renamed_Object (E), True);
6259 Set_Is_Hidden (E, False);
6265 end Install_Formal_Packages;
6267 -------------------------------
6268 -- Install_Noninstance_Specs --
6269 -------------------------------
6271 procedure Install_Noninstance_Specs (Par : Entity_Id) is
6274 and then Par /= Standard_Standard
6275 and then not In_Open_Scopes (Par)
6277 Install_Noninstance_Specs (Scope (Par));
6280 end Install_Noninstance_Specs;
6286 procedure Install_Spec (Par : Entity_Id) is
6287 Spec : constant Node_Id :=
6288 Specification (Unit_Declaration_Node (Par));
6291 if not Is_Child_Unit (Par) then
6292 Parent_Unit_Visible := Is_Immediately_Visible (Par);
6296 Set_Is_Immediately_Visible (Par);
6297 Install_Visible_Declarations (Par);
6298 Install_Private_Declarations (Par);
6299 Set_Use (Visible_Declarations (Spec));
6300 Set_Use (Private_Declarations (Spec));
6303 -- Start of processing for Install_Parent
6306 -- We need to install the parent instance to compile the instantiation
6307 -- of the child, but the child instance must appear in the current
6308 -- scope. Given that we cannot place the parent above the current
6309 -- scope in the scope stack, we duplicate the current scope and unstack
6310 -- both after the instantiation is complete.
6312 -- If the parent is itself the instantiation of a child unit, we must
6313 -- also stack the instantiation of its parent, and so on. Each such
6314 -- ancestor is the prefix of the name in a prior instantiation.
6316 -- If this is a nested instance, the parent unit itself resolves to
6317 -- a renaming of the parent instance, whose declaration we need.
6319 -- Finally, the parent may be a generic (not an instance) when the
6320 -- child unit appears as a formal package.
6324 if Present (Renamed_Entity (Inst_Par)) then
6325 Inst_Par := Renamed_Entity (Inst_Par);
6328 First_Par := Inst_Par;
6331 Generic_Parent (Specification (Unit_Declaration_Node (Inst_Par)));
6333 First_Gen := Gen_Par;
6335 while Present (Gen_Par)
6336 and then Is_Child_Unit (Gen_Par)
6338 -- Load grandparent instance as well
6340 Inst_Node := Get_Package_Instantiation_Node (Inst_Par);
6342 if Nkind (Name (Inst_Node)) = N_Expanded_Name then
6343 Inst_Par := Entity (Prefix (Name (Inst_Node)));
6345 if Present (Renamed_Entity (Inst_Par)) then
6346 Inst_Par := Renamed_Entity (Inst_Par);
6351 (Specification (Unit_Declaration_Node (Inst_Par)));
6353 if Present (Gen_Par) then
6354 Prepend_Elmt (Inst_Par, Ancestors);
6357 -- Parent is not the name of an instantiation
6359 Install_Noninstance_Specs (Inst_Par);
6371 if Present (First_Gen) then
6372 Append_Elmt (First_Par, Ancestors);
6375 Install_Noninstance_Specs (First_Par);
6378 if not Is_Empty_Elmt_List (Ancestors) then
6379 Elmt := First_Elmt (Ancestors);
6381 while Present (Elmt) loop
6382 Install_Spec (Node (Elmt));
6383 Install_Formal_Packages (Node (Elmt));
6394 --------------------------------
6395 -- Instantiate_Formal_Package --
6396 --------------------------------
6398 function Instantiate_Formal_Package
6401 Analyzed_Formal : Node_Id) return List_Id
6403 Loc : constant Source_Ptr := Sloc (Actual);
6404 Actual_Pack : Entity_Id;
6405 Formal_Pack : Entity_Id;
6406 Gen_Parent : Entity_Id;
6409 Parent_Spec : Node_Id;
6411 procedure Find_Matching_Actual
6413 Act : in out Entity_Id);
6414 -- We need to associate each formal entity in the formal package
6415 -- with the corresponding entity in the actual package. The actual
6416 -- package has been analyzed and possibly expanded, and as a result
6417 -- there is no one-to-one correspondence between the two lists (for
6418 -- example, the actual may include subtypes, itypes, and inherited
6419 -- primitive operations, interspersed among the renaming declarations
6420 -- for the actuals) . We retrieve the corresponding actual by name
6421 -- because each actual has the same name as the formal, and they do
6422 -- appear in the same order.
6424 function Formal_Entity
6426 Act_Ent : Entity_Id) return Entity_Id;
6427 -- Returns the entity associated with the given formal F. In the
6428 -- case where F is a formal package, this function will iterate
6429 -- through all of F's formals and enter map associations from the
6430 -- actuals occurring in the formal package's corresponding actual
6431 -- package (obtained via Act_Ent) to the formal package's formal
6432 -- parameters. This function is called recursively for arbitrary
6433 -- levels of formal packages.
6435 function Is_Instance_Of
6436 (Act_Spec : Entity_Id;
6437 Gen_Anc : Entity_Id) return Boolean;
6438 -- The actual can be an instantiation of a generic within another
6439 -- instance, in which case there is no direct link from it to the
6440 -- original generic ancestor. In that case, we recognize that the
6441 -- ultimate ancestor is the same by examining names and scopes.
6443 procedure Map_Entities (Form : Entity_Id; Act : Entity_Id);
6444 -- Within the generic part, entities in the formal package are
6445 -- visible. To validate subsequent type declarations, indicate
6446 -- the correspondence betwen the entities in the analyzed formal,
6447 -- and the entities in the actual package. There are three packages
6448 -- involved in the instantiation of a formal package: the parent
6449 -- generic P1 which appears in the generic declaration, the fake
6450 -- instantiation P2 which appears in the analyzed generic, and whose
6451 -- visible entities may be used in subsequent formals, and the actual
6452 -- P3 in the instance. To validate subsequent formals, me indicate
6453 -- that the entities in P2 are mapped into those of P3. The mapping of
6454 -- entities has to be done recursively for nested packages.
6456 procedure Process_Nested_Formal (Formal : Entity_Id);
6457 -- If the current formal is declared with a box, its own formals are
6458 -- visible in the instance, as they were in the generic, and their
6459 -- Hidden flag must be reset. If some of these formals are themselves
6460 -- packages declared with a box, the processing must be recursive.
6462 --------------------------
6463 -- Find_Matching_Actual --
6464 --------------------------
6466 procedure Find_Matching_Actual
6468 Act : in out Entity_Id)
6470 Formal_Ent : Entity_Id;
6473 case Nkind (Original_Node (F)) is
6474 when N_Formal_Object_Declaration |
6475 N_Formal_Type_Declaration =>
6476 Formal_Ent := Defining_Identifier (F);
6478 while Chars (Act) /= Chars (Formal_Ent) loop
6482 when N_Formal_Subprogram_Declaration |
6483 N_Formal_Package_Declaration |
6484 N_Package_Declaration |
6485 N_Generic_Package_Declaration =>
6486 Formal_Ent := Defining_Entity (F);
6488 while Chars (Act) /= Chars (Formal_Ent) loop
6493 raise Program_Error;
6495 end Find_Matching_Actual;
6501 function Formal_Entity
6503 Act_Ent : Entity_Id) return Entity_Id
6505 Orig_Node : Node_Id := F;
6506 Act_Pkg : Entity_Id;
6509 case Nkind (Original_Node (F)) is
6510 when N_Formal_Object_Declaration =>
6511 return Defining_Identifier (F);
6513 when N_Formal_Type_Declaration =>
6514 return Defining_Identifier (F);
6516 when N_Formal_Subprogram_Declaration =>
6517 return Defining_Unit_Name (Specification (F));
6519 when N_Package_Declaration =>
6520 return Defining_Unit_Name (Specification (F));
6522 when N_Formal_Package_Declaration |
6523 N_Generic_Package_Declaration =>
6525 if Nkind (F) = N_Generic_Package_Declaration then
6526 Orig_Node := Original_Node (F);
6531 -- Find matching actual package, skipping over itypes and
6532 -- other entities generated when analyzing the formal. We
6533 -- know that if the instantiation is legal then there is
6534 -- a matching package for the formal.
6536 while Ekind (Act_Pkg) /= E_Package loop
6537 Act_Pkg := Next_Entity (Act_Pkg);
6541 Actual_Ent : Entity_Id := First_Entity (Act_Pkg);
6542 Formal_Node : Node_Id;
6543 Formal_Ent : Entity_Id;
6545 Gen_Decl : constant Node_Id :=
6546 Unit_Declaration_Node
6547 (Entity (Name (Orig_Node)));
6549 Formals : constant List_Id :=
6550 Generic_Formal_Declarations (Gen_Decl);
6553 if Present (Formals) then
6554 Formal_Node := First_Non_Pragma (Formals);
6556 Formal_Node := Empty;
6559 while Present (Actual_Ent)
6560 and then Present (Formal_Node)
6561 and then Actual_Ent /= First_Private_Entity (Act_Pkg)
6563 -- ??? Are the following calls also needed here:
6565 -- Set_Is_Hidden (Actual_Ent, False);
6566 -- Set_Is_Potentially_Use_Visible
6567 -- (Actual_Ent, In_Use (Act_Ent));
6569 Formal_Ent := Formal_Entity (Formal_Node, Actual_Ent);
6570 if Present (Formal_Ent) then
6571 Set_Instance_Of (Formal_Ent, Actual_Ent);
6573 Next_Non_Pragma (Formal_Node);
6575 Next_Entity (Actual_Ent);
6579 return Defining_Identifier (Orig_Node);
6581 when N_Use_Package_Clause =>
6584 when N_Use_Type_Clause =>
6587 -- We return Empty for all other encountered forms of
6588 -- declarations because there are some cases of nonformal
6589 -- sorts of declaration that can show up (e.g., when array
6590 -- formals are present). Since it's not clear what kinds
6591 -- can appear among the formals, we won't raise failure here.
6599 --------------------
6600 -- Is_Instance_Of --
6601 --------------------
6603 function Is_Instance_Of
6604 (Act_Spec : Entity_Id;
6605 Gen_Anc : Entity_Id) return Boolean
6607 Gen_Par : constant Entity_Id := Generic_Parent (Act_Spec);
6610 if No (Gen_Par) then
6613 -- Simplest case: the generic parent of the actual is the formal
6615 elsif Gen_Par = Gen_Anc then
6618 elsif Chars (Gen_Par) /= Chars (Gen_Anc) then
6621 -- The actual may be obtained through several instantiations. Its
6622 -- scope must itself be an instance of a generic declared in the
6623 -- same scope as the formal. Any other case is detected above.
6625 elsif not Is_Generic_Instance (Scope (Gen_Par)) then
6629 return Generic_Parent (Parent (Scope (Gen_Par))) = Scope (Gen_Anc);
6637 procedure Map_Entities (Form : Entity_Id; Act : Entity_Id) is
6642 Set_Instance_Of (Form, Act);
6644 -- Traverse formal and actual package to map the corresponding
6645 -- entities. We skip over internal entities that may be generated
6646 -- during semantic analysis, and find the matching entities by
6647 -- name, given that they must appear in the same order.
6649 E1 := First_Entity (Form);
6650 E2 := First_Entity (Act);
6652 and then E1 /= First_Private_Entity (Form)
6654 if not Is_Internal (E1)
6655 and then not Is_Class_Wide_Type (E1)
6656 and then Present (Parent (E1))
6659 and then Chars (E2) /= Chars (E1)
6667 Set_Instance_Of (E1, E2);
6670 and then Is_Tagged_Type (E2)
6673 (Class_Wide_Type (E1), Class_Wide_Type (E2));
6676 if Ekind (E1) = E_Package
6677 and then No (Renamed_Object (E1))
6679 Map_Entities (E1, E2);
6688 ---------------------------
6689 -- Process_Nested_Formal --
6690 ---------------------------
6692 procedure Process_Nested_Formal (Formal : Entity_Id) is
6696 if Present (Associated_Formal_Package (Formal))
6697 and then Box_Present (Parent (Associated_Formal_Package (Formal)))
6699 Ent := First_Entity (Formal);
6700 while Present (Ent) loop
6701 Set_Is_Hidden (Ent, False);
6702 Set_Is_Potentially_Use_Visible
6703 (Ent, Is_Potentially_Use_Visible (Formal));
6705 if Ekind (Ent) = E_Package then
6706 exit when Renamed_Entity (Ent) = Renamed_Entity (Formal);
6707 Process_Nested_Formal (Ent);
6713 end Process_Nested_Formal;
6715 -- Start of processing for Instantiate_Formal_Package
6720 if not Is_Entity_Name (Actual)
6721 or else Ekind (Entity (Actual)) /= E_Package
6724 ("expect package instance to instantiate formal", Actual);
6725 Abandon_Instantiation (Actual);
6726 raise Program_Error;
6729 Actual_Pack := Entity (Actual);
6730 Set_Is_Instantiated (Actual_Pack);
6732 -- The actual may be a renamed package, or an outer generic
6733 -- formal package whose instantiation is converted into a renaming.
6735 if Present (Renamed_Object (Actual_Pack)) then
6736 Actual_Pack := Renamed_Object (Actual_Pack);
6739 if Nkind (Analyzed_Formal) = N_Formal_Package_Declaration then
6740 Gen_Parent := Get_Instance_Of (Entity (Name (Analyzed_Formal)));
6741 Formal_Pack := Defining_Identifier (Analyzed_Formal);
6744 Generic_Parent (Specification (Analyzed_Formal));
6746 Defining_Unit_Name (Specification (Analyzed_Formal));
6749 if Nkind (Parent (Actual_Pack)) = N_Defining_Program_Unit_Name then
6750 Parent_Spec := Specification (Unit_Declaration_Node (Actual_Pack));
6752 Parent_Spec := Parent (Actual_Pack);
6755 if Gen_Parent = Any_Id then
6757 ("previous error in declaration of formal package", Actual);
6758 Abandon_Instantiation (Actual);
6761 Is_Instance_Of (Parent_Spec, Get_Instance_Of (Gen_Parent))
6767 ("actual parameter must be instance of&", Actual, Gen_Parent);
6768 Abandon_Instantiation (Actual);
6771 Set_Instance_Of (Defining_Identifier (Formal), Actual_Pack);
6772 Map_Entities (Formal_Pack, Actual_Pack);
6775 Make_Package_Renaming_Declaration (Loc,
6776 Defining_Unit_Name => New_Copy (Defining_Identifier (Formal)),
6777 Name => New_Reference_To (Actual_Pack, Loc));
6779 Set_Associated_Formal_Package (Defining_Unit_Name (Nod),
6780 Defining_Identifier (Formal));
6781 Decls := New_List (Nod);
6783 -- If the formal F has a box, then the generic declarations are
6784 -- visible in the generic G. In an instance of G, the corresponding
6785 -- entities in the actual for F (which are the actuals for the
6786 -- instantiation of the generic that F denotes) must also be made
6787 -- visible for analysis of the current instance. On exit from the
6788 -- current instance, those entities are made private again. If the
6789 -- actual is currently in use, these entities are also use-visible.
6791 -- The loop through the actual entities also steps through the
6792 -- formal entities and enters associations from formals to
6793 -- actuals into the renaming map. This is necessary to properly
6794 -- handle checking of actual parameter associations for later
6795 -- formals that depend on actuals declared in the formal package.
6797 if Box_Present (Formal) then
6799 Gen_Decl : constant Node_Id :=
6800 Unit_Declaration_Node (Gen_Parent);
6801 Formals : constant List_Id :=
6802 Generic_Formal_Declarations (Gen_Decl);
6803 Actual_Ent : Entity_Id;
6804 Formal_Node : Node_Id;
6805 Formal_Ent : Entity_Id;
6808 if Present (Formals) then
6809 Formal_Node := First_Non_Pragma (Formals);
6811 Formal_Node := Empty;
6814 Actual_Ent := First_Entity (Actual_Pack);
6816 while Present (Actual_Ent)
6817 and then Actual_Ent /= First_Private_Entity (Actual_Pack)
6819 Set_Is_Hidden (Actual_Ent, False);
6820 Set_Is_Potentially_Use_Visible
6821 (Actual_Ent, In_Use (Actual_Pack));
6823 if Ekind (Actual_Ent) = E_Package then
6824 Process_Nested_Formal (Actual_Ent);
6827 if Present (Formal_Node) then
6828 Formal_Ent := Formal_Entity (Formal_Node, Actual_Ent);
6830 if Present (Formal_Ent) then
6831 Find_Matching_Actual (Formal_Node, Actual_Ent);
6832 Set_Instance_Of (Formal_Ent, Actual_Ent);
6835 Next_Non_Pragma (Formal_Node);
6838 -- No further formals to match, but the generic
6839 -- part may contain inherited operation that are
6840 -- not hidden in the enclosing instance.
6842 Next_Entity (Actual_Ent);
6848 -- If the formal is not declared with a box, reanalyze it as
6849 -- an instantiation, to verify the matching rules of 12.7. The
6850 -- actual checks are performed after the generic associations
6855 I_Pack : constant Entity_Id :=
6856 Make_Defining_Identifier (Sloc (Actual),
6857 Chars => New_Internal_Name ('P'));
6860 Set_Is_Internal (I_Pack);
6863 Make_Package_Instantiation (Sloc (Actual),
6864 Defining_Unit_Name => I_Pack,
6865 Name => New_Occurrence_Of (Gen_Parent, Sloc (Actual)),
6866 Generic_Associations =>
6867 Generic_Associations (Formal)));
6873 end Instantiate_Formal_Package;
6875 -----------------------------------
6876 -- Instantiate_Formal_Subprogram --
6877 -----------------------------------
6879 function Instantiate_Formal_Subprogram
6882 Analyzed_Formal : Node_Id) return Node_Id
6884 Loc : Source_Ptr := Sloc (Instantiation_Node);
6885 Formal_Sub : constant Entity_Id :=
6886 Defining_Unit_Name (Specification (Formal));
6887 Analyzed_S : constant Entity_Id :=
6888 Defining_Unit_Name (Specification (Analyzed_Formal));
6889 Decl_Node : Node_Id;
6893 function From_Parent_Scope (Subp : Entity_Id) return Boolean;
6894 -- If the generic is a child unit, the parent has been installed on the
6895 -- scope stack, but a default subprogram cannot resolve to something on
6896 -- the parent because that parent is not really part of the visible
6897 -- context (it is there to resolve explicit local entities). If the
6898 -- default has resolved in this way, we remove the entity from
6899 -- immediate visibility and analyze the node again to emit an error
6900 -- message or find another visible candidate.
6902 procedure Valid_Actual_Subprogram (Act : Node_Id);
6903 -- Perform legality check and raise exception on failure
6905 -----------------------
6906 -- From_Parent_Scope --
6907 -----------------------
6909 function From_Parent_Scope (Subp : Entity_Id) return Boolean is
6910 Gen_Scope : Node_Id := Scope (Analyzed_S);
6913 while Present (Gen_Scope)
6914 and then Is_Child_Unit (Gen_Scope)
6916 if Scope (Subp) = Scope (Gen_Scope) then
6920 Gen_Scope := Scope (Gen_Scope);
6924 end From_Parent_Scope;
6926 -----------------------------
6927 -- Valid_Actual_Subprogram --
6928 -----------------------------
6930 procedure Valid_Actual_Subprogram (Act : Node_Id) is
6931 Act_E : Entity_Id := Empty;
6934 if Is_Entity_Name (Act) then
6935 Act_E := Entity (Act);
6936 elsif Nkind (Act) = N_Selected_Component
6937 and then Is_Entity_Name (Selector_Name (Act))
6939 Act_E := Entity (Selector_Name (Act));
6942 if (Present (Act_E) and then Is_Overloadable (Act_E))
6943 or else Nkind (Act) = N_Attribute_Reference
6944 or else Nkind (Act) = N_Indexed_Component
6945 or else Nkind (Act) = N_Character_Literal
6946 or else Nkind (Act) = N_Explicit_Dereference
6952 ("expect subprogram or entry name in instantiation of&",
6953 Instantiation_Node, Formal_Sub);
6954 Abandon_Instantiation (Instantiation_Node);
6956 end Valid_Actual_Subprogram;
6958 -- Start of processing for Instantiate_Formal_Subprogram
6961 New_Spec := New_Copy_Tree (Specification (Formal));
6963 -- Create new entity for the actual (New_Copy_Tree does not)
6965 Set_Defining_Unit_Name
6966 (New_Spec, Make_Defining_Identifier (Loc, Chars (Formal_Sub)));
6968 -- Find entity of actual. If the actual is an attribute reference, it
6969 -- cannot be resolved here (its formal is missing) but is handled
6970 -- instead in Attribute_Renaming. If the actual is overloaded, it is
6971 -- fully resolved subsequently, when the renaming declaration for the
6972 -- formal is analyzed. If it is an explicit dereference, resolve the
6973 -- prefix but not the actual itself, to prevent interpretation as a
6976 if Present (Actual) then
6977 Loc := Sloc (Actual);
6978 Set_Sloc (New_Spec, Loc);
6980 if Nkind (Actual) = N_Operator_Symbol then
6981 Find_Direct_Name (Actual);
6983 elsif Nkind (Actual) = N_Explicit_Dereference then
6984 Analyze (Prefix (Actual));
6986 elsif Nkind (Actual) /= N_Attribute_Reference then
6990 Valid_Actual_Subprogram (Actual);
6993 elsif Present (Default_Name (Formal)) then
6994 if Nkind (Default_Name (Formal)) /= N_Attribute_Reference
6995 and then Nkind (Default_Name (Formal)) /= N_Selected_Component
6996 and then Nkind (Default_Name (Formal)) /= N_Indexed_Component
6997 and then Nkind (Default_Name (Formal)) /= N_Character_Literal
6998 and then Present (Entity (Default_Name (Formal)))
7000 Nam := New_Occurrence_Of (Entity (Default_Name (Formal)), Loc);
7002 Nam := New_Copy (Default_Name (Formal));
7003 Set_Sloc (Nam, Loc);
7006 elsif Box_Present (Formal) then
7008 -- Actual is resolved at the point of instantiation. Create
7009 -- an identifier or operator with the same name as the formal.
7011 if Nkind (Formal_Sub) = N_Defining_Operator_Symbol then
7012 Nam := Make_Operator_Symbol (Loc,
7013 Chars => Chars (Formal_Sub),
7014 Strval => No_String);
7016 Nam := Make_Identifier (Loc, Chars (Formal_Sub));
7019 elsif Nkind (Specification (Formal)) = N_Procedure_Specification
7020 and then Null_Present (Specification (Formal))
7022 -- Generate null body for procedure, for use in the instance
7025 Make_Subprogram_Body (Loc,
7026 Specification => New_Spec,
7027 Declarations => New_List,
7028 Handled_Statement_Sequence =>
7029 Make_Handled_Sequence_Of_Statements (Loc,
7030 Statements => New_List (Make_Null_Statement (Loc))));
7032 Set_Is_Intrinsic_Subprogram (Defining_Unit_Name (New_Spec));
7036 Error_Msg_Sloc := Sloc (Scope (Analyzed_S));
7038 ("missing actual&", Instantiation_Node, Formal_Sub);
7040 ("\in instantiation of & declared#",
7041 Instantiation_Node, Scope (Analyzed_S));
7042 Abandon_Instantiation (Instantiation_Node);
7046 Make_Subprogram_Renaming_Declaration (Loc,
7047 Specification => New_Spec,
7050 -- If we do not have an actual and the formal specified <> then
7051 -- set to get proper default.
7053 if No (Actual) and then Box_Present (Formal) then
7054 Set_From_Default (Decl_Node);
7057 -- Gather possible interpretations for the actual before analyzing the
7058 -- instance. If overloaded, it will be resolved when analyzing the
7059 -- renaming declaration.
7061 if Box_Present (Formal)
7062 and then No (Actual)
7066 if Is_Child_Unit (Scope (Analyzed_S))
7067 and then Present (Entity (Nam))
7069 if not Is_Overloaded (Nam) then
7071 if From_Parent_Scope (Entity (Nam)) then
7072 Set_Is_Immediately_Visible (Entity (Nam), False);
7073 Set_Entity (Nam, Empty);
7074 Set_Etype (Nam, Empty);
7078 Set_Is_Immediately_Visible (Entity (Nam));
7087 Get_First_Interp (Nam, I, It);
7089 while Present (It.Nam) loop
7090 if From_Parent_Scope (It.Nam) then
7094 Get_Next_Interp (I, It);
7101 -- The generic instantiation freezes the actual. This can only be
7102 -- done once the actual is resolved, in the analysis of the renaming
7103 -- declaration. To make the formal subprogram entity available, we set
7104 -- Corresponding_Formal_Spec to point to the formal subprogram entity.
7105 -- This is also needed in Analyze_Subprogram_Renaming for the processing
7106 -- of formal abstract subprograms.
7108 Set_Corresponding_Formal_Spec (Decl_Node, Analyzed_S);
7110 -- We cannot analyze the renaming declaration, and thus find the
7111 -- actual, until the all the actuals are assembled in the instance.
7112 -- For subsequent checks of other actuals, indicate the node that
7113 -- will hold the instance of this formal.
7115 Set_Instance_Of (Analyzed_S, Nam);
7117 if Nkind (Actual) = N_Selected_Component
7118 and then Is_Task_Type (Etype (Prefix (Actual)))
7119 and then not Is_Frozen (Etype (Prefix (Actual)))
7121 -- The renaming declaration will create a body, which must appear
7122 -- outside of the instantiation, We move the renaming declaration
7123 -- out of the instance, and create an additional renaming inside,
7124 -- to prevent freezing anomalies.
7127 Anon_Id : constant Entity_Id :=
7128 Make_Defining_Identifier
7129 (Loc, New_Internal_Name ('E'));
7131 Set_Defining_Unit_Name (New_Spec, Anon_Id);
7132 Insert_Before (Instantiation_Node, Decl_Node);
7133 Analyze (Decl_Node);
7135 -- Now create renaming within the instance
7138 Make_Subprogram_Renaming_Declaration (Loc,
7139 Specification => New_Copy_Tree (New_Spec),
7140 Name => New_Occurrence_Of (Anon_Id, Loc));
7142 Set_Defining_Unit_Name (Specification (Decl_Node),
7143 Make_Defining_Identifier (Loc, Chars (Formal_Sub)));
7148 end Instantiate_Formal_Subprogram;
7150 ------------------------
7151 -- Instantiate_Object --
7152 ------------------------
7154 function Instantiate_Object
7157 Analyzed_Formal : Node_Id) return List_Id
7159 Formal_Id : constant Entity_Id := Defining_Identifier (Formal);
7160 Type_Id : constant Node_Id := Subtype_Mark (Formal);
7161 Loc : constant Source_Ptr := Sloc (Actual);
7162 Act_Assoc : constant Node_Id := Parent (Actual);
7163 Orig_Ftyp : constant Entity_Id :=
7164 Etype (Defining_Identifier (Analyzed_Formal));
7165 List : constant List_Id := New_List;
7167 Decl_Node : Node_Id;
7168 Subt_Decl : Node_Id := Empty;
7171 -- Sloc for error message on missing actual
7173 Error_Msg_Sloc := Sloc (Scope (Defining_Identifier (Analyzed_Formal)));
7175 if Get_Instance_Of (Formal_Id) /= Formal_Id then
7176 Error_Msg_N ("duplicate instantiation of generic parameter", Actual);
7179 Set_Parent (List, Parent (Actual));
7183 if Out_Present (Formal) then
7185 -- An IN OUT generic actual must be a name. The instantiation is a
7186 -- renaming declaration. The actual is the name being renamed. We
7187 -- use the actual directly, rather than a copy, because it is not
7188 -- used further in the list of actuals, and because a copy or a use
7189 -- of relocate_node is incorrect if the instance is nested within a
7190 -- generic. In order to simplify ASIS searches, the Generic_Parent
7191 -- field links the declaration to the generic association.
7196 Instantiation_Node, Formal_Id);
7198 ("\in instantiation of & declared#",
7200 Scope (Defining_Identifier (Analyzed_Formal)));
7201 Abandon_Instantiation (Instantiation_Node);
7205 Make_Object_Renaming_Declaration (Loc,
7206 Defining_Identifier => New_Copy (Formal_Id),
7207 Subtype_Mark => New_Copy_Tree (Type_Id),
7210 Set_Corresponding_Generic_Association (Decl_Node, Act_Assoc);
7212 -- The analysis of the actual may produce insert_action nodes, so
7213 -- the declaration must have a context in which to attach them.
7215 Append (Decl_Node, List);
7218 -- Return if the analysis of the actual reported some error
7220 if Etype (Actual) = Any_Type then
7224 -- This check is performed here because Analyze_Object_Renaming
7225 -- will not check it when Comes_From_Source is False. Note
7226 -- though that the check for the actual being the name of an
7227 -- object will be performed in Analyze_Object_Renaming.
7229 if Is_Object_Reference (Actual)
7230 and then Is_Dependent_Component_Of_Mutable_Object (Actual)
7233 ("illegal discriminant-dependent component for in out parameter",
7237 -- The actual has to be resolved in order to check that it is
7238 -- a variable (due to cases such as F(1), where F returns
7239 -- access to an array, and for overloaded prefixes).
7242 Get_Instance_Of (Etype (Defining_Identifier (Analyzed_Formal)));
7244 if Is_Private_Type (Ftyp)
7245 and then not Is_Private_Type (Etype (Actual))
7246 and then (Base_Type (Full_View (Ftyp)) = Base_Type (Etype (Actual))
7247 or else Base_Type (Etype (Actual)) = Ftyp)
7249 -- If the actual has the type of the full view of the formal,
7250 -- or else a non-private subtype of the formal, then
7251 -- the visibility of the formal type has changed. Add to the
7252 -- actuals a subtype declaration that will force the exchange
7253 -- of views in the body of the instance as well.
7256 Make_Subtype_Declaration (Loc,
7257 Defining_Identifier =>
7258 Make_Defining_Identifier (Loc, New_Internal_Name ('P')),
7259 Subtype_Indication => New_Occurrence_Of (Ftyp, Loc));
7261 Prepend (Subt_Decl, List);
7263 Append_Elmt (Full_View (Ftyp), Exchanged_Views);
7264 Exchange_Declarations (Ftyp);
7267 Resolve (Actual, Ftyp);
7269 if not Is_Variable (Actual) or else Paren_Count (Actual) > 0 then
7271 ("actual for& must be a variable", Actual, Formal_Id);
7273 elsif Base_Type (Ftyp) /= Base_Type (Etype (Actual)) then
7275 "type of actual does not match type of&", Actual, Formal_Id);
7279 Note_Possible_Modification (Actual);
7281 -- Check for instantiation of atomic/volatile actual for
7282 -- non-atomic/volatile formal (RM C.6 (12)).
7284 if Is_Atomic_Object (Actual)
7285 and then not Is_Atomic (Orig_Ftyp)
7288 ("cannot instantiate non-atomic formal object " &
7289 "with atomic actual", Actual);
7291 elsif Is_Volatile_Object (Actual)
7292 and then not Is_Volatile (Orig_Ftyp)
7295 ("cannot instantiate non-volatile formal object " &
7296 "with volatile actual", Actual);
7302 -- The instantiation of a generic formal in-parameter
7303 -- is a constant declaration. The actual is the expression for
7304 -- that declaration.
7306 if Present (Actual) then
7308 Decl_Node := Make_Object_Declaration (Loc,
7309 Defining_Identifier => New_Copy (Formal_Id),
7310 Constant_Present => True,
7311 Object_Definition => New_Copy_Tree (Type_Id),
7312 Expression => Actual);
7314 Set_Corresponding_Generic_Association (Decl_Node, Act_Assoc);
7316 -- A generic formal object of a tagged type is defined
7317 -- to be aliased so the new constant must also be treated
7321 (Etype (Defining_Identifier (Analyzed_Formal)))
7323 Set_Aliased_Present (Decl_Node);
7326 Append (Decl_Node, List);
7328 -- No need to repeat (pre-)analysis of some expression nodes
7329 -- already handled in Pre_Analyze_Actuals.
7331 if Nkind (Actual) /= N_Allocator then
7334 -- Return if the analysis of the actual reported some error
7336 if Etype (Actual) = Any_Type then
7342 Typ : constant Entity_Id :=
7344 (Etype (Defining_Identifier (Analyzed_Formal)));
7347 Freeze_Before (Instantiation_Node, Typ);
7349 -- If the actual is an aggregate, perform name resolution on
7350 -- its components (the analysis of an aggregate does not do
7351 -- it) to capture local names that may be hidden if the
7352 -- generic is a child unit.
7354 if Nkind (Actual) = N_Aggregate then
7355 Pre_Analyze_And_Resolve (Actual, Typ);
7359 elsif Present (Expression (Formal)) then
7361 -- Use default to construct declaration
7364 Make_Object_Declaration (Sloc (Formal),
7365 Defining_Identifier => New_Copy (Formal_Id),
7366 Constant_Present => True,
7367 Object_Definition => New_Copy (Type_Id),
7368 Expression => New_Copy_Tree (Expression (Formal)));
7370 Append (Decl_Node, List);
7371 Set_Analyzed (Expression (Decl_Node), False);
7376 Instantiation_Node, Formal_Id);
7377 Error_Msg_NE ("\in instantiation of & declared#",
7379 Scope (Defining_Identifier (Analyzed_Formal)));
7382 (Etype (Defining_Identifier (Analyzed_Formal)))
7384 -- Create dummy constant declaration so that instance can
7385 -- be analyzed, to minimize cascaded visibility errors.
7388 Make_Object_Declaration (Loc,
7389 Defining_Identifier => New_Copy (Formal_Id),
7390 Constant_Present => True,
7391 Object_Definition => New_Copy (Type_Id),
7393 Make_Attribute_Reference (Sloc (Formal_Id),
7394 Attribute_Name => Name_First,
7395 Prefix => New_Copy (Type_Id)));
7397 Append (Decl_Node, List);
7400 Abandon_Instantiation (Instantiation_Node);
7407 end Instantiate_Object;
7409 ------------------------------
7410 -- Instantiate_Package_Body --
7411 ------------------------------
7413 procedure Instantiate_Package_Body
7414 (Body_Info : Pending_Body_Info;
7415 Inlined_Body : Boolean := False)
7417 Act_Decl : constant Node_Id := Body_Info.Act_Decl;
7418 Inst_Node : constant Node_Id := Body_Info.Inst_Node;
7419 Loc : constant Source_Ptr := Sloc (Inst_Node);
7421 Gen_Id : constant Node_Id := Name (Inst_Node);
7422 Gen_Unit : constant Entity_Id := Get_Generic_Entity (Inst_Node);
7423 Gen_Decl : constant Node_Id := Unit_Declaration_Node (Gen_Unit);
7424 Act_Spec : constant Node_Id := Specification (Act_Decl);
7425 Act_Decl_Id : constant Entity_Id := Defining_Entity (Act_Spec);
7427 Act_Body_Name : Node_Id;
7429 Gen_Body_Id : Node_Id;
7431 Act_Body_Id : Entity_Id;
7433 Parent_Installed : Boolean := False;
7434 Save_Style_Check : constant Boolean := Style_Check;
7437 Gen_Body_Id := Corresponding_Body (Gen_Decl);
7439 -- The instance body may already have been processed, as the parent
7440 -- of another instance that is inlined. (Load_Parent_Of_Generic).
7442 if Present (Corresponding_Body (Instance_Spec (Inst_Node))) then
7446 Expander_Mode_Save_And_Set (Body_Info.Expander_Status);
7448 if No (Gen_Body_Id) then
7449 Load_Parent_Of_Generic (Inst_Node, Specification (Gen_Decl));
7450 Gen_Body_Id := Corresponding_Body (Gen_Decl);
7453 -- Establish global variable for sloc adjustment and for error
7456 Instantiation_Node := Inst_Node;
7458 if Present (Gen_Body_Id) then
7459 Save_Env (Gen_Unit, Act_Decl_Id);
7460 Style_Check := False;
7461 Current_Sem_Unit := Body_Info.Current_Sem_Unit;
7463 Gen_Body := Unit_Declaration_Node (Gen_Body_Id);
7465 Create_Instantiation_Source
7466 (Inst_Node, Gen_Body_Id, False, S_Adjustment);
7470 (Original_Node (Gen_Body), Empty, Instantiating => True);
7472 -- Build new name (possibly qualified) for body declaration
7474 Act_Body_Id := New_Copy (Act_Decl_Id);
7476 -- Some attributes of the spec entity are not inherited by the
7479 Set_Handler_Records (Act_Body_Id, No_List);
7481 if Nkind (Defining_Unit_Name (Act_Spec)) =
7482 N_Defining_Program_Unit_Name
7485 Make_Defining_Program_Unit_Name (Loc,
7486 Name => New_Copy_Tree (Name (Defining_Unit_Name (Act_Spec))),
7487 Defining_Identifier => Act_Body_Id);
7489 Act_Body_Name := Act_Body_Id;
7492 Set_Defining_Unit_Name (Act_Body, Act_Body_Name);
7494 Set_Corresponding_Spec (Act_Body, Act_Decl_Id);
7495 Check_Generic_Actuals (Act_Decl_Id, False);
7497 -- If it is a child unit, make the parent instance (which is an
7498 -- instance of the parent of the generic) visible. The parent
7499 -- instance is the prefix of the name of the generic unit.
7501 if Ekind (Scope (Gen_Unit)) = E_Generic_Package
7502 and then Nkind (Gen_Id) = N_Expanded_Name
7504 Install_Parent (Entity (Prefix (Gen_Id)), In_Body => True);
7505 Parent_Installed := True;
7507 elsif Is_Child_Unit (Gen_Unit) then
7508 Install_Parent (Scope (Gen_Unit), In_Body => True);
7509 Parent_Installed := True;
7512 -- If the instantiation is a library unit, and this is the main
7513 -- unit, then build the resulting compilation unit nodes for the
7514 -- instance. If this is a compilation unit but it is not the main
7515 -- unit, then it is the body of a unit in the context, that is being
7516 -- compiled because it is encloses some inlined unit or another
7517 -- generic unit being instantiated. In that case, this body is not
7518 -- part of the current compilation, and is not attached to the tree,
7519 -- but its parent must be set for analysis.
7521 if Nkind (Parent (Inst_Node)) = N_Compilation_Unit then
7523 -- Replace instance node with body of instance, and create
7524 -- new node for corresponding instance declaration.
7526 Build_Instance_Compilation_Unit_Nodes
7527 (Inst_Node, Act_Body, Act_Decl);
7528 Analyze (Inst_Node);
7530 if Parent (Inst_Node) = Cunit (Main_Unit) then
7532 -- If the instance is a child unit itself, then set the
7533 -- scope of the expanded body to be the parent of the
7534 -- instantiation (ensuring that the fully qualified name
7535 -- will be generated for the elaboration subprogram).
7537 if Nkind (Defining_Unit_Name (Act_Spec)) =
7538 N_Defining_Program_Unit_Name
7541 (Defining_Entity (Inst_Node), Scope (Act_Decl_Id));
7545 -- Case where instantiation is not a library unit
7548 -- If this is an early instantiation, i.e. appears textually
7549 -- before the corresponding body and must be elaborated first,
7550 -- indicate that the body instance is to be delayed.
7552 Install_Body (Act_Body, Inst_Node, Gen_Body, Gen_Decl);
7554 -- Now analyze the body. We turn off all checks if this is
7555 -- an internal unit, since there is no reason to have checks
7556 -- on for any predefined run-time library code. All such
7557 -- code is designed to be compiled with checks off.
7559 -- Note that we do NOT apply this criterion to children of
7560 -- GNAT (or on VMS, children of DEC). The latter units must
7561 -- suppress checks explicitly if this is needed.
7563 if Is_Predefined_File_Name
7564 (Unit_File_Name (Get_Source_Unit (Gen_Decl)))
7566 Analyze (Act_Body, Suppress => All_Checks);
7572 if not Generic_Separately_Compiled (Gen_Unit) then
7573 Inherit_Context (Gen_Body, Inst_Node);
7576 -- Remove the parent instances if they have been placed on the
7577 -- scope stack to compile the body.
7579 if Parent_Installed then
7580 Remove_Parent (In_Body => True);
7583 Restore_Private_Views (Act_Decl_Id);
7585 -- Remove the current unit from visibility if this is an instance
7586 -- that is not elaborated on the fly for inlining purposes.
7588 if not Inlined_Body then
7589 Set_Is_Immediately_Visible (Act_Decl_Id, False);
7593 Style_Check := Save_Style_Check;
7595 -- If we have no body, and the unit requires a body, then complain.
7596 -- This complaint is suppressed if we have detected other errors
7597 -- (since a common reason for missing the body is that it had errors).
7599 elsif Unit_Requires_Body (Gen_Unit) then
7600 if Serious_Errors_Detected = 0 then
7602 ("cannot find body of generic package &", Inst_Node, Gen_Unit);
7604 -- Don't attempt to perform any cleanup actions if some other
7605 -- error was aready detected, since this can cause blowups.
7611 -- Case of package that does not need a body
7614 -- If the instantiation of the declaration is a library unit,
7615 -- rewrite the original package instantiation as a package
7616 -- declaration in the compilation unit node.
7618 if Nkind (Parent (Inst_Node)) = N_Compilation_Unit then
7619 Set_Parent_Spec (Act_Decl, Parent_Spec (Inst_Node));
7620 Rewrite (Inst_Node, Act_Decl);
7622 -- Generate elaboration entity, in case spec has elaboration
7623 -- code. This cannot be done when the instance is analyzed,
7624 -- because it is not known yet whether the body exists.
7626 Set_Elaboration_Entity_Required (Act_Decl_Id, False);
7627 Build_Elaboration_Entity (Parent (Inst_Node), Act_Decl_Id);
7629 -- If the instantiation is not a library unit, then append the
7630 -- declaration to the list of implicitly generated entities.
7631 -- unless it is already a list member which means that it was
7632 -- already processed
7634 elsif not Is_List_Member (Act_Decl) then
7635 Mark_Rewrite_Insertion (Act_Decl);
7636 Insert_Before (Inst_Node, Act_Decl);
7640 Expander_Mode_Restore;
7641 end Instantiate_Package_Body;
7643 ---------------------------------
7644 -- Instantiate_Subprogram_Body --
7645 ---------------------------------
7647 procedure Instantiate_Subprogram_Body
7648 (Body_Info : Pending_Body_Info)
7650 Act_Decl : constant Node_Id := Body_Info.Act_Decl;
7651 Inst_Node : constant Node_Id := Body_Info.Inst_Node;
7652 Loc : constant Source_Ptr := Sloc (Inst_Node);
7653 Gen_Id : constant Node_Id := Name (Inst_Node);
7654 Gen_Unit : constant Entity_Id := Get_Generic_Entity (Inst_Node);
7655 Gen_Decl : constant Node_Id := Unit_Declaration_Node (Gen_Unit);
7656 Anon_Id : constant Entity_Id :=
7657 Defining_Unit_Name (Specification (Act_Decl));
7658 Pack_Id : constant Entity_Id :=
7659 Defining_Unit_Name (Parent (Act_Decl));
7662 Gen_Body_Id : Node_Id;
7664 Act_Body_Id : Entity_Id;
7665 Pack_Body : Node_Id;
7666 Prev_Formal : Entity_Id;
7668 Unit_Renaming : Node_Id;
7670 Parent_Installed : Boolean := False;
7671 Save_Style_Check : constant Boolean := Style_Check;
7674 Gen_Body_Id := Corresponding_Body (Gen_Decl);
7676 Expander_Mode_Save_And_Set (Body_Info.Expander_Status);
7678 if No (Gen_Body_Id) then
7679 Load_Parent_Of_Generic (Inst_Node, Specification (Gen_Decl));
7680 Gen_Body_Id := Corresponding_Body (Gen_Decl);
7683 Instantiation_Node := Inst_Node;
7685 if Present (Gen_Body_Id) then
7686 Gen_Body := Unit_Declaration_Node (Gen_Body_Id);
7688 if Nkind (Gen_Body) = N_Subprogram_Body_Stub then
7690 -- Either body is not present, or context is non-expanding, as
7691 -- when compiling a subunit. Mark the instance as completed, and
7692 -- diagnose a missing body when needed.
7695 and then Operating_Mode = Generate_Code
7698 ("missing proper body for instantiation", Gen_Body);
7701 Set_Has_Completion (Anon_Id);
7705 Save_Env (Gen_Unit, Anon_Id);
7706 Style_Check := False;
7707 Current_Sem_Unit := Body_Info.Current_Sem_Unit;
7708 Create_Instantiation_Source
7716 (Original_Node (Gen_Body), Empty, Instantiating => True);
7717 Act_Body_Id := Defining_Entity (Act_Body);
7718 Set_Chars (Act_Body_Id, Chars (Anon_Id));
7719 Set_Sloc (Act_Body_Id, Sloc (Defining_Entity (Inst_Node)));
7720 Set_Corresponding_Spec (Act_Body, Anon_Id);
7721 Set_Has_Completion (Anon_Id);
7722 Check_Generic_Actuals (Pack_Id, False);
7724 -- If it is a child unit, make the parent instance (which is an
7725 -- instance of the parent of the generic) visible. The parent
7726 -- instance is the prefix of the name of the generic unit.
7728 if Ekind (Scope (Gen_Unit)) = E_Generic_Package
7729 and then Nkind (Gen_Id) = N_Expanded_Name
7731 Install_Parent (Entity (Prefix (Gen_Id)), In_Body => True);
7732 Parent_Installed := True;
7734 elsif Is_Child_Unit (Gen_Unit) then
7735 Install_Parent (Scope (Gen_Unit), In_Body => True);
7736 Parent_Installed := True;
7739 -- Inside its body, a reference to the generic unit is a reference
7740 -- to the instance. The corresponding renaming is the first
7741 -- declaration in the body.
7744 Make_Subprogram_Renaming_Declaration (Loc,
7747 Specification (Original_Node (Gen_Body)),
7749 Instantiating => True),
7750 Name => New_Occurrence_Of (Anon_Id, Loc));
7752 -- If there is a formal subprogram with the same name as the
7753 -- unit itself, do not add this renaming declaration. This is
7754 -- a temporary fix for one ACVC test. ???
7756 Prev_Formal := First_Entity (Pack_Id);
7757 while Present (Prev_Formal) loop
7758 if Chars (Prev_Formal) = Chars (Gen_Unit)
7759 and then Is_Overloadable (Prev_Formal)
7764 Next_Entity (Prev_Formal);
7767 if Present (Prev_Formal) then
7768 Decls := New_List (Act_Body);
7770 Decls := New_List (Unit_Renaming, Act_Body);
7773 -- The subprogram body is placed in the body of a dummy package
7774 -- body, whose spec contains the subprogram declaration as well
7775 -- as the renaming declarations for the generic parameters.
7777 Pack_Body := Make_Package_Body (Loc,
7778 Defining_Unit_Name => New_Copy (Pack_Id),
7779 Declarations => Decls);
7781 Set_Corresponding_Spec (Pack_Body, Pack_Id);
7783 -- If the instantiation is a library unit, then build resulting
7784 -- compilation unit nodes for the instance. The declaration of
7785 -- the enclosing package is the grandparent of the subprogram
7786 -- declaration. First replace the instantiation node as the unit
7787 -- of the corresponding compilation.
7789 if Nkind (Parent (Inst_Node)) = N_Compilation_Unit then
7790 if Parent (Inst_Node) = Cunit (Main_Unit) then
7791 Set_Unit (Parent (Inst_Node), Inst_Node);
7792 Build_Instance_Compilation_Unit_Nodes
7793 (Inst_Node, Pack_Body, Parent (Parent (Act_Decl)));
7794 Analyze (Inst_Node);
7796 Set_Parent (Pack_Body, Parent (Inst_Node));
7797 Analyze (Pack_Body);
7801 Insert_Before (Inst_Node, Pack_Body);
7802 Mark_Rewrite_Insertion (Pack_Body);
7803 Analyze (Pack_Body);
7805 if Expander_Active then
7806 Freeze_Subprogram_Body (Inst_Node, Gen_Body, Pack_Id);
7810 if not Generic_Separately_Compiled (Gen_Unit) then
7811 Inherit_Context (Gen_Body, Inst_Node);
7814 Restore_Private_Views (Pack_Id, False);
7816 if Parent_Installed then
7817 Remove_Parent (In_Body => True);
7821 Style_Check := Save_Style_Check;
7823 -- Body not found. Error was emitted already. If there were no
7824 -- previous errors, this may be an instance whose scope is a premature
7825 -- instance. In that case we must insure that the (legal) program does
7826 -- raise program error if executed. We generate a subprogram body for
7827 -- this purpose. See DEC ac30vso.
7829 elsif Serious_Errors_Detected = 0
7830 and then Nkind (Parent (Inst_Node)) /= N_Compilation_Unit
7832 if Ekind (Anon_Id) = E_Procedure then
7834 Make_Subprogram_Body (Loc,
7836 Make_Procedure_Specification (Loc,
7837 Defining_Unit_Name => New_Copy (Anon_Id),
7838 Parameter_Specifications =>
7840 (Parameter_Specifications (Parent (Anon_Id)))),
7842 Declarations => Empty_List,
7843 Handled_Statement_Sequence =>
7844 Make_Handled_Sequence_Of_Statements (Loc,
7847 Make_Raise_Program_Error (Loc,
7849 PE_Access_Before_Elaboration))));
7853 Make_Raise_Program_Error (Loc,
7854 Reason => PE_Access_Before_Elaboration);
7856 Set_Etype (Ret_Expr, (Etype (Anon_Id)));
7857 Set_Analyzed (Ret_Expr);
7860 Make_Subprogram_Body (Loc,
7862 Make_Function_Specification (Loc,
7863 Defining_Unit_Name => New_Copy (Anon_Id),
7864 Parameter_Specifications =>
7866 (Parameter_Specifications (Parent (Anon_Id))),
7868 New_Occurrence_Of (Etype (Anon_Id), Loc)),
7870 Declarations => Empty_List,
7871 Handled_Statement_Sequence =>
7872 Make_Handled_Sequence_Of_Statements (Loc,
7874 New_List (Make_Return_Statement (Loc, Ret_Expr))));
7877 Pack_Body := Make_Package_Body (Loc,
7878 Defining_Unit_Name => New_Copy (Pack_Id),
7879 Declarations => New_List (Act_Body));
7881 Insert_After (Inst_Node, Pack_Body);
7882 Set_Corresponding_Spec (Pack_Body, Pack_Id);
7883 Analyze (Pack_Body);
7886 Expander_Mode_Restore;
7887 end Instantiate_Subprogram_Body;
7889 ----------------------
7890 -- Instantiate_Type --
7891 ----------------------
7893 function Instantiate_Type
7896 Analyzed_Formal : Node_Id;
7897 Actual_Decls : List_Id) return Node_Id
7899 Loc : constant Source_Ptr := Sloc (Actual);
7900 Gen_T : constant Entity_Id := Defining_Identifier (Formal);
7901 A_Gen_T : constant Entity_Id := Defining_Identifier (Analyzed_Formal);
7902 Ancestor : Entity_Id := Empty;
7903 Def : constant Node_Id := Formal_Type_Definition (Formal);
7905 Decl_Node : Node_Id;
7907 procedure Validate_Array_Type_Instance;
7908 procedure Validate_Access_Subprogram_Instance;
7909 procedure Validate_Access_Type_Instance;
7910 procedure Validate_Derived_Type_Instance;
7911 procedure Validate_Private_Type_Instance;
7912 -- These procedures perform validation tests for the named case
7914 function Subtypes_Match (Gen_T, Act_T : Entity_Id) return Boolean;
7915 -- Check that base types are the same and that the subtypes match
7916 -- statically. Used in several of the above.
7918 --------------------
7919 -- Subtypes_Match --
7920 --------------------
7922 function Subtypes_Match (Gen_T, Act_T : Entity_Id) return Boolean is
7923 T : constant Entity_Id := Get_Instance_Of (Gen_T);
7926 return (Base_Type (T) = Base_Type (Act_T)
7927 -- why is the and then commented out here???
7928 -- and then Is_Constrained (T) = Is_Constrained (Act_T)
7929 and then Subtypes_Statically_Match (T, Act_T))
7931 or else (Is_Class_Wide_Type (Gen_T)
7932 and then Is_Class_Wide_Type (Act_T)
7935 Get_Instance_Of (Root_Type (Gen_T)),
7936 Root_Type (Act_T)));
7939 -----------------------------------------
7940 -- Validate_Access_Subprogram_Instance --
7941 -----------------------------------------
7943 procedure Validate_Access_Subprogram_Instance is
7945 if not Is_Access_Type (Act_T)
7946 or else Ekind (Designated_Type (Act_T)) /= E_Subprogram_Type
7949 ("expect access type in instantiation of &", Actual, Gen_T);
7950 Abandon_Instantiation (Actual);
7953 Check_Mode_Conformant
7954 (Designated_Type (Act_T),
7955 Designated_Type (A_Gen_T),
7959 if Ekind (Base_Type (Act_T)) = E_Access_Protected_Subprogram_Type then
7960 if Ekind (A_Gen_T) = E_Access_Subprogram_Type then
7962 ("protected access type not allowed for formal &",
7966 elsif Ekind (A_Gen_T) = E_Access_Protected_Subprogram_Type then
7968 ("expect protected access type for formal &",
7971 end Validate_Access_Subprogram_Instance;
7973 -----------------------------------
7974 -- Validate_Access_Type_Instance --
7975 -----------------------------------
7977 procedure Validate_Access_Type_Instance is
7978 Desig_Type : constant Entity_Id :=
7980 (Designated_Type (A_Gen_T), Scope (A_Gen_T));
7983 if not Is_Access_Type (Act_T) then
7985 ("expect access type in instantiation of &", Actual, Gen_T);
7986 Abandon_Instantiation (Actual);
7989 if Is_Access_Constant (A_Gen_T) then
7990 if not Is_Access_Constant (Act_T) then
7992 ("actual type must be access-to-constant type", Actual);
7993 Abandon_Instantiation (Actual);
7996 if Is_Access_Constant (Act_T) then
7998 ("actual type must be access-to-variable type", Actual);
7999 Abandon_Instantiation (Actual);
8001 elsif Ekind (A_Gen_T) = E_General_Access_Type
8002 and then Ekind (Base_Type (Act_T)) /= E_General_Access_Type
8004 Error_Msg_N ("actual must be general access type!", Actual);
8005 Error_Msg_NE ("add ALL to }!", Actual, Act_T);
8006 Abandon_Instantiation (Actual);
8010 -- The designated subtypes, that is to say the subtypes introduced
8011 -- by an access type declaration (and not by a subtype declaration)
8014 if not Subtypes_Match
8015 (Desig_Type, Designated_Type (Base_Type (Act_T)))
8018 ("designated type of actual does not match that of formal &",
8020 Abandon_Instantiation (Actual);
8022 elsif Is_Access_Type (Designated_Type (Act_T))
8023 and then Is_Constrained (Designated_Type (Designated_Type (Act_T)))
8025 Is_Constrained (Designated_Type (Desig_Type))
8028 ("designated type of actual does not match that of formal &",
8030 Abandon_Instantiation (Actual);
8032 end Validate_Access_Type_Instance;
8034 ----------------------------------
8035 -- Validate_Array_Type_Instance --
8036 ----------------------------------
8038 procedure Validate_Array_Type_Instance is
8043 function Formal_Dimensions return Int;
8044 -- Count number of dimensions in array type formal
8046 -----------------------
8047 -- Formal_Dimensions --
8048 -----------------------
8050 function Formal_Dimensions return Int is
8055 if Nkind (Def) = N_Constrained_Array_Definition then
8056 Index := First (Discrete_Subtype_Definitions (Def));
8058 Index := First (Subtype_Marks (Def));
8061 while Present (Index) loop
8067 end Formal_Dimensions;
8069 -- Start of processing for Validate_Array_Type_Instance
8072 if not Is_Array_Type (Act_T) then
8074 ("expect array type in instantiation of &", Actual, Gen_T);
8075 Abandon_Instantiation (Actual);
8077 elsif Nkind (Def) = N_Constrained_Array_Definition then
8078 if not (Is_Constrained (Act_T)) then
8080 ("expect constrained array in instantiation of &",
8082 Abandon_Instantiation (Actual);
8086 if Is_Constrained (Act_T) then
8088 ("expect unconstrained array in instantiation of &",
8090 Abandon_Instantiation (Actual);
8094 if Formal_Dimensions /= Number_Dimensions (Act_T) then
8096 ("dimensions of actual do not match formal &", Actual, Gen_T);
8097 Abandon_Instantiation (Actual);
8100 I1 := First_Index (A_Gen_T);
8101 I2 := First_Index (Act_T);
8102 for J in 1 .. Formal_Dimensions loop
8104 -- If the indices of the actual were given by a subtype_mark,
8105 -- the index was transformed into a range attribute. Retrieve
8106 -- the original type mark for checking.
8108 if Is_Entity_Name (Original_Node (I2)) then
8109 T2 := Entity (Original_Node (I2));
8114 if not Subtypes_Match
8115 (Find_Actual_Type (Etype (I1), Scope (A_Gen_T)), T2)
8118 ("index types of actual do not match those of formal &",
8120 Abandon_Instantiation (Actual);
8127 if not Subtypes_Match (
8128 Find_Actual_Type (Component_Type (A_Gen_T), Scope (A_Gen_T)),
8129 Component_Type (Act_T))
8132 ("component subtype of actual does not match that of formal &",
8134 Abandon_Instantiation (Actual);
8137 if Has_Aliased_Components (A_Gen_T)
8138 and then not Has_Aliased_Components (Act_T)
8141 ("actual must have aliased components to match formal type &",
8145 end Validate_Array_Type_Instance;
8147 ------------------------------------
8148 -- Validate_Derived_Type_Instance --
8149 ------------------------------------
8151 procedure Validate_Derived_Type_Instance is
8152 Actual_Discr : Entity_Id;
8153 Ancestor_Discr : Entity_Id;
8156 -- If the parent type in the generic declaration is itself
8157 -- a previous formal type, then it is local to the generic
8158 -- and absent from the analyzed generic definition. In that
8159 -- case the ancestor is the instance of the formal (which must
8160 -- have been instantiated previously), unless the ancestor is
8161 -- itself a formal derived type. In this latter case (which is the
8162 -- subject of Corrigendum 8652/0038 (AI-202) the ancestor of the
8163 -- formals is the ancestor of its parent. Otherwise, the analyzed
8164 -- generic carries the parent type. If the parent type is defined
8165 -- in a previous formal package, then the scope of that formal
8166 -- package is that of the generic type itself, and it has already
8167 -- been mapped into the corresponding type in the actual package.
8169 -- Common case: parent type defined outside of the generic
8171 if Is_Entity_Name (Subtype_Mark (Def))
8172 and then Present (Entity (Subtype_Mark (Def)))
8174 Ancestor := Get_Instance_Of (Entity (Subtype_Mark (Def)));
8176 -- Check whether parent is defined in a previous formal package
8179 Scope (Scope (Base_Type (Etype (A_Gen_T)))) = Scope (A_Gen_T)
8182 Get_Instance_Of (Base_Type (Etype (A_Gen_T)));
8184 -- The type may be a local derivation, or a type extension of
8185 -- a previous formal, or of a formal of a parent package.
8187 elsif Is_Derived_Type (Get_Instance_Of (A_Gen_T))
8189 Ekind (Get_Instance_Of (A_Gen_T)) = E_Record_Type_With_Private
8191 -- Check whether the parent is another derived formal type
8192 -- in the same generic unit.
8194 if Etype (A_Gen_T) /= A_Gen_T
8195 and then Is_Generic_Type (Etype (A_Gen_T))
8196 and then Scope (Etype (A_Gen_T)) = Scope (A_Gen_T)
8197 and then Etype (Etype (A_Gen_T)) /= Etype (A_Gen_T)
8199 -- Locate ancestor of parent from the subtype declaration
8200 -- created for the actual.
8206 Decl := First (Actual_Decls);
8207 while Present (Decl) loop
8208 if Nkind (Decl) = N_Subtype_Declaration
8209 and then Chars (Defining_Identifier (Decl)) =
8210 Chars (Etype (A_Gen_T))
8212 Ancestor := Generic_Parent_Type (Decl);
8220 pragma Assert (Present (Ancestor));
8224 Get_Instance_Of (Base_Type (Get_Instance_Of (A_Gen_T)));
8228 Ancestor := Get_Instance_Of (Etype (Base_Type (A_Gen_T)));
8231 -- Ada 2005 (AI-251)
8233 if Ada_Version >= Ada_05
8234 and then Is_Interface (Ancestor)
8236 if not Interface_Present_In_Ancestor (Act_T, Ancestor) then
8238 ("(Ada 2005) expected type implementing & in instantiation",
8242 elsif not Is_Ancestor (Base_Type (Ancestor), Act_T) then
8244 ("expect type derived from & in instantiation",
8245 Actual, First_Subtype (Ancestor));
8246 Abandon_Instantiation (Actual);
8249 -- Perform atomic/volatile checks (RM C.6(12))
8251 if Is_Atomic (Act_T) and then not Is_Atomic (Ancestor) then
8253 ("cannot have atomic actual type for non-atomic formal type",
8256 elsif Is_Volatile (Act_T)
8257 and then not Is_Volatile (Ancestor)
8258 and then Is_By_Reference_Type (Ancestor)
8261 ("cannot have volatile actual type for non-volatile formal type",
8265 -- It should not be necessary to check for unknown discriminants
8266 -- on Formal, but for some reason Has_Unknown_Discriminants is
8267 -- false for A_Gen_T, so Is_Indefinite_Subtype incorrectly
8268 -- returns False. This needs fixing. ???
8270 if not Is_Indefinite_Subtype (A_Gen_T)
8271 and then not Unknown_Discriminants_Present (Formal)
8272 and then Is_Indefinite_Subtype (Act_T)
8275 ("actual subtype must be constrained", Actual);
8276 Abandon_Instantiation (Actual);
8279 if not Unknown_Discriminants_Present (Formal) then
8280 if Is_Constrained (Ancestor) then
8281 if not Is_Constrained (Act_T) then
8283 ("actual subtype must be constrained", Actual);
8284 Abandon_Instantiation (Actual);
8287 -- Ancestor is unconstrained
8289 elsif Is_Constrained (Act_T) then
8290 if Ekind (Ancestor) = E_Access_Type
8291 or else Is_Composite_Type (Ancestor)
8294 ("actual subtype must be unconstrained", Actual);
8295 Abandon_Instantiation (Actual);
8298 -- A class-wide type is only allowed if the formal has
8299 -- unknown discriminants.
8301 elsif Is_Class_Wide_Type (Act_T)
8302 and then not Has_Unknown_Discriminants (Ancestor)
8305 ("actual for & cannot be a class-wide type", Actual, Gen_T);
8306 Abandon_Instantiation (Actual);
8308 -- Otherwise, the formal and actual shall have the same
8309 -- number of discriminants and each discriminant of the
8310 -- actual must correspond to a discriminant of the formal.
8312 elsif Has_Discriminants (Act_T)
8313 and then not Has_Unknown_Discriminants (Act_T)
8314 and then Has_Discriminants (Ancestor)
8316 Actual_Discr := First_Discriminant (Act_T);
8317 Ancestor_Discr := First_Discriminant (Ancestor);
8318 while Present (Actual_Discr)
8319 and then Present (Ancestor_Discr)
8321 if Base_Type (Act_T) /= Base_Type (Ancestor) and then
8322 not Present (Corresponding_Discriminant (Actual_Discr))
8325 ("discriminant & does not correspond " &
8326 "to ancestor discriminant", Actual, Actual_Discr);
8327 Abandon_Instantiation (Actual);
8330 Next_Discriminant (Actual_Discr);
8331 Next_Discriminant (Ancestor_Discr);
8334 if Present (Actual_Discr) or else Present (Ancestor_Discr) then
8336 ("actual for & must have same number of discriminants",
8338 Abandon_Instantiation (Actual);
8341 -- This case should be caught by the earlier check for
8342 -- for constrainedness, but the check here is added for
8345 elsif Has_Discriminants (Act_T)
8346 and then not Has_Unknown_Discriminants (Act_T)
8349 ("actual for & must not have discriminants", Actual, Gen_T);
8350 Abandon_Instantiation (Actual);
8352 elsif Has_Discriminants (Ancestor) then
8354 ("actual for & must have known discriminants", Actual, Gen_T);
8355 Abandon_Instantiation (Actual);
8358 if not Subtypes_Statically_Compatible (Act_T, Ancestor) then
8360 ("constraint on actual is incompatible with formal", Actual);
8361 Abandon_Instantiation (Actual);
8364 end Validate_Derived_Type_Instance;
8366 ------------------------------------
8367 -- Validate_Private_Type_Instance --
8368 ------------------------------------
8370 procedure Validate_Private_Type_Instance is
8371 Formal_Discr : Entity_Id;
8372 Actual_Discr : Entity_Id;
8373 Formal_Subt : Entity_Id;
8376 if Is_Limited_Type (Act_T)
8377 and then not Is_Limited_Type (A_Gen_T)
8380 ("actual for non-limited & cannot be a limited type", Actual,
8382 Explain_Limited_Type (Act_T, Actual);
8383 Abandon_Instantiation (Actual);
8385 elsif Is_Indefinite_Subtype (Act_T)
8386 and then not Is_Indefinite_Subtype (A_Gen_T)
8387 and then Ada_Version >= Ada_95
8390 ("actual for & must be a definite subtype", Actual, Gen_T);
8392 elsif not Is_Tagged_Type (Act_T)
8393 and then Is_Tagged_Type (A_Gen_T)
8396 ("actual for & must be a tagged type", Actual, Gen_T);
8398 elsif Has_Discriminants (A_Gen_T) then
8399 if not Has_Discriminants (Act_T) then
8401 ("actual for & must have discriminants", Actual, Gen_T);
8402 Abandon_Instantiation (Actual);
8404 elsif Is_Constrained (Act_T) then
8406 ("actual for & must be unconstrained", Actual, Gen_T);
8407 Abandon_Instantiation (Actual);
8410 Formal_Discr := First_Discriminant (A_Gen_T);
8411 Actual_Discr := First_Discriminant (Act_T);
8412 while Formal_Discr /= Empty loop
8413 if Actual_Discr = Empty then
8415 ("discriminants on actual do not match formal",
8417 Abandon_Instantiation (Actual);
8420 Formal_Subt := Get_Instance_Of (Etype (Formal_Discr));
8422 -- Access discriminants match if designated types do
8424 if Ekind (Base_Type (Formal_Subt)) = E_Anonymous_Access_Type
8425 and then (Ekind (Base_Type (Etype (Actual_Discr)))) =
8426 E_Anonymous_Access_Type
8429 (Designated_Type (Base_Type (Formal_Subt))) =
8430 Designated_Type (Base_Type (Etype (Actual_Discr)))
8434 elsif Base_Type (Formal_Subt) /=
8435 Base_Type (Etype (Actual_Discr))
8438 ("types of actual discriminants must match formal",
8440 Abandon_Instantiation (Actual);
8442 elsif not Subtypes_Statically_Match
8443 (Formal_Subt, Etype (Actual_Discr))
8444 and then Ada_Version >= Ada_95
8447 ("subtypes of actual discriminants must match formal",
8449 Abandon_Instantiation (Actual);
8452 Next_Discriminant (Formal_Discr);
8453 Next_Discriminant (Actual_Discr);
8456 if Actual_Discr /= Empty then
8458 ("discriminants on actual do not match formal",
8460 Abandon_Instantiation (Actual);
8467 end Validate_Private_Type_Instance;
8469 -- Start of processing for Instantiate_Type
8472 if Get_Instance_Of (A_Gen_T) /= A_Gen_T then
8473 Error_Msg_N ("duplicate instantiation of generic type", Actual);
8476 elsif not Is_Entity_Name (Actual)
8477 or else not Is_Type (Entity (Actual))
8480 ("expect valid subtype mark to instantiate &", Actual, Gen_T);
8481 Abandon_Instantiation (Actual);
8484 Act_T := Entity (Actual);
8486 -- Ada 2005 (AI-216): An Unchecked_Union subtype shall only be passed
8487 -- as a generic actual parameter if the corresponding formal type
8488 -- does not have a known_discriminant_part, or is a formal derived
8489 -- type that is an Unchecked_Union type.
8491 if Is_Unchecked_Union (Base_Type (Act_T)) then
8492 if not Has_Discriminants (A_Gen_T)
8494 (Is_Derived_Type (A_Gen_T)
8496 Is_Unchecked_Union (A_Gen_T))
8500 Error_Msg_N ("Unchecked_Union cannot be the actual for a" &
8501 " discriminated formal type", Act_T);
8506 -- Deal with fixed/floating restrictions
8508 if Is_Floating_Point_Type (Act_T) then
8509 Check_Restriction (No_Floating_Point, Actual);
8510 elsif Is_Fixed_Point_Type (Act_T) then
8511 Check_Restriction (No_Fixed_Point, Actual);
8514 -- Deal with error of using incomplete type as generic actual
8516 if Ekind (Act_T) = E_Incomplete_Type then
8517 if No (Underlying_Type (Act_T)) then
8518 Error_Msg_N ("premature use of incomplete type", Actual);
8519 Abandon_Instantiation (Actual);
8521 Act_T := Full_View (Act_T);
8522 Set_Entity (Actual, Act_T);
8524 if Has_Private_Component (Act_T) then
8526 ("premature use of type with private component", Actual);
8530 -- Deal with error of premature use of private type as generic actual
8532 elsif Is_Private_Type (Act_T)
8533 and then Is_Private_Type (Base_Type (Act_T))
8534 and then not Is_Generic_Type (Act_T)
8535 and then not Is_Derived_Type (Act_T)
8536 and then No (Full_View (Root_Type (Act_T)))
8538 Error_Msg_N ("premature use of private type", Actual);
8540 elsif Has_Private_Component (Act_T) then
8542 ("premature use of type with private component", Actual);
8545 Set_Instance_Of (A_Gen_T, Act_T);
8547 -- If the type is generic, the class-wide type may also be used
8549 if Is_Tagged_Type (A_Gen_T)
8550 and then Is_Tagged_Type (Act_T)
8551 and then not Is_Class_Wide_Type (A_Gen_T)
8553 Set_Instance_Of (Class_Wide_Type (A_Gen_T),
8554 Class_Wide_Type (Act_T));
8557 if not Is_Abstract (A_Gen_T)
8558 and then Is_Abstract (Act_T)
8561 ("actual of non-abstract formal cannot be abstract", Actual);
8564 if Is_Scalar_Type (Gen_T) then
8565 Set_Instance_Of (Etype (A_Gen_T), Etype (Act_T));
8570 when N_Formal_Private_Type_Definition =>
8571 Validate_Private_Type_Instance;
8573 when N_Formal_Derived_Type_Definition =>
8574 Validate_Derived_Type_Instance;
8576 when N_Formal_Discrete_Type_Definition =>
8577 if not Is_Discrete_Type (Act_T) then
8579 ("expect discrete type in instantiation of&", Actual, Gen_T);
8580 Abandon_Instantiation (Actual);
8583 when N_Formal_Signed_Integer_Type_Definition =>
8584 if not Is_Signed_Integer_Type (Act_T) then
8586 ("expect signed integer type in instantiation of&",
8588 Abandon_Instantiation (Actual);
8591 when N_Formal_Modular_Type_Definition =>
8592 if not Is_Modular_Integer_Type (Act_T) then
8594 ("expect modular type in instantiation of &", Actual, Gen_T);
8595 Abandon_Instantiation (Actual);
8598 when N_Formal_Floating_Point_Definition =>
8599 if not Is_Floating_Point_Type (Act_T) then
8601 ("expect float type in instantiation of &", Actual, Gen_T);
8602 Abandon_Instantiation (Actual);
8605 when N_Formal_Ordinary_Fixed_Point_Definition =>
8606 if not Is_Ordinary_Fixed_Point_Type (Act_T) then
8608 ("expect ordinary fixed point type in instantiation of &",
8610 Abandon_Instantiation (Actual);
8613 when N_Formal_Decimal_Fixed_Point_Definition =>
8614 if not Is_Decimal_Fixed_Point_Type (Act_T) then
8616 ("expect decimal type in instantiation of &",
8618 Abandon_Instantiation (Actual);
8621 when N_Array_Type_Definition =>
8622 Validate_Array_Type_Instance;
8624 when N_Access_To_Object_Definition =>
8625 Validate_Access_Type_Instance;
8627 when N_Access_Function_Definition |
8628 N_Access_Procedure_Definition =>
8629 Validate_Access_Subprogram_Instance;
8632 raise Program_Error;
8637 Make_Subtype_Declaration (Loc,
8638 Defining_Identifier => New_Copy (Gen_T),
8639 Subtype_Indication => New_Reference_To (Act_T, Loc));
8641 if Is_Private_Type (Act_T) then
8642 Set_Has_Private_View (Subtype_Indication (Decl_Node));
8644 elsif Is_Access_Type (Act_T)
8645 and then Is_Private_Type (Designated_Type (Act_T))
8647 Set_Has_Private_View (Subtype_Indication (Decl_Node));
8650 -- Flag actual derived types so their elaboration produces the
8651 -- appropriate renamings for the primitive operations of the ancestor.
8652 -- Flag actual for formal private types as well, to determine whether
8653 -- operations in the private part may override inherited operations.
8655 if Nkind (Def) = N_Formal_Derived_Type_Definition
8656 or else Nkind (Def) = N_Formal_Private_Type_Definition
8658 Set_Generic_Parent_Type (Decl_Node, Ancestor);
8662 end Instantiate_Type;
8664 ---------------------
8665 -- Is_In_Main_Unit --
8666 ---------------------
8668 function Is_In_Main_Unit (N : Node_Id) return Boolean is
8669 Unum : constant Unit_Number_Type := Get_Source_Unit (N);
8670 Current_Unit : Node_Id;
8673 if Unum = Main_Unit then
8676 -- If the current unit is a subunit then it is either the main unit
8677 -- or is being compiled as part of the main unit.
8679 elsif Nkind (N) = N_Compilation_Unit then
8680 return Nkind (Unit (N)) = N_Subunit;
8683 Current_Unit := Parent (N);
8684 while Present (Current_Unit)
8685 and then Nkind (Current_Unit) /= N_Compilation_Unit
8687 Current_Unit := Parent (Current_Unit);
8690 -- The instantiation node is in the main unit, or else the current
8691 -- node (perhaps as the result of nested instantiations) is in the
8692 -- main unit, or in the declaration of the main unit, which in this
8693 -- last case must be a body.
8695 return Unum = Main_Unit
8696 or else Current_Unit = Cunit (Main_Unit)
8697 or else Current_Unit = Library_Unit (Cunit (Main_Unit))
8698 or else (Present (Library_Unit (Current_Unit))
8699 and then Is_In_Main_Unit (Library_Unit (Current_Unit)));
8700 end Is_In_Main_Unit;
8702 ----------------------------
8703 -- Load_Parent_Of_Generic --
8704 ----------------------------
8706 procedure Load_Parent_Of_Generic (N : Node_Id; Spec : Node_Id) is
8707 Comp_Unit : constant Node_Id := Cunit (Get_Source_Unit (Spec));
8708 Save_Style_Check : constant Boolean := Style_Check;
8709 True_Parent : Node_Id;
8710 Inst_Node : Node_Id;
8714 if not In_Same_Source_Unit (N, Spec)
8715 or else Nkind (Unit (Comp_Unit)) = N_Package_Declaration
8716 or else (Nkind (Unit (Comp_Unit)) = N_Package_Body
8717 and then not Is_In_Main_Unit (Spec))
8719 -- Find body of parent of spec, and analyze it. A special case
8720 -- arises when the parent is an instantiation, that is to say when
8721 -- we are currently instantiating a nested generic. In that case,
8722 -- there is no separate file for the body of the enclosing instance.
8723 -- Instead, the enclosing body must be instantiated as if it were
8724 -- a pending instantiation, in order to produce the body for the
8725 -- nested generic we require now. Note that in that case the
8726 -- generic may be defined in a package body, the instance defined
8727 -- in the same package body, and the original enclosing body may not
8728 -- be in the main unit.
8730 True_Parent := Parent (Spec);
8733 while Present (True_Parent)
8734 and then Nkind (True_Parent) /= N_Compilation_Unit
8736 if Nkind (True_Parent) = N_Package_Declaration
8738 Nkind (Original_Node (True_Parent)) = N_Package_Instantiation
8740 -- Parent is a compilation unit that is an instantiation.
8741 -- Instantiation node has been replaced with package decl.
8743 Inst_Node := Original_Node (True_Parent);
8746 elsif Nkind (True_Parent) = N_Package_Declaration
8747 and then Present (Generic_Parent (Specification (True_Parent)))
8748 and then Nkind (Parent (True_Parent)) /= N_Compilation_Unit
8750 -- Parent is an instantiation within another specification.
8751 -- Declaration for instance has been inserted before original
8752 -- instantiation node. A direct link would be preferable?
8754 Inst_Node := Next (True_Parent);
8756 while Present (Inst_Node)
8757 and then Nkind (Inst_Node) /= N_Package_Instantiation
8762 -- If the instance appears within a generic, and the generic
8763 -- unit is defined within a formal package of the enclosing
8764 -- generic, there is no generic body available, and none
8765 -- needed. A more precise test should be used ???
8767 if No (Inst_Node) then
8773 True_Parent := Parent (True_Parent);
8777 -- Case where we are currently instantiating a nested generic
8779 if Present (Inst_Node) then
8780 if Nkind (Parent (True_Parent)) = N_Compilation_Unit then
8782 -- Instantiation node and declaration of instantiated package
8783 -- were exchanged when only the declaration was needed.
8784 -- Restore instantiation node before proceeding with body.
8786 Set_Unit (Parent (True_Parent), Inst_Node);
8789 -- Now complete instantiation of enclosing body, if it appears
8790 -- in some other unit. If it appears in the current unit, the
8791 -- body will have been instantiated already.
8793 if No (Corresponding_Body (Instance_Spec (Inst_Node))) then
8795 -- We need to determine the expander mode to instantiate
8796 -- the enclosing body. Because the generic body we need
8797 -- may use global entities declared in the enclosing package
8798 -- (including aggregates) it is in general necessary to
8799 -- compile this body with expansion enabled. The exception
8800 -- is if we are within a generic package, in which case
8801 -- the usual generic rule applies.
8804 Exp_Status : Boolean := True;
8808 -- Loop through scopes looking for generic package
8810 Scop := Scope (Defining_Entity (Instance_Spec (Inst_Node)));
8811 while Present (Scop)
8812 and then Scop /= Standard_Standard
8814 if Ekind (Scop) = E_Generic_Package then
8815 Exp_Status := False;
8819 Scop := Scope (Scop);
8822 Instantiate_Package_Body
8823 (Pending_Body_Info'(
8824 Inst_Node, True_Parent, Exp_Status,
8825 Get_Code_Unit (Sloc (Inst_Node))));
8829 -- Case where we are not instantiating a nested generic
8832 Opt.Style_Check := False;
8833 Expander_Mode_Save_And_Set (True);
8834 Load_Needed_Body (Comp_Unit, OK);
8835 Opt.Style_Check := Save_Style_Check;
8836 Expander_Mode_Restore;
8839 and then Unit_Requires_Body (Defining_Entity (Spec))
8842 Bname : constant Unit_Name_Type :=
8843 Get_Body_Name (Get_Unit_Name (Unit (Comp_Unit)));
8846 Error_Msg_Unit_1 := Bname;
8847 Error_Msg_N ("this instantiation requires$!", N);
8849 Get_File_Name (Bname, Subunit => False);
8850 Error_Msg_N ("\but file{ was not found!", N);
8851 raise Unrecoverable_Error;
8857 -- If loading the parent of the generic caused an instantiation
8858 -- circularity, we abandon compilation at this point, because
8859 -- otherwise in some cases we get into trouble with infinite
8860 -- recursions after this point.
8862 if Circularity_Detected then
8863 raise Unrecoverable_Error;
8865 end Load_Parent_Of_Generic;
8867 -----------------------
8868 -- Move_Freeze_Nodes --
8869 -----------------------
8871 procedure Move_Freeze_Nodes
8872 (Out_Of : Entity_Id;
8877 Next_Decl : Node_Id;
8878 Next_Node : Node_Id := After;
8881 function Is_Outer_Type (T : Entity_Id) return Boolean;
8882 -- Check whether entity is declared in a scope external to that
8883 -- of the generic unit.
8889 function Is_Outer_Type (T : Entity_Id) return Boolean is
8890 Scop : Entity_Id := Scope (T);
8893 if Scope_Depth (Scop) < Scope_Depth (Out_Of) then
8897 while Scop /= Standard_Standard loop
8899 if Scop = Out_Of then
8902 Scop := Scope (Scop);
8910 -- Start of processing for Move_Freeze_Nodes
8917 -- First remove the freeze nodes that may appear before all other
8921 while Present (Decl)
8922 and then Nkind (Decl) = N_Freeze_Entity
8923 and then Is_Outer_Type (Entity (Decl))
8925 Decl := Remove_Head (L);
8926 Insert_After (Next_Node, Decl);
8927 Set_Analyzed (Decl, False);
8932 -- Next scan the list of declarations and remove each freeze node that
8933 -- appears ahead of the current node.
8935 while Present (Decl) loop
8936 while Present (Next (Decl))
8937 and then Nkind (Next (Decl)) = N_Freeze_Entity
8938 and then Is_Outer_Type (Entity (Next (Decl)))
8940 Next_Decl := Remove_Next (Decl);
8941 Insert_After (Next_Node, Next_Decl);
8942 Set_Analyzed (Next_Decl, False);
8943 Next_Node := Next_Decl;
8946 -- If the declaration is a nested package or concurrent type, then
8947 -- recurse. Nested generic packages will have been processed from the
8950 if Nkind (Decl) = N_Package_Declaration then
8951 Spec := Specification (Decl);
8953 elsif Nkind (Decl) = N_Task_Type_Declaration then
8954 Spec := Task_Definition (Decl);
8956 elsif Nkind (Decl) = N_Protected_Type_Declaration then
8957 Spec := Protected_Definition (Decl);
8963 if Present (Spec) then
8964 Move_Freeze_Nodes (Out_Of, Next_Node,
8965 Visible_Declarations (Spec));
8966 Move_Freeze_Nodes (Out_Of, Next_Node,
8967 Private_Declarations (Spec));
8972 end Move_Freeze_Nodes;
8978 function Next_Assoc (E : Assoc_Ptr) return Assoc_Ptr is
8980 return Generic_Renamings.Table (E).Next_In_HTable;
8983 ------------------------
8984 -- Preanalyze_Actuals --
8985 ------------------------
8987 procedure Pre_Analyze_Actuals (N : Node_Id) is
8990 Errs : constant Int := Serious_Errors_Detected;
8993 Assoc := First (Generic_Associations (N));
8995 while Present (Assoc) loop
8996 Act := Explicit_Generic_Actual_Parameter (Assoc);
8998 -- Within a nested instantiation, a defaulted actual is an
8999 -- empty association, so nothing to analyze. If the actual for
9000 -- a subprogram is an attribute, analyze prefix only, because
9001 -- actual is not a complete attribute reference.
9003 -- If actual is an allocator, analyze expression only. The full
9004 -- analysis can generate code, and if the instance is a compilation
9005 -- unit we have to wait until the package instance is installed to
9006 -- have a proper place to insert this code.
9008 -- String literals may be operators, but at this point we do not
9009 -- know whether the actual is a formal subprogram or a string.
9014 elsif Nkind (Act) = N_Attribute_Reference then
9015 Analyze (Prefix (Act));
9017 elsif Nkind (Act) = N_Explicit_Dereference then
9018 Analyze (Prefix (Act));
9020 elsif Nkind (Act) = N_Allocator then
9022 Expr : constant Node_Id := Expression (Act);
9025 if Nkind (Expr) = N_Subtype_Indication then
9026 Analyze (Subtype_Mark (Expr));
9027 Analyze_List (Constraints (Constraint (Expr)));
9033 elsif Nkind (Act) /= N_Operator_Symbol then
9037 if Errs /= Serious_Errors_Detected then
9038 Abandon_Instantiation (Act);
9043 end Pre_Analyze_Actuals;
9049 procedure Remove_Parent (In_Body : Boolean := False) is
9050 S : Entity_Id := Current_Scope;
9056 -- After child instantiation is complete, remove from scope stack
9057 -- the extra copy of the current scope, and then remove parent
9063 while Current_Scope /= S loop
9065 End_Package_Scope (Current_Scope);
9067 if In_Open_Scopes (P) then
9068 E := First_Entity (P);
9070 while Present (E) loop
9071 Set_Is_Immediately_Visible (E, True);
9075 if Is_Generic_Instance (Current_Scope)
9076 and then P /= Current_Scope
9078 -- We are within an instance of some sibling. Retain
9079 -- visibility of parent, for proper subsequent cleanup,
9080 -- and reinstall private declarations as well.
9082 Set_In_Private_Part (P);
9083 Install_Private_Declarations (P);
9086 -- If the ultimate parent is a compilation unit, reset its
9087 -- visibility to what it was before instantiation.
9089 elsif not In_Open_Scopes (Scope (P))
9091 (not Is_Child_Unit (P) and then not Parent_Unit_Visible)
9093 Set_Is_Immediately_Visible (P, False);
9097 -- Reset visibility of entities in the enclosing scope
9099 Set_Is_Hidden_Open_Scope (Current_Scope, False);
9100 Hidden := First_Elmt (Hidden_Entities);
9102 while Present (Hidden) loop
9103 Set_Is_Immediately_Visible (Node (Hidden), True);
9108 -- Each body is analyzed separately, and there is no context
9109 -- that needs preserving from one body instance to the next,
9110 -- so remove all parent scopes that have been installed.
9112 while Present (S) loop
9113 End_Package_Scope (S);
9114 Set_Is_Immediately_Visible (S, False);
9116 exit when S = Standard_Standard;
9126 procedure Restore_Env is
9127 Saved : Instance_Env renames Instance_Envs.Table (Instance_Envs.Last);
9130 Ada_Version := Saved.Ada_Version;
9131 Ada_Version_Explicit := Saved.Ada_Version_Explicit;
9133 if No (Current_Instantiated_Parent.Act_Id) then
9135 -- Restore environment after subprogram inlining
9137 Restore_Private_Views (Empty);
9140 Current_Instantiated_Parent := Saved.Instantiated_Parent;
9141 Exchanged_Views := Saved.Exchanged_Views;
9142 Hidden_Entities := Saved.Hidden_Entities;
9143 Current_Sem_Unit := Saved.Current_Sem_Unit;
9144 Parent_Unit_Visible := Saved.Parent_Unit_Visible;
9146 Instance_Envs.Decrement_Last;
9149 ---------------------------
9150 -- Restore_Private_Views --
9151 ---------------------------
9153 procedure Restore_Private_Views
9154 (Pack_Id : Entity_Id;
9155 Is_Package : Boolean := True)
9163 procedure Restore_Nested_Formal (Formal : Entity_Id);
9164 -- Hide the generic formals of formal packages declared with box
9165 -- which were reachable in the current instantiation.
9167 procedure Restore_Nested_Formal (Formal : Entity_Id) is
9170 if Present (Renamed_Object (Formal))
9171 and then Denotes_Formal_Package (Renamed_Object (Formal), True)
9175 elsif Present (Associated_Formal_Package (Formal))
9176 and then Box_Present (Parent (Associated_Formal_Package (Formal)))
9178 Ent := First_Entity (Formal);
9180 while Present (Ent) loop
9181 exit when Ekind (Ent) = E_Package
9182 and then Renamed_Entity (Ent) = Renamed_Entity (Formal);
9184 Set_Is_Hidden (Ent);
9185 Set_Is_Potentially_Use_Visible (Ent, False);
9187 -- If package, then recurse
9189 if Ekind (Ent) = E_Package then
9190 Restore_Nested_Formal (Ent);
9196 end Restore_Nested_Formal;
9199 M := First_Elmt (Exchanged_Views);
9200 while Present (M) loop
9203 -- Subtypes of types whose views have been exchanged, and that
9204 -- are defined within the instance, were not on the list of
9205 -- Private_Dependents on entry to the instance, so they have to
9206 -- be exchanged explicitly now, in order to remain consistent with
9207 -- the view of the parent type.
9209 if Ekind (Typ) = E_Private_Type
9210 or else Ekind (Typ) = E_Limited_Private_Type
9211 or else Ekind (Typ) = E_Record_Type_With_Private
9213 Dep_Elmt := First_Elmt (Private_Dependents (Typ));
9215 while Present (Dep_Elmt) loop
9216 Dep_Typ := Node (Dep_Elmt);
9218 if Scope (Dep_Typ) = Pack_Id
9219 and then Present (Full_View (Dep_Typ))
9221 Replace_Elmt (Dep_Elmt, Full_View (Dep_Typ));
9222 Exchange_Declarations (Dep_Typ);
9225 Next_Elmt (Dep_Elmt);
9229 Exchange_Declarations (Node (M));
9233 if No (Pack_Id) then
9237 -- Make the generic formal parameters private, and make the formal
9238 -- types into subtypes of the actuals again.
9240 E := First_Entity (Pack_Id);
9242 while Present (E) loop
9243 Set_Is_Hidden (E, True);
9246 and then Nkind (Parent (E)) = N_Subtype_Declaration
9248 Set_Is_Generic_Actual_Type (E, False);
9250 -- An unusual case of aliasing: the actual may also be directly
9251 -- visible in the generic, and be private there, while it is
9252 -- fully visible in the context of the instance. The internal
9253 -- subtype is private in the instance, but has full visibility
9254 -- like its parent in the enclosing scope. This enforces the
9255 -- invariant that the privacy status of all private dependents of
9256 -- a type coincide with that of the parent type. This can only
9257 -- happen when a generic child unit is instantiated within a
9260 if Is_Private_Type (E)
9261 and then not Is_Private_Type (Etype (E))
9263 Exchange_Declarations (E);
9266 elsif Ekind (E) = E_Package then
9268 -- The end of the renaming list is the renaming of the generic
9269 -- package itself. If the instance is a subprogram, all entities
9270 -- in the corresponding package are renamings. If this entity is
9271 -- a formal package, make its own formals private as well. The
9272 -- actual in this case is itself the renaming of an instantation.
9273 -- If the entity is not a package renaming, it is the entity
9274 -- created to validate formal package actuals: ignore.
9276 -- If the actual is itself a formal package for the enclosing
9277 -- generic, or the actual for such a formal package, it remains
9278 -- visible on exit from the instance, and therefore nothing
9279 -- needs to be done either, except to keep it accessible.
9282 and then Renamed_Object (E) = Pack_Id
9286 elsif Nkind (Parent (E)) /= N_Package_Renaming_Declaration then
9289 elsif Denotes_Formal_Package (Renamed_Object (E), True) then
9290 Set_Is_Hidden (E, False);
9294 Act_P : constant Entity_Id := Renamed_Object (E);
9298 Id := First_Entity (Act_P);
9300 and then Id /= First_Private_Entity (Act_P)
9302 exit when Ekind (Id) = E_Package
9303 and then Renamed_Object (Id) = Act_P;
9305 Set_Is_Hidden (Id, True);
9306 Set_Is_Potentially_Use_Visible (Id, In_Use (Act_P));
9308 if Ekind (Id) = E_Package then
9309 Restore_Nested_Formal (Id);
9320 end Restore_Private_Views;
9327 (Gen_Unit : Entity_Id;
9328 Act_Unit : Entity_Id)
9332 Set_Instance_Env (Gen_Unit, Act_Unit);
9335 ----------------------------
9336 -- Save_Global_References --
9337 ----------------------------
9339 procedure Save_Global_References (N : Node_Id) is
9340 Gen_Scope : Entity_Id;
9344 function Is_Global (E : Entity_Id) return Boolean;
9345 -- Check whether entity is defined outside of generic unit.
9346 -- Examine the scope of an entity, and the scope of the scope,
9347 -- etc, until we find either Standard, in which case the entity
9348 -- is global, or the generic unit itself, which indicates that
9349 -- the entity is local. If the entity is the generic unit itself,
9350 -- as in the case of a recursive call, or the enclosing generic unit,
9351 -- if different from the current scope, then it is local as well,
9352 -- because it will be replaced at the point of instantiation. On
9353 -- the other hand, if it is a reference to a child unit of a common
9354 -- ancestor, which appears in an instantiation, it is global because
9355 -- it is used to denote a specific compilation unit at the time the
9356 -- instantiations will be analyzed.
9358 procedure Reset_Entity (N : Node_Id);
9359 -- Save semantic information on global entity, so that it is not
9360 -- resolved again at instantiation time.
9362 procedure Save_Entity_Descendants (N : Node_Id);
9363 -- Apply Save_Global_References to the two syntactic descendants of
9364 -- non-terminal nodes that carry an Associated_Node and are processed
9365 -- through Reset_Entity. Once the global entity (if any) has been
9366 -- captured together with its type, only two syntactic descendants
9367 -- need to be traversed to complete the processing of the tree rooted
9368 -- at N. This applies to Selected_Components, Expanded_Names, and to
9369 -- Operator nodes. N can also be a character literal, identifier, or
9370 -- operator symbol node, but the call has no effect in these cases.
9372 procedure Save_Global_Defaults (N1, N2 : Node_Id);
9373 -- Default actuals in nested instances must be handled specially
9374 -- because there is no link to them from the original tree. When an
9375 -- actual subprogram is given by a default, we add an explicit generic
9376 -- association for it in the instantiation node. When we save the
9377 -- global references on the name of the instance, we recover the list
9378 -- of generic associations, and add an explicit one to the original
9379 -- generic tree, through which a global actual can be preserved.
9380 -- Similarly, if a child unit is instantiated within a sibling, in the
9381 -- context of the parent, we must preserve the identifier of the parent
9382 -- so that it can be properly resolved in a subsequent instantiation.
9384 procedure Save_Global_Descendant (D : Union_Id);
9385 -- Apply Save_Global_References recursively to the descendents of
9388 procedure Save_References (N : Node_Id);
9389 -- This is the recursive procedure that does the work, once the
9390 -- enclosing generic scope has been established.
9396 function Is_Global (E : Entity_Id) return Boolean is
9397 Se : Entity_Id := Scope (E);
9399 function Is_Instance_Node (Decl : Node_Id) return Boolean;
9400 -- Determine whether the parent node of a reference to a child unit
9401 -- denotes an instantiation or a formal package, in which case the
9402 -- reference to the child unit is global, even if it appears within
9403 -- the current scope (e.g. when the instance appears within the body
9406 ----------------------
9407 -- Is_Instance_Node --
9408 ----------------------
9410 function Is_Instance_Node (Decl : Node_Id) return Boolean is
9412 return (Nkind (Decl) in N_Generic_Instantiation
9414 Nkind (Original_Node (Decl)) = N_Formal_Package_Declaration);
9415 end Is_Instance_Node;
9417 -- Start of processing for Is_Global
9420 if E = Gen_Scope then
9423 elsif E = Standard_Standard then
9426 elsif Is_Child_Unit (E)
9427 and then (Is_Instance_Node (Parent (N2))
9428 or else (Nkind (Parent (N2)) = N_Expanded_Name
9429 and then N2 = Selector_Name (Parent (N2))
9430 and then Is_Instance_Node (Parent (Parent (N2)))))
9435 while Se /= Gen_Scope loop
9436 if Se = Standard_Standard then
9451 procedure Reset_Entity (N : Node_Id) is
9453 procedure Set_Global_Type (N : Node_Id; N2 : Node_Id);
9454 -- The type of N2 is global to the generic unit. Save the
9455 -- type in the generic node.
9457 function Top_Ancestor (E : Entity_Id) return Entity_Id;
9458 -- Find the ultimate ancestor of the current unit. If it is
9459 -- not a generic unit, then the name of the current unit
9460 -- in the prefix of an expanded name must be replaced with
9461 -- its generic homonym to ensure that it will be properly
9462 -- resolved in an instance.
9464 ---------------------
9465 -- Set_Global_Type --
9466 ---------------------
9468 procedure Set_Global_Type (N : Node_Id; N2 : Node_Id) is
9469 Typ : constant Entity_Id := Etype (N2);
9475 and then Has_Private_View (Entity (N))
9477 -- If the entity of N is not the associated node, this is
9478 -- a nested generic and it has an associated node as well,
9479 -- whose type is already the full view (see below). Indicate
9480 -- that the original node has a private view.
9482 Set_Has_Private_View (N);
9485 -- If not a private type, nothing else to do
9487 if not Is_Private_Type (Typ) then
9488 if Is_Array_Type (Typ)
9489 and then Is_Private_Type (Component_Type (Typ))
9491 Set_Has_Private_View (N);
9494 -- If it is a derivation of a private type in a context where
9495 -- no full view is needed, nothing to do either.
9497 elsif No (Full_View (Typ)) and then Typ /= Etype (Typ) then
9500 -- Otherwise mark the type for flipping and use the full_view
9504 Set_Has_Private_View (N);
9506 if Present (Full_View (Typ)) then
9507 Set_Etype (N2, Full_View (Typ));
9510 end Set_Global_Type;
9516 function Top_Ancestor (E : Entity_Id) return Entity_Id is
9517 Par : Entity_Id := E;
9520 while Is_Child_Unit (Par) loop
9527 -- Start of processing for Reset_Entity
9530 N2 := Get_Associated_Node (N);
9534 if Is_Global (E) then
9535 Set_Global_Type (N, N2);
9537 elsif Nkind (N) = N_Op_Concat
9538 and then Is_Generic_Type (Etype (N2))
9540 (Base_Type (Etype (Right_Opnd (N2))) = Etype (N2)
9541 or else Base_Type (Etype (Left_Opnd (N2))) = Etype (N2))
9542 and then Is_Intrinsic_Subprogram (E)
9547 -- Entity is local. Mark generic node as unresolved.
9548 -- Note that now it does not have an entity.
9550 Set_Associated_Node (N, Empty);
9551 Set_Etype (N, Empty);
9554 if (Nkind (Parent (N)) = N_Package_Instantiation
9555 or else Nkind (Parent (N)) = N_Function_Instantiation
9556 or else Nkind (Parent (N)) = N_Procedure_Instantiation)
9557 and then N = Name (Parent (N))
9559 Save_Global_Defaults (Parent (N), Parent (N2));
9562 elsif Nkind (Parent (N)) = N_Selected_Component
9563 and then Nkind (Parent (N2)) = N_Expanded_Name
9566 if Is_Global (Entity (Parent (N2))) then
9567 Change_Selected_Component_To_Expanded_Name (Parent (N));
9568 Set_Associated_Node (Parent (N), Parent (N2));
9569 Set_Global_Type (Parent (N), Parent (N2));
9570 Save_Entity_Descendants (N);
9572 -- If this is a reference to the current generic entity,
9573 -- replace by the name of the generic homonym of the current
9574 -- package. This is because in an instantiation Par.P.Q will
9575 -- not resolve to the name of the instance, whose enclosing
9576 -- scope is not necessarily Par. We use the generic homonym
9577 -- rather that the name of the generic itself, because it may
9578 -- be hidden by a local declaration.
9580 elsif In_Open_Scopes (Entity (Parent (N2)))
9582 Is_Generic_Unit (Top_Ancestor (Entity (Prefix (Parent (N2)))))
9584 if Ekind (Entity (Parent (N2))) = E_Generic_Package then
9585 Rewrite (Parent (N),
9586 Make_Identifier (Sloc (N),
9588 Chars (Generic_Homonym (Entity (Parent (N2))))));
9590 Rewrite (Parent (N),
9591 Make_Identifier (Sloc (N),
9592 Chars => Chars (Selector_Name (Parent (N2)))));
9596 if (Nkind (Parent (Parent (N))) = N_Package_Instantiation
9597 or else Nkind (Parent (Parent (N)))
9598 = N_Function_Instantiation
9599 or else Nkind (Parent (Parent (N)))
9600 = N_Procedure_Instantiation)
9601 and then Parent (N) = Name (Parent (Parent (N)))
9603 Save_Global_Defaults
9604 (Parent (Parent (N)), Parent (Parent ((N2))));
9607 -- A selected component may denote a static constant that has
9608 -- been folded. Make the same replacement in original tree.
9610 elsif Nkind (Parent (N)) = N_Selected_Component
9611 and then (Nkind (Parent (N2)) = N_Integer_Literal
9612 or else Nkind (Parent (N2)) = N_Real_Literal)
9614 Rewrite (Parent (N),
9615 New_Copy (Parent (N2)));
9616 Set_Analyzed (Parent (N), False);
9618 -- A selected component may be transformed into a parameterless
9619 -- function call. If the called entity is global, rewrite the
9620 -- node appropriately, i.e. as an extended name for the global
9623 elsif Nkind (Parent (N)) = N_Selected_Component
9624 and then Nkind (Parent (N2)) = N_Function_Call
9625 and then Is_Global (Entity (Name (Parent (N2))))
9627 Change_Selected_Component_To_Expanded_Name (Parent (N));
9628 Set_Associated_Node (Parent (N), Name (Parent (N2)));
9629 Set_Global_Type (Parent (N), Name (Parent (N2)));
9630 Save_Entity_Descendants (N);
9633 -- Entity is local. Reset in generic unit, so that node
9634 -- is resolved anew at the point of instantiation.
9636 Set_Associated_Node (N, Empty);
9637 Set_Etype (N, Empty);
9641 -----------------------------
9642 -- Save_Entity_Descendants --
9643 -----------------------------
9645 procedure Save_Entity_Descendants (N : Node_Id) is
9649 Save_Global_Descendant (Union_Id (Left_Opnd (N)));
9650 Save_Global_Descendant (Union_Id (Right_Opnd (N)));
9653 Save_Global_Descendant (Union_Id (Right_Opnd (N)));
9655 when N_Expanded_Name | N_Selected_Component =>
9656 Save_Global_Descendant (Union_Id (Prefix (N)));
9657 Save_Global_Descendant (Union_Id (Selector_Name (N)));
9659 when N_Identifier | N_Character_Literal | N_Operator_Symbol =>
9663 raise Program_Error;
9665 end Save_Entity_Descendants;
9667 --------------------------
9668 -- Save_Global_Defaults --
9669 --------------------------
9671 procedure Save_Global_Defaults (N1, N2 : Node_Id) is
9672 Loc : constant Source_Ptr := Sloc (N1);
9673 Assoc2 : constant List_Id := Generic_Associations (N2);
9674 Gen_Id : constant Entity_Id := Get_Generic_Entity (N2);
9684 Assoc1 := Generic_Associations (N1);
9686 if Present (Assoc1) then
9687 Act1 := First (Assoc1);
9690 Set_Generic_Associations (N1, New_List);
9691 Assoc1 := Generic_Associations (N1);
9694 if Present (Assoc2) then
9695 Act2 := First (Assoc2);
9700 while Present (Act1) and then Present (Act2) loop
9705 -- Find the associations added for default suprograms
9707 if Present (Act2) then
9708 while Nkind (Act2) /= N_Generic_Association
9709 or else No (Entity (Selector_Name (Act2)))
9710 or else not Is_Overloadable (Entity (Selector_Name (Act2)))
9715 -- Add a similar association if the default is global. The
9716 -- renaming declaration for the actual has been analyzed, and
9717 -- its alias is the program it renames. Link the actual in the
9718 -- original generic tree with the node in the analyzed tree.
9720 while Present (Act2) loop
9721 Subp := Entity (Selector_Name (Act2));
9722 Def := Explicit_Generic_Actual_Parameter (Act2);
9724 -- Following test is defence against rubbish errors
9726 if No (Alias (Subp)) then
9730 -- Retrieve the resolved actual from the renaming declaration
9731 -- created for the instantiated formal.
9733 Actual := Entity (Name (Parent (Parent (Subp))));
9734 Set_Entity (Def, Actual);
9735 Set_Etype (Def, Etype (Actual));
9737 if Is_Global (Actual) then
9739 Make_Generic_Association (Loc,
9740 Selector_Name => New_Occurrence_Of (Subp, Loc),
9741 Explicit_Generic_Actual_Parameter =>
9742 New_Occurrence_Of (Actual, Loc));
9745 (Explicit_Generic_Actual_Parameter (Ndec), Def);
9747 Append (Ndec, Assoc1);
9749 -- If there are other defaults, add a dummy association
9750 -- in case there are other defaulted formals with the same
9753 elsif Present (Next (Act2)) then
9755 Make_Generic_Association (Loc,
9756 Selector_Name => New_Occurrence_Of (Subp, Loc),
9757 Explicit_Generic_Actual_Parameter => Empty);
9759 Append (Ndec, Assoc1);
9766 if Nkind (Name (N1)) = N_Identifier
9767 and then Is_Child_Unit (Gen_Id)
9768 and then Is_Global (Gen_Id)
9769 and then Is_Generic_Unit (Scope (Gen_Id))
9770 and then In_Open_Scopes (Scope (Gen_Id))
9772 -- This is an instantiation of a child unit within a sibling,
9773 -- so that the generic parent is in scope. An eventual instance
9774 -- must occur within the scope of an instance of the parent.
9775 -- Make name in instance into an expanded name, to preserve the
9776 -- identifier of the parent, so it can be resolved subsequently.
9779 Make_Expanded_Name (Loc,
9780 Chars => Chars (Gen_Id),
9781 Prefix => New_Occurrence_Of (Scope (Gen_Id), Loc),
9782 Selector_Name => New_Occurrence_Of (Gen_Id, Loc)));
9783 Set_Entity (Name (N2), Gen_Id);
9786 Make_Expanded_Name (Loc,
9787 Chars => Chars (Gen_Id),
9788 Prefix => New_Occurrence_Of (Scope (Gen_Id), Loc),
9789 Selector_Name => New_Occurrence_Of (Gen_Id, Loc)));
9791 Set_Associated_Node (Name (N1), Name (N2));
9792 Set_Associated_Node (Prefix (Name (N1)), Empty);
9794 (Selector_Name (Name (N1)), Selector_Name (Name (N2)));
9795 Set_Etype (Name (N1), Etype (Gen_Id));
9798 end Save_Global_Defaults;
9800 ----------------------------
9801 -- Save_Global_Descendant --
9802 ----------------------------
9804 procedure Save_Global_Descendant (D : Union_Id) is
9808 if D in Node_Range then
9809 if D = Union_Id (Empty) then
9812 elsif Nkind (Node_Id (D)) /= N_Compilation_Unit then
9813 Save_References (Node_Id (D));
9816 elsif D in List_Range then
9817 if D = Union_Id (No_List)
9818 or else Is_Empty_List (List_Id (D))
9823 N1 := First (List_Id (D));
9824 while Present (N1) loop
9825 Save_References (N1);
9830 -- Element list or other non-node field, nothing to do
9835 end Save_Global_Descendant;
9837 ---------------------
9838 -- Save_References --
9839 ---------------------
9841 -- This is the recursive procedure that does the work, once the
9842 -- enclosing generic scope has been established. We have to treat
9843 -- specially a number of node rewritings that are required by semantic
9844 -- processing and which change the kind of nodes in the generic copy:
9845 -- typically constant-folding, replacing an operator node by a string
9846 -- literal, or a selected component by an expanded name. In each of
9847 -- those cases, the transformation is propagated to the generic unit.
9849 procedure Save_References (N : Node_Id) is
9854 elsif Nkind (N) = N_Character_Literal
9855 or else Nkind (N) = N_Operator_Symbol
9857 if Nkind (N) = Nkind (Get_Associated_Node (N)) then
9860 elsif Nkind (N) = N_Operator_Symbol
9861 and then Nkind (Get_Associated_Node (N)) = N_String_Literal
9863 Change_Operator_Symbol_To_String_Literal (N);
9866 elsif Nkind (N) in N_Op then
9868 if Nkind (N) = Nkind (Get_Associated_Node (N)) then
9870 if Nkind (N) = N_Op_Concat then
9871 Set_Is_Component_Left_Opnd (N,
9872 Is_Component_Left_Opnd (Get_Associated_Node (N)));
9874 Set_Is_Component_Right_Opnd (N,
9875 Is_Component_Right_Opnd (Get_Associated_Node (N)));
9880 -- Node may be transformed into call to a user-defined operator
9882 N2 := Get_Associated_Node (N);
9884 if Nkind (N2) = N_Function_Call then
9885 E := Entity (Name (N2));
9888 and then Is_Global (E)
9890 Set_Etype (N, Etype (N2));
9892 Set_Associated_Node (N, Empty);
9893 Set_Etype (N, Empty);
9896 elsif Nkind (N2) = N_Integer_Literal
9897 or else Nkind (N2) = N_Real_Literal
9898 or else Nkind (N2) = N_String_Literal
9900 -- Operation was constant-folded, perform the same
9901 -- replacement in generic.
9903 Rewrite (N, New_Copy (N2));
9904 Set_Analyzed (N, False);
9906 elsif Nkind (N2) = N_Identifier
9907 and then Ekind (Entity (N2)) = E_Enumeration_Literal
9909 -- Same if call was folded into a literal, but in this
9910 -- case retain the entity to avoid spurious ambiguities
9911 -- if id is overloaded at the point of instantiation or
9914 Rewrite (N, New_Copy (N2));
9915 Set_Analyzed (N, False);
9919 -- Complete the check on operands, if node has not been
9922 if Nkind (N) in N_Op then
9923 Save_Entity_Descendants (N);
9926 elsif Nkind (N) = N_Identifier then
9927 if Nkind (N) = Nkind (Get_Associated_Node (N)) then
9929 -- If this is a discriminant reference, always save it.
9930 -- It is used in the instance to find the corresponding
9931 -- discriminant positionally rather than by name.
9933 Set_Original_Discriminant
9934 (N, Original_Discriminant (Get_Associated_Node (N)));
9938 N2 := Get_Associated_Node (N);
9940 if Nkind (N2) = N_Function_Call then
9941 E := Entity (Name (N2));
9943 -- Name resolves to a call to parameterless function.
9944 -- If original entity is global, mark node as resolved.
9947 and then Is_Global (E)
9949 Set_Etype (N, Etype (N2));
9951 Set_Associated_Node (N, Empty);
9952 Set_Etype (N, Empty);
9956 Nkind (N2) = N_Integer_Literal or else
9957 Nkind (N2) = N_Real_Literal or else
9958 Nkind (N2) = N_String_Literal
9960 -- Name resolves to named number that is constant-folded,
9961 -- or to string literal from concatenation.
9962 -- Perform the same replacement in generic.
9964 Rewrite (N, New_Copy (N2));
9965 Set_Analyzed (N, False);
9967 elsif Nkind (N2) = N_Explicit_Dereference then
9969 -- An identifier is rewritten as a dereference if it is
9970 -- the prefix in a selected component, and it denotes an
9971 -- access to a composite type, or a parameterless function
9972 -- call that returns an access type.
9974 -- Check whether corresponding entity in prefix is global
9976 if Is_Entity_Name (Prefix (N2))
9977 and then Present (Entity (Prefix (N2)))
9978 and then Is_Global (Entity (Prefix (N2)))
9981 Make_Explicit_Dereference (Sloc (N),
9982 Prefix => Make_Identifier (Sloc (N),
9983 Chars => Chars (N))));
9984 Set_Associated_Node (Prefix (N), Prefix (N2));
9986 elsif Nkind (Prefix (N2)) = N_Function_Call
9987 and then Is_Global (Entity (Name (Prefix (N2))))
9990 Make_Explicit_Dereference (Sloc (N),
9991 Prefix => Make_Function_Call (Sloc (N),
9993 Make_Identifier (Sloc (N),
9994 Chars => Chars (N)))));
9997 (Name (Prefix (N)), Name (Prefix (N2)));
10000 Set_Associated_Node (N, Empty);
10001 Set_Etype (N, Empty);
10004 -- The subtype mark of a nominally unconstrained object
10005 -- is rewritten as a subtype indication using the bounds
10006 -- of the expression. Recover the original subtype mark.
10008 elsif Nkind (N2) = N_Subtype_Indication
10009 and then Is_Entity_Name (Original_Node (N2))
10011 Set_Associated_Node (N, Original_Node (N2));
10019 elsif Nkind (N) in N_Entity then
10024 use Atree.Unchecked_Access;
10025 -- This code section is part of implementing an untyped tree
10026 -- traversal, so it needs direct access to node fields.
10029 if Nkind (N) = N_Aggregate
10031 Nkind (N) = N_Extension_Aggregate
10033 N2 := Get_Associated_Node (N);
10036 or else No (Etype (N2))
10037 or else not Is_Global (Etype (N2))
10039 Set_Associated_Node (N, Empty);
10042 Save_Global_Descendant (Field1 (N));
10043 Save_Global_Descendant (Field2 (N));
10044 Save_Global_Descendant (Field3 (N));
10045 Save_Global_Descendant (Field5 (N));
10047 -- All other cases than aggregates
10050 Save_Global_Descendant (Field1 (N));
10051 Save_Global_Descendant (Field2 (N));
10052 Save_Global_Descendant (Field3 (N));
10053 Save_Global_Descendant (Field4 (N));
10054 Save_Global_Descendant (Field5 (N));
10058 end Save_References;
10060 -- Start of processing for Save_Global_References
10063 Gen_Scope := Current_Scope;
10065 -- If the generic unit is a child unit, references to entities in
10066 -- the parent are treated as local, because they will be resolved
10067 -- anew in the context of the instance of the parent.
10069 while Is_Child_Unit (Gen_Scope)
10070 and then Ekind (Scope (Gen_Scope)) = E_Generic_Package
10072 Gen_Scope := Scope (Gen_Scope);
10075 Save_References (N);
10076 end Save_Global_References;
10078 --------------------------------------
10079 -- Set_Copied_Sloc_For_Inlined_Body --
10080 --------------------------------------
10082 procedure Set_Copied_Sloc_For_Inlined_Body (N : Node_Id; E : Entity_Id) is
10084 Create_Instantiation_Source (N, E, True, S_Adjustment);
10085 end Set_Copied_Sloc_For_Inlined_Body;
10087 ---------------------
10088 -- Set_Instance_Of --
10089 ---------------------
10091 procedure Set_Instance_Of (A : Entity_Id; B : Entity_Id) is
10093 Generic_Renamings.Table (Generic_Renamings.Last) := (A, B, Assoc_Null);
10094 Generic_Renamings_HTable.Set (Generic_Renamings.Last);
10095 Generic_Renamings.Increment_Last;
10096 end Set_Instance_Of;
10098 --------------------
10099 -- Set_Next_Assoc --
10100 --------------------
10102 procedure Set_Next_Assoc (E : Assoc_Ptr; Next : Assoc_Ptr) is
10104 Generic_Renamings.Table (E).Next_In_HTable := Next;
10105 end Set_Next_Assoc;
10107 -------------------
10108 -- Start_Generic --
10109 -------------------
10111 procedure Start_Generic is
10113 -- ??? I am sure more things could be factored out in this
10114 -- routine. Should probably be done at a later stage.
10116 Generic_Flags.Increment_Last;
10117 Generic_Flags.Table (Generic_Flags.Last) := Inside_A_Generic;
10118 Inside_A_Generic := True;
10120 Expander_Mode_Save_And_Set (False);
10123 ----------------------
10124 -- Set_Instance_Env --
10125 ----------------------
10127 procedure Set_Instance_Env
10128 (Gen_Unit : Entity_Id;
10129 Act_Unit : Entity_Id)
10132 -- Regardless of the current mode, predefined units are analyzed in
10133 -- the most current Ada mode, and earlier version Ada checks do not
10134 -- apply to predefined units.
10136 -- Why is this not using the routine Opt.Set_Opt_Config_Switches ???
10138 if Is_Internal_File_Name
10139 (Fname => Unit_File_Name (Get_Source_Unit (Gen_Unit)),
10140 Renamings_Included => True) then
10141 Ada_Version := Ada_Version_Type'Last;
10142 Ada_Version_Explicit := Ada_Version_Explicit_Config;
10145 Current_Instantiated_Parent := (Gen_Unit, Act_Unit, Assoc_Null);
10146 end Set_Instance_Env;
10152 procedure Switch_View (T : Entity_Id) is
10153 BT : constant Entity_Id := Base_Type (T);
10154 Priv_Elmt : Elmt_Id := No_Elmt;
10155 Priv_Sub : Entity_Id;
10158 -- T may be private but its base type may have been exchanged through
10159 -- some other occurrence, in which case there is nothing to switch.
10161 if not Is_Private_Type (BT) then
10165 Priv_Elmt := First_Elmt (Private_Dependents (BT));
10167 if Present (Full_View (BT)) then
10168 Append_Elmt (Full_View (BT), Exchanged_Views);
10169 Exchange_Declarations (BT);
10172 while Present (Priv_Elmt) loop
10173 Priv_Sub := (Node (Priv_Elmt));
10175 -- We avoid flipping the subtype if the Etype of its full
10176 -- view is private because this would result in a malformed
10177 -- subtype. This occurs when the Etype of the subtype full
10178 -- view is the full view of the base type (and since the
10179 -- base types were just switched, the subtype is pointing
10180 -- to the wrong view). This is currently the case for
10181 -- tagged record types, access types (maybe more?) and
10182 -- needs to be resolved. ???
10184 if Present (Full_View (Priv_Sub))
10185 and then not Is_Private_Type (Etype (Full_View (Priv_Sub)))
10187 Append_Elmt (Full_View (Priv_Sub), Exchanged_Views);
10188 Exchange_Declarations (Priv_Sub);
10191 Next_Elmt (Priv_Elmt);
10195 -----------------------------
10196 -- Valid_Default_Attribute --
10197 -----------------------------
10199 procedure Valid_Default_Attribute (Nam : Entity_Id; Def : Node_Id) is
10200 Attr_Id : constant Attribute_Id :=
10201 Get_Attribute_Id (Attribute_Name (Def));
10202 T : constant Entity_Id := Entity (Prefix (Def));
10203 Is_Fun : constant Boolean := (Ekind (Nam) = E_Function);
10216 F := First_Formal (Nam);
10217 while Present (F) loop
10218 Num_F := Num_F + 1;
10223 when Attribute_Adjacent | Attribute_Ceiling | Attribute_Copy_Sign |
10224 Attribute_Floor | Attribute_Fraction | Attribute_Machine |
10225 Attribute_Model | Attribute_Remainder | Attribute_Rounding |
10226 Attribute_Unbiased_Rounding =>
10229 and then Is_Floating_Point_Type (T);
10231 when Attribute_Image | Attribute_Pred | Attribute_Succ |
10232 Attribute_Value | Attribute_Wide_Image |
10233 Attribute_Wide_Value =>
10234 OK := (Is_Fun and then Num_F = 1 and then Is_Scalar_Type (T));
10236 when Attribute_Max | Attribute_Min =>
10237 OK := (Is_Fun and then Num_F = 2 and then Is_Scalar_Type (T));
10239 when Attribute_Input =>
10240 OK := (Is_Fun and then Num_F = 1);
10242 when Attribute_Output | Attribute_Read | Attribute_Write =>
10243 OK := (not Is_Fun and then Num_F = 2);
10250 Error_Msg_N ("attribute reference has wrong profile for subprogram",
10253 end Valid_Default_Attribute;