1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
9 -- Copyright (C) 1992-2004, Free Software Foundation, Inc. --
11 -- GNAT is free software; you can redistribute it and/or modify it under --
12 -- terms of the GNU General Public License as published by the Free Soft- --
13 -- ware Foundation; either version 2, or (at your option) any later ver- --
14 -- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
15 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
16 -- or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License --
17 -- for more details. You should have received a copy of the GNU General --
18 -- Public License distributed with GNAT; see file COPYING. If not, write --
19 -- to the Free Software Foundation, 59 Temple Place - Suite 330, Boston, --
20 -- MA 02111-1307, USA. --
22 -- GNAT was originally developed by the GNAT team at New York University. --
23 -- Extensive contributions were provided by Ada Core Technologies Inc. --
25 ------------------------------------------------------------------------------
27 with Atree; use Atree;
28 with Einfo; use Einfo;
29 with Elists; use Elists;
30 with Errout; use Errout;
31 with Expander; use Expander;
32 with Fname; use Fname;
33 with Fname.UF; use Fname.UF;
34 with Freeze; use Freeze;
36 with Inline; use Inline;
38 with Lib.Load; use Lib.Load;
39 with Lib.Xref; use Lib.Xref;
40 with Nlists; use Nlists;
41 with Nmake; use Nmake;
43 with Rident; use Rident;
44 with Restrict; use Restrict;
45 with Rtsfind; use Rtsfind;
47 with Sem_Cat; use Sem_Cat;
48 with Sem_Ch3; use Sem_Ch3;
49 with Sem_Ch6; use Sem_Ch6;
50 with Sem_Ch7; use Sem_Ch7;
51 with Sem_Ch8; use Sem_Ch8;
52 with Sem_Ch10; use Sem_Ch10;
53 with Sem_Ch13; use Sem_Ch13;
54 with Sem_Elab; use Sem_Elab;
55 with Sem_Elim; use Sem_Elim;
56 with Sem_Eval; use Sem_Eval;
57 with Sem_Res; use Sem_Res;
58 with Sem_Type; use Sem_Type;
59 with Sem_Util; use Sem_Util;
60 with Sem_Warn; use Sem_Warn;
61 with Stand; use Stand;
62 with Sinfo; use Sinfo;
63 with Sinfo.CN; use Sinfo.CN;
64 with Sinput; use Sinput;
65 with Sinput.L; use Sinput.L;
66 with Snames; use Snames;
67 with Stringt; use Stringt;
68 with Uname; use Uname;
70 with Tbuild; use Tbuild;
71 with Uintp; use Uintp;
72 with Urealp; use Urealp;
76 package body Sem_Ch12 is
78 ----------------------------------------------------------
79 -- Implementation of Generic Analysis and Instantiation --
80 -----------------------------------------------------------
82 -- GNAT implements generics by macro expansion. No attempt is made to
83 -- share generic instantiations (for now). Analysis of a generic definition
84 -- does not perform any expansion action, but the expander must be called
85 -- on the tree for each instantiation, because the expansion may of course
86 -- depend on the generic actuals. All of this is best achieved as follows:
88 -- a) Semantic analysis of a generic unit is performed on a copy of the
89 -- tree for the generic unit. All tree modifications that follow analysis
90 -- do not affect the original tree. Links are kept between the original
91 -- tree and the copy, in order to recognize non-local references within
92 -- the generic, and propagate them to each instance (recall that name
93 -- resolution is done on the generic declaration: generics are not really
94 -- macros!). This is summarized in the following diagram:
96 -- .-----------. .----------.
97 -- | semantic |<--------------| generic |
99 -- | |==============>| |
100 -- |___________| global |__________|
111 -- b) Each instantiation copies the original tree, and inserts into it a
112 -- series of declarations that describe the mapping between generic formals
113 -- and actuals. For example, a generic In OUT parameter is an object
114 -- renaming of the corresponing actual, etc. Generic IN parameters are
115 -- constant declarations.
117 -- c) In order to give the right visibility for these renamings, we use
118 -- a different scheme for package and subprogram instantiations. For
119 -- packages, the list of renamings is inserted into the package
120 -- specification, before the visible declarations of the package. The
121 -- renamings are analyzed before any of the text of the instance, and are
122 -- thus visible at the right place. Furthermore, outside of the instance,
123 -- the generic parameters are visible and denote their corresponding
126 -- For subprograms, we create a container package to hold the renamings
127 -- and the subprogram instance itself. Analysis of the package makes the
128 -- renaming declarations visible to the subprogram. After analyzing the
129 -- package, the defining entity for the subprogram is touched-up so that
130 -- it appears declared in the current scope, and not inside the container
133 -- If the instantiation is a compilation unit, the container package is
134 -- given the same name as the subprogram instance. This ensures that
135 -- the elaboration procedure called by the binder, using the compilation
136 -- unit name, calls in fact the elaboration procedure for the package.
138 -- Not surprisingly, private types complicate this approach. By saving in
139 -- the original generic object the non-local references, we guarantee that
140 -- the proper entities are referenced at the point of instantiation.
141 -- However, for private types, this by itself does not insure that the
142 -- proper VIEW of the entity is used (the full type may be visible at the
143 -- point of generic definition, but not at instantiation, or vice-versa).
144 -- In order to reference the proper view, we special-case any reference
145 -- to private types in the generic object, by saving both views, one in
146 -- the generic and one in the semantic copy. At time of instantiation, we
147 -- check whether the two views are consistent, and exchange declarations if
148 -- necessary, in order to restore the correct visibility. Similarly, if
149 -- the instance view is private when the generic view was not, we perform
150 -- the exchange. After completing the instantiation, we restore the
151 -- current visibility. The flag Has_Private_View marks identifiers in the
152 -- the generic unit that require checking.
154 -- Visibility within nested generic units requires special handling.
155 -- Consider the following scheme:
157 -- type Global is ... -- outside of generic unit.
161 -- type Semi_Global is ... -- global to inner.
164 -- procedure inner (X1 : Global; X2 : Semi_Global);
166 -- procedure in2 is new inner (...); -- 4
169 -- package New_Outer is new Outer (...); -- 2
170 -- procedure New_Inner is new New_Outer.Inner (...); -- 3
172 -- The semantic analysis of Outer captures all occurrences of Global.
173 -- The semantic analysis of Inner (at 1) captures both occurrences of
174 -- Global and Semi_Global.
176 -- At point 2 (instantiation of Outer), we also produce a generic copy
177 -- of Inner, even though Inner is, at that point, not being instantiated.
178 -- (This is just part of the semantic analysis of New_Outer).
180 -- Critically, references to Global within Inner must be preserved, while
181 -- references to Semi_Global should not preserved, because they must now
182 -- resolve to an entity within New_Outer. To distinguish between these, we
183 -- use a global variable, Current_Instantiated_Parent, which is set when
184 -- performing a generic copy during instantiation (at 2). This variable is
185 -- used when performing a generic copy that is not an instantiation, but
186 -- that is nested within one, as the occurrence of 1 within 2. The analysis
187 -- of a nested generic only preserves references that are global to the
188 -- enclosing Current_Instantiated_Parent. We use the Scope_Depth value to
189 -- determine whether a reference is external to the given parent.
191 -- The instantiation at point 3 requires no special treatment. The method
192 -- works as well for further nestings of generic units, but of course the
193 -- variable Current_Instantiated_Parent must be stacked because nested
194 -- instantiations can occur, e.g. the occurrence of 4 within 2.
196 -- The instantiation of package and subprogram bodies is handled in a
197 -- similar manner, except that it is delayed until after semantic
198 -- analysis is complete. In this fashion complex cross-dependencies
199 -- between several package declarations and bodies containing generics
200 -- can be compiled which otherwise would diagnose spurious circularities.
202 -- For example, it is possible to compile two packages A and B that
203 -- have the following structure:
205 -- package A is package B is
206 -- generic ... generic ...
207 -- package G_A is package G_B is
210 -- package body A is package body B is
211 -- package N_B is new G_B (..) package N_A is new G_A (..)
213 -- The table Pending_Instantiations in package Inline is used to keep
214 -- track of body instantiations that are delayed in this manner. Inline
215 -- handles the actual calls to do the body instantiations. This activity
216 -- is part of Inline, since the processing occurs at the same point, and
217 -- for essentially the same reason, as the handling of inlined routines.
219 ----------------------------------------------
220 -- Detection of Instantiation Circularities --
221 ----------------------------------------------
223 -- If we have a chain of instantiations that is circular, this is a
224 -- static error which must be detected at compile time. The detection
225 -- of these circularities is carried out at the point that we insert
226 -- a generic instance spec or body. If there is a circularity, then
227 -- the analysis of the offending spec or body will eventually result
228 -- in trying to load the same unit again, and we detect this problem
229 -- as we analyze the package instantiation for the second time.
231 -- At least in some cases after we have detected the circularity, we
232 -- get into trouble if we try to keep going. The following flag is
233 -- set if a circularity is detected, and used to abandon compilation
234 -- after the messages have been posted.
236 Circularity_Detected : Boolean := False;
237 -- This should really be reset on encountering a new main unit, but in
238 -- practice we are not using multiple main units so it is not critical.
240 -----------------------
241 -- Local subprograms --
242 -----------------------
244 procedure Abandon_Instantiation (N : Node_Id);
245 pragma No_Return (Abandon_Instantiation);
246 -- Posts an error message "instantiation abandoned" at the indicated
247 -- node and then raises the exception Instantiation_Error to do it.
249 procedure Analyze_Formal_Array_Type
250 (T : in out Entity_Id;
252 -- A formal array type is treated like an array type declaration, and
253 -- invokes Array_Type_Declaration (sem_ch3) whose first parameter is
254 -- in-out, because in the case of an anonymous type the entity is
255 -- actually created in the procedure.
257 -- The following procedures treat other kinds of formal parameters.
259 procedure Analyze_Formal_Derived_Type
264 -- All the following need comments???
266 procedure Analyze_Formal_Decimal_Fixed_Point_Type
267 (T : Entity_Id; Def : Node_Id);
268 procedure Analyze_Formal_Discrete_Type (T : Entity_Id; Def : Node_Id);
269 procedure Analyze_Formal_Floating_Type (T : Entity_Id; Def : Node_Id);
270 procedure Analyze_Formal_Signed_Integer_Type (T : Entity_Id; Def : Node_Id);
271 procedure Analyze_Formal_Modular_Type (T : Entity_Id; Def : Node_Id);
272 procedure Analyze_Formal_Ordinary_Fixed_Point_Type
273 (T : Entity_Id; Def : Node_Id);
275 procedure Analyze_Formal_Private_Type
279 -- This needs comments???
281 procedure Analyze_Generic_Formal_Part (N : Node_Id);
283 procedure Analyze_Generic_Access_Type (T : Entity_Id; Def : Node_Id);
284 -- This needs comments ???
286 function Analyze_Associations
291 -- At instantiation time, build the list of associations between formals
292 -- and actuals. Each association becomes a renaming declaration for the
293 -- formal entity. F_Copy is the analyzed list of formals in the generic
294 -- copy. It is used to apply legality checks to the actuals. I_Node is the
295 -- instantiation node itself.
297 procedure Analyze_Subprogram_Instantiation
301 procedure Build_Instance_Compilation_Unit_Nodes
305 -- This procedure is used in the case where the generic instance of a
306 -- subprogram body or package body is a library unit. In this case, the
307 -- original library unit node for the generic instantiation must be
308 -- replaced by the resulting generic body, and a link made to a new
309 -- compilation unit node for the generic declaration. The argument N is
310 -- the original generic instantiation. Act_Body and Act_Decl are the body
311 -- and declaration of the instance (either package body and declaration
312 -- nodes or subprogram body and declaration nodes depending on the case).
313 -- On return, the node N has been rewritten with the actual body.
315 procedure Check_Formal_Packages (P_Id : Entity_Id);
316 -- Apply the following to all formal packages in generic associations.
318 procedure Check_Formal_Package_Instance
319 (Formal_Pack : Entity_Id;
320 Actual_Pack : Entity_Id);
321 -- Verify that the actuals of the actual instance match the actuals of
322 -- the template for a formal package that is not declared with a box.
324 procedure Check_Forward_Instantiation (Decl : Node_Id);
325 -- If the generic is a local entity and the corresponding body has not
326 -- been seen yet, flag enclosing packages to indicate that it will be
327 -- elaborated after the generic body. Subprograms declared in the same
328 -- package cannot be inlined by the front-end because front-end inlining
329 -- requires a strict linear order of elaboration.
331 procedure Check_Hidden_Child_Unit
333 Gen_Unit : Entity_Id;
334 Act_Decl_Id : Entity_Id);
335 -- If the generic unit is an implicit child instance within a parent
336 -- instance, we need to make an explicit test that it is not hidden by
337 -- a child instance of the same name and parent.
339 procedure Check_Private_View (N : Node_Id);
340 -- Check whether the type of a generic entity has a different view between
341 -- the point of generic analysis and the point of instantiation. If the
342 -- view has changed, then at the point of instantiation we restore the
343 -- correct view to perform semantic analysis of the instance, and reset
344 -- the current view after instantiation. The processing is driven by the
345 -- current private status of the type of the node, and Has_Private_View,
346 -- a flag that is set at the point of generic compilation. If view and
347 -- flag are inconsistent then the type is updated appropriately.
349 procedure Check_Generic_Actuals
350 (Instance : Entity_Id;
351 Is_Formal_Box : Boolean);
352 -- Similar to previous one. Check the actuals in the instantiation,
353 -- whose views can change between the point of instantiation and the point
354 -- of instantiation of the body. In addition, mark the generic renamings
355 -- as generic actuals, so that they are not compatible with other actuals.
356 -- Recurse on an actual that is a formal package whose declaration has
359 function Contains_Instance_Of
364 -- Inner is instantiated within the generic Outer. Check whether Inner
365 -- directly or indirectly contains an instance of Outer or of one of its
366 -- parents, in the case of a subunit. Each generic unit holds a list of
367 -- the entities instantiated within (at any depth). This procedure
368 -- determines whether the set of such lists contains a cycle, i.e. an
369 -- illegal circular instantiation.
371 function Denotes_Formal_Package (Pack : Entity_Id) return Boolean;
372 -- Returns True if E is a formal package of an enclosing generic, or
373 -- the actual for such a formal in an enclosing instantiation. Used in
374 -- Restore_Private_Views, to keep the formals of such a package visible
375 -- on exit from an inner instantiation.
377 function Find_Actual_Type
379 Gen_Scope : Entity_Id)
381 -- When validating the actual types of a child instance, check whether
382 -- the formal is a formal type of the parent unit, and retrieve the current
383 -- actual for it. Typ is the entity in the analyzed formal type declaration
384 -- (component or index type of an array type) and Gen_Scope is the scope of
385 -- the analyzed formal array type.
387 function Get_Package_Instantiation_Node (A : Entity_Id) return Node_Id;
388 -- Given the entity of a unit that is an instantiation, retrieve the
389 -- original instance node. This is used when loading the instantiations
390 -- of the ancestors of a child generic that is being instantiated.
392 function In_Same_Declarative_Part
396 -- True if the instantiation Inst and the given freeze_node F_Node appear
397 -- within the same declarative part, ignoring subunits, but with no inter-
398 -- vening suprograms or concurrent units. If true, the freeze node
399 -- of the instance can be placed after the freeze node of the parent,
400 -- which it itself an instance.
402 procedure Set_Instance_Env
403 (Gen_Unit : Entity_Id;
404 Act_Unit : Entity_Id);
405 -- Save current instance on saved environment, to be used to determine
406 -- the global status of entities in nested instances. Part of Save_Env.
407 -- called after verifying that the generic unit is legal for the instance.
409 procedure Set_Instance_Of (A : Entity_Id; B : Entity_Id);
410 -- Associate analyzed generic parameter with corresponding
411 -- instance. Used for semantic checks at instantiation time.
413 function Has_Been_Exchanged (E : Entity_Id) return Boolean;
414 -- Traverse the Exchanged_Views list to see if a type was private
415 -- and has already been flipped during this phase of instantiation.
417 procedure Hide_Current_Scope;
418 -- When compiling a generic child unit, the parent context must be
419 -- present, but the instance and all entities that may be generated
420 -- must be inserted in the current scope. We leave the current scope
421 -- on the stack, but make its entities invisible to avoid visibility
422 -- problems. This is reversed at the end of instantiations. This is
423 -- not done for the instantiation of the bodies, which only require the
424 -- instances of the generic parents to be in scope.
426 procedure Install_Body
431 -- If the instantiation happens textually before the body of the generic,
432 -- the instantiation of the body must be analyzed after the generic body,
433 -- and not at the point of instantiation. Such early instantiations can
434 -- happen if the generic and the instance appear in a package declaration
435 -- because the generic body can only appear in the corresponding package
436 -- body. Early instantiations can also appear if generic, instance and
437 -- body are all in the declarative part of a subprogram or entry. Entities
438 -- of packages that are early instantiations are delayed, and their freeze
439 -- node appears after the generic body.
441 procedure Insert_After_Last_Decl (N : Node_Id; F_Node : Node_Id);
442 -- Insert freeze node at the end of the declarative part that includes the
443 -- instance node N. If N is in the visible part of an enclosing package
444 -- declaration, the freeze node has to be inserted at the end of the
445 -- private declarations, if any.
447 procedure Freeze_Subprogram_Body
448 (Inst_Node : Node_Id;
450 Pack_Id : Entity_Id);
451 -- The generic body may appear textually after the instance, including
452 -- in the proper body of a stub, or within a different package instance.
453 -- Given that the instance can only be elaborated after the generic, we
454 -- place freeze_nodes for the instance and/or for packages that may enclose
455 -- the instance and the generic, so that the back-end can establish the
456 -- proper order of elaboration.
459 -- Establish environment for subsequent instantiation. Separated from
460 -- Save_Env because data-structures for visibility handling must be
461 -- initialized before call to Check_Generic_Child_Unit.
463 procedure Install_Parent (P : Entity_Id; In_Body : Boolean := False);
464 -- When compiling an instance of a child unit the parent (which is
465 -- itself an instance) is an enclosing scope that must be made
466 -- immediately visible. This procedure is also used to install the non-
467 -- generic parent of a generic child unit when compiling its body, so that
468 -- full views of types in the parent are made visible.
470 procedure Remove_Parent (In_Body : Boolean := False);
471 -- Reverse effect after instantiation of child is complete.
473 procedure Inline_Instance_Body
475 Gen_Unit : Entity_Id;
477 -- If front-end inlining is requested, instantiate the package body,
478 -- and preserve the visibility of its compilation unit, to insure
479 -- that successive instantiations succeed.
481 -- The functions Instantiate_XXX perform various legality checks and build
482 -- the declarations for instantiated generic parameters.
483 -- Need to describe what the parameters are ???
485 function Instantiate_Object
488 Analyzed_Formal : Node_Id)
491 function Instantiate_Type
494 Analyzed_Formal : Node_Id;
495 Actual_Decls : List_Id)
498 function Instantiate_Formal_Subprogram
501 Analyzed_Formal : Node_Id)
504 function Instantiate_Formal_Package
507 Analyzed_Formal : Node_Id)
509 -- If the formal package is declared with a box, special visibility rules
510 -- apply to its formals: they are in the visible part of the package. This
511 -- is true in the declarative region of the formal package, that is to say
512 -- in the enclosing generic or instantiation. For an instantiation, the
513 -- parameters of the formal package are made visible in an explicit step.
514 -- Furthermore, if the actual is a visible use_clause, these formals must
515 -- be made potentially use_visible as well. On exit from the enclosing
516 -- instantiation, the reverse must be done.
518 -- For a formal package declared without a box, there are conformance rules
519 -- that apply to the actuals in the generic declaration and the actuals of
520 -- the actual package in the enclosing instantiation. The simplest way to
521 -- apply these rules is to repeat the instantiation of the formal package
522 -- in the context of the enclosing instance, and compare the generic
523 -- associations of this instantiation with those of the actual package.
525 function Is_In_Main_Unit (N : Node_Id) return Boolean;
526 -- Test if given node is in the main unit
528 procedure Load_Parent_Of_Generic (N : Node_Id; Spec : Node_Id);
529 -- If the generic appears in a separate non-generic library unit,
530 -- load the corresponding body to retrieve the body of the generic.
531 -- N is the node for the generic instantiation, Spec is the generic
532 -- package declaration.
534 procedure Inherit_Context (Gen_Decl : Node_Id; Inst : Node_Id);
535 -- Add the context clause of the unit containing a generic unit to
536 -- an instantiation that is a compilation unit.
538 function Get_Associated_Node (N : Node_Id) return Node_Id;
539 -- In order to propagate semantic information back from the analyzed
540 -- copy to the original generic, we maintain links between selected nodes
541 -- in the generic and their corresponding copies. At the end of generic
542 -- analysis, the routine Save_Global_References traverses the generic
543 -- tree, examines the semantic information, and preserves the links to
544 -- those nodes that contain global information. At instantiation, the
545 -- information from the associated node is placed on the new copy, so
546 -- that name resolution is not repeated.
548 -- Three kinds of source nodes have associated nodes:
550 -- a) those that can reference (denote) entities, that is identifiers,
551 -- character literals, expanded_names, operator symbols, operators,
552 -- and attribute reference nodes. These nodes have an Entity field
553 -- and are the set of nodes that are in N_Has_Entity.
555 -- b) aggregates (N_Aggregate and N_Extension_Aggregate)
557 -- c) selected components (N_Selected_Component)
559 -- For the first class, the associated node preserves the entity if it is
560 -- global. If the generic contains nested instantiations, the associated
561 -- node itself has been recopied, and a chain of them must be followed.
563 -- For aggregates, the associated node allows retrieval of the type, which
564 -- may otherwise not appear in the generic. The view of this type may be
565 -- different between generic and instantiation, and the full view can be
566 -- installed before the instantiation is analyzed. For aggregates of
567 -- type extensions, the same view exchange may have to be performed for
568 -- some of the ancestor types, if their view is private at the point of
571 -- Nodes that are selected components in the parse tree may be rewritten
572 -- as expanded names after resolution, and must be treated as potential
573 -- entity holders. which is why they also have an Associated_Node.
575 -- Nodes that do not come from source, such as freeze nodes, do not appear
576 -- in the generic tree, and need not have an associated node.
578 -- The associated node is stored in the Associated_Node field. Note that
579 -- this field overlaps Entity, which is fine, because the whole point is
580 -- that we don't need or want the normal Entity field in this situation.
582 procedure Move_Freeze_Nodes
586 -- Freeze nodes can be generated in the analysis of a generic unit, but
587 -- will not be seen by the back-end. It is necessary to move those nodes
588 -- to the enclosing scope if they freeze an outer entity. We place them
589 -- at the end of the enclosing generic package, which is semantically
592 procedure Pre_Analyze_Actuals (N : Node_Id);
593 -- Analyze actuals to perform name resolution. Full resolution is done
594 -- later, when the expected types are known, but names have to be captured
595 -- before installing parents of generics, that are not visible for the
596 -- actuals themselves.
598 procedure Valid_Default_Attribute (Nam : Entity_Id; Def : Node_Id);
599 -- Verify that an attribute that appears as the default for a formal
600 -- subprogram is a function or procedure with the correct profile.
602 -------------------------------------------
603 -- Data Structures for Generic Renamings --
604 -------------------------------------------
606 -- The map Generic_Renamings associates generic entities with their
607 -- corresponding actuals. Currently used to validate type instances.
608 -- It will eventually be used for all generic parameters to eliminate
609 -- the need for overload resolution in the instance.
611 type Assoc_Ptr is new Int;
613 Assoc_Null : constant Assoc_Ptr := -1;
618 Next_In_HTable : Assoc_Ptr;
621 package Generic_Renamings is new Table.Table
622 (Table_Component_Type => Assoc,
623 Table_Index_Type => Assoc_Ptr,
624 Table_Low_Bound => 0,
626 Table_Increment => 100,
627 Table_Name => "Generic_Renamings");
629 -- Variable to hold enclosing instantiation. When the environment is
630 -- saved for a subprogram inlining, the corresponding Act_Id is empty.
632 Current_Instantiated_Parent : Assoc := (Empty, Empty, Assoc_Null);
634 -- Hash table for associations
636 HTable_Size : constant := 37;
637 type HTable_Range is range 0 .. HTable_Size - 1;
639 procedure Set_Next_Assoc (E : Assoc_Ptr; Next : Assoc_Ptr);
640 function Next_Assoc (E : Assoc_Ptr) return Assoc_Ptr;
641 function Get_Gen_Id (E : Assoc_Ptr) return Entity_Id;
642 function Hash (F : Entity_Id) return HTable_Range;
644 package Generic_Renamings_HTable is new GNAT.HTable.Static_HTable (
645 Header_Num => HTable_Range,
647 Elmt_Ptr => Assoc_Ptr,
648 Null_Ptr => Assoc_Null,
649 Set_Next => Set_Next_Assoc,
652 Get_Key => Get_Gen_Id,
656 Exchanged_Views : Elist_Id;
657 -- This list holds the private views that have been exchanged during
658 -- instantiation to restore the visibility of the generic declaration.
659 -- (see comments above). After instantiation, the current visibility is
660 -- reestablished by means of a traversal of this list.
662 Hidden_Entities : Elist_Id;
663 -- This list holds the entities of the current scope that are removed
664 -- from immediate visibility when instantiating a child unit. Their
665 -- visibility is restored in Remove_Parent.
667 -- Because instantiations can be recursive, the following must be saved
668 -- on entry and restored on exit from an instantiation (spec or body).
669 -- This is done by the two procedures Save_Env and Restore_Env. For
670 -- package and subprogram instantiations (but not for the body instances)
671 -- the action of Save_Env is done in two steps: Init_Env is called before
672 -- Check_Generic_Child_Unit, because setting the parent instances requires
673 -- that the visibility data structures be properly initialized. Once the
674 -- generic is unit is validated, Set_Instance_Env completes Save_Env.
676 type Instance_Env is record
678 Instantiated_Parent : Assoc;
679 Exchanged_Views : Elist_Id;
680 Hidden_Entities : Elist_Id;
681 Current_Sem_Unit : Unit_Number_Type;
684 package Instance_Envs is new Table.Table (
685 Table_Component_Type => Instance_Env,
686 Table_Index_Type => Int,
687 Table_Low_Bound => 0,
689 Table_Increment => 100,
690 Table_Name => "Instance_Envs");
692 procedure Restore_Private_Views
693 (Pack_Id : Entity_Id;
694 Is_Package : Boolean := True);
695 -- Restore the private views of external types, and unmark the generic
696 -- renamings of actuals, so that they become comptible subtypes again.
697 -- For subprograms, Pack_Id is the package constructed to hold the
700 procedure Switch_View (T : Entity_Id);
701 -- Switch the partial and full views of a type and its private
702 -- dependents (i.e. its subtypes and derived types).
704 ------------------------------------
705 -- Structures for Error Reporting --
706 ------------------------------------
708 Instantiation_Node : Node_Id;
709 -- Used by subprograms that validate instantiation of formal parameters
710 -- where there might be no actual on which to place the error message.
711 -- Also used to locate the instantiation node for generic subunits.
713 Instantiation_Error : exception;
714 -- When there is a semantic error in the generic parameter matching,
715 -- there is no point in continuing the instantiation, because the
716 -- number of cascaded errors is unpredictable. This exception aborts
717 -- the instantiation process altogether.
719 S_Adjustment : Sloc_Adjustment;
720 -- Offset created for each node in an instantiation, in order to keep
721 -- track of the source position of the instantiation in each of its nodes.
722 -- A subsequent semantic error or warning on a construct of the instance
723 -- points to both places: the original generic node, and the point of
724 -- instantiation. See Sinput and Sinput.L for additional details.
726 ------------------------------------------------------------
727 -- Data structure for keeping track when inside a Generic --
728 ------------------------------------------------------------
730 -- The following table is used to save values of the Inside_A_Generic
731 -- flag (see spec of Sem) when they are saved by Start_Generic.
733 package Generic_Flags is new Table.Table (
734 Table_Component_Type => Boolean,
735 Table_Index_Type => Int,
736 Table_Low_Bound => 0,
738 Table_Increment => 200,
739 Table_Name => "Generic_Flags");
741 ---------------------------
742 -- Abandon_Instantiation --
743 ---------------------------
745 procedure Abandon_Instantiation (N : Node_Id) is
747 Error_Msg_N ("instantiation abandoned!", N);
748 raise Instantiation_Error;
749 end Abandon_Instantiation;
751 --------------------------
752 -- Analyze_Associations --
753 --------------------------
755 function Analyze_Associations
761 Actual_Types : constant Elist_Id := New_Elmt_List;
762 Assoc : constant List_Id := New_List;
763 Defaults : constant Elist_Id := New_Elmt_List;
764 Gen_Unit : constant Entity_Id := Defining_Entity
769 Next_Formal : Node_Id;
770 Temp_Formal : Node_Id;
771 Analyzed_Formal : Node_Id;
774 First_Named : Node_Id := Empty;
775 Found_Assoc : Node_Id;
776 Is_Named_Assoc : Boolean;
777 Num_Matched : Int := 0;
778 Num_Actuals : Int := 0;
780 function Matching_Actual
784 -- Find actual that corresponds to a given a formal parameter. If the
785 -- actuals are positional, return the next one, if any. If the actuals
786 -- are named, scan the parameter associations to find the right one.
787 -- A_F is the corresponding entity in the analyzed generic,which is
788 -- placed on the selector name for ASIS use.
790 procedure Set_Analyzed_Formal;
791 -- Find the node in the generic copy that corresponds to a given formal.
792 -- The semantic information on this node is used to perform legality
793 -- checks on the actuals. Because semantic analysis can introduce some
794 -- anonymous entities or modify the declaration node itself, the
795 -- correspondence between the two lists is not one-one. In addition to
796 -- anonymous types, the presence a formal equality will introduce an
797 -- implicit declaration for the corresponding inequality.
799 ---------------------
800 -- Matching_Actual --
801 ---------------------
803 function Matching_Actual
812 Is_Named_Assoc := False;
814 -- End of list of purely positional parameters
819 -- Case of positional parameter corresponding to current formal
821 elsif No (Selector_Name (Actual)) then
822 Found := Explicit_Generic_Actual_Parameter (Actual);
823 Found_Assoc := Actual;
824 Num_Matched := Num_Matched + 1;
827 -- Otherwise scan list of named actuals to find the one with the
828 -- desired name. All remaining actuals have explicit names.
831 Is_Named_Assoc := True;
835 while Present (Actual) loop
836 if Chars (Selector_Name (Actual)) = Chars (F) then
837 Found := Explicit_Generic_Actual_Parameter (Actual);
838 Set_Entity (Selector_Name (Actual), A_F);
839 Set_Etype (Selector_Name (Actual), Etype (A_F));
840 Generate_Reference (A_F, Selector_Name (Actual));
841 Found_Assoc := Actual;
842 Num_Matched := Num_Matched + 1;
850 -- Reset for subsequent searches. In most cases the named
851 -- associations are in order. If they are not, we reorder them
852 -- to avoid scanning twice the same actual. This is not just a
853 -- question of efficiency: there may be multiple defaults with
854 -- boxes that have the same name. In a nested instantiation we
855 -- insert actuals for those defaults, and cannot rely on their
856 -- names to disambiguate them.
858 if Actual = First_Named then
861 elsif Present (Actual) then
862 Insert_Before (First_Named, Remove_Next (Prev));
865 Actual := First_Named;
871 -------------------------
872 -- Set_Analyzed_Formal --
873 -------------------------
875 procedure Set_Analyzed_Formal is
878 while Present (Analyzed_Formal) loop
879 Kind := Nkind (Analyzed_Formal);
881 case Nkind (Formal) is
883 when N_Formal_Subprogram_Declaration =>
884 exit when Kind = N_Formal_Subprogram_Declaration
887 (Defining_Unit_Name (Specification (Formal))) =
889 (Defining_Unit_Name (Specification (Analyzed_Formal)));
891 when N_Formal_Package_Declaration =>
893 Kind = N_Formal_Package_Declaration
895 Kind = N_Generic_Package_Declaration;
897 when N_Use_Package_Clause | N_Use_Type_Clause => exit;
901 -- Skip freeze nodes, and nodes inserted to replace
902 -- unrecognized pragmas.
905 Kind /= N_Formal_Subprogram_Declaration
906 and then Kind /= N_Subprogram_Declaration
907 and then Kind /= N_Freeze_Entity
908 and then Kind /= N_Null_Statement
909 and then Kind /= N_Itype_Reference
910 and then Chars (Defining_Identifier (Formal)) =
911 Chars (Defining_Identifier (Analyzed_Formal));
914 Next (Analyzed_Formal);
917 end Set_Analyzed_Formal;
919 -- Start of processing for Analyze_Associations
922 -- If named associations are present, save the first named association
923 -- (it may of course be Empty) to facilitate subsequent name search.
925 Actuals := Generic_Associations (I_Node);
927 if Present (Actuals) then
928 First_Named := First (Actuals);
930 while Present (First_Named)
931 and then No (Selector_Name (First_Named))
933 Num_Actuals := Num_Actuals + 1;
938 Named := First_Named;
939 while Present (Named) loop
940 if No (Selector_Name (Named)) then
941 Error_Msg_N ("invalid positional actual after named one", Named);
942 Abandon_Instantiation (Named);
945 -- A named association may lack an actual parameter, if it was
946 -- introduced for a default subprogram that turns out to be local
947 -- to the outer instantiation.
949 if Present (Explicit_Generic_Actual_Parameter (Named)) then
950 Num_Actuals := Num_Actuals + 1;
956 if Present (Formals) then
957 Formal := First_Non_Pragma (Formals);
958 Analyzed_Formal := First_Non_Pragma (F_Copy);
960 if Present (Actuals) then
961 Actual := First (Actuals);
963 -- All formals should have default values
969 while Present (Formal) loop
971 Next_Formal := Next_Non_Pragma (Formal);
973 case Nkind (Formal) is
974 when N_Formal_Object_Declaration =>
977 Defining_Identifier (Formal),
978 Defining_Identifier (Analyzed_Formal));
981 (Instantiate_Object (Formal, Match, Analyzed_Formal),
984 when N_Formal_Type_Declaration =>
987 Defining_Identifier (Formal),
988 Defining_Identifier (Analyzed_Formal));
991 Error_Msg_Sloc := Sloc (Gen_Unit);
994 Instantiation_Node, Defining_Identifier (Formal));
995 Error_Msg_NE ("\in instantiation of & declared#",
996 Instantiation_Node, Gen_Unit);
997 Abandon_Instantiation (Instantiation_Node);
1003 (Formal, Match, Analyzed_Formal, Assoc));
1005 -- an instantiation is a freeze point for the actuals,
1006 -- unless this is a rewritten formal package.
1008 if Nkind (I_Node) /= N_Formal_Package_Declaration then
1009 Append_Elmt (Entity (Match), Actual_Types);
1013 -- A remote access-to-class-wide type must not be an
1014 -- actual parameter for a generic formal of an access
1015 -- type (E.2.2 (17)).
1017 if Nkind (Analyzed_Formal) = N_Formal_Type_Declaration
1019 Nkind (Formal_Type_Definition (Analyzed_Formal)) =
1020 N_Access_To_Object_Definition
1022 Validate_Remote_Access_To_Class_Wide_Type (Match);
1025 when N_Formal_Subprogram_Declaration =>
1028 Defining_Unit_Name (Specification (Formal)),
1029 Defining_Unit_Name (Specification (Analyzed_Formal)));
1031 -- If the formal subprogram has the same name as
1032 -- another formal subprogram of the generic, then
1033 -- a named association is illegal (12.3(9)). Exclude
1034 -- named associations that are generated for a nested
1038 and then Is_Named_Assoc
1039 and then Comes_From_Source (Found_Assoc)
1041 Temp_Formal := First (Formals);
1042 while Present (Temp_Formal) loop
1043 if Nkind (Temp_Formal) =
1044 N_Formal_Subprogram_Declaration
1045 and then Temp_Formal /= Formal
1047 Chars (Selector_Name (Found_Assoc)) =
1048 Chars (Defining_Unit_Name
1049 (Specification (Temp_Formal)))
1052 ("name not allowed for overloaded formal",
1054 Abandon_Instantiation (Instantiation_Node);
1062 Instantiate_Formal_Subprogram
1063 (Formal, Match, Analyzed_Formal));
1066 and then Box_Present (Formal)
1069 (Defining_Unit_Name (Specification (Last (Assoc))),
1073 when N_Formal_Package_Declaration =>
1076 Defining_Identifier (Formal),
1077 Defining_Identifier (Original_Node (Analyzed_Formal)));
1080 Error_Msg_Sloc := Sloc (Gen_Unit);
1083 Instantiation_Node, Defining_Identifier (Formal));
1084 Error_Msg_NE ("\in instantiation of & declared#",
1085 Instantiation_Node, Gen_Unit);
1087 Abandon_Instantiation (Instantiation_Node);
1092 (Instantiate_Formal_Package
1093 (Formal, Match, Analyzed_Formal),
1097 -- For use type and use package appearing in the context
1098 -- clause, we have already copied them, so we can just
1099 -- move them where they belong (we mustn't recopy them
1100 -- since this would mess up the Sloc values).
1102 when N_Use_Package_Clause |
1103 N_Use_Type_Clause =>
1105 Append (Formal, Assoc);
1108 raise Program_Error;
1112 Formal := Next_Formal;
1113 Next_Non_Pragma (Analyzed_Formal);
1116 if Num_Actuals > Num_Matched then
1117 Error_Msg_Sloc := Sloc (Gen_Unit);
1119 if Present (Selector_Name (Actual)) then
1121 ("unmatched actual&",
1122 Actual, Selector_Name (Actual));
1123 Error_Msg_NE ("\in instantiation of& declared#",
1127 ("unmatched actual in instantiation of& declared#",
1132 elsif Present (Actuals) then
1134 ("too many actuals in generic instantiation", Instantiation_Node);
1138 Elmt : Elmt_Id := First_Elmt (Actual_Types);
1141 while Present (Elmt) loop
1142 Freeze_Before (I_Node, Node (Elmt));
1147 -- If there are default subprograms, normalize the tree by adding
1148 -- explicit associations for them. This is required if the instance
1149 -- appears within a generic.
1157 Elmt := First_Elmt (Defaults);
1158 while Present (Elmt) loop
1159 if No (Actuals) then
1160 Actuals := New_List;
1161 Set_Generic_Associations (I_Node, Actuals);
1164 Subp := Node (Elmt);
1166 Make_Generic_Association (Sloc (Subp),
1167 Selector_Name => New_Occurrence_Of (Subp, Sloc (Subp)),
1168 Explicit_Generic_Actual_Parameter =>
1169 New_Occurrence_Of (Subp, Sloc (Subp)));
1170 Mark_Rewrite_Insertion (New_D);
1171 Append_To (Actuals, New_D);
1177 end Analyze_Associations;
1179 -------------------------------
1180 -- Analyze_Formal_Array_Type --
1181 -------------------------------
1183 procedure Analyze_Formal_Array_Type
1184 (T : in out Entity_Id;
1190 -- Treated like a non-generic array declaration, with
1191 -- additional semantic checks.
1195 if Nkind (Def) = N_Constrained_Array_Definition then
1196 DSS := First (Discrete_Subtype_Definitions (Def));
1197 while Present (DSS) loop
1198 if Nkind (DSS) = N_Subtype_Indication
1199 or else Nkind (DSS) = N_Range
1200 or else Nkind (DSS) = N_Attribute_Reference
1202 Error_Msg_N ("only a subtype mark is allowed in a formal", DSS);
1209 Array_Type_Declaration (T, Def);
1210 Set_Is_Generic_Type (Base_Type (T));
1212 if Ekind (Component_Type (T)) = E_Incomplete_Type
1213 and then No (Full_View (Component_Type (T)))
1215 Error_Msg_N ("premature usage of incomplete type", Def);
1217 elsif Is_Internal (Component_Type (T))
1218 and then Nkind (Original_Node
1219 (Subtype_Indication (Component_Definition (Def))))
1220 /= N_Attribute_Reference
1223 ("only a subtype mark is allowed in a formal",
1224 Subtype_Indication (Component_Definition (Def)));
1227 end Analyze_Formal_Array_Type;
1229 ---------------------------------------------
1230 -- Analyze_Formal_Decimal_Fixed_Point_Type --
1231 ---------------------------------------------
1233 -- As for other generic types, we create a valid type representation
1234 -- with legal but arbitrary attributes, whose values are never considered
1235 -- static. For all scalar types we introduce an anonymous base type, with
1236 -- the same attributes. We choose the corresponding integer type to be
1237 -- Standard_Integer.
1239 procedure Analyze_Formal_Decimal_Fixed_Point_Type
1243 Loc : constant Source_Ptr := Sloc (Def);
1244 Base : constant Entity_Id :=
1246 (E_Decimal_Fixed_Point_Type,
1247 Current_Scope, Sloc (Def), 'G');
1248 Int_Base : constant Entity_Id := Standard_Integer;
1249 Delta_Val : constant Ureal := Ureal_1;
1250 Digs_Val : constant Uint := Uint_6;
1255 Set_Etype (Base, Base);
1256 Set_Size_Info (Base, Int_Base);
1257 Set_RM_Size (Base, RM_Size (Int_Base));
1258 Set_First_Rep_Item (Base, First_Rep_Item (Int_Base));
1259 Set_Digits_Value (Base, Digs_Val);
1260 Set_Delta_Value (Base, Delta_Val);
1261 Set_Small_Value (Base, Delta_Val);
1262 Set_Scalar_Range (Base,
1264 Low_Bound => Make_Real_Literal (Loc, Ureal_1),
1265 High_Bound => Make_Real_Literal (Loc, Ureal_1)));
1267 Set_Is_Generic_Type (Base);
1268 Set_Parent (Base, Parent (Def));
1270 Set_Ekind (T, E_Decimal_Fixed_Point_Subtype);
1271 Set_Etype (T, Base);
1272 Set_Size_Info (T, Int_Base);
1273 Set_RM_Size (T, RM_Size (Int_Base));
1274 Set_First_Rep_Item (T, First_Rep_Item (Int_Base));
1275 Set_Digits_Value (T, Digs_Val);
1276 Set_Delta_Value (T, Delta_Val);
1277 Set_Small_Value (T, Delta_Val);
1278 Set_Scalar_Range (T, Scalar_Range (Base));
1280 Check_Restriction (No_Fixed_Point, Def);
1281 end Analyze_Formal_Decimal_Fixed_Point_Type;
1283 ---------------------------------
1284 -- Analyze_Formal_Derived_Type --
1285 ---------------------------------
1287 procedure Analyze_Formal_Derived_Type
1292 Loc : constant Source_Ptr := Sloc (Def);
1293 Unk_Disc : constant Boolean := Unknown_Discriminants_Present (N);
1297 Set_Is_Generic_Type (T);
1299 if Private_Present (Def) then
1301 Make_Private_Extension_Declaration (Loc,
1302 Defining_Identifier => T,
1303 Discriminant_Specifications => Discriminant_Specifications (N),
1304 Unknown_Discriminants_Present => Unk_Disc,
1305 Subtype_Indication => Subtype_Mark (Def));
1307 Set_Abstract_Present (New_N, Abstract_Present (Def));
1311 Make_Full_Type_Declaration (Loc,
1312 Defining_Identifier => T,
1313 Discriminant_Specifications =>
1314 Discriminant_Specifications (Parent (T)),
1316 Make_Derived_Type_Definition (Loc,
1317 Subtype_Indication => Subtype_Mark (Def)));
1319 Set_Abstract_Present
1320 (Type_Definition (New_N), Abstract_Present (Def));
1327 if not Is_Composite_Type (T) then
1329 ("unknown discriminants not allowed for elementary types", N);
1331 Set_Has_Unknown_Discriminants (T);
1332 Set_Is_Constrained (T, False);
1336 -- If the parent type has a known size, so does the formal, which
1337 -- makes legal representation clauses that involve the formal.
1339 Set_Size_Known_At_Compile_Time
1340 (T, Size_Known_At_Compile_Time (Entity (Subtype_Mark (Def))));
1342 end Analyze_Formal_Derived_Type;
1344 ----------------------------------
1345 -- Analyze_Formal_Discrete_Type --
1346 ----------------------------------
1348 -- The operations defined for a discrete types are those of an
1349 -- enumeration type. The size is set to an arbitrary value, for use
1350 -- in analyzing the generic unit.
1352 procedure Analyze_Formal_Discrete_Type (T : Entity_Id; Def : Node_Id) is
1353 Loc : constant Source_Ptr := Sloc (Def);
1359 Set_Ekind (T, E_Enumeration_Type);
1364 -- For semantic analysis, the bounds of the type must be set to some
1365 -- non-static value. The simplest is to create attribute nodes for
1366 -- those bounds, that refer to the type itself. These bounds are never
1367 -- analyzed but serve as place-holders.
1370 Make_Attribute_Reference (Loc,
1371 Attribute_Name => Name_First,
1372 Prefix => New_Reference_To (T, Loc));
1376 Make_Attribute_Reference (Loc,
1377 Attribute_Name => Name_Last,
1378 Prefix => New_Reference_To (T, Loc));
1381 Set_Scalar_Range (T,
1386 end Analyze_Formal_Discrete_Type;
1388 ----------------------------------
1389 -- Analyze_Formal_Floating_Type --
1390 ---------------------------------
1392 procedure Analyze_Formal_Floating_Type (T : Entity_Id; Def : Node_Id) is
1393 Base : constant Entity_Id :=
1395 (E_Floating_Point_Type, Current_Scope, Sloc (Def), 'G');
1398 -- The various semantic attributes are taken from the predefined type
1399 -- Float, just so that all of them are initialized. Their values are
1400 -- never used because no constant folding or expansion takes place in
1401 -- the generic itself.
1404 Set_Ekind (T, E_Floating_Point_Subtype);
1405 Set_Etype (T, Base);
1406 Set_Size_Info (T, (Standard_Float));
1407 Set_RM_Size (T, RM_Size (Standard_Float));
1408 Set_Digits_Value (T, Digits_Value (Standard_Float));
1409 Set_Scalar_Range (T, Scalar_Range (Standard_Float));
1411 Set_Is_Generic_Type (Base);
1412 Set_Etype (Base, Base);
1413 Set_Size_Info (Base, (Standard_Float));
1414 Set_RM_Size (Base, RM_Size (Standard_Float));
1415 Set_Digits_Value (Base, Digits_Value (Standard_Float));
1416 Set_Scalar_Range (Base, Scalar_Range (Standard_Float));
1417 Set_Parent (Base, Parent (Def));
1419 Check_Restriction (No_Floating_Point, Def);
1420 end Analyze_Formal_Floating_Type;
1422 ---------------------------------
1423 -- Analyze_Formal_Modular_Type --
1424 ---------------------------------
1426 procedure Analyze_Formal_Modular_Type (T : Entity_Id; Def : Node_Id) is
1428 -- Apart from their entity kind, generic modular types are treated
1429 -- like signed integer types, and have the same attributes.
1431 Analyze_Formal_Signed_Integer_Type (T, Def);
1432 Set_Ekind (T, E_Modular_Integer_Subtype);
1433 Set_Ekind (Etype (T), E_Modular_Integer_Type);
1435 end Analyze_Formal_Modular_Type;
1437 ---------------------------------------
1438 -- Analyze_Formal_Object_Declaration --
1439 ---------------------------------------
1441 procedure Analyze_Formal_Object_Declaration (N : Node_Id) is
1442 E : constant Node_Id := Expression (N);
1443 Id : constant Node_Id := Defining_Identifier (N);
1450 -- Determine the mode of the formal object
1452 if Out_Present (N) then
1453 K := E_Generic_In_Out_Parameter;
1455 if not In_Present (N) then
1456 Error_Msg_N ("formal generic objects cannot have mode OUT", N);
1460 K := E_Generic_In_Parameter;
1463 Find_Type (Subtype_Mark (N));
1464 T := Entity (Subtype_Mark (N));
1466 if Ekind (T) = E_Incomplete_Type then
1467 Error_Msg_N ("premature usage of incomplete type", Subtype_Mark (N));
1470 if K = E_Generic_In_Parameter then
1472 -- Ada 0Y (AI-287): Limited aggregates allowed in generic formals
1474 if not Extensions_Allowed and then Is_Limited_Type (T) then
1476 ("generic formal of mode IN must not be of limited type", N);
1477 Explain_Limited_Type (T, N);
1480 if Is_Abstract (T) then
1482 ("generic formal of mode IN must not be of abstract type", N);
1486 Analyze_Per_Use_Expression (E, T);
1492 -- Case of generic IN OUT parameter.
1495 -- If the formal has an unconstrained type, construct its
1496 -- actual subtype, as is done for subprogram formals. In this
1497 -- fashion, all its uses can refer to specific bounds.
1502 if (Is_Array_Type (T)
1503 and then not Is_Constrained (T))
1505 (Ekind (T) = E_Record_Type
1506 and then Has_Discriminants (T))
1509 Non_Freezing_Ref : constant Node_Id :=
1510 New_Reference_To (Id, Sloc (Id));
1514 -- Make sure that the actual subtype doesn't generate
1517 Set_Must_Not_Freeze (Non_Freezing_Ref);
1518 Decl := Build_Actual_Subtype (T, Non_Freezing_Ref);
1519 Insert_Before_And_Analyze (N, Decl);
1520 Set_Actual_Subtype (Id, Defining_Identifier (Decl));
1523 Set_Actual_Subtype (Id, T);
1528 ("initialization not allowed for `IN OUT` formals", N);
1532 end Analyze_Formal_Object_Declaration;
1534 ----------------------------------------------
1535 -- Analyze_Formal_Ordinary_Fixed_Point_Type --
1536 ----------------------------------------------
1538 procedure Analyze_Formal_Ordinary_Fixed_Point_Type
1542 Loc : constant Source_Ptr := Sloc (Def);
1543 Base : constant Entity_Id :=
1545 (E_Ordinary_Fixed_Point_Type, Current_Scope, Sloc (Def), 'G');
1547 -- The semantic attributes are set for completeness only, their
1548 -- values will never be used, because all properties of the type
1552 Set_Ekind (T, E_Ordinary_Fixed_Point_Subtype);
1553 Set_Etype (T, Base);
1554 Set_Size_Info (T, Standard_Integer);
1555 Set_RM_Size (T, RM_Size (Standard_Integer));
1556 Set_Small_Value (T, Ureal_1);
1557 Set_Delta_Value (T, Ureal_1);
1558 Set_Scalar_Range (T,
1560 Low_Bound => Make_Real_Literal (Loc, Ureal_1),
1561 High_Bound => Make_Real_Literal (Loc, Ureal_1)));
1563 Set_Is_Generic_Type (Base);
1564 Set_Etype (Base, Base);
1565 Set_Size_Info (Base, Standard_Integer);
1566 Set_RM_Size (Base, RM_Size (Standard_Integer));
1567 Set_Small_Value (Base, Ureal_1);
1568 Set_Delta_Value (Base, Ureal_1);
1569 Set_Scalar_Range (Base, Scalar_Range (T));
1570 Set_Parent (Base, Parent (Def));
1572 Check_Restriction (No_Fixed_Point, Def);
1573 end Analyze_Formal_Ordinary_Fixed_Point_Type;
1575 ----------------------------
1576 -- Analyze_Formal_Package --
1577 ----------------------------
1579 procedure Analyze_Formal_Package (N : Node_Id) is
1580 Loc : constant Source_Ptr := Sloc (N);
1581 Pack_Id : constant Entity_Id := Defining_Identifier (N);
1583 Gen_Id : constant Node_Id := Name (N);
1585 Gen_Unit : Entity_Id;
1587 Parent_Installed : Boolean := False;
1589 Parent_Instance : Entity_Id;
1590 Renaming_In_Par : Entity_Id;
1593 Text_IO_Kludge (Gen_Id);
1596 Check_Generic_Child_Unit (Gen_Id, Parent_Installed);
1597 Gen_Unit := Entity (Gen_Id);
1599 if Ekind (Gen_Unit) /= E_Generic_Package then
1600 Error_Msg_N ("expect generic package name", Gen_Id);
1604 elsif Gen_Unit = Current_Scope then
1606 ("generic package cannot be used as a formal package of itself",
1611 elsif In_Open_Scopes (Gen_Unit) then
1612 if Is_Compilation_Unit (Gen_Unit)
1613 and then Is_Child_Unit (Current_Scope)
1615 -- Special-case the error when the formal is a parent, and
1616 -- continue analysis to minimize cascaded errors.
1619 ("generic parent cannot be used as formal package "
1620 & "of a child unit",
1625 ("generic package cannot be used as a formal package "
1633 -- Check for a formal package that is a package renaming.
1635 if Present (Renamed_Object (Gen_Unit)) then
1636 Gen_Unit := Renamed_Object (Gen_Unit);
1639 -- The formal package is treated like a regular instance, but only
1640 -- the specification needs to be instantiated, to make entities visible.
1642 if not Box_Present (N) then
1643 Hidden_Entities := New_Elmt_List;
1644 Analyze_Package_Instantiation (N);
1646 if Parent_Installed then
1651 -- If there are no generic associations, the generic parameters
1652 -- appear as local entities and are instantiated like them. We copy
1653 -- the generic package declaration as if it were an instantiation,
1654 -- and analyze it like a regular package, except that we treat the
1655 -- formals as additional visible components.
1657 Gen_Decl := Unit_Declaration_Node (Gen_Unit);
1659 if In_Extended_Main_Source_Unit (N) then
1660 Set_Is_Instantiated (Gen_Unit);
1661 Generate_Reference (Gen_Unit, N);
1664 Formal := New_Copy (Pack_Id);
1667 (Original_Node (Gen_Decl), Empty, Instantiating => True);
1669 Set_Defining_Unit_Name (Specification (New_N), Formal);
1670 Set_Instance_Env (Gen_Unit, Formal);
1672 Enter_Name (Formal);
1673 Set_Ekind (Formal, E_Generic_Package);
1674 Set_Etype (Formal, Standard_Void_Type);
1675 Set_Inner_Instances (Formal, New_Elmt_List);
1678 -- Within the formal, the name of the generic package is a renaming
1679 -- of the formal (as for a regular instantiation).
1681 Renaming := Make_Package_Renaming_Declaration (Loc,
1682 Defining_Unit_Name =>
1683 Make_Defining_Identifier (Loc, Chars (Gen_Unit)),
1684 Name => New_Reference_To (Formal, Loc));
1686 if Present (Visible_Declarations (Specification (N))) then
1687 Prepend (Renaming, To => Visible_Declarations (Specification (N)));
1688 elsif Present (Private_Declarations (Specification (N))) then
1689 Prepend (Renaming, To => Private_Declarations (Specification (N)));
1692 if Is_Child_Unit (Gen_Unit)
1693 and then Parent_Installed
1695 -- Similarly, we have to make the name of the formal visible in
1696 -- the parent instance, to resolve properly fully qualified names
1697 -- that may appear in the generic unit. The parent instance has
1698 -- been placed on the scope stack ahead of the current scope.
1700 Parent_Instance := Scope_Stack.Table (Scope_Stack.Last - 1).Entity;
1703 Make_Defining_Identifier (Loc, Chars (Gen_Unit));
1704 Set_Ekind (Renaming_In_Par, E_Package);
1705 Set_Etype (Renaming_In_Par, Standard_Void_Type);
1706 Set_Scope (Renaming_In_Par, Parent_Instance);
1707 Set_Parent (Renaming_In_Par, Parent (Formal));
1708 Set_Renamed_Object (Renaming_In_Par, Formal);
1709 Append_Entity (Renaming_In_Par, Parent_Instance);
1712 Analyze_Generic_Formal_Part (N);
1713 Analyze (Specification (N));
1714 End_Package_Scope (Formal);
1716 if Parent_Installed then
1722 -- Inside the generic unit, the formal package is a regular
1723 -- package, but no body is needed for it. Note that after
1724 -- instantiation, the defining_unit_name we need is in the
1725 -- new tree and not in the original. (see Package_Instantiation).
1726 -- A generic formal package is an instance, and can be used as
1727 -- an actual for an inner instance. Mark its generic parent.
1729 Set_Ekind (Formal, E_Package);
1730 Set_Generic_Parent (Specification (N), Gen_Unit);
1731 Set_Has_Completion (Formal, True);
1733 Set_Ekind (Pack_Id, E_Package);
1734 Set_Etype (Pack_Id, Standard_Void_Type);
1735 Set_Scope (Pack_Id, Scope (Formal));
1736 Set_Has_Completion (Pack_Id, True);
1738 end Analyze_Formal_Package;
1740 ---------------------------------
1741 -- Analyze_Formal_Private_Type --
1742 ---------------------------------
1744 procedure Analyze_Formal_Private_Type
1750 New_Private_Type (N, T, Def);
1752 -- Set the size to an arbitrary but legal value.
1754 Set_Size_Info (T, Standard_Integer);
1755 Set_RM_Size (T, RM_Size (Standard_Integer));
1756 end Analyze_Formal_Private_Type;
1758 ----------------------------------------
1759 -- Analyze_Formal_Signed_Integer_Type --
1760 ----------------------------------------
1762 procedure Analyze_Formal_Signed_Integer_Type
1766 Base : constant Entity_Id :=
1768 (E_Signed_Integer_Type, Current_Scope, Sloc (Def), 'G');
1773 Set_Ekind (T, E_Signed_Integer_Subtype);
1774 Set_Etype (T, Base);
1775 Set_Size_Info (T, Standard_Integer);
1776 Set_RM_Size (T, RM_Size (Standard_Integer));
1777 Set_Scalar_Range (T, Scalar_Range (Standard_Integer));
1779 Set_Is_Generic_Type (Base);
1780 Set_Size_Info (Base, Standard_Integer);
1781 Set_RM_Size (Base, RM_Size (Standard_Integer));
1782 Set_Etype (Base, Base);
1783 Set_Scalar_Range (Base, Scalar_Range (Standard_Integer));
1784 Set_Parent (Base, Parent (Def));
1785 end Analyze_Formal_Signed_Integer_Type;
1787 -------------------------------
1788 -- Analyze_Formal_Subprogram --
1789 -------------------------------
1791 procedure Analyze_Formal_Subprogram (N : Node_Id) is
1792 Spec : constant Node_Id := Specification (N);
1793 Def : constant Node_Id := Default_Name (N);
1794 Nam : constant Entity_Id := Defining_Unit_Name (Spec);
1802 if Nkind (Nam) = N_Defining_Program_Unit_Name then
1803 Error_Msg_N ("name of formal subprogram must be a direct name", Nam);
1807 Analyze_Subprogram_Declaration (N);
1808 Set_Is_Formal_Subprogram (Nam);
1809 Set_Has_Completion (Nam);
1811 -- Default name is resolved at the point of instantiation
1813 if Box_Present (N) then
1816 -- Else default is bound at the point of generic declaration
1818 elsif Present (Def) then
1819 if Nkind (Def) = N_Operator_Symbol then
1820 Find_Direct_Name (Def);
1822 elsif Nkind (Def) /= N_Attribute_Reference then
1826 -- For an attribute reference, analyze the prefix and verify
1827 -- that it has the proper profile for the subprogram.
1829 Analyze (Prefix (Def));
1830 Valid_Default_Attribute (Nam, Def);
1834 -- Default name may be overloaded, in which case the interpretation
1835 -- with the correct profile must be selected, as for a renaming.
1837 if Etype (Def) = Any_Type then
1840 elsif Nkind (Def) = N_Selected_Component then
1841 Subp := Entity (Selector_Name (Def));
1843 if Ekind (Subp) /= E_Entry then
1844 Error_Msg_N ("expect valid subprogram name as default", Def);
1848 elsif Nkind (Def) = N_Indexed_Component then
1850 if Nkind (Prefix (Def)) /= N_Selected_Component then
1851 Error_Msg_N ("expect valid subprogram name as default", Def);
1855 Subp := Entity (Selector_Name (Prefix (Def)));
1857 if Ekind (Subp) /= E_Entry_Family then
1858 Error_Msg_N ("expect valid subprogram name as default", Def);
1863 elsif Nkind (Def) = N_Character_Literal then
1865 -- Needs some type checks: subprogram should be parameterless???
1867 Resolve (Def, (Etype (Nam)));
1869 elsif not Is_Entity_Name (Def)
1870 or else not Is_Overloadable (Entity (Def))
1872 Error_Msg_N ("expect valid subprogram name as default", Def);
1875 elsif not Is_Overloaded (Def) then
1876 Subp := Entity (Def);
1879 Error_Msg_N ("premature usage of formal subprogram", Def);
1881 elsif not Entity_Matches_Spec (Subp, Nam) then
1882 Error_Msg_N ("no visible entity matches specification", Def);
1888 I1 : Interp_Index := 0;
1894 Get_First_Interp (Def, I, It);
1895 while Present (It.Nam) loop
1897 if Entity_Matches_Spec (It.Nam, Nam) then
1898 if Subp /= Any_Id then
1899 It1 := Disambiguate (Def, I1, I, Etype (Subp));
1901 if It1 = No_Interp then
1902 Error_Msg_N ("ambiguous default subprogram", Def);
1915 Get_Next_Interp (I, It);
1919 if Subp /= Any_Id then
1920 Set_Entity (Def, Subp);
1923 Error_Msg_N ("premature usage of formal subprogram", Def);
1925 elsif Ekind (Subp) /= E_Operator then
1926 Check_Mode_Conformant (Subp, Nam);
1930 Error_Msg_N ("no visible subprogram matches specification", N);
1934 end Analyze_Formal_Subprogram;
1936 -------------------------------------
1937 -- Analyze_Formal_Type_Declaration --
1938 -------------------------------------
1940 procedure Analyze_Formal_Type_Declaration (N : Node_Id) is
1941 Def : constant Node_Id := Formal_Type_Definition (N);
1945 T := Defining_Identifier (N);
1947 if Present (Discriminant_Specifications (N))
1948 and then Nkind (Def) /= N_Formal_Private_Type_Definition
1951 ("discriminants not allowed for this formal type",
1952 Defining_Identifier (First (Discriminant_Specifications (N))));
1955 -- Enter the new name, and branch to specific routine.
1958 when N_Formal_Private_Type_Definition =>
1959 Analyze_Formal_Private_Type (N, T, Def);
1961 when N_Formal_Derived_Type_Definition =>
1962 Analyze_Formal_Derived_Type (N, T, Def);
1964 when N_Formal_Discrete_Type_Definition =>
1965 Analyze_Formal_Discrete_Type (T, Def);
1967 when N_Formal_Signed_Integer_Type_Definition =>
1968 Analyze_Formal_Signed_Integer_Type (T, Def);
1970 when N_Formal_Modular_Type_Definition =>
1971 Analyze_Formal_Modular_Type (T, Def);
1973 when N_Formal_Floating_Point_Definition =>
1974 Analyze_Formal_Floating_Type (T, Def);
1976 when N_Formal_Ordinary_Fixed_Point_Definition =>
1977 Analyze_Formal_Ordinary_Fixed_Point_Type (T, Def);
1979 when N_Formal_Decimal_Fixed_Point_Definition =>
1980 Analyze_Formal_Decimal_Fixed_Point_Type (T, Def);
1982 when N_Array_Type_Definition =>
1983 Analyze_Formal_Array_Type (T, Def);
1985 when N_Access_To_Object_Definition |
1986 N_Access_Function_Definition |
1987 N_Access_Procedure_Definition =>
1988 Analyze_Generic_Access_Type (T, Def);
1994 raise Program_Error;
1998 Set_Is_Generic_Type (T);
1999 end Analyze_Formal_Type_Declaration;
2001 ------------------------------------
2002 -- Analyze_Function_Instantiation --
2003 ------------------------------------
2005 procedure Analyze_Function_Instantiation (N : Node_Id) is
2007 Analyze_Subprogram_Instantiation (N, E_Function);
2008 end Analyze_Function_Instantiation;
2010 ---------------------------------
2011 -- Analyze_Generic_Access_Type --
2012 ---------------------------------
2014 procedure Analyze_Generic_Access_Type (T : Entity_Id; Def : Node_Id) is
2018 if Nkind (Def) = N_Access_To_Object_Definition then
2019 Access_Type_Declaration (T, Def);
2021 if Is_Incomplete_Or_Private_Type (Designated_Type (T))
2022 and then No (Full_View (Designated_Type (T)))
2023 and then not Is_Generic_Type (Designated_Type (T))
2025 Error_Msg_N ("premature usage of incomplete type", Def);
2027 elsif Is_Internal (Designated_Type (T)) then
2029 ("only a subtype mark is allowed in a formal", Def);
2033 Access_Subprogram_Declaration (T, Def);
2035 end Analyze_Generic_Access_Type;
2037 ---------------------------------
2038 -- Analyze_Generic_Formal_Part --
2039 ---------------------------------
2041 procedure Analyze_Generic_Formal_Part (N : Node_Id) is
2042 Gen_Parm_Decl : Node_Id;
2045 -- The generic formals are processed in the scope of the generic
2046 -- unit, where they are immediately visible. The scope is installed
2049 Gen_Parm_Decl := First (Generic_Formal_Declarations (N));
2051 while Present (Gen_Parm_Decl) loop
2052 Analyze (Gen_Parm_Decl);
2053 Next (Gen_Parm_Decl);
2056 Generate_Reference_To_Generic_Formals (Current_Scope);
2057 end Analyze_Generic_Formal_Part;
2059 ------------------------------------------
2060 -- Analyze_Generic_Package_Declaration --
2061 ------------------------------------------
2063 procedure Analyze_Generic_Package_Declaration (N : Node_Id) is
2064 Loc : constant Source_Ptr := Sloc (N);
2067 Save_Parent : Node_Id;
2069 Decls : constant List_Id :=
2070 Visible_Declarations (Specification (N));
2074 -- We introduce a renaming of the enclosing package, to have a usable
2075 -- entity as the prefix of an expanded name for a local entity of the
2076 -- form Par.P.Q, where P is the generic package. This is because a local
2077 -- entity named P may hide it, so that the usual visibility rules in
2078 -- the instance will not resolve properly.
2081 Make_Package_Renaming_Declaration (Loc,
2082 Defining_Unit_Name =>
2083 Make_Defining_Identifier (Loc,
2084 Chars => New_External_Name (Chars (Defining_Entity (N)), "GH")),
2085 Name => Make_Identifier (Loc, Chars (Defining_Entity (N))));
2087 if Present (Decls) then
2088 Decl := First (Decls);
2089 while Present (Decl)
2090 and then Nkind (Decl) = N_Pragma
2095 if Present (Decl) then
2096 Insert_Before (Decl, Renaming);
2098 Append (Renaming, Visible_Declarations (Specification (N)));
2102 Set_Visible_Declarations (Specification (N), New_List (Renaming));
2105 -- Create copy of generic unit, and save for instantiation.
2106 -- If the unit is a child unit, do not copy the specifications
2107 -- for the parent, which are not part of the generic tree.
2109 Save_Parent := Parent_Spec (N);
2110 Set_Parent_Spec (N, Empty);
2112 New_N := Copy_Generic_Node (N, Empty, Instantiating => False);
2113 Set_Parent_Spec (New_N, Save_Parent);
2115 Id := Defining_Entity (N);
2116 Generate_Definition (Id);
2118 -- Expansion is not applied to generic units.
2123 Set_Ekind (Id, E_Generic_Package);
2124 Set_Etype (Id, Standard_Void_Type);
2126 Enter_Generic_Scope (Id);
2127 Set_Inner_Instances (Id, New_Elmt_List);
2129 Set_Categorization_From_Pragmas (N);
2130 Set_Is_Pure (Id, Is_Pure (Current_Scope));
2132 -- Link the declaration of the generic homonym in the generic copy
2133 -- to the package it renames, so that it is always resolved properly.
2135 Set_Generic_Homonym (Id, Defining_Unit_Name (Renaming));
2136 Set_Entity (Associated_Node (Name (Renaming)), Id);
2138 -- For a library unit, we have reconstructed the entity for the
2139 -- unit, and must reset it in the library tables.
2141 if Nkind (Parent (N)) = N_Compilation_Unit then
2142 Set_Cunit_Entity (Current_Sem_Unit, Id);
2145 Analyze_Generic_Formal_Part (N);
2147 -- After processing the generic formals, analysis proceeds
2148 -- as for a non-generic package.
2150 Analyze (Specification (N));
2152 Validate_Categorization_Dependency (N, Id);
2156 End_Package_Scope (Id);
2157 Exit_Generic_Scope (Id);
2159 if Nkind (Parent (N)) /= N_Compilation_Unit then
2160 Move_Freeze_Nodes (Id, N, Visible_Declarations (Specification (N)));
2161 Move_Freeze_Nodes (Id, N, Private_Declarations (Specification (N)));
2162 Move_Freeze_Nodes (Id, N, Generic_Formal_Declarations (N));
2165 Set_Body_Required (Parent (N), Unit_Requires_Body (Id));
2166 Validate_RT_RAT_Component (N);
2168 -- If this is a spec without a body, check that generic parameters
2171 if not Body_Required (Parent (N)) then
2172 Check_References (Id);
2175 end Analyze_Generic_Package_Declaration;
2177 --------------------------------------------
2178 -- Analyze_Generic_Subprogram_Declaration --
2179 --------------------------------------------
2181 procedure Analyze_Generic_Subprogram_Declaration (N : Node_Id) is
2186 Save_Parent : Node_Id;
2189 -- Create copy of generic unit,and save for instantiation.
2190 -- If the unit is a child unit, do not copy the specifications
2191 -- for the parent, which are not part of the generic tree.
2193 Save_Parent := Parent_Spec (N);
2194 Set_Parent_Spec (N, Empty);
2196 New_N := Copy_Generic_Node (N, Empty, Instantiating => False);
2197 Set_Parent_Spec (New_N, Save_Parent);
2200 Spec := Specification (N);
2201 Id := Defining_Entity (Spec);
2202 Generate_Definition (Id);
2204 if Nkind (Id) = N_Defining_Operator_Symbol then
2206 ("operator symbol not allowed for generic subprogram", Id);
2213 Set_Scope_Depth_Value (Id, Scope_Depth (Current_Scope) + 1);
2215 Enter_Generic_Scope (Id);
2216 Set_Inner_Instances (Id, New_Elmt_List);
2217 Set_Is_Pure (Id, Is_Pure (Current_Scope));
2219 Analyze_Generic_Formal_Part (N);
2221 Formals := Parameter_Specifications (Spec);
2223 if Present (Formals) then
2224 Process_Formals (Formals, Spec);
2227 if Nkind (Spec) = N_Function_Specification then
2228 Set_Ekind (Id, E_Generic_Function);
2229 Find_Type (Subtype_Mark (Spec));
2230 Set_Etype (Id, Entity (Subtype_Mark (Spec)));
2232 Set_Ekind (Id, E_Generic_Procedure);
2233 Set_Etype (Id, Standard_Void_Type);
2236 -- For a library unit, we have reconstructed the entity for the
2237 -- unit, and must reset it in the library tables. We also need
2238 -- to make sure that Body_Required is set properly in the original
2239 -- compilation unit node.
2241 if Nkind (Parent (N)) = N_Compilation_Unit then
2242 Set_Cunit_Entity (Current_Sem_Unit, Id);
2243 Set_Body_Required (Parent (N), Unit_Requires_Body (Id));
2246 Set_Categorization_From_Pragmas (N);
2247 Validate_Categorization_Dependency (N, Id);
2249 Save_Global_References (Original_Node (N));
2253 Exit_Generic_Scope (Id);
2254 Generate_Reference_To_Formals (Id);
2255 end Analyze_Generic_Subprogram_Declaration;
2257 -----------------------------------
2258 -- Analyze_Package_Instantiation --
2259 -----------------------------------
2261 -- Note: this procedure is also used for formal package declarations,
2262 -- in which case the argument N is an N_Formal_Package_Declaration
2263 -- node. This should really be noted in the spec! ???
2265 procedure Analyze_Package_Instantiation (N : Node_Id) is
2266 Loc : constant Source_Ptr := Sloc (N);
2267 Gen_Id : constant Node_Id := Name (N);
2270 Act_Decl_Name : Node_Id;
2271 Act_Decl_Id : Entity_Id;
2276 Gen_Unit : Entity_Id;
2278 Is_Actual_Pack : constant Boolean :=
2279 Is_Internal (Defining_Entity (N));
2281 Parent_Installed : Boolean := False;
2282 Renaming_List : List_Id;
2283 Unit_Renaming : Node_Id;
2284 Needs_Body : Boolean;
2285 Inline_Now : Boolean := False;
2287 procedure Delay_Descriptors (E : Entity_Id);
2288 -- Delay generation of subprogram descriptors for given entity
2290 function Might_Inline_Subp return Boolean;
2291 -- If inlining is active and the generic contains inlined subprograms,
2292 -- we instantiate the body. This may cause superfluous instantiations,
2293 -- but it is simpler than detecting the need for the body at the point
2294 -- of inlining, when the context of the instance is not available.
2296 -----------------------
2297 -- Delay_Descriptors --
2298 -----------------------
2300 procedure Delay_Descriptors (E : Entity_Id) is
2302 if not Delay_Subprogram_Descriptors (E) then
2303 Set_Delay_Subprogram_Descriptors (E);
2304 Pending_Descriptor.Increment_Last;
2305 Pending_Descriptor.Table (Pending_Descriptor.Last) := E;
2307 end Delay_Descriptors;
2309 -----------------------
2310 -- Might_Inline_Subp --
2311 -----------------------
2313 function Might_Inline_Subp return Boolean is
2317 if not Inline_Processing_Required then
2321 E := First_Entity (Gen_Unit);
2323 while Present (E) loop
2325 if Is_Subprogram (E)
2326 and then Is_Inlined (E)
2336 end Might_Inline_Subp;
2338 -- Start of processing for Analyze_Package_Instantiation
2341 -- Very first thing: apply the special kludge for Text_IO processing
2342 -- in case we are instantiating one of the children of [Wide_]Text_IO.
2344 Text_IO_Kludge (Name (N));
2346 -- Make node global for error reporting.
2348 Instantiation_Node := N;
2350 -- Case of instantiation of a generic package
2352 if Nkind (N) = N_Package_Instantiation then
2353 Act_Decl_Id := New_Copy (Defining_Entity (N));
2354 Set_Comes_From_Source (Act_Decl_Id, True);
2356 if Nkind (Defining_Unit_Name (N)) = N_Defining_Program_Unit_Name then
2358 Make_Defining_Program_Unit_Name (Loc,
2359 Name => New_Copy_Tree (Name (Defining_Unit_Name (N))),
2360 Defining_Identifier => Act_Decl_Id);
2362 Act_Decl_Name := Act_Decl_Id;
2365 -- Case of instantiation of a formal package
2368 Act_Decl_Id := Defining_Identifier (N);
2369 Act_Decl_Name := Act_Decl_Id;
2372 Generate_Definition (Act_Decl_Id);
2373 Pre_Analyze_Actuals (N);
2376 Check_Generic_Child_Unit (Gen_Id, Parent_Installed);
2377 Gen_Unit := Entity (Gen_Id);
2379 -- Verify that it is the name of a generic package
2381 if Etype (Gen_Unit) = Any_Type then
2385 elsif Ekind (Gen_Unit) /= E_Generic_Package then
2387 -- Ada 0Y (AI-50217): Instance can not be used in limited with_clause
2389 if From_With_Type (Gen_Unit) then
2391 ("cannot instantiate a limited withed package", Gen_Id);
2394 ("expect name of generic package in instantiation", Gen_Id);
2401 if In_Extended_Main_Source_Unit (N) then
2402 Set_Is_Instantiated (Gen_Unit);
2403 Generate_Reference (Gen_Unit, N);
2405 if Present (Renamed_Object (Gen_Unit)) then
2406 Set_Is_Instantiated (Renamed_Object (Gen_Unit));
2407 Generate_Reference (Renamed_Object (Gen_Unit), N);
2411 if Nkind (Gen_Id) = N_Identifier
2412 and then Chars (Gen_Unit) = Chars (Defining_Entity (N))
2415 ("& is hidden within declaration of instance", Gen_Id, Gen_Unit);
2417 elsif Nkind (Gen_Id) = N_Expanded_Name
2418 and then Is_Child_Unit (Gen_Unit)
2419 and then Nkind (Prefix (Gen_Id)) = N_Identifier
2420 and then Chars (Act_Decl_Id) = Chars (Prefix (Gen_Id))
2423 ("& is hidden within declaration of instance ", Prefix (Gen_Id));
2426 Set_Entity (Gen_Id, Gen_Unit);
2428 -- If generic is a renaming, get original generic unit.
2430 if Present (Renamed_Object (Gen_Unit))
2431 and then Ekind (Renamed_Object (Gen_Unit)) = E_Generic_Package
2433 Gen_Unit := Renamed_Object (Gen_Unit);
2436 -- Verify that there are no circular instantiations.
2438 if In_Open_Scopes (Gen_Unit) then
2439 Error_Msg_NE ("instantiation of & within itself", N, Gen_Unit);
2443 elsif Contains_Instance_Of (Gen_Unit, Current_Scope, Gen_Id) then
2444 Error_Msg_Node_2 := Current_Scope;
2446 ("circular Instantiation: & instantiated in &!", N, Gen_Unit);
2447 Circularity_Detected := True;
2452 Set_Instance_Env (Gen_Unit, Act_Decl_Id);
2453 Gen_Decl := Unit_Declaration_Node (Gen_Unit);
2455 -- Initialize renamings map, for error checking, and the list
2456 -- that holds private entities whose views have changed between
2457 -- generic definition and instantiation. If this is the instance
2458 -- created to validate an actual package, the instantiation
2459 -- environment is that of the enclosing instance.
2461 Generic_Renamings.Set_Last (0);
2462 Generic_Renamings_HTable.Reset;
2464 Create_Instantiation_Source (N, Gen_Unit, False, S_Adjustment);
2466 -- Copy original generic tree, to produce text for instantiation.
2470 (Original_Node (Gen_Decl), Empty, Instantiating => True);
2472 Act_Spec := Specification (Act_Tree);
2474 -- If this is the instance created to validate an actual package,
2475 -- only the formals matter, do not examine the package spec itself.
2477 if Is_Actual_Pack then
2478 Set_Visible_Declarations (Act_Spec, New_List);
2479 Set_Private_Declarations (Act_Spec, New_List);
2483 Analyze_Associations
2485 Generic_Formal_Declarations (Act_Tree),
2486 Generic_Formal_Declarations (Gen_Decl));
2488 Set_Defining_Unit_Name (Act_Spec, Act_Decl_Name);
2489 Set_Is_Generic_Instance (Act_Decl_Id);
2491 Set_Generic_Parent (Act_Spec, Gen_Unit);
2493 -- References to the generic in its own declaration or its body
2494 -- are references to the instance. Add a renaming declaration for
2495 -- the generic unit itself. This declaration, as well as the renaming
2496 -- declarations for the generic formals, must remain private to the
2497 -- unit: the formals, because this is the language semantics, and
2498 -- the unit because its use is an artifact of the implementation.
2501 Make_Package_Renaming_Declaration (Loc,
2502 Defining_Unit_Name =>
2503 Make_Defining_Identifier (Loc, Chars (Gen_Unit)),
2504 Name => New_Reference_To (Act_Decl_Id, Loc));
2506 Append (Unit_Renaming, Renaming_List);
2508 -- The renaming declarations are the first local declarations of
2511 if Is_Non_Empty_List (Visible_Declarations (Act_Spec)) then
2513 (First (Visible_Declarations (Act_Spec)), Renaming_List);
2515 Set_Visible_Declarations (Act_Spec, Renaming_List);
2519 Make_Package_Declaration (Loc,
2520 Specification => Act_Spec);
2522 -- Save the instantiation node, for subsequent instantiation
2523 -- of the body, if there is one and we are generating code for
2524 -- the current unit. Mark the unit as having a body, to avoid
2525 -- a premature error message.
2527 -- We instantiate the body if we are generating code, if we are
2528 -- generating cross-reference information, or if we are building
2529 -- trees for ASIS use.
2532 Enclosing_Body_Present : Boolean := False;
2533 -- If the generic unit is not a compilation unit, then a body
2534 -- may be present in its parent even if none is required. We
2535 -- create a tentative pending instantiation for the body, which
2536 -- will be discarded if none is actually present.
2541 if Scope (Gen_Unit) /= Standard_Standard
2542 and then not Is_Child_Unit (Gen_Unit)
2544 Scop := Scope (Gen_Unit);
2546 while Present (Scop)
2547 and then Scop /= Standard_Standard
2549 if Unit_Requires_Body (Scop) then
2550 Enclosing_Body_Present := True;
2554 exit when Is_Compilation_Unit (Scop);
2555 Scop := Scope (Scop);
2559 -- If front-end inlining is enabled, and this is a unit for which
2560 -- code will be generated, we instantiate the body at once.
2561 -- This is done if the instance is not the main unit, and if the
2562 -- generic is not a child unit of another generic, to avoid scope
2563 -- problems and the reinstallation of parent instances.
2565 if Front_End_Inlining
2566 and then Expander_Active
2567 and then (not Is_Child_Unit (Gen_Unit)
2568 or else not Is_Generic_Unit (Scope (Gen_Unit)))
2569 and then Is_In_Main_Unit (N)
2570 and then Nkind (Parent (N)) /= N_Compilation_Unit
2571 and then Might_Inline_Subp
2572 and then not Is_Actual_Pack
2578 (Unit_Requires_Body (Gen_Unit)
2579 or else Enclosing_Body_Present
2580 or else Present (Corresponding_Body (Gen_Decl)))
2581 and then (Is_In_Main_Unit (N)
2582 or else Might_Inline_Subp)
2583 and then not Is_Actual_Pack
2584 and then not Inline_Now
2586 and then (Operating_Mode = Generate_Code
2587 or else (Operating_Mode = Check_Semantics
2588 and then ASIS_Mode));
2590 -- If front_end_inlining is enabled, do not instantiate a
2591 -- body if within a generic context.
2593 if Front_End_Inlining
2594 and then not Expander_Active
2596 Needs_Body := False;
2599 -- If the current context is generic, and the package being
2600 -- instantiated is declared within a formal package, there
2601 -- is no body to instantiate until the enclosing generic is
2602 -- instantiated, and there is an actual for the formal
2603 -- package. If the formal package has parameters, we build a
2604 -- regular package instance for it, that preceeds the original
2605 -- formal package declaration.
2607 if In_Open_Scopes (Scope (Scope (Gen_Unit))) then
2609 Decl : constant Node_Id :=
2611 (Unit_Declaration_Node (Scope (Gen_Unit)));
2613 if Nkind (Decl) = N_Formal_Package_Declaration
2614 or else (Nkind (Decl) = N_Package_Declaration
2615 and then Is_List_Member (Decl)
2616 and then Present (Next (Decl))
2618 Nkind (Next (Decl)) = N_Formal_Package_Declaration)
2620 Needs_Body := False;
2626 -- If we are generating the calling stubs from the instantiation
2627 -- of a generic RCI package, we will not use the body of the
2630 if Distribution_Stub_Mode = Generate_Caller_Stub_Body
2631 and then Is_Compilation_Unit (Defining_Entity (N))
2633 Needs_Body := False;
2638 -- Here is a defence against a ludicrous number of instantiations
2639 -- caused by a circular set of instantiation attempts.
2641 if Pending_Instantiations.Last >
2642 Hostparm.Max_Instantiations
2644 Error_Msg_N ("too many instantiations", N);
2645 raise Unrecoverable_Error;
2648 -- Indicate that the enclosing scopes contain an instantiation,
2649 -- and that cleanup actions should be delayed until after the
2650 -- instance body is expanded.
2652 Check_Forward_Instantiation (Gen_Decl);
2653 if Nkind (N) = N_Package_Instantiation then
2655 Enclosing_Master : Entity_Id := Current_Scope;
2658 while Enclosing_Master /= Standard_Standard loop
2660 if Ekind (Enclosing_Master) = E_Package then
2661 if Is_Compilation_Unit (Enclosing_Master) then
2662 if In_Package_Body (Enclosing_Master) then
2664 (Body_Entity (Enclosing_Master));
2673 Enclosing_Master := Scope (Enclosing_Master);
2676 elsif Ekind (Enclosing_Master) = E_Generic_Package then
2677 Enclosing_Master := Scope (Enclosing_Master);
2679 elsif Is_Generic_Subprogram (Enclosing_Master)
2680 or else Ekind (Enclosing_Master) = E_Void
2682 -- Cleanup actions will eventually be performed on
2683 -- the enclosing instance, if any. enclosing scope
2684 -- is void in the formal part of a generic subp.
2689 if Ekind (Enclosing_Master) = E_Entry
2691 Ekind (Scope (Enclosing_Master)) = E_Protected_Type
2694 Protected_Body_Subprogram (Enclosing_Master);
2697 Set_Delay_Cleanups (Enclosing_Master);
2699 while Ekind (Enclosing_Master) = E_Block loop
2700 Enclosing_Master := Scope (Enclosing_Master);
2703 if Is_Subprogram (Enclosing_Master) then
2704 Delay_Descriptors (Enclosing_Master);
2706 elsif Is_Task_Type (Enclosing_Master) then
2708 TBP : constant Node_Id :=
2709 Get_Task_Body_Procedure
2713 if Present (TBP) then
2714 Delay_Descriptors (TBP);
2715 Set_Delay_Cleanups (TBP);
2725 -- Make entry in table
2727 Pending_Instantiations.Increment_Last;
2728 Pending_Instantiations.Table (Pending_Instantiations.Last) :=
2729 (N, Act_Decl, Expander_Active, Current_Sem_Unit);
2733 Set_Categorization_From_Pragmas (Act_Decl);
2735 if Parent_Installed then
2739 Set_Instance_Spec (N, Act_Decl);
2741 -- If not a compilation unit, insert the package declaration
2742 -- before the original instantiation node.
2744 if Nkind (Parent (N)) /= N_Compilation_Unit then
2745 Mark_Rewrite_Insertion (Act_Decl);
2746 Insert_Before (N, Act_Decl);
2749 -- For an instantiation that is a compilation unit, place
2750 -- declaration on current node so context is complete
2751 -- for analysis (including nested instantiations). It this
2752 -- is the main unit, the declaration eventually replaces the
2753 -- instantiation node. If the instance body is later created, it
2754 -- replaces the instance node, and the declation is attached to
2755 -- it (see Build_Instance_Compilation_Unit_Nodes).
2758 if Cunit_Entity (Current_Sem_Unit) = Defining_Entity (N) then
2760 -- The entity for the current unit is the newly created one,
2761 -- and all semantic information is attached to it.
2763 Set_Cunit_Entity (Current_Sem_Unit, Act_Decl_Id);
2765 -- If this is the main unit, replace the main entity as well.
2767 if Current_Sem_Unit = Main_Unit then
2768 Main_Unit_Entity := Act_Decl_Id;
2772 Set_Unit (Parent (N), Act_Decl);
2773 Set_Parent_Spec (Act_Decl, Parent_Spec (N));
2775 Set_Unit (Parent (N), N);
2776 Set_Body_Required (Parent (N), False);
2778 -- We never need elaboration checks on instantiations, since
2779 -- by definition, the body instantiation is elaborated at the
2780 -- same time as the spec instantiation.
2782 Set_Suppress_Elaboration_Warnings (Act_Decl_Id);
2783 Set_Kill_Elaboration_Checks (Act_Decl_Id);
2786 Check_Elab_Instantiation (N);
2788 if ABE_Is_Certain (N) and then Needs_Body then
2789 Pending_Instantiations.Decrement_Last;
2791 Check_Hidden_Child_Unit (N, Gen_Unit, Act_Decl_Id);
2793 Set_First_Private_Entity (Defining_Unit_Name (Unit_Renaming),
2794 First_Private_Entity (Act_Decl_Id));
2796 -- If the instantiation will receive a body, the unit will
2797 -- be transformed into a package body, and receive its own
2798 -- elaboration entity. Otherwise, the nature of the unit is
2799 -- now a package declaration.
2801 if Nkind (Parent (N)) = N_Compilation_Unit
2802 and then not Needs_Body
2804 Rewrite (N, Act_Decl);
2807 if Present (Corresponding_Body (Gen_Decl))
2808 or else Unit_Requires_Body (Gen_Unit)
2810 Set_Has_Completion (Act_Decl_Id);
2813 Check_Formal_Packages (Act_Decl_Id);
2815 Restore_Private_Views (Act_Decl_Id);
2817 if not Generic_Separately_Compiled (Gen_Unit) then
2818 Inherit_Context (Gen_Decl, N);
2821 if Parent_Installed then
2828 Validate_Categorization_Dependency (N, Act_Decl_Id);
2830 -- Check restriction, but skip this if something went wrong in
2831 -- the above analysis, indicated by Act_Decl_Id being void.
2833 if Ekind (Act_Decl_Id) /= E_Void
2834 and then not Is_Library_Level_Entity (Act_Decl_Id)
2836 Check_Restriction (No_Local_Allocators, N);
2840 Inline_Instance_Body (N, Gen_Unit, Act_Decl);
2844 when Instantiation_Error =>
2845 if Parent_Installed then
2848 end Analyze_Package_Instantiation;
2850 ---------------------------
2851 -- Inline_Instance_Body --
2852 ---------------------------
2854 procedure Inline_Instance_Body
2856 Gen_Unit : Entity_Id;
2860 Gen_Comp : constant Entity_Id :=
2861 Cunit_Entity (Get_Source_Unit (Gen_Unit));
2862 Curr_Comp : constant Node_Id := Cunit (Current_Sem_Unit);
2863 Curr_Scope : Entity_Id := Empty;
2864 Curr_Unit : constant Entity_Id :=
2865 Cunit_Entity (Current_Sem_Unit);
2866 Removed : Boolean := False;
2867 Num_Scopes : Int := 0;
2868 Use_Clauses : array (1 .. Scope_Stack.Last) of Node_Id;
2869 Instances : array (1 .. Scope_Stack.Last) of Entity_Id;
2870 Inner_Scopes : array (1 .. Scope_Stack.Last) of Entity_Id;
2871 Num_Inner : Int := 0;
2872 N_Instances : Int := 0;
2876 -- Case of generic unit defined in another unit. We must remove
2877 -- the complete context of the current unit to install that of
2880 if Gen_Comp /= Cunit_Entity (Current_Sem_Unit) then
2884 and then S /= Standard_Standard
2886 Num_Scopes := Num_Scopes + 1;
2888 Use_Clauses (Num_Scopes) :=
2890 (Scope_Stack.Last - Num_Scopes + 1).
2892 End_Use_Clauses (Use_Clauses (Num_Scopes));
2894 exit when Is_Generic_Instance (S)
2895 and then (In_Package_Body (S)
2896 or else Ekind (S) = E_Procedure
2897 or else Ekind (S) = E_Function);
2901 Vis := Is_Immediately_Visible (Gen_Comp);
2903 -- Find and save all enclosing instances
2908 and then S /= Standard_Standard
2910 if Is_Generic_Instance (S) then
2911 N_Instances := N_Instances + 1;
2912 Instances (N_Instances) := S;
2914 exit when In_Package_Body (S);
2920 -- Remove context of current compilation unit, unless we
2921 -- are within a nested package instantiation, in which case
2922 -- the context has been removed previously.
2924 -- If current scope is the body of a child unit, remove context
2930 and then S /= Standard_Standard
2932 exit when Is_Generic_Instance (S)
2933 and then (In_Package_Body (S)
2934 or else Ekind (S) = E_Procedure
2935 or else Ekind (S) = E_Function);
2938 or else (Ekind (Curr_Unit) = E_Package_Body
2939 and then S = Spec_Entity (Curr_Unit))
2940 or else (Ekind (Curr_Unit) = E_Subprogram_Body
2943 (Unit_Declaration_Node (Curr_Unit)))
2947 -- Remove entities in current scopes from visibility, so
2948 -- than instance body is compiled in a clean environment.
2950 Save_Scope_Stack (Handle_Use => False);
2952 if Is_Child_Unit (S) then
2954 -- Remove child unit from stack, as well as inner scopes.
2955 -- Removing the context of a child unit removes parent
2958 while Current_Scope /= S loop
2959 Num_Inner := Num_Inner + 1;
2960 Inner_Scopes (Num_Inner) := Current_Scope;
2965 Remove_Context (Curr_Comp);
2969 Remove_Context (Curr_Comp);
2972 if Ekind (Curr_Unit) = E_Package_Body then
2973 Remove_Context (Library_Unit (Curr_Comp));
2980 New_Scope (Standard_Standard);
2981 Scope_Stack.Table (Scope_Stack.Last).Is_Active_Stack_Base := True;
2982 Instantiate_Package_Body
2983 ((N, Act_Decl, Expander_Active, Current_Sem_Unit), True);
2988 Set_Is_Immediately_Visible (Gen_Comp, Vis);
2990 -- Reset Generic_Instance flag so that use clauses can be installed
2991 -- in the proper order. (See Use_One_Package for effect of enclosing
2992 -- instances on processing of use clauses).
2994 for J in 1 .. N_Instances loop
2995 Set_Is_Generic_Instance (Instances (J), False);
2999 Install_Context (Curr_Comp);
3001 if Present (Curr_Scope)
3002 and then Is_Child_Unit (Curr_Scope)
3004 New_Scope (Curr_Scope);
3005 Set_Is_Immediately_Visible (Curr_Scope);
3007 -- Finally, restore inner scopes as well.
3009 for J in reverse 1 .. Num_Inner loop
3010 New_Scope (Inner_Scopes (J));
3014 Restore_Scope_Stack (Handle_Use => False);
3017 -- Restore use clauses. For a child unit, use clauses in the
3018 -- parents are restored when installing the context, so only
3019 -- those in inner scopes (and those local to the child unit itself)
3020 -- need to be installed explicitly.
3022 if Is_Child_Unit (Curr_Unit)
3025 for J in reverse 1 .. Num_Inner + 1 loop
3026 Scope_Stack.Table (Scope_Stack.Last - J + 1).First_Use_Clause :=
3028 Install_Use_Clauses (Use_Clauses (J));
3032 for J in reverse 1 .. Num_Scopes loop
3033 Scope_Stack.Table (Scope_Stack.Last - J + 1).First_Use_Clause :=
3035 Install_Use_Clauses (Use_Clauses (J));
3039 for J in 1 .. N_Instances loop
3040 Set_Is_Generic_Instance (Instances (J), True);
3043 -- If generic unit is in current unit, current context is correct.
3046 Instantiate_Package_Body
3047 ((N, Act_Decl, Expander_Active, Current_Sem_Unit), True);
3049 end Inline_Instance_Body;
3051 -------------------------------------
3052 -- Analyze_Procedure_Instantiation --
3053 -------------------------------------
3055 procedure Analyze_Procedure_Instantiation (N : Node_Id) is
3057 Analyze_Subprogram_Instantiation (N, E_Procedure);
3058 end Analyze_Procedure_Instantiation;
3060 --------------------------------------
3061 -- Analyze_Subprogram_Instantiation --
3062 --------------------------------------
3064 procedure Analyze_Subprogram_Instantiation
3068 Loc : constant Source_Ptr := Sloc (N);
3069 Gen_Id : constant Node_Id := Name (N);
3071 Anon_Id : constant Entity_Id :=
3072 Make_Defining_Identifier (Sloc (Defining_Entity (N)),
3073 Chars => New_External_Name
3074 (Chars (Defining_Entity (N)), 'R'));
3076 Act_Decl_Id : Entity_Id;
3081 Gen_Unit : Entity_Id;
3083 Pack_Id : Entity_Id;
3084 Parent_Installed : Boolean := False;
3085 Renaming_List : List_Id;
3087 procedure Analyze_Instance_And_Renamings;
3088 -- The instance must be analyzed in a context that includes the
3089 -- mappings of generic parameters into actuals. We create a package
3090 -- declaration for this purpose, and a subprogram with an internal
3091 -- name within the package. The subprogram instance is simply an
3092 -- alias for the internal subprogram, declared in the current scope.
3094 ------------------------------------
3095 -- Analyze_Instance_And_Renamings --
3096 ------------------------------------
3098 procedure Analyze_Instance_And_Renamings is
3099 Def_Ent : constant Entity_Id := Defining_Entity (N);
3100 Pack_Decl : Node_Id;
3103 if Nkind (Parent (N)) = N_Compilation_Unit then
3105 -- For the case of a compilation unit, the container package
3106 -- has the same name as the instantiation, to insure that the
3107 -- binder calls the elaboration procedure with the right name.
3108 -- Copy the entity of the instance, which may have compilation
3109 -- level flags (e.g. Is_Child_Unit) set.
3111 Pack_Id := New_Copy (Def_Ent);
3114 -- Otherwise we use the name of the instantiation concatenated
3115 -- with its source position to ensure uniqueness if there are
3116 -- several instantiations with the same name.
3119 Make_Defining_Identifier (Loc,
3120 Chars => New_External_Name
3121 (Related_Id => Chars (Def_Ent),
3123 Suffix_Index => Source_Offset (Sloc (Def_Ent))));
3126 Pack_Decl := Make_Package_Declaration (Loc,
3127 Specification => Make_Package_Specification (Loc,
3128 Defining_Unit_Name => Pack_Id,
3129 Visible_Declarations => Renaming_List,
3130 End_Label => Empty));
3132 Set_Instance_Spec (N, Pack_Decl);
3133 Set_Is_Generic_Instance (Pack_Id);
3134 Set_Needs_Debug_Info (Pack_Id);
3136 -- Case of not a compilation unit
3138 if Nkind (Parent (N)) /= N_Compilation_Unit then
3139 Mark_Rewrite_Insertion (Pack_Decl);
3140 Insert_Before (N, Pack_Decl);
3141 Set_Has_Completion (Pack_Id);
3143 -- Case of an instantiation that is a compilation unit
3145 -- Place declaration on current node so context is complete
3146 -- for analysis (including nested instantiations), and for
3147 -- use in a context_clause (see Analyze_With_Clause).
3150 Set_Unit (Parent (N), Pack_Decl);
3151 Set_Parent_Spec (Pack_Decl, Parent_Spec (N));
3154 Analyze (Pack_Decl);
3155 Check_Formal_Packages (Pack_Id);
3156 Set_Is_Generic_Instance (Pack_Id, False);
3158 -- Body of the enclosing package is supplied when instantiating
3159 -- the subprogram body, after semantic analysis is completed.
3161 if Nkind (Parent (N)) = N_Compilation_Unit then
3163 -- Remove package itself from visibility, so it does not
3164 -- conflict with subprogram.
3166 Set_Name_Entity_Id (Chars (Pack_Id), Homonym (Pack_Id));
3168 -- Set name and scope of internal subprogram so that the
3169 -- proper external name will be generated. The proper scope
3170 -- is the scope of the wrapper package. We need to generate
3171 -- debugging information for the internal subprogram, so set
3172 -- flag accordingly.
3174 Set_Chars (Anon_Id, Chars (Defining_Entity (N)));
3175 Set_Scope (Anon_Id, Scope (Pack_Id));
3177 -- Mark wrapper package as referenced, to avoid spurious
3178 -- warnings if the instantiation appears in various with_
3179 -- clauses of subunits of the main unit.
3181 Set_Referenced (Pack_Id);
3184 Set_Is_Generic_Instance (Anon_Id);
3185 Set_Needs_Debug_Info (Anon_Id);
3186 Act_Decl_Id := New_Copy (Anon_Id);
3188 Set_Parent (Act_Decl_Id, Parent (Anon_Id));
3189 Set_Chars (Act_Decl_Id, Chars (Defining_Entity (N)));
3190 Set_Sloc (Act_Decl_Id, Sloc (Defining_Entity (N)));
3191 Set_Comes_From_Source (Act_Decl_Id, True);
3193 -- The signature may involve types that are not frozen yet, but
3194 -- the subprogram will be frozen at the point the wrapper package
3195 -- is frozen, so it does not need its own freeze node. In fact, if
3196 -- one is created, it might conflict with the freezing actions from
3197 -- the wrapper package (see 7206-013).
3199 Set_Has_Delayed_Freeze (Anon_Id, False);
3201 -- If the instance is a child unit, mark the Id accordingly. Mark
3202 -- the anonymous entity as well, which is the real subprogram and
3203 -- which is used when the instance appears in a context clause.
3205 Set_Is_Child_Unit (Act_Decl_Id, Is_Child_Unit (Defining_Entity (N)));
3206 Set_Is_Child_Unit (Anon_Id, Is_Child_Unit (Defining_Entity (N)));
3207 New_Overloaded_Entity (Act_Decl_Id);
3208 Check_Eliminated (Act_Decl_Id);
3210 -- In compilation unit case, kill elaboration checks on the
3211 -- instantiation, since they are never needed -- the body is
3212 -- instantiated at the same point as the spec.
3214 if Nkind (Parent (N)) = N_Compilation_Unit then
3215 Set_Suppress_Elaboration_Warnings (Act_Decl_Id);
3216 Set_Kill_Elaboration_Checks (Act_Decl_Id);
3217 Set_Is_Compilation_Unit (Anon_Id);
3219 Set_Cunit_Entity (Current_Sem_Unit, Pack_Id);
3222 -- The instance is not a freezing point for the new subprogram.
3224 Set_Is_Frozen (Act_Decl_Id, False);
3226 if Nkind (Defining_Entity (N)) = N_Defining_Operator_Symbol then
3227 Valid_Operator_Definition (Act_Decl_Id);
3230 Set_Alias (Act_Decl_Id, Anon_Id);
3231 Set_Parent (Act_Decl_Id, Parent (Anon_Id));
3232 Set_Has_Completion (Act_Decl_Id);
3233 Set_Related_Instance (Pack_Id, Act_Decl_Id);
3235 if Nkind (Parent (N)) = N_Compilation_Unit then
3236 Set_Body_Required (Parent (N), False);
3239 end Analyze_Instance_And_Renamings;
3241 -- Start of processing for Analyze_Subprogram_Instantiation
3244 -- Very first thing: apply the special kludge for Text_IO processing
3245 -- in case we are instantiating one of the children of [Wide_]Text_IO.
3246 -- Of course such an instantiation is bogus (these are packages, not
3247 -- subprograms), but we get a better error message if we do this.
3249 Text_IO_Kludge (Gen_Id);
3251 -- Make node global for error reporting.
3253 Instantiation_Node := N;
3254 Pre_Analyze_Actuals (N);
3257 Check_Generic_Child_Unit (Gen_Id, Parent_Installed);
3258 Gen_Unit := Entity (Gen_Id);
3260 Generate_Reference (Gen_Unit, Gen_Id);
3262 if Nkind (Gen_Id) = N_Identifier
3263 and then Chars (Gen_Unit) = Chars (Defining_Entity (N))
3266 ("& is hidden within declaration of instance", Gen_Id, Gen_Unit);
3269 if Etype (Gen_Unit) = Any_Type then
3274 -- Verify that it is a generic subprogram of the right kind, and that
3275 -- it does not lead to a circular instantiation.
3277 if Ekind (Gen_Unit) /= E_Generic_Procedure
3278 and then Ekind (Gen_Unit) /= E_Generic_Function
3280 Error_Msg_N ("expect generic subprogram in instantiation", Gen_Id);
3282 elsif In_Open_Scopes (Gen_Unit) then
3283 Error_Msg_NE ("instantiation of & within itself", N, Gen_Unit);
3285 elsif K = E_Procedure
3286 and then Ekind (Gen_Unit) /= E_Generic_Procedure
3288 if Ekind (Gen_Unit) = E_Generic_Function then
3290 ("cannot instantiate generic function as procedure", Gen_Id);
3293 ("expect name of generic procedure in instantiation", Gen_Id);
3296 elsif K = E_Function
3297 and then Ekind (Gen_Unit) /= E_Generic_Function
3299 if Ekind (Gen_Unit) = E_Generic_Procedure then
3301 ("cannot instantiate generic procedure as function", Gen_Id);
3304 ("expect name of generic function in instantiation", Gen_Id);
3308 Set_Entity (Gen_Id, Gen_Unit);
3309 Set_Is_Instantiated (Gen_Unit);
3311 if In_Extended_Main_Source_Unit (N) then
3312 Generate_Reference (Gen_Unit, N);
3315 -- If renaming, get original unit
3317 if Present (Renamed_Object (Gen_Unit))
3318 and then (Ekind (Renamed_Object (Gen_Unit)) = E_Generic_Procedure
3320 Ekind (Renamed_Object (Gen_Unit)) = E_Generic_Function)
3322 Gen_Unit := Renamed_Object (Gen_Unit);
3323 Set_Is_Instantiated (Gen_Unit);
3324 Generate_Reference (Gen_Unit, N);
3327 if Contains_Instance_Of (Gen_Unit, Current_Scope, Gen_Id) then
3328 Error_Msg_Node_2 := Current_Scope;
3330 ("circular Instantiation: & instantiated in &!", N, Gen_Unit);
3331 Circularity_Detected := True;
3335 Gen_Decl := Unit_Declaration_Node (Gen_Unit);
3337 -- The subprogram itself cannot contain a nested instance, so
3338 -- the current parent is left empty.
3340 Set_Instance_Env (Gen_Unit, Empty);
3342 -- Initialize renamings map, for error checking.
3344 Generic_Renamings.Set_Last (0);
3345 Generic_Renamings_HTable.Reset;
3347 Create_Instantiation_Source (N, Gen_Unit, False, S_Adjustment);
3349 -- Copy original generic tree, to produce text for instantiation.
3353 (Original_Node (Gen_Decl), Empty, Instantiating => True);
3355 Act_Spec := Specification (Act_Tree);
3357 Analyze_Associations
3359 Generic_Formal_Declarations (Act_Tree),
3360 Generic_Formal_Declarations (Gen_Decl));
3362 -- Build the subprogram declaration, which does not appear
3363 -- in the generic template, and give it a sloc consistent
3364 -- with that of the template.
3366 Set_Defining_Unit_Name (Act_Spec, Anon_Id);
3367 Set_Generic_Parent (Act_Spec, Gen_Unit);
3369 Make_Subprogram_Declaration (Sloc (Act_Spec),
3370 Specification => Act_Spec);
3372 Set_Categorization_From_Pragmas (Act_Decl);
3374 if Parent_Installed then
3378 Append (Act_Decl, Renaming_List);
3379 Analyze_Instance_And_Renamings;
3381 -- If the generic is marked Import (Intrinsic), then so is the
3382 -- instance. This indicates that there is no body to instantiate.
3383 -- If generic is marked inline, so it the instance, and the
3384 -- anonymous subprogram it renames. If inlined, or else if inlining
3385 -- is enabled for the compilation, we generate the instance body
3386 -- even if it is not within the main unit.
3388 -- Any other pragmas might also be inherited ???
3390 if Is_Intrinsic_Subprogram (Gen_Unit) then
3391 Set_Is_Intrinsic_Subprogram (Anon_Id);
3392 Set_Is_Intrinsic_Subprogram (Act_Decl_Id);
3394 if Chars (Gen_Unit) = Name_Unchecked_Conversion then
3395 Validate_Unchecked_Conversion (N, Act_Decl_Id);
3399 Generate_Definition (Act_Decl_Id);
3401 Set_Is_Inlined (Act_Decl_Id, Is_Inlined (Gen_Unit));
3402 Set_Is_Inlined (Anon_Id, Is_Inlined (Gen_Unit));
3404 if not Is_Intrinsic_Subprogram (Gen_Unit) then
3405 Check_Elab_Instantiation (N);
3408 Check_Hidden_Child_Unit (N, Gen_Unit, Act_Decl_Id);
3410 -- Subject to change, pending on if other pragmas are inherited ???
3412 Validate_Categorization_Dependency (N, Act_Decl_Id);
3414 if not Is_Intrinsic_Subprogram (Act_Decl_Id) then
3416 if not Generic_Separately_Compiled (Gen_Unit) then
3417 Inherit_Context (Gen_Decl, N);
3420 Restore_Private_Views (Pack_Id, False);
3422 -- If the context requires a full instantiation, mark node for
3423 -- subsequent construction of the body.
3425 if (Is_In_Main_Unit (N)
3426 or else Is_Inlined (Act_Decl_Id))
3427 and then (Operating_Mode = Generate_Code
3428 or else (Operating_Mode = Check_Semantics
3429 and then ASIS_Mode))
3430 and then (Expander_Active or else ASIS_Mode)
3431 and then not ABE_Is_Certain (N)
3432 and then not Is_Eliminated (Act_Decl_Id)
3434 Pending_Instantiations.Increment_Last;
3435 Pending_Instantiations.Table (Pending_Instantiations.Last) :=
3436 (N, Act_Decl, Expander_Active, Current_Sem_Unit);
3437 Check_Forward_Instantiation (Gen_Decl);
3439 -- The wrapper package is always delayed, because it does
3440 -- not constitute a freeze point, but to insure that the
3441 -- freeze node is placed properly, it is created directly
3442 -- when instantiating the body (otherwise the freeze node
3443 -- might appear to early for nested instantiations).
3445 elsif Nkind (Parent (N)) = N_Compilation_Unit then
3447 -- For ASIS purposes, indicate that the wrapper package has
3448 -- replaced the instantiation node.
3450 Rewrite (N, Unit (Parent (N)));
3451 Set_Unit (Parent (N), N);
3454 elsif Nkind (Parent (N)) = N_Compilation_Unit then
3456 -- Replace instance node for library-level instantiations
3457 -- of intrinsic subprograms, for ASIS use.
3459 Rewrite (N, Unit (Parent (N)));
3460 Set_Unit (Parent (N), N);
3463 if Parent_Installed then
3468 Generic_Renamings.Set_Last (0);
3469 Generic_Renamings_HTable.Reset;
3473 when Instantiation_Error =>
3474 if Parent_Installed then
3477 end Analyze_Subprogram_Instantiation;
3479 -------------------------
3480 -- Get_Associated_Node --
3481 -------------------------
3483 function Get_Associated_Node (N : Node_Id) return Node_Id is
3484 Assoc : Node_Id := Associated_Node (N);
3487 if Nkind (Assoc) /= Nkind (N) then
3490 elsif Nkind (Assoc) = N_Aggregate
3491 or else Nkind (Assoc) = N_Extension_Aggregate
3495 -- If the node is part of an inner generic, it may itself have been
3496 -- remapped into a further generic copy. Associated_Node is otherwise
3497 -- used for the entity of the node, and will be of a different node
3498 -- kind, or else N has been rewritten as a literal or function call.
3500 while Present (Associated_Node (Assoc))
3501 and then Nkind (Associated_Node (Assoc)) = Nkind (Assoc)
3503 Assoc := Associated_Node (Assoc);
3506 -- Follow and additional link in case the final node was rewritten.
3507 -- This can only happen with nested generic units.
3509 if (Nkind (Assoc) = N_Identifier or else Nkind (Assoc) in N_Op)
3510 and then Present (Associated_Node (Assoc))
3511 and then (Nkind (Associated_Node (Assoc)) = N_Function_Call
3513 Nkind (Associated_Node (Assoc)) = N_Explicit_Dereference
3515 Nkind (Associated_Node (Assoc)) = N_Integer_Literal
3517 Nkind (Associated_Node (Assoc)) = N_Real_Literal
3519 Nkind (Associated_Node (Assoc)) = N_String_Literal)
3521 Assoc := Associated_Node (Assoc);
3526 end Get_Associated_Node;
3528 -------------------------------------------
3529 -- Build_Instance_Compilation_Unit_Nodes --
3530 -------------------------------------------
3532 procedure Build_Instance_Compilation_Unit_Nodes
3537 Decl_Cunit : Node_Id;
3538 Body_Cunit : Node_Id;
3540 New_Main : constant Entity_Id := Defining_Entity (Act_Decl);
3541 Old_Main : constant Entity_Id := Cunit_Entity (Main_Unit);
3544 -- A new compilation unit node is built for the instance declaration
3547 Make_Compilation_Unit (Sloc (N),
3548 Context_Items => Empty_List,
3551 Make_Compilation_Unit_Aux (Sloc (N)));
3553 Set_Parent_Spec (Act_Decl, Parent_Spec (N));
3554 Set_Body_Required (Decl_Cunit, True);
3556 -- We use the original instantiation compilation unit as the resulting
3557 -- compilation unit of the instance, since this is the main unit.
3559 Rewrite (N, Act_Body);
3560 Body_Cunit := Parent (N);
3562 -- The two compilation unit nodes are linked by the Library_Unit field
3564 Set_Library_Unit (Decl_Cunit, Body_Cunit);
3565 Set_Library_Unit (Body_Cunit, Decl_Cunit);
3567 -- Preserve the private nature of the package if needed.
3569 Set_Private_Present (Decl_Cunit, Private_Present (Body_Cunit));
3571 -- If the instance is not the main unit, its context, categorization,
3572 -- and elaboration entity are not relevant to the compilation.
3574 if Parent (N) /= Cunit (Main_Unit) then
3578 -- The context clause items on the instantiation, which are now
3579 -- attached to the body compilation unit (since the body overwrote
3580 -- the original instantiation node), semantically belong on the spec,
3581 -- so copy them there. It's harmless to leave them on the body as well.
3582 -- In fact one could argue that they belong in both places.
3584 Citem := First (Context_Items (Body_Cunit));
3585 while Present (Citem) loop
3586 Append (New_Copy (Citem), Context_Items (Decl_Cunit));
3590 -- Propagate categorization flags on packages, so that they appear
3591 -- in ali file for the spec of the unit.
3593 if Ekind (New_Main) = E_Package then
3594 Set_Is_Pure (Old_Main, Is_Pure (New_Main));
3595 Set_Is_Preelaborated (Old_Main, Is_Preelaborated (New_Main));
3596 Set_Is_Remote_Types (Old_Main, Is_Remote_Types (New_Main));
3597 Set_Is_Shared_Passive (Old_Main, Is_Shared_Passive (New_Main));
3598 Set_Is_Remote_Call_Interface
3599 (Old_Main, Is_Remote_Call_Interface (New_Main));
3602 -- Make entry in Units table, so that binder can generate call to
3603 -- elaboration procedure for body, if any.
3605 Make_Instance_Unit (Body_Cunit);
3606 Main_Unit_Entity := New_Main;
3607 Set_Cunit_Entity (Main_Unit, Main_Unit_Entity);
3609 -- Build elaboration entity, since the instance may certainly
3610 -- generate elaboration code requiring a flag for protection.
3612 Build_Elaboration_Entity (Decl_Cunit, New_Main);
3613 end Build_Instance_Compilation_Unit_Nodes;
3615 -----------------------------------
3616 -- Check_Formal_Package_Instance --
3617 -----------------------------------
3619 -- If the formal has specific parameters, they must match those of the
3620 -- actual. Both of them are instances, and the renaming declarations
3621 -- for their formal parameters appear in the same order in both. The
3622 -- analyzed formal has been analyzed in the context of the current
3625 procedure Check_Formal_Package_Instance
3626 (Formal_Pack : Entity_Id;
3627 Actual_Pack : Entity_Id)
3629 E1 : Entity_Id := First_Entity (Actual_Pack);
3630 E2 : Entity_Id := First_Entity (Formal_Pack);
3635 procedure Check_Mismatch (B : Boolean);
3636 -- Common error routine for mismatch between the parameters of
3637 -- the actual instance and those of the formal package.
3639 procedure Check_Mismatch (B : Boolean) is
3643 ("actual for & in actual instance does not match formal",
3644 Parent (Actual_Pack), E1);
3648 -- Start of processing for Check_Formal_Package_Instance
3652 and then Present (E2)
3654 exit when Ekind (E1) = E_Package
3655 and then Renamed_Entity (E1) = Renamed_Entity (Actual_Pack);
3657 if Is_Type (E1) then
3659 -- Subtypes must statically match. E1 and E2 are the
3660 -- local entities that are subtypes of the actuals.
3661 -- Itypes generated for other parameters need not be checked,
3662 -- the check will be performed on the parameters themselves.
3664 if not Is_Itype (E1)
3665 and then not Is_Itype (E2)
3669 or else Etype (E1) /= Etype (E2)
3670 or else not Subtypes_Statically_Match (E1, E2));
3673 elsif Ekind (E1) = E_Constant then
3675 -- IN parameters must denote the same static value, or
3676 -- the same constant, or the literal null.
3678 Expr1 := Expression (Parent (E1));
3680 if Ekind (E2) /= E_Constant then
3681 Check_Mismatch (True);
3684 Expr2 := Expression (Parent (E2));
3687 if Is_Static_Expression (Expr1) then
3689 if not Is_Static_Expression (Expr2) then
3690 Check_Mismatch (True);
3692 elsif Is_Integer_Type (Etype (E1)) then
3695 V1 : constant Uint := Expr_Value (Expr1);
3696 V2 : constant Uint := Expr_Value (Expr2);
3698 Check_Mismatch (V1 /= V2);
3701 elsif Is_Real_Type (Etype (E1)) then
3703 V1 : constant Ureal := Expr_Value_R (Expr1);
3704 V2 : constant Ureal := Expr_Value_R (Expr2);
3706 Check_Mismatch (V1 /= V2);
3709 elsif Is_String_Type (Etype (E1))
3710 and then Nkind (Expr1) = N_String_Literal
3713 if Nkind (Expr2) /= N_String_Literal then
3714 Check_Mismatch (True);
3717 (not String_Equal (Strval (Expr1), Strval (Expr2)));
3721 elsif Is_Entity_Name (Expr1) then
3722 if Is_Entity_Name (Expr2) then
3723 if Entity (Expr1) = Entity (Expr2) then
3726 elsif Ekind (Entity (Expr2)) = E_Constant
3727 and then Is_Entity_Name (Constant_Value (Entity (Expr2)))
3729 Entity (Constant_Value (Entity (Expr2))) = Entity (Expr1)
3733 Check_Mismatch (True);
3736 Check_Mismatch (True);
3739 elsif Nkind (Expr1) = N_Null then
3740 Check_Mismatch (Nkind (Expr1) /= N_Null);
3743 Check_Mismatch (True);
3746 elsif Ekind (E1) = E_Variable
3747 or else Ekind (E1) = E_Package
3750 (Ekind (E1) /= Ekind (E2)
3751 or else Renamed_Object (E1) /= Renamed_Object (E2));
3753 elsif Is_Overloadable (E1) then
3755 -- Verify that the names of the entities match.
3756 -- What if actual is an attribute ???
3759 (Ekind (E2) /= Ekind (E1) or else (Alias (E1)) /= Alias (E2));
3762 raise Program_Error;
3769 end Check_Formal_Package_Instance;
3771 ---------------------------
3772 -- Check_Formal_Packages --
3773 ---------------------------
3775 procedure Check_Formal_Packages (P_Id : Entity_Id) is
3777 Formal_P : Entity_Id;
3780 -- Iterate through the declarations in the instance, looking for
3781 -- package renaming declarations that denote instances of formal
3782 -- packages. Stop when we find the renaming of the current package
3783 -- itself. The declaration for a formal package without a box is
3784 -- followed by an internal entity that repeats the instantiation.
3786 E := First_Entity (P_Id);
3787 while Present (E) loop
3788 if Ekind (E) = E_Package then
3789 if Renamed_Object (E) = P_Id then
3792 elsif Nkind (Parent (E)) /= N_Package_Renaming_Declaration then
3795 elsif not Box_Present (Parent (Associated_Formal_Package (E))) then
3796 Formal_P := Next_Entity (E);
3797 Check_Formal_Package_Instance (Formal_P, E);
3803 end Check_Formal_Packages;
3805 ---------------------------------
3806 -- Check_Forward_Instantiation --
3807 ---------------------------------
3809 procedure Check_Forward_Instantiation (Decl : Node_Id) is
3811 Gen_Comp : Entity_Id := Cunit_Entity (Get_Source_Unit (Decl));
3814 -- The instantiation appears before the generic body if we are in the
3815 -- scope of the unit containing the generic, either in its spec or in
3816 -- the package body. and before the generic body.
3818 if Ekind (Gen_Comp) = E_Package_Body then
3819 Gen_Comp := Spec_Entity (Gen_Comp);
3822 if In_Open_Scopes (Gen_Comp)
3823 and then No (Corresponding_Body (Decl))
3828 and then not Is_Compilation_Unit (S)
3829 and then not Is_Child_Unit (S)
3831 if Ekind (S) = E_Package then
3832 Set_Has_Forward_Instantiation (S);
3838 end Check_Forward_Instantiation;
3840 ---------------------------
3841 -- Check_Generic_Actuals --
3842 ---------------------------
3844 -- The visibility of the actuals may be different between the
3845 -- point of generic instantiation and the instantiation of the body.
3847 procedure Check_Generic_Actuals
3848 (Instance : Entity_Id;
3849 Is_Formal_Box : Boolean)
3855 E := First_Entity (Instance);
3856 while Present (E) loop
3858 and then Nkind (Parent (E)) = N_Subtype_Declaration
3859 and then Scope (Etype (E)) /= Instance
3860 and then Is_Entity_Name (Subtype_Indication (Parent (E)))
3862 Check_Private_View (Subtype_Indication (Parent (E)));
3863 Set_Is_Generic_Actual_Type (E, True);
3864 Set_Is_Hidden (E, False);
3866 -- We constructed the generic actual type as a subtype of
3867 -- the supplied type. This means that it normally would not
3868 -- inherit subtype specific attributes of the actual, which
3869 -- is wrong for the generic case.
3871 Astype := Ancestor_Subtype (E);
3875 -- can happen when E is an itype that is the full view of
3876 -- a private type completed, e.g. with a constrained array.
3878 Astype := Base_Type (E);
3881 Set_Size_Info (E, (Astype));
3882 Set_RM_Size (E, RM_Size (Astype));
3883 Set_First_Rep_Item (E, First_Rep_Item (Astype));
3885 if Is_Discrete_Or_Fixed_Point_Type (E) then
3886 Set_RM_Size (E, RM_Size (Astype));
3888 -- In nested instances, the base type of an access actual
3889 -- may itself be private, and need to be exchanged.
3891 elsif Is_Access_Type (E)
3892 and then Is_Private_Type (Etype (E))
3895 (New_Occurrence_Of (Etype (E), Sloc (Instance)));
3898 elsif Ekind (E) = E_Package then
3900 -- If this is the renaming for the current instance, we're done.
3901 -- Otherwise it is a formal package. If the corresponding formal
3902 -- was declared with a box, the (instantiations of the) generic
3903 -- formal part are also visible. Otherwise, ignore the entity
3904 -- created to validate the actuals.
3906 if Renamed_Object (E) = Instance then
3909 elsif Nkind (Parent (E)) /= N_Package_Renaming_Declaration then
3912 -- The visibility of a formal of an enclosing generic is already
3915 elsif Denotes_Formal_Package (E) then
3918 elsif Present (Associated_Formal_Package (E))
3919 and then Box_Present (Parent (Associated_Formal_Package (E)))
3921 Check_Generic_Actuals (Renamed_Object (E), True);
3922 Set_Is_Hidden (E, False);
3925 -- If this is a subprogram instance (in a wrapper package) the
3926 -- actual is fully visible.
3928 elsif Is_Wrapper_Package (Instance) then
3929 Set_Is_Hidden (E, False);
3932 Set_Is_Hidden (E, not Is_Formal_Box);
3937 end Check_Generic_Actuals;
3939 ------------------------------
3940 -- Check_Generic_Child_Unit --
3941 ------------------------------
3943 procedure Check_Generic_Child_Unit
3945 Parent_Installed : in out Boolean)
3947 Loc : constant Source_Ptr := Sloc (Gen_Id);
3948 Gen_Par : Entity_Id := Empty;
3949 Inst_Par : Entity_Id;
3953 function Find_Generic_Child
3957 -- Search generic parent for possible child unit with the given name.
3959 function In_Enclosing_Instance return Boolean;
3960 -- Within an instance of the parent, the child unit may be denoted
3961 -- by a simple name, or an abbreviated expanded name. Examine enclosing
3962 -- scopes to locate a possible parent instantiation.
3964 ------------------------
3965 -- Find_Generic_Child --
3966 ------------------------
3968 function Find_Generic_Child
3976 -- If entity of name is already set, instance has already been
3977 -- resolved, e.g. in an enclosing instantiation.
3979 if Present (Entity (Id)) then
3980 if Scope (Entity (Id)) = Scop then
3987 E := First_Entity (Scop);
3988 while Present (E) loop
3989 if Chars (E) = Chars (Id)
3990 and then Is_Child_Unit (E)
3992 if Is_Child_Unit (E)
3993 and then not Is_Visible_Child_Unit (E)
3996 ("generic child unit& is not visible", Gen_Id, E);
4008 end Find_Generic_Child;
4010 ---------------------------
4011 -- In_Enclosing_Instance --
4012 ---------------------------
4014 function In_Enclosing_Instance return Boolean is
4015 Enclosing_Instance : Node_Id;
4016 Instance_Decl : Node_Id;
4019 Enclosing_Instance := Current_Scope;
4021 while Present (Enclosing_Instance) loop
4022 Instance_Decl := Unit_Declaration_Node (Enclosing_Instance);
4024 if Ekind (Enclosing_Instance) = E_Package
4025 and then Is_Generic_Instance (Enclosing_Instance)
4027 (Generic_Parent (Specification (Instance_Decl)))
4029 -- Check whether the generic we are looking for is a child
4030 -- of this instance.
4032 E := Find_Generic_Child
4033 (Generic_Parent (Specification (Instance_Decl)), Gen_Id);
4034 exit when Present (E);
4040 Enclosing_Instance := Scope (Enclosing_Instance);
4052 Make_Expanded_Name (Loc,
4054 Prefix => New_Occurrence_Of (Enclosing_Instance, Loc),
4055 Selector_Name => New_Occurrence_Of (E, Loc)));
4057 Set_Entity (Gen_Id, E);
4058 Set_Etype (Gen_Id, Etype (E));
4059 Parent_Installed := False; -- Already in scope.
4062 end In_Enclosing_Instance;
4064 -- Start of processing for Check_Generic_Child_Unit
4067 -- If the name of the generic is given by a selected component, it
4068 -- may be the name of a generic child unit, and the prefix is the name
4069 -- of an instance of the parent, in which case the child unit must be
4070 -- visible. If this instance is not in scope, it must be placed there
4071 -- and removed after instantiation, because what is being instantiated
4072 -- is not the original child, but the corresponding child present in
4073 -- the instance of the parent.
4075 -- If the child is instantiated within the parent, it can be given by
4076 -- a simple name. In this case the instance is already in scope, but
4077 -- the child generic must be recovered from the generic parent as well.
4079 if Nkind (Gen_Id) = N_Selected_Component then
4080 S := Selector_Name (Gen_Id);
4081 Analyze (Prefix (Gen_Id));
4082 Inst_Par := Entity (Prefix (Gen_Id));
4084 if Ekind (Inst_Par) = E_Package
4085 and then Present (Renamed_Object (Inst_Par))
4087 Inst_Par := Renamed_Object (Inst_Par);
4090 if Ekind (Inst_Par) = E_Package then
4091 if Nkind (Parent (Inst_Par)) = N_Package_Specification then
4092 Gen_Par := Generic_Parent (Parent (Inst_Par));
4094 elsif Nkind (Parent (Inst_Par)) = N_Defining_Program_Unit_Name
4096 Nkind (Parent (Parent (Inst_Par))) = N_Package_Specification
4098 Gen_Par := Generic_Parent (Parent (Parent (Inst_Par)));
4101 elsif Ekind (Inst_Par) = E_Generic_Package
4102 and then Nkind (Parent (Gen_Id)) = N_Formal_Package_Declaration
4104 -- A formal package may be a real child package, and not the
4105 -- implicit instance within a parent. In this case the child is
4106 -- not visible and has to be retrieved explicitly as well.
4108 Gen_Par := Inst_Par;
4111 if Present (Gen_Par) then
4113 -- The prefix denotes an instantiation. The entity itself
4114 -- may be a nested generic, or a child unit.
4116 E := Find_Generic_Child (Gen_Par, S);
4119 Change_Selected_Component_To_Expanded_Name (Gen_Id);
4120 Set_Entity (Gen_Id, E);
4121 Set_Etype (Gen_Id, Etype (E));
4123 Set_Etype (S, Etype (E));
4125 -- Indicate that this is a reference to the parent.
4127 if In_Extended_Main_Source_Unit (Gen_Id) then
4128 Set_Is_Instantiated (Inst_Par);
4131 -- A common mistake is to replicate the naming scheme of
4132 -- a hierarchy by instantiating a generic child directly,
4133 -- rather than the implicit child in a parent instance:
4135 -- generic .. package Gpar is ..
4136 -- generic .. package Gpar.Child is ..
4137 -- package Par is new Gpar ();
4140 -- package Par.Child is new Gpar.Child ();
4141 -- rather than Par.Child
4143 -- In this case the instantiation is within Par, which is
4144 -- an instance, but Gpar does not denote Par because we are
4145 -- not IN the instance of Gpar, so this is illegal. The test
4146 -- below recognizes this particular case.
4148 if Is_Child_Unit (E)
4149 and then not Comes_From_Source (Entity (Prefix (Gen_Id)))
4150 and then (not In_Instance
4151 or else Nkind (Parent (Parent (Gen_Id))) =
4155 ("prefix of generic child unit must be instance of parent",
4159 if not In_Open_Scopes (Inst_Par)
4160 and then Nkind (Parent (Gen_Id)) not in
4161 N_Generic_Renaming_Declaration
4163 Install_Parent (Inst_Par);
4164 Parent_Installed := True;
4168 -- If the generic parent does not contain an entity that
4169 -- corresponds to the selector, the instance doesn't either.
4170 -- Analyzing the node will yield the appropriate error message.
4171 -- If the entity is not a child unit, then it is an inner
4172 -- generic in the parent.
4180 if Is_Child_Unit (Entity (Gen_Id))
4182 Nkind (Parent (Gen_Id)) not in N_Generic_Renaming_Declaration
4183 and then not In_Open_Scopes (Inst_Par)
4185 Install_Parent (Inst_Par);
4186 Parent_Installed := True;
4190 elsif Nkind (Gen_Id) = N_Expanded_Name then
4192 -- Entity already present, analyze prefix, whose meaning may be
4193 -- an instance in the current context. If it is an instance of
4194 -- a relative within another, the proper parent may still have
4195 -- to be installed, if they are not of the same generation.
4197 Analyze (Prefix (Gen_Id));
4198 Inst_Par := Entity (Prefix (Gen_Id));
4200 if In_Enclosing_Instance then
4203 elsif Present (Entity (Gen_Id))
4204 and then Is_Child_Unit (Entity (Gen_Id))
4205 and then not In_Open_Scopes (Inst_Par)
4207 Install_Parent (Inst_Par);
4208 Parent_Installed := True;
4211 elsif In_Enclosing_Instance then
4213 -- The child unit is found in some enclosing scope
4220 -- If this is the renaming of the implicit child in a parent
4221 -- instance, recover the parent name and install it.
4223 if Is_Entity_Name (Gen_Id) then
4224 E := Entity (Gen_Id);
4226 if Is_Generic_Unit (E)
4227 and then Nkind (Parent (E)) in N_Generic_Renaming_Declaration
4228 and then Is_Child_Unit (Renamed_Object (E))
4229 and then Is_Generic_Unit (Scope (Renamed_Object (E)))
4230 and then Nkind (Name (Parent (E))) = N_Expanded_Name
4233 New_Copy_Tree (Name (Parent (E))));
4234 Inst_Par := Entity (Prefix (Gen_Id));
4236 if not In_Open_Scopes (Inst_Par) then
4237 Install_Parent (Inst_Par);
4238 Parent_Installed := True;
4241 -- If it is a child unit of a non-generic parent, it may be
4242 -- use-visible and given by a direct name. Install parent as
4245 elsif Is_Generic_Unit (E)
4246 and then Is_Child_Unit (E)
4248 Nkind (Parent (Gen_Id)) not in N_Generic_Renaming_Declaration
4249 and then not Is_Generic_Unit (Scope (E))
4251 if not In_Open_Scopes (Scope (E)) then
4252 Install_Parent (Scope (E));
4253 Parent_Installed := True;
4258 end Check_Generic_Child_Unit;
4260 -----------------------------
4261 -- Check_Hidden_Child_Unit --
4262 -----------------------------
4264 procedure Check_Hidden_Child_Unit
4266 Gen_Unit : Entity_Id;
4267 Act_Decl_Id : Entity_Id)
4269 Gen_Id : constant Node_Id := Name (N);
4272 if Is_Child_Unit (Gen_Unit)
4273 and then Is_Child_Unit (Act_Decl_Id)
4274 and then Nkind (Gen_Id) = N_Expanded_Name
4275 and then Entity (Prefix (Gen_Id)) = Scope (Act_Decl_Id)
4276 and then Chars (Gen_Unit) = Chars (Act_Decl_Id)
4278 Error_Msg_Node_2 := Scope (Act_Decl_Id);
4280 ("generic unit & is implicitly declared in &",
4281 Defining_Unit_Name (N), Gen_Unit);
4282 Error_Msg_N ("\instance must have different name",
4283 Defining_Unit_Name (N));
4285 end Check_Hidden_Child_Unit;
4287 ------------------------
4288 -- Check_Private_View --
4289 ------------------------
4291 procedure Check_Private_View (N : Node_Id) is
4292 T : constant Entity_Id := Etype (N);
4296 -- Exchange views if the type was not private in the generic but is
4297 -- private at the point of instantiation. Do not exchange views if
4298 -- the scope of the type is in scope. This can happen if both generic
4299 -- and instance are sibling units, or if type is defined in a parent.
4300 -- In this case the visibility of the type will be correct for all
4304 BT := Base_Type (T);
4306 if Is_Private_Type (T)
4307 and then not Has_Private_View (N)
4308 and then Present (Full_View (T))
4309 and then not In_Open_Scopes (Scope (T))
4311 -- In the generic, the full type was visible. Save the
4312 -- private entity, for subsequent exchange.
4316 elsif Has_Private_View (N)
4317 and then not Is_Private_Type (T)
4318 and then not Has_Been_Exchanged (T)
4319 and then Etype (Get_Associated_Node (N)) /= T
4321 -- Only the private declaration was visible in the generic. If
4322 -- the type appears in a subtype declaration, the subtype in the
4323 -- instance must have a view compatible with that of its parent,
4324 -- which must be exchanged (see corresponding code in Restore_
4325 -- Private_Views). Otherwise, if the type is defined in a parent
4326 -- unit, leave full visibility within instance, which is safe.
4328 if In_Open_Scopes (Scope (Base_Type (T)))
4329 and then not Is_Private_Type (Base_Type (T))
4330 and then Comes_From_Source (Base_Type (T))
4334 elsif Nkind (Parent (N)) = N_Subtype_Declaration
4335 or else not In_Private_Part (Scope (Base_Type (T)))
4337 Append_Elmt (T, Exchanged_Views);
4338 Exchange_Declarations (Etype (Get_Associated_Node (N)));
4341 -- For composite types with inconsistent representation
4342 -- exchange component types accordingly.
4344 elsif Is_Access_Type (T)
4345 and then Is_Private_Type (Designated_Type (T))
4346 and then not Has_Private_View (N)
4347 and then Present (Full_View (Designated_Type (T)))
4349 Switch_View (Designated_Type (T));
4351 elsif Is_Array_Type (T)
4352 and then Is_Private_Type (Component_Type (T))
4353 and then not Has_Private_View (N)
4354 and then Present (Full_View (Component_Type (T)))
4356 Switch_View (Component_Type (T));
4358 elsif Is_Private_Type (T)
4359 and then Present (Full_View (T))
4360 and then Is_Array_Type (Full_View (T))
4361 and then Is_Private_Type (Component_Type (Full_View (T)))
4365 -- Finally, a non-private subtype may have a private base type,
4366 -- which must be exchanged for consistency. This can happen when
4367 -- instantiating a package body, when the scope stack is empty
4368 -- but in fact the subtype and the base type are declared in an
4371 elsif not Is_Private_Type (T)
4372 and then not Has_Private_View (N)
4373 and then Is_Private_Type (Base_Type (T))
4374 and then Present (Full_View (BT))
4375 and then not Is_Generic_Type (BT)
4376 and then not In_Open_Scopes (BT)
4378 Append_Elmt (Full_View (BT), Exchanged_Views);
4379 Exchange_Declarations (BT);
4382 end Check_Private_View;
4384 --------------------------
4385 -- Contains_Instance_Of --
4386 --------------------------
4388 function Contains_Instance_Of
4400 -- Verify that there are no circular instantiations. We check whether
4401 -- the unit contains an instance of the current scope or some enclosing
4402 -- scope (in case one of the instances appears in a subunit). Longer
4403 -- circularities involving subunits might seem too pathological to
4404 -- consider, but they were not too pathological for the authors of
4405 -- DEC bc30vsq, so we loop over all enclosing scopes, and mark all
4406 -- enclosing generic scopes as containing an instance.
4409 -- Within a generic subprogram body, the scope is not generic, to
4410 -- allow for recursive subprograms. Use the declaration to determine
4411 -- whether this is a generic unit.
4413 if Ekind (Scop) = E_Generic_Package
4414 or else (Is_Subprogram (Scop)
4415 and then Nkind (Unit_Declaration_Node (Scop)) =
4416 N_Generic_Subprogram_Declaration)
4418 Elmt := First_Elmt (Inner_Instances (Inner));
4420 while Present (Elmt) loop
4421 if Node (Elmt) = Scop then
4422 Error_Msg_Node_2 := Inner;
4424 ("circular Instantiation: & instantiated within &!",
4428 elsif Node (Elmt) = Inner then
4431 elsif Contains_Instance_Of (Node (Elmt), Scop, N) then
4432 Error_Msg_Node_2 := Inner;
4434 ("circular Instantiation: & instantiated within &!",
4442 -- Indicate that Inner is being instantiated within Scop.
4444 Append_Elmt (Inner, Inner_Instances (Scop));
4447 if Scop = Standard_Standard then
4450 Scop := Scope (Scop);
4455 end Contains_Instance_Of;
4457 -----------------------
4458 -- Copy_Generic_Node --
4459 -----------------------
4461 function Copy_Generic_Node
4463 Parent_Id : Node_Id;
4464 Instantiating : Boolean)
4470 function Copy_Generic_Descendant (D : Union_Id) return Union_Id;
4471 -- Check the given value of one of the Fields referenced by the
4472 -- current node to determine whether to copy it recursively. The
4473 -- field may hold a Node_Id, a List_Id, or an Elist_Id, or a plain
4474 -- value (Sloc, Uint, Char) in which case it need not be copied.
4476 procedure Copy_Descendants;
4477 -- Common utility for various nodes.
4479 function Copy_Generic_Elist (E : Elist_Id) return Elist_Id;
4480 -- Make copy of element list.
4482 function Copy_Generic_List
4484 Parent_Id : Node_Id)
4486 -- Apply Copy_Node recursively to the members of a node list.
4488 function In_Defining_Unit_Name (Nam : Node_Id) return Boolean;
4489 -- True if an identifier is part of the defining program unit name
4490 -- of a child unit. The entity of such an identifier must be kept
4491 -- (for ASIS use) even though as the name of an enclosing generic
4492 -- it would otherwise not be preserved in the generic tree.
4494 -----------------------
4495 -- Copy_Descendants --
4496 -----------------------
4498 procedure Copy_Descendants is
4500 use Atree.Unchecked_Access;
4501 -- This code section is part of the implementation of an untyped
4502 -- tree traversal, so it needs direct access to node fields.
4505 Set_Field1 (New_N, Copy_Generic_Descendant (Field1 (N)));
4506 Set_Field2 (New_N, Copy_Generic_Descendant (Field2 (N)));
4507 Set_Field3 (New_N, Copy_Generic_Descendant (Field3 (N)));
4508 Set_Field4 (New_N, Copy_Generic_Descendant (Field4 (N)));
4509 Set_Field5 (New_N, Copy_Generic_Descendant (Field5 (N)));
4510 end Copy_Descendants;
4512 -----------------------------
4513 -- Copy_Generic_Descendant --
4514 -----------------------------
4516 function Copy_Generic_Descendant (D : Union_Id) return Union_Id is
4518 if D = Union_Id (Empty) then
4521 elsif D in Node_Range then
4523 (Copy_Generic_Node (Node_Id (D), New_N, Instantiating));
4525 elsif D in List_Range then
4526 return Union_Id (Copy_Generic_List (List_Id (D), New_N));
4528 elsif D in Elist_Range then
4529 return Union_Id (Copy_Generic_Elist (Elist_Id (D)));
4531 -- Nothing else is copyable (e.g. Uint values), return as is
4536 end Copy_Generic_Descendant;
4538 ------------------------
4539 -- Copy_Generic_Elist --
4540 ------------------------
4542 function Copy_Generic_Elist (E : Elist_Id) return Elist_Id is
4549 M := First_Elmt (E);
4550 while Present (M) loop
4552 (Copy_Generic_Node (Node (M), Empty, Instantiating), L);
4561 end Copy_Generic_Elist;
4563 -----------------------
4564 -- Copy_Generic_List --
4565 -----------------------
4567 function Copy_Generic_List
4569 Parent_Id : Node_Id)
4578 Set_Parent (New_L, Parent_Id);
4581 while Present (N) loop
4582 Append (Copy_Generic_Node (N, Empty, Instantiating), New_L);
4591 end Copy_Generic_List;
4593 ---------------------------
4594 -- In_Defining_Unit_Name --
4595 ---------------------------
4597 function In_Defining_Unit_Name (Nam : Node_Id) return Boolean is
4599 return Present (Parent (Nam))
4600 and then (Nkind (Parent (Nam)) = N_Defining_Program_Unit_Name
4602 (Nkind (Parent (Nam)) = N_Expanded_Name
4603 and then In_Defining_Unit_Name (Parent (Nam))));
4604 end In_Defining_Unit_Name;
4606 -- Start of processing for Copy_Generic_Node
4613 New_N := New_Copy (N);
4615 if Instantiating then
4616 Adjust_Instantiation_Sloc (New_N, S_Adjustment);
4619 if not Is_List_Member (N) then
4620 Set_Parent (New_N, Parent_Id);
4623 -- If defining identifier, then all fields have been copied already
4625 if Nkind (New_N) in N_Entity then
4628 -- Special casing for identifiers and other entity names and operators
4630 elsif Nkind (New_N) = N_Identifier
4631 or else Nkind (New_N) = N_Character_Literal
4632 or else Nkind (New_N) = N_Expanded_Name
4633 or else Nkind (New_N) = N_Operator_Symbol
4634 or else Nkind (New_N) in N_Op
4636 if not Instantiating then
4638 -- Link both nodes in order to assign subsequently the
4639 -- entity of the copy to the original node, in case this
4640 -- is a global reference.
4642 Set_Associated_Node (N, New_N);
4644 -- If we are within an instantiation, this is a nested generic
4645 -- that has already been analyzed at the point of definition. We
4646 -- must preserve references that were global to the enclosing
4647 -- parent at that point. Other occurrences, whether global or
4648 -- local to the current generic, must be resolved anew, so we
4649 -- reset the entity in the generic copy. A global reference has
4650 -- a smaller depth than the parent, or else the same depth in
4651 -- case both are distinct compilation units.
4653 -- It is also possible for Current_Instantiated_Parent to be
4654 -- defined, and for this not to be a nested generic, namely
4655 -- if the unit is loaded through Rtsfind. In that case, the
4656 -- entity of New_N is only a link to the associated node, and
4657 -- not a defining occurrence.
4659 -- The entities for parent units in the defining_program_unit
4660 -- of a generic child unit are established when the context of
4661 -- the unit is first analyzed, before the generic copy is made.
4662 -- They are preserved in the copy for use in ASIS queries.
4664 Ent := Entity (New_N);
4666 if No (Current_Instantiated_Parent.Gen_Id) then
4668 or else Nkind (Ent) /= N_Defining_Identifier
4669 or else not In_Defining_Unit_Name (N)
4671 Set_Associated_Node (New_N, Empty);
4676 not (Nkind (Ent) = N_Defining_Identifier
4678 Nkind (Ent) = N_Defining_Character_Literal
4680 Nkind (Ent) = N_Defining_Operator_Symbol)
4681 or else No (Scope (Ent))
4682 or else Scope (Ent) = Current_Instantiated_Parent.Gen_Id
4683 or else (Scope_Depth (Scope (Ent)) >
4684 Scope_Depth (Current_Instantiated_Parent.Gen_Id)
4686 Get_Source_Unit (Ent) =
4687 Get_Source_Unit (Current_Instantiated_Parent.Gen_Id))
4689 Set_Associated_Node (New_N, Empty);
4692 -- Case of instantiating identifier or some other name or operator
4695 -- If the associated node is still defined, the entity in
4696 -- it is global, and must be copied to the instance.
4697 -- If this copy is being made for a body to inline, it is
4698 -- applied to an instantiated tree, and the entity is already
4699 -- present and must be also preserved.
4702 Assoc : constant Node_Id := Get_Associated_Node (N);
4704 if Present (Assoc) then
4705 if Nkind (Assoc) = Nkind (N) then
4706 Set_Entity (New_N, Entity (Assoc));
4707 Check_Private_View (N);
4709 elsif Nkind (Assoc) = N_Function_Call then
4710 Set_Entity (New_N, Entity (Name (Assoc)));
4712 elsif (Nkind (Assoc) = N_Defining_Identifier
4713 or else Nkind (Assoc) = N_Defining_Character_Literal
4714 or else Nkind (Assoc) = N_Defining_Operator_Symbol)
4715 and then Expander_Active
4717 -- Inlining case: we are copying a tree that contains
4718 -- global entities, which are preserved in the copy
4719 -- to be used for subsequent inlining.
4724 Set_Entity (New_N, Empty);
4730 -- For expanded name, we must copy the Prefix and Selector_Name
4732 if Nkind (N) = N_Expanded_Name then
4734 (New_N, Copy_Generic_Node (Prefix (N), New_N, Instantiating));
4736 Set_Selector_Name (New_N,
4737 Copy_Generic_Node (Selector_Name (N), New_N, Instantiating));
4739 -- For operators, we must copy the right operand
4741 elsif Nkind (N) in N_Op then
4742 Set_Right_Opnd (New_N,
4743 Copy_Generic_Node (Right_Opnd (N), New_N, Instantiating));
4745 -- And for binary operators, the left operand as well
4747 if Nkind (N) in N_Binary_Op then
4748 Set_Left_Opnd (New_N,
4749 Copy_Generic_Node (Left_Opnd (N), New_N, Instantiating));
4753 -- Special casing for stubs
4755 elsif Nkind (N) in N_Body_Stub then
4757 -- In any case, we must copy the specification or defining
4758 -- identifier as appropriate.
4760 if Nkind (N) = N_Subprogram_Body_Stub then
4761 Set_Specification (New_N,
4762 Copy_Generic_Node (Specification (N), New_N, Instantiating));
4765 Set_Defining_Identifier (New_N,
4767 (Defining_Identifier (N), New_N, Instantiating));
4770 -- If we are not instantiating, then this is where we load and
4771 -- analyze subunits, i.e. at the point where the stub occurs. A
4772 -- more permissivle system might defer this analysis to the point
4773 -- of instantiation, but this seems to complicated for now.
4775 if not Instantiating then
4777 Subunit_Name : constant Unit_Name_Type := Get_Unit_Name (N);
4779 Unum : Unit_Number_Type;
4785 (Load_Name => Subunit_Name,
4790 -- If the proper body is not found, a warning message will
4791 -- be emitted when analyzing the stub, or later at the the
4792 -- point of instantiation. Here we just leave the stub as is.
4794 if Unum = No_Unit then
4795 Subunits_Missing := True;
4796 goto Subunit_Not_Found;
4799 Subunit := Cunit (Unum);
4801 if Nkind (Unit (Subunit)) /= N_Subunit then
4802 Error_Msg_Sloc := Sloc (N);
4804 ("expected SEPARATE subunit to complete stub at#,"
4805 & " found child unit", Subunit);
4806 goto Subunit_Not_Found;
4809 -- We must create a generic copy of the subunit, in order
4810 -- to perform semantic analysis on it, and we must replace
4811 -- the stub in the original generic unit with the subunit,
4812 -- in order to preserve non-local references within.
4814 -- Only the proper body needs to be copied. Library_Unit and
4815 -- context clause are simply inherited by the generic copy.
4816 -- Note that the copy (which may be recursive if there are
4817 -- nested subunits) must be done first, before attaching it
4818 -- to the enclosing generic.
4822 (Proper_Body (Unit (Subunit)),
4823 Empty, Instantiating => False);
4825 -- Now place the original proper body in the original
4826 -- generic unit. This is a body, not a compilation unit.
4828 Rewrite (N, Proper_Body (Unit (Subunit)));
4829 Set_Is_Compilation_Unit (Defining_Entity (N), False);
4830 Set_Was_Originally_Stub (N);
4832 -- Finally replace the body of the subunit with its copy,
4833 -- and make this new subunit into the library unit of the
4834 -- generic copy, which does not have stubs any longer.
4836 Set_Proper_Body (Unit (Subunit), New_Body);
4837 Set_Library_Unit (New_N, Subunit);
4838 Inherit_Context (Unit (Subunit), N);
4841 -- If we are instantiating, this must be an error case, since
4842 -- otherwise we would have replaced the stub node by the proper
4843 -- body that corresponds. So just ignore it in the copy (i.e.
4844 -- we have copied it, and that is good enough).
4850 <<Subunit_Not_Found>> null;
4852 -- If the node is a compilation unit, it is the subunit of a stub,
4853 -- which has been loaded already (see code below). In this case,
4854 -- the library unit field of N points to the parent unit (which
4855 -- is a compilation unit) and need not (and cannot!) be copied.
4857 -- When the proper body of the stub is analyzed, thie library_unit
4858 -- link is used to establish the proper context (see sem_ch10).
4860 -- The other fields of a compilation unit are copied as usual
4862 elsif Nkind (N) = N_Compilation_Unit then
4864 -- This code can only be executed when not instantiating, because
4865 -- in the copy made for an instantiation, the compilation unit
4866 -- node has disappeared at the point that a stub is replaced by
4869 pragma Assert (not Instantiating);
4871 Set_Context_Items (New_N,
4872 Copy_Generic_List (Context_Items (N), New_N));
4875 Copy_Generic_Node (Unit (N), New_N, False));
4877 Set_First_Inlined_Subprogram (New_N,
4879 (First_Inlined_Subprogram (N), New_N, False));
4881 Set_Aux_Decls_Node (New_N,
4882 Copy_Generic_Node (Aux_Decls_Node (N), New_N, False));
4884 -- For an assignment node, the assignment is known to be semantically
4885 -- legal if we are instantiating the template. This avoids incorrect
4886 -- diagnostics in generated code.
4888 elsif Nkind (N) = N_Assignment_Statement then
4890 -- Copy name and expression fields in usual manner
4893 Copy_Generic_Node (Name (N), New_N, Instantiating));
4895 Set_Expression (New_N,
4896 Copy_Generic_Node (Expression (N), New_N, Instantiating));
4898 if Instantiating then
4899 Set_Assignment_OK (Name (New_N), True);
4902 elsif Nkind (N) = N_Aggregate
4903 or else Nkind (N) = N_Extension_Aggregate
4906 if not Instantiating then
4907 Set_Associated_Node (N, New_N);
4910 if Present (Get_Associated_Node (N))
4911 and then Nkind (Get_Associated_Node (N)) = Nkind (N)
4913 -- In the generic the aggregate has some composite type. If at
4914 -- the point of instantiation the type has a private view,
4915 -- install the full view (and that of its ancestors, if any).
4918 T : Entity_Id := (Etype (Get_Associated_Node (New_N)));
4923 and then Is_Private_Type (T)
4929 and then Is_Tagged_Type (T)
4930 and then Is_Derived_Type (T)
4932 Rt := Root_Type (T);
4937 if Is_Private_Type (T) then
4948 -- Do not copy the associated node, which points to
4949 -- the generic copy of the aggregate.
4952 use Atree.Unchecked_Access;
4953 -- This code section is part of the implementation of an untyped
4954 -- tree traversal, so it needs direct access to node fields.
4957 Set_Field1 (New_N, Copy_Generic_Descendant (Field1 (N)));
4958 Set_Field2 (New_N, Copy_Generic_Descendant (Field2 (N)));
4959 Set_Field3 (New_N, Copy_Generic_Descendant (Field3 (N)));
4960 Set_Field5 (New_N, Copy_Generic_Descendant (Field5 (N)));
4963 -- Allocators do not have an identifier denoting the access type,
4964 -- so we must locate it through the expression to check whether
4965 -- the views are consistent.
4967 elsif Nkind (N) = N_Allocator
4968 and then Nkind (Expression (N)) = N_Qualified_Expression
4969 and then Is_Entity_Name (Subtype_Mark (Expression (N)))
4970 and then Instantiating
4973 T : constant Node_Id :=
4974 Get_Associated_Node (Subtype_Mark (Expression (N)));
4979 -- Retrieve the allocator node in the generic copy.
4981 Acc_T := Etype (Parent (Parent (T)));
4983 and then Is_Private_Type (Acc_T)
4985 Switch_View (Acc_T);
4992 -- For a proper body, we must catch the case of a proper body that
4993 -- replaces a stub. This represents the point at which a separate
4994 -- compilation unit, and hence template file, may be referenced, so
4995 -- we must make a new source instantiation entry for the template
4996 -- of the subunit, and ensure that all nodes in the subunit are
4997 -- adjusted using this new source instantiation entry.
4999 elsif Nkind (N) in N_Proper_Body then
5001 Save_Adjustment : constant Sloc_Adjustment := S_Adjustment;
5004 if Instantiating and then Was_Originally_Stub (N) then
5005 Create_Instantiation_Source
5006 (Instantiation_Node,
5007 Defining_Entity (N),
5012 -- Now copy the fields of the proper body, using the new
5013 -- adjustment factor if one was needed as per test above.
5017 -- Restore the original adjustment factor in case changed
5019 S_Adjustment := Save_Adjustment;
5022 -- Don't copy Ident or Comment pragmas, since the comment belongs
5023 -- to the generic unit, not to the instantiating unit.
5025 elsif Nkind (N) = N_Pragma
5026 and then Instantiating
5029 Prag_Id : constant Pragma_Id := Get_Pragma_Id (Chars (N));
5032 if Prag_Id = Pragma_Ident
5033 or else Prag_Id = Pragma_Comment
5035 New_N := Make_Null_Statement (Sloc (N));
5042 elsif Nkind (N) = N_Integer_Literal
5043 or else Nkind (N) = N_Real_Literal
5045 -- No descendant fields need traversing
5049 -- For the remaining nodes, copy recursively their descendants
5055 and then Nkind (N) = N_Subprogram_Body
5057 Set_Generic_Parent (Specification (New_N), N);
5062 end Copy_Generic_Node;
5064 ----------------------------
5065 -- Denotes_Formal_Package --
5066 ----------------------------
5068 function Denotes_Formal_Package (Pack : Entity_Id) return Boolean is
5069 Par : constant Entity_Id := Current_Instantiated_Parent.Act_Id;
5070 Scop : constant Entity_Id := Scope (Pack);
5074 if Ekind (Scop) = E_Generic_Package
5075 or else Nkind (Unit_Declaration_Node (Scop)) =
5076 N_Generic_Subprogram_Declaration
5080 elsif Nkind (Parent (Pack)) = N_Formal_Package_Declaration then
5087 -- Check whether this package is associated with a formal
5088 -- package of the enclosing instantiation. Iterate over the
5089 -- list of renamings.
5091 E := First_Entity (Par);
5092 while Present (E) loop
5093 if Ekind (E) /= E_Package
5094 or else Nkind (Parent (E)) /= N_Package_Renaming_Declaration
5097 elsif Renamed_Object (E) = Par then
5100 elsif Renamed_Object (E) = Pack then
5109 end Denotes_Formal_Package;
5115 procedure End_Generic is
5117 -- ??? More things could be factored out in this
5118 -- routine. Should probably be done at a later stage.
5120 Inside_A_Generic := Generic_Flags.Table (Generic_Flags.Last);
5121 Generic_Flags.Decrement_Last;
5123 Expander_Mode_Restore;
5126 ----------------------
5127 -- Find_Actual_Type --
5128 ----------------------
5130 function Find_Actual_Type
5132 Gen_Scope : Entity_Id)
5138 if not Is_Child_Unit (Gen_Scope) then
5139 return Get_Instance_Of (Typ);
5141 elsif not Is_Generic_Type (Typ)
5142 or else Scope (Typ) = Gen_Scope
5144 return Get_Instance_Of (Typ);
5147 T := Current_Entity (Typ);
5148 while Present (T) loop
5149 if In_Open_Scopes (Scope (T)) then
5158 end Find_Actual_Type;
5160 ----------------------------
5161 -- Freeze_Subprogram_Body --
5162 ----------------------------
5164 procedure Freeze_Subprogram_Body
5165 (Inst_Node : Node_Id;
5167 Pack_Id : Entity_Id)
5170 Gen_Unit : constant Entity_Id := Get_Generic_Entity (Inst_Node);
5171 Par : constant Entity_Id := Scope (Gen_Unit);
5176 function Earlier (N1, N2 : Node_Id) return Boolean;
5177 -- Yields True if N1 and N2 appear in the same compilation unit,
5178 -- ignoring subunits, and if N1 is to the left of N2 in a left-to-right
5179 -- traversal of the tree for the unit.
5181 function Enclosing_Body (N : Node_Id) return Node_Id;
5182 -- Find innermost package body that encloses the given node, and which
5183 -- is not a compilation unit. Freeze nodes for the instance, or for its
5184 -- enclosing body, may be inserted after the enclosing_body of the
5187 function Package_Freeze_Node (B : Node_Id) return Node_Id;
5188 -- Find entity for given package body, and locate or create a freeze
5191 function True_Parent (N : Node_Id) return Node_Id;
5192 -- For a subunit, return parent of corresponding stub.
5198 function Earlier (N1, N2 : Node_Id) return Boolean is
5204 procedure Find_Depth (P : in out Node_Id; D : in out Integer);
5205 -- Find distance from given node to enclosing compilation unit.
5211 procedure Find_Depth (P : in out Node_Id; D : in out Integer) is
5214 and then Nkind (P) /= N_Compilation_Unit
5216 P := True_Parent (P);
5221 -- Start of procesing for Earlier
5224 Find_Depth (P1, D1);
5225 Find_Depth (P2, D2);
5235 P1 := True_Parent (P1);
5240 P2 := True_Parent (P2);
5244 -- At this point P1 and P2 are at the same distance from the root.
5245 -- We examine their parents until we find a common declarative
5246 -- list, at which point we can establish their relative placement
5247 -- by comparing their ultimate slocs. If we reach the root,
5248 -- N1 and N2 do not descend from the same declarative list (e.g.
5249 -- one is nested in the declarative part and the other is in a block
5250 -- in the statement part) and the earlier one is already frozen.
5252 while not Is_List_Member (P1)
5253 or else not Is_List_Member (P2)
5254 or else List_Containing (P1) /= List_Containing (P2)
5256 P1 := True_Parent (P1);
5257 P2 := True_Parent (P2);
5259 if Nkind (Parent (P1)) = N_Subunit then
5260 P1 := Corresponding_Stub (Parent (P1));
5263 if Nkind (Parent (P2)) = N_Subunit then
5264 P2 := Corresponding_Stub (Parent (P2));
5273 Top_Level_Location (Sloc (P1)) < Top_Level_Location (Sloc (P2));
5276 --------------------
5277 -- Enclosing_Body --
5278 --------------------
5280 function Enclosing_Body (N : Node_Id) return Node_Id is
5281 P : Node_Id := Parent (N);
5285 and then Nkind (Parent (P)) /= N_Compilation_Unit
5287 if Nkind (P) = N_Package_Body then
5289 if Nkind (Parent (P)) = N_Subunit then
5290 return Corresponding_Stub (Parent (P));
5296 P := True_Parent (P);
5302 -------------------------
5303 -- Package_Freeze_Node --
5304 -------------------------
5306 function Package_Freeze_Node (B : Node_Id) return Node_Id is
5310 if Nkind (B) = N_Package_Body then
5311 Id := Corresponding_Spec (B);
5313 else pragma Assert (Nkind (B) = N_Package_Body_Stub);
5314 Id := Corresponding_Spec (Proper_Body (Unit (Library_Unit (B))));
5317 Ensure_Freeze_Node (Id);
5318 return Freeze_Node (Id);
5319 end Package_Freeze_Node;
5325 function True_Parent (N : Node_Id) return Node_Id is
5327 if Nkind (Parent (N)) = N_Subunit then
5328 return Parent (Corresponding_Stub (Parent (N)));
5334 -- Start of processing of Freeze_Subprogram_Body
5337 -- If the instance and the generic body appear within the same
5338 -- unit, and the instance preceeds the generic, the freeze node for
5339 -- the instance must appear after that of the generic. If the generic
5340 -- is nested within another instance I2, then current instance must
5341 -- be frozen after I2. In both cases, the freeze nodes are those of
5342 -- enclosing packages. Otherwise, the freeze node is placed at the end
5343 -- of the current declarative part.
5345 Enc_G := Enclosing_Body (Gen_Body);
5346 Enc_I := Enclosing_Body (Inst_Node);
5347 Ensure_Freeze_Node (Pack_Id);
5348 F_Node := Freeze_Node (Pack_Id);
5350 if Is_Generic_Instance (Par)
5351 and then Present (Freeze_Node (Par))
5353 In_Same_Declarative_Part (Freeze_Node (Par), Inst_Node)
5355 if ABE_Is_Certain (Get_Package_Instantiation_Node (Par)) then
5357 -- The parent was a premature instantiation. Insert freeze
5358 -- node at the end the current declarative part.
5360 Insert_After_Last_Decl (Inst_Node, F_Node);
5363 Insert_After (Freeze_Node (Par), F_Node);
5366 -- The body enclosing the instance should be frozen after the body
5367 -- that includes the generic, because the body of the instance may
5368 -- make references to entities therein. If the two are not in the
5369 -- same declarative part, or if the one enclosing the instance is
5370 -- frozen already, freeze the instance at the end of the current
5371 -- declarative part.
5373 elsif Is_Generic_Instance (Par)
5374 and then Present (Freeze_Node (Par))
5375 and then Present (Enc_I)
5377 if In_Same_Declarative_Part (Freeze_Node (Par), Enc_I)
5379 (Nkind (Enc_I) = N_Package_Body
5381 In_Same_Declarative_Part (Freeze_Node (Par), Parent (Enc_I)))
5383 -- The enclosing package may contain several instances. Rather
5384 -- than computing the earliest point at which to insert its
5385 -- freeze node, we place it at the end of the declarative part
5386 -- of the parent of the generic.
5388 Insert_After_Last_Decl
5389 (Freeze_Node (Par), Package_Freeze_Node (Enc_I));
5392 Insert_After_Last_Decl (Inst_Node, F_Node);
5394 elsif Present (Enc_G)
5395 and then Present (Enc_I)
5396 and then Enc_G /= Enc_I
5397 and then Earlier (Inst_Node, Gen_Body)
5399 if Nkind (Enc_G) = N_Package_Body then
5400 E_G_Id := Corresponding_Spec (Enc_G);
5401 else pragma Assert (Nkind (Enc_G) = N_Package_Body_Stub);
5403 Corresponding_Spec (Proper_Body (Unit (Library_Unit (Enc_G))));
5406 -- Freeze package that encloses instance, and place node after
5407 -- package that encloses generic. If enclosing package is already
5408 -- frozen we have to assume it is at the proper place. This may
5409 -- be a potential ABE that requires dynamic checking.
5411 Insert_After_Last_Decl (Enc_G, Package_Freeze_Node (Enc_I));
5413 -- Freeze enclosing subunit before instance
5415 Ensure_Freeze_Node (E_G_Id);
5417 if not Is_List_Member (Freeze_Node (E_G_Id)) then
5418 Insert_After (Enc_G, Freeze_Node (E_G_Id));
5421 Insert_After_Last_Decl (Inst_Node, F_Node);
5424 -- If none of the above, insert freeze node at the end of the
5425 -- current declarative part.
5427 Insert_After_Last_Decl (Inst_Node, F_Node);
5429 end Freeze_Subprogram_Body;
5435 function Get_Gen_Id (E : Assoc_Ptr) return Entity_Id is
5437 return Generic_Renamings.Table (E).Gen_Id;
5440 ---------------------
5441 -- Get_Instance_Of --
5442 ---------------------
5444 function Get_Instance_Of (A : Entity_Id) return Entity_Id is
5445 Res : constant Assoc_Ptr := Generic_Renamings_HTable.Get (A);
5448 if Res /= Assoc_Null then
5449 return Generic_Renamings.Table (Res).Act_Id;
5451 -- On exit, entity is not instantiated: not a generic parameter,
5452 -- or else parameter of an inner generic unit.
5456 end Get_Instance_Of;
5458 ------------------------------------
5459 -- Get_Package_Instantiation_Node --
5460 ------------------------------------
5462 function Get_Package_Instantiation_Node (A : Entity_Id) return Node_Id is
5463 Decl : Node_Id := Unit_Declaration_Node (A);
5467 -- If the instantiation is a compilation unit that does not need a
5468 -- body then the instantiation node has been rewritten as a package
5469 -- declaration for the instance, and we return the original node.
5471 -- If it is a compilation unit and the instance node has not been
5472 -- rewritten, then it is still the unit of the compilation. Finally,
5473 -- if a body is present, this is a parent of the main unit whose body
5474 -- has been compiled for inlining purposes, and the instantiation node
5475 -- has been rewritten with the instance body.
5477 -- Otherwise the instantiation node appears after the declaration.
5478 -- If the entity is a formal package, the declaration may have been
5479 -- rewritten as a generic declaration (in the case of a formal with a
5480 -- box) or left as a formal package declaration if it has actuals, and
5481 -- is found with a forward search.
5483 if Nkind (Parent (Decl)) = N_Compilation_Unit then
5484 if Nkind (Decl) = N_Package_Declaration
5485 and then Present (Corresponding_Body (Decl))
5487 Decl := Unit_Declaration_Node (Corresponding_Body (Decl));
5490 if Nkind (Original_Node (Decl)) = N_Package_Instantiation then
5491 return Original_Node (Decl);
5493 return Unit (Parent (Decl));
5496 elsif Nkind (Decl) = N_Generic_Package_Declaration
5497 and then Nkind (Original_Node (Decl)) = N_Formal_Package_Declaration
5499 return Original_Node (Decl);
5502 Inst := Next (Decl);
5503 while Nkind (Inst) /= N_Package_Instantiation
5504 and then Nkind (Inst) /= N_Formal_Package_Declaration
5511 end Get_Package_Instantiation_Node;
5513 ------------------------
5514 -- Has_Been_Exchanged --
5515 ------------------------
5517 function Has_Been_Exchanged (E : Entity_Id) return Boolean is
5518 Next : Elmt_Id := First_Elmt (Exchanged_Views);
5521 while Present (Next) loop
5522 if Full_View (Node (Next)) = E then
5530 end Has_Been_Exchanged;
5536 function Hash (F : Entity_Id) return HTable_Range is
5538 return HTable_Range (F mod HTable_Size);
5541 ------------------------
5542 -- Hide_Current_Scope --
5543 ------------------------
5545 procedure Hide_Current_Scope is
5546 C : constant Entity_Id := Current_Scope;
5550 Set_Is_Hidden_Open_Scope (C);
5551 E := First_Entity (C);
5553 while Present (E) loop
5554 if Is_Immediately_Visible (E) then
5555 Set_Is_Immediately_Visible (E, False);
5556 Append_Elmt (E, Hidden_Entities);
5562 -- Make the scope name invisible as well. This is necessary, but
5563 -- might conflict with calls to Rtsfind later on, in case the scope
5564 -- is a predefined one. There is no clean solution to this problem, so
5565 -- for now we depend on the user not redefining Standard itself in one
5566 -- of the parent units.
5568 if Is_Immediately_Visible (C)
5569 and then C /= Standard_Standard
5571 Set_Is_Immediately_Visible (C, False);
5572 Append_Elmt (C, Hidden_Entities);
5575 end Hide_Current_Scope;
5581 procedure Init_Env is
5582 Saved : Instance_Env;
5585 Saved.Ada_83 := Ada_83;
5586 Saved.Instantiated_Parent := Current_Instantiated_Parent;
5587 Saved.Exchanged_Views := Exchanged_Views;
5588 Saved.Hidden_Entities := Hidden_Entities;
5589 Saved.Current_Sem_Unit := Current_Sem_Unit;
5590 Instance_Envs.Increment_Last;
5591 Instance_Envs.Table (Instance_Envs.Last) := Saved;
5593 Exchanged_Views := New_Elmt_List;
5594 Hidden_Entities := New_Elmt_List;
5596 -- Make dummy entry for Instantiated parent. If generic unit is
5597 -- legal, this is set properly in Set_Instance_Env.
5599 Current_Instantiated_Parent :=
5600 (Current_Scope, Current_Scope, Assoc_Null);
5603 ------------------------------
5604 -- In_Same_Declarative_Part --
5605 ------------------------------
5607 function In_Same_Declarative_Part
5612 Decls : constant Node_Id := Parent (F_Node);
5613 Nod : Node_Id := Parent (Inst);
5616 while Present (Nod) loop
5620 elsif Nkind (Nod) = N_Subprogram_Body
5621 or else Nkind (Nod) = N_Package_Body
5622 or else Nkind (Nod) = N_Task_Body
5623 or else Nkind (Nod) = N_Protected_Body
5624 or else Nkind (Nod) = N_Block_Statement
5628 elsif Nkind (Nod) = N_Subunit then
5629 Nod := Corresponding_Stub (Nod);
5631 elsif Nkind (Nod) = N_Compilation_Unit then
5634 Nod := Parent (Nod);
5639 end In_Same_Declarative_Part;
5641 ---------------------
5642 -- Inherit_Context --
5643 ---------------------
5645 procedure Inherit_Context (Gen_Decl : Node_Id; Inst : Node_Id) is
5646 Current_Context : List_Id;
5647 Current_Unit : Node_Id;
5652 if Nkind (Parent (Gen_Decl)) = N_Compilation_Unit then
5654 -- The inherited context is attached to the enclosing compilation
5655 -- unit. This is either the main unit, or the declaration for the
5656 -- main unit (in case the instantation appears within the package
5657 -- declaration and the main unit is its body).
5659 Current_Unit := Parent (Inst);
5660 while Present (Current_Unit)
5661 and then Nkind (Current_Unit) /= N_Compilation_Unit
5663 Current_Unit := Parent (Current_Unit);
5666 Current_Context := Context_Items (Current_Unit);
5668 Item := First (Context_Items (Parent (Gen_Decl)));
5669 while Present (Item) loop
5670 if Nkind (Item) = N_With_Clause then
5671 New_I := New_Copy (Item);
5672 Set_Implicit_With (New_I, True);
5673 Append (New_I, Current_Context);
5679 end Inherit_Context;
5685 procedure Initialize is
5687 Generic_Renamings.Init;
5690 Generic_Renamings_HTable.Reset;
5691 Circularity_Detected := False;
5692 Exchanged_Views := No_Elist;
5693 Hidden_Entities := No_Elist;
5696 ----------------------------
5697 -- Insert_After_Last_Decl --
5698 ----------------------------
5700 procedure Insert_After_Last_Decl (N : Node_Id; F_Node : Node_Id) is
5701 L : List_Id := List_Containing (N);
5702 P : constant Node_Id := Parent (L);
5705 if not Is_List_Member (F_Node) then
5706 if Nkind (P) = N_Package_Specification
5707 and then L = Visible_Declarations (P)
5708 and then Present (Private_Declarations (P))
5709 and then not Is_Empty_List (Private_Declarations (P))
5711 L := Private_Declarations (P);
5714 Insert_After (Last (L), F_Node);
5716 end Insert_After_Last_Decl;
5722 procedure Install_Body
5723 (Act_Body : Node_Id;
5728 Act_Id : constant Entity_Id := Corresponding_Spec (Act_Body);
5729 Act_Unit : constant Node_Id := Unit (Cunit (Get_Source_Unit (N)));
5730 Gen_Id : constant Entity_Id := Corresponding_Spec (Gen_Body);
5731 Par : constant Entity_Id := Scope (Gen_Id);
5732 Gen_Unit : constant Node_Id :=
5733 Unit (Cunit (Get_Source_Unit (Gen_Decl)));
5734 Orig_Body : Node_Id := Gen_Body;
5736 Body_Unit : Node_Id;
5738 Must_Delay : Boolean;
5740 function Enclosing_Subp (Id : Entity_Id) return Entity_Id;
5741 -- Find subprogram (if any) that encloses instance and/or generic body.
5743 function True_Sloc (N : Node_Id) return Source_Ptr;
5744 -- If the instance is nested inside a generic unit, the Sloc of the
5745 -- instance indicates the place of the original definition, not the
5746 -- point of the current enclosing instance. Pending a better usage of
5747 -- Slocs to indicate instantiation places, we determine the place of
5748 -- origin of a node by finding the maximum sloc of any ancestor node.
5749 -- Why is this not equivalent fo Top_Level_Location ???
5751 function Enclosing_Subp (Id : Entity_Id) return Entity_Id is
5752 Scop : Entity_Id := Scope (Id);
5755 while Scop /= Standard_Standard
5756 and then not Is_Overloadable (Scop)
5758 Scop := Scope (Scop);
5764 function True_Sloc (N : Node_Id) return Source_Ptr is
5771 while Present (N1) and then N1 /= Act_Unit loop
5772 if Sloc (N1) > Res then
5782 -- Start of processing for Install_Body
5785 -- If the body is a subunit, the freeze point is the corresponding
5786 -- stub in the current compilation, not the subunit itself.
5788 if Nkind (Parent (Gen_Body)) = N_Subunit then
5789 Orig_Body := Corresponding_Stub (Parent (Gen_Body));
5791 Orig_Body := Gen_Body;
5794 Body_Unit := Unit (Cunit (Get_Source_Unit (Orig_Body)));
5796 -- If the instantiation and the generic definition appear in the
5797 -- same package declaration, this is an early instantiation.
5798 -- If they appear in the same declarative part, it is an early
5799 -- instantiation only if the generic body appears textually later,
5800 -- and the generic body is also in the main unit.
5802 -- If instance is nested within a subprogram, and the generic body is
5803 -- not, the instance is delayed because the enclosing body is. If
5804 -- instance and body are within the same scope, or the same sub-
5805 -- program body, indicate explicitly that the instance is delayed.
5808 (Gen_Unit = Act_Unit
5809 and then ((Nkind (Gen_Unit) = N_Package_Declaration)
5810 or else Nkind (Gen_Unit) = N_Generic_Package_Declaration
5811 or else (Gen_Unit = Body_Unit
5812 and then True_Sloc (N) < Sloc (Orig_Body)))
5813 and then Is_In_Main_Unit (Gen_Unit)
5814 and then (Scope (Act_Id) = Scope (Gen_Id)
5816 Enclosing_Subp (Act_Id) = Enclosing_Subp (Gen_Id)));
5818 -- If this is an early instantiation, the freeze node is placed after
5819 -- the generic body. Otherwise, if the generic appears in an instance,
5820 -- we cannot freeze the current instance until the outer one is frozen.
5821 -- This is only relevant if the current instance is nested within some
5822 -- inner scope not itself within the outer instance. If this scope is
5823 -- a package body in the same declarative part as the outer instance,
5824 -- then that body needs to be frozen after the outer instance. Finally,
5825 -- if no delay is needed, we place the freeze node at the end of the
5826 -- current declarative part.
5828 if Expander_Active then
5829 Ensure_Freeze_Node (Act_Id);
5830 F_Node := Freeze_Node (Act_Id);
5833 Insert_After (Orig_Body, F_Node);
5835 elsif Is_Generic_Instance (Par)
5836 and then Present (Freeze_Node (Par))
5837 and then Scope (Act_Id) /= Par
5839 -- Freeze instance of inner generic after instance of enclosing
5842 if In_Same_Declarative_Part (Freeze_Node (Par), N) then
5843 Insert_After (Freeze_Node (Par), F_Node);
5845 -- Freeze package enclosing instance of inner generic after
5846 -- instance of enclosing generic.
5848 elsif Nkind (Parent (N)) = N_Package_Body
5849 and then In_Same_Declarative_Part (Freeze_Node (Par), Parent (N))
5853 Enclosing : constant Entity_Id :=
5854 Corresponding_Spec (Parent (N));
5857 Insert_After_Last_Decl (N, F_Node);
5858 Ensure_Freeze_Node (Enclosing);
5860 if not Is_List_Member (Freeze_Node (Enclosing)) then
5861 Insert_After (Freeze_Node (Par), Freeze_Node (Enclosing));
5866 Insert_After_Last_Decl (N, F_Node);
5870 Insert_After_Last_Decl (N, F_Node);
5874 Set_Is_Frozen (Act_Id);
5875 Insert_Before (N, Act_Body);
5876 Mark_Rewrite_Insertion (Act_Body);
5879 --------------------
5880 -- Install_Parent --
5881 --------------------
5883 procedure Install_Parent (P : Entity_Id; In_Body : Boolean := False) is
5884 Ancestors : constant Elist_Id := New_Elmt_List;
5885 S : constant Entity_Id := Current_Scope;
5886 Inst_Par : Entity_Id;
5887 First_Par : Entity_Id;
5888 Inst_Node : Node_Id;
5889 Gen_Par : Entity_Id;
5890 First_Gen : Entity_Id;
5893 procedure Install_Formal_Packages (Par : Entity_Id);
5894 -- If any of the formals of the parent are formal packages with box,
5895 -- their formal parts are visible in the parent and thus in the child
5896 -- unit as well. Analogous to what is done in Check_Generic_Actuals
5897 -- for the unit itself.
5899 procedure Install_Noninstance_Specs (Par : Entity_Id);
5900 -- Install the scopes of noninstance parent units ending with Par.
5902 procedure Install_Spec (Par : Entity_Id);
5903 -- The child unit is within the declarative part of the parent, so
5904 -- the declarations within the parent are immediately visible.
5906 -----------------------------
5907 -- Install_Formal_Packages --
5908 -----------------------------
5910 procedure Install_Formal_Packages (Par : Entity_Id) is
5914 E := First_Entity (Par);
5916 while Present (E) loop
5918 if Ekind (E) = E_Package
5919 and then Nkind (Parent (E)) = N_Package_Renaming_Declaration
5921 -- If this is the renaming for the parent instance, done.
5923 if Renamed_Object (E) = Par then
5926 -- The visibility of a formal of an enclosing generic is
5929 elsif Denotes_Formal_Package (E) then
5932 elsif Present (Associated_Formal_Package (E))
5933 and then Box_Present (Parent (Associated_Formal_Package (E)))
5935 Check_Generic_Actuals (Renamed_Object (E), True);
5936 Set_Is_Hidden (E, False);
5942 end Install_Formal_Packages;
5944 -------------------------------
5945 -- Install_Noninstance_Specs --
5946 -------------------------------
5948 procedure Install_Noninstance_Specs (Par : Entity_Id) is
5951 and then Par /= Standard_Standard
5952 and then not In_Open_Scopes (Par)
5954 Install_Noninstance_Specs (Scope (Par));
5957 end Install_Noninstance_Specs;
5963 procedure Install_Spec (Par : Entity_Id) is
5964 Spec : constant Node_Id :=
5965 Specification (Unit_Declaration_Node (Par));
5969 Set_Is_Immediately_Visible (Par);
5970 Install_Visible_Declarations (Par);
5971 Install_Private_Declarations (Par);
5972 Set_Use (Visible_Declarations (Spec));
5973 Set_Use (Private_Declarations (Spec));
5976 -- Start of processing for Install_Parent
5979 -- We need to install the parent instance to compile the instantiation
5980 -- of the child, but the child instance must appear in the current
5981 -- scope. Given that we cannot place the parent above the current
5982 -- scope in the scope stack, we duplicate the current scope and unstack
5983 -- both after the instantiation is complete.
5985 -- If the parent is itself the instantiation of a child unit, we must
5986 -- also stack the instantiation of its parent, and so on. Each such
5987 -- ancestor is the prefix of the name in a prior instantiation.
5989 -- If this is a nested instance, the parent unit itself resolves to
5990 -- a renaming of the parent instance, whose declaration we need.
5992 -- Finally, the parent may be a generic (not an instance) when the
5993 -- child unit appears as a formal package.
5997 if Present (Renamed_Entity (Inst_Par)) then
5998 Inst_Par := Renamed_Entity (Inst_Par);
6001 First_Par := Inst_Par;
6004 Generic_Parent (Specification (Unit_Declaration_Node (Inst_Par)));
6006 First_Gen := Gen_Par;
6008 while Present (Gen_Par)
6009 and then Is_Child_Unit (Gen_Par)
6011 -- Load grandparent instance as well
6013 Inst_Node := Get_Package_Instantiation_Node (Inst_Par);
6015 if Nkind (Name (Inst_Node)) = N_Expanded_Name then
6016 Inst_Par := Entity (Prefix (Name (Inst_Node)));
6018 if Present (Renamed_Entity (Inst_Par)) then
6019 Inst_Par := Renamed_Entity (Inst_Par);
6024 (Specification (Unit_Declaration_Node (Inst_Par)));
6026 if Present (Gen_Par) then
6027 Prepend_Elmt (Inst_Par, Ancestors);
6030 -- Parent is not the name of an instantiation
6032 Install_Noninstance_Specs (Inst_Par);
6044 if Present (First_Gen) then
6045 Append_Elmt (First_Par, Ancestors);
6048 Install_Noninstance_Specs (First_Par);
6051 if not Is_Empty_Elmt_List (Ancestors) then
6052 Elmt := First_Elmt (Ancestors);
6054 while Present (Elmt) loop
6055 Install_Spec (Node (Elmt));
6056 Install_Formal_Packages (Node (Elmt));
6067 --------------------------------
6068 -- Instantiate_Formal_Package --
6069 --------------------------------
6071 function Instantiate_Formal_Package
6074 Analyzed_Formal : Node_Id)
6077 Loc : constant Source_Ptr := Sloc (Actual);
6078 Actual_Pack : Entity_Id;
6079 Formal_Pack : Entity_Id;
6080 Gen_Parent : Entity_Id;
6083 Parent_Spec : Node_Id;
6085 procedure Find_Matching_Actual
6087 Act : in out Entity_Id);
6088 -- We need to associate each formal entity in the formal package
6089 -- with the corresponding entity in the actual package. The actual
6090 -- package has been analyzed and possibly expanded, and as a result
6091 -- there is no one-to-one correspondence between the two lists (for
6092 -- example, the actual may include subtypes, itypes, and inherited
6093 -- primitive operations, interspersed among the renaming declarations
6094 -- for the actuals) . We retrieve the corresponding actual by name
6095 -- because each actual has the same name as the formal, and they do
6096 -- appear in the same order.
6098 function Formal_Entity
6100 Act_Ent : Entity_Id)
6102 -- Returns the entity associated with the given formal F. In the
6103 -- case where F is a formal package, this function will iterate
6104 -- through all of F's formals and enter map associations from the
6105 -- actuals occurring in the formal package's corresponding actual
6106 -- package (obtained via Act_Ent) to the formal package's formal
6107 -- parameters. This function is called recursively for arbitrary
6108 -- levels of formal packages.
6110 function Is_Instance_Of
6111 (Act_Spec : Entity_Id;
6112 Gen_Anc : Entity_Id)
6114 -- The actual can be an instantiation of a generic within another
6115 -- instance, in which case there is no direct link from it to the
6116 -- original generic ancestor. In that case, we recognize that the
6117 -- ultimate ancestor is the same by examining names and scopes.
6119 procedure Map_Entities (Form : Entity_Id; Act : Entity_Id);
6120 -- Within the generic part, entities in the formal package are
6121 -- visible. To validate subsequent type declarations, indicate
6122 -- the correspondence betwen the entities in the analyzed formal,
6123 -- and the entities in the actual package. There are three packages
6124 -- involved in the instantiation of a formal package: the parent
6125 -- generic P1 which appears in the generic declaration, the fake
6126 -- instantiation P2 which appears in the analyzed generic, and whose
6127 -- visible entities may be used in subsequent formals, and the actual
6128 -- P3 in the instance. To validate subsequent formals, me indicate
6129 -- that the entities in P2 are mapped into those of P3. The mapping of
6130 -- entities has to be done recursively for nested packages.
6132 --------------------------
6133 -- Find_Matching_Actual --
6134 --------------------------
6136 procedure Find_Matching_Actual
6138 Act : in out Entity_Id)
6140 Formal_Ent : Entity_Id;
6143 case Nkind (Original_Node (F)) is
6144 when N_Formal_Object_Declaration |
6145 N_Formal_Type_Declaration =>
6146 Formal_Ent := Defining_Identifier (F);
6148 while Chars (Act) /= Chars (Formal_Ent) loop
6152 when N_Formal_Subprogram_Declaration |
6153 N_Formal_Package_Declaration |
6154 N_Package_Declaration |
6155 N_Generic_Package_Declaration =>
6156 Formal_Ent := Defining_Entity (F);
6158 while Chars (Act) /= Chars (Formal_Ent) loop
6164 pragma Assert (False);
6166 end Find_Matching_Actual;
6172 function Formal_Entity
6174 Act_Ent : Entity_Id)
6177 Orig_Node : Node_Id := F;
6178 Act_Pkg : Entity_Id;
6181 case Nkind (Original_Node (F)) is
6182 when N_Formal_Object_Declaration =>
6183 return Defining_Identifier (F);
6185 when N_Formal_Type_Declaration =>
6186 return Defining_Identifier (F);
6188 when N_Formal_Subprogram_Declaration =>
6189 return Defining_Unit_Name (Specification (F));
6191 when N_Package_Declaration =>
6192 return Defining_Unit_Name (Specification (F));
6194 when N_Formal_Package_Declaration |
6195 N_Generic_Package_Declaration =>
6197 if Nkind (F) = N_Generic_Package_Declaration then
6198 Orig_Node := Original_Node (F);
6203 -- Find matching actual package, skipping over itypes and
6204 -- other entities generated when analyzing the formal. We
6205 -- know that if the instantiation is legal then there is
6206 -- a matching package for the formal.
6208 while Ekind (Act_Pkg) /= E_Package loop
6209 Act_Pkg := Next_Entity (Act_Pkg);
6213 Actual_Ent : Entity_Id := First_Entity (Act_Pkg);
6214 Formal_Node : Node_Id;
6215 Formal_Ent : Entity_Id;
6217 Gen_Decl : constant Node_Id :=
6218 Unit_Declaration_Node
6219 (Entity (Name (Orig_Node)));
6221 Formals : constant List_Id :=
6222 Generic_Formal_Declarations (Gen_Decl);
6225 if Present (Formals) then
6226 Formal_Node := First_Non_Pragma (Formals);
6228 Formal_Node := Empty;
6231 while Present (Actual_Ent)
6232 and then Present (Formal_Node)
6233 and then Actual_Ent /= First_Private_Entity (Act_Ent)
6235 -- ??? Are the following calls also needed here:
6237 -- Set_Is_Hidden (Actual_Ent, False);
6238 -- Set_Is_Potentially_Use_Visible
6239 -- (Actual_Ent, In_Use (Act_Ent));
6241 Formal_Ent := Formal_Entity (Formal_Node, Actual_Ent);
6242 if Present (Formal_Ent) then
6243 Set_Instance_Of (Formal_Ent, Actual_Ent);
6245 Next_Non_Pragma (Formal_Node);
6247 Next_Entity (Actual_Ent);
6251 return Defining_Identifier (Orig_Node);
6253 when N_Use_Package_Clause =>
6256 when N_Use_Type_Clause =>
6259 -- We return Empty for all other encountered forms of
6260 -- declarations because there are some cases of nonformal
6261 -- sorts of declaration that can show up (e.g., when array
6262 -- formals are present). Since it's not clear what kinds
6263 -- can appear among the formals, we won't raise failure here.
6271 --------------------
6272 -- Is_Instance_Of --
6273 --------------------
6275 function Is_Instance_Of
6276 (Act_Spec : Entity_Id;
6277 Gen_Anc : Entity_Id)
6280 Gen_Par : constant Entity_Id := Generic_Parent (Act_Spec);
6283 if No (Gen_Par) then
6286 -- Simplest case: the generic parent of the actual is the formal.
6288 elsif Gen_Par = Gen_Anc then
6291 elsif Chars (Gen_Par) /= Chars (Gen_Anc) then
6294 -- The actual may be obtained through several instantiations. Its
6295 -- scope must itself be an instance of a generic declared in the
6296 -- same scope as the formal. Any other case is detected above.
6298 elsif not Is_Generic_Instance (Scope (Gen_Par)) then
6302 return Generic_Parent (Parent (Scope (Gen_Par))) = Scope (Gen_Anc);
6310 procedure Map_Entities (Form : Entity_Id; Act : Entity_Id) is
6315 Set_Instance_Of (Form, Act);
6317 -- Traverse formal and actual package to map the corresponding
6318 -- entities. We skip over internal entities that may be generated
6319 -- during semantic analysis, and find the matching entities by
6320 -- name, given that they must appear in the same order.
6322 E1 := First_Entity (Form);
6323 E2 := First_Entity (Act);
6325 and then E1 /= First_Private_Entity (Form)
6327 if not Is_Internal (E1)
6328 and then not Is_Class_Wide_Type (E1)
6329 and then Present (Parent (E1))
6332 and then Chars (E2) /= Chars (E1)
6340 Set_Instance_Of (E1, E2);
6343 and then Is_Tagged_Type (E2)
6346 (Class_Wide_Type (E1), Class_Wide_Type (E2));
6349 if Ekind (E1) = E_Package
6350 and then No (Renamed_Object (E1))
6352 Map_Entities (E1, E2);
6361 -- Start of processing for Instantiate_Formal_Package
6366 if not Is_Entity_Name (Actual)
6367 or else Ekind (Entity (Actual)) /= E_Package
6370 ("expect package instance to instantiate formal", Actual);
6371 Abandon_Instantiation (Actual);
6372 raise Program_Error;
6375 Actual_Pack := Entity (Actual);
6376 Set_Is_Instantiated (Actual_Pack);
6378 -- The actual may be a renamed package, or an outer generic
6379 -- formal package whose instantiation is converted into a renaming.
6381 if Present (Renamed_Object (Actual_Pack)) then
6382 Actual_Pack := Renamed_Object (Actual_Pack);
6385 if Nkind (Analyzed_Formal) = N_Formal_Package_Declaration then
6386 Gen_Parent := Get_Instance_Of (Entity (Name (Analyzed_Formal)));
6387 Formal_Pack := Defining_Identifier (Analyzed_Formal);
6390 Generic_Parent (Specification (Analyzed_Formal));
6392 Defining_Unit_Name (Specification (Analyzed_Formal));
6395 if Nkind (Parent (Actual_Pack)) = N_Defining_Program_Unit_Name then
6396 Parent_Spec := Specification (Unit_Declaration_Node (Actual_Pack));
6398 Parent_Spec := Parent (Actual_Pack);
6401 if Gen_Parent = Any_Id then
6403 ("previous error in declaration of formal package", Actual);
6404 Abandon_Instantiation (Actual);
6407 Is_Instance_Of (Parent_Spec, Get_Instance_Of (Gen_Parent))
6413 ("actual parameter must be instance of&", Actual, Gen_Parent);
6414 Abandon_Instantiation (Actual);
6417 Set_Instance_Of (Defining_Identifier (Formal), Actual_Pack);
6418 Map_Entities (Formal_Pack, Actual_Pack);
6421 Make_Package_Renaming_Declaration (Loc,
6422 Defining_Unit_Name => New_Copy (Defining_Identifier (Formal)),
6423 Name => New_Reference_To (Actual_Pack, Loc));
6425 Set_Associated_Formal_Package (Defining_Unit_Name (Nod),
6426 Defining_Identifier (Formal));
6427 Decls := New_List (Nod);
6429 -- If the formal F has a box, then the generic declarations are
6430 -- visible in the generic G. In an instance of G, the corresponding
6431 -- entities in the actual for F (which are the actuals for the
6432 -- instantiation of the generic that F denotes) must also be made
6433 -- visible for analysis of the current instance. On exit from the
6434 -- current instance, those entities are made private again. If the
6435 -- actual is currently in use, these entities are also use-visible.
6437 -- The loop through the actual entities also steps through the
6438 -- formal entities and enters associations from formals to
6439 -- actuals into the renaming map. This is necessary to properly
6440 -- handle checking of actual parameter associations for later
6441 -- formals that depend on actuals declared in the formal package.
6443 if Box_Present (Formal) then
6445 Gen_Decl : constant Node_Id :=
6446 Unit_Declaration_Node (Gen_Parent);
6447 Formals : constant List_Id :=
6448 Generic_Formal_Declarations (Gen_Decl);
6449 Actual_Ent : Entity_Id;
6450 Formal_Node : Node_Id;
6451 Formal_Ent : Entity_Id;
6454 if Present (Formals) then
6455 Formal_Node := First_Non_Pragma (Formals);
6457 Formal_Node := Empty;
6460 Actual_Ent := First_Entity (Actual_Pack);
6462 while Present (Actual_Ent)
6463 and then Actual_Ent /= First_Private_Entity (Actual_Pack)
6465 Set_Is_Hidden (Actual_Ent, False);
6466 Set_Is_Potentially_Use_Visible
6467 (Actual_Ent, In_Use (Actual_Pack));
6469 if Present (Formal_Node) then
6470 Formal_Ent := Formal_Entity (Formal_Node, Actual_Ent);
6472 if Present (Formal_Ent) then
6473 Find_Matching_Actual (Formal_Node, Actual_Ent);
6474 Set_Instance_Of (Formal_Ent, Actual_Ent);
6477 Next_Non_Pragma (Formal_Node);
6480 -- No further formals to match.
6488 -- If the formal is not declared with a box, reanalyze it as
6489 -- an instantiation, to verify the matching rules of 12.7. The
6490 -- actual checks are performed after the generic associations
6495 I_Pack : constant Entity_Id :=
6496 Make_Defining_Identifier (Sloc (Actual),
6497 Chars => New_Internal_Name ('P'));
6500 Set_Is_Internal (I_Pack);
6503 Make_Package_Instantiation (Sloc (Actual),
6504 Defining_Unit_Name => I_Pack,
6505 Name => New_Occurrence_Of (Gen_Parent, Sloc (Actual)),
6506 Generic_Associations =>
6507 Generic_Associations (Formal)));
6513 end Instantiate_Formal_Package;
6515 -----------------------------------
6516 -- Instantiate_Formal_Subprogram --
6517 -----------------------------------
6519 function Instantiate_Formal_Subprogram
6522 Analyzed_Formal : Node_Id)
6525 Loc : Source_Ptr := Sloc (Instantiation_Node);
6526 Formal_Sub : constant Entity_Id :=
6527 Defining_Unit_Name (Specification (Formal));
6528 Analyzed_S : constant Entity_Id :=
6529 Defining_Unit_Name (Specification (Analyzed_Formal));
6530 Decl_Node : Node_Id;
6534 function From_Parent_Scope (Subp : Entity_Id) return Boolean;
6535 -- If the generic is a child unit, the parent has been installed
6536 -- on the scope stack, but a default subprogram cannot resolve to
6537 -- something on the parent because that parent is not really part
6538 -- of the visible context (it is there to resolve explicit local
6539 -- entities). If the default has resolved in this way, we remove
6540 -- the entity from immediate visibility and analyze the node again
6541 -- to emit an error message or find another visible candidate.
6543 procedure Valid_Actual_Subprogram (Act : Node_Id);
6544 -- Perform legality check and raise exception on failure.
6546 -----------------------
6547 -- From_Parent_Scope --
6548 -----------------------
6550 function From_Parent_Scope (Subp : Entity_Id) return Boolean is
6551 Gen_Scope : Node_Id := Scope (Analyzed_S);
6554 while Present (Gen_Scope)
6555 and then Is_Child_Unit (Gen_Scope)
6557 if Scope (Subp) = Scope (Gen_Scope) then
6561 Gen_Scope := Scope (Gen_Scope);
6565 end From_Parent_Scope;
6567 -----------------------------
6568 -- Valid_Actual_Subprogram --
6569 -----------------------------
6571 procedure Valid_Actual_Subprogram (Act : Node_Id) is
6572 Act_E : Entity_Id := Empty;
6575 if Is_Entity_Name (Act) then
6576 Act_E := Entity (Act);
6577 elsif Nkind (Act) = N_Selected_Component
6578 and then Is_Entity_Name (Selector_Name (Act))
6580 Act_E := Entity (Selector_Name (Act));
6583 if (Present (Act_E) and then Is_Overloadable (Act_E))
6584 or else Nkind (Act) = N_Attribute_Reference
6585 or else Nkind (Act) = N_Indexed_Component
6586 or else Nkind (Act) = N_Character_Literal
6587 or else Nkind (Act) = N_Explicit_Dereference
6593 ("expect subprogram or entry name in instantiation of&",
6594 Instantiation_Node, Formal_Sub);
6595 Abandon_Instantiation (Instantiation_Node);
6597 end Valid_Actual_Subprogram;
6599 -- Start of processing for Instantiate_Formal_Subprogram
6602 New_Spec := New_Copy_Tree (Specification (Formal));
6604 -- Create new entity for the actual (New_Copy_Tree does not).
6606 Set_Defining_Unit_Name
6607 (New_Spec, Make_Defining_Identifier (Loc, Chars (Formal_Sub)));
6609 -- Find entity of actual. If the actual is an attribute reference, it
6610 -- cannot be resolved here (its formal is missing) but is handled
6611 -- instead in Attribute_Renaming. If the actual is overloaded, it is
6612 -- fully resolved subsequently, when the renaming declaration for the
6613 -- formal is analyzed. If it is an explicit dereference, resolve the
6614 -- prefix but not the actual itself, to prevent interpretation as a
6617 if Present (Actual) then
6618 Loc := Sloc (Actual);
6619 Set_Sloc (New_Spec, Loc);
6621 if Nkind (Actual) = N_Operator_Symbol then
6622 Find_Direct_Name (Actual);
6624 elsif Nkind (Actual) = N_Explicit_Dereference then
6625 Analyze (Prefix (Actual));
6627 elsif Nkind (Actual) /= N_Attribute_Reference then
6631 Valid_Actual_Subprogram (Actual);
6634 elsif Present (Default_Name (Formal)) then
6635 if Nkind (Default_Name (Formal)) /= N_Attribute_Reference
6636 and then Nkind (Default_Name (Formal)) /= N_Selected_Component
6637 and then Nkind (Default_Name (Formal)) /= N_Indexed_Component
6638 and then Nkind (Default_Name (Formal)) /= N_Character_Literal
6639 and then Present (Entity (Default_Name (Formal)))
6641 Nam := New_Occurrence_Of (Entity (Default_Name (Formal)), Loc);
6643 Nam := New_Copy (Default_Name (Formal));
6644 Set_Sloc (Nam, Loc);
6647 elsif Box_Present (Formal) then
6649 -- Actual is resolved at the point of instantiation. Create
6650 -- an identifier or operator with the same name as the formal.
6652 if Nkind (Formal_Sub) = N_Defining_Operator_Symbol then
6653 Nam := Make_Operator_Symbol (Loc,
6654 Chars => Chars (Formal_Sub),
6655 Strval => No_String);
6657 Nam := Make_Identifier (Loc, Chars (Formal_Sub));
6661 Error_Msg_Sloc := Sloc (Scope (Analyzed_S));
6663 ("missing actual&", Instantiation_Node, Formal_Sub);
6665 ("\in instantiation of & declared#",
6666 Instantiation_Node, Scope (Analyzed_S));
6667 Abandon_Instantiation (Instantiation_Node);
6671 Make_Subprogram_Renaming_Declaration (Loc,
6672 Specification => New_Spec,
6675 -- Gather possible interpretations for the actual before analyzing the
6676 -- instance. If overloaded, it will be resolved when analyzing the
6677 -- renaming declaration.
6679 if Box_Present (Formal)
6680 and then No (Actual)
6684 if Is_Child_Unit (Scope (Analyzed_S))
6685 and then Present (Entity (Nam))
6687 if not Is_Overloaded (Nam) then
6689 if From_Parent_Scope (Entity (Nam)) then
6690 Set_Is_Immediately_Visible (Entity (Nam), False);
6691 Set_Entity (Nam, Empty);
6692 Set_Etype (Nam, Empty);
6696 Set_Is_Immediately_Visible (Entity (Nam));
6705 Get_First_Interp (Nam, I, It);
6707 while Present (It.Nam) loop
6708 if From_Parent_Scope (It.Nam) then
6712 Get_Next_Interp (I, It);
6719 -- The generic instantiation freezes the actual. This can only be
6720 -- done once the actual is resolved, in the analysis of the renaming
6721 -- declaration. To indicate that must be done, we set the corresponding
6722 -- spec of the node to point to the formal subprogram entity.
6724 Set_Corresponding_Spec (Decl_Node, Analyzed_S);
6726 -- We cannot analyze the renaming declaration, and thus find the
6727 -- actual, until the all the actuals are assembled in the instance.
6728 -- For subsequent checks of other actuals, indicate the node that
6729 -- will hold the instance of this formal.
6731 Set_Instance_Of (Analyzed_S, Nam);
6733 if Nkind (Actual) = N_Selected_Component
6734 and then Is_Task_Type (Etype (Prefix (Actual)))
6735 and then not Is_Frozen (Etype (Prefix (Actual)))
6737 -- The renaming declaration will create a body, which must appear
6738 -- outside of the instantiation, We move the renaming declaration
6739 -- out of the instance, and create an additional renaming inside,
6740 -- to prevent freezing anomalies.
6743 Anon_Id : constant Entity_Id :=
6744 Make_Defining_Identifier
6745 (Loc, New_Internal_Name ('E'));
6747 Set_Defining_Unit_Name (New_Spec, Anon_Id);
6748 Insert_Before (Instantiation_Node, Decl_Node);
6749 Analyze (Decl_Node);
6751 -- Now create renaming within the instance
6754 Make_Subprogram_Renaming_Declaration (Loc,
6755 Specification => New_Copy_Tree (New_Spec),
6756 Name => New_Occurrence_Of (Anon_Id, Loc));
6758 Set_Defining_Unit_Name (Specification (Decl_Node),
6759 Make_Defining_Identifier (Loc, Chars (Formal_Sub)));
6764 end Instantiate_Formal_Subprogram;
6766 ------------------------
6767 -- Instantiate_Object --
6768 ------------------------
6770 function Instantiate_Object
6773 Analyzed_Formal : Node_Id)
6776 Formal_Id : constant Entity_Id := Defining_Identifier (Formal);
6777 Type_Id : constant Node_Id := Subtype_Mark (Formal);
6778 Loc : constant Source_Ptr := Sloc (Actual);
6779 Act_Assoc : constant Node_Id := Parent (Actual);
6780 Orig_Ftyp : constant Entity_Id :=
6781 Etype (Defining_Identifier (Analyzed_Formal));
6782 List : constant List_Id := New_List;
6784 Decl_Node : Node_Id;
6785 Subt_Decl : Node_Id := Empty;
6788 -- Sloc for error message on missing actual.
6789 Error_Msg_Sloc := Sloc (Scope (Defining_Identifier (Analyzed_Formal)));
6791 if Get_Instance_Of (Formal_Id) /= Formal_Id then
6792 Error_Msg_N ("duplicate instantiation of generic parameter", Actual);
6795 Set_Parent (List, Parent (Actual));
6799 if Out_Present (Formal) then
6801 -- An IN OUT generic actual must be a name. The instantiation is
6802 -- a renaming declaration. The actual is the name being renamed.
6803 -- We use the actual directly, rather than a copy, because it is not
6804 -- used further in the list of actuals, and because a copy or a use
6805 -- of relocate_node is incorrect if the instance is nested within
6806 -- a generic. In order to simplify ASIS searches, the Generic_Parent
6807 -- field links the declaration to the generic association.
6812 Instantiation_Node, Formal_Id);
6814 ("\in instantiation of & declared#",
6816 Scope (Defining_Identifier (Analyzed_Formal)));
6817 Abandon_Instantiation (Instantiation_Node);
6821 Make_Object_Renaming_Declaration (Loc,
6822 Defining_Identifier => New_Copy (Formal_Id),
6823 Subtype_Mark => New_Copy_Tree (Type_Id),
6826 Set_Corresponding_Generic_Association (Decl_Node, Act_Assoc);
6828 -- The analysis of the actual may produce insert_action nodes, so
6829 -- the declaration must have a context in which to attach them.
6831 Append (Decl_Node, List);
6834 -- This check is performed here because Analyze_Object_Renaming
6835 -- will not check it when Comes_From_Source is False. Note
6836 -- though that the check for the actual being the name of an
6837 -- object will be performed in Analyze_Object_Renaming.
6839 if Is_Object_Reference (Actual)
6840 and then Is_Dependent_Component_Of_Mutable_Object (Actual)
6843 ("illegal discriminant-dependent component for in out parameter",
6847 -- The actual has to be resolved in order to check that it is
6848 -- a variable (due to cases such as F(1), where F returns
6849 -- access to an array, and for overloaded prefixes).
6852 Get_Instance_Of (Etype (Defining_Identifier (Analyzed_Formal)));
6854 if Is_Private_Type (Ftyp)
6855 and then not Is_Private_Type (Etype (Actual))
6856 and then (Base_Type (Full_View (Ftyp)) = Base_Type (Etype (Actual))
6857 or else Base_Type (Etype (Actual)) = Ftyp)
6859 -- If the actual has the type of the full view of the formal,
6860 -- or else a non-private subtype of the formal, then
6861 -- the visibility of the formal type has changed. Add to the
6862 -- actuals a subtype declaration that will force the exchange
6863 -- of views in the body of the instance as well.
6866 Make_Subtype_Declaration (Loc,
6867 Defining_Identifier =>
6868 Make_Defining_Identifier (Loc, New_Internal_Name ('P')),
6869 Subtype_Indication => New_Occurrence_Of (Ftyp, Loc));
6871 Prepend (Subt_Decl, List);
6873 Append_Elmt (Full_View (Ftyp), Exchanged_Views);
6874 Exchange_Declarations (Ftyp);
6877 Resolve (Actual, Ftyp);
6879 if not Is_Variable (Actual) or else Paren_Count (Actual) > 0 then
6881 ("actual for& must be a variable", Actual, Formal_Id);
6883 elsif Base_Type (Ftyp) /= Base_Type (Etype (Actual)) then
6885 "type of actual does not match type of&", Actual, Formal_Id);
6889 Note_Possible_Modification (Actual);
6891 -- Check for instantiation of atomic/volatile actual for
6892 -- non-atomic/volatile formal (RM C.6 (12)).
6894 if Is_Atomic_Object (Actual)
6895 and then not Is_Atomic (Orig_Ftyp)
6898 ("cannot instantiate non-atomic formal object " &
6899 "with atomic actual", Actual);
6901 elsif Is_Volatile_Object (Actual)
6902 and then not Is_Volatile (Orig_Ftyp)
6905 ("cannot instantiate non-volatile formal object " &
6906 "with volatile actual", Actual);
6912 -- The instantiation of a generic formal in-parameter
6913 -- is a constant declaration. The actual is the expression for
6914 -- that declaration.
6916 if Present (Actual) then
6918 Decl_Node := Make_Object_Declaration (Loc,
6919 Defining_Identifier => New_Copy (Formal_Id),
6920 Constant_Present => True,
6921 Object_Definition => New_Copy_Tree (Type_Id),
6922 Expression => Actual);
6924 Set_Corresponding_Generic_Association (Decl_Node, Act_Assoc);
6926 -- A generic formal object of a tagged type is defined
6927 -- to be aliased so the new constant must also be treated
6931 (Etype (Defining_Identifier (Analyzed_Formal)))
6933 Set_Aliased_Present (Decl_Node);
6936 Append (Decl_Node, List);
6938 -- No need to repeat (pre-)analysis of some expression nodes
6939 -- already handled in Pre_Analyze_Actuals.
6941 if Nkind (Actual) /= N_Allocator then
6946 Typ : constant Entity_Id :=
6948 (Etype (Defining_Identifier (Analyzed_Formal)));
6951 Freeze_Before (Instantiation_Node, Typ);
6953 -- If the actual is an aggregate, perform name resolution
6954 -- on its components (the analysis of an aggregate does not
6955 -- do it) to capture local names that may be hidden if the
6956 -- generic is a child unit.
6958 if Nkind (Actual) = N_Aggregate then
6959 Pre_Analyze_And_Resolve (Actual, Typ);
6963 elsif Present (Expression (Formal)) then
6965 -- Use default to construct declaration.
6968 Make_Object_Declaration (Sloc (Formal),
6969 Defining_Identifier => New_Copy (Formal_Id),
6970 Constant_Present => True,
6971 Object_Definition => New_Copy (Type_Id),
6972 Expression => New_Copy_Tree (Expression (Formal)));
6974 Append (Decl_Node, List);
6975 Set_Analyzed (Expression (Decl_Node), False);
6980 Instantiation_Node, Formal_Id);
6981 Error_Msg_NE ("\in instantiation of & declared#",
6983 Scope (Defining_Identifier (Analyzed_Formal)));
6986 (Etype (Defining_Identifier (Analyzed_Formal)))
6988 -- Create dummy constant declaration so that instance can
6989 -- be analyzed, to minimize cascaded visibility errors.
6992 Make_Object_Declaration (Loc,
6993 Defining_Identifier => New_Copy (Formal_Id),
6994 Constant_Present => True,
6995 Object_Definition => New_Copy (Type_Id),
6997 Make_Attribute_Reference (Sloc (Formal_Id),
6998 Attribute_Name => Name_First,
6999 Prefix => New_Copy (Type_Id)));
7001 Append (Decl_Node, List);
7004 Abandon_Instantiation (Instantiation_Node);
7011 end Instantiate_Object;
7013 ------------------------------
7014 -- Instantiate_Package_Body --
7015 ------------------------------
7017 procedure Instantiate_Package_Body
7018 (Body_Info : Pending_Body_Info;
7019 Inlined_Body : Boolean := False)
7021 Act_Decl : constant Node_Id := Body_Info.Act_Decl;
7022 Inst_Node : constant Node_Id := Body_Info.Inst_Node;
7023 Loc : constant Source_Ptr := Sloc (Inst_Node);
7025 Gen_Id : constant Node_Id := Name (Inst_Node);
7026 Gen_Unit : constant Entity_Id := Get_Generic_Entity (Inst_Node);
7027 Gen_Decl : constant Node_Id := Unit_Declaration_Node (Gen_Unit);
7028 Act_Spec : constant Node_Id := Specification (Act_Decl);
7029 Act_Decl_Id : constant Entity_Id := Defining_Entity (Act_Spec);
7031 Act_Body_Name : Node_Id;
7033 Gen_Body_Id : Node_Id;
7035 Act_Body_Id : Entity_Id;
7037 Parent_Installed : Boolean := False;
7038 Save_Style_Check : constant Boolean := Style_Check;
7041 Gen_Body_Id := Corresponding_Body (Gen_Decl);
7043 -- The instance body may already have been processed, as the parent
7044 -- of another instance that is inlined. (Load_Parent_Of_Generic).
7046 if Present (Corresponding_Body (Instance_Spec (Inst_Node))) then
7050 Expander_Mode_Save_And_Set (Body_Info.Expander_Status);
7052 if No (Gen_Body_Id) then
7053 Load_Parent_Of_Generic (Inst_Node, Specification (Gen_Decl));
7054 Gen_Body_Id := Corresponding_Body (Gen_Decl);
7057 -- Establish global variable for sloc adjustment and for error
7060 Instantiation_Node := Inst_Node;
7062 if Present (Gen_Body_Id) then
7063 Save_Env (Gen_Unit, Act_Decl_Id);
7064 Style_Check := False;
7065 Current_Sem_Unit := Body_Info.Current_Sem_Unit;
7067 Gen_Body := Unit_Declaration_Node (Gen_Body_Id);
7069 Create_Instantiation_Source
7070 (Inst_Node, Gen_Body_Id, False, S_Adjustment);
7074 (Original_Node (Gen_Body), Empty, Instantiating => True);
7076 -- Build new name (possibly qualified) for body declaration
7078 Act_Body_Id := New_Copy (Act_Decl_Id);
7080 -- Some attributes of the spec entity are not inherited by the
7083 Set_Handler_Records (Act_Body_Id, No_List);
7085 if Nkind (Defining_Unit_Name (Act_Spec)) =
7086 N_Defining_Program_Unit_Name
7089 Make_Defining_Program_Unit_Name (Loc,
7090 Name => New_Copy_Tree (Name (Defining_Unit_Name (Act_Spec))),
7091 Defining_Identifier => Act_Body_Id);
7093 Act_Body_Name := Act_Body_Id;
7096 Set_Defining_Unit_Name (Act_Body, Act_Body_Name);
7098 Set_Corresponding_Spec (Act_Body, Act_Decl_Id);
7099 Check_Generic_Actuals (Act_Decl_Id, False);
7101 -- If it is a child unit, make the parent instance (which is an
7102 -- instance of the parent of the generic) visible. The parent
7103 -- instance is the prefix of the name of the generic unit.
7105 if Ekind (Scope (Gen_Unit)) = E_Generic_Package
7106 and then Nkind (Gen_Id) = N_Expanded_Name
7108 Install_Parent (Entity (Prefix (Gen_Id)), In_Body => True);
7109 Parent_Installed := True;
7111 elsif Is_Child_Unit (Gen_Unit) then
7112 Install_Parent (Scope (Gen_Unit), In_Body => True);
7113 Parent_Installed := True;
7116 -- If the instantiation is a library unit, and this is the main
7117 -- unit, then build the resulting compilation unit nodes for the
7118 -- instance. If this is a compilation unit but it is not the main
7119 -- unit, then it is the body of a unit in the context, that is being
7120 -- compiled because it is encloses some inlined unit or another
7121 -- generic unit being instantiated. In that case, this body is not
7122 -- part of the current compilation, and is not attached to the tree,
7123 -- but its parent must be set for analysis.
7125 if Nkind (Parent (Inst_Node)) = N_Compilation_Unit then
7127 -- Replace instance node with body of instance, and create
7128 -- new node for corresponding instance declaration.
7130 Build_Instance_Compilation_Unit_Nodes
7131 (Inst_Node, Act_Body, Act_Decl);
7132 Analyze (Inst_Node);
7134 if Parent (Inst_Node) = Cunit (Main_Unit) then
7136 -- If the instance is a child unit itself, then set the
7137 -- scope of the expanded body to be the parent of the
7138 -- instantiation (ensuring that the fully qualified name
7139 -- will be generated for the elaboration subprogram).
7141 if Nkind (Defining_Unit_Name (Act_Spec)) =
7142 N_Defining_Program_Unit_Name
7145 (Defining_Entity (Inst_Node), Scope (Act_Decl_Id));
7149 -- Case where instantiation is not a library unit
7152 -- If this is an early instantiation, i.e. appears textually
7153 -- before the corresponding body and must be elaborated first,
7154 -- indicate that the body instance is to be delayed.
7156 Install_Body (Act_Body, Inst_Node, Gen_Body, Gen_Decl);
7158 -- Now analyze the body. We turn off all checks if this is
7159 -- an internal unit, since there is no reason to have checks
7160 -- on for any predefined run-time library code. All such
7161 -- code is designed to be compiled with checks off.
7163 -- Note that we do NOT apply this criterion to children of
7164 -- GNAT (or on VMS, children of DEC). The latter units must
7165 -- suppress checks explicitly if this is needed.
7167 if Is_Predefined_File_Name
7168 (Unit_File_Name (Get_Source_Unit (Gen_Decl)))
7170 Analyze (Act_Body, Suppress => All_Checks);
7176 if not Generic_Separately_Compiled (Gen_Unit) then
7177 Inherit_Context (Gen_Body, Inst_Node);
7180 -- Remove the parent instances if they have been placed on the
7181 -- scope stack to compile the body.
7183 if Parent_Installed then
7184 Remove_Parent (In_Body => True);
7187 Restore_Private_Views (Act_Decl_Id);
7189 -- Remove the current unit from visibility if this is an instance
7190 -- that is not elaborated on the fly for inlining purposes.
7192 if not Inlined_Body then
7193 Set_Is_Immediately_Visible (Act_Decl_Id, False);
7197 Style_Check := Save_Style_Check;
7199 -- If we have no body, and the unit requires a body, then complain.
7200 -- This complaint is suppressed if we have detected other errors
7201 -- (since a common reason for missing the body is that it had errors).
7203 elsif Unit_Requires_Body (Gen_Unit) then
7204 if Serious_Errors_Detected = 0 then
7206 ("cannot find body of generic package &", Inst_Node, Gen_Unit);
7208 -- Don't attempt to perform any cleanup actions if some other
7209 -- error was aready detected, since this can cause blowups.
7215 -- Case of package that does not need a body
7218 -- If the instantiation of the declaration is a library unit,
7219 -- rewrite the original package instantiation as a package
7220 -- declaration in the compilation unit node.
7222 if Nkind (Parent (Inst_Node)) = N_Compilation_Unit then
7223 Set_Parent_Spec (Act_Decl, Parent_Spec (Inst_Node));
7224 Rewrite (Inst_Node, Act_Decl);
7226 -- Generate elaboration entity, in case spec has elaboration
7227 -- code. This cannot be done when the instance is analyzed,
7228 -- because it is not known yet whether the body exists.
7230 Set_Elaboration_Entity_Required (Act_Decl_Id, False);
7231 Build_Elaboration_Entity (Parent (Inst_Node), Act_Decl_Id);
7233 -- If the instantiation is not a library unit, then append the
7234 -- declaration to the list of implicitly generated entities.
7235 -- unless it is already a list member which means that it was
7236 -- already processed
7238 elsif not Is_List_Member (Act_Decl) then
7239 Mark_Rewrite_Insertion (Act_Decl);
7240 Insert_Before (Inst_Node, Act_Decl);
7244 Expander_Mode_Restore;
7245 end Instantiate_Package_Body;
7247 ---------------------------------
7248 -- Instantiate_Subprogram_Body --
7249 ---------------------------------
7251 procedure Instantiate_Subprogram_Body
7252 (Body_Info : Pending_Body_Info)
7254 Act_Decl : constant Node_Id := Body_Info.Act_Decl;
7255 Inst_Node : constant Node_Id := Body_Info.Inst_Node;
7256 Loc : constant Source_Ptr := Sloc (Inst_Node);
7257 Gen_Id : constant Node_Id := Name (Inst_Node);
7258 Gen_Unit : constant Entity_Id := Get_Generic_Entity (Inst_Node);
7259 Gen_Decl : constant Node_Id := Unit_Declaration_Node (Gen_Unit);
7260 Anon_Id : constant Entity_Id :=
7261 Defining_Unit_Name (Specification (Act_Decl));
7262 Pack_Id : constant Entity_Id :=
7263 Defining_Unit_Name (Parent (Act_Decl));
7266 Gen_Body_Id : Node_Id;
7268 Act_Body_Id : Entity_Id;
7269 Pack_Body : Node_Id;
7270 Prev_Formal : Entity_Id;
7272 Unit_Renaming : Node_Id;
7274 Parent_Installed : Boolean := False;
7275 Save_Style_Check : constant Boolean := Style_Check;
7278 Gen_Body_Id := Corresponding_Body (Gen_Decl);
7280 Expander_Mode_Save_And_Set (Body_Info.Expander_Status);
7282 if No (Gen_Body_Id) then
7283 Load_Parent_Of_Generic (Inst_Node, Specification (Gen_Decl));
7284 Gen_Body_Id := Corresponding_Body (Gen_Decl);
7287 Instantiation_Node := Inst_Node;
7289 if Present (Gen_Body_Id) then
7290 Gen_Body := Unit_Declaration_Node (Gen_Body_Id);
7292 if Nkind (Gen_Body) = N_Subprogram_Body_Stub then
7294 -- Either body is not present, or context is non-expanding, as
7295 -- when compiling a subunit. Mark the instance as completed.
7297 Set_Has_Completion (Anon_Id);
7301 Save_Env (Gen_Unit, Anon_Id);
7302 Style_Check := False;
7303 Current_Sem_Unit := Body_Info.Current_Sem_Unit;
7304 Create_Instantiation_Source
7312 (Original_Node (Gen_Body), Empty, Instantiating => True);
7313 Act_Body_Id := Defining_Entity (Act_Body);
7314 Set_Chars (Act_Body_Id, Chars (Anon_Id));
7315 Set_Sloc (Act_Body_Id, Sloc (Defining_Entity (Inst_Node)));
7316 Set_Corresponding_Spec (Act_Body, Anon_Id);
7317 Set_Has_Completion (Anon_Id);
7318 Check_Generic_Actuals (Pack_Id, False);
7320 -- If it is a child unit, make the parent instance (which is an
7321 -- instance of the parent of the generic) visible. The parent
7322 -- instance is the prefix of the name of the generic unit.
7324 if Ekind (Scope (Gen_Unit)) = E_Generic_Package
7325 and then Nkind (Gen_Id) = N_Expanded_Name
7327 Install_Parent (Entity (Prefix (Gen_Id)), In_Body => True);
7328 Parent_Installed := True;
7330 elsif Is_Child_Unit (Gen_Unit) then
7331 Install_Parent (Scope (Gen_Unit), In_Body => True);
7332 Parent_Installed := True;
7335 -- Inside its body, a reference to the generic unit is a reference
7336 -- to the instance. The corresponding renaming is the first
7337 -- declaration in the body.
7340 Make_Subprogram_Renaming_Declaration (Loc,
7343 Specification (Original_Node (Gen_Body)),
7345 Instantiating => True),
7346 Name => New_Occurrence_Of (Anon_Id, Loc));
7348 -- If there is a formal subprogram with the same name as the
7349 -- unit itself, do not add this renaming declaration. This is
7350 -- a temporary fix for one ACVC test. ???
7352 Prev_Formal := First_Entity (Pack_Id);
7353 while Present (Prev_Formal) loop
7354 if Chars (Prev_Formal) = Chars (Gen_Unit)
7355 and then Is_Overloadable (Prev_Formal)
7360 Next_Entity (Prev_Formal);
7363 if Present (Prev_Formal) then
7364 Decls := New_List (Act_Body);
7366 Decls := New_List (Unit_Renaming, Act_Body);
7369 -- The subprogram body is placed in the body of a dummy package
7370 -- body, whose spec contains the subprogram declaration as well
7371 -- as the renaming declarations for the generic parameters.
7373 Pack_Body := Make_Package_Body (Loc,
7374 Defining_Unit_Name => New_Copy (Pack_Id),
7375 Declarations => Decls);
7377 Set_Corresponding_Spec (Pack_Body, Pack_Id);
7379 -- If the instantiation is a library unit, then build resulting
7380 -- compilation unit nodes for the instance. The declaration of
7381 -- the enclosing package is the grandparent of the subprogram
7382 -- declaration. First replace the instantiation node as the unit
7383 -- of the corresponding compilation.
7385 if Nkind (Parent (Inst_Node)) = N_Compilation_Unit then
7386 if Parent (Inst_Node) = Cunit (Main_Unit) then
7387 Set_Unit (Parent (Inst_Node), Inst_Node);
7388 Build_Instance_Compilation_Unit_Nodes
7389 (Inst_Node, Pack_Body, Parent (Parent (Act_Decl)));
7390 Analyze (Inst_Node);
7392 Set_Parent (Pack_Body, Parent (Inst_Node));
7393 Analyze (Pack_Body);
7397 Insert_Before (Inst_Node, Pack_Body);
7398 Mark_Rewrite_Insertion (Pack_Body);
7399 Analyze (Pack_Body);
7401 if Expander_Active then
7402 Freeze_Subprogram_Body (Inst_Node, Gen_Body, Pack_Id);
7406 if not Generic_Separately_Compiled (Gen_Unit) then
7407 Inherit_Context (Gen_Body, Inst_Node);
7410 Restore_Private_Views (Pack_Id, False);
7412 if Parent_Installed then
7413 Remove_Parent (In_Body => True);
7417 Style_Check := Save_Style_Check;
7419 -- Body not found. Error was emitted already. If there were no
7420 -- previous errors, this may be an instance whose scope is a premature
7421 -- instance. In that case we must insure that the (legal) program does
7422 -- raise program error if executed. We generate a subprogram body for
7423 -- this purpose. See DEC ac30vso.
7425 elsif Serious_Errors_Detected = 0
7426 and then Nkind (Parent (Inst_Node)) /= N_Compilation_Unit
7428 if Ekind (Anon_Id) = E_Procedure then
7430 Make_Subprogram_Body (Loc,
7432 Make_Procedure_Specification (Loc,
7433 Defining_Unit_Name => New_Copy (Anon_Id),
7434 Parameter_Specifications =>
7436 (Parameter_Specifications (Parent (Anon_Id)))),
7438 Declarations => Empty_List,
7439 Handled_Statement_Sequence =>
7440 Make_Handled_Sequence_Of_Statements (Loc,
7443 Make_Raise_Program_Error (Loc,
7445 PE_Access_Before_Elaboration))));
7449 Make_Raise_Program_Error (Loc,
7450 Reason => PE_Access_Before_Elaboration);
7452 Set_Etype (Ret_Expr, (Etype (Anon_Id)));
7453 Set_Analyzed (Ret_Expr);
7456 Make_Subprogram_Body (Loc,
7458 Make_Function_Specification (Loc,
7459 Defining_Unit_Name => New_Copy (Anon_Id),
7460 Parameter_Specifications =>
7462 (Parameter_Specifications (Parent (Anon_Id))),
7464 New_Occurrence_Of (Etype (Anon_Id), Loc)),
7466 Declarations => Empty_List,
7467 Handled_Statement_Sequence =>
7468 Make_Handled_Sequence_Of_Statements (Loc,
7470 New_List (Make_Return_Statement (Loc, Ret_Expr))));
7473 Pack_Body := Make_Package_Body (Loc,
7474 Defining_Unit_Name => New_Copy (Pack_Id),
7475 Declarations => New_List (Act_Body));
7477 Insert_After (Inst_Node, Pack_Body);
7478 Set_Corresponding_Spec (Pack_Body, Pack_Id);
7479 Analyze (Pack_Body);
7482 Expander_Mode_Restore;
7483 end Instantiate_Subprogram_Body;
7485 ----------------------
7486 -- Instantiate_Type --
7487 ----------------------
7489 function Instantiate_Type
7492 Analyzed_Formal : Node_Id;
7493 Actual_Decls : List_Id)
7496 Loc : constant Source_Ptr := Sloc (Actual);
7497 Gen_T : constant Entity_Id := Defining_Identifier (Formal);
7498 A_Gen_T : constant Entity_Id := Defining_Identifier (Analyzed_Formal);
7499 Ancestor : Entity_Id := Empty;
7500 Def : constant Node_Id := Formal_Type_Definition (Formal);
7502 Decl_Node : Node_Id;
7504 procedure Validate_Array_Type_Instance;
7505 procedure Validate_Access_Subprogram_Instance;
7506 procedure Validate_Access_Type_Instance;
7507 procedure Validate_Derived_Type_Instance;
7508 procedure Validate_Private_Type_Instance;
7509 -- These procedures perform validation tests for the named case
7511 function Subtypes_Match (Gen_T, Act_T : Entity_Id) return Boolean;
7512 -- Check that base types are the same and that the subtypes match
7513 -- statically. Used in several of the above.
7515 --------------------
7516 -- Subtypes_Match --
7517 --------------------
7519 function Subtypes_Match (Gen_T, Act_T : Entity_Id) return Boolean is
7520 T : constant Entity_Id := Get_Instance_Of (Gen_T);
7523 return (Base_Type (T) = Base_Type (Act_T)
7524 -- why is the and then commented out here???
7525 -- and then Is_Constrained (T) = Is_Constrained (Act_T)
7526 and then Subtypes_Statically_Match (T, Act_T))
7528 or else (Is_Class_Wide_Type (Gen_T)
7529 and then Is_Class_Wide_Type (Act_T)
7532 Get_Instance_Of (Root_Type (Gen_T)),
7533 Root_Type (Act_T)));
7536 -----------------------------------------
7537 -- Validate_Access_Subprogram_Instance --
7538 -----------------------------------------
7540 procedure Validate_Access_Subprogram_Instance is
7542 if not Is_Access_Type (Act_T)
7543 or else Ekind (Designated_Type (Act_T)) /= E_Subprogram_Type
7546 ("expect access type in instantiation of &", Actual, Gen_T);
7547 Abandon_Instantiation (Actual);
7550 Check_Mode_Conformant
7551 (Designated_Type (Act_T),
7552 Designated_Type (A_Gen_T),
7556 if Ekind (Base_Type (Act_T)) = E_Access_Protected_Subprogram_Type then
7557 if Ekind (A_Gen_T) = E_Access_Subprogram_Type then
7559 ("protected access type not allowed for formal &",
7563 elsif Ekind (A_Gen_T) = E_Access_Protected_Subprogram_Type then
7565 ("expect protected access type for formal &",
7568 end Validate_Access_Subprogram_Instance;
7570 -----------------------------------
7571 -- Validate_Access_Type_Instance --
7572 -----------------------------------
7574 procedure Validate_Access_Type_Instance is
7575 Desig_Type : constant Entity_Id :=
7577 (Designated_Type (A_Gen_T), Scope (A_Gen_T));
7580 if not Is_Access_Type (Act_T) then
7582 ("expect access type in instantiation of &", Actual, Gen_T);
7583 Abandon_Instantiation (Actual);
7586 if Is_Access_Constant (A_Gen_T) then
7587 if not Is_Access_Constant (Act_T) then
7589 ("actual type must be access-to-constant type", Actual);
7590 Abandon_Instantiation (Actual);
7593 if Is_Access_Constant (Act_T) then
7595 ("actual type must be access-to-variable type", Actual);
7596 Abandon_Instantiation (Actual);
7598 elsif Ekind (A_Gen_T) = E_General_Access_Type
7599 and then Ekind (Base_Type (Act_T)) /= E_General_Access_Type
7601 Error_Msg_N ("actual must be general access type!", Actual);
7602 Error_Msg_NE ("add ALL to }!", Actual, Act_T);
7603 Abandon_Instantiation (Actual);
7607 -- The designated subtypes, that is to say the subtypes introduced
7608 -- by an access type declaration (and not by a subtype declaration)
7611 if not Subtypes_Match
7612 (Desig_Type, Designated_Type (Base_Type (Act_T)))
7615 ("designated type of actual does not match that of formal &",
7617 Abandon_Instantiation (Actual);
7619 elsif Is_Access_Type (Designated_Type (Act_T))
7620 and then Is_Constrained (Designated_Type (Designated_Type (Act_T)))
7622 Is_Constrained (Designated_Type (Desig_Type))
7625 ("designated type of actual does not match that of formal &",
7627 Abandon_Instantiation (Actual);
7629 end Validate_Access_Type_Instance;
7631 ----------------------------------
7632 -- Validate_Array_Type_Instance --
7633 ----------------------------------
7635 procedure Validate_Array_Type_Instance is
7640 function Formal_Dimensions return Int;
7641 -- Count number of dimensions in array type formal
7643 function Formal_Dimensions return Int is
7648 if Nkind (Def) = N_Constrained_Array_Definition then
7649 Index := First (Discrete_Subtype_Definitions (Def));
7651 Index := First (Subtype_Marks (Def));
7654 while Present (Index) loop
7660 end Formal_Dimensions;
7662 -- Start of processing for Validate_Array_Type_Instance
7665 if not Is_Array_Type (Act_T) then
7667 ("expect array type in instantiation of &", Actual, Gen_T);
7668 Abandon_Instantiation (Actual);
7670 elsif Nkind (Def) = N_Constrained_Array_Definition then
7671 if not (Is_Constrained (Act_T)) then
7673 ("expect constrained array in instantiation of &",
7675 Abandon_Instantiation (Actual);
7679 if Is_Constrained (Act_T) then
7681 ("expect unconstrained array in instantiation of &",
7683 Abandon_Instantiation (Actual);
7687 if Formal_Dimensions /= Number_Dimensions (Act_T) then
7689 ("dimensions of actual do not match formal &", Actual, Gen_T);
7690 Abandon_Instantiation (Actual);
7693 I1 := First_Index (A_Gen_T);
7694 I2 := First_Index (Act_T);
7695 for J in 1 .. Formal_Dimensions loop
7697 -- If the indices of the actual were given by a subtype_mark,
7698 -- the index was transformed into a range attribute. Retrieve
7699 -- the original type mark for checking.
7701 if Is_Entity_Name (Original_Node (I2)) then
7702 T2 := Entity (Original_Node (I2));
7707 if not Subtypes_Match
7708 (Find_Actual_Type (Etype (I1), Scope (A_Gen_T)), T2)
7711 ("index types of actual do not match those of formal &",
7713 Abandon_Instantiation (Actual);
7720 if not Subtypes_Match (
7721 Find_Actual_Type (Component_Type (A_Gen_T), Scope (A_Gen_T)),
7722 Component_Type (Act_T))
7725 ("component subtype of actual does not match that of formal &",
7727 Abandon_Instantiation (Actual);
7730 if Has_Aliased_Components (A_Gen_T)
7731 and then not Has_Aliased_Components (Act_T)
7734 ("actual must have aliased components to match formal type &",
7738 end Validate_Array_Type_Instance;
7740 ------------------------------------
7741 -- Validate_Derived_Type_Instance --
7742 ------------------------------------
7744 procedure Validate_Derived_Type_Instance is
7745 Actual_Discr : Entity_Id;
7746 Ancestor_Discr : Entity_Id;
7749 -- If the parent type in the generic declaration is itself
7750 -- a previous formal type, then it is local to the generic
7751 -- and absent from the analyzed generic definition. In that
7752 -- case the ancestor is the instance of the formal (which must
7753 -- have been instantiated previously), unless the ancestor is
7754 -- itself a formal derived type. In this latter case (which is the
7755 -- subject of Corrigendum 8652/0038 (AI-202) the ancestor of the
7756 -- formals is the ancestor of its parent. Otherwise, the analyzed
7757 -- generic carries the parent type. If the parent type is defined
7758 -- in a previous formal package, then the scope of that formal
7759 -- package is that of the generic type itself, and it has already
7760 -- been mapped into the corresponding type in the actual package.
7762 -- Common case: parent type defined outside of the generic
7764 if Is_Entity_Name (Subtype_Mark (Def))
7765 and then Present (Entity (Subtype_Mark (Def)))
7767 Ancestor := Get_Instance_Of (Entity (Subtype_Mark (Def)));
7769 -- Check whether parent is defined in a previous formal package
7772 Scope (Scope (Base_Type (Etype (A_Gen_T)))) = Scope (A_Gen_T)
7775 Get_Instance_Of (Base_Type (Etype (A_Gen_T)));
7777 -- The type may be a local derivation, or a type extension of
7778 -- a previous formal, or of a formal of a parent package.
7780 elsif Is_Derived_Type (Get_Instance_Of (A_Gen_T))
7782 Ekind (Get_Instance_Of (A_Gen_T)) = E_Record_Type_With_Private
7784 -- Check whether the parent is another derived formal type
7785 -- in the same generic unit.
7787 if Etype (A_Gen_T) /= A_Gen_T
7788 and then Is_Generic_Type (Etype (A_Gen_T))
7789 and then Scope (Etype (A_Gen_T)) = Scope (A_Gen_T)
7790 and then Etype (Etype (A_Gen_T)) /= Etype (A_Gen_T)
7792 -- Locate ancestor of parent from the subtype declaration
7793 -- created for the actual.
7799 Decl := First (Actual_Decls);
7800 while Present (Decl) loop
7801 if Nkind (Decl) = N_Subtype_Declaration
7802 and then Chars (Defining_Identifier (Decl)) =
7803 Chars (Etype (A_Gen_T))
7805 Ancestor := Generic_Parent_Type (Decl);
7813 pragma Assert (Present (Ancestor));
7817 Get_Instance_Of (Base_Type (Get_Instance_Of (A_Gen_T)));
7821 Ancestor := Get_Instance_Of (Etype (Base_Type (A_Gen_T)));
7824 if not Is_Ancestor (Base_Type (Ancestor), Act_T) then
7826 ("expect type derived from & in instantiation",
7827 Actual, First_Subtype (Ancestor));
7828 Abandon_Instantiation (Actual);
7831 -- Perform atomic/volatile checks (RM C.6(12))
7833 if Is_Atomic (Act_T) and then not Is_Atomic (Ancestor) then
7835 ("cannot have atomic actual type for non-atomic formal type",
7838 elsif Is_Volatile (Act_T)
7839 and then not Is_Volatile (Ancestor)
7840 and then Is_By_Reference_Type (Ancestor)
7843 ("cannot have volatile actual type for non-volatile formal type",
7847 -- It should not be necessary to check for unknown discriminants
7848 -- on Formal, but for some reason Has_Unknown_Discriminants is
7849 -- false for A_Gen_T, so Is_Indefinite_Subtype incorrectly
7850 -- returns False. This needs fixing. ???
7852 if not Is_Indefinite_Subtype (A_Gen_T)
7853 and then not Unknown_Discriminants_Present (Formal)
7854 and then Is_Indefinite_Subtype (Act_T)
7857 ("actual subtype must be constrained", Actual);
7858 Abandon_Instantiation (Actual);
7861 if not Unknown_Discriminants_Present (Formal) then
7862 if Is_Constrained (Ancestor) then
7863 if not Is_Constrained (Act_T) then
7865 ("actual subtype must be constrained", Actual);
7866 Abandon_Instantiation (Actual);
7869 -- Ancestor is unconstrained
7871 elsif Is_Constrained (Act_T) then
7872 if Ekind (Ancestor) = E_Access_Type
7873 or else Is_Composite_Type (Ancestor)
7876 ("actual subtype must be unconstrained", Actual);
7877 Abandon_Instantiation (Actual);
7880 -- A class-wide type is only allowed if the formal has
7881 -- unknown discriminants.
7883 elsif Is_Class_Wide_Type (Act_T)
7884 and then not Has_Unknown_Discriminants (Ancestor)
7887 ("actual for & cannot be a class-wide type", Actual, Gen_T);
7888 Abandon_Instantiation (Actual);
7890 -- Otherwise, the formal and actual shall have the same
7891 -- number of discriminants and each discriminant of the
7892 -- actual must correspond to a discriminant of the formal.
7894 elsif Has_Discriminants (Act_T)
7895 and then not Has_Unknown_Discriminants (Act_T)
7896 and then Has_Discriminants (Ancestor)
7898 Actual_Discr := First_Discriminant (Act_T);
7899 Ancestor_Discr := First_Discriminant (Ancestor);
7900 while Present (Actual_Discr)
7901 and then Present (Ancestor_Discr)
7903 if Base_Type (Act_T) /= Base_Type (Ancestor) and then
7904 not Present (Corresponding_Discriminant (Actual_Discr))
7907 ("discriminant & does not correspond " &
7908 "to ancestor discriminant", Actual, Actual_Discr);
7909 Abandon_Instantiation (Actual);
7912 Next_Discriminant (Actual_Discr);
7913 Next_Discriminant (Ancestor_Discr);
7916 if Present (Actual_Discr) or else Present (Ancestor_Discr) then
7918 ("actual for & must have same number of discriminants",
7920 Abandon_Instantiation (Actual);
7923 -- This case should be caught by the earlier check for
7924 -- for constrainedness, but the check here is added for
7927 elsif Has_Discriminants (Act_T)
7928 and then not Has_Unknown_Discriminants (Act_T)
7931 ("actual for & must not have discriminants", Actual, Gen_T);
7932 Abandon_Instantiation (Actual);
7934 elsif Has_Discriminants (Ancestor) then
7936 ("actual for & must have known discriminants", Actual, Gen_T);
7937 Abandon_Instantiation (Actual);
7940 if not Subtypes_Statically_Compatible (Act_T, Ancestor) then
7942 ("constraint on actual is incompatible with formal", Actual);
7943 Abandon_Instantiation (Actual);
7946 end Validate_Derived_Type_Instance;
7948 ------------------------------------
7949 -- Validate_Private_Type_Instance --
7950 ------------------------------------
7952 procedure Validate_Private_Type_Instance is
7953 Formal_Discr : Entity_Id;
7954 Actual_Discr : Entity_Id;
7955 Formal_Subt : Entity_Id;
7958 if Is_Limited_Type (Act_T)
7959 and then not Is_Limited_Type (A_Gen_T)
7962 ("actual for non-limited & cannot be a limited type", Actual,
7964 Explain_Limited_Type (Act_T, Actual);
7965 Abandon_Instantiation (Actual);
7967 elsif Is_Indefinite_Subtype (Act_T)
7968 and then not Is_Indefinite_Subtype (A_Gen_T)
7972 ("actual for & must be a definite subtype", Actual, Gen_T);
7974 elsif not Is_Tagged_Type (Act_T)
7975 and then Is_Tagged_Type (A_Gen_T)
7978 ("actual for & must be a tagged type", Actual, Gen_T);
7980 elsif Has_Discriminants (A_Gen_T) then
7981 if not Has_Discriminants (Act_T) then
7983 ("actual for & must have discriminants", Actual, Gen_T);
7984 Abandon_Instantiation (Actual);
7986 elsif Is_Constrained (Act_T) then
7988 ("actual for & must be unconstrained", Actual, Gen_T);
7989 Abandon_Instantiation (Actual);
7992 Formal_Discr := First_Discriminant (A_Gen_T);
7993 Actual_Discr := First_Discriminant (Act_T);
7994 while Formal_Discr /= Empty loop
7995 if Actual_Discr = Empty then
7997 ("discriminants on actual do not match formal",
7999 Abandon_Instantiation (Actual);
8002 Formal_Subt := Get_Instance_Of (Etype (Formal_Discr));
8004 -- access discriminants match if designated types do.
8006 if Ekind (Base_Type (Formal_Subt)) = E_Anonymous_Access_Type
8007 and then (Ekind (Base_Type (Etype (Actual_Discr))))
8008 = E_Anonymous_Access_Type
8009 and then Get_Instance_Of (
8010 Designated_Type (Base_Type (Formal_Subt)))
8011 = Designated_Type (Base_Type (Etype (Actual_Discr)))
8015 elsif Base_Type (Formal_Subt) /=
8016 Base_Type (Etype (Actual_Discr))
8019 ("types of actual discriminants must match formal",
8021 Abandon_Instantiation (Actual);
8023 elsif not Subtypes_Statically_Match
8024 (Formal_Subt, Etype (Actual_Discr))
8028 ("subtypes of actual discriminants must match formal",
8030 Abandon_Instantiation (Actual);
8033 Next_Discriminant (Formal_Discr);
8034 Next_Discriminant (Actual_Discr);
8037 if Actual_Discr /= Empty then
8039 ("discriminants on actual do not match formal",
8041 Abandon_Instantiation (Actual);
8048 end Validate_Private_Type_Instance;
8050 -- Start of processing for Instantiate_Type
8053 if Get_Instance_Of (A_Gen_T) /= A_Gen_T then
8054 Error_Msg_N ("duplicate instantiation of generic type", Actual);
8057 elsif not Is_Entity_Name (Actual)
8058 or else not Is_Type (Entity (Actual))
8061 ("expect valid subtype mark to instantiate &", Actual, Gen_T);
8062 Abandon_Instantiation (Actual);
8065 Act_T := Entity (Actual);
8067 -- Deal with fixed/floating restrictions
8069 if Is_Floating_Point_Type (Act_T) then
8070 Check_Restriction (No_Floating_Point, Actual);
8071 elsif Is_Fixed_Point_Type (Act_T) then
8072 Check_Restriction (No_Fixed_Point, Actual);
8075 -- Deal with error of using incomplete type as generic actual
8077 if Ekind (Act_T) = E_Incomplete_Type then
8078 if No (Underlying_Type (Act_T)) then
8079 Error_Msg_N ("premature use of incomplete type", Actual);
8080 Abandon_Instantiation (Actual);
8082 Act_T := Full_View (Act_T);
8083 Set_Entity (Actual, Act_T);
8085 if Has_Private_Component (Act_T) then
8087 ("premature use of type with private component", Actual);
8091 -- Deal with error of premature use of private type as generic actual
8093 elsif Is_Private_Type (Act_T)
8094 and then Is_Private_Type (Base_Type (Act_T))
8095 and then not Is_Generic_Type (Act_T)
8096 and then not Is_Derived_Type (Act_T)
8097 and then No (Full_View (Root_Type (Act_T)))
8099 Error_Msg_N ("premature use of private type", Actual);
8101 elsif Has_Private_Component (Act_T) then
8103 ("premature use of type with private component", Actual);
8106 Set_Instance_Of (A_Gen_T, Act_T);
8108 -- If the type is generic, the class-wide type may also be used
8110 if Is_Tagged_Type (A_Gen_T)
8111 and then Is_Tagged_Type (Act_T)
8112 and then not Is_Class_Wide_Type (A_Gen_T)
8114 Set_Instance_Of (Class_Wide_Type (A_Gen_T),
8115 Class_Wide_Type (Act_T));
8118 if not Is_Abstract (A_Gen_T)
8119 and then Is_Abstract (Act_T)
8122 ("actual of non-abstract formal cannot be abstract", Actual);
8125 if Is_Scalar_Type (Gen_T) then
8126 Set_Instance_Of (Etype (A_Gen_T), Etype (Act_T));
8131 when N_Formal_Private_Type_Definition =>
8132 Validate_Private_Type_Instance;
8134 when N_Formal_Derived_Type_Definition =>
8135 Validate_Derived_Type_Instance;
8137 when N_Formal_Discrete_Type_Definition =>
8138 if not Is_Discrete_Type (Act_T) then
8140 ("expect discrete type in instantiation of&", Actual, Gen_T);
8141 Abandon_Instantiation (Actual);
8144 when N_Formal_Signed_Integer_Type_Definition =>
8145 if not Is_Signed_Integer_Type (Act_T) then
8147 ("expect signed integer type in instantiation of&",
8149 Abandon_Instantiation (Actual);
8152 when N_Formal_Modular_Type_Definition =>
8153 if not Is_Modular_Integer_Type (Act_T) then
8155 ("expect modular type in instantiation of &", Actual, Gen_T);
8156 Abandon_Instantiation (Actual);
8159 when N_Formal_Floating_Point_Definition =>
8160 if not Is_Floating_Point_Type (Act_T) then
8162 ("expect float type in instantiation of &", Actual, Gen_T);
8163 Abandon_Instantiation (Actual);
8166 when N_Formal_Ordinary_Fixed_Point_Definition =>
8167 if not Is_Ordinary_Fixed_Point_Type (Act_T) then
8169 ("expect ordinary fixed point type in instantiation of &",
8171 Abandon_Instantiation (Actual);
8174 when N_Formal_Decimal_Fixed_Point_Definition =>
8175 if not Is_Decimal_Fixed_Point_Type (Act_T) then
8177 ("expect decimal type in instantiation of &",
8179 Abandon_Instantiation (Actual);
8182 when N_Array_Type_Definition =>
8183 Validate_Array_Type_Instance;
8185 when N_Access_To_Object_Definition =>
8186 Validate_Access_Type_Instance;
8188 when N_Access_Function_Definition |
8189 N_Access_Procedure_Definition =>
8190 Validate_Access_Subprogram_Instance;
8193 raise Program_Error;
8198 Make_Subtype_Declaration (Loc,
8199 Defining_Identifier => New_Copy (Gen_T),
8200 Subtype_Indication => New_Reference_To (Act_T, Loc));
8202 if Is_Private_Type (Act_T) then
8203 Set_Has_Private_View (Subtype_Indication (Decl_Node));
8205 elsif Is_Access_Type (Act_T)
8206 and then Is_Private_Type (Designated_Type (Act_T))
8208 Set_Has_Private_View (Subtype_Indication (Decl_Node));
8211 -- Flag actual derived types so their elaboration produces the
8212 -- appropriate renamings for the primitive operations of the ancestor.
8213 -- Flag actual for formal private types as well, to determine whether
8214 -- operations in the private part may override inherited operations.
8216 if Nkind (Def) = N_Formal_Derived_Type_Definition
8217 or else Nkind (Def) = N_Formal_Private_Type_Definition
8219 Set_Generic_Parent_Type (Decl_Node, Ancestor);
8223 end Instantiate_Type;
8225 ---------------------
8226 -- Is_In_Main_Unit --
8227 ---------------------
8229 function Is_In_Main_Unit (N : Node_Id) return Boolean is
8230 Unum : constant Unit_Number_Type := Get_Source_Unit (N);
8232 Current_Unit : Node_Id;
8235 if Unum = Main_Unit then
8238 -- If the current unit is a subunit then it is either the main unit
8239 -- or is being compiled as part of the main unit.
8241 elsif Nkind (N) = N_Compilation_Unit then
8242 return Nkind (Unit (N)) = N_Subunit;
8245 Current_Unit := Parent (N);
8246 while Present (Current_Unit)
8247 and then Nkind (Current_Unit) /= N_Compilation_Unit
8249 Current_Unit := Parent (Current_Unit);
8252 -- The instantiation node is in the main unit, or else the current
8253 -- node (perhaps as the result of nested instantiations) is in the
8254 -- main unit, or in the declaration of the main unit, which in this
8255 -- last case must be a body.
8257 return Unum = Main_Unit
8258 or else Current_Unit = Cunit (Main_Unit)
8259 or else Current_Unit = Library_Unit (Cunit (Main_Unit))
8260 or else (Present (Library_Unit (Current_Unit))
8261 and then Is_In_Main_Unit (Library_Unit (Current_Unit)));
8262 end Is_In_Main_Unit;
8264 ----------------------------
8265 -- Load_Parent_Of_Generic --
8266 ----------------------------
8268 procedure Load_Parent_Of_Generic (N : Node_Id; Spec : Node_Id) is
8269 Comp_Unit : constant Node_Id := Cunit (Get_Source_Unit (Spec));
8270 Save_Style_Check : constant Boolean := Style_Check;
8271 True_Parent : Node_Id;
8272 Inst_Node : Node_Id;
8276 if not In_Same_Source_Unit (N, Spec)
8277 or else Nkind (Unit (Comp_Unit)) = N_Package_Declaration
8278 or else (Nkind (Unit (Comp_Unit)) = N_Package_Body
8279 and then not Is_In_Main_Unit (Spec))
8281 -- Find body of parent of spec, and analyze it. A special case
8282 -- arises when the parent is an instantiation, that is to say when
8283 -- we are currently instantiating a nested generic. In that case,
8284 -- there is no separate file for the body of the enclosing instance.
8285 -- Instead, the enclosing body must be instantiated as if it were
8286 -- a pending instantiation, in order to produce the body for the
8287 -- nested generic we require now. Note that in that case the
8288 -- generic may be defined in a package body, the instance defined
8289 -- in the same package body, and the original enclosing body may not
8290 -- be in the main unit.
8292 True_Parent := Parent (Spec);
8295 while Present (True_Parent)
8296 and then Nkind (True_Parent) /= N_Compilation_Unit
8298 if Nkind (True_Parent) = N_Package_Declaration
8300 Nkind (Original_Node (True_Parent)) = N_Package_Instantiation
8302 -- Parent is a compilation unit that is an instantiation.
8303 -- Instantiation node has been replaced with package decl.
8305 Inst_Node := Original_Node (True_Parent);
8308 elsif Nkind (True_Parent) = N_Package_Declaration
8309 and then Present (Generic_Parent (Specification (True_Parent)))
8310 and then Nkind (Parent (True_Parent)) /= N_Compilation_Unit
8312 -- Parent is an instantiation within another specification.
8313 -- Declaration for instance has been inserted before original
8314 -- instantiation node. A direct link would be preferable?
8316 Inst_Node := Next (True_Parent);
8318 while Present (Inst_Node)
8319 and then Nkind (Inst_Node) /= N_Package_Instantiation
8324 -- If the instance appears within a generic, and the generic
8325 -- unit is defined within a formal package of the enclosing
8326 -- generic, there is no generic body available, and none
8327 -- needed. A more precise test should be used ???
8329 if No (Inst_Node) then
8335 True_Parent := Parent (True_Parent);
8339 -- Case where we are currently instantiating a nested generic
8341 if Present (Inst_Node) then
8342 if Nkind (Parent (True_Parent)) = N_Compilation_Unit then
8344 -- Instantiation node and declaration of instantiated package
8345 -- were exchanged when only the declaration was needed.
8346 -- Restore instantiation node before proceeding with body.
8348 Set_Unit (Parent (True_Parent), Inst_Node);
8351 -- Now complete instantiation of enclosing body, if it appears
8352 -- in some other unit. If it appears in the current unit, the
8353 -- body will have been instantiated already.
8355 if No (Corresponding_Body (Instance_Spec (Inst_Node))) then
8357 -- We need to determine the expander mode to instantiate
8358 -- the enclosing body. Because the generic body we need
8359 -- may use global entities declared in the enclosing package
8360 -- (including aggregates) it is in general necessary to
8361 -- compile this body with expansion enabled. The exception
8362 -- is if we are within a generic package, in which case
8363 -- the usual generic rule applies.
8366 Exp_Status : Boolean := True;
8370 -- Loop through scopes looking for generic package
8372 Scop := Scope (Defining_Entity (Instance_Spec (Inst_Node)));
8373 while Present (Scop)
8374 and then Scop /= Standard_Standard
8376 if Ekind (Scop) = E_Generic_Package then
8377 Exp_Status := False;
8381 Scop := Scope (Scop);
8384 Instantiate_Package_Body
8385 (Pending_Body_Info'(
8386 Inst_Node, True_Parent, Exp_Status,
8387 Get_Code_Unit (Sloc (Inst_Node))));
8391 -- Case where we are not instantiating a nested generic
8394 Opt.Style_Check := False;
8395 Expander_Mode_Save_And_Set (True);
8396 Load_Needed_Body (Comp_Unit, OK);
8397 Opt.Style_Check := Save_Style_Check;
8398 Expander_Mode_Restore;
8401 and then Unit_Requires_Body (Defining_Entity (Spec))
8404 Bname : constant Unit_Name_Type :=
8405 Get_Body_Name (Get_Unit_Name (Unit (Comp_Unit)));
8408 Error_Msg_Unit_1 := Bname;
8409 Error_Msg_N ("this instantiation requires$!", N);
8411 Get_File_Name (Bname, Subunit => False);
8412 Error_Msg_N ("\but file{ was not found!", N);
8413 raise Unrecoverable_Error;
8419 -- If loading the parent of the generic caused an instantiation
8420 -- circularity, we abandon compilation at this point, because
8421 -- otherwise in some cases we get into trouble with infinite
8422 -- recursions after this point.
8424 if Circularity_Detected then
8425 raise Unrecoverable_Error;
8427 end Load_Parent_Of_Generic;
8429 -----------------------
8430 -- Move_Freeze_Nodes --
8431 -----------------------
8433 procedure Move_Freeze_Nodes
8434 (Out_Of : Entity_Id;
8439 Next_Decl : Node_Id;
8440 Next_Node : Node_Id := After;
8443 function Is_Outer_Type (T : Entity_Id) return Boolean;
8444 -- Check whether entity is declared in a scope external to that
8445 -- of the generic unit.
8451 function Is_Outer_Type (T : Entity_Id) return Boolean is
8452 Scop : Entity_Id := Scope (T);
8455 if Scope_Depth (Scop) < Scope_Depth (Out_Of) then
8459 while Scop /= Standard_Standard loop
8461 if Scop = Out_Of then
8464 Scop := Scope (Scop);
8472 -- Start of processing for Move_Freeze_Nodes
8479 -- First remove the freeze nodes that may appear before all other
8483 while Present (Decl)
8484 and then Nkind (Decl) = N_Freeze_Entity
8485 and then Is_Outer_Type (Entity (Decl))
8487 Decl := Remove_Head (L);
8488 Insert_After (Next_Node, Decl);
8489 Set_Analyzed (Decl, False);
8494 -- Next scan the list of declarations and remove each freeze node that
8495 -- appears ahead of the current node.
8497 while Present (Decl) loop
8498 while Present (Next (Decl))
8499 and then Nkind (Next (Decl)) = N_Freeze_Entity
8500 and then Is_Outer_Type (Entity (Next (Decl)))
8502 Next_Decl := Remove_Next (Decl);
8503 Insert_After (Next_Node, Next_Decl);
8504 Set_Analyzed (Next_Decl, False);
8505 Next_Node := Next_Decl;
8508 -- If the declaration is a nested package or concurrent type, then
8509 -- recurse. Nested generic packages will have been processed from the
8512 if Nkind (Decl) = N_Package_Declaration then
8513 Spec := Specification (Decl);
8515 elsif Nkind (Decl) = N_Task_Type_Declaration then
8516 Spec := Task_Definition (Decl);
8518 elsif Nkind (Decl) = N_Protected_Type_Declaration then
8519 Spec := Protected_Definition (Decl);
8525 if Present (Spec) then
8526 Move_Freeze_Nodes (Out_Of, Next_Node,
8527 Visible_Declarations (Spec));
8528 Move_Freeze_Nodes (Out_Of, Next_Node,
8529 Private_Declarations (Spec));
8534 end Move_Freeze_Nodes;
8540 function Next_Assoc (E : Assoc_Ptr) return Assoc_Ptr is
8542 return Generic_Renamings.Table (E).Next_In_HTable;
8545 ------------------------
8546 -- Preanalyze_Actuals --
8547 ------------------------
8549 procedure Pre_Analyze_Actuals (N : Node_Id) is
8552 Errs : constant Int := Serious_Errors_Detected;
8555 Assoc := First (Generic_Associations (N));
8557 while Present (Assoc) loop
8558 Act := Explicit_Generic_Actual_Parameter (Assoc);
8560 -- Within a nested instantiation, a defaulted actual is an
8561 -- empty association, so nothing to analyze. If the actual for
8562 -- a subprogram is an attribute, analyze prefix only, because
8563 -- actual is not a complete attribute reference.
8565 -- If actual is an allocator, analyze expression only. The full
8566 -- analysis can generate code, and if the instance is a compilation
8567 -- unit we have to wait until the package instance is installed to
8568 -- have a proper place to insert this code.
8570 -- String literals may be operators, but at this point we do not
8571 -- know whether the actual is a formal subprogram or a string.
8576 elsif Nkind (Act) = N_Attribute_Reference then
8577 Analyze (Prefix (Act));
8579 elsif Nkind (Act) = N_Explicit_Dereference then
8580 Analyze (Prefix (Act));
8582 elsif Nkind (Act) = N_Allocator then
8584 Expr : constant Node_Id := Expression (Act);
8587 if Nkind (Expr) = N_Subtype_Indication then
8588 Analyze (Subtype_Mark (Expr));
8589 Analyze_List (Constraints (Constraint (Expr)));
8595 elsif Nkind (Act) /= N_Operator_Symbol then
8599 if Errs /= Serious_Errors_Detected then
8600 Abandon_Instantiation (Act);
8605 end Pre_Analyze_Actuals;
8611 procedure Remove_Parent (In_Body : Boolean := False) is
8612 S : Entity_Id := Current_Scope;
8618 -- After child instantiation is complete, remove from scope stack
8619 -- the extra copy of the current scope, and then remove parent
8625 while Current_Scope /= S loop
8627 End_Package_Scope (Current_Scope);
8629 if In_Open_Scopes (P) then
8630 E := First_Entity (P);
8632 while Present (E) loop
8633 Set_Is_Immediately_Visible (E, True);
8637 if Is_Generic_Instance (Current_Scope)
8638 and then P /= Current_Scope
8640 -- We are within an instance of some sibling. Retain
8641 -- visibility of parent, for proper subsequent cleanup.
8643 Set_In_Private_Part (P);
8646 elsif not In_Open_Scopes (Scope (P)) then
8647 Set_Is_Immediately_Visible (P, False);
8651 -- Reset visibility of entities in the enclosing scope.
8653 Set_Is_Hidden_Open_Scope (Current_Scope, False);
8654 Hidden := First_Elmt (Hidden_Entities);
8656 while Present (Hidden) loop
8657 Set_Is_Immediately_Visible (Node (Hidden), True);
8662 -- Each body is analyzed separately, and there is no context
8663 -- that needs preserving from one body instance to the next,
8664 -- so remove all parent scopes that have been installed.
8666 while Present (S) loop
8667 End_Package_Scope (S);
8668 Set_Is_Immediately_Visible (S, False);
8670 exit when S = Standard_Standard;
8680 procedure Restore_Env is
8681 Saved : Instance_Env renames Instance_Envs.Table (Instance_Envs.Last);
8684 Ada_83 := Saved.Ada_83;
8686 if No (Current_Instantiated_Parent.Act_Id) then
8688 -- Restore environment after subprogram inlining
8690 Restore_Private_Views (Empty);
8693 Current_Instantiated_Parent := Saved.Instantiated_Parent;
8694 Exchanged_Views := Saved.Exchanged_Views;
8695 Hidden_Entities := Saved.Hidden_Entities;
8696 Current_Sem_Unit := Saved.Current_Sem_Unit;
8698 Instance_Envs.Decrement_Last;
8701 ---------------------------
8702 -- Restore_Private_Views --
8703 ---------------------------
8705 procedure Restore_Private_Views
8706 (Pack_Id : Entity_Id;
8707 Is_Package : Boolean := True)
8716 M := First_Elmt (Exchanged_Views);
8717 while Present (M) loop
8720 -- Subtypes of types whose views have been exchanged, and that
8721 -- are defined within the instance, were not on the list of
8722 -- Private_Dependents on entry to the instance, so they have to
8723 -- be exchanged explicitly now, in order to remain consistent with
8724 -- the view of the parent type.
8726 if Ekind (Typ) = E_Private_Type
8727 or else Ekind (Typ) = E_Limited_Private_Type
8728 or else Ekind (Typ) = E_Record_Type_With_Private
8730 Dep_Elmt := First_Elmt (Private_Dependents (Typ));
8732 while Present (Dep_Elmt) loop
8733 Dep_Typ := Node (Dep_Elmt);
8735 if Scope (Dep_Typ) = Pack_Id
8736 and then Present (Full_View (Dep_Typ))
8738 Replace_Elmt (Dep_Elmt, Full_View (Dep_Typ));
8739 Exchange_Declarations (Dep_Typ);
8742 Next_Elmt (Dep_Elmt);
8746 Exchange_Declarations (Node (M));
8750 if No (Pack_Id) then
8754 -- Make the generic formal parameters private, and make the formal
8755 -- types into subtypes of the actuals again.
8757 E := First_Entity (Pack_Id);
8759 while Present (E) loop
8760 Set_Is_Hidden (E, True);
8763 and then Nkind (Parent (E)) = N_Subtype_Declaration
8765 Set_Is_Generic_Actual_Type (E, False);
8767 -- An unusual case of aliasing: the actual may also be directly
8768 -- visible in the generic, and be private there, while it is
8769 -- fully visible in the context of the instance. The internal
8770 -- subtype is private in the instance, but has full visibility
8771 -- like its parent in the enclosing scope. This enforces the
8772 -- invariant that the privacy status of all private dependents of
8773 -- a type coincide with that of the parent type. This can only
8774 -- happen when a generic child unit is instantiated within a
8777 if Is_Private_Type (E)
8778 and then not Is_Private_Type (Etype (E))
8780 Exchange_Declarations (E);
8783 elsif Ekind (E) = E_Package then
8785 -- The end of the renaming list is the renaming of the generic
8786 -- package itself. If the instance is a subprogram, all entities
8787 -- in the corresponding package are renamings. If this entity is
8788 -- a formal package, make its own formals private as well. The
8789 -- actual in this case is itself the renaming of an instantation.
8790 -- If the entity is not a package renaming, it is the entity
8791 -- created to validate formal package actuals: ignore.
8793 -- If the actual is itself a formal package for the enclosing
8794 -- generic, or the actual for such a formal package, it remains
8795 -- visible after the current instance, and therefore nothing
8796 -- needs to be done either, except to keep it accessible.
8799 and then Renamed_Object (E) = Pack_Id
8803 elsif Nkind (Parent (E)) /= N_Package_Renaming_Declaration then
8806 elsif Denotes_Formal_Package (Renamed_Object (E)) then
8807 Set_Is_Hidden (E, False);
8811 Act_P : constant Entity_Id := Renamed_Object (E);
8815 Id := First_Entity (Act_P);
8817 and then Id /= First_Private_Entity (Act_P)
8819 Set_Is_Hidden (Id, True);
8820 Set_Is_Potentially_Use_Visible (Id, In_Use (Act_P));
8821 exit when Ekind (Id) = E_Package
8822 and then Renamed_Object (Id) = Act_P;
8833 end Restore_Private_Views;
8840 (Gen_Unit : Entity_Id;
8841 Act_Unit : Entity_Id)
8845 Set_Instance_Env (Gen_Unit, Act_Unit);
8848 ----------------------------
8849 -- Save_Global_References --
8850 ----------------------------
8852 procedure Save_Global_References (N : Node_Id) is
8853 Gen_Scope : Entity_Id;
8857 function Is_Global (E : Entity_Id) return Boolean;
8858 -- Check whether entity is defined outside of generic unit.
8859 -- Examine the scope of an entity, and the scope of the scope,
8860 -- etc, until we find either Standard, in which case the entity
8861 -- is global, or the generic unit itself, which indicates that
8862 -- the entity is local. If the entity is the generic unit itself,
8863 -- as in the case of a recursive call, or the enclosing generic unit,
8864 -- if different from the current scope, then it is local as well,
8865 -- because it will be replaced at the point of instantiation. On
8866 -- the other hand, if it is a reference to a child unit of a common
8867 -- ancestor, which appears in an instantiation, it is global because
8868 -- it is used to denote a specific compilation unit at the time the
8869 -- instantiations will be analyzed.
8871 procedure Reset_Entity (N : Node_Id);
8872 -- Save semantic information on global entity, so that it is not
8873 -- resolved again at instantiation time.
8875 procedure Save_Entity_Descendants (N : Node_Id);
8876 -- Apply Save_Global_References to the two syntactic descendants of
8877 -- non-terminal nodes that carry an Associated_Node and are processed
8878 -- through Reset_Entity. Once the global entity (if any) has been
8879 -- captured together with its type, only two syntactic descendants
8880 -- need to be traversed to complete the processing of the tree rooted
8881 -- at N. This applies to Selected_Components, Expanded_Names, and to
8882 -- Operator nodes. N can also be a character literal, identifier, or
8883 -- operator symbol node, but the call has no effect in these cases.
8885 procedure Save_Global_Defaults (N1, N2 : Node_Id);
8886 -- Default actuals in nested instances must be handled specially
8887 -- because there is no link to them from the original tree. When an
8888 -- actual subprogram is given by a default, we add an explicit generic
8889 -- association for it in the instantiation node. When we save the
8890 -- global references on the name of the instance, we recover the list
8891 -- of generic associations, and add an explicit one to the original
8892 -- generic tree, through which a global actual can be preserved.
8893 -- Similarly, if a child unit is instantiated within a sibling, in the
8894 -- context of the parent, we must preserve the identifier of the parent
8895 -- so that it can be properly resolved in a subsequent instantiation.
8897 procedure Save_Global_Descendant (D : Union_Id);
8898 -- Apply Save_Global_References recursively to the descendents of
8901 procedure Save_References (N : Node_Id);
8902 -- This is the recursive procedure that does the work, once the
8903 -- enclosing generic scope has been established.
8909 function Is_Global (E : Entity_Id) return Boolean is
8910 Se : Entity_Id := Scope (E);
8912 function Is_Instance_Node (Decl : Node_Id) return Boolean;
8913 -- Determine whether the parent node of a reference to a child unit
8914 -- denotes an instantiation or a formal package, in which case the
8915 -- reference to the child unit is global, even if it appears within
8916 -- the current scope (e.g. when the instance appears within the body
8919 function Is_Instance_Node (Decl : Node_Id) return Boolean is
8921 return (Nkind (Decl) in N_Generic_Instantiation
8923 Nkind (Original_Node (Decl)) = N_Formal_Package_Declaration);
8924 end Is_Instance_Node;
8926 -- Start of processing for Is_Global
8929 if E = Gen_Scope then
8932 elsif E = Standard_Standard then
8935 elsif Is_Child_Unit (E)
8936 and then (Is_Instance_Node (Parent (N2))
8937 or else (Nkind (Parent (N2)) = N_Expanded_Name
8938 and then N2 = Selector_Name (Parent (N2))
8939 and then Is_Instance_Node (Parent (Parent (N2)))))
8944 while Se /= Gen_Scope loop
8945 if Se = Standard_Standard then
8960 procedure Reset_Entity (N : Node_Id) is
8962 procedure Set_Global_Type (N : Node_Id; N2 : Node_Id);
8963 -- The type of N2 is global to the generic unit. Save the
8964 -- type in the generic node.
8966 function Top_Ancestor (E : Entity_Id) return Entity_Id;
8967 -- Find the ultimate ancestor of the current unit. If it is
8968 -- not a generic unit, then the name of the current unit
8969 -- in the prefix of an expanded name must be replaced with
8970 -- its generic homonym to ensure that it will be properly
8971 -- resolved in an instance.
8973 ---------------------
8974 -- Set_Global_Type --
8975 ---------------------
8977 procedure Set_Global_Type (N : Node_Id; N2 : Node_Id) is
8978 Typ : constant Entity_Id := Etype (N2);
8984 and then Has_Private_View (Entity (N))
8986 -- If the entity of N is not the associated node, this is
8987 -- a nested generic and it has an associated node as well,
8988 -- whose type is already the full view (see below). Indicate
8989 -- that the original node has a private view.
8991 Set_Has_Private_View (N);
8994 -- If not a private type, nothing else to do
8996 if not Is_Private_Type (Typ) then
8997 if Is_Array_Type (Typ)
8998 and then Is_Private_Type (Component_Type (Typ))
9000 Set_Has_Private_View (N);
9003 -- If it is a derivation of a private type in a context where
9004 -- no full view is needed, nothing to do either.
9006 elsif No (Full_View (Typ)) and then Typ /= Etype (Typ) then
9009 -- Otherwise mark the type for flipping and use the full_view
9013 Set_Has_Private_View (N);
9015 if Present (Full_View (Typ)) then
9016 Set_Etype (N2, Full_View (Typ));
9019 end Set_Global_Type;
9025 function Top_Ancestor (E : Entity_Id) return Entity_Id is
9026 Par : Entity_Id := E;
9029 while Is_Child_Unit (Par) loop
9036 -- Start of processing for Reset_Entity
9039 N2 := Get_Associated_Node (N);
9043 if Is_Global (E) then
9044 Set_Global_Type (N, N2);
9046 elsif Nkind (N) = N_Op_Concat
9047 and then Is_Generic_Type (Etype (N2))
9049 (Base_Type (Etype (Right_Opnd (N2))) = Etype (N2)
9050 or else Base_Type (Etype (Left_Opnd (N2))) = Etype (N2))
9051 and then Is_Intrinsic_Subprogram (E)
9056 -- Entity is local. Mark generic node as unresolved.
9057 -- Note that now it does not have an entity.
9059 Set_Associated_Node (N, Empty);
9060 Set_Etype (N, Empty);
9063 if (Nkind (Parent (N)) = N_Package_Instantiation
9064 or else Nkind (Parent (N)) = N_Function_Instantiation
9065 or else Nkind (Parent (N)) = N_Procedure_Instantiation)
9066 and then N = Name (Parent (N))
9068 Save_Global_Defaults (Parent (N), Parent (N2));
9071 elsif Nkind (Parent (N)) = N_Selected_Component
9072 and then Nkind (Parent (N2)) = N_Expanded_Name
9075 if Is_Global (Entity (Parent (N2))) then
9076 Change_Selected_Component_To_Expanded_Name (Parent (N));
9077 Set_Associated_Node (Parent (N), Parent (N2));
9078 Set_Global_Type (Parent (N), Parent (N2));
9079 Save_Entity_Descendants (N);
9081 -- If this is a reference to the current generic entity,
9082 -- replace by the name of the generic homonym of the current
9083 -- package. This is because in an instantiation Par.P.Q will
9084 -- not resolve to the name of the instance, whose enclosing
9085 -- scope is not necessarily Par. We use the generic homonym
9086 -- rather that the name of the generic itself, because it may
9087 -- be hidden by a local declaration.
9089 elsif In_Open_Scopes (Entity (Parent (N2)))
9091 Is_Generic_Unit (Top_Ancestor (Entity (Prefix (Parent (N2)))))
9093 if Ekind (Entity (Parent (N2))) = E_Generic_Package then
9094 Rewrite (Parent (N),
9095 Make_Identifier (Sloc (N),
9097 Chars (Generic_Homonym (Entity (Parent (N2))))));
9099 Rewrite (Parent (N),
9100 Make_Identifier (Sloc (N),
9101 Chars => Chars (Selector_Name (Parent (N2)))));
9105 if (Nkind (Parent (Parent (N))) = N_Package_Instantiation
9106 or else Nkind (Parent (Parent (N)))
9107 = N_Function_Instantiation
9108 or else Nkind (Parent (Parent (N)))
9109 = N_Procedure_Instantiation)
9110 and then Parent (N) = Name (Parent (Parent (N)))
9112 Save_Global_Defaults
9113 (Parent (Parent (N)), Parent (Parent ((N2))));
9116 -- A selected component may denote a static constant that has
9117 -- been folded. Make the same replacement in original tree.
9119 elsif Nkind (Parent (N)) = N_Selected_Component
9120 and then (Nkind (Parent (N2)) = N_Integer_Literal
9121 or else Nkind (Parent (N2)) = N_Real_Literal)
9123 Rewrite (Parent (N),
9124 New_Copy (Parent (N2)));
9125 Set_Analyzed (Parent (N), False);
9127 -- A selected component may be transformed into a parameterless
9128 -- function call. If the called entity is global, rewrite the
9129 -- node appropriately, i.e. as an extended name for the global
9132 elsif Nkind (Parent (N)) = N_Selected_Component
9133 and then Nkind (Parent (N2)) = N_Function_Call
9134 and then Is_Global (Entity (Name (Parent (N2))))
9136 Change_Selected_Component_To_Expanded_Name (Parent (N));
9137 Set_Associated_Node (Parent (N), Name (Parent (N2)));
9138 Set_Global_Type (Parent (N), Name (Parent (N2)));
9139 Save_Entity_Descendants (N);
9142 -- Entity is local. Reset in generic unit, so that node
9143 -- is resolved anew at the point of instantiation.
9145 Set_Associated_Node (N, Empty);
9146 Set_Etype (N, Empty);
9150 -----------------------------
9151 -- Save_Entity_Descendants --
9152 -----------------------------
9154 procedure Save_Entity_Descendants (N : Node_Id) is
9158 Save_Global_Descendant (Union_Id (Left_Opnd (N)));
9159 Save_Global_Descendant (Union_Id (Right_Opnd (N)));
9162 Save_Global_Descendant (Union_Id (Right_Opnd (N)));
9164 when N_Expanded_Name | N_Selected_Component =>
9165 Save_Global_Descendant (Union_Id (Prefix (N)));
9166 Save_Global_Descendant (Union_Id (Selector_Name (N)));
9168 when N_Identifier | N_Character_Literal | N_Operator_Symbol =>
9172 raise Program_Error;
9174 end Save_Entity_Descendants;
9176 --------------------------
9177 -- Save_Global_Defaults --
9178 --------------------------
9180 procedure Save_Global_Defaults (N1, N2 : Node_Id) is
9181 Loc : constant Source_Ptr := Sloc (N1);
9182 Assoc2 : constant List_Id := Generic_Associations (N2);
9183 Gen_Id : constant Entity_Id := Get_Generic_Entity (N2);
9193 Assoc1 := Generic_Associations (N1);
9195 if Present (Assoc1) then
9196 Act1 := First (Assoc1);
9199 Set_Generic_Associations (N1, New_List);
9200 Assoc1 := Generic_Associations (N1);
9203 if Present (Assoc2) then
9204 Act2 := First (Assoc2);
9209 while Present (Act1) and then Present (Act2) loop
9214 -- Find the associations added for default suprograms.
9216 if Present (Act2) then
9217 while Nkind (Act2) /= N_Generic_Association
9218 or else No (Entity (Selector_Name (Act2)))
9219 or else not Is_Overloadable (Entity (Selector_Name (Act2)))
9224 -- Add a similar association if the default is global. The
9225 -- renaming declaration for the actual has been analyzed, and
9226 -- its alias is the program it renames. Link the actual in the
9227 -- original generic tree with the node in the analyzed tree.
9229 while Present (Act2) loop
9230 Subp := Entity (Selector_Name (Act2));
9231 Def := Explicit_Generic_Actual_Parameter (Act2);
9233 -- Following test is defence against rubbish errors
9235 if No (Alias (Subp)) then
9239 -- Retrieve the resolved actual from the renaming declaration
9240 -- created for the instantiated formal.
9242 Actual := Entity (Name (Parent (Parent (Subp))));
9243 Set_Entity (Def, Actual);
9244 Set_Etype (Def, Etype (Actual));
9246 if Is_Global (Actual) then
9248 Make_Generic_Association (Loc,
9249 Selector_Name => New_Occurrence_Of (Subp, Loc),
9250 Explicit_Generic_Actual_Parameter =>
9251 New_Occurrence_Of (Actual, Loc));
9254 (Explicit_Generic_Actual_Parameter (Ndec), Def);
9256 Append (Ndec, Assoc1);
9258 -- If there are other defaults, add a dummy association
9259 -- in case there are other defaulted formals with the same
9262 elsif Present (Next (Act2)) then
9264 Make_Generic_Association (Loc,
9265 Selector_Name => New_Occurrence_Of (Subp, Loc),
9266 Explicit_Generic_Actual_Parameter => Empty);
9268 Append (Ndec, Assoc1);
9275 if Nkind (Name (N1)) = N_Identifier
9276 and then Is_Child_Unit (Gen_Id)
9277 and then Is_Global (Gen_Id)
9278 and then Is_Generic_Unit (Scope (Gen_Id))
9279 and then In_Open_Scopes (Scope (Gen_Id))
9281 -- This is an instantiation of a child unit within a sibling,
9282 -- so that the generic parent is in scope. An eventual instance
9283 -- must occur within the scope of an instance of the parent.
9284 -- Make name in instance into an expanded name, to preserve the
9285 -- identifier of the parent, so it can be resolved subsequently.
9288 Make_Expanded_Name (Loc,
9289 Chars => Chars (Gen_Id),
9290 Prefix => New_Occurrence_Of (Scope (Gen_Id), Loc),
9291 Selector_Name => New_Occurrence_Of (Gen_Id, Loc)));
9292 Set_Entity (Name (N2), Gen_Id);
9295 Make_Expanded_Name (Loc,
9296 Chars => Chars (Gen_Id),
9297 Prefix => New_Occurrence_Of (Scope (Gen_Id), Loc),
9298 Selector_Name => New_Occurrence_Of (Gen_Id, Loc)));
9300 Set_Associated_Node (Name (N1), Name (N2));
9301 Set_Associated_Node (Prefix (Name (N1)), Empty);
9303 (Selector_Name (Name (N1)), Selector_Name (Name (N2)));
9304 Set_Etype (Name (N1), Etype (Gen_Id));
9307 end Save_Global_Defaults;
9309 ----------------------------
9310 -- Save_Global_Descendant --
9311 ----------------------------
9313 procedure Save_Global_Descendant (D : Union_Id) is
9317 if D in Node_Range then
9318 if D = Union_Id (Empty) then
9321 elsif Nkind (Node_Id (D)) /= N_Compilation_Unit then
9322 Save_References (Node_Id (D));
9325 elsif D in List_Range then
9326 if D = Union_Id (No_List)
9327 or else Is_Empty_List (List_Id (D))
9332 N1 := First (List_Id (D));
9333 while Present (N1) loop
9334 Save_References (N1);
9339 -- Element list or other non-node field, nothing to do
9344 end Save_Global_Descendant;
9346 ---------------------
9347 -- Save_References --
9348 ---------------------
9350 -- This is the recursive procedure that does the work, once the
9351 -- enclosing generic scope has been established. We have to treat
9352 -- specially a number of node rewritings that are required by semantic
9353 -- processing and which change the kind of nodes in the generic copy:
9354 -- typically constant-folding, replacing an operator node by a string
9355 -- literal, or a selected component by an expanded name. In each of
9356 -- those cases, the transformation is propagated to the generic unit.
9358 procedure Save_References (N : Node_Id) is
9363 elsif Nkind (N) = N_Character_Literal
9364 or else Nkind (N) = N_Operator_Symbol
9366 if Nkind (N) = Nkind (Get_Associated_Node (N)) then
9369 elsif Nkind (N) = N_Operator_Symbol
9370 and then Nkind (Get_Associated_Node (N)) = N_String_Literal
9372 Change_Operator_Symbol_To_String_Literal (N);
9375 elsif Nkind (N) in N_Op then
9377 if Nkind (N) = Nkind (Get_Associated_Node (N)) then
9379 if Nkind (N) = N_Op_Concat then
9380 Set_Is_Component_Left_Opnd (N,
9381 Is_Component_Left_Opnd (Get_Associated_Node (N)));
9383 Set_Is_Component_Right_Opnd (N,
9384 Is_Component_Right_Opnd (Get_Associated_Node (N)));
9389 -- Node may be transformed into call to a user-defined operator
9391 N2 := Get_Associated_Node (N);
9393 if Nkind (N2) = N_Function_Call then
9394 E := Entity (Name (N2));
9397 and then Is_Global (E)
9399 Set_Etype (N, Etype (N2));
9401 Set_Associated_Node (N, Empty);
9402 Set_Etype (N, Empty);
9405 elsif Nkind (N2) = N_Integer_Literal
9406 or else Nkind (N2) = N_Real_Literal
9407 or else Nkind (N2) = N_String_Literal
9409 -- Operation was constant-folded, perform the same
9410 -- replacement in generic.
9412 Rewrite (N, New_Copy (N2));
9413 Set_Analyzed (N, False);
9415 elsif Nkind (N2) = N_Identifier
9416 and then Ekind (Entity (N2)) = E_Enumeration_Literal
9418 -- Same if call was folded into a literal, but in this
9419 -- case retain the entity to avoid spurious ambiguities
9420 -- if id is overloaded at the point of instantiation or
9423 Rewrite (N, New_Copy (N2));
9424 Set_Associated_Node (N, N2);
9425 Set_Analyzed (N, False);
9429 -- Complete the check on operands, if node has not been
9432 if Nkind (N) in N_Op then
9433 Save_Entity_Descendants (N);
9436 elsif Nkind (N) = N_Identifier then
9437 if Nkind (N) = Nkind (Get_Associated_Node (N)) then
9439 -- If this is a discriminant reference, always save it.
9440 -- It is used in the instance to find the corresponding
9441 -- discriminant positionally rather than by name.
9443 Set_Original_Discriminant
9444 (N, Original_Discriminant (Get_Associated_Node (N)));
9448 N2 := Get_Associated_Node (N);
9450 if Nkind (N2) = N_Function_Call then
9451 E := Entity (Name (N2));
9453 -- Name resolves to a call to parameterless function.
9454 -- If original entity is global, mark node as resolved.
9457 and then Is_Global (E)
9459 Set_Etype (N, Etype (N2));
9461 Set_Associated_Node (N, Empty);
9462 Set_Etype (N, Empty);
9466 Nkind (N2) = N_Integer_Literal or else
9467 Nkind (N2) = N_Real_Literal or else
9468 Nkind (N2) = N_String_Literal
9470 -- Name resolves to named number that is constant-folded,
9471 -- or to string literal from concatenation.
9472 -- Perform the same replacement in generic.
9474 Rewrite (N, New_Copy (N2));
9475 Set_Analyzed (N, False);
9477 elsif Nkind (N2) = N_Explicit_Dereference then
9479 -- An identifier is rewritten as a dereference if it is
9480 -- the prefix in a selected component, and it denotes an
9481 -- access to a composite type, or a parameterless function
9482 -- call that returns an access type.
9484 -- Check whether corresponding entity in prefix is global.
9486 if Is_Entity_Name (Prefix (N2))
9487 and then Present (Entity (Prefix (N2)))
9488 and then Is_Global (Entity (Prefix (N2)))
9491 Make_Explicit_Dereference (Sloc (N),
9492 Prefix => Make_Identifier (Sloc (N),
9493 Chars => Chars (N))));
9494 Set_Associated_Node (Prefix (N), Prefix (N2));
9496 elsif Nkind (Prefix (N2)) = N_Function_Call
9497 and then Is_Global (Entity (Name (Prefix (N2))))
9500 Make_Explicit_Dereference (Sloc (N),
9501 Prefix => Make_Function_Call (Sloc (N),
9503 Make_Identifier (Sloc (N),
9504 Chars => Chars (N)))));
9507 (Name (Prefix (N)), Name (Prefix (N2)));
9510 Set_Associated_Node (N, Empty);
9511 Set_Etype (N, Empty);
9514 -- The subtype mark of a nominally unconstrained object
9515 -- is rewritten as a subtype indication using the bounds
9516 -- of the expression. Recover the original subtype mark.
9518 elsif Nkind (N2) = N_Subtype_Indication
9519 and then Is_Entity_Name (Original_Node (N2))
9521 Set_Associated_Node (N, Original_Node (N2));
9529 elsif Nkind (N) in N_Entity then
9534 use Atree.Unchecked_Access;
9535 -- This code section is part of implementing an untyped tree
9536 -- traversal, so it needs direct access to node fields.
9539 if Nkind (N) = N_Aggregate
9541 Nkind (N) = N_Extension_Aggregate
9543 N2 := Get_Associated_Node (N);
9546 or else No (Etype (N2))
9547 or else not Is_Global (Etype (N2))
9549 Set_Associated_Node (N, Empty);
9552 Save_Global_Descendant (Field1 (N));
9553 Save_Global_Descendant (Field2 (N));
9554 Save_Global_Descendant (Field3 (N));
9555 Save_Global_Descendant (Field5 (N));
9557 -- All other cases than aggregates
9560 Save_Global_Descendant (Field1 (N));
9561 Save_Global_Descendant (Field2 (N));
9562 Save_Global_Descendant (Field3 (N));
9563 Save_Global_Descendant (Field4 (N));
9564 Save_Global_Descendant (Field5 (N));
9568 end Save_References;
9570 -- Start of processing for Save_Global_References
9573 Gen_Scope := Current_Scope;
9575 -- If the generic unit is a child unit, references to entities in
9576 -- the parent are treated as local, because they will be resolved
9577 -- anew in the context of the instance of the parent.
9579 while Is_Child_Unit (Gen_Scope)
9580 and then Ekind (Scope (Gen_Scope)) = E_Generic_Package
9582 Gen_Scope := Scope (Gen_Scope);
9585 Save_References (N);
9586 end Save_Global_References;
9588 --------------------------------------
9589 -- Set_Copied_Sloc_For_Inlined_Body --
9590 --------------------------------------
9592 procedure Set_Copied_Sloc_For_Inlined_Body (N : Node_Id; E : Entity_Id) is
9594 Create_Instantiation_Source (N, E, True, S_Adjustment);
9595 end Set_Copied_Sloc_For_Inlined_Body;
9597 ---------------------
9598 -- Set_Instance_Of --
9599 ---------------------
9601 procedure Set_Instance_Of (A : Entity_Id; B : Entity_Id) is
9603 Generic_Renamings.Table (Generic_Renamings.Last) := (A, B, Assoc_Null);
9604 Generic_Renamings_HTable.Set (Generic_Renamings.Last);
9605 Generic_Renamings.Increment_Last;
9606 end Set_Instance_Of;
9608 --------------------
9609 -- Set_Next_Assoc --
9610 --------------------
9612 procedure Set_Next_Assoc (E : Assoc_Ptr; Next : Assoc_Ptr) is
9614 Generic_Renamings.Table (E).Next_In_HTable := Next;
9621 procedure Start_Generic is
9623 -- ??? I am sure more things could be factored out in this
9624 -- routine. Should probably be done at a later stage.
9626 Generic_Flags.Increment_Last;
9627 Generic_Flags.Table (Generic_Flags.Last) := Inside_A_Generic;
9628 Inside_A_Generic := True;
9630 Expander_Mode_Save_And_Set (False);
9633 ----------------------
9634 -- Set_Instance_Env --
9635 ----------------------
9637 procedure Set_Instance_Env
9638 (Gen_Unit : Entity_Id;
9639 Act_Unit : Entity_Id)
9643 -- Regardless of the current mode, predefined units are analyzed in
9644 -- Ada95 mode, and Ada83 checks don't apply.
9646 if Is_Internal_File_Name
9647 (Fname => Unit_File_Name (Get_Source_Unit (Gen_Unit)),
9648 Renamings_Included => True) then
9652 Current_Instantiated_Parent := (Gen_Unit, Act_Unit, Assoc_Null);
9653 end Set_Instance_Env;
9659 procedure Switch_View (T : Entity_Id) is
9660 BT : constant Entity_Id := Base_Type (T);
9661 Priv_Elmt : Elmt_Id := No_Elmt;
9662 Priv_Sub : Entity_Id;
9665 -- T may be private but its base type may have been exchanged through
9666 -- some other occurrence, in which case there is nothing to switch.
9668 if not Is_Private_Type (BT) then
9672 Priv_Elmt := First_Elmt (Private_Dependents (BT));
9674 if Present (Full_View (BT)) then
9675 Append_Elmt (Full_View (BT), Exchanged_Views);
9676 Exchange_Declarations (BT);
9679 while Present (Priv_Elmt) loop
9680 Priv_Sub := (Node (Priv_Elmt));
9682 -- We avoid flipping the subtype if the Etype of its full
9683 -- view is private because this would result in a malformed
9684 -- subtype. This occurs when the Etype of the subtype full
9685 -- view is the full view of the base type (and since the
9686 -- base types were just switched, the subtype is pointing
9687 -- to the wrong view). This is currently the case for
9688 -- tagged record types, access types (maybe more?) and
9689 -- needs to be resolved. ???
9691 if Present (Full_View (Priv_Sub))
9692 and then not Is_Private_Type (Etype (Full_View (Priv_Sub)))
9694 Append_Elmt (Full_View (Priv_Sub), Exchanged_Views);
9695 Exchange_Declarations (Priv_Sub);
9698 Next_Elmt (Priv_Elmt);
9702 -----------------------------
9703 -- Valid_Default_Attribute --
9704 -----------------------------
9706 procedure Valid_Default_Attribute (Nam : Entity_Id; Def : Node_Id) is
9707 Attr_Id : constant Attribute_Id :=
9708 Get_Attribute_Id (Attribute_Name (Def));
9709 T : constant Entity_Id := Entity (Prefix (Def));
9710 Is_Fun : constant Boolean := (Ekind (Nam) = E_Function);
9723 F := First_Formal (Nam);
9724 while Present (F) loop
9730 when Attribute_Adjacent | Attribute_Ceiling | Attribute_Copy_Sign |
9731 Attribute_Floor | Attribute_Fraction | Attribute_Machine |
9732 Attribute_Model | Attribute_Remainder | Attribute_Rounding |
9733 Attribute_Unbiased_Rounding =>
9736 and then Is_Floating_Point_Type (T);
9738 when Attribute_Image | Attribute_Pred | Attribute_Succ |
9739 Attribute_Value | Attribute_Wide_Image |
9740 Attribute_Wide_Value =>
9741 OK := (Is_Fun and then Num_F = 1 and then Is_Scalar_Type (T));
9743 when Attribute_Max | Attribute_Min =>
9744 OK := (Is_Fun and then Num_F = 2 and then Is_Scalar_Type (T));
9746 when Attribute_Input =>
9747 OK := (Is_Fun and then Num_F = 1);
9749 when Attribute_Output | Attribute_Read | Attribute_Write =>
9750 OK := (not Is_Fun and then Num_F = 2);
9757 Error_Msg_N ("attribute reference has wrong profile for subprogram",
9760 end Valid_Default_Attribute;