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 procedure Check_Overloaded_Formal_Subprogram (Formal : Entity_Id);
957 -- Apply RM 12.3 (9): if a formal subprogram is overloaded, the instance
958 -- cannot have a named association for it. AI05-0025 extends this rule
959 -- to formals of formal packages by AI05-0025, and it also applies to
960 -- box-initialized formals.
962 function Has_Fully_Defined_Profile (Subp : Entity_Id) return Boolean;
963 -- Determine whether the parameter types and the return type of Subp
964 -- are fully defined at the point of instantiation.
966 function Matching_Actual
968 A_F : Entity_Id) return Node_Id;
969 -- Find actual that corresponds to a given a formal parameter. If the
970 -- actuals are positional, return the next one, if any. If the actuals
971 -- are named, scan the parameter associations to find the right one.
972 -- A_F is the corresponding entity in the analyzed generic,which is
973 -- placed on the selector name for ASIS use.
975 -- In Ada 2005, a named association may be given with a box, in which
976 -- case Matching_Actual sets Found_Assoc to the generic association,
977 -- but return Empty for the actual itself. In this case the code below
978 -- creates a corresponding declaration for the formal.
980 function Partial_Parameterization return Boolean;
981 -- Ada 2005: if no match is found for a given formal, check if the
982 -- association for it includes a box, or whether the associations
983 -- include an Others clause.
985 procedure Process_Default (F : Entity_Id);
986 -- Add a copy of the declaration of generic formal F to the list of
987 -- associations, and add an explicit box association for F if there
988 -- is none yet, and the default comes from an Others_Choice.
990 function Renames_Standard_Subprogram (Subp : Entity_Id) return Boolean;
991 -- Determine whether Subp renames one of the subprograms defined in the
992 -- generated package Standard.
994 procedure Set_Analyzed_Formal;
995 -- Find the node in the generic copy that corresponds to a given formal.
996 -- The semantic information on this node is used to perform legality
997 -- checks on the actuals. Because semantic analysis can introduce some
998 -- anonymous entities or modify the declaration node itself, the
999 -- correspondence between the two lists is not one-one. In addition to
1000 -- anonymous types, the presence a formal equality will introduce an
1001 -- implicit declaration for the corresponding inequality.
1003 ----------------------------------------
1004 -- Check_Overloaded_Formal_Subprogram --
1005 ----------------------------------------
1007 procedure Check_Overloaded_Formal_Subprogram (Formal : Entity_Id) is
1008 Temp_Formal : Entity_Id;
1011 Temp_Formal := First (Formals);
1012 while Present (Temp_Formal) loop
1013 if Nkind (Temp_Formal) in N_Formal_Subprogram_Declaration
1014 and then Temp_Formal /= Formal
1016 Chars (Defining_Unit_Name (Specification (Formal))) =
1017 Chars (Defining_Unit_Name (Specification (Temp_Formal)))
1019 if Present (Found_Assoc) then
1021 ("named association not allowed for overloaded formal",
1026 ("named association not allowed for overloaded formal",
1030 Abandon_Instantiation (Instantiation_Node);
1035 end Check_Overloaded_Formal_Subprogram;
1037 -------------------------------
1038 -- Has_Fully_Defined_Profile --
1039 -------------------------------
1041 function Has_Fully_Defined_Profile (Subp : Entity_Id) return Boolean is
1042 function Is_Fully_Defined_Type (Typ : Entity_Id) return Boolean;
1043 -- Determine whethet type Typ is fully defined
1045 ---------------------------
1046 -- Is_Fully_Defined_Type --
1047 ---------------------------
1049 function Is_Fully_Defined_Type (Typ : Entity_Id) return Boolean is
1051 -- A private type without a full view is not fully defined
1053 if Is_Private_Type (Typ)
1054 and then No (Full_View (Typ))
1058 -- An incomplete type is never fully defined
1060 elsif Is_Incomplete_Type (Typ) then
1063 -- All other types are fully defined
1068 end Is_Fully_Defined_Type;
1070 -- Local declarations
1074 -- Start of processing for Has_Fully_Defined_Profile
1077 -- Check the parameters
1079 Param := First_Formal (Subp);
1080 while Present (Param) loop
1081 if not Is_Fully_Defined_Type (Etype (Param)) then
1085 Next_Formal (Param);
1088 -- Check the return type
1090 return Is_Fully_Defined_Type (Etype (Subp));
1091 end Has_Fully_Defined_Profile;
1093 ---------------------
1094 -- Matching_Actual --
1095 ---------------------
1097 function Matching_Actual
1099 A_F : Entity_Id) return Node_Id
1105 Is_Named_Assoc := False;
1107 -- End of list of purely positional parameters
1109 if No (Actual) or else Nkind (Actual) = N_Others_Choice then
1110 Found_Assoc := Empty;
1113 -- Case of positional parameter corresponding to current formal
1115 elsif No (Selector_Name (Actual)) then
1116 Found_Assoc := Actual;
1117 Act := Explicit_Generic_Actual_Parameter (Actual);
1118 Num_Matched := Num_Matched + 1;
1121 -- Otherwise scan list of named actuals to find the one with the
1122 -- desired name. All remaining actuals have explicit names.
1125 Is_Named_Assoc := True;
1126 Found_Assoc := Empty;
1130 while Present (Actual) loop
1131 if Chars (Selector_Name (Actual)) = Chars (F) then
1132 Set_Entity (Selector_Name (Actual), A_F);
1133 Set_Etype (Selector_Name (Actual), Etype (A_F));
1134 Generate_Reference (A_F, Selector_Name (Actual));
1135 Found_Assoc := Actual;
1136 Act := Explicit_Generic_Actual_Parameter (Actual);
1137 Num_Matched := Num_Matched + 1;
1145 -- Reset for subsequent searches. In most cases the named
1146 -- associations are in order. If they are not, we reorder them
1147 -- to avoid scanning twice the same actual. This is not just a
1148 -- question of efficiency: there may be multiple defaults with
1149 -- boxes that have the same name. In a nested instantiation we
1150 -- insert actuals for those defaults, and cannot rely on their
1151 -- names to disambiguate them.
1153 if Actual = First_Named then
1156 elsif Present (Actual) then
1157 Insert_Before (First_Named, Remove_Next (Prev));
1160 Actual := First_Named;
1163 if Is_Entity_Name (Act) and then Present (Entity (Act)) then
1164 Set_Used_As_Generic_Actual (Entity (Act));
1168 end Matching_Actual;
1170 ------------------------------
1171 -- Partial_Parameterization --
1172 ------------------------------
1174 function Partial_Parameterization return Boolean is
1176 return Others_Present
1177 or else (Present (Found_Assoc) and then Box_Present (Found_Assoc));
1178 end Partial_Parameterization;
1180 ---------------------
1181 -- Process_Default --
1182 ---------------------
1184 procedure Process_Default (F : Entity_Id) is
1185 Loc : constant Source_Ptr := Sloc (I_Node);
1186 F_Id : constant Entity_Id := Defining_Entity (F);
1192 -- Append copy of formal declaration to associations, and create new
1193 -- defining identifier for it.
1195 Decl := New_Copy_Tree (F);
1196 Id := Make_Defining_Identifier (Sloc (F_Id), Chars (F_Id));
1198 if Nkind (F) in N_Formal_Subprogram_Declaration then
1199 Set_Defining_Unit_Name (Specification (Decl), Id);
1202 Set_Defining_Identifier (Decl, Id);
1205 Append (Decl, Assoc);
1207 if No (Found_Assoc) then
1209 Make_Generic_Association (Loc,
1211 New_Occurrence_Of (Id, Loc),
1212 Explicit_Generic_Actual_Parameter => Empty);
1213 Set_Box_Present (Default);
1214 Append (Default, Default_Formals);
1216 end Process_Default;
1218 ---------------------------------
1219 -- Renames_Standard_Subprogram --
1220 ---------------------------------
1222 function Renames_Standard_Subprogram (Subp : Entity_Id) return Boolean is
1227 while Present (Id) loop
1228 if Scope (Id) = Standard_Standard then
1236 end Renames_Standard_Subprogram;
1238 -------------------------
1239 -- Set_Analyzed_Formal --
1240 -------------------------
1242 procedure Set_Analyzed_Formal is
1246 while Present (Analyzed_Formal) loop
1247 Kind := Nkind (Analyzed_Formal);
1249 case Nkind (Formal) is
1251 when N_Formal_Subprogram_Declaration =>
1252 exit when Kind in N_Formal_Subprogram_Declaration
1255 (Defining_Unit_Name (Specification (Formal))) =
1257 (Defining_Unit_Name (Specification (Analyzed_Formal)));
1259 when N_Formal_Package_Declaration =>
1260 exit when Nkind_In (Kind, N_Formal_Package_Declaration,
1261 N_Generic_Package_Declaration,
1262 N_Package_Declaration);
1264 when N_Use_Package_Clause | N_Use_Type_Clause => exit;
1268 -- Skip freeze nodes, and nodes inserted to replace
1269 -- unrecognized pragmas.
1272 Kind not in N_Formal_Subprogram_Declaration
1273 and then not Nkind_In (Kind, N_Subprogram_Declaration,
1277 and then Chars (Defining_Identifier (Formal)) =
1278 Chars (Defining_Identifier (Analyzed_Formal));
1281 Next (Analyzed_Formal);
1283 end Set_Analyzed_Formal;
1285 -- Start of processing for Analyze_Associations
1288 Actuals := Generic_Associations (I_Node);
1290 if Present (Actuals) then
1292 -- Check for an Others choice, indicating a partial parameterization
1293 -- for a formal package.
1295 Actual := First (Actuals);
1296 while Present (Actual) loop
1297 if Nkind (Actual) = N_Others_Choice then
1298 Others_Present := True;
1299 Others_Choice := Actual;
1301 if Present (Next (Actual)) then
1302 Error_Msg_N ("others must be last association", Actual);
1305 -- This subprogram is used both for formal packages and for
1306 -- instantiations. For the latter, associations must all be
1309 if Nkind (I_Node) /= N_Formal_Package_Declaration
1310 and then Comes_From_Source (I_Node)
1313 ("others association not allowed in an instance",
1317 -- In any case, nothing to do after the others association
1321 elsif Box_Present (Actual)
1322 and then Comes_From_Source (I_Node)
1323 and then Nkind (I_Node) /= N_Formal_Package_Declaration
1326 ("box association not allowed in an instance", Actual);
1332 -- If named associations are present, save first named association
1333 -- (it may of course be Empty) to facilitate subsequent name search.
1335 First_Named := First (Actuals);
1336 while Present (First_Named)
1337 and then Nkind (First_Named) /= N_Others_Choice
1338 and then No (Selector_Name (First_Named))
1340 Num_Actuals := Num_Actuals + 1;
1345 Named := First_Named;
1346 while Present (Named) loop
1347 if Nkind (Named) /= N_Others_Choice
1348 and then No (Selector_Name (Named))
1350 Error_Msg_N ("invalid positional actual after named one", Named);
1351 Abandon_Instantiation (Named);
1354 -- A named association may lack an actual parameter, if it was
1355 -- introduced for a default subprogram that turns out to be local
1356 -- to the outer instantiation.
1358 if Nkind (Named) /= N_Others_Choice
1359 and then Present (Explicit_Generic_Actual_Parameter (Named))
1361 Num_Actuals := Num_Actuals + 1;
1367 if Present (Formals) then
1368 Formal := First_Non_Pragma (Formals);
1369 Analyzed_Formal := First_Non_Pragma (F_Copy);
1371 if Present (Actuals) then
1372 Actual := First (Actuals);
1374 -- All formals should have default values
1380 while Present (Formal) loop
1381 Set_Analyzed_Formal;
1382 Saved_Formal := Next_Non_Pragma (Formal);
1384 case Nkind (Formal) is
1385 when N_Formal_Object_Declaration =>
1388 Defining_Identifier (Formal),
1389 Defining_Identifier (Analyzed_Formal));
1391 if No (Match) and then Partial_Parameterization then
1392 Process_Default (Formal);
1395 (Instantiate_Object (Formal, Match, Analyzed_Formal),
1399 -- If the object is a call to an expression function, this
1400 -- is a freezing point for it.
1402 if Is_Entity_Name (Match)
1403 and then Present (Entity (Match))
1405 (Original_Node (Unit_Declaration_Node (Entity (Match))))
1406 = N_Expression_Function
1408 Append_Elmt (Entity (Match), Actuals_To_Freeze);
1411 when N_Formal_Type_Declaration =>
1414 Defining_Identifier (Formal),
1415 Defining_Identifier (Analyzed_Formal));
1418 if Partial_Parameterization then
1419 Process_Default (Formal);
1422 Error_Msg_Sloc := Sloc (Gen_Unit);
1425 Instantiation_Node, Defining_Identifier (Formal));
1427 ("\in instantiation of & declared#",
1428 Instantiation_Node, Gen_Unit);
1429 Abandon_Instantiation (Instantiation_Node);
1436 (Formal, Match, Analyzed_Formal, Assoc),
1439 -- An instantiation is a freeze point for the actuals,
1440 -- unless this is a rewritten formal package, or the
1441 -- formal is an Ada 2012 formal incomplete type.
1443 if Nkind (I_Node) = N_Formal_Package_Declaration
1445 (Ada_Version >= Ada_2012
1447 Ekind (Defining_Identifier (Analyzed_Formal)) =
1453 Append_Elmt (Entity (Match), Actuals_To_Freeze);
1457 -- A remote access-to-class-wide type is not a legal actual
1458 -- for a generic formal of an access type (E.2.2(17/2)).
1459 -- In GNAT an exception to this rule is introduced when
1460 -- the formal is marked as remote using implementation
1461 -- defined aspect/pragma Remote_Access_Type. In that case
1462 -- the actual must be remote as well.
1464 -- If the current instantiation is the construction of a
1465 -- local copy for a formal package the actuals may be
1466 -- defaulted, and there is no matching actual to check.
1468 if Nkind (Analyzed_Formal) = N_Formal_Type_Declaration
1470 Nkind (Formal_Type_Definition (Analyzed_Formal)) =
1471 N_Access_To_Object_Definition
1472 and then Present (Match)
1475 Formal_Ent : constant Entity_Id :=
1476 Defining_Identifier (Analyzed_Formal);
1478 if Is_Remote_Access_To_Class_Wide_Type (Entity (Match))
1479 = Is_Remote_Types (Formal_Ent)
1481 -- Remoteness of formal and actual match
1485 elsif Is_Remote_Types (Formal_Ent) then
1487 -- Remote formal, non-remote actual
1490 ("actual for& must be remote", Match, Formal_Ent);
1493 -- Non-remote formal, remote actual
1496 ("actual for& may not be remote",
1502 when N_Formal_Subprogram_Declaration =>
1505 (Defining_Unit_Name (Specification (Formal)),
1506 Defining_Unit_Name (Specification (Analyzed_Formal)));
1508 -- If the formal subprogram has the same name as another
1509 -- formal subprogram of the generic, then a named
1510 -- association is illegal (12.3(9)). Exclude named
1511 -- associations that are generated for a nested instance.
1514 and then Is_Named_Assoc
1515 and then Comes_From_Source (Found_Assoc)
1517 Check_Overloaded_Formal_Subprogram (Formal);
1520 -- If there is no corresponding actual, this may be case
1521 -- of partial parameterization, or else the formal has a
1522 -- default or a box.
1524 if No (Match) and then Partial_Parameterization then
1525 Process_Default (Formal);
1527 if Nkind (I_Node) = N_Formal_Package_Declaration then
1528 Check_Overloaded_Formal_Subprogram (Formal);
1533 Instantiate_Formal_Subprogram
1534 (Formal, Match, Analyzed_Formal));
1536 -- An instantiation is a freeze point for the actuals,
1537 -- unless this is a rewritten formal package.
1539 if Nkind (I_Node) /= N_Formal_Package_Declaration
1540 and then Nkind (Match) = N_Identifier
1541 and then Is_Subprogram (Entity (Match))
1543 -- The actual subprogram may rename a routine defined
1544 -- in Standard. Avoid freezing such renamings because
1545 -- subprograms coming from Standard cannot be frozen.
1548 not Renames_Standard_Subprogram (Entity (Match))
1550 -- If the actual subprogram comes from a different
1551 -- unit, it is already frozen, either by a body in
1552 -- that unit or by the end of the declarative part
1553 -- of the unit. This check avoids the freezing of
1554 -- subprograms defined in Standard which are used
1555 -- as generic actuals.
1557 and then In_Same_Code_Unit (Entity (Match), I_Node)
1558 and then Has_Fully_Defined_Profile (Entity (Match))
1560 -- Mark the subprogram as having a delayed freeze
1561 -- since this may be an out-of-order action.
1563 Set_Has_Delayed_Freeze (Entity (Match));
1564 Append_Elmt (Entity (Match), Actuals_To_Freeze);
1568 -- If this is a nested generic, preserve default for later
1571 if No (Match) and then Box_Present (Formal) then
1573 (Defining_Unit_Name (Specification (Last (Assoc))),
1577 when N_Formal_Package_Declaration =>
1580 (Defining_Identifier (Formal),
1581 Defining_Identifier (Original_Node (Analyzed_Formal)));
1584 if Partial_Parameterization then
1585 Process_Default (Formal);
1588 Error_Msg_Sloc := Sloc (Gen_Unit);
1591 Instantiation_Node, Defining_Identifier (Formal));
1593 ("\in instantiation of & declared#",
1594 Instantiation_Node, Gen_Unit);
1596 Abandon_Instantiation (Instantiation_Node);
1602 (Instantiate_Formal_Package
1603 (Formal, Match, Analyzed_Formal),
1607 -- For use type and use package appearing in the generic part,
1608 -- we have already copied them, so we can just move them where
1609 -- they belong (we mustn't recopy them since this would mess up
1610 -- the Sloc values).
1612 when N_Use_Package_Clause |
1613 N_Use_Type_Clause =>
1614 if Nkind (Original_Node (I_Node)) =
1615 N_Formal_Package_Declaration
1617 Append (New_Copy_Tree (Formal), Assoc);
1620 Append (Formal, Assoc);
1624 raise Program_Error;
1628 Formal := Saved_Formal;
1629 Next_Non_Pragma (Analyzed_Formal);
1632 if Num_Actuals > Num_Matched then
1633 Error_Msg_Sloc := Sloc (Gen_Unit);
1635 if Present (Selector_Name (Actual)) then
1637 ("unmatched actual &", Actual, Selector_Name (Actual));
1639 ("\in instantiation of & declared#", Actual, Gen_Unit);
1642 ("unmatched actual in instantiation of & declared#",
1647 elsif Present (Actuals) then
1649 ("too many actuals in generic instantiation", Instantiation_Node);
1652 -- An instantiation freezes all generic actuals. The only exceptions
1653 -- to this are incomplete types and subprograms which are not fully
1654 -- defined at the point of instantiation.
1657 Elmt : Elmt_Id := First_Elmt (Actuals_To_Freeze);
1659 while Present (Elmt) loop
1660 Freeze_Before (I_Node, Node (Elmt));
1665 -- If there are default subprograms, normalize the tree by adding
1666 -- explicit associations for them. This is required if the instance
1667 -- appears within a generic.
1675 Elmt := First_Elmt (Default_Actuals);
1676 while Present (Elmt) loop
1677 if No (Actuals) then
1678 Actuals := New_List;
1679 Set_Generic_Associations (I_Node, Actuals);
1682 Subp := Node (Elmt);
1684 Make_Generic_Association (Sloc (Subp),
1686 New_Occurrence_Of (Subp, Sloc (Subp)),
1687 Explicit_Generic_Actual_Parameter =>
1688 New_Occurrence_Of (Subp, Sloc (Subp)));
1689 Mark_Rewrite_Insertion (New_D);
1690 Append_To (Actuals, New_D);
1695 -- If this is a formal package, normalize the parameter list by adding
1696 -- explicit box associations for the formals that are covered by an
1699 if not Is_Empty_List (Default_Formals) then
1700 Append_List (Default_Formals, Formals);
1704 end Analyze_Associations;
1706 -------------------------------
1707 -- Analyze_Formal_Array_Type --
1708 -------------------------------
1710 procedure Analyze_Formal_Array_Type
1711 (T : in out Entity_Id;
1717 -- Treated like a non-generic array declaration, with additional
1722 if Nkind (Def) = N_Constrained_Array_Definition then
1723 DSS := First (Discrete_Subtype_Definitions (Def));
1724 while Present (DSS) loop
1725 if Nkind_In (DSS, N_Subtype_Indication,
1727 N_Attribute_Reference)
1729 Error_Msg_N ("only a subtype mark is allowed in a formal", DSS);
1736 Array_Type_Declaration (T, Def);
1737 Set_Is_Generic_Type (Base_Type (T));
1739 if Ekind (Component_Type (T)) = E_Incomplete_Type
1740 and then No (Full_View (Component_Type (T)))
1742 Error_Msg_N ("premature usage of incomplete type", Def);
1744 -- Check that range constraint is not allowed on the component type
1745 -- of a generic formal array type (AARM 12.5.3(3))
1747 elsif Is_Internal (Component_Type (T))
1748 and then Present (Subtype_Indication (Component_Definition (Def)))
1749 and then Nkind (Original_Node
1750 (Subtype_Indication (Component_Definition (Def)))) =
1751 N_Subtype_Indication
1754 ("in a formal, a subtype indication can only be "
1755 & "a subtype mark (RM 12.5.3(3))",
1756 Subtype_Indication (Component_Definition (Def)));
1759 end Analyze_Formal_Array_Type;
1761 ---------------------------------------------
1762 -- Analyze_Formal_Decimal_Fixed_Point_Type --
1763 ---------------------------------------------
1765 -- As for other generic types, we create a valid type representation with
1766 -- legal but arbitrary attributes, whose values are never considered
1767 -- static. For all scalar types we introduce an anonymous base type, with
1768 -- the same attributes. We choose the corresponding integer type to be
1769 -- Standard_Integer.
1770 -- Here and in other similar routines, the Sloc of the generated internal
1771 -- type must be the same as the sloc of the defining identifier of the
1772 -- formal type declaration, to provide proper source navigation.
1774 procedure Analyze_Formal_Decimal_Fixed_Point_Type
1778 Loc : constant Source_Ptr := Sloc (Def);
1780 Base : constant Entity_Id :=
1782 (E_Decimal_Fixed_Point_Type,
1784 Sloc (Defining_Identifier (Parent (Def))), 'G');
1786 Int_Base : constant Entity_Id := Standard_Integer;
1787 Delta_Val : constant Ureal := Ureal_1;
1788 Digs_Val : constant Uint := Uint_6;
1790 function Make_Dummy_Bound return Node_Id;
1791 -- Return a properly typed universal real literal to use as a bound
1793 ----------------------
1794 -- Make_Dummy_Bound --
1795 ----------------------
1797 function Make_Dummy_Bound return Node_Id is
1798 Bound : constant Node_Id := Make_Real_Literal (Loc, Ureal_1);
1800 Set_Etype (Bound, Universal_Real);
1802 end Make_Dummy_Bound;
1804 -- Start of processing for Analyze_Formal_Decimal_Fixed_Point_Type
1809 Set_Etype (Base, Base);
1810 Set_Size_Info (Base, Int_Base);
1811 Set_RM_Size (Base, RM_Size (Int_Base));
1812 Set_First_Rep_Item (Base, First_Rep_Item (Int_Base));
1813 Set_Digits_Value (Base, Digs_Val);
1814 Set_Delta_Value (Base, Delta_Val);
1815 Set_Small_Value (Base, Delta_Val);
1816 Set_Scalar_Range (Base,
1818 Low_Bound => Make_Dummy_Bound,
1819 High_Bound => Make_Dummy_Bound));
1821 Set_Is_Generic_Type (Base);
1822 Set_Parent (Base, Parent (Def));
1824 Set_Ekind (T, E_Decimal_Fixed_Point_Subtype);
1825 Set_Etype (T, Base);
1826 Set_Size_Info (T, Int_Base);
1827 Set_RM_Size (T, RM_Size (Int_Base));
1828 Set_First_Rep_Item (T, First_Rep_Item (Int_Base));
1829 Set_Digits_Value (T, Digs_Val);
1830 Set_Delta_Value (T, Delta_Val);
1831 Set_Small_Value (T, Delta_Val);
1832 Set_Scalar_Range (T, Scalar_Range (Base));
1833 Set_Is_Constrained (T);
1835 Check_Restriction (No_Fixed_Point, Def);
1836 end Analyze_Formal_Decimal_Fixed_Point_Type;
1838 -------------------------------------------
1839 -- Analyze_Formal_Derived_Interface_Type --
1840 -------------------------------------------
1842 procedure Analyze_Formal_Derived_Interface_Type
1847 Loc : constant Source_Ptr := Sloc (Def);
1850 -- Rewrite as a type declaration of a derived type. This ensures that
1851 -- the interface list and primitive operations are properly captured.
1854 Make_Full_Type_Declaration (Loc,
1855 Defining_Identifier => T,
1856 Type_Definition => Def));
1858 Set_Is_Generic_Type (T);
1859 end Analyze_Formal_Derived_Interface_Type;
1861 ---------------------------------
1862 -- Analyze_Formal_Derived_Type --
1863 ---------------------------------
1865 procedure Analyze_Formal_Derived_Type
1870 Loc : constant Source_Ptr := Sloc (Def);
1871 Unk_Disc : constant Boolean := Unknown_Discriminants_Present (N);
1875 Set_Is_Generic_Type (T);
1877 if Private_Present (Def) then
1879 Make_Private_Extension_Declaration (Loc,
1880 Defining_Identifier => T,
1881 Discriminant_Specifications => Discriminant_Specifications (N),
1882 Unknown_Discriminants_Present => Unk_Disc,
1883 Subtype_Indication => Subtype_Mark (Def),
1884 Interface_List => Interface_List (Def));
1886 Set_Abstract_Present (New_N, Abstract_Present (Def));
1887 Set_Limited_Present (New_N, Limited_Present (Def));
1888 Set_Synchronized_Present (New_N, Synchronized_Present (Def));
1892 Make_Full_Type_Declaration (Loc,
1893 Defining_Identifier => T,
1894 Discriminant_Specifications =>
1895 Discriminant_Specifications (Parent (T)),
1897 Make_Derived_Type_Definition (Loc,
1898 Subtype_Indication => Subtype_Mark (Def)));
1900 Set_Abstract_Present
1901 (Type_Definition (New_N), Abstract_Present (Def));
1903 (Type_Definition (New_N), Limited_Present (Def));
1910 if not Is_Composite_Type (T) then
1912 ("unknown discriminants not allowed for elementary types", N);
1914 Set_Has_Unknown_Discriminants (T);
1915 Set_Is_Constrained (T, False);
1919 -- If the parent type has a known size, so does the formal, which makes
1920 -- legal representation clauses that involve the formal.
1922 Set_Size_Known_At_Compile_Time
1923 (T, Size_Known_At_Compile_Time (Entity (Subtype_Mark (Def))));
1924 end Analyze_Formal_Derived_Type;
1926 ----------------------------------
1927 -- Analyze_Formal_Discrete_Type --
1928 ----------------------------------
1930 -- The operations defined for a discrete types are those of an enumeration
1931 -- type. The size is set to an arbitrary value, for use in analyzing the
1934 procedure Analyze_Formal_Discrete_Type (T : Entity_Id; Def : Node_Id) is
1935 Loc : constant Source_Ptr := Sloc (Def);
1939 Base : constant Entity_Id :=
1941 (E_Floating_Point_Type, Current_Scope,
1942 Sloc (Defining_Identifier (Parent (Def))), 'G');
1946 Set_Ekind (T, E_Enumeration_Subtype);
1947 Set_Etype (T, Base);
1950 Set_Is_Generic_Type (T);
1951 Set_Is_Constrained (T);
1953 -- For semantic analysis, the bounds of the type must be set to some
1954 -- non-static value. The simplest is to create attribute nodes for those
1955 -- bounds, that refer to the type itself. These bounds are never
1956 -- analyzed but serve as place-holders.
1959 Make_Attribute_Reference (Loc,
1960 Attribute_Name => Name_First,
1961 Prefix => New_Occurrence_Of (T, Loc));
1965 Make_Attribute_Reference (Loc,
1966 Attribute_Name => Name_Last,
1967 Prefix => New_Occurrence_Of (T, Loc));
1970 Set_Scalar_Range (T,
1975 Set_Ekind (Base, E_Enumeration_Type);
1976 Set_Etype (Base, Base);
1977 Init_Size (Base, 8);
1978 Init_Alignment (Base);
1979 Set_Is_Generic_Type (Base);
1980 Set_Scalar_Range (Base, Scalar_Range (T));
1981 Set_Parent (Base, Parent (Def));
1982 end Analyze_Formal_Discrete_Type;
1984 ----------------------------------
1985 -- Analyze_Formal_Floating_Type --
1986 ---------------------------------
1988 procedure Analyze_Formal_Floating_Type (T : Entity_Id; Def : Node_Id) is
1989 Base : constant Entity_Id :=
1991 (E_Floating_Point_Type, Current_Scope,
1992 Sloc (Defining_Identifier (Parent (Def))), 'G');
1995 -- The various semantic attributes are taken from the predefined type
1996 -- Float, just so that all of them are initialized. Their values are
1997 -- never used because no constant folding or expansion takes place in
1998 -- the generic itself.
2001 Set_Ekind (T, E_Floating_Point_Subtype);
2002 Set_Etype (T, Base);
2003 Set_Size_Info (T, (Standard_Float));
2004 Set_RM_Size (T, RM_Size (Standard_Float));
2005 Set_Digits_Value (T, Digits_Value (Standard_Float));
2006 Set_Scalar_Range (T, Scalar_Range (Standard_Float));
2007 Set_Is_Constrained (T);
2009 Set_Is_Generic_Type (Base);
2010 Set_Etype (Base, Base);
2011 Set_Size_Info (Base, (Standard_Float));
2012 Set_RM_Size (Base, RM_Size (Standard_Float));
2013 Set_Digits_Value (Base, Digits_Value (Standard_Float));
2014 Set_Scalar_Range (Base, Scalar_Range (Standard_Float));
2015 Set_Parent (Base, Parent (Def));
2017 Check_Restriction (No_Floating_Point, Def);
2018 end Analyze_Formal_Floating_Type;
2020 -----------------------------------
2021 -- Analyze_Formal_Interface_Type;--
2022 -----------------------------------
2024 procedure Analyze_Formal_Interface_Type
2029 Loc : constant Source_Ptr := Sloc (N);
2034 Make_Full_Type_Declaration (Loc,
2035 Defining_Identifier => T,
2036 Type_Definition => Def);
2040 Set_Is_Generic_Type (T);
2041 end Analyze_Formal_Interface_Type;
2043 ---------------------------------
2044 -- Analyze_Formal_Modular_Type --
2045 ---------------------------------
2047 procedure Analyze_Formal_Modular_Type (T : Entity_Id; Def : Node_Id) is
2049 -- Apart from their entity kind, generic modular types are treated like
2050 -- signed integer types, and have the same attributes.
2052 Analyze_Formal_Signed_Integer_Type (T, Def);
2053 Set_Ekind (T, E_Modular_Integer_Subtype);
2054 Set_Ekind (Etype (T), E_Modular_Integer_Type);
2056 end Analyze_Formal_Modular_Type;
2058 ---------------------------------------
2059 -- Analyze_Formal_Object_Declaration --
2060 ---------------------------------------
2062 procedure Analyze_Formal_Object_Declaration (N : Node_Id) is
2063 E : constant Node_Id := Default_Expression (N);
2064 Id : constant Node_Id := Defining_Identifier (N);
2071 -- Determine the mode of the formal object
2073 if Out_Present (N) then
2074 K := E_Generic_In_Out_Parameter;
2076 if not In_Present (N) then
2077 Error_Msg_N ("formal generic objects cannot have mode OUT", N);
2081 K := E_Generic_In_Parameter;
2084 if Present (Subtype_Mark (N)) then
2085 Find_Type (Subtype_Mark (N));
2086 T := Entity (Subtype_Mark (N));
2088 -- Verify that there is no redundant null exclusion
2090 if Null_Exclusion_Present (N) then
2091 if not Is_Access_Type (T) then
2093 ("null exclusion can only apply to an access type", N);
2095 elsif Can_Never_Be_Null (T) then
2097 ("`NOT NULL` not allowed (& already excludes null)", N, T);
2101 -- Ada 2005 (AI-423): Formal object with an access definition
2104 Check_Access_Definition (N);
2105 T := Access_Definition
2107 N => Access_Definition (N));
2110 if Ekind (T) = E_Incomplete_Type then
2112 Error_Node : Node_Id;
2115 if Present (Subtype_Mark (N)) then
2116 Error_Node := Subtype_Mark (N);
2118 Check_Access_Definition (N);
2119 Error_Node := Access_Definition (N);
2122 Error_Msg_N ("premature usage of incomplete type", Error_Node);
2126 if K = E_Generic_In_Parameter then
2128 -- Ada 2005 (AI-287): Limited aggregates allowed in generic formals
2130 if Ada_Version < Ada_2005 and then Is_Limited_Type (T) then
2132 ("generic formal of mode IN must not be of limited type", N);
2133 Explain_Limited_Type (T, N);
2136 if Is_Abstract_Type (T) then
2138 ("generic formal of mode IN must not be of abstract type", N);
2142 Preanalyze_Spec_Expression (E, T);
2144 if Is_Limited_Type (T) and then not OK_For_Limited_Init (T, E) then
2146 ("initialization not allowed for limited types", E);
2147 Explain_Limited_Type (T, E);
2154 -- Case of generic IN OUT parameter
2157 -- If the formal has an unconstrained type, construct its actual
2158 -- subtype, as is done for subprogram formals. In this fashion, all
2159 -- its uses can refer to specific bounds.
2164 if (Is_Array_Type (T) and then not Is_Constrained (T))
2165 or else (Ekind (T) = E_Record_Type and then Has_Discriminants (T))
2168 Non_Freezing_Ref : constant Node_Id :=
2169 New_Occurrence_Of (Id, Sloc (Id));
2173 -- Make sure the actual subtype doesn't generate bogus freezing
2175 Set_Must_Not_Freeze (Non_Freezing_Ref);
2176 Decl := Build_Actual_Subtype (T, Non_Freezing_Ref);
2177 Insert_Before_And_Analyze (N, Decl);
2178 Set_Actual_Subtype (Id, Defining_Identifier (Decl));
2181 Set_Actual_Subtype (Id, T);
2186 ("initialization not allowed for `IN OUT` formals", N);
2190 if Has_Aspects (N) then
2191 Analyze_Aspect_Specifications (N, Id);
2193 end Analyze_Formal_Object_Declaration;
2195 ----------------------------------------------
2196 -- Analyze_Formal_Ordinary_Fixed_Point_Type --
2197 ----------------------------------------------
2199 procedure Analyze_Formal_Ordinary_Fixed_Point_Type
2203 Loc : constant Source_Ptr := Sloc (Def);
2204 Base : constant Entity_Id :=
2206 (E_Ordinary_Fixed_Point_Type, Current_Scope,
2207 Sloc (Defining_Identifier (Parent (Def))), 'G');
2210 -- The semantic attributes are set for completeness only, their values
2211 -- will never be used, since all properties of the type are non-static.
2214 Set_Ekind (T, E_Ordinary_Fixed_Point_Subtype);
2215 Set_Etype (T, Base);
2216 Set_Size_Info (T, Standard_Integer);
2217 Set_RM_Size (T, RM_Size (Standard_Integer));
2218 Set_Small_Value (T, Ureal_1);
2219 Set_Delta_Value (T, Ureal_1);
2220 Set_Scalar_Range (T,
2222 Low_Bound => Make_Real_Literal (Loc, Ureal_1),
2223 High_Bound => Make_Real_Literal (Loc, Ureal_1)));
2224 Set_Is_Constrained (T);
2226 Set_Is_Generic_Type (Base);
2227 Set_Etype (Base, Base);
2228 Set_Size_Info (Base, Standard_Integer);
2229 Set_RM_Size (Base, RM_Size (Standard_Integer));
2230 Set_Small_Value (Base, Ureal_1);
2231 Set_Delta_Value (Base, Ureal_1);
2232 Set_Scalar_Range (Base, Scalar_Range (T));
2233 Set_Parent (Base, Parent (Def));
2235 Check_Restriction (No_Fixed_Point, Def);
2236 end Analyze_Formal_Ordinary_Fixed_Point_Type;
2238 ----------------------------------------
2239 -- Analyze_Formal_Package_Declaration --
2240 ----------------------------------------
2242 procedure Analyze_Formal_Package_Declaration (N : Node_Id) is
2243 Loc : constant Source_Ptr := Sloc (N);
2244 Pack_Id : constant Entity_Id := Defining_Identifier (N);
2246 Gen_Id : constant Node_Id := Name (N);
2248 Gen_Unit : Entity_Id;
2250 Parent_Installed : Boolean := False;
2252 Parent_Instance : Entity_Id;
2253 Renaming_In_Par : Entity_Id;
2254 Associations : Boolean := True;
2256 Vis_Prims_List : Elist_Id := No_Elist;
2257 -- List of primitives made temporarily visible in the instantiation
2258 -- to match the visibility of the formal type
2260 function Build_Local_Package return Node_Id;
2261 -- The formal package is rewritten so that its parameters are replaced
2262 -- with corresponding declarations. For parameters with bona fide
2263 -- associations these declarations are created by Analyze_Associations
2264 -- as for a regular instantiation. For boxed parameters, we preserve
2265 -- the formal declarations and analyze them, in order to introduce
2266 -- entities of the right kind in the environment of the formal.
2268 -------------------------
2269 -- Build_Local_Package --
2270 -------------------------
2272 function Build_Local_Package return Node_Id is
2274 Pack_Decl : Node_Id;
2277 -- Within the formal, the name of the generic package is a renaming
2278 -- of the formal (as for a regular instantiation).
2281 Make_Package_Declaration (Loc,
2284 (Specification (Original_Node (Gen_Decl)),
2285 Empty, Instantiating => True));
2287 Renaming := Make_Package_Renaming_Declaration (Loc,
2288 Defining_Unit_Name =>
2289 Make_Defining_Identifier (Loc, Chars (Gen_Unit)),
2290 Name => New_Occurrence_Of (Formal, Loc));
2292 if Nkind (Gen_Id) = N_Identifier
2293 and then Chars (Gen_Id) = Chars (Pack_Id)
2296 ("& is hidden within declaration of instance", Gen_Id, Gen_Unit);
2299 -- If the formal is declared with a box, or with an others choice,
2300 -- create corresponding declarations for all entities in the formal
2301 -- part, so that names with the proper types are available in the
2302 -- specification of the formal package.
2304 -- On the other hand, if there are no associations, then all the
2305 -- formals must have defaults, and this will be checked by the
2306 -- call to Analyze_Associations.
2309 or else Nkind (First (Generic_Associations (N))) = N_Others_Choice
2312 Formal_Decl : Node_Id;
2315 -- TBA : for a formal package, need to recurse ???
2320 (Generic_Formal_Declarations (Original_Node (Gen_Decl)));
2321 while Present (Formal_Decl) loop
2323 (Decls, Copy_Generic_Node (Formal_Decl, Empty, True));
2328 -- If generic associations are present, use Analyze_Associations to
2329 -- create the proper renaming declarations.
2333 Act_Tree : constant Node_Id :=
2335 (Original_Node (Gen_Decl), Empty,
2336 Instantiating => True);
2339 Generic_Renamings.Set_Last (0);
2340 Generic_Renamings_HTable.Reset;
2341 Instantiation_Node := N;
2344 Analyze_Associations
2345 (I_Node => Original_Node (N),
2346 Formals => Generic_Formal_Declarations (Act_Tree),
2347 F_Copy => Generic_Formal_Declarations (Gen_Decl));
2349 Vis_Prims_List := Check_Hidden_Primitives (Decls);
2353 Append (Renaming, To => Decls);
2355 -- Add generated declarations ahead of local declarations in
2358 if No (Visible_Declarations (Specification (Pack_Decl))) then
2359 Set_Visible_Declarations (Specification (Pack_Decl), Decls);
2362 (First (Visible_Declarations (Specification (Pack_Decl))),
2367 end Build_Local_Package;
2369 -- Start of processing for Analyze_Formal_Package_Declaration
2372 Check_Text_IO_Special_Unit (Gen_Id);
2375 Check_Generic_Child_Unit (Gen_Id, Parent_Installed);
2376 Gen_Unit := Entity (Gen_Id);
2378 -- Check for a formal package that is a package renaming
2380 if Present (Renamed_Object (Gen_Unit)) then
2382 -- Indicate that unit is used, before replacing it with renamed
2383 -- entity for use below.
2385 if In_Extended_Main_Source_Unit (N) then
2386 Set_Is_Instantiated (Gen_Unit);
2387 Generate_Reference (Gen_Unit, N);
2390 Gen_Unit := Renamed_Object (Gen_Unit);
2393 if Ekind (Gen_Unit) /= E_Generic_Package then
2394 Error_Msg_N ("expect generic package name", Gen_Id);
2398 elsif Gen_Unit = Current_Scope then
2400 ("generic package cannot be used as a formal package of itself",
2405 elsif In_Open_Scopes (Gen_Unit) then
2406 if Is_Compilation_Unit (Gen_Unit)
2407 and then Is_Child_Unit (Current_Scope)
2409 -- Special-case the error when the formal is a parent, and
2410 -- continue analysis to minimize cascaded errors.
2413 ("generic parent cannot be used as formal package "
2414 & "of a child unit", Gen_Id);
2418 ("generic package cannot be used as a formal package "
2419 & "within itself", Gen_Id);
2425 -- Check that name of formal package does not hide name of generic,
2426 -- or its leading prefix. This check must be done separately because
2427 -- the name of the generic has already been analyzed.
2430 Gen_Name : Entity_Id;
2434 while Nkind (Gen_Name) = N_Expanded_Name loop
2435 Gen_Name := Prefix (Gen_Name);
2438 if Chars (Gen_Name) = Chars (Pack_Id) then
2440 ("& is hidden within declaration of formal package",
2446 or else No (Generic_Associations (N))
2447 or else Nkind (First (Generic_Associations (N))) = N_Others_Choice
2449 Associations := False;
2452 -- If there are no generic associations, the generic parameters appear
2453 -- as local entities and are instantiated like them. We copy the generic
2454 -- package declaration as if it were an instantiation, and analyze it
2455 -- like a regular package, except that we treat the formals as
2456 -- additional visible components.
2458 Gen_Decl := Unit_Declaration_Node (Gen_Unit);
2460 if In_Extended_Main_Source_Unit (N) then
2461 Set_Is_Instantiated (Gen_Unit);
2462 Generate_Reference (Gen_Unit, N);
2465 Formal := New_Copy (Pack_Id);
2466 Create_Instantiation_Source (N, Gen_Unit, False, S_Adjustment);
2469 -- Make local generic without formals. The formals will be replaced
2470 -- with internal declarations.
2472 New_N := Build_Local_Package;
2474 -- If there are errors in the parameter list, Analyze_Associations
2475 -- raises Instantiation_Error. Patch the declaration to prevent
2476 -- further exception propagation.
2479 when Instantiation_Error =>
2481 Enter_Name (Formal);
2482 Set_Ekind (Formal, E_Variable);
2483 Set_Etype (Formal, Any_Type);
2484 Restore_Hidden_Primitives (Vis_Prims_List);
2486 if Parent_Installed then
2494 Set_Defining_Unit_Name (Specification (New_N), Formal);
2495 Set_Generic_Parent (Specification (N), Gen_Unit);
2496 Set_Instance_Env (Gen_Unit, Formal);
2497 Set_Is_Generic_Instance (Formal);
2499 Enter_Name (Formal);
2500 Set_Ekind (Formal, E_Package);
2501 Set_Etype (Formal, Standard_Void_Type);
2502 Set_Inner_Instances (Formal, New_Elmt_List);
2503 Push_Scope (Formal);
2505 if Is_Child_Unit (Gen_Unit) and then Parent_Installed then
2507 -- Similarly, we have to make the name of the formal visible in the
2508 -- parent instance, to resolve properly fully qualified names that
2509 -- may appear in the generic unit. The parent instance has been
2510 -- placed on the scope stack ahead of the current scope.
2512 Parent_Instance := Scope_Stack.Table (Scope_Stack.Last - 1).Entity;
2515 Make_Defining_Identifier (Loc, Chars (Gen_Unit));
2516 Set_Ekind (Renaming_In_Par, E_Package);
2517 Set_Etype (Renaming_In_Par, Standard_Void_Type);
2518 Set_Scope (Renaming_In_Par, Parent_Instance);
2519 Set_Parent (Renaming_In_Par, Parent (Formal));
2520 Set_Renamed_Object (Renaming_In_Par, Formal);
2521 Append_Entity (Renaming_In_Par, Parent_Instance);
2524 Analyze (Specification (N));
2526 -- The formals for which associations are provided are not visible
2527 -- outside of the formal package. The others are still declared by a
2528 -- formal parameter declaration.
2530 -- If there are no associations, the only local entity to hide is the
2531 -- generated package renaming itself.
2537 E := First_Entity (Formal);
2538 while Present (E) loop
2539 if Associations and then not Is_Generic_Formal (E) then
2543 if Ekind (E) = E_Package and then Renamed_Entity (E) = Formal then
2552 End_Package_Scope (Formal);
2553 Restore_Hidden_Primitives (Vis_Prims_List);
2555 if Parent_Installed then
2561 -- Inside the generic unit, the formal package is a regular package, but
2562 -- no body is needed for it. Note that after instantiation, the defining
2563 -- unit name we need is in the new tree and not in the original (see
2564 -- Package_Instantiation). A generic formal package is an instance, and
2565 -- can be used as an actual for an inner instance.
2567 Set_Has_Completion (Formal, True);
2569 -- Add semantic information to the original defining identifier.
2572 Set_Ekind (Pack_Id, E_Package);
2573 Set_Etype (Pack_Id, Standard_Void_Type);
2574 Set_Scope (Pack_Id, Scope (Formal));
2575 Set_Has_Completion (Pack_Id, True);
2578 if Has_Aspects (N) then
2579 Analyze_Aspect_Specifications (N, Pack_Id);
2581 end Analyze_Formal_Package_Declaration;
2583 ---------------------------------
2584 -- Analyze_Formal_Private_Type --
2585 ---------------------------------
2587 procedure Analyze_Formal_Private_Type
2593 New_Private_Type (N, T, Def);
2595 -- Set the size to an arbitrary but legal value
2597 Set_Size_Info (T, Standard_Integer);
2598 Set_RM_Size (T, RM_Size (Standard_Integer));
2599 end Analyze_Formal_Private_Type;
2601 ------------------------------------
2602 -- Analyze_Formal_Incomplete_Type --
2603 ------------------------------------
2605 procedure Analyze_Formal_Incomplete_Type
2611 Set_Ekind (T, E_Incomplete_Type);
2613 Set_Private_Dependents (T, New_Elmt_List);
2615 if Tagged_Present (Def) then
2616 Set_Is_Tagged_Type (T);
2617 Make_Class_Wide_Type (T);
2618 Set_Direct_Primitive_Operations (T, New_Elmt_List);
2620 end Analyze_Formal_Incomplete_Type;
2622 ----------------------------------------
2623 -- Analyze_Formal_Signed_Integer_Type --
2624 ----------------------------------------
2626 procedure Analyze_Formal_Signed_Integer_Type
2630 Base : constant Entity_Id :=
2632 (E_Signed_Integer_Type,
2634 Sloc (Defining_Identifier (Parent (Def))), 'G');
2639 Set_Ekind (T, E_Signed_Integer_Subtype);
2640 Set_Etype (T, Base);
2641 Set_Size_Info (T, Standard_Integer);
2642 Set_RM_Size (T, RM_Size (Standard_Integer));
2643 Set_Scalar_Range (T, Scalar_Range (Standard_Integer));
2644 Set_Is_Constrained (T);
2646 Set_Is_Generic_Type (Base);
2647 Set_Size_Info (Base, Standard_Integer);
2648 Set_RM_Size (Base, RM_Size (Standard_Integer));
2649 Set_Etype (Base, Base);
2650 Set_Scalar_Range (Base, Scalar_Range (Standard_Integer));
2651 Set_Parent (Base, Parent (Def));
2652 end Analyze_Formal_Signed_Integer_Type;
2654 -------------------------------------------
2655 -- Analyze_Formal_Subprogram_Declaration --
2656 -------------------------------------------
2658 procedure Analyze_Formal_Subprogram_Declaration (N : Node_Id) is
2659 Spec : constant Node_Id := Specification (N);
2660 Def : constant Node_Id := Default_Name (N);
2661 Nam : constant Entity_Id := Defining_Unit_Name (Spec);
2669 if Nkind (Nam) = N_Defining_Program_Unit_Name then
2670 Error_Msg_N ("name of formal subprogram must be a direct name", Nam);
2674 Analyze_Subprogram_Declaration (N);
2675 Set_Is_Formal_Subprogram (Nam);
2676 Set_Has_Completion (Nam);
2678 if Nkind (N) = N_Formal_Abstract_Subprogram_Declaration then
2679 Set_Is_Abstract_Subprogram (Nam);
2680 Set_Is_Dispatching_Operation (Nam);
2683 Ctrl_Type : constant Entity_Id := Find_Dispatching_Type (Nam);
2685 if No (Ctrl_Type) then
2687 ("abstract formal subprogram must have a controlling type",
2690 elsif Ada_Version >= Ada_2012
2691 and then Is_Incomplete_Type (Ctrl_Type)
2694 ("controlling type of abstract formal subprogram cannot "
2695 & "be incomplete type", N, Ctrl_Type);
2698 Check_Controlling_Formals (Ctrl_Type, Nam);
2703 -- Default name is resolved at the point of instantiation
2705 if Box_Present (N) then
2708 -- Else default is bound at the point of generic declaration
2710 elsif Present (Def) then
2711 if Nkind (Def) = N_Operator_Symbol then
2712 Find_Direct_Name (Def);
2714 elsif Nkind (Def) /= N_Attribute_Reference then
2718 -- For an attribute reference, analyze the prefix and verify
2719 -- that it has the proper profile for the subprogram.
2721 Analyze (Prefix (Def));
2722 Valid_Default_Attribute (Nam, Def);
2726 -- Default name may be overloaded, in which case the interpretation
2727 -- with the correct profile must be selected, as for a renaming.
2728 -- If the definition is an indexed component, it must denote a
2729 -- member of an entry family. If it is a selected component, it
2730 -- can be a protected operation.
2732 if Etype (Def) = Any_Type then
2735 elsif Nkind (Def) = N_Selected_Component then
2736 if not Is_Overloadable (Entity (Selector_Name (Def))) then
2737 Error_Msg_N ("expect valid subprogram name as default", Def);
2740 elsif Nkind (Def) = N_Indexed_Component then
2741 if Is_Entity_Name (Prefix (Def)) then
2742 if Ekind (Entity (Prefix (Def))) /= E_Entry_Family then
2743 Error_Msg_N ("expect valid subprogram name as default", Def);
2746 elsif Nkind (Prefix (Def)) = N_Selected_Component then
2747 if Ekind (Entity (Selector_Name (Prefix (Def)))) /=
2750 Error_Msg_N ("expect valid subprogram name as default", Def);
2754 Error_Msg_N ("expect valid subprogram name as default", Def);
2758 elsif Nkind (Def) = N_Character_Literal then
2760 -- Needs some type checks: subprogram should be parameterless???
2762 Resolve (Def, (Etype (Nam)));
2764 elsif not Is_Entity_Name (Def)
2765 or else not Is_Overloadable (Entity (Def))
2767 Error_Msg_N ("expect valid subprogram name as default", Def);
2770 elsif not Is_Overloaded (Def) then
2771 Subp := Entity (Def);
2774 Error_Msg_N ("premature usage of formal subprogram", Def);
2776 elsif not Entity_Matches_Spec (Subp, Nam) then
2777 Error_Msg_N ("no visible entity matches specification", Def);
2780 -- More than one interpretation, so disambiguate as for a renaming
2785 I1 : Interp_Index := 0;
2791 Get_First_Interp (Def, I, It);
2792 while Present (It.Nam) loop
2793 if Entity_Matches_Spec (It.Nam, Nam) then
2794 if Subp /= Any_Id then
2795 It1 := Disambiguate (Def, I1, I, Etype (Subp));
2797 if It1 = No_Interp then
2798 Error_Msg_N ("ambiguous default subprogram", Def);
2811 Get_Next_Interp (I, It);
2815 if Subp /= Any_Id then
2817 -- Subprogram found, generate reference to it
2819 Set_Entity (Def, Subp);
2820 Generate_Reference (Subp, Def);
2823 Error_Msg_N ("premature usage of formal subprogram", Def);
2825 elsif Ekind (Subp) /= E_Operator then
2826 Check_Mode_Conformant (Subp, Nam);
2830 Error_Msg_N ("no visible subprogram matches specification", N);
2836 if Has_Aspects (N) then
2837 Analyze_Aspect_Specifications (N, Nam);
2840 end Analyze_Formal_Subprogram_Declaration;
2842 -------------------------------------
2843 -- Analyze_Formal_Type_Declaration --
2844 -------------------------------------
2846 procedure Analyze_Formal_Type_Declaration (N : Node_Id) is
2847 Def : constant Node_Id := Formal_Type_Definition (N);
2851 T := Defining_Identifier (N);
2853 if Present (Discriminant_Specifications (N))
2854 and then Nkind (Def) /= N_Formal_Private_Type_Definition
2857 ("discriminants not allowed for this formal type", T);
2860 -- Enter the new name, and branch to specific routine
2863 when N_Formal_Private_Type_Definition =>
2864 Analyze_Formal_Private_Type (N, T, Def);
2866 when N_Formal_Derived_Type_Definition =>
2867 Analyze_Formal_Derived_Type (N, T, Def);
2869 when N_Formal_Incomplete_Type_Definition =>
2870 Analyze_Formal_Incomplete_Type (T, Def);
2872 when N_Formal_Discrete_Type_Definition =>
2873 Analyze_Formal_Discrete_Type (T, Def);
2875 when N_Formal_Signed_Integer_Type_Definition =>
2876 Analyze_Formal_Signed_Integer_Type (T, Def);
2878 when N_Formal_Modular_Type_Definition =>
2879 Analyze_Formal_Modular_Type (T, Def);
2881 when N_Formal_Floating_Point_Definition =>
2882 Analyze_Formal_Floating_Type (T, Def);
2884 when N_Formal_Ordinary_Fixed_Point_Definition =>
2885 Analyze_Formal_Ordinary_Fixed_Point_Type (T, Def);
2887 when N_Formal_Decimal_Fixed_Point_Definition =>
2888 Analyze_Formal_Decimal_Fixed_Point_Type (T, Def);
2890 when N_Array_Type_Definition =>
2891 Analyze_Formal_Array_Type (T, Def);
2893 when N_Access_To_Object_Definition |
2894 N_Access_Function_Definition |
2895 N_Access_Procedure_Definition =>
2896 Analyze_Generic_Access_Type (T, Def);
2898 -- Ada 2005: a interface declaration is encoded as an abstract
2899 -- record declaration or a abstract type derivation.
2901 when N_Record_Definition =>
2902 Analyze_Formal_Interface_Type (N, T, Def);
2904 when N_Derived_Type_Definition =>
2905 Analyze_Formal_Derived_Interface_Type (N, T, Def);
2911 raise Program_Error;
2915 Set_Is_Generic_Type (T);
2917 if Has_Aspects (N) then
2918 Analyze_Aspect_Specifications (N, T);
2920 end Analyze_Formal_Type_Declaration;
2922 ------------------------------------
2923 -- Analyze_Function_Instantiation --
2924 ------------------------------------
2926 procedure Analyze_Function_Instantiation (N : Node_Id) is
2928 Analyze_Subprogram_Instantiation (N, E_Function);
2929 end Analyze_Function_Instantiation;
2931 ---------------------------------
2932 -- Analyze_Generic_Access_Type --
2933 ---------------------------------
2935 procedure Analyze_Generic_Access_Type (T : Entity_Id; Def : Node_Id) is
2939 if Nkind (Def) = N_Access_To_Object_Definition then
2940 Access_Type_Declaration (T, Def);
2942 if Is_Incomplete_Or_Private_Type (Designated_Type (T))
2943 and then No (Full_View (Designated_Type (T)))
2944 and then not Is_Generic_Type (Designated_Type (T))
2946 Error_Msg_N ("premature usage of incomplete type", Def);
2948 elsif not Is_Entity_Name (Subtype_Indication (Def)) then
2950 ("only a subtype mark is allowed in a formal", Def);
2954 Access_Subprogram_Declaration (T, Def);
2956 end Analyze_Generic_Access_Type;
2958 ---------------------------------
2959 -- Analyze_Generic_Formal_Part --
2960 ---------------------------------
2962 procedure Analyze_Generic_Formal_Part (N : Node_Id) is
2963 Gen_Parm_Decl : Node_Id;
2966 -- The generic formals are processed in the scope of the generic unit,
2967 -- where they are immediately visible. The scope is installed by the
2970 Gen_Parm_Decl := First (Generic_Formal_Declarations (N));
2971 while Present (Gen_Parm_Decl) loop
2972 Analyze (Gen_Parm_Decl);
2973 Next (Gen_Parm_Decl);
2976 Generate_Reference_To_Generic_Formals (Current_Scope);
2977 end Analyze_Generic_Formal_Part;
2979 ------------------------------------------
2980 -- Analyze_Generic_Package_Declaration --
2981 ------------------------------------------
2983 procedure Analyze_Generic_Package_Declaration (N : Node_Id) is
2984 Loc : constant Source_Ptr := Sloc (N);
2987 Save_Parent : Node_Id;
2989 Decls : constant List_Id :=
2990 Visible_Declarations (Specification (N));
2994 Check_SPARK_05_Restriction ("generic is not allowed", N);
2996 -- We introduce a renaming of the enclosing package, to have a usable
2997 -- entity as the prefix of an expanded name for a local entity of the
2998 -- form Par.P.Q, where P is the generic package. This is because a local
2999 -- entity named P may hide it, so that the usual visibility rules in
3000 -- the instance will not resolve properly.
3003 Make_Package_Renaming_Declaration (Loc,
3004 Defining_Unit_Name =>
3005 Make_Defining_Identifier (Loc,
3006 Chars => New_External_Name (Chars (Defining_Entity (N)), "GH")),
3008 Make_Identifier (Loc, Chars (Defining_Entity (N))));
3010 if Present (Decls) then
3011 Decl := First (Decls);
3012 while Present (Decl) and then Nkind (Decl) = N_Pragma loop
3016 if Present (Decl) then
3017 Insert_Before (Decl, Renaming);
3019 Append (Renaming, Visible_Declarations (Specification (N)));
3023 Set_Visible_Declarations (Specification (N), New_List (Renaming));
3026 -- Create copy of generic unit, and save for instantiation. If the unit
3027 -- is a child unit, do not copy the specifications for the parent, which
3028 -- are not part of the generic tree.
3030 Save_Parent := Parent_Spec (N);
3031 Set_Parent_Spec (N, Empty);
3033 New_N := Copy_Generic_Node (N, Empty, Instantiating => False);
3034 Set_Parent_Spec (New_N, Save_Parent);
3037 -- Once the contents of the generic copy and the template are swapped,
3038 -- do the same for their respective aspect specifications.
3040 Exchange_Aspects (N, New_N);
3041 Id := Defining_Entity (N);
3042 Generate_Definition (Id);
3044 -- Expansion is not applied to generic units
3049 Set_Ekind (Id, E_Generic_Package);
3050 Set_Etype (Id, Standard_Void_Type);
3051 Set_Contract (Id, Make_Contract (Sloc (Id)));
3053 -- A generic package declared within a Ghost scope is rendered Ghost
3054 -- (SPARK RM 6.9(2)).
3056 if Within_Ghost_Scope then
3057 Set_Is_Ghost_Entity (Id);
3060 -- Analyze aspects now, so that generated pragmas appear in the
3061 -- declarations before building and analyzing the generic copy.
3063 if Has_Aspects (N) then
3064 Analyze_Aspect_Specifications (N, Id);
3068 Enter_Generic_Scope (Id);
3069 Set_Inner_Instances (Id, New_Elmt_List);
3071 Set_Categorization_From_Pragmas (N);
3072 Set_Is_Pure (Id, Is_Pure (Current_Scope));
3074 -- Link the declaration of the generic homonym in the generic copy to
3075 -- the package it renames, so that it is always resolved properly.
3077 Set_Generic_Homonym (Id, Defining_Unit_Name (Renaming));
3078 Set_Entity (Associated_Node (Name (Renaming)), Id);
3080 -- For a library unit, we have reconstructed the entity for the unit,
3081 -- and must reset it in the library tables.
3083 if Nkind (Parent (N)) = N_Compilation_Unit then
3084 Set_Cunit_Entity (Current_Sem_Unit, Id);
3087 Analyze_Generic_Formal_Part (N);
3089 -- After processing the generic formals, analysis proceeds as for a
3090 -- non-generic package.
3092 Analyze (Specification (N));
3094 Validate_Categorization_Dependency (N, Id);
3098 End_Package_Scope (Id);
3099 Exit_Generic_Scope (Id);
3101 if Nkind (Parent (N)) /= N_Compilation_Unit then
3102 Move_Freeze_Nodes (Id, N, Visible_Declarations (Specification (N)));
3103 Move_Freeze_Nodes (Id, N, Private_Declarations (Specification (N)));
3104 Move_Freeze_Nodes (Id, N, Generic_Formal_Declarations (N));
3107 Set_Body_Required (Parent (N), Unit_Requires_Body (Id));
3108 Validate_RT_RAT_Component (N);
3110 -- If this is a spec without a body, check that generic parameters
3113 if not Body_Required (Parent (N)) then
3114 Check_References (Id);
3118 -- If there is a specified storage pool in the context, create an
3119 -- aspect on the package declaration, so that it is used in any
3120 -- instance that does not override it.
3122 if Present (Default_Pool) then
3128 Make_Aspect_Specification (Loc,
3129 Identifier => Make_Identifier (Loc, Name_Default_Storage_Pool),
3130 Expression => New_Copy (Default_Pool));
3132 if No (Aspect_Specifications (Specification (N))) then
3133 Set_Aspect_Specifications (Specification (N), New_List (ASN));
3135 Append (ASN, Aspect_Specifications (Specification (N)));
3139 end Analyze_Generic_Package_Declaration;
3141 --------------------------------------------
3142 -- Analyze_Generic_Subprogram_Declaration --
3143 --------------------------------------------
3145 procedure Analyze_Generic_Subprogram_Declaration (N : Node_Id) is
3150 Result_Type : Entity_Id;
3151 Save_Parent : Node_Id;
3155 Check_SPARK_05_Restriction ("generic is not allowed", N);
3157 -- Create copy of generic unit, and save for instantiation. If the unit
3158 -- is a child unit, do not copy the specifications for the parent, which
3159 -- are not part of the generic tree.
3161 Save_Parent := Parent_Spec (N);
3162 Set_Parent_Spec (N, Empty);
3164 New_N := Copy_Generic_Node (N, Empty, Instantiating => False);
3165 Set_Parent_Spec (New_N, Save_Parent);
3168 -- Once the contents of the generic copy and the template are swapped,
3169 -- do the same for their respective aspect specifications.
3171 Exchange_Aspects (N, New_N);
3173 Spec := Specification (N);
3174 Id := Defining_Entity (Spec);
3175 Generate_Definition (Id);
3176 Set_Contract (Id, Make_Contract (Sloc (Id)));
3178 if Nkind (Id) = N_Defining_Operator_Symbol then
3180 ("operator symbol not allowed for generic subprogram", Id);
3186 Set_Scope_Depth_Value (Id, Scope_Depth (Current_Scope) + 1);
3188 -- Analyze the aspects of the generic copy to ensure that all generated
3189 -- pragmas (if any) perform their semantic effects.
3191 if Has_Aspects (N) then
3192 Analyze_Aspect_Specifications (N, Id);
3196 Enter_Generic_Scope (Id);
3197 Set_Inner_Instances (Id, New_Elmt_List);
3198 Set_Is_Pure (Id, Is_Pure (Current_Scope));
3200 Analyze_Generic_Formal_Part (N);
3202 Formals := Parameter_Specifications (Spec);
3204 if Present (Formals) then
3205 Process_Formals (Formals, Spec);
3208 if Nkind (Spec) = N_Function_Specification then
3209 Set_Ekind (Id, E_Generic_Function);
3211 if Nkind (Result_Definition (Spec)) = N_Access_Definition then
3212 Result_Type := Access_Definition (Spec, Result_Definition (Spec));
3213 Set_Etype (Id, Result_Type);
3215 -- Check restriction imposed by AI05-073: a generic function
3216 -- cannot return an abstract type or an access to such.
3218 -- This is a binding interpretation should it apply to earlier
3219 -- versions of Ada as well as Ada 2012???
3221 if Is_Abstract_Type (Designated_Type (Result_Type))
3222 and then Ada_Version >= Ada_2012
3225 ("generic function cannot have an access result "
3226 & "that designates an abstract type", Spec);
3230 Find_Type (Result_Definition (Spec));
3231 Typ := Entity (Result_Definition (Spec));
3233 if Is_Abstract_Type (Typ)
3234 and then Ada_Version >= Ada_2012
3237 ("generic function cannot have abstract result type", Spec);
3240 -- If a null exclusion is imposed on the result type, then create
3241 -- a null-excluding itype (an access subtype) and use it as the
3242 -- function's Etype.
3244 if Is_Access_Type (Typ)
3245 and then Null_Exclusion_Present (Spec)
3248 Create_Null_Excluding_Itype
3250 Related_Nod => Spec,
3251 Scope_Id => Defining_Unit_Name (Spec)));
3253 Set_Etype (Id, Typ);
3258 Set_Ekind (Id, E_Generic_Procedure);
3259 Set_Etype (Id, Standard_Void_Type);
3262 -- A generic subprogram declared within a Ghost scope is rendered Ghost
3263 -- (SPARK RM 6.9(2)).
3265 if Within_Ghost_Scope then
3266 Set_Is_Ghost_Entity (Id);
3269 -- For a library unit, we have reconstructed the entity for the unit,
3270 -- and must reset it in the library tables. We also make sure that
3271 -- Body_Required is set properly in the original compilation unit node.
3273 if Nkind (Parent (N)) = N_Compilation_Unit then
3274 Set_Cunit_Entity (Current_Sem_Unit, Id);
3275 Set_Body_Required (Parent (N), Unit_Requires_Body (Id));
3278 Set_Categorization_From_Pragmas (N);
3279 Validate_Categorization_Dependency (N, Id);
3281 Save_Global_References (Original_Node (N));
3283 -- For ASIS purposes, convert any postcondition, precondition pragmas
3284 -- into aspects, if N is not a compilation unit by itself, in order to
3285 -- enable the analysis of expressions inside the corresponding PPC
3288 if ASIS_Mode and then Is_List_Member (N) then
3289 Make_Aspect_For_PPC_In_Gen_Sub_Decl (N);
3294 Exit_Generic_Scope (Id);
3295 Generate_Reference_To_Formals (Id);
3297 List_Inherited_Pre_Post_Aspects (Id);
3298 end Analyze_Generic_Subprogram_Declaration;
3300 -----------------------------------
3301 -- Analyze_Package_Instantiation --
3302 -----------------------------------
3304 procedure Analyze_Package_Instantiation (N : Node_Id) is
3305 Loc : constant Source_Ptr := Sloc (N);
3306 Gen_Id : constant Node_Id := Name (N);
3309 Act_Decl_Name : Node_Id;
3310 Act_Decl_Id : Entity_Id;
3316 Gen_Unit : Entity_Id;
3318 Is_Actual_Pack : constant Boolean :=
3319 Is_Internal (Defining_Entity (N));
3321 Env_Installed : Boolean := False;
3322 Parent_Installed : Boolean := False;
3323 Renaming_List : List_Id;
3324 Unit_Renaming : Node_Id;
3325 Needs_Body : Boolean;
3326 Inline_Now : Boolean := False;
3328 Save_IPSM : constant Boolean := Ignore_Pragma_SPARK_Mode;
3329 -- Save flag Ignore_Pragma_SPARK_Mode for restore on exit
3331 Save_SM : constant SPARK_Mode_Type := SPARK_Mode;
3332 Save_SMP : constant Node_Id := SPARK_Mode_Pragma;
3333 -- Save the SPARK_Mode-related data for restore on exit
3335 Save_Style_Check : constant Boolean := Style_Check;
3336 -- Save style check mode for restore on exit
3338 procedure Delay_Descriptors (E : Entity_Id);
3339 -- Delay generation of subprogram descriptors for given entity
3341 function Might_Inline_Subp return Boolean;
3342 -- If inlining is active and the generic contains inlined subprograms,
3343 -- we instantiate the body. This may cause superfluous instantiations,
3344 -- but it is simpler than detecting the need for the body at the point
3345 -- of inlining, when the context of the instance is not available.
3347 -----------------------
3348 -- Delay_Descriptors --
3349 -----------------------
3351 procedure Delay_Descriptors (E : Entity_Id) is
3353 if not Delay_Subprogram_Descriptors (E) then
3354 Set_Delay_Subprogram_Descriptors (E);
3355 Pending_Descriptor.Append (E);
3357 end Delay_Descriptors;
3359 -----------------------
3360 -- Might_Inline_Subp --
3361 -----------------------
3363 function Might_Inline_Subp return Boolean is
3367 if not Inline_Processing_Required then
3371 E := First_Entity (Gen_Unit);
3372 while Present (E) loop
3373 if Is_Subprogram (E) and then Is_Inlined (E) then
3382 end Might_Inline_Subp;
3384 -- Local declarations
3386 Vis_Prims_List : Elist_Id := No_Elist;
3387 -- List of primitives made temporarily visible in the instantiation
3388 -- to match the visibility of the formal type
3390 -- Start of processing for Analyze_Package_Instantiation
3393 Check_SPARK_05_Restriction ("generic is not allowed", N);
3395 -- Very first thing: check for Text_IO sp[ecial unit in case we are
3396 -- instantiating one of the children of [[Wide_]Wide_]Text_IO.
3398 Check_Text_IO_Special_Unit (Name (N));
3400 -- Make node global for error reporting
3402 Instantiation_Node := N;
3404 -- Turn off style checking in instances. If the check is enabled on the
3405 -- generic unit, a warning in an instance would just be noise. If not
3406 -- enabled on the generic, then a warning in an instance is just wrong.
3408 Style_Check := False;
3410 -- Case of instantiation of a generic package
3412 if Nkind (N) = N_Package_Instantiation then
3413 Act_Decl_Id := New_Copy (Defining_Entity (N));
3414 Set_Comes_From_Source (Act_Decl_Id, True);
3416 if Nkind (Defining_Unit_Name (N)) = N_Defining_Program_Unit_Name then
3418 Make_Defining_Program_Unit_Name (Loc,
3420 New_Copy_Tree (Name (Defining_Unit_Name (N))),
3421 Defining_Identifier => Act_Decl_Id);
3423 Act_Decl_Name := Act_Decl_Id;
3426 -- Case of instantiation of a formal package
3429 Act_Decl_Id := Defining_Identifier (N);
3430 Act_Decl_Name := Act_Decl_Id;
3433 Generate_Definition (Act_Decl_Id);
3434 Preanalyze_Actuals (N);
3437 Env_Installed := True;
3439 -- Reset renaming map for formal types. The mapping is established
3440 -- when analyzing the generic associations, but some mappings are
3441 -- inherited from formal packages of parent units, and these are
3442 -- constructed when the parents are installed.
3444 Generic_Renamings.Set_Last (0);
3445 Generic_Renamings_HTable.Reset;
3447 Check_Generic_Child_Unit (Gen_Id, Parent_Installed);
3448 Gen_Unit := Entity (Gen_Id);
3450 -- Verify that it is the name of a generic package
3452 -- A visibility glitch: if the instance is a child unit and the generic
3453 -- is the generic unit of a parent instance (i.e. both the parent and
3454 -- the child units are instances of the same package) the name now
3455 -- denotes the renaming within the parent, not the intended generic
3456 -- unit. See if there is a homonym that is the desired generic. The
3457 -- renaming declaration must be visible inside the instance of the
3458 -- child, but not when analyzing the name in the instantiation itself.
3460 if Ekind (Gen_Unit) = E_Package
3461 and then Present (Renamed_Entity (Gen_Unit))
3462 and then In_Open_Scopes (Renamed_Entity (Gen_Unit))
3463 and then Is_Generic_Instance (Renamed_Entity (Gen_Unit))
3464 and then Present (Homonym (Gen_Unit))
3466 Gen_Unit := Homonym (Gen_Unit);
3469 if Etype (Gen_Unit) = Any_Type then
3473 elsif Ekind (Gen_Unit) /= E_Generic_Package then
3475 -- Ada 2005 (AI-50217): Cannot use instance in limited with_clause
3477 if From_Limited_With (Gen_Unit) then
3479 ("cannot instantiate a limited withed package", Gen_Id);
3482 ("& is not the name of a generic package", Gen_Id, Gen_Unit);
3489 if In_Extended_Main_Source_Unit (N) then
3490 Set_Is_Instantiated (Gen_Unit);
3491 Generate_Reference (Gen_Unit, N);
3493 if Present (Renamed_Object (Gen_Unit)) then
3494 Set_Is_Instantiated (Renamed_Object (Gen_Unit));
3495 Generate_Reference (Renamed_Object (Gen_Unit), N);
3499 if Nkind (Gen_Id) = N_Identifier
3500 and then Chars (Gen_Unit) = Chars (Defining_Entity (N))
3503 ("& is hidden within declaration of instance", Gen_Id, Gen_Unit);
3505 elsif Nkind (Gen_Id) = N_Expanded_Name
3506 and then Is_Child_Unit (Gen_Unit)
3507 and then Nkind (Prefix (Gen_Id)) = N_Identifier
3508 and then Chars (Act_Decl_Id) = Chars (Prefix (Gen_Id))
3511 ("& is hidden within declaration of instance ", Prefix (Gen_Id));
3514 Set_Entity (Gen_Id, Gen_Unit);
3516 -- If generic is a renaming, get original generic unit
3518 if Present (Renamed_Object (Gen_Unit))
3519 and then Ekind (Renamed_Object (Gen_Unit)) = E_Generic_Package
3521 Gen_Unit := Renamed_Object (Gen_Unit);
3524 -- Verify that there are no circular instantiations
3526 if In_Open_Scopes (Gen_Unit) then
3527 Error_Msg_NE ("instantiation of & within itself", N, Gen_Unit);
3531 elsif Contains_Instance_Of (Gen_Unit, Current_Scope, Gen_Id) then
3532 Error_Msg_Node_2 := Current_Scope;
3534 ("circular Instantiation: & instantiated in &!", N, Gen_Unit);
3535 Circularity_Detected := True;
3540 -- If the context of the instance is subject to SPARK_Mode "off",
3541 -- set the global flag which signals Analyze_Pragma to ignore all
3542 -- SPARK_Mode pragmas within the instance.
3544 if SPARK_Mode = Off then
3545 Ignore_Pragma_SPARK_Mode := True;
3548 Gen_Decl := Unit_Declaration_Node (Gen_Unit);
3549 Gen_Spec := Specification (Gen_Decl);
3551 -- Initialize renamings map, for error checking, and the list that
3552 -- holds private entities whose views have changed between generic
3553 -- definition and instantiation. If this is the instance created to
3554 -- validate an actual package, the instantiation environment is that
3555 -- of the enclosing instance.
3557 Create_Instantiation_Source (N, Gen_Unit, False, S_Adjustment);
3559 -- Copy original generic tree, to produce text for instantiation
3563 (Original_Node (Gen_Decl), Empty, Instantiating => True);
3565 Act_Spec := Specification (Act_Tree);
3567 -- If this is the instance created to validate an actual package,
3568 -- only the formals matter, do not examine the package spec itself.
3570 if Is_Actual_Pack then
3571 Set_Visible_Declarations (Act_Spec, New_List);
3572 Set_Private_Declarations (Act_Spec, New_List);
3576 Analyze_Associations
3578 Formals => Generic_Formal_Declarations (Act_Tree),
3579 F_Copy => Generic_Formal_Declarations (Gen_Decl));
3581 Vis_Prims_List := Check_Hidden_Primitives (Renaming_List);
3583 Set_Instance_Env (Gen_Unit, Act_Decl_Id);
3584 Set_Defining_Unit_Name (Act_Spec, Act_Decl_Name);
3585 Set_Is_Generic_Instance (Act_Decl_Id);
3586 Set_Generic_Parent (Act_Spec, Gen_Unit);
3588 -- References to the generic in its own declaration or its body are
3589 -- references to the instance. Add a renaming declaration for the
3590 -- generic unit itself. This declaration, as well as the renaming
3591 -- declarations for the generic formals, must remain private to the
3592 -- unit: the formals, because this is the language semantics, and
3593 -- the unit because its use is an artifact of the implementation.
3596 Make_Package_Renaming_Declaration (Loc,
3597 Defining_Unit_Name =>
3598 Make_Defining_Identifier (Loc, Chars (Gen_Unit)),
3599 Name => New_Occurrence_Of (Act_Decl_Id, Loc));
3601 Append (Unit_Renaming, Renaming_List);
3603 -- The renaming declarations are the first local declarations of the
3606 if Is_Non_Empty_List (Visible_Declarations (Act_Spec)) then
3608 (First (Visible_Declarations (Act_Spec)), Renaming_List);
3610 Set_Visible_Declarations (Act_Spec, Renaming_List);
3613 Act_Decl := Make_Package_Declaration (Loc, Specification => Act_Spec);
3615 -- Propagate the aspect specifications from the package declaration
3616 -- template to the instantiated version of the package declaration.
3618 if Has_Aspects (Act_Tree) then
3619 Set_Aspect_Specifications (Act_Decl,
3620 New_Copy_List_Tree (Aspect_Specifications (Act_Tree)));
3623 -- The generic may have a generated Default_Storage_Pool aspect,
3624 -- set at the point of generic declaration. If the instance has
3625 -- that aspect, it overrides the one inherited from the generic.
3627 if Has_Aspects (Gen_Spec) then
3628 if No (Aspect_Specifications (N)) then
3629 Set_Aspect_Specifications (N,
3631 (Aspect_Specifications (Gen_Spec))));
3635 ASN1, ASN2 : Node_Id;
3638 ASN1 := First (Aspect_Specifications (N));
3639 while Present (ASN1) loop
3640 if Chars (Identifier (ASN1)) = Name_Default_Storage_Pool
3642 -- If generic carries a default storage pool, remove
3643 -- it in favor of the instance one.
3645 ASN2 := First (Aspect_Specifications (Gen_Spec));
3646 while Present (ASN2) loop
3647 if Chars (Identifier (ASN2)) =
3648 Name_Default_Storage_Pool
3661 Prepend_List_To (Aspect_Specifications (N),
3663 (Aspect_Specifications (Gen_Spec))));
3668 -- Save the instantiation node, for subsequent instantiation of the
3669 -- body, if there is one and we are generating code for the current
3670 -- unit. Mark unit as having a body (avoids premature error message).
3672 -- We instantiate the body if we are generating code, if we are
3673 -- generating cross-reference information, or if we are building
3674 -- trees for ASIS use or GNATprove use.
3677 Enclosing_Body_Present : Boolean := False;
3678 -- If the generic unit is not a compilation unit, then a body may
3679 -- be present in its parent even if none is required. We create a
3680 -- tentative pending instantiation for the body, which will be
3681 -- discarded if none is actually present.
3686 if Scope (Gen_Unit) /= Standard_Standard
3687 and then not Is_Child_Unit (Gen_Unit)
3689 Scop := Scope (Gen_Unit);
3690 while Present (Scop)
3691 and then Scop /= Standard_Standard
3693 if Unit_Requires_Body (Scop) then
3694 Enclosing_Body_Present := True;
3697 elsif In_Open_Scopes (Scop)
3698 and then In_Package_Body (Scop)
3700 Enclosing_Body_Present := True;
3704 exit when Is_Compilation_Unit (Scop);
3705 Scop := Scope (Scop);
3709 -- If front-end inlining is enabled, and this is a unit for which
3710 -- code will be generated, we instantiate the body at once.
3712 -- This is done if the instance is not the main unit, and if the
3713 -- generic is not a child unit of another generic, to avoid scope
3714 -- problems and the reinstallation of parent instances.
3717 and then (not Is_Child_Unit (Gen_Unit)
3718 or else not Is_Generic_Unit (Scope (Gen_Unit)))
3719 and then Might_Inline_Subp
3720 and then not Is_Actual_Pack
3722 if not Back_End_Inlining
3723 and then Front_End_Inlining
3724 and then (Is_In_Main_Unit (N)
3725 or else In_Main_Context (Current_Scope))
3726 and then Nkind (Parent (N)) /= N_Compilation_Unit
3730 -- In configurable_run_time mode we force the inlining of
3731 -- predefined subprograms marked Inline_Always, to minimize
3732 -- the use of the run-time library.
3734 elsif Is_Predefined_File_Name
3735 (Unit_File_Name (Get_Source_Unit (Gen_Decl)))
3736 and then Configurable_Run_Time_Mode
3737 and then Nkind (Parent (N)) /= N_Compilation_Unit
3742 -- If the current scope is itself an instance within a child
3743 -- unit, there will be duplications in the scope stack, and the
3744 -- unstacking mechanism in Inline_Instance_Body will fail.
3745 -- This loses some rare cases of optimization, and might be
3746 -- improved some day, if we can find a proper abstraction for
3747 -- "the complete compilation context" that can be saved and
3750 if Is_Generic_Instance (Current_Scope) then
3752 Curr_Unit : constant Entity_Id :=
3753 Cunit_Entity (Current_Sem_Unit);
3755 if Curr_Unit /= Current_Scope
3756 and then Is_Child_Unit (Curr_Unit)
3758 Inline_Now := False;
3765 (Unit_Requires_Body (Gen_Unit)
3766 or else Enclosing_Body_Present
3767 or else Present (Corresponding_Body (Gen_Decl)))
3768 and then (Is_In_Main_Unit (N) or else Might_Inline_Subp)
3769 and then not Is_Actual_Pack
3770 and then not Inline_Now
3771 and then (Operating_Mode = Generate_Code
3773 -- Need comment for this check ???
3775 or else (Operating_Mode = Check_Semantics
3776 and then (ASIS_Mode or GNATprove_Mode)));
3778 -- If front_end_inlining is enabled, do not instantiate body if
3779 -- within a generic context.
3781 if (Front_End_Inlining and then not Expander_Active)
3782 or else Is_Generic_Unit (Cunit_Entity (Main_Unit))
3784 Needs_Body := False;
3787 -- If the current context is generic, and the package being
3788 -- instantiated is declared within a formal package, there is no
3789 -- body to instantiate until the enclosing generic is instantiated
3790 -- and there is an actual for the formal package. If the formal
3791 -- package has parameters, we build a regular package instance for
3792 -- it, that precedes the original formal package declaration.
3794 if In_Open_Scopes (Scope (Scope (Gen_Unit))) then
3796 Decl : constant Node_Id :=
3798 (Unit_Declaration_Node (Scope (Gen_Unit)));
3800 if Nkind (Decl) = N_Formal_Package_Declaration
3801 or else (Nkind (Decl) = N_Package_Declaration
3802 and then Is_List_Member (Decl)
3803 and then Present (Next (Decl))
3805 Nkind (Next (Decl)) =
3806 N_Formal_Package_Declaration)
3808 Needs_Body := False;
3814 -- For RCI unit calling stubs, we omit the instance body if the
3815 -- instance is the RCI library unit itself.
3817 -- However there is a special case for nested instances: in this case
3818 -- we do generate the instance body, as it might be required, e.g.
3819 -- because it provides stream attributes for some type used in the
3820 -- profile of a remote subprogram. This is consistent with 12.3(12),
3821 -- which indicates that the instance body occurs at the place of the
3822 -- instantiation, and thus is part of the RCI declaration, which is
3823 -- present on all client partitions (this is E.2.3(18)).
3825 -- Note that AI12-0002 may make it illegal at some point to have
3826 -- stream attributes defined in an RCI unit, in which case this
3827 -- special case will become unnecessary. In the meantime, there
3828 -- is known application code in production that depends on this
3829 -- being possible, so we definitely cannot eliminate the body in
3830 -- the case of nested instances for the time being.
3832 -- When we generate a nested instance body, calling stubs for any
3833 -- relevant subprogram will be be inserted immediately after the
3834 -- subprogram declarations, and will take precedence over the
3835 -- subsequent (original) body. (The stub and original body will be
3836 -- complete homographs, but this is permitted in an instance).
3837 -- (Could we do better and remove the original body???)
3839 if Distribution_Stub_Mode = Generate_Caller_Stub_Body
3840 and then Comes_From_Source (N)
3841 and then Nkind (Parent (N)) = N_Compilation_Unit
3843 Needs_Body := False;
3848 -- Here is a defence against a ludicrous number of instantiations
3849 -- caused by a circular set of instantiation attempts.
3851 if Pending_Instantiations.Last > Maximum_Instantiations then
3852 Error_Msg_Uint_1 := UI_From_Int (Maximum_Instantiations);
3853 Error_Msg_N ("too many instantiations, exceeds max of^", N);
3854 Error_Msg_N ("\limit can be changed using -gnateinn switch", N);
3855 raise Unrecoverable_Error;
3858 -- Indicate that the enclosing scopes contain an instantiation,
3859 -- and that cleanup actions should be delayed until after the
3860 -- instance body is expanded.
3862 Check_Forward_Instantiation (Gen_Decl);
3863 if Nkind (N) = N_Package_Instantiation then
3865 Enclosing_Master : Entity_Id;
3868 -- Loop to search enclosing masters
3870 Enclosing_Master := Current_Scope;
3871 Scope_Loop : while Enclosing_Master /= Standard_Standard loop
3872 if Ekind (Enclosing_Master) = E_Package then
3873 if Is_Compilation_Unit (Enclosing_Master) then
3874 if In_Package_Body (Enclosing_Master) then
3876 (Body_Entity (Enclosing_Master));
3885 Enclosing_Master := Scope (Enclosing_Master);
3888 elsif Is_Generic_Unit (Enclosing_Master)
3889 or else Ekind (Enclosing_Master) = E_Void
3891 -- Cleanup actions will eventually be performed on the
3892 -- enclosing subprogram or package instance, if any.
3893 -- Enclosing scope is void in the formal part of a
3894 -- generic subprogram.
3899 if Ekind (Enclosing_Master) = E_Entry
3901 Ekind (Scope (Enclosing_Master)) = E_Protected_Type
3903 if not Expander_Active then
3907 Protected_Body_Subprogram (Enclosing_Master);
3911 Set_Delay_Cleanups (Enclosing_Master);
3913 while Ekind (Enclosing_Master) = E_Block loop
3914 Enclosing_Master := Scope (Enclosing_Master);
3917 if Is_Subprogram (Enclosing_Master) then
3918 Delay_Descriptors (Enclosing_Master);
3920 elsif Is_Task_Type (Enclosing_Master) then
3922 TBP : constant Node_Id :=
3923 Get_Task_Body_Procedure
3926 if Present (TBP) then
3927 Delay_Descriptors (TBP);
3928 Set_Delay_Cleanups (TBP);
3935 end loop Scope_Loop;
3938 -- Make entry in table
3940 Pending_Instantiations.Append
3942 Act_Decl => Act_Decl,
3943 Expander_Status => Expander_Active,
3944 Current_Sem_Unit => Current_Sem_Unit,
3945 Scope_Suppress => Scope_Suppress,
3946 Local_Suppress_Stack_Top => Local_Suppress_Stack_Top,
3947 Version => Ada_Version,
3948 Version_Pragma => Ada_Version_Pragma,
3949 Warnings => Save_Warnings,
3950 SPARK_Mode => SPARK_Mode,
3951 SPARK_Mode_Pragma => SPARK_Mode_Pragma));
3955 Set_Categorization_From_Pragmas (Act_Decl);
3957 if Parent_Installed then
3961 Set_Instance_Spec (N, Act_Decl);
3963 -- If not a compilation unit, insert the package declaration before
3964 -- the original instantiation node.
3966 if Nkind (Parent (N)) /= N_Compilation_Unit then
3967 Mark_Rewrite_Insertion (Act_Decl);
3968 Insert_Before (N, Act_Decl);
3970 if Has_Aspects (N) then
3971 Analyze_Aspect_Specifications (N, Act_Decl_Id);
3973 -- The pragma created for a Default_Storage_Pool aspect must
3974 -- appear ahead of the declarations in the instance spec.
3975 -- Analysis has placed it after the instance node, so remove
3976 -- it and reinsert it properly now.
3979 ASN : constant Node_Id := First (Aspect_Specifications (N));
3980 A_Name : constant Name_Id := Chars (Identifier (ASN));
3984 if A_Name = Name_Default_Storage_Pool then
3985 if No (Visible_Declarations (Act_Spec)) then
3986 Set_Visible_Declarations (Act_Spec, New_List);
3990 while Present (Decl) loop
3991 if Nkind (Decl) = N_Pragma then
3993 Prepend (Decl, Visible_Declarations (Act_Spec));
4005 -- For an instantiation that is a compilation unit, place
4006 -- declaration on current node so context is complete for analysis
4007 -- (including nested instantiations). If this is the main unit,
4008 -- the declaration eventually replaces the instantiation node.
4009 -- If the instance body is created later, it replaces the
4010 -- instance node, and the declaration is attached to it
4011 -- (see Build_Instance_Compilation_Unit_Nodes).
4014 if Cunit_Entity (Current_Sem_Unit) = Defining_Entity (N) then
4016 -- The entity for the current unit is the newly created one,
4017 -- and all semantic information is attached to it.
4019 Set_Cunit_Entity (Current_Sem_Unit, Act_Decl_Id);
4021 -- If this is the main unit, replace the main entity as well
4023 if Current_Sem_Unit = Main_Unit then
4024 Main_Unit_Entity := Act_Decl_Id;
4028 Set_Unit (Parent (N), Act_Decl);
4029 Set_Parent_Spec (Act_Decl, Parent_Spec (N));
4030 Set_Package_Instantiation (Act_Decl_Id, N);
4032 -- Process aspect specifications of the instance node, if any, to
4033 -- take into account categorization pragmas before analyzing the
4036 if Has_Aspects (N) then
4037 Analyze_Aspect_Specifications (N, Act_Decl_Id);
4041 Set_Unit (Parent (N), N);
4042 Set_Body_Required (Parent (N), False);
4044 -- We never need elaboration checks on instantiations, since by
4045 -- definition, the body instantiation is elaborated at the same
4046 -- time as the spec instantiation.
4048 Set_Suppress_Elaboration_Warnings (Act_Decl_Id);
4049 Set_Kill_Elaboration_Checks (Act_Decl_Id);
4052 Check_Elab_Instantiation (N);
4054 if ABE_Is_Certain (N) and then Needs_Body then
4055 Pending_Instantiations.Decrement_Last;
4058 Check_Hidden_Child_Unit (N, Gen_Unit, Act_Decl_Id);
4060 Set_First_Private_Entity (Defining_Unit_Name (Unit_Renaming),
4061 First_Private_Entity (Act_Decl_Id));
4063 -- If the instantiation will receive a body, the unit will be
4064 -- transformed into a package body, and receive its own elaboration
4065 -- entity. Otherwise, the nature of the unit is now a package
4068 if Nkind (Parent (N)) = N_Compilation_Unit
4069 and then not Needs_Body
4071 Rewrite (N, Act_Decl);
4074 if Present (Corresponding_Body (Gen_Decl))
4075 or else Unit_Requires_Body (Gen_Unit)
4077 Set_Has_Completion (Act_Decl_Id);
4080 Check_Formal_Packages (Act_Decl_Id);
4082 Restore_Hidden_Primitives (Vis_Prims_List);
4083 Restore_Private_Views (Act_Decl_Id);
4085 Inherit_Context (Gen_Decl, N);
4087 if Parent_Installed then
4092 Env_Installed := False;
4095 Validate_Categorization_Dependency (N, Act_Decl_Id);
4097 -- There used to be a check here to prevent instantiations in local
4098 -- contexts if the No_Local_Allocators restriction was active. This
4099 -- check was removed by a binding interpretation in AI-95-00130/07,
4100 -- but we retain the code for documentation purposes.
4102 -- if Ekind (Act_Decl_Id) /= E_Void
4103 -- and then not Is_Library_Level_Entity (Act_Decl_Id)
4105 -- Check_Restriction (No_Local_Allocators, N);
4109 Inline_Instance_Body (N, Gen_Unit, Act_Decl);
4112 -- The following is a tree patch for ASIS: ASIS needs separate nodes to
4113 -- be used as defining identifiers for a formal package and for the
4114 -- corresponding expanded package.
4116 if Nkind (N) = N_Formal_Package_Declaration then
4117 Act_Decl_Id := New_Copy (Defining_Entity (N));
4118 Set_Comes_From_Source (Act_Decl_Id, True);
4119 Set_Is_Generic_Instance (Act_Decl_Id, False);
4120 Set_Defining_Identifier (N, Act_Decl_Id);
4123 Ignore_Pragma_SPARK_Mode := Save_IPSM;
4124 SPARK_Mode := Save_SM;
4125 SPARK_Mode_Pragma := Save_SMP;
4126 Style_Check := Save_Style_Check;
4128 if SPARK_Mode = On then
4129 Dynamic_Elaboration_Checks := False;
4132 -- Check that if N is an instantiation of System.Dim_Float_IO or
4133 -- System.Dim_Integer_IO, the formal type has a dimension system.
4135 if Nkind (N) = N_Package_Instantiation
4136 and then Is_Dim_IO_Package_Instantiation (N)
4139 Assoc : constant Node_Id := First (Generic_Associations (N));
4141 if not Has_Dimension_System
4142 (Etype (Explicit_Generic_Actual_Parameter (Assoc)))
4144 Error_Msg_N ("type with a dimension system expected", Assoc);
4150 if Has_Aspects (N) and then Nkind (Parent (N)) /= N_Compilation_Unit then
4151 Analyze_Aspect_Specifications (N, Act_Decl_Id);
4155 when Instantiation_Error =>
4156 if Parent_Installed then
4160 if Env_Installed then
4164 Ignore_Pragma_SPARK_Mode := Save_IPSM;
4165 SPARK_Mode := Save_SM;
4166 SPARK_Mode_Pragma := Save_SMP;
4167 Style_Check := Save_Style_Check;
4169 if SPARK_Mode = On then
4170 Dynamic_Elaboration_Checks := False;
4172 end Analyze_Package_Instantiation;
4174 --------------------------
4175 -- Inline_Instance_Body --
4176 --------------------------
4178 procedure Inline_Instance_Body
4180 Gen_Unit : Entity_Id;
4183 Curr_Comp : constant Node_Id := Cunit (Current_Sem_Unit);
4184 Curr_Unit : constant Entity_Id := Cunit_Entity (Current_Sem_Unit);
4185 Gen_Comp : constant Entity_Id :=
4186 Cunit_Entity (Get_Source_Unit (Gen_Unit));
4188 Save_SM : constant SPARK_Mode_Type := SPARK_Mode;
4189 Save_SMP : constant Node_Id := SPARK_Mode_Pragma;
4190 -- Save all SPARK_Mode-related attributes as removing enclosing scopes
4191 -- to provide a clean environment for analysis of the inlined body will
4192 -- eliminate any previously set SPARK_Mode.
4194 Scope_Stack_Depth : constant Int :=
4195 Scope_Stack.Last - Scope_Stack.First + 1;
4197 Use_Clauses : array (1 .. Scope_Stack_Depth) of Node_Id;
4198 Instances : array (1 .. Scope_Stack_Depth) of Entity_Id;
4199 Inner_Scopes : array (1 .. Scope_Stack_Depth) of Entity_Id;
4200 Curr_Scope : Entity_Id := Empty;
4202 Num_Inner : Int := 0;
4203 Num_Scopes : Int := 0;
4204 N_Instances : Int := 0;
4205 Removed : Boolean := False;
4210 -- Case of generic unit defined in another unit. We must remove the
4211 -- complete context of the current unit to install that of the generic.
4213 if Gen_Comp /= Cunit_Entity (Current_Sem_Unit) then
4215 -- Add some comments for the following two loops ???
4218 while Present (S) and then S /= Standard_Standard loop
4220 Num_Scopes := Num_Scopes + 1;
4222 Use_Clauses (Num_Scopes) :=
4224 (Scope_Stack.Last - Num_Scopes + 1).
4226 End_Use_Clauses (Use_Clauses (Num_Scopes));
4228 exit when Scope_Stack.Last - Num_Scopes + 1 = Scope_Stack.First
4229 or else Scope_Stack.Table
4230 (Scope_Stack.Last - Num_Scopes).Entity = Scope (S);
4233 exit when Is_Generic_Instance (S)
4234 and then (In_Package_Body (S)
4235 or else Ekind (S) = E_Procedure
4236 or else Ekind (S) = E_Function);
4240 Vis := Is_Immediately_Visible (Gen_Comp);
4242 -- Find and save all enclosing instances
4247 and then S /= Standard_Standard
4249 if Is_Generic_Instance (S) then
4250 N_Instances := N_Instances + 1;
4251 Instances (N_Instances) := S;
4253 exit when In_Package_Body (S);
4259 -- Remove context of current compilation unit, unless we are within a
4260 -- nested package instantiation, in which case the context has been
4261 -- removed previously.
4263 -- If current scope is the body of a child unit, remove context of
4264 -- spec as well. If an enclosing scope is an instance body, the
4265 -- context has already been removed, but the entities in the body
4266 -- must be made invisible as well.
4269 while Present (S) and then S /= Standard_Standard loop
4270 if Is_Generic_Instance (S)
4271 and then (In_Package_Body (S)
4272 or else Ekind_In (S, E_Procedure, E_Function))
4274 -- We still have to remove the entities of the enclosing
4275 -- instance from direct visibility.
4280 E := First_Entity (S);
4281 while Present (E) loop
4282 Set_Is_Immediately_Visible (E, False);
4291 or else (Ekind (Curr_Unit) = E_Package_Body
4292 and then S = Spec_Entity (Curr_Unit))
4293 or else (Ekind (Curr_Unit) = E_Subprogram_Body
4294 and then S = Corresponding_Spec
4295 (Unit_Declaration_Node (Curr_Unit)))
4299 -- Remove entities in current scopes from visibility, so that
4300 -- instance body is compiled in a clean environment.
4302 List := Save_Scope_Stack (Handle_Use => False);
4304 if Is_Child_Unit (S) then
4306 -- Remove child unit from stack, as well as inner scopes.
4307 -- Removing the context of a child unit removes parent units
4310 while Current_Scope /= S loop
4311 Num_Inner := Num_Inner + 1;
4312 Inner_Scopes (Num_Inner) := Current_Scope;
4317 Remove_Context (Curr_Comp);
4321 Remove_Context (Curr_Comp);
4324 if Ekind (Curr_Unit) = E_Package_Body then
4325 Remove_Context (Library_Unit (Curr_Comp));
4332 pragma Assert (Num_Inner < Num_Scopes);
4334 -- The inlined package body must be analyzed with the SPARK_Mode of
4335 -- the enclosing context, otherwise the body may cause bogus errors
4336 -- if a configuration SPARK_Mode pragma in in effect.
4338 Push_Scope (Standard_Standard);
4339 Scope_Stack.Table (Scope_Stack.Last).Is_Active_Stack_Base := True;
4340 Instantiate_Package_Body
4343 Act_Decl => Act_Decl,
4344 Expander_Status => Expander_Active,
4345 Current_Sem_Unit => Current_Sem_Unit,
4346 Scope_Suppress => Scope_Suppress,
4347 Local_Suppress_Stack_Top => Local_Suppress_Stack_Top,
4348 Version => Ada_Version,
4349 Version_Pragma => Ada_Version_Pragma,
4350 Warnings => Save_Warnings,
4351 SPARK_Mode => Save_SM,
4352 SPARK_Mode_Pragma => Save_SMP)),
4353 Inlined_Body => True);
4359 Set_Is_Immediately_Visible (Gen_Comp, Vis);
4361 -- Reset Generic_Instance flag so that use clauses can be installed
4362 -- in the proper order. (See Use_One_Package for effect of enclosing
4363 -- instances on processing of use clauses).
4365 for J in 1 .. N_Instances loop
4366 Set_Is_Generic_Instance (Instances (J), False);
4370 Install_Context (Curr_Comp);
4372 if Present (Curr_Scope)
4373 and then Is_Child_Unit (Curr_Scope)
4375 Push_Scope (Curr_Scope);
4376 Set_Is_Immediately_Visible (Curr_Scope);
4378 -- Finally, restore inner scopes as well
4380 for J in reverse 1 .. Num_Inner loop
4381 Push_Scope (Inner_Scopes (J));
4385 Restore_Scope_Stack (List, Handle_Use => False);
4387 if Present (Curr_Scope)
4389 (In_Private_Part (Curr_Scope)
4390 or else In_Package_Body (Curr_Scope))
4392 -- Install private declaration of ancestor units, which are
4393 -- currently available. Restore_Scope_Stack and Install_Context
4394 -- only install the visible part of parents.
4399 Par := Scope (Curr_Scope);
4400 while (Present (Par)) and then Par /= Standard_Standard loop
4401 Install_Private_Declarations (Par);
4408 -- Restore use clauses. For a child unit, use clauses in the parents
4409 -- are restored when installing the context, so only those in inner
4410 -- scopes (and those local to the child unit itself) need to be
4411 -- installed explicitly.
4413 if Is_Child_Unit (Curr_Unit) and then Removed then
4414 for J in reverse 1 .. Num_Inner + 1 loop
4415 Scope_Stack.Table (Scope_Stack.Last - J + 1).First_Use_Clause :=
4417 Install_Use_Clauses (Use_Clauses (J));
4421 for J in reverse 1 .. Num_Scopes loop
4422 Scope_Stack.Table (Scope_Stack.Last - J + 1).First_Use_Clause :=
4424 Install_Use_Clauses (Use_Clauses (J));
4428 -- Restore status of instances. If one of them is a body, make its
4429 -- local entities visible again.
4436 for J in 1 .. N_Instances loop
4437 Inst := Instances (J);
4438 Set_Is_Generic_Instance (Inst, True);
4440 if In_Package_Body (Inst)
4441 or else Ekind_In (S, E_Procedure, E_Function)
4443 E := First_Entity (Instances (J));
4444 while Present (E) loop
4445 Set_Is_Immediately_Visible (E);
4452 -- If generic unit is in current unit, current context is correct. Note
4453 -- that the context is guaranteed to carry the correct SPARK_Mode as no
4454 -- enclosing scopes were removed.
4457 Instantiate_Package_Body
4460 Act_Decl => Act_Decl,
4461 Expander_Status => Expander_Active,
4462 Current_Sem_Unit => Current_Sem_Unit,
4463 Scope_Suppress => Scope_Suppress,
4464 Local_Suppress_Stack_Top => Local_Suppress_Stack_Top,
4465 Version => Ada_Version,
4466 Version_Pragma => Ada_Version_Pragma,
4467 Warnings => Save_Warnings,
4468 SPARK_Mode => SPARK_Mode,
4469 SPARK_Mode_Pragma => SPARK_Mode_Pragma)),
4470 Inlined_Body => True);
4472 end Inline_Instance_Body;
4474 -------------------------------------
4475 -- Analyze_Procedure_Instantiation --
4476 -------------------------------------
4478 procedure Analyze_Procedure_Instantiation (N : Node_Id) is
4480 Analyze_Subprogram_Instantiation (N, E_Procedure);
4481 end Analyze_Procedure_Instantiation;
4483 -----------------------------------
4484 -- Need_Subprogram_Instance_Body --
4485 -----------------------------------
4487 function Need_Subprogram_Instance_Body
4489 Subp : Entity_Id) return Boolean
4492 -- Must be inlined (or inlined renaming)
4494 if (Is_In_Main_Unit (N)
4495 or else Is_Inlined (Subp)
4496 or else Is_Inlined (Alias (Subp)))
4498 -- Must be generating code or analyzing code in ASIS/GNATprove mode
4500 and then (Operating_Mode = Generate_Code
4501 or else (Operating_Mode = Check_Semantics
4502 and then (ASIS_Mode or GNATprove_Mode)))
4504 -- The body is needed when generating code (full expansion), in ASIS
4505 -- mode for other tools, and in GNATprove mode (special expansion) for
4506 -- formal verification of the body itself.
4508 and then (Expander_Active or ASIS_Mode or GNATprove_Mode)
4510 -- No point in inlining if ABE is inevitable
4512 and then not ABE_Is_Certain (N)
4514 -- Or if subprogram is eliminated
4516 and then not Is_Eliminated (Subp)
4518 Pending_Instantiations.Append
4520 Act_Decl => Unit_Declaration_Node (Subp),
4521 Expander_Status => Expander_Active,
4522 Current_Sem_Unit => Current_Sem_Unit,
4523 Scope_Suppress => Scope_Suppress,
4524 Local_Suppress_Stack_Top => Local_Suppress_Stack_Top,
4525 Version => Ada_Version,
4526 Version_Pragma => Ada_Version_Pragma,
4527 Warnings => Save_Warnings,
4528 SPARK_Mode => SPARK_Mode,
4529 SPARK_Mode_Pragma => SPARK_Mode_Pragma));
4532 -- Here if not inlined, or we ignore the inlining
4537 end Need_Subprogram_Instance_Body;
4539 --------------------------------------
4540 -- Analyze_Subprogram_Instantiation --
4541 --------------------------------------
4543 procedure Analyze_Subprogram_Instantiation
4547 Loc : constant Source_Ptr := Sloc (N);
4548 Gen_Id : constant Node_Id := Name (N);
4550 Anon_Id : constant Entity_Id :=
4551 Make_Defining_Identifier (Sloc (Defining_Entity (N)),
4552 Chars => New_External_Name
4553 (Chars (Defining_Entity (N)), 'R'));
4555 Act_Decl_Id : Entity_Id;
4560 Env_Installed : Boolean := False;
4561 Gen_Unit : Entity_Id;
4563 Pack_Id : Entity_Id;
4564 Parent_Installed : Boolean := False;
4565 Renaming_List : List_Id;
4567 procedure Analyze_Instance_And_Renamings;
4568 -- The instance must be analyzed in a context that includes the mappings
4569 -- of generic parameters into actuals. We create a package declaration
4570 -- for this purpose, and a subprogram with an internal name within the
4571 -- package. The subprogram instance is simply an alias for the internal
4572 -- subprogram, declared in the current scope.
4574 ------------------------------------
4575 -- Analyze_Instance_And_Renamings --
4576 ------------------------------------
4578 procedure Analyze_Instance_And_Renamings is
4579 Def_Ent : constant Entity_Id := Defining_Entity (N);
4580 Pack_Decl : Node_Id;
4583 if Nkind (Parent (N)) = N_Compilation_Unit then
4585 -- For the case of a compilation unit, the container package has
4586 -- the same name as the instantiation, to insure that the binder
4587 -- calls the elaboration procedure with the right name. Copy the
4588 -- entity of the instance, which may have compilation level flags
4589 -- (e.g. Is_Child_Unit) set.
4591 Pack_Id := New_Copy (Def_Ent);
4594 -- Otherwise we use the name of the instantiation concatenated
4595 -- with its source position to ensure uniqueness if there are
4596 -- several instantiations with the same name.
4599 Make_Defining_Identifier (Loc,
4600 Chars => New_External_Name
4601 (Related_Id => Chars (Def_Ent),
4603 Suffix_Index => Source_Offset (Sloc (Def_Ent))));
4606 Pack_Decl := Make_Package_Declaration (Loc,
4607 Specification => Make_Package_Specification (Loc,
4608 Defining_Unit_Name => Pack_Id,
4609 Visible_Declarations => Renaming_List,
4610 End_Label => Empty));
4612 Set_Instance_Spec (N, Pack_Decl);
4613 Set_Is_Generic_Instance (Pack_Id);
4614 Set_Debug_Info_Needed (Pack_Id);
4616 -- Case of not a compilation unit
4618 if Nkind (Parent (N)) /= N_Compilation_Unit then
4619 Mark_Rewrite_Insertion (Pack_Decl);
4620 Insert_Before (N, Pack_Decl);
4621 Set_Has_Completion (Pack_Id);
4623 -- Case of an instantiation that is a compilation unit
4625 -- Place declaration on current node so context is complete for
4626 -- analysis (including nested instantiations), and for use in a
4627 -- context_clause (see Analyze_With_Clause).
4630 Set_Unit (Parent (N), Pack_Decl);
4631 Set_Parent_Spec (Pack_Decl, Parent_Spec (N));
4634 Analyze (Pack_Decl);
4635 Check_Formal_Packages (Pack_Id);
4636 Set_Is_Generic_Instance (Pack_Id, False);
4638 -- Why do we clear Is_Generic_Instance??? We set it 20 lines
4641 -- Body of the enclosing package is supplied when instantiating the
4642 -- subprogram body, after semantic analysis is completed.
4644 if Nkind (Parent (N)) = N_Compilation_Unit then
4646 -- Remove package itself from visibility, so it does not
4647 -- conflict with subprogram.
4649 Set_Name_Entity_Id (Chars (Pack_Id), Homonym (Pack_Id));
4651 -- Set name and scope of internal subprogram so that the proper
4652 -- external name will be generated. The proper scope is the scope
4653 -- of the wrapper package. We need to generate debugging info for
4654 -- the internal subprogram, so set flag accordingly.
4656 Set_Chars (Anon_Id, Chars (Defining_Entity (N)));
4657 Set_Scope (Anon_Id, Scope (Pack_Id));
4659 -- Mark wrapper package as referenced, to avoid spurious warnings
4660 -- if the instantiation appears in various with_ clauses of
4661 -- subunits of the main unit.
4663 Set_Referenced (Pack_Id);
4666 Set_Is_Generic_Instance (Anon_Id);
4667 Set_Debug_Info_Needed (Anon_Id);
4668 Act_Decl_Id := New_Copy (Anon_Id);
4670 Set_Parent (Act_Decl_Id, Parent (Anon_Id));
4671 Set_Chars (Act_Decl_Id, Chars (Defining_Entity (N)));
4672 Set_Sloc (Act_Decl_Id, Sloc (Defining_Entity (N)));
4673 Set_Comes_From_Source (Act_Decl_Id, True);
4675 -- The signature may involve types that are not frozen yet, but the
4676 -- subprogram will be frozen at the point the wrapper package is
4677 -- frozen, so it does not need its own freeze node. In fact, if one
4678 -- is created, it might conflict with the freezing actions from the
4681 Set_Has_Delayed_Freeze (Anon_Id, False);
4683 -- If the instance is a child unit, mark the Id accordingly. Mark
4684 -- the anonymous entity as well, which is the real subprogram and
4685 -- which is used when the instance appears in a context clause.
4686 -- Similarly, propagate the Is_Eliminated flag to handle properly
4687 -- nested eliminated subprograms.
4689 Set_Is_Child_Unit (Act_Decl_Id, Is_Child_Unit (Defining_Entity (N)));
4690 Set_Is_Child_Unit (Anon_Id, Is_Child_Unit (Defining_Entity (N)));
4691 New_Overloaded_Entity (Act_Decl_Id);
4692 Check_Eliminated (Act_Decl_Id);
4693 Set_Is_Eliminated (Anon_Id, Is_Eliminated (Act_Decl_Id));
4695 -- In compilation unit case, kill elaboration checks on the
4696 -- instantiation, since they are never needed -- the body is
4697 -- instantiated at the same point as the spec.
4699 if Nkind (Parent (N)) = N_Compilation_Unit then
4700 Set_Suppress_Elaboration_Warnings (Act_Decl_Id);
4701 Set_Kill_Elaboration_Checks (Act_Decl_Id);
4702 Set_Is_Compilation_Unit (Anon_Id);
4704 Set_Cunit_Entity (Current_Sem_Unit, Pack_Id);
4707 -- The instance is not a freezing point for the new subprogram
4709 Set_Is_Frozen (Act_Decl_Id, False);
4711 if Nkind (Defining_Entity (N)) = N_Defining_Operator_Symbol then
4712 Valid_Operator_Definition (Act_Decl_Id);
4715 Set_Alias (Act_Decl_Id, Anon_Id);
4716 Set_Parent (Act_Decl_Id, Parent (Anon_Id));
4717 Set_Has_Completion (Act_Decl_Id);
4718 Set_Related_Instance (Pack_Id, Act_Decl_Id);
4720 if Nkind (Parent (N)) = N_Compilation_Unit then
4721 Set_Body_Required (Parent (N), False);
4723 end Analyze_Instance_And_Renamings;
4727 Save_IPSM : constant Boolean := Ignore_Pragma_SPARK_Mode;
4728 -- Save flag Ignore_Pragma_SPARK_Mode for restore on exit
4730 Save_SM : constant SPARK_Mode_Type := SPARK_Mode;
4731 Save_SMP : constant Node_Id := SPARK_Mode_Pragma;
4732 -- Save the SPARK_Mode-related data for restore on exit
4734 Vis_Prims_List : Elist_Id := No_Elist;
4735 -- List of primitives made temporarily visible in the instantiation
4736 -- to match the visibility of the formal type
4738 -- Start of processing for Analyze_Subprogram_Instantiation
4741 Check_SPARK_05_Restriction ("generic is not allowed", N);
4743 -- Very first thing: check for special Text_IO unit in case we are
4744 -- instantiating one of the children of [[Wide_]Wide_]Text_IO. Of course
4745 -- such an instantiation is bogus (these are packages, not subprograms),
4746 -- but we get a better error message if we do this.
4748 Check_Text_IO_Special_Unit (Gen_Id);
4750 -- Make node global for error reporting
4752 Instantiation_Node := N;
4754 -- For package instantiations we turn off style checks, because they
4755 -- will have been emitted in the generic. For subprogram instantiations
4756 -- we want to apply at least the check on overriding indicators so we
4757 -- do not modify the style check status.
4759 -- The renaming declarations for the actuals do not come from source and
4760 -- will not generate spurious warnings.
4762 Preanalyze_Actuals (N);
4765 Env_Installed := True;
4766 Check_Generic_Child_Unit (Gen_Id, Parent_Installed);
4767 Gen_Unit := Entity (Gen_Id);
4769 Generate_Reference (Gen_Unit, Gen_Id);
4771 if Nkind (Gen_Id) = N_Identifier
4772 and then Chars (Gen_Unit) = Chars (Defining_Entity (N))
4775 ("& is hidden within declaration of instance", Gen_Id, Gen_Unit);
4778 if Etype (Gen_Unit) = Any_Type then
4783 -- Verify that it is a generic subprogram of the right kind, and that
4784 -- it does not lead to a circular instantiation.
4786 if K = E_Procedure and then Ekind (Gen_Unit) /= E_Generic_Procedure then
4788 ("& is not the name of a generic procedure", Gen_Id, Gen_Unit);
4790 elsif K = E_Function and then Ekind (Gen_Unit) /= E_Generic_Function then
4792 ("& is not the name of a generic function", Gen_Id, Gen_Unit);
4794 elsif In_Open_Scopes (Gen_Unit) then
4795 Error_Msg_NE ("instantiation of & within itself", N, Gen_Unit);
4798 -- If the context of the instance is subject to SPARK_Mode "off",
4799 -- set the global flag which signals Analyze_Pragma to ignore all
4800 -- SPARK_Mode pragmas within the instance.
4802 if SPARK_Mode = Off then
4803 Ignore_Pragma_SPARK_Mode := True;
4806 Set_Entity (Gen_Id, Gen_Unit);
4807 Set_Is_Instantiated (Gen_Unit);
4809 if In_Extended_Main_Source_Unit (N) then
4810 Generate_Reference (Gen_Unit, N);
4813 -- If renaming, get original unit
4815 if Present (Renamed_Object (Gen_Unit))
4816 and then Ekind_In (Renamed_Object (Gen_Unit), E_Generic_Procedure,
4819 Gen_Unit := Renamed_Object (Gen_Unit);
4820 Set_Is_Instantiated (Gen_Unit);
4821 Generate_Reference (Gen_Unit, N);
4824 if Contains_Instance_Of (Gen_Unit, Current_Scope, Gen_Id) then
4825 Error_Msg_Node_2 := Current_Scope;
4827 ("circular Instantiation: & instantiated in &!", N, Gen_Unit);
4828 Circularity_Detected := True;
4829 Restore_Hidden_Primitives (Vis_Prims_List);
4833 Gen_Decl := Unit_Declaration_Node (Gen_Unit);
4835 -- Initialize renamings map, for error checking
4837 Generic_Renamings.Set_Last (0);
4838 Generic_Renamings_HTable.Reset;
4840 Create_Instantiation_Source (N, Gen_Unit, False, S_Adjustment);
4842 -- Copy original generic tree, to produce text for instantiation
4846 (Original_Node (Gen_Decl), Empty, Instantiating => True);
4848 -- Inherit overriding indicator from instance node
4850 Act_Spec := Specification (Act_Tree);
4851 Set_Must_Override (Act_Spec, Must_Override (N));
4852 Set_Must_Not_Override (Act_Spec, Must_Not_Override (N));
4855 Analyze_Associations
4857 Formals => Generic_Formal_Declarations (Act_Tree),
4858 F_Copy => Generic_Formal_Declarations (Gen_Decl));
4860 Vis_Prims_List := Check_Hidden_Primitives (Renaming_List);
4862 -- The subprogram itself cannot contain a nested instance, so the
4863 -- current parent is left empty.
4865 Set_Instance_Env (Gen_Unit, Empty);
4867 -- Build the subprogram declaration, which does not appear in the
4868 -- generic template, and give it a sloc consistent with that of the
4871 Set_Defining_Unit_Name (Act_Spec, Anon_Id);
4872 Set_Generic_Parent (Act_Spec, Gen_Unit);
4874 Make_Subprogram_Declaration (Sloc (Act_Spec),
4875 Specification => Act_Spec);
4877 -- The aspects have been copied previously, but they have to be
4878 -- linked explicitly to the new subprogram declaration. Explicit
4879 -- pre/postconditions on the instance are analyzed below, in a
4882 Move_Aspects (Act_Tree, To => Act_Decl);
4883 Set_Categorization_From_Pragmas (Act_Decl);
4885 if Parent_Installed then
4889 Append (Act_Decl, Renaming_List);
4890 Analyze_Instance_And_Renamings;
4892 -- If the generic is marked Import (Intrinsic), then so is the
4893 -- instance. This indicates that there is no body to instantiate. If
4894 -- generic is marked inline, so it the instance, and the anonymous
4895 -- subprogram it renames. If inlined, or else if inlining is enabled
4896 -- for the compilation, we generate the instance body even if it is
4897 -- not within the main unit.
4899 if Is_Intrinsic_Subprogram (Gen_Unit) then
4900 Set_Is_Intrinsic_Subprogram (Anon_Id);
4901 Set_Is_Intrinsic_Subprogram (Act_Decl_Id);
4903 if Chars (Gen_Unit) = Name_Unchecked_Conversion then
4904 Validate_Unchecked_Conversion (N, Act_Decl_Id);
4908 -- Inherit convention from generic unit. Intrinsic convention, as for
4909 -- an instance of unchecked conversion, is not inherited because an
4910 -- explicit Ada instance has been created.
4912 if Has_Convention_Pragma (Gen_Unit)
4913 and then Convention (Gen_Unit) /= Convention_Intrinsic
4915 Set_Convention (Act_Decl_Id, Convention (Gen_Unit));
4916 Set_Is_Exported (Act_Decl_Id, Is_Exported (Gen_Unit));
4919 Generate_Definition (Act_Decl_Id);
4920 -- Set_Contract (Anon_Id, Make_Contract (Sloc (Anon_Id)));
4922 Set_Contract (Act_Decl_Id, Make_Contract (Sloc (Act_Decl_Id)));
4924 -- Inherit all inlining-related flags which apply to the generic in
4925 -- the subprogram and its declaration.
4927 Set_Is_Inlined (Act_Decl_Id, Is_Inlined (Gen_Unit));
4928 Set_Is_Inlined (Anon_Id, Is_Inlined (Gen_Unit));
4930 Set_Has_Pragma_Inline (Act_Decl_Id, Has_Pragma_Inline (Gen_Unit));
4931 Set_Has_Pragma_Inline (Anon_Id, Has_Pragma_Inline (Gen_Unit));
4933 Set_Has_Pragma_Inline_Always
4934 (Act_Decl_Id, Has_Pragma_Inline_Always (Gen_Unit));
4935 Set_Has_Pragma_Inline_Always
4936 (Anon_Id, Has_Pragma_Inline_Always (Gen_Unit));
4938 if not Is_Intrinsic_Subprogram (Gen_Unit) then
4939 Check_Elab_Instantiation (N);
4942 if Is_Dispatching_Operation (Act_Decl_Id)
4943 and then Ada_Version >= Ada_2005
4949 Formal := First_Formal (Act_Decl_Id);
4950 while Present (Formal) loop
4951 if Ekind (Etype (Formal)) = E_Anonymous_Access_Type
4952 and then Is_Controlling_Formal (Formal)
4953 and then not Can_Never_Be_Null (Formal)
4956 ("access parameter& is controlling,", N, Formal);
4958 ("\corresponding parameter of & must be "
4959 & "explicitly null-excluding", N, Gen_Id);
4962 Next_Formal (Formal);
4967 Check_Hidden_Child_Unit (N, Gen_Unit, Act_Decl_Id);
4969 Validate_Categorization_Dependency (N, Act_Decl_Id);
4971 if not Is_Intrinsic_Subprogram (Act_Decl_Id) then
4972 Inherit_Context (Gen_Decl, N);
4974 Restore_Private_Views (Pack_Id, False);
4976 -- If the context requires a full instantiation, mark node for
4977 -- subsequent construction of the body.
4979 if Need_Subprogram_Instance_Body (N, Act_Decl_Id) then
4980 Check_Forward_Instantiation (Gen_Decl);
4982 -- The wrapper package is always delayed, because it does not
4983 -- constitute a freeze point, but to insure that the freeze
4984 -- node is placed properly, it is created directly when
4985 -- instantiating the body (otherwise the freeze node might
4986 -- appear to early for nested instantiations).
4988 elsif Nkind (Parent (N)) = N_Compilation_Unit then
4990 -- For ASIS purposes, indicate that the wrapper package has
4991 -- replaced the instantiation node.
4993 Rewrite (N, Unit (Parent (N)));
4994 Set_Unit (Parent (N), N);
4997 elsif Nkind (Parent (N)) = N_Compilation_Unit then
4999 -- Replace instance node for library-level instantiations of
5000 -- intrinsic subprograms, for ASIS use.
5002 Rewrite (N, Unit (Parent (N)));
5003 Set_Unit (Parent (N), N);
5006 if Parent_Installed then
5010 Restore_Hidden_Primitives (Vis_Prims_List);
5012 Env_Installed := False;
5013 Generic_Renamings.Set_Last (0);
5014 Generic_Renamings_HTable.Reset;
5016 Ignore_Pragma_SPARK_Mode := Save_IPSM;
5017 SPARK_Mode := Save_SM;
5018 SPARK_Mode_Pragma := Save_SMP;
5020 if SPARK_Mode = On then
5021 Dynamic_Elaboration_Checks := False;
5027 if Has_Aspects (N) then
5028 Analyze_Aspect_Specifications (N, Act_Decl_Id);
5032 when Instantiation_Error =>
5033 if Parent_Installed then
5037 if Env_Installed then
5041 Ignore_Pragma_SPARK_Mode := Save_IPSM;
5042 SPARK_Mode := Save_SM;
5043 SPARK_Mode_Pragma := Save_SMP;
5045 if SPARK_Mode = On then
5046 Dynamic_Elaboration_Checks := False;
5048 end Analyze_Subprogram_Instantiation;
5050 -------------------------
5051 -- Get_Associated_Node --
5052 -------------------------
5054 function Get_Associated_Node (N : Node_Id) return Node_Id is
5058 Assoc := Associated_Node (N);
5060 if Nkind (Assoc) /= Nkind (N) then
5063 elsif Nkind_In (Assoc, N_Aggregate, N_Extension_Aggregate) then
5067 -- If the node is part of an inner generic, it may itself have been
5068 -- remapped into a further generic copy. Associated_Node is otherwise
5069 -- used for the entity of the node, and will be of a different node
5070 -- kind, or else N has been rewritten as a literal or function call.
5072 while Present (Associated_Node (Assoc))
5073 and then Nkind (Associated_Node (Assoc)) = Nkind (Assoc)
5075 Assoc := Associated_Node (Assoc);
5078 -- Follow and additional link in case the final node was rewritten.
5079 -- This can only happen with nested generic units.
5081 if (Nkind (Assoc) = N_Identifier or else Nkind (Assoc) in N_Op)
5082 and then Present (Associated_Node (Assoc))
5083 and then (Nkind_In (Associated_Node (Assoc), N_Function_Call,
5084 N_Explicit_Dereference,
5089 Assoc := Associated_Node (Assoc);
5092 -- An additional special case: an unconstrained type in an object
5093 -- declaration may have been rewritten as a local subtype constrained
5094 -- by the expression in the declaration. We need to recover the
5095 -- original entity which may be global.
5097 if Present (Original_Node (Assoc))
5098 and then Nkind (Parent (N)) = N_Object_Declaration
5100 Assoc := Original_Node (Assoc);
5105 end Get_Associated_Node;
5107 ----------------------------
5108 -- Build_Function_Wrapper --
5109 ----------------------------
5111 function Build_Function_Wrapper
5112 (Formal_Subp : Entity_Id;
5113 Actual_Subp : Entity_Id) return Node_Id
5115 Loc : constant Source_Ptr := Sloc (Formal_Subp);
5116 Ret_Type : constant Entity_Id := Get_Instance_Of (Etype (Formal_Subp));
5119 Func_Name : Node_Id;
5121 Parm_Type : Node_Id;
5122 Profile : List_Id := New_List;
5129 Func_Name := New_Occurrence_Of (Actual_Subp, Loc);
5131 Func := Make_Defining_Identifier (Loc, Chars (Formal_Subp));
5132 Set_Ekind (Func, E_Function);
5133 Set_Is_Generic_Actual_Subprogram (Func);
5135 Actuals := New_List;
5136 Profile := New_List;
5138 Act_F := First_Formal (Actual_Subp);
5139 Form_F := First_Formal (Formal_Subp);
5140 while Present (Form_F) loop
5142 -- Create new formal for profile of wrapper, and add a reference
5143 -- to it in the list of actuals for the enclosing call. The name
5144 -- must be that of the formal in the formal subprogram, because
5145 -- calls to it in the generic body may use named associations.
5147 New_F := Make_Defining_Identifier (Loc, Chars (Form_F));
5150 New_Occurrence_Of (Get_Instance_Of (Etype (Form_F)), Loc);
5153 Make_Parameter_Specification (Loc,
5154 Defining_Identifier => New_F,
5155 Parameter_Type => Parm_Type));
5157 Append_To (Actuals, New_Occurrence_Of (New_F, Loc));
5158 Next_Formal (Form_F);
5160 if Present (Act_F) then
5161 Next_Formal (Act_F);
5166 Make_Function_Specification (Loc,
5167 Defining_Unit_Name => Func,
5168 Parameter_Specifications => Profile,
5169 Result_Definition => New_Occurrence_Of (Ret_Type, Loc));
5172 Make_Expression_Function (Loc,
5173 Specification => Spec,
5175 Make_Function_Call (Loc,
5177 Parameter_Associations => Actuals));
5180 end Build_Function_Wrapper;
5182 ----------------------------
5183 -- Build_Operator_Wrapper --
5184 ----------------------------
5186 function Build_Operator_Wrapper
5187 (Formal_Subp : Entity_Id;
5188 Actual_Subp : Entity_Id) return Node_Id
5190 Loc : constant Source_Ptr := Sloc (Formal_Subp);
5191 Ret_Type : constant Entity_Id := Get_Instance_Of (Etype (Formal_Subp));
5192 Op_Type : constant Entity_Id := Get_Instance_Of
5193 (Etype (First_Formal (Formal_Subp)));
5194 Is_Binary : constant Boolean :=
5195 Present (Next_Formal (First_Formal (Formal_Subp)));
5206 Op_Name := Chars (Actual_Subp);
5208 -- Create entities for wrapper function and its formals
5210 F1 := Make_Temporary (Loc, 'A');
5211 F2 := Make_Temporary (Loc, 'B');
5212 L := New_Occurrence_Of (F1, Loc);
5213 R := New_Occurrence_Of (F2, Loc);
5215 Func := Make_Defining_Identifier (Loc, Chars (Formal_Subp));
5216 Set_Ekind (Func, E_Function);
5217 Set_Is_Generic_Actual_Subprogram (Func);
5220 Make_Function_Specification (Loc,
5221 Defining_Unit_Name => Func,
5222 Parameter_Specifications => New_List (
5223 Make_Parameter_Specification (Loc,
5224 Defining_Identifier => F1,
5225 Parameter_Type => New_Occurrence_Of (Op_Type, Loc))),
5226 Result_Definition => New_Occurrence_Of (Ret_Type, Loc));
5229 Append_To (Parameter_Specifications (Spec),
5230 Make_Parameter_Specification (Loc,
5231 Defining_Identifier => F2,
5232 Parameter_Type => New_Occurrence_Of (Op_Type, Loc)));
5235 -- Build expression as a function call, or as an operator node
5236 -- that corresponds to the name of the actual, starting with
5237 -- binary operators.
5239 if Op_Name not in Any_Operator_Name then
5241 Make_Function_Call (Loc,
5243 New_Occurrence_Of (Actual_Subp, Loc),
5244 Parameter_Associations => New_List (L));
5247 Append_To (Parameter_Associations (Expr), R);
5252 elsif Is_Binary then
5253 if Op_Name = Name_Op_And then
5254 Expr := Make_Op_And (Loc, Left_Opnd => L, Right_Opnd => R);
5255 elsif Op_Name = Name_Op_Or then
5256 Expr := Make_Op_Or (Loc, Left_Opnd => L, Right_Opnd => R);
5257 elsif Op_Name = Name_Op_Xor then
5258 Expr := Make_Op_Xor (Loc, Left_Opnd => L, Right_Opnd => R);
5259 elsif Op_Name = Name_Op_Eq then
5260 Expr := Make_Op_Eq (Loc, Left_Opnd => L, Right_Opnd => R);
5261 elsif Op_Name = Name_Op_Ne then
5262 Expr := Make_Op_Ne (Loc, Left_Opnd => L, Right_Opnd => R);
5263 elsif Op_Name = Name_Op_Le then
5264 Expr := Make_Op_Le (Loc, Left_Opnd => L, Right_Opnd => R);
5265 elsif Op_Name = Name_Op_Gt then
5266 Expr := Make_Op_Gt (Loc, Left_Opnd => L, Right_Opnd => R);
5267 elsif Op_Name = Name_Op_Ge then
5268 Expr := Make_Op_Ge (Loc, Left_Opnd => L, Right_Opnd => R);
5269 elsif Op_Name = Name_Op_Lt then
5270 Expr := Make_Op_Lt (Loc, Left_Opnd => L, Right_Opnd => R);
5271 elsif Op_Name = Name_Op_Add then
5272 Expr := Make_Op_Add (Loc, Left_Opnd => L, Right_Opnd => R);
5273 elsif Op_Name = Name_Op_Subtract then
5274 Expr := Make_Op_Subtract (Loc, Left_Opnd => L, Right_Opnd => R);
5275 elsif Op_Name = Name_Op_Concat then
5276 Expr := Make_Op_Concat (Loc, Left_Opnd => L, Right_Opnd => R);
5277 elsif Op_Name = Name_Op_Multiply then
5278 Expr := Make_Op_Multiply (Loc, Left_Opnd => L, Right_Opnd => R);
5279 elsif Op_Name = Name_Op_Divide then
5280 Expr := Make_Op_Divide (Loc, Left_Opnd => L, Right_Opnd => R);
5281 elsif Op_Name = Name_Op_Mod then
5282 Expr := Make_Op_Mod (Loc, Left_Opnd => L, Right_Opnd => R);
5283 elsif Op_Name = Name_Op_Rem then
5284 Expr := Make_Op_Rem (Loc, Left_Opnd => L, Right_Opnd => R);
5285 elsif Op_Name = Name_Op_Expon then
5286 Expr := Make_Op_Expon (Loc, Left_Opnd => L, Right_Opnd => R);
5292 if Op_Name = Name_Op_Add then
5293 Expr := Make_Op_Plus (Loc, Right_Opnd => L);
5294 elsif Op_Name = Name_Op_Subtract then
5295 Expr := Make_Op_Minus (Loc, Right_Opnd => L);
5296 elsif Op_Name = Name_Op_Abs then
5297 Expr := Make_Op_Abs (Loc, Right_Opnd => L);
5298 elsif Op_Name = Name_Op_Not then
5299 Expr := Make_Op_Not (Loc, Right_Opnd => L);
5304 Make_Expression_Function (Loc,
5305 Specification => Spec,
5306 Expression => Expr);
5309 end Build_Operator_Wrapper;
5311 -------------------------------------------
5312 -- Build_Instance_Compilation_Unit_Nodes --
5313 -------------------------------------------
5315 procedure Build_Instance_Compilation_Unit_Nodes
5320 Decl_Cunit : Node_Id;
5321 Body_Cunit : Node_Id;
5323 New_Main : constant Entity_Id := Defining_Entity (Act_Decl);
5324 Old_Main : constant Entity_Id := Cunit_Entity (Main_Unit);
5327 -- A new compilation unit node is built for the instance declaration
5330 Make_Compilation_Unit (Sloc (N),
5331 Context_Items => Empty_List,
5333 Aux_Decls_Node => Make_Compilation_Unit_Aux (Sloc (N)));
5335 Set_Parent_Spec (Act_Decl, Parent_Spec (N));
5337 -- The new compilation unit is linked to its body, but both share the
5338 -- same file, so we do not set Body_Required on the new unit so as not
5339 -- to create a spurious dependency on a non-existent body in the ali.
5340 -- This simplifies CodePeer unit traversal.
5342 -- We use the original instantiation compilation unit as the resulting
5343 -- compilation unit of the instance, since this is the main unit.
5345 Rewrite (N, Act_Body);
5347 -- Propagate the aspect specifications from the package body template to
5348 -- the instantiated version of the package body.
5350 if Has_Aspects (Act_Body) then
5351 Set_Aspect_Specifications
5352 (N, New_Copy_List_Tree (Aspect_Specifications (Act_Body)));
5355 Body_Cunit := Parent (N);
5357 -- The two compilation unit nodes are linked by the Library_Unit field
5359 Set_Library_Unit (Decl_Cunit, Body_Cunit);
5360 Set_Library_Unit (Body_Cunit, Decl_Cunit);
5362 -- Preserve the private nature of the package if needed
5364 Set_Private_Present (Decl_Cunit, Private_Present (Body_Cunit));
5366 -- If the instance is not the main unit, its context, categorization
5367 -- and elaboration entity are not relevant to the compilation.
5369 if Body_Cunit /= Cunit (Main_Unit) then
5370 Make_Instance_Unit (Body_Cunit, In_Main => False);
5374 -- The context clause items on the instantiation, which are now attached
5375 -- to the body compilation unit (since the body overwrote the original
5376 -- instantiation node), semantically belong on the spec, so copy them
5377 -- there. It's harmless to leave them on the body as well. In fact one
5378 -- could argue that they belong in both places.
5380 Citem := First (Context_Items (Body_Cunit));
5381 while Present (Citem) loop
5382 Append (New_Copy (Citem), Context_Items (Decl_Cunit));
5386 -- Propagate categorization flags on packages, so that they appear in
5387 -- the ali file for the spec of the unit.
5389 if Ekind (New_Main) = E_Package then
5390 Set_Is_Pure (Old_Main, Is_Pure (New_Main));
5391 Set_Is_Preelaborated (Old_Main, Is_Preelaborated (New_Main));
5392 Set_Is_Remote_Types (Old_Main, Is_Remote_Types (New_Main));
5393 Set_Is_Shared_Passive (Old_Main, Is_Shared_Passive (New_Main));
5394 Set_Is_Remote_Call_Interface
5395 (Old_Main, Is_Remote_Call_Interface (New_Main));
5398 -- Make entry in Units table, so that binder can generate call to
5399 -- elaboration procedure for body, if any.
5401 Make_Instance_Unit (Body_Cunit, In_Main => True);
5402 Main_Unit_Entity := New_Main;
5403 Set_Cunit_Entity (Main_Unit, Main_Unit_Entity);
5405 -- Build elaboration entity, since the instance may certainly generate
5406 -- elaboration code requiring a flag for protection.
5408 Build_Elaboration_Entity (Decl_Cunit, New_Main);
5409 end Build_Instance_Compilation_Unit_Nodes;
5411 -----------------------------
5412 -- Check_Access_Definition --
5413 -----------------------------
5415 procedure Check_Access_Definition (N : Node_Id) is
5418 (Ada_Version >= Ada_2005 and then Present (Access_Definition (N)));
5420 end Check_Access_Definition;
5422 -----------------------------------
5423 -- Check_Formal_Package_Instance --
5424 -----------------------------------
5426 -- If the formal has specific parameters, they must match those of the
5427 -- actual. Both of them are instances, and the renaming declarations for
5428 -- their formal parameters appear in the same order in both. The analyzed
5429 -- formal has been analyzed in the context of the current instance.
5431 procedure Check_Formal_Package_Instance
5432 (Formal_Pack : Entity_Id;
5433 Actual_Pack : Entity_Id)
5435 E1 : Entity_Id := First_Entity (Actual_Pack);
5436 E2 : Entity_Id := First_Entity (Formal_Pack);
5441 procedure Check_Mismatch (B : Boolean);
5442 -- Common error routine for mismatch between the parameters of the
5443 -- actual instance and those of the formal package.
5445 function Same_Instantiated_Constant (E1, E2 : Entity_Id) return Boolean;
5446 -- The formal may come from a nested formal package, and the actual may
5447 -- have been constant-folded. To determine whether the two denote the
5448 -- same entity we may have to traverse several definitions to recover
5449 -- the ultimate entity that they refer to.
5451 function Same_Instantiated_Variable (E1, E2 : Entity_Id) return Boolean;
5452 -- Similarly, if the formal comes from a nested formal package, the
5453 -- actual may designate the formal through multiple renamings, which
5454 -- have to be followed to determine the original variable in question.
5456 --------------------
5457 -- Check_Mismatch --
5458 --------------------
5460 procedure Check_Mismatch (B : Boolean) is
5461 Kind : constant Node_Kind := Nkind (Parent (E2));
5464 if Kind = N_Formal_Type_Declaration then
5467 elsif Nkind_In (Kind, N_Formal_Object_Declaration,
5468 N_Formal_Package_Declaration)
5469 or else Kind in N_Formal_Subprogram_Declaration
5475 ("actual for & in actual instance does not match formal",
5476 Parent (Actual_Pack), E1);
5480 --------------------------------
5481 -- Same_Instantiated_Constant --
5482 --------------------------------
5484 function Same_Instantiated_Constant
5485 (E1, E2 : Entity_Id) return Boolean
5491 while Present (Ent) loop
5495 elsif Ekind (Ent) /= E_Constant then
5498 elsif Is_Entity_Name (Constant_Value (Ent)) then
5499 if Entity (Constant_Value (Ent)) = E1 then
5502 Ent := Entity (Constant_Value (Ent));
5505 -- The actual may be a constant that has been folded. Recover
5508 elsif Is_Entity_Name (Original_Node (Constant_Value (Ent))) then
5509 Ent := Entity (Original_Node (Constant_Value (Ent)));
5517 end Same_Instantiated_Constant;
5519 --------------------------------
5520 -- Same_Instantiated_Variable --
5521 --------------------------------
5523 function Same_Instantiated_Variable
5524 (E1, E2 : Entity_Id) return Boolean
5526 function Original_Entity (E : Entity_Id) return Entity_Id;
5527 -- Follow chain of renamings to the ultimate ancestor
5529 ---------------------
5530 -- Original_Entity --
5531 ---------------------
5533 function Original_Entity (E : Entity_Id) return Entity_Id is
5538 while Nkind (Parent (Orig)) = N_Object_Renaming_Declaration
5539 and then Present (Renamed_Object (Orig))
5540 and then Is_Entity_Name (Renamed_Object (Orig))
5542 Orig := Entity (Renamed_Object (Orig));
5546 end Original_Entity;
5548 -- Start of processing for Same_Instantiated_Variable
5551 return Ekind (E1) = Ekind (E2)
5552 and then Original_Entity (E1) = Original_Entity (E2);
5553 end Same_Instantiated_Variable;
5555 -- Start of processing for Check_Formal_Package_Instance
5558 while Present (E1) and then Present (E2) loop
5559 exit when Ekind (E1) = E_Package
5560 and then Renamed_Entity (E1) = Renamed_Entity (Actual_Pack);
5562 -- If the formal is the renaming of the formal package, this
5563 -- is the end of its formal part, which may occur before the
5564 -- end of the formal part in the actual in the presence of
5565 -- defaulted parameters in the formal package.
5567 exit when Nkind (Parent (E2)) = N_Package_Renaming_Declaration
5568 and then Renamed_Entity (E2) = Scope (E2);
5570 -- The analysis of the actual may generate additional internal
5571 -- entities. If the formal is defaulted, there is no corresponding
5572 -- analysis and the internal entities must be skipped, until we
5573 -- find corresponding entities again.
5575 if Comes_From_Source (E2)
5576 and then not Comes_From_Source (E1)
5577 and then Chars (E1) /= Chars (E2)
5579 while Present (E1) and then Chars (E1) /= Chars (E2) loop
5587 -- If the formal entity comes from a formal declaration, it was
5588 -- defaulted in the formal package, and no check is needed on it.
5590 elsif Nkind (Parent (E2)) = N_Formal_Object_Declaration then
5593 -- Ditto for defaulted formal subprograms.
5595 elsif Is_Overloadable (E1)
5596 and then Nkind (Unit_Declaration_Node (E2)) in
5597 N_Formal_Subprogram_Declaration
5601 elsif Is_Type (E1) then
5603 -- Subtypes must statically match. E1, E2 are the local entities
5604 -- that are subtypes of the actuals. Itypes generated for other
5605 -- parameters need not be checked, the check will be performed
5606 -- on the parameters themselves.
5608 -- If E2 is a formal type declaration, it is a defaulted parameter
5609 -- and needs no checking.
5611 if not Is_Itype (E1) and then not Is_Itype (E2) then
5614 or else Etype (E1) /= Etype (E2)
5615 or else not Subtypes_Statically_Match (E1, E2));
5618 elsif Ekind (E1) = E_Constant then
5620 -- IN parameters must denote the same static value, or the same
5621 -- constant, or the literal null.
5623 Expr1 := Expression (Parent (E1));
5625 if Ekind (E2) /= E_Constant then
5626 Check_Mismatch (True);
5629 Expr2 := Expression (Parent (E2));
5632 if Is_OK_Static_Expression (Expr1) then
5633 if not Is_OK_Static_Expression (Expr2) then
5634 Check_Mismatch (True);
5636 elsif Is_Discrete_Type (Etype (E1)) then
5638 V1 : constant Uint := Expr_Value (Expr1);
5639 V2 : constant Uint := Expr_Value (Expr2);
5641 Check_Mismatch (V1 /= V2);
5644 elsif Is_Real_Type (Etype (E1)) then
5646 V1 : constant Ureal := Expr_Value_R (Expr1);
5647 V2 : constant Ureal := Expr_Value_R (Expr2);
5649 Check_Mismatch (V1 /= V2);
5652 elsif Is_String_Type (Etype (E1))
5653 and then Nkind (Expr1) = N_String_Literal
5655 if Nkind (Expr2) /= N_String_Literal then
5656 Check_Mismatch (True);
5659 (not String_Equal (Strval (Expr1), Strval (Expr2)));
5663 elsif Is_Entity_Name (Expr1) then
5664 if Is_Entity_Name (Expr2) then
5665 if Entity (Expr1) = Entity (Expr2) then
5669 (not Same_Instantiated_Constant
5670 (Entity (Expr1), Entity (Expr2)));
5674 Check_Mismatch (True);
5677 elsif Is_Entity_Name (Original_Node (Expr1))
5678 and then Is_Entity_Name (Expr2)
5679 and then Same_Instantiated_Constant
5680 (Entity (Original_Node (Expr1)), Entity (Expr2))
5684 elsif Nkind (Expr1) = N_Null then
5685 Check_Mismatch (Nkind (Expr1) /= N_Null);
5688 Check_Mismatch (True);
5691 elsif Ekind (E1) = E_Variable then
5692 Check_Mismatch (not Same_Instantiated_Variable (E1, E2));
5694 elsif Ekind (E1) = E_Package then
5696 (Ekind (E1) /= Ekind (E2)
5697 or else Renamed_Object (E1) /= Renamed_Object (E2));
5699 elsif Is_Overloadable (E1) then
5701 -- Verify that the actual subprograms match. Note that actuals
5702 -- that are attributes are rewritten as subprograms. If the
5703 -- subprogram in the formal package is defaulted, no check is
5704 -- needed. Note that this can only happen in Ada 2005 when the
5705 -- formal package can be partially parameterized.
5707 if Nkind (Unit_Declaration_Node (E1)) =
5708 N_Subprogram_Renaming_Declaration
5709 and then From_Default (Unit_Declaration_Node (E1))
5713 -- If the formal package has an "others" box association that
5714 -- covers this formal, there is no need for a check either.
5716 elsif Nkind (Unit_Declaration_Node (E2)) in
5717 N_Formal_Subprogram_Declaration
5718 and then Box_Present (Unit_Declaration_Node (E2))
5722 -- No check needed if subprogram is a defaulted null procedure
5724 elsif No (Alias (E2))
5725 and then Ekind (E2) = E_Procedure
5727 Null_Present (Specification (Unit_Declaration_Node (E2)))
5731 -- Otherwise the actual in the formal and the actual in the
5732 -- instantiation of the formal must match, up to renamings.
5736 (Ekind (E2) /= Ekind (E1) or else (Alias (E1)) /= Alias (E2));
5740 raise Program_Error;
5747 end Check_Formal_Package_Instance;
5749 ---------------------------
5750 -- Check_Formal_Packages --
5751 ---------------------------
5753 procedure Check_Formal_Packages (P_Id : Entity_Id) is
5755 Formal_P : Entity_Id;
5758 -- Iterate through the declarations in the instance, looking for package
5759 -- renaming declarations that denote instances of formal packages. Stop
5760 -- when we find the renaming of the current package itself. The
5761 -- declaration for a formal package without a box is followed by an
5762 -- internal entity that repeats the instantiation.
5764 E := First_Entity (P_Id);
5765 while Present (E) loop
5766 if Ekind (E) = E_Package then
5767 if Renamed_Object (E) = P_Id then
5770 elsif Nkind (Parent (E)) /= N_Package_Renaming_Declaration then
5773 elsif not Box_Present (Parent (Associated_Formal_Package (E))) then
5774 Formal_P := Next_Entity (E);
5775 Check_Formal_Package_Instance (Formal_P, E);
5777 -- After checking, remove the internal validating package. It
5778 -- is only needed for semantic checks, and as it may contain
5779 -- generic formal declarations it should not reach gigi.
5781 Remove (Unit_Declaration_Node (Formal_P));
5787 end Check_Formal_Packages;
5789 ---------------------------------
5790 -- Check_Forward_Instantiation --
5791 ---------------------------------
5793 procedure Check_Forward_Instantiation (Decl : Node_Id) is
5795 Gen_Comp : Entity_Id := Cunit_Entity (Get_Source_Unit (Decl));
5798 -- The instantiation appears before the generic body if we are in the
5799 -- scope of the unit containing the generic, either in its spec or in
5800 -- the package body, and before the generic body.
5802 if Ekind (Gen_Comp) = E_Package_Body then
5803 Gen_Comp := Spec_Entity (Gen_Comp);
5806 if In_Open_Scopes (Gen_Comp)
5807 and then No (Corresponding_Body (Decl))
5812 and then not Is_Compilation_Unit (S)
5813 and then not Is_Child_Unit (S)
5815 if Ekind (S) = E_Package then
5816 Set_Has_Forward_Instantiation (S);
5822 end Check_Forward_Instantiation;
5824 ---------------------------
5825 -- Check_Generic_Actuals --
5826 ---------------------------
5828 -- The visibility of the actuals may be different between the point of
5829 -- generic instantiation and the instantiation of the body.
5831 procedure Check_Generic_Actuals
5832 (Instance : Entity_Id;
5833 Is_Formal_Box : Boolean)
5838 function Denotes_Previous_Actual (Typ : Entity_Id) return Boolean;
5839 -- For a formal that is an array type, the component type is often a
5840 -- previous formal in the same unit. The privacy status of the component
5841 -- type will have been examined earlier in the traversal of the
5842 -- corresponding actuals, and this status should not be modified for
5843 -- the array (sub)type itself. However, if the base type of the array
5844 -- (sub)type is private, its full view must be restored in the body to
5845 -- be consistent with subsequent index subtypes, etc.
5847 -- To detect this case we have to rescan the list of formals, which is
5848 -- usually short enough to ignore the resulting inefficiency.
5850 -----------------------------
5851 -- Denotes_Previous_Actual --
5852 -----------------------------
5854 function Denotes_Previous_Actual (Typ : Entity_Id) return Boolean is
5858 Prev := First_Entity (Instance);
5859 while Present (Prev) loop
5861 and then Nkind (Parent (Prev)) = N_Subtype_Declaration
5862 and then Is_Entity_Name (Subtype_Indication (Parent (Prev)))
5863 and then Entity (Subtype_Indication (Parent (Prev))) = Typ
5876 end Denotes_Previous_Actual;
5878 -- Start of processing for Check_Generic_Actuals
5881 E := First_Entity (Instance);
5882 while Present (E) loop
5884 and then Nkind (Parent (E)) = N_Subtype_Declaration
5885 and then Scope (Etype (E)) /= Instance
5886 and then Is_Entity_Name (Subtype_Indication (Parent (E)))
5888 if Is_Array_Type (E)
5889 and then not Is_Private_Type (Etype (E))
5890 and then Denotes_Previous_Actual (Component_Type (E))
5894 Check_Private_View (Subtype_Indication (Parent (E)));
5897 Set_Is_Generic_Actual_Type (E, True);
5898 Set_Is_Hidden (E, False);
5899 Set_Is_Potentially_Use_Visible (E,
5902 -- We constructed the generic actual type as a subtype of the
5903 -- supplied type. This means that it normally would not inherit
5904 -- subtype specific attributes of the actual, which is wrong for
5905 -- the generic case.
5907 Astype := Ancestor_Subtype (E);
5911 -- This can happen when E is an itype that is the full view of
5912 -- a private type completed, e.g. with a constrained array. In
5913 -- that case, use the first subtype, which will carry size
5914 -- information. The base type itself is unconstrained and will
5917 Astype := First_Subtype (E);
5920 Set_Size_Info (E, (Astype));
5921 Set_RM_Size (E, RM_Size (Astype));
5922 Set_First_Rep_Item (E, First_Rep_Item (Astype));
5924 if Is_Discrete_Or_Fixed_Point_Type (E) then
5925 Set_RM_Size (E, RM_Size (Astype));
5927 -- In nested instances, the base type of an access actual may
5928 -- itself be private, and need to be exchanged.
5930 elsif Is_Access_Type (E)
5931 and then Is_Private_Type (Etype (E))
5934 (New_Occurrence_Of (Etype (E), Sloc (Instance)));
5937 elsif Ekind (E) = E_Package then
5939 -- If this is the renaming for the current instance, we're done.
5940 -- Otherwise it is a formal package. If the corresponding formal
5941 -- was declared with a box, the (instantiations of the) generic
5942 -- formal part are also visible. Otherwise, ignore the entity
5943 -- created to validate the actuals.
5945 if Renamed_Object (E) = Instance then
5948 elsif Nkind (Parent (E)) /= N_Package_Renaming_Declaration then
5951 -- The visibility of a formal of an enclosing generic is already
5954 elsif Denotes_Formal_Package (E) then
5957 elsif Present (Associated_Formal_Package (E))
5958 and then not Is_Generic_Formal (E)
5960 if Box_Present (Parent (Associated_Formal_Package (E))) then
5961 Check_Generic_Actuals (Renamed_Object (E), True);
5964 Check_Generic_Actuals (Renamed_Object (E), False);
5967 Set_Is_Hidden (E, False);
5970 -- If this is a subprogram instance (in a wrapper package) the
5971 -- actual is fully visible.
5973 elsif Is_Wrapper_Package (Instance) then
5974 Set_Is_Hidden (E, False);
5976 -- If the formal package is declared with a box, or if the formal
5977 -- parameter is defaulted, it is visible in the body.
5979 elsif Is_Formal_Box or else Is_Visible_Formal (E) then
5980 Set_Is_Hidden (E, False);
5983 if Ekind (E) = E_Constant then
5985 -- If the type of the actual is a private type declared in the
5986 -- enclosing scope of the generic unit, the body of the generic
5987 -- sees the full view of the type (because it has to appear in
5988 -- the corresponding package body). If the type is private now,
5989 -- exchange views to restore the proper visiblity in the instance.
5992 Typ : constant Entity_Id := Base_Type (Etype (E));
5993 -- The type of the actual
5998 Parent_Scope : Entity_Id;
5999 -- The enclosing scope of the generic unit
6002 if Is_Wrapper_Package (Instance) then
6006 (Unit_Declaration_Node
6007 (Related_Instance (Instance))));
6010 Generic_Parent (Package_Specification (Instance));
6013 Parent_Scope := Scope (Gen_Id);
6015 -- The exchange is only needed if the generic is defined
6016 -- within a package which is not a common ancestor of the
6017 -- scope of the instance, and is not already in scope.
6019 if Is_Private_Type (Typ)
6020 and then Scope (Typ) = Parent_Scope
6021 and then Scope (Instance) /= Parent_Scope
6022 and then Ekind (Parent_Scope) = E_Package
6023 and then not Is_Child_Unit (Gen_Id)
6027 -- If the type of the entity is a subtype, it may also have
6028 -- to be made visible, together with the base type of its
6029 -- full view, after exchange.
6031 if Is_Private_Type (Etype (E)) then
6032 Switch_View (Etype (E));
6033 Switch_View (Base_Type (Etype (E)));
6041 end Check_Generic_Actuals;
6043 ------------------------------
6044 -- Check_Generic_Child_Unit --
6045 ------------------------------
6047 procedure Check_Generic_Child_Unit
6049 Parent_Installed : in out Boolean)
6051 Loc : constant Source_Ptr := Sloc (Gen_Id);
6052 Gen_Par : Entity_Id := Empty;
6054 Inst_Par : Entity_Id;
6057 function Find_Generic_Child
6059 Id : Node_Id) return Entity_Id;
6060 -- Search generic parent for possible child unit with the given name
6062 function In_Enclosing_Instance return Boolean;
6063 -- Within an instance of the parent, the child unit may be denoted by
6064 -- a simple name, or an abbreviated expanded name. Examine enclosing
6065 -- scopes to locate a possible parent instantiation.
6067 ------------------------
6068 -- Find_Generic_Child --
6069 ------------------------
6071 function Find_Generic_Child
6073 Id : Node_Id) return Entity_Id
6078 -- If entity of name is already set, instance has already been
6079 -- resolved, e.g. in an enclosing instantiation.
6081 if Present (Entity (Id)) then
6082 if Scope (Entity (Id)) = Scop then
6089 E := First_Entity (Scop);
6090 while Present (E) loop
6091 if Chars (E) = Chars (Id)
6092 and then Is_Child_Unit (E)
6094 if Is_Child_Unit (E)
6095 and then not Is_Visible_Lib_Unit (E)
6098 ("generic child unit& is not visible", Gen_Id, E);
6110 end Find_Generic_Child;
6112 ---------------------------
6113 -- In_Enclosing_Instance --
6114 ---------------------------
6116 function In_Enclosing_Instance return Boolean is
6117 Enclosing_Instance : Node_Id;
6118 Instance_Decl : Node_Id;
6121 -- We do not inline any call that contains instantiations, except
6122 -- for instantiations of Unchecked_Conversion, so if we are within
6123 -- an inlined body the current instance does not require parents.
6125 if In_Inlined_Body then
6126 pragma Assert (Chars (Gen_Id) = Name_Unchecked_Conversion);
6130 -- Loop to check enclosing scopes
6132 Enclosing_Instance := Current_Scope;
6133 while Present (Enclosing_Instance) loop
6134 Instance_Decl := Unit_Declaration_Node (Enclosing_Instance);
6136 if Ekind (Enclosing_Instance) = E_Package
6137 and then Is_Generic_Instance (Enclosing_Instance)
6139 (Generic_Parent (Specification (Instance_Decl)))
6141 -- Check whether the generic we are looking for is a child of
6144 E := Find_Generic_Child
6145 (Generic_Parent (Specification (Instance_Decl)), Gen_Id);
6146 exit when Present (E);
6152 Enclosing_Instance := Scope (Enclosing_Instance);
6164 Make_Expanded_Name (Loc,
6166 Prefix => New_Occurrence_Of (Enclosing_Instance, Loc),
6167 Selector_Name => New_Occurrence_Of (E, Loc)));
6169 Set_Entity (Gen_Id, E);
6170 Set_Etype (Gen_Id, Etype (E));
6171 Parent_Installed := False; -- Already in scope.
6174 end In_Enclosing_Instance;
6176 -- Start of processing for Check_Generic_Child_Unit
6179 -- If the name of the generic is given by a selected component, it may
6180 -- be the name of a generic child unit, and the prefix is the name of an
6181 -- instance of the parent, in which case the child unit must be visible.
6182 -- If this instance is not in scope, it must be placed there and removed
6183 -- after instantiation, because what is being instantiated is not the
6184 -- original child, but the corresponding child present in the instance
6187 -- If the child is instantiated within the parent, it can be given by
6188 -- a simple name. In this case the instance is already in scope, but
6189 -- the child generic must be recovered from the generic parent as well.
6191 if Nkind (Gen_Id) = N_Selected_Component then
6192 S := Selector_Name (Gen_Id);
6193 Analyze (Prefix (Gen_Id));
6194 Inst_Par := Entity (Prefix (Gen_Id));
6196 if Ekind (Inst_Par) = E_Package
6197 and then Present (Renamed_Object (Inst_Par))
6199 Inst_Par := Renamed_Object (Inst_Par);
6202 if Ekind (Inst_Par) = E_Package then
6203 if Nkind (Parent (Inst_Par)) = N_Package_Specification then
6204 Gen_Par := Generic_Parent (Parent (Inst_Par));
6206 elsif Nkind (Parent (Inst_Par)) = N_Defining_Program_Unit_Name
6208 Nkind (Parent (Parent (Inst_Par))) = N_Package_Specification
6210 Gen_Par := Generic_Parent (Parent (Parent (Inst_Par)));
6213 elsif Ekind (Inst_Par) = E_Generic_Package
6214 and then Nkind (Parent (Gen_Id)) = N_Formal_Package_Declaration
6216 -- A formal package may be a real child package, and not the
6217 -- implicit instance within a parent. In this case the child is
6218 -- not visible and has to be retrieved explicitly as well.
6220 Gen_Par := Inst_Par;
6223 if Present (Gen_Par) then
6225 -- The prefix denotes an instantiation. The entity itself may be a
6226 -- nested generic, or a child unit.
6228 E := Find_Generic_Child (Gen_Par, S);
6231 Change_Selected_Component_To_Expanded_Name (Gen_Id);
6232 Set_Entity (Gen_Id, E);
6233 Set_Etype (Gen_Id, Etype (E));
6235 Set_Etype (S, Etype (E));
6237 -- Indicate that this is a reference to the parent
6239 if In_Extended_Main_Source_Unit (Gen_Id) then
6240 Set_Is_Instantiated (Inst_Par);
6243 -- A common mistake is to replicate the naming scheme of a
6244 -- hierarchy by instantiating a generic child directly, rather
6245 -- than the implicit child in a parent instance:
6247 -- generic .. package Gpar is ..
6248 -- generic .. package Gpar.Child is ..
6249 -- package Par is new Gpar ();
6252 -- package Par.Child is new Gpar.Child ();
6253 -- rather than Par.Child
6255 -- In this case the instantiation is within Par, which is an
6256 -- instance, but Gpar does not denote Par because we are not IN
6257 -- the instance of Gpar, so this is illegal. The test below
6258 -- recognizes this particular case.
6260 if Is_Child_Unit (E)
6261 and then not Comes_From_Source (Entity (Prefix (Gen_Id)))
6262 and then (not In_Instance
6263 or else Nkind (Parent (Parent (Gen_Id))) =
6267 ("prefix of generic child unit must be instance of parent",
6271 if not In_Open_Scopes (Inst_Par)
6272 and then Nkind (Parent (Gen_Id)) not in
6273 N_Generic_Renaming_Declaration
6275 Install_Parent (Inst_Par);
6276 Parent_Installed := True;
6278 elsif In_Open_Scopes (Inst_Par) then
6280 -- If the parent is already installed, install the actuals
6281 -- for its formal packages. This is necessary when the child
6282 -- instance is a child of the parent instance: in this case,
6283 -- the parent is placed on the scope stack but the formal
6284 -- packages are not made visible.
6286 Install_Formal_Packages (Inst_Par);
6290 -- If the generic parent does not contain an entity that
6291 -- corresponds to the selector, the instance doesn't either.
6292 -- Analyzing the node will yield the appropriate error message.
6293 -- If the entity is not a child unit, then it is an inner
6294 -- generic in the parent.
6302 if Is_Child_Unit (Entity (Gen_Id))
6304 Nkind (Parent (Gen_Id)) not in N_Generic_Renaming_Declaration
6305 and then not In_Open_Scopes (Inst_Par)
6307 Install_Parent (Inst_Par);
6308 Parent_Installed := True;
6310 -- The generic unit may be the renaming of the implicit child
6311 -- present in an instance. In that case the parent instance is
6312 -- obtained from the name of the renamed entity.
6314 elsif Ekind (Entity (Gen_Id)) = E_Generic_Package
6315 and then Present (Renamed_Entity (Entity (Gen_Id)))
6316 and then Is_Child_Unit (Renamed_Entity (Entity (Gen_Id)))
6319 Renamed_Package : constant Node_Id :=
6320 Name (Parent (Entity (Gen_Id)));
6322 if Nkind (Renamed_Package) = N_Expanded_Name then
6323 Inst_Par := Entity (Prefix (Renamed_Package));
6324 Install_Parent (Inst_Par);
6325 Parent_Installed := True;
6331 elsif Nkind (Gen_Id) = N_Expanded_Name then
6333 -- Entity already present, analyze prefix, whose meaning may be
6334 -- an instance in the current context. If it is an instance of
6335 -- a relative within another, the proper parent may still have
6336 -- to be installed, if they are not of the same generation.
6338 Analyze (Prefix (Gen_Id));
6340 -- In the unlikely case that a local declaration hides the name
6341 -- of the parent package, locate it on the homonym chain. If the
6342 -- context is an instance of the parent, the renaming entity is
6345 Inst_Par := Entity (Prefix (Gen_Id));
6346 while Present (Inst_Par)
6347 and then not Is_Package_Or_Generic_Package (Inst_Par)
6349 Inst_Par := Homonym (Inst_Par);
6352 pragma Assert (Present (Inst_Par));
6353 Set_Entity (Prefix (Gen_Id), Inst_Par);
6355 if In_Enclosing_Instance then
6358 elsif Present (Entity (Gen_Id))
6359 and then Is_Child_Unit (Entity (Gen_Id))
6360 and then not In_Open_Scopes (Inst_Par)
6362 Install_Parent (Inst_Par);
6363 Parent_Installed := True;
6366 elsif In_Enclosing_Instance then
6368 -- The child unit is found in some enclosing scope
6375 -- If this is the renaming of the implicit child in a parent
6376 -- instance, recover the parent name and install it.
6378 if Is_Entity_Name (Gen_Id) then
6379 E := Entity (Gen_Id);
6381 if Is_Generic_Unit (E)
6382 and then Nkind (Parent (E)) in N_Generic_Renaming_Declaration
6383 and then Is_Child_Unit (Renamed_Object (E))
6384 and then Is_Generic_Unit (Scope (Renamed_Object (E)))
6385 and then Nkind (Name (Parent (E))) = N_Expanded_Name
6387 Rewrite (Gen_Id, New_Copy_Tree (Name (Parent (E))));
6388 Inst_Par := Entity (Prefix (Gen_Id));
6390 if not In_Open_Scopes (Inst_Par) then
6391 Install_Parent (Inst_Par);
6392 Parent_Installed := True;
6395 -- If it is a child unit of a non-generic parent, it may be
6396 -- use-visible and given by a direct name. Install parent as
6399 elsif Is_Generic_Unit (E)
6400 and then Is_Child_Unit (E)
6402 Nkind (Parent (Gen_Id)) not in N_Generic_Renaming_Declaration
6403 and then not Is_Generic_Unit (Scope (E))
6405 if not In_Open_Scopes (Scope (E)) then
6406 Install_Parent (Scope (E));
6407 Parent_Installed := True;
6412 end Check_Generic_Child_Unit;
6414 -----------------------------
6415 -- Check_Hidden_Child_Unit --
6416 -----------------------------
6418 procedure Check_Hidden_Child_Unit
6420 Gen_Unit : Entity_Id;
6421 Act_Decl_Id : Entity_Id)
6423 Gen_Id : constant Node_Id := Name (N);
6426 if Is_Child_Unit (Gen_Unit)
6427 and then Is_Child_Unit (Act_Decl_Id)
6428 and then Nkind (Gen_Id) = N_Expanded_Name
6429 and then Entity (Prefix (Gen_Id)) = Scope (Act_Decl_Id)
6430 and then Chars (Gen_Unit) = Chars (Act_Decl_Id)
6432 Error_Msg_Node_2 := Scope (Act_Decl_Id);
6434 ("generic unit & is implicitly declared in &",
6435 Defining_Unit_Name (N), Gen_Unit);
6436 Error_Msg_N ("\instance must have different name",
6437 Defining_Unit_Name (N));
6439 end Check_Hidden_Child_Unit;
6441 ------------------------
6442 -- Check_Private_View --
6443 ------------------------
6445 procedure Check_Private_View (N : Node_Id) is
6446 T : constant Entity_Id := Etype (N);
6450 -- Exchange views if the type was not private in the generic but is
6451 -- private at the point of instantiation. Do not exchange views if
6452 -- the scope of the type is in scope. This can happen if both generic
6453 -- and instance are sibling units, or if type is defined in a parent.
6454 -- In this case the visibility of the type will be correct for all
6458 BT := Base_Type (T);
6460 if Is_Private_Type (T)
6461 and then not Has_Private_View (N)
6462 and then Present (Full_View (T))
6463 and then not In_Open_Scopes (Scope (T))
6465 -- In the generic, the full type was visible. Save the private
6466 -- entity, for subsequent exchange.
6470 elsif Has_Private_View (N)
6471 and then not Is_Private_Type (T)
6472 and then not Has_Been_Exchanged (T)
6473 and then Etype (Get_Associated_Node (N)) /= T
6475 -- Only the private declaration was visible in the generic. If
6476 -- the type appears in a subtype declaration, the subtype in the
6477 -- instance must have a view compatible with that of its parent,
6478 -- which must be exchanged (see corresponding code in Restore_
6479 -- Private_Views). Otherwise, if the type is defined in a parent
6480 -- unit, leave full visibility within instance, which is safe.
6482 if In_Open_Scopes (Scope (Base_Type (T)))
6483 and then not Is_Private_Type (Base_Type (T))
6484 and then Comes_From_Source (Base_Type (T))
6488 elsif Nkind (Parent (N)) = N_Subtype_Declaration
6489 or else not In_Private_Part (Scope (Base_Type (T)))
6491 Prepend_Elmt (T, Exchanged_Views);
6492 Exchange_Declarations (Etype (Get_Associated_Node (N)));
6495 -- For composite types with inconsistent representation exchange
6496 -- component types accordingly.
6498 elsif Is_Access_Type (T)
6499 and then Is_Private_Type (Designated_Type (T))
6500 and then not Has_Private_View (N)
6501 and then Present (Full_View (Designated_Type (T)))
6503 Switch_View (Designated_Type (T));
6505 elsif Is_Array_Type (T) then
6506 if Is_Private_Type (Component_Type (T))
6507 and then not Has_Private_View (N)
6508 and then Present (Full_View (Component_Type (T)))
6510 Switch_View (Component_Type (T));
6513 -- The normal exchange mechanism relies on the setting of a
6514 -- flag on the reference in the generic. However, an additional
6515 -- mechanism is needed for types that are not explicitly
6516 -- mentioned in the generic, but may be needed in expanded code
6517 -- in the instance. This includes component types of arrays and
6518 -- designated types of access types. This processing must also
6519 -- include the index types of arrays which we take care of here.
6526 Indx := First_Index (T);
6527 while Present (Indx) loop
6528 Typ := Base_Type (Etype (Indx));
6530 if Is_Private_Type (Typ)
6531 and then Present (Full_View (Typ))
6540 elsif Is_Private_Type (T)
6541 and then Present (Full_View (T))
6542 and then Is_Array_Type (Full_View (T))
6543 and then Is_Private_Type (Component_Type (Full_View (T)))
6547 -- Finally, a non-private subtype may have a private base type, which
6548 -- must be exchanged for consistency. This can happen when a package
6549 -- body is instantiated, when the scope stack is empty but in fact
6550 -- the subtype and the base type are declared in an enclosing scope.
6552 -- Note that in this case we introduce an inconsistency in the view
6553 -- set, because we switch the base type BT, but there could be some
6554 -- private dependent subtypes of BT which remain unswitched. Such
6555 -- subtypes might need to be switched at a later point (see specific
6556 -- provision for that case in Switch_View).
6558 elsif not Is_Private_Type (T)
6559 and then not Has_Private_View (N)
6560 and then Is_Private_Type (BT)
6561 and then Present (Full_View (BT))
6562 and then not Is_Generic_Type (BT)
6563 and then not In_Open_Scopes (BT)
6565 Prepend_Elmt (Full_View (BT), Exchanged_Views);
6566 Exchange_Declarations (BT);
6569 end Check_Private_View;
6571 -----------------------------
6572 -- Check_Hidden_Primitives --
6573 -----------------------------
6575 function Check_Hidden_Primitives (Assoc_List : List_Id) return Elist_Id is
6578 Result : Elist_Id := No_Elist;
6581 if No (Assoc_List) then
6585 -- Traverse the list of associations between formals and actuals
6586 -- searching for renamings of tagged types
6588 Actual := First (Assoc_List);
6589 while Present (Actual) loop
6590 if Nkind (Actual) = N_Subtype_Declaration then
6591 Gen_T := Generic_Parent_Type (Actual);
6593 if Present (Gen_T) and then Is_Tagged_Type (Gen_T) then
6595 -- Traverse the list of primitives of the actual types
6596 -- searching for hidden primitives that are visible in the
6597 -- corresponding generic formal; leave them visible and
6598 -- append them to Result to restore their decoration later.
6600 Install_Hidden_Primitives
6601 (Prims_List => Result,
6603 Act_T => Entity (Subtype_Indication (Actual)));
6611 end Check_Hidden_Primitives;
6613 --------------------------
6614 -- Contains_Instance_Of --
6615 --------------------------
6617 function Contains_Instance_Of
6620 N : Node_Id) return Boolean
6628 -- Verify that there are no circular instantiations. We check whether
6629 -- the unit contains an instance of the current scope or some enclosing
6630 -- scope (in case one of the instances appears in a subunit). Longer
6631 -- circularities involving subunits might seem too pathological to
6632 -- consider, but they were not too pathological for the authors of
6633 -- DEC bc30vsq, so we loop over all enclosing scopes, and mark all
6634 -- enclosing generic scopes as containing an instance.
6637 -- Within a generic subprogram body, the scope is not generic, to
6638 -- allow for recursive subprograms. Use the declaration to determine
6639 -- whether this is a generic unit.
6641 if Ekind (Scop) = E_Generic_Package
6642 or else (Is_Subprogram (Scop)
6643 and then Nkind (Unit_Declaration_Node (Scop)) =
6644 N_Generic_Subprogram_Declaration)
6646 Elmt := First_Elmt (Inner_Instances (Inner));
6648 while Present (Elmt) loop
6649 if Node (Elmt) = Scop then
6650 Error_Msg_Node_2 := Inner;
6652 ("circular Instantiation: & instantiated within &!",
6656 elsif Node (Elmt) = Inner then
6659 elsif Contains_Instance_Of (Node (Elmt), Scop, N) then
6660 Error_Msg_Node_2 := Inner;
6662 ("circular Instantiation: & instantiated within &!",
6670 -- Indicate that Inner is being instantiated within Scop
6672 Append_Elmt (Inner, Inner_Instances (Scop));
6675 if Scop = Standard_Standard then
6678 Scop := Scope (Scop);
6683 end Contains_Instance_Of;
6685 -----------------------
6686 -- Copy_Generic_Node --
6687 -----------------------
6689 function Copy_Generic_Node
6691 Parent_Id : Node_Id;
6692 Instantiating : Boolean) return Node_Id
6697 function Copy_Generic_Descendant (D : Union_Id) return Union_Id;
6698 -- Check the given value of one of the Fields referenced by the current
6699 -- node to determine whether to copy it recursively. The field may hold
6700 -- a Node_Id, a List_Id, or an Elist_Id, or a plain value (Sloc, Uint,
6701 -- Char) in which case it need not be copied.
6703 procedure Copy_Descendants;
6704 -- Common utility for various nodes
6706 function Copy_Generic_Elist (E : Elist_Id) return Elist_Id;
6707 -- Make copy of element list
6709 function Copy_Generic_List
6711 Parent_Id : Node_Id) return List_Id;
6712 -- Apply Copy_Node recursively to the members of a node list
6714 function In_Defining_Unit_Name (Nam : Node_Id) return Boolean;
6715 -- True if an identifier is part of the defining program unit name of
6716 -- a child unit. The entity of such an identifier must be kept (for
6717 -- ASIS use) even though as the name of an enclosing generic it would
6718 -- otherwise not be preserved in the generic tree.
6720 ----------------------
6721 -- Copy_Descendants --
6722 ----------------------
6724 procedure Copy_Descendants is
6726 use Atree.Unchecked_Access;
6727 -- This code section is part of the implementation of an untyped
6728 -- tree traversal, so it needs direct access to node fields.
6731 Set_Field1 (New_N, Copy_Generic_Descendant (Field1 (N)));
6732 Set_Field2 (New_N, Copy_Generic_Descendant (Field2 (N)));
6733 Set_Field3 (New_N, Copy_Generic_Descendant (Field3 (N)));
6734 Set_Field4 (New_N, Copy_Generic_Descendant (Field4 (N)));
6735 Set_Field5 (New_N, Copy_Generic_Descendant (Field5 (N)));
6736 end Copy_Descendants;
6738 -----------------------------
6739 -- Copy_Generic_Descendant --
6740 -----------------------------
6742 function Copy_Generic_Descendant (D : Union_Id) return Union_Id is
6744 if D = Union_Id (Empty) then
6747 elsif D in Node_Range then
6749 (Copy_Generic_Node (Node_Id (D), New_N, Instantiating));
6751 elsif D in List_Range then
6752 return Union_Id (Copy_Generic_List (List_Id (D), New_N));
6754 elsif D in Elist_Range then
6755 return Union_Id (Copy_Generic_Elist (Elist_Id (D)));
6757 -- Nothing else is copyable (e.g. Uint values), return as is
6762 end Copy_Generic_Descendant;
6764 ------------------------
6765 -- Copy_Generic_Elist --
6766 ------------------------
6768 function Copy_Generic_Elist (E : Elist_Id) return Elist_Id is
6775 M := First_Elmt (E);
6776 while Present (M) loop
6778 (Copy_Generic_Node (Node (M), Empty, Instantiating), L);
6787 end Copy_Generic_Elist;
6789 -----------------------
6790 -- Copy_Generic_List --
6791 -----------------------
6793 function Copy_Generic_List
6795 Parent_Id : Node_Id) return List_Id
6803 Set_Parent (New_L, Parent_Id);
6806 while Present (N) loop
6807 Append (Copy_Generic_Node (N, Empty, Instantiating), New_L);
6816 end Copy_Generic_List;
6818 ---------------------------
6819 -- In_Defining_Unit_Name --
6820 ---------------------------
6822 function In_Defining_Unit_Name (Nam : Node_Id) return Boolean is
6824 return Present (Parent (Nam))
6825 and then (Nkind (Parent (Nam)) = N_Defining_Program_Unit_Name
6827 (Nkind (Parent (Nam)) = N_Expanded_Name
6828 and then In_Defining_Unit_Name (Parent (Nam))));
6829 end In_Defining_Unit_Name;
6831 -- Start of processing for Copy_Generic_Node
6838 New_N := New_Copy (N);
6840 -- Copy aspects if present
6842 if Has_Aspects (N) then
6843 Set_Has_Aspects (New_N, False);
6844 Set_Aspect_Specifications
6845 (New_N, Copy_Generic_List (Aspect_Specifications (N), Parent_Id));
6848 if Instantiating then
6849 Adjust_Instantiation_Sloc (New_N, S_Adjustment);
6852 if not Is_List_Member (N) then
6853 Set_Parent (New_N, Parent_Id);
6856 -- If defining identifier, then all fields have been copied already
6858 if Nkind (New_N) in N_Entity then
6861 -- Special casing for identifiers and other entity names and operators
6863 elsif Nkind_In (New_N, N_Identifier,
6864 N_Character_Literal,
6867 or else Nkind (New_N) in N_Op
6869 if not Instantiating then
6871 -- Link both nodes in order to assign subsequently the entity of
6872 -- the copy to the original node, in case this is a global
6875 Set_Associated_Node (N, New_N);
6877 -- If we are within an instantiation, this is a nested generic
6878 -- that has already been analyzed at the point of definition.
6879 -- We must preserve references that were global to the enclosing
6880 -- parent at that point. Other occurrences, whether global or
6881 -- local to the current generic, must be resolved anew, so we
6882 -- reset the entity in the generic copy. A global reference has a
6883 -- smaller depth than the parent, or else the same depth in case
6884 -- both are distinct compilation units.
6886 -- A child unit is implicitly declared within the enclosing parent
6887 -- but is in fact global to it, and must be preserved.
6889 -- It is also possible for Current_Instantiated_Parent to be
6890 -- defined, and for this not to be a nested generic, namely if
6891 -- the unit is loaded through Rtsfind. In that case, the entity of
6892 -- New_N is only a link to the associated node, and not a defining
6895 -- The entities for parent units in the defining_program_unit of a
6896 -- generic child unit are established when the context of the unit
6897 -- is first analyzed, before the generic copy is made. They are
6898 -- preserved in the copy for use in ASIS queries.
6900 Ent := Entity (New_N);
6902 if No (Current_Instantiated_Parent.Gen_Id) then
6904 or else Nkind (Ent) /= N_Defining_Identifier
6905 or else not In_Defining_Unit_Name (N)
6907 Set_Associated_Node (New_N, Empty);
6912 not Nkind_In (Ent, N_Defining_Identifier,
6913 N_Defining_Character_Literal,
6914 N_Defining_Operator_Symbol)
6915 or else No (Scope (Ent))
6917 (Scope (Ent) = Current_Instantiated_Parent.Gen_Id
6918 and then not Is_Child_Unit (Ent))
6920 (Scope_Depth (Scope (Ent)) >
6921 Scope_Depth (Current_Instantiated_Parent.Gen_Id)
6923 Get_Source_Unit (Ent) =
6924 Get_Source_Unit (Current_Instantiated_Parent.Gen_Id))
6926 Set_Associated_Node (New_N, Empty);
6929 -- Case of instantiating identifier or some other name or operator
6932 -- If the associated node is still defined, the entity in it
6933 -- is global, and must be copied to the instance. If this copy
6934 -- is being made for a body to inline, it is applied to an
6935 -- instantiated tree, and the entity is already present and
6936 -- must be also preserved.
6939 Assoc : constant Node_Id := Get_Associated_Node (N);
6942 if Present (Assoc) then
6943 if Nkind (Assoc) = Nkind (N) then
6944 Set_Entity (New_N, Entity (Assoc));
6945 Check_Private_View (N);
6947 -- The name in the call may be a selected component if the
6948 -- call has not been analyzed yet, as may be the case for
6949 -- pre/post conditions in a generic unit.
6951 elsif Nkind (Assoc) = N_Function_Call
6952 and then Is_Entity_Name (Name (Assoc))
6954 Set_Entity (New_N, Entity (Name (Assoc)));
6956 elsif Nkind_In (Assoc, N_Defining_Identifier,
6957 N_Defining_Character_Literal,
6958 N_Defining_Operator_Symbol)
6959 and then Expander_Active
6961 -- Inlining case: we are copying a tree that contains
6962 -- global entities, which are preserved in the copy to be
6963 -- used for subsequent inlining.
6968 Set_Entity (New_N, Empty);
6974 -- For expanded name, we must copy the Prefix and Selector_Name
6976 if Nkind (N) = N_Expanded_Name then
6978 (New_N, Copy_Generic_Node (Prefix (N), New_N, Instantiating));
6980 Set_Selector_Name (New_N,
6981 Copy_Generic_Node (Selector_Name (N), New_N, Instantiating));
6983 -- For operators, we must copy the right operand
6985 elsif Nkind (N) in N_Op then
6986 Set_Right_Opnd (New_N,
6987 Copy_Generic_Node (Right_Opnd (N), New_N, Instantiating));
6989 -- And for binary operators, the left operand as well
6991 if Nkind (N) in N_Binary_Op then
6992 Set_Left_Opnd (New_N,
6993 Copy_Generic_Node (Left_Opnd (N), New_N, Instantiating));
6997 -- Special casing for stubs
6999 elsif Nkind (N) in N_Body_Stub then
7001 -- In any case, we must copy the specification or defining
7002 -- identifier as appropriate.
7004 if Nkind (N) = N_Subprogram_Body_Stub then
7005 Set_Specification (New_N,
7006 Copy_Generic_Node (Specification (N), New_N, Instantiating));
7009 Set_Defining_Identifier (New_N,
7011 (Defining_Identifier (N), New_N, Instantiating));
7014 -- If we are not instantiating, then this is where we load and
7015 -- analyze subunits, i.e. at the point where the stub occurs. A
7016 -- more permissive system might defer this analysis to the point
7017 -- of instantiation, but this seems too complicated for now.
7019 if not Instantiating then
7021 Subunit_Name : constant Unit_Name_Type := Get_Unit_Name (N);
7023 Unum : Unit_Number_Type;
7027 -- Make sure that, if it is a subunit of the main unit that is
7028 -- preprocessed and if -gnateG is specified, the preprocessed
7029 -- file will be written.
7031 Lib.Analysing_Subunit_Of_Main :=
7032 Lib.In_Extended_Main_Source_Unit (N);
7035 (Load_Name => Subunit_Name,
7039 Lib.Analysing_Subunit_Of_Main := False;
7041 -- If the proper body is not found, a warning message will be
7042 -- emitted when analyzing the stub, or later at the point of
7043 -- instantiation. Here we just leave the stub as is.
7045 if Unum = No_Unit then
7046 Subunits_Missing := True;
7047 goto Subunit_Not_Found;
7050 Subunit := Cunit (Unum);
7052 if Nkind (Unit (Subunit)) /= N_Subunit then
7054 ("found child unit instead of expected SEPARATE subunit",
7056 Error_Msg_Sloc := Sloc (N);
7057 Error_Msg_N ("\to complete stub #", Subunit);
7058 goto Subunit_Not_Found;
7061 -- We must create a generic copy of the subunit, in order to
7062 -- perform semantic analysis on it, and we must replace the
7063 -- stub in the original generic unit with the subunit, in order
7064 -- to preserve non-local references within.
7066 -- Only the proper body needs to be copied. Library_Unit and
7067 -- context clause are simply inherited by the generic copy.
7068 -- Note that the copy (which may be recursive if there are
7069 -- nested subunits) must be done first, before attaching it to
7070 -- the enclosing generic.
7074 (Proper_Body (Unit (Subunit)),
7075 Empty, Instantiating => False);
7077 -- Now place the original proper body in the original generic
7078 -- unit. This is a body, not a compilation unit.
7080 Rewrite (N, Proper_Body (Unit (Subunit)));
7081 Set_Is_Compilation_Unit (Defining_Entity (N), False);
7082 Set_Was_Originally_Stub (N);
7084 -- Finally replace the body of the subunit with its copy, and
7085 -- make this new subunit into the library unit of the generic
7086 -- copy, which does not have stubs any longer.
7088 Set_Proper_Body (Unit (Subunit), New_Body);
7089 Set_Library_Unit (New_N, Subunit);
7090 Inherit_Context (Unit (Subunit), N);
7093 -- If we are instantiating, this must be an error case, since
7094 -- otherwise we would have replaced the stub node by the proper body
7095 -- that corresponds. So just ignore it in the copy (i.e. we have
7096 -- copied it, and that is good enough).
7102 <<Subunit_Not_Found>> null;
7104 -- If the node is a compilation unit, it is the subunit of a stub, which
7105 -- has been loaded already (see code below). In this case, the library
7106 -- unit field of N points to the parent unit (which is a compilation
7107 -- unit) and need not (and cannot) be copied.
7109 -- When the proper body of the stub is analyzed, the library_unit link
7110 -- is used to establish the proper context (see sem_ch10).
7112 -- The other fields of a compilation unit are copied as usual
7114 elsif Nkind (N) = N_Compilation_Unit then
7116 -- This code can only be executed when not instantiating, because in
7117 -- the copy made for an instantiation, the compilation unit node has
7118 -- disappeared at the point that a stub is replaced by its proper
7121 pragma Assert (not Instantiating);
7123 Set_Context_Items (New_N,
7124 Copy_Generic_List (Context_Items (N), New_N));
7127 Copy_Generic_Node (Unit (N), New_N, False));
7129 Set_First_Inlined_Subprogram (New_N,
7131 (First_Inlined_Subprogram (N), New_N, False));
7133 Set_Aux_Decls_Node (New_N,
7134 Copy_Generic_Node (Aux_Decls_Node (N), New_N, False));
7136 -- For an assignment node, the assignment is known to be semantically
7137 -- legal if we are instantiating the template. This avoids incorrect
7138 -- diagnostics in generated code.
7140 elsif Nkind (N) = N_Assignment_Statement then
7142 -- Copy name and expression fields in usual manner
7145 Copy_Generic_Node (Name (N), New_N, Instantiating));
7147 Set_Expression (New_N,
7148 Copy_Generic_Node (Expression (N), New_N, Instantiating));
7150 if Instantiating then
7151 Set_Assignment_OK (Name (New_N), True);
7154 elsif Nkind_In (N, N_Aggregate, N_Extension_Aggregate) then
7155 if not Instantiating then
7156 Set_Associated_Node (N, New_N);
7159 if Present (Get_Associated_Node (N))
7160 and then Nkind (Get_Associated_Node (N)) = Nkind (N)
7162 -- In the generic the aggregate has some composite type. If at
7163 -- the point of instantiation the type has a private view,
7164 -- install the full view (and that of its ancestors, if any).
7167 T : Entity_Id := (Etype (Get_Associated_Node (New_N)));
7171 if Present (T) and then Is_Private_Type (T) then
7176 and then Is_Tagged_Type (T)
7177 and then Is_Derived_Type (T)
7179 Rt := Root_Type (T);
7184 if Is_Private_Type (T) then
7195 -- Do not copy the associated node, which points to the generic copy
7196 -- of the aggregate.
7199 use Atree.Unchecked_Access;
7200 -- This code section is part of the implementation of an untyped
7201 -- tree traversal, so it needs direct access to node fields.
7204 Set_Field1 (New_N, Copy_Generic_Descendant (Field1 (N)));
7205 Set_Field2 (New_N, Copy_Generic_Descendant (Field2 (N)));
7206 Set_Field3 (New_N, Copy_Generic_Descendant (Field3 (N)));
7207 Set_Field5 (New_N, Copy_Generic_Descendant (Field5 (N)));
7210 -- Allocators do not have an identifier denoting the access type, so we
7211 -- must locate it through the expression to check whether the views are
7214 elsif Nkind (N) = N_Allocator
7215 and then Nkind (Expression (N)) = N_Qualified_Expression
7216 and then Is_Entity_Name (Subtype_Mark (Expression (N)))
7217 and then Instantiating
7220 T : constant Node_Id :=
7221 Get_Associated_Node (Subtype_Mark (Expression (N)));
7227 -- Retrieve the allocator node in the generic copy
7229 Acc_T := Etype (Parent (Parent (T)));
7231 if Present (Acc_T) and then Is_Private_Type (Acc_T) then
7232 Switch_View (Acc_T);
7239 -- For a proper body, we must catch the case of a proper body that
7240 -- replaces a stub. This represents the point at which a separate
7241 -- compilation unit, and hence template file, may be referenced, so we
7242 -- must make a new source instantiation entry for the template of the
7243 -- subunit, and ensure that all nodes in the subunit are adjusted using
7244 -- this new source instantiation entry.
7246 elsif Nkind (N) in N_Proper_Body then
7248 Save_Adjustment : constant Sloc_Adjustment := S_Adjustment;
7251 if Instantiating and then Was_Originally_Stub (N) then
7252 Create_Instantiation_Source
7253 (Instantiation_Node,
7254 Defining_Entity (N),
7259 -- Now copy the fields of the proper body, using the new
7260 -- adjustment factor if one was needed as per test above.
7264 -- Restore the original adjustment factor in case changed
7266 S_Adjustment := Save_Adjustment;
7269 -- Don't copy Ident or Comment pragmas, since the comment belongs to the
7270 -- generic unit, not to the instantiating unit.
7272 elsif Nkind (N) = N_Pragma and then Instantiating then
7274 Prag_Id : constant Pragma_Id := Get_Pragma_Id (N);
7276 if Prag_Id = Pragma_Ident or else Prag_Id = Pragma_Comment then
7277 New_N := Make_Null_Statement (Sloc (N));
7283 elsif Nkind_In (N, N_Integer_Literal, N_Real_Literal) then
7285 -- No descendant fields need traversing
7289 elsif Nkind (N) = N_String_Literal
7290 and then Present (Etype (N))
7291 and then Instantiating
7293 -- If the string is declared in an outer scope, the string_literal
7294 -- subtype created for it may have the wrong scope. Force reanalysis
7295 -- of the constant to generate a new itype in the proper context.
7297 Set_Etype (New_N, Empty);
7298 Set_Analyzed (New_N, False);
7300 -- For the remaining nodes, copy their descendants recursively
7305 if Instantiating and then Nkind (N) = N_Subprogram_Body then
7306 Set_Generic_Parent (Specification (New_N), N);
7308 -- Should preserve Corresponding_Spec??? (12.3(14))
7313 end Copy_Generic_Node;
7315 ----------------------------
7316 -- Denotes_Formal_Package --
7317 ----------------------------
7319 function Denotes_Formal_Package
7321 On_Exit : Boolean := False;
7322 Instance : Entity_Id := Empty) return Boolean
7325 Scop : constant Entity_Id := Scope (Pack);
7328 function Is_Actual_Of_Previous_Formal (P : Entity_Id) return Boolean;
7329 -- The package in question may be an actual for a previous formal
7330 -- package P of the current instance, so examine its actuals as well.
7331 -- This must be recursive over other formal packages.
7333 ----------------------------------
7334 -- Is_Actual_Of_Previous_Formal --
7335 ----------------------------------
7337 function Is_Actual_Of_Previous_Formal (P : Entity_Id) return Boolean is
7341 E1 := First_Entity (P);
7342 while Present (E1) and then E1 /= Instance loop
7343 if Ekind (E1) = E_Package
7344 and then Nkind (Parent (E1)) = N_Package_Renaming_Declaration
7346 if Renamed_Object (E1) = Pack then
7349 elsif E1 = P or else Renamed_Object (E1) = P then
7352 elsif Is_Actual_Of_Previous_Formal (E1) then
7361 end Is_Actual_Of_Previous_Formal;
7363 -- Start of processing for Denotes_Formal_Package
7369 (Instance_Envs.Last).Instantiated_Parent.Act_Id;
7371 Par := Current_Instantiated_Parent.Act_Id;
7374 if Ekind (Scop) = E_Generic_Package
7375 or else Nkind (Unit_Declaration_Node (Scop)) =
7376 N_Generic_Subprogram_Declaration
7380 elsif Nkind (Original_Node (Unit_Declaration_Node (Pack))) =
7381 N_Formal_Package_Declaration
7389 -- Check whether this package is associated with a formal package of
7390 -- the enclosing instantiation. Iterate over the list of renamings.
7392 E := First_Entity (Par);
7393 while Present (E) loop
7394 if Ekind (E) /= E_Package
7395 or else Nkind (Parent (E)) /= N_Package_Renaming_Declaration
7399 elsif Renamed_Object (E) = Par then
7402 elsif Renamed_Object (E) = Pack then
7405 elsif Is_Actual_Of_Previous_Formal (E) then
7415 end Denotes_Formal_Package;
7421 procedure End_Generic is
7423 -- ??? More things could be factored out in this routine. Should
7424 -- probably be done at a later stage.
7426 Inside_A_Generic := Generic_Flags.Table (Generic_Flags.Last);
7427 Generic_Flags.Decrement_Last;
7429 Expander_Mode_Restore;
7436 function Earlier (N1, N2 : Node_Id) return Boolean is
7437 procedure Find_Depth (P : in out Node_Id; D : in out Integer);
7438 -- Find distance from given node to enclosing compilation unit
7444 procedure Find_Depth (P : in out Node_Id; D : in out Integer) is
7447 and then Nkind (P) /= N_Compilation_Unit
7449 P := True_Parent (P);
7454 -- Local declarations
7463 -- Start of processing for Earlier
7466 Find_Depth (P1, D1);
7467 Find_Depth (P2, D2);
7477 P1 := True_Parent (P1);
7482 P2 := True_Parent (P2);
7486 -- At this point P1 and P2 are at the same distance from the root.
7487 -- We examine their parents until we find a common declarative list.
7488 -- If we reach the root, N1 and N2 do not descend from the same
7489 -- declarative list (e.g. one is nested in the declarative part and
7490 -- the other is in a block in the statement part) and the earlier
7491 -- one is already frozen.
7493 while not Is_List_Member (P1)
7494 or else not Is_List_Member (P2)
7495 or else List_Containing (P1) /= List_Containing (P2)
7497 P1 := True_Parent (P1);
7498 P2 := True_Parent (P2);
7500 if Nkind (Parent (P1)) = N_Subunit then
7501 P1 := Corresponding_Stub (Parent (P1));
7504 if Nkind (Parent (P2)) = N_Subunit then
7505 P2 := Corresponding_Stub (Parent (P2));
7513 -- Expanded code usually shares the source location of the original
7514 -- construct it was generated for. This however may not necessarely
7515 -- reflect the true location of the code within the tree.
7517 -- Before comparing the slocs of the two nodes, make sure that we are
7518 -- working with correct source locations. Assume that P1 is to the left
7519 -- of P2. If either one does not come from source, traverse the common
7520 -- list heading towards the other node and locate the first source
7524 -- ----+===+===+--------------+===+===+----
7525 -- expanded code expanded code
7527 if not Comes_From_Source (P1) then
7528 while Present (P1) loop
7530 -- Neither P2 nor a source statement were located during the
7531 -- search. If we reach the end of the list, then P1 does not
7532 -- occur earlier than P2.
7535 -- start --- P2 ----- P1 --- end
7537 if No (Next (P1)) then
7540 -- We encounter P2 while going to the right of the list. This
7541 -- means that P1 does indeed appear earlier.
7544 -- start --- P1 ===== P2 --- end
7545 -- expanded code in between
7550 -- No need to look any further since we have located a source
7553 elsif Comes_From_Source (P1) then
7563 if not Comes_From_Source (P2) then
7564 while Present (P2) loop
7566 -- Neither P1 nor a source statement were located during the
7567 -- search. If we reach the start of the list, then P1 does not
7568 -- occur earlier than P2.
7571 -- start --- P2 --- P1 --- end
7573 if No (Prev (P2)) then
7576 -- We encounter P1 while going to the left of the list. This
7577 -- means that P1 does indeed appear earlier.
7580 -- start --- P1 ===== P2 --- end
7581 -- expanded code in between
7586 -- No need to look any further since we have located a source
7589 elsif Comes_From_Source (P2) then
7599 -- At this point either both nodes came from source or we approximated
7600 -- their source locations through neighbouring source statements.
7602 T1 := Top_Level_Location (Sloc (P1));
7603 T2 := Top_Level_Location (Sloc (P2));
7605 -- When two nodes come from the same instance, they have identical top
7606 -- level locations. To determine proper relation within the tree, check
7607 -- their locations within the template.
7610 return Sloc (P1) < Sloc (P2);
7612 -- The two nodes either come from unrelated instances or do not come
7613 -- from instantiated code at all.
7620 ----------------------
7621 -- Find_Actual_Type --
7622 ----------------------
7624 function Find_Actual_Type
7626 Gen_Type : Entity_Id) return Entity_Id
7628 Gen_Scope : constant Entity_Id := Scope (Gen_Type);
7632 -- Special processing only applies to child units
7634 if not Is_Child_Unit (Gen_Scope) then
7635 return Get_Instance_Of (Typ);
7637 -- If designated or component type is itself a formal of the child unit,
7638 -- its instance is available.
7640 elsif Scope (Typ) = Gen_Scope then
7641 return Get_Instance_Of (Typ);
7643 -- If the array or access type is not declared in the parent unit,
7644 -- no special processing needed.
7646 elsif not Is_Generic_Type (Typ)
7647 and then Scope (Gen_Scope) /= Scope (Typ)
7649 return Get_Instance_Of (Typ);
7651 -- Otherwise, retrieve designated or component type by visibility
7654 T := Current_Entity (Typ);
7655 while Present (T) loop
7656 if In_Open_Scopes (Scope (T)) then
7659 elsif Is_Generic_Actual_Type (T) then
7668 end Find_Actual_Type;
7670 ----------------------------
7671 -- Freeze_Subprogram_Body --
7672 ----------------------------
7674 procedure Freeze_Subprogram_Body
7675 (Inst_Node : Node_Id;
7677 Pack_Id : Entity_Id)
7679 Gen_Unit : constant Entity_Id := Get_Generic_Entity (Inst_Node);
7680 Par : constant Entity_Id := Scope (Gen_Unit);
7686 function Enclosing_Package_Body (N : Node_Id) return Node_Id;
7687 -- Find innermost package body that encloses the given node, and which
7688 -- is not a compilation unit. Freeze nodes for the instance, or for its
7689 -- enclosing body, may be inserted after the enclosing_body of the
7690 -- generic unit. Used to determine proper placement of freeze node for
7691 -- both package and subprogram instances.
7693 function Package_Freeze_Node (B : Node_Id) return Node_Id;
7694 -- Find entity for given package body, and locate or create a freeze
7697 ----------------------------
7698 -- Enclosing_Package_Body --
7699 ----------------------------
7701 function Enclosing_Package_Body (N : Node_Id) return Node_Id is
7707 and then Nkind (Parent (P)) /= N_Compilation_Unit
7709 if Nkind (P) = N_Package_Body then
7710 if Nkind (Parent (P)) = N_Subunit then
7711 return Corresponding_Stub (Parent (P));
7717 P := True_Parent (P);
7721 end Enclosing_Package_Body;
7723 -------------------------
7724 -- Package_Freeze_Node --
7725 -------------------------
7727 function Package_Freeze_Node (B : Node_Id) return Node_Id is
7731 if Nkind (B) = N_Package_Body then
7732 Id := Corresponding_Spec (B);
7733 else pragma Assert (Nkind (B) = N_Package_Body_Stub);
7734 Id := Corresponding_Spec (Proper_Body (Unit (Library_Unit (B))));
7737 Ensure_Freeze_Node (Id);
7738 return Freeze_Node (Id);
7739 end Package_Freeze_Node;
7741 -- Start of processing of Freeze_Subprogram_Body
7744 -- If the instance and the generic body appear within the same unit, and
7745 -- the instance precedes the generic, the freeze node for the instance
7746 -- must appear after that of the generic. If the generic is nested
7747 -- within another instance I2, then current instance must be frozen
7748 -- after I2. In both cases, the freeze nodes are those of enclosing
7749 -- packages. Otherwise, the freeze node is placed at the end of the
7750 -- current declarative part.
7752 Enc_G := Enclosing_Package_Body (Gen_Body);
7753 Enc_I := Enclosing_Package_Body (Inst_Node);
7754 Ensure_Freeze_Node (Pack_Id);
7755 F_Node := Freeze_Node (Pack_Id);
7757 if Is_Generic_Instance (Par)
7758 and then Present (Freeze_Node (Par))
7759 and then In_Same_Declarative_Part (Freeze_Node (Par), Inst_Node)
7761 -- The parent was a premature instantiation. Insert freeze node at
7762 -- the end the current declarative part.
7764 if ABE_Is_Certain (Get_Package_Instantiation_Node (Par)) then
7765 Insert_Freeze_Node_For_Instance (Inst_Node, F_Node);
7767 -- Handle the following case:
7769 -- package Parent_Inst is new ...
7772 -- procedure P ... -- this body freezes Parent_Inst
7774 -- package Inst is new ...
7776 -- In this particular scenario, the freeze node for Inst must be
7777 -- inserted in the same manner as that of Parent_Inst - before the
7778 -- next source body or at the end of the declarative list (body not
7779 -- available). If body P did not exist and Parent_Inst was frozen
7780 -- after Inst, either by a body following Inst or at the end of the
7781 -- declarative region, the freeze node for Inst must be inserted
7782 -- after that of Parent_Inst. This relation is established by
7783 -- comparing the Slocs of Parent_Inst freeze node and Inst.
7785 elsif List_Containing (Get_Package_Instantiation_Node (Par)) =
7786 List_Containing (Inst_Node)
7787 and then Sloc (Freeze_Node (Par)) < Sloc (Inst_Node)
7789 Insert_Freeze_Node_For_Instance (Inst_Node, F_Node);
7792 Insert_After (Freeze_Node (Par), F_Node);
7795 -- The body enclosing the instance should be frozen after the body that
7796 -- includes the generic, because the body of the instance may make
7797 -- references to entities therein. If the two are not in the same
7798 -- declarative part, or if the one enclosing the instance is frozen
7799 -- already, freeze the instance at the end of the current declarative
7802 elsif Is_Generic_Instance (Par)
7803 and then Present (Freeze_Node (Par))
7804 and then Present (Enc_I)
7806 if In_Same_Declarative_Part (Freeze_Node (Par), Enc_I)
7808 (Nkind (Enc_I) = N_Package_Body
7810 In_Same_Declarative_Part (Freeze_Node (Par), Parent (Enc_I)))
7812 -- The enclosing package may contain several instances. Rather
7813 -- than computing the earliest point at which to insert its freeze
7814 -- node, we place it at the end of the declarative part of the
7815 -- parent of the generic.
7817 Insert_Freeze_Node_For_Instance
7818 (Freeze_Node (Par), Package_Freeze_Node (Enc_I));
7821 Insert_Freeze_Node_For_Instance (Inst_Node, F_Node);
7823 elsif Present (Enc_G)
7824 and then Present (Enc_I)
7825 and then Enc_G /= Enc_I
7826 and then Earlier (Inst_Node, Gen_Body)
7828 if Nkind (Enc_G) = N_Package_Body then
7830 Corresponding_Spec (Enc_G);
7831 else pragma Assert (Nkind (Enc_G) = N_Package_Body_Stub);
7833 Corresponding_Spec (Proper_Body (Unit (Library_Unit (Enc_G))));
7836 -- Freeze package that encloses instance, and place node after the
7837 -- package that encloses generic. If enclosing package is already
7838 -- frozen we have to assume it is at the proper place. This may be a
7839 -- potential ABE that requires dynamic checking. Do not add a freeze
7840 -- node if the package that encloses the generic is inside the body
7841 -- that encloses the instance, because the freeze node would be in
7842 -- the wrong scope. Additional contortions needed if the bodies are
7843 -- within a subunit.
7846 Enclosing_Body : Node_Id;
7849 if Nkind (Enc_I) = N_Package_Body_Stub then
7850 Enclosing_Body := Proper_Body (Unit (Library_Unit (Enc_I)));
7852 Enclosing_Body := Enc_I;
7855 if Parent (List_Containing (Enc_G)) /= Enclosing_Body then
7856 Insert_Freeze_Node_For_Instance
7857 (Enc_G, Package_Freeze_Node (Enc_I));
7861 -- Freeze enclosing subunit before instance
7863 Ensure_Freeze_Node (E_G_Id);
7865 if not Is_List_Member (Freeze_Node (E_G_Id)) then
7866 Insert_After (Enc_G, Freeze_Node (E_G_Id));
7869 Insert_Freeze_Node_For_Instance (Inst_Node, F_Node);
7872 -- If none of the above, insert freeze node at the end of the current
7873 -- declarative part.
7875 Insert_Freeze_Node_For_Instance (Inst_Node, F_Node);
7877 end Freeze_Subprogram_Body;
7883 function Get_Gen_Id (E : Assoc_Ptr) return Entity_Id is
7885 return Generic_Renamings.Table (E).Gen_Id;
7888 ---------------------
7889 -- Get_Instance_Of --
7890 ---------------------
7892 function Get_Instance_Of (A : Entity_Id) return Entity_Id is
7893 Res : constant Assoc_Ptr := Generic_Renamings_HTable.Get (A);
7896 if Res /= Assoc_Null then
7897 return Generic_Renamings.Table (Res).Act_Id;
7900 -- On exit, entity is not instantiated: not a generic parameter, or
7901 -- else parameter of an inner generic unit.
7905 end Get_Instance_Of;
7907 ------------------------------------
7908 -- Get_Package_Instantiation_Node --
7909 ------------------------------------
7911 function Get_Package_Instantiation_Node (A : Entity_Id) return Node_Id is
7912 Decl : Node_Id := Unit_Declaration_Node (A);
7916 -- If the Package_Instantiation attribute has been set on the package
7917 -- entity, then use it directly when it (or its Original_Node) refers
7918 -- to an N_Package_Instantiation node. In principle it should be
7919 -- possible to have this field set in all cases, which should be
7920 -- investigated, and would allow this function to be significantly
7923 Inst := Package_Instantiation (A);
7925 if Present (Inst) then
7926 if Nkind (Inst) = N_Package_Instantiation then
7929 elsif Nkind (Original_Node (Inst)) = N_Package_Instantiation then
7930 return Original_Node (Inst);
7934 -- If the instantiation is a compilation unit that does not need body
7935 -- then the instantiation node has been rewritten as a package
7936 -- declaration for the instance, and we return the original node.
7938 -- If it is a compilation unit and the instance node has not been
7939 -- rewritten, then it is still the unit of the compilation. Finally, if
7940 -- a body is present, this is a parent of the main unit whose body has
7941 -- been compiled for inlining purposes, and the instantiation node has
7942 -- been rewritten with the instance body.
7944 -- Otherwise the instantiation node appears after the declaration. If
7945 -- the entity is a formal package, the declaration may have been
7946 -- rewritten as a generic declaration (in the case of a formal with box)
7947 -- or left as a formal package declaration if it has actuals, and is
7948 -- found with a forward search.
7950 if Nkind (Parent (Decl)) = N_Compilation_Unit then
7951 if Nkind (Decl) = N_Package_Declaration
7952 and then Present (Corresponding_Body (Decl))
7954 Decl := Unit_Declaration_Node (Corresponding_Body (Decl));
7957 if Nkind (Original_Node (Decl)) = N_Package_Instantiation then
7958 return Original_Node (Decl);
7960 return Unit (Parent (Decl));
7963 elsif Nkind (Decl) = N_Package_Declaration
7964 and then Nkind (Original_Node (Decl)) = N_Formal_Package_Declaration
7966 return Original_Node (Decl);
7969 Inst := Next (Decl);
7970 while not Nkind_In (Inst, N_Package_Instantiation,
7971 N_Formal_Package_Declaration)
7978 end Get_Package_Instantiation_Node;
7980 ------------------------
7981 -- Has_Been_Exchanged --
7982 ------------------------
7984 function Has_Been_Exchanged (E : Entity_Id) return Boolean is
7988 Next := First_Elmt (Exchanged_Views);
7989 while Present (Next) loop
7990 if Full_View (Node (Next)) = E then
7998 end Has_Been_Exchanged;
8004 function Hash (F : Entity_Id) return HTable_Range is
8006 return HTable_Range (F mod HTable_Size);
8009 ------------------------
8010 -- Hide_Current_Scope --
8011 ------------------------
8013 procedure Hide_Current_Scope is
8014 C : constant Entity_Id := Current_Scope;
8018 Set_Is_Hidden_Open_Scope (C);
8020 E := First_Entity (C);
8021 while Present (E) loop
8022 if Is_Immediately_Visible (E) then
8023 Set_Is_Immediately_Visible (E, False);
8024 Append_Elmt (E, Hidden_Entities);
8030 -- Make the scope name invisible as well. This is necessary, but might
8031 -- conflict with calls to Rtsfind later on, in case the scope is a
8032 -- predefined one. There is no clean solution to this problem, so for
8033 -- now we depend on the user not redefining Standard itself in one of
8034 -- the parent units.
8036 if Is_Immediately_Visible (C) and then C /= Standard_Standard then
8037 Set_Is_Immediately_Visible (C, False);
8038 Append_Elmt (C, Hidden_Entities);
8041 end Hide_Current_Scope;
8047 procedure Init_Env is
8048 Saved : Instance_Env;
8051 Saved.Instantiated_Parent := Current_Instantiated_Parent;
8052 Saved.Exchanged_Views := Exchanged_Views;
8053 Saved.Hidden_Entities := Hidden_Entities;
8054 Saved.Current_Sem_Unit := Current_Sem_Unit;
8055 Saved.Parent_Unit_Visible := Parent_Unit_Visible;
8056 Saved.Instance_Parent_Unit := Instance_Parent_Unit;
8058 -- Save configuration switches. These may be reset if the unit is a
8059 -- predefined unit, and the current mode is not Ada 2005.
8061 Save_Opt_Config_Switches (Saved.Switches);
8063 Instance_Envs.Append (Saved);
8065 Exchanged_Views := New_Elmt_List;
8066 Hidden_Entities := New_Elmt_List;
8068 -- Make dummy entry for Instantiated parent. If generic unit is legal,
8069 -- this is set properly in Set_Instance_Env.
8071 Current_Instantiated_Parent :=
8072 (Current_Scope, Current_Scope, Assoc_Null);
8075 ------------------------------
8076 -- In_Same_Declarative_Part --
8077 ------------------------------
8079 function In_Same_Declarative_Part
8081 Inst : Node_Id) return Boolean
8083 Decls : constant Node_Id := Parent (F_Node);
8087 Nod := Parent (Inst);
8088 while Present (Nod) loop
8092 elsif Nkind_In (Nod, N_Subprogram_Body,
8094 N_Package_Declaration,
8101 elsif Nkind (Nod) = N_Subunit then
8102 Nod := Corresponding_Stub (Nod);
8104 elsif Nkind (Nod) = N_Compilation_Unit then
8108 Nod := Parent (Nod);
8113 end In_Same_Declarative_Part;
8115 ---------------------
8116 -- In_Main_Context --
8117 ---------------------
8119 function In_Main_Context (E : Entity_Id) return Boolean is
8125 if not Is_Compilation_Unit (E)
8126 or else Ekind (E) /= E_Package
8127 or else In_Private_Part (E)
8132 Context := Context_Items (Cunit (Main_Unit));
8134 Clause := First (Context);
8135 while Present (Clause) loop
8136 if Nkind (Clause) = N_With_Clause then
8137 Nam := Name (Clause);
8139 -- If the current scope is part of the context of the main unit,
8140 -- analysis of the corresponding with_clause is not complete, and
8141 -- the entity is not set. We use the Chars field directly, which
8142 -- might produce false positives in rare cases, but guarantees
8143 -- that we produce all the instance bodies we will need.
8145 if (Is_Entity_Name (Nam) and then Chars (Nam) = Chars (E))
8146 or else (Nkind (Nam) = N_Selected_Component
8147 and then Chars (Selector_Name (Nam)) = Chars (E))
8157 end In_Main_Context;
8159 ---------------------
8160 -- Inherit_Context --
8161 ---------------------
8163 procedure Inherit_Context (Gen_Decl : Node_Id; Inst : Node_Id) is
8164 Current_Context : List_Id;
8165 Current_Unit : Node_Id;
8174 if Nkind (Parent (Gen_Decl)) = N_Compilation_Unit then
8176 -- The inherited context is attached to the enclosing compilation
8177 -- unit. This is either the main unit, or the declaration for the
8178 -- main unit (in case the instantiation appears within the package
8179 -- declaration and the main unit is its body).
8181 Current_Unit := Parent (Inst);
8182 while Present (Current_Unit)
8183 and then Nkind (Current_Unit) /= N_Compilation_Unit
8185 Current_Unit := Parent (Current_Unit);
8188 Current_Context := Context_Items (Current_Unit);
8190 Item := First (Context_Items (Parent (Gen_Decl)));
8191 while Present (Item) loop
8192 if Nkind (Item) = N_With_Clause then
8193 Lib_Unit := Library_Unit (Item);
8195 -- Take care to prevent direct cyclic with's
8197 if Lib_Unit /= Current_Unit then
8199 -- Do not add a unit if it is already in the context
8201 Clause := First (Current_Context);
8203 while Present (Clause) loop
8204 if Nkind (Clause) = N_With_Clause and then
8205 Library_Unit (Clause) = Lib_Unit
8215 New_I := New_Copy (Item);
8216 Set_Implicit_With (New_I, True);
8217 Set_Implicit_With_From_Instantiation (New_I, True);
8218 Append (New_I, Current_Context);
8226 end Inherit_Context;
8232 procedure Initialize is
8234 Generic_Renamings.Init;
8237 Generic_Renamings_HTable.Reset;
8238 Circularity_Detected := False;
8239 Exchanged_Views := No_Elist;
8240 Hidden_Entities := No_Elist;
8243 -------------------------------------
8244 -- Insert_Freeze_Node_For_Instance --
8245 -------------------------------------
8247 procedure Insert_Freeze_Node_For_Instance
8256 function Enclosing_Body (N : Node_Id) return Node_Id;
8257 -- Find enclosing package or subprogram body, if any. Freeze node may
8258 -- be placed at end of current declarative list if previous instance
8259 -- and current one have different enclosing bodies.
8261 function Previous_Instance (Gen : Entity_Id) return Entity_Id;
8262 -- Find the local instance, if any, that declares the generic that is
8263 -- being instantiated. If present, the freeze node for this instance
8264 -- must follow the freeze node for the previous instance.
8266 --------------------
8267 -- Enclosing_Body --
8268 --------------------
8270 function Enclosing_Body (N : Node_Id) return Node_Id is
8276 and then Nkind (Parent (P)) /= N_Compilation_Unit
8278 if Nkind_In (P, N_Package_Body, N_Subprogram_Body) then
8279 if Nkind (Parent (P)) = N_Subunit then
8280 return Corresponding_Stub (Parent (P));
8286 P := True_Parent (P);
8292 -----------------------
8293 -- Previous_Instance --
8294 -----------------------
8296 function Previous_Instance (Gen : Entity_Id) return Entity_Id is
8301 while Present (S) and then S /= Standard_Standard loop
8302 if Is_Generic_Instance (S)
8303 and then In_Same_Source_Unit (S, N)
8312 end Previous_Instance;
8314 -- Start of processing for Insert_Freeze_Node_For_Instance
8317 if not Is_List_Member (F_Node) then
8319 Decls := List_Containing (N);
8320 Inst := Entity (F_Node);
8321 Par_N := Parent (Decls);
8323 -- When processing a subprogram instantiation, utilize the actual
8324 -- subprogram instantiation rather than its package wrapper as it
8325 -- carries all the context information.
8327 if Is_Wrapper_Package (Inst) then
8328 Inst := Related_Instance (Inst);
8331 -- If this is a package instance, check whether the generic is
8332 -- declared in a previous instance and the current instance is
8333 -- not within the previous one.
8335 if Present (Generic_Parent (Parent (Inst)))
8336 and then Is_In_Main_Unit (N)
8339 Enclosing_N : constant Node_Id := Enclosing_Body (N);
8340 Par_I : constant Entity_Id :=
8342 (Generic_Parent (Parent (Inst)));
8347 and then Earlier (N, Freeze_Node (Par_I))
8349 Scop := Scope (Inst);
8351 -- If the current instance is within the one that contains
8352 -- the generic, the freeze node for the current one must
8353 -- appear in the current declarative part. Ditto, if the
8354 -- current instance is within another package instance or
8355 -- within a body that does not enclose the current instance.
8356 -- In these three cases the freeze node of the previous
8357 -- instance is not relevant.
8359 while Present (Scop) and then Scop /= Standard_Standard loop
8360 exit when Scop = Par_I
8362 (Is_Generic_Instance (Scop)
8363 and then Scope_Depth (Scop) > Scope_Depth (Par_I));
8364 Scop := Scope (Scop);
8367 -- Previous instance encloses current instance
8369 if Scop = Par_I then
8372 -- If the next node is a source body we must freeze in
8373 -- the current scope as well.
8375 elsif Present (Next (N))
8376 and then Nkind_In (Next (N), N_Subprogram_Body,
8378 and then Comes_From_Source (Next (N))
8382 -- Current instance is within an unrelated instance
8384 elsif Is_Generic_Instance (Scop) then
8387 -- Current instance is within an unrelated body
8389 elsif Present (Enclosing_N)
8390 and then Enclosing_N /= Enclosing_Body (Par_I)
8395 Insert_After (Freeze_Node (Par_I), F_Node);
8402 -- When the instantiation occurs in a package declaration, append the
8403 -- freeze node to the private declarations (if any).
8405 if Nkind (Par_N) = N_Package_Specification
8406 and then Decls = Visible_Declarations (Par_N)
8407 and then Present (Private_Declarations (Par_N))
8408 and then not Is_Empty_List (Private_Declarations (Par_N))
8410 Decls := Private_Declarations (Par_N);
8411 Decl := First (Decls);
8414 -- Determine the proper freeze point of a package instantiation. We
8415 -- adhere to the general rule of a package or subprogram body causing
8416 -- freezing of anything before it in the same declarative region. In
8417 -- this case, the proper freeze point of a package instantiation is
8418 -- before the first source body which follows, or before a stub. This
8419 -- ensures that entities coming from the instance are already frozen
8420 -- and usable in source bodies.
8422 if Nkind (Par_N) /= N_Package_Declaration
8423 and then Ekind (Inst) = E_Package
8424 and then Is_Generic_Instance (Inst)
8426 not In_Same_Source_Unit (Generic_Parent (Parent (Inst)), Inst)
8428 while Present (Decl) loop
8429 if (Nkind (Decl) in N_Unit_Body
8431 Nkind (Decl) in N_Body_Stub)
8432 and then Comes_From_Source (Decl)
8434 Insert_Before (Decl, F_Node);
8442 -- In a package declaration, or if no previous body, insert at end
8445 Set_Sloc (F_Node, Sloc (Last (Decls)));
8446 Insert_After (Last (Decls), F_Node);
8448 end Insert_Freeze_Node_For_Instance;
8454 procedure Install_Body
8455 (Act_Body : Node_Id;
8460 Act_Id : constant Entity_Id := Corresponding_Spec (Act_Body);
8461 Act_Unit : constant Node_Id := Unit (Cunit (Get_Source_Unit (N)));
8462 Gen_Id : constant Entity_Id := Corresponding_Spec (Gen_Body);
8463 Par : constant Entity_Id := Scope (Gen_Id);
8464 Gen_Unit : constant Node_Id :=
8465 Unit (Cunit (Get_Source_Unit (Gen_Decl)));
8466 Orig_Body : Node_Id := Gen_Body;
8468 Body_Unit : Node_Id;
8470 Must_Delay : Boolean;
8472 function In_Same_Enclosing_Subp return Boolean;
8473 -- Check whether instance and generic body are within same subprogram.
8475 function True_Sloc (N : Node_Id) return Source_Ptr;
8476 -- If the instance is nested inside a generic unit, the Sloc of the
8477 -- instance indicates the place of the original definition, not the
8478 -- point of the current enclosing instance. Pending a better usage of
8479 -- Slocs to indicate instantiation places, we determine the place of
8480 -- origin of a node by finding the maximum sloc of any ancestor node.
8481 -- Why is this not equivalent to Top_Level_Location ???
8483 ----------------------------
8484 -- In_Same_Enclosing_Subp --
8485 ----------------------------
8487 function In_Same_Enclosing_Subp return Boolean is
8492 Scop := Scope (Act_Id);
8493 while Scop /= Standard_Standard
8494 and then not Is_Overloadable (Scop)
8496 Scop := Scope (Scop);
8499 if Scop = Standard_Standard then
8505 Scop := Scope (Gen_Id);
8506 while Scop /= Standard_Standard loop
8510 Scop := Scope (Scop);
8515 end In_Same_Enclosing_Subp;
8521 function True_Sloc (N : Node_Id) return Source_Ptr is
8528 while Present (N1) and then N1 /= Act_Unit loop
8529 if Sloc (N1) > Res then
8539 -- Start of processing for Install_Body
8542 -- If the body is a subunit, the freeze point is the corresponding stub
8543 -- in the current compilation, not the subunit itself.
8545 if Nkind (Parent (Gen_Body)) = N_Subunit then
8546 Orig_Body := Corresponding_Stub (Parent (Gen_Body));
8548 Orig_Body := Gen_Body;
8551 Body_Unit := Unit (Cunit (Get_Source_Unit (Orig_Body)));
8553 -- If the instantiation and the generic definition appear in the same
8554 -- package declaration, this is an early instantiation. If they appear
8555 -- in the same declarative part, it is an early instantiation only if
8556 -- the generic body appears textually later, and the generic body is
8557 -- also in the main unit.
8559 -- If instance is nested within a subprogram, and the generic body
8560 -- is not, the instance is delayed because the enclosing body is. If
8561 -- instance and body are within the same scope, or the same subprogram
8562 -- body, indicate explicitly that the instance is delayed.
8565 (Gen_Unit = Act_Unit
8566 and then (Nkind_In (Gen_Unit, N_Package_Declaration,
8567 N_Generic_Package_Declaration)
8568 or else (Gen_Unit = Body_Unit
8569 and then True_Sloc (N) < Sloc (Orig_Body)))
8570 and then Is_In_Main_Unit (Gen_Unit)
8571 and then (Scope (Act_Id) = Scope (Gen_Id)
8572 or else In_Same_Enclosing_Subp));
8574 -- If this is an early instantiation, the freeze node is placed after
8575 -- the generic body. Otherwise, if the generic appears in an instance,
8576 -- we cannot freeze the current instance until the outer one is frozen.
8577 -- This is only relevant if the current instance is nested within some
8578 -- inner scope not itself within the outer instance. If this scope is
8579 -- a package body in the same declarative part as the outer instance,
8580 -- then that body needs to be frozen after the outer instance. Finally,
8581 -- if no delay is needed, we place the freeze node at the end of the
8582 -- current declarative part.
8584 if Expander_Active then
8585 Ensure_Freeze_Node (Act_Id);
8586 F_Node := Freeze_Node (Act_Id);
8589 Insert_After (Orig_Body, F_Node);
8591 elsif Is_Generic_Instance (Par)
8592 and then Present (Freeze_Node (Par))
8593 and then Scope (Act_Id) /= Par
8595 -- Freeze instance of inner generic after instance of enclosing
8598 if In_Same_Declarative_Part (Freeze_Node (Par), N) then
8600 -- Handle the following case:
8602 -- package Parent_Inst is new ...
8605 -- procedure P ... -- this body freezes Parent_Inst
8607 -- package Inst is new ...
8609 -- In this particular scenario, the freeze node for Inst must
8610 -- be inserted in the same manner as that of Parent_Inst,
8611 -- before the next source body or at the end of the declarative
8612 -- list (body not available). If body P did not exist and
8613 -- Parent_Inst was frozen after Inst, either by a body
8614 -- following Inst or at the end of the declarative region,
8615 -- the freeze node for Inst must be inserted after that of
8616 -- Parent_Inst. This relation is established by comparing
8617 -- the Slocs of Parent_Inst freeze node and Inst.
8619 if List_Containing (Get_Package_Instantiation_Node (Par)) =
8621 and then Sloc (Freeze_Node (Par)) < Sloc (N)
8623 Insert_Freeze_Node_For_Instance (N, F_Node);
8625 Insert_After (Freeze_Node (Par), F_Node);
8628 -- Freeze package enclosing instance of inner generic after
8629 -- instance of enclosing generic.
8631 elsif Nkind_In (Parent (N), N_Package_Body, N_Subprogram_Body)
8632 and then In_Same_Declarative_Part (Freeze_Node (Par), Parent (N))
8635 Enclosing : Entity_Id;
8638 Enclosing := Corresponding_Spec (Parent (N));
8640 if No (Enclosing) then
8641 Enclosing := Defining_Entity (Parent (N));
8644 Insert_Freeze_Node_For_Instance (N, F_Node);
8645 Ensure_Freeze_Node (Enclosing);
8647 if not Is_List_Member (Freeze_Node (Enclosing)) then
8649 -- The enclosing context is a subunit, insert the freeze
8650 -- node after the stub.
8652 if Nkind (Parent (Parent (N))) = N_Subunit then
8653 Insert_Freeze_Node_For_Instance
8654 (Corresponding_Stub (Parent (Parent (N))),
8655 Freeze_Node (Enclosing));
8657 -- The enclosing context is a package with a stub body
8658 -- which has already been replaced by the real body.
8659 -- Insert the freeze node after the actual body.
8661 elsif Ekind (Enclosing) = E_Package
8662 and then Present (Body_Entity (Enclosing))
8663 and then Was_Originally_Stub
8664 (Parent (Body_Entity (Enclosing)))
8666 Insert_Freeze_Node_For_Instance
8667 (Parent (Body_Entity (Enclosing)),
8668 Freeze_Node (Enclosing));
8670 -- The parent instance has been frozen before the body of
8671 -- the enclosing package, insert the freeze node after
8674 elsif List_Containing (Freeze_Node (Par)) =
8675 List_Containing (Parent (N))
8676 and then Sloc (Freeze_Node (Par)) < Sloc (Parent (N))
8678 Insert_Freeze_Node_For_Instance
8679 (Parent (N), Freeze_Node (Enclosing));
8683 (Freeze_Node (Par), Freeze_Node (Enclosing));
8689 Insert_Freeze_Node_For_Instance (N, F_Node);
8693 Insert_Freeze_Node_For_Instance (N, F_Node);
8697 Set_Is_Frozen (Act_Id);
8698 Insert_Before (N, Act_Body);
8699 Mark_Rewrite_Insertion (Act_Body);
8702 -----------------------------
8703 -- Install_Formal_Packages --
8704 -----------------------------
8706 procedure Install_Formal_Packages (Par : Entity_Id) is
8709 Gen_E : Entity_Id := Empty;
8712 E := First_Entity (Par);
8714 -- If we are installing an instance parent, locate the formal packages
8715 -- of its generic parent.
8717 if Is_Generic_Instance (Par) then
8718 Gen := Generic_Parent (Package_Specification (Par));
8719 Gen_E := First_Entity (Gen);
8722 while Present (E) loop
8723 if Ekind (E) = E_Package
8724 and then Nkind (Parent (E)) = N_Package_Renaming_Declaration
8726 -- If this is the renaming for the parent instance, done
8728 if Renamed_Object (E) = Par then
8731 -- The visibility of a formal of an enclosing generic is already
8734 elsif Denotes_Formal_Package (E) then
8737 elsif Present (Associated_Formal_Package (E)) then
8738 Check_Generic_Actuals (Renamed_Object (E), True);
8739 Set_Is_Hidden (E, False);
8741 -- Find formal package in generic unit that corresponds to
8742 -- (instance of) formal package in instance.
8744 while Present (Gen_E) and then Chars (Gen_E) /= Chars (E) loop
8745 Next_Entity (Gen_E);
8748 if Present (Gen_E) then
8749 Map_Formal_Package_Entities (Gen_E, E);
8756 if Present (Gen_E) then
8757 Next_Entity (Gen_E);
8760 end Install_Formal_Packages;
8762 --------------------
8763 -- Install_Parent --
8764 --------------------
8766 procedure Install_Parent (P : Entity_Id; In_Body : Boolean := False) is
8767 Ancestors : constant Elist_Id := New_Elmt_List;
8768 S : constant Entity_Id := Current_Scope;
8769 Inst_Par : Entity_Id;
8770 First_Par : Entity_Id;
8771 Inst_Node : Node_Id;
8772 Gen_Par : Entity_Id;
8773 First_Gen : Entity_Id;
8776 procedure Install_Noninstance_Specs (Par : Entity_Id);
8777 -- Install the scopes of noninstance parent units ending with Par
8779 procedure Install_Spec (Par : Entity_Id);
8780 -- The child unit is within the declarative part of the parent, so the
8781 -- declarations within the parent are immediately visible.
8783 -------------------------------
8784 -- Install_Noninstance_Specs --
8785 -------------------------------
8787 procedure Install_Noninstance_Specs (Par : Entity_Id) is
8790 and then Par /= Standard_Standard
8791 and then not In_Open_Scopes (Par)
8793 Install_Noninstance_Specs (Scope (Par));
8796 end Install_Noninstance_Specs;
8802 procedure Install_Spec (Par : Entity_Id) is
8803 Spec : constant Node_Id := Package_Specification (Par);
8806 -- If this parent of the child instance is a top-level unit,
8807 -- then record the unit and its visibility for later resetting in
8808 -- Remove_Parent. We exclude units that are generic instances, as we
8809 -- only want to record this information for the ultimate top-level
8810 -- noninstance parent (is that always correct???).
8812 if Scope (Par) = Standard_Standard
8813 and then not Is_Generic_Instance (Par)
8815 Parent_Unit_Visible := Is_Immediately_Visible (Par);
8816 Instance_Parent_Unit := Par;
8819 -- Open the parent scope and make it and its declarations visible.
8820 -- If this point is not within a body, then only the visible
8821 -- declarations should be made visible, and installation of the
8822 -- private declarations is deferred until the appropriate point
8823 -- within analysis of the spec being instantiated (see the handling
8824 -- of parent visibility in Analyze_Package_Specification). This is
8825 -- relaxed in the case where the parent unit is Ada.Tags, to avoid
8826 -- private view problems that occur when compiling instantiations of
8827 -- a generic child of that package (Generic_Dispatching_Constructor).
8828 -- If the instance freezes a tagged type, inlinings of operations
8829 -- from Ada.Tags may need the full view of type Tag. If inlining took
8830 -- proper account of establishing visibility of inlined subprograms'
8831 -- parents then it should be possible to remove this
8832 -- special check. ???
8835 Set_Is_Immediately_Visible (Par);
8836 Install_Visible_Declarations (Par);
8837 Set_Use (Visible_Declarations (Spec));
8839 if In_Body or else Is_RTU (Par, Ada_Tags) then
8840 Install_Private_Declarations (Par);
8841 Set_Use (Private_Declarations (Spec));
8845 -- Start of processing for Install_Parent
8848 -- We need to install the parent instance to compile the instantiation
8849 -- of the child, but the child instance must appear in the current
8850 -- scope. Given that we cannot place the parent above the current scope
8851 -- in the scope stack, we duplicate the current scope and unstack both
8852 -- after the instantiation is complete.
8854 -- If the parent is itself the instantiation of a child unit, we must
8855 -- also stack the instantiation of its parent, and so on. Each such
8856 -- ancestor is the prefix of the name in a prior instantiation.
8858 -- If this is a nested instance, the parent unit itself resolves to
8859 -- a renaming of the parent instance, whose declaration we need.
8861 -- Finally, the parent may be a generic (not an instance) when the
8862 -- child unit appears as a formal package.
8866 if Present (Renamed_Entity (Inst_Par)) then
8867 Inst_Par := Renamed_Entity (Inst_Par);
8870 First_Par := Inst_Par;
8872 Gen_Par := Generic_Parent (Package_Specification (Inst_Par));
8874 First_Gen := Gen_Par;
8876 while Present (Gen_Par) and then Is_Child_Unit (Gen_Par) loop
8878 -- Load grandparent instance as well
8880 Inst_Node := Get_Package_Instantiation_Node (Inst_Par);
8882 if Nkind (Name (Inst_Node)) = N_Expanded_Name then
8883 Inst_Par := Entity (Prefix (Name (Inst_Node)));
8885 if Present (Renamed_Entity (Inst_Par)) then
8886 Inst_Par := Renamed_Entity (Inst_Par);
8889 Gen_Par := Generic_Parent (Package_Specification (Inst_Par));
8891 if Present (Gen_Par) then
8892 Prepend_Elmt (Inst_Par, Ancestors);
8895 -- Parent is not the name of an instantiation
8897 Install_Noninstance_Specs (Inst_Par);
8908 if Present (First_Gen) then
8909 Append_Elmt (First_Par, Ancestors);
8911 Install_Noninstance_Specs (First_Par);
8914 if not Is_Empty_Elmt_List (Ancestors) then
8915 Elmt := First_Elmt (Ancestors);
8916 while Present (Elmt) loop
8917 Install_Spec (Node (Elmt));
8918 Install_Formal_Packages (Node (Elmt));
8928 -------------------------------
8929 -- Install_Hidden_Primitives --
8930 -------------------------------
8932 procedure Install_Hidden_Primitives
8933 (Prims_List : in out Elist_Id;
8938 List : Elist_Id := No_Elist;
8939 Prim_G_Elmt : Elmt_Id;
8940 Prim_A_Elmt : Elmt_Id;
8945 -- No action needed in case of serious errors because we cannot trust
8946 -- in the order of primitives
8948 if Serious_Errors_Detected > 0 then
8951 -- No action possible if we don't have available the list of primitive
8955 or else not Is_Record_Type (Gen_T)
8956 or else not Is_Tagged_Type (Gen_T)
8957 or else not Is_Record_Type (Act_T)
8958 or else not Is_Tagged_Type (Act_T)
8962 -- There is no need to handle interface types since their primitives
8965 elsif Is_Interface (Gen_T) then
8969 Prim_G_Elmt := First_Elmt (Primitive_Operations (Gen_T));
8971 if not Is_Class_Wide_Type (Act_T) then
8972 Prim_A_Elmt := First_Elmt (Primitive_Operations (Act_T));
8974 Prim_A_Elmt := First_Elmt (Primitive_Operations (Root_Type (Act_T)));
8978 -- Skip predefined primitives in the generic formal
8980 while Present (Prim_G_Elmt)
8981 and then Is_Predefined_Dispatching_Operation (Node (Prim_G_Elmt))
8983 Next_Elmt (Prim_G_Elmt);
8986 -- Skip predefined primitives in the generic actual
8988 while Present (Prim_A_Elmt)
8989 and then Is_Predefined_Dispatching_Operation (Node (Prim_A_Elmt))
8991 Next_Elmt (Prim_A_Elmt);
8994 exit when No (Prim_G_Elmt) or else No (Prim_A_Elmt);
8996 Prim_G := Node (Prim_G_Elmt);
8997 Prim_A := Node (Prim_A_Elmt);
8999 -- There is no need to handle interface primitives because their
9000 -- primitives are not hidden
9002 exit when Present (Interface_Alias (Prim_G));
9004 -- Here we install one hidden primitive
9006 if Chars (Prim_G) /= Chars (Prim_A)
9007 and then Has_Suffix (Prim_A, 'P')
9008 and then Remove_Suffix (Prim_A, 'P') = Chars (Prim_G)
9010 Set_Chars (Prim_A, Chars (Prim_G));
9011 Append_New_Elmt (Prim_A, To => List);
9014 Next_Elmt (Prim_A_Elmt);
9015 Next_Elmt (Prim_G_Elmt);
9018 -- Append the elements to the list of temporarily visible primitives
9019 -- avoiding duplicates.
9021 if Present (List) then
9022 if No (Prims_List) then
9023 Prims_List := New_Elmt_List;
9026 Elmt := First_Elmt (List);
9027 while Present (Elmt) loop
9028 Append_Unique_Elmt (Node (Elmt), Prims_List);
9032 end Install_Hidden_Primitives;
9034 -------------------------------
9035 -- Restore_Hidden_Primitives --
9036 -------------------------------
9038 procedure Restore_Hidden_Primitives (Prims_List : in out Elist_Id) is
9039 Prim_Elmt : Elmt_Id;
9043 if Prims_List /= No_Elist then
9044 Prim_Elmt := First_Elmt (Prims_List);
9045 while Present (Prim_Elmt) loop
9046 Prim := Node (Prim_Elmt);
9047 Set_Chars (Prim, Add_Suffix (Prim, 'P'));
9048 Next_Elmt (Prim_Elmt);
9051 Prims_List := No_Elist;
9053 end Restore_Hidden_Primitives;
9055 --------------------------------
9056 -- Instantiate_Formal_Package --
9057 --------------------------------
9059 function Instantiate_Formal_Package
9062 Analyzed_Formal : Node_Id) return List_Id
9064 Loc : constant Source_Ptr := Sloc (Actual);
9065 Actual_Pack : Entity_Id;
9066 Formal_Pack : Entity_Id;
9067 Gen_Parent : Entity_Id;
9070 Parent_Spec : Node_Id;
9072 procedure Find_Matching_Actual
9074 Act : in out Entity_Id);
9075 -- We need to associate each formal entity in the formal package with
9076 -- the corresponding entity in the actual package. The actual package
9077 -- has been analyzed and possibly expanded, and as a result there is
9078 -- no one-to-one correspondence between the two lists (for example,
9079 -- the actual may include subtypes, itypes, and inherited primitive
9080 -- operations, interspersed among the renaming declarations for the
9081 -- actuals) . We retrieve the corresponding actual by name because each
9082 -- actual has the same name as the formal, and they do appear in the
9085 function Get_Formal_Entity (N : Node_Id) return Entity_Id;
9086 -- Retrieve entity of defining entity of generic formal parameter.
9087 -- Only the declarations of formals need to be considered when
9088 -- linking them to actuals, but the declarative list may include
9089 -- internal entities generated during analysis, and those are ignored.
9091 procedure Match_Formal_Entity
9092 (Formal_Node : Node_Id;
9093 Formal_Ent : Entity_Id;
9094 Actual_Ent : Entity_Id);
9095 -- Associates the formal entity with the actual. In the case where
9096 -- Formal_Ent is a formal package, this procedure iterates through all
9097 -- of its formals and enters associations between the actuals occurring
9098 -- in the formal package's corresponding actual package (given by
9099 -- Actual_Ent) and the formal package's formal parameters. This
9100 -- procedure recurses if any of the parameters is itself a package.
9102 function Is_Instance_Of
9103 (Act_Spec : Entity_Id;
9104 Gen_Anc : Entity_Id) return Boolean;
9105 -- The actual can be an instantiation of a generic within another
9106 -- instance, in which case there is no direct link from it to the
9107 -- original generic ancestor. In that case, we recognize that the
9108 -- ultimate ancestor is the same by examining names and scopes.
9110 procedure Process_Nested_Formal (Formal : Entity_Id);
9111 -- If the current formal is declared with a box, its own formals are
9112 -- visible in the instance, as they were in the generic, and their
9113 -- Hidden flag must be reset. If some of these formals are themselves
9114 -- packages declared with a box, the processing must be recursive.
9116 --------------------------
9117 -- Find_Matching_Actual --
9118 --------------------------
9120 procedure Find_Matching_Actual
9122 Act : in out Entity_Id)
9124 Formal_Ent : Entity_Id;
9127 case Nkind (Original_Node (F)) is
9128 when N_Formal_Object_Declaration |
9129 N_Formal_Type_Declaration =>
9130 Formal_Ent := Defining_Identifier (F);
9132 while Chars (Act) /= Chars (Formal_Ent) loop
9136 when N_Formal_Subprogram_Declaration |
9137 N_Formal_Package_Declaration |
9138 N_Package_Declaration |
9139 N_Generic_Package_Declaration =>
9140 Formal_Ent := Defining_Entity (F);
9142 while Chars (Act) /= Chars (Formal_Ent) loop
9147 raise Program_Error;
9149 end Find_Matching_Actual;
9151 -------------------------
9152 -- Match_Formal_Entity --
9153 -------------------------
9155 procedure Match_Formal_Entity
9156 (Formal_Node : Node_Id;
9157 Formal_Ent : Entity_Id;
9158 Actual_Ent : Entity_Id)
9160 Act_Pkg : Entity_Id;
9163 Set_Instance_Of (Formal_Ent, Actual_Ent);
9165 if Ekind (Actual_Ent) = E_Package then
9167 -- Record associations for each parameter
9169 Act_Pkg := Actual_Ent;
9172 A_Ent : Entity_Id := First_Entity (Act_Pkg);
9181 -- Retrieve the actual given in the formal package declaration
9183 Actual := Entity (Name (Original_Node (Formal_Node)));
9185 -- The actual in the formal package declaration may be a
9186 -- renamed generic package, in which case we want to retrieve
9187 -- the original generic in order to traverse its formal part.
9189 if Present (Renamed_Entity (Actual)) then
9190 Gen_Decl := Unit_Declaration_Node (Renamed_Entity (Actual));
9192 Gen_Decl := Unit_Declaration_Node (Actual);
9195 Formals := Generic_Formal_Declarations (Gen_Decl);
9197 if Present (Formals) then
9198 F_Node := First_Non_Pragma (Formals);
9203 while Present (A_Ent)
9204 and then Present (F_Node)
9205 and then A_Ent /= First_Private_Entity (Act_Pkg)
9207 F_Ent := Get_Formal_Entity (F_Node);
9209 if Present (F_Ent) then
9211 -- This is a formal of the original package. Record
9212 -- association and recurse.
9214 Find_Matching_Actual (F_Node, A_Ent);
9215 Match_Formal_Entity (F_Node, F_Ent, A_Ent);
9216 Next_Entity (A_Ent);
9219 Next_Non_Pragma (F_Node);
9223 end Match_Formal_Entity;
9225 -----------------------
9226 -- Get_Formal_Entity --
9227 -----------------------
9229 function Get_Formal_Entity (N : Node_Id) return Entity_Id is
9230 Kind : constant Node_Kind := Nkind (Original_Node (N));
9233 when N_Formal_Object_Declaration =>
9234 return Defining_Identifier (N);
9236 when N_Formal_Type_Declaration =>
9237 return Defining_Identifier (N);
9239 when N_Formal_Subprogram_Declaration =>
9240 return Defining_Unit_Name (Specification (N));
9242 when N_Formal_Package_Declaration =>
9243 return Defining_Identifier (Original_Node (N));
9245 when N_Generic_Package_Declaration =>
9246 return Defining_Identifier (Original_Node (N));
9248 -- All other declarations are introduced by semantic analysis and
9249 -- have no match in the actual.
9254 end Get_Formal_Entity;
9256 --------------------
9257 -- Is_Instance_Of --
9258 --------------------
9260 function Is_Instance_Of
9261 (Act_Spec : Entity_Id;
9262 Gen_Anc : Entity_Id) return Boolean
9264 Gen_Par : constant Entity_Id := Generic_Parent (Act_Spec);
9267 if No (Gen_Par) then
9270 -- Simplest case: the generic parent of the actual is the formal
9272 elsif Gen_Par = Gen_Anc then
9275 elsif Chars (Gen_Par) /= Chars (Gen_Anc) then
9278 -- The actual may be obtained through several instantiations. Its
9279 -- scope must itself be an instance of a generic declared in the
9280 -- same scope as the formal. Any other case is detected above.
9282 elsif not Is_Generic_Instance (Scope (Gen_Par)) then
9286 return Generic_Parent (Parent (Scope (Gen_Par))) = Scope (Gen_Anc);
9290 ---------------------------
9291 -- Process_Nested_Formal --
9292 ---------------------------
9294 procedure Process_Nested_Formal (Formal : Entity_Id) is
9298 if Present (Associated_Formal_Package (Formal))
9299 and then Box_Present (Parent (Associated_Formal_Package (Formal)))
9301 Ent := First_Entity (Formal);
9302 while Present (Ent) loop
9303 Set_Is_Hidden (Ent, False);
9304 Set_Is_Visible_Formal (Ent);
9305 Set_Is_Potentially_Use_Visible
9306 (Ent, Is_Potentially_Use_Visible (Formal));
9308 if Ekind (Ent) = E_Package then
9309 exit when Renamed_Entity (Ent) = Renamed_Entity (Formal);
9310 Process_Nested_Formal (Ent);
9316 end Process_Nested_Formal;
9318 -- Start of processing for Instantiate_Formal_Package
9323 if not Is_Entity_Name (Actual)
9324 or else Ekind (Entity (Actual)) /= E_Package
9327 ("expect package instance to instantiate formal", Actual);
9328 Abandon_Instantiation (Actual);
9329 raise Program_Error;
9332 Actual_Pack := Entity (Actual);
9333 Set_Is_Instantiated (Actual_Pack);
9335 -- The actual may be a renamed package, or an outer generic formal
9336 -- package whose instantiation is converted into a renaming.
9338 if Present (Renamed_Object (Actual_Pack)) then
9339 Actual_Pack := Renamed_Object (Actual_Pack);
9342 if Nkind (Analyzed_Formal) = N_Formal_Package_Declaration then
9343 Gen_Parent := Get_Instance_Of (Entity (Name (Analyzed_Formal)));
9344 Formal_Pack := Defining_Identifier (Analyzed_Formal);
9347 Generic_Parent (Specification (Analyzed_Formal));
9349 Defining_Unit_Name (Specification (Analyzed_Formal));
9352 if Nkind (Parent (Actual_Pack)) = N_Defining_Program_Unit_Name then
9353 Parent_Spec := Package_Specification (Actual_Pack);
9355 Parent_Spec := Parent (Actual_Pack);
9358 if Gen_Parent = Any_Id then
9360 ("previous error in declaration of formal package", Actual);
9361 Abandon_Instantiation (Actual);
9364 Is_Instance_Of (Parent_Spec, Get_Instance_Of (Gen_Parent))
9370 ("actual parameter must be instance of&", Actual, Gen_Parent);
9371 Abandon_Instantiation (Actual);
9374 Set_Instance_Of (Defining_Identifier (Formal), Actual_Pack);
9375 Map_Formal_Package_Entities (Formal_Pack, Actual_Pack);
9378 Make_Package_Renaming_Declaration (Loc,
9379 Defining_Unit_Name => New_Copy (Defining_Identifier (Formal)),
9380 Name => New_Occurrence_Of (Actual_Pack, Loc));
9382 Set_Associated_Formal_Package
9383 (Defining_Unit_Name (Nod), Defining_Identifier (Formal));
9384 Decls := New_List (Nod);
9386 -- If the formal F has a box, then the generic declarations are
9387 -- visible in the generic G. In an instance of G, the corresponding
9388 -- entities in the actual for F (which are the actuals for the
9389 -- instantiation of the generic that F denotes) must also be made
9390 -- visible for analysis of the current instance. On exit from the
9391 -- current instance, those entities are made private again. If the
9392 -- actual is currently in use, these entities are also use-visible.
9394 -- The loop through the actual entities also steps through the formal
9395 -- entities and enters associations from formals to actuals into the
9396 -- renaming map. This is necessary to properly handle checking of
9397 -- actual parameter associations for later formals that depend on
9398 -- actuals declared in the formal package.
9400 -- In Ada 2005, partial parameterization requires that we make
9401 -- visible the actuals corresponding to formals that were defaulted
9402 -- in the formal package. There formals are identified because they
9403 -- remain formal generics within the formal package, rather than
9404 -- being renamings of the actuals supplied.
9407 Gen_Decl : constant Node_Id :=
9408 Unit_Declaration_Node (Gen_Parent);
9409 Formals : constant List_Id :=
9410 Generic_Formal_Declarations (Gen_Decl);
9412 Actual_Ent : Entity_Id;
9413 Actual_Of_Formal : Node_Id;
9414 Formal_Node : Node_Id;
9415 Formal_Ent : Entity_Id;
9418 if Present (Formals) then
9419 Formal_Node := First_Non_Pragma (Formals);
9421 Formal_Node := Empty;
9424 Actual_Ent := First_Entity (Actual_Pack);
9426 First (Visible_Declarations (Specification (Analyzed_Formal)));
9427 while Present (Actual_Ent)
9428 and then Actual_Ent /= First_Private_Entity (Actual_Pack)
9430 if Present (Formal_Node) then
9431 Formal_Ent := Get_Formal_Entity (Formal_Node);
9433 if Present (Formal_Ent) then
9434 Find_Matching_Actual (Formal_Node, Actual_Ent);
9436 (Formal_Node, Formal_Ent, Actual_Ent);
9438 -- We iterate at the same time over the actuals of the
9439 -- local package created for the formal, to determine
9440 -- which one of the formals of the original generic were
9441 -- defaulted in the formal. The corresponding actual
9442 -- entities are visible in the enclosing instance.
9444 if Box_Present (Formal)
9446 (Present (Actual_Of_Formal)
9449 (Get_Formal_Entity (Actual_Of_Formal)))
9451 Set_Is_Hidden (Actual_Ent, False);
9452 Set_Is_Visible_Formal (Actual_Ent);
9453 Set_Is_Potentially_Use_Visible
9454 (Actual_Ent, In_Use (Actual_Pack));
9456 if Ekind (Actual_Ent) = E_Package then
9457 Process_Nested_Formal (Actual_Ent);
9461 Set_Is_Hidden (Actual_Ent);
9462 Set_Is_Potentially_Use_Visible (Actual_Ent, False);
9466 Next_Non_Pragma (Formal_Node);
9467 Next (Actual_Of_Formal);
9470 -- No further formals to match, but the generic part may
9471 -- contain inherited operation that are not hidden in the
9472 -- enclosing instance.
9474 Next_Entity (Actual_Ent);
9478 -- Inherited subprograms generated by formal derived types are
9479 -- also visible if the types are.
9481 Actual_Ent := First_Entity (Actual_Pack);
9482 while Present (Actual_Ent)
9483 and then Actual_Ent /= First_Private_Entity (Actual_Pack)
9485 if Is_Overloadable (Actual_Ent)
9487 Nkind (Parent (Actual_Ent)) = N_Subtype_Declaration
9489 not Is_Hidden (Defining_Identifier (Parent (Actual_Ent)))
9491 Set_Is_Hidden (Actual_Ent, False);
9492 Set_Is_Potentially_Use_Visible
9493 (Actual_Ent, In_Use (Actual_Pack));
9496 Next_Entity (Actual_Ent);
9500 -- If the formal is not declared with a box, reanalyze it as an
9501 -- abbreviated instantiation, to verify the matching rules of 12.7.
9502 -- The actual checks are performed after the generic associations
9503 -- have been analyzed, to guarantee the same visibility for this
9504 -- instantiation and for the actuals.
9506 -- In Ada 2005, the generic associations for the formal can include
9507 -- defaulted parameters. These are ignored during check. This
9508 -- internal instantiation is removed from the tree after conformance
9509 -- checking, because it contains formal declarations for those
9510 -- defaulted parameters, and those should not reach the back-end.
9512 if not Box_Present (Formal) then
9514 I_Pack : constant Entity_Id :=
9515 Make_Temporary (Sloc (Actual), 'P');
9518 Set_Is_Internal (I_Pack);
9521 Make_Package_Instantiation (Sloc (Actual),
9522 Defining_Unit_Name => I_Pack,
9525 (Get_Instance_Of (Gen_Parent), Sloc (Actual)),
9526 Generic_Associations =>
9527 Generic_Associations (Formal)));
9533 end Instantiate_Formal_Package;
9535 -----------------------------------
9536 -- Instantiate_Formal_Subprogram --
9537 -----------------------------------
9539 function Instantiate_Formal_Subprogram
9542 Analyzed_Formal : Node_Id) return Node_Id
9544 Analyzed_S : constant Entity_Id :=
9545 Defining_Unit_Name (Specification (Analyzed_Formal));
9546 Formal_Sub : constant Entity_Id :=
9547 Defining_Unit_Name (Specification (Formal));
9549 function From_Parent_Scope (Subp : Entity_Id) return Boolean;
9550 -- If the generic is a child unit, the parent has been installed on the
9551 -- scope stack, but a default subprogram cannot resolve to something
9552 -- on the parent because that parent is not really part of the visible
9553 -- context (it is there to resolve explicit local entities). If the
9554 -- default has resolved in this way, we remove the entity from immediate
9555 -- visibility and analyze the node again to emit an error message or
9556 -- find another visible candidate.
9558 procedure Valid_Actual_Subprogram (Act : Node_Id);
9559 -- Perform legality check and raise exception on failure
9561 -----------------------
9562 -- From_Parent_Scope --
9563 -----------------------
9565 function From_Parent_Scope (Subp : Entity_Id) return Boolean is
9566 Gen_Scope : Node_Id;
9569 Gen_Scope := Scope (Analyzed_S);
9570 while Present (Gen_Scope) and then Is_Child_Unit (Gen_Scope) loop
9571 if Scope (Subp) = Scope (Gen_Scope) then
9575 Gen_Scope := Scope (Gen_Scope);
9579 end From_Parent_Scope;
9581 -----------------------------
9582 -- Valid_Actual_Subprogram --
9583 -----------------------------
9585 procedure Valid_Actual_Subprogram (Act : Node_Id) is
9589 if Is_Entity_Name (Act) then
9590 Act_E := Entity (Act);
9592 elsif Nkind (Act) = N_Selected_Component
9593 and then Is_Entity_Name (Selector_Name (Act))
9595 Act_E := Entity (Selector_Name (Act));
9601 if (Present (Act_E) and then Is_Overloadable (Act_E))
9602 or else Nkind_In (Act, N_Attribute_Reference,
9603 N_Indexed_Component,
9604 N_Character_Literal,
9605 N_Explicit_Dereference)
9611 ("expect subprogram or entry name in instantiation of &",
9612 Instantiation_Node, Formal_Sub);
9613 Abandon_Instantiation (Instantiation_Node);
9614 end Valid_Actual_Subprogram;
9618 Decl_Node : Node_Id;
9622 New_Subp : Entity_Id;
9624 -- Start of processing for Instantiate_Formal_Subprogram
9627 New_Spec := New_Copy_Tree (Specification (Formal));
9629 -- The tree copy has created the proper instantiation sloc for the
9630 -- new specification. Use this location for all other constructed
9633 Loc := Sloc (Defining_Unit_Name (New_Spec));
9635 -- Create new entity for the actual (New_Copy_Tree does not), and
9636 -- indicate that it is an actual.
9638 New_Subp := Make_Defining_Identifier (Loc, Chars (Formal_Sub));
9639 Set_Ekind (New_Subp, Ekind (Analyzed_S));
9640 Set_Is_Generic_Actual_Subprogram (New_Subp);
9641 Set_Defining_Unit_Name (New_Spec, New_Subp);
9643 -- Create new entities for the each of the formals in the specification
9644 -- of the renaming declaration built for the actual.
9646 if Present (Parameter_Specifications (New_Spec)) then
9652 F := First (Parameter_Specifications (New_Spec));
9653 while Present (F) loop
9654 F_Id := Defining_Identifier (F);
9656 Set_Defining_Identifier (F,
9657 Make_Defining_Identifier (Sloc (F_Id), Chars (F_Id)));
9663 -- Find entity of actual. If the actual is an attribute reference, it
9664 -- cannot be resolved here (its formal is missing) but is handled
9665 -- instead in Attribute_Renaming. If the actual is overloaded, it is
9666 -- fully resolved subsequently, when the renaming declaration for the
9667 -- formal is analyzed. If it is an explicit dereference, resolve the
9668 -- prefix but not the actual itself, to prevent interpretation as call.
9670 if Present (Actual) then
9671 Loc := Sloc (Actual);
9672 Set_Sloc (New_Spec, Loc);
9674 if Nkind (Actual) = N_Operator_Symbol then
9675 Find_Direct_Name (Actual);
9677 elsif Nkind (Actual) = N_Explicit_Dereference then
9678 Analyze (Prefix (Actual));
9680 elsif Nkind (Actual) /= N_Attribute_Reference then
9684 Valid_Actual_Subprogram (Actual);
9687 elsif Present (Default_Name (Formal)) then
9688 if not Nkind_In (Default_Name (Formal), N_Attribute_Reference,
9689 N_Selected_Component,
9690 N_Indexed_Component,
9691 N_Character_Literal)
9692 and then Present (Entity (Default_Name (Formal)))
9694 Nam := New_Occurrence_Of (Entity (Default_Name (Formal)), Loc);
9696 Nam := New_Copy (Default_Name (Formal));
9697 Set_Sloc (Nam, Loc);
9700 elsif Box_Present (Formal) then
9702 -- Actual is resolved at the point of instantiation. Create an
9703 -- identifier or operator with the same name as the formal.
9705 if Nkind (Formal_Sub) = N_Defining_Operator_Symbol then
9707 Make_Operator_Symbol (Loc,
9708 Chars => Chars (Formal_Sub),
9709 Strval => No_String);
9711 Nam := Make_Identifier (Loc, Chars (Formal_Sub));
9714 elsif Nkind (Specification (Formal)) = N_Procedure_Specification
9715 and then Null_Present (Specification (Formal))
9717 -- Generate null body for procedure, for use in the instance
9720 Make_Subprogram_Body (Loc,
9721 Specification => New_Spec,
9722 Declarations => New_List,
9723 Handled_Statement_Sequence =>
9724 Make_Handled_Sequence_Of_Statements (Loc,
9725 Statements => New_List (Make_Null_Statement (Loc))));
9727 Set_Is_Intrinsic_Subprogram (Defining_Unit_Name (New_Spec));
9731 Error_Msg_Sloc := Sloc (Scope (Analyzed_S));
9733 ("missing actual&", Instantiation_Node, Formal_Sub);
9735 ("\in instantiation of & declared#",
9736 Instantiation_Node, Scope (Analyzed_S));
9737 Abandon_Instantiation (Instantiation_Node);
9741 Make_Subprogram_Renaming_Declaration (Loc,
9742 Specification => New_Spec,
9745 -- If we do not have an actual and the formal specified <> then set to
9746 -- get proper default.
9748 if No (Actual) and then Box_Present (Formal) then
9749 Set_From_Default (Decl_Node);
9752 -- Gather possible interpretations for the actual before analyzing the
9753 -- instance. If overloaded, it will be resolved when analyzing the
9754 -- renaming declaration.
9756 if Box_Present (Formal) and then No (Actual) then
9759 if Is_Child_Unit (Scope (Analyzed_S))
9760 and then Present (Entity (Nam))
9762 if not Is_Overloaded (Nam) then
9763 if From_Parent_Scope (Entity (Nam)) then
9764 Set_Is_Immediately_Visible (Entity (Nam), False);
9765 Set_Entity (Nam, Empty);
9766 Set_Etype (Nam, Empty);
9769 Set_Is_Immediately_Visible (Entity (Nam));
9778 Get_First_Interp (Nam, I, It);
9779 while Present (It.Nam) loop
9780 if From_Parent_Scope (It.Nam) then
9784 Get_Next_Interp (I, It);
9791 -- The generic instantiation freezes the actual. This can only be done
9792 -- once the actual is resolved, in the analysis of the renaming
9793 -- declaration. To make the formal subprogram entity available, we set
9794 -- Corresponding_Formal_Spec to point to the formal subprogram entity.
9795 -- This is also needed in Analyze_Subprogram_Renaming for the processing
9796 -- of formal abstract subprograms.
9798 Set_Corresponding_Formal_Spec (Decl_Node, Analyzed_S);
9800 -- We cannot analyze the renaming declaration, and thus find the actual,
9801 -- until all the actuals are assembled in the instance. For subsequent
9802 -- checks of other actuals, indicate the node that will hold the
9803 -- instance of this formal.
9805 Set_Instance_Of (Analyzed_S, Nam);
9807 if Nkind (Actual) = N_Selected_Component
9808 and then Is_Task_Type (Etype (Prefix (Actual)))
9809 and then not Is_Frozen (Etype (Prefix (Actual)))
9811 -- The renaming declaration will create a body, which must appear
9812 -- outside of the instantiation, We move the renaming declaration
9813 -- out of the instance, and create an additional renaming inside,
9814 -- to prevent freezing anomalies.
9817 Anon_Id : constant Entity_Id := Make_Temporary (Loc, 'E');
9820 Set_Defining_Unit_Name (New_Spec, Anon_Id);
9821 Insert_Before (Instantiation_Node, Decl_Node);
9822 Analyze (Decl_Node);
9824 -- Now create renaming within the instance
9827 Make_Subprogram_Renaming_Declaration (Loc,
9828 Specification => New_Copy_Tree (New_Spec),
9829 Name => New_Occurrence_Of (Anon_Id, Loc));
9831 Set_Defining_Unit_Name (Specification (Decl_Node),
9832 Make_Defining_Identifier (Loc, Chars (Formal_Sub)));
9837 end Instantiate_Formal_Subprogram;
9839 ------------------------
9840 -- Instantiate_Object --
9841 ------------------------
9843 function Instantiate_Object
9846 Analyzed_Formal : Node_Id) return List_Id
9848 Gen_Obj : constant Entity_Id := Defining_Identifier (Formal);
9849 A_Gen_Obj : constant Entity_Id :=
9850 Defining_Identifier (Analyzed_Formal);
9851 Acc_Def : Node_Id := Empty;
9852 Act_Assoc : constant Node_Id := Parent (Actual);
9853 Actual_Decl : Node_Id := Empty;
9854 Decl_Node : Node_Id;
9857 List : constant List_Id := New_List;
9858 Loc : constant Source_Ptr := Sloc (Actual);
9859 Orig_Ftyp : constant Entity_Id := Etype (A_Gen_Obj);
9860 Subt_Decl : Node_Id := Empty;
9861 Subt_Mark : Node_Id := Empty;
9864 if Present (Subtype_Mark (Formal)) then
9865 Subt_Mark := Subtype_Mark (Formal);
9867 Check_Access_Definition (Formal);
9868 Acc_Def := Access_Definition (Formal);
9871 -- Sloc for error message on missing actual
9873 Error_Msg_Sloc := Sloc (Scope (A_Gen_Obj));
9875 if Get_Instance_Of (Gen_Obj) /= Gen_Obj then
9876 Error_Msg_N ("duplicate instantiation of generic parameter", Actual);
9879 Set_Parent (List, Parent (Actual));
9883 if Out_Present (Formal) then
9885 -- An IN OUT generic actual must be a name. The instantiation is a
9886 -- renaming declaration. The actual is the name being renamed. We
9887 -- use the actual directly, rather than a copy, because it is not
9888 -- used further in the list of actuals, and because a copy or a use
9889 -- of relocate_node is incorrect if the instance is nested within a
9890 -- generic. In order to simplify ASIS searches, the Generic_Parent
9891 -- field links the declaration to the generic association.
9895 ("missing actual &",
9896 Instantiation_Node, Gen_Obj);
9898 ("\in instantiation of & declared#",
9899 Instantiation_Node, Scope (A_Gen_Obj));
9900 Abandon_Instantiation (Instantiation_Node);
9903 if Present (Subt_Mark) then
9905 Make_Object_Renaming_Declaration (Loc,
9906 Defining_Identifier => New_Copy (Gen_Obj),
9907 Subtype_Mark => New_Copy_Tree (Subt_Mark),
9910 else pragma Assert (Present (Acc_Def));
9912 Make_Object_Renaming_Declaration (Loc,
9913 Defining_Identifier => New_Copy (Gen_Obj),
9914 Access_Definition => New_Copy_Tree (Acc_Def),
9918 Set_Corresponding_Generic_Association (Decl_Node, Act_Assoc);
9920 -- The analysis of the actual may produce Insert_Action nodes, so
9921 -- the declaration must have a context in which to attach them.
9923 Append (Decl_Node, List);
9926 -- Return if the analysis of the actual reported some error
9928 if Etype (Actual) = Any_Type then
9932 -- This check is performed here because Analyze_Object_Renaming will
9933 -- not check it when Comes_From_Source is False. Note though that the
9934 -- check for the actual being the name of an object will be performed
9935 -- in Analyze_Object_Renaming.
9937 if Is_Object_Reference (Actual)
9938 and then Is_Dependent_Component_Of_Mutable_Object (Actual)
9941 ("illegal discriminant-dependent component for in out parameter",
9945 -- The actual has to be resolved in order to check that it is a
9946 -- variable (due to cases such as F (1), where F returns access to
9947 -- an array, and for overloaded prefixes).
9949 Ftyp := Get_Instance_Of (Etype (A_Gen_Obj));
9951 -- If the type of the formal is not itself a formal, and the current
9952 -- unit is a child unit, the formal type must be declared in a
9953 -- parent, and must be retrieved by visibility.
9956 and then Is_Generic_Unit (Scope (Ftyp))
9957 and then Is_Child_Unit (Scope (A_Gen_Obj))
9960 Temp : constant Node_Id :=
9961 New_Copy_Tree (Subtype_Mark (Analyzed_Formal));
9963 Set_Entity (Temp, Empty);
9965 Ftyp := Entity (Temp);
9969 if Is_Private_Type (Ftyp)
9970 and then not Is_Private_Type (Etype (Actual))
9971 and then (Base_Type (Full_View (Ftyp)) = Base_Type (Etype (Actual))
9972 or else Base_Type (Etype (Actual)) = Ftyp)
9974 -- If the actual has the type of the full view of the formal, or
9975 -- else a non-private subtype of the formal, then the visibility
9976 -- of the formal type has changed. Add to the actuals a subtype
9977 -- declaration that will force the exchange of views in the body
9978 -- of the instance as well.
9981 Make_Subtype_Declaration (Loc,
9982 Defining_Identifier => Make_Temporary (Loc, 'P'),
9983 Subtype_Indication => New_Occurrence_Of (Ftyp, Loc));
9985 Prepend (Subt_Decl, List);
9987 Prepend_Elmt (Full_View (Ftyp), Exchanged_Views);
9988 Exchange_Declarations (Ftyp);
9991 Resolve (Actual, Ftyp);
9993 if not Denotes_Variable (Actual) then
9994 Error_Msg_NE ("actual for& must be a variable", Actual, Gen_Obj);
9996 elsif Base_Type (Ftyp) /= Base_Type (Etype (Actual)) then
9998 -- Ada 2005 (AI-423): For a generic formal object of mode in out,
9999 -- the type of the actual shall resolve to a specific anonymous
10002 if Ada_Version < Ada_2005
10003 or else Ekind (Base_Type (Ftyp)) /=
10004 E_Anonymous_Access_Type
10005 or else Ekind (Base_Type (Etype (Actual))) /=
10006 E_Anonymous_Access_Type
10009 ("type of actual does not match type of&", Actual, Gen_Obj);
10013 Note_Possible_Modification (Actual, Sure => True);
10015 -- Check for instantiation of atomic/volatile actual for
10016 -- non-atomic/volatile formal (RM C.6 (12)).
10018 if Is_Atomic_Object (Actual) and then not Is_Atomic (Orig_Ftyp) then
10020 ("cannot instantiate non-atomic formal object "
10021 & "with atomic actual", Actual);
10023 elsif Is_Volatile_Object (Actual) and then not Is_Volatile (Orig_Ftyp)
10026 ("cannot instantiate non-volatile formal object "
10027 & "with volatile actual", Actual);
10030 -- Formal in-parameter
10033 -- The instantiation of a generic formal in-parameter is constant
10034 -- declaration. The actual is the expression for that declaration.
10036 if Present (Actual) then
10037 if Present (Subt_Mark) then
10039 else pragma Assert (Present (Acc_Def));
10044 Make_Object_Declaration (Loc,
10045 Defining_Identifier => New_Copy (Gen_Obj),
10046 Constant_Present => True,
10047 Null_Exclusion_Present => Null_Exclusion_Present (Formal),
10048 Object_Definition => New_Copy_Tree (Def),
10049 Expression => Actual);
10051 Set_Corresponding_Generic_Association (Decl_Node, Act_Assoc);
10053 -- A generic formal object of a tagged type is defined to be
10054 -- aliased so the new constant must also be treated as aliased.
10056 if Is_Tagged_Type (Etype (A_Gen_Obj)) then
10057 Set_Aliased_Present (Decl_Node);
10060 Append (Decl_Node, List);
10062 -- No need to repeat (pre-)analysis of some expression nodes
10063 -- already handled in Preanalyze_Actuals.
10065 if Nkind (Actual) /= N_Allocator then
10068 -- Return if the analysis of the actual reported some error
10070 if Etype (Actual) = Any_Type then
10076 Formal_Type : constant Entity_Id := Etype (A_Gen_Obj);
10080 Typ := Get_Instance_Of (Formal_Type);
10082 -- If the actual appears in the current or an enclosing scope,
10083 -- use its type directly. This is relevant if it has an actual
10084 -- subtype that is distinct from its nominal one. This cannot
10085 -- be done in general because the type of the actual may
10086 -- depend on other actuals, and only be fully determined when
10087 -- the enclosing instance is analyzed.
10089 if Present (Etype (Actual))
10090 and then Is_Constr_Subt_For_U_Nominal (Etype (Actual))
10092 Freeze_Before (Instantiation_Node, Etype (Actual));
10094 Freeze_Before (Instantiation_Node, Typ);
10097 -- If the actual is an aggregate, perform name resolution on
10098 -- its components (the analysis of an aggregate does not do it)
10099 -- to capture local names that may be hidden if the generic is
10102 if Nkind (Actual) = N_Aggregate then
10103 Preanalyze_And_Resolve (Actual, Typ);
10106 if Is_Limited_Type (Typ)
10107 and then not OK_For_Limited_Init (Typ, Actual)
10110 ("initialization not allowed for limited types", Actual);
10111 Explain_Limited_Type (Typ, Actual);
10115 elsif Present (Default_Expression (Formal)) then
10117 -- Use default to construct declaration
10119 if Present (Subt_Mark) then
10121 else pragma Assert (Present (Acc_Def));
10126 Make_Object_Declaration (Sloc (Formal),
10127 Defining_Identifier => New_Copy (Gen_Obj),
10128 Constant_Present => True,
10129 Null_Exclusion_Present => Null_Exclusion_Present (Formal),
10130 Object_Definition => New_Copy (Def),
10131 Expression => New_Copy_Tree
10132 (Default_Expression (Formal)));
10134 Append (Decl_Node, List);
10135 Set_Analyzed (Expression (Decl_Node), False);
10139 ("missing actual&",
10140 Instantiation_Node, Gen_Obj);
10141 Error_Msg_NE ("\in instantiation of & declared#",
10142 Instantiation_Node, Scope (A_Gen_Obj));
10144 if Is_Scalar_Type (Etype (A_Gen_Obj)) then
10146 -- Create dummy constant declaration so that instance can be
10147 -- analyzed, to minimize cascaded visibility errors.
10149 if Present (Subt_Mark) then
10151 else pragma Assert (Present (Acc_Def));
10156 Make_Object_Declaration (Loc,
10157 Defining_Identifier => New_Copy (Gen_Obj),
10158 Constant_Present => True,
10159 Null_Exclusion_Present => Null_Exclusion_Present (Formal),
10160 Object_Definition => New_Copy (Def),
10162 Make_Attribute_Reference (Sloc (Gen_Obj),
10163 Attribute_Name => Name_First,
10164 Prefix => New_Copy (Def)));
10166 Append (Decl_Node, List);
10169 Abandon_Instantiation (Instantiation_Node);
10174 if Nkind (Actual) in N_Has_Entity then
10175 Actual_Decl := Parent (Entity (Actual));
10178 -- Ada 2005 (AI-423): For a formal object declaration with a null
10179 -- exclusion or an access definition that has a null exclusion: If the
10180 -- actual matching the formal object declaration denotes a generic
10181 -- formal object of another generic unit G, and the instantiation
10182 -- containing the actual occurs within the body of G or within the body
10183 -- of a generic unit declared within the declarative region of G, then
10184 -- the declaration of the formal object of G must have a null exclusion.
10185 -- Otherwise, the subtype of the actual matching the formal object
10186 -- declaration shall exclude null.
10188 if Ada_Version >= Ada_2005
10189 and then Present (Actual_Decl)
10190 and then Nkind_In (Actual_Decl, N_Formal_Object_Declaration,
10191 N_Object_Declaration)
10192 and then Nkind (Analyzed_Formal) = N_Formal_Object_Declaration
10193 and then not Has_Null_Exclusion (Actual_Decl)
10194 and then Has_Null_Exclusion (Analyzed_Formal)
10196 Error_Msg_Sloc := Sloc (Analyzed_Formal);
10198 ("actual must exclude null to match generic formal#", Actual);
10201 -- An effectively volatile object cannot be used as an actual in
10202 -- a generic instance. The following check is only relevant when
10203 -- SPARK_Mode is on as it is not a standard Ada legality rule.
10206 and then Present (Actual)
10207 and then Is_Effectively_Volatile_Object (Actual)
10210 ("volatile object cannot act as actual in generic instantiation "
10211 & "(SPARK RM 7.1.3(8))", Actual);
10215 end Instantiate_Object;
10217 ------------------------------
10218 -- Instantiate_Package_Body --
10219 ------------------------------
10221 procedure Instantiate_Package_Body
10222 (Body_Info : Pending_Body_Info;
10223 Inlined_Body : Boolean := False;
10224 Body_Optional : Boolean := False)
10226 Act_Decl : constant Node_Id := Body_Info.Act_Decl;
10227 Inst_Node : constant Node_Id := Body_Info.Inst_Node;
10228 Loc : constant Source_Ptr := Sloc (Inst_Node);
10230 Gen_Id : constant Node_Id := Name (Inst_Node);
10231 Gen_Unit : constant Entity_Id := Get_Generic_Entity (Inst_Node);
10232 Gen_Decl : constant Node_Id := Unit_Declaration_Node (Gen_Unit);
10233 Act_Spec : constant Node_Id := Specification (Act_Decl);
10234 Act_Decl_Id : constant Entity_Id := Defining_Entity (Act_Spec);
10236 Act_Body_Name : Node_Id;
10237 Gen_Body : Node_Id;
10238 Gen_Body_Id : Node_Id;
10239 Act_Body : Node_Id;
10240 Act_Body_Id : Entity_Id;
10242 Parent_Installed : Boolean := False;
10243 Save_Style_Check : constant Boolean := Style_Check;
10245 Par_Ent : Entity_Id := Empty;
10246 Par_Vis : Boolean := False;
10248 Vis_Prims_List : Elist_Id := No_Elist;
10249 -- List of primitives made temporarily visible in the instantiation
10250 -- to match the visibility of the formal type
10252 procedure Check_Initialized_Types;
10253 -- In a generic package body, an entity of a generic private type may
10254 -- appear uninitialized. This is suspicious, unless the actual is a
10255 -- fully initialized type.
10257 -----------------------------
10258 -- Check_Initialized_Types --
10259 -----------------------------
10261 procedure Check_Initialized_Types is
10263 Formal : Entity_Id;
10264 Actual : Entity_Id;
10265 Uninit_Var : Entity_Id;
10268 Decl := First (Generic_Formal_Declarations (Gen_Decl));
10269 while Present (Decl) loop
10270 Uninit_Var := Empty;
10272 if Nkind (Decl) = N_Private_Extension_Declaration then
10273 Uninit_Var := Uninitialized_Variable (Decl);
10275 elsif Nkind (Decl) = N_Formal_Type_Declaration
10276 and then Nkind (Formal_Type_Definition (Decl)) =
10277 N_Formal_Private_Type_Definition
10280 Uninitialized_Variable (Formal_Type_Definition (Decl));
10283 if Present (Uninit_Var) then
10284 Formal := Defining_Identifier (Decl);
10285 Actual := First_Entity (Act_Decl_Id);
10287 -- For each formal there is a subtype declaration that renames
10288 -- the actual and has the same name as the formal. Locate the
10289 -- formal for warning message about uninitialized variables
10290 -- in the generic, for which the actual type should be a fully
10291 -- initialized type.
10293 while Present (Actual) loop
10294 exit when Ekind (Actual) = E_Package
10295 and then Present (Renamed_Object (Actual));
10297 if Chars (Actual) = Chars (Formal)
10298 and then not Is_Scalar_Type (Actual)
10299 and then not Is_Fully_Initialized_Type (Actual)
10300 and then Warn_On_No_Value_Assigned
10302 Error_Msg_Node_2 := Formal;
10304 ("generic unit has uninitialized variable& of "
10305 & "formal private type &?v?", Actual, Uninit_Var);
10307 ("actual type for& should be fully initialized type?v?",
10312 Next_Entity (Actual);
10318 end Check_Initialized_Types;
10320 -- Start of processing for Instantiate_Package_Body
10323 Gen_Body_Id := Corresponding_Body (Gen_Decl);
10325 -- The instance body may already have been processed, as the parent of
10326 -- another instance that is inlined (Load_Parent_Of_Generic).
10328 if Present (Corresponding_Body (Instance_Spec (Inst_Node))) then
10332 Expander_Mode_Save_And_Set (Body_Info.Expander_Status);
10334 -- Re-establish the state of information on which checks are suppressed.
10335 -- This information was set in Body_Info at the point of instantiation,
10336 -- and now we restore it so that the instance is compiled using the
10337 -- check status at the instantiation (RM 11.5 (7.2/2), AI95-00224-01).
10339 Local_Suppress_Stack_Top := Body_Info.Local_Suppress_Stack_Top;
10340 Scope_Suppress := Body_Info.Scope_Suppress;
10341 Opt.Ada_Version := Body_Info.Version;
10342 Opt.Ada_Version_Pragma := Body_Info.Version_Pragma;
10343 Restore_Warnings (Body_Info.Warnings);
10344 Opt.SPARK_Mode := Body_Info.SPARK_Mode;
10345 Opt.SPARK_Mode_Pragma := Body_Info.SPARK_Mode_Pragma;
10347 if No (Gen_Body_Id) then
10349 -- Do not look for parent of generic body if none is required.
10350 -- This may happen when the routine is called as part of the
10351 -- Pending_Instantiations processing, when nested instances
10352 -- may precede the one generated from the main unit.
10354 if not Unit_Requires_Body (Defining_Entity (Gen_Decl))
10355 and then Body_Optional
10359 Load_Parent_Of_Generic
10360 (Inst_Node, Specification (Gen_Decl), Body_Optional);
10361 Gen_Body_Id := Corresponding_Body (Gen_Decl);
10365 -- Establish global variable for sloc adjustment and for error recovery
10367 Instantiation_Node := Inst_Node;
10369 if Present (Gen_Body_Id) then
10370 Save_Env (Gen_Unit, Act_Decl_Id);
10371 Style_Check := False;
10372 Current_Sem_Unit := Body_Info.Current_Sem_Unit;
10374 Gen_Body := Unit_Declaration_Node (Gen_Body_Id);
10376 Create_Instantiation_Source
10377 (Inst_Node, Gen_Body_Id, False, S_Adjustment);
10381 (Original_Node (Gen_Body), Empty, Instantiating => True);
10383 -- Build new name (possibly qualified) for body declaration
10385 Act_Body_Id := New_Copy (Act_Decl_Id);
10387 -- Some attributes of spec entity are not inherited by body entity
10389 Set_Handler_Records (Act_Body_Id, No_List);
10391 if Nkind (Defining_Unit_Name (Act_Spec)) =
10392 N_Defining_Program_Unit_Name
10395 Make_Defining_Program_Unit_Name (Loc,
10396 Name => New_Copy_Tree (Name (Defining_Unit_Name (Act_Spec))),
10397 Defining_Identifier => Act_Body_Id);
10399 Act_Body_Name := Act_Body_Id;
10402 Set_Defining_Unit_Name (Act_Body, Act_Body_Name);
10404 Set_Corresponding_Spec (Act_Body, Act_Decl_Id);
10405 Check_Generic_Actuals (Act_Decl_Id, False);
10406 Check_Initialized_Types;
10408 -- Install primitives hidden at the point of the instantiation but
10409 -- visible when processing the generic formals
10415 E := First_Entity (Act_Decl_Id);
10416 while Present (E) loop
10418 and then Is_Generic_Actual_Type (E)
10419 and then Is_Tagged_Type (E)
10421 Install_Hidden_Primitives
10422 (Prims_List => Vis_Prims_List,
10423 Gen_T => Generic_Parent_Type (Parent (E)),
10431 -- If it is a child unit, make the parent instance (which is an
10432 -- instance of the parent of the generic) visible. The parent
10433 -- instance is the prefix of the name of the generic unit.
10435 if Ekind (Scope (Gen_Unit)) = E_Generic_Package
10436 and then Nkind (Gen_Id) = N_Expanded_Name
10438 Par_Ent := Entity (Prefix (Gen_Id));
10439 Par_Vis := Is_Immediately_Visible (Par_Ent);
10440 Install_Parent (Par_Ent, In_Body => True);
10441 Parent_Installed := True;
10443 elsif Is_Child_Unit (Gen_Unit) then
10444 Par_Ent := Scope (Gen_Unit);
10445 Par_Vis := Is_Immediately_Visible (Par_Ent);
10446 Install_Parent (Par_Ent, In_Body => True);
10447 Parent_Installed := True;
10450 -- If the instantiation is a library unit, and this is the main unit,
10451 -- then build the resulting compilation unit nodes for the instance.
10452 -- If this is a compilation unit but it is not the main unit, then it
10453 -- is the body of a unit in the context, that is being compiled
10454 -- because it is encloses some inlined unit or another generic unit
10455 -- being instantiated. In that case, this body is not part of the
10456 -- current compilation, and is not attached to the tree, but its
10457 -- parent must be set for analysis.
10459 if Nkind (Parent (Inst_Node)) = N_Compilation_Unit then
10461 -- Replace instance node with body of instance, and create new
10462 -- node for corresponding instance declaration.
10464 Build_Instance_Compilation_Unit_Nodes
10465 (Inst_Node, Act_Body, Act_Decl);
10466 Analyze (Inst_Node);
10468 if Parent (Inst_Node) = Cunit (Main_Unit) then
10470 -- If the instance is a child unit itself, then set the scope
10471 -- of the expanded body to be the parent of the instantiation
10472 -- (ensuring that the fully qualified name will be generated
10473 -- for the elaboration subprogram).
10475 if Nkind (Defining_Unit_Name (Act_Spec)) =
10476 N_Defining_Program_Unit_Name
10478 Set_Scope (Defining_Entity (Inst_Node), Scope (Act_Decl_Id));
10482 -- Case where instantiation is not a library unit
10485 -- If this is an early instantiation, i.e. appears textually
10486 -- before the corresponding body and must be elaborated first,
10487 -- indicate that the body instance is to be delayed.
10489 Install_Body (Act_Body, Inst_Node, Gen_Body, Gen_Decl);
10491 -- Now analyze the body. We turn off all checks if this is an
10492 -- internal unit, since there is no reason to have checks on for
10493 -- any predefined run-time library code. All such code is designed
10494 -- to be compiled with checks off.
10496 -- Note that we do NOT apply this criterion to children of GNAT
10497 -- The latter units must suppress checks explicitly if needed.
10499 if Is_Predefined_File_Name
10500 (Unit_File_Name (Get_Source_Unit (Gen_Decl)))
10502 Analyze (Act_Body, Suppress => All_Checks);
10504 Analyze (Act_Body);
10508 Inherit_Context (Gen_Body, Inst_Node);
10510 -- Remove the parent instances if they have been placed on the scope
10511 -- stack to compile the body.
10513 if Parent_Installed then
10514 Remove_Parent (In_Body => True);
10516 -- Restore the previous visibility of the parent
10518 Set_Is_Immediately_Visible (Par_Ent, Par_Vis);
10521 Restore_Hidden_Primitives (Vis_Prims_List);
10522 Restore_Private_Views (Act_Decl_Id);
10524 -- Remove the current unit from visibility if this is an instance
10525 -- that is not elaborated on the fly for inlining purposes.
10527 if not Inlined_Body then
10528 Set_Is_Immediately_Visible (Act_Decl_Id, False);
10532 Style_Check := Save_Style_Check;
10534 -- If we have no body, and the unit requires a body, then complain. This
10535 -- complaint is suppressed if we have detected other errors (since a
10536 -- common reason for missing the body is that it had errors).
10537 -- In CodePeer mode, a warning has been emitted already, no need for
10538 -- further messages.
10540 elsif Unit_Requires_Body (Gen_Unit)
10541 and then not Body_Optional
10543 if CodePeer_Mode then
10546 elsif Serious_Errors_Detected = 0 then
10548 ("cannot find body of generic package &", Inst_Node, Gen_Unit);
10550 -- Don't attempt to perform any cleanup actions if some other error
10551 -- was already detected, since this can cause blowups.
10557 -- Case of package that does not need a body
10560 -- If the instantiation of the declaration is a library unit, rewrite
10561 -- the original package instantiation as a package declaration in the
10562 -- compilation unit node.
10564 if Nkind (Parent (Inst_Node)) = N_Compilation_Unit then
10565 Set_Parent_Spec (Act_Decl, Parent_Spec (Inst_Node));
10566 Rewrite (Inst_Node, Act_Decl);
10568 -- Generate elaboration entity, in case spec has elaboration code.
10569 -- This cannot be done when the instance is analyzed, because it
10570 -- is not known yet whether the body exists.
10572 Set_Elaboration_Entity_Required (Act_Decl_Id, False);
10573 Build_Elaboration_Entity (Parent (Inst_Node), Act_Decl_Id);
10575 -- If the instantiation is not a library unit, then append the
10576 -- declaration to the list of implicitly generated entities, unless
10577 -- it is already a list member which means that it was already
10580 elsif not Is_List_Member (Act_Decl) then
10581 Mark_Rewrite_Insertion (Act_Decl);
10582 Insert_Before (Inst_Node, Act_Decl);
10586 Expander_Mode_Restore;
10587 end Instantiate_Package_Body;
10589 ---------------------------------
10590 -- Instantiate_Subprogram_Body --
10591 ---------------------------------
10593 procedure Instantiate_Subprogram_Body
10594 (Body_Info : Pending_Body_Info;
10595 Body_Optional : Boolean := False)
10597 Act_Decl : constant Node_Id := Body_Info.Act_Decl;
10598 Inst_Node : constant Node_Id := Body_Info.Inst_Node;
10599 Loc : constant Source_Ptr := Sloc (Inst_Node);
10600 Gen_Id : constant Node_Id := Name (Inst_Node);
10601 Gen_Unit : constant Entity_Id := Get_Generic_Entity (Inst_Node);
10602 Gen_Decl : constant Node_Id := Unit_Declaration_Node (Gen_Unit);
10603 Anon_Id : constant Entity_Id :=
10604 Defining_Unit_Name (Specification (Act_Decl));
10605 Pack_Id : constant Entity_Id :=
10606 Defining_Unit_Name (Parent (Act_Decl));
10608 Gen_Body : Node_Id;
10609 Gen_Body_Id : Node_Id;
10610 Act_Body : Node_Id;
10611 Pack_Body : Node_Id;
10612 Prev_Formal : Entity_Id;
10613 Ret_Expr : Node_Id;
10614 Unit_Renaming : Node_Id;
10616 Parent_Installed : Boolean := False;
10618 Saved_Style_Check : constant Boolean := Style_Check;
10619 Saved_Warnings : constant Warning_Record := Save_Warnings;
10621 Par_Ent : Entity_Id := Empty;
10622 Par_Vis : Boolean := False;
10625 Gen_Body_Id := Corresponding_Body (Gen_Decl);
10627 -- Subprogram body may have been created already because of an inline
10628 -- pragma, or because of multiple elaborations of the enclosing package
10629 -- when several instances of the subprogram appear in the main unit.
10631 if Present (Corresponding_Body (Act_Decl)) then
10635 Expander_Mode_Save_And_Set (Body_Info.Expander_Status);
10637 -- Re-establish the state of information on which checks are suppressed.
10638 -- This information was set in Body_Info at the point of instantiation,
10639 -- and now we restore it so that the instance is compiled using the
10640 -- check status at the instantiation (RM 11.5 (7.2/2), AI95-00224-01).
10642 Local_Suppress_Stack_Top := Body_Info.Local_Suppress_Stack_Top;
10643 Scope_Suppress := Body_Info.Scope_Suppress;
10644 Opt.Ada_Version := Body_Info.Version;
10645 Opt.Ada_Version_Pragma := Body_Info.Version_Pragma;
10646 Restore_Warnings (Body_Info.Warnings);
10647 Opt.SPARK_Mode := Body_Info.SPARK_Mode;
10648 Opt.SPARK_Mode_Pragma := Body_Info.SPARK_Mode_Pragma;
10650 if No (Gen_Body_Id) then
10652 -- For imported generic subprogram, no body to compile, complete
10653 -- the spec entity appropriately.
10655 if Is_Imported (Gen_Unit) then
10656 Set_Is_Imported (Anon_Id);
10657 Set_First_Rep_Item (Anon_Id, First_Rep_Item (Gen_Unit));
10658 Set_Interface_Name (Anon_Id, Interface_Name (Gen_Unit));
10659 Set_Convention (Anon_Id, Convention (Gen_Unit));
10660 Set_Has_Completion (Anon_Id);
10663 -- For other cases, compile the body
10666 Load_Parent_Of_Generic
10667 (Inst_Node, Specification (Gen_Decl), Body_Optional);
10668 Gen_Body_Id := Corresponding_Body (Gen_Decl);
10672 Instantiation_Node := Inst_Node;
10674 if Present (Gen_Body_Id) then
10675 Gen_Body := Unit_Declaration_Node (Gen_Body_Id);
10677 if Nkind (Gen_Body) = N_Subprogram_Body_Stub then
10679 -- Either body is not present, or context is non-expanding, as
10680 -- when compiling a subunit. Mark the instance as completed, and
10681 -- diagnose a missing body when needed.
10684 and then Operating_Mode = Generate_Code
10687 ("missing proper body for instantiation", Gen_Body);
10690 Set_Has_Completion (Anon_Id);
10694 Save_Env (Gen_Unit, Anon_Id);
10695 Style_Check := False;
10696 Current_Sem_Unit := Body_Info.Current_Sem_Unit;
10697 Create_Instantiation_Source
10705 (Original_Node (Gen_Body), Empty, Instantiating => True);
10707 -- Create proper defining name for the body, to correspond to
10708 -- the one in the spec.
10710 Set_Defining_Unit_Name (Specification (Act_Body),
10711 Make_Defining_Identifier
10712 (Sloc (Defining_Entity (Inst_Node)), Chars (Anon_Id)));
10713 Set_Corresponding_Spec (Act_Body, Anon_Id);
10714 Set_Has_Completion (Anon_Id);
10715 Check_Generic_Actuals (Pack_Id, False);
10717 -- Generate a reference to link the visible subprogram instance to
10718 -- the generic body, which for navigation purposes is the only
10719 -- available source for the instance.
10722 (Related_Instance (Pack_Id),
10723 Gen_Body_Id, 'b', Set_Ref => False, Force => True);
10725 -- If it is a child unit, make the parent instance (which is an
10726 -- instance of the parent of the generic) visible. The parent
10727 -- instance is the prefix of the name of the generic unit.
10729 if Ekind (Scope (Gen_Unit)) = E_Generic_Package
10730 and then Nkind (Gen_Id) = N_Expanded_Name
10732 Par_Ent := Entity (Prefix (Gen_Id));
10733 Par_Vis := Is_Immediately_Visible (Par_Ent);
10734 Install_Parent (Par_Ent, In_Body => True);
10735 Parent_Installed := True;
10737 elsif Is_Child_Unit (Gen_Unit) then
10738 Par_Ent := Scope (Gen_Unit);
10739 Par_Vis := Is_Immediately_Visible (Par_Ent);
10740 Install_Parent (Par_Ent, In_Body => True);
10741 Parent_Installed := True;
10744 -- Inside its body, a reference to the generic unit is a reference
10745 -- to the instance. The corresponding renaming is the first
10746 -- declaration in the body.
10749 Make_Subprogram_Renaming_Declaration (Loc,
10751 Copy_Generic_Node (
10752 Specification (Original_Node (Gen_Body)),
10754 Instantiating => True),
10755 Name => New_Occurrence_Of (Anon_Id, Loc));
10757 -- If there is a formal subprogram with the same name as the unit
10758 -- itself, do not add this renaming declaration. This is a temporary
10759 -- fix for one ACATS test. ???
10761 Prev_Formal := First_Entity (Pack_Id);
10762 while Present (Prev_Formal) loop
10763 if Chars (Prev_Formal) = Chars (Gen_Unit)
10764 and then Is_Overloadable (Prev_Formal)
10769 Next_Entity (Prev_Formal);
10772 if Present (Prev_Formal) then
10773 Decls := New_List (Act_Body);
10775 Decls := New_List (Unit_Renaming, Act_Body);
10778 -- The subprogram body is placed in the body of a dummy package body,
10779 -- whose spec contains the subprogram declaration as well as the
10780 -- renaming declarations for the generic parameters.
10782 Pack_Body := Make_Package_Body (Loc,
10783 Defining_Unit_Name => New_Copy (Pack_Id),
10784 Declarations => Decls);
10786 Set_Corresponding_Spec (Pack_Body, Pack_Id);
10788 -- If the instantiation is a library unit, then build resulting
10789 -- compilation unit nodes for the instance. The declaration of
10790 -- the enclosing package is the grandparent of the subprogram
10791 -- declaration. First replace the instantiation node as the unit
10792 -- of the corresponding compilation.
10794 if Nkind (Parent (Inst_Node)) = N_Compilation_Unit then
10795 if Parent (Inst_Node) = Cunit (Main_Unit) then
10796 Set_Unit (Parent (Inst_Node), Inst_Node);
10797 Build_Instance_Compilation_Unit_Nodes
10798 (Inst_Node, Pack_Body, Parent (Parent (Act_Decl)));
10799 Analyze (Inst_Node);
10801 Set_Parent (Pack_Body, Parent (Inst_Node));
10802 Analyze (Pack_Body);
10806 Insert_Before (Inst_Node, Pack_Body);
10807 Mark_Rewrite_Insertion (Pack_Body);
10808 Analyze (Pack_Body);
10810 if Expander_Active then
10811 Freeze_Subprogram_Body (Inst_Node, Gen_Body, Pack_Id);
10815 Inherit_Context (Gen_Body, Inst_Node);
10817 Restore_Private_Views (Pack_Id, False);
10819 if Parent_Installed then
10820 Remove_Parent (In_Body => True);
10822 -- Restore the previous visibility of the parent
10824 Set_Is_Immediately_Visible (Par_Ent, Par_Vis);
10828 Style_Check := Saved_Style_Check;
10829 Restore_Warnings (Saved_Warnings);
10831 -- Body not found. Error was emitted already. If there were no previous
10832 -- errors, this may be an instance whose scope is a premature instance.
10833 -- In that case we must insure that the (legal) program does raise
10834 -- program error if executed. We generate a subprogram body for this
10835 -- purpose. See DEC ac30vso.
10837 -- Should not reference proprietary DEC tests in comments ???
10839 elsif Serious_Errors_Detected = 0
10840 and then Nkind (Parent (Inst_Node)) /= N_Compilation_Unit
10842 if Body_Optional then
10845 elsif Ekind (Anon_Id) = E_Procedure then
10847 Make_Subprogram_Body (Loc,
10849 Make_Procedure_Specification (Loc,
10850 Defining_Unit_Name =>
10851 Make_Defining_Identifier (Loc, Chars (Anon_Id)),
10852 Parameter_Specifications =>
10854 (Parameter_Specifications (Parent (Anon_Id)))),
10856 Declarations => Empty_List,
10857 Handled_Statement_Sequence =>
10858 Make_Handled_Sequence_Of_Statements (Loc,
10861 Make_Raise_Program_Error (Loc,
10863 PE_Access_Before_Elaboration))));
10867 Make_Raise_Program_Error (Loc,
10868 Reason => PE_Access_Before_Elaboration);
10870 Set_Etype (Ret_Expr, (Etype (Anon_Id)));
10871 Set_Analyzed (Ret_Expr);
10874 Make_Subprogram_Body (Loc,
10876 Make_Function_Specification (Loc,
10877 Defining_Unit_Name =>
10878 Make_Defining_Identifier (Loc, Chars (Anon_Id)),
10879 Parameter_Specifications =>
10881 (Parameter_Specifications (Parent (Anon_Id))),
10882 Result_Definition =>
10883 New_Occurrence_Of (Etype (Anon_Id), Loc)),
10885 Declarations => Empty_List,
10886 Handled_Statement_Sequence =>
10887 Make_Handled_Sequence_Of_Statements (Loc,
10890 (Make_Simple_Return_Statement (Loc, Ret_Expr))));
10893 Pack_Body := Make_Package_Body (Loc,
10894 Defining_Unit_Name => New_Copy (Pack_Id),
10895 Declarations => New_List (Act_Body));
10897 Insert_After (Inst_Node, Pack_Body);
10898 Set_Corresponding_Spec (Pack_Body, Pack_Id);
10899 Analyze (Pack_Body);
10902 Expander_Mode_Restore;
10903 end Instantiate_Subprogram_Body;
10905 ----------------------
10906 -- Instantiate_Type --
10907 ----------------------
10909 function Instantiate_Type
10912 Analyzed_Formal : Node_Id;
10913 Actual_Decls : List_Id) return List_Id
10915 Gen_T : constant Entity_Id := Defining_Identifier (Formal);
10916 A_Gen_T : constant Entity_Id :=
10917 Defining_Identifier (Analyzed_Formal);
10918 Ancestor : Entity_Id := Empty;
10919 Def : constant Node_Id := Formal_Type_Definition (Formal);
10921 Decl_Node : Node_Id;
10922 Decl_Nodes : List_Id;
10926 procedure Diagnose_Predicated_Actual;
10927 -- There are a number of constructs in which a discrete type with
10928 -- predicates is illegal, e.g. as an index in an array type declaration.
10929 -- If a generic type is used is such a construct in a generic package
10930 -- declaration, it carries the flag No_Predicate_On_Actual. it is part
10931 -- of the generic contract that the actual cannot have predicates.
10933 procedure Validate_Array_Type_Instance;
10934 procedure Validate_Access_Subprogram_Instance;
10935 procedure Validate_Access_Type_Instance;
10936 procedure Validate_Derived_Type_Instance;
10937 procedure Validate_Derived_Interface_Type_Instance;
10938 procedure Validate_Discriminated_Formal_Type;
10939 procedure Validate_Interface_Type_Instance;
10940 procedure Validate_Private_Type_Instance;
10941 procedure Validate_Incomplete_Type_Instance;
10942 -- These procedures perform validation tests for the named case.
10943 -- Validate_Discriminated_Formal_Type is shared by formal private
10944 -- types and Ada 2012 formal incomplete types.
10946 function Subtypes_Match (Gen_T, Act_T : Entity_Id) return Boolean;
10947 -- Check that base types are the same and that the subtypes match
10948 -- statically. Used in several of the above.
10950 ---------------------------------
10951 -- Diagnose_Predicated_Actual --
10952 ---------------------------------
10954 procedure Diagnose_Predicated_Actual is
10956 if No_Predicate_On_Actual (A_Gen_T)
10957 and then Has_Predicates (Act_T)
10960 ("actual for& cannot be a type with predicate",
10961 Instantiation_Node, A_Gen_T);
10963 elsif No_Dynamic_Predicate_On_Actual (A_Gen_T)
10964 and then Has_Predicates (Act_T)
10965 and then not Has_Static_Predicate_Aspect (Act_T)
10968 ("actual for& cannot be a type with a dynamic predicate",
10969 Instantiation_Node, A_Gen_T);
10971 end Diagnose_Predicated_Actual;
10973 --------------------
10974 -- Subtypes_Match --
10975 --------------------
10977 function Subtypes_Match (Gen_T, Act_T : Entity_Id) return Boolean is
10978 T : constant Entity_Id := Get_Instance_Of (Gen_T);
10981 -- Some detailed comments would be useful here ???
10983 return ((Base_Type (T) = Act_T
10984 or else Base_Type (T) = Base_Type (Act_T))
10985 and then Subtypes_Statically_Match (T, Act_T))
10987 or else (Is_Class_Wide_Type (Gen_T)
10988 and then Is_Class_Wide_Type (Act_T)
10989 and then Subtypes_Match
10990 (Get_Instance_Of (Root_Type (Gen_T)),
10991 Root_Type (Act_T)))
10994 (Ekind_In (Gen_T, E_Anonymous_Access_Subprogram_Type,
10995 E_Anonymous_Access_Type)
10996 and then Ekind (Act_T) = Ekind (Gen_T)
10997 and then Subtypes_Statically_Match
10998 (Designated_Type (Gen_T), Designated_Type (Act_T)));
10999 end Subtypes_Match;
11001 -----------------------------------------
11002 -- Validate_Access_Subprogram_Instance --
11003 -----------------------------------------
11005 procedure Validate_Access_Subprogram_Instance is
11007 if not Is_Access_Type (Act_T)
11008 or else Ekind (Designated_Type (Act_T)) /= E_Subprogram_Type
11011 ("expect access type in instantiation of &", Actual, Gen_T);
11012 Abandon_Instantiation (Actual);
11015 -- According to AI05-288, actuals for access_to_subprograms must be
11016 -- subtype conformant with the generic formal. Previous to AI05-288
11017 -- only mode conformance was required.
11019 -- This is a binding interpretation that applies to previous versions
11020 -- of the language, no need to maintain previous weaker checks.
11022 Check_Subtype_Conformant
11023 (Designated_Type (Act_T),
11024 Designated_Type (A_Gen_T),
11028 if Ekind (Base_Type (Act_T)) = E_Access_Protected_Subprogram_Type then
11029 if Ekind (A_Gen_T) = E_Access_Subprogram_Type then
11031 ("protected access type not allowed for formal &",
11035 elsif Ekind (A_Gen_T) = E_Access_Protected_Subprogram_Type then
11037 ("expect protected access type for formal &",
11040 end Validate_Access_Subprogram_Instance;
11042 -----------------------------------
11043 -- Validate_Access_Type_Instance --
11044 -----------------------------------
11046 procedure Validate_Access_Type_Instance is
11047 Desig_Type : constant Entity_Id :=
11048 Find_Actual_Type (Designated_Type (A_Gen_T), A_Gen_T);
11049 Desig_Act : Entity_Id;
11052 if not Is_Access_Type (Act_T) then
11054 ("expect access type in instantiation of &", Actual, Gen_T);
11055 Abandon_Instantiation (Actual);
11058 if Is_Access_Constant (A_Gen_T) then
11059 if not Is_Access_Constant (Act_T) then
11061 ("actual type must be access-to-constant type", Actual);
11062 Abandon_Instantiation (Actual);
11065 if Is_Access_Constant (Act_T) then
11067 ("actual type must be access-to-variable type", Actual);
11068 Abandon_Instantiation (Actual);
11070 elsif Ekind (A_Gen_T) = E_General_Access_Type
11071 and then Ekind (Base_Type (Act_T)) /= E_General_Access_Type
11073 Error_Msg_N -- CODEFIX
11074 ("actual must be general access type!", Actual);
11075 Error_Msg_NE -- CODEFIX
11076 ("add ALL to }!", Actual, Act_T);
11077 Abandon_Instantiation (Actual);
11081 -- The designated subtypes, that is to say the subtypes introduced
11082 -- by an access type declaration (and not by a subtype declaration)
11085 Desig_Act := Designated_Type (Base_Type (Act_T));
11087 -- The designated type may have been introduced through a limited_
11088 -- with clause, in which case retrieve the non-limited view. This
11089 -- applies to incomplete types as well as to class-wide types.
11091 if From_Limited_With (Desig_Act) then
11092 Desig_Act := Available_View (Desig_Act);
11095 if not Subtypes_Match (Desig_Type, Desig_Act) then
11097 ("designated type of actual does not match that of formal &",
11100 if not Predicates_Match (Desig_Type, Desig_Act) then
11101 Error_Msg_N ("\predicates do not match", Actual);
11104 Abandon_Instantiation (Actual);
11106 elsif Is_Access_Type (Designated_Type (Act_T))
11107 and then Is_Constrained (Designated_Type (Designated_Type (Act_T)))
11109 Is_Constrained (Designated_Type (Desig_Type))
11112 ("designated type of actual does not match that of formal &",
11115 if not Predicates_Match (Desig_Type, Desig_Act) then
11116 Error_Msg_N ("\predicates do not match", Actual);
11119 Abandon_Instantiation (Actual);
11122 -- Ada 2005: null-exclusion indicators of the two types must agree
11124 if Can_Never_Be_Null (A_Gen_T) /= Can_Never_Be_Null (Act_T) then
11126 ("non null exclusion of actual and formal & do not match",
11129 end Validate_Access_Type_Instance;
11131 ----------------------------------
11132 -- Validate_Array_Type_Instance --
11133 ----------------------------------
11135 procedure Validate_Array_Type_Instance is
11140 function Formal_Dimensions return Int;
11141 -- Count number of dimensions in array type formal
11143 -----------------------
11144 -- Formal_Dimensions --
11145 -----------------------
11147 function Formal_Dimensions return Int is
11152 if Nkind (Def) = N_Constrained_Array_Definition then
11153 Index := First (Discrete_Subtype_Definitions (Def));
11155 Index := First (Subtype_Marks (Def));
11158 while Present (Index) loop
11160 Next_Index (Index);
11164 end Formal_Dimensions;
11166 -- Start of processing for Validate_Array_Type_Instance
11169 if not Is_Array_Type (Act_T) then
11171 ("expect array type in instantiation of &", Actual, Gen_T);
11172 Abandon_Instantiation (Actual);
11174 elsif Nkind (Def) = N_Constrained_Array_Definition then
11175 if not (Is_Constrained (Act_T)) then
11177 ("expect constrained array in instantiation of &",
11179 Abandon_Instantiation (Actual);
11183 if Is_Constrained (Act_T) then
11185 ("expect unconstrained array in instantiation of &",
11187 Abandon_Instantiation (Actual);
11191 if Formal_Dimensions /= Number_Dimensions (Act_T) then
11193 ("dimensions of actual do not match formal &", Actual, Gen_T);
11194 Abandon_Instantiation (Actual);
11197 I1 := First_Index (A_Gen_T);
11198 I2 := First_Index (Act_T);
11199 for J in 1 .. Formal_Dimensions loop
11201 -- If the indexes of the actual were given by a subtype_mark,
11202 -- the index was transformed into a range attribute. Retrieve
11203 -- the original type mark for checking.
11205 if Is_Entity_Name (Original_Node (I2)) then
11206 T2 := Entity (Original_Node (I2));
11211 if not Subtypes_Match
11212 (Find_Actual_Type (Etype (I1), A_Gen_T), T2)
11215 ("index types of actual do not match those of formal &",
11217 Abandon_Instantiation (Actual);
11224 -- Check matching subtypes. Note that there are complex visibility
11225 -- issues when the generic is a child unit and some aspect of the
11226 -- generic type is declared in a parent unit of the generic. We do
11227 -- the test to handle this special case only after a direct check
11228 -- for static matching has failed. The case where both the component
11229 -- type and the array type are separate formals, and the component
11230 -- type is a private view may also require special checking in
11234 (Component_Type (A_Gen_T), Component_Type (Act_T))
11237 (Find_Actual_Type (Component_Type (A_Gen_T), A_Gen_T),
11238 Component_Type (Act_T))
11243 ("component subtype of actual does not match that of formal &",
11245 Abandon_Instantiation (Actual);
11248 if Has_Aliased_Components (A_Gen_T)
11249 and then not Has_Aliased_Components (Act_T)
11252 ("actual must have aliased components to match formal type &",
11255 end Validate_Array_Type_Instance;
11257 -----------------------------------------------
11258 -- Validate_Derived_Interface_Type_Instance --
11259 -----------------------------------------------
11261 procedure Validate_Derived_Interface_Type_Instance is
11262 Par : constant Entity_Id := Entity (Subtype_Indication (Def));
11266 -- First apply interface instance checks
11268 Validate_Interface_Type_Instance;
11270 -- Verify that immediate parent interface is an ancestor of
11274 and then not Interface_Present_In_Ancestor (Act_T, Par)
11277 ("interface actual must include progenitor&", Actual, Par);
11280 -- Now verify that the actual includes all other ancestors of
11283 Elmt := First_Elmt (Interfaces (A_Gen_T));
11284 while Present (Elmt) loop
11285 if not Interface_Present_In_Ancestor
11286 (Act_T, Get_Instance_Of (Node (Elmt)))
11289 ("interface actual must include progenitor&",
11290 Actual, Node (Elmt));
11295 end Validate_Derived_Interface_Type_Instance;
11297 ------------------------------------
11298 -- Validate_Derived_Type_Instance --
11299 ------------------------------------
11301 procedure Validate_Derived_Type_Instance is
11302 Actual_Discr : Entity_Id;
11303 Ancestor_Discr : Entity_Id;
11306 -- If the parent type in the generic declaration is itself a previous
11307 -- formal type, then it is local to the generic and absent from the
11308 -- analyzed generic definition. In that case the ancestor is the
11309 -- instance of the formal (which must have been instantiated
11310 -- previously), unless the ancestor is itself a formal derived type.
11311 -- In this latter case (which is the subject of Corrigendum 8652/0038
11312 -- (AI-202) the ancestor of the formals is the ancestor of its
11313 -- parent. Otherwise, the analyzed generic carries the parent type.
11314 -- If the parent type is defined in a previous formal package, then
11315 -- the scope of that formal package is that of the generic type
11316 -- itself, and it has already been mapped into the corresponding type
11317 -- in the actual package.
11319 -- Common case: parent type defined outside of the generic
11321 if Is_Entity_Name (Subtype_Mark (Def))
11322 and then Present (Entity (Subtype_Mark (Def)))
11324 Ancestor := Get_Instance_Of (Entity (Subtype_Mark (Def)));
11326 -- Check whether parent is defined in a previous formal package
11329 Scope (Scope (Base_Type (Etype (A_Gen_T)))) = Scope (A_Gen_T)
11332 Get_Instance_Of (Base_Type (Etype (A_Gen_T)));
11334 -- The type may be a local derivation, or a type extension of a
11335 -- previous formal, or of a formal of a parent package.
11337 elsif Is_Derived_Type (Get_Instance_Of (A_Gen_T))
11339 Ekind (Get_Instance_Of (A_Gen_T)) = E_Record_Type_With_Private
11341 -- Check whether the parent is another derived formal type in the
11342 -- same generic unit.
11344 if Etype (A_Gen_T) /= A_Gen_T
11345 and then Is_Generic_Type (Etype (A_Gen_T))
11346 and then Scope (Etype (A_Gen_T)) = Scope (A_Gen_T)
11347 and then Etype (Etype (A_Gen_T)) /= Etype (A_Gen_T)
11349 -- Locate ancestor of parent from the subtype declaration
11350 -- created for the actual.
11356 Decl := First (Actual_Decls);
11357 while Present (Decl) loop
11358 if Nkind (Decl) = N_Subtype_Declaration
11359 and then Chars (Defining_Identifier (Decl)) =
11360 Chars (Etype (A_Gen_T))
11362 Ancestor := Generic_Parent_Type (Decl);
11370 pragma Assert (Present (Ancestor));
11372 -- The ancestor itself may be a previous formal that has been
11375 Ancestor := Get_Instance_Of (Ancestor);
11379 Get_Instance_Of (Base_Type (Get_Instance_Of (A_Gen_T)));
11382 -- An unusual case: the actual is a type declared in a parent unit,
11383 -- but is not a formal type so there is no instance_of for it.
11384 -- Retrieve it by analyzing the record extension.
11386 elsif Is_Child_Unit (Scope (A_Gen_T))
11387 and then In_Open_Scopes (Scope (Act_T))
11388 and then Is_Generic_Instance (Scope (Act_T))
11390 Analyze (Subtype_Mark (Def));
11391 Ancestor := Entity (Subtype_Mark (Def));
11394 Ancestor := Get_Instance_Of (Etype (Base_Type (A_Gen_T)));
11397 -- If the formal derived type has pragma Preelaborable_Initialization
11398 -- then the actual type must have preelaborable initialization.
11400 if Known_To_Have_Preelab_Init (A_Gen_T)
11401 and then not Has_Preelaborable_Initialization (Act_T)
11404 ("actual for & must have preelaborable initialization",
11408 -- Ada 2005 (AI-251)
11410 if Ada_Version >= Ada_2005 and then Is_Interface (Ancestor) then
11411 if not Interface_Present_In_Ancestor (Act_T, Ancestor) then
11413 ("(Ada 2005) expected type implementing & in instantiation",
11417 elsif not Is_Ancestor (Base_Type (Ancestor), Act_T) then
11419 ("expect type derived from & in instantiation",
11420 Actual, First_Subtype (Ancestor));
11421 Abandon_Instantiation (Actual);
11424 -- Ada 2005 (AI-443): Synchronized formal derived type checks. Note
11425 -- that the formal type declaration has been rewritten as a private
11428 if Ada_Version >= Ada_2005
11429 and then Nkind (Parent (A_Gen_T)) = N_Private_Extension_Declaration
11430 and then Synchronized_Present (Parent (A_Gen_T))
11432 -- The actual must be a synchronized tagged type
11434 if not Is_Tagged_Type (Act_T) then
11436 ("actual of synchronized type must be tagged", Actual);
11437 Abandon_Instantiation (Actual);
11439 elsif Nkind (Parent (Act_T)) = N_Full_Type_Declaration
11440 and then Nkind (Type_Definition (Parent (Act_T))) =
11441 N_Derived_Type_Definition
11442 and then not Synchronized_Present
11443 (Type_Definition (Parent (Act_T)))
11446 ("actual of synchronized type must be synchronized", Actual);
11447 Abandon_Instantiation (Actual);
11451 -- Perform atomic/volatile checks (RM C.6(12)). Note that AI05-0218-1
11452 -- removes the second instance of the phrase "or allow pass by copy".
11454 if Is_Atomic (Act_T) and then not Is_Atomic (Ancestor) then
11456 ("cannot have atomic actual type for non-atomic formal type",
11459 elsif Is_Volatile (Act_T) and then not Is_Volatile (Ancestor) then
11461 ("cannot have volatile actual type for non-volatile formal type",
11465 -- It should not be necessary to check for unknown discriminants on
11466 -- Formal, but for some reason Has_Unknown_Discriminants is false for
11467 -- A_Gen_T, so Is_Indefinite_Subtype incorrectly returns False. This
11468 -- needs fixing. ???
11470 if not Is_Indefinite_Subtype (A_Gen_T)
11471 and then not Unknown_Discriminants_Present (Formal)
11472 and then Is_Indefinite_Subtype (Act_T)
11474 Error_Msg_N ("actual subtype must be constrained", Actual);
11475 Abandon_Instantiation (Actual);
11478 if not Unknown_Discriminants_Present (Formal) then
11479 if Is_Constrained (Ancestor) then
11480 if not Is_Constrained (Act_T) then
11481 Error_Msg_N ("actual subtype must be constrained", Actual);
11482 Abandon_Instantiation (Actual);
11485 -- Ancestor is unconstrained, Check if generic formal and actual
11486 -- agree on constrainedness. The check only applies to array types
11487 -- and discriminated types.
11489 elsif Is_Constrained (Act_T) then
11490 if Ekind (Ancestor) = E_Access_Type
11491 or else (not Is_Constrained (A_Gen_T)
11492 and then Is_Composite_Type (A_Gen_T))
11494 Error_Msg_N ("actual subtype must be unconstrained", Actual);
11495 Abandon_Instantiation (Actual);
11498 -- A class-wide type is only allowed if the formal has unknown
11501 elsif Is_Class_Wide_Type (Act_T)
11502 and then not Has_Unknown_Discriminants (Ancestor)
11505 ("actual for & cannot be a class-wide type", Actual, Gen_T);
11506 Abandon_Instantiation (Actual);
11508 -- Otherwise, the formal and actual must have the same number
11509 -- of discriminants and each discriminant of the actual must
11510 -- correspond to a discriminant of the formal.
11512 elsif Has_Discriminants (Act_T)
11513 and then not Has_Unknown_Discriminants (Act_T)
11514 and then Has_Discriminants (Ancestor)
11516 Actual_Discr := First_Discriminant (Act_T);
11517 Ancestor_Discr := First_Discriminant (Ancestor);
11518 while Present (Actual_Discr)
11519 and then Present (Ancestor_Discr)
11521 if Base_Type (Act_T) /= Base_Type (Ancestor) and then
11522 No (Corresponding_Discriminant (Actual_Discr))
11525 ("discriminant & does not correspond "
11526 & "to ancestor discriminant", Actual, Actual_Discr);
11527 Abandon_Instantiation (Actual);
11530 Next_Discriminant (Actual_Discr);
11531 Next_Discriminant (Ancestor_Discr);
11534 if Present (Actual_Discr) or else Present (Ancestor_Discr) then
11536 ("actual for & must have same number of discriminants",
11538 Abandon_Instantiation (Actual);
11541 -- This case should be caught by the earlier check for
11542 -- constrainedness, but the check here is added for completeness.
11544 elsif Has_Discriminants (Act_T)
11545 and then not Has_Unknown_Discriminants (Act_T)
11548 ("actual for & must not have discriminants", Actual, Gen_T);
11549 Abandon_Instantiation (Actual);
11551 elsif Has_Discriminants (Ancestor) then
11553 ("actual for & must have known discriminants", Actual, Gen_T);
11554 Abandon_Instantiation (Actual);
11557 if not Subtypes_Statically_Compatible
11558 (Act_T, Ancestor, Formal_Derived_Matching => True)
11561 ("constraint on actual is incompatible with formal", Actual);
11562 Abandon_Instantiation (Actual);
11566 -- If the formal and actual types are abstract, check that there
11567 -- are no abstract primitives of the actual type that correspond to
11568 -- nonabstract primitives of the formal type (second sentence of
11571 if Is_Abstract_Type (A_Gen_T) and then Is_Abstract_Type (Act_T) then
11572 Check_Abstract_Primitives : declare
11573 Gen_Prims : constant Elist_Id :=
11574 Primitive_Operations (A_Gen_T);
11575 Gen_Elmt : Elmt_Id;
11576 Gen_Subp : Entity_Id;
11577 Anc_Subp : Entity_Id;
11578 Anc_Formal : Entity_Id;
11579 Anc_F_Type : Entity_Id;
11581 Act_Prims : constant Elist_Id := Primitive_Operations (Act_T);
11582 Act_Elmt : Elmt_Id;
11583 Act_Subp : Entity_Id;
11584 Act_Formal : Entity_Id;
11585 Act_F_Type : Entity_Id;
11587 Subprograms_Correspond : Boolean;
11589 function Is_Tagged_Ancestor (T1, T2 : Entity_Id) return Boolean;
11590 -- Returns true if T2 is derived directly or indirectly from
11591 -- T1, including derivations from interfaces. T1 and T2 are
11592 -- required to be specific tagged base types.
11594 ------------------------
11595 -- Is_Tagged_Ancestor --
11596 ------------------------
11598 function Is_Tagged_Ancestor (T1, T2 : Entity_Id) return Boolean
11600 Intfc_Elmt : Elmt_Id;
11603 -- The predicate is satisfied if the types are the same
11608 -- If we've reached the top of the derivation chain then
11609 -- we know that T1 is not an ancestor of T2.
11611 elsif Etype (T2) = T2 then
11614 -- Proceed to check T2's immediate parent
11616 elsif Is_Ancestor (T1, Base_Type (Etype (T2))) then
11619 -- Finally, check to see if T1 is an ancestor of any of T2's
11623 Intfc_Elmt := First_Elmt (Interfaces (T2));
11624 while Present (Intfc_Elmt) loop
11625 if Is_Ancestor (T1, Node (Intfc_Elmt)) then
11629 Next_Elmt (Intfc_Elmt);
11634 end Is_Tagged_Ancestor;
11636 -- Start of processing for Check_Abstract_Primitives
11639 -- Loop over all of the formal derived type's primitives
11641 Gen_Elmt := First_Elmt (Gen_Prims);
11642 while Present (Gen_Elmt) loop
11643 Gen_Subp := Node (Gen_Elmt);
11645 -- If the primitive of the formal is not abstract, then
11646 -- determine whether there is a corresponding primitive of
11647 -- the actual type that's abstract.
11649 if not Is_Abstract_Subprogram (Gen_Subp) then
11650 Act_Elmt := First_Elmt (Act_Prims);
11651 while Present (Act_Elmt) loop
11652 Act_Subp := Node (Act_Elmt);
11654 -- If we find an abstract primitive of the actual,
11655 -- then we need to test whether it corresponds to the
11656 -- subprogram from which the generic formal primitive
11659 if Is_Abstract_Subprogram (Act_Subp) then
11660 Anc_Subp := Alias (Gen_Subp);
11662 -- Test whether we have a corresponding primitive
11663 -- by comparing names, kinds, formal types, and
11666 if Chars (Anc_Subp) = Chars (Act_Subp)
11667 and then Ekind (Anc_Subp) = Ekind (Act_Subp)
11669 Anc_Formal := First_Formal (Anc_Subp);
11670 Act_Formal := First_Formal (Act_Subp);
11671 while Present (Anc_Formal)
11672 and then Present (Act_Formal)
11674 Anc_F_Type := Etype (Anc_Formal);
11675 Act_F_Type := Etype (Act_Formal);
11677 if Ekind (Anc_F_Type) =
11678 E_Anonymous_Access_Type
11680 Anc_F_Type := Designated_Type (Anc_F_Type);
11682 if Ekind (Act_F_Type) =
11683 E_Anonymous_Access_Type
11686 Designated_Type (Act_F_Type);
11692 Ekind (Act_F_Type) = E_Anonymous_Access_Type
11697 Anc_F_Type := Base_Type (Anc_F_Type);
11698 Act_F_Type := Base_Type (Act_F_Type);
11700 -- If the formal is controlling, then the
11701 -- the type of the actual primitive's formal
11702 -- must be derived directly or indirectly
11703 -- from the type of the ancestor primitive's
11706 if Is_Controlling_Formal (Anc_Formal) then
11707 if not Is_Tagged_Ancestor
11708 (Anc_F_Type, Act_F_Type)
11713 -- Otherwise the types of the formals must
11716 elsif Anc_F_Type /= Act_F_Type then
11720 Next_Entity (Anc_Formal);
11721 Next_Entity (Act_Formal);
11724 -- If we traversed through all of the formals
11725 -- then so far the subprograms correspond, so
11726 -- now check that any result types correspond.
11728 if No (Anc_Formal) and then No (Act_Formal) then
11729 Subprograms_Correspond := True;
11731 if Ekind (Act_Subp) = E_Function then
11732 Anc_F_Type := Etype (Anc_Subp);
11733 Act_F_Type := Etype (Act_Subp);
11735 if Ekind (Anc_F_Type) =
11736 E_Anonymous_Access_Type
11739 Designated_Type (Anc_F_Type);
11741 if Ekind (Act_F_Type) =
11742 E_Anonymous_Access_Type
11745 Designated_Type (Act_F_Type);
11747 Subprograms_Correspond := False;
11752 = E_Anonymous_Access_Type
11754 Subprograms_Correspond := False;
11757 Anc_F_Type := Base_Type (Anc_F_Type);
11758 Act_F_Type := Base_Type (Act_F_Type);
11760 -- Now either the result types must be
11761 -- the same or, if the result type is
11762 -- controlling, the result type of the
11763 -- actual primitive must descend from the
11764 -- result type of the ancestor primitive.
11766 if Subprograms_Correspond
11767 and then Anc_F_Type /= Act_F_Type
11769 Has_Controlling_Result (Anc_Subp)
11770 and then not Is_Tagged_Ancestor
11771 (Anc_F_Type, Act_F_Type)
11773 Subprograms_Correspond := False;
11777 -- Found a matching subprogram belonging to
11778 -- formal ancestor type, so actual subprogram
11779 -- corresponds and this violates 3.9.3(9).
11781 if Subprograms_Correspond then
11783 ("abstract subprogram & overrides "
11784 & "nonabstract subprogram of ancestor",
11791 Next_Elmt (Act_Elmt);
11795 Next_Elmt (Gen_Elmt);
11797 end Check_Abstract_Primitives;
11800 -- Verify that limitedness matches. If parent is a limited
11801 -- interface then the generic formal is not unless declared
11802 -- explicitly so. If not declared limited, the actual cannot be
11803 -- limited (see AI05-0087).
11805 -- Even though this AI is a binding interpretation, we enable the
11806 -- check only in Ada 2012 mode, because this improper construct
11807 -- shows up in user code and in existing B-tests.
11809 if Is_Limited_Type (Act_T)
11810 and then not Is_Limited_Type (A_Gen_T)
11811 and then Ada_Version >= Ada_2012
11813 if In_Instance then
11817 ("actual for non-limited & cannot be a limited type",
11819 Explain_Limited_Type (Act_T, Actual);
11820 Abandon_Instantiation (Actual);
11823 end Validate_Derived_Type_Instance;
11825 ----------------------------------------
11826 -- Validate_Discriminated_Formal_Type --
11827 ----------------------------------------
11829 procedure Validate_Discriminated_Formal_Type is
11830 Formal_Discr : Entity_Id;
11831 Actual_Discr : Entity_Id;
11832 Formal_Subt : Entity_Id;
11835 if Has_Discriminants (A_Gen_T) then
11836 if not Has_Discriminants (Act_T) then
11838 ("actual for & must have discriminants", Actual, Gen_T);
11839 Abandon_Instantiation (Actual);
11841 elsif Is_Constrained (Act_T) then
11843 ("actual for & must be unconstrained", Actual, Gen_T);
11844 Abandon_Instantiation (Actual);
11847 Formal_Discr := First_Discriminant (A_Gen_T);
11848 Actual_Discr := First_Discriminant (Act_T);
11849 while Formal_Discr /= Empty loop
11850 if Actual_Discr = Empty then
11852 ("discriminants on actual do not match formal",
11854 Abandon_Instantiation (Actual);
11857 Formal_Subt := Get_Instance_Of (Etype (Formal_Discr));
11859 -- Access discriminants match if designated types do
11861 if Ekind (Base_Type (Formal_Subt)) = E_Anonymous_Access_Type
11862 and then (Ekind (Base_Type (Etype (Actual_Discr)))) =
11863 E_Anonymous_Access_Type
11866 (Designated_Type (Base_Type (Formal_Subt))) =
11867 Designated_Type (Base_Type (Etype (Actual_Discr)))
11871 elsif Base_Type (Formal_Subt) /=
11872 Base_Type (Etype (Actual_Discr))
11875 ("types of actual discriminants must match formal",
11877 Abandon_Instantiation (Actual);
11879 elsif not Subtypes_Statically_Match
11880 (Formal_Subt, Etype (Actual_Discr))
11881 and then Ada_Version >= Ada_95
11884 ("subtypes of actual discriminants must match formal",
11886 Abandon_Instantiation (Actual);
11889 Next_Discriminant (Formal_Discr);
11890 Next_Discriminant (Actual_Discr);
11893 if Actual_Discr /= Empty then
11895 ("discriminants on actual do not match formal",
11897 Abandon_Instantiation (Actual);
11901 end Validate_Discriminated_Formal_Type;
11903 ---------------------------------------
11904 -- Validate_Incomplete_Type_Instance --
11905 ---------------------------------------
11907 procedure Validate_Incomplete_Type_Instance is
11909 if not Is_Tagged_Type (Act_T)
11910 and then Is_Tagged_Type (A_Gen_T)
11913 ("actual for & must be a tagged type", Actual, Gen_T);
11916 Validate_Discriminated_Formal_Type;
11917 end Validate_Incomplete_Type_Instance;
11919 --------------------------------------
11920 -- Validate_Interface_Type_Instance --
11921 --------------------------------------
11923 procedure Validate_Interface_Type_Instance is
11925 if not Is_Interface (Act_T) then
11927 ("actual for formal interface type must be an interface",
11930 elsif Is_Limited_Type (Act_T) /= Is_Limited_Type (A_Gen_T)
11931 or else Is_Task_Interface (A_Gen_T) /= Is_Task_Interface (Act_T)
11932 or else Is_Protected_Interface (A_Gen_T) /=
11933 Is_Protected_Interface (Act_T)
11934 or else Is_Synchronized_Interface (A_Gen_T) /=
11935 Is_Synchronized_Interface (Act_T)
11938 ("actual for interface& does not match (RM 12.5.5(4))",
11941 end Validate_Interface_Type_Instance;
11943 ------------------------------------
11944 -- Validate_Private_Type_Instance --
11945 ------------------------------------
11947 procedure Validate_Private_Type_Instance is
11949 if Is_Limited_Type (Act_T)
11950 and then not Is_Limited_Type (A_Gen_T)
11952 if In_Instance then
11956 ("actual for non-limited & cannot be a limited type", Actual,
11958 Explain_Limited_Type (Act_T, Actual);
11959 Abandon_Instantiation (Actual);
11962 elsif Known_To_Have_Preelab_Init (A_Gen_T)
11963 and then not Has_Preelaborable_Initialization (Act_T)
11966 ("actual for & must have preelaborable initialization", Actual,
11969 elsif Is_Indefinite_Subtype (Act_T)
11970 and then not Is_Indefinite_Subtype (A_Gen_T)
11971 and then Ada_Version >= Ada_95
11974 ("actual for & must be a definite subtype", Actual, Gen_T);
11976 elsif not Is_Tagged_Type (Act_T)
11977 and then Is_Tagged_Type (A_Gen_T)
11980 ("actual for & must be a tagged type", Actual, Gen_T);
11983 Validate_Discriminated_Formal_Type;
11985 end Validate_Private_Type_Instance;
11987 -- Start of processing for Instantiate_Type
11990 if Get_Instance_Of (A_Gen_T) /= A_Gen_T then
11991 Error_Msg_N ("duplicate instantiation of generic type", Actual);
11992 return New_List (Error);
11994 elsif not Is_Entity_Name (Actual)
11995 or else not Is_Type (Entity (Actual))
11998 ("expect valid subtype mark to instantiate &", Actual, Gen_T);
11999 Abandon_Instantiation (Actual);
12002 Act_T := Entity (Actual);
12004 -- Ada 2005 (AI-216): An Unchecked_Union subtype shall only be passed
12005 -- as a generic actual parameter if the corresponding formal type
12006 -- does not have a known_discriminant_part, or is a formal derived
12007 -- type that is an Unchecked_Union type.
12009 if Is_Unchecked_Union (Base_Type (Act_T)) then
12010 if not Has_Discriminants (A_Gen_T)
12011 or else (Is_Derived_Type (A_Gen_T)
12012 and then Is_Unchecked_Union (A_Gen_T))
12016 Error_Msg_N ("unchecked union cannot be the actual for a "
12017 & "discriminated formal type", Act_T);
12022 -- Deal with fixed/floating restrictions
12024 if Is_Floating_Point_Type (Act_T) then
12025 Check_Restriction (No_Floating_Point, Actual);
12026 elsif Is_Fixed_Point_Type (Act_T) then
12027 Check_Restriction (No_Fixed_Point, Actual);
12030 -- Deal with error of using incomplete type as generic actual.
12031 -- This includes limited views of a type, even if the non-limited
12032 -- view may be available.
12034 if Ekind (Act_T) = E_Incomplete_Type
12035 or else (Is_Class_Wide_Type (Act_T)
12036 and then Ekind (Root_Type (Act_T)) = E_Incomplete_Type)
12038 -- If the formal is an incomplete type, the actual can be
12039 -- incomplete as well.
12041 if Ekind (A_Gen_T) = E_Incomplete_Type then
12044 elsif Is_Class_Wide_Type (Act_T)
12045 or else No (Full_View (Act_T))
12047 Error_Msg_N ("premature use of incomplete type", Actual);
12048 Abandon_Instantiation (Actual);
12050 Act_T := Full_View (Act_T);
12051 Set_Entity (Actual, Act_T);
12053 if Has_Private_Component (Act_T) then
12055 ("premature use of type with private component", Actual);
12059 -- Deal with error of premature use of private type as generic actual
12061 elsif Is_Private_Type (Act_T)
12062 and then Is_Private_Type (Base_Type (Act_T))
12063 and then not Is_Generic_Type (Act_T)
12064 and then not Is_Derived_Type (Act_T)
12065 and then No (Full_View (Root_Type (Act_T)))
12067 -- If the formal is an incomplete type, the actual can be
12068 -- private or incomplete as well.
12070 if Ekind (A_Gen_T) = E_Incomplete_Type then
12073 Error_Msg_N ("premature use of private type", Actual);
12076 elsif Has_Private_Component (Act_T) then
12078 ("premature use of type with private component", Actual);
12081 Set_Instance_Of (A_Gen_T, Act_T);
12083 -- If the type is generic, the class-wide type may also be used
12085 if Is_Tagged_Type (A_Gen_T)
12086 and then Is_Tagged_Type (Act_T)
12087 and then not Is_Class_Wide_Type (A_Gen_T)
12089 Set_Instance_Of (Class_Wide_Type (A_Gen_T),
12090 Class_Wide_Type (Act_T));
12093 if not Is_Abstract_Type (A_Gen_T)
12094 and then Is_Abstract_Type (Act_T)
12097 ("actual of non-abstract formal cannot be abstract", Actual);
12100 -- A generic scalar type is a first subtype for which we generate
12101 -- an anonymous base type. Indicate that the instance of this base
12102 -- is the base type of the actual.
12104 if Is_Scalar_Type (A_Gen_T) then
12105 Set_Instance_Of (Etype (A_Gen_T), Etype (Act_T));
12109 if Error_Posted (Act_T) then
12112 case Nkind (Def) is
12113 when N_Formal_Private_Type_Definition =>
12114 Validate_Private_Type_Instance;
12116 when N_Formal_Incomplete_Type_Definition =>
12117 Validate_Incomplete_Type_Instance;
12119 when N_Formal_Derived_Type_Definition =>
12120 Validate_Derived_Type_Instance;
12122 when N_Formal_Discrete_Type_Definition =>
12123 if not Is_Discrete_Type (Act_T) then
12125 ("expect discrete type in instantiation of&",
12127 Abandon_Instantiation (Actual);
12130 Diagnose_Predicated_Actual;
12132 when N_Formal_Signed_Integer_Type_Definition =>
12133 if not Is_Signed_Integer_Type (Act_T) then
12135 ("expect signed integer type in instantiation of&",
12137 Abandon_Instantiation (Actual);
12140 Diagnose_Predicated_Actual;
12142 when N_Formal_Modular_Type_Definition =>
12143 if not Is_Modular_Integer_Type (Act_T) then
12145 ("expect modular type in instantiation of &",
12147 Abandon_Instantiation (Actual);
12150 Diagnose_Predicated_Actual;
12152 when N_Formal_Floating_Point_Definition =>
12153 if not Is_Floating_Point_Type (Act_T) then
12155 ("expect float type in instantiation of &", Actual, Gen_T);
12156 Abandon_Instantiation (Actual);
12159 when N_Formal_Ordinary_Fixed_Point_Definition =>
12160 if not Is_Ordinary_Fixed_Point_Type (Act_T) then
12162 ("expect ordinary fixed point type in instantiation of &",
12164 Abandon_Instantiation (Actual);
12167 when N_Formal_Decimal_Fixed_Point_Definition =>
12168 if not Is_Decimal_Fixed_Point_Type (Act_T) then
12170 ("expect decimal type in instantiation of &",
12172 Abandon_Instantiation (Actual);
12175 when N_Array_Type_Definition =>
12176 Validate_Array_Type_Instance;
12178 when N_Access_To_Object_Definition =>
12179 Validate_Access_Type_Instance;
12181 when N_Access_Function_Definition |
12182 N_Access_Procedure_Definition =>
12183 Validate_Access_Subprogram_Instance;
12185 when N_Record_Definition =>
12186 Validate_Interface_Type_Instance;
12188 when N_Derived_Type_Definition =>
12189 Validate_Derived_Interface_Type_Instance;
12192 raise Program_Error;
12197 Subt := New_Copy (Gen_T);
12199 -- Use adjusted sloc of subtype name as the location for other nodes in
12200 -- the subtype declaration.
12202 Loc := Sloc (Subt);
12205 Make_Subtype_Declaration (Loc,
12206 Defining_Identifier => Subt,
12207 Subtype_Indication => New_Occurrence_Of (Act_T, Loc));
12209 if Is_Private_Type (Act_T) then
12210 Set_Has_Private_View (Subtype_Indication (Decl_Node));
12212 elsif Is_Access_Type (Act_T)
12213 and then Is_Private_Type (Designated_Type (Act_T))
12215 Set_Has_Private_View (Subtype_Indication (Decl_Node));
12218 Decl_Nodes := New_List (Decl_Node);
12220 -- Flag actual derived types so their elaboration produces the
12221 -- appropriate renamings for the primitive operations of the ancestor.
12222 -- Flag actual for formal private types as well, to determine whether
12223 -- operations in the private part may override inherited operations.
12224 -- If the formal has an interface list, the ancestor is not the
12225 -- parent, but the analyzed formal that includes the interface
12226 -- operations of all its progenitors.
12228 -- Same treatment for formal private types, so we can check whether the
12229 -- type is tagged limited when validating derivations in the private
12230 -- part. (See AI05-096).
12232 if Nkind (Def) = N_Formal_Derived_Type_Definition then
12233 if Present (Interface_List (Def)) then
12234 Set_Generic_Parent_Type (Decl_Node, A_Gen_T);
12236 Set_Generic_Parent_Type (Decl_Node, Ancestor);
12239 elsif Nkind_In (Def, N_Formal_Private_Type_Definition,
12240 N_Formal_Incomplete_Type_Definition)
12242 Set_Generic_Parent_Type (Decl_Node, A_Gen_T);
12245 -- If the actual is a synchronized type that implements an interface,
12246 -- the primitive operations are attached to the corresponding record,
12247 -- and we have to treat it as an additional generic actual, so that its
12248 -- primitive operations become visible in the instance. The task or
12249 -- protected type itself does not carry primitive operations.
12251 if Is_Concurrent_Type (Act_T)
12252 and then Is_Tagged_Type (Act_T)
12253 and then Present (Corresponding_Record_Type (Act_T))
12254 and then Present (Ancestor)
12255 and then Is_Interface (Ancestor)
12258 Corr_Rec : constant Entity_Id :=
12259 Corresponding_Record_Type (Act_T);
12260 New_Corr : Entity_Id;
12261 Corr_Decl : Node_Id;
12264 New_Corr := Make_Temporary (Loc, 'S');
12266 Make_Subtype_Declaration (Loc,
12267 Defining_Identifier => New_Corr,
12268 Subtype_Indication =>
12269 New_Occurrence_Of (Corr_Rec, Loc));
12270 Append_To (Decl_Nodes, Corr_Decl);
12272 if Ekind (Act_T) = E_Task_Type then
12273 Set_Ekind (Subt, E_Task_Subtype);
12275 Set_Ekind (Subt, E_Protected_Subtype);
12278 Set_Corresponding_Record_Type (Subt, Corr_Rec);
12279 Set_Generic_Parent_Type (Corr_Decl, Ancestor);
12280 Set_Generic_Parent_Type (Decl_Node, Empty);
12285 end Instantiate_Type;
12287 ---------------------
12288 -- Is_In_Main_Unit --
12289 ---------------------
12291 function Is_In_Main_Unit (N : Node_Id) return Boolean is
12292 Unum : constant Unit_Number_Type := Get_Source_Unit (N);
12293 Current_Unit : Node_Id;
12296 if Unum = Main_Unit then
12299 -- If the current unit is a subunit then it is either the main unit or
12300 -- is being compiled as part of the main unit.
12302 elsif Nkind (N) = N_Compilation_Unit then
12303 return Nkind (Unit (N)) = N_Subunit;
12306 Current_Unit := Parent (N);
12307 while Present (Current_Unit)
12308 and then Nkind (Current_Unit) /= N_Compilation_Unit
12310 Current_Unit := Parent (Current_Unit);
12313 -- The instantiation node is in the main unit, or else the current node
12314 -- (perhaps as the result of nested instantiations) is in the main unit,
12315 -- or in the declaration of the main unit, which in this last case must
12318 return Unum = Main_Unit
12319 or else Current_Unit = Cunit (Main_Unit)
12320 or else Current_Unit = Library_Unit (Cunit (Main_Unit))
12321 or else (Present (Library_Unit (Current_Unit))
12322 and then Is_In_Main_Unit (Library_Unit (Current_Unit)));
12323 end Is_In_Main_Unit;
12325 ----------------------------
12326 -- Load_Parent_Of_Generic --
12327 ----------------------------
12329 procedure Load_Parent_Of_Generic
12332 Body_Optional : Boolean := False)
12334 Comp_Unit : constant Node_Id := Cunit (Get_Source_Unit (Spec));
12335 Saved_Style_Check : constant Boolean := Style_Check;
12336 Saved_Warnings : constant Warning_Record := Save_Warnings;
12337 True_Parent : Node_Id;
12338 Inst_Node : Node_Id;
12340 Previous_Instances : constant Elist_Id := New_Elmt_List;
12342 procedure Collect_Previous_Instances (Decls : List_Id);
12343 -- Collect all instantiations in the given list of declarations, that
12344 -- precede the generic that we need to load. If the bodies of these
12345 -- instantiations are available, we must analyze them, to ensure that
12346 -- the public symbols generated are the same when the unit is compiled
12347 -- to generate code, and when it is compiled in the context of a unit
12348 -- that needs a particular nested instance. This process is applied to
12349 -- both package and subprogram instances.
12351 --------------------------------
12352 -- Collect_Previous_Instances --
12353 --------------------------------
12355 procedure Collect_Previous_Instances (Decls : List_Id) is
12359 Decl := First (Decls);
12360 while Present (Decl) loop
12361 if Sloc (Decl) >= Sloc (Inst_Node) then
12364 -- If Decl is an instantiation, then record it as requiring
12365 -- instantiation of the corresponding body, except if it is an
12366 -- abbreviated instantiation generated internally for conformance
12367 -- checking purposes only for the case of a formal package
12368 -- declared without a box (see Instantiate_Formal_Package). Such
12369 -- an instantiation does not generate any code (the actual code
12370 -- comes from actual) and thus does not need to be analyzed here.
12371 -- If the instantiation appears with a generic package body it is
12372 -- not analyzed here either.
12374 elsif Nkind (Decl) = N_Package_Instantiation
12375 and then not Is_Internal (Defining_Entity (Decl))
12377 Append_Elmt (Decl, Previous_Instances);
12379 -- For a subprogram instantiation, omit instantiations intrinsic
12380 -- operations (Unchecked_Conversions, etc.) that have no bodies.
12382 elsif Nkind_In (Decl, N_Function_Instantiation,
12383 N_Procedure_Instantiation)
12384 and then not Is_Intrinsic_Subprogram (Entity (Name (Decl)))
12386 Append_Elmt (Decl, Previous_Instances);
12388 elsif Nkind (Decl) = N_Package_Declaration then
12389 Collect_Previous_Instances
12390 (Visible_Declarations (Specification (Decl)));
12391 Collect_Previous_Instances
12392 (Private_Declarations (Specification (Decl)));
12394 -- Previous non-generic bodies may contain instances as well
12396 elsif Nkind (Decl) = N_Package_Body
12397 and then Ekind (Corresponding_Spec (Decl)) /= E_Generic_Package
12399 Collect_Previous_Instances (Declarations (Decl));
12401 elsif Nkind (Decl) = N_Subprogram_Body
12402 and then not Acts_As_Spec (Decl)
12403 and then not Is_Generic_Subprogram (Corresponding_Spec (Decl))
12405 Collect_Previous_Instances (Declarations (Decl));
12410 end Collect_Previous_Instances;
12412 -- Start of processing for Load_Parent_Of_Generic
12415 if not In_Same_Source_Unit (N, Spec)
12416 or else Nkind (Unit (Comp_Unit)) = N_Package_Declaration
12417 or else (Nkind (Unit (Comp_Unit)) = N_Package_Body
12418 and then not Is_In_Main_Unit (Spec))
12420 -- Find body of parent of spec, and analyze it. A special case arises
12421 -- when the parent is an instantiation, that is to say when we are
12422 -- currently instantiating a nested generic. In that case, there is
12423 -- no separate file for the body of the enclosing instance. Instead,
12424 -- the enclosing body must be instantiated as if it were a pending
12425 -- instantiation, in order to produce the body for the nested generic
12426 -- we require now. Note that in that case the generic may be defined
12427 -- in a package body, the instance defined in the same package body,
12428 -- and the original enclosing body may not be in the main unit.
12430 Inst_Node := Empty;
12432 True_Parent := Parent (Spec);
12433 while Present (True_Parent)
12434 and then Nkind (True_Parent) /= N_Compilation_Unit
12436 if Nkind (True_Parent) = N_Package_Declaration
12438 Nkind (Original_Node (True_Parent)) = N_Package_Instantiation
12440 -- Parent is a compilation unit that is an instantiation.
12441 -- Instantiation node has been replaced with package decl.
12443 Inst_Node := Original_Node (True_Parent);
12446 elsif Nkind (True_Parent) = N_Package_Declaration
12447 and then Present (Generic_Parent (Specification (True_Parent)))
12448 and then Nkind (Parent (True_Parent)) /= N_Compilation_Unit
12450 -- Parent is an instantiation within another specification.
12451 -- Declaration for instance has been inserted before original
12452 -- instantiation node. A direct link would be preferable?
12454 Inst_Node := Next (True_Parent);
12455 while Present (Inst_Node)
12456 and then Nkind (Inst_Node) /= N_Package_Instantiation
12461 -- If the instance appears within a generic, and the generic
12462 -- unit is defined within a formal package of the enclosing
12463 -- generic, there is no generic body available, and none
12464 -- needed. A more precise test should be used ???
12466 if No (Inst_Node) then
12473 True_Parent := Parent (True_Parent);
12477 -- Case where we are currently instantiating a nested generic
12479 if Present (Inst_Node) then
12480 if Nkind (Parent (True_Parent)) = N_Compilation_Unit then
12482 -- Instantiation node and declaration of instantiated package
12483 -- were exchanged when only the declaration was needed.
12484 -- Restore instantiation node before proceeding with body.
12486 Set_Unit (Parent (True_Parent), Inst_Node);
12489 -- Now complete instantiation of enclosing body, if it appears in
12490 -- some other unit. If it appears in the current unit, the body
12491 -- will have been instantiated already.
12493 if No (Corresponding_Body (Instance_Spec (Inst_Node))) then
12495 -- We need to determine the expander mode to instantiate the
12496 -- enclosing body. Because the generic body we need may use
12497 -- global entities declared in the enclosing package (including
12498 -- aggregates) it is in general necessary to compile this body
12499 -- with expansion enabled, except if we are within a generic
12500 -- package, in which case the usual generic rule applies.
12503 Exp_Status : Boolean := True;
12507 -- Loop through scopes looking for generic package
12509 Scop := Scope (Defining_Entity (Instance_Spec (Inst_Node)));
12510 while Present (Scop)
12511 and then Scop /= Standard_Standard
12513 if Ekind (Scop) = E_Generic_Package then
12514 Exp_Status := False;
12518 Scop := Scope (Scop);
12521 -- Collect previous instantiations in the unit that contains
12522 -- the desired generic.
12524 if Nkind (Parent (True_Parent)) /= N_Compilation_Unit
12525 and then not Body_Optional
12529 Info : Pending_Body_Info;
12533 Par := Parent (Inst_Node);
12534 while Present (Par) loop
12535 exit when Nkind (Parent (Par)) = N_Compilation_Unit;
12536 Par := Parent (Par);
12539 pragma Assert (Present (Par));
12541 if Nkind (Par) = N_Package_Body then
12542 Collect_Previous_Instances (Declarations (Par));
12544 elsif Nkind (Par) = N_Package_Declaration then
12545 Collect_Previous_Instances
12546 (Visible_Declarations (Specification (Par)));
12547 Collect_Previous_Instances
12548 (Private_Declarations (Specification (Par)));
12551 -- Enclosing unit is a subprogram body. In this
12552 -- case all instance bodies are processed in order
12553 -- and there is no need to collect them separately.
12558 Decl := First_Elmt (Previous_Instances);
12559 while Present (Decl) loop
12561 (Inst_Node => Node (Decl),
12563 Instance_Spec (Node (Decl)),
12564 Expander_Status => Exp_Status,
12565 Current_Sem_Unit =>
12566 Get_Code_Unit (Sloc (Node (Decl))),
12567 Scope_Suppress => Scope_Suppress,
12568 Local_Suppress_Stack_Top =>
12569 Local_Suppress_Stack_Top,
12570 Version => Ada_Version,
12571 Version_Pragma => Ada_Version_Pragma,
12572 Warnings => Save_Warnings,
12573 SPARK_Mode => SPARK_Mode,
12574 SPARK_Mode_Pragma => SPARK_Mode_Pragma);
12576 -- Package instance
12579 Nkind (Node (Decl)) = N_Package_Instantiation
12581 Instantiate_Package_Body
12582 (Info, Body_Optional => True);
12584 -- Subprogram instance
12587 -- The instance_spec is the wrapper package,
12588 -- and the subprogram declaration is the last
12589 -- declaration in the wrapper.
12593 (Visible_Declarations
12594 (Specification (Info.Act_Decl)));
12596 Instantiate_Subprogram_Body
12597 (Info, Body_Optional => True);
12605 Instantiate_Package_Body
12607 ((Inst_Node => Inst_Node,
12608 Act_Decl => True_Parent,
12609 Expander_Status => Exp_Status,
12610 Current_Sem_Unit => Get_Code_Unit
12611 (Sloc (Inst_Node)),
12612 Scope_Suppress => Scope_Suppress,
12613 Local_Suppress_Stack_Top => Local_Suppress_Stack_Top,
12614 Version => Ada_Version,
12615 Version_Pragma => Ada_Version_Pragma,
12616 Warnings => Save_Warnings,
12617 SPARK_Mode => SPARK_Mode,
12618 SPARK_Mode_Pragma => SPARK_Mode_Pragma)),
12619 Body_Optional => Body_Optional);
12623 -- Case where we are not instantiating a nested generic
12626 Opt.Style_Check := False;
12627 Expander_Mode_Save_And_Set (True);
12628 Load_Needed_Body (Comp_Unit, OK);
12629 Opt.Style_Check := Saved_Style_Check;
12630 Restore_Warnings (Saved_Warnings);
12631 Expander_Mode_Restore;
12634 and then Unit_Requires_Body (Defining_Entity (Spec))
12635 and then not Body_Optional
12638 Bname : constant Unit_Name_Type :=
12639 Get_Body_Name (Get_Unit_Name (Unit (Comp_Unit)));
12642 -- In CodePeer mode, the missing body may make the analysis
12643 -- incomplete, but we do not treat it as fatal.
12645 if CodePeer_Mode then
12649 Error_Msg_Unit_1 := Bname;
12650 Error_Msg_N ("this instantiation requires$!", N);
12651 Error_Msg_File_1 :=
12652 Get_File_Name (Bname, Subunit => False);
12653 Error_Msg_N ("\but file{ was not found!", N);
12654 raise Unrecoverable_Error;
12661 -- If loading parent of the generic caused an instantiation circularity,
12662 -- we abandon compilation at this point, because otherwise in some cases
12663 -- we get into trouble with infinite recursions after this point.
12665 if Circularity_Detected then
12666 raise Unrecoverable_Error;
12668 end Load_Parent_Of_Generic;
12670 ---------------------------------
12671 -- Map_Formal_Package_Entities --
12672 ---------------------------------
12674 procedure Map_Formal_Package_Entities (Form : Entity_Id; Act : Entity_Id) is
12679 Set_Instance_Of (Form, Act);
12681 -- Traverse formal and actual package to map the corresponding entities.
12682 -- We skip over internal entities that may be generated during semantic
12683 -- analysis, and find the matching entities by name, given that they
12684 -- must appear in the same order.
12686 E1 := First_Entity (Form);
12687 E2 := First_Entity (Act);
12688 while Present (E1) and then E1 /= First_Private_Entity (Form) loop
12689 -- Could this test be a single condition??? Seems like it could, and
12690 -- isn't FPE (Form) a constant anyway???
12692 if not Is_Internal (E1)
12693 and then Present (Parent (E1))
12694 and then not Is_Class_Wide_Type (E1)
12695 and then not Is_Internal_Name (Chars (E1))
12697 while Present (E2) and then Chars (E2) /= Chars (E1) loop
12704 Set_Instance_Of (E1, E2);
12706 if Is_Type (E1) and then Is_Tagged_Type (E2) then
12707 Set_Instance_Of (Class_Wide_Type (E1), Class_Wide_Type (E2));
12710 if Is_Constrained (E1) then
12711 Set_Instance_Of (Base_Type (E1), Base_Type (E2));
12714 if Ekind (E1) = E_Package and then No (Renamed_Object (E1)) then
12715 Map_Formal_Package_Entities (E1, E2);
12722 end Map_Formal_Package_Entities;
12724 -----------------------
12725 -- Move_Freeze_Nodes --
12726 -----------------------
12728 procedure Move_Freeze_Nodes
12729 (Out_Of : Entity_Id;
12734 Next_Decl : Node_Id;
12735 Next_Node : Node_Id := After;
12738 function Is_Outer_Type (T : Entity_Id) return Boolean;
12739 -- Check whether entity is declared in a scope external to that of the
12742 -------------------
12743 -- Is_Outer_Type --
12744 -------------------
12746 function Is_Outer_Type (T : Entity_Id) return Boolean is
12747 Scop : Entity_Id := Scope (T);
12750 if Scope_Depth (Scop) < Scope_Depth (Out_Of) then
12754 while Scop /= Standard_Standard loop
12755 if Scop = Out_Of then
12758 Scop := Scope (Scop);
12766 -- Start of processing for Move_Freeze_Nodes
12773 -- First remove the freeze nodes that may appear before all other
12777 while Present (Decl)
12778 and then Nkind (Decl) = N_Freeze_Entity
12779 and then Is_Outer_Type (Entity (Decl))
12781 Decl := Remove_Head (L);
12782 Insert_After (Next_Node, Decl);
12783 Set_Analyzed (Decl, False);
12788 -- Next scan the list of declarations and remove each freeze node that
12789 -- appears ahead of the current node.
12791 while Present (Decl) loop
12792 while Present (Next (Decl))
12793 and then Nkind (Next (Decl)) = N_Freeze_Entity
12794 and then Is_Outer_Type (Entity (Next (Decl)))
12796 Next_Decl := Remove_Next (Decl);
12797 Insert_After (Next_Node, Next_Decl);
12798 Set_Analyzed (Next_Decl, False);
12799 Next_Node := Next_Decl;
12802 -- If the declaration is a nested package or concurrent type, then
12803 -- recurse. Nested generic packages will have been processed from the
12806 case Nkind (Decl) is
12807 when N_Package_Declaration =>
12808 Spec := Specification (Decl);
12810 when N_Task_Type_Declaration =>
12811 Spec := Task_Definition (Decl);
12813 when N_Protected_Type_Declaration =>
12814 Spec := Protected_Definition (Decl);
12820 if Present (Spec) then
12821 Move_Freeze_Nodes (Out_Of, Next_Node, Visible_Declarations (Spec));
12822 Move_Freeze_Nodes (Out_Of, Next_Node, Private_Declarations (Spec));
12827 end Move_Freeze_Nodes;
12833 function Next_Assoc (E : Assoc_Ptr) return Assoc_Ptr is
12835 return Generic_Renamings.Table (E).Next_In_HTable;
12838 ------------------------
12839 -- Preanalyze_Actuals --
12840 ------------------------
12842 procedure Preanalyze_Actuals (N : Node_Id) is
12845 Errs : constant Int := Serious_Errors_Detected;
12847 Cur : Entity_Id := Empty;
12848 -- Current homograph of the instance name
12851 -- Saved visibility status of the current homograph
12854 Assoc := First (Generic_Associations (N));
12856 -- If the instance is a child unit, its name may hide an outer homonym,
12857 -- so make it invisible to perform name resolution on the actuals.
12859 if Nkind (Defining_Unit_Name (N)) = N_Defining_Program_Unit_Name
12861 (Current_Entity (Defining_Identifier (Defining_Unit_Name (N))))
12863 Cur := Current_Entity (Defining_Identifier (Defining_Unit_Name (N)));
12865 if Is_Compilation_Unit (Cur) then
12866 Vis := Is_Immediately_Visible (Cur);
12867 Set_Is_Immediately_Visible (Cur, False);
12873 while Present (Assoc) loop
12874 if Nkind (Assoc) /= N_Others_Choice then
12875 Act := Explicit_Generic_Actual_Parameter (Assoc);
12877 -- Within a nested instantiation, a defaulted actual is an empty
12878 -- association, so nothing to analyze. If the subprogram actual
12879 -- is an attribute, analyze prefix only, because actual is not a
12880 -- complete attribute reference.
12882 -- If actual is an allocator, analyze expression only. The full
12883 -- analysis can generate code, and if instance is a compilation
12884 -- unit we have to wait until the package instance is installed
12885 -- to have a proper place to insert this code.
12887 -- String literals may be operators, but at this point we do not
12888 -- know whether the actual is a formal subprogram or a string.
12893 elsif Nkind (Act) = N_Attribute_Reference then
12894 Analyze (Prefix (Act));
12896 elsif Nkind (Act) = N_Explicit_Dereference then
12897 Analyze (Prefix (Act));
12899 elsif Nkind (Act) = N_Allocator then
12901 Expr : constant Node_Id := Expression (Act);
12904 if Nkind (Expr) = N_Subtype_Indication then
12905 Analyze (Subtype_Mark (Expr));
12907 -- Analyze separately each discriminant constraint, when
12908 -- given with a named association.
12914 Constr := First (Constraints (Constraint (Expr)));
12915 while Present (Constr) loop
12916 if Nkind (Constr) = N_Discriminant_Association then
12917 Analyze (Expression (Constr));
12931 elsif Nkind (Act) /= N_Operator_Symbol then
12935 if Errs /= Serious_Errors_Detected then
12937 -- Do a minimal analysis of the generic, to prevent spurious
12938 -- warnings complaining about the generic being unreferenced,
12939 -- before abandoning the instantiation.
12941 Analyze (Name (N));
12943 if Is_Entity_Name (Name (N))
12944 and then Etype (Name (N)) /= Any_Type
12946 Generate_Reference (Entity (Name (N)), Name (N));
12947 Set_Is_Instantiated (Entity (Name (N)));
12950 if Present (Cur) then
12952 -- For the case of a child instance hiding an outer homonym,
12953 -- provide additional warning which might explain the error.
12955 Set_Is_Immediately_Visible (Cur, Vis);
12957 ("& hides outer unit with the same name??",
12958 N, Defining_Unit_Name (N));
12961 Abandon_Instantiation (Act);
12968 if Present (Cur) then
12969 Set_Is_Immediately_Visible (Cur, Vis);
12971 end Preanalyze_Actuals;
12973 -------------------
12974 -- Remove_Parent --
12975 -------------------
12977 procedure Remove_Parent (In_Body : Boolean := False) is
12978 S : Entity_Id := Current_Scope;
12979 -- S is the scope containing the instantiation just completed. The scope
12980 -- stack contains the parent instances of the instantiation, followed by
12989 -- After child instantiation is complete, remove from scope stack the
12990 -- extra copy of the current scope, and then remove parent instances.
12992 if not In_Body then
12995 while Current_Scope /= S loop
12996 P := Current_Scope;
12997 End_Package_Scope (Current_Scope);
12999 if In_Open_Scopes (P) then
13000 E := First_Entity (P);
13001 while Present (E) loop
13002 Set_Is_Immediately_Visible (E, True);
13006 -- If instantiation is declared in a block, it is the enclosing
13007 -- scope that might be a parent instance. Note that only one
13008 -- block can be involved, because the parent instances have
13009 -- been installed within it.
13011 if Ekind (P) = E_Block then
13012 Cur_P := Scope (P);
13017 if Is_Generic_Instance (Cur_P) and then P /= Current_Scope then
13018 -- We are within an instance of some sibling. Retain
13019 -- visibility of parent, for proper subsequent cleanup, and
13020 -- reinstall private declarations as well.
13022 Set_In_Private_Part (P);
13023 Install_Private_Declarations (P);
13026 -- If the ultimate parent is a top-level unit recorded in
13027 -- Instance_Parent_Unit, then reset its visibility to what it was
13028 -- before instantiation. (It's not clear what the purpose is of
13029 -- testing whether Scope (P) is In_Open_Scopes, but that test was
13030 -- present before the ultimate parent test was added.???)
13032 elsif not In_Open_Scopes (Scope (P))
13033 or else (P = Instance_Parent_Unit
13034 and then not Parent_Unit_Visible)
13036 Set_Is_Immediately_Visible (P, False);
13038 -- If the current scope is itself an instantiation of a generic
13039 -- nested within P, and we are in the private part of body of this
13040 -- instantiation, restore the full views of P, that were removed
13041 -- in End_Package_Scope above. This obscure case can occur when a
13042 -- subunit of a generic contains an instance of a child unit of
13043 -- its generic parent unit.
13045 elsif S = Current_Scope and then Is_Generic_Instance (S) then
13047 Par : constant Entity_Id :=
13048 Generic_Parent (Package_Specification (S));
13051 and then P = Scope (Par)
13052 and then (In_Package_Body (S) or else In_Private_Part (S))
13054 Set_In_Private_Part (P);
13055 Install_Private_Declarations (P);
13061 -- Reset visibility of entities in the enclosing scope
13063 Set_Is_Hidden_Open_Scope (Current_Scope, False);
13065 Hidden := First_Elmt (Hidden_Entities);
13066 while Present (Hidden) loop
13067 Set_Is_Immediately_Visible (Node (Hidden), True);
13068 Next_Elmt (Hidden);
13072 -- Each body is analyzed separately, and there is no context that
13073 -- needs preserving from one body instance to the next, so remove all
13074 -- parent scopes that have been installed.
13076 while Present (S) loop
13077 End_Package_Scope (S);
13078 Set_Is_Immediately_Visible (S, False);
13079 S := Current_Scope;
13080 exit when S = Standard_Standard;
13089 procedure Restore_Env is
13090 Saved : Instance_Env renames Instance_Envs.Table (Instance_Envs.Last);
13093 if No (Current_Instantiated_Parent.Act_Id) then
13094 -- Restore environment after subprogram inlining
13096 Restore_Private_Views (Empty);
13099 Current_Instantiated_Parent := Saved.Instantiated_Parent;
13100 Exchanged_Views := Saved.Exchanged_Views;
13101 Hidden_Entities := Saved.Hidden_Entities;
13102 Current_Sem_Unit := Saved.Current_Sem_Unit;
13103 Parent_Unit_Visible := Saved.Parent_Unit_Visible;
13104 Instance_Parent_Unit := Saved.Instance_Parent_Unit;
13106 Restore_Opt_Config_Switches (Saved.Switches);
13108 Instance_Envs.Decrement_Last;
13111 ---------------------------
13112 -- Restore_Private_Views --
13113 ---------------------------
13115 procedure Restore_Private_Views
13116 (Pack_Id : Entity_Id;
13117 Is_Package : Boolean := True)
13122 Dep_Elmt : Elmt_Id;
13125 procedure Restore_Nested_Formal (Formal : Entity_Id);
13126 -- Hide the generic formals of formal packages declared with box which
13127 -- were reachable in the current instantiation.
13129 ---------------------------
13130 -- Restore_Nested_Formal --
13131 ---------------------------
13133 procedure Restore_Nested_Formal (Formal : Entity_Id) is
13137 if Present (Renamed_Object (Formal))
13138 and then Denotes_Formal_Package (Renamed_Object (Formal), True)
13142 elsif Present (Associated_Formal_Package (Formal)) then
13143 Ent := First_Entity (Formal);
13144 while Present (Ent) loop
13145 exit when Ekind (Ent) = E_Package
13146 and then Renamed_Entity (Ent) = Renamed_Entity (Formal);
13148 Set_Is_Hidden (Ent);
13149 Set_Is_Potentially_Use_Visible (Ent, False);
13151 -- If package, then recurse
13153 if Ekind (Ent) = E_Package then
13154 Restore_Nested_Formal (Ent);
13160 end Restore_Nested_Formal;
13162 -- Start of processing for Restore_Private_Views
13165 M := First_Elmt (Exchanged_Views);
13166 while Present (M) loop
13169 -- Subtypes of types whose views have been exchanged, and that are
13170 -- defined within the instance, were not on the Private_Dependents
13171 -- list on entry to the instance, so they have to be exchanged
13172 -- explicitly now, in order to remain consistent with the view of the
13175 if Ekind_In (Typ, E_Private_Type,
13176 E_Limited_Private_Type,
13177 E_Record_Type_With_Private)
13179 Dep_Elmt := First_Elmt (Private_Dependents (Typ));
13180 while Present (Dep_Elmt) loop
13181 Dep_Typ := Node (Dep_Elmt);
13183 if Scope (Dep_Typ) = Pack_Id
13184 and then Present (Full_View (Dep_Typ))
13186 Replace_Elmt (Dep_Elmt, Full_View (Dep_Typ));
13187 Exchange_Declarations (Dep_Typ);
13190 Next_Elmt (Dep_Elmt);
13194 Exchange_Declarations (Node (M));
13198 if No (Pack_Id) then
13202 -- Make the generic formal parameters private, and make the formal types
13203 -- into subtypes of the actuals again.
13205 E := First_Entity (Pack_Id);
13206 while Present (E) loop
13207 Set_Is_Hidden (E, True);
13210 and then Nkind (Parent (E)) = N_Subtype_Declaration
13212 -- If the actual for E is itself a generic actual type from
13213 -- an enclosing instance, E is still a generic actual type
13214 -- outside of the current instance. This matter when resolving
13215 -- an overloaded call that may be ambiguous in the enclosing
13216 -- instance, when two of its actuals coincide.
13218 if Is_Entity_Name (Subtype_Indication (Parent (E)))
13219 and then Is_Generic_Actual_Type
13220 (Entity (Subtype_Indication (Parent (E))))
13224 Set_Is_Generic_Actual_Type (E, False);
13227 -- An unusual case of aliasing: the actual may also be directly
13228 -- visible in the generic, and be private there, while it is fully
13229 -- visible in the context of the instance. The internal subtype
13230 -- is private in the instance but has full visibility like its
13231 -- parent in the enclosing scope. This enforces the invariant that
13232 -- the privacy status of all private dependents of a type coincide
13233 -- with that of the parent type. This can only happen when a
13234 -- generic child unit is instantiated within a sibling.
13236 if Is_Private_Type (E)
13237 and then not Is_Private_Type (Etype (E))
13239 Exchange_Declarations (E);
13242 elsif Ekind (E) = E_Package then
13244 -- The end of the renaming list is the renaming of the generic
13245 -- package itself. If the instance is a subprogram, all entities
13246 -- in the corresponding package are renamings. If this entity is
13247 -- a formal package, make its own formals private as well. The
13248 -- actual in this case is itself the renaming of an instantiation.
13249 -- If the entity is not a package renaming, it is the entity
13250 -- created to validate formal package actuals: ignore it.
13252 -- If the actual is itself a formal package for the enclosing
13253 -- generic, or the actual for such a formal package, it remains
13254 -- visible on exit from the instance, and therefore nothing needs
13255 -- to be done either, except to keep it accessible.
13257 if Is_Package and then Renamed_Object (E) = Pack_Id then
13260 elsif Nkind (Parent (E)) /= N_Package_Renaming_Declaration then
13264 Denotes_Formal_Package (Renamed_Object (E), True, Pack_Id)
13266 Set_Is_Hidden (E, False);
13270 Act_P : constant Entity_Id := Renamed_Object (E);
13274 Id := First_Entity (Act_P);
13276 and then Id /= First_Private_Entity (Act_P)
13278 exit when Ekind (Id) = E_Package
13279 and then Renamed_Object (Id) = Act_P;
13281 Set_Is_Hidden (Id, True);
13282 Set_Is_Potentially_Use_Visible (Id, In_Use (Act_P));
13284 if Ekind (Id) = E_Package then
13285 Restore_Nested_Formal (Id);
13296 end Restore_Private_Views;
13303 (Gen_Unit : Entity_Id;
13304 Act_Unit : Entity_Id)
13308 Set_Instance_Env (Gen_Unit, Act_Unit);
13311 ----------------------------
13312 -- Save_Global_References --
13313 ----------------------------
13315 procedure Save_Global_References (N : Node_Id) is
13316 Gen_Scope : Entity_Id;
13320 function Is_Global (E : Entity_Id) return Boolean;
13321 -- Check whether entity is defined outside of generic unit. Examine the
13322 -- scope of an entity, and the scope of the scope, etc, until we find
13323 -- either Standard, in which case the entity is global, or the generic
13324 -- unit itself, which indicates that the entity is local. If the entity
13325 -- is the generic unit itself, as in the case of a recursive call, or
13326 -- the enclosing generic unit, if different from the current scope, then
13327 -- it is local as well, because it will be replaced at the point of
13328 -- instantiation. On the other hand, if it is a reference to a child
13329 -- unit of a common ancestor, which appears in an instantiation, it is
13330 -- global because it is used to denote a specific compilation unit at
13331 -- the time the instantiations will be analyzed.
13333 procedure Reset_Entity (N : Node_Id);
13334 -- Save semantic information on global entity so that it is not resolved
13335 -- again at instantiation time.
13337 procedure Save_Entity_Descendants (N : Node_Id);
13338 -- Apply Save_Global_References to the two syntactic descendants of
13339 -- non-terminal nodes that carry an Associated_Node and are processed
13340 -- through Reset_Entity. Once the global entity (if any) has been
13341 -- captured together with its type, only two syntactic descendants need
13342 -- to be traversed to complete the processing of the tree rooted at N.
13343 -- This applies to Selected_Components, Expanded_Names, and to Operator
13344 -- nodes. N can also be a character literal, identifier, or operator
13345 -- symbol node, but the call has no effect in these cases.
13347 procedure Save_Global_Defaults (N1, N2 : Node_Id);
13348 -- Default actuals in nested instances must be handled specially
13349 -- because there is no link to them from the original tree. When an
13350 -- actual subprogram is given by a default, we add an explicit generic
13351 -- association for it in the instantiation node. When we save the
13352 -- global references on the name of the instance, we recover the list
13353 -- of generic associations, and add an explicit one to the original
13354 -- generic tree, through which a global actual can be preserved.
13355 -- Similarly, if a child unit is instantiated within a sibling, in the
13356 -- context of the parent, we must preserve the identifier of the parent
13357 -- so that it can be properly resolved in a subsequent instantiation.
13359 procedure Save_Global_Descendant (D : Union_Id);
13360 -- Apply Save_Global_References recursively to the descendents of the
13363 procedure Save_References (N : Node_Id);
13364 -- This is the recursive procedure that does the work, once the
13365 -- enclosing generic scope has been established.
13371 function Is_Global (E : Entity_Id) return Boolean is
13374 function Is_Instance_Node (Decl : Node_Id) return Boolean;
13375 -- Determine whether the parent node of a reference to a child unit
13376 -- denotes an instantiation or a formal package, in which case the
13377 -- reference to the child unit is global, even if it appears within
13378 -- the current scope (e.g. when the instance appears within the body
13379 -- of an ancestor).
13381 ----------------------
13382 -- Is_Instance_Node --
13383 ----------------------
13385 function Is_Instance_Node (Decl : Node_Id) return Boolean is
13387 return Nkind (Decl) in N_Generic_Instantiation
13389 Nkind (Original_Node (Decl)) = N_Formal_Package_Declaration;
13390 end Is_Instance_Node;
13392 -- Start of processing for Is_Global
13395 if E = Gen_Scope then
13398 elsif E = Standard_Standard then
13401 elsif Is_Child_Unit (E)
13402 and then (Is_Instance_Node (Parent (N2))
13403 or else (Nkind (Parent (N2)) = N_Expanded_Name
13404 and then N2 = Selector_Name (Parent (N2))
13406 Is_Instance_Node (Parent (Parent (N2)))))
13412 while Se /= Gen_Scope loop
13413 if Se = Standard_Standard then
13428 procedure Reset_Entity (N : Node_Id) is
13430 procedure Set_Global_Type (N : Node_Id; N2 : Node_Id);
13431 -- If the type of N2 is global to the generic unit, save the type in
13432 -- the generic node. Just as we perform name capture for explicit
13433 -- references within the generic, we must capture the global types
13434 -- of local entities because they may participate in resolution in
13437 function Top_Ancestor (E : Entity_Id) return Entity_Id;
13438 -- Find the ultimate ancestor of the current unit. If it is not a
13439 -- generic unit, then the name of the current unit in the prefix of
13440 -- an expanded name must be replaced with its generic homonym to
13441 -- ensure that it will be properly resolved in an instance.
13443 ---------------------
13444 -- Set_Global_Type --
13445 ---------------------
13447 procedure Set_Global_Type (N : Node_Id; N2 : Node_Id) is
13448 Typ : constant Entity_Id := Etype (N2);
13451 Set_Etype (N, Typ);
13453 if Entity (N) /= N2
13454 and then Has_Private_View (Entity (N))
13456 -- If the entity of N is not the associated node, this is a
13457 -- nested generic and it has an associated node as well, whose
13458 -- type is already the full view (see below). Indicate that the
13459 -- original node has a private view.
13461 Set_Has_Private_View (N);
13464 -- If not a private type, nothing else to do
13466 if not Is_Private_Type (Typ) then
13467 if Is_Array_Type (Typ)
13468 and then Is_Private_Type (Component_Type (Typ))
13470 Set_Has_Private_View (N);
13473 -- If it is a derivation of a private type in a context where no
13474 -- full view is needed, nothing to do either.
13476 elsif No (Full_View (Typ)) and then Typ /= Etype (Typ) then
13479 -- Otherwise mark the type for flipping and use the full view when
13483 Set_Has_Private_View (N);
13485 if Present (Full_View (Typ)) then
13486 Set_Etype (N2, Full_View (Typ));
13489 end Set_Global_Type;
13495 function Top_Ancestor (E : Entity_Id) return Entity_Id is
13500 while Is_Child_Unit (Par) loop
13501 Par := Scope (Par);
13507 -- Start of processing for Reset_Entity
13510 N2 := Get_Associated_Node (N);
13513 if Present (E) then
13515 -- If the node is an entry call to an entry in an enclosing task,
13516 -- it is rewritten as a selected component. No global entity to
13517 -- preserve in this case, since the expansion will be redone in
13520 if not Nkind_In (E, N_Defining_Identifier,
13521 N_Defining_Character_Literal,
13522 N_Defining_Operator_Symbol)
13524 Set_Associated_Node (N, Empty);
13525 Set_Etype (N, Empty);
13529 -- If the entity is an itype created as a subtype of an access
13530 -- type with a null exclusion restore source entity for proper
13531 -- visibility. The itype will be created anew in the instance.
13534 and then Ekind (E) = E_Access_Subtype
13535 and then Is_Entity_Name (N)
13536 and then Chars (Etype (E)) = Chars (N)
13539 Set_Entity (N2, E);
13543 if Is_Global (E) then
13545 -- If the entity is a package renaming that is the prefix of
13546 -- an expanded name, it has been rewritten as the renamed
13547 -- package, which is necessary semantically but complicates
13548 -- ASIS tree traversal, so we recover the original entity to
13549 -- expose the renaming. Take into account that the context may
13550 -- be a nested generic, that the original node may itself have
13551 -- an associated node that had better be an entity, and that
13552 -- the current node is still a selected component.
13554 if Ekind (E) = E_Package
13555 and then Nkind (N) = N_Selected_Component
13556 and then Nkind (Parent (N)) = N_Expanded_Name
13557 and then Present (Original_Node (N2))
13558 and then Is_Entity_Name (Original_Node (N2))
13559 and then Present (Entity (Original_Node (N2)))
13561 if Is_Global (Entity (Original_Node (N2))) then
13562 N2 := Original_Node (N2);
13563 Set_Associated_Node (N, N2);
13564 Set_Global_Type (N, N2);
13567 -- Renaming is local, and will be resolved in instance
13569 Set_Associated_Node (N, Empty);
13570 Set_Etype (N, Empty);
13574 Set_Global_Type (N, N2);
13577 elsif Nkind (N) = N_Op_Concat
13578 and then Is_Generic_Type (Etype (N2))
13579 and then (Base_Type (Etype (Right_Opnd (N2))) = Etype (N2)
13581 Base_Type (Etype (Left_Opnd (N2))) = Etype (N2))
13582 and then Is_Intrinsic_Subprogram (E)
13587 -- Entity is local. Mark generic node as unresolved.
13588 -- Note that now it does not have an entity.
13590 Set_Associated_Node (N, Empty);
13591 Set_Etype (N, Empty);
13594 if Nkind (Parent (N)) in N_Generic_Instantiation
13595 and then N = Name (Parent (N))
13597 Save_Global_Defaults (Parent (N), Parent (N2));
13600 elsif Nkind (Parent (N)) = N_Selected_Component
13601 and then Nkind (Parent (N2)) = N_Expanded_Name
13603 if Is_Global (Entity (Parent (N2))) then
13604 Change_Selected_Component_To_Expanded_Name (Parent (N));
13605 Set_Associated_Node (Parent (N), Parent (N2));
13606 Set_Global_Type (Parent (N), Parent (N2));
13607 Save_Entity_Descendants (N);
13609 -- If this is a reference to the current generic entity, replace
13610 -- by the name of the generic homonym of the current package. This
13611 -- is because in an instantiation Par.P.Q will not resolve to the
13612 -- name of the instance, whose enclosing scope is not necessarily
13613 -- Par. We use the generic homonym rather that the name of the
13614 -- generic itself because it may be hidden by a local declaration.
13616 elsif In_Open_Scopes (Entity (Parent (N2)))
13618 Is_Generic_Unit (Top_Ancestor (Entity (Prefix (Parent (N2)))))
13620 if Ekind (Entity (Parent (N2))) = E_Generic_Package then
13621 Rewrite (Parent (N),
13622 Make_Identifier (Sloc (N),
13624 Chars (Generic_Homonym (Entity (Parent (N2))))));
13626 Rewrite (Parent (N),
13627 Make_Identifier (Sloc (N),
13628 Chars => Chars (Selector_Name (Parent (N2)))));
13632 if Nkind (Parent (Parent (N))) in N_Generic_Instantiation
13633 and then Parent (N) = Name (Parent (Parent (N)))
13635 Save_Global_Defaults
13636 (Parent (Parent (N)), Parent (Parent ((N2))));
13639 -- A selected component may denote a static constant that has been
13640 -- folded. If the static constant is global to the generic, capture
13641 -- its value. Otherwise the folding will happen in any instantiation.
13643 elsif Nkind (Parent (N)) = N_Selected_Component
13644 and then Nkind_In (Parent (N2), N_Integer_Literal, N_Real_Literal)
13646 if Present (Entity (Original_Node (Parent (N2))))
13647 and then Is_Global (Entity (Original_Node (Parent (N2))))
13649 Rewrite (Parent (N), New_Copy (Parent (N2)));
13650 Set_Analyzed (Parent (N), False);
13656 -- A selected component may be transformed into a parameterless
13657 -- function call. If the called entity is global, rewrite the node
13658 -- appropriately, i.e. as an extended name for the global entity.
13660 elsif Nkind (Parent (N)) = N_Selected_Component
13661 and then Nkind (Parent (N2)) = N_Function_Call
13662 and then N = Selector_Name (Parent (N))
13664 if No (Parameter_Associations (Parent (N2))) then
13665 if Is_Global (Entity (Name (Parent (N2)))) then
13666 Change_Selected_Component_To_Expanded_Name (Parent (N));
13667 Set_Associated_Node (Parent (N), Name (Parent (N2)));
13668 Set_Global_Type (Parent (N), Name (Parent (N2)));
13669 Save_Entity_Descendants (N);
13672 Set_Is_Prefixed_Call (Parent (N));
13673 Set_Associated_Node (N, Empty);
13674 Set_Etype (N, Empty);
13677 -- In Ada 2005, X.F may be a call to a primitive operation,
13678 -- rewritten as F (X). This rewriting will be done again in an
13679 -- instance, so keep the original node. Global entities will be
13680 -- captured as for other constructs. Indicate that this must
13681 -- resolve as a call, to prevent accidental overloading in the
13682 -- instance, if both a component and a primitive operation appear
13686 Set_Is_Prefixed_Call (Parent (N));
13689 -- Entity is local. Reset in generic unit, so that node is resolved
13690 -- anew at the point of instantiation.
13693 Set_Associated_Node (N, Empty);
13694 Set_Etype (N, Empty);
13698 -----------------------------
13699 -- Save_Entity_Descendants --
13700 -----------------------------
13702 procedure Save_Entity_Descendants (N : Node_Id) is
13705 when N_Binary_Op =>
13706 Save_Global_Descendant (Union_Id (Left_Opnd (N)));
13707 Save_Global_Descendant (Union_Id (Right_Opnd (N)));
13710 Save_Global_Descendant (Union_Id (Right_Opnd (N)));
13712 when N_Expanded_Name | N_Selected_Component =>
13713 Save_Global_Descendant (Union_Id (Prefix (N)));
13714 Save_Global_Descendant (Union_Id (Selector_Name (N)));
13716 when N_Identifier | N_Character_Literal | N_Operator_Symbol =>
13720 raise Program_Error;
13722 end Save_Entity_Descendants;
13724 --------------------------
13725 -- Save_Global_Defaults --
13726 --------------------------
13728 procedure Save_Global_Defaults (N1, N2 : Node_Id) is
13729 Loc : constant Source_Ptr := Sloc (N1);
13730 Assoc2 : constant List_Id := Generic_Associations (N2);
13731 Gen_Id : constant Entity_Id := Get_Generic_Entity (N2);
13738 Actual : Entity_Id;
13741 Assoc1 := Generic_Associations (N1);
13743 if Present (Assoc1) then
13744 Act1 := First (Assoc1);
13747 Set_Generic_Associations (N1, New_List);
13748 Assoc1 := Generic_Associations (N1);
13751 if Present (Assoc2) then
13752 Act2 := First (Assoc2);
13757 while Present (Act1) and then Present (Act2) loop
13762 -- Find the associations added for default subprograms
13764 if Present (Act2) then
13765 while Nkind (Act2) /= N_Generic_Association
13766 or else No (Entity (Selector_Name (Act2)))
13767 or else not Is_Overloadable (Entity (Selector_Name (Act2)))
13772 -- Add a similar association if the default is global. The
13773 -- renaming declaration for the actual has been analyzed, and
13774 -- its alias is the program it renames. Link the actual in the
13775 -- original generic tree with the node in the analyzed tree.
13777 while Present (Act2) loop
13778 Subp := Entity (Selector_Name (Act2));
13779 Def := Explicit_Generic_Actual_Parameter (Act2);
13781 -- Following test is defence against rubbish errors
13783 if No (Alias (Subp)) then
13787 -- Retrieve the resolved actual from the renaming declaration
13788 -- created for the instantiated formal.
13790 Actual := Entity (Name (Parent (Parent (Subp))));
13791 Set_Entity (Def, Actual);
13792 Set_Etype (Def, Etype (Actual));
13794 if Is_Global (Actual) then
13796 Make_Generic_Association (Loc,
13797 Selector_Name => New_Occurrence_Of (Subp, Loc),
13798 Explicit_Generic_Actual_Parameter =>
13799 New_Occurrence_Of (Actual, Loc));
13801 Set_Associated_Node
13802 (Explicit_Generic_Actual_Parameter (Ndec), Def);
13804 Append (Ndec, Assoc1);
13806 -- If there are other defaults, add a dummy association in case
13807 -- there are other defaulted formals with the same name.
13809 elsif Present (Next (Act2)) then
13811 Make_Generic_Association (Loc,
13812 Selector_Name => New_Occurrence_Of (Subp, Loc),
13813 Explicit_Generic_Actual_Parameter => Empty);
13815 Append (Ndec, Assoc1);
13822 if Nkind (Name (N1)) = N_Identifier
13823 and then Is_Child_Unit (Gen_Id)
13824 and then Is_Global (Gen_Id)
13825 and then Is_Generic_Unit (Scope (Gen_Id))
13826 and then In_Open_Scopes (Scope (Gen_Id))
13828 -- This is an instantiation of a child unit within a sibling, so
13829 -- that the generic parent is in scope. An eventual instance must
13830 -- occur within the scope of an instance of the parent. Make name
13831 -- in instance into an expanded name, to preserve the identifier
13832 -- of the parent, so it can be resolved subsequently.
13834 Rewrite (Name (N2),
13835 Make_Expanded_Name (Loc,
13836 Chars => Chars (Gen_Id),
13837 Prefix => New_Occurrence_Of (Scope (Gen_Id), Loc),
13838 Selector_Name => New_Occurrence_Of (Gen_Id, Loc)));
13839 Set_Entity (Name (N2), Gen_Id);
13841 Rewrite (Name (N1),
13842 Make_Expanded_Name (Loc,
13843 Chars => Chars (Gen_Id),
13844 Prefix => New_Occurrence_Of (Scope (Gen_Id), Loc),
13845 Selector_Name => New_Occurrence_Of (Gen_Id, Loc)));
13847 Set_Associated_Node (Name (N1), Name (N2));
13848 Set_Associated_Node (Prefix (Name (N1)), Empty);
13849 Set_Associated_Node
13850 (Selector_Name (Name (N1)), Selector_Name (Name (N2)));
13851 Set_Etype (Name (N1), Etype (Gen_Id));
13854 end Save_Global_Defaults;
13856 ----------------------------
13857 -- Save_Global_Descendant --
13858 ----------------------------
13860 procedure Save_Global_Descendant (D : Union_Id) is
13864 if D in Node_Range then
13865 if D = Union_Id (Empty) then
13868 elsif Nkind (Node_Id (D)) /= N_Compilation_Unit then
13869 Save_References (Node_Id (D));
13872 elsif D in List_Range then
13873 if D = Union_Id (No_List) or else Is_Empty_List (List_Id (D)) then
13877 N1 := First (List_Id (D));
13878 while Present (N1) loop
13879 Save_References (N1);
13884 -- Element list or other non-node field, nothing to do
13889 end Save_Global_Descendant;
13891 ---------------------
13892 -- Save_References --
13893 ---------------------
13895 -- This is the recursive procedure that does the work once the enclosing
13896 -- generic scope has been established. We have to treat specially a
13897 -- number of node rewritings that are required by semantic processing
13898 -- and which change the kind of nodes in the generic copy: typically
13899 -- constant-folding, replacing an operator node by a string literal, or
13900 -- a selected component by an expanded name. In each of those cases, the
13901 -- transformation is propagated to the generic unit.
13903 procedure Save_References (N : Node_Id) is
13904 Loc : constant Source_Ptr := Sloc (N);
13910 elsif Nkind_In (N, N_Character_Literal, N_Operator_Symbol) then
13911 if Nkind (N) = Nkind (Get_Associated_Node (N)) then
13914 elsif Nkind (N) = N_Operator_Symbol
13915 and then Nkind (Get_Associated_Node (N)) = N_String_Literal
13917 Change_Operator_Symbol_To_String_Literal (N);
13920 elsif Nkind (N) in N_Op then
13921 if Nkind (N) = Nkind (Get_Associated_Node (N)) then
13922 if Nkind (N) = N_Op_Concat then
13923 Set_Is_Component_Left_Opnd (N,
13924 Is_Component_Left_Opnd (Get_Associated_Node (N)));
13926 Set_Is_Component_Right_Opnd (N,
13927 Is_Component_Right_Opnd (Get_Associated_Node (N)));
13933 -- Node may be transformed into call to a user-defined operator
13935 N2 := Get_Associated_Node (N);
13937 if Nkind (N2) = N_Function_Call then
13938 E := Entity (Name (N2));
13941 and then Is_Global (E)
13943 Set_Etype (N, Etype (N2));
13945 Set_Associated_Node (N, Empty);
13946 Set_Etype (N, Empty);
13949 elsif Nkind_In (N2, N_Integer_Literal,
13953 if Present (Original_Node (N2))
13954 and then Nkind (Original_Node (N2)) = Nkind (N)
13957 -- Operation was constant-folded. Whenever possible,
13958 -- recover semantic information from unfolded node,
13961 Set_Associated_Node (N, Original_Node (N2));
13963 if Nkind (N) = N_Op_Concat then
13964 Set_Is_Component_Left_Opnd (N,
13965 Is_Component_Left_Opnd (Get_Associated_Node (N)));
13966 Set_Is_Component_Right_Opnd (N,
13967 Is_Component_Right_Opnd (Get_Associated_Node (N)));
13973 -- If original node is already modified, propagate
13974 -- constant-folding to template.
13976 Rewrite (N, New_Copy (N2));
13977 Set_Analyzed (N, False);
13980 elsif Nkind (N2) = N_Identifier
13981 and then Ekind (Entity (N2)) = E_Enumeration_Literal
13983 -- Same if call was folded into a literal, but in this case
13984 -- retain the entity to avoid spurious ambiguities if it is
13985 -- overloaded at the point of instantiation or inlining.
13987 Rewrite (N, New_Copy (N2));
13988 Set_Analyzed (N, False);
13992 -- Complete operands check if node has not been constant-folded
13994 if Nkind (N) in N_Op then
13995 Save_Entity_Descendants (N);
13998 elsif Nkind (N) = N_Identifier then
13999 if Nkind (N) = Nkind (Get_Associated_Node (N)) then
14001 -- If this is a discriminant reference, always save it. It is
14002 -- used in the instance to find the corresponding discriminant
14003 -- positionally rather than by name.
14005 Set_Original_Discriminant
14006 (N, Original_Discriminant (Get_Associated_Node (N)));
14010 N2 := Get_Associated_Node (N);
14012 if Nkind (N2) = N_Function_Call then
14013 E := Entity (Name (N2));
14015 -- Name resolves to a call to parameterless function. If
14016 -- original entity is global, mark node as resolved.
14019 and then Is_Global (E)
14021 Set_Etype (N, Etype (N2));
14023 Set_Associated_Node (N, Empty);
14024 Set_Etype (N, Empty);
14027 elsif Nkind_In (N2, N_Integer_Literal, N_Real_Literal)
14028 and then Is_Entity_Name (Original_Node (N2))
14030 -- Name resolves to named number that is constant-folded,
14031 -- We must preserve the original name for ASIS use, and
14032 -- undo the constant-folding, which will be repeated in
14035 Set_Associated_Node (N, Original_Node (N2));
14038 elsif Nkind (N2) = N_String_Literal then
14040 -- Name resolves to string literal. Perform the same
14041 -- replacement in generic.
14043 Rewrite (N, New_Copy (N2));
14045 elsif Nkind (N2) = N_Explicit_Dereference then
14047 -- An identifier is rewritten as a dereference if it is the
14048 -- prefix in an implicit dereference (call or attribute).
14049 -- The analysis of an instantiation will expand the node
14050 -- again, so we preserve the original tree but link it to
14051 -- the resolved entity in case it is global.
14053 if Is_Entity_Name (Prefix (N2))
14054 and then Present (Entity (Prefix (N2)))
14055 and then Is_Global (Entity (Prefix (N2)))
14057 Set_Associated_Node (N, Prefix (N2));
14059 elsif Nkind (Prefix (N2)) = N_Function_Call
14060 and then Is_Global (Entity (Name (Prefix (N2))))
14063 Make_Explicit_Dereference (Loc,
14064 Prefix => Make_Function_Call (Loc,
14067 (Entity (Name (Prefix (N2))), Loc))));
14070 Set_Associated_Node (N, Empty);
14071 Set_Etype (N, Empty);
14074 -- The subtype mark of a nominally unconstrained object is
14075 -- rewritten as a subtype indication using the bounds of the
14076 -- expression. Recover the original subtype mark.
14078 elsif Nkind (N2) = N_Subtype_Indication
14079 and then Is_Entity_Name (Original_Node (N2))
14081 Set_Associated_Node (N, Original_Node (N2));
14089 elsif Nkind (N) in N_Entity then
14094 Qual : Node_Id := Empty;
14095 Typ : Entity_Id := Empty;
14098 use Atree.Unchecked_Access;
14099 -- This code section is part of implementing an untyped tree
14100 -- traversal, so it needs direct access to node fields.
14103 if Nkind_In (N, N_Aggregate, N_Extension_Aggregate) then
14104 N2 := Get_Associated_Node (N);
14112 -- In an instance within a generic, use the name of the
14113 -- actual and not the original generic parameter. If the
14114 -- actual is global in the current generic it must be
14115 -- preserved for its instantiation.
14117 if Nkind (Parent (Typ)) = N_Subtype_Declaration
14119 Present (Generic_Parent_Type (Parent (Typ)))
14121 Typ := Base_Type (Typ);
14122 Set_Etype (N2, Typ);
14126 if No (N2) or else No (Typ) or else not Is_Global (Typ) then
14127 Set_Associated_Node (N, Empty);
14129 -- If the aggregate is an actual in a call, it has been
14130 -- resolved in the current context, to some local type.
14131 -- The enclosing call may have been disambiguated by the
14132 -- aggregate, and this disambiguation might fail at
14133 -- instantiation time because the type to which the
14134 -- aggregate did resolve is not preserved. In order to
14135 -- preserve some of this information, we wrap the
14136 -- aggregate in a qualified expression, using the id of
14137 -- its type. For further disambiguation we qualify the
14138 -- type name with its scope (if visible) because both
14139 -- id's will have corresponding entities in an instance.
14140 -- This resolves most of the problems with missing type
14141 -- information on aggregates in instances.
14143 if Nkind (N2) = Nkind (N)
14144 and then Nkind (Parent (N2)) in N_Subprogram_Call
14145 and then Comes_From_Source (Typ)
14147 if Is_Immediately_Visible (Scope (Typ)) then
14149 Make_Selected_Component (Loc,
14151 Make_Identifier (Loc, Chars (Scope (Typ))),
14153 Make_Identifier (Loc, Chars (Typ)));
14155 Nam := Make_Identifier (Loc, Chars (Typ));
14159 Make_Qualified_Expression (Loc,
14160 Subtype_Mark => Nam,
14161 Expression => Relocate_Node (N));
14165 Save_Global_Descendant (Field1 (N));
14166 Save_Global_Descendant (Field2 (N));
14167 Save_Global_Descendant (Field3 (N));
14168 Save_Global_Descendant (Field5 (N));
14170 if Present (Qual) then
14174 -- All other cases than aggregates
14177 Save_Global_Descendant (Field1 (N));
14178 Save_Global_Descendant (Field2 (N));
14179 Save_Global_Descendant (Field3 (N));
14180 Save_Global_Descendant (Field4 (N));
14181 Save_Global_Descendant (Field5 (N));
14186 -- If a node has aspects, references within their expressions must
14187 -- be saved separately, given they are not directly in the tree.
14189 if Has_Aspects (N) then
14194 Aspect := First (Aspect_Specifications (N));
14195 while Present (Aspect) loop
14196 if Present (Expression (Aspect)) then
14197 Save_Global_References (Expression (Aspect));
14204 end Save_References;
14206 -- Start of processing for Save_Global_References
14209 Gen_Scope := Current_Scope;
14211 -- If the generic unit is a child unit, references to entities in the
14212 -- parent are treated as local, because they will be resolved anew in
14213 -- the context of the instance of the parent.
14215 while Is_Child_Unit (Gen_Scope)
14216 and then Ekind (Scope (Gen_Scope)) = E_Generic_Package
14218 Gen_Scope := Scope (Gen_Scope);
14221 Save_References (N);
14222 end Save_Global_References;
14224 --------------------------------------
14225 -- Set_Copied_Sloc_For_Inlined_Body --
14226 --------------------------------------
14228 procedure Set_Copied_Sloc_For_Inlined_Body (N : Node_Id; E : Entity_Id) is
14230 Create_Instantiation_Source (N, E, True, S_Adjustment);
14231 end Set_Copied_Sloc_For_Inlined_Body;
14233 ---------------------
14234 -- Set_Instance_Of --
14235 ---------------------
14237 procedure Set_Instance_Of (A : Entity_Id; B : Entity_Id) is
14239 Generic_Renamings.Table (Generic_Renamings.Last) := (A, B, Assoc_Null);
14240 Generic_Renamings_HTable.Set (Generic_Renamings.Last);
14241 Generic_Renamings.Increment_Last;
14242 end Set_Instance_Of;
14244 --------------------
14245 -- Set_Next_Assoc --
14246 --------------------
14248 procedure Set_Next_Assoc (E : Assoc_Ptr; Next : Assoc_Ptr) is
14250 Generic_Renamings.Table (E).Next_In_HTable := Next;
14251 end Set_Next_Assoc;
14253 -------------------
14254 -- Start_Generic --
14255 -------------------
14257 procedure Start_Generic is
14259 -- ??? More things could be factored out in this routine.
14260 -- Should probably be done at a later stage.
14262 Generic_Flags.Append (Inside_A_Generic);
14263 Inside_A_Generic := True;
14265 Expander_Mode_Save_And_Set (False);
14268 ----------------------
14269 -- Set_Instance_Env --
14270 ----------------------
14272 procedure Set_Instance_Env
14273 (Gen_Unit : Entity_Id;
14274 Act_Unit : Entity_Id)
14276 Assertion_Status : constant Boolean := Assertions_Enabled;
14277 Save_SPARK_Mode : constant SPARK_Mode_Type := SPARK_Mode;
14278 Save_SPARK_Mode_Pragma : constant Node_Id := SPARK_Mode_Pragma;
14281 -- Regardless of the current mode, predefined units are analyzed in the
14282 -- most current Ada mode, and earlier version Ada checks do not apply
14283 -- to predefined units. Nothing needs to be done for non-internal units.
14284 -- These are always analyzed in the current mode.
14286 if Is_Internal_File_Name
14287 (Fname => Unit_File_Name (Get_Source_Unit (Gen_Unit)),
14288 Renamings_Included => True)
14290 Set_Opt_Config_Switches (True, Current_Sem_Unit = Main_Unit);
14292 -- In Ada2012 we may want to enable assertions in an instance of a
14293 -- predefined unit, in which case we need to preserve the current
14294 -- setting for the Assertions_Enabled flag. This will become more
14295 -- critical when pre/postconditions are added to predefined units,
14296 -- as is already the case for some numeric libraries.
14298 if Ada_Version >= Ada_2012 then
14299 Assertions_Enabled := Assertion_Status;
14302 -- SPARK_Mode for an instance is the one applicable at the point of
14305 SPARK_Mode := Save_SPARK_Mode;
14306 SPARK_Mode_Pragma := Save_SPARK_Mode_Pragma;
14308 -- Make sure dynamic elaboration checks are off in SPARK Mode
14310 if SPARK_Mode = On then
14311 Dynamic_Elaboration_Checks := False;
14315 Current_Instantiated_Parent :=
14316 (Gen_Id => Gen_Unit,
14317 Act_Id => Act_Unit,
14318 Next_In_HTable => Assoc_Null);
14319 end Set_Instance_Env;
14325 procedure Switch_View (T : Entity_Id) is
14326 BT : constant Entity_Id := Base_Type (T);
14327 Priv_Elmt : Elmt_Id := No_Elmt;
14328 Priv_Sub : Entity_Id;
14331 -- T may be private but its base type may have been exchanged through
14332 -- some other occurrence, in which case there is nothing to switch
14333 -- besides T itself. Note that a private dependent subtype of a private
14334 -- type might not have been switched even if the base type has been,
14335 -- because of the last branch of Check_Private_View (see comment there).
14337 if not Is_Private_Type (BT) then
14338 Prepend_Elmt (Full_View (T), Exchanged_Views);
14339 Exchange_Declarations (T);
14343 Priv_Elmt := First_Elmt (Private_Dependents (BT));
14345 if Present (Full_View (BT)) then
14346 Prepend_Elmt (Full_View (BT), Exchanged_Views);
14347 Exchange_Declarations (BT);
14350 while Present (Priv_Elmt) loop
14351 Priv_Sub := (Node (Priv_Elmt));
14353 -- We avoid flipping the subtype if the Etype of its full view is
14354 -- private because this would result in a malformed subtype. This
14355 -- occurs when the Etype of the subtype full view is the full view of
14356 -- the base type (and since the base types were just switched, the
14357 -- subtype is pointing to the wrong view). This is currently the case
14358 -- for tagged record types, access types (maybe more?) and needs to
14359 -- be resolved. ???
14361 if Present (Full_View (Priv_Sub))
14362 and then not Is_Private_Type (Etype (Full_View (Priv_Sub)))
14364 Prepend_Elmt (Full_View (Priv_Sub), Exchanged_Views);
14365 Exchange_Declarations (Priv_Sub);
14368 Next_Elmt (Priv_Elmt);
14376 function True_Parent (N : Node_Id) return Node_Id is
14378 if Nkind (Parent (N)) = N_Subunit then
14379 return Parent (Corresponding_Stub (Parent (N)));
14385 -----------------------------
14386 -- Valid_Default_Attribute --
14387 -----------------------------
14389 procedure Valid_Default_Attribute (Nam : Entity_Id; Def : Node_Id) is
14390 Attr_Id : constant Attribute_Id :=
14391 Get_Attribute_Id (Attribute_Name (Def));
14392 T : constant Entity_Id := Entity (Prefix (Def));
14393 Is_Fun : constant Boolean := (Ekind (Nam) = E_Function);
14399 if No (T) or else T = Any_Id then
14404 F := First_Formal (Nam);
14405 while Present (F) loop
14406 Num_F := Num_F + 1;
14411 when Attribute_Adjacent | Attribute_Ceiling | Attribute_Copy_Sign |
14412 Attribute_Floor | Attribute_Fraction | Attribute_Machine |
14413 Attribute_Model | Attribute_Remainder | Attribute_Rounding |
14414 Attribute_Unbiased_Rounding =>
14417 and then Is_Floating_Point_Type (T);
14419 when Attribute_Image | Attribute_Pred | Attribute_Succ |
14420 Attribute_Value | Attribute_Wide_Image |
14421 Attribute_Wide_Value =>
14422 OK := (Is_Fun and then Num_F = 1 and then Is_Scalar_Type (T));
14424 when Attribute_Max | Attribute_Min =>
14425 OK := (Is_Fun and then Num_F = 2 and then Is_Scalar_Type (T));
14427 when Attribute_Input =>
14428 OK := (Is_Fun and then Num_F = 1);
14430 when Attribute_Output | Attribute_Read | Attribute_Write =>
14431 OK := (not Is_Fun and then Num_F = 2);
14439 ("attribute reference has wrong profile for subprogram", Def);
14441 end Valid_Default_Attribute;