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 Exp_Util; use Exp_Util;
34 with Fname; use Fname;
35 with Fname.UF; use Fname.UF;
36 with Freeze; use Freeze;
37 with Itypes; use Itypes;
39 with Lib.Load; use Lib.Load;
40 with Lib.Xref; use Lib.Xref;
41 with Nlists; use Nlists;
42 with Namet; use Namet;
43 with Nmake; use Nmake;
45 with Rident; use Rident;
46 with Restrict; use Restrict;
47 with Rtsfind; use Rtsfind;
49 with Sem_Aux; use Sem_Aux;
50 with Sem_Cat; use Sem_Cat;
51 with Sem_Ch3; use Sem_Ch3;
52 with Sem_Ch6; use Sem_Ch6;
53 with Sem_Ch7; use Sem_Ch7;
54 with Sem_Ch8; use Sem_Ch8;
55 with Sem_Ch10; use Sem_Ch10;
56 with Sem_Ch13; use Sem_Ch13;
57 with Sem_Dim; use Sem_Dim;
58 with Sem_Disp; use Sem_Disp;
59 with Sem_Elab; use Sem_Elab;
60 with Sem_Elim; use Sem_Elim;
61 with Sem_Eval; use Sem_Eval;
62 with Sem_Prag; use Sem_Prag;
63 with Sem_Res; use Sem_Res;
64 with Sem_Type; use Sem_Type;
65 with Sem_Util; use Sem_Util;
66 with Sem_Warn; use Sem_Warn;
67 with Stand; use Stand;
68 with Sinfo; use Sinfo;
69 with Sinfo.CN; use Sinfo.CN;
70 with Sinput; use Sinput;
71 with Sinput.L; use Sinput.L;
72 with Snames; use Snames;
73 with Stringt; use Stringt;
74 with Uname; use Uname;
76 with Tbuild; use Tbuild;
77 with Uintp; use Uintp;
78 with Urealp; use Urealp;
79 with Warnsw; use Warnsw;
83 package body Sem_Ch12 is
85 ----------------------------------------------------------
86 -- Implementation of Generic Analysis and Instantiation --
87 ----------------------------------------------------------
89 -- GNAT implements generics by macro expansion. No attempt is made to share
90 -- generic instantiations (for now). Analysis of a generic definition does
91 -- not perform any expansion action, but the expander must be called on the
92 -- tree for each instantiation, because the expansion may of course depend
93 -- on the generic actuals. All of this is best achieved as follows:
95 -- a) Semantic analysis of a generic unit is performed on a copy of the
96 -- tree for the generic unit. All tree modifications that follow analysis
97 -- do not affect the original tree. Links are kept between the original
98 -- tree and the copy, in order to recognize non-local references within
99 -- the generic, and propagate them to each instance (recall that name
100 -- resolution is done on the generic declaration: generics are not really
101 -- macros). This is summarized in the following diagram:
103 -- .-----------. .----------.
104 -- | semantic |<--------------| generic |
106 -- | |==============>| |
107 -- |___________| global |__________|
118 -- b) Each instantiation copies the original tree, and inserts into it a
119 -- series of declarations that describe the mapping between generic formals
120 -- and actuals. For example, a generic In OUT parameter is an object
121 -- renaming of the corresponding actual, etc. Generic IN parameters are
122 -- constant declarations.
124 -- c) In order to give the right visibility for these renamings, we use
125 -- a different scheme for package and subprogram instantiations. For
126 -- packages, the list of renamings is inserted into the package
127 -- specification, before the visible declarations of the package. The
128 -- renamings are analyzed before any of the text of the instance, and are
129 -- thus visible at the right place. Furthermore, outside of the instance,
130 -- the generic parameters are visible and denote their corresponding
133 -- For subprograms, we create a container package to hold the renamings
134 -- and the subprogram instance itself. Analysis of the package makes the
135 -- renaming declarations visible to the subprogram. After analyzing the
136 -- package, the defining entity for the subprogram is touched-up so that
137 -- it appears declared in the current scope, and not inside the container
140 -- If the instantiation is a compilation unit, the container package is
141 -- given the same name as the subprogram instance. This ensures that
142 -- the elaboration procedure called by the binder, using the compilation
143 -- unit name, calls in fact the elaboration procedure for the package.
145 -- Not surprisingly, private types complicate this approach. By saving in
146 -- the original generic object the non-local references, we guarantee that
147 -- the proper entities are referenced at the point of instantiation.
148 -- However, for private types, this by itself does not insure that the
149 -- proper VIEW of the entity is used (the full type may be visible at the
150 -- point of generic definition, but not at instantiation, or vice-versa).
151 -- In order to reference the proper view, we special-case any reference
152 -- to private types in the generic object, by saving both views, one in
153 -- the generic and one in the semantic copy. At time of instantiation, we
154 -- check whether the two views are consistent, and exchange declarations if
155 -- necessary, in order to restore the correct visibility. Similarly, if
156 -- the instance view is private when the generic view was not, we perform
157 -- the exchange. After completing the instantiation, we restore the
158 -- current visibility. The flag Has_Private_View marks identifiers in the
159 -- the generic unit that require checking.
161 -- Visibility within nested generic units requires special handling.
162 -- Consider the following scheme:
164 -- type Global is ... -- outside of generic unit.
168 -- type Semi_Global is ... -- global to inner.
171 -- procedure inner (X1 : Global; X2 : Semi_Global);
173 -- procedure in2 is new inner (...); -- 4
176 -- package New_Outer is new Outer (...); -- 2
177 -- procedure New_Inner is new New_Outer.Inner (...); -- 3
179 -- The semantic analysis of Outer captures all occurrences of Global.
180 -- The semantic analysis of Inner (at 1) captures both occurrences of
181 -- Global and Semi_Global.
183 -- At point 2 (instantiation of Outer), we also produce a generic copy
184 -- of Inner, even though Inner is, at that point, not being instantiated.
185 -- (This is just part of the semantic analysis of New_Outer).
187 -- Critically, references to Global within Inner must be preserved, while
188 -- references to Semi_Global should not preserved, because they must now
189 -- resolve to an entity within New_Outer. To distinguish between these, we
190 -- use a global variable, Current_Instantiated_Parent, which is set when
191 -- performing a generic copy during instantiation (at 2). This variable is
192 -- used when performing a generic copy that is not an instantiation, but
193 -- that is nested within one, as the occurrence of 1 within 2. The analysis
194 -- of a nested generic only preserves references that are global to the
195 -- enclosing Current_Instantiated_Parent. We use the Scope_Depth value to
196 -- determine whether a reference is external to the given parent.
198 -- The instantiation at point 3 requires no special treatment. The method
199 -- works as well for further nestings of generic units, but of course the
200 -- variable Current_Instantiated_Parent must be stacked because nested
201 -- instantiations can occur, e.g. the occurrence of 4 within 2.
203 -- The instantiation of package and subprogram bodies is handled in a
204 -- similar manner, except that it is delayed until after semantic
205 -- analysis is complete. In this fashion complex cross-dependencies
206 -- between several package declarations and bodies containing generics
207 -- can be compiled which otherwise would diagnose spurious circularities.
209 -- For example, it is possible to compile two packages A and B that
210 -- have the following structure:
212 -- package A is package B is
213 -- generic ... generic ...
214 -- package G_A is package G_B is
217 -- package body A is package body B is
218 -- package N_B is new G_B (..) package N_A is new G_A (..)
220 -- The table Pending_Instantiations in package Inline is used to keep
221 -- track of body instantiations that are delayed in this manner. Inline
222 -- handles the actual calls to do the body instantiations. This activity
223 -- is part of Inline, since the processing occurs at the same point, and
224 -- for essentially the same reason, as the handling of inlined routines.
226 ----------------------------------------------
227 -- Detection of Instantiation Circularities --
228 ----------------------------------------------
230 -- If we have a chain of instantiations that is circular, this is static
231 -- error which must be detected at compile time. The detection of these
232 -- circularities is carried out at the point that we insert a generic
233 -- instance spec or body. If there is a circularity, then the analysis of
234 -- the offending spec or body will eventually result in trying to load the
235 -- same unit again, and we detect this problem as we analyze the package
236 -- instantiation for the second time.
238 -- At least in some cases after we have detected the circularity, we get
239 -- into trouble if we try to keep going. The following flag is set if a
240 -- circularity is detected, and used to abandon compilation after the
241 -- messages have been posted.
243 Circularity_Detected : Boolean := False;
244 -- This should really be reset on encountering a new main unit, but in
245 -- practice we are not using multiple main units so it is not critical.
247 --------------------------------------------------
248 -- Formal packages and partial parameterization --
249 --------------------------------------------------
251 -- When compiling a generic, a formal package is a local instantiation. If
252 -- declared with a box, its generic formals are visible in the enclosing
253 -- generic. If declared with a partial list of actuals, those actuals that
254 -- are defaulted (covered by an Others clause, or given an explicit box
255 -- initialization) are also visible in the enclosing generic, while those
256 -- that have a corresponding actual are not.
258 -- In our source model of instantiation, the same visibility must be
259 -- present in the spec and body of an instance: the names of the formals
260 -- that are defaulted must be made visible within the instance, and made
261 -- invisible (hidden) after the instantiation is complete, so that they
262 -- are not accessible outside of the instance.
264 -- In a generic, a formal package is treated like a special instantiation.
265 -- Our Ada 95 compiler handled formals with and without box in different
266 -- ways. With partial parameterization, we use a single model for both.
267 -- We create a package declaration that consists of the specification of
268 -- the generic package, and a set of declarations that map the actuals
269 -- into local renamings, just as we do for bona fide instantiations. For
270 -- defaulted parameters and formals with a box, we copy directly the
271 -- declarations of the formal into this local package. The result is a
272 -- a package whose visible declarations may include generic formals. This
273 -- package is only used for type checking and visibility analysis, and
274 -- never reaches the back-end, so it can freely violate the placement
275 -- rules for generic formal declarations.
277 -- The list of declarations (renamings and copies of formals) is built
278 -- by Analyze_Associations, just as for regular instantiations.
280 -- At the point of instantiation, conformance checking must be applied only
281 -- to those parameters that were specified in the formal. We perform this
282 -- checking by creating another internal instantiation, this one including
283 -- only the renamings and the formals (the rest of the package spec is not
284 -- relevant to conformance checking). We can then traverse two lists: the
285 -- list of actuals in the instance that corresponds to the formal package,
286 -- and the list of actuals produced for this bogus instantiation. We apply
287 -- the conformance rules to those actuals that are not defaulted (i.e.
288 -- which still appear as generic formals.
290 -- When we compile an instance body we must make the right parameters
291 -- visible again. The predicate Is_Generic_Formal indicates which of the
292 -- formals should have its Is_Hidden flag reset.
294 -----------------------
295 -- Local subprograms --
296 -----------------------
298 procedure Abandon_Instantiation (N : Node_Id);
299 pragma No_Return (Abandon_Instantiation);
300 -- Posts an error message "instantiation abandoned" at the indicated node
301 -- and then raises the exception Instantiation_Error to do it.
303 procedure Analyze_Formal_Array_Type
304 (T : in out Entity_Id;
306 -- A formal array type is treated like an array type declaration, and
307 -- invokes Array_Type_Declaration (sem_ch3) whose first parameter is
308 -- in-out, because in the case of an anonymous type the entity is
309 -- actually created in the procedure.
311 -- The following procedures treat other kinds of formal parameters
313 procedure Analyze_Formal_Derived_Interface_Type
318 procedure Analyze_Formal_Derived_Type
323 procedure Analyze_Formal_Interface_Type
328 -- The following subprograms create abbreviated declarations for formal
329 -- scalar types. We introduce an anonymous base of the proper class for
330 -- each of them, and define the formals as constrained first subtypes of
331 -- their bases. The bounds are expressions that are non-static in the
334 procedure Analyze_Formal_Decimal_Fixed_Point_Type
335 (T : Entity_Id; Def : Node_Id);
336 procedure Analyze_Formal_Discrete_Type (T : Entity_Id; Def : Node_Id);
337 procedure Analyze_Formal_Floating_Type (T : Entity_Id; Def : Node_Id);
338 procedure Analyze_Formal_Signed_Integer_Type (T : Entity_Id; Def : Node_Id);
339 procedure Analyze_Formal_Modular_Type (T : Entity_Id; Def : Node_Id);
340 procedure Analyze_Formal_Ordinary_Fixed_Point_Type
341 (T : Entity_Id; Def : Node_Id);
343 procedure Analyze_Formal_Private_Type
347 -- Creates a new private type, which does not require completion
349 procedure Analyze_Formal_Incomplete_Type (T : Entity_Id; Def : Node_Id);
350 -- Ada 2012: Creates a new incomplete type whose actual does not freeze
352 procedure Analyze_Generic_Formal_Part (N : Node_Id);
353 -- Analyze generic formal part
355 procedure Analyze_Generic_Access_Type (T : Entity_Id; Def : Node_Id);
356 -- Create a new access type with the given designated type
358 function Analyze_Associations
361 F_Copy : List_Id) return List_Id;
362 -- At instantiation time, build the list of associations between formals
363 -- and actuals. Each association becomes a renaming declaration for the
364 -- formal entity. F_Copy is the analyzed list of formals in the generic
365 -- copy. It is used to apply legality checks to the actuals. I_Node is the
366 -- instantiation node itself.
368 procedure Analyze_Subprogram_Instantiation
372 procedure Build_Instance_Compilation_Unit_Nodes
376 -- This procedure is used in the case where the generic instance of a
377 -- subprogram body or package body is a library unit. In this case, the
378 -- original library unit node for the generic instantiation must be
379 -- replaced by the resulting generic body, and a link made to a new
380 -- compilation unit node for the generic declaration. The argument N is
381 -- the original generic instantiation. Act_Body and Act_Decl are the body
382 -- and declaration of the instance (either package body and declaration
383 -- nodes or subprogram body and declaration nodes depending on the case).
384 -- On return, the node N has been rewritten with the actual body.
386 procedure Check_Access_Definition (N : Node_Id);
387 -- Subsidiary routine to null exclusion processing. Perform an assertion
388 -- check on Ada version and the presence of an access definition in N.
390 procedure Check_Formal_Packages (P_Id : Entity_Id);
391 -- Apply the following to all formal packages in generic associations
393 procedure Check_Formal_Package_Instance
394 (Formal_Pack : Entity_Id;
395 Actual_Pack : Entity_Id);
396 -- Verify that the actuals of the actual instance match the actuals of
397 -- the template for a formal package that is not declared with a box.
399 procedure Check_Forward_Instantiation (Decl : Node_Id);
400 -- If the generic is a local entity and the corresponding body has not
401 -- been seen yet, flag enclosing packages to indicate that it will be
402 -- elaborated after the generic body. Subprograms declared in the same
403 -- package cannot be inlined by the front-end because front-end inlining
404 -- requires a strict linear order of elaboration.
406 function Check_Hidden_Primitives (Assoc_List : List_Id) return Elist_Id;
407 -- Check if some association between formals and actuals requires to make
408 -- visible primitives of a tagged type, and make those primitives visible.
409 -- Return the list of primitives whose visibility is modified (to restore
410 -- their visibility later through Restore_Hidden_Primitives). If no
411 -- candidate is found then return No_Elist.
413 procedure Check_Hidden_Child_Unit
415 Gen_Unit : Entity_Id;
416 Act_Decl_Id : Entity_Id);
417 -- If the generic unit is an implicit child instance within a parent
418 -- instance, we need to make an explicit test that it is not hidden by
419 -- a child instance of the same name and parent.
421 procedure Check_Generic_Actuals
422 (Instance : Entity_Id;
423 Is_Formal_Box : Boolean);
424 -- Similar to previous one. Check the actuals in the instantiation,
425 -- whose views can change between the point of instantiation and the point
426 -- of instantiation of the body. In addition, mark the generic renamings
427 -- as generic actuals, so that they are not compatible with other actuals.
428 -- Recurse on an actual that is a formal package whose declaration has
431 function Contains_Instance_Of
434 N : Node_Id) return Boolean;
435 -- Inner is instantiated within the generic Outer. Check whether Inner
436 -- directly or indirectly contains an instance of Outer or of one of its
437 -- parents, in the case of a subunit. Each generic unit holds a list of
438 -- the entities instantiated within (at any depth). This procedure
439 -- determines whether the set of such lists contains a cycle, i.e. an
440 -- illegal circular instantiation.
442 function Denotes_Formal_Package
444 On_Exit : Boolean := False;
445 Instance : Entity_Id := Empty) return Boolean;
446 -- Returns True if E is a formal package of an enclosing generic, or
447 -- the actual for such a formal in an enclosing instantiation. If such
448 -- a package is used as a formal in an nested generic, or as an actual
449 -- in a nested instantiation, the visibility of ITS formals should not
450 -- be modified. When called from within Restore_Private_Views, the flag
451 -- On_Exit is true, to indicate that the search for a possible enclosing
452 -- instance should ignore the current one. In that case Instance denotes
453 -- the declaration for which this is an actual. This declaration may be
454 -- an instantiation in the source, or the internal instantiation that
455 -- corresponds to the actual for a formal package.
457 function Earlier (N1, N2 : Node_Id) return Boolean;
458 -- Yields True if N1 and N2 appear in the same compilation unit,
459 -- ignoring subunits, and if N1 is to the left of N2 in a left-to-right
460 -- traversal of the tree for the unit. Used to determine the placement
461 -- of freeze nodes for instance bodies that may depend on other instances.
463 function Find_Actual_Type
465 Gen_Type : Entity_Id) return Entity_Id;
466 -- When validating the actual types of a child instance, check whether
467 -- the formal is a formal type of the parent unit, and retrieve the current
468 -- actual for it. Typ is the entity in the analyzed formal type declaration
469 -- (component or index type of an array type, or designated type of an
470 -- access formal) and Gen_Type is the enclosing analyzed formal array
471 -- or access type. The desired actual may be a formal of a parent, or may
472 -- be declared in a formal package of a parent. In both cases it is a
473 -- generic actual type because it appears within a visible instance.
474 -- Finally, it may be declared in a parent unit without being a formal
475 -- of that unit, in which case it must be retrieved by visibility.
476 -- Ambiguities may still arise if two homonyms are declared in two formal
477 -- packages, and the prefix of the formal type may be needed to resolve
478 -- the ambiguity in the instance ???
480 function In_Same_Declarative_Part
482 Inst : Node_Id) return Boolean;
483 -- True if the instantiation Inst and the given freeze_node F_Node appear
484 -- within the same declarative part, ignoring subunits, but with no inter-
485 -- vening subprograms or concurrent units. Used to find the proper plave
486 -- for the freeze node of an instance, when the generic is declared in a
487 -- previous instance. If predicate is true, the freeze node of the instance
488 -- can be placed after the freeze node of the previous instance, Otherwise
489 -- it has to be placed at the end of the current declarative part.
491 function In_Main_Context (E : Entity_Id) return Boolean;
492 -- Check whether an instantiation is in the context of the main unit.
493 -- Used to determine whether its body should be elaborated to allow
494 -- front-end inlining.
496 procedure Set_Instance_Env
497 (Gen_Unit : Entity_Id;
498 Act_Unit : Entity_Id);
499 -- Save current instance on saved environment, to be used to determine
500 -- the global status of entities in nested instances. Part of Save_Env.
501 -- called after verifying that the generic unit is legal for the instance,
502 -- The procedure also examines whether the generic unit is a predefined
503 -- unit, in order to set configuration switches accordingly. As a result
504 -- the procedure must be called after analyzing and freezing the actuals.
506 procedure Set_Instance_Of (A : Entity_Id; B : Entity_Id);
507 -- Associate analyzed generic parameter with corresponding
508 -- instance. Used for semantic checks at instantiation time.
510 function Has_Been_Exchanged (E : Entity_Id) return Boolean;
511 -- Traverse the Exchanged_Views list to see if a type was private
512 -- and has already been flipped during this phase of instantiation.
514 procedure Hide_Current_Scope;
515 -- When instantiating a generic child unit, the parent context must be
516 -- present, but the instance and all entities that may be generated
517 -- must be inserted in the current scope. We leave the current scope
518 -- on the stack, but make its entities invisible to avoid visibility
519 -- problems. This is reversed at the end of the instantiation. This is
520 -- not done for the instantiation of the bodies, which only require the
521 -- instances of the generic parents to be in scope.
523 procedure Install_Body
528 -- If the instantiation happens textually before the body of the generic,
529 -- the instantiation of the body must be analyzed after the generic body,
530 -- and not at the point of instantiation. Such early instantiations can
531 -- happen if the generic and the instance appear in a package declaration
532 -- because the generic body can only appear in the corresponding package
533 -- body. Early instantiations can also appear if generic, instance and
534 -- body are all in the declarative part of a subprogram or entry. Entities
535 -- of packages that are early instantiations are delayed, and their freeze
536 -- node appears after the generic body.
538 procedure Insert_Freeze_Node_For_Instance
541 -- N denotes a package or a subprogram instantiation and F_Node is the
542 -- associated freeze node. Insert the freeze node before the first source
543 -- body which follows immediately after N. If no such body is found, the
544 -- freeze node is inserted at the end of the declarative region which
547 procedure Freeze_Subprogram_Body
548 (Inst_Node : Node_Id;
550 Pack_Id : Entity_Id);
551 -- The generic body may appear textually after the instance, including
552 -- in the proper body of a stub, or within a different package instance.
553 -- Given that the instance can only be elaborated after the generic, we
554 -- place freeze_nodes for the instance and/or for packages that may enclose
555 -- the instance and the generic, so that the back-end can establish the
556 -- proper order of elaboration.
559 -- Establish environment for subsequent instantiation. Separated from
560 -- Save_Env because data-structures for visibility handling must be
561 -- initialized before call to Check_Generic_Child_Unit.
563 procedure Install_Formal_Packages (Par : Entity_Id);
564 -- Install the visible part of any formal of the parent that is a formal
565 -- package. Note that for the case of a formal package with a box, this
566 -- includes the formal part of the formal package (12.7(10/2)).
568 procedure Install_Parent (P : Entity_Id; In_Body : Boolean := False);
569 -- When compiling an instance of a child unit the parent (which is
570 -- itself an instance) is an enclosing scope that must be made
571 -- immediately visible. This procedure is also used to install the non-
572 -- generic parent of a generic child unit when compiling its body, so
573 -- that full views of types in the parent are made visible.
575 procedure Remove_Parent (In_Body : Boolean := False);
576 -- Reverse effect after instantiation of child is complete
578 procedure Install_Hidden_Primitives
579 (Prims_List : in out Elist_Id;
582 -- Remove suffix 'P' from hidden primitives of Act_T to match the
583 -- visibility of primitives of Gen_T. The list of primitives to which
584 -- the suffix is removed is added to Prims_List to restore them later.
586 procedure Restore_Hidden_Primitives (Prims_List : in out Elist_Id);
587 -- Restore suffix 'P' to primitives of Prims_List and leave Prims_List
590 procedure Inline_Instance_Body
592 Gen_Unit : Entity_Id;
594 -- If front-end inlining is requested, instantiate the package body,
595 -- and preserve the visibility of its compilation unit, to insure
596 -- that successive instantiations succeed.
598 -- The functions Instantiate_XXX perform various legality checks and build
599 -- the declarations for instantiated generic parameters. In all of these
600 -- Formal is the entity in the generic unit, Actual is the entity of
601 -- expression in the generic associations, and Analyzed_Formal is the
602 -- formal in the generic copy, which contains the semantic information to
603 -- be used to validate the actual.
605 function Instantiate_Object
608 Analyzed_Formal : Node_Id) return List_Id;
610 function Instantiate_Type
613 Analyzed_Formal : Node_Id;
614 Actual_Decls : List_Id) return List_Id;
616 function Instantiate_Formal_Subprogram
619 Analyzed_Formal : Node_Id) return Node_Id;
621 function Instantiate_Formal_Package
624 Analyzed_Formal : Node_Id) return List_Id;
625 -- If the formal package is declared with a box, special visibility rules
626 -- apply to its formals: they are in the visible part of the package. This
627 -- is true in the declarative region of the formal package, that is to say
628 -- in the enclosing generic or instantiation. For an instantiation, the
629 -- parameters of the formal package are made visible in an explicit step.
630 -- Furthermore, if the actual has a visible USE clause, these formals must
631 -- be made potentially use-visible as well. On exit from the enclosing
632 -- instantiation, the reverse must be done.
634 -- For a formal package declared without a box, there are conformance rules
635 -- that apply to the actuals in the generic declaration and the actuals of
636 -- the actual package in the enclosing instantiation. The simplest way to
637 -- apply these rules is to repeat the instantiation of the formal package
638 -- in the context of the enclosing instance, and compare the generic
639 -- associations of this instantiation with those of the actual package.
640 -- This internal instantiation only needs to contain the renamings of the
641 -- formals: the visible and private declarations themselves need not be
644 -- In Ada 2005, the formal package may be only partially parameterized.
645 -- In that case the visibility step must make visible those actuals whose
646 -- corresponding formals were given with a box. A final complication
647 -- involves inherited operations from formal derived types, which must
648 -- be visible if the type is.
650 function Is_In_Main_Unit (N : Node_Id) return Boolean;
651 -- Test if given node is in the main unit
653 procedure Load_Parent_Of_Generic
656 Body_Optional : Boolean := False);
657 -- If the generic appears in a separate non-generic library unit, load the
658 -- corresponding body to retrieve the body of the generic. N is the node
659 -- for the generic instantiation, Spec is the generic package declaration.
661 -- Body_Optional is a flag that indicates that the body is being loaded to
662 -- ensure that temporaries are generated consistently when there are other
663 -- instances in the current declarative part that precede the one being
664 -- loaded. In that case a missing body is acceptable.
666 procedure Inherit_Context (Gen_Decl : Node_Id; Inst : Node_Id);
667 -- Add the context clause of the unit containing a generic unit to a
668 -- compilation unit that is, or contains, an instantiation.
670 function Get_Associated_Node (N : Node_Id) return Node_Id;
671 -- In order to propagate semantic information back from the analyzed copy
672 -- to the original generic, we maintain links between selected nodes in the
673 -- generic and their corresponding copies. At the end of generic analysis,
674 -- the routine Save_Global_References traverses the generic tree, examines
675 -- the semantic information, and preserves the links to those nodes that
676 -- contain global information. At instantiation, the information from the
677 -- associated node is placed on the new copy, so that name resolution is
680 -- Three kinds of source nodes have associated nodes:
682 -- a) those that can reference (denote) entities, that is identifiers,
683 -- character literals, expanded_names, operator symbols, operators,
684 -- and attribute reference nodes. These nodes have an Entity field
685 -- and are the set of nodes that are in N_Has_Entity.
687 -- b) aggregates (N_Aggregate and N_Extension_Aggregate)
689 -- c) selected components (N_Selected_Component)
691 -- For the first class, the associated node preserves the entity if it is
692 -- global. If the generic contains nested instantiations, the associated
693 -- node itself has been recopied, and a chain of them must be followed.
695 -- For aggregates, the associated node allows retrieval of the type, which
696 -- may otherwise not appear in the generic. The view of this type may be
697 -- different between generic and instantiation, and the full view can be
698 -- installed before the instantiation is analyzed. For aggregates of type
699 -- extensions, the same view exchange may have to be performed for some of
700 -- the ancestor types, if their view is private at the point of
703 -- Nodes that are selected components in the parse tree may be rewritten
704 -- as expanded names after resolution, and must be treated as potential
705 -- entity holders, which is why they also have an Associated_Node.
707 -- Nodes that do not come from source, such as freeze nodes, do not appear
708 -- in the generic tree, and need not have an associated node.
710 -- The associated node is stored in the Associated_Node field. Note that
711 -- this field overlaps Entity, which is fine, because the whole point is
712 -- that we don't need or want the normal Entity field in this situation.
714 procedure Map_Formal_Package_Entities (Form : Entity_Id; Act : Entity_Id);
715 -- Within the generic part, entities in the formal package are
716 -- visible. To validate subsequent type declarations, indicate
717 -- the correspondence between the entities in the analyzed formal,
718 -- and the entities in the actual package. There are three packages
719 -- involved in the instantiation of a formal package: the parent
720 -- generic P1 which appears in the generic declaration, the fake
721 -- instantiation P2 which appears in the analyzed generic, and whose
722 -- visible entities may be used in subsequent formals, and the actual
723 -- P3 in the instance. To validate subsequent formals, me indicate
724 -- that the entities in P2 are mapped into those of P3. The mapping of
725 -- entities has to be done recursively for nested packages.
727 procedure Move_Freeze_Nodes
731 -- Freeze nodes can be generated in the analysis of a generic unit, but
732 -- will not be seen by the back-end. It is necessary to move those nodes
733 -- to the enclosing scope if they freeze an outer entity. We place them
734 -- at the end of the enclosing generic package, which is semantically
737 procedure Preanalyze_Actuals (N : Node_Id);
738 -- Analyze actuals to perform name resolution. Full resolution is done
739 -- later, when the expected types are known, but names have to be captured
740 -- before installing parents of generics, that are not visible for the
741 -- actuals themselves.
743 function True_Parent (N : Node_Id) return Node_Id;
744 -- For a subunit, return parent of corresponding stub, else return
747 procedure Valid_Default_Attribute (Nam : Entity_Id; Def : Node_Id);
748 -- Verify that an attribute that appears as the default for a formal
749 -- subprogram is a function or procedure with the correct profile.
751 -------------------------------------------
752 -- Data Structures for Generic Renamings --
753 -------------------------------------------
755 -- The map Generic_Renamings associates generic entities with their
756 -- corresponding actuals. Currently used to validate type instances. It
757 -- will eventually be used for all generic parameters to eliminate the
758 -- need for overload resolution in the instance.
760 type Assoc_Ptr is new Int;
762 Assoc_Null : constant Assoc_Ptr := -1;
767 Next_In_HTable : Assoc_Ptr;
770 package Generic_Renamings is new Table.Table
771 (Table_Component_Type => Assoc,
772 Table_Index_Type => Assoc_Ptr,
773 Table_Low_Bound => 0,
775 Table_Increment => 100,
776 Table_Name => "Generic_Renamings");
778 -- Variable to hold enclosing instantiation. When the environment is
779 -- saved for a subprogram inlining, the corresponding Act_Id is empty.
781 Current_Instantiated_Parent : Assoc := (Empty, Empty, Assoc_Null);
783 -- Hash table for associations
785 HTable_Size : constant := 37;
786 type HTable_Range is range 0 .. HTable_Size - 1;
788 procedure Set_Next_Assoc (E : Assoc_Ptr; Next : Assoc_Ptr);
789 function Next_Assoc (E : Assoc_Ptr) return Assoc_Ptr;
790 function Get_Gen_Id (E : Assoc_Ptr) return Entity_Id;
791 function Hash (F : Entity_Id) return HTable_Range;
793 package Generic_Renamings_HTable is new GNAT.HTable.Static_HTable (
794 Header_Num => HTable_Range,
796 Elmt_Ptr => Assoc_Ptr,
797 Null_Ptr => Assoc_Null,
798 Set_Next => Set_Next_Assoc,
801 Get_Key => Get_Gen_Id,
805 Exchanged_Views : Elist_Id;
806 -- This list holds the private views that have been exchanged during
807 -- instantiation to restore the visibility of the generic declaration.
808 -- (see comments above). After instantiation, the current visibility is
809 -- reestablished by means of a traversal of this list.
811 Hidden_Entities : Elist_Id;
812 -- This list holds the entities of the current scope that are removed
813 -- from immediate visibility when instantiating a child unit. Their
814 -- visibility is restored in Remove_Parent.
816 -- Because instantiations can be recursive, the following must be saved
817 -- on entry and restored on exit from an instantiation (spec or body).
818 -- This is done by the two procedures Save_Env and Restore_Env. For
819 -- package and subprogram instantiations (but not for the body instances)
820 -- the action of Save_Env is done in two steps: Init_Env is called before
821 -- Check_Generic_Child_Unit, because setting the parent instances requires
822 -- that the visibility data structures be properly initialized. Once the
823 -- generic is unit is validated, Set_Instance_Env completes Save_Env.
825 Parent_Unit_Visible : Boolean := False;
826 -- Parent_Unit_Visible is used when the generic is a child unit, and
827 -- indicates whether the ultimate parent of the generic is visible in the
828 -- instantiation environment. It is used to reset the visibility of the
829 -- parent at the end of the instantiation (see Remove_Parent).
831 Instance_Parent_Unit : Entity_Id := Empty;
832 -- This records the ultimate parent unit of an instance of a generic
833 -- child unit and is used in conjunction with Parent_Unit_Visible to
834 -- indicate the unit to which the Parent_Unit_Visible flag corresponds.
836 type Instance_Env is record
837 Instantiated_Parent : Assoc;
838 Exchanged_Views : Elist_Id;
839 Hidden_Entities : Elist_Id;
840 Current_Sem_Unit : Unit_Number_Type;
841 Parent_Unit_Visible : Boolean := False;
842 Instance_Parent_Unit : Entity_Id := Empty;
843 Switches : Config_Switches_Type;
846 package Instance_Envs is new Table.Table (
847 Table_Component_Type => Instance_Env,
848 Table_Index_Type => Int,
849 Table_Low_Bound => 0,
851 Table_Increment => 100,
852 Table_Name => "Instance_Envs");
854 procedure Restore_Private_Views
855 (Pack_Id : Entity_Id;
856 Is_Package : Boolean := True);
857 -- Restore the private views of external types, and unmark the generic
858 -- renamings of actuals, so that they become compatible subtypes again.
859 -- For subprograms, Pack_Id is the package constructed to hold the
862 procedure Switch_View (T : Entity_Id);
863 -- Switch the partial and full views of a type and its private
864 -- dependents (i.e. its subtypes and derived types).
866 ------------------------------------
867 -- Structures for Error Reporting --
868 ------------------------------------
870 Instantiation_Node : Node_Id;
871 -- Used by subprograms that validate instantiation of formal parameters
872 -- where there might be no actual on which to place the error message.
873 -- Also used to locate the instantiation node for generic subunits.
875 Instantiation_Error : exception;
876 -- When there is a semantic error in the generic parameter matching,
877 -- there is no point in continuing the instantiation, because the
878 -- number of cascaded errors is unpredictable. This exception aborts
879 -- the instantiation process altogether.
881 S_Adjustment : Sloc_Adjustment;
882 -- Offset created for each node in an instantiation, in order to keep
883 -- track of the source position of the instantiation in each of its nodes.
884 -- A subsequent semantic error or warning on a construct of the instance
885 -- points to both places: the original generic node, and the point of
886 -- instantiation. See Sinput and Sinput.L for additional details.
888 ------------------------------------------------------------
889 -- Data structure for keeping track when inside a Generic --
890 ------------------------------------------------------------
892 -- The following table is used to save values of the Inside_A_Generic
893 -- flag (see spec of Sem) when they are saved by Start_Generic.
895 package Generic_Flags is new Table.Table (
896 Table_Component_Type => Boolean,
897 Table_Index_Type => Int,
898 Table_Low_Bound => 0,
900 Table_Increment => 200,
901 Table_Name => "Generic_Flags");
903 ---------------------------
904 -- Abandon_Instantiation --
905 ---------------------------
907 procedure Abandon_Instantiation (N : Node_Id) is
909 Error_Msg_N ("\instantiation abandoned!", N);
910 raise Instantiation_Error;
911 end Abandon_Instantiation;
913 --------------------------
914 -- Analyze_Associations --
915 --------------------------
917 function Analyze_Associations
920 F_Copy : List_Id) return List_Id
922 Actuals_To_Freeze : constant Elist_Id := New_Elmt_List;
923 Assoc : constant List_Id := New_List;
924 Default_Actuals : constant Elist_Id := New_Elmt_List;
925 Gen_Unit : constant Entity_Id :=
926 Defining_Entity (Parent (F_Copy));
930 Analyzed_Formal : Node_Id;
931 First_Named : Node_Id := Empty;
935 Saved_Formal : Node_Id;
937 Default_Formals : constant List_Id := New_List;
938 -- If an Others_Choice is present, some of the formals may be defaulted.
939 -- To simplify the treatment of visibility in an instance, we introduce
940 -- individual defaults for each such formal. These defaults are
941 -- appended to the list of associations and replace the Others_Choice.
943 Found_Assoc : Node_Id;
944 -- Association for the current formal being match. Empty if there are
945 -- no remaining actuals, or if there is no named association with the
946 -- name of the formal.
948 Is_Named_Assoc : Boolean;
949 Num_Matched : Int := 0;
950 Num_Actuals : Int := 0;
952 Others_Present : Boolean := False;
953 Others_Choice : Node_Id := Empty;
954 -- In Ada 2005, indicates partial parameterization of a formal
955 -- package. As usual an other association must be last in the list.
957 function Build_Function_Wrapper
959 Actual : Entity_Id := Empty) return Node_Id;
960 -- In GNATprove mode, create a wrapper function for actuals that are
961 -- functions with any number of formal parameters, in order to propagate
962 -- their contract to the renaming declarations generated for them.
963 -- If the actual is absent, the formal has a default, and the name of
964 -- the function is that of the formal.
966 function Build_Operator_Wrapper
968 Actual : Entity_Id := Empty) return Node_Id;
969 -- In GNATprove mode, create a wrapper function for actuals that are
970 -- operators, in order to propagate their contract to the renaming
971 -- declarations generated for them. If the actual is absent, this is
972 -- a formal with a default, and the name of the operator is that of the
975 procedure Check_Overloaded_Formal_Subprogram (Formal : Entity_Id);
976 -- Apply RM 12.3 (9): if a formal subprogram is overloaded, the instance
977 -- cannot have a named association for it. AI05-0025 extends this rule
978 -- to formals of formal packages by AI05-0025, and it also applies to
979 -- box-initialized formals.
981 function Has_Fully_Defined_Profile (Subp : Entity_Id) return Boolean;
982 -- Determine whether the parameter types and the return type of Subp
983 -- are fully defined at the point of instantiation.
985 function Matching_Actual
987 A_F : Entity_Id) return Node_Id;
988 -- Find actual that corresponds to a given a formal parameter. If the
989 -- actuals are positional, return the next one, if any. If the actuals
990 -- are named, scan the parameter associations to find the right one.
991 -- A_F is the corresponding entity in the analyzed generic,which is
992 -- placed on the selector name for ASIS use.
994 -- In Ada 2005, a named association may be given with a box, in which
995 -- case Matching_Actual sets Found_Assoc to the generic association,
996 -- but return Empty for the actual itself. In this case the code below
997 -- creates a corresponding declaration for the formal.
999 function Partial_Parameterization return Boolean;
1000 -- Ada 2005: if no match is found for a given formal, check if the
1001 -- association for it includes a box, or whether the associations
1002 -- include an Others clause.
1004 procedure Process_Default (F : Entity_Id);
1005 -- Add a copy of the declaration of generic formal F to the list of
1006 -- associations, and add an explicit box association for F if there
1007 -- is none yet, and the default comes from an Others_Choice.
1009 function Renames_Standard_Subprogram (Subp : Entity_Id) return Boolean;
1010 -- Determine whether Subp renames one of the subprograms defined in the
1011 -- generated package Standard.
1013 procedure Set_Analyzed_Formal;
1014 -- Find the node in the generic copy that corresponds to a given formal.
1015 -- The semantic information on this node is used to perform legality
1016 -- checks on the actuals. Because semantic analysis can introduce some
1017 -- anonymous entities or modify the declaration node itself, the
1018 -- correspondence between the two lists is not one-one. In addition to
1019 -- anonymous types, the presence a formal equality will introduce an
1020 -- implicit declaration for the corresponding inequality.
1022 ----------------------------
1023 -- Build_Function_Wrapper --
1024 ----------------------------
1026 function Build_Function_Wrapper
1027 (Formal : Entity_Id;
1028 Actual : Entity_Id := Empty) return Node_Id
1030 Loc : constant Source_Ptr := Sloc (I_Node);
1033 Func_Name : Node_Id;
1036 Parm_Type : Node_Id;
1037 Profile : List_Id := New_List;
1043 -- If there is no actual, the formal has a default and is retrieved
1044 -- by name. Otherwise the wrapper encloses a call to the actual.
1047 Func_Name := Make_Identifier (Loc, Chars (Formal));
1049 Func_Name := New_Occurrence_Of (Entity (Actual), Loc);
1052 Func := Make_Defining_Identifier (Loc, Chars (Formal));
1053 Set_Ekind (Func, E_Function);
1054 Set_Is_Generic_Actual_Subprogram (Func);
1056 Actuals := New_List;
1057 Profile := New_List;
1059 if Present (Actual) then
1060 F := First_Formal (Entity (Actual));
1062 F := First_Formal (Formal);
1066 while Present (F) loop
1068 -- Create new formal for profile of wrapper, and add a reference
1069 -- to it in the list of actuals for the enclosing call.
1071 New_F := Make_Temporary
1072 (Loc, Character'Val (Character'Pos ('A') + N_Parms));
1076 -- If formal has a class-wide type rewrite as the corresponding
1077 -- attribute, because the class-wide type is not retrievable by
1080 if Is_Class_Wide_Type (Etype (F)) then
1082 Make_Attribute_Reference (Loc,
1083 Attribute_Name => Name_Class,
1085 Make_Identifier (Loc, Chars (Etype (Etype (F)))));
1089 Make_Identifier (Loc, Chars (Etype (Etype (F))));
1092 -- If actual is present, use the type of its own formal
1095 Parm_Type := New_Occurrence_Of (Etype (F), Loc);
1099 Make_Parameter_Specification (Loc,
1100 Defining_Identifier => New_F,
1101 Parameter_Type => Parm_Type));
1103 Append_To (Actuals, New_Occurrence_Of (New_F, Loc));
1105 N_Parms := N_Parms + 1;
1109 Make_Function_Specification (Loc,
1110 Defining_Unit_Name => Func,
1111 Parameter_Specifications => Profile,
1112 Result_Definition =>
1113 Make_Identifier (Loc, Chars (Etype (Formal))));
1115 Make_Expression_Function (Loc,
1116 Specification => Spec,
1118 Make_Function_Call (Loc,
1120 Parameter_Associations => Actuals));
1123 end Build_Function_Wrapper;
1125 ----------------------------
1126 -- Build_Operator_Wrapper --
1127 ----------------------------
1129 function Build_Operator_Wrapper
1130 (Formal : Entity_Id;
1131 Actual : Entity_Id := Empty) return Node_Id
1133 Loc : constant Source_Ptr := Sloc (I_Node);
1134 Typ : constant Entity_Id := Etype (Formal);
1135 Is_Binary : constant Boolean :=
1136 Present (Next_Formal (First_Formal (Formal)));
1148 Op_Name := Chars (Formal);
1150 Op_Name := Chars (Actual);
1153 -- Create entities for wrapper function and its formals
1155 F1 := Make_Temporary (Loc, 'A');
1156 F2 := Make_Temporary (Loc, 'B');
1157 L := New_Occurrence_Of (F1, Loc);
1158 R := New_Occurrence_Of (F2, Loc);
1160 Func := Make_Defining_Identifier (Loc, Chars (Formal));
1161 Set_Ekind (Func, E_Function);
1162 Set_Is_Generic_Actual_Subprogram (Func);
1165 Make_Function_Specification (Loc,
1166 Defining_Unit_Name => Func,
1167 Parameter_Specifications => New_List (
1168 Make_Parameter_Specification (Loc,
1169 Defining_Identifier => F1,
1171 Make_Identifier (Loc,
1172 Chars => Chars (Etype (First_Formal (Formal)))))),
1173 Result_Definition => Make_Identifier (Loc, Chars (Typ)));
1176 Append_To (Parameter_Specifications (Spec),
1177 Make_Parameter_Specification (Loc,
1178 Defining_Identifier => F2,
1180 Make_Identifier (Loc,
1181 Chars (Etype (Next_Formal (First_Formal (Formal)))))));
1184 -- Build expression as a function call, or as an operator node
1185 -- that corresponds to the name of the actual, starting with binary
1188 if Present (Actual) and then Op_Name not in Any_Operator_Name then
1190 Make_Function_Call (Loc,
1192 New_Occurrence_Of (Entity (Actual), Loc),
1193 Parameter_Associations => New_List (L));
1196 Append_To (Parameter_Associations (Expr), R);
1201 elsif Is_Binary then
1202 if Op_Name = Name_Op_And then
1203 Expr := Make_Op_And (Loc, Left_Opnd => L, Right_Opnd => R);
1204 elsif Op_Name = Name_Op_Or then
1205 Expr := Make_Op_Or (Loc, Left_Opnd => L, Right_Opnd => R);
1206 elsif Op_Name = Name_Op_Xor then
1207 Expr := Make_Op_Xor (Loc, Left_Opnd => L, Right_Opnd => R);
1208 elsif Op_Name = Name_Op_Eq then
1209 Expr := Make_Op_Eq (Loc, Left_Opnd => L, Right_Opnd => R);
1210 elsif Op_Name = Name_Op_Ne then
1211 Expr := Make_Op_Ne (Loc, Left_Opnd => L, Right_Opnd => R);
1212 elsif Op_Name = Name_Op_Le then
1213 Expr := Make_Op_Le (Loc, Left_Opnd => L, Right_Opnd => R);
1214 elsif Op_Name = Name_Op_Gt then
1215 Expr := Make_Op_Gt (Loc, Left_Opnd => L, Right_Opnd => R);
1216 elsif Op_Name = Name_Op_Ge then
1217 Expr := Make_Op_Ge (Loc, Left_Opnd => L, Right_Opnd => R);
1218 elsif Op_Name = Name_Op_Lt then
1219 Expr := Make_Op_Lt (Loc, Left_Opnd => L, Right_Opnd => R);
1220 elsif Op_Name = Name_Op_Add then
1221 Expr := Make_Op_Add (Loc, Left_Opnd => L, Right_Opnd => R);
1222 elsif Op_Name = Name_Op_Subtract then
1223 Expr := Make_Op_Subtract (Loc, Left_Opnd => L, Right_Opnd => R);
1224 elsif Op_Name = Name_Op_Concat then
1225 Expr := Make_Op_Concat (Loc, Left_Opnd => L, Right_Opnd => R);
1226 elsif Op_Name = Name_Op_Multiply then
1227 Expr := Make_Op_Multiply (Loc, Left_Opnd => L, Right_Opnd => R);
1228 elsif Op_Name = Name_Op_Divide then
1229 Expr := Make_Op_Divide (Loc, Left_Opnd => L, Right_Opnd => R);
1230 elsif Op_Name = Name_Op_Mod then
1231 Expr := Make_Op_Mod (Loc, Left_Opnd => L, Right_Opnd => R);
1232 elsif Op_Name = Name_Op_Rem then
1233 Expr := Make_Op_Rem (Loc, Left_Opnd => L, Right_Opnd => R);
1234 elsif Op_Name = Name_Op_Expon then
1235 Expr := Make_Op_Expon (Loc, Left_Opnd => L, Right_Opnd => R);
1241 if Op_Name = Name_Op_Add then
1242 Expr := Make_Op_Plus (Loc, Right_Opnd => L);
1243 elsif Op_Name = Name_Op_Subtract then
1244 Expr := Make_Op_Minus (Loc, Right_Opnd => L);
1245 elsif Op_Name = Name_Op_Abs then
1246 Expr := Make_Op_Abs (Loc, Right_Opnd => L);
1247 elsif Op_Name = Name_Op_Not then
1248 Expr := Make_Op_Not (Loc, Right_Opnd => L);
1252 -- Propagate visible entity to operator node, either from a
1253 -- given actual or from a default.
1255 if Is_Entity_Name (Actual) and then Nkind (Expr) in N_Op then
1256 Set_Entity (Expr, Entity (Actual));
1260 Make_Expression_Function (Loc,
1261 Specification => Spec,
1262 Expression => Expr);
1265 end Build_Operator_Wrapper;
1267 ----------------------------------------
1268 -- Check_Overloaded_Formal_Subprogram --
1269 ----------------------------------------
1271 procedure Check_Overloaded_Formal_Subprogram (Formal : Entity_Id) is
1272 Temp_Formal : Entity_Id;
1275 Temp_Formal := First (Formals);
1276 while Present (Temp_Formal) loop
1277 if Nkind (Temp_Formal) in N_Formal_Subprogram_Declaration
1278 and then Temp_Formal /= Formal
1280 Chars (Defining_Unit_Name (Specification (Formal))) =
1281 Chars (Defining_Unit_Name (Specification (Temp_Formal)))
1283 if Present (Found_Assoc) then
1285 ("named association not allowed for overloaded formal",
1290 ("named association not allowed for overloaded formal",
1294 Abandon_Instantiation (Instantiation_Node);
1299 end Check_Overloaded_Formal_Subprogram;
1301 -------------------------------
1302 -- Has_Fully_Defined_Profile --
1303 -------------------------------
1305 function Has_Fully_Defined_Profile (Subp : Entity_Id) return Boolean is
1306 function Is_Fully_Defined_Type (Typ : Entity_Id) return Boolean;
1307 -- Determine whethet type Typ is fully defined
1309 ---------------------------
1310 -- Is_Fully_Defined_Type --
1311 ---------------------------
1313 function Is_Fully_Defined_Type (Typ : Entity_Id) return Boolean is
1315 -- A private type without a full view is not fully defined
1317 if Is_Private_Type (Typ)
1318 and then No (Full_View (Typ))
1322 -- An incomplete type is never fully defined
1324 elsif Is_Incomplete_Type (Typ) then
1327 -- All other types are fully defined
1332 end Is_Fully_Defined_Type;
1334 -- Local declarations
1338 -- Start of processing for Has_Fully_Defined_Profile
1341 -- Check the parameters
1343 Param := First_Formal (Subp);
1344 while Present (Param) loop
1345 if not Is_Fully_Defined_Type (Etype (Param)) then
1349 Next_Formal (Param);
1352 -- Check the return type
1354 return Is_Fully_Defined_Type (Etype (Subp));
1355 end Has_Fully_Defined_Profile;
1357 ---------------------
1358 -- Matching_Actual --
1359 ---------------------
1361 function Matching_Actual
1363 A_F : Entity_Id) return Node_Id
1369 Is_Named_Assoc := False;
1371 -- End of list of purely positional parameters
1373 if No (Actual) or else Nkind (Actual) = N_Others_Choice then
1374 Found_Assoc := Empty;
1377 -- Case of positional parameter corresponding to current formal
1379 elsif No (Selector_Name (Actual)) then
1380 Found_Assoc := Actual;
1381 Act := Explicit_Generic_Actual_Parameter (Actual);
1382 Num_Matched := Num_Matched + 1;
1385 -- Otherwise scan list of named actuals to find the one with the
1386 -- desired name. All remaining actuals have explicit names.
1389 Is_Named_Assoc := True;
1390 Found_Assoc := Empty;
1394 while Present (Actual) loop
1395 if Chars (Selector_Name (Actual)) = Chars (F) then
1396 Set_Entity (Selector_Name (Actual), A_F);
1397 Set_Etype (Selector_Name (Actual), Etype (A_F));
1398 Generate_Reference (A_F, Selector_Name (Actual));
1399 Found_Assoc := Actual;
1400 Act := Explicit_Generic_Actual_Parameter (Actual);
1401 Num_Matched := Num_Matched + 1;
1409 -- Reset for subsequent searches. In most cases the named
1410 -- associations are in order. If they are not, we reorder them
1411 -- to avoid scanning twice the same actual. This is not just a
1412 -- question of efficiency: there may be multiple defaults with
1413 -- boxes that have the same name. In a nested instantiation we
1414 -- insert actuals for those defaults, and cannot rely on their
1415 -- names to disambiguate them.
1417 if Actual = First_Named then
1420 elsif Present (Actual) then
1421 Insert_Before (First_Named, Remove_Next (Prev));
1424 Actual := First_Named;
1427 if Is_Entity_Name (Act) and then Present (Entity (Act)) then
1428 Set_Used_As_Generic_Actual (Entity (Act));
1432 end Matching_Actual;
1434 ------------------------------
1435 -- Partial_Parameterization --
1436 ------------------------------
1438 function Partial_Parameterization return Boolean is
1440 return Others_Present
1441 or else (Present (Found_Assoc) and then Box_Present (Found_Assoc));
1442 end Partial_Parameterization;
1444 ---------------------
1445 -- Process_Default --
1446 ---------------------
1448 procedure Process_Default (F : Entity_Id) is
1449 Loc : constant Source_Ptr := Sloc (I_Node);
1450 F_Id : constant Entity_Id := Defining_Entity (F);
1456 -- Append copy of formal declaration to associations, and create new
1457 -- defining identifier for it.
1459 Decl := New_Copy_Tree (F);
1460 Id := Make_Defining_Identifier (Sloc (F_Id), Chars (F_Id));
1462 if Nkind (F) in N_Formal_Subprogram_Declaration then
1463 Set_Defining_Unit_Name (Specification (Decl), Id);
1466 Set_Defining_Identifier (Decl, Id);
1469 Append (Decl, Assoc);
1471 if No (Found_Assoc) then
1473 Make_Generic_Association (Loc,
1474 Selector_Name => New_Occurrence_Of (Id, Loc),
1475 Explicit_Generic_Actual_Parameter => Empty);
1476 Set_Box_Present (Default);
1477 Append (Default, Default_Formals);
1479 end Process_Default;
1481 ---------------------------------
1482 -- Renames_Standard_Subprogram --
1483 ---------------------------------
1485 function Renames_Standard_Subprogram (Subp : Entity_Id) return Boolean is
1490 while Present (Id) loop
1491 if Scope (Id) = Standard_Standard then
1499 end Renames_Standard_Subprogram;
1501 -------------------------
1502 -- Set_Analyzed_Formal --
1503 -------------------------
1505 procedure Set_Analyzed_Formal is
1509 while Present (Analyzed_Formal) loop
1510 Kind := Nkind (Analyzed_Formal);
1512 case Nkind (Formal) is
1514 when N_Formal_Subprogram_Declaration =>
1515 exit when Kind in N_Formal_Subprogram_Declaration
1518 (Defining_Unit_Name (Specification (Formal))) =
1520 (Defining_Unit_Name (Specification (Analyzed_Formal)));
1522 when N_Formal_Package_Declaration =>
1523 exit when Nkind_In (Kind, N_Formal_Package_Declaration,
1524 N_Generic_Package_Declaration,
1525 N_Package_Declaration);
1527 when N_Use_Package_Clause | N_Use_Type_Clause => exit;
1531 -- Skip freeze nodes, and nodes inserted to replace
1532 -- unrecognized pragmas.
1535 Kind not in N_Formal_Subprogram_Declaration
1536 and then not Nkind_In (Kind, N_Subprogram_Declaration,
1540 and then Chars (Defining_Identifier (Formal)) =
1541 Chars (Defining_Identifier (Analyzed_Formal));
1544 Next (Analyzed_Formal);
1546 end Set_Analyzed_Formal;
1548 -- Start of processing for Analyze_Associations
1551 Actuals := Generic_Associations (I_Node);
1553 if Present (Actuals) then
1555 -- Check for an Others choice, indicating a partial parameterization
1556 -- for a formal package.
1558 Actual := First (Actuals);
1559 while Present (Actual) loop
1560 if Nkind (Actual) = N_Others_Choice then
1561 Others_Present := True;
1562 Others_Choice := Actual;
1564 if Present (Next (Actual)) then
1565 Error_Msg_N ("others must be last association", Actual);
1568 -- This subprogram is used both for formal packages and for
1569 -- instantiations. For the latter, associations must all be
1572 if Nkind (I_Node) /= N_Formal_Package_Declaration
1573 and then Comes_From_Source (I_Node)
1576 ("others association not allowed in an instance",
1580 -- In any case, nothing to do after the others association
1584 elsif Box_Present (Actual)
1585 and then Comes_From_Source (I_Node)
1586 and then Nkind (I_Node) /= N_Formal_Package_Declaration
1589 ("box association not allowed in an instance", Actual);
1595 -- If named associations are present, save first named association
1596 -- (it may of course be Empty) to facilitate subsequent name search.
1598 First_Named := First (Actuals);
1599 while Present (First_Named)
1600 and then Nkind (First_Named) /= N_Others_Choice
1601 and then No (Selector_Name (First_Named))
1603 Num_Actuals := Num_Actuals + 1;
1608 Named := First_Named;
1609 while Present (Named) loop
1610 if Nkind (Named) /= N_Others_Choice
1611 and then No (Selector_Name (Named))
1613 Error_Msg_N ("invalid positional actual after named one", Named);
1614 Abandon_Instantiation (Named);
1617 -- A named association may lack an actual parameter, if it was
1618 -- introduced for a default subprogram that turns out to be local
1619 -- to the outer instantiation.
1621 if Nkind (Named) /= N_Others_Choice
1622 and then Present (Explicit_Generic_Actual_Parameter (Named))
1624 Num_Actuals := Num_Actuals + 1;
1630 if Present (Formals) then
1631 Formal := First_Non_Pragma (Formals);
1632 Analyzed_Formal := First_Non_Pragma (F_Copy);
1634 if Present (Actuals) then
1635 Actual := First (Actuals);
1637 -- All formals should have default values
1643 while Present (Formal) loop
1644 Set_Analyzed_Formal;
1645 Saved_Formal := Next_Non_Pragma (Formal);
1647 case Nkind (Formal) is
1648 when N_Formal_Object_Declaration =>
1651 Defining_Identifier (Formal),
1652 Defining_Identifier (Analyzed_Formal));
1654 if No (Match) and then Partial_Parameterization then
1655 Process_Default (Formal);
1658 (Instantiate_Object (Formal, Match, Analyzed_Formal),
1662 when N_Formal_Type_Declaration =>
1665 Defining_Identifier (Formal),
1666 Defining_Identifier (Analyzed_Formal));
1669 if Partial_Parameterization then
1670 Process_Default (Formal);
1673 Error_Msg_Sloc := Sloc (Gen_Unit);
1677 Defining_Identifier (Formal));
1678 Error_Msg_NE ("\in instantiation of & declared#",
1679 Instantiation_Node, Gen_Unit);
1680 Abandon_Instantiation (Instantiation_Node);
1687 (Formal, Match, Analyzed_Formal, Assoc),
1690 -- An instantiation is a freeze point for the actuals,
1691 -- unless this is a rewritten formal package, or the
1692 -- formal is an Ada 2012 formal incomplete type.
1694 if Nkind (I_Node) = N_Formal_Package_Declaration
1696 (Ada_Version >= Ada_2012
1698 Ekind (Defining_Identifier (Analyzed_Formal)) =
1704 Append_Elmt (Entity (Match), Actuals_To_Freeze);
1708 -- A remote access-to-class-wide type is not a legal actual
1709 -- for a generic formal of an access type (E.2.2(17/2)).
1710 -- In GNAT an exception to this rule is introduced when
1711 -- the formal is marked as remote using implementation
1712 -- defined aspect/pragma Remote_Access_Type. In that case
1713 -- the actual must be remote as well.
1715 -- If the current instantiation is the construction of a
1716 -- local copy for a formal package the actuals may be
1717 -- defaulted, and there is no matching actual to check.
1719 if Nkind (Analyzed_Formal) = N_Formal_Type_Declaration
1721 Nkind (Formal_Type_Definition (Analyzed_Formal)) =
1722 N_Access_To_Object_Definition
1723 and then Present (Match)
1726 Formal_Ent : constant Entity_Id :=
1727 Defining_Identifier (Analyzed_Formal);
1729 if Is_Remote_Access_To_Class_Wide_Type (Entity (Match))
1730 = Is_Remote_Types (Formal_Ent)
1732 -- Remoteness of formal and actual match
1736 elsif Is_Remote_Types (Formal_Ent) then
1738 -- Remote formal, non-remote actual
1741 ("actual for& must be remote", Match, Formal_Ent);
1744 -- Non-remote formal, remote actual
1747 ("actual for& may not be remote",
1753 when N_Formal_Subprogram_Declaration =>
1756 (Defining_Unit_Name (Specification (Formal)),
1757 Defining_Unit_Name (Specification (Analyzed_Formal)));
1759 -- If the formal subprogram has the same name as another
1760 -- formal subprogram of the generic, then a named
1761 -- association is illegal (12.3(9)). Exclude named
1762 -- associations that are generated for a nested instance.
1765 and then Is_Named_Assoc
1766 and then Comes_From_Source (Found_Assoc)
1768 Check_Overloaded_Formal_Subprogram (Formal);
1771 -- If there is no corresponding actual, this may be case
1772 -- of partial parameterization, or else the formal has a
1773 -- default or a box.
1775 if No (Match) and then Partial_Parameterization then
1776 Process_Default (Formal);
1778 if Nkind (I_Node) = N_Formal_Package_Declaration then
1779 Check_Overloaded_Formal_Subprogram (Formal);
1785 (Containing_Package_With_Ext_Axioms
1786 (Defining_Entity (Analyzed_Formal)))
1787 and then Ekind (Defining_Entity (Analyzed_Formal)) =
1790 -- If actual is an entity (function or operator),
1791 -- build wrapper for it.
1793 if Present (Match) then
1794 if Nkind (Match) = N_Operator_Symbol then
1796 -- If the name is a default, find its visible
1797 -- entity at the point of instantiation.
1799 if Is_Entity_Name (Match)
1800 and then No (Entity (Match))
1802 Find_Direct_Name (Match);
1807 Build_Operator_Wrapper
1808 (Defining_Entity (Analyzed_Formal), Match));
1812 Build_Function_Wrapper
1813 (Defining_Entity (Analyzed_Formal), Match));
1816 -- Ditto if formal is an operator with a default.
1818 elsif Box_Present (Formal)
1819 and then Nkind (Defining_Entity (Analyzed_Formal)) =
1820 N_Defining_Operator_Symbol
1823 Build_Operator_Wrapper
1824 (Defining_Entity (Analyzed_Formal)));
1826 -- Otherwise create renaming declaration.
1830 Build_Function_Wrapper
1831 (Defining_Entity (Analyzed_Formal)));
1836 Instantiate_Formal_Subprogram
1837 (Formal, Match, Analyzed_Formal));
1840 -- An instantiation is a freeze point for the actuals,
1841 -- unless this is a rewritten formal package.
1843 if Nkind (I_Node) /= N_Formal_Package_Declaration
1844 and then Nkind (Match) = N_Identifier
1845 and then Is_Subprogram (Entity (Match))
1847 -- The actual subprogram may rename a routine defined
1848 -- in Standard. Avoid freezing such renamings because
1849 -- subprograms coming from Standard cannot be frozen.
1852 not Renames_Standard_Subprogram (Entity (Match))
1854 -- If the actual subprogram comes from a different
1855 -- unit, it is already frozen, either by a body in
1856 -- that unit or by the end of the declarative part
1857 -- of the unit. This check avoids the freezing of
1858 -- subprograms defined in Standard which are used
1859 -- as generic actuals.
1861 and then In_Same_Code_Unit (Entity (Match), I_Node)
1862 and then Has_Fully_Defined_Profile (Entity (Match))
1864 -- Mark the subprogram as having a delayed freeze
1865 -- since this may be an out-of-order action.
1867 Set_Has_Delayed_Freeze (Entity (Match));
1868 Append_Elmt (Entity (Match), Actuals_To_Freeze);
1872 -- If this is a nested generic, preserve default for later
1875 if No (Match) and then Box_Present (Formal) then
1877 (Defining_Unit_Name (Specification (Last (Assoc))),
1881 when N_Formal_Package_Declaration =>
1884 Defining_Identifier (Formal),
1885 Defining_Identifier (Original_Node (Analyzed_Formal)));
1888 if Partial_Parameterization then
1889 Process_Default (Formal);
1892 Error_Msg_Sloc := Sloc (Gen_Unit);
1895 Instantiation_Node, Defining_Identifier (Formal));
1896 Error_Msg_NE ("\in instantiation of & declared#",
1897 Instantiation_Node, Gen_Unit);
1899 Abandon_Instantiation (Instantiation_Node);
1905 (Instantiate_Formal_Package
1906 (Formal, Match, Analyzed_Formal),
1910 -- For use type and use package appearing in the generic part,
1911 -- we have already copied them, so we can just move them where
1912 -- they belong (we mustn't recopy them since this would mess up
1913 -- the Sloc values).
1915 when N_Use_Package_Clause |
1916 N_Use_Type_Clause =>
1917 if Nkind (Original_Node (I_Node)) =
1918 N_Formal_Package_Declaration
1920 Append (New_Copy_Tree (Formal), Assoc);
1923 Append (Formal, Assoc);
1927 raise Program_Error;
1931 Formal := Saved_Formal;
1932 Next_Non_Pragma (Analyzed_Formal);
1935 if Num_Actuals > Num_Matched then
1936 Error_Msg_Sloc := Sloc (Gen_Unit);
1938 if Present (Selector_Name (Actual)) then
1940 ("unmatched actual&",
1941 Actual, Selector_Name (Actual));
1942 Error_Msg_NE ("\in instantiation of& declared#",
1946 ("unmatched actual in instantiation of& declared#",
1951 elsif Present (Actuals) then
1953 ("too many actuals in generic instantiation", Instantiation_Node);
1956 -- An instantiation freezes all generic actuals. The only exceptions
1957 -- to this are incomplete types and subprograms which are not fully
1958 -- defined at the point of instantiation.
1961 Elmt : Elmt_Id := First_Elmt (Actuals_To_Freeze);
1963 while Present (Elmt) loop
1964 Freeze_Before (I_Node, Node (Elmt));
1969 -- If there are default subprograms, normalize the tree by adding
1970 -- explicit associations for them. This is required if the instance
1971 -- appears within a generic.
1979 Elmt := First_Elmt (Default_Actuals);
1980 while Present (Elmt) loop
1981 if No (Actuals) then
1982 Actuals := New_List;
1983 Set_Generic_Associations (I_Node, Actuals);
1986 Subp := Node (Elmt);
1988 Make_Generic_Association (Sloc (Subp),
1989 Selector_Name => New_Occurrence_Of (Subp, Sloc (Subp)),
1990 Explicit_Generic_Actual_Parameter =>
1991 New_Occurrence_Of (Subp, Sloc (Subp)));
1992 Mark_Rewrite_Insertion (New_D);
1993 Append_To (Actuals, New_D);
1998 -- If this is a formal package, normalize the parameter list by adding
1999 -- explicit box associations for the formals that are covered by an
2002 if not Is_Empty_List (Default_Formals) then
2003 Append_List (Default_Formals, Formals);
2007 end Analyze_Associations;
2009 -------------------------------
2010 -- Analyze_Formal_Array_Type --
2011 -------------------------------
2013 procedure Analyze_Formal_Array_Type
2014 (T : in out Entity_Id;
2020 -- Treated like a non-generic array declaration, with additional
2025 if Nkind (Def) = N_Constrained_Array_Definition then
2026 DSS := First (Discrete_Subtype_Definitions (Def));
2027 while Present (DSS) loop
2028 if Nkind_In (DSS, N_Subtype_Indication,
2030 N_Attribute_Reference)
2032 Error_Msg_N ("only a subtype mark is allowed in a formal", DSS);
2039 Array_Type_Declaration (T, Def);
2040 Set_Is_Generic_Type (Base_Type (T));
2042 if Ekind (Component_Type (T)) = E_Incomplete_Type
2043 and then No (Full_View (Component_Type (T)))
2045 Error_Msg_N ("premature usage of incomplete type", Def);
2047 -- Check that range constraint is not allowed on the component type
2048 -- of a generic formal array type (AARM 12.5.3(3))
2050 elsif Is_Internal (Component_Type (T))
2051 and then Present (Subtype_Indication (Component_Definition (Def)))
2052 and then Nkind (Original_Node
2053 (Subtype_Indication (Component_Definition (Def)))) =
2054 N_Subtype_Indication
2057 ("in a formal, a subtype indication can only be "
2058 & "a subtype mark (RM 12.5.3(3))",
2059 Subtype_Indication (Component_Definition (Def)));
2062 end Analyze_Formal_Array_Type;
2064 ---------------------------------------------
2065 -- Analyze_Formal_Decimal_Fixed_Point_Type --
2066 ---------------------------------------------
2068 -- As for other generic types, we create a valid type representation with
2069 -- legal but arbitrary attributes, whose values are never considered
2070 -- static. For all scalar types we introduce an anonymous base type, with
2071 -- the same attributes. We choose the corresponding integer type to be
2072 -- Standard_Integer.
2073 -- Here and in other similar routines, the Sloc of the generated internal
2074 -- type must be the same as the sloc of the defining identifier of the
2075 -- formal type declaration, to provide proper source navigation.
2077 procedure Analyze_Formal_Decimal_Fixed_Point_Type
2081 Loc : constant Source_Ptr := Sloc (Def);
2083 Base : constant Entity_Id :=
2085 (E_Decimal_Fixed_Point_Type,
2087 Sloc (Defining_Identifier (Parent (Def))), 'G');
2089 Int_Base : constant Entity_Id := Standard_Integer;
2090 Delta_Val : constant Ureal := Ureal_1;
2091 Digs_Val : constant Uint := Uint_6;
2093 function Make_Dummy_Bound return Node_Id;
2094 -- Return a properly typed universal real literal to use as a bound
2096 ----------------------
2097 -- Make_Dummy_Bound --
2098 ----------------------
2100 function Make_Dummy_Bound return Node_Id is
2101 Bound : constant Node_Id := Make_Real_Literal (Loc, Ureal_1);
2103 Set_Etype (Bound, Universal_Real);
2105 end Make_Dummy_Bound;
2107 -- Start of processing for Analyze_Formal_Decimal_Fixed_Point_Type
2112 Set_Etype (Base, Base);
2113 Set_Size_Info (Base, Int_Base);
2114 Set_RM_Size (Base, RM_Size (Int_Base));
2115 Set_First_Rep_Item (Base, First_Rep_Item (Int_Base));
2116 Set_Digits_Value (Base, Digs_Val);
2117 Set_Delta_Value (Base, Delta_Val);
2118 Set_Small_Value (Base, Delta_Val);
2119 Set_Scalar_Range (Base,
2121 Low_Bound => Make_Dummy_Bound,
2122 High_Bound => Make_Dummy_Bound));
2124 Set_Is_Generic_Type (Base);
2125 Set_Parent (Base, Parent (Def));
2127 Set_Ekind (T, E_Decimal_Fixed_Point_Subtype);
2128 Set_Etype (T, Base);
2129 Set_Size_Info (T, Int_Base);
2130 Set_RM_Size (T, RM_Size (Int_Base));
2131 Set_First_Rep_Item (T, First_Rep_Item (Int_Base));
2132 Set_Digits_Value (T, Digs_Val);
2133 Set_Delta_Value (T, Delta_Val);
2134 Set_Small_Value (T, Delta_Val);
2135 Set_Scalar_Range (T, Scalar_Range (Base));
2136 Set_Is_Constrained (T);
2138 Check_Restriction (No_Fixed_Point, Def);
2139 end Analyze_Formal_Decimal_Fixed_Point_Type;
2141 -------------------------------------------
2142 -- Analyze_Formal_Derived_Interface_Type --
2143 -------------------------------------------
2145 procedure Analyze_Formal_Derived_Interface_Type
2150 Loc : constant Source_Ptr := Sloc (Def);
2153 -- Rewrite as a type declaration of a derived type. This ensures that
2154 -- the interface list and primitive operations are properly captured.
2157 Make_Full_Type_Declaration (Loc,
2158 Defining_Identifier => T,
2159 Type_Definition => Def));
2161 Set_Is_Generic_Type (T);
2162 end Analyze_Formal_Derived_Interface_Type;
2164 ---------------------------------
2165 -- Analyze_Formal_Derived_Type --
2166 ---------------------------------
2168 procedure Analyze_Formal_Derived_Type
2173 Loc : constant Source_Ptr := Sloc (Def);
2174 Unk_Disc : constant Boolean := Unknown_Discriminants_Present (N);
2178 Set_Is_Generic_Type (T);
2180 if Private_Present (Def) then
2182 Make_Private_Extension_Declaration (Loc,
2183 Defining_Identifier => T,
2184 Discriminant_Specifications => Discriminant_Specifications (N),
2185 Unknown_Discriminants_Present => Unk_Disc,
2186 Subtype_Indication => Subtype_Mark (Def),
2187 Interface_List => Interface_List (Def));
2189 Set_Abstract_Present (New_N, Abstract_Present (Def));
2190 Set_Limited_Present (New_N, Limited_Present (Def));
2191 Set_Synchronized_Present (New_N, Synchronized_Present (Def));
2195 Make_Full_Type_Declaration (Loc,
2196 Defining_Identifier => T,
2197 Discriminant_Specifications =>
2198 Discriminant_Specifications (Parent (T)),
2200 Make_Derived_Type_Definition (Loc,
2201 Subtype_Indication => Subtype_Mark (Def)));
2203 Set_Abstract_Present
2204 (Type_Definition (New_N), Abstract_Present (Def));
2206 (Type_Definition (New_N), Limited_Present (Def));
2213 if not Is_Composite_Type (T) then
2215 ("unknown discriminants not allowed for elementary types", N);
2217 Set_Has_Unknown_Discriminants (T);
2218 Set_Is_Constrained (T, False);
2222 -- If the parent type has a known size, so does the formal, which makes
2223 -- legal representation clauses that involve the formal.
2225 Set_Size_Known_At_Compile_Time
2226 (T, Size_Known_At_Compile_Time (Entity (Subtype_Mark (Def))));
2227 end Analyze_Formal_Derived_Type;
2229 ----------------------------------
2230 -- Analyze_Formal_Discrete_Type --
2231 ----------------------------------
2233 -- The operations defined for a discrete types are those of an enumeration
2234 -- type. The size is set to an arbitrary value, for use in analyzing the
2237 procedure Analyze_Formal_Discrete_Type (T : Entity_Id; Def : Node_Id) is
2238 Loc : constant Source_Ptr := Sloc (Def);
2242 Base : constant Entity_Id :=
2244 (E_Floating_Point_Type, Current_Scope,
2245 Sloc (Defining_Identifier (Parent (Def))), 'G');
2249 Set_Ekind (T, E_Enumeration_Subtype);
2250 Set_Etype (T, Base);
2253 Set_Is_Generic_Type (T);
2254 Set_Is_Constrained (T);
2256 -- For semantic analysis, the bounds of the type must be set to some
2257 -- non-static value. The simplest is to create attribute nodes for those
2258 -- bounds, that refer to the type itself. These bounds are never
2259 -- analyzed but serve as place-holders.
2262 Make_Attribute_Reference (Loc,
2263 Attribute_Name => Name_First,
2264 Prefix => New_Occurrence_Of (T, Loc));
2268 Make_Attribute_Reference (Loc,
2269 Attribute_Name => Name_Last,
2270 Prefix => New_Occurrence_Of (T, Loc));
2273 Set_Scalar_Range (T,
2278 Set_Ekind (Base, E_Enumeration_Type);
2279 Set_Etype (Base, Base);
2280 Init_Size (Base, 8);
2281 Init_Alignment (Base);
2282 Set_Is_Generic_Type (Base);
2283 Set_Scalar_Range (Base, Scalar_Range (T));
2284 Set_Parent (Base, Parent (Def));
2285 end Analyze_Formal_Discrete_Type;
2287 ----------------------------------
2288 -- Analyze_Formal_Floating_Type --
2289 ---------------------------------
2291 procedure Analyze_Formal_Floating_Type (T : Entity_Id; Def : Node_Id) is
2292 Base : constant Entity_Id :=
2294 (E_Floating_Point_Type, Current_Scope,
2295 Sloc (Defining_Identifier (Parent (Def))), 'G');
2298 -- The various semantic attributes are taken from the predefined type
2299 -- Float, just so that all of them are initialized. Their values are
2300 -- never used because no constant folding or expansion takes place in
2301 -- the generic itself.
2304 Set_Ekind (T, E_Floating_Point_Subtype);
2305 Set_Etype (T, Base);
2306 Set_Size_Info (T, (Standard_Float));
2307 Set_RM_Size (T, RM_Size (Standard_Float));
2308 Set_Digits_Value (T, Digits_Value (Standard_Float));
2309 Set_Scalar_Range (T, Scalar_Range (Standard_Float));
2310 Set_Is_Constrained (T);
2312 Set_Is_Generic_Type (Base);
2313 Set_Etype (Base, Base);
2314 Set_Size_Info (Base, (Standard_Float));
2315 Set_RM_Size (Base, RM_Size (Standard_Float));
2316 Set_Digits_Value (Base, Digits_Value (Standard_Float));
2317 Set_Scalar_Range (Base, Scalar_Range (Standard_Float));
2318 Set_Parent (Base, Parent (Def));
2320 Check_Restriction (No_Floating_Point, Def);
2321 end Analyze_Formal_Floating_Type;
2323 -----------------------------------
2324 -- Analyze_Formal_Interface_Type;--
2325 -----------------------------------
2327 procedure Analyze_Formal_Interface_Type
2332 Loc : constant Source_Ptr := Sloc (N);
2337 Make_Full_Type_Declaration (Loc,
2338 Defining_Identifier => T,
2339 Type_Definition => Def);
2343 Set_Is_Generic_Type (T);
2344 end Analyze_Formal_Interface_Type;
2346 ---------------------------------
2347 -- Analyze_Formal_Modular_Type --
2348 ---------------------------------
2350 procedure Analyze_Formal_Modular_Type (T : Entity_Id; Def : Node_Id) is
2352 -- Apart from their entity kind, generic modular types are treated like
2353 -- signed integer types, and have the same attributes.
2355 Analyze_Formal_Signed_Integer_Type (T, Def);
2356 Set_Ekind (T, E_Modular_Integer_Subtype);
2357 Set_Ekind (Etype (T), E_Modular_Integer_Type);
2359 end Analyze_Formal_Modular_Type;
2361 ---------------------------------------
2362 -- Analyze_Formal_Object_Declaration --
2363 ---------------------------------------
2365 procedure Analyze_Formal_Object_Declaration (N : Node_Id) is
2366 E : constant Node_Id := Default_Expression (N);
2367 Id : constant Node_Id := Defining_Identifier (N);
2374 -- Determine the mode of the formal object
2376 if Out_Present (N) then
2377 K := E_Generic_In_Out_Parameter;
2379 if not In_Present (N) then
2380 Error_Msg_N ("formal generic objects cannot have mode OUT", N);
2384 K := E_Generic_In_Parameter;
2387 if Present (Subtype_Mark (N)) then
2388 Find_Type (Subtype_Mark (N));
2389 T := Entity (Subtype_Mark (N));
2391 -- Verify that there is no redundant null exclusion
2393 if Null_Exclusion_Present (N) then
2394 if not Is_Access_Type (T) then
2396 ("null exclusion can only apply to an access type", N);
2398 elsif Can_Never_Be_Null (T) then
2400 ("`NOT NULL` not allowed (& already excludes null)",
2405 -- Ada 2005 (AI-423): Formal object with an access definition
2408 Check_Access_Definition (N);
2409 T := Access_Definition
2411 N => Access_Definition (N));
2414 if Ekind (T) = E_Incomplete_Type then
2416 Error_Node : Node_Id;
2419 if Present (Subtype_Mark (N)) then
2420 Error_Node := Subtype_Mark (N);
2422 Check_Access_Definition (N);
2423 Error_Node := Access_Definition (N);
2426 Error_Msg_N ("premature usage of incomplete type", Error_Node);
2430 if K = E_Generic_In_Parameter then
2432 -- Ada 2005 (AI-287): Limited aggregates allowed in generic formals
2434 if Ada_Version < Ada_2005 and then Is_Limited_Type (T) then
2436 ("generic formal of mode IN must not be of limited type", N);
2437 Explain_Limited_Type (T, N);
2440 if Is_Abstract_Type (T) then
2442 ("generic formal of mode IN must not be of abstract type", N);
2446 Preanalyze_Spec_Expression (E, T);
2448 if Is_Limited_Type (T) and then not OK_For_Limited_Init (T, E) then
2450 ("initialization not allowed for limited types", E);
2451 Explain_Limited_Type (T, E);
2458 -- Case of generic IN OUT parameter
2461 -- If the formal has an unconstrained type, construct its actual
2462 -- subtype, as is done for subprogram formals. In this fashion, all
2463 -- its uses can refer to specific bounds.
2468 if (Is_Array_Type (T)
2469 and then not Is_Constrained (T))
2471 (Ekind (T) = E_Record_Type
2472 and then Has_Discriminants (T))
2475 Non_Freezing_Ref : constant Node_Id :=
2476 New_Occurrence_Of (Id, Sloc (Id));
2480 -- Make sure the actual subtype doesn't generate bogus freezing
2482 Set_Must_Not_Freeze (Non_Freezing_Ref);
2483 Decl := Build_Actual_Subtype (T, Non_Freezing_Ref);
2484 Insert_Before_And_Analyze (N, Decl);
2485 Set_Actual_Subtype (Id, Defining_Identifier (Decl));
2488 Set_Actual_Subtype (Id, T);
2493 ("initialization not allowed for `IN OUT` formals", N);
2497 if Has_Aspects (N) then
2498 Analyze_Aspect_Specifications (N, Id);
2500 end Analyze_Formal_Object_Declaration;
2502 ----------------------------------------------
2503 -- Analyze_Formal_Ordinary_Fixed_Point_Type --
2504 ----------------------------------------------
2506 procedure Analyze_Formal_Ordinary_Fixed_Point_Type
2510 Loc : constant Source_Ptr := Sloc (Def);
2511 Base : constant Entity_Id :=
2513 (E_Ordinary_Fixed_Point_Type, Current_Scope,
2514 Sloc (Defining_Identifier (Parent (Def))), 'G');
2517 -- The semantic attributes are set for completeness only, their values
2518 -- will never be used, since all properties of the type are non-static.
2521 Set_Ekind (T, E_Ordinary_Fixed_Point_Subtype);
2522 Set_Etype (T, Base);
2523 Set_Size_Info (T, Standard_Integer);
2524 Set_RM_Size (T, RM_Size (Standard_Integer));
2525 Set_Small_Value (T, Ureal_1);
2526 Set_Delta_Value (T, Ureal_1);
2527 Set_Scalar_Range (T,
2529 Low_Bound => Make_Real_Literal (Loc, Ureal_1),
2530 High_Bound => Make_Real_Literal (Loc, Ureal_1)));
2531 Set_Is_Constrained (T);
2533 Set_Is_Generic_Type (Base);
2534 Set_Etype (Base, Base);
2535 Set_Size_Info (Base, Standard_Integer);
2536 Set_RM_Size (Base, RM_Size (Standard_Integer));
2537 Set_Small_Value (Base, Ureal_1);
2538 Set_Delta_Value (Base, Ureal_1);
2539 Set_Scalar_Range (Base, Scalar_Range (T));
2540 Set_Parent (Base, Parent (Def));
2542 Check_Restriction (No_Fixed_Point, Def);
2543 end Analyze_Formal_Ordinary_Fixed_Point_Type;
2545 ----------------------------------------
2546 -- Analyze_Formal_Package_Declaration --
2547 ----------------------------------------
2549 procedure Analyze_Formal_Package_Declaration (N : Node_Id) is
2550 Loc : constant Source_Ptr := Sloc (N);
2551 Pack_Id : constant Entity_Id := Defining_Identifier (N);
2553 Gen_Id : constant Node_Id := Name (N);
2555 Gen_Unit : Entity_Id;
2557 Parent_Installed : Boolean := False;
2559 Parent_Instance : Entity_Id;
2560 Renaming_In_Par : Entity_Id;
2561 Associations : Boolean := True;
2563 Vis_Prims_List : Elist_Id := No_Elist;
2564 -- List of primitives made temporarily visible in the instantiation
2565 -- to match the visibility of the formal type
2567 function Build_Local_Package return Node_Id;
2568 -- The formal package is rewritten so that its parameters are replaced
2569 -- with corresponding declarations. For parameters with bona fide
2570 -- associations these declarations are created by Analyze_Associations
2571 -- as for a regular instantiation. For boxed parameters, we preserve
2572 -- the formal declarations and analyze them, in order to introduce
2573 -- entities of the right kind in the environment of the formal.
2575 -------------------------
2576 -- Build_Local_Package --
2577 -------------------------
2579 function Build_Local_Package return Node_Id is
2581 Pack_Decl : Node_Id;
2584 -- Within the formal, the name of the generic package is a renaming
2585 -- of the formal (as for a regular instantiation).
2588 Make_Package_Declaration (Loc,
2591 (Specification (Original_Node (Gen_Decl)),
2592 Empty, Instantiating => True));
2594 Renaming := Make_Package_Renaming_Declaration (Loc,
2595 Defining_Unit_Name =>
2596 Make_Defining_Identifier (Loc, Chars (Gen_Unit)),
2597 Name => New_Occurrence_Of (Formal, Loc));
2599 if Nkind (Gen_Id) = N_Identifier
2600 and then Chars (Gen_Id) = Chars (Pack_Id)
2603 ("& is hidden within declaration of instance", Gen_Id, Gen_Unit);
2606 -- If the formal is declared with a box, or with an others choice,
2607 -- create corresponding declarations for all entities in the formal
2608 -- part, so that names with the proper types are available in the
2609 -- specification of the formal package.
2611 -- On the other hand, if there are no associations, then all the
2612 -- formals must have defaults, and this will be checked by the
2613 -- call to Analyze_Associations.
2616 or else Nkind (First (Generic_Associations (N))) = N_Others_Choice
2619 Formal_Decl : Node_Id;
2622 -- TBA : for a formal package, need to recurse ???
2627 (Generic_Formal_Declarations (Original_Node (Gen_Decl)));
2628 while Present (Formal_Decl) loop
2630 (Decls, Copy_Generic_Node (Formal_Decl, Empty, True));
2635 -- If generic associations are present, use Analyze_Associations to
2636 -- create the proper renaming declarations.
2640 Act_Tree : constant Node_Id :=
2642 (Original_Node (Gen_Decl), Empty,
2643 Instantiating => True);
2646 Generic_Renamings.Set_Last (0);
2647 Generic_Renamings_HTable.Reset;
2648 Instantiation_Node := N;
2651 Analyze_Associations
2652 (I_Node => Original_Node (N),
2653 Formals => Generic_Formal_Declarations (Act_Tree),
2654 F_Copy => Generic_Formal_Declarations (Gen_Decl));
2656 Vis_Prims_List := Check_Hidden_Primitives (Decls);
2660 Append (Renaming, To => Decls);
2662 -- Add generated declarations ahead of local declarations in
2665 if No (Visible_Declarations (Specification (Pack_Decl))) then
2666 Set_Visible_Declarations (Specification (Pack_Decl), Decls);
2669 (First (Visible_Declarations (Specification (Pack_Decl))),
2674 end Build_Local_Package;
2676 -- Start of processing for Analyze_Formal_Package_Declaration
2679 Check_Text_IO_Special_Unit (Gen_Id);
2682 Check_Generic_Child_Unit (Gen_Id, Parent_Installed);
2683 Gen_Unit := Entity (Gen_Id);
2685 -- Check for a formal package that is a package renaming
2687 if Present (Renamed_Object (Gen_Unit)) then
2689 -- Indicate that unit is used, before replacing it with renamed
2690 -- entity for use below.
2692 if In_Extended_Main_Source_Unit (N) then
2693 Set_Is_Instantiated (Gen_Unit);
2694 Generate_Reference (Gen_Unit, N);
2697 Gen_Unit := Renamed_Object (Gen_Unit);
2700 if Ekind (Gen_Unit) /= E_Generic_Package then
2701 Error_Msg_N ("expect generic package name", Gen_Id);
2705 elsif Gen_Unit = Current_Scope then
2707 ("generic package cannot be used as a formal package of itself",
2712 elsif In_Open_Scopes (Gen_Unit) then
2713 if Is_Compilation_Unit (Gen_Unit)
2714 and then Is_Child_Unit (Current_Scope)
2716 -- Special-case the error when the formal is a parent, and
2717 -- continue analysis to minimize cascaded errors.
2720 ("generic parent cannot be used as formal package "
2721 & "of a child unit",
2726 ("generic package cannot be used as a formal package "
2734 -- Check that name of formal package does not hide name of generic,
2735 -- or its leading prefix. This check must be done separately because
2736 -- the name of the generic has already been analyzed.
2739 Gen_Name : Entity_Id;
2743 while Nkind (Gen_Name) = N_Expanded_Name loop
2744 Gen_Name := Prefix (Gen_Name);
2747 if Chars (Gen_Name) = Chars (Pack_Id) then
2749 ("& is hidden within declaration of formal package",
2755 or else No (Generic_Associations (N))
2756 or else Nkind (First (Generic_Associations (N))) = N_Others_Choice
2758 Associations := False;
2761 -- If there are no generic associations, the generic parameters appear
2762 -- as local entities and are instantiated like them. We copy the generic
2763 -- package declaration as if it were an instantiation, and analyze it
2764 -- like a regular package, except that we treat the formals as
2765 -- additional visible components.
2767 Gen_Decl := Unit_Declaration_Node (Gen_Unit);
2769 if In_Extended_Main_Source_Unit (N) then
2770 Set_Is_Instantiated (Gen_Unit);
2771 Generate_Reference (Gen_Unit, N);
2774 Formal := New_Copy (Pack_Id);
2775 Create_Instantiation_Source (N, Gen_Unit, False, S_Adjustment);
2778 -- Make local generic without formals. The formals will be replaced
2779 -- with internal declarations.
2781 New_N := Build_Local_Package;
2783 -- If there are errors in the parameter list, Analyze_Associations
2784 -- raises Instantiation_Error. Patch the declaration to prevent
2785 -- further exception propagation.
2788 when Instantiation_Error =>
2790 Enter_Name (Formal);
2791 Set_Ekind (Formal, E_Variable);
2792 Set_Etype (Formal, Any_Type);
2793 Restore_Hidden_Primitives (Vis_Prims_List);
2795 if Parent_Installed then
2803 Set_Defining_Unit_Name (Specification (New_N), Formal);
2804 Set_Generic_Parent (Specification (N), Gen_Unit);
2805 Set_Instance_Env (Gen_Unit, Formal);
2806 Set_Is_Generic_Instance (Formal);
2808 Enter_Name (Formal);
2809 Set_Ekind (Formal, E_Package);
2810 Set_Etype (Formal, Standard_Void_Type);
2811 Set_Inner_Instances (Formal, New_Elmt_List);
2812 Push_Scope (Formal);
2814 if Is_Child_Unit (Gen_Unit)
2815 and then Parent_Installed
2817 -- Similarly, we have to make the name of the formal visible in the
2818 -- parent instance, to resolve properly fully qualified names that
2819 -- may appear in the generic unit. The parent instance has been
2820 -- placed on the scope stack ahead of the current scope.
2822 Parent_Instance := Scope_Stack.Table (Scope_Stack.Last - 1).Entity;
2825 Make_Defining_Identifier (Loc, Chars (Gen_Unit));
2826 Set_Ekind (Renaming_In_Par, E_Package);
2827 Set_Etype (Renaming_In_Par, Standard_Void_Type);
2828 Set_Scope (Renaming_In_Par, Parent_Instance);
2829 Set_Parent (Renaming_In_Par, Parent (Formal));
2830 Set_Renamed_Object (Renaming_In_Par, Formal);
2831 Append_Entity (Renaming_In_Par, Parent_Instance);
2834 Analyze (Specification (N));
2836 -- The formals for which associations are provided are not visible
2837 -- outside of the formal package. The others are still declared by a
2838 -- formal parameter declaration.
2840 -- If there are no associations, the only local entity to hide is the
2841 -- generated package renaming itself.
2847 E := First_Entity (Formal);
2848 while Present (E) loop
2850 and then not Is_Generic_Formal (E)
2855 if Ekind (E) = E_Package
2856 and then Renamed_Entity (E) = Formal
2866 End_Package_Scope (Formal);
2867 Restore_Hidden_Primitives (Vis_Prims_List);
2869 if Parent_Installed then
2875 -- Inside the generic unit, the formal package is a regular package, but
2876 -- no body is needed for it. Note that after instantiation, the defining
2877 -- unit name we need is in the new tree and not in the original (see
2878 -- Package_Instantiation). A generic formal package is an instance, and
2879 -- can be used as an actual for an inner instance.
2881 Set_Has_Completion (Formal, True);
2883 -- Add semantic information to the original defining identifier.
2886 Set_Ekind (Pack_Id, E_Package);
2887 Set_Etype (Pack_Id, Standard_Void_Type);
2888 Set_Scope (Pack_Id, Scope (Formal));
2889 Set_Has_Completion (Pack_Id, True);
2892 if Has_Aspects (N) then
2893 Analyze_Aspect_Specifications (N, Pack_Id);
2895 end Analyze_Formal_Package_Declaration;
2897 ---------------------------------
2898 -- Analyze_Formal_Private_Type --
2899 ---------------------------------
2901 procedure Analyze_Formal_Private_Type
2907 New_Private_Type (N, T, Def);
2909 -- Set the size to an arbitrary but legal value
2911 Set_Size_Info (T, Standard_Integer);
2912 Set_RM_Size (T, RM_Size (Standard_Integer));
2913 end Analyze_Formal_Private_Type;
2915 ------------------------------------
2916 -- Analyze_Formal_Incomplete_Type --
2917 ------------------------------------
2919 procedure Analyze_Formal_Incomplete_Type
2925 Set_Ekind (T, E_Incomplete_Type);
2927 Set_Private_Dependents (T, New_Elmt_List);
2929 if Tagged_Present (Def) then
2930 Set_Is_Tagged_Type (T);
2931 Make_Class_Wide_Type (T);
2932 Set_Direct_Primitive_Operations (T, New_Elmt_List);
2934 end Analyze_Formal_Incomplete_Type;
2936 ----------------------------------------
2937 -- Analyze_Formal_Signed_Integer_Type --
2938 ----------------------------------------
2940 procedure Analyze_Formal_Signed_Integer_Type
2944 Base : constant Entity_Id :=
2946 (E_Signed_Integer_Type,
2948 Sloc (Defining_Identifier (Parent (Def))), 'G');
2953 Set_Ekind (T, E_Signed_Integer_Subtype);
2954 Set_Etype (T, Base);
2955 Set_Size_Info (T, Standard_Integer);
2956 Set_RM_Size (T, RM_Size (Standard_Integer));
2957 Set_Scalar_Range (T, Scalar_Range (Standard_Integer));
2958 Set_Is_Constrained (T);
2960 Set_Is_Generic_Type (Base);
2961 Set_Size_Info (Base, Standard_Integer);
2962 Set_RM_Size (Base, RM_Size (Standard_Integer));
2963 Set_Etype (Base, Base);
2964 Set_Scalar_Range (Base, Scalar_Range (Standard_Integer));
2965 Set_Parent (Base, Parent (Def));
2966 end Analyze_Formal_Signed_Integer_Type;
2968 -------------------------------------------
2969 -- Analyze_Formal_Subprogram_Declaration --
2970 -------------------------------------------
2972 procedure Analyze_Formal_Subprogram_Declaration (N : Node_Id) is
2973 Spec : constant Node_Id := Specification (N);
2974 Def : constant Node_Id := Default_Name (N);
2975 Nam : constant Entity_Id := Defining_Unit_Name (Spec);
2983 if Nkind (Nam) = N_Defining_Program_Unit_Name then
2984 Error_Msg_N ("name of formal subprogram must be a direct name", Nam);
2988 Analyze_Subprogram_Declaration (N);
2989 Set_Is_Formal_Subprogram (Nam);
2990 Set_Has_Completion (Nam);
2992 if Nkind (N) = N_Formal_Abstract_Subprogram_Declaration then
2993 Set_Is_Abstract_Subprogram (Nam);
2994 Set_Is_Dispatching_Operation (Nam);
2997 Ctrl_Type : constant Entity_Id := Find_Dispatching_Type (Nam);
2999 if No (Ctrl_Type) then
3001 ("abstract formal subprogram must have a controlling type",
3004 elsif Ada_Version >= Ada_2012
3005 and then Is_Incomplete_Type (Ctrl_Type)
3008 ("controlling type of abstract formal subprogram cannot " &
3009 "be incomplete type", N, Ctrl_Type);
3012 Check_Controlling_Formals (Ctrl_Type, Nam);
3017 -- Default name is resolved at the point of instantiation
3019 if Box_Present (N) then
3022 -- Else default is bound at the point of generic declaration
3024 elsif Present (Def) then
3025 if Nkind (Def) = N_Operator_Symbol then
3026 Find_Direct_Name (Def);
3028 elsif Nkind (Def) /= N_Attribute_Reference then
3032 -- For an attribute reference, analyze the prefix and verify
3033 -- that it has the proper profile for the subprogram.
3035 Analyze (Prefix (Def));
3036 Valid_Default_Attribute (Nam, Def);
3040 -- Default name may be overloaded, in which case the interpretation
3041 -- with the correct profile must be selected, as for a renaming.
3042 -- If the definition is an indexed component, it must denote a
3043 -- member of an entry family. If it is a selected component, it
3044 -- can be a protected operation.
3046 if Etype (Def) = Any_Type then
3049 elsif Nkind (Def) = N_Selected_Component then
3050 if not Is_Overloadable (Entity (Selector_Name (Def))) then
3051 Error_Msg_N ("expect valid subprogram name as default", Def);
3054 elsif Nkind (Def) = N_Indexed_Component then
3055 if Is_Entity_Name (Prefix (Def)) then
3056 if Ekind (Entity (Prefix (Def))) /= E_Entry_Family then
3057 Error_Msg_N ("expect valid subprogram name as default", Def);
3060 elsif Nkind (Prefix (Def)) = N_Selected_Component then
3061 if Ekind (Entity (Selector_Name (Prefix (Def)))) /=
3064 Error_Msg_N ("expect valid subprogram name as default", Def);
3068 Error_Msg_N ("expect valid subprogram name as default", Def);
3072 elsif Nkind (Def) = N_Character_Literal then
3074 -- Needs some type checks: subprogram should be parameterless???
3076 Resolve (Def, (Etype (Nam)));
3078 elsif not Is_Entity_Name (Def)
3079 or else not Is_Overloadable (Entity (Def))
3081 Error_Msg_N ("expect valid subprogram name as default", Def);
3084 elsif not Is_Overloaded (Def) then
3085 Subp := Entity (Def);
3088 Error_Msg_N ("premature usage of formal subprogram", Def);
3090 elsif not Entity_Matches_Spec (Subp, Nam) then
3091 Error_Msg_N ("no visible entity matches specification", Def);
3094 -- More than one interpretation, so disambiguate as for a renaming
3099 I1 : Interp_Index := 0;
3105 Get_First_Interp (Def, I, It);
3106 while Present (It.Nam) loop
3107 if Entity_Matches_Spec (It.Nam, Nam) then
3108 if Subp /= Any_Id then
3109 It1 := Disambiguate (Def, I1, I, Etype (Subp));
3111 if It1 = No_Interp then
3112 Error_Msg_N ("ambiguous default subprogram", Def);
3125 Get_Next_Interp (I, It);
3129 if Subp /= Any_Id then
3131 -- Subprogram found, generate reference to it
3133 Set_Entity (Def, Subp);
3134 Generate_Reference (Subp, Def);
3137 Error_Msg_N ("premature usage of formal subprogram", Def);
3139 elsif Ekind (Subp) /= E_Operator then
3140 Check_Mode_Conformant (Subp, Nam);
3144 Error_Msg_N ("no visible subprogram matches specification", N);
3150 if Has_Aspects (N) then
3151 Analyze_Aspect_Specifications (N, Nam);
3154 end Analyze_Formal_Subprogram_Declaration;
3156 -------------------------------------
3157 -- Analyze_Formal_Type_Declaration --
3158 -------------------------------------
3160 procedure Analyze_Formal_Type_Declaration (N : Node_Id) is
3161 Def : constant Node_Id := Formal_Type_Definition (N);
3165 T := Defining_Identifier (N);
3167 if Present (Discriminant_Specifications (N))
3168 and then Nkind (Def) /= N_Formal_Private_Type_Definition
3171 ("discriminants not allowed for this formal type", T);
3174 -- Enter the new name, and branch to specific routine
3177 when N_Formal_Private_Type_Definition =>
3178 Analyze_Formal_Private_Type (N, T, Def);
3180 when N_Formal_Derived_Type_Definition =>
3181 Analyze_Formal_Derived_Type (N, T, Def);
3183 when N_Formal_Incomplete_Type_Definition =>
3184 Analyze_Formal_Incomplete_Type (T, Def);
3186 when N_Formal_Discrete_Type_Definition =>
3187 Analyze_Formal_Discrete_Type (T, Def);
3189 when N_Formal_Signed_Integer_Type_Definition =>
3190 Analyze_Formal_Signed_Integer_Type (T, Def);
3192 when N_Formal_Modular_Type_Definition =>
3193 Analyze_Formal_Modular_Type (T, Def);
3195 when N_Formal_Floating_Point_Definition =>
3196 Analyze_Formal_Floating_Type (T, Def);
3198 when N_Formal_Ordinary_Fixed_Point_Definition =>
3199 Analyze_Formal_Ordinary_Fixed_Point_Type (T, Def);
3201 when N_Formal_Decimal_Fixed_Point_Definition =>
3202 Analyze_Formal_Decimal_Fixed_Point_Type (T, Def);
3204 when N_Array_Type_Definition =>
3205 Analyze_Formal_Array_Type (T, Def);
3207 when N_Access_To_Object_Definition |
3208 N_Access_Function_Definition |
3209 N_Access_Procedure_Definition =>
3210 Analyze_Generic_Access_Type (T, Def);
3212 -- Ada 2005: a interface declaration is encoded as an abstract
3213 -- record declaration or a abstract type derivation.
3215 when N_Record_Definition =>
3216 Analyze_Formal_Interface_Type (N, T, Def);
3218 when N_Derived_Type_Definition =>
3219 Analyze_Formal_Derived_Interface_Type (N, T, Def);
3225 raise Program_Error;
3229 Set_Is_Generic_Type (T);
3231 if Has_Aspects (N) then
3232 Analyze_Aspect_Specifications (N, T);
3234 end Analyze_Formal_Type_Declaration;
3236 ------------------------------------
3237 -- Analyze_Function_Instantiation --
3238 ------------------------------------
3240 procedure Analyze_Function_Instantiation (N : Node_Id) is
3242 Analyze_Subprogram_Instantiation (N, E_Function);
3243 end Analyze_Function_Instantiation;
3245 ---------------------------------
3246 -- Analyze_Generic_Access_Type --
3247 ---------------------------------
3249 procedure Analyze_Generic_Access_Type (T : Entity_Id; Def : Node_Id) is
3253 if Nkind (Def) = N_Access_To_Object_Definition then
3254 Access_Type_Declaration (T, Def);
3256 if Is_Incomplete_Or_Private_Type (Designated_Type (T))
3257 and then No (Full_View (Designated_Type (T)))
3258 and then not Is_Generic_Type (Designated_Type (T))
3260 Error_Msg_N ("premature usage of incomplete type", Def);
3262 elsif not Is_Entity_Name (Subtype_Indication (Def)) then
3264 ("only a subtype mark is allowed in a formal", Def);
3268 Access_Subprogram_Declaration (T, Def);
3270 end Analyze_Generic_Access_Type;
3272 ---------------------------------
3273 -- Analyze_Generic_Formal_Part --
3274 ---------------------------------
3276 procedure Analyze_Generic_Formal_Part (N : Node_Id) is
3277 Gen_Parm_Decl : Node_Id;
3280 -- The generic formals are processed in the scope of the generic unit,
3281 -- where they are immediately visible. The scope is installed by the
3284 Gen_Parm_Decl := First (Generic_Formal_Declarations (N));
3286 while Present (Gen_Parm_Decl) loop
3287 Analyze (Gen_Parm_Decl);
3288 Next (Gen_Parm_Decl);
3291 Generate_Reference_To_Generic_Formals (Current_Scope);
3292 end Analyze_Generic_Formal_Part;
3294 ------------------------------------------
3295 -- Analyze_Generic_Package_Declaration --
3296 ------------------------------------------
3298 procedure Analyze_Generic_Package_Declaration (N : Node_Id) is
3299 Loc : constant Source_Ptr := Sloc (N);
3302 Save_Parent : Node_Id;
3304 Decls : constant List_Id :=
3305 Visible_Declarations (Specification (N));
3309 Check_SPARK_05_Restriction ("generic is not allowed", N);
3311 -- We introduce a renaming of the enclosing package, to have a usable
3312 -- entity as the prefix of an expanded name for a local entity of the
3313 -- form Par.P.Q, where P is the generic package. This is because a local
3314 -- entity named P may hide it, so that the usual visibility rules in
3315 -- the instance will not resolve properly.
3318 Make_Package_Renaming_Declaration (Loc,
3319 Defining_Unit_Name =>
3320 Make_Defining_Identifier (Loc,
3321 Chars => New_External_Name (Chars (Defining_Entity (N)), "GH")),
3322 Name => Make_Identifier (Loc, Chars (Defining_Entity (N))));
3324 if Present (Decls) then
3325 Decl := First (Decls);
3326 while Present (Decl)
3327 and then Nkind (Decl) = N_Pragma
3332 if Present (Decl) then
3333 Insert_Before (Decl, Renaming);
3335 Append (Renaming, Visible_Declarations (Specification (N)));
3339 Set_Visible_Declarations (Specification (N), New_List (Renaming));
3342 -- Create copy of generic unit, and save for instantiation. If the unit
3343 -- is a child unit, do not copy the specifications for the parent, which
3344 -- are not part of the generic tree.
3346 Save_Parent := Parent_Spec (N);
3347 Set_Parent_Spec (N, Empty);
3349 New_N := Copy_Generic_Node (N, Empty, Instantiating => False);
3350 Set_Parent_Spec (New_N, Save_Parent);
3353 -- Once the contents of the generic copy and the template are swapped,
3354 -- do the same for their respective aspect specifications.
3356 Exchange_Aspects (N, New_N);
3357 Id := Defining_Entity (N);
3358 Generate_Definition (Id);
3360 -- Expansion is not applied to generic units
3365 Set_Ekind (Id, E_Generic_Package);
3366 Set_Etype (Id, Standard_Void_Type);
3367 Set_Contract (Id, Make_Contract (Sloc (Id)));
3369 -- Analyze aspects now, so that generated pragmas appear in the
3370 -- declarations before building and analyzing the generic copy.
3372 if Has_Aspects (N) then
3373 Analyze_Aspect_Specifications (N, Id);
3377 Enter_Generic_Scope (Id);
3378 Set_Inner_Instances (Id, New_Elmt_List);
3380 Set_Categorization_From_Pragmas (N);
3381 Set_Is_Pure (Id, Is_Pure (Current_Scope));
3383 -- Link the declaration of the generic homonym in the generic copy to
3384 -- the package it renames, so that it is always resolved properly.
3386 Set_Generic_Homonym (Id, Defining_Unit_Name (Renaming));
3387 Set_Entity (Associated_Node (Name (Renaming)), Id);
3389 -- For a library unit, we have reconstructed the entity for the unit,
3390 -- and must reset it in the library tables.
3392 if Nkind (Parent (N)) = N_Compilation_Unit then
3393 Set_Cunit_Entity (Current_Sem_Unit, Id);
3396 Analyze_Generic_Formal_Part (N);
3398 -- After processing the generic formals, analysis proceeds as for a
3399 -- non-generic package.
3401 Analyze (Specification (N));
3403 Validate_Categorization_Dependency (N, Id);
3407 End_Package_Scope (Id);
3408 Exit_Generic_Scope (Id);
3410 if Nkind (Parent (N)) /= N_Compilation_Unit then
3411 Move_Freeze_Nodes (Id, N, Visible_Declarations (Specification (N)));
3412 Move_Freeze_Nodes (Id, N, Private_Declarations (Specification (N)));
3413 Move_Freeze_Nodes (Id, N, Generic_Formal_Declarations (N));
3416 Set_Body_Required (Parent (N), Unit_Requires_Body (Id));
3417 Validate_RT_RAT_Component (N);
3419 -- If this is a spec without a body, check that generic parameters
3422 if not Body_Required (Parent (N)) then
3423 Check_References (Id);
3426 end Analyze_Generic_Package_Declaration;
3428 --------------------------------------------
3429 -- Analyze_Generic_Subprogram_Declaration --
3430 --------------------------------------------
3432 procedure Analyze_Generic_Subprogram_Declaration (N : Node_Id) is
3437 Result_Type : Entity_Id;
3438 Save_Parent : Node_Id;
3442 Check_SPARK_05_Restriction ("generic is not allowed", N);
3444 -- Create copy of generic unit, and save for instantiation. If the unit
3445 -- is a child unit, do not copy the specifications for the parent, which
3446 -- are not part of the generic tree.
3448 Save_Parent := Parent_Spec (N);
3449 Set_Parent_Spec (N, Empty);
3451 New_N := Copy_Generic_Node (N, Empty, Instantiating => False);
3452 Set_Parent_Spec (New_N, Save_Parent);
3455 -- Once the contents of the generic copy and the template are swapped,
3456 -- do the same for their respective aspect specifications.
3458 Exchange_Aspects (N, New_N);
3460 Spec := Specification (N);
3461 Id := Defining_Entity (Spec);
3462 Generate_Definition (Id);
3463 Set_Contract (Id, Make_Contract (Sloc (Id)));
3465 if Nkind (Id) = N_Defining_Operator_Symbol then
3467 ("operator symbol not allowed for generic subprogram", Id);
3473 Set_Scope_Depth_Value (Id, Scope_Depth (Current_Scope) + 1);
3475 -- Analyze the aspects of the generic copy to ensure that all generated
3476 -- pragmas (if any) perform their semantic effects.
3478 if Has_Aspects (N) then
3479 Analyze_Aspect_Specifications (N, Id);
3483 Enter_Generic_Scope (Id);
3484 Set_Inner_Instances (Id, New_Elmt_List);
3485 Set_Is_Pure (Id, Is_Pure (Current_Scope));
3487 Analyze_Generic_Formal_Part (N);
3489 Formals := Parameter_Specifications (Spec);
3491 if Present (Formals) then
3492 Process_Formals (Formals, Spec);
3495 if Nkind (Spec) = N_Function_Specification then
3496 Set_Ekind (Id, E_Generic_Function);
3498 if Nkind (Result_Definition (Spec)) = N_Access_Definition then
3499 Result_Type := Access_Definition (Spec, Result_Definition (Spec));
3500 Set_Etype (Id, Result_Type);
3502 -- Check restriction imposed by AI05-073: a generic function
3503 -- cannot return an abstract type or an access to such.
3505 -- This is a binding interpretation should it apply to earlier
3506 -- versions of Ada as well as Ada 2012???
3508 if Is_Abstract_Type (Designated_Type (Result_Type))
3509 and then Ada_Version >= Ada_2012
3511 Error_Msg_N ("generic function cannot have an access result"
3512 & " that designates an abstract type", Spec);
3516 Find_Type (Result_Definition (Spec));
3517 Typ := Entity (Result_Definition (Spec));
3519 if Is_Abstract_Type (Typ)
3520 and then Ada_Version >= Ada_2012
3523 ("generic function cannot have abstract result type", Spec);
3526 -- If a null exclusion is imposed on the result type, then create
3527 -- a null-excluding itype (an access subtype) and use it as the
3528 -- function's Etype.
3530 if Is_Access_Type (Typ)
3531 and then Null_Exclusion_Present (Spec)
3534 Create_Null_Excluding_Itype
3536 Related_Nod => Spec,
3537 Scope_Id => Defining_Unit_Name (Spec)));
3539 Set_Etype (Id, Typ);
3544 Set_Ekind (Id, E_Generic_Procedure);
3545 Set_Etype (Id, Standard_Void_Type);
3548 -- For a library unit, we have reconstructed the entity for the unit,
3549 -- and must reset it in the library tables. We also make sure that
3550 -- Body_Required is set properly in the original compilation unit node.
3552 if Nkind (Parent (N)) = N_Compilation_Unit then
3553 Set_Cunit_Entity (Current_Sem_Unit, Id);
3554 Set_Body_Required (Parent (N), Unit_Requires_Body (Id));
3557 Set_Categorization_From_Pragmas (N);
3558 Validate_Categorization_Dependency (N, Id);
3560 Save_Global_References (Original_Node (N));
3562 -- For ASIS purposes, convert any postcondition, precondition pragmas
3563 -- into aspects, if N is not a compilation unit by itself, in order to
3564 -- enable the analysis of expressions inside the corresponding PPC
3567 if ASIS_Mode and then Is_List_Member (N) then
3568 Make_Aspect_For_PPC_In_Gen_Sub_Decl (N);
3573 Exit_Generic_Scope (Id);
3574 Generate_Reference_To_Formals (Id);
3576 List_Inherited_Pre_Post_Aspects (Id);
3577 end Analyze_Generic_Subprogram_Declaration;
3579 -----------------------------------
3580 -- Analyze_Package_Instantiation --
3581 -----------------------------------
3583 procedure Analyze_Package_Instantiation (N : Node_Id) is
3584 Loc : constant Source_Ptr := Sloc (N);
3585 Gen_Id : constant Node_Id := Name (N);
3588 Act_Decl_Name : Node_Id;
3589 Act_Decl_Id : Entity_Id;
3594 Gen_Unit : Entity_Id;
3596 Is_Actual_Pack : constant Boolean :=
3597 Is_Internal (Defining_Entity (N));
3599 Env_Installed : Boolean := False;
3600 Parent_Installed : Boolean := False;
3601 Renaming_List : List_Id;
3602 Unit_Renaming : Node_Id;
3603 Needs_Body : Boolean;
3604 Inline_Now : Boolean := False;
3606 Save_IPSM : constant Boolean := Ignore_Pragma_SPARK_Mode;
3607 -- Save flag Ignore_Pragma_SPARK_Mode for restore on exit
3609 Save_SM : constant SPARK_Mode_Type := SPARK_Mode;
3610 Save_SMP : constant Node_Id := SPARK_Mode_Pragma;
3611 -- Save the SPARK_Mode-related data for restore on exit
3613 Save_Style_Check : constant Boolean := Style_Check;
3614 -- Save style check mode for restore on exit
3616 procedure Delay_Descriptors (E : Entity_Id);
3617 -- Delay generation of subprogram descriptors for given entity
3619 function Might_Inline_Subp return Boolean;
3620 -- If inlining is active and the generic contains inlined subprograms,
3621 -- we instantiate the body. This may cause superfluous instantiations,
3622 -- but it is simpler than detecting the need for the body at the point
3623 -- of inlining, when the context of the instance is not available.
3625 function Must_Inline_Subp return Boolean;
3626 -- If inlining is active and the generic contains inlined subprograms,
3627 -- return True if some of the inlined subprograms must be inlined by
3630 -----------------------
3631 -- Delay_Descriptors --
3632 -----------------------
3634 procedure Delay_Descriptors (E : Entity_Id) is
3636 if not Delay_Subprogram_Descriptors (E) then
3637 Set_Delay_Subprogram_Descriptors (E);
3638 Pending_Descriptor.Append (E);
3640 end Delay_Descriptors;
3642 -----------------------
3643 -- Might_Inline_Subp --
3644 -----------------------
3646 function Might_Inline_Subp return Boolean is
3650 if not Inline_Processing_Required then
3654 E := First_Entity (Gen_Unit);
3655 while Present (E) loop
3656 if Is_Subprogram (E) and then Is_Inlined (E) then
3665 end Might_Inline_Subp;
3667 ----------------------
3668 -- Must_Inline_Subp --
3669 ----------------------
3671 function Must_Inline_Subp return Boolean is
3675 if not Inline_Processing_Required then
3679 E := First_Entity (Gen_Unit);
3680 while Present (E) loop
3681 if Is_Subprogram (E)
3682 and then Is_Inlined (E)
3683 and then Must_Inline (E)
3693 end Must_Inline_Subp;
3695 -- Local declarations
3697 Vis_Prims_List : Elist_Id := No_Elist;
3698 -- List of primitives made temporarily visible in the instantiation
3699 -- to match the visibility of the formal type
3701 -- Start of processing for Analyze_Package_Instantiation
3704 Check_SPARK_05_Restriction ("generic is not allowed", N);
3706 -- Very first thing: check for Text_IO sp[ecial unit in case we are
3707 -- instantiating one of the children of [[Wide_]Wide_]Text_IO.
3709 Check_Text_IO_Special_Unit (Name (N));
3711 -- Make node global for error reporting
3713 Instantiation_Node := N;
3715 -- Turn off style checking in instances. If the check is enabled on the
3716 -- generic unit, a warning in an instance would just be noise. If not
3717 -- enabled on the generic, then a warning in an instance is just wrong.
3719 Style_Check := False;
3721 -- Case of instantiation of a generic package
3723 if Nkind (N) = N_Package_Instantiation then
3724 Act_Decl_Id := New_Copy (Defining_Entity (N));
3725 Set_Comes_From_Source (Act_Decl_Id, True);
3727 if Nkind (Defining_Unit_Name (N)) = N_Defining_Program_Unit_Name then
3729 Make_Defining_Program_Unit_Name (Loc,
3730 Name => New_Copy_Tree (Name (Defining_Unit_Name (N))),
3731 Defining_Identifier => Act_Decl_Id);
3733 Act_Decl_Name := Act_Decl_Id;
3736 -- Case of instantiation of a formal package
3739 Act_Decl_Id := Defining_Identifier (N);
3740 Act_Decl_Name := Act_Decl_Id;
3743 Generate_Definition (Act_Decl_Id);
3744 Preanalyze_Actuals (N);
3747 Env_Installed := True;
3749 -- Reset renaming map for formal types. The mapping is established
3750 -- when analyzing the generic associations, but some mappings are
3751 -- inherited from formal packages of parent units, and these are
3752 -- constructed when the parents are installed.
3754 Generic_Renamings.Set_Last (0);
3755 Generic_Renamings_HTable.Reset;
3757 Check_Generic_Child_Unit (Gen_Id, Parent_Installed);
3758 Gen_Unit := Entity (Gen_Id);
3760 -- Verify that it is the name of a generic package
3762 -- A visibility glitch: if the instance is a child unit and the generic
3763 -- is the generic unit of a parent instance (i.e. both the parent and
3764 -- the child units are instances of the same package) the name now
3765 -- denotes the renaming within the parent, not the intended generic
3766 -- unit. See if there is a homonym that is the desired generic. The
3767 -- renaming declaration must be visible inside the instance of the
3768 -- child, but not when analyzing the name in the instantiation itself.
3770 if Ekind (Gen_Unit) = E_Package
3771 and then Present (Renamed_Entity (Gen_Unit))
3772 and then In_Open_Scopes (Renamed_Entity (Gen_Unit))
3773 and then Is_Generic_Instance (Renamed_Entity (Gen_Unit))
3774 and then Present (Homonym (Gen_Unit))
3776 Gen_Unit := Homonym (Gen_Unit);
3779 if Etype (Gen_Unit) = Any_Type then
3783 elsif Ekind (Gen_Unit) /= E_Generic_Package then
3785 -- Ada 2005 (AI-50217): Cannot use instance in limited with_clause
3787 if From_Limited_With (Gen_Unit) then
3789 ("cannot instantiate a limited withed package", Gen_Id);
3792 ("& is not the name of a generic package", Gen_Id, Gen_Unit);
3799 if In_Extended_Main_Source_Unit (N) then
3800 Set_Is_Instantiated (Gen_Unit);
3801 Generate_Reference (Gen_Unit, N);
3803 if Present (Renamed_Object (Gen_Unit)) then
3804 Set_Is_Instantiated (Renamed_Object (Gen_Unit));
3805 Generate_Reference (Renamed_Object (Gen_Unit), N);
3809 if Nkind (Gen_Id) = N_Identifier
3810 and then Chars (Gen_Unit) = Chars (Defining_Entity (N))
3813 ("& is hidden within declaration of instance", Gen_Id, Gen_Unit);
3815 elsif Nkind (Gen_Id) = N_Expanded_Name
3816 and then Is_Child_Unit (Gen_Unit)
3817 and then Nkind (Prefix (Gen_Id)) = N_Identifier
3818 and then Chars (Act_Decl_Id) = Chars (Prefix (Gen_Id))
3821 ("& is hidden within declaration of instance ", Prefix (Gen_Id));
3824 Set_Entity (Gen_Id, Gen_Unit);
3826 -- If generic is a renaming, get original generic unit
3828 if Present (Renamed_Object (Gen_Unit))
3829 and then Ekind (Renamed_Object (Gen_Unit)) = E_Generic_Package
3831 Gen_Unit := Renamed_Object (Gen_Unit);
3834 -- Verify that there are no circular instantiations
3836 if In_Open_Scopes (Gen_Unit) then
3837 Error_Msg_NE ("instantiation of & within itself", N, Gen_Unit);
3841 elsif Contains_Instance_Of (Gen_Unit, Current_Scope, Gen_Id) then
3842 Error_Msg_Node_2 := Current_Scope;
3844 ("circular Instantiation: & instantiated in &!", N, Gen_Unit);
3845 Circularity_Detected := True;
3850 -- If the context of the instance is subject to SPARK_Mode "off",
3851 -- set the global flag which signals Analyze_Pragma to ignore all
3852 -- SPARK_Mode pragmas within the instance.
3854 if SPARK_Mode = Off then
3855 Ignore_Pragma_SPARK_Mode := True;
3858 Gen_Decl := Unit_Declaration_Node (Gen_Unit);
3860 -- Initialize renamings map, for error checking, and the list that
3861 -- holds private entities whose views have changed between generic
3862 -- definition and instantiation. If this is the instance created to
3863 -- validate an actual package, the instantiation environment is that
3864 -- of the enclosing instance.
3866 Create_Instantiation_Source (N, Gen_Unit, False, S_Adjustment);
3868 -- Copy original generic tree, to produce text for instantiation
3872 (Original_Node (Gen_Decl), Empty, Instantiating => True);
3874 Act_Spec := Specification (Act_Tree);
3876 -- If this is the instance created to validate an actual package,
3877 -- only the formals matter, do not examine the package spec itself.
3879 if Is_Actual_Pack then
3880 Set_Visible_Declarations (Act_Spec, New_List);
3881 Set_Private_Declarations (Act_Spec, New_List);
3885 Analyze_Associations
3887 Formals => Generic_Formal_Declarations (Act_Tree),
3888 F_Copy => Generic_Formal_Declarations (Gen_Decl));
3890 Vis_Prims_List := Check_Hidden_Primitives (Renaming_List);
3892 Set_Instance_Env (Gen_Unit, Act_Decl_Id);
3893 Set_Defining_Unit_Name (Act_Spec, Act_Decl_Name);
3894 Set_Is_Generic_Instance (Act_Decl_Id);
3895 Set_Generic_Parent (Act_Spec, Gen_Unit);
3897 -- References to the generic in its own declaration or its body are
3898 -- references to the instance. Add a renaming declaration for the
3899 -- generic unit itself. This declaration, as well as the renaming
3900 -- declarations for the generic formals, must remain private to the
3901 -- unit: the formals, because this is the language semantics, and
3902 -- the unit because its use is an artifact of the implementation.
3905 Make_Package_Renaming_Declaration (Loc,
3906 Defining_Unit_Name =>
3907 Make_Defining_Identifier (Loc, Chars (Gen_Unit)),
3908 Name => New_Occurrence_Of (Act_Decl_Id, Loc));
3910 Append (Unit_Renaming, Renaming_List);
3912 -- The renaming declarations are the first local declarations of the
3915 if Is_Non_Empty_List (Visible_Declarations (Act_Spec)) then
3917 (First (Visible_Declarations (Act_Spec)), Renaming_List);
3919 Set_Visible_Declarations (Act_Spec, Renaming_List);
3922 Act_Decl := Make_Package_Declaration (Loc, Specification => Act_Spec);
3924 -- Propagate the aspect specifications from the package declaration
3925 -- template to the instantiated version of the package declaration.
3927 if Has_Aspects (Act_Tree) then
3928 Set_Aspect_Specifications (Act_Decl,
3929 New_Copy_List_Tree (Aspect_Specifications (Act_Tree)));
3932 -- Save the instantiation node, for subsequent instantiation of the
3933 -- body, if there is one and we are generating code for the current
3934 -- unit. Mark unit as having a body (avoids premature error message).
3936 -- We instantiate the body if we are generating code, if we are
3937 -- generating cross-reference information, or if we are building
3938 -- trees for ASIS use or GNATprove use.
3941 Enclosing_Body_Present : Boolean := False;
3942 -- If the generic unit is not a compilation unit, then a body may
3943 -- be present in its parent even if none is required. We create a
3944 -- tentative pending instantiation for the body, which will be
3945 -- discarded if none is actually present.
3950 if Scope (Gen_Unit) /= Standard_Standard
3951 and then not Is_Child_Unit (Gen_Unit)
3953 Scop := Scope (Gen_Unit);
3955 while Present (Scop)
3956 and then Scop /= Standard_Standard
3958 if Unit_Requires_Body (Scop) then
3959 Enclosing_Body_Present := True;
3962 elsif In_Open_Scopes (Scop)
3963 and then In_Package_Body (Scop)
3965 Enclosing_Body_Present := True;
3969 exit when Is_Compilation_Unit (Scop);
3970 Scop := Scope (Scop);
3974 -- If front-end inlining is enabled, and this is a unit for which
3975 -- code will be generated, we instantiate the body at once.
3977 -- This is done if the instance is not the main unit, and if the
3978 -- generic is not a child unit of another generic, to avoid scope
3979 -- problems and the reinstallation of parent instances.
3982 and then (not Is_Child_Unit (Gen_Unit)
3983 or else not Is_Generic_Unit (Scope (Gen_Unit)))
3984 and then Might_Inline_Subp
3985 and then not Is_Actual_Pack
3987 if not Back_End_Inlining
3988 and then Front_End_Inlining
3989 and then (Is_In_Main_Unit (N)
3990 or else In_Main_Context (Current_Scope))
3991 and then Nkind (Parent (N)) /= N_Compilation_Unit
3995 elsif Back_End_Inlining
3996 and then Must_Inline_Subp
3997 and then (Is_In_Main_Unit (N)
3998 or else In_Main_Context (Current_Scope))
3999 and then Nkind (Parent (N)) /= N_Compilation_Unit
4003 -- In configurable_run_time mode we force the inlining of
4004 -- predefined subprograms marked Inline_Always, to minimize
4005 -- the use of the run-time library.
4007 elsif Is_Predefined_File_Name
4008 (Unit_File_Name (Get_Source_Unit (Gen_Decl)))
4009 and then Configurable_Run_Time_Mode
4010 and then Nkind (Parent (N)) /= N_Compilation_Unit
4015 -- If the current scope is itself an instance within a child
4016 -- unit, there will be duplications in the scope stack, and the
4017 -- unstacking mechanism in Inline_Instance_Body will fail.
4018 -- This loses some rare cases of optimization, and might be
4019 -- improved some day, if we can find a proper abstraction for
4020 -- "the complete compilation context" that can be saved and
4023 if Is_Generic_Instance (Current_Scope) then
4025 Curr_Unit : constant Entity_Id :=
4026 Cunit_Entity (Current_Sem_Unit);
4028 if Curr_Unit /= Current_Scope
4029 and then Is_Child_Unit (Curr_Unit)
4031 Inline_Now := False;
4038 (Unit_Requires_Body (Gen_Unit)
4039 or else Enclosing_Body_Present
4040 or else Present (Corresponding_Body (Gen_Decl)))
4041 and then (Is_In_Main_Unit (N) or else Might_Inline_Subp)
4042 and then not Is_Actual_Pack
4043 and then not Inline_Now
4044 and then (Operating_Mode = Generate_Code
4046 -- Need comment for this check ???
4048 or else (Operating_Mode = Check_Semantics
4049 and then (ASIS_Mode or GNATprove_Mode)));
4051 -- If front_end_inlining is enabled, do not instantiate body if
4052 -- within a generic context.
4054 if (Front_End_Inlining and then not Expander_Active)
4055 or else Is_Generic_Unit (Cunit_Entity (Main_Unit))
4057 Needs_Body := False;
4060 -- If the current context is generic, and the package being
4061 -- instantiated is declared within a formal package, there is no
4062 -- body to instantiate until the enclosing generic is instantiated
4063 -- and there is an actual for the formal package. If the formal
4064 -- package has parameters, we build a regular package instance for
4065 -- it, that precedes the original formal package declaration.
4067 if In_Open_Scopes (Scope (Scope (Gen_Unit))) then
4069 Decl : constant Node_Id :=
4071 (Unit_Declaration_Node (Scope (Gen_Unit)));
4073 if Nkind (Decl) = N_Formal_Package_Declaration
4074 or else (Nkind (Decl) = N_Package_Declaration
4075 and then Is_List_Member (Decl)
4076 and then Present (Next (Decl))
4078 Nkind (Next (Decl)) =
4079 N_Formal_Package_Declaration)
4081 Needs_Body := False;
4087 -- For RCI unit calling stubs, we omit the instance body if the
4088 -- instance is the RCI library unit itself.
4090 -- However there is a special case for nested instances: in this case
4091 -- we do generate the instance body, as it might be required, e.g.
4092 -- because it provides stream attributes for some type used in the
4093 -- profile of a remote subprogram. This is consistent with 12.3(12),
4094 -- which indicates that the instance body occurs at the place of the
4095 -- instantiation, and thus is part of the RCI declaration, which is
4096 -- present on all client partitions (this is E.2.3(18)).
4098 -- Note that AI12-0002 may make it illegal at some point to have
4099 -- stream attributes defined in an RCI unit, in which case this
4100 -- special case will become unnecessary. In the meantime, there
4101 -- is known application code in production that depends on this
4102 -- being possible, so we definitely cannot eliminate the body in
4103 -- the case of nested instances for the time being.
4105 -- When we generate a nested instance body, calling stubs for any
4106 -- relevant subprogram will be be inserted immediately after the
4107 -- subprogram declarations, and will take precedence over the
4108 -- subsequent (original) body. (The stub and original body will be
4109 -- complete homographs, but this is permitted in an instance).
4110 -- (Could we do better and remove the original body???)
4112 if Distribution_Stub_Mode = Generate_Caller_Stub_Body
4113 and then Comes_From_Source (N)
4114 and then Nkind (Parent (N)) = N_Compilation_Unit
4116 Needs_Body := False;
4121 -- Here is a defence against a ludicrous number of instantiations
4122 -- caused by a circular set of instantiation attempts.
4124 if Pending_Instantiations.Last > Maximum_Instantiations then
4125 Error_Msg_Uint_1 := UI_From_Int (Maximum_Instantiations);
4126 Error_Msg_N ("too many instantiations, exceeds max of^", N);
4127 Error_Msg_N ("\limit can be changed using -gnateinn switch", N);
4128 raise Unrecoverable_Error;
4131 -- Indicate that the enclosing scopes contain an instantiation,
4132 -- and that cleanup actions should be delayed until after the
4133 -- instance body is expanded.
4135 Check_Forward_Instantiation (Gen_Decl);
4136 if Nkind (N) = N_Package_Instantiation then
4138 Enclosing_Master : Entity_Id;
4141 -- Loop to search enclosing masters
4143 Enclosing_Master := Current_Scope;
4144 Scope_Loop : while Enclosing_Master /= Standard_Standard loop
4145 if Ekind (Enclosing_Master) = E_Package then
4146 if Is_Compilation_Unit (Enclosing_Master) then
4147 if In_Package_Body (Enclosing_Master) then
4149 (Body_Entity (Enclosing_Master));
4158 Enclosing_Master := Scope (Enclosing_Master);
4161 elsif Is_Generic_Unit (Enclosing_Master)
4162 or else Ekind (Enclosing_Master) = E_Void
4164 -- Cleanup actions will eventually be performed on the
4165 -- enclosing subprogram or package instance, if any.
4166 -- Enclosing scope is void in the formal part of a
4167 -- generic subprogram.
4172 if Ekind (Enclosing_Master) = E_Entry
4174 Ekind (Scope (Enclosing_Master)) = E_Protected_Type
4176 if not Expander_Active then
4180 Protected_Body_Subprogram (Enclosing_Master);
4184 Set_Delay_Cleanups (Enclosing_Master);
4186 while Ekind (Enclosing_Master) = E_Block loop
4187 Enclosing_Master := Scope (Enclosing_Master);
4190 if Is_Subprogram (Enclosing_Master) then
4191 Delay_Descriptors (Enclosing_Master);
4193 elsif Is_Task_Type (Enclosing_Master) then
4195 TBP : constant Node_Id :=
4196 Get_Task_Body_Procedure
4199 if Present (TBP) then
4200 Delay_Descriptors (TBP);
4201 Set_Delay_Cleanups (TBP);
4208 end loop Scope_Loop;
4211 -- Make entry in table
4213 Pending_Instantiations.Append
4215 Act_Decl => Act_Decl,
4216 Expander_Status => Expander_Active,
4217 Current_Sem_Unit => Current_Sem_Unit,
4218 Scope_Suppress => Scope_Suppress,
4219 Local_Suppress_Stack_Top => Local_Suppress_Stack_Top,
4220 Version => Ada_Version,
4221 Version_Pragma => Ada_Version_Pragma,
4222 Warnings => Save_Warnings,
4223 SPARK_Mode => SPARK_Mode,
4224 SPARK_Mode_Pragma => SPARK_Mode_Pragma));
4228 Set_Categorization_From_Pragmas (Act_Decl);
4230 if Parent_Installed then
4234 Set_Instance_Spec (N, Act_Decl);
4236 -- If not a compilation unit, insert the package declaration before
4237 -- the original instantiation node.
4239 if Nkind (Parent (N)) /= N_Compilation_Unit then
4240 Mark_Rewrite_Insertion (Act_Decl);
4241 Insert_Before (N, Act_Decl);
4244 -- For an instantiation that is a compilation unit, place
4245 -- declaration on current node so context is complete for analysis
4246 -- (including nested instantiations). If this is the main unit,
4247 -- the declaration eventually replaces the instantiation node.
4248 -- If the instance body is created later, it replaces the
4249 -- instance node, and the declaration is attached to it
4250 -- (see Build_Instance_Compilation_Unit_Nodes).
4253 if Cunit_Entity (Current_Sem_Unit) = Defining_Entity (N) then
4255 -- The entity for the current unit is the newly created one,
4256 -- and all semantic information is attached to it.
4258 Set_Cunit_Entity (Current_Sem_Unit, Act_Decl_Id);
4260 -- If this is the main unit, replace the main entity as well
4262 if Current_Sem_Unit = Main_Unit then
4263 Main_Unit_Entity := Act_Decl_Id;
4267 Set_Unit (Parent (N), Act_Decl);
4268 Set_Parent_Spec (Act_Decl, Parent_Spec (N));
4269 Set_Package_Instantiation (Act_Decl_Id, N);
4271 -- Process aspect specifications of the instance node, if any, to
4272 -- take into account categorization pragmas before analyzing the
4275 if Has_Aspects (N) then
4276 Analyze_Aspect_Specifications (N, Act_Decl_Id);
4280 Set_Unit (Parent (N), N);
4281 Set_Body_Required (Parent (N), False);
4283 -- We never need elaboration checks on instantiations, since by
4284 -- definition, the body instantiation is elaborated at the same
4285 -- time as the spec instantiation.
4287 Set_Suppress_Elaboration_Warnings (Act_Decl_Id);
4288 Set_Kill_Elaboration_Checks (Act_Decl_Id);
4291 Check_Elab_Instantiation (N);
4293 if ABE_Is_Certain (N) and then Needs_Body then
4294 Pending_Instantiations.Decrement_Last;
4297 Check_Hidden_Child_Unit (N, Gen_Unit, Act_Decl_Id);
4299 Set_First_Private_Entity (Defining_Unit_Name (Unit_Renaming),
4300 First_Private_Entity (Act_Decl_Id));
4302 -- If the instantiation will receive a body, the unit will be
4303 -- transformed into a package body, and receive its own elaboration
4304 -- entity. Otherwise, the nature of the unit is now a package
4307 if Nkind (Parent (N)) = N_Compilation_Unit
4308 and then not Needs_Body
4310 Rewrite (N, Act_Decl);
4313 if Present (Corresponding_Body (Gen_Decl))
4314 or else Unit_Requires_Body (Gen_Unit)
4316 Set_Has_Completion (Act_Decl_Id);
4319 Check_Formal_Packages (Act_Decl_Id);
4321 Restore_Hidden_Primitives (Vis_Prims_List);
4322 Restore_Private_Views (Act_Decl_Id);
4324 Inherit_Context (Gen_Decl, N);
4326 if Parent_Installed then
4331 Env_Installed := False;
4334 Validate_Categorization_Dependency (N, Act_Decl_Id);
4336 -- There used to be a check here to prevent instantiations in local
4337 -- contexts if the No_Local_Allocators restriction was active. This
4338 -- check was removed by a binding interpretation in AI-95-00130/07,
4339 -- but we retain the code for documentation purposes.
4341 -- if Ekind (Act_Decl_Id) /= E_Void
4342 -- and then not Is_Library_Level_Entity (Act_Decl_Id)
4344 -- Check_Restriction (No_Local_Allocators, N);
4348 Inline_Instance_Body (N, Gen_Unit, Act_Decl);
4351 -- The following is a tree patch for ASIS: ASIS needs separate nodes to
4352 -- be used as defining identifiers for a formal package and for the
4353 -- corresponding expanded package.
4355 if Nkind (N) = N_Formal_Package_Declaration then
4356 Act_Decl_Id := New_Copy (Defining_Entity (N));
4357 Set_Comes_From_Source (Act_Decl_Id, True);
4358 Set_Is_Generic_Instance (Act_Decl_Id, False);
4359 Set_Defining_Identifier (N, Act_Decl_Id);
4362 Ignore_Pragma_SPARK_Mode := Save_IPSM;
4363 SPARK_Mode := Save_SM;
4364 SPARK_Mode_Pragma := Save_SMP;
4365 Style_Check := Save_Style_Check;
4367 -- Check that if N is an instantiation of System.Dim_Float_IO or
4368 -- System.Dim_Integer_IO, the formal type has a dimension system.
4370 if Nkind (N) = N_Package_Instantiation
4371 and then Is_Dim_IO_Package_Instantiation (N)
4374 Assoc : constant Node_Id := First (Generic_Associations (N));
4376 if not Has_Dimension_System
4377 (Etype (Explicit_Generic_Actual_Parameter (Assoc)))
4379 Error_Msg_N ("type with a dimension system expected", Assoc);
4385 if Has_Aspects (N) and then Nkind (Parent (N)) /= N_Compilation_Unit then
4386 Analyze_Aspect_Specifications (N, Act_Decl_Id);
4390 when Instantiation_Error =>
4391 if Parent_Installed then
4395 if Env_Installed then
4399 Ignore_Pragma_SPARK_Mode := Save_IPSM;
4400 SPARK_Mode := Save_SM;
4401 SPARK_Mode_Pragma := Save_SMP;
4402 Style_Check := Save_Style_Check;
4403 end Analyze_Package_Instantiation;
4405 --------------------------
4406 -- Inline_Instance_Body --
4407 --------------------------
4409 procedure Inline_Instance_Body
4411 Gen_Unit : Entity_Id;
4415 Gen_Comp : constant Entity_Id :=
4416 Cunit_Entity (Get_Source_Unit (Gen_Unit));
4417 Curr_Comp : constant Node_Id := Cunit (Current_Sem_Unit);
4418 Curr_Scope : Entity_Id := Empty;
4419 Curr_Unit : constant Entity_Id := Cunit_Entity (Current_Sem_Unit);
4420 Removed : Boolean := False;
4421 Num_Scopes : Int := 0;
4423 Scope_Stack_Depth : constant Int :=
4424 Scope_Stack.Last - Scope_Stack.First + 1;
4426 Use_Clauses : array (1 .. Scope_Stack_Depth) of Node_Id;
4427 Instances : array (1 .. Scope_Stack_Depth) of Entity_Id;
4428 Inner_Scopes : array (1 .. Scope_Stack_Depth) of Entity_Id;
4430 Num_Inner : Int := 0;
4431 N_Instances : Int := 0;
4435 -- Case of generic unit defined in another unit. We must remove the
4436 -- complete context of the current unit to install that of the generic.
4438 if Gen_Comp /= Cunit_Entity (Current_Sem_Unit) then
4440 -- Add some comments for the following two loops ???
4443 while Present (S) and then S /= Standard_Standard loop
4445 Num_Scopes := Num_Scopes + 1;
4447 Use_Clauses (Num_Scopes) :=
4449 (Scope_Stack.Last - Num_Scopes + 1).
4451 End_Use_Clauses (Use_Clauses (Num_Scopes));
4453 exit when Scope_Stack.Last - Num_Scopes + 1 = Scope_Stack.First
4454 or else Scope_Stack.Table
4455 (Scope_Stack.Last - Num_Scopes).Entity
4459 exit when Is_Generic_Instance (S)
4460 and then (In_Package_Body (S)
4461 or else Ekind (S) = E_Procedure
4462 or else Ekind (S) = E_Function);
4466 Vis := Is_Immediately_Visible (Gen_Comp);
4468 -- Find and save all enclosing instances
4473 and then S /= Standard_Standard
4475 if Is_Generic_Instance (S) then
4476 N_Instances := N_Instances + 1;
4477 Instances (N_Instances) := S;
4479 exit when In_Package_Body (S);
4485 -- Remove context of current compilation unit, unless we are within a
4486 -- nested package instantiation, in which case the context has been
4487 -- removed previously.
4489 -- If current scope is the body of a child unit, remove context of
4490 -- spec as well. If an enclosing scope is an instance body, the
4491 -- context has already been removed, but the entities in the body
4492 -- must be made invisible as well.
4497 and then S /= Standard_Standard
4499 if Is_Generic_Instance (S)
4500 and then (In_Package_Body (S)
4501 or else Ekind (S) = E_Procedure
4502 or else Ekind (S) = E_Function)
4504 -- We still have to remove the entities of the enclosing
4505 -- instance from direct visibility.
4510 E := First_Entity (S);
4511 while Present (E) loop
4512 Set_Is_Immediately_Visible (E, False);
4521 or else (Ekind (Curr_Unit) = E_Package_Body
4522 and then S = Spec_Entity (Curr_Unit))
4523 or else (Ekind (Curr_Unit) = E_Subprogram_Body
4526 (Unit_Declaration_Node (Curr_Unit)))
4530 -- Remove entities in current scopes from visibility, so that
4531 -- instance body is compiled in a clean environment.
4533 List := Save_Scope_Stack (Handle_Use => False);
4535 if Is_Child_Unit (S) then
4537 -- Remove child unit from stack, as well as inner scopes.
4538 -- Removing the context of a child unit removes parent units
4541 while Current_Scope /= S loop
4542 Num_Inner := Num_Inner + 1;
4543 Inner_Scopes (Num_Inner) := Current_Scope;
4548 Remove_Context (Curr_Comp);
4552 Remove_Context (Curr_Comp);
4555 if Ekind (Curr_Unit) = E_Package_Body then
4556 Remove_Context (Library_Unit (Curr_Comp));
4562 pragma Assert (Num_Inner < Num_Scopes);
4564 Push_Scope (Standard_Standard);
4565 Scope_Stack.Table (Scope_Stack.Last).Is_Active_Stack_Base := True;
4566 Instantiate_Package_Body
4569 Act_Decl => Act_Decl,
4570 Expander_Status => Expander_Active,
4571 Current_Sem_Unit => Current_Sem_Unit,
4572 Scope_Suppress => Scope_Suppress,
4573 Local_Suppress_Stack_Top => Local_Suppress_Stack_Top,
4574 Version => Ada_Version,
4575 Version_Pragma => Ada_Version_Pragma,
4576 Warnings => Save_Warnings,
4577 SPARK_Mode => SPARK_Mode,
4578 SPARK_Mode_Pragma => SPARK_Mode_Pragma)),
4579 Inlined_Body => True);
4585 Set_Is_Immediately_Visible (Gen_Comp, Vis);
4587 -- Reset Generic_Instance flag so that use clauses can be installed
4588 -- in the proper order. (See Use_One_Package for effect of enclosing
4589 -- instances on processing of use clauses).
4591 for J in 1 .. N_Instances loop
4592 Set_Is_Generic_Instance (Instances (J), False);
4596 Install_Context (Curr_Comp);
4598 if Present (Curr_Scope)
4599 and then Is_Child_Unit (Curr_Scope)
4601 Push_Scope (Curr_Scope);
4602 Set_Is_Immediately_Visible (Curr_Scope);
4604 -- Finally, restore inner scopes as well
4606 for J in reverse 1 .. Num_Inner loop
4607 Push_Scope (Inner_Scopes (J));
4611 Restore_Scope_Stack (List, Handle_Use => False);
4613 if Present (Curr_Scope)
4615 (In_Private_Part (Curr_Scope)
4616 or else In_Package_Body (Curr_Scope))
4618 -- Install private declaration of ancestor units, which are
4619 -- currently available. Restore_Scope_Stack and Install_Context
4620 -- only install the visible part of parents.
4625 Par := Scope (Curr_Scope);
4626 while (Present (Par))
4627 and then Par /= Standard_Standard
4629 Install_Private_Declarations (Par);
4636 -- Restore use clauses. For a child unit, use clauses in the parents
4637 -- are restored when installing the context, so only those in inner
4638 -- scopes (and those local to the child unit itself) need to be
4639 -- installed explicitly.
4641 if Is_Child_Unit (Curr_Unit)
4644 for J in reverse 1 .. Num_Inner + 1 loop
4645 Scope_Stack.Table (Scope_Stack.Last - J + 1).First_Use_Clause :=
4647 Install_Use_Clauses (Use_Clauses (J));
4651 for J in reverse 1 .. Num_Scopes loop
4652 Scope_Stack.Table (Scope_Stack.Last - J + 1).First_Use_Clause :=
4654 Install_Use_Clauses (Use_Clauses (J));
4658 -- Restore status of instances. If one of them is a body, make its
4659 -- local entities visible again.
4666 for J in 1 .. N_Instances loop
4667 Inst := Instances (J);
4668 Set_Is_Generic_Instance (Inst, True);
4670 if In_Package_Body (Inst)
4671 or else Ekind (S) = E_Procedure
4672 or else Ekind (S) = E_Function
4674 E := First_Entity (Instances (J));
4675 while Present (E) loop
4676 Set_Is_Immediately_Visible (E);
4683 -- If generic unit is in current unit, current context is correct
4686 Instantiate_Package_Body
4689 Act_Decl => Act_Decl,
4690 Expander_Status => Expander_Active,
4691 Current_Sem_Unit => Current_Sem_Unit,
4692 Scope_Suppress => Scope_Suppress,
4693 Local_Suppress_Stack_Top => Local_Suppress_Stack_Top,
4694 Version => Ada_Version,
4695 Version_Pragma => Ada_Version_Pragma,
4696 Warnings => Save_Warnings,
4697 SPARK_Mode => SPARK_Mode,
4698 SPARK_Mode_Pragma => SPARK_Mode_Pragma)),
4699 Inlined_Body => True);
4701 end Inline_Instance_Body;
4703 -------------------------------------
4704 -- Analyze_Procedure_Instantiation --
4705 -------------------------------------
4707 procedure Analyze_Procedure_Instantiation (N : Node_Id) is
4709 Analyze_Subprogram_Instantiation (N, E_Procedure);
4710 end Analyze_Procedure_Instantiation;
4712 -----------------------------------
4713 -- Need_Subprogram_Instance_Body --
4714 -----------------------------------
4716 function Need_Subprogram_Instance_Body
4718 Subp : Entity_Id) return Boolean
4721 -- Must be inlined (or inlined renaming)
4723 if (Is_In_Main_Unit (N)
4724 or else Is_Inlined (Subp)
4725 or else Is_Inlined (Alias (Subp)))
4727 -- Must be generating code or analyzing code in ASIS/GNATprove mode
4729 and then (Operating_Mode = Generate_Code
4730 or else (Operating_Mode = Check_Semantics
4731 and then (ASIS_Mode or GNATprove_Mode)))
4733 -- The body is needed when generating code (full expansion), in ASIS
4734 -- mode for other tools, and in GNATprove mode (special expansion) for
4735 -- formal verification of the body itself.
4737 and then (Expander_Active or ASIS_Mode or GNATprove_Mode)
4739 -- No point in inlining if ABE is inevitable
4741 and then not ABE_Is_Certain (N)
4743 -- Or if subprogram is eliminated
4745 and then not Is_Eliminated (Subp)
4747 Pending_Instantiations.Append
4749 Act_Decl => Unit_Declaration_Node (Subp),
4750 Expander_Status => Expander_Active,
4751 Current_Sem_Unit => Current_Sem_Unit,
4752 Scope_Suppress => Scope_Suppress,
4753 Local_Suppress_Stack_Top => Local_Suppress_Stack_Top,
4754 Version => Ada_Version,
4755 Version_Pragma => Ada_Version_Pragma,
4756 Warnings => Save_Warnings,
4757 SPARK_Mode => SPARK_Mode,
4758 SPARK_Mode_Pragma => SPARK_Mode_Pragma));
4761 -- Here if not inlined, or we ignore the inlining
4766 end Need_Subprogram_Instance_Body;
4768 --------------------------------------
4769 -- Analyze_Subprogram_Instantiation --
4770 --------------------------------------
4772 procedure Analyze_Subprogram_Instantiation
4776 Loc : constant Source_Ptr := Sloc (N);
4777 Gen_Id : constant Node_Id := Name (N);
4779 Anon_Id : constant Entity_Id :=
4780 Make_Defining_Identifier (Sloc (Defining_Entity (N)),
4781 Chars => New_External_Name
4782 (Chars (Defining_Entity (N)), 'R'));
4784 Act_Decl_Id : Entity_Id;
4789 Env_Installed : Boolean := False;
4790 Gen_Unit : Entity_Id;
4792 Pack_Id : Entity_Id;
4793 Parent_Installed : Boolean := False;
4794 Renaming_List : List_Id;
4796 procedure Analyze_Instance_And_Renamings;
4797 -- The instance must be analyzed in a context that includes the mappings
4798 -- of generic parameters into actuals. We create a package declaration
4799 -- for this purpose, and a subprogram with an internal name within the
4800 -- package. The subprogram instance is simply an alias for the internal
4801 -- subprogram, declared in the current scope.
4803 ------------------------------------
4804 -- Analyze_Instance_And_Renamings --
4805 ------------------------------------
4807 procedure Analyze_Instance_And_Renamings is
4808 Def_Ent : constant Entity_Id := Defining_Entity (N);
4809 Pack_Decl : Node_Id;
4812 if Nkind (Parent (N)) = N_Compilation_Unit then
4814 -- For the case of a compilation unit, the container package has
4815 -- the same name as the instantiation, to insure that the binder
4816 -- calls the elaboration procedure with the right name. Copy the
4817 -- entity of the instance, which may have compilation level flags
4818 -- (e.g. Is_Child_Unit) set.
4820 Pack_Id := New_Copy (Def_Ent);
4823 -- Otherwise we use the name of the instantiation concatenated
4824 -- with its source position to ensure uniqueness if there are
4825 -- several instantiations with the same name.
4828 Make_Defining_Identifier (Loc,
4829 Chars => New_External_Name
4830 (Related_Id => Chars (Def_Ent),
4832 Suffix_Index => Source_Offset (Sloc (Def_Ent))));
4835 Pack_Decl := Make_Package_Declaration (Loc,
4836 Specification => Make_Package_Specification (Loc,
4837 Defining_Unit_Name => Pack_Id,
4838 Visible_Declarations => Renaming_List,
4839 End_Label => Empty));
4841 Set_Instance_Spec (N, Pack_Decl);
4842 Set_Is_Generic_Instance (Pack_Id);
4843 Set_Debug_Info_Needed (Pack_Id);
4845 -- Case of not a compilation unit
4847 if Nkind (Parent (N)) /= N_Compilation_Unit then
4848 Mark_Rewrite_Insertion (Pack_Decl);
4849 Insert_Before (N, Pack_Decl);
4850 Set_Has_Completion (Pack_Id);
4852 -- Case of an instantiation that is a compilation unit
4854 -- Place declaration on current node so context is complete for
4855 -- analysis (including nested instantiations), and for use in a
4856 -- context_clause (see Analyze_With_Clause).
4859 Set_Unit (Parent (N), Pack_Decl);
4860 Set_Parent_Spec (Pack_Decl, Parent_Spec (N));
4863 Analyze (Pack_Decl);
4864 Check_Formal_Packages (Pack_Id);
4865 Set_Is_Generic_Instance (Pack_Id, False);
4867 -- Why do we clear Is_Generic_Instance??? We set it 20 lines
4870 -- Body of the enclosing package is supplied when instantiating the
4871 -- subprogram body, after semantic analysis is completed.
4873 if Nkind (Parent (N)) = N_Compilation_Unit then
4875 -- Remove package itself from visibility, so it does not
4876 -- conflict with subprogram.
4878 Set_Name_Entity_Id (Chars (Pack_Id), Homonym (Pack_Id));
4880 -- Set name and scope of internal subprogram so that the proper
4881 -- external name will be generated. The proper scope is the scope
4882 -- of the wrapper package. We need to generate debugging info for
4883 -- the internal subprogram, so set flag accordingly.
4885 Set_Chars (Anon_Id, Chars (Defining_Entity (N)));
4886 Set_Scope (Anon_Id, Scope (Pack_Id));
4888 -- Mark wrapper package as referenced, to avoid spurious warnings
4889 -- if the instantiation appears in various with_ clauses of
4890 -- subunits of the main unit.
4892 Set_Referenced (Pack_Id);
4895 Set_Is_Generic_Instance (Anon_Id);
4896 Set_Debug_Info_Needed (Anon_Id);
4897 Act_Decl_Id := New_Copy (Anon_Id);
4899 Set_Parent (Act_Decl_Id, Parent (Anon_Id));
4900 Set_Chars (Act_Decl_Id, Chars (Defining_Entity (N)));
4901 Set_Sloc (Act_Decl_Id, Sloc (Defining_Entity (N)));
4902 Set_Comes_From_Source (Act_Decl_Id, True);
4904 -- The signature may involve types that are not frozen yet, but the
4905 -- subprogram will be frozen at the point the wrapper package is
4906 -- frozen, so it does not need its own freeze node. In fact, if one
4907 -- is created, it might conflict with the freezing actions from the
4910 Set_Has_Delayed_Freeze (Anon_Id, False);
4912 -- If the instance is a child unit, mark the Id accordingly. Mark
4913 -- the anonymous entity as well, which is the real subprogram and
4914 -- which is used when the instance appears in a context clause.
4915 -- Similarly, propagate the Is_Eliminated flag to handle properly
4916 -- nested eliminated subprograms.
4918 Set_Is_Child_Unit (Act_Decl_Id, Is_Child_Unit (Defining_Entity (N)));
4919 Set_Is_Child_Unit (Anon_Id, Is_Child_Unit (Defining_Entity (N)));
4920 New_Overloaded_Entity (Act_Decl_Id);
4921 Check_Eliminated (Act_Decl_Id);
4922 Set_Is_Eliminated (Anon_Id, Is_Eliminated (Act_Decl_Id));
4924 -- In compilation unit case, kill elaboration checks on the
4925 -- instantiation, since they are never needed -- the body is
4926 -- instantiated at the same point as the spec.
4928 if Nkind (Parent (N)) = N_Compilation_Unit then
4929 Set_Suppress_Elaboration_Warnings (Act_Decl_Id);
4930 Set_Kill_Elaboration_Checks (Act_Decl_Id);
4931 Set_Is_Compilation_Unit (Anon_Id);
4933 Set_Cunit_Entity (Current_Sem_Unit, Pack_Id);
4936 -- The instance is not a freezing point for the new subprogram
4938 Set_Is_Frozen (Act_Decl_Id, False);
4940 if Nkind (Defining_Entity (N)) = N_Defining_Operator_Symbol then
4941 Valid_Operator_Definition (Act_Decl_Id);
4944 Set_Alias (Act_Decl_Id, Anon_Id);
4945 Set_Parent (Act_Decl_Id, Parent (Anon_Id));
4946 Set_Has_Completion (Act_Decl_Id);
4947 Set_Related_Instance (Pack_Id, Act_Decl_Id);
4949 if Nkind (Parent (N)) = N_Compilation_Unit then
4950 Set_Body_Required (Parent (N), False);
4952 end Analyze_Instance_And_Renamings;
4956 Save_IPSM : constant Boolean := Ignore_Pragma_SPARK_Mode;
4957 -- Save flag Ignore_Pragma_SPARK_Mode for restore on exit
4959 Save_SM : constant SPARK_Mode_Type := SPARK_Mode;
4960 Save_SMP : constant Node_Id := SPARK_Mode_Pragma;
4961 -- Save the SPARK_Mode-related data for restore on exit
4963 Vis_Prims_List : Elist_Id := No_Elist;
4964 -- List of primitives made temporarily visible in the instantiation
4965 -- to match the visibility of the formal type
4967 -- Start of processing for Analyze_Subprogram_Instantiation
4970 Check_SPARK_05_Restriction ("generic is not allowed", N);
4972 -- Very first thing: check for special Text_IO unit in case we are
4973 -- instantiating one of the children of [[Wide_]Wide_]Text_IO. Of course
4974 -- such an instantiation is bogus (these are packages, not subprograms),
4975 -- but we get a better error message if we do this.
4977 Check_Text_IO_Special_Unit (Gen_Id);
4979 -- Make node global for error reporting
4981 Instantiation_Node := N;
4983 -- For package instantiations we turn off style checks, because they
4984 -- will have been emitted in the generic. For subprogram instantiations
4985 -- we want to apply at least the check on overriding indicators so we
4986 -- do not modify the style check status.
4988 -- The renaming declarations for the actuals do not come from source and
4989 -- will not generate spurious warnings.
4991 Preanalyze_Actuals (N);
4994 Env_Installed := True;
4995 Check_Generic_Child_Unit (Gen_Id, Parent_Installed);
4996 Gen_Unit := Entity (Gen_Id);
4998 Generate_Reference (Gen_Unit, Gen_Id);
5000 if Nkind (Gen_Id) = N_Identifier
5001 and then Chars (Gen_Unit) = Chars (Defining_Entity (N))
5004 ("& is hidden within declaration of instance", Gen_Id, Gen_Unit);
5007 if Etype (Gen_Unit) = Any_Type then
5012 -- Verify that it is a generic subprogram of the right kind, and that
5013 -- it does not lead to a circular instantiation.
5015 if K = E_Procedure and then Ekind (Gen_Unit) /= E_Generic_Procedure then
5017 ("& is not the name of a generic procedure", Gen_Id, Gen_Unit);
5019 elsif K = E_Function and then Ekind (Gen_Unit) /= E_Generic_Function then
5021 ("& is not the name of a generic function", Gen_Id, Gen_Unit);
5023 elsif In_Open_Scopes (Gen_Unit) then
5024 Error_Msg_NE ("instantiation of & within itself", N, Gen_Unit);
5027 -- If the context of the instance is subject to SPARK_Mode "off",
5028 -- set the global flag which signals Analyze_Pragma to ignore all
5029 -- SPARK_Mode pragmas within the instance.
5031 if SPARK_Mode = Off then
5032 Ignore_Pragma_SPARK_Mode := True;
5035 Set_Entity (Gen_Id, Gen_Unit);
5036 Set_Is_Instantiated (Gen_Unit);
5038 if In_Extended_Main_Source_Unit (N) then
5039 Generate_Reference (Gen_Unit, N);
5042 -- If renaming, get original unit
5044 if Present (Renamed_Object (Gen_Unit))
5045 and then (Ekind (Renamed_Object (Gen_Unit)) = E_Generic_Procedure
5047 Ekind (Renamed_Object (Gen_Unit)) = E_Generic_Function)
5049 Gen_Unit := Renamed_Object (Gen_Unit);
5050 Set_Is_Instantiated (Gen_Unit);
5051 Generate_Reference (Gen_Unit, N);
5054 if Contains_Instance_Of (Gen_Unit, Current_Scope, Gen_Id) then
5055 Error_Msg_Node_2 := Current_Scope;
5057 ("circular Instantiation: & instantiated in &!", N, Gen_Unit);
5058 Circularity_Detected := True;
5059 Restore_Hidden_Primitives (Vis_Prims_List);
5063 Gen_Decl := Unit_Declaration_Node (Gen_Unit);
5065 -- Initialize renamings map, for error checking
5067 Generic_Renamings.Set_Last (0);
5068 Generic_Renamings_HTable.Reset;
5070 Create_Instantiation_Source (N, Gen_Unit, False, S_Adjustment);
5072 -- Copy original generic tree, to produce text for instantiation
5076 (Original_Node (Gen_Decl), Empty, Instantiating => True);
5078 -- Inherit overriding indicator from instance node
5080 Act_Spec := Specification (Act_Tree);
5081 Set_Must_Override (Act_Spec, Must_Override (N));
5082 Set_Must_Not_Override (Act_Spec, Must_Not_Override (N));
5085 Analyze_Associations
5087 Formals => Generic_Formal_Declarations (Act_Tree),
5088 F_Copy => Generic_Formal_Declarations (Gen_Decl));
5090 Vis_Prims_List := Check_Hidden_Primitives (Renaming_List);
5092 -- The subprogram itself cannot contain a nested instance, so the
5093 -- current parent is left empty.
5095 Set_Instance_Env (Gen_Unit, Empty);
5097 -- Build the subprogram declaration, which does not appear in the
5098 -- generic template, and give it a sloc consistent with that of the
5101 Set_Defining_Unit_Name (Act_Spec, Anon_Id);
5102 Set_Generic_Parent (Act_Spec, Gen_Unit);
5104 Make_Subprogram_Declaration (Sloc (Act_Spec),
5105 Specification => Act_Spec);
5107 -- The aspects have been copied previously, but they have to be
5108 -- linked explicitly to the new subprogram declaration. Explicit
5109 -- pre/postconditions on the instance are analyzed below, in a
5112 Move_Aspects (Act_Tree, To => Act_Decl);
5113 Set_Categorization_From_Pragmas (Act_Decl);
5115 if Parent_Installed then
5119 Append (Act_Decl, Renaming_List);
5120 Analyze_Instance_And_Renamings;
5122 -- If the generic is marked Import (Intrinsic), then so is the
5123 -- instance. This indicates that there is no body to instantiate. If
5124 -- generic is marked inline, so it the instance, and the anonymous
5125 -- subprogram it renames. If inlined, or else if inlining is enabled
5126 -- for the compilation, we generate the instance body even if it is
5127 -- not within the main unit.
5129 if Is_Intrinsic_Subprogram (Gen_Unit) then
5130 Set_Is_Intrinsic_Subprogram (Anon_Id);
5131 Set_Is_Intrinsic_Subprogram (Act_Decl_Id);
5133 if Chars (Gen_Unit) = Name_Unchecked_Conversion then
5134 Validate_Unchecked_Conversion (N, Act_Decl_Id);
5138 -- Inherit convention from generic unit. Intrinsic convention, as for
5139 -- an instance of unchecked conversion, is not inherited because an
5140 -- explicit Ada instance has been created.
5142 if Has_Convention_Pragma (Gen_Unit)
5143 and then Convention (Gen_Unit) /= Convention_Intrinsic
5145 Set_Convention (Act_Decl_Id, Convention (Gen_Unit));
5146 Set_Is_Exported (Act_Decl_Id, Is_Exported (Gen_Unit));
5149 Generate_Definition (Act_Decl_Id);
5150 -- Set_Contract (Anon_Id, Make_Contract (Sloc (Anon_Id)));
5152 Set_Contract (Act_Decl_Id, Make_Contract (Sloc (Act_Decl_Id)));
5154 -- Inherit all inlining-related flags which apply to the generic in
5155 -- the subprogram and its declaration.
5157 Set_Is_Inlined (Act_Decl_Id, Is_Inlined (Gen_Unit));
5158 Set_Is_Inlined (Anon_Id, Is_Inlined (Gen_Unit));
5160 Set_Has_Pragma_Inline (Act_Decl_Id, Has_Pragma_Inline (Gen_Unit));
5161 Set_Has_Pragma_Inline (Anon_Id, Has_Pragma_Inline (Gen_Unit));
5163 Set_Has_Pragma_Inline_Always
5164 (Act_Decl_Id, Has_Pragma_Inline_Always (Gen_Unit));
5165 Set_Has_Pragma_Inline_Always
5166 (Anon_Id, Has_Pragma_Inline_Always (Gen_Unit));
5168 if not Is_Intrinsic_Subprogram (Gen_Unit) then
5169 Check_Elab_Instantiation (N);
5172 if Is_Dispatching_Operation (Act_Decl_Id)
5173 and then Ada_Version >= Ada_2005
5179 Formal := First_Formal (Act_Decl_Id);
5180 while Present (Formal) loop
5181 if Ekind (Etype (Formal)) = E_Anonymous_Access_Type
5182 and then Is_Controlling_Formal (Formal)
5183 and then not Can_Never_Be_Null (Formal)
5185 Error_Msg_NE ("access parameter& is controlling,",
5188 ("\corresponding parameter of & must be"
5189 & " explicitly null-excluding", N, Gen_Id);
5192 Next_Formal (Formal);
5197 Check_Hidden_Child_Unit (N, Gen_Unit, Act_Decl_Id);
5199 Validate_Categorization_Dependency (N, Act_Decl_Id);
5201 if not Is_Intrinsic_Subprogram (Act_Decl_Id) then
5202 Inherit_Context (Gen_Decl, N);
5204 Restore_Private_Views (Pack_Id, False);
5206 -- If the context requires a full instantiation, mark node for
5207 -- subsequent construction of the body.
5209 if Need_Subprogram_Instance_Body (N, Act_Decl_Id) then
5210 Check_Forward_Instantiation (Gen_Decl);
5212 -- The wrapper package is always delayed, because it does not
5213 -- constitute a freeze point, but to insure that the freeze
5214 -- node is placed properly, it is created directly when
5215 -- instantiating the body (otherwise the freeze node might
5216 -- appear to early for nested instantiations).
5218 elsif Nkind (Parent (N)) = N_Compilation_Unit then
5220 -- For ASIS purposes, indicate that the wrapper package has
5221 -- replaced the instantiation node.
5223 Rewrite (N, Unit (Parent (N)));
5224 Set_Unit (Parent (N), N);
5227 elsif Nkind (Parent (N)) = N_Compilation_Unit then
5229 -- Replace instance node for library-level instantiations of
5230 -- intrinsic subprograms, for ASIS use.
5232 Rewrite (N, Unit (Parent (N)));
5233 Set_Unit (Parent (N), N);
5236 if Parent_Installed then
5240 Restore_Hidden_Primitives (Vis_Prims_List);
5242 Env_Installed := False;
5243 Generic_Renamings.Set_Last (0);
5244 Generic_Renamings_HTable.Reset;
5246 Ignore_Pragma_SPARK_Mode := Save_IPSM;
5247 SPARK_Mode := Save_SM;
5248 SPARK_Mode_Pragma := Save_SMP;
5252 if Has_Aspects (N) then
5253 Analyze_Aspect_Specifications (N, Act_Decl_Id);
5257 when Instantiation_Error =>
5258 if Parent_Installed then
5262 if Env_Installed then
5266 Ignore_Pragma_SPARK_Mode := Save_IPSM;
5267 SPARK_Mode := Save_SM;
5268 SPARK_Mode_Pragma := Save_SMP;
5269 end Analyze_Subprogram_Instantiation;
5271 -------------------------
5272 -- Get_Associated_Node --
5273 -------------------------
5275 function Get_Associated_Node (N : Node_Id) return Node_Id is
5279 Assoc := Associated_Node (N);
5281 if Nkind (Assoc) /= Nkind (N) then
5284 elsif Nkind_In (Assoc, N_Aggregate, N_Extension_Aggregate) then
5288 -- If the node is part of an inner generic, it may itself have been
5289 -- remapped into a further generic copy. Associated_Node is otherwise
5290 -- used for the entity of the node, and will be of a different node
5291 -- kind, or else N has been rewritten as a literal or function call.
5293 while Present (Associated_Node (Assoc))
5294 and then Nkind (Associated_Node (Assoc)) = Nkind (Assoc)
5296 Assoc := Associated_Node (Assoc);
5299 -- Follow and additional link in case the final node was rewritten.
5300 -- This can only happen with nested generic units.
5302 if (Nkind (Assoc) = N_Identifier or else Nkind (Assoc) in N_Op)
5303 and then Present (Associated_Node (Assoc))
5304 and then (Nkind_In (Associated_Node (Assoc), N_Function_Call,
5305 N_Explicit_Dereference,
5310 Assoc := Associated_Node (Assoc);
5313 -- An additional special case: an unconstrained type in an object
5314 -- declaration may have been rewritten as a local subtype constrained
5315 -- by the expression in the declaration. We need to recover the
5316 -- original entity which may be global.
5318 if Present (Original_Node (Assoc))
5319 and then Nkind (Parent (N)) = N_Object_Declaration
5321 Assoc := Original_Node (Assoc);
5326 end Get_Associated_Node;
5328 -------------------------------------------
5329 -- Build_Instance_Compilation_Unit_Nodes --
5330 -------------------------------------------
5332 procedure Build_Instance_Compilation_Unit_Nodes
5337 Decl_Cunit : Node_Id;
5338 Body_Cunit : Node_Id;
5340 New_Main : constant Entity_Id := Defining_Entity (Act_Decl);
5341 Old_Main : constant Entity_Id := Cunit_Entity (Main_Unit);
5344 -- A new compilation unit node is built for the instance declaration
5347 Make_Compilation_Unit (Sloc (N),
5348 Context_Items => Empty_List,
5350 Aux_Decls_Node => Make_Compilation_Unit_Aux (Sloc (N)));
5352 Set_Parent_Spec (Act_Decl, Parent_Spec (N));
5354 -- The new compilation unit is linked to its body, but both share the
5355 -- same file, so we do not set Body_Required on the new unit so as not
5356 -- to create a spurious dependency on a non-existent body in the ali.
5357 -- This simplifies CodePeer unit traversal.
5359 -- We use the original instantiation compilation unit as the resulting
5360 -- compilation unit of the instance, since this is the main unit.
5362 Rewrite (N, Act_Body);
5364 -- Propagate the aspect specifications from the package body template to
5365 -- the instantiated version of the package body.
5367 if Has_Aspects (Act_Body) then
5368 Set_Aspect_Specifications
5369 (N, New_Copy_List_Tree (Aspect_Specifications (Act_Body)));
5372 Body_Cunit := Parent (N);
5374 -- The two compilation unit nodes are linked by the Library_Unit field
5376 Set_Library_Unit (Decl_Cunit, Body_Cunit);
5377 Set_Library_Unit (Body_Cunit, Decl_Cunit);
5379 -- Preserve the private nature of the package if needed
5381 Set_Private_Present (Decl_Cunit, Private_Present (Body_Cunit));
5383 -- If the instance is not the main unit, its context, categorization
5384 -- and elaboration entity are not relevant to the compilation.
5386 if Body_Cunit /= Cunit (Main_Unit) then
5387 Make_Instance_Unit (Body_Cunit, In_Main => False);
5391 -- The context clause items on the instantiation, which are now attached
5392 -- to the body compilation unit (since the body overwrote the original
5393 -- instantiation node), semantically belong on the spec, so copy them
5394 -- there. It's harmless to leave them on the body as well. In fact one
5395 -- could argue that they belong in both places.
5397 Citem := First (Context_Items (Body_Cunit));
5398 while Present (Citem) loop
5399 Append (New_Copy (Citem), Context_Items (Decl_Cunit));
5403 -- Propagate categorization flags on packages, so that they appear in
5404 -- the ali file for the spec of the unit.
5406 if Ekind (New_Main) = E_Package then
5407 Set_Is_Pure (Old_Main, Is_Pure (New_Main));
5408 Set_Is_Preelaborated (Old_Main, Is_Preelaborated (New_Main));
5409 Set_Is_Remote_Types (Old_Main, Is_Remote_Types (New_Main));
5410 Set_Is_Shared_Passive (Old_Main, Is_Shared_Passive (New_Main));
5411 Set_Is_Remote_Call_Interface
5412 (Old_Main, Is_Remote_Call_Interface (New_Main));
5415 -- Make entry in Units table, so that binder can generate call to
5416 -- elaboration procedure for body, if any.
5418 Make_Instance_Unit (Body_Cunit, In_Main => True);
5419 Main_Unit_Entity := New_Main;
5420 Set_Cunit_Entity (Main_Unit, Main_Unit_Entity);
5422 -- Build elaboration entity, since the instance may certainly generate
5423 -- elaboration code requiring a flag for protection.
5425 Build_Elaboration_Entity (Decl_Cunit, New_Main);
5426 end Build_Instance_Compilation_Unit_Nodes;
5428 -----------------------------
5429 -- Check_Access_Definition --
5430 -----------------------------
5432 procedure Check_Access_Definition (N : Node_Id) is
5435 (Ada_Version >= Ada_2005 and then Present (Access_Definition (N)));
5437 end Check_Access_Definition;
5439 -----------------------------------
5440 -- Check_Formal_Package_Instance --
5441 -----------------------------------
5443 -- If the formal has specific parameters, they must match those of the
5444 -- actual. Both of them are instances, and the renaming declarations for
5445 -- their formal parameters appear in the same order in both. The analyzed
5446 -- formal has been analyzed in the context of the current instance.
5448 procedure Check_Formal_Package_Instance
5449 (Formal_Pack : Entity_Id;
5450 Actual_Pack : Entity_Id)
5452 E1 : Entity_Id := First_Entity (Actual_Pack);
5453 E2 : Entity_Id := First_Entity (Formal_Pack);
5458 procedure Check_Mismatch (B : Boolean);
5459 -- Common error routine for mismatch between the parameters of the
5460 -- actual instance and those of the formal package.
5462 function Same_Instantiated_Constant (E1, E2 : Entity_Id) return Boolean;
5463 -- The formal may come from a nested formal package, and the actual may
5464 -- have been constant-folded. To determine whether the two denote the
5465 -- same entity we may have to traverse several definitions to recover
5466 -- the ultimate entity that they refer to.
5468 function Same_Instantiated_Variable (E1, E2 : Entity_Id) return Boolean;
5469 -- Similarly, if the formal comes from a nested formal package, the
5470 -- actual may designate the formal through multiple renamings, which
5471 -- have to be followed to determine the original variable in question.
5473 --------------------
5474 -- Check_Mismatch --
5475 --------------------
5477 procedure Check_Mismatch (B : Boolean) is
5478 Kind : constant Node_Kind := Nkind (Parent (E2));
5481 if Kind = N_Formal_Type_Declaration then
5484 elsif Nkind_In (Kind, N_Formal_Object_Declaration,
5485 N_Formal_Package_Declaration)
5486 or else Kind in N_Formal_Subprogram_Declaration
5492 ("actual for & in actual instance does not match formal",
5493 Parent (Actual_Pack), E1);
5497 --------------------------------
5498 -- Same_Instantiated_Constant --
5499 --------------------------------
5501 function Same_Instantiated_Constant
5502 (E1, E2 : Entity_Id) return Boolean
5508 while Present (Ent) loop
5512 elsif Ekind (Ent) /= E_Constant then
5515 elsif Is_Entity_Name (Constant_Value (Ent)) then
5516 if Entity (Constant_Value (Ent)) = E1 then
5519 Ent := Entity (Constant_Value (Ent));
5522 -- The actual may be a constant that has been folded. Recover
5525 elsif Is_Entity_Name (Original_Node (Constant_Value (Ent))) then
5526 Ent := Entity (Original_Node (Constant_Value (Ent)));
5533 end Same_Instantiated_Constant;
5535 --------------------------------
5536 -- Same_Instantiated_Variable --
5537 --------------------------------
5539 function Same_Instantiated_Variable
5540 (E1, E2 : Entity_Id) return Boolean
5542 function Original_Entity (E : Entity_Id) return Entity_Id;
5543 -- Follow chain of renamings to the ultimate ancestor
5545 ---------------------
5546 -- Original_Entity --
5547 ---------------------
5549 function Original_Entity (E : Entity_Id) return Entity_Id is
5554 while Nkind (Parent (Orig)) = N_Object_Renaming_Declaration
5555 and then Present (Renamed_Object (Orig))
5556 and then Is_Entity_Name (Renamed_Object (Orig))
5558 Orig := Entity (Renamed_Object (Orig));
5562 end Original_Entity;
5564 -- Start of processing for Same_Instantiated_Variable
5567 return Ekind (E1) = Ekind (E2)
5568 and then Original_Entity (E1) = Original_Entity (E2);
5569 end Same_Instantiated_Variable;
5571 -- Start of processing for Check_Formal_Package_Instance
5575 and then Present (E2)
5577 exit when Ekind (E1) = E_Package
5578 and then Renamed_Entity (E1) = Renamed_Entity (Actual_Pack);
5580 -- If the formal is the renaming of the formal package, this
5581 -- is the end of its formal part, which may occur before the
5582 -- end of the formal part in the actual in the presence of
5583 -- defaulted parameters in the formal package.
5585 exit when Nkind (Parent (E2)) = N_Package_Renaming_Declaration
5586 and then Renamed_Entity (E2) = Scope (E2);
5588 -- The analysis of the actual may generate additional internal
5589 -- entities. If the formal is defaulted, there is no corresponding
5590 -- analysis and the internal entities must be skipped, until we
5591 -- find corresponding entities again.
5593 if Comes_From_Source (E2)
5594 and then not Comes_From_Source (E1)
5595 and then Chars (E1) /= Chars (E2)
5598 and then Chars (E1) /= Chars (E2)
5607 -- If the formal entity comes from a formal declaration, it was
5608 -- defaulted in the formal package, and no check is needed on it.
5610 elsif Nkind (Parent (E2)) = N_Formal_Object_Declaration then
5613 -- Ditto for defaulted formal subprograms.
5615 elsif Is_Overloadable (E1)
5616 and then Nkind (Unit_Declaration_Node (E2)) in
5617 N_Formal_Subprogram_Declaration
5621 elsif Is_Type (E1) then
5623 -- Subtypes must statically match. E1, E2 are the local entities
5624 -- that are subtypes of the actuals. Itypes generated for other
5625 -- parameters need not be checked, the check will be performed
5626 -- on the parameters themselves.
5628 -- If E2 is a formal type declaration, it is a defaulted parameter
5629 -- and needs no checking.
5631 if not Is_Itype (E1)
5632 and then not Is_Itype (E2)
5636 or else Etype (E1) /= Etype (E2)
5637 or else not Subtypes_Statically_Match (E1, E2));
5640 elsif Ekind (E1) = E_Constant then
5642 -- IN parameters must denote the same static value, or the same
5643 -- constant, or the literal null.
5645 Expr1 := Expression (Parent (E1));
5647 if Ekind (E2) /= E_Constant then
5648 Check_Mismatch (True);
5651 Expr2 := Expression (Parent (E2));
5654 if Is_OK_Static_Expression (Expr1) then
5655 if not Is_OK_Static_Expression (Expr2) then
5656 Check_Mismatch (True);
5658 elsif Is_Discrete_Type (Etype (E1)) then
5660 V1 : constant Uint := Expr_Value (Expr1);
5661 V2 : constant Uint := Expr_Value (Expr2);
5663 Check_Mismatch (V1 /= V2);
5666 elsif Is_Real_Type (Etype (E1)) then
5668 V1 : constant Ureal := Expr_Value_R (Expr1);
5669 V2 : constant Ureal := Expr_Value_R (Expr2);
5671 Check_Mismatch (V1 /= V2);
5674 elsif Is_String_Type (Etype (E1))
5675 and then Nkind (Expr1) = N_String_Literal
5677 if Nkind (Expr2) /= N_String_Literal then
5678 Check_Mismatch (True);
5681 (not String_Equal (Strval (Expr1), Strval (Expr2)));
5685 elsif Is_Entity_Name (Expr1) then
5686 if Is_Entity_Name (Expr2) then
5687 if Entity (Expr1) = Entity (Expr2) then
5691 (not Same_Instantiated_Constant
5692 (Entity (Expr1), Entity (Expr2)));
5695 Check_Mismatch (True);
5698 elsif Is_Entity_Name (Original_Node (Expr1))
5699 and then Is_Entity_Name (Expr2)
5701 Same_Instantiated_Constant
5702 (Entity (Original_Node (Expr1)), Entity (Expr2))
5706 elsif Nkind (Expr1) = N_Null then
5707 Check_Mismatch (Nkind (Expr1) /= N_Null);
5710 Check_Mismatch (True);
5713 elsif Ekind (E1) = E_Variable then
5714 Check_Mismatch (not Same_Instantiated_Variable (E1, E2));
5716 elsif Ekind (E1) = E_Package then
5718 (Ekind (E1) /= Ekind (E2)
5719 or else Renamed_Object (E1) /= Renamed_Object (E2));
5721 elsif Is_Overloadable (E1) then
5723 -- Verify that the actual subprograms match. Note that actuals
5724 -- that are attributes are rewritten as subprograms. If the
5725 -- subprogram in the formal package is defaulted, no check is
5726 -- needed. Note that this can only happen in Ada 2005 when the
5727 -- formal package can be partially parameterized.
5729 if Nkind (Unit_Declaration_Node (E1)) =
5730 N_Subprogram_Renaming_Declaration
5731 and then From_Default (Unit_Declaration_Node (E1))
5735 -- If the formal package has an "others" box association that
5736 -- covers this formal, there is no need for a check either.
5738 elsif Nkind (Unit_Declaration_Node (E2)) in
5739 N_Formal_Subprogram_Declaration
5740 and then Box_Present (Unit_Declaration_Node (E2))
5744 -- No check needed if subprogram is a defaulted null procedure
5746 elsif No (Alias (E2))
5747 and then Ekind (E2) = E_Procedure
5749 Null_Present (Specification (Unit_Declaration_Node (E2)))
5753 -- Otherwise the actual in the formal and the actual in the
5754 -- instantiation of the formal must match, up to renamings.
5758 (Ekind (E2) /= Ekind (E1) or else (Alias (E1)) /= Alias (E2));
5762 raise Program_Error;
5769 end Check_Formal_Package_Instance;
5771 ---------------------------
5772 -- Check_Formal_Packages --
5773 ---------------------------
5775 procedure Check_Formal_Packages (P_Id : Entity_Id) is
5777 Formal_P : Entity_Id;
5780 -- Iterate through the declarations in the instance, looking for package
5781 -- renaming declarations that denote instances of formal packages. Stop
5782 -- when we find the renaming of the current package itself. The
5783 -- declaration for a formal package without a box is followed by an
5784 -- internal entity that repeats the instantiation.
5786 E := First_Entity (P_Id);
5787 while Present (E) loop
5788 if Ekind (E) = E_Package then
5789 if Renamed_Object (E) = P_Id then
5792 elsif Nkind (Parent (E)) /= N_Package_Renaming_Declaration then
5795 elsif not Box_Present (Parent (Associated_Formal_Package (E))) then
5796 Formal_P := Next_Entity (E);
5797 Check_Formal_Package_Instance (Formal_P, E);
5799 -- After checking, remove the internal validating package. It
5800 -- is only needed for semantic checks, and as it may contain
5801 -- generic formal declarations it should not reach gigi.
5803 Remove (Unit_Declaration_Node (Formal_P));
5809 end Check_Formal_Packages;
5811 ---------------------------------
5812 -- Check_Forward_Instantiation --
5813 ---------------------------------
5815 procedure Check_Forward_Instantiation (Decl : Node_Id) is
5817 Gen_Comp : Entity_Id := Cunit_Entity (Get_Source_Unit (Decl));
5820 -- The instantiation appears before the generic body if we are in the
5821 -- scope of the unit containing the generic, either in its spec or in
5822 -- the package body, and before the generic body.
5824 if Ekind (Gen_Comp) = E_Package_Body then
5825 Gen_Comp := Spec_Entity (Gen_Comp);
5828 if In_Open_Scopes (Gen_Comp)
5829 and then No (Corresponding_Body (Decl))
5834 and then not Is_Compilation_Unit (S)
5835 and then not Is_Child_Unit (S)
5837 if Ekind (S) = E_Package then
5838 Set_Has_Forward_Instantiation (S);
5844 end Check_Forward_Instantiation;
5846 ---------------------------
5847 -- Check_Generic_Actuals --
5848 ---------------------------
5850 -- The visibility of the actuals may be different between the point of
5851 -- generic instantiation and the instantiation of the body.
5853 procedure Check_Generic_Actuals
5854 (Instance : Entity_Id;
5855 Is_Formal_Box : Boolean)
5860 function Denotes_Previous_Actual (Typ : Entity_Id) return Boolean;
5861 -- For a formal that is an array type, the component type is often a
5862 -- previous formal in the same unit. The privacy status of the component
5863 -- type will have been examined earlier in the traversal of the
5864 -- corresponding actuals, and this status should not be modified for
5865 -- the array (sub)type itself. However, if the base type of the array
5866 -- (sub)type is private, its full view must be restored in the body to
5867 -- be consistent with subsequent index subtypes, etc.
5869 -- To detect this case we have to rescan the list of formals, which is
5870 -- usually short enough to ignore the resulting inefficiency.
5872 -----------------------------
5873 -- Denotes_Previous_Actual --
5874 -----------------------------
5876 function Denotes_Previous_Actual (Typ : Entity_Id) return Boolean is
5880 Prev := First_Entity (Instance);
5881 while Present (Prev) loop
5883 and then Nkind (Parent (Prev)) = N_Subtype_Declaration
5884 and then Is_Entity_Name (Subtype_Indication (Parent (Prev)))
5885 and then Entity (Subtype_Indication (Parent (Prev))) = Typ
5898 end Denotes_Previous_Actual;
5900 -- Start of processing for Check_Generic_Actuals
5903 E := First_Entity (Instance);
5904 while Present (E) loop
5906 and then Nkind (Parent (E)) = N_Subtype_Declaration
5907 and then Scope (Etype (E)) /= Instance
5908 and then Is_Entity_Name (Subtype_Indication (Parent (E)))
5910 if Is_Array_Type (E)
5911 and then not Is_Private_Type (Etype (E))
5912 and then Denotes_Previous_Actual (Component_Type (E))
5916 Check_Private_View (Subtype_Indication (Parent (E)));
5919 Set_Is_Generic_Actual_Type (E, True);
5920 Set_Is_Hidden (E, False);
5921 Set_Is_Potentially_Use_Visible (E,
5924 -- We constructed the generic actual type as a subtype of the
5925 -- supplied type. This means that it normally would not inherit
5926 -- subtype specific attributes of the actual, which is wrong for
5927 -- the generic case.
5929 Astype := Ancestor_Subtype (E);
5933 -- This can happen when E is an itype that is the full view of
5934 -- a private type completed, e.g. with a constrained array. In
5935 -- that case, use the first subtype, which will carry size
5936 -- information. The base type itself is unconstrained and will
5939 Astype := First_Subtype (E);
5942 Set_Size_Info (E, (Astype));
5943 Set_RM_Size (E, RM_Size (Astype));
5944 Set_First_Rep_Item (E, First_Rep_Item (Astype));
5946 if Is_Discrete_Or_Fixed_Point_Type (E) then
5947 Set_RM_Size (E, RM_Size (Astype));
5949 -- In nested instances, the base type of an access actual may
5950 -- itself be private, and need to be exchanged.
5952 elsif Is_Access_Type (E)
5953 and then Is_Private_Type (Etype (E))
5956 (New_Occurrence_Of (Etype (E), Sloc (Instance)));
5959 elsif Ekind (E) = E_Package then
5961 -- If this is the renaming for the current instance, we're done.
5962 -- Otherwise it is a formal package. If the corresponding formal
5963 -- was declared with a box, the (instantiations of the) generic
5964 -- formal part are also visible. Otherwise, ignore the entity
5965 -- created to validate the actuals.
5967 if Renamed_Object (E) = Instance then
5970 elsif Nkind (Parent (E)) /= N_Package_Renaming_Declaration then
5973 -- The visibility of a formal of an enclosing generic is already
5976 elsif Denotes_Formal_Package (E) then
5979 elsif Present (Associated_Formal_Package (E))
5980 and then not Is_Generic_Formal (E)
5982 if Box_Present (Parent (Associated_Formal_Package (E))) then
5983 Check_Generic_Actuals (Renamed_Object (E), True);
5986 Check_Generic_Actuals (Renamed_Object (E), False);
5989 Set_Is_Hidden (E, False);
5992 -- If this is a subprogram instance (in a wrapper package) the
5993 -- actual is fully visible.
5995 elsif Is_Wrapper_Package (Instance) then
5996 Set_Is_Hidden (E, False);
5998 -- If the formal package is declared with a box, or if the formal
5999 -- parameter is defaulted, it is visible in the body.
6002 or else Is_Visible_Formal (E)
6004 Set_Is_Hidden (E, False);
6007 if Ekind (E) = E_Constant then
6009 -- If the type of the actual is a private type declared in the
6010 -- enclosing scope of the generic unit, the body of the generic
6011 -- sees the full view of the type (because it has to appear in
6012 -- the corresponding package body). If the type is private now,
6013 -- exchange views to restore the proper visiblity in the instance.
6016 Typ : constant Entity_Id := Base_Type (Etype (E));
6017 -- The type of the actual
6022 Parent_Scope : Entity_Id;
6023 -- The enclosing scope of the generic unit
6026 if Is_Wrapper_Package (Instance) then
6030 (Unit_Declaration_Node
6031 (Related_Instance (Instance))));
6034 Generic_Parent (Package_Specification (Instance));
6037 Parent_Scope := Scope (Gen_Id);
6039 -- The exchange is only needed if the generic is defined
6040 -- within a package which is not a common ancestor of the
6041 -- scope of the instance, and is not already in scope.
6043 if Is_Private_Type (Typ)
6044 and then Scope (Typ) = Parent_Scope
6045 and then Scope (Instance) /= Parent_Scope
6046 and then Ekind (Parent_Scope) = E_Package
6047 and then not Is_Child_Unit (Gen_Id)
6051 -- If the type of the entity is a subtype, it may also have
6052 -- to be made visible, together with the base type of its
6053 -- full view, after exchange.
6055 if Is_Private_Type (Etype (E)) then
6056 Switch_View (Etype (E));
6057 Switch_View (Base_Type (Etype (E)));
6065 end Check_Generic_Actuals;
6067 ------------------------------
6068 -- Check_Generic_Child_Unit --
6069 ------------------------------
6071 procedure Check_Generic_Child_Unit
6073 Parent_Installed : in out Boolean)
6075 Loc : constant Source_Ptr := Sloc (Gen_Id);
6076 Gen_Par : Entity_Id := Empty;
6078 Inst_Par : Entity_Id;
6081 function Find_Generic_Child
6083 Id : Node_Id) return Entity_Id;
6084 -- Search generic parent for possible child unit with the given name
6086 function In_Enclosing_Instance return Boolean;
6087 -- Within an instance of the parent, the child unit may be denoted by
6088 -- a simple name, or an abbreviated expanded name. Examine enclosing
6089 -- scopes to locate a possible parent instantiation.
6091 ------------------------
6092 -- Find_Generic_Child --
6093 ------------------------
6095 function Find_Generic_Child
6097 Id : Node_Id) return Entity_Id
6102 -- If entity of name is already set, instance has already been
6103 -- resolved, e.g. in an enclosing instantiation.
6105 if Present (Entity (Id)) then
6106 if Scope (Entity (Id)) = Scop then
6113 E := First_Entity (Scop);
6114 while Present (E) loop
6115 if Chars (E) = Chars (Id)
6116 and then Is_Child_Unit (E)
6118 if Is_Child_Unit (E)
6119 and then not Is_Visible_Lib_Unit (E)
6122 ("generic child unit& is not visible", Gen_Id, E);
6134 end Find_Generic_Child;
6136 ---------------------------
6137 -- In_Enclosing_Instance --
6138 ---------------------------
6140 function In_Enclosing_Instance return Boolean is
6141 Enclosing_Instance : Node_Id;
6142 Instance_Decl : Node_Id;
6145 -- We do not inline any call that contains instantiations, except
6146 -- for instantiations of Unchecked_Conversion, so if we are within
6147 -- an inlined body the current instance does not require parents.
6149 if In_Inlined_Body then
6150 pragma Assert (Chars (Gen_Id) = Name_Unchecked_Conversion);
6154 -- Loop to check enclosing scopes
6156 Enclosing_Instance := Current_Scope;
6157 while Present (Enclosing_Instance) loop
6158 Instance_Decl := Unit_Declaration_Node (Enclosing_Instance);
6160 if Ekind (Enclosing_Instance) = E_Package
6161 and then Is_Generic_Instance (Enclosing_Instance)
6163 (Generic_Parent (Specification (Instance_Decl)))
6165 -- Check whether the generic we are looking for is a child of
6168 E := Find_Generic_Child
6169 (Generic_Parent (Specification (Instance_Decl)), Gen_Id);
6170 exit when Present (E);
6176 Enclosing_Instance := Scope (Enclosing_Instance);
6188 Make_Expanded_Name (Loc,
6190 Prefix => New_Occurrence_Of (Enclosing_Instance, Loc),
6191 Selector_Name => New_Occurrence_Of (E, Loc)));
6193 Set_Entity (Gen_Id, E);
6194 Set_Etype (Gen_Id, Etype (E));
6195 Parent_Installed := False; -- Already in scope.
6198 end In_Enclosing_Instance;
6200 -- Start of processing for Check_Generic_Child_Unit
6203 -- If the name of the generic is given by a selected component, it may
6204 -- be the name of a generic child unit, and the prefix is the name of an
6205 -- instance of the parent, in which case the child unit must be visible.
6206 -- If this instance is not in scope, it must be placed there and removed
6207 -- after instantiation, because what is being instantiated is not the
6208 -- original child, but the corresponding child present in the instance
6211 -- If the child is instantiated within the parent, it can be given by
6212 -- a simple name. In this case the instance is already in scope, but
6213 -- the child generic must be recovered from the generic parent as well.
6215 if Nkind (Gen_Id) = N_Selected_Component then
6216 S := Selector_Name (Gen_Id);
6217 Analyze (Prefix (Gen_Id));
6218 Inst_Par := Entity (Prefix (Gen_Id));
6220 if Ekind (Inst_Par) = E_Package
6221 and then Present (Renamed_Object (Inst_Par))
6223 Inst_Par := Renamed_Object (Inst_Par);
6226 if Ekind (Inst_Par) = E_Package then
6227 if Nkind (Parent (Inst_Par)) = N_Package_Specification then
6228 Gen_Par := Generic_Parent (Parent (Inst_Par));
6230 elsif Nkind (Parent (Inst_Par)) = N_Defining_Program_Unit_Name
6232 Nkind (Parent (Parent (Inst_Par))) = N_Package_Specification
6234 Gen_Par := Generic_Parent (Parent (Parent (Inst_Par)));
6237 elsif Ekind (Inst_Par) = E_Generic_Package
6238 and then Nkind (Parent (Gen_Id)) = N_Formal_Package_Declaration
6240 -- A formal package may be a real child package, and not the
6241 -- implicit instance within a parent. In this case the child is
6242 -- not visible and has to be retrieved explicitly as well.
6244 Gen_Par := Inst_Par;
6247 if Present (Gen_Par) then
6249 -- The prefix denotes an instantiation. The entity itself may be a
6250 -- nested generic, or a child unit.
6252 E := Find_Generic_Child (Gen_Par, S);
6255 Change_Selected_Component_To_Expanded_Name (Gen_Id);
6256 Set_Entity (Gen_Id, E);
6257 Set_Etype (Gen_Id, Etype (E));
6259 Set_Etype (S, Etype (E));
6261 -- Indicate that this is a reference to the parent
6263 if In_Extended_Main_Source_Unit (Gen_Id) then
6264 Set_Is_Instantiated (Inst_Par);
6267 -- A common mistake is to replicate the naming scheme of a
6268 -- hierarchy by instantiating a generic child directly, rather
6269 -- than the implicit child in a parent instance:
6271 -- generic .. package Gpar is ..
6272 -- generic .. package Gpar.Child is ..
6273 -- package Par is new Gpar ();
6276 -- package Par.Child is new Gpar.Child ();
6277 -- rather than Par.Child
6279 -- In this case the instantiation is within Par, which is an
6280 -- instance, but Gpar does not denote Par because we are not IN
6281 -- the instance of Gpar, so this is illegal. The test below
6282 -- recognizes this particular case.
6284 if Is_Child_Unit (E)
6285 and then not Comes_From_Source (Entity (Prefix (Gen_Id)))
6286 and then (not In_Instance
6287 or else Nkind (Parent (Parent (Gen_Id))) =
6291 ("prefix of generic child unit must be instance of parent",
6295 if not In_Open_Scopes (Inst_Par)
6296 and then Nkind (Parent (Gen_Id)) not in
6297 N_Generic_Renaming_Declaration
6299 Install_Parent (Inst_Par);
6300 Parent_Installed := True;
6302 elsif In_Open_Scopes (Inst_Par) then
6304 -- If the parent is already installed, install the actuals
6305 -- for its formal packages. This is necessary when the child
6306 -- instance is a child of the parent instance: in this case,
6307 -- the parent is placed on the scope stack but the formal
6308 -- packages are not made visible.
6310 Install_Formal_Packages (Inst_Par);
6314 -- If the generic parent does not contain an entity that
6315 -- corresponds to the selector, the instance doesn't either.
6316 -- Analyzing the node will yield the appropriate error message.
6317 -- If the entity is not a child unit, then it is an inner
6318 -- generic in the parent.
6326 if Is_Child_Unit (Entity (Gen_Id))
6328 Nkind (Parent (Gen_Id)) not in N_Generic_Renaming_Declaration
6329 and then not In_Open_Scopes (Inst_Par)
6331 Install_Parent (Inst_Par);
6332 Parent_Installed := True;
6334 -- The generic unit may be the renaming of the implicit child
6335 -- present in an instance. In that case the parent instance is
6336 -- obtained from the name of the renamed entity.
6338 elsif Ekind (Entity (Gen_Id)) = E_Generic_Package
6339 and then Present (Renamed_Entity (Entity (Gen_Id)))
6340 and then Is_Child_Unit (Renamed_Entity (Entity (Gen_Id)))
6343 Renamed_Package : constant Node_Id :=
6344 Name (Parent (Entity (Gen_Id)));
6346 if Nkind (Renamed_Package) = N_Expanded_Name then
6347 Inst_Par := Entity (Prefix (Renamed_Package));
6348 Install_Parent (Inst_Par);
6349 Parent_Installed := True;
6355 elsif Nkind (Gen_Id) = N_Expanded_Name then
6357 -- Entity already present, analyze prefix, whose meaning may be
6358 -- an instance in the current context. If it is an instance of
6359 -- a relative within another, the proper parent may still have
6360 -- to be installed, if they are not of the same generation.
6362 Analyze (Prefix (Gen_Id));
6364 -- In the unlikely case that a local declaration hides the name
6365 -- of the parent package, locate it on the homonym chain. If the
6366 -- context is an instance of the parent, the renaming entity is
6369 Inst_Par := Entity (Prefix (Gen_Id));
6370 while Present (Inst_Par)
6371 and then not Is_Package_Or_Generic_Package (Inst_Par)
6373 Inst_Par := Homonym (Inst_Par);
6376 pragma Assert (Present (Inst_Par));
6377 Set_Entity (Prefix (Gen_Id), Inst_Par);
6379 if In_Enclosing_Instance then
6382 elsif Present (Entity (Gen_Id))
6383 and then Is_Child_Unit (Entity (Gen_Id))
6384 and then not In_Open_Scopes (Inst_Par)
6386 Install_Parent (Inst_Par);
6387 Parent_Installed := True;
6390 elsif In_Enclosing_Instance then
6392 -- The child unit is found in some enclosing scope
6399 -- If this is the renaming of the implicit child in a parent
6400 -- instance, recover the parent name and install it.
6402 if Is_Entity_Name (Gen_Id) then
6403 E := Entity (Gen_Id);
6405 if Is_Generic_Unit (E)
6406 and then Nkind (Parent (E)) in N_Generic_Renaming_Declaration
6407 and then Is_Child_Unit (Renamed_Object (E))
6408 and then Is_Generic_Unit (Scope (Renamed_Object (E)))
6409 and then Nkind (Name (Parent (E))) = N_Expanded_Name
6412 New_Copy_Tree (Name (Parent (E))));
6413 Inst_Par := Entity (Prefix (Gen_Id));
6415 if not In_Open_Scopes (Inst_Par) then
6416 Install_Parent (Inst_Par);
6417 Parent_Installed := True;
6420 -- If it is a child unit of a non-generic parent, it may be
6421 -- use-visible and given by a direct name. Install parent as
6424 elsif Is_Generic_Unit (E)
6425 and then Is_Child_Unit (E)
6427 Nkind (Parent (Gen_Id)) not in N_Generic_Renaming_Declaration
6428 and then not Is_Generic_Unit (Scope (E))
6430 if not In_Open_Scopes (Scope (E)) then
6431 Install_Parent (Scope (E));
6432 Parent_Installed := True;
6437 end Check_Generic_Child_Unit;
6439 -----------------------------
6440 -- Check_Hidden_Child_Unit --
6441 -----------------------------
6443 procedure Check_Hidden_Child_Unit
6445 Gen_Unit : Entity_Id;
6446 Act_Decl_Id : Entity_Id)
6448 Gen_Id : constant Node_Id := Name (N);
6451 if Is_Child_Unit (Gen_Unit)
6452 and then Is_Child_Unit (Act_Decl_Id)
6453 and then Nkind (Gen_Id) = N_Expanded_Name
6454 and then Entity (Prefix (Gen_Id)) = Scope (Act_Decl_Id)
6455 and then Chars (Gen_Unit) = Chars (Act_Decl_Id)
6457 Error_Msg_Node_2 := Scope (Act_Decl_Id);
6459 ("generic unit & is implicitly declared in &",
6460 Defining_Unit_Name (N), Gen_Unit);
6461 Error_Msg_N ("\instance must have different name",
6462 Defining_Unit_Name (N));
6464 end Check_Hidden_Child_Unit;
6466 ------------------------
6467 -- Check_Private_View --
6468 ------------------------
6470 procedure Check_Private_View (N : Node_Id) is
6471 T : constant Entity_Id := Etype (N);
6475 -- Exchange views if the type was not private in the generic but is
6476 -- private at the point of instantiation. Do not exchange views if
6477 -- the scope of the type is in scope. This can happen if both generic
6478 -- and instance are sibling units, or if type is defined in a parent.
6479 -- In this case the visibility of the type will be correct for all
6483 BT := Base_Type (T);
6485 if Is_Private_Type (T)
6486 and then not Has_Private_View (N)
6487 and then Present (Full_View (T))
6488 and then not In_Open_Scopes (Scope (T))
6490 -- In the generic, the full type was visible. Save the private
6491 -- entity, for subsequent exchange.
6495 elsif Has_Private_View (N)
6496 and then not Is_Private_Type (T)
6497 and then not Has_Been_Exchanged (T)
6498 and then Etype (Get_Associated_Node (N)) /= T
6500 -- Only the private declaration was visible in the generic. If
6501 -- the type appears in a subtype declaration, the subtype in the
6502 -- instance must have a view compatible with that of its parent,
6503 -- which must be exchanged (see corresponding code in Restore_
6504 -- Private_Views). Otherwise, if the type is defined in a parent
6505 -- unit, leave full visibility within instance, which is safe.
6507 if In_Open_Scopes (Scope (Base_Type (T)))
6508 and then not Is_Private_Type (Base_Type (T))
6509 and then Comes_From_Source (Base_Type (T))
6513 elsif Nkind (Parent (N)) = N_Subtype_Declaration
6514 or else not In_Private_Part (Scope (Base_Type (T)))
6516 Prepend_Elmt (T, Exchanged_Views);
6517 Exchange_Declarations (Etype (Get_Associated_Node (N)));
6520 -- For composite types with inconsistent representation exchange
6521 -- component types accordingly.
6523 elsif Is_Access_Type (T)
6524 and then Is_Private_Type (Designated_Type (T))
6525 and then not Has_Private_View (N)
6526 and then Present (Full_View (Designated_Type (T)))
6528 Switch_View (Designated_Type (T));
6530 elsif Is_Array_Type (T) then
6531 if Is_Private_Type (Component_Type (T))
6532 and then not Has_Private_View (N)
6533 and then Present (Full_View (Component_Type (T)))
6535 Switch_View (Component_Type (T));
6538 -- The normal exchange mechanism relies on the setting of a
6539 -- flag on the reference in the generic. However, an additional
6540 -- mechanism is needed for types that are not explicitly
6541 -- mentioned in the generic, but may be needed in expanded code
6542 -- in the instance. This includes component types of arrays and
6543 -- designated types of access types. This processing must also
6544 -- include the index types of arrays which we take care of here.
6551 Indx := First_Index (T);
6552 while Present (Indx) loop
6553 Typ := Base_Type (Etype (Indx));
6555 if Is_Private_Type (Typ)
6556 and then Present (Full_View (Typ))
6565 elsif Is_Private_Type (T)
6566 and then Present (Full_View (T))
6567 and then Is_Array_Type (Full_View (T))
6568 and then Is_Private_Type (Component_Type (Full_View (T)))
6572 -- Finally, a non-private subtype may have a private base type, which
6573 -- must be exchanged for consistency. This can happen when a package
6574 -- body is instantiated, when the scope stack is empty but in fact
6575 -- the subtype and the base type are declared in an enclosing scope.
6577 -- Note that in this case we introduce an inconsistency in the view
6578 -- set, because we switch the base type BT, but there could be some
6579 -- private dependent subtypes of BT which remain unswitched. Such
6580 -- subtypes might need to be switched at a later point (see specific
6581 -- provision for that case in Switch_View).
6583 elsif not Is_Private_Type (T)
6584 and then not Has_Private_View (N)
6585 and then Is_Private_Type (BT)
6586 and then Present (Full_View (BT))
6587 and then not Is_Generic_Type (BT)
6588 and then not In_Open_Scopes (BT)
6590 Prepend_Elmt (Full_View (BT), Exchanged_Views);
6591 Exchange_Declarations (BT);
6594 end Check_Private_View;
6596 -----------------------------
6597 -- Check_Hidden_Primitives --
6598 -----------------------------
6600 function Check_Hidden_Primitives (Assoc_List : List_Id) return Elist_Id is
6603 Result : Elist_Id := No_Elist;
6606 if No (Assoc_List) then
6610 -- Traverse the list of associations between formals and actuals
6611 -- searching for renamings of tagged types
6613 Actual := First (Assoc_List);
6614 while Present (Actual) loop
6615 if Nkind (Actual) = N_Subtype_Declaration then
6616 Gen_T := Generic_Parent_Type (Actual);
6619 and then Is_Tagged_Type (Gen_T)
6621 -- Traverse the list of primitives of the actual types
6622 -- searching for hidden primitives that are visible in the
6623 -- corresponding generic formal; leave them visible and
6624 -- append them to Result to restore their decoration later.
6626 Install_Hidden_Primitives
6627 (Prims_List => Result,
6629 Act_T => Entity (Subtype_Indication (Actual)));
6637 end Check_Hidden_Primitives;
6639 --------------------------
6640 -- Contains_Instance_Of --
6641 --------------------------
6643 function Contains_Instance_Of
6646 N : Node_Id) return Boolean
6654 -- Verify that there are no circular instantiations. We check whether
6655 -- the unit contains an instance of the current scope or some enclosing
6656 -- scope (in case one of the instances appears in a subunit). Longer
6657 -- circularities involving subunits might seem too pathological to
6658 -- consider, but they were not too pathological for the authors of
6659 -- DEC bc30vsq, so we loop over all enclosing scopes, and mark all
6660 -- enclosing generic scopes as containing an instance.
6663 -- Within a generic subprogram body, the scope is not generic, to
6664 -- allow for recursive subprograms. Use the declaration to determine
6665 -- whether this is a generic unit.
6667 if Ekind (Scop) = E_Generic_Package
6668 or else (Is_Subprogram (Scop)
6669 and then Nkind (Unit_Declaration_Node (Scop)) =
6670 N_Generic_Subprogram_Declaration)
6672 Elmt := First_Elmt (Inner_Instances (Inner));
6674 while Present (Elmt) loop
6675 if Node (Elmt) = Scop then
6676 Error_Msg_Node_2 := Inner;
6678 ("circular Instantiation: & instantiated within &!",
6682 elsif Node (Elmt) = Inner then
6685 elsif Contains_Instance_Of (Node (Elmt), Scop, N) then
6686 Error_Msg_Node_2 := Inner;
6688 ("circular Instantiation: & instantiated within &!",
6696 -- Indicate that Inner is being instantiated within Scop
6698 Append_Elmt (Inner, Inner_Instances (Scop));
6701 if Scop = Standard_Standard then
6704 Scop := Scope (Scop);
6709 end Contains_Instance_Of;
6711 -----------------------
6712 -- Copy_Generic_Node --
6713 -----------------------
6715 function Copy_Generic_Node
6717 Parent_Id : Node_Id;
6718 Instantiating : Boolean) return Node_Id
6723 function Copy_Generic_Descendant (D : Union_Id) return Union_Id;
6724 -- Check the given value of one of the Fields referenced by the current
6725 -- node to determine whether to copy it recursively. The field may hold
6726 -- a Node_Id, a List_Id, or an Elist_Id, or a plain value (Sloc, Uint,
6727 -- Char) in which case it need not be copied.
6729 procedure Copy_Descendants;
6730 -- Common utility for various nodes
6732 function Copy_Generic_Elist (E : Elist_Id) return Elist_Id;
6733 -- Make copy of element list
6735 function Copy_Generic_List
6737 Parent_Id : Node_Id) return List_Id;
6738 -- Apply Copy_Node recursively to the members of a node list
6740 function In_Defining_Unit_Name (Nam : Node_Id) return Boolean;
6741 -- True if an identifier is part of the defining program unit name of
6742 -- a child unit. The entity of such an identifier must be kept (for
6743 -- ASIS use) even though as the name of an enclosing generic it would
6744 -- otherwise not be preserved in the generic tree.
6746 ----------------------
6747 -- Copy_Descendants --
6748 ----------------------
6750 procedure Copy_Descendants is
6752 use Atree.Unchecked_Access;
6753 -- This code section is part of the implementation of an untyped
6754 -- tree traversal, so it needs direct access to node fields.
6757 Set_Field1 (New_N, Copy_Generic_Descendant (Field1 (N)));
6758 Set_Field2 (New_N, Copy_Generic_Descendant (Field2 (N)));
6759 Set_Field3 (New_N, Copy_Generic_Descendant (Field3 (N)));
6760 Set_Field4 (New_N, Copy_Generic_Descendant (Field4 (N)));
6761 Set_Field5 (New_N, Copy_Generic_Descendant (Field5 (N)));
6762 end Copy_Descendants;
6764 -----------------------------
6765 -- Copy_Generic_Descendant --
6766 -----------------------------
6768 function Copy_Generic_Descendant (D : Union_Id) return Union_Id is
6770 if D = Union_Id (Empty) then
6773 elsif D in Node_Range then
6775 (Copy_Generic_Node (Node_Id (D), New_N, Instantiating));
6777 elsif D in List_Range then
6778 return Union_Id (Copy_Generic_List (List_Id (D), New_N));
6780 elsif D in Elist_Range then
6781 return Union_Id (Copy_Generic_Elist (Elist_Id (D)));
6783 -- Nothing else is copyable (e.g. Uint values), return as is
6788 end Copy_Generic_Descendant;
6790 ------------------------
6791 -- Copy_Generic_Elist --
6792 ------------------------
6794 function Copy_Generic_Elist (E : Elist_Id) return Elist_Id is
6801 M := First_Elmt (E);
6802 while Present (M) loop
6804 (Copy_Generic_Node (Node (M), Empty, Instantiating), L);
6813 end Copy_Generic_Elist;
6815 -----------------------
6816 -- Copy_Generic_List --
6817 -----------------------
6819 function Copy_Generic_List
6821 Parent_Id : Node_Id) return List_Id
6829 Set_Parent (New_L, Parent_Id);
6832 while Present (N) loop
6833 Append (Copy_Generic_Node (N, Empty, Instantiating), New_L);
6842 end Copy_Generic_List;
6844 ---------------------------
6845 -- In_Defining_Unit_Name --
6846 ---------------------------
6848 function In_Defining_Unit_Name (Nam : Node_Id) return Boolean is
6850 return Present (Parent (Nam))
6851 and then (Nkind (Parent (Nam)) = N_Defining_Program_Unit_Name
6853 (Nkind (Parent (Nam)) = N_Expanded_Name
6854 and then In_Defining_Unit_Name (Parent (Nam))));
6855 end In_Defining_Unit_Name;
6857 -- Start of processing for Copy_Generic_Node
6864 New_N := New_Copy (N);
6866 -- Copy aspects if present
6868 if Has_Aspects (N) then
6869 Set_Has_Aspects (New_N, False);
6870 Set_Aspect_Specifications
6871 (New_N, Copy_Generic_List (Aspect_Specifications (N), Parent_Id));
6874 if Instantiating then
6875 Adjust_Instantiation_Sloc (New_N, S_Adjustment);
6878 if not Is_List_Member (N) then
6879 Set_Parent (New_N, Parent_Id);
6882 -- If defining identifier, then all fields have been copied already
6884 if Nkind (New_N) in N_Entity then
6887 -- Special casing for identifiers and other entity names and operators
6889 elsif Nkind_In (New_N, N_Identifier,
6890 N_Character_Literal,
6893 or else Nkind (New_N) in N_Op
6895 if not Instantiating then
6897 -- Link both nodes in order to assign subsequently the entity of
6898 -- the copy to the original node, in case this is a global
6901 Set_Associated_Node (N, New_N);
6903 -- If we are within an instantiation, this is a nested generic
6904 -- that has already been analyzed at the point of definition.
6905 -- We must preserve references that were global to the enclosing
6906 -- parent at that point. Other occurrences, whether global or
6907 -- local to the current generic, must be resolved anew, so we
6908 -- reset the entity in the generic copy. A global reference has a
6909 -- smaller depth than the parent, or else the same depth in case
6910 -- both are distinct compilation units.
6912 -- A child unit is implicitly declared within the enclosing parent
6913 -- but is in fact global to it, and must be preserved.
6915 -- It is also possible for Current_Instantiated_Parent to be
6916 -- defined, and for this not to be a nested generic, namely if
6917 -- the unit is loaded through Rtsfind. In that case, the entity of
6918 -- New_N is only a link to the associated node, and not a defining
6921 -- The entities for parent units in the defining_program_unit of a
6922 -- generic child unit are established when the context of the unit
6923 -- is first analyzed, before the generic copy is made. They are
6924 -- preserved in the copy for use in ASIS queries.
6926 Ent := Entity (New_N);
6928 if No (Current_Instantiated_Parent.Gen_Id) then
6930 or else Nkind (Ent) /= N_Defining_Identifier
6931 or else not In_Defining_Unit_Name (N)
6933 Set_Associated_Node (New_N, Empty);
6938 not Nkind_In (Ent, N_Defining_Identifier,
6939 N_Defining_Character_Literal,
6940 N_Defining_Operator_Symbol)
6941 or else No (Scope (Ent))
6943 (Scope (Ent) = Current_Instantiated_Parent.Gen_Id
6944 and then not Is_Child_Unit (Ent))
6946 (Scope_Depth (Scope (Ent)) >
6947 Scope_Depth (Current_Instantiated_Parent.Gen_Id)
6949 Get_Source_Unit (Ent) =
6950 Get_Source_Unit (Current_Instantiated_Parent.Gen_Id))
6952 Set_Associated_Node (New_N, Empty);
6955 -- Case of instantiating identifier or some other name or operator
6958 -- If the associated node is still defined, the entity in it
6959 -- is global, and must be copied to the instance. If this copy
6960 -- is being made for a body to inline, it is applied to an
6961 -- instantiated tree, and the entity is already present and
6962 -- must be also preserved.
6965 Assoc : constant Node_Id := Get_Associated_Node (N);
6968 if Present (Assoc) then
6969 if Nkind (Assoc) = Nkind (N) then
6970 Set_Entity (New_N, Entity (Assoc));
6971 Check_Private_View (N);
6973 -- The name in the call may be a selected component if the
6974 -- call has not been analyzed yet, as may be the case for
6975 -- pre/post conditions in a generic unit.
6977 elsif Nkind (Assoc) = N_Function_Call
6978 and then Is_Entity_Name (Name (Assoc))
6980 Set_Entity (New_N, Entity (Name (Assoc)));
6982 elsif Nkind_In (Assoc, N_Defining_Identifier,
6983 N_Defining_Character_Literal,
6984 N_Defining_Operator_Symbol)
6985 and then Expander_Active
6987 -- Inlining case: we are copying a tree that contains
6988 -- global entities, which are preserved in the copy to be
6989 -- used for subsequent inlining.
6994 Set_Entity (New_N, Empty);
7000 -- For expanded name, we must copy the Prefix and Selector_Name
7002 if Nkind (N) = N_Expanded_Name then
7004 (New_N, Copy_Generic_Node (Prefix (N), New_N, Instantiating));
7006 Set_Selector_Name (New_N,
7007 Copy_Generic_Node (Selector_Name (N), New_N, Instantiating));
7009 -- For operators, we must copy the right operand
7011 elsif Nkind (N) in N_Op then
7012 Set_Right_Opnd (New_N,
7013 Copy_Generic_Node (Right_Opnd (N), New_N, Instantiating));
7015 -- And for binary operators, the left operand as well
7017 if Nkind (N) in N_Binary_Op then
7018 Set_Left_Opnd (New_N,
7019 Copy_Generic_Node (Left_Opnd (N), New_N, Instantiating));
7023 -- Special casing for stubs
7025 elsif Nkind (N) in N_Body_Stub then
7027 -- In any case, we must copy the specification or defining
7028 -- identifier as appropriate.
7030 if Nkind (N) = N_Subprogram_Body_Stub then
7031 Set_Specification (New_N,
7032 Copy_Generic_Node (Specification (N), New_N, Instantiating));
7035 Set_Defining_Identifier (New_N,
7037 (Defining_Identifier (N), New_N, Instantiating));
7040 -- If we are not instantiating, then this is where we load and
7041 -- analyze subunits, i.e. at the point where the stub occurs. A
7042 -- more permissive system might defer this analysis to the point
7043 -- of instantiation, but this seems too complicated for now.
7045 if not Instantiating then
7047 Subunit_Name : constant Unit_Name_Type := Get_Unit_Name (N);
7049 Unum : Unit_Number_Type;
7053 -- Make sure that, if it is a subunit of the main unit that is
7054 -- preprocessed and if -gnateG is specified, the preprocessed
7055 -- file will be written.
7057 Lib.Analysing_Subunit_Of_Main :=
7058 Lib.In_Extended_Main_Source_Unit (N);
7061 (Load_Name => Subunit_Name,
7065 Lib.Analysing_Subunit_Of_Main := False;
7067 -- If the proper body is not found, a warning message will be
7068 -- emitted when analyzing the stub, or later at the point of
7069 -- instantiation. Here we just leave the stub as is.
7071 if Unum = No_Unit then
7072 Subunits_Missing := True;
7073 goto Subunit_Not_Found;
7076 Subunit := Cunit (Unum);
7078 if Nkind (Unit (Subunit)) /= N_Subunit then
7080 ("found child unit instead of expected SEPARATE subunit",
7082 Error_Msg_Sloc := Sloc (N);
7083 Error_Msg_N ("\to complete stub #", Subunit);
7084 goto Subunit_Not_Found;
7087 -- We must create a generic copy of the subunit, in order to
7088 -- perform semantic analysis on it, and we must replace the
7089 -- stub in the original generic unit with the subunit, in order
7090 -- to preserve non-local references within.
7092 -- Only the proper body needs to be copied. Library_Unit and
7093 -- context clause are simply inherited by the generic copy.
7094 -- Note that the copy (which may be recursive if there are
7095 -- nested subunits) must be done first, before attaching it to
7096 -- the enclosing generic.
7100 (Proper_Body (Unit (Subunit)),
7101 Empty, Instantiating => False);
7103 -- Now place the original proper body in the original generic
7104 -- unit. This is a body, not a compilation unit.
7106 Rewrite (N, Proper_Body (Unit (Subunit)));
7107 Set_Is_Compilation_Unit (Defining_Entity (N), False);
7108 Set_Was_Originally_Stub (N);
7110 -- Finally replace the body of the subunit with its copy, and
7111 -- make this new subunit into the library unit of the generic
7112 -- copy, which does not have stubs any longer.
7114 Set_Proper_Body (Unit (Subunit), New_Body);
7115 Set_Library_Unit (New_N, Subunit);
7116 Inherit_Context (Unit (Subunit), N);
7119 -- If we are instantiating, this must be an error case, since
7120 -- otherwise we would have replaced the stub node by the proper body
7121 -- that corresponds. So just ignore it in the copy (i.e. we have
7122 -- copied it, and that is good enough).
7128 <<Subunit_Not_Found>> null;
7130 -- If the node is a compilation unit, it is the subunit of a stub, which
7131 -- has been loaded already (see code below). In this case, the library
7132 -- unit field of N points to the parent unit (which is a compilation
7133 -- unit) and need not (and cannot) be copied.
7135 -- When the proper body of the stub is analyzed, the library_unit link
7136 -- is used to establish the proper context (see sem_ch10).
7138 -- The other fields of a compilation unit are copied as usual
7140 elsif Nkind (N) = N_Compilation_Unit then
7142 -- This code can only be executed when not instantiating, because in
7143 -- the copy made for an instantiation, the compilation unit node has
7144 -- disappeared at the point that a stub is replaced by its proper
7147 pragma Assert (not Instantiating);
7149 Set_Context_Items (New_N,
7150 Copy_Generic_List (Context_Items (N), New_N));
7153 Copy_Generic_Node (Unit (N), New_N, False));
7155 Set_First_Inlined_Subprogram (New_N,
7157 (First_Inlined_Subprogram (N), New_N, False));
7159 Set_Aux_Decls_Node (New_N,
7160 Copy_Generic_Node (Aux_Decls_Node (N), New_N, False));
7162 -- For an assignment node, the assignment is known to be semantically
7163 -- legal if we are instantiating the template. This avoids incorrect
7164 -- diagnostics in generated code.
7166 elsif Nkind (N) = N_Assignment_Statement then
7168 -- Copy name and expression fields in usual manner
7171 Copy_Generic_Node (Name (N), New_N, Instantiating));
7173 Set_Expression (New_N,
7174 Copy_Generic_Node (Expression (N), New_N, Instantiating));
7176 if Instantiating then
7177 Set_Assignment_OK (Name (New_N), True);
7180 elsif Nkind_In (N, N_Aggregate, N_Extension_Aggregate) then
7181 if not Instantiating then
7182 Set_Associated_Node (N, New_N);
7185 if Present (Get_Associated_Node (N))
7186 and then Nkind (Get_Associated_Node (N)) = Nkind (N)
7188 -- In the generic the aggregate has some composite type. If at
7189 -- the point of instantiation the type has a private view,
7190 -- install the full view (and that of its ancestors, if any).
7193 T : Entity_Id := (Etype (Get_Associated_Node (New_N)));
7198 and then Is_Private_Type (T)
7204 and then Is_Tagged_Type (T)
7205 and then Is_Derived_Type (T)
7207 Rt := Root_Type (T);
7212 if Is_Private_Type (T) then
7223 -- Do not copy the associated node, which points to the generic copy
7224 -- of the aggregate.
7227 use Atree.Unchecked_Access;
7228 -- This code section is part of the implementation of an untyped
7229 -- tree traversal, so it needs direct access to node fields.
7232 Set_Field1 (New_N, Copy_Generic_Descendant (Field1 (N)));
7233 Set_Field2 (New_N, Copy_Generic_Descendant (Field2 (N)));
7234 Set_Field3 (New_N, Copy_Generic_Descendant (Field3 (N)));
7235 Set_Field5 (New_N, Copy_Generic_Descendant (Field5 (N)));
7238 -- Allocators do not have an identifier denoting the access type, so we
7239 -- must locate it through the expression to check whether the views are
7242 elsif Nkind (N) = N_Allocator
7243 and then Nkind (Expression (N)) = N_Qualified_Expression
7244 and then Is_Entity_Name (Subtype_Mark (Expression (N)))
7245 and then Instantiating
7248 T : constant Node_Id :=
7249 Get_Associated_Node (Subtype_Mark (Expression (N)));
7255 -- Retrieve the allocator node in the generic copy
7257 Acc_T := Etype (Parent (Parent (T)));
7259 and then Is_Private_Type (Acc_T)
7261 Switch_View (Acc_T);
7268 -- For a proper body, we must catch the case of a proper body that
7269 -- replaces a stub. This represents the point at which a separate
7270 -- compilation unit, and hence template file, may be referenced, so we
7271 -- must make a new source instantiation entry for the template of the
7272 -- subunit, and ensure that all nodes in the subunit are adjusted using
7273 -- this new source instantiation entry.
7275 elsif Nkind (N) in N_Proper_Body then
7277 Save_Adjustment : constant Sloc_Adjustment := S_Adjustment;
7280 if Instantiating and then Was_Originally_Stub (N) then
7281 Create_Instantiation_Source
7282 (Instantiation_Node,
7283 Defining_Entity (N),
7288 -- Now copy the fields of the proper body, using the new
7289 -- adjustment factor if one was needed as per test above.
7293 -- Restore the original adjustment factor in case changed
7295 S_Adjustment := Save_Adjustment;
7298 -- Don't copy Ident or Comment pragmas, since the comment belongs to the
7299 -- generic unit, not to the instantiating unit.
7301 elsif Nkind (N) = N_Pragma and then Instantiating then
7303 Prag_Id : constant Pragma_Id := Get_Pragma_Id (N);
7305 if Prag_Id = Pragma_Ident or else Prag_Id = Pragma_Comment then
7306 New_N := Make_Null_Statement (Sloc (N));
7312 elsif Nkind_In (N, N_Integer_Literal, N_Real_Literal) then
7314 -- No descendant fields need traversing
7318 elsif Nkind (N) = N_String_Literal
7319 and then Present (Etype (N))
7320 and then Instantiating
7322 -- If the string is declared in an outer scope, the string_literal
7323 -- subtype created for it may have the wrong scope. We force the
7324 -- reanalysis of the constant to generate a new itype in the proper
7327 Set_Etype (New_N, Empty);
7328 Set_Analyzed (New_N, False);
7330 -- For the remaining nodes, copy their descendants recursively
7335 if Instantiating and then Nkind (N) = N_Subprogram_Body then
7336 Set_Generic_Parent (Specification (New_N), N);
7338 -- Should preserve Corresponding_Spec??? (12.3(14))
7343 end Copy_Generic_Node;
7345 ----------------------------
7346 -- Denotes_Formal_Package --
7347 ----------------------------
7349 function Denotes_Formal_Package
7351 On_Exit : Boolean := False;
7352 Instance : Entity_Id := Empty) return Boolean
7355 Scop : constant Entity_Id := Scope (Pack);
7358 function Is_Actual_Of_Previous_Formal (P : Entity_Id) return Boolean;
7359 -- The package in question may be an actual for a previous formal
7360 -- package P of the current instance, so examine its actuals as well.
7361 -- This must be recursive over other formal packages.
7363 ----------------------------------
7364 -- Is_Actual_Of_Previous_Formal --
7365 ----------------------------------
7367 function Is_Actual_Of_Previous_Formal (P : Entity_Id) return Boolean is
7371 E1 := First_Entity (P);
7372 while Present (E1) and then E1 /= Instance loop
7373 if Ekind (E1) = E_Package
7374 and then Nkind (Parent (E1)) = N_Package_Renaming_Declaration
7376 if Renamed_Object (E1) = Pack then
7379 elsif E1 = P or else Renamed_Object (E1) = P then
7382 elsif Is_Actual_Of_Previous_Formal (E1) then
7391 end Is_Actual_Of_Previous_Formal;
7393 -- Start of processing for Denotes_Formal_Package
7399 (Instance_Envs.Last).Instantiated_Parent.Act_Id;
7401 Par := Current_Instantiated_Parent.Act_Id;
7404 if Ekind (Scop) = E_Generic_Package
7405 or else Nkind (Unit_Declaration_Node (Scop)) =
7406 N_Generic_Subprogram_Declaration
7410 elsif Nkind (Original_Node (Unit_Declaration_Node (Pack))) =
7411 N_Formal_Package_Declaration
7419 -- Check whether this package is associated with a formal package of
7420 -- the enclosing instantiation. Iterate over the list of renamings.
7422 E := First_Entity (Par);
7423 while Present (E) loop
7424 if Ekind (E) /= E_Package
7425 or else Nkind (Parent (E)) /= N_Package_Renaming_Declaration
7429 elsif Renamed_Object (E) = Par then
7432 elsif Renamed_Object (E) = Pack then
7435 elsif Is_Actual_Of_Previous_Formal (E) then
7445 end Denotes_Formal_Package;
7451 procedure End_Generic is
7453 -- ??? More things could be factored out in this routine. Should
7454 -- probably be done at a later stage.
7456 Inside_A_Generic := Generic_Flags.Table (Generic_Flags.Last);
7457 Generic_Flags.Decrement_Last;
7459 Expander_Mode_Restore;
7466 function Earlier (N1, N2 : Node_Id) return Boolean is
7467 procedure Find_Depth (P : in out Node_Id; D : in out Integer);
7468 -- Find distance from given node to enclosing compilation unit
7474 procedure Find_Depth (P : in out Node_Id; D : in out Integer) is
7477 and then Nkind (P) /= N_Compilation_Unit
7479 P := True_Parent (P);
7484 -- Local declarations
7493 -- Start of processing for Earlier
7496 Find_Depth (P1, D1);
7497 Find_Depth (P2, D2);
7507 P1 := True_Parent (P1);
7512 P2 := True_Parent (P2);
7516 -- At this point P1 and P2 are at the same distance from the root.
7517 -- We examine their parents until we find a common declarative list.
7518 -- If we reach the root, N1 and N2 do not descend from the same
7519 -- declarative list (e.g. one is nested in the declarative part and
7520 -- the other is in a block in the statement part) and the earlier
7521 -- one is already frozen.
7523 while not Is_List_Member (P1)
7524 or else not Is_List_Member (P2)
7525 or else List_Containing (P1) /= List_Containing (P2)
7527 P1 := True_Parent (P1);
7528 P2 := True_Parent (P2);
7530 if Nkind (Parent (P1)) = N_Subunit then
7531 P1 := Corresponding_Stub (Parent (P1));
7534 if Nkind (Parent (P2)) = N_Subunit then
7535 P2 := Corresponding_Stub (Parent (P2));
7543 -- Expanded code usually shares the source location of the original
7544 -- construct it was generated for. This however may not necessarely
7545 -- reflect the true location of the code within the tree.
7547 -- Before comparing the slocs of the two nodes, make sure that we are
7548 -- working with correct source locations. Assume that P1 is to the left
7549 -- of P2. If either one does not come from source, traverse the common
7550 -- list heading towards the other node and locate the first source
7554 -- ----+===+===+--------------+===+===+----
7555 -- expanded code expanded code
7557 if not Comes_From_Source (P1) then
7558 while Present (P1) loop
7560 -- Neither P2 nor a source statement were located during the
7561 -- search. If we reach the end of the list, then P1 does not
7562 -- occur earlier than P2.
7565 -- start --- P2 ----- P1 --- end
7567 if No (Next (P1)) then
7570 -- We encounter P2 while going to the right of the list. This
7571 -- means that P1 does indeed appear earlier.
7574 -- start --- P1 ===== P2 --- end
7575 -- expanded code in between
7580 -- No need to look any further since we have located a source
7583 elsif Comes_From_Source (P1) then
7593 if not Comes_From_Source (P2) then
7594 while Present (P2) loop
7596 -- Neither P1 nor a source statement were located during the
7597 -- search. If we reach the start of the list, then P1 does not
7598 -- occur earlier than P2.
7601 -- start --- P2 --- P1 --- end
7603 if No (Prev (P2)) then
7606 -- We encounter P1 while going to the left of the list. This
7607 -- means that P1 does indeed appear earlier.
7610 -- start --- P1 ===== P2 --- end
7611 -- expanded code in between
7616 -- No need to look any further since we have located a source
7619 elsif Comes_From_Source (P2) then
7629 -- At this point either both nodes came from source or we approximated
7630 -- their source locations through neighbouring source statements.
7632 T1 := Top_Level_Location (Sloc (P1));
7633 T2 := Top_Level_Location (Sloc (P2));
7635 -- When two nodes come from the same instance, they have identical top
7636 -- level locations. To determine proper relation within the tree, check
7637 -- their locations within the template.
7640 return Sloc (P1) < Sloc (P2);
7642 -- The two nodes either come from unrelated instances or do not come
7643 -- from instantiated code at all.
7650 ----------------------
7651 -- Find_Actual_Type --
7652 ----------------------
7654 function Find_Actual_Type
7656 Gen_Type : Entity_Id) return Entity_Id
7658 Gen_Scope : constant Entity_Id := Scope (Gen_Type);
7662 -- Special processing only applies to child units
7664 if not Is_Child_Unit (Gen_Scope) then
7665 return Get_Instance_Of (Typ);
7667 -- If designated or component type is itself a formal of the child unit,
7668 -- its instance is available.
7670 elsif Scope (Typ) = Gen_Scope then
7671 return Get_Instance_Of (Typ);
7673 -- If the array or access type is not declared in the parent unit,
7674 -- no special processing needed.
7676 elsif not Is_Generic_Type (Typ)
7677 and then Scope (Gen_Scope) /= Scope (Typ)
7679 return Get_Instance_Of (Typ);
7681 -- Otherwise, retrieve designated or component type by visibility
7684 T := Current_Entity (Typ);
7685 while Present (T) loop
7686 if In_Open_Scopes (Scope (T)) then
7689 elsif Is_Generic_Actual_Type (T) then
7698 end Find_Actual_Type;
7700 ----------------------------
7701 -- Freeze_Subprogram_Body --
7702 ----------------------------
7704 procedure Freeze_Subprogram_Body
7705 (Inst_Node : Node_Id;
7707 Pack_Id : Entity_Id)
7709 Gen_Unit : constant Entity_Id := Get_Generic_Entity (Inst_Node);
7710 Par : constant Entity_Id := Scope (Gen_Unit);
7716 function Enclosing_Package_Body (N : Node_Id) return Node_Id;
7717 -- Find innermost package body that encloses the given node, and which
7718 -- is not a compilation unit. Freeze nodes for the instance, or for its
7719 -- enclosing body, may be inserted after the enclosing_body of the
7720 -- generic unit. Used to determine proper placement of freeze node for
7721 -- both package and subprogram instances.
7723 function Package_Freeze_Node (B : Node_Id) return Node_Id;
7724 -- Find entity for given package body, and locate or create a freeze
7727 ----------------------------
7728 -- Enclosing_Package_Body --
7729 ----------------------------
7731 function Enclosing_Package_Body (N : Node_Id) return Node_Id is
7737 and then Nkind (Parent (P)) /= N_Compilation_Unit
7739 if Nkind (P) = N_Package_Body then
7740 if Nkind (Parent (P)) = N_Subunit then
7741 return Corresponding_Stub (Parent (P));
7747 P := True_Parent (P);
7751 end Enclosing_Package_Body;
7753 -------------------------
7754 -- Package_Freeze_Node --
7755 -------------------------
7757 function Package_Freeze_Node (B : Node_Id) return Node_Id is
7761 if Nkind (B) = N_Package_Body then
7762 Id := Corresponding_Spec (B);
7763 else pragma Assert (Nkind (B) = N_Package_Body_Stub);
7764 Id := Corresponding_Spec (Proper_Body (Unit (Library_Unit (B))));
7767 Ensure_Freeze_Node (Id);
7768 return Freeze_Node (Id);
7769 end Package_Freeze_Node;
7771 -- Start of processing of Freeze_Subprogram_Body
7774 -- If the instance and the generic body appear within the same unit, and
7775 -- the instance precedes the generic, the freeze node for the instance
7776 -- must appear after that of the generic. If the generic is nested
7777 -- within another instance I2, then current instance must be frozen
7778 -- after I2. In both cases, the freeze nodes are those of enclosing
7779 -- packages. Otherwise, the freeze node is placed at the end of the
7780 -- current declarative part.
7782 Enc_G := Enclosing_Package_Body (Gen_Body);
7783 Enc_I := Enclosing_Package_Body (Inst_Node);
7784 Ensure_Freeze_Node (Pack_Id);
7785 F_Node := Freeze_Node (Pack_Id);
7787 if Is_Generic_Instance (Par)
7788 and then Present (Freeze_Node (Par))
7789 and then In_Same_Declarative_Part (Freeze_Node (Par), Inst_Node)
7791 -- The parent was a premature instantiation. Insert freeze node at
7792 -- the end the current declarative part.
7794 if ABE_Is_Certain (Get_Package_Instantiation_Node (Par)) then
7795 Insert_Freeze_Node_For_Instance (Inst_Node, F_Node);
7797 -- Handle the following case:
7799 -- package Parent_Inst is new ...
7802 -- procedure P ... -- this body freezes Parent_Inst
7804 -- package Inst is new ...
7806 -- In this particular scenario, the freeze node for Inst must be
7807 -- inserted in the same manner as that of Parent_Inst - before the
7808 -- next source body or at the end of the declarative list (body not
7809 -- available). If body P did not exist and Parent_Inst was frozen
7810 -- after Inst, either by a body following Inst or at the end of the
7811 -- declarative region, the freeze node for Inst must be inserted
7812 -- after that of Parent_Inst. This relation is established by
7813 -- comparing the Slocs of Parent_Inst freeze node and Inst.
7815 elsif List_Containing (Get_Package_Instantiation_Node (Par)) =
7816 List_Containing (Inst_Node)
7817 and then Sloc (Freeze_Node (Par)) < Sloc (Inst_Node)
7819 Insert_Freeze_Node_For_Instance (Inst_Node, F_Node);
7822 Insert_After (Freeze_Node (Par), F_Node);
7825 -- The body enclosing the instance should be frozen after the body that
7826 -- includes the generic, because the body of the instance may make
7827 -- references to entities therein. If the two are not in the same
7828 -- declarative part, or if the one enclosing the instance is frozen
7829 -- already, freeze the instance at the end of the current declarative
7832 elsif Is_Generic_Instance (Par)
7833 and then Present (Freeze_Node (Par))
7834 and then Present (Enc_I)
7836 if In_Same_Declarative_Part (Freeze_Node (Par), Enc_I)
7838 (Nkind (Enc_I) = N_Package_Body
7840 In_Same_Declarative_Part (Freeze_Node (Par), Parent (Enc_I)))
7842 -- The enclosing package may contain several instances. Rather
7843 -- than computing the earliest point at which to insert its freeze
7844 -- node, we place it at the end of the declarative part of the
7845 -- parent of the generic.
7847 Insert_Freeze_Node_For_Instance
7848 (Freeze_Node (Par), Package_Freeze_Node (Enc_I));
7851 Insert_Freeze_Node_For_Instance (Inst_Node, F_Node);
7853 elsif Present (Enc_G)
7854 and then Present (Enc_I)
7855 and then Enc_G /= Enc_I
7856 and then Earlier (Inst_Node, Gen_Body)
7858 if Nkind (Enc_G) = N_Package_Body then
7859 E_G_Id := Corresponding_Spec (Enc_G);
7860 else pragma Assert (Nkind (Enc_G) = N_Package_Body_Stub);
7862 Corresponding_Spec (Proper_Body (Unit (Library_Unit (Enc_G))));
7865 -- Freeze package that encloses instance, and place node after the
7866 -- package that encloses generic. If enclosing package is already
7867 -- frozen we have to assume it is at the proper place. This may be a
7868 -- potential ABE that requires dynamic checking. Do not add a freeze
7869 -- node if the package that encloses the generic is inside the body
7870 -- that encloses the instance, because the freeze node would be in
7871 -- the wrong scope. Additional contortions needed if the bodies are
7872 -- within a subunit.
7875 Enclosing_Body : Node_Id;
7878 if Nkind (Enc_I) = N_Package_Body_Stub then
7879 Enclosing_Body := Proper_Body (Unit (Library_Unit (Enc_I)));
7881 Enclosing_Body := Enc_I;
7884 if Parent (List_Containing (Enc_G)) /= Enclosing_Body then
7885 Insert_Freeze_Node_For_Instance
7886 (Enc_G, Package_Freeze_Node (Enc_I));
7890 -- Freeze enclosing subunit before instance
7892 Ensure_Freeze_Node (E_G_Id);
7894 if not Is_List_Member (Freeze_Node (E_G_Id)) then
7895 Insert_After (Enc_G, Freeze_Node (E_G_Id));
7898 Insert_Freeze_Node_For_Instance (Inst_Node, F_Node);
7901 -- If none of the above, insert freeze node at the end of the current
7902 -- declarative part.
7904 Insert_Freeze_Node_For_Instance (Inst_Node, F_Node);
7906 end Freeze_Subprogram_Body;
7912 function Get_Gen_Id (E : Assoc_Ptr) return Entity_Id is
7914 return Generic_Renamings.Table (E).Gen_Id;
7917 ---------------------
7918 -- Get_Instance_Of --
7919 ---------------------
7921 function Get_Instance_Of (A : Entity_Id) return Entity_Id is
7922 Res : constant Assoc_Ptr := Generic_Renamings_HTable.Get (A);
7925 if Res /= Assoc_Null then
7926 return Generic_Renamings.Table (Res).Act_Id;
7928 -- On exit, entity is not instantiated: not a generic parameter, or
7929 -- else parameter of an inner generic unit.
7933 end Get_Instance_Of;
7935 ------------------------------------
7936 -- Get_Package_Instantiation_Node --
7937 ------------------------------------
7939 function Get_Package_Instantiation_Node (A : Entity_Id) return Node_Id is
7940 Decl : Node_Id := Unit_Declaration_Node (A);
7944 -- If the Package_Instantiation attribute has been set on the package
7945 -- entity, then use it directly when it (or its Original_Node) refers
7946 -- to an N_Package_Instantiation node. In principle it should be
7947 -- possible to have this field set in all cases, which should be
7948 -- investigated, and would allow this function to be significantly
7951 Inst := Package_Instantiation (A);
7953 if Present (Inst) then
7954 if Nkind (Inst) = N_Package_Instantiation then
7957 elsif Nkind (Original_Node (Inst)) = N_Package_Instantiation then
7958 return Original_Node (Inst);
7962 -- If the instantiation is a compilation unit that does not need body
7963 -- then the instantiation node has been rewritten as a package
7964 -- declaration for the instance, and we return the original node.
7966 -- If it is a compilation unit and the instance node has not been
7967 -- rewritten, then it is still the unit of the compilation. Finally, if
7968 -- a body is present, this is a parent of the main unit whose body has
7969 -- been compiled for inlining purposes, and the instantiation node has
7970 -- been rewritten with the instance body.
7972 -- Otherwise the instantiation node appears after the declaration. If
7973 -- the entity is a formal package, the declaration may have been
7974 -- rewritten as a generic declaration (in the case of a formal with box)
7975 -- or left as a formal package declaration if it has actuals, and is
7976 -- found with a forward search.
7978 if Nkind (Parent (Decl)) = N_Compilation_Unit then
7979 if Nkind (Decl) = N_Package_Declaration
7980 and then Present (Corresponding_Body (Decl))
7982 Decl := Unit_Declaration_Node (Corresponding_Body (Decl));
7985 if Nkind (Original_Node (Decl)) = N_Package_Instantiation then
7986 return Original_Node (Decl);
7988 return Unit (Parent (Decl));
7991 elsif Nkind (Decl) = N_Package_Declaration
7992 and then Nkind (Original_Node (Decl)) = N_Formal_Package_Declaration
7994 return Original_Node (Decl);
7997 Inst := Next (Decl);
7998 while not Nkind_In (Inst, N_Package_Instantiation,
7999 N_Formal_Package_Declaration)
8006 end Get_Package_Instantiation_Node;
8008 ------------------------
8009 -- Has_Been_Exchanged --
8010 ------------------------
8012 function Has_Been_Exchanged (E : Entity_Id) return Boolean is
8016 Next := First_Elmt (Exchanged_Views);
8017 while Present (Next) loop
8018 if Full_View (Node (Next)) = E then
8026 end Has_Been_Exchanged;
8032 function Hash (F : Entity_Id) return HTable_Range is
8034 return HTable_Range (F mod HTable_Size);
8037 ------------------------
8038 -- Hide_Current_Scope --
8039 ------------------------
8041 procedure Hide_Current_Scope is
8042 C : constant Entity_Id := Current_Scope;
8046 Set_Is_Hidden_Open_Scope (C);
8048 E := First_Entity (C);
8049 while Present (E) loop
8050 if Is_Immediately_Visible (E) then
8051 Set_Is_Immediately_Visible (E, False);
8052 Append_Elmt (E, Hidden_Entities);
8058 -- Make the scope name invisible as well. This is necessary, but might
8059 -- conflict with calls to Rtsfind later on, in case the scope is a
8060 -- predefined one. There is no clean solution to this problem, so for
8061 -- now we depend on the user not redefining Standard itself in one of
8062 -- the parent units.
8064 if Is_Immediately_Visible (C) and then C /= Standard_Standard then
8065 Set_Is_Immediately_Visible (C, False);
8066 Append_Elmt (C, Hidden_Entities);
8069 end Hide_Current_Scope;
8075 procedure Init_Env is
8076 Saved : Instance_Env;
8079 Saved.Instantiated_Parent := Current_Instantiated_Parent;
8080 Saved.Exchanged_Views := Exchanged_Views;
8081 Saved.Hidden_Entities := Hidden_Entities;
8082 Saved.Current_Sem_Unit := Current_Sem_Unit;
8083 Saved.Parent_Unit_Visible := Parent_Unit_Visible;
8084 Saved.Instance_Parent_Unit := Instance_Parent_Unit;
8086 -- Save configuration switches. These may be reset if the unit is a
8087 -- predefined unit, and the current mode is not Ada 2005.
8089 Save_Opt_Config_Switches (Saved.Switches);
8091 Instance_Envs.Append (Saved);
8093 Exchanged_Views := New_Elmt_List;
8094 Hidden_Entities := New_Elmt_List;
8096 -- Make dummy entry for Instantiated parent. If generic unit is legal,
8097 -- this is set properly in Set_Instance_Env.
8099 Current_Instantiated_Parent :=
8100 (Current_Scope, Current_Scope, Assoc_Null);
8103 ------------------------------
8104 -- In_Same_Declarative_Part --
8105 ------------------------------
8107 function In_Same_Declarative_Part
8109 Inst : Node_Id) return Boolean
8111 Decls : constant Node_Id := Parent (F_Node);
8112 Nod : Node_Id := Parent (Inst);
8115 while Present (Nod) loop
8119 elsif Nkind_In (Nod, N_Subprogram_Body,
8121 N_Package_Declaration,
8128 elsif Nkind (Nod) = N_Subunit then
8129 Nod := Corresponding_Stub (Nod);
8131 elsif Nkind (Nod) = N_Compilation_Unit then
8135 Nod := Parent (Nod);
8140 end In_Same_Declarative_Part;
8142 ---------------------
8143 -- In_Main_Context --
8144 ---------------------
8146 function In_Main_Context (E : Entity_Id) return Boolean is
8152 if not Is_Compilation_Unit (E)
8153 or else Ekind (E) /= E_Package
8154 or else In_Private_Part (E)
8159 Context := Context_Items (Cunit (Main_Unit));
8161 Clause := First (Context);
8162 while Present (Clause) loop
8163 if Nkind (Clause) = N_With_Clause then
8164 Nam := Name (Clause);
8166 -- If the current scope is part of the context of the main unit,
8167 -- analysis of the corresponding with_clause is not complete, and
8168 -- the entity is not set. We use the Chars field directly, which
8169 -- might produce false positives in rare cases, but guarantees
8170 -- that we produce all the instance bodies we will need.
8172 if (Is_Entity_Name (Nam) and then Chars (Nam) = Chars (E))
8173 or else (Nkind (Nam) = N_Selected_Component
8174 and then Chars (Selector_Name (Nam)) = Chars (E))
8184 end In_Main_Context;
8186 ---------------------
8187 -- Inherit_Context --
8188 ---------------------
8190 procedure Inherit_Context (Gen_Decl : Node_Id; Inst : Node_Id) is
8191 Current_Context : List_Id;
8192 Current_Unit : Node_Id;
8201 if Nkind (Parent (Gen_Decl)) = N_Compilation_Unit then
8203 -- The inherited context is attached to the enclosing compilation
8204 -- unit. This is either the main unit, or the declaration for the
8205 -- main unit (in case the instantiation appears within the package
8206 -- declaration and the main unit is its body).
8208 Current_Unit := Parent (Inst);
8209 while Present (Current_Unit)
8210 and then Nkind (Current_Unit) /= N_Compilation_Unit
8212 Current_Unit := Parent (Current_Unit);
8215 Current_Context := Context_Items (Current_Unit);
8217 Item := First (Context_Items (Parent (Gen_Decl)));
8218 while Present (Item) loop
8219 if Nkind (Item) = N_With_Clause then
8220 Lib_Unit := Library_Unit (Item);
8222 -- Take care to prevent direct cyclic with's
8224 if Lib_Unit /= Current_Unit then
8226 -- Do not add a unit if it is already in the context
8228 Clause := First (Current_Context);
8230 while Present (Clause) loop
8231 if Nkind (Clause) = N_With_Clause and then
8232 Library_Unit (Clause) = Lib_Unit
8242 New_I := New_Copy (Item);
8243 Set_Implicit_With (New_I, True);
8244 Set_Implicit_With_From_Instantiation (New_I, True);
8245 Append (New_I, Current_Context);
8253 end Inherit_Context;
8259 procedure Initialize is
8261 Generic_Renamings.Init;
8264 Generic_Renamings_HTable.Reset;
8265 Circularity_Detected := False;
8266 Exchanged_Views := No_Elist;
8267 Hidden_Entities := No_Elist;
8270 -------------------------------------
8271 -- Insert_Freeze_Node_For_Instance --
8272 -------------------------------------
8274 procedure Insert_Freeze_Node_For_Instance
8283 function Enclosing_Body (N : Node_Id) return Node_Id;
8284 -- Find enclosing package or subprogram body, if any. Freeze node may
8285 -- be placed at end of current declarative list if previous instance
8286 -- and current one have different enclosing bodies.
8288 function Previous_Instance (Gen : Entity_Id) return Entity_Id;
8289 -- Find the local instance, if any, that declares the generic that is
8290 -- being instantiated. If present, the freeze node for this instance
8291 -- must follow the freeze node for the previous instance.
8293 --------------------
8294 -- Enclosing_Body --
8295 --------------------
8297 function Enclosing_Body (N : Node_Id) return Node_Id is
8303 and then Nkind (Parent (P)) /= N_Compilation_Unit
8305 if Nkind_In (P, N_Package_Body, N_Subprogram_Body) then
8306 if Nkind (Parent (P)) = N_Subunit then
8307 return Corresponding_Stub (Parent (P));
8313 P := True_Parent (P);
8319 -----------------------
8320 -- Previous_Instance --
8321 -----------------------
8323 function Previous_Instance (Gen : Entity_Id) return Entity_Id is
8329 and then S /= Standard_Standard
8331 if Is_Generic_Instance (S)
8332 and then In_Same_Source_Unit (S, N)
8341 end Previous_Instance;
8343 -- Start of processing for Insert_Freeze_Node_For_Instance
8346 if not Is_List_Member (F_Node) then
8348 Decls := List_Containing (N);
8349 Inst := Entity (F_Node);
8350 Par_N := Parent (Decls);
8352 -- When processing a subprogram instantiation, utilize the actual
8353 -- subprogram instantiation rather than its package wrapper as it
8354 -- carries all the context information.
8356 if Is_Wrapper_Package (Inst) then
8357 Inst := Related_Instance (Inst);
8360 -- If this is a package instance, check whether the generic is
8361 -- declared in a previous instance and the current instance is
8362 -- not within the previous one.
8364 if Present (Generic_Parent (Parent (Inst)))
8365 and then Is_In_Main_Unit (N)
8368 Enclosing_N : constant Node_Id := Enclosing_Body (N);
8369 Par_I : constant Entity_Id :=
8371 (Generic_Parent (Parent (Inst)));
8376 and then Earlier (N, Freeze_Node (Par_I))
8378 Scop := Scope (Inst);
8380 -- If the current instance is within the one that contains
8381 -- the generic, the freeze node for the current one must
8382 -- appear in the current declarative part. Ditto, if the
8383 -- current instance is within another package instance or
8384 -- within a body that does not enclose the current instance.
8385 -- In these three cases the freeze node of the previous
8386 -- instance is not relevant.
8388 while Present (Scop)
8389 and then Scop /= Standard_Standard
8391 exit when Scop = Par_I
8393 (Is_Generic_Instance (Scop)
8394 and then Scope_Depth (Scop) > Scope_Depth (Par_I));
8395 Scop := Scope (Scop);
8398 -- Previous instance encloses current instance
8400 if Scop = Par_I then
8403 -- If the next node is a source body we must freeze in
8404 -- the current scope as well.
8406 elsif Present (Next (N))
8407 and then Nkind_In (Next (N),
8408 N_Subprogram_Body, N_Package_Body)
8409 and then Comes_From_Source (Next (N))
8413 -- Current instance is within an unrelated instance
8415 elsif Is_Generic_Instance (Scop) then
8418 -- Current instance is within an unrelated body
8420 elsif Present (Enclosing_N)
8421 and then Enclosing_N /= Enclosing_Body (Par_I)
8426 Insert_After (Freeze_Node (Par_I), F_Node);
8433 -- When the instantiation occurs in a package declaration, append the
8434 -- freeze node to the private declarations (if any).
8436 if Nkind (Par_N) = N_Package_Specification
8437 and then Decls = Visible_Declarations (Par_N)
8438 and then Present (Private_Declarations (Par_N))
8439 and then not Is_Empty_List (Private_Declarations (Par_N))
8441 Decls := Private_Declarations (Par_N);
8442 Decl := First (Decls);
8445 -- Determine the proper freeze point of a package instantiation. We
8446 -- adhere to the general rule of a package or subprogram body causing
8447 -- freezing of anything before it in the same declarative region. In
8448 -- this case, the proper freeze point of a package instantiation is
8449 -- before the first source body which follows, or before a stub. This
8450 -- ensures that entities coming from the instance are already frozen
8451 -- and usable in source bodies.
8453 if Nkind (Par_N) /= N_Package_Declaration
8454 and then Ekind (Inst) = E_Package
8455 and then Is_Generic_Instance (Inst)
8457 not In_Same_Source_Unit (Generic_Parent (Parent (Inst)), Inst)
8459 while Present (Decl) loop
8460 if (Nkind (Decl) in N_Unit_Body
8462 Nkind (Decl) in N_Body_Stub)
8463 and then Comes_From_Source (Decl)
8465 Insert_Before (Decl, F_Node);
8473 -- In a package declaration, or if no previous body, insert at end
8476 Set_Sloc (F_Node, Sloc (Last (Decls)));
8477 Insert_After (Last (Decls), F_Node);
8479 end Insert_Freeze_Node_For_Instance;
8485 procedure Install_Body
8486 (Act_Body : Node_Id;
8491 Act_Id : constant Entity_Id := Corresponding_Spec (Act_Body);
8492 Act_Unit : constant Node_Id := Unit (Cunit (Get_Source_Unit (N)));
8493 Gen_Id : constant Entity_Id := Corresponding_Spec (Gen_Body);
8494 Par : constant Entity_Id := Scope (Gen_Id);
8495 Gen_Unit : constant Node_Id :=
8496 Unit (Cunit (Get_Source_Unit (Gen_Decl)));
8497 Orig_Body : Node_Id := Gen_Body;
8499 Body_Unit : Node_Id;
8501 Must_Delay : Boolean;
8503 function In_Same_Enclosing_Subp return Boolean;
8504 -- Check whether instance and generic body are within same subprogram.
8506 function True_Sloc (N : Node_Id) return Source_Ptr;
8507 -- If the instance is nested inside a generic unit, the Sloc of the
8508 -- instance indicates the place of the original definition, not the
8509 -- point of the current enclosing instance. Pending a better usage of
8510 -- Slocs to indicate instantiation places, we determine the place of
8511 -- origin of a node by finding the maximum sloc of any ancestor node.
8512 -- Why is this not equivalent to Top_Level_Location ???
8514 ----------------------------
8515 -- In_Same_Enclosing_Subp --
8516 ----------------------------
8518 function In_Same_Enclosing_Subp return Boolean is
8523 Scop := Scope (Act_Id);
8524 while Scop /= Standard_Standard
8525 and then not Is_Overloadable (Scop)
8527 Scop := Scope (Scop);
8530 if Scop = Standard_Standard then
8536 Scop := Scope (Gen_Id);
8537 while Scop /= Standard_Standard loop
8541 Scop := Scope (Scop);
8546 end In_Same_Enclosing_Subp;
8552 function True_Sloc (N : Node_Id) return Source_Ptr is
8559 while Present (N1) and then N1 /= Act_Unit loop
8560 if Sloc (N1) > Res then
8570 -- Start of processing for Install_Body
8573 -- If the body is a subunit, the freeze point is the corresponding stub
8574 -- in the current compilation, not the subunit itself.
8576 if Nkind (Parent (Gen_Body)) = N_Subunit then
8577 Orig_Body := Corresponding_Stub (Parent (Gen_Body));
8579 Orig_Body := Gen_Body;
8582 Body_Unit := Unit (Cunit (Get_Source_Unit (Orig_Body)));
8584 -- If the instantiation and the generic definition appear in the same
8585 -- package declaration, this is an early instantiation. If they appear
8586 -- in the same declarative part, it is an early instantiation only if
8587 -- the generic body appears textually later, and the generic body is
8588 -- also in the main unit.
8590 -- If instance is nested within a subprogram, and the generic body
8591 -- is not, the instance is delayed because the enclosing body is. If
8592 -- instance and body are within the same scope, or the same subprogram
8593 -- body, indicate explicitly that the instance is delayed.
8596 (Gen_Unit = Act_Unit
8597 and then (Nkind_In (Gen_Unit, N_Package_Declaration,
8598 N_Generic_Package_Declaration)
8599 or else (Gen_Unit = Body_Unit
8600 and then True_Sloc (N) < Sloc (Orig_Body)))
8601 and then Is_In_Main_Unit (Gen_Unit)
8602 and then (Scope (Act_Id) = Scope (Gen_Id)
8603 or else In_Same_Enclosing_Subp));
8605 -- If this is an early instantiation, the freeze node is placed after
8606 -- the generic body. Otherwise, if the generic appears in an instance,
8607 -- we cannot freeze the current instance until the outer one is frozen.
8608 -- This is only relevant if the current instance is nested within some
8609 -- inner scope not itself within the outer instance. If this scope is
8610 -- a package body in the same declarative part as the outer instance,
8611 -- then that body needs to be frozen after the outer instance. Finally,
8612 -- if no delay is needed, we place the freeze node at the end of the
8613 -- current declarative part.
8615 if Expander_Active then
8616 Ensure_Freeze_Node (Act_Id);
8617 F_Node := Freeze_Node (Act_Id);
8620 Insert_After (Orig_Body, F_Node);
8622 elsif Is_Generic_Instance (Par)
8623 and then Present (Freeze_Node (Par))
8624 and then Scope (Act_Id) /= Par
8626 -- Freeze instance of inner generic after instance of enclosing
8629 if In_Same_Declarative_Part (Freeze_Node (Par), N) then
8631 -- Handle the following case:
8633 -- package Parent_Inst is new ...
8636 -- procedure P ... -- this body freezes Parent_Inst
8638 -- package Inst is new ...
8640 -- In this particular scenario, the freeze node for Inst must
8641 -- be inserted in the same manner as that of Parent_Inst,
8642 -- before the next source body or at the end of the declarative
8643 -- list (body not available). If body P did not exist and
8644 -- Parent_Inst was frozen after Inst, either by a body
8645 -- following Inst or at the end of the declarative region,
8646 -- the freeze node for Inst must be inserted after that of
8647 -- Parent_Inst. This relation is established by comparing
8648 -- the Slocs of Parent_Inst freeze node and Inst.
8650 if List_Containing (Get_Package_Instantiation_Node (Par)) =
8652 and then Sloc (Freeze_Node (Par)) < Sloc (N)
8654 Insert_Freeze_Node_For_Instance (N, F_Node);
8656 Insert_After (Freeze_Node (Par), F_Node);
8659 -- Freeze package enclosing instance of inner generic after
8660 -- instance of enclosing generic.
8662 elsif Nkind_In (Parent (N), N_Package_Body, N_Subprogram_Body)
8663 and then In_Same_Declarative_Part (Freeze_Node (Par), Parent (N))
8666 Enclosing : Entity_Id;
8669 Enclosing := Corresponding_Spec (Parent (N));
8671 if No (Enclosing) then
8672 Enclosing := Defining_Entity (Parent (N));
8675 Insert_Freeze_Node_For_Instance (N, F_Node);
8676 Ensure_Freeze_Node (Enclosing);
8678 if not Is_List_Member (Freeze_Node (Enclosing)) then
8680 -- The enclosing context is a subunit, insert the freeze
8681 -- node after the stub.
8683 if Nkind (Parent (Parent (N))) = N_Subunit then
8684 Insert_Freeze_Node_For_Instance
8685 (Corresponding_Stub (Parent (Parent (N))),
8686 Freeze_Node (Enclosing));
8688 -- The enclosing context is a package with a stub body
8689 -- which has already been replaced by the real body.
8690 -- Insert the freeze node after the actual body.
8692 elsif Ekind (Enclosing) = E_Package
8693 and then Present (Body_Entity (Enclosing))
8694 and then Was_Originally_Stub
8695 (Parent (Body_Entity (Enclosing)))
8697 Insert_Freeze_Node_For_Instance
8698 (Parent (Body_Entity (Enclosing)),
8699 Freeze_Node (Enclosing));
8701 -- The parent instance has been frozen before the body of
8702 -- the enclosing package, insert the freeze node after
8705 elsif List_Containing (Freeze_Node (Par)) =
8706 List_Containing (Parent (N))
8707 and then Sloc (Freeze_Node (Par)) < Sloc (Parent (N))
8709 Insert_Freeze_Node_For_Instance
8710 (Parent (N), Freeze_Node (Enclosing));
8714 (Freeze_Node (Par), Freeze_Node (Enclosing));
8720 Insert_Freeze_Node_For_Instance (N, F_Node);
8724 Insert_Freeze_Node_For_Instance (N, F_Node);
8728 Set_Is_Frozen (Act_Id);
8729 Insert_Before (N, Act_Body);
8730 Mark_Rewrite_Insertion (Act_Body);
8733 -----------------------------
8734 -- Install_Formal_Packages --
8735 -----------------------------
8737 procedure Install_Formal_Packages (Par : Entity_Id) is
8740 Gen_E : Entity_Id := Empty;
8743 E := First_Entity (Par);
8745 -- If we are installing an instance parent, locate the formal packages
8746 -- of its generic parent.
8748 if Is_Generic_Instance (Par) then
8749 Gen := Generic_Parent (Package_Specification (Par));
8750 Gen_E := First_Entity (Gen);
8753 while Present (E) loop
8754 if Ekind (E) = E_Package
8755 and then Nkind (Parent (E)) = N_Package_Renaming_Declaration
8757 -- If this is the renaming for the parent instance, done
8759 if Renamed_Object (E) = Par then
8762 -- The visibility of a formal of an enclosing generic is already
8765 elsif Denotes_Formal_Package (E) then
8768 elsif Present (Associated_Formal_Package (E)) then
8769 Check_Generic_Actuals (Renamed_Object (E), True);
8770 Set_Is_Hidden (E, False);
8772 -- Find formal package in generic unit that corresponds to
8773 -- (instance of) formal package in instance.
8775 while Present (Gen_E) and then Chars (Gen_E) /= Chars (E) loop
8776 Next_Entity (Gen_E);
8779 if Present (Gen_E) then
8780 Map_Formal_Package_Entities (Gen_E, E);
8786 if Present (Gen_E) then
8787 Next_Entity (Gen_E);
8790 end Install_Formal_Packages;
8792 --------------------
8793 -- Install_Parent --
8794 --------------------
8796 procedure Install_Parent (P : Entity_Id; In_Body : Boolean := False) is
8797 Ancestors : constant Elist_Id := New_Elmt_List;
8798 S : constant Entity_Id := Current_Scope;
8799 Inst_Par : Entity_Id;
8800 First_Par : Entity_Id;
8801 Inst_Node : Node_Id;
8802 Gen_Par : Entity_Id;
8803 First_Gen : Entity_Id;
8806 procedure Install_Noninstance_Specs (Par : Entity_Id);
8807 -- Install the scopes of noninstance parent units ending with Par
8809 procedure Install_Spec (Par : Entity_Id);
8810 -- The child unit is within the declarative part of the parent, so the
8811 -- declarations within the parent are immediately visible.
8813 -------------------------------
8814 -- Install_Noninstance_Specs --
8815 -------------------------------
8817 procedure Install_Noninstance_Specs (Par : Entity_Id) is
8820 and then Par /= Standard_Standard
8821 and then not In_Open_Scopes (Par)
8823 Install_Noninstance_Specs (Scope (Par));
8826 end Install_Noninstance_Specs;
8832 procedure Install_Spec (Par : Entity_Id) is
8833 Spec : constant Node_Id := Package_Specification (Par);
8836 -- If this parent of the child instance is a top-level unit,
8837 -- then record the unit and its visibility for later resetting in
8838 -- Remove_Parent. We exclude units that are generic instances, as we
8839 -- only want to record this information for the ultimate top-level
8840 -- noninstance parent (is that always correct???).
8842 if Scope (Par) = Standard_Standard
8843 and then not Is_Generic_Instance (Par)
8845 Parent_Unit_Visible := Is_Immediately_Visible (Par);
8846 Instance_Parent_Unit := Par;
8849 -- Open the parent scope and make it and its declarations visible.
8850 -- If this point is not within a body, then only the visible
8851 -- declarations should be made visible, and installation of the
8852 -- private declarations is deferred until the appropriate point
8853 -- within analysis of the spec being instantiated (see the handling
8854 -- of parent visibility in Analyze_Package_Specification). This is
8855 -- relaxed in the case where the parent unit is Ada.Tags, to avoid
8856 -- private view problems that occur when compiling instantiations of
8857 -- a generic child of that package (Generic_Dispatching_Constructor).
8858 -- If the instance freezes a tagged type, inlinings of operations
8859 -- from Ada.Tags may need the full view of type Tag. If inlining took
8860 -- proper account of establishing visibility of inlined subprograms'
8861 -- parents then it should be possible to remove this
8862 -- special check. ???
8865 Set_Is_Immediately_Visible (Par);
8866 Install_Visible_Declarations (Par);
8867 Set_Use (Visible_Declarations (Spec));
8869 if In_Body or else Is_RTU (Par, Ada_Tags) then
8870 Install_Private_Declarations (Par);
8871 Set_Use (Private_Declarations (Spec));
8875 -- Start of processing for Install_Parent
8878 -- We need to install the parent instance to compile the instantiation
8879 -- of the child, but the child instance must appear in the current
8880 -- scope. Given that we cannot place the parent above the current scope
8881 -- in the scope stack, we duplicate the current scope and unstack both
8882 -- after the instantiation is complete.
8884 -- If the parent is itself the instantiation of a child unit, we must
8885 -- also stack the instantiation of its parent, and so on. Each such
8886 -- ancestor is the prefix of the name in a prior instantiation.
8888 -- If this is a nested instance, the parent unit itself resolves to
8889 -- a renaming of the parent instance, whose declaration we need.
8891 -- Finally, the parent may be a generic (not an instance) when the
8892 -- child unit appears as a formal package.
8896 if Present (Renamed_Entity (Inst_Par)) then
8897 Inst_Par := Renamed_Entity (Inst_Par);
8900 First_Par := Inst_Par;
8902 Gen_Par := Generic_Parent (Package_Specification (Inst_Par));
8904 First_Gen := Gen_Par;
8906 while Present (Gen_Par)
8907 and then Is_Child_Unit (Gen_Par)
8909 -- Load grandparent instance as well
8911 Inst_Node := Get_Package_Instantiation_Node (Inst_Par);
8913 if Nkind (Name (Inst_Node)) = N_Expanded_Name then
8914 Inst_Par := Entity (Prefix (Name (Inst_Node)));
8916 if Present (Renamed_Entity (Inst_Par)) then
8917 Inst_Par := Renamed_Entity (Inst_Par);
8920 Gen_Par := Generic_Parent (Package_Specification (Inst_Par));
8922 if Present (Gen_Par) then
8923 Prepend_Elmt (Inst_Par, Ancestors);
8926 -- Parent is not the name of an instantiation
8928 Install_Noninstance_Specs (Inst_Par);
8939 if Present (First_Gen) then
8940 Append_Elmt (First_Par, Ancestors);
8942 Install_Noninstance_Specs (First_Par);
8945 if not Is_Empty_Elmt_List (Ancestors) then
8946 Elmt := First_Elmt (Ancestors);
8947 while Present (Elmt) loop
8948 Install_Spec (Node (Elmt));
8949 Install_Formal_Packages (Node (Elmt));
8959 -------------------------------
8960 -- Install_Hidden_Primitives --
8961 -------------------------------
8963 procedure Install_Hidden_Primitives
8964 (Prims_List : in out Elist_Id;
8969 List : Elist_Id := No_Elist;
8970 Prim_G_Elmt : Elmt_Id;
8971 Prim_A_Elmt : Elmt_Id;
8976 -- No action needed in case of serious errors because we cannot trust
8977 -- in the order of primitives
8979 if Serious_Errors_Detected > 0 then
8982 -- No action possible if we don't have available the list of primitive
8986 or else not Is_Record_Type (Gen_T)
8987 or else not Is_Tagged_Type (Gen_T)
8988 or else not Is_Record_Type (Act_T)
8989 or else not Is_Tagged_Type (Act_T)
8993 -- There is no need to handle interface types since their primitives
8996 elsif Is_Interface (Gen_T) then
9000 Prim_G_Elmt := First_Elmt (Primitive_Operations (Gen_T));
9002 if not Is_Class_Wide_Type (Act_T) then
9003 Prim_A_Elmt := First_Elmt (Primitive_Operations (Act_T));
9005 Prim_A_Elmt := First_Elmt (Primitive_Operations (Root_Type (Act_T)));
9009 -- Skip predefined primitives in the generic formal
9011 while Present (Prim_G_Elmt)
9012 and then Is_Predefined_Dispatching_Operation (Node (Prim_G_Elmt))
9014 Next_Elmt (Prim_G_Elmt);
9017 -- Skip predefined primitives in the generic actual
9019 while Present (Prim_A_Elmt)
9020 and then Is_Predefined_Dispatching_Operation (Node (Prim_A_Elmt))
9022 Next_Elmt (Prim_A_Elmt);
9025 exit when No (Prim_G_Elmt) or else No (Prim_A_Elmt);
9027 Prim_G := Node (Prim_G_Elmt);
9028 Prim_A := Node (Prim_A_Elmt);
9030 -- There is no need to handle interface primitives because their
9031 -- primitives are not hidden
9033 exit when Present (Interface_Alias (Prim_G));
9035 -- Here we install one hidden primitive
9037 if Chars (Prim_G) /= Chars (Prim_A)
9038 and then Has_Suffix (Prim_A, 'P')
9039 and then Remove_Suffix (Prim_A, 'P') = Chars (Prim_G)
9041 Set_Chars (Prim_A, Chars (Prim_G));
9042 Append_New_Elmt (Prim_A, To => List);
9045 Next_Elmt (Prim_A_Elmt);
9046 Next_Elmt (Prim_G_Elmt);
9049 -- Append the elements to the list of temporarily visible primitives
9050 -- avoiding duplicates.
9052 if Present (List) then
9053 if No (Prims_List) then
9054 Prims_List := New_Elmt_List;
9057 Elmt := First_Elmt (List);
9058 while Present (Elmt) loop
9059 Append_Unique_Elmt (Node (Elmt), Prims_List);
9063 end Install_Hidden_Primitives;
9065 -------------------------------
9066 -- Restore_Hidden_Primitives --
9067 -------------------------------
9069 procedure Restore_Hidden_Primitives (Prims_List : in out Elist_Id) is
9070 Prim_Elmt : Elmt_Id;
9074 if Prims_List /= No_Elist then
9075 Prim_Elmt := First_Elmt (Prims_List);
9076 while Present (Prim_Elmt) loop
9077 Prim := Node (Prim_Elmt);
9078 Set_Chars (Prim, Add_Suffix (Prim, 'P'));
9079 Next_Elmt (Prim_Elmt);
9082 Prims_List := No_Elist;
9084 end Restore_Hidden_Primitives;
9086 --------------------------------
9087 -- Instantiate_Formal_Package --
9088 --------------------------------
9090 function Instantiate_Formal_Package
9093 Analyzed_Formal : Node_Id) return List_Id
9095 Loc : constant Source_Ptr := Sloc (Actual);
9096 Actual_Pack : Entity_Id;
9097 Formal_Pack : Entity_Id;
9098 Gen_Parent : Entity_Id;
9101 Parent_Spec : Node_Id;
9103 procedure Find_Matching_Actual
9105 Act : in out Entity_Id);
9106 -- We need to associate each formal entity in the formal package with
9107 -- the corresponding entity in the actual package. The actual package
9108 -- has been analyzed and possibly expanded, and as a result there is
9109 -- no one-to-one correspondence between the two lists (for example,
9110 -- the actual may include subtypes, itypes, and inherited primitive
9111 -- operations, interspersed among the renaming declarations for the
9112 -- actuals) . We retrieve the corresponding actual by name because each
9113 -- actual has the same name as the formal, and they do appear in the
9116 function Get_Formal_Entity (N : Node_Id) return Entity_Id;
9117 -- Retrieve entity of defining entity of generic formal parameter.
9118 -- Only the declarations of formals need to be considered when
9119 -- linking them to actuals, but the declarative list may include
9120 -- internal entities generated during analysis, and those are ignored.
9122 procedure Match_Formal_Entity
9123 (Formal_Node : Node_Id;
9124 Formal_Ent : Entity_Id;
9125 Actual_Ent : Entity_Id);
9126 -- Associates the formal entity with the actual. In the case where
9127 -- Formal_Ent is a formal package, this procedure iterates through all
9128 -- of its formals and enters associations between the actuals occurring
9129 -- in the formal package's corresponding actual package (given by
9130 -- Actual_Ent) and the formal package's formal parameters. This
9131 -- procedure recurses if any of the parameters is itself a package.
9133 function Is_Instance_Of
9134 (Act_Spec : Entity_Id;
9135 Gen_Anc : Entity_Id) return Boolean;
9136 -- The actual can be an instantiation of a generic within another
9137 -- instance, in which case there is no direct link from it to the
9138 -- original generic ancestor. In that case, we recognize that the
9139 -- ultimate ancestor is the same by examining names and scopes.
9141 procedure Process_Nested_Formal (Formal : Entity_Id);
9142 -- If the current formal is declared with a box, its own formals are
9143 -- visible in the instance, as they were in the generic, and their
9144 -- Hidden flag must be reset. If some of these formals are themselves
9145 -- packages declared with a box, the processing must be recursive.
9147 --------------------------
9148 -- Find_Matching_Actual --
9149 --------------------------
9151 procedure Find_Matching_Actual
9153 Act : in out Entity_Id)
9155 Formal_Ent : Entity_Id;
9158 case Nkind (Original_Node (F)) is
9159 when N_Formal_Object_Declaration |
9160 N_Formal_Type_Declaration =>
9161 Formal_Ent := Defining_Identifier (F);
9163 while Chars (Act) /= Chars (Formal_Ent) loop
9167 when N_Formal_Subprogram_Declaration |
9168 N_Formal_Package_Declaration |
9169 N_Package_Declaration |
9170 N_Generic_Package_Declaration =>
9171 Formal_Ent := Defining_Entity (F);
9173 while Chars (Act) /= Chars (Formal_Ent) loop
9178 raise Program_Error;
9180 end Find_Matching_Actual;
9182 -------------------------
9183 -- Match_Formal_Entity --
9184 -------------------------
9186 procedure Match_Formal_Entity
9187 (Formal_Node : Node_Id;
9188 Formal_Ent : Entity_Id;
9189 Actual_Ent : Entity_Id)
9191 Act_Pkg : Entity_Id;
9194 Set_Instance_Of (Formal_Ent, Actual_Ent);
9196 if Ekind (Actual_Ent) = E_Package then
9198 -- Record associations for each parameter
9200 Act_Pkg := Actual_Ent;
9203 A_Ent : Entity_Id := First_Entity (Act_Pkg);
9212 -- Retrieve the actual given in the formal package declaration
9214 Actual := Entity (Name (Original_Node (Formal_Node)));
9216 -- The actual in the formal package declaration may be a
9217 -- renamed generic package, in which case we want to retrieve
9218 -- the original generic in order to traverse its formal part.
9220 if Present (Renamed_Entity (Actual)) then
9221 Gen_Decl := Unit_Declaration_Node (Renamed_Entity (Actual));
9223 Gen_Decl := Unit_Declaration_Node (Actual);
9226 Formals := Generic_Formal_Declarations (Gen_Decl);
9228 if Present (Formals) then
9229 F_Node := First_Non_Pragma (Formals);
9234 while Present (A_Ent)
9235 and then Present (F_Node)
9236 and then A_Ent /= First_Private_Entity (Act_Pkg)
9238 F_Ent := Get_Formal_Entity (F_Node);
9240 if Present (F_Ent) then
9242 -- This is a formal of the original package. Record
9243 -- association and recurse.
9245 Find_Matching_Actual (F_Node, A_Ent);
9246 Match_Formal_Entity (F_Node, F_Ent, A_Ent);
9247 Next_Entity (A_Ent);
9250 Next_Non_Pragma (F_Node);
9254 end Match_Formal_Entity;
9256 -----------------------
9257 -- Get_Formal_Entity --
9258 -----------------------
9260 function Get_Formal_Entity (N : Node_Id) return Entity_Id is
9261 Kind : constant Node_Kind := Nkind (Original_Node (N));
9264 when N_Formal_Object_Declaration =>
9265 return Defining_Identifier (N);
9267 when N_Formal_Type_Declaration =>
9268 return Defining_Identifier (N);
9270 when N_Formal_Subprogram_Declaration =>
9271 return Defining_Unit_Name (Specification (N));
9273 when N_Formal_Package_Declaration =>
9274 return Defining_Identifier (Original_Node (N));
9276 when N_Generic_Package_Declaration =>
9277 return Defining_Identifier (Original_Node (N));
9279 -- All other declarations are introduced by semantic analysis and
9280 -- have no match in the actual.
9285 end Get_Formal_Entity;
9287 --------------------
9288 -- Is_Instance_Of --
9289 --------------------
9291 function Is_Instance_Of
9292 (Act_Spec : Entity_Id;
9293 Gen_Anc : Entity_Id) return Boolean
9295 Gen_Par : constant Entity_Id := Generic_Parent (Act_Spec);
9298 if No (Gen_Par) then
9301 -- Simplest case: the generic parent of the actual is the formal
9303 elsif Gen_Par = Gen_Anc then
9306 elsif Chars (Gen_Par) /= Chars (Gen_Anc) then
9309 -- The actual may be obtained through several instantiations. Its
9310 -- scope must itself be an instance of a generic declared in the
9311 -- same scope as the formal. Any other case is detected above.
9313 elsif not Is_Generic_Instance (Scope (Gen_Par)) then
9317 return Generic_Parent (Parent (Scope (Gen_Par))) = Scope (Gen_Anc);
9321 ---------------------------
9322 -- Process_Nested_Formal --
9323 ---------------------------
9325 procedure Process_Nested_Formal (Formal : Entity_Id) is
9329 if Present (Associated_Formal_Package (Formal))
9330 and then Box_Present (Parent (Associated_Formal_Package (Formal)))
9332 Ent := First_Entity (Formal);
9333 while Present (Ent) loop
9334 Set_Is_Hidden (Ent, False);
9335 Set_Is_Visible_Formal (Ent);
9336 Set_Is_Potentially_Use_Visible
9337 (Ent, Is_Potentially_Use_Visible (Formal));
9339 if Ekind (Ent) = E_Package then
9340 exit when Renamed_Entity (Ent) = Renamed_Entity (Formal);
9341 Process_Nested_Formal (Ent);
9347 end Process_Nested_Formal;
9349 -- Start of processing for Instantiate_Formal_Package
9354 if not Is_Entity_Name (Actual)
9355 or else Ekind (Entity (Actual)) /= E_Package
9358 ("expect package instance to instantiate formal", Actual);
9359 Abandon_Instantiation (Actual);
9360 raise Program_Error;
9363 Actual_Pack := Entity (Actual);
9364 Set_Is_Instantiated (Actual_Pack);
9366 -- The actual may be a renamed package, or an outer generic formal
9367 -- package whose instantiation is converted into a renaming.
9369 if Present (Renamed_Object (Actual_Pack)) then
9370 Actual_Pack := Renamed_Object (Actual_Pack);
9373 if Nkind (Analyzed_Formal) = N_Formal_Package_Declaration then
9374 Gen_Parent := Get_Instance_Of (Entity (Name (Analyzed_Formal)));
9375 Formal_Pack := Defining_Identifier (Analyzed_Formal);
9378 Generic_Parent (Specification (Analyzed_Formal));
9380 Defining_Unit_Name (Specification (Analyzed_Formal));
9383 if Nkind (Parent (Actual_Pack)) = N_Defining_Program_Unit_Name then
9384 Parent_Spec := Package_Specification (Actual_Pack);
9386 Parent_Spec := Parent (Actual_Pack);
9389 if Gen_Parent = Any_Id then
9391 ("previous error in declaration of formal package", Actual);
9392 Abandon_Instantiation (Actual);
9395 Is_Instance_Of (Parent_Spec, Get_Instance_Of (Gen_Parent))
9401 ("actual parameter must be instance of&", Actual, Gen_Parent);
9402 Abandon_Instantiation (Actual);
9405 Set_Instance_Of (Defining_Identifier (Formal), Actual_Pack);
9406 Map_Formal_Package_Entities (Formal_Pack, Actual_Pack);
9409 Make_Package_Renaming_Declaration (Loc,
9410 Defining_Unit_Name => New_Copy (Defining_Identifier (Formal)),
9411 Name => New_Occurrence_Of (Actual_Pack, Loc));
9413 Set_Associated_Formal_Package (Defining_Unit_Name (Nod),
9414 Defining_Identifier (Formal));
9415 Decls := New_List (Nod);
9417 -- If the formal F has a box, then the generic declarations are
9418 -- visible in the generic G. In an instance of G, the corresponding
9419 -- entities in the actual for F (which are the actuals for the
9420 -- instantiation of the generic that F denotes) must also be made
9421 -- visible for analysis of the current instance. On exit from the
9422 -- current instance, those entities are made private again. If the
9423 -- actual is currently in use, these entities are also use-visible.
9425 -- The loop through the actual entities also steps through the formal
9426 -- entities and enters associations from formals to actuals into the
9427 -- renaming map. This is necessary to properly handle checking of
9428 -- actual parameter associations for later formals that depend on
9429 -- actuals declared in the formal package.
9431 -- In Ada 2005, partial parameterization requires that we make
9432 -- visible the actuals corresponding to formals that were defaulted
9433 -- in the formal package. There formals are identified because they
9434 -- remain formal generics within the formal package, rather than
9435 -- being renamings of the actuals supplied.
9438 Gen_Decl : constant Node_Id :=
9439 Unit_Declaration_Node (Gen_Parent);
9440 Formals : constant List_Id :=
9441 Generic_Formal_Declarations (Gen_Decl);
9443 Actual_Ent : Entity_Id;
9444 Actual_Of_Formal : Node_Id;
9445 Formal_Node : Node_Id;
9446 Formal_Ent : Entity_Id;
9449 if Present (Formals) then
9450 Formal_Node := First_Non_Pragma (Formals);
9452 Formal_Node := Empty;
9455 Actual_Ent := First_Entity (Actual_Pack);
9457 First (Visible_Declarations (Specification (Analyzed_Formal)));
9458 while Present (Actual_Ent)
9459 and then Actual_Ent /= First_Private_Entity (Actual_Pack)
9461 if Present (Formal_Node) then
9462 Formal_Ent := Get_Formal_Entity (Formal_Node);
9464 if Present (Formal_Ent) then
9465 Find_Matching_Actual (Formal_Node, Actual_Ent);
9467 (Formal_Node, Formal_Ent, Actual_Ent);
9469 -- We iterate at the same time over the actuals of the
9470 -- local package created for the formal, to determine
9471 -- which one of the formals of the original generic were
9472 -- defaulted in the formal. The corresponding actual
9473 -- entities are visible in the enclosing instance.
9475 if Box_Present (Formal)
9477 (Present (Actual_Of_Formal)
9480 (Get_Formal_Entity (Actual_Of_Formal)))
9482 Set_Is_Hidden (Actual_Ent, False);
9483 Set_Is_Visible_Formal (Actual_Ent);
9484 Set_Is_Potentially_Use_Visible
9485 (Actual_Ent, In_Use (Actual_Pack));
9487 if Ekind (Actual_Ent) = E_Package then
9488 Process_Nested_Formal (Actual_Ent);
9492 Set_Is_Hidden (Actual_Ent);
9493 Set_Is_Potentially_Use_Visible (Actual_Ent, False);
9497 Next_Non_Pragma (Formal_Node);
9498 Next (Actual_Of_Formal);
9501 -- No further formals to match, but the generic part may
9502 -- contain inherited operation that are not hidden in the
9503 -- enclosing instance.
9505 Next_Entity (Actual_Ent);
9509 -- Inherited subprograms generated by formal derived types are
9510 -- also visible if the types are.
9512 Actual_Ent := First_Entity (Actual_Pack);
9513 while Present (Actual_Ent)
9514 and then Actual_Ent /= First_Private_Entity (Actual_Pack)
9516 if Is_Overloadable (Actual_Ent)
9518 Nkind (Parent (Actual_Ent)) = N_Subtype_Declaration
9520 not Is_Hidden (Defining_Identifier (Parent (Actual_Ent)))
9522 Set_Is_Hidden (Actual_Ent, False);
9523 Set_Is_Potentially_Use_Visible
9524 (Actual_Ent, In_Use (Actual_Pack));
9527 Next_Entity (Actual_Ent);
9531 -- If the formal is not declared with a box, reanalyze it as an
9532 -- abbreviated instantiation, to verify the matching rules of 12.7.
9533 -- The actual checks are performed after the generic associations
9534 -- have been analyzed, to guarantee the same visibility for this
9535 -- instantiation and for the actuals.
9537 -- In Ada 2005, the generic associations for the formal can include
9538 -- defaulted parameters. These are ignored during check. This
9539 -- internal instantiation is removed from the tree after conformance
9540 -- checking, because it contains formal declarations for those
9541 -- defaulted parameters, and those should not reach the back-end.
9543 if not Box_Present (Formal) then
9545 I_Pack : constant Entity_Id :=
9546 Make_Temporary (Sloc (Actual), 'P');
9549 Set_Is_Internal (I_Pack);
9552 Make_Package_Instantiation (Sloc (Actual),
9553 Defining_Unit_Name => I_Pack,
9556 (Get_Instance_Of (Gen_Parent), Sloc (Actual)),
9557 Generic_Associations =>
9558 Generic_Associations (Formal)));
9564 end Instantiate_Formal_Package;
9566 -----------------------------------
9567 -- Instantiate_Formal_Subprogram --
9568 -----------------------------------
9570 function Instantiate_Formal_Subprogram
9573 Analyzed_Formal : Node_Id) return Node_Id
9575 Analyzed_S : constant Entity_Id :=
9576 Defining_Unit_Name (Specification (Analyzed_Formal));
9577 Formal_Sub : constant Entity_Id :=
9578 Defining_Unit_Name (Specification (Formal));
9580 function From_Parent_Scope (Subp : Entity_Id) return Boolean;
9581 -- If the generic is a child unit, the parent has been installed on the
9582 -- scope stack, but a default subprogram cannot resolve to something
9583 -- on the parent because that parent is not really part of the visible
9584 -- context (it is there to resolve explicit local entities). If the
9585 -- default has resolved in this way, we remove the entity from immediate
9586 -- visibility and analyze the node again to emit an error message or
9587 -- find another visible candidate.
9589 procedure Valid_Actual_Subprogram (Act : Node_Id);
9590 -- Perform legality check and raise exception on failure
9592 -----------------------
9593 -- From_Parent_Scope --
9594 -----------------------
9596 function From_Parent_Scope (Subp : Entity_Id) return Boolean is
9597 Gen_Scope : Node_Id;
9600 Gen_Scope := Scope (Analyzed_S);
9601 while Present (Gen_Scope) and then Is_Child_Unit (Gen_Scope) loop
9602 if Scope (Subp) = Scope (Gen_Scope) then
9606 Gen_Scope := Scope (Gen_Scope);
9610 end From_Parent_Scope;
9612 -----------------------------
9613 -- Valid_Actual_Subprogram --
9614 -----------------------------
9616 procedure Valid_Actual_Subprogram (Act : Node_Id) is
9620 if Is_Entity_Name (Act) then
9621 Act_E := Entity (Act);
9623 elsif Nkind (Act) = N_Selected_Component
9624 and then Is_Entity_Name (Selector_Name (Act))
9626 Act_E := Entity (Selector_Name (Act));
9632 if (Present (Act_E) and then Is_Overloadable (Act_E))
9633 or else Nkind_In (Act, N_Attribute_Reference,
9634 N_Indexed_Component,
9635 N_Character_Literal,
9636 N_Explicit_Dereference)
9642 ("expect subprogram or entry name in instantiation of&",
9643 Instantiation_Node, Formal_Sub);
9644 Abandon_Instantiation (Instantiation_Node);
9645 end Valid_Actual_Subprogram;
9649 Decl_Node : Node_Id;
9654 -- Start of processing for Instantiate_Formal_Subprogram
9657 New_Spec := New_Copy_Tree (Specification (Formal));
9659 -- The tree copy has created the proper instantiation sloc for the
9660 -- new specification. Use this location for all other constructed
9663 Loc := Sloc (Defining_Unit_Name (New_Spec));
9665 -- Create new entity for the actual (New_Copy_Tree does not), and
9666 -- indicate that it is an actual.
9668 Set_Defining_Unit_Name
9669 (New_Spec, Make_Defining_Identifier (Loc, Chars (Formal_Sub)));
9670 Set_Ekind (Defining_Unit_Name (New_Spec), Ekind (Analyzed_S));
9671 Set_Is_Generic_Actual_Subprogram (Defining_Unit_Name (New_Spec));
9673 -- Create new entities for the each of the formals in the specification
9674 -- of the renaming declaration built for the actual.
9676 if Present (Parameter_Specifications (New_Spec)) then
9682 F := First (Parameter_Specifications (New_Spec));
9683 while Present (F) loop
9684 F_Id := Defining_Identifier (F);
9686 Set_Defining_Identifier (F,
9687 Make_Defining_Identifier (Sloc (F_Id), Chars (F_Id)));
9693 -- Find entity of actual. If the actual is an attribute reference, it
9694 -- cannot be resolved here (its formal is missing) but is handled
9695 -- instead in Attribute_Renaming. If the actual is overloaded, it is
9696 -- fully resolved subsequently, when the renaming declaration for the
9697 -- formal is analyzed. If it is an explicit dereference, resolve the
9698 -- prefix but not the actual itself, to prevent interpretation as call.
9700 if Present (Actual) then
9701 Loc := Sloc (Actual);
9702 Set_Sloc (New_Spec, Loc);
9704 if Nkind (Actual) = N_Operator_Symbol then
9705 Find_Direct_Name (Actual);
9707 elsif Nkind (Actual) = N_Explicit_Dereference then
9708 Analyze (Prefix (Actual));
9710 elsif Nkind (Actual) /= N_Attribute_Reference then
9714 Valid_Actual_Subprogram (Actual);
9717 elsif Present (Default_Name (Formal)) then
9718 if not Nkind_In (Default_Name (Formal), N_Attribute_Reference,
9719 N_Selected_Component,
9720 N_Indexed_Component,
9721 N_Character_Literal)
9722 and then Present (Entity (Default_Name (Formal)))
9724 Nam := New_Occurrence_Of (Entity (Default_Name (Formal)), Loc);
9726 Nam := New_Copy (Default_Name (Formal));
9727 Set_Sloc (Nam, Loc);
9730 elsif Box_Present (Formal) then
9732 -- Actual is resolved at the point of instantiation. Create an
9733 -- identifier or operator with the same name as the formal.
9735 if Nkind (Formal_Sub) = N_Defining_Operator_Symbol then
9737 Make_Operator_Symbol (Loc,
9738 Chars => Chars (Formal_Sub),
9739 Strval => No_String);
9741 Nam := Make_Identifier (Loc, Chars (Formal_Sub));
9744 elsif Nkind (Specification (Formal)) = N_Procedure_Specification
9745 and then Null_Present (Specification (Formal))
9747 -- Generate null body for procedure, for use in the instance
9750 Make_Subprogram_Body (Loc,
9751 Specification => New_Spec,
9752 Declarations => New_List,
9753 Handled_Statement_Sequence =>
9754 Make_Handled_Sequence_Of_Statements (Loc,
9755 Statements => New_List (Make_Null_Statement (Loc))));
9757 Set_Is_Intrinsic_Subprogram (Defining_Unit_Name (New_Spec));
9761 Error_Msg_Sloc := Sloc (Scope (Analyzed_S));
9763 ("missing actual&", Instantiation_Node, Formal_Sub);
9765 ("\in instantiation of & declared#",
9766 Instantiation_Node, Scope (Analyzed_S));
9767 Abandon_Instantiation (Instantiation_Node);
9771 Make_Subprogram_Renaming_Declaration (Loc,
9772 Specification => New_Spec,
9775 -- If we do not have an actual and the formal specified <> then set to
9776 -- get proper default.
9778 if No (Actual) and then Box_Present (Formal) then
9779 Set_From_Default (Decl_Node);
9782 -- Gather possible interpretations for the actual before analyzing the
9783 -- instance. If overloaded, it will be resolved when analyzing the
9784 -- renaming declaration.
9786 if Box_Present (Formal) and then No (Actual) then
9789 if Is_Child_Unit (Scope (Analyzed_S))
9790 and then Present (Entity (Nam))
9792 if not Is_Overloaded (Nam) then
9793 if From_Parent_Scope (Entity (Nam)) then
9794 Set_Is_Immediately_Visible (Entity (Nam), False);
9795 Set_Entity (Nam, Empty);
9796 Set_Etype (Nam, Empty);
9799 Set_Is_Immediately_Visible (Entity (Nam));
9808 Get_First_Interp (Nam, I, It);
9809 while Present (It.Nam) loop
9810 if From_Parent_Scope (It.Nam) then
9814 Get_Next_Interp (I, It);
9821 -- The generic instantiation freezes the actual. This can only be done
9822 -- once the actual is resolved, in the analysis of the renaming
9823 -- declaration. To make the formal subprogram entity available, we set
9824 -- Corresponding_Formal_Spec to point to the formal subprogram entity.
9825 -- This is also needed in Analyze_Subprogram_Renaming for the processing
9826 -- of formal abstract subprograms.
9828 Set_Corresponding_Formal_Spec (Decl_Node, Analyzed_S);
9830 -- We cannot analyze the renaming declaration, and thus find the actual,
9831 -- until all the actuals are assembled in the instance. For subsequent
9832 -- checks of other actuals, indicate the node that will hold the
9833 -- instance of this formal.
9835 Set_Instance_Of (Analyzed_S, Nam);
9837 if Nkind (Actual) = N_Selected_Component
9838 and then Is_Task_Type (Etype (Prefix (Actual)))
9839 and then not Is_Frozen (Etype (Prefix (Actual)))
9841 -- The renaming declaration will create a body, which must appear
9842 -- outside of the instantiation, We move the renaming declaration
9843 -- out of the instance, and create an additional renaming inside,
9844 -- to prevent freezing anomalies.
9847 Anon_Id : constant Entity_Id := Make_Temporary (Loc, 'E');
9850 Set_Defining_Unit_Name (New_Spec, Anon_Id);
9851 Insert_Before (Instantiation_Node, Decl_Node);
9852 Analyze (Decl_Node);
9854 -- Now create renaming within the instance
9857 Make_Subprogram_Renaming_Declaration (Loc,
9858 Specification => New_Copy_Tree (New_Spec),
9859 Name => New_Occurrence_Of (Anon_Id, Loc));
9861 Set_Defining_Unit_Name (Specification (Decl_Node),
9862 Make_Defining_Identifier (Loc, Chars (Formal_Sub)));
9867 end Instantiate_Formal_Subprogram;
9869 ------------------------
9870 -- Instantiate_Object --
9871 ------------------------
9873 function Instantiate_Object
9876 Analyzed_Formal : Node_Id) return List_Id
9878 Gen_Obj : constant Entity_Id := Defining_Identifier (Formal);
9879 A_Gen_Obj : constant Entity_Id :=
9880 Defining_Identifier (Analyzed_Formal);
9881 Acc_Def : Node_Id := Empty;
9882 Act_Assoc : constant Node_Id := Parent (Actual);
9883 Actual_Decl : Node_Id := Empty;
9884 Decl_Node : Node_Id;
9887 List : constant List_Id := New_List;
9888 Loc : constant Source_Ptr := Sloc (Actual);
9889 Orig_Ftyp : constant Entity_Id := Etype (A_Gen_Obj);
9890 Subt_Decl : Node_Id := Empty;
9891 Subt_Mark : Node_Id := Empty;
9894 if Present (Subtype_Mark (Formal)) then
9895 Subt_Mark := Subtype_Mark (Formal);
9897 Check_Access_Definition (Formal);
9898 Acc_Def := Access_Definition (Formal);
9901 -- Sloc for error message on missing actual
9903 Error_Msg_Sloc := Sloc (Scope (A_Gen_Obj));
9905 if Get_Instance_Of (Gen_Obj) /= Gen_Obj then
9906 Error_Msg_N ("duplicate instantiation of generic parameter", Actual);
9909 Set_Parent (List, Parent (Actual));
9913 if Out_Present (Formal) then
9915 -- An IN OUT generic actual must be a name. The instantiation is a
9916 -- renaming declaration. The actual is the name being renamed. We
9917 -- use the actual directly, rather than a copy, because it is not
9918 -- used further in the list of actuals, and because a copy or a use
9919 -- of relocate_node is incorrect if the instance is nested within a
9920 -- generic. In order to simplify ASIS searches, the Generic_Parent
9921 -- field links the declaration to the generic association.
9926 Instantiation_Node, Gen_Obj);
9928 ("\in instantiation of & declared#",
9929 Instantiation_Node, Scope (A_Gen_Obj));
9930 Abandon_Instantiation (Instantiation_Node);
9933 if Present (Subt_Mark) then
9935 Make_Object_Renaming_Declaration (Loc,
9936 Defining_Identifier => New_Copy (Gen_Obj),
9937 Subtype_Mark => New_Copy_Tree (Subt_Mark),
9940 else pragma Assert (Present (Acc_Def));
9942 Make_Object_Renaming_Declaration (Loc,
9943 Defining_Identifier => New_Copy (Gen_Obj),
9944 Access_Definition => New_Copy_Tree (Acc_Def),
9948 Set_Corresponding_Generic_Association (Decl_Node, Act_Assoc);
9950 -- The analysis of the actual may produce Insert_Action nodes, so
9951 -- the declaration must have a context in which to attach them.
9953 Append (Decl_Node, List);
9956 -- Return if the analysis of the actual reported some error
9958 if Etype (Actual) = Any_Type then
9962 -- This check is performed here because Analyze_Object_Renaming will
9963 -- not check it when Comes_From_Source is False. Note though that the
9964 -- check for the actual being the name of an object will be performed
9965 -- in Analyze_Object_Renaming.
9967 if Is_Object_Reference (Actual)
9968 and then Is_Dependent_Component_Of_Mutable_Object (Actual)
9971 ("illegal discriminant-dependent component for in out parameter",
9975 -- The actual has to be resolved in order to check that it is a
9976 -- variable (due to cases such as F (1), where F returns access to
9977 -- an array, and for overloaded prefixes).
9979 Ftyp := Get_Instance_Of (Etype (A_Gen_Obj));
9981 -- If the type of the formal is not itself a formal, and the current
9982 -- unit is a child unit, the formal type must be declared in a
9983 -- parent, and must be retrieved by visibility.
9986 and then Is_Generic_Unit (Scope (Ftyp))
9987 and then Is_Child_Unit (Scope (A_Gen_Obj))
9990 Temp : constant Node_Id :=
9991 New_Copy_Tree (Subtype_Mark (Analyzed_Formal));
9993 Set_Entity (Temp, Empty);
9995 Ftyp := Entity (Temp);
9999 if Is_Private_Type (Ftyp)
10000 and then not Is_Private_Type (Etype (Actual))
10001 and then (Base_Type (Full_View (Ftyp)) = Base_Type (Etype (Actual))
10002 or else Base_Type (Etype (Actual)) = Ftyp)
10004 -- If the actual has the type of the full view of the formal, or
10005 -- else a non-private subtype of the formal, then the visibility
10006 -- of the formal type has changed. Add to the actuals a subtype
10007 -- declaration that will force the exchange of views in the body
10008 -- of the instance as well.
10011 Make_Subtype_Declaration (Loc,
10012 Defining_Identifier => Make_Temporary (Loc, 'P'),
10013 Subtype_Indication => New_Occurrence_Of (Ftyp, Loc));
10015 Prepend (Subt_Decl, List);
10017 Prepend_Elmt (Full_View (Ftyp), Exchanged_Views);
10018 Exchange_Declarations (Ftyp);
10021 Resolve (Actual, Ftyp);
10023 if not Denotes_Variable (Actual) then
10025 ("actual for& must be a variable", Actual, Gen_Obj);
10027 elsif Base_Type (Ftyp) /= Base_Type (Etype (Actual)) then
10029 -- Ada 2005 (AI-423): For a generic formal object of mode in out,
10030 -- the type of the actual shall resolve to a specific anonymous
10033 if Ada_Version < Ada_2005
10035 Ekind (Base_Type (Ftyp)) /=
10036 E_Anonymous_Access_Type
10038 Ekind (Base_Type (Etype (Actual))) /=
10039 E_Anonymous_Access_Type
10041 Error_Msg_NE ("type of actual does not match type of&",
10046 Note_Possible_Modification (Actual, Sure => True);
10048 -- Check for instantiation of atomic/volatile actual for
10049 -- non-atomic/volatile formal (RM C.6 (12)).
10051 if Is_Atomic_Object (Actual)
10052 and then not Is_Atomic (Orig_Ftyp)
10055 ("cannot instantiate non-atomic formal object " &
10056 "with atomic actual", Actual);
10058 elsif Is_Volatile_Object (Actual)
10059 and then not Is_Volatile (Orig_Ftyp)
10062 ("cannot instantiate non-volatile formal object " &
10063 "with volatile actual", Actual);
10066 -- Formal in-parameter
10069 -- The instantiation of a generic formal in-parameter is constant
10070 -- declaration. The actual is the expression for that declaration.
10072 if Present (Actual) then
10073 if Present (Subt_Mark) then
10075 else pragma Assert (Present (Acc_Def));
10080 Make_Object_Declaration (Loc,
10081 Defining_Identifier => New_Copy (Gen_Obj),
10082 Constant_Present => True,
10083 Null_Exclusion_Present => Null_Exclusion_Present (Formal),
10084 Object_Definition => New_Copy_Tree (Def),
10085 Expression => Actual);
10087 Set_Corresponding_Generic_Association (Decl_Node, Act_Assoc);
10089 -- A generic formal object of a tagged type is defined to be
10090 -- aliased so the new constant must also be treated as aliased.
10092 if Is_Tagged_Type (Etype (A_Gen_Obj)) then
10093 Set_Aliased_Present (Decl_Node);
10096 Append (Decl_Node, List);
10098 -- No need to repeat (pre-)analysis of some expression nodes
10099 -- already handled in Preanalyze_Actuals.
10101 if Nkind (Actual) /= N_Allocator then
10104 -- Return if the analysis of the actual reported some error
10106 if Etype (Actual) = Any_Type then
10112 Formal_Type : constant Entity_Id := Etype (A_Gen_Obj);
10116 Typ := Get_Instance_Of (Formal_Type);
10118 Freeze_Before (Instantiation_Node, Typ);
10120 -- If the actual is an aggregate, perform name resolution on
10121 -- its components (the analysis of an aggregate does not do it)
10122 -- to capture local names that may be hidden if the generic is
10125 if Nkind (Actual) = N_Aggregate then
10126 Preanalyze_And_Resolve (Actual, Typ);
10129 if Is_Limited_Type (Typ)
10130 and then not OK_For_Limited_Init (Typ, Actual)
10133 ("initialization not allowed for limited types", Actual);
10134 Explain_Limited_Type (Typ, Actual);
10138 elsif Present (Default_Expression (Formal)) then
10140 -- Use default to construct declaration
10142 if Present (Subt_Mark) then
10144 else pragma Assert (Present (Acc_Def));
10149 Make_Object_Declaration (Sloc (Formal),
10150 Defining_Identifier => New_Copy (Gen_Obj),
10151 Constant_Present => True,
10152 Null_Exclusion_Present => Null_Exclusion_Present (Formal),
10153 Object_Definition => New_Copy (Def),
10154 Expression => New_Copy_Tree
10155 (Default_Expression (Formal)));
10157 Append (Decl_Node, List);
10158 Set_Analyzed (Expression (Decl_Node), False);
10162 ("missing actual&",
10163 Instantiation_Node, Gen_Obj);
10164 Error_Msg_NE ("\in instantiation of & declared#",
10165 Instantiation_Node, Scope (A_Gen_Obj));
10167 if Is_Scalar_Type (Etype (A_Gen_Obj)) then
10169 -- Create dummy constant declaration so that instance can be
10170 -- analyzed, to minimize cascaded visibility errors.
10172 if Present (Subt_Mark) then
10174 else pragma Assert (Present (Acc_Def));
10179 Make_Object_Declaration (Loc,
10180 Defining_Identifier => New_Copy (Gen_Obj),
10181 Constant_Present => True,
10182 Null_Exclusion_Present => Null_Exclusion_Present (Formal),
10183 Object_Definition => New_Copy (Def),
10185 Make_Attribute_Reference (Sloc (Gen_Obj),
10186 Attribute_Name => Name_First,
10187 Prefix => New_Copy (Def)));
10189 Append (Decl_Node, List);
10192 Abandon_Instantiation (Instantiation_Node);
10197 if Nkind (Actual) in N_Has_Entity then
10198 Actual_Decl := Parent (Entity (Actual));
10201 -- Ada 2005 (AI-423): For a formal object declaration with a null
10202 -- exclusion or an access definition that has a null exclusion: If the
10203 -- actual matching the formal object declaration denotes a generic
10204 -- formal object of another generic unit G, and the instantiation
10205 -- containing the actual occurs within the body of G or within the body
10206 -- of a generic unit declared within the declarative region of G, then
10207 -- the declaration of the formal object of G must have a null exclusion.
10208 -- Otherwise, the subtype of the actual matching the formal object
10209 -- declaration shall exclude null.
10211 if Ada_Version >= Ada_2005
10212 and then Present (Actual_Decl)
10214 Nkind_In (Actual_Decl, N_Formal_Object_Declaration,
10215 N_Object_Declaration)
10216 and then Nkind (Analyzed_Formal) = N_Formal_Object_Declaration
10217 and then not Has_Null_Exclusion (Actual_Decl)
10218 and then Has_Null_Exclusion (Analyzed_Formal)
10220 Error_Msg_Sloc := Sloc (Analyzed_Formal);
10222 ("actual must exclude null to match generic formal#", Actual);
10225 -- An effectively volatile object cannot be used as an actual in
10226 -- a generic instance. The following check is only relevant when
10227 -- SPARK_Mode is on as it is not a standard Ada legality rule.
10230 and then Present (Actual)
10231 and then Is_Effectively_Volatile_Object (Actual)
10234 ("volatile object cannot act as actual in generic instantiation "
10235 & "(SPARK RM 7.1.3(8))", Actual);
10239 end Instantiate_Object;
10241 ------------------------------
10242 -- Instantiate_Package_Body --
10243 ------------------------------
10245 procedure Instantiate_Package_Body
10246 (Body_Info : Pending_Body_Info;
10247 Inlined_Body : Boolean := False;
10248 Body_Optional : Boolean := False)
10250 Act_Decl : constant Node_Id := Body_Info.Act_Decl;
10251 Inst_Node : constant Node_Id := Body_Info.Inst_Node;
10252 Loc : constant Source_Ptr := Sloc (Inst_Node);
10254 Gen_Id : constant Node_Id := Name (Inst_Node);
10255 Gen_Unit : constant Entity_Id := Get_Generic_Entity (Inst_Node);
10256 Gen_Decl : constant Node_Id := Unit_Declaration_Node (Gen_Unit);
10257 Act_Spec : constant Node_Id := Specification (Act_Decl);
10258 Act_Decl_Id : constant Entity_Id := Defining_Entity (Act_Spec);
10260 Act_Body_Name : Node_Id;
10261 Gen_Body : Node_Id;
10262 Gen_Body_Id : Node_Id;
10263 Act_Body : Node_Id;
10264 Act_Body_Id : Entity_Id;
10266 Parent_Installed : Boolean := False;
10267 Save_Style_Check : constant Boolean := Style_Check;
10269 Par_Ent : Entity_Id := Empty;
10270 Par_Vis : Boolean := False;
10272 Vis_Prims_List : Elist_Id := No_Elist;
10273 -- List of primitives made temporarily visible in the instantiation
10274 -- to match the visibility of the formal type
10276 procedure Check_Initialized_Types;
10277 -- In a generic package body, an entity of a generic private type may
10278 -- appear uninitialized. This is suspicious, unless the actual is a
10279 -- fully initialized type.
10281 -----------------------------
10282 -- Check_Initialized_Types --
10283 -----------------------------
10285 procedure Check_Initialized_Types is
10287 Formal : Entity_Id;
10288 Actual : Entity_Id;
10289 Uninit_Var : Entity_Id;
10292 Decl := First (Generic_Formal_Declarations (Gen_Decl));
10293 while Present (Decl) loop
10294 Uninit_Var := Empty;
10296 if Nkind (Decl) = N_Private_Extension_Declaration then
10297 Uninit_Var := Uninitialized_Variable (Decl);
10299 elsif Nkind (Decl) = N_Formal_Type_Declaration
10300 and then Nkind (Formal_Type_Definition (Decl)) =
10301 N_Formal_Private_Type_Definition
10304 Uninitialized_Variable (Formal_Type_Definition (Decl));
10307 if Present (Uninit_Var) then
10308 Formal := Defining_Identifier (Decl);
10309 Actual := First_Entity (Act_Decl_Id);
10311 -- For each formal there is a subtype declaration that renames
10312 -- the actual and has the same name as the formal. Locate the
10313 -- formal for warning message about uninitialized variables
10314 -- in the generic, for which the actual type should be a fully
10315 -- initialized type.
10317 while Present (Actual) loop
10318 exit when Ekind (Actual) = E_Package
10319 and then Present (Renamed_Object (Actual));
10321 if Chars (Actual) = Chars (Formal)
10322 and then not Is_Scalar_Type (Actual)
10323 and then not Is_Fully_Initialized_Type (Actual)
10324 and then Warn_On_No_Value_Assigned
10326 Error_Msg_Node_2 := Formal;
10328 ("generic unit has uninitialized variable& of "
10329 & "formal private type &?v?", Actual, Uninit_Var);
10331 ("actual type for& should be fully initialized type?v?",
10336 Next_Entity (Actual);
10342 end Check_Initialized_Types;
10344 -- Start of processing for Instantiate_Package_Body
10347 Gen_Body_Id := Corresponding_Body (Gen_Decl);
10349 -- The instance body may already have been processed, as the parent of
10350 -- another instance that is inlined (Load_Parent_Of_Generic).
10352 if Present (Corresponding_Body (Instance_Spec (Inst_Node))) then
10356 Expander_Mode_Save_And_Set (Body_Info.Expander_Status);
10358 -- Re-establish the state of information on which checks are suppressed.
10359 -- This information was set in Body_Info at the point of instantiation,
10360 -- and now we restore it so that the instance is compiled using the
10361 -- check status at the instantiation (RM 11.5 (7.2/2), AI95-00224-01).
10363 Local_Suppress_Stack_Top := Body_Info.Local_Suppress_Stack_Top;
10364 Scope_Suppress := Body_Info.Scope_Suppress;
10365 Opt.Ada_Version := Body_Info.Version;
10366 Opt.Ada_Version_Pragma := Body_Info.Version_Pragma;
10367 Restore_Warnings (Body_Info.Warnings);
10368 Opt.SPARK_Mode := Body_Info.SPARK_Mode;
10369 Opt.SPARK_Mode_Pragma := Body_Info.SPARK_Mode_Pragma;
10371 if No (Gen_Body_Id) then
10373 -- Do not look for parent of generic body if none is required.
10374 -- This may happen when the routine is called as part of the
10375 -- Pending_Instantiations processing, when nested instances
10376 -- may precede the one generated from the main unit.
10378 if not Unit_Requires_Body (Defining_Entity (Gen_Decl))
10379 and then Body_Optional
10383 Load_Parent_Of_Generic
10384 (Inst_Node, Specification (Gen_Decl), Body_Optional);
10385 Gen_Body_Id := Corresponding_Body (Gen_Decl);
10389 -- Establish global variable for sloc adjustment and for error recovery
10391 Instantiation_Node := Inst_Node;
10393 if Present (Gen_Body_Id) then
10394 Save_Env (Gen_Unit, Act_Decl_Id);
10395 Style_Check := False;
10396 Current_Sem_Unit := Body_Info.Current_Sem_Unit;
10398 Gen_Body := Unit_Declaration_Node (Gen_Body_Id);
10400 Create_Instantiation_Source
10401 (Inst_Node, Gen_Body_Id, False, S_Adjustment);
10405 (Original_Node (Gen_Body), Empty, Instantiating => True);
10407 -- Build new name (possibly qualified) for body declaration
10409 Act_Body_Id := New_Copy (Act_Decl_Id);
10411 -- Some attributes of spec entity are not inherited by body entity
10413 Set_Handler_Records (Act_Body_Id, No_List);
10415 if Nkind (Defining_Unit_Name (Act_Spec)) =
10416 N_Defining_Program_Unit_Name
10419 Make_Defining_Program_Unit_Name (Loc,
10420 Name => New_Copy_Tree (Name (Defining_Unit_Name (Act_Spec))),
10421 Defining_Identifier => Act_Body_Id);
10423 Act_Body_Name := Act_Body_Id;
10426 Set_Defining_Unit_Name (Act_Body, Act_Body_Name);
10428 Set_Corresponding_Spec (Act_Body, Act_Decl_Id);
10429 Check_Generic_Actuals (Act_Decl_Id, False);
10430 Check_Initialized_Types;
10432 -- Install primitives hidden at the point of the instantiation but
10433 -- visible when processing the generic formals
10439 E := First_Entity (Act_Decl_Id);
10440 while Present (E) loop
10442 and then Is_Generic_Actual_Type (E)
10443 and then Is_Tagged_Type (E)
10445 Install_Hidden_Primitives
10446 (Prims_List => Vis_Prims_List,
10447 Gen_T => Generic_Parent_Type (Parent (E)),
10455 -- If it is a child unit, make the parent instance (which is an
10456 -- instance of the parent of the generic) visible. The parent
10457 -- instance is the prefix of the name of the generic unit.
10459 if Ekind (Scope (Gen_Unit)) = E_Generic_Package
10460 and then Nkind (Gen_Id) = N_Expanded_Name
10462 Par_Ent := Entity (Prefix (Gen_Id));
10463 Par_Vis := Is_Immediately_Visible (Par_Ent);
10464 Install_Parent (Par_Ent, In_Body => True);
10465 Parent_Installed := True;
10467 elsif Is_Child_Unit (Gen_Unit) then
10468 Par_Ent := Scope (Gen_Unit);
10469 Par_Vis := Is_Immediately_Visible (Par_Ent);
10470 Install_Parent (Par_Ent, In_Body => True);
10471 Parent_Installed := True;
10474 -- If the instantiation is a library unit, and this is the main unit,
10475 -- then build the resulting compilation unit nodes for the instance.
10476 -- If this is a compilation unit but it is not the main unit, then it
10477 -- is the body of a unit in the context, that is being compiled
10478 -- because it is encloses some inlined unit or another generic unit
10479 -- being instantiated. In that case, this body is not part of the
10480 -- current compilation, and is not attached to the tree, but its
10481 -- parent must be set for analysis.
10483 if Nkind (Parent (Inst_Node)) = N_Compilation_Unit then
10485 -- Replace instance node with body of instance, and create new
10486 -- node for corresponding instance declaration.
10488 Build_Instance_Compilation_Unit_Nodes
10489 (Inst_Node, Act_Body, Act_Decl);
10490 Analyze (Inst_Node);
10492 if Parent (Inst_Node) = Cunit (Main_Unit) then
10494 -- If the instance is a child unit itself, then set the scope
10495 -- of the expanded body to be the parent of the instantiation
10496 -- (ensuring that the fully qualified name will be generated
10497 -- for the elaboration subprogram).
10499 if Nkind (Defining_Unit_Name (Act_Spec)) =
10500 N_Defining_Program_Unit_Name
10503 (Defining_Entity (Inst_Node), Scope (Act_Decl_Id));
10507 -- Case where instantiation is not a library unit
10510 -- If this is an early instantiation, i.e. appears textually
10511 -- before the corresponding body and must be elaborated first,
10512 -- indicate that the body instance is to be delayed.
10514 Install_Body (Act_Body, Inst_Node, Gen_Body, Gen_Decl);
10516 -- Now analyze the body. We turn off all checks if this is an
10517 -- internal unit, since there is no reason to have checks on for
10518 -- any predefined run-time library code. All such code is designed
10519 -- to be compiled with checks off.
10521 -- Note that we do NOT apply this criterion to children of GNAT
10522 -- The latter units must suppress checks explicitly if needed.
10524 if Is_Predefined_File_Name
10525 (Unit_File_Name (Get_Source_Unit (Gen_Decl)))
10527 Analyze (Act_Body, Suppress => All_Checks);
10529 Analyze (Act_Body);
10533 Inherit_Context (Gen_Body, Inst_Node);
10535 -- Remove the parent instances if they have been placed on the scope
10536 -- stack to compile the body.
10538 if Parent_Installed then
10539 Remove_Parent (In_Body => True);
10541 -- Restore the previous visibility of the parent
10543 Set_Is_Immediately_Visible (Par_Ent, Par_Vis);
10546 Restore_Hidden_Primitives (Vis_Prims_List);
10547 Restore_Private_Views (Act_Decl_Id);
10549 -- Remove the current unit from visibility if this is an instance
10550 -- that is not elaborated on the fly for inlining purposes.
10552 if not Inlined_Body then
10553 Set_Is_Immediately_Visible (Act_Decl_Id, False);
10557 Style_Check := Save_Style_Check;
10559 -- If we have no body, and the unit requires a body, then complain. This
10560 -- complaint is suppressed if we have detected other errors (since a
10561 -- common reason for missing the body is that it had errors).
10562 -- In CodePeer mode, a warning has been emitted already, no need for
10563 -- further messages.
10565 elsif Unit_Requires_Body (Gen_Unit)
10566 and then not Body_Optional
10568 if CodePeer_Mode then
10571 elsif Serious_Errors_Detected = 0 then
10573 ("cannot find body of generic package &", Inst_Node, Gen_Unit);
10575 -- Don't attempt to perform any cleanup actions if some other error
10576 -- was already detected, since this can cause blowups.
10582 -- Case of package that does not need a body
10585 -- If the instantiation of the declaration is a library unit, rewrite
10586 -- the original package instantiation as a package declaration in the
10587 -- compilation unit node.
10589 if Nkind (Parent (Inst_Node)) = N_Compilation_Unit then
10590 Set_Parent_Spec (Act_Decl, Parent_Spec (Inst_Node));
10591 Rewrite (Inst_Node, Act_Decl);
10593 -- Generate elaboration entity, in case spec has elaboration code.
10594 -- This cannot be done when the instance is analyzed, because it
10595 -- is not known yet whether the body exists.
10597 Set_Elaboration_Entity_Required (Act_Decl_Id, False);
10598 Build_Elaboration_Entity (Parent (Inst_Node), Act_Decl_Id);
10600 -- If the instantiation is not a library unit, then append the
10601 -- declaration to the list of implicitly generated entities, unless
10602 -- it is already a list member which means that it was already
10605 elsif not Is_List_Member (Act_Decl) then
10606 Mark_Rewrite_Insertion (Act_Decl);
10607 Insert_Before (Inst_Node, Act_Decl);
10611 Expander_Mode_Restore;
10612 end Instantiate_Package_Body;
10614 ---------------------------------
10615 -- Instantiate_Subprogram_Body --
10616 ---------------------------------
10618 procedure Instantiate_Subprogram_Body
10619 (Body_Info : Pending_Body_Info;
10620 Body_Optional : Boolean := False)
10622 Act_Decl : constant Node_Id := Body_Info.Act_Decl;
10623 Inst_Node : constant Node_Id := Body_Info.Inst_Node;
10624 Loc : constant Source_Ptr := Sloc (Inst_Node);
10625 Gen_Id : constant Node_Id := Name (Inst_Node);
10626 Gen_Unit : constant Entity_Id := Get_Generic_Entity (Inst_Node);
10627 Gen_Decl : constant Node_Id := Unit_Declaration_Node (Gen_Unit);
10628 Anon_Id : constant Entity_Id :=
10629 Defining_Unit_Name (Specification (Act_Decl));
10630 Pack_Id : constant Entity_Id :=
10631 Defining_Unit_Name (Parent (Act_Decl));
10633 Gen_Body : Node_Id;
10634 Gen_Body_Id : Node_Id;
10635 Act_Body : Node_Id;
10636 Pack_Body : Node_Id;
10637 Prev_Formal : Entity_Id;
10638 Ret_Expr : Node_Id;
10639 Unit_Renaming : Node_Id;
10641 Parent_Installed : Boolean := False;
10643 Saved_Style_Check : constant Boolean := Style_Check;
10644 Saved_Warnings : constant Warning_Record := Save_Warnings;
10646 Par_Ent : Entity_Id := Empty;
10647 Par_Vis : Boolean := False;
10650 Gen_Body_Id := Corresponding_Body (Gen_Decl);
10652 -- Subprogram body may have been created already because of an inline
10653 -- pragma, or because of multiple elaborations of the enclosing package
10654 -- when several instances of the subprogram appear in the main unit.
10656 if Present (Corresponding_Body (Act_Decl)) then
10660 Expander_Mode_Save_And_Set (Body_Info.Expander_Status);
10662 -- Re-establish the state of information on which checks are suppressed.
10663 -- This information was set in Body_Info at the point of instantiation,
10664 -- and now we restore it so that the instance is compiled using the
10665 -- check status at the instantiation (RM 11.5 (7.2/2), AI95-00224-01).
10667 Local_Suppress_Stack_Top := Body_Info.Local_Suppress_Stack_Top;
10668 Scope_Suppress := Body_Info.Scope_Suppress;
10669 Opt.Ada_Version := Body_Info.Version;
10670 Opt.Ada_Version_Pragma := Body_Info.Version_Pragma;
10671 Restore_Warnings (Body_Info.Warnings);
10672 Opt.SPARK_Mode := Body_Info.SPARK_Mode;
10673 Opt.SPARK_Mode_Pragma := Body_Info.SPARK_Mode_Pragma;
10675 if No (Gen_Body_Id) then
10677 -- For imported generic subprogram, no body to compile, complete
10678 -- the spec entity appropriately.
10680 if Is_Imported (Gen_Unit) then
10681 Set_Is_Imported (Anon_Id);
10682 Set_First_Rep_Item (Anon_Id, First_Rep_Item (Gen_Unit));
10683 Set_Interface_Name (Anon_Id, Interface_Name (Gen_Unit));
10684 Set_Convention (Anon_Id, Convention (Gen_Unit));
10685 Set_Has_Completion (Anon_Id);
10688 -- For other cases, compile the body
10691 Load_Parent_Of_Generic
10692 (Inst_Node, Specification (Gen_Decl), Body_Optional);
10693 Gen_Body_Id := Corresponding_Body (Gen_Decl);
10697 Instantiation_Node := Inst_Node;
10699 if Present (Gen_Body_Id) then
10700 Gen_Body := Unit_Declaration_Node (Gen_Body_Id);
10702 if Nkind (Gen_Body) = N_Subprogram_Body_Stub then
10704 -- Either body is not present, or context is non-expanding, as
10705 -- when compiling a subunit. Mark the instance as completed, and
10706 -- diagnose a missing body when needed.
10709 and then Operating_Mode = Generate_Code
10712 ("missing proper body for instantiation", Gen_Body);
10715 Set_Has_Completion (Anon_Id);
10719 Save_Env (Gen_Unit, Anon_Id);
10720 Style_Check := False;
10721 Current_Sem_Unit := Body_Info.Current_Sem_Unit;
10722 Create_Instantiation_Source
10730 (Original_Node (Gen_Body), Empty, Instantiating => True);
10732 -- Create proper defining name for the body, to correspond to
10733 -- the one in the spec.
10735 Set_Defining_Unit_Name (Specification (Act_Body),
10736 Make_Defining_Identifier
10737 (Sloc (Defining_Entity (Inst_Node)), Chars (Anon_Id)));
10738 Set_Corresponding_Spec (Act_Body, Anon_Id);
10739 Set_Has_Completion (Anon_Id);
10740 Check_Generic_Actuals (Pack_Id, False);
10742 -- Generate a reference to link the visible subprogram instance to
10743 -- the generic body, which for navigation purposes is the only
10744 -- available source for the instance.
10747 (Related_Instance (Pack_Id),
10748 Gen_Body_Id, 'b', Set_Ref => False, Force => True);
10750 -- If it is a child unit, make the parent instance (which is an
10751 -- instance of the parent of the generic) visible. The parent
10752 -- instance is the prefix of the name of the generic unit.
10754 if Ekind (Scope (Gen_Unit)) = E_Generic_Package
10755 and then Nkind (Gen_Id) = N_Expanded_Name
10757 Par_Ent := Entity (Prefix (Gen_Id));
10758 Par_Vis := Is_Immediately_Visible (Par_Ent);
10759 Install_Parent (Par_Ent, In_Body => True);
10760 Parent_Installed := True;
10762 elsif Is_Child_Unit (Gen_Unit) then
10763 Par_Ent := Scope (Gen_Unit);
10764 Par_Vis := Is_Immediately_Visible (Par_Ent);
10765 Install_Parent (Par_Ent, In_Body => True);
10766 Parent_Installed := True;
10769 -- Inside its body, a reference to the generic unit is a reference
10770 -- to the instance. The corresponding renaming is the first
10771 -- declaration in the body.
10774 Make_Subprogram_Renaming_Declaration (Loc,
10776 Copy_Generic_Node (
10777 Specification (Original_Node (Gen_Body)),
10779 Instantiating => True),
10780 Name => New_Occurrence_Of (Anon_Id, Loc));
10782 -- If there is a formal subprogram with the same name as the unit
10783 -- itself, do not add this renaming declaration. This is a temporary
10784 -- fix for one ACVC test. ???
10786 Prev_Formal := First_Entity (Pack_Id);
10787 while Present (Prev_Formal) loop
10788 if Chars (Prev_Formal) = Chars (Gen_Unit)
10789 and then Is_Overloadable (Prev_Formal)
10794 Next_Entity (Prev_Formal);
10797 if Present (Prev_Formal) then
10798 Decls := New_List (Act_Body);
10800 Decls := New_List (Unit_Renaming, Act_Body);
10803 -- The subprogram body is placed in the body of a dummy package body,
10804 -- whose spec contains the subprogram declaration as well as the
10805 -- renaming declarations for the generic parameters.
10807 Pack_Body := Make_Package_Body (Loc,
10808 Defining_Unit_Name => New_Copy (Pack_Id),
10809 Declarations => Decls);
10811 Set_Corresponding_Spec (Pack_Body, Pack_Id);
10813 -- If the instantiation is a library unit, then build resulting
10814 -- compilation unit nodes for the instance. The declaration of
10815 -- the enclosing package is the grandparent of the subprogram
10816 -- declaration. First replace the instantiation node as the unit
10817 -- of the corresponding compilation.
10819 if Nkind (Parent (Inst_Node)) = N_Compilation_Unit then
10820 if Parent (Inst_Node) = Cunit (Main_Unit) then
10821 Set_Unit (Parent (Inst_Node), Inst_Node);
10822 Build_Instance_Compilation_Unit_Nodes
10823 (Inst_Node, Pack_Body, Parent (Parent (Act_Decl)));
10824 Analyze (Inst_Node);
10826 Set_Parent (Pack_Body, Parent (Inst_Node));
10827 Analyze (Pack_Body);
10831 Insert_Before (Inst_Node, Pack_Body);
10832 Mark_Rewrite_Insertion (Pack_Body);
10833 Analyze (Pack_Body);
10835 if Expander_Active then
10836 Freeze_Subprogram_Body (Inst_Node, Gen_Body, Pack_Id);
10840 Inherit_Context (Gen_Body, Inst_Node);
10842 Restore_Private_Views (Pack_Id, False);
10844 if Parent_Installed then
10845 Remove_Parent (In_Body => True);
10847 -- Restore the previous visibility of the parent
10849 Set_Is_Immediately_Visible (Par_Ent, Par_Vis);
10853 Style_Check := Saved_Style_Check;
10854 Restore_Warnings (Saved_Warnings);
10856 -- Body not found. Error was emitted already. If there were no previous
10857 -- errors, this may be an instance whose scope is a premature instance.
10858 -- In that case we must insure that the (legal) program does raise
10859 -- program error if executed. We generate a subprogram body for this
10860 -- purpose. See DEC ac30vso.
10862 -- Should not reference proprietary DEC tests in comments ???
10864 elsif Serious_Errors_Detected = 0
10865 and then Nkind (Parent (Inst_Node)) /= N_Compilation_Unit
10867 if Body_Optional then
10870 elsif Ekind (Anon_Id) = E_Procedure then
10872 Make_Subprogram_Body (Loc,
10874 Make_Procedure_Specification (Loc,
10875 Defining_Unit_Name =>
10876 Make_Defining_Identifier (Loc, Chars (Anon_Id)),
10877 Parameter_Specifications =>
10879 (Parameter_Specifications (Parent (Anon_Id)))),
10881 Declarations => Empty_List,
10882 Handled_Statement_Sequence =>
10883 Make_Handled_Sequence_Of_Statements (Loc,
10886 Make_Raise_Program_Error (Loc,
10888 PE_Access_Before_Elaboration))));
10892 Make_Raise_Program_Error (Loc,
10893 Reason => PE_Access_Before_Elaboration);
10895 Set_Etype (Ret_Expr, (Etype (Anon_Id)));
10896 Set_Analyzed (Ret_Expr);
10899 Make_Subprogram_Body (Loc,
10901 Make_Function_Specification (Loc,
10902 Defining_Unit_Name =>
10903 Make_Defining_Identifier (Loc, Chars (Anon_Id)),
10904 Parameter_Specifications =>
10906 (Parameter_Specifications (Parent (Anon_Id))),
10907 Result_Definition =>
10908 New_Occurrence_Of (Etype (Anon_Id), Loc)),
10910 Declarations => Empty_List,
10911 Handled_Statement_Sequence =>
10912 Make_Handled_Sequence_Of_Statements (Loc,
10915 (Make_Simple_Return_Statement (Loc, Ret_Expr))));
10918 Pack_Body := Make_Package_Body (Loc,
10919 Defining_Unit_Name => New_Copy (Pack_Id),
10920 Declarations => New_List (Act_Body));
10922 Insert_After (Inst_Node, Pack_Body);
10923 Set_Corresponding_Spec (Pack_Body, Pack_Id);
10924 Analyze (Pack_Body);
10927 Expander_Mode_Restore;
10928 end Instantiate_Subprogram_Body;
10930 ----------------------
10931 -- Instantiate_Type --
10932 ----------------------
10934 function Instantiate_Type
10937 Analyzed_Formal : Node_Id;
10938 Actual_Decls : List_Id) return List_Id
10940 Gen_T : constant Entity_Id := Defining_Identifier (Formal);
10941 A_Gen_T : constant Entity_Id :=
10942 Defining_Identifier (Analyzed_Formal);
10943 Ancestor : Entity_Id := Empty;
10944 Def : constant Node_Id := Formal_Type_Definition (Formal);
10946 Decl_Node : Node_Id;
10947 Decl_Nodes : List_Id;
10951 procedure Diagnose_Predicated_Actual;
10952 -- There are a number of constructs in which a discrete type with
10953 -- predicates is illegal, e.g. as an index in an array type declaration.
10954 -- If a generic type is used is such a construct in a generic package
10955 -- declaration, it carries the flag No_Predicate_On_Actual. it is part
10956 -- of the generic contract that the actual cannot have predicates.
10958 procedure Validate_Array_Type_Instance;
10959 procedure Validate_Access_Subprogram_Instance;
10960 procedure Validate_Access_Type_Instance;
10961 procedure Validate_Derived_Type_Instance;
10962 procedure Validate_Derived_Interface_Type_Instance;
10963 procedure Validate_Discriminated_Formal_Type;
10964 procedure Validate_Interface_Type_Instance;
10965 procedure Validate_Private_Type_Instance;
10966 procedure Validate_Incomplete_Type_Instance;
10967 -- These procedures perform validation tests for the named case.
10968 -- Validate_Discriminated_Formal_Type is shared by formal private
10969 -- types and Ada 2012 formal incomplete types.
10971 function Subtypes_Match (Gen_T, Act_T : Entity_Id) return Boolean;
10972 -- Check that base types are the same and that the subtypes match
10973 -- statically. Used in several of the above.
10975 ---------------------------------
10976 -- Diagnose_Predicated_Actual --
10977 ---------------------------------
10979 procedure Diagnose_Predicated_Actual is
10981 if No_Predicate_On_Actual (A_Gen_T)
10982 and then Has_Predicates (Act_T)
10985 ("actual for& cannot be a type with predicate",
10986 Instantiation_Node, A_Gen_T);
10988 elsif No_Dynamic_Predicate_On_Actual (A_Gen_T)
10989 and then Has_Predicates (Act_T)
10990 and then not Has_Static_Predicate_Aspect (Act_T)
10993 ("actual for& cannot be a type with a dynamic predicate",
10994 Instantiation_Node, A_Gen_T);
10996 end Diagnose_Predicated_Actual;
10998 --------------------
10999 -- Subtypes_Match --
11000 --------------------
11002 function Subtypes_Match (Gen_T, Act_T : Entity_Id) return Boolean is
11003 T : constant Entity_Id := Get_Instance_Of (Gen_T);
11006 -- Some detailed comments would be useful here ???
11008 return ((Base_Type (T) = Act_T
11009 or else Base_Type (T) = Base_Type (Act_T))
11010 and then Subtypes_Statically_Match (T, Act_T))
11012 or else (Is_Class_Wide_Type (Gen_T)
11013 and then Is_Class_Wide_Type (Act_T)
11014 and then Subtypes_Match
11015 (Get_Instance_Of (Root_Type (Gen_T)),
11016 Root_Type (Act_T)))
11019 (Ekind_In (Gen_T, E_Anonymous_Access_Subprogram_Type,
11020 E_Anonymous_Access_Type)
11021 and then Ekind (Act_T) = Ekind (Gen_T)
11022 and then Subtypes_Statically_Match
11023 (Designated_Type (Gen_T), Designated_Type (Act_T)));
11024 end Subtypes_Match;
11026 -----------------------------------------
11027 -- Validate_Access_Subprogram_Instance --
11028 -----------------------------------------
11030 procedure Validate_Access_Subprogram_Instance is
11032 if not Is_Access_Type (Act_T)
11033 or else Ekind (Designated_Type (Act_T)) /= E_Subprogram_Type
11036 ("expect access type in instantiation of &", Actual, Gen_T);
11037 Abandon_Instantiation (Actual);
11040 -- According to AI05-288, actuals for access_to_subprograms must be
11041 -- subtype conformant with the generic formal. Previous to AI05-288
11042 -- only mode conformance was required.
11044 -- This is a binding interpretation that applies to previous versions
11045 -- of the language, no need to maintain previous weaker checks.
11047 Check_Subtype_Conformant
11048 (Designated_Type (Act_T),
11049 Designated_Type (A_Gen_T),
11053 if Ekind (Base_Type (Act_T)) = E_Access_Protected_Subprogram_Type then
11054 if Ekind (A_Gen_T) = E_Access_Subprogram_Type then
11056 ("protected access type not allowed for formal &",
11060 elsif Ekind (A_Gen_T) = E_Access_Protected_Subprogram_Type then
11062 ("expect protected access type for formal &",
11065 end Validate_Access_Subprogram_Instance;
11067 -----------------------------------
11068 -- Validate_Access_Type_Instance --
11069 -----------------------------------
11071 procedure Validate_Access_Type_Instance is
11072 Desig_Type : constant Entity_Id :=
11073 Find_Actual_Type (Designated_Type (A_Gen_T), A_Gen_T);
11074 Desig_Act : Entity_Id;
11077 if not Is_Access_Type (Act_T) then
11079 ("expect access type in instantiation of &", Actual, Gen_T);
11080 Abandon_Instantiation (Actual);
11083 if Is_Access_Constant (A_Gen_T) then
11084 if not Is_Access_Constant (Act_T) then
11086 ("actual type must be access-to-constant type", Actual);
11087 Abandon_Instantiation (Actual);
11090 if Is_Access_Constant (Act_T) then
11092 ("actual type must be access-to-variable type", Actual);
11093 Abandon_Instantiation (Actual);
11095 elsif Ekind (A_Gen_T) = E_General_Access_Type
11096 and then Ekind (Base_Type (Act_T)) /= E_General_Access_Type
11098 Error_Msg_N -- CODEFIX
11099 ("actual must be general access type!", Actual);
11100 Error_Msg_NE -- CODEFIX
11101 ("add ALL to }!", Actual, Act_T);
11102 Abandon_Instantiation (Actual);
11106 -- The designated subtypes, that is to say the subtypes introduced
11107 -- by an access type declaration (and not by a subtype declaration)
11110 Desig_Act := Designated_Type (Base_Type (Act_T));
11112 -- The designated type may have been introduced through a limited_
11113 -- with clause, in which case retrieve the non-limited view. This
11114 -- applies to incomplete types as well as to class-wide types.
11116 if From_Limited_With (Desig_Act) then
11117 Desig_Act := Available_View (Desig_Act);
11120 if not Subtypes_Match (Desig_Type, Desig_Act) then
11122 ("designated type of actual does not match that of formal &",
11125 if not Predicates_Match (Desig_Type, Desig_Act) then
11126 Error_Msg_N ("\predicates do not match", Actual);
11129 Abandon_Instantiation (Actual);
11131 elsif Is_Access_Type (Designated_Type (Act_T))
11132 and then Is_Constrained (Designated_Type (Designated_Type (Act_T)))
11134 Is_Constrained (Designated_Type (Desig_Type))
11137 ("designated type of actual does not match that of formal &",
11140 if not Predicates_Match (Desig_Type, Desig_Act) then
11141 Error_Msg_N ("\predicates do not match", Actual);
11144 Abandon_Instantiation (Actual);
11147 -- Ada 2005: null-exclusion indicators of the two types must agree
11149 if Can_Never_Be_Null (A_Gen_T) /= Can_Never_Be_Null (Act_T) then
11151 ("non null exclusion of actual and formal & do not match",
11154 end Validate_Access_Type_Instance;
11156 ----------------------------------
11157 -- Validate_Array_Type_Instance --
11158 ----------------------------------
11160 procedure Validate_Array_Type_Instance is
11165 function Formal_Dimensions return Int;
11166 -- Count number of dimensions in array type formal
11168 -----------------------
11169 -- Formal_Dimensions --
11170 -----------------------
11172 function Formal_Dimensions return Int is
11177 if Nkind (Def) = N_Constrained_Array_Definition then
11178 Index := First (Discrete_Subtype_Definitions (Def));
11180 Index := First (Subtype_Marks (Def));
11183 while Present (Index) loop
11185 Next_Index (Index);
11189 end Formal_Dimensions;
11191 -- Start of processing for Validate_Array_Type_Instance
11194 if not Is_Array_Type (Act_T) then
11196 ("expect array type in instantiation of &", Actual, Gen_T);
11197 Abandon_Instantiation (Actual);
11199 elsif Nkind (Def) = N_Constrained_Array_Definition then
11200 if not (Is_Constrained (Act_T)) then
11202 ("expect constrained array in instantiation of &",
11204 Abandon_Instantiation (Actual);
11208 if Is_Constrained (Act_T) then
11210 ("expect unconstrained array in instantiation of &",
11212 Abandon_Instantiation (Actual);
11216 if Formal_Dimensions /= Number_Dimensions (Act_T) then
11218 ("dimensions of actual do not match formal &", Actual, Gen_T);
11219 Abandon_Instantiation (Actual);
11222 I1 := First_Index (A_Gen_T);
11223 I2 := First_Index (Act_T);
11224 for J in 1 .. Formal_Dimensions loop
11226 -- If the indexes of the actual were given by a subtype_mark,
11227 -- the index was transformed into a range attribute. Retrieve
11228 -- the original type mark for checking.
11230 if Is_Entity_Name (Original_Node (I2)) then
11231 T2 := Entity (Original_Node (I2));
11236 if not Subtypes_Match
11237 (Find_Actual_Type (Etype (I1), A_Gen_T), T2)
11240 ("index types of actual do not match those of formal &",
11242 Abandon_Instantiation (Actual);
11249 -- Check matching subtypes. Note that there are complex visibility
11250 -- issues when the generic is a child unit and some aspect of the
11251 -- generic type is declared in a parent unit of the generic. We do
11252 -- the test to handle this special case only after a direct check
11253 -- for static matching has failed. The case where both the component
11254 -- type and the array type are separate formals, and the component
11255 -- type is a private view may also require special checking in
11259 (Component_Type (A_Gen_T), Component_Type (Act_T))
11260 or else Subtypes_Match
11261 (Find_Actual_Type (Component_Type (A_Gen_T), A_Gen_T),
11262 Component_Type (Act_T))
11267 ("component subtype of actual does not match that of formal &",
11269 Abandon_Instantiation (Actual);
11272 if Has_Aliased_Components (A_Gen_T)
11273 and then not Has_Aliased_Components (Act_T)
11276 ("actual must have aliased components to match formal type &",
11279 end Validate_Array_Type_Instance;
11281 -----------------------------------------------
11282 -- Validate_Derived_Interface_Type_Instance --
11283 -----------------------------------------------
11285 procedure Validate_Derived_Interface_Type_Instance is
11286 Par : constant Entity_Id := Entity (Subtype_Indication (Def));
11290 -- First apply interface instance checks
11292 Validate_Interface_Type_Instance;
11294 -- Verify that immediate parent interface is an ancestor of
11298 and then not Interface_Present_In_Ancestor (Act_T, Par)
11301 ("interface actual must include progenitor&", Actual, Par);
11304 -- Now verify that the actual includes all other ancestors of
11307 Elmt := First_Elmt (Interfaces (A_Gen_T));
11308 while Present (Elmt) loop
11309 if not Interface_Present_In_Ancestor
11310 (Act_T, Get_Instance_Of (Node (Elmt)))
11313 ("interface actual must include progenitor&",
11314 Actual, Node (Elmt));
11319 end Validate_Derived_Interface_Type_Instance;
11321 ------------------------------------
11322 -- Validate_Derived_Type_Instance --
11323 ------------------------------------
11325 procedure Validate_Derived_Type_Instance is
11326 Actual_Discr : Entity_Id;
11327 Ancestor_Discr : Entity_Id;
11330 -- If the parent type in the generic declaration is itself a previous
11331 -- formal type, then it is local to the generic and absent from the
11332 -- analyzed generic definition. In that case the ancestor is the
11333 -- instance of the formal (which must have been instantiated
11334 -- previously), unless the ancestor is itself a formal derived type.
11335 -- In this latter case (which is the subject of Corrigendum 8652/0038
11336 -- (AI-202) the ancestor of the formals is the ancestor of its
11337 -- parent. Otherwise, the analyzed generic carries the parent type.
11338 -- If the parent type is defined in a previous formal package, then
11339 -- the scope of that formal package is that of the generic type
11340 -- itself, and it has already been mapped into the corresponding type
11341 -- in the actual package.
11343 -- Common case: parent type defined outside of the generic
11345 if Is_Entity_Name (Subtype_Mark (Def))
11346 and then Present (Entity (Subtype_Mark (Def)))
11348 Ancestor := Get_Instance_Of (Entity (Subtype_Mark (Def)));
11350 -- Check whether parent is defined in a previous formal package
11353 Scope (Scope (Base_Type (Etype (A_Gen_T)))) = Scope (A_Gen_T)
11356 Get_Instance_Of (Base_Type (Etype (A_Gen_T)));
11358 -- The type may be a local derivation, or a type extension of a
11359 -- previous formal, or of a formal of a parent package.
11361 elsif Is_Derived_Type (Get_Instance_Of (A_Gen_T))
11363 Ekind (Get_Instance_Of (A_Gen_T)) = E_Record_Type_With_Private
11365 -- Check whether the parent is another derived formal type in the
11366 -- same generic unit.
11368 if Etype (A_Gen_T) /= A_Gen_T
11369 and then Is_Generic_Type (Etype (A_Gen_T))
11370 and then Scope (Etype (A_Gen_T)) = Scope (A_Gen_T)
11371 and then Etype (Etype (A_Gen_T)) /= Etype (A_Gen_T)
11373 -- Locate ancestor of parent from the subtype declaration
11374 -- created for the actual.
11380 Decl := First (Actual_Decls);
11381 while Present (Decl) loop
11382 if Nkind (Decl) = N_Subtype_Declaration
11383 and then Chars (Defining_Identifier (Decl)) =
11384 Chars (Etype (A_Gen_T))
11386 Ancestor := Generic_Parent_Type (Decl);
11394 pragma Assert (Present (Ancestor));
11396 -- The ancestor itself may be a previous formal that has been
11399 Ancestor := Get_Instance_Of (Ancestor);
11403 Get_Instance_Of (Base_Type (Get_Instance_Of (A_Gen_T)));
11406 -- An unusual case: the actual is a type declared in a parent unit,
11407 -- but is not a formal type so there is no instance_of for it.
11408 -- Retrieve it by analyzing the record extension.
11410 elsif Is_Child_Unit (Scope (A_Gen_T))
11411 and then In_Open_Scopes (Scope (Act_T))
11412 and then Is_Generic_Instance (Scope (Act_T))
11414 Analyze (Subtype_Mark (Def));
11415 Ancestor := Entity (Subtype_Mark (Def));
11418 Ancestor := Get_Instance_Of (Etype (Base_Type (A_Gen_T)));
11421 -- If the formal derived type has pragma Preelaborable_Initialization
11422 -- then the actual type must have preelaborable initialization.
11424 if Known_To_Have_Preelab_Init (A_Gen_T)
11425 and then not Has_Preelaborable_Initialization (Act_T)
11428 ("actual for & must have preelaborable initialization",
11432 -- Ada 2005 (AI-251)
11434 if Ada_Version >= Ada_2005 and then Is_Interface (Ancestor) then
11435 if not Interface_Present_In_Ancestor (Act_T, Ancestor) then
11437 ("(Ada 2005) expected type implementing & in instantiation",
11441 elsif not Is_Ancestor (Base_Type (Ancestor), Act_T) then
11443 ("expect type derived from & in instantiation",
11444 Actual, First_Subtype (Ancestor));
11445 Abandon_Instantiation (Actual);
11448 -- Ada 2005 (AI-443): Synchronized formal derived type checks. Note
11449 -- that the formal type declaration has been rewritten as a private
11452 if Ada_Version >= Ada_2005
11453 and then Nkind (Parent (A_Gen_T)) = N_Private_Extension_Declaration
11454 and then Synchronized_Present (Parent (A_Gen_T))
11456 -- The actual must be a synchronized tagged type
11458 if not Is_Tagged_Type (Act_T) then
11460 ("actual of synchronized type must be tagged", Actual);
11461 Abandon_Instantiation (Actual);
11463 elsif Nkind (Parent (Act_T)) = N_Full_Type_Declaration
11464 and then Nkind (Type_Definition (Parent (Act_T))) =
11465 N_Derived_Type_Definition
11466 and then not Synchronized_Present (Type_Definition
11470 ("actual of synchronized type must be synchronized", Actual);
11471 Abandon_Instantiation (Actual);
11475 -- Perform atomic/volatile checks (RM C.6(12)). Note that AI05-0218-1
11476 -- removes the second instance of the phrase "or allow pass by copy".
11478 if Is_Atomic (Act_T) and then not Is_Atomic (Ancestor) then
11480 ("cannot have atomic actual type for non-atomic formal type",
11483 elsif Is_Volatile (Act_T) and then not Is_Volatile (Ancestor) then
11485 ("cannot have volatile actual type for non-volatile formal type",
11489 -- It should not be necessary to check for unknown discriminants on
11490 -- Formal, but for some reason Has_Unknown_Discriminants is false for
11491 -- A_Gen_T, so Is_Indefinite_Subtype incorrectly returns False. This
11492 -- needs fixing. ???
11494 if not Is_Indefinite_Subtype (A_Gen_T)
11495 and then not Unknown_Discriminants_Present (Formal)
11496 and then Is_Indefinite_Subtype (Act_T)
11499 ("actual subtype must be constrained", Actual);
11500 Abandon_Instantiation (Actual);
11503 if not Unknown_Discriminants_Present (Formal) then
11504 if Is_Constrained (Ancestor) then
11505 if not Is_Constrained (Act_T) then
11507 ("actual subtype must be constrained", Actual);
11508 Abandon_Instantiation (Actual);
11511 -- Ancestor is unconstrained, Check if generic formal and actual
11512 -- agree on constrainedness. The check only applies to array types
11513 -- and discriminated types.
11515 elsif Is_Constrained (Act_T) then
11516 if Ekind (Ancestor) = E_Access_Type
11518 (not Is_Constrained (A_Gen_T)
11519 and then Is_Composite_Type (A_Gen_T))
11522 ("actual subtype must be unconstrained", Actual);
11523 Abandon_Instantiation (Actual);
11526 -- A class-wide type is only allowed if the formal has unknown
11529 elsif Is_Class_Wide_Type (Act_T)
11530 and then not Has_Unknown_Discriminants (Ancestor)
11533 ("actual for & cannot be a class-wide type", Actual, Gen_T);
11534 Abandon_Instantiation (Actual);
11536 -- Otherwise, the formal and actual must have the same number
11537 -- of discriminants and each discriminant of the actual must
11538 -- correspond to a discriminant of the formal.
11540 elsif Has_Discriminants (Act_T)
11541 and then not Has_Unknown_Discriminants (Act_T)
11542 and then Has_Discriminants (Ancestor)
11544 Actual_Discr := First_Discriminant (Act_T);
11545 Ancestor_Discr := First_Discriminant (Ancestor);
11546 while Present (Actual_Discr)
11547 and then Present (Ancestor_Discr)
11549 if Base_Type (Act_T) /= Base_Type (Ancestor) and then
11550 No (Corresponding_Discriminant (Actual_Discr))
11553 ("discriminant & does not correspond " &
11554 "to ancestor discriminant", Actual, Actual_Discr);
11555 Abandon_Instantiation (Actual);
11558 Next_Discriminant (Actual_Discr);
11559 Next_Discriminant (Ancestor_Discr);
11562 if Present (Actual_Discr) or else Present (Ancestor_Discr) then
11564 ("actual for & must have same number of discriminants",
11566 Abandon_Instantiation (Actual);
11569 -- This case should be caught by the earlier check for
11570 -- constrainedness, but the check here is added for completeness.
11572 elsif Has_Discriminants (Act_T)
11573 and then not Has_Unknown_Discriminants (Act_T)
11576 ("actual for & must not have discriminants", Actual, Gen_T);
11577 Abandon_Instantiation (Actual);
11579 elsif Has_Discriminants (Ancestor) then
11581 ("actual for & must have known discriminants", Actual, Gen_T);
11582 Abandon_Instantiation (Actual);
11585 if not Subtypes_Statically_Compatible
11586 (Act_T, Ancestor, Formal_Derived_Matching => True)
11589 ("constraint on actual is incompatible with formal", Actual);
11590 Abandon_Instantiation (Actual);
11594 -- If the formal and actual types are abstract, check that there
11595 -- are no abstract primitives of the actual type that correspond to
11596 -- nonabstract primitives of the formal type (second sentence of
11599 if Is_Abstract_Type (A_Gen_T) and then Is_Abstract_Type (Act_T) then
11600 Check_Abstract_Primitives : declare
11601 Gen_Prims : constant Elist_Id :=
11602 Primitive_Operations (A_Gen_T);
11603 Gen_Elmt : Elmt_Id;
11604 Gen_Subp : Entity_Id;
11605 Anc_Subp : Entity_Id;
11606 Anc_Formal : Entity_Id;
11607 Anc_F_Type : Entity_Id;
11609 Act_Prims : constant Elist_Id := Primitive_Operations (Act_T);
11610 Act_Elmt : Elmt_Id;
11611 Act_Subp : Entity_Id;
11612 Act_Formal : Entity_Id;
11613 Act_F_Type : Entity_Id;
11615 Subprograms_Correspond : Boolean;
11617 function Is_Tagged_Ancestor (T1, T2 : Entity_Id) return Boolean;
11618 -- Returns true if T2 is derived directly or indirectly from
11619 -- T1, including derivations from interfaces. T1 and T2 are
11620 -- required to be specific tagged base types.
11622 ------------------------
11623 -- Is_Tagged_Ancestor --
11624 ------------------------
11626 function Is_Tagged_Ancestor (T1, T2 : Entity_Id) return Boolean
11628 Intfc_Elmt : Elmt_Id;
11631 -- The predicate is satisfied if the types are the same
11636 -- If we've reached the top of the derivation chain then
11637 -- we know that T1 is not an ancestor of T2.
11639 elsif Etype (T2) = T2 then
11642 -- Proceed to check T2's immediate parent
11644 elsif Is_Ancestor (T1, Base_Type (Etype (T2))) then
11647 -- Finally, check to see if T1 is an ancestor of any of T2's
11651 Intfc_Elmt := First_Elmt (Interfaces (T2));
11652 while Present (Intfc_Elmt) loop
11653 if Is_Ancestor (T1, Node (Intfc_Elmt)) then
11657 Next_Elmt (Intfc_Elmt);
11662 end Is_Tagged_Ancestor;
11664 -- Start of processing for Check_Abstract_Primitives
11667 -- Loop over all of the formal derived type's primitives
11669 Gen_Elmt := First_Elmt (Gen_Prims);
11670 while Present (Gen_Elmt) loop
11671 Gen_Subp := Node (Gen_Elmt);
11673 -- If the primitive of the formal is not abstract, then
11674 -- determine whether there is a corresponding primitive of
11675 -- the actual type that's abstract.
11677 if not Is_Abstract_Subprogram (Gen_Subp) then
11678 Act_Elmt := First_Elmt (Act_Prims);
11679 while Present (Act_Elmt) loop
11680 Act_Subp := Node (Act_Elmt);
11682 -- If we find an abstract primitive of the actual,
11683 -- then we need to test whether it corresponds to the
11684 -- subprogram from which the generic formal primitive
11687 if Is_Abstract_Subprogram (Act_Subp) then
11688 Anc_Subp := Alias (Gen_Subp);
11690 -- Test whether we have a corresponding primitive
11691 -- by comparing names, kinds, formal types, and
11694 if Chars (Anc_Subp) = Chars (Act_Subp)
11695 and then Ekind (Anc_Subp) = Ekind (Act_Subp)
11697 Anc_Formal := First_Formal (Anc_Subp);
11698 Act_Formal := First_Formal (Act_Subp);
11699 while Present (Anc_Formal)
11700 and then Present (Act_Formal)
11702 Anc_F_Type := Etype (Anc_Formal);
11703 Act_F_Type := Etype (Act_Formal);
11705 if Ekind (Anc_F_Type)
11706 = E_Anonymous_Access_Type
11708 Anc_F_Type := Designated_Type (Anc_F_Type);
11710 if Ekind (Act_F_Type)
11711 = E_Anonymous_Access_Type
11714 Designated_Type (Act_F_Type);
11720 Ekind (Act_F_Type) = E_Anonymous_Access_Type
11725 Anc_F_Type := Base_Type (Anc_F_Type);
11726 Act_F_Type := Base_Type (Act_F_Type);
11728 -- If the formal is controlling, then the
11729 -- the type of the actual primitive's formal
11730 -- must be derived directly or indirectly
11731 -- from the type of the ancestor primitive's
11734 if Is_Controlling_Formal (Anc_Formal) then
11735 if not Is_Tagged_Ancestor
11736 (Anc_F_Type, Act_F_Type)
11741 -- Otherwise the types of the formals must
11744 elsif Anc_F_Type /= Act_F_Type then
11748 Next_Entity (Anc_Formal);
11749 Next_Entity (Act_Formal);
11752 -- If we traversed through all of the formals
11753 -- then so far the subprograms correspond, so
11754 -- now check that any result types correspond.
11756 if No (Anc_Formal) and then No (Act_Formal) then
11757 Subprograms_Correspond := True;
11759 if Ekind (Act_Subp) = E_Function then
11760 Anc_F_Type := Etype (Anc_Subp);
11761 Act_F_Type := Etype (Act_Subp);
11763 if Ekind (Anc_F_Type)
11764 = E_Anonymous_Access_Type
11767 Designated_Type (Anc_F_Type);
11769 if Ekind (Act_F_Type)
11770 = E_Anonymous_Access_Type
11773 Designated_Type (Act_F_Type);
11775 Subprograms_Correspond := False;
11780 = E_Anonymous_Access_Type
11782 Subprograms_Correspond := False;
11785 Anc_F_Type := Base_Type (Anc_F_Type);
11786 Act_F_Type := Base_Type (Act_F_Type);
11788 -- Now either the result types must be
11789 -- the same or, if the result type is
11790 -- controlling, the result type of the
11791 -- actual primitive must descend from the
11792 -- result type of the ancestor primitive.
11794 if Subprograms_Correspond
11795 and then Anc_F_Type /= Act_F_Type
11797 Has_Controlling_Result (Anc_Subp)
11799 not Is_Tagged_Ancestor
11800 (Anc_F_Type, Act_F_Type)
11802 Subprograms_Correspond := False;
11806 -- Found a matching subprogram belonging to
11807 -- formal ancestor type, so actual subprogram
11808 -- corresponds and this violates 3.9.3(9).
11810 if Subprograms_Correspond then
11812 ("abstract subprogram & overrides " &
11813 "nonabstract subprogram of ancestor",
11821 Next_Elmt (Act_Elmt);
11825 Next_Elmt (Gen_Elmt);
11827 end Check_Abstract_Primitives;
11830 -- Verify that limitedness matches. If parent is a limited
11831 -- interface then the generic formal is not unless declared
11832 -- explicitly so. If not declared limited, the actual cannot be
11833 -- limited (see AI05-0087).
11835 -- Even though this AI is a binding interpretation, we enable the
11836 -- check only in Ada 2012 mode, because this improper construct
11837 -- shows up in user code and in existing B-tests.
11839 if Is_Limited_Type (Act_T)
11840 and then not Is_Limited_Type (A_Gen_T)
11841 and then Ada_Version >= Ada_2012
11843 if In_Instance then
11847 ("actual for non-limited & cannot be a limited type", Actual,
11849 Explain_Limited_Type (Act_T, Actual);
11850 Abandon_Instantiation (Actual);
11853 end Validate_Derived_Type_Instance;
11855 ----------------------------------------
11856 -- Validate_Discriminated_Formal_Type --
11857 ----------------------------------------
11859 procedure Validate_Discriminated_Formal_Type is
11860 Formal_Discr : Entity_Id;
11861 Actual_Discr : Entity_Id;
11862 Formal_Subt : Entity_Id;
11865 if Has_Discriminants (A_Gen_T) then
11866 if not Has_Discriminants (Act_T) then
11868 ("actual for & must have discriminants", Actual, Gen_T);
11869 Abandon_Instantiation (Actual);
11871 elsif Is_Constrained (Act_T) then
11873 ("actual for & must be unconstrained", Actual, Gen_T);
11874 Abandon_Instantiation (Actual);
11877 Formal_Discr := First_Discriminant (A_Gen_T);
11878 Actual_Discr := First_Discriminant (Act_T);
11879 while Formal_Discr /= Empty loop
11880 if Actual_Discr = Empty then
11882 ("discriminants on actual do not match formal",
11884 Abandon_Instantiation (Actual);
11887 Formal_Subt := Get_Instance_Of (Etype (Formal_Discr));
11889 -- Access discriminants match if designated types do
11891 if Ekind (Base_Type (Formal_Subt)) = E_Anonymous_Access_Type
11892 and then (Ekind (Base_Type (Etype (Actual_Discr)))) =
11893 E_Anonymous_Access_Type
11896 (Designated_Type (Base_Type (Formal_Subt))) =
11897 Designated_Type (Base_Type (Etype (Actual_Discr)))
11901 elsif Base_Type (Formal_Subt) /=
11902 Base_Type (Etype (Actual_Discr))
11905 ("types of actual discriminants must match formal",
11907 Abandon_Instantiation (Actual);
11909 elsif not Subtypes_Statically_Match
11910 (Formal_Subt, Etype (Actual_Discr))
11911 and then Ada_Version >= Ada_95
11914 ("subtypes of actual discriminants must match formal",
11916 Abandon_Instantiation (Actual);
11919 Next_Discriminant (Formal_Discr);
11920 Next_Discriminant (Actual_Discr);
11923 if Actual_Discr /= Empty then
11925 ("discriminants on actual do not match formal",
11927 Abandon_Instantiation (Actual);
11931 end Validate_Discriminated_Formal_Type;
11933 ---------------------------------------
11934 -- Validate_Incomplete_Type_Instance --
11935 ---------------------------------------
11937 procedure Validate_Incomplete_Type_Instance is
11939 if not Is_Tagged_Type (Act_T)
11940 and then Is_Tagged_Type (A_Gen_T)
11943 ("actual for & must be a tagged type", Actual, Gen_T);
11946 Validate_Discriminated_Formal_Type;
11947 end Validate_Incomplete_Type_Instance;
11949 --------------------------------------
11950 -- Validate_Interface_Type_Instance --
11951 --------------------------------------
11953 procedure Validate_Interface_Type_Instance is
11955 if not Is_Interface (Act_T) then
11957 ("actual for formal interface type must be an interface",
11960 elsif Is_Limited_Type (Act_T) /= Is_Limited_Type (A_Gen_T)
11962 Is_Task_Interface (A_Gen_T) /= Is_Task_Interface (Act_T)
11964 Is_Protected_Interface (A_Gen_T) /=
11965 Is_Protected_Interface (Act_T)
11967 Is_Synchronized_Interface (A_Gen_T) /=
11968 Is_Synchronized_Interface (Act_T)
11971 ("actual for interface& does not match (RM 12.5.5(4))",
11974 end Validate_Interface_Type_Instance;
11976 ------------------------------------
11977 -- Validate_Private_Type_Instance --
11978 ------------------------------------
11980 procedure Validate_Private_Type_Instance is
11982 if Is_Limited_Type (Act_T)
11983 and then not Is_Limited_Type (A_Gen_T)
11985 if In_Instance then
11989 ("actual for non-limited & cannot be a limited type", Actual,
11991 Explain_Limited_Type (Act_T, Actual);
11992 Abandon_Instantiation (Actual);
11995 elsif Known_To_Have_Preelab_Init (A_Gen_T)
11996 and then not Has_Preelaborable_Initialization (Act_T)
11999 ("actual for & must have preelaborable initialization", Actual,
12002 elsif Is_Indefinite_Subtype (Act_T)
12003 and then not Is_Indefinite_Subtype (A_Gen_T)
12004 and then Ada_Version >= Ada_95
12007 ("actual for & must be a definite subtype", Actual, Gen_T);
12009 elsif not Is_Tagged_Type (Act_T)
12010 and then Is_Tagged_Type (A_Gen_T)
12013 ("actual for & must be a tagged type", Actual, Gen_T);
12016 Validate_Discriminated_Formal_Type;
12018 end Validate_Private_Type_Instance;
12020 -- Start of processing for Instantiate_Type
12023 if Get_Instance_Of (A_Gen_T) /= A_Gen_T then
12024 Error_Msg_N ("duplicate instantiation of generic type", Actual);
12025 return New_List (Error);
12027 elsif not Is_Entity_Name (Actual)
12028 or else not Is_Type (Entity (Actual))
12031 ("expect valid subtype mark to instantiate &", Actual, Gen_T);
12032 Abandon_Instantiation (Actual);
12035 Act_T := Entity (Actual);
12037 -- Ada 2005 (AI-216): An Unchecked_Union subtype shall only be passed
12038 -- as a generic actual parameter if the corresponding formal type
12039 -- does not have a known_discriminant_part, or is a formal derived
12040 -- type that is an Unchecked_Union type.
12042 if Is_Unchecked_Union (Base_Type (Act_T)) then
12043 if not Has_Discriminants (A_Gen_T)
12045 (Is_Derived_Type (A_Gen_T)
12047 Is_Unchecked_Union (A_Gen_T))
12051 Error_Msg_N ("unchecked union cannot be the actual for a" &
12052 " discriminated formal type", Act_T);
12057 -- Deal with fixed/floating restrictions
12059 if Is_Floating_Point_Type (Act_T) then
12060 Check_Restriction (No_Floating_Point, Actual);
12061 elsif Is_Fixed_Point_Type (Act_T) then
12062 Check_Restriction (No_Fixed_Point, Actual);
12065 -- Deal with error of using incomplete type as generic actual.
12066 -- This includes limited views of a type, even if the non-limited
12067 -- view may be available.
12069 if Ekind (Act_T) = E_Incomplete_Type
12070 or else (Is_Class_Wide_Type (Act_T)
12072 Ekind (Root_Type (Act_T)) = E_Incomplete_Type)
12074 -- If the formal is an incomplete type, the actual can be
12075 -- incomplete as well.
12077 if Ekind (A_Gen_T) = E_Incomplete_Type then
12080 elsif Is_Class_Wide_Type (Act_T)
12081 or else No (Full_View (Act_T))
12083 Error_Msg_N ("premature use of incomplete type", Actual);
12084 Abandon_Instantiation (Actual);
12086 Act_T := Full_View (Act_T);
12087 Set_Entity (Actual, Act_T);
12089 if Has_Private_Component (Act_T) then
12091 ("premature use of type with private component", Actual);
12095 -- Deal with error of premature use of private type as generic actual
12097 elsif Is_Private_Type (Act_T)
12098 and then Is_Private_Type (Base_Type (Act_T))
12099 and then not Is_Generic_Type (Act_T)
12100 and then not Is_Derived_Type (Act_T)
12101 and then No (Full_View (Root_Type (Act_T)))
12103 -- If the formal is an incomplete type, the actual can be
12104 -- private or incomplete as well.
12106 if Ekind (A_Gen_T) = E_Incomplete_Type then
12109 Error_Msg_N ("premature use of private type", Actual);
12112 elsif Has_Private_Component (Act_T) then
12114 ("premature use of type with private component", Actual);
12117 Set_Instance_Of (A_Gen_T, Act_T);
12119 -- If the type is generic, the class-wide type may also be used
12121 if Is_Tagged_Type (A_Gen_T)
12122 and then Is_Tagged_Type (Act_T)
12123 and then not Is_Class_Wide_Type (A_Gen_T)
12125 Set_Instance_Of (Class_Wide_Type (A_Gen_T),
12126 Class_Wide_Type (Act_T));
12129 if not Is_Abstract_Type (A_Gen_T)
12130 and then Is_Abstract_Type (Act_T)
12133 ("actual of non-abstract formal cannot be abstract", Actual);
12136 -- A generic scalar type is a first subtype for which we generate
12137 -- an anonymous base type. Indicate that the instance of this base
12138 -- is the base type of the actual.
12140 if Is_Scalar_Type (A_Gen_T) then
12141 Set_Instance_Of (Etype (A_Gen_T), Etype (Act_T));
12145 if Error_Posted (Act_T) then
12148 case Nkind (Def) is
12149 when N_Formal_Private_Type_Definition =>
12150 Validate_Private_Type_Instance;
12152 when N_Formal_Incomplete_Type_Definition =>
12153 Validate_Incomplete_Type_Instance;
12155 when N_Formal_Derived_Type_Definition =>
12156 Validate_Derived_Type_Instance;
12158 when N_Formal_Discrete_Type_Definition =>
12159 if not Is_Discrete_Type (Act_T) then
12161 ("expect discrete type in instantiation of&",
12163 Abandon_Instantiation (Actual);
12166 Diagnose_Predicated_Actual;
12168 when N_Formal_Signed_Integer_Type_Definition =>
12169 if not Is_Signed_Integer_Type (Act_T) then
12171 ("expect signed integer type in instantiation of&",
12173 Abandon_Instantiation (Actual);
12176 Diagnose_Predicated_Actual;
12178 when N_Formal_Modular_Type_Definition =>
12179 if not Is_Modular_Integer_Type (Act_T) then
12181 ("expect modular type in instantiation of &",
12183 Abandon_Instantiation (Actual);
12186 Diagnose_Predicated_Actual;
12188 when N_Formal_Floating_Point_Definition =>
12189 if not Is_Floating_Point_Type (Act_T) then
12191 ("expect float type in instantiation of &", Actual, Gen_T);
12192 Abandon_Instantiation (Actual);
12195 when N_Formal_Ordinary_Fixed_Point_Definition =>
12196 if not Is_Ordinary_Fixed_Point_Type (Act_T) then
12198 ("expect ordinary fixed point type in instantiation of &",
12200 Abandon_Instantiation (Actual);
12203 when N_Formal_Decimal_Fixed_Point_Definition =>
12204 if not Is_Decimal_Fixed_Point_Type (Act_T) then
12206 ("expect decimal type in instantiation of &",
12208 Abandon_Instantiation (Actual);
12211 when N_Array_Type_Definition =>
12212 Validate_Array_Type_Instance;
12214 when N_Access_To_Object_Definition =>
12215 Validate_Access_Type_Instance;
12217 when N_Access_Function_Definition |
12218 N_Access_Procedure_Definition =>
12219 Validate_Access_Subprogram_Instance;
12221 when N_Record_Definition =>
12222 Validate_Interface_Type_Instance;
12224 when N_Derived_Type_Definition =>
12225 Validate_Derived_Interface_Type_Instance;
12228 raise Program_Error;
12233 Subt := New_Copy (Gen_T);
12235 -- Use adjusted sloc of subtype name as the location for other nodes in
12236 -- the subtype declaration.
12238 Loc := Sloc (Subt);
12241 Make_Subtype_Declaration (Loc,
12242 Defining_Identifier => Subt,
12243 Subtype_Indication => New_Occurrence_Of (Act_T, Loc));
12245 if Is_Private_Type (Act_T) then
12246 Set_Has_Private_View (Subtype_Indication (Decl_Node));
12248 elsif Is_Access_Type (Act_T)
12249 and then Is_Private_Type (Designated_Type (Act_T))
12251 Set_Has_Private_View (Subtype_Indication (Decl_Node));
12254 Decl_Nodes := New_List (Decl_Node);
12256 -- Flag actual derived types so their elaboration produces the
12257 -- appropriate renamings for the primitive operations of the ancestor.
12258 -- Flag actual for formal private types as well, to determine whether
12259 -- operations in the private part may override inherited operations.
12260 -- If the formal has an interface list, the ancestor is not the
12261 -- parent, but the analyzed formal that includes the interface
12262 -- operations of all its progenitors.
12264 -- Same treatment for formal private types, so we can check whether the
12265 -- type is tagged limited when validating derivations in the private
12266 -- part. (See AI05-096).
12268 if Nkind (Def) = N_Formal_Derived_Type_Definition then
12269 if Present (Interface_List (Def)) then
12270 Set_Generic_Parent_Type (Decl_Node, A_Gen_T);
12272 Set_Generic_Parent_Type (Decl_Node, Ancestor);
12275 elsif Nkind_In (Def,
12276 N_Formal_Private_Type_Definition,
12277 N_Formal_Incomplete_Type_Definition)
12279 Set_Generic_Parent_Type (Decl_Node, A_Gen_T);
12282 -- If the actual is a synchronized type that implements an interface,
12283 -- the primitive operations are attached to the corresponding record,
12284 -- and we have to treat it as an additional generic actual, so that its
12285 -- primitive operations become visible in the instance. The task or
12286 -- protected type itself does not carry primitive operations.
12288 if Is_Concurrent_Type (Act_T)
12289 and then Is_Tagged_Type (Act_T)
12290 and then Present (Corresponding_Record_Type (Act_T))
12291 and then Present (Ancestor)
12292 and then Is_Interface (Ancestor)
12295 Corr_Rec : constant Entity_Id :=
12296 Corresponding_Record_Type (Act_T);
12297 New_Corr : Entity_Id;
12298 Corr_Decl : Node_Id;
12301 New_Corr := Make_Temporary (Loc, 'S');
12303 Make_Subtype_Declaration (Loc,
12304 Defining_Identifier => New_Corr,
12305 Subtype_Indication =>
12306 New_Occurrence_Of (Corr_Rec, Loc));
12307 Append_To (Decl_Nodes, Corr_Decl);
12309 if Ekind (Act_T) = E_Task_Type then
12310 Set_Ekind (Subt, E_Task_Subtype);
12312 Set_Ekind (Subt, E_Protected_Subtype);
12315 Set_Corresponding_Record_Type (Subt, Corr_Rec);
12316 Set_Generic_Parent_Type (Corr_Decl, Ancestor);
12317 Set_Generic_Parent_Type (Decl_Node, Empty);
12322 end Instantiate_Type;
12324 ---------------------
12325 -- Is_In_Main_Unit --
12326 ---------------------
12328 function Is_In_Main_Unit (N : Node_Id) return Boolean is
12329 Unum : constant Unit_Number_Type := Get_Source_Unit (N);
12330 Current_Unit : Node_Id;
12333 if Unum = Main_Unit then
12336 -- If the current unit is a subunit then it is either the main unit or
12337 -- is being compiled as part of the main unit.
12339 elsif Nkind (N) = N_Compilation_Unit then
12340 return Nkind (Unit (N)) = N_Subunit;
12343 Current_Unit := Parent (N);
12344 while Present (Current_Unit)
12345 and then Nkind (Current_Unit) /= N_Compilation_Unit
12347 Current_Unit := Parent (Current_Unit);
12350 -- The instantiation node is in the main unit, or else the current node
12351 -- (perhaps as the result of nested instantiations) is in the main unit,
12352 -- or in the declaration of the main unit, which in this last case must
12355 return Unum = Main_Unit
12356 or else Current_Unit = Cunit (Main_Unit)
12357 or else Current_Unit = Library_Unit (Cunit (Main_Unit))
12358 or else (Present (Library_Unit (Current_Unit))
12359 and then Is_In_Main_Unit (Library_Unit (Current_Unit)));
12360 end Is_In_Main_Unit;
12362 ----------------------------
12363 -- Load_Parent_Of_Generic --
12364 ----------------------------
12366 procedure Load_Parent_Of_Generic
12369 Body_Optional : Boolean := False)
12371 Comp_Unit : constant Node_Id := Cunit (Get_Source_Unit (Spec));
12372 Saved_Style_Check : constant Boolean := Style_Check;
12373 Saved_Warnings : constant Warning_Record := Save_Warnings;
12374 True_Parent : Node_Id;
12375 Inst_Node : Node_Id;
12377 Previous_Instances : constant Elist_Id := New_Elmt_List;
12379 procedure Collect_Previous_Instances (Decls : List_Id);
12380 -- Collect all instantiations in the given list of declarations, that
12381 -- precede the generic that we need to load. If the bodies of these
12382 -- instantiations are available, we must analyze them, to ensure that
12383 -- the public symbols generated are the same when the unit is compiled
12384 -- to generate code, and when it is compiled in the context of a unit
12385 -- that needs a particular nested instance. This process is applied to
12386 -- both package and subprogram instances.
12388 --------------------------------
12389 -- Collect_Previous_Instances --
12390 --------------------------------
12392 procedure Collect_Previous_Instances (Decls : List_Id) is
12396 Decl := First (Decls);
12397 while Present (Decl) loop
12398 if Sloc (Decl) >= Sloc (Inst_Node) then
12401 -- If Decl is an instantiation, then record it as requiring
12402 -- instantiation of the corresponding body, except if it is an
12403 -- abbreviated instantiation generated internally for conformance
12404 -- checking purposes only for the case of a formal package
12405 -- declared without a box (see Instantiate_Formal_Package). Such
12406 -- an instantiation does not generate any code (the actual code
12407 -- comes from actual) and thus does not need to be analyzed here.
12408 -- If the instantiation appears with a generic package body it is
12409 -- not analyzed here either.
12411 elsif Nkind (Decl) = N_Package_Instantiation
12412 and then not Is_Internal (Defining_Entity (Decl))
12414 Append_Elmt (Decl, Previous_Instances);
12416 -- For a subprogram instantiation, omit instantiations intrinsic
12417 -- operations (Unchecked_Conversions, etc.) that have no bodies.
12419 elsif Nkind_In (Decl, N_Function_Instantiation,
12420 N_Procedure_Instantiation)
12421 and then not Is_Intrinsic_Subprogram (Entity (Name (Decl)))
12423 Append_Elmt (Decl, Previous_Instances);
12425 elsif Nkind (Decl) = N_Package_Declaration then
12426 Collect_Previous_Instances
12427 (Visible_Declarations (Specification (Decl)));
12428 Collect_Previous_Instances
12429 (Private_Declarations (Specification (Decl)));
12431 -- Previous non-generic bodies may contain instances as well
12433 elsif Nkind (Decl) = N_Package_Body
12434 and then Ekind (Corresponding_Spec (Decl)) /= E_Generic_Package
12436 Collect_Previous_Instances (Declarations (Decl));
12438 elsif Nkind (Decl) = N_Subprogram_Body
12439 and then not Acts_As_Spec (Decl)
12440 and then not Is_Generic_Subprogram (Corresponding_Spec (Decl))
12442 Collect_Previous_Instances (Declarations (Decl));
12447 end Collect_Previous_Instances;
12449 -- Start of processing for Load_Parent_Of_Generic
12452 if not In_Same_Source_Unit (N, Spec)
12453 or else Nkind (Unit (Comp_Unit)) = N_Package_Declaration
12454 or else (Nkind (Unit (Comp_Unit)) = N_Package_Body
12455 and then not Is_In_Main_Unit (Spec))
12457 -- Find body of parent of spec, and analyze it. A special case arises
12458 -- when the parent is an instantiation, that is to say when we are
12459 -- currently instantiating a nested generic. In that case, there is
12460 -- no separate file for the body of the enclosing instance. Instead,
12461 -- the enclosing body must be instantiated as if it were a pending
12462 -- instantiation, in order to produce the body for the nested generic
12463 -- we require now. Note that in that case the generic may be defined
12464 -- in a package body, the instance defined in the same package body,
12465 -- and the original enclosing body may not be in the main unit.
12467 Inst_Node := Empty;
12469 True_Parent := Parent (Spec);
12470 while Present (True_Parent)
12471 and then Nkind (True_Parent) /= N_Compilation_Unit
12473 if Nkind (True_Parent) = N_Package_Declaration
12475 Nkind (Original_Node (True_Parent)) = N_Package_Instantiation
12477 -- Parent is a compilation unit that is an instantiation.
12478 -- Instantiation node has been replaced with package decl.
12480 Inst_Node := Original_Node (True_Parent);
12483 elsif Nkind (True_Parent) = N_Package_Declaration
12484 and then Present (Generic_Parent (Specification (True_Parent)))
12485 and then Nkind (Parent (True_Parent)) /= N_Compilation_Unit
12487 -- Parent is an instantiation within another specification.
12488 -- Declaration for instance has been inserted before original
12489 -- instantiation node. A direct link would be preferable?
12491 Inst_Node := Next (True_Parent);
12492 while Present (Inst_Node)
12493 and then Nkind (Inst_Node) /= N_Package_Instantiation
12498 -- If the instance appears within a generic, and the generic
12499 -- unit is defined within a formal package of the enclosing
12500 -- generic, there is no generic body available, and none
12501 -- needed. A more precise test should be used ???
12503 if No (Inst_Node) then
12510 True_Parent := Parent (True_Parent);
12514 -- Case where we are currently instantiating a nested generic
12516 if Present (Inst_Node) then
12517 if Nkind (Parent (True_Parent)) = N_Compilation_Unit then
12519 -- Instantiation node and declaration of instantiated package
12520 -- were exchanged when only the declaration was needed.
12521 -- Restore instantiation node before proceeding with body.
12523 Set_Unit (Parent (True_Parent), Inst_Node);
12526 -- Now complete instantiation of enclosing body, if it appears in
12527 -- some other unit. If it appears in the current unit, the body
12528 -- will have been instantiated already.
12530 if No (Corresponding_Body (Instance_Spec (Inst_Node))) then
12532 -- We need to determine the expander mode to instantiate the
12533 -- enclosing body. Because the generic body we need may use
12534 -- global entities declared in the enclosing package (including
12535 -- aggregates) it is in general necessary to compile this body
12536 -- with expansion enabled, except if we are within a generic
12537 -- package, in which case the usual generic rule applies.
12540 Exp_Status : Boolean := True;
12544 -- Loop through scopes looking for generic package
12546 Scop := Scope (Defining_Entity (Instance_Spec (Inst_Node)));
12547 while Present (Scop)
12548 and then Scop /= Standard_Standard
12550 if Ekind (Scop) = E_Generic_Package then
12551 Exp_Status := False;
12555 Scop := Scope (Scop);
12558 -- Collect previous instantiations in the unit that contains
12559 -- the desired generic.
12561 if Nkind (Parent (True_Parent)) /= N_Compilation_Unit
12562 and then not Body_Optional
12566 Info : Pending_Body_Info;
12570 Par := Parent (Inst_Node);
12571 while Present (Par) loop
12572 exit when Nkind (Parent (Par)) = N_Compilation_Unit;
12573 Par := Parent (Par);
12576 pragma Assert (Present (Par));
12578 if Nkind (Par) = N_Package_Body then
12579 Collect_Previous_Instances (Declarations (Par));
12581 elsif Nkind (Par) = N_Package_Declaration then
12582 Collect_Previous_Instances
12583 (Visible_Declarations (Specification (Par)));
12584 Collect_Previous_Instances
12585 (Private_Declarations (Specification (Par)));
12588 -- Enclosing unit is a subprogram body. In this
12589 -- case all instance bodies are processed in order
12590 -- and there is no need to collect them separately.
12595 Decl := First_Elmt (Previous_Instances);
12596 while Present (Decl) loop
12598 (Inst_Node => Node (Decl),
12600 Instance_Spec (Node (Decl)),
12601 Expander_Status => Exp_Status,
12602 Current_Sem_Unit =>
12603 Get_Code_Unit (Sloc (Node (Decl))),
12604 Scope_Suppress => Scope_Suppress,
12605 Local_Suppress_Stack_Top =>
12606 Local_Suppress_Stack_Top,
12607 Version => Ada_Version,
12608 Version_Pragma => Ada_Version_Pragma,
12609 Warnings => Save_Warnings,
12610 SPARK_Mode => SPARK_Mode,
12611 SPARK_Mode_Pragma => SPARK_Mode_Pragma);
12613 -- Package instance
12616 Nkind (Node (Decl)) = N_Package_Instantiation
12618 Instantiate_Package_Body
12619 (Info, Body_Optional => True);
12621 -- Subprogram instance
12624 -- The instance_spec is the wrapper package,
12625 -- and the subprogram declaration is the last
12626 -- declaration in the wrapper.
12630 (Visible_Declarations
12631 (Specification (Info.Act_Decl)));
12633 Instantiate_Subprogram_Body
12634 (Info, Body_Optional => True);
12642 Instantiate_Package_Body
12644 ((Inst_Node => Inst_Node,
12645 Act_Decl => True_Parent,
12646 Expander_Status => Exp_Status,
12647 Current_Sem_Unit => Get_Code_Unit
12648 (Sloc (Inst_Node)),
12649 Scope_Suppress => Scope_Suppress,
12650 Local_Suppress_Stack_Top => Local_Suppress_Stack_Top,
12651 Version => Ada_Version,
12652 Version_Pragma => Ada_Version_Pragma,
12653 Warnings => Save_Warnings,
12654 SPARK_Mode => SPARK_Mode,
12655 SPARK_Mode_Pragma => SPARK_Mode_Pragma)),
12656 Body_Optional => Body_Optional);
12660 -- Case where we are not instantiating a nested generic
12663 Opt.Style_Check := False;
12664 Expander_Mode_Save_And_Set (True);
12665 Load_Needed_Body (Comp_Unit, OK);
12666 Opt.Style_Check := Saved_Style_Check;
12667 Restore_Warnings (Saved_Warnings);
12668 Expander_Mode_Restore;
12671 and then Unit_Requires_Body (Defining_Entity (Spec))
12672 and then not Body_Optional
12675 Bname : constant Unit_Name_Type :=
12676 Get_Body_Name (Get_Unit_Name (Unit (Comp_Unit)));
12679 -- In CodePeer mode, the missing body may make the analysis
12680 -- incomplete, but we do not treat it as fatal.
12682 if CodePeer_Mode then
12686 Error_Msg_Unit_1 := Bname;
12687 Error_Msg_N ("this instantiation requires$!", N);
12688 Error_Msg_File_1 :=
12689 Get_File_Name (Bname, Subunit => False);
12690 Error_Msg_N ("\but file{ was not found!", N);
12691 raise Unrecoverable_Error;
12698 -- If loading parent of the generic caused an instantiation circularity,
12699 -- we abandon compilation at this point, because otherwise in some cases
12700 -- we get into trouble with infinite recursions after this point.
12702 if Circularity_Detected then
12703 raise Unrecoverable_Error;
12705 end Load_Parent_Of_Generic;
12707 ---------------------------------
12708 -- Map_Formal_Package_Entities --
12709 ---------------------------------
12711 procedure Map_Formal_Package_Entities (Form : Entity_Id; Act : Entity_Id) is
12716 Set_Instance_Of (Form, Act);
12718 -- Traverse formal and actual package to map the corresponding entities.
12719 -- We skip over internal entities that may be generated during semantic
12720 -- analysis, and find the matching entities by name, given that they
12721 -- must appear in the same order.
12723 E1 := First_Entity (Form);
12724 E2 := First_Entity (Act);
12725 while Present (E1) and then E1 /= First_Private_Entity (Form) loop
12726 -- Could this test be a single condition??? Seems like it could, and
12727 -- isn't FPE (Form) a constant anyway???
12729 if not Is_Internal (E1)
12730 and then Present (Parent (E1))
12731 and then not Is_Class_Wide_Type (E1)
12732 and then not Is_Internal_Name (Chars (E1))
12734 while Present (E2) and then Chars (E2) /= Chars (E1) loop
12741 Set_Instance_Of (E1, E2);
12743 if Is_Type (E1) and then Is_Tagged_Type (E2) then
12744 Set_Instance_Of (Class_Wide_Type (E1), Class_Wide_Type (E2));
12747 if Is_Constrained (E1) then
12748 Set_Instance_Of (Base_Type (E1), Base_Type (E2));
12751 if Ekind (E1) = E_Package and then No (Renamed_Object (E1)) then
12752 Map_Formal_Package_Entities (E1, E2);
12759 end Map_Formal_Package_Entities;
12761 -----------------------
12762 -- Move_Freeze_Nodes --
12763 -----------------------
12765 procedure Move_Freeze_Nodes
12766 (Out_Of : Entity_Id;
12771 Next_Decl : Node_Id;
12772 Next_Node : Node_Id := After;
12775 function Is_Outer_Type (T : Entity_Id) return Boolean;
12776 -- Check whether entity is declared in a scope external to that of the
12779 -------------------
12780 -- Is_Outer_Type --
12781 -------------------
12783 function Is_Outer_Type (T : Entity_Id) return Boolean is
12784 Scop : Entity_Id := Scope (T);
12787 if Scope_Depth (Scop) < Scope_Depth (Out_Of) then
12791 while Scop /= Standard_Standard loop
12792 if Scop = Out_Of then
12795 Scop := Scope (Scop);
12803 -- Start of processing for Move_Freeze_Nodes
12810 -- First remove the freeze nodes that may appear before all other
12814 while Present (Decl)
12815 and then Nkind (Decl) = N_Freeze_Entity
12816 and then Is_Outer_Type (Entity (Decl))
12818 Decl := Remove_Head (L);
12819 Insert_After (Next_Node, Decl);
12820 Set_Analyzed (Decl, False);
12825 -- Next scan the list of declarations and remove each freeze node that
12826 -- appears ahead of the current node.
12828 while Present (Decl) loop
12829 while Present (Next (Decl))
12830 and then Nkind (Next (Decl)) = N_Freeze_Entity
12831 and then Is_Outer_Type (Entity (Next (Decl)))
12833 Next_Decl := Remove_Next (Decl);
12834 Insert_After (Next_Node, Next_Decl);
12835 Set_Analyzed (Next_Decl, False);
12836 Next_Node := Next_Decl;
12839 -- If the declaration is a nested package or concurrent type, then
12840 -- recurse. Nested generic packages will have been processed from the
12843 case Nkind (Decl) is
12844 when N_Package_Declaration =>
12845 Spec := Specification (Decl);
12847 when N_Task_Type_Declaration =>
12848 Spec := Task_Definition (Decl);
12850 when N_Protected_Type_Declaration =>
12851 Spec := Protected_Definition (Decl);
12857 if Present (Spec) then
12858 Move_Freeze_Nodes (Out_Of, Next_Node, Visible_Declarations (Spec));
12859 Move_Freeze_Nodes (Out_Of, Next_Node, Private_Declarations (Spec));
12864 end Move_Freeze_Nodes;
12870 function Next_Assoc (E : Assoc_Ptr) return Assoc_Ptr is
12872 return Generic_Renamings.Table (E).Next_In_HTable;
12875 ------------------------
12876 -- Preanalyze_Actuals --
12877 ------------------------
12879 procedure Preanalyze_Actuals (N : Node_Id) is
12882 Errs : constant Int := Serious_Errors_Detected;
12884 Cur : Entity_Id := Empty;
12885 -- Current homograph of the instance name
12888 -- Saved visibility status of the current homograph
12891 Assoc := First (Generic_Associations (N));
12893 -- If the instance is a child unit, its name may hide an outer homonym,
12894 -- so make it invisible to perform name resolution on the actuals.
12896 if Nkind (Defining_Unit_Name (N)) = N_Defining_Program_Unit_Name
12898 (Current_Entity (Defining_Identifier (Defining_Unit_Name (N))))
12900 Cur := Current_Entity (Defining_Identifier (Defining_Unit_Name (N)));
12902 if Is_Compilation_Unit (Cur) then
12903 Vis := Is_Immediately_Visible (Cur);
12904 Set_Is_Immediately_Visible (Cur, False);
12910 while Present (Assoc) loop
12911 if Nkind (Assoc) /= N_Others_Choice then
12912 Act := Explicit_Generic_Actual_Parameter (Assoc);
12914 -- Within a nested instantiation, a defaulted actual is an empty
12915 -- association, so nothing to analyze. If the subprogram actual
12916 -- is an attribute, analyze prefix only, because actual is not a
12917 -- complete attribute reference.
12919 -- If actual is an allocator, analyze expression only. The full
12920 -- analysis can generate code, and if instance is a compilation
12921 -- unit we have to wait until the package instance is installed
12922 -- to have a proper place to insert this code.
12924 -- String literals may be operators, but at this point we do not
12925 -- know whether the actual is a formal subprogram or a string.
12930 elsif Nkind (Act) = N_Attribute_Reference then
12931 Analyze (Prefix (Act));
12933 elsif Nkind (Act) = N_Explicit_Dereference then
12934 Analyze (Prefix (Act));
12936 elsif Nkind (Act) = N_Allocator then
12938 Expr : constant Node_Id := Expression (Act);
12941 if Nkind (Expr) = N_Subtype_Indication then
12942 Analyze (Subtype_Mark (Expr));
12944 -- Analyze separately each discriminant constraint, when
12945 -- given with a named association.
12951 Constr := First (Constraints (Constraint (Expr)));
12952 while Present (Constr) loop
12953 if Nkind (Constr) = N_Discriminant_Association then
12954 Analyze (Expression (Constr));
12968 elsif Nkind (Act) /= N_Operator_Symbol then
12972 -- Ensure that a ghost subprogram does not act as generic actual
12974 if Is_Entity_Name (Act)
12975 and then Is_Ghost_Subprogram (Entity (Act))
12978 ("ghost subprogram & cannot act as generic actual", Act);
12979 Abandon_Instantiation (Act);
12981 elsif Errs /= Serious_Errors_Detected then
12983 -- Do a minimal analysis of the generic, to prevent spurious
12984 -- warnings complaining about the generic being unreferenced,
12985 -- before abandoning the instantiation.
12987 Analyze (Name (N));
12989 if Is_Entity_Name (Name (N))
12990 and then Etype (Name (N)) /= Any_Type
12992 Generate_Reference (Entity (Name (N)), Name (N));
12993 Set_Is_Instantiated (Entity (Name (N)));
12996 if Present (Cur) then
12998 -- For the case of a child instance hiding an outer homonym,
12999 -- provide additional warning which might explain the error.
13001 Set_Is_Immediately_Visible (Cur, Vis);
13002 Error_Msg_NE ("& hides outer unit with the same name??",
13003 N, Defining_Unit_Name (N));
13006 Abandon_Instantiation (Act);
13013 if Present (Cur) then
13014 Set_Is_Immediately_Visible (Cur, Vis);
13016 end Preanalyze_Actuals;
13018 -------------------
13019 -- Remove_Parent --
13020 -------------------
13022 procedure Remove_Parent (In_Body : Boolean := False) is
13023 S : Entity_Id := Current_Scope;
13024 -- S is the scope containing the instantiation just completed. The scope
13025 -- stack contains the parent instances of the instantiation, followed by
13034 -- After child instantiation is complete, remove from scope stack the
13035 -- extra copy of the current scope, and then remove parent instances.
13037 if not In_Body then
13040 while Current_Scope /= S loop
13041 P := Current_Scope;
13042 End_Package_Scope (Current_Scope);
13044 if In_Open_Scopes (P) then
13045 E := First_Entity (P);
13046 while Present (E) loop
13047 Set_Is_Immediately_Visible (E, True);
13051 -- If instantiation is declared in a block, it is the enclosing
13052 -- scope that might be a parent instance. Note that only one
13053 -- block can be involved, because the parent instances have
13054 -- been installed within it.
13056 if Ekind (P) = E_Block then
13057 Cur_P := Scope (P);
13062 if Is_Generic_Instance (Cur_P) and then P /= Current_Scope then
13063 -- We are within an instance of some sibling. Retain
13064 -- visibility of parent, for proper subsequent cleanup, and
13065 -- reinstall private declarations as well.
13067 Set_In_Private_Part (P);
13068 Install_Private_Declarations (P);
13071 -- If the ultimate parent is a top-level unit recorded in
13072 -- Instance_Parent_Unit, then reset its visibility to what it was
13073 -- before instantiation. (It's not clear what the purpose is of
13074 -- testing whether Scope (P) is In_Open_Scopes, but that test was
13075 -- present before the ultimate parent test was added.???)
13077 elsif not In_Open_Scopes (Scope (P))
13078 or else (P = Instance_Parent_Unit
13079 and then not Parent_Unit_Visible)
13081 Set_Is_Immediately_Visible (P, False);
13083 -- If the current scope is itself an instantiation of a generic
13084 -- nested within P, and we are in the private part of body of this
13085 -- instantiation, restore the full views of P, that were removed
13086 -- in End_Package_Scope above. This obscure case can occur when a
13087 -- subunit of a generic contains an instance of a child unit of
13088 -- its generic parent unit.
13090 elsif S = Current_Scope and then Is_Generic_Instance (S) then
13092 Par : constant Entity_Id :=
13093 Generic_Parent (Package_Specification (S));
13096 and then P = Scope (Par)
13097 and then (In_Package_Body (S) or else In_Private_Part (S))
13099 Set_In_Private_Part (P);
13100 Install_Private_Declarations (P);
13106 -- Reset visibility of entities in the enclosing scope
13108 Set_Is_Hidden_Open_Scope (Current_Scope, False);
13110 Hidden := First_Elmt (Hidden_Entities);
13111 while Present (Hidden) loop
13112 Set_Is_Immediately_Visible (Node (Hidden), True);
13113 Next_Elmt (Hidden);
13117 -- Each body is analyzed separately, and there is no context that
13118 -- needs preserving from one body instance to the next, so remove all
13119 -- parent scopes that have been installed.
13121 while Present (S) loop
13122 End_Package_Scope (S);
13123 Set_Is_Immediately_Visible (S, False);
13124 S := Current_Scope;
13125 exit when S = Standard_Standard;
13134 procedure Restore_Env is
13135 Saved : Instance_Env renames Instance_Envs.Table (Instance_Envs.Last);
13138 if No (Current_Instantiated_Parent.Act_Id) then
13139 -- Restore environment after subprogram inlining
13141 Restore_Private_Views (Empty);
13144 Current_Instantiated_Parent := Saved.Instantiated_Parent;
13145 Exchanged_Views := Saved.Exchanged_Views;
13146 Hidden_Entities := Saved.Hidden_Entities;
13147 Current_Sem_Unit := Saved.Current_Sem_Unit;
13148 Parent_Unit_Visible := Saved.Parent_Unit_Visible;
13149 Instance_Parent_Unit := Saved.Instance_Parent_Unit;
13151 Restore_Opt_Config_Switches (Saved.Switches);
13153 Instance_Envs.Decrement_Last;
13156 ---------------------------
13157 -- Restore_Private_Views --
13158 ---------------------------
13160 procedure Restore_Private_Views
13161 (Pack_Id : Entity_Id;
13162 Is_Package : Boolean := True)
13167 Dep_Elmt : Elmt_Id;
13170 procedure Restore_Nested_Formal (Formal : Entity_Id);
13171 -- Hide the generic formals of formal packages declared with box which
13172 -- were reachable in the current instantiation.
13174 ---------------------------
13175 -- Restore_Nested_Formal --
13176 ---------------------------
13178 procedure Restore_Nested_Formal (Formal : Entity_Id) is
13182 if Present (Renamed_Object (Formal))
13183 and then Denotes_Formal_Package (Renamed_Object (Formal), True)
13187 elsif Present (Associated_Formal_Package (Formal)) then
13188 Ent := First_Entity (Formal);
13189 while Present (Ent) loop
13190 exit when Ekind (Ent) = E_Package
13191 and then Renamed_Entity (Ent) = Renamed_Entity (Formal);
13193 Set_Is_Hidden (Ent);
13194 Set_Is_Potentially_Use_Visible (Ent, False);
13196 -- If package, then recurse
13198 if Ekind (Ent) = E_Package then
13199 Restore_Nested_Formal (Ent);
13205 end Restore_Nested_Formal;
13207 -- Start of processing for Restore_Private_Views
13210 M := First_Elmt (Exchanged_Views);
13211 while Present (M) loop
13214 -- Subtypes of types whose views have been exchanged, and that are
13215 -- defined within the instance, were not on the Private_Dependents
13216 -- list on entry to the instance, so they have to be exchanged
13217 -- explicitly now, in order to remain consistent with the view of the
13220 if Ekind_In (Typ, E_Private_Type,
13221 E_Limited_Private_Type,
13222 E_Record_Type_With_Private)
13224 Dep_Elmt := First_Elmt (Private_Dependents (Typ));
13225 while Present (Dep_Elmt) loop
13226 Dep_Typ := Node (Dep_Elmt);
13228 if Scope (Dep_Typ) = Pack_Id
13229 and then Present (Full_View (Dep_Typ))
13231 Replace_Elmt (Dep_Elmt, Full_View (Dep_Typ));
13232 Exchange_Declarations (Dep_Typ);
13235 Next_Elmt (Dep_Elmt);
13239 Exchange_Declarations (Node (M));
13243 if No (Pack_Id) then
13247 -- Make the generic formal parameters private, and make the formal types
13248 -- into subtypes of the actuals again.
13250 E := First_Entity (Pack_Id);
13251 while Present (E) loop
13252 Set_Is_Hidden (E, True);
13255 and then Nkind (Parent (E)) = N_Subtype_Declaration
13257 -- If the actual for E is itself a generic actual type from
13258 -- an enclosing instance, E is still a generic actual type
13259 -- outside of the current instance. This matter when resolving
13260 -- an overloaded call that may be ambiguous in the enclosing
13261 -- instance, when two of its actuals coincide.
13263 if Is_Entity_Name (Subtype_Indication (Parent (E)))
13264 and then Is_Generic_Actual_Type
13265 (Entity (Subtype_Indication (Parent (E))))
13269 Set_Is_Generic_Actual_Type (E, False);
13272 -- An unusual case of aliasing: the actual may also be directly
13273 -- visible in the generic, and be private there, while it is fully
13274 -- visible in the context of the instance. The internal subtype
13275 -- is private in the instance but has full visibility like its
13276 -- parent in the enclosing scope. This enforces the invariant that
13277 -- the privacy status of all private dependents of a type coincide
13278 -- with that of the parent type. This can only happen when a
13279 -- generic child unit is instantiated within a sibling.
13281 if Is_Private_Type (E)
13282 and then not Is_Private_Type (Etype (E))
13284 Exchange_Declarations (E);
13287 elsif Ekind (E) = E_Package then
13289 -- The end of the renaming list is the renaming of the generic
13290 -- package itself. If the instance is a subprogram, all entities
13291 -- in the corresponding package are renamings. If this entity is
13292 -- a formal package, make its own formals private as well. The
13293 -- actual in this case is itself the renaming of an instantiation.
13294 -- If the entity is not a package renaming, it is the entity
13295 -- created to validate formal package actuals: ignore it.
13297 -- If the actual is itself a formal package for the enclosing
13298 -- generic, or the actual for such a formal package, it remains
13299 -- visible on exit from the instance, and therefore nothing needs
13300 -- to be done either, except to keep it accessible.
13302 if Is_Package and then Renamed_Object (E) = Pack_Id then
13305 elsif Nkind (Parent (E)) /= N_Package_Renaming_Declaration then
13309 Denotes_Formal_Package (Renamed_Object (E), True, Pack_Id)
13311 Set_Is_Hidden (E, False);
13315 Act_P : constant Entity_Id := Renamed_Object (E);
13319 Id := First_Entity (Act_P);
13321 and then Id /= First_Private_Entity (Act_P)
13323 exit when Ekind (Id) = E_Package
13324 and then Renamed_Object (Id) = Act_P;
13326 Set_Is_Hidden (Id, True);
13327 Set_Is_Potentially_Use_Visible (Id, In_Use (Act_P));
13329 if Ekind (Id) = E_Package then
13330 Restore_Nested_Formal (Id);
13341 end Restore_Private_Views;
13348 (Gen_Unit : Entity_Id;
13349 Act_Unit : Entity_Id)
13353 Set_Instance_Env (Gen_Unit, Act_Unit);
13356 ----------------------------
13357 -- Save_Global_References --
13358 ----------------------------
13360 procedure Save_Global_References (N : Node_Id) is
13361 Gen_Scope : Entity_Id;
13365 function Is_Global (E : Entity_Id) return Boolean;
13366 -- Check whether entity is defined outside of generic unit. Examine the
13367 -- scope of an entity, and the scope of the scope, etc, until we find
13368 -- either Standard, in which case the entity is global, or the generic
13369 -- unit itself, which indicates that the entity is local. If the entity
13370 -- is the generic unit itself, as in the case of a recursive call, or
13371 -- the enclosing generic unit, if different from the current scope, then
13372 -- it is local as well, because it will be replaced at the point of
13373 -- instantiation. On the other hand, if it is a reference to a child
13374 -- unit of a common ancestor, which appears in an instantiation, it is
13375 -- global because it is used to denote a specific compilation unit at
13376 -- the time the instantiations will be analyzed.
13378 procedure Reset_Entity (N : Node_Id);
13379 -- Save semantic information on global entity so that it is not resolved
13380 -- again at instantiation time.
13382 procedure Save_Entity_Descendants (N : Node_Id);
13383 -- Apply Save_Global_References to the two syntactic descendants of
13384 -- non-terminal nodes that carry an Associated_Node and are processed
13385 -- through Reset_Entity. Once the global entity (if any) has been
13386 -- captured together with its type, only two syntactic descendants need
13387 -- to be traversed to complete the processing of the tree rooted at N.
13388 -- This applies to Selected_Components, Expanded_Names, and to Operator
13389 -- nodes. N can also be a character literal, identifier, or operator
13390 -- symbol node, but the call has no effect in these cases.
13392 procedure Save_Global_Defaults (N1, N2 : Node_Id);
13393 -- Default actuals in nested instances must be handled specially
13394 -- because there is no link to them from the original tree. When an
13395 -- actual subprogram is given by a default, we add an explicit generic
13396 -- association for it in the instantiation node. When we save the
13397 -- global references on the name of the instance, we recover the list
13398 -- of generic associations, and add an explicit one to the original
13399 -- generic tree, through which a global actual can be preserved.
13400 -- Similarly, if a child unit is instantiated within a sibling, in the
13401 -- context of the parent, we must preserve the identifier of the parent
13402 -- so that it can be properly resolved in a subsequent instantiation.
13404 procedure Save_Global_Descendant (D : Union_Id);
13405 -- Apply Save_Global_References recursively to the descendents of the
13408 procedure Save_References (N : Node_Id);
13409 -- This is the recursive procedure that does the work, once the
13410 -- enclosing generic scope has been established.
13416 function Is_Global (E : Entity_Id) return Boolean is
13419 function Is_Instance_Node (Decl : Node_Id) return Boolean;
13420 -- Determine whether the parent node of a reference to a child unit
13421 -- denotes an instantiation or a formal package, in which case the
13422 -- reference to the child unit is global, even if it appears within
13423 -- the current scope (e.g. when the instance appears within the body
13424 -- of an ancestor).
13426 ----------------------
13427 -- Is_Instance_Node --
13428 ----------------------
13430 function Is_Instance_Node (Decl : Node_Id) return Boolean is
13432 return Nkind (Decl) in N_Generic_Instantiation
13434 Nkind (Original_Node (Decl)) = N_Formal_Package_Declaration;
13435 end Is_Instance_Node;
13437 -- Start of processing for Is_Global
13440 if E = Gen_Scope then
13443 elsif E = Standard_Standard then
13446 elsif Is_Child_Unit (E)
13447 and then (Is_Instance_Node (Parent (N2))
13448 or else (Nkind (Parent (N2)) = N_Expanded_Name
13449 and then N2 = Selector_Name (Parent (N2))
13451 Is_Instance_Node (Parent (Parent (N2)))))
13457 while Se /= Gen_Scope loop
13458 if Se = Standard_Standard then
13473 procedure Reset_Entity (N : Node_Id) is
13475 procedure Set_Global_Type (N : Node_Id; N2 : Node_Id);
13476 -- If the type of N2 is global to the generic unit, save the type in
13477 -- the generic node. Just as we perform name capture for explicit
13478 -- references within the generic, we must capture the global types
13479 -- of local entities because they may participate in resolution in
13482 function Top_Ancestor (E : Entity_Id) return Entity_Id;
13483 -- Find the ultimate ancestor of the current unit. If it is not a
13484 -- generic unit, then the name of the current unit in the prefix of
13485 -- an expanded name must be replaced with its generic homonym to
13486 -- ensure that it will be properly resolved in an instance.
13488 ---------------------
13489 -- Set_Global_Type --
13490 ---------------------
13492 procedure Set_Global_Type (N : Node_Id; N2 : Node_Id) is
13493 Typ : constant Entity_Id := Etype (N2);
13496 Set_Etype (N, Typ);
13498 if Entity (N) /= N2
13499 and then Has_Private_View (Entity (N))
13501 -- If the entity of N is not the associated node, this is a
13502 -- nested generic and it has an associated node as well, whose
13503 -- type is already the full view (see below). Indicate that the
13504 -- original node has a private view.
13506 Set_Has_Private_View (N);
13509 -- If not a private type, nothing else to do
13511 if not Is_Private_Type (Typ) then
13512 if Is_Array_Type (Typ)
13513 and then Is_Private_Type (Component_Type (Typ))
13515 Set_Has_Private_View (N);
13518 -- If it is a derivation of a private type in a context where no
13519 -- full view is needed, nothing to do either.
13521 elsif No (Full_View (Typ)) and then Typ /= Etype (Typ) then
13524 -- Otherwise mark the type for flipping and use the full view when
13528 Set_Has_Private_View (N);
13530 if Present (Full_View (Typ)) then
13531 Set_Etype (N2, Full_View (Typ));
13534 end Set_Global_Type;
13540 function Top_Ancestor (E : Entity_Id) return Entity_Id is
13545 while Is_Child_Unit (Par) loop
13546 Par := Scope (Par);
13552 -- Start of processing for Reset_Entity
13555 N2 := Get_Associated_Node (N);
13558 if Present (E) then
13560 -- If the node is an entry call to an entry in an enclosing task,
13561 -- it is rewritten as a selected component. No global entity to
13562 -- preserve in this case, since the expansion will be redone in
13565 if not Nkind_In (E, N_Defining_Identifier,
13566 N_Defining_Character_Literal,
13567 N_Defining_Operator_Symbol)
13569 Set_Associated_Node (N, Empty);
13570 Set_Etype (N, Empty);
13574 -- If the entity is an itype created as a subtype of an access
13575 -- type with a null exclusion restore source entity for proper
13576 -- visibility. The itype will be created anew in the instance.
13579 and then Ekind (E) = E_Access_Subtype
13580 and then Is_Entity_Name (N)
13581 and then Chars (Etype (E)) = Chars (N)
13584 Set_Entity (N2, E);
13588 if Is_Global (E) then
13590 -- If the entity is a package renaming that is the prefix of
13591 -- an expanded name, it has been rewritten as the renamed
13592 -- package, which is necessary semantically but complicates
13593 -- ASIS tree traversal, so we recover the original entity to
13594 -- expose the renaming. Take into account that the context may
13595 -- be a nested generic, that the original node may itself have
13596 -- an associated node that had better be an entity, and that
13597 -- the current node is still a selected component.
13599 if Ekind (E) = E_Package
13600 and then Nkind (N) = N_Selected_Component
13601 and then Nkind (Parent (N)) = N_Expanded_Name
13602 and then Present (Original_Node (N2))
13603 and then Is_Entity_Name (Original_Node (N2))
13604 and then Present (Entity (Original_Node (N2)))
13606 if Is_Global (Entity (Original_Node (N2))) then
13607 N2 := Original_Node (N2);
13608 Set_Associated_Node (N, N2);
13609 Set_Global_Type (N, N2);
13612 -- Renaming is local, and will be resolved in instance
13614 Set_Associated_Node (N, Empty);
13615 Set_Etype (N, Empty);
13619 Set_Global_Type (N, N2);
13622 elsif Nkind (N) = N_Op_Concat
13623 and then Is_Generic_Type (Etype (N2))
13624 and then (Base_Type (Etype (Right_Opnd (N2))) = Etype (N2)
13626 Base_Type (Etype (Left_Opnd (N2))) = Etype (N2))
13627 and then Is_Intrinsic_Subprogram (E)
13632 -- Entity is local. Mark generic node as unresolved.
13633 -- Note that now it does not have an entity.
13635 Set_Associated_Node (N, Empty);
13636 Set_Etype (N, Empty);
13639 if Nkind (Parent (N)) in N_Generic_Instantiation
13640 and then N = Name (Parent (N))
13642 Save_Global_Defaults (Parent (N), Parent (N2));
13645 elsif Nkind (Parent (N)) = N_Selected_Component
13646 and then Nkind (Parent (N2)) = N_Expanded_Name
13648 if Is_Global (Entity (Parent (N2))) then
13649 Change_Selected_Component_To_Expanded_Name (Parent (N));
13650 Set_Associated_Node (Parent (N), Parent (N2));
13651 Set_Global_Type (Parent (N), Parent (N2));
13652 Save_Entity_Descendants (N);
13654 -- If this is a reference to the current generic entity, replace
13655 -- by the name of the generic homonym of the current package. This
13656 -- is because in an instantiation Par.P.Q will not resolve to the
13657 -- name of the instance, whose enclosing scope is not necessarily
13658 -- Par. We use the generic homonym rather that the name of the
13659 -- generic itself because it may be hidden by a local declaration.
13661 elsif In_Open_Scopes (Entity (Parent (N2)))
13663 Is_Generic_Unit (Top_Ancestor (Entity (Prefix (Parent (N2)))))
13665 if Ekind (Entity (Parent (N2))) = E_Generic_Package then
13666 Rewrite (Parent (N),
13667 Make_Identifier (Sloc (N),
13669 Chars (Generic_Homonym (Entity (Parent (N2))))));
13671 Rewrite (Parent (N),
13672 Make_Identifier (Sloc (N),
13673 Chars => Chars (Selector_Name (Parent (N2)))));
13677 if Nkind (Parent (Parent (N))) in N_Generic_Instantiation
13678 and then Parent (N) = Name (Parent (Parent (N)))
13680 Save_Global_Defaults
13681 (Parent (Parent (N)), Parent (Parent ((N2))));
13684 -- A selected component may denote a static constant that has been
13685 -- folded. If the static constant is global to the generic, capture
13686 -- its value. Otherwise the folding will happen in any instantiation.
13688 elsif Nkind (Parent (N)) = N_Selected_Component
13689 and then Nkind_In (Parent (N2), N_Integer_Literal, N_Real_Literal)
13691 if Present (Entity (Original_Node (Parent (N2))))
13692 and then Is_Global (Entity (Original_Node (Parent (N2))))
13694 Rewrite (Parent (N), New_Copy (Parent (N2)));
13695 Set_Analyzed (Parent (N), False);
13701 -- A selected component may be transformed into a parameterless
13702 -- function call. If the called entity is global, rewrite the node
13703 -- appropriately, i.e. as an extended name for the global entity.
13705 elsif Nkind (Parent (N)) = N_Selected_Component
13706 and then Nkind (Parent (N2)) = N_Function_Call
13707 and then N = Selector_Name (Parent (N))
13709 if No (Parameter_Associations (Parent (N2))) then
13710 if Is_Global (Entity (Name (Parent (N2)))) then
13711 Change_Selected_Component_To_Expanded_Name (Parent (N));
13712 Set_Associated_Node (Parent (N), Name (Parent (N2)));
13713 Set_Global_Type (Parent (N), Name (Parent (N2)));
13714 Save_Entity_Descendants (N);
13717 Set_Is_Prefixed_Call (Parent (N));
13718 Set_Associated_Node (N, Empty);
13719 Set_Etype (N, Empty);
13722 -- In Ada 2005, X.F may be a call to a primitive operation,
13723 -- rewritten as F (X). This rewriting will be done again in an
13724 -- instance, so keep the original node. Global entities will be
13725 -- captured as for other constructs. Indicate that this must
13726 -- resolve as a call, to prevent accidental overloading in the
13727 -- instance, if both a component and a primitive operation appear
13731 Set_Is_Prefixed_Call (Parent (N));
13734 -- Entity is local. Reset in generic unit, so that node is resolved
13735 -- anew at the point of instantiation.
13738 Set_Associated_Node (N, Empty);
13739 Set_Etype (N, Empty);
13743 -----------------------------
13744 -- Save_Entity_Descendants --
13745 -----------------------------
13747 procedure Save_Entity_Descendants (N : Node_Id) is
13750 when N_Binary_Op =>
13751 Save_Global_Descendant (Union_Id (Left_Opnd (N)));
13752 Save_Global_Descendant (Union_Id (Right_Opnd (N)));
13755 Save_Global_Descendant (Union_Id (Right_Opnd (N)));
13757 when N_Expanded_Name | N_Selected_Component =>
13758 Save_Global_Descendant (Union_Id (Prefix (N)));
13759 Save_Global_Descendant (Union_Id (Selector_Name (N)));
13761 when N_Identifier | N_Character_Literal | N_Operator_Symbol =>
13765 raise Program_Error;
13767 end Save_Entity_Descendants;
13769 --------------------------
13770 -- Save_Global_Defaults --
13771 --------------------------
13773 procedure Save_Global_Defaults (N1, N2 : Node_Id) is
13774 Loc : constant Source_Ptr := Sloc (N1);
13775 Assoc2 : constant List_Id := Generic_Associations (N2);
13776 Gen_Id : constant Entity_Id := Get_Generic_Entity (N2);
13783 Actual : Entity_Id;
13786 Assoc1 := Generic_Associations (N1);
13788 if Present (Assoc1) then
13789 Act1 := First (Assoc1);
13792 Set_Generic_Associations (N1, New_List);
13793 Assoc1 := Generic_Associations (N1);
13796 if Present (Assoc2) then
13797 Act2 := First (Assoc2);
13802 while Present (Act1) and then Present (Act2) loop
13807 -- Find the associations added for default subprograms
13809 if Present (Act2) then
13810 while Nkind (Act2) /= N_Generic_Association
13811 or else No (Entity (Selector_Name (Act2)))
13812 or else not Is_Overloadable (Entity (Selector_Name (Act2)))
13817 -- Add a similar association if the default is global. The
13818 -- renaming declaration for the actual has been analyzed, and
13819 -- its alias is the program it renames. Link the actual in the
13820 -- original generic tree with the node in the analyzed tree.
13822 while Present (Act2) loop
13823 Subp := Entity (Selector_Name (Act2));
13824 Def := Explicit_Generic_Actual_Parameter (Act2);
13826 -- Following test is defence against rubbish errors
13828 if No (Alias (Subp)) then
13832 -- Retrieve the resolved actual from the renaming declaration
13833 -- created for the instantiated formal.
13835 Actual := Entity (Name (Parent (Parent (Subp))));
13836 Set_Entity (Def, Actual);
13837 Set_Etype (Def, Etype (Actual));
13839 if Is_Global (Actual) then
13841 Make_Generic_Association (Loc,
13842 Selector_Name => New_Occurrence_Of (Subp, Loc),
13843 Explicit_Generic_Actual_Parameter =>
13844 New_Occurrence_Of (Actual, Loc));
13846 Set_Associated_Node
13847 (Explicit_Generic_Actual_Parameter (Ndec), Def);
13849 Append (Ndec, Assoc1);
13851 -- If there are other defaults, add a dummy association in case
13852 -- there are other defaulted formals with the same name.
13854 elsif Present (Next (Act2)) then
13856 Make_Generic_Association (Loc,
13857 Selector_Name => New_Occurrence_Of (Subp, Loc),
13858 Explicit_Generic_Actual_Parameter => Empty);
13860 Append (Ndec, Assoc1);
13867 if Nkind (Name (N1)) = N_Identifier
13868 and then Is_Child_Unit (Gen_Id)
13869 and then Is_Global (Gen_Id)
13870 and then Is_Generic_Unit (Scope (Gen_Id))
13871 and then In_Open_Scopes (Scope (Gen_Id))
13873 -- This is an instantiation of a child unit within a sibling, so
13874 -- that the generic parent is in scope. An eventual instance must
13875 -- occur within the scope of an instance of the parent. Make name
13876 -- in instance into an expanded name, to preserve the identifier
13877 -- of the parent, so it can be resolved subsequently.
13879 Rewrite (Name (N2),
13880 Make_Expanded_Name (Loc,
13881 Chars => Chars (Gen_Id),
13882 Prefix => New_Occurrence_Of (Scope (Gen_Id), Loc),
13883 Selector_Name => New_Occurrence_Of (Gen_Id, Loc)));
13884 Set_Entity (Name (N2), Gen_Id);
13886 Rewrite (Name (N1),
13887 Make_Expanded_Name (Loc,
13888 Chars => Chars (Gen_Id),
13889 Prefix => New_Occurrence_Of (Scope (Gen_Id), Loc),
13890 Selector_Name => New_Occurrence_Of (Gen_Id, Loc)));
13892 Set_Associated_Node (Name (N1), Name (N2));
13893 Set_Associated_Node (Prefix (Name (N1)), Empty);
13894 Set_Associated_Node
13895 (Selector_Name (Name (N1)), Selector_Name (Name (N2)));
13896 Set_Etype (Name (N1), Etype (Gen_Id));
13899 end Save_Global_Defaults;
13901 ----------------------------
13902 -- Save_Global_Descendant --
13903 ----------------------------
13905 procedure Save_Global_Descendant (D : Union_Id) is
13909 if D in Node_Range then
13910 if D = Union_Id (Empty) then
13913 elsif Nkind (Node_Id (D)) /= N_Compilation_Unit then
13914 Save_References (Node_Id (D));
13917 elsif D in List_Range then
13918 if D = Union_Id (No_List)
13919 or else Is_Empty_List (List_Id (D))
13924 N1 := First (List_Id (D));
13925 while Present (N1) loop
13926 Save_References (N1);
13931 -- Element list or other non-node field, nothing to do
13936 end Save_Global_Descendant;
13938 ---------------------
13939 -- Save_References --
13940 ---------------------
13942 -- This is the recursive procedure that does the work once the enclosing
13943 -- generic scope has been established. We have to treat specially a
13944 -- number of node rewritings that are required by semantic processing
13945 -- and which change the kind of nodes in the generic copy: typically
13946 -- constant-folding, replacing an operator node by a string literal, or
13947 -- a selected component by an expanded name. In each of those cases, the
13948 -- transformation is propagated to the generic unit.
13950 procedure Save_References (N : Node_Id) is
13951 Loc : constant Source_Ptr := Sloc (N);
13957 elsif Nkind_In (N, N_Character_Literal, N_Operator_Symbol) then
13958 if Nkind (N) = Nkind (Get_Associated_Node (N)) then
13961 elsif Nkind (N) = N_Operator_Symbol
13962 and then Nkind (Get_Associated_Node (N)) = N_String_Literal
13964 Change_Operator_Symbol_To_String_Literal (N);
13967 elsif Nkind (N) in N_Op then
13968 if Nkind (N) = Nkind (Get_Associated_Node (N)) then
13969 if Nkind (N) = N_Op_Concat then
13970 Set_Is_Component_Left_Opnd (N,
13971 Is_Component_Left_Opnd (Get_Associated_Node (N)));
13973 Set_Is_Component_Right_Opnd (N,
13974 Is_Component_Right_Opnd (Get_Associated_Node (N)));
13980 -- Node may be transformed into call to a user-defined operator
13982 N2 := Get_Associated_Node (N);
13984 if Nkind (N2) = N_Function_Call then
13985 E := Entity (Name (N2));
13988 and then Is_Global (E)
13990 Set_Etype (N, Etype (N2));
13992 Set_Associated_Node (N, Empty);
13993 Set_Etype (N, Empty);
13996 elsif Nkind_In (N2, N_Integer_Literal,
14000 if Present (Original_Node (N2))
14001 and then Nkind (Original_Node (N2)) = Nkind (N)
14004 -- Operation was constant-folded. Whenever possible,
14005 -- recover semantic information from unfolded node,
14008 Set_Associated_Node (N, Original_Node (N2));
14010 if Nkind (N) = N_Op_Concat then
14011 Set_Is_Component_Left_Opnd (N,
14012 Is_Component_Left_Opnd (Get_Associated_Node (N)));
14013 Set_Is_Component_Right_Opnd (N,
14014 Is_Component_Right_Opnd (Get_Associated_Node (N)));
14020 -- If original node is already modified, propagate
14021 -- constant-folding to template.
14023 Rewrite (N, New_Copy (N2));
14024 Set_Analyzed (N, False);
14027 elsif Nkind (N2) = N_Identifier
14028 and then Ekind (Entity (N2)) = E_Enumeration_Literal
14030 -- Same if call was folded into a literal, but in this case
14031 -- retain the entity to avoid spurious ambiguities if it is
14032 -- overloaded at the point of instantiation or inlining.
14034 Rewrite (N, New_Copy (N2));
14035 Set_Analyzed (N, False);
14039 -- Complete operands check if node has not been constant-folded
14041 if Nkind (N) in N_Op then
14042 Save_Entity_Descendants (N);
14045 elsif Nkind (N) = N_Identifier then
14046 if Nkind (N) = Nkind (Get_Associated_Node (N)) then
14048 -- If this is a discriminant reference, always save it. It is
14049 -- used in the instance to find the corresponding discriminant
14050 -- positionally rather than by name.
14052 Set_Original_Discriminant
14053 (N, Original_Discriminant (Get_Associated_Node (N)));
14057 N2 := Get_Associated_Node (N);
14059 if Nkind (N2) = N_Function_Call then
14060 E := Entity (Name (N2));
14062 -- Name resolves to a call to parameterless function. If
14063 -- original entity is global, mark node as resolved.
14066 and then Is_Global (E)
14068 Set_Etype (N, Etype (N2));
14070 Set_Associated_Node (N, Empty);
14071 Set_Etype (N, Empty);
14074 elsif Nkind_In (N2, N_Integer_Literal, N_Real_Literal)
14075 and then Is_Entity_Name (Original_Node (N2))
14077 -- Name resolves to named number that is constant-folded,
14078 -- We must preserve the original name for ASIS use, and
14079 -- undo the constant-folding, which will be repeated in
14082 Set_Associated_Node (N, Original_Node (N2));
14085 elsif Nkind (N2) = N_String_Literal then
14087 -- Name resolves to string literal. Perform the same
14088 -- replacement in generic.
14090 Rewrite (N, New_Copy (N2));
14092 elsif Nkind (N2) = N_Explicit_Dereference then
14094 -- An identifier is rewritten as a dereference if it is the
14095 -- prefix in an implicit dereference (call or attribute).
14096 -- The analysis of an instantiation will expand the node
14097 -- again, so we preserve the original tree but link it to
14098 -- the resolved entity in case it is global.
14100 if Is_Entity_Name (Prefix (N2))
14101 and then Present (Entity (Prefix (N2)))
14102 and then Is_Global (Entity (Prefix (N2)))
14104 Set_Associated_Node (N, Prefix (N2));
14106 elsif Nkind (Prefix (N2)) = N_Function_Call
14107 and then Is_Global (Entity (Name (Prefix (N2))))
14110 Make_Explicit_Dereference (Loc,
14111 Prefix => Make_Function_Call (Loc,
14113 New_Occurrence_Of (Entity (Name (Prefix (N2))),
14117 Set_Associated_Node (N, Empty);
14118 Set_Etype (N, Empty);
14121 -- The subtype mark of a nominally unconstrained object is
14122 -- rewritten as a subtype indication using the bounds of the
14123 -- expression. Recover the original subtype mark.
14125 elsif Nkind (N2) = N_Subtype_Indication
14126 and then Is_Entity_Name (Original_Node (N2))
14128 Set_Associated_Node (N, Original_Node (N2));
14136 elsif Nkind (N) in N_Entity then
14141 Qual : Node_Id := Empty;
14142 Typ : Entity_Id := Empty;
14145 use Atree.Unchecked_Access;
14146 -- This code section is part of implementing an untyped tree
14147 -- traversal, so it needs direct access to node fields.
14150 if Nkind_In (N, N_Aggregate, N_Extension_Aggregate) then
14151 N2 := Get_Associated_Node (N);
14158 -- In an instance within a generic, use the name of the
14159 -- actual and not the original generic parameter. If the
14160 -- actual is global in the current generic it must be
14161 -- preserved for its instantiation.
14163 if Nkind (Parent (Typ)) = N_Subtype_Declaration
14165 Present (Generic_Parent_Type (Parent (Typ)))
14167 Typ := Base_Type (Typ);
14168 Set_Etype (N2, Typ);
14174 or else not Is_Global (Typ)
14176 Set_Associated_Node (N, Empty);
14178 -- If the aggregate is an actual in a call, it has been
14179 -- resolved in the current context, to some local type.
14180 -- The enclosing call may have been disambiguated by the
14181 -- aggregate, and this disambiguation might fail at
14182 -- instantiation time because the type to which the
14183 -- aggregate did resolve is not preserved. In order to
14184 -- preserve some of this information, we wrap the
14185 -- aggregate in a qualified expression, using the id of
14186 -- its type. For further disambiguation we qualify the
14187 -- type name with its scope (if visible) because both
14188 -- id's will have corresponding entities in an instance.
14189 -- This resolves most of the problems with missing type
14190 -- information on aggregates in instances.
14192 if Nkind (N2) = Nkind (N)
14193 and then Nkind (Parent (N2)) in N_Subprogram_Call
14194 and then Comes_From_Source (Typ)
14196 if Is_Immediately_Visible (Scope (Typ)) then
14197 Nam := Make_Selected_Component (Loc,
14199 Make_Identifier (Loc, Chars (Scope (Typ))),
14201 Make_Identifier (Loc, Chars (Typ)));
14203 Nam := Make_Identifier (Loc, Chars (Typ));
14207 Make_Qualified_Expression (Loc,
14208 Subtype_Mark => Nam,
14209 Expression => Relocate_Node (N));
14213 Save_Global_Descendant (Field1 (N));
14214 Save_Global_Descendant (Field2 (N));
14215 Save_Global_Descendant (Field3 (N));
14216 Save_Global_Descendant (Field5 (N));
14218 if Present (Qual) then
14222 -- All other cases than aggregates
14225 Save_Global_Descendant (Field1 (N));
14226 Save_Global_Descendant (Field2 (N));
14227 Save_Global_Descendant (Field3 (N));
14228 Save_Global_Descendant (Field4 (N));
14229 Save_Global_Descendant (Field5 (N));
14234 -- If a node has aspects, references within their expressions must
14235 -- be saved separately, given they are not directly in the tree.
14237 if Has_Aspects (N) then
14242 Aspect := First (Aspect_Specifications (N));
14243 while Present (Aspect) loop
14244 if Present (Expression (Aspect)) then
14245 Save_Global_References (Expression (Aspect));
14252 end Save_References;
14254 -- Start of processing for Save_Global_References
14257 Gen_Scope := Current_Scope;
14259 -- If the generic unit is a child unit, references to entities in the
14260 -- parent are treated as local, because they will be resolved anew in
14261 -- the context of the instance of the parent.
14263 while Is_Child_Unit (Gen_Scope)
14264 and then Ekind (Scope (Gen_Scope)) = E_Generic_Package
14266 Gen_Scope := Scope (Gen_Scope);
14269 Save_References (N);
14270 end Save_Global_References;
14272 --------------------------------------
14273 -- Set_Copied_Sloc_For_Inlined_Body --
14274 --------------------------------------
14276 procedure Set_Copied_Sloc_For_Inlined_Body (N : Node_Id; E : Entity_Id) is
14278 Create_Instantiation_Source (N, E, True, S_Adjustment);
14279 end Set_Copied_Sloc_For_Inlined_Body;
14281 ---------------------
14282 -- Set_Instance_Of --
14283 ---------------------
14285 procedure Set_Instance_Of (A : Entity_Id; B : Entity_Id) is
14287 Generic_Renamings.Table (Generic_Renamings.Last) := (A, B, Assoc_Null);
14288 Generic_Renamings_HTable.Set (Generic_Renamings.Last);
14289 Generic_Renamings.Increment_Last;
14290 end Set_Instance_Of;
14292 --------------------
14293 -- Set_Next_Assoc --
14294 --------------------
14296 procedure Set_Next_Assoc (E : Assoc_Ptr; Next : Assoc_Ptr) is
14298 Generic_Renamings.Table (E).Next_In_HTable := Next;
14299 end Set_Next_Assoc;
14301 -------------------
14302 -- Start_Generic --
14303 -------------------
14305 procedure Start_Generic is
14307 -- ??? More things could be factored out in this routine.
14308 -- Should probably be done at a later stage.
14310 Generic_Flags.Append (Inside_A_Generic);
14311 Inside_A_Generic := True;
14313 Expander_Mode_Save_And_Set (False);
14316 ----------------------
14317 -- Set_Instance_Env --
14318 ----------------------
14320 procedure Set_Instance_Env
14321 (Gen_Unit : Entity_Id;
14322 Act_Unit : Entity_Id)
14324 Assertion_Status : constant Boolean := Assertions_Enabled;
14325 Save_SPARK_Mode : constant SPARK_Mode_Type := SPARK_Mode;
14326 Save_SPARK_Mode_Pragma : constant Node_Id := SPARK_Mode_Pragma;
14329 -- Regardless of the current mode, predefined units are analyzed in the
14330 -- most current Ada mode, and earlier version Ada checks do not apply
14331 -- to predefined units. Nothing needs to be done for non-internal units.
14332 -- These are always analyzed in the current mode.
14334 if Is_Internal_File_Name
14335 (Fname => Unit_File_Name (Get_Source_Unit (Gen_Unit)),
14336 Renamings_Included => True)
14338 Set_Opt_Config_Switches (True, Current_Sem_Unit = Main_Unit);
14340 -- In Ada2012 we may want to enable assertions in an instance of a
14341 -- predefined unit, in which case we need to preserve the current
14342 -- setting for the Assertions_Enabled flag. This will become more
14343 -- critical when pre/postconditions are added to predefined units,
14344 -- as is already the case for some numeric libraries.
14346 if Ada_Version >= Ada_2012 then
14347 Assertions_Enabled := Assertion_Status;
14350 -- SPARK_Mode for an instance is the one applicable at the point of
14353 SPARK_Mode := Save_SPARK_Mode;
14354 SPARK_Mode_Pragma := Save_SPARK_Mode_Pragma;
14357 Current_Instantiated_Parent :=
14358 (Gen_Id => Gen_Unit,
14359 Act_Id => Act_Unit,
14360 Next_In_HTable => Assoc_Null);
14361 end Set_Instance_Env;
14367 procedure Switch_View (T : Entity_Id) is
14368 BT : constant Entity_Id := Base_Type (T);
14369 Priv_Elmt : Elmt_Id := No_Elmt;
14370 Priv_Sub : Entity_Id;
14373 -- T may be private but its base type may have been exchanged through
14374 -- some other occurrence, in which case there is nothing to switch
14375 -- besides T itself. Note that a private dependent subtype of a private
14376 -- type might not have been switched even if the base type has been,
14377 -- because of the last branch of Check_Private_View (see comment there).
14379 if not Is_Private_Type (BT) then
14380 Prepend_Elmt (Full_View (T), Exchanged_Views);
14381 Exchange_Declarations (T);
14385 Priv_Elmt := First_Elmt (Private_Dependents (BT));
14387 if Present (Full_View (BT)) then
14388 Prepend_Elmt (Full_View (BT), Exchanged_Views);
14389 Exchange_Declarations (BT);
14392 while Present (Priv_Elmt) loop
14393 Priv_Sub := (Node (Priv_Elmt));
14395 -- We avoid flipping the subtype if the Etype of its full view is
14396 -- private because this would result in a malformed subtype. This
14397 -- occurs when the Etype of the subtype full view is the full view of
14398 -- the base type (and since the base types were just switched, the
14399 -- subtype is pointing to the wrong view). This is currently the case
14400 -- for tagged record types, access types (maybe more?) and needs to
14401 -- be resolved. ???
14403 if Present (Full_View (Priv_Sub))
14404 and then not Is_Private_Type (Etype (Full_View (Priv_Sub)))
14406 Prepend_Elmt (Full_View (Priv_Sub), Exchanged_Views);
14407 Exchange_Declarations (Priv_Sub);
14410 Next_Elmt (Priv_Elmt);
14418 function True_Parent (N : Node_Id) return Node_Id is
14420 if Nkind (Parent (N)) = N_Subunit then
14421 return Parent (Corresponding_Stub (Parent (N)));
14427 -----------------------------
14428 -- Valid_Default_Attribute --
14429 -----------------------------
14431 procedure Valid_Default_Attribute (Nam : Entity_Id; Def : Node_Id) is
14432 Attr_Id : constant Attribute_Id :=
14433 Get_Attribute_Id (Attribute_Name (Def));
14434 T : constant Entity_Id := Entity (Prefix (Def));
14435 Is_Fun : constant Boolean := (Ekind (Nam) = E_Function);
14448 F := First_Formal (Nam);
14449 while Present (F) loop
14450 Num_F := Num_F + 1;
14455 when Attribute_Adjacent | Attribute_Ceiling | Attribute_Copy_Sign |
14456 Attribute_Floor | Attribute_Fraction | Attribute_Machine |
14457 Attribute_Model | Attribute_Remainder | Attribute_Rounding |
14458 Attribute_Unbiased_Rounding =>
14461 and then Is_Floating_Point_Type (T);
14463 when Attribute_Image | Attribute_Pred | Attribute_Succ |
14464 Attribute_Value | Attribute_Wide_Image |
14465 Attribute_Wide_Value =>
14466 OK := (Is_Fun and then Num_F = 1 and then Is_Scalar_Type (T));
14468 when Attribute_Max | Attribute_Min =>
14469 OK := (Is_Fun and then Num_F = 2 and then Is_Scalar_Type (T));
14471 when Attribute_Input =>
14472 OK := (Is_Fun and then Num_F = 1);
14474 when Attribute_Output | Attribute_Read | Attribute_Write =>
14475 OK := (not Is_Fun and then Num_F = 2);
14482 Error_Msg_N ("attribute reference has wrong profile for subprogram",
14485 end Valid_Default_Attribute;