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);
1141 Make_Expression_Function (Loc,
1142 Specification => Spec,
1143 Expression => Expr);
1148 ----------------------------------------
1149 -- Check_Overloaded_Formal_Subprogram --
1150 ----------------------------------------
1152 procedure Check_Overloaded_Formal_Subprogram (Formal : Entity_Id) is
1153 Temp_Formal : Entity_Id;
1156 Temp_Formal := First (Formals);
1157 while Present (Temp_Formal) loop
1158 if Nkind (Temp_Formal) in N_Formal_Subprogram_Declaration
1159 and then Temp_Formal /= Formal
1161 Chars (Defining_Unit_Name (Specification (Formal))) =
1162 Chars (Defining_Unit_Name (Specification (Temp_Formal)))
1164 if Present (Found_Assoc) then
1166 ("named association not allowed for overloaded formal",
1171 ("named association not allowed for overloaded formal",
1175 Abandon_Instantiation (Instantiation_Node);
1180 end Check_Overloaded_Formal_Subprogram;
1182 -------------------------------
1183 -- Has_Fully_Defined_Profile --
1184 -------------------------------
1186 function Has_Fully_Defined_Profile (Subp : Entity_Id) return Boolean is
1187 function Is_Fully_Defined_Type (Typ : Entity_Id) return Boolean;
1188 -- Determine whethet type Typ is fully defined
1190 ---------------------------
1191 -- Is_Fully_Defined_Type --
1192 ---------------------------
1194 function Is_Fully_Defined_Type (Typ : Entity_Id) return Boolean is
1196 -- A private type without a full view is not fully defined
1198 if Is_Private_Type (Typ)
1199 and then No (Full_View (Typ))
1203 -- An incomplete type is never fully defined
1205 elsif Is_Incomplete_Type (Typ) then
1208 -- All other types are fully defined
1213 end Is_Fully_Defined_Type;
1215 -- Local declarations
1219 -- Start of processing for Has_Fully_Defined_Profile
1222 -- Check the parameters
1224 Param := First_Formal (Subp);
1225 while Present (Param) loop
1226 if not Is_Fully_Defined_Type (Etype (Param)) then
1230 Next_Formal (Param);
1233 -- Check the return type
1235 return Is_Fully_Defined_Type (Etype (Subp));
1236 end Has_Fully_Defined_Profile;
1238 ---------------------
1239 -- Matching_Actual --
1240 ---------------------
1242 function Matching_Actual
1244 A_F : Entity_Id) return Node_Id
1250 Is_Named_Assoc := False;
1252 -- End of list of purely positional parameters
1254 if No (Actual) or else Nkind (Actual) = N_Others_Choice then
1255 Found_Assoc := Empty;
1258 -- Case of positional parameter corresponding to current formal
1260 elsif No (Selector_Name (Actual)) then
1261 Found_Assoc := Actual;
1262 Act := Explicit_Generic_Actual_Parameter (Actual);
1263 Num_Matched := Num_Matched + 1;
1266 -- Otherwise scan list of named actuals to find the one with the
1267 -- desired name. All remaining actuals have explicit names.
1270 Is_Named_Assoc := True;
1271 Found_Assoc := Empty;
1275 while Present (Actual) loop
1276 if Chars (Selector_Name (Actual)) = Chars (F) then
1277 Set_Entity (Selector_Name (Actual), A_F);
1278 Set_Etype (Selector_Name (Actual), Etype (A_F));
1279 Generate_Reference (A_F, Selector_Name (Actual));
1280 Found_Assoc := Actual;
1281 Act := Explicit_Generic_Actual_Parameter (Actual);
1282 Num_Matched := Num_Matched + 1;
1290 -- Reset for subsequent searches. In most cases the named
1291 -- associations are in order. If they are not, we reorder them
1292 -- to avoid scanning twice the same actual. This is not just a
1293 -- question of efficiency: there may be multiple defaults with
1294 -- boxes that have the same name. In a nested instantiation we
1295 -- insert actuals for those defaults, and cannot rely on their
1296 -- names to disambiguate them.
1298 if Actual = First_Named then
1301 elsif Present (Actual) then
1302 Insert_Before (First_Named, Remove_Next (Prev));
1305 Actual := First_Named;
1308 if Is_Entity_Name (Act) and then Present (Entity (Act)) then
1309 Set_Used_As_Generic_Actual (Entity (Act));
1313 end Matching_Actual;
1315 ------------------------------
1316 -- Partial_Parameterization --
1317 ------------------------------
1319 function Partial_Parameterization return Boolean is
1321 return Others_Present
1322 or else (Present (Found_Assoc) and then Box_Present (Found_Assoc));
1323 end Partial_Parameterization;
1325 ---------------------
1326 -- Process_Default --
1327 ---------------------
1329 procedure Process_Default (F : Entity_Id) is
1330 Loc : constant Source_Ptr := Sloc (I_Node);
1331 F_Id : constant Entity_Id := Defining_Entity (F);
1337 -- Append copy of formal declaration to associations, and create new
1338 -- defining identifier for it.
1340 Decl := New_Copy_Tree (F);
1341 Id := Make_Defining_Identifier (Sloc (F_Id), Chars (F_Id));
1343 if Nkind (F) in N_Formal_Subprogram_Declaration then
1344 Set_Defining_Unit_Name (Specification (Decl), Id);
1347 Set_Defining_Identifier (Decl, Id);
1350 Append (Decl, Assoc);
1352 if No (Found_Assoc) then
1354 Make_Generic_Association (Loc,
1355 Selector_Name => New_Occurrence_Of (Id, Loc),
1356 Explicit_Generic_Actual_Parameter => Empty);
1357 Set_Box_Present (Default);
1358 Append (Default, Default_Formals);
1360 end Process_Default;
1362 ---------------------------------
1363 -- Renames_Standard_Subprogram --
1364 ---------------------------------
1366 function Renames_Standard_Subprogram (Subp : Entity_Id) return Boolean is
1371 while Present (Id) loop
1372 if Scope (Id) = Standard_Standard then
1380 end Renames_Standard_Subprogram;
1382 -------------------------
1383 -- Set_Analyzed_Formal --
1384 -------------------------
1386 procedure Set_Analyzed_Formal is
1390 while Present (Analyzed_Formal) loop
1391 Kind := Nkind (Analyzed_Formal);
1393 case Nkind (Formal) is
1395 when N_Formal_Subprogram_Declaration =>
1396 exit when Kind in N_Formal_Subprogram_Declaration
1399 (Defining_Unit_Name (Specification (Formal))) =
1401 (Defining_Unit_Name (Specification (Analyzed_Formal)));
1403 when N_Formal_Package_Declaration =>
1404 exit when Nkind_In (Kind, N_Formal_Package_Declaration,
1405 N_Generic_Package_Declaration,
1406 N_Package_Declaration);
1408 when N_Use_Package_Clause | N_Use_Type_Clause => exit;
1412 -- Skip freeze nodes, and nodes inserted to replace
1413 -- unrecognized pragmas.
1416 Kind not in N_Formal_Subprogram_Declaration
1417 and then not Nkind_In (Kind, N_Subprogram_Declaration,
1421 and then Chars (Defining_Identifier (Formal)) =
1422 Chars (Defining_Identifier (Analyzed_Formal));
1425 Next (Analyzed_Formal);
1427 end Set_Analyzed_Formal;
1429 -- Start of processing for Analyze_Associations
1432 Actuals := Generic_Associations (I_Node);
1434 if Present (Actuals) then
1436 -- Check for an Others choice, indicating a partial parameterization
1437 -- for a formal package.
1439 Actual := First (Actuals);
1440 while Present (Actual) loop
1441 if Nkind (Actual) = N_Others_Choice then
1442 Others_Present := True;
1443 Others_Choice := Actual;
1445 if Present (Next (Actual)) then
1446 Error_Msg_N ("others must be last association", Actual);
1449 -- This subprogram is used both for formal packages and for
1450 -- instantiations. For the latter, associations must all be
1453 if Nkind (I_Node) /= N_Formal_Package_Declaration
1454 and then Comes_From_Source (I_Node)
1457 ("others association not allowed in an instance",
1461 -- In any case, nothing to do after the others association
1465 elsif Box_Present (Actual)
1466 and then Comes_From_Source (I_Node)
1467 and then Nkind (I_Node) /= N_Formal_Package_Declaration
1470 ("box association not allowed in an instance", Actual);
1476 -- If named associations are present, save first named association
1477 -- (it may of course be Empty) to facilitate subsequent name search.
1479 First_Named := First (Actuals);
1480 while Present (First_Named)
1481 and then Nkind (First_Named) /= N_Others_Choice
1482 and then No (Selector_Name (First_Named))
1484 Num_Actuals := Num_Actuals + 1;
1489 Named := First_Named;
1490 while Present (Named) loop
1491 if Nkind (Named) /= N_Others_Choice
1492 and then No (Selector_Name (Named))
1494 Error_Msg_N ("invalid positional actual after named one", Named);
1495 Abandon_Instantiation (Named);
1498 -- A named association may lack an actual parameter, if it was
1499 -- introduced for a default subprogram that turns out to be local
1500 -- to the outer instantiation.
1502 if Nkind (Named) /= N_Others_Choice
1503 and then Present (Explicit_Generic_Actual_Parameter (Named))
1505 Num_Actuals := Num_Actuals + 1;
1511 if Present (Formals) then
1512 Formal := First_Non_Pragma (Formals);
1513 Analyzed_Formal := First_Non_Pragma (F_Copy);
1515 if Present (Actuals) then
1516 Actual := First (Actuals);
1518 -- All formals should have default values
1524 while Present (Formal) loop
1525 Set_Analyzed_Formal;
1526 Saved_Formal := Next_Non_Pragma (Formal);
1528 case Nkind (Formal) is
1529 when N_Formal_Object_Declaration =>
1532 Defining_Identifier (Formal),
1533 Defining_Identifier (Analyzed_Formal));
1535 if No (Match) and then Partial_Parameterization then
1536 Process_Default (Formal);
1539 (Instantiate_Object (Formal, Match, Analyzed_Formal),
1543 when N_Formal_Type_Declaration =>
1546 Defining_Identifier (Formal),
1547 Defining_Identifier (Analyzed_Formal));
1550 if Partial_Parameterization then
1551 Process_Default (Formal);
1554 Error_Msg_Sloc := Sloc (Gen_Unit);
1558 Defining_Identifier (Formal));
1559 Error_Msg_NE ("\in instantiation of & declared#",
1560 Instantiation_Node, Gen_Unit);
1561 Abandon_Instantiation (Instantiation_Node);
1568 (Formal, Match, Analyzed_Formal, Assoc),
1571 -- An instantiation is a freeze point for the actuals,
1572 -- unless this is a rewritten formal package, or the
1573 -- formal is an Ada 2012 formal incomplete type.
1575 if Nkind (I_Node) = N_Formal_Package_Declaration
1577 (Ada_Version >= Ada_2012
1579 Ekind (Defining_Identifier (Analyzed_Formal)) =
1585 Append_Elmt (Entity (Match), Actuals_To_Freeze);
1589 -- A remote access-to-class-wide type is not a legal actual
1590 -- for a generic formal of an access type (E.2.2(17/2)).
1591 -- In GNAT an exception to this rule is introduced when
1592 -- the formal is marked as remote using implementation
1593 -- defined aspect/pragma Remote_Access_Type. In that case
1594 -- the actual must be remote as well.
1596 -- If the current instantiation is the construction of a
1597 -- local copy for a formal package the actuals may be
1598 -- defaulted, and there is no matching actual to check.
1600 if Nkind (Analyzed_Formal) = N_Formal_Type_Declaration
1602 Nkind (Formal_Type_Definition (Analyzed_Formal)) =
1603 N_Access_To_Object_Definition
1604 and then Present (Match)
1607 Formal_Ent : constant Entity_Id :=
1608 Defining_Identifier (Analyzed_Formal);
1610 if Is_Remote_Access_To_Class_Wide_Type (Entity (Match))
1611 = Is_Remote_Types (Formal_Ent)
1613 -- Remoteness of formal and actual match
1617 elsif Is_Remote_Types (Formal_Ent) then
1619 -- Remote formal, non-remote actual
1622 ("actual for& must be remote", Match, Formal_Ent);
1625 -- Non-remote formal, remote actual
1628 ("actual for& may not be remote",
1634 when N_Formal_Subprogram_Declaration =>
1637 (Defining_Unit_Name (Specification (Formal)),
1638 Defining_Unit_Name (Specification (Analyzed_Formal)));
1640 -- If the formal subprogram has the same name as another
1641 -- formal subprogram of the generic, then a named
1642 -- association is illegal (12.3(9)). Exclude named
1643 -- associations that are generated for a nested instance.
1646 and then Is_Named_Assoc
1647 and then Comes_From_Source (Found_Assoc)
1649 Check_Overloaded_Formal_Subprogram (Formal);
1652 -- If there is no corresponding actual, this may be case
1653 -- of partial parameterization, or else the formal has a
1654 -- default or a box.
1656 if No (Match) and then Partial_Parameterization then
1657 Process_Default (Formal);
1659 if Nkind (I_Node) = N_Formal_Package_Declaration then
1660 Check_Overloaded_Formal_Subprogram (Formal);
1665 and then Ekind (Defining_Entity (Analyzed_Formal))
1669 -- If actual is an entity (function or operator),
1670 -- build wrapper for it.
1672 if Present (Match) and then Is_Entity_Name (Match) then
1675 (Defining_Entity (Analyzed_Formal), Match));
1677 -- Ditto if formal is an operator with a default.
1679 elsif Box_Present (Formal)
1680 and then Nkind (Defining_Entity (Analyzed_Formal))
1681 = N_Defining_Operator_Symbol
1686 (Defining_Entity (Analyzed_Formal)));
1688 -- Otherwise create renaming declaration.
1692 Instantiate_Formal_Subprogram
1693 (Formal, Match, Analyzed_Formal));
1698 Instantiate_Formal_Subprogram
1699 (Formal, Match, Analyzed_Formal));
1702 -- An instantiation is a freeze point for the actuals,
1703 -- unless this is a rewritten formal package.
1705 if Nkind (I_Node) /= N_Formal_Package_Declaration
1706 and then Nkind (Match) = N_Identifier
1707 and then Is_Subprogram (Entity (Match))
1709 -- The actual subprogram may rename a routine defined
1710 -- in Standard. Avoid freezing such renamings because
1711 -- subprograms coming from Standard cannot be frozen.
1714 not Renames_Standard_Subprogram (Entity (Match))
1716 -- If the actual subprogram comes from a different
1717 -- unit, it is already frozen, either by a body in
1718 -- that unit or by the end of the declarative part
1719 -- of the unit. This check avoids the freezing of
1720 -- subprograms defined in Standard which are used
1721 -- as generic actuals.
1723 and then In_Same_Code_Unit (Entity (Match), I_Node)
1724 and then Has_Fully_Defined_Profile (Entity (Match))
1726 -- Mark the subprogram as having a delayed freeze
1727 -- since this may be an out-of-order action.
1729 Set_Has_Delayed_Freeze (Entity (Match));
1730 Append_Elmt (Entity (Match), Actuals_To_Freeze);
1734 -- If this is a nested generic, preserve default for later
1738 and then Box_Present (Formal)
1741 (Defining_Unit_Name (Specification (Last (Assoc))),
1745 when N_Formal_Package_Declaration =>
1748 Defining_Identifier (Formal),
1749 Defining_Identifier (Original_Node (Analyzed_Formal)));
1752 if Partial_Parameterization then
1753 Process_Default (Formal);
1756 Error_Msg_Sloc := Sloc (Gen_Unit);
1759 Instantiation_Node, Defining_Identifier (Formal));
1760 Error_Msg_NE ("\in instantiation of & declared#",
1761 Instantiation_Node, Gen_Unit);
1763 Abandon_Instantiation (Instantiation_Node);
1769 (Instantiate_Formal_Package
1770 (Formal, Match, Analyzed_Formal),
1774 -- For use type and use package appearing in the generic part,
1775 -- we have already copied them, so we can just move them where
1776 -- they belong (we mustn't recopy them since this would mess up
1777 -- the Sloc values).
1779 when N_Use_Package_Clause |
1780 N_Use_Type_Clause =>
1781 if Nkind (Original_Node (I_Node)) =
1782 N_Formal_Package_Declaration
1784 Append (New_Copy_Tree (Formal), Assoc);
1787 Append (Formal, Assoc);
1791 raise Program_Error;
1795 Formal := Saved_Formal;
1796 Next_Non_Pragma (Analyzed_Formal);
1799 if Num_Actuals > Num_Matched then
1800 Error_Msg_Sloc := Sloc (Gen_Unit);
1802 if Present (Selector_Name (Actual)) then
1804 ("unmatched actual&",
1805 Actual, Selector_Name (Actual));
1806 Error_Msg_NE ("\in instantiation of& declared#",
1810 ("unmatched actual in instantiation of& declared#",
1815 elsif Present (Actuals) then
1817 ("too many actuals in generic instantiation", Instantiation_Node);
1820 -- An instantiation freezes all generic actuals. The only exceptions
1821 -- to this are incomplete types and subprograms which are not fully
1822 -- defined at the point of instantiation.
1825 Elmt : Elmt_Id := First_Elmt (Actuals_To_Freeze);
1827 while Present (Elmt) loop
1828 Freeze_Before (I_Node, Node (Elmt));
1833 -- If there are default subprograms, normalize the tree by adding
1834 -- explicit associations for them. This is required if the instance
1835 -- appears within a generic.
1843 Elmt := First_Elmt (Default_Actuals);
1844 while Present (Elmt) loop
1845 if No (Actuals) then
1846 Actuals := New_List;
1847 Set_Generic_Associations (I_Node, Actuals);
1850 Subp := Node (Elmt);
1852 Make_Generic_Association (Sloc (Subp),
1853 Selector_Name => New_Occurrence_Of (Subp, Sloc (Subp)),
1854 Explicit_Generic_Actual_Parameter =>
1855 New_Occurrence_Of (Subp, Sloc (Subp)));
1856 Mark_Rewrite_Insertion (New_D);
1857 Append_To (Actuals, New_D);
1862 -- If this is a formal package, normalize the parameter list by adding
1863 -- explicit box associations for the formals that are covered by an
1866 if not Is_Empty_List (Default_Formals) then
1867 Append_List (Default_Formals, Formals);
1871 end Analyze_Associations;
1873 -------------------------------
1874 -- Analyze_Formal_Array_Type --
1875 -------------------------------
1877 procedure Analyze_Formal_Array_Type
1878 (T : in out Entity_Id;
1884 -- Treated like a non-generic array declaration, with additional
1889 if Nkind (Def) = N_Constrained_Array_Definition then
1890 DSS := First (Discrete_Subtype_Definitions (Def));
1891 while Present (DSS) loop
1892 if Nkind_In (DSS, N_Subtype_Indication,
1894 N_Attribute_Reference)
1896 Error_Msg_N ("only a subtype mark is allowed in a formal", DSS);
1903 Array_Type_Declaration (T, Def);
1904 Set_Is_Generic_Type (Base_Type (T));
1906 if Ekind (Component_Type (T)) = E_Incomplete_Type
1907 and then No (Full_View (Component_Type (T)))
1909 Error_Msg_N ("premature usage of incomplete type", Def);
1911 -- Check that range constraint is not allowed on the component type
1912 -- of a generic formal array type (AARM 12.5.3(3))
1914 elsif Is_Internal (Component_Type (T))
1915 and then Present (Subtype_Indication (Component_Definition (Def)))
1916 and then Nkind (Original_Node
1917 (Subtype_Indication (Component_Definition (Def)))) =
1918 N_Subtype_Indication
1921 ("in a formal, a subtype indication can only be "
1922 & "a subtype mark (RM 12.5.3(3))",
1923 Subtype_Indication (Component_Definition (Def)));
1926 end Analyze_Formal_Array_Type;
1928 ---------------------------------------------
1929 -- Analyze_Formal_Decimal_Fixed_Point_Type --
1930 ---------------------------------------------
1932 -- As for other generic types, we create a valid type representation with
1933 -- legal but arbitrary attributes, whose values are never considered
1934 -- static. For all scalar types we introduce an anonymous base type, with
1935 -- the same attributes. We choose the corresponding integer type to be
1936 -- Standard_Integer.
1937 -- Here and in other similar routines, the Sloc of the generated internal
1938 -- type must be the same as the sloc of the defining identifier of the
1939 -- formal type declaration, to provide proper source navigation.
1941 procedure Analyze_Formal_Decimal_Fixed_Point_Type
1945 Loc : constant Source_Ptr := Sloc (Def);
1947 Base : constant Entity_Id :=
1949 (E_Decimal_Fixed_Point_Type,
1951 Sloc (Defining_Identifier (Parent (Def))), 'G');
1953 Int_Base : constant Entity_Id := Standard_Integer;
1954 Delta_Val : constant Ureal := Ureal_1;
1955 Digs_Val : constant Uint := Uint_6;
1957 function Make_Dummy_Bound return Node_Id;
1958 -- Return a properly typed universal real literal to use as a bound
1960 ----------------------
1961 -- Make_Dummy_Bound --
1962 ----------------------
1964 function Make_Dummy_Bound return Node_Id is
1965 Bound : constant Node_Id := Make_Real_Literal (Loc, Ureal_1);
1967 Set_Etype (Bound, Universal_Real);
1969 end Make_Dummy_Bound;
1971 -- Start of processing for Analyze_Formal_Decimal_Fixed_Point_Type
1976 Set_Etype (Base, Base);
1977 Set_Size_Info (Base, Int_Base);
1978 Set_RM_Size (Base, RM_Size (Int_Base));
1979 Set_First_Rep_Item (Base, First_Rep_Item (Int_Base));
1980 Set_Digits_Value (Base, Digs_Val);
1981 Set_Delta_Value (Base, Delta_Val);
1982 Set_Small_Value (Base, Delta_Val);
1983 Set_Scalar_Range (Base,
1985 Low_Bound => Make_Dummy_Bound,
1986 High_Bound => Make_Dummy_Bound));
1988 Set_Is_Generic_Type (Base);
1989 Set_Parent (Base, Parent (Def));
1991 Set_Ekind (T, E_Decimal_Fixed_Point_Subtype);
1992 Set_Etype (T, Base);
1993 Set_Size_Info (T, Int_Base);
1994 Set_RM_Size (T, RM_Size (Int_Base));
1995 Set_First_Rep_Item (T, First_Rep_Item (Int_Base));
1996 Set_Digits_Value (T, Digs_Val);
1997 Set_Delta_Value (T, Delta_Val);
1998 Set_Small_Value (T, Delta_Val);
1999 Set_Scalar_Range (T, Scalar_Range (Base));
2000 Set_Is_Constrained (T);
2002 Check_Restriction (No_Fixed_Point, Def);
2003 end Analyze_Formal_Decimal_Fixed_Point_Type;
2005 -------------------------------------------
2006 -- Analyze_Formal_Derived_Interface_Type --
2007 -------------------------------------------
2009 procedure Analyze_Formal_Derived_Interface_Type
2014 Loc : constant Source_Ptr := Sloc (Def);
2017 -- Rewrite as a type declaration of a derived type. This ensures that
2018 -- the interface list and primitive operations are properly captured.
2021 Make_Full_Type_Declaration (Loc,
2022 Defining_Identifier => T,
2023 Type_Definition => Def));
2025 Set_Is_Generic_Type (T);
2026 end Analyze_Formal_Derived_Interface_Type;
2028 ---------------------------------
2029 -- Analyze_Formal_Derived_Type --
2030 ---------------------------------
2032 procedure Analyze_Formal_Derived_Type
2037 Loc : constant Source_Ptr := Sloc (Def);
2038 Unk_Disc : constant Boolean := Unknown_Discriminants_Present (N);
2042 Set_Is_Generic_Type (T);
2044 if Private_Present (Def) then
2046 Make_Private_Extension_Declaration (Loc,
2047 Defining_Identifier => T,
2048 Discriminant_Specifications => Discriminant_Specifications (N),
2049 Unknown_Discriminants_Present => Unk_Disc,
2050 Subtype_Indication => Subtype_Mark (Def),
2051 Interface_List => Interface_List (Def));
2053 Set_Abstract_Present (New_N, Abstract_Present (Def));
2054 Set_Limited_Present (New_N, Limited_Present (Def));
2055 Set_Synchronized_Present (New_N, Synchronized_Present (Def));
2059 Make_Full_Type_Declaration (Loc,
2060 Defining_Identifier => T,
2061 Discriminant_Specifications =>
2062 Discriminant_Specifications (Parent (T)),
2064 Make_Derived_Type_Definition (Loc,
2065 Subtype_Indication => Subtype_Mark (Def)));
2067 Set_Abstract_Present
2068 (Type_Definition (New_N), Abstract_Present (Def));
2070 (Type_Definition (New_N), Limited_Present (Def));
2077 if not Is_Composite_Type (T) then
2079 ("unknown discriminants not allowed for elementary types", N);
2081 Set_Has_Unknown_Discriminants (T);
2082 Set_Is_Constrained (T, False);
2086 -- If the parent type has a known size, so does the formal, which makes
2087 -- legal representation clauses that involve the formal.
2089 Set_Size_Known_At_Compile_Time
2090 (T, Size_Known_At_Compile_Time (Entity (Subtype_Mark (Def))));
2091 end Analyze_Formal_Derived_Type;
2093 ----------------------------------
2094 -- Analyze_Formal_Discrete_Type --
2095 ----------------------------------
2097 -- The operations defined for a discrete types are those of an enumeration
2098 -- type. The size is set to an arbitrary value, for use in analyzing the
2101 procedure Analyze_Formal_Discrete_Type (T : Entity_Id; Def : Node_Id) is
2102 Loc : constant Source_Ptr := Sloc (Def);
2106 Base : constant Entity_Id :=
2108 (E_Floating_Point_Type, Current_Scope,
2109 Sloc (Defining_Identifier (Parent (Def))), 'G');
2113 Set_Ekind (T, E_Enumeration_Subtype);
2114 Set_Etype (T, Base);
2117 Set_Is_Generic_Type (T);
2118 Set_Is_Constrained (T);
2120 -- For semantic analysis, the bounds of the type must be set to some
2121 -- non-static value. The simplest is to create attribute nodes for those
2122 -- bounds, that refer to the type itself. These bounds are never
2123 -- analyzed but serve as place-holders.
2126 Make_Attribute_Reference (Loc,
2127 Attribute_Name => Name_First,
2128 Prefix => New_Occurrence_Of (T, Loc));
2132 Make_Attribute_Reference (Loc,
2133 Attribute_Name => Name_Last,
2134 Prefix => New_Occurrence_Of (T, Loc));
2137 Set_Scalar_Range (T,
2142 Set_Ekind (Base, E_Enumeration_Type);
2143 Set_Etype (Base, Base);
2144 Init_Size (Base, 8);
2145 Init_Alignment (Base);
2146 Set_Is_Generic_Type (Base);
2147 Set_Scalar_Range (Base, Scalar_Range (T));
2148 Set_Parent (Base, Parent (Def));
2149 end Analyze_Formal_Discrete_Type;
2151 ----------------------------------
2152 -- Analyze_Formal_Floating_Type --
2153 ---------------------------------
2155 procedure Analyze_Formal_Floating_Type (T : Entity_Id; Def : Node_Id) is
2156 Base : constant Entity_Id :=
2158 (E_Floating_Point_Type, Current_Scope,
2159 Sloc (Defining_Identifier (Parent (Def))), 'G');
2162 -- The various semantic attributes are taken from the predefined type
2163 -- Float, just so that all of them are initialized. Their values are
2164 -- never used because no constant folding or expansion takes place in
2165 -- the generic itself.
2168 Set_Ekind (T, E_Floating_Point_Subtype);
2169 Set_Etype (T, Base);
2170 Set_Size_Info (T, (Standard_Float));
2171 Set_RM_Size (T, RM_Size (Standard_Float));
2172 Set_Digits_Value (T, Digits_Value (Standard_Float));
2173 Set_Scalar_Range (T, Scalar_Range (Standard_Float));
2174 Set_Is_Constrained (T);
2176 Set_Is_Generic_Type (Base);
2177 Set_Etype (Base, Base);
2178 Set_Size_Info (Base, (Standard_Float));
2179 Set_RM_Size (Base, RM_Size (Standard_Float));
2180 Set_Digits_Value (Base, Digits_Value (Standard_Float));
2181 Set_Scalar_Range (Base, Scalar_Range (Standard_Float));
2182 Set_Parent (Base, Parent (Def));
2184 Check_Restriction (No_Floating_Point, Def);
2185 end Analyze_Formal_Floating_Type;
2187 -----------------------------------
2188 -- Analyze_Formal_Interface_Type;--
2189 -----------------------------------
2191 procedure Analyze_Formal_Interface_Type
2196 Loc : constant Source_Ptr := Sloc (N);
2201 Make_Full_Type_Declaration (Loc,
2202 Defining_Identifier => T,
2203 Type_Definition => Def);
2207 Set_Is_Generic_Type (T);
2208 end Analyze_Formal_Interface_Type;
2210 ---------------------------------
2211 -- Analyze_Formal_Modular_Type --
2212 ---------------------------------
2214 procedure Analyze_Formal_Modular_Type (T : Entity_Id; Def : Node_Id) is
2216 -- Apart from their entity kind, generic modular types are treated like
2217 -- signed integer types, and have the same attributes.
2219 Analyze_Formal_Signed_Integer_Type (T, Def);
2220 Set_Ekind (T, E_Modular_Integer_Subtype);
2221 Set_Ekind (Etype (T), E_Modular_Integer_Type);
2223 end Analyze_Formal_Modular_Type;
2225 ---------------------------------------
2226 -- Analyze_Formal_Object_Declaration --
2227 ---------------------------------------
2229 procedure Analyze_Formal_Object_Declaration (N : Node_Id) is
2230 E : constant Node_Id := Default_Expression (N);
2231 Id : constant Node_Id := Defining_Identifier (N);
2238 -- Determine the mode of the formal object
2240 if Out_Present (N) then
2241 K := E_Generic_In_Out_Parameter;
2243 if not In_Present (N) then
2244 Error_Msg_N ("formal generic objects cannot have mode OUT", N);
2248 K := E_Generic_In_Parameter;
2251 if Present (Subtype_Mark (N)) then
2252 Find_Type (Subtype_Mark (N));
2253 T := Entity (Subtype_Mark (N));
2255 -- Verify that there is no redundant null exclusion
2257 if Null_Exclusion_Present (N) then
2258 if not Is_Access_Type (T) then
2260 ("null exclusion can only apply to an access type", N);
2262 elsif Can_Never_Be_Null (T) then
2264 ("`NOT NULL` not allowed (& already excludes null)",
2269 -- Ada 2005 (AI-423): Formal object with an access definition
2272 Check_Access_Definition (N);
2273 T := Access_Definition
2275 N => Access_Definition (N));
2278 if Ekind (T) = E_Incomplete_Type then
2280 Error_Node : Node_Id;
2283 if Present (Subtype_Mark (N)) then
2284 Error_Node := Subtype_Mark (N);
2286 Check_Access_Definition (N);
2287 Error_Node := Access_Definition (N);
2290 Error_Msg_N ("premature usage of incomplete type", Error_Node);
2294 if K = E_Generic_In_Parameter then
2296 -- Ada 2005 (AI-287): Limited aggregates allowed in generic formals
2298 if Ada_Version < Ada_2005 and then Is_Limited_Type (T) then
2300 ("generic formal of mode IN must not be of limited type", N);
2301 Explain_Limited_Type (T, N);
2304 if Is_Abstract_Type (T) then
2306 ("generic formal of mode IN must not be of abstract type", N);
2310 Preanalyze_Spec_Expression (E, T);
2312 if Is_Limited_Type (T) and then not OK_For_Limited_Init (T, E) then
2314 ("initialization not allowed for limited types", E);
2315 Explain_Limited_Type (T, E);
2322 -- Case of generic IN OUT parameter
2325 -- If the formal has an unconstrained type, construct its actual
2326 -- subtype, as is done for subprogram formals. In this fashion, all
2327 -- its uses can refer to specific bounds.
2332 if (Is_Array_Type (T)
2333 and then not Is_Constrained (T))
2335 (Ekind (T) = E_Record_Type
2336 and then Has_Discriminants (T))
2339 Non_Freezing_Ref : constant Node_Id :=
2340 New_Occurrence_Of (Id, Sloc (Id));
2344 -- Make sure the actual subtype doesn't generate bogus freezing
2346 Set_Must_Not_Freeze (Non_Freezing_Ref);
2347 Decl := Build_Actual_Subtype (T, Non_Freezing_Ref);
2348 Insert_Before_And_Analyze (N, Decl);
2349 Set_Actual_Subtype (Id, Defining_Identifier (Decl));
2352 Set_Actual_Subtype (Id, T);
2357 ("initialization not allowed for `IN OUT` formals", N);
2361 if Has_Aspects (N) then
2362 Analyze_Aspect_Specifications (N, Id);
2364 end Analyze_Formal_Object_Declaration;
2366 ----------------------------------------------
2367 -- Analyze_Formal_Ordinary_Fixed_Point_Type --
2368 ----------------------------------------------
2370 procedure Analyze_Formal_Ordinary_Fixed_Point_Type
2374 Loc : constant Source_Ptr := Sloc (Def);
2375 Base : constant Entity_Id :=
2377 (E_Ordinary_Fixed_Point_Type, Current_Scope,
2378 Sloc (Defining_Identifier (Parent (Def))), 'G');
2381 -- The semantic attributes are set for completeness only, their values
2382 -- will never be used, since all properties of the type are non-static.
2385 Set_Ekind (T, E_Ordinary_Fixed_Point_Subtype);
2386 Set_Etype (T, Base);
2387 Set_Size_Info (T, Standard_Integer);
2388 Set_RM_Size (T, RM_Size (Standard_Integer));
2389 Set_Small_Value (T, Ureal_1);
2390 Set_Delta_Value (T, Ureal_1);
2391 Set_Scalar_Range (T,
2393 Low_Bound => Make_Real_Literal (Loc, Ureal_1),
2394 High_Bound => Make_Real_Literal (Loc, Ureal_1)));
2395 Set_Is_Constrained (T);
2397 Set_Is_Generic_Type (Base);
2398 Set_Etype (Base, Base);
2399 Set_Size_Info (Base, Standard_Integer);
2400 Set_RM_Size (Base, RM_Size (Standard_Integer));
2401 Set_Small_Value (Base, Ureal_1);
2402 Set_Delta_Value (Base, Ureal_1);
2403 Set_Scalar_Range (Base, Scalar_Range (T));
2404 Set_Parent (Base, Parent (Def));
2406 Check_Restriction (No_Fixed_Point, Def);
2407 end Analyze_Formal_Ordinary_Fixed_Point_Type;
2409 ----------------------------------------
2410 -- Analyze_Formal_Package_Declaration --
2411 ----------------------------------------
2413 procedure Analyze_Formal_Package_Declaration (N : Node_Id) is
2414 Loc : constant Source_Ptr := Sloc (N);
2415 Pack_Id : constant Entity_Id := Defining_Identifier (N);
2417 Gen_Id : constant Node_Id := Name (N);
2419 Gen_Unit : Entity_Id;
2421 Parent_Installed : Boolean := False;
2423 Parent_Instance : Entity_Id;
2424 Renaming_In_Par : Entity_Id;
2425 Associations : Boolean := True;
2427 Vis_Prims_List : Elist_Id := No_Elist;
2428 -- List of primitives made temporarily visible in the instantiation
2429 -- to match the visibility of the formal type
2431 function Build_Local_Package return Node_Id;
2432 -- The formal package is rewritten so that its parameters are replaced
2433 -- with corresponding declarations. For parameters with bona fide
2434 -- associations these declarations are created by Analyze_Associations
2435 -- as for a regular instantiation. For boxed parameters, we preserve
2436 -- the formal declarations and analyze them, in order to introduce
2437 -- entities of the right kind in the environment of the formal.
2439 -------------------------
2440 -- Build_Local_Package --
2441 -------------------------
2443 function Build_Local_Package return Node_Id is
2445 Pack_Decl : Node_Id;
2448 -- Within the formal, the name of the generic package is a renaming
2449 -- of the formal (as for a regular instantiation).
2452 Make_Package_Declaration (Loc,
2455 (Specification (Original_Node (Gen_Decl)),
2456 Empty, Instantiating => True));
2458 Renaming := Make_Package_Renaming_Declaration (Loc,
2459 Defining_Unit_Name =>
2460 Make_Defining_Identifier (Loc, Chars (Gen_Unit)),
2461 Name => New_Occurrence_Of (Formal, Loc));
2463 if Nkind (Gen_Id) = N_Identifier
2464 and then Chars (Gen_Id) = Chars (Pack_Id)
2467 ("& is hidden within declaration of instance", Gen_Id, Gen_Unit);
2470 -- If the formal is declared with a box, or with an others choice,
2471 -- create corresponding declarations for all entities in the formal
2472 -- part, so that names with the proper types are available in the
2473 -- specification of the formal package.
2475 -- On the other hand, if there are no associations, then all the
2476 -- formals must have defaults, and this will be checked by the
2477 -- call to Analyze_Associations.
2480 or else Nkind (First (Generic_Associations (N))) = N_Others_Choice
2483 Formal_Decl : Node_Id;
2486 -- TBA : for a formal package, need to recurse ???
2491 (Generic_Formal_Declarations (Original_Node (Gen_Decl)));
2492 while Present (Formal_Decl) loop
2494 (Decls, Copy_Generic_Node (Formal_Decl, Empty, True));
2499 -- If generic associations are present, use Analyze_Associations to
2500 -- create the proper renaming declarations.
2504 Act_Tree : constant Node_Id :=
2506 (Original_Node (Gen_Decl), Empty,
2507 Instantiating => True);
2510 Generic_Renamings.Set_Last (0);
2511 Generic_Renamings_HTable.Reset;
2512 Instantiation_Node := N;
2515 Analyze_Associations
2516 (I_Node => Original_Node (N),
2517 Formals => Generic_Formal_Declarations (Act_Tree),
2518 F_Copy => Generic_Formal_Declarations (Gen_Decl));
2520 Vis_Prims_List := Check_Hidden_Primitives (Decls);
2524 Append (Renaming, To => Decls);
2526 -- Add generated declarations ahead of local declarations in
2529 if No (Visible_Declarations (Specification (Pack_Decl))) then
2530 Set_Visible_Declarations (Specification (Pack_Decl), Decls);
2533 (First (Visible_Declarations (Specification (Pack_Decl))),
2538 end Build_Local_Package;
2540 -- Start of processing for Analyze_Formal_Package_Declaration
2543 Check_Text_IO_Special_Unit (Gen_Id);
2546 Check_Generic_Child_Unit (Gen_Id, Parent_Installed);
2547 Gen_Unit := Entity (Gen_Id);
2549 -- Check for a formal package that is a package renaming
2551 if Present (Renamed_Object (Gen_Unit)) then
2553 -- Indicate that unit is used, before replacing it with renamed
2554 -- entity for use below.
2556 if In_Extended_Main_Source_Unit (N) then
2557 Set_Is_Instantiated (Gen_Unit);
2558 Generate_Reference (Gen_Unit, N);
2561 Gen_Unit := Renamed_Object (Gen_Unit);
2564 if Ekind (Gen_Unit) /= E_Generic_Package then
2565 Error_Msg_N ("expect generic package name", Gen_Id);
2569 elsif Gen_Unit = Current_Scope then
2571 ("generic package cannot be used as a formal package of itself",
2576 elsif In_Open_Scopes (Gen_Unit) then
2577 if Is_Compilation_Unit (Gen_Unit)
2578 and then Is_Child_Unit (Current_Scope)
2580 -- Special-case the error when the formal is a parent, and
2581 -- continue analysis to minimize cascaded errors.
2584 ("generic parent cannot be used as formal package "
2585 & "of a child unit",
2590 ("generic package cannot be used as a formal package "
2598 -- Check that name of formal package does not hide name of generic,
2599 -- or its leading prefix. This check must be done separately because
2600 -- the name of the generic has already been analyzed.
2603 Gen_Name : Entity_Id;
2607 while Nkind (Gen_Name) = N_Expanded_Name loop
2608 Gen_Name := Prefix (Gen_Name);
2611 if Chars (Gen_Name) = Chars (Pack_Id) then
2613 ("& is hidden within declaration of formal package",
2619 or else No (Generic_Associations (N))
2620 or else Nkind (First (Generic_Associations (N))) = N_Others_Choice
2622 Associations := False;
2625 -- If there are no generic associations, the generic parameters appear
2626 -- as local entities and are instantiated like them. We copy the generic
2627 -- package declaration as if it were an instantiation, and analyze it
2628 -- like a regular package, except that we treat the formals as
2629 -- additional visible components.
2631 Gen_Decl := Unit_Declaration_Node (Gen_Unit);
2633 if In_Extended_Main_Source_Unit (N) then
2634 Set_Is_Instantiated (Gen_Unit);
2635 Generate_Reference (Gen_Unit, N);
2638 Formal := New_Copy (Pack_Id);
2639 Create_Instantiation_Source (N, Gen_Unit, False, S_Adjustment);
2642 -- Make local generic without formals. The formals will be replaced
2643 -- with internal declarations.
2645 New_N := Build_Local_Package;
2647 -- If there are errors in the parameter list, Analyze_Associations
2648 -- raises Instantiation_Error. Patch the declaration to prevent
2649 -- further exception propagation.
2652 when Instantiation_Error =>
2654 Enter_Name (Formal);
2655 Set_Ekind (Formal, E_Variable);
2656 Set_Etype (Formal, Any_Type);
2657 Restore_Hidden_Primitives (Vis_Prims_List);
2659 if Parent_Installed then
2667 Set_Defining_Unit_Name (Specification (New_N), Formal);
2668 Set_Generic_Parent (Specification (N), Gen_Unit);
2669 Set_Instance_Env (Gen_Unit, Formal);
2670 Set_Is_Generic_Instance (Formal);
2672 Enter_Name (Formal);
2673 Set_Ekind (Formal, E_Package);
2674 Set_Etype (Formal, Standard_Void_Type);
2675 Set_Inner_Instances (Formal, New_Elmt_List);
2676 Push_Scope (Formal);
2678 if Is_Child_Unit (Gen_Unit)
2679 and then Parent_Installed
2681 -- Similarly, we have to make the name of the formal visible in the
2682 -- parent instance, to resolve properly fully qualified names that
2683 -- may appear in the generic unit. The parent instance has been
2684 -- placed on the scope stack ahead of the current scope.
2686 Parent_Instance := Scope_Stack.Table (Scope_Stack.Last - 1).Entity;
2689 Make_Defining_Identifier (Loc, Chars (Gen_Unit));
2690 Set_Ekind (Renaming_In_Par, E_Package);
2691 Set_Etype (Renaming_In_Par, Standard_Void_Type);
2692 Set_Scope (Renaming_In_Par, Parent_Instance);
2693 Set_Parent (Renaming_In_Par, Parent (Formal));
2694 Set_Renamed_Object (Renaming_In_Par, Formal);
2695 Append_Entity (Renaming_In_Par, Parent_Instance);
2698 Analyze (Specification (N));
2700 -- The formals for which associations are provided are not visible
2701 -- outside of the formal package. The others are still declared by a
2702 -- formal parameter declaration.
2704 -- If there are no associations, the only local entity to hide is the
2705 -- generated package renaming itself.
2711 E := First_Entity (Formal);
2712 while Present (E) loop
2714 and then not Is_Generic_Formal (E)
2719 if Ekind (E) = E_Package
2720 and then Renamed_Entity (E) = Formal
2730 End_Package_Scope (Formal);
2731 Restore_Hidden_Primitives (Vis_Prims_List);
2733 if Parent_Installed then
2739 -- Inside the generic unit, the formal package is a regular package, but
2740 -- no body is needed for it. Note that after instantiation, the defining
2741 -- unit name we need is in the new tree and not in the original (see
2742 -- Package_Instantiation). A generic formal package is an instance, and
2743 -- can be used as an actual for an inner instance.
2745 Set_Has_Completion (Formal, True);
2747 -- Add semantic information to the original defining identifier.
2750 Set_Ekind (Pack_Id, E_Package);
2751 Set_Etype (Pack_Id, Standard_Void_Type);
2752 Set_Scope (Pack_Id, Scope (Formal));
2753 Set_Has_Completion (Pack_Id, True);
2756 if Has_Aspects (N) then
2757 Analyze_Aspect_Specifications (N, Pack_Id);
2759 end Analyze_Formal_Package_Declaration;
2761 ---------------------------------
2762 -- Analyze_Formal_Private_Type --
2763 ---------------------------------
2765 procedure Analyze_Formal_Private_Type
2771 New_Private_Type (N, T, Def);
2773 -- Set the size to an arbitrary but legal value
2775 Set_Size_Info (T, Standard_Integer);
2776 Set_RM_Size (T, RM_Size (Standard_Integer));
2777 end Analyze_Formal_Private_Type;
2779 ------------------------------------
2780 -- Analyze_Formal_Incomplete_Type --
2781 ------------------------------------
2783 procedure Analyze_Formal_Incomplete_Type
2789 Set_Ekind (T, E_Incomplete_Type);
2791 Set_Private_Dependents (T, New_Elmt_List);
2793 if Tagged_Present (Def) then
2794 Set_Is_Tagged_Type (T);
2795 Make_Class_Wide_Type (T);
2796 Set_Direct_Primitive_Operations (T, New_Elmt_List);
2798 end Analyze_Formal_Incomplete_Type;
2800 ----------------------------------------
2801 -- Analyze_Formal_Signed_Integer_Type --
2802 ----------------------------------------
2804 procedure Analyze_Formal_Signed_Integer_Type
2808 Base : constant Entity_Id :=
2810 (E_Signed_Integer_Type,
2812 Sloc (Defining_Identifier (Parent (Def))), 'G');
2817 Set_Ekind (T, E_Signed_Integer_Subtype);
2818 Set_Etype (T, Base);
2819 Set_Size_Info (T, Standard_Integer);
2820 Set_RM_Size (T, RM_Size (Standard_Integer));
2821 Set_Scalar_Range (T, Scalar_Range (Standard_Integer));
2822 Set_Is_Constrained (T);
2824 Set_Is_Generic_Type (Base);
2825 Set_Size_Info (Base, Standard_Integer);
2826 Set_RM_Size (Base, RM_Size (Standard_Integer));
2827 Set_Etype (Base, Base);
2828 Set_Scalar_Range (Base, Scalar_Range (Standard_Integer));
2829 Set_Parent (Base, Parent (Def));
2830 end Analyze_Formal_Signed_Integer_Type;
2832 -------------------------------------------
2833 -- Analyze_Formal_Subprogram_Declaration --
2834 -------------------------------------------
2836 procedure Analyze_Formal_Subprogram_Declaration (N : Node_Id) is
2837 Spec : constant Node_Id := Specification (N);
2838 Def : constant Node_Id := Default_Name (N);
2839 Nam : constant Entity_Id := Defining_Unit_Name (Spec);
2847 if Nkind (Nam) = N_Defining_Program_Unit_Name then
2848 Error_Msg_N ("name of formal subprogram must be a direct name", Nam);
2852 Analyze_Subprogram_Declaration (N);
2853 Set_Is_Formal_Subprogram (Nam);
2854 Set_Has_Completion (Nam);
2856 if Nkind (N) = N_Formal_Abstract_Subprogram_Declaration then
2857 Set_Is_Abstract_Subprogram (Nam);
2858 Set_Is_Dispatching_Operation (Nam);
2861 Ctrl_Type : constant Entity_Id := Find_Dispatching_Type (Nam);
2863 if No (Ctrl_Type) then
2865 ("abstract formal subprogram must have a controlling type",
2868 elsif Ada_Version >= Ada_2012
2869 and then Is_Incomplete_Type (Ctrl_Type)
2872 ("controlling type of abstract formal subprogram cannot " &
2873 "be incomplete type", N, Ctrl_Type);
2876 Check_Controlling_Formals (Ctrl_Type, Nam);
2881 -- Default name is resolved at the point of instantiation
2883 if Box_Present (N) then
2886 -- Else default is bound at the point of generic declaration
2888 elsif Present (Def) then
2889 if Nkind (Def) = N_Operator_Symbol then
2890 Find_Direct_Name (Def);
2892 elsif Nkind (Def) /= N_Attribute_Reference then
2896 -- For an attribute reference, analyze the prefix and verify
2897 -- that it has the proper profile for the subprogram.
2899 Analyze (Prefix (Def));
2900 Valid_Default_Attribute (Nam, Def);
2904 -- Default name may be overloaded, in which case the interpretation
2905 -- with the correct profile must be selected, as for a renaming.
2906 -- If the definition is an indexed component, it must denote a
2907 -- member of an entry family. If it is a selected component, it
2908 -- can be a protected operation.
2910 if Etype (Def) = Any_Type then
2913 elsif Nkind (Def) = N_Selected_Component then
2914 if not Is_Overloadable (Entity (Selector_Name (Def))) then
2915 Error_Msg_N ("expect valid subprogram name as default", Def);
2918 elsif Nkind (Def) = N_Indexed_Component then
2919 if Is_Entity_Name (Prefix (Def)) then
2920 if Ekind (Entity (Prefix (Def))) /= E_Entry_Family then
2921 Error_Msg_N ("expect valid subprogram name as default", Def);
2924 elsif Nkind (Prefix (Def)) = N_Selected_Component then
2925 if Ekind (Entity (Selector_Name (Prefix (Def)))) /=
2928 Error_Msg_N ("expect valid subprogram name as default", Def);
2932 Error_Msg_N ("expect valid subprogram name as default", Def);
2936 elsif Nkind (Def) = N_Character_Literal then
2938 -- Needs some type checks: subprogram should be parameterless???
2940 Resolve (Def, (Etype (Nam)));
2942 elsif not Is_Entity_Name (Def)
2943 or else not Is_Overloadable (Entity (Def))
2945 Error_Msg_N ("expect valid subprogram name as default", Def);
2948 elsif not Is_Overloaded (Def) then
2949 Subp := Entity (Def);
2952 Error_Msg_N ("premature usage of formal subprogram", Def);
2954 elsif not Entity_Matches_Spec (Subp, Nam) then
2955 Error_Msg_N ("no visible entity matches specification", Def);
2958 -- More than one interpretation, so disambiguate as for a renaming
2963 I1 : Interp_Index := 0;
2969 Get_First_Interp (Def, I, It);
2970 while Present (It.Nam) loop
2971 if Entity_Matches_Spec (It.Nam, Nam) then
2972 if Subp /= Any_Id then
2973 It1 := Disambiguate (Def, I1, I, Etype (Subp));
2975 if It1 = No_Interp then
2976 Error_Msg_N ("ambiguous default subprogram", Def);
2989 Get_Next_Interp (I, It);
2993 if Subp /= Any_Id then
2995 -- Subprogram found, generate reference to it
2997 Set_Entity (Def, Subp);
2998 Generate_Reference (Subp, Def);
3001 Error_Msg_N ("premature usage of formal subprogram", Def);
3003 elsif Ekind (Subp) /= E_Operator then
3004 Check_Mode_Conformant (Subp, Nam);
3008 Error_Msg_N ("no visible subprogram matches specification", N);
3014 if Has_Aspects (N) then
3015 Analyze_Aspect_Specifications (N, Nam);
3018 end Analyze_Formal_Subprogram_Declaration;
3020 -------------------------------------
3021 -- Analyze_Formal_Type_Declaration --
3022 -------------------------------------
3024 procedure Analyze_Formal_Type_Declaration (N : Node_Id) is
3025 Def : constant Node_Id := Formal_Type_Definition (N);
3029 T := Defining_Identifier (N);
3031 if Present (Discriminant_Specifications (N))
3032 and then Nkind (Def) /= N_Formal_Private_Type_Definition
3035 ("discriminants not allowed for this formal type", T);
3038 -- Enter the new name, and branch to specific routine
3041 when N_Formal_Private_Type_Definition =>
3042 Analyze_Formal_Private_Type (N, T, Def);
3044 when N_Formal_Derived_Type_Definition =>
3045 Analyze_Formal_Derived_Type (N, T, Def);
3047 when N_Formal_Incomplete_Type_Definition =>
3048 Analyze_Formal_Incomplete_Type (T, Def);
3050 when N_Formal_Discrete_Type_Definition =>
3051 Analyze_Formal_Discrete_Type (T, Def);
3053 when N_Formal_Signed_Integer_Type_Definition =>
3054 Analyze_Formal_Signed_Integer_Type (T, Def);
3056 when N_Formal_Modular_Type_Definition =>
3057 Analyze_Formal_Modular_Type (T, Def);
3059 when N_Formal_Floating_Point_Definition =>
3060 Analyze_Formal_Floating_Type (T, Def);
3062 when N_Formal_Ordinary_Fixed_Point_Definition =>
3063 Analyze_Formal_Ordinary_Fixed_Point_Type (T, Def);
3065 when N_Formal_Decimal_Fixed_Point_Definition =>
3066 Analyze_Formal_Decimal_Fixed_Point_Type (T, Def);
3068 when N_Array_Type_Definition =>
3069 Analyze_Formal_Array_Type (T, Def);
3071 when N_Access_To_Object_Definition |
3072 N_Access_Function_Definition |
3073 N_Access_Procedure_Definition =>
3074 Analyze_Generic_Access_Type (T, Def);
3076 -- Ada 2005: a interface declaration is encoded as an abstract
3077 -- record declaration or a abstract type derivation.
3079 when N_Record_Definition =>
3080 Analyze_Formal_Interface_Type (N, T, Def);
3082 when N_Derived_Type_Definition =>
3083 Analyze_Formal_Derived_Interface_Type (N, T, Def);
3089 raise Program_Error;
3093 Set_Is_Generic_Type (T);
3095 if Has_Aspects (N) then
3096 Analyze_Aspect_Specifications (N, T);
3098 end Analyze_Formal_Type_Declaration;
3100 ------------------------------------
3101 -- Analyze_Function_Instantiation --
3102 ------------------------------------
3104 procedure Analyze_Function_Instantiation (N : Node_Id) is
3106 Analyze_Subprogram_Instantiation (N, E_Function);
3107 end Analyze_Function_Instantiation;
3109 ---------------------------------
3110 -- Analyze_Generic_Access_Type --
3111 ---------------------------------
3113 procedure Analyze_Generic_Access_Type (T : Entity_Id; Def : Node_Id) is
3117 if Nkind (Def) = N_Access_To_Object_Definition then
3118 Access_Type_Declaration (T, Def);
3120 if Is_Incomplete_Or_Private_Type (Designated_Type (T))
3121 and then No (Full_View (Designated_Type (T)))
3122 and then not Is_Generic_Type (Designated_Type (T))
3124 Error_Msg_N ("premature usage of incomplete type", Def);
3126 elsif not Is_Entity_Name (Subtype_Indication (Def)) then
3128 ("only a subtype mark is allowed in a formal", Def);
3132 Access_Subprogram_Declaration (T, Def);
3134 end Analyze_Generic_Access_Type;
3136 ---------------------------------
3137 -- Analyze_Generic_Formal_Part --
3138 ---------------------------------
3140 procedure Analyze_Generic_Formal_Part (N : Node_Id) is
3141 Gen_Parm_Decl : Node_Id;
3144 -- The generic formals are processed in the scope of the generic unit,
3145 -- where they are immediately visible. The scope is installed by the
3148 Gen_Parm_Decl := First (Generic_Formal_Declarations (N));
3150 while Present (Gen_Parm_Decl) loop
3151 Analyze (Gen_Parm_Decl);
3152 Next (Gen_Parm_Decl);
3155 Generate_Reference_To_Generic_Formals (Current_Scope);
3156 end Analyze_Generic_Formal_Part;
3158 ------------------------------------------
3159 -- Analyze_Generic_Package_Declaration --
3160 ------------------------------------------
3162 procedure Analyze_Generic_Package_Declaration (N : Node_Id) is
3163 Loc : constant Source_Ptr := Sloc (N);
3166 Save_Parent : Node_Id;
3168 Decls : constant List_Id :=
3169 Visible_Declarations (Specification (N));
3173 Check_SPARK_Restriction ("generic is not allowed", N);
3175 -- We introduce a renaming of the enclosing package, to have a usable
3176 -- entity as the prefix of an expanded name for a local entity of the
3177 -- form Par.P.Q, where P is the generic package. This is because a local
3178 -- entity named P may hide it, so that the usual visibility rules in
3179 -- the instance will not resolve properly.
3182 Make_Package_Renaming_Declaration (Loc,
3183 Defining_Unit_Name =>
3184 Make_Defining_Identifier (Loc,
3185 Chars => New_External_Name (Chars (Defining_Entity (N)), "GH")),
3186 Name => Make_Identifier (Loc, Chars (Defining_Entity (N))));
3188 if Present (Decls) then
3189 Decl := First (Decls);
3190 while Present (Decl)
3191 and then Nkind (Decl) = N_Pragma
3196 if Present (Decl) then
3197 Insert_Before (Decl, Renaming);
3199 Append (Renaming, Visible_Declarations (Specification (N)));
3203 Set_Visible_Declarations (Specification (N), New_List (Renaming));
3206 -- Create copy of generic unit, and save for instantiation. If the unit
3207 -- is a child unit, do not copy the specifications for the parent, which
3208 -- are not part of the generic tree.
3210 Save_Parent := Parent_Spec (N);
3211 Set_Parent_Spec (N, Empty);
3213 New_N := Copy_Generic_Node (N, Empty, Instantiating => False);
3214 Set_Parent_Spec (New_N, Save_Parent);
3217 -- Once the contents of the generic copy and the template are swapped,
3218 -- do the same for their respective aspect specifications.
3220 Exchange_Aspects (N, New_N);
3221 Id := Defining_Entity (N);
3222 Generate_Definition (Id);
3224 -- Expansion is not applied to generic units
3229 Set_Ekind (Id, E_Generic_Package);
3230 Set_Etype (Id, Standard_Void_Type);
3231 Set_Contract (Id, Make_Contract (Sloc (Id)));
3233 -- Analyze aspects now, so that generated pragmas appear in the
3234 -- declarations before building and analyzing the generic copy.
3236 if Has_Aspects (N) then
3237 Analyze_Aspect_Specifications (N, Id);
3241 Enter_Generic_Scope (Id);
3242 Set_Inner_Instances (Id, New_Elmt_List);
3244 Set_Categorization_From_Pragmas (N);
3245 Set_Is_Pure (Id, Is_Pure (Current_Scope));
3247 -- Link the declaration of the generic homonym in the generic copy to
3248 -- the package it renames, so that it is always resolved properly.
3250 Set_Generic_Homonym (Id, Defining_Unit_Name (Renaming));
3251 Set_Entity (Associated_Node (Name (Renaming)), Id);
3253 -- For a library unit, we have reconstructed the entity for the unit,
3254 -- and must reset it in the library tables.
3256 if Nkind (Parent (N)) = N_Compilation_Unit then
3257 Set_Cunit_Entity (Current_Sem_Unit, Id);
3260 Analyze_Generic_Formal_Part (N);
3262 -- After processing the generic formals, analysis proceeds as for a
3263 -- non-generic package.
3265 Analyze (Specification (N));
3267 Validate_Categorization_Dependency (N, Id);
3271 End_Package_Scope (Id);
3272 Exit_Generic_Scope (Id);
3274 if Nkind (Parent (N)) /= N_Compilation_Unit then
3275 Move_Freeze_Nodes (Id, N, Visible_Declarations (Specification (N)));
3276 Move_Freeze_Nodes (Id, N, Private_Declarations (Specification (N)));
3277 Move_Freeze_Nodes (Id, N, Generic_Formal_Declarations (N));
3280 Set_Body_Required (Parent (N), Unit_Requires_Body (Id));
3281 Validate_RT_RAT_Component (N);
3283 -- If this is a spec without a body, check that generic parameters
3286 if not Body_Required (Parent (N)) then
3287 Check_References (Id);
3290 end Analyze_Generic_Package_Declaration;
3292 --------------------------------------------
3293 -- Analyze_Generic_Subprogram_Declaration --
3294 --------------------------------------------
3296 procedure Analyze_Generic_Subprogram_Declaration (N : Node_Id) is
3301 Result_Type : Entity_Id;
3302 Save_Parent : Node_Id;
3306 Check_SPARK_Restriction ("generic is not allowed", N);
3308 -- Create copy of generic unit, and save for instantiation. If the unit
3309 -- is a child unit, do not copy the specifications for the parent, which
3310 -- are not part of the generic tree.
3312 Save_Parent := Parent_Spec (N);
3313 Set_Parent_Spec (N, Empty);
3315 New_N := Copy_Generic_Node (N, Empty, Instantiating => False);
3316 Set_Parent_Spec (New_N, Save_Parent);
3319 Check_SPARK_Mode_In_Generic (N);
3321 -- The aspect specifications are not attached to the tree, and must
3322 -- be copied and attached to the generic copy explicitly.
3324 if Present (Aspect_Specifications (New_N)) then
3326 Aspects : constant List_Id := Aspect_Specifications (N);
3328 Set_Has_Aspects (N, False);
3329 Move_Aspects (New_N, To => N);
3330 Set_Has_Aspects (Original_Node (N), False);
3331 Set_Aspect_Specifications (Original_Node (N), Aspects);
3335 Spec := Specification (N);
3336 Id := Defining_Entity (Spec);
3337 Generate_Definition (Id);
3338 Set_Contract (Id, Make_Contract (Sloc (Id)));
3340 if Nkind (Id) = N_Defining_Operator_Symbol then
3342 ("operator symbol not allowed for generic subprogram", Id);
3349 Set_Scope_Depth_Value (Id, Scope_Depth (Current_Scope) + 1);
3351 Enter_Generic_Scope (Id);
3352 Set_Inner_Instances (Id, New_Elmt_List);
3353 Set_Is_Pure (Id, Is_Pure (Current_Scope));
3355 Analyze_Generic_Formal_Part (N);
3357 Formals := Parameter_Specifications (Spec);
3359 if Present (Formals) then
3360 Process_Formals (Formals, Spec);
3363 if Nkind (Spec) = N_Function_Specification then
3364 Set_Ekind (Id, E_Generic_Function);
3366 if Nkind (Result_Definition (Spec)) = N_Access_Definition then
3367 Result_Type := Access_Definition (Spec, Result_Definition (Spec));
3368 Set_Etype (Id, Result_Type);
3370 -- Check restriction imposed by AI05-073: a generic function
3371 -- cannot return an abstract type or an access to such.
3373 -- This is a binding interpretation should it apply to earlier
3374 -- versions of Ada as well as Ada 2012???
3376 if Is_Abstract_Type (Designated_Type (Result_Type))
3377 and then Ada_Version >= Ada_2012
3379 Error_Msg_N ("generic function cannot have an access result"
3380 & " that designates an abstract type", Spec);
3384 Find_Type (Result_Definition (Spec));
3385 Typ := Entity (Result_Definition (Spec));
3387 if Is_Abstract_Type (Typ)
3388 and then Ada_Version >= Ada_2012
3391 ("generic function cannot have abstract result type", Spec);
3394 -- If a null exclusion is imposed on the result type, then create
3395 -- a null-excluding itype (an access subtype) and use it as the
3396 -- function's Etype.
3398 if Is_Access_Type (Typ)
3399 and then Null_Exclusion_Present (Spec)
3402 Create_Null_Excluding_Itype
3404 Related_Nod => Spec,
3405 Scope_Id => Defining_Unit_Name (Spec)));
3407 Set_Etype (Id, Typ);
3412 Set_Ekind (Id, E_Generic_Procedure);
3413 Set_Etype (Id, Standard_Void_Type);
3416 -- For a library unit, we have reconstructed the entity for the unit,
3417 -- and must reset it in the library tables. We also make sure that
3418 -- Body_Required is set properly in the original compilation unit node.
3420 if Nkind (Parent (N)) = N_Compilation_Unit then
3421 Set_Cunit_Entity (Current_Sem_Unit, Id);
3422 Set_Body_Required (Parent (N), Unit_Requires_Body (Id));
3425 Set_Categorization_From_Pragmas (N);
3426 Validate_Categorization_Dependency (N, Id);
3428 Save_Global_References (Original_Node (N));
3430 -- For ASIS purposes, convert any postcondition, precondition pragmas
3431 -- into aspects, if N is not a compilation unit by itself, in order to
3432 -- enable the analysis of expressions inside the corresponding PPC
3435 if ASIS_Mode and then Is_List_Member (N) then
3436 Make_Aspect_For_PPC_In_Gen_Sub_Decl (N);
3439 -- To capture global references, analyze the expressions of aspects,
3440 -- and propagate information to original tree. Note that in this case
3441 -- analysis of attributes is not delayed until the freeze point.
3443 -- It seems very hard to recreate the proper visibility of the generic
3444 -- subprogram at a later point because the analysis of an aspect may
3445 -- create pragmas after the generic copies have been made ???
3447 if Has_Aspects (N) then
3452 Aspect := First (Aspect_Specifications (N));
3453 while Present (Aspect) loop
3454 if Get_Aspect_Id (Aspect) /= Aspect_Warnings
3455 and then Present (Expression (Aspect))
3457 Analyze (Expression (Aspect));
3463 Aspect := First (Aspect_Specifications (Original_Node (N)));
3464 while Present (Aspect) loop
3465 if Present (Expression (Aspect)) then
3466 Save_Global_References (Expression (Aspect));
3476 Exit_Generic_Scope (Id);
3477 Generate_Reference_To_Formals (Id);
3479 List_Inherited_Pre_Post_Aspects (Id);
3480 end Analyze_Generic_Subprogram_Declaration;
3482 -----------------------------------
3483 -- Analyze_Package_Instantiation --
3484 -----------------------------------
3486 procedure Analyze_Package_Instantiation (N : Node_Id) is
3487 Loc : constant Source_Ptr := Sloc (N);
3488 Gen_Id : constant Node_Id := Name (N);
3491 Act_Decl_Name : Node_Id;
3492 Act_Decl_Id : Entity_Id;
3497 Gen_Unit : Entity_Id;
3499 Is_Actual_Pack : constant Boolean :=
3500 Is_Internal (Defining_Entity (N));
3502 Env_Installed : Boolean := False;
3503 Parent_Installed : Boolean := False;
3504 Renaming_List : List_Id;
3505 Unit_Renaming : Node_Id;
3506 Needs_Body : Boolean;
3507 Inline_Now : Boolean := False;
3509 Save_Style_Check : constant Boolean := Style_Check;
3510 -- Save style check mode for restore on exit
3512 procedure Delay_Descriptors (E : Entity_Id);
3513 -- Delay generation of subprogram descriptors for given entity
3515 function Might_Inline_Subp return Boolean;
3516 -- If inlining is active and the generic contains inlined subprograms,
3517 -- we instantiate the body. This may cause superfluous instantiations,
3518 -- but it is simpler than detecting the need for the body at the point
3519 -- of inlining, when the context of the instance is not available.
3521 function Must_Inline_Subp return Boolean;
3522 -- If inlining is active and the generic contains inlined subprograms,
3523 -- return True if some of the inlined subprograms must be inlined by
3526 -----------------------
3527 -- Delay_Descriptors --
3528 -----------------------
3530 procedure Delay_Descriptors (E : Entity_Id) is
3532 if not Delay_Subprogram_Descriptors (E) then
3533 Set_Delay_Subprogram_Descriptors (E);
3534 Pending_Descriptor.Append (E);
3536 end Delay_Descriptors;
3538 -----------------------
3539 -- Might_Inline_Subp --
3540 -----------------------
3542 function Might_Inline_Subp return Boolean is
3546 if not Inline_Processing_Required then
3550 E := First_Entity (Gen_Unit);
3551 while Present (E) loop
3552 if Is_Subprogram (E)
3553 and then Is_Inlined (E)
3563 end Might_Inline_Subp;
3565 ----------------------
3566 -- Must_Inline_Subp --
3567 ----------------------
3569 function Must_Inline_Subp return Boolean is
3573 if not Inline_Processing_Required then
3577 E := First_Entity (Gen_Unit);
3578 while Present (E) loop
3579 if Is_Subprogram (E)
3580 and then Is_Inlined (E)
3581 and then Must_Inline (E)
3591 end Must_Inline_Subp;
3593 -- Local declarations
3595 Vis_Prims_List : Elist_Id := No_Elist;
3596 -- List of primitives made temporarily visible in the instantiation
3597 -- to match the visibility of the formal type
3599 -- Start of processing for Analyze_Package_Instantiation
3602 Check_SPARK_Restriction ("generic is not allowed", N);
3604 -- Very first thing: check for Text_IO sp[ecial unit in case we are
3605 -- instantiating one of the children of [[Wide_]Wide_]Text_IO.
3607 Check_Text_IO_Special_Unit (Name (N));
3609 -- Make node global for error reporting
3611 Instantiation_Node := N;
3613 -- Turn off style checking in instances. If the check is enabled on the
3614 -- generic unit, a warning in an instance would just be noise. If not
3615 -- enabled on the generic, then a warning in an instance is just wrong.
3617 Style_Check := False;
3619 -- Case of instantiation of a generic package
3621 if Nkind (N) = N_Package_Instantiation then
3622 Act_Decl_Id := New_Copy (Defining_Entity (N));
3623 Set_Comes_From_Source (Act_Decl_Id, True);
3625 if Nkind (Defining_Unit_Name (N)) = N_Defining_Program_Unit_Name then
3627 Make_Defining_Program_Unit_Name (Loc,
3628 Name => New_Copy_Tree (Name (Defining_Unit_Name (N))),
3629 Defining_Identifier => Act_Decl_Id);
3631 Act_Decl_Name := Act_Decl_Id;
3634 -- Case of instantiation of a formal package
3637 Act_Decl_Id := Defining_Identifier (N);
3638 Act_Decl_Name := Act_Decl_Id;
3641 Generate_Definition (Act_Decl_Id);
3642 Preanalyze_Actuals (N);
3645 Env_Installed := True;
3647 -- Reset renaming map for formal types. The mapping is established
3648 -- when analyzing the generic associations, but some mappings are
3649 -- inherited from formal packages of parent units, and these are
3650 -- constructed when the parents are installed.
3652 Generic_Renamings.Set_Last (0);
3653 Generic_Renamings_HTable.Reset;
3655 Check_Generic_Child_Unit (Gen_Id, Parent_Installed);
3656 Gen_Unit := Entity (Gen_Id);
3658 -- Verify that it is the name of a generic package
3660 -- A visibility glitch: if the instance is a child unit and the generic
3661 -- is the generic unit of a parent instance (i.e. both the parent and
3662 -- the child units are instances of the same package) the name now
3663 -- denotes the renaming within the parent, not the intended generic
3664 -- unit. See if there is a homonym that is the desired generic. The
3665 -- renaming declaration must be visible inside the instance of the
3666 -- child, but not when analyzing the name in the instantiation itself.
3668 if Ekind (Gen_Unit) = E_Package
3669 and then Present (Renamed_Entity (Gen_Unit))
3670 and then In_Open_Scopes (Renamed_Entity (Gen_Unit))
3671 and then Is_Generic_Instance (Renamed_Entity (Gen_Unit))
3672 and then Present (Homonym (Gen_Unit))
3674 Gen_Unit := Homonym (Gen_Unit);
3677 if Etype (Gen_Unit) = Any_Type then
3681 elsif Ekind (Gen_Unit) /= E_Generic_Package then
3683 -- Ada 2005 (AI-50217): Cannot use instance in limited with_clause
3685 if From_Limited_With (Gen_Unit) then
3687 ("cannot instantiate a limited withed package", Gen_Id);
3690 ("& is not the name of a generic package", Gen_Id, Gen_Unit);
3697 if In_Extended_Main_Source_Unit (N) then
3698 Set_Is_Instantiated (Gen_Unit);
3699 Generate_Reference (Gen_Unit, N);
3701 if Present (Renamed_Object (Gen_Unit)) then
3702 Set_Is_Instantiated (Renamed_Object (Gen_Unit));
3703 Generate_Reference (Renamed_Object (Gen_Unit), N);
3707 if Nkind (Gen_Id) = N_Identifier
3708 and then Chars (Gen_Unit) = Chars (Defining_Entity (N))
3711 ("& is hidden within declaration of instance", Gen_Id, Gen_Unit);
3713 elsif Nkind (Gen_Id) = N_Expanded_Name
3714 and then Is_Child_Unit (Gen_Unit)
3715 and then Nkind (Prefix (Gen_Id)) = N_Identifier
3716 and then Chars (Act_Decl_Id) = Chars (Prefix (Gen_Id))
3719 ("& is hidden within declaration of instance ", Prefix (Gen_Id));
3722 Set_Entity (Gen_Id, Gen_Unit);
3724 -- If generic is a renaming, get original generic unit
3726 if Present (Renamed_Object (Gen_Unit))
3727 and then Ekind (Renamed_Object (Gen_Unit)) = E_Generic_Package
3729 Gen_Unit := Renamed_Object (Gen_Unit);
3732 -- Verify that there are no circular instantiations
3734 if In_Open_Scopes (Gen_Unit) then
3735 Error_Msg_NE ("instantiation of & within itself", N, Gen_Unit);
3739 elsif Contains_Instance_Of (Gen_Unit, Current_Scope, Gen_Id) then
3740 Error_Msg_Node_2 := Current_Scope;
3742 ("circular Instantiation: & instantiated in &!", N, Gen_Unit);
3743 Circularity_Detected := True;
3748 Gen_Decl := Unit_Declaration_Node (Gen_Unit);
3750 -- Initialize renamings map, for error checking, and the list that
3751 -- holds private entities whose views have changed between generic
3752 -- definition and instantiation. If this is the instance created to
3753 -- validate an actual package, the instantiation environment is that
3754 -- of the enclosing instance.
3756 Create_Instantiation_Source (N, Gen_Unit, False, S_Adjustment);
3758 -- Copy original generic tree, to produce text for instantiation
3762 (Original_Node (Gen_Decl), Empty, Instantiating => True);
3764 Act_Spec := Specification (Act_Tree);
3766 -- If this is the instance created to validate an actual package,
3767 -- only the formals matter, do not examine the package spec itself.
3769 if Is_Actual_Pack then
3770 Set_Visible_Declarations (Act_Spec, New_List);
3771 Set_Private_Declarations (Act_Spec, New_List);
3775 Analyze_Associations
3777 Formals => Generic_Formal_Declarations (Act_Tree),
3778 F_Copy => Generic_Formal_Declarations (Gen_Decl));
3780 Vis_Prims_List := Check_Hidden_Primitives (Renaming_List);
3782 Set_Instance_Env (Gen_Unit, Act_Decl_Id);
3783 Set_Defining_Unit_Name (Act_Spec, Act_Decl_Name);
3784 Set_Is_Generic_Instance (Act_Decl_Id);
3785 Set_Generic_Parent (Act_Spec, Gen_Unit);
3787 -- References to the generic in its own declaration or its body are
3788 -- references to the instance. Add a renaming declaration for the
3789 -- generic unit itself. This declaration, as well as the renaming
3790 -- declarations for the generic formals, must remain private to the
3791 -- unit: the formals, because this is the language semantics, and
3792 -- the unit because its use is an artifact of the implementation.
3795 Make_Package_Renaming_Declaration (Loc,
3796 Defining_Unit_Name =>
3797 Make_Defining_Identifier (Loc, Chars (Gen_Unit)),
3798 Name => New_Occurrence_Of (Act_Decl_Id, Loc));
3800 Append (Unit_Renaming, Renaming_List);
3802 -- The renaming declarations are the first local declarations of the
3805 if Is_Non_Empty_List (Visible_Declarations (Act_Spec)) then
3807 (First (Visible_Declarations (Act_Spec)), Renaming_List);
3809 Set_Visible_Declarations (Act_Spec, Renaming_List);
3813 Make_Package_Declaration (Loc,
3814 Specification => Act_Spec);
3816 -- Propagate the aspect specifications from the package declaration
3817 -- template to the instantiated version of the package declaration.
3819 if Has_Aspects (Act_Tree) then
3820 Set_Aspect_Specifications (Act_Decl,
3821 New_Copy_List_Tree (Aspect_Specifications (Act_Tree)));
3824 -- Save the instantiation node, for subsequent instantiation of the
3825 -- body, if there is one and we are generating code for the current
3826 -- unit. Mark unit as having a body (avoids premature error message).
3828 -- We instantiate the body if we are generating code, if we are
3829 -- generating cross-reference information, or if we are building
3830 -- trees for ASIS use or GNATprove use.
3833 Enclosing_Body_Present : Boolean := False;
3834 -- If the generic unit is not a compilation unit, then a body may
3835 -- be present in its parent even if none is required. We create a
3836 -- tentative pending instantiation for the body, which will be
3837 -- discarded if none is actually present.
3842 if Scope (Gen_Unit) /= Standard_Standard
3843 and then not Is_Child_Unit (Gen_Unit)
3845 Scop := Scope (Gen_Unit);
3847 while Present (Scop)
3848 and then Scop /= Standard_Standard
3850 if Unit_Requires_Body (Scop) then
3851 Enclosing_Body_Present := True;
3854 elsif In_Open_Scopes (Scop)
3855 and then In_Package_Body (Scop)
3857 Enclosing_Body_Present := True;
3861 exit when Is_Compilation_Unit (Scop);
3862 Scop := Scope (Scop);
3866 -- If front-end inlining is enabled, and this is a unit for which
3867 -- code will be generated, we instantiate the body at once.
3869 -- This is done if the instance is not the main unit, and if the
3870 -- generic is not a child unit of another generic, to avoid scope
3871 -- problems and the reinstallation of parent instances.
3874 and then (not Is_Child_Unit (Gen_Unit)
3875 or else not Is_Generic_Unit (Scope (Gen_Unit)))
3876 and then Might_Inline_Subp
3877 and then not Is_Actual_Pack
3879 if not Back_End_Inlining
3880 and then Front_End_Inlining
3881 and then (Is_In_Main_Unit (N)
3882 or else In_Main_Context (Current_Scope))
3883 and then Nkind (Parent (N)) /= N_Compilation_Unit
3887 elsif Back_End_Inlining
3888 and then Must_Inline_Subp
3889 and then (Is_In_Main_Unit (N)
3890 or else In_Main_Context (Current_Scope))
3891 and then Nkind (Parent (N)) /= N_Compilation_Unit
3895 -- In configurable_run_time mode we force the inlining of
3896 -- predefined subprograms marked Inline_Always, to minimize
3897 -- the use of the run-time library.
3899 elsif Is_Predefined_File_Name
3900 (Unit_File_Name (Get_Source_Unit (Gen_Decl)))
3901 and then Configurable_Run_Time_Mode
3902 and then Nkind (Parent (N)) /= N_Compilation_Unit
3907 -- If the current scope is itself an instance within a child
3908 -- unit, there will be duplications in the scope stack, and the
3909 -- unstacking mechanism in Inline_Instance_Body will fail.
3910 -- This loses some rare cases of optimization, and might be
3911 -- improved some day, if we can find a proper abstraction for
3912 -- "the complete compilation context" that can be saved and
3915 if Is_Generic_Instance (Current_Scope) then
3917 Curr_Unit : constant Entity_Id :=
3918 Cunit_Entity (Current_Sem_Unit);
3920 if Curr_Unit /= Current_Scope
3921 and then Is_Child_Unit (Curr_Unit)
3923 Inline_Now := False;
3930 (Unit_Requires_Body (Gen_Unit)
3931 or else Enclosing_Body_Present
3932 or else Present (Corresponding_Body (Gen_Decl)))
3933 and then (Is_In_Main_Unit (N) or else Might_Inline_Subp)
3934 and then not Is_Actual_Pack
3935 and then not Inline_Now
3936 and then (Operating_Mode = Generate_Code
3938 -- Need comment for this check ???
3940 or else (Operating_Mode = Check_Semantics
3941 and then (ASIS_Mode or GNATprove_Mode)));
3943 -- If front_end_inlining is enabled, do not instantiate body if
3944 -- within a generic context.
3946 if (Front_End_Inlining and then not Expander_Active)
3947 or else Is_Generic_Unit (Cunit_Entity (Main_Unit))
3949 Needs_Body := False;
3952 -- If the current context is generic, and the package being
3953 -- instantiated is declared within a formal package, there is no
3954 -- body to instantiate until the enclosing generic is instantiated
3955 -- and there is an actual for the formal package. If the formal
3956 -- package has parameters, we build a regular package instance for
3957 -- it, that precedes the original formal package declaration.
3959 if In_Open_Scopes (Scope (Scope (Gen_Unit))) then
3961 Decl : constant Node_Id :=
3963 (Unit_Declaration_Node (Scope (Gen_Unit)));
3965 if Nkind (Decl) = N_Formal_Package_Declaration
3966 or else (Nkind (Decl) = N_Package_Declaration
3967 and then Is_List_Member (Decl)
3968 and then Present (Next (Decl))
3970 Nkind (Next (Decl)) =
3971 N_Formal_Package_Declaration)
3973 Needs_Body := False;
3979 -- For RCI unit calling stubs, we omit the instance body if the
3980 -- instance is the RCI library unit itself.
3982 -- However there is a special case for nested instances: in this case
3983 -- we do generate the instance body, as it might be required, e.g.
3984 -- because it provides stream attributes for some type used in the
3985 -- profile of a remote subprogram. This is consistent with 12.3(12),
3986 -- which indicates that the instance body occurs at the place of the
3987 -- instantiation, and thus is part of the RCI declaration, which is
3988 -- present on all client partitions (this is E.2.3(18)).
3990 -- Note that AI12-0002 may make it illegal at some point to have
3991 -- stream attributes defined in an RCI unit, in which case this
3992 -- special case will become unnecessary. In the meantime, there
3993 -- is known application code in production that depends on this
3994 -- being possible, so we definitely cannot eliminate the body in
3995 -- the case of nested instances for the time being.
3997 -- When we generate a nested instance body, calling stubs for any
3998 -- relevant subprogram will be be inserted immediately after the
3999 -- subprogram declarations, and will take precedence over the
4000 -- subsequent (original) body. (The stub and original body will be
4001 -- complete homographs, but this is permitted in an instance).
4002 -- (Could we do better and remove the original body???)
4004 if Distribution_Stub_Mode = Generate_Caller_Stub_Body
4005 and then Comes_From_Source (N)
4006 and then Nkind (Parent (N)) = N_Compilation_Unit
4008 Needs_Body := False;
4013 -- Here is a defence against a ludicrous number of instantiations
4014 -- caused by a circular set of instantiation attempts.
4016 if Pending_Instantiations.Last > Maximum_Instantiations then
4017 Error_Msg_Uint_1 := UI_From_Int (Maximum_Instantiations);
4018 Error_Msg_N ("too many instantiations, exceeds max of^", N);
4019 Error_Msg_N ("\limit can be changed using -gnateinn switch", N);
4020 raise Unrecoverable_Error;
4023 -- Indicate that the enclosing scopes contain an instantiation,
4024 -- and that cleanup actions should be delayed until after the
4025 -- instance body is expanded.
4027 Check_Forward_Instantiation (Gen_Decl);
4028 if Nkind (N) = N_Package_Instantiation then
4030 Enclosing_Master : Entity_Id;
4033 -- Loop to search enclosing masters
4035 Enclosing_Master := Current_Scope;
4036 Scope_Loop : while Enclosing_Master /= Standard_Standard loop
4037 if Ekind (Enclosing_Master) = E_Package then
4038 if Is_Compilation_Unit (Enclosing_Master) then
4039 if In_Package_Body (Enclosing_Master) then
4041 (Body_Entity (Enclosing_Master));
4050 Enclosing_Master := Scope (Enclosing_Master);
4053 elsif Is_Generic_Unit (Enclosing_Master)
4054 or else Ekind (Enclosing_Master) = E_Void
4056 -- Cleanup actions will eventually be performed on the
4057 -- enclosing subprogram or package instance, if any.
4058 -- Enclosing scope is void in the formal part of a
4059 -- generic subprogram.
4064 if Ekind (Enclosing_Master) = E_Entry
4066 Ekind (Scope (Enclosing_Master)) = E_Protected_Type
4068 if not Expander_Active then
4072 Protected_Body_Subprogram (Enclosing_Master);
4076 Set_Delay_Cleanups (Enclosing_Master);
4078 while Ekind (Enclosing_Master) = E_Block loop
4079 Enclosing_Master := Scope (Enclosing_Master);
4082 if Is_Subprogram (Enclosing_Master) then
4083 Delay_Descriptors (Enclosing_Master);
4085 elsif Is_Task_Type (Enclosing_Master) then
4087 TBP : constant Node_Id :=
4088 Get_Task_Body_Procedure
4091 if Present (TBP) then
4092 Delay_Descriptors (TBP);
4093 Set_Delay_Cleanups (TBP);
4100 end loop Scope_Loop;
4103 -- Make entry in table
4105 Pending_Instantiations.Append
4107 Act_Decl => Act_Decl,
4108 Expander_Status => Expander_Active,
4109 Current_Sem_Unit => Current_Sem_Unit,
4110 Scope_Suppress => Scope_Suppress,
4111 Local_Suppress_Stack_Top => Local_Suppress_Stack_Top,
4112 Version => Ada_Version,
4113 Version_Pragma => Ada_Version_Pragma,
4114 Warnings => Save_Warnings,
4115 SPARK_Mode => SPARK_Mode,
4116 SPARK_Mode_Pragma => SPARK_Mode_Pragma));
4120 Set_Categorization_From_Pragmas (Act_Decl);
4122 if Parent_Installed then
4126 Set_Instance_Spec (N, Act_Decl);
4128 -- If not a compilation unit, insert the package declaration before
4129 -- the original instantiation node.
4131 if Nkind (Parent (N)) /= N_Compilation_Unit then
4132 Mark_Rewrite_Insertion (Act_Decl);
4133 Insert_Before (N, Act_Decl);
4136 -- For an instantiation that is a compilation unit, place
4137 -- declaration on current node so context is complete for analysis
4138 -- (including nested instantiations). If this is the main unit,
4139 -- the declaration eventually replaces the instantiation node.
4140 -- If the instance body is created later, it replaces the
4141 -- instance node, and the declaration is attached to it
4142 -- (see Build_Instance_Compilation_Unit_Nodes).
4145 if Cunit_Entity (Current_Sem_Unit) = Defining_Entity (N) then
4147 -- The entity for the current unit is the newly created one,
4148 -- and all semantic information is attached to it.
4150 Set_Cunit_Entity (Current_Sem_Unit, Act_Decl_Id);
4152 -- If this is the main unit, replace the main entity as well
4154 if Current_Sem_Unit = Main_Unit then
4155 Main_Unit_Entity := Act_Decl_Id;
4159 Set_Unit (Parent (N), Act_Decl);
4160 Set_Parent_Spec (Act_Decl, Parent_Spec (N));
4161 Set_Package_Instantiation (Act_Decl_Id, N);
4163 -- Process aspect specifications of the instance node, if any, to
4164 -- take into account categorization pragmas before analyzing the
4167 if Has_Aspects (N) then
4168 Analyze_Aspect_Specifications (N, Act_Decl_Id);
4172 Set_Unit (Parent (N), N);
4173 Set_Body_Required (Parent (N), False);
4175 -- We never need elaboration checks on instantiations, since by
4176 -- definition, the body instantiation is elaborated at the same
4177 -- time as the spec instantiation.
4179 Set_Suppress_Elaboration_Warnings (Act_Decl_Id);
4180 Set_Kill_Elaboration_Checks (Act_Decl_Id);
4183 Check_Elab_Instantiation (N);
4185 if ABE_Is_Certain (N) and then Needs_Body then
4186 Pending_Instantiations.Decrement_Last;
4189 Check_Hidden_Child_Unit (N, Gen_Unit, Act_Decl_Id);
4191 Set_First_Private_Entity (Defining_Unit_Name (Unit_Renaming),
4192 First_Private_Entity (Act_Decl_Id));
4194 -- If the instantiation will receive a body, the unit will be
4195 -- transformed into a package body, and receive its own elaboration
4196 -- entity. Otherwise, the nature of the unit is now a package
4199 if Nkind (Parent (N)) = N_Compilation_Unit
4200 and then not Needs_Body
4202 Rewrite (N, Act_Decl);
4205 if Present (Corresponding_Body (Gen_Decl))
4206 or else Unit_Requires_Body (Gen_Unit)
4208 Set_Has_Completion (Act_Decl_Id);
4211 Check_Formal_Packages (Act_Decl_Id);
4213 Restore_Hidden_Primitives (Vis_Prims_List);
4214 Restore_Private_Views (Act_Decl_Id);
4216 Inherit_Context (Gen_Decl, N);
4218 if Parent_Installed then
4223 Env_Installed := False;
4226 Validate_Categorization_Dependency (N, Act_Decl_Id);
4228 -- There used to be a check here to prevent instantiations in local
4229 -- contexts if the No_Local_Allocators restriction was active. This
4230 -- check was removed by a binding interpretation in AI-95-00130/07,
4231 -- but we retain the code for documentation purposes.
4233 -- if Ekind (Act_Decl_Id) /= E_Void
4234 -- and then not Is_Library_Level_Entity (Act_Decl_Id)
4236 -- Check_Restriction (No_Local_Allocators, N);
4240 Inline_Instance_Body (N, Gen_Unit, Act_Decl);
4243 -- The following is a tree patch for ASIS: ASIS needs separate nodes to
4244 -- be used as defining identifiers for a formal package and for the
4245 -- corresponding expanded package.
4247 if Nkind (N) = N_Formal_Package_Declaration then
4248 Act_Decl_Id := New_Copy (Defining_Entity (N));
4249 Set_Comes_From_Source (Act_Decl_Id, True);
4250 Set_Is_Generic_Instance (Act_Decl_Id, False);
4251 Set_Defining_Identifier (N, Act_Decl_Id);
4254 Style_Check := Save_Style_Check;
4256 -- Check that if N is an instantiation of System.Dim_Float_IO or
4257 -- System.Dim_Integer_IO, the formal type has a dimension system.
4259 if Nkind (N) = N_Package_Instantiation
4260 and then Is_Dim_IO_Package_Instantiation (N)
4263 Assoc : constant Node_Id := First (Generic_Associations (N));
4265 if not Has_Dimension_System
4266 (Etype (Explicit_Generic_Actual_Parameter (Assoc)))
4268 Error_Msg_N ("type with a dimension system expected", Assoc);
4274 if Has_Aspects (N) and then Nkind (Parent (N)) /= N_Compilation_Unit then
4275 Analyze_Aspect_Specifications (N, Act_Decl_Id);
4279 when Instantiation_Error =>
4280 if Parent_Installed then
4284 if Env_Installed then
4288 Style_Check := Save_Style_Check;
4289 end Analyze_Package_Instantiation;
4291 --------------------------
4292 -- Inline_Instance_Body --
4293 --------------------------
4295 procedure Inline_Instance_Body
4297 Gen_Unit : Entity_Id;
4301 Gen_Comp : constant Entity_Id :=
4302 Cunit_Entity (Get_Source_Unit (Gen_Unit));
4303 Curr_Comp : constant Node_Id := Cunit (Current_Sem_Unit);
4304 Curr_Scope : Entity_Id := Empty;
4305 Curr_Unit : constant Entity_Id := Cunit_Entity (Current_Sem_Unit);
4306 Removed : Boolean := False;
4307 Num_Scopes : Int := 0;
4309 Scope_Stack_Depth : constant Int :=
4310 Scope_Stack.Last - Scope_Stack.First + 1;
4312 Use_Clauses : array (1 .. Scope_Stack_Depth) of Node_Id;
4313 Instances : array (1 .. Scope_Stack_Depth) of Entity_Id;
4314 Inner_Scopes : array (1 .. Scope_Stack_Depth) of Entity_Id;
4316 Num_Inner : Int := 0;
4317 N_Instances : Int := 0;
4321 -- Case of generic unit defined in another unit. We must remove the
4322 -- complete context of the current unit to install that of the generic.
4324 if Gen_Comp /= Cunit_Entity (Current_Sem_Unit) then
4326 -- Add some comments for the following two loops ???
4329 while Present (S) and then S /= Standard_Standard loop
4331 Num_Scopes := Num_Scopes + 1;
4333 Use_Clauses (Num_Scopes) :=
4335 (Scope_Stack.Last - Num_Scopes + 1).
4337 End_Use_Clauses (Use_Clauses (Num_Scopes));
4339 exit when Scope_Stack.Last - Num_Scopes + 1 = Scope_Stack.First
4340 or else Scope_Stack.Table
4341 (Scope_Stack.Last - Num_Scopes).Entity
4345 exit when Is_Generic_Instance (S)
4346 and then (In_Package_Body (S)
4347 or else Ekind (S) = E_Procedure
4348 or else Ekind (S) = E_Function);
4352 Vis := Is_Immediately_Visible (Gen_Comp);
4354 -- Find and save all enclosing instances
4359 and then S /= Standard_Standard
4361 if Is_Generic_Instance (S) then
4362 N_Instances := N_Instances + 1;
4363 Instances (N_Instances) := S;
4365 exit when In_Package_Body (S);
4371 -- Remove context of current compilation unit, unless we are within a
4372 -- nested package instantiation, in which case the context has been
4373 -- removed previously.
4375 -- If current scope is the body of a child unit, remove context of
4376 -- spec as well. If an enclosing scope is an instance body, the
4377 -- context has already been removed, but the entities in the body
4378 -- must be made invisible as well.
4383 and then S /= Standard_Standard
4385 if Is_Generic_Instance (S)
4386 and then (In_Package_Body (S)
4387 or else Ekind (S) = E_Procedure
4388 or else Ekind (S) = E_Function)
4390 -- We still have to remove the entities of the enclosing
4391 -- instance from direct visibility.
4396 E := First_Entity (S);
4397 while Present (E) loop
4398 Set_Is_Immediately_Visible (E, False);
4407 or else (Ekind (Curr_Unit) = E_Package_Body
4408 and then S = Spec_Entity (Curr_Unit))
4409 or else (Ekind (Curr_Unit) = E_Subprogram_Body
4412 (Unit_Declaration_Node (Curr_Unit)))
4416 -- Remove entities in current scopes from visibility, so that
4417 -- instance body is compiled in a clean environment.
4419 List := Save_Scope_Stack (Handle_Use => False);
4421 if Is_Child_Unit (S) then
4423 -- Remove child unit from stack, as well as inner scopes.
4424 -- Removing the context of a child unit removes parent units
4427 while Current_Scope /= S loop
4428 Num_Inner := Num_Inner + 1;
4429 Inner_Scopes (Num_Inner) := Current_Scope;
4434 Remove_Context (Curr_Comp);
4438 Remove_Context (Curr_Comp);
4441 if Ekind (Curr_Unit) = E_Package_Body then
4442 Remove_Context (Library_Unit (Curr_Comp));
4448 pragma Assert (Num_Inner < Num_Scopes);
4450 Push_Scope (Standard_Standard);
4451 Scope_Stack.Table (Scope_Stack.Last).Is_Active_Stack_Base := True;
4452 Instantiate_Package_Body
4455 Act_Decl => Act_Decl,
4456 Expander_Status => Expander_Active,
4457 Current_Sem_Unit => Current_Sem_Unit,
4458 Scope_Suppress => Scope_Suppress,
4459 Local_Suppress_Stack_Top => Local_Suppress_Stack_Top,
4460 Version => Ada_Version,
4461 Version_Pragma => Ada_Version_Pragma,
4462 Warnings => Save_Warnings,
4463 SPARK_Mode => SPARK_Mode,
4464 SPARK_Mode_Pragma => SPARK_Mode_Pragma)),
4465 Inlined_Body => True);
4471 Set_Is_Immediately_Visible (Gen_Comp, Vis);
4473 -- Reset Generic_Instance flag so that use clauses can be installed
4474 -- in the proper order. (See Use_One_Package for effect of enclosing
4475 -- instances on processing of use clauses).
4477 for J in 1 .. N_Instances loop
4478 Set_Is_Generic_Instance (Instances (J), False);
4482 Install_Context (Curr_Comp);
4484 if Present (Curr_Scope)
4485 and then Is_Child_Unit (Curr_Scope)
4487 Push_Scope (Curr_Scope);
4488 Set_Is_Immediately_Visible (Curr_Scope);
4490 -- Finally, restore inner scopes as well
4492 for J in reverse 1 .. Num_Inner loop
4493 Push_Scope (Inner_Scopes (J));
4497 Restore_Scope_Stack (List, Handle_Use => False);
4499 if Present (Curr_Scope)
4501 (In_Private_Part (Curr_Scope)
4502 or else In_Package_Body (Curr_Scope))
4504 -- Install private declaration of ancestor units, which are
4505 -- currently available. Restore_Scope_Stack and Install_Context
4506 -- only install the visible part of parents.
4511 Par := Scope (Curr_Scope);
4512 while (Present (Par))
4513 and then Par /= Standard_Standard
4515 Install_Private_Declarations (Par);
4522 -- Restore use clauses. For a child unit, use clauses in the parents
4523 -- are restored when installing the context, so only those in inner
4524 -- scopes (and those local to the child unit itself) need to be
4525 -- installed explicitly.
4527 if Is_Child_Unit (Curr_Unit)
4530 for J in reverse 1 .. Num_Inner + 1 loop
4531 Scope_Stack.Table (Scope_Stack.Last - J + 1).First_Use_Clause :=
4533 Install_Use_Clauses (Use_Clauses (J));
4537 for J in reverse 1 .. Num_Scopes loop
4538 Scope_Stack.Table (Scope_Stack.Last - J + 1).First_Use_Clause :=
4540 Install_Use_Clauses (Use_Clauses (J));
4544 -- Restore status of instances. If one of them is a body, make its
4545 -- local entities visible again.
4552 for J in 1 .. N_Instances loop
4553 Inst := Instances (J);
4554 Set_Is_Generic_Instance (Inst, True);
4556 if In_Package_Body (Inst)
4557 or else Ekind (S) = E_Procedure
4558 or else Ekind (S) = E_Function
4560 E := First_Entity (Instances (J));
4561 while Present (E) loop
4562 Set_Is_Immediately_Visible (E);
4569 -- If generic unit is in current unit, current context is correct
4572 Instantiate_Package_Body
4575 Act_Decl => Act_Decl,
4576 Expander_Status => Expander_Active,
4577 Current_Sem_Unit => Current_Sem_Unit,
4578 Scope_Suppress => Scope_Suppress,
4579 Local_Suppress_Stack_Top => Local_Suppress_Stack_Top,
4580 Version => Ada_Version,
4581 Version_Pragma => Ada_Version_Pragma,
4582 Warnings => Save_Warnings,
4583 SPARK_Mode => SPARK_Mode,
4584 SPARK_Mode_Pragma => SPARK_Mode_Pragma)),
4585 Inlined_Body => True);
4587 end Inline_Instance_Body;
4589 -------------------------------------
4590 -- Analyze_Procedure_Instantiation --
4591 -------------------------------------
4593 procedure Analyze_Procedure_Instantiation (N : Node_Id) is
4595 Analyze_Subprogram_Instantiation (N, E_Procedure);
4596 end Analyze_Procedure_Instantiation;
4598 -----------------------------------
4599 -- Need_Subprogram_Instance_Body --
4600 -----------------------------------
4602 function Need_Subprogram_Instance_Body
4604 Subp : Entity_Id) return Boolean
4607 -- Must be inlined (or inlined renaming)
4609 if (Is_In_Main_Unit (N)
4610 or else Is_Inlined (Subp)
4611 or else Is_Inlined (Alias (Subp)))
4613 -- Must be generating code or analyzing code in ASIS/GNATprove mode
4615 and then (Operating_Mode = Generate_Code
4616 or else (Operating_Mode = Check_Semantics
4617 and then (ASIS_Mode or GNATprove_Mode)))
4619 -- The body is needed when generating code (full expansion), in ASIS
4620 -- mode for other tools, and in GNATprove mode (special expansion) for
4621 -- formal verification of the body itself.
4623 and then (Expander_Active or ASIS_Mode or GNATprove_Mode)
4625 -- No point in inlining if ABE is inevitable
4627 and then not ABE_Is_Certain (N)
4629 -- Or if subprogram is eliminated
4631 and then not Is_Eliminated (Subp)
4633 Pending_Instantiations.Append
4635 Act_Decl => Unit_Declaration_Node (Subp),
4636 Expander_Status => Expander_Active,
4637 Current_Sem_Unit => Current_Sem_Unit,
4638 Scope_Suppress => Scope_Suppress,
4639 Local_Suppress_Stack_Top => Local_Suppress_Stack_Top,
4640 Version => Ada_Version,
4641 Version_Pragma => Ada_Version_Pragma,
4642 Warnings => Save_Warnings,
4643 SPARK_Mode => SPARK_Mode,
4644 SPARK_Mode_Pragma => SPARK_Mode_Pragma));
4647 -- Here if not inlined, or we ignore the inlining
4652 end Need_Subprogram_Instance_Body;
4654 --------------------------------------
4655 -- Analyze_Subprogram_Instantiation --
4656 --------------------------------------
4658 procedure Analyze_Subprogram_Instantiation
4662 Loc : constant Source_Ptr := Sloc (N);
4663 Gen_Id : constant Node_Id := Name (N);
4665 Anon_Id : constant Entity_Id :=
4666 Make_Defining_Identifier (Sloc (Defining_Entity (N)),
4667 Chars => New_External_Name
4668 (Chars (Defining_Entity (N)), 'R'));
4670 Act_Decl_Id : Entity_Id;
4675 Env_Installed : Boolean := False;
4676 Gen_Unit : Entity_Id;
4678 Pack_Id : Entity_Id;
4679 Parent_Installed : Boolean := False;
4680 Renaming_List : List_Id;
4682 procedure Analyze_Instance_And_Renamings;
4683 -- The instance must be analyzed in a context that includes the mappings
4684 -- of generic parameters into actuals. We create a package declaration
4685 -- for this purpose, and a subprogram with an internal name within the
4686 -- package. The subprogram instance is simply an alias for the internal
4687 -- subprogram, declared in the current scope.
4689 ------------------------------------
4690 -- Analyze_Instance_And_Renamings --
4691 ------------------------------------
4693 procedure Analyze_Instance_And_Renamings is
4694 Def_Ent : constant Entity_Id := Defining_Entity (N);
4695 Pack_Decl : Node_Id;
4698 if Nkind (Parent (N)) = N_Compilation_Unit then
4700 -- For the case of a compilation unit, the container package has
4701 -- the same name as the instantiation, to insure that the binder
4702 -- calls the elaboration procedure with the right name. Copy the
4703 -- entity of the instance, which may have compilation level flags
4704 -- (e.g. Is_Child_Unit) set.
4706 Pack_Id := New_Copy (Def_Ent);
4709 -- Otherwise we use the name of the instantiation concatenated
4710 -- with its source position to ensure uniqueness if there are
4711 -- several instantiations with the same name.
4714 Make_Defining_Identifier (Loc,
4715 Chars => New_External_Name
4716 (Related_Id => Chars (Def_Ent),
4718 Suffix_Index => Source_Offset (Sloc (Def_Ent))));
4721 Pack_Decl := Make_Package_Declaration (Loc,
4722 Specification => Make_Package_Specification (Loc,
4723 Defining_Unit_Name => Pack_Id,
4724 Visible_Declarations => Renaming_List,
4725 End_Label => Empty));
4727 Set_Instance_Spec (N, Pack_Decl);
4728 Set_Is_Generic_Instance (Pack_Id);
4729 Set_Debug_Info_Needed (Pack_Id);
4731 -- Case of not a compilation unit
4733 if Nkind (Parent (N)) /= N_Compilation_Unit then
4734 Mark_Rewrite_Insertion (Pack_Decl);
4735 Insert_Before (N, Pack_Decl);
4736 Set_Has_Completion (Pack_Id);
4738 -- Case of an instantiation that is a compilation unit
4740 -- Place declaration on current node so context is complete for
4741 -- analysis (including nested instantiations), and for use in a
4742 -- context_clause (see Analyze_With_Clause).
4745 Set_Unit (Parent (N), Pack_Decl);
4746 Set_Parent_Spec (Pack_Decl, Parent_Spec (N));
4749 Analyze (Pack_Decl);
4750 Check_Formal_Packages (Pack_Id);
4751 Set_Is_Generic_Instance (Pack_Id, False);
4753 -- Why do we clear Is_Generic_Instance??? We set it 20 lines
4756 -- Body of the enclosing package is supplied when instantiating the
4757 -- subprogram body, after semantic analysis is completed.
4759 if Nkind (Parent (N)) = N_Compilation_Unit then
4761 -- Remove package itself from visibility, so it does not
4762 -- conflict with subprogram.
4764 Set_Name_Entity_Id (Chars (Pack_Id), Homonym (Pack_Id));
4766 -- Set name and scope of internal subprogram so that the proper
4767 -- external name will be generated. The proper scope is the scope
4768 -- of the wrapper package. We need to generate debugging info for
4769 -- the internal subprogram, so set flag accordingly.
4771 Set_Chars (Anon_Id, Chars (Defining_Entity (N)));
4772 Set_Scope (Anon_Id, Scope (Pack_Id));
4774 -- Mark wrapper package as referenced, to avoid spurious warnings
4775 -- if the instantiation appears in various with_ clauses of
4776 -- subunits of the main unit.
4778 Set_Referenced (Pack_Id);
4781 Set_Is_Generic_Instance (Anon_Id);
4782 Set_Debug_Info_Needed (Anon_Id);
4783 Act_Decl_Id := New_Copy (Anon_Id);
4785 Set_Parent (Act_Decl_Id, Parent (Anon_Id));
4786 Set_Chars (Act_Decl_Id, Chars (Defining_Entity (N)));
4787 Set_Sloc (Act_Decl_Id, Sloc (Defining_Entity (N)));
4788 Set_Comes_From_Source (Act_Decl_Id, True);
4790 -- The signature may involve types that are not frozen yet, but the
4791 -- subprogram will be frozen at the point the wrapper package is
4792 -- frozen, so it does not need its own freeze node. In fact, if one
4793 -- is created, it might conflict with the freezing actions from the
4796 Set_Has_Delayed_Freeze (Anon_Id, False);
4798 -- If the instance is a child unit, mark the Id accordingly. Mark
4799 -- the anonymous entity as well, which is the real subprogram and
4800 -- which is used when the instance appears in a context clause.
4801 -- Similarly, propagate the Is_Eliminated flag to handle properly
4802 -- nested eliminated subprograms.
4804 Set_Is_Child_Unit (Act_Decl_Id, Is_Child_Unit (Defining_Entity (N)));
4805 Set_Is_Child_Unit (Anon_Id, Is_Child_Unit (Defining_Entity (N)));
4806 New_Overloaded_Entity (Act_Decl_Id);
4807 Check_Eliminated (Act_Decl_Id);
4808 Set_Is_Eliminated (Anon_Id, Is_Eliminated (Act_Decl_Id));
4810 -- In compilation unit case, kill elaboration checks on the
4811 -- instantiation, since they are never needed -- the body is
4812 -- instantiated at the same point as the spec.
4814 if Nkind (Parent (N)) = N_Compilation_Unit then
4815 Set_Suppress_Elaboration_Warnings (Act_Decl_Id);
4816 Set_Kill_Elaboration_Checks (Act_Decl_Id);
4817 Set_Is_Compilation_Unit (Anon_Id);
4819 Set_Cunit_Entity (Current_Sem_Unit, Pack_Id);
4822 -- The instance is not a freezing point for the new subprogram
4824 Set_Is_Frozen (Act_Decl_Id, False);
4826 if Nkind (Defining_Entity (N)) = N_Defining_Operator_Symbol then
4827 Valid_Operator_Definition (Act_Decl_Id);
4830 Set_Alias (Act_Decl_Id, Anon_Id);
4831 Set_Parent (Act_Decl_Id, Parent (Anon_Id));
4832 Set_Has_Completion (Act_Decl_Id);
4833 Set_Related_Instance (Pack_Id, Act_Decl_Id);
4835 if Nkind (Parent (N)) = N_Compilation_Unit then
4836 Set_Body_Required (Parent (N), False);
4838 end Analyze_Instance_And_Renamings;
4842 Vis_Prims_List : Elist_Id := No_Elist;
4843 -- List of primitives made temporarily visible in the instantiation
4844 -- to match the visibility of the formal type
4846 -- Start of processing for Analyze_Subprogram_Instantiation
4849 Check_SPARK_Restriction ("generic is not allowed", N);
4851 -- Very first thing: check for special Text_IO unit in case we are
4852 -- instantiating one of the children of [[Wide_]Wide_]Text_IO. Of course
4853 -- such an instantiation is bogus (these are packages, not subprograms),
4854 -- but we get a better error message if we do this.
4856 Check_Text_IO_Special_Unit (Gen_Id);
4858 -- Make node global for error reporting
4860 Instantiation_Node := N;
4862 -- For package instantiations we turn off style checks, because they
4863 -- will have been emitted in the generic. For subprogram instantiations
4864 -- we want to apply at least the check on overriding indicators so we
4865 -- do not modify the style check status.
4867 -- The renaming declarations for the actuals do not come from source and
4868 -- will not generate spurious warnings.
4870 Preanalyze_Actuals (N);
4873 Env_Installed := True;
4874 Check_Generic_Child_Unit (Gen_Id, Parent_Installed);
4875 Gen_Unit := Entity (Gen_Id);
4877 Generate_Reference (Gen_Unit, Gen_Id);
4879 if Nkind (Gen_Id) = N_Identifier
4880 and then Chars (Gen_Unit) = Chars (Defining_Entity (N))
4883 ("& is hidden within declaration of instance", Gen_Id, Gen_Unit);
4886 if Etype (Gen_Unit) = Any_Type then
4891 -- Verify that it is a generic subprogram of the right kind, and that
4892 -- it does not lead to a circular instantiation.
4894 if K = E_Procedure and then Ekind (Gen_Unit) /= E_Generic_Procedure then
4896 ("& is not the name of a generic procedure", Gen_Id, Gen_Unit);
4898 elsif K = E_Function and then Ekind (Gen_Unit) /= E_Generic_Function then
4900 ("& is not the name of a generic function", Gen_Id, Gen_Unit);
4902 elsif In_Open_Scopes (Gen_Unit) then
4903 Error_Msg_NE ("instantiation of & within itself", N, Gen_Unit);
4906 Set_Entity (Gen_Id, Gen_Unit);
4907 Set_Is_Instantiated (Gen_Unit);
4909 if In_Extended_Main_Source_Unit (N) then
4910 Generate_Reference (Gen_Unit, N);
4913 -- If renaming, get original unit
4915 if Present (Renamed_Object (Gen_Unit))
4916 and then (Ekind (Renamed_Object (Gen_Unit)) = E_Generic_Procedure
4918 Ekind (Renamed_Object (Gen_Unit)) = E_Generic_Function)
4920 Gen_Unit := Renamed_Object (Gen_Unit);
4921 Set_Is_Instantiated (Gen_Unit);
4922 Generate_Reference (Gen_Unit, N);
4925 if Contains_Instance_Of (Gen_Unit, Current_Scope, Gen_Id) then
4926 Error_Msg_Node_2 := Current_Scope;
4928 ("circular Instantiation: & instantiated in &!", N, Gen_Unit);
4929 Circularity_Detected := True;
4930 Restore_Hidden_Primitives (Vis_Prims_List);
4934 Gen_Decl := Unit_Declaration_Node (Gen_Unit);
4936 -- Initialize renamings map, for error checking
4938 Generic_Renamings.Set_Last (0);
4939 Generic_Renamings_HTable.Reset;
4941 Create_Instantiation_Source (N, Gen_Unit, False, S_Adjustment);
4943 -- Copy original generic tree, to produce text for instantiation
4947 (Original_Node (Gen_Decl), Empty, Instantiating => True);
4949 -- Inherit overriding indicator from instance node
4951 Act_Spec := Specification (Act_Tree);
4952 Set_Must_Override (Act_Spec, Must_Override (N));
4953 Set_Must_Not_Override (Act_Spec, Must_Not_Override (N));
4956 Analyze_Associations
4958 Formals => Generic_Formal_Declarations (Act_Tree),
4959 F_Copy => Generic_Formal_Declarations (Gen_Decl));
4961 Vis_Prims_List := Check_Hidden_Primitives (Renaming_List);
4963 -- The subprogram itself cannot contain a nested instance, so the
4964 -- current parent is left empty.
4966 Set_Instance_Env (Gen_Unit, Empty);
4968 -- Build the subprogram declaration, which does not appear in the
4969 -- generic template, and give it a sloc consistent with that of the
4972 Set_Defining_Unit_Name (Act_Spec, Anon_Id);
4973 Set_Generic_Parent (Act_Spec, Gen_Unit);
4975 Make_Subprogram_Declaration (Sloc (Act_Spec),
4976 Specification => Act_Spec);
4978 -- The aspects have been copied previously, but they have to be
4979 -- linked explicitly to the new subprogram declaration. Explicit
4980 -- pre/postconditions on the instance are analyzed below, in a
4983 Move_Aspects (Act_Tree, To => Act_Decl);
4984 Set_Categorization_From_Pragmas (Act_Decl);
4986 if Parent_Installed then
4990 Append (Act_Decl, Renaming_List);
4991 Analyze_Instance_And_Renamings;
4993 -- If the generic is marked Import (Intrinsic), then so is the
4994 -- instance. This indicates that there is no body to instantiate. If
4995 -- generic is marked inline, so it the instance, and the anonymous
4996 -- subprogram it renames. If inlined, or else if inlining is enabled
4997 -- for the compilation, we generate the instance body even if it is
4998 -- not within the main unit.
5000 if Is_Intrinsic_Subprogram (Gen_Unit) then
5001 Set_Is_Intrinsic_Subprogram (Anon_Id);
5002 Set_Is_Intrinsic_Subprogram (Act_Decl_Id);
5004 if Chars (Gen_Unit) = Name_Unchecked_Conversion then
5005 Validate_Unchecked_Conversion (N, Act_Decl_Id);
5009 -- Inherit convention from generic unit. Intrinsic convention, as for
5010 -- an instance of unchecked conversion, is not inherited because an
5011 -- explicit Ada instance has been created.
5013 if Has_Convention_Pragma (Gen_Unit)
5014 and then Convention (Gen_Unit) /= Convention_Intrinsic
5016 Set_Convention (Act_Decl_Id, Convention (Gen_Unit));
5017 Set_Is_Exported (Act_Decl_Id, Is_Exported (Gen_Unit));
5020 Generate_Definition (Act_Decl_Id);
5021 -- Set_Contract (Anon_Id, Make_Contract (Sloc (Anon_Id)));
5023 Set_Contract (Act_Decl_Id, Make_Contract (Sloc (Act_Decl_Id)));
5025 -- Inherit all inlining-related flags which apply to the generic in
5026 -- the subprogram and its declaration.
5028 Set_Is_Inlined (Act_Decl_Id, Is_Inlined (Gen_Unit));
5029 Set_Is_Inlined (Anon_Id, Is_Inlined (Gen_Unit));
5031 Set_Has_Pragma_Inline (Act_Decl_Id, Has_Pragma_Inline (Gen_Unit));
5032 Set_Has_Pragma_Inline (Anon_Id, Has_Pragma_Inline (Gen_Unit));
5034 Set_Has_Pragma_Inline_Always
5035 (Act_Decl_Id, Has_Pragma_Inline_Always (Gen_Unit));
5036 Set_Has_Pragma_Inline_Always
5037 (Anon_Id, Has_Pragma_Inline_Always (Gen_Unit));
5039 if not Is_Intrinsic_Subprogram (Gen_Unit) then
5040 Check_Elab_Instantiation (N);
5043 if Is_Dispatching_Operation (Act_Decl_Id)
5044 and then Ada_Version >= Ada_2005
5050 Formal := First_Formal (Act_Decl_Id);
5051 while Present (Formal) loop
5052 if Ekind (Etype (Formal)) = E_Anonymous_Access_Type
5053 and then Is_Controlling_Formal (Formal)
5054 and then not Can_Never_Be_Null (Formal)
5056 Error_Msg_NE ("access parameter& is controlling,",
5059 ("\corresponding parameter of & must be"
5060 & " explicitly null-excluding", N, Gen_Id);
5063 Next_Formal (Formal);
5068 Check_Hidden_Child_Unit (N, Gen_Unit, Act_Decl_Id);
5070 Validate_Categorization_Dependency (N, Act_Decl_Id);
5072 if not Is_Intrinsic_Subprogram (Act_Decl_Id) then
5073 Inherit_Context (Gen_Decl, N);
5075 Restore_Private_Views (Pack_Id, False);
5077 -- If the context requires a full instantiation, mark node for
5078 -- subsequent construction of the body.
5080 if Need_Subprogram_Instance_Body (N, Act_Decl_Id) then
5081 Check_Forward_Instantiation (Gen_Decl);
5083 -- The wrapper package is always delayed, because it does not
5084 -- constitute a freeze point, but to insure that the freeze
5085 -- node is placed properly, it is created directly when
5086 -- instantiating the body (otherwise the freeze node might
5087 -- appear to early for nested instantiations).
5089 elsif Nkind (Parent (N)) = N_Compilation_Unit then
5091 -- For ASIS purposes, indicate that the wrapper package has
5092 -- replaced the instantiation node.
5094 Rewrite (N, Unit (Parent (N)));
5095 Set_Unit (Parent (N), N);
5098 elsif Nkind (Parent (N)) = N_Compilation_Unit then
5100 -- Replace instance node for library-level instantiations of
5101 -- intrinsic subprograms, for ASIS use.
5103 Rewrite (N, Unit (Parent (N)));
5104 Set_Unit (Parent (N), N);
5107 if Parent_Installed then
5111 Restore_Hidden_Primitives (Vis_Prims_List);
5113 Env_Installed := False;
5114 Generic_Renamings.Set_Last (0);
5115 Generic_Renamings_HTable.Reset;
5119 if Has_Aspects (N) then
5120 Analyze_Aspect_Specifications (N, Act_Decl_Id);
5124 when Instantiation_Error =>
5125 if Parent_Installed then
5129 if Env_Installed then
5132 end Analyze_Subprogram_Instantiation;
5134 -------------------------
5135 -- Get_Associated_Node --
5136 -------------------------
5138 function Get_Associated_Node (N : Node_Id) return Node_Id is
5142 Assoc := Associated_Node (N);
5144 if Nkind (Assoc) /= Nkind (N) then
5147 elsif Nkind_In (Assoc, N_Aggregate, N_Extension_Aggregate) then
5151 -- If the node is part of an inner generic, it may itself have been
5152 -- remapped into a further generic copy. Associated_Node is otherwise
5153 -- used for the entity of the node, and will be of a different node
5154 -- kind, or else N has been rewritten as a literal or function call.
5156 while Present (Associated_Node (Assoc))
5157 and then Nkind (Associated_Node (Assoc)) = Nkind (Assoc)
5159 Assoc := Associated_Node (Assoc);
5162 -- Follow and additional link in case the final node was rewritten.
5163 -- This can only happen with nested generic units.
5165 if (Nkind (Assoc) = N_Identifier or else Nkind (Assoc) in N_Op)
5166 and then Present (Associated_Node (Assoc))
5167 and then (Nkind_In (Associated_Node (Assoc), N_Function_Call,
5168 N_Explicit_Dereference,
5173 Assoc := Associated_Node (Assoc);
5176 -- An additional special case: an unconstrained type in an object
5177 -- declaration may have been rewritten as a local subtype constrained
5178 -- by the expression in the declaration. We need to recover the
5179 -- original entity which may be global.
5181 if Present (Original_Node (Assoc))
5182 and then Nkind (Parent (N)) = N_Object_Declaration
5184 Assoc := Original_Node (Assoc);
5189 end Get_Associated_Node;
5191 -------------------------------------------
5192 -- Build_Instance_Compilation_Unit_Nodes --
5193 -------------------------------------------
5195 procedure Build_Instance_Compilation_Unit_Nodes
5200 Decl_Cunit : Node_Id;
5201 Body_Cunit : Node_Id;
5203 New_Main : constant Entity_Id := Defining_Entity (Act_Decl);
5204 Old_Main : constant Entity_Id := Cunit_Entity (Main_Unit);
5207 -- A new compilation unit node is built for the instance declaration
5210 Make_Compilation_Unit (Sloc (N),
5211 Context_Items => Empty_List,
5213 Aux_Decls_Node => Make_Compilation_Unit_Aux (Sloc (N)));
5215 Set_Parent_Spec (Act_Decl, Parent_Spec (N));
5217 -- The new compilation unit is linked to its body, but both share the
5218 -- same file, so we do not set Body_Required on the new unit so as not
5219 -- to create a spurious dependency on a non-existent body in the ali.
5220 -- This simplifies CodePeer unit traversal.
5222 -- We use the original instantiation compilation unit as the resulting
5223 -- compilation unit of the instance, since this is the main unit.
5225 Rewrite (N, Act_Body);
5227 -- Propagate the aspect specifications from the package body template to
5228 -- the instantiated version of the package body.
5230 if Has_Aspects (Act_Body) then
5231 Set_Aspect_Specifications
5232 (N, New_Copy_List_Tree (Aspect_Specifications (Act_Body)));
5235 Body_Cunit := Parent (N);
5237 -- The two compilation unit nodes are linked by the Library_Unit field
5239 Set_Library_Unit (Decl_Cunit, Body_Cunit);
5240 Set_Library_Unit (Body_Cunit, Decl_Cunit);
5242 -- Preserve the private nature of the package if needed
5244 Set_Private_Present (Decl_Cunit, Private_Present (Body_Cunit));
5246 -- If the instance is not the main unit, its context, categorization
5247 -- and elaboration entity are not relevant to the compilation.
5249 if Body_Cunit /= Cunit (Main_Unit) then
5250 Make_Instance_Unit (Body_Cunit, In_Main => False);
5254 -- The context clause items on the instantiation, which are now attached
5255 -- to the body compilation unit (since the body overwrote the original
5256 -- instantiation node), semantically belong on the spec, so copy them
5257 -- there. It's harmless to leave them on the body as well. In fact one
5258 -- could argue that they belong in both places.
5260 Citem := First (Context_Items (Body_Cunit));
5261 while Present (Citem) loop
5262 Append (New_Copy (Citem), Context_Items (Decl_Cunit));
5266 -- Propagate categorization flags on packages, so that they appear in
5267 -- the ali file for the spec of the unit.
5269 if Ekind (New_Main) = E_Package then
5270 Set_Is_Pure (Old_Main, Is_Pure (New_Main));
5271 Set_Is_Preelaborated (Old_Main, Is_Preelaborated (New_Main));
5272 Set_Is_Remote_Types (Old_Main, Is_Remote_Types (New_Main));
5273 Set_Is_Shared_Passive (Old_Main, Is_Shared_Passive (New_Main));
5274 Set_Is_Remote_Call_Interface
5275 (Old_Main, Is_Remote_Call_Interface (New_Main));
5278 -- Make entry in Units table, so that binder can generate call to
5279 -- elaboration procedure for body, if any.
5281 Make_Instance_Unit (Body_Cunit, In_Main => True);
5282 Main_Unit_Entity := New_Main;
5283 Set_Cunit_Entity (Main_Unit, Main_Unit_Entity);
5285 -- Build elaboration entity, since the instance may certainly generate
5286 -- elaboration code requiring a flag for protection.
5288 Build_Elaboration_Entity (Decl_Cunit, New_Main);
5289 end Build_Instance_Compilation_Unit_Nodes;
5291 -----------------------------
5292 -- Check_Access_Definition --
5293 -----------------------------
5295 procedure Check_Access_Definition (N : Node_Id) is
5298 (Ada_Version >= Ada_2005 and then Present (Access_Definition (N)));
5300 end Check_Access_Definition;
5302 -----------------------------------
5303 -- Check_Formal_Package_Instance --
5304 -----------------------------------
5306 -- If the formal has specific parameters, they must match those of the
5307 -- actual. Both of them are instances, and the renaming declarations for
5308 -- their formal parameters appear in the same order in both. The analyzed
5309 -- formal has been analyzed in the context of the current instance.
5311 procedure Check_Formal_Package_Instance
5312 (Formal_Pack : Entity_Id;
5313 Actual_Pack : Entity_Id)
5315 E1 : Entity_Id := First_Entity (Actual_Pack);
5316 E2 : Entity_Id := First_Entity (Formal_Pack);
5321 procedure Check_Mismatch (B : Boolean);
5322 -- Common error routine for mismatch between the parameters of the
5323 -- actual instance and those of the formal package.
5325 function Same_Instantiated_Constant (E1, E2 : Entity_Id) return Boolean;
5326 -- The formal may come from a nested formal package, and the actual may
5327 -- have been constant-folded. To determine whether the two denote the
5328 -- same entity we may have to traverse several definitions to recover
5329 -- the ultimate entity that they refer to.
5331 function Same_Instantiated_Variable (E1, E2 : Entity_Id) return Boolean;
5332 -- Similarly, if the formal comes from a nested formal package, the
5333 -- actual may designate the formal through multiple renamings, which
5334 -- have to be followed to determine the original variable in question.
5336 --------------------
5337 -- Check_Mismatch --
5338 --------------------
5340 procedure Check_Mismatch (B : Boolean) is
5341 Kind : constant Node_Kind := Nkind (Parent (E2));
5344 if Kind = N_Formal_Type_Declaration then
5347 elsif Nkind_In (Kind, N_Formal_Object_Declaration,
5348 N_Formal_Package_Declaration)
5349 or else Kind in N_Formal_Subprogram_Declaration
5355 ("actual for & in actual instance does not match formal",
5356 Parent (Actual_Pack), E1);
5360 --------------------------------
5361 -- Same_Instantiated_Constant --
5362 --------------------------------
5364 function Same_Instantiated_Constant
5365 (E1, E2 : Entity_Id) return Boolean
5371 while Present (Ent) loop
5375 elsif Ekind (Ent) /= E_Constant then
5378 elsif Is_Entity_Name (Constant_Value (Ent)) then
5379 if Entity (Constant_Value (Ent)) = E1 then
5382 Ent := Entity (Constant_Value (Ent));
5385 -- The actual may be a constant that has been folded. Recover
5388 elsif Is_Entity_Name (Original_Node (Constant_Value (Ent))) then
5389 Ent := Entity (Original_Node (Constant_Value (Ent)));
5396 end Same_Instantiated_Constant;
5398 --------------------------------
5399 -- Same_Instantiated_Variable --
5400 --------------------------------
5402 function Same_Instantiated_Variable
5403 (E1, E2 : Entity_Id) return Boolean
5405 function Original_Entity (E : Entity_Id) return Entity_Id;
5406 -- Follow chain of renamings to the ultimate ancestor
5408 ---------------------
5409 -- Original_Entity --
5410 ---------------------
5412 function Original_Entity (E : Entity_Id) return Entity_Id is
5417 while Nkind (Parent (Orig)) = N_Object_Renaming_Declaration
5418 and then Present (Renamed_Object (Orig))
5419 and then Is_Entity_Name (Renamed_Object (Orig))
5421 Orig := Entity (Renamed_Object (Orig));
5425 end Original_Entity;
5427 -- Start of processing for Same_Instantiated_Variable
5430 return Ekind (E1) = Ekind (E2)
5431 and then Original_Entity (E1) = Original_Entity (E2);
5432 end Same_Instantiated_Variable;
5434 -- Start of processing for Check_Formal_Package_Instance
5438 and then Present (E2)
5440 exit when Ekind (E1) = E_Package
5441 and then Renamed_Entity (E1) = Renamed_Entity (Actual_Pack);
5443 -- If the formal is the renaming of the formal package, this
5444 -- is the end of its formal part, which may occur before the
5445 -- end of the formal part in the actual in the presence of
5446 -- defaulted parameters in the formal package.
5448 exit when Nkind (Parent (E2)) = N_Package_Renaming_Declaration
5449 and then Renamed_Entity (E2) = Scope (E2);
5451 -- The analysis of the actual may generate additional internal
5452 -- entities. If the formal is defaulted, there is no corresponding
5453 -- analysis and the internal entities must be skipped, until we
5454 -- find corresponding entities again.
5456 if Comes_From_Source (E2)
5457 and then not Comes_From_Source (E1)
5458 and then Chars (E1) /= Chars (E2)
5461 and then Chars (E1) /= Chars (E2)
5470 -- If the formal entity comes from a formal declaration, it was
5471 -- defaulted in the formal package, and no check is needed on it.
5473 elsif Nkind (Parent (E2)) = N_Formal_Object_Declaration then
5476 -- Ditto for defaulted formal subprograms.
5478 elsif Is_Overloadable (E1)
5479 and then Nkind (Unit_Declaration_Node (E2)) in
5480 N_Formal_Subprogram_Declaration
5484 elsif Is_Type (E1) then
5486 -- Subtypes must statically match. E1, E2 are the local entities
5487 -- that are subtypes of the actuals. Itypes generated for other
5488 -- parameters need not be checked, the check will be performed
5489 -- on the parameters themselves.
5491 -- If E2 is a formal type declaration, it is a defaulted parameter
5492 -- and needs no checking.
5494 if not Is_Itype (E1)
5495 and then not Is_Itype (E2)
5499 or else Etype (E1) /= Etype (E2)
5500 or else not Subtypes_Statically_Match (E1, E2));
5503 elsif Ekind (E1) = E_Constant then
5505 -- IN parameters must denote the same static value, or the same
5506 -- constant, or the literal null.
5508 Expr1 := Expression (Parent (E1));
5510 if Ekind (E2) /= E_Constant then
5511 Check_Mismatch (True);
5514 Expr2 := Expression (Parent (E2));
5517 if Is_OK_Static_Expression (Expr1) then
5518 if not Is_OK_Static_Expression (Expr2) then
5519 Check_Mismatch (True);
5521 elsif Is_Discrete_Type (Etype (E1)) then
5523 V1 : constant Uint := Expr_Value (Expr1);
5524 V2 : constant Uint := Expr_Value (Expr2);
5526 Check_Mismatch (V1 /= V2);
5529 elsif Is_Real_Type (Etype (E1)) then
5531 V1 : constant Ureal := Expr_Value_R (Expr1);
5532 V2 : constant Ureal := Expr_Value_R (Expr2);
5534 Check_Mismatch (V1 /= V2);
5537 elsif Is_String_Type (Etype (E1))
5538 and then Nkind (Expr1) = N_String_Literal
5540 if Nkind (Expr2) /= N_String_Literal then
5541 Check_Mismatch (True);
5544 (not String_Equal (Strval (Expr1), Strval (Expr2)));
5548 elsif Is_Entity_Name (Expr1) then
5549 if Is_Entity_Name (Expr2) then
5550 if Entity (Expr1) = Entity (Expr2) then
5554 (not Same_Instantiated_Constant
5555 (Entity (Expr1), Entity (Expr2)));
5558 Check_Mismatch (True);
5561 elsif Is_Entity_Name (Original_Node (Expr1))
5562 and then Is_Entity_Name (Expr2)
5564 Same_Instantiated_Constant
5565 (Entity (Original_Node (Expr1)), Entity (Expr2))
5569 elsif Nkind (Expr1) = N_Null then
5570 Check_Mismatch (Nkind (Expr1) /= N_Null);
5573 Check_Mismatch (True);
5576 elsif Ekind (E1) = E_Variable then
5577 Check_Mismatch (not Same_Instantiated_Variable (E1, E2));
5579 elsif Ekind (E1) = E_Package then
5581 (Ekind (E1) /= Ekind (E2)
5582 or else Renamed_Object (E1) /= Renamed_Object (E2));
5584 elsif Is_Overloadable (E1) then
5586 -- Verify that the actual subprograms match. Note that actuals
5587 -- that are attributes are rewritten as subprograms. If the
5588 -- subprogram in the formal package is defaulted, no check is
5589 -- needed. Note that this can only happen in Ada 2005 when the
5590 -- formal package can be partially parameterized.
5592 if Nkind (Unit_Declaration_Node (E1)) =
5593 N_Subprogram_Renaming_Declaration
5594 and then From_Default (Unit_Declaration_Node (E1))
5598 -- If the formal package has an "others" box association that
5599 -- covers this formal, there is no need for a check either.
5601 elsif Nkind (Unit_Declaration_Node (E2)) in
5602 N_Formal_Subprogram_Declaration
5603 and then Box_Present (Unit_Declaration_Node (E2))
5607 -- No check needed if subprogram is a defaulted null procedure
5609 elsif No (Alias (E2))
5610 and then Ekind (E2) = E_Procedure
5612 Null_Present (Specification (Unit_Declaration_Node (E2)))
5616 -- Otherwise the actual in the formal and the actual in the
5617 -- instantiation of the formal must match, up to renamings.
5621 (Ekind (E2) /= Ekind (E1) or else (Alias (E1)) /= Alias (E2));
5625 raise Program_Error;
5632 end Check_Formal_Package_Instance;
5634 ---------------------------
5635 -- Check_Formal_Packages --
5636 ---------------------------
5638 procedure Check_Formal_Packages (P_Id : Entity_Id) is
5640 Formal_P : Entity_Id;
5643 -- Iterate through the declarations in the instance, looking for package
5644 -- renaming declarations that denote instances of formal packages. Stop
5645 -- when we find the renaming of the current package itself. The
5646 -- declaration for a formal package without a box is followed by an
5647 -- internal entity that repeats the instantiation.
5649 E := First_Entity (P_Id);
5650 while Present (E) loop
5651 if Ekind (E) = E_Package then
5652 if Renamed_Object (E) = P_Id then
5655 elsif Nkind (Parent (E)) /= N_Package_Renaming_Declaration then
5658 elsif not Box_Present (Parent (Associated_Formal_Package (E))) then
5659 Formal_P := Next_Entity (E);
5660 Check_Formal_Package_Instance (Formal_P, E);
5662 -- After checking, remove the internal validating package. It
5663 -- is only needed for semantic checks, and as it may contain
5664 -- generic formal declarations it should not reach gigi.
5666 Remove (Unit_Declaration_Node (Formal_P));
5672 end Check_Formal_Packages;
5674 ---------------------------------
5675 -- Check_Forward_Instantiation --
5676 ---------------------------------
5678 procedure Check_Forward_Instantiation (Decl : Node_Id) is
5680 Gen_Comp : Entity_Id := Cunit_Entity (Get_Source_Unit (Decl));
5683 -- The instantiation appears before the generic body if we are in the
5684 -- scope of the unit containing the generic, either in its spec or in
5685 -- the package body, and before the generic body.
5687 if Ekind (Gen_Comp) = E_Package_Body then
5688 Gen_Comp := Spec_Entity (Gen_Comp);
5691 if In_Open_Scopes (Gen_Comp)
5692 and then No (Corresponding_Body (Decl))
5697 and then not Is_Compilation_Unit (S)
5698 and then not Is_Child_Unit (S)
5700 if Ekind (S) = E_Package then
5701 Set_Has_Forward_Instantiation (S);
5707 end Check_Forward_Instantiation;
5709 ---------------------------
5710 -- Check_Generic_Actuals --
5711 ---------------------------
5713 -- The visibility of the actuals may be different between the point of
5714 -- generic instantiation and the instantiation of the body.
5716 procedure Check_Generic_Actuals
5717 (Instance : Entity_Id;
5718 Is_Formal_Box : Boolean)
5723 function Denotes_Previous_Actual (Typ : Entity_Id) return Boolean;
5724 -- For a formal that is an array type, the component type is often a
5725 -- previous formal in the same unit. The privacy status of the component
5726 -- type will have been examined earlier in the traversal of the
5727 -- corresponding actuals, and this status should not be modified for
5728 -- the array (sub)type itself. However, if the base type of the array
5729 -- (sub)type is private, its full view must be restored in the body to
5730 -- be consistent with subsequent index subtypes, etc.
5732 -- To detect this case we have to rescan the list of formals, which is
5733 -- usually short enough to ignore the resulting inefficiency.
5735 -----------------------------
5736 -- Denotes_Previous_Actual --
5737 -----------------------------
5739 function Denotes_Previous_Actual (Typ : Entity_Id) return Boolean is
5743 Prev := First_Entity (Instance);
5744 while Present (Prev) loop
5746 and then Nkind (Parent (Prev)) = N_Subtype_Declaration
5747 and then Is_Entity_Name (Subtype_Indication (Parent (Prev)))
5748 and then Entity (Subtype_Indication (Parent (Prev))) = Typ
5761 end Denotes_Previous_Actual;
5763 -- Start of processing for Check_Generic_Actuals
5766 E := First_Entity (Instance);
5767 while Present (E) loop
5769 and then Nkind (Parent (E)) = N_Subtype_Declaration
5770 and then Scope (Etype (E)) /= Instance
5771 and then Is_Entity_Name (Subtype_Indication (Parent (E)))
5773 if Is_Array_Type (E)
5774 and then not Is_Private_Type (Etype (E))
5775 and then Denotes_Previous_Actual (Component_Type (E))
5779 Check_Private_View (Subtype_Indication (Parent (E)));
5782 Set_Is_Generic_Actual_Type (E, True);
5783 Set_Is_Hidden (E, False);
5784 Set_Is_Potentially_Use_Visible (E,
5787 -- We constructed the generic actual type as a subtype of the
5788 -- supplied type. This means that it normally would not inherit
5789 -- subtype specific attributes of the actual, which is wrong for
5790 -- the generic case.
5792 Astype := Ancestor_Subtype (E);
5796 -- This can happen when E is an itype that is the full view of
5797 -- a private type completed, e.g. with a constrained array. In
5798 -- that case, use the first subtype, which will carry size
5799 -- information. The base type itself is unconstrained and will
5802 Astype := First_Subtype (E);
5805 Set_Size_Info (E, (Astype));
5806 Set_RM_Size (E, RM_Size (Astype));
5807 Set_First_Rep_Item (E, First_Rep_Item (Astype));
5809 if Is_Discrete_Or_Fixed_Point_Type (E) then
5810 Set_RM_Size (E, RM_Size (Astype));
5812 -- In nested instances, the base type of an access actual may
5813 -- itself be private, and need to be exchanged.
5815 elsif Is_Access_Type (E)
5816 and then Is_Private_Type (Etype (E))
5819 (New_Occurrence_Of (Etype (E), Sloc (Instance)));
5822 elsif Ekind (E) = E_Package then
5824 -- If this is the renaming for the current instance, we're done.
5825 -- Otherwise it is a formal package. If the corresponding formal
5826 -- was declared with a box, the (instantiations of the) generic
5827 -- formal part are also visible. Otherwise, ignore the entity
5828 -- created to validate the actuals.
5830 if Renamed_Object (E) = Instance then
5833 elsif Nkind (Parent (E)) /= N_Package_Renaming_Declaration then
5836 -- The visibility of a formal of an enclosing generic is already
5839 elsif Denotes_Formal_Package (E) then
5842 elsif Present (Associated_Formal_Package (E))
5843 and then not Is_Generic_Formal (E)
5845 if Box_Present (Parent (Associated_Formal_Package (E))) then
5846 Check_Generic_Actuals (Renamed_Object (E), True);
5849 Check_Generic_Actuals (Renamed_Object (E), False);
5852 Set_Is_Hidden (E, False);
5855 -- If this is a subprogram instance (in a wrapper package) the
5856 -- actual is fully visible.
5858 elsif Is_Wrapper_Package (Instance) then
5859 Set_Is_Hidden (E, False);
5861 -- If the formal package is declared with a box, or if the formal
5862 -- parameter is defaulted, it is visible in the body.
5865 or else Is_Visible_Formal (E)
5867 Set_Is_Hidden (E, False);
5870 if Ekind (E) = E_Constant then
5872 -- If the type of the actual is a private type declared in the
5873 -- enclosing scope of the generic unit, the body of the generic
5874 -- sees the full view of the type (because it has to appear in
5875 -- the corresponding package body). If the type is private now,
5876 -- exchange views to restore the proper visiblity in the instance.
5879 Typ : constant Entity_Id := Base_Type (Etype (E));
5880 -- The type of the actual
5885 Parent_Scope : Entity_Id;
5886 -- The enclosing scope of the generic unit
5889 if Is_Wrapper_Package (Instance) then
5893 (Unit_Declaration_Node
5894 (Related_Instance (Instance))));
5897 Generic_Parent (Package_Specification (Instance));
5900 Parent_Scope := Scope (Gen_Id);
5902 -- The exchange is only needed if the generic is defined
5903 -- within a package which is not a common ancestor of the
5904 -- scope of the instance, and is not already in scope.
5906 if Is_Private_Type (Typ)
5907 and then Scope (Typ) = Parent_Scope
5908 and then Scope (Instance) /= Parent_Scope
5909 and then Ekind (Parent_Scope) = E_Package
5910 and then not Is_Child_Unit (Gen_Id)
5914 -- If the type of the entity is a subtype, it may also have
5915 -- to be made visible, together with the base type of its
5916 -- full view, after exchange.
5918 if Is_Private_Type (Etype (E)) then
5919 Switch_View (Etype (E));
5920 Switch_View (Base_Type (Etype (E)));
5928 end Check_Generic_Actuals;
5930 ------------------------------
5931 -- Check_Generic_Child_Unit --
5932 ------------------------------
5934 procedure Check_Generic_Child_Unit
5936 Parent_Installed : in out Boolean)
5938 Loc : constant Source_Ptr := Sloc (Gen_Id);
5939 Gen_Par : Entity_Id := Empty;
5941 Inst_Par : Entity_Id;
5944 function Find_Generic_Child
5946 Id : Node_Id) return Entity_Id;
5947 -- Search generic parent for possible child unit with the given name
5949 function In_Enclosing_Instance return Boolean;
5950 -- Within an instance of the parent, the child unit may be denoted by
5951 -- a simple name, or an abbreviated expanded name. Examine enclosing
5952 -- scopes to locate a possible parent instantiation.
5954 ------------------------
5955 -- Find_Generic_Child --
5956 ------------------------
5958 function Find_Generic_Child
5960 Id : Node_Id) return Entity_Id
5965 -- If entity of name is already set, instance has already been
5966 -- resolved, e.g. in an enclosing instantiation.
5968 if Present (Entity (Id)) then
5969 if Scope (Entity (Id)) = Scop then
5976 E := First_Entity (Scop);
5977 while Present (E) loop
5978 if Chars (E) = Chars (Id)
5979 and then Is_Child_Unit (E)
5981 if Is_Child_Unit (E)
5982 and then not Is_Visible_Lib_Unit (E)
5985 ("generic child unit& is not visible", Gen_Id, E);
5997 end Find_Generic_Child;
5999 ---------------------------
6000 -- In_Enclosing_Instance --
6001 ---------------------------
6003 function In_Enclosing_Instance return Boolean is
6004 Enclosing_Instance : Node_Id;
6005 Instance_Decl : Node_Id;
6008 -- We do not inline any call that contains instantiations, except
6009 -- for instantiations of Unchecked_Conversion, so if we are within
6010 -- an inlined body the current instance does not require parents.
6012 if In_Inlined_Body then
6013 pragma Assert (Chars (Gen_Id) = Name_Unchecked_Conversion);
6017 -- Loop to check enclosing scopes
6019 Enclosing_Instance := Current_Scope;
6020 while Present (Enclosing_Instance) loop
6021 Instance_Decl := Unit_Declaration_Node (Enclosing_Instance);
6023 if Ekind (Enclosing_Instance) = E_Package
6024 and then Is_Generic_Instance (Enclosing_Instance)
6026 (Generic_Parent (Specification (Instance_Decl)))
6028 -- Check whether the generic we are looking for is a child of
6031 E := Find_Generic_Child
6032 (Generic_Parent (Specification (Instance_Decl)), Gen_Id);
6033 exit when Present (E);
6039 Enclosing_Instance := Scope (Enclosing_Instance);
6051 Make_Expanded_Name (Loc,
6053 Prefix => New_Occurrence_Of (Enclosing_Instance, Loc),
6054 Selector_Name => New_Occurrence_Of (E, Loc)));
6056 Set_Entity (Gen_Id, E);
6057 Set_Etype (Gen_Id, Etype (E));
6058 Parent_Installed := False; -- Already in scope.
6061 end In_Enclosing_Instance;
6063 -- Start of processing for Check_Generic_Child_Unit
6066 -- If the name of the generic is given by a selected component, it may
6067 -- be the name of a generic child unit, and the prefix is the name of an
6068 -- instance of the parent, in which case the child unit must be visible.
6069 -- If this instance is not in scope, it must be placed there and removed
6070 -- after instantiation, because what is being instantiated is not the
6071 -- original child, but the corresponding child present in the instance
6074 -- If the child is instantiated within the parent, it can be given by
6075 -- a simple name. In this case the instance is already in scope, but
6076 -- the child generic must be recovered from the generic parent as well.
6078 if Nkind (Gen_Id) = N_Selected_Component then
6079 S := Selector_Name (Gen_Id);
6080 Analyze (Prefix (Gen_Id));
6081 Inst_Par := Entity (Prefix (Gen_Id));
6083 if Ekind (Inst_Par) = E_Package
6084 and then Present (Renamed_Object (Inst_Par))
6086 Inst_Par := Renamed_Object (Inst_Par);
6089 if Ekind (Inst_Par) = E_Package then
6090 if Nkind (Parent (Inst_Par)) = N_Package_Specification then
6091 Gen_Par := Generic_Parent (Parent (Inst_Par));
6093 elsif Nkind (Parent (Inst_Par)) = N_Defining_Program_Unit_Name
6095 Nkind (Parent (Parent (Inst_Par))) = N_Package_Specification
6097 Gen_Par := Generic_Parent (Parent (Parent (Inst_Par)));
6100 elsif Ekind (Inst_Par) = E_Generic_Package
6101 and then Nkind (Parent (Gen_Id)) = N_Formal_Package_Declaration
6103 -- A formal package may be a real child package, and not the
6104 -- implicit instance within a parent. In this case the child is
6105 -- not visible and has to be retrieved explicitly as well.
6107 Gen_Par := Inst_Par;
6110 if Present (Gen_Par) then
6112 -- The prefix denotes an instantiation. The entity itself may be a
6113 -- nested generic, or a child unit.
6115 E := Find_Generic_Child (Gen_Par, S);
6118 Change_Selected_Component_To_Expanded_Name (Gen_Id);
6119 Set_Entity (Gen_Id, E);
6120 Set_Etype (Gen_Id, Etype (E));
6122 Set_Etype (S, Etype (E));
6124 -- Indicate that this is a reference to the parent
6126 if In_Extended_Main_Source_Unit (Gen_Id) then
6127 Set_Is_Instantiated (Inst_Par);
6130 -- A common mistake is to replicate the naming scheme of a
6131 -- hierarchy by instantiating a generic child directly, rather
6132 -- than the implicit child in a parent instance:
6134 -- generic .. package Gpar is ..
6135 -- generic .. package Gpar.Child is ..
6136 -- package Par is new Gpar ();
6139 -- package Par.Child is new Gpar.Child ();
6140 -- rather than Par.Child
6142 -- In this case the instantiation is within Par, which is an
6143 -- instance, but Gpar does not denote Par because we are not IN
6144 -- the instance of Gpar, so this is illegal. The test below
6145 -- recognizes this particular case.
6147 if Is_Child_Unit (E)
6148 and then not Comes_From_Source (Entity (Prefix (Gen_Id)))
6149 and then (not In_Instance
6150 or else Nkind (Parent (Parent (Gen_Id))) =
6154 ("prefix of generic child unit must be instance of parent",
6158 if not In_Open_Scopes (Inst_Par)
6159 and then Nkind (Parent (Gen_Id)) not in
6160 N_Generic_Renaming_Declaration
6162 Install_Parent (Inst_Par);
6163 Parent_Installed := True;
6165 elsif In_Open_Scopes (Inst_Par) then
6167 -- If the parent is already installed, install the actuals
6168 -- for its formal packages. This is necessary when the child
6169 -- instance is a child of the parent instance: in this case,
6170 -- the parent is placed on the scope stack but the formal
6171 -- packages are not made visible.
6173 Install_Formal_Packages (Inst_Par);
6177 -- If the generic parent does not contain an entity that
6178 -- corresponds to the selector, the instance doesn't either.
6179 -- Analyzing the node will yield the appropriate error message.
6180 -- If the entity is not a child unit, then it is an inner
6181 -- generic in the parent.
6189 if Is_Child_Unit (Entity (Gen_Id))
6191 Nkind (Parent (Gen_Id)) not in N_Generic_Renaming_Declaration
6192 and then not In_Open_Scopes (Inst_Par)
6194 Install_Parent (Inst_Par);
6195 Parent_Installed := True;
6197 -- The generic unit may be the renaming of the implicit child
6198 -- present in an instance. In that case the parent instance is
6199 -- obtained from the name of the renamed entity.
6201 elsif Ekind (Entity (Gen_Id)) = E_Generic_Package
6202 and then Present (Renamed_Entity (Entity (Gen_Id)))
6203 and then Is_Child_Unit (Renamed_Entity (Entity (Gen_Id)))
6206 Renamed_Package : constant Node_Id :=
6207 Name (Parent (Entity (Gen_Id)));
6209 if Nkind (Renamed_Package) = N_Expanded_Name then
6210 Inst_Par := Entity (Prefix (Renamed_Package));
6211 Install_Parent (Inst_Par);
6212 Parent_Installed := True;
6218 elsif Nkind (Gen_Id) = N_Expanded_Name then
6220 -- Entity already present, analyze prefix, whose meaning may be
6221 -- an instance in the current context. If it is an instance of
6222 -- a relative within another, the proper parent may still have
6223 -- to be installed, if they are not of the same generation.
6225 Analyze (Prefix (Gen_Id));
6227 -- In the unlikely case that a local declaration hides the name
6228 -- of the parent package, locate it on the homonym chain. If the
6229 -- context is an instance of the parent, the renaming entity is
6232 Inst_Par := Entity (Prefix (Gen_Id));
6233 while Present (Inst_Par)
6234 and then not Is_Package_Or_Generic_Package (Inst_Par)
6236 Inst_Par := Homonym (Inst_Par);
6239 pragma Assert (Present (Inst_Par));
6240 Set_Entity (Prefix (Gen_Id), Inst_Par);
6242 if In_Enclosing_Instance then
6245 elsif Present (Entity (Gen_Id))
6246 and then Is_Child_Unit (Entity (Gen_Id))
6247 and then not In_Open_Scopes (Inst_Par)
6249 Install_Parent (Inst_Par);
6250 Parent_Installed := True;
6253 elsif In_Enclosing_Instance then
6255 -- The child unit is found in some enclosing scope
6262 -- If this is the renaming of the implicit child in a parent
6263 -- instance, recover the parent name and install it.
6265 if Is_Entity_Name (Gen_Id) then
6266 E := Entity (Gen_Id);
6268 if Is_Generic_Unit (E)
6269 and then Nkind (Parent (E)) in N_Generic_Renaming_Declaration
6270 and then Is_Child_Unit (Renamed_Object (E))
6271 and then Is_Generic_Unit (Scope (Renamed_Object (E)))
6272 and then Nkind (Name (Parent (E))) = N_Expanded_Name
6275 New_Copy_Tree (Name (Parent (E))));
6276 Inst_Par := Entity (Prefix (Gen_Id));
6278 if not In_Open_Scopes (Inst_Par) then
6279 Install_Parent (Inst_Par);
6280 Parent_Installed := True;
6283 -- If it is a child unit of a non-generic parent, it may be
6284 -- use-visible and given by a direct name. Install parent as
6287 elsif Is_Generic_Unit (E)
6288 and then Is_Child_Unit (E)
6290 Nkind (Parent (Gen_Id)) not in N_Generic_Renaming_Declaration
6291 and then not Is_Generic_Unit (Scope (E))
6293 if not In_Open_Scopes (Scope (E)) then
6294 Install_Parent (Scope (E));
6295 Parent_Installed := True;
6300 end Check_Generic_Child_Unit;
6302 -----------------------------
6303 -- Check_Hidden_Child_Unit --
6304 -----------------------------
6306 procedure Check_Hidden_Child_Unit
6308 Gen_Unit : Entity_Id;
6309 Act_Decl_Id : Entity_Id)
6311 Gen_Id : constant Node_Id := Name (N);
6314 if Is_Child_Unit (Gen_Unit)
6315 and then Is_Child_Unit (Act_Decl_Id)
6316 and then Nkind (Gen_Id) = N_Expanded_Name
6317 and then Entity (Prefix (Gen_Id)) = Scope (Act_Decl_Id)
6318 and then Chars (Gen_Unit) = Chars (Act_Decl_Id)
6320 Error_Msg_Node_2 := Scope (Act_Decl_Id);
6322 ("generic unit & is implicitly declared in &",
6323 Defining_Unit_Name (N), Gen_Unit);
6324 Error_Msg_N ("\instance must have different name",
6325 Defining_Unit_Name (N));
6327 end Check_Hidden_Child_Unit;
6329 ------------------------
6330 -- Check_Private_View --
6331 ------------------------
6333 procedure Check_Private_View (N : Node_Id) is
6334 T : constant Entity_Id := Etype (N);
6338 -- Exchange views if the type was not private in the generic but is
6339 -- private at the point of instantiation. Do not exchange views if
6340 -- the scope of the type is in scope. This can happen if both generic
6341 -- and instance are sibling units, or if type is defined in a parent.
6342 -- In this case the visibility of the type will be correct for all
6346 BT := Base_Type (T);
6348 if Is_Private_Type (T)
6349 and then not Has_Private_View (N)
6350 and then Present (Full_View (T))
6351 and then not In_Open_Scopes (Scope (T))
6353 -- In the generic, the full type was visible. Save the private
6354 -- entity, for subsequent exchange.
6358 elsif Has_Private_View (N)
6359 and then not Is_Private_Type (T)
6360 and then not Has_Been_Exchanged (T)
6361 and then Etype (Get_Associated_Node (N)) /= T
6363 -- Only the private declaration was visible in the generic. If
6364 -- the type appears in a subtype declaration, the subtype in the
6365 -- instance must have a view compatible with that of its parent,
6366 -- which must be exchanged (see corresponding code in Restore_
6367 -- Private_Views). Otherwise, if the type is defined in a parent
6368 -- unit, leave full visibility within instance, which is safe.
6370 if In_Open_Scopes (Scope (Base_Type (T)))
6371 and then not Is_Private_Type (Base_Type (T))
6372 and then Comes_From_Source (Base_Type (T))
6376 elsif Nkind (Parent (N)) = N_Subtype_Declaration
6377 or else not In_Private_Part (Scope (Base_Type (T)))
6379 Prepend_Elmt (T, Exchanged_Views);
6380 Exchange_Declarations (Etype (Get_Associated_Node (N)));
6383 -- For composite types with inconsistent representation exchange
6384 -- component types accordingly.
6386 elsif Is_Access_Type (T)
6387 and then Is_Private_Type (Designated_Type (T))
6388 and then not Has_Private_View (N)
6389 and then Present (Full_View (Designated_Type (T)))
6391 Switch_View (Designated_Type (T));
6393 elsif Is_Array_Type (T) then
6394 if Is_Private_Type (Component_Type (T))
6395 and then not Has_Private_View (N)
6396 and then Present (Full_View (Component_Type (T)))
6398 Switch_View (Component_Type (T));
6401 -- The normal exchange mechanism relies on the setting of a
6402 -- flag on the reference in the generic. However, an additional
6403 -- mechanism is needed for types that are not explicitly
6404 -- mentioned in the generic, but may be needed in expanded code
6405 -- in the instance. This includes component types of arrays and
6406 -- designated types of access types. This processing must also
6407 -- include the index types of arrays which we take care of here.
6414 Indx := First_Index (T);
6415 while Present (Indx) loop
6416 Typ := Base_Type (Etype (Indx));
6418 if Is_Private_Type (Typ)
6419 and then Present (Full_View (Typ))
6428 elsif Is_Private_Type (T)
6429 and then Present (Full_View (T))
6430 and then Is_Array_Type (Full_View (T))
6431 and then Is_Private_Type (Component_Type (Full_View (T)))
6435 -- Finally, a non-private subtype may have a private base type, which
6436 -- must be exchanged for consistency. This can happen when a package
6437 -- body is instantiated, when the scope stack is empty but in fact
6438 -- the subtype and the base type are declared in an enclosing scope.
6440 -- Note that in this case we introduce an inconsistency in the view
6441 -- set, because we switch the base type BT, but there could be some
6442 -- private dependent subtypes of BT which remain unswitched. Such
6443 -- subtypes might need to be switched at a later point (see specific
6444 -- provision for that case in Switch_View).
6446 elsif not Is_Private_Type (T)
6447 and then not Has_Private_View (N)
6448 and then Is_Private_Type (BT)
6449 and then Present (Full_View (BT))
6450 and then not Is_Generic_Type (BT)
6451 and then not In_Open_Scopes (BT)
6453 Prepend_Elmt (Full_View (BT), Exchanged_Views);
6454 Exchange_Declarations (BT);
6457 end Check_Private_View;
6459 -----------------------------
6460 -- Check_Hidden_Primitives --
6461 -----------------------------
6463 function Check_Hidden_Primitives (Assoc_List : List_Id) return Elist_Id is
6466 Result : Elist_Id := No_Elist;
6469 if No (Assoc_List) then
6473 -- Traverse the list of associations between formals and actuals
6474 -- searching for renamings of tagged types
6476 Actual := First (Assoc_List);
6477 while Present (Actual) loop
6478 if Nkind (Actual) = N_Subtype_Declaration then
6479 Gen_T := Generic_Parent_Type (Actual);
6482 and then Is_Tagged_Type (Gen_T)
6484 -- Traverse the list of primitives of the actual types
6485 -- searching for hidden primitives that are visible in the
6486 -- corresponding generic formal; leave them visible and
6487 -- append them to Result to restore their decoration later.
6489 Install_Hidden_Primitives
6490 (Prims_List => Result,
6492 Act_T => Entity (Subtype_Indication (Actual)));
6500 end Check_Hidden_Primitives;
6502 --------------------------
6503 -- Contains_Instance_Of --
6504 --------------------------
6506 function Contains_Instance_Of
6509 N : Node_Id) return Boolean
6517 -- Verify that there are no circular instantiations. We check whether
6518 -- the unit contains an instance of the current scope or some enclosing
6519 -- scope (in case one of the instances appears in a subunit). Longer
6520 -- circularities involving subunits might seem too pathological to
6521 -- consider, but they were not too pathological for the authors of
6522 -- DEC bc30vsq, so we loop over all enclosing scopes, and mark all
6523 -- enclosing generic scopes as containing an instance.
6526 -- Within a generic subprogram body, the scope is not generic, to
6527 -- allow for recursive subprograms. Use the declaration to determine
6528 -- whether this is a generic unit.
6530 if Ekind (Scop) = E_Generic_Package
6531 or else (Is_Subprogram (Scop)
6532 and then Nkind (Unit_Declaration_Node (Scop)) =
6533 N_Generic_Subprogram_Declaration)
6535 Elmt := First_Elmt (Inner_Instances (Inner));
6537 while Present (Elmt) loop
6538 if Node (Elmt) = Scop then
6539 Error_Msg_Node_2 := Inner;
6541 ("circular Instantiation: & instantiated within &!",
6545 elsif Node (Elmt) = Inner then
6548 elsif Contains_Instance_Of (Node (Elmt), Scop, N) then
6549 Error_Msg_Node_2 := Inner;
6551 ("circular Instantiation: & instantiated within &!",
6559 -- Indicate that Inner is being instantiated within Scop
6561 Append_Elmt (Inner, Inner_Instances (Scop));
6564 if Scop = Standard_Standard then
6567 Scop := Scope (Scop);
6572 end Contains_Instance_Of;
6574 -----------------------
6575 -- Copy_Generic_Node --
6576 -----------------------
6578 function Copy_Generic_Node
6580 Parent_Id : Node_Id;
6581 Instantiating : Boolean) return Node_Id
6586 function Copy_Generic_Descendant (D : Union_Id) return Union_Id;
6587 -- Check the given value of one of the Fields referenced by the current
6588 -- node to determine whether to copy it recursively. The field may hold
6589 -- a Node_Id, a List_Id, or an Elist_Id, or a plain value (Sloc, Uint,
6590 -- Char) in which case it need not be copied.
6592 procedure Copy_Descendants;
6593 -- Common utility for various nodes
6595 function Copy_Generic_Elist (E : Elist_Id) return Elist_Id;
6596 -- Make copy of element list
6598 function Copy_Generic_List
6600 Parent_Id : Node_Id) return List_Id;
6601 -- Apply Copy_Node recursively to the members of a node list
6603 function In_Defining_Unit_Name (Nam : Node_Id) return Boolean;
6604 -- True if an identifier is part of the defining program unit name of
6605 -- a child unit. The entity of such an identifier must be kept (for
6606 -- ASIS use) even though as the name of an enclosing generic it would
6607 -- otherwise not be preserved in the generic tree.
6609 ----------------------
6610 -- Copy_Descendants --
6611 ----------------------
6613 procedure Copy_Descendants is
6615 use Atree.Unchecked_Access;
6616 -- This code section is part of the implementation of an untyped
6617 -- tree traversal, so it needs direct access to node fields.
6620 Set_Field1 (New_N, Copy_Generic_Descendant (Field1 (N)));
6621 Set_Field2 (New_N, Copy_Generic_Descendant (Field2 (N)));
6622 Set_Field3 (New_N, Copy_Generic_Descendant (Field3 (N)));
6623 Set_Field4 (New_N, Copy_Generic_Descendant (Field4 (N)));
6624 Set_Field5 (New_N, Copy_Generic_Descendant (Field5 (N)));
6625 end Copy_Descendants;
6627 -----------------------------
6628 -- Copy_Generic_Descendant --
6629 -----------------------------
6631 function Copy_Generic_Descendant (D : Union_Id) return Union_Id is
6633 if D = Union_Id (Empty) then
6636 elsif D in Node_Range then
6638 (Copy_Generic_Node (Node_Id (D), New_N, Instantiating));
6640 elsif D in List_Range then
6641 return Union_Id (Copy_Generic_List (List_Id (D), New_N));
6643 elsif D in Elist_Range then
6644 return Union_Id (Copy_Generic_Elist (Elist_Id (D)));
6646 -- Nothing else is copyable (e.g. Uint values), return as is
6651 end Copy_Generic_Descendant;
6653 ------------------------
6654 -- Copy_Generic_Elist --
6655 ------------------------
6657 function Copy_Generic_Elist (E : Elist_Id) return Elist_Id is
6664 M := First_Elmt (E);
6665 while Present (M) loop
6667 (Copy_Generic_Node (Node (M), Empty, Instantiating), L);
6676 end Copy_Generic_Elist;
6678 -----------------------
6679 -- Copy_Generic_List --
6680 -----------------------
6682 function Copy_Generic_List
6684 Parent_Id : Node_Id) return List_Id
6692 Set_Parent (New_L, Parent_Id);
6695 while Present (N) loop
6696 Append (Copy_Generic_Node (N, Empty, Instantiating), New_L);
6705 end Copy_Generic_List;
6707 ---------------------------
6708 -- In_Defining_Unit_Name --
6709 ---------------------------
6711 function In_Defining_Unit_Name (Nam : Node_Id) return Boolean is
6713 return Present (Parent (Nam))
6714 and then (Nkind (Parent (Nam)) = N_Defining_Program_Unit_Name
6716 (Nkind (Parent (Nam)) = N_Expanded_Name
6717 and then In_Defining_Unit_Name (Parent (Nam))));
6718 end In_Defining_Unit_Name;
6720 -- Start of processing for Copy_Generic_Node
6727 New_N := New_Copy (N);
6729 -- Copy aspects if present
6731 if Has_Aspects (N) then
6732 Set_Has_Aspects (New_N, False);
6733 Set_Aspect_Specifications
6734 (New_N, Copy_Generic_List (Aspect_Specifications (N), Parent_Id));
6737 if Instantiating then
6738 Adjust_Instantiation_Sloc (New_N, S_Adjustment);
6741 if not Is_List_Member (N) then
6742 Set_Parent (New_N, Parent_Id);
6745 -- If defining identifier, then all fields have been copied already
6747 if Nkind (New_N) in N_Entity then
6750 -- Special casing for identifiers and other entity names and operators
6752 elsif Nkind_In (New_N, N_Identifier,
6753 N_Character_Literal,
6756 or else Nkind (New_N) in N_Op
6758 if not Instantiating then
6760 -- Link both nodes in order to assign subsequently the entity of
6761 -- the copy to the original node, in case this is a global
6764 Set_Associated_Node (N, New_N);
6766 -- If we are within an instantiation, this is a nested generic
6767 -- that has already been analyzed at the point of definition.
6768 -- We must preserve references that were global to the enclosing
6769 -- parent at that point. Other occurrences, whether global or
6770 -- local to the current generic, must be resolved anew, so we
6771 -- reset the entity in the generic copy. A global reference has a
6772 -- smaller depth than the parent, or else the same depth in case
6773 -- both are distinct compilation units.
6775 -- A child unit is implicitly declared within the enclosing parent
6776 -- but is in fact global to it, and must be preserved.
6778 -- It is also possible for Current_Instantiated_Parent to be
6779 -- defined, and for this not to be a nested generic, namely if
6780 -- the unit is loaded through Rtsfind. In that case, the entity of
6781 -- New_N is only a link to the associated node, and not a defining
6784 -- The entities for parent units in the defining_program_unit of a
6785 -- generic child unit are established when the context of the unit
6786 -- is first analyzed, before the generic copy is made. They are
6787 -- preserved in the copy for use in ASIS queries.
6789 Ent := Entity (New_N);
6791 if No (Current_Instantiated_Parent.Gen_Id) then
6793 or else Nkind (Ent) /= N_Defining_Identifier
6794 or else not In_Defining_Unit_Name (N)
6796 Set_Associated_Node (New_N, Empty);
6801 not Nkind_In (Ent, N_Defining_Identifier,
6802 N_Defining_Character_Literal,
6803 N_Defining_Operator_Symbol)
6804 or else No (Scope (Ent))
6806 (Scope (Ent) = Current_Instantiated_Parent.Gen_Id
6807 and then not Is_Child_Unit (Ent))
6809 (Scope_Depth (Scope (Ent)) >
6810 Scope_Depth (Current_Instantiated_Parent.Gen_Id)
6812 Get_Source_Unit (Ent) =
6813 Get_Source_Unit (Current_Instantiated_Parent.Gen_Id))
6815 Set_Associated_Node (New_N, Empty);
6818 -- Case of instantiating identifier or some other name or operator
6821 -- If the associated node is still defined, the entity in it
6822 -- is global, and must be copied to the instance. If this copy
6823 -- is being made for a body to inline, it is applied to an
6824 -- instantiated tree, and the entity is already present and
6825 -- must be also preserved.
6828 Assoc : constant Node_Id := Get_Associated_Node (N);
6831 if Present (Assoc) then
6832 if Nkind (Assoc) = Nkind (N) then
6833 Set_Entity (New_N, Entity (Assoc));
6834 Check_Private_View (N);
6836 -- The name in the call may be a selected component if the
6837 -- call has not been analyzed yet, as may be the case for
6838 -- pre/post conditions in a generic unit.
6840 elsif Nkind (Assoc) = N_Function_Call
6841 and then Is_Entity_Name (Name (Assoc))
6843 Set_Entity (New_N, Entity (Name (Assoc)));
6845 elsif Nkind_In (Assoc, N_Defining_Identifier,
6846 N_Defining_Character_Literal,
6847 N_Defining_Operator_Symbol)
6848 and then Expander_Active
6850 -- Inlining case: we are copying a tree that contains
6851 -- global entities, which are preserved in the copy to be
6852 -- used for subsequent inlining.
6857 Set_Entity (New_N, Empty);
6863 -- For expanded name, we must copy the Prefix and Selector_Name
6865 if Nkind (N) = N_Expanded_Name then
6867 (New_N, Copy_Generic_Node (Prefix (N), New_N, Instantiating));
6869 Set_Selector_Name (New_N,
6870 Copy_Generic_Node (Selector_Name (N), New_N, Instantiating));
6872 -- For operators, we must copy the right operand
6874 elsif Nkind (N) in N_Op then
6875 Set_Right_Opnd (New_N,
6876 Copy_Generic_Node (Right_Opnd (N), New_N, Instantiating));
6878 -- And for binary operators, the left operand as well
6880 if Nkind (N) in N_Binary_Op then
6881 Set_Left_Opnd (New_N,
6882 Copy_Generic_Node (Left_Opnd (N), New_N, Instantiating));
6886 -- Special casing for stubs
6888 elsif Nkind (N) in N_Body_Stub then
6890 -- In any case, we must copy the specification or defining
6891 -- identifier as appropriate.
6893 if Nkind (N) = N_Subprogram_Body_Stub then
6894 Set_Specification (New_N,
6895 Copy_Generic_Node (Specification (N), New_N, Instantiating));
6898 Set_Defining_Identifier (New_N,
6900 (Defining_Identifier (N), New_N, Instantiating));
6903 -- If we are not instantiating, then this is where we load and
6904 -- analyze subunits, i.e. at the point where the stub occurs. A
6905 -- more permissive system might defer this analysis to the point
6906 -- of instantiation, but this seems too complicated for now.
6908 if not Instantiating then
6910 Subunit_Name : constant Unit_Name_Type := Get_Unit_Name (N);
6912 Unum : Unit_Number_Type;
6916 -- Make sure that, if it is a subunit of the main unit that is
6917 -- preprocessed and if -gnateG is specified, the preprocessed
6918 -- file will be written.
6920 Lib.Analysing_Subunit_Of_Main :=
6921 Lib.In_Extended_Main_Source_Unit (N);
6924 (Load_Name => Subunit_Name,
6928 Lib.Analysing_Subunit_Of_Main := False;
6930 -- If the proper body is not found, a warning message will be
6931 -- emitted when analyzing the stub, or later at the point of
6932 -- instantiation. Here we just leave the stub as is.
6934 if Unum = No_Unit then
6935 Subunits_Missing := True;
6936 goto Subunit_Not_Found;
6939 Subunit := Cunit (Unum);
6941 if Nkind (Unit (Subunit)) /= N_Subunit then
6943 ("found child unit instead of expected SEPARATE subunit",
6945 Error_Msg_Sloc := Sloc (N);
6946 Error_Msg_N ("\to complete stub #", Subunit);
6947 goto Subunit_Not_Found;
6950 -- We must create a generic copy of the subunit, in order to
6951 -- perform semantic analysis on it, and we must replace the
6952 -- stub in the original generic unit with the subunit, in order
6953 -- to preserve non-local references within.
6955 -- Only the proper body needs to be copied. Library_Unit and
6956 -- context clause are simply inherited by the generic copy.
6957 -- Note that the copy (which may be recursive if there are
6958 -- nested subunits) must be done first, before attaching it to
6959 -- the enclosing generic.
6963 (Proper_Body (Unit (Subunit)),
6964 Empty, Instantiating => False);
6966 -- Now place the original proper body in the original generic
6967 -- unit. This is a body, not a compilation unit.
6969 Rewrite (N, Proper_Body (Unit (Subunit)));
6970 Set_Is_Compilation_Unit (Defining_Entity (N), False);
6971 Set_Was_Originally_Stub (N);
6973 -- Finally replace the body of the subunit with its copy, and
6974 -- make this new subunit into the library unit of the generic
6975 -- copy, which does not have stubs any longer.
6977 Set_Proper_Body (Unit (Subunit), New_Body);
6978 Set_Library_Unit (New_N, Subunit);
6979 Inherit_Context (Unit (Subunit), N);
6982 -- If we are instantiating, this must be an error case, since
6983 -- otherwise we would have replaced the stub node by the proper body
6984 -- that corresponds. So just ignore it in the copy (i.e. we have
6985 -- copied it, and that is good enough).
6991 <<Subunit_Not_Found>> null;
6993 -- If the node is a compilation unit, it is the subunit of a stub, which
6994 -- has been loaded already (see code below). In this case, the library
6995 -- unit field of N points to the parent unit (which is a compilation
6996 -- unit) and need not (and cannot) be copied.
6998 -- When the proper body of the stub is analyzed, the library_unit link
6999 -- is used to establish the proper context (see sem_ch10).
7001 -- The other fields of a compilation unit are copied as usual
7003 elsif Nkind (N) = N_Compilation_Unit then
7005 -- This code can only be executed when not instantiating, because in
7006 -- the copy made for an instantiation, the compilation unit node has
7007 -- disappeared at the point that a stub is replaced by its proper
7010 pragma Assert (not Instantiating);
7012 Set_Context_Items (New_N,
7013 Copy_Generic_List (Context_Items (N), New_N));
7016 Copy_Generic_Node (Unit (N), New_N, False));
7018 Set_First_Inlined_Subprogram (New_N,
7020 (First_Inlined_Subprogram (N), New_N, False));
7022 Set_Aux_Decls_Node (New_N,
7023 Copy_Generic_Node (Aux_Decls_Node (N), New_N, False));
7025 -- For an assignment node, the assignment is known to be semantically
7026 -- legal if we are instantiating the template. This avoids incorrect
7027 -- diagnostics in generated code.
7029 elsif Nkind (N) = N_Assignment_Statement then
7031 -- Copy name and expression fields in usual manner
7034 Copy_Generic_Node (Name (N), New_N, Instantiating));
7036 Set_Expression (New_N,
7037 Copy_Generic_Node (Expression (N), New_N, Instantiating));
7039 if Instantiating then
7040 Set_Assignment_OK (Name (New_N), True);
7043 elsif Nkind_In (N, N_Aggregate, N_Extension_Aggregate) then
7044 if not Instantiating then
7045 Set_Associated_Node (N, New_N);
7048 if Present (Get_Associated_Node (N))
7049 and then Nkind (Get_Associated_Node (N)) = Nkind (N)
7051 -- In the generic the aggregate has some composite type. If at
7052 -- the point of instantiation the type has a private view,
7053 -- install the full view (and that of its ancestors, if any).
7056 T : Entity_Id := (Etype (Get_Associated_Node (New_N)));
7061 and then Is_Private_Type (T)
7067 and then Is_Tagged_Type (T)
7068 and then Is_Derived_Type (T)
7070 Rt := Root_Type (T);
7075 if Is_Private_Type (T) then
7086 -- Do not copy the associated node, which points to the generic copy
7087 -- of the aggregate.
7090 use Atree.Unchecked_Access;
7091 -- This code section is part of the implementation of an untyped
7092 -- tree traversal, so it needs direct access to node fields.
7095 Set_Field1 (New_N, Copy_Generic_Descendant (Field1 (N)));
7096 Set_Field2 (New_N, Copy_Generic_Descendant (Field2 (N)));
7097 Set_Field3 (New_N, Copy_Generic_Descendant (Field3 (N)));
7098 Set_Field5 (New_N, Copy_Generic_Descendant (Field5 (N)));
7101 -- Allocators do not have an identifier denoting the access type, so we
7102 -- must locate it through the expression to check whether the views are
7105 elsif Nkind (N) = N_Allocator
7106 and then Nkind (Expression (N)) = N_Qualified_Expression
7107 and then Is_Entity_Name (Subtype_Mark (Expression (N)))
7108 and then Instantiating
7111 T : constant Node_Id :=
7112 Get_Associated_Node (Subtype_Mark (Expression (N)));
7118 -- Retrieve the allocator node in the generic copy
7120 Acc_T := Etype (Parent (Parent (T)));
7122 and then Is_Private_Type (Acc_T)
7124 Switch_View (Acc_T);
7131 -- For a proper body, we must catch the case of a proper body that
7132 -- replaces a stub. This represents the point at which a separate
7133 -- compilation unit, and hence template file, may be referenced, so we
7134 -- must make a new source instantiation entry for the template of the
7135 -- subunit, and ensure that all nodes in the subunit are adjusted using
7136 -- this new source instantiation entry.
7138 elsif Nkind (N) in N_Proper_Body then
7140 Save_Adjustment : constant Sloc_Adjustment := S_Adjustment;
7143 if Instantiating and then Was_Originally_Stub (N) then
7144 Create_Instantiation_Source
7145 (Instantiation_Node,
7146 Defining_Entity (N),
7151 -- Now copy the fields of the proper body, using the new
7152 -- adjustment factor if one was needed as per test above.
7156 -- Restore the original adjustment factor in case changed
7158 S_Adjustment := Save_Adjustment;
7161 -- Don't copy Ident or Comment pragmas, since the comment belongs to the
7162 -- generic unit, not to the instantiating unit.
7164 elsif Nkind (N) = N_Pragma and then Instantiating then
7166 Prag_Id : constant Pragma_Id := Get_Pragma_Id (N);
7168 if Prag_Id = Pragma_Ident or else Prag_Id = Pragma_Comment then
7169 New_N := Make_Null_Statement (Sloc (N));
7175 elsif Nkind_In (N, N_Integer_Literal, N_Real_Literal) then
7177 -- No descendant fields need traversing
7181 elsif Nkind (N) = N_String_Literal
7182 and then Present (Etype (N))
7183 and then Instantiating
7185 -- If the string is declared in an outer scope, the string_literal
7186 -- subtype created for it may have the wrong scope. We force the
7187 -- reanalysis of the constant to generate a new itype in the proper
7190 Set_Etype (New_N, Empty);
7191 Set_Analyzed (New_N, False);
7193 -- For the remaining nodes, copy their descendants recursively
7198 if Instantiating and then Nkind (N) = N_Subprogram_Body then
7199 Set_Generic_Parent (Specification (New_N), N);
7201 -- Should preserve Corresponding_Spec??? (12.3(14))
7206 end Copy_Generic_Node;
7208 ----------------------------
7209 -- Denotes_Formal_Package --
7210 ----------------------------
7212 function Denotes_Formal_Package
7214 On_Exit : Boolean := False;
7215 Instance : Entity_Id := Empty) return Boolean
7218 Scop : constant Entity_Id := Scope (Pack);
7221 function Is_Actual_Of_Previous_Formal (P : Entity_Id) return Boolean;
7222 -- The package in question may be an actual for a previous formal
7223 -- package P of the current instance, so examine its actuals as well.
7224 -- This must be recursive over other formal packages.
7226 ----------------------------------
7227 -- Is_Actual_Of_Previous_Formal --
7228 ----------------------------------
7230 function Is_Actual_Of_Previous_Formal (P : Entity_Id) return Boolean is
7234 E1 := First_Entity (P);
7235 while Present (E1) and then E1 /= Instance loop
7236 if Ekind (E1) = E_Package
7237 and then Nkind (Parent (E1)) = N_Package_Renaming_Declaration
7239 if Renamed_Object (E1) = Pack then
7242 elsif E1 = P or else Renamed_Object (E1) = P then
7245 elsif Is_Actual_Of_Previous_Formal (E1) then
7254 end Is_Actual_Of_Previous_Formal;
7256 -- Start of processing for Denotes_Formal_Package
7262 (Instance_Envs.Last).Instantiated_Parent.Act_Id;
7264 Par := Current_Instantiated_Parent.Act_Id;
7267 if Ekind (Scop) = E_Generic_Package
7268 or else Nkind (Unit_Declaration_Node (Scop)) =
7269 N_Generic_Subprogram_Declaration
7273 elsif Nkind (Original_Node (Unit_Declaration_Node (Pack))) =
7274 N_Formal_Package_Declaration
7282 -- Check whether this package is associated with a formal package of
7283 -- the enclosing instantiation. Iterate over the list of renamings.
7285 E := First_Entity (Par);
7286 while Present (E) loop
7287 if Ekind (E) /= E_Package
7288 or else Nkind (Parent (E)) /= N_Package_Renaming_Declaration
7292 elsif Renamed_Object (E) = Par then
7295 elsif Renamed_Object (E) = Pack then
7298 elsif Is_Actual_Of_Previous_Formal (E) then
7308 end Denotes_Formal_Package;
7314 procedure End_Generic is
7316 -- ??? More things could be factored out in this routine. Should
7317 -- probably be done at a later stage.
7319 Inside_A_Generic := Generic_Flags.Table (Generic_Flags.Last);
7320 Generic_Flags.Decrement_Last;
7322 Expander_Mode_Restore;
7329 function Earlier (N1, N2 : Node_Id) return Boolean is
7330 procedure Find_Depth (P : in out Node_Id; D : in out Integer);
7331 -- Find distance from given node to enclosing compilation unit
7337 procedure Find_Depth (P : in out Node_Id; D : in out Integer) is
7340 and then Nkind (P) /= N_Compilation_Unit
7342 P := True_Parent (P);
7347 -- Local declarations
7356 -- Start of processing for Earlier
7359 Find_Depth (P1, D1);
7360 Find_Depth (P2, D2);
7370 P1 := True_Parent (P1);
7375 P2 := True_Parent (P2);
7379 -- At this point P1 and P2 are at the same distance from the root.
7380 -- We examine their parents until we find a common declarative list.
7381 -- If we reach the root, N1 and N2 do not descend from the same
7382 -- declarative list (e.g. one is nested in the declarative part and
7383 -- the other is in a block in the statement part) and the earlier
7384 -- one is already frozen.
7386 while not Is_List_Member (P1)
7387 or else not Is_List_Member (P2)
7388 or else List_Containing (P1) /= List_Containing (P2)
7390 P1 := True_Parent (P1);
7391 P2 := True_Parent (P2);
7393 if Nkind (Parent (P1)) = N_Subunit then
7394 P1 := Corresponding_Stub (Parent (P1));
7397 if Nkind (Parent (P2)) = N_Subunit then
7398 P2 := Corresponding_Stub (Parent (P2));
7406 -- Expanded code usually shares the source location of the original
7407 -- construct it was generated for. This however may not necessarely
7408 -- reflect the true location of the code within the tree.
7410 -- Before comparing the slocs of the two nodes, make sure that we are
7411 -- working with correct source locations. Assume that P1 is to the left
7412 -- of P2. If either one does not come from source, traverse the common
7413 -- list heading towards the other node and locate the first source
7417 -- ----+===+===+--------------+===+===+----
7418 -- expanded code expanded code
7420 if not Comes_From_Source (P1) then
7421 while Present (P1) loop
7423 -- Neither P2 nor a source statement were located during the
7424 -- search. If we reach the end of the list, then P1 does not
7425 -- occur earlier than P2.
7428 -- start --- P2 ----- P1 --- end
7430 if No (Next (P1)) then
7433 -- We encounter P2 while going to the right of the list. This
7434 -- means that P1 does indeed appear earlier.
7437 -- start --- P1 ===== P2 --- end
7438 -- expanded code in between
7443 -- No need to look any further since we have located a source
7446 elsif Comes_From_Source (P1) then
7456 if not Comes_From_Source (P2) then
7457 while Present (P2) loop
7459 -- Neither P1 nor a source statement were located during the
7460 -- search. If we reach the start of the list, then P1 does not
7461 -- occur earlier than P2.
7464 -- start --- P2 --- P1 --- end
7466 if No (Prev (P2)) then
7469 -- We encounter P1 while going to the left of the list. This
7470 -- means that P1 does indeed appear earlier.
7473 -- start --- P1 ===== P2 --- end
7474 -- expanded code in between
7479 -- No need to look any further since we have located a source
7482 elsif Comes_From_Source (P2) then
7492 -- At this point either both nodes came from source or we approximated
7493 -- their source locations through neighbouring source statements.
7495 T1 := Top_Level_Location (Sloc (P1));
7496 T2 := Top_Level_Location (Sloc (P2));
7498 -- When two nodes come from the same instance, they have identical top
7499 -- level locations. To determine proper relation within the tree, check
7500 -- their locations within the template.
7503 return Sloc (P1) < Sloc (P2);
7505 -- The two nodes either come from unrelated instances or do not come
7506 -- from instantiated code at all.
7513 ----------------------
7514 -- Find_Actual_Type --
7515 ----------------------
7517 function Find_Actual_Type
7519 Gen_Type : Entity_Id) return Entity_Id
7521 Gen_Scope : constant Entity_Id := Scope (Gen_Type);
7525 -- Special processing only applies to child units
7527 if not Is_Child_Unit (Gen_Scope) then
7528 return Get_Instance_Of (Typ);
7530 -- If designated or component type is itself a formal of the child unit,
7531 -- its instance is available.
7533 elsif Scope (Typ) = Gen_Scope then
7534 return Get_Instance_Of (Typ);
7536 -- If the array or access type is not declared in the parent unit,
7537 -- no special processing needed.
7539 elsif not Is_Generic_Type (Typ)
7540 and then Scope (Gen_Scope) /= Scope (Typ)
7542 return Get_Instance_Of (Typ);
7544 -- Otherwise, retrieve designated or component type by visibility
7547 T := Current_Entity (Typ);
7548 while Present (T) loop
7549 if In_Open_Scopes (Scope (T)) then
7552 elsif Is_Generic_Actual_Type (T) then
7561 end Find_Actual_Type;
7563 ----------------------------
7564 -- Freeze_Subprogram_Body --
7565 ----------------------------
7567 procedure Freeze_Subprogram_Body
7568 (Inst_Node : Node_Id;
7570 Pack_Id : Entity_Id)
7572 Gen_Unit : constant Entity_Id := Get_Generic_Entity (Inst_Node);
7573 Par : constant Entity_Id := Scope (Gen_Unit);
7579 function Enclosing_Package_Body (N : Node_Id) return Node_Id;
7580 -- Find innermost package body that encloses the given node, and which
7581 -- is not a compilation unit. Freeze nodes for the instance, or for its
7582 -- enclosing body, may be inserted after the enclosing_body of the
7583 -- generic unit. Used to determine proper placement of freeze node for
7584 -- both package and subprogram instances.
7586 function Package_Freeze_Node (B : Node_Id) return Node_Id;
7587 -- Find entity for given package body, and locate or create a freeze
7590 ----------------------------
7591 -- Enclosing_Package_Body --
7592 ----------------------------
7594 function Enclosing_Package_Body (N : Node_Id) return Node_Id is
7600 and then Nkind (Parent (P)) /= N_Compilation_Unit
7602 if Nkind (P) = N_Package_Body then
7603 if Nkind (Parent (P)) = N_Subunit then
7604 return Corresponding_Stub (Parent (P));
7610 P := True_Parent (P);
7614 end Enclosing_Package_Body;
7616 -------------------------
7617 -- Package_Freeze_Node --
7618 -------------------------
7620 function Package_Freeze_Node (B : Node_Id) return Node_Id is
7624 if Nkind (B) = N_Package_Body then
7625 Id := Corresponding_Spec (B);
7626 else pragma Assert (Nkind (B) = N_Package_Body_Stub);
7627 Id := Corresponding_Spec (Proper_Body (Unit (Library_Unit (B))));
7630 Ensure_Freeze_Node (Id);
7631 return Freeze_Node (Id);
7632 end Package_Freeze_Node;
7634 -- Start of processing of Freeze_Subprogram_Body
7637 -- If the instance and the generic body appear within the same unit, and
7638 -- the instance precedes the generic, the freeze node for the instance
7639 -- must appear after that of the generic. If the generic is nested
7640 -- within another instance I2, then current instance must be frozen
7641 -- after I2. In both cases, the freeze nodes are those of enclosing
7642 -- packages. Otherwise, the freeze node is placed at the end of the
7643 -- current declarative part.
7645 Enc_G := Enclosing_Package_Body (Gen_Body);
7646 Enc_I := Enclosing_Package_Body (Inst_Node);
7647 Ensure_Freeze_Node (Pack_Id);
7648 F_Node := Freeze_Node (Pack_Id);
7650 if Is_Generic_Instance (Par)
7651 and then Present (Freeze_Node (Par))
7652 and then In_Same_Declarative_Part (Freeze_Node (Par), Inst_Node)
7654 -- The parent was a premature instantiation. Insert freeze node at
7655 -- the end the current declarative part.
7657 if ABE_Is_Certain (Get_Package_Instantiation_Node (Par)) then
7658 Insert_Freeze_Node_For_Instance (Inst_Node, F_Node);
7660 -- Handle the following case:
7662 -- package Parent_Inst is new ...
7665 -- procedure P ... -- this body freezes Parent_Inst
7667 -- package Inst is new ...
7669 -- In this particular scenario, the freeze node for Inst must be
7670 -- inserted in the same manner as that of Parent_Inst - before the
7671 -- next source body or at the end of the declarative list (body not
7672 -- available). If body P did not exist and Parent_Inst was frozen
7673 -- after Inst, either by a body following Inst or at the end of the
7674 -- declarative region, the freeze node for Inst must be inserted
7675 -- after that of Parent_Inst. This relation is established by
7676 -- comparing the Slocs of Parent_Inst freeze node and Inst.
7678 elsif List_Containing (Get_Package_Instantiation_Node (Par)) =
7679 List_Containing (Inst_Node)
7680 and then Sloc (Freeze_Node (Par)) < Sloc (Inst_Node)
7682 Insert_Freeze_Node_For_Instance (Inst_Node, F_Node);
7685 Insert_After (Freeze_Node (Par), F_Node);
7688 -- The body enclosing the instance should be frozen after the body that
7689 -- includes the generic, because the body of the instance may make
7690 -- references to entities therein. If the two are not in the same
7691 -- declarative part, or if the one enclosing the instance is frozen
7692 -- already, freeze the instance at the end of the current declarative
7695 elsif Is_Generic_Instance (Par)
7696 and then Present (Freeze_Node (Par))
7697 and then Present (Enc_I)
7699 if In_Same_Declarative_Part (Freeze_Node (Par), Enc_I)
7701 (Nkind (Enc_I) = N_Package_Body
7703 In_Same_Declarative_Part (Freeze_Node (Par), Parent (Enc_I)))
7705 -- The enclosing package may contain several instances. Rather
7706 -- than computing the earliest point at which to insert its freeze
7707 -- node, we place it at the end of the declarative part of the
7708 -- parent of the generic.
7710 Insert_Freeze_Node_For_Instance
7711 (Freeze_Node (Par), Package_Freeze_Node (Enc_I));
7714 Insert_Freeze_Node_For_Instance (Inst_Node, F_Node);
7716 elsif Present (Enc_G)
7717 and then Present (Enc_I)
7718 and then Enc_G /= Enc_I
7719 and then Earlier (Inst_Node, Gen_Body)
7721 if Nkind (Enc_G) = N_Package_Body then
7722 E_G_Id := Corresponding_Spec (Enc_G);
7723 else pragma Assert (Nkind (Enc_G) = N_Package_Body_Stub);
7725 Corresponding_Spec (Proper_Body (Unit (Library_Unit (Enc_G))));
7728 -- Freeze package that encloses instance, and place node after the
7729 -- package that encloses generic. If enclosing package is already
7730 -- frozen we have to assume it is at the proper place. This may be a
7731 -- potential ABE that requires dynamic checking. Do not add a freeze
7732 -- node if the package that encloses the generic is inside the body
7733 -- that encloses the instance, because the freeze node would be in
7734 -- the wrong scope. Additional contortions needed if the bodies are
7735 -- within a subunit.
7738 Enclosing_Body : Node_Id;
7741 if Nkind (Enc_I) = N_Package_Body_Stub then
7742 Enclosing_Body := Proper_Body (Unit (Library_Unit (Enc_I)));
7744 Enclosing_Body := Enc_I;
7747 if Parent (List_Containing (Enc_G)) /= Enclosing_Body then
7748 Insert_Freeze_Node_For_Instance
7749 (Enc_G, Package_Freeze_Node (Enc_I));
7753 -- Freeze enclosing subunit before instance
7755 Ensure_Freeze_Node (E_G_Id);
7757 if not Is_List_Member (Freeze_Node (E_G_Id)) then
7758 Insert_After (Enc_G, Freeze_Node (E_G_Id));
7761 Insert_Freeze_Node_For_Instance (Inst_Node, F_Node);
7764 -- If none of the above, insert freeze node at the end of the current
7765 -- declarative part.
7767 Insert_Freeze_Node_For_Instance (Inst_Node, F_Node);
7769 end Freeze_Subprogram_Body;
7775 function Get_Gen_Id (E : Assoc_Ptr) return Entity_Id is
7777 return Generic_Renamings.Table (E).Gen_Id;
7780 ---------------------
7781 -- Get_Instance_Of --
7782 ---------------------
7784 function Get_Instance_Of (A : Entity_Id) return Entity_Id is
7785 Res : constant Assoc_Ptr := Generic_Renamings_HTable.Get (A);
7788 if Res /= Assoc_Null then
7789 return Generic_Renamings.Table (Res).Act_Id;
7791 -- On exit, entity is not instantiated: not a generic parameter, or
7792 -- else parameter of an inner generic unit.
7796 end Get_Instance_Of;
7798 ------------------------------------
7799 -- Get_Package_Instantiation_Node --
7800 ------------------------------------
7802 function Get_Package_Instantiation_Node (A : Entity_Id) return Node_Id is
7803 Decl : Node_Id := Unit_Declaration_Node (A);
7807 -- If the Package_Instantiation attribute has been set on the package
7808 -- entity, then use it directly when it (or its Original_Node) refers
7809 -- to an N_Package_Instantiation node. In principle it should be
7810 -- possible to have this field set in all cases, which should be
7811 -- investigated, and would allow this function to be significantly
7814 Inst := Package_Instantiation (A);
7816 if Present (Inst) then
7817 if Nkind (Inst) = N_Package_Instantiation then
7820 elsif Nkind (Original_Node (Inst)) = N_Package_Instantiation then
7821 return Original_Node (Inst);
7825 -- If the instantiation is a compilation unit that does not need body
7826 -- then the instantiation node has been rewritten as a package
7827 -- declaration for the instance, and we return the original node.
7829 -- If it is a compilation unit and the instance node has not been
7830 -- rewritten, then it is still the unit of the compilation. Finally, if
7831 -- a body is present, this is a parent of the main unit whose body has
7832 -- been compiled for inlining purposes, and the instantiation node has
7833 -- been rewritten with the instance body.
7835 -- Otherwise the instantiation node appears after the declaration. If
7836 -- the entity is a formal package, the declaration may have been
7837 -- rewritten as a generic declaration (in the case of a formal with box)
7838 -- or left as a formal package declaration if it has actuals, and is
7839 -- found with a forward search.
7841 if Nkind (Parent (Decl)) = N_Compilation_Unit then
7842 if Nkind (Decl) = N_Package_Declaration
7843 and then Present (Corresponding_Body (Decl))
7845 Decl := Unit_Declaration_Node (Corresponding_Body (Decl));
7848 if Nkind (Original_Node (Decl)) = N_Package_Instantiation then
7849 return Original_Node (Decl);
7851 return Unit (Parent (Decl));
7854 elsif Nkind (Decl) = N_Package_Declaration
7855 and then Nkind (Original_Node (Decl)) = N_Formal_Package_Declaration
7857 return Original_Node (Decl);
7860 Inst := Next (Decl);
7861 while not Nkind_In (Inst, N_Package_Instantiation,
7862 N_Formal_Package_Declaration)
7869 end Get_Package_Instantiation_Node;
7871 ------------------------
7872 -- Has_Been_Exchanged --
7873 ------------------------
7875 function Has_Been_Exchanged (E : Entity_Id) return Boolean is
7879 Next := First_Elmt (Exchanged_Views);
7880 while Present (Next) loop
7881 if Full_View (Node (Next)) = E then
7889 end Has_Been_Exchanged;
7895 function Hash (F : Entity_Id) return HTable_Range is
7897 return HTable_Range (F mod HTable_Size);
7900 ------------------------
7901 -- Hide_Current_Scope --
7902 ------------------------
7904 procedure Hide_Current_Scope is
7905 C : constant Entity_Id := Current_Scope;
7909 Set_Is_Hidden_Open_Scope (C);
7911 E := First_Entity (C);
7912 while Present (E) loop
7913 if Is_Immediately_Visible (E) then
7914 Set_Is_Immediately_Visible (E, False);
7915 Append_Elmt (E, Hidden_Entities);
7921 -- Make the scope name invisible as well. This is necessary, but might
7922 -- conflict with calls to Rtsfind later on, in case the scope is a
7923 -- predefined one. There is no clean solution to this problem, so for
7924 -- now we depend on the user not redefining Standard itself in one of
7925 -- the parent units.
7927 if Is_Immediately_Visible (C) and then C /= Standard_Standard then
7928 Set_Is_Immediately_Visible (C, False);
7929 Append_Elmt (C, Hidden_Entities);
7932 end Hide_Current_Scope;
7938 procedure Init_Env is
7939 Saved : Instance_Env;
7942 Saved.Instantiated_Parent := Current_Instantiated_Parent;
7943 Saved.Exchanged_Views := Exchanged_Views;
7944 Saved.Hidden_Entities := Hidden_Entities;
7945 Saved.Current_Sem_Unit := Current_Sem_Unit;
7946 Saved.Parent_Unit_Visible := Parent_Unit_Visible;
7947 Saved.Instance_Parent_Unit := Instance_Parent_Unit;
7949 -- Save configuration switches. These may be reset if the unit is a
7950 -- predefined unit, and the current mode is not Ada 2005.
7952 Save_Opt_Config_Switches (Saved.Switches);
7954 Instance_Envs.Append (Saved);
7956 Exchanged_Views := New_Elmt_List;
7957 Hidden_Entities := New_Elmt_List;
7959 -- Make dummy entry for Instantiated parent. If generic unit is legal,
7960 -- this is set properly in Set_Instance_Env.
7962 Current_Instantiated_Parent :=
7963 (Current_Scope, Current_Scope, Assoc_Null);
7966 ------------------------------
7967 -- In_Same_Declarative_Part --
7968 ------------------------------
7970 function In_Same_Declarative_Part
7972 Inst : Node_Id) return Boolean
7974 Decls : constant Node_Id := Parent (F_Node);
7975 Nod : Node_Id := Parent (Inst);
7978 while Present (Nod) loop
7982 elsif Nkind_In (Nod, N_Subprogram_Body,
7984 N_Package_Declaration,
7991 elsif Nkind (Nod) = N_Subunit then
7992 Nod := Corresponding_Stub (Nod);
7994 elsif Nkind (Nod) = N_Compilation_Unit then
7998 Nod := Parent (Nod);
8003 end In_Same_Declarative_Part;
8005 ---------------------
8006 -- In_Main_Context --
8007 ---------------------
8009 function In_Main_Context (E : Entity_Id) return Boolean is
8015 if not Is_Compilation_Unit (E)
8016 or else Ekind (E) /= E_Package
8017 or else In_Private_Part (E)
8022 Context := Context_Items (Cunit (Main_Unit));
8024 Clause := First (Context);
8025 while Present (Clause) loop
8026 if Nkind (Clause) = N_With_Clause then
8027 Nam := Name (Clause);
8029 -- If the current scope is part of the context of the main unit,
8030 -- analysis of the corresponding with_clause is not complete, and
8031 -- the entity is not set. We use the Chars field directly, which
8032 -- might produce false positives in rare cases, but guarantees
8033 -- that we produce all the instance bodies we will need.
8035 if (Is_Entity_Name (Nam) and then Chars (Nam) = Chars (E))
8036 or else (Nkind (Nam) = N_Selected_Component
8037 and then Chars (Selector_Name (Nam)) = Chars (E))
8047 end In_Main_Context;
8049 ---------------------
8050 -- Inherit_Context --
8051 ---------------------
8053 procedure Inherit_Context (Gen_Decl : Node_Id; Inst : Node_Id) is
8054 Current_Context : List_Id;
8055 Current_Unit : Node_Id;
8064 if Nkind (Parent (Gen_Decl)) = N_Compilation_Unit then
8066 -- The inherited context is attached to the enclosing compilation
8067 -- unit. This is either the main unit, or the declaration for the
8068 -- main unit (in case the instantiation appears within the package
8069 -- declaration and the main unit is its body).
8071 Current_Unit := Parent (Inst);
8072 while Present (Current_Unit)
8073 and then Nkind (Current_Unit) /= N_Compilation_Unit
8075 Current_Unit := Parent (Current_Unit);
8078 Current_Context := Context_Items (Current_Unit);
8080 Item := First (Context_Items (Parent (Gen_Decl)));
8081 while Present (Item) loop
8082 if Nkind (Item) = N_With_Clause then
8083 Lib_Unit := Library_Unit (Item);
8085 -- Take care to prevent direct cyclic with's
8087 if Lib_Unit /= Current_Unit then
8089 -- Do not add a unit if it is already in the context
8091 Clause := First (Current_Context);
8093 while Present (Clause) loop
8094 if Nkind (Clause) = N_With_Clause and then
8095 Library_Unit (Clause) = Lib_Unit
8105 New_I := New_Copy (Item);
8106 Set_Implicit_With (New_I, True);
8107 Set_Implicit_With_From_Instantiation (New_I, True);
8108 Append (New_I, Current_Context);
8116 end Inherit_Context;
8122 procedure Initialize is
8124 Generic_Renamings.Init;
8127 Generic_Renamings_HTable.Reset;
8128 Circularity_Detected := False;
8129 Exchanged_Views := No_Elist;
8130 Hidden_Entities := No_Elist;
8133 -------------------------------------
8134 -- Insert_Freeze_Node_For_Instance --
8135 -------------------------------------
8137 procedure Insert_Freeze_Node_For_Instance
8146 function Enclosing_Body (N : Node_Id) return Node_Id;
8147 -- Find enclosing package or subprogram body, if any. Freeze node may
8148 -- be placed at end of current declarative list if previous instance
8149 -- and current one have different enclosing bodies.
8151 function Previous_Instance (Gen : Entity_Id) return Entity_Id;
8152 -- Find the local instance, if any, that declares the generic that is
8153 -- being instantiated. If present, the freeze node for this instance
8154 -- must follow the freeze node for the previous instance.
8156 --------------------
8157 -- Enclosing_Body --
8158 --------------------
8160 function Enclosing_Body (N : Node_Id) return Node_Id is
8166 and then Nkind (Parent (P)) /= N_Compilation_Unit
8168 if Nkind_In (P, N_Package_Body, N_Subprogram_Body) then
8169 if Nkind (Parent (P)) = N_Subunit then
8170 return Corresponding_Stub (Parent (P));
8176 P := True_Parent (P);
8182 -----------------------
8183 -- Previous_Instance --
8184 -----------------------
8186 function Previous_Instance (Gen : Entity_Id) return Entity_Id is
8192 and then S /= Standard_Standard
8194 if Is_Generic_Instance (S)
8195 and then In_Same_Source_Unit (S, N)
8204 end Previous_Instance;
8206 -- Start of processing for Insert_Freeze_Node_For_Instance
8209 if not Is_List_Member (F_Node) then
8211 Decls := List_Containing (N);
8212 Inst := Entity (F_Node);
8213 Par_N := Parent (Decls);
8215 -- When processing a subprogram instantiation, utilize the actual
8216 -- subprogram instantiation rather than its package wrapper as it
8217 -- carries all the context information.
8219 if Is_Wrapper_Package (Inst) then
8220 Inst := Related_Instance (Inst);
8223 -- If this is a package instance, check whether the generic is
8224 -- declared in a previous instance and the current instance is
8225 -- not within the previous one.
8227 if Present (Generic_Parent (Parent (Inst)))
8228 and then Is_In_Main_Unit (N)
8231 Enclosing_N : constant Node_Id := Enclosing_Body (N);
8232 Par_I : constant Entity_Id :=
8234 (Generic_Parent (Parent (Inst)));
8239 and then Earlier (N, Freeze_Node (Par_I))
8241 Scop := Scope (Inst);
8243 -- If the current instance is within the one that contains
8244 -- the generic, the freeze node for the current one must
8245 -- appear in the current declarative part. Ditto, if the
8246 -- current instance is within another package instance or
8247 -- within a body that does not enclose the current instance.
8248 -- In these three cases the freeze node of the previous
8249 -- instance is not relevant.
8251 while Present (Scop)
8252 and then Scop /= Standard_Standard
8254 exit when Scop = Par_I
8256 (Is_Generic_Instance (Scop)
8257 and then Scope_Depth (Scop) > Scope_Depth (Par_I));
8258 Scop := Scope (Scop);
8261 -- Previous instance encloses current instance
8263 if Scop = Par_I then
8266 -- If the next node is a source body we must freeze in
8267 -- the current scope as well.
8269 elsif Present (Next (N))
8270 and then Nkind_In (Next (N),
8271 N_Subprogram_Body, N_Package_Body)
8272 and then Comes_From_Source (Next (N))
8276 -- Current instance is within an unrelated instance
8278 elsif Is_Generic_Instance (Scop) then
8281 -- Current instance is within an unrelated body
8283 elsif Present (Enclosing_N)
8284 and then Enclosing_N /= Enclosing_Body (Par_I)
8289 Insert_After (Freeze_Node (Par_I), F_Node);
8296 -- When the instantiation occurs in a package declaration, append the
8297 -- freeze node to the private declarations (if any).
8299 if Nkind (Par_N) = N_Package_Specification
8300 and then Decls = Visible_Declarations (Par_N)
8301 and then Present (Private_Declarations (Par_N))
8302 and then not Is_Empty_List (Private_Declarations (Par_N))
8304 Decls := Private_Declarations (Par_N);
8305 Decl := First (Decls);
8308 -- Determine the proper freeze point of a package instantiation. We
8309 -- adhere to the general rule of a package or subprogram body causing
8310 -- freezing of anything before it in the same declarative region. In
8311 -- this case, the proper freeze point of a package instantiation is
8312 -- before the first source body which follows, or before a stub. This
8313 -- ensures that entities coming from the instance are already frozen
8314 -- and usable in source bodies.
8316 if Nkind (Par_N) /= N_Package_Declaration
8317 and then Ekind (Inst) = E_Package
8318 and then Is_Generic_Instance (Inst)
8320 not In_Same_Source_Unit (Generic_Parent (Parent (Inst)), Inst)
8322 while Present (Decl) loop
8323 if (Nkind (Decl) in N_Unit_Body
8325 Nkind (Decl) in N_Body_Stub)
8326 and then Comes_From_Source (Decl)
8328 Insert_Before (Decl, F_Node);
8336 -- In a package declaration, or if no previous body, insert at end
8339 Set_Sloc (F_Node, Sloc (Last (Decls)));
8340 Insert_After (Last (Decls), F_Node);
8342 end Insert_Freeze_Node_For_Instance;
8348 procedure Install_Body
8349 (Act_Body : Node_Id;
8354 Act_Id : constant Entity_Id := Corresponding_Spec (Act_Body);
8355 Act_Unit : constant Node_Id := Unit (Cunit (Get_Source_Unit (N)));
8356 Gen_Id : constant Entity_Id := Corresponding_Spec (Gen_Body);
8357 Par : constant Entity_Id := Scope (Gen_Id);
8358 Gen_Unit : constant Node_Id :=
8359 Unit (Cunit (Get_Source_Unit (Gen_Decl)));
8360 Orig_Body : Node_Id := Gen_Body;
8362 Body_Unit : Node_Id;
8364 Must_Delay : Boolean;
8366 function In_Same_Enclosing_Subp return Boolean;
8367 -- Check whether instance and generic body are within same subprogram.
8369 function True_Sloc (N : Node_Id) return Source_Ptr;
8370 -- If the instance is nested inside a generic unit, the Sloc of the
8371 -- instance indicates the place of the original definition, not the
8372 -- point of the current enclosing instance. Pending a better usage of
8373 -- Slocs to indicate instantiation places, we determine the place of
8374 -- origin of a node by finding the maximum sloc of any ancestor node.
8375 -- Why is this not equivalent to Top_Level_Location ???
8377 ----------------------------
8378 -- In_Same_Enclosing_Subp --
8379 ----------------------------
8381 function In_Same_Enclosing_Subp return Boolean is
8386 Scop := Scope (Act_Id);
8387 while Scop /= Standard_Standard
8388 and then not Is_Overloadable (Scop)
8390 Scop := Scope (Scop);
8393 if Scop = Standard_Standard then
8399 Scop := Scope (Gen_Id);
8400 while Scop /= Standard_Standard loop
8404 Scop := Scope (Scop);
8409 end In_Same_Enclosing_Subp;
8415 function True_Sloc (N : Node_Id) return Source_Ptr is
8422 while Present (N1) and then N1 /= Act_Unit loop
8423 if Sloc (N1) > Res then
8433 -- Start of processing for Install_Body
8436 -- If the body is a subunit, the freeze point is the corresponding stub
8437 -- in the current compilation, not the subunit itself.
8439 if Nkind (Parent (Gen_Body)) = N_Subunit then
8440 Orig_Body := Corresponding_Stub (Parent (Gen_Body));
8442 Orig_Body := Gen_Body;
8445 Body_Unit := Unit (Cunit (Get_Source_Unit (Orig_Body)));
8447 -- If the instantiation and the generic definition appear in the same
8448 -- package declaration, this is an early instantiation. If they appear
8449 -- in the same declarative part, it is an early instantiation only if
8450 -- the generic body appears textually later, and the generic body is
8451 -- also in the main unit.
8453 -- If instance is nested within a subprogram, and the generic body
8454 -- is not, the instance is delayed because the enclosing body is. If
8455 -- instance and body are within the same scope, or the same subprogram
8456 -- body, indicate explicitly that the instance is delayed.
8459 (Gen_Unit = Act_Unit
8460 and then (Nkind_In (Gen_Unit, N_Package_Declaration,
8461 N_Generic_Package_Declaration)
8462 or else (Gen_Unit = Body_Unit
8463 and then True_Sloc (N) < Sloc (Orig_Body)))
8464 and then Is_In_Main_Unit (Gen_Unit)
8465 and then (Scope (Act_Id) = Scope (Gen_Id)
8466 or else In_Same_Enclosing_Subp));
8468 -- If this is an early instantiation, the freeze node is placed after
8469 -- the generic body. Otherwise, if the generic appears in an instance,
8470 -- we cannot freeze the current instance until the outer one is frozen.
8471 -- This is only relevant if the current instance is nested within some
8472 -- inner scope not itself within the outer instance. If this scope is
8473 -- a package body in the same declarative part as the outer instance,
8474 -- then that body needs to be frozen after the outer instance. Finally,
8475 -- if no delay is needed, we place the freeze node at the end of the
8476 -- current declarative part.
8478 if Expander_Active then
8479 Ensure_Freeze_Node (Act_Id);
8480 F_Node := Freeze_Node (Act_Id);
8483 Insert_After (Orig_Body, F_Node);
8485 elsif Is_Generic_Instance (Par)
8486 and then Present (Freeze_Node (Par))
8487 and then Scope (Act_Id) /= Par
8489 -- Freeze instance of inner generic after instance of enclosing
8492 if In_Same_Declarative_Part (Freeze_Node (Par), N) then
8494 -- Handle the following case:
8496 -- package Parent_Inst is new ...
8499 -- procedure P ... -- this body freezes Parent_Inst
8501 -- package Inst is new ...
8503 -- In this particular scenario, the freeze node for Inst must
8504 -- be inserted in the same manner as that of Parent_Inst,
8505 -- before the next source body or at the end of the declarative
8506 -- list (body not available). If body P did not exist and
8507 -- Parent_Inst was frozen after Inst, either by a body
8508 -- following Inst or at the end of the declarative region,
8509 -- the freeze node for Inst must be inserted after that of
8510 -- Parent_Inst. This relation is established by comparing
8511 -- the Slocs of Parent_Inst freeze node and Inst.
8513 if List_Containing (Get_Package_Instantiation_Node (Par)) =
8515 and then Sloc (Freeze_Node (Par)) < Sloc (N)
8517 Insert_Freeze_Node_For_Instance (N, F_Node);
8519 Insert_After (Freeze_Node (Par), F_Node);
8522 -- Freeze package enclosing instance of inner generic after
8523 -- instance of enclosing generic.
8525 elsif Nkind_In (Parent (N), N_Package_Body, N_Subprogram_Body)
8526 and then In_Same_Declarative_Part (Freeze_Node (Par), Parent (N))
8529 Enclosing : Entity_Id;
8532 Enclosing := Corresponding_Spec (Parent (N));
8534 if No (Enclosing) then
8535 Enclosing := Defining_Entity (Parent (N));
8538 Insert_Freeze_Node_For_Instance (N, F_Node);
8539 Ensure_Freeze_Node (Enclosing);
8541 if not Is_List_Member (Freeze_Node (Enclosing)) then
8543 -- The enclosing context is a subunit, insert the freeze
8544 -- node after the stub.
8546 if Nkind (Parent (Parent (N))) = N_Subunit then
8547 Insert_Freeze_Node_For_Instance
8548 (Corresponding_Stub (Parent (Parent (N))),
8549 Freeze_Node (Enclosing));
8551 -- The enclosing context is a package with a stub body
8552 -- which has already been replaced by the real body.
8553 -- Insert the freeze node after the actual body.
8555 elsif Ekind (Enclosing) = E_Package
8556 and then Present (Body_Entity (Enclosing))
8557 and then Was_Originally_Stub
8558 (Parent (Body_Entity (Enclosing)))
8560 Insert_Freeze_Node_For_Instance
8561 (Parent (Body_Entity (Enclosing)),
8562 Freeze_Node (Enclosing));
8564 -- The parent instance has been frozen before the body of
8565 -- the enclosing package, insert the freeze node after
8568 elsif List_Containing (Freeze_Node (Par)) =
8569 List_Containing (Parent (N))
8570 and then Sloc (Freeze_Node (Par)) < Sloc (Parent (N))
8572 Insert_Freeze_Node_For_Instance
8573 (Parent (N), Freeze_Node (Enclosing));
8577 (Freeze_Node (Par), Freeze_Node (Enclosing));
8583 Insert_Freeze_Node_For_Instance (N, F_Node);
8587 Insert_Freeze_Node_For_Instance (N, F_Node);
8591 Set_Is_Frozen (Act_Id);
8592 Insert_Before (N, Act_Body);
8593 Mark_Rewrite_Insertion (Act_Body);
8596 -----------------------------
8597 -- Install_Formal_Packages --
8598 -----------------------------
8600 procedure Install_Formal_Packages (Par : Entity_Id) is
8603 Gen_E : Entity_Id := Empty;
8606 E := First_Entity (Par);
8608 -- If we are installing an instance parent, locate the formal packages
8609 -- of its generic parent.
8611 if Is_Generic_Instance (Par) then
8612 Gen := Generic_Parent (Package_Specification (Par));
8613 Gen_E := First_Entity (Gen);
8616 while Present (E) loop
8617 if Ekind (E) = E_Package
8618 and then Nkind (Parent (E)) = N_Package_Renaming_Declaration
8620 -- If this is the renaming for the parent instance, done
8622 if Renamed_Object (E) = Par then
8625 -- The visibility of a formal of an enclosing generic is already
8628 elsif Denotes_Formal_Package (E) then
8631 elsif Present (Associated_Formal_Package (E)) then
8632 Check_Generic_Actuals (Renamed_Object (E), True);
8633 Set_Is_Hidden (E, False);
8635 -- Find formal package in generic unit that corresponds to
8636 -- (instance of) formal package in instance.
8638 while Present (Gen_E) and then Chars (Gen_E) /= Chars (E) loop
8639 Next_Entity (Gen_E);
8642 if Present (Gen_E) then
8643 Map_Formal_Package_Entities (Gen_E, E);
8649 if Present (Gen_E) then
8650 Next_Entity (Gen_E);
8653 end Install_Formal_Packages;
8655 --------------------
8656 -- Install_Parent --
8657 --------------------
8659 procedure Install_Parent (P : Entity_Id; In_Body : Boolean := False) is
8660 Ancestors : constant Elist_Id := New_Elmt_List;
8661 S : constant Entity_Id := Current_Scope;
8662 Inst_Par : Entity_Id;
8663 First_Par : Entity_Id;
8664 Inst_Node : Node_Id;
8665 Gen_Par : Entity_Id;
8666 First_Gen : Entity_Id;
8669 procedure Install_Noninstance_Specs (Par : Entity_Id);
8670 -- Install the scopes of noninstance parent units ending with Par
8672 procedure Install_Spec (Par : Entity_Id);
8673 -- The child unit is within the declarative part of the parent, so the
8674 -- declarations within the parent are immediately visible.
8676 -------------------------------
8677 -- Install_Noninstance_Specs --
8678 -------------------------------
8680 procedure Install_Noninstance_Specs (Par : Entity_Id) is
8683 and then Par /= Standard_Standard
8684 and then not In_Open_Scopes (Par)
8686 Install_Noninstance_Specs (Scope (Par));
8689 end Install_Noninstance_Specs;
8695 procedure Install_Spec (Par : Entity_Id) is
8696 Spec : constant Node_Id := Package_Specification (Par);
8699 -- If this parent of the child instance is a top-level unit,
8700 -- then record the unit and its visibility for later resetting in
8701 -- Remove_Parent. We exclude units that are generic instances, as we
8702 -- only want to record this information for the ultimate top-level
8703 -- noninstance parent (is that always correct???).
8705 if Scope (Par) = Standard_Standard
8706 and then not Is_Generic_Instance (Par)
8708 Parent_Unit_Visible := Is_Immediately_Visible (Par);
8709 Instance_Parent_Unit := Par;
8712 -- Open the parent scope and make it and its declarations visible.
8713 -- If this point is not within a body, then only the visible
8714 -- declarations should be made visible, and installation of the
8715 -- private declarations is deferred until the appropriate point
8716 -- within analysis of the spec being instantiated (see the handling
8717 -- of parent visibility in Analyze_Package_Specification). This is
8718 -- relaxed in the case where the parent unit is Ada.Tags, to avoid
8719 -- private view problems that occur when compiling instantiations of
8720 -- a generic child of that package (Generic_Dispatching_Constructor).
8721 -- If the instance freezes a tagged type, inlinings of operations
8722 -- from Ada.Tags may need the full view of type Tag. If inlining took
8723 -- proper account of establishing visibility of inlined subprograms'
8724 -- parents then it should be possible to remove this
8725 -- special check. ???
8728 Set_Is_Immediately_Visible (Par);
8729 Install_Visible_Declarations (Par);
8730 Set_Use (Visible_Declarations (Spec));
8732 if In_Body or else Is_RTU (Par, Ada_Tags) then
8733 Install_Private_Declarations (Par);
8734 Set_Use (Private_Declarations (Spec));
8738 -- Start of processing for Install_Parent
8741 -- We need to install the parent instance to compile the instantiation
8742 -- of the child, but the child instance must appear in the current
8743 -- scope. Given that we cannot place the parent above the current scope
8744 -- in the scope stack, we duplicate the current scope and unstack both
8745 -- after the instantiation is complete.
8747 -- If the parent is itself the instantiation of a child unit, we must
8748 -- also stack the instantiation of its parent, and so on. Each such
8749 -- ancestor is the prefix of the name in a prior instantiation.
8751 -- If this is a nested instance, the parent unit itself resolves to
8752 -- a renaming of the parent instance, whose declaration we need.
8754 -- Finally, the parent may be a generic (not an instance) when the
8755 -- child unit appears as a formal package.
8759 if Present (Renamed_Entity (Inst_Par)) then
8760 Inst_Par := Renamed_Entity (Inst_Par);
8763 First_Par := Inst_Par;
8765 Gen_Par := Generic_Parent (Package_Specification (Inst_Par));
8767 First_Gen := Gen_Par;
8769 while Present (Gen_Par)
8770 and then Is_Child_Unit (Gen_Par)
8772 -- Load grandparent instance as well
8774 Inst_Node := Get_Package_Instantiation_Node (Inst_Par);
8776 if Nkind (Name (Inst_Node)) = N_Expanded_Name then
8777 Inst_Par := Entity (Prefix (Name (Inst_Node)));
8779 if Present (Renamed_Entity (Inst_Par)) then
8780 Inst_Par := Renamed_Entity (Inst_Par);
8783 Gen_Par := Generic_Parent (Package_Specification (Inst_Par));
8785 if Present (Gen_Par) then
8786 Prepend_Elmt (Inst_Par, Ancestors);
8789 -- Parent is not the name of an instantiation
8791 Install_Noninstance_Specs (Inst_Par);
8802 if Present (First_Gen) then
8803 Append_Elmt (First_Par, Ancestors);
8805 Install_Noninstance_Specs (First_Par);
8808 if not Is_Empty_Elmt_List (Ancestors) then
8809 Elmt := First_Elmt (Ancestors);
8810 while Present (Elmt) loop
8811 Install_Spec (Node (Elmt));
8812 Install_Formal_Packages (Node (Elmt));
8822 -------------------------------
8823 -- Install_Hidden_Primitives --
8824 -------------------------------
8826 procedure Install_Hidden_Primitives
8827 (Prims_List : in out Elist_Id;
8832 List : Elist_Id := No_Elist;
8833 Prim_G_Elmt : Elmt_Id;
8834 Prim_A_Elmt : Elmt_Id;
8839 -- No action needed in case of serious errors because we cannot trust
8840 -- in the order of primitives
8842 if Serious_Errors_Detected > 0 then
8845 -- No action possible if we don't have available the list of primitive
8849 or else not Is_Record_Type (Gen_T)
8850 or else not Is_Tagged_Type (Gen_T)
8851 or else not Is_Record_Type (Act_T)
8852 or else not Is_Tagged_Type (Act_T)
8856 -- There is no need to handle interface types since their primitives
8859 elsif Is_Interface (Gen_T) then
8863 Prim_G_Elmt := First_Elmt (Primitive_Operations (Gen_T));
8865 if not Is_Class_Wide_Type (Act_T) then
8866 Prim_A_Elmt := First_Elmt (Primitive_Operations (Act_T));
8868 Prim_A_Elmt := First_Elmt (Primitive_Operations (Root_Type (Act_T)));
8872 -- Skip predefined primitives in the generic formal
8874 while Present (Prim_G_Elmt)
8875 and then Is_Predefined_Dispatching_Operation (Node (Prim_G_Elmt))
8877 Next_Elmt (Prim_G_Elmt);
8880 -- Skip predefined primitives in the generic actual
8882 while Present (Prim_A_Elmt)
8883 and then Is_Predefined_Dispatching_Operation (Node (Prim_A_Elmt))
8885 Next_Elmt (Prim_A_Elmt);
8888 exit when No (Prim_G_Elmt) or else No (Prim_A_Elmt);
8890 Prim_G := Node (Prim_G_Elmt);
8891 Prim_A := Node (Prim_A_Elmt);
8893 -- There is no need to handle interface primitives because their
8894 -- primitives are not hidden
8896 exit when Present (Interface_Alias (Prim_G));
8898 -- Here we install one hidden primitive
8900 if Chars (Prim_G) /= Chars (Prim_A)
8901 and then Has_Suffix (Prim_A, 'P')
8902 and then Remove_Suffix (Prim_A, 'P') = Chars (Prim_G)
8904 Set_Chars (Prim_A, Chars (Prim_G));
8906 if List = No_Elist then
8907 List := New_Elmt_List;
8910 Append_Elmt (Prim_A, List);
8913 Next_Elmt (Prim_A_Elmt);
8914 Next_Elmt (Prim_G_Elmt);
8917 -- Append the elements to the list of temporarily visible primitives
8918 -- avoiding duplicates.
8920 if Present (List) then
8921 if No (Prims_List) then
8922 Prims_List := New_Elmt_List;
8925 Elmt := First_Elmt (List);
8926 while Present (Elmt) loop
8927 Append_Unique_Elmt (Node (Elmt), Prims_List);
8931 end Install_Hidden_Primitives;
8933 -------------------------------
8934 -- Restore_Hidden_Primitives --
8935 -------------------------------
8937 procedure Restore_Hidden_Primitives (Prims_List : in out Elist_Id) is
8938 Prim_Elmt : Elmt_Id;
8942 if Prims_List /= No_Elist then
8943 Prim_Elmt := First_Elmt (Prims_List);
8944 while Present (Prim_Elmt) loop
8945 Prim := Node (Prim_Elmt);
8946 Set_Chars (Prim, Add_Suffix (Prim, 'P'));
8947 Next_Elmt (Prim_Elmt);
8950 Prims_List := No_Elist;
8952 end Restore_Hidden_Primitives;
8954 --------------------------------
8955 -- Instantiate_Formal_Package --
8956 --------------------------------
8958 function Instantiate_Formal_Package
8961 Analyzed_Formal : Node_Id) return List_Id
8963 Loc : constant Source_Ptr := Sloc (Actual);
8964 Actual_Pack : Entity_Id;
8965 Formal_Pack : Entity_Id;
8966 Gen_Parent : Entity_Id;
8969 Parent_Spec : Node_Id;
8971 procedure Find_Matching_Actual
8973 Act : in out Entity_Id);
8974 -- We need to associate each formal entity in the formal package with
8975 -- the corresponding entity in the actual package. The actual package
8976 -- has been analyzed and possibly expanded, and as a result there is
8977 -- no one-to-one correspondence between the two lists (for example,
8978 -- the actual may include subtypes, itypes, and inherited primitive
8979 -- operations, interspersed among the renaming declarations for the
8980 -- actuals) . We retrieve the corresponding actual by name because each
8981 -- actual has the same name as the formal, and they do appear in the
8984 function Get_Formal_Entity (N : Node_Id) return Entity_Id;
8985 -- Retrieve entity of defining entity of generic formal parameter.
8986 -- Only the declarations of formals need to be considered when
8987 -- linking them to actuals, but the declarative list may include
8988 -- internal entities generated during analysis, and those are ignored.
8990 procedure Match_Formal_Entity
8991 (Formal_Node : Node_Id;
8992 Formal_Ent : Entity_Id;
8993 Actual_Ent : Entity_Id);
8994 -- Associates the formal entity with the actual. In the case where
8995 -- Formal_Ent is a formal package, this procedure iterates through all
8996 -- of its formals and enters associations between the actuals occurring
8997 -- in the formal package's corresponding actual package (given by
8998 -- Actual_Ent) and the formal package's formal parameters. This
8999 -- procedure recurses if any of the parameters is itself a package.
9001 function Is_Instance_Of
9002 (Act_Spec : Entity_Id;
9003 Gen_Anc : Entity_Id) return Boolean;
9004 -- The actual can be an instantiation of a generic within another
9005 -- instance, in which case there is no direct link from it to the
9006 -- original generic ancestor. In that case, we recognize that the
9007 -- ultimate ancestor is the same by examining names and scopes.
9009 procedure Process_Nested_Formal (Formal : Entity_Id);
9010 -- If the current formal is declared with a box, its own formals are
9011 -- visible in the instance, as they were in the generic, and their
9012 -- Hidden flag must be reset. If some of these formals are themselves
9013 -- packages declared with a box, the processing must be recursive.
9015 --------------------------
9016 -- Find_Matching_Actual --
9017 --------------------------
9019 procedure Find_Matching_Actual
9021 Act : in out Entity_Id)
9023 Formal_Ent : Entity_Id;
9026 case Nkind (Original_Node (F)) is
9027 when N_Formal_Object_Declaration |
9028 N_Formal_Type_Declaration =>
9029 Formal_Ent := Defining_Identifier (F);
9031 while Chars (Act) /= Chars (Formal_Ent) loop
9035 when N_Formal_Subprogram_Declaration |
9036 N_Formal_Package_Declaration |
9037 N_Package_Declaration |
9038 N_Generic_Package_Declaration =>
9039 Formal_Ent := Defining_Entity (F);
9041 while Chars (Act) /= Chars (Formal_Ent) loop
9046 raise Program_Error;
9048 end Find_Matching_Actual;
9050 -------------------------
9051 -- Match_Formal_Entity --
9052 -------------------------
9054 procedure Match_Formal_Entity
9055 (Formal_Node : Node_Id;
9056 Formal_Ent : Entity_Id;
9057 Actual_Ent : Entity_Id)
9059 Act_Pkg : Entity_Id;
9062 Set_Instance_Of (Formal_Ent, Actual_Ent);
9064 if Ekind (Actual_Ent) = E_Package then
9066 -- Record associations for each parameter
9068 Act_Pkg := Actual_Ent;
9071 A_Ent : Entity_Id := First_Entity (Act_Pkg);
9080 -- Retrieve the actual given in the formal package declaration
9082 Actual := Entity (Name (Original_Node (Formal_Node)));
9084 -- The actual in the formal package declaration may be a
9085 -- renamed generic package, in which case we want to retrieve
9086 -- the original generic in order to traverse its formal part.
9088 if Present (Renamed_Entity (Actual)) then
9089 Gen_Decl := Unit_Declaration_Node (Renamed_Entity (Actual));
9091 Gen_Decl := Unit_Declaration_Node (Actual);
9094 Formals := Generic_Formal_Declarations (Gen_Decl);
9096 if Present (Formals) then
9097 F_Node := First_Non_Pragma (Formals);
9102 while Present (A_Ent)
9103 and then Present (F_Node)
9104 and then A_Ent /= First_Private_Entity (Act_Pkg)
9106 F_Ent := Get_Formal_Entity (F_Node);
9108 if Present (F_Ent) then
9110 -- This is a formal of the original package. Record
9111 -- association and recurse.
9113 Find_Matching_Actual (F_Node, A_Ent);
9114 Match_Formal_Entity (F_Node, F_Ent, A_Ent);
9115 Next_Entity (A_Ent);
9118 Next_Non_Pragma (F_Node);
9122 end Match_Formal_Entity;
9124 -----------------------
9125 -- Get_Formal_Entity --
9126 -----------------------
9128 function Get_Formal_Entity (N : Node_Id) return Entity_Id is
9129 Kind : constant Node_Kind := Nkind (Original_Node (N));
9132 when N_Formal_Object_Declaration =>
9133 return Defining_Identifier (N);
9135 when N_Formal_Type_Declaration =>
9136 return Defining_Identifier (N);
9138 when N_Formal_Subprogram_Declaration =>
9139 return Defining_Unit_Name (Specification (N));
9141 when N_Formal_Package_Declaration =>
9142 return Defining_Identifier (Original_Node (N));
9144 when N_Generic_Package_Declaration =>
9145 return Defining_Identifier (Original_Node (N));
9147 -- All other declarations are introduced by semantic analysis and
9148 -- have no match in the actual.
9153 end Get_Formal_Entity;
9155 --------------------
9156 -- Is_Instance_Of --
9157 --------------------
9159 function Is_Instance_Of
9160 (Act_Spec : Entity_Id;
9161 Gen_Anc : Entity_Id) return Boolean
9163 Gen_Par : constant Entity_Id := Generic_Parent (Act_Spec);
9166 if No (Gen_Par) then
9169 -- Simplest case: the generic parent of the actual is the formal
9171 elsif Gen_Par = Gen_Anc then
9174 elsif Chars (Gen_Par) /= Chars (Gen_Anc) then
9177 -- The actual may be obtained through several instantiations. Its
9178 -- scope must itself be an instance of a generic declared in the
9179 -- same scope as the formal. Any other case is detected above.
9181 elsif not Is_Generic_Instance (Scope (Gen_Par)) then
9185 return Generic_Parent (Parent (Scope (Gen_Par))) = Scope (Gen_Anc);
9189 ---------------------------
9190 -- Process_Nested_Formal --
9191 ---------------------------
9193 procedure Process_Nested_Formal (Formal : Entity_Id) is
9197 if Present (Associated_Formal_Package (Formal))
9198 and then Box_Present (Parent (Associated_Formal_Package (Formal)))
9200 Ent := First_Entity (Formal);
9201 while Present (Ent) loop
9202 Set_Is_Hidden (Ent, False);
9203 Set_Is_Visible_Formal (Ent);
9204 Set_Is_Potentially_Use_Visible
9205 (Ent, Is_Potentially_Use_Visible (Formal));
9207 if Ekind (Ent) = E_Package then
9208 exit when Renamed_Entity (Ent) = Renamed_Entity (Formal);
9209 Process_Nested_Formal (Ent);
9215 end Process_Nested_Formal;
9217 -- Start of processing for Instantiate_Formal_Package
9222 if not Is_Entity_Name (Actual)
9223 or else Ekind (Entity (Actual)) /= E_Package
9226 ("expect package instance to instantiate formal", Actual);
9227 Abandon_Instantiation (Actual);
9228 raise Program_Error;
9231 Actual_Pack := Entity (Actual);
9232 Set_Is_Instantiated (Actual_Pack);
9234 -- The actual may be a renamed package, or an outer generic formal
9235 -- package whose instantiation is converted into a renaming.
9237 if Present (Renamed_Object (Actual_Pack)) then
9238 Actual_Pack := Renamed_Object (Actual_Pack);
9241 if Nkind (Analyzed_Formal) = N_Formal_Package_Declaration then
9242 Gen_Parent := Get_Instance_Of (Entity (Name (Analyzed_Formal)));
9243 Formal_Pack := Defining_Identifier (Analyzed_Formal);
9246 Generic_Parent (Specification (Analyzed_Formal));
9248 Defining_Unit_Name (Specification (Analyzed_Formal));
9251 if Nkind (Parent (Actual_Pack)) = N_Defining_Program_Unit_Name then
9252 Parent_Spec := Package_Specification (Actual_Pack);
9254 Parent_Spec := Parent (Actual_Pack);
9257 if Gen_Parent = Any_Id then
9259 ("previous error in declaration of formal package", Actual);
9260 Abandon_Instantiation (Actual);
9263 Is_Instance_Of (Parent_Spec, Get_Instance_Of (Gen_Parent))
9269 ("actual parameter must be instance of&", Actual, Gen_Parent);
9270 Abandon_Instantiation (Actual);
9273 Set_Instance_Of (Defining_Identifier (Formal), Actual_Pack);
9274 Map_Formal_Package_Entities (Formal_Pack, Actual_Pack);
9277 Make_Package_Renaming_Declaration (Loc,
9278 Defining_Unit_Name => New_Copy (Defining_Identifier (Formal)),
9279 Name => New_Occurrence_Of (Actual_Pack, Loc));
9281 Set_Associated_Formal_Package (Defining_Unit_Name (Nod),
9282 Defining_Identifier (Formal));
9283 Decls := New_List (Nod);
9285 -- If the formal F has a box, then the generic declarations are
9286 -- visible in the generic G. In an instance of G, the corresponding
9287 -- entities in the actual for F (which are the actuals for the
9288 -- instantiation of the generic that F denotes) must also be made
9289 -- visible for analysis of the current instance. On exit from the
9290 -- current instance, those entities are made private again. If the
9291 -- actual is currently in use, these entities are also use-visible.
9293 -- The loop through the actual entities also steps through the formal
9294 -- entities and enters associations from formals to actuals into the
9295 -- renaming map. This is necessary to properly handle checking of
9296 -- actual parameter associations for later formals that depend on
9297 -- actuals declared in the formal package.
9299 -- In Ada 2005, partial parameterization requires that we make
9300 -- visible the actuals corresponding to formals that were defaulted
9301 -- in the formal package. There formals are identified because they
9302 -- remain formal generics within the formal package, rather than
9303 -- being renamings of the actuals supplied.
9306 Gen_Decl : constant Node_Id :=
9307 Unit_Declaration_Node (Gen_Parent);
9308 Formals : constant List_Id :=
9309 Generic_Formal_Declarations (Gen_Decl);
9311 Actual_Ent : Entity_Id;
9312 Actual_Of_Formal : Node_Id;
9313 Formal_Node : Node_Id;
9314 Formal_Ent : Entity_Id;
9317 if Present (Formals) then
9318 Formal_Node := First_Non_Pragma (Formals);
9320 Formal_Node := Empty;
9323 Actual_Ent := First_Entity (Actual_Pack);
9325 First (Visible_Declarations (Specification (Analyzed_Formal)));
9326 while Present (Actual_Ent)
9327 and then Actual_Ent /= First_Private_Entity (Actual_Pack)
9329 if Present (Formal_Node) then
9330 Formal_Ent := Get_Formal_Entity (Formal_Node);
9332 if Present (Formal_Ent) then
9333 Find_Matching_Actual (Formal_Node, Actual_Ent);
9335 (Formal_Node, Formal_Ent, Actual_Ent);
9337 -- We iterate at the same time over the actuals of the
9338 -- local package created for the formal, to determine
9339 -- which one of the formals of the original generic were
9340 -- defaulted in the formal. The corresponding actual
9341 -- entities are visible in the enclosing instance.
9343 if Box_Present (Formal)
9345 (Present (Actual_Of_Formal)
9348 (Get_Formal_Entity (Actual_Of_Formal)))
9350 Set_Is_Hidden (Actual_Ent, False);
9351 Set_Is_Visible_Formal (Actual_Ent);
9352 Set_Is_Potentially_Use_Visible
9353 (Actual_Ent, In_Use (Actual_Pack));
9355 if Ekind (Actual_Ent) = E_Package then
9356 Process_Nested_Formal (Actual_Ent);
9360 Set_Is_Hidden (Actual_Ent);
9361 Set_Is_Potentially_Use_Visible (Actual_Ent, False);
9365 Next_Non_Pragma (Formal_Node);
9366 Next (Actual_Of_Formal);
9369 -- No further formals to match, but the generic part may
9370 -- contain inherited operation that are not hidden in the
9371 -- enclosing instance.
9373 Next_Entity (Actual_Ent);
9377 -- Inherited subprograms generated by formal derived types are
9378 -- also visible if the types are.
9380 Actual_Ent := First_Entity (Actual_Pack);
9381 while Present (Actual_Ent)
9382 and then Actual_Ent /= First_Private_Entity (Actual_Pack)
9384 if Is_Overloadable (Actual_Ent)
9386 Nkind (Parent (Actual_Ent)) = N_Subtype_Declaration
9388 not Is_Hidden (Defining_Identifier (Parent (Actual_Ent)))
9390 Set_Is_Hidden (Actual_Ent, False);
9391 Set_Is_Potentially_Use_Visible
9392 (Actual_Ent, In_Use (Actual_Pack));
9395 Next_Entity (Actual_Ent);
9399 -- If the formal is not declared with a box, reanalyze it as an
9400 -- abbreviated instantiation, to verify the matching rules of 12.7.
9401 -- The actual checks are performed after the generic associations
9402 -- have been analyzed, to guarantee the same visibility for this
9403 -- instantiation and for the actuals.
9405 -- In Ada 2005, the generic associations for the formal can include
9406 -- defaulted parameters. These are ignored during check. This
9407 -- internal instantiation is removed from the tree after conformance
9408 -- checking, because it contains formal declarations for those
9409 -- defaulted parameters, and those should not reach the back-end.
9411 if not Box_Present (Formal) then
9413 I_Pack : constant Entity_Id :=
9414 Make_Temporary (Sloc (Actual), 'P');
9417 Set_Is_Internal (I_Pack);
9420 Make_Package_Instantiation (Sloc (Actual),
9421 Defining_Unit_Name => I_Pack,
9424 (Get_Instance_Of (Gen_Parent), Sloc (Actual)),
9425 Generic_Associations =>
9426 Generic_Associations (Formal)));
9432 end Instantiate_Formal_Package;
9434 -----------------------------------
9435 -- Instantiate_Formal_Subprogram --
9436 -----------------------------------
9438 function Instantiate_Formal_Subprogram
9441 Analyzed_Formal : Node_Id) return Node_Id
9444 Formal_Sub : constant Entity_Id :=
9445 Defining_Unit_Name (Specification (Formal));
9446 Analyzed_S : constant Entity_Id :=
9447 Defining_Unit_Name (Specification (Analyzed_Formal));
9448 Decl_Node : Node_Id;
9452 function From_Parent_Scope (Subp : Entity_Id) return Boolean;
9453 -- If the generic is a child unit, the parent has been installed on the
9454 -- scope stack, but a default subprogram cannot resolve to something
9455 -- on the parent because that parent is not really part of the visible
9456 -- context (it is there to resolve explicit local entities). If the
9457 -- default has resolved in this way, we remove the entity from immediate
9458 -- visibility and analyze the node again to emit an error message or
9459 -- find another visible candidate.
9461 procedure Valid_Actual_Subprogram (Act : Node_Id);
9462 -- Perform legality check and raise exception on failure
9464 -----------------------
9465 -- From_Parent_Scope --
9466 -----------------------
9468 function From_Parent_Scope (Subp : Entity_Id) return Boolean is
9469 Gen_Scope : Node_Id;
9472 Gen_Scope := Scope (Analyzed_S);
9473 while Present (Gen_Scope) and then Is_Child_Unit (Gen_Scope) loop
9474 if Scope (Subp) = Scope (Gen_Scope) then
9478 Gen_Scope := Scope (Gen_Scope);
9482 end From_Parent_Scope;
9484 -----------------------------
9485 -- Valid_Actual_Subprogram --
9486 -----------------------------
9488 procedure Valid_Actual_Subprogram (Act : Node_Id) is
9492 if Is_Entity_Name (Act) then
9493 Act_E := Entity (Act);
9495 elsif Nkind (Act) = N_Selected_Component
9496 and then Is_Entity_Name (Selector_Name (Act))
9498 Act_E := Entity (Selector_Name (Act));
9504 if (Present (Act_E) and then Is_Overloadable (Act_E))
9505 or else Nkind_In (Act, N_Attribute_Reference,
9506 N_Indexed_Component,
9507 N_Character_Literal,
9508 N_Explicit_Dereference)
9514 ("expect subprogram or entry name in instantiation of&",
9515 Instantiation_Node, Formal_Sub);
9516 Abandon_Instantiation (Instantiation_Node);
9518 end Valid_Actual_Subprogram;
9520 -- Start of processing for Instantiate_Formal_Subprogram
9523 New_Spec := New_Copy_Tree (Specification (Formal));
9525 -- The tree copy has created the proper instantiation sloc for the
9526 -- new specification. Use this location for all other constructed
9529 Loc := Sloc (Defining_Unit_Name (New_Spec));
9531 -- Create new entity for the actual (New_Copy_Tree does not)
9533 Set_Defining_Unit_Name
9534 (New_Spec, Make_Defining_Identifier (Loc, Chars (Formal_Sub)));
9536 -- Create new entities for the each of the formals in the
9537 -- specification of the renaming declaration built for the actual.
9539 if Present (Parameter_Specifications (New_Spec)) then
9543 F := First (Parameter_Specifications (New_Spec));
9544 while Present (F) loop
9545 Set_Defining_Identifier (F,
9546 Make_Defining_Identifier (Sloc (F),
9547 Chars => Chars (Defining_Identifier (F))));
9553 -- Find entity of actual. If the actual is an attribute reference, it
9554 -- cannot be resolved here (its formal is missing) but is handled
9555 -- instead in Attribute_Renaming. If the actual is overloaded, it is
9556 -- fully resolved subsequently, when the renaming declaration for the
9557 -- formal is analyzed. If it is an explicit dereference, resolve the
9558 -- prefix but not the actual itself, to prevent interpretation as call.
9560 if Present (Actual) then
9561 Loc := Sloc (Actual);
9562 Set_Sloc (New_Spec, Loc);
9564 if Nkind (Actual) = N_Operator_Symbol then
9565 Find_Direct_Name (Actual);
9567 elsif Nkind (Actual) = N_Explicit_Dereference then
9568 Analyze (Prefix (Actual));
9570 elsif Nkind (Actual) /= N_Attribute_Reference then
9574 Valid_Actual_Subprogram (Actual);
9577 elsif Present (Default_Name (Formal)) then
9578 if not Nkind_In (Default_Name (Formal), N_Attribute_Reference,
9579 N_Selected_Component,
9580 N_Indexed_Component,
9581 N_Character_Literal)
9582 and then Present (Entity (Default_Name (Formal)))
9584 Nam := New_Occurrence_Of (Entity (Default_Name (Formal)), Loc);
9586 Nam := New_Copy (Default_Name (Formal));
9587 Set_Sloc (Nam, Loc);
9590 elsif Box_Present (Formal) then
9592 -- Actual is resolved at the point of instantiation. Create an
9593 -- identifier or operator with the same name as the formal.
9595 if Nkind (Formal_Sub) = N_Defining_Operator_Symbol then
9596 Nam := Make_Operator_Symbol (Loc,
9597 Chars => Chars (Formal_Sub),
9598 Strval => No_String);
9600 Nam := Make_Identifier (Loc, Chars (Formal_Sub));
9603 elsif Nkind (Specification (Formal)) = N_Procedure_Specification
9604 and then Null_Present (Specification (Formal))
9606 -- Generate null body for procedure, for use in the instance
9609 Make_Subprogram_Body (Loc,
9610 Specification => New_Spec,
9611 Declarations => New_List,
9612 Handled_Statement_Sequence =>
9613 Make_Handled_Sequence_Of_Statements (Loc,
9614 Statements => New_List (Make_Null_Statement (Loc))));
9616 Set_Is_Intrinsic_Subprogram (Defining_Unit_Name (New_Spec));
9620 Error_Msg_Sloc := Sloc (Scope (Analyzed_S));
9622 ("missing actual&", Instantiation_Node, Formal_Sub);
9624 ("\in instantiation of & declared#",
9625 Instantiation_Node, Scope (Analyzed_S));
9626 Abandon_Instantiation (Instantiation_Node);
9630 Make_Subprogram_Renaming_Declaration (Loc,
9631 Specification => New_Spec,
9634 -- If we do not have an actual and the formal specified <> then set to
9635 -- get proper default.
9637 if No (Actual) and then Box_Present (Formal) then
9638 Set_From_Default (Decl_Node);
9641 -- Gather possible interpretations for the actual before analyzing the
9642 -- instance. If overloaded, it will be resolved when analyzing the
9643 -- renaming declaration.
9645 if Box_Present (Formal)
9646 and then No (Actual)
9650 if Is_Child_Unit (Scope (Analyzed_S))
9651 and then Present (Entity (Nam))
9653 if not Is_Overloaded (Nam) then
9654 if From_Parent_Scope (Entity (Nam)) then
9655 Set_Is_Immediately_Visible (Entity (Nam), False);
9656 Set_Entity (Nam, Empty);
9657 Set_Etype (Nam, Empty);
9660 Set_Is_Immediately_Visible (Entity (Nam));
9669 Get_First_Interp (Nam, I, It);
9670 while Present (It.Nam) loop
9671 if From_Parent_Scope (It.Nam) then
9675 Get_Next_Interp (I, It);
9682 -- The generic instantiation freezes the actual. This can only be done
9683 -- once the actual is resolved, in the analysis of the renaming
9684 -- declaration. To make the formal subprogram entity available, we set
9685 -- Corresponding_Formal_Spec to point to the formal subprogram entity.
9686 -- This is also needed in Analyze_Subprogram_Renaming for the processing
9687 -- of formal abstract subprograms.
9689 Set_Corresponding_Formal_Spec (Decl_Node, Analyzed_S);
9691 -- We cannot analyze the renaming declaration, and thus find the actual,
9692 -- until all the actuals are assembled in the instance. For subsequent
9693 -- checks of other actuals, indicate the node that will hold the
9694 -- instance of this formal.
9696 Set_Instance_Of (Analyzed_S, Nam);
9698 if Nkind (Actual) = N_Selected_Component
9699 and then Is_Task_Type (Etype (Prefix (Actual)))
9700 and then not Is_Frozen (Etype (Prefix (Actual)))
9702 -- The renaming declaration will create a body, which must appear
9703 -- outside of the instantiation, We move the renaming declaration
9704 -- out of the instance, and create an additional renaming inside,
9705 -- to prevent freezing anomalies.
9708 Anon_Id : constant Entity_Id := Make_Temporary (Loc, 'E');
9711 Set_Defining_Unit_Name (New_Spec, Anon_Id);
9712 Insert_Before (Instantiation_Node, Decl_Node);
9713 Analyze (Decl_Node);
9715 -- Now create renaming within the instance
9718 Make_Subprogram_Renaming_Declaration (Loc,
9719 Specification => New_Copy_Tree (New_Spec),
9720 Name => New_Occurrence_Of (Anon_Id, Loc));
9722 Set_Defining_Unit_Name (Specification (Decl_Node),
9723 Make_Defining_Identifier (Loc, Chars (Formal_Sub)));
9728 end Instantiate_Formal_Subprogram;
9730 ------------------------
9731 -- Instantiate_Object --
9732 ------------------------
9734 function Instantiate_Object
9737 Analyzed_Formal : Node_Id) return List_Id
9739 Gen_Obj : constant Entity_Id := Defining_Identifier (Formal);
9740 A_Gen_Obj : constant Entity_Id :=
9741 Defining_Identifier (Analyzed_Formal);
9742 Acc_Def : Node_Id := Empty;
9743 Act_Assoc : constant Node_Id := Parent (Actual);
9744 Actual_Decl : Node_Id := Empty;
9745 Decl_Node : Node_Id;
9748 List : constant List_Id := New_List;
9749 Loc : constant Source_Ptr := Sloc (Actual);
9750 Orig_Ftyp : constant Entity_Id := Etype (A_Gen_Obj);
9751 Subt_Decl : Node_Id := Empty;
9752 Subt_Mark : Node_Id := Empty;
9755 if Present (Subtype_Mark (Formal)) then
9756 Subt_Mark := Subtype_Mark (Formal);
9758 Check_Access_Definition (Formal);
9759 Acc_Def := Access_Definition (Formal);
9762 -- Sloc for error message on missing actual
9764 Error_Msg_Sloc := Sloc (Scope (A_Gen_Obj));
9766 if Get_Instance_Of (Gen_Obj) /= Gen_Obj then
9767 Error_Msg_N ("duplicate instantiation of generic parameter", Actual);
9770 Set_Parent (List, Parent (Actual));
9774 if Out_Present (Formal) then
9776 -- An IN OUT generic actual must be a name. The instantiation is a
9777 -- renaming declaration. The actual is the name being renamed. We
9778 -- use the actual directly, rather than a copy, because it is not
9779 -- used further in the list of actuals, and because a copy or a use
9780 -- of relocate_node is incorrect if the instance is nested within a
9781 -- generic. In order to simplify ASIS searches, the Generic_Parent
9782 -- field links the declaration to the generic association.
9787 Instantiation_Node, Gen_Obj);
9789 ("\in instantiation of & declared#",
9790 Instantiation_Node, Scope (A_Gen_Obj));
9791 Abandon_Instantiation (Instantiation_Node);
9794 if Present (Subt_Mark) then
9796 Make_Object_Renaming_Declaration (Loc,
9797 Defining_Identifier => New_Copy (Gen_Obj),
9798 Subtype_Mark => New_Copy_Tree (Subt_Mark),
9801 else pragma Assert (Present (Acc_Def));
9803 Make_Object_Renaming_Declaration (Loc,
9804 Defining_Identifier => New_Copy (Gen_Obj),
9805 Access_Definition => New_Copy_Tree (Acc_Def),
9809 Set_Corresponding_Generic_Association (Decl_Node, Act_Assoc);
9811 -- The analysis of the actual may produce Insert_Action nodes, so
9812 -- the declaration must have a context in which to attach them.
9814 Append (Decl_Node, List);
9817 -- Return if the analysis of the actual reported some error
9819 if Etype (Actual) = Any_Type then
9823 -- This check is performed here because Analyze_Object_Renaming will
9824 -- not check it when Comes_From_Source is False. Note though that the
9825 -- check for the actual being the name of an object will be performed
9826 -- in Analyze_Object_Renaming.
9828 if Is_Object_Reference (Actual)
9829 and then Is_Dependent_Component_Of_Mutable_Object (Actual)
9832 ("illegal discriminant-dependent component for in out parameter",
9836 -- The actual has to be resolved in order to check that it is a
9837 -- variable (due to cases such as F (1), where F returns access to
9838 -- an array, and for overloaded prefixes).
9840 Ftyp := Get_Instance_Of (Etype (A_Gen_Obj));
9842 -- If the type of the formal is not itself a formal, and the current
9843 -- unit is a child unit, the formal type must be declared in a
9844 -- parent, and must be retrieved by visibility.
9847 and then Is_Generic_Unit (Scope (Ftyp))
9848 and then Is_Child_Unit (Scope (A_Gen_Obj))
9851 Temp : constant Node_Id :=
9852 New_Copy_Tree (Subtype_Mark (Analyzed_Formal));
9854 Set_Entity (Temp, Empty);
9856 Ftyp := Entity (Temp);
9860 if Is_Private_Type (Ftyp)
9861 and then not Is_Private_Type (Etype (Actual))
9862 and then (Base_Type (Full_View (Ftyp)) = Base_Type (Etype (Actual))
9863 or else Base_Type (Etype (Actual)) = Ftyp)
9865 -- If the actual has the type of the full view of the formal, or
9866 -- else a non-private subtype of the formal, then the visibility
9867 -- of the formal type has changed. Add to the actuals a subtype
9868 -- declaration that will force the exchange of views in the body
9869 -- of the instance as well.
9872 Make_Subtype_Declaration (Loc,
9873 Defining_Identifier => Make_Temporary (Loc, 'P'),
9874 Subtype_Indication => New_Occurrence_Of (Ftyp, Loc));
9876 Prepend (Subt_Decl, List);
9878 Prepend_Elmt (Full_View (Ftyp), Exchanged_Views);
9879 Exchange_Declarations (Ftyp);
9882 Resolve (Actual, Ftyp);
9884 if not Denotes_Variable (Actual) then
9886 ("actual for& must be a variable", Actual, Gen_Obj);
9888 elsif Base_Type (Ftyp) /= Base_Type (Etype (Actual)) then
9890 -- Ada 2005 (AI-423): For a generic formal object of mode in out,
9891 -- the type of the actual shall resolve to a specific anonymous
9894 if Ada_Version < Ada_2005
9896 Ekind (Base_Type (Ftyp)) /=
9897 E_Anonymous_Access_Type
9899 Ekind (Base_Type (Etype (Actual))) /=
9900 E_Anonymous_Access_Type
9902 Error_Msg_NE ("type of actual does not match type of&",
9907 Note_Possible_Modification (Actual, Sure => True);
9909 -- Check for instantiation of atomic/volatile actual for
9910 -- non-atomic/volatile formal (RM C.6 (12)).
9912 if Is_Atomic_Object (Actual)
9913 and then not Is_Atomic (Orig_Ftyp)
9916 ("cannot instantiate non-atomic formal object " &
9917 "with atomic actual", Actual);
9919 elsif Is_Volatile_Object (Actual)
9920 and then not Is_Volatile (Orig_Ftyp)
9923 ("cannot instantiate non-volatile formal object " &
9924 "with volatile actual", Actual);
9927 -- Formal in-parameter
9930 -- The instantiation of a generic formal in-parameter is constant
9931 -- declaration. The actual is the expression for that declaration.
9933 if Present (Actual) then
9934 if Present (Subt_Mark) then
9936 else pragma Assert (Present (Acc_Def));
9941 Make_Object_Declaration (Loc,
9942 Defining_Identifier => New_Copy (Gen_Obj),
9943 Constant_Present => True,
9944 Null_Exclusion_Present => Null_Exclusion_Present (Formal),
9945 Object_Definition => New_Copy_Tree (Def),
9946 Expression => Actual);
9948 Set_Corresponding_Generic_Association (Decl_Node, Act_Assoc);
9950 -- A generic formal object of a tagged type is defined to be
9951 -- aliased so the new constant must also be treated as aliased.
9953 if Is_Tagged_Type (Etype (A_Gen_Obj)) then
9954 Set_Aliased_Present (Decl_Node);
9957 Append (Decl_Node, List);
9959 -- No need to repeat (pre-)analysis of some expression nodes
9960 -- already handled in Preanalyze_Actuals.
9962 if Nkind (Actual) /= N_Allocator then
9965 -- Return if the analysis of the actual reported some error
9967 if Etype (Actual) = Any_Type then
9973 Formal_Type : constant Entity_Id := Etype (A_Gen_Obj);
9977 Typ := Get_Instance_Of (Formal_Type);
9979 Freeze_Before (Instantiation_Node, Typ);
9981 -- If the actual is an aggregate, perform name resolution on
9982 -- its components (the analysis of an aggregate does not do it)
9983 -- to capture local names that may be hidden if the generic is
9986 if Nkind (Actual) = N_Aggregate then
9987 Preanalyze_And_Resolve (Actual, Typ);
9990 if Is_Limited_Type (Typ)
9991 and then not OK_For_Limited_Init (Typ, Actual)
9994 ("initialization not allowed for limited types", Actual);
9995 Explain_Limited_Type (Typ, Actual);
9999 elsif Present (Default_Expression (Formal)) then
10001 -- Use default to construct declaration
10003 if Present (Subt_Mark) then
10005 else pragma Assert (Present (Acc_Def));
10010 Make_Object_Declaration (Sloc (Formal),
10011 Defining_Identifier => New_Copy (Gen_Obj),
10012 Constant_Present => True,
10013 Null_Exclusion_Present => Null_Exclusion_Present (Formal),
10014 Object_Definition => New_Copy (Def),
10015 Expression => New_Copy_Tree
10016 (Default_Expression (Formal)));
10018 Append (Decl_Node, List);
10019 Set_Analyzed (Expression (Decl_Node), False);
10023 ("missing actual&",
10024 Instantiation_Node, Gen_Obj);
10025 Error_Msg_NE ("\in instantiation of & declared#",
10026 Instantiation_Node, Scope (A_Gen_Obj));
10028 if Is_Scalar_Type (Etype (A_Gen_Obj)) then
10030 -- Create dummy constant declaration so that instance can be
10031 -- analyzed, to minimize cascaded visibility errors.
10033 if Present (Subt_Mark) then
10035 else pragma Assert (Present (Acc_Def));
10040 Make_Object_Declaration (Loc,
10041 Defining_Identifier => New_Copy (Gen_Obj),
10042 Constant_Present => True,
10043 Null_Exclusion_Present => Null_Exclusion_Present (Formal),
10044 Object_Definition => New_Copy (Def),
10046 Make_Attribute_Reference (Sloc (Gen_Obj),
10047 Attribute_Name => Name_First,
10048 Prefix => New_Copy (Def)));
10050 Append (Decl_Node, List);
10053 Abandon_Instantiation (Instantiation_Node);
10058 if Nkind (Actual) in N_Has_Entity then
10059 Actual_Decl := Parent (Entity (Actual));
10062 -- Ada 2005 (AI-423): For a formal object declaration with a null
10063 -- exclusion or an access definition that has a null exclusion: If the
10064 -- actual matching the formal object declaration denotes a generic
10065 -- formal object of another generic unit G, and the instantiation
10066 -- containing the actual occurs within the body of G or within the body
10067 -- of a generic unit declared within the declarative region of G, then
10068 -- the declaration of the formal object of G must have a null exclusion.
10069 -- Otherwise, the subtype of the actual matching the formal object
10070 -- declaration shall exclude null.
10072 if Ada_Version >= Ada_2005
10073 and then Present (Actual_Decl)
10075 Nkind_In (Actual_Decl, N_Formal_Object_Declaration,
10076 N_Object_Declaration)
10077 and then Nkind (Analyzed_Formal) = N_Formal_Object_Declaration
10078 and then not Has_Null_Exclusion (Actual_Decl)
10079 and then Has_Null_Exclusion (Analyzed_Formal)
10081 Error_Msg_Sloc := Sloc (Analyzed_Formal);
10083 ("actual must exclude null to match generic formal#", Actual);
10086 -- An effectively volatile object cannot be used as an actual in
10087 -- a generic instance. The following check is only relevant when
10088 -- SPARK_Mode is on as it is not a standard Ada legality rule.
10091 and then Present (Actual)
10092 and then Is_Effectively_Volatile_Object (Actual)
10095 ("volatile object cannot act as actual in generic instantiation "
10096 & "(SPARK RM 7.1.3(8))", Actual);
10100 end Instantiate_Object;
10102 ------------------------------
10103 -- Instantiate_Package_Body --
10104 ------------------------------
10106 procedure Instantiate_Package_Body
10107 (Body_Info : Pending_Body_Info;
10108 Inlined_Body : Boolean := False;
10109 Body_Optional : Boolean := False)
10111 Act_Decl : constant Node_Id := Body_Info.Act_Decl;
10112 Inst_Node : constant Node_Id := Body_Info.Inst_Node;
10113 Loc : constant Source_Ptr := Sloc (Inst_Node);
10115 Gen_Id : constant Node_Id := Name (Inst_Node);
10116 Gen_Unit : constant Entity_Id := Get_Generic_Entity (Inst_Node);
10117 Gen_Decl : constant Node_Id := Unit_Declaration_Node (Gen_Unit);
10118 Act_Spec : constant Node_Id := Specification (Act_Decl);
10119 Act_Decl_Id : constant Entity_Id := Defining_Entity (Act_Spec);
10121 Act_Body_Name : Node_Id;
10122 Gen_Body : Node_Id;
10123 Gen_Body_Id : Node_Id;
10124 Act_Body : Node_Id;
10125 Act_Body_Id : Entity_Id;
10127 Parent_Installed : Boolean := False;
10128 Save_Style_Check : constant Boolean := Style_Check;
10130 Par_Ent : Entity_Id := Empty;
10131 Par_Vis : Boolean := False;
10133 Vis_Prims_List : Elist_Id := No_Elist;
10134 -- List of primitives made temporarily visible in the instantiation
10135 -- to match the visibility of the formal type
10137 procedure Check_Initialized_Types;
10138 -- In a generic package body, an entity of a generic private type may
10139 -- appear uninitialized. This is suspicious, unless the actual is a
10140 -- fully initialized type.
10142 -----------------------------
10143 -- Check_Initialized_Types --
10144 -----------------------------
10146 procedure Check_Initialized_Types is
10148 Formal : Entity_Id;
10149 Actual : Entity_Id;
10150 Uninit_Var : Entity_Id;
10153 Decl := First (Generic_Formal_Declarations (Gen_Decl));
10154 while Present (Decl) loop
10155 Uninit_Var := Empty;
10157 if Nkind (Decl) = N_Private_Extension_Declaration then
10158 Uninit_Var := Uninitialized_Variable (Decl);
10160 elsif Nkind (Decl) = N_Formal_Type_Declaration
10161 and then Nkind (Formal_Type_Definition (Decl)) =
10162 N_Formal_Private_Type_Definition
10165 Uninitialized_Variable (Formal_Type_Definition (Decl));
10168 if Present (Uninit_Var) then
10169 Formal := Defining_Identifier (Decl);
10170 Actual := First_Entity (Act_Decl_Id);
10172 -- For each formal there is a subtype declaration that renames
10173 -- the actual and has the same name as the formal. Locate the
10174 -- formal for warning message about uninitialized variables
10175 -- in the generic, for which the actual type should be a fully
10176 -- initialized type.
10178 while Present (Actual) loop
10179 exit when Ekind (Actual) = E_Package
10180 and then Present (Renamed_Object (Actual));
10182 if Chars (Actual) = Chars (Formal)
10183 and then not Is_Scalar_Type (Actual)
10184 and then not Is_Fully_Initialized_Type (Actual)
10185 and then Warn_On_No_Value_Assigned
10187 Error_Msg_Node_2 := Formal;
10189 ("generic unit has uninitialized variable& of "
10190 & "formal private type &?v?", Actual, Uninit_Var);
10192 ("actual type for& should be fully initialized type?v?",
10197 Next_Entity (Actual);
10203 end Check_Initialized_Types;
10205 -- Start of processing for Instantiate_Package_Body
10208 Gen_Body_Id := Corresponding_Body (Gen_Decl);
10210 -- The instance body may already have been processed, as the parent of
10211 -- another instance that is inlined (Load_Parent_Of_Generic).
10213 if Present (Corresponding_Body (Instance_Spec (Inst_Node))) then
10217 Expander_Mode_Save_And_Set (Body_Info.Expander_Status);
10219 -- Re-establish the state of information on which checks are suppressed.
10220 -- This information was set in Body_Info at the point of instantiation,
10221 -- and now we restore it so that the instance is compiled using the
10222 -- check status at the instantiation (RM 11.5 (7.2/2), AI95-00224-01).
10224 Local_Suppress_Stack_Top := Body_Info.Local_Suppress_Stack_Top;
10225 Scope_Suppress := Body_Info.Scope_Suppress;
10226 Opt.Ada_Version := Body_Info.Version;
10227 Opt.Ada_Version_Pragma := Body_Info.Version_Pragma;
10228 Restore_Warnings (Body_Info.Warnings);
10229 Opt.SPARK_Mode := Body_Info.SPARK_Mode;
10230 Opt.SPARK_Mode_Pragma := Body_Info.SPARK_Mode_Pragma;
10232 if No (Gen_Body_Id) then
10234 -- Do not look for parent of generic body if none is required.
10235 -- This may happen when the routine is called as part of the
10236 -- Pending_Instantiations processing, when nested instances
10237 -- may precede the one generated from the main unit.
10239 if not Unit_Requires_Body (Defining_Entity (Gen_Decl))
10240 and then Body_Optional
10244 Load_Parent_Of_Generic
10245 (Inst_Node, Specification (Gen_Decl), Body_Optional);
10246 Gen_Body_Id := Corresponding_Body (Gen_Decl);
10250 -- Establish global variable for sloc adjustment and for error recovery
10252 Instantiation_Node := Inst_Node;
10254 if Present (Gen_Body_Id) then
10255 Save_Env (Gen_Unit, Act_Decl_Id);
10256 Style_Check := False;
10257 Current_Sem_Unit := Body_Info.Current_Sem_Unit;
10259 Gen_Body := Unit_Declaration_Node (Gen_Body_Id);
10261 Create_Instantiation_Source
10262 (Inst_Node, Gen_Body_Id, False, S_Adjustment);
10266 (Original_Node (Gen_Body), Empty, Instantiating => True);
10268 -- Build new name (possibly qualified) for body declaration
10270 Act_Body_Id := New_Copy (Act_Decl_Id);
10272 -- Some attributes of spec entity are not inherited by body entity
10274 Set_Handler_Records (Act_Body_Id, No_List);
10276 if Nkind (Defining_Unit_Name (Act_Spec)) =
10277 N_Defining_Program_Unit_Name
10280 Make_Defining_Program_Unit_Name (Loc,
10281 Name => New_Copy_Tree (Name (Defining_Unit_Name (Act_Spec))),
10282 Defining_Identifier => Act_Body_Id);
10284 Act_Body_Name := Act_Body_Id;
10287 Set_Defining_Unit_Name (Act_Body, Act_Body_Name);
10289 Set_Corresponding_Spec (Act_Body, Act_Decl_Id);
10290 Check_Generic_Actuals (Act_Decl_Id, False);
10291 Check_Initialized_Types;
10293 -- Install primitives hidden at the point of the instantiation but
10294 -- visible when processing the generic formals
10300 E := First_Entity (Act_Decl_Id);
10301 while Present (E) loop
10303 and then Is_Generic_Actual_Type (E)
10304 and then Is_Tagged_Type (E)
10306 Install_Hidden_Primitives
10307 (Prims_List => Vis_Prims_List,
10308 Gen_T => Generic_Parent_Type (Parent (E)),
10316 -- If it is a child unit, make the parent instance (which is an
10317 -- instance of the parent of the generic) visible. The parent
10318 -- instance is the prefix of the name of the generic unit.
10320 if Ekind (Scope (Gen_Unit)) = E_Generic_Package
10321 and then Nkind (Gen_Id) = N_Expanded_Name
10323 Par_Ent := Entity (Prefix (Gen_Id));
10324 Par_Vis := Is_Immediately_Visible (Par_Ent);
10325 Install_Parent (Par_Ent, In_Body => True);
10326 Parent_Installed := True;
10328 elsif Is_Child_Unit (Gen_Unit) then
10329 Par_Ent := Scope (Gen_Unit);
10330 Par_Vis := Is_Immediately_Visible (Par_Ent);
10331 Install_Parent (Par_Ent, In_Body => True);
10332 Parent_Installed := True;
10335 -- If the instantiation is a library unit, and this is the main unit,
10336 -- then build the resulting compilation unit nodes for the instance.
10337 -- If this is a compilation unit but it is not the main unit, then it
10338 -- is the body of a unit in the context, that is being compiled
10339 -- because it is encloses some inlined unit or another generic unit
10340 -- being instantiated. In that case, this body is not part of the
10341 -- current compilation, and is not attached to the tree, but its
10342 -- parent must be set for analysis.
10344 if Nkind (Parent (Inst_Node)) = N_Compilation_Unit then
10346 -- Replace instance node with body of instance, and create new
10347 -- node for corresponding instance declaration.
10349 Build_Instance_Compilation_Unit_Nodes
10350 (Inst_Node, Act_Body, Act_Decl);
10351 Analyze (Inst_Node);
10353 if Parent (Inst_Node) = Cunit (Main_Unit) then
10355 -- If the instance is a child unit itself, then set the scope
10356 -- of the expanded body to be the parent of the instantiation
10357 -- (ensuring that the fully qualified name will be generated
10358 -- for the elaboration subprogram).
10360 if Nkind (Defining_Unit_Name (Act_Spec)) =
10361 N_Defining_Program_Unit_Name
10364 (Defining_Entity (Inst_Node), Scope (Act_Decl_Id));
10368 -- Case where instantiation is not a library unit
10371 -- If this is an early instantiation, i.e. appears textually
10372 -- before the corresponding body and must be elaborated first,
10373 -- indicate that the body instance is to be delayed.
10375 Install_Body (Act_Body, Inst_Node, Gen_Body, Gen_Decl);
10377 -- Now analyze the body. We turn off all checks if this is an
10378 -- internal unit, since there is no reason to have checks on for
10379 -- any predefined run-time library code. All such code is designed
10380 -- to be compiled with checks off.
10382 -- Note that we do NOT apply this criterion to children of GNAT
10383 -- (or on VMS, children of DEC). The latter units must suppress
10384 -- checks explicitly if this is needed.
10386 if Is_Predefined_File_Name
10387 (Unit_File_Name (Get_Source_Unit (Gen_Decl)))
10389 Analyze (Act_Body, Suppress => All_Checks);
10391 Analyze (Act_Body);
10395 Inherit_Context (Gen_Body, Inst_Node);
10397 -- Remove the parent instances if they have been placed on the scope
10398 -- stack to compile the body.
10400 if Parent_Installed then
10401 Remove_Parent (In_Body => True);
10403 -- Restore the previous visibility of the parent
10405 Set_Is_Immediately_Visible (Par_Ent, Par_Vis);
10408 Restore_Hidden_Primitives (Vis_Prims_List);
10409 Restore_Private_Views (Act_Decl_Id);
10411 -- Remove the current unit from visibility if this is an instance
10412 -- that is not elaborated on the fly for inlining purposes.
10414 if not Inlined_Body then
10415 Set_Is_Immediately_Visible (Act_Decl_Id, False);
10419 Style_Check := Save_Style_Check;
10421 -- If we have no body, and the unit requires a body, then complain. This
10422 -- complaint is suppressed if we have detected other errors (since a
10423 -- common reason for missing the body is that it had errors).
10424 -- In CodePeer mode, a warning has been emitted already, no need for
10425 -- further messages.
10427 elsif Unit_Requires_Body (Gen_Unit)
10428 and then not Body_Optional
10430 if CodePeer_Mode then
10433 elsif Serious_Errors_Detected = 0 then
10435 ("cannot find body of generic package &", Inst_Node, Gen_Unit);
10437 -- Don't attempt to perform any cleanup actions if some other error
10438 -- was already detected, since this can cause blowups.
10444 -- Case of package that does not need a body
10447 -- If the instantiation of the declaration is a library unit, rewrite
10448 -- the original package instantiation as a package declaration in the
10449 -- compilation unit node.
10451 if Nkind (Parent (Inst_Node)) = N_Compilation_Unit then
10452 Set_Parent_Spec (Act_Decl, Parent_Spec (Inst_Node));
10453 Rewrite (Inst_Node, Act_Decl);
10455 -- Generate elaboration entity, in case spec has elaboration code.
10456 -- This cannot be done when the instance is analyzed, because it
10457 -- is not known yet whether the body exists.
10459 Set_Elaboration_Entity_Required (Act_Decl_Id, False);
10460 Build_Elaboration_Entity (Parent (Inst_Node), Act_Decl_Id);
10462 -- If the instantiation is not a library unit, then append the
10463 -- declaration to the list of implicitly generated entities, unless
10464 -- it is already a list member which means that it was already
10467 elsif not Is_List_Member (Act_Decl) then
10468 Mark_Rewrite_Insertion (Act_Decl);
10469 Insert_Before (Inst_Node, Act_Decl);
10473 Expander_Mode_Restore;
10474 end Instantiate_Package_Body;
10476 ---------------------------------
10477 -- Instantiate_Subprogram_Body --
10478 ---------------------------------
10480 procedure Instantiate_Subprogram_Body
10481 (Body_Info : Pending_Body_Info;
10482 Body_Optional : Boolean := False)
10484 Act_Decl : constant Node_Id := Body_Info.Act_Decl;
10485 Inst_Node : constant Node_Id := Body_Info.Inst_Node;
10486 Loc : constant Source_Ptr := Sloc (Inst_Node);
10487 Gen_Id : constant Node_Id := Name (Inst_Node);
10488 Gen_Unit : constant Entity_Id := Get_Generic_Entity (Inst_Node);
10489 Gen_Decl : constant Node_Id := Unit_Declaration_Node (Gen_Unit);
10490 Anon_Id : constant Entity_Id :=
10491 Defining_Unit_Name (Specification (Act_Decl));
10492 Pack_Id : constant Entity_Id :=
10493 Defining_Unit_Name (Parent (Act_Decl));
10495 Gen_Body : Node_Id;
10496 Gen_Body_Id : Node_Id;
10497 Act_Body : Node_Id;
10498 Pack_Body : Node_Id;
10499 Prev_Formal : Entity_Id;
10500 Ret_Expr : Node_Id;
10501 Unit_Renaming : Node_Id;
10503 Parent_Installed : Boolean := False;
10505 Saved_Style_Check : constant Boolean := Style_Check;
10506 Saved_Warnings : constant Warning_Record := Save_Warnings;
10508 Par_Ent : Entity_Id := Empty;
10509 Par_Vis : Boolean := False;
10512 Gen_Body_Id := Corresponding_Body (Gen_Decl);
10514 -- Subprogram body may have been created already because of an inline
10515 -- pragma, or because of multiple elaborations of the enclosing package
10516 -- when several instances of the subprogram appear in the main unit.
10518 if Present (Corresponding_Body (Act_Decl)) then
10522 Expander_Mode_Save_And_Set (Body_Info.Expander_Status);
10524 -- Re-establish the state of information on which checks are suppressed.
10525 -- This information was set in Body_Info at the point of instantiation,
10526 -- and now we restore it so that the instance is compiled using the
10527 -- check status at the instantiation (RM 11.5 (7.2/2), AI95-00224-01).
10529 Local_Suppress_Stack_Top := Body_Info.Local_Suppress_Stack_Top;
10530 Scope_Suppress := Body_Info.Scope_Suppress;
10531 Opt.Ada_Version := Body_Info.Version;
10532 Opt.Ada_Version_Pragma := Body_Info.Version_Pragma;
10533 Restore_Warnings (Body_Info.Warnings);
10534 Opt.SPARK_Mode := Body_Info.SPARK_Mode;
10535 Opt.SPARK_Mode_Pragma := Body_Info.SPARK_Mode_Pragma;
10537 if No (Gen_Body_Id) then
10539 -- For imported generic subprogram, no body to compile, complete
10540 -- the spec entity appropriately.
10542 if Is_Imported (Gen_Unit) then
10543 Set_Is_Imported (Anon_Id);
10544 Set_First_Rep_Item (Anon_Id, First_Rep_Item (Gen_Unit));
10545 Set_Interface_Name (Anon_Id, Interface_Name (Gen_Unit));
10546 Set_Convention (Anon_Id, Convention (Gen_Unit));
10547 Set_Has_Completion (Anon_Id);
10550 -- For other cases, compile the body
10553 Load_Parent_Of_Generic
10554 (Inst_Node, Specification (Gen_Decl), Body_Optional);
10555 Gen_Body_Id := Corresponding_Body (Gen_Decl);
10559 Instantiation_Node := Inst_Node;
10561 if Present (Gen_Body_Id) then
10562 Gen_Body := Unit_Declaration_Node (Gen_Body_Id);
10564 if Nkind (Gen_Body) = N_Subprogram_Body_Stub then
10566 -- Either body is not present, or context is non-expanding, as
10567 -- when compiling a subunit. Mark the instance as completed, and
10568 -- diagnose a missing body when needed.
10571 and then Operating_Mode = Generate_Code
10574 ("missing proper body for instantiation", Gen_Body);
10577 Set_Has_Completion (Anon_Id);
10581 Save_Env (Gen_Unit, Anon_Id);
10582 Style_Check := False;
10583 Current_Sem_Unit := Body_Info.Current_Sem_Unit;
10584 Create_Instantiation_Source
10592 (Original_Node (Gen_Body), Empty, Instantiating => True);
10594 -- Create proper defining name for the body, to correspond to
10595 -- the one in the spec.
10597 Set_Defining_Unit_Name (Specification (Act_Body),
10598 Make_Defining_Identifier
10599 (Sloc (Defining_Entity (Inst_Node)), Chars (Anon_Id)));
10600 Set_Corresponding_Spec (Act_Body, Anon_Id);
10601 Set_Has_Completion (Anon_Id);
10602 Check_Generic_Actuals (Pack_Id, False);
10604 -- Generate a reference to link the visible subprogram instance to
10605 -- the generic body, which for navigation purposes is the only
10606 -- available source for the instance.
10609 (Related_Instance (Pack_Id),
10610 Gen_Body_Id, 'b', Set_Ref => False, Force => True);
10612 -- If it is a child unit, make the parent instance (which is an
10613 -- instance of the parent of the generic) visible. The parent
10614 -- instance is the prefix of the name of the generic unit.
10616 if Ekind (Scope (Gen_Unit)) = E_Generic_Package
10617 and then Nkind (Gen_Id) = N_Expanded_Name
10619 Par_Ent := Entity (Prefix (Gen_Id));
10620 Par_Vis := Is_Immediately_Visible (Par_Ent);
10621 Install_Parent (Par_Ent, In_Body => True);
10622 Parent_Installed := True;
10624 elsif Is_Child_Unit (Gen_Unit) then
10625 Par_Ent := Scope (Gen_Unit);
10626 Par_Vis := Is_Immediately_Visible (Par_Ent);
10627 Install_Parent (Par_Ent, In_Body => True);
10628 Parent_Installed := True;
10631 -- Inside its body, a reference to the generic unit is a reference
10632 -- to the instance. The corresponding renaming is the first
10633 -- declaration in the body.
10636 Make_Subprogram_Renaming_Declaration (Loc,
10638 Copy_Generic_Node (
10639 Specification (Original_Node (Gen_Body)),
10641 Instantiating => True),
10642 Name => New_Occurrence_Of (Anon_Id, Loc));
10644 -- If there is a formal subprogram with the same name as the unit
10645 -- itself, do not add this renaming declaration. This is a temporary
10646 -- fix for one ACVC test. ???
10648 Prev_Formal := First_Entity (Pack_Id);
10649 while Present (Prev_Formal) loop
10650 if Chars (Prev_Formal) = Chars (Gen_Unit)
10651 and then Is_Overloadable (Prev_Formal)
10656 Next_Entity (Prev_Formal);
10659 if Present (Prev_Formal) then
10660 Decls := New_List (Act_Body);
10662 Decls := New_List (Unit_Renaming, Act_Body);
10665 -- The subprogram body is placed in the body of a dummy package body,
10666 -- whose spec contains the subprogram declaration as well as the
10667 -- renaming declarations for the generic parameters.
10669 Pack_Body := Make_Package_Body (Loc,
10670 Defining_Unit_Name => New_Copy (Pack_Id),
10671 Declarations => Decls);
10673 Set_Corresponding_Spec (Pack_Body, Pack_Id);
10675 -- If the instantiation is a library unit, then build resulting
10676 -- compilation unit nodes for the instance. The declaration of
10677 -- the enclosing package is the grandparent of the subprogram
10678 -- declaration. First replace the instantiation node as the unit
10679 -- of the corresponding compilation.
10681 if Nkind (Parent (Inst_Node)) = N_Compilation_Unit then
10682 if Parent (Inst_Node) = Cunit (Main_Unit) then
10683 Set_Unit (Parent (Inst_Node), Inst_Node);
10684 Build_Instance_Compilation_Unit_Nodes
10685 (Inst_Node, Pack_Body, Parent (Parent (Act_Decl)));
10686 Analyze (Inst_Node);
10688 Set_Parent (Pack_Body, Parent (Inst_Node));
10689 Analyze (Pack_Body);
10693 Insert_Before (Inst_Node, Pack_Body);
10694 Mark_Rewrite_Insertion (Pack_Body);
10695 Analyze (Pack_Body);
10697 if Expander_Active then
10698 Freeze_Subprogram_Body (Inst_Node, Gen_Body, Pack_Id);
10702 Inherit_Context (Gen_Body, Inst_Node);
10704 Restore_Private_Views (Pack_Id, False);
10706 if Parent_Installed then
10707 Remove_Parent (In_Body => True);
10709 -- Restore the previous visibility of the parent
10711 Set_Is_Immediately_Visible (Par_Ent, Par_Vis);
10715 Style_Check := Saved_Style_Check;
10716 Restore_Warnings (Saved_Warnings);
10718 -- Body not found. Error was emitted already. If there were no previous
10719 -- errors, this may be an instance whose scope is a premature instance.
10720 -- In that case we must insure that the (legal) program does raise
10721 -- program error if executed. We generate a subprogram body for this
10722 -- purpose. See DEC ac30vso.
10724 -- Should not reference proprietary DEC tests in comments ???
10726 elsif Serious_Errors_Detected = 0
10727 and then Nkind (Parent (Inst_Node)) /= N_Compilation_Unit
10729 if Body_Optional then
10732 elsif Ekind (Anon_Id) = E_Procedure then
10734 Make_Subprogram_Body (Loc,
10736 Make_Procedure_Specification (Loc,
10737 Defining_Unit_Name =>
10738 Make_Defining_Identifier (Loc, Chars (Anon_Id)),
10739 Parameter_Specifications =>
10741 (Parameter_Specifications (Parent (Anon_Id)))),
10743 Declarations => Empty_List,
10744 Handled_Statement_Sequence =>
10745 Make_Handled_Sequence_Of_Statements (Loc,
10748 Make_Raise_Program_Error (Loc,
10750 PE_Access_Before_Elaboration))));
10754 Make_Raise_Program_Error (Loc,
10755 Reason => PE_Access_Before_Elaboration);
10757 Set_Etype (Ret_Expr, (Etype (Anon_Id)));
10758 Set_Analyzed (Ret_Expr);
10761 Make_Subprogram_Body (Loc,
10763 Make_Function_Specification (Loc,
10764 Defining_Unit_Name =>
10765 Make_Defining_Identifier (Loc, Chars (Anon_Id)),
10766 Parameter_Specifications =>
10768 (Parameter_Specifications (Parent (Anon_Id))),
10769 Result_Definition =>
10770 New_Occurrence_Of (Etype (Anon_Id), Loc)),
10772 Declarations => Empty_List,
10773 Handled_Statement_Sequence =>
10774 Make_Handled_Sequence_Of_Statements (Loc,
10777 (Make_Simple_Return_Statement (Loc, Ret_Expr))));
10780 Pack_Body := Make_Package_Body (Loc,
10781 Defining_Unit_Name => New_Copy (Pack_Id),
10782 Declarations => New_List (Act_Body));
10784 Insert_After (Inst_Node, Pack_Body);
10785 Set_Corresponding_Spec (Pack_Body, Pack_Id);
10786 Analyze (Pack_Body);
10789 Expander_Mode_Restore;
10790 end Instantiate_Subprogram_Body;
10792 ----------------------
10793 -- Instantiate_Type --
10794 ----------------------
10796 function Instantiate_Type
10799 Analyzed_Formal : Node_Id;
10800 Actual_Decls : List_Id) return List_Id
10802 Gen_T : constant Entity_Id := Defining_Identifier (Formal);
10803 A_Gen_T : constant Entity_Id :=
10804 Defining_Identifier (Analyzed_Formal);
10805 Ancestor : Entity_Id := Empty;
10806 Def : constant Node_Id := Formal_Type_Definition (Formal);
10808 Decl_Node : Node_Id;
10809 Decl_Nodes : List_Id;
10813 procedure Diagnose_Predicated_Actual;
10814 -- There are a number of constructs in which a discrete type with
10815 -- predicates is illegal, e.g. as an index in an array type declaration.
10816 -- If a generic type is used is such a construct in a generic package
10817 -- declaration, it carries the flag No_Predicate_On_Actual. it is part
10818 -- of the generic contract that the actual cannot have predicates.
10820 procedure Validate_Array_Type_Instance;
10821 procedure Validate_Access_Subprogram_Instance;
10822 procedure Validate_Access_Type_Instance;
10823 procedure Validate_Derived_Type_Instance;
10824 procedure Validate_Derived_Interface_Type_Instance;
10825 procedure Validate_Discriminated_Formal_Type;
10826 procedure Validate_Interface_Type_Instance;
10827 procedure Validate_Private_Type_Instance;
10828 procedure Validate_Incomplete_Type_Instance;
10829 -- These procedures perform validation tests for the named case.
10830 -- Validate_Discriminated_Formal_Type is shared by formal private
10831 -- types and Ada 2012 formal incomplete types.
10833 function Subtypes_Match (Gen_T, Act_T : Entity_Id) return Boolean;
10834 -- Check that base types are the same and that the subtypes match
10835 -- statically. Used in several of the above.
10837 ---------------------------------
10838 -- Diagnose_Predicated_Actual --
10839 ---------------------------------
10841 procedure Diagnose_Predicated_Actual is
10843 if No_Predicate_On_Actual (A_Gen_T)
10844 and then Has_Predicates (Act_T)
10847 ("actual for& cannot be a type with predicate",
10848 Instantiation_Node, A_Gen_T);
10850 elsif No_Dynamic_Predicate_On_Actual (A_Gen_T)
10851 and then Has_Predicates (Act_T)
10852 and then not Has_Static_Predicate_Aspect (Act_T)
10855 ("actual for& cannot be a type with a dynamic predicate",
10856 Instantiation_Node, A_Gen_T);
10858 end Diagnose_Predicated_Actual;
10860 --------------------
10861 -- Subtypes_Match --
10862 --------------------
10864 function Subtypes_Match (Gen_T, Act_T : Entity_Id) return Boolean is
10865 T : constant Entity_Id := Get_Instance_Of (Gen_T);
10868 -- Some detailed comments would be useful here ???
10870 return ((Base_Type (T) = Act_T
10871 or else Base_Type (T) = Base_Type (Act_T))
10872 and then Subtypes_Statically_Match (T, Act_T))
10874 or else (Is_Class_Wide_Type (Gen_T)
10875 and then Is_Class_Wide_Type (Act_T)
10876 and then Subtypes_Match
10877 (Get_Instance_Of (Root_Type (Gen_T)),
10878 Root_Type (Act_T)))
10881 (Ekind_In (Gen_T, E_Anonymous_Access_Subprogram_Type,
10882 E_Anonymous_Access_Type)
10883 and then Ekind (Act_T) = Ekind (Gen_T)
10884 and then Subtypes_Statically_Match
10885 (Designated_Type (Gen_T), Designated_Type (Act_T)));
10886 end Subtypes_Match;
10888 -----------------------------------------
10889 -- Validate_Access_Subprogram_Instance --
10890 -----------------------------------------
10892 procedure Validate_Access_Subprogram_Instance is
10894 if not Is_Access_Type (Act_T)
10895 or else Ekind (Designated_Type (Act_T)) /= E_Subprogram_Type
10898 ("expect access type in instantiation of &", Actual, Gen_T);
10899 Abandon_Instantiation (Actual);
10902 -- According to AI05-288, actuals for access_to_subprograms must be
10903 -- subtype conformant with the generic formal. Previous to AI05-288
10904 -- only mode conformance was required.
10906 -- This is a binding interpretation that applies to previous versions
10907 -- of the language, no need to maintain previous weaker checks.
10909 Check_Subtype_Conformant
10910 (Designated_Type (Act_T),
10911 Designated_Type (A_Gen_T),
10915 if Ekind (Base_Type (Act_T)) = E_Access_Protected_Subprogram_Type then
10916 if Ekind (A_Gen_T) = E_Access_Subprogram_Type then
10918 ("protected access type not allowed for formal &",
10922 elsif Ekind (A_Gen_T) = E_Access_Protected_Subprogram_Type then
10924 ("expect protected access type for formal &",
10927 end Validate_Access_Subprogram_Instance;
10929 -----------------------------------
10930 -- Validate_Access_Type_Instance --
10931 -----------------------------------
10933 procedure Validate_Access_Type_Instance is
10934 Desig_Type : constant Entity_Id :=
10935 Find_Actual_Type (Designated_Type (A_Gen_T), A_Gen_T);
10936 Desig_Act : Entity_Id;
10939 if not Is_Access_Type (Act_T) then
10941 ("expect access type in instantiation of &", Actual, Gen_T);
10942 Abandon_Instantiation (Actual);
10945 if Is_Access_Constant (A_Gen_T) then
10946 if not Is_Access_Constant (Act_T) then
10948 ("actual type must be access-to-constant type", Actual);
10949 Abandon_Instantiation (Actual);
10952 if Is_Access_Constant (Act_T) then
10954 ("actual type must be access-to-variable type", Actual);
10955 Abandon_Instantiation (Actual);
10957 elsif Ekind (A_Gen_T) = E_General_Access_Type
10958 and then Ekind (Base_Type (Act_T)) /= E_General_Access_Type
10960 Error_Msg_N -- CODEFIX
10961 ("actual must be general access type!", Actual);
10962 Error_Msg_NE -- CODEFIX
10963 ("add ALL to }!", Actual, Act_T);
10964 Abandon_Instantiation (Actual);
10968 -- The designated subtypes, that is to say the subtypes introduced
10969 -- by an access type declaration (and not by a subtype declaration)
10972 Desig_Act := Designated_Type (Base_Type (Act_T));
10974 -- The designated type may have been introduced through a limited_
10975 -- with clause, in which case retrieve the non-limited view. This
10976 -- applies to incomplete types as well as to class-wide types.
10978 if From_Limited_With (Desig_Act) then
10979 Desig_Act := Available_View (Desig_Act);
10982 if not Subtypes_Match (Desig_Type, Desig_Act) then
10984 ("designated type of actual does not match that of formal &",
10987 if not Predicates_Match (Desig_Type, Desig_Act) then
10988 Error_Msg_N ("\predicates do not match", Actual);
10991 Abandon_Instantiation (Actual);
10993 elsif Is_Access_Type (Designated_Type (Act_T))
10994 and then Is_Constrained (Designated_Type (Designated_Type (Act_T)))
10996 Is_Constrained (Designated_Type (Desig_Type))
10999 ("designated type of actual does not match that of formal &",
11002 if not Predicates_Match (Desig_Type, Desig_Act) then
11003 Error_Msg_N ("\predicates do not match", Actual);
11006 Abandon_Instantiation (Actual);
11009 -- Ada 2005: null-exclusion indicators of the two types must agree
11011 if Can_Never_Be_Null (A_Gen_T) /= Can_Never_Be_Null (Act_T) then
11013 ("non null exclusion of actual and formal & do not match",
11016 end Validate_Access_Type_Instance;
11018 ----------------------------------
11019 -- Validate_Array_Type_Instance --
11020 ----------------------------------
11022 procedure Validate_Array_Type_Instance is
11027 function Formal_Dimensions return Int;
11028 -- Count number of dimensions in array type formal
11030 -----------------------
11031 -- Formal_Dimensions --
11032 -----------------------
11034 function Formal_Dimensions return Int is
11039 if Nkind (Def) = N_Constrained_Array_Definition then
11040 Index := First (Discrete_Subtype_Definitions (Def));
11042 Index := First (Subtype_Marks (Def));
11045 while Present (Index) loop
11047 Next_Index (Index);
11051 end Formal_Dimensions;
11053 -- Start of processing for Validate_Array_Type_Instance
11056 if not Is_Array_Type (Act_T) then
11058 ("expect array type in instantiation of &", Actual, Gen_T);
11059 Abandon_Instantiation (Actual);
11061 elsif Nkind (Def) = N_Constrained_Array_Definition then
11062 if not (Is_Constrained (Act_T)) then
11064 ("expect constrained array in instantiation of &",
11066 Abandon_Instantiation (Actual);
11070 if Is_Constrained (Act_T) then
11072 ("expect unconstrained array in instantiation of &",
11074 Abandon_Instantiation (Actual);
11078 if Formal_Dimensions /= Number_Dimensions (Act_T) then
11080 ("dimensions of actual do not match formal &", Actual, Gen_T);
11081 Abandon_Instantiation (Actual);
11084 I1 := First_Index (A_Gen_T);
11085 I2 := First_Index (Act_T);
11086 for J in 1 .. Formal_Dimensions loop
11088 -- If the indexes of the actual were given by a subtype_mark,
11089 -- the index was transformed into a range attribute. Retrieve
11090 -- the original type mark for checking.
11092 if Is_Entity_Name (Original_Node (I2)) then
11093 T2 := Entity (Original_Node (I2));
11098 if not Subtypes_Match
11099 (Find_Actual_Type (Etype (I1), A_Gen_T), T2)
11102 ("index types of actual do not match those of formal &",
11104 Abandon_Instantiation (Actual);
11111 -- Check matching subtypes. Note that there are complex visibility
11112 -- issues when the generic is a child unit and some aspect of the
11113 -- generic type is declared in a parent unit of the generic. We do
11114 -- the test to handle this special case only after a direct check
11115 -- for static matching has failed. The case where both the component
11116 -- type and the array type are separate formals, and the component
11117 -- type is a private view may also require special checking in
11121 (Component_Type (A_Gen_T), Component_Type (Act_T))
11122 or else Subtypes_Match
11123 (Find_Actual_Type (Component_Type (A_Gen_T), A_Gen_T),
11124 Component_Type (Act_T))
11129 ("component subtype of actual does not match that of formal &",
11131 Abandon_Instantiation (Actual);
11134 if Has_Aliased_Components (A_Gen_T)
11135 and then not Has_Aliased_Components (Act_T)
11138 ("actual must have aliased components to match formal type &",
11141 end Validate_Array_Type_Instance;
11143 -----------------------------------------------
11144 -- Validate_Derived_Interface_Type_Instance --
11145 -----------------------------------------------
11147 procedure Validate_Derived_Interface_Type_Instance is
11148 Par : constant Entity_Id := Entity (Subtype_Indication (Def));
11152 -- First apply interface instance checks
11154 Validate_Interface_Type_Instance;
11156 -- Verify that immediate parent interface is an ancestor of
11160 and then not Interface_Present_In_Ancestor (Act_T, Par)
11163 ("interface actual must include progenitor&", Actual, Par);
11166 -- Now verify that the actual includes all other ancestors of
11169 Elmt := First_Elmt (Interfaces (A_Gen_T));
11170 while Present (Elmt) loop
11171 if not Interface_Present_In_Ancestor
11172 (Act_T, Get_Instance_Of (Node (Elmt)))
11175 ("interface actual must include progenitor&",
11176 Actual, Node (Elmt));
11181 end Validate_Derived_Interface_Type_Instance;
11183 ------------------------------------
11184 -- Validate_Derived_Type_Instance --
11185 ------------------------------------
11187 procedure Validate_Derived_Type_Instance is
11188 Actual_Discr : Entity_Id;
11189 Ancestor_Discr : Entity_Id;
11192 -- If the parent type in the generic declaration is itself a previous
11193 -- formal type, then it is local to the generic and absent from the
11194 -- analyzed generic definition. In that case the ancestor is the
11195 -- instance of the formal (which must have been instantiated
11196 -- previously), unless the ancestor is itself a formal derived type.
11197 -- In this latter case (which is the subject of Corrigendum 8652/0038
11198 -- (AI-202) the ancestor of the formals is the ancestor of its
11199 -- parent. Otherwise, the analyzed generic carries the parent type.
11200 -- If the parent type is defined in a previous formal package, then
11201 -- the scope of that formal package is that of the generic type
11202 -- itself, and it has already been mapped into the corresponding type
11203 -- in the actual package.
11205 -- Common case: parent type defined outside of the generic
11207 if Is_Entity_Name (Subtype_Mark (Def))
11208 and then Present (Entity (Subtype_Mark (Def)))
11210 Ancestor := Get_Instance_Of (Entity (Subtype_Mark (Def)));
11212 -- Check whether parent is defined in a previous formal package
11215 Scope (Scope (Base_Type (Etype (A_Gen_T)))) = Scope (A_Gen_T)
11218 Get_Instance_Of (Base_Type (Etype (A_Gen_T)));
11220 -- The type may be a local derivation, or a type extension of a
11221 -- previous formal, or of a formal of a parent package.
11223 elsif Is_Derived_Type (Get_Instance_Of (A_Gen_T))
11225 Ekind (Get_Instance_Of (A_Gen_T)) = E_Record_Type_With_Private
11227 -- Check whether the parent is another derived formal type in the
11228 -- same generic unit.
11230 if Etype (A_Gen_T) /= A_Gen_T
11231 and then Is_Generic_Type (Etype (A_Gen_T))
11232 and then Scope (Etype (A_Gen_T)) = Scope (A_Gen_T)
11233 and then Etype (Etype (A_Gen_T)) /= Etype (A_Gen_T)
11235 -- Locate ancestor of parent from the subtype declaration
11236 -- created for the actual.
11242 Decl := First (Actual_Decls);
11243 while Present (Decl) loop
11244 if Nkind (Decl) = N_Subtype_Declaration
11245 and then Chars (Defining_Identifier (Decl)) =
11246 Chars (Etype (A_Gen_T))
11248 Ancestor := Generic_Parent_Type (Decl);
11256 pragma Assert (Present (Ancestor));
11258 -- The ancestor itself may be a previous formal that has been
11261 Ancestor := Get_Instance_Of (Ancestor);
11265 Get_Instance_Of (Base_Type (Get_Instance_Of (A_Gen_T)));
11268 -- An unusual case: the actual is a type declared in a parent unit,
11269 -- but is not a formal type so there is no instance_of for it.
11270 -- Retrieve it by analyzing the record extension.
11272 elsif Is_Child_Unit (Scope (A_Gen_T))
11273 and then In_Open_Scopes (Scope (Act_T))
11274 and then Is_Generic_Instance (Scope (Act_T))
11276 Analyze (Subtype_Mark (Def));
11277 Ancestor := Entity (Subtype_Mark (Def));
11280 Ancestor := Get_Instance_Of (Etype (Base_Type (A_Gen_T)));
11283 -- If the formal derived type has pragma Preelaborable_Initialization
11284 -- then the actual type must have preelaborable initialization.
11286 if Known_To_Have_Preelab_Init (A_Gen_T)
11287 and then not Has_Preelaborable_Initialization (Act_T)
11290 ("actual for & must have preelaborable initialization",
11294 -- Ada 2005 (AI-251)
11296 if Ada_Version >= Ada_2005 and then Is_Interface (Ancestor) then
11297 if not Interface_Present_In_Ancestor (Act_T, Ancestor) then
11299 ("(Ada 2005) expected type implementing & in instantiation",
11303 elsif not Is_Ancestor (Base_Type (Ancestor), Act_T) then
11305 ("expect type derived from & in instantiation",
11306 Actual, First_Subtype (Ancestor));
11307 Abandon_Instantiation (Actual);
11310 -- Ada 2005 (AI-443): Synchronized formal derived type checks. Note
11311 -- that the formal type declaration has been rewritten as a private
11314 if Ada_Version >= Ada_2005
11315 and then Nkind (Parent (A_Gen_T)) = N_Private_Extension_Declaration
11316 and then Synchronized_Present (Parent (A_Gen_T))
11318 -- The actual must be a synchronized tagged type
11320 if not Is_Tagged_Type (Act_T) then
11322 ("actual of synchronized type must be tagged", Actual);
11323 Abandon_Instantiation (Actual);
11325 elsif Nkind (Parent (Act_T)) = N_Full_Type_Declaration
11326 and then Nkind (Type_Definition (Parent (Act_T))) =
11327 N_Derived_Type_Definition
11328 and then not Synchronized_Present (Type_Definition
11332 ("actual of synchronized type must be synchronized", Actual);
11333 Abandon_Instantiation (Actual);
11337 -- Perform atomic/volatile checks (RM C.6(12)). Note that AI05-0218-1
11338 -- removes the second instance of the phrase "or allow pass by copy".
11340 if Is_Atomic (Act_T) and then not Is_Atomic (Ancestor) then
11342 ("cannot have atomic actual type for non-atomic formal type",
11345 elsif Is_Volatile (Act_T) and then not Is_Volatile (Ancestor) then
11347 ("cannot have volatile actual type for non-volatile formal type",
11351 -- It should not be necessary to check for unknown discriminants on
11352 -- Formal, but for some reason Has_Unknown_Discriminants is false for
11353 -- A_Gen_T, so Is_Indefinite_Subtype incorrectly returns False. This
11354 -- needs fixing. ???
11356 if not Is_Indefinite_Subtype (A_Gen_T)
11357 and then not Unknown_Discriminants_Present (Formal)
11358 and then Is_Indefinite_Subtype (Act_T)
11361 ("actual subtype must be constrained", Actual);
11362 Abandon_Instantiation (Actual);
11365 if not Unknown_Discriminants_Present (Formal) then
11366 if Is_Constrained (Ancestor) then
11367 if not Is_Constrained (Act_T) then
11369 ("actual subtype must be constrained", Actual);
11370 Abandon_Instantiation (Actual);
11373 -- Ancestor is unconstrained, Check if generic formal and actual
11374 -- agree on constrainedness. The check only applies to array types
11375 -- and discriminated types.
11377 elsif Is_Constrained (Act_T) then
11378 if Ekind (Ancestor) = E_Access_Type
11380 (not Is_Constrained (A_Gen_T)
11381 and then Is_Composite_Type (A_Gen_T))
11384 ("actual subtype must be unconstrained", Actual);
11385 Abandon_Instantiation (Actual);
11388 -- A class-wide type is only allowed if the formal has unknown
11391 elsif Is_Class_Wide_Type (Act_T)
11392 and then not Has_Unknown_Discriminants (Ancestor)
11395 ("actual for & cannot be a class-wide type", Actual, Gen_T);
11396 Abandon_Instantiation (Actual);
11398 -- Otherwise, the formal and actual shall have the same number
11399 -- of discriminants and each discriminant of the actual must
11400 -- correspond to a discriminant of the formal.
11402 elsif Has_Discriminants (Act_T)
11403 and then not Has_Unknown_Discriminants (Act_T)
11404 and then Has_Discriminants (Ancestor)
11406 Actual_Discr := First_Discriminant (Act_T);
11407 Ancestor_Discr := First_Discriminant (Ancestor);
11408 while Present (Actual_Discr)
11409 and then Present (Ancestor_Discr)
11411 if Base_Type (Act_T) /= Base_Type (Ancestor) and then
11412 No (Corresponding_Discriminant (Actual_Discr))
11415 ("discriminant & does not correspond " &
11416 "to ancestor discriminant", Actual, Actual_Discr);
11417 Abandon_Instantiation (Actual);
11420 Next_Discriminant (Actual_Discr);
11421 Next_Discriminant (Ancestor_Discr);
11424 if Present (Actual_Discr) or else Present (Ancestor_Discr) then
11426 ("actual for & must have same number of discriminants",
11428 Abandon_Instantiation (Actual);
11431 -- This case should be caught by the earlier check for
11432 -- constrainedness, but the check here is added for completeness.
11434 elsif Has_Discriminants (Act_T)
11435 and then not Has_Unknown_Discriminants (Act_T)
11438 ("actual for & must not have discriminants", Actual, Gen_T);
11439 Abandon_Instantiation (Actual);
11441 elsif Has_Discriminants (Ancestor) then
11443 ("actual for & must have known discriminants", Actual, Gen_T);
11444 Abandon_Instantiation (Actual);
11447 if not Subtypes_Statically_Compatible
11448 (Act_T, Ancestor, Formal_Derived_Matching => True)
11451 ("constraint on actual is incompatible with formal", Actual);
11452 Abandon_Instantiation (Actual);
11456 -- If the formal and actual types are abstract, check that there
11457 -- are no abstract primitives of the actual type that correspond to
11458 -- nonabstract primitives of the formal type (second sentence of
11461 if Is_Abstract_Type (A_Gen_T) and then Is_Abstract_Type (Act_T) then
11462 Check_Abstract_Primitives : declare
11463 Gen_Prims : constant Elist_Id :=
11464 Primitive_Operations (A_Gen_T);
11465 Gen_Elmt : Elmt_Id;
11466 Gen_Subp : Entity_Id;
11467 Anc_Subp : Entity_Id;
11468 Anc_Formal : Entity_Id;
11469 Anc_F_Type : Entity_Id;
11471 Act_Prims : constant Elist_Id := Primitive_Operations (Act_T);
11472 Act_Elmt : Elmt_Id;
11473 Act_Subp : Entity_Id;
11474 Act_Formal : Entity_Id;
11475 Act_F_Type : Entity_Id;
11477 Subprograms_Correspond : Boolean;
11479 function Is_Tagged_Ancestor (T1, T2 : Entity_Id) return Boolean;
11480 -- Returns true if T2 is derived directly or indirectly from
11481 -- T1, including derivations from interfaces. T1 and T2 are
11482 -- required to be specific tagged base types.
11484 ------------------------
11485 -- Is_Tagged_Ancestor --
11486 ------------------------
11488 function Is_Tagged_Ancestor (T1, T2 : Entity_Id) return Boolean
11490 Intfc_Elmt : Elmt_Id;
11493 -- The predicate is satisfied if the types are the same
11498 -- If we've reached the top of the derivation chain then
11499 -- we know that T1 is not an ancestor of T2.
11501 elsif Etype (T2) = T2 then
11504 -- Proceed to check T2's immediate parent
11506 elsif Is_Ancestor (T1, Base_Type (Etype (T2))) then
11509 -- Finally, check to see if T1 is an ancestor of any of T2's
11513 Intfc_Elmt := First_Elmt (Interfaces (T2));
11514 while Present (Intfc_Elmt) loop
11515 if Is_Ancestor (T1, Node (Intfc_Elmt)) then
11519 Next_Elmt (Intfc_Elmt);
11524 end Is_Tagged_Ancestor;
11526 -- Start of processing for Check_Abstract_Primitives
11529 -- Loop over all of the formal derived type's primitives
11531 Gen_Elmt := First_Elmt (Gen_Prims);
11532 while Present (Gen_Elmt) loop
11533 Gen_Subp := Node (Gen_Elmt);
11535 -- If the primitive of the formal is not abstract, then
11536 -- determine whether there is a corresponding primitive of
11537 -- the actual type that's abstract.
11539 if not Is_Abstract_Subprogram (Gen_Subp) then
11540 Act_Elmt := First_Elmt (Act_Prims);
11541 while Present (Act_Elmt) loop
11542 Act_Subp := Node (Act_Elmt);
11544 -- If we find an abstract primitive of the actual,
11545 -- then we need to test whether it corresponds to the
11546 -- subprogram from which the generic formal primitive
11549 if Is_Abstract_Subprogram (Act_Subp) then
11550 Anc_Subp := Alias (Gen_Subp);
11552 -- Test whether we have a corresponding primitive
11553 -- by comparing names, kinds, formal types, and
11556 if Chars (Anc_Subp) = Chars (Act_Subp)
11557 and then Ekind (Anc_Subp) = Ekind (Act_Subp)
11559 Anc_Formal := First_Formal (Anc_Subp);
11560 Act_Formal := First_Formal (Act_Subp);
11561 while Present (Anc_Formal)
11562 and then Present (Act_Formal)
11564 Anc_F_Type := Etype (Anc_Formal);
11565 Act_F_Type := Etype (Act_Formal);
11567 if Ekind (Anc_F_Type)
11568 = E_Anonymous_Access_Type
11570 Anc_F_Type := Designated_Type (Anc_F_Type);
11572 if Ekind (Act_F_Type)
11573 = E_Anonymous_Access_Type
11576 Designated_Type (Act_F_Type);
11582 Ekind (Act_F_Type) = E_Anonymous_Access_Type
11587 Anc_F_Type := Base_Type (Anc_F_Type);
11588 Act_F_Type := Base_Type (Act_F_Type);
11590 -- If the formal is controlling, then the
11591 -- the type of the actual primitive's formal
11592 -- must be derived directly or indirectly
11593 -- from the type of the ancestor primitive's
11596 if Is_Controlling_Formal (Anc_Formal) then
11597 if not Is_Tagged_Ancestor
11598 (Anc_F_Type, Act_F_Type)
11603 -- Otherwise the types of the formals must
11606 elsif Anc_F_Type /= Act_F_Type then
11610 Next_Entity (Anc_Formal);
11611 Next_Entity (Act_Formal);
11614 -- If we traversed through all of the formals
11615 -- then so far the subprograms correspond, so
11616 -- now check that any result types correspond.
11618 if No (Anc_Formal) and then No (Act_Formal) then
11619 Subprograms_Correspond := True;
11621 if Ekind (Act_Subp) = E_Function then
11622 Anc_F_Type := Etype (Anc_Subp);
11623 Act_F_Type := Etype (Act_Subp);
11625 if Ekind (Anc_F_Type)
11626 = E_Anonymous_Access_Type
11629 Designated_Type (Anc_F_Type);
11631 if Ekind (Act_F_Type)
11632 = E_Anonymous_Access_Type
11635 Designated_Type (Act_F_Type);
11637 Subprograms_Correspond := False;
11642 = E_Anonymous_Access_Type
11644 Subprograms_Correspond := False;
11647 Anc_F_Type := Base_Type (Anc_F_Type);
11648 Act_F_Type := Base_Type (Act_F_Type);
11650 -- Now either the result types must be
11651 -- the same or, if the result type is
11652 -- controlling, the result type of the
11653 -- actual primitive must descend from the
11654 -- result type of the ancestor primitive.
11656 if Subprograms_Correspond
11657 and then Anc_F_Type /= Act_F_Type
11659 Has_Controlling_Result (Anc_Subp)
11661 not Is_Tagged_Ancestor
11662 (Anc_F_Type, Act_F_Type)
11664 Subprograms_Correspond := False;
11668 -- Found a matching subprogram belonging to
11669 -- formal ancestor type, so actual subprogram
11670 -- corresponds and this violates 3.9.3(9).
11672 if Subprograms_Correspond then
11674 ("abstract subprogram & overrides " &
11675 "nonabstract subprogram of ancestor",
11683 Next_Elmt (Act_Elmt);
11687 Next_Elmt (Gen_Elmt);
11689 end Check_Abstract_Primitives;
11692 -- Verify that limitedness matches. If parent is a limited
11693 -- interface then the generic formal is not unless declared
11694 -- explicitly so. If not declared limited, the actual cannot be
11695 -- limited (see AI05-0087).
11697 -- Even though this AI is a binding interpretation, we enable the
11698 -- check only in Ada 2012 mode, because this improper construct
11699 -- shows up in user code and in existing B-tests.
11701 if Is_Limited_Type (Act_T)
11702 and then not Is_Limited_Type (A_Gen_T)
11703 and then Ada_Version >= Ada_2012
11705 if In_Instance then
11709 ("actual for non-limited & cannot be a limited type", Actual,
11711 Explain_Limited_Type (Act_T, Actual);
11712 Abandon_Instantiation (Actual);
11715 end Validate_Derived_Type_Instance;
11717 ----------------------------------------
11718 -- Validate_Discriminated_Formal_Type --
11719 ----------------------------------------
11721 procedure Validate_Discriminated_Formal_Type is
11722 Formal_Discr : Entity_Id;
11723 Actual_Discr : Entity_Id;
11724 Formal_Subt : Entity_Id;
11727 if Has_Discriminants (A_Gen_T) then
11728 if not Has_Discriminants (Act_T) then
11730 ("actual for & must have discriminants", Actual, Gen_T);
11731 Abandon_Instantiation (Actual);
11733 elsif Is_Constrained (Act_T) then
11735 ("actual for & must be unconstrained", Actual, Gen_T);
11736 Abandon_Instantiation (Actual);
11739 Formal_Discr := First_Discriminant (A_Gen_T);
11740 Actual_Discr := First_Discriminant (Act_T);
11741 while Formal_Discr /= Empty loop
11742 if Actual_Discr = Empty then
11744 ("discriminants on actual do not match formal",
11746 Abandon_Instantiation (Actual);
11749 Formal_Subt := Get_Instance_Of (Etype (Formal_Discr));
11751 -- Access discriminants match if designated types do
11753 if Ekind (Base_Type (Formal_Subt)) = E_Anonymous_Access_Type
11754 and then (Ekind (Base_Type (Etype (Actual_Discr)))) =
11755 E_Anonymous_Access_Type
11758 (Designated_Type (Base_Type (Formal_Subt))) =
11759 Designated_Type (Base_Type (Etype (Actual_Discr)))
11763 elsif Base_Type (Formal_Subt) /=
11764 Base_Type (Etype (Actual_Discr))
11767 ("types of actual discriminants must match formal",
11769 Abandon_Instantiation (Actual);
11771 elsif not Subtypes_Statically_Match
11772 (Formal_Subt, Etype (Actual_Discr))
11773 and then Ada_Version >= Ada_95
11776 ("subtypes of actual discriminants must match formal",
11778 Abandon_Instantiation (Actual);
11781 Next_Discriminant (Formal_Discr);
11782 Next_Discriminant (Actual_Discr);
11785 if Actual_Discr /= Empty then
11787 ("discriminants on actual do not match formal",
11789 Abandon_Instantiation (Actual);
11793 end Validate_Discriminated_Formal_Type;
11795 ---------------------------------------
11796 -- Validate_Incomplete_Type_Instance --
11797 ---------------------------------------
11799 procedure Validate_Incomplete_Type_Instance is
11801 if not Is_Tagged_Type (Act_T)
11802 and then Is_Tagged_Type (A_Gen_T)
11805 ("actual for & must be a tagged type", Actual, Gen_T);
11808 Validate_Discriminated_Formal_Type;
11809 end Validate_Incomplete_Type_Instance;
11811 --------------------------------------
11812 -- Validate_Interface_Type_Instance --
11813 --------------------------------------
11815 procedure Validate_Interface_Type_Instance is
11817 if not Is_Interface (Act_T) then
11819 ("actual for formal interface type must be an interface",
11822 elsif Is_Limited_Type (Act_T) /= Is_Limited_Type (A_Gen_T)
11824 Is_Task_Interface (A_Gen_T) /= Is_Task_Interface (Act_T)
11826 Is_Protected_Interface (A_Gen_T) /=
11827 Is_Protected_Interface (Act_T)
11829 Is_Synchronized_Interface (A_Gen_T) /=
11830 Is_Synchronized_Interface (Act_T)
11833 ("actual for interface& does not match (RM 12.5.5(4))",
11836 end Validate_Interface_Type_Instance;
11838 ------------------------------------
11839 -- Validate_Private_Type_Instance --
11840 ------------------------------------
11842 procedure Validate_Private_Type_Instance is
11844 if Is_Limited_Type (Act_T)
11845 and then not Is_Limited_Type (A_Gen_T)
11847 if In_Instance then
11851 ("actual for non-limited & cannot be a limited type", Actual,
11853 Explain_Limited_Type (Act_T, Actual);
11854 Abandon_Instantiation (Actual);
11857 elsif Known_To_Have_Preelab_Init (A_Gen_T)
11858 and then not Has_Preelaborable_Initialization (Act_T)
11861 ("actual for & must have preelaborable initialization", Actual,
11864 elsif Is_Indefinite_Subtype (Act_T)
11865 and then not Is_Indefinite_Subtype (A_Gen_T)
11866 and then Ada_Version >= Ada_95
11869 ("actual for & must be a definite subtype", Actual, Gen_T);
11871 elsif not Is_Tagged_Type (Act_T)
11872 and then Is_Tagged_Type (A_Gen_T)
11875 ("actual for & must be a tagged type", Actual, Gen_T);
11878 Validate_Discriminated_Formal_Type;
11880 end Validate_Private_Type_Instance;
11882 -- Start of processing for Instantiate_Type
11885 if Get_Instance_Of (A_Gen_T) /= A_Gen_T then
11886 Error_Msg_N ("duplicate instantiation of generic type", Actual);
11887 return New_List (Error);
11889 elsif not Is_Entity_Name (Actual)
11890 or else not Is_Type (Entity (Actual))
11893 ("expect valid subtype mark to instantiate &", Actual, Gen_T);
11894 Abandon_Instantiation (Actual);
11897 Act_T := Entity (Actual);
11899 -- Ada 2005 (AI-216): An Unchecked_Union subtype shall only be passed
11900 -- as a generic actual parameter if the corresponding formal type
11901 -- does not have a known_discriminant_part, or is a formal derived
11902 -- type that is an Unchecked_Union type.
11904 if Is_Unchecked_Union (Base_Type (Act_T)) then
11905 if not Has_Discriminants (A_Gen_T)
11907 (Is_Derived_Type (A_Gen_T)
11909 Is_Unchecked_Union (A_Gen_T))
11913 Error_Msg_N ("unchecked union cannot be the actual for a" &
11914 " discriminated formal type", Act_T);
11919 -- Deal with fixed/floating restrictions
11921 if Is_Floating_Point_Type (Act_T) then
11922 Check_Restriction (No_Floating_Point, Actual);
11923 elsif Is_Fixed_Point_Type (Act_T) then
11924 Check_Restriction (No_Fixed_Point, Actual);
11927 -- Deal with error of using incomplete type as generic actual.
11928 -- This includes limited views of a type, even if the non-limited
11929 -- view may be available.
11931 if Ekind (Act_T) = E_Incomplete_Type
11932 or else (Is_Class_Wide_Type (Act_T)
11934 Ekind (Root_Type (Act_T)) = E_Incomplete_Type)
11936 -- If the formal is an incomplete type, the actual can be
11937 -- incomplete as well.
11939 if Ekind (A_Gen_T) = E_Incomplete_Type then
11942 elsif Is_Class_Wide_Type (Act_T)
11943 or else No (Full_View (Act_T))
11945 Error_Msg_N ("premature use of incomplete type", Actual);
11946 Abandon_Instantiation (Actual);
11948 Act_T := Full_View (Act_T);
11949 Set_Entity (Actual, Act_T);
11951 if Has_Private_Component (Act_T) then
11953 ("premature use of type with private component", Actual);
11957 -- Deal with error of premature use of private type as generic actual
11959 elsif Is_Private_Type (Act_T)
11960 and then Is_Private_Type (Base_Type (Act_T))
11961 and then not Is_Generic_Type (Act_T)
11962 and then not Is_Derived_Type (Act_T)
11963 and then No (Full_View (Root_Type (Act_T)))
11965 -- If the formal is an incomplete type, the actual can be
11966 -- private or incomplete as well.
11968 if Ekind (A_Gen_T) = E_Incomplete_Type then
11971 Error_Msg_N ("premature use of private type", Actual);
11974 elsif Has_Private_Component (Act_T) then
11976 ("premature use of type with private component", Actual);
11979 Set_Instance_Of (A_Gen_T, Act_T);
11981 -- If the type is generic, the class-wide type may also be used
11983 if Is_Tagged_Type (A_Gen_T)
11984 and then Is_Tagged_Type (Act_T)
11985 and then not Is_Class_Wide_Type (A_Gen_T)
11987 Set_Instance_Of (Class_Wide_Type (A_Gen_T),
11988 Class_Wide_Type (Act_T));
11991 if not Is_Abstract_Type (A_Gen_T)
11992 and then Is_Abstract_Type (Act_T)
11995 ("actual of non-abstract formal cannot be abstract", Actual);
11998 -- A generic scalar type is a first subtype for which we generate
11999 -- an anonymous base type. Indicate that the instance of this base
12000 -- is the base type of the actual.
12002 if Is_Scalar_Type (A_Gen_T) then
12003 Set_Instance_Of (Etype (A_Gen_T), Etype (Act_T));
12007 if Error_Posted (Act_T) then
12010 case Nkind (Def) is
12011 when N_Formal_Private_Type_Definition =>
12012 Validate_Private_Type_Instance;
12014 when N_Formal_Incomplete_Type_Definition =>
12015 Validate_Incomplete_Type_Instance;
12017 when N_Formal_Derived_Type_Definition =>
12018 Validate_Derived_Type_Instance;
12020 when N_Formal_Discrete_Type_Definition =>
12021 if not Is_Discrete_Type (Act_T) then
12023 ("expect discrete type in instantiation of&",
12025 Abandon_Instantiation (Actual);
12028 Diagnose_Predicated_Actual;
12030 when N_Formal_Signed_Integer_Type_Definition =>
12031 if not Is_Signed_Integer_Type (Act_T) then
12033 ("expect signed integer type in instantiation of&",
12035 Abandon_Instantiation (Actual);
12038 Diagnose_Predicated_Actual;
12040 when N_Formal_Modular_Type_Definition =>
12041 if not Is_Modular_Integer_Type (Act_T) then
12043 ("expect modular type in instantiation of &",
12045 Abandon_Instantiation (Actual);
12048 Diagnose_Predicated_Actual;
12050 when N_Formal_Floating_Point_Definition =>
12051 if not Is_Floating_Point_Type (Act_T) then
12053 ("expect float type in instantiation of &", Actual, Gen_T);
12054 Abandon_Instantiation (Actual);
12057 when N_Formal_Ordinary_Fixed_Point_Definition =>
12058 if not Is_Ordinary_Fixed_Point_Type (Act_T) then
12060 ("expect ordinary fixed point type in instantiation of &",
12062 Abandon_Instantiation (Actual);
12065 when N_Formal_Decimal_Fixed_Point_Definition =>
12066 if not Is_Decimal_Fixed_Point_Type (Act_T) then
12068 ("expect decimal type in instantiation of &",
12070 Abandon_Instantiation (Actual);
12073 when N_Array_Type_Definition =>
12074 Validate_Array_Type_Instance;
12076 when N_Access_To_Object_Definition =>
12077 Validate_Access_Type_Instance;
12079 when N_Access_Function_Definition |
12080 N_Access_Procedure_Definition =>
12081 Validate_Access_Subprogram_Instance;
12083 when N_Record_Definition =>
12084 Validate_Interface_Type_Instance;
12086 when N_Derived_Type_Definition =>
12087 Validate_Derived_Interface_Type_Instance;
12090 raise Program_Error;
12095 Subt := New_Copy (Gen_T);
12097 -- Use adjusted sloc of subtype name as the location for other nodes in
12098 -- the subtype declaration.
12100 Loc := Sloc (Subt);
12103 Make_Subtype_Declaration (Loc,
12104 Defining_Identifier => Subt,
12105 Subtype_Indication => New_Occurrence_Of (Act_T, Loc));
12107 if Is_Private_Type (Act_T) then
12108 Set_Has_Private_View (Subtype_Indication (Decl_Node));
12110 elsif Is_Access_Type (Act_T)
12111 and then Is_Private_Type (Designated_Type (Act_T))
12113 Set_Has_Private_View (Subtype_Indication (Decl_Node));
12116 Decl_Nodes := New_List (Decl_Node);
12118 -- Flag actual derived types so their elaboration produces the
12119 -- appropriate renamings for the primitive operations of the ancestor.
12120 -- Flag actual for formal private types as well, to determine whether
12121 -- operations in the private part may override inherited operations.
12122 -- If the formal has an interface list, the ancestor is not the
12123 -- parent, but the analyzed formal that includes the interface
12124 -- operations of all its progenitors.
12126 -- Same treatment for formal private types, so we can check whether the
12127 -- type is tagged limited when validating derivations in the private
12128 -- part. (See AI05-096).
12130 if Nkind (Def) = N_Formal_Derived_Type_Definition then
12131 if Present (Interface_List (Def)) then
12132 Set_Generic_Parent_Type (Decl_Node, A_Gen_T);
12134 Set_Generic_Parent_Type (Decl_Node, Ancestor);
12137 elsif Nkind_In (Def,
12138 N_Formal_Private_Type_Definition,
12139 N_Formal_Incomplete_Type_Definition)
12141 Set_Generic_Parent_Type (Decl_Node, A_Gen_T);
12144 -- If the actual is a synchronized type that implements an interface,
12145 -- the primitive operations are attached to the corresponding record,
12146 -- and we have to treat it as an additional generic actual, so that its
12147 -- primitive operations become visible in the instance. The task or
12148 -- protected type itself does not carry primitive operations.
12150 if Is_Concurrent_Type (Act_T)
12151 and then Is_Tagged_Type (Act_T)
12152 and then Present (Corresponding_Record_Type (Act_T))
12153 and then Present (Ancestor)
12154 and then Is_Interface (Ancestor)
12157 Corr_Rec : constant Entity_Id :=
12158 Corresponding_Record_Type (Act_T);
12159 New_Corr : Entity_Id;
12160 Corr_Decl : Node_Id;
12163 New_Corr := Make_Temporary (Loc, 'S');
12165 Make_Subtype_Declaration (Loc,
12166 Defining_Identifier => New_Corr,
12167 Subtype_Indication =>
12168 New_Occurrence_Of (Corr_Rec, Loc));
12169 Append_To (Decl_Nodes, Corr_Decl);
12171 if Ekind (Act_T) = E_Task_Type then
12172 Set_Ekind (Subt, E_Task_Subtype);
12174 Set_Ekind (Subt, E_Protected_Subtype);
12177 Set_Corresponding_Record_Type (Subt, Corr_Rec);
12178 Set_Generic_Parent_Type (Corr_Decl, Ancestor);
12179 Set_Generic_Parent_Type (Decl_Node, Empty);
12184 end Instantiate_Type;
12186 ---------------------
12187 -- Is_In_Main_Unit --
12188 ---------------------
12190 function Is_In_Main_Unit (N : Node_Id) return Boolean is
12191 Unum : constant Unit_Number_Type := Get_Source_Unit (N);
12192 Current_Unit : Node_Id;
12195 if Unum = Main_Unit then
12198 -- If the current unit is a subunit then it is either the main unit or
12199 -- is being compiled as part of the main unit.
12201 elsif Nkind (N) = N_Compilation_Unit then
12202 return Nkind (Unit (N)) = N_Subunit;
12205 Current_Unit := Parent (N);
12206 while Present (Current_Unit)
12207 and then Nkind (Current_Unit) /= N_Compilation_Unit
12209 Current_Unit := Parent (Current_Unit);
12212 -- The instantiation node is in the main unit, or else the current node
12213 -- (perhaps as the result of nested instantiations) is in the main unit,
12214 -- or in the declaration of the main unit, which in this last case must
12217 return Unum = Main_Unit
12218 or else Current_Unit = Cunit (Main_Unit)
12219 or else Current_Unit = Library_Unit (Cunit (Main_Unit))
12220 or else (Present (Library_Unit (Current_Unit))
12221 and then Is_In_Main_Unit (Library_Unit (Current_Unit)));
12222 end Is_In_Main_Unit;
12224 ----------------------------
12225 -- Load_Parent_Of_Generic --
12226 ----------------------------
12228 procedure Load_Parent_Of_Generic
12231 Body_Optional : Boolean := False)
12233 Comp_Unit : constant Node_Id := Cunit (Get_Source_Unit (Spec));
12234 Saved_Style_Check : constant Boolean := Style_Check;
12235 Saved_Warnings : constant Warning_Record := Save_Warnings;
12236 True_Parent : Node_Id;
12237 Inst_Node : Node_Id;
12239 Previous_Instances : constant Elist_Id := New_Elmt_List;
12241 procedure Collect_Previous_Instances (Decls : List_Id);
12242 -- Collect all instantiations in the given list of declarations, that
12243 -- precede the generic that we need to load. If the bodies of these
12244 -- instantiations are available, we must analyze them, to ensure that
12245 -- the public symbols generated are the same when the unit is compiled
12246 -- to generate code, and when it is compiled in the context of a unit
12247 -- that needs a particular nested instance. This process is applied to
12248 -- both package and subprogram instances.
12250 --------------------------------
12251 -- Collect_Previous_Instances --
12252 --------------------------------
12254 procedure Collect_Previous_Instances (Decls : List_Id) is
12258 Decl := First (Decls);
12259 while Present (Decl) loop
12260 if Sloc (Decl) >= Sloc (Inst_Node) then
12263 -- If Decl is an instantiation, then record it as requiring
12264 -- instantiation of the corresponding body, except if it is an
12265 -- abbreviated instantiation generated internally for conformance
12266 -- checking purposes only for the case of a formal package
12267 -- declared without a box (see Instantiate_Formal_Package). Such
12268 -- an instantiation does not generate any code (the actual code
12269 -- comes from actual) and thus does not need to be analyzed here.
12270 -- If the instantiation appears with a generic package body it is
12271 -- not analyzed here either.
12273 elsif Nkind (Decl) = N_Package_Instantiation
12274 and then not Is_Internal (Defining_Entity (Decl))
12276 Append_Elmt (Decl, Previous_Instances);
12278 -- For a subprogram instantiation, omit instantiations intrinsic
12279 -- operations (Unchecked_Conversions, etc.) that have no bodies.
12281 elsif Nkind_In (Decl, N_Function_Instantiation,
12282 N_Procedure_Instantiation)
12283 and then not Is_Intrinsic_Subprogram (Entity (Name (Decl)))
12285 Append_Elmt (Decl, Previous_Instances);
12287 elsif Nkind (Decl) = N_Package_Declaration then
12288 Collect_Previous_Instances
12289 (Visible_Declarations (Specification (Decl)));
12290 Collect_Previous_Instances
12291 (Private_Declarations (Specification (Decl)));
12293 -- Previous non-generic bodies may contain instances as well
12295 elsif Nkind (Decl) = N_Package_Body
12296 and then Ekind (Corresponding_Spec (Decl)) /= E_Generic_Package
12298 Collect_Previous_Instances (Declarations (Decl));
12300 elsif Nkind (Decl) = N_Subprogram_Body
12301 and then not Acts_As_Spec (Decl)
12302 and then not Is_Generic_Subprogram (Corresponding_Spec (Decl))
12304 Collect_Previous_Instances (Declarations (Decl));
12309 end Collect_Previous_Instances;
12311 -- Start of processing for Load_Parent_Of_Generic
12314 if not In_Same_Source_Unit (N, Spec)
12315 or else Nkind (Unit (Comp_Unit)) = N_Package_Declaration
12316 or else (Nkind (Unit (Comp_Unit)) = N_Package_Body
12317 and then not Is_In_Main_Unit (Spec))
12319 -- Find body of parent of spec, and analyze it. A special case arises
12320 -- when the parent is an instantiation, that is to say when we are
12321 -- currently instantiating a nested generic. In that case, there is
12322 -- no separate file for the body of the enclosing instance. Instead,
12323 -- the enclosing body must be instantiated as if it were a pending
12324 -- instantiation, in order to produce the body for the nested generic
12325 -- we require now. Note that in that case the generic may be defined
12326 -- in a package body, the instance defined in the same package body,
12327 -- and the original enclosing body may not be in the main unit.
12329 Inst_Node := Empty;
12331 True_Parent := Parent (Spec);
12332 while Present (True_Parent)
12333 and then Nkind (True_Parent) /= N_Compilation_Unit
12335 if Nkind (True_Parent) = N_Package_Declaration
12337 Nkind (Original_Node (True_Parent)) = N_Package_Instantiation
12339 -- Parent is a compilation unit that is an instantiation.
12340 -- Instantiation node has been replaced with package decl.
12342 Inst_Node := Original_Node (True_Parent);
12345 elsif Nkind (True_Parent) = N_Package_Declaration
12346 and then Present (Generic_Parent (Specification (True_Parent)))
12347 and then Nkind (Parent (True_Parent)) /= N_Compilation_Unit
12349 -- Parent is an instantiation within another specification.
12350 -- Declaration for instance has been inserted before original
12351 -- instantiation node. A direct link would be preferable?
12353 Inst_Node := Next (True_Parent);
12354 while Present (Inst_Node)
12355 and then Nkind (Inst_Node) /= N_Package_Instantiation
12360 -- If the instance appears within a generic, and the generic
12361 -- unit is defined within a formal package of the enclosing
12362 -- generic, there is no generic body available, and none
12363 -- needed. A more precise test should be used ???
12365 if No (Inst_Node) then
12372 True_Parent := Parent (True_Parent);
12376 -- Case where we are currently instantiating a nested generic
12378 if Present (Inst_Node) then
12379 if Nkind (Parent (True_Parent)) = N_Compilation_Unit then
12381 -- Instantiation node and declaration of instantiated package
12382 -- were exchanged when only the declaration was needed.
12383 -- Restore instantiation node before proceeding with body.
12385 Set_Unit (Parent (True_Parent), Inst_Node);
12388 -- Now complete instantiation of enclosing body, if it appears in
12389 -- some other unit. If it appears in the current unit, the body
12390 -- will have been instantiated already.
12392 if No (Corresponding_Body (Instance_Spec (Inst_Node))) then
12394 -- We need to determine the expander mode to instantiate the
12395 -- enclosing body. Because the generic body we need may use
12396 -- global entities declared in the enclosing package (including
12397 -- aggregates) it is in general necessary to compile this body
12398 -- with expansion enabled, except if we are within a generic
12399 -- package, in which case the usual generic rule applies.
12402 Exp_Status : Boolean := True;
12406 -- Loop through scopes looking for generic package
12408 Scop := Scope (Defining_Entity (Instance_Spec (Inst_Node)));
12409 while Present (Scop)
12410 and then Scop /= Standard_Standard
12412 if Ekind (Scop) = E_Generic_Package then
12413 Exp_Status := False;
12417 Scop := Scope (Scop);
12420 -- Collect previous instantiations in the unit that contains
12421 -- the desired generic.
12423 if Nkind (Parent (True_Parent)) /= N_Compilation_Unit
12424 and then not Body_Optional
12428 Info : Pending_Body_Info;
12432 Par := Parent (Inst_Node);
12433 while Present (Par) loop
12434 exit when Nkind (Parent (Par)) = N_Compilation_Unit;
12435 Par := Parent (Par);
12438 pragma Assert (Present (Par));
12440 if Nkind (Par) = N_Package_Body then
12441 Collect_Previous_Instances (Declarations (Par));
12443 elsif Nkind (Par) = N_Package_Declaration then
12444 Collect_Previous_Instances
12445 (Visible_Declarations (Specification (Par)));
12446 Collect_Previous_Instances
12447 (Private_Declarations (Specification (Par)));
12450 -- Enclosing unit is a subprogram body. In this
12451 -- case all instance bodies are processed in order
12452 -- and there is no need to collect them separately.
12457 Decl := First_Elmt (Previous_Instances);
12458 while Present (Decl) loop
12460 (Inst_Node => Node (Decl),
12462 Instance_Spec (Node (Decl)),
12463 Expander_Status => Exp_Status,
12464 Current_Sem_Unit =>
12465 Get_Code_Unit (Sloc (Node (Decl))),
12466 Scope_Suppress => Scope_Suppress,
12467 Local_Suppress_Stack_Top =>
12468 Local_Suppress_Stack_Top,
12469 Version => Ada_Version,
12470 Version_Pragma => Ada_Version_Pragma,
12471 Warnings => Save_Warnings,
12472 SPARK_Mode => SPARK_Mode,
12473 SPARK_Mode_Pragma => SPARK_Mode_Pragma);
12475 -- Package instance
12478 Nkind (Node (Decl)) = N_Package_Instantiation
12480 Instantiate_Package_Body
12481 (Info, Body_Optional => True);
12483 -- Subprogram instance
12486 -- The instance_spec is the wrapper package,
12487 -- and the subprogram declaration is the last
12488 -- declaration in the wrapper.
12492 (Visible_Declarations
12493 (Specification (Info.Act_Decl)));
12495 Instantiate_Subprogram_Body
12496 (Info, Body_Optional => True);
12504 Instantiate_Package_Body
12506 ((Inst_Node => Inst_Node,
12507 Act_Decl => True_Parent,
12508 Expander_Status => Exp_Status,
12509 Current_Sem_Unit => Get_Code_Unit
12510 (Sloc (Inst_Node)),
12511 Scope_Suppress => Scope_Suppress,
12512 Local_Suppress_Stack_Top => Local_Suppress_Stack_Top,
12513 Version => Ada_Version,
12514 Version_Pragma => Ada_Version_Pragma,
12515 Warnings => Save_Warnings,
12516 SPARK_Mode => SPARK_Mode,
12517 SPARK_Mode_Pragma => SPARK_Mode_Pragma)),
12518 Body_Optional => Body_Optional);
12522 -- Case where we are not instantiating a nested generic
12525 Opt.Style_Check := False;
12526 Expander_Mode_Save_And_Set (True);
12527 Load_Needed_Body (Comp_Unit, OK);
12528 Opt.Style_Check := Saved_Style_Check;
12529 Restore_Warnings (Saved_Warnings);
12530 Expander_Mode_Restore;
12533 and then Unit_Requires_Body (Defining_Entity (Spec))
12534 and then not Body_Optional
12537 Bname : constant Unit_Name_Type :=
12538 Get_Body_Name (Get_Unit_Name (Unit (Comp_Unit)));
12541 -- In CodePeer mode, the missing body may make the analysis
12542 -- incomplete, but we do not treat it as fatal.
12544 if CodePeer_Mode then
12548 Error_Msg_Unit_1 := Bname;
12549 Error_Msg_N ("this instantiation requires$!", N);
12550 Error_Msg_File_1 :=
12551 Get_File_Name (Bname, Subunit => False);
12552 Error_Msg_N ("\but file{ was not found!", N);
12553 raise Unrecoverable_Error;
12560 -- If loading parent of the generic caused an instantiation circularity,
12561 -- we abandon compilation at this point, because otherwise in some cases
12562 -- we get into trouble with infinite recursions after this point.
12564 if Circularity_Detected then
12565 raise Unrecoverable_Error;
12567 end Load_Parent_Of_Generic;
12569 ---------------------------------
12570 -- Map_Formal_Package_Entities --
12571 ---------------------------------
12573 procedure Map_Formal_Package_Entities (Form : Entity_Id; Act : Entity_Id) is
12578 Set_Instance_Of (Form, Act);
12580 -- Traverse formal and actual package to map the corresponding entities.
12581 -- We skip over internal entities that may be generated during semantic
12582 -- analysis, and find the matching entities by name, given that they
12583 -- must appear in the same order.
12585 E1 := First_Entity (Form);
12586 E2 := First_Entity (Act);
12587 while Present (E1) and then E1 /= First_Private_Entity (Form) loop
12588 -- Could this test be a single condition??? Seems like it could, and
12589 -- isn't FPE (Form) a constant anyway???
12591 if not Is_Internal (E1)
12592 and then Present (Parent (E1))
12593 and then not Is_Class_Wide_Type (E1)
12594 and then not Is_Internal_Name (Chars (E1))
12596 while Present (E2) and then Chars (E2) /= Chars (E1) loop
12603 Set_Instance_Of (E1, E2);
12605 if Is_Type (E1) and then Is_Tagged_Type (E2) then
12606 Set_Instance_Of (Class_Wide_Type (E1), Class_Wide_Type (E2));
12609 if Is_Constrained (E1) then
12610 Set_Instance_Of (Base_Type (E1), Base_Type (E2));
12613 if Ekind (E1) = E_Package and then No (Renamed_Object (E1)) then
12614 Map_Formal_Package_Entities (E1, E2);
12621 end Map_Formal_Package_Entities;
12623 -----------------------
12624 -- Move_Freeze_Nodes --
12625 -----------------------
12627 procedure Move_Freeze_Nodes
12628 (Out_Of : Entity_Id;
12633 Next_Decl : Node_Id;
12634 Next_Node : Node_Id := After;
12637 function Is_Outer_Type (T : Entity_Id) return Boolean;
12638 -- Check whether entity is declared in a scope external to that of the
12641 -------------------
12642 -- Is_Outer_Type --
12643 -------------------
12645 function Is_Outer_Type (T : Entity_Id) return Boolean is
12646 Scop : Entity_Id := Scope (T);
12649 if Scope_Depth (Scop) < Scope_Depth (Out_Of) then
12653 while Scop /= Standard_Standard loop
12654 if Scop = Out_Of then
12657 Scop := Scope (Scop);
12665 -- Start of processing for Move_Freeze_Nodes
12672 -- First remove the freeze nodes that may appear before all other
12676 while Present (Decl)
12677 and then Nkind (Decl) = N_Freeze_Entity
12678 and then Is_Outer_Type (Entity (Decl))
12680 Decl := Remove_Head (L);
12681 Insert_After (Next_Node, Decl);
12682 Set_Analyzed (Decl, False);
12687 -- Next scan the list of declarations and remove each freeze node that
12688 -- appears ahead of the current node.
12690 while Present (Decl) loop
12691 while Present (Next (Decl))
12692 and then Nkind (Next (Decl)) = N_Freeze_Entity
12693 and then Is_Outer_Type (Entity (Next (Decl)))
12695 Next_Decl := Remove_Next (Decl);
12696 Insert_After (Next_Node, Next_Decl);
12697 Set_Analyzed (Next_Decl, False);
12698 Next_Node := Next_Decl;
12701 -- If the declaration is a nested package or concurrent type, then
12702 -- recurse. Nested generic packages will have been processed from the
12705 case Nkind (Decl) is
12706 when N_Package_Declaration =>
12707 Spec := Specification (Decl);
12709 when N_Task_Type_Declaration =>
12710 Spec := Task_Definition (Decl);
12712 when N_Protected_Type_Declaration =>
12713 Spec := Protected_Definition (Decl);
12719 if Present (Spec) then
12720 Move_Freeze_Nodes (Out_Of, Next_Node, Visible_Declarations (Spec));
12721 Move_Freeze_Nodes (Out_Of, Next_Node, Private_Declarations (Spec));
12726 end Move_Freeze_Nodes;
12732 function Next_Assoc (E : Assoc_Ptr) return Assoc_Ptr is
12734 return Generic_Renamings.Table (E).Next_In_HTable;
12737 ------------------------
12738 -- Preanalyze_Actuals --
12739 ------------------------
12741 procedure Preanalyze_Actuals (N : Node_Id) is
12744 Errs : constant Int := Serious_Errors_Detected;
12746 Cur : Entity_Id := Empty;
12747 -- Current homograph of the instance name
12750 -- Saved visibility status of the current homograph
12753 Assoc := First (Generic_Associations (N));
12755 -- If the instance is a child unit, its name may hide an outer homonym,
12756 -- so make it invisible to perform name resolution on the actuals.
12758 if Nkind (Defining_Unit_Name (N)) = N_Defining_Program_Unit_Name
12760 (Current_Entity (Defining_Identifier (Defining_Unit_Name (N))))
12762 Cur := Current_Entity (Defining_Identifier (Defining_Unit_Name (N)));
12764 if Is_Compilation_Unit (Cur) then
12765 Vis := Is_Immediately_Visible (Cur);
12766 Set_Is_Immediately_Visible (Cur, False);
12772 while Present (Assoc) loop
12773 if Nkind (Assoc) /= N_Others_Choice then
12774 Act := Explicit_Generic_Actual_Parameter (Assoc);
12776 -- Within a nested instantiation, a defaulted actual is an empty
12777 -- association, so nothing to analyze. If the subprogram actual
12778 -- is an attribute, analyze prefix only, because actual is not a
12779 -- complete attribute reference.
12781 -- If actual is an allocator, analyze expression only. The full
12782 -- analysis can generate code, and if instance is a compilation
12783 -- unit we have to wait until the package instance is installed
12784 -- to have a proper place to insert this code.
12786 -- String literals may be operators, but at this point we do not
12787 -- know whether the actual is a formal subprogram or a string.
12792 elsif Nkind (Act) = N_Attribute_Reference then
12793 Analyze (Prefix (Act));
12795 elsif Nkind (Act) = N_Explicit_Dereference then
12796 Analyze (Prefix (Act));
12798 elsif Nkind (Act) = N_Allocator then
12800 Expr : constant Node_Id := Expression (Act);
12803 if Nkind (Expr) = N_Subtype_Indication then
12804 Analyze (Subtype_Mark (Expr));
12806 -- Analyze separately each discriminant constraint, when
12807 -- given with a named association.
12813 Constr := First (Constraints (Constraint (Expr)));
12814 while Present (Constr) loop
12815 if Nkind (Constr) = N_Discriminant_Association then
12816 Analyze (Expression (Constr));
12830 elsif Nkind (Act) /= N_Operator_Symbol then
12834 -- Ensure that a ghost subprogram does not act as generic actual
12836 if Is_Entity_Name (Act)
12837 and then Is_Ghost_Subprogram (Entity (Act))
12840 ("ghost subprogram & cannot act as generic actual", Act);
12841 Abandon_Instantiation (Act);
12843 elsif Errs /= Serious_Errors_Detected then
12845 -- Do a minimal analysis of the generic, to prevent spurious
12846 -- warnings complaining about the generic being unreferenced,
12847 -- before abandoning the instantiation.
12849 Analyze (Name (N));
12851 if Is_Entity_Name (Name (N))
12852 and then Etype (Name (N)) /= Any_Type
12854 Generate_Reference (Entity (Name (N)), Name (N));
12855 Set_Is_Instantiated (Entity (Name (N)));
12858 if Present (Cur) then
12860 -- For the case of a child instance hiding an outer homonym,
12861 -- provide additional warning which might explain the error.
12863 Set_Is_Immediately_Visible (Cur, Vis);
12864 Error_Msg_NE ("& hides outer unit with the same name??",
12865 N, Defining_Unit_Name (N));
12868 Abandon_Instantiation (Act);
12875 if Present (Cur) then
12876 Set_Is_Immediately_Visible (Cur, Vis);
12878 end Preanalyze_Actuals;
12880 -------------------
12881 -- Remove_Parent --
12882 -------------------
12884 procedure Remove_Parent (In_Body : Boolean := False) is
12885 S : Entity_Id := Current_Scope;
12886 -- S is the scope containing the instantiation just completed. The scope
12887 -- stack contains the parent instances of the instantiation, followed by
12896 -- After child instantiation is complete, remove from scope stack the
12897 -- extra copy of the current scope, and then remove parent instances.
12899 if not In_Body then
12902 while Current_Scope /= S loop
12903 P := Current_Scope;
12904 End_Package_Scope (Current_Scope);
12906 if In_Open_Scopes (P) then
12907 E := First_Entity (P);
12908 while Present (E) loop
12909 Set_Is_Immediately_Visible (E, True);
12913 -- If instantiation is declared in a block, it is the enclosing
12914 -- scope that might be a parent instance. Note that only one
12915 -- block can be involved, because the parent instances have
12916 -- been installed within it.
12918 if Ekind (P) = E_Block then
12919 Cur_P := Scope (P);
12924 if Is_Generic_Instance (Cur_P) and then P /= Current_Scope then
12925 -- We are within an instance of some sibling. Retain
12926 -- visibility of parent, for proper subsequent cleanup, and
12927 -- reinstall private declarations as well.
12929 Set_In_Private_Part (P);
12930 Install_Private_Declarations (P);
12933 -- If the ultimate parent is a top-level unit recorded in
12934 -- Instance_Parent_Unit, then reset its visibility to what it was
12935 -- before instantiation. (It's not clear what the purpose is of
12936 -- testing whether Scope (P) is In_Open_Scopes, but that test was
12937 -- present before the ultimate parent test was added.???)
12939 elsif not In_Open_Scopes (Scope (P))
12940 or else (P = Instance_Parent_Unit
12941 and then not Parent_Unit_Visible)
12943 Set_Is_Immediately_Visible (P, False);
12945 -- If the current scope is itself an instantiation of a generic
12946 -- nested within P, and we are in the private part of body of this
12947 -- instantiation, restore the full views of P, that were removed
12948 -- in End_Package_Scope above. This obscure case can occur when a
12949 -- subunit of a generic contains an instance of a child unit of
12950 -- its generic parent unit.
12952 elsif S = Current_Scope and then Is_Generic_Instance (S) then
12954 Par : constant Entity_Id :=
12955 Generic_Parent (Package_Specification (S));
12958 and then P = Scope (Par)
12959 and then (In_Package_Body (S) or else In_Private_Part (S))
12961 Set_In_Private_Part (P);
12962 Install_Private_Declarations (P);
12968 -- Reset visibility of entities in the enclosing scope
12970 Set_Is_Hidden_Open_Scope (Current_Scope, False);
12972 Hidden := First_Elmt (Hidden_Entities);
12973 while Present (Hidden) loop
12974 Set_Is_Immediately_Visible (Node (Hidden), True);
12975 Next_Elmt (Hidden);
12979 -- Each body is analyzed separately, and there is no context that
12980 -- needs preserving from one body instance to the next, so remove all
12981 -- parent scopes that have been installed.
12983 while Present (S) loop
12984 End_Package_Scope (S);
12985 Set_Is_Immediately_Visible (S, False);
12986 S := Current_Scope;
12987 exit when S = Standard_Standard;
12996 procedure Restore_Env is
12997 Saved : Instance_Env renames Instance_Envs.Table (Instance_Envs.Last);
13000 if No (Current_Instantiated_Parent.Act_Id) then
13001 -- Restore environment after subprogram inlining
13003 Restore_Private_Views (Empty);
13006 Current_Instantiated_Parent := Saved.Instantiated_Parent;
13007 Exchanged_Views := Saved.Exchanged_Views;
13008 Hidden_Entities := Saved.Hidden_Entities;
13009 Current_Sem_Unit := Saved.Current_Sem_Unit;
13010 Parent_Unit_Visible := Saved.Parent_Unit_Visible;
13011 Instance_Parent_Unit := Saved.Instance_Parent_Unit;
13013 Restore_Opt_Config_Switches (Saved.Switches);
13015 Instance_Envs.Decrement_Last;
13018 ---------------------------
13019 -- Restore_Private_Views --
13020 ---------------------------
13022 procedure Restore_Private_Views
13023 (Pack_Id : Entity_Id;
13024 Is_Package : Boolean := True)
13029 Dep_Elmt : Elmt_Id;
13032 procedure Restore_Nested_Formal (Formal : Entity_Id);
13033 -- Hide the generic formals of formal packages declared with box which
13034 -- were reachable in the current instantiation.
13036 ---------------------------
13037 -- Restore_Nested_Formal --
13038 ---------------------------
13040 procedure Restore_Nested_Formal (Formal : Entity_Id) is
13044 if Present (Renamed_Object (Formal))
13045 and then Denotes_Formal_Package (Renamed_Object (Formal), True)
13049 elsif Present (Associated_Formal_Package (Formal)) then
13050 Ent := First_Entity (Formal);
13051 while Present (Ent) loop
13052 exit when Ekind (Ent) = E_Package
13053 and then Renamed_Entity (Ent) = Renamed_Entity (Formal);
13055 Set_Is_Hidden (Ent);
13056 Set_Is_Potentially_Use_Visible (Ent, False);
13058 -- If package, then recurse
13060 if Ekind (Ent) = E_Package then
13061 Restore_Nested_Formal (Ent);
13067 end Restore_Nested_Formal;
13069 -- Start of processing for Restore_Private_Views
13072 M := First_Elmt (Exchanged_Views);
13073 while Present (M) loop
13076 -- Subtypes of types whose views have been exchanged, and that are
13077 -- defined within the instance, were not on the Private_Dependents
13078 -- list on entry to the instance, so they have to be exchanged
13079 -- explicitly now, in order to remain consistent with the view of the
13082 if Ekind_In (Typ, E_Private_Type,
13083 E_Limited_Private_Type,
13084 E_Record_Type_With_Private)
13086 Dep_Elmt := First_Elmt (Private_Dependents (Typ));
13087 while Present (Dep_Elmt) loop
13088 Dep_Typ := Node (Dep_Elmt);
13090 if Scope (Dep_Typ) = Pack_Id
13091 and then Present (Full_View (Dep_Typ))
13093 Replace_Elmt (Dep_Elmt, Full_View (Dep_Typ));
13094 Exchange_Declarations (Dep_Typ);
13097 Next_Elmt (Dep_Elmt);
13101 Exchange_Declarations (Node (M));
13105 if No (Pack_Id) then
13109 -- Make the generic formal parameters private, and make the formal types
13110 -- into subtypes of the actuals again.
13112 E := First_Entity (Pack_Id);
13113 while Present (E) loop
13114 Set_Is_Hidden (E, True);
13117 and then Nkind (Parent (E)) = N_Subtype_Declaration
13119 -- If the actual for E is itself a generic actual type from
13120 -- an enclosing instance, E is still a generic actual type
13121 -- outside of the current instance. This matter when resolving
13122 -- an overloaded call that may be ambiguous in the enclosing
13123 -- instance, when two of its actuals coincide.
13125 if Is_Entity_Name (Subtype_Indication (Parent (E)))
13126 and then Is_Generic_Actual_Type
13127 (Entity (Subtype_Indication (Parent (E))))
13131 Set_Is_Generic_Actual_Type (E, False);
13134 -- An unusual case of aliasing: the actual may also be directly
13135 -- visible in the generic, and be private there, while it is fully
13136 -- visible in the context of the instance. The internal subtype
13137 -- is private in the instance but has full visibility like its
13138 -- parent in the enclosing scope. This enforces the invariant that
13139 -- the privacy status of all private dependents of a type coincide
13140 -- with that of the parent type. This can only happen when a
13141 -- generic child unit is instantiated within a sibling.
13143 if Is_Private_Type (E)
13144 and then not Is_Private_Type (Etype (E))
13146 Exchange_Declarations (E);
13149 elsif Ekind (E) = E_Package then
13151 -- The end of the renaming list is the renaming of the generic
13152 -- package itself. If the instance is a subprogram, all entities
13153 -- in the corresponding package are renamings. If this entity is
13154 -- a formal package, make its own formals private as well. The
13155 -- actual in this case is itself the renaming of an instantiation.
13156 -- If the entity is not a package renaming, it is the entity
13157 -- created to validate formal package actuals: ignore it.
13159 -- If the actual is itself a formal package for the enclosing
13160 -- generic, or the actual for such a formal package, it remains
13161 -- visible on exit from the instance, and therefore nothing needs
13162 -- to be done either, except to keep it accessible.
13164 if Is_Package and then Renamed_Object (E) = Pack_Id then
13167 elsif Nkind (Parent (E)) /= N_Package_Renaming_Declaration then
13171 Denotes_Formal_Package (Renamed_Object (E), True, Pack_Id)
13173 Set_Is_Hidden (E, False);
13177 Act_P : constant Entity_Id := Renamed_Object (E);
13181 Id := First_Entity (Act_P);
13183 and then Id /= First_Private_Entity (Act_P)
13185 exit when Ekind (Id) = E_Package
13186 and then Renamed_Object (Id) = Act_P;
13188 Set_Is_Hidden (Id, True);
13189 Set_Is_Potentially_Use_Visible (Id, In_Use (Act_P));
13191 if Ekind (Id) = E_Package then
13192 Restore_Nested_Formal (Id);
13203 end Restore_Private_Views;
13210 (Gen_Unit : Entity_Id;
13211 Act_Unit : Entity_Id)
13215 Set_Instance_Env (Gen_Unit, Act_Unit);
13218 ----------------------------
13219 -- Save_Global_References --
13220 ----------------------------
13222 procedure Save_Global_References (N : Node_Id) is
13223 Gen_Scope : Entity_Id;
13227 function Is_Global (E : Entity_Id) return Boolean;
13228 -- Check whether entity is defined outside of generic unit. Examine the
13229 -- scope of an entity, and the scope of the scope, etc, until we find
13230 -- either Standard, in which case the entity is global, or the generic
13231 -- unit itself, which indicates that the entity is local. If the entity
13232 -- is the generic unit itself, as in the case of a recursive call, or
13233 -- the enclosing generic unit, if different from the current scope, then
13234 -- it is local as well, because it will be replaced at the point of
13235 -- instantiation. On the other hand, if it is a reference to a child
13236 -- unit of a common ancestor, which appears in an instantiation, it is
13237 -- global because it is used to denote a specific compilation unit at
13238 -- the time the instantiations will be analyzed.
13240 procedure Reset_Entity (N : Node_Id);
13241 -- Save semantic information on global entity so that it is not resolved
13242 -- again at instantiation time.
13244 procedure Save_Entity_Descendants (N : Node_Id);
13245 -- Apply Save_Global_References to the two syntactic descendants of
13246 -- non-terminal nodes that carry an Associated_Node and are processed
13247 -- through Reset_Entity. Once the global entity (if any) has been
13248 -- captured together with its type, only two syntactic descendants need
13249 -- to be traversed to complete the processing of the tree rooted at N.
13250 -- This applies to Selected_Components, Expanded_Names, and to Operator
13251 -- nodes. N can also be a character literal, identifier, or operator
13252 -- symbol node, but the call has no effect in these cases.
13254 procedure Save_Global_Defaults (N1, N2 : Node_Id);
13255 -- Default actuals in nested instances must be handled specially
13256 -- because there is no link to them from the original tree. When an
13257 -- actual subprogram is given by a default, we add an explicit generic
13258 -- association for it in the instantiation node. When we save the
13259 -- global references on the name of the instance, we recover the list
13260 -- of generic associations, and add an explicit one to the original
13261 -- generic tree, through which a global actual can be preserved.
13262 -- Similarly, if a child unit is instantiated within a sibling, in the
13263 -- context of the parent, we must preserve the identifier of the parent
13264 -- so that it can be properly resolved in a subsequent instantiation.
13266 procedure Save_Global_Descendant (D : Union_Id);
13267 -- Apply Save_Global_References recursively to the descendents of the
13270 procedure Save_References (N : Node_Id);
13271 -- This is the recursive procedure that does the work, once the
13272 -- enclosing generic scope has been established.
13278 function Is_Global (E : Entity_Id) return Boolean is
13281 function Is_Instance_Node (Decl : Node_Id) return Boolean;
13282 -- Determine whether the parent node of a reference to a child unit
13283 -- denotes an instantiation or a formal package, in which case the
13284 -- reference to the child unit is global, even if it appears within
13285 -- the current scope (e.g. when the instance appears within the body
13286 -- of an ancestor).
13288 ----------------------
13289 -- Is_Instance_Node --
13290 ----------------------
13292 function Is_Instance_Node (Decl : Node_Id) return Boolean is
13294 return Nkind (Decl) in N_Generic_Instantiation
13296 Nkind (Original_Node (Decl)) = N_Formal_Package_Declaration;
13297 end Is_Instance_Node;
13299 -- Start of processing for Is_Global
13302 if E = Gen_Scope then
13305 elsif E = Standard_Standard then
13308 elsif Is_Child_Unit (E)
13309 and then (Is_Instance_Node (Parent (N2))
13310 or else (Nkind (Parent (N2)) = N_Expanded_Name
13311 and then N2 = Selector_Name (Parent (N2))
13313 Is_Instance_Node (Parent (Parent (N2)))))
13319 while Se /= Gen_Scope loop
13320 if Se = Standard_Standard then
13335 procedure Reset_Entity (N : Node_Id) is
13337 procedure Set_Global_Type (N : Node_Id; N2 : Node_Id);
13338 -- If the type of N2 is global to the generic unit, save the type in
13339 -- the generic node. Just as we perform name capture for explicit
13340 -- references within the generic, we must capture the global types
13341 -- of local entities because they may participate in resolution in
13344 function Top_Ancestor (E : Entity_Id) return Entity_Id;
13345 -- Find the ultimate ancestor of the current unit. If it is not a
13346 -- generic unit, then the name of the current unit in the prefix of
13347 -- an expanded name must be replaced with its generic homonym to
13348 -- ensure that it will be properly resolved in an instance.
13350 ---------------------
13351 -- Set_Global_Type --
13352 ---------------------
13354 procedure Set_Global_Type (N : Node_Id; N2 : Node_Id) is
13355 Typ : constant Entity_Id := Etype (N2);
13358 Set_Etype (N, Typ);
13360 if Entity (N) /= N2
13361 and then Has_Private_View (Entity (N))
13363 -- If the entity of N is not the associated node, this is a
13364 -- nested generic and it has an associated node as well, whose
13365 -- type is already the full view (see below). Indicate that the
13366 -- original node has a private view.
13368 Set_Has_Private_View (N);
13371 -- If not a private type, nothing else to do
13373 if not Is_Private_Type (Typ) then
13374 if Is_Array_Type (Typ)
13375 and then Is_Private_Type (Component_Type (Typ))
13377 Set_Has_Private_View (N);
13380 -- If it is a derivation of a private type in a context where no
13381 -- full view is needed, nothing to do either.
13383 elsif No (Full_View (Typ)) and then Typ /= Etype (Typ) then
13386 -- Otherwise mark the type for flipping and use the full view when
13390 Set_Has_Private_View (N);
13392 if Present (Full_View (Typ)) then
13393 Set_Etype (N2, Full_View (Typ));
13396 end Set_Global_Type;
13402 function Top_Ancestor (E : Entity_Id) return Entity_Id is
13407 while Is_Child_Unit (Par) loop
13408 Par := Scope (Par);
13414 -- Start of processing for Reset_Entity
13417 N2 := Get_Associated_Node (N);
13420 if Present (E) then
13422 -- If the node is an entry call to an entry in an enclosing task,
13423 -- it is rewritten as a selected component. No global entity to
13424 -- preserve in this case, since the expansion will be redone in
13427 if not Nkind_In (E, N_Defining_Identifier,
13428 N_Defining_Character_Literal,
13429 N_Defining_Operator_Symbol)
13431 Set_Associated_Node (N, Empty);
13432 Set_Etype (N, Empty);
13436 -- If the entity is an itype created as a subtype of an access
13437 -- type with a null exclusion restore source entity for proper
13438 -- visibility. The itype will be created anew in the instance.
13441 and then Ekind (E) = E_Access_Subtype
13442 and then Is_Entity_Name (N)
13443 and then Chars (Etype (E)) = Chars (N)
13446 Set_Entity (N2, E);
13450 if Is_Global (E) then
13452 -- If the entity is a package renaming that is the prefix of
13453 -- an expanded name, it has been rewritten as the renamed
13454 -- package, which is necessary semantically but complicates
13455 -- ASIS tree traversal, so we recover the original entity to
13456 -- expose the renaming. Take into account that the context may
13457 -- be a nested generic, that the original node may itself have
13458 -- an associated node that had better be an entity, and that
13459 -- the current node is still a selected component.
13461 if Ekind (E) = E_Package
13462 and then Nkind (N) = N_Selected_Component
13463 and then Nkind (Parent (N)) = N_Expanded_Name
13464 and then Present (Original_Node (N2))
13465 and then Is_Entity_Name (Original_Node (N2))
13466 and then Present (Entity (Original_Node (N2)))
13468 if Is_Global (Entity (Original_Node (N2))) then
13469 N2 := Original_Node (N2);
13470 Set_Associated_Node (N, N2);
13471 Set_Global_Type (N, N2);
13474 -- Renaming is local, and will be resolved in instance
13476 Set_Associated_Node (N, Empty);
13477 Set_Etype (N, Empty);
13481 Set_Global_Type (N, N2);
13484 elsif Nkind (N) = N_Op_Concat
13485 and then Is_Generic_Type (Etype (N2))
13486 and then (Base_Type (Etype (Right_Opnd (N2))) = Etype (N2)
13488 Base_Type (Etype (Left_Opnd (N2))) = Etype (N2))
13489 and then Is_Intrinsic_Subprogram (E)
13494 -- Entity is local. Mark generic node as unresolved.
13495 -- Note that now it does not have an entity.
13497 Set_Associated_Node (N, Empty);
13498 Set_Etype (N, Empty);
13501 if Nkind (Parent (N)) in N_Generic_Instantiation
13502 and then N = Name (Parent (N))
13504 Save_Global_Defaults (Parent (N), Parent (N2));
13507 elsif Nkind (Parent (N)) = N_Selected_Component
13508 and then Nkind (Parent (N2)) = N_Expanded_Name
13510 if Is_Global (Entity (Parent (N2))) then
13511 Change_Selected_Component_To_Expanded_Name (Parent (N));
13512 Set_Associated_Node (Parent (N), Parent (N2));
13513 Set_Global_Type (Parent (N), Parent (N2));
13514 Save_Entity_Descendants (N);
13516 -- If this is a reference to the current generic entity, replace
13517 -- by the name of the generic homonym of the current package. This
13518 -- is because in an instantiation Par.P.Q will not resolve to the
13519 -- name of the instance, whose enclosing scope is not necessarily
13520 -- Par. We use the generic homonym rather that the name of the
13521 -- generic itself because it may be hidden by a local declaration.
13523 elsif In_Open_Scopes (Entity (Parent (N2)))
13525 Is_Generic_Unit (Top_Ancestor (Entity (Prefix (Parent (N2)))))
13527 if Ekind (Entity (Parent (N2))) = E_Generic_Package then
13528 Rewrite (Parent (N),
13529 Make_Identifier (Sloc (N),
13531 Chars (Generic_Homonym (Entity (Parent (N2))))));
13533 Rewrite (Parent (N),
13534 Make_Identifier (Sloc (N),
13535 Chars => Chars (Selector_Name (Parent (N2)))));
13539 if Nkind (Parent (Parent (N))) in N_Generic_Instantiation
13540 and then Parent (N) = Name (Parent (Parent (N)))
13542 Save_Global_Defaults
13543 (Parent (Parent (N)), Parent (Parent ((N2))));
13546 -- A selected component may denote a static constant that has been
13547 -- folded. If the static constant is global to the generic, capture
13548 -- its value. Otherwise the folding will happen in any instantiation.
13550 elsif Nkind (Parent (N)) = N_Selected_Component
13551 and then Nkind_In (Parent (N2), N_Integer_Literal, N_Real_Literal)
13553 if Present (Entity (Original_Node (Parent (N2))))
13554 and then Is_Global (Entity (Original_Node (Parent (N2))))
13556 Rewrite (Parent (N), New_Copy (Parent (N2)));
13557 Set_Analyzed (Parent (N), False);
13563 -- A selected component may be transformed into a parameterless
13564 -- function call. If the called entity is global, rewrite the node
13565 -- appropriately, i.e. as an extended name for the global entity.
13567 elsif Nkind (Parent (N)) = N_Selected_Component
13568 and then Nkind (Parent (N2)) = N_Function_Call
13569 and then N = Selector_Name (Parent (N))
13571 if No (Parameter_Associations (Parent (N2))) then
13572 if Is_Global (Entity (Name (Parent (N2)))) then
13573 Change_Selected_Component_To_Expanded_Name (Parent (N));
13574 Set_Associated_Node (Parent (N), Name (Parent (N2)));
13575 Set_Global_Type (Parent (N), Name (Parent (N2)));
13576 Save_Entity_Descendants (N);
13579 Set_Is_Prefixed_Call (Parent (N));
13580 Set_Associated_Node (N, Empty);
13581 Set_Etype (N, Empty);
13584 -- In Ada 2005, X.F may be a call to a primitive operation,
13585 -- rewritten as F (X). This rewriting will be done again in an
13586 -- instance, so keep the original node. Global entities will be
13587 -- captured as for other constructs. Indicate that this must
13588 -- resolve as a call, to prevent accidental overloading in the
13589 -- instance, if both a component and a primitive operation appear
13593 Set_Is_Prefixed_Call (Parent (N));
13596 -- Entity is local. Reset in generic unit, so that node is resolved
13597 -- anew at the point of instantiation.
13600 Set_Associated_Node (N, Empty);
13601 Set_Etype (N, Empty);
13605 -----------------------------
13606 -- Save_Entity_Descendants --
13607 -----------------------------
13609 procedure Save_Entity_Descendants (N : Node_Id) is
13612 when N_Binary_Op =>
13613 Save_Global_Descendant (Union_Id (Left_Opnd (N)));
13614 Save_Global_Descendant (Union_Id (Right_Opnd (N)));
13617 Save_Global_Descendant (Union_Id (Right_Opnd (N)));
13619 when N_Expanded_Name | N_Selected_Component =>
13620 Save_Global_Descendant (Union_Id (Prefix (N)));
13621 Save_Global_Descendant (Union_Id (Selector_Name (N)));
13623 when N_Identifier | N_Character_Literal | N_Operator_Symbol =>
13627 raise Program_Error;
13629 end Save_Entity_Descendants;
13631 --------------------------
13632 -- Save_Global_Defaults --
13633 --------------------------
13635 procedure Save_Global_Defaults (N1, N2 : Node_Id) is
13636 Loc : constant Source_Ptr := Sloc (N1);
13637 Assoc2 : constant List_Id := Generic_Associations (N2);
13638 Gen_Id : constant Entity_Id := Get_Generic_Entity (N2);
13645 Actual : Entity_Id;
13648 Assoc1 := Generic_Associations (N1);
13650 if Present (Assoc1) then
13651 Act1 := First (Assoc1);
13654 Set_Generic_Associations (N1, New_List);
13655 Assoc1 := Generic_Associations (N1);
13658 if Present (Assoc2) then
13659 Act2 := First (Assoc2);
13664 while Present (Act1) and then Present (Act2) loop
13669 -- Find the associations added for default subprograms
13671 if Present (Act2) then
13672 while Nkind (Act2) /= N_Generic_Association
13673 or else No (Entity (Selector_Name (Act2)))
13674 or else not Is_Overloadable (Entity (Selector_Name (Act2)))
13679 -- Add a similar association if the default is global. The
13680 -- renaming declaration for the actual has been analyzed, and
13681 -- its alias is the program it renames. Link the actual in the
13682 -- original generic tree with the node in the analyzed tree.
13684 while Present (Act2) loop
13685 Subp := Entity (Selector_Name (Act2));
13686 Def := Explicit_Generic_Actual_Parameter (Act2);
13688 -- Following test is defence against rubbish errors
13690 if No (Alias (Subp)) then
13694 -- Retrieve the resolved actual from the renaming declaration
13695 -- created for the instantiated formal.
13697 Actual := Entity (Name (Parent (Parent (Subp))));
13698 Set_Entity (Def, Actual);
13699 Set_Etype (Def, Etype (Actual));
13701 if Is_Global (Actual) then
13703 Make_Generic_Association (Loc,
13704 Selector_Name => New_Occurrence_Of (Subp, Loc),
13705 Explicit_Generic_Actual_Parameter =>
13706 New_Occurrence_Of (Actual, Loc));
13708 Set_Associated_Node
13709 (Explicit_Generic_Actual_Parameter (Ndec), Def);
13711 Append (Ndec, Assoc1);
13713 -- If there are other defaults, add a dummy association in case
13714 -- there are other defaulted formals with the same name.
13716 elsif Present (Next (Act2)) then
13718 Make_Generic_Association (Loc,
13719 Selector_Name => New_Occurrence_Of (Subp, Loc),
13720 Explicit_Generic_Actual_Parameter => Empty);
13722 Append (Ndec, Assoc1);
13729 if Nkind (Name (N1)) = N_Identifier
13730 and then Is_Child_Unit (Gen_Id)
13731 and then Is_Global (Gen_Id)
13732 and then Is_Generic_Unit (Scope (Gen_Id))
13733 and then In_Open_Scopes (Scope (Gen_Id))
13735 -- This is an instantiation of a child unit within a sibling, so
13736 -- that the generic parent is in scope. An eventual instance must
13737 -- occur within the scope of an instance of the parent. Make name
13738 -- in instance into an expanded name, to preserve the identifier
13739 -- of the parent, so it can be resolved subsequently.
13741 Rewrite (Name (N2),
13742 Make_Expanded_Name (Loc,
13743 Chars => Chars (Gen_Id),
13744 Prefix => New_Occurrence_Of (Scope (Gen_Id), Loc),
13745 Selector_Name => New_Occurrence_Of (Gen_Id, Loc)));
13746 Set_Entity (Name (N2), Gen_Id);
13748 Rewrite (Name (N1),
13749 Make_Expanded_Name (Loc,
13750 Chars => Chars (Gen_Id),
13751 Prefix => New_Occurrence_Of (Scope (Gen_Id), Loc),
13752 Selector_Name => New_Occurrence_Of (Gen_Id, Loc)));
13754 Set_Associated_Node (Name (N1), Name (N2));
13755 Set_Associated_Node (Prefix (Name (N1)), Empty);
13756 Set_Associated_Node
13757 (Selector_Name (Name (N1)), Selector_Name (Name (N2)));
13758 Set_Etype (Name (N1), Etype (Gen_Id));
13761 end Save_Global_Defaults;
13763 ----------------------------
13764 -- Save_Global_Descendant --
13765 ----------------------------
13767 procedure Save_Global_Descendant (D : Union_Id) is
13771 if D in Node_Range then
13772 if D = Union_Id (Empty) then
13775 elsif Nkind (Node_Id (D)) /= N_Compilation_Unit then
13776 Save_References (Node_Id (D));
13779 elsif D in List_Range then
13780 if D = Union_Id (No_List)
13781 or else Is_Empty_List (List_Id (D))
13786 N1 := First (List_Id (D));
13787 while Present (N1) loop
13788 Save_References (N1);
13793 -- Element list or other non-node field, nothing to do
13798 end Save_Global_Descendant;
13800 ---------------------
13801 -- Save_References --
13802 ---------------------
13804 -- This is the recursive procedure that does the work once the enclosing
13805 -- generic scope has been established. We have to treat specially a
13806 -- number of node rewritings that are required by semantic processing
13807 -- and which change the kind of nodes in the generic copy: typically
13808 -- constant-folding, replacing an operator node by a string literal, or
13809 -- a selected component by an expanded name. In each of those cases, the
13810 -- transformation is propagated to the generic unit.
13812 procedure Save_References (N : Node_Id) is
13813 Loc : constant Source_Ptr := Sloc (N);
13819 elsif Nkind_In (N, N_Character_Literal, N_Operator_Symbol) then
13820 if Nkind (N) = Nkind (Get_Associated_Node (N)) then
13823 elsif Nkind (N) = N_Operator_Symbol
13824 and then Nkind (Get_Associated_Node (N)) = N_String_Literal
13826 Change_Operator_Symbol_To_String_Literal (N);
13829 elsif Nkind (N) in N_Op then
13830 if Nkind (N) = Nkind (Get_Associated_Node (N)) then
13831 if Nkind (N) = N_Op_Concat then
13832 Set_Is_Component_Left_Opnd (N,
13833 Is_Component_Left_Opnd (Get_Associated_Node (N)));
13835 Set_Is_Component_Right_Opnd (N,
13836 Is_Component_Right_Opnd (Get_Associated_Node (N)));
13842 -- Node may be transformed into call to a user-defined operator
13844 N2 := Get_Associated_Node (N);
13846 if Nkind (N2) = N_Function_Call then
13847 E := Entity (Name (N2));
13850 and then Is_Global (E)
13852 Set_Etype (N, Etype (N2));
13854 Set_Associated_Node (N, Empty);
13855 Set_Etype (N, Empty);
13858 elsif Nkind_In (N2, N_Integer_Literal,
13862 if Present (Original_Node (N2))
13863 and then Nkind (Original_Node (N2)) = Nkind (N)
13866 -- Operation was constant-folded. Whenever possible,
13867 -- recover semantic information from unfolded node,
13870 Set_Associated_Node (N, Original_Node (N2));
13872 if Nkind (N) = N_Op_Concat then
13873 Set_Is_Component_Left_Opnd (N,
13874 Is_Component_Left_Opnd (Get_Associated_Node (N)));
13875 Set_Is_Component_Right_Opnd (N,
13876 Is_Component_Right_Opnd (Get_Associated_Node (N)));
13882 -- If original node is already modified, propagate
13883 -- constant-folding to template.
13885 Rewrite (N, New_Copy (N2));
13886 Set_Analyzed (N, False);
13889 elsif Nkind (N2) = N_Identifier
13890 and then Ekind (Entity (N2)) = E_Enumeration_Literal
13892 -- Same if call was folded into a literal, but in this case
13893 -- retain the entity to avoid spurious ambiguities if it is
13894 -- overloaded at the point of instantiation or inlining.
13896 Rewrite (N, New_Copy (N2));
13897 Set_Analyzed (N, False);
13901 -- Complete operands check if node has not been constant-folded
13903 if Nkind (N) in N_Op then
13904 Save_Entity_Descendants (N);
13907 elsif Nkind (N) = N_Identifier then
13908 if Nkind (N) = Nkind (Get_Associated_Node (N)) then
13910 -- If this is a discriminant reference, always save it. It is
13911 -- used in the instance to find the corresponding discriminant
13912 -- positionally rather than by name.
13914 Set_Original_Discriminant
13915 (N, Original_Discriminant (Get_Associated_Node (N)));
13919 N2 := Get_Associated_Node (N);
13921 if Nkind (N2) = N_Function_Call then
13922 E := Entity (Name (N2));
13924 -- Name resolves to a call to parameterless function. If
13925 -- original entity is global, mark node as resolved.
13928 and then Is_Global (E)
13930 Set_Etype (N, Etype (N2));
13932 Set_Associated_Node (N, Empty);
13933 Set_Etype (N, Empty);
13936 elsif Nkind_In (N2, N_Integer_Literal, N_Real_Literal)
13937 and then Is_Entity_Name (Original_Node (N2))
13939 -- Name resolves to named number that is constant-folded,
13940 -- We must preserve the original name for ASIS use, and
13941 -- undo the constant-folding, which will be repeated in
13944 Set_Associated_Node (N, Original_Node (N2));
13947 elsif Nkind (N2) = N_String_Literal then
13949 -- Name resolves to string literal. Perform the same
13950 -- replacement in generic.
13952 Rewrite (N, New_Copy (N2));
13954 elsif Nkind (N2) = N_Explicit_Dereference then
13956 -- An identifier is rewritten as a dereference if it is the
13957 -- prefix in an implicit dereference (call or attribute).
13958 -- The analysis of an instantiation will expand the node
13959 -- again, so we preserve the original tree but link it to
13960 -- the resolved entity in case it is global.
13962 if Is_Entity_Name (Prefix (N2))
13963 and then Present (Entity (Prefix (N2)))
13964 and then Is_Global (Entity (Prefix (N2)))
13966 Set_Associated_Node (N, Prefix (N2));
13968 elsif Nkind (Prefix (N2)) = N_Function_Call
13969 and then Is_Global (Entity (Name (Prefix (N2))))
13972 Make_Explicit_Dereference (Loc,
13973 Prefix => Make_Function_Call (Loc,
13975 New_Occurrence_Of (Entity (Name (Prefix (N2))),
13979 Set_Associated_Node (N, Empty);
13980 Set_Etype (N, Empty);
13983 -- The subtype mark of a nominally unconstrained object is
13984 -- rewritten as a subtype indication using the bounds of the
13985 -- expression. Recover the original subtype mark.
13987 elsif Nkind (N2) = N_Subtype_Indication
13988 and then Is_Entity_Name (Original_Node (N2))
13990 Set_Associated_Node (N, Original_Node (N2));
13998 elsif Nkind (N) in N_Entity then
14003 Qual : Node_Id := Empty;
14004 Typ : Entity_Id := Empty;
14007 use Atree.Unchecked_Access;
14008 -- This code section is part of implementing an untyped tree
14009 -- traversal, so it needs direct access to node fields.
14012 if Nkind_In (N, N_Aggregate, N_Extension_Aggregate) then
14013 N2 := Get_Associated_Node (N);
14020 -- In an instance within a generic, use the name of the
14021 -- actual and not the original generic parameter. If the
14022 -- actual is global in the current generic it must be
14023 -- preserved for its instantiation.
14025 if Nkind (Parent (Typ)) = N_Subtype_Declaration
14027 Present (Generic_Parent_Type (Parent (Typ)))
14029 Typ := Base_Type (Typ);
14030 Set_Etype (N2, Typ);
14036 or else not Is_Global (Typ)
14038 Set_Associated_Node (N, Empty);
14040 -- If the aggregate is an actual in a call, it has been
14041 -- resolved in the current context, to some local type.
14042 -- The enclosing call may have been disambiguated by the
14043 -- aggregate, and this disambiguation might fail at
14044 -- instantiation time because the type to which the
14045 -- aggregate did resolve is not preserved. In order to
14046 -- preserve some of this information, we wrap the
14047 -- aggregate in a qualified expression, using the id of
14048 -- its type. For further disambiguation we qualify the
14049 -- type name with its scope (if visible) because both
14050 -- id's will have corresponding entities in an instance.
14051 -- This resolves most of the problems with missing type
14052 -- information on aggregates in instances.
14054 if Nkind (N2) = Nkind (N)
14055 and then Nkind (Parent (N2)) in N_Subprogram_Call
14056 and then Comes_From_Source (Typ)
14058 if Is_Immediately_Visible (Scope (Typ)) then
14059 Nam := Make_Selected_Component (Loc,
14061 Make_Identifier (Loc, Chars (Scope (Typ))),
14063 Make_Identifier (Loc, Chars (Typ)));
14065 Nam := Make_Identifier (Loc, Chars (Typ));
14069 Make_Qualified_Expression (Loc,
14070 Subtype_Mark => Nam,
14071 Expression => Relocate_Node (N));
14075 Save_Global_Descendant (Field1 (N));
14076 Save_Global_Descendant (Field2 (N));
14077 Save_Global_Descendant (Field3 (N));
14078 Save_Global_Descendant (Field5 (N));
14080 if Present (Qual) then
14084 -- All other cases than aggregates
14087 Save_Global_Descendant (Field1 (N));
14088 Save_Global_Descendant (Field2 (N));
14089 Save_Global_Descendant (Field3 (N));
14090 Save_Global_Descendant (Field4 (N));
14091 Save_Global_Descendant (Field5 (N));
14096 -- If a node has aspects, references within their expressions must
14097 -- be saved separately, given they are not directly in the tree.
14099 if Has_Aspects (N) then
14104 Aspect := First (Aspect_Specifications (N));
14105 while Present (Aspect) loop
14106 if Present (Expression (Aspect)) then
14107 Save_Global_References (Expression (Aspect));
14114 end Save_References;
14116 -- Start of processing for Save_Global_References
14119 Gen_Scope := Current_Scope;
14121 -- If the generic unit is a child unit, references to entities in the
14122 -- parent are treated as local, because they will be resolved anew in
14123 -- the context of the instance of the parent.
14125 while Is_Child_Unit (Gen_Scope)
14126 and then Ekind (Scope (Gen_Scope)) = E_Generic_Package
14128 Gen_Scope := Scope (Gen_Scope);
14131 Save_References (N);
14132 end Save_Global_References;
14134 --------------------------------------
14135 -- Set_Copied_Sloc_For_Inlined_Body --
14136 --------------------------------------
14138 procedure Set_Copied_Sloc_For_Inlined_Body (N : Node_Id; E : Entity_Id) is
14140 Create_Instantiation_Source (N, E, True, S_Adjustment);
14141 end Set_Copied_Sloc_For_Inlined_Body;
14143 ---------------------
14144 -- Set_Instance_Of --
14145 ---------------------
14147 procedure Set_Instance_Of (A : Entity_Id; B : Entity_Id) is
14149 Generic_Renamings.Table (Generic_Renamings.Last) := (A, B, Assoc_Null);
14150 Generic_Renamings_HTable.Set (Generic_Renamings.Last);
14151 Generic_Renamings.Increment_Last;
14152 end Set_Instance_Of;
14154 --------------------
14155 -- Set_Next_Assoc --
14156 --------------------
14158 procedure Set_Next_Assoc (E : Assoc_Ptr; Next : Assoc_Ptr) is
14160 Generic_Renamings.Table (E).Next_In_HTable := Next;
14161 end Set_Next_Assoc;
14163 -------------------
14164 -- Start_Generic --
14165 -------------------
14167 procedure Start_Generic is
14169 -- ??? More things could be factored out in this routine.
14170 -- Should probably be done at a later stage.
14172 Generic_Flags.Append (Inside_A_Generic);
14173 Inside_A_Generic := True;
14175 Expander_Mode_Save_And_Set (False);
14178 ----------------------
14179 -- Set_Instance_Env --
14180 ----------------------
14182 procedure Set_Instance_Env
14183 (Gen_Unit : Entity_Id;
14184 Act_Unit : Entity_Id)
14186 Assertion_Status : constant Boolean := Assertions_Enabled;
14187 Save_SPARK_Mode : constant SPARK_Mode_Type := SPARK_Mode;
14188 Save_SPARK_Mode_Pragma : constant Node_Id := SPARK_Mode_Pragma;
14191 -- Regardless of the current mode, predefined units are analyzed in the
14192 -- most current Ada mode, and earlier version Ada checks do not apply
14193 -- to predefined units. Nothing needs to be done for non-internal units.
14194 -- These are always analyzed in the current mode.
14196 if Is_Internal_File_Name
14197 (Fname => Unit_File_Name (Get_Source_Unit (Gen_Unit)),
14198 Renamings_Included => True)
14200 Set_Opt_Config_Switches (True, Current_Sem_Unit = Main_Unit);
14202 -- In Ada2012 we may want to enable assertions in an instance of a
14203 -- predefined unit, in which case we need to preserve the current
14204 -- setting for the Assertions_Enabled flag. This will become more
14205 -- critical when pre/postconditions are added to predefined units,
14206 -- as is already the case for some numeric libraries.
14208 if Ada_Version >= Ada_2012 then
14209 Assertions_Enabled := Assertion_Status;
14212 -- SPARK_Mode for an instance is the one applicable at the point of
14215 SPARK_Mode := Save_SPARK_Mode;
14216 SPARK_Mode_Pragma := Save_SPARK_Mode_Pragma;
14219 Current_Instantiated_Parent :=
14220 (Gen_Id => Gen_Unit,
14221 Act_Id => Act_Unit,
14222 Next_In_HTable => Assoc_Null);
14223 end Set_Instance_Env;
14229 procedure Switch_View (T : Entity_Id) is
14230 BT : constant Entity_Id := Base_Type (T);
14231 Priv_Elmt : Elmt_Id := No_Elmt;
14232 Priv_Sub : Entity_Id;
14235 -- T may be private but its base type may have been exchanged through
14236 -- some other occurrence, in which case there is nothing to switch
14237 -- besides T itself. Note that a private dependent subtype of a private
14238 -- type might not have been switched even if the base type has been,
14239 -- because of the last branch of Check_Private_View (see comment there).
14241 if not Is_Private_Type (BT) then
14242 Prepend_Elmt (Full_View (T), Exchanged_Views);
14243 Exchange_Declarations (T);
14247 Priv_Elmt := First_Elmt (Private_Dependents (BT));
14249 if Present (Full_View (BT)) then
14250 Prepend_Elmt (Full_View (BT), Exchanged_Views);
14251 Exchange_Declarations (BT);
14254 while Present (Priv_Elmt) loop
14255 Priv_Sub := (Node (Priv_Elmt));
14257 -- We avoid flipping the subtype if the Etype of its full view is
14258 -- private because this would result in a malformed subtype. This
14259 -- occurs when the Etype of the subtype full view is the full view of
14260 -- the base type (and since the base types were just switched, the
14261 -- subtype is pointing to the wrong view). This is currently the case
14262 -- for tagged record types, access types (maybe more?) and needs to
14263 -- be resolved. ???
14265 if Present (Full_View (Priv_Sub))
14266 and then not Is_Private_Type (Etype (Full_View (Priv_Sub)))
14268 Prepend_Elmt (Full_View (Priv_Sub), Exchanged_Views);
14269 Exchange_Declarations (Priv_Sub);
14272 Next_Elmt (Priv_Elmt);
14280 function True_Parent (N : Node_Id) return Node_Id is
14282 if Nkind (Parent (N)) = N_Subunit then
14283 return Parent (Corresponding_Stub (Parent (N)));
14289 -----------------------------
14290 -- Valid_Default_Attribute --
14291 -----------------------------
14293 procedure Valid_Default_Attribute (Nam : Entity_Id; Def : Node_Id) is
14294 Attr_Id : constant Attribute_Id :=
14295 Get_Attribute_Id (Attribute_Name (Def));
14296 T : constant Entity_Id := Entity (Prefix (Def));
14297 Is_Fun : constant Boolean := (Ekind (Nam) = E_Function);
14310 F := First_Formal (Nam);
14311 while Present (F) loop
14312 Num_F := Num_F + 1;
14317 when Attribute_Adjacent | Attribute_Ceiling | Attribute_Copy_Sign |
14318 Attribute_Floor | Attribute_Fraction | Attribute_Machine |
14319 Attribute_Model | Attribute_Remainder | Attribute_Rounding |
14320 Attribute_Unbiased_Rounding =>
14323 and then Is_Floating_Point_Type (T);
14325 when Attribute_Image | Attribute_Pred | Attribute_Succ |
14326 Attribute_Value | Attribute_Wide_Image |
14327 Attribute_Wide_Value =>
14328 OK := (Is_Fun and then Num_F = 1 and then Is_Scalar_Type (T));
14330 when Attribute_Max | Attribute_Min =>
14331 OK := (Is_Fun and then Num_F = 2 and then Is_Scalar_Type (T));
14333 when Attribute_Input =>
14334 OK := (Is_Fun and then Num_F = 1);
14336 when Attribute_Output | Attribute_Read | Attribute_Write =>
14337 OK := (not Is_Fun and then Num_F = 2);
14344 Error_Msg_N ("attribute reference has wrong profile for subprogram",
14347 end Valid_Default_Attribute;