1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
9 -- Copyright (C) 1992-2008, 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 3, 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 COPYING3. If not, go to --
19 -- http://www.gnu.org/licenses for a complete copy of the license. --
21 -- GNAT was originally developed by the GNAT team at New York University. --
22 -- Extensive contributions were provided by Ada Core Technologies Inc. --
24 ------------------------------------------------------------------------------
26 with Atree; use Atree;
27 with Einfo; use Einfo;
28 with Elists; use Elists;
29 with Errout; use Errout;
30 with Expander; use Expander;
31 with Fname; use Fname;
32 with Fname.UF; use Fname.UF;
33 with Freeze; use Freeze;
35 with Itypes; use Itypes;
37 with Lib.Load; use Lib.Load;
38 with Lib.Xref; use Lib.Xref;
39 with Nlists; use Nlists;
40 with Namet; use Namet;
41 with Nmake; use Nmake;
43 with Rident; use Rident;
44 with Restrict; use Restrict;
45 with Rtsfind; use Rtsfind;
47 with Sem_Aux; use Sem_Aux;
48 with Sem_Cat; use Sem_Cat;
49 with Sem_Ch3; use Sem_Ch3;
50 with Sem_Ch6; use Sem_Ch6;
51 with Sem_Ch7; use Sem_Ch7;
52 with Sem_Ch8; use Sem_Ch8;
53 with Sem_Ch10; use Sem_Ch10;
54 with Sem_Ch13; use Sem_Ch13;
55 with Sem_Disp; use Sem_Disp;
56 with Sem_Elab; use Sem_Elab;
57 with Sem_Elim; use Sem_Elim;
58 with Sem_Eval; use Sem_Eval;
59 with Sem_Res; use Sem_Res;
60 with Sem_Type; use Sem_Type;
61 with Sem_Util; use Sem_Util;
62 with Sem_Warn; use Sem_Warn;
63 with Stand; use Stand;
64 with Sinfo; use Sinfo;
65 with Sinfo.CN; use Sinfo.CN;
66 with Sinput; use Sinput;
67 with Sinput.L; use Sinput.L;
68 with Snames; use Snames;
69 with Stringt; use Stringt;
70 with Uname; use Uname;
72 with Tbuild; use Tbuild;
73 with Uintp; use Uintp;
74 with Urealp; use Urealp;
78 package body Sem_Ch12 is
80 ----------------------------------------------------------
81 -- Implementation of Generic Analysis and Instantiation --
82 ----------------------------------------------------------
84 -- GNAT implements generics by macro expansion. No attempt is made to share
85 -- generic instantiations (for now). Analysis of a generic definition does
86 -- not perform any expansion action, but the expander must be called on the
87 -- tree for each instantiation, because the expansion may of course depend
88 -- on the generic actuals. All of this is best achieved as follows:
90 -- a) Semantic analysis of a generic unit is performed on a copy of the
91 -- tree for the generic unit. All tree modifications that follow analysis
92 -- do not affect the original tree. Links are kept between the original
93 -- tree and the copy, in order to recognize non-local references within
94 -- the generic, and propagate them to each instance (recall that name
95 -- resolution is done on the generic declaration: generics are not really
96 -- macros!). This is summarized in the following diagram:
98 -- .-----------. .----------.
99 -- | semantic |<--------------| generic |
101 -- | |==============>| |
102 -- |___________| global |__________|
113 -- b) Each instantiation copies the original tree, and inserts into it a
114 -- series of declarations that describe the mapping between generic formals
115 -- and actuals. For example, a generic In OUT parameter is an object
116 -- renaming of the corresponding actual, etc. Generic IN parameters are
117 -- constant declarations.
119 -- c) In order to give the right visibility for these renamings, we use
120 -- a different scheme for package and subprogram instantiations. For
121 -- packages, the list of renamings is inserted into the package
122 -- specification, before the visible declarations of the package. The
123 -- renamings are analyzed before any of the text of the instance, and are
124 -- thus visible at the right place. Furthermore, outside of the instance,
125 -- the generic parameters are visible and denote their corresponding
128 -- For subprograms, we create a container package to hold the renamings
129 -- and the subprogram instance itself. Analysis of the package makes the
130 -- renaming declarations visible to the subprogram. After analyzing the
131 -- package, the defining entity for the subprogram is touched-up so that
132 -- it appears declared in the current scope, and not inside the container
135 -- If the instantiation is a compilation unit, the container package is
136 -- given the same name as the subprogram instance. This ensures that
137 -- the elaboration procedure called by the binder, using the compilation
138 -- unit name, calls in fact the elaboration procedure for the package.
140 -- Not surprisingly, private types complicate this approach. By saving in
141 -- the original generic object the non-local references, we guarantee that
142 -- the proper entities are referenced at the point of instantiation.
143 -- However, for private types, this by itself does not insure that the
144 -- proper VIEW of the entity is used (the full type may be visible at the
145 -- point of generic definition, but not at instantiation, or vice-versa).
146 -- In order to reference the proper view, we special-case any reference
147 -- to private types in the generic object, by saving both views, one in
148 -- the generic and one in the semantic copy. At time of instantiation, we
149 -- check whether the two views are consistent, and exchange declarations if
150 -- necessary, in order to restore the correct visibility. Similarly, if
151 -- the instance view is private when the generic view was not, we perform
152 -- the exchange. After completing the instantiation, we restore the
153 -- current visibility. The flag Has_Private_View marks identifiers in the
154 -- the generic unit that require checking.
156 -- Visibility within nested generic units requires special handling.
157 -- Consider the following scheme:
159 -- type Global is ... -- outside of generic unit.
163 -- type Semi_Global is ... -- global to inner.
166 -- procedure inner (X1 : Global; X2 : Semi_Global);
168 -- procedure in2 is new inner (...); -- 4
171 -- package New_Outer is new Outer (...); -- 2
172 -- procedure New_Inner is new New_Outer.Inner (...); -- 3
174 -- The semantic analysis of Outer captures all occurrences of Global.
175 -- The semantic analysis of Inner (at 1) captures both occurrences of
176 -- Global and Semi_Global.
178 -- At point 2 (instantiation of Outer), we also produce a generic copy
179 -- of Inner, even though Inner is, at that point, not being instantiated.
180 -- (This is just part of the semantic analysis of New_Outer).
182 -- Critically, references to Global within Inner must be preserved, while
183 -- references to Semi_Global should not preserved, because they must now
184 -- resolve to an entity within New_Outer. To distinguish between these, we
185 -- use a global variable, Current_Instantiated_Parent, which is set when
186 -- performing a generic copy during instantiation (at 2). This variable is
187 -- used when performing a generic copy that is not an instantiation, but
188 -- that is nested within one, as the occurrence of 1 within 2. The analysis
189 -- of a nested generic only preserves references that are global to the
190 -- enclosing Current_Instantiated_Parent. We use the Scope_Depth value to
191 -- determine whether a reference is external to the given parent.
193 -- The instantiation at point 3 requires no special treatment. The method
194 -- works as well for further nestings of generic units, but of course the
195 -- variable Current_Instantiated_Parent must be stacked because nested
196 -- instantiations can occur, e.g. the occurrence of 4 within 2.
198 -- The instantiation of package and subprogram bodies is handled in a
199 -- similar manner, except that it is delayed until after semantic
200 -- analysis is complete. In this fashion complex cross-dependencies
201 -- between several package declarations and bodies containing generics
202 -- can be compiled which otherwise would diagnose spurious circularities.
204 -- For example, it is possible to compile two packages A and B that
205 -- have the following structure:
207 -- package A is package B is
208 -- generic ... generic ...
209 -- package G_A is package G_B is
212 -- package body A is package body B is
213 -- package N_B is new G_B (..) package N_A is new G_A (..)
215 -- The table Pending_Instantiations in package Inline is used to keep
216 -- track of body instantiations that are delayed in this manner. Inline
217 -- handles the actual calls to do the body instantiations. This activity
218 -- is part of Inline, since the processing occurs at the same point, and
219 -- for essentially the same reason, as the handling of inlined routines.
221 ----------------------------------------------
222 -- Detection of Instantiation Circularities --
223 ----------------------------------------------
225 -- If we have a chain of instantiations that is circular, this is static
226 -- error which must be detected at compile time. The detection of these
227 -- circularities is carried out at the point that we insert a generic
228 -- instance spec or body. If there is a circularity, then the analysis of
229 -- the offending spec or body will eventually result in trying to load the
230 -- same unit again, and we detect this problem as we analyze the package
231 -- instantiation for the second time.
233 -- At least in some cases after we have detected the circularity, we get
234 -- into trouble if we try to keep going. The following flag is set if a
235 -- circularity is detected, and used to abandon compilation after the
236 -- messages have been posted.
238 Circularity_Detected : Boolean := False;
239 -- This should really be reset on encountering a new main unit, but in
240 -- practice we are not using multiple main units so it is not critical.
242 -------------------------------------------------
243 -- Formal packages and partial parametrization --
244 -------------------------------------------------
246 -- When compiling a generic, a formal package is a local instantiation. If
247 -- declared with a box, its generic formals are visible in the enclosing
248 -- generic. If declared with a partial list of actuals, those actuals that
249 -- are defaulted (covered by an Others clause, or given an explicit box
250 -- initialization) are also visible in the enclosing generic, while those
251 -- that have a corresponding actual are not.
253 -- In our source model of instantiation, the same visibility must be
254 -- present in the spec and body of an instance: the names of the formals
255 -- that are defaulted must be made visible within the instance, and made
256 -- invisible (hidden) after the instantiation is complete, so that they
257 -- are not accessible outside of the instance.
259 -- In a generic, a formal package is treated like a special instantiation.
260 -- Our Ada95 compiler handled formals with and without box in different
261 -- ways. With partial parametrization, we use a single model for both.
262 -- We create a package declaration that consists of the specification of
263 -- the generic package, and a set of declarations that map the actuals
264 -- into local renamings, just as we do for bona fide instantiations. For
265 -- defaulted parameters and formals with a box, we copy directly the
266 -- declarations of the formal into this local package. The result is a
267 -- a package whose visible declarations may include generic formals. This
268 -- package is only used for type checking and visibility analysis, and
269 -- never reaches the back-end, so it can freely violate the placement
270 -- rules for generic formal declarations.
272 -- The list of declarations (renamings and copies of formals) is built
273 -- by Analyze_Associations, just as for regular instantiations.
275 -- At the point of instantiation, conformance checking must be applied only
276 -- to those parameters that were specified in the formal. We perform this
277 -- checking by creating another internal instantiation, this one including
278 -- only the renamings and the formals (the rest of the package spec is not
279 -- relevant to conformance checking). We can then traverse two lists: the
280 -- list of actuals in the instance that corresponds to the formal package,
281 -- and the list of actuals produced for this bogus instantiation. We apply
282 -- the conformance rules to those actuals that are not defaulted (i.e.
283 -- which still appear as generic formals.
285 -- When we compile an instance body we must make the right parameters
286 -- visible again. The predicate Is_Generic_Formal indicates which of the
287 -- formals should have its Is_Hidden flag reset.
289 -----------------------
290 -- Local subprograms --
291 -----------------------
293 procedure Abandon_Instantiation (N : Node_Id);
294 pragma No_Return (Abandon_Instantiation);
295 -- Posts an error message "instantiation abandoned" at the indicated node
296 -- and then raises the exception Instantiation_Error to do it.
298 procedure Analyze_Formal_Array_Type
299 (T : in out Entity_Id;
301 -- A formal array type is treated like an array type declaration, and
302 -- invokes Array_Type_Declaration (sem_ch3) whose first parameter is
303 -- in-out, because in the case of an anonymous type the entity is
304 -- actually created in the procedure.
306 -- The following procedures treat other kinds of formal parameters
308 procedure Analyze_Formal_Derived_Interface_Type
313 procedure Analyze_Formal_Derived_Type
318 procedure Analyze_Formal_Interface_Type
323 -- The following subprograms create abbreviated declarations for formal
324 -- scalar types. We introduce an anonymous base of the proper class for
325 -- each of them, and define the formals as constrained first subtypes of
326 -- their bases. The bounds are expressions that are non-static in the
329 procedure Analyze_Formal_Decimal_Fixed_Point_Type
330 (T : Entity_Id; Def : Node_Id);
331 procedure Analyze_Formal_Discrete_Type (T : Entity_Id; Def : Node_Id);
332 procedure Analyze_Formal_Floating_Type (T : Entity_Id; Def : Node_Id);
333 procedure Analyze_Formal_Signed_Integer_Type (T : Entity_Id; Def : Node_Id);
334 procedure Analyze_Formal_Modular_Type (T : Entity_Id; Def : Node_Id);
335 procedure Analyze_Formal_Ordinary_Fixed_Point_Type
336 (T : Entity_Id; Def : Node_Id);
338 procedure Analyze_Formal_Private_Type
342 -- Creates a new private type, which does not require completion
344 procedure Analyze_Generic_Formal_Part (N : Node_Id);
346 procedure Analyze_Generic_Access_Type (T : Entity_Id; Def : Node_Id);
347 -- Create a new access type with the given designated type
349 function Analyze_Associations
352 F_Copy : List_Id) return List_Id;
353 -- At instantiation time, build the list of associations between formals
354 -- and actuals. Each association becomes a renaming declaration for the
355 -- formal entity. F_Copy is the analyzed list of formals in the generic
356 -- copy. It is used to apply legality checks to the actuals. I_Node is the
357 -- instantiation node itself.
359 procedure Analyze_Subprogram_Instantiation
363 procedure Build_Instance_Compilation_Unit_Nodes
367 -- This procedure is used in the case where the generic instance of a
368 -- subprogram body or package body is a library unit. In this case, the
369 -- original library unit node for the generic instantiation must be
370 -- replaced by the resulting generic body, and a link made to a new
371 -- compilation unit node for the generic declaration. The argument N is
372 -- the original generic instantiation. Act_Body and Act_Decl are the body
373 -- and declaration of the instance (either package body and declaration
374 -- nodes or subprogram body and declaration nodes depending on the case).
375 -- On return, the node N has been rewritten with the actual body.
377 procedure Check_Access_Definition (N : Node_Id);
378 -- Subsidiary routine to null exclusion processing. Perform an assertion
379 -- check on Ada version and the presence of an access definition in N.
381 procedure Check_Formal_Packages (P_Id : Entity_Id);
382 -- Apply the following to all formal packages in generic associations
384 procedure Check_Formal_Package_Instance
385 (Formal_Pack : Entity_Id;
386 Actual_Pack : Entity_Id);
387 -- Verify that the actuals of the actual instance match the actuals of
388 -- the template for a formal package that is not declared with a box.
390 procedure Check_Forward_Instantiation (Decl : Node_Id);
391 -- If the generic is a local entity and the corresponding body has not
392 -- been seen yet, flag enclosing packages to indicate that it will be
393 -- elaborated after the generic body. Subprograms declared in the same
394 -- package cannot be inlined by the front-end because front-end inlining
395 -- requires a strict linear order of elaboration.
397 procedure Check_Hidden_Child_Unit
399 Gen_Unit : Entity_Id;
400 Act_Decl_Id : Entity_Id);
401 -- If the generic unit is an implicit child instance within a parent
402 -- instance, we need to make an explicit test that it is not hidden by
403 -- a child instance of the same name and parent.
405 procedure Check_Generic_Actuals
406 (Instance : Entity_Id;
407 Is_Formal_Box : Boolean);
408 -- Similar to previous one. Check the actuals in the instantiation,
409 -- whose views can change between the point of instantiation and the point
410 -- of instantiation of the body. In addition, mark the generic renamings
411 -- as generic actuals, so that they are not compatible with other actuals.
412 -- Recurse on an actual that is a formal package whose declaration has
415 function Contains_Instance_Of
418 N : Node_Id) return Boolean;
419 -- Inner is instantiated within the generic Outer. Check whether Inner
420 -- directly or indirectly contains an instance of Outer or of one of its
421 -- parents, in the case of a subunit. Each generic unit holds a list of
422 -- the entities instantiated within (at any depth). This procedure
423 -- determines whether the set of such lists contains a cycle, i.e. an
424 -- illegal circular instantiation.
426 function Denotes_Formal_Package
428 On_Exit : Boolean := False) return Boolean;
429 -- Returns True if E is a formal package of an enclosing generic, or
430 -- the actual for such a formal in an enclosing instantiation. If such
431 -- a package is used as a formal in an nested generic, or as an actual
432 -- in a nested instantiation, the visibility of ITS formals should not
433 -- be modified. When called from within Restore_Private_Views, the flag
434 -- On_Exit is true, to indicate that the search for a possible enclosing
435 -- instance should ignore the current one.
437 function Find_Actual_Type
439 Gen_Type : Entity_Id) return Entity_Id;
440 -- When validating the actual types of a child instance, check whether
441 -- the formal is a formal type of the parent unit, and retrieve the current
442 -- actual for it. Typ is the entity in the analyzed formal type declaration
443 -- (component or index type of an array type, or designated type of an
444 -- access formal) and Gen_Type is the enclosing analyzed formal array
445 -- or access type. The desired actual may be a formal of a parent, or may
446 -- be declared in a formal package of a parent. In both cases it is a
447 -- generic actual type because it appears within a visible instance.
448 -- Finally, it may be declared in a parent unit without being a formal
449 -- of that unit, in which case it must be retrieved by visibility.
450 -- Ambiguities may still arise if two homonyms are declared in two formal
451 -- packages, and the prefix of the formal type may be needed to resolve
452 -- the ambiguity in the instance ???
454 function In_Same_Declarative_Part
456 Inst : Node_Id) return Boolean;
457 -- True if the instantiation Inst and the given freeze_node F_Node appear
458 -- within the same declarative part, ignoring subunits, but with no inter-
459 -- vening subprograms or concurrent units. If true, the freeze node
460 -- of the instance can be placed after the freeze node of the parent,
461 -- which it itself an instance.
463 function In_Main_Context (E : Entity_Id) return Boolean;
464 -- Check whether an instantiation is in the context of the main unit.
465 -- Used to determine whether its body should be elaborated to allow
466 -- front-end inlining.
468 function Is_Generic_Formal (E : Entity_Id) return Boolean;
469 -- Utility to determine whether a given entity is declared by means of
470 -- of a formal parameter declaration. Used to set properly the visibility
471 -- of generic formals of a generic package declared with a box or with
472 -- partial parametrization.
474 procedure Set_Instance_Env
475 (Gen_Unit : Entity_Id;
476 Act_Unit : Entity_Id);
477 -- Save current instance on saved environment, to be used to determine
478 -- the global status of entities in nested instances. Part of Save_Env.
479 -- called after verifying that the generic unit is legal for the instance,
480 -- The procedure also examines whether the generic unit is a predefined
481 -- unit, in order to set configuration switches accordingly. As a result
482 -- the procedure must be called after analyzing and freezing the actuals.
484 procedure Set_Instance_Of (A : Entity_Id; B : Entity_Id);
485 -- Associate analyzed generic parameter with corresponding
486 -- instance. Used for semantic checks at instantiation time.
488 function Has_Been_Exchanged (E : Entity_Id) return Boolean;
489 -- Traverse the Exchanged_Views list to see if a type was private
490 -- and has already been flipped during this phase of instantiation.
492 procedure Hide_Current_Scope;
493 -- When instantiating a generic child unit, the parent context must be
494 -- present, but the instance and all entities that may be generated
495 -- must be inserted in the current scope. We leave the current scope
496 -- on the stack, but make its entities invisible to avoid visibility
497 -- problems. This is reversed at the end of the instantiation. This is
498 -- not done for the instantiation of the bodies, which only require the
499 -- instances of the generic parents to be in scope.
501 procedure Install_Body
506 -- If the instantiation happens textually before the body of the generic,
507 -- the instantiation of the body must be analyzed after the generic body,
508 -- and not at the point of instantiation. Such early instantiations can
509 -- happen if the generic and the instance appear in a package declaration
510 -- because the generic body can only appear in the corresponding package
511 -- body. Early instantiations can also appear if generic, instance and
512 -- body are all in the declarative part of a subprogram or entry. Entities
513 -- of packages that are early instantiations are delayed, and their freeze
514 -- node appears after the generic body.
516 procedure Insert_After_Last_Decl (N : Node_Id; F_Node : Node_Id);
517 -- Insert freeze node at the end of the declarative part that includes the
518 -- instance node N. If N is in the visible part of an enclosing package
519 -- declaration, the freeze node has to be inserted at the end of the
520 -- private declarations, if any.
522 procedure Freeze_Subprogram_Body
523 (Inst_Node : Node_Id;
525 Pack_Id : Entity_Id);
526 -- The generic body may appear textually after the instance, including
527 -- in the proper body of a stub, or within a different package instance.
528 -- Given that the instance can only be elaborated after the generic, we
529 -- place freeze_nodes for the instance and/or for packages that may enclose
530 -- the instance and the generic, so that the back-end can establish the
531 -- proper order of elaboration.
534 -- Establish environment for subsequent instantiation. Separated from
535 -- Save_Env because data-structures for visibility handling must be
536 -- initialized before call to Check_Generic_Child_Unit.
538 procedure Install_Formal_Packages (Par : Entity_Id);
539 -- If any of the formals of the parent are formal packages with box,
540 -- their formal parts are visible in the parent and thus in the child
541 -- unit as well. Analogous to what is done in Check_Generic_Actuals
542 -- for the unit itself. This procedure is also used in an instance, to
543 -- make visible the proper entities of the actual for a formal package
544 -- declared with a box.
546 procedure Install_Parent (P : Entity_Id; In_Body : Boolean := False);
547 -- When compiling an instance of a child unit the parent (which is
548 -- itself an instance) is an enclosing scope that must be made
549 -- immediately visible. This procedure is also used to install the non-
550 -- generic parent of a generic child unit when compiling its body, so
551 -- that full views of types in the parent are made visible.
553 procedure Remove_Parent (In_Body : Boolean := False);
554 -- Reverse effect after instantiation of child is complete
556 procedure Inline_Instance_Body
558 Gen_Unit : Entity_Id;
560 -- If front-end inlining is requested, instantiate the package body,
561 -- and preserve the visibility of its compilation unit, to insure
562 -- that successive instantiations succeed.
564 -- The functions Instantiate_XXX perform various legality checks and build
565 -- the declarations for instantiated generic parameters. In all of these
566 -- Formal is the entity in the generic unit, Actual is the entity of
567 -- expression in the generic associations, and Analyzed_Formal is the
568 -- formal in the generic copy, which contains the semantic information to
569 -- be used to validate the actual.
571 function Instantiate_Object
574 Analyzed_Formal : Node_Id) return List_Id;
576 function Instantiate_Type
579 Analyzed_Formal : Node_Id;
580 Actual_Decls : List_Id) return List_Id;
582 function Instantiate_Formal_Subprogram
585 Analyzed_Formal : Node_Id) return Node_Id;
587 function Instantiate_Formal_Package
590 Analyzed_Formal : Node_Id) return List_Id;
591 -- If the formal package is declared with a box, special visibility rules
592 -- apply to its formals: they are in the visible part of the package. This
593 -- is true in the declarative region of the formal package, that is to say
594 -- in the enclosing generic or instantiation. For an instantiation, the
595 -- parameters of the formal package are made visible in an explicit step.
596 -- Furthermore, if the actual has a visible USE clause, these formals must
597 -- be made potentially use-visible as well. On exit from the enclosing
598 -- instantiation, the reverse must be done.
600 -- For a formal package declared without a box, there are conformance rules
601 -- that apply to the actuals in the generic declaration and the actuals of
602 -- the actual package in the enclosing instantiation. The simplest way to
603 -- apply these rules is to repeat the instantiation of the formal package
604 -- in the context of the enclosing instance, and compare the generic
605 -- associations of this instantiation with those of the actual package.
606 -- This internal instantiation only needs to contain the renamings of the
607 -- formals: the visible and private declarations themselves need not be
610 -- In Ada 2005, the formal package may be only partially parametrized. In
611 -- that case the visibility step must make visible those actuals whose
612 -- corresponding formals were given with a box. A final complication
613 -- involves inherited operations from formal derived types, which must be
614 -- visible if the type is.
616 function Is_In_Main_Unit (N : Node_Id) return Boolean;
617 -- Test if given node is in the main unit
619 procedure Load_Parent_Of_Generic
622 Body_Optional : Boolean := False);
623 -- If the generic appears in a separate non-generic library unit, load the
624 -- corresponding body to retrieve the body of the generic. N is the node
625 -- for the generic instantiation, Spec is the generic package declaration.
627 -- Body_Optional is a flag that indicates that the body is being loaded to
628 -- ensure that temporaries are generated consistently when there are other
629 -- instances in the current declarative part that precede the one being
630 -- loaded. In that case a missing body is acceptable.
632 procedure Inherit_Context (Gen_Decl : Node_Id; Inst : Node_Id);
633 -- Add the context clause of the unit containing a generic unit to an
634 -- instantiation that is a compilation unit.
636 function Get_Associated_Node (N : Node_Id) return Node_Id;
637 -- In order to propagate semantic information back from the analyzed copy
638 -- to the original generic, we maintain links between selected nodes in the
639 -- generic and their corresponding copies. At the end of generic analysis,
640 -- the routine Save_Global_References traverses the generic tree, examines
641 -- the semantic information, and preserves the links to those nodes that
642 -- contain global information. At instantiation, the information from the
643 -- associated node is placed on the new copy, so that name resolution is
646 -- Three kinds of source nodes have associated nodes:
648 -- a) those that can reference (denote) entities, that is identifiers,
649 -- character literals, expanded_names, operator symbols, operators,
650 -- and attribute reference nodes. These nodes have an Entity field
651 -- and are the set of nodes that are in N_Has_Entity.
653 -- b) aggregates (N_Aggregate and N_Extension_Aggregate)
655 -- c) selected components (N_Selected_Component)
657 -- For the first class, the associated node preserves the entity if it is
658 -- global. If the generic contains nested instantiations, the associated
659 -- node itself has been recopied, and a chain of them must be followed.
661 -- For aggregates, the associated node allows retrieval of the type, which
662 -- may otherwise not appear in the generic. The view of this type may be
663 -- different between generic and instantiation, and the full view can be
664 -- installed before the instantiation is analyzed. For aggregates of type
665 -- extensions, the same view exchange may have to be performed for some of
666 -- the ancestor types, if their view is private at the point of
669 -- Nodes that are selected components in the parse tree may be rewritten
670 -- as expanded names after resolution, and must be treated as potential
671 -- entity holders, which is why they also have an Associated_Node.
673 -- Nodes that do not come from source, such as freeze nodes, do not appear
674 -- in the generic tree, and need not have an associated node.
676 -- The associated node is stored in the Associated_Node field. Note that
677 -- this field overlaps Entity, which is fine, because the whole point is
678 -- that we don't need or want the normal Entity field in this situation.
680 procedure Move_Freeze_Nodes
684 -- Freeze nodes can be generated in the analysis of a generic unit, but
685 -- will not be seen by the back-end. It is necessary to move those nodes
686 -- to the enclosing scope if they freeze an outer entity. We place them
687 -- at the end of the enclosing generic package, which is semantically
690 procedure Preanalyze_Actuals (N : Node_Id);
691 -- Analyze actuals to perform name resolution. Full resolution is done
692 -- later, when the expected types are known, but names have to be captured
693 -- before installing parents of generics, that are not visible for the
694 -- actuals themselves.
696 procedure Valid_Default_Attribute (Nam : Entity_Id; Def : Node_Id);
697 -- Verify that an attribute that appears as the default for a formal
698 -- subprogram is a function or procedure with the correct profile.
700 -------------------------------------------
701 -- Data Structures for Generic Renamings --
702 -------------------------------------------
704 -- The map Generic_Renamings associates generic entities with their
705 -- corresponding actuals. Currently used to validate type instances. It
706 -- will eventually be used for all generic parameters to eliminate the
707 -- need for overload resolution in the instance.
709 type Assoc_Ptr is new Int;
711 Assoc_Null : constant Assoc_Ptr := -1;
716 Next_In_HTable : Assoc_Ptr;
719 package Generic_Renamings is new Table.Table
720 (Table_Component_Type => Assoc,
721 Table_Index_Type => Assoc_Ptr,
722 Table_Low_Bound => 0,
724 Table_Increment => 100,
725 Table_Name => "Generic_Renamings");
727 -- Variable to hold enclosing instantiation. When the environment is
728 -- saved for a subprogram inlining, the corresponding Act_Id is empty.
730 Current_Instantiated_Parent : Assoc := (Empty, Empty, Assoc_Null);
732 -- Hash table for associations
734 HTable_Size : constant := 37;
735 type HTable_Range is range 0 .. HTable_Size - 1;
737 procedure Set_Next_Assoc (E : Assoc_Ptr; Next : Assoc_Ptr);
738 function Next_Assoc (E : Assoc_Ptr) return Assoc_Ptr;
739 function Get_Gen_Id (E : Assoc_Ptr) return Entity_Id;
740 function Hash (F : Entity_Id) return HTable_Range;
742 package Generic_Renamings_HTable is new GNAT.HTable.Static_HTable (
743 Header_Num => HTable_Range,
745 Elmt_Ptr => Assoc_Ptr,
746 Null_Ptr => Assoc_Null,
747 Set_Next => Set_Next_Assoc,
750 Get_Key => Get_Gen_Id,
754 Exchanged_Views : Elist_Id;
755 -- This list holds the private views that have been exchanged during
756 -- instantiation to restore the visibility of the generic declaration.
757 -- (see comments above). After instantiation, the current visibility is
758 -- reestablished by means of a traversal of this list.
760 Hidden_Entities : Elist_Id;
761 -- This list holds the entities of the current scope that are removed
762 -- from immediate visibility when instantiating a child unit. Their
763 -- visibility is restored in Remove_Parent.
765 -- Because instantiations can be recursive, the following must be saved
766 -- on entry and restored on exit from an instantiation (spec or body).
767 -- This is done by the two procedures Save_Env and Restore_Env. For
768 -- package and subprogram instantiations (but not for the body instances)
769 -- the action of Save_Env is done in two steps: Init_Env is called before
770 -- Check_Generic_Child_Unit, because setting the parent instances requires
771 -- that the visibility data structures be properly initialized. Once the
772 -- generic is unit is validated, Set_Instance_Env completes Save_Env.
774 Parent_Unit_Visible : Boolean := False;
775 -- Parent_Unit_Visible is used when the generic is a child unit, and
776 -- indicates whether the ultimate parent of the generic is visible in the
777 -- instantiation environment. It is used to reset the visibility of the
778 -- parent at the end of the instantiation (see Remove_Parent).
780 Instance_Parent_Unit : Entity_Id := Empty;
781 -- This records the ultimate parent unit of an instance of a generic
782 -- child unit and is used in conjunction with Parent_Unit_Visible to
783 -- indicate the unit to which the Parent_Unit_Visible flag corresponds.
785 type Instance_Env is record
786 Instantiated_Parent : Assoc;
787 Exchanged_Views : Elist_Id;
788 Hidden_Entities : Elist_Id;
789 Current_Sem_Unit : Unit_Number_Type;
790 Parent_Unit_Visible : Boolean := False;
791 Instance_Parent_Unit : Entity_Id := Empty;
792 Switches : Config_Switches_Type;
795 package Instance_Envs is new Table.Table (
796 Table_Component_Type => Instance_Env,
797 Table_Index_Type => Int,
798 Table_Low_Bound => 0,
800 Table_Increment => 100,
801 Table_Name => "Instance_Envs");
803 procedure Restore_Private_Views
804 (Pack_Id : Entity_Id;
805 Is_Package : Boolean := True);
806 -- Restore the private views of external types, and unmark the generic
807 -- renamings of actuals, so that they become compatible subtypes again.
808 -- For subprograms, Pack_Id is the package constructed to hold the
811 procedure Switch_View (T : Entity_Id);
812 -- Switch the partial and full views of a type and its private
813 -- dependents (i.e. its subtypes and derived types).
815 ------------------------------------
816 -- Structures for Error Reporting --
817 ------------------------------------
819 Instantiation_Node : Node_Id;
820 -- Used by subprograms that validate instantiation of formal parameters
821 -- where there might be no actual on which to place the error message.
822 -- Also used to locate the instantiation node for generic subunits.
824 Instantiation_Error : exception;
825 -- When there is a semantic error in the generic parameter matching,
826 -- there is no point in continuing the instantiation, because the
827 -- number of cascaded errors is unpredictable. This exception aborts
828 -- the instantiation process altogether.
830 S_Adjustment : Sloc_Adjustment;
831 -- Offset created for each node in an instantiation, in order to keep
832 -- track of the source position of the instantiation in each of its nodes.
833 -- A subsequent semantic error or warning on a construct of the instance
834 -- points to both places: the original generic node, and the point of
835 -- instantiation. See Sinput and Sinput.L for additional details.
837 ------------------------------------------------------------
838 -- Data structure for keeping track when inside a Generic --
839 ------------------------------------------------------------
841 -- The following table is used to save values of the Inside_A_Generic
842 -- flag (see spec of Sem) when they are saved by Start_Generic.
844 package Generic_Flags is new Table.Table (
845 Table_Component_Type => Boolean,
846 Table_Index_Type => Int,
847 Table_Low_Bound => 0,
849 Table_Increment => 200,
850 Table_Name => "Generic_Flags");
852 ---------------------------
853 -- Abandon_Instantiation --
854 ---------------------------
856 procedure Abandon_Instantiation (N : Node_Id) is
858 Error_Msg_N ("\instantiation abandoned!", N);
859 raise Instantiation_Error;
860 end Abandon_Instantiation;
862 --------------------------
863 -- Analyze_Associations --
864 --------------------------
866 function Analyze_Associations
869 F_Copy : List_Id) return List_Id
871 Actual_Types : constant Elist_Id := New_Elmt_List;
872 Assoc : constant List_Id := New_List;
873 Default_Actuals : constant Elist_Id := New_Elmt_List;
874 Gen_Unit : constant Entity_Id := Defining_Entity (Parent (F_Copy));
878 Next_Formal : Node_Id;
879 Temp_Formal : Node_Id;
880 Analyzed_Formal : Node_Id;
883 First_Named : Node_Id := Empty;
885 Default_Formals : constant List_Id := New_List;
886 -- If an Other_Choice is present, some of the formals may be defaulted.
887 -- To simplify the treatment of visibility in an instance, we introduce
888 -- individual defaults for each such formal. These defaults are
889 -- appended to the list of associations and replace the Others_Choice.
891 Found_Assoc : Node_Id;
892 -- Association for the current formal being match. Empty if there are
893 -- no remaining actuals, or if there is no named association with the
894 -- name of the formal.
896 Is_Named_Assoc : Boolean;
897 Num_Matched : Int := 0;
898 Num_Actuals : Int := 0;
900 Others_Present : Boolean := False;
901 -- In Ada 2005, indicates partial parametrization of a formal
902 -- package. As usual an other association must be last in the list.
904 function Matching_Actual
906 A_F : Entity_Id) return Node_Id;
907 -- Find actual that corresponds to a given a formal parameter. If the
908 -- actuals are positional, return the next one, if any. If the actuals
909 -- are named, scan the parameter associations to find the right one.
910 -- A_F is the corresponding entity in the analyzed generic,which is
911 -- placed on the selector name for ASIS use.
913 -- In Ada 2005, a named association may be given with a box, in which
914 -- case Matching_Actual sets Found_Assoc to the generic association,
915 -- but return Empty for the actual itself. In this case the code below
916 -- creates a corresponding declaration for the formal.
918 function Partial_Parametrization return Boolean;
919 -- Ada 2005: if no match is found for a given formal, check if the
920 -- association for it includes a box, or whether the associations
921 -- include an Others clause.
923 procedure Process_Default (F : Entity_Id);
924 -- Add a copy of the declaration of generic formal F to the list of
925 -- associations, and add an explicit box association for F if there
926 -- is none yet, and the default comes from an Others_Choice.
928 procedure Set_Analyzed_Formal;
929 -- Find the node in the generic copy that corresponds to a given formal.
930 -- The semantic information on this node is used to perform legality
931 -- checks on the actuals. Because semantic analysis can introduce some
932 -- anonymous entities or modify the declaration node itself, the
933 -- correspondence between the two lists is not one-one. In addition to
934 -- anonymous types, the presence a formal equality will introduce an
935 -- implicit declaration for the corresponding inequality.
937 ---------------------
938 -- Matching_Actual --
939 ---------------------
941 function Matching_Actual
943 A_F : Entity_Id) return Node_Id
949 Is_Named_Assoc := False;
951 -- End of list of purely positional parameters
954 or else Nkind (Actual) = N_Others_Choice
956 Found_Assoc := Empty;
959 -- Case of positional parameter corresponding to current formal
961 elsif No (Selector_Name (Actual)) then
962 Found_Assoc := Actual;
963 Act := Explicit_Generic_Actual_Parameter (Actual);
964 Num_Matched := Num_Matched + 1;
967 -- Otherwise scan list of named actuals to find the one with the
968 -- desired name. All remaining actuals have explicit names.
971 Is_Named_Assoc := True;
972 Found_Assoc := Empty;
976 while Present (Actual) loop
977 if Chars (Selector_Name (Actual)) = Chars (F) then
978 Set_Entity (Selector_Name (Actual), A_F);
979 Set_Etype (Selector_Name (Actual), Etype (A_F));
980 Generate_Reference (A_F, Selector_Name (Actual));
981 Found_Assoc := Actual;
982 Act := Explicit_Generic_Actual_Parameter (Actual);
983 Num_Matched := Num_Matched + 1;
991 -- Reset for subsequent searches. In most cases the named
992 -- associations are in order. If they are not, we reorder them
993 -- to avoid scanning twice the same actual. This is not just a
994 -- question of efficiency: there may be multiple defaults with
995 -- boxes that have the same name. In a nested instantiation we
996 -- insert actuals for those defaults, and cannot rely on their
997 -- names to disambiguate them.
999 if Actual = First_Named then
1002 elsif Present (Actual) then
1003 Insert_Before (First_Named, Remove_Next (Prev));
1006 Actual := First_Named;
1009 if Is_Entity_Name (Act) and then Present (Entity (Act)) then
1010 Set_Used_As_Generic_Actual (Entity (Act));
1014 end Matching_Actual;
1016 -----------------------------
1017 -- Partial_Parametrization --
1018 -----------------------------
1020 function Partial_Parametrization return Boolean is
1022 return Others_Present
1023 or else (Present (Found_Assoc) and then Box_Present (Found_Assoc));
1024 end Partial_Parametrization;
1026 ---------------------
1027 -- Process_Default --
1028 ---------------------
1030 procedure Process_Default (F : Entity_Id) is
1031 Loc : constant Source_Ptr := Sloc (I_Node);
1032 F_Id : constant Entity_Id := Defining_Entity (F);
1039 -- Append copy of formal declaration to associations, and create
1040 -- new defining identifier for it.
1042 Decl := New_Copy_Tree (F);
1043 Id := Make_Defining_Identifier (Sloc (F_Id), Chars => Chars (F_Id));
1045 if Nkind (F) in N_Formal_Subprogram_Declaration then
1046 Set_Defining_Unit_Name (Specification (Decl), Id);
1049 Set_Defining_Identifier (Decl, Id);
1052 Append (Decl, Assoc);
1054 if No (Found_Assoc) then
1056 Make_Generic_Association (Loc,
1057 Selector_Name => New_Occurrence_Of (Id, Loc),
1058 Explicit_Generic_Actual_Parameter => Empty);
1059 Set_Box_Present (Default);
1060 Append (Default, Default_Formals);
1062 end Process_Default;
1064 -------------------------
1065 -- Set_Analyzed_Formal --
1066 -------------------------
1068 procedure Set_Analyzed_Formal is
1072 while Present (Analyzed_Formal) loop
1073 Kind := Nkind (Analyzed_Formal);
1075 case Nkind (Formal) is
1077 when N_Formal_Subprogram_Declaration =>
1078 exit when Kind in N_Formal_Subprogram_Declaration
1081 (Defining_Unit_Name (Specification (Formal))) =
1083 (Defining_Unit_Name (Specification (Analyzed_Formal)));
1085 when N_Formal_Package_Declaration =>
1086 exit when Nkind_In (Kind, N_Formal_Package_Declaration,
1087 N_Generic_Package_Declaration,
1088 N_Package_Declaration);
1090 when N_Use_Package_Clause | N_Use_Type_Clause => exit;
1094 -- Skip freeze nodes, and nodes inserted to replace
1095 -- unrecognized pragmas.
1098 Kind not in N_Formal_Subprogram_Declaration
1099 and then not Nkind_In (Kind, N_Subprogram_Declaration,
1103 and then Chars (Defining_Identifier (Formal)) =
1104 Chars (Defining_Identifier (Analyzed_Formal));
1107 Next (Analyzed_Formal);
1109 end Set_Analyzed_Formal;
1111 -- Start of processing for Analyze_Associations
1114 Actuals := Generic_Associations (I_Node);
1116 if Present (Actuals) then
1118 -- check for an Others choice, indicating a partial parametrization
1119 -- for a formal package.
1121 Actual := First (Actuals);
1122 while Present (Actual) loop
1123 if Nkind (Actual) = N_Others_Choice then
1124 Others_Present := True;
1126 if Present (Next (Actual)) then
1127 Error_Msg_N ("others must be last association", Actual);
1130 -- This subprogram is used both for formal packages and for
1131 -- instantiations. For the latter, associations must all be
1134 if Nkind (I_Node) /= N_Formal_Package_Declaration
1135 and then Comes_From_Source (I_Node)
1138 ("others association not allowed in an instance",
1142 -- In any case, nothing to do after the others association
1146 elsif Box_Present (Actual)
1147 and then Comes_From_Source (I_Node)
1148 and then Nkind (I_Node) /= N_Formal_Package_Declaration
1151 ("box association not allowed in an instance", Actual);
1157 -- If named associations are present, save first named association
1158 -- (it may of course be Empty) to facilitate subsequent name search.
1160 First_Named := First (Actuals);
1161 while Present (First_Named)
1162 and then Nkind (First_Named) /= N_Others_Choice
1163 and then No (Selector_Name (First_Named))
1165 Num_Actuals := Num_Actuals + 1;
1170 Named := First_Named;
1171 while Present (Named) loop
1172 if Nkind (Named) /= N_Others_Choice
1173 and then No (Selector_Name (Named))
1175 Error_Msg_N ("invalid positional actual after named one", Named);
1176 Abandon_Instantiation (Named);
1179 -- A named association may lack an actual parameter, if it was
1180 -- introduced for a default subprogram that turns out to be local
1181 -- to the outer instantiation.
1183 if Nkind (Named) /= N_Others_Choice
1184 and then Present (Explicit_Generic_Actual_Parameter (Named))
1186 Num_Actuals := Num_Actuals + 1;
1192 if Present (Formals) then
1193 Formal := First_Non_Pragma (Formals);
1194 Analyzed_Formal := First_Non_Pragma (F_Copy);
1196 if Present (Actuals) then
1197 Actual := First (Actuals);
1199 -- All formals should have default values
1205 while Present (Formal) loop
1206 Set_Analyzed_Formal;
1207 Next_Formal := Next_Non_Pragma (Formal);
1209 case Nkind (Formal) is
1210 when N_Formal_Object_Declaration =>
1213 Defining_Identifier (Formal),
1214 Defining_Identifier (Analyzed_Formal));
1216 if No (Match) and then Partial_Parametrization then
1217 Process_Default (Formal);
1220 (Instantiate_Object (Formal, Match, Analyzed_Formal),
1224 when N_Formal_Type_Declaration =>
1227 Defining_Identifier (Formal),
1228 Defining_Identifier (Analyzed_Formal));
1231 if Partial_Parametrization then
1232 Process_Default (Formal);
1235 Error_Msg_Sloc := Sloc (Gen_Unit);
1239 Defining_Identifier (Formal));
1240 Error_Msg_NE ("\in instantiation of & declared#",
1241 Instantiation_Node, Gen_Unit);
1242 Abandon_Instantiation (Instantiation_Node);
1249 (Formal, Match, Analyzed_Formal, Assoc),
1252 -- An instantiation is a freeze point for the actuals,
1253 -- unless this is a rewritten formal package.
1255 if Nkind (I_Node) /= N_Formal_Package_Declaration then
1256 Append_Elmt (Entity (Match), Actual_Types);
1260 -- A remote access-to-class-wide type must not be an
1261 -- actual parameter for a generic formal of an access
1262 -- type (E.2.2 (17)).
1264 if Nkind (Analyzed_Formal) = N_Formal_Type_Declaration
1266 Nkind (Formal_Type_Definition (Analyzed_Formal)) =
1267 N_Access_To_Object_Definition
1269 Validate_Remote_Access_To_Class_Wide_Type (Match);
1272 when N_Formal_Subprogram_Declaration =>
1275 Defining_Unit_Name (Specification (Formal)),
1276 Defining_Unit_Name (Specification (Analyzed_Formal)));
1278 -- If the formal subprogram has the same name as
1279 -- another formal subprogram of the generic, then
1280 -- a named association is illegal (12.3(9)). Exclude
1281 -- named associations that are generated for a nested
1285 and then Is_Named_Assoc
1286 and then Comes_From_Source (Found_Assoc)
1288 Temp_Formal := First (Formals);
1289 while Present (Temp_Formal) loop
1290 if Nkind (Temp_Formal) in
1291 N_Formal_Subprogram_Declaration
1292 and then Temp_Formal /= Formal
1294 Chars (Selector_Name (Found_Assoc)) =
1295 Chars (Defining_Unit_Name
1296 (Specification (Temp_Formal)))
1299 ("name not allowed for overloaded formal",
1301 Abandon_Instantiation (Instantiation_Node);
1308 -- If there is no corresponding actual, this may be case of
1309 -- partial parametrization, or else the formal has a default
1313 and then Partial_Parametrization
1315 Process_Default (Formal);
1318 Instantiate_Formal_Subprogram
1319 (Formal, Match, Analyzed_Formal));
1322 -- If this is a nested generic, preserve default for later
1326 and then Box_Present (Formal)
1329 (Defining_Unit_Name (Specification (Last (Assoc))),
1333 when N_Formal_Package_Declaration =>
1336 Defining_Identifier (Formal),
1337 Defining_Identifier (Original_Node (Analyzed_Formal)));
1340 if Partial_Parametrization then
1341 Process_Default (Formal);
1344 Error_Msg_Sloc := Sloc (Gen_Unit);
1347 Instantiation_Node, Defining_Identifier (Formal));
1348 Error_Msg_NE ("\in instantiation of & declared#",
1349 Instantiation_Node, Gen_Unit);
1351 Abandon_Instantiation (Instantiation_Node);
1357 (Instantiate_Formal_Package
1358 (Formal, Match, Analyzed_Formal),
1362 -- For use type and use package appearing in the generic part,
1363 -- we have already copied them, so we can just move them where
1364 -- they belong (we mustn't recopy them since this would mess up
1365 -- the Sloc values).
1367 when N_Use_Package_Clause |
1368 N_Use_Type_Clause =>
1369 if Nkind (Original_Node (I_Node)) =
1370 N_Formal_Package_Declaration
1372 Append (New_Copy_Tree (Formal), Assoc);
1375 Append (Formal, Assoc);
1379 raise Program_Error;
1383 Formal := Next_Formal;
1384 Next_Non_Pragma (Analyzed_Formal);
1387 if Num_Actuals > Num_Matched then
1388 Error_Msg_Sloc := Sloc (Gen_Unit);
1390 if Present (Selector_Name (Actual)) then
1392 ("unmatched actual&",
1393 Actual, Selector_Name (Actual));
1394 Error_Msg_NE ("\in instantiation of& declared#",
1398 ("unmatched actual in instantiation of& declared#",
1403 elsif Present (Actuals) then
1405 ("too many actuals in generic instantiation", Instantiation_Node);
1409 Elmt : Elmt_Id := First_Elmt (Actual_Types);
1412 while Present (Elmt) loop
1413 Freeze_Before (I_Node, Node (Elmt));
1418 -- If there are default subprograms, normalize the tree by adding
1419 -- explicit associations for them. This is required if the instance
1420 -- appears within a generic.
1428 Elmt := First_Elmt (Default_Actuals);
1429 while Present (Elmt) loop
1430 if No (Actuals) then
1431 Actuals := New_List;
1432 Set_Generic_Associations (I_Node, Actuals);
1435 Subp := Node (Elmt);
1437 Make_Generic_Association (Sloc (Subp),
1438 Selector_Name => New_Occurrence_Of (Subp, Sloc (Subp)),
1439 Explicit_Generic_Actual_Parameter =>
1440 New_Occurrence_Of (Subp, Sloc (Subp)));
1441 Mark_Rewrite_Insertion (New_D);
1442 Append_To (Actuals, New_D);
1447 -- If this is a formal package, normalize the parameter list by adding
1448 -- explicit box associations for the formals that are covered by an
1451 if not Is_Empty_List (Default_Formals) then
1452 Append_List (Default_Formals, Formals);
1456 end Analyze_Associations;
1458 -------------------------------
1459 -- Analyze_Formal_Array_Type --
1460 -------------------------------
1462 procedure Analyze_Formal_Array_Type
1463 (T : in out Entity_Id;
1469 -- Treated like a non-generic array declaration, with additional
1474 if Nkind (Def) = N_Constrained_Array_Definition then
1475 DSS := First (Discrete_Subtype_Definitions (Def));
1476 while Present (DSS) loop
1477 if Nkind_In (DSS, N_Subtype_Indication,
1479 N_Attribute_Reference)
1481 Error_Msg_N ("only a subtype mark is allowed in a formal", DSS);
1488 Array_Type_Declaration (T, Def);
1489 Set_Is_Generic_Type (Base_Type (T));
1491 if Ekind (Component_Type (T)) = E_Incomplete_Type
1492 and then No (Full_View (Component_Type (T)))
1494 Error_Msg_N ("premature usage of incomplete type", Def);
1496 -- Check that range constraint is not allowed on the component type
1497 -- of a generic formal array type (AARM 12.5.3(3))
1499 elsif Is_Internal (Component_Type (T))
1500 and then Present (Subtype_Indication (Component_Definition (Def)))
1501 and then Nkind (Original_Node
1502 (Subtype_Indication (Component_Definition (Def)))) =
1503 N_Subtype_Indication
1506 ("in a formal, a subtype indication can only be "
1507 & "a subtype mark (RM 12.5.3(3))",
1508 Subtype_Indication (Component_Definition (Def)));
1511 end Analyze_Formal_Array_Type;
1513 ---------------------------------------------
1514 -- Analyze_Formal_Decimal_Fixed_Point_Type --
1515 ---------------------------------------------
1517 -- As for other generic types, we create a valid type representation with
1518 -- legal but arbitrary attributes, whose values are never considered
1519 -- static. For all scalar types we introduce an anonymous base type, with
1520 -- the same attributes. We choose the corresponding integer type to be
1521 -- Standard_Integer.
1523 procedure Analyze_Formal_Decimal_Fixed_Point_Type
1527 Loc : constant Source_Ptr := Sloc (Def);
1528 Base : constant Entity_Id :=
1530 (E_Decimal_Fixed_Point_Type,
1531 Current_Scope, Sloc (Def), 'G');
1532 Int_Base : constant Entity_Id := Standard_Integer;
1533 Delta_Val : constant Ureal := Ureal_1;
1534 Digs_Val : constant Uint := Uint_6;
1539 Set_Etype (Base, Base);
1540 Set_Size_Info (Base, Int_Base);
1541 Set_RM_Size (Base, RM_Size (Int_Base));
1542 Set_First_Rep_Item (Base, First_Rep_Item (Int_Base));
1543 Set_Digits_Value (Base, Digs_Val);
1544 Set_Delta_Value (Base, Delta_Val);
1545 Set_Small_Value (Base, Delta_Val);
1546 Set_Scalar_Range (Base,
1548 Low_Bound => Make_Real_Literal (Loc, Ureal_1),
1549 High_Bound => Make_Real_Literal (Loc, Ureal_1)));
1551 Set_Is_Generic_Type (Base);
1552 Set_Parent (Base, Parent (Def));
1554 Set_Ekind (T, E_Decimal_Fixed_Point_Subtype);
1555 Set_Etype (T, Base);
1556 Set_Size_Info (T, Int_Base);
1557 Set_RM_Size (T, RM_Size (Int_Base));
1558 Set_First_Rep_Item (T, First_Rep_Item (Int_Base));
1559 Set_Digits_Value (T, Digs_Val);
1560 Set_Delta_Value (T, Delta_Val);
1561 Set_Small_Value (T, Delta_Val);
1562 Set_Scalar_Range (T, Scalar_Range (Base));
1563 Set_Is_Constrained (T);
1565 Check_Restriction (No_Fixed_Point, Def);
1566 end Analyze_Formal_Decimal_Fixed_Point_Type;
1568 -------------------------------------------
1569 -- Analyze_Formal_Derived_Interface_Type --
1570 -------------------------------------------
1572 procedure Analyze_Formal_Derived_Interface_Type
1577 Loc : constant Source_Ptr := Sloc (Def);
1580 -- Rewrite as a type declaration of a derived type. This ensures that
1581 -- the interface list and primitive operations are properly captured.
1584 Make_Full_Type_Declaration (Loc,
1585 Defining_Identifier => T,
1586 Type_Definition => Def));
1588 Set_Is_Generic_Type (T);
1589 end Analyze_Formal_Derived_Interface_Type;
1591 ---------------------------------
1592 -- Analyze_Formal_Derived_Type --
1593 ---------------------------------
1595 procedure Analyze_Formal_Derived_Type
1600 Loc : constant Source_Ptr := Sloc (Def);
1601 Unk_Disc : constant Boolean := Unknown_Discriminants_Present (N);
1605 Set_Is_Generic_Type (T);
1607 if Private_Present (Def) then
1609 Make_Private_Extension_Declaration (Loc,
1610 Defining_Identifier => T,
1611 Discriminant_Specifications => Discriminant_Specifications (N),
1612 Unknown_Discriminants_Present => Unk_Disc,
1613 Subtype_Indication => Subtype_Mark (Def),
1614 Interface_List => Interface_List (Def));
1616 Set_Abstract_Present (New_N, Abstract_Present (Def));
1617 Set_Limited_Present (New_N, Limited_Present (Def));
1618 Set_Synchronized_Present (New_N, Synchronized_Present (Def));
1622 Make_Full_Type_Declaration (Loc,
1623 Defining_Identifier => T,
1624 Discriminant_Specifications =>
1625 Discriminant_Specifications (Parent (T)),
1627 Make_Derived_Type_Definition (Loc,
1628 Subtype_Indication => Subtype_Mark (Def)));
1630 Set_Abstract_Present
1631 (Type_Definition (New_N), Abstract_Present (Def));
1633 (Type_Definition (New_N), Limited_Present (Def));
1640 if not Is_Composite_Type (T) then
1642 ("unknown discriminants not allowed for elementary types", N);
1644 Set_Has_Unknown_Discriminants (T);
1645 Set_Is_Constrained (T, False);
1649 -- If the parent type has a known size, so does the formal, which makes
1650 -- legal representation clauses that involve the formal.
1652 Set_Size_Known_At_Compile_Time
1653 (T, Size_Known_At_Compile_Time (Entity (Subtype_Mark (Def))));
1654 end Analyze_Formal_Derived_Type;
1656 ----------------------------------
1657 -- Analyze_Formal_Discrete_Type --
1658 ----------------------------------
1660 -- The operations defined for a discrete types are those of an enumeration
1661 -- type. The size is set to an arbitrary value, for use in analyzing the
1664 procedure Analyze_Formal_Discrete_Type (T : Entity_Id; Def : Node_Id) is
1665 Loc : constant Source_Ptr := Sloc (Def);
1669 Base : constant Entity_Id :=
1671 (E_Floating_Point_Type, Current_Scope, Sloc (Def), 'G');
1674 Set_Ekind (T, E_Enumeration_Subtype);
1675 Set_Etype (T, Base);
1678 Set_Is_Generic_Type (T);
1679 Set_Is_Constrained (T);
1681 -- For semantic analysis, the bounds of the type must be set to some
1682 -- non-static value. The simplest is to create attribute nodes for those
1683 -- bounds, that refer to the type itself. These bounds are never
1684 -- analyzed but serve as place-holders.
1687 Make_Attribute_Reference (Loc,
1688 Attribute_Name => Name_First,
1689 Prefix => New_Reference_To (T, Loc));
1693 Make_Attribute_Reference (Loc,
1694 Attribute_Name => Name_Last,
1695 Prefix => New_Reference_To (T, Loc));
1698 Set_Scalar_Range (T,
1703 Set_Ekind (Base, E_Enumeration_Type);
1704 Set_Etype (Base, Base);
1705 Init_Size (Base, 8);
1706 Init_Alignment (Base);
1707 Set_Is_Generic_Type (Base);
1708 Set_Scalar_Range (Base, Scalar_Range (T));
1709 Set_Parent (Base, Parent (Def));
1710 end Analyze_Formal_Discrete_Type;
1712 ----------------------------------
1713 -- Analyze_Formal_Floating_Type --
1714 ---------------------------------
1716 procedure Analyze_Formal_Floating_Type (T : Entity_Id; Def : Node_Id) is
1717 Base : constant Entity_Id :=
1719 (E_Floating_Point_Type, Current_Scope, Sloc (Def), 'G');
1722 -- The various semantic attributes are taken from the predefined type
1723 -- Float, just so that all of them are initialized. Their values are
1724 -- never used because no constant folding or expansion takes place in
1725 -- the generic itself.
1728 Set_Ekind (T, E_Floating_Point_Subtype);
1729 Set_Etype (T, Base);
1730 Set_Size_Info (T, (Standard_Float));
1731 Set_RM_Size (T, RM_Size (Standard_Float));
1732 Set_Digits_Value (T, Digits_Value (Standard_Float));
1733 Set_Scalar_Range (T, Scalar_Range (Standard_Float));
1734 Set_Is_Constrained (T);
1736 Set_Is_Generic_Type (Base);
1737 Set_Etype (Base, Base);
1738 Set_Size_Info (Base, (Standard_Float));
1739 Set_RM_Size (Base, RM_Size (Standard_Float));
1740 Set_Digits_Value (Base, Digits_Value (Standard_Float));
1741 Set_Scalar_Range (Base, Scalar_Range (Standard_Float));
1742 Set_Parent (Base, Parent (Def));
1744 Check_Restriction (No_Floating_Point, Def);
1745 end Analyze_Formal_Floating_Type;
1747 -----------------------------------
1748 -- Analyze_Formal_Interface_Type;--
1749 -----------------------------------
1751 procedure Analyze_Formal_Interface_Type
1756 Loc : constant Source_Ptr := Sloc (N);
1761 Make_Full_Type_Declaration (Loc,
1762 Defining_Identifier => T,
1763 Type_Definition => Def);
1767 Set_Is_Generic_Type (T);
1768 end Analyze_Formal_Interface_Type;
1770 ---------------------------------
1771 -- Analyze_Formal_Modular_Type --
1772 ---------------------------------
1774 procedure Analyze_Formal_Modular_Type (T : Entity_Id; Def : Node_Id) is
1776 -- Apart from their entity kind, generic modular types are treated like
1777 -- signed integer types, and have the same attributes.
1779 Analyze_Formal_Signed_Integer_Type (T, Def);
1780 Set_Ekind (T, E_Modular_Integer_Subtype);
1781 Set_Ekind (Etype (T), E_Modular_Integer_Type);
1783 end Analyze_Formal_Modular_Type;
1785 ---------------------------------------
1786 -- Analyze_Formal_Object_Declaration --
1787 ---------------------------------------
1789 procedure Analyze_Formal_Object_Declaration (N : Node_Id) is
1790 E : constant Node_Id := Default_Expression (N);
1791 Id : constant Node_Id := Defining_Identifier (N);
1798 -- Determine the mode of the formal object
1800 if Out_Present (N) then
1801 K := E_Generic_In_Out_Parameter;
1803 if not In_Present (N) then
1804 Error_Msg_N ("formal generic objects cannot have mode OUT", N);
1808 K := E_Generic_In_Parameter;
1811 if Present (Subtype_Mark (N)) then
1812 Find_Type (Subtype_Mark (N));
1813 T := Entity (Subtype_Mark (N));
1815 -- Verify that there is no redundant null exclusion
1817 if Null_Exclusion_Present (N) then
1818 if not Is_Access_Type (T) then
1820 ("null exclusion can only apply to an access type", N);
1822 elsif Can_Never_Be_Null (T) then
1824 ("`NOT NULL` not allowed (& already excludes null)",
1829 -- Ada 2005 (AI-423): Formal object with an access definition
1832 Check_Access_Definition (N);
1833 T := Access_Definition
1835 N => Access_Definition (N));
1838 if Ekind (T) = E_Incomplete_Type then
1840 Error_Node : Node_Id;
1843 if Present (Subtype_Mark (N)) then
1844 Error_Node := Subtype_Mark (N);
1846 Check_Access_Definition (N);
1847 Error_Node := Access_Definition (N);
1850 Error_Msg_N ("premature usage of incomplete type", Error_Node);
1854 if K = E_Generic_In_Parameter then
1856 -- Ada 2005 (AI-287): Limited aggregates allowed in generic formals
1858 if Ada_Version < Ada_05 and then Is_Limited_Type (T) then
1860 ("generic formal of mode IN must not be of limited type", N);
1861 Explain_Limited_Type (T, N);
1864 if Is_Abstract_Type (T) then
1866 ("generic formal of mode IN must not be of abstract type", N);
1870 Preanalyze_Spec_Expression (E, T);
1872 if Is_Limited_Type (T) and then not OK_For_Limited_Init (E) then
1874 ("initialization not allowed for limited types", E);
1875 Explain_Limited_Type (T, E);
1882 -- Case of generic IN OUT parameter
1885 -- If the formal has an unconstrained type, construct its actual
1886 -- subtype, as is done for subprogram formals. In this fashion, all
1887 -- its uses can refer to specific bounds.
1892 if (Is_Array_Type (T)
1893 and then not Is_Constrained (T))
1895 (Ekind (T) = E_Record_Type
1896 and then Has_Discriminants (T))
1899 Non_Freezing_Ref : constant Node_Id :=
1900 New_Reference_To (Id, Sloc (Id));
1904 -- Make sure the actual subtype doesn't generate bogus freezing
1906 Set_Must_Not_Freeze (Non_Freezing_Ref);
1907 Decl := Build_Actual_Subtype (T, Non_Freezing_Ref);
1908 Insert_Before_And_Analyze (N, Decl);
1909 Set_Actual_Subtype (Id, Defining_Identifier (Decl));
1912 Set_Actual_Subtype (Id, T);
1917 ("initialization not allowed for `IN OUT` formals", N);
1921 end Analyze_Formal_Object_Declaration;
1923 ----------------------------------------------
1924 -- Analyze_Formal_Ordinary_Fixed_Point_Type --
1925 ----------------------------------------------
1927 procedure Analyze_Formal_Ordinary_Fixed_Point_Type
1931 Loc : constant Source_Ptr := Sloc (Def);
1932 Base : constant Entity_Id :=
1934 (E_Ordinary_Fixed_Point_Type, Current_Scope, Sloc (Def), 'G');
1936 -- The semantic attributes are set for completeness only, their values
1937 -- will never be used, since all properties of the type are non-static.
1940 Set_Ekind (T, E_Ordinary_Fixed_Point_Subtype);
1941 Set_Etype (T, Base);
1942 Set_Size_Info (T, Standard_Integer);
1943 Set_RM_Size (T, RM_Size (Standard_Integer));
1944 Set_Small_Value (T, Ureal_1);
1945 Set_Delta_Value (T, Ureal_1);
1946 Set_Scalar_Range (T,
1948 Low_Bound => Make_Real_Literal (Loc, Ureal_1),
1949 High_Bound => Make_Real_Literal (Loc, Ureal_1)));
1950 Set_Is_Constrained (T);
1952 Set_Is_Generic_Type (Base);
1953 Set_Etype (Base, Base);
1954 Set_Size_Info (Base, Standard_Integer);
1955 Set_RM_Size (Base, RM_Size (Standard_Integer));
1956 Set_Small_Value (Base, Ureal_1);
1957 Set_Delta_Value (Base, Ureal_1);
1958 Set_Scalar_Range (Base, Scalar_Range (T));
1959 Set_Parent (Base, Parent (Def));
1961 Check_Restriction (No_Fixed_Point, Def);
1962 end Analyze_Formal_Ordinary_Fixed_Point_Type;
1964 ----------------------------
1965 -- Analyze_Formal_Package --
1966 ----------------------------
1968 procedure Analyze_Formal_Package (N : Node_Id) is
1969 Loc : constant Source_Ptr := Sloc (N);
1970 Pack_Id : constant Entity_Id := Defining_Identifier (N);
1972 Gen_Id : constant Node_Id := Name (N);
1974 Gen_Unit : Entity_Id;
1976 Parent_Installed : Boolean := False;
1978 Parent_Instance : Entity_Id;
1979 Renaming_In_Par : Entity_Id;
1980 No_Associations : Boolean := False;
1982 function Build_Local_Package return Node_Id;
1983 -- The formal package is rewritten so that its parameters are replaced
1984 -- with corresponding declarations. For parameters with bona fide
1985 -- associations these declarations are created by Analyze_Associations
1986 -- as for a regular instantiation. For boxed parameters, we preserve
1987 -- the formal declarations and analyze them, in order to introduce
1988 -- entities of the right kind in the environment of the formal.
1990 -------------------------
1991 -- Build_Local_Package --
1992 -------------------------
1994 function Build_Local_Package return Node_Id is
1996 Pack_Decl : Node_Id;
1999 -- Within the formal, the name of the generic package is a renaming
2000 -- of the formal (as for a regular instantiation).
2003 Make_Package_Declaration (Loc,
2006 (Specification (Original_Node (Gen_Decl)),
2007 Empty, Instantiating => True));
2009 Renaming := Make_Package_Renaming_Declaration (Loc,
2010 Defining_Unit_Name =>
2011 Make_Defining_Identifier (Loc, Chars (Gen_Unit)),
2012 Name => New_Occurrence_Of (Formal, Loc));
2014 if Nkind (Gen_Id) = N_Identifier
2015 and then Chars (Gen_Id) = Chars (Pack_Id)
2018 ("& is hidden within declaration of instance", Gen_Id, Gen_Unit);
2021 -- If the formal is declared with a box, or with an others choice,
2022 -- create corresponding declarations for all entities in the formal
2023 -- part, so that names with the proper types are available in the
2024 -- specification of the formal package.
2025 -- On the other hand, if there are no associations, then all the
2026 -- formals must have defaults, and this will be checked by the
2027 -- call to Analyze_Associations.
2030 or else Nkind (First (Generic_Associations (N))) = N_Others_Choice
2033 Formal_Decl : Node_Id;
2036 -- TBA : for a formal package, need to recurse ???
2041 (Generic_Formal_Declarations (Original_Node (Gen_Decl)));
2042 while Present (Formal_Decl) loop
2044 (Decls, Copy_Generic_Node (Formal_Decl, Empty, True));
2049 -- If generic associations are present, use Analyze_Associations to
2050 -- create the proper renaming declarations.
2054 Act_Tree : constant Node_Id :=
2056 (Original_Node (Gen_Decl), Empty,
2057 Instantiating => True);
2060 Generic_Renamings.Set_Last (0);
2061 Generic_Renamings_HTable.Reset;
2062 Instantiation_Node := N;
2065 Analyze_Associations
2067 Generic_Formal_Declarations (Act_Tree),
2068 Generic_Formal_Declarations (Gen_Decl));
2072 Append (Renaming, To => Decls);
2074 -- Add generated declarations ahead of local declarations in
2077 if No (Visible_Declarations (Specification (Pack_Decl))) then
2078 Set_Visible_Declarations (Specification (Pack_Decl), Decls);
2081 (First (Visible_Declarations (Specification (Pack_Decl))),
2086 end Build_Local_Package;
2088 -- Start of processing for Analyze_Formal_Package
2091 Text_IO_Kludge (Gen_Id);
2094 Check_Generic_Child_Unit (Gen_Id, Parent_Installed);
2095 Gen_Unit := Entity (Gen_Id);
2097 -- Check for a formal package that is a package renaming
2099 if Present (Renamed_Object (Gen_Unit)) then
2100 Gen_Unit := Renamed_Object (Gen_Unit);
2103 if Ekind (Gen_Unit) /= E_Generic_Package then
2104 Error_Msg_N ("expect generic package name", Gen_Id);
2108 elsif Gen_Unit = Current_Scope then
2110 ("generic package cannot be used as a formal package of itself",
2115 elsif In_Open_Scopes (Gen_Unit) then
2116 if Is_Compilation_Unit (Gen_Unit)
2117 and then Is_Child_Unit (Current_Scope)
2119 -- Special-case the error when the formal is a parent, and
2120 -- continue analysis to minimize cascaded errors.
2123 ("generic parent cannot be used as formal package "
2124 & "of a child unit",
2129 ("generic package cannot be used as a formal package "
2138 or else No (Generic_Associations (N))
2139 or else Nkind (First (Generic_Associations (N))) = N_Others_Choice
2141 No_Associations := True;
2144 -- If there are no generic associations, the generic parameters appear
2145 -- as local entities and are instantiated like them. We copy the generic
2146 -- package declaration as if it were an instantiation, and analyze it
2147 -- like a regular package, except that we treat the formals as
2148 -- additional visible components.
2150 Gen_Decl := Unit_Declaration_Node (Gen_Unit);
2152 if In_Extended_Main_Source_Unit (N) then
2153 Set_Is_Instantiated (Gen_Unit);
2154 Generate_Reference (Gen_Unit, N);
2157 Formal := New_Copy (Pack_Id);
2158 Create_Instantiation_Source (N, Gen_Unit, False, S_Adjustment);
2161 -- Make local generic without formals. The formals will be replaced
2162 -- with internal declarations.
2164 New_N := Build_Local_Package;
2166 -- If there are errors in the parameter list, Analyze_Associations
2167 -- raises Instantiation_Error. Patch the declaration to prevent
2168 -- further exception propagation.
2171 when Instantiation_Error =>
2173 Enter_Name (Formal);
2174 Set_Ekind (Formal, E_Variable);
2175 Set_Etype (Formal, Any_Type);
2177 if Parent_Installed then
2185 Set_Defining_Unit_Name (Specification (New_N), Formal);
2186 Set_Generic_Parent (Specification (N), Gen_Unit);
2187 Set_Instance_Env (Gen_Unit, Formal);
2188 Set_Is_Generic_Instance (Formal);
2190 Enter_Name (Formal);
2191 Set_Ekind (Formal, E_Package);
2192 Set_Etype (Formal, Standard_Void_Type);
2193 Set_Inner_Instances (Formal, New_Elmt_List);
2194 Push_Scope (Formal);
2196 if Is_Child_Unit (Gen_Unit)
2197 and then Parent_Installed
2199 -- Similarly, we have to make the name of the formal visible in the
2200 -- parent instance, to resolve properly fully qualified names that
2201 -- may appear in the generic unit. The parent instance has been
2202 -- placed on the scope stack ahead of the current scope.
2204 Parent_Instance := Scope_Stack.Table (Scope_Stack.Last - 1).Entity;
2207 Make_Defining_Identifier (Loc, Chars (Gen_Unit));
2208 Set_Ekind (Renaming_In_Par, E_Package);
2209 Set_Etype (Renaming_In_Par, Standard_Void_Type);
2210 Set_Scope (Renaming_In_Par, Parent_Instance);
2211 Set_Parent (Renaming_In_Par, Parent (Formal));
2212 Set_Renamed_Object (Renaming_In_Par, Formal);
2213 Append_Entity (Renaming_In_Par, Parent_Instance);
2216 Analyze (Specification (N));
2218 -- The formals for which associations are provided are not visible
2219 -- outside of the formal package. The others are still declared by a
2220 -- formal parameter declaration.
2222 if not No_Associations then
2227 E := First_Entity (Formal);
2228 while Present (E) loop
2229 exit when Ekind (E) = E_Package
2230 and then Renamed_Entity (E) = Formal;
2232 if not Is_Generic_Formal (E) then
2241 End_Package_Scope (Formal);
2243 if Parent_Installed then
2249 -- Inside the generic unit, the formal package is a regular package, but
2250 -- no body is needed for it. Note that after instantiation, the defining
2251 -- unit name we need is in the new tree and not in the original (see
2252 -- Package_Instantiation). A generic formal package is an instance, and
2253 -- can be used as an actual for an inner instance.
2255 Set_Has_Completion (Formal, True);
2257 -- Add semantic information to the original defining identifier.
2260 Set_Ekind (Pack_Id, E_Package);
2261 Set_Etype (Pack_Id, Standard_Void_Type);
2262 Set_Scope (Pack_Id, Scope (Formal));
2263 Set_Has_Completion (Pack_Id, True);
2264 end Analyze_Formal_Package;
2266 ---------------------------------
2267 -- Analyze_Formal_Private_Type --
2268 ---------------------------------
2270 procedure Analyze_Formal_Private_Type
2276 New_Private_Type (N, T, Def);
2278 -- Set the size to an arbitrary but legal value
2280 Set_Size_Info (T, Standard_Integer);
2281 Set_RM_Size (T, RM_Size (Standard_Integer));
2282 end Analyze_Formal_Private_Type;
2284 ----------------------------------------
2285 -- Analyze_Formal_Signed_Integer_Type --
2286 ----------------------------------------
2288 procedure Analyze_Formal_Signed_Integer_Type
2292 Base : constant Entity_Id :=
2294 (E_Signed_Integer_Type, Current_Scope, Sloc (Def), 'G');
2299 Set_Ekind (T, E_Signed_Integer_Subtype);
2300 Set_Etype (T, Base);
2301 Set_Size_Info (T, Standard_Integer);
2302 Set_RM_Size (T, RM_Size (Standard_Integer));
2303 Set_Scalar_Range (T, Scalar_Range (Standard_Integer));
2304 Set_Is_Constrained (T);
2306 Set_Is_Generic_Type (Base);
2307 Set_Size_Info (Base, Standard_Integer);
2308 Set_RM_Size (Base, RM_Size (Standard_Integer));
2309 Set_Etype (Base, Base);
2310 Set_Scalar_Range (Base, Scalar_Range (Standard_Integer));
2311 Set_Parent (Base, Parent (Def));
2312 end Analyze_Formal_Signed_Integer_Type;
2314 -------------------------------
2315 -- Analyze_Formal_Subprogram --
2316 -------------------------------
2318 procedure Analyze_Formal_Subprogram (N : Node_Id) is
2319 Spec : constant Node_Id := Specification (N);
2320 Def : constant Node_Id := Default_Name (N);
2321 Nam : constant Entity_Id := Defining_Unit_Name (Spec);
2329 if Nkind (Nam) = N_Defining_Program_Unit_Name then
2330 Error_Msg_N ("name of formal subprogram must be a direct name", Nam);
2334 Analyze_Subprogram_Declaration (N);
2335 Set_Is_Formal_Subprogram (Nam);
2336 Set_Has_Completion (Nam);
2338 if Nkind (N) = N_Formal_Abstract_Subprogram_Declaration then
2339 Set_Is_Abstract_Subprogram (Nam);
2340 Set_Is_Dispatching_Operation (Nam);
2343 Ctrl_Type : constant Entity_Id := Find_Dispatching_Type (Nam);
2345 if No (Ctrl_Type) then
2347 ("abstract formal subprogram must have a controlling type",
2350 Check_Controlling_Formals (Ctrl_Type, Nam);
2355 -- Default name is resolved at the point of instantiation
2357 if Box_Present (N) then
2360 -- Else default is bound at the point of generic declaration
2362 elsif Present (Def) then
2363 if Nkind (Def) = N_Operator_Symbol then
2364 Find_Direct_Name (Def);
2366 elsif Nkind (Def) /= N_Attribute_Reference then
2370 -- For an attribute reference, analyze the prefix and verify
2371 -- that it has the proper profile for the subprogram.
2373 Analyze (Prefix (Def));
2374 Valid_Default_Attribute (Nam, Def);
2378 -- Default name may be overloaded, in which case the interpretation
2379 -- with the correct profile must be selected, as for a renaming.
2380 -- If the definition is an indexed component, it must denote a
2381 -- member of an entry family. If it is a selected component, it
2382 -- can be a protected operation.
2384 if Etype (Def) = Any_Type then
2387 elsif Nkind (Def) = N_Selected_Component then
2388 if not Is_Overloadable (Entity (Selector_Name (Def))) then
2389 Error_Msg_N ("expect valid subprogram name as default", Def);
2392 elsif Nkind (Def) = N_Indexed_Component then
2393 if Is_Entity_Name (Prefix (Def)) then
2394 if Ekind (Entity (Prefix (Def))) /= E_Entry_Family then
2395 Error_Msg_N ("expect valid subprogram name as default", Def);
2398 elsif Nkind (Prefix (Def)) = N_Selected_Component then
2399 if Ekind (Entity (Selector_Name (Prefix (Def))))
2402 Error_Msg_N ("expect valid subprogram name as default", Def);
2406 Error_Msg_N ("expect valid subprogram name as default", Def);
2410 elsif Nkind (Def) = N_Character_Literal then
2412 -- Needs some type checks: subprogram should be parameterless???
2414 Resolve (Def, (Etype (Nam)));
2416 elsif not Is_Entity_Name (Def)
2417 or else not Is_Overloadable (Entity (Def))
2419 Error_Msg_N ("expect valid subprogram name as default", Def);
2422 elsif not Is_Overloaded (Def) then
2423 Subp := Entity (Def);
2426 Error_Msg_N ("premature usage of formal subprogram", Def);
2428 elsif not Entity_Matches_Spec (Subp, Nam) then
2429 Error_Msg_N ("no visible entity matches specification", Def);
2432 -- More than one interpretation, so disambiguate as for a renaming
2437 I1 : Interp_Index := 0;
2443 Get_First_Interp (Def, I, It);
2444 while Present (It.Nam) loop
2445 if Entity_Matches_Spec (It.Nam, Nam) then
2446 if Subp /= Any_Id then
2447 It1 := Disambiguate (Def, I1, I, Etype (Subp));
2449 if It1 = No_Interp then
2450 Error_Msg_N ("ambiguous default subprogram", Def);
2463 Get_Next_Interp (I, It);
2467 if Subp /= Any_Id then
2468 Set_Entity (Def, Subp);
2471 Error_Msg_N ("premature usage of formal subprogram", Def);
2473 elsif Ekind (Subp) /= E_Operator then
2474 Check_Mode_Conformant (Subp, Nam);
2478 Error_Msg_N ("no visible subprogram matches specification", N);
2482 end Analyze_Formal_Subprogram;
2484 -------------------------------------
2485 -- Analyze_Formal_Type_Declaration --
2486 -------------------------------------
2488 procedure Analyze_Formal_Type_Declaration (N : Node_Id) is
2489 Def : constant Node_Id := Formal_Type_Definition (N);
2493 T := Defining_Identifier (N);
2495 if Present (Discriminant_Specifications (N))
2496 and then Nkind (Def) /= N_Formal_Private_Type_Definition
2499 ("discriminants not allowed for this formal type", T);
2502 -- Enter the new name, and branch to specific routine
2505 when N_Formal_Private_Type_Definition =>
2506 Analyze_Formal_Private_Type (N, T, Def);
2508 when N_Formal_Derived_Type_Definition =>
2509 Analyze_Formal_Derived_Type (N, T, Def);
2511 when N_Formal_Discrete_Type_Definition =>
2512 Analyze_Formal_Discrete_Type (T, Def);
2514 when N_Formal_Signed_Integer_Type_Definition =>
2515 Analyze_Formal_Signed_Integer_Type (T, Def);
2517 when N_Formal_Modular_Type_Definition =>
2518 Analyze_Formal_Modular_Type (T, Def);
2520 when N_Formal_Floating_Point_Definition =>
2521 Analyze_Formal_Floating_Type (T, Def);
2523 when N_Formal_Ordinary_Fixed_Point_Definition =>
2524 Analyze_Formal_Ordinary_Fixed_Point_Type (T, Def);
2526 when N_Formal_Decimal_Fixed_Point_Definition =>
2527 Analyze_Formal_Decimal_Fixed_Point_Type (T, Def);
2529 when N_Array_Type_Definition =>
2530 Analyze_Formal_Array_Type (T, Def);
2532 when N_Access_To_Object_Definition |
2533 N_Access_Function_Definition |
2534 N_Access_Procedure_Definition =>
2535 Analyze_Generic_Access_Type (T, Def);
2537 -- Ada 2005: a interface declaration is encoded as an abstract
2538 -- record declaration or a abstract type derivation.
2540 when N_Record_Definition =>
2541 Analyze_Formal_Interface_Type (N, T, Def);
2543 when N_Derived_Type_Definition =>
2544 Analyze_Formal_Derived_Interface_Type (N, T, Def);
2550 raise Program_Error;
2554 Set_Is_Generic_Type (T);
2555 end Analyze_Formal_Type_Declaration;
2557 ------------------------------------
2558 -- Analyze_Function_Instantiation --
2559 ------------------------------------
2561 procedure Analyze_Function_Instantiation (N : Node_Id) is
2563 Analyze_Subprogram_Instantiation (N, E_Function);
2564 end Analyze_Function_Instantiation;
2566 ---------------------------------
2567 -- Analyze_Generic_Access_Type --
2568 ---------------------------------
2570 procedure Analyze_Generic_Access_Type (T : Entity_Id; Def : Node_Id) is
2574 if Nkind (Def) = N_Access_To_Object_Definition then
2575 Access_Type_Declaration (T, Def);
2577 if Is_Incomplete_Or_Private_Type (Designated_Type (T))
2578 and then No (Full_View (Designated_Type (T)))
2579 and then not Is_Generic_Type (Designated_Type (T))
2581 Error_Msg_N ("premature usage of incomplete type", Def);
2583 elsif Is_Internal (Designated_Type (T)) then
2585 ("only a subtype mark is allowed in a formal", Def);
2589 Access_Subprogram_Declaration (T, Def);
2591 end Analyze_Generic_Access_Type;
2593 ---------------------------------
2594 -- Analyze_Generic_Formal_Part --
2595 ---------------------------------
2597 procedure Analyze_Generic_Formal_Part (N : Node_Id) is
2598 Gen_Parm_Decl : Node_Id;
2601 -- The generic formals are processed in the scope of the generic unit,
2602 -- where they are immediately visible. The scope is installed by the
2605 Gen_Parm_Decl := First (Generic_Formal_Declarations (N));
2607 while Present (Gen_Parm_Decl) loop
2608 Analyze (Gen_Parm_Decl);
2609 Next (Gen_Parm_Decl);
2612 Generate_Reference_To_Generic_Formals (Current_Scope);
2613 end Analyze_Generic_Formal_Part;
2615 ------------------------------------------
2616 -- Analyze_Generic_Package_Declaration --
2617 ------------------------------------------
2619 procedure Analyze_Generic_Package_Declaration (N : Node_Id) is
2620 Loc : constant Source_Ptr := Sloc (N);
2623 Save_Parent : Node_Id;
2625 Decls : constant List_Id :=
2626 Visible_Declarations (Specification (N));
2630 -- We introduce a renaming of the enclosing package, to have a usable
2631 -- entity as the prefix of an expanded name for a local entity of the
2632 -- form Par.P.Q, where P is the generic package. This is because a local
2633 -- entity named P may hide it, so that the usual visibility rules in
2634 -- the instance will not resolve properly.
2637 Make_Package_Renaming_Declaration (Loc,
2638 Defining_Unit_Name =>
2639 Make_Defining_Identifier (Loc,
2640 Chars => New_External_Name (Chars (Defining_Entity (N)), "GH")),
2641 Name => Make_Identifier (Loc, Chars (Defining_Entity (N))));
2643 if Present (Decls) then
2644 Decl := First (Decls);
2645 while Present (Decl)
2646 and then Nkind (Decl) = N_Pragma
2651 if Present (Decl) then
2652 Insert_Before (Decl, Renaming);
2654 Append (Renaming, Visible_Declarations (Specification (N)));
2658 Set_Visible_Declarations (Specification (N), New_List (Renaming));
2661 -- Create copy of generic unit, and save for instantiation. If the unit
2662 -- is a child unit, do not copy the specifications for the parent, which
2663 -- are not part of the generic tree.
2665 Save_Parent := Parent_Spec (N);
2666 Set_Parent_Spec (N, Empty);
2668 New_N := Copy_Generic_Node (N, Empty, Instantiating => False);
2669 Set_Parent_Spec (New_N, Save_Parent);
2671 Id := Defining_Entity (N);
2672 Generate_Definition (Id);
2674 -- Expansion is not applied to generic units
2679 Set_Ekind (Id, E_Generic_Package);
2680 Set_Etype (Id, Standard_Void_Type);
2682 Enter_Generic_Scope (Id);
2683 Set_Inner_Instances (Id, New_Elmt_List);
2685 Set_Categorization_From_Pragmas (N);
2686 Set_Is_Pure (Id, Is_Pure (Current_Scope));
2688 -- Link the declaration of the generic homonym in the generic copy to
2689 -- the package it renames, so that it is always resolved properly.
2691 Set_Generic_Homonym (Id, Defining_Unit_Name (Renaming));
2692 Set_Entity (Associated_Node (Name (Renaming)), Id);
2694 -- For a library unit, we have reconstructed the entity for the unit,
2695 -- and must reset it in the library tables.
2697 if Nkind (Parent (N)) = N_Compilation_Unit then
2698 Set_Cunit_Entity (Current_Sem_Unit, Id);
2701 Analyze_Generic_Formal_Part (N);
2703 -- After processing the generic formals, analysis proceeds as for a
2704 -- non-generic package.
2706 Analyze (Specification (N));
2708 Validate_Categorization_Dependency (N, Id);
2712 End_Package_Scope (Id);
2713 Exit_Generic_Scope (Id);
2715 if Nkind (Parent (N)) /= N_Compilation_Unit then
2716 Move_Freeze_Nodes (Id, N, Visible_Declarations (Specification (N)));
2717 Move_Freeze_Nodes (Id, N, Private_Declarations (Specification (N)));
2718 Move_Freeze_Nodes (Id, N, Generic_Formal_Declarations (N));
2721 Set_Body_Required (Parent (N), Unit_Requires_Body (Id));
2722 Validate_RT_RAT_Component (N);
2724 -- If this is a spec without a body, check that generic parameters
2727 if not Body_Required (Parent (N)) then
2728 Check_References (Id);
2731 end Analyze_Generic_Package_Declaration;
2733 --------------------------------------------
2734 -- Analyze_Generic_Subprogram_Declaration --
2735 --------------------------------------------
2737 procedure Analyze_Generic_Subprogram_Declaration (N : Node_Id) is
2742 Result_Type : Entity_Id;
2743 Save_Parent : Node_Id;
2747 -- Create copy of generic unit, and save for instantiation. If the unit
2748 -- is a child unit, do not copy the specifications for the parent, which
2749 -- are not part of the generic tree.
2751 Save_Parent := Parent_Spec (N);
2752 Set_Parent_Spec (N, Empty);
2754 New_N := Copy_Generic_Node (N, Empty, Instantiating => False);
2755 Set_Parent_Spec (New_N, Save_Parent);
2758 Spec := Specification (N);
2759 Id := Defining_Entity (Spec);
2760 Generate_Definition (Id);
2762 if Nkind (Id) = N_Defining_Operator_Symbol then
2764 ("operator symbol not allowed for generic subprogram", Id);
2771 Set_Scope_Depth_Value (Id, Scope_Depth (Current_Scope) + 1);
2773 Enter_Generic_Scope (Id);
2774 Set_Inner_Instances (Id, New_Elmt_List);
2775 Set_Is_Pure (Id, Is_Pure (Current_Scope));
2777 Analyze_Generic_Formal_Part (N);
2779 Formals := Parameter_Specifications (Spec);
2781 if Present (Formals) then
2782 Process_Formals (Formals, Spec);
2785 if Nkind (Spec) = N_Function_Specification then
2786 Set_Ekind (Id, E_Generic_Function);
2788 if Nkind (Result_Definition (Spec)) = N_Access_Definition then
2789 Result_Type := Access_Definition (Spec, Result_Definition (Spec));
2790 Set_Etype (Id, Result_Type);
2792 Find_Type (Result_Definition (Spec));
2793 Typ := Entity (Result_Definition (Spec));
2795 -- If a null exclusion is imposed on the result type, then create
2796 -- a null-excluding itype (an access subtype) and use it as the
2797 -- function's Etype.
2799 if Is_Access_Type (Typ)
2800 and then Null_Exclusion_Present (Spec)
2803 Create_Null_Excluding_Itype
2805 Related_Nod => Spec,
2806 Scope_Id => Defining_Unit_Name (Spec)));
2808 Set_Etype (Id, Typ);
2813 Set_Ekind (Id, E_Generic_Procedure);
2814 Set_Etype (Id, Standard_Void_Type);
2817 -- For a library unit, we have reconstructed the entity for the unit,
2818 -- and must reset it in the library tables. We also make sure that
2819 -- Body_Required is set properly in the original compilation unit node.
2821 if Nkind (Parent (N)) = N_Compilation_Unit then
2822 Set_Cunit_Entity (Current_Sem_Unit, Id);
2823 Set_Body_Required (Parent (N), Unit_Requires_Body (Id));
2826 Set_Categorization_From_Pragmas (N);
2827 Validate_Categorization_Dependency (N, Id);
2829 Save_Global_References (Original_Node (N));
2833 Exit_Generic_Scope (Id);
2834 Generate_Reference_To_Formals (Id);
2835 end Analyze_Generic_Subprogram_Declaration;
2837 -----------------------------------
2838 -- Analyze_Package_Instantiation --
2839 -----------------------------------
2841 procedure Analyze_Package_Instantiation (N : Node_Id) is
2842 Loc : constant Source_Ptr := Sloc (N);
2843 Gen_Id : constant Node_Id := Name (N);
2846 Act_Decl_Name : Node_Id;
2847 Act_Decl_Id : Entity_Id;
2852 Gen_Unit : Entity_Id;
2854 Is_Actual_Pack : constant Boolean :=
2855 Is_Internal (Defining_Entity (N));
2857 Env_Installed : Boolean := False;
2858 Parent_Installed : Boolean := False;
2859 Renaming_List : List_Id;
2860 Unit_Renaming : Node_Id;
2861 Needs_Body : Boolean;
2862 Inline_Now : Boolean := False;
2864 procedure Delay_Descriptors (E : Entity_Id);
2865 -- Delay generation of subprogram descriptors for given entity
2867 function Might_Inline_Subp return Boolean;
2868 -- If inlining is active and the generic contains inlined subprograms,
2869 -- we instantiate the body. This may cause superfluous instantiations,
2870 -- but it is simpler than detecting the need for the body at the point
2871 -- of inlining, when the context of the instance is not available.
2873 -----------------------
2874 -- Delay_Descriptors --
2875 -----------------------
2877 procedure Delay_Descriptors (E : Entity_Id) is
2879 if not Delay_Subprogram_Descriptors (E) then
2880 Set_Delay_Subprogram_Descriptors (E);
2881 Pending_Descriptor.Append (E);
2883 end Delay_Descriptors;
2885 -----------------------
2886 -- Might_Inline_Subp --
2887 -----------------------
2889 function Might_Inline_Subp return Boolean is
2893 if not Inline_Processing_Required then
2897 E := First_Entity (Gen_Unit);
2898 while Present (E) loop
2899 if Is_Subprogram (E)
2900 and then Is_Inlined (E)
2910 end Might_Inline_Subp;
2912 -- Start of processing for Analyze_Package_Instantiation
2915 -- Very first thing: apply the special kludge for Text_IO processing
2916 -- in case we are instantiating one of the children of [Wide_]Text_IO.
2918 Text_IO_Kludge (Name (N));
2920 -- Make node global for error reporting
2922 Instantiation_Node := N;
2924 -- Case of instantiation of a generic package
2926 if Nkind (N) = N_Package_Instantiation then
2927 Act_Decl_Id := New_Copy (Defining_Entity (N));
2928 Set_Comes_From_Source (Act_Decl_Id, True);
2930 if Nkind (Defining_Unit_Name (N)) = N_Defining_Program_Unit_Name then
2932 Make_Defining_Program_Unit_Name (Loc,
2933 Name => New_Copy_Tree (Name (Defining_Unit_Name (N))),
2934 Defining_Identifier => Act_Decl_Id);
2936 Act_Decl_Name := Act_Decl_Id;
2939 -- Case of instantiation of a formal package
2942 Act_Decl_Id := Defining_Identifier (N);
2943 Act_Decl_Name := Act_Decl_Id;
2946 Generate_Definition (Act_Decl_Id);
2947 Preanalyze_Actuals (N);
2950 Env_Installed := True;
2951 Check_Generic_Child_Unit (Gen_Id, Parent_Installed);
2952 Gen_Unit := Entity (Gen_Id);
2954 -- Verify that it is the name of a generic package
2956 if Etype (Gen_Unit) = Any_Type then
2960 elsif Ekind (Gen_Unit) /= E_Generic_Package then
2962 -- Ada 2005 (AI-50217): Cannot use instance in limited with_clause
2964 if From_With_Type (Gen_Unit) then
2966 ("cannot instantiate a limited withed package", Gen_Id);
2969 ("expect name of generic package in instantiation", Gen_Id);
2976 if In_Extended_Main_Source_Unit (N) then
2977 Set_Is_Instantiated (Gen_Unit);
2978 Generate_Reference (Gen_Unit, N);
2980 if Present (Renamed_Object (Gen_Unit)) then
2981 Set_Is_Instantiated (Renamed_Object (Gen_Unit));
2982 Generate_Reference (Renamed_Object (Gen_Unit), N);
2986 if Nkind (Gen_Id) = N_Identifier
2987 and then Chars (Gen_Unit) = Chars (Defining_Entity (N))
2990 ("& is hidden within declaration of instance", Gen_Id, Gen_Unit);
2992 elsif Nkind (Gen_Id) = N_Expanded_Name
2993 and then Is_Child_Unit (Gen_Unit)
2994 and then Nkind (Prefix (Gen_Id)) = N_Identifier
2995 and then Chars (Act_Decl_Id) = Chars (Prefix (Gen_Id))
2998 ("& is hidden within declaration of instance ", Prefix (Gen_Id));
3001 Set_Entity (Gen_Id, Gen_Unit);
3003 -- If generic is a renaming, get original generic unit
3005 if Present (Renamed_Object (Gen_Unit))
3006 and then Ekind (Renamed_Object (Gen_Unit)) = E_Generic_Package
3008 Gen_Unit := Renamed_Object (Gen_Unit);
3011 -- Verify that there are no circular instantiations
3013 if In_Open_Scopes (Gen_Unit) then
3014 Error_Msg_NE ("instantiation of & within itself", N, Gen_Unit);
3018 elsif Contains_Instance_Of (Gen_Unit, Current_Scope, Gen_Id) then
3019 Error_Msg_Node_2 := Current_Scope;
3021 ("circular Instantiation: & instantiated in &!", N, Gen_Unit);
3022 Circularity_Detected := True;
3027 Gen_Decl := Unit_Declaration_Node (Gen_Unit);
3029 -- Initialize renamings map, for error checking, and the list that
3030 -- holds private entities whose views have changed between generic
3031 -- definition and instantiation. If this is the instance created to
3032 -- validate an actual package, the instantiation environment is that
3033 -- of the enclosing instance.
3035 Generic_Renamings.Set_Last (0);
3036 Generic_Renamings_HTable.Reset;
3038 Create_Instantiation_Source (N, Gen_Unit, False, S_Adjustment);
3040 -- Copy original generic tree, to produce text for instantiation
3044 (Original_Node (Gen_Decl), Empty, Instantiating => True);
3046 Act_Spec := Specification (Act_Tree);
3048 -- If this is the instance created to validate an actual package,
3049 -- only the formals matter, do not examine the package spec itself.
3051 if Is_Actual_Pack then
3052 Set_Visible_Declarations (Act_Spec, New_List);
3053 Set_Private_Declarations (Act_Spec, New_List);
3057 Analyze_Associations
3059 Generic_Formal_Declarations (Act_Tree),
3060 Generic_Formal_Declarations (Gen_Decl));
3062 Set_Instance_Env (Gen_Unit, Act_Decl_Id);
3063 Set_Defining_Unit_Name (Act_Spec, Act_Decl_Name);
3064 Set_Is_Generic_Instance (Act_Decl_Id);
3066 Set_Generic_Parent (Act_Spec, Gen_Unit);
3068 -- References to the generic in its own declaration or its body are
3069 -- references to the instance. Add a renaming declaration for the
3070 -- generic unit itself. This declaration, as well as the renaming
3071 -- declarations for the generic formals, must remain private to the
3072 -- unit: the formals, because this is the language semantics, and
3073 -- the unit because its use is an artifact of the implementation.
3076 Make_Package_Renaming_Declaration (Loc,
3077 Defining_Unit_Name =>
3078 Make_Defining_Identifier (Loc, Chars (Gen_Unit)),
3079 Name => New_Reference_To (Act_Decl_Id, Loc));
3081 Append (Unit_Renaming, Renaming_List);
3083 -- The renaming declarations are the first local declarations of
3086 if Is_Non_Empty_List (Visible_Declarations (Act_Spec)) then
3088 (First (Visible_Declarations (Act_Spec)), Renaming_List);
3090 Set_Visible_Declarations (Act_Spec, Renaming_List);
3094 Make_Package_Declaration (Loc,
3095 Specification => Act_Spec);
3097 -- Save the instantiation node, for subsequent instantiation of the
3098 -- body, if there is one and we are generating code for the current
3099 -- unit. Mark the unit as having a body, to avoid a premature error
3102 -- We instantiate the body if we are generating code, if we are
3103 -- generating cross-reference information, or if we are building
3104 -- trees for ASIS use.
3107 Enclosing_Body_Present : Boolean := False;
3108 -- If the generic unit is not a compilation unit, then a body may
3109 -- be present in its parent even if none is required. We create a
3110 -- tentative pending instantiation for the body, which will be
3111 -- discarded if none is actually present.
3116 if Scope (Gen_Unit) /= Standard_Standard
3117 and then not Is_Child_Unit (Gen_Unit)
3119 Scop := Scope (Gen_Unit);
3121 while Present (Scop)
3122 and then Scop /= Standard_Standard
3124 if Unit_Requires_Body (Scop) then
3125 Enclosing_Body_Present := True;
3128 elsif In_Open_Scopes (Scop)
3129 and then In_Package_Body (Scop)
3131 Enclosing_Body_Present := True;
3135 exit when Is_Compilation_Unit (Scop);
3136 Scop := Scope (Scop);
3140 -- If front-end inlining is enabled, and this is a unit for which
3141 -- code will be generated, we instantiate the body at once.
3143 -- This is done if the instance is not the main unit, and if the
3144 -- generic is not a child unit of another generic, to avoid scope
3145 -- problems and the reinstallation of parent instances.
3148 and then (not Is_Child_Unit (Gen_Unit)
3149 or else not Is_Generic_Unit (Scope (Gen_Unit)))
3150 and then Might_Inline_Subp
3151 and then not Is_Actual_Pack
3153 if Front_End_Inlining
3154 and then (Is_In_Main_Unit (N)
3155 or else In_Main_Context (Current_Scope))
3156 and then Nkind (Parent (N)) /= N_Compilation_Unit
3160 -- In configurable_run_time mode we force the inlining of
3161 -- predefined subprograms marked Inline_Always, to minimize
3162 -- the use of the run-time library.
3164 elsif Is_Predefined_File_Name
3165 (Unit_File_Name (Get_Source_Unit (Gen_Decl)))
3166 and then Configurable_Run_Time_Mode
3167 and then Nkind (Parent (N)) /= N_Compilation_Unit
3172 -- If the current scope is itself an instance within a child
3173 -- unit, there will be duplications in the scope stack, and the
3174 -- unstacking mechanism in Inline_Instance_Body will fail.
3175 -- This loses some rare cases of optimization, and might be
3176 -- improved some day, if we can find a proper abstraction for
3177 -- "the complete compilation context" that can be saved and
3180 if Is_Generic_Instance (Current_Scope) then
3182 Curr_Unit : constant Entity_Id :=
3183 Cunit_Entity (Current_Sem_Unit);
3185 if Curr_Unit /= Current_Scope
3186 and then Is_Child_Unit (Curr_Unit)
3188 Inline_Now := False;
3195 (Unit_Requires_Body (Gen_Unit)
3196 or else Enclosing_Body_Present
3197 or else Present (Corresponding_Body (Gen_Decl)))
3198 and then (Is_In_Main_Unit (N)
3199 or else Might_Inline_Subp)
3200 and then not Is_Actual_Pack
3201 and then not Inline_Now
3202 and then (Operating_Mode = Generate_Code
3203 or else (Operating_Mode = Check_Semantics
3204 and then ASIS_Mode));
3206 -- If front_end_inlining is enabled, do not instantiate body if
3207 -- within a generic context.
3209 if (Front_End_Inlining
3210 and then not Expander_Active)
3211 or else Is_Generic_Unit (Cunit_Entity (Main_Unit))
3213 Needs_Body := False;
3216 -- If the current context is generic, and the package being
3217 -- instantiated is declared within a formal package, there is no
3218 -- body to instantiate until the enclosing generic is instantiated
3219 -- and there is an actual for the formal package. If the formal
3220 -- package has parameters, we build a regular package instance for
3221 -- it, that precedes the original formal package declaration.
3223 if In_Open_Scopes (Scope (Scope (Gen_Unit))) then
3225 Decl : constant Node_Id :=
3227 (Unit_Declaration_Node (Scope (Gen_Unit)));
3229 if Nkind (Decl) = N_Formal_Package_Declaration
3230 or else (Nkind (Decl) = N_Package_Declaration
3231 and then Is_List_Member (Decl)
3232 and then Present (Next (Decl))
3234 Nkind (Next (Decl)) =
3235 N_Formal_Package_Declaration)
3237 Needs_Body := False;
3243 -- If we are generating the calling stubs from the instantiation of
3244 -- a generic RCI package, we will not use the body of the generic
3247 if Distribution_Stub_Mode = Generate_Caller_Stub_Body
3248 and then Is_Compilation_Unit (Defining_Entity (N))
3250 Needs_Body := False;
3255 -- Here is a defence against a ludicrous number of instantiations
3256 -- caused by a circular set of instantiation attempts.
3258 if Pending_Instantiations.Last >
3259 Hostparm.Max_Instantiations
3261 Error_Msg_N ("too many instantiations", N);
3262 raise Unrecoverable_Error;
3265 -- Indicate that the enclosing scopes contain an instantiation,
3266 -- and that cleanup actions should be delayed until after the
3267 -- instance body is expanded.
3269 Check_Forward_Instantiation (Gen_Decl);
3270 if Nkind (N) = N_Package_Instantiation then
3272 Enclosing_Master : Entity_Id;
3275 -- Loop to search enclosing masters
3277 Enclosing_Master := Current_Scope;
3278 Scope_Loop : while Enclosing_Master /= Standard_Standard loop
3279 if Ekind (Enclosing_Master) = E_Package then
3280 if Is_Compilation_Unit (Enclosing_Master) then
3281 if In_Package_Body (Enclosing_Master) then
3283 (Body_Entity (Enclosing_Master));
3292 Enclosing_Master := Scope (Enclosing_Master);
3295 elsif Ekind (Enclosing_Master) = E_Generic_Package then
3296 Enclosing_Master := Scope (Enclosing_Master);
3298 elsif Is_Generic_Subprogram (Enclosing_Master)
3299 or else Ekind (Enclosing_Master) = E_Void
3301 -- Cleanup actions will eventually be performed on the
3302 -- enclosing instance, if any. Enclosing scope is void
3303 -- in the formal part of a generic subprogram.
3308 if Ekind (Enclosing_Master) = E_Entry
3310 Ekind (Scope (Enclosing_Master)) = E_Protected_Type
3312 if not Expander_Active then
3316 Protected_Body_Subprogram (Enclosing_Master);
3320 Set_Delay_Cleanups (Enclosing_Master);
3322 while Ekind (Enclosing_Master) = E_Block loop
3323 Enclosing_Master := Scope (Enclosing_Master);
3326 if Is_Subprogram (Enclosing_Master) then
3327 Delay_Descriptors (Enclosing_Master);
3329 elsif Is_Task_Type (Enclosing_Master) then
3331 TBP : constant Node_Id :=
3332 Get_Task_Body_Procedure
3335 if Present (TBP) then
3336 Delay_Descriptors (TBP);
3337 Set_Delay_Cleanups (TBP);
3344 end loop Scope_Loop;
3347 -- Make entry in table
3349 Pending_Instantiations.Append
3351 Act_Decl => Act_Decl,
3352 Expander_Status => Expander_Active,
3353 Current_Sem_Unit => Current_Sem_Unit,
3354 Scope_Suppress => Scope_Suppress,
3355 Local_Suppress_Stack_Top => Local_Suppress_Stack_Top));
3359 Set_Categorization_From_Pragmas (Act_Decl);
3361 if Parent_Installed then
3365 Set_Instance_Spec (N, Act_Decl);
3367 -- If not a compilation unit, insert the package declaration before
3368 -- the original instantiation node.
3370 if Nkind (Parent (N)) /= N_Compilation_Unit then
3371 Mark_Rewrite_Insertion (Act_Decl);
3372 Insert_Before (N, Act_Decl);
3375 -- For an instantiation that is a compilation unit, place declaration
3376 -- on current node so context is complete for analysis (including
3377 -- nested instantiations). If this is the main unit, the declaration
3378 -- eventually replaces the instantiation node. If the instance body
3379 -- is created later, it replaces the instance node, and the
3380 -- declaration is attached to it (see
3381 -- Build_Instance_Compilation_Unit_Nodes).
3384 if Cunit_Entity (Current_Sem_Unit) = Defining_Entity (N) then
3386 -- The entity for the current unit is the newly created one,
3387 -- and all semantic information is attached to it.
3389 Set_Cunit_Entity (Current_Sem_Unit, Act_Decl_Id);
3391 -- If this is the main unit, replace the main entity as well
3393 if Current_Sem_Unit = Main_Unit then
3394 Main_Unit_Entity := Act_Decl_Id;
3398 Set_Unit (Parent (N), Act_Decl);
3399 Set_Parent_Spec (Act_Decl, Parent_Spec (N));
3400 Set_Package_Instantiation (Act_Decl_Id, N);
3402 Set_Unit (Parent (N), N);
3403 Set_Body_Required (Parent (N), False);
3405 -- We never need elaboration checks on instantiations, since by
3406 -- definition, the body instantiation is elaborated at the same
3407 -- time as the spec instantiation.
3409 Set_Suppress_Elaboration_Warnings (Act_Decl_Id);
3410 Set_Kill_Elaboration_Checks (Act_Decl_Id);
3413 Check_Elab_Instantiation (N);
3415 if ABE_Is_Certain (N) and then Needs_Body then
3416 Pending_Instantiations.Decrement_Last;
3419 Check_Hidden_Child_Unit (N, Gen_Unit, Act_Decl_Id);
3421 Set_First_Private_Entity (Defining_Unit_Name (Unit_Renaming),
3422 First_Private_Entity (Act_Decl_Id));
3424 -- If the instantiation will receive a body, the unit will be
3425 -- transformed into a package body, and receive its own elaboration
3426 -- entity. Otherwise, the nature of the unit is now a package
3429 if Nkind (Parent (N)) = N_Compilation_Unit
3430 and then not Needs_Body
3432 Rewrite (N, Act_Decl);
3435 if Present (Corresponding_Body (Gen_Decl))
3436 or else Unit_Requires_Body (Gen_Unit)
3438 Set_Has_Completion (Act_Decl_Id);
3441 Check_Formal_Packages (Act_Decl_Id);
3443 Restore_Private_Views (Act_Decl_Id);
3445 Inherit_Context (Gen_Decl, N);
3447 if Parent_Installed then
3452 Env_Installed := False;
3455 Validate_Categorization_Dependency (N, Act_Decl_Id);
3457 -- There used to be a check here to prevent instantiations in local
3458 -- contexts if the No_Local_Allocators restriction was active. This
3459 -- check was removed by a binding interpretation in AI-95-00130/07,
3460 -- but we retain the code for documentation purposes.
3462 -- if Ekind (Act_Decl_Id) /= E_Void
3463 -- and then not Is_Library_Level_Entity (Act_Decl_Id)
3465 -- Check_Restriction (No_Local_Allocators, N);
3469 Inline_Instance_Body (N, Gen_Unit, Act_Decl);
3472 -- The following is a tree patch for ASIS: ASIS needs separate nodes to
3473 -- be used as defining identifiers for a formal package and for the
3474 -- corresponding expanded package.
3476 if Nkind (N) = N_Formal_Package_Declaration then
3477 Act_Decl_Id := New_Copy (Defining_Entity (N));
3478 Set_Comes_From_Source (Act_Decl_Id, True);
3479 Set_Is_Generic_Instance (Act_Decl_Id, False);
3480 Set_Defining_Identifier (N, Act_Decl_Id);
3484 when Instantiation_Error =>
3485 if Parent_Installed then
3489 if Env_Installed then
3492 end Analyze_Package_Instantiation;
3494 --------------------------
3495 -- Inline_Instance_Body --
3496 --------------------------
3498 procedure Inline_Instance_Body
3500 Gen_Unit : Entity_Id;
3504 Gen_Comp : constant Entity_Id :=
3505 Cunit_Entity (Get_Source_Unit (Gen_Unit));
3506 Curr_Comp : constant Node_Id := Cunit (Current_Sem_Unit);
3507 Curr_Scope : Entity_Id := Empty;
3508 Curr_Unit : constant Entity_Id :=
3509 Cunit_Entity (Current_Sem_Unit);
3510 Removed : Boolean := False;
3511 Num_Scopes : Int := 0;
3513 Scope_Stack_Depth : constant Int :=
3514 Scope_Stack.Last - Scope_Stack.First + 1;
3516 Use_Clauses : array (1 .. Scope_Stack_Depth) of Node_Id;
3517 Instances : array (1 .. Scope_Stack_Depth) of Entity_Id;
3518 Inner_Scopes : array (1 .. Scope_Stack_Depth) of Entity_Id;
3519 Num_Inner : Int := 0;
3520 N_Instances : Int := 0;
3524 -- Case of generic unit defined in another unit. We must remove the
3525 -- complete context of the current unit to install that of the generic.
3527 if Gen_Comp /= Cunit_Entity (Current_Sem_Unit) then
3529 -- Add some comments for the following two loops ???
3532 while Present (S) and then S /= Standard_Standard loop
3534 Num_Scopes := Num_Scopes + 1;
3536 Use_Clauses (Num_Scopes) :=
3538 (Scope_Stack.Last - Num_Scopes + 1).
3540 End_Use_Clauses (Use_Clauses (Num_Scopes));
3542 exit when Scope_Stack.Last - Num_Scopes + 1 = Scope_Stack.First
3543 or else Scope_Stack.Table
3544 (Scope_Stack.Last - Num_Scopes).Entity
3548 exit when Is_Generic_Instance (S)
3549 and then (In_Package_Body (S)
3550 or else Ekind (S) = E_Procedure
3551 or else Ekind (S) = E_Function);
3555 Vis := Is_Immediately_Visible (Gen_Comp);
3557 -- Find and save all enclosing instances
3562 and then S /= Standard_Standard
3564 if Is_Generic_Instance (S) then
3565 N_Instances := N_Instances + 1;
3566 Instances (N_Instances) := S;
3568 exit when In_Package_Body (S);
3574 -- Remove context of current compilation unit, unless we are within a
3575 -- nested package instantiation, in which case the context has been
3576 -- removed previously.
3578 -- If current scope is the body of a child unit, remove context of
3579 -- spec as well. If an enclosing scope is an instance body, the
3580 -- context has already been removed, but the entities in the body
3581 -- must be made invisible as well.
3586 and then S /= Standard_Standard
3588 if Is_Generic_Instance (S)
3589 and then (In_Package_Body (S)
3590 or else Ekind (S) = E_Procedure
3591 or else Ekind (S) = E_Function)
3593 -- We still have to remove the entities of the enclosing
3594 -- instance from direct visibility.
3599 E := First_Entity (S);
3600 while Present (E) loop
3601 Set_Is_Immediately_Visible (E, False);
3610 or else (Ekind (Curr_Unit) = E_Package_Body
3611 and then S = Spec_Entity (Curr_Unit))
3612 or else (Ekind (Curr_Unit) = E_Subprogram_Body
3615 (Unit_Declaration_Node (Curr_Unit)))
3619 -- Remove entities in current scopes from visibility, so that
3620 -- instance body is compiled in a clean environment.
3622 Save_Scope_Stack (Handle_Use => False);
3624 if Is_Child_Unit (S) then
3626 -- Remove child unit from stack, as well as inner scopes.
3627 -- Removing the context of a child unit removes parent units
3630 while Current_Scope /= S loop
3631 Num_Inner := Num_Inner + 1;
3632 Inner_Scopes (Num_Inner) := Current_Scope;
3637 Remove_Context (Curr_Comp);
3641 Remove_Context (Curr_Comp);
3644 if Ekind (Curr_Unit) = E_Package_Body then
3645 Remove_Context (Library_Unit (Curr_Comp));
3651 pragma Assert (Num_Inner < Num_Scopes);
3653 Push_Scope (Standard_Standard);
3654 Scope_Stack.Table (Scope_Stack.Last).Is_Active_Stack_Base := True;
3655 Instantiate_Package_Body
3658 Act_Decl => Act_Decl,
3659 Expander_Status => Expander_Active,
3660 Current_Sem_Unit => Current_Sem_Unit,
3661 Scope_Suppress => Scope_Suppress,
3662 Local_Suppress_Stack_Top => Local_Suppress_Stack_Top)),
3663 Inlined_Body => True);
3669 Set_Is_Immediately_Visible (Gen_Comp, Vis);
3671 -- Reset Generic_Instance flag so that use clauses can be installed
3672 -- in the proper order. (See Use_One_Package for effect of enclosing
3673 -- instances on processing of use clauses).
3675 for J in 1 .. N_Instances loop
3676 Set_Is_Generic_Instance (Instances (J), False);
3680 Install_Context (Curr_Comp);
3682 if Present (Curr_Scope)
3683 and then Is_Child_Unit (Curr_Scope)
3685 Push_Scope (Curr_Scope);
3686 Set_Is_Immediately_Visible (Curr_Scope);
3688 -- Finally, restore inner scopes as well
3690 for J in reverse 1 .. Num_Inner loop
3691 Push_Scope (Inner_Scopes (J));
3695 Restore_Scope_Stack (Handle_Use => False);
3697 if Present (Curr_Scope)
3699 (In_Private_Part (Curr_Scope)
3700 or else In_Package_Body (Curr_Scope))
3702 -- Install private declaration of ancestor units, which are
3703 -- currently available. Restore_Scope_Stack and Install_Context
3704 -- only install the visible part of parents.
3709 Par := Scope (Curr_Scope);
3710 while (Present (Par))
3711 and then Par /= Standard_Standard
3713 Install_Private_Declarations (Par);
3720 -- Restore use clauses. For a child unit, use clauses in the parents
3721 -- are restored when installing the context, so only those in inner
3722 -- scopes (and those local to the child unit itself) need to be
3723 -- installed explicitly.
3725 if Is_Child_Unit (Curr_Unit)
3728 for J in reverse 1 .. Num_Inner + 1 loop
3729 Scope_Stack.Table (Scope_Stack.Last - J + 1).First_Use_Clause :=
3731 Install_Use_Clauses (Use_Clauses (J));
3735 for J in reverse 1 .. Num_Scopes loop
3736 Scope_Stack.Table (Scope_Stack.Last - J + 1).First_Use_Clause :=
3738 Install_Use_Clauses (Use_Clauses (J));
3742 -- Restore status of instances. If one of them is a body, make
3743 -- its local entities visible again.
3750 for J in 1 .. N_Instances loop
3751 Inst := Instances (J);
3752 Set_Is_Generic_Instance (Inst, True);
3754 if In_Package_Body (Inst)
3755 or else Ekind (S) = E_Procedure
3756 or else Ekind (S) = E_Function
3758 E := First_Entity (Instances (J));
3759 while Present (E) loop
3760 Set_Is_Immediately_Visible (E);
3767 -- If generic unit is in current unit, current context is correct
3770 Instantiate_Package_Body
3773 Act_Decl => Act_Decl,
3774 Expander_Status => Expander_Active,
3775 Current_Sem_Unit => Current_Sem_Unit,
3776 Scope_Suppress => Scope_Suppress,
3777 Local_Suppress_Stack_Top => Local_Suppress_Stack_Top)),
3778 Inlined_Body => True);
3780 end Inline_Instance_Body;
3782 -------------------------------------
3783 -- Analyze_Procedure_Instantiation --
3784 -------------------------------------
3786 procedure Analyze_Procedure_Instantiation (N : Node_Id) is
3788 Analyze_Subprogram_Instantiation (N, E_Procedure);
3789 end Analyze_Procedure_Instantiation;
3791 -----------------------------------
3792 -- Need_Subprogram_Instance_Body --
3793 -----------------------------------
3795 function Need_Subprogram_Instance_Body
3797 Subp : Entity_Id) return Boolean
3800 if (Is_In_Main_Unit (N)
3801 or else Is_Inlined (Subp)
3802 or else Is_Inlined (Alias (Subp)))
3803 and then (Operating_Mode = Generate_Code
3804 or else (Operating_Mode = Check_Semantics
3805 and then ASIS_Mode))
3806 and then (Expander_Active or else ASIS_Mode)
3807 and then not ABE_Is_Certain (N)
3808 and then not Is_Eliminated (Subp)
3810 Pending_Instantiations.Append
3812 Act_Decl => Unit_Declaration_Node (Subp),
3813 Expander_Status => Expander_Active,
3814 Current_Sem_Unit => Current_Sem_Unit,
3815 Scope_Suppress => Scope_Suppress,
3816 Local_Suppress_Stack_Top => Local_Suppress_Stack_Top));
3821 end Need_Subprogram_Instance_Body;
3823 --------------------------------------
3824 -- Analyze_Subprogram_Instantiation --
3825 --------------------------------------
3827 procedure Analyze_Subprogram_Instantiation
3831 Loc : constant Source_Ptr := Sloc (N);
3832 Gen_Id : constant Node_Id := Name (N);
3834 Anon_Id : constant Entity_Id :=
3835 Make_Defining_Identifier (Sloc (Defining_Entity (N)),
3836 Chars => New_External_Name
3837 (Chars (Defining_Entity (N)), 'R'));
3839 Act_Decl_Id : Entity_Id;
3844 Env_Installed : Boolean := False;
3845 Gen_Unit : Entity_Id;
3847 Pack_Id : Entity_Id;
3848 Parent_Installed : Boolean := False;
3849 Renaming_List : List_Id;
3851 procedure Analyze_Instance_And_Renamings;
3852 -- The instance must be analyzed in a context that includes the mappings
3853 -- of generic parameters into actuals. We create a package declaration
3854 -- for this purpose, and a subprogram with an internal name within the
3855 -- package. The subprogram instance is simply an alias for the internal
3856 -- subprogram, declared in the current scope.
3858 ------------------------------------
3859 -- Analyze_Instance_And_Renamings --
3860 ------------------------------------
3862 procedure Analyze_Instance_And_Renamings is
3863 Def_Ent : constant Entity_Id := Defining_Entity (N);
3864 Pack_Decl : Node_Id;
3867 if Nkind (Parent (N)) = N_Compilation_Unit then
3869 -- For the case of a compilation unit, the container package has
3870 -- the same name as the instantiation, to insure that the binder
3871 -- calls the elaboration procedure with the right name. Copy the
3872 -- entity of the instance, which may have compilation level flags
3873 -- (e.g. Is_Child_Unit) set.
3875 Pack_Id := New_Copy (Def_Ent);
3878 -- Otherwise we use the name of the instantiation concatenated
3879 -- with its source position to ensure uniqueness if there are
3880 -- several instantiations with the same name.
3883 Make_Defining_Identifier (Loc,
3884 Chars => New_External_Name
3885 (Related_Id => Chars (Def_Ent),
3887 Suffix_Index => Source_Offset (Sloc (Def_Ent))));
3890 Pack_Decl := Make_Package_Declaration (Loc,
3891 Specification => Make_Package_Specification (Loc,
3892 Defining_Unit_Name => Pack_Id,
3893 Visible_Declarations => Renaming_List,
3894 End_Label => Empty));
3896 Set_Instance_Spec (N, Pack_Decl);
3897 Set_Is_Generic_Instance (Pack_Id);
3898 Set_Debug_Info_Needed (Pack_Id);
3900 -- Case of not a compilation unit
3902 if Nkind (Parent (N)) /= N_Compilation_Unit then
3903 Mark_Rewrite_Insertion (Pack_Decl);
3904 Insert_Before (N, Pack_Decl);
3905 Set_Has_Completion (Pack_Id);
3907 -- Case of an instantiation that is a compilation unit
3909 -- Place declaration on current node so context is complete for
3910 -- analysis (including nested instantiations), and for use in a
3911 -- context_clause (see Analyze_With_Clause).
3914 Set_Unit (Parent (N), Pack_Decl);
3915 Set_Parent_Spec (Pack_Decl, Parent_Spec (N));
3918 Analyze (Pack_Decl);
3919 Check_Formal_Packages (Pack_Id);
3920 Set_Is_Generic_Instance (Pack_Id, False);
3922 -- Body of the enclosing package is supplied when instantiating the
3923 -- subprogram body, after semantic analysis is completed.
3925 if Nkind (Parent (N)) = N_Compilation_Unit then
3927 -- Remove package itself from visibility, so it does not
3928 -- conflict with subprogram.
3930 Set_Name_Entity_Id (Chars (Pack_Id), Homonym (Pack_Id));
3932 -- Set name and scope of internal subprogram so that the proper
3933 -- external name will be generated. The proper scope is the scope
3934 -- of the wrapper package. We need to generate debugging info for
3935 -- the internal subprogram, so set flag accordingly.
3937 Set_Chars (Anon_Id, Chars (Defining_Entity (N)));
3938 Set_Scope (Anon_Id, Scope (Pack_Id));
3940 -- Mark wrapper package as referenced, to avoid spurious warnings
3941 -- if the instantiation appears in various with_ clauses of
3942 -- subunits of the main unit.
3944 Set_Referenced (Pack_Id);
3947 Set_Is_Generic_Instance (Anon_Id);
3948 Set_Debug_Info_Needed (Anon_Id);
3949 Act_Decl_Id := New_Copy (Anon_Id);
3951 Set_Parent (Act_Decl_Id, Parent (Anon_Id));
3952 Set_Chars (Act_Decl_Id, Chars (Defining_Entity (N)));
3953 Set_Sloc (Act_Decl_Id, Sloc (Defining_Entity (N)));
3954 Set_Comes_From_Source (Act_Decl_Id, True);
3956 -- The signature may involve types that are not frozen yet, but the
3957 -- subprogram will be frozen at the point the wrapper package is
3958 -- frozen, so it does not need its own freeze node. In fact, if one
3959 -- is created, it might conflict with the freezing actions from the
3962 Set_Has_Delayed_Freeze (Anon_Id, False);
3964 -- If the instance is a child unit, mark the Id accordingly. Mark
3965 -- the anonymous entity as well, which is the real subprogram and
3966 -- which is used when the instance appears in a context clause.
3968 Set_Is_Child_Unit (Act_Decl_Id, Is_Child_Unit (Defining_Entity (N)));
3969 Set_Is_Child_Unit (Anon_Id, Is_Child_Unit (Defining_Entity (N)));
3970 New_Overloaded_Entity (Act_Decl_Id);
3971 Check_Eliminated (Act_Decl_Id);
3973 -- In compilation unit case, kill elaboration checks on the
3974 -- instantiation, since they are never needed -- the body is
3975 -- instantiated at the same point as the spec.
3977 if Nkind (Parent (N)) = N_Compilation_Unit then
3978 Set_Suppress_Elaboration_Warnings (Act_Decl_Id);
3979 Set_Kill_Elaboration_Checks (Act_Decl_Id);
3980 Set_Is_Compilation_Unit (Anon_Id);
3982 Set_Cunit_Entity (Current_Sem_Unit, Pack_Id);
3985 -- The instance is not a freezing point for the new subprogram
3987 Set_Is_Frozen (Act_Decl_Id, False);
3989 if Nkind (Defining_Entity (N)) = N_Defining_Operator_Symbol then
3990 Valid_Operator_Definition (Act_Decl_Id);
3993 Set_Alias (Act_Decl_Id, Anon_Id);
3994 Set_Parent (Act_Decl_Id, Parent (Anon_Id));
3995 Set_Has_Completion (Act_Decl_Id);
3996 Set_Related_Instance (Pack_Id, Act_Decl_Id);
3998 if Nkind (Parent (N)) = N_Compilation_Unit then
3999 Set_Body_Required (Parent (N), False);
4001 end Analyze_Instance_And_Renamings;
4003 -- Start of processing for Analyze_Subprogram_Instantiation
4006 -- Very first thing: apply the special kludge for Text_IO processing
4007 -- in case we are instantiating one of the children of [Wide_]Text_IO.
4008 -- Of course such an instantiation is bogus (these are packages, not
4009 -- subprograms), but we get a better error message if we do this.
4011 Text_IO_Kludge (Gen_Id);
4013 -- Make node global for error reporting
4015 Instantiation_Node := N;
4016 Preanalyze_Actuals (N);
4019 Env_Installed := True;
4020 Check_Generic_Child_Unit (Gen_Id, Parent_Installed);
4021 Gen_Unit := Entity (Gen_Id);
4023 Generate_Reference (Gen_Unit, Gen_Id);
4025 if Nkind (Gen_Id) = N_Identifier
4026 and then Chars (Gen_Unit) = Chars (Defining_Entity (N))
4029 ("& is hidden within declaration of instance", Gen_Id, Gen_Unit);
4032 if Etype (Gen_Unit) = Any_Type then
4037 -- Verify that it is a generic subprogram of the right kind, and that
4038 -- it does not lead to a circular instantiation.
4040 if Ekind (Gen_Unit) /= E_Generic_Procedure
4041 and then Ekind (Gen_Unit) /= E_Generic_Function
4043 Error_Msg_N ("expect generic subprogram in instantiation", Gen_Id);
4045 elsif In_Open_Scopes (Gen_Unit) then
4046 Error_Msg_NE ("instantiation of & within itself", N, Gen_Unit);
4048 elsif K = E_Procedure
4049 and then Ekind (Gen_Unit) /= E_Generic_Procedure
4051 if Ekind (Gen_Unit) = E_Generic_Function then
4053 ("cannot instantiate generic function as procedure", Gen_Id);
4056 ("expect name of generic procedure in instantiation", Gen_Id);
4059 elsif K = E_Function
4060 and then Ekind (Gen_Unit) /= E_Generic_Function
4062 if Ekind (Gen_Unit) = E_Generic_Procedure then
4064 ("cannot instantiate generic procedure as function", Gen_Id);
4067 ("expect name of generic function in instantiation", Gen_Id);
4071 Set_Entity (Gen_Id, Gen_Unit);
4072 Set_Is_Instantiated (Gen_Unit);
4074 if In_Extended_Main_Source_Unit (N) then
4075 Generate_Reference (Gen_Unit, N);
4078 -- If renaming, get original unit
4080 if Present (Renamed_Object (Gen_Unit))
4081 and then (Ekind (Renamed_Object (Gen_Unit)) = E_Generic_Procedure
4083 Ekind (Renamed_Object (Gen_Unit)) = E_Generic_Function)
4085 Gen_Unit := Renamed_Object (Gen_Unit);
4086 Set_Is_Instantiated (Gen_Unit);
4087 Generate_Reference (Gen_Unit, N);
4090 if Contains_Instance_Of (Gen_Unit, Current_Scope, Gen_Id) then
4091 Error_Msg_Node_2 := Current_Scope;
4093 ("circular Instantiation: & instantiated in &!", N, Gen_Unit);
4094 Circularity_Detected := True;
4098 Gen_Decl := Unit_Declaration_Node (Gen_Unit);
4100 -- Initialize renamings map, for error checking
4102 Generic_Renamings.Set_Last (0);
4103 Generic_Renamings_HTable.Reset;
4105 Create_Instantiation_Source (N, Gen_Unit, False, S_Adjustment);
4107 -- Copy original generic tree, to produce text for instantiation
4111 (Original_Node (Gen_Decl), Empty, Instantiating => True);
4113 -- Inherit overriding indicator from instance node
4115 Act_Spec := Specification (Act_Tree);
4116 Set_Must_Override (Act_Spec, Must_Override (N));
4117 Set_Must_Not_Override (Act_Spec, Must_Not_Override (N));
4120 Analyze_Associations
4122 Generic_Formal_Declarations (Act_Tree),
4123 Generic_Formal_Declarations (Gen_Decl));
4125 -- The subprogram itself cannot contain a nested instance, so the
4126 -- current parent is left empty.
4128 Set_Instance_Env (Gen_Unit, Empty);
4130 -- Build the subprogram declaration, which does not appear in the
4131 -- generic template, and give it a sloc consistent with that of the
4134 Set_Defining_Unit_Name (Act_Spec, Anon_Id);
4135 Set_Generic_Parent (Act_Spec, Gen_Unit);
4137 Make_Subprogram_Declaration (Sloc (Act_Spec),
4138 Specification => Act_Spec);
4140 Set_Categorization_From_Pragmas (Act_Decl);
4142 if Parent_Installed then
4146 Append (Act_Decl, Renaming_List);
4147 Analyze_Instance_And_Renamings;
4149 -- If the generic is marked Import (Intrinsic), then so is the
4150 -- instance. This indicates that there is no body to instantiate. If
4151 -- generic is marked inline, so it the instance, and the anonymous
4152 -- subprogram it renames. If inlined, or else if inlining is enabled
4153 -- for the compilation, we generate the instance body even if it is
4154 -- not within the main unit.
4156 -- Any other pragmas might also be inherited ???
4158 if Is_Intrinsic_Subprogram (Gen_Unit) then
4159 Set_Is_Intrinsic_Subprogram (Anon_Id);
4160 Set_Is_Intrinsic_Subprogram (Act_Decl_Id);
4162 if Chars (Gen_Unit) = Name_Unchecked_Conversion then
4163 Validate_Unchecked_Conversion (N, Act_Decl_Id);
4167 Generate_Definition (Act_Decl_Id);
4169 Set_Is_Inlined (Act_Decl_Id, Is_Inlined (Gen_Unit));
4170 Set_Is_Inlined (Anon_Id, Is_Inlined (Gen_Unit));
4172 if not Is_Intrinsic_Subprogram (Gen_Unit) then
4173 Check_Elab_Instantiation (N);
4176 if Is_Dispatching_Operation (Act_Decl_Id)
4177 and then Ada_Version >= Ada_05
4183 Formal := First_Formal (Act_Decl_Id);
4184 while Present (Formal) loop
4185 if Ekind (Etype (Formal)) = E_Anonymous_Access_Type
4186 and then Is_Controlling_Formal (Formal)
4187 and then not Can_Never_Be_Null (Formal)
4189 Error_Msg_NE ("access parameter& is controlling,",
4191 Error_Msg_NE ("\corresponding parameter of & must be"
4192 & " explicitly null-excluding", N, Gen_Id);
4195 Next_Formal (Formal);
4200 Check_Hidden_Child_Unit (N, Gen_Unit, Act_Decl_Id);
4202 -- Subject to change, pending on if other pragmas are inherited ???
4204 Validate_Categorization_Dependency (N, Act_Decl_Id);
4206 if not Is_Intrinsic_Subprogram (Act_Decl_Id) then
4207 Inherit_Context (Gen_Decl, N);
4209 Restore_Private_Views (Pack_Id, False);
4211 -- If the context requires a full instantiation, mark node for
4212 -- subsequent construction of the body.
4214 if Need_Subprogram_Instance_Body (N, Act_Decl_Id) then
4216 Check_Forward_Instantiation (Gen_Decl);
4218 -- The wrapper package is always delayed, because it does not
4219 -- constitute a freeze point, but to insure that the freeze
4220 -- node is placed properly, it is created directly when
4221 -- instantiating the body (otherwise the freeze node might
4222 -- appear to early for nested instantiations).
4224 elsif Nkind (Parent (N)) = N_Compilation_Unit then
4226 -- For ASIS purposes, indicate that the wrapper package has
4227 -- replaced the instantiation node.
4229 Rewrite (N, Unit (Parent (N)));
4230 Set_Unit (Parent (N), N);
4233 elsif Nkind (Parent (N)) = N_Compilation_Unit then
4235 -- Replace instance node for library-level instantiations of
4236 -- intrinsic subprograms, for ASIS use.
4238 Rewrite (N, Unit (Parent (N)));
4239 Set_Unit (Parent (N), N);
4242 if Parent_Installed then
4247 Env_Installed := False;
4248 Generic_Renamings.Set_Last (0);
4249 Generic_Renamings_HTable.Reset;
4253 when Instantiation_Error =>
4254 if Parent_Installed then
4258 if Env_Installed then
4261 end Analyze_Subprogram_Instantiation;
4263 -------------------------
4264 -- Get_Associated_Node --
4265 -------------------------
4267 function Get_Associated_Node (N : Node_Id) return Node_Id is
4271 Assoc := Associated_Node (N);
4273 if Nkind (Assoc) /= Nkind (N) then
4276 elsif Nkind_In (Assoc, N_Aggregate, N_Extension_Aggregate) then
4280 -- If the node is part of an inner generic, it may itself have been
4281 -- remapped into a further generic copy. Associated_Node is otherwise
4282 -- used for the entity of the node, and will be of a different node
4283 -- kind, or else N has been rewritten as a literal or function call.
4285 while Present (Associated_Node (Assoc))
4286 and then Nkind (Associated_Node (Assoc)) = Nkind (Assoc)
4288 Assoc := Associated_Node (Assoc);
4291 -- Follow and additional link in case the final node was rewritten.
4292 -- This can only happen with nested generic units.
4294 if (Nkind (Assoc) = N_Identifier or else Nkind (Assoc) in N_Op)
4295 and then Present (Associated_Node (Assoc))
4296 and then (Nkind_In (Associated_Node (Assoc), N_Function_Call,
4297 N_Explicit_Dereference,
4302 Assoc := Associated_Node (Assoc);
4307 end Get_Associated_Node;
4309 -------------------------------------------
4310 -- Build_Instance_Compilation_Unit_Nodes --
4311 -------------------------------------------
4313 procedure Build_Instance_Compilation_Unit_Nodes
4318 Decl_Cunit : Node_Id;
4319 Body_Cunit : Node_Id;
4321 New_Main : constant Entity_Id := Defining_Entity (Act_Decl);
4322 Old_Main : constant Entity_Id := Cunit_Entity (Main_Unit);
4325 -- A new compilation unit node is built for the instance declaration
4328 Make_Compilation_Unit (Sloc (N),
4329 Context_Items => Empty_List,
4332 Make_Compilation_Unit_Aux (Sloc (N)));
4334 Set_Parent_Spec (Act_Decl, Parent_Spec (N));
4335 Set_Body_Required (Decl_Cunit, True);
4337 -- We use the original instantiation compilation unit as the resulting
4338 -- compilation unit of the instance, since this is the main unit.
4340 Rewrite (N, Act_Body);
4341 Body_Cunit := Parent (N);
4343 -- The two compilation unit nodes are linked by the Library_Unit field
4345 Set_Library_Unit (Decl_Cunit, Body_Cunit);
4346 Set_Library_Unit (Body_Cunit, Decl_Cunit);
4348 -- Preserve the private nature of the package if needed
4350 Set_Private_Present (Decl_Cunit, Private_Present (Body_Cunit));
4352 -- If the instance is not the main unit, its context, categorization,
4353 -- and elaboration entity are not relevant to the compilation.
4355 if Parent (N) /= Cunit (Main_Unit) then
4359 -- The context clause items on the instantiation, which are now attached
4360 -- to the body compilation unit (since the body overwrote the original
4361 -- instantiation node), semantically belong on the spec, so copy them
4362 -- there. It's harmless to leave them on the body as well. In fact one
4363 -- could argue that they belong in both places.
4365 Citem := First (Context_Items (Body_Cunit));
4366 while Present (Citem) loop
4367 Append (New_Copy (Citem), Context_Items (Decl_Cunit));
4371 -- Propagate categorization flags on packages, so that they appear in
4372 -- the ali file for the spec of the unit.
4374 if Ekind (New_Main) = E_Package then
4375 Set_Is_Pure (Old_Main, Is_Pure (New_Main));
4376 Set_Is_Preelaborated (Old_Main, Is_Preelaborated (New_Main));
4377 Set_Is_Remote_Types (Old_Main, Is_Remote_Types (New_Main));
4378 Set_Is_Shared_Passive (Old_Main, Is_Shared_Passive (New_Main));
4379 Set_Is_Remote_Call_Interface
4380 (Old_Main, Is_Remote_Call_Interface (New_Main));
4383 -- Make entry in Units table, so that binder can generate call to
4384 -- elaboration procedure for body, if any.
4386 Make_Instance_Unit (Body_Cunit);
4387 Main_Unit_Entity := New_Main;
4388 Set_Cunit_Entity (Main_Unit, Main_Unit_Entity);
4390 -- Build elaboration entity, since the instance may certainly generate
4391 -- elaboration code requiring a flag for protection.
4393 Build_Elaboration_Entity (Decl_Cunit, New_Main);
4394 end Build_Instance_Compilation_Unit_Nodes;
4396 -----------------------------
4397 -- Check_Access_Definition --
4398 -----------------------------
4400 procedure Check_Access_Definition (N : Node_Id) is
4403 (Ada_Version >= Ada_05
4404 and then Present (Access_Definition (N)));
4406 end Check_Access_Definition;
4408 -----------------------------------
4409 -- Check_Formal_Package_Instance --
4410 -----------------------------------
4412 -- If the formal has specific parameters, they must match those of the
4413 -- actual. Both of them are instances, and the renaming declarations for
4414 -- their formal parameters appear in the same order in both. The analyzed
4415 -- formal has been analyzed in the context of the current instance.
4417 procedure Check_Formal_Package_Instance
4418 (Formal_Pack : Entity_Id;
4419 Actual_Pack : Entity_Id)
4421 E1 : Entity_Id := First_Entity (Actual_Pack);
4422 E2 : Entity_Id := First_Entity (Formal_Pack);
4427 procedure Check_Mismatch (B : Boolean);
4428 -- Common error routine for mismatch between the parameters of the
4429 -- actual instance and those of the formal package.
4431 function Same_Instantiated_Constant (E1, E2 : Entity_Id) return Boolean;
4432 -- The formal may come from a nested formal package, and the actual may
4433 -- have been constant-folded. To determine whether the two denote the
4434 -- same entity we may have to traverse several definitions to recover
4435 -- the ultimate entity that they refer to.
4437 function Same_Instantiated_Variable (E1, E2 : Entity_Id) return Boolean;
4438 -- Similarly, if the formal comes from a nested formal package, the
4439 -- actual may designate the formal through multiple renamings, which
4440 -- have to be followed to determine the original variable in question.
4442 --------------------
4443 -- Check_Mismatch --
4444 --------------------
4446 procedure Check_Mismatch (B : Boolean) is
4447 Kind : constant Node_Kind := Nkind (Parent (E2));
4450 if Kind = N_Formal_Type_Declaration then
4453 elsif Nkind_In (Kind, N_Formal_Object_Declaration,
4454 N_Formal_Package_Declaration)
4455 or else Kind in N_Formal_Subprogram_Declaration
4461 ("actual for & in actual instance does not match formal",
4462 Parent (Actual_Pack), E1);
4466 --------------------------------
4467 -- Same_Instantiated_Constant --
4468 --------------------------------
4470 function Same_Instantiated_Constant
4471 (E1, E2 : Entity_Id) return Boolean
4477 while Present (Ent) loop
4481 elsif Ekind (Ent) /= E_Constant then
4484 elsif Is_Entity_Name (Constant_Value (Ent)) then
4485 if Entity (Constant_Value (Ent)) = E1 then
4488 Ent := Entity (Constant_Value (Ent));
4491 -- The actual may be a constant that has been folded. Recover
4494 elsif Is_Entity_Name (Original_Node (Constant_Value (Ent))) then
4495 Ent := Entity (Original_Node (Constant_Value (Ent)));
4502 end Same_Instantiated_Constant;
4504 --------------------------------
4505 -- Same_Instantiated_Variable --
4506 --------------------------------
4508 function Same_Instantiated_Variable
4509 (E1, E2 : Entity_Id) return Boolean
4511 function Original_Entity (E : Entity_Id) return Entity_Id;
4512 -- Follow chain of renamings to the ultimate ancestor
4514 ---------------------
4515 -- Original_Entity --
4516 ---------------------
4518 function Original_Entity (E : Entity_Id) return Entity_Id is
4523 while Nkind (Parent (Orig)) = N_Object_Renaming_Declaration
4524 and then Present (Renamed_Object (Orig))
4525 and then Is_Entity_Name (Renamed_Object (Orig))
4527 Orig := Entity (Renamed_Object (Orig));
4531 end Original_Entity;
4533 -- Start of processing for Same_Instantiated_Variable
4536 return Ekind (E1) = Ekind (E2)
4537 and then Original_Entity (E1) = Original_Entity (E2);
4538 end Same_Instantiated_Variable;
4540 -- Start of processing for Check_Formal_Package_Instance
4544 and then Present (E2)
4546 exit when Ekind (E1) = E_Package
4547 and then Renamed_Entity (E1) = Renamed_Entity (Actual_Pack);
4549 -- If the formal is the renaming of the formal package, this
4550 -- is the end of its formal part, which may occur before the
4551 -- end of the formal part in the actual in the presence of
4552 -- defaulted parameters in the formal package.
4554 exit when Nkind (Parent (E2)) = N_Package_Renaming_Declaration
4555 and then Renamed_Entity (E2) = Scope (E2);
4557 -- The analysis of the actual may generate additional internal
4558 -- entities. If the formal is defaulted, there is no corresponding
4559 -- analysis and the internal entities must be skipped, until we
4560 -- find corresponding entities again.
4562 if Comes_From_Source (E2)
4563 and then not Comes_From_Source (E1)
4564 and then Chars (E1) /= Chars (E2)
4567 and then Chars (E1) /= Chars (E2)
4576 -- If the formal entity comes from a formal declaration, it was
4577 -- defaulted in the formal package, and no check is needed on it.
4579 elsif Nkind (Parent (E2)) = N_Formal_Object_Declaration then
4582 elsif Is_Type (E1) then
4584 -- Subtypes must statically match. E1, E2 are the local entities
4585 -- that are subtypes of the actuals. Itypes generated for other
4586 -- parameters need not be checked, the check will be performed
4587 -- on the parameters themselves.
4589 -- If E2 is a formal type declaration, it is a defaulted parameter
4590 -- and needs no checking.
4592 if not Is_Itype (E1)
4593 and then not Is_Itype (E2)
4597 or else Etype (E1) /= Etype (E2)
4598 or else not Subtypes_Statically_Match (E1, E2));
4601 elsif Ekind (E1) = E_Constant then
4603 -- IN parameters must denote the same static value, or the same
4604 -- constant, or the literal null.
4606 Expr1 := Expression (Parent (E1));
4608 if Ekind (E2) /= E_Constant then
4609 Check_Mismatch (True);
4612 Expr2 := Expression (Parent (E2));
4615 if Is_Static_Expression (Expr1) then
4617 if not Is_Static_Expression (Expr2) then
4618 Check_Mismatch (True);
4620 elsif Is_Discrete_Type (Etype (E1)) then
4622 V1 : constant Uint := Expr_Value (Expr1);
4623 V2 : constant Uint := Expr_Value (Expr2);
4625 Check_Mismatch (V1 /= V2);
4628 elsif Is_Real_Type (Etype (E1)) then
4630 V1 : constant Ureal := Expr_Value_R (Expr1);
4631 V2 : constant Ureal := Expr_Value_R (Expr2);
4633 Check_Mismatch (V1 /= V2);
4636 elsif Is_String_Type (Etype (E1))
4637 and then Nkind (Expr1) = N_String_Literal
4639 if Nkind (Expr2) /= N_String_Literal then
4640 Check_Mismatch (True);
4643 (not String_Equal (Strval (Expr1), Strval (Expr2)));
4647 elsif Is_Entity_Name (Expr1) then
4648 if Is_Entity_Name (Expr2) then
4649 if Entity (Expr1) = Entity (Expr2) then
4653 (not Same_Instantiated_Constant
4654 (Entity (Expr1), Entity (Expr2)));
4657 Check_Mismatch (True);
4660 elsif Is_Entity_Name (Original_Node (Expr1))
4661 and then Is_Entity_Name (Expr2)
4663 Same_Instantiated_Constant
4664 (Entity (Original_Node (Expr1)), Entity (Expr2))
4668 elsif Nkind (Expr1) = N_Null then
4669 Check_Mismatch (Nkind (Expr1) /= N_Null);
4672 Check_Mismatch (True);
4675 elsif Ekind (E1) = E_Variable then
4676 Check_Mismatch (not Same_Instantiated_Variable (E1, E2));
4678 elsif Ekind (E1) = E_Package then
4680 (Ekind (E1) /= Ekind (E2)
4681 or else Renamed_Object (E1) /= Renamed_Object (E2));
4683 elsif Is_Overloadable (E1) then
4685 -- Verify that the actual subprograms match. Note that actuals
4686 -- that are attributes are rewritten as subprograms. If the
4687 -- subprogram in the formal package is defaulted, no check is
4688 -- needed. Note that this can only happen in Ada 2005 when the
4689 -- formal package can be partially parametrized.
4691 if Nkind (Unit_Declaration_Node (E1)) =
4692 N_Subprogram_Renaming_Declaration
4693 and then From_Default (Unit_Declaration_Node (E1))
4699 (Ekind (E2) /= Ekind (E1) or else (Alias (E1)) /= Alias (E2));
4703 raise Program_Error;
4710 end Check_Formal_Package_Instance;
4712 ---------------------------
4713 -- Check_Formal_Packages --
4714 ---------------------------
4716 procedure Check_Formal_Packages (P_Id : Entity_Id) is
4718 Formal_P : Entity_Id;
4721 -- Iterate through the declarations in the instance, looking for package
4722 -- renaming declarations that denote instances of formal packages. Stop
4723 -- when we find the renaming of the current package itself. The
4724 -- declaration for a formal package without a box is followed by an
4725 -- internal entity that repeats the instantiation.
4727 E := First_Entity (P_Id);
4728 while Present (E) loop
4729 if Ekind (E) = E_Package then
4730 if Renamed_Object (E) = P_Id then
4733 elsif Nkind (Parent (E)) /= N_Package_Renaming_Declaration then
4736 elsif not Box_Present (Parent (Associated_Formal_Package (E))) then
4737 Formal_P := Next_Entity (E);
4738 Check_Formal_Package_Instance (Formal_P, E);
4740 -- After checking, remove the internal validating package. It
4741 -- is only needed for semantic checks, and as it may contain
4742 -- generic formal declarations it should not reach gigi.
4744 Remove (Unit_Declaration_Node (Formal_P));
4750 end Check_Formal_Packages;
4752 ---------------------------------
4753 -- Check_Forward_Instantiation --
4754 ---------------------------------
4756 procedure Check_Forward_Instantiation (Decl : Node_Id) is
4758 Gen_Comp : Entity_Id := Cunit_Entity (Get_Source_Unit (Decl));
4761 -- The instantiation appears before the generic body if we are in the
4762 -- scope of the unit containing the generic, either in its spec or in
4763 -- the package body, and before the generic body.
4765 if Ekind (Gen_Comp) = E_Package_Body then
4766 Gen_Comp := Spec_Entity (Gen_Comp);
4769 if In_Open_Scopes (Gen_Comp)
4770 and then No (Corresponding_Body (Decl))
4775 and then not Is_Compilation_Unit (S)
4776 and then not Is_Child_Unit (S)
4778 if Ekind (S) = E_Package then
4779 Set_Has_Forward_Instantiation (S);
4785 end Check_Forward_Instantiation;
4787 ---------------------------
4788 -- Check_Generic_Actuals --
4789 ---------------------------
4791 -- The visibility of the actuals may be different between the point of
4792 -- generic instantiation and the instantiation of the body.
4794 procedure Check_Generic_Actuals
4795 (Instance : Entity_Id;
4796 Is_Formal_Box : Boolean)
4801 function Denotes_Previous_Actual (Typ : Entity_Id) return Boolean;
4802 -- For a formal that is an array type, the component type is often a
4803 -- previous formal in the same unit. The privacy status of the component
4804 -- type will have been examined earlier in the traversal of the
4805 -- corresponding actuals, and this status should not be modified for the
4806 -- array type itself.
4808 -- To detect this case we have to rescan the list of formals, which
4809 -- is usually short enough to ignore the resulting inefficiency.
4811 function Denotes_Previous_Actual (Typ : Entity_Id) return Boolean is
4814 Prev := First_Entity (Instance);
4815 while Present (Prev) loop
4817 and then Nkind (Parent (Prev)) = N_Subtype_Declaration
4818 and then Is_Entity_Name (Subtype_Indication (Parent (Prev)))
4819 and then Entity (Subtype_Indication (Parent (Prev))) = Typ
4829 end Denotes_Previous_Actual;
4831 -- Start of processing for Check_Generic_Actuals
4834 E := First_Entity (Instance);
4835 while Present (E) loop
4837 and then Nkind (Parent (E)) = N_Subtype_Declaration
4838 and then Scope (Etype (E)) /= Instance
4839 and then Is_Entity_Name (Subtype_Indication (Parent (E)))
4841 if Is_Array_Type (E)
4842 and then Denotes_Previous_Actual (Component_Type (E))
4846 Check_Private_View (Subtype_Indication (Parent (E)));
4848 Set_Is_Generic_Actual_Type (E, True);
4849 Set_Is_Hidden (E, False);
4850 Set_Is_Potentially_Use_Visible (E,
4853 -- We constructed the generic actual type as a subtype of the
4854 -- supplied type. This means that it normally would not inherit
4855 -- subtype specific attributes of the actual, which is wrong for
4856 -- the generic case.
4858 Astype := Ancestor_Subtype (E);
4862 -- This can happen when E is an itype that is the full view of
4863 -- a private type completed, e.g. with a constrained array. In
4864 -- that case, use the first subtype, which will carry size
4865 -- information. The base type itself is unconstrained and will
4868 Astype := First_Subtype (E);
4871 Set_Size_Info (E, (Astype));
4872 Set_RM_Size (E, RM_Size (Astype));
4873 Set_First_Rep_Item (E, First_Rep_Item (Astype));
4875 if Is_Discrete_Or_Fixed_Point_Type (E) then
4876 Set_RM_Size (E, RM_Size (Astype));
4878 -- In nested instances, the base type of an access actual
4879 -- may itself be private, and need to be exchanged.
4881 elsif Is_Access_Type (E)
4882 and then Is_Private_Type (Etype (E))
4885 (New_Occurrence_Of (Etype (E), Sloc (Instance)));
4888 elsif Ekind (E) = E_Package then
4890 -- If this is the renaming for the current instance, we're done.
4891 -- Otherwise it is a formal package. If the corresponding formal
4892 -- was declared with a box, the (instantiations of the) generic
4893 -- formal part are also visible. Otherwise, ignore the entity
4894 -- created to validate the actuals.
4896 if Renamed_Object (E) = Instance then
4899 elsif Nkind (Parent (E)) /= N_Package_Renaming_Declaration then
4902 -- The visibility of a formal of an enclosing generic is already
4905 elsif Denotes_Formal_Package (E) then
4908 elsif Present (Associated_Formal_Package (E))
4909 and then not Is_Generic_Formal (E)
4911 if Box_Present (Parent (Associated_Formal_Package (E))) then
4912 Check_Generic_Actuals (Renamed_Object (E), True);
4915 Check_Generic_Actuals (Renamed_Object (E), False);
4918 Set_Is_Hidden (E, False);
4921 -- If this is a subprogram instance (in a wrapper package) the
4922 -- actual is fully visible.
4924 elsif Is_Wrapper_Package (Instance) then
4925 Set_Is_Hidden (E, False);
4927 -- If the formal package is declared with a box, or if the formal
4928 -- parameter is defaulted, it is visible in the body.
4931 or else Is_Visible_Formal (E)
4933 Set_Is_Hidden (E, False);
4938 end Check_Generic_Actuals;
4940 ------------------------------
4941 -- Check_Generic_Child_Unit --
4942 ------------------------------
4944 procedure Check_Generic_Child_Unit
4946 Parent_Installed : in out Boolean)
4948 Loc : constant Source_Ptr := Sloc (Gen_Id);
4949 Gen_Par : Entity_Id := Empty;
4951 Inst_Par : Entity_Id;
4954 function Find_Generic_Child
4956 Id : Node_Id) return Entity_Id;
4957 -- Search generic parent for possible child unit with the given name
4959 function In_Enclosing_Instance return Boolean;
4960 -- Within an instance of the parent, the child unit may be denoted
4961 -- by a simple name, or an abbreviated expanded name. Examine enclosing
4962 -- scopes to locate a possible parent instantiation.
4964 ------------------------
4965 -- Find_Generic_Child --
4966 ------------------------
4968 function Find_Generic_Child
4970 Id : Node_Id) return Entity_Id
4975 -- If entity of name is already set, instance has already been
4976 -- resolved, e.g. in an enclosing instantiation.
4978 if Present (Entity (Id)) then
4979 if Scope (Entity (Id)) = Scop then
4986 E := First_Entity (Scop);
4987 while Present (E) loop
4988 if Chars (E) = Chars (Id)
4989 and then Is_Child_Unit (E)
4991 if Is_Child_Unit (E)
4992 and then not Is_Visible_Child_Unit (E)
4995 ("generic child unit& is not visible", Gen_Id, E);
5007 end Find_Generic_Child;
5009 ---------------------------
5010 -- In_Enclosing_Instance --
5011 ---------------------------
5013 function In_Enclosing_Instance return Boolean is
5014 Enclosing_Instance : Node_Id;
5015 Instance_Decl : Node_Id;
5018 -- We do not inline any call that contains instantiations, except
5019 -- for instantiations of Unchecked_Conversion, so if we are within
5020 -- an inlined body the current instance does not require parents.
5022 if In_Inlined_Body then
5023 pragma Assert (Chars (Gen_Id) = Name_Unchecked_Conversion);
5027 -- Loop to check enclosing scopes
5029 Enclosing_Instance := Current_Scope;
5030 while Present (Enclosing_Instance) loop
5031 Instance_Decl := Unit_Declaration_Node (Enclosing_Instance);
5033 if Ekind (Enclosing_Instance) = E_Package
5034 and then Is_Generic_Instance (Enclosing_Instance)
5036 (Generic_Parent (Specification (Instance_Decl)))
5038 -- Check whether the generic we are looking for is a child of
5041 E := Find_Generic_Child
5042 (Generic_Parent (Specification (Instance_Decl)), Gen_Id);
5043 exit when Present (E);
5049 Enclosing_Instance := Scope (Enclosing_Instance);
5061 Make_Expanded_Name (Loc,
5063 Prefix => New_Occurrence_Of (Enclosing_Instance, Loc),
5064 Selector_Name => New_Occurrence_Of (E, Loc)));
5066 Set_Entity (Gen_Id, E);
5067 Set_Etype (Gen_Id, Etype (E));
5068 Parent_Installed := False; -- Already in scope.
5071 end In_Enclosing_Instance;
5073 -- Start of processing for Check_Generic_Child_Unit
5076 -- If the name of the generic is given by a selected component, it may
5077 -- be the name of a generic child unit, and the prefix is the name of an
5078 -- instance of the parent, in which case the child unit must be visible.
5079 -- If this instance is not in scope, it must be placed there and removed
5080 -- after instantiation, because what is being instantiated is not the
5081 -- original child, but the corresponding child present in the instance
5084 -- If the child is instantiated within the parent, it can be given by
5085 -- a simple name. In this case the instance is already in scope, but
5086 -- the child generic must be recovered from the generic parent as well.
5088 if Nkind (Gen_Id) = N_Selected_Component then
5089 S := Selector_Name (Gen_Id);
5090 Analyze (Prefix (Gen_Id));
5091 Inst_Par := Entity (Prefix (Gen_Id));
5093 if Ekind (Inst_Par) = E_Package
5094 and then Present (Renamed_Object (Inst_Par))
5096 Inst_Par := Renamed_Object (Inst_Par);
5099 if Ekind (Inst_Par) = E_Package then
5100 if Nkind (Parent (Inst_Par)) = N_Package_Specification then
5101 Gen_Par := Generic_Parent (Parent (Inst_Par));
5103 elsif Nkind (Parent (Inst_Par)) = N_Defining_Program_Unit_Name
5105 Nkind (Parent (Parent (Inst_Par))) = N_Package_Specification
5107 Gen_Par := Generic_Parent (Parent (Parent (Inst_Par)));
5110 elsif Ekind (Inst_Par) = E_Generic_Package
5111 and then Nkind (Parent (Gen_Id)) = N_Formal_Package_Declaration
5113 -- A formal package may be a real child package, and not the
5114 -- implicit instance within a parent. In this case the child is
5115 -- not visible and has to be retrieved explicitly as well.
5117 Gen_Par := Inst_Par;
5120 if Present (Gen_Par) then
5122 -- The prefix denotes an instantiation. The entity itself may be a
5123 -- nested generic, or a child unit.
5125 E := Find_Generic_Child (Gen_Par, S);
5128 Change_Selected_Component_To_Expanded_Name (Gen_Id);
5129 Set_Entity (Gen_Id, E);
5130 Set_Etype (Gen_Id, Etype (E));
5132 Set_Etype (S, Etype (E));
5134 -- Indicate that this is a reference to the parent
5136 if In_Extended_Main_Source_Unit (Gen_Id) then
5137 Set_Is_Instantiated (Inst_Par);
5140 -- A common mistake is to replicate the naming scheme of a
5141 -- hierarchy by instantiating a generic child directly, rather
5142 -- than the implicit child in a parent instance:
5144 -- generic .. package Gpar is ..
5145 -- generic .. package Gpar.Child is ..
5146 -- package Par is new Gpar ();
5149 -- package Par.Child is new Gpar.Child ();
5150 -- rather than Par.Child
5152 -- In this case the instantiation is within Par, which is an
5153 -- instance, but Gpar does not denote Par because we are not IN
5154 -- the instance of Gpar, so this is illegal. The test below
5155 -- recognizes this particular case.
5157 if Is_Child_Unit (E)
5158 and then not Comes_From_Source (Entity (Prefix (Gen_Id)))
5159 and then (not In_Instance
5160 or else Nkind (Parent (Parent (Gen_Id))) =
5164 ("prefix of generic child unit must be instance of parent",
5168 if not In_Open_Scopes (Inst_Par)
5169 and then Nkind (Parent (Gen_Id)) not in
5170 N_Generic_Renaming_Declaration
5172 Install_Parent (Inst_Par);
5173 Parent_Installed := True;
5175 elsif In_Open_Scopes (Inst_Par) then
5177 -- If the parent is already installed verify that the
5178 -- actuals for its formal packages declared with a box
5179 -- are already installed. This is necessary when the
5180 -- child instance is a child of the parent instance.
5181 -- In this case the parent is placed on the scope stack
5182 -- but the formal packages are not made visible.
5184 Install_Formal_Packages (Inst_Par);
5188 -- If the generic parent does not contain an entity that
5189 -- corresponds to the selector, the instance doesn't either.
5190 -- Analyzing the node will yield the appropriate error message.
5191 -- If the entity is not a child unit, then it is an inner
5192 -- generic in the parent.
5200 if Is_Child_Unit (Entity (Gen_Id))
5202 Nkind (Parent (Gen_Id)) not in N_Generic_Renaming_Declaration
5203 and then not In_Open_Scopes (Inst_Par)
5205 Install_Parent (Inst_Par);
5206 Parent_Installed := True;
5210 elsif Nkind (Gen_Id) = N_Expanded_Name then
5212 -- Entity already present, analyze prefix, whose meaning may be
5213 -- an instance in the current context. If it is an instance of
5214 -- a relative within another, the proper parent may still have
5215 -- to be installed, if they are not of the same generation.
5217 Analyze (Prefix (Gen_Id));
5219 -- In the unlikely case that a local declaration hides the name
5220 -- of the parent package, locate it on the homonym chain. If the
5221 -- context is an instance of the parent, the renaming entity is
5224 Inst_Par := Entity (Prefix (Gen_Id));
5225 while Present (Inst_Par)
5226 and then not Is_Package_Or_Generic_Package (Inst_Par)
5228 Inst_Par := Homonym (Inst_Par);
5231 pragma Assert (Present (Inst_Par));
5232 Set_Entity (Prefix (Gen_Id), Inst_Par);
5234 if In_Enclosing_Instance then
5237 elsif Present (Entity (Gen_Id))
5238 and then Is_Child_Unit (Entity (Gen_Id))
5239 and then not In_Open_Scopes (Inst_Par)
5241 Install_Parent (Inst_Par);
5242 Parent_Installed := True;
5245 elsif In_Enclosing_Instance then
5247 -- The child unit is found in some enclosing scope
5254 -- If this is the renaming of the implicit child in a parent
5255 -- instance, recover the parent name and install it.
5257 if Is_Entity_Name (Gen_Id) then
5258 E := Entity (Gen_Id);
5260 if Is_Generic_Unit (E)
5261 and then Nkind (Parent (E)) in N_Generic_Renaming_Declaration
5262 and then Is_Child_Unit (Renamed_Object (E))
5263 and then Is_Generic_Unit (Scope (Renamed_Object (E)))
5264 and then Nkind (Name (Parent (E))) = N_Expanded_Name
5267 New_Copy_Tree (Name (Parent (E))));
5268 Inst_Par := Entity (Prefix (Gen_Id));
5270 if not In_Open_Scopes (Inst_Par) then
5271 Install_Parent (Inst_Par);
5272 Parent_Installed := True;
5275 -- If it is a child unit of a non-generic parent, it may be
5276 -- use-visible and given by a direct name. Install parent as
5279 elsif Is_Generic_Unit (E)
5280 and then Is_Child_Unit (E)
5282 Nkind (Parent (Gen_Id)) not in N_Generic_Renaming_Declaration
5283 and then not Is_Generic_Unit (Scope (E))
5285 if not In_Open_Scopes (Scope (E)) then
5286 Install_Parent (Scope (E));
5287 Parent_Installed := True;
5292 end Check_Generic_Child_Unit;
5294 -----------------------------
5295 -- Check_Hidden_Child_Unit --
5296 -----------------------------
5298 procedure Check_Hidden_Child_Unit
5300 Gen_Unit : Entity_Id;
5301 Act_Decl_Id : Entity_Id)
5303 Gen_Id : constant Node_Id := Name (N);
5306 if Is_Child_Unit (Gen_Unit)
5307 and then Is_Child_Unit (Act_Decl_Id)
5308 and then Nkind (Gen_Id) = N_Expanded_Name
5309 and then Entity (Prefix (Gen_Id)) = Scope (Act_Decl_Id)
5310 and then Chars (Gen_Unit) = Chars (Act_Decl_Id)
5312 Error_Msg_Node_2 := Scope (Act_Decl_Id);
5314 ("generic unit & is implicitly declared in &",
5315 Defining_Unit_Name (N), Gen_Unit);
5316 Error_Msg_N ("\instance must have different name",
5317 Defining_Unit_Name (N));
5319 end Check_Hidden_Child_Unit;
5321 ------------------------
5322 -- Check_Private_View --
5323 ------------------------
5325 procedure Check_Private_View (N : Node_Id) is
5326 T : constant Entity_Id := Etype (N);
5330 -- Exchange views if the type was not private in the generic but is
5331 -- private at the point of instantiation. Do not exchange views if
5332 -- the scope of the type is in scope. This can happen if both generic
5333 -- and instance are sibling units, or if type is defined in a parent.
5334 -- In this case the visibility of the type will be correct for all
5338 BT := Base_Type (T);
5340 if Is_Private_Type (T)
5341 and then not Has_Private_View (N)
5342 and then Present (Full_View (T))
5343 and then not In_Open_Scopes (Scope (T))
5345 -- In the generic, the full type was visible. Save the private
5346 -- entity, for subsequent exchange.
5350 elsif Has_Private_View (N)
5351 and then not Is_Private_Type (T)
5352 and then not Has_Been_Exchanged (T)
5353 and then Etype (Get_Associated_Node (N)) /= T
5355 -- Only the private declaration was visible in the generic. If
5356 -- the type appears in a subtype declaration, the subtype in the
5357 -- instance must have a view compatible with that of its parent,
5358 -- which must be exchanged (see corresponding code in Restore_
5359 -- Private_Views). Otherwise, if the type is defined in a parent
5360 -- unit, leave full visibility within instance, which is safe.
5362 if In_Open_Scopes (Scope (Base_Type (T)))
5363 and then not Is_Private_Type (Base_Type (T))
5364 and then Comes_From_Source (Base_Type (T))
5368 elsif Nkind (Parent (N)) = N_Subtype_Declaration
5369 or else not In_Private_Part (Scope (Base_Type (T)))
5371 Prepend_Elmt (T, Exchanged_Views);
5372 Exchange_Declarations (Etype (Get_Associated_Node (N)));
5375 -- For composite types with inconsistent representation exchange
5376 -- component types accordingly.
5378 elsif Is_Access_Type (T)
5379 and then Is_Private_Type (Designated_Type (T))
5380 and then not Has_Private_View (N)
5381 and then Present (Full_View (Designated_Type (T)))
5383 Switch_View (Designated_Type (T));
5385 elsif Is_Array_Type (T) then
5386 if Is_Private_Type (Component_Type (T))
5387 and then not Has_Private_View (N)
5388 and then Present (Full_View (Component_Type (T)))
5390 Switch_View (Component_Type (T));
5393 -- The normal exchange mechanism relies on the setting of a
5394 -- flag on the reference in the generic. However, an additional
5395 -- mechanism is needed for types that are not explicitly mentioned
5396 -- in the generic, but may be needed in expanded code in the
5397 -- instance. This includes component types of arrays and
5398 -- designated types of access types. This processing must also
5399 -- include the index types of arrays which we take care of here.
5406 Indx := First_Index (T);
5407 Typ := Base_Type (Etype (Indx));
5408 while Present (Indx) loop
5409 if Is_Private_Type (Typ)
5410 and then Present (Full_View (Typ))
5419 elsif Is_Private_Type (T)
5420 and then Present (Full_View (T))
5421 and then Is_Array_Type (Full_View (T))
5422 and then Is_Private_Type (Component_Type (Full_View (T)))
5426 -- Finally, a non-private subtype may have a private base type, which
5427 -- must be exchanged for consistency. This can happen when a package
5428 -- body is instantiated, when the scope stack is empty but in fact
5429 -- the subtype and the base type are declared in an enclosing scope.
5431 -- Note that in this case we introduce an inconsistency in the view
5432 -- set, because we switch the base type BT, but there could be some
5433 -- private dependent subtypes of BT which remain unswitched. Such
5434 -- subtypes might need to be switched at a later point (see specific
5435 -- provision for that case in Switch_View).
5437 elsif not Is_Private_Type (T)
5438 and then not Has_Private_View (N)
5439 and then Is_Private_Type (BT)
5440 and then Present (Full_View (BT))
5441 and then not Is_Generic_Type (BT)
5442 and then not In_Open_Scopes (BT)
5444 Prepend_Elmt (Full_View (BT), Exchanged_Views);
5445 Exchange_Declarations (BT);
5448 end Check_Private_View;
5450 --------------------------
5451 -- Contains_Instance_Of --
5452 --------------------------
5454 function Contains_Instance_Of
5457 N : Node_Id) return Boolean
5465 -- Verify that there are no circular instantiations. We check whether
5466 -- the unit contains an instance of the current scope or some enclosing
5467 -- scope (in case one of the instances appears in a subunit). Longer
5468 -- circularities involving subunits might seem too pathological to
5469 -- consider, but they were not too pathological for the authors of
5470 -- DEC bc30vsq, so we loop over all enclosing scopes, and mark all
5471 -- enclosing generic scopes as containing an instance.
5474 -- Within a generic subprogram body, the scope is not generic, to
5475 -- allow for recursive subprograms. Use the declaration to determine
5476 -- whether this is a generic unit.
5478 if Ekind (Scop) = E_Generic_Package
5479 or else (Is_Subprogram (Scop)
5480 and then Nkind (Unit_Declaration_Node (Scop)) =
5481 N_Generic_Subprogram_Declaration)
5483 Elmt := First_Elmt (Inner_Instances (Inner));
5485 while Present (Elmt) loop
5486 if Node (Elmt) = Scop then
5487 Error_Msg_Node_2 := Inner;
5489 ("circular Instantiation: & instantiated within &!",
5493 elsif Node (Elmt) = Inner then
5496 elsif Contains_Instance_Of (Node (Elmt), Scop, N) then
5497 Error_Msg_Node_2 := Inner;
5499 ("circular Instantiation: & instantiated within &!",
5507 -- Indicate that Inner is being instantiated within Scop
5509 Append_Elmt (Inner, Inner_Instances (Scop));
5512 if Scop = Standard_Standard then
5515 Scop := Scope (Scop);
5520 end Contains_Instance_Of;
5522 -----------------------
5523 -- Copy_Generic_Node --
5524 -----------------------
5526 function Copy_Generic_Node
5528 Parent_Id : Node_Id;
5529 Instantiating : Boolean) return Node_Id
5534 function Copy_Generic_Descendant (D : Union_Id) return Union_Id;
5535 -- Check the given value of one of the Fields referenced by the
5536 -- current node to determine whether to copy it recursively. The
5537 -- field may hold a Node_Id, a List_Id, or an Elist_Id, or a plain
5538 -- value (Sloc, Uint, Char) in which case it need not be copied.
5540 procedure Copy_Descendants;
5541 -- Common utility for various nodes
5543 function Copy_Generic_Elist (E : Elist_Id) return Elist_Id;
5544 -- Make copy of element list
5546 function Copy_Generic_List
5548 Parent_Id : Node_Id) return List_Id;
5549 -- Apply Copy_Node recursively to the members of a node list
5551 function In_Defining_Unit_Name (Nam : Node_Id) return Boolean;
5552 -- True if an identifier is part of the defining program unit name
5553 -- of a child unit. The entity of such an identifier must be kept
5554 -- (for ASIS use) even though as the name of an enclosing generic
5555 -- it would otherwise not be preserved in the generic tree.
5557 ----------------------
5558 -- Copy_Descendants --
5559 ----------------------
5561 procedure Copy_Descendants is
5563 use Atree.Unchecked_Access;
5564 -- This code section is part of the implementation of an untyped
5565 -- tree traversal, so it needs direct access to node fields.
5568 Set_Field1 (New_N, Copy_Generic_Descendant (Field1 (N)));
5569 Set_Field2 (New_N, Copy_Generic_Descendant (Field2 (N)));
5570 Set_Field3 (New_N, Copy_Generic_Descendant (Field3 (N)));
5571 Set_Field4 (New_N, Copy_Generic_Descendant (Field4 (N)));
5572 Set_Field5 (New_N, Copy_Generic_Descendant (Field5 (N)));
5573 end Copy_Descendants;
5575 -----------------------------
5576 -- Copy_Generic_Descendant --
5577 -----------------------------
5579 function Copy_Generic_Descendant (D : Union_Id) return Union_Id is
5581 if D = Union_Id (Empty) then
5584 elsif D in Node_Range then
5586 (Copy_Generic_Node (Node_Id (D), New_N, Instantiating));
5588 elsif D in List_Range then
5589 return Union_Id (Copy_Generic_List (List_Id (D), New_N));
5591 elsif D in Elist_Range then
5592 return Union_Id (Copy_Generic_Elist (Elist_Id (D)));
5594 -- Nothing else is copyable (e.g. Uint values), return as is
5599 end Copy_Generic_Descendant;
5601 ------------------------
5602 -- Copy_Generic_Elist --
5603 ------------------------
5605 function Copy_Generic_Elist (E : Elist_Id) return Elist_Id is
5612 M := First_Elmt (E);
5613 while Present (M) loop
5615 (Copy_Generic_Node (Node (M), Empty, Instantiating), L);
5624 end Copy_Generic_Elist;
5626 -----------------------
5627 -- Copy_Generic_List --
5628 -----------------------
5630 function Copy_Generic_List
5632 Parent_Id : Node_Id) return List_Id
5640 Set_Parent (New_L, Parent_Id);
5643 while Present (N) loop
5644 Append (Copy_Generic_Node (N, Empty, Instantiating), New_L);
5653 end Copy_Generic_List;
5655 ---------------------------
5656 -- In_Defining_Unit_Name --
5657 ---------------------------
5659 function In_Defining_Unit_Name (Nam : Node_Id) return Boolean is
5661 return Present (Parent (Nam))
5662 and then (Nkind (Parent (Nam)) = N_Defining_Program_Unit_Name
5664 (Nkind (Parent (Nam)) = N_Expanded_Name
5665 and then In_Defining_Unit_Name (Parent (Nam))));
5666 end In_Defining_Unit_Name;
5668 -- Start of processing for Copy_Generic_Node
5675 New_N := New_Copy (N);
5677 if Instantiating then
5678 Adjust_Instantiation_Sloc (New_N, S_Adjustment);
5681 if not Is_List_Member (N) then
5682 Set_Parent (New_N, Parent_Id);
5685 -- If defining identifier, then all fields have been copied already
5687 if Nkind (New_N) in N_Entity then
5690 -- Special casing for identifiers and other entity names and operators
5692 elsif Nkind_In (New_N, N_Identifier,
5693 N_Character_Literal,
5696 or else Nkind (New_N) in N_Op
5698 if not Instantiating then
5700 -- Link both nodes in order to assign subsequently the
5701 -- entity of the copy to the original node, in case this
5702 -- is a global reference.
5704 Set_Associated_Node (N, New_N);
5706 -- If we are within an instantiation, this is a nested generic
5707 -- that has already been analyzed at the point of definition. We
5708 -- must preserve references that were global to the enclosing
5709 -- parent at that point. Other occurrences, whether global or
5710 -- local to the current generic, must be resolved anew, so we
5711 -- reset the entity in the generic copy. A global reference has a
5712 -- smaller depth than the parent, or else the same depth in case
5713 -- both are distinct compilation units.
5714 -- A child unit is implicitly declared within the enclosing parent
5715 -- but is in fact global to it, and must be preserved.
5717 -- It is also possible for Current_Instantiated_Parent to be
5718 -- defined, and for this not to be a nested generic, namely if the
5719 -- unit is loaded through Rtsfind. In that case, the entity of
5720 -- New_N is only a link to the associated node, and not a defining
5723 -- The entities for parent units in the defining_program_unit of a
5724 -- generic child unit are established when the context of the unit
5725 -- is first analyzed, before the generic copy is made. They are
5726 -- preserved in the copy for use in ASIS queries.
5728 Ent := Entity (New_N);
5730 if No (Current_Instantiated_Parent.Gen_Id) then
5732 or else Nkind (Ent) /= N_Defining_Identifier
5733 or else not In_Defining_Unit_Name (N)
5735 Set_Associated_Node (New_N, Empty);
5740 not Nkind_In (Ent, N_Defining_Identifier,
5741 N_Defining_Character_Literal,
5742 N_Defining_Operator_Symbol)
5743 or else No (Scope (Ent))
5745 (Scope (Ent) = Current_Instantiated_Parent.Gen_Id
5746 and then not Is_Child_Unit (Ent))
5748 (Scope_Depth (Scope (Ent)) >
5749 Scope_Depth (Current_Instantiated_Parent.Gen_Id)
5751 Get_Source_Unit (Ent) =
5752 Get_Source_Unit (Current_Instantiated_Parent.Gen_Id))
5754 Set_Associated_Node (New_N, Empty);
5757 -- Case of instantiating identifier or some other name or operator
5760 -- If the associated node is still defined, the entity in it is
5761 -- global, and must be copied to the instance. If this copy is
5762 -- being made for a body to inline, it is applied to an
5763 -- instantiated tree, and the entity is already present and must
5764 -- be also preserved.
5767 Assoc : constant Node_Id := Get_Associated_Node (N);
5770 if Present (Assoc) then
5771 if Nkind (Assoc) = Nkind (N) then
5772 Set_Entity (New_N, Entity (Assoc));
5773 Check_Private_View (N);
5775 elsif Nkind (Assoc) = N_Function_Call then
5776 Set_Entity (New_N, Entity (Name (Assoc)));
5778 elsif Nkind_In (Assoc, N_Defining_Identifier,
5779 N_Defining_Character_Literal,
5780 N_Defining_Operator_Symbol)
5781 and then Expander_Active
5783 -- Inlining case: we are copying a tree that contains
5784 -- global entities, which are preserved in the copy to be
5785 -- used for subsequent inlining.
5790 Set_Entity (New_N, Empty);
5796 -- For expanded name, we must copy the Prefix and Selector_Name
5798 if Nkind (N) = N_Expanded_Name then
5800 (New_N, Copy_Generic_Node (Prefix (N), New_N, Instantiating));
5802 Set_Selector_Name (New_N,
5803 Copy_Generic_Node (Selector_Name (N), New_N, Instantiating));
5805 -- For operators, we must copy the right operand
5807 elsif Nkind (N) in N_Op then
5808 Set_Right_Opnd (New_N,
5809 Copy_Generic_Node (Right_Opnd (N), New_N, Instantiating));
5811 -- And for binary operators, the left operand as well
5813 if Nkind (N) in N_Binary_Op then
5814 Set_Left_Opnd (New_N,
5815 Copy_Generic_Node (Left_Opnd (N), New_N, Instantiating));
5819 -- Special casing for stubs
5821 elsif Nkind (N) in N_Body_Stub then
5823 -- In any case, we must copy the specification or defining
5824 -- identifier as appropriate.
5826 if Nkind (N) = N_Subprogram_Body_Stub then
5827 Set_Specification (New_N,
5828 Copy_Generic_Node (Specification (N), New_N, Instantiating));
5831 Set_Defining_Identifier (New_N,
5833 (Defining_Identifier (N), New_N, Instantiating));
5836 -- If we are not instantiating, then this is where we load and
5837 -- analyze subunits, i.e. at the point where the stub occurs. A
5838 -- more permissible system might defer this analysis to the point
5839 -- of instantiation, but this seems to complicated for now.
5841 if not Instantiating then
5843 Subunit_Name : constant Unit_Name_Type := Get_Unit_Name (N);
5845 Unum : Unit_Number_Type;
5851 (Load_Name => Subunit_Name,
5856 -- If the proper body is not found, a warning message will be
5857 -- emitted when analyzing the stub, or later at the point
5858 -- of instantiation. Here we just leave the stub as is.
5860 if Unum = No_Unit then
5861 Subunits_Missing := True;
5862 goto Subunit_Not_Found;
5865 Subunit := Cunit (Unum);
5867 if Nkind (Unit (Subunit)) /= N_Subunit then
5869 ("found child unit instead of expected SEPARATE subunit",
5871 Error_Msg_Sloc := Sloc (N);
5872 Error_Msg_N ("\to complete stub #", Subunit);
5873 goto Subunit_Not_Found;
5876 -- We must create a generic copy of the subunit, in order to
5877 -- perform semantic analysis on it, and we must replace the
5878 -- stub in the original generic unit with the subunit, in order
5879 -- to preserve non-local references within.
5881 -- Only the proper body needs to be copied. Library_Unit and
5882 -- context clause are simply inherited by the generic copy.
5883 -- Note that the copy (which may be recursive if there are
5884 -- nested subunits) must be done first, before attaching it to
5885 -- the enclosing generic.
5889 (Proper_Body (Unit (Subunit)),
5890 Empty, Instantiating => False);
5892 -- Now place the original proper body in the original generic
5893 -- unit. This is a body, not a compilation unit.
5895 Rewrite (N, Proper_Body (Unit (Subunit)));
5896 Set_Is_Compilation_Unit (Defining_Entity (N), False);
5897 Set_Was_Originally_Stub (N);
5899 -- Finally replace the body of the subunit with its copy, and
5900 -- make this new subunit into the library unit of the generic
5901 -- copy, which does not have stubs any longer.
5903 Set_Proper_Body (Unit (Subunit), New_Body);
5904 Set_Library_Unit (New_N, Subunit);
5905 Inherit_Context (Unit (Subunit), N);
5908 -- If we are instantiating, this must be an error case, since
5909 -- otherwise we would have replaced the stub node by the proper body
5910 -- that corresponds. So just ignore it in the copy (i.e. we have
5911 -- copied it, and that is good enough).
5917 <<Subunit_Not_Found>> null;
5919 -- If the node is a compilation unit, it is the subunit of a stub, which
5920 -- has been loaded already (see code below). In this case, the library
5921 -- unit field of N points to the parent unit (which is a compilation
5922 -- unit) and need not (and cannot!) be copied.
5924 -- When the proper body of the stub is analyzed, the library_unit link
5925 -- is used to establish the proper context (see sem_ch10).
5927 -- The other fields of a compilation unit are copied as usual
5929 elsif Nkind (N) = N_Compilation_Unit then
5931 -- This code can only be executed when not instantiating, because in
5932 -- the copy made for an instantiation, the compilation unit node has
5933 -- disappeared at the point that a stub is replaced by its proper
5936 pragma Assert (not Instantiating);
5938 Set_Context_Items (New_N,
5939 Copy_Generic_List (Context_Items (N), New_N));
5942 Copy_Generic_Node (Unit (N), New_N, False));
5944 Set_First_Inlined_Subprogram (New_N,
5946 (First_Inlined_Subprogram (N), New_N, False));
5948 Set_Aux_Decls_Node (New_N,
5949 Copy_Generic_Node (Aux_Decls_Node (N), New_N, False));
5951 -- For an assignment node, the assignment is known to be semantically
5952 -- legal if we are instantiating the template. This avoids incorrect
5953 -- diagnostics in generated code.
5955 elsif Nkind (N) = N_Assignment_Statement then
5957 -- Copy name and expression fields in usual manner
5960 Copy_Generic_Node (Name (N), New_N, Instantiating));
5962 Set_Expression (New_N,
5963 Copy_Generic_Node (Expression (N), New_N, Instantiating));
5965 if Instantiating then
5966 Set_Assignment_OK (Name (New_N), True);
5969 elsif Nkind_In (N, N_Aggregate, N_Extension_Aggregate) then
5970 if not Instantiating then
5971 Set_Associated_Node (N, New_N);
5974 if Present (Get_Associated_Node (N))
5975 and then Nkind (Get_Associated_Node (N)) = Nkind (N)
5977 -- In the generic the aggregate has some composite type. If at
5978 -- the point of instantiation the type has a private view,
5979 -- install the full view (and that of its ancestors, if any).
5982 T : Entity_Id := (Etype (Get_Associated_Node (New_N)));
5987 and then Is_Private_Type (T)
5993 and then Is_Tagged_Type (T)
5994 and then Is_Derived_Type (T)
5996 Rt := Root_Type (T);
6001 if Is_Private_Type (T) then
6012 -- Do not copy the associated node, which points to
6013 -- the generic copy of the aggregate.
6016 use Atree.Unchecked_Access;
6017 -- This code section is part of the implementation of an untyped
6018 -- tree traversal, so it needs direct access to node fields.
6021 Set_Field1 (New_N, Copy_Generic_Descendant (Field1 (N)));
6022 Set_Field2 (New_N, Copy_Generic_Descendant (Field2 (N)));
6023 Set_Field3 (New_N, Copy_Generic_Descendant (Field3 (N)));
6024 Set_Field5 (New_N, Copy_Generic_Descendant (Field5 (N)));
6027 -- Allocators do not have an identifier denoting the access type,
6028 -- so we must locate it through the expression to check whether
6029 -- the views are consistent.
6031 elsif Nkind (N) = N_Allocator
6032 and then Nkind (Expression (N)) = N_Qualified_Expression
6033 and then Is_Entity_Name (Subtype_Mark (Expression (N)))
6034 and then Instantiating
6037 T : constant Node_Id :=
6038 Get_Associated_Node (Subtype_Mark (Expression (N)));
6044 -- Retrieve the allocator node in the generic copy
6046 Acc_T := Etype (Parent (Parent (T)));
6048 and then Is_Private_Type (Acc_T)
6050 Switch_View (Acc_T);
6057 -- For a proper body, we must catch the case of a proper body that
6058 -- replaces a stub. This represents the point at which a separate
6059 -- compilation unit, and hence template file, may be referenced, so we
6060 -- must make a new source instantiation entry for the template of the
6061 -- subunit, and ensure that all nodes in the subunit are adjusted using
6062 -- this new source instantiation entry.
6064 elsif Nkind (N) in N_Proper_Body then
6066 Save_Adjustment : constant Sloc_Adjustment := S_Adjustment;
6069 if Instantiating and then Was_Originally_Stub (N) then
6070 Create_Instantiation_Source
6071 (Instantiation_Node,
6072 Defining_Entity (N),
6077 -- Now copy the fields of the proper body, using the new
6078 -- adjustment factor if one was needed as per test above.
6082 -- Restore the original adjustment factor in case changed
6084 S_Adjustment := Save_Adjustment;
6087 -- Don't copy Ident or Comment pragmas, since the comment belongs to the
6088 -- generic unit, not to the instantiating unit.
6090 elsif Nkind (N) = N_Pragma
6091 and then Instantiating
6094 Prag_Id : constant Pragma_Id := Get_Pragma_Id (N);
6096 if Prag_Id = Pragma_Ident
6097 or else Prag_Id = Pragma_Comment
6099 New_N := Make_Null_Statement (Sloc (N));
6105 elsif Nkind_In (N, N_Integer_Literal,
6109 -- No descendant fields need traversing
6113 -- For the remaining nodes, copy recursively their descendants
6119 and then Nkind (N) = N_Subprogram_Body
6121 Set_Generic_Parent (Specification (New_N), N);
6126 end Copy_Generic_Node;
6128 ----------------------------
6129 -- Denotes_Formal_Package --
6130 ----------------------------
6132 function Denotes_Formal_Package
6134 On_Exit : Boolean := False) return Boolean
6137 Scop : constant Entity_Id := Scope (Pack);
6144 (Instance_Envs.Last).Instantiated_Parent.Act_Id;
6146 Par := Current_Instantiated_Parent.Act_Id;
6149 if Ekind (Scop) = E_Generic_Package
6150 or else Nkind (Unit_Declaration_Node (Scop)) =
6151 N_Generic_Subprogram_Declaration
6155 elsif Nkind (Original_Node (Unit_Declaration_Node (Pack))) =
6156 N_Formal_Package_Declaration
6164 -- Check whether this package is associated with a formal package of
6165 -- the enclosing instantiation. Iterate over the list of renamings.
6167 E := First_Entity (Par);
6168 while Present (E) loop
6169 if Ekind (E) /= E_Package
6170 or else Nkind (Parent (E)) /= N_Package_Renaming_Declaration
6174 elsif Renamed_Object (E) = Par then
6177 elsif Renamed_Object (E) = Pack then
6186 end Denotes_Formal_Package;
6192 procedure End_Generic is
6194 -- ??? More things could be factored out in this routine. Should
6195 -- probably be done at a later stage.
6197 Inside_A_Generic := Generic_Flags.Table (Generic_Flags.Last);
6198 Generic_Flags.Decrement_Last;
6200 Expander_Mode_Restore;
6203 ----------------------
6204 -- Find_Actual_Type --
6205 ----------------------
6207 function Find_Actual_Type
6209 Gen_Type : Entity_Id) return Entity_Id
6211 Gen_Scope : constant Entity_Id := Scope (Gen_Type);
6215 -- Special processing only applies to child units
6217 if not Is_Child_Unit (Gen_Scope) then
6218 return Get_Instance_Of (Typ);
6220 -- If designated or component type is itself a formal of the child unit,
6221 -- its instance is available.
6223 elsif Scope (Typ) = Gen_Scope then
6224 return Get_Instance_Of (Typ);
6226 -- If the array or access type is not declared in the parent unit,
6227 -- no special processing needed.
6229 elsif not Is_Generic_Type (Typ)
6230 and then Scope (Gen_Scope) /= Scope (Typ)
6232 return Get_Instance_Of (Typ);
6234 -- Otherwise, retrieve designated or component type by visibility
6237 T := Current_Entity (Typ);
6238 while Present (T) loop
6239 if In_Open_Scopes (Scope (T)) then
6242 elsif Is_Generic_Actual_Type (T) then
6251 end Find_Actual_Type;
6253 ----------------------------
6254 -- Freeze_Subprogram_Body --
6255 ----------------------------
6257 procedure Freeze_Subprogram_Body
6258 (Inst_Node : Node_Id;
6260 Pack_Id : Entity_Id)
6263 Gen_Unit : constant Entity_Id := Get_Generic_Entity (Inst_Node);
6264 Par : constant Entity_Id := Scope (Gen_Unit);
6269 function Earlier (N1, N2 : Node_Id) return Boolean;
6270 -- Yields True if N1 and N2 appear in the same compilation unit,
6271 -- ignoring subunits, and if N1 is to the left of N2 in a left-to-right
6272 -- traversal of the tree for the unit.
6274 function Enclosing_Body (N : Node_Id) return Node_Id;
6275 -- Find innermost package body that encloses the given node, and which
6276 -- is not a compilation unit. Freeze nodes for the instance, or for its
6277 -- enclosing body, may be inserted after the enclosing_body of the
6280 function Package_Freeze_Node (B : Node_Id) return Node_Id;
6281 -- Find entity for given package body, and locate or create a freeze
6284 function True_Parent (N : Node_Id) return Node_Id;
6285 -- For a subunit, return parent of corresponding stub
6291 function Earlier (N1, N2 : Node_Id) return Boolean is
6297 procedure Find_Depth (P : in out Node_Id; D : in out Integer);
6298 -- Find distance from given node to enclosing compilation unit
6304 procedure Find_Depth (P : in out Node_Id; D : in out Integer) is
6307 and then Nkind (P) /= N_Compilation_Unit
6309 P := True_Parent (P);
6314 -- Start of processing for Earlier
6317 Find_Depth (P1, D1);
6318 Find_Depth (P2, D2);
6328 P1 := True_Parent (P1);
6333 P2 := True_Parent (P2);
6337 -- At this point P1 and P2 are at the same distance from the root.
6338 -- We examine their parents until we find a common declarative
6339 -- list, at which point we can establish their relative placement
6340 -- by comparing their ultimate slocs. If we reach the root,
6341 -- N1 and N2 do not descend from the same declarative list (e.g.
6342 -- one is nested in the declarative part and the other is in a block
6343 -- in the statement part) and the earlier one is already frozen.
6345 while not Is_List_Member (P1)
6346 or else not Is_List_Member (P2)
6347 or else List_Containing (P1) /= List_Containing (P2)
6349 P1 := True_Parent (P1);
6350 P2 := True_Parent (P2);
6352 if Nkind (Parent (P1)) = N_Subunit then
6353 P1 := Corresponding_Stub (Parent (P1));
6356 if Nkind (Parent (P2)) = N_Subunit then
6357 P2 := Corresponding_Stub (Parent (P2));
6366 Top_Level_Location (Sloc (P1)) < Top_Level_Location (Sloc (P2));
6369 --------------------
6370 -- Enclosing_Body --
6371 --------------------
6373 function Enclosing_Body (N : Node_Id) return Node_Id is
6374 P : Node_Id := Parent (N);
6378 and then Nkind (Parent (P)) /= N_Compilation_Unit
6380 if Nkind (P) = N_Package_Body then
6382 if Nkind (Parent (P)) = N_Subunit then
6383 return Corresponding_Stub (Parent (P));
6389 P := True_Parent (P);
6395 -------------------------
6396 -- Package_Freeze_Node --
6397 -------------------------
6399 function Package_Freeze_Node (B : Node_Id) return Node_Id is
6403 if Nkind (B) = N_Package_Body then
6404 Id := Corresponding_Spec (B);
6406 else pragma Assert (Nkind (B) = N_Package_Body_Stub);
6407 Id := Corresponding_Spec (Proper_Body (Unit (Library_Unit (B))));
6410 Ensure_Freeze_Node (Id);
6411 return Freeze_Node (Id);
6412 end Package_Freeze_Node;
6418 function True_Parent (N : Node_Id) return Node_Id is
6420 if Nkind (Parent (N)) = N_Subunit then
6421 return Parent (Corresponding_Stub (Parent (N)));
6427 -- Start of processing of Freeze_Subprogram_Body
6430 -- If the instance and the generic body appear within the same unit, and
6431 -- the instance precedes the generic, the freeze node for the instance
6432 -- must appear after that of the generic. If the generic is nested
6433 -- within another instance I2, then current instance must be frozen
6434 -- after I2. In both cases, the freeze nodes are those of enclosing
6435 -- packages. Otherwise, the freeze node is placed at the end of the
6436 -- current declarative part.
6438 Enc_G := Enclosing_Body (Gen_Body);
6439 Enc_I := Enclosing_Body (Inst_Node);
6440 Ensure_Freeze_Node (Pack_Id);
6441 F_Node := Freeze_Node (Pack_Id);
6443 if Is_Generic_Instance (Par)
6444 and then Present (Freeze_Node (Par))
6446 In_Same_Declarative_Part (Freeze_Node (Par), Inst_Node)
6448 if ABE_Is_Certain (Get_Package_Instantiation_Node (Par)) then
6450 -- The parent was a premature instantiation. Insert freeze node at
6451 -- the end the current declarative part.
6453 Insert_After_Last_Decl (Inst_Node, F_Node);
6456 Insert_After (Freeze_Node (Par), F_Node);
6459 -- The body enclosing the instance should be frozen after the body that
6460 -- includes the generic, because the body of the instance may make
6461 -- references to entities therein. If the two are not in the same
6462 -- declarative part, or if the one enclosing the instance is frozen
6463 -- already, freeze the instance at the end of the current declarative
6466 elsif Is_Generic_Instance (Par)
6467 and then Present (Freeze_Node (Par))
6468 and then Present (Enc_I)
6470 if In_Same_Declarative_Part (Freeze_Node (Par), Enc_I)
6472 (Nkind (Enc_I) = N_Package_Body
6474 In_Same_Declarative_Part (Freeze_Node (Par), Parent (Enc_I)))
6476 -- The enclosing package may contain several instances. Rather
6477 -- than computing the earliest point at which to insert its
6478 -- freeze node, we place it at the end of the declarative part
6479 -- of the parent of the generic.
6481 Insert_After_Last_Decl
6482 (Freeze_Node (Par), Package_Freeze_Node (Enc_I));
6485 Insert_After_Last_Decl (Inst_Node, F_Node);
6487 elsif Present (Enc_G)
6488 and then Present (Enc_I)
6489 and then Enc_G /= Enc_I
6490 and then Earlier (Inst_Node, Gen_Body)
6492 if Nkind (Enc_G) = N_Package_Body then
6493 E_G_Id := Corresponding_Spec (Enc_G);
6494 else pragma Assert (Nkind (Enc_G) = N_Package_Body_Stub);
6496 Corresponding_Spec (Proper_Body (Unit (Library_Unit (Enc_G))));
6499 -- Freeze package that encloses instance, and place node after
6500 -- package that encloses generic. If enclosing package is already
6501 -- frozen we have to assume it is at the proper place. This may be
6502 -- a potential ABE that requires dynamic checking. Do not add a
6503 -- freeze node if the package that encloses the generic is inside
6504 -- the body that encloses the instance, because the freeze node
6505 -- would be in the wrong scope. Additional contortions needed if
6506 -- the bodies are within a subunit.
6509 Enclosing_Body : Node_Id;
6512 if Nkind (Enc_I) = N_Package_Body_Stub then
6513 Enclosing_Body := Proper_Body (Unit (Library_Unit (Enc_I)));
6515 Enclosing_Body := Enc_I;
6518 if Parent (List_Containing (Enc_G)) /= Enclosing_Body then
6519 Insert_After_Last_Decl (Enc_G, Package_Freeze_Node (Enc_I));
6523 -- Freeze enclosing subunit before instance
6525 Ensure_Freeze_Node (E_G_Id);
6527 if not Is_List_Member (Freeze_Node (E_G_Id)) then
6528 Insert_After (Enc_G, Freeze_Node (E_G_Id));
6531 Insert_After_Last_Decl (Inst_Node, F_Node);
6534 -- If none of the above, insert freeze node at the end of the current
6535 -- declarative part.
6537 Insert_After_Last_Decl (Inst_Node, F_Node);
6539 end Freeze_Subprogram_Body;
6545 function Get_Gen_Id (E : Assoc_Ptr) return Entity_Id is
6547 return Generic_Renamings.Table (E).Gen_Id;
6550 ---------------------
6551 -- Get_Instance_Of --
6552 ---------------------
6554 function Get_Instance_Of (A : Entity_Id) return Entity_Id is
6555 Res : constant Assoc_Ptr := Generic_Renamings_HTable.Get (A);
6558 if Res /= Assoc_Null then
6559 return Generic_Renamings.Table (Res).Act_Id;
6561 -- On exit, entity is not instantiated: not a generic parameter, or
6562 -- else parameter of an inner generic unit.
6566 end Get_Instance_Of;
6568 ------------------------------------
6569 -- Get_Package_Instantiation_Node --
6570 ------------------------------------
6572 function Get_Package_Instantiation_Node (A : Entity_Id) return Node_Id is
6573 Decl : Node_Id := Unit_Declaration_Node (A);
6577 -- If the Package_Instantiation attribute has been set on the package
6578 -- entity, then use it directly when it (or its Original_Node) refers
6579 -- to an N_Package_Instantiation node. In principle it should be
6580 -- possible to have this field set in all cases, which should be
6581 -- investigated, and would allow this function to be significantly
6584 if Present (Package_Instantiation (A)) then
6585 if Nkind (Package_Instantiation (A)) = N_Package_Instantiation then
6586 return Package_Instantiation (A);
6588 elsif Nkind (Original_Node (Package_Instantiation (A))) =
6589 N_Package_Instantiation
6591 return Original_Node (Package_Instantiation (A));
6595 -- If the instantiation is a compilation unit that does not need body
6596 -- then the instantiation node has been rewritten as a package
6597 -- declaration for the instance, and we return the original node.
6599 -- If it is a compilation unit and the instance node has not been
6600 -- rewritten, then it is still the unit of the compilation. Finally, if
6601 -- a body is present, this is a parent of the main unit whose body has
6602 -- been compiled for inlining purposes, and the instantiation node has
6603 -- been rewritten with the instance body.
6605 -- Otherwise the instantiation node appears after the declaration. If
6606 -- the entity is a formal package, the declaration may have been
6607 -- rewritten as a generic declaration (in the case of a formal with box)
6608 -- or left as a formal package declaration if it has actuals, and is
6609 -- found with a forward search.
6611 if Nkind (Parent (Decl)) = N_Compilation_Unit then
6612 if Nkind (Decl) = N_Package_Declaration
6613 and then Present (Corresponding_Body (Decl))
6615 Decl := Unit_Declaration_Node (Corresponding_Body (Decl));
6618 if Nkind (Original_Node (Decl)) = N_Package_Instantiation then
6619 return Original_Node (Decl);
6621 return Unit (Parent (Decl));
6624 elsif Nkind (Decl) = N_Package_Declaration
6625 and then Nkind (Original_Node (Decl)) = N_Formal_Package_Declaration
6627 return Original_Node (Decl);
6630 Inst := Next (Decl);
6631 while not Nkind_In (Inst, N_Package_Instantiation,
6632 N_Formal_Package_Declaration)
6639 end Get_Package_Instantiation_Node;
6641 ------------------------
6642 -- Has_Been_Exchanged --
6643 ------------------------
6645 function Has_Been_Exchanged (E : Entity_Id) return Boolean is
6649 Next := First_Elmt (Exchanged_Views);
6650 while Present (Next) loop
6651 if Full_View (Node (Next)) = E then
6659 end Has_Been_Exchanged;
6665 function Hash (F : Entity_Id) return HTable_Range is
6667 return HTable_Range (F mod HTable_Size);
6670 ------------------------
6671 -- Hide_Current_Scope --
6672 ------------------------
6674 procedure Hide_Current_Scope is
6675 C : constant Entity_Id := Current_Scope;
6679 Set_Is_Hidden_Open_Scope (C);
6681 E := First_Entity (C);
6682 while Present (E) loop
6683 if Is_Immediately_Visible (E) then
6684 Set_Is_Immediately_Visible (E, False);
6685 Append_Elmt (E, Hidden_Entities);
6691 -- Make the scope name invisible as well. This is necessary, but might
6692 -- conflict with calls to Rtsfind later on, in case the scope is a
6693 -- predefined one. There is no clean solution to this problem, so for
6694 -- now we depend on the user not redefining Standard itself in one of
6695 -- the parent units.
6697 if Is_Immediately_Visible (C)
6698 and then C /= Standard_Standard
6700 Set_Is_Immediately_Visible (C, False);
6701 Append_Elmt (C, Hidden_Entities);
6704 end Hide_Current_Scope;
6710 procedure Init_Env is
6711 Saved : Instance_Env;
6714 Saved.Instantiated_Parent := Current_Instantiated_Parent;
6715 Saved.Exchanged_Views := Exchanged_Views;
6716 Saved.Hidden_Entities := Hidden_Entities;
6717 Saved.Current_Sem_Unit := Current_Sem_Unit;
6718 Saved.Parent_Unit_Visible := Parent_Unit_Visible;
6719 Saved.Instance_Parent_Unit := Instance_Parent_Unit;
6721 -- Save configuration switches. These may be reset if the unit is a
6722 -- predefined unit, and the current mode is not Ada 2005.
6724 Save_Opt_Config_Switches (Saved.Switches);
6726 Instance_Envs.Append (Saved);
6728 Exchanged_Views := New_Elmt_List;
6729 Hidden_Entities := New_Elmt_List;
6731 -- Make dummy entry for Instantiated parent. If generic unit is legal,
6732 -- this is set properly in Set_Instance_Env.
6734 Current_Instantiated_Parent :=
6735 (Current_Scope, Current_Scope, Assoc_Null);
6738 ------------------------------
6739 -- In_Same_Declarative_Part --
6740 ------------------------------
6742 function In_Same_Declarative_Part
6744 Inst : Node_Id) return Boolean
6746 Decls : constant Node_Id := Parent (F_Node);
6747 Nod : Node_Id := Parent (Inst);
6750 while Present (Nod) loop
6754 elsif Nkind_In (Nod, N_Subprogram_Body,
6762 elsif Nkind (Nod) = N_Subunit then
6763 Nod := Corresponding_Stub (Nod);
6765 elsif Nkind (Nod) = N_Compilation_Unit then
6769 Nod := Parent (Nod);
6774 end In_Same_Declarative_Part;
6776 ---------------------
6777 -- In_Main_Context --
6778 ---------------------
6780 function In_Main_Context (E : Entity_Id) return Boolean is
6786 if not Is_Compilation_Unit (E)
6787 or else Ekind (E) /= E_Package
6788 or else In_Private_Part (E)
6793 Context := Context_Items (Cunit (Main_Unit));
6795 Clause := First (Context);
6796 while Present (Clause) loop
6797 if Nkind (Clause) = N_With_Clause then
6798 Nam := Name (Clause);
6800 -- If the current scope is part of the context of the main unit,
6801 -- analysis of the corresponding with_clause is not complete, and
6802 -- the entity is not set. We use the Chars field directly, which
6803 -- might produce false positives in rare cases, but guarantees
6804 -- that we produce all the instance bodies we will need.
6806 if (Is_Entity_Name (Nam)
6807 and then Chars (Nam) = Chars (E))
6808 or else (Nkind (Nam) = N_Selected_Component
6809 and then Chars (Selector_Name (Nam)) = Chars (E))
6819 end In_Main_Context;
6821 ---------------------
6822 -- Inherit_Context --
6823 ---------------------
6825 procedure Inherit_Context (Gen_Decl : Node_Id; Inst : Node_Id) is
6826 Current_Context : List_Id;
6827 Current_Unit : Node_Id;
6832 if Nkind (Parent (Gen_Decl)) = N_Compilation_Unit then
6834 -- The inherited context is attached to the enclosing compilation
6835 -- unit. This is either the main unit, or the declaration for the
6836 -- main unit (in case the instantiation appears within the package
6837 -- declaration and the main unit is its body).
6839 Current_Unit := Parent (Inst);
6840 while Present (Current_Unit)
6841 and then Nkind (Current_Unit) /= N_Compilation_Unit
6843 Current_Unit := Parent (Current_Unit);
6846 Current_Context := Context_Items (Current_Unit);
6848 Item := First (Context_Items (Parent (Gen_Decl)));
6849 while Present (Item) loop
6850 if Nkind (Item) = N_With_Clause then
6851 New_I := New_Copy (Item);
6852 Set_Implicit_With (New_I, True);
6853 Append (New_I, Current_Context);
6859 end Inherit_Context;
6865 procedure Initialize is
6867 Generic_Renamings.Init;
6870 Generic_Renamings_HTable.Reset;
6871 Circularity_Detected := False;
6872 Exchanged_Views := No_Elist;
6873 Hidden_Entities := No_Elist;
6876 ----------------------------
6877 -- Insert_After_Last_Decl --
6878 ----------------------------
6880 procedure Insert_After_Last_Decl (N : Node_Id; F_Node : Node_Id) is
6881 L : List_Id := List_Containing (N);
6882 P : constant Node_Id := Parent (L);
6885 if not Is_List_Member (F_Node) then
6886 if Nkind (P) = N_Package_Specification
6887 and then L = Visible_Declarations (P)
6888 and then Present (Private_Declarations (P))
6889 and then not Is_Empty_List (Private_Declarations (P))
6891 L := Private_Declarations (P);
6894 Insert_After (Last (L), F_Node);
6896 end Insert_After_Last_Decl;
6902 procedure Install_Body
6903 (Act_Body : Node_Id;
6908 Act_Id : constant Entity_Id := Corresponding_Spec (Act_Body);
6909 Act_Unit : constant Node_Id := Unit (Cunit (Get_Source_Unit (N)));
6910 Gen_Id : constant Entity_Id := Corresponding_Spec (Gen_Body);
6911 Par : constant Entity_Id := Scope (Gen_Id);
6912 Gen_Unit : constant Node_Id :=
6913 Unit (Cunit (Get_Source_Unit (Gen_Decl)));
6914 Orig_Body : Node_Id := Gen_Body;
6916 Body_Unit : Node_Id;
6918 Must_Delay : Boolean;
6920 function Enclosing_Subp (Id : Entity_Id) return Entity_Id;
6921 -- Find subprogram (if any) that encloses instance and/or generic body
6923 function True_Sloc (N : Node_Id) return Source_Ptr;
6924 -- If the instance is nested inside a generic unit, the Sloc of the
6925 -- instance indicates the place of the original definition, not the
6926 -- point of the current enclosing instance. Pending a better usage of
6927 -- Slocs to indicate instantiation places, we determine the place of
6928 -- origin of a node by finding the maximum sloc of any ancestor node.
6929 -- Why is this not equivalent to Top_Level_Location ???
6931 --------------------
6932 -- Enclosing_Subp --
6933 --------------------
6935 function Enclosing_Subp (Id : Entity_Id) return Entity_Id is
6936 Scop : Entity_Id := Scope (Id);
6939 while Scop /= Standard_Standard
6940 and then not Is_Overloadable (Scop)
6942 Scop := Scope (Scop);
6952 function True_Sloc (N : Node_Id) return Source_Ptr is
6959 while Present (N1) and then N1 /= Act_Unit loop
6960 if Sloc (N1) > Res then
6970 -- Start of processing for Install_Body
6974 -- If the body is a subunit, the freeze point is the corresponding
6975 -- stub in the current compilation, not the subunit itself.
6977 if Nkind (Parent (Gen_Body)) = N_Subunit then
6978 Orig_Body := Corresponding_Stub (Parent (Gen_Body));
6980 Orig_Body := Gen_Body;
6983 Body_Unit := Unit (Cunit (Get_Source_Unit (Orig_Body)));
6985 -- If the instantiation and the generic definition appear in the same
6986 -- package declaration, this is an early instantiation. If they appear
6987 -- in the same declarative part, it is an early instantiation only if
6988 -- the generic body appears textually later, and the generic body is
6989 -- also in the main unit.
6991 -- If instance is nested within a subprogram, and the generic body is
6992 -- not, the instance is delayed because the enclosing body is. If
6993 -- instance and body are within the same scope, or the same sub-
6994 -- program body, indicate explicitly that the instance is delayed.
6997 (Gen_Unit = Act_Unit
6998 and then (Nkind_In (Gen_Unit, N_Package_Declaration,
6999 N_Generic_Package_Declaration)
7000 or else (Gen_Unit = Body_Unit
7001 and then True_Sloc (N) < Sloc (Orig_Body)))
7002 and then Is_In_Main_Unit (Gen_Unit)
7003 and then (Scope (Act_Id) = Scope (Gen_Id)
7005 Enclosing_Subp (Act_Id) = Enclosing_Subp (Gen_Id)));
7007 -- If this is an early instantiation, the freeze node is placed after
7008 -- the generic body. Otherwise, if the generic appears in an instance,
7009 -- we cannot freeze the current instance until the outer one is frozen.
7010 -- This is only relevant if the current instance is nested within some
7011 -- inner scope not itself within the outer instance. If this scope is
7012 -- a package body in the same declarative part as the outer instance,
7013 -- then that body needs to be frozen after the outer instance. Finally,
7014 -- if no delay is needed, we place the freeze node at the end of the
7015 -- current declarative part.
7017 if Expander_Active then
7018 Ensure_Freeze_Node (Act_Id);
7019 F_Node := Freeze_Node (Act_Id);
7022 Insert_After (Orig_Body, F_Node);
7024 elsif Is_Generic_Instance (Par)
7025 and then Present (Freeze_Node (Par))
7026 and then Scope (Act_Id) /= Par
7028 -- Freeze instance of inner generic after instance of enclosing
7031 if In_Same_Declarative_Part (Freeze_Node (Par), N) then
7032 Insert_After (Freeze_Node (Par), F_Node);
7034 -- Freeze package enclosing instance of inner generic after
7035 -- instance of enclosing generic.
7037 elsif Nkind (Parent (N)) = N_Package_Body
7038 and then In_Same_Declarative_Part (Freeze_Node (Par), Parent (N))
7042 Enclosing : constant Entity_Id :=
7043 Corresponding_Spec (Parent (N));
7046 Insert_After_Last_Decl (N, F_Node);
7047 Ensure_Freeze_Node (Enclosing);
7049 if not Is_List_Member (Freeze_Node (Enclosing)) then
7050 Insert_After (Freeze_Node (Par), Freeze_Node (Enclosing));
7055 Insert_After_Last_Decl (N, F_Node);
7059 Insert_After_Last_Decl (N, F_Node);
7063 Set_Is_Frozen (Act_Id);
7064 Insert_Before (N, Act_Body);
7065 Mark_Rewrite_Insertion (Act_Body);
7068 -----------------------------
7069 -- Install_Formal_Packages --
7070 -----------------------------
7072 procedure Install_Formal_Packages (Par : Entity_Id) is
7076 E := First_Entity (Par);
7077 while Present (E) loop
7078 if Ekind (E) = E_Package
7079 and then Nkind (Parent (E)) = N_Package_Renaming_Declaration
7081 -- If this is the renaming for the parent instance, done
7083 if Renamed_Object (E) = Par then
7086 -- The visibility of a formal of an enclosing generic is
7089 elsif Denotes_Formal_Package (E) then
7092 elsif Present (Associated_Formal_Package (E))
7093 and then Box_Present (Parent (Associated_Formal_Package (E)))
7095 Check_Generic_Actuals (Renamed_Object (E), True);
7096 Set_Is_Hidden (E, False);
7102 end Install_Formal_Packages;
7104 --------------------
7105 -- Install_Parent --
7106 --------------------
7108 procedure Install_Parent (P : Entity_Id; In_Body : Boolean := False) is
7109 Ancestors : constant Elist_Id := New_Elmt_List;
7110 S : constant Entity_Id := Current_Scope;
7111 Inst_Par : Entity_Id;
7112 First_Par : Entity_Id;
7113 Inst_Node : Node_Id;
7114 Gen_Par : Entity_Id;
7115 First_Gen : Entity_Id;
7118 procedure Install_Noninstance_Specs (Par : Entity_Id);
7119 -- Install the scopes of noninstance parent units ending with Par
7121 procedure Install_Spec (Par : Entity_Id);
7122 -- The child unit is within the declarative part of the parent, so
7123 -- the declarations within the parent are immediately visible.
7125 -------------------------------
7126 -- Install_Noninstance_Specs --
7127 -------------------------------
7129 procedure Install_Noninstance_Specs (Par : Entity_Id) is
7132 and then Par /= Standard_Standard
7133 and then not In_Open_Scopes (Par)
7135 Install_Noninstance_Specs (Scope (Par));
7138 end Install_Noninstance_Specs;
7144 procedure Install_Spec (Par : Entity_Id) is
7145 Spec : constant Node_Id :=
7146 Specification (Unit_Declaration_Node (Par));
7149 -- If this parent of the child instance is a top-level unit,
7150 -- then record the unit and its visibility for later resetting
7151 -- in Remove_Parent. We exclude units that are generic instances,
7152 -- as we only want to record this information for the ultimate
7153 -- top-level noninstance parent (is that always correct???).
7155 if Scope (Par) = Standard_Standard
7156 and then not Is_Generic_Instance (Par)
7158 Parent_Unit_Visible := Is_Immediately_Visible (Par);
7159 Instance_Parent_Unit := Par;
7162 -- Open the parent scope and make it and its declarations visible.
7163 -- If this point is not within a body, then only the visible
7164 -- declarations should be made visible, and installation of the
7165 -- private declarations is deferred until the appropriate point
7166 -- within analysis of the spec being instantiated (see the handling
7167 -- of parent visibility in Analyze_Package_Specification). This is
7168 -- relaxed in the case where the parent unit is Ada.Tags, to avoid
7169 -- private view problems that occur when compiling instantiations of
7170 -- a generic child of that package (Generic_Dispatching_Constructor).
7171 -- If the instance freezes a tagged type, inlinings of operations
7172 -- from Ada.Tags may need the full view of type Tag. If inlining took
7173 -- proper account of establishing visibility of inlined subprograms'
7174 -- parents then it should be possible to remove this
7175 -- special check. ???
7178 Set_Is_Immediately_Visible (Par);
7179 Install_Visible_Declarations (Par);
7180 Set_Use (Visible_Declarations (Spec));
7182 if In_Body or else Is_RTU (Par, Ada_Tags) then
7183 Install_Private_Declarations (Par);
7184 Set_Use (Private_Declarations (Spec));
7188 -- Start of processing for Install_Parent
7191 -- We need to install the parent instance to compile the instantiation
7192 -- of the child, but the child instance must appear in the current
7193 -- scope. Given that we cannot place the parent above the current scope
7194 -- in the scope stack, we duplicate the current scope and unstack both
7195 -- after the instantiation is complete.
7197 -- If the parent is itself the instantiation of a child unit, we must
7198 -- also stack the instantiation of its parent, and so on. Each such
7199 -- ancestor is the prefix of the name in a prior instantiation.
7201 -- If this is a nested instance, the parent unit itself resolves to
7202 -- a renaming of the parent instance, whose declaration we need.
7204 -- Finally, the parent may be a generic (not an instance) when the
7205 -- child unit appears as a formal package.
7209 if Present (Renamed_Entity (Inst_Par)) then
7210 Inst_Par := Renamed_Entity (Inst_Par);
7213 First_Par := Inst_Par;
7216 Generic_Parent (Specification (Unit_Declaration_Node (Inst_Par)));
7218 First_Gen := Gen_Par;
7220 while Present (Gen_Par)
7221 and then Is_Child_Unit (Gen_Par)
7223 -- Load grandparent instance as well
7225 Inst_Node := Get_Package_Instantiation_Node (Inst_Par);
7227 if Nkind (Name (Inst_Node)) = N_Expanded_Name then
7228 Inst_Par := Entity (Prefix (Name (Inst_Node)));
7230 if Present (Renamed_Entity (Inst_Par)) then
7231 Inst_Par := Renamed_Entity (Inst_Par);
7236 (Specification (Unit_Declaration_Node (Inst_Par)));
7238 if Present (Gen_Par) then
7239 Prepend_Elmt (Inst_Par, Ancestors);
7242 -- Parent is not the name of an instantiation
7244 Install_Noninstance_Specs (Inst_Par);
7256 if Present (First_Gen) then
7257 Append_Elmt (First_Par, Ancestors);
7260 Install_Noninstance_Specs (First_Par);
7263 if not Is_Empty_Elmt_List (Ancestors) then
7264 Elmt := First_Elmt (Ancestors);
7266 while Present (Elmt) loop
7267 Install_Spec (Node (Elmt));
7268 Install_Formal_Packages (Node (Elmt));
7279 --------------------------------
7280 -- Instantiate_Formal_Package --
7281 --------------------------------
7283 function Instantiate_Formal_Package
7286 Analyzed_Formal : Node_Id) return List_Id
7288 Loc : constant Source_Ptr := Sloc (Actual);
7289 Actual_Pack : Entity_Id;
7290 Formal_Pack : Entity_Id;
7291 Gen_Parent : Entity_Id;
7294 Parent_Spec : Node_Id;
7296 procedure Find_Matching_Actual
7298 Act : in out Entity_Id);
7299 -- We need to associate each formal entity in the formal package
7300 -- with the corresponding entity in the actual package. The actual
7301 -- package has been analyzed and possibly expanded, and as a result
7302 -- there is no one-to-one correspondence between the two lists (for
7303 -- example, the actual may include subtypes, itypes, and inherited
7304 -- primitive operations, interspersed among the renaming declarations
7305 -- for the actuals) . We retrieve the corresponding actual by name
7306 -- because each actual has the same name as the formal, and they do
7307 -- appear in the same order.
7309 function Get_Formal_Entity (N : Node_Id) return Entity_Id;
7310 -- Retrieve entity of defining entity of generic formal parameter.
7311 -- Only the declarations of formals need to be considered when
7312 -- linking them to actuals, but the declarative list may include
7313 -- internal entities generated during analysis, and those are ignored.
7315 procedure Match_Formal_Entity
7316 (Formal_Node : Node_Id;
7317 Formal_Ent : Entity_Id;
7318 Actual_Ent : Entity_Id);
7319 -- Associates the formal entity with the actual. In the case
7320 -- where Formal_Ent is a formal package, this procedure iterates
7321 -- through all of its formals and enters associations between the
7322 -- actuals occurring in the formal package's corresponding actual
7323 -- package (given by Actual_Ent) and the formal package's formal
7324 -- parameters. This procedure recurses if any of the parameters is
7325 -- itself a package.
7327 function Is_Instance_Of
7328 (Act_Spec : Entity_Id;
7329 Gen_Anc : Entity_Id) return Boolean;
7330 -- The actual can be an instantiation of a generic within another
7331 -- instance, in which case there is no direct link from it to the
7332 -- original generic ancestor. In that case, we recognize that the
7333 -- ultimate ancestor is the same by examining names and scopes.
7335 procedure Map_Entities (Form : Entity_Id; Act : Entity_Id);
7336 -- Within the generic part, entities in the formal package are
7337 -- visible. To validate subsequent type declarations, indicate
7338 -- the correspondence between the entities in the analyzed formal,
7339 -- and the entities in the actual package. There are three packages
7340 -- involved in the instantiation of a formal package: the parent
7341 -- generic P1 which appears in the generic declaration, the fake
7342 -- instantiation P2 which appears in the analyzed generic, and whose
7343 -- visible entities may be used in subsequent formals, and the actual
7344 -- P3 in the instance. To validate subsequent formals, me indicate
7345 -- that the entities in P2 are mapped into those of P3. The mapping of
7346 -- entities has to be done recursively for nested packages.
7348 procedure Process_Nested_Formal (Formal : Entity_Id);
7349 -- If the current formal is declared with a box, its own formals are
7350 -- visible in the instance, as they were in the generic, and their
7351 -- Hidden flag must be reset. If some of these formals are themselves
7352 -- packages declared with a box, the processing must be recursive.
7354 --------------------------
7355 -- Find_Matching_Actual --
7356 --------------------------
7358 procedure Find_Matching_Actual
7360 Act : in out Entity_Id)
7362 Formal_Ent : Entity_Id;
7365 case Nkind (Original_Node (F)) is
7366 when N_Formal_Object_Declaration |
7367 N_Formal_Type_Declaration =>
7368 Formal_Ent := Defining_Identifier (F);
7370 while Chars (Act) /= Chars (Formal_Ent) loop
7374 when N_Formal_Subprogram_Declaration |
7375 N_Formal_Package_Declaration |
7376 N_Package_Declaration |
7377 N_Generic_Package_Declaration =>
7378 Formal_Ent := Defining_Entity (F);
7380 while Chars (Act) /= Chars (Formal_Ent) loop
7385 raise Program_Error;
7387 end Find_Matching_Actual;
7389 -------------------------
7390 -- Match_Formal_Entity --
7391 -------------------------
7393 procedure Match_Formal_Entity
7394 (Formal_Node : Node_Id;
7395 Formal_Ent : Entity_Id;
7396 Actual_Ent : Entity_Id)
7398 Act_Pkg : Entity_Id;
7401 Set_Instance_Of (Formal_Ent, Actual_Ent);
7403 if Ekind (Actual_Ent) = E_Package then
7405 -- Record associations for each parameter
7407 Act_Pkg := Actual_Ent;
7410 A_Ent : Entity_Id := First_Entity (Act_Pkg);
7419 -- Retrieve the actual given in the formal package declaration
7421 Actual := Entity (Name (Original_Node (Formal_Node)));
7423 -- The actual in the formal package declaration may be a
7424 -- renamed generic package, in which case we want to retrieve
7425 -- the original generic in order to traverse its formal part.
7427 if Present (Renamed_Entity (Actual)) then
7428 Gen_Decl := Unit_Declaration_Node (Renamed_Entity (Actual));
7430 Gen_Decl := Unit_Declaration_Node (Actual);
7433 Formals := Generic_Formal_Declarations (Gen_Decl);
7435 if Present (Formals) then
7436 F_Node := First_Non_Pragma (Formals);
7441 while Present (A_Ent)
7442 and then Present (F_Node)
7443 and then A_Ent /= First_Private_Entity (Act_Pkg)
7445 F_Ent := Get_Formal_Entity (F_Node);
7447 if Present (F_Ent) then
7449 -- This is a formal of the original package. Record
7450 -- association and recurse.
7452 Find_Matching_Actual (F_Node, A_Ent);
7453 Match_Formal_Entity (F_Node, F_Ent, A_Ent);
7454 Next_Entity (A_Ent);
7457 Next_Non_Pragma (F_Node);
7461 end Match_Formal_Entity;
7463 -----------------------
7464 -- Get_Formal_Entity --
7465 -----------------------
7467 function Get_Formal_Entity (N : Node_Id) return Entity_Id is
7468 Kind : constant Node_Kind := Nkind (Original_Node (N));
7471 when N_Formal_Object_Declaration =>
7472 return Defining_Identifier (N);
7474 when N_Formal_Type_Declaration =>
7475 return Defining_Identifier (N);
7477 when N_Formal_Subprogram_Declaration =>
7478 return Defining_Unit_Name (Specification (N));
7480 when N_Formal_Package_Declaration =>
7481 return Defining_Identifier (Original_Node (N));
7483 when N_Generic_Package_Declaration =>
7484 return Defining_Identifier (Original_Node (N));
7486 -- All other declarations are introduced by semantic analysis and
7487 -- have no match in the actual.
7492 end Get_Formal_Entity;
7494 --------------------
7495 -- Is_Instance_Of --
7496 --------------------
7498 function Is_Instance_Of
7499 (Act_Spec : Entity_Id;
7500 Gen_Anc : Entity_Id) return Boolean
7502 Gen_Par : constant Entity_Id := Generic_Parent (Act_Spec);
7505 if No (Gen_Par) then
7508 -- Simplest case: the generic parent of the actual is the formal
7510 elsif Gen_Par = Gen_Anc then
7513 elsif Chars (Gen_Par) /= Chars (Gen_Anc) then
7516 -- The actual may be obtained through several instantiations. Its
7517 -- scope must itself be an instance of a generic declared in the
7518 -- same scope as the formal. Any other case is detected above.
7520 elsif not Is_Generic_Instance (Scope (Gen_Par)) then
7524 return Generic_Parent (Parent (Scope (Gen_Par))) = Scope (Gen_Anc);
7532 procedure Map_Entities (Form : Entity_Id; Act : Entity_Id) is
7537 Set_Instance_Of (Form, Act);
7539 -- Traverse formal and actual package to map the corresponding
7540 -- entities. We skip over internal entities that may be generated
7541 -- during semantic analysis, and find the matching entities by
7542 -- name, given that they must appear in the same order.
7544 E1 := First_Entity (Form);
7545 E2 := First_Entity (Act);
7547 and then E1 /= First_Private_Entity (Form)
7549 -- Could this test be a single condition???
7550 -- Seems like it could, and isn't FPE (Form) a constant anyway???
7552 if not Is_Internal (E1)
7553 and then Present (Parent (E1))
7554 and then not Is_Class_Wide_Type (E1)
7555 and then not Is_Internal_Name (Chars (E1))
7558 and then Chars (E2) /= Chars (E1)
7566 Set_Instance_Of (E1, E2);
7569 and then Is_Tagged_Type (E2)
7572 (Class_Wide_Type (E1), Class_Wide_Type (E2));
7575 if Ekind (E1) = E_Package
7576 and then No (Renamed_Object (E1))
7578 Map_Entities (E1, E2);
7587 ---------------------------
7588 -- Process_Nested_Formal --
7589 ---------------------------
7591 procedure Process_Nested_Formal (Formal : Entity_Id) is
7595 if Present (Associated_Formal_Package (Formal))
7596 and then Box_Present (Parent (Associated_Formal_Package (Formal)))
7598 Ent := First_Entity (Formal);
7599 while Present (Ent) loop
7600 Set_Is_Hidden (Ent, False);
7601 Set_Is_Visible_Formal (Ent);
7602 Set_Is_Potentially_Use_Visible
7603 (Ent, Is_Potentially_Use_Visible (Formal));
7605 if Ekind (Ent) = E_Package then
7606 exit when Renamed_Entity (Ent) = Renamed_Entity (Formal);
7607 Process_Nested_Formal (Ent);
7613 end Process_Nested_Formal;
7615 -- Start of processing for Instantiate_Formal_Package
7620 if not Is_Entity_Name (Actual)
7621 or else Ekind (Entity (Actual)) /= E_Package
7624 ("expect package instance to instantiate formal", Actual);
7625 Abandon_Instantiation (Actual);
7626 raise Program_Error;
7629 Actual_Pack := Entity (Actual);
7630 Set_Is_Instantiated (Actual_Pack);
7632 -- The actual may be a renamed package, or an outer generic formal
7633 -- package whose instantiation is converted into a renaming.
7635 if Present (Renamed_Object (Actual_Pack)) then
7636 Actual_Pack := Renamed_Object (Actual_Pack);
7639 if Nkind (Analyzed_Formal) = N_Formal_Package_Declaration then
7640 Gen_Parent := Get_Instance_Of (Entity (Name (Analyzed_Formal)));
7641 Formal_Pack := Defining_Identifier (Analyzed_Formal);
7644 Generic_Parent (Specification (Analyzed_Formal));
7646 Defining_Unit_Name (Specification (Analyzed_Formal));
7649 if Nkind (Parent (Actual_Pack)) = N_Defining_Program_Unit_Name then
7650 Parent_Spec := Specification (Unit_Declaration_Node (Actual_Pack));
7652 Parent_Spec := Parent (Actual_Pack);
7655 if Gen_Parent = Any_Id then
7657 ("previous error in declaration of formal package", Actual);
7658 Abandon_Instantiation (Actual);
7661 Is_Instance_Of (Parent_Spec, Get_Instance_Of (Gen_Parent))
7667 ("actual parameter must be instance of&", Actual, Gen_Parent);
7668 Abandon_Instantiation (Actual);
7671 Set_Instance_Of (Defining_Identifier (Formal), Actual_Pack);
7672 Map_Entities (Formal_Pack, Actual_Pack);
7675 Make_Package_Renaming_Declaration (Loc,
7676 Defining_Unit_Name => New_Copy (Defining_Identifier (Formal)),
7677 Name => New_Reference_To (Actual_Pack, Loc));
7679 Set_Associated_Formal_Package (Defining_Unit_Name (Nod),
7680 Defining_Identifier (Formal));
7681 Decls := New_List (Nod);
7683 -- If the formal F has a box, then the generic declarations are
7684 -- visible in the generic G. In an instance of G, the corresponding
7685 -- entities in the actual for F (which are the actuals for the
7686 -- instantiation of the generic that F denotes) must also be made
7687 -- visible for analysis of the current instance. On exit from the
7688 -- current instance, those entities are made private again. If the
7689 -- actual is currently in use, these entities are also use-visible.
7691 -- The loop through the actual entities also steps through the formal
7692 -- entities and enters associations from formals to actuals into the
7693 -- renaming map. This is necessary to properly handle checking of
7694 -- actual parameter associations for later formals that depend on
7695 -- actuals declared in the formal package.
7697 -- In Ada 2005, partial parametrization requires that we make visible
7698 -- the actuals corresponding to formals that were defaulted in the
7699 -- formal package. There formals are identified because they remain
7700 -- formal generics within the formal package, rather than being
7701 -- renamings of the actuals supplied.
7704 Gen_Decl : constant Node_Id :=
7705 Unit_Declaration_Node (Gen_Parent);
7706 Formals : constant List_Id :=
7707 Generic_Formal_Declarations (Gen_Decl);
7709 Actual_Ent : Entity_Id;
7710 Actual_Of_Formal : Node_Id;
7711 Formal_Node : Node_Id;
7712 Formal_Ent : Entity_Id;
7715 if Present (Formals) then
7716 Formal_Node := First_Non_Pragma (Formals);
7718 Formal_Node := Empty;
7721 Actual_Ent := First_Entity (Actual_Pack);
7723 First (Visible_Declarations (Specification (Analyzed_Formal)));
7724 while Present (Actual_Ent)
7725 and then Actual_Ent /= First_Private_Entity (Actual_Pack)
7727 if Present (Formal_Node) then
7728 Formal_Ent := Get_Formal_Entity (Formal_Node);
7730 if Present (Formal_Ent) then
7731 Find_Matching_Actual (Formal_Node, Actual_Ent);
7733 (Formal_Node, Formal_Ent, Actual_Ent);
7735 -- We iterate at the same time over the actuals of the
7736 -- local package created for the formal, to determine
7737 -- which one of the formals of the original generic were
7738 -- defaulted in the formal. The corresponding actual
7739 -- entities are visible in the enclosing instance.
7741 if Box_Present (Formal)
7743 (Present (Actual_Of_Formal)
7746 (Get_Formal_Entity (Actual_Of_Formal)))
7748 Set_Is_Hidden (Actual_Ent, False);
7749 Set_Is_Visible_Formal (Actual_Ent);
7750 Set_Is_Potentially_Use_Visible
7751 (Actual_Ent, In_Use (Actual_Pack));
7753 if Ekind (Actual_Ent) = E_Package then
7754 Process_Nested_Formal (Actual_Ent);
7758 Set_Is_Hidden (Actual_Ent);
7759 Set_Is_Potentially_Use_Visible (Actual_Ent, False);
7763 Next_Non_Pragma (Formal_Node);
7764 Next (Actual_Of_Formal);
7767 -- No further formals to match, but the generic part may
7768 -- contain inherited operation that are not hidden in the
7769 -- enclosing instance.
7771 Next_Entity (Actual_Ent);
7775 -- Inherited subprograms generated by formal derived types are
7776 -- also visible if the types are.
7778 Actual_Ent := First_Entity (Actual_Pack);
7779 while Present (Actual_Ent)
7780 and then Actual_Ent /= First_Private_Entity (Actual_Pack)
7782 if Is_Overloadable (Actual_Ent)
7784 Nkind (Parent (Actual_Ent)) = N_Subtype_Declaration
7786 not Is_Hidden (Defining_Identifier (Parent (Actual_Ent)))
7788 Set_Is_Hidden (Actual_Ent, False);
7789 Set_Is_Potentially_Use_Visible
7790 (Actual_Ent, In_Use (Actual_Pack));
7793 Next_Entity (Actual_Ent);
7797 -- If the formal is not declared with a box, reanalyze it as an
7798 -- abbreviated instantiation, to verify the matching rules of 12.7.
7799 -- The actual checks are performed after the generic associations
7800 -- have been analyzed, to guarantee the same visibility for this
7801 -- instantiation and for the actuals.
7803 -- In Ada 2005, the generic associations for the formal can include
7804 -- defaulted parameters. These are ignored during check. This
7805 -- internal instantiation is removed from the tree after conformance
7806 -- checking, because it contains formal declarations for those
7807 -- defaulted parameters, and those should not reach the back-end.
7809 if not Box_Present (Formal) then
7811 I_Pack : constant Entity_Id :=
7812 Make_Defining_Identifier (Sloc (Actual),
7813 Chars => New_Internal_Name ('P'));
7816 Set_Is_Internal (I_Pack);
7819 Make_Package_Instantiation (Sloc (Actual),
7820 Defining_Unit_Name => I_Pack,
7823 (Get_Instance_Of (Gen_Parent), Sloc (Actual)),
7824 Generic_Associations =>
7825 Generic_Associations (Formal)));
7831 end Instantiate_Formal_Package;
7833 -----------------------------------
7834 -- Instantiate_Formal_Subprogram --
7835 -----------------------------------
7837 function Instantiate_Formal_Subprogram
7840 Analyzed_Formal : Node_Id) return Node_Id
7843 Formal_Sub : constant Entity_Id :=
7844 Defining_Unit_Name (Specification (Formal));
7845 Analyzed_S : constant Entity_Id :=
7846 Defining_Unit_Name (Specification (Analyzed_Formal));
7847 Decl_Node : Node_Id;
7851 function From_Parent_Scope (Subp : Entity_Id) return Boolean;
7852 -- If the generic is a child unit, the parent has been installed on the
7853 -- scope stack, but a default subprogram cannot resolve to something on
7854 -- the parent because that parent is not really part of the visible
7855 -- context (it is there to resolve explicit local entities). If the
7856 -- default has resolved in this way, we remove the entity from
7857 -- immediate visibility and analyze the node again to emit an error
7858 -- message or find another visible candidate.
7860 procedure Valid_Actual_Subprogram (Act : Node_Id);
7861 -- Perform legality check and raise exception on failure
7863 -----------------------
7864 -- From_Parent_Scope --
7865 -----------------------
7867 function From_Parent_Scope (Subp : Entity_Id) return Boolean is
7868 Gen_Scope : Node_Id;
7871 Gen_Scope := Scope (Analyzed_S);
7872 while Present (Gen_Scope)
7873 and then Is_Child_Unit (Gen_Scope)
7875 if Scope (Subp) = Scope (Gen_Scope) then
7879 Gen_Scope := Scope (Gen_Scope);
7883 end From_Parent_Scope;
7885 -----------------------------
7886 -- Valid_Actual_Subprogram --
7887 -----------------------------
7889 procedure Valid_Actual_Subprogram (Act : Node_Id) is
7893 if Is_Entity_Name (Act) then
7894 Act_E := Entity (Act);
7896 elsif Nkind (Act) = N_Selected_Component
7897 and then Is_Entity_Name (Selector_Name (Act))
7899 Act_E := Entity (Selector_Name (Act));
7905 if (Present (Act_E) and then Is_Overloadable (Act_E))
7906 or else Nkind_In (Act, N_Attribute_Reference,
7907 N_Indexed_Component,
7908 N_Character_Literal,
7909 N_Explicit_Dereference)
7915 ("expect subprogram or entry name in instantiation of&",
7916 Instantiation_Node, Formal_Sub);
7917 Abandon_Instantiation (Instantiation_Node);
7919 end Valid_Actual_Subprogram;
7921 -- Start of processing for Instantiate_Formal_Subprogram
7924 New_Spec := New_Copy_Tree (Specification (Formal));
7926 -- The tree copy has created the proper instantiation sloc for the
7927 -- new specification. Use this location for all other constructed
7930 Loc := Sloc (Defining_Unit_Name (New_Spec));
7932 -- Create new entity for the actual (New_Copy_Tree does not)
7934 Set_Defining_Unit_Name
7935 (New_Spec, Make_Defining_Identifier (Loc, Chars (Formal_Sub)));
7937 -- Create new entities for the each of the formals in the
7938 -- specification of the renaming declaration built for the actual.
7940 if Present (Parameter_Specifications (New_Spec)) then
7944 F := First (Parameter_Specifications (New_Spec));
7945 while Present (F) loop
7946 Set_Defining_Identifier (F,
7947 Make_Defining_Identifier (Sloc (F),
7948 Chars => Chars (Defining_Identifier (F))));
7954 -- Find entity of actual. If the actual is an attribute reference, it
7955 -- cannot be resolved here (its formal is missing) but is handled
7956 -- instead in Attribute_Renaming. If the actual is overloaded, it is
7957 -- fully resolved subsequently, when the renaming declaration for the
7958 -- formal is analyzed. If it is an explicit dereference, resolve the
7959 -- prefix but not the actual itself, to prevent interpretation as call.
7961 if Present (Actual) then
7962 Loc := Sloc (Actual);
7963 Set_Sloc (New_Spec, Loc);
7965 if Nkind (Actual) = N_Operator_Symbol then
7966 Find_Direct_Name (Actual);
7968 elsif Nkind (Actual) = N_Explicit_Dereference then
7969 Analyze (Prefix (Actual));
7971 elsif Nkind (Actual) /= N_Attribute_Reference then
7975 Valid_Actual_Subprogram (Actual);
7978 elsif Present (Default_Name (Formal)) then
7979 if not Nkind_In (Default_Name (Formal), N_Attribute_Reference,
7980 N_Selected_Component,
7981 N_Indexed_Component,
7982 N_Character_Literal)
7983 and then Present (Entity (Default_Name (Formal)))
7985 Nam := New_Occurrence_Of (Entity (Default_Name (Formal)), Loc);
7987 Nam := New_Copy (Default_Name (Formal));
7988 Set_Sloc (Nam, Loc);
7991 elsif Box_Present (Formal) then
7993 -- Actual is resolved at the point of instantiation. Create an
7994 -- identifier or operator with the same name as the formal.
7996 if Nkind (Formal_Sub) = N_Defining_Operator_Symbol then
7997 Nam := Make_Operator_Symbol (Loc,
7998 Chars => Chars (Formal_Sub),
7999 Strval => No_String);
8001 Nam := Make_Identifier (Loc, Chars (Formal_Sub));
8004 elsif Nkind (Specification (Formal)) = N_Procedure_Specification
8005 and then Null_Present (Specification (Formal))
8007 -- Generate null body for procedure, for use in the instance
8010 Make_Subprogram_Body (Loc,
8011 Specification => New_Spec,
8012 Declarations => New_List,
8013 Handled_Statement_Sequence =>
8014 Make_Handled_Sequence_Of_Statements (Loc,
8015 Statements => New_List (Make_Null_Statement (Loc))));
8017 Set_Is_Intrinsic_Subprogram (Defining_Unit_Name (New_Spec));
8021 Error_Msg_Sloc := Sloc (Scope (Analyzed_S));
8023 ("missing actual&", Instantiation_Node, Formal_Sub);
8025 ("\in instantiation of & declared#",
8026 Instantiation_Node, Scope (Analyzed_S));
8027 Abandon_Instantiation (Instantiation_Node);
8031 Make_Subprogram_Renaming_Declaration (Loc,
8032 Specification => New_Spec,
8035 -- If we do not have an actual and the formal specified <> then set to
8036 -- get proper default.
8038 if No (Actual) and then Box_Present (Formal) then
8039 Set_From_Default (Decl_Node);
8042 -- Gather possible interpretations for the actual before analyzing the
8043 -- instance. If overloaded, it will be resolved when analyzing the
8044 -- renaming declaration.
8046 if Box_Present (Formal)
8047 and then No (Actual)
8051 if Is_Child_Unit (Scope (Analyzed_S))
8052 and then Present (Entity (Nam))
8054 if not Is_Overloaded (Nam) then
8056 if From_Parent_Scope (Entity (Nam)) then
8057 Set_Is_Immediately_Visible (Entity (Nam), False);
8058 Set_Entity (Nam, Empty);
8059 Set_Etype (Nam, Empty);
8063 Set_Is_Immediately_Visible (Entity (Nam));
8072 Get_First_Interp (Nam, I, It);
8074 while Present (It.Nam) loop
8075 if From_Parent_Scope (It.Nam) then
8079 Get_Next_Interp (I, It);
8086 -- The generic instantiation freezes the actual. This can only be done
8087 -- once the actual is resolved, in the analysis of the renaming
8088 -- declaration. To make the formal subprogram entity available, we set
8089 -- Corresponding_Formal_Spec to point to the formal subprogram entity.
8090 -- This is also needed in Analyze_Subprogram_Renaming for the processing
8091 -- of formal abstract subprograms.
8093 Set_Corresponding_Formal_Spec (Decl_Node, Analyzed_S);
8095 -- We cannot analyze the renaming declaration, and thus find the actual,
8096 -- until all the actuals are assembled in the instance. For subsequent
8097 -- checks of other actuals, indicate the node that will hold the
8098 -- instance of this formal.
8100 Set_Instance_Of (Analyzed_S, Nam);
8102 if Nkind (Actual) = N_Selected_Component
8103 and then Is_Task_Type (Etype (Prefix (Actual)))
8104 and then not Is_Frozen (Etype (Prefix (Actual)))
8106 -- The renaming declaration will create a body, which must appear
8107 -- outside of the instantiation, We move the renaming declaration
8108 -- out of the instance, and create an additional renaming inside,
8109 -- to prevent freezing anomalies.
8112 Anon_Id : constant Entity_Id :=
8113 Make_Defining_Identifier
8114 (Loc, New_Internal_Name ('E'));
8116 Set_Defining_Unit_Name (New_Spec, Anon_Id);
8117 Insert_Before (Instantiation_Node, Decl_Node);
8118 Analyze (Decl_Node);
8120 -- Now create renaming within the instance
8123 Make_Subprogram_Renaming_Declaration (Loc,
8124 Specification => New_Copy_Tree (New_Spec),
8125 Name => New_Occurrence_Of (Anon_Id, Loc));
8127 Set_Defining_Unit_Name (Specification (Decl_Node),
8128 Make_Defining_Identifier (Loc, Chars (Formal_Sub)));
8133 end Instantiate_Formal_Subprogram;
8135 ------------------------
8136 -- Instantiate_Object --
8137 ------------------------
8139 function Instantiate_Object
8142 Analyzed_Formal : Node_Id) return List_Id
8144 Acc_Def : Node_Id := Empty;
8145 Act_Assoc : constant Node_Id := Parent (Actual);
8146 Actual_Decl : Node_Id := Empty;
8147 Formal_Id : constant Entity_Id := Defining_Identifier (Formal);
8148 Decl_Node : Node_Id;
8151 List : constant List_Id := New_List;
8152 Loc : constant Source_Ptr := Sloc (Actual);
8153 Orig_Ftyp : constant Entity_Id :=
8154 Etype (Defining_Identifier (Analyzed_Formal));
8155 Subt_Decl : Node_Id := Empty;
8156 Subt_Mark : Node_Id := Empty;
8159 if Present (Subtype_Mark (Formal)) then
8160 Subt_Mark := Subtype_Mark (Formal);
8162 Check_Access_Definition (Formal);
8163 Acc_Def := Access_Definition (Formal);
8166 -- Sloc for error message on missing actual
8168 Error_Msg_Sloc := Sloc (Scope (Defining_Identifier (Analyzed_Formal)));
8170 if Get_Instance_Of (Formal_Id) /= Formal_Id then
8171 Error_Msg_N ("duplicate instantiation of generic parameter", Actual);
8174 Set_Parent (List, Parent (Actual));
8178 if Out_Present (Formal) then
8180 -- An IN OUT generic actual must be a name. The instantiation is a
8181 -- renaming declaration. The actual is the name being renamed. We
8182 -- use the actual directly, rather than a copy, because it is not
8183 -- used further in the list of actuals, and because a copy or a use
8184 -- of relocate_node is incorrect if the instance is nested within a
8185 -- generic. In order to simplify ASIS searches, the Generic_Parent
8186 -- field links the declaration to the generic association.
8191 Instantiation_Node, Formal_Id);
8193 ("\in instantiation of & declared#",
8195 Scope (Defining_Identifier (Analyzed_Formal)));
8196 Abandon_Instantiation (Instantiation_Node);
8199 if Present (Subt_Mark) then
8201 Make_Object_Renaming_Declaration (Loc,
8202 Defining_Identifier => New_Copy (Formal_Id),
8203 Subtype_Mark => New_Copy_Tree (Subt_Mark),
8206 else pragma Assert (Present (Acc_Def));
8208 Make_Object_Renaming_Declaration (Loc,
8209 Defining_Identifier => New_Copy (Formal_Id),
8210 Access_Definition => New_Copy_Tree (Acc_Def),
8214 Set_Corresponding_Generic_Association (Decl_Node, Act_Assoc);
8216 -- The analysis of the actual may produce insert_action nodes, so
8217 -- the declaration must have a context in which to attach them.
8219 Append (Decl_Node, List);
8222 -- Return if the analysis of the actual reported some error
8224 if Etype (Actual) = Any_Type then
8228 -- This check is performed here because Analyze_Object_Renaming will
8229 -- not check it when Comes_From_Source is False. Note though that the
8230 -- check for the actual being the name of an object will be performed
8231 -- in Analyze_Object_Renaming.
8233 if Is_Object_Reference (Actual)
8234 and then Is_Dependent_Component_Of_Mutable_Object (Actual)
8237 ("illegal discriminant-dependent component for in out parameter",
8241 -- The actual has to be resolved in order to check that it is a
8242 -- variable (due to cases such as F(1), where F returns
8243 -- access to an array, and for overloaded prefixes).
8246 Get_Instance_Of (Etype (Defining_Identifier (Analyzed_Formal)));
8248 if Is_Private_Type (Ftyp)
8249 and then not Is_Private_Type (Etype (Actual))
8250 and then (Base_Type (Full_View (Ftyp)) = Base_Type (Etype (Actual))
8251 or else Base_Type (Etype (Actual)) = Ftyp)
8253 -- If the actual has the type of the full view of the formal, or
8254 -- else a non-private subtype of the formal, then the visibility
8255 -- of the formal type has changed. Add to the actuals a subtype
8256 -- declaration that will force the exchange of views in the body
8257 -- of the instance as well.
8260 Make_Subtype_Declaration (Loc,
8261 Defining_Identifier =>
8262 Make_Defining_Identifier (Loc, New_Internal_Name ('P')),
8263 Subtype_Indication => New_Occurrence_Of (Ftyp, Loc));
8265 Prepend (Subt_Decl, List);
8267 Prepend_Elmt (Full_View (Ftyp), Exchanged_Views);
8268 Exchange_Declarations (Ftyp);
8271 Resolve (Actual, Ftyp);
8273 if not Denotes_Variable (Actual) then
8275 ("actual for& must be a variable", Actual, Formal_Id);
8277 elsif Base_Type (Ftyp) /= Base_Type (Etype (Actual)) then
8279 -- Ada 2005 (AI-423): For a generic formal object of mode in out,
8280 -- the type of the actual shall resolve to a specific anonymous
8283 if Ada_Version < Ada_05
8285 Ekind (Base_Type (Ftyp)) /=
8286 E_Anonymous_Access_Type
8288 Ekind (Base_Type (Etype (Actual))) /=
8289 E_Anonymous_Access_Type
8291 Error_Msg_NE ("type of actual does not match type of&",
8296 Note_Possible_Modification (Actual, Sure => True);
8298 -- Check for instantiation of atomic/volatile actual for
8299 -- non-atomic/volatile formal (RM C.6 (12)).
8301 if Is_Atomic_Object (Actual)
8302 and then not Is_Atomic (Orig_Ftyp)
8305 ("cannot instantiate non-atomic formal object " &
8306 "with atomic actual", Actual);
8308 elsif Is_Volatile_Object (Actual)
8309 and then not Is_Volatile (Orig_Ftyp)
8312 ("cannot instantiate non-volatile formal object " &
8313 "with volatile actual", Actual);
8319 -- The instantiation of a generic formal in-parameter is constant
8320 -- declaration. The actual is the expression for that declaration.
8322 if Present (Actual) then
8323 if Present (Subt_Mark) then
8325 else pragma Assert (Present (Acc_Def));
8330 Make_Object_Declaration (Loc,
8331 Defining_Identifier => New_Copy (Formal_Id),
8332 Constant_Present => True,
8333 Null_Exclusion_Present => Null_Exclusion_Present (Formal),
8334 Object_Definition => New_Copy_Tree (Def),
8335 Expression => Actual);
8337 Set_Corresponding_Generic_Association (Decl_Node, Act_Assoc);
8339 -- A generic formal object of a tagged type is defined to be
8340 -- aliased so the new constant must also be treated as aliased.
8343 (Etype (Defining_Identifier (Analyzed_Formal)))
8345 Set_Aliased_Present (Decl_Node);
8348 Append (Decl_Node, List);
8350 -- No need to repeat (pre-)analysis of some expression nodes
8351 -- already handled in Preanalyze_Actuals.
8353 if Nkind (Actual) /= N_Allocator then
8356 -- Return if the analysis of the actual reported some error
8358 if Etype (Actual) = Any_Type then
8364 Typ : constant Entity_Id :=
8366 (Etype (Defining_Identifier (Analyzed_Formal)));
8369 Freeze_Before (Instantiation_Node, Typ);
8371 -- If the actual is an aggregate, perform name resolution on
8372 -- its components (the analysis of an aggregate does not do it)
8373 -- to capture local names that may be hidden if the generic is
8376 if Nkind (Actual) = N_Aggregate then
8377 Preanalyze_And_Resolve (Actual, Typ);
8380 if Is_Limited_Type (Typ)
8381 and then not OK_For_Limited_Init (Actual)
8384 ("initialization not allowed for limited types", Actual);
8385 Explain_Limited_Type (Typ, Actual);
8389 elsif Present (Default_Expression (Formal)) then
8391 -- Use default to construct declaration
8393 if Present (Subt_Mark) then
8395 else pragma Assert (Present (Acc_Def));
8400 Make_Object_Declaration (Sloc (Formal),
8401 Defining_Identifier => New_Copy (Formal_Id),
8402 Constant_Present => True,
8403 Null_Exclusion_Present => Null_Exclusion_Present (Formal),
8404 Object_Definition => New_Copy (Def),
8405 Expression => New_Copy_Tree
8406 (Default_Expression (Formal)));
8408 Append (Decl_Node, List);
8409 Set_Analyzed (Expression (Decl_Node), False);
8414 Instantiation_Node, Formal_Id);
8415 Error_Msg_NE ("\in instantiation of & declared#",
8417 Scope (Defining_Identifier (Analyzed_Formal)));
8420 (Etype (Defining_Identifier (Analyzed_Formal)))
8422 -- Create dummy constant declaration so that instance can be
8423 -- analyzed, to minimize cascaded visibility errors.
8425 if Present (Subt_Mark) then
8427 else pragma Assert (Present (Acc_Def));
8432 Make_Object_Declaration (Loc,
8433 Defining_Identifier => New_Copy (Formal_Id),
8434 Constant_Present => True,
8435 Null_Exclusion_Present => Null_Exclusion_Present (Formal),
8436 Object_Definition => New_Copy (Def),
8438 Make_Attribute_Reference (Sloc (Formal_Id),
8439 Attribute_Name => Name_First,
8440 Prefix => New_Copy (Def)));
8442 Append (Decl_Node, List);
8445 Abandon_Instantiation (Instantiation_Node);
8450 if Nkind (Actual) in N_Has_Entity then
8451 Actual_Decl := Parent (Entity (Actual));
8454 -- Ada 2005 (AI-423): For a formal object declaration with a null
8455 -- exclusion or an access definition that has a null exclusion: If the
8456 -- actual matching the formal object declaration denotes a generic
8457 -- formal object of another generic unit G, and the instantiation
8458 -- containing the actual occurs within the body of G or within the body
8459 -- of a generic unit declared within the declarative region of G, then
8460 -- the declaration of the formal object of G must have a null exclusion.
8461 -- Otherwise, the subtype of the actual matching the formal object
8462 -- declaration shall exclude null.
8464 if Ada_Version >= Ada_05
8465 and then Present (Actual_Decl)
8467 Nkind_In (Actual_Decl, N_Formal_Object_Declaration,
8468 N_Object_Declaration)
8469 and then Nkind (Analyzed_Formal) = N_Formal_Object_Declaration
8470 and then not Has_Null_Exclusion (Actual_Decl)
8471 and then Has_Null_Exclusion (Analyzed_Formal)
8473 Error_Msg_Sloc := Sloc (Analyzed_Formal);
8475 ("actual must exclude null to match generic formal#", Actual);
8479 end Instantiate_Object;
8481 ------------------------------
8482 -- Instantiate_Package_Body --
8483 ------------------------------
8485 procedure Instantiate_Package_Body
8486 (Body_Info : Pending_Body_Info;
8487 Inlined_Body : Boolean := False;
8488 Body_Optional : Boolean := False)
8490 Act_Decl : constant Node_Id := Body_Info.Act_Decl;
8491 Inst_Node : constant Node_Id := Body_Info.Inst_Node;
8492 Loc : constant Source_Ptr := Sloc (Inst_Node);
8494 Gen_Id : constant Node_Id := Name (Inst_Node);
8495 Gen_Unit : constant Entity_Id := Get_Generic_Entity (Inst_Node);
8496 Gen_Decl : constant Node_Id := Unit_Declaration_Node (Gen_Unit);
8497 Act_Spec : constant Node_Id := Specification (Act_Decl);
8498 Act_Decl_Id : constant Entity_Id := Defining_Entity (Act_Spec);
8500 Act_Body_Name : Node_Id;
8502 Gen_Body_Id : Node_Id;
8504 Act_Body_Id : Entity_Id;
8506 Parent_Installed : Boolean := False;
8507 Save_Style_Check : constant Boolean := Style_Check;
8510 Gen_Body_Id := Corresponding_Body (Gen_Decl);
8512 -- The instance body may already have been processed, as the parent of
8513 -- another instance that is inlined (Load_Parent_Of_Generic).
8515 if Present (Corresponding_Body (Instance_Spec (Inst_Node))) then
8519 Expander_Mode_Save_And_Set (Body_Info.Expander_Status);
8521 -- Re-establish the state of information on which checks are suppressed.
8522 -- This information was set in Body_Info at the point of instantiation,
8523 -- and now we restore it so that the instance is compiled using the
8524 -- check status at the instantiation (RM 11.5 (7.2/2), AI95-00224-01).
8526 Local_Suppress_Stack_Top := Body_Info.Local_Suppress_Stack_Top;
8527 Scope_Suppress := Body_Info.Scope_Suppress;
8529 if No (Gen_Body_Id) then
8530 Load_Parent_Of_Generic
8531 (Inst_Node, Specification (Gen_Decl), Body_Optional);
8532 Gen_Body_Id := Corresponding_Body (Gen_Decl);
8535 -- Establish global variable for sloc adjustment and for error recovery
8537 Instantiation_Node := Inst_Node;
8539 if Present (Gen_Body_Id) then
8540 Save_Env (Gen_Unit, Act_Decl_Id);
8541 Style_Check := False;
8542 Current_Sem_Unit := Body_Info.Current_Sem_Unit;
8544 Gen_Body := Unit_Declaration_Node (Gen_Body_Id);
8546 Create_Instantiation_Source
8547 (Inst_Node, Gen_Body_Id, False, S_Adjustment);
8551 (Original_Node (Gen_Body), Empty, Instantiating => True);
8553 -- Build new name (possibly qualified) for body declaration
8555 Act_Body_Id := New_Copy (Act_Decl_Id);
8557 -- Some attributes of spec entity are not inherited by body entity
8559 Set_Handler_Records (Act_Body_Id, No_List);
8561 if Nkind (Defining_Unit_Name (Act_Spec)) =
8562 N_Defining_Program_Unit_Name
8565 Make_Defining_Program_Unit_Name (Loc,
8566 Name => New_Copy_Tree (Name (Defining_Unit_Name (Act_Spec))),
8567 Defining_Identifier => Act_Body_Id);
8569 Act_Body_Name := Act_Body_Id;
8572 Set_Defining_Unit_Name (Act_Body, Act_Body_Name);
8574 Set_Corresponding_Spec (Act_Body, Act_Decl_Id);
8575 Check_Generic_Actuals (Act_Decl_Id, False);
8577 -- If it is a child unit, make the parent instance (which is an
8578 -- instance of the parent of the generic) visible. The parent
8579 -- instance is the prefix of the name of the generic unit.
8581 if Ekind (Scope (Gen_Unit)) = E_Generic_Package
8582 and then Nkind (Gen_Id) = N_Expanded_Name
8584 Install_Parent (Entity (Prefix (Gen_Id)), In_Body => True);
8585 Parent_Installed := True;
8587 elsif Is_Child_Unit (Gen_Unit) then
8588 Install_Parent (Scope (Gen_Unit), In_Body => True);
8589 Parent_Installed := True;
8592 -- If the instantiation is a library unit, and this is the main unit,
8593 -- then build the resulting compilation unit nodes for the instance.
8594 -- If this is a compilation unit but it is not the main unit, then it
8595 -- is the body of a unit in the context, that is being compiled
8596 -- because it is encloses some inlined unit or another generic unit
8597 -- being instantiated. In that case, this body is not part of the
8598 -- current compilation, and is not attached to the tree, but its
8599 -- parent must be set for analysis.
8601 if Nkind (Parent (Inst_Node)) = N_Compilation_Unit then
8603 -- Replace instance node with body of instance, and create new
8604 -- node for corresponding instance declaration.
8606 Build_Instance_Compilation_Unit_Nodes
8607 (Inst_Node, Act_Body, Act_Decl);
8608 Analyze (Inst_Node);
8610 if Parent (Inst_Node) = Cunit (Main_Unit) then
8612 -- If the instance is a child unit itself, then set the scope
8613 -- of the expanded body to be the parent of the instantiation
8614 -- (ensuring that the fully qualified name will be generated
8615 -- for the elaboration subprogram).
8617 if Nkind (Defining_Unit_Name (Act_Spec)) =
8618 N_Defining_Program_Unit_Name
8621 (Defining_Entity (Inst_Node), Scope (Act_Decl_Id));
8625 -- Case where instantiation is not a library unit
8628 -- If this is an early instantiation, i.e. appears textually
8629 -- before the corresponding body and must be elaborated first,
8630 -- indicate that the body instance is to be delayed.
8632 Install_Body (Act_Body, Inst_Node, Gen_Body, Gen_Decl);
8634 -- Now analyze the body. We turn off all checks if this is an
8635 -- internal unit, since there is no reason to have checks on for
8636 -- any predefined run-time library code. All such code is designed
8637 -- to be compiled with checks off.
8639 -- Note that we do NOT apply this criterion to children of GNAT
8640 -- (or on VMS, children of DEC). The latter units must suppress
8641 -- checks explicitly if this is needed.
8643 if Is_Predefined_File_Name
8644 (Unit_File_Name (Get_Source_Unit (Gen_Decl)))
8646 Analyze (Act_Body, Suppress => All_Checks);
8652 Inherit_Context (Gen_Body, Inst_Node);
8654 -- Remove the parent instances if they have been placed on the scope
8655 -- stack to compile the body.
8657 if Parent_Installed then
8658 Remove_Parent (In_Body => True);
8661 Restore_Private_Views (Act_Decl_Id);
8663 -- Remove the current unit from visibility if this is an instance
8664 -- that is not elaborated on the fly for inlining purposes.
8666 if not Inlined_Body then
8667 Set_Is_Immediately_Visible (Act_Decl_Id, False);
8671 Style_Check := Save_Style_Check;
8673 -- If we have no body, and the unit requires a body, then complain. This
8674 -- complaint is suppressed if we have detected other errors (since a
8675 -- common reason for missing the body is that it had errors).
8677 elsif Unit_Requires_Body (Gen_Unit)
8678 and then not Body_Optional
8680 if Serious_Errors_Detected = 0 then
8682 ("cannot find body of generic package &", Inst_Node, Gen_Unit);
8684 -- Don't attempt to perform any cleanup actions if some other error
8685 -- was already detected, since this can cause blowups.
8691 -- Case of package that does not need a body
8694 -- If the instantiation of the declaration is a library unit, rewrite
8695 -- the original package instantiation as a package declaration in the
8696 -- compilation unit node.
8698 if Nkind (Parent (Inst_Node)) = N_Compilation_Unit then
8699 Set_Parent_Spec (Act_Decl, Parent_Spec (Inst_Node));
8700 Rewrite (Inst_Node, Act_Decl);
8702 -- Generate elaboration entity, in case spec has elaboration code.
8703 -- This cannot be done when the instance is analyzed, because it
8704 -- is not known yet whether the body exists.
8706 Set_Elaboration_Entity_Required (Act_Decl_Id, False);
8707 Build_Elaboration_Entity (Parent (Inst_Node), Act_Decl_Id);
8709 -- If the instantiation is not a library unit, then append the
8710 -- declaration to the list of implicitly generated entities, unless
8711 -- it is already a list member which means that it was already
8714 elsif not Is_List_Member (Act_Decl) then
8715 Mark_Rewrite_Insertion (Act_Decl);
8716 Insert_Before (Inst_Node, Act_Decl);
8720 Expander_Mode_Restore;
8721 end Instantiate_Package_Body;
8723 ---------------------------------
8724 -- Instantiate_Subprogram_Body --
8725 ---------------------------------
8727 procedure Instantiate_Subprogram_Body
8728 (Body_Info : Pending_Body_Info;
8729 Body_Optional : Boolean := False)
8731 Act_Decl : constant Node_Id := Body_Info.Act_Decl;
8732 Inst_Node : constant Node_Id := Body_Info.Inst_Node;
8733 Loc : constant Source_Ptr := Sloc (Inst_Node);
8734 Gen_Id : constant Node_Id := Name (Inst_Node);
8735 Gen_Unit : constant Entity_Id := Get_Generic_Entity (Inst_Node);
8736 Gen_Decl : constant Node_Id := Unit_Declaration_Node (Gen_Unit);
8737 Anon_Id : constant Entity_Id :=
8738 Defining_Unit_Name (Specification (Act_Decl));
8739 Pack_Id : constant Entity_Id :=
8740 Defining_Unit_Name (Parent (Act_Decl));
8743 Gen_Body_Id : Node_Id;
8745 Pack_Body : Node_Id;
8746 Prev_Formal : Entity_Id;
8748 Unit_Renaming : Node_Id;
8750 Parent_Installed : Boolean := False;
8751 Save_Style_Check : constant Boolean := Style_Check;
8754 Gen_Body_Id := Corresponding_Body (Gen_Decl);
8756 -- Subprogram body may have been created already because of an inline
8757 -- pragma, or because of multiple elaborations of the enclosing package
8758 -- when several instances of the subprogram appear in the main unit.
8760 if Present (Corresponding_Body (Act_Decl)) then
8764 Expander_Mode_Save_And_Set (Body_Info.Expander_Status);
8766 -- Re-establish the state of information on which checks are suppressed.
8767 -- This information was set in Body_Info at the point of instantiation,
8768 -- and now we restore it so that the instance is compiled using the
8769 -- check status at the instantiation (RM 11.5 (7.2/2), AI95-00224-01).
8771 Local_Suppress_Stack_Top := Body_Info.Local_Suppress_Stack_Top;
8772 Scope_Suppress := Body_Info.Scope_Suppress;
8774 if No (Gen_Body_Id) then
8776 -- For imported generic subprogram, no body to compile, complete
8777 -- the spec entity appropriately.
8779 if Is_Imported (Gen_Unit) then
8780 Set_Is_Imported (Anon_Id);
8781 Set_First_Rep_Item (Anon_Id, First_Rep_Item (Gen_Unit));
8782 Set_Interface_Name (Anon_Id, Interface_Name (Gen_Unit));
8783 Set_Convention (Anon_Id, Convention (Gen_Unit));
8784 Set_Has_Completion (Anon_Id);
8787 -- For other cases, compile the body
8790 Load_Parent_Of_Generic
8791 (Inst_Node, Specification (Gen_Decl), Body_Optional);
8792 Gen_Body_Id := Corresponding_Body (Gen_Decl);
8796 Instantiation_Node := Inst_Node;
8798 if Present (Gen_Body_Id) then
8799 Gen_Body := Unit_Declaration_Node (Gen_Body_Id);
8801 if Nkind (Gen_Body) = N_Subprogram_Body_Stub then
8803 -- Either body is not present, or context is non-expanding, as
8804 -- when compiling a subunit. Mark the instance as completed, and
8805 -- diagnose a missing body when needed.
8808 and then Operating_Mode = Generate_Code
8811 ("missing proper body for instantiation", Gen_Body);
8814 Set_Has_Completion (Anon_Id);
8818 Save_Env (Gen_Unit, Anon_Id);
8819 Style_Check := False;
8820 Current_Sem_Unit := Body_Info.Current_Sem_Unit;
8821 Create_Instantiation_Source
8829 (Original_Node (Gen_Body), Empty, Instantiating => True);
8831 -- Create proper defining name for the body, to correspond to
8832 -- the one in the spec.
8834 Set_Defining_Unit_Name (Specification (Act_Body),
8835 Make_Defining_Identifier
8836 (Sloc (Defining_Entity (Inst_Node)), Chars (Anon_Id)));
8837 Set_Corresponding_Spec (Act_Body, Anon_Id);
8838 Set_Has_Completion (Anon_Id);
8839 Check_Generic_Actuals (Pack_Id, False);
8841 -- Generate a reference to link the visible subprogram instance to
8842 -- the generic body, which for navigation purposes is the only
8843 -- available source for the instance.
8846 (Related_Instance (Pack_Id),
8847 Gen_Body_Id, 'b', Set_Ref => False, Force => True);
8849 -- If it is a child unit, make the parent instance (which is an
8850 -- instance of the parent of the generic) visible. The parent
8851 -- instance is the prefix of the name of the generic unit.
8853 if Ekind (Scope (Gen_Unit)) = E_Generic_Package
8854 and then Nkind (Gen_Id) = N_Expanded_Name
8856 Install_Parent (Entity (Prefix (Gen_Id)), In_Body => True);
8857 Parent_Installed := True;
8859 elsif Is_Child_Unit (Gen_Unit) then
8860 Install_Parent (Scope (Gen_Unit), In_Body => True);
8861 Parent_Installed := True;
8864 -- Inside its body, a reference to the generic unit is a reference
8865 -- to the instance. The corresponding renaming is the first
8866 -- declaration in the body.
8869 Make_Subprogram_Renaming_Declaration (Loc,
8872 Specification (Original_Node (Gen_Body)),
8874 Instantiating => True),
8875 Name => New_Occurrence_Of (Anon_Id, Loc));
8877 -- If there is a formal subprogram with the same name as the unit
8878 -- itself, do not add this renaming declaration. This is a temporary
8879 -- fix for one ACVC test. ???
8881 Prev_Formal := First_Entity (Pack_Id);
8882 while Present (Prev_Formal) loop
8883 if Chars (Prev_Formal) = Chars (Gen_Unit)
8884 and then Is_Overloadable (Prev_Formal)
8889 Next_Entity (Prev_Formal);
8892 if Present (Prev_Formal) then
8893 Decls := New_List (Act_Body);
8895 Decls := New_List (Unit_Renaming, Act_Body);
8898 -- The subprogram body is placed in the body of a dummy package body,
8899 -- whose spec contains the subprogram declaration as well as the
8900 -- renaming declarations for the generic parameters.
8902 Pack_Body := Make_Package_Body (Loc,
8903 Defining_Unit_Name => New_Copy (Pack_Id),
8904 Declarations => Decls);
8906 Set_Corresponding_Spec (Pack_Body, Pack_Id);
8908 -- If the instantiation is a library unit, then build resulting
8909 -- compilation unit nodes for the instance. The declaration of
8910 -- the enclosing package is the grandparent of the subprogram
8911 -- declaration. First replace the instantiation node as the unit
8912 -- of the corresponding compilation.
8914 if Nkind (Parent (Inst_Node)) = N_Compilation_Unit then
8915 if Parent (Inst_Node) = Cunit (Main_Unit) then
8916 Set_Unit (Parent (Inst_Node), Inst_Node);
8917 Build_Instance_Compilation_Unit_Nodes
8918 (Inst_Node, Pack_Body, Parent (Parent (Act_Decl)));
8919 Analyze (Inst_Node);
8921 Set_Parent (Pack_Body, Parent (Inst_Node));
8922 Analyze (Pack_Body);
8926 Insert_Before (Inst_Node, Pack_Body);
8927 Mark_Rewrite_Insertion (Pack_Body);
8928 Analyze (Pack_Body);
8930 if Expander_Active then
8931 Freeze_Subprogram_Body (Inst_Node, Gen_Body, Pack_Id);
8935 Inherit_Context (Gen_Body, Inst_Node);
8937 Restore_Private_Views (Pack_Id, False);
8939 if Parent_Installed then
8940 Remove_Parent (In_Body => True);
8944 Style_Check := Save_Style_Check;
8946 -- Body not found. Error was emitted already. If there were no previous
8947 -- errors, this may be an instance whose scope is a premature instance.
8948 -- In that case we must insure that the (legal) program does raise
8949 -- program error if executed. We generate a subprogram body for this
8950 -- purpose. See DEC ac30vso.
8952 -- Should not reference proprietary DEC tests in comments ???
8954 elsif Serious_Errors_Detected = 0
8955 and then Nkind (Parent (Inst_Node)) /= N_Compilation_Unit
8957 if Body_Optional then
8960 elsif Ekind (Anon_Id) = E_Procedure then
8962 Make_Subprogram_Body (Loc,
8964 Make_Procedure_Specification (Loc,
8965 Defining_Unit_Name =>
8966 Make_Defining_Identifier (Loc, Chars (Anon_Id)),
8967 Parameter_Specifications =>
8969 (Parameter_Specifications (Parent (Anon_Id)))),
8971 Declarations => Empty_List,
8972 Handled_Statement_Sequence =>
8973 Make_Handled_Sequence_Of_Statements (Loc,
8976 Make_Raise_Program_Error (Loc,
8978 PE_Access_Before_Elaboration))));
8982 Make_Raise_Program_Error (Loc,
8983 Reason => PE_Access_Before_Elaboration);
8985 Set_Etype (Ret_Expr, (Etype (Anon_Id)));
8986 Set_Analyzed (Ret_Expr);
8989 Make_Subprogram_Body (Loc,
8991 Make_Function_Specification (Loc,
8992 Defining_Unit_Name =>
8993 Make_Defining_Identifier (Loc, Chars (Anon_Id)),
8994 Parameter_Specifications =>
8996 (Parameter_Specifications (Parent (Anon_Id))),
8997 Result_Definition =>
8998 New_Occurrence_Of (Etype (Anon_Id), Loc)),
9000 Declarations => Empty_List,
9001 Handled_Statement_Sequence =>
9002 Make_Handled_Sequence_Of_Statements (Loc,
9005 (Make_Simple_Return_Statement (Loc, Ret_Expr))));
9008 Pack_Body := Make_Package_Body (Loc,
9009 Defining_Unit_Name => New_Copy (Pack_Id),
9010 Declarations => New_List (Act_Body));
9012 Insert_After (Inst_Node, Pack_Body);
9013 Set_Corresponding_Spec (Pack_Body, Pack_Id);
9014 Analyze (Pack_Body);
9017 Expander_Mode_Restore;
9018 end Instantiate_Subprogram_Body;
9020 ----------------------
9021 -- Instantiate_Type --
9022 ----------------------
9024 function Instantiate_Type
9027 Analyzed_Formal : Node_Id;
9028 Actual_Decls : List_Id) return List_Id
9030 Gen_T : constant Entity_Id := Defining_Identifier (Formal);
9031 A_Gen_T : constant Entity_Id :=
9032 Defining_Identifier (Analyzed_Formal);
9033 Ancestor : Entity_Id := Empty;
9034 Def : constant Node_Id := Formal_Type_Definition (Formal);
9036 Decl_Node : Node_Id;
9037 Decl_Nodes : List_Id;
9041 procedure Validate_Array_Type_Instance;
9042 procedure Validate_Access_Subprogram_Instance;
9043 procedure Validate_Access_Type_Instance;
9044 procedure Validate_Derived_Type_Instance;
9045 procedure Validate_Derived_Interface_Type_Instance;
9046 procedure Validate_Interface_Type_Instance;
9047 procedure Validate_Private_Type_Instance;
9048 -- These procedures perform validation tests for the named case
9050 function Subtypes_Match (Gen_T, Act_T : Entity_Id) return Boolean;
9051 -- Check that base types are the same and that the subtypes match
9052 -- statically. Used in several of the above.
9054 --------------------
9055 -- Subtypes_Match --
9056 --------------------
9058 function Subtypes_Match (Gen_T, Act_T : Entity_Id) return Boolean is
9059 T : constant Entity_Id := Get_Instance_Of (Gen_T);
9062 return (Base_Type (T) = Base_Type (Act_T)
9063 and then Subtypes_Statically_Match (T, Act_T))
9065 or else (Is_Class_Wide_Type (Gen_T)
9066 and then Is_Class_Wide_Type (Act_T)
9069 (Get_Instance_Of (Root_Type (Gen_T)),
9073 ((Ekind (Gen_T) = E_Anonymous_Access_Subprogram_Type
9074 or else Ekind (Gen_T) = E_Anonymous_Access_Type)
9075 and then Ekind (Act_T) = Ekind (Gen_T)
9077 Subtypes_Statically_Match
9078 (Designated_Type (Gen_T), Designated_Type (Act_T)));
9081 -----------------------------------------
9082 -- Validate_Access_Subprogram_Instance --
9083 -----------------------------------------
9085 procedure Validate_Access_Subprogram_Instance is
9087 if not Is_Access_Type (Act_T)
9088 or else Ekind (Designated_Type (Act_T)) /= E_Subprogram_Type
9091 ("expect access type in instantiation of &", Actual, Gen_T);
9092 Abandon_Instantiation (Actual);
9095 Check_Mode_Conformant
9096 (Designated_Type (Act_T),
9097 Designated_Type (A_Gen_T),
9101 if Ekind (Base_Type (Act_T)) = E_Access_Protected_Subprogram_Type then
9102 if Ekind (A_Gen_T) = E_Access_Subprogram_Type then
9104 ("protected access type not allowed for formal &",
9108 elsif Ekind (A_Gen_T) = E_Access_Protected_Subprogram_Type then
9110 ("expect protected access type for formal &",
9113 end Validate_Access_Subprogram_Instance;
9115 -----------------------------------
9116 -- Validate_Access_Type_Instance --
9117 -----------------------------------
9119 procedure Validate_Access_Type_Instance is
9120 Desig_Type : constant Entity_Id :=
9121 Find_Actual_Type (Designated_Type (A_Gen_T), A_Gen_T);
9122 Desig_Act : Entity_Id;
9125 if not Is_Access_Type (Act_T) then
9127 ("expect access type in instantiation of &", Actual, Gen_T);
9128 Abandon_Instantiation (Actual);
9131 if Is_Access_Constant (A_Gen_T) then
9132 if not Is_Access_Constant (Act_T) then
9134 ("actual type must be access-to-constant type", Actual);
9135 Abandon_Instantiation (Actual);
9138 if Is_Access_Constant (Act_T) then
9140 ("actual type must be access-to-variable type", Actual);
9141 Abandon_Instantiation (Actual);
9143 elsif Ekind (A_Gen_T) = E_General_Access_Type
9144 and then Ekind (Base_Type (Act_T)) /= E_General_Access_Type
9146 Error_Msg_N ("actual must be general access type!", Actual);
9147 Error_Msg_NE ("add ALL to }!", Actual, Act_T);
9148 Abandon_Instantiation (Actual);
9152 -- The designated subtypes, that is to say the subtypes introduced
9153 -- by an access type declaration (and not by a subtype declaration)
9156 Desig_Act := Designated_Type (Base_Type (Act_T));
9158 -- The designated type may have been introduced through a limited_
9159 -- with clause, in which case retrieve the non-limited view. This
9160 -- applies to incomplete types as well as to class-wide types.
9162 if From_With_Type (Desig_Act) then
9163 Desig_Act := Available_View (Desig_Act);
9166 if not Subtypes_Match
9167 (Desig_Type, Desig_Act) then
9169 ("designated type of actual does not match that of formal &",
9171 Abandon_Instantiation (Actual);
9173 elsif Is_Access_Type (Designated_Type (Act_T))
9174 and then Is_Constrained (Designated_Type (Designated_Type (Act_T)))
9176 Is_Constrained (Designated_Type (Desig_Type))
9179 ("designated type of actual does not match that of formal &",
9181 Abandon_Instantiation (Actual);
9184 -- Ada 2005: null-exclusion indicators of the two types must agree
9186 if Can_Never_Be_Null (A_Gen_T) /= Can_Never_Be_Null (Act_T) then
9188 ("non null exclusion of actual and formal & do not match",
9191 end Validate_Access_Type_Instance;
9193 ----------------------------------
9194 -- Validate_Array_Type_Instance --
9195 ----------------------------------
9197 procedure Validate_Array_Type_Instance is
9202 function Formal_Dimensions return Int;
9203 -- Count number of dimensions in array type formal
9205 -----------------------
9206 -- Formal_Dimensions --
9207 -----------------------
9209 function Formal_Dimensions return Int is
9214 if Nkind (Def) = N_Constrained_Array_Definition then
9215 Index := First (Discrete_Subtype_Definitions (Def));
9217 Index := First (Subtype_Marks (Def));
9220 while Present (Index) loop
9226 end Formal_Dimensions;
9228 -- Start of processing for Validate_Array_Type_Instance
9231 if not Is_Array_Type (Act_T) then
9233 ("expect array type in instantiation of &", Actual, Gen_T);
9234 Abandon_Instantiation (Actual);
9236 elsif Nkind (Def) = N_Constrained_Array_Definition then
9237 if not (Is_Constrained (Act_T)) then
9239 ("expect constrained array in instantiation of &",
9241 Abandon_Instantiation (Actual);
9245 if Is_Constrained (Act_T) then
9247 ("expect unconstrained array in instantiation of &",
9249 Abandon_Instantiation (Actual);
9253 if Formal_Dimensions /= Number_Dimensions (Act_T) then
9255 ("dimensions of actual do not match formal &", Actual, Gen_T);
9256 Abandon_Instantiation (Actual);
9259 I1 := First_Index (A_Gen_T);
9260 I2 := First_Index (Act_T);
9261 for J in 1 .. Formal_Dimensions loop
9263 -- If the indices of the actual were given by a subtype_mark,
9264 -- the index was transformed into a range attribute. Retrieve
9265 -- the original type mark for checking.
9267 if Is_Entity_Name (Original_Node (I2)) then
9268 T2 := Entity (Original_Node (I2));
9273 if not Subtypes_Match
9274 (Find_Actual_Type (Etype (I1), A_Gen_T), T2)
9277 ("index types of actual do not match those of formal &",
9279 Abandon_Instantiation (Actual);
9286 -- Check matching subtypes. Note that there are complex visibility
9287 -- issues when the generic is a child unit and some aspect of the
9288 -- generic type is declared in a parent unit of the generic. We do
9289 -- the test to handle this special case only after a direct check
9290 -- for static matching has failed.
9293 (Component_Type (A_Gen_T), Component_Type (Act_T))
9294 or else Subtypes_Match
9295 (Find_Actual_Type (Component_Type (A_Gen_T), A_Gen_T),
9296 Component_Type (Act_T))
9301 ("component subtype of actual does not match that of formal &",
9303 Abandon_Instantiation (Actual);
9306 if Has_Aliased_Components (A_Gen_T)
9307 and then not Has_Aliased_Components (Act_T)
9310 ("actual must have aliased components to match formal type &",
9313 end Validate_Array_Type_Instance;
9315 -----------------------------------------------
9316 -- Validate_Derived_Interface_Type_Instance --
9317 -----------------------------------------------
9319 procedure Validate_Derived_Interface_Type_Instance is
9320 Par : constant Entity_Id := Entity (Subtype_Indication (Def));
9324 -- First apply interface instance checks
9326 Validate_Interface_Type_Instance;
9328 -- Verify that immediate parent interface is an ancestor of
9332 and then not Interface_Present_In_Ancestor (Act_T, Par)
9335 ("interface actual must include progenitor&", Actual, Par);
9338 -- Now verify that the actual includes all other ancestors of
9341 Elmt := First_Elmt (Interfaces (A_Gen_T));
9342 while Present (Elmt) loop
9343 if not Interface_Present_In_Ancestor
9344 (Act_T, Get_Instance_Of (Node (Elmt)))
9347 ("interface actual must include progenitor&",
9348 Actual, Node (Elmt));
9353 end Validate_Derived_Interface_Type_Instance;
9355 ------------------------------------
9356 -- Validate_Derived_Type_Instance --
9357 ------------------------------------
9359 procedure Validate_Derived_Type_Instance is
9360 Actual_Discr : Entity_Id;
9361 Ancestor_Discr : Entity_Id;
9364 -- If the parent type in the generic declaration is itself a previous
9365 -- formal type, then it is local to the generic and absent from the
9366 -- analyzed generic definition. In that case the ancestor is the
9367 -- instance of the formal (which must have been instantiated
9368 -- previously), unless the ancestor is itself a formal derived type.
9369 -- In this latter case (which is the subject of Corrigendum 8652/0038
9370 -- (AI-202) the ancestor of the formals is the ancestor of its
9371 -- parent. Otherwise, the analyzed generic carries the parent type.
9372 -- If the parent type is defined in a previous formal package, then
9373 -- the scope of that formal package is that of the generic type
9374 -- itself, and it has already been mapped into the corresponding type
9375 -- in the actual package.
9377 -- Common case: parent type defined outside of the generic
9379 if Is_Entity_Name (Subtype_Mark (Def))
9380 and then Present (Entity (Subtype_Mark (Def)))
9382 Ancestor := Get_Instance_Of (Entity (Subtype_Mark (Def)));
9384 -- Check whether parent is defined in a previous formal package
9387 Scope (Scope (Base_Type (Etype (A_Gen_T)))) = Scope (A_Gen_T)
9390 Get_Instance_Of (Base_Type (Etype (A_Gen_T)));
9392 -- The type may be a local derivation, or a type extension of a
9393 -- previous formal, or of a formal of a parent package.
9395 elsif Is_Derived_Type (Get_Instance_Of (A_Gen_T))
9397 Ekind (Get_Instance_Of (A_Gen_T)) = E_Record_Type_With_Private
9399 -- Check whether the parent is another derived formal type in the
9400 -- same generic unit.
9402 if Etype (A_Gen_T) /= A_Gen_T
9403 and then Is_Generic_Type (Etype (A_Gen_T))
9404 and then Scope (Etype (A_Gen_T)) = Scope (A_Gen_T)
9405 and then Etype (Etype (A_Gen_T)) /= Etype (A_Gen_T)
9407 -- Locate ancestor of parent from the subtype declaration
9408 -- created for the actual.
9414 Decl := First (Actual_Decls);
9415 while Present (Decl) loop
9416 if Nkind (Decl) = N_Subtype_Declaration
9417 and then Chars (Defining_Identifier (Decl)) =
9418 Chars (Etype (A_Gen_T))
9420 Ancestor := Generic_Parent_Type (Decl);
9428 pragma Assert (Present (Ancestor));
9432 Get_Instance_Of (Base_Type (Get_Instance_Of (A_Gen_T)));
9436 Ancestor := Get_Instance_Of (Etype (Base_Type (A_Gen_T)));
9439 -- If the formal derived type has pragma Preelaborable_Initialization
9440 -- then the actual type must have preelaborable initialization.
9442 if Known_To_Have_Preelab_Init (A_Gen_T)
9443 and then not Has_Preelaborable_Initialization (Act_T)
9446 ("actual for & must have preelaborable initialization",
9450 -- Ada 2005 (AI-251)
9452 if Ada_Version >= Ada_05
9453 and then Is_Interface (Ancestor)
9455 if not Interface_Present_In_Ancestor (Act_T, Ancestor) then
9457 ("(Ada 2005) expected type implementing & in instantiation",
9461 elsif not Is_Ancestor (Base_Type (Ancestor), Act_T) then
9463 ("expect type derived from & in instantiation",
9464 Actual, First_Subtype (Ancestor));
9465 Abandon_Instantiation (Actual);
9468 -- Ada 2005 (AI-443): Synchronized formal derived type checks. Note
9469 -- that the formal type declaration has been rewritten as a private
9472 if Ada_Version >= Ada_05
9473 and then Nkind (Parent (A_Gen_T)) = N_Private_Extension_Declaration
9474 and then Synchronized_Present (Parent (A_Gen_T))
9476 -- The actual must be a synchronized tagged type
9478 if not Is_Tagged_Type (Act_T) then
9480 ("actual of synchronized type must be tagged", Actual);
9481 Abandon_Instantiation (Actual);
9483 elsif Nkind (Parent (Act_T)) = N_Full_Type_Declaration
9484 and then Nkind (Type_Definition (Parent (Act_T))) =
9485 N_Derived_Type_Definition
9486 and then not Synchronized_Present (Type_Definition
9490 ("actual of synchronized type must be synchronized", Actual);
9491 Abandon_Instantiation (Actual);
9495 -- Perform atomic/volatile checks (RM C.6(12))
9497 if Is_Atomic (Act_T) and then not Is_Atomic (Ancestor) then
9499 ("cannot have atomic actual type for non-atomic formal type",
9502 elsif Is_Volatile (Act_T)
9503 and then not Is_Volatile (Ancestor)
9504 and then Is_By_Reference_Type (Ancestor)
9507 ("cannot have volatile actual type for non-volatile formal type",
9511 -- It should not be necessary to check for unknown discriminants on
9512 -- Formal, but for some reason Has_Unknown_Discriminants is false for
9513 -- A_Gen_T, so Is_Indefinite_Subtype incorrectly returns False. This
9514 -- needs fixing. ???
9516 if not Is_Indefinite_Subtype (A_Gen_T)
9517 and then not Unknown_Discriminants_Present (Formal)
9518 and then Is_Indefinite_Subtype (Act_T)
9521 ("actual subtype must be constrained", Actual);
9522 Abandon_Instantiation (Actual);
9525 if not Unknown_Discriminants_Present (Formal) then
9526 if Is_Constrained (Ancestor) then
9527 if not Is_Constrained (Act_T) then
9529 ("actual subtype must be constrained", Actual);
9530 Abandon_Instantiation (Actual);
9533 -- Ancestor is unconstrained, Check if generic formal and actual
9534 -- agree on constrainedness. The check only applies to array types
9535 -- and discriminated types.
9537 elsif Is_Constrained (Act_T) then
9538 if Ekind (Ancestor) = E_Access_Type
9540 (not Is_Constrained (A_Gen_T)
9541 and then Is_Composite_Type (A_Gen_T))
9544 ("actual subtype must be unconstrained", Actual);
9545 Abandon_Instantiation (Actual);
9548 -- A class-wide type is only allowed if the formal has unknown
9551 elsif Is_Class_Wide_Type (Act_T)
9552 and then not Has_Unknown_Discriminants (Ancestor)
9555 ("actual for & cannot be a class-wide type", Actual, Gen_T);
9556 Abandon_Instantiation (Actual);
9558 -- Otherwise, the formal and actual shall have the same number
9559 -- of discriminants and each discriminant of the actual must
9560 -- correspond to a discriminant of the formal.
9562 elsif Has_Discriminants (Act_T)
9563 and then not Has_Unknown_Discriminants (Act_T)
9564 and then Has_Discriminants (Ancestor)
9566 Actual_Discr := First_Discriminant (Act_T);
9567 Ancestor_Discr := First_Discriminant (Ancestor);
9568 while Present (Actual_Discr)
9569 and then Present (Ancestor_Discr)
9571 if Base_Type (Act_T) /= Base_Type (Ancestor) and then
9572 No (Corresponding_Discriminant (Actual_Discr))
9575 ("discriminant & does not correspond " &
9576 "to ancestor discriminant", Actual, Actual_Discr);
9577 Abandon_Instantiation (Actual);
9580 Next_Discriminant (Actual_Discr);
9581 Next_Discriminant (Ancestor_Discr);
9584 if Present (Actual_Discr) or else Present (Ancestor_Discr) then
9586 ("actual for & must have same number of discriminants",
9588 Abandon_Instantiation (Actual);
9591 -- This case should be caught by the earlier check for
9592 -- constrainedness, but the check here is added for completeness.
9594 elsif Has_Discriminants (Act_T)
9595 and then not Has_Unknown_Discriminants (Act_T)
9598 ("actual for & must not have discriminants", Actual, Gen_T);
9599 Abandon_Instantiation (Actual);
9601 elsif Has_Discriminants (Ancestor) then
9603 ("actual for & must have known discriminants", Actual, Gen_T);
9604 Abandon_Instantiation (Actual);
9607 if not Subtypes_Statically_Compatible (Act_T, Ancestor) then
9609 ("constraint on actual is incompatible with formal", Actual);
9610 Abandon_Instantiation (Actual);
9614 -- If the formal and actual types are abstract, check that there
9615 -- are no abstract primitives of the actual type that correspond to
9616 -- nonabstract primitives of the formal type (second sentence of
9619 if Is_Abstract_Type (A_Gen_T) and then Is_Abstract_Type (Act_T) then
9620 Check_Abstract_Primitives : declare
9621 Gen_Prims : constant Elist_Id :=
9622 Primitive_Operations (A_Gen_T);
9624 Gen_Subp : Entity_Id;
9625 Anc_Subp : Entity_Id;
9626 Anc_Formal : Entity_Id;
9627 Anc_F_Type : Entity_Id;
9629 Act_Prims : constant Elist_Id := Primitive_Operations (Act_T);
9631 Act_Subp : Entity_Id;
9632 Act_Formal : Entity_Id;
9633 Act_F_Type : Entity_Id;
9635 Subprograms_Correspond : Boolean;
9637 function Is_Tagged_Ancestor (T1, T2 : Entity_Id) return Boolean;
9638 -- Returns true if T2 is derived directly or indirectly from
9639 -- T1, including derivations from interfaces. T1 and T2 are
9640 -- required to be specific tagged base types.
9642 ------------------------
9643 -- Is_Tagged_Ancestor --
9644 ------------------------
9646 function Is_Tagged_Ancestor (T1, T2 : Entity_Id) return Boolean
9648 Intfc_Elmt : Elmt_Id;
9651 -- The predicate is satisfied if the types are the same
9656 -- If we've reached the top of the derivation chain then
9657 -- we know that T1 is not an ancestor of T2.
9659 elsif Etype (T2) = T2 then
9662 -- Proceed to check T2's immediate parent
9664 elsif Is_Ancestor (T1, Base_Type (Etype (T2))) then
9667 -- Finally, check to see if T1 is an ancestor of any of T2's
9671 Intfc_Elmt := First_Elmt (Interfaces (T2));
9672 while Present (Intfc_Elmt) loop
9673 if Is_Ancestor (T1, Node (Intfc_Elmt)) then
9677 Next_Elmt (Intfc_Elmt);
9682 end Is_Tagged_Ancestor;
9684 -- Start of processing for Check_Abstract_Primitives
9687 -- Loop over all of the formal derived type's primitives
9689 Gen_Elmt := First_Elmt (Gen_Prims);
9690 while Present (Gen_Elmt) loop
9691 Gen_Subp := Node (Gen_Elmt);
9693 -- If the primitive of the formal is not abstract, then
9694 -- determine whether there is a corresponding primitive of
9695 -- the actual type that's abstract.
9697 if not Is_Abstract_Subprogram (Gen_Subp) then
9698 Act_Elmt := First_Elmt (Act_Prims);
9699 while Present (Act_Elmt) loop
9700 Act_Subp := Node (Act_Elmt);
9702 -- If we find an abstract primitive of the actual,
9703 -- then we need to test whether it corresponds to the
9704 -- subprogram from which the generic formal primitive
9707 if Is_Abstract_Subprogram (Act_Subp) then
9708 Anc_Subp := Alias (Gen_Subp);
9710 -- Test whether we have a corresponding primitive
9711 -- by comparing names, kinds, formal types, and
9714 if Chars (Anc_Subp) = Chars (Act_Subp)
9715 and then Ekind (Anc_Subp) = Ekind (Act_Subp)
9717 Anc_Formal := First_Formal (Anc_Subp);
9718 Act_Formal := First_Formal (Act_Subp);
9719 while Present (Anc_Formal)
9720 and then Present (Act_Formal)
9722 Anc_F_Type := Etype (Anc_Formal);
9723 Act_F_Type := Etype (Act_Formal);
9725 if Ekind (Anc_F_Type)
9726 = E_Anonymous_Access_Type
9728 Anc_F_Type := Designated_Type (Anc_F_Type);
9730 if Ekind (Act_F_Type)
9731 = E_Anonymous_Access_Type
9734 Designated_Type (Act_F_Type);
9740 Ekind (Act_F_Type) = E_Anonymous_Access_Type
9745 Anc_F_Type := Base_Type (Anc_F_Type);
9746 Act_F_Type := Base_Type (Act_F_Type);
9748 -- If the formal is controlling, then the
9749 -- the type of the actual primitive's formal
9750 -- must be derived directly or indirectly
9751 -- from the type of the ancestor primitive's
9754 if Is_Controlling_Formal (Anc_Formal) then
9755 if not Is_Tagged_Ancestor
9756 (Anc_F_Type, Act_F_Type)
9761 -- Otherwise the types of the formals must
9764 elsif Anc_F_Type /= Act_F_Type then
9768 Next_Entity (Anc_Formal);
9769 Next_Entity (Act_Formal);
9772 -- If we traversed through all of the formals
9773 -- then so far the subprograms correspond, so
9774 -- now check that any result types correspond.
9777 and then No (Act_Formal)
9779 Subprograms_Correspond := True;
9781 if Ekind (Act_Subp) = E_Function then
9782 Anc_F_Type := Etype (Anc_Subp);
9783 Act_F_Type := Etype (Act_Subp);
9785 if Ekind (Anc_F_Type)
9786 = E_Anonymous_Access_Type
9789 Designated_Type (Anc_F_Type);
9791 if Ekind (Act_F_Type)
9792 = E_Anonymous_Access_Type
9795 Designated_Type (Act_F_Type);
9797 Subprograms_Correspond := False;
9802 = E_Anonymous_Access_Type
9804 Subprograms_Correspond := False;
9807 Anc_F_Type := Base_Type (Anc_F_Type);
9808 Act_F_Type := Base_Type (Act_F_Type);
9810 -- Now either the result types must be
9811 -- the same or, if the result type is
9812 -- controlling, the result type of the
9813 -- actual primitive must descend from the
9814 -- result type of the ancestor primitive.
9816 if Subprograms_Correspond
9817 and then Anc_F_Type /= Act_F_Type
9819 Has_Controlling_Result (Anc_Subp)
9821 not Is_Tagged_Ancestor
9822 (Anc_F_Type, Act_F_Type)
9824 Subprograms_Correspond := False;
9828 -- Found a matching subprogram belonging to
9829 -- formal ancestor type, so actual subprogram
9830 -- corresponds and this violates 3.9.3(9).
9832 if Subprograms_Correspond then
9834 ("abstract subprogram & overrides " &
9835 "nonabstract subprogram of ancestor",
9843 Next_Elmt (Act_Elmt);
9847 Next_Elmt (Gen_Elmt);
9849 end Check_Abstract_Primitives;
9852 -- Verify that limitedness matches. If parent is a limited
9853 -- interface then the generic formal is not unless declared
9854 -- explicitly so. If not declared limited, the actual cannot be
9855 -- limited (see AI05-0087).
9856 -- Disable check for now, limited interfaces implemented by
9857 -- protected types are common, Need to update tests ???
9859 if Is_Limited_Type (Act_T)
9860 and then not Is_Limited_Type (A_Gen_T)
9864 ("actual for non-limited & cannot be a limited type", Actual,
9866 Explain_Limited_Type (Act_T, Actual);
9867 Abandon_Instantiation (Actual);
9869 end Validate_Derived_Type_Instance;
9871 --------------------------------------
9872 -- Validate_Interface_Type_Instance --
9873 --------------------------------------
9875 procedure Validate_Interface_Type_Instance is
9877 if not Is_Interface (Act_T) then
9879 ("actual for formal interface type must be an interface",
9882 elsif Is_Limited_Type (Act_T) /= Is_Limited_Type (A_Gen_T)
9884 Is_Task_Interface (A_Gen_T) /= Is_Task_Interface (Act_T)
9886 Is_Protected_Interface (A_Gen_T) /=
9887 Is_Protected_Interface (Act_T)
9889 Is_Synchronized_Interface (A_Gen_T) /=
9890 Is_Synchronized_Interface (Act_T)
9893 ("actual for interface& does not match (RM 12.5.5(4))",
9896 end Validate_Interface_Type_Instance;
9898 ------------------------------------
9899 -- Validate_Private_Type_Instance --
9900 ------------------------------------
9902 procedure Validate_Private_Type_Instance is
9903 Formal_Discr : Entity_Id;
9904 Actual_Discr : Entity_Id;
9905 Formal_Subt : Entity_Id;
9908 if Is_Limited_Type (Act_T)
9909 and then not Is_Limited_Type (A_Gen_T)
9912 ("actual for non-limited & cannot be a limited type", Actual,
9914 Explain_Limited_Type (Act_T, Actual);
9915 Abandon_Instantiation (Actual);
9917 elsif Known_To_Have_Preelab_Init (A_Gen_T)
9918 and then not Has_Preelaborable_Initialization (Act_T)
9921 ("actual for & must have preelaborable initialization", Actual,
9924 elsif Is_Indefinite_Subtype (Act_T)
9925 and then not Is_Indefinite_Subtype (A_Gen_T)
9926 and then Ada_Version >= Ada_95
9929 ("actual for & must be a definite subtype", Actual, Gen_T);
9931 elsif not Is_Tagged_Type (Act_T)
9932 and then Is_Tagged_Type (A_Gen_T)
9935 ("actual for & must be a tagged type", Actual, Gen_T);
9937 elsif Has_Discriminants (A_Gen_T) then
9938 if not Has_Discriminants (Act_T) then
9940 ("actual for & must have discriminants", Actual, Gen_T);
9941 Abandon_Instantiation (Actual);
9943 elsif Is_Constrained (Act_T) then
9945 ("actual for & must be unconstrained", Actual, Gen_T);
9946 Abandon_Instantiation (Actual);
9949 Formal_Discr := First_Discriminant (A_Gen_T);
9950 Actual_Discr := First_Discriminant (Act_T);
9951 while Formal_Discr /= Empty loop
9952 if Actual_Discr = Empty then
9954 ("discriminants on actual do not match formal",
9956 Abandon_Instantiation (Actual);
9959 Formal_Subt := Get_Instance_Of (Etype (Formal_Discr));
9961 -- Access discriminants match if designated types do
9963 if Ekind (Base_Type (Formal_Subt)) = E_Anonymous_Access_Type
9964 and then (Ekind (Base_Type (Etype (Actual_Discr)))) =
9965 E_Anonymous_Access_Type
9968 (Designated_Type (Base_Type (Formal_Subt))) =
9969 Designated_Type (Base_Type (Etype (Actual_Discr)))
9973 elsif Base_Type (Formal_Subt) /=
9974 Base_Type (Etype (Actual_Discr))
9977 ("types of actual discriminants must match formal",
9979 Abandon_Instantiation (Actual);
9981 elsif not Subtypes_Statically_Match
9982 (Formal_Subt, Etype (Actual_Discr))
9983 and then Ada_Version >= Ada_95
9986 ("subtypes of actual discriminants must match formal",
9988 Abandon_Instantiation (Actual);
9991 Next_Discriminant (Formal_Discr);
9992 Next_Discriminant (Actual_Discr);
9995 if Actual_Discr /= Empty then
9997 ("discriminants on actual do not match formal",
9999 Abandon_Instantiation (Actual);
10006 end Validate_Private_Type_Instance;
10008 -- Start of processing for Instantiate_Type
10011 if Get_Instance_Of (A_Gen_T) /= A_Gen_T then
10012 Error_Msg_N ("duplicate instantiation of generic type", Actual);
10013 return New_List (Error);
10015 elsif not Is_Entity_Name (Actual)
10016 or else not Is_Type (Entity (Actual))
10019 ("expect valid subtype mark to instantiate &", Actual, Gen_T);
10020 Abandon_Instantiation (Actual);
10023 Act_T := Entity (Actual);
10025 -- Ada 2005 (AI-216): An Unchecked_Union subtype shall only be passed
10026 -- as a generic actual parameter if the corresponding formal type
10027 -- does not have a known_discriminant_part, or is a formal derived
10028 -- type that is an Unchecked_Union type.
10030 if Is_Unchecked_Union (Base_Type (Act_T)) then
10031 if not Has_Discriminants (A_Gen_T)
10033 (Is_Derived_Type (A_Gen_T)
10035 Is_Unchecked_Union (A_Gen_T))
10039 Error_Msg_N ("Unchecked_Union cannot be the actual for a" &
10040 " discriminated formal type", Act_T);
10045 -- Deal with fixed/floating restrictions
10047 if Is_Floating_Point_Type (Act_T) then
10048 Check_Restriction (No_Floating_Point, Actual);
10049 elsif Is_Fixed_Point_Type (Act_T) then
10050 Check_Restriction (No_Fixed_Point, Actual);
10053 -- Deal with error of using incomplete type as generic actual.
10054 -- This includes limited views of a type, even if the non-limited
10055 -- view may be available.
10057 if Ekind (Act_T) = E_Incomplete_Type
10058 or else (Is_Class_Wide_Type (Act_T)
10060 Ekind (Root_Type (Act_T)) = E_Incomplete_Type)
10062 if Is_Class_Wide_Type (Act_T)
10063 or else No (Full_View (Act_T))
10065 Error_Msg_N ("premature use of incomplete type", Actual);
10066 Abandon_Instantiation (Actual);
10068 Act_T := Full_View (Act_T);
10069 Set_Entity (Actual, Act_T);
10071 if Has_Private_Component (Act_T) then
10073 ("premature use of type with private component", Actual);
10077 -- Deal with error of premature use of private type as generic actual
10079 elsif Is_Private_Type (Act_T)
10080 and then Is_Private_Type (Base_Type (Act_T))
10081 and then not Is_Generic_Type (Act_T)
10082 and then not Is_Derived_Type (Act_T)
10083 and then No (Full_View (Root_Type (Act_T)))
10085 Error_Msg_N ("premature use of private type", Actual);
10087 elsif Has_Private_Component (Act_T) then
10089 ("premature use of type with private component", Actual);
10092 Set_Instance_Of (A_Gen_T, Act_T);
10094 -- If the type is generic, the class-wide type may also be used
10096 if Is_Tagged_Type (A_Gen_T)
10097 and then Is_Tagged_Type (Act_T)
10098 and then not Is_Class_Wide_Type (A_Gen_T)
10100 Set_Instance_Of (Class_Wide_Type (A_Gen_T),
10101 Class_Wide_Type (Act_T));
10104 if not Is_Abstract_Type (A_Gen_T)
10105 and then Is_Abstract_Type (Act_T)
10108 ("actual of non-abstract formal cannot be abstract", Actual);
10111 -- A generic scalar type is a first subtype for which we generate
10112 -- an anonymous base type. Indicate that the instance of this base
10113 -- is the base type of the actual.
10115 if Is_Scalar_Type (A_Gen_T) then
10116 Set_Instance_Of (Etype (A_Gen_T), Etype (Act_T));
10120 if Error_Posted (Act_T) then
10123 case Nkind (Def) is
10124 when N_Formal_Private_Type_Definition =>
10125 Validate_Private_Type_Instance;
10127 when N_Formal_Derived_Type_Definition =>
10128 Validate_Derived_Type_Instance;
10130 when N_Formal_Discrete_Type_Definition =>
10131 if not Is_Discrete_Type (Act_T) then
10133 ("expect discrete type in instantiation of&",
10135 Abandon_Instantiation (Actual);
10138 when N_Formal_Signed_Integer_Type_Definition =>
10139 if not Is_Signed_Integer_Type (Act_T) then
10141 ("expect signed integer type in instantiation of&",
10143 Abandon_Instantiation (Actual);
10146 when N_Formal_Modular_Type_Definition =>
10147 if not Is_Modular_Integer_Type (Act_T) then
10149 ("expect modular type in instantiation of &",
10151 Abandon_Instantiation (Actual);
10154 when N_Formal_Floating_Point_Definition =>
10155 if not Is_Floating_Point_Type (Act_T) then
10157 ("expect float type in instantiation of &", Actual, Gen_T);
10158 Abandon_Instantiation (Actual);
10161 when N_Formal_Ordinary_Fixed_Point_Definition =>
10162 if not Is_Ordinary_Fixed_Point_Type (Act_T) then
10164 ("expect ordinary fixed point type in instantiation of &",
10166 Abandon_Instantiation (Actual);
10169 when N_Formal_Decimal_Fixed_Point_Definition =>
10170 if not Is_Decimal_Fixed_Point_Type (Act_T) then
10172 ("expect decimal type in instantiation of &",
10174 Abandon_Instantiation (Actual);
10177 when N_Array_Type_Definition =>
10178 Validate_Array_Type_Instance;
10180 when N_Access_To_Object_Definition =>
10181 Validate_Access_Type_Instance;
10183 when N_Access_Function_Definition |
10184 N_Access_Procedure_Definition =>
10185 Validate_Access_Subprogram_Instance;
10187 when N_Record_Definition =>
10188 Validate_Interface_Type_Instance;
10190 when N_Derived_Type_Definition =>
10191 Validate_Derived_Interface_Type_Instance;
10194 raise Program_Error;
10199 Subt := New_Copy (Gen_T);
10201 -- Use adjusted sloc of subtype name as the location for other nodes in
10202 -- the subtype declaration.
10204 Loc := Sloc (Subt);
10207 Make_Subtype_Declaration (Loc,
10208 Defining_Identifier => Subt,
10209 Subtype_Indication => New_Reference_To (Act_T, Loc));
10211 if Is_Private_Type (Act_T) then
10212 Set_Has_Private_View (Subtype_Indication (Decl_Node));
10214 elsif Is_Access_Type (Act_T)
10215 and then Is_Private_Type (Designated_Type (Act_T))
10217 Set_Has_Private_View (Subtype_Indication (Decl_Node));
10220 Decl_Nodes := New_List (Decl_Node);
10222 -- Flag actual derived types so their elaboration produces the
10223 -- appropriate renamings for the primitive operations of the ancestor.
10224 -- Flag actual for formal private types as well, to determine whether
10225 -- operations in the private part may override inherited operations.
10226 -- If the formal has an interface list, the ancestor is not the
10227 -- parent, but the analyzed formal that includes the interface
10228 -- operations of all its progenitors.
10230 if Nkind (Def) = N_Formal_Derived_Type_Definition then
10231 if Present (Interface_List (Def)) then
10232 Set_Generic_Parent_Type (Decl_Node, A_Gen_T);
10234 Set_Generic_Parent_Type (Decl_Node, Ancestor);
10237 elsif Nkind (Def) = N_Formal_Private_Type_Definition then
10238 Set_Generic_Parent_Type (Decl_Node, Ancestor);
10241 -- If the actual is a synchronized type that implements an interface,
10242 -- the primitive operations are attached to the corresponding record,
10243 -- and we have to treat it as an additional generic actual, so that its
10244 -- primitive operations become visible in the instance. The task or
10245 -- protected type itself does not carry primitive operations.
10247 if Is_Concurrent_Type (Act_T)
10248 and then Is_Tagged_Type (Act_T)
10249 and then Present (Corresponding_Record_Type (Act_T))
10250 and then Present (Ancestor)
10251 and then Is_Interface (Ancestor)
10254 Corr_Rec : constant Entity_Id :=
10255 Corresponding_Record_Type (Act_T);
10256 New_Corr : Entity_Id;
10257 Corr_Decl : Node_Id;
10260 New_Corr := Make_Defining_Identifier (Loc,
10261 Chars => New_Internal_Name ('S'));
10263 Make_Subtype_Declaration (Loc,
10264 Defining_Identifier => New_Corr,
10265 Subtype_Indication =>
10266 New_Reference_To (Corr_Rec, Loc));
10267 Append_To (Decl_Nodes, Corr_Decl);
10269 if Ekind (Act_T) = E_Task_Type then
10270 Set_Ekind (Subt, E_Task_Subtype);
10272 Set_Ekind (Subt, E_Protected_Subtype);
10275 Set_Corresponding_Record_Type (Subt, Corr_Rec);
10276 Set_Generic_Parent_Type (Corr_Decl, Ancestor);
10277 Set_Generic_Parent_Type (Decl_Node, Empty);
10282 end Instantiate_Type;
10284 -----------------------
10285 -- Is_Generic_Formal --
10286 -----------------------
10288 function Is_Generic_Formal (E : Entity_Id) return Boolean is
10294 Kind := Nkind (Parent (E));
10296 Nkind_In (Kind, N_Formal_Object_Declaration,
10297 N_Formal_Package_Declaration,
10298 N_Formal_Type_Declaration)
10300 (Is_Formal_Subprogram (E)
10302 Nkind (Parent (Parent (E))) in
10303 N_Formal_Subprogram_Declaration);
10305 end Is_Generic_Formal;
10307 ---------------------
10308 -- Is_In_Main_Unit --
10309 ---------------------
10311 function Is_In_Main_Unit (N : Node_Id) return Boolean is
10312 Unum : constant Unit_Number_Type := Get_Source_Unit (N);
10313 Current_Unit : Node_Id;
10316 if Unum = Main_Unit then
10319 -- If the current unit is a subunit then it is either the main unit or
10320 -- is being compiled as part of the main unit.
10322 elsif Nkind (N) = N_Compilation_Unit then
10323 return Nkind (Unit (N)) = N_Subunit;
10326 Current_Unit := Parent (N);
10327 while Present (Current_Unit)
10328 and then Nkind (Current_Unit) /= N_Compilation_Unit
10330 Current_Unit := Parent (Current_Unit);
10333 -- The instantiation node is in the main unit, or else the current node
10334 -- (perhaps as the result of nested instantiations) is in the main unit,
10335 -- or in the declaration of the main unit, which in this last case must
10338 return Unum = Main_Unit
10339 or else Current_Unit = Cunit (Main_Unit)
10340 or else Current_Unit = Library_Unit (Cunit (Main_Unit))
10341 or else (Present (Library_Unit (Current_Unit))
10342 and then Is_In_Main_Unit (Library_Unit (Current_Unit)));
10343 end Is_In_Main_Unit;
10345 ----------------------------
10346 -- Load_Parent_Of_Generic --
10347 ----------------------------
10349 procedure Load_Parent_Of_Generic
10352 Body_Optional : Boolean := False)
10354 Comp_Unit : constant Node_Id := Cunit (Get_Source_Unit (Spec));
10355 Save_Style_Check : constant Boolean := Style_Check;
10356 True_Parent : Node_Id;
10357 Inst_Node : Node_Id;
10359 Previous_Instances : constant Elist_Id := New_Elmt_List;
10361 procedure Collect_Previous_Instances (Decls : List_Id);
10362 -- Collect all instantiations in the given list of declarations, that
10363 -- precede the generic that we need to load. If the bodies of these
10364 -- instantiations are available, we must analyze them, to ensure that
10365 -- the public symbols generated are the same when the unit is compiled
10366 -- to generate code, and when it is compiled in the context of a unit
10367 -- that needs a particular nested instance. This process is applied
10368 -- to both package and subprogram instances.
10370 --------------------------------
10371 -- Collect_Previous_Instances --
10372 --------------------------------
10374 procedure Collect_Previous_Instances (Decls : List_Id) is
10378 Decl := First (Decls);
10379 while Present (Decl) loop
10380 if Sloc (Decl) >= Sloc (Inst_Node) then
10383 -- If Decl is an instantiation, then record it as requiring
10384 -- instantiation of the corresponding body, except if it is an
10385 -- abbreviated instantiation generated internally for conformance
10386 -- checking purposes only for the case of a formal package
10387 -- declared without a box (see Instantiate_Formal_Package). Such
10388 -- an instantiation does not generate any code (the actual code
10389 -- comes from actual) and thus does not need to be analyzed here.
10391 elsif Nkind (Decl) = N_Package_Instantiation
10392 and then not Is_Internal (Defining_Entity (Decl))
10394 Append_Elmt (Decl, Previous_Instances);
10396 -- For a subprogram instantiation, omit instantiations of
10397 -- intrinsic operations (Unchecked_Conversions, etc.) that
10400 elsif Nkind_In (Decl, N_Function_Instantiation,
10401 N_Procedure_Instantiation)
10402 and then not Is_Intrinsic_Subprogram (Entity (Name (Decl)))
10404 Append_Elmt (Decl, Previous_Instances);
10406 elsif Nkind (Decl) = N_Package_Declaration then
10407 Collect_Previous_Instances
10408 (Visible_Declarations (Specification (Decl)));
10409 Collect_Previous_Instances
10410 (Private_Declarations (Specification (Decl)));
10412 elsif Nkind (Decl) = N_Package_Body then
10413 Collect_Previous_Instances (Declarations (Decl));
10418 end Collect_Previous_Instances;
10420 -- Start of processing for Load_Parent_Of_Generic
10423 if not In_Same_Source_Unit (N, Spec)
10424 or else Nkind (Unit (Comp_Unit)) = N_Package_Declaration
10425 or else (Nkind (Unit (Comp_Unit)) = N_Package_Body
10426 and then not Is_In_Main_Unit (Spec))
10428 -- Find body of parent of spec, and analyze it. A special case arises
10429 -- when the parent is an instantiation, that is to say when we are
10430 -- currently instantiating a nested generic. In that case, there is
10431 -- no separate file for the body of the enclosing instance. Instead,
10432 -- the enclosing body must be instantiated as if it were a pending
10433 -- instantiation, in order to produce the body for the nested generic
10434 -- we require now. Note that in that case the generic may be defined
10435 -- in a package body, the instance defined in the same package body,
10436 -- and the original enclosing body may not be in the main unit.
10438 Inst_Node := Empty;
10440 True_Parent := Parent (Spec);
10441 while Present (True_Parent)
10442 and then Nkind (True_Parent) /= N_Compilation_Unit
10444 if Nkind (True_Parent) = N_Package_Declaration
10446 Nkind (Original_Node (True_Parent)) = N_Package_Instantiation
10448 -- Parent is a compilation unit that is an instantiation.
10449 -- Instantiation node has been replaced with package decl.
10451 Inst_Node := Original_Node (True_Parent);
10454 elsif Nkind (True_Parent) = N_Package_Declaration
10455 and then Present (Generic_Parent (Specification (True_Parent)))
10456 and then Nkind (Parent (True_Parent)) /= N_Compilation_Unit
10458 -- Parent is an instantiation within another specification.
10459 -- Declaration for instance has been inserted before original
10460 -- instantiation node. A direct link would be preferable?
10462 Inst_Node := Next (True_Parent);
10463 while Present (Inst_Node)
10464 and then Nkind (Inst_Node) /= N_Package_Instantiation
10469 -- If the instance appears within a generic, and the generic
10470 -- unit is defined within a formal package of the enclosing
10471 -- generic, there is no generic body available, and none
10472 -- needed. A more precise test should be used ???
10474 if No (Inst_Node) then
10481 True_Parent := Parent (True_Parent);
10485 -- Case where we are currently instantiating a nested generic
10487 if Present (Inst_Node) then
10488 if Nkind (Parent (True_Parent)) = N_Compilation_Unit then
10490 -- Instantiation node and declaration of instantiated package
10491 -- were exchanged when only the declaration was needed.
10492 -- Restore instantiation node before proceeding with body.
10494 Set_Unit (Parent (True_Parent), Inst_Node);
10497 -- Now complete instantiation of enclosing body, if it appears
10498 -- in some other unit. If it appears in the current unit, the
10499 -- body will have been instantiated already.
10501 if No (Corresponding_Body (Instance_Spec (Inst_Node))) then
10503 -- We need to determine the expander mode to instantiate the
10504 -- enclosing body. Because the generic body we need may use
10505 -- global entities declared in the enclosing package (including
10506 -- aggregates) it is in general necessary to compile this body
10507 -- with expansion enabled. The exception is if we are within a
10508 -- generic package, in which case the usual generic rule
10512 Exp_Status : Boolean := True;
10516 -- Loop through scopes looking for generic package
10518 Scop := Scope (Defining_Entity (Instance_Spec (Inst_Node)));
10519 while Present (Scop)
10520 and then Scop /= Standard_Standard
10522 if Ekind (Scop) = E_Generic_Package then
10523 Exp_Status := False;
10527 Scop := Scope (Scop);
10530 -- Collect previous instantiations in the unit that
10531 -- contains the desired generic.
10533 if Nkind (Parent (True_Parent)) /= N_Compilation_Unit
10534 and then not Body_Optional
10538 Info : Pending_Body_Info;
10542 Par := Parent (Inst_Node);
10543 while Present (Par) loop
10544 exit when Nkind (Parent (Par)) = N_Compilation_Unit;
10545 Par := Parent (Par);
10548 pragma Assert (Present (Par));
10550 if Nkind (Par) = N_Package_Body then
10551 Collect_Previous_Instances (Declarations (Par));
10553 elsif Nkind (Par) = N_Package_Declaration then
10554 Collect_Previous_Instances
10555 (Visible_Declarations (Specification (Par)));
10556 Collect_Previous_Instances
10557 (Private_Declarations (Specification (Par)));
10560 -- Enclosing unit is a subprogram body, In this
10561 -- case all instance bodies are processed in order
10562 -- and there is no need to collect them separately.
10567 Decl := First_Elmt (Previous_Instances);
10568 while Present (Decl) loop
10570 (Inst_Node => Node (Decl),
10572 Instance_Spec (Node (Decl)),
10573 Expander_Status => Exp_Status,
10574 Current_Sem_Unit =>
10575 Get_Code_Unit (Sloc (Node (Decl))),
10576 Scope_Suppress => Scope_Suppress,
10577 Local_Suppress_Stack_Top =>
10578 Local_Suppress_Stack_Top);
10580 -- Package instance
10583 Nkind (Node (Decl)) = N_Package_Instantiation
10585 Instantiate_Package_Body
10586 (Info, Body_Optional => True);
10588 -- Subprogram instance
10591 -- The instance_spec is the wrapper package,
10592 -- and the subprogram declaration is the last
10593 -- declaration in the wrapper.
10597 (Visible_Declarations
10598 (Specification (Info.Act_Decl)));
10600 Instantiate_Subprogram_Body
10601 (Info, Body_Optional => True);
10609 Instantiate_Package_Body
10611 ((Inst_Node => Inst_Node,
10612 Act_Decl => True_Parent,
10613 Expander_Status => Exp_Status,
10614 Current_Sem_Unit =>
10615 Get_Code_Unit (Sloc (Inst_Node)),
10616 Scope_Suppress => Scope_Suppress,
10617 Local_Suppress_Stack_Top =>
10618 Local_Suppress_Stack_Top)),
10619 Body_Optional => Body_Optional);
10623 -- Case where we are not instantiating a nested generic
10626 Opt.Style_Check := False;
10627 Expander_Mode_Save_And_Set (True);
10628 Load_Needed_Body (Comp_Unit, OK);
10629 Opt.Style_Check := Save_Style_Check;
10630 Expander_Mode_Restore;
10633 and then Unit_Requires_Body (Defining_Entity (Spec))
10634 and then not Body_Optional
10637 Bname : constant Unit_Name_Type :=
10638 Get_Body_Name (Get_Unit_Name (Unit (Comp_Unit)));
10641 Error_Msg_Unit_1 := Bname;
10642 Error_Msg_N ("this instantiation requires$!", N);
10643 Error_Msg_File_1 := Get_File_Name (Bname, Subunit => False);
10644 Error_Msg_N ("\but file{ was not found!", N);
10645 raise Unrecoverable_Error;
10651 -- If loading parent of the generic caused an instantiation circularity,
10652 -- we abandon compilation at this point, because otherwise in some cases
10653 -- we get into trouble with infinite recursions after this point.
10655 if Circularity_Detected then
10656 raise Unrecoverable_Error;
10658 end Load_Parent_Of_Generic;
10660 -----------------------
10661 -- Move_Freeze_Nodes --
10662 -----------------------
10664 procedure Move_Freeze_Nodes
10665 (Out_Of : Entity_Id;
10670 Next_Decl : Node_Id;
10671 Next_Node : Node_Id := After;
10674 function Is_Outer_Type (T : Entity_Id) return Boolean;
10675 -- Check whether entity is declared in a scope external to that
10676 -- of the generic unit.
10678 -------------------
10679 -- Is_Outer_Type --
10680 -------------------
10682 function Is_Outer_Type (T : Entity_Id) return Boolean is
10683 Scop : Entity_Id := Scope (T);
10686 if Scope_Depth (Scop) < Scope_Depth (Out_Of) then
10690 while Scop /= Standard_Standard loop
10691 if Scop = Out_Of then
10694 Scop := Scope (Scop);
10702 -- Start of processing for Move_Freeze_Nodes
10709 -- First remove the freeze nodes that may appear before all other
10713 while Present (Decl)
10714 and then Nkind (Decl) = N_Freeze_Entity
10715 and then Is_Outer_Type (Entity (Decl))
10717 Decl := Remove_Head (L);
10718 Insert_After (Next_Node, Decl);
10719 Set_Analyzed (Decl, False);
10724 -- Next scan the list of declarations and remove each freeze node that
10725 -- appears ahead of the current node.
10727 while Present (Decl) loop
10728 while Present (Next (Decl))
10729 and then Nkind (Next (Decl)) = N_Freeze_Entity
10730 and then Is_Outer_Type (Entity (Next (Decl)))
10732 Next_Decl := Remove_Next (Decl);
10733 Insert_After (Next_Node, Next_Decl);
10734 Set_Analyzed (Next_Decl, False);
10735 Next_Node := Next_Decl;
10738 -- If the declaration is a nested package or concurrent type, then
10739 -- recurse. Nested generic packages will have been processed from the
10742 if Nkind (Decl) = N_Package_Declaration then
10743 Spec := Specification (Decl);
10745 elsif Nkind (Decl) = N_Task_Type_Declaration then
10746 Spec := Task_Definition (Decl);
10748 elsif Nkind (Decl) = N_Protected_Type_Declaration then
10749 Spec := Protected_Definition (Decl);
10755 if Present (Spec) then
10756 Move_Freeze_Nodes (Out_Of, Next_Node,
10757 Visible_Declarations (Spec));
10758 Move_Freeze_Nodes (Out_Of, Next_Node,
10759 Private_Declarations (Spec));
10764 end Move_Freeze_Nodes;
10770 function Next_Assoc (E : Assoc_Ptr) return Assoc_Ptr is
10772 return Generic_Renamings.Table (E).Next_In_HTable;
10775 ------------------------
10776 -- Preanalyze_Actuals --
10777 ------------------------
10779 procedure Preanalyze_Actuals (N : Node_Id) is
10782 Errs : constant Int := Serious_Errors_Detected;
10785 Assoc := First (Generic_Associations (N));
10786 while Present (Assoc) loop
10787 if Nkind (Assoc) /= N_Others_Choice then
10788 Act := Explicit_Generic_Actual_Parameter (Assoc);
10790 -- Within a nested instantiation, a defaulted actual is an empty
10791 -- association, so nothing to analyze. If the subprogram actual
10792 -- is an attribute, analyze prefix only, because actual is not a
10793 -- complete attribute reference.
10795 -- If actual is an allocator, analyze expression only. The full
10796 -- analysis can generate code, and if instance is a compilation
10797 -- unit we have to wait until the package instance is installed
10798 -- to have a proper place to insert this code.
10800 -- String literals may be operators, but at this point we do not
10801 -- know whether the actual is a formal subprogram or a string.
10806 elsif Nkind (Act) = N_Attribute_Reference then
10807 Analyze (Prefix (Act));
10809 elsif Nkind (Act) = N_Explicit_Dereference then
10810 Analyze (Prefix (Act));
10812 elsif Nkind (Act) = N_Allocator then
10814 Expr : constant Node_Id := Expression (Act);
10817 if Nkind (Expr) = N_Subtype_Indication then
10818 Analyze (Subtype_Mark (Expr));
10820 -- Analyze separately each discriminant constraint,
10821 -- when given with a named association.
10827 Constr := First (Constraints (Constraint (Expr)));
10828 while Present (Constr) loop
10829 if Nkind (Constr) = N_Discriminant_Association then
10830 Analyze (Expression (Constr));
10844 elsif Nkind (Act) /= N_Operator_Symbol then
10848 if Errs /= Serious_Errors_Detected then
10850 -- Do a minimal analysis of the generic, to prevent spurious
10851 -- warnings complaining about the generic being unreferenced,
10852 -- before abandoning the instantiation.
10854 Analyze (Name (N));
10856 if Is_Entity_Name (Name (N))
10857 and then Etype (Name (N)) /= Any_Type
10859 Generate_Reference (Entity (Name (N)), Name (N));
10860 Set_Is_Instantiated (Entity (Name (N)));
10863 Abandon_Instantiation (Act);
10869 end Preanalyze_Actuals;
10871 -------------------
10872 -- Remove_Parent --
10873 -------------------
10875 procedure Remove_Parent (In_Body : Boolean := False) is
10876 S : Entity_Id := Current_Scope;
10877 -- S is the scope containing the instantiation just completed. The
10878 -- scope stack contains the parent instances of the instantiation,
10879 -- followed by the original S.
10886 -- After child instantiation is complete, remove from scope stack the
10887 -- extra copy of the current scope, and then remove parent instances.
10889 if not In_Body then
10892 while Current_Scope /= S loop
10893 P := Current_Scope;
10894 End_Package_Scope (Current_Scope);
10896 if In_Open_Scopes (P) then
10897 E := First_Entity (P);
10898 while Present (E) loop
10899 Set_Is_Immediately_Visible (E, True);
10903 if Is_Generic_Instance (Current_Scope)
10904 and then P /= Current_Scope
10906 -- We are within an instance of some sibling. Retain
10907 -- visibility of parent, for proper subsequent cleanup,
10908 -- and reinstall private declarations as well.
10910 Set_In_Private_Part (P);
10911 Install_Private_Declarations (P);
10914 -- If the ultimate parent is a top-level unit recorded in
10915 -- Instance_Parent_Unit, then reset its visibility to what
10916 -- it was before instantiation. (It's not clear what the
10917 -- purpose is of testing whether Scope (P) is In_Open_Scopes,
10918 -- but that test was present before the ultimate parent test
10921 elsif not In_Open_Scopes (Scope (P))
10922 or else (P = Instance_Parent_Unit
10923 and then not Parent_Unit_Visible)
10925 Set_Is_Immediately_Visible (P, False);
10927 -- If the current scope is itself an instantiation of a generic
10928 -- nested within P, and we are in the private part of body of this
10929 -- instantiation, restore the full views of P, that were removed
10930 -- in End_Package_Scope above. This obscure case can occur when a
10931 -- subunit of a generic contains an instance of a child unit of
10932 -- its generic parent unit.
10934 elsif S = Current_Scope
10935 and then Is_Generic_Instance (S)
10938 Par : constant Entity_Id :=
10940 (Specification (Unit_Declaration_Node (S)));
10943 and then P = Scope (Par)
10944 and then (In_Package_Body (S) or else In_Private_Part (S))
10946 Set_In_Private_Part (P);
10947 Install_Private_Declarations (P);
10953 -- Reset visibility of entities in the enclosing scope
10955 Set_Is_Hidden_Open_Scope (Current_Scope, False);
10957 Hidden := First_Elmt (Hidden_Entities);
10958 while Present (Hidden) loop
10959 Set_Is_Immediately_Visible (Node (Hidden), True);
10960 Next_Elmt (Hidden);
10964 -- Each body is analyzed separately, and there is no context
10965 -- that needs preserving from one body instance to the next,
10966 -- so remove all parent scopes that have been installed.
10968 while Present (S) loop
10969 End_Package_Scope (S);
10970 Set_Is_Immediately_Visible (S, False);
10971 S := Current_Scope;
10972 exit when S = Standard_Standard;
10981 procedure Restore_Env is
10982 Saved : Instance_Env renames Instance_Envs.Table (Instance_Envs.Last);
10985 if No (Current_Instantiated_Parent.Act_Id) then
10987 -- Restore environment after subprogram inlining
10989 Restore_Private_Views (Empty);
10992 Current_Instantiated_Parent := Saved.Instantiated_Parent;
10993 Exchanged_Views := Saved.Exchanged_Views;
10994 Hidden_Entities := Saved.Hidden_Entities;
10995 Current_Sem_Unit := Saved.Current_Sem_Unit;
10996 Parent_Unit_Visible := Saved.Parent_Unit_Visible;
10997 Instance_Parent_Unit := Saved.Instance_Parent_Unit;
10999 Restore_Opt_Config_Switches (Saved.Switches);
11001 Instance_Envs.Decrement_Last;
11004 ---------------------------
11005 -- Restore_Private_Views --
11006 ---------------------------
11008 procedure Restore_Private_Views
11009 (Pack_Id : Entity_Id;
11010 Is_Package : Boolean := True)
11015 Dep_Elmt : Elmt_Id;
11018 procedure Restore_Nested_Formal (Formal : Entity_Id);
11019 -- Hide the generic formals of formal packages declared with box
11020 -- which were reachable in the current instantiation.
11022 ---------------------------
11023 -- Restore_Nested_Formal --
11024 ---------------------------
11026 procedure Restore_Nested_Formal (Formal : Entity_Id) is
11030 if Present (Renamed_Object (Formal))
11031 and then Denotes_Formal_Package (Renamed_Object (Formal), True)
11035 elsif Present (Associated_Formal_Package (Formal)) then
11036 Ent := First_Entity (Formal);
11037 while Present (Ent) loop
11038 exit when Ekind (Ent) = E_Package
11039 and then Renamed_Entity (Ent) = Renamed_Entity (Formal);
11041 Set_Is_Hidden (Ent);
11042 Set_Is_Potentially_Use_Visible (Ent, False);
11044 -- If package, then recurse
11046 if Ekind (Ent) = E_Package then
11047 Restore_Nested_Formal (Ent);
11053 end Restore_Nested_Formal;
11055 -- Start of processing for Restore_Private_Views
11058 M := First_Elmt (Exchanged_Views);
11059 while Present (M) loop
11062 -- Subtypes of types whose views have been exchanged, and that
11063 -- are defined within the instance, were not on the list of
11064 -- Private_Dependents on entry to the instance, so they have to
11065 -- be exchanged explicitly now, in order to remain consistent with
11066 -- the view of the parent type.
11068 if Ekind (Typ) = E_Private_Type
11069 or else Ekind (Typ) = E_Limited_Private_Type
11070 or else Ekind (Typ) = E_Record_Type_With_Private
11072 Dep_Elmt := First_Elmt (Private_Dependents (Typ));
11073 while Present (Dep_Elmt) loop
11074 Dep_Typ := Node (Dep_Elmt);
11076 if Scope (Dep_Typ) = Pack_Id
11077 and then Present (Full_View (Dep_Typ))
11079 Replace_Elmt (Dep_Elmt, Full_View (Dep_Typ));
11080 Exchange_Declarations (Dep_Typ);
11083 Next_Elmt (Dep_Elmt);
11087 Exchange_Declarations (Node (M));
11091 if No (Pack_Id) then
11095 -- Make the generic formal parameters private, and make the formal
11096 -- types into subtypes of the actuals again.
11098 E := First_Entity (Pack_Id);
11099 while Present (E) loop
11100 Set_Is_Hidden (E, True);
11103 and then Nkind (Parent (E)) = N_Subtype_Declaration
11105 Set_Is_Generic_Actual_Type (E, False);
11107 -- An unusual case of aliasing: the actual may also be directly
11108 -- visible in the generic, and be private there, while it is fully
11109 -- visible in the context of the instance. The internal subtype
11110 -- is private in the instance, but has full visibility like its
11111 -- parent in the enclosing scope. This enforces the invariant that
11112 -- the privacy status of all private dependents of a type coincide
11113 -- with that of the parent type. This can only happen when a
11114 -- generic child unit is instantiated within sibling.
11116 if Is_Private_Type (E)
11117 and then not Is_Private_Type (Etype (E))
11119 Exchange_Declarations (E);
11122 elsif Ekind (E) = E_Package then
11124 -- The end of the renaming list is the renaming of the generic
11125 -- package itself. If the instance is a subprogram, all entities
11126 -- in the corresponding package are renamings. If this entity is
11127 -- a formal package, make its own formals private as well. The
11128 -- actual in this case is itself the renaming of an instantiation.
11129 -- If the entity is not a package renaming, it is the entity
11130 -- created to validate formal package actuals: ignore.
11132 -- If the actual is itself a formal package for the enclosing
11133 -- generic, or the actual for such a formal package, it remains
11134 -- visible on exit from the instance, and therefore nothing needs
11135 -- to be done either, except to keep it accessible.
11138 and then Renamed_Object (E) = Pack_Id
11142 elsif Nkind (Parent (E)) /= N_Package_Renaming_Declaration then
11145 elsif Denotes_Formal_Package (Renamed_Object (E), True) then
11146 Set_Is_Hidden (E, False);
11150 Act_P : constant Entity_Id := Renamed_Object (E);
11154 Id := First_Entity (Act_P);
11156 and then Id /= First_Private_Entity (Act_P)
11158 exit when Ekind (Id) = E_Package
11159 and then Renamed_Object (Id) = Act_P;
11161 Set_Is_Hidden (Id, True);
11162 Set_Is_Potentially_Use_Visible (Id, In_Use (Act_P));
11164 if Ekind (Id) = E_Package then
11165 Restore_Nested_Formal (Id);
11176 end Restore_Private_Views;
11183 (Gen_Unit : Entity_Id;
11184 Act_Unit : Entity_Id)
11188 Set_Instance_Env (Gen_Unit, Act_Unit);
11191 ----------------------------
11192 -- Save_Global_References --
11193 ----------------------------
11195 procedure Save_Global_References (N : Node_Id) is
11196 Gen_Scope : Entity_Id;
11200 function Is_Global (E : Entity_Id) return Boolean;
11201 -- Check whether entity is defined outside of generic unit. Examine the
11202 -- scope of an entity, and the scope of the scope, etc, until we find
11203 -- either Standard, in which case the entity is global, or the generic
11204 -- unit itself, which indicates that the entity is local. If the entity
11205 -- is the generic unit itself, as in the case of a recursive call, or
11206 -- the enclosing generic unit, if different from the current scope, then
11207 -- it is local as well, because it will be replaced at the point of
11208 -- instantiation. On the other hand, if it is a reference to a child
11209 -- unit of a common ancestor, which appears in an instantiation, it is
11210 -- global because it is used to denote a specific compilation unit at
11211 -- the time the instantiations will be analyzed.
11213 procedure Reset_Entity (N : Node_Id);
11214 -- Save semantic information on global entity, so that it is not
11215 -- resolved again at instantiation time.
11217 procedure Save_Entity_Descendants (N : Node_Id);
11218 -- Apply Save_Global_References to the two syntactic descendants of
11219 -- non-terminal nodes that carry an Associated_Node and are processed
11220 -- through Reset_Entity. Once the global entity (if any) has been
11221 -- captured together with its type, only two syntactic descendants need
11222 -- to be traversed to complete the processing of the tree rooted at N.
11223 -- This applies to Selected_Components, Expanded_Names, and to Operator
11224 -- nodes. N can also be a character literal, identifier, or operator
11225 -- symbol node, but the call has no effect in these cases.
11227 procedure Save_Global_Defaults (N1, N2 : Node_Id);
11228 -- Default actuals in nested instances must be handled specially
11229 -- because there is no link to them from the original tree. When an
11230 -- actual subprogram is given by a default, we add an explicit generic
11231 -- association for it in the instantiation node. When we save the
11232 -- global references on the name of the instance, we recover the list
11233 -- of generic associations, and add an explicit one to the original
11234 -- generic tree, through which a global actual can be preserved.
11235 -- Similarly, if a child unit is instantiated within a sibling, in the
11236 -- context of the parent, we must preserve the identifier of the parent
11237 -- so that it can be properly resolved in a subsequent instantiation.
11239 procedure Save_Global_Descendant (D : Union_Id);
11240 -- Apply Save_Global_References recursively to the descendents of the
11243 procedure Save_References (N : Node_Id);
11244 -- This is the recursive procedure that does the work, once the
11245 -- enclosing generic scope has been established.
11251 function Is_Global (E : Entity_Id) return Boolean is
11254 function Is_Instance_Node (Decl : Node_Id) return Boolean;
11255 -- Determine whether the parent node of a reference to a child unit
11256 -- denotes an instantiation or a formal package, in which case the
11257 -- reference to the child unit is global, even if it appears within
11258 -- the current scope (e.g. when the instance appears within the body
11259 -- of an ancestor).
11261 ----------------------
11262 -- Is_Instance_Node --
11263 ----------------------
11265 function Is_Instance_Node (Decl : Node_Id) return Boolean is
11267 return (Nkind (Decl) in N_Generic_Instantiation
11269 Nkind (Original_Node (Decl)) = N_Formal_Package_Declaration);
11270 end Is_Instance_Node;
11272 -- Start of processing for Is_Global
11275 if E = Gen_Scope then
11278 elsif E = Standard_Standard then
11281 elsif Is_Child_Unit (E)
11282 and then (Is_Instance_Node (Parent (N2))
11283 or else (Nkind (Parent (N2)) = N_Expanded_Name
11284 and then N2 = Selector_Name (Parent (N2))
11286 Is_Instance_Node (Parent (Parent (N2)))))
11292 while Se /= Gen_Scope loop
11293 if Se = Standard_Standard then
11308 procedure Reset_Entity (N : Node_Id) is
11310 procedure Set_Global_Type (N : Node_Id; N2 : Node_Id);
11311 -- If the type of N2 is global to the generic unit. Save
11312 -- the type in the generic node.
11314 function Top_Ancestor (E : Entity_Id) return Entity_Id;
11315 -- Find the ultimate ancestor of the current unit. If it is
11316 -- not a generic unit, then the name of the current unit
11317 -- in the prefix of an expanded name must be replaced with
11318 -- its generic homonym to ensure that it will be properly
11319 -- resolved in an instance.
11321 ---------------------
11322 -- Set_Global_Type --
11323 ---------------------
11325 procedure Set_Global_Type (N : Node_Id; N2 : Node_Id) is
11326 Typ : constant Entity_Id := Etype (N2);
11329 Set_Etype (N, Typ);
11331 if Entity (N) /= N2
11332 and then Has_Private_View (Entity (N))
11334 -- If the entity of N is not the associated node, this is
11335 -- a nested generic and it has an associated node as well,
11336 -- whose type is already the full view (see below). Indicate
11337 -- that the original node has a private view.
11339 Set_Has_Private_View (N);
11342 -- If not a private type, nothing else to do
11344 if not Is_Private_Type (Typ) then
11345 if Is_Array_Type (Typ)
11346 and then Is_Private_Type (Component_Type (Typ))
11348 Set_Has_Private_View (N);
11351 -- If it is a derivation of a private type in a context where
11352 -- no full view is needed, nothing to do either.
11354 elsif No (Full_View (Typ)) and then Typ /= Etype (Typ) then
11357 -- Otherwise mark the type for flipping and use the full_view
11361 Set_Has_Private_View (N);
11363 if Present (Full_View (Typ)) then
11364 Set_Etype (N2, Full_View (Typ));
11367 end Set_Global_Type;
11373 function Top_Ancestor (E : Entity_Id) return Entity_Id is
11378 while Is_Child_Unit (Par) loop
11379 Par := Scope (Par);
11385 -- Start of processing for Reset_Entity
11388 N2 := Get_Associated_Node (N);
11391 if Present (E) then
11392 if Is_Global (E) then
11393 Set_Global_Type (N, N2);
11395 elsif Nkind (N) = N_Op_Concat
11396 and then Is_Generic_Type (Etype (N2))
11398 (Base_Type (Etype (Right_Opnd (N2))) = Etype (N2)
11399 or else Base_Type (Etype (Left_Opnd (N2))) = Etype (N2))
11400 and then Is_Intrinsic_Subprogram (E)
11405 -- Entity is local. Mark generic node as unresolved.
11406 -- Note that now it does not have an entity.
11408 Set_Associated_Node (N, Empty);
11409 Set_Etype (N, Empty);
11412 if Nkind (Parent (N)) in N_Generic_Instantiation
11413 and then N = Name (Parent (N))
11415 Save_Global_Defaults (Parent (N), Parent (N2));
11418 elsif Nkind (Parent (N)) = N_Selected_Component
11419 and then Nkind (Parent (N2)) = N_Expanded_Name
11421 if Is_Global (Entity (Parent (N2))) then
11422 Change_Selected_Component_To_Expanded_Name (Parent (N));
11423 Set_Associated_Node (Parent (N), Parent (N2));
11424 Set_Global_Type (Parent (N), Parent (N2));
11425 Save_Entity_Descendants (N);
11427 -- If this is a reference to the current generic entity, replace
11428 -- by the name of the generic homonym of the current package. This
11429 -- is because in an instantiation Par.P.Q will not resolve to the
11430 -- name of the instance, whose enclosing scope is not necessarily
11431 -- Par. We use the generic homonym rather that the name of the
11432 -- generic itself, because it may be hidden by a local
11435 elsif In_Open_Scopes (Entity (Parent (N2)))
11437 Is_Generic_Unit (Top_Ancestor (Entity (Prefix (Parent (N2)))))
11439 if Ekind (Entity (Parent (N2))) = E_Generic_Package then
11440 Rewrite (Parent (N),
11441 Make_Identifier (Sloc (N),
11443 Chars (Generic_Homonym (Entity (Parent (N2))))));
11445 Rewrite (Parent (N),
11446 Make_Identifier (Sloc (N),
11447 Chars => Chars (Selector_Name (Parent (N2)))));
11451 if Nkind (Parent (Parent (N))) in N_Generic_Instantiation
11452 and then Parent (N) = Name (Parent (Parent (N)))
11454 Save_Global_Defaults
11455 (Parent (Parent (N)), Parent (Parent ((N2))));
11458 -- A selected component may denote a static constant that has been
11459 -- folded. If the static constant is global to the generic, capture
11460 -- its value. Otherwise the folding will happen in any instantiation,
11462 elsif Nkind (Parent (N)) = N_Selected_Component
11463 and then Nkind_In (Parent (N2), N_Integer_Literal, N_Real_Literal)
11465 if Present (Entity (Original_Node (Parent (N2))))
11466 and then Is_Global (Entity (Original_Node (Parent (N2))))
11468 Rewrite (Parent (N), New_Copy (Parent (N2)));
11469 Set_Analyzed (Parent (N), False);
11475 -- A selected component may be transformed into a parameterless
11476 -- function call. If the called entity is global, rewrite the node
11477 -- appropriately, i.e. as an extended name for the global entity.
11479 elsif Nkind (Parent (N)) = N_Selected_Component
11480 and then Nkind (Parent (N2)) = N_Function_Call
11481 and then N = Selector_Name (Parent (N))
11483 if No (Parameter_Associations (Parent (N2))) then
11484 if Is_Global (Entity (Name (Parent (N2)))) then
11485 Change_Selected_Component_To_Expanded_Name (Parent (N));
11486 Set_Associated_Node (Parent (N), Name (Parent (N2)));
11487 Set_Global_Type (Parent (N), Name (Parent (N2)));
11488 Save_Entity_Descendants (N);
11491 Set_Associated_Node (N, Empty);
11492 Set_Etype (N, Empty);
11495 -- In Ada 2005, X.F may be a call to a primitive operation,
11496 -- rewritten as F (X). This rewriting will be done again in an
11497 -- instance, so keep the original node. Global entities will be
11498 -- captured as for other constructs.
11504 -- Entity is local. Reset in generic unit, so that node is resolved
11505 -- anew at the point of instantiation.
11508 Set_Associated_Node (N, Empty);
11509 Set_Etype (N, Empty);
11513 -----------------------------
11514 -- Save_Entity_Descendants --
11515 -----------------------------
11517 procedure Save_Entity_Descendants (N : Node_Id) is
11520 when N_Binary_Op =>
11521 Save_Global_Descendant (Union_Id (Left_Opnd (N)));
11522 Save_Global_Descendant (Union_Id (Right_Opnd (N)));
11525 Save_Global_Descendant (Union_Id (Right_Opnd (N)));
11527 when N_Expanded_Name | N_Selected_Component =>
11528 Save_Global_Descendant (Union_Id (Prefix (N)));
11529 Save_Global_Descendant (Union_Id (Selector_Name (N)));
11531 when N_Identifier | N_Character_Literal | N_Operator_Symbol =>
11535 raise Program_Error;
11537 end Save_Entity_Descendants;
11539 --------------------------
11540 -- Save_Global_Defaults --
11541 --------------------------
11543 procedure Save_Global_Defaults (N1, N2 : Node_Id) is
11544 Loc : constant Source_Ptr := Sloc (N1);
11545 Assoc2 : constant List_Id := Generic_Associations (N2);
11546 Gen_Id : constant Entity_Id := Get_Generic_Entity (N2);
11553 Actual : Entity_Id;
11556 Assoc1 := Generic_Associations (N1);
11558 if Present (Assoc1) then
11559 Act1 := First (Assoc1);
11562 Set_Generic_Associations (N1, New_List);
11563 Assoc1 := Generic_Associations (N1);
11566 if Present (Assoc2) then
11567 Act2 := First (Assoc2);
11572 while Present (Act1) and then Present (Act2) loop
11577 -- Find the associations added for default subprograms
11579 if Present (Act2) then
11580 while Nkind (Act2) /= N_Generic_Association
11581 or else No (Entity (Selector_Name (Act2)))
11582 or else not Is_Overloadable (Entity (Selector_Name (Act2)))
11587 -- Add a similar association if the default is global. The
11588 -- renaming declaration for the actual has been analyzed, and
11589 -- its alias is the program it renames. Link the actual in the
11590 -- original generic tree with the node in the analyzed tree.
11592 while Present (Act2) loop
11593 Subp := Entity (Selector_Name (Act2));
11594 Def := Explicit_Generic_Actual_Parameter (Act2);
11596 -- Following test is defence against rubbish errors
11598 if No (Alias (Subp)) then
11602 -- Retrieve the resolved actual from the renaming declaration
11603 -- created for the instantiated formal.
11605 Actual := Entity (Name (Parent (Parent (Subp))));
11606 Set_Entity (Def, Actual);
11607 Set_Etype (Def, Etype (Actual));
11609 if Is_Global (Actual) then
11611 Make_Generic_Association (Loc,
11612 Selector_Name => New_Occurrence_Of (Subp, Loc),
11613 Explicit_Generic_Actual_Parameter =>
11614 New_Occurrence_Of (Actual, Loc));
11616 Set_Associated_Node
11617 (Explicit_Generic_Actual_Parameter (Ndec), Def);
11619 Append (Ndec, Assoc1);
11621 -- If there are other defaults, add a dummy association in case
11622 -- there are other defaulted formals with the same name.
11624 elsif Present (Next (Act2)) then
11626 Make_Generic_Association (Loc,
11627 Selector_Name => New_Occurrence_Of (Subp, Loc),
11628 Explicit_Generic_Actual_Parameter => Empty);
11630 Append (Ndec, Assoc1);
11637 if Nkind (Name (N1)) = N_Identifier
11638 and then Is_Child_Unit (Gen_Id)
11639 and then Is_Global (Gen_Id)
11640 and then Is_Generic_Unit (Scope (Gen_Id))
11641 and then In_Open_Scopes (Scope (Gen_Id))
11643 -- This is an instantiation of a child unit within a sibling,
11644 -- so that the generic parent is in scope. An eventual instance
11645 -- must occur within the scope of an instance of the parent.
11646 -- Make name in instance into an expanded name, to preserve the
11647 -- identifier of the parent, so it can be resolved subsequently.
11649 Rewrite (Name (N2),
11650 Make_Expanded_Name (Loc,
11651 Chars => Chars (Gen_Id),
11652 Prefix => New_Occurrence_Of (Scope (Gen_Id), Loc),
11653 Selector_Name => New_Occurrence_Of (Gen_Id, Loc)));
11654 Set_Entity (Name (N2), Gen_Id);
11656 Rewrite (Name (N1),
11657 Make_Expanded_Name (Loc,
11658 Chars => Chars (Gen_Id),
11659 Prefix => New_Occurrence_Of (Scope (Gen_Id), Loc),
11660 Selector_Name => New_Occurrence_Of (Gen_Id, Loc)));
11662 Set_Associated_Node (Name (N1), Name (N2));
11663 Set_Associated_Node (Prefix (Name (N1)), Empty);
11664 Set_Associated_Node
11665 (Selector_Name (Name (N1)), Selector_Name (Name (N2)));
11666 Set_Etype (Name (N1), Etype (Gen_Id));
11669 end Save_Global_Defaults;
11671 ----------------------------
11672 -- Save_Global_Descendant --
11673 ----------------------------
11675 procedure Save_Global_Descendant (D : Union_Id) is
11679 if D in Node_Range then
11680 if D = Union_Id (Empty) then
11683 elsif Nkind (Node_Id (D)) /= N_Compilation_Unit then
11684 Save_References (Node_Id (D));
11687 elsif D in List_Range then
11688 if D = Union_Id (No_List)
11689 or else Is_Empty_List (List_Id (D))
11694 N1 := First (List_Id (D));
11695 while Present (N1) loop
11696 Save_References (N1);
11701 -- Element list or other non-node field, nothing to do
11706 end Save_Global_Descendant;
11708 ---------------------
11709 -- Save_References --
11710 ---------------------
11712 -- This is the recursive procedure that does the work, once the
11713 -- enclosing generic scope has been established. We have to treat
11714 -- specially a number of node rewritings that are required by semantic
11715 -- processing and which change the kind of nodes in the generic copy:
11716 -- typically constant-folding, replacing an operator node by a string
11717 -- literal, or a selected component by an expanded name. In each of
11718 -- those cases, the transformation is propagated to the generic unit.
11720 procedure Save_References (N : Node_Id) is
11725 elsif Nkind_In (N, N_Character_Literal, N_Operator_Symbol) then
11726 if Nkind (N) = Nkind (Get_Associated_Node (N)) then
11729 elsif Nkind (N) = N_Operator_Symbol
11730 and then Nkind (Get_Associated_Node (N)) = N_String_Literal
11732 Change_Operator_Symbol_To_String_Literal (N);
11735 elsif Nkind (N) in N_Op then
11736 if Nkind (N) = Nkind (Get_Associated_Node (N)) then
11737 if Nkind (N) = N_Op_Concat then
11738 Set_Is_Component_Left_Opnd (N,
11739 Is_Component_Left_Opnd (Get_Associated_Node (N)));
11741 Set_Is_Component_Right_Opnd (N,
11742 Is_Component_Right_Opnd (Get_Associated_Node (N)));
11748 -- Node may be transformed into call to a user-defined operator
11750 N2 := Get_Associated_Node (N);
11752 if Nkind (N2) = N_Function_Call then
11753 E := Entity (Name (N2));
11756 and then Is_Global (E)
11758 Set_Etype (N, Etype (N2));
11760 Set_Associated_Node (N, Empty);
11761 Set_Etype (N, Empty);
11764 elsif Nkind_In (N2, N_Integer_Literal,
11768 if Present (Original_Node (N2))
11769 and then Nkind (Original_Node (N2)) = Nkind (N)
11772 -- Operation was constant-folded. Whenever possible,
11773 -- recover semantic information from unfolded node,
11776 Set_Associated_Node (N, Original_Node (N2));
11778 if Nkind (N) = N_Op_Concat then
11779 Set_Is_Component_Left_Opnd (N,
11780 Is_Component_Left_Opnd (Get_Associated_Node (N)));
11781 Set_Is_Component_Right_Opnd (N,
11782 Is_Component_Right_Opnd (Get_Associated_Node (N)));
11788 -- If original node is already modified, propagate
11789 -- constant-folding to template.
11791 Rewrite (N, New_Copy (N2));
11792 Set_Analyzed (N, False);
11795 elsif Nkind (N2) = N_Identifier
11796 and then Ekind (Entity (N2)) = E_Enumeration_Literal
11798 -- Same if call was folded into a literal, but in this case
11799 -- retain the entity to avoid spurious ambiguities if id is
11800 -- overloaded at the point of instantiation or inlining.
11802 Rewrite (N, New_Copy (N2));
11803 Set_Analyzed (N, False);
11807 -- Complete operands check if node has not been constant-folded
11809 if Nkind (N) in N_Op then
11810 Save_Entity_Descendants (N);
11813 elsif Nkind (N) = N_Identifier then
11814 if Nkind (N) = Nkind (Get_Associated_Node (N)) then
11816 -- If this is a discriminant reference, always save it. It is
11817 -- used in the instance to find the corresponding discriminant
11818 -- positionally rather than by name.
11820 Set_Original_Discriminant
11821 (N, Original_Discriminant (Get_Associated_Node (N)));
11825 N2 := Get_Associated_Node (N);
11827 if Nkind (N2) = N_Function_Call then
11828 E := Entity (Name (N2));
11830 -- Name resolves to a call to parameterless function. If
11831 -- original entity is global, mark node as resolved.
11834 and then Is_Global (E)
11836 Set_Etype (N, Etype (N2));
11838 Set_Associated_Node (N, Empty);
11839 Set_Etype (N, Empty);
11842 elsif Nkind_In (N2, N_Integer_Literal, N_Real_Literal)
11843 and then Is_Entity_Name (Original_Node (N2))
11845 -- Name resolves to named number that is constant-folded,
11846 -- We must preserve the original name for ASIS use, and
11847 -- undo the constant-folding, which will be repeated in
11850 Set_Associated_Node (N, Original_Node (N2));
11853 elsif Nkind (N2) = N_String_Literal then
11855 -- Name resolves to string literal. Perform the same
11856 -- replacement in generic.
11858 Rewrite (N, New_Copy (N2));
11860 elsif Nkind (N2) = N_Explicit_Dereference then
11862 -- An identifier is rewritten as a dereference if it is
11863 -- the prefix in a selected component, and it denotes an
11864 -- access to a composite type, or a parameterless function
11865 -- call that returns an access type.
11867 -- Check whether corresponding entity in prefix is global
11869 if Is_Entity_Name (Prefix (N2))
11870 and then Present (Entity (Prefix (N2)))
11871 and then Is_Global (Entity (Prefix (N2)))
11874 Make_Explicit_Dereference (Sloc (N),
11875 Prefix => Make_Identifier (Sloc (N),
11876 Chars => Chars (N))));
11877 Set_Associated_Node (Prefix (N), Prefix (N2));
11879 elsif Nkind (Prefix (N2)) = N_Function_Call
11880 and then Is_Global (Entity (Name (Prefix (N2))))
11883 Make_Explicit_Dereference (Sloc (N),
11884 Prefix => Make_Function_Call (Sloc (N),
11886 Make_Identifier (Sloc (N),
11887 Chars => Chars (N)))));
11889 Set_Associated_Node
11890 (Name (Prefix (N)), Name (Prefix (N2)));
11893 Set_Associated_Node (N, Empty);
11894 Set_Etype (N, Empty);
11897 -- The subtype mark of a nominally unconstrained object is
11898 -- rewritten as a subtype indication using the bounds of the
11899 -- expression. Recover the original subtype mark.
11901 elsif Nkind (N2) = N_Subtype_Indication
11902 and then Is_Entity_Name (Original_Node (N2))
11904 Set_Associated_Node (N, Original_Node (N2));
11912 elsif Nkind (N) in N_Entity then
11917 Loc : constant Source_Ptr := Sloc (N);
11918 Qual : Node_Id := Empty;
11919 Typ : Entity_Id := Empty;
11922 use Atree.Unchecked_Access;
11923 -- This code section is part of implementing an untyped tree
11924 -- traversal, so it needs direct access to node fields.
11927 if Nkind_In (N, N_Aggregate, N_Extension_Aggregate) then
11928 N2 := Get_Associated_Node (N);
11935 -- In an instance within a generic, use the name of the
11936 -- actual and not the original generic parameter. If the
11937 -- actual is global in the current generic it must be
11938 -- preserved for its instantiation.
11940 if Nkind (Parent (Typ)) = N_Subtype_Declaration
11942 Present (Generic_Parent_Type (Parent (Typ)))
11944 Typ := Base_Type (Typ);
11945 Set_Etype (N2, Typ);
11951 or else not Is_Global (Typ)
11953 Set_Associated_Node (N, Empty);
11955 -- If the aggregate is an actual in a call, it has been
11956 -- resolved in the current context, to some local type.
11957 -- The enclosing call may have been disambiguated by the
11958 -- aggregate, and this disambiguation might fail at
11959 -- instantiation time because the type to which the
11960 -- aggregate did resolve is not preserved. In order to
11961 -- preserve some of this information, we wrap the
11962 -- aggregate in a qualified expression, using the id of
11963 -- its type. For further disambiguation we qualify the
11964 -- type name with its scope (if visible) because both
11965 -- id's will have corresponding entities in an instance.
11966 -- This resolves most of the problems with missing type
11967 -- information on aggregates in instances.
11969 if Nkind (N2) = Nkind (N)
11971 Nkind_In (Parent (N2), N_Procedure_Call_Statement,
11973 and then Comes_From_Source (Typ)
11975 if Is_Immediately_Visible (Scope (Typ)) then
11976 Nam := Make_Selected_Component (Loc,
11978 Make_Identifier (Loc, Chars (Scope (Typ))),
11980 Make_Identifier (Loc, Chars (Typ)));
11982 Nam := Make_Identifier (Loc, Chars (Typ));
11986 Make_Qualified_Expression (Loc,
11987 Subtype_Mark => Nam,
11988 Expression => Relocate_Node (N));
11992 Save_Global_Descendant (Field1 (N));
11993 Save_Global_Descendant (Field2 (N));
11994 Save_Global_Descendant (Field3 (N));
11995 Save_Global_Descendant (Field5 (N));
11997 if Present (Qual) then
12001 -- All other cases than aggregates
12004 Save_Global_Descendant (Field1 (N));
12005 Save_Global_Descendant (Field2 (N));
12006 Save_Global_Descendant (Field3 (N));
12007 Save_Global_Descendant (Field4 (N));
12008 Save_Global_Descendant (Field5 (N));
12012 end Save_References;
12014 -- Start of processing for Save_Global_References
12017 Gen_Scope := Current_Scope;
12019 -- If the generic unit is a child unit, references to entities in the
12020 -- parent are treated as local, because they will be resolved anew in
12021 -- the context of the instance of the parent.
12023 while Is_Child_Unit (Gen_Scope)
12024 and then Ekind (Scope (Gen_Scope)) = E_Generic_Package
12026 Gen_Scope := Scope (Gen_Scope);
12029 Save_References (N);
12030 end Save_Global_References;
12032 --------------------------------------
12033 -- Set_Copied_Sloc_For_Inlined_Body --
12034 --------------------------------------
12036 procedure Set_Copied_Sloc_For_Inlined_Body (N : Node_Id; E : Entity_Id) is
12038 Create_Instantiation_Source (N, E, True, S_Adjustment);
12039 end Set_Copied_Sloc_For_Inlined_Body;
12041 ---------------------
12042 -- Set_Instance_Of --
12043 ---------------------
12045 procedure Set_Instance_Of (A : Entity_Id; B : Entity_Id) is
12047 Generic_Renamings.Table (Generic_Renamings.Last) := (A, B, Assoc_Null);
12048 Generic_Renamings_HTable.Set (Generic_Renamings.Last);
12049 Generic_Renamings.Increment_Last;
12050 end Set_Instance_Of;
12052 --------------------
12053 -- Set_Next_Assoc --
12054 --------------------
12056 procedure Set_Next_Assoc (E : Assoc_Ptr; Next : Assoc_Ptr) is
12058 Generic_Renamings.Table (E).Next_In_HTable := Next;
12059 end Set_Next_Assoc;
12061 -------------------
12062 -- Start_Generic --
12063 -------------------
12065 procedure Start_Generic is
12067 -- ??? More things could be factored out in this routine.
12068 -- Should probably be done at a later stage.
12070 Generic_Flags.Append (Inside_A_Generic);
12071 Inside_A_Generic := True;
12073 Expander_Mode_Save_And_Set (False);
12076 ----------------------
12077 -- Set_Instance_Env --
12078 ----------------------
12080 procedure Set_Instance_Env
12081 (Gen_Unit : Entity_Id;
12082 Act_Unit : Entity_Id)
12085 -- Regardless of the current mode, predefined units are analyzed in
12086 -- the most current Ada mode, and earlier version Ada checks do not
12087 -- apply to predefined units. Nothing needs to be done for non-internal
12088 -- units. These are always analyzed in the current mode.
12090 if Is_Internal_File_Name
12091 (Fname => Unit_File_Name (Get_Source_Unit (Gen_Unit)),
12092 Renamings_Included => True)
12094 Set_Opt_Config_Switches (True, Current_Sem_Unit = Main_Unit);
12097 Current_Instantiated_Parent := (Gen_Unit, Act_Unit, Assoc_Null);
12098 end Set_Instance_Env;
12104 procedure Switch_View (T : Entity_Id) is
12105 BT : constant Entity_Id := Base_Type (T);
12106 Priv_Elmt : Elmt_Id := No_Elmt;
12107 Priv_Sub : Entity_Id;
12110 -- T may be private but its base type may have been exchanged through
12111 -- some other occurrence, in which case there is nothing to switch
12112 -- besides T itself. Note that a private dependent subtype of a private
12113 -- type might not have been switched even if the base type has been,
12114 -- because of the last branch of Check_Private_View (see comment there).
12116 if not Is_Private_Type (BT) then
12117 Prepend_Elmt (Full_View (T), Exchanged_Views);
12118 Exchange_Declarations (T);
12122 Priv_Elmt := First_Elmt (Private_Dependents (BT));
12124 if Present (Full_View (BT)) then
12125 Prepend_Elmt (Full_View (BT), Exchanged_Views);
12126 Exchange_Declarations (BT);
12129 while Present (Priv_Elmt) loop
12130 Priv_Sub := (Node (Priv_Elmt));
12132 -- We avoid flipping the subtype if the Etype of its full view is
12133 -- private because this would result in a malformed subtype. This
12134 -- occurs when the Etype of the subtype full view is the full view of
12135 -- the base type (and since the base types were just switched, the
12136 -- subtype is pointing to the wrong view). This is currently the case
12137 -- for tagged record types, access types (maybe more?) and needs to
12138 -- be resolved. ???
12140 if Present (Full_View (Priv_Sub))
12141 and then not Is_Private_Type (Etype (Full_View (Priv_Sub)))
12143 Prepend_Elmt (Full_View (Priv_Sub), Exchanged_Views);
12144 Exchange_Declarations (Priv_Sub);
12147 Next_Elmt (Priv_Elmt);
12151 -----------------------------
12152 -- Valid_Default_Attribute --
12153 -----------------------------
12155 procedure Valid_Default_Attribute (Nam : Entity_Id; Def : Node_Id) is
12156 Attr_Id : constant Attribute_Id :=
12157 Get_Attribute_Id (Attribute_Name (Def));
12158 T : constant Entity_Id := Entity (Prefix (Def));
12159 Is_Fun : constant Boolean := (Ekind (Nam) = E_Function);
12172 F := First_Formal (Nam);
12173 while Present (F) loop
12174 Num_F := Num_F + 1;
12179 when Attribute_Adjacent | Attribute_Ceiling | Attribute_Copy_Sign |
12180 Attribute_Floor | Attribute_Fraction | Attribute_Machine |
12181 Attribute_Model | Attribute_Remainder | Attribute_Rounding |
12182 Attribute_Unbiased_Rounding =>
12185 and then Is_Floating_Point_Type (T);
12187 when Attribute_Image | Attribute_Pred | Attribute_Succ |
12188 Attribute_Value | Attribute_Wide_Image |
12189 Attribute_Wide_Value =>
12190 OK := (Is_Fun and then Num_F = 1 and then Is_Scalar_Type (T));
12192 when Attribute_Max | Attribute_Min =>
12193 OK := (Is_Fun and then Num_F = 2 and then Is_Scalar_Type (T));
12195 when Attribute_Input =>
12196 OK := (Is_Fun and then Num_F = 1);
12198 when Attribute_Output | Attribute_Read | Attribute_Write =>
12199 OK := (not Is_Fun and then Num_F = 2);
12206 Error_Msg_N ("attribute reference has wrong profile for subprogram",
12209 end Valid_Default_Attribute;