1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
9 -- Copyright (C) 1992-2014, 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 Aspects; use Aspects;
27 with Atree; use Atree;
28 with Einfo; use Einfo;
29 with Elists; use Elists;
30 with Errout; use Errout;
31 with Expander; use Expander;
32 with Exp_Disp; use Exp_Disp;
33 with Fname; use Fname;
34 with Fname.UF; use Fname.UF;
35 with Freeze; use Freeze;
36 with Itypes; use Itypes;
38 with Lib.Load; use Lib.Load;
39 with Lib.Xref; use Lib.Xref;
40 with Nlists; use Nlists;
41 with Namet; use Namet;
42 with Nmake; use Nmake;
44 with Rident; use Rident;
45 with Restrict; use Restrict;
46 with Rtsfind; use Rtsfind;
48 with Sem_Aux; use Sem_Aux;
49 with Sem_Cat; use Sem_Cat;
50 with Sem_Ch3; use Sem_Ch3;
51 with Sem_Ch6; use Sem_Ch6;
52 with Sem_Ch7; use Sem_Ch7;
53 with Sem_Ch8; use Sem_Ch8;
54 with Sem_Ch10; use Sem_Ch10;
55 with Sem_Ch13; use Sem_Ch13;
56 with Sem_Dim; use Sem_Dim;
57 with Sem_Disp; use Sem_Disp;
58 with Sem_Elab; use Sem_Elab;
59 with Sem_Elim; use Sem_Elim;
60 with Sem_Eval; use Sem_Eval;
61 with Sem_Prag; use Sem_Prag;
62 with Sem_Res; use Sem_Res;
63 with Sem_Type; use Sem_Type;
64 with Sem_Util; use Sem_Util;
65 with Sem_Warn; use Sem_Warn;
66 with Stand; use Stand;
67 with Sinfo; use Sinfo;
68 with Sinfo.CN; use Sinfo.CN;
69 with Sinput; use Sinput;
70 with Sinput.L; use Sinput.L;
71 with Snames; use Snames;
72 with Stringt; use Stringt;
73 with Uname; use Uname;
75 with Tbuild; use Tbuild;
76 with Uintp; use Uintp;
77 with Urealp; use Urealp;
78 with Warnsw; use Warnsw;
82 package body Sem_Ch12 is
84 ----------------------------------------------------------
85 -- Implementation of Generic Analysis and Instantiation --
86 ----------------------------------------------------------
88 -- GNAT implements generics by macro expansion. No attempt is made to share
89 -- generic instantiations (for now). Analysis of a generic definition does
90 -- not perform any expansion action, but the expander must be called on the
91 -- tree for each instantiation, because the expansion may of course depend
92 -- on the generic actuals. All of this is best achieved as follows:
94 -- a) Semantic analysis of a generic unit is performed on a copy of the
95 -- tree for the generic unit. All tree modifications that follow analysis
96 -- do not affect the original tree. Links are kept between the original
97 -- tree and the copy, in order to recognize non-local references within
98 -- the generic, and propagate them to each instance (recall that name
99 -- resolution is done on the generic declaration: generics are not really
100 -- macros). This is summarized in the following diagram:
102 -- .-----------. .----------.
103 -- | semantic |<--------------| generic |
105 -- | |==============>| |
106 -- |___________| global |__________|
117 -- b) Each instantiation copies the original tree, and inserts into it a
118 -- series of declarations that describe the mapping between generic formals
119 -- and actuals. For example, a generic In OUT parameter is an object
120 -- renaming of the corresponding actual, etc. Generic IN parameters are
121 -- constant declarations.
123 -- c) In order to give the right visibility for these renamings, we use
124 -- a different scheme for package and subprogram instantiations. For
125 -- packages, the list of renamings is inserted into the package
126 -- specification, before the visible declarations of the package. The
127 -- renamings are analyzed before any of the text of the instance, and are
128 -- thus visible at the right place. Furthermore, outside of the instance,
129 -- the generic parameters are visible and denote their corresponding
132 -- For subprograms, we create a container package to hold the renamings
133 -- and the subprogram instance itself. Analysis of the package makes the
134 -- renaming declarations visible to the subprogram. After analyzing the
135 -- package, the defining entity for the subprogram is touched-up so that
136 -- it appears declared in the current scope, and not inside the container
139 -- If the instantiation is a compilation unit, the container package is
140 -- given the same name as the subprogram instance. This ensures that
141 -- the elaboration procedure called by the binder, using the compilation
142 -- unit name, calls in fact the elaboration procedure for the package.
144 -- Not surprisingly, private types complicate this approach. By saving in
145 -- the original generic object the non-local references, we guarantee that
146 -- the proper entities are referenced at the point of instantiation.
147 -- However, for private types, this by itself does not insure that the
148 -- proper VIEW of the entity is used (the full type may be visible at the
149 -- point of generic definition, but not at instantiation, or vice-versa).
150 -- In order to reference the proper view, we special-case any reference
151 -- to private types in the generic object, by saving both views, one in
152 -- the generic and one in the semantic copy. At time of instantiation, we
153 -- check whether the two views are consistent, and exchange declarations if
154 -- necessary, in order to restore the correct visibility. Similarly, if
155 -- the instance view is private when the generic view was not, we perform
156 -- the exchange. After completing the instantiation, we restore the
157 -- current visibility. The flag Has_Private_View marks identifiers in the
158 -- the generic unit that require checking.
160 -- Visibility within nested generic units requires special handling.
161 -- Consider the following scheme:
163 -- type Global is ... -- outside of generic unit.
167 -- type Semi_Global is ... -- global to inner.
170 -- procedure inner (X1 : Global; X2 : Semi_Global);
172 -- procedure in2 is new inner (...); -- 4
175 -- package New_Outer is new Outer (...); -- 2
176 -- procedure New_Inner is new New_Outer.Inner (...); -- 3
178 -- The semantic analysis of Outer captures all occurrences of Global.
179 -- The semantic analysis of Inner (at 1) captures both occurrences of
180 -- Global and Semi_Global.
182 -- At point 2 (instantiation of Outer), we also produce a generic copy
183 -- of Inner, even though Inner is, at that point, not being instantiated.
184 -- (This is just part of the semantic analysis of New_Outer).
186 -- Critically, references to Global within Inner must be preserved, while
187 -- references to Semi_Global should not preserved, because they must now
188 -- resolve to an entity within New_Outer. To distinguish between these, we
189 -- use a global variable, Current_Instantiated_Parent, which is set when
190 -- performing a generic copy during instantiation (at 2). This variable is
191 -- used when performing a generic copy that is not an instantiation, but
192 -- that is nested within one, as the occurrence of 1 within 2. The analysis
193 -- of a nested generic only preserves references that are global to the
194 -- enclosing Current_Instantiated_Parent. We use the Scope_Depth value to
195 -- determine whether a reference is external to the given parent.
197 -- The instantiation at point 3 requires no special treatment. The method
198 -- works as well for further nestings of generic units, but of course the
199 -- variable Current_Instantiated_Parent must be stacked because nested
200 -- instantiations can occur, e.g. the occurrence of 4 within 2.
202 -- The instantiation of package and subprogram bodies is handled in a
203 -- similar manner, except that it is delayed until after semantic
204 -- analysis is complete. In this fashion complex cross-dependencies
205 -- between several package declarations and bodies containing generics
206 -- can be compiled which otherwise would diagnose spurious circularities.
208 -- For example, it is possible to compile two packages A and B that
209 -- have the following structure:
211 -- package A is package B is
212 -- generic ... generic ...
213 -- package G_A is package G_B is
216 -- package body A is package body B is
217 -- package N_B is new G_B (..) package N_A is new G_A (..)
219 -- The table Pending_Instantiations in package Inline is used to keep
220 -- track of body instantiations that are delayed in this manner. Inline
221 -- handles the actual calls to do the body instantiations. This activity
222 -- is part of Inline, since the processing occurs at the same point, and
223 -- for essentially the same reason, as the handling of inlined routines.
225 ----------------------------------------------
226 -- Detection of Instantiation Circularities --
227 ----------------------------------------------
229 -- If we have a chain of instantiations that is circular, this is static
230 -- error which must be detected at compile time. The detection of these
231 -- circularities is carried out at the point that we insert a generic
232 -- instance spec or body. If there is a circularity, then the analysis of
233 -- the offending spec or body will eventually result in trying to load the
234 -- same unit again, and we detect this problem as we analyze the package
235 -- instantiation for the second time.
237 -- At least in some cases after we have detected the circularity, we get
238 -- into trouble if we try to keep going. The following flag is set if a
239 -- circularity is detected, and used to abandon compilation after the
240 -- messages have been posted.
242 Circularity_Detected : Boolean := False;
243 -- This should really be reset on encountering a new main unit, but in
244 -- practice we are not using multiple main units so it is not critical.
246 --------------------------------------------------
247 -- Formal packages and partial parameterization --
248 --------------------------------------------------
250 -- When compiling a generic, a formal package is a local instantiation. If
251 -- declared with a box, its generic formals are visible in the enclosing
252 -- generic. If declared with a partial list of actuals, those actuals that
253 -- are defaulted (covered by an Others clause, or given an explicit box
254 -- initialization) are also visible in the enclosing generic, while those
255 -- that have a corresponding actual are not.
257 -- In our source model of instantiation, the same visibility must be
258 -- present in the spec and body of an instance: the names of the formals
259 -- that are defaulted must be made visible within the instance, and made
260 -- invisible (hidden) after the instantiation is complete, so that they
261 -- are not accessible outside of the instance.
263 -- In a generic, a formal package is treated like a special instantiation.
264 -- Our Ada 95 compiler handled formals with and without box in different
265 -- ways. With partial parameterization, we use a single model for both.
266 -- We create a package declaration that consists of the specification of
267 -- the generic package, and a set of declarations that map the actuals
268 -- into local renamings, just as we do for bona fide instantiations. For
269 -- defaulted parameters and formals with a box, we copy directly the
270 -- declarations of the formal into this local package. The result is a
271 -- a package whose visible declarations may include generic formals. This
272 -- package is only used for type checking and visibility analysis, and
273 -- never reaches the back-end, so it can freely violate the placement
274 -- rules for generic formal declarations.
276 -- The list of declarations (renamings and copies of formals) is built
277 -- by Analyze_Associations, just as for regular instantiations.
279 -- At the point of instantiation, conformance checking must be applied only
280 -- to those parameters that were specified in the formal. We perform this
281 -- checking by creating another internal instantiation, this one including
282 -- only the renamings and the formals (the rest of the package spec is not
283 -- relevant to conformance checking). We can then traverse two lists: the
284 -- list of actuals in the instance that corresponds to the formal package,
285 -- and the list of actuals produced for this bogus instantiation. We apply
286 -- the conformance rules to those actuals that are not defaulted (i.e.
287 -- which still appear as generic formals.
289 -- When we compile an instance body we must make the right parameters
290 -- visible again. The predicate Is_Generic_Formal indicates which of the
291 -- formals should have its Is_Hidden flag reset.
293 -----------------------
294 -- Local subprograms --
295 -----------------------
297 procedure Abandon_Instantiation (N : Node_Id);
298 pragma No_Return (Abandon_Instantiation);
299 -- Posts an error message "instantiation abandoned" at the indicated node
300 -- and then raises the exception Instantiation_Error to do it.
302 procedure Analyze_Formal_Array_Type
303 (T : in out Entity_Id;
305 -- A formal array type is treated like an array type declaration, and
306 -- invokes Array_Type_Declaration (sem_ch3) whose first parameter is
307 -- in-out, because in the case of an anonymous type the entity is
308 -- actually created in the procedure.
310 -- The following procedures treat other kinds of formal parameters
312 procedure Analyze_Formal_Derived_Interface_Type
317 procedure Analyze_Formal_Derived_Type
322 procedure Analyze_Formal_Interface_Type
327 -- The following subprograms create abbreviated declarations for formal
328 -- scalar types. We introduce an anonymous base of the proper class for
329 -- each of them, and define the formals as constrained first subtypes of
330 -- their bases. The bounds are expressions that are non-static in the
333 procedure Analyze_Formal_Decimal_Fixed_Point_Type
334 (T : Entity_Id; Def : Node_Id);
335 procedure Analyze_Formal_Discrete_Type (T : Entity_Id; Def : Node_Id);
336 procedure Analyze_Formal_Floating_Type (T : Entity_Id; Def : Node_Id);
337 procedure Analyze_Formal_Signed_Integer_Type (T : Entity_Id; Def : Node_Id);
338 procedure Analyze_Formal_Modular_Type (T : Entity_Id; Def : Node_Id);
339 procedure Analyze_Formal_Ordinary_Fixed_Point_Type
340 (T : Entity_Id; Def : Node_Id);
342 procedure Analyze_Formal_Private_Type
346 -- Creates a new private type, which does not require completion
348 procedure Analyze_Formal_Incomplete_Type (T : Entity_Id; Def : Node_Id);
349 -- Ada 2012: Creates a new incomplete type whose actual does not freeze
351 procedure Analyze_Generic_Formal_Part (N : Node_Id);
352 -- Analyze generic formal part
354 procedure Analyze_Generic_Access_Type (T : Entity_Id; Def : Node_Id);
355 -- Create a new access type with the given designated type
357 function Analyze_Associations
360 F_Copy : List_Id) return List_Id;
361 -- At instantiation time, build the list of associations between formals
362 -- and actuals. Each association becomes a renaming declaration for the
363 -- formal entity. F_Copy is the analyzed list of formals in the generic
364 -- copy. It is used to apply legality checks to the actuals. I_Node is the
365 -- instantiation node itself.
367 procedure Analyze_Subprogram_Instantiation
371 procedure Build_Instance_Compilation_Unit_Nodes
375 -- This procedure is used in the case where the generic instance of a
376 -- subprogram body or package body is a library unit. In this case, the
377 -- original library unit node for the generic instantiation must be
378 -- replaced by the resulting generic body, and a link made to a new
379 -- compilation unit node for the generic declaration. The argument N is
380 -- the original generic instantiation. Act_Body and Act_Decl are the body
381 -- and declaration of the instance (either package body and declaration
382 -- nodes or subprogram body and declaration nodes depending on the case).
383 -- On return, the node N has been rewritten with the actual body.
385 procedure Check_Access_Definition (N : Node_Id);
386 -- Subsidiary routine to null exclusion processing. Perform an assertion
387 -- check on Ada version and the presence of an access definition in N.
389 procedure Check_Formal_Packages (P_Id : Entity_Id);
390 -- Apply the following to all formal packages in generic associations
392 procedure Check_Formal_Package_Instance
393 (Formal_Pack : Entity_Id;
394 Actual_Pack : Entity_Id);
395 -- Verify that the actuals of the actual instance match the actuals of
396 -- the template for a formal package that is not declared with a box.
398 procedure Check_Forward_Instantiation (Decl : Node_Id);
399 -- If the generic is a local entity and the corresponding body has not
400 -- been seen yet, flag enclosing packages to indicate that it will be
401 -- elaborated after the generic body. Subprograms declared in the same
402 -- package cannot be inlined by the front-end because front-end inlining
403 -- requires a strict linear order of elaboration.
405 function Check_Hidden_Primitives (Assoc_List : List_Id) return Elist_Id;
406 -- Check if some association between formals and actuals requires to make
407 -- visible primitives of a tagged type, and make those primitives visible.
408 -- Return the list of primitives whose visibility is modified (to restore
409 -- their visibility later through Restore_Hidden_Primitives). If no
410 -- candidate is found then return No_Elist.
412 procedure Check_Hidden_Child_Unit
414 Gen_Unit : Entity_Id;
415 Act_Decl_Id : Entity_Id);
416 -- If the generic unit is an implicit child instance within a parent
417 -- instance, we need to make an explicit test that it is not hidden by
418 -- a child instance of the same name and parent.
420 procedure Check_Generic_Actuals
421 (Instance : Entity_Id;
422 Is_Formal_Box : Boolean);
423 -- Similar to previous one. Check the actuals in the instantiation,
424 -- whose views can change between the point of instantiation and the point
425 -- of instantiation of the body. In addition, mark the generic renamings
426 -- as generic actuals, so that they are not compatible with other actuals.
427 -- Recurse on an actual that is a formal package whose declaration has
430 function Contains_Instance_Of
433 N : Node_Id) return Boolean;
434 -- Inner is instantiated within the generic Outer. Check whether Inner
435 -- directly or indirectly contains an instance of Outer or of one of its
436 -- parents, in the case of a subunit. Each generic unit holds a list of
437 -- the entities instantiated within (at any depth). This procedure
438 -- determines whether the set of such lists contains a cycle, i.e. an
439 -- illegal circular instantiation.
441 function Denotes_Formal_Package
443 On_Exit : Boolean := False;
444 Instance : Entity_Id := Empty) return Boolean;
445 -- Returns True if E is a formal package of an enclosing generic, or
446 -- the actual for such a formal in an enclosing instantiation. If such
447 -- a package is used as a formal in an nested generic, or as an actual
448 -- in a nested instantiation, the visibility of ITS formals should not
449 -- be modified. When called from within Restore_Private_Views, the flag
450 -- On_Exit is true, to indicate that the search for a possible enclosing
451 -- instance should ignore the current one. In that case Instance denotes
452 -- the declaration for which this is an actual. This declaration may be
453 -- an instantiation in the source, or the internal instantiation that
454 -- corresponds to the actual for a formal package.
456 function Earlier (N1, N2 : Node_Id) return Boolean;
457 -- Yields True if N1 and N2 appear in the same compilation unit,
458 -- ignoring subunits, and if N1 is to the left of N2 in a left-to-right
459 -- traversal of the tree for the unit. Used to determine the placement
460 -- of freeze nodes for instance bodies that may depend on other instances.
462 function Find_Actual_Type
464 Gen_Type : Entity_Id) return Entity_Id;
465 -- When validating the actual types of a child instance, check whether
466 -- the formal is a formal type of the parent unit, and retrieve the current
467 -- actual for it. Typ is the entity in the analyzed formal type declaration
468 -- (component or index type of an array type, or designated type of an
469 -- access formal) and Gen_Type is the enclosing analyzed formal array
470 -- or access type. The desired actual may be a formal of a parent, or may
471 -- be declared in a formal package of a parent. In both cases it is a
472 -- generic actual type because it appears within a visible instance.
473 -- Finally, it may be declared in a parent unit without being a formal
474 -- of that unit, in which case it must be retrieved by visibility.
475 -- Ambiguities may still arise if two homonyms are declared in two formal
476 -- packages, and the prefix of the formal type may be needed to resolve
477 -- the ambiguity in the instance ???
479 function In_Same_Declarative_Part
481 Inst : Node_Id) return Boolean;
482 -- True if the instantiation Inst and the given freeze_node F_Node appear
483 -- within the same declarative part, ignoring subunits, but with no inter-
484 -- vening subprograms or concurrent units. Used to find the proper plave
485 -- for the freeze node of an instance, when the generic is declared in a
486 -- previous instance. If predicate is true, the freeze node of the instance
487 -- can be placed after the freeze node of the previous instance, Otherwise
488 -- it has to be placed at the end of the current declarative part.
490 function In_Main_Context (E : Entity_Id) return Boolean;
491 -- Check whether an instantiation is in the context of the main unit.
492 -- Used to determine whether its body should be elaborated to allow
493 -- front-end inlining.
495 procedure Set_Instance_Env
496 (Gen_Unit : Entity_Id;
497 Act_Unit : Entity_Id);
498 -- Save current instance on saved environment, to be used to determine
499 -- the global status of entities in nested instances. Part of Save_Env.
500 -- called after verifying that the generic unit is legal for the instance,
501 -- The procedure also examines whether the generic unit is a predefined
502 -- unit, in order to set configuration switches accordingly. As a result
503 -- the procedure must be called after analyzing and freezing the actuals.
505 procedure Set_Instance_Of (A : Entity_Id; B : Entity_Id);
506 -- Associate analyzed generic parameter with corresponding
507 -- instance. Used for semantic checks at instantiation time.
509 function Has_Been_Exchanged (E : Entity_Id) return Boolean;
510 -- Traverse the Exchanged_Views list to see if a type was private
511 -- and has already been flipped during this phase of instantiation.
513 procedure Hide_Current_Scope;
514 -- When instantiating a generic child unit, the parent context must be
515 -- present, but the instance and all entities that may be generated
516 -- must be inserted in the current scope. We leave the current scope
517 -- on the stack, but make its entities invisible to avoid visibility
518 -- problems. This is reversed at the end of the instantiation. This is
519 -- not done for the instantiation of the bodies, which only require the
520 -- instances of the generic parents to be in scope.
522 procedure Install_Body
527 -- If the instantiation happens textually before the body of the generic,
528 -- the instantiation of the body must be analyzed after the generic body,
529 -- and not at the point of instantiation. Such early instantiations can
530 -- happen if the generic and the instance appear in a package declaration
531 -- because the generic body can only appear in the corresponding package
532 -- body. Early instantiations can also appear if generic, instance and
533 -- body are all in the declarative part of a subprogram or entry. Entities
534 -- of packages that are early instantiations are delayed, and their freeze
535 -- node appears after the generic body.
537 procedure Insert_Freeze_Node_For_Instance
540 -- N denotes a package or a subprogram instantiation and F_Node is the
541 -- associated freeze node. Insert the freeze node before the first source
542 -- body which follows immediately after N. If no such body is found, the
543 -- freeze node is inserted at the end of the declarative region which
546 procedure Freeze_Subprogram_Body
547 (Inst_Node : Node_Id;
549 Pack_Id : Entity_Id);
550 -- The generic body may appear textually after the instance, including
551 -- in the proper body of a stub, or within a different package instance.
552 -- Given that the instance can only be elaborated after the generic, we
553 -- place freeze_nodes for the instance and/or for packages that may enclose
554 -- the instance and the generic, so that the back-end can establish the
555 -- proper order of elaboration.
558 -- Establish environment for subsequent instantiation. Separated from
559 -- Save_Env because data-structures for visibility handling must be
560 -- initialized before call to Check_Generic_Child_Unit.
562 procedure Install_Formal_Packages (Par : Entity_Id);
563 -- Install the visible part of any formal of the parent that is a formal
564 -- package. Note that for the case of a formal package with a box, this
565 -- includes the formal part of the formal package (12.7(10/2)).
567 procedure Install_Parent (P : Entity_Id; In_Body : Boolean := False);
568 -- When compiling an instance of a child unit the parent (which is
569 -- itself an instance) is an enclosing scope that must be made
570 -- immediately visible. This procedure is also used to install the non-
571 -- generic parent of a generic child unit when compiling its body, so
572 -- that full views of types in the parent are made visible.
574 procedure Remove_Parent (In_Body : Boolean := False);
575 -- Reverse effect after instantiation of child is complete
577 procedure Install_Hidden_Primitives
578 (Prims_List : in out Elist_Id;
581 -- Remove suffix 'P' from hidden primitives of Act_T to match the
582 -- visibility of primitives of Gen_T. The list of primitives to which
583 -- the suffix is removed is added to Prims_List to restore them later.
585 procedure Restore_Hidden_Primitives (Prims_List : in out Elist_Id);
586 -- Restore suffix 'P' to primitives of Prims_List and leave Prims_List
589 procedure Inline_Instance_Body
591 Gen_Unit : Entity_Id;
593 -- If front-end inlining is requested, instantiate the package body,
594 -- and preserve the visibility of its compilation unit, to insure
595 -- that successive instantiations succeed.
597 -- The functions Instantiate_XXX perform various legality checks and build
598 -- the declarations for instantiated generic parameters. In all of these
599 -- Formal is the entity in the generic unit, Actual is the entity of
600 -- expression in the generic associations, and Analyzed_Formal is the
601 -- formal in the generic copy, which contains the semantic information to
602 -- be used to validate the actual.
604 function Instantiate_Object
607 Analyzed_Formal : Node_Id) return List_Id;
609 function Instantiate_Type
612 Analyzed_Formal : Node_Id;
613 Actual_Decls : List_Id) return List_Id;
615 function Instantiate_Formal_Subprogram
618 Analyzed_Formal : Node_Id) return Node_Id;
620 function Instantiate_Formal_Package
623 Analyzed_Formal : Node_Id) return List_Id;
624 -- If the formal package is declared with a box, special visibility rules
625 -- apply to its formals: they are in the visible part of the package. This
626 -- is true in the declarative region of the formal package, that is to say
627 -- in the enclosing generic or instantiation. For an instantiation, the
628 -- parameters of the formal package are made visible in an explicit step.
629 -- Furthermore, if the actual has a visible USE clause, these formals must
630 -- be made potentially use-visible as well. On exit from the enclosing
631 -- instantiation, the reverse must be done.
633 -- For a formal package declared without a box, there are conformance rules
634 -- that apply to the actuals in the generic declaration and the actuals of
635 -- the actual package in the enclosing instantiation. The simplest way to
636 -- apply these rules is to repeat the instantiation of the formal package
637 -- in the context of the enclosing instance, and compare the generic
638 -- associations of this instantiation with those of the actual package.
639 -- This internal instantiation only needs to contain the renamings of the
640 -- formals: the visible and private declarations themselves need not be
643 -- In Ada 2005, the formal package may be only partially parameterized.
644 -- In that case the visibility step must make visible those actuals whose
645 -- corresponding formals were given with a box. A final complication
646 -- involves inherited operations from formal derived types, which must
647 -- be visible if the type is.
649 function Is_In_Main_Unit (N : Node_Id) return Boolean;
650 -- Test if given node is in the main unit
652 procedure Load_Parent_Of_Generic
655 Body_Optional : Boolean := False);
656 -- If the generic appears in a separate non-generic library unit, load the
657 -- corresponding body to retrieve the body of the generic. N is the node
658 -- for the generic instantiation, Spec is the generic package declaration.
660 -- Body_Optional is a flag that indicates that the body is being loaded to
661 -- ensure that temporaries are generated consistently when there are other
662 -- instances in the current declarative part that precede the one being
663 -- loaded. In that case a missing body is acceptable.
665 procedure Inherit_Context (Gen_Decl : Node_Id; Inst : Node_Id);
666 -- Add the context clause of the unit containing a generic unit to a
667 -- compilation unit that is, or contains, an instantiation.
669 function Get_Associated_Node (N : Node_Id) return Node_Id;
670 -- In order to propagate semantic information back from the analyzed copy
671 -- to the original generic, we maintain links between selected nodes in the
672 -- generic and their corresponding copies. At the end of generic analysis,
673 -- the routine Save_Global_References traverses the generic tree, examines
674 -- the semantic information, and preserves the links to those nodes that
675 -- contain global information. At instantiation, the information from the
676 -- associated node is placed on the new copy, so that name resolution is
679 -- Three kinds of source nodes have associated nodes:
681 -- a) those that can reference (denote) entities, that is identifiers,
682 -- character literals, expanded_names, operator symbols, operators,
683 -- and attribute reference nodes. These nodes have an Entity field
684 -- and are the set of nodes that are in N_Has_Entity.
686 -- b) aggregates (N_Aggregate and N_Extension_Aggregate)
688 -- c) selected components (N_Selected_Component)
690 -- For the first class, the associated node preserves the entity if it is
691 -- global. If the generic contains nested instantiations, the associated
692 -- node itself has been recopied, and a chain of them must be followed.
694 -- For aggregates, the associated node allows retrieval of the type, which
695 -- may otherwise not appear in the generic. The view of this type may be
696 -- different between generic and instantiation, and the full view can be
697 -- installed before the instantiation is analyzed. For aggregates of type
698 -- extensions, the same view exchange may have to be performed for some of
699 -- the ancestor types, if their view is private at the point of
702 -- Nodes that are selected components in the parse tree may be rewritten
703 -- as expanded names after resolution, and must be treated as potential
704 -- entity holders, which is why they also have an Associated_Node.
706 -- Nodes that do not come from source, such as freeze nodes, do not appear
707 -- in the generic tree, and need not have an associated node.
709 -- The associated node is stored in the Associated_Node field. Note that
710 -- this field overlaps Entity, which is fine, because the whole point is
711 -- that we don't need or want the normal Entity field in this situation.
713 procedure Map_Formal_Package_Entities (Form : Entity_Id; Act : Entity_Id);
714 -- Within the generic part, entities in the formal package are
715 -- visible. To validate subsequent type declarations, indicate
716 -- the correspondence between the entities in the analyzed formal,
717 -- and the entities in the actual package. There are three packages
718 -- involved in the instantiation of a formal package: the parent
719 -- generic P1 which appears in the generic declaration, the fake
720 -- instantiation P2 which appears in the analyzed generic, and whose
721 -- visible entities may be used in subsequent formals, and the actual
722 -- P3 in the instance. To validate subsequent formals, me indicate
723 -- that the entities in P2 are mapped into those of P3. The mapping of
724 -- entities has to be done recursively for nested packages.
726 procedure Move_Freeze_Nodes
730 -- Freeze nodes can be generated in the analysis of a generic unit, but
731 -- will not be seen by the back-end. It is necessary to move those nodes
732 -- to the enclosing scope if they freeze an outer entity. We place them
733 -- at the end of the enclosing generic package, which is semantically
736 procedure Preanalyze_Actuals (N : Node_Id);
737 -- Analyze actuals to perform name resolution. Full resolution is done
738 -- later, when the expected types are known, but names have to be captured
739 -- before installing parents of generics, that are not visible for the
740 -- actuals themselves.
742 function True_Parent (N : Node_Id) return Node_Id;
743 -- For a subunit, return parent of corresponding stub, else return
746 procedure Valid_Default_Attribute (Nam : Entity_Id; Def : Node_Id);
747 -- Verify that an attribute that appears as the default for a formal
748 -- subprogram is a function or procedure with the correct profile.
750 -------------------------------------------
751 -- Data Structures for Generic Renamings --
752 -------------------------------------------
754 -- The map Generic_Renamings associates generic entities with their
755 -- corresponding actuals. Currently used to validate type instances. It
756 -- will eventually be used for all generic parameters to eliminate the
757 -- need for overload resolution in the instance.
759 type Assoc_Ptr is new Int;
761 Assoc_Null : constant Assoc_Ptr := -1;
766 Next_In_HTable : Assoc_Ptr;
769 package Generic_Renamings is new Table.Table
770 (Table_Component_Type => Assoc,
771 Table_Index_Type => Assoc_Ptr,
772 Table_Low_Bound => 0,
774 Table_Increment => 100,
775 Table_Name => "Generic_Renamings");
777 -- Variable to hold enclosing instantiation. When the environment is
778 -- saved for a subprogram inlining, the corresponding Act_Id is empty.
780 Current_Instantiated_Parent : Assoc := (Empty, Empty, Assoc_Null);
782 -- Hash table for associations
784 HTable_Size : constant := 37;
785 type HTable_Range is range 0 .. HTable_Size - 1;
787 procedure Set_Next_Assoc (E : Assoc_Ptr; Next : Assoc_Ptr);
788 function Next_Assoc (E : Assoc_Ptr) return Assoc_Ptr;
789 function Get_Gen_Id (E : Assoc_Ptr) return Entity_Id;
790 function Hash (F : Entity_Id) return HTable_Range;
792 package Generic_Renamings_HTable is new GNAT.HTable.Static_HTable (
793 Header_Num => HTable_Range,
795 Elmt_Ptr => Assoc_Ptr,
796 Null_Ptr => Assoc_Null,
797 Set_Next => Set_Next_Assoc,
800 Get_Key => Get_Gen_Id,
804 Exchanged_Views : Elist_Id;
805 -- This list holds the private views that have been exchanged during
806 -- instantiation to restore the visibility of the generic declaration.
807 -- (see comments above). After instantiation, the current visibility is
808 -- reestablished by means of a traversal of this list.
810 Hidden_Entities : Elist_Id;
811 -- This list holds the entities of the current scope that are removed
812 -- from immediate visibility when instantiating a child unit. Their
813 -- visibility is restored in Remove_Parent.
815 -- Because instantiations can be recursive, the following must be saved
816 -- on entry and restored on exit from an instantiation (spec or body).
817 -- This is done by the two procedures Save_Env and Restore_Env. For
818 -- package and subprogram instantiations (but not for the body instances)
819 -- the action of Save_Env is done in two steps: Init_Env is called before
820 -- Check_Generic_Child_Unit, because setting the parent instances requires
821 -- that the visibility data structures be properly initialized. Once the
822 -- generic is unit is validated, Set_Instance_Env completes Save_Env.
824 Parent_Unit_Visible : Boolean := False;
825 -- Parent_Unit_Visible is used when the generic is a child unit, and
826 -- indicates whether the ultimate parent of the generic is visible in the
827 -- instantiation environment. It is used to reset the visibility of the
828 -- parent at the end of the instantiation (see Remove_Parent).
830 Instance_Parent_Unit : Entity_Id := Empty;
831 -- This records the ultimate parent unit of an instance of a generic
832 -- child unit and is used in conjunction with Parent_Unit_Visible to
833 -- indicate the unit to which the Parent_Unit_Visible flag corresponds.
835 type Instance_Env is record
836 Instantiated_Parent : Assoc;
837 Exchanged_Views : Elist_Id;
838 Hidden_Entities : Elist_Id;
839 Current_Sem_Unit : Unit_Number_Type;
840 Parent_Unit_Visible : Boolean := False;
841 Instance_Parent_Unit : Entity_Id := Empty;
842 Switches : Config_Switches_Type;
845 package Instance_Envs is new Table.Table (
846 Table_Component_Type => Instance_Env,
847 Table_Index_Type => Int,
848 Table_Low_Bound => 0,
850 Table_Increment => 100,
851 Table_Name => "Instance_Envs");
853 procedure Restore_Private_Views
854 (Pack_Id : Entity_Id;
855 Is_Package : Boolean := True);
856 -- Restore the private views of external types, and unmark the generic
857 -- renamings of actuals, so that they become compatible subtypes again.
858 -- For subprograms, Pack_Id is the package constructed to hold the
861 procedure Switch_View (T : Entity_Id);
862 -- Switch the partial and full views of a type and its private
863 -- dependents (i.e. its subtypes and derived types).
865 ------------------------------------
866 -- Structures for Error Reporting --
867 ------------------------------------
869 Instantiation_Node : Node_Id;
870 -- Used by subprograms that validate instantiation of formal parameters
871 -- where there might be no actual on which to place the error message.
872 -- Also used to locate the instantiation node for generic subunits.
874 Instantiation_Error : exception;
875 -- When there is a semantic error in the generic parameter matching,
876 -- there is no point in continuing the instantiation, because the
877 -- number of cascaded errors is unpredictable. This exception aborts
878 -- the instantiation process altogether.
880 S_Adjustment : Sloc_Adjustment;
881 -- Offset created for each node in an instantiation, in order to keep
882 -- track of the source position of the instantiation in each of its nodes.
883 -- A subsequent semantic error or warning on a construct of the instance
884 -- points to both places: the original generic node, and the point of
885 -- instantiation. See Sinput and Sinput.L for additional details.
887 ------------------------------------------------------------
888 -- Data structure for keeping track when inside a Generic --
889 ------------------------------------------------------------
891 -- The following table is used to save values of the Inside_A_Generic
892 -- flag (see spec of Sem) when they are saved by Start_Generic.
894 package Generic_Flags is new Table.Table (
895 Table_Component_Type => Boolean,
896 Table_Index_Type => Int,
897 Table_Low_Bound => 0,
899 Table_Increment => 200,
900 Table_Name => "Generic_Flags");
902 ---------------------------
903 -- Abandon_Instantiation --
904 ---------------------------
906 procedure Abandon_Instantiation (N : Node_Id) is
908 Error_Msg_N ("\instantiation abandoned!", N);
909 raise Instantiation_Error;
910 end Abandon_Instantiation;
912 --------------------------
913 -- Analyze_Associations --
914 --------------------------
916 function Analyze_Associations
919 F_Copy : List_Id) return List_Id
921 Actuals_To_Freeze : constant Elist_Id := New_Elmt_List;
922 Assoc : constant List_Id := New_List;
923 Default_Actuals : constant Elist_Id := New_Elmt_List;
924 Gen_Unit : constant Entity_Id :=
925 Defining_Entity (Parent (F_Copy));
929 Analyzed_Formal : Node_Id;
930 First_Named : Node_Id := Empty;
934 Saved_Formal : Node_Id;
936 Default_Formals : constant List_Id := New_List;
937 -- If an Others_Choice is present, some of the formals may be defaulted.
938 -- To simplify the treatment of visibility in an instance, we introduce
939 -- individual defaults for each such formal. These defaults are
940 -- appended to the list of associations and replace the Others_Choice.
942 Found_Assoc : Node_Id;
943 -- Association for the current formal being match. Empty if there are
944 -- no remaining actuals, or if there is no named association with the
945 -- name of the formal.
947 Is_Named_Assoc : Boolean;
948 Num_Matched : Int := 0;
949 Num_Actuals : Int := 0;
951 Others_Present : Boolean := False;
952 Others_Choice : Node_Id := Empty;
953 -- In Ada 2005, indicates partial parameterization of a formal
954 -- package. As usual an other association must be last in the list.
956 function Build_Wrapper
958 Actual : Entity_Id := Empty) return Node_Id;
959 -- In GNATProve mode, create a wrapper function for actuals that are
960 -- operators, in order to propagate their contract to the renaming
961 -- declarations generated for them. If the actual is absent, this is
962 -- a formal with a default, and the name of the operator is that of the
965 procedure Check_Overloaded_Formal_Subprogram (Formal : Entity_Id);
966 -- Apply RM 12.3 (9): if a formal subprogram is overloaded, the instance
967 -- cannot have a named association for it. AI05-0025 extends this rule
968 -- to formals of formal packages by AI05-0025, and it also applies to
969 -- box-initialized formals.
971 function Has_Fully_Defined_Profile (Subp : Entity_Id) return Boolean;
972 -- Determine whether the parameter types and the return type of Subp
973 -- are fully defined at the point of instantiation.
975 function Matching_Actual
977 A_F : Entity_Id) return Node_Id;
978 -- Find actual that corresponds to a given a formal parameter. If the
979 -- actuals are positional, return the next one, if any. If the actuals
980 -- are named, scan the parameter associations to find the right one.
981 -- A_F is the corresponding entity in the analyzed generic,which is
982 -- placed on the selector name for ASIS use.
984 -- In Ada 2005, a named association may be given with a box, in which
985 -- case Matching_Actual sets Found_Assoc to the generic association,
986 -- but return Empty for the actual itself. In this case the code below
987 -- creates a corresponding declaration for the formal.
989 function Partial_Parameterization return Boolean;
990 -- Ada 2005: if no match is found for a given formal, check if the
991 -- association for it includes a box, or whether the associations
992 -- include an Others clause.
994 procedure Process_Default (F : Entity_Id);
995 -- Add a copy of the declaration of generic formal F to the list of
996 -- associations, and add an explicit box association for F if there
997 -- is none yet, and the default comes from an Others_Choice.
999 function Renames_Standard_Subprogram (Subp : Entity_Id) return Boolean;
1000 -- Determine whether Subp renames one of the subprograms defined in the
1001 -- generated package Standard.
1003 procedure Set_Analyzed_Formal;
1004 -- Find the node in the generic copy that corresponds to a given formal.
1005 -- The semantic information on this node is used to perform legality
1006 -- checks on the actuals. Because semantic analysis can introduce some
1007 -- anonymous entities or modify the declaration node itself, the
1008 -- correspondence between the two lists is not one-one. In addition to
1009 -- anonymous types, the presence a formal equality will introduce an
1010 -- implicit declaration for the corresponding inequality.
1016 function Build_Wrapper
1017 (Formal : Entity_Id;
1018 Actual : Entity_Id := Empty) return Node_Id
1020 Loc : constant Source_Ptr := Sloc (I_Node);
1021 Typ : constant Entity_Id := Etype (Formal);
1022 Is_Binary : constant Boolean :=
1023 Present (Next_Formal (First_Formal (Formal)));
1036 Op_Name := Chars (Formal);
1038 Op_Name := Chars (Actual);
1041 -- Create entities for wrapper function and its formals
1043 F1 := Make_Temporary (Loc, 'A');
1044 F2 := Make_Temporary (Loc, 'B');
1045 L := New_Occurrence_Of (F1, Loc);
1046 R := New_Occurrence_Of (F2, Loc);
1048 Func := Make_Defining_Identifier (Loc, Chars (Formal));
1049 Set_Ekind (Func, E_Function);
1050 Set_Is_Generic_Actual_Subprogram (Func);
1053 Make_Function_Specification (Loc,
1054 Defining_Unit_Name => Func,
1055 Parameter_Specifications => New_List (
1056 Make_Parameter_Specification (Loc,
1057 Defining_Identifier => F1,
1059 Make_Identifier (Loc,
1060 Chars => Chars (Etype (First_Formal (Formal)))))),
1061 Result_Definition => Make_Identifier (Loc, Chars (Typ)));
1064 Append_To (Parameter_Specifications (Spec),
1065 Make_Parameter_Specification (Loc,
1066 Defining_Identifier => F2,
1068 Make_Identifier (Loc,
1069 Chars (Etype (Next_Formal (First_Formal (Formal)))))));
1072 -- Build expression as a function call, or as an operator node
1073 -- that corresponds to the name of the actual, starting with binary
1076 if Present (Actual) and then Op_Name not in Any_Operator_Name then
1078 Make_Function_Call (Loc,
1080 New_Occurrence_Of (Entity (Actual), Loc),
1081 Parameter_Associations => New_List (L));
1084 Append_To (Parameter_Associations (Expr), R);
1089 elsif Is_Binary then
1090 if Op_Name = Name_Op_And then
1091 Expr := Make_Op_And (Loc, Left_Opnd => L, Right_Opnd => R);
1092 elsif Op_Name = Name_Op_Or then
1093 Expr := Make_Op_Or (Loc, Left_Opnd => L, Right_Opnd => R);
1094 elsif Op_Name = Name_Op_Xor then
1095 Expr := Make_Op_Xor (Loc, Left_Opnd => L, Right_Opnd => R);
1096 elsif Op_Name = Name_Op_Eq then
1097 Expr := Make_Op_Eq (Loc, Left_Opnd => L, Right_Opnd => R);
1098 elsif Op_Name = Name_Op_Ne then
1099 Expr := Make_Op_Ne (Loc, Left_Opnd => L, Right_Opnd => R);
1100 elsif Op_Name = Name_Op_Le then
1101 Expr := Make_Op_Le (Loc, Left_Opnd => L, Right_Opnd => R);
1102 elsif Op_Name = Name_Op_Gt then
1103 Expr := Make_Op_Gt (Loc, Left_Opnd => L, Right_Opnd => R);
1104 elsif Op_Name = Name_Op_Ge then
1105 Expr := Make_Op_Ge (Loc, Left_Opnd => L, Right_Opnd => R);
1106 elsif Op_Name = Name_Op_Lt then
1107 Expr := Make_Op_Lt (Loc, Left_Opnd => L, Right_Opnd => R);
1108 elsif Op_Name = Name_Op_Add then
1109 Expr := Make_Op_Add (Loc, Left_Opnd => L, Right_Opnd => R);
1110 elsif Op_Name = Name_Op_Subtract then
1111 Expr := Make_Op_Subtract (Loc, Left_Opnd => L, Right_Opnd => R);
1112 elsif Op_Name = Name_Op_Concat then
1113 Expr := Make_Op_Concat (Loc, Left_Opnd => L, Right_Opnd => R);
1114 elsif Op_Name = Name_Op_Multiply then
1115 Expr := Make_Op_Multiply (Loc, Left_Opnd => L, Right_Opnd => R);
1116 elsif Op_Name = Name_Op_Divide then
1117 Expr := Make_Op_Divide (Loc, Left_Opnd => L, Right_Opnd => R);
1118 elsif Op_Name = Name_Op_Mod then
1119 Expr := Make_Op_Mod (Loc, Left_Opnd => L, Right_Opnd => R);
1120 elsif Op_Name = Name_Op_Rem then
1121 Expr := Make_Op_Rem (Loc, Left_Opnd => L, Right_Opnd => R);
1122 elsif Op_Name = Name_Op_Expon then
1123 Expr := Make_Op_Expon (Loc, Left_Opnd => L, Right_Opnd => R);
1129 if Op_Name = Name_Op_Add then
1130 Expr := Make_Op_Plus (Loc, Right_Opnd => L);
1131 elsif Op_Name = Name_Op_Subtract then
1132 Expr := Make_Op_Minus (Loc, Right_Opnd => L);
1133 elsif Op_Name = Name_Op_Abs then
1134 Expr := Make_Op_Abs (Loc, Right_Opnd => L);
1135 elsif Op_Name = Name_Op_Not then
1136 Expr := Make_Op_Not (Loc, Right_Opnd => L);
1140 -- Propagate visible entity to operator node, either from a
1141 -- given actual or from a default.
1143 if Is_Entity_Name (Actual)
1144 and then Nkind (Expr) in N_Op
1146 Set_Entity (Expr, Entity (Actual));
1150 Make_Expression_Function (Loc,
1151 Specification => Spec,
1152 Expression => Expr);
1157 ----------------------------------------
1158 -- Check_Overloaded_Formal_Subprogram --
1159 ----------------------------------------
1161 procedure Check_Overloaded_Formal_Subprogram (Formal : Entity_Id) is
1162 Temp_Formal : Entity_Id;
1165 Temp_Formal := First (Formals);
1166 while Present (Temp_Formal) loop
1167 if Nkind (Temp_Formal) in N_Formal_Subprogram_Declaration
1168 and then Temp_Formal /= Formal
1170 Chars (Defining_Unit_Name (Specification (Formal))) =
1171 Chars (Defining_Unit_Name (Specification (Temp_Formal)))
1173 if Present (Found_Assoc) then
1175 ("named association not allowed for overloaded formal",
1180 ("named association not allowed for overloaded formal",
1184 Abandon_Instantiation (Instantiation_Node);
1189 end Check_Overloaded_Formal_Subprogram;
1191 -------------------------------
1192 -- Has_Fully_Defined_Profile --
1193 -------------------------------
1195 function Has_Fully_Defined_Profile (Subp : Entity_Id) return Boolean is
1196 function Is_Fully_Defined_Type (Typ : Entity_Id) return Boolean;
1197 -- Determine whethet type Typ is fully defined
1199 ---------------------------
1200 -- Is_Fully_Defined_Type --
1201 ---------------------------
1203 function Is_Fully_Defined_Type (Typ : Entity_Id) return Boolean is
1205 -- A private type without a full view is not fully defined
1207 if Is_Private_Type (Typ)
1208 and then No (Full_View (Typ))
1212 -- An incomplete type is never fully defined
1214 elsif Is_Incomplete_Type (Typ) then
1217 -- All other types are fully defined
1222 end Is_Fully_Defined_Type;
1224 -- Local declarations
1228 -- Start of processing for Has_Fully_Defined_Profile
1231 -- Check the parameters
1233 Param := First_Formal (Subp);
1234 while Present (Param) loop
1235 if not Is_Fully_Defined_Type (Etype (Param)) then
1239 Next_Formal (Param);
1242 -- Check the return type
1244 return Is_Fully_Defined_Type (Etype (Subp));
1245 end Has_Fully_Defined_Profile;
1247 ---------------------
1248 -- Matching_Actual --
1249 ---------------------
1251 function Matching_Actual
1253 A_F : Entity_Id) return Node_Id
1259 Is_Named_Assoc := False;
1261 -- End of list of purely positional parameters
1263 if No (Actual) or else Nkind (Actual) = N_Others_Choice then
1264 Found_Assoc := Empty;
1267 -- Case of positional parameter corresponding to current formal
1269 elsif No (Selector_Name (Actual)) then
1270 Found_Assoc := Actual;
1271 Act := Explicit_Generic_Actual_Parameter (Actual);
1272 Num_Matched := Num_Matched + 1;
1275 -- Otherwise scan list of named actuals to find the one with the
1276 -- desired name. All remaining actuals have explicit names.
1279 Is_Named_Assoc := True;
1280 Found_Assoc := Empty;
1284 while Present (Actual) loop
1285 if Chars (Selector_Name (Actual)) = Chars (F) then
1286 Set_Entity (Selector_Name (Actual), A_F);
1287 Set_Etype (Selector_Name (Actual), Etype (A_F));
1288 Generate_Reference (A_F, Selector_Name (Actual));
1289 Found_Assoc := Actual;
1290 Act := Explicit_Generic_Actual_Parameter (Actual);
1291 Num_Matched := Num_Matched + 1;
1299 -- Reset for subsequent searches. In most cases the named
1300 -- associations are in order. If they are not, we reorder them
1301 -- to avoid scanning twice the same actual. This is not just a
1302 -- question of efficiency: there may be multiple defaults with
1303 -- boxes that have the same name. In a nested instantiation we
1304 -- insert actuals for those defaults, and cannot rely on their
1305 -- names to disambiguate them.
1307 if Actual = First_Named then
1310 elsif Present (Actual) then
1311 Insert_Before (First_Named, Remove_Next (Prev));
1314 Actual := First_Named;
1317 if Is_Entity_Name (Act) and then Present (Entity (Act)) then
1318 Set_Used_As_Generic_Actual (Entity (Act));
1322 end Matching_Actual;
1324 ------------------------------
1325 -- Partial_Parameterization --
1326 ------------------------------
1328 function Partial_Parameterization return Boolean is
1330 return Others_Present
1331 or else (Present (Found_Assoc) and then Box_Present (Found_Assoc));
1332 end Partial_Parameterization;
1334 ---------------------
1335 -- Process_Default --
1336 ---------------------
1338 procedure Process_Default (F : Entity_Id) is
1339 Loc : constant Source_Ptr := Sloc (I_Node);
1340 F_Id : constant Entity_Id := Defining_Entity (F);
1346 -- Append copy of formal declaration to associations, and create new
1347 -- defining identifier for it.
1349 Decl := New_Copy_Tree (F);
1350 Id := Make_Defining_Identifier (Sloc (F_Id), Chars (F_Id));
1352 if Nkind (F) in N_Formal_Subprogram_Declaration then
1353 Set_Defining_Unit_Name (Specification (Decl), Id);
1356 Set_Defining_Identifier (Decl, Id);
1359 Append (Decl, Assoc);
1361 if No (Found_Assoc) then
1363 Make_Generic_Association (Loc,
1364 Selector_Name => New_Occurrence_Of (Id, Loc),
1365 Explicit_Generic_Actual_Parameter => Empty);
1366 Set_Box_Present (Default);
1367 Append (Default, Default_Formals);
1369 end Process_Default;
1371 ---------------------------------
1372 -- Renames_Standard_Subprogram --
1373 ---------------------------------
1375 function Renames_Standard_Subprogram (Subp : Entity_Id) return Boolean is
1380 while Present (Id) loop
1381 if Scope (Id) = Standard_Standard then
1389 end Renames_Standard_Subprogram;
1391 -------------------------
1392 -- Set_Analyzed_Formal --
1393 -------------------------
1395 procedure Set_Analyzed_Formal is
1399 while Present (Analyzed_Formal) loop
1400 Kind := Nkind (Analyzed_Formal);
1402 case Nkind (Formal) is
1404 when N_Formal_Subprogram_Declaration =>
1405 exit when Kind in N_Formal_Subprogram_Declaration
1408 (Defining_Unit_Name (Specification (Formal))) =
1410 (Defining_Unit_Name (Specification (Analyzed_Formal)));
1412 when N_Formal_Package_Declaration =>
1413 exit when Nkind_In (Kind, N_Formal_Package_Declaration,
1414 N_Generic_Package_Declaration,
1415 N_Package_Declaration);
1417 when N_Use_Package_Clause | N_Use_Type_Clause => exit;
1421 -- Skip freeze nodes, and nodes inserted to replace
1422 -- unrecognized pragmas.
1425 Kind not in N_Formal_Subprogram_Declaration
1426 and then not Nkind_In (Kind, N_Subprogram_Declaration,
1430 and then Chars (Defining_Identifier (Formal)) =
1431 Chars (Defining_Identifier (Analyzed_Formal));
1434 Next (Analyzed_Formal);
1436 end Set_Analyzed_Formal;
1438 -- Start of processing for Analyze_Associations
1441 Actuals := Generic_Associations (I_Node);
1443 if Present (Actuals) then
1445 -- Check for an Others choice, indicating a partial parameterization
1446 -- for a formal package.
1448 Actual := First (Actuals);
1449 while Present (Actual) loop
1450 if Nkind (Actual) = N_Others_Choice then
1451 Others_Present := True;
1452 Others_Choice := Actual;
1454 if Present (Next (Actual)) then
1455 Error_Msg_N ("others must be last association", Actual);
1458 -- This subprogram is used both for formal packages and for
1459 -- instantiations. For the latter, associations must all be
1462 if Nkind (I_Node) /= N_Formal_Package_Declaration
1463 and then Comes_From_Source (I_Node)
1466 ("others association not allowed in an instance",
1470 -- In any case, nothing to do after the others association
1474 elsif Box_Present (Actual)
1475 and then Comes_From_Source (I_Node)
1476 and then Nkind (I_Node) /= N_Formal_Package_Declaration
1479 ("box association not allowed in an instance", Actual);
1485 -- If named associations are present, save first named association
1486 -- (it may of course be Empty) to facilitate subsequent name search.
1488 First_Named := First (Actuals);
1489 while Present (First_Named)
1490 and then Nkind (First_Named) /= N_Others_Choice
1491 and then No (Selector_Name (First_Named))
1493 Num_Actuals := Num_Actuals + 1;
1498 Named := First_Named;
1499 while Present (Named) loop
1500 if Nkind (Named) /= N_Others_Choice
1501 and then No (Selector_Name (Named))
1503 Error_Msg_N ("invalid positional actual after named one", Named);
1504 Abandon_Instantiation (Named);
1507 -- A named association may lack an actual parameter, if it was
1508 -- introduced for a default subprogram that turns out to be local
1509 -- to the outer instantiation.
1511 if Nkind (Named) /= N_Others_Choice
1512 and then Present (Explicit_Generic_Actual_Parameter (Named))
1514 Num_Actuals := Num_Actuals + 1;
1520 if Present (Formals) then
1521 Formal := First_Non_Pragma (Formals);
1522 Analyzed_Formal := First_Non_Pragma (F_Copy);
1524 if Present (Actuals) then
1525 Actual := First (Actuals);
1527 -- All formals should have default values
1533 while Present (Formal) loop
1534 Set_Analyzed_Formal;
1535 Saved_Formal := Next_Non_Pragma (Formal);
1537 case Nkind (Formal) is
1538 when N_Formal_Object_Declaration =>
1541 Defining_Identifier (Formal),
1542 Defining_Identifier (Analyzed_Formal));
1544 if No (Match) and then Partial_Parameterization then
1545 Process_Default (Formal);
1548 (Instantiate_Object (Formal, Match, Analyzed_Formal),
1552 when N_Formal_Type_Declaration =>
1555 Defining_Identifier (Formal),
1556 Defining_Identifier (Analyzed_Formal));
1559 if Partial_Parameterization then
1560 Process_Default (Formal);
1563 Error_Msg_Sloc := Sloc (Gen_Unit);
1567 Defining_Identifier (Formal));
1568 Error_Msg_NE ("\in instantiation of & declared#",
1569 Instantiation_Node, Gen_Unit);
1570 Abandon_Instantiation (Instantiation_Node);
1577 (Formal, Match, Analyzed_Formal, Assoc),
1580 -- An instantiation is a freeze point for the actuals,
1581 -- unless this is a rewritten formal package, or the
1582 -- formal is an Ada 2012 formal incomplete type.
1584 if Nkind (I_Node) = N_Formal_Package_Declaration
1586 (Ada_Version >= Ada_2012
1588 Ekind (Defining_Identifier (Analyzed_Formal)) =
1594 Append_Elmt (Entity (Match), Actuals_To_Freeze);
1598 -- A remote access-to-class-wide type is not a legal actual
1599 -- for a generic formal of an access type (E.2.2(17/2)).
1600 -- In GNAT an exception to this rule is introduced when
1601 -- the formal is marked as remote using implementation
1602 -- defined aspect/pragma Remote_Access_Type. In that case
1603 -- the actual must be remote as well.
1605 -- If the current instantiation is the construction of a
1606 -- local copy for a formal package the actuals may be
1607 -- defaulted, and there is no matching actual to check.
1609 if Nkind (Analyzed_Formal) = N_Formal_Type_Declaration
1611 Nkind (Formal_Type_Definition (Analyzed_Formal)) =
1612 N_Access_To_Object_Definition
1613 and then Present (Match)
1616 Formal_Ent : constant Entity_Id :=
1617 Defining_Identifier (Analyzed_Formal);
1619 if Is_Remote_Access_To_Class_Wide_Type (Entity (Match))
1620 = Is_Remote_Types (Formal_Ent)
1622 -- Remoteness of formal and actual match
1626 elsif Is_Remote_Types (Formal_Ent) then
1628 -- Remote formal, non-remote actual
1631 ("actual for& must be remote", Match, Formal_Ent);
1634 -- Non-remote formal, remote actual
1637 ("actual for& may not be remote",
1643 when N_Formal_Subprogram_Declaration =>
1646 (Defining_Unit_Name (Specification (Formal)),
1647 Defining_Unit_Name (Specification (Analyzed_Formal)));
1649 -- If the formal subprogram has the same name as another
1650 -- formal subprogram of the generic, then a named
1651 -- association is illegal (12.3(9)). Exclude named
1652 -- associations that are generated for a nested instance.
1655 and then Is_Named_Assoc
1656 and then Comes_From_Source (Found_Assoc)
1658 Check_Overloaded_Formal_Subprogram (Formal);
1661 -- If there is no corresponding actual, this may be case
1662 -- of partial parameterization, or else the formal has a
1663 -- default or a box.
1665 if No (Match) and then Partial_Parameterization then
1666 Process_Default (Formal);
1668 if Nkind (I_Node) = N_Formal_Package_Declaration then
1669 Check_Overloaded_Formal_Subprogram (Formal);
1674 and then Ekind (Defining_Entity (Analyzed_Formal))
1678 -- If actual is an entity (function or operator),
1679 -- build wrapper for it.
1682 and then Nkind (Match) = N_Operator_Symbol
1685 -- If the name is a default, find its visible
1686 -- entity at the point of instantiation.
1688 if Is_Entity_Name (Match)
1689 and then No (Entity (Match))
1691 Find_Direct_Name (Match);
1696 (Defining_Entity (Analyzed_Formal), Match));
1698 -- Ditto if formal is an operator with a default.
1700 elsif Box_Present (Formal)
1701 and then Nkind (Defining_Entity (Analyzed_Formal))
1702 = N_Defining_Operator_Symbol
1706 (Defining_Entity (Analyzed_Formal)));
1708 -- Otherwise create renaming declaration.
1712 Instantiate_Formal_Subprogram
1713 (Formal, Match, Analyzed_Formal));
1718 Instantiate_Formal_Subprogram
1719 (Formal, Match, Analyzed_Formal));
1722 -- An instantiation is a freeze point for the actuals,
1723 -- unless this is a rewritten formal package.
1725 if Nkind (I_Node) /= N_Formal_Package_Declaration
1726 and then Nkind (Match) = N_Identifier
1727 and then Is_Subprogram (Entity (Match))
1729 -- The actual subprogram may rename a routine defined
1730 -- in Standard. Avoid freezing such renamings because
1731 -- subprograms coming from Standard cannot be frozen.
1734 not Renames_Standard_Subprogram (Entity (Match))
1736 -- If the actual subprogram comes from a different
1737 -- unit, it is already frozen, either by a body in
1738 -- that unit or by the end of the declarative part
1739 -- of the unit. This check avoids the freezing of
1740 -- subprograms defined in Standard which are used
1741 -- as generic actuals.
1743 and then In_Same_Code_Unit (Entity (Match), I_Node)
1744 and then Has_Fully_Defined_Profile (Entity (Match))
1746 -- Mark the subprogram as having a delayed freeze
1747 -- since this may be an out-of-order action.
1749 Set_Has_Delayed_Freeze (Entity (Match));
1750 Append_Elmt (Entity (Match), Actuals_To_Freeze);
1754 -- If this is a nested generic, preserve default for later
1758 and then Box_Present (Formal)
1761 (Defining_Unit_Name (Specification (Last (Assoc))),
1765 when N_Formal_Package_Declaration =>
1768 Defining_Identifier (Formal),
1769 Defining_Identifier (Original_Node (Analyzed_Formal)));
1772 if Partial_Parameterization then
1773 Process_Default (Formal);
1776 Error_Msg_Sloc := Sloc (Gen_Unit);
1779 Instantiation_Node, Defining_Identifier (Formal));
1780 Error_Msg_NE ("\in instantiation of & declared#",
1781 Instantiation_Node, Gen_Unit);
1783 Abandon_Instantiation (Instantiation_Node);
1789 (Instantiate_Formal_Package
1790 (Formal, Match, Analyzed_Formal),
1794 -- For use type and use package appearing in the generic part,
1795 -- we have already copied them, so we can just move them where
1796 -- they belong (we mustn't recopy them since this would mess up
1797 -- the Sloc values).
1799 when N_Use_Package_Clause |
1800 N_Use_Type_Clause =>
1801 if Nkind (Original_Node (I_Node)) =
1802 N_Formal_Package_Declaration
1804 Append (New_Copy_Tree (Formal), Assoc);
1807 Append (Formal, Assoc);
1811 raise Program_Error;
1815 Formal := Saved_Formal;
1816 Next_Non_Pragma (Analyzed_Formal);
1819 if Num_Actuals > Num_Matched then
1820 Error_Msg_Sloc := Sloc (Gen_Unit);
1822 if Present (Selector_Name (Actual)) then
1824 ("unmatched actual&",
1825 Actual, Selector_Name (Actual));
1826 Error_Msg_NE ("\in instantiation of& declared#",
1830 ("unmatched actual in instantiation of& declared#",
1835 elsif Present (Actuals) then
1837 ("too many actuals in generic instantiation", Instantiation_Node);
1840 -- An instantiation freezes all generic actuals. The only exceptions
1841 -- to this are incomplete types and subprograms which are not fully
1842 -- defined at the point of instantiation.
1845 Elmt : Elmt_Id := First_Elmt (Actuals_To_Freeze);
1847 while Present (Elmt) loop
1848 Freeze_Before (I_Node, Node (Elmt));
1853 -- If there are default subprograms, normalize the tree by adding
1854 -- explicit associations for them. This is required if the instance
1855 -- appears within a generic.
1863 Elmt := First_Elmt (Default_Actuals);
1864 while Present (Elmt) loop
1865 if No (Actuals) then
1866 Actuals := New_List;
1867 Set_Generic_Associations (I_Node, Actuals);
1870 Subp := Node (Elmt);
1872 Make_Generic_Association (Sloc (Subp),
1873 Selector_Name => New_Occurrence_Of (Subp, Sloc (Subp)),
1874 Explicit_Generic_Actual_Parameter =>
1875 New_Occurrence_Of (Subp, Sloc (Subp)));
1876 Mark_Rewrite_Insertion (New_D);
1877 Append_To (Actuals, New_D);
1882 -- If this is a formal package, normalize the parameter list by adding
1883 -- explicit box associations for the formals that are covered by an
1886 if not Is_Empty_List (Default_Formals) then
1887 Append_List (Default_Formals, Formals);
1891 end Analyze_Associations;
1893 -------------------------------
1894 -- Analyze_Formal_Array_Type --
1895 -------------------------------
1897 procedure Analyze_Formal_Array_Type
1898 (T : in out Entity_Id;
1904 -- Treated like a non-generic array declaration, with additional
1909 if Nkind (Def) = N_Constrained_Array_Definition then
1910 DSS := First (Discrete_Subtype_Definitions (Def));
1911 while Present (DSS) loop
1912 if Nkind_In (DSS, N_Subtype_Indication,
1914 N_Attribute_Reference)
1916 Error_Msg_N ("only a subtype mark is allowed in a formal", DSS);
1923 Array_Type_Declaration (T, Def);
1924 Set_Is_Generic_Type (Base_Type (T));
1926 if Ekind (Component_Type (T)) = E_Incomplete_Type
1927 and then No (Full_View (Component_Type (T)))
1929 Error_Msg_N ("premature usage of incomplete type", Def);
1931 -- Check that range constraint is not allowed on the component type
1932 -- of a generic formal array type (AARM 12.5.3(3))
1934 elsif Is_Internal (Component_Type (T))
1935 and then Present (Subtype_Indication (Component_Definition (Def)))
1936 and then Nkind (Original_Node
1937 (Subtype_Indication (Component_Definition (Def)))) =
1938 N_Subtype_Indication
1941 ("in a formal, a subtype indication can only be "
1942 & "a subtype mark (RM 12.5.3(3))",
1943 Subtype_Indication (Component_Definition (Def)));
1946 end Analyze_Formal_Array_Type;
1948 ---------------------------------------------
1949 -- Analyze_Formal_Decimal_Fixed_Point_Type --
1950 ---------------------------------------------
1952 -- As for other generic types, we create a valid type representation with
1953 -- legal but arbitrary attributes, whose values are never considered
1954 -- static. For all scalar types we introduce an anonymous base type, with
1955 -- the same attributes. We choose the corresponding integer type to be
1956 -- Standard_Integer.
1957 -- Here and in other similar routines, the Sloc of the generated internal
1958 -- type must be the same as the sloc of the defining identifier of the
1959 -- formal type declaration, to provide proper source navigation.
1961 procedure Analyze_Formal_Decimal_Fixed_Point_Type
1965 Loc : constant Source_Ptr := Sloc (Def);
1967 Base : constant Entity_Id :=
1969 (E_Decimal_Fixed_Point_Type,
1971 Sloc (Defining_Identifier (Parent (Def))), 'G');
1973 Int_Base : constant Entity_Id := Standard_Integer;
1974 Delta_Val : constant Ureal := Ureal_1;
1975 Digs_Val : constant Uint := Uint_6;
1977 function Make_Dummy_Bound return Node_Id;
1978 -- Return a properly typed universal real literal to use as a bound
1980 ----------------------
1981 -- Make_Dummy_Bound --
1982 ----------------------
1984 function Make_Dummy_Bound return Node_Id is
1985 Bound : constant Node_Id := Make_Real_Literal (Loc, Ureal_1);
1987 Set_Etype (Bound, Universal_Real);
1989 end Make_Dummy_Bound;
1991 -- Start of processing for Analyze_Formal_Decimal_Fixed_Point_Type
1996 Set_Etype (Base, Base);
1997 Set_Size_Info (Base, Int_Base);
1998 Set_RM_Size (Base, RM_Size (Int_Base));
1999 Set_First_Rep_Item (Base, First_Rep_Item (Int_Base));
2000 Set_Digits_Value (Base, Digs_Val);
2001 Set_Delta_Value (Base, Delta_Val);
2002 Set_Small_Value (Base, Delta_Val);
2003 Set_Scalar_Range (Base,
2005 Low_Bound => Make_Dummy_Bound,
2006 High_Bound => Make_Dummy_Bound));
2008 Set_Is_Generic_Type (Base);
2009 Set_Parent (Base, Parent (Def));
2011 Set_Ekind (T, E_Decimal_Fixed_Point_Subtype);
2012 Set_Etype (T, Base);
2013 Set_Size_Info (T, Int_Base);
2014 Set_RM_Size (T, RM_Size (Int_Base));
2015 Set_First_Rep_Item (T, First_Rep_Item (Int_Base));
2016 Set_Digits_Value (T, Digs_Val);
2017 Set_Delta_Value (T, Delta_Val);
2018 Set_Small_Value (T, Delta_Val);
2019 Set_Scalar_Range (T, Scalar_Range (Base));
2020 Set_Is_Constrained (T);
2022 Check_Restriction (No_Fixed_Point, Def);
2023 end Analyze_Formal_Decimal_Fixed_Point_Type;
2025 -------------------------------------------
2026 -- Analyze_Formal_Derived_Interface_Type --
2027 -------------------------------------------
2029 procedure Analyze_Formal_Derived_Interface_Type
2034 Loc : constant Source_Ptr := Sloc (Def);
2037 -- Rewrite as a type declaration of a derived type. This ensures that
2038 -- the interface list and primitive operations are properly captured.
2041 Make_Full_Type_Declaration (Loc,
2042 Defining_Identifier => T,
2043 Type_Definition => Def));
2045 Set_Is_Generic_Type (T);
2046 end Analyze_Formal_Derived_Interface_Type;
2048 ---------------------------------
2049 -- Analyze_Formal_Derived_Type --
2050 ---------------------------------
2052 procedure Analyze_Formal_Derived_Type
2057 Loc : constant Source_Ptr := Sloc (Def);
2058 Unk_Disc : constant Boolean := Unknown_Discriminants_Present (N);
2062 Set_Is_Generic_Type (T);
2064 if Private_Present (Def) then
2066 Make_Private_Extension_Declaration (Loc,
2067 Defining_Identifier => T,
2068 Discriminant_Specifications => Discriminant_Specifications (N),
2069 Unknown_Discriminants_Present => Unk_Disc,
2070 Subtype_Indication => Subtype_Mark (Def),
2071 Interface_List => Interface_List (Def));
2073 Set_Abstract_Present (New_N, Abstract_Present (Def));
2074 Set_Limited_Present (New_N, Limited_Present (Def));
2075 Set_Synchronized_Present (New_N, Synchronized_Present (Def));
2079 Make_Full_Type_Declaration (Loc,
2080 Defining_Identifier => T,
2081 Discriminant_Specifications =>
2082 Discriminant_Specifications (Parent (T)),
2084 Make_Derived_Type_Definition (Loc,
2085 Subtype_Indication => Subtype_Mark (Def)));
2087 Set_Abstract_Present
2088 (Type_Definition (New_N), Abstract_Present (Def));
2090 (Type_Definition (New_N), Limited_Present (Def));
2097 if not Is_Composite_Type (T) then
2099 ("unknown discriminants not allowed for elementary types", N);
2101 Set_Has_Unknown_Discriminants (T);
2102 Set_Is_Constrained (T, False);
2106 -- If the parent type has a known size, so does the formal, which makes
2107 -- legal representation clauses that involve the formal.
2109 Set_Size_Known_At_Compile_Time
2110 (T, Size_Known_At_Compile_Time (Entity (Subtype_Mark (Def))));
2111 end Analyze_Formal_Derived_Type;
2113 ----------------------------------
2114 -- Analyze_Formal_Discrete_Type --
2115 ----------------------------------
2117 -- The operations defined for a discrete types are those of an enumeration
2118 -- type. The size is set to an arbitrary value, for use in analyzing the
2121 procedure Analyze_Formal_Discrete_Type (T : Entity_Id; Def : Node_Id) is
2122 Loc : constant Source_Ptr := Sloc (Def);
2126 Base : constant Entity_Id :=
2128 (E_Floating_Point_Type, Current_Scope,
2129 Sloc (Defining_Identifier (Parent (Def))), 'G');
2133 Set_Ekind (T, E_Enumeration_Subtype);
2134 Set_Etype (T, Base);
2137 Set_Is_Generic_Type (T);
2138 Set_Is_Constrained (T);
2140 -- For semantic analysis, the bounds of the type must be set to some
2141 -- non-static value. The simplest is to create attribute nodes for those
2142 -- bounds, that refer to the type itself. These bounds are never
2143 -- analyzed but serve as place-holders.
2146 Make_Attribute_Reference (Loc,
2147 Attribute_Name => Name_First,
2148 Prefix => New_Occurrence_Of (T, Loc));
2152 Make_Attribute_Reference (Loc,
2153 Attribute_Name => Name_Last,
2154 Prefix => New_Occurrence_Of (T, Loc));
2157 Set_Scalar_Range (T,
2162 Set_Ekind (Base, E_Enumeration_Type);
2163 Set_Etype (Base, Base);
2164 Init_Size (Base, 8);
2165 Init_Alignment (Base);
2166 Set_Is_Generic_Type (Base);
2167 Set_Scalar_Range (Base, Scalar_Range (T));
2168 Set_Parent (Base, Parent (Def));
2169 end Analyze_Formal_Discrete_Type;
2171 ----------------------------------
2172 -- Analyze_Formal_Floating_Type --
2173 ---------------------------------
2175 procedure Analyze_Formal_Floating_Type (T : Entity_Id; Def : Node_Id) is
2176 Base : constant Entity_Id :=
2178 (E_Floating_Point_Type, Current_Scope,
2179 Sloc (Defining_Identifier (Parent (Def))), 'G');
2182 -- The various semantic attributes are taken from the predefined type
2183 -- Float, just so that all of them are initialized. Their values are
2184 -- never used because no constant folding or expansion takes place in
2185 -- the generic itself.
2188 Set_Ekind (T, E_Floating_Point_Subtype);
2189 Set_Etype (T, Base);
2190 Set_Size_Info (T, (Standard_Float));
2191 Set_RM_Size (T, RM_Size (Standard_Float));
2192 Set_Digits_Value (T, Digits_Value (Standard_Float));
2193 Set_Scalar_Range (T, Scalar_Range (Standard_Float));
2194 Set_Is_Constrained (T);
2196 Set_Is_Generic_Type (Base);
2197 Set_Etype (Base, Base);
2198 Set_Size_Info (Base, (Standard_Float));
2199 Set_RM_Size (Base, RM_Size (Standard_Float));
2200 Set_Digits_Value (Base, Digits_Value (Standard_Float));
2201 Set_Scalar_Range (Base, Scalar_Range (Standard_Float));
2202 Set_Parent (Base, Parent (Def));
2204 Check_Restriction (No_Floating_Point, Def);
2205 end Analyze_Formal_Floating_Type;
2207 -----------------------------------
2208 -- Analyze_Formal_Interface_Type;--
2209 -----------------------------------
2211 procedure Analyze_Formal_Interface_Type
2216 Loc : constant Source_Ptr := Sloc (N);
2221 Make_Full_Type_Declaration (Loc,
2222 Defining_Identifier => T,
2223 Type_Definition => Def);
2227 Set_Is_Generic_Type (T);
2228 end Analyze_Formal_Interface_Type;
2230 ---------------------------------
2231 -- Analyze_Formal_Modular_Type --
2232 ---------------------------------
2234 procedure Analyze_Formal_Modular_Type (T : Entity_Id; Def : Node_Id) is
2236 -- Apart from their entity kind, generic modular types are treated like
2237 -- signed integer types, and have the same attributes.
2239 Analyze_Formal_Signed_Integer_Type (T, Def);
2240 Set_Ekind (T, E_Modular_Integer_Subtype);
2241 Set_Ekind (Etype (T), E_Modular_Integer_Type);
2243 end Analyze_Formal_Modular_Type;
2245 ---------------------------------------
2246 -- Analyze_Formal_Object_Declaration --
2247 ---------------------------------------
2249 procedure Analyze_Formal_Object_Declaration (N : Node_Id) is
2250 E : constant Node_Id := Default_Expression (N);
2251 Id : constant Node_Id := Defining_Identifier (N);
2258 -- Determine the mode of the formal object
2260 if Out_Present (N) then
2261 K := E_Generic_In_Out_Parameter;
2263 if not In_Present (N) then
2264 Error_Msg_N ("formal generic objects cannot have mode OUT", N);
2268 K := E_Generic_In_Parameter;
2271 if Present (Subtype_Mark (N)) then
2272 Find_Type (Subtype_Mark (N));
2273 T := Entity (Subtype_Mark (N));
2275 -- Verify that there is no redundant null exclusion
2277 if Null_Exclusion_Present (N) then
2278 if not Is_Access_Type (T) then
2280 ("null exclusion can only apply to an access type", N);
2282 elsif Can_Never_Be_Null (T) then
2284 ("`NOT NULL` not allowed (& already excludes null)",
2289 -- Ada 2005 (AI-423): Formal object with an access definition
2292 Check_Access_Definition (N);
2293 T := Access_Definition
2295 N => Access_Definition (N));
2298 if Ekind (T) = E_Incomplete_Type then
2300 Error_Node : Node_Id;
2303 if Present (Subtype_Mark (N)) then
2304 Error_Node := Subtype_Mark (N);
2306 Check_Access_Definition (N);
2307 Error_Node := Access_Definition (N);
2310 Error_Msg_N ("premature usage of incomplete type", Error_Node);
2314 if K = E_Generic_In_Parameter then
2316 -- Ada 2005 (AI-287): Limited aggregates allowed in generic formals
2318 if Ada_Version < Ada_2005 and then Is_Limited_Type (T) then
2320 ("generic formal of mode IN must not be of limited type", N);
2321 Explain_Limited_Type (T, N);
2324 if Is_Abstract_Type (T) then
2326 ("generic formal of mode IN must not be of abstract type", N);
2330 Preanalyze_Spec_Expression (E, T);
2332 if Is_Limited_Type (T) and then not OK_For_Limited_Init (T, E) then
2334 ("initialization not allowed for limited types", E);
2335 Explain_Limited_Type (T, E);
2342 -- Case of generic IN OUT parameter
2345 -- If the formal has an unconstrained type, construct its actual
2346 -- subtype, as is done for subprogram formals. In this fashion, all
2347 -- its uses can refer to specific bounds.
2352 if (Is_Array_Type (T)
2353 and then not Is_Constrained (T))
2355 (Ekind (T) = E_Record_Type
2356 and then Has_Discriminants (T))
2359 Non_Freezing_Ref : constant Node_Id :=
2360 New_Occurrence_Of (Id, Sloc (Id));
2364 -- Make sure the actual subtype doesn't generate bogus freezing
2366 Set_Must_Not_Freeze (Non_Freezing_Ref);
2367 Decl := Build_Actual_Subtype (T, Non_Freezing_Ref);
2368 Insert_Before_And_Analyze (N, Decl);
2369 Set_Actual_Subtype (Id, Defining_Identifier (Decl));
2372 Set_Actual_Subtype (Id, T);
2377 ("initialization not allowed for `IN OUT` formals", N);
2381 if Has_Aspects (N) then
2382 Analyze_Aspect_Specifications (N, Id);
2384 end Analyze_Formal_Object_Declaration;
2386 ----------------------------------------------
2387 -- Analyze_Formal_Ordinary_Fixed_Point_Type --
2388 ----------------------------------------------
2390 procedure Analyze_Formal_Ordinary_Fixed_Point_Type
2394 Loc : constant Source_Ptr := Sloc (Def);
2395 Base : constant Entity_Id :=
2397 (E_Ordinary_Fixed_Point_Type, Current_Scope,
2398 Sloc (Defining_Identifier (Parent (Def))), 'G');
2401 -- The semantic attributes are set for completeness only, their values
2402 -- will never be used, since all properties of the type are non-static.
2405 Set_Ekind (T, E_Ordinary_Fixed_Point_Subtype);
2406 Set_Etype (T, Base);
2407 Set_Size_Info (T, Standard_Integer);
2408 Set_RM_Size (T, RM_Size (Standard_Integer));
2409 Set_Small_Value (T, Ureal_1);
2410 Set_Delta_Value (T, Ureal_1);
2411 Set_Scalar_Range (T,
2413 Low_Bound => Make_Real_Literal (Loc, Ureal_1),
2414 High_Bound => Make_Real_Literal (Loc, Ureal_1)));
2415 Set_Is_Constrained (T);
2417 Set_Is_Generic_Type (Base);
2418 Set_Etype (Base, Base);
2419 Set_Size_Info (Base, Standard_Integer);
2420 Set_RM_Size (Base, RM_Size (Standard_Integer));
2421 Set_Small_Value (Base, Ureal_1);
2422 Set_Delta_Value (Base, Ureal_1);
2423 Set_Scalar_Range (Base, Scalar_Range (T));
2424 Set_Parent (Base, Parent (Def));
2426 Check_Restriction (No_Fixed_Point, Def);
2427 end Analyze_Formal_Ordinary_Fixed_Point_Type;
2429 ----------------------------------------
2430 -- Analyze_Formal_Package_Declaration --
2431 ----------------------------------------
2433 procedure Analyze_Formal_Package_Declaration (N : Node_Id) is
2434 Loc : constant Source_Ptr := Sloc (N);
2435 Pack_Id : constant Entity_Id := Defining_Identifier (N);
2437 Gen_Id : constant Node_Id := Name (N);
2439 Gen_Unit : Entity_Id;
2441 Parent_Installed : Boolean := False;
2443 Parent_Instance : Entity_Id;
2444 Renaming_In_Par : Entity_Id;
2445 Associations : Boolean := True;
2447 Vis_Prims_List : Elist_Id := No_Elist;
2448 -- List of primitives made temporarily visible in the instantiation
2449 -- to match the visibility of the formal type
2451 function Build_Local_Package return Node_Id;
2452 -- The formal package is rewritten so that its parameters are replaced
2453 -- with corresponding declarations. For parameters with bona fide
2454 -- associations these declarations are created by Analyze_Associations
2455 -- as for a regular instantiation. For boxed parameters, we preserve
2456 -- the formal declarations and analyze them, in order to introduce
2457 -- entities of the right kind in the environment of the formal.
2459 -------------------------
2460 -- Build_Local_Package --
2461 -------------------------
2463 function Build_Local_Package return Node_Id is
2465 Pack_Decl : Node_Id;
2468 -- Within the formal, the name of the generic package is a renaming
2469 -- of the formal (as for a regular instantiation).
2472 Make_Package_Declaration (Loc,
2475 (Specification (Original_Node (Gen_Decl)),
2476 Empty, Instantiating => True));
2478 Renaming := Make_Package_Renaming_Declaration (Loc,
2479 Defining_Unit_Name =>
2480 Make_Defining_Identifier (Loc, Chars (Gen_Unit)),
2481 Name => New_Occurrence_Of (Formal, Loc));
2483 if Nkind (Gen_Id) = N_Identifier
2484 and then Chars (Gen_Id) = Chars (Pack_Id)
2487 ("& is hidden within declaration of instance", Gen_Id, Gen_Unit);
2490 -- If the formal is declared with a box, or with an others choice,
2491 -- create corresponding declarations for all entities in the formal
2492 -- part, so that names with the proper types are available in the
2493 -- specification of the formal package.
2495 -- On the other hand, if there are no associations, then all the
2496 -- formals must have defaults, and this will be checked by the
2497 -- call to Analyze_Associations.
2500 or else Nkind (First (Generic_Associations (N))) = N_Others_Choice
2503 Formal_Decl : Node_Id;
2506 -- TBA : for a formal package, need to recurse ???
2511 (Generic_Formal_Declarations (Original_Node (Gen_Decl)));
2512 while Present (Formal_Decl) loop
2514 (Decls, Copy_Generic_Node (Formal_Decl, Empty, True));
2519 -- If generic associations are present, use Analyze_Associations to
2520 -- create the proper renaming declarations.
2524 Act_Tree : constant Node_Id :=
2526 (Original_Node (Gen_Decl), Empty,
2527 Instantiating => True);
2530 Generic_Renamings.Set_Last (0);
2531 Generic_Renamings_HTable.Reset;
2532 Instantiation_Node := N;
2535 Analyze_Associations
2536 (I_Node => Original_Node (N),
2537 Formals => Generic_Formal_Declarations (Act_Tree),
2538 F_Copy => Generic_Formal_Declarations (Gen_Decl));
2540 Vis_Prims_List := Check_Hidden_Primitives (Decls);
2544 Append (Renaming, To => Decls);
2546 -- Add generated declarations ahead of local declarations in
2549 if No (Visible_Declarations (Specification (Pack_Decl))) then
2550 Set_Visible_Declarations (Specification (Pack_Decl), Decls);
2553 (First (Visible_Declarations (Specification (Pack_Decl))),
2558 end Build_Local_Package;
2560 -- Start of processing for Analyze_Formal_Package_Declaration
2563 Check_Text_IO_Special_Unit (Gen_Id);
2566 Check_Generic_Child_Unit (Gen_Id, Parent_Installed);
2567 Gen_Unit := Entity (Gen_Id);
2569 -- Check for a formal package that is a package renaming
2571 if Present (Renamed_Object (Gen_Unit)) then
2573 -- Indicate that unit is used, before replacing it with renamed
2574 -- entity for use below.
2576 if In_Extended_Main_Source_Unit (N) then
2577 Set_Is_Instantiated (Gen_Unit);
2578 Generate_Reference (Gen_Unit, N);
2581 Gen_Unit := Renamed_Object (Gen_Unit);
2584 if Ekind (Gen_Unit) /= E_Generic_Package then
2585 Error_Msg_N ("expect generic package name", Gen_Id);
2589 elsif Gen_Unit = Current_Scope then
2591 ("generic package cannot be used as a formal package of itself",
2596 elsif In_Open_Scopes (Gen_Unit) then
2597 if Is_Compilation_Unit (Gen_Unit)
2598 and then Is_Child_Unit (Current_Scope)
2600 -- Special-case the error when the formal is a parent, and
2601 -- continue analysis to minimize cascaded errors.
2604 ("generic parent cannot be used as formal package "
2605 & "of a child unit",
2610 ("generic package cannot be used as a formal package "
2618 -- Check that name of formal package does not hide name of generic,
2619 -- or its leading prefix. This check must be done separately because
2620 -- the name of the generic has already been analyzed.
2623 Gen_Name : Entity_Id;
2627 while Nkind (Gen_Name) = N_Expanded_Name loop
2628 Gen_Name := Prefix (Gen_Name);
2631 if Chars (Gen_Name) = Chars (Pack_Id) then
2633 ("& is hidden within declaration of formal package",
2639 or else No (Generic_Associations (N))
2640 or else Nkind (First (Generic_Associations (N))) = N_Others_Choice
2642 Associations := False;
2645 -- If there are no generic associations, the generic parameters appear
2646 -- as local entities and are instantiated like them. We copy the generic
2647 -- package declaration as if it were an instantiation, and analyze it
2648 -- like a regular package, except that we treat the formals as
2649 -- additional visible components.
2651 Gen_Decl := Unit_Declaration_Node (Gen_Unit);
2653 if In_Extended_Main_Source_Unit (N) then
2654 Set_Is_Instantiated (Gen_Unit);
2655 Generate_Reference (Gen_Unit, N);
2658 Formal := New_Copy (Pack_Id);
2659 Create_Instantiation_Source (N, Gen_Unit, False, S_Adjustment);
2662 -- Make local generic without formals. The formals will be replaced
2663 -- with internal declarations.
2665 New_N := Build_Local_Package;
2667 -- If there are errors in the parameter list, Analyze_Associations
2668 -- raises Instantiation_Error. Patch the declaration to prevent
2669 -- further exception propagation.
2672 when Instantiation_Error =>
2674 Enter_Name (Formal);
2675 Set_Ekind (Formal, E_Variable);
2676 Set_Etype (Formal, Any_Type);
2677 Restore_Hidden_Primitives (Vis_Prims_List);
2679 if Parent_Installed then
2687 Set_Defining_Unit_Name (Specification (New_N), Formal);
2688 Set_Generic_Parent (Specification (N), Gen_Unit);
2689 Set_Instance_Env (Gen_Unit, Formal);
2690 Set_Is_Generic_Instance (Formal);
2692 Enter_Name (Formal);
2693 Set_Ekind (Formal, E_Package);
2694 Set_Etype (Formal, Standard_Void_Type);
2695 Set_Inner_Instances (Formal, New_Elmt_List);
2696 Push_Scope (Formal);
2698 if Is_Child_Unit (Gen_Unit)
2699 and then Parent_Installed
2701 -- Similarly, we have to make the name of the formal visible in the
2702 -- parent instance, to resolve properly fully qualified names that
2703 -- may appear in the generic unit. The parent instance has been
2704 -- placed on the scope stack ahead of the current scope.
2706 Parent_Instance := Scope_Stack.Table (Scope_Stack.Last - 1).Entity;
2709 Make_Defining_Identifier (Loc, Chars (Gen_Unit));
2710 Set_Ekind (Renaming_In_Par, E_Package);
2711 Set_Etype (Renaming_In_Par, Standard_Void_Type);
2712 Set_Scope (Renaming_In_Par, Parent_Instance);
2713 Set_Parent (Renaming_In_Par, Parent (Formal));
2714 Set_Renamed_Object (Renaming_In_Par, Formal);
2715 Append_Entity (Renaming_In_Par, Parent_Instance);
2718 Analyze (Specification (N));
2720 -- The formals for which associations are provided are not visible
2721 -- outside of the formal package. The others are still declared by a
2722 -- formal parameter declaration.
2724 -- If there are no associations, the only local entity to hide is the
2725 -- generated package renaming itself.
2731 E := First_Entity (Formal);
2732 while Present (E) loop
2734 and then not Is_Generic_Formal (E)
2739 if Ekind (E) = E_Package
2740 and then Renamed_Entity (E) = Formal
2750 End_Package_Scope (Formal);
2751 Restore_Hidden_Primitives (Vis_Prims_List);
2753 if Parent_Installed then
2759 -- Inside the generic unit, the formal package is a regular package, but
2760 -- no body is needed for it. Note that after instantiation, the defining
2761 -- unit name we need is in the new tree and not in the original (see
2762 -- Package_Instantiation). A generic formal package is an instance, and
2763 -- can be used as an actual for an inner instance.
2765 Set_Has_Completion (Formal, True);
2767 -- Add semantic information to the original defining identifier.
2770 Set_Ekind (Pack_Id, E_Package);
2771 Set_Etype (Pack_Id, Standard_Void_Type);
2772 Set_Scope (Pack_Id, Scope (Formal));
2773 Set_Has_Completion (Pack_Id, True);
2776 if Has_Aspects (N) then
2777 Analyze_Aspect_Specifications (N, Pack_Id);
2779 end Analyze_Formal_Package_Declaration;
2781 ---------------------------------
2782 -- Analyze_Formal_Private_Type --
2783 ---------------------------------
2785 procedure Analyze_Formal_Private_Type
2791 New_Private_Type (N, T, Def);
2793 -- Set the size to an arbitrary but legal value
2795 Set_Size_Info (T, Standard_Integer);
2796 Set_RM_Size (T, RM_Size (Standard_Integer));
2797 end Analyze_Formal_Private_Type;
2799 ------------------------------------
2800 -- Analyze_Formal_Incomplete_Type --
2801 ------------------------------------
2803 procedure Analyze_Formal_Incomplete_Type
2809 Set_Ekind (T, E_Incomplete_Type);
2811 Set_Private_Dependents (T, New_Elmt_List);
2813 if Tagged_Present (Def) then
2814 Set_Is_Tagged_Type (T);
2815 Make_Class_Wide_Type (T);
2816 Set_Direct_Primitive_Operations (T, New_Elmt_List);
2818 end Analyze_Formal_Incomplete_Type;
2820 ----------------------------------------
2821 -- Analyze_Formal_Signed_Integer_Type --
2822 ----------------------------------------
2824 procedure Analyze_Formal_Signed_Integer_Type
2828 Base : constant Entity_Id :=
2830 (E_Signed_Integer_Type,
2832 Sloc (Defining_Identifier (Parent (Def))), 'G');
2837 Set_Ekind (T, E_Signed_Integer_Subtype);
2838 Set_Etype (T, Base);
2839 Set_Size_Info (T, Standard_Integer);
2840 Set_RM_Size (T, RM_Size (Standard_Integer));
2841 Set_Scalar_Range (T, Scalar_Range (Standard_Integer));
2842 Set_Is_Constrained (T);
2844 Set_Is_Generic_Type (Base);
2845 Set_Size_Info (Base, Standard_Integer);
2846 Set_RM_Size (Base, RM_Size (Standard_Integer));
2847 Set_Etype (Base, Base);
2848 Set_Scalar_Range (Base, Scalar_Range (Standard_Integer));
2849 Set_Parent (Base, Parent (Def));
2850 end Analyze_Formal_Signed_Integer_Type;
2852 -------------------------------------------
2853 -- Analyze_Formal_Subprogram_Declaration --
2854 -------------------------------------------
2856 procedure Analyze_Formal_Subprogram_Declaration (N : Node_Id) is
2857 Spec : constant Node_Id := Specification (N);
2858 Def : constant Node_Id := Default_Name (N);
2859 Nam : constant Entity_Id := Defining_Unit_Name (Spec);
2867 if Nkind (Nam) = N_Defining_Program_Unit_Name then
2868 Error_Msg_N ("name of formal subprogram must be a direct name", Nam);
2872 Analyze_Subprogram_Declaration (N);
2873 Set_Is_Formal_Subprogram (Nam);
2874 Set_Has_Completion (Nam);
2876 if Nkind (N) = N_Formal_Abstract_Subprogram_Declaration then
2877 Set_Is_Abstract_Subprogram (Nam);
2878 Set_Is_Dispatching_Operation (Nam);
2881 Ctrl_Type : constant Entity_Id := Find_Dispatching_Type (Nam);
2883 if No (Ctrl_Type) then
2885 ("abstract formal subprogram must have a controlling type",
2888 elsif Ada_Version >= Ada_2012
2889 and then Is_Incomplete_Type (Ctrl_Type)
2892 ("controlling type of abstract formal subprogram cannot " &
2893 "be incomplete type", N, Ctrl_Type);
2896 Check_Controlling_Formals (Ctrl_Type, Nam);
2901 -- Default name is resolved at the point of instantiation
2903 if Box_Present (N) then
2906 -- Else default is bound at the point of generic declaration
2908 elsif Present (Def) then
2909 if Nkind (Def) = N_Operator_Symbol then
2910 Find_Direct_Name (Def);
2912 elsif Nkind (Def) /= N_Attribute_Reference then
2916 -- For an attribute reference, analyze the prefix and verify
2917 -- that it has the proper profile for the subprogram.
2919 Analyze (Prefix (Def));
2920 Valid_Default_Attribute (Nam, Def);
2924 -- Default name may be overloaded, in which case the interpretation
2925 -- with the correct profile must be selected, as for a renaming.
2926 -- If the definition is an indexed component, it must denote a
2927 -- member of an entry family. If it is a selected component, it
2928 -- can be a protected operation.
2930 if Etype (Def) = Any_Type then
2933 elsif Nkind (Def) = N_Selected_Component then
2934 if not Is_Overloadable (Entity (Selector_Name (Def))) then
2935 Error_Msg_N ("expect valid subprogram name as default", Def);
2938 elsif Nkind (Def) = N_Indexed_Component then
2939 if Is_Entity_Name (Prefix (Def)) then
2940 if Ekind (Entity (Prefix (Def))) /= E_Entry_Family then
2941 Error_Msg_N ("expect valid subprogram name as default", Def);
2944 elsif Nkind (Prefix (Def)) = N_Selected_Component then
2945 if Ekind (Entity (Selector_Name (Prefix (Def)))) /=
2948 Error_Msg_N ("expect valid subprogram name as default", Def);
2952 Error_Msg_N ("expect valid subprogram name as default", Def);
2956 elsif Nkind (Def) = N_Character_Literal then
2958 -- Needs some type checks: subprogram should be parameterless???
2960 Resolve (Def, (Etype (Nam)));
2962 elsif not Is_Entity_Name (Def)
2963 or else not Is_Overloadable (Entity (Def))
2965 Error_Msg_N ("expect valid subprogram name as default", Def);
2968 elsif not Is_Overloaded (Def) then
2969 Subp := Entity (Def);
2972 Error_Msg_N ("premature usage of formal subprogram", Def);
2974 elsif not Entity_Matches_Spec (Subp, Nam) then
2975 Error_Msg_N ("no visible entity matches specification", Def);
2978 -- More than one interpretation, so disambiguate as for a renaming
2983 I1 : Interp_Index := 0;
2989 Get_First_Interp (Def, I, It);
2990 while Present (It.Nam) loop
2991 if Entity_Matches_Spec (It.Nam, Nam) then
2992 if Subp /= Any_Id then
2993 It1 := Disambiguate (Def, I1, I, Etype (Subp));
2995 if It1 = No_Interp then
2996 Error_Msg_N ("ambiguous default subprogram", Def);
3009 Get_Next_Interp (I, It);
3013 if Subp /= Any_Id then
3015 -- Subprogram found, generate reference to it
3017 Set_Entity (Def, Subp);
3018 Generate_Reference (Subp, Def);
3021 Error_Msg_N ("premature usage of formal subprogram", Def);
3023 elsif Ekind (Subp) /= E_Operator then
3024 Check_Mode_Conformant (Subp, Nam);
3028 Error_Msg_N ("no visible subprogram matches specification", N);
3034 if Has_Aspects (N) then
3035 Analyze_Aspect_Specifications (N, Nam);
3038 end Analyze_Formal_Subprogram_Declaration;
3040 -------------------------------------
3041 -- Analyze_Formal_Type_Declaration --
3042 -------------------------------------
3044 procedure Analyze_Formal_Type_Declaration (N : Node_Id) is
3045 Def : constant Node_Id := Formal_Type_Definition (N);
3049 T := Defining_Identifier (N);
3051 if Present (Discriminant_Specifications (N))
3052 and then Nkind (Def) /= N_Formal_Private_Type_Definition
3055 ("discriminants not allowed for this formal type", T);
3058 -- Enter the new name, and branch to specific routine
3061 when N_Formal_Private_Type_Definition =>
3062 Analyze_Formal_Private_Type (N, T, Def);
3064 when N_Formal_Derived_Type_Definition =>
3065 Analyze_Formal_Derived_Type (N, T, Def);
3067 when N_Formal_Incomplete_Type_Definition =>
3068 Analyze_Formal_Incomplete_Type (T, Def);
3070 when N_Formal_Discrete_Type_Definition =>
3071 Analyze_Formal_Discrete_Type (T, Def);
3073 when N_Formal_Signed_Integer_Type_Definition =>
3074 Analyze_Formal_Signed_Integer_Type (T, Def);
3076 when N_Formal_Modular_Type_Definition =>
3077 Analyze_Formal_Modular_Type (T, Def);
3079 when N_Formal_Floating_Point_Definition =>
3080 Analyze_Formal_Floating_Type (T, Def);
3082 when N_Formal_Ordinary_Fixed_Point_Definition =>
3083 Analyze_Formal_Ordinary_Fixed_Point_Type (T, Def);
3085 when N_Formal_Decimal_Fixed_Point_Definition =>
3086 Analyze_Formal_Decimal_Fixed_Point_Type (T, Def);
3088 when N_Array_Type_Definition =>
3089 Analyze_Formal_Array_Type (T, Def);
3091 when N_Access_To_Object_Definition |
3092 N_Access_Function_Definition |
3093 N_Access_Procedure_Definition =>
3094 Analyze_Generic_Access_Type (T, Def);
3096 -- Ada 2005: a interface declaration is encoded as an abstract
3097 -- record declaration or a abstract type derivation.
3099 when N_Record_Definition =>
3100 Analyze_Formal_Interface_Type (N, T, Def);
3102 when N_Derived_Type_Definition =>
3103 Analyze_Formal_Derived_Interface_Type (N, T, Def);
3109 raise Program_Error;
3113 Set_Is_Generic_Type (T);
3115 if Has_Aspects (N) then
3116 Analyze_Aspect_Specifications (N, T);
3118 end Analyze_Formal_Type_Declaration;
3120 ------------------------------------
3121 -- Analyze_Function_Instantiation --
3122 ------------------------------------
3124 procedure Analyze_Function_Instantiation (N : Node_Id) is
3126 Analyze_Subprogram_Instantiation (N, E_Function);
3127 end Analyze_Function_Instantiation;
3129 ---------------------------------
3130 -- Analyze_Generic_Access_Type --
3131 ---------------------------------
3133 procedure Analyze_Generic_Access_Type (T : Entity_Id; Def : Node_Id) is
3137 if Nkind (Def) = N_Access_To_Object_Definition then
3138 Access_Type_Declaration (T, Def);
3140 if Is_Incomplete_Or_Private_Type (Designated_Type (T))
3141 and then No (Full_View (Designated_Type (T)))
3142 and then not Is_Generic_Type (Designated_Type (T))
3144 Error_Msg_N ("premature usage of incomplete type", Def);
3146 elsif not Is_Entity_Name (Subtype_Indication (Def)) then
3148 ("only a subtype mark is allowed in a formal", Def);
3152 Access_Subprogram_Declaration (T, Def);
3154 end Analyze_Generic_Access_Type;
3156 ---------------------------------
3157 -- Analyze_Generic_Formal_Part --
3158 ---------------------------------
3160 procedure Analyze_Generic_Formal_Part (N : Node_Id) is
3161 Gen_Parm_Decl : Node_Id;
3164 -- The generic formals are processed in the scope of the generic unit,
3165 -- where they are immediately visible. The scope is installed by the
3168 Gen_Parm_Decl := First (Generic_Formal_Declarations (N));
3170 while Present (Gen_Parm_Decl) loop
3171 Analyze (Gen_Parm_Decl);
3172 Next (Gen_Parm_Decl);
3175 Generate_Reference_To_Generic_Formals (Current_Scope);
3176 end Analyze_Generic_Formal_Part;
3178 ------------------------------------------
3179 -- Analyze_Generic_Package_Declaration --
3180 ------------------------------------------
3182 procedure Analyze_Generic_Package_Declaration (N : Node_Id) is
3183 Loc : constant Source_Ptr := Sloc (N);
3186 Save_Parent : Node_Id;
3188 Decls : constant List_Id :=
3189 Visible_Declarations (Specification (N));
3193 Check_SPARK_Restriction ("generic is not allowed", N);
3195 -- We introduce a renaming of the enclosing package, to have a usable
3196 -- entity as the prefix of an expanded name for a local entity of the
3197 -- form Par.P.Q, where P is the generic package. This is because a local
3198 -- entity named P may hide it, so that the usual visibility rules in
3199 -- the instance will not resolve properly.
3202 Make_Package_Renaming_Declaration (Loc,
3203 Defining_Unit_Name =>
3204 Make_Defining_Identifier (Loc,
3205 Chars => New_External_Name (Chars (Defining_Entity (N)), "GH")),
3206 Name => Make_Identifier (Loc, Chars (Defining_Entity (N))));
3208 if Present (Decls) then
3209 Decl := First (Decls);
3210 while Present (Decl)
3211 and then Nkind (Decl) = N_Pragma
3216 if Present (Decl) then
3217 Insert_Before (Decl, Renaming);
3219 Append (Renaming, Visible_Declarations (Specification (N)));
3223 Set_Visible_Declarations (Specification (N), New_List (Renaming));
3226 -- Create copy of generic unit, and save for instantiation. If the unit
3227 -- is a child unit, do not copy the specifications for the parent, which
3228 -- are not part of the generic tree.
3230 Save_Parent := Parent_Spec (N);
3231 Set_Parent_Spec (N, Empty);
3233 New_N := Copy_Generic_Node (N, Empty, Instantiating => False);
3234 Set_Parent_Spec (New_N, Save_Parent);
3237 -- Once the contents of the generic copy and the template are swapped,
3238 -- do the same for their respective aspect specifications.
3240 Exchange_Aspects (N, New_N);
3241 Id := Defining_Entity (N);
3242 Generate_Definition (Id);
3244 -- Expansion is not applied to generic units
3249 Set_Ekind (Id, E_Generic_Package);
3250 Set_Etype (Id, Standard_Void_Type);
3251 Set_Contract (Id, Make_Contract (Sloc (Id)));
3253 -- Analyze aspects now, so that generated pragmas appear in the
3254 -- declarations before building and analyzing the generic copy.
3256 if Has_Aspects (N) then
3257 Analyze_Aspect_Specifications (N, Id);
3261 Enter_Generic_Scope (Id);
3262 Set_Inner_Instances (Id, New_Elmt_List);
3264 Set_Categorization_From_Pragmas (N);
3265 Set_Is_Pure (Id, Is_Pure (Current_Scope));
3267 -- Link the declaration of the generic homonym in the generic copy to
3268 -- the package it renames, so that it is always resolved properly.
3270 Set_Generic_Homonym (Id, Defining_Unit_Name (Renaming));
3271 Set_Entity (Associated_Node (Name (Renaming)), Id);
3273 -- For a library unit, we have reconstructed the entity for the unit,
3274 -- and must reset it in the library tables.
3276 if Nkind (Parent (N)) = N_Compilation_Unit then
3277 Set_Cunit_Entity (Current_Sem_Unit, Id);
3280 Analyze_Generic_Formal_Part (N);
3282 -- After processing the generic formals, analysis proceeds as for a
3283 -- non-generic package.
3285 Analyze (Specification (N));
3287 Validate_Categorization_Dependency (N, Id);
3291 End_Package_Scope (Id);
3292 Exit_Generic_Scope (Id);
3294 if Nkind (Parent (N)) /= N_Compilation_Unit then
3295 Move_Freeze_Nodes (Id, N, Visible_Declarations (Specification (N)));
3296 Move_Freeze_Nodes (Id, N, Private_Declarations (Specification (N)));
3297 Move_Freeze_Nodes (Id, N, Generic_Formal_Declarations (N));
3300 Set_Body_Required (Parent (N), Unit_Requires_Body (Id));
3301 Validate_RT_RAT_Component (N);
3303 -- If this is a spec without a body, check that generic parameters
3306 if not Body_Required (Parent (N)) then
3307 Check_References (Id);
3310 end Analyze_Generic_Package_Declaration;
3312 --------------------------------------------
3313 -- Analyze_Generic_Subprogram_Declaration --
3314 --------------------------------------------
3316 procedure Analyze_Generic_Subprogram_Declaration (N : Node_Id) is
3321 Result_Type : Entity_Id;
3322 Save_Parent : Node_Id;
3326 Check_SPARK_Restriction ("generic is not allowed", N);
3328 -- Create copy of generic unit, and save for instantiation. If the unit
3329 -- is a child unit, do not copy the specifications for the parent, which
3330 -- are not part of the generic tree.
3332 Save_Parent := Parent_Spec (N);
3333 Set_Parent_Spec (N, Empty);
3335 New_N := Copy_Generic_Node (N, Empty, Instantiating => False);
3336 Set_Parent_Spec (New_N, Save_Parent);
3339 Check_SPARK_Mode_In_Generic (N);
3341 -- The aspect specifications are not attached to the tree, and must
3342 -- be copied and attached to the generic copy explicitly.
3344 if Present (Aspect_Specifications (New_N)) then
3346 Aspects : constant List_Id := Aspect_Specifications (N);
3348 Set_Has_Aspects (N, False);
3349 Move_Aspects (New_N, To => N);
3350 Set_Has_Aspects (Original_Node (N), False);
3351 Set_Aspect_Specifications (Original_Node (N), Aspects);
3355 Spec := Specification (N);
3356 Id := Defining_Entity (Spec);
3357 Generate_Definition (Id);
3358 Set_Contract (Id, Make_Contract (Sloc (Id)));
3360 if Nkind (Id) = N_Defining_Operator_Symbol then
3362 ("operator symbol not allowed for generic subprogram", Id);
3369 Set_Scope_Depth_Value (Id, Scope_Depth (Current_Scope) + 1);
3371 Enter_Generic_Scope (Id);
3372 Set_Inner_Instances (Id, New_Elmt_List);
3373 Set_Is_Pure (Id, Is_Pure (Current_Scope));
3375 Analyze_Generic_Formal_Part (N);
3377 Formals := Parameter_Specifications (Spec);
3379 if Present (Formals) then
3380 Process_Formals (Formals, Spec);
3383 if Nkind (Spec) = N_Function_Specification then
3384 Set_Ekind (Id, E_Generic_Function);
3386 if Nkind (Result_Definition (Spec)) = N_Access_Definition then
3387 Result_Type := Access_Definition (Spec, Result_Definition (Spec));
3388 Set_Etype (Id, Result_Type);
3390 -- Check restriction imposed by AI05-073: a generic function
3391 -- cannot return an abstract type or an access to such.
3393 -- This is a binding interpretation should it apply to earlier
3394 -- versions of Ada as well as Ada 2012???
3396 if Is_Abstract_Type (Designated_Type (Result_Type))
3397 and then Ada_Version >= Ada_2012
3399 Error_Msg_N ("generic function cannot have an access result"
3400 & " that designates an abstract type", Spec);
3404 Find_Type (Result_Definition (Spec));
3405 Typ := Entity (Result_Definition (Spec));
3407 if Is_Abstract_Type (Typ)
3408 and then Ada_Version >= Ada_2012
3411 ("generic function cannot have abstract result type", Spec);
3414 -- If a null exclusion is imposed on the result type, then create
3415 -- a null-excluding itype (an access subtype) and use it as the
3416 -- function's Etype.
3418 if Is_Access_Type (Typ)
3419 and then Null_Exclusion_Present (Spec)
3422 Create_Null_Excluding_Itype
3424 Related_Nod => Spec,
3425 Scope_Id => Defining_Unit_Name (Spec)));
3427 Set_Etype (Id, Typ);
3432 Set_Ekind (Id, E_Generic_Procedure);
3433 Set_Etype (Id, Standard_Void_Type);
3436 -- For a library unit, we have reconstructed the entity for the unit,
3437 -- and must reset it in the library tables. We also make sure that
3438 -- Body_Required is set properly in the original compilation unit node.
3440 if Nkind (Parent (N)) = N_Compilation_Unit then
3441 Set_Cunit_Entity (Current_Sem_Unit, Id);
3442 Set_Body_Required (Parent (N), Unit_Requires_Body (Id));
3445 Set_Categorization_From_Pragmas (N);
3446 Validate_Categorization_Dependency (N, Id);
3448 Save_Global_References (Original_Node (N));
3450 -- For ASIS purposes, convert any postcondition, precondition pragmas
3451 -- into aspects, if N is not a compilation unit by itself, in order to
3452 -- enable the analysis of expressions inside the corresponding PPC
3455 if ASIS_Mode and then Is_List_Member (N) then
3456 Make_Aspect_For_PPC_In_Gen_Sub_Decl (N);
3459 -- To capture global references, analyze the expressions of aspects,
3460 -- and propagate information to original tree. Note that in this case
3461 -- analysis of attributes is not delayed until the freeze point.
3463 -- It seems very hard to recreate the proper visibility of the generic
3464 -- subprogram at a later point because the analysis of an aspect may
3465 -- create pragmas after the generic copies have been made ???
3467 if Has_Aspects (N) then
3472 Aspect := First (Aspect_Specifications (N));
3473 while Present (Aspect) loop
3474 if Get_Aspect_Id (Aspect) /= Aspect_Warnings
3475 and then Present (Expression (Aspect))
3477 Analyze (Expression (Aspect));
3483 Aspect := First (Aspect_Specifications (Original_Node (N)));
3484 while Present (Aspect) loop
3485 if Present (Expression (Aspect)) then
3486 Save_Global_References (Expression (Aspect));
3496 Exit_Generic_Scope (Id);
3497 Generate_Reference_To_Formals (Id);
3499 List_Inherited_Pre_Post_Aspects (Id);
3500 end Analyze_Generic_Subprogram_Declaration;
3502 -----------------------------------
3503 -- Analyze_Package_Instantiation --
3504 -----------------------------------
3506 procedure Analyze_Package_Instantiation (N : Node_Id) is
3507 Loc : constant Source_Ptr := Sloc (N);
3508 Gen_Id : constant Node_Id := Name (N);
3511 Act_Decl_Name : Node_Id;
3512 Act_Decl_Id : Entity_Id;
3517 Gen_Unit : Entity_Id;
3519 Is_Actual_Pack : constant Boolean :=
3520 Is_Internal (Defining_Entity (N));
3522 Env_Installed : Boolean := False;
3523 Parent_Installed : Boolean := False;
3524 Renaming_List : List_Id;
3525 Unit_Renaming : Node_Id;
3526 Needs_Body : Boolean;
3527 Inline_Now : Boolean := False;
3529 Save_Style_Check : constant Boolean := Style_Check;
3530 -- Save style check mode for restore on exit
3532 procedure Delay_Descriptors (E : Entity_Id);
3533 -- Delay generation of subprogram descriptors for given entity
3535 function Might_Inline_Subp return Boolean;
3536 -- If inlining is active and the generic contains inlined subprograms,
3537 -- we instantiate the body. This may cause superfluous instantiations,
3538 -- but it is simpler than detecting the need for the body at the point
3539 -- of inlining, when the context of the instance is not available.
3541 function Must_Inline_Subp return Boolean;
3542 -- If inlining is active and the generic contains inlined subprograms,
3543 -- return True if some of the inlined subprograms must be inlined by
3546 -----------------------
3547 -- Delay_Descriptors --
3548 -----------------------
3550 procedure Delay_Descriptors (E : Entity_Id) is
3552 if not Delay_Subprogram_Descriptors (E) then
3553 Set_Delay_Subprogram_Descriptors (E);
3554 Pending_Descriptor.Append (E);
3556 end Delay_Descriptors;
3558 -----------------------
3559 -- Might_Inline_Subp --
3560 -----------------------
3562 function Might_Inline_Subp return Boolean is
3566 if not Inline_Processing_Required then
3570 E := First_Entity (Gen_Unit);
3571 while Present (E) loop
3572 if Is_Subprogram (E)
3573 and then Is_Inlined (E)
3583 end Might_Inline_Subp;
3585 ----------------------
3586 -- Must_Inline_Subp --
3587 ----------------------
3589 function Must_Inline_Subp return Boolean is
3593 if not Inline_Processing_Required then
3597 E := First_Entity (Gen_Unit);
3598 while Present (E) loop
3599 if Is_Subprogram (E)
3600 and then Is_Inlined (E)
3601 and then Must_Inline (E)
3611 end Must_Inline_Subp;
3613 -- Local declarations
3615 Vis_Prims_List : Elist_Id := No_Elist;
3616 -- List of primitives made temporarily visible in the instantiation
3617 -- to match the visibility of the formal type
3619 -- Start of processing for Analyze_Package_Instantiation
3622 Check_SPARK_Restriction ("generic is not allowed", N);
3624 -- Very first thing: check for Text_IO sp[ecial unit in case we are
3625 -- instantiating one of the children of [[Wide_]Wide_]Text_IO.
3627 Check_Text_IO_Special_Unit (Name (N));
3629 -- Make node global for error reporting
3631 Instantiation_Node := N;
3633 -- Turn off style checking in instances. If the check is enabled on the
3634 -- generic unit, a warning in an instance would just be noise. If not
3635 -- enabled on the generic, then a warning in an instance is just wrong.
3637 Style_Check := False;
3639 -- Case of instantiation of a generic package
3641 if Nkind (N) = N_Package_Instantiation then
3642 Act_Decl_Id := New_Copy (Defining_Entity (N));
3643 Set_Comes_From_Source (Act_Decl_Id, True);
3645 if Nkind (Defining_Unit_Name (N)) = N_Defining_Program_Unit_Name then
3647 Make_Defining_Program_Unit_Name (Loc,
3648 Name => New_Copy_Tree (Name (Defining_Unit_Name (N))),
3649 Defining_Identifier => Act_Decl_Id);
3651 Act_Decl_Name := Act_Decl_Id;
3654 -- Case of instantiation of a formal package
3657 Act_Decl_Id := Defining_Identifier (N);
3658 Act_Decl_Name := Act_Decl_Id;
3661 Generate_Definition (Act_Decl_Id);
3662 Preanalyze_Actuals (N);
3665 Env_Installed := True;
3667 -- Reset renaming map for formal types. The mapping is established
3668 -- when analyzing the generic associations, but some mappings are
3669 -- inherited from formal packages of parent units, and these are
3670 -- constructed when the parents are installed.
3672 Generic_Renamings.Set_Last (0);
3673 Generic_Renamings_HTable.Reset;
3675 Check_Generic_Child_Unit (Gen_Id, Parent_Installed);
3676 Gen_Unit := Entity (Gen_Id);
3678 -- Verify that it is the name of a generic package
3680 -- A visibility glitch: if the instance is a child unit and the generic
3681 -- is the generic unit of a parent instance (i.e. both the parent and
3682 -- the child units are instances of the same package) the name now
3683 -- denotes the renaming within the parent, not the intended generic
3684 -- unit. See if there is a homonym that is the desired generic. The
3685 -- renaming declaration must be visible inside the instance of the
3686 -- child, but not when analyzing the name in the instantiation itself.
3688 if Ekind (Gen_Unit) = E_Package
3689 and then Present (Renamed_Entity (Gen_Unit))
3690 and then In_Open_Scopes (Renamed_Entity (Gen_Unit))
3691 and then Is_Generic_Instance (Renamed_Entity (Gen_Unit))
3692 and then Present (Homonym (Gen_Unit))
3694 Gen_Unit := Homonym (Gen_Unit);
3697 if Etype (Gen_Unit) = Any_Type then
3701 elsif Ekind (Gen_Unit) /= E_Generic_Package then
3703 -- Ada 2005 (AI-50217): Cannot use instance in limited with_clause
3705 if From_Limited_With (Gen_Unit) then
3707 ("cannot instantiate a limited withed package", Gen_Id);
3710 ("& is not the name of a generic package", Gen_Id, Gen_Unit);
3717 if In_Extended_Main_Source_Unit (N) then
3718 Set_Is_Instantiated (Gen_Unit);
3719 Generate_Reference (Gen_Unit, N);
3721 if Present (Renamed_Object (Gen_Unit)) then
3722 Set_Is_Instantiated (Renamed_Object (Gen_Unit));
3723 Generate_Reference (Renamed_Object (Gen_Unit), N);
3727 if Nkind (Gen_Id) = N_Identifier
3728 and then Chars (Gen_Unit) = Chars (Defining_Entity (N))
3731 ("& is hidden within declaration of instance", Gen_Id, Gen_Unit);
3733 elsif Nkind (Gen_Id) = N_Expanded_Name
3734 and then Is_Child_Unit (Gen_Unit)
3735 and then Nkind (Prefix (Gen_Id)) = N_Identifier
3736 and then Chars (Act_Decl_Id) = Chars (Prefix (Gen_Id))
3739 ("& is hidden within declaration of instance ", Prefix (Gen_Id));
3742 Set_Entity (Gen_Id, Gen_Unit);
3744 -- If generic is a renaming, get original generic unit
3746 if Present (Renamed_Object (Gen_Unit))
3747 and then Ekind (Renamed_Object (Gen_Unit)) = E_Generic_Package
3749 Gen_Unit := Renamed_Object (Gen_Unit);
3752 -- Verify that there are no circular instantiations
3754 if In_Open_Scopes (Gen_Unit) then
3755 Error_Msg_NE ("instantiation of & within itself", N, Gen_Unit);
3759 elsif Contains_Instance_Of (Gen_Unit, Current_Scope, Gen_Id) then
3760 Error_Msg_Node_2 := Current_Scope;
3762 ("circular Instantiation: & instantiated in &!", N, Gen_Unit);
3763 Circularity_Detected := True;
3768 Gen_Decl := Unit_Declaration_Node (Gen_Unit);
3770 -- Initialize renamings map, for error checking, and the list that
3771 -- holds private entities whose views have changed between generic
3772 -- definition and instantiation. If this is the instance created to
3773 -- validate an actual package, the instantiation environment is that
3774 -- of the enclosing instance.
3776 Create_Instantiation_Source (N, Gen_Unit, False, S_Adjustment);
3778 -- Copy original generic tree, to produce text for instantiation
3782 (Original_Node (Gen_Decl), Empty, Instantiating => True);
3784 Act_Spec := Specification (Act_Tree);
3786 -- If this is the instance created to validate an actual package,
3787 -- only the formals matter, do not examine the package spec itself.
3789 if Is_Actual_Pack then
3790 Set_Visible_Declarations (Act_Spec, New_List);
3791 Set_Private_Declarations (Act_Spec, New_List);
3795 Analyze_Associations
3797 Formals => Generic_Formal_Declarations (Act_Tree),
3798 F_Copy => Generic_Formal_Declarations (Gen_Decl));
3800 Vis_Prims_List := Check_Hidden_Primitives (Renaming_List);
3802 Set_Instance_Env (Gen_Unit, Act_Decl_Id);
3803 Set_Defining_Unit_Name (Act_Spec, Act_Decl_Name);
3804 Set_Is_Generic_Instance (Act_Decl_Id);
3805 Set_Generic_Parent (Act_Spec, Gen_Unit);
3807 -- References to the generic in its own declaration or its body are
3808 -- references to the instance. Add a renaming declaration for the
3809 -- generic unit itself. This declaration, as well as the renaming
3810 -- declarations for the generic formals, must remain private to the
3811 -- unit: the formals, because this is the language semantics, and
3812 -- the unit because its use is an artifact of the implementation.
3815 Make_Package_Renaming_Declaration (Loc,
3816 Defining_Unit_Name =>
3817 Make_Defining_Identifier (Loc, Chars (Gen_Unit)),
3818 Name => New_Occurrence_Of (Act_Decl_Id, Loc));
3820 Append (Unit_Renaming, Renaming_List);
3822 -- The renaming declarations are the first local declarations of the
3825 if Is_Non_Empty_List (Visible_Declarations (Act_Spec)) then
3827 (First (Visible_Declarations (Act_Spec)), Renaming_List);
3829 Set_Visible_Declarations (Act_Spec, Renaming_List);
3833 Make_Package_Declaration (Loc,
3834 Specification => Act_Spec);
3836 -- Propagate the aspect specifications from the package declaration
3837 -- template to the instantiated version of the package declaration.
3839 if Has_Aspects (Act_Tree) then
3840 Set_Aspect_Specifications (Act_Decl,
3841 New_Copy_List_Tree (Aspect_Specifications (Act_Tree)));
3844 -- Save the instantiation node, for subsequent instantiation of the
3845 -- body, if there is one and we are generating code for the current
3846 -- unit. Mark unit as having a body (avoids premature error message).
3848 -- We instantiate the body if we are generating code, if we are
3849 -- generating cross-reference information, or if we are building
3850 -- trees for ASIS use or GNATprove use.
3853 Enclosing_Body_Present : Boolean := False;
3854 -- If the generic unit is not a compilation unit, then a body may
3855 -- be present in its parent even if none is required. We create a
3856 -- tentative pending instantiation for the body, which will be
3857 -- discarded if none is actually present.
3862 if Scope (Gen_Unit) /= Standard_Standard
3863 and then not Is_Child_Unit (Gen_Unit)
3865 Scop := Scope (Gen_Unit);
3867 while Present (Scop)
3868 and then Scop /= Standard_Standard
3870 if Unit_Requires_Body (Scop) then
3871 Enclosing_Body_Present := True;
3874 elsif In_Open_Scopes (Scop)
3875 and then In_Package_Body (Scop)
3877 Enclosing_Body_Present := True;
3881 exit when Is_Compilation_Unit (Scop);
3882 Scop := Scope (Scop);
3886 -- If front-end inlining is enabled, and this is a unit for which
3887 -- code will be generated, we instantiate the body at once.
3889 -- This is done if the instance is not the main unit, and if the
3890 -- generic is not a child unit of another generic, to avoid scope
3891 -- problems and the reinstallation of parent instances.
3894 and then (not Is_Child_Unit (Gen_Unit)
3895 or else not Is_Generic_Unit (Scope (Gen_Unit)))
3896 and then Might_Inline_Subp
3897 and then not Is_Actual_Pack
3899 if not Back_End_Inlining
3900 and then Front_End_Inlining
3901 and then (Is_In_Main_Unit (N)
3902 or else In_Main_Context (Current_Scope))
3903 and then Nkind (Parent (N)) /= N_Compilation_Unit
3907 elsif Back_End_Inlining
3908 and then Must_Inline_Subp
3909 and then (Is_In_Main_Unit (N)
3910 or else In_Main_Context (Current_Scope))
3911 and then Nkind (Parent (N)) /= N_Compilation_Unit
3915 -- In configurable_run_time mode we force the inlining of
3916 -- predefined subprograms marked Inline_Always, to minimize
3917 -- the use of the run-time library.
3919 elsif Is_Predefined_File_Name
3920 (Unit_File_Name (Get_Source_Unit (Gen_Decl)))
3921 and then Configurable_Run_Time_Mode
3922 and then Nkind (Parent (N)) /= N_Compilation_Unit
3927 -- If the current scope is itself an instance within a child
3928 -- unit, there will be duplications in the scope stack, and the
3929 -- unstacking mechanism in Inline_Instance_Body will fail.
3930 -- This loses some rare cases of optimization, and might be
3931 -- improved some day, if we can find a proper abstraction for
3932 -- "the complete compilation context" that can be saved and
3935 if Is_Generic_Instance (Current_Scope) then
3937 Curr_Unit : constant Entity_Id :=
3938 Cunit_Entity (Current_Sem_Unit);
3940 if Curr_Unit /= Current_Scope
3941 and then Is_Child_Unit (Curr_Unit)
3943 Inline_Now := False;
3950 (Unit_Requires_Body (Gen_Unit)
3951 or else Enclosing_Body_Present
3952 or else Present (Corresponding_Body (Gen_Decl)))
3953 and then (Is_In_Main_Unit (N) or else Might_Inline_Subp)
3954 and then not Is_Actual_Pack
3955 and then not Inline_Now
3956 and then (Operating_Mode = Generate_Code
3958 -- Need comment for this check ???
3960 or else (Operating_Mode = Check_Semantics
3961 and then (ASIS_Mode or GNATprove_Mode)));
3963 -- If front_end_inlining is enabled, do not instantiate body if
3964 -- within a generic context.
3966 if (Front_End_Inlining and then not Expander_Active)
3967 or else Is_Generic_Unit (Cunit_Entity (Main_Unit))
3969 Needs_Body := False;
3972 -- If the current context is generic, and the package being
3973 -- instantiated is declared within a formal package, there is no
3974 -- body to instantiate until the enclosing generic is instantiated
3975 -- and there is an actual for the formal package. If the formal
3976 -- package has parameters, we build a regular package instance for
3977 -- it, that precedes the original formal package declaration.
3979 if In_Open_Scopes (Scope (Scope (Gen_Unit))) then
3981 Decl : constant Node_Id :=
3983 (Unit_Declaration_Node (Scope (Gen_Unit)));
3985 if Nkind (Decl) = N_Formal_Package_Declaration
3986 or else (Nkind (Decl) = N_Package_Declaration
3987 and then Is_List_Member (Decl)
3988 and then Present (Next (Decl))
3990 Nkind (Next (Decl)) =
3991 N_Formal_Package_Declaration)
3993 Needs_Body := False;
3999 -- For RCI unit calling stubs, we omit the instance body if the
4000 -- instance is the RCI library unit itself.
4002 -- However there is a special case for nested instances: in this case
4003 -- we do generate the instance body, as it might be required, e.g.
4004 -- because it provides stream attributes for some type used in the
4005 -- profile of a remote subprogram. This is consistent with 12.3(12),
4006 -- which indicates that the instance body occurs at the place of the
4007 -- instantiation, and thus is part of the RCI declaration, which is
4008 -- present on all client partitions (this is E.2.3(18)).
4010 -- Note that AI12-0002 may make it illegal at some point to have
4011 -- stream attributes defined in an RCI unit, in which case this
4012 -- special case will become unnecessary. In the meantime, there
4013 -- is known application code in production that depends on this
4014 -- being possible, so we definitely cannot eliminate the body in
4015 -- the case of nested instances for the time being.
4017 -- When we generate a nested instance body, calling stubs for any
4018 -- relevant subprogram will be be inserted immediately after the
4019 -- subprogram declarations, and will take precedence over the
4020 -- subsequent (original) body. (The stub and original body will be
4021 -- complete homographs, but this is permitted in an instance).
4022 -- (Could we do better and remove the original body???)
4024 if Distribution_Stub_Mode = Generate_Caller_Stub_Body
4025 and then Comes_From_Source (N)
4026 and then Nkind (Parent (N)) = N_Compilation_Unit
4028 Needs_Body := False;
4033 -- Here is a defence against a ludicrous number of instantiations
4034 -- caused by a circular set of instantiation attempts.
4036 if Pending_Instantiations.Last > Maximum_Instantiations then
4037 Error_Msg_Uint_1 := UI_From_Int (Maximum_Instantiations);
4038 Error_Msg_N ("too many instantiations, exceeds max of^", N);
4039 Error_Msg_N ("\limit can be changed using -gnateinn switch", N);
4040 raise Unrecoverable_Error;
4043 -- Indicate that the enclosing scopes contain an instantiation,
4044 -- and that cleanup actions should be delayed until after the
4045 -- instance body is expanded.
4047 Check_Forward_Instantiation (Gen_Decl);
4048 if Nkind (N) = N_Package_Instantiation then
4050 Enclosing_Master : Entity_Id;
4053 -- Loop to search enclosing masters
4055 Enclosing_Master := Current_Scope;
4056 Scope_Loop : while Enclosing_Master /= Standard_Standard loop
4057 if Ekind (Enclosing_Master) = E_Package then
4058 if Is_Compilation_Unit (Enclosing_Master) then
4059 if In_Package_Body (Enclosing_Master) then
4061 (Body_Entity (Enclosing_Master));
4070 Enclosing_Master := Scope (Enclosing_Master);
4073 elsif Is_Generic_Unit (Enclosing_Master)
4074 or else Ekind (Enclosing_Master) = E_Void
4076 -- Cleanup actions will eventually be performed on the
4077 -- enclosing subprogram or package instance, if any.
4078 -- Enclosing scope is void in the formal part of a
4079 -- generic subprogram.
4084 if Ekind (Enclosing_Master) = E_Entry
4086 Ekind (Scope (Enclosing_Master)) = E_Protected_Type
4088 if not Expander_Active then
4092 Protected_Body_Subprogram (Enclosing_Master);
4096 Set_Delay_Cleanups (Enclosing_Master);
4098 while Ekind (Enclosing_Master) = E_Block loop
4099 Enclosing_Master := Scope (Enclosing_Master);
4102 if Is_Subprogram (Enclosing_Master) then
4103 Delay_Descriptors (Enclosing_Master);
4105 elsif Is_Task_Type (Enclosing_Master) then
4107 TBP : constant Node_Id :=
4108 Get_Task_Body_Procedure
4111 if Present (TBP) then
4112 Delay_Descriptors (TBP);
4113 Set_Delay_Cleanups (TBP);
4120 end loop Scope_Loop;
4123 -- Make entry in table
4125 Pending_Instantiations.Append
4127 Act_Decl => Act_Decl,
4128 Expander_Status => Expander_Active,
4129 Current_Sem_Unit => Current_Sem_Unit,
4130 Scope_Suppress => Scope_Suppress,
4131 Local_Suppress_Stack_Top => Local_Suppress_Stack_Top,
4132 Version => Ada_Version,
4133 Version_Pragma => Ada_Version_Pragma,
4134 Warnings => Save_Warnings,
4135 SPARK_Mode => SPARK_Mode,
4136 SPARK_Mode_Pragma => SPARK_Mode_Pragma));
4140 Set_Categorization_From_Pragmas (Act_Decl);
4142 if Parent_Installed then
4146 Set_Instance_Spec (N, Act_Decl);
4148 -- If not a compilation unit, insert the package declaration before
4149 -- the original instantiation node.
4151 if Nkind (Parent (N)) /= N_Compilation_Unit then
4152 Mark_Rewrite_Insertion (Act_Decl);
4153 Insert_Before (N, Act_Decl);
4156 -- For an instantiation that is a compilation unit, place
4157 -- declaration on current node so context is complete for analysis
4158 -- (including nested instantiations). If this is the main unit,
4159 -- the declaration eventually replaces the instantiation node.
4160 -- If the instance body is created later, it replaces the
4161 -- instance node, and the declaration is attached to it
4162 -- (see Build_Instance_Compilation_Unit_Nodes).
4165 if Cunit_Entity (Current_Sem_Unit) = Defining_Entity (N) then
4167 -- The entity for the current unit is the newly created one,
4168 -- and all semantic information is attached to it.
4170 Set_Cunit_Entity (Current_Sem_Unit, Act_Decl_Id);
4172 -- If this is the main unit, replace the main entity as well
4174 if Current_Sem_Unit = Main_Unit then
4175 Main_Unit_Entity := Act_Decl_Id;
4179 Set_Unit (Parent (N), Act_Decl);
4180 Set_Parent_Spec (Act_Decl, Parent_Spec (N));
4181 Set_Package_Instantiation (Act_Decl_Id, N);
4183 -- Process aspect specifications of the instance node, if any, to
4184 -- take into account categorization pragmas before analyzing the
4187 if Has_Aspects (N) then
4188 Analyze_Aspect_Specifications (N, Act_Decl_Id);
4192 Set_Unit (Parent (N), N);
4193 Set_Body_Required (Parent (N), False);
4195 -- We never need elaboration checks on instantiations, since by
4196 -- definition, the body instantiation is elaborated at the same
4197 -- time as the spec instantiation.
4199 Set_Suppress_Elaboration_Warnings (Act_Decl_Id);
4200 Set_Kill_Elaboration_Checks (Act_Decl_Id);
4203 Check_Elab_Instantiation (N);
4205 if ABE_Is_Certain (N) and then Needs_Body then
4206 Pending_Instantiations.Decrement_Last;
4209 Check_Hidden_Child_Unit (N, Gen_Unit, Act_Decl_Id);
4211 Set_First_Private_Entity (Defining_Unit_Name (Unit_Renaming),
4212 First_Private_Entity (Act_Decl_Id));
4214 -- If the instantiation will receive a body, the unit will be
4215 -- transformed into a package body, and receive its own elaboration
4216 -- entity. Otherwise, the nature of the unit is now a package
4219 if Nkind (Parent (N)) = N_Compilation_Unit
4220 and then not Needs_Body
4222 Rewrite (N, Act_Decl);
4225 if Present (Corresponding_Body (Gen_Decl))
4226 or else Unit_Requires_Body (Gen_Unit)
4228 Set_Has_Completion (Act_Decl_Id);
4231 Check_Formal_Packages (Act_Decl_Id);
4233 Restore_Hidden_Primitives (Vis_Prims_List);
4234 Restore_Private_Views (Act_Decl_Id);
4236 Inherit_Context (Gen_Decl, N);
4238 if Parent_Installed then
4243 Env_Installed := False;
4246 Validate_Categorization_Dependency (N, Act_Decl_Id);
4248 -- There used to be a check here to prevent instantiations in local
4249 -- contexts if the No_Local_Allocators restriction was active. This
4250 -- check was removed by a binding interpretation in AI-95-00130/07,
4251 -- but we retain the code for documentation purposes.
4253 -- if Ekind (Act_Decl_Id) /= E_Void
4254 -- and then not Is_Library_Level_Entity (Act_Decl_Id)
4256 -- Check_Restriction (No_Local_Allocators, N);
4260 Inline_Instance_Body (N, Gen_Unit, Act_Decl);
4263 -- The following is a tree patch for ASIS: ASIS needs separate nodes to
4264 -- be used as defining identifiers for a formal package and for the
4265 -- corresponding expanded package.
4267 if Nkind (N) = N_Formal_Package_Declaration then
4268 Act_Decl_Id := New_Copy (Defining_Entity (N));
4269 Set_Comes_From_Source (Act_Decl_Id, True);
4270 Set_Is_Generic_Instance (Act_Decl_Id, False);
4271 Set_Defining_Identifier (N, Act_Decl_Id);
4274 Style_Check := Save_Style_Check;
4276 -- Check that if N is an instantiation of System.Dim_Float_IO or
4277 -- System.Dim_Integer_IO, the formal type has a dimension system.
4279 if Nkind (N) = N_Package_Instantiation
4280 and then Is_Dim_IO_Package_Instantiation (N)
4283 Assoc : constant Node_Id := First (Generic_Associations (N));
4285 if not Has_Dimension_System
4286 (Etype (Explicit_Generic_Actual_Parameter (Assoc)))
4288 Error_Msg_N ("type with a dimension system expected", Assoc);
4294 if Has_Aspects (N) and then Nkind (Parent (N)) /= N_Compilation_Unit then
4295 Analyze_Aspect_Specifications (N, Act_Decl_Id);
4299 when Instantiation_Error =>
4300 if Parent_Installed then
4304 if Env_Installed then
4308 Style_Check := Save_Style_Check;
4309 end Analyze_Package_Instantiation;
4311 --------------------------
4312 -- Inline_Instance_Body --
4313 --------------------------
4315 procedure Inline_Instance_Body
4317 Gen_Unit : Entity_Id;
4321 Gen_Comp : constant Entity_Id :=
4322 Cunit_Entity (Get_Source_Unit (Gen_Unit));
4323 Curr_Comp : constant Node_Id := Cunit (Current_Sem_Unit);
4324 Curr_Scope : Entity_Id := Empty;
4325 Curr_Unit : constant Entity_Id := Cunit_Entity (Current_Sem_Unit);
4326 Removed : Boolean := False;
4327 Num_Scopes : Int := 0;
4329 Scope_Stack_Depth : constant Int :=
4330 Scope_Stack.Last - Scope_Stack.First + 1;
4332 Use_Clauses : array (1 .. Scope_Stack_Depth) of Node_Id;
4333 Instances : array (1 .. Scope_Stack_Depth) of Entity_Id;
4334 Inner_Scopes : array (1 .. Scope_Stack_Depth) of Entity_Id;
4336 Num_Inner : Int := 0;
4337 N_Instances : Int := 0;
4341 -- Case of generic unit defined in another unit. We must remove the
4342 -- complete context of the current unit to install that of the generic.
4344 if Gen_Comp /= Cunit_Entity (Current_Sem_Unit) then
4346 -- Add some comments for the following two loops ???
4349 while Present (S) and then S /= Standard_Standard loop
4351 Num_Scopes := Num_Scopes + 1;
4353 Use_Clauses (Num_Scopes) :=
4355 (Scope_Stack.Last - Num_Scopes + 1).
4357 End_Use_Clauses (Use_Clauses (Num_Scopes));
4359 exit when Scope_Stack.Last - Num_Scopes + 1 = Scope_Stack.First
4360 or else Scope_Stack.Table
4361 (Scope_Stack.Last - Num_Scopes).Entity
4365 exit when Is_Generic_Instance (S)
4366 and then (In_Package_Body (S)
4367 or else Ekind (S) = E_Procedure
4368 or else Ekind (S) = E_Function);
4372 Vis := Is_Immediately_Visible (Gen_Comp);
4374 -- Find and save all enclosing instances
4379 and then S /= Standard_Standard
4381 if Is_Generic_Instance (S) then
4382 N_Instances := N_Instances + 1;
4383 Instances (N_Instances) := S;
4385 exit when In_Package_Body (S);
4391 -- Remove context of current compilation unit, unless we are within a
4392 -- nested package instantiation, in which case the context has been
4393 -- removed previously.
4395 -- If current scope is the body of a child unit, remove context of
4396 -- spec as well. If an enclosing scope is an instance body, the
4397 -- context has already been removed, but the entities in the body
4398 -- must be made invisible as well.
4403 and then S /= Standard_Standard
4405 if Is_Generic_Instance (S)
4406 and then (In_Package_Body (S)
4407 or else Ekind (S) = E_Procedure
4408 or else Ekind (S) = E_Function)
4410 -- We still have to remove the entities of the enclosing
4411 -- instance from direct visibility.
4416 E := First_Entity (S);
4417 while Present (E) loop
4418 Set_Is_Immediately_Visible (E, False);
4427 or else (Ekind (Curr_Unit) = E_Package_Body
4428 and then S = Spec_Entity (Curr_Unit))
4429 or else (Ekind (Curr_Unit) = E_Subprogram_Body
4432 (Unit_Declaration_Node (Curr_Unit)))
4436 -- Remove entities in current scopes from visibility, so that
4437 -- instance body is compiled in a clean environment.
4439 List := Save_Scope_Stack (Handle_Use => False);
4441 if Is_Child_Unit (S) then
4443 -- Remove child unit from stack, as well as inner scopes.
4444 -- Removing the context of a child unit removes parent units
4447 while Current_Scope /= S loop
4448 Num_Inner := Num_Inner + 1;
4449 Inner_Scopes (Num_Inner) := Current_Scope;
4454 Remove_Context (Curr_Comp);
4458 Remove_Context (Curr_Comp);
4461 if Ekind (Curr_Unit) = E_Package_Body then
4462 Remove_Context (Library_Unit (Curr_Comp));
4468 pragma Assert (Num_Inner < Num_Scopes);
4470 Push_Scope (Standard_Standard);
4471 Scope_Stack.Table (Scope_Stack.Last).Is_Active_Stack_Base := True;
4472 Instantiate_Package_Body
4475 Act_Decl => Act_Decl,
4476 Expander_Status => Expander_Active,
4477 Current_Sem_Unit => Current_Sem_Unit,
4478 Scope_Suppress => Scope_Suppress,
4479 Local_Suppress_Stack_Top => Local_Suppress_Stack_Top,
4480 Version => Ada_Version,
4481 Version_Pragma => Ada_Version_Pragma,
4482 Warnings => Save_Warnings,
4483 SPARK_Mode => SPARK_Mode,
4484 SPARK_Mode_Pragma => SPARK_Mode_Pragma)),
4485 Inlined_Body => True);
4491 Set_Is_Immediately_Visible (Gen_Comp, Vis);
4493 -- Reset Generic_Instance flag so that use clauses can be installed
4494 -- in the proper order. (See Use_One_Package for effect of enclosing
4495 -- instances on processing of use clauses).
4497 for J in 1 .. N_Instances loop
4498 Set_Is_Generic_Instance (Instances (J), False);
4502 Install_Context (Curr_Comp);
4504 if Present (Curr_Scope)
4505 and then Is_Child_Unit (Curr_Scope)
4507 Push_Scope (Curr_Scope);
4508 Set_Is_Immediately_Visible (Curr_Scope);
4510 -- Finally, restore inner scopes as well
4512 for J in reverse 1 .. Num_Inner loop
4513 Push_Scope (Inner_Scopes (J));
4517 Restore_Scope_Stack (List, Handle_Use => False);
4519 if Present (Curr_Scope)
4521 (In_Private_Part (Curr_Scope)
4522 or else In_Package_Body (Curr_Scope))
4524 -- Install private declaration of ancestor units, which are
4525 -- currently available. Restore_Scope_Stack and Install_Context
4526 -- only install the visible part of parents.
4531 Par := Scope (Curr_Scope);
4532 while (Present (Par))
4533 and then Par /= Standard_Standard
4535 Install_Private_Declarations (Par);
4542 -- Restore use clauses. For a child unit, use clauses in the parents
4543 -- are restored when installing the context, so only those in inner
4544 -- scopes (and those local to the child unit itself) need to be
4545 -- installed explicitly.
4547 if Is_Child_Unit (Curr_Unit)
4550 for J in reverse 1 .. Num_Inner + 1 loop
4551 Scope_Stack.Table (Scope_Stack.Last - J + 1).First_Use_Clause :=
4553 Install_Use_Clauses (Use_Clauses (J));
4557 for J in reverse 1 .. Num_Scopes loop
4558 Scope_Stack.Table (Scope_Stack.Last - J + 1).First_Use_Clause :=
4560 Install_Use_Clauses (Use_Clauses (J));
4564 -- Restore status of instances. If one of them is a body, make its
4565 -- local entities visible again.
4572 for J in 1 .. N_Instances loop
4573 Inst := Instances (J);
4574 Set_Is_Generic_Instance (Inst, True);
4576 if In_Package_Body (Inst)
4577 or else Ekind (S) = E_Procedure
4578 or else Ekind (S) = E_Function
4580 E := First_Entity (Instances (J));
4581 while Present (E) loop
4582 Set_Is_Immediately_Visible (E);
4589 -- If generic unit is in current unit, current context is correct
4592 Instantiate_Package_Body
4595 Act_Decl => Act_Decl,
4596 Expander_Status => Expander_Active,
4597 Current_Sem_Unit => Current_Sem_Unit,
4598 Scope_Suppress => Scope_Suppress,
4599 Local_Suppress_Stack_Top => Local_Suppress_Stack_Top,
4600 Version => Ada_Version,
4601 Version_Pragma => Ada_Version_Pragma,
4602 Warnings => Save_Warnings,
4603 SPARK_Mode => SPARK_Mode,
4604 SPARK_Mode_Pragma => SPARK_Mode_Pragma)),
4605 Inlined_Body => True);
4607 end Inline_Instance_Body;
4609 -------------------------------------
4610 -- Analyze_Procedure_Instantiation --
4611 -------------------------------------
4613 procedure Analyze_Procedure_Instantiation (N : Node_Id) is
4615 Analyze_Subprogram_Instantiation (N, E_Procedure);
4616 end Analyze_Procedure_Instantiation;
4618 -----------------------------------
4619 -- Need_Subprogram_Instance_Body --
4620 -----------------------------------
4622 function Need_Subprogram_Instance_Body
4624 Subp : Entity_Id) return Boolean
4627 -- Must be inlined (or inlined renaming)
4629 if (Is_In_Main_Unit (N)
4630 or else Is_Inlined (Subp)
4631 or else Is_Inlined (Alias (Subp)))
4633 -- Must be generating code or analyzing code in ASIS/GNATprove mode
4635 and then (Operating_Mode = Generate_Code
4636 or else (Operating_Mode = Check_Semantics
4637 and then (ASIS_Mode or GNATprove_Mode)))
4639 -- The body is needed when generating code (full expansion), in ASIS
4640 -- mode for other tools, and in GNATprove mode (special expansion) for
4641 -- formal verification of the body itself.
4643 and then (Expander_Active or ASIS_Mode or GNATprove_Mode)
4645 -- No point in inlining if ABE is inevitable
4647 and then not ABE_Is_Certain (N)
4649 -- Or if subprogram is eliminated
4651 and then not Is_Eliminated (Subp)
4653 Pending_Instantiations.Append
4655 Act_Decl => Unit_Declaration_Node (Subp),
4656 Expander_Status => Expander_Active,
4657 Current_Sem_Unit => Current_Sem_Unit,
4658 Scope_Suppress => Scope_Suppress,
4659 Local_Suppress_Stack_Top => Local_Suppress_Stack_Top,
4660 Version => Ada_Version,
4661 Version_Pragma => Ada_Version_Pragma,
4662 Warnings => Save_Warnings,
4663 SPARK_Mode => SPARK_Mode,
4664 SPARK_Mode_Pragma => SPARK_Mode_Pragma));
4667 -- Here if not inlined, or we ignore the inlining
4672 end Need_Subprogram_Instance_Body;
4674 --------------------------------------
4675 -- Analyze_Subprogram_Instantiation --
4676 --------------------------------------
4678 procedure Analyze_Subprogram_Instantiation
4682 Loc : constant Source_Ptr := Sloc (N);
4683 Gen_Id : constant Node_Id := Name (N);
4685 Anon_Id : constant Entity_Id :=
4686 Make_Defining_Identifier (Sloc (Defining_Entity (N)),
4687 Chars => New_External_Name
4688 (Chars (Defining_Entity (N)), 'R'));
4690 Act_Decl_Id : Entity_Id;
4695 Env_Installed : Boolean := False;
4696 Gen_Unit : Entity_Id;
4698 Pack_Id : Entity_Id;
4699 Parent_Installed : Boolean := False;
4700 Renaming_List : List_Id;
4702 procedure Analyze_Instance_And_Renamings;
4703 -- The instance must be analyzed in a context that includes the mappings
4704 -- of generic parameters into actuals. We create a package declaration
4705 -- for this purpose, and a subprogram with an internal name within the
4706 -- package. The subprogram instance is simply an alias for the internal
4707 -- subprogram, declared in the current scope.
4709 ------------------------------------
4710 -- Analyze_Instance_And_Renamings --
4711 ------------------------------------
4713 procedure Analyze_Instance_And_Renamings is
4714 Def_Ent : constant Entity_Id := Defining_Entity (N);
4715 Pack_Decl : Node_Id;
4718 if Nkind (Parent (N)) = N_Compilation_Unit then
4720 -- For the case of a compilation unit, the container package has
4721 -- the same name as the instantiation, to insure that the binder
4722 -- calls the elaboration procedure with the right name. Copy the
4723 -- entity of the instance, which may have compilation level flags
4724 -- (e.g. Is_Child_Unit) set.
4726 Pack_Id := New_Copy (Def_Ent);
4729 -- Otherwise we use the name of the instantiation concatenated
4730 -- with its source position to ensure uniqueness if there are
4731 -- several instantiations with the same name.
4734 Make_Defining_Identifier (Loc,
4735 Chars => New_External_Name
4736 (Related_Id => Chars (Def_Ent),
4738 Suffix_Index => Source_Offset (Sloc (Def_Ent))));
4741 Pack_Decl := Make_Package_Declaration (Loc,
4742 Specification => Make_Package_Specification (Loc,
4743 Defining_Unit_Name => Pack_Id,
4744 Visible_Declarations => Renaming_List,
4745 End_Label => Empty));
4747 Set_Instance_Spec (N, Pack_Decl);
4748 Set_Is_Generic_Instance (Pack_Id);
4749 Set_Debug_Info_Needed (Pack_Id);
4751 -- Case of not a compilation unit
4753 if Nkind (Parent (N)) /= N_Compilation_Unit then
4754 Mark_Rewrite_Insertion (Pack_Decl);
4755 Insert_Before (N, Pack_Decl);
4756 Set_Has_Completion (Pack_Id);
4758 -- Case of an instantiation that is a compilation unit
4760 -- Place declaration on current node so context is complete for
4761 -- analysis (including nested instantiations), and for use in a
4762 -- context_clause (see Analyze_With_Clause).
4765 Set_Unit (Parent (N), Pack_Decl);
4766 Set_Parent_Spec (Pack_Decl, Parent_Spec (N));
4769 Analyze (Pack_Decl);
4770 Check_Formal_Packages (Pack_Id);
4771 Set_Is_Generic_Instance (Pack_Id, False);
4773 -- Why do we clear Is_Generic_Instance??? We set it 20 lines
4776 -- Body of the enclosing package is supplied when instantiating the
4777 -- subprogram body, after semantic analysis is completed.
4779 if Nkind (Parent (N)) = N_Compilation_Unit then
4781 -- Remove package itself from visibility, so it does not
4782 -- conflict with subprogram.
4784 Set_Name_Entity_Id (Chars (Pack_Id), Homonym (Pack_Id));
4786 -- Set name and scope of internal subprogram so that the proper
4787 -- external name will be generated. The proper scope is the scope
4788 -- of the wrapper package. We need to generate debugging info for
4789 -- the internal subprogram, so set flag accordingly.
4791 Set_Chars (Anon_Id, Chars (Defining_Entity (N)));
4792 Set_Scope (Anon_Id, Scope (Pack_Id));
4794 -- Mark wrapper package as referenced, to avoid spurious warnings
4795 -- if the instantiation appears in various with_ clauses of
4796 -- subunits of the main unit.
4798 Set_Referenced (Pack_Id);
4801 Set_Is_Generic_Instance (Anon_Id);
4802 Set_Debug_Info_Needed (Anon_Id);
4803 Act_Decl_Id := New_Copy (Anon_Id);
4805 Set_Parent (Act_Decl_Id, Parent (Anon_Id));
4806 Set_Chars (Act_Decl_Id, Chars (Defining_Entity (N)));
4807 Set_Sloc (Act_Decl_Id, Sloc (Defining_Entity (N)));
4808 Set_Comes_From_Source (Act_Decl_Id, True);
4810 -- The signature may involve types that are not frozen yet, but the
4811 -- subprogram will be frozen at the point the wrapper package is
4812 -- frozen, so it does not need its own freeze node. In fact, if one
4813 -- is created, it might conflict with the freezing actions from the
4816 Set_Has_Delayed_Freeze (Anon_Id, False);
4818 -- If the instance is a child unit, mark the Id accordingly. Mark
4819 -- the anonymous entity as well, which is the real subprogram and
4820 -- which is used when the instance appears in a context clause.
4821 -- Similarly, propagate the Is_Eliminated flag to handle properly
4822 -- nested eliminated subprograms.
4824 Set_Is_Child_Unit (Act_Decl_Id, Is_Child_Unit (Defining_Entity (N)));
4825 Set_Is_Child_Unit (Anon_Id, Is_Child_Unit (Defining_Entity (N)));
4826 New_Overloaded_Entity (Act_Decl_Id);
4827 Check_Eliminated (Act_Decl_Id);
4828 Set_Is_Eliminated (Anon_Id, Is_Eliminated (Act_Decl_Id));
4830 -- In compilation unit case, kill elaboration checks on the
4831 -- instantiation, since they are never needed -- the body is
4832 -- instantiated at the same point as the spec.
4834 if Nkind (Parent (N)) = N_Compilation_Unit then
4835 Set_Suppress_Elaboration_Warnings (Act_Decl_Id);
4836 Set_Kill_Elaboration_Checks (Act_Decl_Id);
4837 Set_Is_Compilation_Unit (Anon_Id);
4839 Set_Cunit_Entity (Current_Sem_Unit, Pack_Id);
4842 -- The instance is not a freezing point for the new subprogram
4844 Set_Is_Frozen (Act_Decl_Id, False);
4846 if Nkind (Defining_Entity (N)) = N_Defining_Operator_Symbol then
4847 Valid_Operator_Definition (Act_Decl_Id);
4850 Set_Alias (Act_Decl_Id, Anon_Id);
4851 Set_Parent (Act_Decl_Id, Parent (Anon_Id));
4852 Set_Has_Completion (Act_Decl_Id);
4853 Set_Related_Instance (Pack_Id, Act_Decl_Id);
4855 if Nkind (Parent (N)) = N_Compilation_Unit then
4856 Set_Body_Required (Parent (N), False);
4858 end Analyze_Instance_And_Renamings;
4862 Vis_Prims_List : Elist_Id := No_Elist;
4863 -- List of primitives made temporarily visible in the instantiation
4864 -- to match the visibility of the formal type
4866 -- Start of processing for Analyze_Subprogram_Instantiation
4869 Check_SPARK_Restriction ("generic is not allowed", N);
4871 -- Very first thing: check for special Text_IO unit in case we are
4872 -- instantiating one of the children of [[Wide_]Wide_]Text_IO. Of course
4873 -- such an instantiation is bogus (these are packages, not subprograms),
4874 -- but we get a better error message if we do this.
4876 Check_Text_IO_Special_Unit (Gen_Id);
4878 -- Make node global for error reporting
4880 Instantiation_Node := N;
4882 -- For package instantiations we turn off style checks, because they
4883 -- will have been emitted in the generic. For subprogram instantiations
4884 -- we want to apply at least the check on overriding indicators so we
4885 -- do not modify the style check status.
4887 -- The renaming declarations for the actuals do not come from source and
4888 -- will not generate spurious warnings.
4890 Preanalyze_Actuals (N);
4893 Env_Installed := True;
4894 Check_Generic_Child_Unit (Gen_Id, Parent_Installed);
4895 Gen_Unit := Entity (Gen_Id);
4897 Generate_Reference (Gen_Unit, Gen_Id);
4899 if Nkind (Gen_Id) = N_Identifier
4900 and then Chars (Gen_Unit) = Chars (Defining_Entity (N))
4903 ("& is hidden within declaration of instance", Gen_Id, Gen_Unit);
4906 if Etype (Gen_Unit) = Any_Type then
4911 -- Verify that it is a generic subprogram of the right kind, and that
4912 -- it does not lead to a circular instantiation.
4914 if K = E_Procedure and then Ekind (Gen_Unit) /= E_Generic_Procedure then
4916 ("& is not the name of a generic procedure", Gen_Id, Gen_Unit);
4918 elsif K = E_Function and then Ekind (Gen_Unit) /= E_Generic_Function then
4920 ("& is not the name of a generic function", Gen_Id, Gen_Unit);
4922 elsif In_Open_Scopes (Gen_Unit) then
4923 Error_Msg_NE ("instantiation of & within itself", N, Gen_Unit);
4926 Set_Entity (Gen_Id, Gen_Unit);
4927 Set_Is_Instantiated (Gen_Unit);
4929 if In_Extended_Main_Source_Unit (N) then
4930 Generate_Reference (Gen_Unit, N);
4933 -- If renaming, get original unit
4935 if Present (Renamed_Object (Gen_Unit))
4936 and then (Ekind (Renamed_Object (Gen_Unit)) = E_Generic_Procedure
4938 Ekind (Renamed_Object (Gen_Unit)) = E_Generic_Function)
4940 Gen_Unit := Renamed_Object (Gen_Unit);
4941 Set_Is_Instantiated (Gen_Unit);
4942 Generate_Reference (Gen_Unit, N);
4945 if Contains_Instance_Of (Gen_Unit, Current_Scope, Gen_Id) then
4946 Error_Msg_Node_2 := Current_Scope;
4948 ("circular Instantiation: & instantiated in &!", N, Gen_Unit);
4949 Circularity_Detected := True;
4950 Restore_Hidden_Primitives (Vis_Prims_List);
4954 Gen_Decl := Unit_Declaration_Node (Gen_Unit);
4956 -- Initialize renamings map, for error checking
4958 Generic_Renamings.Set_Last (0);
4959 Generic_Renamings_HTable.Reset;
4961 Create_Instantiation_Source (N, Gen_Unit, False, S_Adjustment);
4963 -- Copy original generic tree, to produce text for instantiation
4967 (Original_Node (Gen_Decl), Empty, Instantiating => True);
4969 -- Inherit overriding indicator from instance node
4971 Act_Spec := Specification (Act_Tree);
4972 Set_Must_Override (Act_Spec, Must_Override (N));
4973 Set_Must_Not_Override (Act_Spec, Must_Not_Override (N));
4976 Analyze_Associations
4978 Formals => Generic_Formal_Declarations (Act_Tree),
4979 F_Copy => Generic_Formal_Declarations (Gen_Decl));
4981 Vis_Prims_List := Check_Hidden_Primitives (Renaming_List);
4983 -- The subprogram itself cannot contain a nested instance, so the
4984 -- current parent is left empty.
4986 Set_Instance_Env (Gen_Unit, Empty);
4988 -- Build the subprogram declaration, which does not appear in the
4989 -- generic template, and give it a sloc consistent with that of the
4992 Set_Defining_Unit_Name (Act_Spec, Anon_Id);
4993 Set_Generic_Parent (Act_Spec, Gen_Unit);
4995 Make_Subprogram_Declaration (Sloc (Act_Spec),
4996 Specification => Act_Spec);
4998 -- The aspects have been copied previously, but they have to be
4999 -- linked explicitly to the new subprogram declaration. Explicit
5000 -- pre/postconditions on the instance are analyzed below, in a
5003 Move_Aspects (Act_Tree, To => Act_Decl);
5004 Set_Categorization_From_Pragmas (Act_Decl);
5006 if Parent_Installed then
5010 Append (Act_Decl, Renaming_List);
5011 Analyze_Instance_And_Renamings;
5013 -- If the generic is marked Import (Intrinsic), then so is the
5014 -- instance. This indicates that there is no body to instantiate. If
5015 -- generic is marked inline, so it the instance, and the anonymous
5016 -- subprogram it renames. If inlined, or else if inlining is enabled
5017 -- for the compilation, we generate the instance body even if it is
5018 -- not within the main unit.
5020 if Is_Intrinsic_Subprogram (Gen_Unit) then
5021 Set_Is_Intrinsic_Subprogram (Anon_Id);
5022 Set_Is_Intrinsic_Subprogram (Act_Decl_Id);
5024 if Chars (Gen_Unit) = Name_Unchecked_Conversion then
5025 Validate_Unchecked_Conversion (N, Act_Decl_Id);
5029 -- Inherit convention from generic unit. Intrinsic convention, as for
5030 -- an instance of unchecked conversion, is not inherited because an
5031 -- explicit Ada instance has been created.
5033 if Has_Convention_Pragma (Gen_Unit)
5034 and then Convention (Gen_Unit) /= Convention_Intrinsic
5036 Set_Convention (Act_Decl_Id, Convention (Gen_Unit));
5037 Set_Is_Exported (Act_Decl_Id, Is_Exported (Gen_Unit));
5040 Generate_Definition (Act_Decl_Id);
5041 -- Set_Contract (Anon_Id, Make_Contract (Sloc (Anon_Id)));
5043 Set_Contract (Act_Decl_Id, Make_Contract (Sloc (Act_Decl_Id)));
5045 -- Inherit all inlining-related flags which apply to the generic in
5046 -- the subprogram and its declaration.
5048 Set_Is_Inlined (Act_Decl_Id, Is_Inlined (Gen_Unit));
5049 Set_Is_Inlined (Anon_Id, Is_Inlined (Gen_Unit));
5051 Set_Has_Pragma_Inline (Act_Decl_Id, Has_Pragma_Inline (Gen_Unit));
5052 Set_Has_Pragma_Inline (Anon_Id, Has_Pragma_Inline (Gen_Unit));
5054 Set_Has_Pragma_Inline_Always
5055 (Act_Decl_Id, Has_Pragma_Inline_Always (Gen_Unit));
5056 Set_Has_Pragma_Inline_Always
5057 (Anon_Id, Has_Pragma_Inline_Always (Gen_Unit));
5059 if not Is_Intrinsic_Subprogram (Gen_Unit) then
5060 Check_Elab_Instantiation (N);
5063 if Is_Dispatching_Operation (Act_Decl_Id)
5064 and then Ada_Version >= Ada_2005
5070 Formal := First_Formal (Act_Decl_Id);
5071 while Present (Formal) loop
5072 if Ekind (Etype (Formal)) = E_Anonymous_Access_Type
5073 and then Is_Controlling_Formal (Formal)
5074 and then not Can_Never_Be_Null (Formal)
5076 Error_Msg_NE ("access parameter& is controlling,",
5079 ("\corresponding parameter of & must be"
5080 & " explicitly null-excluding", N, Gen_Id);
5083 Next_Formal (Formal);
5088 Check_Hidden_Child_Unit (N, Gen_Unit, Act_Decl_Id);
5090 Validate_Categorization_Dependency (N, Act_Decl_Id);
5092 if not Is_Intrinsic_Subprogram (Act_Decl_Id) then
5093 Inherit_Context (Gen_Decl, N);
5095 Restore_Private_Views (Pack_Id, False);
5097 -- If the context requires a full instantiation, mark node for
5098 -- subsequent construction of the body.
5100 if Need_Subprogram_Instance_Body (N, Act_Decl_Id) then
5101 Check_Forward_Instantiation (Gen_Decl);
5103 -- The wrapper package is always delayed, because it does not
5104 -- constitute a freeze point, but to insure that the freeze
5105 -- node is placed properly, it is created directly when
5106 -- instantiating the body (otherwise the freeze node might
5107 -- appear to early for nested instantiations).
5109 elsif Nkind (Parent (N)) = N_Compilation_Unit then
5111 -- For ASIS purposes, indicate that the wrapper package has
5112 -- replaced the instantiation node.
5114 Rewrite (N, Unit (Parent (N)));
5115 Set_Unit (Parent (N), N);
5118 elsif Nkind (Parent (N)) = N_Compilation_Unit then
5120 -- Replace instance node for library-level instantiations of
5121 -- intrinsic subprograms, for ASIS use.
5123 Rewrite (N, Unit (Parent (N)));
5124 Set_Unit (Parent (N), N);
5127 if Parent_Installed then
5131 Restore_Hidden_Primitives (Vis_Prims_List);
5133 Env_Installed := False;
5134 Generic_Renamings.Set_Last (0);
5135 Generic_Renamings_HTable.Reset;
5139 if Has_Aspects (N) then
5140 Analyze_Aspect_Specifications (N, Act_Decl_Id);
5144 when Instantiation_Error =>
5145 if Parent_Installed then
5149 if Env_Installed then
5152 end Analyze_Subprogram_Instantiation;
5154 -------------------------
5155 -- Get_Associated_Node --
5156 -------------------------
5158 function Get_Associated_Node (N : Node_Id) return Node_Id is
5162 Assoc := Associated_Node (N);
5164 if Nkind (Assoc) /= Nkind (N) then
5167 elsif Nkind_In (Assoc, N_Aggregate, N_Extension_Aggregate) then
5171 -- If the node is part of an inner generic, it may itself have been
5172 -- remapped into a further generic copy. Associated_Node is otherwise
5173 -- used for the entity of the node, and will be of a different node
5174 -- kind, or else N has been rewritten as a literal or function call.
5176 while Present (Associated_Node (Assoc))
5177 and then Nkind (Associated_Node (Assoc)) = Nkind (Assoc)
5179 Assoc := Associated_Node (Assoc);
5182 -- Follow and additional link in case the final node was rewritten.
5183 -- This can only happen with nested generic units.
5185 if (Nkind (Assoc) = N_Identifier or else Nkind (Assoc) in N_Op)
5186 and then Present (Associated_Node (Assoc))
5187 and then (Nkind_In (Associated_Node (Assoc), N_Function_Call,
5188 N_Explicit_Dereference,
5193 Assoc := Associated_Node (Assoc);
5196 -- An additional special case: an unconstrained type in an object
5197 -- declaration may have been rewritten as a local subtype constrained
5198 -- by the expression in the declaration. We need to recover the
5199 -- original entity which may be global.
5201 if Present (Original_Node (Assoc))
5202 and then Nkind (Parent (N)) = N_Object_Declaration
5204 Assoc := Original_Node (Assoc);
5209 end Get_Associated_Node;
5211 -------------------------------------------
5212 -- Build_Instance_Compilation_Unit_Nodes --
5213 -------------------------------------------
5215 procedure Build_Instance_Compilation_Unit_Nodes
5220 Decl_Cunit : Node_Id;
5221 Body_Cunit : Node_Id;
5223 New_Main : constant Entity_Id := Defining_Entity (Act_Decl);
5224 Old_Main : constant Entity_Id := Cunit_Entity (Main_Unit);
5227 -- A new compilation unit node is built for the instance declaration
5230 Make_Compilation_Unit (Sloc (N),
5231 Context_Items => Empty_List,
5233 Aux_Decls_Node => Make_Compilation_Unit_Aux (Sloc (N)));
5235 Set_Parent_Spec (Act_Decl, Parent_Spec (N));
5237 -- The new compilation unit is linked to its body, but both share the
5238 -- same file, so we do not set Body_Required on the new unit so as not
5239 -- to create a spurious dependency on a non-existent body in the ali.
5240 -- This simplifies CodePeer unit traversal.
5242 -- We use the original instantiation compilation unit as the resulting
5243 -- compilation unit of the instance, since this is the main unit.
5245 Rewrite (N, Act_Body);
5247 -- Propagate the aspect specifications from the package body template to
5248 -- the instantiated version of the package body.
5250 if Has_Aspects (Act_Body) then
5251 Set_Aspect_Specifications
5252 (N, New_Copy_List_Tree (Aspect_Specifications (Act_Body)));
5255 Body_Cunit := Parent (N);
5257 -- The two compilation unit nodes are linked by the Library_Unit field
5259 Set_Library_Unit (Decl_Cunit, Body_Cunit);
5260 Set_Library_Unit (Body_Cunit, Decl_Cunit);
5262 -- Preserve the private nature of the package if needed
5264 Set_Private_Present (Decl_Cunit, Private_Present (Body_Cunit));
5266 -- If the instance is not the main unit, its context, categorization
5267 -- and elaboration entity are not relevant to the compilation.
5269 if Body_Cunit /= Cunit (Main_Unit) then
5270 Make_Instance_Unit (Body_Cunit, In_Main => False);
5274 -- The context clause items on the instantiation, which are now attached
5275 -- to the body compilation unit (since the body overwrote the original
5276 -- instantiation node), semantically belong on the spec, so copy them
5277 -- there. It's harmless to leave them on the body as well. In fact one
5278 -- could argue that they belong in both places.
5280 Citem := First (Context_Items (Body_Cunit));
5281 while Present (Citem) loop
5282 Append (New_Copy (Citem), Context_Items (Decl_Cunit));
5286 -- Propagate categorization flags on packages, so that they appear in
5287 -- the ali file for the spec of the unit.
5289 if Ekind (New_Main) = E_Package then
5290 Set_Is_Pure (Old_Main, Is_Pure (New_Main));
5291 Set_Is_Preelaborated (Old_Main, Is_Preelaborated (New_Main));
5292 Set_Is_Remote_Types (Old_Main, Is_Remote_Types (New_Main));
5293 Set_Is_Shared_Passive (Old_Main, Is_Shared_Passive (New_Main));
5294 Set_Is_Remote_Call_Interface
5295 (Old_Main, Is_Remote_Call_Interface (New_Main));
5298 -- Make entry in Units table, so that binder can generate call to
5299 -- elaboration procedure for body, if any.
5301 Make_Instance_Unit (Body_Cunit, In_Main => True);
5302 Main_Unit_Entity := New_Main;
5303 Set_Cunit_Entity (Main_Unit, Main_Unit_Entity);
5305 -- Build elaboration entity, since the instance may certainly generate
5306 -- elaboration code requiring a flag for protection.
5308 Build_Elaboration_Entity (Decl_Cunit, New_Main);
5309 end Build_Instance_Compilation_Unit_Nodes;
5311 -----------------------------
5312 -- Check_Access_Definition --
5313 -----------------------------
5315 procedure Check_Access_Definition (N : Node_Id) is
5318 (Ada_Version >= Ada_2005 and then Present (Access_Definition (N)));
5320 end Check_Access_Definition;
5322 -----------------------------------
5323 -- Check_Formal_Package_Instance --
5324 -----------------------------------
5326 -- If the formal has specific parameters, they must match those of the
5327 -- actual. Both of them are instances, and the renaming declarations for
5328 -- their formal parameters appear in the same order in both. The analyzed
5329 -- formal has been analyzed in the context of the current instance.
5331 procedure Check_Formal_Package_Instance
5332 (Formal_Pack : Entity_Id;
5333 Actual_Pack : Entity_Id)
5335 E1 : Entity_Id := First_Entity (Actual_Pack);
5336 E2 : Entity_Id := First_Entity (Formal_Pack);
5341 procedure Check_Mismatch (B : Boolean);
5342 -- Common error routine for mismatch between the parameters of the
5343 -- actual instance and those of the formal package.
5345 function Same_Instantiated_Constant (E1, E2 : Entity_Id) return Boolean;
5346 -- The formal may come from a nested formal package, and the actual may
5347 -- have been constant-folded. To determine whether the two denote the
5348 -- same entity we may have to traverse several definitions to recover
5349 -- the ultimate entity that they refer to.
5351 function Same_Instantiated_Variable (E1, E2 : Entity_Id) return Boolean;
5352 -- Similarly, if the formal comes from a nested formal package, the
5353 -- actual may designate the formal through multiple renamings, which
5354 -- have to be followed to determine the original variable in question.
5356 --------------------
5357 -- Check_Mismatch --
5358 --------------------
5360 procedure Check_Mismatch (B : Boolean) is
5361 Kind : constant Node_Kind := Nkind (Parent (E2));
5364 if Kind = N_Formal_Type_Declaration then
5367 elsif Nkind_In (Kind, N_Formal_Object_Declaration,
5368 N_Formal_Package_Declaration)
5369 or else Kind in N_Formal_Subprogram_Declaration
5375 ("actual for & in actual instance does not match formal",
5376 Parent (Actual_Pack), E1);
5380 --------------------------------
5381 -- Same_Instantiated_Constant --
5382 --------------------------------
5384 function Same_Instantiated_Constant
5385 (E1, E2 : Entity_Id) return Boolean
5391 while Present (Ent) loop
5395 elsif Ekind (Ent) /= E_Constant then
5398 elsif Is_Entity_Name (Constant_Value (Ent)) then
5399 if Entity (Constant_Value (Ent)) = E1 then
5402 Ent := Entity (Constant_Value (Ent));
5405 -- The actual may be a constant that has been folded. Recover
5408 elsif Is_Entity_Name (Original_Node (Constant_Value (Ent))) then
5409 Ent := Entity (Original_Node (Constant_Value (Ent)));
5416 end Same_Instantiated_Constant;
5418 --------------------------------
5419 -- Same_Instantiated_Variable --
5420 --------------------------------
5422 function Same_Instantiated_Variable
5423 (E1, E2 : Entity_Id) return Boolean
5425 function Original_Entity (E : Entity_Id) return Entity_Id;
5426 -- Follow chain of renamings to the ultimate ancestor
5428 ---------------------
5429 -- Original_Entity --
5430 ---------------------
5432 function Original_Entity (E : Entity_Id) return Entity_Id is
5437 while Nkind (Parent (Orig)) = N_Object_Renaming_Declaration
5438 and then Present (Renamed_Object (Orig))
5439 and then Is_Entity_Name (Renamed_Object (Orig))
5441 Orig := Entity (Renamed_Object (Orig));
5445 end Original_Entity;
5447 -- Start of processing for Same_Instantiated_Variable
5450 return Ekind (E1) = Ekind (E2)
5451 and then Original_Entity (E1) = Original_Entity (E2);
5452 end Same_Instantiated_Variable;
5454 -- Start of processing for Check_Formal_Package_Instance
5458 and then Present (E2)
5460 exit when Ekind (E1) = E_Package
5461 and then Renamed_Entity (E1) = Renamed_Entity (Actual_Pack);
5463 -- If the formal is the renaming of the formal package, this
5464 -- is the end of its formal part, which may occur before the
5465 -- end of the formal part in the actual in the presence of
5466 -- defaulted parameters in the formal package.
5468 exit when Nkind (Parent (E2)) = N_Package_Renaming_Declaration
5469 and then Renamed_Entity (E2) = Scope (E2);
5471 -- The analysis of the actual may generate additional internal
5472 -- entities. If the formal is defaulted, there is no corresponding
5473 -- analysis and the internal entities must be skipped, until we
5474 -- find corresponding entities again.
5476 if Comes_From_Source (E2)
5477 and then not Comes_From_Source (E1)
5478 and then Chars (E1) /= Chars (E2)
5481 and then Chars (E1) /= Chars (E2)
5490 -- If the formal entity comes from a formal declaration, it was
5491 -- defaulted in the formal package, and no check is needed on it.
5493 elsif Nkind (Parent (E2)) = N_Formal_Object_Declaration then
5496 -- Ditto for defaulted formal subprograms.
5498 elsif Is_Overloadable (E1)
5499 and then Nkind (Unit_Declaration_Node (E2)) in
5500 N_Formal_Subprogram_Declaration
5504 elsif Is_Type (E1) then
5506 -- Subtypes must statically match. E1, E2 are the local entities
5507 -- that are subtypes of the actuals. Itypes generated for other
5508 -- parameters need not be checked, the check will be performed
5509 -- on the parameters themselves.
5511 -- If E2 is a formal type declaration, it is a defaulted parameter
5512 -- and needs no checking.
5514 if not Is_Itype (E1)
5515 and then not Is_Itype (E2)
5519 or else Etype (E1) /= Etype (E2)
5520 or else not Subtypes_Statically_Match (E1, E2));
5523 elsif Ekind (E1) = E_Constant then
5525 -- IN parameters must denote the same static value, or the same
5526 -- constant, or the literal null.
5528 Expr1 := Expression (Parent (E1));
5530 if Ekind (E2) /= E_Constant then
5531 Check_Mismatch (True);
5534 Expr2 := Expression (Parent (E2));
5537 if Is_OK_Static_Expression (Expr1) then
5538 if not Is_OK_Static_Expression (Expr2) then
5539 Check_Mismatch (True);
5541 elsif Is_Discrete_Type (Etype (E1)) then
5543 V1 : constant Uint := Expr_Value (Expr1);
5544 V2 : constant Uint := Expr_Value (Expr2);
5546 Check_Mismatch (V1 /= V2);
5549 elsif Is_Real_Type (Etype (E1)) then
5551 V1 : constant Ureal := Expr_Value_R (Expr1);
5552 V2 : constant Ureal := Expr_Value_R (Expr2);
5554 Check_Mismatch (V1 /= V2);
5557 elsif Is_String_Type (Etype (E1))
5558 and then Nkind (Expr1) = N_String_Literal
5560 if Nkind (Expr2) /= N_String_Literal then
5561 Check_Mismatch (True);
5564 (not String_Equal (Strval (Expr1), Strval (Expr2)));
5568 elsif Is_Entity_Name (Expr1) then
5569 if Is_Entity_Name (Expr2) then
5570 if Entity (Expr1) = Entity (Expr2) then
5574 (not Same_Instantiated_Constant
5575 (Entity (Expr1), Entity (Expr2)));
5578 Check_Mismatch (True);
5581 elsif Is_Entity_Name (Original_Node (Expr1))
5582 and then Is_Entity_Name (Expr2)
5584 Same_Instantiated_Constant
5585 (Entity (Original_Node (Expr1)), Entity (Expr2))
5589 elsif Nkind (Expr1) = N_Null then
5590 Check_Mismatch (Nkind (Expr1) /= N_Null);
5593 Check_Mismatch (True);
5596 elsif Ekind (E1) = E_Variable then
5597 Check_Mismatch (not Same_Instantiated_Variable (E1, E2));
5599 elsif Ekind (E1) = E_Package then
5601 (Ekind (E1) /= Ekind (E2)
5602 or else Renamed_Object (E1) /= Renamed_Object (E2));
5604 elsif Is_Overloadable (E1) then
5606 -- Verify that the actual subprograms match. Note that actuals
5607 -- that are attributes are rewritten as subprograms. If the
5608 -- subprogram in the formal package is defaulted, no check is
5609 -- needed. Note that this can only happen in Ada 2005 when the
5610 -- formal package can be partially parameterized.
5612 if Nkind (Unit_Declaration_Node (E1)) =
5613 N_Subprogram_Renaming_Declaration
5614 and then From_Default (Unit_Declaration_Node (E1))
5618 -- If the formal package has an "others" box association that
5619 -- covers this formal, there is no need for a check either.
5621 elsif Nkind (Unit_Declaration_Node (E2)) in
5622 N_Formal_Subprogram_Declaration
5623 and then Box_Present (Unit_Declaration_Node (E2))
5627 -- No check needed if subprogram is a defaulted null procedure
5629 elsif No (Alias (E2))
5630 and then Ekind (E2) = E_Procedure
5632 Null_Present (Specification (Unit_Declaration_Node (E2)))
5636 -- Otherwise the actual in the formal and the actual in the
5637 -- instantiation of the formal must match, up to renamings.
5641 (Ekind (E2) /= Ekind (E1) or else (Alias (E1)) /= Alias (E2));
5645 raise Program_Error;
5652 end Check_Formal_Package_Instance;
5654 ---------------------------
5655 -- Check_Formal_Packages --
5656 ---------------------------
5658 procedure Check_Formal_Packages (P_Id : Entity_Id) is
5660 Formal_P : Entity_Id;
5663 -- Iterate through the declarations in the instance, looking for package
5664 -- renaming declarations that denote instances of formal packages. Stop
5665 -- when we find the renaming of the current package itself. The
5666 -- declaration for a formal package without a box is followed by an
5667 -- internal entity that repeats the instantiation.
5669 E := First_Entity (P_Id);
5670 while Present (E) loop
5671 if Ekind (E) = E_Package then
5672 if Renamed_Object (E) = P_Id then
5675 elsif Nkind (Parent (E)) /= N_Package_Renaming_Declaration then
5678 elsif not Box_Present (Parent (Associated_Formal_Package (E))) then
5679 Formal_P := Next_Entity (E);
5680 Check_Formal_Package_Instance (Formal_P, E);
5682 -- After checking, remove the internal validating package. It
5683 -- is only needed for semantic checks, and as it may contain
5684 -- generic formal declarations it should not reach gigi.
5686 Remove (Unit_Declaration_Node (Formal_P));
5692 end Check_Formal_Packages;
5694 ---------------------------------
5695 -- Check_Forward_Instantiation --
5696 ---------------------------------
5698 procedure Check_Forward_Instantiation (Decl : Node_Id) is
5700 Gen_Comp : Entity_Id := Cunit_Entity (Get_Source_Unit (Decl));
5703 -- The instantiation appears before the generic body if we are in the
5704 -- scope of the unit containing the generic, either in its spec or in
5705 -- the package body, and before the generic body.
5707 if Ekind (Gen_Comp) = E_Package_Body then
5708 Gen_Comp := Spec_Entity (Gen_Comp);
5711 if In_Open_Scopes (Gen_Comp)
5712 and then No (Corresponding_Body (Decl))
5717 and then not Is_Compilation_Unit (S)
5718 and then not Is_Child_Unit (S)
5720 if Ekind (S) = E_Package then
5721 Set_Has_Forward_Instantiation (S);
5727 end Check_Forward_Instantiation;
5729 ---------------------------
5730 -- Check_Generic_Actuals --
5731 ---------------------------
5733 -- The visibility of the actuals may be different between the point of
5734 -- generic instantiation and the instantiation of the body.
5736 procedure Check_Generic_Actuals
5737 (Instance : Entity_Id;
5738 Is_Formal_Box : Boolean)
5743 function Denotes_Previous_Actual (Typ : Entity_Id) return Boolean;
5744 -- For a formal that is an array type, the component type is often a
5745 -- previous formal in the same unit. The privacy status of the component
5746 -- type will have been examined earlier in the traversal of the
5747 -- corresponding actuals, and this status should not be modified for
5748 -- the array (sub)type itself. However, if the base type of the array
5749 -- (sub)type is private, its full view must be restored in the body to
5750 -- be consistent with subsequent index subtypes, etc.
5752 -- To detect this case we have to rescan the list of formals, which is
5753 -- usually short enough to ignore the resulting inefficiency.
5755 -----------------------------
5756 -- Denotes_Previous_Actual --
5757 -----------------------------
5759 function Denotes_Previous_Actual (Typ : Entity_Id) return Boolean is
5763 Prev := First_Entity (Instance);
5764 while Present (Prev) loop
5766 and then Nkind (Parent (Prev)) = N_Subtype_Declaration
5767 and then Is_Entity_Name (Subtype_Indication (Parent (Prev)))
5768 and then Entity (Subtype_Indication (Parent (Prev))) = Typ
5781 end Denotes_Previous_Actual;
5783 -- Start of processing for Check_Generic_Actuals
5786 E := First_Entity (Instance);
5787 while Present (E) loop
5789 and then Nkind (Parent (E)) = N_Subtype_Declaration
5790 and then Scope (Etype (E)) /= Instance
5791 and then Is_Entity_Name (Subtype_Indication (Parent (E)))
5793 if Is_Array_Type (E)
5794 and then not Is_Private_Type (Etype (E))
5795 and then Denotes_Previous_Actual (Component_Type (E))
5799 Check_Private_View (Subtype_Indication (Parent (E)));
5802 Set_Is_Generic_Actual_Type (E, True);
5803 Set_Is_Hidden (E, False);
5804 Set_Is_Potentially_Use_Visible (E,
5807 -- We constructed the generic actual type as a subtype of the
5808 -- supplied type. This means that it normally would not inherit
5809 -- subtype specific attributes of the actual, which is wrong for
5810 -- the generic case.
5812 Astype := Ancestor_Subtype (E);
5816 -- This can happen when E is an itype that is the full view of
5817 -- a private type completed, e.g. with a constrained array. In
5818 -- that case, use the first subtype, which will carry size
5819 -- information. The base type itself is unconstrained and will
5822 Astype := First_Subtype (E);
5825 Set_Size_Info (E, (Astype));
5826 Set_RM_Size (E, RM_Size (Astype));
5827 Set_First_Rep_Item (E, First_Rep_Item (Astype));
5829 if Is_Discrete_Or_Fixed_Point_Type (E) then
5830 Set_RM_Size (E, RM_Size (Astype));
5832 -- In nested instances, the base type of an access actual may
5833 -- itself be private, and need to be exchanged.
5835 elsif Is_Access_Type (E)
5836 and then Is_Private_Type (Etype (E))
5839 (New_Occurrence_Of (Etype (E), Sloc (Instance)));
5842 elsif Ekind (E) = E_Package then
5844 -- If this is the renaming for the current instance, we're done.
5845 -- Otherwise it is a formal package. If the corresponding formal
5846 -- was declared with a box, the (instantiations of the) generic
5847 -- formal part are also visible. Otherwise, ignore the entity
5848 -- created to validate the actuals.
5850 if Renamed_Object (E) = Instance then
5853 elsif Nkind (Parent (E)) /= N_Package_Renaming_Declaration then
5856 -- The visibility of a formal of an enclosing generic is already
5859 elsif Denotes_Formal_Package (E) then
5862 elsif Present (Associated_Formal_Package (E))
5863 and then not Is_Generic_Formal (E)
5865 if Box_Present (Parent (Associated_Formal_Package (E))) then
5866 Check_Generic_Actuals (Renamed_Object (E), True);
5869 Check_Generic_Actuals (Renamed_Object (E), False);
5872 Set_Is_Hidden (E, False);
5875 -- If this is a subprogram instance (in a wrapper package) the
5876 -- actual is fully visible.
5878 elsif Is_Wrapper_Package (Instance) then
5879 Set_Is_Hidden (E, False);
5881 -- If the formal package is declared with a box, or if the formal
5882 -- parameter is defaulted, it is visible in the body.
5885 or else Is_Visible_Formal (E)
5887 Set_Is_Hidden (E, False);
5890 if Ekind (E) = E_Constant then
5892 -- If the type of the actual is a private type declared in the
5893 -- enclosing scope of the generic unit, the body of the generic
5894 -- sees the full view of the type (because it has to appear in
5895 -- the corresponding package body). If the type is private now,
5896 -- exchange views to restore the proper visiblity in the instance.
5899 Typ : constant Entity_Id := Base_Type (Etype (E));
5900 -- The type of the actual
5905 Parent_Scope : Entity_Id;
5906 -- The enclosing scope of the generic unit
5909 if Is_Wrapper_Package (Instance) then
5913 (Unit_Declaration_Node
5914 (Related_Instance (Instance))));
5917 Generic_Parent (Package_Specification (Instance));
5920 Parent_Scope := Scope (Gen_Id);
5922 -- The exchange is only needed if the generic is defined
5923 -- within a package which is not a common ancestor of the
5924 -- scope of the instance, and is not already in scope.
5926 if Is_Private_Type (Typ)
5927 and then Scope (Typ) = Parent_Scope
5928 and then Scope (Instance) /= Parent_Scope
5929 and then Ekind (Parent_Scope) = E_Package
5930 and then not Is_Child_Unit (Gen_Id)
5934 -- If the type of the entity is a subtype, it may also have
5935 -- to be made visible, together with the base type of its
5936 -- full view, after exchange.
5938 if Is_Private_Type (Etype (E)) then
5939 Switch_View (Etype (E));
5940 Switch_View (Base_Type (Etype (E)));
5948 end Check_Generic_Actuals;
5950 ------------------------------
5951 -- Check_Generic_Child_Unit --
5952 ------------------------------
5954 procedure Check_Generic_Child_Unit
5956 Parent_Installed : in out Boolean)
5958 Loc : constant Source_Ptr := Sloc (Gen_Id);
5959 Gen_Par : Entity_Id := Empty;
5961 Inst_Par : Entity_Id;
5964 function Find_Generic_Child
5966 Id : Node_Id) return Entity_Id;
5967 -- Search generic parent for possible child unit with the given name
5969 function In_Enclosing_Instance return Boolean;
5970 -- Within an instance of the parent, the child unit may be denoted by
5971 -- a simple name, or an abbreviated expanded name. Examine enclosing
5972 -- scopes to locate a possible parent instantiation.
5974 ------------------------
5975 -- Find_Generic_Child --
5976 ------------------------
5978 function Find_Generic_Child
5980 Id : Node_Id) return Entity_Id
5985 -- If entity of name is already set, instance has already been
5986 -- resolved, e.g. in an enclosing instantiation.
5988 if Present (Entity (Id)) then
5989 if Scope (Entity (Id)) = Scop then
5996 E := First_Entity (Scop);
5997 while Present (E) loop
5998 if Chars (E) = Chars (Id)
5999 and then Is_Child_Unit (E)
6001 if Is_Child_Unit (E)
6002 and then not Is_Visible_Lib_Unit (E)
6005 ("generic child unit& is not visible", Gen_Id, E);
6017 end Find_Generic_Child;
6019 ---------------------------
6020 -- In_Enclosing_Instance --
6021 ---------------------------
6023 function In_Enclosing_Instance return Boolean is
6024 Enclosing_Instance : Node_Id;
6025 Instance_Decl : Node_Id;
6028 -- We do not inline any call that contains instantiations, except
6029 -- for instantiations of Unchecked_Conversion, so if we are within
6030 -- an inlined body the current instance does not require parents.
6032 if In_Inlined_Body then
6033 pragma Assert (Chars (Gen_Id) = Name_Unchecked_Conversion);
6037 -- Loop to check enclosing scopes
6039 Enclosing_Instance := Current_Scope;
6040 while Present (Enclosing_Instance) loop
6041 Instance_Decl := Unit_Declaration_Node (Enclosing_Instance);
6043 if Ekind (Enclosing_Instance) = E_Package
6044 and then Is_Generic_Instance (Enclosing_Instance)
6046 (Generic_Parent (Specification (Instance_Decl)))
6048 -- Check whether the generic we are looking for is a child of
6051 E := Find_Generic_Child
6052 (Generic_Parent (Specification (Instance_Decl)), Gen_Id);
6053 exit when Present (E);
6059 Enclosing_Instance := Scope (Enclosing_Instance);
6071 Make_Expanded_Name (Loc,
6073 Prefix => New_Occurrence_Of (Enclosing_Instance, Loc),
6074 Selector_Name => New_Occurrence_Of (E, Loc)));
6076 Set_Entity (Gen_Id, E);
6077 Set_Etype (Gen_Id, Etype (E));
6078 Parent_Installed := False; -- Already in scope.
6081 end In_Enclosing_Instance;
6083 -- Start of processing for Check_Generic_Child_Unit
6086 -- If the name of the generic is given by a selected component, it may
6087 -- be the name of a generic child unit, and the prefix is the name of an
6088 -- instance of the parent, in which case the child unit must be visible.
6089 -- If this instance is not in scope, it must be placed there and removed
6090 -- after instantiation, because what is being instantiated is not the
6091 -- original child, but the corresponding child present in the instance
6094 -- If the child is instantiated within the parent, it can be given by
6095 -- a simple name. In this case the instance is already in scope, but
6096 -- the child generic must be recovered from the generic parent as well.
6098 if Nkind (Gen_Id) = N_Selected_Component then
6099 S := Selector_Name (Gen_Id);
6100 Analyze (Prefix (Gen_Id));
6101 Inst_Par := Entity (Prefix (Gen_Id));
6103 if Ekind (Inst_Par) = E_Package
6104 and then Present (Renamed_Object (Inst_Par))
6106 Inst_Par := Renamed_Object (Inst_Par);
6109 if Ekind (Inst_Par) = E_Package then
6110 if Nkind (Parent (Inst_Par)) = N_Package_Specification then
6111 Gen_Par := Generic_Parent (Parent (Inst_Par));
6113 elsif Nkind (Parent (Inst_Par)) = N_Defining_Program_Unit_Name
6115 Nkind (Parent (Parent (Inst_Par))) = N_Package_Specification
6117 Gen_Par := Generic_Parent (Parent (Parent (Inst_Par)));
6120 elsif Ekind (Inst_Par) = E_Generic_Package
6121 and then Nkind (Parent (Gen_Id)) = N_Formal_Package_Declaration
6123 -- A formal package may be a real child package, and not the
6124 -- implicit instance within a parent. In this case the child is
6125 -- not visible and has to be retrieved explicitly as well.
6127 Gen_Par := Inst_Par;
6130 if Present (Gen_Par) then
6132 -- The prefix denotes an instantiation. The entity itself may be a
6133 -- nested generic, or a child unit.
6135 E := Find_Generic_Child (Gen_Par, S);
6138 Change_Selected_Component_To_Expanded_Name (Gen_Id);
6139 Set_Entity (Gen_Id, E);
6140 Set_Etype (Gen_Id, Etype (E));
6142 Set_Etype (S, Etype (E));
6144 -- Indicate that this is a reference to the parent
6146 if In_Extended_Main_Source_Unit (Gen_Id) then
6147 Set_Is_Instantiated (Inst_Par);
6150 -- A common mistake is to replicate the naming scheme of a
6151 -- hierarchy by instantiating a generic child directly, rather
6152 -- than the implicit child in a parent instance:
6154 -- generic .. package Gpar is ..
6155 -- generic .. package Gpar.Child is ..
6156 -- package Par is new Gpar ();
6159 -- package Par.Child is new Gpar.Child ();
6160 -- rather than Par.Child
6162 -- In this case the instantiation is within Par, which is an
6163 -- instance, but Gpar does not denote Par because we are not IN
6164 -- the instance of Gpar, so this is illegal. The test below
6165 -- recognizes this particular case.
6167 if Is_Child_Unit (E)
6168 and then not Comes_From_Source (Entity (Prefix (Gen_Id)))
6169 and then (not In_Instance
6170 or else Nkind (Parent (Parent (Gen_Id))) =
6174 ("prefix of generic child unit must be instance of parent",
6178 if not In_Open_Scopes (Inst_Par)
6179 and then Nkind (Parent (Gen_Id)) not in
6180 N_Generic_Renaming_Declaration
6182 Install_Parent (Inst_Par);
6183 Parent_Installed := True;
6185 elsif In_Open_Scopes (Inst_Par) then
6187 -- If the parent is already installed, install the actuals
6188 -- for its formal packages. This is necessary when the child
6189 -- instance is a child of the parent instance: in this case,
6190 -- the parent is placed on the scope stack but the formal
6191 -- packages are not made visible.
6193 Install_Formal_Packages (Inst_Par);
6197 -- If the generic parent does not contain an entity that
6198 -- corresponds to the selector, the instance doesn't either.
6199 -- Analyzing the node will yield the appropriate error message.
6200 -- If the entity is not a child unit, then it is an inner
6201 -- generic in the parent.
6209 if Is_Child_Unit (Entity (Gen_Id))
6211 Nkind (Parent (Gen_Id)) not in N_Generic_Renaming_Declaration
6212 and then not In_Open_Scopes (Inst_Par)
6214 Install_Parent (Inst_Par);
6215 Parent_Installed := True;
6217 -- The generic unit may be the renaming of the implicit child
6218 -- present in an instance. In that case the parent instance is
6219 -- obtained from the name of the renamed entity.
6221 elsif Ekind (Entity (Gen_Id)) = E_Generic_Package
6222 and then Present (Renamed_Entity (Entity (Gen_Id)))
6223 and then Is_Child_Unit (Renamed_Entity (Entity (Gen_Id)))
6226 Renamed_Package : constant Node_Id :=
6227 Name (Parent (Entity (Gen_Id)));
6229 if Nkind (Renamed_Package) = N_Expanded_Name then
6230 Inst_Par := Entity (Prefix (Renamed_Package));
6231 Install_Parent (Inst_Par);
6232 Parent_Installed := True;
6238 elsif Nkind (Gen_Id) = N_Expanded_Name then
6240 -- Entity already present, analyze prefix, whose meaning may be
6241 -- an instance in the current context. If it is an instance of
6242 -- a relative within another, the proper parent may still have
6243 -- to be installed, if they are not of the same generation.
6245 Analyze (Prefix (Gen_Id));
6247 -- In the unlikely case that a local declaration hides the name
6248 -- of the parent package, locate it on the homonym chain. If the
6249 -- context is an instance of the parent, the renaming entity is
6252 Inst_Par := Entity (Prefix (Gen_Id));
6253 while Present (Inst_Par)
6254 and then not Is_Package_Or_Generic_Package (Inst_Par)
6256 Inst_Par := Homonym (Inst_Par);
6259 pragma Assert (Present (Inst_Par));
6260 Set_Entity (Prefix (Gen_Id), Inst_Par);
6262 if In_Enclosing_Instance then
6265 elsif Present (Entity (Gen_Id))
6266 and then Is_Child_Unit (Entity (Gen_Id))
6267 and then not In_Open_Scopes (Inst_Par)
6269 Install_Parent (Inst_Par);
6270 Parent_Installed := True;
6273 elsif In_Enclosing_Instance then
6275 -- The child unit is found in some enclosing scope
6282 -- If this is the renaming of the implicit child in a parent
6283 -- instance, recover the parent name and install it.
6285 if Is_Entity_Name (Gen_Id) then
6286 E := Entity (Gen_Id);
6288 if Is_Generic_Unit (E)
6289 and then Nkind (Parent (E)) in N_Generic_Renaming_Declaration
6290 and then Is_Child_Unit (Renamed_Object (E))
6291 and then Is_Generic_Unit (Scope (Renamed_Object (E)))
6292 and then Nkind (Name (Parent (E))) = N_Expanded_Name
6295 New_Copy_Tree (Name (Parent (E))));
6296 Inst_Par := Entity (Prefix (Gen_Id));
6298 if not In_Open_Scopes (Inst_Par) then
6299 Install_Parent (Inst_Par);
6300 Parent_Installed := True;
6303 -- If it is a child unit of a non-generic parent, it may be
6304 -- use-visible and given by a direct name. Install parent as
6307 elsif Is_Generic_Unit (E)
6308 and then Is_Child_Unit (E)
6310 Nkind (Parent (Gen_Id)) not in N_Generic_Renaming_Declaration
6311 and then not Is_Generic_Unit (Scope (E))
6313 if not In_Open_Scopes (Scope (E)) then
6314 Install_Parent (Scope (E));
6315 Parent_Installed := True;
6320 end Check_Generic_Child_Unit;
6322 -----------------------------
6323 -- Check_Hidden_Child_Unit --
6324 -----------------------------
6326 procedure Check_Hidden_Child_Unit
6328 Gen_Unit : Entity_Id;
6329 Act_Decl_Id : Entity_Id)
6331 Gen_Id : constant Node_Id := Name (N);
6334 if Is_Child_Unit (Gen_Unit)
6335 and then Is_Child_Unit (Act_Decl_Id)
6336 and then Nkind (Gen_Id) = N_Expanded_Name
6337 and then Entity (Prefix (Gen_Id)) = Scope (Act_Decl_Id)
6338 and then Chars (Gen_Unit) = Chars (Act_Decl_Id)
6340 Error_Msg_Node_2 := Scope (Act_Decl_Id);
6342 ("generic unit & is implicitly declared in &",
6343 Defining_Unit_Name (N), Gen_Unit);
6344 Error_Msg_N ("\instance must have different name",
6345 Defining_Unit_Name (N));
6347 end Check_Hidden_Child_Unit;
6349 ------------------------
6350 -- Check_Private_View --
6351 ------------------------
6353 procedure Check_Private_View (N : Node_Id) is
6354 T : constant Entity_Id := Etype (N);
6358 -- Exchange views if the type was not private in the generic but is
6359 -- private at the point of instantiation. Do not exchange views if
6360 -- the scope of the type is in scope. This can happen if both generic
6361 -- and instance are sibling units, or if type is defined in a parent.
6362 -- In this case the visibility of the type will be correct for all
6366 BT := Base_Type (T);
6368 if Is_Private_Type (T)
6369 and then not Has_Private_View (N)
6370 and then Present (Full_View (T))
6371 and then not In_Open_Scopes (Scope (T))
6373 -- In the generic, the full type was visible. Save the private
6374 -- entity, for subsequent exchange.
6378 elsif Has_Private_View (N)
6379 and then not Is_Private_Type (T)
6380 and then not Has_Been_Exchanged (T)
6381 and then Etype (Get_Associated_Node (N)) /= T
6383 -- Only the private declaration was visible in the generic. If
6384 -- the type appears in a subtype declaration, the subtype in the
6385 -- instance must have a view compatible with that of its parent,
6386 -- which must be exchanged (see corresponding code in Restore_
6387 -- Private_Views). Otherwise, if the type is defined in a parent
6388 -- unit, leave full visibility within instance, which is safe.
6390 if In_Open_Scopes (Scope (Base_Type (T)))
6391 and then not Is_Private_Type (Base_Type (T))
6392 and then Comes_From_Source (Base_Type (T))
6396 elsif Nkind (Parent (N)) = N_Subtype_Declaration
6397 or else not In_Private_Part (Scope (Base_Type (T)))
6399 Prepend_Elmt (T, Exchanged_Views);
6400 Exchange_Declarations (Etype (Get_Associated_Node (N)));
6403 -- For composite types with inconsistent representation exchange
6404 -- component types accordingly.
6406 elsif Is_Access_Type (T)
6407 and then Is_Private_Type (Designated_Type (T))
6408 and then not Has_Private_View (N)
6409 and then Present (Full_View (Designated_Type (T)))
6411 Switch_View (Designated_Type (T));
6413 elsif Is_Array_Type (T) then
6414 if Is_Private_Type (Component_Type (T))
6415 and then not Has_Private_View (N)
6416 and then Present (Full_View (Component_Type (T)))
6418 Switch_View (Component_Type (T));
6421 -- The normal exchange mechanism relies on the setting of a
6422 -- flag on the reference in the generic. However, an additional
6423 -- mechanism is needed for types that are not explicitly
6424 -- mentioned in the generic, but may be needed in expanded code
6425 -- in the instance. This includes component types of arrays and
6426 -- designated types of access types. This processing must also
6427 -- include the index types of arrays which we take care of here.
6434 Indx := First_Index (T);
6435 while Present (Indx) loop
6436 Typ := Base_Type (Etype (Indx));
6438 if Is_Private_Type (Typ)
6439 and then Present (Full_View (Typ))
6448 elsif Is_Private_Type (T)
6449 and then Present (Full_View (T))
6450 and then Is_Array_Type (Full_View (T))
6451 and then Is_Private_Type (Component_Type (Full_View (T)))
6455 -- Finally, a non-private subtype may have a private base type, which
6456 -- must be exchanged for consistency. This can happen when a package
6457 -- body is instantiated, when the scope stack is empty but in fact
6458 -- the subtype and the base type are declared in an enclosing scope.
6460 -- Note that in this case we introduce an inconsistency in the view
6461 -- set, because we switch the base type BT, but there could be some
6462 -- private dependent subtypes of BT which remain unswitched. Such
6463 -- subtypes might need to be switched at a later point (see specific
6464 -- provision for that case in Switch_View).
6466 elsif not Is_Private_Type (T)
6467 and then not Has_Private_View (N)
6468 and then Is_Private_Type (BT)
6469 and then Present (Full_View (BT))
6470 and then not Is_Generic_Type (BT)
6471 and then not In_Open_Scopes (BT)
6473 Prepend_Elmt (Full_View (BT), Exchanged_Views);
6474 Exchange_Declarations (BT);
6477 end Check_Private_View;
6479 -----------------------------
6480 -- Check_Hidden_Primitives --
6481 -----------------------------
6483 function Check_Hidden_Primitives (Assoc_List : List_Id) return Elist_Id is
6486 Result : Elist_Id := No_Elist;
6489 if No (Assoc_List) then
6493 -- Traverse the list of associations between formals and actuals
6494 -- searching for renamings of tagged types
6496 Actual := First (Assoc_List);
6497 while Present (Actual) loop
6498 if Nkind (Actual) = N_Subtype_Declaration then
6499 Gen_T := Generic_Parent_Type (Actual);
6502 and then Is_Tagged_Type (Gen_T)
6504 -- Traverse the list of primitives of the actual types
6505 -- searching for hidden primitives that are visible in the
6506 -- corresponding generic formal; leave them visible and
6507 -- append them to Result to restore their decoration later.
6509 Install_Hidden_Primitives
6510 (Prims_List => Result,
6512 Act_T => Entity (Subtype_Indication (Actual)));
6520 end Check_Hidden_Primitives;
6522 --------------------------
6523 -- Contains_Instance_Of --
6524 --------------------------
6526 function Contains_Instance_Of
6529 N : Node_Id) return Boolean
6537 -- Verify that there are no circular instantiations. We check whether
6538 -- the unit contains an instance of the current scope or some enclosing
6539 -- scope (in case one of the instances appears in a subunit). Longer
6540 -- circularities involving subunits might seem too pathological to
6541 -- consider, but they were not too pathological for the authors of
6542 -- DEC bc30vsq, so we loop over all enclosing scopes, and mark all
6543 -- enclosing generic scopes as containing an instance.
6546 -- Within a generic subprogram body, the scope is not generic, to
6547 -- allow for recursive subprograms. Use the declaration to determine
6548 -- whether this is a generic unit.
6550 if Ekind (Scop) = E_Generic_Package
6551 or else (Is_Subprogram (Scop)
6552 and then Nkind (Unit_Declaration_Node (Scop)) =
6553 N_Generic_Subprogram_Declaration)
6555 Elmt := First_Elmt (Inner_Instances (Inner));
6557 while Present (Elmt) loop
6558 if Node (Elmt) = Scop then
6559 Error_Msg_Node_2 := Inner;
6561 ("circular Instantiation: & instantiated within &!",
6565 elsif Node (Elmt) = Inner then
6568 elsif Contains_Instance_Of (Node (Elmt), Scop, N) then
6569 Error_Msg_Node_2 := Inner;
6571 ("circular Instantiation: & instantiated within &!",
6579 -- Indicate that Inner is being instantiated within Scop
6581 Append_Elmt (Inner, Inner_Instances (Scop));
6584 if Scop = Standard_Standard then
6587 Scop := Scope (Scop);
6592 end Contains_Instance_Of;
6594 -----------------------
6595 -- Copy_Generic_Node --
6596 -----------------------
6598 function Copy_Generic_Node
6600 Parent_Id : Node_Id;
6601 Instantiating : Boolean) return Node_Id
6606 function Copy_Generic_Descendant (D : Union_Id) return Union_Id;
6607 -- Check the given value of one of the Fields referenced by the current
6608 -- node to determine whether to copy it recursively. The field may hold
6609 -- a Node_Id, a List_Id, or an Elist_Id, or a plain value (Sloc, Uint,
6610 -- Char) in which case it need not be copied.
6612 procedure Copy_Descendants;
6613 -- Common utility for various nodes
6615 function Copy_Generic_Elist (E : Elist_Id) return Elist_Id;
6616 -- Make copy of element list
6618 function Copy_Generic_List
6620 Parent_Id : Node_Id) return List_Id;
6621 -- Apply Copy_Node recursively to the members of a node list
6623 function In_Defining_Unit_Name (Nam : Node_Id) return Boolean;
6624 -- True if an identifier is part of the defining program unit name of
6625 -- a child unit. The entity of such an identifier must be kept (for
6626 -- ASIS use) even though as the name of an enclosing generic it would
6627 -- otherwise not be preserved in the generic tree.
6629 ----------------------
6630 -- Copy_Descendants --
6631 ----------------------
6633 procedure Copy_Descendants is
6635 use Atree.Unchecked_Access;
6636 -- This code section is part of the implementation of an untyped
6637 -- tree traversal, so it needs direct access to node fields.
6640 Set_Field1 (New_N, Copy_Generic_Descendant (Field1 (N)));
6641 Set_Field2 (New_N, Copy_Generic_Descendant (Field2 (N)));
6642 Set_Field3 (New_N, Copy_Generic_Descendant (Field3 (N)));
6643 Set_Field4 (New_N, Copy_Generic_Descendant (Field4 (N)));
6644 Set_Field5 (New_N, Copy_Generic_Descendant (Field5 (N)));
6645 end Copy_Descendants;
6647 -----------------------------
6648 -- Copy_Generic_Descendant --
6649 -----------------------------
6651 function Copy_Generic_Descendant (D : Union_Id) return Union_Id is
6653 if D = Union_Id (Empty) then
6656 elsif D in Node_Range then
6658 (Copy_Generic_Node (Node_Id (D), New_N, Instantiating));
6660 elsif D in List_Range then
6661 return Union_Id (Copy_Generic_List (List_Id (D), New_N));
6663 elsif D in Elist_Range then
6664 return Union_Id (Copy_Generic_Elist (Elist_Id (D)));
6666 -- Nothing else is copyable (e.g. Uint values), return as is
6671 end Copy_Generic_Descendant;
6673 ------------------------
6674 -- Copy_Generic_Elist --
6675 ------------------------
6677 function Copy_Generic_Elist (E : Elist_Id) return Elist_Id is
6684 M := First_Elmt (E);
6685 while Present (M) loop
6687 (Copy_Generic_Node (Node (M), Empty, Instantiating), L);
6696 end Copy_Generic_Elist;
6698 -----------------------
6699 -- Copy_Generic_List --
6700 -----------------------
6702 function Copy_Generic_List
6704 Parent_Id : Node_Id) return List_Id
6712 Set_Parent (New_L, Parent_Id);
6715 while Present (N) loop
6716 Append (Copy_Generic_Node (N, Empty, Instantiating), New_L);
6725 end Copy_Generic_List;
6727 ---------------------------
6728 -- In_Defining_Unit_Name --
6729 ---------------------------
6731 function In_Defining_Unit_Name (Nam : Node_Id) return Boolean is
6733 return Present (Parent (Nam))
6734 and then (Nkind (Parent (Nam)) = N_Defining_Program_Unit_Name
6736 (Nkind (Parent (Nam)) = N_Expanded_Name
6737 and then In_Defining_Unit_Name (Parent (Nam))));
6738 end In_Defining_Unit_Name;
6740 -- Start of processing for Copy_Generic_Node
6747 New_N := New_Copy (N);
6749 -- Copy aspects if present
6751 if Has_Aspects (N) then
6752 Set_Has_Aspects (New_N, False);
6753 Set_Aspect_Specifications
6754 (New_N, Copy_Generic_List (Aspect_Specifications (N), Parent_Id));
6757 if Instantiating then
6758 Adjust_Instantiation_Sloc (New_N, S_Adjustment);
6761 if not Is_List_Member (N) then
6762 Set_Parent (New_N, Parent_Id);
6765 -- If defining identifier, then all fields have been copied already
6767 if Nkind (New_N) in N_Entity then
6770 -- Special casing for identifiers and other entity names and operators
6772 elsif Nkind_In (New_N, N_Identifier,
6773 N_Character_Literal,
6776 or else Nkind (New_N) in N_Op
6778 if not Instantiating then
6780 -- Link both nodes in order to assign subsequently the entity of
6781 -- the copy to the original node, in case this is a global
6784 Set_Associated_Node (N, New_N);
6786 -- If we are within an instantiation, this is a nested generic
6787 -- that has already been analyzed at the point of definition.
6788 -- We must preserve references that were global to the enclosing
6789 -- parent at that point. Other occurrences, whether global or
6790 -- local to the current generic, must be resolved anew, so we
6791 -- reset the entity in the generic copy. A global reference has a
6792 -- smaller depth than the parent, or else the same depth in case
6793 -- both are distinct compilation units.
6795 -- A child unit is implicitly declared within the enclosing parent
6796 -- but is in fact global to it, and must be preserved.
6798 -- It is also possible for Current_Instantiated_Parent to be
6799 -- defined, and for this not to be a nested generic, namely if
6800 -- the unit is loaded through Rtsfind. In that case, the entity of
6801 -- New_N is only a link to the associated node, and not a defining
6804 -- The entities for parent units in the defining_program_unit of a
6805 -- generic child unit are established when the context of the unit
6806 -- is first analyzed, before the generic copy is made. They are
6807 -- preserved in the copy for use in ASIS queries.
6809 Ent := Entity (New_N);
6811 if No (Current_Instantiated_Parent.Gen_Id) then
6813 or else Nkind (Ent) /= N_Defining_Identifier
6814 or else not In_Defining_Unit_Name (N)
6816 Set_Associated_Node (New_N, Empty);
6821 not Nkind_In (Ent, N_Defining_Identifier,
6822 N_Defining_Character_Literal,
6823 N_Defining_Operator_Symbol)
6824 or else No (Scope (Ent))
6826 (Scope (Ent) = Current_Instantiated_Parent.Gen_Id
6827 and then not Is_Child_Unit (Ent))
6829 (Scope_Depth (Scope (Ent)) >
6830 Scope_Depth (Current_Instantiated_Parent.Gen_Id)
6832 Get_Source_Unit (Ent) =
6833 Get_Source_Unit (Current_Instantiated_Parent.Gen_Id))
6835 Set_Associated_Node (New_N, Empty);
6838 -- Case of instantiating identifier or some other name or operator
6841 -- If the associated node is still defined, the entity in it
6842 -- is global, and must be copied to the instance. If this copy
6843 -- is being made for a body to inline, it is applied to an
6844 -- instantiated tree, and the entity is already present and
6845 -- must be also preserved.
6848 Assoc : constant Node_Id := Get_Associated_Node (N);
6851 if Present (Assoc) then
6852 if Nkind (Assoc) = Nkind (N) then
6853 Set_Entity (New_N, Entity (Assoc));
6854 Check_Private_View (N);
6856 -- The name in the call may be a selected component if the
6857 -- call has not been analyzed yet, as may be the case for
6858 -- pre/post conditions in a generic unit.
6860 elsif Nkind (Assoc) = N_Function_Call
6861 and then Is_Entity_Name (Name (Assoc))
6863 Set_Entity (New_N, Entity (Name (Assoc)));
6865 elsif Nkind_In (Assoc, N_Defining_Identifier,
6866 N_Defining_Character_Literal,
6867 N_Defining_Operator_Symbol)
6868 and then Expander_Active
6870 -- Inlining case: we are copying a tree that contains
6871 -- global entities, which are preserved in the copy to be
6872 -- used for subsequent inlining.
6877 Set_Entity (New_N, Empty);
6883 -- For expanded name, we must copy the Prefix and Selector_Name
6885 if Nkind (N) = N_Expanded_Name then
6887 (New_N, Copy_Generic_Node (Prefix (N), New_N, Instantiating));
6889 Set_Selector_Name (New_N,
6890 Copy_Generic_Node (Selector_Name (N), New_N, Instantiating));
6892 -- For operators, we must copy the right operand
6894 elsif Nkind (N) in N_Op then
6895 Set_Right_Opnd (New_N,
6896 Copy_Generic_Node (Right_Opnd (N), New_N, Instantiating));
6898 -- And for binary operators, the left operand as well
6900 if Nkind (N) in N_Binary_Op then
6901 Set_Left_Opnd (New_N,
6902 Copy_Generic_Node (Left_Opnd (N), New_N, Instantiating));
6906 -- Special casing for stubs
6908 elsif Nkind (N) in N_Body_Stub then
6910 -- In any case, we must copy the specification or defining
6911 -- identifier as appropriate.
6913 if Nkind (N) = N_Subprogram_Body_Stub then
6914 Set_Specification (New_N,
6915 Copy_Generic_Node (Specification (N), New_N, Instantiating));
6918 Set_Defining_Identifier (New_N,
6920 (Defining_Identifier (N), New_N, Instantiating));
6923 -- If we are not instantiating, then this is where we load and
6924 -- analyze subunits, i.e. at the point where the stub occurs. A
6925 -- more permissive system might defer this analysis to the point
6926 -- of instantiation, but this seems too complicated for now.
6928 if not Instantiating then
6930 Subunit_Name : constant Unit_Name_Type := Get_Unit_Name (N);
6932 Unum : Unit_Number_Type;
6936 -- Make sure that, if it is a subunit of the main unit that is
6937 -- preprocessed and if -gnateG is specified, the preprocessed
6938 -- file will be written.
6940 Lib.Analysing_Subunit_Of_Main :=
6941 Lib.In_Extended_Main_Source_Unit (N);
6944 (Load_Name => Subunit_Name,
6948 Lib.Analysing_Subunit_Of_Main := False;
6950 -- If the proper body is not found, a warning message will be
6951 -- emitted when analyzing the stub, or later at the point of
6952 -- instantiation. Here we just leave the stub as is.
6954 if Unum = No_Unit then
6955 Subunits_Missing := True;
6956 goto Subunit_Not_Found;
6959 Subunit := Cunit (Unum);
6961 if Nkind (Unit (Subunit)) /= N_Subunit then
6963 ("found child unit instead of expected SEPARATE subunit",
6965 Error_Msg_Sloc := Sloc (N);
6966 Error_Msg_N ("\to complete stub #", Subunit);
6967 goto Subunit_Not_Found;
6970 -- We must create a generic copy of the subunit, in order to
6971 -- perform semantic analysis on it, and we must replace the
6972 -- stub in the original generic unit with the subunit, in order
6973 -- to preserve non-local references within.
6975 -- Only the proper body needs to be copied. Library_Unit and
6976 -- context clause are simply inherited by the generic copy.
6977 -- Note that the copy (which may be recursive if there are
6978 -- nested subunits) must be done first, before attaching it to
6979 -- the enclosing generic.
6983 (Proper_Body (Unit (Subunit)),
6984 Empty, Instantiating => False);
6986 -- Now place the original proper body in the original generic
6987 -- unit. This is a body, not a compilation unit.
6989 Rewrite (N, Proper_Body (Unit (Subunit)));
6990 Set_Is_Compilation_Unit (Defining_Entity (N), False);
6991 Set_Was_Originally_Stub (N);
6993 -- Finally replace the body of the subunit with its copy, and
6994 -- make this new subunit into the library unit of the generic
6995 -- copy, which does not have stubs any longer.
6997 Set_Proper_Body (Unit (Subunit), New_Body);
6998 Set_Library_Unit (New_N, Subunit);
6999 Inherit_Context (Unit (Subunit), N);
7002 -- If we are instantiating, this must be an error case, since
7003 -- otherwise we would have replaced the stub node by the proper body
7004 -- that corresponds. So just ignore it in the copy (i.e. we have
7005 -- copied it, and that is good enough).
7011 <<Subunit_Not_Found>> null;
7013 -- If the node is a compilation unit, it is the subunit of a stub, which
7014 -- has been loaded already (see code below). In this case, the library
7015 -- unit field of N points to the parent unit (which is a compilation
7016 -- unit) and need not (and cannot) be copied.
7018 -- When the proper body of the stub is analyzed, the library_unit link
7019 -- is used to establish the proper context (see sem_ch10).
7021 -- The other fields of a compilation unit are copied as usual
7023 elsif Nkind (N) = N_Compilation_Unit then
7025 -- This code can only be executed when not instantiating, because in
7026 -- the copy made for an instantiation, the compilation unit node has
7027 -- disappeared at the point that a stub is replaced by its proper
7030 pragma Assert (not Instantiating);
7032 Set_Context_Items (New_N,
7033 Copy_Generic_List (Context_Items (N), New_N));
7036 Copy_Generic_Node (Unit (N), New_N, False));
7038 Set_First_Inlined_Subprogram (New_N,
7040 (First_Inlined_Subprogram (N), New_N, False));
7042 Set_Aux_Decls_Node (New_N,
7043 Copy_Generic_Node (Aux_Decls_Node (N), New_N, False));
7045 -- For an assignment node, the assignment is known to be semantically
7046 -- legal if we are instantiating the template. This avoids incorrect
7047 -- diagnostics in generated code.
7049 elsif Nkind (N) = N_Assignment_Statement then
7051 -- Copy name and expression fields in usual manner
7054 Copy_Generic_Node (Name (N), New_N, Instantiating));
7056 Set_Expression (New_N,
7057 Copy_Generic_Node (Expression (N), New_N, Instantiating));
7059 if Instantiating then
7060 Set_Assignment_OK (Name (New_N), True);
7063 elsif Nkind_In (N, N_Aggregate, N_Extension_Aggregate) then
7064 if not Instantiating then
7065 Set_Associated_Node (N, New_N);
7068 if Present (Get_Associated_Node (N))
7069 and then Nkind (Get_Associated_Node (N)) = Nkind (N)
7071 -- In the generic the aggregate has some composite type. If at
7072 -- the point of instantiation the type has a private view,
7073 -- install the full view (and that of its ancestors, if any).
7076 T : Entity_Id := (Etype (Get_Associated_Node (New_N)));
7081 and then Is_Private_Type (T)
7087 and then Is_Tagged_Type (T)
7088 and then Is_Derived_Type (T)
7090 Rt := Root_Type (T);
7095 if Is_Private_Type (T) then
7106 -- Do not copy the associated node, which points to the generic copy
7107 -- of the aggregate.
7110 use Atree.Unchecked_Access;
7111 -- This code section is part of the implementation of an untyped
7112 -- tree traversal, so it needs direct access to node fields.
7115 Set_Field1 (New_N, Copy_Generic_Descendant (Field1 (N)));
7116 Set_Field2 (New_N, Copy_Generic_Descendant (Field2 (N)));
7117 Set_Field3 (New_N, Copy_Generic_Descendant (Field3 (N)));
7118 Set_Field5 (New_N, Copy_Generic_Descendant (Field5 (N)));
7121 -- Allocators do not have an identifier denoting the access type, so we
7122 -- must locate it through the expression to check whether the views are
7125 elsif Nkind (N) = N_Allocator
7126 and then Nkind (Expression (N)) = N_Qualified_Expression
7127 and then Is_Entity_Name (Subtype_Mark (Expression (N)))
7128 and then Instantiating
7131 T : constant Node_Id :=
7132 Get_Associated_Node (Subtype_Mark (Expression (N)));
7138 -- Retrieve the allocator node in the generic copy
7140 Acc_T := Etype (Parent (Parent (T)));
7142 and then Is_Private_Type (Acc_T)
7144 Switch_View (Acc_T);
7151 -- For a proper body, we must catch the case of a proper body that
7152 -- replaces a stub. This represents the point at which a separate
7153 -- compilation unit, and hence template file, may be referenced, so we
7154 -- must make a new source instantiation entry for the template of the
7155 -- subunit, and ensure that all nodes in the subunit are adjusted using
7156 -- this new source instantiation entry.
7158 elsif Nkind (N) in N_Proper_Body then
7160 Save_Adjustment : constant Sloc_Adjustment := S_Adjustment;
7163 if Instantiating and then Was_Originally_Stub (N) then
7164 Create_Instantiation_Source
7165 (Instantiation_Node,
7166 Defining_Entity (N),
7171 -- Now copy the fields of the proper body, using the new
7172 -- adjustment factor if one was needed as per test above.
7176 -- Restore the original adjustment factor in case changed
7178 S_Adjustment := Save_Adjustment;
7181 -- Don't copy Ident or Comment pragmas, since the comment belongs to the
7182 -- generic unit, not to the instantiating unit.
7184 elsif Nkind (N) = N_Pragma and then Instantiating then
7186 Prag_Id : constant Pragma_Id := Get_Pragma_Id (N);
7188 if Prag_Id = Pragma_Ident or else Prag_Id = Pragma_Comment then
7189 New_N := Make_Null_Statement (Sloc (N));
7195 elsif Nkind_In (N, N_Integer_Literal, N_Real_Literal) then
7197 -- No descendant fields need traversing
7201 elsif Nkind (N) = N_String_Literal
7202 and then Present (Etype (N))
7203 and then Instantiating
7205 -- If the string is declared in an outer scope, the string_literal
7206 -- subtype created for it may have the wrong scope. We force the
7207 -- reanalysis of the constant to generate a new itype in the proper
7210 Set_Etype (New_N, Empty);
7211 Set_Analyzed (New_N, False);
7213 -- For the remaining nodes, copy their descendants recursively
7218 if Instantiating and then Nkind (N) = N_Subprogram_Body then
7219 Set_Generic_Parent (Specification (New_N), N);
7221 -- Should preserve Corresponding_Spec??? (12.3(14))
7226 end Copy_Generic_Node;
7228 ----------------------------
7229 -- Denotes_Formal_Package --
7230 ----------------------------
7232 function Denotes_Formal_Package
7234 On_Exit : Boolean := False;
7235 Instance : Entity_Id := Empty) return Boolean
7238 Scop : constant Entity_Id := Scope (Pack);
7241 function Is_Actual_Of_Previous_Formal (P : Entity_Id) return Boolean;
7242 -- The package in question may be an actual for a previous formal
7243 -- package P of the current instance, so examine its actuals as well.
7244 -- This must be recursive over other formal packages.
7246 ----------------------------------
7247 -- Is_Actual_Of_Previous_Formal --
7248 ----------------------------------
7250 function Is_Actual_Of_Previous_Formal (P : Entity_Id) return Boolean is
7254 E1 := First_Entity (P);
7255 while Present (E1) and then E1 /= Instance loop
7256 if Ekind (E1) = E_Package
7257 and then Nkind (Parent (E1)) = N_Package_Renaming_Declaration
7259 if Renamed_Object (E1) = Pack then
7262 elsif E1 = P or else Renamed_Object (E1) = P then
7265 elsif Is_Actual_Of_Previous_Formal (E1) then
7274 end Is_Actual_Of_Previous_Formal;
7276 -- Start of processing for Denotes_Formal_Package
7282 (Instance_Envs.Last).Instantiated_Parent.Act_Id;
7284 Par := Current_Instantiated_Parent.Act_Id;
7287 if Ekind (Scop) = E_Generic_Package
7288 or else Nkind (Unit_Declaration_Node (Scop)) =
7289 N_Generic_Subprogram_Declaration
7293 elsif Nkind (Original_Node (Unit_Declaration_Node (Pack))) =
7294 N_Formal_Package_Declaration
7302 -- Check whether this package is associated with a formal package of
7303 -- the enclosing instantiation. Iterate over the list of renamings.
7305 E := First_Entity (Par);
7306 while Present (E) loop
7307 if Ekind (E) /= E_Package
7308 or else Nkind (Parent (E)) /= N_Package_Renaming_Declaration
7312 elsif Renamed_Object (E) = Par then
7315 elsif Renamed_Object (E) = Pack then
7318 elsif Is_Actual_Of_Previous_Formal (E) then
7328 end Denotes_Formal_Package;
7334 procedure End_Generic is
7336 -- ??? More things could be factored out in this routine. Should
7337 -- probably be done at a later stage.
7339 Inside_A_Generic := Generic_Flags.Table (Generic_Flags.Last);
7340 Generic_Flags.Decrement_Last;
7342 Expander_Mode_Restore;
7349 function Earlier (N1, N2 : Node_Id) return Boolean is
7350 procedure Find_Depth (P : in out Node_Id; D : in out Integer);
7351 -- Find distance from given node to enclosing compilation unit
7357 procedure Find_Depth (P : in out Node_Id; D : in out Integer) is
7360 and then Nkind (P) /= N_Compilation_Unit
7362 P := True_Parent (P);
7367 -- Local declarations
7376 -- Start of processing for Earlier
7379 Find_Depth (P1, D1);
7380 Find_Depth (P2, D2);
7390 P1 := True_Parent (P1);
7395 P2 := True_Parent (P2);
7399 -- At this point P1 and P2 are at the same distance from the root.
7400 -- We examine their parents until we find a common declarative list.
7401 -- If we reach the root, N1 and N2 do not descend from the same
7402 -- declarative list (e.g. one is nested in the declarative part and
7403 -- the other is in a block in the statement part) and the earlier
7404 -- one is already frozen.
7406 while not Is_List_Member (P1)
7407 or else not Is_List_Member (P2)
7408 or else List_Containing (P1) /= List_Containing (P2)
7410 P1 := True_Parent (P1);
7411 P2 := True_Parent (P2);
7413 if Nkind (Parent (P1)) = N_Subunit then
7414 P1 := Corresponding_Stub (Parent (P1));
7417 if Nkind (Parent (P2)) = N_Subunit then
7418 P2 := Corresponding_Stub (Parent (P2));
7426 -- Expanded code usually shares the source location of the original
7427 -- construct it was generated for. This however may not necessarely
7428 -- reflect the true location of the code within the tree.
7430 -- Before comparing the slocs of the two nodes, make sure that we are
7431 -- working with correct source locations. Assume that P1 is to the left
7432 -- of P2. If either one does not come from source, traverse the common
7433 -- list heading towards the other node and locate the first source
7437 -- ----+===+===+--------------+===+===+----
7438 -- expanded code expanded code
7440 if not Comes_From_Source (P1) then
7441 while Present (P1) loop
7443 -- Neither P2 nor a source statement were located during the
7444 -- search. If we reach the end of the list, then P1 does not
7445 -- occur earlier than P2.
7448 -- start --- P2 ----- P1 --- end
7450 if No (Next (P1)) then
7453 -- We encounter P2 while going to the right of the list. This
7454 -- means that P1 does indeed appear earlier.
7457 -- start --- P1 ===== P2 --- end
7458 -- expanded code in between
7463 -- No need to look any further since we have located a source
7466 elsif Comes_From_Source (P1) then
7476 if not Comes_From_Source (P2) then
7477 while Present (P2) loop
7479 -- Neither P1 nor a source statement were located during the
7480 -- search. If we reach the start of the list, then P1 does not
7481 -- occur earlier than P2.
7484 -- start --- P2 --- P1 --- end
7486 if No (Prev (P2)) then
7489 -- We encounter P1 while going to the left of the list. This
7490 -- means that P1 does indeed appear earlier.
7493 -- start --- P1 ===== P2 --- end
7494 -- expanded code in between
7499 -- No need to look any further since we have located a source
7502 elsif Comes_From_Source (P2) then
7512 -- At this point either both nodes came from source or we approximated
7513 -- their source locations through neighbouring source statements.
7515 T1 := Top_Level_Location (Sloc (P1));
7516 T2 := Top_Level_Location (Sloc (P2));
7518 -- When two nodes come from the same instance, they have identical top
7519 -- level locations. To determine proper relation within the tree, check
7520 -- their locations within the template.
7523 return Sloc (P1) < Sloc (P2);
7525 -- The two nodes either come from unrelated instances or do not come
7526 -- from instantiated code at all.
7533 ----------------------
7534 -- Find_Actual_Type --
7535 ----------------------
7537 function Find_Actual_Type
7539 Gen_Type : Entity_Id) return Entity_Id
7541 Gen_Scope : constant Entity_Id := Scope (Gen_Type);
7545 -- Special processing only applies to child units
7547 if not Is_Child_Unit (Gen_Scope) then
7548 return Get_Instance_Of (Typ);
7550 -- If designated or component type is itself a formal of the child unit,
7551 -- its instance is available.
7553 elsif Scope (Typ) = Gen_Scope then
7554 return Get_Instance_Of (Typ);
7556 -- If the array or access type is not declared in the parent unit,
7557 -- no special processing needed.
7559 elsif not Is_Generic_Type (Typ)
7560 and then Scope (Gen_Scope) /= Scope (Typ)
7562 return Get_Instance_Of (Typ);
7564 -- Otherwise, retrieve designated or component type by visibility
7567 T := Current_Entity (Typ);
7568 while Present (T) loop
7569 if In_Open_Scopes (Scope (T)) then
7572 elsif Is_Generic_Actual_Type (T) then
7581 end Find_Actual_Type;
7583 ----------------------------
7584 -- Freeze_Subprogram_Body --
7585 ----------------------------
7587 procedure Freeze_Subprogram_Body
7588 (Inst_Node : Node_Id;
7590 Pack_Id : Entity_Id)
7592 Gen_Unit : constant Entity_Id := Get_Generic_Entity (Inst_Node);
7593 Par : constant Entity_Id := Scope (Gen_Unit);
7599 function Enclosing_Package_Body (N : Node_Id) return Node_Id;
7600 -- Find innermost package body that encloses the given node, and which
7601 -- is not a compilation unit. Freeze nodes for the instance, or for its
7602 -- enclosing body, may be inserted after the enclosing_body of the
7603 -- generic unit. Used to determine proper placement of freeze node for
7604 -- both package and subprogram instances.
7606 function Package_Freeze_Node (B : Node_Id) return Node_Id;
7607 -- Find entity for given package body, and locate or create a freeze
7610 ----------------------------
7611 -- Enclosing_Package_Body --
7612 ----------------------------
7614 function Enclosing_Package_Body (N : Node_Id) return Node_Id is
7620 and then Nkind (Parent (P)) /= N_Compilation_Unit
7622 if Nkind (P) = N_Package_Body then
7623 if Nkind (Parent (P)) = N_Subunit then
7624 return Corresponding_Stub (Parent (P));
7630 P := True_Parent (P);
7634 end Enclosing_Package_Body;
7636 -------------------------
7637 -- Package_Freeze_Node --
7638 -------------------------
7640 function Package_Freeze_Node (B : Node_Id) return Node_Id is
7644 if Nkind (B) = N_Package_Body then
7645 Id := Corresponding_Spec (B);
7646 else pragma Assert (Nkind (B) = N_Package_Body_Stub);
7647 Id := Corresponding_Spec (Proper_Body (Unit (Library_Unit (B))));
7650 Ensure_Freeze_Node (Id);
7651 return Freeze_Node (Id);
7652 end Package_Freeze_Node;
7654 -- Start of processing of Freeze_Subprogram_Body
7657 -- If the instance and the generic body appear within the same unit, and
7658 -- the instance precedes the generic, the freeze node for the instance
7659 -- must appear after that of the generic. If the generic is nested
7660 -- within another instance I2, then current instance must be frozen
7661 -- after I2. In both cases, the freeze nodes are those of enclosing
7662 -- packages. Otherwise, the freeze node is placed at the end of the
7663 -- current declarative part.
7665 Enc_G := Enclosing_Package_Body (Gen_Body);
7666 Enc_I := Enclosing_Package_Body (Inst_Node);
7667 Ensure_Freeze_Node (Pack_Id);
7668 F_Node := Freeze_Node (Pack_Id);
7670 if Is_Generic_Instance (Par)
7671 and then Present (Freeze_Node (Par))
7672 and then In_Same_Declarative_Part (Freeze_Node (Par), Inst_Node)
7674 -- The parent was a premature instantiation. Insert freeze node at
7675 -- the end the current declarative part.
7677 if ABE_Is_Certain (Get_Package_Instantiation_Node (Par)) then
7678 Insert_Freeze_Node_For_Instance (Inst_Node, F_Node);
7680 -- Handle the following case:
7682 -- package Parent_Inst is new ...
7685 -- procedure P ... -- this body freezes Parent_Inst
7687 -- package Inst is new ...
7689 -- In this particular scenario, the freeze node for Inst must be
7690 -- inserted in the same manner as that of Parent_Inst - before the
7691 -- next source body or at the end of the declarative list (body not
7692 -- available). If body P did not exist and Parent_Inst was frozen
7693 -- after Inst, either by a body following Inst or at the end of the
7694 -- declarative region, the freeze node for Inst must be inserted
7695 -- after that of Parent_Inst. This relation is established by
7696 -- comparing the Slocs of Parent_Inst freeze node and Inst.
7698 elsif List_Containing (Get_Package_Instantiation_Node (Par)) =
7699 List_Containing (Inst_Node)
7700 and then Sloc (Freeze_Node (Par)) < Sloc (Inst_Node)
7702 Insert_Freeze_Node_For_Instance (Inst_Node, F_Node);
7705 Insert_After (Freeze_Node (Par), F_Node);
7708 -- The body enclosing the instance should be frozen after the body that
7709 -- includes the generic, because the body of the instance may make
7710 -- references to entities therein. If the two are not in the same
7711 -- declarative part, or if the one enclosing the instance is frozen
7712 -- already, freeze the instance at the end of the current declarative
7715 elsif Is_Generic_Instance (Par)
7716 and then Present (Freeze_Node (Par))
7717 and then Present (Enc_I)
7719 if In_Same_Declarative_Part (Freeze_Node (Par), Enc_I)
7721 (Nkind (Enc_I) = N_Package_Body
7723 In_Same_Declarative_Part (Freeze_Node (Par), Parent (Enc_I)))
7725 -- The enclosing package may contain several instances. Rather
7726 -- than computing the earliest point at which to insert its freeze
7727 -- node, we place it at the end of the declarative part of the
7728 -- parent of the generic.
7730 Insert_Freeze_Node_For_Instance
7731 (Freeze_Node (Par), Package_Freeze_Node (Enc_I));
7734 Insert_Freeze_Node_For_Instance (Inst_Node, F_Node);
7736 elsif Present (Enc_G)
7737 and then Present (Enc_I)
7738 and then Enc_G /= Enc_I
7739 and then Earlier (Inst_Node, Gen_Body)
7741 if Nkind (Enc_G) = N_Package_Body then
7742 E_G_Id := Corresponding_Spec (Enc_G);
7743 else pragma Assert (Nkind (Enc_G) = N_Package_Body_Stub);
7745 Corresponding_Spec (Proper_Body (Unit (Library_Unit (Enc_G))));
7748 -- Freeze package that encloses instance, and place node after the
7749 -- package that encloses generic. If enclosing package is already
7750 -- frozen we have to assume it is at the proper place. This may be a
7751 -- potential ABE that requires dynamic checking. Do not add a freeze
7752 -- node if the package that encloses the generic is inside the body
7753 -- that encloses the instance, because the freeze node would be in
7754 -- the wrong scope. Additional contortions needed if the bodies are
7755 -- within a subunit.
7758 Enclosing_Body : Node_Id;
7761 if Nkind (Enc_I) = N_Package_Body_Stub then
7762 Enclosing_Body := Proper_Body (Unit (Library_Unit (Enc_I)));
7764 Enclosing_Body := Enc_I;
7767 if Parent (List_Containing (Enc_G)) /= Enclosing_Body then
7768 Insert_Freeze_Node_For_Instance
7769 (Enc_G, Package_Freeze_Node (Enc_I));
7773 -- Freeze enclosing subunit before instance
7775 Ensure_Freeze_Node (E_G_Id);
7777 if not Is_List_Member (Freeze_Node (E_G_Id)) then
7778 Insert_After (Enc_G, Freeze_Node (E_G_Id));
7781 Insert_Freeze_Node_For_Instance (Inst_Node, F_Node);
7784 -- If none of the above, insert freeze node at the end of the current
7785 -- declarative part.
7787 Insert_Freeze_Node_For_Instance (Inst_Node, F_Node);
7789 end Freeze_Subprogram_Body;
7795 function Get_Gen_Id (E : Assoc_Ptr) return Entity_Id is
7797 return Generic_Renamings.Table (E).Gen_Id;
7800 ---------------------
7801 -- Get_Instance_Of --
7802 ---------------------
7804 function Get_Instance_Of (A : Entity_Id) return Entity_Id is
7805 Res : constant Assoc_Ptr := Generic_Renamings_HTable.Get (A);
7808 if Res /= Assoc_Null then
7809 return Generic_Renamings.Table (Res).Act_Id;
7811 -- On exit, entity is not instantiated: not a generic parameter, or
7812 -- else parameter of an inner generic unit.
7816 end Get_Instance_Of;
7818 ------------------------------------
7819 -- Get_Package_Instantiation_Node --
7820 ------------------------------------
7822 function Get_Package_Instantiation_Node (A : Entity_Id) return Node_Id is
7823 Decl : Node_Id := Unit_Declaration_Node (A);
7827 -- If the Package_Instantiation attribute has been set on the package
7828 -- entity, then use it directly when it (or its Original_Node) refers
7829 -- to an N_Package_Instantiation node. In principle it should be
7830 -- possible to have this field set in all cases, which should be
7831 -- investigated, and would allow this function to be significantly
7834 Inst := Package_Instantiation (A);
7836 if Present (Inst) then
7837 if Nkind (Inst) = N_Package_Instantiation then
7840 elsif Nkind (Original_Node (Inst)) = N_Package_Instantiation then
7841 return Original_Node (Inst);
7845 -- If the instantiation is a compilation unit that does not need body
7846 -- then the instantiation node has been rewritten as a package
7847 -- declaration for the instance, and we return the original node.
7849 -- If it is a compilation unit and the instance node has not been
7850 -- rewritten, then it is still the unit of the compilation. Finally, if
7851 -- a body is present, this is a parent of the main unit whose body has
7852 -- been compiled for inlining purposes, and the instantiation node has
7853 -- been rewritten with the instance body.
7855 -- Otherwise the instantiation node appears after the declaration. If
7856 -- the entity is a formal package, the declaration may have been
7857 -- rewritten as a generic declaration (in the case of a formal with box)
7858 -- or left as a formal package declaration if it has actuals, and is
7859 -- found with a forward search.
7861 if Nkind (Parent (Decl)) = N_Compilation_Unit then
7862 if Nkind (Decl) = N_Package_Declaration
7863 and then Present (Corresponding_Body (Decl))
7865 Decl := Unit_Declaration_Node (Corresponding_Body (Decl));
7868 if Nkind (Original_Node (Decl)) = N_Package_Instantiation then
7869 return Original_Node (Decl);
7871 return Unit (Parent (Decl));
7874 elsif Nkind (Decl) = N_Package_Declaration
7875 and then Nkind (Original_Node (Decl)) = N_Formal_Package_Declaration
7877 return Original_Node (Decl);
7880 Inst := Next (Decl);
7881 while not Nkind_In (Inst, N_Package_Instantiation,
7882 N_Formal_Package_Declaration)
7889 end Get_Package_Instantiation_Node;
7891 ------------------------
7892 -- Has_Been_Exchanged --
7893 ------------------------
7895 function Has_Been_Exchanged (E : Entity_Id) return Boolean is
7899 Next := First_Elmt (Exchanged_Views);
7900 while Present (Next) loop
7901 if Full_View (Node (Next)) = E then
7909 end Has_Been_Exchanged;
7915 function Hash (F : Entity_Id) return HTable_Range is
7917 return HTable_Range (F mod HTable_Size);
7920 ------------------------
7921 -- Hide_Current_Scope --
7922 ------------------------
7924 procedure Hide_Current_Scope is
7925 C : constant Entity_Id := Current_Scope;
7929 Set_Is_Hidden_Open_Scope (C);
7931 E := First_Entity (C);
7932 while Present (E) loop
7933 if Is_Immediately_Visible (E) then
7934 Set_Is_Immediately_Visible (E, False);
7935 Append_Elmt (E, Hidden_Entities);
7941 -- Make the scope name invisible as well. This is necessary, but might
7942 -- conflict with calls to Rtsfind later on, in case the scope is a
7943 -- predefined one. There is no clean solution to this problem, so for
7944 -- now we depend on the user not redefining Standard itself in one of
7945 -- the parent units.
7947 if Is_Immediately_Visible (C) and then C /= Standard_Standard then
7948 Set_Is_Immediately_Visible (C, False);
7949 Append_Elmt (C, Hidden_Entities);
7952 end Hide_Current_Scope;
7958 procedure Init_Env is
7959 Saved : Instance_Env;
7962 Saved.Instantiated_Parent := Current_Instantiated_Parent;
7963 Saved.Exchanged_Views := Exchanged_Views;
7964 Saved.Hidden_Entities := Hidden_Entities;
7965 Saved.Current_Sem_Unit := Current_Sem_Unit;
7966 Saved.Parent_Unit_Visible := Parent_Unit_Visible;
7967 Saved.Instance_Parent_Unit := Instance_Parent_Unit;
7969 -- Save configuration switches. These may be reset if the unit is a
7970 -- predefined unit, and the current mode is not Ada 2005.
7972 Save_Opt_Config_Switches (Saved.Switches);
7974 Instance_Envs.Append (Saved);
7976 Exchanged_Views := New_Elmt_List;
7977 Hidden_Entities := New_Elmt_List;
7979 -- Make dummy entry for Instantiated parent. If generic unit is legal,
7980 -- this is set properly in Set_Instance_Env.
7982 Current_Instantiated_Parent :=
7983 (Current_Scope, Current_Scope, Assoc_Null);
7986 ------------------------------
7987 -- In_Same_Declarative_Part --
7988 ------------------------------
7990 function In_Same_Declarative_Part
7992 Inst : Node_Id) return Boolean
7994 Decls : constant Node_Id := Parent (F_Node);
7995 Nod : Node_Id := Parent (Inst);
7998 while Present (Nod) loop
8002 elsif Nkind_In (Nod, N_Subprogram_Body,
8004 N_Package_Declaration,
8011 elsif Nkind (Nod) = N_Subunit then
8012 Nod := Corresponding_Stub (Nod);
8014 elsif Nkind (Nod) = N_Compilation_Unit then
8018 Nod := Parent (Nod);
8023 end In_Same_Declarative_Part;
8025 ---------------------
8026 -- In_Main_Context --
8027 ---------------------
8029 function In_Main_Context (E : Entity_Id) return Boolean is
8035 if not Is_Compilation_Unit (E)
8036 or else Ekind (E) /= E_Package
8037 or else In_Private_Part (E)
8042 Context := Context_Items (Cunit (Main_Unit));
8044 Clause := First (Context);
8045 while Present (Clause) loop
8046 if Nkind (Clause) = N_With_Clause then
8047 Nam := Name (Clause);
8049 -- If the current scope is part of the context of the main unit,
8050 -- analysis of the corresponding with_clause is not complete, and
8051 -- the entity is not set. We use the Chars field directly, which
8052 -- might produce false positives in rare cases, but guarantees
8053 -- that we produce all the instance bodies we will need.
8055 if (Is_Entity_Name (Nam) and then Chars (Nam) = Chars (E))
8056 or else (Nkind (Nam) = N_Selected_Component
8057 and then Chars (Selector_Name (Nam)) = Chars (E))
8067 end In_Main_Context;
8069 ---------------------
8070 -- Inherit_Context --
8071 ---------------------
8073 procedure Inherit_Context (Gen_Decl : Node_Id; Inst : Node_Id) is
8074 Current_Context : List_Id;
8075 Current_Unit : Node_Id;
8084 if Nkind (Parent (Gen_Decl)) = N_Compilation_Unit then
8086 -- The inherited context is attached to the enclosing compilation
8087 -- unit. This is either the main unit, or the declaration for the
8088 -- main unit (in case the instantiation appears within the package
8089 -- declaration and the main unit is its body).
8091 Current_Unit := Parent (Inst);
8092 while Present (Current_Unit)
8093 and then Nkind (Current_Unit) /= N_Compilation_Unit
8095 Current_Unit := Parent (Current_Unit);
8098 Current_Context := Context_Items (Current_Unit);
8100 Item := First (Context_Items (Parent (Gen_Decl)));
8101 while Present (Item) loop
8102 if Nkind (Item) = N_With_Clause then
8103 Lib_Unit := Library_Unit (Item);
8105 -- Take care to prevent direct cyclic with's
8107 if Lib_Unit /= Current_Unit then
8109 -- Do not add a unit if it is already in the context
8111 Clause := First (Current_Context);
8113 while Present (Clause) loop
8114 if Nkind (Clause) = N_With_Clause and then
8115 Library_Unit (Clause) = Lib_Unit
8125 New_I := New_Copy (Item);
8126 Set_Implicit_With (New_I, True);
8127 Set_Implicit_With_From_Instantiation (New_I, True);
8128 Append (New_I, Current_Context);
8136 end Inherit_Context;
8142 procedure Initialize is
8144 Generic_Renamings.Init;
8147 Generic_Renamings_HTable.Reset;
8148 Circularity_Detected := False;
8149 Exchanged_Views := No_Elist;
8150 Hidden_Entities := No_Elist;
8153 -------------------------------------
8154 -- Insert_Freeze_Node_For_Instance --
8155 -------------------------------------
8157 procedure Insert_Freeze_Node_For_Instance
8166 function Enclosing_Body (N : Node_Id) return Node_Id;
8167 -- Find enclosing package or subprogram body, if any. Freeze node may
8168 -- be placed at end of current declarative list if previous instance
8169 -- and current one have different enclosing bodies.
8171 function Previous_Instance (Gen : Entity_Id) return Entity_Id;
8172 -- Find the local instance, if any, that declares the generic that is
8173 -- being instantiated. If present, the freeze node for this instance
8174 -- must follow the freeze node for the previous instance.
8176 --------------------
8177 -- Enclosing_Body --
8178 --------------------
8180 function Enclosing_Body (N : Node_Id) return Node_Id is
8186 and then Nkind (Parent (P)) /= N_Compilation_Unit
8188 if Nkind_In (P, N_Package_Body, N_Subprogram_Body) then
8189 if Nkind (Parent (P)) = N_Subunit then
8190 return Corresponding_Stub (Parent (P));
8196 P := True_Parent (P);
8202 -----------------------
8203 -- Previous_Instance --
8204 -----------------------
8206 function Previous_Instance (Gen : Entity_Id) return Entity_Id is
8212 and then S /= Standard_Standard
8214 if Is_Generic_Instance (S)
8215 and then In_Same_Source_Unit (S, N)
8224 end Previous_Instance;
8226 -- Start of processing for Insert_Freeze_Node_For_Instance
8229 if not Is_List_Member (F_Node) then
8231 Decls := List_Containing (N);
8232 Inst := Entity (F_Node);
8233 Par_N := Parent (Decls);
8235 -- When processing a subprogram instantiation, utilize the actual
8236 -- subprogram instantiation rather than its package wrapper as it
8237 -- carries all the context information.
8239 if Is_Wrapper_Package (Inst) then
8240 Inst := Related_Instance (Inst);
8243 -- If this is a package instance, check whether the generic is
8244 -- declared in a previous instance and the current instance is
8245 -- not within the previous one.
8247 if Present (Generic_Parent (Parent (Inst)))
8248 and then Is_In_Main_Unit (N)
8251 Enclosing_N : constant Node_Id := Enclosing_Body (N);
8252 Par_I : constant Entity_Id :=
8254 (Generic_Parent (Parent (Inst)));
8259 and then Earlier (N, Freeze_Node (Par_I))
8261 Scop := Scope (Inst);
8263 -- If the current instance is within the one that contains
8264 -- the generic, the freeze node for the current one must
8265 -- appear in the current declarative part. Ditto, if the
8266 -- current instance is within another package instance or
8267 -- within a body that does not enclose the current instance.
8268 -- In these three cases the freeze node of the previous
8269 -- instance is not relevant.
8271 while Present (Scop)
8272 and then Scop /= Standard_Standard
8274 exit when Scop = Par_I
8276 (Is_Generic_Instance (Scop)
8277 and then Scope_Depth (Scop) > Scope_Depth (Par_I));
8278 Scop := Scope (Scop);
8281 -- Previous instance encloses current instance
8283 if Scop = Par_I then
8286 -- If the next node is a source body we must freeze in
8287 -- the current scope as well.
8289 elsif Present (Next (N))
8290 and then Nkind_In (Next (N),
8291 N_Subprogram_Body, N_Package_Body)
8292 and then Comes_From_Source (Next (N))
8296 -- Current instance is within an unrelated instance
8298 elsif Is_Generic_Instance (Scop) then
8301 -- Current instance is within an unrelated body
8303 elsif Present (Enclosing_N)
8304 and then Enclosing_N /= Enclosing_Body (Par_I)
8309 Insert_After (Freeze_Node (Par_I), F_Node);
8316 -- When the instantiation occurs in a package declaration, append the
8317 -- freeze node to the private declarations (if any).
8319 if Nkind (Par_N) = N_Package_Specification
8320 and then Decls = Visible_Declarations (Par_N)
8321 and then Present (Private_Declarations (Par_N))
8322 and then not Is_Empty_List (Private_Declarations (Par_N))
8324 Decls := Private_Declarations (Par_N);
8325 Decl := First (Decls);
8328 -- Determine the proper freeze point of a package instantiation. We
8329 -- adhere to the general rule of a package or subprogram body causing
8330 -- freezing of anything before it in the same declarative region. In
8331 -- this case, the proper freeze point of a package instantiation is
8332 -- before the first source body which follows, or before a stub. This
8333 -- ensures that entities coming from the instance are already frozen
8334 -- and usable in source bodies.
8336 if Nkind (Par_N) /= N_Package_Declaration
8337 and then Ekind (Inst) = E_Package
8338 and then Is_Generic_Instance (Inst)
8340 not In_Same_Source_Unit (Generic_Parent (Parent (Inst)), Inst)
8342 while Present (Decl) loop
8343 if (Nkind (Decl) in N_Unit_Body
8345 Nkind (Decl) in N_Body_Stub)
8346 and then Comes_From_Source (Decl)
8348 Insert_Before (Decl, F_Node);
8356 -- In a package declaration, or if no previous body, insert at end
8359 Set_Sloc (F_Node, Sloc (Last (Decls)));
8360 Insert_After (Last (Decls), F_Node);
8362 end Insert_Freeze_Node_For_Instance;
8368 procedure Install_Body
8369 (Act_Body : Node_Id;
8374 Act_Id : constant Entity_Id := Corresponding_Spec (Act_Body);
8375 Act_Unit : constant Node_Id := Unit (Cunit (Get_Source_Unit (N)));
8376 Gen_Id : constant Entity_Id := Corresponding_Spec (Gen_Body);
8377 Par : constant Entity_Id := Scope (Gen_Id);
8378 Gen_Unit : constant Node_Id :=
8379 Unit (Cunit (Get_Source_Unit (Gen_Decl)));
8380 Orig_Body : Node_Id := Gen_Body;
8382 Body_Unit : Node_Id;
8384 Must_Delay : Boolean;
8386 function In_Same_Enclosing_Subp return Boolean;
8387 -- Check whether instance and generic body are within same subprogram.
8389 function True_Sloc (N : Node_Id) return Source_Ptr;
8390 -- If the instance is nested inside a generic unit, the Sloc of the
8391 -- instance indicates the place of the original definition, not the
8392 -- point of the current enclosing instance. Pending a better usage of
8393 -- Slocs to indicate instantiation places, we determine the place of
8394 -- origin of a node by finding the maximum sloc of any ancestor node.
8395 -- Why is this not equivalent to Top_Level_Location ???
8397 ----------------------------
8398 -- In_Same_Enclosing_Subp --
8399 ----------------------------
8401 function In_Same_Enclosing_Subp return Boolean is
8406 Scop := Scope (Act_Id);
8407 while Scop /= Standard_Standard
8408 and then not Is_Overloadable (Scop)
8410 Scop := Scope (Scop);
8413 if Scop = Standard_Standard then
8419 Scop := Scope (Gen_Id);
8420 while Scop /= Standard_Standard loop
8424 Scop := Scope (Scop);
8429 end In_Same_Enclosing_Subp;
8435 function True_Sloc (N : Node_Id) return Source_Ptr is
8442 while Present (N1) and then N1 /= Act_Unit loop
8443 if Sloc (N1) > Res then
8453 -- Start of processing for Install_Body
8456 -- If the body is a subunit, the freeze point is the corresponding stub
8457 -- in the current compilation, not the subunit itself.
8459 if Nkind (Parent (Gen_Body)) = N_Subunit then
8460 Orig_Body := Corresponding_Stub (Parent (Gen_Body));
8462 Orig_Body := Gen_Body;
8465 Body_Unit := Unit (Cunit (Get_Source_Unit (Orig_Body)));
8467 -- If the instantiation and the generic definition appear in the same
8468 -- package declaration, this is an early instantiation. If they appear
8469 -- in the same declarative part, it is an early instantiation only if
8470 -- the generic body appears textually later, and the generic body is
8471 -- also in the main unit.
8473 -- If instance is nested within a subprogram, and the generic body
8474 -- is not, the instance is delayed because the enclosing body is. If
8475 -- instance and body are within the same scope, or the same subprogram
8476 -- body, indicate explicitly that the instance is delayed.
8479 (Gen_Unit = Act_Unit
8480 and then (Nkind_In (Gen_Unit, N_Package_Declaration,
8481 N_Generic_Package_Declaration)
8482 or else (Gen_Unit = Body_Unit
8483 and then True_Sloc (N) < Sloc (Orig_Body)))
8484 and then Is_In_Main_Unit (Gen_Unit)
8485 and then (Scope (Act_Id) = Scope (Gen_Id)
8486 or else In_Same_Enclosing_Subp));
8488 -- If this is an early instantiation, the freeze node is placed after
8489 -- the generic body. Otherwise, if the generic appears in an instance,
8490 -- we cannot freeze the current instance until the outer one is frozen.
8491 -- This is only relevant if the current instance is nested within some
8492 -- inner scope not itself within the outer instance. If this scope is
8493 -- a package body in the same declarative part as the outer instance,
8494 -- then that body needs to be frozen after the outer instance. Finally,
8495 -- if no delay is needed, we place the freeze node at the end of the
8496 -- current declarative part.
8498 if Expander_Active then
8499 Ensure_Freeze_Node (Act_Id);
8500 F_Node := Freeze_Node (Act_Id);
8503 Insert_After (Orig_Body, F_Node);
8505 elsif Is_Generic_Instance (Par)
8506 and then Present (Freeze_Node (Par))
8507 and then Scope (Act_Id) /= Par
8509 -- Freeze instance of inner generic after instance of enclosing
8512 if In_Same_Declarative_Part (Freeze_Node (Par), N) then
8514 -- Handle the following case:
8516 -- package Parent_Inst is new ...
8519 -- procedure P ... -- this body freezes Parent_Inst
8521 -- package Inst is new ...
8523 -- In this particular scenario, the freeze node for Inst must
8524 -- be inserted in the same manner as that of Parent_Inst,
8525 -- before the next source body or at the end of the declarative
8526 -- list (body not available). If body P did not exist and
8527 -- Parent_Inst was frozen after Inst, either by a body
8528 -- following Inst or at the end of the declarative region,
8529 -- the freeze node for Inst must be inserted after that of
8530 -- Parent_Inst. This relation is established by comparing
8531 -- the Slocs of Parent_Inst freeze node and Inst.
8533 if List_Containing (Get_Package_Instantiation_Node (Par)) =
8535 and then Sloc (Freeze_Node (Par)) < Sloc (N)
8537 Insert_Freeze_Node_For_Instance (N, F_Node);
8539 Insert_After (Freeze_Node (Par), F_Node);
8542 -- Freeze package enclosing instance of inner generic after
8543 -- instance of enclosing generic.
8545 elsif Nkind_In (Parent (N), N_Package_Body, N_Subprogram_Body)
8546 and then In_Same_Declarative_Part (Freeze_Node (Par), Parent (N))
8549 Enclosing : Entity_Id;
8552 Enclosing := Corresponding_Spec (Parent (N));
8554 if No (Enclosing) then
8555 Enclosing := Defining_Entity (Parent (N));
8558 Insert_Freeze_Node_For_Instance (N, F_Node);
8559 Ensure_Freeze_Node (Enclosing);
8561 if not Is_List_Member (Freeze_Node (Enclosing)) then
8563 -- The enclosing context is a subunit, insert the freeze
8564 -- node after the stub.
8566 if Nkind (Parent (Parent (N))) = N_Subunit then
8567 Insert_Freeze_Node_For_Instance
8568 (Corresponding_Stub (Parent (Parent (N))),
8569 Freeze_Node (Enclosing));
8571 -- The enclosing context is a package with a stub body
8572 -- which has already been replaced by the real body.
8573 -- Insert the freeze node after the actual body.
8575 elsif Ekind (Enclosing) = E_Package
8576 and then Present (Body_Entity (Enclosing))
8577 and then Was_Originally_Stub
8578 (Parent (Body_Entity (Enclosing)))
8580 Insert_Freeze_Node_For_Instance
8581 (Parent (Body_Entity (Enclosing)),
8582 Freeze_Node (Enclosing));
8584 -- The parent instance has been frozen before the body of
8585 -- the enclosing package, insert the freeze node after
8588 elsif List_Containing (Freeze_Node (Par)) =
8589 List_Containing (Parent (N))
8590 and then Sloc (Freeze_Node (Par)) < Sloc (Parent (N))
8592 Insert_Freeze_Node_For_Instance
8593 (Parent (N), Freeze_Node (Enclosing));
8597 (Freeze_Node (Par), Freeze_Node (Enclosing));
8603 Insert_Freeze_Node_For_Instance (N, F_Node);
8607 Insert_Freeze_Node_For_Instance (N, F_Node);
8611 Set_Is_Frozen (Act_Id);
8612 Insert_Before (N, Act_Body);
8613 Mark_Rewrite_Insertion (Act_Body);
8616 -----------------------------
8617 -- Install_Formal_Packages --
8618 -----------------------------
8620 procedure Install_Formal_Packages (Par : Entity_Id) is
8623 Gen_E : Entity_Id := Empty;
8626 E := First_Entity (Par);
8628 -- If we are installing an instance parent, locate the formal packages
8629 -- of its generic parent.
8631 if Is_Generic_Instance (Par) then
8632 Gen := Generic_Parent (Package_Specification (Par));
8633 Gen_E := First_Entity (Gen);
8636 while Present (E) loop
8637 if Ekind (E) = E_Package
8638 and then Nkind (Parent (E)) = N_Package_Renaming_Declaration
8640 -- If this is the renaming for the parent instance, done
8642 if Renamed_Object (E) = Par then
8645 -- The visibility of a formal of an enclosing generic is already
8648 elsif Denotes_Formal_Package (E) then
8651 elsif Present (Associated_Formal_Package (E)) then
8652 Check_Generic_Actuals (Renamed_Object (E), True);
8653 Set_Is_Hidden (E, False);
8655 -- Find formal package in generic unit that corresponds to
8656 -- (instance of) formal package in instance.
8658 while Present (Gen_E) and then Chars (Gen_E) /= Chars (E) loop
8659 Next_Entity (Gen_E);
8662 if Present (Gen_E) then
8663 Map_Formal_Package_Entities (Gen_E, E);
8669 if Present (Gen_E) then
8670 Next_Entity (Gen_E);
8673 end Install_Formal_Packages;
8675 --------------------
8676 -- Install_Parent --
8677 --------------------
8679 procedure Install_Parent (P : Entity_Id; In_Body : Boolean := False) is
8680 Ancestors : constant Elist_Id := New_Elmt_List;
8681 S : constant Entity_Id := Current_Scope;
8682 Inst_Par : Entity_Id;
8683 First_Par : Entity_Id;
8684 Inst_Node : Node_Id;
8685 Gen_Par : Entity_Id;
8686 First_Gen : Entity_Id;
8689 procedure Install_Noninstance_Specs (Par : Entity_Id);
8690 -- Install the scopes of noninstance parent units ending with Par
8692 procedure Install_Spec (Par : Entity_Id);
8693 -- The child unit is within the declarative part of the parent, so the
8694 -- declarations within the parent are immediately visible.
8696 -------------------------------
8697 -- Install_Noninstance_Specs --
8698 -------------------------------
8700 procedure Install_Noninstance_Specs (Par : Entity_Id) is
8703 and then Par /= Standard_Standard
8704 and then not In_Open_Scopes (Par)
8706 Install_Noninstance_Specs (Scope (Par));
8709 end Install_Noninstance_Specs;
8715 procedure Install_Spec (Par : Entity_Id) is
8716 Spec : constant Node_Id := Package_Specification (Par);
8719 -- If this parent of the child instance is a top-level unit,
8720 -- then record the unit and its visibility for later resetting in
8721 -- Remove_Parent. We exclude units that are generic instances, as we
8722 -- only want to record this information for the ultimate top-level
8723 -- noninstance parent (is that always correct???).
8725 if Scope (Par) = Standard_Standard
8726 and then not Is_Generic_Instance (Par)
8728 Parent_Unit_Visible := Is_Immediately_Visible (Par);
8729 Instance_Parent_Unit := Par;
8732 -- Open the parent scope and make it and its declarations visible.
8733 -- If this point is not within a body, then only the visible
8734 -- declarations should be made visible, and installation of the
8735 -- private declarations is deferred until the appropriate point
8736 -- within analysis of the spec being instantiated (see the handling
8737 -- of parent visibility in Analyze_Package_Specification). This is
8738 -- relaxed in the case where the parent unit is Ada.Tags, to avoid
8739 -- private view problems that occur when compiling instantiations of
8740 -- a generic child of that package (Generic_Dispatching_Constructor).
8741 -- If the instance freezes a tagged type, inlinings of operations
8742 -- from Ada.Tags may need the full view of type Tag. If inlining took
8743 -- proper account of establishing visibility of inlined subprograms'
8744 -- parents then it should be possible to remove this
8745 -- special check. ???
8748 Set_Is_Immediately_Visible (Par);
8749 Install_Visible_Declarations (Par);
8750 Set_Use (Visible_Declarations (Spec));
8752 if In_Body or else Is_RTU (Par, Ada_Tags) then
8753 Install_Private_Declarations (Par);
8754 Set_Use (Private_Declarations (Spec));
8758 -- Start of processing for Install_Parent
8761 -- We need to install the parent instance to compile the instantiation
8762 -- of the child, but the child instance must appear in the current
8763 -- scope. Given that we cannot place the parent above the current scope
8764 -- in the scope stack, we duplicate the current scope and unstack both
8765 -- after the instantiation is complete.
8767 -- If the parent is itself the instantiation of a child unit, we must
8768 -- also stack the instantiation of its parent, and so on. Each such
8769 -- ancestor is the prefix of the name in a prior instantiation.
8771 -- If this is a nested instance, the parent unit itself resolves to
8772 -- a renaming of the parent instance, whose declaration we need.
8774 -- Finally, the parent may be a generic (not an instance) when the
8775 -- child unit appears as a formal package.
8779 if Present (Renamed_Entity (Inst_Par)) then
8780 Inst_Par := Renamed_Entity (Inst_Par);
8783 First_Par := Inst_Par;
8785 Gen_Par := Generic_Parent (Package_Specification (Inst_Par));
8787 First_Gen := Gen_Par;
8789 while Present (Gen_Par)
8790 and then Is_Child_Unit (Gen_Par)
8792 -- Load grandparent instance as well
8794 Inst_Node := Get_Package_Instantiation_Node (Inst_Par);
8796 if Nkind (Name (Inst_Node)) = N_Expanded_Name then
8797 Inst_Par := Entity (Prefix (Name (Inst_Node)));
8799 if Present (Renamed_Entity (Inst_Par)) then
8800 Inst_Par := Renamed_Entity (Inst_Par);
8803 Gen_Par := Generic_Parent (Package_Specification (Inst_Par));
8805 if Present (Gen_Par) then
8806 Prepend_Elmt (Inst_Par, Ancestors);
8809 -- Parent is not the name of an instantiation
8811 Install_Noninstance_Specs (Inst_Par);
8822 if Present (First_Gen) then
8823 Append_Elmt (First_Par, Ancestors);
8825 Install_Noninstance_Specs (First_Par);
8828 if not Is_Empty_Elmt_List (Ancestors) then
8829 Elmt := First_Elmt (Ancestors);
8830 while Present (Elmt) loop
8831 Install_Spec (Node (Elmt));
8832 Install_Formal_Packages (Node (Elmt));
8842 -------------------------------
8843 -- Install_Hidden_Primitives --
8844 -------------------------------
8846 procedure Install_Hidden_Primitives
8847 (Prims_List : in out Elist_Id;
8852 List : Elist_Id := No_Elist;
8853 Prim_G_Elmt : Elmt_Id;
8854 Prim_A_Elmt : Elmt_Id;
8859 -- No action needed in case of serious errors because we cannot trust
8860 -- in the order of primitives
8862 if Serious_Errors_Detected > 0 then
8865 -- No action possible if we don't have available the list of primitive
8869 or else not Is_Record_Type (Gen_T)
8870 or else not Is_Tagged_Type (Gen_T)
8871 or else not Is_Record_Type (Act_T)
8872 or else not Is_Tagged_Type (Act_T)
8876 -- There is no need to handle interface types since their primitives
8879 elsif Is_Interface (Gen_T) then
8883 Prim_G_Elmt := First_Elmt (Primitive_Operations (Gen_T));
8885 if not Is_Class_Wide_Type (Act_T) then
8886 Prim_A_Elmt := First_Elmt (Primitive_Operations (Act_T));
8888 Prim_A_Elmt := First_Elmt (Primitive_Operations (Root_Type (Act_T)));
8892 -- Skip predefined primitives in the generic formal
8894 while Present (Prim_G_Elmt)
8895 and then Is_Predefined_Dispatching_Operation (Node (Prim_G_Elmt))
8897 Next_Elmt (Prim_G_Elmt);
8900 -- Skip predefined primitives in the generic actual
8902 while Present (Prim_A_Elmt)
8903 and then Is_Predefined_Dispatching_Operation (Node (Prim_A_Elmt))
8905 Next_Elmt (Prim_A_Elmt);
8908 exit when No (Prim_G_Elmt) or else No (Prim_A_Elmt);
8910 Prim_G := Node (Prim_G_Elmt);
8911 Prim_A := Node (Prim_A_Elmt);
8913 -- There is no need to handle interface primitives because their
8914 -- primitives are not hidden
8916 exit when Present (Interface_Alias (Prim_G));
8918 -- Here we install one hidden primitive
8920 if Chars (Prim_G) /= Chars (Prim_A)
8921 and then Has_Suffix (Prim_A, 'P')
8922 and then Remove_Suffix (Prim_A, 'P') = Chars (Prim_G)
8924 Set_Chars (Prim_A, Chars (Prim_G));
8926 if List = No_Elist then
8927 List := New_Elmt_List;
8930 Append_Elmt (Prim_A, List);
8933 Next_Elmt (Prim_A_Elmt);
8934 Next_Elmt (Prim_G_Elmt);
8937 -- Append the elements to the list of temporarily visible primitives
8938 -- avoiding duplicates.
8940 if Present (List) then
8941 if No (Prims_List) then
8942 Prims_List := New_Elmt_List;
8945 Elmt := First_Elmt (List);
8946 while Present (Elmt) loop
8947 Append_Unique_Elmt (Node (Elmt), Prims_List);
8951 end Install_Hidden_Primitives;
8953 -------------------------------
8954 -- Restore_Hidden_Primitives --
8955 -------------------------------
8957 procedure Restore_Hidden_Primitives (Prims_List : in out Elist_Id) is
8958 Prim_Elmt : Elmt_Id;
8962 if Prims_List /= No_Elist then
8963 Prim_Elmt := First_Elmt (Prims_List);
8964 while Present (Prim_Elmt) loop
8965 Prim := Node (Prim_Elmt);
8966 Set_Chars (Prim, Add_Suffix (Prim, 'P'));
8967 Next_Elmt (Prim_Elmt);
8970 Prims_List := No_Elist;
8972 end Restore_Hidden_Primitives;
8974 --------------------------------
8975 -- Instantiate_Formal_Package --
8976 --------------------------------
8978 function Instantiate_Formal_Package
8981 Analyzed_Formal : Node_Id) return List_Id
8983 Loc : constant Source_Ptr := Sloc (Actual);
8984 Actual_Pack : Entity_Id;
8985 Formal_Pack : Entity_Id;
8986 Gen_Parent : Entity_Id;
8989 Parent_Spec : Node_Id;
8991 procedure Find_Matching_Actual
8993 Act : in out Entity_Id);
8994 -- We need to associate each formal entity in the formal package with
8995 -- the corresponding entity in the actual package. The actual package
8996 -- has been analyzed and possibly expanded, and as a result there is
8997 -- no one-to-one correspondence between the two lists (for example,
8998 -- the actual may include subtypes, itypes, and inherited primitive
8999 -- operations, interspersed among the renaming declarations for the
9000 -- actuals) . We retrieve the corresponding actual by name because each
9001 -- actual has the same name as the formal, and they do appear in the
9004 function Get_Formal_Entity (N : Node_Id) return Entity_Id;
9005 -- Retrieve entity of defining entity of generic formal parameter.
9006 -- Only the declarations of formals need to be considered when
9007 -- linking them to actuals, but the declarative list may include
9008 -- internal entities generated during analysis, and those are ignored.
9010 procedure Match_Formal_Entity
9011 (Formal_Node : Node_Id;
9012 Formal_Ent : Entity_Id;
9013 Actual_Ent : Entity_Id);
9014 -- Associates the formal entity with the actual. In the case where
9015 -- Formal_Ent is a formal package, this procedure iterates through all
9016 -- of its formals and enters associations between the actuals occurring
9017 -- in the formal package's corresponding actual package (given by
9018 -- Actual_Ent) and the formal package's formal parameters. This
9019 -- procedure recurses if any of the parameters is itself a package.
9021 function Is_Instance_Of
9022 (Act_Spec : Entity_Id;
9023 Gen_Anc : Entity_Id) return Boolean;
9024 -- The actual can be an instantiation of a generic within another
9025 -- instance, in which case there is no direct link from it to the
9026 -- original generic ancestor. In that case, we recognize that the
9027 -- ultimate ancestor is the same by examining names and scopes.
9029 procedure Process_Nested_Formal (Formal : Entity_Id);
9030 -- If the current formal is declared with a box, its own formals are
9031 -- visible in the instance, as they were in the generic, and their
9032 -- Hidden flag must be reset. If some of these formals are themselves
9033 -- packages declared with a box, the processing must be recursive.
9035 --------------------------
9036 -- Find_Matching_Actual --
9037 --------------------------
9039 procedure Find_Matching_Actual
9041 Act : in out Entity_Id)
9043 Formal_Ent : Entity_Id;
9046 case Nkind (Original_Node (F)) is
9047 when N_Formal_Object_Declaration |
9048 N_Formal_Type_Declaration =>
9049 Formal_Ent := Defining_Identifier (F);
9051 while Chars (Act) /= Chars (Formal_Ent) loop
9055 when N_Formal_Subprogram_Declaration |
9056 N_Formal_Package_Declaration |
9057 N_Package_Declaration |
9058 N_Generic_Package_Declaration =>
9059 Formal_Ent := Defining_Entity (F);
9061 while Chars (Act) /= Chars (Formal_Ent) loop
9066 raise Program_Error;
9068 end Find_Matching_Actual;
9070 -------------------------
9071 -- Match_Formal_Entity --
9072 -------------------------
9074 procedure Match_Formal_Entity
9075 (Formal_Node : Node_Id;
9076 Formal_Ent : Entity_Id;
9077 Actual_Ent : Entity_Id)
9079 Act_Pkg : Entity_Id;
9082 Set_Instance_Of (Formal_Ent, Actual_Ent);
9084 if Ekind (Actual_Ent) = E_Package then
9086 -- Record associations for each parameter
9088 Act_Pkg := Actual_Ent;
9091 A_Ent : Entity_Id := First_Entity (Act_Pkg);
9100 -- Retrieve the actual given in the formal package declaration
9102 Actual := Entity (Name (Original_Node (Formal_Node)));
9104 -- The actual in the formal package declaration may be a
9105 -- renamed generic package, in which case we want to retrieve
9106 -- the original generic in order to traverse its formal part.
9108 if Present (Renamed_Entity (Actual)) then
9109 Gen_Decl := Unit_Declaration_Node (Renamed_Entity (Actual));
9111 Gen_Decl := Unit_Declaration_Node (Actual);
9114 Formals := Generic_Formal_Declarations (Gen_Decl);
9116 if Present (Formals) then
9117 F_Node := First_Non_Pragma (Formals);
9122 while Present (A_Ent)
9123 and then Present (F_Node)
9124 and then A_Ent /= First_Private_Entity (Act_Pkg)
9126 F_Ent := Get_Formal_Entity (F_Node);
9128 if Present (F_Ent) then
9130 -- This is a formal of the original package. Record
9131 -- association and recurse.
9133 Find_Matching_Actual (F_Node, A_Ent);
9134 Match_Formal_Entity (F_Node, F_Ent, A_Ent);
9135 Next_Entity (A_Ent);
9138 Next_Non_Pragma (F_Node);
9142 end Match_Formal_Entity;
9144 -----------------------
9145 -- Get_Formal_Entity --
9146 -----------------------
9148 function Get_Formal_Entity (N : Node_Id) return Entity_Id is
9149 Kind : constant Node_Kind := Nkind (Original_Node (N));
9152 when N_Formal_Object_Declaration =>
9153 return Defining_Identifier (N);
9155 when N_Formal_Type_Declaration =>
9156 return Defining_Identifier (N);
9158 when N_Formal_Subprogram_Declaration =>
9159 return Defining_Unit_Name (Specification (N));
9161 when N_Formal_Package_Declaration =>
9162 return Defining_Identifier (Original_Node (N));
9164 when N_Generic_Package_Declaration =>
9165 return Defining_Identifier (Original_Node (N));
9167 -- All other declarations are introduced by semantic analysis and
9168 -- have no match in the actual.
9173 end Get_Formal_Entity;
9175 --------------------
9176 -- Is_Instance_Of --
9177 --------------------
9179 function Is_Instance_Of
9180 (Act_Spec : Entity_Id;
9181 Gen_Anc : Entity_Id) return Boolean
9183 Gen_Par : constant Entity_Id := Generic_Parent (Act_Spec);
9186 if No (Gen_Par) then
9189 -- Simplest case: the generic parent of the actual is the formal
9191 elsif Gen_Par = Gen_Anc then
9194 elsif Chars (Gen_Par) /= Chars (Gen_Anc) then
9197 -- The actual may be obtained through several instantiations. Its
9198 -- scope must itself be an instance of a generic declared in the
9199 -- same scope as the formal. Any other case is detected above.
9201 elsif not Is_Generic_Instance (Scope (Gen_Par)) then
9205 return Generic_Parent (Parent (Scope (Gen_Par))) = Scope (Gen_Anc);
9209 ---------------------------
9210 -- Process_Nested_Formal --
9211 ---------------------------
9213 procedure Process_Nested_Formal (Formal : Entity_Id) is
9217 if Present (Associated_Formal_Package (Formal))
9218 and then Box_Present (Parent (Associated_Formal_Package (Formal)))
9220 Ent := First_Entity (Formal);
9221 while Present (Ent) loop
9222 Set_Is_Hidden (Ent, False);
9223 Set_Is_Visible_Formal (Ent);
9224 Set_Is_Potentially_Use_Visible
9225 (Ent, Is_Potentially_Use_Visible (Formal));
9227 if Ekind (Ent) = E_Package then
9228 exit when Renamed_Entity (Ent) = Renamed_Entity (Formal);
9229 Process_Nested_Formal (Ent);
9235 end Process_Nested_Formal;
9237 -- Start of processing for Instantiate_Formal_Package
9242 if not Is_Entity_Name (Actual)
9243 or else Ekind (Entity (Actual)) /= E_Package
9246 ("expect package instance to instantiate formal", Actual);
9247 Abandon_Instantiation (Actual);
9248 raise Program_Error;
9251 Actual_Pack := Entity (Actual);
9252 Set_Is_Instantiated (Actual_Pack);
9254 -- The actual may be a renamed package, or an outer generic formal
9255 -- package whose instantiation is converted into a renaming.
9257 if Present (Renamed_Object (Actual_Pack)) then
9258 Actual_Pack := Renamed_Object (Actual_Pack);
9261 if Nkind (Analyzed_Formal) = N_Formal_Package_Declaration then
9262 Gen_Parent := Get_Instance_Of (Entity (Name (Analyzed_Formal)));
9263 Formal_Pack := Defining_Identifier (Analyzed_Formal);
9266 Generic_Parent (Specification (Analyzed_Formal));
9268 Defining_Unit_Name (Specification (Analyzed_Formal));
9271 if Nkind (Parent (Actual_Pack)) = N_Defining_Program_Unit_Name then
9272 Parent_Spec := Package_Specification (Actual_Pack);
9274 Parent_Spec := Parent (Actual_Pack);
9277 if Gen_Parent = Any_Id then
9279 ("previous error in declaration of formal package", Actual);
9280 Abandon_Instantiation (Actual);
9283 Is_Instance_Of (Parent_Spec, Get_Instance_Of (Gen_Parent))
9289 ("actual parameter must be instance of&", Actual, Gen_Parent);
9290 Abandon_Instantiation (Actual);
9293 Set_Instance_Of (Defining_Identifier (Formal), Actual_Pack);
9294 Map_Formal_Package_Entities (Formal_Pack, Actual_Pack);
9297 Make_Package_Renaming_Declaration (Loc,
9298 Defining_Unit_Name => New_Copy (Defining_Identifier (Formal)),
9299 Name => New_Occurrence_Of (Actual_Pack, Loc));
9301 Set_Associated_Formal_Package (Defining_Unit_Name (Nod),
9302 Defining_Identifier (Formal));
9303 Decls := New_List (Nod);
9305 -- If the formal F has a box, then the generic declarations are
9306 -- visible in the generic G. In an instance of G, the corresponding
9307 -- entities in the actual for F (which are the actuals for the
9308 -- instantiation of the generic that F denotes) must also be made
9309 -- visible for analysis of the current instance. On exit from the
9310 -- current instance, those entities are made private again. If the
9311 -- actual is currently in use, these entities are also use-visible.
9313 -- The loop through the actual entities also steps through the formal
9314 -- entities and enters associations from formals to actuals into the
9315 -- renaming map. This is necessary to properly handle checking of
9316 -- actual parameter associations for later formals that depend on
9317 -- actuals declared in the formal package.
9319 -- In Ada 2005, partial parameterization requires that we make
9320 -- visible the actuals corresponding to formals that were defaulted
9321 -- in the formal package. There formals are identified because they
9322 -- remain formal generics within the formal package, rather than
9323 -- being renamings of the actuals supplied.
9326 Gen_Decl : constant Node_Id :=
9327 Unit_Declaration_Node (Gen_Parent);
9328 Formals : constant List_Id :=
9329 Generic_Formal_Declarations (Gen_Decl);
9331 Actual_Ent : Entity_Id;
9332 Actual_Of_Formal : Node_Id;
9333 Formal_Node : Node_Id;
9334 Formal_Ent : Entity_Id;
9337 if Present (Formals) then
9338 Formal_Node := First_Non_Pragma (Formals);
9340 Formal_Node := Empty;
9343 Actual_Ent := First_Entity (Actual_Pack);
9345 First (Visible_Declarations (Specification (Analyzed_Formal)));
9346 while Present (Actual_Ent)
9347 and then Actual_Ent /= First_Private_Entity (Actual_Pack)
9349 if Present (Formal_Node) then
9350 Formal_Ent := Get_Formal_Entity (Formal_Node);
9352 if Present (Formal_Ent) then
9353 Find_Matching_Actual (Formal_Node, Actual_Ent);
9355 (Formal_Node, Formal_Ent, Actual_Ent);
9357 -- We iterate at the same time over the actuals of the
9358 -- local package created for the formal, to determine
9359 -- which one of the formals of the original generic were
9360 -- defaulted in the formal. The corresponding actual
9361 -- entities are visible in the enclosing instance.
9363 if Box_Present (Formal)
9365 (Present (Actual_Of_Formal)
9368 (Get_Formal_Entity (Actual_Of_Formal)))
9370 Set_Is_Hidden (Actual_Ent, False);
9371 Set_Is_Visible_Formal (Actual_Ent);
9372 Set_Is_Potentially_Use_Visible
9373 (Actual_Ent, In_Use (Actual_Pack));
9375 if Ekind (Actual_Ent) = E_Package then
9376 Process_Nested_Formal (Actual_Ent);
9380 Set_Is_Hidden (Actual_Ent);
9381 Set_Is_Potentially_Use_Visible (Actual_Ent, False);
9385 Next_Non_Pragma (Formal_Node);
9386 Next (Actual_Of_Formal);
9389 -- No further formals to match, but the generic part may
9390 -- contain inherited operation that are not hidden in the
9391 -- enclosing instance.
9393 Next_Entity (Actual_Ent);
9397 -- Inherited subprograms generated by formal derived types are
9398 -- also visible if the types are.
9400 Actual_Ent := First_Entity (Actual_Pack);
9401 while Present (Actual_Ent)
9402 and then Actual_Ent /= First_Private_Entity (Actual_Pack)
9404 if Is_Overloadable (Actual_Ent)
9406 Nkind (Parent (Actual_Ent)) = N_Subtype_Declaration
9408 not Is_Hidden (Defining_Identifier (Parent (Actual_Ent)))
9410 Set_Is_Hidden (Actual_Ent, False);
9411 Set_Is_Potentially_Use_Visible
9412 (Actual_Ent, In_Use (Actual_Pack));
9415 Next_Entity (Actual_Ent);
9419 -- If the formal is not declared with a box, reanalyze it as an
9420 -- abbreviated instantiation, to verify the matching rules of 12.7.
9421 -- The actual checks are performed after the generic associations
9422 -- have been analyzed, to guarantee the same visibility for this
9423 -- instantiation and for the actuals.
9425 -- In Ada 2005, the generic associations for the formal can include
9426 -- defaulted parameters. These are ignored during check. This
9427 -- internal instantiation is removed from the tree after conformance
9428 -- checking, because it contains formal declarations for those
9429 -- defaulted parameters, and those should not reach the back-end.
9431 if not Box_Present (Formal) then
9433 I_Pack : constant Entity_Id :=
9434 Make_Temporary (Sloc (Actual), 'P');
9437 Set_Is_Internal (I_Pack);
9440 Make_Package_Instantiation (Sloc (Actual),
9441 Defining_Unit_Name => I_Pack,
9444 (Get_Instance_Of (Gen_Parent), Sloc (Actual)),
9445 Generic_Associations =>
9446 Generic_Associations (Formal)));
9452 end Instantiate_Formal_Package;
9454 -----------------------------------
9455 -- Instantiate_Formal_Subprogram --
9456 -----------------------------------
9458 function Instantiate_Formal_Subprogram
9461 Analyzed_Formal : Node_Id) return Node_Id
9464 Formal_Sub : constant Entity_Id :=
9465 Defining_Unit_Name (Specification (Formal));
9466 Analyzed_S : constant Entity_Id :=
9467 Defining_Unit_Name (Specification (Analyzed_Formal));
9468 Decl_Node : Node_Id;
9472 function From_Parent_Scope (Subp : Entity_Id) return Boolean;
9473 -- If the generic is a child unit, the parent has been installed on the
9474 -- scope stack, but a default subprogram cannot resolve to something
9475 -- on the parent because that parent is not really part of the visible
9476 -- context (it is there to resolve explicit local entities). If the
9477 -- default has resolved in this way, we remove the entity from immediate
9478 -- visibility and analyze the node again to emit an error message or
9479 -- find another visible candidate.
9481 procedure Valid_Actual_Subprogram (Act : Node_Id);
9482 -- Perform legality check and raise exception on failure
9484 -----------------------
9485 -- From_Parent_Scope --
9486 -----------------------
9488 function From_Parent_Scope (Subp : Entity_Id) return Boolean is
9489 Gen_Scope : Node_Id;
9492 Gen_Scope := Scope (Analyzed_S);
9493 while Present (Gen_Scope) and then Is_Child_Unit (Gen_Scope) loop
9494 if Scope (Subp) = Scope (Gen_Scope) then
9498 Gen_Scope := Scope (Gen_Scope);
9502 end From_Parent_Scope;
9504 -----------------------------
9505 -- Valid_Actual_Subprogram --
9506 -----------------------------
9508 procedure Valid_Actual_Subprogram (Act : Node_Id) is
9512 if Is_Entity_Name (Act) then
9513 Act_E := Entity (Act);
9515 elsif Nkind (Act) = N_Selected_Component
9516 and then Is_Entity_Name (Selector_Name (Act))
9518 Act_E := Entity (Selector_Name (Act));
9524 if (Present (Act_E) and then Is_Overloadable (Act_E))
9525 or else Nkind_In (Act, N_Attribute_Reference,
9526 N_Indexed_Component,
9527 N_Character_Literal,
9528 N_Explicit_Dereference)
9534 ("expect subprogram or entry name in instantiation of&",
9535 Instantiation_Node, Formal_Sub);
9536 Abandon_Instantiation (Instantiation_Node);
9538 end Valid_Actual_Subprogram;
9540 -- Start of processing for Instantiate_Formal_Subprogram
9543 New_Spec := New_Copy_Tree (Specification (Formal));
9545 -- The tree copy has created the proper instantiation sloc for the
9546 -- new specification. Use this location for all other constructed
9549 Loc := Sloc (Defining_Unit_Name (New_Spec));
9551 -- Create new entity for the actual (New_Copy_Tree does not)
9553 Set_Defining_Unit_Name
9554 (New_Spec, Make_Defining_Identifier (Loc, Chars (Formal_Sub)));
9556 -- Create new entities for the each of the formals in the
9557 -- specification of the renaming declaration built for the actual.
9559 if Present (Parameter_Specifications (New_Spec)) then
9563 F := First (Parameter_Specifications (New_Spec));
9564 while Present (F) loop
9565 Set_Defining_Identifier (F,
9566 Make_Defining_Identifier (Sloc (F),
9567 Chars => Chars (Defining_Identifier (F))));
9573 -- Find entity of actual. If the actual is an attribute reference, it
9574 -- cannot be resolved here (its formal is missing) but is handled
9575 -- instead in Attribute_Renaming. If the actual is overloaded, it is
9576 -- fully resolved subsequently, when the renaming declaration for the
9577 -- formal is analyzed. If it is an explicit dereference, resolve the
9578 -- prefix but not the actual itself, to prevent interpretation as call.
9580 if Present (Actual) then
9581 Loc := Sloc (Actual);
9582 Set_Sloc (New_Spec, Loc);
9584 if Nkind (Actual) = N_Operator_Symbol then
9585 Find_Direct_Name (Actual);
9587 elsif Nkind (Actual) = N_Explicit_Dereference then
9588 Analyze (Prefix (Actual));
9590 elsif Nkind (Actual) /= N_Attribute_Reference then
9594 Valid_Actual_Subprogram (Actual);
9597 elsif Present (Default_Name (Formal)) then
9598 if not Nkind_In (Default_Name (Formal), N_Attribute_Reference,
9599 N_Selected_Component,
9600 N_Indexed_Component,
9601 N_Character_Literal)
9602 and then Present (Entity (Default_Name (Formal)))
9604 Nam := New_Occurrence_Of (Entity (Default_Name (Formal)), Loc);
9606 Nam := New_Copy (Default_Name (Formal));
9607 Set_Sloc (Nam, Loc);
9610 elsif Box_Present (Formal) then
9612 -- Actual is resolved at the point of instantiation. Create an
9613 -- identifier or operator with the same name as the formal.
9615 if Nkind (Formal_Sub) = N_Defining_Operator_Symbol then
9616 Nam := Make_Operator_Symbol (Loc,
9617 Chars => Chars (Formal_Sub),
9618 Strval => No_String);
9620 Nam := Make_Identifier (Loc, Chars (Formal_Sub));
9623 elsif Nkind (Specification (Formal)) = N_Procedure_Specification
9624 and then Null_Present (Specification (Formal))
9626 -- Generate null body for procedure, for use in the instance
9629 Make_Subprogram_Body (Loc,
9630 Specification => New_Spec,
9631 Declarations => New_List,
9632 Handled_Statement_Sequence =>
9633 Make_Handled_Sequence_Of_Statements (Loc,
9634 Statements => New_List (Make_Null_Statement (Loc))));
9636 Set_Is_Intrinsic_Subprogram (Defining_Unit_Name (New_Spec));
9640 Error_Msg_Sloc := Sloc (Scope (Analyzed_S));
9642 ("missing actual&", Instantiation_Node, Formal_Sub);
9644 ("\in instantiation of & declared#",
9645 Instantiation_Node, Scope (Analyzed_S));
9646 Abandon_Instantiation (Instantiation_Node);
9650 Make_Subprogram_Renaming_Declaration (Loc,
9651 Specification => New_Spec,
9654 -- If we do not have an actual and the formal specified <> then set to
9655 -- get proper default.
9657 if No (Actual) and then Box_Present (Formal) then
9658 Set_From_Default (Decl_Node);
9661 -- Gather possible interpretations for the actual before analyzing the
9662 -- instance. If overloaded, it will be resolved when analyzing the
9663 -- renaming declaration.
9665 if Box_Present (Formal)
9666 and then No (Actual)
9670 if Is_Child_Unit (Scope (Analyzed_S))
9671 and then Present (Entity (Nam))
9673 if not Is_Overloaded (Nam) then
9674 if From_Parent_Scope (Entity (Nam)) then
9675 Set_Is_Immediately_Visible (Entity (Nam), False);
9676 Set_Entity (Nam, Empty);
9677 Set_Etype (Nam, Empty);
9680 Set_Is_Immediately_Visible (Entity (Nam));
9689 Get_First_Interp (Nam, I, It);
9690 while Present (It.Nam) loop
9691 if From_Parent_Scope (It.Nam) then
9695 Get_Next_Interp (I, It);
9702 -- The generic instantiation freezes the actual. This can only be done
9703 -- once the actual is resolved, in the analysis of the renaming
9704 -- declaration. To make the formal subprogram entity available, we set
9705 -- Corresponding_Formal_Spec to point to the formal subprogram entity.
9706 -- This is also needed in Analyze_Subprogram_Renaming for the processing
9707 -- of formal abstract subprograms.
9709 Set_Corresponding_Formal_Spec (Decl_Node, Analyzed_S);
9711 -- We cannot analyze the renaming declaration, and thus find the actual,
9712 -- until all the actuals are assembled in the instance. For subsequent
9713 -- checks of other actuals, indicate the node that will hold the
9714 -- instance of this formal.
9716 Set_Instance_Of (Analyzed_S, Nam);
9718 if Nkind (Actual) = N_Selected_Component
9719 and then Is_Task_Type (Etype (Prefix (Actual)))
9720 and then not Is_Frozen (Etype (Prefix (Actual)))
9722 -- The renaming declaration will create a body, which must appear
9723 -- outside of the instantiation, We move the renaming declaration
9724 -- out of the instance, and create an additional renaming inside,
9725 -- to prevent freezing anomalies.
9728 Anon_Id : constant Entity_Id := Make_Temporary (Loc, 'E');
9731 Set_Defining_Unit_Name (New_Spec, Anon_Id);
9732 Insert_Before (Instantiation_Node, Decl_Node);
9733 Analyze (Decl_Node);
9735 -- Now create renaming within the instance
9738 Make_Subprogram_Renaming_Declaration (Loc,
9739 Specification => New_Copy_Tree (New_Spec),
9740 Name => New_Occurrence_Of (Anon_Id, Loc));
9742 Set_Defining_Unit_Name (Specification (Decl_Node),
9743 Make_Defining_Identifier (Loc, Chars (Formal_Sub)));
9748 end Instantiate_Formal_Subprogram;
9750 ------------------------
9751 -- Instantiate_Object --
9752 ------------------------
9754 function Instantiate_Object
9757 Analyzed_Formal : Node_Id) return List_Id
9759 Gen_Obj : constant Entity_Id := Defining_Identifier (Formal);
9760 A_Gen_Obj : constant Entity_Id :=
9761 Defining_Identifier (Analyzed_Formal);
9762 Acc_Def : Node_Id := Empty;
9763 Act_Assoc : constant Node_Id := Parent (Actual);
9764 Actual_Decl : Node_Id := Empty;
9765 Decl_Node : Node_Id;
9768 List : constant List_Id := New_List;
9769 Loc : constant Source_Ptr := Sloc (Actual);
9770 Orig_Ftyp : constant Entity_Id := Etype (A_Gen_Obj);
9771 Subt_Decl : Node_Id := Empty;
9772 Subt_Mark : Node_Id := Empty;
9775 if Present (Subtype_Mark (Formal)) then
9776 Subt_Mark := Subtype_Mark (Formal);
9778 Check_Access_Definition (Formal);
9779 Acc_Def := Access_Definition (Formal);
9782 -- Sloc for error message on missing actual
9784 Error_Msg_Sloc := Sloc (Scope (A_Gen_Obj));
9786 if Get_Instance_Of (Gen_Obj) /= Gen_Obj then
9787 Error_Msg_N ("duplicate instantiation of generic parameter", Actual);
9790 Set_Parent (List, Parent (Actual));
9794 if Out_Present (Formal) then
9796 -- An IN OUT generic actual must be a name. The instantiation is a
9797 -- renaming declaration. The actual is the name being renamed. We
9798 -- use the actual directly, rather than a copy, because it is not
9799 -- used further in the list of actuals, and because a copy or a use
9800 -- of relocate_node is incorrect if the instance is nested within a
9801 -- generic. In order to simplify ASIS searches, the Generic_Parent
9802 -- field links the declaration to the generic association.
9807 Instantiation_Node, Gen_Obj);
9809 ("\in instantiation of & declared#",
9810 Instantiation_Node, Scope (A_Gen_Obj));
9811 Abandon_Instantiation (Instantiation_Node);
9814 if Present (Subt_Mark) then
9816 Make_Object_Renaming_Declaration (Loc,
9817 Defining_Identifier => New_Copy (Gen_Obj),
9818 Subtype_Mark => New_Copy_Tree (Subt_Mark),
9821 else pragma Assert (Present (Acc_Def));
9823 Make_Object_Renaming_Declaration (Loc,
9824 Defining_Identifier => New_Copy (Gen_Obj),
9825 Access_Definition => New_Copy_Tree (Acc_Def),
9829 Set_Corresponding_Generic_Association (Decl_Node, Act_Assoc);
9831 -- The analysis of the actual may produce Insert_Action nodes, so
9832 -- the declaration must have a context in which to attach them.
9834 Append (Decl_Node, List);
9837 -- Return if the analysis of the actual reported some error
9839 if Etype (Actual) = Any_Type then
9843 -- This check is performed here because Analyze_Object_Renaming will
9844 -- not check it when Comes_From_Source is False. Note though that the
9845 -- check for the actual being the name of an object will be performed
9846 -- in Analyze_Object_Renaming.
9848 if Is_Object_Reference (Actual)
9849 and then Is_Dependent_Component_Of_Mutable_Object (Actual)
9852 ("illegal discriminant-dependent component for in out parameter",
9856 -- The actual has to be resolved in order to check that it is a
9857 -- variable (due to cases such as F (1), where F returns access to
9858 -- an array, and for overloaded prefixes).
9860 Ftyp := Get_Instance_Of (Etype (A_Gen_Obj));
9862 -- If the type of the formal is not itself a formal, and the current
9863 -- unit is a child unit, the formal type must be declared in a
9864 -- parent, and must be retrieved by visibility.
9867 and then Is_Generic_Unit (Scope (Ftyp))
9868 and then Is_Child_Unit (Scope (A_Gen_Obj))
9871 Temp : constant Node_Id :=
9872 New_Copy_Tree (Subtype_Mark (Analyzed_Formal));
9874 Set_Entity (Temp, Empty);
9876 Ftyp := Entity (Temp);
9880 if Is_Private_Type (Ftyp)
9881 and then not Is_Private_Type (Etype (Actual))
9882 and then (Base_Type (Full_View (Ftyp)) = Base_Type (Etype (Actual))
9883 or else Base_Type (Etype (Actual)) = Ftyp)
9885 -- If the actual has the type of the full view of the formal, or
9886 -- else a non-private subtype of the formal, then the visibility
9887 -- of the formal type has changed. Add to the actuals a subtype
9888 -- declaration that will force the exchange of views in the body
9889 -- of the instance as well.
9892 Make_Subtype_Declaration (Loc,
9893 Defining_Identifier => Make_Temporary (Loc, 'P'),
9894 Subtype_Indication => New_Occurrence_Of (Ftyp, Loc));
9896 Prepend (Subt_Decl, List);
9898 Prepend_Elmt (Full_View (Ftyp), Exchanged_Views);
9899 Exchange_Declarations (Ftyp);
9902 Resolve (Actual, Ftyp);
9904 if not Denotes_Variable (Actual) then
9906 ("actual for& must be a variable", Actual, Gen_Obj);
9908 elsif Base_Type (Ftyp) /= Base_Type (Etype (Actual)) then
9910 -- Ada 2005 (AI-423): For a generic formal object of mode in out,
9911 -- the type of the actual shall resolve to a specific anonymous
9914 if Ada_Version < Ada_2005
9916 Ekind (Base_Type (Ftyp)) /=
9917 E_Anonymous_Access_Type
9919 Ekind (Base_Type (Etype (Actual))) /=
9920 E_Anonymous_Access_Type
9922 Error_Msg_NE ("type of actual does not match type of&",
9927 Note_Possible_Modification (Actual, Sure => True);
9929 -- Check for instantiation of atomic/volatile actual for
9930 -- non-atomic/volatile formal (RM C.6 (12)).
9932 if Is_Atomic_Object (Actual)
9933 and then not Is_Atomic (Orig_Ftyp)
9936 ("cannot instantiate non-atomic formal object " &
9937 "with atomic actual", Actual);
9939 elsif Is_Volatile_Object (Actual)
9940 and then not Is_Volatile (Orig_Ftyp)
9943 ("cannot instantiate non-volatile formal object " &
9944 "with volatile actual", Actual);
9947 -- Formal in-parameter
9950 -- The instantiation of a generic formal in-parameter is constant
9951 -- declaration. The actual is the expression for that declaration.
9953 if Present (Actual) then
9954 if Present (Subt_Mark) then
9956 else pragma Assert (Present (Acc_Def));
9961 Make_Object_Declaration (Loc,
9962 Defining_Identifier => New_Copy (Gen_Obj),
9963 Constant_Present => True,
9964 Null_Exclusion_Present => Null_Exclusion_Present (Formal),
9965 Object_Definition => New_Copy_Tree (Def),
9966 Expression => Actual);
9968 Set_Corresponding_Generic_Association (Decl_Node, Act_Assoc);
9970 -- A generic formal object of a tagged type is defined to be
9971 -- aliased so the new constant must also be treated as aliased.
9973 if Is_Tagged_Type (Etype (A_Gen_Obj)) then
9974 Set_Aliased_Present (Decl_Node);
9977 Append (Decl_Node, List);
9979 -- No need to repeat (pre-)analysis of some expression nodes
9980 -- already handled in Preanalyze_Actuals.
9982 if Nkind (Actual) /= N_Allocator then
9985 -- Return if the analysis of the actual reported some error
9987 if Etype (Actual) = Any_Type then
9993 Formal_Type : constant Entity_Id := Etype (A_Gen_Obj);
9997 Typ := Get_Instance_Of (Formal_Type);
9999 Freeze_Before (Instantiation_Node, Typ);
10001 -- If the actual is an aggregate, perform name resolution on
10002 -- its components (the analysis of an aggregate does not do it)
10003 -- to capture local names that may be hidden if the generic is
10006 if Nkind (Actual) = N_Aggregate then
10007 Preanalyze_And_Resolve (Actual, Typ);
10010 if Is_Limited_Type (Typ)
10011 and then not OK_For_Limited_Init (Typ, Actual)
10014 ("initialization not allowed for limited types", Actual);
10015 Explain_Limited_Type (Typ, Actual);
10019 elsif Present (Default_Expression (Formal)) then
10021 -- Use default to construct declaration
10023 if Present (Subt_Mark) then
10025 else pragma Assert (Present (Acc_Def));
10030 Make_Object_Declaration (Sloc (Formal),
10031 Defining_Identifier => New_Copy (Gen_Obj),
10032 Constant_Present => True,
10033 Null_Exclusion_Present => Null_Exclusion_Present (Formal),
10034 Object_Definition => New_Copy (Def),
10035 Expression => New_Copy_Tree
10036 (Default_Expression (Formal)));
10038 Append (Decl_Node, List);
10039 Set_Analyzed (Expression (Decl_Node), False);
10043 ("missing actual&",
10044 Instantiation_Node, Gen_Obj);
10045 Error_Msg_NE ("\in instantiation of & declared#",
10046 Instantiation_Node, Scope (A_Gen_Obj));
10048 if Is_Scalar_Type (Etype (A_Gen_Obj)) then
10050 -- Create dummy constant declaration so that instance can be
10051 -- analyzed, to minimize cascaded visibility errors.
10053 if Present (Subt_Mark) then
10055 else pragma Assert (Present (Acc_Def));
10060 Make_Object_Declaration (Loc,
10061 Defining_Identifier => New_Copy (Gen_Obj),
10062 Constant_Present => True,
10063 Null_Exclusion_Present => Null_Exclusion_Present (Formal),
10064 Object_Definition => New_Copy (Def),
10066 Make_Attribute_Reference (Sloc (Gen_Obj),
10067 Attribute_Name => Name_First,
10068 Prefix => New_Copy (Def)));
10070 Append (Decl_Node, List);
10073 Abandon_Instantiation (Instantiation_Node);
10078 if Nkind (Actual) in N_Has_Entity then
10079 Actual_Decl := Parent (Entity (Actual));
10082 -- Ada 2005 (AI-423): For a formal object declaration with a null
10083 -- exclusion or an access definition that has a null exclusion: If the
10084 -- actual matching the formal object declaration denotes a generic
10085 -- formal object of another generic unit G, and the instantiation
10086 -- containing the actual occurs within the body of G or within the body
10087 -- of a generic unit declared within the declarative region of G, then
10088 -- the declaration of the formal object of G must have a null exclusion.
10089 -- Otherwise, the subtype of the actual matching the formal object
10090 -- declaration shall exclude null.
10092 if Ada_Version >= Ada_2005
10093 and then Present (Actual_Decl)
10095 Nkind_In (Actual_Decl, N_Formal_Object_Declaration,
10096 N_Object_Declaration)
10097 and then Nkind (Analyzed_Formal) = N_Formal_Object_Declaration
10098 and then not Has_Null_Exclusion (Actual_Decl)
10099 and then Has_Null_Exclusion (Analyzed_Formal)
10101 Error_Msg_Sloc := Sloc (Analyzed_Formal);
10103 ("actual must exclude null to match generic formal#", Actual);
10106 -- An effectively volatile object cannot be used as an actual in
10107 -- a generic instance. The following check is only relevant when
10108 -- SPARK_Mode is on as it is not a standard Ada legality rule.
10111 and then Present (Actual)
10112 and then Is_Effectively_Volatile_Object (Actual)
10115 ("volatile object cannot act as actual in generic instantiation "
10116 & "(SPARK RM 7.1.3(8))", Actual);
10120 end Instantiate_Object;
10122 ------------------------------
10123 -- Instantiate_Package_Body --
10124 ------------------------------
10126 procedure Instantiate_Package_Body
10127 (Body_Info : Pending_Body_Info;
10128 Inlined_Body : Boolean := False;
10129 Body_Optional : Boolean := False)
10131 Act_Decl : constant Node_Id := Body_Info.Act_Decl;
10132 Inst_Node : constant Node_Id := Body_Info.Inst_Node;
10133 Loc : constant Source_Ptr := Sloc (Inst_Node);
10135 Gen_Id : constant Node_Id := Name (Inst_Node);
10136 Gen_Unit : constant Entity_Id := Get_Generic_Entity (Inst_Node);
10137 Gen_Decl : constant Node_Id := Unit_Declaration_Node (Gen_Unit);
10138 Act_Spec : constant Node_Id := Specification (Act_Decl);
10139 Act_Decl_Id : constant Entity_Id := Defining_Entity (Act_Spec);
10141 Act_Body_Name : Node_Id;
10142 Gen_Body : Node_Id;
10143 Gen_Body_Id : Node_Id;
10144 Act_Body : Node_Id;
10145 Act_Body_Id : Entity_Id;
10147 Parent_Installed : Boolean := False;
10148 Save_Style_Check : constant Boolean := Style_Check;
10150 Par_Ent : Entity_Id := Empty;
10151 Par_Vis : Boolean := False;
10153 Vis_Prims_List : Elist_Id := No_Elist;
10154 -- List of primitives made temporarily visible in the instantiation
10155 -- to match the visibility of the formal type
10157 procedure Check_Initialized_Types;
10158 -- In a generic package body, an entity of a generic private type may
10159 -- appear uninitialized. This is suspicious, unless the actual is a
10160 -- fully initialized type.
10162 -----------------------------
10163 -- Check_Initialized_Types --
10164 -----------------------------
10166 procedure Check_Initialized_Types is
10168 Formal : Entity_Id;
10169 Actual : Entity_Id;
10170 Uninit_Var : Entity_Id;
10173 Decl := First (Generic_Formal_Declarations (Gen_Decl));
10174 while Present (Decl) loop
10175 Uninit_Var := Empty;
10177 if Nkind (Decl) = N_Private_Extension_Declaration then
10178 Uninit_Var := Uninitialized_Variable (Decl);
10180 elsif Nkind (Decl) = N_Formal_Type_Declaration
10181 and then Nkind (Formal_Type_Definition (Decl)) =
10182 N_Formal_Private_Type_Definition
10185 Uninitialized_Variable (Formal_Type_Definition (Decl));
10188 if Present (Uninit_Var) then
10189 Formal := Defining_Identifier (Decl);
10190 Actual := First_Entity (Act_Decl_Id);
10192 -- For each formal there is a subtype declaration that renames
10193 -- the actual and has the same name as the formal. Locate the
10194 -- formal for warning message about uninitialized variables
10195 -- in the generic, for which the actual type should be a fully
10196 -- initialized type.
10198 while Present (Actual) loop
10199 exit when Ekind (Actual) = E_Package
10200 and then Present (Renamed_Object (Actual));
10202 if Chars (Actual) = Chars (Formal)
10203 and then not Is_Scalar_Type (Actual)
10204 and then not Is_Fully_Initialized_Type (Actual)
10205 and then Warn_On_No_Value_Assigned
10207 Error_Msg_Node_2 := Formal;
10209 ("generic unit has uninitialized variable& of "
10210 & "formal private type &?v?", Actual, Uninit_Var);
10212 ("actual type for& should be fully initialized type?v?",
10217 Next_Entity (Actual);
10223 end Check_Initialized_Types;
10225 -- Start of processing for Instantiate_Package_Body
10228 Gen_Body_Id := Corresponding_Body (Gen_Decl);
10230 -- The instance body may already have been processed, as the parent of
10231 -- another instance that is inlined (Load_Parent_Of_Generic).
10233 if Present (Corresponding_Body (Instance_Spec (Inst_Node))) then
10237 Expander_Mode_Save_And_Set (Body_Info.Expander_Status);
10239 -- Re-establish the state of information on which checks are suppressed.
10240 -- This information was set in Body_Info at the point of instantiation,
10241 -- and now we restore it so that the instance is compiled using the
10242 -- check status at the instantiation (RM 11.5 (7.2/2), AI95-00224-01).
10244 Local_Suppress_Stack_Top := Body_Info.Local_Suppress_Stack_Top;
10245 Scope_Suppress := Body_Info.Scope_Suppress;
10246 Opt.Ada_Version := Body_Info.Version;
10247 Opt.Ada_Version_Pragma := Body_Info.Version_Pragma;
10248 Restore_Warnings (Body_Info.Warnings);
10249 Opt.SPARK_Mode := Body_Info.SPARK_Mode;
10250 Opt.SPARK_Mode_Pragma := Body_Info.SPARK_Mode_Pragma;
10252 if No (Gen_Body_Id) then
10254 -- Do not look for parent of generic body if none is required.
10255 -- This may happen when the routine is called as part of the
10256 -- Pending_Instantiations processing, when nested instances
10257 -- may precede the one generated from the main unit.
10259 if not Unit_Requires_Body (Defining_Entity (Gen_Decl))
10260 and then Body_Optional
10264 Load_Parent_Of_Generic
10265 (Inst_Node, Specification (Gen_Decl), Body_Optional);
10266 Gen_Body_Id := Corresponding_Body (Gen_Decl);
10270 -- Establish global variable for sloc adjustment and for error recovery
10272 Instantiation_Node := Inst_Node;
10274 if Present (Gen_Body_Id) then
10275 Save_Env (Gen_Unit, Act_Decl_Id);
10276 Style_Check := False;
10277 Current_Sem_Unit := Body_Info.Current_Sem_Unit;
10279 Gen_Body := Unit_Declaration_Node (Gen_Body_Id);
10281 Create_Instantiation_Source
10282 (Inst_Node, Gen_Body_Id, False, S_Adjustment);
10286 (Original_Node (Gen_Body), Empty, Instantiating => True);
10288 -- Build new name (possibly qualified) for body declaration
10290 Act_Body_Id := New_Copy (Act_Decl_Id);
10292 -- Some attributes of spec entity are not inherited by body entity
10294 Set_Handler_Records (Act_Body_Id, No_List);
10296 if Nkind (Defining_Unit_Name (Act_Spec)) =
10297 N_Defining_Program_Unit_Name
10300 Make_Defining_Program_Unit_Name (Loc,
10301 Name => New_Copy_Tree (Name (Defining_Unit_Name (Act_Spec))),
10302 Defining_Identifier => Act_Body_Id);
10304 Act_Body_Name := Act_Body_Id;
10307 Set_Defining_Unit_Name (Act_Body, Act_Body_Name);
10309 Set_Corresponding_Spec (Act_Body, Act_Decl_Id);
10310 Check_Generic_Actuals (Act_Decl_Id, False);
10311 Check_Initialized_Types;
10313 -- Install primitives hidden at the point of the instantiation but
10314 -- visible when processing the generic formals
10320 E := First_Entity (Act_Decl_Id);
10321 while Present (E) loop
10323 and then Is_Generic_Actual_Type (E)
10324 and then Is_Tagged_Type (E)
10326 Install_Hidden_Primitives
10327 (Prims_List => Vis_Prims_List,
10328 Gen_T => Generic_Parent_Type (Parent (E)),
10336 -- If it is a child unit, make the parent instance (which is an
10337 -- instance of the parent of the generic) visible. The parent
10338 -- instance is the prefix of the name of the generic unit.
10340 if Ekind (Scope (Gen_Unit)) = E_Generic_Package
10341 and then Nkind (Gen_Id) = N_Expanded_Name
10343 Par_Ent := Entity (Prefix (Gen_Id));
10344 Par_Vis := Is_Immediately_Visible (Par_Ent);
10345 Install_Parent (Par_Ent, In_Body => True);
10346 Parent_Installed := True;
10348 elsif Is_Child_Unit (Gen_Unit) then
10349 Par_Ent := Scope (Gen_Unit);
10350 Par_Vis := Is_Immediately_Visible (Par_Ent);
10351 Install_Parent (Par_Ent, In_Body => True);
10352 Parent_Installed := True;
10355 -- If the instantiation is a library unit, and this is the main unit,
10356 -- then build the resulting compilation unit nodes for the instance.
10357 -- If this is a compilation unit but it is not the main unit, then it
10358 -- is the body of a unit in the context, that is being compiled
10359 -- because it is encloses some inlined unit or another generic unit
10360 -- being instantiated. In that case, this body is not part of the
10361 -- current compilation, and is not attached to the tree, but its
10362 -- parent must be set for analysis.
10364 if Nkind (Parent (Inst_Node)) = N_Compilation_Unit then
10366 -- Replace instance node with body of instance, and create new
10367 -- node for corresponding instance declaration.
10369 Build_Instance_Compilation_Unit_Nodes
10370 (Inst_Node, Act_Body, Act_Decl);
10371 Analyze (Inst_Node);
10373 if Parent (Inst_Node) = Cunit (Main_Unit) then
10375 -- If the instance is a child unit itself, then set the scope
10376 -- of the expanded body to be the parent of the instantiation
10377 -- (ensuring that the fully qualified name will be generated
10378 -- for the elaboration subprogram).
10380 if Nkind (Defining_Unit_Name (Act_Spec)) =
10381 N_Defining_Program_Unit_Name
10384 (Defining_Entity (Inst_Node), Scope (Act_Decl_Id));
10388 -- Case where instantiation is not a library unit
10391 -- If this is an early instantiation, i.e. appears textually
10392 -- before the corresponding body and must be elaborated first,
10393 -- indicate that the body instance is to be delayed.
10395 Install_Body (Act_Body, Inst_Node, Gen_Body, Gen_Decl);
10397 -- Now analyze the body. We turn off all checks if this is an
10398 -- internal unit, since there is no reason to have checks on for
10399 -- any predefined run-time library code. All such code is designed
10400 -- to be compiled with checks off.
10402 -- Note that we do NOT apply this criterion to children of GNAT
10403 -- (or on VMS, children of DEC). The latter units must suppress
10404 -- checks explicitly if this is needed.
10406 if Is_Predefined_File_Name
10407 (Unit_File_Name (Get_Source_Unit (Gen_Decl)))
10409 Analyze (Act_Body, Suppress => All_Checks);
10411 Analyze (Act_Body);
10415 Inherit_Context (Gen_Body, Inst_Node);
10417 -- Remove the parent instances if they have been placed on the scope
10418 -- stack to compile the body.
10420 if Parent_Installed then
10421 Remove_Parent (In_Body => True);
10423 -- Restore the previous visibility of the parent
10425 Set_Is_Immediately_Visible (Par_Ent, Par_Vis);
10428 Restore_Hidden_Primitives (Vis_Prims_List);
10429 Restore_Private_Views (Act_Decl_Id);
10431 -- Remove the current unit from visibility if this is an instance
10432 -- that is not elaborated on the fly for inlining purposes.
10434 if not Inlined_Body then
10435 Set_Is_Immediately_Visible (Act_Decl_Id, False);
10439 Style_Check := Save_Style_Check;
10441 -- If we have no body, and the unit requires a body, then complain. This
10442 -- complaint is suppressed if we have detected other errors (since a
10443 -- common reason for missing the body is that it had errors).
10444 -- In CodePeer mode, a warning has been emitted already, no need for
10445 -- further messages.
10447 elsif Unit_Requires_Body (Gen_Unit)
10448 and then not Body_Optional
10450 if CodePeer_Mode then
10453 elsif Serious_Errors_Detected = 0 then
10455 ("cannot find body of generic package &", Inst_Node, Gen_Unit);
10457 -- Don't attempt to perform any cleanup actions if some other error
10458 -- was already detected, since this can cause blowups.
10464 -- Case of package that does not need a body
10467 -- If the instantiation of the declaration is a library unit, rewrite
10468 -- the original package instantiation as a package declaration in the
10469 -- compilation unit node.
10471 if Nkind (Parent (Inst_Node)) = N_Compilation_Unit then
10472 Set_Parent_Spec (Act_Decl, Parent_Spec (Inst_Node));
10473 Rewrite (Inst_Node, Act_Decl);
10475 -- Generate elaboration entity, in case spec has elaboration code.
10476 -- This cannot be done when the instance is analyzed, because it
10477 -- is not known yet whether the body exists.
10479 Set_Elaboration_Entity_Required (Act_Decl_Id, False);
10480 Build_Elaboration_Entity (Parent (Inst_Node), Act_Decl_Id);
10482 -- If the instantiation is not a library unit, then append the
10483 -- declaration to the list of implicitly generated entities, unless
10484 -- it is already a list member which means that it was already
10487 elsif not Is_List_Member (Act_Decl) then
10488 Mark_Rewrite_Insertion (Act_Decl);
10489 Insert_Before (Inst_Node, Act_Decl);
10493 Expander_Mode_Restore;
10494 end Instantiate_Package_Body;
10496 ---------------------------------
10497 -- Instantiate_Subprogram_Body --
10498 ---------------------------------
10500 procedure Instantiate_Subprogram_Body
10501 (Body_Info : Pending_Body_Info;
10502 Body_Optional : Boolean := False)
10504 Act_Decl : constant Node_Id := Body_Info.Act_Decl;
10505 Inst_Node : constant Node_Id := Body_Info.Inst_Node;
10506 Loc : constant Source_Ptr := Sloc (Inst_Node);
10507 Gen_Id : constant Node_Id := Name (Inst_Node);
10508 Gen_Unit : constant Entity_Id := Get_Generic_Entity (Inst_Node);
10509 Gen_Decl : constant Node_Id := Unit_Declaration_Node (Gen_Unit);
10510 Anon_Id : constant Entity_Id :=
10511 Defining_Unit_Name (Specification (Act_Decl));
10512 Pack_Id : constant Entity_Id :=
10513 Defining_Unit_Name (Parent (Act_Decl));
10515 Gen_Body : Node_Id;
10516 Gen_Body_Id : Node_Id;
10517 Act_Body : Node_Id;
10518 Pack_Body : Node_Id;
10519 Prev_Formal : Entity_Id;
10520 Ret_Expr : Node_Id;
10521 Unit_Renaming : Node_Id;
10523 Parent_Installed : Boolean := False;
10525 Saved_Style_Check : constant Boolean := Style_Check;
10526 Saved_Warnings : constant Warning_Record := Save_Warnings;
10528 Par_Ent : Entity_Id := Empty;
10529 Par_Vis : Boolean := False;
10532 Gen_Body_Id := Corresponding_Body (Gen_Decl);
10534 -- Subprogram body may have been created already because of an inline
10535 -- pragma, or because of multiple elaborations of the enclosing package
10536 -- when several instances of the subprogram appear in the main unit.
10538 if Present (Corresponding_Body (Act_Decl)) then
10542 Expander_Mode_Save_And_Set (Body_Info.Expander_Status);
10544 -- Re-establish the state of information on which checks are suppressed.
10545 -- This information was set in Body_Info at the point of instantiation,
10546 -- and now we restore it so that the instance is compiled using the
10547 -- check status at the instantiation (RM 11.5 (7.2/2), AI95-00224-01).
10549 Local_Suppress_Stack_Top := Body_Info.Local_Suppress_Stack_Top;
10550 Scope_Suppress := Body_Info.Scope_Suppress;
10551 Opt.Ada_Version := Body_Info.Version;
10552 Opt.Ada_Version_Pragma := Body_Info.Version_Pragma;
10553 Restore_Warnings (Body_Info.Warnings);
10554 Opt.SPARK_Mode := Body_Info.SPARK_Mode;
10555 Opt.SPARK_Mode_Pragma := Body_Info.SPARK_Mode_Pragma;
10557 if No (Gen_Body_Id) then
10559 -- For imported generic subprogram, no body to compile, complete
10560 -- the spec entity appropriately.
10562 if Is_Imported (Gen_Unit) then
10563 Set_Is_Imported (Anon_Id);
10564 Set_First_Rep_Item (Anon_Id, First_Rep_Item (Gen_Unit));
10565 Set_Interface_Name (Anon_Id, Interface_Name (Gen_Unit));
10566 Set_Convention (Anon_Id, Convention (Gen_Unit));
10567 Set_Has_Completion (Anon_Id);
10570 -- For other cases, compile the body
10573 Load_Parent_Of_Generic
10574 (Inst_Node, Specification (Gen_Decl), Body_Optional);
10575 Gen_Body_Id := Corresponding_Body (Gen_Decl);
10579 Instantiation_Node := Inst_Node;
10581 if Present (Gen_Body_Id) then
10582 Gen_Body := Unit_Declaration_Node (Gen_Body_Id);
10584 if Nkind (Gen_Body) = N_Subprogram_Body_Stub then
10586 -- Either body is not present, or context is non-expanding, as
10587 -- when compiling a subunit. Mark the instance as completed, and
10588 -- diagnose a missing body when needed.
10591 and then Operating_Mode = Generate_Code
10594 ("missing proper body for instantiation", Gen_Body);
10597 Set_Has_Completion (Anon_Id);
10601 Save_Env (Gen_Unit, Anon_Id);
10602 Style_Check := False;
10603 Current_Sem_Unit := Body_Info.Current_Sem_Unit;
10604 Create_Instantiation_Source
10612 (Original_Node (Gen_Body), Empty, Instantiating => True);
10614 -- Create proper defining name for the body, to correspond to
10615 -- the one in the spec.
10617 Set_Defining_Unit_Name (Specification (Act_Body),
10618 Make_Defining_Identifier
10619 (Sloc (Defining_Entity (Inst_Node)), Chars (Anon_Id)));
10620 Set_Corresponding_Spec (Act_Body, Anon_Id);
10621 Set_Has_Completion (Anon_Id);
10622 Check_Generic_Actuals (Pack_Id, False);
10624 -- Generate a reference to link the visible subprogram instance to
10625 -- the generic body, which for navigation purposes is the only
10626 -- available source for the instance.
10629 (Related_Instance (Pack_Id),
10630 Gen_Body_Id, 'b', Set_Ref => False, Force => True);
10632 -- If it is a child unit, make the parent instance (which is an
10633 -- instance of the parent of the generic) visible. The parent
10634 -- instance is the prefix of the name of the generic unit.
10636 if Ekind (Scope (Gen_Unit)) = E_Generic_Package
10637 and then Nkind (Gen_Id) = N_Expanded_Name
10639 Par_Ent := Entity (Prefix (Gen_Id));
10640 Par_Vis := Is_Immediately_Visible (Par_Ent);
10641 Install_Parent (Par_Ent, In_Body => True);
10642 Parent_Installed := True;
10644 elsif Is_Child_Unit (Gen_Unit) then
10645 Par_Ent := Scope (Gen_Unit);
10646 Par_Vis := Is_Immediately_Visible (Par_Ent);
10647 Install_Parent (Par_Ent, In_Body => True);
10648 Parent_Installed := True;
10651 -- Inside its body, a reference to the generic unit is a reference
10652 -- to the instance. The corresponding renaming is the first
10653 -- declaration in the body.
10656 Make_Subprogram_Renaming_Declaration (Loc,
10658 Copy_Generic_Node (
10659 Specification (Original_Node (Gen_Body)),
10661 Instantiating => True),
10662 Name => New_Occurrence_Of (Anon_Id, Loc));
10664 -- If there is a formal subprogram with the same name as the unit
10665 -- itself, do not add this renaming declaration. This is a temporary
10666 -- fix for one ACVC test. ???
10668 Prev_Formal := First_Entity (Pack_Id);
10669 while Present (Prev_Formal) loop
10670 if Chars (Prev_Formal) = Chars (Gen_Unit)
10671 and then Is_Overloadable (Prev_Formal)
10676 Next_Entity (Prev_Formal);
10679 if Present (Prev_Formal) then
10680 Decls := New_List (Act_Body);
10682 Decls := New_List (Unit_Renaming, Act_Body);
10685 -- The subprogram body is placed in the body of a dummy package body,
10686 -- whose spec contains the subprogram declaration as well as the
10687 -- renaming declarations for the generic parameters.
10689 Pack_Body := Make_Package_Body (Loc,
10690 Defining_Unit_Name => New_Copy (Pack_Id),
10691 Declarations => Decls);
10693 Set_Corresponding_Spec (Pack_Body, Pack_Id);
10695 -- If the instantiation is a library unit, then build resulting
10696 -- compilation unit nodes for the instance. The declaration of
10697 -- the enclosing package is the grandparent of the subprogram
10698 -- declaration. First replace the instantiation node as the unit
10699 -- of the corresponding compilation.
10701 if Nkind (Parent (Inst_Node)) = N_Compilation_Unit then
10702 if Parent (Inst_Node) = Cunit (Main_Unit) then
10703 Set_Unit (Parent (Inst_Node), Inst_Node);
10704 Build_Instance_Compilation_Unit_Nodes
10705 (Inst_Node, Pack_Body, Parent (Parent (Act_Decl)));
10706 Analyze (Inst_Node);
10708 Set_Parent (Pack_Body, Parent (Inst_Node));
10709 Analyze (Pack_Body);
10713 Insert_Before (Inst_Node, Pack_Body);
10714 Mark_Rewrite_Insertion (Pack_Body);
10715 Analyze (Pack_Body);
10717 if Expander_Active then
10718 Freeze_Subprogram_Body (Inst_Node, Gen_Body, Pack_Id);
10722 Inherit_Context (Gen_Body, Inst_Node);
10724 Restore_Private_Views (Pack_Id, False);
10726 if Parent_Installed then
10727 Remove_Parent (In_Body => True);
10729 -- Restore the previous visibility of the parent
10731 Set_Is_Immediately_Visible (Par_Ent, Par_Vis);
10735 Style_Check := Saved_Style_Check;
10736 Restore_Warnings (Saved_Warnings);
10738 -- Body not found. Error was emitted already. If there were no previous
10739 -- errors, this may be an instance whose scope is a premature instance.
10740 -- In that case we must insure that the (legal) program does raise
10741 -- program error if executed. We generate a subprogram body for this
10742 -- purpose. See DEC ac30vso.
10744 -- Should not reference proprietary DEC tests in comments ???
10746 elsif Serious_Errors_Detected = 0
10747 and then Nkind (Parent (Inst_Node)) /= N_Compilation_Unit
10749 if Body_Optional then
10752 elsif Ekind (Anon_Id) = E_Procedure then
10754 Make_Subprogram_Body (Loc,
10756 Make_Procedure_Specification (Loc,
10757 Defining_Unit_Name =>
10758 Make_Defining_Identifier (Loc, Chars (Anon_Id)),
10759 Parameter_Specifications =>
10761 (Parameter_Specifications (Parent (Anon_Id)))),
10763 Declarations => Empty_List,
10764 Handled_Statement_Sequence =>
10765 Make_Handled_Sequence_Of_Statements (Loc,
10768 Make_Raise_Program_Error (Loc,
10770 PE_Access_Before_Elaboration))));
10774 Make_Raise_Program_Error (Loc,
10775 Reason => PE_Access_Before_Elaboration);
10777 Set_Etype (Ret_Expr, (Etype (Anon_Id)));
10778 Set_Analyzed (Ret_Expr);
10781 Make_Subprogram_Body (Loc,
10783 Make_Function_Specification (Loc,
10784 Defining_Unit_Name =>
10785 Make_Defining_Identifier (Loc, Chars (Anon_Id)),
10786 Parameter_Specifications =>
10788 (Parameter_Specifications (Parent (Anon_Id))),
10789 Result_Definition =>
10790 New_Occurrence_Of (Etype (Anon_Id), Loc)),
10792 Declarations => Empty_List,
10793 Handled_Statement_Sequence =>
10794 Make_Handled_Sequence_Of_Statements (Loc,
10797 (Make_Simple_Return_Statement (Loc, Ret_Expr))));
10800 Pack_Body := Make_Package_Body (Loc,
10801 Defining_Unit_Name => New_Copy (Pack_Id),
10802 Declarations => New_List (Act_Body));
10804 Insert_After (Inst_Node, Pack_Body);
10805 Set_Corresponding_Spec (Pack_Body, Pack_Id);
10806 Analyze (Pack_Body);
10809 Expander_Mode_Restore;
10810 end Instantiate_Subprogram_Body;
10812 ----------------------
10813 -- Instantiate_Type --
10814 ----------------------
10816 function Instantiate_Type
10819 Analyzed_Formal : Node_Id;
10820 Actual_Decls : List_Id) return List_Id
10822 Gen_T : constant Entity_Id := Defining_Identifier (Formal);
10823 A_Gen_T : constant Entity_Id :=
10824 Defining_Identifier (Analyzed_Formal);
10825 Ancestor : Entity_Id := Empty;
10826 Def : constant Node_Id := Formal_Type_Definition (Formal);
10828 Decl_Node : Node_Id;
10829 Decl_Nodes : List_Id;
10833 procedure Diagnose_Predicated_Actual;
10834 -- There are a number of constructs in which a discrete type with
10835 -- predicates is illegal, e.g. as an index in an array type declaration.
10836 -- If a generic type is used is such a construct in a generic package
10837 -- declaration, it carries the flag No_Predicate_On_Actual. it is part
10838 -- of the generic contract that the actual cannot have predicates.
10840 procedure Validate_Array_Type_Instance;
10841 procedure Validate_Access_Subprogram_Instance;
10842 procedure Validate_Access_Type_Instance;
10843 procedure Validate_Derived_Type_Instance;
10844 procedure Validate_Derived_Interface_Type_Instance;
10845 procedure Validate_Discriminated_Formal_Type;
10846 procedure Validate_Interface_Type_Instance;
10847 procedure Validate_Private_Type_Instance;
10848 procedure Validate_Incomplete_Type_Instance;
10849 -- These procedures perform validation tests for the named case.
10850 -- Validate_Discriminated_Formal_Type is shared by formal private
10851 -- types and Ada 2012 formal incomplete types.
10853 function Subtypes_Match (Gen_T, Act_T : Entity_Id) return Boolean;
10854 -- Check that base types are the same and that the subtypes match
10855 -- statically. Used in several of the above.
10857 ---------------------------------
10858 -- Diagnose_Predicated_Actual --
10859 ---------------------------------
10861 procedure Diagnose_Predicated_Actual is
10863 if No_Predicate_On_Actual (A_Gen_T)
10864 and then Has_Predicates (Act_T)
10867 ("actual for& cannot be a type with predicate",
10868 Instantiation_Node, A_Gen_T);
10870 elsif No_Dynamic_Predicate_On_Actual (A_Gen_T)
10871 and then Has_Predicates (Act_T)
10872 and then not Has_Static_Predicate_Aspect (Act_T)
10875 ("actual for& cannot be a type with a dynamic predicate",
10876 Instantiation_Node, A_Gen_T);
10878 end Diagnose_Predicated_Actual;
10880 --------------------
10881 -- Subtypes_Match --
10882 --------------------
10884 function Subtypes_Match (Gen_T, Act_T : Entity_Id) return Boolean is
10885 T : constant Entity_Id := Get_Instance_Of (Gen_T);
10888 -- Some detailed comments would be useful here ???
10890 return ((Base_Type (T) = Act_T
10891 or else Base_Type (T) = Base_Type (Act_T))
10892 and then Subtypes_Statically_Match (T, Act_T))
10894 or else (Is_Class_Wide_Type (Gen_T)
10895 and then Is_Class_Wide_Type (Act_T)
10896 and then Subtypes_Match
10897 (Get_Instance_Of (Root_Type (Gen_T)),
10898 Root_Type (Act_T)))
10901 (Ekind_In (Gen_T, E_Anonymous_Access_Subprogram_Type,
10902 E_Anonymous_Access_Type)
10903 and then Ekind (Act_T) = Ekind (Gen_T)
10904 and then Subtypes_Statically_Match
10905 (Designated_Type (Gen_T), Designated_Type (Act_T)));
10906 end Subtypes_Match;
10908 -----------------------------------------
10909 -- Validate_Access_Subprogram_Instance --
10910 -----------------------------------------
10912 procedure Validate_Access_Subprogram_Instance is
10914 if not Is_Access_Type (Act_T)
10915 or else Ekind (Designated_Type (Act_T)) /= E_Subprogram_Type
10918 ("expect access type in instantiation of &", Actual, Gen_T);
10919 Abandon_Instantiation (Actual);
10922 -- According to AI05-288, actuals for access_to_subprograms must be
10923 -- subtype conformant with the generic formal. Previous to AI05-288
10924 -- only mode conformance was required.
10926 -- This is a binding interpretation that applies to previous versions
10927 -- of the language, no need to maintain previous weaker checks.
10929 Check_Subtype_Conformant
10930 (Designated_Type (Act_T),
10931 Designated_Type (A_Gen_T),
10935 if Ekind (Base_Type (Act_T)) = E_Access_Protected_Subprogram_Type then
10936 if Ekind (A_Gen_T) = E_Access_Subprogram_Type then
10938 ("protected access type not allowed for formal &",
10942 elsif Ekind (A_Gen_T) = E_Access_Protected_Subprogram_Type then
10944 ("expect protected access type for formal &",
10947 end Validate_Access_Subprogram_Instance;
10949 -----------------------------------
10950 -- Validate_Access_Type_Instance --
10951 -----------------------------------
10953 procedure Validate_Access_Type_Instance is
10954 Desig_Type : constant Entity_Id :=
10955 Find_Actual_Type (Designated_Type (A_Gen_T), A_Gen_T);
10956 Desig_Act : Entity_Id;
10959 if not Is_Access_Type (Act_T) then
10961 ("expect access type in instantiation of &", Actual, Gen_T);
10962 Abandon_Instantiation (Actual);
10965 if Is_Access_Constant (A_Gen_T) then
10966 if not Is_Access_Constant (Act_T) then
10968 ("actual type must be access-to-constant type", Actual);
10969 Abandon_Instantiation (Actual);
10972 if Is_Access_Constant (Act_T) then
10974 ("actual type must be access-to-variable type", Actual);
10975 Abandon_Instantiation (Actual);
10977 elsif Ekind (A_Gen_T) = E_General_Access_Type
10978 and then Ekind (Base_Type (Act_T)) /= E_General_Access_Type
10980 Error_Msg_N -- CODEFIX
10981 ("actual must be general access type!", Actual);
10982 Error_Msg_NE -- CODEFIX
10983 ("add ALL to }!", Actual, Act_T);
10984 Abandon_Instantiation (Actual);
10988 -- The designated subtypes, that is to say the subtypes introduced
10989 -- by an access type declaration (and not by a subtype declaration)
10992 Desig_Act := Designated_Type (Base_Type (Act_T));
10994 -- The designated type may have been introduced through a limited_
10995 -- with clause, in which case retrieve the non-limited view. This
10996 -- applies to incomplete types as well as to class-wide types.
10998 if From_Limited_With (Desig_Act) then
10999 Desig_Act := Available_View (Desig_Act);
11002 if not Subtypes_Match (Desig_Type, Desig_Act) then
11004 ("designated type of actual does not match that of formal &",
11007 if not Predicates_Match (Desig_Type, Desig_Act) then
11008 Error_Msg_N ("\predicates do not match", Actual);
11011 Abandon_Instantiation (Actual);
11013 elsif Is_Access_Type (Designated_Type (Act_T))
11014 and then Is_Constrained (Designated_Type (Designated_Type (Act_T)))
11016 Is_Constrained (Designated_Type (Desig_Type))
11019 ("designated type of actual does not match that of formal &",
11022 if not Predicates_Match (Desig_Type, Desig_Act) then
11023 Error_Msg_N ("\predicates do not match", Actual);
11026 Abandon_Instantiation (Actual);
11029 -- Ada 2005: null-exclusion indicators of the two types must agree
11031 if Can_Never_Be_Null (A_Gen_T) /= Can_Never_Be_Null (Act_T) then
11033 ("non null exclusion of actual and formal & do not match",
11036 end Validate_Access_Type_Instance;
11038 ----------------------------------
11039 -- Validate_Array_Type_Instance --
11040 ----------------------------------
11042 procedure Validate_Array_Type_Instance is
11047 function Formal_Dimensions return Int;
11048 -- Count number of dimensions in array type formal
11050 -----------------------
11051 -- Formal_Dimensions --
11052 -----------------------
11054 function Formal_Dimensions return Int is
11059 if Nkind (Def) = N_Constrained_Array_Definition then
11060 Index := First (Discrete_Subtype_Definitions (Def));
11062 Index := First (Subtype_Marks (Def));
11065 while Present (Index) loop
11067 Next_Index (Index);
11071 end Formal_Dimensions;
11073 -- Start of processing for Validate_Array_Type_Instance
11076 if not Is_Array_Type (Act_T) then
11078 ("expect array type in instantiation of &", Actual, Gen_T);
11079 Abandon_Instantiation (Actual);
11081 elsif Nkind (Def) = N_Constrained_Array_Definition then
11082 if not (Is_Constrained (Act_T)) then
11084 ("expect constrained array in instantiation of &",
11086 Abandon_Instantiation (Actual);
11090 if Is_Constrained (Act_T) then
11092 ("expect unconstrained array in instantiation of &",
11094 Abandon_Instantiation (Actual);
11098 if Formal_Dimensions /= Number_Dimensions (Act_T) then
11100 ("dimensions of actual do not match formal &", Actual, Gen_T);
11101 Abandon_Instantiation (Actual);
11104 I1 := First_Index (A_Gen_T);
11105 I2 := First_Index (Act_T);
11106 for J in 1 .. Formal_Dimensions loop
11108 -- If the indexes of the actual were given by a subtype_mark,
11109 -- the index was transformed into a range attribute. Retrieve
11110 -- the original type mark for checking.
11112 if Is_Entity_Name (Original_Node (I2)) then
11113 T2 := Entity (Original_Node (I2));
11118 if not Subtypes_Match
11119 (Find_Actual_Type (Etype (I1), A_Gen_T), T2)
11122 ("index types of actual do not match those of formal &",
11124 Abandon_Instantiation (Actual);
11131 -- Check matching subtypes. Note that there are complex visibility
11132 -- issues when the generic is a child unit and some aspect of the
11133 -- generic type is declared in a parent unit of the generic. We do
11134 -- the test to handle this special case only after a direct check
11135 -- for static matching has failed. The case where both the component
11136 -- type and the array type are separate formals, and the component
11137 -- type is a private view may also require special checking in
11141 (Component_Type (A_Gen_T), Component_Type (Act_T))
11142 or else Subtypes_Match
11143 (Find_Actual_Type (Component_Type (A_Gen_T), A_Gen_T),
11144 Component_Type (Act_T))
11149 ("component subtype of actual does not match that of formal &",
11151 Abandon_Instantiation (Actual);
11154 if Has_Aliased_Components (A_Gen_T)
11155 and then not Has_Aliased_Components (Act_T)
11158 ("actual must have aliased components to match formal type &",
11161 end Validate_Array_Type_Instance;
11163 -----------------------------------------------
11164 -- Validate_Derived_Interface_Type_Instance --
11165 -----------------------------------------------
11167 procedure Validate_Derived_Interface_Type_Instance is
11168 Par : constant Entity_Id := Entity (Subtype_Indication (Def));
11172 -- First apply interface instance checks
11174 Validate_Interface_Type_Instance;
11176 -- Verify that immediate parent interface is an ancestor of
11180 and then not Interface_Present_In_Ancestor (Act_T, Par)
11183 ("interface actual must include progenitor&", Actual, Par);
11186 -- Now verify that the actual includes all other ancestors of
11189 Elmt := First_Elmt (Interfaces (A_Gen_T));
11190 while Present (Elmt) loop
11191 if not Interface_Present_In_Ancestor
11192 (Act_T, Get_Instance_Of (Node (Elmt)))
11195 ("interface actual must include progenitor&",
11196 Actual, Node (Elmt));
11201 end Validate_Derived_Interface_Type_Instance;
11203 ------------------------------------
11204 -- Validate_Derived_Type_Instance --
11205 ------------------------------------
11207 procedure Validate_Derived_Type_Instance is
11208 Actual_Discr : Entity_Id;
11209 Ancestor_Discr : Entity_Id;
11212 -- If the parent type in the generic declaration is itself a previous
11213 -- formal type, then it is local to the generic and absent from the
11214 -- analyzed generic definition. In that case the ancestor is the
11215 -- instance of the formal (which must have been instantiated
11216 -- previously), unless the ancestor is itself a formal derived type.
11217 -- In this latter case (which is the subject of Corrigendum 8652/0038
11218 -- (AI-202) the ancestor of the formals is the ancestor of its
11219 -- parent. Otherwise, the analyzed generic carries the parent type.
11220 -- If the parent type is defined in a previous formal package, then
11221 -- the scope of that formal package is that of the generic type
11222 -- itself, and it has already been mapped into the corresponding type
11223 -- in the actual package.
11225 -- Common case: parent type defined outside of the generic
11227 if Is_Entity_Name (Subtype_Mark (Def))
11228 and then Present (Entity (Subtype_Mark (Def)))
11230 Ancestor := Get_Instance_Of (Entity (Subtype_Mark (Def)));
11232 -- Check whether parent is defined in a previous formal package
11235 Scope (Scope (Base_Type (Etype (A_Gen_T)))) = Scope (A_Gen_T)
11238 Get_Instance_Of (Base_Type (Etype (A_Gen_T)));
11240 -- The type may be a local derivation, or a type extension of a
11241 -- previous formal, or of a formal of a parent package.
11243 elsif Is_Derived_Type (Get_Instance_Of (A_Gen_T))
11245 Ekind (Get_Instance_Of (A_Gen_T)) = E_Record_Type_With_Private
11247 -- Check whether the parent is another derived formal type in the
11248 -- same generic unit.
11250 if Etype (A_Gen_T) /= A_Gen_T
11251 and then Is_Generic_Type (Etype (A_Gen_T))
11252 and then Scope (Etype (A_Gen_T)) = Scope (A_Gen_T)
11253 and then Etype (Etype (A_Gen_T)) /= Etype (A_Gen_T)
11255 -- Locate ancestor of parent from the subtype declaration
11256 -- created for the actual.
11262 Decl := First (Actual_Decls);
11263 while Present (Decl) loop
11264 if Nkind (Decl) = N_Subtype_Declaration
11265 and then Chars (Defining_Identifier (Decl)) =
11266 Chars (Etype (A_Gen_T))
11268 Ancestor := Generic_Parent_Type (Decl);
11276 pragma Assert (Present (Ancestor));
11278 -- The ancestor itself may be a previous formal that has been
11281 Ancestor := Get_Instance_Of (Ancestor);
11285 Get_Instance_Of (Base_Type (Get_Instance_Of (A_Gen_T)));
11288 -- An unusual case: the actual is a type declared in a parent unit,
11289 -- but is not a formal type so there is no instance_of for it.
11290 -- Retrieve it by analyzing the record extension.
11292 elsif Is_Child_Unit (Scope (A_Gen_T))
11293 and then In_Open_Scopes (Scope (Act_T))
11294 and then Is_Generic_Instance (Scope (Act_T))
11296 Analyze (Subtype_Mark (Def));
11297 Ancestor := Entity (Subtype_Mark (Def));
11300 Ancestor := Get_Instance_Of (Etype (Base_Type (A_Gen_T)));
11303 -- If the formal derived type has pragma Preelaborable_Initialization
11304 -- then the actual type must have preelaborable initialization.
11306 if Known_To_Have_Preelab_Init (A_Gen_T)
11307 and then not Has_Preelaborable_Initialization (Act_T)
11310 ("actual for & must have preelaborable initialization",
11314 -- Ada 2005 (AI-251)
11316 if Ada_Version >= Ada_2005 and then Is_Interface (Ancestor) then
11317 if not Interface_Present_In_Ancestor (Act_T, Ancestor) then
11319 ("(Ada 2005) expected type implementing & in instantiation",
11323 elsif not Is_Ancestor (Base_Type (Ancestor), Act_T) then
11325 ("expect type derived from & in instantiation",
11326 Actual, First_Subtype (Ancestor));
11327 Abandon_Instantiation (Actual);
11330 -- Ada 2005 (AI-443): Synchronized formal derived type checks. Note
11331 -- that the formal type declaration has been rewritten as a private
11334 if Ada_Version >= Ada_2005
11335 and then Nkind (Parent (A_Gen_T)) = N_Private_Extension_Declaration
11336 and then Synchronized_Present (Parent (A_Gen_T))
11338 -- The actual must be a synchronized tagged type
11340 if not Is_Tagged_Type (Act_T) then
11342 ("actual of synchronized type must be tagged", Actual);
11343 Abandon_Instantiation (Actual);
11345 elsif Nkind (Parent (Act_T)) = N_Full_Type_Declaration
11346 and then Nkind (Type_Definition (Parent (Act_T))) =
11347 N_Derived_Type_Definition
11348 and then not Synchronized_Present (Type_Definition
11352 ("actual of synchronized type must be synchronized", Actual);
11353 Abandon_Instantiation (Actual);
11357 -- Perform atomic/volatile checks (RM C.6(12)). Note that AI05-0218-1
11358 -- removes the second instance of the phrase "or allow pass by copy".
11360 if Is_Atomic (Act_T) and then not Is_Atomic (Ancestor) then
11362 ("cannot have atomic actual type for non-atomic formal type",
11365 elsif Is_Volatile (Act_T) and then not Is_Volatile (Ancestor) then
11367 ("cannot have volatile actual type for non-volatile formal type",
11371 -- It should not be necessary to check for unknown discriminants on
11372 -- Formal, but for some reason Has_Unknown_Discriminants is false for
11373 -- A_Gen_T, so Is_Indefinite_Subtype incorrectly returns False. This
11374 -- needs fixing. ???
11376 if not Is_Indefinite_Subtype (A_Gen_T)
11377 and then not Unknown_Discriminants_Present (Formal)
11378 and then Is_Indefinite_Subtype (Act_T)
11381 ("actual subtype must be constrained", Actual);
11382 Abandon_Instantiation (Actual);
11385 if not Unknown_Discriminants_Present (Formal) then
11386 if Is_Constrained (Ancestor) then
11387 if not Is_Constrained (Act_T) then
11389 ("actual subtype must be constrained", Actual);
11390 Abandon_Instantiation (Actual);
11393 -- Ancestor is unconstrained, Check if generic formal and actual
11394 -- agree on constrainedness. The check only applies to array types
11395 -- and discriminated types.
11397 elsif Is_Constrained (Act_T) then
11398 if Ekind (Ancestor) = E_Access_Type
11400 (not Is_Constrained (A_Gen_T)
11401 and then Is_Composite_Type (A_Gen_T))
11404 ("actual subtype must be unconstrained", Actual);
11405 Abandon_Instantiation (Actual);
11408 -- A class-wide type is only allowed if the formal has unknown
11411 elsif Is_Class_Wide_Type (Act_T)
11412 and then not Has_Unknown_Discriminants (Ancestor)
11415 ("actual for & cannot be a class-wide type", Actual, Gen_T);
11416 Abandon_Instantiation (Actual);
11418 -- Otherwise, the formal and actual shall have the same number
11419 -- of discriminants and each discriminant of the actual must
11420 -- correspond to a discriminant of the formal.
11422 elsif Has_Discriminants (Act_T)
11423 and then not Has_Unknown_Discriminants (Act_T)
11424 and then Has_Discriminants (Ancestor)
11426 Actual_Discr := First_Discriminant (Act_T);
11427 Ancestor_Discr := First_Discriminant (Ancestor);
11428 while Present (Actual_Discr)
11429 and then Present (Ancestor_Discr)
11431 if Base_Type (Act_T) /= Base_Type (Ancestor) and then
11432 No (Corresponding_Discriminant (Actual_Discr))
11435 ("discriminant & does not correspond " &
11436 "to ancestor discriminant", Actual, Actual_Discr);
11437 Abandon_Instantiation (Actual);
11440 Next_Discriminant (Actual_Discr);
11441 Next_Discriminant (Ancestor_Discr);
11444 if Present (Actual_Discr) or else Present (Ancestor_Discr) then
11446 ("actual for & must have same number of discriminants",
11448 Abandon_Instantiation (Actual);
11451 -- This case should be caught by the earlier check for
11452 -- constrainedness, but the check here is added for completeness.
11454 elsif Has_Discriminants (Act_T)
11455 and then not Has_Unknown_Discriminants (Act_T)
11458 ("actual for & must not have discriminants", Actual, Gen_T);
11459 Abandon_Instantiation (Actual);
11461 elsif Has_Discriminants (Ancestor) then
11463 ("actual for & must have known discriminants", Actual, Gen_T);
11464 Abandon_Instantiation (Actual);
11467 if not Subtypes_Statically_Compatible
11468 (Act_T, Ancestor, Formal_Derived_Matching => True)
11471 ("constraint on actual is incompatible with formal", Actual);
11472 Abandon_Instantiation (Actual);
11476 -- If the formal and actual types are abstract, check that there
11477 -- are no abstract primitives of the actual type that correspond to
11478 -- nonabstract primitives of the formal type (second sentence of
11481 if Is_Abstract_Type (A_Gen_T) and then Is_Abstract_Type (Act_T) then
11482 Check_Abstract_Primitives : declare
11483 Gen_Prims : constant Elist_Id :=
11484 Primitive_Operations (A_Gen_T);
11485 Gen_Elmt : Elmt_Id;
11486 Gen_Subp : Entity_Id;
11487 Anc_Subp : Entity_Id;
11488 Anc_Formal : Entity_Id;
11489 Anc_F_Type : Entity_Id;
11491 Act_Prims : constant Elist_Id := Primitive_Operations (Act_T);
11492 Act_Elmt : Elmt_Id;
11493 Act_Subp : Entity_Id;
11494 Act_Formal : Entity_Id;
11495 Act_F_Type : Entity_Id;
11497 Subprograms_Correspond : Boolean;
11499 function Is_Tagged_Ancestor (T1, T2 : Entity_Id) return Boolean;
11500 -- Returns true if T2 is derived directly or indirectly from
11501 -- T1, including derivations from interfaces. T1 and T2 are
11502 -- required to be specific tagged base types.
11504 ------------------------
11505 -- Is_Tagged_Ancestor --
11506 ------------------------
11508 function Is_Tagged_Ancestor (T1, T2 : Entity_Id) return Boolean
11510 Intfc_Elmt : Elmt_Id;
11513 -- The predicate is satisfied if the types are the same
11518 -- If we've reached the top of the derivation chain then
11519 -- we know that T1 is not an ancestor of T2.
11521 elsif Etype (T2) = T2 then
11524 -- Proceed to check T2's immediate parent
11526 elsif Is_Ancestor (T1, Base_Type (Etype (T2))) then
11529 -- Finally, check to see if T1 is an ancestor of any of T2's
11533 Intfc_Elmt := First_Elmt (Interfaces (T2));
11534 while Present (Intfc_Elmt) loop
11535 if Is_Ancestor (T1, Node (Intfc_Elmt)) then
11539 Next_Elmt (Intfc_Elmt);
11544 end Is_Tagged_Ancestor;
11546 -- Start of processing for Check_Abstract_Primitives
11549 -- Loop over all of the formal derived type's primitives
11551 Gen_Elmt := First_Elmt (Gen_Prims);
11552 while Present (Gen_Elmt) loop
11553 Gen_Subp := Node (Gen_Elmt);
11555 -- If the primitive of the formal is not abstract, then
11556 -- determine whether there is a corresponding primitive of
11557 -- the actual type that's abstract.
11559 if not Is_Abstract_Subprogram (Gen_Subp) then
11560 Act_Elmt := First_Elmt (Act_Prims);
11561 while Present (Act_Elmt) loop
11562 Act_Subp := Node (Act_Elmt);
11564 -- If we find an abstract primitive of the actual,
11565 -- then we need to test whether it corresponds to the
11566 -- subprogram from which the generic formal primitive
11569 if Is_Abstract_Subprogram (Act_Subp) then
11570 Anc_Subp := Alias (Gen_Subp);
11572 -- Test whether we have a corresponding primitive
11573 -- by comparing names, kinds, formal types, and
11576 if Chars (Anc_Subp) = Chars (Act_Subp)
11577 and then Ekind (Anc_Subp) = Ekind (Act_Subp)
11579 Anc_Formal := First_Formal (Anc_Subp);
11580 Act_Formal := First_Formal (Act_Subp);
11581 while Present (Anc_Formal)
11582 and then Present (Act_Formal)
11584 Anc_F_Type := Etype (Anc_Formal);
11585 Act_F_Type := Etype (Act_Formal);
11587 if Ekind (Anc_F_Type)
11588 = E_Anonymous_Access_Type
11590 Anc_F_Type := Designated_Type (Anc_F_Type);
11592 if Ekind (Act_F_Type)
11593 = E_Anonymous_Access_Type
11596 Designated_Type (Act_F_Type);
11602 Ekind (Act_F_Type) = E_Anonymous_Access_Type
11607 Anc_F_Type := Base_Type (Anc_F_Type);
11608 Act_F_Type := Base_Type (Act_F_Type);
11610 -- If the formal is controlling, then the
11611 -- the type of the actual primitive's formal
11612 -- must be derived directly or indirectly
11613 -- from the type of the ancestor primitive's
11616 if Is_Controlling_Formal (Anc_Formal) then
11617 if not Is_Tagged_Ancestor
11618 (Anc_F_Type, Act_F_Type)
11623 -- Otherwise the types of the formals must
11626 elsif Anc_F_Type /= Act_F_Type then
11630 Next_Entity (Anc_Formal);
11631 Next_Entity (Act_Formal);
11634 -- If we traversed through all of the formals
11635 -- then so far the subprograms correspond, so
11636 -- now check that any result types correspond.
11638 if No (Anc_Formal) and then No (Act_Formal) then
11639 Subprograms_Correspond := True;
11641 if Ekind (Act_Subp) = E_Function then
11642 Anc_F_Type := Etype (Anc_Subp);
11643 Act_F_Type := Etype (Act_Subp);
11645 if Ekind (Anc_F_Type)
11646 = E_Anonymous_Access_Type
11649 Designated_Type (Anc_F_Type);
11651 if Ekind (Act_F_Type)
11652 = E_Anonymous_Access_Type
11655 Designated_Type (Act_F_Type);
11657 Subprograms_Correspond := False;
11662 = E_Anonymous_Access_Type
11664 Subprograms_Correspond := False;
11667 Anc_F_Type := Base_Type (Anc_F_Type);
11668 Act_F_Type := Base_Type (Act_F_Type);
11670 -- Now either the result types must be
11671 -- the same or, if the result type is
11672 -- controlling, the result type of the
11673 -- actual primitive must descend from the
11674 -- result type of the ancestor primitive.
11676 if Subprograms_Correspond
11677 and then Anc_F_Type /= Act_F_Type
11679 Has_Controlling_Result (Anc_Subp)
11681 not Is_Tagged_Ancestor
11682 (Anc_F_Type, Act_F_Type)
11684 Subprograms_Correspond := False;
11688 -- Found a matching subprogram belonging to
11689 -- formal ancestor type, so actual subprogram
11690 -- corresponds and this violates 3.9.3(9).
11692 if Subprograms_Correspond then
11694 ("abstract subprogram & overrides " &
11695 "nonabstract subprogram of ancestor",
11703 Next_Elmt (Act_Elmt);
11707 Next_Elmt (Gen_Elmt);
11709 end Check_Abstract_Primitives;
11712 -- Verify that limitedness matches. If parent is a limited
11713 -- interface then the generic formal is not unless declared
11714 -- explicitly so. If not declared limited, the actual cannot be
11715 -- limited (see AI05-0087).
11717 -- Even though this AI is a binding interpretation, we enable the
11718 -- check only in Ada 2012 mode, because this improper construct
11719 -- shows up in user code and in existing B-tests.
11721 if Is_Limited_Type (Act_T)
11722 and then not Is_Limited_Type (A_Gen_T)
11723 and then Ada_Version >= Ada_2012
11725 if In_Instance then
11729 ("actual for non-limited & cannot be a limited type", Actual,
11731 Explain_Limited_Type (Act_T, Actual);
11732 Abandon_Instantiation (Actual);
11735 end Validate_Derived_Type_Instance;
11737 ----------------------------------------
11738 -- Validate_Discriminated_Formal_Type --
11739 ----------------------------------------
11741 procedure Validate_Discriminated_Formal_Type is
11742 Formal_Discr : Entity_Id;
11743 Actual_Discr : Entity_Id;
11744 Formal_Subt : Entity_Id;
11747 if Has_Discriminants (A_Gen_T) then
11748 if not Has_Discriminants (Act_T) then
11750 ("actual for & must have discriminants", Actual, Gen_T);
11751 Abandon_Instantiation (Actual);
11753 elsif Is_Constrained (Act_T) then
11755 ("actual for & must be unconstrained", Actual, Gen_T);
11756 Abandon_Instantiation (Actual);
11759 Formal_Discr := First_Discriminant (A_Gen_T);
11760 Actual_Discr := First_Discriminant (Act_T);
11761 while Formal_Discr /= Empty loop
11762 if Actual_Discr = Empty then
11764 ("discriminants on actual do not match formal",
11766 Abandon_Instantiation (Actual);
11769 Formal_Subt := Get_Instance_Of (Etype (Formal_Discr));
11771 -- Access discriminants match if designated types do
11773 if Ekind (Base_Type (Formal_Subt)) = E_Anonymous_Access_Type
11774 and then (Ekind (Base_Type (Etype (Actual_Discr)))) =
11775 E_Anonymous_Access_Type
11778 (Designated_Type (Base_Type (Formal_Subt))) =
11779 Designated_Type (Base_Type (Etype (Actual_Discr)))
11783 elsif Base_Type (Formal_Subt) /=
11784 Base_Type (Etype (Actual_Discr))
11787 ("types of actual discriminants must match formal",
11789 Abandon_Instantiation (Actual);
11791 elsif not Subtypes_Statically_Match
11792 (Formal_Subt, Etype (Actual_Discr))
11793 and then Ada_Version >= Ada_95
11796 ("subtypes of actual discriminants must match formal",
11798 Abandon_Instantiation (Actual);
11801 Next_Discriminant (Formal_Discr);
11802 Next_Discriminant (Actual_Discr);
11805 if Actual_Discr /= Empty then
11807 ("discriminants on actual do not match formal",
11809 Abandon_Instantiation (Actual);
11813 end Validate_Discriminated_Formal_Type;
11815 ---------------------------------------
11816 -- Validate_Incomplete_Type_Instance --
11817 ---------------------------------------
11819 procedure Validate_Incomplete_Type_Instance is
11821 if not Is_Tagged_Type (Act_T)
11822 and then Is_Tagged_Type (A_Gen_T)
11825 ("actual for & must be a tagged type", Actual, Gen_T);
11828 Validate_Discriminated_Formal_Type;
11829 end Validate_Incomplete_Type_Instance;
11831 --------------------------------------
11832 -- Validate_Interface_Type_Instance --
11833 --------------------------------------
11835 procedure Validate_Interface_Type_Instance is
11837 if not Is_Interface (Act_T) then
11839 ("actual for formal interface type must be an interface",
11842 elsif Is_Limited_Type (Act_T) /= Is_Limited_Type (A_Gen_T)
11844 Is_Task_Interface (A_Gen_T) /= Is_Task_Interface (Act_T)
11846 Is_Protected_Interface (A_Gen_T) /=
11847 Is_Protected_Interface (Act_T)
11849 Is_Synchronized_Interface (A_Gen_T) /=
11850 Is_Synchronized_Interface (Act_T)
11853 ("actual for interface& does not match (RM 12.5.5(4))",
11856 end Validate_Interface_Type_Instance;
11858 ------------------------------------
11859 -- Validate_Private_Type_Instance --
11860 ------------------------------------
11862 procedure Validate_Private_Type_Instance is
11864 if Is_Limited_Type (Act_T)
11865 and then not Is_Limited_Type (A_Gen_T)
11867 if In_Instance then
11871 ("actual for non-limited & cannot be a limited type", Actual,
11873 Explain_Limited_Type (Act_T, Actual);
11874 Abandon_Instantiation (Actual);
11877 elsif Known_To_Have_Preelab_Init (A_Gen_T)
11878 and then not Has_Preelaborable_Initialization (Act_T)
11881 ("actual for & must have preelaborable initialization", Actual,
11884 elsif Is_Indefinite_Subtype (Act_T)
11885 and then not Is_Indefinite_Subtype (A_Gen_T)
11886 and then Ada_Version >= Ada_95
11889 ("actual for & must be a definite subtype", Actual, Gen_T);
11891 elsif not Is_Tagged_Type (Act_T)
11892 and then Is_Tagged_Type (A_Gen_T)
11895 ("actual for & must be a tagged type", Actual, Gen_T);
11898 Validate_Discriminated_Formal_Type;
11900 end Validate_Private_Type_Instance;
11902 -- Start of processing for Instantiate_Type
11905 if Get_Instance_Of (A_Gen_T) /= A_Gen_T then
11906 Error_Msg_N ("duplicate instantiation of generic type", Actual);
11907 return New_List (Error);
11909 elsif not Is_Entity_Name (Actual)
11910 or else not Is_Type (Entity (Actual))
11913 ("expect valid subtype mark to instantiate &", Actual, Gen_T);
11914 Abandon_Instantiation (Actual);
11917 Act_T := Entity (Actual);
11919 -- Ada 2005 (AI-216): An Unchecked_Union subtype shall only be passed
11920 -- as a generic actual parameter if the corresponding formal type
11921 -- does not have a known_discriminant_part, or is a formal derived
11922 -- type that is an Unchecked_Union type.
11924 if Is_Unchecked_Union (Base_Type (Act_T)) then
11925 if not Has_Discriminants (A_Gen_T)
11927 (Is_Derived_Type (A_Gen_T)
11929 Is_Unchecked_Union (A_Gen_T))
11933 Error_Msg_N ("unchecked union cannot be the actual for a" &
11934 " discriminated formal type", Act_T);
11939 -- Deal with fixed/floating restrictions
11941 if Is_Floating_Point_Type (Act_T) then
11942 Check_Restriction (No_Floating_Point, Actual);
11943 elsif Is_Fixed_Point_Type (Act_T) then
11944 Check_Restriction (No_Fixed_Point, Actual);
11947 -- Deal with error of using incomplete type as generic actual.
11948 -- This includes limited views of a type, even if the non-limited
11949 -- view may be available.
11951 if Ekind (Act_T) = E_Incomplete_Type
11952 or else (Is_Class_Wide_Type (Act_T)
11954 Ekind (Root_Type (Act_T)) = E_Incomplete_Type)
11956 -- If the formal is an incomplete type, the actual can be
11957 -- incomplete as well.
11959 if Ekind (A_Gen_T) = E_Incomplete_Type then
11962 elsif Is_Class_Wide_Type (Act_T)
11963 or else No (Full_View (Act_T))
11965 Error_Msg_N ("premature use of incomplete type", Actual);
11966 Abandon_Instantiation (Actual);
11968 Act_T := Full_View (Act_T);
11969 Set_Entity (Actual, Act_T);
11971 if Has_Private_Component (Act_T) then
11973 ("premature use of type with private component", Actual);
11977 -- Deal with error of premature use of private type as generic actual
11979 elsif Is_Private_Type (Act_T)
11980 and then Is_Private_Type (Base_Type (Act_T))
11981 and then not Is_Generic_Type (Act_T)
11982 and then not Is_Derived_Type (Act_T)
11983 and then No (Full_View (Root_Type (Act_T)))
11985 -- If the formal is an incomplete type, the actual can be
11986 -- private or incomplete as well.
11988 if Ekind (A_Gen_T) = E_Incomplete_Type then
11991 Error_Msg_N ("premature use of private type", Actual);
11994 elsif Has_Private_Component (Act_T) then
11996 ("premature use of type with private component", Actual);
11999 Set_Instance_Of (A_Gen_T, Act_T);
12001 -- If the type is generic, the class-wide type may also be used
12003 if Is_Tagged_Type (A_Gen_T)
12004 and then Is_Tagged_Type (Act_T)
12005 and then not Is_Class_Wide_Type (A_Gen_T)
12007 Set_Instance_Of (Class_Wide_Type (A_Gen_T),
12008 Class_Wide_Type (Act_T));
12011 if not Is_Abstract_Type (A_Gen_T)
12012 and then Is_Abstract_Type (Act_T)
12015 ("actual of non-abstract formal cannot be abstract", Actual);
12018 -- A generic scalar type is a first subtype for which we generate
12019 -- an anonymous base type. Indicate that the instance of this base
12020 -- is the base type of the actual.
12022 if Is_Scalar_Type (A_Gen_T) then
12023 Set_Instance_Of (Etype (A_Gen_T), Etype (Act_T));
12027 if Error_Posted (Act_T) then
12030 case Nkind (Def) is
12031 when N_Formal_Private_Type_Definition =>
12032 Validate_Private_Type_Instance;
12034 when N_Formal_Incomplete_Type_Definition =>
12035 Validate_Incomplete_Type_Instance;
12037 when N_Formal_Derived_Type_Definition =>
12038 Validate_Derived_Type_Instance;
12040 when N_Formal_Discrete_Type_Definition =>
12041 if not Is_Discrete_Type (Act_T) then
12043 ("expect discrete type in instantiation of&",
12045 Abandon_Instantiation (Actual);
12048 Diagnose_Predicated_Actual;
12050 when N_Formal_Signed_Integer_Type_Definition =>
12051 if not Is_Signed_Integer_Type (Act_T) then
12053 ("expect signed integer type in instantiation of&",
12055 Abandon_Instantiation (Actual);
12058 Diagnose_Predicated_Actual;
12060 when N_Formal_Modular_Type_Definition =>
12061 if not Is_Modular_Integer_Type (Act_T) then
12063 ("expect modular type in instantiation of &",
12065 Abandon_Instantiation (Actual);
12068 Diagnose_Predicated_Actual;
12070 when N_Formal_Floating_Point_Definition =>
12071 if not Is_Floating_Point_Type (Act_T) then
12073 ("expect float type in instantiation of &", Actual, Gen_T);
12074 Abandon_Instantiation (Actual);
12077 when N_Formal_Ordinary_Fixed_Point_Definition =>
12078 if not Is_Ordinary_Fixed_Point_Type (Act_T) then
12080 ("expect ordinary fixed point type in instantiation of &",
12082 Abandon_Instantiation (Actual);
12085 when N_Formal_Decimal_Fixed_Point_Definition =>
12086 if not Is_Decimal_Fixed_Point_Type (Act_T) then
12088 ("expect decimal type in instantiation of &",
12090 Abandon_Instantiation (Actual);
12093 when N_Array_Type_Definition =>
12094 Validate_Array_Type_Instance;
12096 when N_Access_To_Object_Definition =>
12097 Validate_Access_Type_Instance;
12099 when N_Access_Function_Definition |
12100 N_Access_Procedure_Definition =>
12101 Validate_Access_Subprogram_Instance;
12103 when N_Record_Definition =>
12104 Validate_Interface_Type_Instance;
12106 when N_Derived_Type_Definition =>
12107 Validate_Derived_Interface_Type_Instance;
12110 raise Program_Error;
12115 Subt := New_Copy (Gen_T);
12117 -- Use adjusted sloc of subtype name as the location for other nodes in
12118 -- the subtype declaration.
12120 Loc := Sloc (Subt);
12123 Make_Subtype_Declaration (Loc,
12124 Defining_Identifier => Subt,
12125 Subtype_Indication => New_Occurrence_Of (Act_T, Loc));
12127 if Is_Private_Type (Act_T) then
12128 Set_Has_Private_View (Subtype_Indication (Decl_Node));
12130 elsif Is_Access_Type (Act_T)
12131 and then Is_Private_Type (Designated_Type (Act_T))
12133 Set_Has_Private_View (Subtype_Indication (Decl_Node));
12136 Decl_Nodes := New_List (Decl_Node);
12138 -- Flag actual derived types so their elaboration produces the
12139 -- appropriate renamings for the primitive operations of the ancestor.
12140 -- Flag actual for formal private types as well, to determine whether
12141 -- operations in the private part may override inherited operations.
12142 -- If the formal has an interface list, the ancestor is not the
12143 -- parent, but the analyzed formal that includes the interface
12144 -- operations of all its progenitors.
12146 -- Same treatment for formal private types, so we can check whether the
12147 -- type is tagged limited when validating derivations in the private
12148 -- part. (See AI05-096).
12150 if Nkind (Def) = N_Formal_Derived_Type_Definition then
12151 if Present (Interface_List (Def)) then
12152 Set_Generic_Parent_Type (Decl_Node, A_Gen_T);
12154 Set_Generic_Parent_Type (Decl_Node, Ancestor);
12157 elsif Nkind_In (Def,
12158 N_Formal_Private_Type_Definition,
12159 N_Formal_Incomplete_Type_Definition)
12161 Set_Generic_Parent_Type (Decl_Node, A_Gen_T);
12164 -- If the actual is a synchronized type that implements an interface,
12165 -- the primitive operations are attached to the corresponding record,
12166 -- and we have to treat it as an additional generic actual, so that its
12167 -- primitive operations become visible in the instance. The task or
12168 -- protected type itself does not carry primitive operations.
12170 if Is_Concurrent_Type (Act_T)
12171 and then Is_Tagged_Type (Act_T)
12172 and then Present (Corresponding_Record_Type (Act_T))
12173 and then Present (Ancestor)
12174 and then Is_Interface (Ancestor)
12177 Corr_Rec : constant Entity_Id :=
12178 Corresponding_Record_Type (Act_T);
12179 New_Corr : Entity_Id;
12180 Corr_Decl : Node_Id;
12183 New_Corr := Make_Temporary (Loc, 'S');
12185 Make_Subtype_Declaration (Loc,
12186 Defining_Identifier => New_Corr,
12187 Subtype_Indication =>
12188 New_Occurrence_Of (Corr_Rec, Loc));
12189 Append_To (Decl_Nodes, Corr_Decl);
12191 if Ekind (Act_T) = E_Task_Type then
12192 Set_Ekind (Subt, E_Task_Subtype);
12194 Set_Ekind (Subt, E_Protected_Subtype);
12197 Set_Corresponding_Record_Type (Subt, Corr_Rec);
12198 Set_Generic_Parent_Type (Corr_Decl, Ancestor);
12199 Set_Generic_Parent_Type (Decl_Node, Empty);
12204 end Instantiate_Type;
12206 ---------------------
12207 -- Is_In_Main_Unit --
12208 ---------------------
12210 function Is_In_Main_Unit (N : Node_Id) return Boolean is
12211 Unum : constant Unit_Number_Type := Get_Source_Unit (N);
12212 Current_Unit : Node_Id;
12215 if Unum = Main_Unit then
12218 -- If the current unit is a subunit then it is either the main unit or
12219 -- is being compiled as part of the main unit.
12221 elsif Nkind (N) = N_Compilation_Unit then
12222 return Nkind (Unit (N)) = N_Subunit;
12225 Current_Unit := Parent (N);
12226 while Present (Current_Unit)
12227 and then Nkind (Current_Unit) /= N_Compilation_Unit
12229 Current_Unit := Parent (Current_Unit);
12232 -- The instantiation node is in the main unit, or else the current node
12233 -- (perhaps as the result of nested instantiations) is in the main unit,
12234 -- or in the declaration of the main unit, which in this last case must
12237 return Unum = Main_Unit
12238 or else Current_Unit = Cunit (Main_Unit)
12239 or else Current_Unit = Library_Unit (Cunit (Main_Unit))
12240 or else (Present (Library_Unit (Current_Unit))
12241 and then Is_In_Main_Unit (Library_Unit (Current_Unit)));
12242 end Is_In_Main_Unit;
12244 ----------------------------
12245 -- Load_Parent_Of_Generic --
12246 ----------------------------
12248 procedure Load_Parent_Of_Generic
12251 Body_Optional : Boolean := False)
12253 Comp_Unit : constant Node_Id := Cunit (Get_Source_Unit (Spec));
12254 Saved_Style_Check : constant Boolean := Style_Check;
12255 Saved_Warnings : constant Warning_Record := Save_Warnings;
12256 True_Parent : Node_Id;
12257 Inst_Node : Node_Id;
12259 Previous_Instances : constant Elist_Id := New_Elmt_List;
12261 procedure Collect_Previous_Instances (Decls : List_Id);
12262 -- Collect all instantiations in the given list of declarations, that
12263 -- precede the generic that we need to load. If the bodies of these
12264 -- instantiations are available, we must analyze them, to ensure that
12265 -- the public symbols generated are the same when the unit is compiled
12266 -- to generate code, and when it is compiled in the context of a unit
12267 -- that needs a particular nested instance. This process is applied to
12268 -- both package and subprogram instances.
12270 --------------------------------
12271 -- Collect_Previous_Instances --
12272 --------------------------------
12274 procedure Collect_Previous_Instances (Decls : List_Id) is
12278 Decl := First (Decls);
12279 while Present (Decl) loop
12280 if Sloc (Decl) >= Sloc (Inst_Node) then
12283 -- If Decl is an instantiation, then record it as requiring
12284 -- instantiation of the corresponding body, except if it is an
12285 -- abbreviated instantiation generated internally for conformance
12286 -- checking purposes only for the case of a formal package
12287 -- declared without a box (see Instantiate_Formal_Package). Such
12288 -- an instantiation does not generate any code (the actual code
12289 -- comes from actual) and thus does not need to be analyzed here.
12290 -- If the instantiation appears with a generic package body it is
12291 -- not analyzed here either.
12293 elsif Nkind (Decl) = N_Package_Instantiation
12294 and then not Is_Internal (Defining_Entity (Decl))
12296 Append_Elmt (Decl, Previous_Instances);
12298 -- For a subprogram instantiation, omit instantiations intrinsic
12299 -- operations (Unchecked_Conversions, etc.) that have no bodies.
12301 elsif Nkind_In (Decl, N_Function_Instantiation,
12302 N_Procedure_Instantiation)
12303 and then not Is_Intrinsic_Subprogram (Entity (Name (Decl)))
12305 Append_Elmt (Decl, Previous_Instances);
12307 elsif Nkind (Decl) = N_Package_Declaration then
12308 Collect_Previous_Instances
12309 (Visible_Declarations (Specification (Decl)));
12310 Collect_Previous_Instances
12311 (Private_Declarations (Specification (Decl)));
12313 -- Previous non-generic bodies may contain instances as well
12315 elsif Nkind (Decl) = N_Package_Body
12316 and then Ekind (Corresponding_Spec (Decl)) /= E_Generic_Package
12318 Collect_Previous_Instances (Declarations (Decl));
12320 elsif Nkind (Decl) = N_Subprogram_Body
12321 and then not Acts_As_Spec (Decl)
12322 and then not Is_Generic_Subprogram (Corresponding_Spec (Decl))
12324 Collect_Previous_Instances (Declarations (Decl));
12329 end Collect_Previous_Instances;
12331 -- Start of processing for Load_Parent_Of_Generic
12334 if not In_Same_Source_Unit (N, Spec)
12335 or else Nkind (Unit (Comp_Unit)) = N_Package_Declaration
12336 or else (Nkind (Unit (Comp_Unit)) = N_Package_Body
12337 and then not Is_In_Main_Unit (Spec))
12339 -- Find body of parent of spec, and analyze it. A special case arises
12340 -- when the parent is an instantiation, that is to say when we are
12341 -- currently instantiating a nested generic. In that case, there is
12342 -- no separate file for the body of the enclosing instance. Instead,
12343 -- the enclosing body must be instantiated as if it were a pending
12344 -- instantiation, in order to produce the body for the nested generic
12345 -- we require now. Note that in that case the generic may be defined
12346 -- in a package body, the instance defined in the same package body,
12347 -- and the original enclosing body may not be in the main unit.
12349 Inst_Node := Empty;
12351 True_Parent := Parent (Spec);
12352 while Present (True_Parent)
12353 and then Nkind (True_Parent) /= N_Compilation_Unit
12355 if Nkind (True_Parent) = N_Package_Declaration
12357 Nkind (Original_Node (True_Parent)) = N_Package_Instantiation
12359 -- Parent is a compilation unit that is an instantiation.
12360 -- Instantiation node has been replaced with package decl.
12362 Inst_Node := Original_Node (True_Parent);
12365 elsif Nkind (True_Parent) = N_Package_Declaration
12366 and then Present (Generic_Parent (Specification (True_Parent)))
12367 and then Nkind (Parent (True_Parent)) /= N_Compilation_Unit
12369 -- Parent is an instantiation within another specification.
12370 -- Declaration for instance has been inserted before original
12371 -- instantiation node. A direct link would be preferable?
12373 Inst_Node := Next (True_Parent);
12374 while Present (Inst_Node)
12375 and then Nkind (Inst_Node) /= N_Package_Instantiation
12380 -- If the instance appears within a generic, and the generic
12381 -- unit is defined within a formal package of the enclosing
12382 -- generic, there is no generic body available, and none
12383 -- needed. A more precise test should be used ???
12385 if No (Inst_Node) then
12392 True_Parent := Parent (True_Parent);
12396 -- Case where we are currently instantiating a nested generic
12398 if Present (Inst_Node) then
12399 if Nkind (Parent (True_Parent)) = N_Compilation_Unit then
12401 -- Instantiation node and declaration of instantiated package
12402 -- were exchanged when only the declaration was needed.
12403 -- Restore instantiation node before proceeding with body.
12405 Set_Unit (Parent (True_Parent), Inst_Node);
12408 -- Now complete instantiation of enclosing body, if it appears in
12409 -- some other unit. If it appears in the current unit, the body
12410 -- will have been instantiated already.
12412 if No (Corresponding_Body (Instance_Spec (Inst_Node))) then
12414 -- We need to determine the expander mode to instantiate the
12415 -- enclosing body. Because the generic body we need may use
12416 -- global entities declared in the enclosing package (including
12417 -- aggregates) it is in general necessary to compile this body
12418 -- with expansion enabled, except if we are within a generic
12419 -- package, in which case the usual generic rule applies.
12422 Exp_Status : Boolean := True;
12426 -- Loop through scopes looking for generic package
12428 Scop := Scope (Defining_Entity (Instance_Spec (Inst_Node)));
12429 while Present (Scop)
12430 and then Scop /= Standard_Standard
12432 if Ekind (Scop) = E_Generic_Package then
12433 Exp_Status := False;
12437 Scop := Scope (Scop);
12440 -- Collect previous instantiations in the unit that contains
12441 -- the desired generic.
12443 if Nkind (Parent (True_Parent)) /= N_Compilation_Unit
12444 and then not Body_Optional
12448 Info : Pending_Body_Info;
12452 Par := Parent (Inst_Node);
12453 while Present (Par) loop
12454 exit when Nkind (Parent (Par)) = N_Compilation_Unit;
12455 Par := Parent (Par);
12458 pragma Assert (Present (Par));
12460 if Nkind (Par) = N_Package_Body then
12461 Collect_Previous_Instances (Declarations (Par));
12463 elsif Nkind (Par) = N_Package_Declaration then
12464 Collect_Previous_Instances
12465 (Visible_Declarations (Specification (Par)));
12466 Collect_Previous_Instances
12467 (Private_Declarations (Specification (Par)));
12470 -- Enclosing unit is a subprogram body. In this
12471 -- case all instance bodies are processed in order
12472 -- and there is no need to collect them separately.
12477 Decl := First_Elmt (Previous_Instances);
12478 while Present (Decl) loop
12480 (Inst_Node => Node (Decl),
12482 Instance_Spec (Node (Decl)),
12483 Expander_Status => Exp_Status,
12484 Current_Sem_Unit =>
12485 Get_Code_Unit (Sloc (Node (Decl))),
12486 Scope_Suppress => Scope_Suppress,
12487 Local_Suppress_Stack_Top =>
12488 Local_Suppress_Stack_Top,
12489 Version => Ada_Version,
12490 Version_Pragma => Ada_Version_Pragma,
12491 Warnings => Save_Warnings,
12492 SPARK_Mode => SPARK_Mode,
12493 SPARK_Mode_Pragma => SPARK_Mode_Pragma);
12495 -- Package instance
12498 Nkind (Node (Decl)) = N_Package_Instantiation
12500 Instantiate_Package_Body
12501 (Info, Body_Optional => True);
12503 -- Subprogram instance
12506 -- The instance_spec is the wrapper package,
12507 -- and the subprogram declaration is the last
12508 -- declaration in the wrapper.
12512 (Visible_Declarations
12513 (Specification (Info.Act_Decl)));
12515 Instantiate_Subprogram_Body
12516 (Info, Body_Optional => True);
12524 Instantiate_Package_Body
12526 ((Inst_Node => Inst_Node,
12527 Act_Decl => True_Parent,
12528 Expander_Status => Exp_Status,
12529 Current_Sem_Unit => Get_Code_Unit
12530 (Sloc (Inst_Node)),
12531 Scope_Suppress => Scope_Suppress,
12532 Local_Suppress_Stack_Top => Local_Suppress_Stack_Top,
12533 Version => Ada_Version,
12534 Version_Pragma => Ada_Version_Pragma,
12535 Warnings => Save_Warnings,
12536 SPARK_Mode => SPARK_Mode,
12537 SPARK_Mode_Pragma => SPARK_Mode_Pragma)),
12538 Body_Optional => Body_Optional);
12542 -- Case where we are not instantiating a nested generic
12545 Opt.Style_Check := False;
12546 Expander_Mode_Save_And_Set (True);
12547 Load_Needed_Body (Comp_Unit, OK);
12548 Opt.Style_Check := Saved_Style_Check;
12549 Restore_Warnings (Saved_Warnings);
12550 Expander_Mode_Restore;
12553 and then Unit_Requires_Body (Defining_Entity (Spec))
12554 and then not Body_Optional
12557 Bname : constant Unit_Name_Type :=
12558 Get_Body_Name (Get_Unit_Name (Unit (Comp_Unit)));
12561 -- In CodePeer mode, the missing body may make the analysis
12562 -- incomplete, but we do not treat it as fatal.
12564 if CodePeer_Mode then
12568 Error_Msg_Unit_1 := Bname;
12569 Error_Msg_N ("this instantiation requires$!", N);
12570 Error_Msg_File_1 :=
12571 Get_File_Name (Bname, Subunit => False);
12572 Error_Msg_N ("\but file{ was not found!", N);
12573 raise Unrecoverable_Error;
12580 -- If loading parent of the generic caused an instantiation circularity,
12581 -- we abandon compilation at this point, because otherwise in some cases
12582 -- we get into trouble with infinite recursions after this point.
12584 if Circularity_Detected then
12585 raise Unrecoverable_Error;
12587 end Load_Parent_Of_Generic;
12589 ---------------------------------
12590 -- Map_Formal_Package_Entities --
12591 ---------------------------------
12593 procedure Map_Formal_Package_Entities (Form : Entity_Id; Act : Entity_Id) is
12598 Set_Instance_Of (Form, Act);
12600 -- Traverse formal and actual package to map the corresponding entities.
12601 -- We skip over internal entities that may be generated during semantic
12602 -- analysis, and find the matching entities by name, given that they
12603 -- must appear in the same order.
12605 E1 := First_Entity (Form);
12606 E2 := First_Entity (Act);
12607 while Present (E1) and then E1 /= First_Private_Entity (Form) loop
12608 -- Could this test be a single condition??? Seems like it could, and
12609 -- isn't FPE (Form) a constant anyway???
12611 if not Is_Internal (E1)
12612 and then Present (Parent (E1))
12613 and then not Is_Class_Wide_Type (E1)
12614 and then not Is_Internal_Name (Chars (E1))
12616 while Present (E2) and then Chars (E2) /= Chars (E1) loop
12623 Set_Instance_Of (E1, E2);
12625 if Is_Type (E1) and then Is_Tagged_Type (E2) then
12626 Set_Instance_Of (Class_Wide_Type (E1), Class_Wide_Type (E2));
12629 if Is_Constrained (E1) then
12630 Set_Instance_Of (Base_Type (E1), Base_Type (E2));
12633 if Ekind (E1) = E_Package and then No (Renamed_Object (E1)) then
12634 Map_Formal_Package_Entities (E1, E2);
12641 end Map_Formal_Package_Entities;
12643 -----------------------
12644 -- Move_Freeze_Nodes --
12645 -----------------------
12647 procedure Move_Freeze_Nodes
12648 (Out_Of : Entity_Id;
12653 Next_Decl : Node_Id;
12654 Next_Node : Node_Id := After;
12657 function Is_Outer_Type (T : Entity_Id) return Boolean;
12658 -- Check whether entity is declared in a scope external to that of the
12661 -------------------
12662 -- Is_Outer_Type --
12663 -------------------
12665 function Is_Outer_Type (T : Entity_Id) return Boolean is
12666 Scop : Entity_Id := Scope (T);
12669 if Scope_Depth (Scop) < Scope_Depth (Out_Of) then
12673 while Scop /= Standard_Standard loop
12674 if Scop = Out_Of then
12677 Scop := Scope (Scop);
12685 -- Start of processing for Move_Freeze_Nodes
12692 -- First remove the freeze nodes that may appear before all other
12696 while Present (Decl)
12697 and then Nkind (Decl) = N_Freeze_Entity
12698 and then Is_Outer_Type (Entity (Decl))
12700 Decl := Remove_Head (L);
12701 Insert_After (Next_Node, Decl);
12702 Set_Analyzed (Decl, False);
12707 -- Next scan the list of declarations and remove each freeze node that
12708 -- appears ahead of the current node.
12710 while Present (Decl) loop
12711 while Present (Next (Decl))
12712 and then Nkind (Next (Decl)) = N_Freeze_Entity
12713 and then Is_Outer_Type (Entity (Next (Decl)))
12715 Next_Decl := Remove_Next (Decl);
12716 Insert_After (Next_Node, Next_Decl);
12717 Set_Analyzed (Next_Decl, False);
12718 Next_Node := Next_Decl;
12721 -- If the declaration is a nested package or concurrent type, then
12722 -- recurse. Nested generic packages will have been processed from the
12725 case Nkind (Decl) is
12726 when N_Package_Declaration =>
12727 Spec := Specification (Decl);
12729 when N_Task_Type_Declaration =>
12730 Spec := Task_Definition (Decl);
12732 when N_Protected_Type_Declaration =>
12733 Spec := Protected_Definition (Decl);
12739 if Present (Spec) then
12740 Move_Freeze_Nodes (Out_Of, Next_Node, Visible_Declarations (Spec));
12741 Move_Freeze_Nodes (Out_Of, Next_Node, Private_Declarations (Spec));
12746 end Move_Freeze_Nodes;
12752 function Next_Assoc (E : Assoc_Ptr) return Assoc_Ptr is
12754 return Generic_Renamings.Table (E).Next_In_HTable;
12757 ------------------------
12758 -- Preanalyze_Actuals --
12759 ------------------------
12761 procedure Preanalyze_Actuals (N : Node_Id) is
12764 Errs : constant Int := Serious_Errors_Detected;
12766 Cur : Entity_Id := Empty;
12767 -- Current homograph of the instance name
12770 -- Saved visibility status of the current homograph
12773 Assoc := First (Generic_Associations (N));
12775 -- If the instance is a child unit, its name may hide an outer homonym,
12776 -- so make it invisible to perform name resolution on the actuals.
12778 if Nkind (Defining_Unit_Name (N)) = N_Defining_Program_Unit_Name
12780 (Current_Entity (Defining_Identifier (Defining_Unit_Name (N))))
12782 Cur := Current_Entity (Defining_Identifier (Defining_Unit_Name (N)));
12784 if Is_Compilation_Unit (Cur) then
12785 Vis := Is_Immediately_Visible (Cur);
12786 Set_Is_Immediately_Visible (Cur, False);
12792 while Present (Assoc) loop
12793 if Nkind (Assoc) /= N_Others_Choice then
12794 Act := Explicit_Generic_Actual_Parameter (Assoc);
12796 -- Within a nested instantiation, a defaulted actual is an empty
12797 -- association, so nothing to analyze. If the subprogram actual
12798 -- is an attribute, analyze prefix only, because actual is not a
12799 -- complete attribute reference.
12801 -- If actual is an allocator, analyze expression only. The full
12802 -- analysis can generate code, and if instance is a compilation
12803 -- unit we have to wait until the package instance is installed
12804 -- to have a proper place to insert this code.
12806 -- String literals may be operators, but at this point we do not
12807 -- know whether the actual is a formal subprogram or a string.
12812 elsif Nkind (Act) = N_Attribute_Reference then
12813 Analyze (Prefix (Act));
12815 elsif Nkind (Act) = N_Explicit_Dereference then
12816 Analyze (Prefix (Act));
12818 elsif Nkind (Act) = N_Allocator then
12820 Expr : constant Node_Id := Expression (Act);
12823 if Nkind (Expr) = N_Subtype_Indication then
12824 Analyze (Subtype_Mark (Expr));
12826 -- Analyze separately each discriminant constraint, when
12827 -- given with a named association.
12833 Constr := First (Constraints (Constraint (Expr)));
12834 while Present (Constr) loop
12835 if Nkind (Constr) = N_Discriminant_Association then
12836 Analyze (Expression (Constr));
12850 elsif Nkind (Act) /= N_Operator_Symbol then
12854 -- Ensure that a ghost subprogram does not act as generic actual
12856 if Is_Entity_Name (Act)
12857 and then Is_Ghost_Subprogram (Entity (Act))
12860 ("ghost subprogram & cannot act as generic actual", Act);
12861 Abandon_Instantiation (Act);
12863 elsif Errs /= Serious_Errors_Detected then
12865 -- Do a minimal analysis of the generic, to prevent spurious
12866 -- warnings complaining about the generic being unreferenced,
12867 -- before abandoning the instantiation.
12869 Analyze (Name (N));
12871 if Is_Entity_Name (Name (N))
12872 and then Etype (Name (N)) /= Any_Type
12874 Generate_Reference (Entity (Name (N)), Name (N));
12875 Set_Is_Instantiated (Entity (Name (N)));
12878 if Present (Cur) then
12880 -- For the case of a child instance hiding an outer homonym,
12881 -- provide additional warning which might explain the error.
12883 Set_Is_Immediately_Visible (Cur, Vis);
12884 Error_Msg_NE ("& hides outer unit with the same name??",
12885 N, Defining_Unit_Name (N));
12888 Abandon_Instantiation (Act);
12895 if Present (Cur) then
12896 Set_Is_Immediately_Visible (Cur, Vis);
12898 end Preanalyze_Actuals;
12900 -------------------
12901 -- Remove_Parent --
12902 -------------------
12904 procedure Remove_Parent (In_Body : Boolean := False) is
12905 S : Entity_Id := Current_Scope;
12906 -- S is the scope containing the instantiation just completed. The scope
12907 -- stack contains the parent instances of the instantiation, followed by
12916 -- After child instantiation is complete, remove from scope stack the
12917 -- extra copy of the current scope, and then remove parent instances.
12919 if not In_Body then
12922 while Current_Scope /= S loop
12923 P := Current_Scope;
12924 End_Package_Scope (Current_Scope);
12926 if In_Open_Scopes (P) then
12927 E := First_Entity (P);
12928 while Present (E) loop
12929 Set_Is_Immediately_Visible (E, True);
12933 -- If instantiation is declared in a block, it is the enclosing
12934 -- scope that might be a parent instance. Note that only one
12935 -- block can be involved, because the parent instances have
12936 -- been installed within it.
12938 if Ekind (P) = E_Block then
12939 Cur_P := Scope (P);
12944 if Is_Generic_Instance (Cur_P) and then P /= Current_Scope then
12945 -- We are within an instance of some sibling. Retain
12946 -- visibility of parent, for proper subsequent cleanup, and
12947 -- reinstall private declarations as well.
12949 Set_In_Private_Part (P);
12950 Install_Private_Declarations (P);
12953 -- If the ultimate parent is a top-level unit recorded in
12954 -- Instance_Parent_Unit, then reset its visibility to what it was
12955 -- before instantiation. (It's not clear what the purpose is of
12956 -- testing whether Scope (P) is In_Open_Scopes, but that test was
12957 -- present before the ultimate parent test was added.???)
12959 elsif not In_Open_Scopes (Scope (P))
12960 or else (P = Instance_Parent_Unit
12961 and then not Parent_Unit_Visible)
12963 Set_Is_Immediately_Visible (P, False);
12965 -- If the current scope is itself an instantiation of a generic
12966 -- nested within P, and we are in the private part of body of this
12967 -- instantiation, restore the full views of P, that were removed
12968 -- in End_Package_Scope above. This obscure case can occur when a
12969 -- subunit of a generic contains an instance of a child unit of
12970 -- its generic parent unit.
12972 elsif S = Current_Scope and then Is_Generic_Instance (S) then
12974 Par : constant Entity_Id :=
12975 Generic_Parent (Package_Specification (S));
12978 and then P = Scope (Par)
12979 and then (In_Package_Body (S) or else In_Private_Part (S))
12981 Set_In_Private_Part (P);
12982 Install_Private_Declarations (P);
12988 -- Reset visibility of entities in the enclosing scope
12990 Set_Is_Hidden_Open_Scope (Current_Scope, False);
12992 Hidden := First_Elmt (Hidden_Entities);
12993 while Present (Hidden) loop
12994 Set_Is_Immediately_Visible (Node (Hidden), True);
12995 Next_Elmt (Hidden);
12999 -- Each body is analyzed separately, and there is no context that
13000 -- needs preserving from one body instance to the next, so remove all
13001 -- parent scopes that have been installed.
13003 while Present (S) loop
13004 End_Package_Scope (S);
13005 Set_Is_Immediately_Visible (S, False);
13006 S := Current_Scope;
13007 exit when S = Standard_Standard;
13016 procedure Restore_Env is
13017 Saved : Instance_Env renames Instance_Envs.Table (Instance_Envs.Last);
13020 if No (Current_Instantiated_Parent.Act_Id) then
13021 -- Restore environment after subprogram inlining
13023 Restore_Private_Views (Empty);
13026 Current_Instantiated_Parent := Saved.Instantiated_Parent;
13027 Exchanged_Views := Saved.Exchanged_Views;
13028 Hidden_Entities := Saved.Hidden_Entities;
13029 Current_Sem_Unit := Saved.Current_Sem_Unit;
13030 Parent_Unit_Visible := Saved.Parent_Unit_Visible;
13031 Instance_Parent_Unit := Saved.Instance_Parent_Unit;
13033 Restore_Opt_Config_Switches (Saved.Switches);
13035 Instance_Envs.Decrement_Last;
13038 ---------------------------
13039 -- Restore_Private_Views --
13040 ---------------------------
13042 procedure Restore_Private_Views
13043 (Pack_Id : Entity_Id;
13044 Is_Package : Boolean := True)
13049 Dep_Elmt : Elmt_Id;
13052 procedure Restore_Nested_Formal (Formal : Entity_Id);
13053 -- Hide the generic formals of formal packages declared with box which
13054 -- were reachable in the current instantiation.
13056 ---------------------------
13057 -- Restore_Nested_Formal --
13058 ---------------------------
13060 procedure Restore_Nested_Formal (Formal : Entity_Id) is
13064 if Present (Renamed_Object (Formal))
13065 and then Denotes_Formal_Package (Renamed_Object (Formal), True)
13069 elsif Present (Associated_Formal_Package (Formal)) then
13070 Ent := First_Entity (Formal);
13071 while Present (Ent) loop
13072 exit when Ekind (Ent) = E_Package
13073 and then Renamed_Entity (Ent) = Renamed_Entity (Formal);
13075 Set_Is_Hidden (Ent);
13076 Set_Is_Potentially_Use_Visible (Ent, False);
13078 -- If package, then recurse
13080 if Ekind (Ent) = E_Package then
13081 Restore_Nested_Formal (Ent);
13087 end Restore_Nested_Formal;
13089 -- Start of processing for Restore_Private_Views
13092 M := First_Elmt (Exchanged_Views);
13093 while Present (M) loop
13096 -- Subtypes of types whose views have been exchanged, and that are
13097 -- defined within the instance, were not on the Private_Dependents
13098 -- list on entry to the instance, so they have to be exchanged
13099 -- explicitly now, in order to remain consistent with the view of the
13102 if Ekind_In (Typ, E_Private_Type,
13103 E_Limited_Private_Type,
13104 E_Record_Type_With_Private)
13106 Dep_Elmt := First_Elmt (Private_Dependents (Typ));
13107 while Present (Dep_Elmt) loop
13108 Dep_Typ := Node (Dep_Elmt);
13110 if Scope (Dep_Typ) = Pack_Id
13111 and then Present (Full_View (Dep_Typ))
13113 Replace_Elmt (Dep_Elmt, Full_View (Dep_Typ));
13114 Exchange_Declarations (Dep_Typ);
13117 Next_Elmt (Dep_Elmt);
13121 Exchange_Declarations (Node (M));
13125 if No (Pack_Id) then
13129 -- Make the generic formal parameters private, and make the formal types
13130 -- into subtypes of the actuals again.
13132 E := First_Entity (Pack_Id);
13133 while Present (E) loop
13134 Set_Is_Hidden (E, True);
13137 and then Nkind (Parent (E)) = N_Subtype_Declaration
13139 -- If the actual for E is itself a generic actual type from
13140 -- an enclosing instance, E is still a generic actual type
13141 -- outside of the current instance. This matter when resolving
13142 -- an overloaded call that may be ambiguous in the enclosing
13143 -- instance, when two of its actuals coincide.
13145 if Is_Entity_Name (Subtype_Indication (Parent (E)))
13146 and then Is_Generic_Actual_Type
13147 (Entity (Subtype_Indication (Parent (E))))
13151 Set_Is_Generic_Actual_Type (E, False);
13154 -- An unusual case of aliasing: the actual may also be directly
13155 -- visible in the generic, and be private there, while it is fully
13156 -- visible in the context of the instance. The internal subtype
13157 -- is private in the instance but has full visibility like its
13158 -- parent in the enclosing scope. This enforces the invariant that
13159 -- the privacy status of all private dependents of a type coincide
13160 -- with that of the parent type. This can only happen when a
13161 -- generic child unit is instantiated within a sibling.
13163 if Is_Private_Type (E)
13164 and then not Is_Private_Type (Etype (E))
13166 Exchange_Declarations (E);
13169 elsif Ekind (E) = E_Package then
13171 -- The end of the renaming list is the renaming of the generic
13172 -- package itself. If the instance is a subprogram, all entities
13173 -- in the corresponding package are renamings. If this entity is
13174 -- a formal package, make its own formals private as well. The
13175 -- actual in this case is itself the renaming of an instantiation.
13176 -- If the entity is not a package renaming, it is the entity
13177 -- created to validate formal package actuals: ignore it.
13179 -- If the actual is itself a formal package for the enclosing
13180 -- generic, or the actual for such a formal package, it remains
13181 -- visible on exit from the instance, and therefore nothing needs
13182 -- to be done either, except to keep it accessible.
13184 if Is_Package and then Renamed_Object (E) = Pack_Id then
13187 elsif Nkind (Parent (E)) /= N_Package_Renaming_Declaration then
13191 Denotes_Formal_Package (Renamed_Object (E), True, Pack_Id)
13193 Set_Is_Hidden (E, False);
13197 Act_P : constant Entity_Id := Renamed_Object (E);
13201 Id := First_Entity (Act_P);
13203 and then Id /= First_Private_Entity (Act_P)
13205 exit when Ekind (Id) = E_Package
13206 and then Renamed_Object (Id) = Act_P;
13208 Set_Is_Hidden (Id, True);
13209 Set_Is_Potentially_Use_Visible (Id, In_Use (Act_P));
13211 if Ekind (Id) = E_Package then
13212 Restore_Nested_Formal (Id);
13223 end Restore_Private_Views;
13230 (Gen_Unit : Entity_Id;
13231 Act_Unit : Entity_Id)
13235 Set_Instance_Env (Gen_Unit, Act_Unit);
13238 ----------------------------
13239 -- Save_Global_References --
13240 ----------------------------
13242 procedure Save_Global_References (N : Node_Id) is
13243 Gen_Scope : Entity_Id;
13247 function Is_Global (E : Entity_Id) return Boolean;
13248 -- Check whether entity is defined outside of generic unit. Examine the
13249 -- scope of an entity, and the scope of the scope, etc, until we find
13250 -- either Standard, in which case the entity is global, or the generic
13251 -- unit itself, which indicates that the entity is local. If the entity
13252 -- is the generic unit itself, as in the case of a recursive call, or
13253 -- the enclosing generic unit, if different from the current scope, then
13254 -- it is local as well, because it will be replaced at the point of
13255 -- instantiation. On the other hand, if it is a reference to a child
13256 -- unit of a common ancestor, which appears in an instantiation, it is
13257 -- global because it is used to denote a specific compilation unit at
13258 -- the time the instantiations will be analyzed.
13260 procedure Reset_Entity (N : Node_Id);
13261 -- Save semantic information on global entity so that it is not resolved
13262 -- again at instantiation time.
13264 procedure Save_Entity_Descendants (N : Node_Id);
13265 -- Apply Save_Global_References to the two syntactic descendants of
13266 -- non-terminal nodes that carry an Associated_Node and are processed
13267 -- through Reset_Entity. Once the global entity (if any) has been
13268 -- captured together with its type, only two syntactic descendants need
13269 -- to be traversed to complete the processing of the tree rooted at N.
13270 -- This applies to Selected_Components, Expanded_Names, and to Operator
13271 -- nodes. N can also be a character literal, identifier, or operator
13272 -- symbol node, but the call has no effect in these cases.
13274 procedure Save_Global_Defaults (N1, N2 : Node_Id);
13275 -- Default actuals in nested instances must be handled specially
13276 -- because there is no link to them from the original tree. When an
13277 -- actual subprogram is given by a default, we add an explicit generic
13278 -- association for it in the instantiation node. When we save the
13279 -- global references on the name of the instance, we recover the list
13280 -- of generic associations, and add an explicit one to the original
13281 -- generic tree, through which a global actual can be preserved.
13282 -- Similarly, if a child unit is instantiated within a sibling, in the
13283 -- context of the parent, we must preserve the identifier of the parent
13284 -- so that it can be properly resolved in a subsequent instantiation.
13286 procedure Save_Global_Descendant (D : Union_Id);
13287 -- Apply Save_Global_References recursively to the descendents of the
13290 procedure Save_References (N : Node_Id);
13291 -- This is the recursive procedure that does the work, once the
13292 -- enclosing generic scope has been established.
13298 function Is_Global (E : Entity_Id) return Boolean is
13301 function Is_Instance_Node (Decl : Node_Id) return Boolean;
13302 -- Determine whether the parent node of a reference to a child unit
13303 -- denotes an instantiation or a formal package, in which case the
13304 -- reference to the child unit is global, even if it appears within
13305 -- the current scope (e.g. when the instance appears within the body
13306 -- of an ancestor).
13308 ----------------------
13309 -- Is_Instance_Node --
13310 ----------------------
13312 function Is_Instance_Node (Decl : Node_Id) return Boolean is
13314 return Nkind (Decl) in N_Generic_Instantiation
13316 Nkind (Original_Node (Decl)) = N_Formal_Package_Declaration;
13317 end Is_Instance_Node;
13319 -- Start of processing for Is_Global
13322 if E = Gen_Scope then
13325 elsif E = Standard_Standard then
13328 elsif Is_Child_Unit (E)
13329 and then (Is_Instance_Node (Parent (N2))
13330 or else (Nkind (Parent (N2)) = N_Expanded_Name
13331 and then N2 = Selector_Name (Parent (N2))
13333 Is_Instance_Node (Parent (Parent (N2)))))
13339 while Se /= Gen_Scope loop
13340 if Se = Standard_Standard then
13355 procedure Reset_Entity (N : Node_Id) is
13357 procedure Set_Global_Type (N : Node_Id; N2 : Node_Id);
13358 -- If the type of N2 is global to the generic unit, save the type in
13359 -- the generic node. Just as we perform name capture for explicit
13360 -- references within the generic, we must capture the global types
13361 -- of local entities because they may participate in resolution in
13364 function Top_Ancestor (E : Entity_Id) return Entity_Id;
13365 -- Find the ultimate ancestor of the current unit. If it is not a
13366 -- generic unit, then the name of the current unit in the prefix of
13367 -- an expanded name must be replaced with its generic homonym to
13368 -- ensure that it will be properly resolved in an instance.
13370 ---------------------
13371 -- Set_Global_Type --
13372 ---------------------
13374 procedure Set_Global_Type (N : Node_Id; N2 : Node_Id) is
13375 Typ : constant Entity_Id := Etype (N2);
13378 Set_Etype (N, Typ);
13380 if Entity (N) /= N2
13381 and then Has_Private_View (Entity (N))
13383 -- If the entity of N is not the associated node, this is a
13384 -- nested generic and it has an associated node as well, whose
13385 -- type is already the full view (see below). Indicate that the
13386 -- original node has a private view.
13388 Set_Has_Private_View (N);
13391 -- If not a private type, nothing else to do
13393 if not Is_Private_Type (Typ) then
13394 if Is_Array_Type (Typ)
13395 and then Is_Private_Type (Component_Type (Typ))
13397 Set_Has_Private_View (N);
13400 -- If it is a derivation of a private type in a context where no
13401 -- full view is needed, nothing to do either.
13403 elsif No (Full_View (Typ)) and then Typ /= Etype (Typ) then
13406 -- Otherwise mark the type for flipping and use the full view when
13410 Set_Has_Private_View (N);
13412 if Present (Full_View (Typ)) then
13413 Set_Etype (N2, Full_View (Typ));
13416 end Set_Global_Type;
13422 function Top_Ancestor (E : Entity_Id) return Entity_Id is
13427 while Is_Child_Unit (Par) loop
13428 Par := Scope (Par);
13434 -- Start of processing for Reset_Entity
13437 N2 := Get_Associated_Node (N);
13440 if Present (E) then
13442 -- If the node is an entry call to an entry in an enclosing task,
13443 -- it is rewritten as a selected component. No global entity to
13444 -- preserve in this case, since the expansion will be redone in
13447 if not Nkind_In (E, N_Defining_Identifier,
13448 N_Defining_Character_Literal,
13449 N_Defining_Operator_Symbol)
13451 Set_Associated_Node (N, Empty);
13452 Set_Etype (N, Empty);
13456 -- If the entity is an itype created as a subtype of an access
13457 -- type with a null exclusion restore source entity for proper
13458 -- visibility. The itype will be created anew in the instance.
13461 and then Ekind (E) = E_Access_Subtype
13462 and then Is_Entity_Name (N)
13463 and then Chars (Etype (E)) = Chars (N)
13466 Set_Entity (N2, E);
13470 if Is_Global (E) then
13472 -- If the entity is a package renaming that is the prefix of
13473 -- an expanded name, it has been rewritten as the renamed
13474 -- package, which is necessary semantically but complicates
13475 -- ASIS tree traversal, so we recover the original entity to
13476 -- expose the renaming. Take into account that the context may
13477 -- be a nested generic, that the original node may itself have
13478 -- an associated node that had better be an entity, and that
13479 -- the current node is still a selected component.
13481 if Ekind (E) = E_Package
13482 and then Nkind (N) = N_Selected_Component
13483 and then Nkind (Parent (N)) = N_Expanded_Name
13484 and then Present (Original_Node (N2))
13485 and then Is_Entity_Name (Original_Node (N2))
13486 and then Present (Entity (Original_Node (N2)))
13488 if Is_Global (Entity (Original_Node (N2))) then
13489 N2 := Original_Node (N2);
13490 Set_Associated_Node (N, N2);
13491 Set_Global_Type (N, N2);
13494 -- Renaming is local, and will be resolved in instance
13496 Set_Associated_Node (N, Empty);
13497 Set_Etype (N, Empty);
13501 Set_Global_Type (N, N2);
13504 elsif Nkind (N) = N_Op_Concat
13505 and then Is_Generic_Type (Etype (N2))
13506 and then (Base_Type (Etype (Right_Opnd (N2))) = Etype (N2)
13508 Base_Type (Etype (Left_Opnd (N2))) = Etype (N2))
13509 and then Is_Intrinsic_Subprogram (E)
13514 -- Entity is local. Mark generic node as unresolved.
13515 -- Note that now it does not have an entity.
13517 Set_Associated_Node (N, Empty);
13518 Set_Etype (N, Empty);
13521 if Nkind (Parent (N)) in N_Generic_Instantiation
13522 and then N = Name (Parent (N))
13524 Save_Global_Defaults (Parent (N), Parent (N2));
13527 elsif Nkind (Parent (N)) = N_Selected_Component
13528 and then Nkind (Parent (N2)) = N_Expanded_Name
13530 if Is_Global (Entity (Parent (N2))) then
13531 Change_Selected_Component_To_Expanded_Name (Parent (N));
13532 Set_Associated_Node (Parent (N), Parent (N2));
13533 Set_Global_Type (Parent (N), Parent (N2));
13534 Save_Entity_Descendants (N);
13536 -- If this is a reference to the current generic entity, replace
13537 -- by the name of the generic homonym of the current package. This
13538 -- is because in an instantiation Par.P.Q will not resolve to the
13539 -- name of the instance, whose enclosing scope is not necessarily
13540 -- Par. We use the generic homonym rather that the name of the
13541 -- generic itself because it may be hidden by a local declaration.
13543 elsif In_Open_Scopes (Entity (Parent (N2)))
13545 Is_Generic_Unit (Top_Ancestor (Entity (Prefix (Parent (N2)))))
13547 if Ekind (Entity (Parent (N2))) = E_Generic_Package then
13548 Rewrite (Parent (N),
13549 Make_Identifier (Sloc (N),
13551 Chars (Generic_Homonym (Entity (Parent (N2))))));
13553 Rewrite (Parent (N),
13554 Make_Identifier (Sloc (N),
13555 Chars => Chars (Selector_Name (Parent (N2)))));
13559 if Nkind (Parent (Parent (N))) in N_Generic_Instantiation
13560 and then Parent (N) = Name (Parent (Parent (N)))
13562 Save_Global_Defaults
13563 (Parent (Parent (N)), Parent (Parent ((N2))));
13566 -- A selected component may denote a static constant that has been
13567 -- folded. If the static constant is global to the generic, capture
13568 -- its value. Otherwise the folding will happen in any instantiation.
13570 elsif Nkind (Parent (N)) = N_Selected_Component
13571 and then Nkind_In (Parent (N2), N_Integer_Literal, N_Real_Literal)
13573 if Present (Entity (Original_Node (Parent (N2))))
13574 and then Is_Global (Entity (Original_Node (Parent (N2))))
13576 Rewrite (Parent (N), New_Copy (Parent (N2)));
13577 Set_Analyzed (Parent (N), False);
13583 -- A selected component may be transformed into a parameterless
13584 -- function call. If the called entity is global, rewrite the node
13585 -- appropriately, i.e. as an extended name for the global entity.
13587 elsif Nkind (Parent (N)) = N_Selected_Component
13588 and then Nkind (Parent (N2)) = N_Function_Call
13589 and then N = Selector_Name (Parent (N))
13591 if No (Parameter_Associations (Parent (N2))) then
13592 if Is_Global (Entity (Name (Parent (N2)))) then
13593 Change_Selected_Component_To_Expanded_Name (Parent (N));
13594 Set_Associated_Node (Parent (N), Name (Parent (N2)));
13595 Set_Global_Type (Parent (N), Name (Parent (N2)));
13596 Save_Entity_Descendants (N);
13599 Set_Is_Prefixed_Call (Parent (N));
13600 Set_Associated_Node (N, Empty);
13601 Set_Etype (N, Empty);
13604 -- In Ada 2005, X.F may be a call to a primitive operation,
13605 -- rewritten as F (X). This rewriting will be done again in an
13606 -- instance, so keep the original node. Global entities will be
13607 -- captured as for other constructs. Indicate that this must
13608 -- resolve as a call, to prevent accidental overloading in the
13609 -- instance, if both a component and a primitive operation appear
13613 Set_Is_Prefixed_Call (Parent (N));
13616 -- Entity is local. Reset in generic unit, so that node is resolved
13617 -- anew at the point of instantiation.
13620 Set_Associated_Node (N, Empty);
13621 Set_Etype (N, Empty);
13625 -----------------------------
13626 -- Save_Entity_Descendants --
13627 -----------------------------
13629 procedure Save_Entity_Descendants (N : Node_Id) is
13632 when N_Binary_Op =>
13633 Save_Global_Descendant (Union_Id (Left_Opnd (N)));
13634 Save_Global_Descendant (Union_Id (Right_Opnd (N)));
13637 Save_Global_Descendant (Union_Id (Right_Opnd (N)));
13639 when N_Expanded_Name | N_Selected_Component =>
13640 Save_Global_Descendant (Union_Id (Prefix (N)));
13641 Save_Global_Descendant (Union_Id (Selector_Name (N)));
13643 when N_Identifier | N_Character_Literal | N_Operator_Symbol =>
13647 raise Program_Error;
13649 end Save_Entity_Descendants;
13651 --------------------------
13652 -- Save_Global_Defaults --
13653 --------------------------
13655 procedure Save_Global_Defaults (N1, N2 : Node_Id) is
13656 Loc : constant Source_Ptr := Sloc (N1);
13657 Assoc2 : constant List_Id := Generic_Associations (N2);
13658 Gen_Id : constant Entity_Id := Get_Generic_Entity (N2);
13665 Actual : Entity_Id;
13668 Assoc1 := Generic_Associations (N1);
13670 if Present (Assoc1) then
13671 Act1 := First (Assoc1);
13674 Set_Generic_Associations (N1, New_List);
13675 Assoc1 := Generic_Associations (N1);
13678 if Present (Assoc2) then
13679 Act2 := First (Assoc2);
13684 while Present (Act1) and then Present (Act2) loop
13689 -- Find the associations added for default subprograms
13691 if Present (Act2) then
13692 while Nkind (Act2) /= N_Generic_Association
13693 or else No (Entity (Selector_Name (Act2)))
13694 or else not Is_Overloadable (Entity (Selector_Name (Act2)))
13699 -- Add a similar association if the default is global. The
13700 -- renaming declaration for the actual has been analyzed, and
13701 -- its alias is the program it renames. Link the actual in the
13702 -- original generic tree with the node in the analyzed tree.
13704 while Present (Act2) loop
13705 Subp := Entity (Selector_Name (Act2));
13706 Def := Explicit_Generic_Actual_Parameter (Act2);
13708 -- Following test is defence against rubbish errors
13710 if No (Alias (Subp)) then
13714 -- Retrieve the resolved actual from the renaming declaration
13715 -- created for the instantiated formal.
13717 Actual := Entity (Name (Parent (Parent (Subp))));
13718 Set_Entity (Def, Actual);
13719 Set_Etype (Def, Etype (Actual));
13721 if Is_Global (Actual) then
13723 Make_Generic_Association (Loc,
13724 Selector_Name => New_Occurrence_Of (Subp, Loc),
13725 Explicit_Generic_Actual_Parameter =>
13726 New_Occurrence_Of (Actual, Loc));
13728 Set_Associated_Node
13729 (Explicit_Generic_Actual_Parameter (Ndec), Def);
13731 Append (Ndec, Assoc1);
13733 -- If there are other defaults, add a dummy association in case
13734 -- there are other defaulted formals with the same name.
13736 elsif Present (Next (Act2)) then
13738 Make_Generic_Association (Loc,
13739 Selector_Name => New_Occurrence_Of (Subp, Loc),
13740 Explicit_Generic_Actual_Parameter => Empty);
13742 Append (Ndec, Assoc1);
13749 if Nkind (Name (N1)) = N_Identifier
13750 and then Is_Child_Unit (Gen_Id)
13751 and then Is_Global (Gen_Id)
13752 and then Is_Generic_Unit (Scope (Gen_Id))
13753 and then In_Open_Scopes (Scope (Gen_Id))
13755 -- This is an instantiation of a child unit within a sibling, so
13756 -- that the generic parent is in scope. An eventual instance must
13757 -- occur within the scope of an instance of the parent. Make name
13758 -- in instance into an expanded name, to preserve the identifier
13759 -- of the parent, so it can be resolved subsequently.
13761 Rewrite (Name (N2),
13762 Make_Expanded_Name (Loc,
13763 Chars => Chars (Gen_Id),
13764 Prefix => New_Occurrence_Of (Scope (Gen_Id), Loc),
13765 Selector_Name => New_Occurrence_Of (Gen_Id, Loc)));
13766 Set_Entity (Name (N2), Gen_Id);
13768 Rewrite (Name (N1),
13769 Make_Expanded_Name (Loc,
13770 Chars => Chars (Gen_Id),
13771 Prefix => New_Occurrence_Of (Scope (Gen_Id), Loc),
13772 Selector_Name => New_Occurrence_Of (Gen_Id, Loc)));
13774 Set_Associated_Node (Name (N1), Name (N2));
13775 Set_Associated_Node (Prefix (Name (N1)), Empty);
13776 Set_Associated_Node
13777 (Selector_Name (Name (N1)), Selector_Name (Name (N2)));
13778 Set_Etype (Name (N1), Etype (Gen_Id));
13781 end Save_Global_Defaults;
13783 ----------------------------
13784 -- Save_Global_Descendant --
13785 ----------------------------
13787 procedure Save_Global_Descendant (D : Union_Id) is
13791 if D in Node_Range then
13792 if D = Union_Id (Empty) then
13795 elsif Nkind (Node_Id (D)) /= N_Compilation_Unit then
13796 Save_References (Node_Id (D));
13799 elsif D in List_Range then
13800 if D = Union_Id (No_List)
13801 or else Is_Empty_List (List_Id (D))
13806 N1 := First (List_Id (D));
13807 while Present (N1) loop
13808 Save_References (N1);
13813 -- Element list or other non-node field, nothing to do
13818 end Save_Global_Descendant;
13820 ---------------------
13821 -- Save_References --
13822 ---------------------
13824 -- This is the recursive procedure that does the work once the enclosing
13825 -- generic scope has been established. We have to treat specially a
13826 -- number of node rewritings that are required by semantic processing
13827 -- and which change the kind of nodes in the generic copy: typically
13828 -- constant-folding, replacing an operator node by a string literal, or
13829 -- a selected component by an expanded name. In each of those cases, the
13830 -- transformation is propagated to the generic unit.
13832 procedure Save_References (N : Node_Id) is
13833 Loc : constant Source_Ptr := Sloc (N);
13839 elsif Nkind_In (N, N_Character_Literal, N_Operator_Symbol) then
13840 if Nkind (N) = Nkind (Get_Associated_Node (N)) then
13843 elsif Nkind (N) = N_Operator_Symbol
13844 and then Nkind (Get_Associated_Node (N)) = N_String_Literal
13846 Change_Operator_Symbol_To_String_Literal (N);
13849 elsif Nkind (N) in N_Op then
13850 if Nkind (N) = Nkind (Get_Associated_Node (N)) then
13851 if Nkind (N) = N_Op_Concat then
13852 Set_Is_Component_Left_Opnd (N,
13853 Is_Component_Left_Opnd (Get_Associated_Node (N)));
13855 Set_Is_Component_Right_Opnd (N,
13856 Is_Component_Right_Opnd (Get_Associated_Node (N)));
13862 -- Node may be transformed into call to a user-defined operator
13864 N2 := Get_Associated_Node (N);
13866 if Nkind (N2) = N_Function_Call then
13867 E := Entity (Name (N2));
13870 and then Is_Global (E)
13872 Set_Etype (N, Etype (N2));
13874 Set_Associated_Node (N, Empty);
13875 Set_Etype (N, Empty);
13878 elsif Nkind_In (N2, N_Integer_Literal,
13882 if Present (Original_Node (N2))
13883 and then Nkind (Original_Node (N2)) = Nkind (N)
13886 -- Operation was constant-folded. Whenever possible,
13887 -- recover semantic information from unfolded node,
13890 Set_Associated_Node (N, Original_Node (N2));
13892 if Nkind (N) = N_Op_Concat then
13893 Set_Is_Component_Left_Opnd (N,
13894 Is_Component_Left_Opnd (Get_Associated_Node (N)));
13895 Set_Is_Component_Right_Opnd (N,
13896 Is_Component_Right_Opnd (Get_Associated_Node (N)));
13902 -- If original node is already modified, propagate
13903 -- constant-folding to template.
13905 Rewrite (N, New_Copy (N2));
13906 Set_Analyzed (N, False);
13909 elsif Nkind (N2) = N_Identifier
13910 and then Ekind (Entity (N2)) = E_Enumeration_Literal
13912 -- Same if call was folded into a literal, but in this case
13913 -- retain the entity to avoid spurious ambiguities if it is
13914 -- overloaded at the point of instantiation or inlining.
13916 Rewrite (N, New_Copy (N2));
13917 Set_Analyzed (N, False);
13921 -- Complete operands check if node has not been constant-folded
13923 if Nkind (N) in N_Op then
13924 Save_Entity_Descendants (N);
13927 elsif Nkind (N) = N_Identifier then
13928 if Nkind (N) = Nkind (Get_Associated_Node (N)) then
13930 -- If this is a discriminant reference, always save it. It is
13931 -- used in the instance to find the corresponding discriminant
13932 -- positionally rather than by name.
13934 Set_Original_Discriminant
13935 (N, Original_Discriminant (Get_Associated_Node (N)));
13939 N2 := Get_Associated_Node (N);
13941 if Nkind (N2) = N_Function_Call then
13942 E := Entity (Name (N2));
13944 -- Name resolves to a call to parameterless function. If
13945 -- original entity is global, mark node as resolved.
13948 and then Is_Global (E)
13950 Set_Etype (N, Etype (N2));
13952 Set_Associated_Node (N, Empty);
13953 Set_Etype (N, Empty);
13956 elsif Nkind_In (N2, N_Integer_Literal, N_Real_Literal)
13957 and then Is_Entity_Name (Original_Node (N2))
13959 -- Name resolves to named number that is constant-folded,
13960 -- We must preserve the original name for ASIS use, and
13961 -- undo the constant-folding, which will be repeated in
13964 Set_Associated_Node (N, Original_Node (N2));
13967 elsif Nkind (N2) = N_String_Literal then
13969 -- Name resolves to string literal. Perform the same
13970 -- replacement in generic.
13972 Rewrite (N, New_Copy (N2));
13974 elsif Nkind (N2) = N_Explicit_Dereference then
13976 -- An identifier is rewritten as a dereference if it is the
13977 -- prefix in an implicit dereference (call or attribute).
13978 -- The analysis of an instantiation will expand the node
13979 -- again, so we preserve the original tree but link it to
13980 -- the resolved entity in case it is global.
13982 if Is_Entity_Name (Prefix (N2))
13983 and then Present (Entity (Prefix (N2)))
13984 and then Is_Global (Entity (Prefix (N2)))
13986 Set_Associated_Node (N, Prefix (N2));
13988 elsif Nkind (Prefix (N2)) = N_Function_Call
13989 and then Is_Global (Entity (Name (Prefix (N2))))
13992 Make_Explicit_Dereference (Loc,
13993 Prefix => Make_Function_Call (Loc,
13995 New_Occurrence_Of (Entity (Name (Prefix (N2))),
13999 Set_Associated_Node (N, Empty);
14000 Set_Etype (N, Empty);
14003 -- The subtype mark of a nominally unconstrained object is
14004 -- rewritten as a subtype indication using the bounds of the
14005 -- expression. Recover the original subtype mark.
14007 elsif Nkind (N2) = N_Subtype_Indication
14008 and then Is_Entity_Name (Original_Node (N2))
14010 Set_Associated_Node (N, Original_Node (N2));
14018 elsif Nkind (N) in N_Entity then
14023 Qual : Node_Id := Empty;
14024 Typ : Entity_Id := Empty;
14027 use Atree.Unchecked_Access;
14028 -- This code section is part of implementing an untyped tree
14029 -- traversal, so it needs direct access to node fields.
14032 if Nkind_In (N, N_Aggregate, N_Extension_Aggregate) then
14033 N2 := Get_Associated_Node (N);
14040 -- In an instance within a generic, use the name of the
14041 -- actual and not the original generic parameter. If the
14042 -- actual is global in the current generic it must be
14043 -- preserved for its instantiation.
14045 if Nkind (Parent (Typ)) = N_Subtype_Declaration
14047 Present (Generic_Parent_Type (Parent (Typ)))
14049 Typ := Base_Type (Typ);
14050 Set_Etype (N2, Typ);
14056 or else not Is_Global (Typ)
14058 Set_Associated_Node (N, Empty);
14060 -- If the aggregate is an actual in a call, it has been
14061 -- resolved in the current context, to some local type.
14062 -- The enclosing call may have been disambiguated by the
14063 -- aggregate, and this disambiguation might fail at
14064 -- instantiation time because the type to which the
14065 -- aggregate did resolve is not preserved. In order to
14066 -- preserve some of this information, we wrap the
14067 -- aggregate in a qualified expression, using the id of
14068 -- its type. For further disambiguation we qualify the
14069 -- type name with its scope (if visible) because both
14070 -- id's will have corresponding entities in an instance.
14071 -- This resolves most of the problems with missing type
14072 -- information on aggregates in instances.
14074 if Nkind (N2) = Nkind (N)
14075 and then Nkind (Parent (N2)) in N_Subprogram_Call
14076 and then Comes_From_Source (Typ)
14078 if Is_Immediately_Visible (Scope (Typ)) then
14079 Nam := Make_Selected_Component (Loc,
14081 Make_Identifier (Loc, Chars (Scope (Typ))),
14083 Make_Identifier (Loc, Chars (Typ)));
14085 Nam := Make_Identifier (Loc, Chars (Typ));
14089 Make_Qualified_Expression (Loc,
14090 Subtype_Mark => Nam,
14091 Expression => Relocate_Node (N));
14095 Save_Global_Descendant (Field1 (N));
14096 Save_Global_Descendant (Field2 (N));
14097 Save_Global_Descendant (Field3 (N));
14098 Save_Global_Descendant (Field5 (N));
14100 if Present (Qual) then
14104 -- All other cases than aggregates
14107 Save_Global_Descendant (Field1 (N));
14108 Save_Global_Descendant (Field2 (N));
14109 Save_Global_Descendant (Field3 (N));
14110 Save_Global_Descendant (Field4 (N));
14111 Save_Global_Descendant (Field5 (N));
14116 -- If a node has aspects, references within their expressions must
14117 -- be saved separately, given they are not directly in the tree.
14119 if Has_Aspects (N) then
14124 Aspect := First (Aspect_Specifications (N));
14125 while Present (Aspect) loop
14126 if Present (Expression (Aspect)) then
14127 Save_Global_References (Expression (Aspect));
14134 end Save_References;
14136 -- Start of processing for Save_Global_References
14139 Gen_Scope := Current_Scope;
14141 -- If the generic unit is a child unit, references to entities in the
14142 -- parent are treated as local, because they will be resolved anew in
14143 -- the context of the instance of the parent.
14145 while Is_Child_Unit (Gen_Scope)
14146 and then Ekind (Scope (Gen_Scope)) = E_Generic_Package
14148 Gen_Scope := Scope (Gen_Scope);
14151 Save_References (N);
14152 end Save_Global_References;
14154 --------------------------------------
14155 -- Set_Copied_Sloc_For_Inlined_Body --
14156 --------------------------------------
14158 procedure Set_Copied_Sloc_For_Inlined_Body (N : Node_Id; E : Entity_Id) is
14160 Create_Instantiation_Source (N, E, True, S_Adjustment);
14161 end Set_Copied_Sloc_For_Inlined_Body;
14163 ---------------------
14164 -- Set_Instance_Of --
14165 ---------------------
14167 procedure Set_Instance_Of (A : Entity_Id; B : Entity_Id) is
14169 Generic_Renamings.Table (Generic_Renamings.Last) := (A, B, Assoc_Null);
14170 Generic_Renamings_HTable.Set (Generic_Renamings.Last);
14171 Generic_Renamings.Increment_Last;
14172 end Set_Instance_Of;
14174 --------------------
14175 -- Set_Next_Assoc --
14176 --------------------
14178 procedure Set_Next_Assoc (E : Assoc_Ptr; Next : Assoc_Ptr) is
14180 Generic_Renamings.Table (E).Next_In_HTable := Next;
14181 end Set_Next_Assoc;
14183 -------------------
14184 -- Start_Generic --
14185 -------------------
14187 procedure Start_Generic is
14189 -- ??? More things could be factored out in this routine.
14190 -- Should probably be done at a later stage.
14192 Generic_Flags.Append (Inside_A_Generic);
14193 Inside_A_Generic := True;
14195 Expander_Mode_Save_And_Set (False);
14198 ----------------------
14199 -- Set_Instance_Env --
14200 ----------------------
14202 procedure Set_Instance_Env
14203 (Gen_Unit : Entity_Id;
14204 Act_Unit : Entity_Id)
14206 Assertion_Status : constant Boolean := Assertions_Enabled;
14207 Save_SPARK_Mode : constant SPARK_Mode_Type := SPARK_Mode;
14208 Save_SPARK_Mode_Pragma : constant Node_Id := SPARK_Mode_Pragma;
14211 -- Regardless of the current mode, predefined units are analyzed in the
14212 -- most current Ada mode, and earlier version Ada checks do not apply
14213 -- to predefined units. Nothing needs to be done for non-internal units.
14214 -- These are always analyzed in the current mode.
14216 if Is_Internal_File_Name
14217 (Fname => Unit_File_Name (Get_Source_Unit (Gen_Unit)),
14218 Renamings_Included => True)
14220 Set_Opt_Config_Switches (True, Current_Sem_Unit = Main_Unit);
14222 -- In Ada2012 we may want to enable assertions in an instance of a
14223 -- predefined unit, in which case we need to preserve the current
14224 -- setting for the Assertions_Enabled flag. This will become more
14225 -- critical when pre/postconditions are added to predefined units,
14226 -- as is already the case for some numeric libraries.
14228 if Ada_Version >= Ada_2012 then
14229 Assertions_Enabled := Assertion_Status;
14232 -- SPARK_Mode for an instance is the one applicable at the point of
14235 SPARK_Mode := Save_SPARK_Mode;
14236 SPARK_Mode_Pragma := Save_SPARK_Mode_Pragma;
14239 Current_Instantiated_Parent :=
14240 (Gen_Id => Gen_Unit,
14241 Act_Id => Act_Unit,
14242 Next_In_HTable => Assoc_Null);
14243 end Set_Instance_Env;
14249 procedure Switch_View (T : Entity_Id) is
14250 BT : constant Entity_Id := Base_Type (T);
14251 Priv_Elmt : Elmt_Id := No_Elmt;
14252 Priv_Sub : Entity_Id;
14255 -- T may be private but its base type may have been exchanged through
14256 -- some other occurrence, in which case there is nothing to switch
14257 -- besides T itself. Note that a private dependent subtype of a private
14258 -- type might not have been switched even if the base type has been,
14259 -- because of the last branch of Check_Private_View (see comment there).
14261 if not Is_Private_Type (BT) then
14262 Prepend_Elmt (Full_View (T), Exchanged_Views);
14263 Exchange_Declarations (T);
14267 Priv_Elmt := First_Elmt (Private_Dependents (BT));
14269 if Present (Full_View (BT)) then
14270 Prepend_Elmt (Full_View (BT), Exchanged_Views);
14271 Exchange_Declarations (BT);
14274 while Present (Priv_Elmt) loop
14275 Priv_Sub := (Node (Priv_Elmt));
14277 -- We avoid flipping the subtype if the Etype of its full view is
14278 -- private because this would result in a malformed subtype. This
14279 -- occurs when the Etype of the subtype full view is the full view of
14280 -- the base type (and since the base types were just switched, the
14281 -- subtype is pointing to the wrong view). This is currently the case
14282 -- for tagged record types, access types (maybe more?) and needs to
14283 -- be resolved. ???
14285 if Present (Full_View (Priv_Sub))
14286 and then not Is_Private_Type (Etype (Full_View (Priv_Sub)))
14288 Prepend_Elmt (Full_View (Priv_Sub), Exchanged_Views);
14289 Exchange_Declarations (Priv_Sub);
14292 Next_Elmt (Priv_Elmt);
14300 function True_Parent (N : Node_Id) return Node_Id is
14302 if Nkind (Parent (N)) = N_Subunit then
14303 return Parent (Corresponding_Stub (Parent (N)));
14309 -----------------------------
14310 -- Valid_Default_Attribute --
14311 -----------------------------
14313 procedure Valid_Default_Attribute (Nam : Entity_Id; Def : Node_Id) is
14314 Attr_Id : constant Attribute_Id :=
14315 Get_Attribute_Id (Attribute_Name (Def));
14316 T : constant Entity_Id := Entity (Prefix (Def));
14317 Is_Fun : constant Boolean := (Ekind (Nam) = E_Function);
14330 F := First_Formal (Nam);
14331 while Present (F) loop
14332 Num_F := Num_F + 1;
14337 when Attribute_Adjacent | Attribute_Ceiling | Attribute_Copy_Sign |
14338 Attribute_Floor | Attribute_Fraction | Attribute_Machine |
14339 Attribute_Model | Attribute_Remainder | Attribute_Rounding |
14340 Attribute_Unbiased_Rounding =>
14343 and then Is_Floating_Point_Type (T);
14345 when Attribute_Image | Attribute_Pred | Attribute_Succ |
14346 Attribute_Value | Attribute_Wide_Image |
14347 Attribute_Wide_Value =>
14348 OK := (Is_Fun and then Num_F = 1 and then Is_Scalar_Type (T));
14350 when Attribute_Max | Attribute_Min =>
14351 OK := (Is_Fun and then Num_F = 2 and then Is_Scalar_Type (T));
14353 when Attribute_Input =>
14354 OK := (Is_Fun and then Num_F = 1);
14356 when Attribute_Output | Attribute_Read | Attribute_Write =>
14357 OK := (not Is_Fun and then Num_F = 2);
14364 Error_Msg_N ("attribute reference has wrong profile for subprogram",
14367 end Valid_Default_Attribute;