1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
9 -- Copyright (C) 1992-2014, Free Software Foundation, Inc. --
11 -- GNAT is free software; you can redistribute it and/or modify it under --
12 -- terms of the GNU General Public License as published by the Free Soft- --
13 -- ware Foundation; either version 3, or (at your option) any later ver- --
14 -- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
15 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
16 -- or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License --
17 -- for more details. You should have received a copy of the GNU General --
18 -- Public License distributed with GNAT; see file COPYING3. If not, go to --
19 -- http://www.gnu.org/licenses for a complete copy of the license. --
21 -- GNAT was originally developed by the GNAT team at New York University. --
22 -- Extensive contributions were provided by Ada Core Technologies Inc. --
24 ------------------------------------------------------------------------------
26 with Aspects; use Aspects;
27 with Atree; use Atree;
28 with Einfo; use Einfo;
29 with Elists; use Elists;
30 with Errout; use Errout;
31 with Expander; use Expander;
32 with Exp_Disp; use Exp_Disp;
33 with Fname; use Fname;
34 with Fname.UF; use Fname.UF;
35 with Freeze; use Freeze;
36 with Itypes; use Itypes;
38 with Lib.Load; use Lib.Load;
39 with Lib.Xref; use Lib.Xref;
40 with Nlists; use Nlists;
41 with Namet; use Namet;
42 with Nmake; use Nmake;
44 with Rident; use Rident;
45 with Restrict; use Restrict;
46 with Rtsfind; use Rtsfind;
48 with Sem_Aux; use Sem_Aux;
49 with Sem_Cat; use Sem_Cat;
50 with Sem_Ch3; use Sem_Ch3;
51 with Sem_Ch6; use Sem_Ch6;
52 with Sem_Ch7; use Sem_Ch7;
53 with Sem_Ch8; use Sem_Ch8;
54 with Sem_Ch10; use Sem_Ch10;
55 with Sem_Ch13; use Sem_Ch13;
56 with Sem_Dim; use Sem_Dim;
57 with Sem_Disp; use Sem_Disp;
58 with Sem_Elab; use Sem_Elab;
59 with Sem_Elim; use Sem_Elim;
60 with Sem_Eval; use Sem_Eval;
61 with Sem_Prag; use Sem_Prag;
62 with Sem_Res; use Sem_Res;
63 with Sem_Type; use Sem_Type;
64 with Sem_Util; use Sem_Util;
65 with Sem_Warn; use Sem_Warn;
66 with Stand; use Stand;
67 with Sinfo; use Sinfo;
68 with Sinfo.CN; use Sinfo.CN;
69 with Sinput; use Sinput;
70 with Sinput.L; use Sinput.L;
71 with Snames; use Snames;
72 with Stringt; use Stringt;
73 with Uname; use Uname;
75 with Tbuild; use Tbuild;
76 with Uintp; use Uintp;
77 with Urealp; use Urealp;
78 with Warnsw; use Warnsw;
82 package body Sem_Ch12 is
84 ----------------------------------------------------------
85 -- Implementation of Generic Analysis and Instantiation --
86 ----------------------------------------------------------
88 -- GNAT implements generics by macro expansion. No attempt is made to share
89 -- generic instantiations (for now). Analysis of a generic definition does
90 -- not perform any expansion action, but the expander must be called on the
91 -- tree for each instantiation, because the expansion may of course depend
92 -- on the generic actuals. All of this is best achieved as follows:
94 -- a) Semantic analysis of a generic unit is performed on a copy of the
95 -- tree for the generic unit. All tree modifications that follow analysis
96 -- do not affect the original tree. Links are kept between the original
97 -- tree and the copy, in order to recognize non-local references within
98 -- the generic, and propagate them to each instance (recall that name
99 -- resolution is done on the generic declaration: generics are not really
100 -- macros). This is summarized in the following diagram:
102 -- .-----------. .----------.
103 -- | semantic |<--------------| generic |
105 -- | |==============>| |
106 -- |___________| global |__________|
117 -- b) Each instantiation copies the original tree, and inserts into it a
118 -- series of declarations that describe the mapping between generic formals
119 -- and actuals. For example, a generic In OUT parameter is an object
120 -- renaming of the corresponding actual, etc. Generic IN parameters are
121 -- constant declarations.
123 -- c) In order to give the right visibility for these renamings, we use
124 -- a different scheme for package and subprogram instantiations. For
125 -- packages, the list of renamings is inserted into the package
126 -- specification, before the visible declarations of the package. The
127 -- renamings are analyzed before any of the text of the instance, and are
128 -- thus visible at the right place. Furthermore, outside of the instance,
129 -- the generic parameters are visible and denote their corresponding
132 -- For subprograms, we create a container package to hold the renamings
133 -- and the subprogram instance itself. Analysis of the package makes the
134 -- renaming declarations visible to the subprogram. After analyzing the
135 -- package, the defining entity for the subprogram is touched-up so that
136 -- it appears declared in the current scope, and not inside the container
139 -- If the instantiation is a compilation unit, the container package is
140 -- given the same name as the subprogram instance. This ensures that
141 -- the elaboration procedure called by the binder, using the compilation
142 -- unit name, calls in fact the elaboration procedure for the package.
144 -- Not surprisingly, private types complicate this approach. By saving in
145 -- the original generic object the non-local references, we guarantee that
146 -- the proper entities are referenced at the point of instantiation.
147 -- However, for private types, this by itself does not insure that the
148 -- proper VIEW of the entity is used (the full type may be visible at the
149 -- point of generic definition, but not at instantiation, or vice-versa).
150 -- In order to reference the proper view, we special-case any reference
151 -- to private types in the generic object, by saving both views, one in
152 -- the generic and one in the semantic copy. At time of instantiation, we
153 -- check whether the two views are consistent, and exchange declarations if
154 -- necessary, in order to restore the correct visibility. Similarly, if
155 -- the instance view is private when the generic view was not, we perform
156 -- the exchange. After completing the instantiation, we restore the
157 -- current visibility. The flag Has_Private_View marks identifiers in the
158 -- the generic unit that require checking.
160 -- Visibility within nested generic units requires special handling.
161 -- Consider the following scheme:
163 -- type Global is ... -- outside of generic unit.
167 -- type Semi_Global is ... -- global to inner.
170 -- procedure inner (X1 : Global; X2 : Semi_Global);
172 -- procedure in2 is new inner (...); -- 4
175 -- package New_Outer is new Outer (...); -- 2
176 -- procedure New_Inner is new New_Outer.Inner (...); -- 3
178 -- The semantic analysis of Outer captures all occurrences of Global.
179 -- The semantic analysis of Inner (at 1) captures both occurrences of
180 -- Global and Semi_Global.
182 -- At point 2 (instantiation of Outer), we also produce a generic copy
183 -- of Inner, even though Inner is, at that point, not being instantiated.
184 -- (This is just part of the semantic analysis of New_Outer).
186 -- Critically, references to Global within Inner must be preserved, while
187 -- references to Semi_Global should not preserved, because they must now
188 -- resolve to an entity within New_Outer. To distinguish between these, we
189 -- use a global variable, Current_Instantiated_Parent, which is set when
190 -- performing a generic copy during instantiation (at 2). This variable is
191 -- used when performing a generic copy that is not an instantiation, but
192 -- that is nested within one, as the occurrence of 1 within 2. The analysis
193 -- of a nested generic only preserves references that are global to the
194 -- enclosing Current_Instantiated_Parent. We use the Scope_Depth value to
195 -- determine whether a reference is external to the given parent.
197 -- The instantiation at point 3 requires no special treatment. The method
198 -- works as well for further nestings of generic units, but of course the
199 -- variable Current_Instantiated_Parent must be stacked because nested
200 -- instantiations can occur, e.g. the occurrence of 4 within 2.
202 -- The instantiation of package and subprogram bodies is handled in a
203 -- similar manner, except that it is delayed until after semantic
204 -- analysis is complete. In this fashion complex cross-dependencies
205 -- between several package declarations and bodies containing generics
206 -- can be compiled which otherwise would diagnose spurious circularities.
208 -- For example, it is possible to compile two packages A and B that
209 -- have the following structure:
211 -- package A is package B is
212 -- generic ... generic ...
213 -- package G_A is package G_B is
216 -- package body A is package body B is
217 -- package N_B is new G_B (..) package N_A is new G_A (..)
219 -- The table Pending_Instantiations in package Inline is used to keep
220 -- track of body instantiations that are delayed in this manner. Inline
221 -- handles the actual calls to do the body instantiations. This activity
222 -- is part of Inline, since the processing occurs at the same point, and
223 -- for essentially the same reason, as the handling of inlined routines.
225 ----------------------------------------------
226 -- Detection of Instantiation Circularities --
227 ----------------------------------------------
229 -- If we have a chain of instantiations that is circular, this is static
230 -- error which must be detected at compile time. The detection of these
231 -- circularities is carried out at the point that we insert a generic
232 -- instance spec or body. If there is a circularity, then the analysis of
233 -- the offending spec or body will eventually result in trying to load the
234 -- same unit again, and we detect this problem as we analyze the package
235 -- instantiation for the second time.
237 -- At least in some cases after we have detected the circularity, we get
238 -- into trouble if we try to keep going. The following flag is set if a
239 -- circularity is detected, and used to abandon compilation after the
240 -- messages have been posted.
242 Circularity_Detected : Boolean := False;
243 -- This should really be reset on encountering a new main unit, but in
244 -- practice we are not using multiple main units so it is not critical.
246 --------------------------------------------------
247 -- Formal packages and partial parameterization --
248 --------------------------------------------------
250 -- When compiling a generic, a formal package is a local instantiation. If
251 -- declared with a box, its generic formals are visible in the enclosing
252 -- generic. If declared with a partial list of actuals, those actuals that
253 -- are defaulted (covered by an Others clause, or given an explicit box
254 -- initialization) are also visible in the enclosing generic, while those
255 -- that have a corresponding actual are not.
257 -- In our source model of instantiation, the same visibility must be
258 -- present in the spec and body of an instance: the names of the formals
259 -- that are defaulted must be made visible within the instance, and made
260 -- invisible (hidden) after the instantiation is complete, so that they
261 -- are not accessible outside of the instance.
263 -- In a generic, a formal package is treated like a special instantiation.
264 -- Our Ada 95 compiler handled formals with and without box in different
265 -- ways. With partial parameterization, we use a single model for both.
266 -- We create a package declaration that consists of the specification of
267 -- the generic package, and a set of declarations that map the actuals
268 -- into local renamings, just as we do for bona fide instantiations. For
269 -- defaulted parameters and formals with a box, we copy directly the
270 -- declarations of the formal into this local package. The result is a
271 -- a package whose visible declarations may include generic formals. This
272 -- package is only used for type checking and visibility analysis, and
273 -- never reaches the back-end, so it can freely violate the placement
274 -- rules for generic formal declarations.
276 -- The list of declarations (renamings and copies of formals) is built
277 -- by Analyze_Associations, just as for regular instantiations.
279 -- At the point of instantiation, conformance checking must be applied only
280 -- to those parameters that were specified in the formal. We perform this
281 -- checking by creating another internal instantiation, this one including
282 -- only the renamings and the formals (the rest of the package spec is not
283 -- relevant to conformance checking). We can then traverse two lists: the
284 -- list of actuals in the instance that corresponds to the formal package,
285 -- and the list of actuals produced for this bogus instantiation. We apply
286 -- the conformance rules to those actuals that are not defaulted (i.e.
287 -- which still appear as generic formals.
289 -- When we compile an instance body we must make the right parameters
290 -- visible again. The predicate Is_Generic_Formal indicates which of the
291 -- formals should have its Is_Hidden flag reset.
293 -----------------------
294 -- Local subprograms --
295 -----------------------
297 procedure Abandon_Instantiation (N : Node_Id);
298 pragma No_Return (Abandon_Instantiation);
299 -- Posts an error message "instantiation abandoned" at the indicated node
300 -- and then raises the exception Instantiation_Error to do it.
302 procedure Analyze_Formal_Array_Type
303 (T : in out Entity_Id;
305 -- A formal array type is treated like an array type declaration, and
306 -- invokes Array_Type_Declaration (sem_ch3) whose first parameter is
307 -- in-out, because in the case of an anonymous type the entity is
308 -- actually created in the procedure.
310 -- The following procedures treat other kinds of formal parameters
312 procedure Analyze_Formal_Derived_Interface_Type
317 procedure Analyze_Formal_Derived_Type
322 procedure Analyze_Formal_Interface_Type
327 -- The following subprograms create abbreviated declarations for formal
328 -- scalar types. We introduce an anonymous base of the proper class for
329 -- each of them, and define the formals as constrained first subtypes of
330 -- their bases. The bounds are expressions that are non-static in the
333 procedure Analyze_Formal_Decimal_Fixed_Point_Type
334 (T : Entity_Id; Def : Node_Id);
335 procedure Analyze_Formal_Discrete_Type (T : Entity_Id; Def : Node_Id);
336 procedure Analyze_Formal_Floating_Type (T : Entity_Id; Def : Node_Id);
337 procedure Analyze_Formal_Signed_Integer_Type (T : Entity_Id; Def : Node_Id);
338 procedure Analyze_Formal_Modular_Type (T : Entity_Id; Def : Node_Id);
339 procedure Analyze_Formal_Ordinary_Fixed_Point_Type
340 (T : Entity_Id; Def : Node_Id);
342 procedure Analyze_Formal_Private_Type
346 -- Creates a new private type, which does not require completion
348 procedure Analyze_Formal_Incomplete_Type (T : Entity_Id; Def : Node_Id);
349 -- Ada 2012: Creates a new incomplete type whose actual does not freeze
351 procedure Analyze_Generic_Formal_Part (N : Node_Id);
352 -- Analyze generic formal part
354 procedure Analyze_Generic_Access_Type (T : Entity_Id; Def : Node_Id);
355 -- Create a new access type with the given designated type
357 function Analyze_Associations
360 F_Copy : List_Id) return List_Id;
361 -- At instantiation time, build the list of associations between formals
362 -- and actuals. Each association becomes a renaming declaration for the
363 -- formal entity. F_Copy is the analyzed list of formals in the generic
364 -- copy. It is used to apply legality checks to the actuals. I_Node is the
365 -- instantiation node itself.
367 procedure Analyze_Subprogram_Instantiation
371 procedure Build_Instance_Compilation_Unit_Nodes
375 -- This procedure is used in the case where the generic instance of a
376 -- subprogram body or package body is a library unit. In this case, the
377 -- original library unit node for the generic instantiation must be
378 -- replaced by the resulting generic body, and a link made to a new
379 -- compilation unit node for the generic declaration. The argument N is
380 -- the original generic instantiation. Act_Body and Act_Decl are the body
381 -- and declaration of the instance (either package body and declaration
382 -- nodes or subprogram body and declaration nodes depending on the case).
383 -- On return, the node N has been rewritten with the actual body.
385 procedure Check_Access_Definition (N : Node_Id);
386 -- Subsidiary routine to null exclusion processing. Perform an assertion
387 -- check on Ada version and the presence of an access definition in N.
389 procedure Check_Formal_Packages (P_Id : Entity_Id);
390 -- Apply the following to all formal packages in generic associations
392 procedure Check_Formal_Package_Instance
393 (Formal_Pack : Entity_Id;
394 Actual_Pack : Entity_Id);
395 -- Verify that the actuals of the actual instance match the actuals of
396 -- the template for a formal package that is not declared with a box.
398 procedure Check_Forward_Instantiation (Decl : Node_Id);
399 -- If the generic is a local entity and the corresponding body has not
400 -- been seen yet, flag enclosing packages to indicate that it will be
401 -- elaborated after the generic body. Subprograms declared in the same
402 -- package cannot be inlined by the front-end because front-end inlining
403 -- requires a strict linear order of elaboration.
405 function Check_Hidden_Primitives (Assoc_List : List_Id) return Elist_Id;
406 -- Check if some association between formals and actuals requires to make
407 -- visible primitives of a tagged type, and make those primitives visible.
408 -- Return the list of primitives whose visibility is modified (to restore
409 -- their visibility later through Restore_Hidden_Primitives). If no
410 -- candidate is found then return No_Elist.
412 procedure Check_Hidden_Child_Unit
414 Gen_Unit : Entity_Id;
415 Act_Decl_Id : Entity_Id);
416 -- If the generic unit is an implicit child instance within a parent
417 -- instance, we need to make an explicit test that it is not hidden by
418 -- a child instance of the same name and parent.
420 procedure Check_Generic_Actuals
421 (Instance : Entity_Id;
422 Is_Formal_Box : Boolean);
423 -- Similar to previous one. Check the actuals in the instantiation,
424 -- whose views can change between the point of instantiation and the point
425 -- of instantiation of the body. In addition, mark the generic renamings
426 -- as generic actuals, so that they are not compatible with other actuals.
427 -- Recurse on an actual that is a formal package whose declaration has
430 function Contains_Instance_Of
433 N : Node_Id) return Boolean;
434 -- Inner is instantiated within the generic Outer. Check whether Inner
435 -- directly or indirectly contains an instance of Outer or of one of its
436 -- parents, in the case of a subunit. Each generic unit holds a list of
437 -- the entities instantiated within (at any depth). This procedure
438 -- determines whether the set of such lists contains a cycle, i.e. an
439 -- illegal circular instantiation.
441 function Denotes_Formal_Package
443 On_Exit : Boolean := False;
444 Instance : Entity_Id := Empty) return Boolean;
445 -- Returns True if E is a formal package of an enclosing generic, or
446 -- the actual for such a formal in an enclosing instantiation. If such
447 -- a package is used as a formal in an nested generic, or as an actual
448 -- in a nested instantiation, the visibility of ITS formals should not
449 -- be modified. When called from within Restore_Private_Views, the flag
450 -- On_Exit is true, to indicate that the search for a possible enclosing
451 -- instance should ignore the current one. In that case Instance denotes
452 -- the declaration for which this is an actual. This declaration may be
453 -- an instantiation in the source, or the internal instantiation that
454 -- corresponds to the actual for a formal package.
456 function Earlier (N1, N2 : Node_Id) return Boolean;
457 -- Yields True if N1 and N2 appear in the same compilation unit,
458 -- ignoring subunits, and if N1 is to the left of N2 in a left-to-right
459 -- traversal of the tree for the unit. Used to determine the placement
460 -- of freeze nodes for instance bodies that may depend on other instances.
462 function Find_Actual_Type
464 Gen_Type : Entity_Id) return Entity_Id;
465 -- When validating the actual types of a child instance, check whether
466 -- the formal is a formal type of the parent unit, and retrieve the current
467 -- actual for it. Typ is the entity in the analyzed formal type declaration
468 -- (component or index type of an array type, or designated type of an
469 -- access formal) and Gen_Type is the enclosing analyzed formal array
470 -- or access type. The desired actual may be a formal of a parent, or may
471 -- be declared in a formal package of a parent. In both cases it is a
472 -- generic actual type because it appears within a visible instance.
473 -- Finally, it may be declared in a parent unit without being a formal
474 -- of that unit, in which case it must be retrieved by visibility.
475 -- Ambiguities may still arise if two homonyms are declared in two formal
476 -- packages, and the prefix of the formal type may be needed to resolve
477 -- the ambiguity in the instance ???
479 function In_Same_Declarative_Part
481 Inst : Node_Id) return Boolean;
482 -- True if the instantiation Inst and the given freeze_node F_Node appear
483 -- within the same declarative part, ignoring subunits, but with no inter-
484 -- vening subprograms or concurrent units. Used to find the proper plave
485 -- for the freeze node of an instance, when the generic is declared in a
486 -- previous instance. If predicate is true, the freeze node of the instance
487 -- can be placed after the freeze node of the previous instance, Otherwise
488 -- it has to be placed at the end of the current declarative part.
490 function In_Main_Context (E : Entity_Id) return Boolean;
491 -- Check whether an instantiation is in the context of the main unit.
492 -- Used to determine whether its body should be elaborated to allow
493 -- front-end inlining.
495 procedure Set_Instance_Env
496 (Gen_Unit : Entity_Id;
497 Act_Unit : Entity_Id);
498 -- Save current instance on saved environment, to be used to determine
499 -- the global status of entities in nested instances. Part of Save_Env.
500 -- called after verifying that the generic unit is legal for the instance,
501 -- The procedure also examines whether the generic unit is a predefined
502 -- unit, in order to set configuration switches accordingly. As a result
503 -- the procedure must be called after analyzing and freezing the actuals.
505 procedure Set_Instance_Of (A : Entity_Id; B : Entity_Id);
506 -- Associate analyzed generic parameter with corresponding
507 -- instance. Used for semantic checks at instantiation time.
509 function Has_Been_Exchanged (E : Entity_Id) return Boolean;
510 -- Traverse the Exchanged_Views list to see if a type was private
511 -- and has already been flipped during this phase of instantiation.
513 procedure Hide_Current_Scope;
514 -- When instantiating a generic child unit, the parent context must be
515 -- present, but the instance and all entities that may be generated
516 -- must be inserted in the current scope. We leave the current scope
517 -- on the stack, but make its entities invisible to avoid visibility
518 -- problems. This is reversed at the end of the instantiation. This is
519 -- not done for the instantiation of the bodies, which only require the
520 -- instances of the generic parents to be in scope.
522 procedure Install_Body
527 -- If the instantiation happens textually before the body of the generic,
528 -- the instantiation of the body must be analyzed after the generic body,
529 -- and not at the point of instantiation. Such early instantiations can
530 -- happen if the generic and the instance appear in a package declaration
531 -- because the generic body can only appear in the corresponding package
532 -- body. Early instantiations can also appear if generic, instance and
533 -- body are all in the declarative part of a subprogram or entry. Entities
534 -- of packages that are early instantiations are delayed, and their freeze
535 -- node appears after the generic body.
537 procedure Insert_Freeze_Node_For_Instance
540 -- N denotes a package or a subprogram instantiation and F_Node is the
541 -- associated freeze node. Insert the freeze node before the first source
542 -- body which follows immediately after N. If no such body is found, the
543 -- freeze node is inserted at the end of the declarative region which
546 procedure Freeze_Subprogram_Body
547 (Inst_Node : Node_Id;
549 Pack_Id : Entity_Id);
550 -- The generic body may appear textually after the instance, including
551 -- in the proper body of a stub, or within a different package instance.
552 -- Given that the instance can only be elaborated after the generic, we
553 -- place freeze_nodes for the instance and/or for packages that may enclose
554 -- the instance and the generic, so that the back-end can establish the
555 -- proper order of elaboration.
558 -- Establish environment for subsequent instantiation. Separated from
559 -- Save_Env because data-structures for visibility handling must be
560 -- initialized before call to Check_Generic_Child_Unit.
562 procedure Install_Formal_Packages (Par : Entity_Id);
563 -- Install the visible part of any formal of the parent that is a formal
564 -- package. Note that for the case of a formal package with a box, this
565 -- includes the formal part of the formal package (12.7(10/2)).
567 procedure Install_Parent (P : Entity_Id; In_Body : Boolean := False);
568 -- When compiling an instance of a child unit the parent (which is
569 -- itself an instance) is an enclosing scope that must be made
570 -- immediately visible. This procedure is also used to install the non-
571 -- generic parent of a generic child unit when compiling its body, so
572 -- that full views of types in the parent are made visible.
574 procedure Remove_Parent (In_Body : Boolean := False);
575 -- Reverse effect after instantiation of child is complete
577 procedure Install_Hidden_Primitives
578 (Prims_List : in out Elist_Id;
581 -- Remove suffix 'P' from hidden primitives of Act_T to match the
582 -- visibility of primitives of Gen_T. The list of primitives to which
583 -- the suffix is removed is added to Prims_List to restore them later.
585 procedure Restore_Hidden_Primitives (Prims_List : in out Elist_Id);
586 -- Restore suffix 'P' to primitives of Prims_List and leave Prims_List
589 procedure Inline_Instance_Body
591 Gen_Unit : Entity_Id;
593 -- If front-end inlining is requested, instantiate the package body,
594 -- and preserve the visibility of its compilation unit, to insure
595 -- that successive instantiations succeed.
597 -- The functions Instantiate_XXX perform various legality checks and build
598 -- the declarations for instantiated generic parameters. In all of these
599 -- Formal is the entity in the generic unit, Actual is the entity of
600 -- expression in the generic associations, and Analyzed_Formal is the
601 -- formal in the generic copy, which contains the semantic information to
602 -- be used to validate the actual.
604 function Instantiate_Object
607 Analyzed_Formal : Node_Id) return List_Id;
609 function Instantiate_Type
612 Analyzed_Formal : Node_Id;
613 Actual_Decls : List_Id) return List_Id;
615 function Instantiate_Formal_Subprogram
618 Analyzed_Formal : Node_Id) return Node_Id;
620 function Instantiate_Formal_Package
623 Analyzed_Formal : Node_Id) return List_Id;
624 -- If the formal package is declared with a box, special visibility rules
625 -- apply to its formals: they are in the visible part of the package. This
626 -- is true in the declarative region of the formal package, that is to say
627 -- in the enclosing generic or instantiation. For an instantiation, the
628 -- parameters of the formal package are made visible in an explicit step.
629 -- Furthermore, if the actual has a visible USE clause, these formals must
630 -- be made potentially use-visible as well. On exit from the enclosing
631 -- instantiation, the reverse must be done.
633 -- For a formal package declared without a box, there are conformance rules
634 -- that apply to the actuals in the generic declaration and the actuals of
635 -- the actual package in the enclosing instantiation. The simplest way to
636 -- apply these rules is to repeat the instantiation of the formal package
637 -- in the context of the enclosing instance, and compare the generic
638 -- associations of this instantiation with those of the actual package.
639 -- This internal instantiation only needs to contain the renamings of the
640 -- formals: the visible and private declarations themselves need not be
643 -- In Ada 2005, the formal package may be only partially parameterized.
644 -- In that case the visibility step must make visible those actuals whose
645 -- corresponding formals were given with a box. A final complication
646 -- involves inherited operations from formal derived types, which must
647 -- be visible if the type is.
649 function Is_In_Main_Unit (N : Node_Id) return Boolean;
650 -- Test if given node is in the main unit
652 procedure Load_Parent_Of_Generic
655 Body_Optional : Boolean := False);
656 -- If the generic appears in a separate non-generic library unit, load the
657 -- corresponding body to retrieve the body of the generic. N is the node
658 -- for the generic instantiation, Spec is the generic package declaration.
660 -- Body_Optional is a flag that indicates that the body is being loaded to
661 -- ensure that temporaries are generated consistently when there are other
662 -- instances in the current declarative part that precede the one being
663 -- loaded. In that case a missing body is acceptable.
665 procedure Inherit_Context (Gen_Decl : Node_Id; Inst : Node_Id);
666 -- Add the context clause of the unit containing a generic unit to a
667 -- compilation unit that is, or contains, an instantiation.
669 function Get_Associated_Node (N : Node_Id) return Node_Id;
670 -- In order to propagate semantic information back from the analyzed copy
671 -- to the original generic, we maintain links between selected nodes in the
672 -- generic and their corresponding copies. At the end of generic analysis,
673 -- the routine Save_Global_References traverses the generic tree, examines
674 -- the semantic information, and preserves the links to those nodes that
675 -- contain global information. At instantiation, the information from the
676 -- associated node is placed on the new copy, so that name resolution is
679 -- Three kinds of source nodes have associated nodes:
681 -- a) those that can reference (denote) entities, that is identifiers,
682 -- character literals, expanded_names, operator symbols, operators,
683 -- and attribute reference nodes. These nodes have an Entity field
684 -- and are the set of nodes that are in N_Has_Entity.
686 -- b) aggregates (N_Aggregate and N_Extension_Aggregate)
688 -- c) selected components (N_Selected_Component)
690 -- For the first class, the associated node preserves the entity if it is
691 -- global. If the generic contains nested instantiations, the associated
692 -- node itself has been recopied, and a chain of them must be followed.
694 -- For aggregates, the associated node allows retrieval of the type, which
695 -- may otherwise not appear in the generic. The view of this type may be
696 -- different between generic and instantiation, and the full view can be
697 -- installed before the instantiation is analyzed. For aggregates of type
698 -- extensions, the same view exchange may have to be performed for some of
699 -- the ancestor types, if their view is private at the point of
702 -- Nodes that are selected components in the parse tree may be rewritten
703 -- as expanded names after resolution, and must be treated as potential
704 -- entity holders, which is why they also have an Associated_Node.
706 -- Nodes that do not come from source, such as freeze nodes, do not appear
707 -- in the generic tree, and need not have an associated node.
709 -- The associated node is stored in the Associated_Node field. Note that
710 -- this field overlaps Entity, which is fine, because the whole point is
711 -- that we don't need or want the normal Entity field in this situation.
713 procedure Map_Formal_Package_Entities (Form : Entity_Id; Act : Entity_Id);
714 -- Within the generic part, entities in the formal package are
715 -- visible. To validate subsequent type declarations, indicate
716 -- the correspondence between the entities in the analyzed formal,
717 -- and the entities in the actual package. There are three packages
718 -- involved in the instantiation of a formal package: the parent
719 -- generic P1 which appears in the generic declaration, the fake
720 -- instantiation P2 which appears in the analyzed generic, and whose
721 -- visible entities may be used in subsequent formals, and the actual
722 -- P3 in the instance. To validate subsequent formals, me indicate
723 -- that the entities in P2 are mapped into those of P3. The mapping of
724 -- entities has to be done recursively for nested packages.
726 procedure Move_Freeze_Nodes
730 -- Freeze nodes can be generated in the analysis of a generic unit, but
731 -- will not be seen by the back-end. It is necessary to move those nodes
732 -- to the enclosing scope if they freeze an outer entity. We place them
733 -- at the end of the enclosing generic package, which is semantically
736 procedure Preanalyze_Actuals (N : Node_Id);
737 -- Analyze actuals to perform name resolution. Full resolution is done
738 -- later, when the expected types are known, but names have to be captured
739 -- before installing parents of generics, that are not visible for the
740 -- actuals themselves.
742 function True_Parent (N : Node_Id) return Node_Id;
743 -- For a subunit, return parent of corresponding stub, else return
746 procedure Valid_Default_Attribute (Nam : Entity_Id; Def : Node_Id);
747 -- Verify that an attribute that appears as the default for a formal
748 -- subprogram is a function or procedure with the correct profile.
750 -------------------------------------------
751 -- Data Structures for Generic Renamings --
752 -------------------------------------------
754 -- The map Generic_Renamings associates generic entities with their
755 -- corresponding actuals. Currently used to validate type instances. It
756 -- will eventually be used for all generic parameters to eliminate the
757 -- need for overload resolution in the instance.
759 type Assoc_Ptr is new Int;
761 Assoc_Null : constant Assoc_Ptr := -1;
766 Next_In_HTable : Assoc_Ptr;
769 package Generic_Renamings is new Table.Table
770 (Table_Component_Type => Assoc,
771 Table_Index_Type => Assoc_Ptr,
772 Table_Low_Bound => 0,
774 Table_Increment => 100,
775 Table_Name => "Generic_Renamings");
777 -- Variable to hold enclosing instantiation. When the environment is
778 -- saved for a subprogram inlining, the corresponding Act_Id is empty.
780 Current_Instantiated_Parent : Assoc := (Empty, Empty, Assoc_Null);
782 -- Hash table for associations
784 HTable_Size : constant := 37;
785 type HTable_Range is range 0 .. HTable_Size - 1;
787 procedure Set_Next_Assoc (E : Assoc_Ptr; Next : Assoc_Ptr);
788 function Next_Assoc (E : Assoc_Ptr) return Assoc_Ptr;
789 function Get_Gen_Id (E : Assoc_Ptr) return Entity_Id;
790 function Hash (F : Entity_Id) return HTable_Range;
792 package Generic_Renamings_HTable is new GNAT.HTable.Static_HTable (
793 Header_Num => HTable_Range,
795 Elmt_Ptr => Assoc_Ptr,
796 Null_Ptr => Assoc_Null,
797 Set_Next => Set_Next_Assoc,
800 Get_Key => Get_Gen_Id,
804 Exchanged_Views : Elist_Id;
805 -- This list holds the private views that have been exchanged during
806 -- instantiation to restore the visibility of the generic declaration.
807 -- (see comments above). After instantiation, the current visibility is
808 -- reestablished by means of a traversal of this list.
810 Hidden_Entities : Elist_Id;
811 -- This list holds the entities of the current scope that are removed
812 -- from immediate visibility when instantiating a child unit. Their
813 -- visibility is restored in Remove_Parent.
815 -- Because instantiations can be recursive, the following must be saved
816 -- on entry and restored on exit from an instantiation (spec or body).
817 -- This is done by the two procedures Save_Env and Restore_Env. For
818 -- package and subprogram instantiations (but not for the body instances)
819 -- the action of Save_Env is done in two steps: Init_Env is called before
820 -- Check_Generic_Child_Unit, because setting the parent instances requires
821 -- that the visibility data structures be properly initialized. Once the
822 -- generic is unit is validated, Set_Instance_Env completes Save_Env.
824 Parent_Unit_Visible : Boolean := False;
825 -- Parent_Unit_Visible is used when the generic is a child unit, and
826 -- indicates whether the ultimate parent of the generic is visible in the
827 -- instantiation environment. It is used to reset the visibility of the
828 -- parent at the end of the instantiation (see Remove_Parent).
830 Instance_Parent_Unit : Entity_Id := Empty;
831 -- This records the ultimate parent unit of an instance of a generic
832 -- child unit and is used in conjunction with Parent_Unit_Visible to
833 -- indicate the unit to which the Parent_Unit_Visible flag corresponds.
835 type Instance_Env is record
836 Instantiated_Parent : Assoc;
837 Exchanged_Views : Elist_Id;
838 Hidden_Entities : Elist_Id;
839 Current_Sem_Unit : Unit_Number_Type;
840 Parent_Unit_Visible : Boolean := False;
841 Instance_Parent_Unit : Entity_Id := Empty;
842 Switches : Config_Switches_Type;
845 package Instance_Envs is new Table.Table (
846 Table_Component_Type => Instance_Env,
847 Table_Index_Type => Int,
848 Table_Low_Bound => 0,
850 Table_Increment => 100,
851 Table_Name => "Instance_Envs");
853 procedure Restore_Private_Views
854 (Pack_Id : Entity_Id;
855 Is_Package : Boolean := True);
856 -- Restore the private views of external types, and unmark the generic
857 -- renamings of actuals, so that they become compatible subtypes again.
858 -- For subprograms, Pack_Id is the package constructed to hold the
861 procedure Switch_View (T : Entity_Id);
862 -- Switch the partial and full views of a type and its private
863 -- dependents (i.e. its subtypes and derived types).
865 ------------------------------------
866 -- Structures for Error Reporting --
867 ------------------------------------
869 Instantiation_Node : Node_Id;
870 -- Used by subprograms that validate instantiation of formal parameters
871 -- where there might be no actual on which to place the error message.
872 -- Also used to locate the instantiation node for generic subunits.
874 Instantiation_Error : exception;
875 -- When there is a semantic error in the generic parameter matching,
876 -- there is no point in continuing the instantiation, because the
877 -- number of cascaded errors is unpredictable. This exception aborts
878 -- the instantiation process altogether.
880 S_Adjustment : Sloc_Adjustment;
881 -- Offset created for each node in an instantiation, in order to keep
882 -- track of the source position of the instantiation in each of its nodes.
883 -- A subsequent semantic error or warning on a construct of the instance
884 -- points to both places: the original generic node, and the point of
885 -- instantiation. See Sinput and Sinput.L for additional details.
887 ------------------------------------------------------------
888 -- Data structure for keeping track when inside a Generic --
889 ------------------------------------------------------------
891 -- The following table is used to save values of the Inside_A_Generic
892 -- flag (see spec of Sem) when they are saved by Start_Generic.
894 package Generic_Flags is new Table.Table (
895 Table_Component_Type => Boolean,
896 Table_Index_Type => Int,
897 Table_Low_Bound => 0,
899 Table_Increment => 200,
900 Table_Name => "Generic_Flags");
902 ---------------------------
903 -- Abandon_Instantiation --
904 ---------------------------
906 procedure Abandon_Instantiation (N : Node_Id) is
908 Error_Msg_N ("\instantiation abandoned!", N);
909 raise Instantiation_Error;
910 end Abandon_Instantiation;
912 --------------------------
913 -- Analyze_Associations --
914 --------------------------
916 function Analyze_Associations
919 F_Copy : List_Id) return List_Id
921 Actuals_To_Freeze : constant Elist_Id := New_Elmt_List;
922 Assoc : constant List_Id := New_List;
923 Default_Actuals : constant Elist_Id := New_Elmt_List;
924 Gen_Unit : constant Entity_Id :=
925 Defining_Entity (Parent (F_Copy));
929 Analyzed_Formal : Node_Id;
930 First_Named : Node_Id := Empty;
934 Saved_Formal : Node_Id;
936 Default_Formals : constant List_Id := New_List;
937 -- If an Others_Choice is present, some of the formals may be defaulted.
938 -- To simplify the treatment of visibility in an instance, we introduce
939 -- individual defaults for each such formal. These defaults are
940 -- appended to the list of associations and replace the Others_Choice.
942 Found_Assoc : Node_Id;
943 -- Association for the current formal being match. Empty if there are
944 -- no remaining actuals, or if there is no named association with the
945 -- name of the formal.
947 Is_Named_Assoc : Boolean;
948 Num_Matched : Int := 0;
949 Num_Actuals : Int := 0;
951 Others_Present : Boolean := False;
952 Others_Choice : Node_Id := Empty;
953 -- In Ada 2005, indicates partial parameterization of a formal
954 -- package. As usual an other association must be last in the list.
956 function Build_Wrapper
958 Actual : Entity_Id := Empty) return Node_Id;
959 -- In GNATProve mode, create a wrapper function for actuals that are
960 -- operators, in order to propagate their contract to the renaming
961 -- declarations generated for them. If the actual is absent, this is
962 -- a formal with a default, and the name of the operator is that of the
965 procedure Check_Overloaded_Formal_Subprogram (Formal : Entity_Id);
966 -- Apply RM 12.3 (9): if a formal subprogram is overloaded, the instance
967 -- cannot have a named association for it. AI05-0025 extends this rule
968 -- to formals of formal packages by AI05-0025, and it also applies to
969 -- box-initialized formals.
971 function Has_Fully_Defined_Profile (Subp : Entity_Id) return Boolean;
972 -- Determine whether the parameter types and the return type of Subp
973 -- are fully defined at the point of instantiation.
975 function Matching_Actual
977 A_F : Entity_Id) return Node_Id;
978 -- Find actual that corresponds to a given a formal parameter. If the
979 -- actuals are positional, return the next one, if any. If the actuals
980 -- are named, scan the parameter associations to find the right one.
981 -- A_F is the corresponding entity in the analyzed generic,which is
982 -- placed on the selector name for ASIS use.
984 -- In Ada 2005, a named association may be given with a box, in which
985 -- case Matching_Actual sets Found_Assoc to the generic association,
986 -- but return Empty for the actual itself. In this case the code below
987 -- creates a corresponding declaration for the formal.
989 function Partial_Parameterization return Boolean;
990 -- Ada 2005: if no match is found for a given formal, check if the
991 -- association for it includes a box, or whether the associations
992 -- include an Others clause.
994 procedure Process_Default (F : Entity_Id);
995 -- Add a copy of the declaration of generic formal F to the list of
996 -- associations, and add an explicit box association for F if there
997 -- is none yet, and the default comes from an Others_Choice.
999 function Renames_Standard_Subprogram (Subp : Entity_Id) return Boolean;
1000 -- Determine whether Subp renames one of the subprograms defined in the
1001 -- generated package Standard.
1003 procedure Set_Analyzed_Formal;
1004 -- Find the node in the generic copy that corresponds to a given formal.
1005 -- The semantic information on this node is used to perform legality
1006 -- checks on the actuals. Because semantic analysis can introduce some
1007 -- anonymous entities or modify the declaration node itself, the
1008 -- correspondence between the two lists is not one-one. In addition to
1009 -- anonymous types, the presence a formal equality will introduce an
1010 -- implicit declaration for the corresponding inequality.
1016 function Build_Wrapper
1017 (Formal : Entity_Id;
1018 Actual : Entity_Id := Empty) return Node_Id
1020 Loc : constant Source_Ptr := Sloc (I_Node);
1021 Typ : constant Entity_Id := Etype (Formal);
1022 Is_Binary : constant Boolean :=
1023 Present (Next_Formal (First_Formal (Formal)));
1036 Op_Name := Chars (Formal);
1038 Op_Name := Chars (Actual);
1041 -- Create entities for wrapper function and its formals
1043 F1 := Make_Temporary (Loc, 'A');
1044 F2 := Make_Temporary (Loc, 'B');
1045 L := New_Occurrence_Of (F1, Loc);
1046 R := New_Occurrence_Of (F2, Loc);
1048 Func := Make_Defining_Identifier (Loc, Chars (Formal));
1049 Set_Ekind (Func, E_Function);
1050 Set_Is_Generic_Actual_Subprogram (Func);
1053 Make_Function_Specification (Loc,
1054 Defining_Unit_Name => Func,
1055 Parameter_Specifications => New_List (
1056 Make_Parameter_Specification (Loc,
1057 Defining_Identifier => F1,
1059 Make_Identifier (Loc,
1060 Chars => Chars (Etype (First_Formal (Formal)))))),
1061 Result_Definition => Make_Identifier (Loc, Chars (Typ)));
1064 Append_To (Parameter_Specifications (Spec),
1065 Make_Parameter_Specification (Loc,
1066 Defining_Identifier => F2,
1068 Make_Identifier (Loc,
1069 Chars (Etype (Next_Formal (First_Formal (Formal)))))));
1072 -- Build expression as a function call, or as an operator node
1073 -- that corresponds to the name of the actual, starting with binary
1076 if Present (Actual) and then Op_Name not in Any_Operator_Name then
1078 Make_Function_Call (Loc,
1080 New_Occurrence_Of (Entity (Actual), Loc),
1081 Parameter_Associations => New_List (L));
1084 Append_To (Parameter_Associations (Expr), R);
1089 elsif Is_Binary then
1090 if Op_Name = Name_Op_And then
1091 Expr := Make_Op_And (Loc, Left_Opnd => L, Right_Opnd => R);
1092 elsif Op_Name = Name_Op_Or then
1093 Expr := Make_Op_Or (Loc, Left_Opnd => L, Right_Opnd => R);
1094 elsif Op_Name = Name_Op_Xor then
1095 Expr := Make_Op_Xor (Loc, Left_Opnd => L, Right_Opnd => R);
1096 elsif Op_Name = Name_Op_Eq then
1097 Expr := Make_Op_Eq (Loc, Left_Opnd => L, Right_Opnd => R);
1098 elsif Op_Name = Name_Op_Ne then
1099 Expr := Make_Op_Ne (Loc, Left_Opnd => L, Right_Opnd => R);
1100 elsif Op_Name = Name_Op_Le then
1101 Expr := Make_Op_Le (Loc, Left_Opnd => L, Right_Opnd => R);
1102 elsif Op_Name = Name_Op_Gt then
1103 Expr := Make_Op_Gt (Loc, Left_Opnd => L, Right_Opnd => R);
1104 elsif Op_Name = Name_Op_Ge then
1105 Expr := Make_Op_Ge (Loc, Left_Opnd => L, Right_Opnd => R);
1106 elsif Op_Name = Name_Op_Lt then
1107 Expr := Make_Op_Lt (Loc, Left_Opnd => L, Right_Opnd => R);
1108 elsif Op_Name = Name_Op_Add then
1109 Expr := Make_Op_Add (Loc, Left_Opnd => L, Right_Opnd => R);
1110 elsif Op_Name = Name_Op_Subtract then
1111 Expr := Make_Op_Subtract (Loc, Left_Opnd => L, Right_Opnd => R);
1112 elsif Op_Name = Name_Op_Concat then
1113 Expr := Make_Op_Concat (Loc, Left_Opnd => L, Right_Opnd => R);
1114 elsif Op_Name = Name_Op_Multiply then
1115 Expr := Make_Op_Multiply (Loc, Left_Opnd => L, Right_Opnd => R);
1116 elsif Op_Name = Name_Op_Divide then
1117 Expr := Make_Op_Divide (Loc, Left_Opnd => L, Right_Opnd => R);
1118 elsif Op_Name = Name_Op_Mod then
1119 Expr := Make_Op_Mod (Loc, Left_Opnd => L, Right_Opnd => R);
1120 elsif Op_Name = Name_Op_Rem then
1121 Expr := Make_Op_Rem (Loc, Left_Opnd => L, Right_Opnd => R);
1122 elsif Op_Name = Name_Op_Expon then
1123 Expr := Make_Op_Expon (Loc, Left_Opnd => L, Right_Opnd => R);
1129 if Op_Name = Name_Op_Add then
1130 Expr := Make_Op_Plus (Loc, Right_Opnd => L);
1131 elsif Op_Name = Name_Op_Subtract then
1132 Expr := Make_Op_Minus (Loc, Right_Opnd => L);
1133 elsif Op_Name = Name_Op_Abs then
1134 Expr := Make_Op_Abs (Loc, Right_Opnd => L);
1135 elsif Op_Name = Name_Op_Not then
1136 Expr := Make_Op_Not (Loc, Right_Opnd => L);
1140 -- Propagate visible entity to operator node, either from a
1141 -- given actual or from a default.
1143 if Is_Entity_Name (Actual) and then Nkind (Expr) in N_Op then
1144 Set_Entity (Expr, Entity (Actual));
1148 Make_Expression_Function (Loc,
1149 Specification => Spec,
1150 Expression => Expr);
1155 ----------------------------------------
1156 -- Check_Overloaded_Formal_Subprogram --
1157 ----------------------------------------
1159 procedure Check_Overloaded_Formal_Subprogram (Formal : Entity_Id) is
1160 Temp_Formal : Entity_Id;
1163 Temp_Formal := First (Formals);
1164 while Present (Temp_Formal) loop
1165 if Nkind (Temp_Formal) in N_Formal_Subprogram_Declaration
1166 and then Temp_Formal /= Formal
1168 Chars (Defining_Unit_Name (Specification (Formal))) =
1169 Chars (Defining_Unit_Name (Specification (Temp_Formal)))
1171 if Present (Found_Assoc) then
1173 ("named association not allowed for overloaded formal",
1178 ("named association not allowed for overloaded formal",
1182 Abandon_Instantiation (Instantiation_Node);
1187 end Check_Overloaded_Formal_Subprogram;
1189 -------------------------------
1190 -- Has_Fully_Defined_Profile --
1191 -------------------------------
1193 function Has_Fully_Defined_Profile (Subp : Entity_Id) return Boolean is
1194 function Is_Fully_Defined_Type (Typ : Entity_Id) return Boolean;
1195 -- Determine whethet type Typ is fully defined
1197 ---------------------------
1198 -- Is_Fully_Defined_Type --
1199 ---------------------------
1201 function Is_Fully_Defined_Type (Typ : Entity_Id) return Boolean is
1203 -- A private type without a full view is not fully defined
1205 if Is_Private_Type (Typ)
1206 and then No (Full_View (Typ))
1210 -- An incomplete type is never fully defined
1212 elsif Is_Incomplete_Type (Typ) then
1215 -- All other types are fully defined
1220 end Is_Fully_Defined_Type;
1222 -- Local declarations
1226 -- Start of processing for Has_Fully_Defined_Profile
1229 -- Check the parameters
1231 Param := First_Formal (Subp);
1232 while Present (Param) loop
1233 if not Is_Fully_Defined_Type (Etype (Param)) then
1237 Next_Formal (Param);
1240 -- Check the return type
1242 return Is_Fully_Defined_Type (Etype (Subp));
1243 end Has_Fully_Defined_Profile;
1245 ---------------------
1246 -- Matching_Actual --
1247 ---------------------
1249 function Matching_Actual
1251 A_F : Entity_Id) return Node_Id
1257 Is_Named_Assoc := False;
1259 -- End of list of purely positional parameters
1261 if No (Actual) or else Nkind (Actual) = N_Others_Choice then
1262 Found_Assoc := Empty;
1265 -- Case of positional parameter corresponding to current formal
1267 elsif No (Selector_Name (Actual)) then
1268 Found_Assoc := Actual;
1269 Act := Explicit_Generic_Actual_Parameter (Actual);
1270 Num_Matched := Num_Matched + 1;
1273 -- Otherwise scan list of named actuals to find the one with the
1274 -- desired name. All remaining actuals have explicit names.
1277 Is_Named_Assoc := True;
1278 Found_Assoc := Empty;
1282 while Present (Actual) loop
1283 if Chars (Selector_Name (Actual)) = Chars (F) then
1284 Set_Entity (Selector_Name (Actual), A_F);
1285 Set_Etype (Selector_Name (Actual), Etype (A_F));
1286 Generate_Reference (A_F, Selector_Name (Actual));
1287 Found_Assoc := Actual;
1288 Act := Explicit_Generic_Actual_Parameter (Actual);
1289 Num_Matched := Num_Matched + 1;
1297 -- Reset for subsequent searches. In most cases the named
1298 -- associations are in order. If they are not, we reorder them
1299 -- to avoid scanning twice the same actual. This is not just a
1300 -- question of efficiency: there may be multiple defaults with
1301 -- boxes that have the same name. In a nested instantiation we
1302 -- insert actuals for those defaults, and cannot rely on their
1303 -- names to disambiguate them.
1305 if Actual = First_Named then
1308 elsif Present (Actual) then
1309 Insert_Before (First_Named, Remove_Next (Prev));
1312 Actual := First_Named;
1315 if Is_Entity_Name (Act) and then Present (Entity (Act)) then
1316 Set_Used_As_Generic_Actual (Entity (Act));
1320 end Matching_Actual;
1322 ------------------------------
1323 -- Partial_Parameterization --
1324 ------------------------------
1326 function Partial_Parameterization return Boolean is
1328 return Others_Present
1329 or else (Present (Found_Assoc) and then Box_Present (Found_Assoc));
1330 end Partial_Parameterization;
1332 ---------------------
1333 -- Process_Default --
1334 ---------------------
1336 procedure Process_Default (F : Entity_Id) is
1337 Loc : constant Source_Ptr := Sloc (I_Node);
1338 F_Id : constant Entity_Id := Defining_Entity (F);
1344 -- Append copy of formal declaration to associations, and create new
1345 -- defining identifier for it.
1347 Decl := New_Copy_Tree (F);
1348 Id := Make_Defining_Identifier (Sloc (F_Id), Chars (F_Id));
1350 if Nkind (F) in N_Formal_Subprogram_Declaration then
1351 Set_Defining_Unit_Name (Specification (Decl), Id);
1354 Set_Defining_Identifier (Decl, Id);
1357 Append (Decl, Assoc);
1359 if No (Found_Assoc) then
1361 Make_Generic_Association (Loc,
1362 Selector_Name => New_Occurrence_Of (Id, Loc),
1363 Explicit_Generic_Actual_Parameter => Empty);
1364 Set_Box_Present (Default);
1365 Append (Default, Default_Formals);
1367 end Process_Default;
1369 ---------------------------------
1370 -- Renames_Standard_Subprogram --
1371 ---------------------------------
1373 function Renames_Standard_Subprogram (Subp : Entity_Id) return Boolean is
1378 while Present (Id) loop
1379 if Scope (Id) = Standard_Standard then
1387 end Renames_Standard_Subprogram;
1389 -------------------------
1390 -- Set_Analyzed_Formal --
1391 -------------------------
1393 procedure Set_Analyzed_Formal is
1397 while Present (Analyzed_Formal) loop
1398 Kind := Nkind (Analyzed_Formal);
1400 case Nkind (Formal) is
1402 when N_Formal_Subprogram_Declaration =>
1403 exit when Kind in N_Formal_Subprogram_Declaration
1406 (Defining_Unit_Name (Specification (Formal))) =
1408 (Defining_Unit_Name (Specification (Analyzed_Formal)));
1410 when N_Formal_Package_Declaration =>
1411 exit when Nkind_In (Kind, N_Formal_Package_Declaration,
1412 N_Generic_Package_Declaration,
1413 N_Package_Declaration);
1415 when N_Use_Package_Clause | N_Use_Type_Clause => exit;
1419 -- Skip freeze nodes, and nodes inserted to replace
1420 -- unrecognized pragmas.
1423 Kind not in N_Formal_Subprogram_Declaration
1424 and then not Nkind_In (Kind, N_Subprogram_Declaration,
1428 and then Chars (Defining_Identifier (Formal)) =
1429 Chars (Defining_Identifier (Analyzed_Formal));
1432 Next (Analyzed_Formal);
1434 end Set_Analyzed_Formal;
1436 -- Start of processing for Analyze_Associations
1439 Actuals := Generic_Associations (I_Node);
1441 if Present (Actuals) then
1443 -- Check for an Others choice, indicating a partial parameterization
1444 -- for a formal package.
1446 Actual := First (Actuals);
1447 while Present (Actual) loop
1448 if Nkind (Actual) = N_Others_Choice then
1449 Others_Present := True;
1450 Others_Choice := Actual;
1452 if Present (Next (Actual)) then
1453 Error_Msg_N ("others must be last association", Actual);
1456 -- This subprogram is used both for formal packages and for
1457 -- instantiations. For the latter, associations must all be
1460 if Nkind (I_Node) /= N_Formal_Package_Declaration
1461 and then Comes_From_Source (I_Node)
1464 ("others association not allowed in an instance",
1468 -- In any case, nothing to do after the others association
1472 elsif Box_Present (Actual)
1473 and then Comes_From_Source (I_Node)
1474 and then Nkind (I_Node) /= N_Formal_Package_Declaration
1477 ("box association not allowed in an instance", Actual);
1483 -- If named associations are present, save first named association
1484 -- (it may of course be Empty) to facilitate subsequent name search.
1486 First_Named := First (Actuals);
1487 while Present (First_Named)
1488 and then Nkind (First_Named) /= N_Others_Choice
1489 and then No (Selector_Name (First_Named))
1491 Num_Actuals := Num_Actuals + 1;
1496 Named := First_Named;
1497 while Present (Named) loop
1498 if Nkind (Named) /= N_Others_Choice
1499 and then No (Selector_Name (Named))
1501 Error_Msg_N ("invalid positional actual after named one", Named);
1502 Abandon_Instantiation (Named);
1505 -- A named association may lack an actual parameter, if it was
1506 -- introduced for a default subprogram that turns out to be local
1507 -- to the outer instantiation.
1509 if Nkind (Named) /= N_Others_Choice
1510 and then Present (Explicit_Generic_Actual_Parameter (Named))
1512 Num_Actuals := Num_Actuals + 1;
1518 if Present (Formals) then
1519 Formal := First_Non_Pragma (Formals);
1520 Analyzed_Formal := First_Non_Pragma (F_Copy);
1522 if Present (Actuals) then
1523 Actual := First (Actuals);
1525 -- All formals should have default values
1531 while Present (Formal) loop
1532 Set_Analyzed_Formal;
1533 Saved_Formal := Next_Non_Pragma (Formal);
1535 case Nkind (Formal) is
1536 when N_Formal_Object_Declaration =>
1539 Defining_Identifier (Formal),
1540 Defining_Identifier (Analyzed_Formal));
1542 if No (Match) and then Partial_Parameterization then
1543 Process_Default (Formal);
1546 (Instantiate_Object (Formal, Match, Analyzed_Formal),
1550 when N_Formal_Type_Declaration =>
1553 Defining_Identifier (Formal),
1554 Defining_Identifier (Analyzed_Formal));
1557 if Partial_Parameterization then
1558 Process_Default (Formal);
1561 Error_Msg_Sloc := Sloc (Gen_Unit);
1565 Defining_Identifier (Formal));
1566 Error_Msg_NE ("\in instantiation of & declared#",
1567 Instantiation_Node, Gen_Unit);
1568 Abandon_Instantiation (Instantiation_Node);
1575 (Formal, Match, Analyzed_Formal, Assoc),
1578 -- An instantiation is a freeze point for the actuals,
1579 -- unless this is a rewritten formal package, or the
1580 -- formal is an Ada 2012 formal incomplete type.
1582 if Nkind (I_Node) = N_Formal_Package_Declaration
1584 (Ada_Version >= Ada_2012
1586 Ekind (Defining_Identifier (Analyzed_Formal)) =
1592 Append_Elmt (Entity (Match), Actuals_To_Freeze);
1596 -- A remote access-to-class-wide type is not a legal actual
1597 -- for a generic formal of an access type (E.2.2(17/2)).
1598 -- In GNAT an exception to this rule is introduced when
1599 -- the formal is marked as remote using implementation
1600 -- defined aspect/pragma Remote_Access_Type. In that case
1601 -- the actual must be remote as well.
1603 -- If the current instantiation is the construction of a
1604 -- local copy for a formal package the actuals may be
1605 -- defaulted, and there is no matching actual to check.
1607 if Nkind (Analyzed_Formal) = N_Formal_Type_Declaration
1609 Nkind (Formal_Type_Definition (Analyzed_Formal)) =
1610 N_Access_To_Object_Definition
1611 and then Present (Match)
1614 Formal_Ent : constant Entity_Id :=
1615 Defining_Identifier (Analyzed_Formal);
1617 if Is_Remote_Access_To_Class_Wide_Type (Entity (Match))
1618 = Is_Remote_Types (Formal_Ent)
1620 -- Remoteness of formal and actual match
1624 elsif Is_Remote_Types (Formal_Ent) then
1626 -- Remote formal, non-remote actual
1629 ("actual for& must be remote", Match, Formal_Ent);
1632 -- Non-remote formal, remote actual
1635 ("actual for& may not be remote",
1641 when N_Formal_Subprogram_Declaration =>
1644 (Defining_Unit_Name (Specification (Formal)),
1645 Defining_Unit_Name (Specification (Analyzed_Formal)));
1647 -- If the formal subprogram has the same name as another
1648 -- formal subprogram of the generic, then a named
1649 -- association is illegal (12.3(9)). Exclude named
1650 -- associations that are generated for a nested instance.
1653 and then Is_Named_Assoc
1654 and then Comes_From_Source (Found_Assoc)
1656 Check_Overloaded_Formal_Subprogram (Formal);
1659 -- If there is no corresponding actual, this may be case
1660 -- of partial parameterization, or else the formal has a
1661 -- default or a box.
1663 if No (Match) and then Partial_Parameterization then
1664 Process_Default (Formal);
1666 if Nkind (I_Node) = N_Formal_Package_Declaration then
1667 Check_Overloaded_Formal_Subprogram (Formal);
1672 and then Ekind (Defining_Entity (Analyzed_Formal))
1676 -- If actual is an entity (function or operator),
1677 -- build wrapper for it.
1680 and then Nkind (Match) = N_Operator_Symbol
1682 -- If the name is a default, find its visible
1683 -- entity at the point of instantiation.
1685 if Is_Entity_Name (Match)
1686 and then No (Entity (Match))
1688 Find_Direct_Name (Match);
1693 (Defining_Entity (Analyzed_Formal), Match));
1695 -- Ditto if formal is an operator with a default.
1697 elsif Box_Present (Formal)
1698 and then Nkind (Defining_Entity (Analyzed_Formal))
1699 = N_Defining_Operator_Symbol
1703 (Defining_Entity (Analyzed_Formal)));
1705 -- Otherwise create renaming declaration.
1709 Instantiate_Formal_Subprogram
1710 (Formal, Match, Analyzed_Formal));
1715 Instantiate_Formal_Subprogram
1716 (Formal, Match, Analyzed_Formal));
1719 -- An instantiation is a freeze point for the actuals,
1720 -- unless this is a rewritten formal package.
1722 if Nkind (I_Node) /= N_Formal_Package_Declaration
1723 and then Nkind (Match) = N_Identifier
1724 and then Is_Subprogram (Entity (Match))
1726 -- The actual subprogram may rename a routine defined
1727 -- in Standard. Avoid freezing such renamings because
1728 -- subprograms coming from Standard cannot be frozen.
1731 not Renames_Standard_Subprogram (Entity (Match))
1733 -- If the actual subprogram comes from a different
1734 -- unit, it is already frozen, either by a body in
1735 -- that unit or by the end of the declarative part
1736 -- of the unit. This check avoids the freezing of
1737 -- subprograms defined in Standard which are used
1738 -- as generic actuals.
1740 and then In_Same_Code_Unit (Entity (Match), I_Node)
1741 and then Has_Fully_Defined_Profile (Entity (Match))
1743 -- Mark the subprogram as having a delayed freeze
1744 -- since this may be an out-of-order action.
1746 Set_Has_Delayed_Freeze (Entity (Match));
1747 Append_Elmt (Entity (Match), Actuals_To_Freeze);
1751 -- If this is a nested generic, preserve default for later
1754 if No (Match) and then Box_Present (Formal) then
1756 (Defining_Unit_Name (Specification (Last (Assoc))),
1760 when N_Formal_Package_Declaration =>
1763 Defining_Identifier (Formal),
1764 Defining_Identifier (Original_Node (Analyzed_Formal)));
1767 if Partial_Parameterization then
1768 Process_Default (Formal);
1771 Error_Msg_Sloc := Sloc (Gen_Unit);
1774 Instantiation_Node, Defining_Identifier (Formal));
1775 Error_Msg_NE ("\in instantiation of & declared#",
1776 Instantiation_Node, Gen_Unit);
1778 Abandon_Instantiation (Instantiation_Node);
1784 (Instantiate_Formal_Package
1785 (Formal, Match, Analyzed_Formal),
1789 -- For use type and use package appearing in the generic part,
1790 -- we have already copied them, so we can just move them where
1791 -- they belong (we mustn't recopy them since this would mess up
1792 -- the Sloc values).
1794 when N_Use_Package_Clause |
1795 N_Use_Type_Clause =>
1796 if Nkind (Original_Node (I_Node)) =
1797 N_Formal_Package_Declaration
1799 Append (New_Copy_Tree (Formal), Assoc);
1802 Append (Formal, Assoc);
1806 raise Program_Error;
1810 Formal := Saved_Formal;
1811 Next_Non_Pragma (Analyzed_Formal);
1814 if Num_Actuals > Num_Matched then
1815 Error_Msg_Sloc := Sloc (Gen_Unit);
1817 if Present (Selector_Name (Actual)) then
1819 ("unmatched actual&",
1820 Actual, Selector_Name (Actual));
1821 Error_Msg_NE ("\in instantiation of& declared#",
1825 ("unmatched actual in instantiation of& declared#",
1830 elsif Present (Actuals) then
1832 ("too many actuals in generic instantiation", Instantiation_Node);
1835 -- An instantiation freezes all generic actuals. The only exceptions
1836 -- to this are incomplete types and subprograms which are not fully
1837 -- defined at the point of instantiation.
1840 Elmt : Elmt_Id := First_Elmt (Actuals_To_Freeze);
1842 while Present (Elmt) loop
1843 Freeze_Before (I_Node, Node (Elmt));
1848 -- If there are default subprograms, normalize the tree by adding
1849 -- explicit associations for them. This is required if the instance
1850 -- appears within a generic.
1858 Elmt := First_Elmt (Default_Actuals);
1859 while Present (Elmt) loop
1860 if No (Actuals) then
1861 Actuals := New_List;
1862 Set_Generic_Associations (I_Node, Actuals);
1865 Subp := Node (Elmt);
1867 Make_Generic_Association (Sloc (Subp),
1868 Selector_Name => New_Occurrence_Of (Subp, Sloc (Subp)),
1869 Explicit_Generic_Actual_Parameter =>
1870 New_Occurrence_Of (Subp, Sloc (Subp)));
1871 Mark_Rewrite_Insertion (New_D);
1872 Append_To (Actuals, New_D);
1877 -- If this is a formal package, normalize the parameter list by adding
1878 -- explicit box associations for the formals that are covered by an
1881 if not Is_Empty_List (Default_Formals) then
1882 Append_List (Default_Formals, Formals);
1886 end Analyze_Associations;
1888 -------------------------------
1889 -- Analyze_Formal_Array_Type --
1890 -------------------------------
1892 procedure Analyze_Formal_Array_Type
1893 (T : in out Entity_Id;
1899 -- Treated like a non-generic array declaration, with additional
1904 if Nkind (Def) = N_Constrained_Array_Definition then
1905 DSS := First (Discrete_Subtype_Definitions (Def));
1906 while Present (DSS) loop
1907 if Nkind_In (DSS, N_Subtype_Indication,
1909 N_Attribute_Reference)
1911 Error_Msg_N ("only a subtype mark is allowed in a formal", DSS);
1918 Array_Type_Declaration (T, Def);
1919 Set_Is_Generic_Type (Base_Type (T));
1921 if Ekind (Component_Type (T)) = E_Incomplete_Type
1922 and then No (Full_View (Component_Type (T)))
1924 Error_Msg_N ("premature usage of incomplete type", Def);
1926 -- Check that range constraint is not allowed on the component type
1927 -- of a generic formal array type (AARM 12.5.3(3))
1929 elsif Is_Internal (Component_Type (T))
1930 and then Present (Subtype_Indication (Component_Definition (Def)))
1931 and then Nkind (Original_Node
1932 (Subtype_Indication (Component_Definition (Def)))) =
1933 N_Subtype_Indication
1936 ("in a formal, a subtype indication can only be "
1937 & "a subtype mark (RM 12.5.3(3))",
1938 Subtype_Indication (Component_Definition (Def)));
1941 end Analyze_Formal_Array_Type;
1943 ---------------------------------------------
1944 -- Analyze_Formal_Decimal_Fixed_Point_Type --
1945 ---------------------------------------------
1947 -- As for other generic types, we create a valid type representation with
1948 -- legal but arbitrary attributes, whose values are never considered
1949 -- static. For all scalar types we introduce an anonymous base type, with
1950 -- the same attributes. We choose the corresponding integer type to be
1951 -- Standard_Integer.
1952 -- Here and in other similar routines, the Sloc of the generated internal
1953 -- type must be the same as the sloc of the defining identifier of the
1954 -- formal type declaration, to provide proper source navigation.
1956 procedure Analyze_Formal_Decimal_Fixed_Point_Type
1960 Loc : constant Source_Ptr := Sloc (Def);
1962 Base : constant Entity_Id :=
1964 (E_Decimal_Fixed_Point_Type,
1966 Sloc (Defining_Identifier (Parent (Def))), 'G');
1968 Int_Base : constant Entity_Id := Standard_Integer;
1969 Delta_Val : constant Ureal := Ureal_1;
1970 Digs_Val : constant Uint := Uint_6;
1972 function Make_Dummy_Bound return Node_Id;
1973 -- Return a properly typed universal real literal to use as a bound
1975 ----------------------
1976 -- Make_Dummy_Bound --
1977 ----------------------
1979 function Make_Dummy_Bound return Node_Id is
1980 Bound : constant Node_Id := Make_Real_Literal (Loc, Ureal_1);
1982 Set_Etype (Bound, Universal_Real);
1984 end Make_Dummy_Bound;
1986 -- Start of processing for Analyze_Formal_Decimal_Fixed_Point_Type
1991 Set_Etype (Base, Base);
1992 Set_Size_Info (Base, Int_Base);
1993 Set_RM_Size (Base, RM_Size (Int_Base));
1994 Set_First_Rep_Item (Base, First_Rep_Item (Int_Base));
1995 Set_Digits_Value (Base, Digs_Val);
1996 Set_Delta_Value (Base, Delta_Val);
1997 Set_Small_Value (Base, Delta_Val);
1998 Set_Scalar_Range (Base,
2000 Low_Bound => Make_Dummy_Bound,
2001 High_Bound => Make_Dummy_Bound));
2003 Set_Is_Generic_Type (Base);
2004 Set_Parent (Base, Parent (Def));
2006 Set_Ekind (T, E_Decimal_Fixed_Point_Subtype);
2007 Set_Etype (T, Base);
2008 Set_Size_Info (T, Int_Base);
2009 Set_RM_Size (T, RM_Size (Int_Base));
2010 Set_First_Rep_Item (T, First_Rep_Item (Int_Base));
2011 Set_Digits_Value (T, Digs_Val);
2012 Set_Delta_Value (T, Delta_Val);
2013 Set_Small_Value (T, Delta_Val);
2014 Set_Scalar_Range (T, Scalar_Range (Base));
2015 Set_Is_Constrained (T);
2017 Check_Restriction (No_Fixed_Point, Def);
2018 end Analyze_Formal_Decimal_Fixed_Point_Type;
2020 -------------------------------------------
2021 -- Analyze_Formal_Derived_Interface_Type --
2022 -------------------------------------------
2024 procedure Analyze_Formal_Derived_Interface_Type
2029 Loc : constant Source_Ptr := Sloc (Def);
2032 -- Rewrite as a type declaration of a derived type. This ensures that
2033 -- the interface list and primitive operations are properly captured.
2036 Make_Full_Type_Declaration (Loc,
2037 Defining_Identifier => T,
2038 Type_Definition => Def));
2040 Set_Is_Generic_Type (T);
2041 end Analyze_Formal_Derived_Interface_Type;
2043 ---------------------------------
2044 -- Analyze_Formal_Derived_Type --
2045 ---------------------------------
2047 procedure Analyze_Formal_Derived_Type
2052 Loc : constant Source_Ptr := Sloc (Def);
2053 Unk_Disc : constant Boolean := Unknown_Discriminants_Present (N);
2057 Set_Is_Generic_Type (T);
2059 if Private_Present (Def) then
2061 Make_Private_Extension_Declaration (Loc,
2062 Defining_Identifier => T,
2063 Discriminant_Specifications => Discriminant_Specifications (N),
2064 Unknown_Discriminants_Present => Unk_Disc,
2065 Subtype_Indication => Subtype_Mark (Def),
2066 Interface_List => Interface_List (Def));
2068 Set_Abstract_Present (New_N, Abstract_Present (Def));
2069 Set_Limited_Present (New_N, Limited_Present (Def));
2070 Set_Synchronized_Present (New_N, Synchronized_Present (Def));
2074 Make_Full_Type_Declaration (Loc,
2075 Defining_Identifier => T,
2076 Discriminant_Specifications =>
2077 Discriminant_Specifications (Parent (T)),
2079 Make_Derived_Type_Definition (Loc,
2080 Subtype_Indication => Subtype_Mark (Def)));
2082 Set_Abstract_Present
2083 (Type_Definition (New_N), Abstract_Present (Def));
2085 (Type_Definition (New_N), Limited_Present (Def));
2092 if not Is_Composite_Type (T) then
2094 ("unknown discriminants not allowed for elementary types", N);
2096 Set_Has_Unknown_Discriminants (T);
2097 Set_Is_Constrained (T, False);
2101 -- If the parent type has a known size, so does the formal, which makes
2102 -- legal representation clauses that involve the formal.
2104 Set_Size_Known_At_Compile_Time
2105 (T, Size_Known_At_Compile_Time (Entity (Subtype_Mark (Def))));
2106 end Analyze_Formal_Derived_Type;
2108 ----------------------------------
2109 -- Analyze_Formal_Discrete_Type --
2110 ----------------------------------
2112 -- The operations defined for a discrete types are those of an enumeration
2113 -- type. The size is set to an arbitrary value, for use in analyzing the
2116 procedure Analyze_Formal_Discrete_Type (T : Entity_Id; Def : Node_Id) is
2117 Loc : constant Source_Ptr := Sloc (Def);
2121 Base : constant Entity_Id :=
2123 (E_Floating_Point_Type, Current_Scope,
2124 Sloc (Defining_Identifier (Parent (Def))), 'G');
2128 Set_Ekind (T, E_Enumeration_Subtype);
2129 Set_Etype (T, Base);
2132 Set_Is_Generic_Type (T);
2133 Set_Is_Constrained (T);
2135 -- For semantic analysis, the bounds of the type must be set to some
2136 -- non-static value. The simplest is to create attribute nodes for those
2137 -- bounds, that refer to the type itself. These bounds are never
2138 -- analyzed but serve as place-holders.
2141 Make_Attribute_Reference (Loc,
2142 Attribute_Name => Name_First,
2143 Prefix => New_Occurrence_Of (T, Loc));
2147 Make_Attribute_Reference (Loc,
2148 Attribute_Name => Name_Last,
2149 Prefix => New_Occurrence_Of (T, Loc));
2152 Set_Scalar_Range (T,
2157 Set_Ekind (Base, E_Enumeration_Type);
2158 Set_Etype (Base, Base);
2159 Init_Size (Base, 8);
2160 Init_Alignment (Base);
2161 Set_Is_Generic_Type (Base);
2162 Set_Scalar_Range (Base, Scalar_Range (T));
2163 Set_Parent (Base, Parent (Def));
2164 end Analyze_Formal_Discrete_Type;
2166 ----------------------------------
2167 -- Analyze_Formal_Floating_Type --
2168 ---------------------------------
2170 procedure Analyze_Formal_Floating_Type (T : Entity_Id; Def : Node_Id) is
2171 Base : constant Entity_Id :=
2173 (E_Floating_Point_Type, Current_Scope,
2174 Sloc (Defining_Identifier (Parent (Def))), 'G');
2177 -- The various semantic attributes are taken from the predefined type
2178 -- Float, just so that all of them are initialized. Their values are
2179 -- never used because no constant folding or expansion takes place in
2180 -- the generic itself.
2183 Set_Ekind (T, E_Floating_Point_Subtype);
2184 Set_Etype (T, Base);
2185 Set_Size_Info (T, (Standard_Float));
2186 Set_RM_Size (T, RM_Size (Standard_Float));
2187 Set_Digits_Value (T, Digits_Value (Standard_Float));
2188 Set_Scalar_Range (T, Scalar_Range (Standard_Float));
2189 Set_Is_Constrained (T);
2191 Set_Is_Generic_Type (Base);
2192 Set_Etype (Base, Base);
2193 Set_Size_Info (Base, (Standard_Float));
2194 Set_RM_Size (Base, RM_Size (Standard_Float));
2195 Set_Digits_Value (Base, Digits_Value (Standard_Float));
2196 Set_Scalar_Range (Base, Scalar_Range (Standard_Float));
2197 Set_Parent (Base, Parent (Def));
2199 Check_Restriction (No_Floating_Point, Def);
2200 end Analyze_Formal_Floating_Type;
2202 -----------------------------------
2203 -- Analyze_Formal_Interface_Type;--
2204 -----------------------------------
2206 procedure Analyze_Formal_Interface_Type
2211 Loc : constant Source_Ptr := Sloc (N);
2216 Make_Full_Type_Declaration (Loc,
2217 Defining_Identifier => T,
2218 Type_Definition => Def);
2222 Set_Is_Generic_Type (T);
2223 end Analyze_Formal_Interface_Type;
2225 ---------------------------------
2226 -- Analyze_Formal_Modular_Type --
2227 ---------------------------------
2229 procedure Analyze_Formal_Modular_Type (T : Entity_Id; Def : Node_Id) is
2231 -- Apart from their entity kind, generic modular types are treated like
2232 -- signed integer types, and have the same attributes.
2234 Analyze_Formal_Signed_Integer_Type (T, Def);
2235 Set_Ekind (T, E_Modular_Integer_Subtype);
2236 Set_Ekind (Etype (T), E_Modular_Integer_Type);
2238 end Analyze_Formal_Modular_Type;
2240 ---------------------------------------
2241 -- Analyze_Formal_Object_Declaration --
2242 ---------------------------------------
2244 procedure Analyze_Formal_Object_Declaration (N : Node_Id) is
2245 E : constant Node_Id := Default_Expression (N);
2246 Id : constant Node_Id := Defining_Identifier (N);
2253 -- Determine the mode of the formal object
2255 if Out_Present (N) then
2256 K := E_Generic_In_Out_Parameter;
2258 if not In_Present (N) then
2259 Error_Msg_N ("formal generic objects cannot have mode OUT", N);
2263 K := E_Generic_In_Parameter;
2266 if Present (Subtype_Mark (N)) then
2267 Find_Type (Subtype_Mark (N));
2268 T := Entity (Subtype_Mark (N));
2270 -- Verify that there is no redundant null exclusion
2272 if Null_Exclusion_Present (N) then
2273 if not Is_Access_Type (T) then
2275 ("null exclusion can only apply to an access type", N);
2277 elsif Can_Never_Be_Null (T) then
2279 ("`NOT NULL` not allowed (& already excludes null)",
2284 -- Ada 2005 (AI-423): Formal object with an access definition
2287 Check_Access_Definition (N);
2288 T := Access_Definition
2290 N => Access_Definition (N));
2293 if Ekind (T) = E_Incomplete_Type then
2295 Error_Node : Node_Id;
2298 if Present (Subtype_Mark (N)) then
2299 Error_Node := Subtype_Mark (N);
2301 Check_Access_Definition (N);
2302 Error_Node := Access_Definition (N);
2305 Error_Msg_N ("premature usage of incomplete type", Error_Node);
2309 if K = E_Generic_In_Parameter then
2311 -- Ada 2005 (AI-287): Limited aggregates allowed in generic formals
2313 if Ada_Version < Ada_2005 and then Is_Limited_Type (T) then
2315 ("generic formal of mode IN must not be of limited type", N);
2316 Explain_Limited_Type (T, N);
2319 if Is_Abstract_Type (T) then
2321 ("generic formal of mode IN must not be of abstract type", N);
2325 Preanalyze_Spec_Expression (E, T);
2327 if Is_Limited_Type (T) and then not OK_For_Limited_Init (T, E) then
2329 ("initialization not allowed for limited types", E);
2330 Explain_Limited_Type (T, E);
2337 -- Case of generic IN OUT parameter
2340 -- If the formal has an unconstrained type, construct its actual
2341 -- subtype, as is done for subprogram formals. In this fashion, all
2342 -- its uses can refer to specific bounds.
2347 if (Is_Array_Type (T)
2348 and then not Is_Constrained (T))
2350 (Ekind (T) = E_Record_Type
2351 and then Has_Discriminants (T))
2354 Non_Freezing_Ref : constant Node_Id :=
2355 New_Occurrence_Of (Id, Sloc (Id));
2359 -- Make sure the actual subtype doesn't generate bogus freezing
2361 Set_Must_Not_Freeze (Non_Freezing_Ref);
2362 Decl := Build_Actual_Subtype (T, Non_Freezing_Ref);
2363 Insert_Before_And_Analyze (N, Decl);
2364 Set_Actual_Subtype (Id, Defining_Identifier (Decl));
2367 Set_Actual_Subtype (Id, T);
2372 ("initialization not allowed for `IN OUT` formals", N);
2376 if Has_Aspects (N) then
2377 Analyze_Aspect_Specifications (N, Id);
2379 end Analyze_Formal_Object_Declaration;
2381 ----------------------------------------------
2382 -- Analyze_Formal_Ordinary_Fixed_Point_Type --
2383 ----------------------------------------------
2385 procedure Analyze_Formal_Ordinary_Fixed_Point_Type
2389 Loc : constant Source_Ptr := Sloc (Def);
2390 Base : constant Entity_Id :=
2392 (E_Ordinary_Fixed_Point_Type, Current_Scope,
2393 Sloc (Defining_Identifier (Parent (Def))), 'G');
2396 -- The semantic attributes are set for completeness only, their values
2397 -- will never be used, since all properties of the type are non-static.
2400 Set_Ekind (T, E_Ordinary_Fixed_Point_Subtype);
2401 Set_Etype (T, Base);
2402 Set_Size_Info (T, Standard_Integer);
2403 Set_RM_Size (T, RM_Size (Standard_Integer));
2404 Set_Small_Value (T, Ureal_1);
2405 Set_Delta_Value (T, Ureal_1);
2406 Set_Scalar_Range (T,
2408 Low_Bound => Make_Real_Literal (Loc, Ureal_1),
2409 High_Bound => Make_Real_Literal (Loc, Ureal_1)));
2410 Set_Is_Constrained (T);
2412 Set_Is_Generic_Type (Base);
2413 Set_Etype (Base, Base);
2414 Set_Size_Info (Base, Standard_Integer);
2415 Set_RM_Size (Base, RM_Size (Standard_Integer));
2416 Set_Small_Value (Base, Ureal_1);
2417 Set_Delta_Value (Base, Ureal_1);
2418 Set_Scalar_Range (Base, Scalar_Range (T));
2419 Set_Parent (Base, Parent (Def));
2421 Check_Restriction (No_Fixed_Point, Def);
2422 end Analyze_Formal_Ordinary_Fixed_Point_Type;
2424 ----------------------------------------
2425 -- Analyze_Formal_Package_Declaration --
2426 ----------------------------------------
2428 procedure Analyze_Formal_Package_Declaration (N : Node_Id) is
2429 Loc : constant Source_Ptr := Sloc (N);
2430 Pack_Id : constant Entity_Id := Defining_Identifier (N);
2432 Gen_Id : constant Node_Id := Name (N);
2434 Gen_Unit : Entity_Id;
2436 Parent_Installed : Boolean := False;
2438 Parent_Instance : Entity_Id;
2439 Renaming_In_Par : Entity_Id;
2440 Associations : Boolean := True;
2442 Vis_Prims_List : Elist_Id := No_Elist;
2443 -- List of primitives made temporarily visible in the instantiation
2444 -- to match the visibility of the formal type
2446 function Build_Local_Package return Node_Id;
2447 -- The formal package is rewritten so that its parameters are replaced
2448 -- with corresponding declarations. For parameters with bona fide
2449 -- associations these declarations are created by Analyze_Associations
2450 -- as for a regular instantiation. For boxed parameters, we preserve
2451 -- the formal declarations and analyze them, in order to introduce
2452 -- entities of the right kind in the environment of the formal.
2454 -------------------------
2455 -- Build_Local_Package --
2456 -------------------------
2458 function Build_Local_Package return Node_Id is
2460 Pack_Decl : Node_Id;
2463 -- Within the formal, the name of the generic package is a renaming
2464 -- of the formal (as for a regular instantiation).
2467 Make_Package_Declaration (Loc,
2470 (Specification (Original_Node (Gen_Decl)),
2471 Empty, Instantiating => True));
2473 Renaming := Make_Package_Renaming_Declaration (Loc,
2474 Defining_Unit_Name =>
2475 Make_Defining_Identifier (Loc, Chars (Gen_Unit)),
2476 Name => New_Occurrence_Of (Formal, Loc));
2478 if Nkind (Gen_Id) = N_Identifier
2479 and then Chars (Gen_Id) = Chars (Pack_Id)
2482 ("& is hidden within declaration of instance", Gen_Id, Gen_Unit);
2485 -- If the formal is declared with a box, or with an others choice,
2486 -- create corresponding declarations for all entities in the formal
2487 -- part, so that names with the proper types are available in the
2488 -- specification of the formal package.
2490 -- On the other hand, if there are no associations, then all the
2491 -- formals must have defaults, and this will be checked by the
2492 -- call to Analyze_Associations.
2495 or else Nkind (First (Generic_Associations (N))) = N_Others_Choice
2498 Formal_Decl : Node_Id;
2501 -- TBA : for a formal package, need to recurse ???
2506 (Generic_Formal_Declarations (Original_Node (Gen_Decl)));
2507 while Present (Formal_Decl) loop
2509 (Decls, Copy_Generic_Node (Formal_Decl, Empty, True));
2514 -- If generic associations are present, use Analyze_Associations to
2515 -- create the proper renaming declarations.
2519 Act_Tree : constant Node_Id :=
2521 (Original_Node (Gen_Decl), Empty,
2522 Instantiating => True);
2525 Generic_Renamings.Set_Last (0);
2526 Generic_Renamings_HTable.Reset;
2527 Instantiation_Node := N;
2530 Analyze_Associations
2531 (I_Node => Original_Node (N),
2532 Formals => Generic_Formal_Declarations (Act_Tree),
2533 F_Copy => Generic_Formal_Declarations (Gen_Decl));
2535 Vis_Prims_List := Check_Hidden_Primitives (Decls);
2539 Append (Renaming, To => Decls);
2541 -- Add generated declarations ahead of local declarations in
2544 if No (Visible_Declarations (Specification (Pack_Decl))) then
2545 Set_Visible_Declarations (Specification (Pack_Decl), Decls);
2548 (First (Visible_Declarations (Specification (Pack_Decl))),
2553 end Build_Local_Package;
2555 -- Start of processing for Analyze_Formal_Package_Declaration
2558 Check_Text_IO_Special_Unit (Gen_Id);
2561 Check_Generic_Child_Unit (Gen_Id, Parent_Installed);
2562 Gen_Unit := Entity (Gen_Id);
2564 -- Check for a formal package that is a package renaming
2566 if Present (Renamed_Object (Gen_Unit)) then
2568 -- Indicate that unit is used, before replacing it with renamed
2569 -- entity for use below.
2571 if In_Extended_Main_Source_Unit (N) then
2572 Set_Is_Instantiated (Gen_Unit);
2573 Generate_Reference (Gen_Unit, N);
2576 Gen_Unit := Renamed_Object (Gen_Unit);
2579 if Ekind (Gen_Unit) /= E_Generic_Package then
2580 Error_Msg_N ("expect generic package name", Gen_Id);
2584 elsif Gen_Unit = Current_Scope then
2586 ("generic package cannot be used as a formal package of itself",
2591 elsif In_Open_Scopes (Gen_Unit) then
2592 if Is_Compilation_Unit (Gen_Unit)
2593 and then Is_Child_Unit (Current_Scope)
2595 -- Special-case the error when the formal is a parent, and
2596 -- continue analysis to minimize cascaded errors.
2599 ("generic parent cannot be used as formal package "
2600 & "of a child unit",
2605 ("generic package cannot be used as a formal package "
2613 -- Check that name of formal package does not hide name of generic,
2614 -- or its leading prefix. This check must be done separately because
2615 -- the name of the generic has already been analyzed.
2618 Gen_Name : Entity_Id;
2622 while Nkind (Gen_Name) = N_Expanded_Name loop
2623 Gen_Name := Prefix (Gen_Name);
2626 if Chars (Gen_Name) = Chars (Pack_Id) then
2628 ("& is hidden within declaration of formal package",
2634 or else No (Generic_Associations (N))
2635 or else Nkind (First (Generic_Associations (N))) = N_Others_Choice
2637 Associations := False;
2640 -- If there are no generic associations, the generic parameters appear
2641 -- as local entities and are instantiated like them. We copy the generic
2642 -- package declaration as if it were an instantiation, and analyze it
2643 -- like a regular package, except that we treat the formals as
2644 -- additional visible components.
2646 Gen_Decl := Unit_Declaration_Node (Gen_Unit);
2648 if In_Extended_Main_Source_Unit (N) then
2649 Set_Is_Instantiated (Gen_Unit);
2650 Generate_Reference (Gen_Unit, N);
2653 Formal := New_Copy (Pack_Id);
2654 Create_Instantiation_Source (N, Gen_Unit, False, S_Adjustment);
2657 -- Make local generic without formals. The formals will be replaced
2658 -- with internal declarations.
2660 New_N := Build_Local_Package;
2662 -- If there are errors in the parameter list, Analyze_Associations
2663 -- raises Instantiation_Error. Patch the declaration to prevent
2664 -- further exception propagation.
2667 when Instantiation_Error =>
2669 Enter_Name (Formal);
2670 Set_Ekind (Formal, E_Variable);
2671 Set_Etype (Formal, Any_Type);
2672 Restore_Hidden_Primitives (Vis_Prims_List);
2674 if Parent_Installed then
2682 Set_Defining_Unit_Name (Specification (New_N), Formal);
2683 Set_Generic_Parent (Specification (N), Gen_Unit);
2684 Set_Instance_Env (Gen_Unit, Formal);
2685 Set_Is_Generic_Instance (Formal);
2687 Enter_Name (Formal);
2688 Set_Ekind (Formal, E_Package);
2689 Set_Etype (Formal, Standard_Void_Type);
2690 Set_Inner_Instances (Formal, New_Elmt_List);
2691 Push_Scope (Formal);
2693 if Is_Child_Unit (Gen_Unit)
2694 and then Parent_Installed
2696 -- Similarly, we have to make the name of the formal visible in the
2697 -- parent instance, to resolve properly fully qualified names that
2698 -- may appear in the generic unit. The parent instance has been
2699 -- placed on the scope stack ahead of the current scope.
2701 Parent_Instance := Scope_Stack.Table (Scope_Stack.Last - 1).Entity;
2704 Make_Defining_Identifier (Loc, Chars (Gen_Unit));
2705 Set_Ekind (Renaming_In_Par, E_Package);
2706 Set_Etype (Renaming_In_Par, Standard_Void_Type);
2707 Set_Scope (Renaming_In_Par, Parent_Instance);
2708 Set_Parent (Renaming_In_Par, Parent (Formal));
2709 Set_Renamed_Object (Renaming_In_Par, Formal);
2710 Append_Entity (Renaming_In_Par, Parent_Instance);
2713 Analyze (Specification (N));
2715 -- The formals for which associations are provided are not visible
2716 -- outside of the formal package. The others are still declared by a
2717 -- formal parameter declaration.
2719 -- If there are no associations, the only local entity to hide is the
2720 -- generated package renaming itself.
2726 E := First_Entity (Formal);
2727 while Present (E) loop
2729 and then not Is_Generic_Formal (E)
2734 if Ekind (E) = E_Package
2735 and then Renamed_Entity (E) = Formal
2745 End_Package_Scope (Formal);
2746 Restore_Hidden_Primitives (Vis_Prims_List);
2748 if Parent_Installed then
2754 -- Inside the generic unit, the formal package is a regular package, but
2755 -- no body is needed for it. Note that after instantiation, the defining
2756 -- unit name we need is in the new tree and not in the original (see
2757 -- Package_Instantiation). A generic formal package is an instance, and
2758 -- can be used as an actual for an inner instance.
2760 Set_Has_Completion (Formal, True);
2762 -- Add semantic information to the original defining identifier.
2765 Set_Ekind (Pack_Id, E_Package);
2766 Set_Etype (Pack_Id, Standard_Void_Type);
2767 Set_Scope (Pack_Id, Scope (Formal));
2768 Set_Has_Completion (Pack_Id, True);
2771 if Has_Aspects (N) then
2772 Analyze_Aspect_Specifications (N, Pack_Id);
2774 end Analyze_Formal_Package_Declaration;
2776 ---------------------------------
2777 -- Analyze_Formal_Private_Type --
2778 ---------------------------------
2780 procedure Analyze_Formal_Private_Type
2786 New_Private_Type (N, T, Def);
2788 -- Set the size to an arbitrary but legal value
2790 Set_Size_Info (T, Standard_Integer);
2791 Set_RM_Size (T, RM_Size (Standard_Integer));
2792 end Analyze_Formal_Private_Type;
2794 ------------------------------------
2795 -- Analyze_Formal_Incomplete_Type --
2796 ------------------------------------
2798 procedure Analyze_Formal_Incomplete_Type
2804 Set_Ekind (T, E_Incomplete_Type);
2806 Set_Private_Dependents (T, New_Elmt_List);
2808 if Tagged_Present (Def) then
2809 Set_Is_Tagged_Type (T);
2810 Make_Class_Wide_Type (T);
2811 Set_Direct_Primitive_Operations (T, New_Elmt_List);
2813 end Analyze_Formal_Incomplete_Type;
2815 ----------------------------------------
2816 -- Analyze_Formal_Signed_Integer_Type --
2817 ----------------------------------------
2819 procedure Analyze_Formal_Signed_Integer_Type
2823 Base : constant Entity_Id :=
2825 (E_Signed_Integer_Type,
2827 Sloc (Defining_Identifier (Parent (Def))), 'G');
2832 Set_Ekind (T, E_Signed_Integer_Subtype);
2833 Set_Etype (T, Base);
2834 Set_Size_Info (T, Standard_Integer);
2835 Set_RM_Size (T, RM_Size (Standard_Integer));
2836 Set_Scalar_Range (T, Scalar_Range (Standard_Integer));
2837 Set_Is_Constrained (T);
2839 Set_Is_Generic_Type (Base);
2840 Set_Size_Info (Base, Standard_Integer);
2841 Set_RM_Size (Base, RM_Size (Standard_Integer));
2842 Set_Etype (Base, Base);
2843 Set_Scalar_Range (Base, Scalar_Range (Standard_Integer));
2844 Set_Parent (Base, Parent (Def));
2845 end Analyze_Formal_Signed_Integer_Type;
2847 -------------------------------------------
2848 -- Analyze_Formal_Subprogram_Declaration --
2849 -------------------------------------------
2851 procedure Analyze_Formal_Subprogram_Declaration (N : Node_Id) is
2852 Spec : constant Node_Id := Specification (N);
2853 Def : constant Node_Id := Default_Name (N);
2854 Nam : constant Entity_Id := Defining_Unit_Name (Spec);
2862 if Nkind (Nam) = N_Defining_Program_Unit_Name then
2863 Error_Msg_N ("name of formal subprogram must be a direct name", Nam);
2867 Analyze_Subprogram_Declaration (N);
2868 Set_Is_Formal_Subprogram (Nam);
2869 Set_Has_Completion (Nam);
2871 if Nkind (N) = N_Formal_Abstract_Subprogram_Declaration then
2872 Set_Is_Abstract_Subprogram (Nam);
2873 Set_Is_Dispatching_Operation (Nam);
2876 Ctrl_Type : constant Entity_Id := Find_Dispatching_Type (Nam);
2878 if No (Ctrl_Type) then
2880 ("abstract formal subprogram must have a controlling type",
2883 elsif Ada_Version >= Ada_2012
2884 and then Is_Incomplete_Type (Ctrl_Type)
2887 ("controlling type of abstract formal subprogram cannot " &
2888 "be incomplete type", N, Ctrl_Type);
2891 Check_Controlling_Formals (Ctrl_Type, Nam);
2896 -- Default name is resolved at the point of instantiation
2898 if Box_Present (N) then
2901 -- Else default is bound at the point of generic declaration
2903 elsif Present (Def) then
2904 if Nkind (Def) = N_Operator_Symbol then
2905 Find_Direct_Name (Def);
2907 elsif Nkind (Def) /= N_Attribute_Reference then
2911 -- For an attribute reference, analyze the prefix and verify
2912 -- that it has the proper profile for the subprogram.
2914 Analyze (Prefix (Def));
2915 Valid_Default_Attribute (Nam, Def);
2919 -- Default name may be overloaded, in which case the interpretation
2920 -- with the correct profile must be selected, as for a renaming.
2921 -- If the definition is an indexed component, it must denote a
2922 -- member of an entry family. If it is a selected component, it
2923 -- can be a protected operation.
2925 if Etype (Def) = Any_Type then
2928 elsif Nkind (Def) = N_Selected_Component then
2929 if not Is_Overloadable (Entity (Selector_Name (Def))) then
2930 Error_Msg_N ("expect valid subprogram name as default", Def);
2933 elsif Nkind (Def) = N_Indexed_Component then
2934 if Is_Entity_Name (Prefix (Def)) then
2935 if Ekind (Entity (Prefix (Def))) /= E_Entry_Family then
2936 Error_Msg_N ("expect valid subprogram name as default", Def);
2939 elsif Nkind (Prefix (Def)) = N_Selected_Component then
2940 if Ekind (Entity (Selector_Name (Prefix (Def)))) /=
2943 Error_Msg_N ("expect valid subprogram name as default", Def);
2947 Error_Msg_N ("expect valid subprogram name as default", Def);
2951 elsif Nkind (Def) = N_Character_Literal then
2953 -- Needs some type checks: subprogram should be parameterless???
2955 Resolve (Def, (Etype (Nam)));
2957 elsif not Is_Entity_Name (Def)
2958 or else not Is_Overloadable (Entity (Def))
2960 Error_Msg_N ("expect valid subprogram name as default", Def);
2963 elsif not Is_Overloaded (Def) then
2964 Subp := Entity (Def);
2967 Error_Msg_N ("premature usage of formal subprogram", Def);
2969 elsif not Entity_Matches_Spec (Subp, Nam) then
2970 Error_Msg_N ("no visible entity matches specification", Def);
2973 -- More than one interpretation, so disambiguate as for a renaming
2978 I1 : Interp_Index := 0;
2984 Get_First_Interp (Def, I, It);
2985 while Present (It.Nam) loop
2986 if Entity_Matches_Spec (It.Nam, Nam) then
2987 if Subp /= Any_Id then
2988 It1 := Disambiguate (Def, I1, I, Etype (Subp));
2990 if It1 = No_Interp then
2991 Error_Msg_N ("ambiguous default subprogram", Def);
3004 Get_Next_Interp (I, It);
3008 if Subp /= Any_Id then
3010 -- Subprogram found, generate reference to it
3012 Set_Entity (Def, Subp);
3013 Generate_Reference (Subp, Def);
3016 Error_Msg_N ("premature usage of formal subprogram", Def);
3018 elsif Ekind (Subp) /= E_Operator then
3019 Check_Mode_Conformant (Subp, Nam);
3023 Error_Msg_N ("no visible subprogram matches specification", N);
3029 if Has_Aspects (N) then
3030 Analyze_Aspect_Specifications (N, Nam);
3033 end Analyze_Formal_Subprogram_Declaration;
3035 -------------------------------------
3036 -- Analyze_Formal_Type_Declaration --
3037 -------------------------------------
3039 procedure Analyze_Formal_Type_Declaration (N : Node_Id) is
3040 Def : constant Node_Id := Formal_Type_Definition (N);
3044 T := Defining_Identifier (N);
3046 if Present (Discriminant_Specifications (N))
3047 and then Nkind (Def) /= N_Formal_Private_Type_Definition
3050 ("discriminants not allowed for this formal type", T);
3053 -- Enter the new name, and branch to specific routine
3056 when N_Formal_Private_Type_Definition =>
3057 Analyze_Formal_Private_Type (N, T, Def);
3059 when N_Formal_Derived_Type_Definition =>
3060 Analyze_Formal_Derived_Type (N, T, Def);
3062 when N_Formal_Incomplete_Type_Definition =>
3063 Analyze_Formal_Incomplete_Type (T, Def);
3065 when N_Formal_Discrete_Type_Definition =>
3066 Analyze_Formal_Discrete_Type (T, Def);
3068 when N_Formal_Signed_Integer_Type_Definition =>
3069 Analyze_Formal_Signed_Integer_Type (T, Def);
3071 when N_Formal_Modular_Type_Definition =>
3072 Analyze_Formal_Modular_Type (T, Def);
3074 when N_Formal_Floating_Point_Definition =>
3075 Analyze_Formal_Floating_Type (T, Def);
3077 when N_Formal_Ordinary_Fixed_Point_Definition =>
3078 Analyze_Formal_Ordinary_Fixed_Point_Type (T, Def);
3080 when N_Formal_Decimal_Fixed_Point_Definition =>
3081 Analyze_Formal_Decimal_Fixed_Point_Type (T, Def);
3083 when N_Array_Type_Definition =>
3084 Analyze_Formal_Array_Type (T, Def);
3086 when N_Access_To_Object_Definition |
3087 N_Access_Function_Definition |
3088 N_Access_Procedure_Definition =>
3089 Analyze_Generic_Access_Type (T, Def);
3091 -- Ada 2005: a interface declaration is encoded as an abstract
3092 -- record declaration or a abstract type derivation.
3094 when N_Record_Definition =>
3095 Analyze_Formal_Interface_Type (N, T, Def);
3097 when N_Derived_Type_Definition =>
3098 Analyze_Formal_Derived_Interface_Type (N, T, Def);
3104 raise Program_Error;
3108 Set_Is_Generic_Type (T);
3110 if Has_Aspects (N) then
3111 Analyze_Aspect_Specifications (N, T);
3113 end Analyze_Formal_Type_Declaration;
3115 ------------------------------------
3116 -- Analyze_Function_Instantiation --
3117 ------------------------------------
3119 procedure Analyze_Function_Instantiation (N : Node_Id) is
3121 Analyze_Subprogram_Instantiation (N, E_Function);
3122 end Analyze_Function_Instantiation;
3124 ---------------------------------
3125 -- Analyze_Generic_Access_Type --
3126 ---------------------------------
3128 procedure Analyze_Generic_Access_Type (T : Entity_Id; Def : Node_Id) is
3132 if Nkind (Def) = N_Access_To_Object_Definition then
3133 Access_Type_Declaration (T, Def);
3135 if Is_Incomplete_Or_Private_Type (Designated_Type (T))
3136 and then No (Full_View (Designated_Type (T)))
3137 and then not Is_Generic_Type (Designated_Type (T))
3139 Error_Msg_N ("premature usage of incomplete type", Def);
3141 elsif not Is_Entity_Name (Subtype_Indication (Def)) then
3143 ("only a subtype mark is allowed in a formal", Def);
3147 Access_Subprogram_Declaration (T, Def);
3149 end Analyze_Generic_Access_Type;
3151 ---------------------------------
3152 -- Analyze_Generic_Formal_Part --
3153 ---------------------------------
3155 procedure Analyze_Generic_Formal_Part (N : Node_Id) is
3156 Gen_Parm_Decl : Node_Id;
3159 -- The generic formals are processed in the scope of the generic unit,
3160 -- where they are immediately visible. The scope is installed by the
3163 Gen_Parm_Decl := First (Generic_Formal_Declarations (N));
3165 while Present (Gen_Parm_Decl) loop
3166 Analyze (Gen_Parm_Decl);
3167 Next (Gen_Parm_Decl);
3170 Generate_Reference_To_Generic_Formals (Current_Scope);
3171 end Analyze_Generic_Formal_Part;
3173 ------------------------------------------
3174 -- Analyze_Generic_Package_Declaration --
3175 ------------------------------------------
3177 procedure Analyze_Generic_Package_Declaration (N : Node_Id) is
3178 Loc : constant Source_Ptr := Sloc (N);
3181 Save_Parent : Node_Id;
3183 Decls : constant List_Id :=
3184 Visible_Declarations (Specification (N));
3188 Check_SPARK_Restriction ("generic is not allowed", N);
3190 -- We introduce a renaming of the enclosing package, to have a usable
3191 -- entity as the prefix of an expanded name for a local entity of the
3192 -- form Par.P.Q, where P is the generic package. This is because a local
3193 -- entity named P may hide it, so that the usual visibility rules in
3194 -- the instance will not resolve properly.
3197 Make_Package_Renaming_Declaration (Loc,
3198 Defining_Unit_Name =>
3199 Make_Defining_Identifier (Loc,
3200 Chars => New_External_Name (Chars (Defining_Entity (N)), "GH")),
3201 Name => Make_Identifier (Loc, Chars (Defining_Entity (N))));
3203 if Present (Decls) then
3204 Decl := First (Decls);
3205 while Present (Decl)
3206 and then Nkind (Decl) = N_Pragma
3211 if Present (Decl) then
3212 Insert_Before (Decl, Renaming);
3214 Append (Renaming, Visible_Declarations (Specification (N)));
3218 Set_Visible_Declarations (Specification (N), New_List (Renaming));
3221 -- Create copy of generic unit, and save for instantiation. If the unit
3222 -- is a child unit, do not copy the specifications for the parent, which
3223 -- are not part of the generic tree.
3225 Save_Parent := Parent_Spec (N);
3226 Set_Parent_Spec (N, Empty);
3228 New_N := Copy_Generic_Node (N, Empty, Instantiating => False);
3229 Set_Parent_Spec (New_N, Save_Parent);
3232 -- Once the contents of the generic copy and the template are swapped,
3233 -- do the same for their respective aspect specifications.
3235 Exchange_Aspects (N, New_N);
3236 Id := Defining_Entity (N);
3237 Generate_Definition (Id);
3239 -- Expansion is not applied to generic units
3244 Set_Ekind (Id, E_Generic_Package);
3245 Set_Etype (Id, Standard_Void_Type);
3246 Set_Contract (Id, Make_Contract (Sloc (Id)));
3248 -- Analyze aspects now, so that generated pragmas appear in the
3249 -- declarations before building and analyzing the generic copy.
3251 if Has_Aspects (N) then
3252 Analyze_Aspect_Specifications (N, Id);
3256 Enter_Generic_Scope (Id);
3257 Set_Inner_Instances (Id, New_Elmt_List);
3259 Set_Categorization_From_Pragmas (N);
3260 Set_Is_Pure (Id, Is_Pure (Current_Scope));
3262 -- Link the declaration of the generic homonym in the generic copy to
3263 -- the package it renames, so that it is always resolved properly.
3265 Set_Generic_Homonym (Id, Defining_Unit_Name (Renaming));
3266 Set_Entity (Associated_Node (Name (Renaming)), Id);
3268 -- For a library unit, we have reconstructed the entity for the unit,
3269 -- and must reset it in the library tables.
3271 if Nkind (Parent (N)) = N_Compilation_Unit then
3272 Set_Cunit_Entity (Current_Sem_Unit, Id);
3275 Analyze_Generic_Formal_Part (N);
3277 -- After processing the generic formals, analysis proceeds as for a
3278 -- non-generic package.
3280 Analyze (Specification (N));
3282 Validate_Categorization_Dependency (N, Id);
3286 End_Package_Scope (Id);
3287 Exit_Generic_Scope (Id);
3289 if Nkind (Parent (N)) /= N_Compilation_Unit then
3290 Move_Freeze_Nodes (Id, N, Visible_Declarations (Specification (N)));
3291 Move_Freeze_Nodes (Id, N, Private_Declarations (Specification (N)));
3292 Move_Freeze_Nodes (Id, N, Generic_Formal_Declarations (N));
3295 Set_Body_Required (Parent (N), Unit_Requires_Body (Id));
3296 Validate_RT_RAT_Component (N);
3298 -- If this is a spec without a body, check that generic parameters
3301 if not Body_Required (Parent (N)) then
3302 Check_References (Id);
3305 end Analyze_Generic_Package_Declaration;
3307 --------------------------------------------
3308 -- Analyze_Generic_Subprogram_Declaration --
3309 --------------------------------------------
3311 procedure Analyze_Generic_Subprogram_Declaration (N : Node_Id) is
3316 Result_Type : Entity_Id;
3317 Save_Parent : Node_Id;
3321 Check_SPARK_Restriction ("generic is not allowed", N);
3323 -- Create copy of generic unit, and save for instantiation. If the unit
3324 -- is a child unit, do not copy the specifications for the parent, which
3325 -- are not part of the generic tree.
3327 Save_Parent := Parent_Spec (N);
3328 Set_Parent_Spec (N, Empty);
3330 New_N := Copy_Generic_Node (N, Empty, Instantiating => False);
3331 Set_Parent_Spec (New_N, Save_Parent);
3334 Check_SPARK_Mode_In_Generic (N);
3336 -- The aspect specifications are not attached to the tree, and must
3337 -- be copied and attached to the generic copy explicitly.
3339 if Present (Aspect_Specifications (New_N)) then
3341 Aspects : constant List_Id := Aspect_Specifications (N);
3343 Set_Has_Aspects (N, False);
3344 Move_Aspects (New_N, To => N);
3345 Set_Has_Aspects (Original_Node (N), False);
3346 Set_Aspect_Specifications (Original_Node (N), Aspects);
3350 Spec := Specification (N);
3351 Id := Defining_Entity (Spec);
3352 Generate_Definition (Id);
3353 Set_Contract (Id, Make_Contract (Sloc (Id)));
3355 if Nkind (Id) = N_Defining_Operator_Symbol then
3357 ("operator symbol not allowed for generic subprogram", Id);
3364 Set_Scope_Depth_Value (Id, Scope_Depth (Current_Scope) + 1);
3366 Enter_Generic_Scope (Id);
3367 Set_Inner_Instances (Id, New_Elmt_List);
3368 Set_Is_Pure (Id, Is_Pure (Current_Scope));
3370 Analyze_Generic_Formal_Part (N);
3372 Formals := Parameter_Specifications (Spec);
3374 if Present (Formals) then
3375 Process_Formals (Formals, Spec);
3378 if Nkind (Spec) = N_Function_Specification then
3379 Set_Ekind (Id, E_Generic_Function);
3381 if Nkind (Result_Definition (Spec)) = N_Access_Definition then
3382 Result_Type := Access_Definition (Spec, Result_Definition (Spec));
3383 Set_Etype (Id, Result_Type);
3385 -- Check restriction imposed by AI05-073: a generic function
3386 -- cannot return an abstract type or an access to such.
3388 -- This is a binding interpretation should it apply to earlier
3389 -- versions of Ada as well as Ada 2012???
3391 if Is_Abstract_Type (Designated_Type (Result_Type))
3392 and then Ada_Version >= Ada_2012
3394 Error_Msg_N ("generic function cannot have an access result"
3395 & " that designates an abstract type", Spec);
3399 Find_Type (Result_Definition (Spec));
3400 Typ := Entity (Result_Definition (Spec));
3402 if Is_Abstract_Type (Typ)
3403 and then Ada_Version >= Ada_2012
3406 ("generic function cannot have abstract result type", Spec);
3409 -- If a null exclusion is imposed on the result type, then create
3410 -- a null-excluding itype (an access subtype) and use it as the
3411 -- function's Etype.
3413 if Is_Access_Type (Typ)
3414 and then Null_Exclusion_Present (Spec)
3417 Create_Null_Excluding_Itype
3419 Related_Nod => Spec,
3420 Scope_Id => Defining_Unit_Name (Spec)));
3422 Set_Etype (Id, Typ);
3427 Set_Ekind (Id, E_Generic_Procedure);
3428 Set_Etype (Id, Standard_Void_Type);
3431 -- For a library unit, we have reconstructed the entity for the unit,
3432 -- and must reset it in the library tables. We also make sure that
3433 -- Body_Required is set properly in the original compilation unit node.
3435 if Nkind (Parent (N)) = N_Compilation_Unit then
3436 Set_Cunit_Entity (Current_Sem_Unit, Id);
3437 Set_Body_Required (Parent (N), Unit_Requires_Body (Id));
3440 Set_Categorization_From_Pragmas (N);
3441 Validate_Categorization_Dependency (N, Id);
3443 Save_Global_References (Original_Node (N));
3445 -- For ASIS purposes, convert any postcondition, precondition pragmas
3446 -- into aspects, if N is not a compilation unit by itself, in order to
3447 -- enable the analysis of expressions inside the corresponding PPC
3450 if ASIS_Mode and then Is_List_Member (N) then
3451 Make_Aspect_For_PPC_In_Gen_Sub_Decl (N);
3454 -- To capture global references, analyze the expressions of aspects,
3455 -- and propagate information to original tree. Note that in this case
3456 -- analysis of attributes is not delayed until the freeze point.
3458 -- It seems very hard to recreate the proper visibility of the generic
3459 -- subprogram at a later point because the analysis of an aspect may
3460 -- create pragmas after the generic copies have been made ???
3462 if Has_Aspects (N) then
3467 Aspect := First (Aspect_Specifications (N));
3468 while Present (Aspect) loop
3469 if Get_Aspect_Id (Aspect) /= Aspect_Warnings
3470 and then Present (Expression (Aspect))
3472 Analyze (Expression (Aspect));
3478 Aspect := First (Aspect_Specifications (Original_Node (N)));
3479 while Present (Aspect) loop
3480 if Present (Expression (Aspect)) then
3481 Save_Global_References (Expression (Aspect));
3491 Exit_Generic_Scope (Id);
3492 Generate_Reference_To_Formals (Id);
3494 List_Inherited_Pre_Post_Aspects (Id);
3495 end Analyze_Generic_Subprogram_Declaration;
3497 -----------------------------------
3498 -- Analyze_Package_Instantiation --
3499 -----------------------------------
3501 procedure Analyze_Package_Instantiation (N : Node_Id) is
3502 Loc : constant Source_Ptr := Sloc (N);
3503 Gen_Id : constant Node_Id := Name (N);
3506 Act_Decl_Name : Node_Id;
3507 Act_Decl_Id : Entity_Id;
3512 Gen_Unit : Entity_Id;
3514 Is_Actual_Pack : constant Boolean :=
3515 Is_Internal (Defining_Entity (N));
3517 Env_Installed : Boolean := False;
3518 Parent_Installed : Boolean := False;
3519 Renaming_List : List_Id;
3520 Unit_Renaming : Node_Id;
3521 Needs_Body : Boolean;
3522 Inline_Now : Boolean := False;
3524 Save_Style_Check : constant Boolean := Style_Check;
3525 -- Save style check mode for restore on exit
3527 procedure Delay_Descriptors (E : Entity_Id);
3528 -- Delay generation of subprogram descriptors for given entity
3530 function Might_Inline_Subp return Boolean;
3531 -- If inlining is active and the generic contains inlined subprograms,
3532 -- we instantiate the body. This may cause superfluous instantiations,
3533 -- but it is simpler than detecting the need for the body at the point
3534 -- of inlining, when the context of the instance is not available.
3536 function Must_Inline_Subp return Boolean;
3537 -- If inlining is active and the generic contains inlined subprograms,
3538 -- return True if some of the inlined subprograms must be inlined by
3541 -----------------------
3542 -- Delay_Descriptors --
3543 -----------------------
3545 procedure Delay_Descriptors (E : Entity_Id) is
3547 if not Delay_Subprogram_Descriptors (E) then
3548 Set_Delay_Subprogram_Descriptors (E);
3549 Pending_Descriptor.Append (E);
3551 end Delay_Descriptors;
3553 -----------------------
3554 -- Might_Inline_Subp --
3555 -----------------------
3557 function Might_Inline_Subp return Boolean is
3561 if not Inline_Processing_Required then
3565 E := First_Entity (Gen_Unit);
3566 while Present (E) loop
3567 if Is_Subprogram (E)
3568 and then Is_Inlined (E)
3578 end Might_Inline_Subp;
3580 ----------------------
3581 -- Must_Inline_Subp --
3582 ----------------------
3584 function Must_Inline_Subp return Boolean is
3588 if not Inline_Processing_Required then
3592 E := First_Entity (Gen_Unit);
3593 while Present (E) loop
3594 if Is_Subprogram (E)
3595 and then Is_Inlined (E)
3596 and then Must_Inline (E)
3606 end Must_Inline_Subp;
3608 -- Local declarations
3610 Vis_Prims_List : Elist_Id := No_Elist;
3611 -- List of primitives made temporarily visible in the instantiation
3612 -- to match the visibility of the formal type
3614 -- Start of processing for Analyze_Package_Instantiation
3617 Check_SPARK_Restriction ("generic is not allowed", N);
3619 -- Very first thing: check for Text_IO sp[ecial unit in case we are
3620 -- instantiating one of the children of [[Wide_]Wide_]Text_IO.
3622 Check_Text_IO_Special_Unit (Name (N));
3624 -- Make node global for error reporting
3626 Instantiation_Node := N;
3628 -- Turn off style checking in instances. If the check is enabled on the
3629 -- generic unit, a warning in an instance would just be noise. If not
3630 -- enabled on the generic, then a warning in an instance is just wrong.
3632 Style_Check := False;
3634 -- Case of instantiation of a generic package
3636 if Nkind (N) = N_Package_Instantiation then
3637 Act_Decl_Id := New_Copy (Defining_Entity (N));
3638 Set_Comes_From_Source (Act_Decl_Id, True);
3640 if Nkind (Defining_Unit_Name (N)) = N_Defining_Program_Unit_Name then
3642 Make_Defining_Program_Unit_Name (Loc,
3643 Name => New_Copy_Tree (Name (Defining_Unit_Name (N))),
3644 Defining_Identifier => Act_Decl_Id);
3646 Act_Decl_Name := Act_Decl_Id;
3649 -- Case of instantiation of a formal package
3652 Act_Decl_Id := Defining_Identifier (N);
3653 Act_Decl_Name := Act_Decl_Id;
3656 Generate_Definition (Act_Decl_Id);
3657 Preanalyze_Actuals (N);
3660 Env_Installed := True;
3662 -- Reset renaming map for formal types. The mapping is established
3663 -- when analyzing the generic associations, but some mappings are
3664 -- inherited from formal packages of parent units, and these are
3665 -- constructed when the parents are installed.
3667 Generic_Renamings.Set_Last (0);
3668 Generic_Renamings_HTable.Reset;
3670 Check_Generic_Child_Unit (Gen_Id, Parent_Installed);
3671 Gen_Unit := Entity (Gen_Id);
3673 -- Verify that it is the name of a generic package
3675 -- A visibility glitch: if the instance is a child unit and the generic
3676 -- is the generic unit of a parent instance (i.e. both the parent and
3677 -- the child units are instances of the same package) the name now
3678 -- denotes the renaming within the parent, not the intended generic
3679 -- unit. See if there is a homonym that is the desired generic. The
3680 -- renaming declaration must be visible inside the instance of the
3681 -- child, but not when analyzing the name in the instantiation itself.
3683 if Ekind (Gen_Unit) = E_Package
3684 and then Present (Renamed_Entity (Gen_Unit))
3685 and then In_Open_Scopes (Renamed_Entity (Gen_Unit))
3686 and then Is_Generic_Instance (Renamed_Entity (Gen_Unit))
3687 and then Present (Homonym (Gen_Unit))
3689 Gen_Unit := Homonym (Gen_Unit);
3692 if Etype (Gen_Unit) = Any_Type then
3696 elsif Ekind (Gen_Unit) /= E_Generic_Package then
3698 -- Ada 2005 (AI-50217): Cannot use instance in limited with_clause
3700 if From_Limited_With (Gen_Unit) then
3702 ("cannot instantiate a limited withed package", Gen_Id);
3705 ("& is not the name of a generic package", Gen_Id, Gen_Unit);
3712 if In_Extended_Main_Source_Unit (N) then
3713 Set_Is_Instantiated (Gen_Unit);
3714 Generate_Reference (Gen_Unit, N);
3716 if Present (Renamed_Object (Gen_Unit)) then
3717 Set_Is_Instantiated (Renamed_Object (Gen_Unit));
3718 Generate_Reference (Renamed_Object (Gen_Unit), N);
3722 if Nkind (Gen_Id) = N_Identifier
3723 and then Chars (Gen_Unit) = Chars (Defining_Entity (N))
3726 ("& is hidden within declaration of instance", Gen_Id, Gen_Unit);
3728 elsif Nkind (Gen_Id) = N_Expanded_Name
3729 and then Is_Child_Unit (Gen_Unit)
3730 and then Nkind (Prefix (Gen_Id)) = N_Identifier
3731 and then Chars (Act_Decl_Id) = Chars (Prefix (Gen_Id))
3734 ("& is hidden within declaration of instance ", Prefix (Gen_Id));
3737 Set_Entity (Gen_Id, Gen_Unit);
3739 -- If generic is a renaming, get original generic unit
3741 if Present (Renamed_Object (Gen_Unit))
3742 and then Ekind (Renamed_Object (Gen_Unit)) = E_Generic_Package
3744 Gen_Unit := Renamed_Object (Gen_Unit);
3747 -- Verify that there are no circular instantiations
3749 if In_Open_Scopes (Gen_Unit) then
3750 Error_Msg_NE ("instantiation of & within itself", N, Gen_Unit);
3754 elsif Contains_Instance_Of (Gen_Unit, Current_Scope, Gen_Id) then
3755 Error_Msg_Node_2 := Current_Scope;
3757 ("circular Instantiation: & instantiated in &!", N, Gen_Unit);
3758 Circularity_Detected := True;
3763 Gen_Decl := Unit_Declaration_Node (Gen_Unit);
3765 -- Initialize renamings map, for error checking, and the list that
3766 -- holds private entities whose views have changed between generic
3767 -- definition and instantiation. If this is the instance created to
3768 -- validate an actual package, the instantiation environment is that
3769 -- of the enclosing instance.
3771 Create_Instantiation_Source (N, Gen_Unit, False, S_Adjustment);
3773 -- Copy original generic tree, to produce text for instantiation
3777 (Original_Node (Gen_Decl), Empty, Instantiating => True);
3779 Act_Spec := Specification (Act_Tree);
3781 -- If this is the instance created to validate an actual package,
3782 -- only the formals matter, do not examine the package spec itself.
3784 if Is_Actual_Pack then
3785 Set_Visible_Declarations (Act_Spec, New_List);
3786 Set_Private_Declarations (Act_Spec, New_List);
3790 Analyze_Associations
3792 Formals => Generic_Formal_Declarations (Act_Tree),
3793 F_Copy => Generic_Formal_Declarations (Gen_Decl));
3795 Vis_Prims_List := Check_Hidden_Primitives (Renaming_List);
3797 Set_Instance_Env (Gen_Unit, Act_Decl_Id);
3798 Set_Defining_Unit_Name (Act_Spec, Act_Decl_Name);
3799 Set_Is_Generic_Instance (Act_Decl_Id);
3800 Set_Generic_Parent (Act_Spec, Gen_Unit);
3802 -- References to the generic in its own declaration or its body are
3803 -- references to the instance. Add a renaming declaration for the
3804 -- generic unit itself. This declaration, as well as the renaming
3805 -- declarations for the generic formals, must remain private to the
3806 -- unit: the formals, because this is the language semantics, and
3807 -- the unit because its use is an artifact of the implementation.
3810 Make_Package_Renaming_Declaration (Loc,
3811 Defining_Unit_Name =>
3812 Make_Defining_Identifier (Loc, Chars (Gen_Unit)),
3813 Name => New_Occurrence_Of (Act_Decl_Id, Loc));
3815 Append (Unit_Renaming, Renaming_List);
3817 -- The renaming declarations are the first local declarations of the
3820 if Is_Non_Empty_List (Visible_Declarations (Act_Spec)) then
3822 (First (Visible_Declarations (Act_Spec)), Renaming_List);
3824 Set_Visible_Declarations (Act_Spec, Renaming_List);
3828 Make_Package_Declaration (Loc,
3829 Specification => Act_Spec);
3831 -- Propagate the aspect specifications from the package declaration
3832 -- template to the instantiated version of the package declaration.
3834 if Has_Aspects (Act_Tree) then
3835 Set_Aspect_Specifications (Act_Decl,
3836 New_Copy_List_Tree (Aspect_Specifications (Act_Tree)));
3839 -- Save the instantiation node, for subsequent instantiation of the
3840 -- body, if there is one and we are generating code for the current
3841 -- unit. Mark unit as having a body (avoids premature error message).
3843 -- We instantiate the body if we are generating code, if we are
3844 -- generating cross-reference information, or if we are building
3845 -- trees for ASIS use or GNATprove use.
3848 Enclosing_Body_Present : Boolean := False;
3849 -- If the generic unit is not a compilation unit, then a body may
3850 -- be present in its parent even if none is required. We create a
3851 -- tentative pending instantiation for the body, which will be
3852 -- discarded if none is actually present.
3857 if Scope (Gen_Unit) /= Standard_Standard
3858 and then not Is_Child_Unit (Gen_Unit)
3860 Scop := Scope (Gen_Unit);
3862 while Present (Scop)
3863 and then Scop /= Standard_Standard
3865 if Unit_Requires_Body (Scop) then
3866 Enclosing_Body_Present := True;
3869 elsif In_Open_Scopes (Scop)
3870 and then In_Package_Body (Scop)
3872 Enclosing_Body_Present := True;
3876 exit when Is_Compilation_Unit (Scop);
3877 Scop := Scope (Scop);
3881 -- If front-end inlining is enabled, and this is a unit for which
3882 -- code will be generated, we instantiate the body at once.
3884 -- This is done if the instance is not the main unit, and if the
3885 -- generic is not a child unit of another generic, to avoid scope
3886 -- problems and the reinstallation of parent instances.
3889 and then (not Is_Child_Unit (Gen_Unit)
3890 or else not Is_Generic_Unit (Scope (Gen_Unit)))
3891 and then Might_Inline_Subp
3892 and then not Is_Actual_Pack
3894 if not Back_End_Inlining
3895 and then Front_End_Inlining
3896 and then (Is_In_Main_Unit (N)
3897 or else In_Main_Context (Current_Scope))
3898 and then Nkind (Parent (N)) /= N_Compilation_Unit
3902 elsif Back_End_Inlining
3903 and then Must_Inline_Subp
3904 and then (Is_In_Main_Unit (N)
3905 or else In_Main_Context (Current_Scope))
3906 and then Nkind (Parent (N)) /= N_Compilation_Unit
3910 -- In configurable_run_time mode we force the inlining of
3911 -- predefined subprograms marked Inline_Always, to minimize
3912 -- the use of the run-time library.
3914 elsif Is_Predefined_File_Name
3915 (Unit_File_Name (Get_Source_Unit (Gen_Decl)))
3916 and then Configurable_Run_Time_Mode
3917 and then Nkind (Parent (N)) /= N_Compilation_Unit
3922 -- If the current scope is itself an instance within a child
3923 -- unit, there will be duplications in the scope stack, and the
3924 -- unstacking mechanism in Inline_Instance_Body will fail.
3925 -- This loses some rare cases of optimization, and might be
3926 -- improved some day, if we can find a proper abstraction for
3927 -- "the complete compilation context" that can be saved and
3930 if Is_Generic_Instance (Current_Scope) then
3932 Curr_Unit : constant Entity_Id :=
3933 Cunit_Entity (Current_Sem_Unit);
3935 if Curr_Unit /= Current_Scope
3936 and then Is_Child_Unit (Curr_Unit)
3938 Inline_Now := False;
3945 (Unit_Requires_Body (Gen_Unit)
3946 or else Enclosing_Body_Present
3947 or else Present (Corresponding_Body (Gen_Decl)))
3948 and then (Is_In_Main_Unit (N) or else Might_Inline_Subp)
3949 and then not Is_Actual_Pack
3950 and then not Inline_Now
3951 and then (Operating_Mode = Generate_Code
3953 -- Need comment for this check ???
3955 or else (Operating_Mode = Check_Semantics
3956 and then (ASIS_Mode or GNATprove_Mode)));
3958 -- If front_end_inlining is enabled, do not instantiate body if
3959 -- within a generic context.
3961 if (Front_End_Inlining and then not Expander_Active)
3962 or else Is_Generic_Unit (Cunit_Entity (Main_Unit))
3964 Needs_Body := False;
3967 -- If the current context is generic, and the package being
3968 -- instantiated is declared within a formal package, there is no
3969 -- body to instantiate until the enclosing generic is instantiated
3970 -- and there is an actual for the formal package. If the formal
3971 -- package has parameters, we build a regular package instance for
3972 -- it, that precedes the original formal package declaration.
3974 if In_Open_Scopes (Scope (Scope (Gen_Unit))) then
3976 Decl : constant Node_Id :=
3978 (Unit_Declaration_Node (Scope (Gen_Unit)));
3980 if Nkind (Decl) = N_Formal_Package_Declaration
3981 or else (Nkind (Decl) = N_Package_Declaration
3982 and then Is_List_Member (Decl)
3983 and then Present (Next (Decl))
3985 Nkind (Next (Decl)) =
3986 N_Formal_Package_Declaration)
3988 Needs_Body := False;
3994 -- For RCI unit calling stubs, we omit the instance body if the
3995 -- instance is the RCI library unit itself.
3997 -- However there is a special case for nested instances: in this case
3998 -- we do generate the instance body, as it might be required, e.g.
3999 -- because it provides stream attributes for some type used in the
4000 -- profile of a remote subprogram. This is consistent with 12.3(12),
4001 -- which indicates that the instance body occurs at the place of the
4002 -- instantiation, and thus is part of the RCI declaration, which is
4003 -- present on all client partitions (this is E.2.3(18)).
4005 -- Note that AI12-0002 may make it illegal at some point to have
4006 -- stream attributes defined in an RCI unit, in which case this
4007 -- special case will become unnecessary. In the meantime, there
4008 -- is known application code in production that depends on this
4009 -- being possible, so we definitely cannot eliminate the body in
4010 -- the case of nested instances for the time being.
4012 -- When we generate a nested instance body, calling stubs for any
4013 -- relevant subprogram will be be inserted immediately after the
4014 -- subprogram declarations, and will take precedence over the
4015 -- subsequent (original) body. (The stub and original body will be
4016 -- complete homographs, but this is permitted in an instance).
4017 -- (Could we do better and remove the original body???)
4019 if Distribution_Stub_Mode = Generate_Caller_Stub_Body
4020 and then Comes_From_Source (N)
4021 and then Nkind (Parent (N)) = N_Compilation_Unit
4023 Needs_Body := False;
4028 -- Here is a defence against a ludicrous number of instantiations
4029 -- caused by a circular set of instantiation attempts.
4031 if Pending_Instantiations.Last > Maximum_Instantiations then
4032 Error_Msg_Uint_1 := UI_From_Int (Maximum_Instantiations);
4033 Error_Msg_N ("too many instantiations, exceeds max of^", N);
4034 Error_Msg_N ("\limit can be changed using -gnateinn switch", N);
4035 raise Unrecoverable_Error;
4038 -- Indicate that the enclosing scopes contain an instantiation,
4039 -- and that cleanup actions should be delayed until after the
4040 -- instance body is expanded.
4042 Check_Forward_Instantiation (Gen_Decl);
4043 if Nkind (N) = N_Package_Instantiation then
4045 Enclosing_Master : Entity_Id;
4048 -- Loop to search enclosing masters
4050 Enclosing_Master := Current_Scope;
4051 Scope_Loop : while Enclosing_Master /= Standard_Standard loop
4052 if Ekind (Enclosing_Master) = E_Package then
4053 if Is_Compilation_Unit (Enclosing_Master) then
4054 if In_Package_Body (Enclosing_Master) then
4056 (Body_Entity (Enclosing_Master));
4065 Enclosing_Master := Scope (Enclosing_Master);
4068 elsif Is_Generic_Unit (Enclosing_Master)
4069 or else Ekind (Enclosing_Master) = E_Void
4071 -- Cleanup actions will eventually be performed on the
4072 -- enclosing subprogram or package instance, if any.
4073 -- Enclosing scope is void in the formal part of a
4074 -- generic subprogram.
4079 if Ekind (Enclosing_Master) = E_Entry
4081 Ekind (Scope (Enclosing_Master)) = E_Protected_Type
4083 if not Expander_Active then
4087 Protected_Body_Subprogram (Enclosing_Master);
4091 Set_Delay_Cleanups (Enclosing_Master);
4093 while Ekind (Enclosing_Master) = E_Block loop
4094 Enclosing_Master := Scope (Enclosing_Master);
4097 if Is_Subprogram (Enclosing_Master) then
4098 Delay_Descriptors (Enclosing_Master);
4100 elsif Is_Task_Type (Enclosing_Master) then
4102 TBP : constant Node_Id :=
4103 Get_Task_Body_Procedure
4106 if Present (TBP) then
4107 Delay_Descriptors (TBP);
4108 Set_Delay_Cleanups (TBP);
4115 end loop Scope_Loop;
4118 -- Make entry in table
4120 Pending_Instantiations.Append
4122 Act_Decl => Act_Decl,
4123 Expander_Status => Expander_Active,
4124 Current_Sem_Unit => Current_Sem_Unit,
4125 Scope_Suppress => Scope_Suppress,
4126 Local_Suppress_Stack_Top => Local_Suppress_Stack_Top,
4127 Version => Ada_Version,
4128 Version_Pragma => Ada_Version_Pragma,
4129 Warnings => Save_Warnings,
4130 SPARK_Mode => SPARK_Mode,
4131 SPARK_Mode_Pragma => SPARK_Mode_Pragma));
4135 Set_Categorization_From_Pragmas (Act_Decl);
4137 if Parent_Installed then
4141 Set_Instance_Spec (N, Act_Decl);
4143 -- If not a compilation unit, insert the package declaration before
4144 -- the original instantiation node.
4146 if Nkind (Parent (N)) /= N_Compilation_Unit then
4147 Mark_Rewrite_Insertion (Act_Decl);
4148 Insert_Before (N, Act_Decl);
4151 -- For an instantiation that is a compilation unit, place
4152 -- declaration on current node so context is complete for analysis
4153 -- (including nested instantiations). If this is the main unit,
4154 -- the declaration eventually replaces the instantiation node.
4155 -- If the instance body is created later, it replaces the
4156 -- instance node, and the declaration is attached to it
4157 -- (see Build_Instance_Compilation_Unit_Nodes).
4160 if Cunit_Entity (Current_Sem_Unit) = Defining_Entity (N) then
4162 -- The entity for the current unit is the newly created one,
4163 -- and all semantic information is attached to it.
4165 Set_Cunit_Entity (Current_Sem_Unit, Act_Decl_Id);
4167 -- If this is the main unit, replace the main entity as well
4169 if Current_Sem_Unit = Main_Unit then
4170 Main_Unit_Entity := Act_Decl_Id;
4174 Set_Unit (Parent (N), Act_Decl);
4175 Set_Parent_Spec (Act_Decl, Parent_Spec (N));
4176 Set_Package_Instantiation (Act_Decl_Id, N);
4178 -- Process aspect specifications of the instance node, if any, to
4179 -- take into account categorization pragmas before analyzing the
4182 if Has_Aspects (N) then
4183 Analyze_Aspect_Specifications (N, Act_Decl_Id);
4187 Set_Unit (Parent (N), N);
4188 Set_Body_Required (Parent (N), False);
4190 -- We never need elaboration checks on instantiations, since by
4191 -- definition, the body instantiation is elaborated at the same
4192 -- time as the spec instantiation.
4194 Set_Suppress_Elaboration_Warnings (Act_Decl_Id);
4195 Set_Kill_Elaboration_Checks (Act_Decl_Id);
4198 Check_Elab_Instantiation (N);
4200 if ABE_Is_Certain (N) and then Needs_Body then
4201 Pending_Instantiations.Decrement_Last;
4204 Check_Hidden_Child_Unit (N, Gen_Unit, Act_Decl_Id);
4206 Set_First_Private_Entity (Defining_Unit_Name (Unit_Renaming),
4207 First_Private_Entity (Act_Decl_Id));
4209 -- If the instantiation will receive a body, the unit will be
4210 -- transformed into a package body, and receive its own elaboration
4211 -- entity. Otherwise, the nature of the unit is now a package
4214 if Nkind (Parent (N)) = N_Compilation_Unit
4215 and then not Needs_Body
4217 Rewrite (N, Act_Decl);
4220 if Present (Corresponding_Body (Gen_Decl))
4221 or else Unit_Requires_Body (Gen_Unit)
4223 Set_Has_Completion (Act_Decl_Id);
4226 Check_Formal_Packages (Act_Decl_Id);
4228 Restore_Hidden_Primitives (Vis_Prims_List);
4229 Restore_Private_Views (Act_Decl_Id);
4231 Inherit_Context (Gen_Decl, N);
4233 if Parent_Installed then
4238 Env_Installed := False;
4241 Validate_Categorization_Dependency (N, Act_Decl_Id);
4243 -- There used to be a check here to prevent instantiations in local
4244 -- contexts if the No_Local_Allocators restriction was active. This
4245 -- check was removed by a binding interpretation in AI-95-00130/07,
4246 -- but we retain the code for documentation purposes.
4248 -- if Ekind (Act_Decl_Id) /= E_Void
4249 -- and then not Is_Library_Level_Entity (Act_Decl_Id)
4251 -- Check_Restriction (No_Local_Allocators, N);
4255 Inline_Instance_Body (N, Gen_Unit, Act_Decl);
4258 -- The following is a tree patch for ASIS: ASIS needs separate nodes to
4259 -- be used as defining identifiers for a formal package and for the
4260 -- corresponding expanded package.
4262 if Nkind (N) = N_Formal_Package_Declaration then
4263 Act_Decl_Id := New_Copy (Defining_Entity (N));
4264 Set_Comes_From_Source (Act_Decl_Id, True);
4265 Set_Is_Generic_Instance (Act_Decl_Id, False);
4266 Set_Defining_Identifier (N, Act_Decl_Id);
4269 Style_Check := Save_Style_Check;
4271 -- Check that if N is an instantiation of System.Dim_Float_IO or
4272 -- System.Dim_Integer_IO, the formal type has a dimension system.
4274 if Nkind (N) = N_Package_Instantiation
4275 and then Is_Dim_IO_Package_Instantiation (N)
4278 Assoc : constant Node_Id := First (Generic_Associations (N));
4280 if not Has_Dimension_System
4281 (Etype (Explicit_Generic_Actual_Parameter (Assoc)))
4283 Error_Msg_N ("type with a dimension system expected", Assoc);
4289 if Has_Aspects (N) and then Nkind (Parent (N)) /= N_Compilation_Unit then
4290 Analyze_Aspect_Specifications (N, Act_Decl_Id);
4294 when Instantiation_Error =>
4295 if Parent_Installed then
4299 if Env_Installed then
4303 Style_Check := Save_Style_Check;
4304 end Analyze_Package_Instantiation;
4306 --------------------------
4307 -- Inline_Instance_Body --
4308 --------------------------
4310 procedure Inline_Instance_Body
4312 Gen_Unit : Entity_Id;
4316 Gen_Comp : constant Entity_Id :=
4317 Cunit_Entity (Get_Source_Unit (Gen_Unit));
4318 Curr_Comp : constant Node_Id := Cunit (Current_Sem_Unit);
4319 Curr_Scope : Entity_Id := Empty;
4320 Curr_Unit : constant Entity_Id := Cunit_Entity (Current_Sem_Unit);
4321 Removed : Boolean := False;
4322 Num_Scopes : Int := 0;
4324 Scope_Stack_Depth : constant Int :=
4325 Scope_Stack.Last - Scope_Stack.First + 1;
4327 Use_Clauses : array (1 .. Scope_Stack_Depth) of Node_Id;
4328 Instances : array (1 .. Scope_Stack_Depth) of Entity_Id;
4329 Inner_Scopes : array (1 .. Scope_Stack_Depth) of Entity_Id;
4331 Num_Inner : Int := 0;
4332 N_Instances : Int := 0;
4336 -- Case of generic unit defined in another unit. We must remove the
4337 -- complete context of the current unit to install that of the generic.
4339 if Gen_Comp /= Cunit_Entity (Current_Sem_Unit) then
4341 -- Add some comments for the following two loops ???
4344 while Present (S) and then S /= Standard_Standard loop
4346 Num_Scopes := Num_Scopes + 1;
4348 Use_Clauses (Num_Scopes) :=
4350 (Scope_Stack.Last - Num_Scopes + 1).
4352 End_Use_Clauses (Use_Clauses (Num_Scopes));
4354 exit when Scope_Stack.Last - Num_Scopes + 1 = Scope_Stack.First
4355 or else Scope_Stack.Table
4356 (Scope_Stack.Last - Num_Scopes).Entity
4360 exit when Is_Generic_Instance (S)
4361 and then (In_Package_Body (S)
4362 or else Ekind (S) = E_Procedure
4363 or else Ekind (S) = E_Function);
4367 Vis := Is_Immediately_Visible (Gen_Comp);
4369 -- Find and save all enclosing instances
4374 and then S /= Standard_Standard
4376 if Is_Generic_Instance (S) then
4377 N_Instances := N_Instances + 1;
4378 Instances (N_Instances) := S;
4380 exit when In_Package_Body (S);
4386 -- Remove context of current compilation unit, unless we are within a
4387 -- nested package instantiation, in which case the context has been
4388 -- removed previously.
4390 -- If current scope is the body of a child unit, remove context of
4391 -- spec as well. If an enclosing scope is an instance body, the
4392 -- context has already been removed, but the entities in the body
4393 -- must be made invisible as well.
4398 and then S /= Standard_Standard
4400 if Is_Generic_Instance (S)
4401 and then (In_Package_Body (S)
4402 or else Ekind (S) = E_Procedure
4403 or else Ekind (S) = E_Function)
4405 -- We still have to remove the entities of the enclosing
4406 -- instance from direct visibility.
4411 E := First_Entity (S);
4412 while Present (E) loop
4413 Set_Is_Immediately_Visible (E, False);
4422 or else (Ekind (Curr_Unit) = E_Package_Body
4423 and then S = Spec_Entity (Curr_Unit))
4424 or else (Ekind (Curr_Unit) = E_Subprogram_Body
4427 (Unit_Declaration_Node (Curr_Unit)))
4431 -- Remove entities in current scopes from visibility, so that
4432 -- instance body is compiled in a clean environment.
4434 List := Save_Scope_Stack (Handle_Use => False);
4436 if Is_Child_Unit (S) then
4438 -- Remove child unit from stack, as well as inner scopes.
4439 -- Removing the context of a child unit removes parent units
4442 while Current_Scope /= S loop
4443 Num_Inner := Num_Inner + 1;
4444 Inner_Scopes (Num_Inner) := Current_Scope;
4449 Remove_Context (Curr_Comp);
4453 Remove_Context (Curr_Comp);
4456 if Ekind (Curr_Unit) = E_Package_Body then
4457 Remove_Context (Library_Unit (Curr_Comp));
4463 pragma Assert (Num_Inner < Num_Scopes);
4465 Push_Scope (Standard_Standard);
4466 Scope_Stack.Table (Scope_Stack.Last).Is_Active_Stack_Base := True;
4467 Instantiate_Package_Body
4470 Act_Decl => Act_Decl,
4471 Expander_Status => Expander_Active,
4472 Current_Sem_Unit => Current_Sem_Unit,
4473 Scope_Suppress => Scope_Suppress,
4474 Local_Suppress_Stack_Top => Local_Suppress_Stack_Top,
4475 Version => Ada_Version,
4476 Version_Pragma => Ada_Version_Pragma,
4477 Warnings => Save_Warnings,
4478 SPARK_Mode => SPARK_Mode,
4479 SPARK_Mode_Pragma => SPARK_Mode_Pragma)),
4480 Inlined_Body => True);
4486 Set_Is_Immediately_Visible (Gen_Comp, Vis);
4488 -- Reset Generic_Instance flag so that use clauses can be installed
4489 -- in the proper order. (See Use_One_Package for effect of enclosing
4490 -- instances on processing of use clauses).
4492 for J in 1 .. N_Instances loop
4493 Set_Is_Generic_Instance (Instances (J), False);
4497 Install_Context (Curr_Comp);
4499 if Present (Curr_Scope)
4500 and then Is_Child_Unit (Curr_Scope)
4502 Push_Scope (Curr_Scope);
4503 Set_Is_Immediately_Visible (Curr_Scope);
4505 -- Finally, restore inner scopes as well
4507 for J in reverse 1 .. Num_Inner loop
4508 Push_Scope (Inner_Scopes (J));
4512 Restore_Scope_Stack (List, Handle_Use => False);
4514 if Present (Curr_Scope)
4516 (In_Private_Part (Curr_Scope)
4517 or else In_Package_Body (Curr_Scope))
4519 -- Install private declaration of ancestor units, which are
4520 -- currently available. Restore_Scope_Stack and Install_Context
4521 -- only install the visible part of parents.
4526 Par := Scope (Curr_Scope);
4527 while (Present (Par))
4528 and then Par /= Standard_Standard
4530 Install_Private_Declarations (Par);
4537 -- Restore use clauses. For a child unit, use clauses in the parents
4538 -- are restored when installing the context, so only those in inner
4539 -- scopes (and those local to the child unit itself) need to be
4540 -- installed explicitly.
4542 if Is_Child_Unit (Curr_Unit)
4545 for J in reverse 1 .. Num_Inner + 1 loop
4546 Scope_Stack.Table (Scope_Stack.Last - J + 1).First_Use_Clause :=
4548 Install_Use_Clauses (Use_Clauses (J));
4552 for J in reverse 1 .. Num_Scopes loop
4553 Scope_Stack.Table (Scope_Stack.Last - J + 1).First_Use_Clause :=
4555 Install_Use_Clauses (Use_Clauses (J));
4559 -- Restore status of instances. If one of them is a body, make its
4560 -- local entities visible again.
4567 for J in 1 .. N_Instances loop
4568 Inst := Instances (J);
4569 Set_Is_Generic_Instance (Inst, True);
4571 if In_Package_Body (Inst)
4572 or else Ekind (S) = E_Procedure
4573 or else Ekind (S) = E_Function
4575 E := First_Entity (Instances (J));
4576 while Present (E) loop
4577 Set_Is_Immediately_Visible (E);
4584 -- If generic unit is in current unit, current context is correct
4587 Instantiate_Package_Body
4590 Act_Decl => Act_Decl,
4591 Expander_Status => Expander_Active,
4592 Current_Sem_Unit => Current_Sem_Unit,
4593 Scope_Suppress => Scope_Suppress,
4594 Local_Suppress_Stack_Top => Local_Suppress_Stack_Top,
4595 Version => Ada_Version,
4596 Version_Pragma => Ada_Version_Pragma,
4597 Warnings => Save_Warnings,
4598 SPARK_Mode => SPARK_Mode,
4599 SPARK_Mode_Pragma => SPARK_Mode_Pragma)),
4600 Inlined_Body => True);
4602 end Inline_Instance_Body;
4604 -------------------------------------
4605 -- Analyze_Procedure_Instantiation --
4606 -------------------------------------
4608 procedure Analyze_Procedure_Instantiation (N : Node_Id) is
4610 Analyze_Subprogram_Instantiation (N, E_Procedure);
4611 end Analyze_Procedure_Instantiation;
4613 -----------------------------------
4614 -- Need_Subprogram_Instance_Body --
4615 -----------------------------------
4617 function Need_Subprogram_Instance_Body
4619 Subp : Entity_Id) return Boolean
4622 -- Must be inlined (or inlined renaming)
4624 if (Is_In_Main_Unit (N)
4625 or else Is_Inlined (Subp)
4626 or else Is_Inlined (Alias (Subp)))
4628 -- Must be generating code or analyzing code in ASIS/GNATprove mode
4630 and then (Operating_Mode = Generate_Code
4631 or else (Operating_Mode = Check_Semantics
4632 and then (ASIS_Mode or GNATprove_Mode)))
4634 -- The body is needed when generating code (full expansion), in ASIS
4635 -- mode for other tools, and in GNATprove mode (special expansion) for
4636 -- formal verification of the body itself.
4638 and then (Expander_Active or ASIS_Mode or GNATprove_Mode)
4640 -- No point in inlining if ABE is inevitable
4642 and then not ABE_Is_Certain (N)
4644 -- Or if subprogram is eliminated
4646 and then not Is_Eliminated (Subp)
4648 Pending_Instantiations.Append
4650 Act_Decl => Unit_Declaration_Node (Subp),
4651 Expander_Status => Expander_Active,
4652 Current_Sem_Unit => Current_Sem_Unit,
4653 Scope_Suppress => Scope_Suppress,
4654 Local_Suppress_Stack_Top => Local_Suppress_Stack_Top,
4655 Version => Ada_Version,
4656 Version_Pragma => Ada_Version_Pragma,
4657 Warnings => Save_Warnings,
4658 SPARK_Mode => SPARK_Mode,
4659 SPARK_Mode_Pragma => SPARK_Mode_Pragma));
4662 -- Here if not inlined, or we ignore the inlining
4667 end Need_Subprogram_Instance_Body;
4669 --------------------------------------
4670 -- Analyze_Subprogram_Instantiation --
4671 --------------------------------------
4673 procedure Analyze_Subprogram_Instantiation
4677 Loc : constant Source_Ptr := Sloc (N);
4678 Gen_Id : constant Node_Id := Name (N);
4680 Anon_Id : constant Entity_Id :=
4681 Make_Defining_Identifier (Sloc (Defining_Entity (N)),
4682 Chars => New_External_Name
4683 (Chars (Defining_Entity (N)), 'R'));
4685 Act_Decl_Id : Entity_Id;
4690 Env_Installed : Boolean := False;
4691 Gen_Unit : Entity_Id;
4693 Pack_Id : Entity_Id;
4694 Parent_Installed : Boolean := False;
4695 Renaming_List : List_Id;
4697 procedure Analyze_Instance_And_Renamings;
4698 -- The instance must be analyzed in a context that includes the mappings
4699 -- of generic parameters into actuals. We create a package declaration
4700 -- for this purpose, and a subprogram with an internal name within the
4701 -- package. The subprogram instance is simply an alias for the internal
4702 -- subprogram, declared in the current scope.
4704 ------------------------------------
4705 -- Analyze_Instance_And_Renamings --
4706 ------------------------------------
4708 procedure Analyze_Instance_And_Renamings is
4709 Def_Ent : constant Entity_Id := Defining_Entity (N);
4710 Pack_Decl : Node_Id;
4713 if Nkind (Parent (N)) = N_Compilation_Unit then
4715 -- For the case of a compilation unit, the container package has
4716 -- the same name as the instantiation, to insure that the binder
4717 -- calls the elaboration procedure with the right name. Copy the
4718 -- entity of the instance, which may have compilation level flags
4719 -- (e.g. Is_Child_Unit) set.
4721 Pack_Id := New_Copy (Def_Ent);
4724 -- Otherwise we use the name of the instantiation concatenated
4725 -- with its source position to ensure uniqueness if there are
4726 -- several instantiations with the same name.
4729 Make_Defining_Identifier (Loc,
4730 Chars => New_External_Name
4731 (Related_Id => Chars (Def_Ent),
4733 Suffix_Index => Source_Offset (Sloc (Def_Ent))));
4736 Pack_Decl := Make_Package_Declaration (Loc,
4737 Specification => Make_Package_Specification (Loc,
4738 Defining_Unit_Name => Pack_Id,
4739 Visible_Declarations => Renaming_List,
4740 End_Label => Empty));
4742 Set_Instance_Spec (N, Pack_Decl);
4743 Set_Is_Generic_Instance (Pack_Id);
4744 Set_Debug_Info_Needed (Pack_Id);
4746 -- Case of not a compilation unit
4748 if Nkind (Parent (N)) /= N_Compilation_Unit then
4749 Mark_Rewrite_Insertion (Pack_Decl);
4750 Insert_Before (N, Pack_Decl);
4751 Set_Has_Completion (Pack_Id);
4753 -- Case of an instantiation that is a compilation unit
4755 -- Place declaration on current node so context is complete for
4756 -- analysis (including nested instantiations), and for use in a
4757 -- context_clause (see Analyze_With_Clause).
4760 Set_Unit (Parent (N), Pack_Decl);
4761 Set_Parent_Spec (Pack_Decl, Parent_Spec (N));
4764 Analyze (Pack_Decl);
4765 Check_Formal_Packages (Pack_Id);
4766 Set_Is_Generic_Instance (Pack_Id, False);
4768 -- Why do we clear Is_Generic_Instance??? We set it 20 lines
4771 -- Body of the enclosing package is supplied when instantiating the
4772 -- subprogram body, after semantic analysis is completed.
4774 if Nkind (Parent (N)) = N_Compilation_Unit then
4776 -- Remove package itself from visibility, so it does not
4777 -- conflict with subprogram.
4779 Set_Name_Entity_Id (Chars (Pack_Id), Homonym (Pack_Id));
4781 -- Set name and scope of internal subprogram so that the proper
4782 -- external name will be generated. The proper scope is the scope
4783 -- of the wrapper package. We need to generate debugging info for
4784 -- the internal subprogram, so set flag accordingly.
4786 Set_Chars (Anon_Id, Chars (Defining_Entity (N)));
4787 Set_Scope (Anon_Id, Scope (Pack_Id));
4789 -- Mark wrapper package as referenced, to avoid spurious warnings
4790 -- if the instantiation appears in various with_ clauses of
4791 -- subunits of the main unit.
4793 Set_Referenced (Pack_Id);
4796 Set_Is_Generic_Instance (Anon_Id);
4797 Set_Debug_Info_Needed (Anon_Id);
4798 Act_Decl_Id := New_Copy (Anon_Id);
4800 Set_Parent (Act_Decl_Id, Parent (Anon_Id));
4801 Set_Chars (Act_Decl_Id, Chars (Defining_Entity (N)));
4802 Set_Sloc (Act_Decl_Id, Sloc (Defining_Entity (N)));
4803 Set_Comes_From_Source (Act_Decl_Id, True);
4805 -- The signature may involve types that are not frozen yet, but the
4806 -- subprogram will be frozen at the point the wrapper package is
4807 -- frozen, so it does not need its own freeze node. In fact, if one
4808 -- is created, it might conflict with the freezing actions from the
4811 Set_Has_Delayed_Freeze (Anon_Id, False);
4813 -- If the instance is a child unit, mark the Id accordingly. Mark
4814 -- the anonymous entity as well, which is the real subprogram and
4815 -- which is used when the instance appears in a context clause.
4816 -- Similarly, propagate the Is_Eliminated flag to handle properly
4817 -- nested eliminated subprograms.
4819 Set_Is_Child_Unit (Act_Decl_Id, Is_Child_Unit (Defining_Entity (N)));
4820 Set_Is_Child_Unit (Anon_Id, Is_Child_Unit (Defining_Entity (N)));
4821 New_Overloaded_Entity (Act_Decl_Id);
4822 Check_Eliminated (Act_Decl_Id);
4823 Set_Is_Eliminated (Anon_Id, Is_Eliminated (Act_Decl_Id));
4825 -- In compilation unit case, kill elaboration checks on the
4826 -- instantiation, since they are never needed -- the body is
4827 -- instantiated at the same point as the spec.
4829 if Nkind (Parent (N)) = N_Compilation_Unit then
4830 Set_Suppress_Elaboration_Warnings (Act_Decl_Id);
4831 Set_Kill_Elaboration_Checks (Act_Decl_Id);
4832 Set_Is_Compilation_Unit (Anon_Id);
4834 Set_Cunit_Entity (Current_Sem_Unit, Pack_Id);
4837 -- The instance is not a freezing point for the new subprogram
4839 Set_Is_Frozen (Act_Decl_Id, False);
4841 if Nkind (Defining_Entity (N)) = N_Defining_Operator_Symbol then
4842 Valid_Operator_Definition (Act_Decl_Id);
4845 Set_Alias (Act_Decl_Id, Anon_Id);
4846 Set_Parent (Act_Decl_Id, Parent (Anon_Id));
4847 Set_Has_Completion (Act_Decl_Id);
4848 Set_Related_Instance (Pack_Id, Act_Decl_Id);
4850 if Nkind (Parent (N)) = N_Compilation_Unit then
4851 Set_Body_Required (Parent (N), False);
4853 end Analyze_Instance_And_Renamings;
4857 Vis_Prims_List : Elist_Id := No_Elist;
4858 -- List of primitives made temporarily visible in the instantiation
4859 -- to match the visibility of the formal type
4861 -- Start of processing for Analyze_Subprogram_Instantiation
4864 Check_SPARK_Restriction ("generic is not allowed", N);
4866 -- Very first thing: check for special Text_IO unit in case we are
4867 -- instantiating one of the children of [[Wide_]Wide_]Text_IO. Of course
4868 -- such an instantiation is bogus (these are packages, not subprograms),
4869 -- but we get a better error message if we do this.
4871 Check_Text_IO_Special_Unit (Gen_Id);
4873 -- Make node global for error reporting
4875 Instantiation_Node := N;
4877 -- For package instantiations we turn off style checks, because they
4878 -- will have been emitted in the generic. For subprogram instantiations
4879 -- we want to apply at least the check on overriding indicators so we
4880 -- do not modify the style check status.
4882 -- The renaming declarations for the actuals do not come from source and
4883 -- will not generate spurious warnings.
4885 Preanalyze_Actuals (N);
4888 Env_Installed := True;
4889 Check_Generic_Child_Unit (Gen_Id, Parent_Installed);
4890 Gen_Unit := Entity (Gen_Id);
4892 Generate_Reference (Gen_Unit, Gen_Id);
4894 if Nkind (Gen_Id) = N_Identifier
4895 and then Chars (Gen_Unit) = Chars (Defining_Entity (N))
4898 ("& is hidden within declaration of instance", Gen_Id, Gen_Unit);
4901 if Etype (Gen_Unit) = Any_Type then
4906 -- Verify that it is a generic subprogram of the right kind, and that
4907 -- it does not lead to a circular instantiation.
4909 if K = E_Procedure and then Ekind (Gen_Unit) /= E_Generic_Procedure then
4911 ("& is not the name of a generic procedure", Gen_Id, Gen_Unit);
4913 elsif K = E_Function and then Ekind (Gen_Unit) /= E_Generic_Function then
4915 ("& is not the name of a generic function", Gen_Id, Gen_Unit);
4917 elsif In_Open_Scopes (Gen_Unit) then
4918 Error_Msg_NE ("instantiation of & within itself", N, Gen_Unit);
4921 Set_Entity (Gen_Id, Gen_Unit);
4922 Set_Is_Instantiated (Gen_Unit);
4924 if In_Extended_Main_Source_Unit (N) then
4925 Generate_Reference (Gen_Unit, N);
4928 -- If renaming, get original unit
4930 if Present (Renamed_Object (Gen_Unit))
4931 and then (Ekind (Renamed_Object (Gen_Unit)) = E_Generic_Procedure
4933 Ekind (Renamed_Object (Gen_Unit)) = E_Generic_Function)
4935 Gen_Unit := Renamed_Object (Gen_Unit);
4936 Set_Is_Instantiated (Gen_Unit);
4937 Generate_Reference (Gen_Unit, N);
4940 if Contains_Instance_Of (Gen_Unit, Current_Scope, Gen_Id) then
4941 Error_Msg_Node_2 := Current_Scope;
4943 ("circular Instantiation: & instantiated in &!", N, Gen_Unit);
4944 Circularity_Detected := True;
4945 Restore_Hidden_Primitives (Vis_Prims_List);
4949 Gen_Decl := Unit_Declaration_Node (Gen_Unit);
4951 -- Initialize renamings map, for error checking
4953 Generic_Renamings.Set_Last (0);
4954 Generic_Renamings_HTable.Reset;
4956 Create_Instantiation_Source (N, Gen_Unit, False, S_Adjustment);
4958 -- Copy original generic tree, to produce text for instantiation
4962 (Original_Node (Gen_Decl), Empty, Instantiating => True);
4964 -- Inherit overriding indicator from instance node
4966 Act_Spec := Specification (Act_Tree);
4967 Set_Must_Override (Act_Spec, Must_Override (N));
4968 Set_Must_Not_Override (Act_Spec, Must_Not_Override (N));
4971 Analyze_Associations
4973 Formals => Generic_Formal_Declarations (Act_Tree),
4974 F_Copy => Generic_Formal_Declarations (Gen_Decl));
4976 Vis_Prims_List := Check_Hidden_Primitives (Renaming_List);
4978 -- The subprogram itself cannot contain a nested instance, so the
4979 -- current parent is left empty.
4981 Set_Instance_Env (Gen_Unit, Empty);
4983 -- Build the subprogram declaration, which does not appear in the
4984 -- generic template, and give it a sloc consistent with that of the
4987 Set_Defining_Unit_Name (Act_Spec, Anon_Id);
4988 Set_Generic_Parent (Act_Spec, Gen_Unit);
4990 Make_Subprogram_Declaration (Sloc (Act_Spec),
4991 Specification => Act_Spec);
4993 -- The aspects have been copied previously, but they have to be
4994 -- linked explicitly to the new subprogram declaration. Explicit
4995 -- pre/postconditions on the instance are analyzed below, in a
4998 Move_Aspects (Act_Tree, To => Act_Decl);
4999 Set_Categorization_From_Pragmas (Act_Decl);
5001 if Parent_Installed then
5005 Append (Act_Decl, Renaming_List);
5006 Analyze_Instance_And_Renamings;
5008 -- If the generic is marked Import (Intrinsic), then so is the
5009 -- instance. This indicates that there is no body to instantiate. If
5010 -- generic is marked inline, so it the instance, and the anonymous
5011 -- subprogram it renames. If inlined, or else if inlining is enabled
5012 -- for the compilation, we generate the instance body even if it is
5013 -- not within the main unit.
5015 if Is_Intrinsic_Subprogram (Gen_Unit) then
5016 Set_Is_Intrinsic_Subprogram (Anon_Id);
5017 Set_Is_Intrinsic_Subprogram (Act_Decl_Id);
5019 if Chars (Gen_Unit) = Name_Unchecked_Conversion then
5020 Validate_Unchecked_Conversion (N, Act_Decl_Id);
5024 -- Inherit convention from generic unit. Intrinsic convention, as for
5025 -- an instance of unchecked conversion, is not inherited because an
5026 -- explicit Ada instance has been created.
5028 if Has_Convention_Pragma (Gen_Unit)
5029 and then Convention (Gen_Unit) /= Convention_Intrinsic
5031 Set_Convention (Act_Decl_Id, Convention (Gen_Unit));
5032 Set_Is_Exported (Act_Decl_Id, Is_Exported (Gen_Unit));
5035 Generate_Definition (Act_Decl_Id);
5036 -- Set_Contract (Anon_Id, Make_Contract (Sloc (Anon_Id)));
5038 Set_Contract (Act_Decl_Id, Make_Contract (Sloc (Act_Decl_Id)));
5040 -- Inherit all inlining-related flags which apply to the generic in
5041 -- the subprogram and its declaration.
5043 Set_Is_Inlined (Act_Decl_Id, Is_Inlined (Gen_Unit));
5044 Set_Is_Inlined (Anon_Id, Is_Inlined (Gen_Unit));
5046 Set_Has_Pragma_Inline (Act_Decl_Id, Has_Pragma_Inline (Gen_Unit));
5047 Set_Has_Pragma_Inline (Anon_Id, Has_Pragma_Inline (Gen_Unit));
5049 Set_Has_Pragma_Inline_Always
5050 (Act_Decl_Id, Has_Pragma_Inline_Always (Gen_Unit));
5051 Set_Has_Pragma_Inline_Always
5052 (Anon_Id, Has_Pragma_Inline_Always (Gen_Unit));
5054 if not Is_Intrinsic_Subprogram (Gen_Unit) then
5055 Check_Elab_Instantiation (N);
5058 if Is_Dispatching_Operation (Act_Decl_Id)
5059 and then Ada_Version >= Ada_2005
5065 Formal := First_Formal (Act_Decl_Id);
5066 while Present (Formal) loop
5067 if Ekind (Etype (Formal)) = E_Anonymous_Access_Type
5068 and then Is_Controlling_Formal (Formal)
5069 and then not Can_Never_Be_Null (Formal)
5071 Error_Msg_NE ("access parameter& is controlling,",
5074 ("\corresponding parameter of & must be"
5075 & " explicitly null-excluding", N, Gen_Id);
5078 Next_Formal (Formal);
5083 Check_Hidden_Child_Unit (N, Gen_Unit, Act_Decl_Id);
5085 Validate_Categorization_Dependency (N, Act_Decl_Id);
5087 if not Is_Intrinsic_Subprogram (Act_Decl_Id) then
5088 Inherit_Context (Gen_Decl, N);
5090 Restore_Private_Views (Pack_Id, False);
5092 -- If the context requires a full instantiation, mark node for
5093 -- subsequent construction of the body.
5095 if Need_Subprogram_Instance_Body (N, Act_Decl_Id) then
5096 Check_Forward_Instantiation (Gen_Decl);
5098 -- The wrapper package is always delayed, because it does not
5099 -- constitute a freeze point, but to insure that the freeze
5100 -- node is placed properly, it is created directly when
5101 -- instantiating the body (otherwise the freeze node might
5102 -- appear to early for nested instantiations).
5104 elsif Nkind (Parent (N)) = N_Compilation_Unit then
5106 -- For ASIS purposes, indicate that the wrapper package has
5107 -- replaced the instantiation node.
5109 Rewrite (N, Unit (Parent (N)));
5110 Set_Unit (Parent (N), N);
5113 elsif Nkind (Parent (N)) = N_Compilation_Unit then
5115 -- Replace instance node for library-level instantiations of
5116 -- intrinsic subprograms, for ASIS use.
5118 Rewrite (N, Unit (Parent (N)));
5119 Set_Unit (Parent (N), N);
5122 if Parent_Installed then
5126 Restore_Hidden_Primitives (Vis_Prims_List);
5128 Env_Installed := False;
5129 Generic_Renamings.Set_Last (0);
5130 Generic_Renamings_HTable.Reset;
5134 if Has_Aspects (N) then
5135 Analyze_Aspect_Specifications (N, Act_Decl_Id);
5139 when Instantiation_Error =>
5140 if Parent_Installed then
5144 if Env_Installed then
5147 end Analyze_Subprogram_Instantiation;
5149 -------------------------
5150 -- Get_Associated_Node --
5151 -------------------------
5153 function Get_Associated_Node (N : Node_Id) return Node_Id is
5157 Assoc := Associated_Node (N);
5159 if Nkind (Assoc) /= Nkind (N) then
5162 elsif Nkind_In (Assoc, N_Aggregate, N_Extension_Aggregate) then
5166 -- If the node is part of an inner generic, it may itself have been
5167 -- remapped into a further generic copy. Associated_Node is otherwise
5168 -- used for the entity of the node, and will be of a different node
5169 -- kind, or else N has been rewritten as a literal or function call.
5171 while Present (Associated_Node (Assoc))
5172 and then Nkind (Associated_Node (Assoc)) = Nkind (Assoc)
5174 Assoc := Associated_Node (Assoc);
5177 -- Follow and additional link in case the final node was rewritten.
5178 -- This can only happen with nested generic units.
5180 if (Nkind (Assoc) = N_Identifier or else Nkind (Assoc) in N_Op)
5181 and then Present (Associated_Node (Assoc))
5182 and then (Nkind_In (Associated_Node (Assoc), N_Function_Call,
5183 N_Explicit_Dereference,
5188 Assoc := Associated_Node (Assoc);
5191 -- An additional special case: an unconstrained type in an object
5192 -- declaration may have been rewritten as a local subtype constrained
5193 -- by the expression in the declaration. We need to recover the
5194 -- original entity which may be global.
5196 if Present (Original_Node (Assoc))
5197 and then Nkind (Parent (N)) = N_Object_Declaration
5199 Assoc := Original_Node (Assoc);
5204 end Get_Associated_Node;
5206 -------------------------------------------
5207 -- Build_Instance_Compilation_Unit_Nodes --
5208 -------------------------------------------
5210 procedure Build_Instance_Compilation_Unit_Nodes
5215 Decl_Cunit : Node_Id;
5216 Body_Cunit : Node_Id;
5218 New_Main : constant Entity_Id := Defining_Entity (Act_Decl);
5219 Old_Main : constant Entity_Id := Cunit_Entity (Main_Unit);
5222 -- A new compilation unit node is built for the instance declaration
5225 Make_Compilation_Unit (Sloc (N),
5226 Context_Items => Empty_List,
5228 Aux_Decls_Node => Make_Compilation_Unit_Aux (Sloc (N)));
5230 Set_Parent_Spec (Act_Decl, Parent_Spec (N));
5232 -- The new compilation unit is linked to its body, but both share the
5233 -- same file, so we do not set Body_Required on the new unit so as not
5234 -- to create a spurious dependency on a non-existent body in the ali.
5235 -- This simplifies CodePeer unit traversal.
5237 -- We use the original instantiation compilation unit as the resulting
5238 -- compilation unit of the instance, since this is the main unit.
5240 Rewrite (N, Act_Body);
5242 -- Propagate the aspect specifications from the package body template to
5243 -- the instantiated version of the package body.
5245 if Has_Aspects (Act_Body) then
5246 Set_Aspect_Specifications
5247 (N, New_Copy_List_Tree (Aspect_Specifications (Act_Body)));
5250 Body_Cunit := Parent (N);
5252 -- The two compilation unit nodes are linked by the Library_Unit field
5254 Set_Library_Unit (Decl_Cunit, Body_Cunit);
5255 Set_Library_Unit (Body_Cunit, Decl_Cunit);
5257 -- Preserve the private nature of the package if needed
5259 Set_Private_Present (Decl_Cunit, Private_Present (Body_Cunit));
5261 -- If the instance is not the main unit, its context, categorization
5262 -- and elaboration entity are not relevant to the compilation.
5264 if Body_Cunit /= Cunit (Main_Unit) then
5265 Make_Instance_Unit (Body_Cunit, In_Main => False);
5269 -- The context clause items on the instantiation, which are now attached
5270 -- to the body compilation unit (since the body overwrote the original
5271 -- instantiation node), semantically belong on the spec, so copy them
5272 -- there. It's harmless to leave them on the body as well. In fact one
5273 -- could argue that they belong in both places.
5275 Citem := First (Context_Items (Body_Cunit));
5276 while Present (Citem) loop
5277 Append (New_Copy (Citem), Context_Items (Decl_Cunit));
5281 -- Propagate categorization flags on packages, so that they appear in
5282 -- the ali file for the spec of the unit.
5284 if Ekind (New_Main) = E_Package then
5285 Set_Is_Pure (Old_Main, Is_Pure (New_Main));
5286 Set_Is_Preelaborated (Old_Main, Is_Preelaborated (New_Main));
5287 Set_Is_Remote_Types (Old_Main, Is_Remote_Types (New_Main));
5288 Set_Is_Shared_Passive (Old_Main, Is_Shared_Passive (New_Main));
5289 Set_Is_Remote_Call_Interface
5290 (Old_Main, Is_Remote_Call_Interface (New_Main));
5293 -- Make entry in Units table, so that binder can generate call to
5294 -- elaboration procedure for body, if any.
5296 Make_Instance_Unit (Body_Cunit, In_Main => True);
5297 Main_Unit_Entity := New_Main;
5298 Set_Cunit_Entity (Main_Unit, Main_Unit_Entity);
5300 -- Build elaboration entity, since the instance may certainly generate
5301 -- elaboration code requiring a flag for protection.
5303 Build_Elaboration_Entity (Decl_Cunit, New_Main);
5304 end Build_Instance_Compilation_Unit_Nodes;
5306 -----------------------------
5307 -- Check_Access_Definition --
5308 -----------------------------
5310 procedure Check_Access_Definition (N : Node_Id) is
5313 (Ada_Version >= Ada_2005 and then Present (Access_Definition (N)));
5315 end Check_Access_Definition;
5317 -----------------------------------
5318 -- Check_Formal_Package_Instance --
5319 -----------------------------------
5321 -- If the formal has specific parameters, they must match those of the
5322 -- actual. Both of them are instances, and the renaming declarations for
5323 -- their formal parameters appear in the same order in both. The analyzed
5324 -- formal has been analyzed in the context of the current instance.
5326 procedure Check_Formal_Package_Instance
5327 (Formal_Pack : Entity_Id;
5328 Actual_Pack : Entity_Id)
5330 E1 : Entity_Id := First_Entity (Actual_Pack);
5331 E2 : Entity_Id := First_Entity (Formal_Pack);
5336 procedure Check_Mismatch (B : Boolean);
5337 -- Common error routine for mismatch between the parameters of the
5338 -- actual instance and those of the formal package.
5340 function Same_Instantiated_Constant (E1, E2 : Entity_Id) return Boolean;
5341 -- The formal may come from a nested formal package, and the actual may
5342 -- have been constant-folded. To determine whether the two denote the
5343 -- same entity we may have to traverse several definitions to recover
5344 -- the ultimate entity that they refer to.
5346 function Same_Instantiated_Variable (E1, E2 : Entity_Id) return Boolean;
5347 -- Similarly, if the formal comes from a nested formal package, the
5348 -- actual may designate the formal through multiple renamings, which
5349 -- have to be followed to determine the original variable in question.
5351 --------------------
5352 -- Check_Mismatch --
5353 --------------------
5355 procedure Check_Mismatch (B : Boolean) is
5356 Kind : constant Node_Kind := Nkind (Parent (E2));
5359 if Kind = N_Formal_Type_Declaration then
5362 elsif Nkind_In (Kind, N_Formal_Object_Declaration,
5363 N_Formal_Package_Declaration)
5364 or else Kind in N_Formal_Subprogram_Declaration
5370 ("actual for & in actual instance does not match formal",
5371 Parent (Actual_Pack), E1);
5375 --------------------------------
5376 -- Same_Instantiated_Constant --
5377 --------------------------------
5379 function Same_Instantiated_Constant
5380 (E1, E2 : Entity_Id) return Boolean
5386 while Present (Ent) loop
5390 elsif Ekind (Ent) /= E_Constant then
5393 elsif Is_Entity_Name (Constant_Value (Ent)) then
5394 if Entity (Constant_Value (Ent)) = E1 then
5397 Ent := Entity (Constant_Value (Ent));
5400 -- The actual may be a constant that has been folded. Recover
5403 elsif Is_Entity_Name (Original_Node (Constant_Value (Ent))) then
5404 Ent := Entity (Original_Node (Constant_Value (Ent)));
5411 end Same_Instantiated_Constant;
5413 --------------------------------
5414 -- Same_Instantiated_Variable --
5415 --------------------------------
5417 function Same_Instantiated_Variable
5418 (E1, E2 : Entity_Id) return Boolean
5420 function Original_Entity (E : Entity_Id) return Entity_Id;
5421 -- Follow chain of renamings to the ultimate ancestor
5423 ---------------------
5424 -- Original_Entity --
5425 ---------------------
5427 function Original_Entity (E : Entity_Id) return Entity_Id is
5432 while Nkind (Parent (Orig)) = N_Object_Renaming_Declaration
5433 and then Present (Renamed_Object (Orig))
5434 and then Is_Entity_Name (Renamed_Object (Orig))
5436 Orig := Entity (Renamed_Object (Orig));
5440 end Original_Entity;
5442 -- Start of processing for Same_Instantiated_Variable
5445 return Ekind (E1) = Ekind (E2)
5446 and then Original_Entity (E1) = Original_Entity (E2);
5447 end Same_Instantiated_Variable;
5449 -- Start of processing for Check_Formal_Package_Instance
5453 and then Present (E2)
5455 exit when Ekind (E1) = E_Package
5456 and then Renamed_Entity (E1) = Renamed_Entity (Actual_Pack);
5458 -- If the formal is the renaming of the formal package, this
5459 -- is the end of its formal part, which may occur before the
5460 -- end of the formal part in the actual in the presence of
5461 -- defaulted parameters in the formal package.
5463 exit when Nkind (Parent (E2)) = N_Package_Renaming_Declaration
5464 and then Renamed_Entity (E2) = Scope (E2);
5466 -- The analysis of the actual may generate additional internal
5467 -- entities. If the formal is defaulted, there is no corresponding
5468 -- analysis and the internal entities must be skipped, until we
5469 -- find corresponding entities again.
5471 if Comes_From_Source (E2)
5472 and then not Comes_From_Source (E1)
5473 and then Chars (E1) /= Chars (E2)
5476 and then Chars (E1) /= Chars (E2)
5485 -- If the formal entity comes from a formal declaration, it was
5486 -- defaulted in the formal package, and no check is needed on it.
5488 elsif Nkind (Parent (E2)) = N_Formal_Object_Declaration then
5491 -- Ditto for defaulted formal subprograms.
5493 elsif Is_Overloadable (E1)
5494 and then Nkind (Unit_Declaration_Node (E2)) in
5495 N_Formal_Subprogram_Declaration
5499 elsif Is_Type (E1) then
5501 -- Subtypes must statically match. E1, E2 are the local entities
5502 -- that are subtypes of the actuals. Itypes generated for other
5503 -- parameters need not be checked, the check will be performed
5504 -- on the parameters themselves.
5506 -- If E2 is a formal type declaration, it is a defaulted parameter
5507 -- and needs no checking.
5509 if not Is_Itype (E1)
5510 and then not Is_Itype (E2)
5514 or else Etype (E1) /= Etype (E2)
5515 or else not Subtypes_Statically_Match (E1, E2));
5518 elsif Ekind (E1) = E_Constant then
5520 -- IN parameters must denote the same static value, or the same
5521 -- constant, or the literal null.
5523 Expr1 := Expression (Parent (E1));
5525 if Ekind (E2) /= E_Constant then
5526 Check_Mismatch (True);
5529 Expr2 := Expression (Parent (E2));
5532 if Is_OK_Static_Expression (Expr1) then
5533 if not Is_OK_Static_Expression (Expr2) then
5534 Check_Mismatch (True);
5536 elsif Is_Discrete_Type (Etype (E1)) then
5538 V1 : constant Uint := Expr_Value (Expr1);
5539 V2 : constant Uint := Expr_Value (Expr2);
5541 Check_Mismatch (V1 /= V2);
5544 elsif Is_Real_Type (Etype (E1)) then
5546 V1 : constant Ureal := Expr_Value_R (Expr1);
5547 V2 : constant Ureal := Expr_Value_R (Expr2);
5549 Check_Mismatch (V1 /= V2);
5552 elsif Is_String_Type (Etype (E1))
5553 and then Nkind (Expr1) = N_String_Literal
5555 if Nkind (Expr2) /= N_String_Literal then
5556 Check_Mismatch (True);
5559 (not String_Equal (Strval (Expr1), Strval (Expr2)));
5563 elsif Is_Entity_Name (Expr1) then
5564 if Is_Entity_Name (Expr2) then
5565 if Entity (Expr1) = Entity (Expr2) then
5569 (not Same_Instantiated_Constant
5570 (Entity (Expr1), Entity (Expr2)));
5573 Check_Mismatch (True);
5576 elsif Is_Entity_Name (Original_Node (Expr1))
5577 and then Is_Entity_Name (Expr2)
5579 Same_Instantiated_Constant
5580 (Entity (Original_Node (Expr1)), Entity (Expr2))
5584 elsif Nkind (Expr1) = N_Null then
5585 Check_Mismatch (Nkind (Expr1) /= N_Null);
5588 Check_Mismatch (True);
5591 elsif Ekind (E1) = E_Variable then
5592 Check_Mismatch (not Same_Instantiated_Variable (E1, E2));
5594 elsif Ekind (E1) = E_Package then
5596 (Ekind (E1) /= Ekind (E2)
5597 or else Renamed_Object (E1) /= Renamed_Object (E2));
5599 elsif Is_Overloadable (E1) then
5601 -- Verify that the actual subprograms match. Note that actuals
5602 -- that are attributes are rewritten as subprograms. If the
5603 -- subprogram in the formal package is defaulted, no check is
5604 -- needed. Note that this can only happen in Ada 2005 when the
5605 -- formal package can be partially parameterized.
5607 if Nkind (Unit_Declaration_Node (E1)) =
5608 N_Subprogram_Renaming_Declaration
5609 and then From_Default (Unit_Declaration_Node (E1))
5613 -- If the formal package has an "others" box association that
5614 -- covers this formal, there is no need for a check either.
5616 elsif Nkind (Unit_Declaration_Node (E2)) in
5617 N_Formal_Subprogram_Declaration
5618 and then Box_Present (Unit_Declaration_Node (E2))
5622 -- No check needed if subprogram is a defaulted null procedure
5624 elsif No (Alias (E2))
5625 and then Ekind (E2) = E_Procedure
5627 Null_Present (Specification (Unit_Declaration_Node (E2)))
5631 -- Otherwise the actual in the formal and the actual in the
5632 -- instantiation of the formal must match, up to renamings.
5636 (Ekind (E2) /= Ekind (E1) or else (Alias (E1)) /= Alias (E2));
5640 raise Program_Error;
5647 end Check_Formal_Package_Instance;
5649 ---------------------------
5650 -- Check_Formal_Packages --
5651 ---------------------------
5653 procedure Check_Formal_Packages (P_Id : Entity_Id) is
5655 Formal_P : Entity_Id;
5658 -- Iterate through the declarations in the instance, looking for package
5659 -- renaming declarations that denote instances of formal packages. Stop
5660 -- when we find the renaming of the current package itself. The
5661 -- declaration for a formal package without a box is followed by an
5662 -- internal entity that repeats the instantiation.
5664 E := First_Entity (P_Id);
5665 while Present (E) loop
5666 if Ekind (E) = E_Package then
5667 if Renamed_Object (E) = P_Id then
5670 elsif Nkind (Parent (E)) /= N_Package_Renaming_Declaration then
5673 elsif not Box_Present (Parent (Associated_Formal_Package (E))) then
5674 Formal_P := Next_Entity (E);
5675 Check_Formal_Package_Instance (Formal_P, E);
5677 -- After checking, remove the internal validating package. It
5678 -- is only needed for semantic checks, and as it may contain
5679 -- generic formal declarations it should not reach gigi.
5681 Remove (Unit_Declaration_Node (Formal_P));
5687 end Check_Formal_Packages;
5689 ---------------------------------
5690 -- Check_Forward_Instantiation --
5691 ---------------------------------
5693 procedure Check_Forward_Instantiation (Decl : Node_Id) is
5695 Gen_Comp : Entity_Id := Cunit_Entity (Get_Source_Unit (Decl));
5698 -- The instantiation appears before the generic body if we are in the
5699 -- scope of the unit containing the generic, either in its spec or in
5700 -- the package body, and before the generic body.
5702 if Ekind (Gen_Comp) = E_Package_Body then
5703 Gen_Comp := Spec_Entity (Gen_Comp);
5706 if In_Open_Scopes (Gen_Comp)
5707 and then No (Corresponding_Body (Decl))
5712 and then not Is_Compilation_Unit (S)
5713 and then not Is_Child_Unit (S)
5715 if Ekind (S) = E_Package then
5716 Set_Has_Forward_Instantiation (S);
5722 end Check_Forward_Instantiation;
5724 ---------------------------
5725 -- Check_Generic_Actuals --
5726 ---------------------------
5728 -- The visibility of the actuals may be different between the point of
5729 -- generic instantiation and the instantiation of the body.
5731 procedure Check_Generic_Actuals
5732 (Instance : Entity_Id;
5733 Is_Formal_Box : Boolean)
5738 function Denotes_Previous_Actual (Typ : Entity_Id) return Boolean;
5739 -- For a formal that is an array type, the component type is often a
5740 -- previous formal in the same unit. The privacy status of the component
5741 -- type will have been examined earlier in the traversal of the
5742 -- corresponding actuals, and this status should not be modified for
5743 -- the array (sub)type itself. However, if the base type of the array
5744 -- (sub)type is private, its full view must be restored in the body to
5745 -- be consistent with subsequent index subtypes, etc.
5747 -- To detect this case we have to rescan the list of formals, which is
5748 -- usually short enough to ignore the resulting inefficiency.
5750 -----------------------------
5751 -- Denotes_Previous_Actual --
5752 -----------------------------
5754 function Denotes_Previous_Actual (Typ : Entity_Id) return Boolean is
5758 Prev := First_Entity (Instance);
5759 while Present (Prev) loop
5761 and then Nkind (Parent (Prev)) = N_Subtype_Declaration
5762 and then Is_Entity_Name (Subtype_Indication (Parent (Prev)))
5763 and then Entity (Subtype_Indication (Parent (Prev))) = Typ
5776 end Denotes_Previous_Actual;
5778 -- Start of processing for Check_Generic_Actuals
5781 E := First_Entity (Instance);
5782 while Present (E) loop
5784 and then Nkind (Parent (E)) = N_Subtype_Declaration
5785 and then Scope (Etype (E)) /= Instance
5786 and then Is_Entity_Name (Subtype_Indication (Parent (E)))
5788 if Is_Array_Type (E)
5789 and then not Is_Private_Type (Etype (E))
5790 and then Denotes_Previous_Actual (Component_Type (E))
5794 Check_Private_View (Subtype_Indication (Parent (E)));
5797 Set_Is_Generic_Actual_Type (E, True);
5798 Set_Is_Hidden (E, False);
5799 Set_Is_Potentially_Use_Visible (E,
5802 -- We constructed the generic actual type as a subtype of the
5803 -- supplied type. This means that it normally would not inherit
5804 -- subtype specific attributes of the actual, which is wrong for
5805 -- the generic case.
5807 Astype := Ancestor_Subtype (E);
5811 -- This can happen when E is an itype that is the full view of
5812 -- a private type completed, e.g. with a constrained array. In
5813 -- that case, use the first subtype, which will carry size
5814 -- information. The base type itself is unconstrained and will
5817 Astype := First_Subtype (E);
5820 Set_Size_Info (E, (Astype));
5821 Set_RM_Size (E, RM_Size (Astype));
5822 Set_First_Rep_Item (E, First_Rep_Item (Astype));
5824 if Is_Discrete_Or_Fixed_Point_Type (E) then
5825 Set_RM_Size (E, RM_Size (Astype));
5827 -- In nested instances, the base type of an access actual may
5828 -- itself be private, and need to be exchanged.
5830 elsif Is_Access_Type (E)
5831 and then Is_Private_Type (Etype (E))
5834 (New_Occurrence_Of (Etype (E), Sloc (Instance)));
5837 elsif Ekind (E) = E_Package then
5839 -- If this is the renaming for the current instance, we're done.
5840 -- Otherwise it is a formal package. If the corresponding formal
5841 -- was declared with a box, the (instantiations of the) generic
5842 -- formal part are also visible. Otherwise, ignore the entity
5843 -- created to validate the actuals.
5845 if Renamed_Object (E) = Instance then
5848 elsif Nkind (Parent (E)) /= N_Package_Renaming_Declaration then
5851 -- The visibility of a formal of an enclosing generic is already
5854 elsif Denotes_Formal_Package (E) then
5857 elsif Present (Associated_Formal_Package (E))
5858 and then not Is_Generic_Formal (E)
5860 if Box_Present (Parent (Associated_Formal_Package (E))) then
5861 Check_Generic_Actuals (Renamed_Object (E), True);
5864 Check_Generic_Actuals (Renamed_Object (E), False);
5867 Set_Is_Hidden (E, False);
5870 -- If this is a subprogram instance (in a wrapper package) the
5871 -- actual is fully visible.
5873 elsif Is_Wrapper_Package (Instance) then
5874 Set_Is_Hidden (E, False);
5876 -- If the formal package is declared with a box, or if the formal
5877 -- parameter is defaulted, it is visible in the body.
5880 or else Is_Visible_Formal (E)
5882 Set_Is_Hidden (E, False);
5885 if Ekind (E) = E_Constant then
5887 -- If the type of the actual is a private type declared in the
5888 -- enclosing scope of the generic unit, the body of the generic
5889 -- sees the full view of the type (because it has to appear in
5890 -- the corresponding package body). If the type is private now,
5891 -- exchange views to restore the proper visiblity in the instance.
5894 Typ : constant Entity_Id := Base_Type (Etype (E));
5895 -- The type of the actual
5900 Parent_Scope : Entity_Id;
5901 -- The enclosing scope of the generic unit
5904 if Is_Wrapper_Package (Instance) then
5908 (Unit_Declaration_Node
5909 (Related_Instance (Instance))));
5912 Generic_Parent (Package_Specification (Instance));
5915 Parent_Scope := Scope (Gen_Id);
5917 -- The exchange is only needed if the generic is defined
5918 -- within a package which is not a common ancestor of the
5919 -- scope of the instance, and is not already in scope.
5921 if Is_Private_Type (Typ)
5922 and then Scope (Typ) = Parent_Scope
5923 and then Scope (Instance) /= Parent_Scope
5924 and then Ekind (Parent_Scope) = E_Package
5925 and then not Is_Child_Unit (Gen_Id)
5929 -- If the type of the entity is a subtype, it may also have
5930 -- to be made visible, together with the base type of its
5931 -- full view, after exchange.
5933 if Is_Private_Type (Etype (E)) then
5934 Switch_View (Etype (E));
5935 Switch_View (Base_Type (Etype (E)));
5943 end Check_Generic_Actuals;
5945 ------------------------------
5946 -- Check_Generic_Child_Unit --
5947 ------------------------------
5949 procedure Check_Generic_Child_Unit
5951 Parent_Installed : in out Boolean)
5953 Loc : constant Source_Ptr := Sloc (Gen_Id);
5954 Gen_Par : Entity_Id := Empty;
5956 Inst_Par : Entity_Id;
5959 function Find_Generic_Child
5961 Id : Node_Id) return Entity_Id;
5962 -- Search generic parent for possible child unit with the given name
5964 function In_Enclosing_Instance return Boolean;
5965 -- Within an instance of the parent, the child unit may be denoted by
5966 -- a simple name, or an abbreviated expanded name. Examine enclosing
5967 -- scopes to locate a possible parent instantiation.
5969 ------------------------
5970 -- Find_Generic_Child --
5971 ------------------------
5973 function Find_Generic_Child
5975 Id : Node_Id) return Entity_Id
5980 -- If entity of name is already set, instance has already been
5981 -- resolved, e.g. in an enclosing instantiation.
5983 if Present (Entity (Id)) then
5984 if Scope (Entity (Id)) = Scop then
5991 E := First_Entity (Scop);
5992 while Present (E) loop
5993 if Chars (E) = Chars (Id)
5994 and then Is_Child_Unit (E)
5996 if Is_Child_Unit (E)
5997 and then not Is_Visible_Lib_Unit (E)
6000 ("generic child unit& is not visible", Gen_Id, E);
6012 end Find_Generic_Child;
6014 ---------------------------
6015 -- In_Enclosing_Instance --
6016 ---------------------------
6018 function In_Enclosing_Instance return Boolean is
6019 Enclosing_Instance : Node_Id;
6020 Instance_Decl : Node_Id;
6023 -- We do not inline any call that contains instantiations, except
6024 -- for instantiations of Unchecked_Conversion, so if we are within
6025 -- an inlined body the current instance does not require parents.
6027 if In_Inlined_Body then
6028 pragma Assert (Chars (Gen_Id) = Name_Unchecked_Conversion);
6032 -- Loop to check enclosing scopes
6034 Enclosing_Instance := Current_Scope;
6035 while Present (Enclosing_Instance) loop
6036 Instance_Decl := Unit_Declaration_Node (Enclosing_Instance);
6038 if Ekind (Enclosing_Instance) = E_Package
6039 and then Is_Generic_Instance (Enclosing_Instance)
6041 (Generic_Parent (Specification (Instance_Decl)))
6043 -- Check whether the generic we are looking for is a child of
6046 E := Find_Generic_Child
6047 (Generic_Parent (Specification (Instance_Decl)), Gen_Id);
6048 exit when Present (E);
6054 Enclosing_Instance := Scope (Enclosing_Instance);
6066 Make_Expanded_Name (Loc,
6068 Prefix => New_Occurrence_Of (Enclosing_Instance, Loc),
6069 Selector_Name => New_Occurrence_Of (E, Loc)));
6071 Set_Entity (Gen_Id, E);
6072 Set_Etype (Gen_Id, Etype (E));
6073 Parent_Installed := False; -- Already in scope.
6076 end In_Enclosing_Instance;
6078 -- Start of processing for Check_Generic_Child_Unit
6081 -- If the name of the generic is given by a selected component, it may
6082 -- be the name of a generic child unit, and the prefix is the name of an
6083 -- instance of the parent, in which case the child unit must be visible.
6084 -- If this instance is not in scope, it must be placed there and removed
6085 -- after instantiation, because what is being instantiated is not the
6086 -- original child, but the corresponding child present in the instance
6089 -- If the child is instantiated within the parent, it can be given by
6090 -- a simple name. In this case the instance is already in scope, but
6091 -- the child generic must be recovered from the generic parent as well.
6093 if Nkind (Gen_Id) = N_Selected_Component then
6094 S := Selector_Name (Gen_Id);
6095 Analyze (Prefix (Gen_Id));
6096 Inst_Par := Entity (Prefix (Gen_Id));
6098 if Ekind (Inst_Par) = E_Package
6099 and then Present (Renamed_Object (Inst_Par))
6101 Inst_Par := Renamed_Object (Inst_Par);
6104 if Ekind (Inst_Par) = E_Package then
6105 if Nkind (Parent (Inst_Par)) = N_Package_Specification then
6106 Gen_Par := Generic_Parent (Parent (Inst_Par));
6108 elsif Nkind (Parent (Inst_Par)) = N_Defining_Program_Unit_Name
6110 Nkind (Parent (Parent (Inst_Par))) = N_Package_Specification
6112 Gen_Par := Generic_Parent (Parent (Parent (Inst_Par)));
6115 elsif Ekind (Inst_Par) = E_Generic_Package
6116 and then Nkind (Parent (Gen_Id)) = N_Formal_Package_Declaration
6118 -- A formal package may be a real child package, and not the
6119 -- implicit instance within a parent. In this case the child is
6120 -- not visible and has to be retrieved explicitly as well.
6122 Gen_Par := Inst_Par;
6125 if Present (Gen_Par) then
6127 -- The prefix denotes an instantiation. The entity itself may be a
6128 -- nested generic, or a child unit.
6130 E := Find_Generic_Child (Gen_Par, S);
6133 Change_Selected_Component_To_Expanded_Name (Gen_Id);
6134 Set_Entity (Gen_Id, E);
6135 Set_Etype (Gen_Id, Etype (E));
6137 Set_Etype (S, Etype (E));
6139 -- Indicate that this is a reference to the parent
6141 if In_Extended_Main_Source_Unit (Gen_Id) then
6142 Set_Is_Instantiated (Inst_Par);
6145 -- A common mistake is to replicate the naming scheme of a
6146 -- hierarchy by instantiating a generic child directly, rather
6147 -- than the implicit child in a parent instance:
6149 -- generic .. package Gpar is ..
6150 -- generic .. package Gpar.Child is ..
6151 -- package Par is new Gpar ();
6154 -- package Par.Child is new Gpar.Child ();
6155 -- rather than Par.Child
6157 -- In this case the instantiation is within Par, which is an
6158 -- instance, but Gpar does not denote Par because we are not IN
6159 -- the instance of Gpar, so this is illegal. The test below
6160 -- recognizes this particular case.
6162 if Is_Child_Unit (E)
6163 and then not Comes_From_Source (Entity (Prefix (Gen_Id)))
6164 and then (not In_Instance
6165 or else Nkind (Parent (Parent (Gen_Id))) =
6169 ("prefix of generic child unit must be instance of parent",
6173 if not In_Open_Scopes (Inst_Par)
6174 and then Nkind (Parent (Gen_Id)) not in
6175 N_Generic_Renaming_Declaration
6177 Install_Parent (Inst_Par);
6178 Parent_Installed := True;
6180 elsif In_Open_Scopes (Inst_Par) then
6182 -- If the parent is already installed, install the actuals
6183 -- for its formal packages. This is necessary when the child
6184 -- instance is a child of the parent instance: in this case,
6185 -- the parent is placed on the scope stack but the formal
6186 -- packages are not made visible.
6188 Install_Formal_Packages (Inst_Par);
6192 -- If the generic parent does not contain an entity that
6193 -- corresponds to the selector, the instance doesn't either.
6194 -- Analyzing the node will yield the appropriate error message.
6195 -- If the entity is not a child unit, then it is an inner
6196 -- generic in the parent.
6204 if Is_Child_Unit (Entity (Gen_Id))
6206 Nkind (Parent (Gen_Id)) not in N_Generic_Renaming_Declaration
6207 and then not In_Open_Scopes (Inst_Par)
6209 Install_Parent (Inst_Par);
6210 Parent_Installed := True;
6212 -- The generic unit may be the renaming of the implicit child
6213 -- present in an instance. In that case the parent instance is
6214 -- obtained from the name of the renamed entity.
6216 elsif Ekind (Entity (Gen_Id)) = E_Generic_Package
6217 and then Present (Renamed_Entity (Entity (Gen_Id)))
6218 and then Is_Child_Unit (Renamed_Entity (Entity (Gen_Id)))
6221 Renamed_Package : constant Node_Id :=
6222 Name (Parent (Entity (Gen_Id)));
6224 if Nkind (Renamed_Package) = N_Expanded_Name then
6225 Inst_Par := Entity (Prefix (Renamed_Package));
6226 Install_Parent (Inst_Par);
6227 Parent_Installed := True;
6233 elsif Nkind (Gen_Id) = N_Expanded_Name then
6235 -- Entity already present, analyze prefix, whose meaning may be
6236 -- an instance in the current context. If it is an instance of
6237 -- a relative within another, the proper parent may still have
6238 -- to be installed, if they are not of the same generation.
6240 Analyze (Prefix (Gen_Id));
6242 -- In the unlikely case that a local declaration hides the name
6243 -- of the parent package, locate it on the homonym chain. If the
6244 -- context is an instance of the parent, the renaming entity is
6247 Inst_Par := Entity (Prefix (Gen_Id));
6248 while Present (Inst_Par)
6249 and then not Is_Package_Or_Generic_Package (Inst_Par)
6251 Inst_Par := Homonym (Inst_Par);
6254 pragma Assert (Present (Inst_Par));
6255 Set_Entity (Prefix (Gen_Id), Inst_Par);
6257 if In_Enclosing_Instance then
6260 elsif Present (Entity (Gen_Id))
6261 and then Is_Child_Unit (Entity (Gen_Id))
6262 and then not In_Open_Scopes (Inst_Par)
6264 Install_Parent (Inst_Par);
6265 Parent_Installed := True;
6268 elsif In_Enclosing_Instance then
6270 -- The child unit is found in some enclosing scope
6277 -- If this is the renaming of the implicit child in a parent
6278 -- instance, recover the parent name and install it.
6280 if Is_Entity_Name (Gen_Id) then
6281 E := Entity (Gen_Id);
6283 if Is_Generic_Unit (E)
6284 and then Nkind (Parent (E)) in N_Generic_Renaming_Declaration
6285 and then Is_Child_Unit (Renamed_Object (E))
6286 and then Is_Generic_Unit (Scope (Renamed_Object (E)))
6287 and then Nkind (Name (Parent (E))) = N_Expanded_Name
6290 New_Copy_Tree (Name (Parent (E))));
6291 Inst_Par := Entity (Prefix (Gen_Id));
6293 if not In_Open_Scopes (Inst_Par) then
6294 Install_Parent (Inst_Par);
6295 Parent_Installed := True;
6298 -- If it is a child unit of a non-generic parent, it may be
6299 -- use-visible and given by a direct name. Install parent as
6302 elsif Is_Generic_Unit (E)
6303 and then Is_Child_Unit (E)
6305 Nkind (Parent (Gen_Id)) not in N_Generic_Renaming_Declaration
6306 and then not Is_Generic_Unit (Scope (E))
6308 if not In_Open_Scopes (Scope (E)) then
6309 Install_Parent (Scope (E));
6310 Parent_Installed := True;
6315 end Check_Generic_Child_Unit;
6317 -----------------------------
6318 -- Check_Hidden_Child_Unit --
6319 -----------------------------
6321 procedure Check_Hidden_Child_Unit
6323 Gen_Unit : Entity_Id;
6324 Act_Decl_Id : Entity_Id)
6326 Gen_Id : constant Node_Id := Name (N);
6329 if Is_Child_Unit (Gen_Unit)
6330 and then Is_Child_Unit (Act_Decl_Id)
6331 and then Nkind (Gen_Id) = N_Expanded_Name
6332 and then Entity (Prefix (Gen_Id)) = Scope (Act_Decl_Id)
6333 and then Chars (Gen_Unit) = Chars (Act_Decl_Id)
6335 Error_Msg_Node_2 := Scope (Act_Decl_Id);
6337 ("generic unit & is implicitly declared in &",
6338 Defining_Unit_Name (N), Gen_Unit);
6339 Error_Msg_N ("\instance must have different name",
6340 Defining_Unit_Name (N));
6342 end Check_Hidden_Child_Unit;
6344 ------------------------
6345 -- Check_Private_View --
6346 ------------------------
6348 procedure Check_Private_View (N : Node_Id) is
6349 T : constant Entity_Id := Etype (N);
6353 -- Exchange views if the type was not private in the generic but is
6354 -- private at the point of instantiation. Do not exchange views if
6355 -- the scope of the type is in scope. This can happen if both generic
6356 -- and instance are sibling units, or if type is defined in a parent.
6357 -- In this case the visibility of the type will be correct for all
6361 BT := Base_Type (T);
6363 if Is_Private_Type (T)
6364 and then not Has_Private_View (N)
6365 and then Present (Full_View (T))
6366 and then not In_Open_Scopes (Scope (T))
6368 -- In the generic, the full type was visible. Save the private
6369 -- entity, for subsequent exchange.
6373 elsif Has_Private_View (N)
6374 and then not Is_Private_Type (T)
6375 and then not Has_Been_Exchanged (T)
6376 and then Etype (Get_Associated_Node (N)) /= T
6378 -- Only the private declaration was visible in the generic. If
6379 -- the type appears in a subtype declaration, the subtype in the
6380 -- instance must have a view compatible with that of its parent,
6381 -- which must be exchanged (see corresponding code in Restore_
6382 -- Private_Views). Otherwise, if the type is defined in a parent
6383 -- unit, leave full visibility within instance, which is safe.
6385 if In_Open_Scopes (Scope (Base_Type (T)))
6386 and then not Is_Private_Type (Base_Type (T))
6387 and then Comes_From_Source (Base_Type (T))
6391 elsif Nkind (Parent (N)) = N_Subtype_Declaration
6392 or else not In_Private_Part (Scope (Base_Type (T)))
6394 Prepend_Elmt (T, Exchanged_Views);
6395 Exchange_Declarations (Etype (Get_Associated_Node (N)));
6398 -- For composite types with inconsistent representation exchange
6399 -- component types accordingly.
6401 elsif Is_Access_Type (T)
6402 and then Is_Private_Type (Designated_Type (T))
6403 and then not Has_Private_View (N)
6404 and then Present (Full_View (Designated_Type (T)))
6406 Switch_View (Designated_Type (T));
6408 elsif Is_Array_Type (T) then
6409 if Is_Private_Type (Component_Type (T))
6410 and then not Has_Private_View (N)
6411 and then Present (Full_View (Component_Type (T)))
6413 Switch_View (Component_Type (T));
6416 -- The normal exchange mechanism relies on the setting of a
6417 -- flag on the reference in the generic. However, an additional
6418 -- mechanism is needed for types that are not explicitly
6419 -- mentioned in the generic, but may be needed in expanded code
6420 -- in the instance. This includes component types of arrays and
6421 -- designated types of access types. This processing must also
6422 -- include the index types of arrays which we take care of here.
6429 Indx := First_Index (T);
6430 while Present (Indx) loop
6431 Typ := Base_Type (Etype (Indx));
6433 if Is_Private_Type (Typ)
6434 and then Present (Full_View (Typ))
6443 elsif Is_Private_Type (T)
6444 and then Present (Full_View (T))
6445 and then Is_Array_Type (Full_View (T))
6446 and then Is_Private_Type (Component_Type (Full_View (T)))
6450 -- Finally, a non-private subtype may have a private base type, which
6451 -- must be exchanged for consistency. This can happen when a package
6452 -- body is instantiated, when the scope stack is empty but in fact
6453 -- the subtype and the base type are declared in an enclosing scope.
6455 -- Note that in this case we introduce an inconsistency in the view
6456 -- set, because we switch the base type BT, but there could be some
6457 -- private dependent subtypes of BT which remain unswitched. Such
6458 -- subtypes might need to be switched at a later point (see specific
6459 -- provision for that case in Switch_View).
6461 elsif not Is_Private_Type (T)
6462 and then not Has_Private_View (N)
6463 and then Is_Private_Type (BT)
6464 and then Present (Full_View (BT))
6465 and then not Is_Generic_Type (BT)
6466 and then not In_Open_Scopes (BT)
6468 Prepend_Elmt (Full_View (BT), Exchanged_Views);
6469 Exchange_Declarations (BT);
6472 end Check_Private_View;
6474 -----------------------------
6475 -- Check_Hidden_Primitives --
6476 -----------------------------
6478 function Check_Hidden_Primitives (Assoc_List : List_Id) return Elist_Id is
6481 Result : Elist_Id := No_Elist;
6484 if No (Assoc_List) then
6488 -- Traverse the list of associations between formals and actuals
6489 -- searching for renamings of tagged types
6491 Actual := First (Assoc_List);
6492 while Present (Actual) loop
6493 if Nkind (Actual) = N_Subtype_Declaration then
6494 Gen_T := Generic_Parent_Type (Actual);
6497 and then Is_Tagged_Type (Gen_T)
6499 -- Traverse the list of primitives of the actual types
6500 -- searching for hidden primitives that are visible in the
6501 -- corresponding generic formal; leave them visible and
6502 -- append them to Result to restore their decoration later.
6504 Install_Hidden_Primitives
6505 (Prims_List => Result,
6507 Act_T => Entity (Subtype_Indication (Actual)));
6515 end Check_Hidden_Primitives;
6517 --------------------------
6518 -- Contains_Instance_Of --
6519 --------------------------
6521 function Contains_Instance_Of
6524 N : Node_Id) return Boolean
6532 -- Verify that there are no circular instantiations. We check whether
6533 -- the unit contains an instance of the current scope or some enclosing
6534 -- scope (in case one of the instances appears in a subunit). Longer
6535 -- circularities involving subunits might seem too pathological to
6536 -- consider, but they were not too pathological for the authors of
6537 -- DEC bc30vsq, so we loop over all enclosing scopes, and mark all
6538 -- enclosing generic scopes as containing an instance.
6541 -- Within a generic subprogram body, the scope is not generic, to
6542 -- allow for recursive subprograms. Use the declaration to determine
6543 -- whether this is a generic unit.
6545 if Ekind (Scop) = E_Generic_Package
6546 or else (Is_Subprogram (Scop)
6547 and then Nkind (Unit_Declaration_Node (Scop)) =
6548 N_Generic_Subprogram_Declaration)
6550 Elmt := First_Elmt (Inner_Instances (Inner));
6552 while Present (Elmt) loop
6553 if Node (Elmt) = Scop then
6554 Error_Msg_Node_2 := Inner;
6556 ("circular Instantiation: & instantiated within &!",
6560 elsif Node (Elmt) = Inner then
6563 elsif Contains_Instance_Of (Node (Elmt), Scop, N) then
6564 Error_Msg_Node_2 := Inner;
6566 ("circular Instantiation: & instantiated within &!",
6574 -- Indicate that Inner is being instantiated within Scop
6576 Append_Elmt (Inner, Inner_Instances (Scop));
6579 if Scop = Standard_Standard then
6582 Scop := Scope (Scop);
6587 end Contains_Instance_Of;
6589 -----------------------
6590 -- Copy_Generic_Node --
6591 -----------------------
6593 function Copy_Generic_Node
6595 Parent_Id : Node_Id;
6596 Instantiating : Boolean) return Node_Id
6601 function Copy_Generic_Descendant (D : Union_Id) return Union_Id;
6602 -- Check the given value of one of the Fields referenced by the current
6603 -- node to determine whether to copy it recursively. The field may hold
6604 -- a Node_Id, a List_Id, or an Elist_Id, or a plain value (Sloc, Uint,
6605 -- Char) in which case it need not be copied.
6607 procedure Copy_Descendants;
6608 -- Common utility for various nodes
6610 function Copy_Generic_Elist (E : Elist_Id) return Elist_Id;
6611 -- Make copy of element list
6613 function Copy_Generic_List
6615 Parent_Id : Node_Id) return List_Id;
6616 -- Apply Copy_Node recursively to the members of a node list
6618 function In_Defining_Unit_Name (Nam : Node_Id) return Boolean;
6619 -- True if an identifier is part of the defining program unit name of
6620 -- a child unit. The entity of such an identifier must be kept (for
6621 -- ASIS use) even though as the name of an enclosing generic it would
6622 -- otherwise not be preserved in the generic tree.
6624 ----------------------
6625 -- Copy_Descendants --
6626 ----------------------
6628 procedure Copy_Descendants is
6630 use Atree.Unchecked_Access;
6631 -- This code section is part of the implementation of an untyped
6632 -- tree traversal, so it needs direct access to node fields.
6635 Set_Field1 (New_N, Copy_Generic_Descendant (Field1 (N)));
6636 Set_Field2 (New_N, Copy_Generic_Descendant (Field2 (N)));
6637 Set_Field3 (New_N, Copy_Generic_Descendant (Field3 (N)));
6638 Set_Field4 (New_N, Copy_Generic_Descendant (Field4 (N)));
6639 Set_Field5 (New_N, Copy_Generic_Descendant (Field5 (N)));
6640 end Copy_Descendants;
6642 -----------------------------
6643 -- Copy_Generic_Descendant --
6644 -----------------------------
6646 function Copy_Generic_Descendant (D : Union_Id) return Union_Id is
6648 if D = Union_Id (Empty) then
6651 elsif D in Node_Range then
6653 (Copy_Generic_Node (Node_Id (D), New_N, Instantiating));
6655 elsif D in List_Range then
6656 return Union_Id (Copy_Generic_List (List_Id (D), New_N));
6658 elsif D in Elist_Range then
6659 return Union_Id (Copy_Generic_Elist (Elist_Id (D)));
6661 -- Nothing else is copyable (e.g. Uint values), return as is
6666 end Copy_Generic_Descendant;
6668 ------------------------
6669 -- Copy_Generic_Elist --
6670 ------------------------
6672 function Copy_Generic_Elist (E : Elist_Id) return Elist_Id is
6679 M := First_Elmt (E);
6680 while Present (M) loop
6682 (Copy_Generic_Node (Node (M), Empty, Instantiating), L);
6691 end Copy_Generic_Elist;
6693 -----------------------
6694 -- Copy_Generic_List --
6695 -----------------------
6697 function Copy_Generic_List
6699 Parent_Id : Node_Id) return List_Id
6707 Set_Parent (New_L, Parent_Id);
6710 while Present (N) loop
6711 Append (Copy_Generic_Node (N, Empty, Instantiating), New_L);
6720 end Copy_Generic_List;
6722 ---------------------------
6723 -- In_Defining_Unit_Name --
6724 ---------------------------
6726 function In_Defining_Unit_Name (Nam : Node_Id) return Boolean is
6728 return Present (Parent (Nam))
6729 and then (Nkind (Parent (Nam)) = N_Defining_Program_Unit_Name
6731 (Nkind (Parent (Nam)) = N_Expanded_Name
6732 and then In_Defining_Unit_Name (Parent (Nam))));
6733 end In_Defining_Unit_Name;
6735 -- Start of processing for Copy_Generic_Node
6742 New_N := New_Copy (N);
6744 -- Copy aspects if present
6746 if Has_Aspects (N) then
6747 Set_Has_Aspects (New_N, False);
6748 Set_Aspect_Specifications
6749 (New_N, Copy_Generic_List (Aspect_Specifications (N), Parent_Id));
6752 if Instantiating then
6753 Adjust_Instantiation_Sloc (New_N, S_Adjustment);
6756 if not Is_List_Member (N) then
6757 Set_Parent (New_N, Parent_Id);
6760 -- If defining identifier, then all fields have been copied already
6762 if Nkind (New_N) in N_Entity then
6765 -- Special casing for identifiers and other entity names and operators
6767 elsif Nkind_In (New_N, N_Identifier,
6768 N_Character_Literal,
6771 or else Nkind (New_N) in N_Op
6773 if not Instantiating then
6775 -- Link both nodes in order to assign subsequently the entity of
6776 -- the copy to the original node, in case this is a global
6779 Set_Associated_Node (N, New_N);
6781 -- If we are within an instantiation, this is a nested generic
6782 -- that has already been analyzed at the point of definition.
6783 -- We must preserve references that were global to the enclosing
6784 -- parent at that point. Other occurrences, whether global or
6785 -- local to the current generic, must be resolved anew, so we
6786 -- reset the entity in the generic copy. A global reference has a
6787 -- smaller depth than the parent, or else the same depth in case
6788 -- both are distinct compilation units.
6790 -- A child unit is implicitly declared within the enclosing parent
6791 -- but is in fact global to it, and must be preserved.
6793 -- It is also possible for Current_Instantiated_Parent to be
6794 -- defined, and for this not to be a nested generic, namely if
6795 -- the unit is loaded through Rtsfind. In that case, the entity of
6796 -- New_N is only a link to the associated node, and not a defining
6799 -- The entities for parent units in the defining_program_unit of a
6800 -- generic child unit are established when the context of the unit
6801 -- is first analyzed, before the generic copy is made. They are
6802 -- preserved in the copy for use in ASIS queries.
6804 Ent := Entity (New_N);
6806 if No (Current_Instantiated_Parent.Gen_Id) then
6808 or else Nkind (Ent) /= N_Defining_Identifier
6809 or else not In_Defining_Unit_Name (N)
6811 Set_Associated_Node (New_N, Empty);
6816 not Nkind_In (Ent, N_Defining_Identifier,
6817 N_Defining_Character_Literal,
6818 N_Defining_Operator_Symbol)
6819 or else No (Scope (Ent))
6821 (Scope (Ent) = Current_Instantiated_Parent.Gen_Id
6822 and then not Is_Child_Unit (Ent))
6824 (Scope_Depth (Scope (Ent)) >
6825 Scope_Depth (Current_Instantiated_Parent.Gen_Id)
6827 Get_Source_Unit (Ent) =
6828 Get_Source_Unit (Current_Instantiated_Parent.Gen_Id))
6830 Set_Associated_Node (New_N, Empty);
6833 -- Case of instantiating identifier or some other name or operator
6836 -- If the associated node is still defined, the entity in it
6837 -- is global, and must be copied to the instance. If this copy
6838 -- is being made for a body to inline, it is applied to an
6839 -- instantiated tree, and the entity is already present and
6840 -- must be also preserved.
6843 Assoc : constant Node_Id := Get_Associated_Node (N);
6846 if Present (Assoc) then
6847 if Nkind (Assoc) = Nkind (N) then
6848 Set_Entity (New_N, Entity (Assoc));
6849 Check_Private_View (N);
6851 -- The name in the call may be a selected component if the
6852 -- call has not been analyzed yet, as may be the case for
6853 -- pre/post conditions in a generic unit.
6855 elsif Nkind (Assoc) = N_Function_Call
6856 and then Is_Entity_Name (Name (Assoc))
6858 Set_Entity (New_N, Entity (Name (Assoc)));
6860 elsif Nkind_In (Assoc, N_Defining_Identifier,
6861 N_Defining_Character_Literal,
6862 N_Defining_Operator_Symbol)
6863 and then Expander_Active
6865 -- Inlining case: we are copying a tree that contains
6866 -- global entities, which are preserved in the copy to be
6867 -- used for subsequent inlining.
6872 Set_Entity (New_N, Empty);
6878 -- For expanded name, we must copy the Prefix and Selector_Name
6880 if Nkind (N) = N_Expanded_Name then
6882 (New_N, Copy_Generic_Node (Prefix (N), New_N, Instantiating));
6884 Set_Selector_Name (New_N,
6885 Copy_Generic_Node (Selector_Name (N), New_N, Instantiating));
6887 -- For operators, we must copy the right operand
6889 elsif Nkind (N) in N_Op then
6890 Set_Right_Opnd (New_N,
6891 Copy_Generic_Node (Right_Opnd (N), New_N, Instantiating));
6893 -- And for binary operators, the left operand as well
6895 if Nkind (N) in N_Binary_Op then
6896 Set_Left_Opnd (New_N,
6897 Copy_Generic_Node (Left_Opnd (N), New_N, Instantiating));
6901 -- Special casing for stubs
6903 elsif Nkind (N) in N_Body_Stub then
6905 -- In any case, we must copy the specification or defining
6906 -- identifier as appropriate.
6908 if Nkind (N) = N_Subprogram_Body_Stub then
6909 Set_Specification (New_N,
6910 Copy_Generic_Node (Specification (N), New_N, Instantiating));
6913 Set_Defining_Identifier (New_N,
6915 (Defining_Identifier (N), New_N, Instantiating));
6918 -- If we are not instantiating, then this is where we load and
6919 -- analyze subunits, i.e. at the point where the stub occurs. A
6920 -- more permissive system might defer this analysis to the point
6921 -- of instantiation, but this seems too complicated for now.
6923 if not Instantiating then
6925 Subunit_Name : constant Unit_Name_Type := Get_Unit_Name (N);
6927 Unum : Unit_Number_Type;
6931 -- Make sure that, if it is a subunit of the main unit that is
6932 -- preprocessed and if -gnateG is specified, the preprocessed
6933 -- file will be written.
6935 Lib.Analysing_Subunit_Of_Main :=
6936 Lib.In_Extended_Main_Source_Unit (N);
6939 (Load_Name => Subunit_Name,
6943 Lib.Analysing_Subunit_Of_Main := False;
6945 -- If the proper body is not found, a warning message will be
6946 -- emitted when analyzing the stub, or later at the point of
6947 -- instantiation. Here we just leave the stub as is.
6949 if Unum = No_Unit then
6950 Subunits_Missing := True;
6951 goto Subunit_Not_Found;
6954 Subunit := Cunit (Unum);
6956 if Nkind (Unit (Subunit)) /= N_Subunit then
6958 ("found child unit instead of expected SEPARATE subunit",
6960 Error_Msg_Sloc := Sloc (N);
6961 Error_Msg_N ("\to complete stub #", Subunit);
6962 goto Subunit_Not_Found;
6965 -- We must create a generic copy of the subunit, in order to
6966 -- perform semantic analysis on it, and we must replace the
6967 -- stub in the original generic unit with the subunit, in order
6968 -- to preserve non-local references within.
6970 -- Only the proper body needs to be copied. Library_Unit and
6971 -- context clause are simply inherited by the generic copy.
6972 -- Note that the copy (which may be recursive if there are
6973 -- nested subunits) must be done first, before attaching it to
6974 -- the enclosing generic.
6978 (Proper_Body (Unit (Subunit)),
6979 Empty, Instantiating => False);
6981 -- Now place the original proper body in the original generic
6982 -- unit. This is a body, not a compilation unit.
6984 Rewrite (N, Proper_Body (Unit (Subunit)));
6985 Set_Is_Compilation_Unit (Defining_Entity (N), False);
6986 Set_Was_Originally_Stub (N);
6988 -- Finally replace the body of the subunit with its copy, and
6989 -- make this new subunit into the library unit of the generic
6990 -- copy, which does not have stubs any longer.
6992 Set_Proper_Body (Unit (Subunit), New_Body);
6993 Set_Library_Unit (New_N, Subunit);
6994 Inherit_Context (Unit (Subunit), N);
6997 -- If we are instantiating, this must be an error case, since
6998 -- otherwise we would have replaced the stub node by the proper body
6999 -- that corresponds. So just ignore it in the copy (i.e. we have
7000 -- copied it, and that is good enough).
7006 <<Subunit_Not_Found>> null;
7008 -- If the node is a compilation unit, it is the subunit of a stub, which
7009 -- has been loaded already (see code below). In this case, the library
7010 -- unit field of N points to the parent unit (which is a compilation
7011 -- unit) and need not (and cannot) be copied.
7013 -- When the proper body of the stub is analyzed, the library_unit link
7014 -- is used to establish the proper context (see sem_ch10).
7016 -- The other fields of a compilation unit are copied as usual
7018 elsif Nkind (N) = N_Compilation_Unit then
7020 -- This code can only be executed when not instantiating, because in
7021 -- the copy made for an instantiation, the compilation unit node has
7022 -- disappeared at the point that a stub is replaced by its proper
7025 pragma Assert (not Instantiating);
7027 Set_Context_Items (New_N,
7028 Copy_Generic_List (Context_Items (N), New_N));
7031 Copy_Generic_Node (Unit (N), New_N, False));
7033 Set_First_Inlined_Subprogram (New_N,
7035 (First_Inlined_Subprogram (N), New_N, False));
7037 Set_Aux_Decls_Node (New_N,
7038 Copy_Generic_Node (Aux_Decls_Node (N), New_N, False));
7040 -- For an assignment node, the assignment is known to be semantically
7041 -- legal if we are instantiating the template. This avoids incorrect
7042 -- diagnostics in generated code.
7044 elsif Nkind (N) = N_Assignment_Statement then
7046 -- Copy name and expression fields in usual manner
7049 Copy_Generic_Node (Name (N), New_N, Instantiating));
7051 Set_Expression (New_N,
7052 Copy_Generic_Node (Expression (N), New_N, Instantiating));
7054 if Instantiating then
7055 Set_Assignment_OK (Name (New_N), True);
7058 elsif Nkind_In (N, N_Aggregate, N_Extension_Aggregate) then
7059 if not Instantiating then
7060 Set_Associated_Node (N, New_N);
7063 if Present (Get_Associated_Node (N))
7064 and then Nkind (Get_Associated_Node (N)) = Nkind (N)
7066 -- In the generic the aggregate has some composite type. If at
7067 -- the point of instantiation the type has a private view,
7068 -- install the full view (and that of its ancestors, if any).
7071 T : Entity_Id := (Etype (Get_Associated_Node (New_N)));
7076 and then Is_Private_Type (T)
7082 and then Is_Tagged_Type (T)
7083 and then Is_Derived_Type (T)
7085 Rt := Root_Type (T);
7090 if Is_Private_Type (T) then
7101 -- Do not copy the associated node, which points to the generic copy
7102 -- of the aggregate.
7105 use Atree.Unchecked_Access;
7106 -- This code section is part of the implementation of an untyped
7107 -- tree traversal, so it needs direct access to node fields.
7110 Set_Field1 (New_N, Copy_Generic_Descendant (Field1 (N)));
7111 Set_Field2 (New_N, Copy_Generic_Descendant (Field2 (N)));
7112 Set_Field3 (New_N, Copy_Generic_Descendant (Field3 (N)));
7113 Set_Field5 (New_N, Copy_Generic_Descendant (Field5 (N)));
7116 -- Allocators do not have an identifier denoting the access type, so we
7117 -- must locate it through the expression to check whether the views are
7120 elsif Nkind (N) = N_Allocator
7121 and then Nkind (Expression (N)) = N_Qualified_Expression
7122 and then Is_Entity_Name (Subtype_Mark (Expression (N)))
7123 and then Instantiating
7126 T : constant Node_Id :=
7127 Get_Associated_Node (Subtype_Mark (Expression (N)));
7133 -- Retrieve the allocator node in the generic copy
7135 Acc_T := Etype (Parent (Parent (T)));
7137 and then Is_Private_Type (Acc_T)
7139 Switch_View (Acc_T);
7146 -- For a proper body, we must catch the case of a proper body that
7147 -- replaces a stub. This represents the point at which a separate
7148 -- compilation unit, and hence template file, may be referenced, so we
7149 -- must make a new source instantiation entry for the template of the
7150 -- subunit, and ensure that all nodes in the subunit are adjusted using
7151 -- this new source instantiation entry.
7153 elsif Nkind (N) in N_Proper_Body then
7155 Save_Adjustment : constant Sloc_Adjustment := S_Adjustment;
7158 if Instantiating and then Was_Originally_Stub (N) then
7159 Create_Instantiation_Source
7160 (Instantiation_Node,
7161 Defining_Entity (N),
7166 -- Now copy the fields of the proper body, using the new
7167 -- adjustment factor if one was needed as per test above.
7171 -- Restore the original adjustment factor in case changed
7173 S_Adjustment := Save_Adjustment;
7176 -- Don't copy Ident or Comment pragmas, since the comment belongs to the
7177 -- generic unit, not to the instantiating unit.
7179 elsif Nkind (N) = N_Pragma and then Instantiating then
7181 Prag_Id : constant Pragma_Id := Get_Pragma_Id (N);
7183 if Prag_Id = Pragma_Ident or else Prag_Id = Pragma_Comment then
7184 New_N := Make_Null_Statement (Sloc (N));
7190 elsif Nkind_In (N, N_Integer_Literal, N_Real_Literal) then
7192 -- No descendant fields need traversing
7196 elsif Nkind (N) = N_String_Literal
7197 and then Present (Etype (N))
7198 and then Instantiating
7200 -- If the string is declared in an outer scope, the string_literal
7201 -- subtype created for it may have the wrong scope. We force the
7202 -- reanalysis of the constant to generate a new itype in the proper
7205 Set_Etype (New_N, Empty);
7206 Set_Analyzed (New_N, False);
7208 -- For the remaining nodes, copy their descendants recursively
7213 if Instantiating and then Nkind (N) = N_Subprogram_Body then
7214 Set_Generic_Parent (Specification (New_N), N);
7216 -- Should preserve Corresponding_Spec??? (12.3(14))
7221 end Copy_Generic_Node;
7223 ----------------------------
7224 -- Denotes_Formal_Package --
7225 ----------------------------
7227 function Denotes_Formal_Package
7229 On_Exit : Boolean := False;
7230 Instance : Entity_Id := Empty) return Boolean
7233 Scop : constant Entity_Id := Scope (Pack);
7236 function Is_Actual_Of_Previous_Formal (P : Entity_Id) return Boolean;
7237 -- The package in question may be an actual for a previous formal
7238 -- package P of the current instance, so examine its actuals as well.
7239 -- This must be recursive over other formal packages.
7241 ----------------------------------
7242 -- Is_Actual_Of_Previous_Formal --
7243 ----------------------------------
7245 function Is_Actual_Of_Previous_Formal (P : Entity_Id) return Boolean is
7249 E1 := First_Entity (P);
7250 while Present (E1) and then E1 /= Instance loop
7251 if Ekind (E1) = E_Package
7252 and then Nkind (Parent (E1)) = N_Package_Renaming_Declaration
7254 if Renamed_Object (E1) = Pack then
7257 elsif E1 = P or else Renamed_Object (E1) = P then
7260 elsif Is_Actual_Of_Previous_Formal (E1) then
7269 end Is_Actual_Of_Previous_Formal;
7271 -- Start of processing for Denotes_Formal_Package
7277 (Instance_Envs.Last).Instantiated_Parent.Act_Id;
7279 Par := Current_Instantiated_Parent.Act_Id;
7282 if Ekind (Scop) = E_Generic_Package
7283 or else Nkind (Unit_Declaration_Node (Scop)) =
7284 N_Generic_Subprogram_Declaration
7288 elsif Nkind (Original_Node (Unit_Declaration_Node (Pack))) =
7289 N_Formal_Package_Declaration
7297 -- Check whether this package is associated with a formal package of
7298 -- the enclosing instantiation. Iterate over the list of renamings.
7300 E := First_Entity (Par);
7301 while Present (E) loop
7302 if Ekind (E) /= E_Package
7303 or else Nkind (Parent (E)) /= N_Package_Renaming_Declaration
7307 elsif Renamed_Object (E) = Par then
7310 elsif Renamed_Object (E) = Pack then
7313 elsif Is_Actual_Of_Previous_Formal (E) then
7323 end Denotes_Formal_Package;
7329 procedure End_Generic is
7331 -- ??? More things could be factored out in this routine. Should
7332 -- probably be done at a later stage.
7334 Inside_A_Generic := Generic_Flags.Table (Generic_Flags.Last);
7335 Generic_Flags.Decrement_Last;
7337 Expander_Mode_Restore;
7344 function Earlier (N1, N2 : Node_Id) return Boolean is
7345 procedure Find_Depth (P : in out Node_Id; D : in out Integer);
7346 -- Find distance from given node to enclosing compilation unit
7352 procedure Find_Depth (P : in out Node_Id; D : in out Integer) is
7355 and then Nkind (P) /= N_Compilation_Unit
7357 P := True_Parent (P);
7362 -- Local declarations
7371 -- Start of processing for Earlier
7374 Find_Depth (P1, D1);
7375 Find_Depth (P2, D2);
7385 P1 := True_Parent (P1);
7390 P2 := True_Parent (P2);
7394 -- At this point P1 and P2 are at the same distance from the root.
7395 -- We examine their parents until we find a common declarative list.
7396 -- If we reach the root, N1 and N2 do not descend from the same
7397 -- declarative list (e.g. one is nested in the declarative part and
7398 -- the other is in a block in the statement part) and the earlier
7399 -- one is already frozen.
7401 while not Is_List_Member (P1)
7402 or else not Is_List_Member (P2)
7403 or else List_Containing (P1) /= List_Containing (P2)
7405 P1 := True_Parent (P1);
7406 P2 := True_Parent (P2);
7408 if Nkind (Parent (P1)) = N_Subunit then
7409 P1 := Corresponding_Stub (Parent (P1));
7412 if Nkind (Parent (P2)) = N_Subunit then
7413 P2 := Corresponding_Stub (Parent (P2));
7421 -- Expanded code usually shares the source location of the original
7422 -- construct it was generated for. This however may not necessarely
7423 -- reflect the true location of the code within the tree.
7425 -- Before comparing the slocs of the two nodes, make sure that we are
7426 -- working with correct source locations. Assume that P1 is to the left
7427 -- of P2. If either one does not come from source, traverse the common
7428 -- list heading towards the other node and locate the first source
7432 -- ----+===+===+--------------+===+===+----
7433 -- expanded code expanded code
7435 if not Comes_From_Source (P1) then
7436 while Present (P1) loop
7438 -- Neither P2 nor a source statement were located during the
7439 -- search. If we reach the end of the list, then P1 does not
7440 -- occur earlier than P2.
7443 -- start --- P2 ----- P1 --- end
7445 if No (Next (P1)) then
7448 -- We encounter P2 while going to the right of the list. This
7449 -- means that P1 does indeed appear earlier.
7452 -- start --- P1 ===== P2 --- end
7453 -- expanded code in between
7458 -- No need to look any further since we have located a source
7461 elsif Comes_From_Source (P1) then
7471 if not Comes_From_Source (P2) then
7472 while Present (P2) loop
7474 -- Neither P1 nor a source statement were located during the
7475 -- search. If we reach the start of the list, then P1 does not
7476 -- occur earlier than P2.
7479 -- start --- P2 --- P1 --- end
7481 if No (Prev (P2)) then
7484 -- We encounter P1 while going to the left of the list. This
7485 -- means that P1 does indeed appear earlier.
7488 -- start --- P1 ===== P2 --- end
7489 -- expanded code in between
7494 -- No need to look any further since we have located a source
7497 elsif Comes_From_Source (P2) then
7507 -- At this point either both nodes came from source or we approximated
7508 -- their source locations through neighbouring source statements.
7510 T1 := Top_Level_Location (Sloc (P1));
7511 T2 := Top_Level_Location (Sloc (P2));
7513 -- When two nodes come from the same instance, they have identical top
7514 -- level locations. To determine proper relation within the tree, check
7515 -- their locations within the template.
7518 return Sloc (P1) < Sloc (P2);
7520 -- The two nodes either come from unrelated instances or do not come
7521 -- from instantiated code at all.
7528 ----------------------
7529 -- Find_Actual_Type --
7530 ----------------------
7532 function Find_Actual_Type
7534 Gen_Type : Entity_Id) return Entity_Id
7536 Gen_Scope : constant Entity_Id := Scope (Gen_Type);
7540 -- Special processing only applies to child units
7542 if not Is_Child_Unit (Gen_Scope) then
7543 return Get_Instance_Of (Typ);
7545 -- If designated or component type is itself a formal of the child unit,
7546 -- its instance is available.
7548 elsif Scope (Typ) = Gen_Scope then
7549 return Get_Instance_Of (Typ);
7551 -- If the array or access type is not declared in the parent unit,
7552 -- no special processing needed.
7554 elsif not Is_Generic_Type (Typ)
7555 and then Scope (Gen_Scope) /= Scope (Typ)
7557 return Get_Instance_Of (Typ);
7559 -- Otherwise, retrieve designated or component type by visibility
7562 T := Current_Entity (Typ);
7563 while Present (T) loop
7564 if In_Open_Scopes (Scope (T)) then
7567 elsif Is_Generic_Actual_Type (T) then
7576 end Find_Actual_Type;
7578 ----------------------------
7579 -- Freeze_Subprogram_Body --
7580 ----------------------------
7582 procedure Freeze_Subprogram_Body
7583 (Inst_Node : Node_Id;
7585 Pack_Id : Entity_Id)
7587 Gen_Unit : constant Entity_Id := Get_Generic_Entity (Inst_Node);
7588 Par : constant Entity_Id := Scope (Gen_Unit);
7594 function Enclosing_Package_Body (N : Node_Id) return Node_Id;
7595 -- Find innermost package body that encloses the given node, and which
7596 -- is not a compilation unit. Freeze nodes for the instance, or for its
7597 -- enclosing body, may be inserted after the enclosing_body of the
7598 -- generic unit. Used to determine proper placement of freeze node for
7599 -- both package and subprogram instances.
7601 function Package_Freeze_Node (B : Node_Id) return Node_Id;
7602 -- Find entity for given package body, and locate or create a freeze
7605 ----------------------------
7606 -- Enclosing_Package_Body --
7607 ----------------------------
7609 function Enclosing_Package_Body (N : Node_Id) return Node_Id is
7615 and then Nkind (Parent (P)) /= N_Compilation_Unit
7617 if Nkind (P) = N_Package_Body then
7618 if Nkind (Parent (P)) = N_Subunit then
7619 return Corresponding_Stub (Parent (P));
7625 P := True_Parent (P);
7629 end Enclosing_Package_Body;
7631 -------------------------
7632 -- Package_Freeze_Node --
7633 -------------------------
7635 function Package_Freeze_Node (B : Node_Id) return Node_Id is
7639 if Nkind (B) = N_Package_Body then
7640 Id := Corresponding_Spec (B);
7641 else pragma Assert (Nkind (B) = N_Package_Body_Stub);
7642 Id := Corresponding_Spec (Proper_Body (Unit (Library_Unit (B))));
7645 Ensure_Freeze_Node (Id);
7646 return Freeze_Node (Id);
7647 end Package_Freeze_Node;
7649 -- Start of processing of Freeze_Subprogram_Body
7652 -- If the instance and the generic body appear within the same unit, and
7653 -- the instance precedes the generic, the freeze node for the instance
7654 -- must appear after that of the generic. If the generic is nested
7655 -- within another instance I2, then current instance must be frozen
7656 -- after I2. In both cases, the freeze nodes are those of enclosing
7657 -- packages. Otherwise, the freeze node is placed at the end of the
7658 -- current declarative part.
7660 Enc_G := Enclosing_Package_Body (Gen_Body);
7661 Enc_I := Enclosing_Package_Body (Inst_Node);
7662 Ensure_Freeze_Node (Pack_Id);
7663 F_Node := Freeze_Node (Pack_Id);
7665 if Is_Generic_Instance (Par)
7666 and then Present (Freeze_Node (Par))
7667 and then In_Same_Declarative_Part (Freeze_Node (Par), Inst_Node)
7669 -- The parent was a premature instantiation. Insert freeze node at
7670 -- the end the current declarative part.
7672 if ABE_Is_Certain (Get_Package_Instantiation_Node (Par)) then
7673 Insert_Freeze_Node_For_Instance (Inst_Node, F_Node);
7675 -- Handle the following case:
7677 -- package Parent_Inst is new ...
7680 -- procedure P ... -- this body freezes Parent_Inst
7682 -- package Inst is new ...
7684 -- In this particular scenario, the freeze node for Inst must be
7685 -- inserted in the same manner as that of Parent_Inst - before the
7686 -- next source body or at the end of the declarative list (body not
7687 -- available). If body P did not exist and Parent_Inst was frozen
7688 -- after Inst, either by a body following Inst or at the end of the
7689 -- declarative region, the freeze node for Inst must be inserted
7690 -- after that of Parent_Inst. This relation is established by
7691 -- comparing the Slocs of Parent_Inst freeze node and Inst.
7693 elsif List_Containing (Get_Package_Instantiation_Node (Par)) =
7694 List_Containing (Inst_Node)
7695 and then Sloc (Freeze_Node (Par)) < Sloc (Inst_Node)
7697 Insert_Freeze_Node_For_Instance (Inst_Node, F_Node);
7700 Insert_After (Freeze_Node (Par), F_Node);
7703 -- The body enclosing the instance should be frozen after the body that
7704 -- includes the generic, because the body of the instance may make
7705 -- references to entities therein. If the two are not in the same
7706 -- declarative part, or if the one enclosing the instance is frozen
7707 -- already, freeze the instance at the end of the current declarative
7710 elsif Is_Generic_Instance (Par)
7711 and then Present (Freeze_Node (Par))
7712 and then Present (Enc_I)
7714 if In_Same_Declarative_Part (Freeze_Node (Par), Enc_I)
7716 (Nkind (Enc_I) = N_Package_Body
7718 In_Same_Declarative_Part (Freeze_Node (Par), Parent (Enc_I)))
7720 -- The enclosing package may contain several instances. Rather
7721 -- than computing the earliest point at which to insert its freeze
7722 -- node, we place it at the end of the declarative part of the
7723 -- parent of the generic.
7725 Insert_Freeze_Node_For_Instance
7726 (Freeze_Node (Par), Package_Freeze_Node (Enc_I));
7729 Insert_Freeze_Node_For_Instance (Inst_Node, F_Node);
7731 elsif Present (Enc_G)
7732 and then Present (Enc_I)
7733 and then Enc_G /= Enc_I
7734 and then Earlier (Inst_Node, Gen_Body)
7736 if Nkind (Enc_G) = N_Package_Body then
7737 E_G_Id := Corresponding_Spec (Enc_G);
7738 else pragma Assert (Nkind (Enc_G) = N_Package_Body_Stub);
7740 Corresponding_Spec (Proper_Body (Unit (Library_Unit (Enc_G))));
7743 -- Freeze package that encloses instance, and place node after the
7744 -- package that encloses generic. If enclosing package is already
7745 -- frozen we have to assume it is at the proper place. This may be a
7746 -- potential ABE that requires dynamic checking. Do not add a freeze
7747 -- node if the package that encloses the generic is inside the body
7748 -- that encloses the instance, because the freeze node would be in
7749 -- the wrong scope. Additional contortions needed if the bodies are
7750 -- within a subunit.
7753 Enclosing_Body : Node_Id;
7756 if Nkind (Enc_I) = N_Package_Body_Stub then
7757 Enclosing_Body := Proper_Body (Unit (Library_Unit (Enc_I)));
7759 Enclosing_Body := Enc_I;
7762 if Parent (List_Containing (Enc_G)) /= Enclosing_Body then
7763 Insert_Freeze_Node_For_Instance
7764 (Enc_G, Package_Freeze_Node (Enc_I));
7768 -- Freeze enclosing subunit before instance
7770 Ensure_Freeze_Node (E_G_Id);
7772 if not Is_List_Member (Freeze_Node (E_G_Id)) then
7773 Insert_After (Enc_G, Freeze_Node (E_G_Id));
7776 Insert_Freeze_Node_For_Instance (Inst_Node, F_Node);
7779 -- If none of the above, insert freeze node at the end of the current
7780 -- declarative part.
7782 Insert_Freeze_Node_For_Instance (Inst_Node, F_Node);
7784 end Freeze_Subprogram_Body;
7790 function Get_Gen_Id (E : Assoc_Ptr) return Entity_Id is
7792 return Generic_Renamings.Table (E).Gen_Id;
7795 ---------------------
7796 -- Get_Instance_Of --
7797 ---------------------
7799 function Get_Instance_Of (A : Entity_Id) return Entity_Id is
7800 Res : constant Assoc_Ptr := Generic_Renamings_HTable.Get (A);
7803 if Res /= Assoc_Null then
7804 return Generic_Renamings.Table (Res).Act_Id;
7806 -- On exit, entity is not instantiated: not a generic parameter, or
7807 -- else parameter of an inner generic unit.
7811 end Get_Instance_Of;
7813 ------------------------------------
7814 -- Get_Package_Instantiation_Node --
7815 ------------------------------------
7817 function Get_Package_Instantiation_Node (A : Entity_Id) return Node_Id is
7818 Decl : Node_Id := Unit_Declaration_Node (A);
7822 -- If the Package_Instantiation attribute has been set on the package
7823 -- entity, then use it directly when it (or its Original_Node) refers
7824 -- to an N_Package_Instantiation node. In principle it should be
7825 -- possible to have this field set in all cases, which should be
7826 -- investigated, and would allow this function to be significantly
7829 Inst := Package_Instantiation (A);
7831 if Present (Inst) then
7832 if Nkind (Inst) = N_Package_Instantiation then
7835 elsif Nkind (Original_Node (Inst)) = N_Package_Instantiation then
7836 return Original_Node (Inst);
7840 -- If the instantiation is a compilation unit that does not need body
7841 -- then the instantiation node has been rewritten as a package
7842 -- declaration for the instance, and we return the original node.
7844 -- If it is a compilation unit and the instance node has not been
7845 -- rewritten, then it is still the unit of the compilation. Finally, if
7846 -- a body is present, this is a parent of the main unit whose body has
7847 -- been compiled for inlining purposes, and the instantiation node has
7848 -- been rewritten with the instance body.
7850 -- Otherwise the instantiation node appears after the declaration. If
7851 -- the entity is a formal package, the declaration may have been
7852 -- rewritten as a generic declaration (in the case of a formal with box)
7853 -- or left as a formal package declaration if it has actuals, and is
7854 -- found with a forward search.
7856 if Nkind (Parent (Decl)) = N_Compilation_Unit then
7857 if Nkind (Decl) = N_Package_Declaration
7858 and then Present (Corresponding_Body (Decl))
7860 Decl := Unit_Declaration_Node (Corresponding_Body (Decl));
7863 if Nkind (Original_Node (Decl)) = N_Package_Instantiation then
7864 return Original_Node (Decl);
7866 return Unit (Parent (Decl));
7869 elsif Nkind (Decl) = N_Package_Declaration
7870 and then Nkind (Original_Node (Decl)) = N_Formal_Package_Declaration
7872 return Original_Node (Decl);
7875 Inst := Next (Decl);
7876 while not Nkind_In (Inst, N_Package_Instantiation,
7877 N_Formal_Package_Declaration)
7884 end Get_Package_Instantiation_Node;
7886 ------------------------
7887 -- Has_Been_Exchanged --
7888 ------------------------
7890 function Has_Been_Exchanged (E : Entity_Id) return Boolean is
7894 Next := First_Elmt (Exchanged_Views);
7895 while Present (Next) loop
7896 if Full_View (Node (Next)) = E then
7904 end Has_Been_Exchanged;
7910 function Hash (F : Entity_Id) return HTable_Range is
7912 return HTable_Range (F mod HTable_Size);
7915 ------------------------
7916 -- Hide_Current_Scope --
7917 ------------------------
7919 procedure Hide_Current_Scope is
7920 C : constant Entity_Id := Current_Scope;
7924 Set_Is_Hidden_Open_Scope (C);
7926 E := First_Entity (C);
7927 while Present (E) loop
7928 if Is_Immediately_Visible (E) then
7929 Set_Is_Immediately_Visible (E, False);
7930 Append_Elmt (E, Hidden_Entities);
7936 -- Make the scope name invisible as well. This is necessary, but might
7937 -- conflict with calls to Rtsfind later on, in case the scope is a
7938 -- predefined one. There is no clean solution to this problem, so for
7939 -- now we depend on the user not redefining Standard itself in one of
7940 -- the parent units.
7942 if Is_Immediately_Visible (C) and then C /= Standard_Standard then
7943 Set_Is_Immediately_Visible (C, False);
7944 Append_Elmt (C, Hidden_Entities);
7947 end Hide_Current_Scope;
7953 procedure Init_Env is
7954 Saved : Instance_Env;
7957 Saved.Instantiated_Parent := Current_Instantiated_Parent;
7958 Saved.Exchanged_Views := Exchanged_Views;
7959 Saved.Hidden_Entities := Hidden_Entities;
7960 Saved.Current_Sem_Unit := Current_Sem_Unit;
7961 Saved.Parent_Unit_Visible := Parent_Unit_Visible;
7962 Saved.Instance_Parent_Unit := Instance_Parent_Unit;
7964 -- Save configuration switches. These may be reset if the unit is a
7965 -- predefined unit, and the current mode is not Ada 2005.
7967 Save_Opt_Config_Switches (Saved.Switches);
7969 Instance_Envs.Append (Saved);
7971 Exchanged_Views := New_Elmt_List;
7972 Hidden_Entities := New_Elmt_List;
7974 -- Make dummy entry for Instantiated parent. If generic unit is legal,
7975 -- this is set properly in Set_Instance_Env.
7977 Current_Instantiated_Parent :=
7978 (Current_Scope, Current_Scope, Assoc_Null);
7981 ------------------------------
7982 -- In_Same_Declarative_Part --
7983 ------------------------------
7985 function In_Same_Declarative_Part
7987 Inst : Node_Id) return Boolean
7989 Decls : constant Node_Id := Parent (F_Node);
7990 Nod : Node_Id := Parent (Inst);
7993 while Present (Nod) loop
7997 elsif Nkind_In (Nod, N_Subprogram_Body,
7999 N_Package_Declaration,
8006 elsif Nkind (Nod) = N_Subunit then
8007 Nod := Corresponding_Stub (Nod);
8009 elsif Nkind (Nod) = N_Compilation_Unit then
8013 Nod := Parent (Nod);
8018 end In_Same_Declarative_Part;
8020 ---------------------
8021 -- In_Main_Context --
8022 ---------------------
8024 function In_Main_Context (E : Entity_Id) return Boolean is
8030 if not Is_Compilation_Unit (E)
8031 or else Ekind (E) /= E_Package
8032 or else In_Private_Part (E)
8037 Context := Context_Items (Cunit (Main_Unit));
8039 Clause := First (Context);
8040 while Present (Clause) loop
8041 if Nkind (Clause) = N_With_Clause then
8042 Nam := Name (Clause);
8044 -- If the current scope is part of the context of the main unit,
8045 -- analysis of the corresponding with_clause is not complete, and
8046 -- the entity is not set. We use the Chars field directly, which
8047 -- might produce false positives in rare cases, but guarantees
8048 -- that we produce all the instance bodies we will need.
8050 if (Is_Entity_Name (Nam) and then Chars (Nam) = Chars (E))
8051 or else (Nkind (Nam) = N_Selected_Component
8052 and then Chars (Selector_Name (Nam)) = Chars (E))
8062 end In_Main_Context;
8064 ---------------------
8065 -- Inherit_Context --
8066 ---------------------
8068 procedure Inherit_Context (Gen_Decl : Node_Id; Inst : Node_Id) is
8069 Current_Context : List_Id;
8070 Current_Unit : Node_Id;
8079 if Nkind (Parent (Gen_Decl)) = N_Compilation_Unit then
8081 -- The inherited context is attached to the enclosing compilation
8082 -- unit. This is either the main unit, or the declaration for the
8083 -- main unit (in case the instantiation appears within the package
8084 -- declaration and the main unit is its body).
8086 Current_Unit := Parent (Inst);
8087 while Present (Current_Unit)
8088 and then Nkind (Current_Unit) /= N_Compilation_Unit
8090 Current_Unit := Parent (Current_Unit);
8093 Current_Context := Context_Items (Current_Unit);
8095 Item := First (Context_Items (Parent (Gen_Decl)));
8096 while Present (Item) loop
8097 if Nkind (Item) = N_With_Clause then
8098 Lib_Unit := Library_Unit (Item);
8100 -- Take care to prevent direct cyclic with's
8102 if Lib_Unit /= Current_Unit then
8104 -- Do not add a unit if it is already in the context
8106 Clause := First (Current_Context);
8108 while Present (Clause) loop
8109 if Nkind (Clause) = N_With_Clause and then
8110 Library_Unit (Clause) = Lib_Unit
8120 New_I := New_Copy (Item);
8121 Set_Implicit_With (New_I, True);
8122 Set_Implicit_With_From_Instantiation (New_I, True);
8123 Append (New_I, Current_Context);
8131 end Inherit_Context;
8137 procedure Initialize is
8139 Generic_Renamings.Init;
8142 Generic_Renamings_HTable.Reset;
8143 Circularity_Detected := False;
8144 Exchanged_Views := No_Elist;
8145 Hidden_Entities := No_Elist;
8148 -------------------------------------
8149 -- Insert_Freeze_Node_For_Instance --
8150 -------------------------------------
8152 procedure Insert_Freeze_Node_For_Instance
8161 function Enclosing_Body (N : Node_Id) return Node_Id;
8162 -- Find enclosing package or subprogram body, if any. Freeze node may
8163 -- be placed at end of current declarative list if previous instance
8164 -- and current one have different enclosing bodies.
8166 function Previous_Instance (Gen : Entity_Id) return Entity_Id;
8167 -- Find the local instance, if any, that declares the generic that is
8168 -- being instantiated. If present, the freeze node for this instance
8169 -- must follow the freeze node for the previous instance.
8171 --------------------
8172 -- Enclosing_Body --
8173 --------------------
8175 function Enclosing_Body (N : Node_Id) return Node_Id is
8181 and then Nkind (Parent (P)) /= N_Compilation_Unit
8183 if Nkind_In (P, N_Package_Body, N_Subprogram_Body) then
8184 if Nkind (Parent (P)) = N_Subunit then
8185 return Corresponding_Stub (Parent (P));
8191 P := True_Parent (P);
8197 -----------------------
8198 -- Previous_Instance --
8199 -----------------------
8201 function Previous_Instance (Gen : Entity_Id) return Entity_Id is
8207 and then S /= Standard_Standard
8209 if Is_Generic_Instance (S)
8210 and then In_Same_Source_Unit (S, N)
8219 end Previous_Instance;
8221 -- Start of processing for Insert_Freeze_Node_For_Instance
8224 if not Is_List_Member (F_Node) then
8226 Decls := List_Containing (N);
8227 Inst := Entity (F_Node);
8228 Par_N := Parent (Decls);
8230 -- When processing a subprogram instantiation, utilize the actual
8231 -- subprogram instantiation rather than its package wrapper as it
8232 -- carries all the context information.
8234 if Is_Wrapper_Package (Inst) then
8235 Inst := Related_Instance (Inst);
8238 -- If this is a package instance, check whether the generic is
8239 -- declared in a previous instance and the current instance is
8240 -- not within the previous one.
8242 if Present (Generic_Parent (Parent (Inst)))
8243 and then Is_In_Main_Unit (N)
8246 Enclosing_N : constant Node_Id := Enclosing_Body (N);
8247 Par_I : constant Entity_Id :=
8249 (Generic_Parent (Parent (Inst)));
8254 and then Earlier (N, Freeze_Node (Par_I))
8256 Scop := Scope (Inst);
8258 -- If the current instance is within the one that contains
8259 -- the generic, the freeze node for the current one must
8260 -- appear in the current declarative part. Ditto, if the
8261 -- current instance is within another package instance or
8262 -- within a body that does not enclose the current instance.
8263 -- In these three cases the freeze node of the previous
8264 -- instance is not relevant.
8266 while Present (Scop)
8267 and then Scop /= Standard_Standard
8269 exit when Scop = Par_I
8271 (Is_Generic_Instance (Scop)
8272 and then Scope_Depth (Scop) > Scope_Depth (Par_I));
8273 Scop := Scope (Scop);
8276 -- Previous instance encloses current instance
8278 if Scop = Par_I then
8281 -- If the next node is a source body we must freeze in
8282 -- the current scope as well.
8284 elsif Present (Next (N))
8285 and then Nkind_In (Next (N),
8286 N_Subprogram_Body, N_Package_Body)
8287 and then Comes_From_Source (Next (N))
8291 -- Current instance is within an unrelated instance
8293 elsif Is_Generic_Instance (Scop) then
8296 -- Current instance is within an unrelated body
8298 elsif Present (Enclosing_N)
8299 and then Enclosing_N /= Enclosing_Body (Par_I)
8304 Insert_After (Freeze_Node (Par_I), F_Node);
8311 -- When the instantiation occurs in a package declaration, append the
8312 -- freeze node to the private declarations (if any).
8314 if Nkind (Par_N) = N_Package_Specification
8315 and then Decls = Visible_Declarations (Par_N)
8316 and then Present (Private_Declarations (Par_N))
8317 and then not Is_Empty_List (Private_Declarations (Par_N))
8319 Decls := Private_Declarations (Par_N);
8320 Decl := First (Decls);
8323 -- Determine the proper freeze point of a package instantiation. We
8324 -- adhere to the general rule of a package or subprogram body causing
8325 -- freezing of anything before it in the same declarative region. In
8326 -- this case, the proper freeze point of a package instantiation is
8327 -- before the first source body which follows, or before a stub. This
8328 -- ensures that entities coming from the instance are already frozen
8329 -- and usable in source bodies.
8331 if Nkind (Par_N) /= N_Package_Declaration
8332 and then Ekind (Inst) = E_Package
8333 and then Is_Generic_Instance (Inst)
8335 not In_Same_Source_Unit (Generic_Parent (Parent (Inst)), Inst)
8337 while Present (Decl) loop
8338 if (Nkind (Decl) in N_Unit_Body
8340 Nkind (Decl) in N_Body_Stub)
8341 and then Comes_From_Source (Decl)
8343 Insert_Before (Decl, F_Node);
8351 -- In a package declaration, or if no previous body, insert at end
8354 Set_Sloc (F_Node, Sloc (Last (Decls)));
8355 Insert_After (Last (Decls), F_Node);
8357 end Insert_Freeze_Node_For_Instance;
8363 procedure Install_Body
8364 (Act_Body : Node_Id;
8369 Act_Id : constant Entity_Id := Corresponding_Spec (Act_Body);
8370 Act_Unit : constant Node_Id := Unit (Cunit (Get_Source_Unit (N)));
8371 Gen_Id : constant Entity_Id := Corresponding_Spec (Gen_Body);
8372 Par : constant Entity_Id := Scope (Gen_Id);
8373 Gen_Unit : constant Node_Id :=
8374 Unit (Cunit (Get_Source_Unit (Gen_Decl)));
8375 Orig_Body : Node_Id := Gen_Body;
8377 Body_Unit : Node_Id;
8379 Must_Delay : Boolean;
8381 function In_Same_Enclosing_Subp return Boolean;
8382 -- Check whether instance and generic body are within same subprogram.
8384 function True_Sloc (N : Node_Id) return Source_Ptr;
8385 -- If the instance is nested inside a generic unit, the Sloc of the
8386 -- instance indicates the place of the original definition, not the
8387 -- point of the current enclosing instance. Pending a better usage of
8388 -- Slocs to indicate instantiation places, we determine the place of
8389 -- origin of a node by finding the maximum sloc of any ancestor node.
8390 -- Why is this not equivalent to Top_Level_Location ???
8392 ----------------------------
8393 -- In_Same_Enclosing_Subp --
8394 ----------------------------
8396 function In_Same_Enclosing_Subp return Boolean is
8401 Scop := Scope (Act_Id);
8402 while Scop /= Standard_Standard
8403 and then not Is_Overloadable (Scop)
8405 Scop := Scope (Scop);
8408 if Scop = Standard_Standard then
8414 Scop := Scope (Gen_Id);
8415 while Scop /= Standard_Standard loop
8419 Scop := Scope (Scop);
8424 end In_Same_Enclosing_Subp;
8430 function True_Sloc (N : Node_Id) return Source_Ptr is
8437 while Present (N1) and then N1 /= Act_Unit loop
8438 if Sloc (N1) > Res then
8448 -- Start of processing for Install_Body
8451 -- If the body is a subunit, the freeze point is the corresponding stub
8452 -- in the current compilation, not the subunit itself.
8454 if Nkind (Parent (Gen_Body)) = N_Subunit then
8455 Orig_Body := Corresponding_Stub (Parent (Gen_Body));
8457 Orig_Body := Gen_Body;
8460 Body_Unit := Unit (Cunit (Get_Source_Unit (Orig_Body)));
8462 -- If the instantiation and the generic definition appear in the same
8463 -- package declaration, this is an early instantiation. If they appear
8464 -- in the same declarative part, it is an early instantiation only if
8465 -- the generic body appears textually later, and the generic body is
8466 -- also in the main unit.
8468 -- If instance is nested within a subprogram, and the generic body
8469 -- is not, the instance is delayed because the enclosing body is. If
8470 -- instance and body are within the same scope, or the same subprogram
8471 -- body, indicate explicitly that the instance is delayed.
8474 (Gen_Unit = Act_Unit
8475 and then (Nkind_In (Gen_Unit, N_Package_Declaration,
8476 N_Generic_Package_Declaration)
8477 or else (Gen_Unit = Body_Unit
8478 and then True_Sloc (N) < Sloc (Orig_Body)))
8479 and then Is_In_Main_Unit (Gen_Unit)
8480 and then (Scope (Act_Id) = Scope (Gen_Id)
8481 or else In_Same_Enclosing_Subp));
8483 -- If this is an early instantiation, the freeze node is placed after
8484 -- the generic body. Otherwise, if the generic appears in an instance,
8485 -- we cannot freeze the current instance until the outer one is frozen.
8486 -- This is only relevant if the current instance is nested within some
8487 -- inner scope not itself within the outer instance. If this scope is
8488 -- a package body in the same declarative part as the outer instance,
8489 -- then that body needs to be frozen after the outer instance. Finally,
8490 -- if no delay is needed, we place the freeze node at the end of the
8491 -- current declarative part.
8493 if Expander_Active then
8494 Ensure_Freeze_Node (Act_Id);
8495 F_Node := Freeze_Node (Act_Id);
8498 Insert_After (Orig_Body, F_Node);
8500 elsif Is_Generic_Instance (Par)
8501 and then Present (Freeze_Node (Par))
8502 and then Scope (Act_Id) /= Par
8504 -- Freeze instance of inner generic after instance of enclosing
8507 if In_Same_Declarative_Part (Freeze_Node (Par), N) then
8509 -- Handle the following case:
8511 -- package Parent_Inst is new ...
8514 -- procedure P ... -- this body freezes Parent_Inst
8516 -- package Inst is new ...
8518 -- In this particular scenario, the freeze node for Inst must
8519 -- be inserted in the same manner as that of Parent_Inst,
8520 -- before the next source body or at the end of the declarative
8521 -- list (body not available). If body P did not exist and
8522 -- Parent_Inst was frozen after Inst, either by a body
8523 -- following Inst or at the end of the declarative region,
8524 -- the freeze node for Inst must be inserted after that of
8525 -- Parent_Inst. This relation is established by comparing
8526 -- the Slocs of Parent_Inst freeze node and Inst.
8528 if List_Containing (Get_Package_Instantiation_Node (Par)) =
8530 and then Sloc (Freeze_Node (Par)) < Sloc (N)
8532 Insert_Freeze_Node_For_Instance (N, F_Node);
8534 Insert_After (Freeze_Node (Par), F_Node);
8537 -- Freeze package enclosing instance of inner generic after
8538 -- instance of enclosing generic.
8540 elsif Nkind_In (Parent (N), N_Package_Body, N_Subprogram_Body)
8541 and then In_Same_Declarative_Part (Freeze_Node (Par), Parent (N))
8544 Enclosing : Entity_Id;
8547 Enclosing := Corresponding_Spec (Parent (N));
8549 if No (Enclosing) then
8550 Enclosing := Defining_Entity (Parent (N));
8553 Insert_Freeze_Node_For_Instance (N, F_Node);
8554 Ensure_Freeze_Node (Enclosing);
8556 if not Is_List_Member (Freeze_Node (Enclosing)) then
8558 -- The enclosing context is a subunit, insert the freeze
8559 -- node after the stub.
8561 if Nkind (Parent (Parent (N))) = N_Subunit then
8562 Insert_Freeze_Node_For_Instance
8563 (Corresponding_Stub (Parent (Parent (N))),
8564 Freeze_Node (Enclosing));
8566 -- The enclosing context is a package with a stub body
8567 -- which has already been replaced by the real body.
8568 -- Insert the freeze node after the actual body.
8570 elsif Ekind (Enclosing) = E_Package
8571 and then Present (Body_Entity (Enclosing))
8572 and then Was_Originally_Stub
8573 (Parent (Body_Entity (Enclosing)))
8575 Insert_Freeze_Node_For_Instance
8576 (Parent (Body_Entity (Enclosing)),
8577 Freeze_Node (Enclosing));
8579 -- The parent instance has been frozen before the body of
8580 -- the enclosing package, insert the freeze node after
8583 elsif List_Containing (Freeze_Node (Par)) =
8584 List_Containing (Parent (N))
8585 and then Sloc (Freeze_Node (Par)) < Sloc (Parent (N))
8587 Insert_Freeze_Node_For_Instance
8588 (Parent (N), Freeze_Node (Enclosing));
8592 (Freeze_Node (Par), Freeze_Node (Enclosing));
8598 Insert_Freeze_Node_For_Instance (N, F_Node);
8602 Insert_Freeze_Node_For_Instance (N, F_Node);
8606 Set_Is_Frozen (Act_Id);
8607 Insert_Before (N, Act_Body);
8608 Mark_Rewrite_Insertion (Act_Body);
8611 -----------------------------
8612 -- Install_Formal_Packages --
8613 -----------------------------
8615 procedure Install_Formal_Packages (Par : Entity_Id) is
8618 Gen_E : Entity_Id := Empty;
8621 E := First_Entity (Par);
8623 -- If we are installing an instance parent, locate the formal packages
8624 -- of its generic parent.
8626 if Is_Generic_Instance (Par) then
8627 Gen := Generic_Parent (Package_Specification (Par));
8628 Gen_E := First_Entity (Gen);
8631 while Present (E) loop
8632 if Ekind (E) = E_Package
8633 and then Nkind (Parent (E)) = N_Package_Renaming_Declaration
8635 -- If this is the renaming for the parent instance, done
8637 if Renamed_Object (E) = Par then
8640 -- The visibility of a formal of an enclosing generic is already
8643 elsif Denotes_Formal_Package (E) then
8646 elsif Present (Associated_Formal_Package (E)) then
8647 Check_Generic_Actuals (Renamed_Object (E), True);
8648 Set_Is_Hidden (E, False);
8650 -- Find formal package in generic unit that corresponds to
8651 -- (instance of) formal package in instance.
8653 while Present (Gen_E) and then Chars (Gen_E) /= Chars (E) loop
8654 Next_Entity (Gen_E);
8657 if Present (Gen_E) then
8658 Map_Formal_Package_Entities (Gen_E, E);
8664 if Present (Gen_E) then
8665 Next_Entity (Gen_E);
8668 end Install_Formal_Packages;
8670 --------------------
8671 -- Install_Parent --
8672 --------------------
8674 procedure Install_Parent (P : Entity_Id; In_Body : Boolean := False) is
8675 Ancestors : constant Elist_Id := New_Elmt_List;
8676 S : constant Entity_Id := Current_Scope;
8677 Inst_Par : Entity_Id;
8678 First_Par : Entity_Id;
8679 Inst_Node : Node_Id;
8680 Gen_Par : Entity_Id;
8681 First_Gen : Entity_Id;
8684 procedure Install_Noninstance_Specs (Par : Entity_Id);
8685 -- Install the scopes of noninstance parent units ending with Par
8687 procedure Install_Spec (Par : Entity_Id);
8688 -- The child unit is within the declarative part of the parent, so the
8689 -- declarations within the parent are immediately visible.
8691 -------------------------------
8692 -- Install_Noninstance_Specs --
8693 -------------------------------
8695 procedure Install_Noninstance_Specs (Par : Entity_Id) is
8698 and then Par /= Standard_Standard
8699 and then not In_Open_Scopes (Par)
8701 Install_Noninstance_Specs (Scope (Par));
8704 end Install_Noninstance_Specs;
8710 procedure Install_Spec (Par : Entity_Id) is
8711 Spec : constant Node_Id := Package_Specification (Par);
8714 -- If this parent of the child instance is a top-level unit,
8715 -- then record the unit and its visibility for later resetting in
8716 -- Remove_Parent. We exclude units that are generic instances, as we
8717 -- only want to record this information for the ultimate top-level
8718 -- noninstance parent (is that always correct???).
8720 if Scope (Par) = Standard_Standard
8721 and then not Is_Generic_Instance (Par)
8723 Parent_Unit_Visible := Is_Immediately_Visible (Par);
8724 Instance_Parent_Unit := Par;
8727 -- Open the parent scope and make it and its declarations visible.
8728 -- If this point is not within a body, then only the visible
8729 -- declarations should be made visible, and installation of the
8730 -- private declarations is deferred until the appropriate point
8731 -- within analysis of the spec being instantiated (see the handling
8732 -- of parent visibility in Analyze_Package_Specification). This is
8733 -- relaxed in the case where the parent unit is Ada.Tags, to avoid
8734 -- private view problems that occur when compiling instantiations of
8735 -- a generic child of that package (Generic_Dispatching_Constructor).
8736 -- If the instance freezes a tagged type, inlinings of operations
8737 -- from Ada.Tags may need the full view of type Tag. If inlining took
8738 -- proper account of establishing visibility of inlined subprograms'
8739 -- parents then it should be possible to remove this
8740 -- special check. ???
8743 Set_Is_Immediately_Visible (Par);
8744 Install_Visible_Declarations (Par);
8745 Set_Use (Visible_Declarations (Spec));
8747 if In_Body or else Is_RTU (Par, Ada_Tags) then
8748 Install_Private_Declarations (Par);
8749 Set_Use (Private_Declarations (Spec));
8753 -- Start of processing for Install_Parent
8756 -- We need to install the parent instance to compile the instantiation
8757 -- of the child, but the child instance must appear in the current
8758 -- scope. Given that we cannot place the parent above the current scope
8759 -- in the scope stack, we duplicate the current scope and unstack both
8760 -- after the instantiation is complete.
8762 -- If the parent is itself the instantiation of a child unit, we must
8763 -- also stack the instantiation of its parent, and so on. Each such
8764 -- ancestor is the prefix of the name in a prior instantiation.
8766 -- If this is a nested instance, the parent unit itself resolves to
8767 -- a renaming of the parent instance, whose declaration we need.
8769 -- Finally, the parent may be a generic (not an instance) when the
8770 -- child unit appears as a formal package.
8774 if Present (Renamed_Entity (Inst_Par)) then
8775 Inst_Par := Renamed_Entity (Inst_Par);
8778 First_Par := Inst_Par;
8780 Gen_Par := Generic_Parent (Package_Specification (Inst_Par));
8782 First_Gen := Gen_Par;
8784 while Present (Gen_Par)
8785 and then Is_Child_Unit (Gen_Par)
8787 -- Load grandparent instance as well
8789 Inst_Node := Get_Package_Instantiation_Node (Inst_Par);
8791 if Nkind (Name (Inst_Node)) = N_Expanded_Name then
8792 Inst_Par := Entity (Prefix (Name (Inst_Node)));
8794 if Present (Renamed_Entity (Inst_Par)) then
8795 Inst_Par := Renamed_Entity (Inst_Par);
8798 Gen_Par := Generic_Parent (Package_Specification (Inst_Par));
8800 if Present (Gen_Par) then
8801 Prepend_Elmt (Inst_Par, Ancestors);
8804 -- Parent is not the name of an instantiation
8806 Install_Noninstance_Specs (Inst_Par);
8817 if Present (First_Gen) then
8818 Append_Elmt (First_Par, Ancestors);
8820 Install_Noninstance_Specs (First_Par);
8823 if not Is_Empty_Elmt_List (Ancestors) then
8824 Elmt := First_Elmt (Ancestors);
8825 while Present (Elmt) loop
8826 Install_Spec (Node (Elmt));
8827 Install_Formal_Packages (Node (Elmt));
8837 -------------------------------
8838 -- Install_Hidden_Primitives --
8839 -------------------------------
8841 procedure Install_Hidden_Primitives
8842 (Prims_List : in out Elist_Id;
8847 List : Elist_Id := No_Elist;
8848 Prim_G_Elmt : Elmt_Id;
8849 Prim_A_Elmt : Elmt_Id;
8854 -- No action needed in case of serious errors because we cannot trust
8855 -- in the order of primitives
8857 if Serious_Errors_Detected > 0 then
8860 -- No action possible if we don't have available the list of primitive
8864 or else not Is_Record_Type (Gen_T)
8865 or else not Is_Tagged_Type (Gen_T)
8866 or else not Is_Record_Type (Act_T)
8867 or else not Is_Tagged_Type (Act_T)
8871 -- There is no need to handle interface types since their primitives
8874 elsif Is_Interface (Gen_T) then
8878 Prim_G_Elmt := First_Elmt (Primitive_Operations (Gen_T));
8880 if not Is_Class_Wide_Type (Act_T) then
8881 Prim_A_Elmt := First_Elmt (Primitive_Operations (Act_T));
8883 Prim_A_Elmt := First_Elmt (Primitive_Operations (Root_Type (Act_T)));
8887 -- Skip predefined primitives in the generic formal
8889 while Present (Prim_G_Elmt)
8890 and then Is_Predefined_Dispatching_Operation (Node (Prim_G_Elmt))
8892 Next_Elmt (Prim_G_Elmt);
8895 -- Skip predefined primitives in the generic actual
8897 while Present (Prim_A_Elmt)
8898 and then Is_Predefined_Dispatching_Operation (Node (Prim_A_Elmt))
8900 Next_Elmt (Prim_A_Elmt);
8903 exit when No (Prim_G_Elmt) or else No (Prim_A_Elmt);
8905 Prim_G := Node (Prim_G_Elmt);
8906 Prim_A := Node (Prim_A_Elmt);
8908 -- There is no need to handle interface primitives because their
8909 -- primitives are not hidden
8911 exit when Present (Interface_Alias (Prim_G));
8913 -- Here we install one hidden primitive
8915 if Chars (Prim_G) /= Chars (Prim_A)
8916 and then Has_Suffix (Prim_A, 'P')
8917 and then Remove_Suffix (Prim_A, 'P') = Chars (Prim_G)
8919 Set_Chars (Prim_A, Chars (Prim_G));
8920 Append_New_Elmt (Prim_A, To => List);
8923 Next_Elmt (Prim_A_Elmt);
8924 Next_Elmt (Prim_G_Elmt);
8927 -- Append the elements to the list of temporarily visible primitives
8928 -- avoiding duplicates.
8930 if Present (List) then
8931 if No (Prims_List) then
8932 Prims_List := New_Elmt_List;
8935 Elmt := First_Elmt (List);
8936 while Present (Elmt) loop
8937 Append_Unique_Elmt (Node (Elmt), Prims_List);
8941 end Install_Hidden_Primitives;
8943 -------------------------------
8944 -- Restore_Hidden_Primitives --
8945 -------------------------------
8947 procedure Restore_Hidden_Primitives (Prims_List : in out Elist_Id) is
8948 Prim_Elmt : Elmt_Id;
8952 if Prims_List /= No_Elist then
8953 Prim_Elmt := First_Elmt (Prims_List);
8954 while Present (Prim_Elmt) loop
8955 Prim := Node (Prim_Elmt);
8956 Set_Chars (Prim, Add_Suffix (Prim, 'P'));
8957 Next_Elmt (Prim_Elmt);
8960 Prims_List := No_Elist;
8962 end Restore_Hidden_Primitives;
8964 --------------------------------
8965 -- Instantiate_Formal_Package --
8966 --------------------------------
8968 function Instantiate_Formal_Package
8971 Analyzed_Formal : Node_Id) return List_Id
8973 Loc : constant Source_Ptr := Sloc (Actual);
8974 Actual_Pack : Entity_Id;
8975 Formal_Pack : Entity_Id;
8976 Gen_Parent : Entity_Id;
8979 Parent_Spec : Node_Id;
8981 procedure Find_Matching_Actual
8983 Act : in out Entity_Id);
8984 -- We need to associate each formal entity in the formal package with
8985 -- the corresponding entity in the actual package. The actual package
8986 -- has been analyzed and possibly expanded, and as a result there is
8987 -- no one-to-one correspondence between the two lists (for example,
8988 -- the actual may include subtypes, itypes, and inherited primitive
8989 -- operations, interspersed among the renaming declarations for the
8990 -- actuals) . We retrieve the corresponding actual by name because each
8991 -- actual has the same name as the formal, and they do appear in the
8994 function Get_Formal_Entity (N : Node_Id) return Entity_Id;
8995 -- Retrieve entity of defining entity of generic formal parameter.
8996 -- Only the declarations of formals need to be considered when
8997 -- linking them to actuals, but the declarative list may include
8998 -- internal entities generated during analysis, and those are ignored.
9000 procedure Match_Formal_Entity
9001 (Formal_Node : Node_Id;
9002 Formal_Ent : Entity_Id;
9003 Actual_Ent : Entity_Id);
9004 -- Associates the formal entity with the actual. In the case where
9005 -- Formal_Ent is a formal package, this procedure iterates through all
9006 -- of its formals and enters associations between the actuals occurring
9007 -- in the formal package's corresponding actual package (given by
9008 -- Actual_Ent) and the formal package's formal parameters. This
9009 -- procedure recurses if any of the parameters is itself a package.
9011 function Is_Instance_Of
9012 (Act_Spec : Entity_Id;
9013 Gen_Anc : Entity_Id) return Boolean;
9014 -- The actual can be an instantiation of a generic within another
9015 -- instance, in which case there is no direct link from it to the
9016 -- original generic ancestor. In that case, we recognize that the
9017 -- ultimate ancestor is the same by examining names and scopes.
9019 procedure Process_Nested_Formal (Formal : Entity_Id);
9020 -- If the current formal is declared with a box, its own formals are
9021 -- visible in the instance, as they were in the generic, and their
9022 -- Hidden flag must be reset. If some of these formals are themselves
9023 -- packages declared with a box, the processing must be recursive.
9025 --------------------------
9026 -- Find_Matching_Actual --
9027 --------------------------
9029 procedure Find_Matching_Actual
9031 Act : in out Entity_Id)
9033 Formal_Ent : Entity_Id;
9036 case Nkind (Original_Node (F)) is
9037 when N_Formal_Object_Declaration |
9038 N_Formal_Type_Declaration =>
9039 Formal_Ent := Defining_Identifier (F);
9041 while Chars (Act) /= Chars (Formal_Ent) loop
9045 when N_Formal_Subprogram_Declaration |
9046 N_Formal_Package_Declaration |
9047 N_Package_Declaration |
9048 N_Generic_Package_Declaration =>
9049 Formal_Ent := Defining_Entity (F);
9051 while Chars (Act) /= Chars (Formal_Ent) loop
9056 raise Program_Error;
9058 end Find_Matching_Actual;
9060 -------------------------
9061 -- Match_Formal_Entity --
9062 -------------------------
9064 procedure Match_Formal_Entity
9065 (Formal_Node : Node_Id;
9066 Formal_Ent : Entity_Id;
9067 Actual_Ent : Entity_Id)
9069 Act_Pkg : Entity_Id;
9072 Set_Instance_Of (Formal_Ent, Actual_Ent);
9074 if Ekind (Actual_Ent) = E_Package then
9076 -- Record associations for each parameter
9078 Act_Pkg := Actual_Ent;
9081 A_Ent : Entity_Id := First_Entity (Act_Pkg);
9090 -- Retrieve the actual given in the formal package declaration
9092 Actual := Entity (Name (Original_Node (Formal_Node)));
9094 -- The actual in the formal package declaration may be a
9095 -- renamed generic package, in which case we want to retrieve
9096 -- the original generic in order to traverse its formal part.
9098 if Present (Renamed_Entity (Actual)) then
9099 Gen_Decl := Unit_Declaration_Node (Renamed_Entity (Actual));
9101 Gen_Decl := Unit_Declaration_Node (Actual);
9104 Formals := Generic_Formal_Declarations (Gen_Decl);
9106 if Present (Formals) then
9107 F_Node := First_Non_Pragma (Formals);
9112 while Present (A_Ent)
9113 and then Present (F_Node)
9114 and then A_Ent /= First_Private_Entity (Act_Pkg)
9116 F_Ent := Get_Formal_Entity (F_Node);
9118 if Present (F_Ent) then
9120 -- This is a formal of the original package. Record
9121 -- association and recurse.
9123 Find_Matching_Actual (F_Node, A_Ent);
9124 Match_Formal_Entity (F_Node, F_Ent, A_Ent);
9125 Next_Entity (A_Ent);
9128 Next_Non_Pragma (F_Node);
9132 end Match_Formal_Entity;
9134 -----------------------
9135 -- Get_Formal_Entity --
9136 -----------------------
9138 function Get_Formal_Entity (N : Node_Id) return Entity_Id is
9139 Kind : constant Node_Kind := Nkind (Original_Node (N));
9142 when N_Formal_Object_Declaration =>
9143 return Defining_Identifier (N);
9145 when N_Formal_Type_Declaration =>
9146 return Defining_Identifier (N);
9148 when N_Formal_Subprogram_Declaration =>
9149 return Defining_Unit_Name (Specification (N));
9151 when N_Formal_Package_Declaration =>
9152 return Defining_Identifier (Original_Node (N));
9154 when N_Generic_Package_Declaration =>
9155 return Defining_Identifier (Original_Node (N));
9157 -- All other declarations are introduced by semantic analysis and
9158 -- have no match in the actual.
9163 end Get_Formal_Entity;
9165 --------------------
9166 -- Is_Instance_Of --
9167 --------------------
9169 function Is_Instance_Of
9170 (Act_Spec : Entity_Id;
9171 Gen_Anc : Entity_Id) return Boolean
9173 Gen_Par : constant Entity_Id := Generic_Parent (Act_Spec);
9176 if No (Gen_Par) then
9179 -- Simplest case: the generic parent of the actual is the formal
9181 elsif Gen_Par = Gen_Anc then
9184 elsif Chars (Gen_Par) /= Chars (Gen_Anc) then
9187 -- The actual may be obtained through several instantiations. Its
9188 -- scope must itself be an instance of a generic declared in the
9189 -- same scope as the formal. Any other case is detected above.
9191 elsif not Is_Generic_Instance (Scope (Gen_Par)) then
9195 return Generic_Parent (Parent (Scope (Gen_Par))) = Scope (Gen_Anc);
9199 ---------------------------
9200 -- Process_Nested_Formal --
9201 ---------------------------
9203 procedure Process_Nested_Formal (Formal : Entity_Id) is
9207 if Present (Associated_Formal_Package (Formal))
9208 and then Box_Present (Parent (Associated_Formal_Package (Formal)))
9210 Ent := First_Entity (Formal);
9211 while Present (Ent) loop
9212 Set_Is_Hidden (Ent, False);
9213 Set_Is_Visible_Formal (Ent);
9214 Set_Is_Potentially_Use_Visible
9215 (Ent, Is_Potentially_Use_Visible (Formal));
9217 if Ekind (Ent) = E_Package then
9218 exit when Renamed_Entity (Ent) = Renamed_Entity (Formal);
9219 Process_Nested_Formal (Ent);
9225 end Process_Nested_Formal;
9227 -- Start of processing for Instantiate_Formal_Package
9232 if not Is_Entity_Name (Actual)
9233 or else Ekind (Entity (Actual)) /= E_Package
9236 ("expect package instance to instantiate formal", Actual);
9237 Abandon_Instantiation (Actual);
9238 raise Program_Error;
9241 Actual_Pack := Entity (Actual);
9242 Set_Is_Instantiated (Actual_Pack);
9244 -- The actual may be a renamed package, or an outer generic formal
9245 -- package whose instantiation is converted into a renaming.
9247 if Present (Renamed_Object (Actual_Pack)) then
9248 Actual_Pack := Renamed_Object (Actual_Pack);
9251 if Nkind (Analyzed_Formal) = N_Formal_Package_Declaration then
9252 Gen_Parent := Get_Instance_Of (Entity (Name (Analyzed_Formal)));
9253 Formal_Pack := Defining_Identifier (Analyzed_Formal);
9256 Generic_Parent (Specification (Analyzed_Formal));
9258 Defining_Unit_Name (Specification (Analyzed_Formal));
9261 if Nkind (Parent (Actual_Pack)) = N_Defining_Program_Unit_Name then
9262 Parent_Spec := Package_Specification (Actual_Pack);
9264 Parent_Spec := Parent (Actual_Pack);
9267 if Gen_Parent = Any_Id then
9269 ("previous error in declaration of formal package", Actual);
9270 Abandon_Instantiation (Actual);
9273 Is_Instance_Of (Parent_Spec, Get_Instance_Of (Gen_Parent))
9279 ("actual parameter must be instance of&", Actual, Gen_Parent);
9280 Abandon_Instantiation (Actual);
9283 Set_Instance_Of (Defining_Identifier (Formal), Actual_Pack);
9284 Map_Formal_Package_Entities (Formal_Pack, Actual_Pack);
9287 Make_Package_Renaming_Declaration (Loc,
9288 Defining_Unit_Name => New_Copy (Defining_Identifier (Formal)),
9289 Name => New_Occurrence_Of (Actual_Pack, Loc));
9291 Set_Associated_Formal_Package (Defining_Unit_Name (Nod),
9292 Defining_Identifier (Formal));
9293 Decls := New_List (Nod);
9295 -- If the formal F has a box, then the generic declarations are
9296 -- visible in the generic G. In an instance of G, the corresponding
9297 -- entities in the actual for F (which are the actuals for the
9298 -- instantiation of the generic that F denotes) must also be made
9299 -- visible for analysis of the current instance. On exit from the
9300 -- current instance, those entities are made private again. If the
9301 -- actual is currently in use, these entities are also use-visible.
9303 -- The loop through the actual entities also steps through the formal
9304 -- entities and enters associations from formals to actuals into the
9305 -- renaming map. This is necessary to properly handle checking of
9306 -- actual parameter associations for later formals that depend on
9307 -- actuals declared in the formal package.
9309 -- In Ada 2005, partial parameterization requires that we make
9310 -- visible the actuals corresponding to formals that were defaulted
9311 -- in the formal package. There formals are identified because they
9312 -- remain formal generics within the formal package, rather than
9313 -- being renamings of the actuals supplied.
9316 Gen_Decl : constant Node_Id :=
9317 Unit_Declaration_Node (Gen_Parent);
9318 Formals : constant List_Id :=
9319 Generic_Formal_Declarations (Gen_Decl);
9321 Actual_Ent : Entity_Id;
9322 Actual_Of_Formal : Node_Id;
9323 Formal_Node : Node_Id;
9324 Formal_Ent : Entity_Id;
9327 if Present (Formals) then
9328 Formal_Node := First_Non_Pragma (Formals);
9330 Formal_Node := Empty;
9333 Actual_Ent := First_Entity (Actual_Pack);
9335 First (Visible_Declarations (Specification (Analyzed_Formal)));
9336 while Present (Actual_Ent)
9337 and then Actual_Ent /= First_Private_Entity (Actual_Pack)
9339 if Present (Formal_Node) then
9340 Formal_Ent := Get_Formal_Entity (Formal_Node);
9342 if Present (Formal_Ent) then
9343 Find_Matching_Actual (Formal_Node, Actual_Ent);
9345 (Formal_Node, Formal_Ent, Actual_Ent);
9347 -- We iterate at the same time over the actuals of the
9348 -- local package created for the formal, to determine
9349 -- which one of the formals of the original generic were
9350 -- defaulted in the formal. The corresponding actual
9351 -- entities are visible in the enclosing instance.
9353 if Box_Present (Formal)
9355 (Present (Actual_Of_Formal)
9358 (Get_Formal_Entity (Actual_Of_Formal)))
9360 Set_Is_Hidden (Actual_Ent, False);
9361 Set_Is_Visible_Formal (Actual_Ent);
9362 Set_Is_Potentially_Use_Visible
9363 (Actual_Ent, In_Use (Actual_Pack));
9365 if Ekind (Actual_Ent) = E_Package then
9366 Process_Nested_Formal (Actual_Ent);
9370 Set_Is_Hidden (Actual_Ent);
9371 Set_Is_Potentially_Use_Visible (Actual_Ent, False);
9375 Next_Non_Pragma (Formal_Node);
9376 Next (Actual_Of_Formal);
9379 -- No further formals to match, but the generic part may
9380 -- contain inherited operation that are not hidden in the
9381 -- enclosing instance.
9383 Next_Entity (Actual_Ent);
9387 -- Inherited subprograms generated by formal derived types are
9388 -- also visible if the types are.
9390 Actual_Ent := First_Entity (Actual_Pack);
9391 while Present (Actual_Ent)
9392 and then Actual_Ent /= First_Private_Entity (Actual_Pack)
9394 if Is_Overloadable (Actual_Ent)
9396 Nkind (Parent (Actual_Ent)) = N_Subtype_Declaration
9398 not Is_Hidden (Defining_Identifier (Parent (Actual_Ent)))
9400 Set_Is_Hidden (Actual_Ent, False);
9401 Set_Is_Potentially_Use_Visible
9402 (Actual_Ent, In_Use (Actual_Pack));
9405 Next_Entity (Actual_Ent);
9409 -- If the formal is not declared with a box, reanalyze it as an
9410 -- abbreviated instantiation, to verify the matching rules of 12.7.
9411 -- The actual checks are performed after the generic associations
9412 -- have been analyzed, to guarantee the same visibility for this
9413 -- instantiation and for the actuals.
9415 -- In Ada 2005, the generic associations for the formal can include
9416 -- defaulted parameters. These are ignored during check. This
9417 -- internal instantiation is removed from the tree after conformance
9418 -- checking, because it contains formal declarations for those
9419 -- defaulted parameters, and those should not reach the back-end.
9421 if not Box_Present (Formal) then
9423 I_Pack : constant Entity_Id :=
9424 Make_Temporary (Sloc (Actual), 'P');
9427 Set_Is_Internal (I_Pack);
9430 Make_Package_Instantiation (Sloc (Actual),
9431 Defining_Unit_Name => I_Pack,
9434 (Get_Instance_Of (Gen_Parent), Sloc (Actual)),
9435 Generic_Associations =>
9436 Generic_Associations (Formal)));
9442 end Instantiate_Formal_Package;
9444 -----------------------------------
9445 -- Instantiate_Formal_Subprogram --
9446 -----------------------------------
9448 function Instantiate_Formal_Subprogram
9451 Analyzed_Formal : Node_Id) return Node_Id
9454 Formal_Sub : constant Entity_Id :=
9455 Defining_Unit_Name (Specification (Formal));
9456 Analyzed_S : constant Entity_Id :=
9457 Defining_Unit_Name (Specification (Analyzed_Formal));
9458 Decl_Node : Node_Id;
9462 function From_Parent_Scope (Subp : Entity_Id) return Boolean;
9463 -- If the generic is a child unit, the parent has been installed on the
9464 -- scope stack, but a default subprogram cannot resolve to something
9465 -- on the parent because that parent is not really part of the visible
9466 -- context (it is there to resolve explicit local entities). If the
9467 -- default has resolved in this way, we remove the entity from immediate
9468 -- visibility and analyze the node again to emit an error message or
9469 -- find another visible candidate.
9471 procedure Valid_Actual_Subprogram (Act : Node_Id);
9472 -- Perform legality check and raise exception on failure
9474 -----------------------
9475 -- From_Parent_Scope --
9476 -----------------------
9478 function From_Parent_Scope (Subp : Entity_Id) return Boolean is
9479 Gen_Scope : Node_Id;
9482 Gen_Scope := Scope (Analyzed_S);
9483 while Present (Gen_Scope) and then Is_Child_Unit (Gen_Scope) loop
9484 if Scope (Subp) = Scope (Gen_Scope) then
9488 Gen_Scope := Scope (Gen_Scope);
9492 end From_Parent_Scope;
9494 -----------------------------
9495 -- Valid_Actual_Subprogram --
9496 -----------------------------
9498 procedure Valid_Actual_Subprogram (Act : Node_Id) is
9502 if Is_Entity_Name (Act) then
9503 Act_E := Entity (Act);
9505 elsif Nkind (Act) = N_Selected_Component
9506 and then Is_Entity_Name (Selector_Name (Act))
9508 Act_E := Entity (Selector_Name (Act));
9514 if (Present (Act_E) and then Is_Overloadable (Act_E))
9515 or else Nkind_In (Act, N_Attribute_Reference,
9516 N_Indexed_Component,
9517 N_Character_Literal,
9518 N_Explicit_Dereference)
9524 ("expect subprogram or entry name in instantiation of&",
9525 Instantiation_Node, Formal_Sub);
9526 Abandon_Instantiation (Instantiation_Node);
9528 end Valid_Actual_Subprogram;
9530 -- Start of processing for Instantiate_Formal_Subprogram
9533 New_Spec := New_Copy_Tree (Specification (Formal));
9535 -- The tree copy has created the proper instantiation sloc for the
9536 -- new specification. Use this location for all other constructed
9539 Loc := Sloc (Defining_Unit_Name (New_Spec));
9541 -- Create new entity for the actual (New_Copy_Tree does not)
9543 Set_Defining_Unit_Name
9544 (New_Spec, Make_Defining_Identifier (Loc, Chars (Formal_Sub)));
9546 -- Create new entities for the each of the formals in the
9547 -- specification of the renaming declaration built for the actual.
9549 if Present (Parameter_Specifications (New_Spec)) then
9553 F := First (Parameter_Specifications (New_Spec));
9554 while Present (F) loop
9555 Set_Defining_Identifier (F,
9556 Make_Defining_Identifier (Sloc (F),
9557 Chars => Chars (Defining_Identifier (F))));
9563 -- Find entity of actual. If the actual is an attribute reference, it
9564 -- cannot be resolved here (its formal is missing) but is handled
9565 -- instead in Attribute_Renaming. If the actual is overloaded, it is
9566 -- fully resolved subsequently, when the renaming declaration for the
9567 -- formal is analyzed. If it is an explicit dereference, resolve the
9568 -- prefix but not the actual itself, to prevent interpretation as call.
9570 if Present (Actual) then
9571 Loc := Sloc (Actual);
9572 Set_Sloc (New_Spec, Loc);
9574 if Nkind (Actual) = N_Operator_Symbol then
9575 Find_Direct_Name (Actual);
9577 elsif Nkind (Actual) = N_Explicit_Dereference then
9578 Analyze (Prefix (Actual));
9580 elsif Nkind (Actual) /= N_Attribute_Reference then
9584 Valid_Actual_Subprogram (Actual);
9587 elsif Present (Default_Name (Formal)) then
9588 if not Nkind_In (Default_Name (Formal), N_Attribute_Reference,
9589 N_Selected_Component,
9590 N_Indexed_Component,
9591 N_Character_Literal)
9592 and then Present (Entity (Default_Name (Formal)))
9594 Nam := New_Occurrence_Of (Entity (Default_Name (Formal)), Loc);
9596 Nam := New_Copy (Default_Name (Formal));
9597 Set_Sloc (Nam, Loc);
9600 elsif Box_Present (Formal) then
9602 -- Actual is resolved at the point of instantiation. Create an
9603 -- identifier or operator with the same name as the formal.
9605 if Nkind (Formal_Sub) = N_Defining_Operator_Symbol then
9606 Nam := Make_Operator_Symbol (Loc,
9607 Chars => Chars (Formal_Sub),
9608 Strval => No_String);
9610 Nam := Make_Identifier (Loc, Chars (Formal_Sub));
9613 elsif Nkind (Specification (Formal)) = N_Procedure_Specification
9614 and then Null_Present (Specification (Formal))
9616 -- Generate null body for procedure, for use in the instance
9619 Make_Subprogram_Body (Loc,
9620 Specification => New_Spec,
9621 Declarations => New_List,
9622 Handled_Statement_Sequence =>
9623 Make_Handled_Sequence_Of_Statements (Loc,
9624 Statements => New_List (Make_Null_Statement (Loc))));
9626 Set_Is_Intrinsic_Subprogram (Defining_Unit_Name (New_Spec));
9630 Error_Msg_Sloc := Sloc (Scope (Analyzed_S));
9632 ("missing actual&", Instantiation_Node, Formal_Sub);
9634 ("\in instantiation of & declared#",
9635 Instantiation_Node, Scope (Analyzed_S));
9636 Abandon_Instantiation (Instantiation_Node);
9640 Make_Subprogram_Renaming_Declaration (Loc,
9641 Specification => New_Spec,
9644 -- If we do not have an actual and the formal specified <> then set to
9645 -- get proper default.
9647 if No (Actual) and then Box_Present (Formal) then
9648 Set_From_Default (Decl_Node);
9651 -- Gather possible interpretations for the actual before analyzing the
9652 -- instance. If overloaded, it will be resolved when analyzing the
9653 -- renaming declaration.
9655 if Box_Present (Formal)
9656 and then No (Actual)
9660 if Is_Child_Unit (Scope (Analyzed_S))
9661 and then Present (Entity (Nam))
9663 if not Is_Overloaded (Nam) then
9664 if From_Parent_Scope (Entity (Nam)) then
9665 Set_Is_Immediately_Visible (Entity (Nam), False);
9666 Set_Entity (Nam, Empty);
9667 Set_Etype (Nam, Empty);
9670 Set_Is_Immediately_Visible (Entity (Nam));
9679 Get_First_Interp (Nam, I, It);
9680 while Present (It.Nam) loop
9681 if From_Parent_Scope (It.Nam) then
9685 Get_Next_Interp (I, It);
9692 -- The generic instantiation freezes the actual. This can only be done
9693 -- once the actual is resolved, in the analysis of the renaming
9694 -- declaration. To make the formal subprogram entity available, we set
9695 -- Corresponding_Formal_Spec to point to the formal subprogram entity.
9696 -- This is also needed in Analyze_Subprogram_Renaming for the processing
9697 -- of formal abstract subprograms.
9699 Set_Corresponding_Formal_Spec (Decl_Node, Analyzed_S);
9701 -- We cannot analyze the renaming declaration, and thus find the actual,
9702 -- until all the actuals are assembled in the instance. For subsequent
9703 -- checks of other actuals, indicate the node that will hold the
9704 -- instance of this formal.
9706 Set_Instance_Of (Analyzed_S, Nam);
9708 if Nkind (Actual) = N_Selected_Component
9709 and then Is_Task_Type (Etype (Prefix (Actual)))
9710 and then not Is_Frozen (Etype (Prefix (Actual)))
9712 -- The renaming declaration will create a body, which must appear
9713 -- outside of the instantiation, We move the renaming declaration
9714 -- out of the instance, and create an additional renaming inside,
9715 -- to prevent freezing anomalies.
9718 Anon_Id : constant Entity_Id := Make_Temporary (Loc, 'E');
9721 Set_Defining_Unit_Name (New_Spec, Anon_Id);
9722 Insert_Before (Instantiation_Node, Decl_Node);
9723 Analyze (Decl_Node);
9725 -- Now create renaming within the instance
9728 Make_Subprogram_Renaming_Declaration (Loc,
9729 Specification => New_Copy_Tree (New_Spec),
9730 Name => New_Occurrence_Of (Anon_Id, Loc));
9732 Set_Defining_Unit_Name (Specification (Decl_Node),
9733 Make_Defining_Identifier (Loc, Chars (Formal_Sub)));
9738 end Instantiate_Formal_Subprogram;
9740 ------------------------
9741 -- Instantiate_Object --
9742 ------------------------
9744 function Instantiate_Object
9747 Analyzed_Formal : Node_Id) return List_Id
9749 Gen_Obj : constant Entity_Id := Defining_Identifier (Formal);
9750 A_Gen_Obj : constant Entity_Id :=
9751 Defining_Identifier (Analyzed_Formal);
9752 Acc_Def : Node_Id := Empty;
9753 Act_Assoc : constant Node_Id := Parent (Actual);
9754 Actual_Decl : Node_Id := Empty;
9755 Decl_Node : Node_Id;
9758 List : constant List_Id := New_List;
9759 Loc : constant Source_Ptr := Sloc (Actual);
9760 Orig_Ftyp : constant Entity_Id := Etype (A_Gen_Obj);
9761 Subt_Decl : Node_Id := Empty;
9762 Subt_Mark : Node_Id := Empty;
9765 if Present (Subtype_Mark (Formal)) then
9766 Subt_Mark := Subtype_Mark (Formal);
9768 Check_Access_Definition (Formal);
9769 Acc_Def := Access_Definition (Formal);
9772 -- Sloc for error message on missing actual
9774 Error_Msg_Sloc := Sloc (Scope (A_Gen_Obj));
9776 if Get_Instance_Of (Gen_Obj) /= Gen_Obj then
9777 Error_Msg_N ("duplicate instantiation of generic parameter", Actual);
9780 Set_Parent (List, Parent (Actual));
9784 if Out_Present (Formal) then
9786 -- An IN OUT generic actual must be a name. The instantiation is a
9787 -- renaming declaration. The actual is the name being renamed. We
9788 -- use the actual directly, rather than a copy, because it is not
9789 -- used further in the list of actuals, and because a copy or a use
9790 -- of relocate_node is incorrect if the instance is nested within a
9791 -- generic. In order to simplify ASIS searches, the Generic_Parent
9792 -- field links the declaration to the generic association.
9797 Instantiation_Node, Gen_Obj);
9799 ("\in instantiation of & declared#",
9800 Instantiation_Node, Scope (A_Gen_Obj));
9801 Abandon_Instantiation (Instantiation_Node);
9804 if Present (Subt_Mark) then
9806 Make_Object_Renaming_Declaration (Loc,
9807 Defining_Identifier => New_Copy (Gen_Obj),
9808 Subtype_Mark => New_Copy_Tree (Subt_Mark),
9811 else pragma Assert (Present (Acc_Def));
9813 Make_Object_Renaming_Declaration (Loc,
9814 Defining_Identifier => New_Copy (Gen_Obj),
9815 Access_Definition => New_Copy_Tree (Acc_Def),
9819 Set_Corresponding_Generic_Association (Decl_Node, Act_Assoc);
9821 -- The analysis of the actual may produce Insert_Action nodes, so
9822 -- the declaration must have a context in which to attach them.
9824 Append (Decl_Node, List);
9827 -- Return if the analysis of the actual reported some error
9829 if Etype (Actual) = Any_Type then
9833 -- This check is performed here because Analyze_Object_Renaming will
9834 -- not check it when Comes_From_Source is False. Note though that the
9835 -- check for the actual being the name of an object will be performed
9836 -- in Analyze_Object_Renaming.
9838 if Is_Object_Reference (Actual)
9839 and then Is_Dependent_Component_Of_Mutable_Object (Actual)
9842 ("illegal discriminant-dependent component for in out parameter",
9846 -- The actual has to be resolved in order to check that it is a
9847 -- variable (due to cases such as F (1), where F returns access to
9848 -- an array, and for overloaded prefixes).
9850 Ftyp := Get_Instance_Of (Etype (A_Gen_Obj));
9852 -- If the type of the formal is not itself a formal, and the current
9853 -- unit is a child unit, the formal type must be declared in a
9854 -- parent, and must be retrieved by visibility.
9857 and then Is_Generic_Unit (Scope (Ftyp))
9858 and then Is_Child_Unit (Scope (A_Gen_Obj))
9861 Temp : constant Node_Id :=
9862 New_Copy_Tree (Subtype_Mark (Analyzed_Formal));
9864 Set_Entity (Temp, Empty);
9866 Ftyp := Entity (Temp);
9870 if Is_Private_Type (Ftyp)
9871 and then not Is_Private_Type (Etype (Actual))
9872 and then (Base_Type (Full_View (Ftyp)) = Base_Type (Etype (Actual))
9873 or else Base_Type (Etype (Actual)) = Ftyp)
9875 -- If the actual has the type of the full view of the formal, or
9876 -- else a non-private subtype of the formal, then the visibility
9877 -- of the formal type has changed. Add to the actuals a subtype
9878 -- declaration that will force the exchange of views in the body
9879 -- of the instance as well.
9882 Make_Subtype_Declaration (Loc,
9883 Defining_Identifier => Make_Temporary (Loc, 'P'),
9884 Subtype_Indication => New_Occurrence_Of (Ftyp, Loc));
9886 Prepend (Subt_Decl, List);
9888 Prepend_Elmt (Full_View (Ftyp), Exchanged_Views);
9889 Exchange_Declarations (Ftyp);
9892 Resolve (Actual, Ftyp);
9894 if not Denotes_Variable (Actual) then
9896 ("actual for& must be a variable", Actual, Gen_Obj);
9898 elsif Base_Type (Ftyp) /= Base_Type (Etype (Actual)) then
9900 -- Ada 2005 (AI-423): For a generic formal object of mode in out,
9901 -- the type of the actual shall resolve to a specific anonymous
9904 if Ada_Version < Ada_2005
9906 Ekind (Base_Type (Ftyp)) /=
9907 E_Anonymous_Access_Type
9909 Ekind (Base_Type (Etype (Actual))) /=
9910 E_Anonymous_Access_Type
9912 Error_Msg_NE ("type of actual does not match type of&",
9917 Note_Possible_Modification (Actual, Sure => True);
9919 -- Check for instantiation of atomic/volatile actual for
9920 -- non-atomic/volatile formal (RM C.6 (12)).
9922 if Is_Atomic_Object (Actual)
9923 and then not Is_Atomic (Orig_Ftyp)
9926 ("cannot instantiate non-atomic formal object " &
9927 "with atomic actual", Actual);
9929 elsif Is_Volatile_Object (Actual)
9930 and then not Is_Volatile (Orig_Ftyp)
9933 ("cannot instantiate non-volatile formal object " &
9934 "with volatile actual", Actual);
9937 -- Formal in-parameter
9940 -- The instantiation of a generic formal in-parameter is constant
9941 -- declaration. The actual is the expression for that declaration.
9943 if Present (Actual) then
9944 if Present (Subt_Mark) then
9946 else pragma Assert (Present (Acc_Def));
9951 Make_Object_Declaration (Loc,
9952 Defining_Identifier => New_Copy (Gen_Obj),
9953 Constant_Present => True,
9954 Null_Exclusion_Present => Null_Exclusion_Present (Formal),
9955 Object_Definition => New_Copy_Tree (Def),
9956 Expression => Actual);
9958 Set_Corresponding_Generic_Association (Decl_Node, Act_Assoc);
9960 -- A generic formal object of a tagged type is defined to be
9961 -- aliased so the new constant must also be treated as aliased.
9963 if Is_Tagged_Type (Etype (A_Gen_Obj)) then
9964 Set_Aliased_Present (Decl_Node);
9967 Append (Decl_Node, List);
9969 -- No need to repeat (pre-)analysis of some expression nodes
9970 -- already handled in Preanalyze_Actuals.
9972 if Nkind (Actual) /= N_Allocator then
9975 -- Return if the analysis of the actual reported some error
9977 if Etype (Actual) = Any_Type then
9983 Formal_Type : constant Entity_Id := Etype (A_Gen_Obj);
9987 Typ := Get_Instance_Of (Formal_Type);
9989 Freeze_Before (Instantiation_Node, Typ);
9991 -- If the actual is an aggregate, perform name resolution on
9992 -- its components (the analysis of an aggregate does not do it)
9993 -- to capture local names that may be hidden if the generic is
9996 if Nkind (Actual) = N_Aggregate then
9997 Preanalyze_And_Resolve (Actual, Typ);
10000 if Is_Limited_Type (Typ)
10001 and then not OK_For_Limited_Init (Typ, Actual)
10004 ("initialization not allowed for limited types", Actual);
10005 Explain_Limited_Type (Typ, Actual);
10009 elsif Present (Default_Expression (Formal)) then
10011 -- Use default to construct declaration
10013 if Present (Subt_Mark) then
10015 else pragma Assert (Present (Acc_Def));
10020 Make_Object_Declaration (Sloc (Formal),
10021 Defining_Identifier => New_Copy (Gen_Obj),
10022 Constant_Present => True,
10023 Null_Exclusion_Present => Null_Exclusion_Present (Formal),
10024 Object_Definition => New_Copy (Def),
10025 Expression => New_Copy_Tree
10026 (Default_Expression (Formal)));
10028 Append (Decl_Node, List);
10029 Set_Analyzed (Expression (Decl_Node), False);
10033 ("missing actual&",
10034 Instantiation_Node, Gen_Obj);
10035 Error_Msg_NE ("\in instantiation of & declared#",
10036 Instantiation_Node, Scope (A_Gen_Obj));
10038 if Is_Scalar_Type (Etype (A_Gen_Obj)) then
10040 -- Create dummy constant declaration so that instance can be
10041 -- analyzed, to minimize cascaded visibility errors.
10043 if Present (Subt_Mark) then
10045 else pragma Assert (Present (Acc_Def));
10050 Make_Object_Declaration (Loc,
10051 Defining_Identifier => New_Copy (Gen_Obj),
10052 Constant_Present => True,
10053 Null_Exclusion_Present => Null_Exclusion_Present (Formal),
10054 Object_Definition => New_Copy (Def),
10056 Make_Attribute_Reference (Sloc (Gen_Obj),
10057 Attribute_Name => Name_First,
10058 Prefix => New_Copy (Def)));
10060 Append (Decl_Node, List);
10063 Abandon_Instantiation (Instantiation_Node);
10068 if Nkind (Actual) in N_Has_Entity then
10069 Actual_Decl := Parent (Entity (Actual));
10072 -- Ada 2005 (AI-423): For a formal object declaration with a null
10073 -- exclusion or an access definition that has a null exclusion: If the
10074 -- actual matching the formal object declaration denotes a generic
10075 -- formal object of another generic unit G, and the instantiation
10076 -- containing the actual occurs within the body of G or within the body
10077 -- of a generic unit declared within the declarative region of G, then
10078 -- the declaration of the formal object of G must have a null exclusion.
10079 -- Otherwise, the subtype of the actual matching the formal object
10080 -- declaration shall exclude null.
10082 if Ada_Version >= Ada_2005
10083 and then Present (Actual_Decl)
10085 Nkind_In (Actual_Decl, N_Formal_Object_Declaration,
10086 N_Object_Declaration)
10087 and then Nkind (Analyzed_Formal) = N_Formal_Object_Declaration
10088 and then not Has_Null_Exclusion (Actual_Decl)
10089 and then Has_Null_Exclusion (Analyzed_Formal)
10091 Error_Msg_Sloc := Sloc (Analyzed_Formal);
10093 ("actual must exclude null to match generic formal#", Actual);
10096 -- An effectively volatile object cannot be used as an actual in
10097 -- a generic instance. The following check is only relevant when
10098 -- SPARK_Mode is on as it is not a standard Ada legality rule.
10101 and then Present (Actual)
10102 and then Is_Effectively_Volatile_Object (Actual)
10105 ("volatile object cannot act as actual in generic instantiation "
10106 & "(SPARK RM 7.1.3(8))", Actual);
10110 end Instantiate_Object;
10112 ------------------------------
10113 -- Instantiate_Package_Body --
10114 ------------------------------
10116 procedure Instantiate_Package_Body
10117 (Body_Info : Pending_Body_Info;
10118 Inlined_Body : Boolean := False;
10119 Body_Optional : Boolean := False)
10121 Act_Decl : constant Node_Id := Body_Info.Act_Decl;
10122 Inst_Node : constant Node_Id := Body_Info.Inst_Node;
10123 Loc : constant Source_Ptr := Sloc (Inst_Node);
10125 Gen_Id : constant Node_Id := Name (Inst_Node);
10126 Gen_Unit : constant Entity_Id := Get_Generic_Entity (Inst_Node);
10127 Gen_Decl : constant Node_Id := Unit_Declaration_Node (Gen_Unit);
10128 Act_Spec : constant Node_Id := Specification (Act_Decl);
10129 Act_Decl_Id : constant Entity_Id := Defining_Entity (Act_Spec);
10131 Act_Body_Name : Node_Id;
10132 Gen_Body : Node_Id;
10133 Gen_Body_Id : Node_Id;
10134 Act_Body : Node_Id;
10135 Act_Body_Id : Entity_Id;
10137 Parent_Installed : Boolean := False;
10138 Save_Style_Check : constant Boolean := Style_Check;
10140 Par_Ent : Entity_Id := Empty;
10141 Par_Vis : Boolean := False;
10143 Vis_Prims_List : Elist_Id := No_Elist;
10144 -- List of primitives made temporarily visible in the instantiation
10145 -- to match the visibility of the formal type
10147 procedure Check_Initialized_Types;
10148 -- In a generic package body, an entity of a generic private type may
10149 -- appear uninitialized. This is suspicious, unless the actual is a
10150 -- fully initialized type.
10152 -----------------------------
10153 -- Check_Initialized_Types --
10154 -----------------------------
10156 procedure Check_Initialized_Types is
10158 Formal : Entity_Id;
10159 Actual : Entity_Id;
10160 Uninit_Var : Entity_Id;
10163 Decl := First (Generic_Formal_Declarations (Gen_Decl));
10164 while Present (Decl) loop
10165 Uninit_Var := Empty;
10167 if Nkind (Decl) = N_Private_Extension_Declaration then
10168 Uninit_Var := Uninitialized_Variable (Decl);
10170 elsif Nkind (Decl) = N_Formal_Type_Declaration
10171 and then Nkind (Formal_Type_Definition (Decl)) =
10172 N_Formal_Private_Type_Definition
10175 Uninitialized_Variable (Formal_Type_Definition (Decl));
10178 if Present (Uninit_Var) then
10179 Formal := Defining_Identifier (Decl);
10180 Actual := First_Entity (Act_Decl_Id);
10182 -- For each formal there is a subtype declaration that renames
10183 -- the actual and has the same name as the formal. Locate the
10184 -- formal for warning message about uninitialized variables
10185 -- in the generic, for which the actual type should be a fully
10186 -- initialized type.
10188 while Present (Actual) loop
10189 exit when Ekind (Actual) = E_Package
10190 and then Present (Renamed_Object (Actual));
10192 if Chars (Actual) = Chars (Formal)
10193 and then not Is_Scalar_Type (Actual)
10194 and then not Is_Fully_Initialized_Type (Actual)
10195 and then Warn_On_No_Value_Assigned
10197 Error_Msg_Node_2 := Formal;
10199 ("generic unit has uninitialized variable& of "
10200 & "formal private type &?v?", Actual, Uninit_Var);
10202 ("actual type for& should be fully initialized type?v?",
10207 Next_Entity (Actual);
10213 end Check_Initialized_Types;
10215 -- Start of processing for Instantiate_Package_Body
10218 Gen_Body_Id := Corresponding_Body (Gen_Decl);
10220 -- The instance body may already have been processed, as the parent of
10221 -- another instance that is inlined (Load_Parent_Of_Generic).
10223 if Present (Corresponding_Body (Instance_Spec (Inst_Node))) then
10227 Expander_Mode_Save_And_Set (Body_Info.Expander_Status);
10229 -- Re-establish the state of information on which checks are suppressed.
10230 -- This information was set in Body_Info at the point of instantiation,
10231 -- and now we restore it so that the instance is compiled using the
10232 -- check status at the instantiation (RM 11.5 (7.2/2), AI95-00224-01).
10234 Local_Suppress_Stack_Top := Body_Info.Local_Suppress_Stack_Top;
10235 Scope_Suppress := Body_Info.Scope_Suppress;
10236 Opt.Ada_Version := Body_Info.Version;
10237 Opt.Ada_Version_Pragma := Body_Info.Version_Pragma;
10238 Restore_Warnings (Body_Info.Warnings);
10239 Opt.SPARK_Mode := Body_Info.SPARK_Mode;
10240 Opt.SPARK_Mode_Pragma := Body_Info.SPARK_Mode_Pragma;
10242 if No (Gen_Body_Id) then
10244 -- Do not look for parent of generic body if none is required.
10245 -- This may happen when the routine is called as part of the
10246 -- Pending_Instantiations processing, when nested instances
10247 -- may precede the one generated from the main unit.
10249 if not Unit_Requires_Body (Defining_Entity (Gen_Decl))
10250 and then Body_Optional
10254 Load_Parent_Of_Generic
10255 (Inst_Node, Specification (Gen_Decl), Body_Optional);
10256 Gen_Body_Id := Corresponding_Body (Gen_Decl);
10260 -- Establish global variable for sloc adjustment and for error recovery
10262 Instantiation_Node := Inst_Node;
10264 if Present (Gen_Body_Id) then
10265 Save_Env (Gen_Unit, Act_Decl_Id);
10266 Style_Check := False;
10267 Current_Sem_Unit := Body_Info.Current_Sem_Unit;
10269 Gen_Body := Unit_Declaration_Node (Gen_Body_Id);
10271 Create_Instantiation_Source
10272 (Inst_Node, Gen_Body_Id, False, S_Adjustment);
10276 (Original_Node (Gen_Body), Empty, Instantiating => True);
10278 -- Build new name (possibly qualified) for body declaration
10280 Act_Body_Id := New_Copy (Act_Decl_Id);
10282 -- Some attributes of spec entity are not inherited by body entity
10284 Set_Handler_Records (Act_Body_Id, No_List);
10286 if Nkind (Defining_Unit_Name (Act_Spec)) =
10287 N_Defining_Program_Unit_Name
10290 Make_Defining_Program_Unit_Name (Loc,
10291 Name => New_Copy_Tree (Name (Defining_Unit_Name (Act_Spec))),
10292 Defining_Identifier => Act_Body_Id);
10294 Act_Body_Name := Act_Body_Id;
10297 Set_Defining_Unit_Name (Act_Body, Act_Body_Name);
10299 Set_Corresponding_Spec (Act_Body, Act_Decl_Id);
10300 Check_Generic_Actuals (Act_Decl_Id, False);
10301 Check_Initialized_Types;
10303 -- Install primitives hidden at the point of the instantiation but
10304 -- visible when processing the generic formals
10310 E := First_Entity (Act_Decl_Id);
10311 while Present (E) loop
10313 and then Is_Generic_Actual_Type (E)
10314 and then Is_Tagged_Type (E)
10316 Install_Hidden_Primitives
10317 (Prims_List => Vis_Prims_List,
10318 Gen_T => Generic_Parent_Type (Parent (E)),
10326 -- If it is a child unit, make the parent instance (which is an
10327 -- instance of the parent of the generic) visible. The parent
10328 -- instance is the prefix of the name of the generic unit.
10330 if Ekind (Scope (Gen_Unit)) = E_Generic_Package
10331 and then Nkind (Gen_Id) = N_Expanded_Name
10333 Par_Ent := Entity (Prefix (Gen_Id));
10334 Par_Vis := Is_Immediately_Visible (Par_Ent);
10335 Install_Parent (Par_Ent, In_Body => True);
10336 Parent_Installed := True;
10338 elsif Is_Child_Unit (Gen_Unit) then
10339 Par_Ent := Scope (Gen_Unit);
10340 Par_Vis := Is_Immediately_Visible (Par_Ent);
10341 Install_Parent (Par_Ent, In_Body => True);
10342 Parent_Installed := True;
10345 -- If the instantiation is a library unit, and this is the main unit,
10346 -- then build the resulting compilation unit nodes for the instance.
10347 -- If this is a compilation unit but it is not the main unit, then it
10348 -- is the body of a unit in the context, that is being compiled
10349 -- because it is encloses some inlined unit or another generic unit
10350 -- being instantiated. In that case, this body is not part of the
10351 -- current compilation, and is not attached to the tree, but its
10352 -- parent must be set for analysis.
10354 if Nkind (Parent (Inst_Node)) = N_Compilation_Unit then
10356 -- Replace instance node with body of instance, and create new
10357 -- node for corresponding instance declaration.
10359 Build_Instance_Compilation_Unit_Nodes
10360 (Inst_Node, Act_Body, Act_Decl);
10361 Analyze (Inst_Node);
10363 if Parent (Inst_Node) = Cunit (Main_Unit) then
10365 -- If the instance is a child unit itself, then set the scope
10366 -- of the expanded body to be the parent of the instantiation
10367 -- (ensuring that the fully qualified name will be generated
10368 -- for the elaboration subprogram).
10370 if Nkind (Defining_Unit_Name (Act_Spec)) =
10371 N_Defining_Program_Unit_Name
10374 (Defining_Entity (Inst_Node), Scope (Act_Decl_Id));
10378 -- Case where instantiation is not a library unit
10381 -- If this is an early instantiation, i.e. appears textually
10382 -- before the corresponding body and must be elaborated first,
10383 -- indicate that the body instance is to be delayed.
10385 Install_Body (Act_Body, Inst_Node, Gen_Body, Gen_Decl);
10387 -- Now analyze the body. We turn off all checks if this is an
10388 -- internal unit, since there is no reason to have checks on for
10389 -- any predefined run-time library code. All such code is designed
10390 -- to be compiled with checks off.
10392 -- Note that we do NOT apply this criterion to children of GNAT
10393 -- The latter units must suppress checks explicitly if needed.
10395 if Is_Predefined_File_Name
10396 (Unit_File_Name (Get_Source_Unit (Gen_Decl)))
10398 Analyze (Act_Body, Suppress => All_Checks);
10400 Analyze (Act_Body);
10404 Inherit_Context (Gen_Body, Inst_Node);
10406 -- Remove the parent instances if they have been placed on the scope
10407 -- stack to compile the body.
10409 if Parent_Installed then
10410 Remove_Parent (In_Body => True);
10412 -- Restore the previous visibility of the parent
10414 Set_Is_Immediately_Visible (Par_Ent, Par_Vis);
10417 Restore_Hidden_Primitives (Vis_Prims_List);
10418 Restore_Private_Views (Act_Decl_Id);
10420 -- Remove the current unit from visibility if this is an instance
10421 -- that is not elaborated on the fly for inlining purposes.
10423 if not Inlined_Body then
10424 Set_Is_Immediately_Visible (Act_Decl_Id, False);
10428 Style_Check := Save_Style_Check;
10430 -- If we have no body, and the unit requires a body, then complain. This
10431 -- complaint is suppressed if we have detected other errors (since a
10432 -- common reason for missing the body is that it had errors).
10433 -- In CodePeer mode, a warning has been emitted already, no need for
10434 -- further messages.
10436 elsif Unit_Requires_Body (Gen_Unit)
10437 and then not Body_Optional
10439 if CodePeer_Mode then
10442 elsif Serious_Errors_Detected = 0 then
10444 ("cannot find body of generic package &", Inst_Node, Gen_Unit);
10446 -- Don't attempt to perform any cleanup actions if some other error
10447 -- was already detected, since this can cause blowups.
10453 -- Case of package that does not need a body
10456 -- If the instantiation of the declaration is a library unit, rewrite
10457 -- the original package instantiation as a package declaration in the
10458 -- compilation unit node.
10460 if Nkind (Parent (Inst_Node)) = N_Compilation_Unit then
10461 Set_Parent_Spec (Act_Decl, Parent_Spec (Inst_Node));
10462 Rewrite (Inst_Node, Act_Decl);
10464 -- Generate elaboration entity, in case spec has elaboration code.
10465 -- This cannot be done when the instance is analyzed, because it
10466 -- is not known yet whether the body exists.
10468 Set_Elaboration_Entity_Required (Act_Decl_Id, False);
10469 Build_Elaboration_Entity (Parent (Inst_Node), Act_Decl_Id);
10471 -- If the instantiation is not a library unit, then append the
10472 -- declaration to the list of implicitly generated entities, unless
10473 -- it is already a list member which means that it was already
10476 elsif not Is_List_Member (Act_Decl) then
10477 Mark_Rewrite_Insertion (Act_Decl);
10478 Insert_Before (Inst_Node, Act_Decl);
10482 Expander_Mode_Restore;
10483 end Instantiate_Package_Body;
10485 ---------------------------------
10486 -- Instantiate_Subprogram_Body --
10487 ---------------------------------
10489 procedure Instantiate_Subprogram_Body
10490 (Body_Info : Pending_Body_Info;
10491 Body_Optional : Boolean := False)
10493 Act_Decl : constant Node_Id := Body_Info.Act_Decl;
10494 Inst_Node : constant Node_Id := Body_Info.Inst_Node;
10495 Loc : constant Source_Ptr := Sloc (Inst_Node);
10496 Gen_Id : constant Node_Id := Name (Inst_Node);
10497 Gen_Unit : constant Entity_Id := Get_Generic_Entity (Inst_Node);
10498 Gen_Decl : constant Node_Id := Unit_Declaration_Node (Gen_Unit);
10499 Anon_Id : constant Entity_Id :=
10500 Defining_Unit_Name (Specification (Act_Decl));
10501 Pack_Id : constant Entity_Id :=
10502 Defining_Unit_Name (Parent (Act_Decl));
10504 Gen_Body : Node_Id;
10505 Gen_Body_Id : Node_Id;
10506 Act_Body : Node_Id;
10507 Pack_Body : Node_Id;
10508 Prev_Formal : Entity_Id;
10509 Ret_Expr : Node_Id;
10510 Unit_Renaming : Node_Id;
10512 Parent_Installed : Boolean := False;
10514 Saved_Style_Check : constant Boolean := Style_Check;
10515 Saved_Warnings : constant Warning_Record := Save_Warnings;
10517 Par_Ent : Entity_Id := Empty;
10518 Par_Vis : Boolean := False;
10521 Gen_Body_Id := Corresponding_Body (Gen_Decl);
10523 -- Subprogram body may have been created already because of an inline
10524 -- pragma, or because of multiple elaborations of the enclosing package
10525 -- when several instances of the subprogram appear in the main unit.
10527 if Present (Corresponding_Body (Act_Decl)) then
10531 Expander_Mode_Save_And_Set (Body_Info.Expander_Status);
10533 -- Re-establish the state of information on which checks are suppressed.
10534 -- This information was set in Body_Info at the point of instantiation,
10535 -- and now we restore it so that the instance is compiled using the
10536 -- check status at the instantiation (RM 11.5 (7.2/2), AI95-00224-01).
10538 Local_Suppress_Stack_Top := Body_Info.Local_Suppress_Stack_Top;
10539 Scope_Suppress := Body_Info.Scope_Suppress;
10540 Opt.Ada_Version := Body_Info.Version;
10541 Opt.Ada_Version_Pragma := Body_Info.Version_Pragma;
10542 Restore_Warnings (Body_Info.Warnings);
10543 Opt.SPARK_Mode := Body_Info.SPARK_Mode;
10544 Opt.SPARK_Mode_Pragma := Body_Info.SPARK_Mode_Pragma;
10546 if No (Gen_Body_Id) then
10548 -- For imported generic subprogram, no body to compile, complete
10549 -- the spec entity appropriately.
10551 if Is_Imported (Gen_Unit) then
10552 Set_Is_Imported (Anon_Id);
10553 Set_First_Rep_Item (Anon_Id, First_Rep_Item (Gen_Unit));
10554 Set_Interface_Name (Anon_Id, Interface_Name (Gen_Unit));
10555 Set_Convention (Anon_Id, Convention (Gen_Unit));
10556 Set_Has_Completion (Anon_Id);
10559 -- For other cases, compile the body
10562 Load_Parent_Of_Generic
10563 (Inst_Node, Specification (Gen_Decl), Body_Optional);
10564 Gen_Body_Id := Corresponding_Body (Gen_Decl);
10568 Instantiation_Node := Inst_Node;
10570 if Present (Gen_Body_Id) then
10571 Gen_Body := Unit_Declaration_Node (Gen_Body_Id);
10573 if Nkind (Gen_Body) = N_Subprogram_Body_Stub then
10575 -- Either body is not present, or context is non-expanding, as
10576 -- when compiling a subunit. Mark the instance as completed, and
10577 -- diagnose a missing body when needed.
10580 and then Operating_Mode = Generate_Code
10583 ("missing proper body for instantiation", Gen_Body);
10586 Set_Has_Completion (Anon_Id);
10590 Save_Env (Gen_Unit, Anon_Id);
10591 Style_Check := False;
10592 Current_Sem_Unit := Body_Info.Current_Sem_Unit;
10593 Create_Instantiation_Source
10601 (Original_Node (Gen_Body), Empty, Instantiating => True);
10603 -- Create proper defining name for the body, to correspond to
10604 -- the one in the spec.
10606 Set_Defining_Unit_Name (Specification (Act_Body),
10607 Make_Defining_Identifier
10608 (Sloc (Defining_Entity (Inst_Node)), Chars (Anon_Id)));
10609 Set_Corresponding_Spec (Act_Body, Anon_Id);
10610 Set_Has_Completion (Anon_Id);
10611 Check_Generic_Actuals (Pack_Id, False);
10613 -- Generate a reference to link the visible subprogram instance to
10614 -- the generic body, which for navigation purposes is the only
10615 -- available source for the instance.
10618 (Related_Instance (Pack_Id),
10619 Gen_Body_Id, 'b', Set_Ref => False, Force => True);
10621 -- If it is a child unit, make the parent instance (which is an
10622 -- instance of the parent of the generic) visible. The parent
10623 -- instance is the prefix of the name of the generic unit.
10625 if Ekind (Scope (Gen_Unit)) = E_Generic_Package
10626 and then Nkind (Gen_Id) = N_Expanded_Name
10628 Par_Ent := Entity (Prefix (Gen_Id));
10629 Par_Vis := Is_Immediately_Visible (Par_Ent);
10630 Install_Parent (Par_Ent, In_Body => True);
10631 Parent_Installed := True;
10633 elsif Is_Child_Unit (Gen_Unit) then
10634 Par_Ent := Scope (Gen_Unit);
10635 Par_Vis := Is_Immediately_Visible (Par_Ent);
10636 Install_Parent (Par_Ent, In_Body => True);
10637 Parent_Installed := True;
10640 -- Inside its body, a reference to the generic unit is a reference
10641 -- to the instance. The corresponding renaming is the first
10642 -- declaration in the body.
10645 Make_Subprogram_Renaming_Declaration (Loc,
10647 Copy_Generic_Node (
10648 Specification (Original_Node (Gen_Body)),
10650 Instantiating => True),
10651 Name => New_Occurrence_Of (Anon_Id, Loc));
10653 -- If there is a formal subprogram with the same name as the unit
10654 -- itself, do not add this renaming declaration. This is a temporary
10655 -- fix for one ACVC test. ???
10657 Prev_Formal := First_Entity (Pack_Id);
10658 while Present (Prev_Formal) loop
10659 if Chars (Prev_Formal) = Chars (Gen_Unit)
10660 and then Is_Overloadable (Prev_Formal)
10665 Next_Entity (Prev_Formal);
10668 if Present (Prev_Formal) then
10669 Decls := New_List (Act_Body);
10671 Decls := New_List (Unit_Renaming, Act_Body);
10674 -- The subprogram body is placed in the body of a dummy package body,
10675 -- whose spec contains the subprogram declaration as well as the
10676 -- renaming declarations for the generic parameters.
10678 Pack_Body := Make_Package_Body (Loc,
10679 Defining_Unit_Name => New_Copy (Pack_Id),
10680 Declarations => Decls);
10682 Set_Corresponding_Spec (Pack_Body, Pack_Id);
10684 -- If the instantiation is a library unit, then build resulting
10685 -- compilation unit nodes for the instance. The declaration of
10686 -- the enclosing package is the grandparent of the subprogram
10687 -- declaration. First replace the instantiation node as the unit
10688 -- of the corresponding compilation.
10690 if Nkind (Parent (Inst_Node)) = N_Compilation_Unit then
10691 if Parent (Inst_Node) = Cunit (Main_Unit) then
10692 Set_Unit (Parent (Inst_Node), Inst_Node);
10693 Build_Instance_Compilation_Unit_Nodes
10694 (Inst_Node, Pack_Body, Parent (Parent (Act_Decl)));
10695 Analyze (Inst_Node);
10697 Set_Parent (Pack_Body, Parent (Inst_Node));
10698 Analyze (Pack_Body);
10702 Insert_Before (Inst_Node, Pack_Body);
10703 Mark_Rewrite_Insertion (Pack_Body);
10704 Analyze (Pack_Body);
10706 if Expander_Active then
10707 Freeze_Subprogram_Body (Inst_Node, Gen_Body, Pack_Id);
10711 Inherit_Context (Gen_Body, Inst_Node);
10713 Restore_Private_Views (Pack_Id, False);
10715 if Parent_Installed then
10716 Remove_Parent (In_Body => True);
10718 -- Restore the previous visibility of the parent
10720 Set_Is_Immediately_Visible (Par_Ent, Par_Vis);
10724 Style_Check := Saved_Style_Check;
10725 Restore_Warnings (Saved_Warnings);
10727 -- Body not found. Error was emitted already. If there were no previous
10728 -- errors, this may be an instance whose scope is a premature instance.
10729 -- In that case we must insure that the (legal) program does raise
10730 -- program error if executed. We generate a subprogram body for this
10731 -- purpose. See DEC ac30vso.
10733 -- Should not reference proprietary DEC tests in comments ???
10735 elsif Serious_Errors_Detected = 0
10736 and then Nkind (Parent (Inst_Node)) /= N_Compilation_Unit
10738 if Body_Optional then
10741 elsif Ekind (Anon_Id) = E_Procedure then
10743 Make_Subprogram_Body (Loc,
10745 Make_Procedure_Specification (Loc,
10746 Defining_Unit_Name =>
10747 Make_Defining_Identifier (Loc, Chars (Anon_Id)),
10748 Parameter_Specifications =>
10750 (Parameter_Specifications (Parent (Anon_Id)))),
10752 Declarations => Empty_List,
10753 Handled_Statement_Sequence =>
10754 Make_Handled_Sequence_Of_Statements (Loc,
10757 Make_Raise_Program_Error (Loc,
10759 PE_Access_Before_Elaboration))));
10763 Make_Raise_Program_Error (Loc,
10764 Reason => PE_Access_Before_Elaboration);
10766 Set_Etype (Ret_Expr, (Etype (Anon_Id)));
10767 Set_Analyzed (Ret_Expr);
10770 Make_Subprogram_Body (Loc,
10772 Make_Function_Specification (Loc,
10773 Defining_Unit_Name =>
10774 Make_Defining_Identifier (Loc, Chars (Anon_Id)),
10775 Parameter_Specifications =>
10777 (Parameter_Specifications (Parent (Anon_Id))),
10778 Result_Definition =>
10779 New_Occurrence_Of (Etype (Anon_Id), Loc)),
10781 Declarations => Empty_List,
10782 Handled_Statement_Sequence =>
10783 Make_Handled_Sequence_Of_Statements (Loc,
10786 (Make_Simple_Return_Statement (Loc, Ret_Expr))));
10789 Pack_Body := Make_Package_Body (Loc,
10790 Defining_Unit_Name => New_Copy (Pack_Id),
10791 Declarations => New_List (Act_Body));
10793 Insert_After (Inst_Node, Pack_Body);
10794 Set_Corresponding_Spec (Pack_Body, Pack_Id);
10795 Analyze (Pack_Body);
10798 Expander_Mode_Restore;
10799 end Instantiate_Subprogram_Body;
10801 ----------------------
10802 -- Instantiate_Type --
10803 ----------------------
10805 function Instantiate_Type
10808 Analyzed_Formal : Node_Id;
10809 Actual_Decls : List_Id) return List_Id
10811 Gen_T : constant Entity_Id := Defining_Identifier (Formal);
10812 A_Gen_T : constant Entity_Id :=
10813 Defining_Identifier (Analyzed_Formal);
10814 Ancestor : Entity_Id := Empty;
10815 Def : constant Node_Id := Formal_Type_Definition (Formal);
10817 Decl_Node : Node_Id;
10818 Decl_Nodes : List_Id;
10822 procedure Diagnose_Predicated_Actual;
10823 -- There are a number of constructs in which a discrete type with
10824 -- predicates is illegal, e.g. as an index in an array type declaration.
10825 -- If a generic type is used is such a construct in a generic package
10826 -- declaration, it carries the flag No_Predicate_On_Actual. it is part
10827 -- of the generic contract that the actual cannot have predicates.
10829 procedure Validate_Array_Type_Instance;
10830 procedure Validate_Access_Subprogram_Instance;
10831 procedure Validate_Access_Type_Instance;
10832 procedure Validate_Derived_Type_Instance;
10833 procedure Validate_Derived_Interface_Type_Instance;
10834 procedure Validate_Discriminated_Formal_Type;
10835 procedure Validate_Interface_Type_Instance;
10836 procedure Validate_Private_Type_Instance;
10837 procedure Validate_Incomplete_Type_Instance;
10838 -- These procedures perform validation tests for the named case.
10839 -- Validate_Discriminated_Formal_Type is shared by formal private
10840 -- types and Ada 2012 formal incomplete types.
10842 function Subtypes_Match (Gen_T, Act_T : Entity_Id) return Boolean;
10843 -- Check that base types are the same and that the subtypes match
10844 -- statically. Used in several of the above.
10846 ---------------------------------
10847 -- Diagnose_Predicated_Actual --
10848 ---------------------------------
10850 procedure Diagnose_Predicated_Actual is
10852 if No_Predicate_On_Actual (A_Gen_T)
10853 and then Has_Predicates (Act_T)
10856 ("actual for& cannot be a type with predicate",
10857 Instantiation_Node, A_Gen_T);
10859 elsif No_Dynamic_Predicate_On_Actual (A_Gen_T)
10860 and then Has_Predicates (Act_T)
10861 and then not Has_Static_Predicate_Aspect (Act_T)
10864 ("actual for& cannot be a type with a dynamic predicate",
10865 Instantiation_Node, A_Gen_T);
10867 end Diagnose_Predicated_Actual;
10869 --------------------
10870 -- Subtypes_Match --
10871 --------------------
10873 function Subtypes_Match (Gen_T, Act_T : Entity_Id) return Boolean is
10874 T : constant Entity_Id := Get_Instance_Of (Gen_T);
10877 -- Some detailed comments would be useful here ???
10879 return ((Base_Type (T) = Act_T
10880 or else Base_Type (T) = Base_Type (Act_T))
10881 and then Subtypes_Statically_Match (T, Act_T))
10883 or else (Is_Class_Wide_Type (Gen_T)
10884 and then Is_Class_Wide_Type (Act_T)
10885 and then Subtypes_Match
10886 (Get_Instance_Of (Root_Type (Gen_T)),
10887 Root_Type (Act_T)))
10890 (Ekind_In (Gen_T, E_Anonymous_Access_Subprogram_Type,
10891 E_Anonymous_Access_Type)
10892 and then Ekind (Act_T) = Ekind (Gen_T)
10893 and then Subtypes_Statically_Match
10894 (Designated_Type (Gen_T), Designated_Type (Act_T)));
10895 end Subtypes_Match;
10897 -----------------------------------------
10898 -- Validate_Access_Subprogram_Instance --
10899 -----------------------------------------
10901 procedure Validate_Access_Subprogram_Instance is
10903 if not Is_Access_Type (Act_T)
10904 or else Ekind (Designated_Type (Act_T)) /= E_Subprogram_Type
10907 ("expect access type in instantiation of &", Actual, Gen_T);
10908 Abandon_Instantiation (Actual);
10911 -- According to AI05-288, actuals for access_to_subprograms must be
10912 -- subtype conformant with the generic formal. Previous to AI05-288
10913 -- only mode conformance was required.
10915 -- This is a binding interpretation that applies to previous versions
10916 -- of the language, no need to maintain previous weaker checks.
10918 Check_Subtype_Conformant
10919 (Designated_Type (Act_T),
10920 Designated_Type (A_Gen_T),
10924 if Ekind (Base_Type (Act_T)) = E_Access_Protected_Subprogram_Type then
10925 if Ekind (A_Gen_T) = E_Access_Subprogram_Type then
10927 ("protected access type not allowed for formal &",
10931 elsif Ekind (A_Gen_T) = E_Access_Protected_Subprogram_Type then
10933 ("expect protected access type for formal &",
10936 end Validate_Access_Subprogram_Instance;
10938 -----------------------------------
10939 -- Validate_Access_Type_Instance --
10940 -----------------------------------
10942 procedure Validate_Access_Type_Instance is
10943 Desig_Type : constant Entity_Id :=
10944 Find_Actual_Type (Designated_Type (A_Gen_T), A_Gen_T);
10945 Desig_Act : Entity_Id;
10948 if not Is_Access_Type (Act_T) then
10950 ("expect access type in instantiation of &", Actual, Gen_T);
10951 Abandon_Instantiation (Actual);
10954 if Is_Access_Constant (A_Gen_T) then
10955 if not Is_Access_Constant (Act_T) then
10957 ("actual type must be access-to-constant type", Actual);
10958 Abandon_Instantiation (Actual);
10961 if Is_Access_Constant (Act_T) then
10963 ("actual type must be access-to-variable type", Actual);
10964 Abandon_Instantiation (Actual);
10966 elsif Ekind (A_Gen_T) = E_General_Access_Type
10967 and then Ekind (Base_Type (Act_T)) /= E_General_Access_Type
10969 Error_Msg_N -- CODEFIX
10970 ("actual must be general access type!", Actual);
10971 Error_Msg_NE -- CODEFIX
10972 ("add ALL to }!", Actual, Act_T);
10973 Abandon_Instantiation (Actual);
10977 -- The designated subtypes, that is to say the subtypes introduced
10978 -- by an access type declaration (and not by a subtype declaration)
10981 Desig_Act := Designated_Type (Base_Type (Act_T));
10983 -- The designated type may have been introduced through a limited_
10984 -- with clause, in which case retrieve the non-limited view. This
10985 -- applies to incomplete types as well as to class-wide types.
10987 if From_Limited_With (Desig_Act) then
10988 Desig_Act := Available_View (Desig_Act);
10991 if not Subtypes_Match (Desig_Type, Desig_Act) then
10993 ("designated type of actual does not match that of formal &",
10996 if not Predicates_Match (Desig_Type, Desig_Act) then
10997 Error_Msg_N ("\predicates do not match", Actual);
11000 Abandon_Instantiation (Actual);
11002 elsif Is_Access_Type (Designated_Type (Act_T))
11003 and then Is_Constrained (Designated_Type (Designated_Type (Act_T)))
11005 Is_Constrained (Designated_Type (Desig_Type))
11008 ("designated type of actual does not match that of formal &",
11011 if not Predicates_Match (Desig_Type, Desig_Act) then
11012 Error_Msg_N ("\predicates do not match", Actual);
11015 Abandon_Instantiation (Actual);
11018 -- Ada 2005: null-exclusion indicators of the two types must agree
11020 if Can_Never_Be_Null (A_Gen_T) /= Can_Never_Be_Null (Act_T) then
11022 ("non null exclusion of actual and formal & do not match",
11025 end Validate_Access_Type_Instance;
11027 ----------------------------------
11028 -- Validate_Array_Type_Instance --
11029 ----------------------------------
11031 procedure Validate_Array_Type_Instance is
11036 function Formal_Dimensions return Int;
11037 -- Count number of dimensions in array type formal
11039 -----------------------
11040 -- Formal_Dimensions --
11041 -----------------------
11043 function Formal_Dimensions return Int is
11048 if Nkind (Def) = N_Constrained_Array_Definition then
11049 Index := First (Discrete_Subtype_Definitions (Def));
11051 Index := First (Subtype_Marks (Def));
11054 while Present (Index) loop
11056 Next_Index (Index);
11060 end Formal_Dimensions;
11062 -- Start of processing for Validate_Array_Type_Instance
11065 if not Is_Array_Type (Act_T) then
11067 ("expect array type in instantiation of &", Actual, Gen_T);
11068 Abandon_Instantiation (Actual);
11070 elsif Nkind (Def) = N_Constrained_Array_Definition then
11071 if not (Is_Constrained (Act_T)) then
11073 ("expect constrained array in instantiation of &",
11075 Abandon_Instantiation (Actual);
11079 if Is_Constrained (Act_T) then
11081 ("expect unconstrained array in instantiation of &",
11083 Abandon_Instantiation (Actual);
11087 if Formal_Dimensions /= Number_Dimensions (Act_T) then
11089 ("dimensions of actual do not match formal &", Actual, Gen_T);
11090 Abandon_Instantiation (Actual);
11093 I1 := First_Index (A_Gen_T);
11094 I2 := First_Index (Act_T);
11095 for J in 1 .. Formal_Dimensions loop
11097 -- If the indexes of the actual were given by a subtype_mark,
11098 -- the index was transformed into a range attribute. Retrieve
11099 -- the original type mark for checking.
11101 if Is_Entity_Name (Original_Node (I2)) then
11102 T2 := Entity (Original_Node (I2));
11107 if not Subtypes_Match
11108 (Find_Actual_Type (Etype (I1), A_Gen_T), T2)
11111 ("index types of actual do not match those of formal &",
11113 Abandon_Instantiation (Actual);
11120 -- Check matching subtypes. Note that there are complex visibility
11121 -- issues when the generic is a child unit and some aspect of the
11122 -- generic type is declared in a parent unit of the generic. We do
11123 -- the test to handle this special case only after a direct check
11124 -- for static matching has failed. The case where both the component
11125 -- type and the array type are separate formals, and the component
11126 -- type is a private view may also require special checking in
11130 (Component_Type (A_Gen_T), Component_Type (Act_T))
11131 or else Subtypes_Match
11132 (Find_Actual_Type (Component_Type (A_Gen_T), A_Gen_T),
11133 Component_Type (Act_T))
11138 ("component subtype of actual does not match that of formal &",
11140 Abandon_Instantiation (Actual);
11143 if Has_Aliased_Components (A_Gen_T)
11144 and then not Has_Aliased_Components (Act_T)
11147 ("actual must have aliased components to match formal type &",
11150 end Validate_Array_Type_Instance;
11152 -----------------------------------------------
11153 -- Validate_Derived_Interface_Type_Instance --
11154 -----------------------------------------------
11156 procedure Validate_Derived_Interface_Type_Instance is
11157 Par : constant Entity_Id := Entity (Subtype_Indication (Def));
11161 -- First apply interface instance checks
11163 Validate_Interface_Type_Instance;
11165 -- Verify that immediate parent interface is an ancestor of
11169 and then not Interface_Present_In_Ancestor (Act_T, Par)
11172 ("interface actual must include progenitor&", Actual, Par);
11175 -- Now verify that the actual includes all other ancestors of
11178 Elmt := First_Elmt (Interfaces (A_Gen_T));
11179 while Present (Elmt) loop
11180 if not Interface_Present_In_Ancestor
11181 (Act_T, Get_Instance_Of (Node (Elmt)))
11184 ("interface actual must include progenitor&",
11185 Actual, Node (Elmt));
11190 end Validate_Derived_Interface_Type_Instance;
11192 ------------------------------------
11193 -- Validate_Derived_Type_Instance --
11194 ------------------------------------
11196 procedure Validate_Derived_Type_Instance is
11197 Actual_Discr : Entity_Id;
11198 Ancestor_Discr : Entity_Id;
11201 -- If the parent type in the generic declaration is itself a previous
11202 -- formal type, then it is local to the generic and absent from the
11203 -- analyzed generic definition. In that case the ancestor is the
11204 -- instance of the formal (which must have been instantiated
11205 -- previously), unless the ancestor is itself a formal derived type.
11206 -- In this latter case (which is the subject of Corrigendum 8652/0038
11207 -- (AI-202) the ancestor of the formals is the ancestor of its
11208 -- parent. Otherwise, the analyzed generic carries the parent type.
11209 -- If the parent type is defined in a previous formal package, then
11210 -- the scope of that formal package is that of the generic type
11211 -- itself, and it has already been mapped into the corresponding type
11212 -- in the actual package.
11214 -- Common case: parent type defined outside of the generic
11216 if Is_Entity_Name (Subtype_Mark (Def))
11217 and then Present (Entity (Subtype_Mark (Def)))
11219 Ancestor := Get_Instance_Of (Entity (Subtype_Mark (Def)));
11221 -- Check whether parent is defined in a previous formal package
11224 Scope (Scope (Base_Type (Etype (A_Gen_T)))) = Scope (A_Gen_T)
11227 Get_Instance_Of (Base_Type (Etype (A_Gen_T)));
11229 -- The type may be a local derivation, or a type extension of a
11230 -- previous formal, or of a formal of a parent package.
11232 elsif Is_Derived_Type (Get_Instance_Of (A_Gen_T))
11234 Ekind (Get_Instance_Of (A_Gen_T)) = E_Record_Type_With_Private
11236 -- Check whether the parent is another derived formal type in the
11237 -- same generic unit.
11239 if Etype (A_Gen_T) /= A_Gen_T
11240 and then Is_Generic_Type (Etype (A_Gen_T))
11241 and then Scope (Etype (A_Gen_T)) = Scope (A_Gen_T)
11242 and then Etype (Etype (A_Gen_T)) /= Etype (A_Gen_T)
11244 -- Locate ancestor of parent from the subtype declaration
11245 -- created for the actual.
11251 Decl := First (Actual_Decls);
11252 while Present (Decl) loop
11253 if Nkind (Decl) = N_Subtype_Declaration
11254 and then Chars (Defining_Identifier (Decl)) =
11255 Chars (Etype (A_Gen_T))
11257 Ancestor := Generic_Parent_Type (Decl);
11265 pragma Assert (Present (Ancestor));
11267 -- The ancestor itself may be a previous formal that has been
11270 Ancestor := Get_Instance_Of (Ancestor);
11274 Get_Instance_Of (Base_Type (Get_Instance_Of (A_Gen_T)));
11277 -- An unusual case: the actual is a type declared in a parent unit,
11278 -- but is not a formal type so there is no instance_of for it.
11279 -- Retrieve it by analyzing the record extension.
11281 elsif Is_Child_Unit (Scope (A_Gen_T))
11282 and then In_Open_Scopes (Scope (Act_T))
11283 and then Is_Generic_Instance (Scope (Act_T))
11285 Analyze (Subtype_Mark (Def));
11286 Ancestor := Entity (Subtype_Mark (Def));
11289 Ancestor := Get_Instance_Of (Etype (Base_Type (A_Gen_T)));
11292 -- If the formal derived type has pragma Preelaborable_Initialization
11293 -- then the actual type must have preelaborable initialization.
11295 if Known_To_Have_Preelab_Init (A_Gen_T)
11296 and then not Has_Preelaborable_Initialization (Act_T)
11299 ("actual for & must have preelaborable initialization",
11303 -- Ada 2005 (AI-251)
11305 if Ada_Version >= Ada_2005 and then Is_Interface (Ancestor) then
11306 if not Interface_Present_In_Ancestor (Act_T, Ancestor) then
11308 ("(Ada 2005) expected type implementing & in instantiation",
11312 elsif not Is_Ancestor (Base_Type (Ancestor), Act_T) then
11314 ("expect type derived from & in instantiation",
11315 Actual, First_Subtype (Ancestor));
11316 Abandon_Instantiation (Actual);
11319 -- Ada 2005 (AI-443): Synchronized formal derived type checks. Note
11320 -- that the formal type declaration has been rewritten as a private
11323 if Ada_Version >= Ada_2005
11324 and then Nkind (Parent (A_Gen_T)) = N_Private_Extension_Declaration
11325 and then Synchronized_Present (Parent (A_Gen_T))
11327 -- The actual must be a synchronized tagged type
11329 if not Is_Tagged_Type (Act_T) then
11331 ("actual of synchronized type must be tagged", Actual);
11332 Abandon_Instantiation (Actual);
11334 elsif Nkind (Parent (Act_T)) = N_Full_Type_Declaration
11335 and then Nkind (Type_Definition (Parent (Act_T))) =
11336 N_Derived_Type_Definition
11337 and then not Synchronized_Present (Type_Definition
11341 ("actual of synchronized type must be synchronized", Actual);
11342 Abandon_Instantiation (Actual);
11346 -- Perform atomic/volatile checks (RM C.6(12)). Note that AI05-0218-1
11347 -- removes the second instance of the phrase "or allow pass by copy".
11349 if Is_Atomic (Act_T) and then not Is_Atomic (Ancestor) then
11351 ("cannot have atomic actual type for non-atomic formal type",
11354 elsif Is_Volatile (Act_T) and then not Is_Volatile (Ancestor) then
11356 ("cannot have volatile actual type for non-volatile formal type",
11360 -- It should not be necessary to check for unknown discriminants on
11361 -- Formal, but for some reason Has_Unknown_Discriminants is false for
11362 -- A_Gen_T, so Is_Indefinite_Subtype incorrectly returns False. This
11363 -- needs fixing. ???
11365 if not Is_Indefinite_Subtype (A_Gen_T)
11366 and then not Unknown_Discriminants_Present (Formal)
11367 and then Is_Indefinite_Subtype (Act_T)
11370 ("actual subtype must be constrained", Actual);
11371 Abandon_Instantiation (Actual);
11374 if not Unknown_Discriminants_Present (Formal) then
11375 if Is_Constrained (Ancestor) then
11376 if not Is_Constrained (Act_T) then
11378 ("actual subtype must be constrained", Actual);
11379 Abandon_Instantiation (Actual);
11382 -- Ancestor is unconstrained, Check if generic formal and actual
11383 -- agree on constrainedness. The check only applies to array types
11384 -- and discriminated types.
11386 elsif Is_Constrained (Act_T) then
11387 if Ekind (Ancestor) = E_Access_Type
11389 (not Is_Constrained (A_Gen_T)
11390 and then Is_Composite_Type (A_Gen_T))
11393 ("actual subtype must be unconstrained", Actual);
11394 Abandon_Instantiation (Actual);
11397 -- A class-wide type is only allowed if the formal has unknown
11400 elsif Is_Class_Wide_Type (Act_T)
11401 and then not Has_Unknown_Discriminants (Ancestor)
11404 ("actual for & cannot be a class-wide type", Actual, Gen_T);
11405 Abandon_Instantiation (Actual);
11407 -- Otherwise, the formal and actual must have the same number
11408 -- of discriminants and each discriminant of the actual must
11409 -- correspond to a discriminant of the formal.
11411 elsif Has_Discriminants (Act_T)
11412 and then not Has_Unknown_Discriminants (Act_T)
11413 and then Has_Discriminants (Ancestor)
11415 Actual_Discr := First_Discriminant (Act_T);
11416 Ancestor_Discr := First_Discriminant (Ancestor);
11417 while Present (Actual_Discr)
11418 and then Present (Ancestor_Discr)
11420 if Base_Type (Act_T) /= Base_Type (Ancestor) and then
11421 No (Corresponding_Discriminant (Actual_Discr))
11424 ("discriminant & does not correspond " &
11425 "to ancestor discriminant", Actual, Actual_Discr);
11426 Abandon_Instantiation (Actual);
11429 Next_Discriminant (Actual_Discr);
11430 Next_Discriminant (Ancestor_Discr);
11433 if Present (Actual_Discr) or else Present (Ancestor_Discr) then
11435 ("actual for & must have same number of discriminants",
11437 Abandon_Instantiation (Actual);
11440 -- This case should be caught by the earlier check for
11441 -- constrainedness, but the check here is added for completeness.
11443 elsif Has_Discriminants (Act_T)
11444 and then not Has_Unknown_Discriminants (Act_T)
11447 ("actual for & must not have discriminants", Actual, Gen_T);
11448 Abandon_Instantiation (Actual);
11450 elsif Has_Discriminants (Ancestor) then
11452 ("actual for & must have known discriminants", Actual, Gen_T);
11453 Abandon_Instantiation (Actual);
11456 if not Subtypes_Statically_Compatible
11457 (Act_T, Ancestor, Formal_Derived_Matching => True)
11460 ("constraint on actual is incompatible with formal", Actual);
11461 Abandon_Instantiation (Actual);
11465 -- If the formal and actual types are abstract, check that there
11466 -- are no abstract primitives of the actual type that correspond to
11467 -- nonabstract primitives of the formal type (second sentence of
11470 if Is_Abstract_Type (A_Gen_T) and then Is_Abstract_Type (Act_T) then
11471 Check_Abstract_Primitives : declare
11472 Gen_Prims : constant Elist_Id :=
11473 Primitive_Operations (A_Gen_T);
11474 Gen_Elmt : Elmt_Id;
11475 Gen_Subp : Entity_Id;
11476 Anc_Subp : Entity_Id;
11477 Anc_Formal : Entity_Id;
11478 Anc_F_Type : Entity_Id;
11480 Act_Prims : constant Elist_Id := Primitive_Operations (Act_T);
11481 Act_Elmt : Elmt_Id;
11482 Act_Subp : Entity_Id;
11483 Act_Formal : Entity_Id;
11484 Act_F_Type : Entity_Id;
11486 Subprograms_Correspond : Boolean;
11488 function Is_Tagged_Ancestor (T1, T2 : Entity_Id) return Boolean;
11489 -- Returns true if T2 is derived directly or indirectly from
11490 -- T1, including derivations from interfaces. T1 and T2 are
11491 -- required to be specific tagged base types.
11493 ------------------------
11494 -- Is_Tagged_Ancestor --
11495 ------------------------
11497 function Is_Tagged_Ancestor (T1, T2 : Entity_Id) return Boolean
11499 Intfc_Elmt : Elmt_Id;
11502 -- The predicate is satisfied if the types are the same
11507 -- If we've reached the top of the derivation chain then
11508 -- we know that T1 is not an ancestor of T2.
11510 elsif Etype (T2) = T2 then
11513 -- Proceed to check T2's immediate parent
11515 elsif Is_Ancestor (T1, Base_Type (Etype (T2))) then
11518 -- Finally, check to see if T1 is an ancestor of any of T2's
11522 Intfc_Elmt := First_Elmt (Interfaces (T2));
11523 while Present (Intfc_Elmt) loop
11524 if Is_Ancestor (T1, Node (Intfc_Elmt)) then
11528 Next_Elmt (Intfc_Elmt);
11533 end Is_Tagged_Ancestor;
11535 -- Start of processing for Check_Abstract_Primitives
11538 -- Loop over all of the formal derived type's primitives
11540 Gen_Elmt := First_Elmt (Gen_Prims);
11541 while Present (Gen_Elmt) loop
11542 Gen_Subp := Node (Gen_Elmt);
11544 -- If the primitive of the formal is not abstract, then
11545 -- determine whether there is a corresponding primitive of
11546 -- the actual type that's abstract.
11548 if not Is_Abstract_Subprogram (Gen_Subp) then
11549 Act_Elmt := First_Elmt (Act_Prims);
11550 while Present (Act_Elmt) loop
11551 Act_Subp := Node (Act_Elmt);
11553 -- If we find an abstract primitive of the actual,
11554 -- then we need to test whether it corresponds to the
11555 -- subprogram from which the generic formal primitive
11558 if Is_Abstract_Subprogram (Act_Subp) then
11559 Anc_Subp := Alias (Gen_Subp);
11561 -- Test whether we have a corresponding primitive
11562 -- by comparing names, kinds, formal types, and
11565 if Chars (Anc_Subp) = Chars (Act_Subp)
11566 and then Ekind (Anc_Subp) = Ekind (Act_Subp)
11568 Anc_Formal := First_Formal (Anc_Subp);
11569 Act_Formal := First_Formal (Act_Subp);
11570 while Present (Anc_Formal)
11571 and then Present (Act_Formal)
11573 Anc_F_Type := Etype (Anc_Formal);
11574 Act_F_Type := Etype (Act_Formal);
11576 if Ekind (Anc_F_Type)
11577 = E_Anonymous_Access_Type
11579 Anc_F_Type := Designated_Type (Anc_F_Type);
11581 if Ekind (Act_F_Type)
11582 = E_Anonymous_Access_Type
11585 Designated_Type (Act_F_Type);
11591 Ekind (Act_F_Type) = E_Anonymous_Access_Type
11596 Anc_F_Type := Base_Type (Anc_F_Type);
11597 Act_F_Type := Base_Type (Act_F_Type);
11599 -- If the formal is controlling, then the
11600 -- the type of the actual primitive's formal
11601 -- must be derived directly or indirectly
11602 -- from the type of the ancestor primitive's
11605 if Is_Controlling_Formal (Anc_Formal) then
11606 if not Is_Tagged_Ancestor
11607 (Anc_F_Type, Act_F_Type)
11612 -- Otherwise the types of the formals must
11615 elsif Anc_F_Type /= Act_F_Type then
11619 Next_Entity (Anc_Formal);
11620 Next_Entity (Act_Formal);
11623 -- If we traversed through all of the formals
11624 -- then so far the subprograms correspond, so
11625 -- now check that any result types correspond.
11627 if No (Anc_Formal) and then No (Act_Formal) then
11628 Subprograms_Correspond := True;
11630 if Ekind (Act_Subp) = E_Function then
11631 Anc_F_Type := Etype (Anc_Subp);
11632 Act_F_Type := Etype (Act_Subp);
11634 if Ekind (Anc_F_Type)
11635 = E_Anonymous_Access_Type
11638 Designated_Type (Anc_F_Type);
11640 if Ekind (Act_F_Type)
11641 = E_Anonymous_Access_Type
11644 Designated_Type (Act_F_Type);
11646 Subprograms_Correspond := False;
11651 = E_Anonymous_Access_Type
11653 Subprograms_Correspond := False;
11656 Anc_F_Type := Base_Type (Anc_F_Type);
11657 Act_F_Type := Base_Type (Act_F_Type);
11659 -- Now either the result types must be
11660 -- the same or, if the result type is
11661 -- controlling, the result type of the
11662 -- actual primitive must descend from the
11663 -- result type of the ancestor primitive.
11665 if Subprograms_Correspond
11666 and then Anc_F_Type /= Act_F_Type
11668 Has_Controlling_Result (Anc_Subp)
11670 not Is_Tagged_Ancestor
11671 (Anc_F_Type, Act_F_Type)
11673 Subprograms_Correspond := False;
11677 -- Found a matching subprogram belonging to
11678 -- formal ancestor type, so actual subprogram
11679 -- corresponds and this violates 3.9.3(9).
11681 if Subprograms_Correspond then
11683 ("abstract subprogram & overrides " &
11684 "nonabstract subprogram of ancestor",
11692 Next_Elmt (Act_Elmt);
11696 Next_Elmt (Gen_Elmt);
11698 end Check_Abstract_Primitives;
11701 -- Verify that limitedness matches. If parent is a limited
11702 -- interface then the generic formal is not unless declared
11703 -- explicitly so. If not declared limited, the actual cannot be
11704 -- limited (see AI05-0087).
11706 -- Even though this AI is a binding interpretation, we enable the
11707 -- check only in Ada 2012 mode, because this improper construct
11708 -- shows up in user code and in existing B-tests.
11710 if Is_Limited_Type (Act_T)
11711 and then not Is_Limited_Type (A_Gen_T)
11712 and then Ada_Version >= Ada_2012
11714 if In_Instance then
11718 ("actual for non-limited & cannot be a limited type", Actual,
11720 Explain_Limited_Type (Act_T, Actual);
11721 Abandon_Instantiation (Actual);
11724 end Validate_Derived_Type_Instance;
11726 ----------------------------------------
11727 -- Validate_Discriminated_Formal_Type --
11728 ----------------------------------------
11730 procedure Validate_Discriminated_Formal_Type is
11731 Formal_Discr : Entity_Id;
11732 Actual_Discr : Entity_Id;
11733 Formal_Subt : Entity_Id;
11736 if Has_Discriminants (A_Gen_T) then
11737 if not Has_Discriminants (Act_T) then
11739 ("actual for & must have discriminants", Actual, Gen_T);
11740 Abandon_Instantiation (Actual);
11742 elsif Is_Constrained (Act_T) then
11744 ("actual for & must be unconstrained", Actual, Gen_T);
11745 Abandon_Instantiation (Actual);
11748 Formal_Discr := First_Discriminant (A_Gen_T);
11749 Actual_Discr := First_Discriminant (Act_T);
11750 while Formal_Discr /= Empty loop
11751 if Actual_Discr = Empty then
11753 ("discriminants on actual do not match formal",
11755 Abandon_Instantiation (Actual);
11758 Formal_Subt := Get_Instance_Of (Etype (Formal_Discr));
11760 -- Access discriminants match if designated types do
11762 if Ekind (Base_Type (Formal_Subt)) = E_Anonymous_Access_Type
11763 and then (Ekind (Base_Type (Etype (Actual_Discr)))) =
11764 E_Anonymous_Access_Type
11767 (Designated_Type (Base_Type (Formal_Subt))) =
11768 Designated_Type (Base_Type (Etype (Actual_Discr)))
11772 elsif Base_Type (Formal_Subt) /=
11773 Base_Type (Etype (Actual_Discr))
11776 ("types of actual discriminants must match formal",
11778 Abandon_Instantiation (Actual);
11780 elsif not Subtypes_Statically_Match
11781 (Formal_Subt, Etype (Actual_Discr))
11782 and then Ada_Version >= Ada_95
11785 ("subtypes of actual discriminants must match formal",
11787 Abandon_Instantiation (Actual);
11790 Next_Discriminant (Formal_Discr);
11791 Next_Discriminant (Actual_Discr);
11794 if Actual_Discr /= Empty then
11796 ("discriminants on actual do not match formal",
11798 Abandon_Instantiation (Actual);
11802 end Validate_Discriminated_Formal_Type;
11804 ---------------------------------------
11805 -- Validate_Incomplete_Type_Instance --
11806 ---------------------------------------
11808 procedure Validate_Incomplete_Type_Instance is
11810 if not Is_Tagged_Type (Act_T)
11811 and then Is_Tagged_Type (A_Gen_T)
11814 ("actual for & must be a tagged type", Actual, Gen_T);
11817 Validate_Discriminated_Formal_Type;
11818 end Validate_Incomplete_Type_Instance;
11820 --------------------------------------
11821 -- Validate_Interface_Type_Instance --
11822 --------------------------------------
11824 procedure Validate_Interface_Type_Instance is
11826 if not Is_Interface (Act_T) then
11828 ("actual for formal interface type must be an interface",
11831 elsif Is_Limited_Type (Act_T) /= Is_Limited_Type (A_Gen_T)
11833 Is_Task_Interface (A_Gen_T) /= Is_Task_Interface (Act_T)
11835 Is_Protected_Interface (A_Gen_T) /=
11836 Is_Protected_Interface (Act_T)
11838 Is_Synchronized_Interface (A_Gen_T) /=
11839 Is_Synchronized_Interface (Act_T)
11842 ("actual for interface& does not match (RM 12.5.5(4))",
11845 end Validate_Interface_Type_Instance;
11847 ------------------------------------
11848 -- Validate_Private_Type_Instance --
11849 ------------------------------------
11851 procedure Validate_Private_Type_Instance is
11853 if Is_Limited_Type (Act_T)
11854 and then not Is_Limited_Type (A_Gen_T)
11856 if In_Instance then
11860 ("actual for non-limited & cannot be a limited type", Actual,
11862 Explain_Limited_Type (Act_T, Actual);
11863 Abandon_Instantiation (Actual);
11866 elsif Known_To_Have_Preelab_Init (A_Gen_T)
11867 and then not Has_Preelaborable_Initialization (Act_T)
11870 ("actual for & must have preelaborable initialization", Actual,
11873 elsif Is_Indefinite_Subtype (Act_T)
11874 and then not Is_Indefinite_Subtype (A_Gen_T)
11875 and then Ada_Version >= Ada_95
11878 ("actual for & must be a definite subtype", Actual, Gen_T);
11880 elsif not Is_Tagged_Type (Act_T)
11881 and then Is_Tagged_Type (A_Gen_T)
11884 ("actual for & must be a tagged type", Actual, Gen_T);
11887 Validate_Discriminated_Formal_Type;
11889 end Validate_Private_Type_Instance;
11891 -- Start of processing for Instantiate_Type
11894 if Get_Instance_Of (A_Gen_T) /= A_Gen_T then
11895 Error_Msg_N ("duplicate instantiation of generic type", Actual);
11896 return New_List (Error);
11898 elsif not Is_Entity_Name (Actual)
11899 or else not Is_Type (Entity (Actual))
11902 ("expect valid subtype mark to instantiate &", Actual, Gen_T);
11903 Abandon_Instantiation (Actual);
11906 Act_T := Entity (Actual);
11908 -- Ada 2005 (AI-216): An Unchecked_Union subtype shall only be passed
11909 -- as a generic actual parameter if the corresponding formal type
11910 -- does not have a known_discriminant_part, or is a formal derived
11911 -- type that is an Unchecked_Union type.
11913 if Is_Unchecked_Union (Base_Type (Act_T)) then
11914 if not Has_Discriminants (A_Gen_T)
11916 (Is_Derived_Type (A_Gen_T)
11918 Is_Unchecked_Union (A_Gen_T))
11922 Error_Msg_N ("unchecked union cannot be the actual for a" &
11923 " discriminated formal type", Act_T);
11928 -- Deal with fixed/floating restrictions
11930 if Is_Floating_Point_Type (Act_T) then
11931 Check_Restriction (No_Floating_Point, Actual);
11932 elsif Is_Fixed_Point_Type (Act_T) then
11933 Check_Restriction (No_Fixed_Point, Actual);
11936 -- Deal with error of using incomplete type as generic actual.
11937 -- This includes limited views of a type, even if the non-limited
11938 -- view may be available.
11940 if Ekind (Act_T) = E_Incomplete_Type
11941 or else (Is_Class_Wide_Type (Act_T)
11943 Ekind (Root_Type (Act_T)) = E_Incomplete_Type)
11945 -- If the formal is an incomplete type, the actual can be
11946 -- incomplete as well.
11948 if Ekind (A_Gen_T) = E_Incomplete_Type then
11951 elsif Is_Class_Wide_Type (Act_T)
11952 or else No (Full_View (Act_T))
11954 Error_Msg_N ("premature use of incomplete type", Actual);
11955 Abandon_Instantiation (Actual);
11957 Act_T := Full_View (Act_T);
11958 Set_Entity (Actual, Act_T);
11960 if Has_Private_Component (Act_T) then
11962 ("premature use of type with private component", Actual);
11966 -- Deal with error of premature use of private type as generic actual
11968 elsif Is_Private_Type (Act_T)
11969 and then Is_Private_Type (Base_Type (Act_T))
11970 and then not Is_Generic_Type (Act_T)
11971 and then not Is_Derived_Type (Act_T)
11972 and then No (Full_View (Root_Type (Act_T)))
11974 -- If the formal is an incomplete type, the actual can be
11975 -- private or incomplete as well.
11977 if Ekind (A_Gen_T) = E_Incomplete_Type then
11980 Error_Msg_N ("premature use of private type", Actual);
11983 elsif Has_Private_Component (Act_T) then
11985 ("premature use of type with private component", Actual);
11988 Set_Instance_Of (A_Gen_T, Act_T);
11990 -- If the type is generic, the class-wide type may also be used
11992 if Is_Tagged_Type (A_Gen_T)
11993 and then Is_Tagged_Type (Act_T)
11994 and then not Is_Class_Wide_Type (A_Gen_T)
11996 Set_Instance_Of (Class_Wide_Type (A_Gen_T),
11997 Class_Wide_Type (Act_T));
12000 if not Is_Abstract_Type (A_Gen_T)
12001 and then Is_Abstract_Type (Act_T)
12004 ("actual of non-abstract formal cannot be abstract", Actual);
12007 -- A generic scalar type is a first subtype for which we generate
12008 -- an anonymous base type. Indicate that the instance of this base
12009 -- is the base type of the actual.
12011 if Is_Scalar_Type (A_Gen_T) then
12012 Set_Instance_Of (Etype (A_Gen_T), Etype (Act_T));
12016 if Error_Posted (Act_T) then
12019 case Nkind (Def) is
12020 when N_Formal_Private_Type_Definition =>
12021 Validate_Private_Type_Instance;
12023 when N_Formal_Incomplete_Type_Definition =>
12024 Validate_Incomplete_Type_Instance;
12026 when N_Formal_Derived_Type_Definition =>
12027 Validate_Derived_Type_Instance;
12029 when N_Formal_Discrete_Type_Definition =>
12030 if not Is_Discrete_Type (Act_T) then
12032 ("expect discrete type in instantiation of&",
12034 Abandon_Instantiation (Actual);
12037 Diagnose_Predicated_Actual;
12039 when N_Formal_Signed_Integer_Type_Definition =>
12040 if not Is_Signed_Integer_Type (Act_T) then
12042 ("expect signed integer type in instantiation of&",
12044 Abandon_Instantiation (Actual);
12047 Diagnose_Predicated_Actual;
12049 when N_Formal_Modular_Type_Definition =>
12050 if not Is_Modular_Integer_Type (Act_T) then
12052 ("expect modular type in instantiation of &",
12054 Abandon_Instantiation (Actual);
12057 Diagnose_Predicated_Actual;
12059 when N_Formal_Floating_Point_Definition =>
12060 if not Is_Floating_Point_Type (Act_T) then
12062 ("expect float type in instantiation of &", Actual, Gen_T);
12063 Abandon_Instantiation (Actual);
12066 when N_Formal_Ordinary_Fixed_Point_Definition =>
12067 if not Is_Ordinary_Fixed_Point_Type (Act_T) then
12069 ("expect ordinary fixed point type in instantiation of &",
12071 Abandon_Instantiation (Actual);
12074 when N_Formal_Decimal_Fixed_Point_Definition =>
12075 if not Is_Decimal_Fixed_Point_Type (Act_T) then
12077 ("expect decimal type in instantiation of &",
12079 Abandon_Instantiation (Actual);
12082 when N_Array_Type_Definition =>
12083 Validate_Array_Type_Instance;
12085 when N_Access_To_Object_Definition =>
12086 Validate_Access_Type_Instance;
12088 when N_Access_Function_Definition |
12089 N_Access_Procedure_Definition =>
12090 Validate_Access_Subprogram_Instance;
12092 when N_Record_Definition =>
12093 Validate_Interface_Type_Instance;
12095 when N_Derived_Type_Definition =>
12096 Validate_Derived_Interface_Type_Instance;
12099 raise Program_Error;
12104 Subt := New_Copy (Gen_T);
12106 -- Use adjusted sloc of subtype name as the location for other nodes in
12107 -- the subtype declaration.
12109 Loc := Sloc (Subt);
12112 Make_Subtype_Declaration (Loc,
12113 Defining_Identifier => Subt,
12114 Subtype_Indication => New_Occurrence_Of (Act_T, Loc));
12116 if Is_Private_Type (Act_T) then
12117 Set_Has_Private_View (Subtype_Indication (Decl_Node));
12119 elsif Is_Access_Type (Act_T)
12120 and then Is_Private_Type (Designated_Type (Act_T))
12122 Set_Has_Private_View (Subtype_Indication (Decl_Node));
12125 Decl_Nodes := New_List (Decl_Node);
12127 -- Flag actual derived types so their elaboration produces the
12128 -- appropriate renamings for the primitive operations of the ancestor.
12129 -- Flag actual for formal private types as well, to determine whether
12130 -- operations in the private part may override inherited operations.
12131 -- If the formal has an interface list, the ancestor is not the
12132 -- parent, but the analyzed formal that includes the interface
12133 -- operations of all its progenitors.
12135 -- Same treatment for formal private types, so we can check whether the
12136 -- type is tagged limited when validating derivations in the private
12137 -- part. (See AI05-096).
12139 if Nkind (Def) = N_Formal_Derived_Type_Definition then
12140 if Present (Interface_List (Def)) then
12141 Set_Generic_Parent_Type (Decl_Node, A_Gen_T);
12143 Set_Generic_Parent_Type (Decl_Node, Ancestor);
12146 elsif Nkind_In (Def,
12147 N_Formal_Private_Type_Definition,
12148 N_Formal_Incomplete_Type_Definition)
12150 Set_Generic_Parent_Type (Decl_Node, A_Gen_T);
12153 -- If the actual is a synchronized type that implements an interface,
12154 -- the primitive operations are attached to the corresponding record,
12155 -- and we have to treat it as an additional generic actual, so that its
12156 -- primitive operations become visible in the instance. The task or
12157 -- protected type itself does not carry primitive operations.
12159 if Is_Concurrent_Type (Act_T)
12160 and then Is_Tagged_Type (Act_T)
12161 and then Present (Corresponding_Record_Type (Act_T))
12162 and then Present (Ancestor)
12163 and then Is_Interface (Ancestor)
12166 Corr_Rec : constant Entity_Id :=
12167 Corresponding_Record_Type (Act_T);
12168 New_Corr : Entity_Id;
12169 Corr_Decl : Node_Id;
12172 New_Corr := Make_Temporary (Loc, 'S');
12174 Make_Subtype_Declaration (Loc,
12175 Defining_Identifier => New_Corr,
12176 Subtype_Indication =>
12177 New_Occurrence_Of (Corr_Rec, Loc));
12178 Append_To (Decl_Nodes, Corr_Decl);
12180 if Ekind (Act_T) = E_Task_Type then
12181 Set_Ekind (Subt, E_Task_Subtype);
12183 Set_Ekind (Subt, E_Protected_Subtype);
12186 Set_Corresponding_Record_Type (Subt, Corr_Rec);
12187 Set_Generic_Parent_Type (Corr_Decl, Ancestor);
12188 Set_Generic_Parent_Type (Decl_Node, Empty);
12193 end Instantiate_Type;
12195 ---------------------
12196 -- Is_In_Main_Unit --
12197 ---------------------
12199 function Is_In_Main_Unit (N : Node_Id) return Boolean is
12200 Unum : constant Unit_Number_Type := Get_Source_Unit (N);
12201 Current_Unit : Node_Id;
12204 if Unum = Main_Unit then
12207 -- If the current unit is a subunit then it is either the main unit or
12208 -- is being compiled as part of the main unit.
12210 elsif Nkind (N) = N_Compilation_Unit then
12211 return Nkind (Unit (N)) = N_Subunit;
12214 Current_Unit := Parent (N);
12215 while Present (Current_Unit)
12216 and then Nkind (Current_Unit) /= N_Compilation_Unit
12218 Current_Unit := Parent (Current_Unit);
12221 -- The instantiation node is in the main unit, or else the current node
12222 -- (perhaps as the result of nested instantiations) is in the main unit,
12223 -- or in the declaration of the main unit, which in this last case must
12226 return Unum = Main_Unit
12227 or else Current_Unit = Cunit (Main_Unit)
12228 or else Current_Unit = Library_Unit (Cunit (Main_Unit))
12229 or else (Present (Library_Unit (Current_Unit))
12230 and then Is_In_Main_Unit (Library_Unit (Current_Unit)));
12231 end Is_In_Main_Unit;
12233 ----------------------------
12234 -- Load_Parent_Of_Generic --
12235 ----------------------------
12237 procedure Load_Parent_Of_Generic
12240 Body_Optional : Boolean := False)
12242 Comp_Unit : constant Node_Id := Cunit (Get_Source_Unit (Spec));
12243 Saved_Style_Check : constant Boolean := Style_Check;
12244 Saved_Warnings : constant Warning_Record := Save_Warnings;
12245 True_Parent : Node_Id;
12246 Inst_Node : Node_Id;
12248 Previous_Instances : constant Elist_Id := New_Elmt_List;
12250 procedure Collect_Previous_Instances (Decls : List_Id);
12251 -- Collect all instantiations in the given list of declarations, that
12252 -- precede the generic that we need to load. If the bodies of these
12253 -- instantiations are available, we must analyze them, to ensure that
12254 -- the public symbols generated are the same when the unit is compiled
12255 -- to generate code, and when it is compiled in the context of a unit
12256 -- that needs a particular nested instance. This process is applied to
12257 -- both package and subprogram instances.
12259 --------------------------------
12260 -- Collect_Previous_Instances --
12261 --------------------------------
12263 procedure Collect_Previous_Instances (Decls : List_Id) is
12267 Decl := First (Decls);
12268 while Present (Decl) loop
12269 if Sloc (Decl) >= Sloc (Inst_Node) then
12272 -- If Decl is an instantiation, then record it as requiring
12273 -- instantiation of the corresponding body, except if it is an
12274 -- abbreviated instantiation generated internally for conformance
12275 -- checking purposes only for the case of a formal package
12276 -- declared without a box (see Instantiate_Formal_Package). Such
12277 -- an instantiation does not generate any code (the actual code
12278 -- comes from actual) and thus does not need to be analyzed here.
12279 -- If the instantiation appears with a generic package body it is
12280 -- not analyzed here either.
12282 elsif Nkind (Decl) = N_Package_Instantiation
12283 and then not Is_Internal (Defining_Entity (Decl))
12285 Append_Elmt (Decl, Previous_Instances);
12287 -- For a subprogram instantiation, omit instantiations intrinsic
12288 -- operations (Unchecked_Conversions, etc.) that have no bodies.
12290 elsif Nkind_In (Decl, N_Function_Instantiation,
12291 N_Procedure_Instantiation)
12292 and then not Is_Intrinsic_Subprogram (Entity (Name (Decl)))
12294 Append_Elmt (Decl, Previous_Instances);
12296 elsif Nkind (Decl) = N_Package_Declaration then
12297 Collect_Previous_Instances
12298 (Visible_Declarations (Specification (Decl)));
12299 Collect_Previous_Instances
12300 (Private_Declarations (Specification (Decl)));
12302 -- Previous non-generic bodies may contain instances as well
12304 elsif Nkind (Decl) = N_Package_Body
12305 and then Ekind (Corresponding_Spec (Decl)) /= E_Generic_Package
12307 Collect_Previous_Instances (Declarations (Decl));
12309 elsif Nkind (Decl) = N_Subprogram_Body
12310 and then not Acts_As_Spec (Decl)
12311 and then not Is_Generic_Subprogram (Corresponding_Spec (Decl))
12313 Collect_Previous_Instances (Declarations (Decl));
12318 end Collect_Previous_Instances;
12320 -- Start of processing for Load_Parent_Of_Generic
12323 if not In_Same_Source_Unit (N, Spec)
12324 or else Nkind (Unit (Comp_Unit)) = N_Package_Declaration
12325 or else (Nkind (Unit (Comp_Unit)) = N_Package_Body
12326 and then not Is_In_Main_Unit (Spec))
12328 -- Find body of parent of spec, and analyze it. A special case arises
12329 -- when the parent is an instantiation, that is to say when we are
12330 -- currently instantiating a nested generic. In that case, there is
12331 -- no separate file for the body of the enclosing instance. Instead,
12332 -- the enclosing body must be instantiated as if it were a pending
12333 -- instantiation, in order to produce the body for the nested generic
12334 -- we require now. Note that in that case the generic may be defined
12335 -- in a package body, the instance defined in the same package body,
12336 -- and the original enclosing body may not be in the main unit.
12338 Inst_Node := Empty;
12340 True_Parent := Parent (Spec);
12341 while Present (True_Parent)
12342 and then Nkind (True_Parent) /= N_Compilation_Unit
12344 if Nkind (True_Parent) = N_Package_Declaration
12346 Nkind (Original_Node (True_Parent)) = N_Package_Instantiation
12348 -- Parent is a compilation unit that is an instantiation.
12349 -- Instantiation node has been replaced with package decl.
12351 Inst_Node := Original_Node (True_Parent);
12354 elsif Nkind (True_Parent) = N_Package_Declaration
12355 and then Present (Generic_Parent (Specification (True_Parent)))
12356 and then Nkind (Parent (True_Parent)) /= N_Compilation_Unit
12358 -- Parent is an instantiation within another specification.
12359 -- Declaration for instance has been inserted before original
12360 -- instantiation node. A direct link would be preferable?
12362 Inst_Node := Next (True_Parent);
12363 while Present (Inst_Node)
12364 and then Nkind (Inst_Node) /= N_Package_Instantiation
12369 -- If the instance appears within a generic, and the generic
12370 -- unit is defined within a formal package of the enclosing
12371 -- generic, there is no generic body available, and none
12372 -- needed. A more precise test should be used ???
12374 if No (Inst_Node) then
12381 True_Parent := Parent (True_Parent);
12385 -- Case where we are currently instantiating a nested generic
12387 if Present (Inst_Node) then
12388 if Nkind (Parent (True_Parent)) = N_Compilation_Unit then
12390 -- Instantiation node and declaration of instantiated package
12391 -- were exchanged when only the declaration was needed.
12392 -- Restore instantiation node before proceeding with body.
12394 Set_Unit (Parent (True_Parent), Inst_Node);
12397 -- Now complete instantiation of enclosing body, if it appears in
12398 -- some other unit. If it appears in the current unit, the body
12399 -- will have been instantiated already.
12401 if No (Corresponding_Body (Instance_Spec (Inst_Node))) then
12403 -- We need to determine the expander mode to instantiate the
12404 -- enclosing body. Because the generic body we need may use
12405 -- global entities declared in the enclosing package (including
12406 -- aggregates) it is in general necessary to compile this body
12407 -- with expansion enabled, except if we are within a generic
12408 -- package, in which case the usual generic rule applies.
12411 Exp_Status : Boolean := True;
12415 -- Loop through scopes looking for generic package
12417 Scop := Scope (Defining_Entity (Instance_Spec (Inst_Node)));
12418 while Present (Scop)
12419 and then Scop /= Standard_Standard
12421 if Ekind (Scop) = E_Generic_Package then
12422 Exp_Status := False;
12426 Scop := Scope (Scop);
12429 -- Collect previous instantiations in the unit that contains
12430 -- the desired generic.
12432 if Nkind (Parent (True_Parent)) /= N_Compilation_Unit
12433 and then not Body_Optional
12437 Info : Pending_Body_Info;
12441 Par := Parent (Inst_Node);
12442 while Present (Par) loop
12443 exit when Nkind (Parent (Par)) = N_Compilation_Unit;
12444 Par := Parent (Par);
12447 pragma Assert (Present (Par));
12449 if Nkind (Par) = N_Package_Body then
12450 Collect_Previous_Instances (Declarations (Par));
12452 elsif Nkind (Par) = N_Package_Declaration then
12453 Collect_Previous_Instances
12454 (Visible_Declarations (Specification (Par)));
12455 Collect_Previous_Instances
12456 (Private_Declarations (Specification (Par)));
12459 -- Enclosing unit is a subprogram body. In this
12460 -- case all instance bodies are processed in order
12461 -- and there is no need to collect them separately.
12466 Decl := First_Elmt (Previous_Instances);
12467 while Present (Decl) loop
12469 (Inst_Node => Node (Decl),
12471 Instance_Spec (Node (Decl)),
12472 Expander_Status => Exp_Status,
12473 Current_Sem_Unit =>
12474 Get_Code_Unit (Sloc (Node (Decl))),
12475 Scope_Suppress => Scope_Suppress,
12476 Local_Suppress_Stack_Top =>
12477 Local_Suppress_Stack_Top,
12478 Version => Ada_Version,
12479 Version_Pragma => Ada_Version_Pragma,
12480 Warnings => Save_Warnings,
12481 SPARK_Mode => SPARK_Mode,
12482 SPARK_Mode_Pragma => SPARK_Mode_Pragma);
12484 -- Package instance
12487 Nkind (Node (Decl)) = N_Package_Instantiation
12489 Instantiate_Package_Body
12490 (Info, Body_Optional => True);
12492 -- Subprogram instance
12495 -- The instance_spec is the wrapper package,
12496 -- and the subprogram declaration is the last
12497 -- declaration in the wrapper.
12501 (Visible_Declarations
12502 (Specification (Info.Act_Decl)));
12504 Instantiate_Subprogram_Body
12505 (Info, Body_Optional => True);
12513 Instantiate_Package_Body
12515 ((Inst_Node => Inst_Node,
12516 Act_Decl => True_Parent,
12517 Expander_Status => Exp_Status,
12518 Current_Sem_Unit => Get_Code_Unit
12519 (Sloc (Inst_Node)),
12520 Scope_Suppress => Scope_Suppress,
12521 Local_Suppress_Stack_Top => Local_Suppress_Stack_Top,
12522 Version => Ada_Version,
12523 Version_Pragma => Ada_Version_Pragma,
12524 Warnings => Save_Warnings,
12525 SPARK_Mode => SPARK_Mode,
12526 SPARK_Mode_Pragma => SPARK_Mode_Pragma)),
12527 Body_Optional => Body_Optional);
12531 -- Case where we are not instantiating a nested generic
12534 Opt.Style_Check := False;
12535 Expander_Mode_Save_And_Set (True);
12536 Load_Needed_Body (Comp_Unit, OK);
12537 Opt.Style_Check := Saved_Style_Check;
12538 Restore_Warnings (Saved_Warnings);
12539 Expander_Mode_Restore;
12542 and then Unit_Requires_Body (Defining_Entity (Spec))
12543 and then not Body_Optional
12546 Bname : constant Unit_Name_Type :=
12547 Get_Body_Name (Get_Unit_Name (Unit (Comp_Unit)));
12550 -- In CodePeer mode, the missing body may make the analysis
12551 -- incomplete, but we do not treat it as fatal.
12553 if CodePeer_Mode then
12557 Error_Msg_Unit_1 := Bname;
12558 Error_Msg_N ("this instantiation requires$!", N);
12559 Error_Msg_File_1 :=
12560 Get_File_Name (Bname, Subunit => False);
12561 Error_Msg_N ("\but file{ was not found!", N);
12562 raise Unrecoverable_Error;
12569 -- If loading parent of the generic caused an instantiation circularity,
12570 -- we abandon compilation at this point, because otherwise in some cases
12571 -- we get into trouble with infinite recursions after this point.
12573 if Circularity_Detected then
12574 raise Unrecoverable_Error;
12576 end Load_Parent_Of_Generic;
12578 ---------------------------------
12579 -- Map_Formal_Package_Entities --
12580 ---------------------------------
12582 procedure Map_Formal_Package_Entities (Form : Entity_Id; Act : Entity_Id) is
12587 Set_Instance_Of (Form, Act);
12589 -- Traverse formal and actual package to map the corresponding entities.
12590 -- We skip over internal entities that may be generated during semantic
12591 -- analysis, and find the matching entities by name, given that they
12592 -- must appear in the same order.
12594 E1 := First_Entity (Form);
12595 E2 := First_Entity (Act);
12596 while Present (E1) and then E1 /= First_Private_Entity (Form) loop
12597 -- Could this test be a single condition??? Seems like it could, and
12598 -- isn't FPE (Form) a constant anyway???
12600 if not Is_Internal (E1)
12601 and then Present (Parent (E1))
12602 and then not Is_Class_Wide_Type (E1)
12603 and then not Is_Internal_Name (Chars (E1))
12605 while Present (E2) and then Chars (E2) /= Chars (E1) loop
12612 Set_Instance_Of (E1, E2);
12614 if Is_Type (E1) and then Is_Tagged_Type (E2) then
12615 Set_Instance_Of (Class_Wide_Type (E1), Class_Wide_Type (E2));
12618 if Is_Constrained (E1) then
12619 Set_Instance_Of (Base_Type (E1), Base_Type (E2));
12622 if Ekind (E1) = E_Package and then No (Renamed_Object (E1)) then
12623 Map_Formal_Package_Entities (E1, E2);
12630 end Map_Formal_Package_Entities;
12632 -----------------------
12633 -- Move_Freeze_Nodes --
12634 -----------------------
12636 procedure Move_Freeze_Nodes
12637 (Out_Of : Entity_Id;
12642 Next_Decl : Node_Id;
12643 Next_Node : Node_Id := After;
12646 function Is_Outer_Type (T : Entity_Id) return Boolean;
12647 -- Check whether entity is declared in a scope external to that of the
12650 -------------------
12651 -- Is_Outer_Type --
12652 -------------------
12654 function Is_Outer_Type (T : Entity_Id) return Boolean is
12655 Scop : Entity_Id := Scope (T);
12658 if Scope_Depth (Scop) < Scope_Depth (Out_Of) then
12662 while Scop /= Standard_Standard loop
12663 if Scop = Out_Of then
12666 Scop := Scope (Scop);
12674 -- Start of processing for Move_Freeze_Nodes
12681 -- First remove the freeze nodes that may appear before all other
12685 while Present (Decl)
12686 and then Nkind (Decl) = N_Freeze_Entity
12687 and then Is_Outer_Type (Entity (Decl))
12689 Decl := Remove_Head (L);
12690 Insert_After (Next_Node, Decl);
12691 Set_Analyzed (Decl, False);
12696 -- Next scan the list of declarations and remove each freeze node that
12697 -- appears ahead of the current node.
12699 while Present (Decl) loop
12700 while Present (Next (Decl))
12701 and then Nkind (Next (Decl)) = N_Freeze_Entity
12702 and then Is_Outer_Type (Entity (Next (Decl)))
12704 Next_Decl := Remove_Next (Decl);
12705 Insert_After (Next_Node, Next_Decl);
12706 Set_Analyzed (Next_Decl, False);
12707 Next_Node := Next_Decl;
12710 -- If the declaration is a nested package or concurrent type, then
12711 -- recurse. Nested generic packages will have been processed from the
12714 case Nkind (Decl) is
12715 when N_Package_Declaration =>
12716 Spec := Specification (Decl);
12718 when N_Task_Type_Declaration =>
12719 Spec := Task_Definition (Decl);
12721 when N_Protected_Type_Declaration =>
12722 Spec := Protected_Definition (Decl);
12728 if Present (Spec) then
12729 Move_Freeze_Nodes (Out_Of, Next_Node, Visible_Declarations (Spec));
12730 Move_Freeze_Nodes (Out_Of, Next_Node, Private_Declarations (Spec));
12735 end Move_Freeze_Nodes;
12741 function Next_Assoc (E : Assoc_Ptr) return Assoc_Ptr is
12743 return Generic_Renamings.Table (E).Next_In_HTable;
12746 ------------------------
12747 -- Preanalyze_Actuals --
12748 ------------------------
12750 procedure Preanalyze_Actuals (N : Node_Id) is
12753 Errs : constant Int := Serious_Errors_Detected;
12755 Cur : Entity_Id := Empty;
12756 -- Current homograph of the instance name
12759 -- Saved visibility status of the current homograph
12762 Assoc := First (Generic_Associations (N));
12764 -- If the instance is a child unit, its name may hide an outer homonym,
12765 -- so make it invisible to perform name resolution on the actuals.
12767 if Nkind (Defining_Unit_Name (N)) = N_Defining_Program_Unit_Name
12769 (Current_Entity (Defining_Identifier (Defining_Unit_Name (N))))
12771 Cur := Current_Entity (Defining_Identifier (Defining_Unit_Name (N)));
12773 if Is_Compilation_Unit (Cur) then
12774 Vis := Is_Immediately_Visible (Cur);
12775 Set_Is_Immediately_Visible (Cur, False);
12781 while Present (Assoc) loop
12782 if Nkind (Assoc) /= N_Others_Choice then
12783 Act := Explicit_Generic_Actual_Parameter (Assoc);
12785 -- Within a nested instantiation, a defaulted actual is an empty
12786 -- association, so nothing to analyze. If the subprogram actual
12787 -- is an attribute, analyze prefix only, because actual is not a
12788 -- complete attribute reference.
12790 -- If actual is an allocator, analyze expression only. The full
12791 -- analysis can generate code, and if instance is a compilation
12792 -- unit we have to wait until the package instance is installed
12793 -- to have a proper place to insert this code.
12795 -- String literals may be operators, but at this point we do not
12796 -- know whether the actual is a formal subprogram or a string.
12801 elsif Nkind (Act) = N_Attribute_Reference then
12802 Analyze (Prefix (Act));
12804 elsif Nkind (Act) = N_Explicit_Dereference then
12805 Analyze (Prefix (Act));
12807 elsif Nkind (Act) = N_Allocator then
12809 Expr : constant Node_Id := Expression (Act);
12812 if Nkind (Expr) = N_Subtype_Indication then
12813 Analyze (Subtype_Mark (Expr));
12815 -- Analyze separately each discriminant constraint, when
12816 -- given with a named association.
12822 Constr := First (Constraints (Constraint (Expr)));
12823 while Present (Constr) loop
12824 if Nkind (Constr) = N_Discriminant_Association then
12825 Analyze (Expression (Constr));
12839 elsif Nkind (Act) /= N_Operator_Symbol then
12843 -- Ensure that a ghost subprogram does not act as generic actual
12845 if Is_Entity_Name (Act)
12846 and then Is_Ghost_Subprogram (Entity (Act))
12849 ("ghost subprogram & cannot act as generic actual", Act);
12850 Abandon_Instantiation (Act);
12852 elsif Errs /= Serious_Errors_Detected then
12854 -- Do a minimal analysis of the generic, to prevent spurious
12855 -- warnings complaining about the generic being unreferenced,
12856 -- before abandoning the instantiation.
12858 Analyze (Name (N));
12860 if Is_Entity_Name (Name (N))
12861 and then Etype (Name (N)) /= Any_Type
12863 Generate_Reference (Entity (Name (N)), Name (N));
12864 Set_Is_Instantiated (Entity (Name (N)));
12867 if Present (Cur) then
12869 -- For the case of a child instance hiding an outer homonym,
12870 -- provide additional warning which might explain the error.
12872 Set_Is_Immediately_Visible (Cur, Vis);
12873 Error_Msg_NE ("& hides outer unit with the same name??",
12874 N, Defining_Unit_Name (N));
12877 Abandon_Instantiation (Act);
12884 if Present (Cur) then
12885 Set_Is_Immediately_Visible (Cur, Vis);
12887 end Preanalyze_Actuals;
12889 -------------------
12890 -- Remove_Parent --
12891 -------------------
12893 procedure Remove_Parent (In_Body : Boolean := False) is
12894 S : Entity_Id := Current_Scope;
12895 -- S is the scope containing the instantiation just completed. The scope
12896 -- stack contains the parent instances of the instantiation, followed by
12905 -- After child instantiation is complete, remove from scope stack the
12906 -- extra copy of the current scope, and then remove parent instances.
12908 if not In_Body then
12911 while Current_Scope /= S loop
12912 P := Current_Scope;
12913 End_Package_Scope (Current_Scope);
12915 if In_Open_Scopes (P) then
12916 E := First_Entity (P);
12917 while Present (E) loop
12918 Set_Is_Immediately_Visible (E, True);
12922 -- If instantiation is declared in a block, it is the enclosing
12923 -- scope that might be a parent instance. Note that only one
12924 -- block can be involved, because the parent instances have
12925 -- been installed within it.
12927 if Ekind (P) = E_Block then
12928 Cur_P := Scope (P);
12933 if Is_Generic_Instance (Cur_P) and then P /= Current_Scope then
12934 -- We are within an instance of some sibling. Retain
12935 -- visibility of parent, for proper subsequent cleanup, and
12936 -- reinstall private declarations as well.
12938 Set_In_Private_Part (P);
12939 Install_Private_Declarations (P);
12942 -- If the ultimate parent is a top-level unit recorded in
12943 -- Instance_Parent_Unit, then reset its visibility to what it was
12944 -- before instantiation. (It's not clear what the purpose is of
12945 -- testing whether Scope (P) is In_Open_Scopes, but that test was
12946 -- present before the ultimate parent test was added.???)
12948 elsif not In_Open_Scopes (Scope (P))
12949 or else (P = Instance_Parent_Unit
12950 and then not Parent_Unit_Visible)
12952 Set_Is_Immediately_Visible (P, False);
12954 -- If the current scope is itself an instantiation of a generic
12955 -- nested within P, and we are in the private part of body of this
12956 -- instantiation, restore the full views of P, that were removed
12957 -- in End_Package_Scope above. This obscure case can occur when a
12958 -- subunit of a generic contains an instance of a child unit of
12959 -- its generic parent unit.
12961 elsif S = Current_Scope and then Is_Generic_Instance (S) then
12963 Par : constant Entity_Id :=
12964 Generic_Parent (Package_Specification (S));
12967 and then P = Scope (Par)
12968 and then (In_Package_Body (S) or else In_Private_Part (S))
12970 Set_In_Private_Part (P);
12971 Install_Private_Declarations (P);
12977 -- Reset visibility of entities in the enclosing scope
12979 Set_Is_Hidden_Open_Scope (Current_Scope, False);
12981 Hidden := First_Elmt (Hidden_Entities);
12982 while Present (Hidden) loop
12983 Set_Is_Immediately_Visible (Node (Hidden), True);
12984 Next_Elmt (Hidden);
12988 -- Each body is analyzed separately, and there is no context that
12989 -- needs preserving from one body instance to the next, so remove all
12990 -- parent scopes that have been installed.
12992 while Present (S) loop
12993 End_Package_Scope (S);
12994 Set_Is_Immediately_Visible (S, False);
12995 S := Current_Scope;
12996 exit when S = Standard_Standard;
13005 procedure Restore_Env is
13006 Saved : Instance_Env renames Instance_Envs.Table (Instance_Envs.Last);
13009 if No (Current_Instantiated_Parent.Act_Id) then
13010 -- Restore environment after subprogram inlining
13012 Restore_Private_Views (Empty);
13015 Current_Instantiated_Parent := Saved.Instantiated_Parent;
13016 Exchanged_Views := Saved.Exchanged_Views;
13017 Hidden_Entities := Saved.Hidden_Entities;
13018 Current_Sem_Unit := Saved.Current_Sem_Unit;
13019 Parent_Unit_Visible := Saved.Parent_Unit_Visible;
13020 Instance_Parent_Unit := Saved.Instance_Parent_Unit;
13022 Restore_Opt_Config_Switches (Saved.Switches);
13024 Instance_Envs.Decrement_Last;
13027 ---------------------------
13028 -- Restore_Private_Views --
13029 ---------------------------
13031 procedure Restore_Private_Views
13032 (Pack_Id : Entity_Id;
13033 Is_Package : Boolean := True)
13038 Dep_Elmt : Elmt_Id;
13041 procedure Restore_Nested_Formal (Formal : Entity_Id);
13042 -- Hide the generic formals of formal packages declared with box which
13043 -- were reachable in the current instantiation.
13045 ---------------------------
13046 -- Restore_Nested_Formal --
13047 ---------------------------
13049 procedure Restore_Nested_Formal (Formal : Entity_Id) is
13053 if Present (Renamed_Object (Formal))
13054 and then Denotes_Formal_Package (Renamed_Object (Formal), True)
13058 elsif Present (Associated_Formal_Package (Formal)) then
13059 Ent := First_Entity (Formal);
13060 while Present (Ent) loop
13061 exit when Ekind (Ent) = E_Package
13062 and then Renamed_Entity (Ent) = Renamed_Entity (Formal);
13064 Set_Is_Hidden (Ent);
13065 Set_Is_Potentially_Use_Visible (Ent, False);
13067 -- If package, then recurse
13069 if Ekind (Ent) = E_Package then
13070 Restore_Nested_Formal (Ent);
13076 end Restore_Nested_Formal;
13078 -- Start of processing for Restore_Private_Views
13081 M := First_Elmt (Exchanged_Views);
13082 while Present (M) loop
13085 -- Subtypes of types whose views have been exchanged, and that are
13086 -- defined within the instance, were not on the Private_Dependents
13087 -- list on entry to the instance, so they have to be exchanged
13088 -- explicitly now, in order to remain consistent with the view of the
13091 if Ekind_In (Typ, E_Private_Type,
13092 E_Limited_Private_Type,
13093 E_Record_Type_With_Private)
13095 Dep_Elmt := First_Elmt (Private_Dependents (Typ));
13096 while Present (Dep_Elmt) loop
13097 Dep_Typ := Node (Dep_Elmt);
13099 if Scope (Dep_Typ) = Pack_Id
13100 and then Present (Full_View (Dep_Typ))
13102 Replace_Elmt (Dep_Elmt, Full_View (Dep_Typ));
13103 Exchange_Declarations (Dep_Typ);
13106 Next_Elmt (Dep_Elmt);
13110 Exchange_Declarations (Node (M));
13114 if No (Pack_Id) then
13118 -- Make the generic formal parameters private, and make the formal types
13119 -- into subtypes of the actuals again.
13121 E := First_Entity (Pack_Id);
13122 while Present (E) loop
13123 Set_Is_Hidden (E, True);
13126 and then Nkind (Parent (E)) = N_Subtype_Declaration
13128 -- If the actual for E is itself a generic actual type from
13129 -- an enclosing instance, E is still a generic actual type
13130 -- outside of the current instance. This matter when resolving
13131 -- an overloaded call that may be ambiguous in the enclosing
13132 -- instance, when two of its actuals coincide.
13134 if Is_Entity_Name (Subtype_Indication (Parent (E)))
13135 and then Is_Generic_Actual_Type
13136 (Entity (Subtype_Indication (Parent (E))))
13140 Set_Is_Generic_Actual_Type (E, False);
13143 -- An unusual case of aliasing: the actual may also be directly
13144 -- visible in the generic, and be private there, while it is fully
13145 -- visible in the context of the instance. The internal subtype
13146 -- is private in the instance but has full visibility like its
13147 -- parent in the enclosing scope. This enforces the invariant that
13148 -- the privacy status of all private dependents of a type coincide
13149 -- with that of the parent type. This can only happen when a
13150 -- generic child unit is instantiated within a sibling.
13152 if Is_Private_Type (E)
13153 and then not Is_Private_Type (Etype (E))
13155 Exchange_Declarations (E);
13158 elsif Ekind (E) = E_Package then
13160 -- The end of the renaming list is the renaming of the generic
13161 -- package itself. If the instance is a subprogram, all entities
13162 -- in the corresponding package are renamings. If this entity is
13163 -- a formal package, make its own formals private as well. The
13164 -- actual in this case is itself the renaming of an instantiation.
13165 -- If the entity is not a package renaming, it is the entity
13166 -- created to validate formal package actuals: ignore it.
13168 -- If the actual is itself a formal package for the enclosing
13169 -- generic, or the actual for such a formal package, it remains
13170 -- visible on exit from the instance, and therefore nothing needs
13171 -- to be done either, except to keep it accessible.
13173 if Is_Package and then Renamed_Object (E) = Pack_Id then
13176 elsif Nkind (Parent (E)) /= N_Package_Renaming_Declaration then
13180 Denotes_Formal_Package (Renamed_Object (E), True, Pack_Id)
13182 Set_Is_Hidden (E, False);
13186 Act_P : constant Entity_Id := Renamed_Object (E);
13190 Id := First_Entity (Act_P);
13192 and then Id /= First_Private_Entity (Act_P)
13194 exit when Ekind (Id) = E_Package
13195 and then Renamed_Object (Id) = Act_P;
13197 Set_Is_Hidden (Id, True);
13198 Set_Is_Potentially_Use_Visible (Id, In_Use (Act_P));
13200 if Ekind (Id) = E_Package then
13201 Restore_Nested_Formal (Id);
13212 end Restore_Private_Views;
13219 (Gen_Unit : Entity_Id;
13220 Act_Unit : Entity_Id)
13224 Set_Instance_Env (Gen_Unit, Act_Unit);
13227 ----------------------------
13228 -- Save_Global_References --
13229 ----------------------------
13231 procedure Save_Global_References (N : Node_Id) is
13232 Gen_Scope : Entity_Id;
13236 function Is_Global (E : Entity_Id) return Boolean;
13237 -- Check whether entity is defined outside of generic unit. Examine the
13238 -- scope of an entity, and the scope of the scope, etc, until we find
13239 -- either Standard, in which case the entity is global, or the generic
13240 -- unit itself, which indicates that the entity is local. If the entity
13241 -- is the generic unit itself, as in the case of a recursive call, or
13242 -- the enclosing generic unit, if different from the current scope, then
13243 -- it is local as well, because it will be replaced at the point of
13244 -- instantiation. On the other hand, if it is a reference to a child
13245 -- unit of a common ancestor, which appears in an instantiation, it is
13246 -- global because it is used to denote a specific compilation unit at
13247 -- the time the instantiations will be analyzed.
13249 procedure Reset_Entity (N : Node_Id);
13250 -- Save semantic information on global entity so that it is not resolved
13251 -- again at instantiation time.
13253 procedure Save_Entity_Descendants (N : Node_Id);
13254 -- Apply Save_Global_References to the two syntactic descendants of
13255 -- non-terminal nodes that carry an Associated_Node and are processed
13256 -- through Reset_Entity. Once the global entity (if any) has been
13257 -- captured together with its type, only two syntactic descendants need
13258 -- to be traversed to complete the processing of the tree rooted at N.
13259 -- This applies to Selected_Components, Expanded_Names, and to Operator
13260 -- nodes. N can also be a character literal, identifier, or operator
13261 -- symbol node, but the call has no effect in these cases.
13263 procedure Save_Global_Defaults (N1, N2 : Node_Id);
13264 -- Default actuals in nested instances must be handled specially
13265 -- because there is no link to them from the original tree. When an
13266 -- actual subprogram is given by a default, we add an explicit generic
13267 -- association for it in the instantiation node. When we save the
13268 -- global references on the name of the instance, we recover the list
13269 -- of generic associations, and add an explicit one to the original
13270 -- generic tree, through which a global actual can be preserved.
13271 -- Similarly, if a child unit is instantiated within a sibling, in the
13272 -- context of the parent, we must preserve the identifier of the parent
13273 -- so that it can be properly resolved in a subsequent instantiation.
13275 procedure Save_Global_Descendant (D : Union_Id);
13276 -- Apply Save_Global_References recursively to the descendents of the
13279 procedure Save_References (N : Node_Id);
13280 -- This is the recursive procedure that does the work, once the
13281 -- enclosing generic scope has been established.
13287 function Is_Global (E : Entity_Id) return Boolean is
13290 function Is_Instance_Node (Decl : Node_Id) return Boolean;
13291 -- Determine whether the parent node of a reference to a child unit
13292 -- denotes an instantiation or a formal package, in which case the
13293 -- reference to the child unit is global, even if it appears within
13294 -- the current scope (e.g. when the instance appears within the body
13295 -- of an ancestor).
13297 ----------------------
13298 -- Is_Instance_Node --
13299 ----------------------
13301 function Is_Instance_Node (Decl : Node_Id) return Boolean is
13303 return Nkind (Decl) in N_Generic_Instantiation
13305 Nkind (Original_Node (Decl)) = N_Formal_Package_Declaration;
13306 end Is_Instance_Node;
13308 -- Start of processing for Is_Global
13311 if E = Gen_Scope then
13314 elsif E = Standard_Standard then
13317 elsif Is_Child_Unit (E)
13318 and then (Is_Instance_Node (Parent (N2))
13319 or else (Nkind (Parent (N2)) = N_Expanded_Name
13320 and then N2 = Selector_Name (Parent (N2))
13322 Is_Instance_Node (Parent (Parent (N2)))))
13328 while Se /= Gen_Scope loop
13329 if Se = Standard_Standard then
13344 procedure Reset_Entity (N : Node_Id) is
13346 procedure Set_Global_Type (N : Node_Id; N2 : Node_Id);
13347 -- If the type of N2 is global to the generic unit, save the type in
13348 -- the generic node. Just as we perform name capture for explicit
13349 -- references within the generic, we must capture the global types
13350 -- of local entities because they may participate in resolution in
13353 function Top_Ancestor (E : Entity_Id) return Entity_Id;
13354 -- Find the ultimate ancestor of the current unit. If it is not a
13355 -- generic unit, then the name of the current unit in the prefix of
13356 -- an expanded name must be replaced with its generic homonym to
13357 -- ensure that it will be properly resolved in an instance.
13359 ---------------------
13360 -- Set_Global_Type --
13361 ---------------------
13363 procedure Set_Global_Type (N : Node_Id; N2 : Node_Id) is
13364 Typ : constant Entity_Id := Etype (N2);
13367 Set_Etype (N, Typ);
13369 if Entity (N) /= N2
13370 and then Has_Private_View (Entity (N))
13372 -- If the entity of N is not the associated node, this is a
13373 -- nested generic and it has an associated node as well, whose
13374 -- type is already the full view (see below). Indicate that the
13375 -- original node has a private view.
13377 Set_Has_Private_View (N);
13380 -- If not a private type, nothing else to do
13382 if not Is_Private_Type (Typ) then
13383 if Is_Array_Type (Typ)
13384 and then Is_Private_Type (Component_Type (Typ))
13386 Set_Has_Private_View (N);
13389 -- If it is a derivation of a private type in a context where no
13390 -- full view is needed, nothing to do either.
13392 elsif No (Full_View (Typ)) and then Typ /= Etype (Typ) then
13395 -- Otherwise mark the type for flipping and use the full view when
13399 Set_Has_Private_View (N);
13401 if Present (Full_View (Typ)) then
13402 Set_Etype (N2, Full_View (Typ));
13405 end Set_Global_Type;
13411 function Top_Ancestor (E : Entity_Id) return Entity_Id is
13416 while Is_Child_Unit (Par) loop
13417 Par := Scope (Par);
13423 -- Start of processing for Reset_Entity
13426 N2 := Get_Associated_Node (N);
13429 if Present (E) then
13431 -- If the node is an entry call to an entry in an enclosing task,
13432 -- it is rewritten as a selected component. No global entity to
13433 -- preserve in this case, since the expansion will be redone in
13436 if not Nkind_In (E, N_Defining_Identifier,
13437 N_Defining_Character_Literal,
13438 N_Defining_Operator_Symbol)
13440 Set_Associated_Node (N, Empty);
13441 Set_Etype (N, Empty);
13445 -- If the entity is an itype created as a subtype of an access
13446 -- type with a null exclusion restore source entity for proper
13447 -- visibility. The itype will be created anew in the instance.
13450 and then Ekind (E) = E_Access_Subtype
13451 and then Is_Entity_Name (N)
13452 and then Chars (Etype (E)) = Chars (N)
13455 Set_Entity (N2, E);
13459 if Is_Global (E) then
13461 -- If the entity is a package renaming that is the prefix of
13462 -- an expanded name, it has been rewritten as the renamed
13463 -- package, which is necessary semantically but complicates
13464 -- ASIS tree traversal, so we recover the original entity to
13465 -- expose the renaming. Take into account that the context may
13466 -- be a nested generic, that the original node may itself have
13467 -- an associated node that had better be an entity, and that
13468 -- the current node is still a selected component.
13470 if Ekind (E) = E_Package
13471 and then Nkind (N) = N_Selected_Component
13472 and then Nkind (Parent (N)) = N_Expanded_Name
13473 and then Present (Original_Node (N2))
13474 and then Is_Entity_Name (Original_Node (N2))
13475 and then Present (Entity (Original_Node (N2)))
13477 if Is_Global (Entity (Original_Node (N2))) then
13478 N2 := Original_Node (N2);
13479 Set_Associated_Node (N, N2);
13480 Set_Global_Type (N, N2);
13483 -- Renaming is local, and will be resolved in instance
13485 Set_Associated_Node (N, Empty);
13486 Set_Etype (N, Empty);
13490 Set_Global_Type (N, N2);
13493 elsif Nkind (N) = N_Op_Concat
13494 and then Is_Generic_Type (Etype (N2))
13495 and then (Base_Type (Etype (Right_Opnd (N2))) = Etype (N2)
13497 Base_Type (Etype (Left_Opnd (N2))) = Etype (N2))
13498 and then Is_Intrinsic_Subprogram (E)
13503 -- Entity is local. Mark generic node as unresolved.
13504 -- Note that now it does not have an entity.
13506 Set_Associated_Node (N, Empty);
13507 Set_Etype (N, Empty);
13510 if Nkind (Parent (N)) in N_Generic_Instantiation
13511 and then N = Name (Parent (N))
13513 Save_Global_Defaults (Parent (N), Parent (N2));
13516 elsif Nkind (Parent (N)) = N_Selected_Component
13517 and then Nkind (Parent (N2)) = N_Expanded_Name
13519 if Is_Global (Entity (Parent (N2))) then
13520 Change_Selected_Component_To_Expanded_Name (Parent (N));
13521 Set_Associated_Node (Parent (N), Parent (N2));
13522 Set_Global_Type (Parent (N), Parent (N2));
13523 Save_Entity_Descendants (N);
13525 -- If this is a reference to the current generic entity, replace
13526 -- by the name of the generic homonym of the current package. This
13527 -- is because in an instantiation Par.P.Q will not resolve to the
13528 -- name of the instance, whose enclosing scope is not necessarily
13529 -- Par. We use the generic homonym rather that the name of the
13530 -- generic itself because it may be hidden by a local declaration.
13532 elsif In_Open_Scopes (Entity (Parent (N2)))
13534 Is_Generic_Unit (Top_Ancestor (Entity (Prefix (Parent (N2)))))
13536 if Ekind (Entity (Parent (N2))) = E_Generic_Package then
13537 Rewrite (Parent (N),
13538 Make_Identifier (Sloc (N),
13540 Chars (Generic_Homonym (Entity (Parent (N2))))));
13542 Rewrite (Parent (N),
13543 Make_Identifier (Sloc (N),
13544 Chars => Chars (Selector_Name (Parent (N2)))));
13548 if Nkind (Parent (Parent (N))) in N_Generic_Instantiation
13549 and then Parent (N) = Name (Parent (Parent (N)))
13551 Save_Global_Defaults
13552 (Parent (Parent (N)), Parent (Parent ((N2))));
13555 -- A selected component may denote a static constant that has been
13556 -- folded. If the static constant is global to the generic, capture
13557 -- its value. Otherwise the folding will happen in any instantiation.
13559 elsif Nkind (Parent (N)) = N_Selected_Component
13560 and then Nkind_In (Parent (N2), N_Integer_Literal, N_Real_Literal)
13562 if Present (Entity (Original_Node (Parent (N2))))
13563 and then Is_Global (Entity (Original_Node (Parent (N2))))
13565 Rewrite (Parent (N), New_Copy (Parent (N2)));
13566 Set_Analyzed (Parent (N), False);
13572 -- A selected component may be transformed into a parameterless
13573 -- function call. If the called entity is global, rewrite the node
13574 -- appropriately, i.e. as an extended name for the global entity.
13576 elsif Nkind (Parent (N)) = N_Selected_Component
13577 and then Nkind (Parent (N2)) = N_Function_Call
13578 and then N = Selector_Name (Parent (N))
13580 if No (Parameter_Associations (Parent (N2))) then
13581 if Is_Global (Entity (Name (Parent (N2)))) then
13582 Change_Selected_Component_To_Expanded_Name (Parent (N));
13583 Set_Associated_Node (Parent (N), Name (Parent (N2)));
13584 Set_Global_Type (Parent (N), Name (Parent (N2)));
13585 Save_Entity_Descendants (N);
13588 Set_Is_Prefixed_Call (Parent (N));
13589 Set_Associated_Node (N, Empty);
13590 Set_Etype (N, Empty);
13593 -- In Ada 2005, X.F may be a call to a primitive operation,
13594 -- rewritten as F (X). This rewriting will be done again in an
13595 -- instance, so keep the original node. Global entities will be
13596 -- captured as for other constructs. Indicate that this must
13597 -- resolve as a call, to prevent accidental overloading in the
13598 -- instance, if both a component and a primitive operation appear
13602 Set_Is_Prefixed_Call (Parent (N));
13605 -- Entity is local. Reset in generic unit, so that node is resolved
13606 -- anew at the point of instantiation.
13609 Set_Associated_Node (N, Empty);
13610 Set_Etype (N, Empty);
13614 -----------------------------
13615 -- Save_Entity_Descendants --
13616 -----------------------------
13618 procedure Save_Entity_Descendants (N : Node_Id) is
13621 when N_Binary_Op =>
13622 Save_Global_Descendant (Union_Id (Left_Opnd (N)));
13623 Save_Global_Descendant (Union_Id (Right_Opnd (N)));
13626 Save_Global_Descendant (Union_Id (Right_Opnd (N)));
13628 when N_Expanded_Name | N_Selected_Component =>
13629 Save_Global_Descendant (Union_Id (Prefix (N)));
13630 Save_Global_Descendant (Union_Id (Selector_Name (N)));
13632 when N_Identifier | N_Character_Literal | N_Operator_Symbol =>
13636 raise Program_Error;
13638 end Save_Entity_Descendants;
13640 --------------------------
13641 -- Save_Global_Defaults --
13642 --------------------------
13644 procedure Save_Global_Defaults (N1, N2 : Node_Id) is
13645 Loc : constant Source_Ptr := Sloc (N1);
13646 Assoc2 : constant List_Id := Generic_Associations (N2);
13647 Gen_Id : constant Entity_Id := Get_Generic_Entity (N2);
13654 Actual : Entity_Id;
13657 Assoc1 := Generic_Associations (N1);
13659 if Present (Assoc1) then
13660 Act1 := First (Assoc1);
13663 Set_Generic_Associations (N1, New_List);
13664 Assoc1 := Generic_Associations (N1);
13667 if Present (Assoc2) then
13668 Act2 := First (Assoc2);
13673 while Present (Act1) and then Present (Act2) loop
13678 -- Find the associations added for default subprograms
13680 if Present (Act2) then
13681 while Nkind (Act2) /= N_Generic_Association
13682 or else No (Entity (Selector_Name (Act2)))
13683 or else not Is_Overloadable (Entity (Selector_Name (Act2)))
13688 -- Add a similar association if the default is global. The
13689 -- renaming declaration for the actual has been analyzed, and
13690 -- its alias is the program it renames. Link the actual in the
13691 -- original generic tree with the node in the analyzed tree.
13693 while Present (Act2) loop
13694 Subp := Entity (Selector_Name (Act2));
13695 Def := Explicit_Generic_Actual_Parameter (Act2);
13697 -- Following test is defence against rubbish errors
13699 if No (Alias (Subp)) then
13703 -- Retrieve the resolved actual from the renaming declaration
13704 -- created for the instantiated formal.
13706 Actual := Entity (Name (Parent (Parent (Subp))));
13707 Set_Entity (Def, Actual);
13708 Set_Etype (Def, Etype (Actual));
13710 if Is_Global (Actual) then
13712 Make_Generic_Association (Loc,
13713 Selector_Name => New_Occurrence_Of (Subp, Loc),
13714 Explicit_Generic_Actual_Parameter =>
13715 New_Occurrence_Of (Actual, Loc));
13717 Set_Associated_Node
13718 (Explicit_Generic_Actual_Parameter (Ndec), Def);
13720 Append (Ndec, Assoc1);
13722 -- If there are other defaults, add a dummy association in case
13723 -- there are other defaulted formals with the same name.
13725 elsif Present (Next (Act2)) then
13727 Make_Generic_Association (Loc,
13728 Selector_Name => New_Occurrence_Of (Subp, Loc),
13729 Explicit_Generic_Actual_Parameter => Empty);
13731 Append (Ndec, Assoc1);
13738 if Nkind (Name (N1)) = N_Identifier
13739 and then Is_Child_Unit (Gen_Id)
13740 and then Is_Global (Gen_Id)
13741 and then Is_Generic_Unit (Scope (Gen_Id))
13742 and then In_Open_Scopes (Scope (Gen_Id))
13744 -- This is an instantiation of a child unit within a sibling, so
13745 -- that the generic parent is in scope. An eventual instance must
13746 -- occur within the scope of an instance of the parent. Make name
13747 -- in instance into an expanded name, to preserve the identifier
13748 -- of the parent, so it can be resolved subsequently.
13750 Rewrite (Name (N2),
13751 Make_Expanded_Name (Loc,
13752 Chars => Chars (Gen_Id),
13753 Prefix => New_Occurrence_Of (Scope (Gen_Id), Loc),
13754 Selector_Name => New_Occurrence_Of (Gen_Id, Loc)));
13755 Set_Entity (Name (N2), Gen_Id);
13757 Rewrite (Name (N1),
13758 Make_Expanded_Name (Loc,
13759 Chars => Chars (Gen_Id),
13760 Prefix => New_Occurrence_Of (Scope (Gen_Id), Loc),
13761 Selector_Name => New_Occurrence_Of (Gen_Id, Loc)));
13763 Set_Associated_Node (Name (N1), Name (N2));
13764 Set_Associated_Node (Prefix (Name (N1)), Empty);
13765 Set_Associated_Node
13766 (Selector_Name (Name (N1)), Selector_Name (Name (N2)));
13767 Set_Etype (Name (N1), Etype (Gen_Id));
13770 end Save_Global_Defaults;
13772 ----------------------------
13773 -- Save_Global_Descendant --
13774 ----------------------------
13776 procedure Save_Global_Descendant (D : Union_Id) is
13780 if D in Node_Range then
13781 if D = Union_Id (Empty) then
13784 elsif Nkind (Node_Id (D)) /= N_Compilation_Unit then
13785 Save_References (Node_Id (D));
13788 elsif D in List_Range then
13789 if D = Union_Id (No_List)
13790 or else Is_Empty_List (List_Id (D))
13795 N1 := First (List_Id (D));
13796 while Present (N1) loop
13797 Save_References (N1);
13802 -- Element list or other non-node field, nothing to do
13807 end Save_Global_Descendant;
13809 ---------------------
13810 -- Save_References --
13811 ---------------------
13813 -- This is the recursive procedure that does the work once the enclosing
13814 -- generic scope has been established. We have to treat specially a
13815 -- number of node rewritings that are required by semantic processing
13816 -- and which change the kind of nodes in the generic copy: typically
13817 -- constant-folding, replacing an operator node by a string literal, or
13818 -- a selected component by an expanded name. In each of those cases, the
13819 -- transformation is propagated to the generic unit.
13821 procedure Save_References (N : Node_Id) is
13822 Loc : constant Source_Ptr := Sloc (N);
13828 elsif Nkind_In (N, N_Character_Literal, N_Operator_Symbol) then
13829 if Nkind (N) = Nkind (Get_Associated_Node (N)) then
13832 elsif Nkind (N) = N_Operator_Symbol
13833 and then Nkind (Get_Associated_Node (N)) = N_String_Literal
13835 Change_Operator_Symbol_To_String_Literal (N);
13838 elsif Nkind (N) in N_Op then
13839 if Nkind (N) = Nkind (Get_Associated_Node (N)) then
13840 if Nkind (N) = N_Op_Concat then
13841 Set_Is_Component_Left_Opnd (N,
13842 Is_Component_Left_Opnd (Get_Associated_Node (N)));
13844 Set_Is_Component_Right_Opnd (N,
13845 Is_Component_Right_Opnd (Get_Associated_Node (N)));
13851 -- Node may be transformed into call to a user-defined operator
13853 N2 := Get_Associated_Node (N);
13855 if Nkind (N2) = N_Function_Call then
13856 E := Entity (Name (N2));
13859 and then Is_Global (E)
13861 Set_Etype (N, Etype (N2));
13863 Set_Associated_Node (N, Empty);
13864 Set_Etype (N, Empty);
13867 elsif Nkind_In (N2, N_Integer_Literal,
13871 if Present (Original_Node (N2))
13872 and then Nkind (Original_Node (N2)) = Nkind (N)
13875 -- Operation was constant-folded. Whenever possible,
13876 -- recover semantic information from unfolded node,
13879 Set_Associated_Node (N, Original_Node (N2));
13881 if Nkind (N) = N_Op_Concat then
13882 Set_Is_Component_Left_Opnd (N,
13883 Is_Component_Left_Opnd (Get_Associated_Node (N)));
13884 Set_Is_Component_Right_Opnd (N,
13885 Is_Component_Right_Opnd (Get_Associated_Node (N)));
13891 -- If original node is already modified, propagate
13892 -- constant-folding to template.
13894 Rewrite (N, New_Copy (N2));
13895 Set_Analyzed (N, False);
13898 elsif Nkind (N2) = N_Identifier
13899 and then Ekind (Entity (N2)) = E_Enumeration_Literal
13901 -- Same if call was folded into a literal, but in this case
13902 -- retain the entity to avoid spurious ambiguities if it is
13903 -- overloaded at the point of instantiation or inlining.
13905 Rewrite (N, New_Copy (N2));
13906 Set_Analyzed (N, False);
13910 -- Complete operands check if node has not been constant-folded
13912 if Nkind (N) in N_Op then
13913 Save_Entity_Descendants (N);
13916 elsif Nkind (N) = N_Identifier then
13917 if Nkind (N) = Nkind (Get_Associated_Node (N)) then
13919 -- If this is a discriminant reference, always save it. It is
13920 -- used in the instance to find the corresponding discriminant
13921 -- positionally rather than by name.
13923 Set_Original_Discriminant
13924 (N, Original_Discriminant (Get_Associated_Node (N)));
13928 N2 := Get_Associated_Node (N);
13930 if Nkind (N2) = N_Function_Call then
13931 E := Entity (Name (N2));
13933 -- Name resolves to a call to parameterless function. If
13934 -- original entity is global, mark node as resolved.
13937 and then Is_Global (E)
13939 Set_Etype (N, Etype (N2));
13941 Set_Associated_Node (N, Empty);
13942 Set_Etype (N, Empty);
13945 elsif Nkind_In (N2, N_Integer_Literal, N_Real_Literal)
13946 and then Is_Entity_Name (Original_Node (N2))
13948 -- Name resolves to named number that is constant-folded,
13949 -- We must preserve the original name for ASIS use, and
13950 -- undo the constant-folding, which will be repeated in
13953 Set_Associated_Node (N, Original_Node (N2));
13956 elsif Nkind (N2) = N_String_Literal then
13958 -- Name resolves to string literal. Perform the same
13959 -- replacement in generic.
13961 Rewrite (N, New_Copy (N2));
13963 elsif Nkind (N2) = N_Explicit_Dereference then
13965 -- An identifier is rewritten as a dereference if it is the
13966 -- prefix in an implicit dereference (call or attribute).
13967 -- The analysis of an instantiation will expand the node
13968 -- again, so we preserve the original tree but link it to
13969 -- the resolved entity in case it is global.
13971 if Is_Entity_Name (Prefix (N2))
13972 and then Present (Entity (Prefix (N2)))
13973 and then Is_Global (Entity (Prefix (N2)))
13975 Set_Associated_Node (N, Prefix (N2));
13977 elsif Nkind (Prefix (N2)) = N_Function_Call
13978 and then Is_Global (Entity (Name (Prefix (N2))))
13981 Make_Explicit_Dereference (Loc,
13982 Prefix => Make_Function_Call (Loc,
13984 New_Occurrence_Of (Entity (Name (Prefix (N2))),
13988 Set_Associated_Node (N, Empty);
13989 Set_Etype (N, Empty);
13992 -- The subtype mark of a nominally unconstrained object is
13993 -- rewritten as a subtype indication using the bounds of the
13994 -- expression. Recover the original subtype mark.
13996 elsif Nkind (N2) = N_Subtype_Indication
13997 and then Is_Entity_Name (Original_Node (N2))
13999 Set_Associated_Node (N, Original_Node (N2));
14007 elsif Nkind (N) in N_Entity then
14012 Qual : Node_Id := Empty;
14013 Typ : Entity_Id := Empty;
14016 use Atree.Unchecked_Access;
14017 -- This code section is part of implementing an untyped tree
14018 -- traversal, so it needs direct access to node fields.
14021 if Nkind_In (N, N_Aggregate, N_Extension_Aggregate) then
14022 N2 := Get_Associated_Node (N);
14029 -- In an instance within a generic, use the name of the
14030 -- actual and not the original generic parameter. If the
14031 -- actual is global in the current generic it must be
14032 -- preserved for its instantiation.
14034 if Nkind (Parent (Typ)) = N_Subtype_Declaration
14036 Present (Generic_Parent_Type (Parent (Typ)))
14038 Typ := Base_Type (Typ);
14039 Set_Etype (N2, Typ);
14045 or else not Is_Global (Typ)
14047 Set_Associated_Node (N, Empty);
14049 -- If the aggregate is an actual in a call, it has been
14050 -- resolved in the current context, to some local type.
14051 -- The enclosing call may have been disambiguated by the
14052 -- aggregate, and this disambiguation might fail at
14053 -- instantiation time because the type to which the
14054 -- aggregate did resolve is not preserved. In order to
14055 -- preserve some of this information, we wrap the
14056 -- aggregate in a qualified expression, using the id of
14057 -- its type. For further disambiguation we qualify the
14058 -- type name with its scope (if visible) because both
14059 -- id's will have corresponding entities in an instance.
14060 -- This resolves most of the problems with missing type
14061 -- information on aggregates in instances.
14063 if Nkind (N2) = Nkind (N)
14064 and then Nkind (Parent (N2)) in N_Subprogram_Call
14065 and then Comes_From_Source (Typ)
14067 if Is_Immediately_Visible (Scope (Typ)) then
14068 Nam := Make_Selected_Component (Loc,
14070 Make_Identifier (Loc, Chars (Scope (Typ))),
14072 Make_Identifier (Loc, Chars (Typ)));
14074 Nam := Make_Identifier (Loc, Chars (Typ));
14078 Make_Qualified_Expression (Loc,
14079 Subtype_Mark => Nam,
14080 Expression => Relocate_Node (N));
14084 Save_Global_Descendant (Field1 (N));
14085 Save_Global_Descendant (Field2 (N));
14086 Save_Global_Descendant (Field3 (N));
14087 Save_Global_Descendant (Field5 (N));
14089 if Present (Qual) then
14093 -- All other cases than aggregates
14096 Save_Global_Descendant (Field1 (N));
14097 Save_Global_Descendant (Field2 (N));
14098 Save_Global_Descendant (Field3 (N));
14099 Save_Global_Descendant (Field4 (N));
14100 Save_Global_Descendant (Field5 (N));
14105 -- If a node has aspects, references within their expressions must
14106 -- be saved separately, given they are not directly in the tree.
14108 if Has_Aspects (N) then
14113 Aspect := First (Aspect_Specifications (N));
14114 while Present (Aspect) loop
14115 if Present (Expression (Aspect)) then
14116 Save_Global_References (Expression (Aspect));
14123 end Save_References;
14125 -- Start of processing for Save_Global_References
14128 Gen_Scope := Current_Scope;
14130 -- If the generic unit is a child unit, references to entities in the
14131 -- parent are treated as local, because they will be resolved anew in
14132 -- the context of the instance of the parent.
14134 while Is_Child_Unit (Gen_Scope)
14135 and then Ekind (Scope (Gen_Scope)) = E_Generic_Package
14137 Gen_Scope := Scope (Gen_Scope);
14140 Save_References (N);
14141 end Save_Global_References;
14143 --------------------------------------
14144 -- Set_Copied_Sloc_For_Inlined_Body --
14145 --------------------------------------
14147 procedure Set_Copied_Sloc_For_Inlined_Body (N : Node_Id; E : Entity_Id) is
14149 Create_Instantiation_Source (N, E, True, S_Adjustment);
14150 end Set_Copied_Sloc_For_Inlined_Body;
14152 ---------------------
14153 -- Set_Instance_Of --
14154 ---------------------
14156 procedure Set_Instance_Of (A : Entity_Id; B : Entity_Id) is
14158 Generic_Renamings.Table (Generic_Renamings.Last) := (A, B, Assoc_Null);
14159 Generic_Renamings_HTable.Set (Generic_Renamings.Last);
14160 Generic_Renamings.Increment_Last;
14161 end Set_Instance_Of;
14163 --------------------
14164 -- Set_Next_Assoc --
14165 --------------------
14167 procedure Set_Next_Assoc (E : Assoc_Ptr; Next : Assoc_Ptr) is
14169 Generic_Renamings.Table (E).Next_In_HTable := Next;
14170 end Set_Next_Assoc;
14172 -------------------
14173 -- Start_Generic --
14174 -------------------
14176 procedure Start_Generic is
14178 -- ??? More things could be factored out in this routine.
14179 -- Should probably be done at a later stage.
14181 Generic_Flags.Append (Inside_A_Generic);
14182 Inside_A_Generic := True;
14184 Expander_Mode_Save_And_Set (False);
14187 ----------------------
14188 -- Set_Instance_Env --
14189 ----------------------
14191 procedure Set_Instance_Env
14192 (Gen_Unit : Entity_Id;
14193 Act_Unit : Entity_Id)
14195 Assertion_Status : constant Boolean := Assertions_Enabled;
14196 Save_SPARK_Mode : constant SPARK_Mode_Type := SPARK_Mode;
14197 Save_SPARK_Mode_Pragma : constant Node_Id := SPARK_Mode_Pragma;
14200 -- Regardless of the current mode, predefined units are analyzed in the
14201 -- most current Ada mode, and earlier version Ada checks do not apply
14202 -- to predefined units. Nothing needs to be done for non-internal units.
14203 -- These are always analyzed in the current mode.
14205 if Is_Internal_File_Name
14206 (Fname => Unit_File_Name (Get_Source_Unit (Gen_Unit)),
14207 Renamings_Included => True)
14209 Set_Opt_Config_Switches (True, Current_Sem_Unit = Main_Unit);
14211 -- In Ada2012 we may want to enable assertions in an instance of a
14212 -- predefined unit, in which case we need to preserve the current
14213 -- setting for the Assertions_Enabled flag. This will become more
14214 -- critical when pre/postconditions are added to predefined units,
14215 -- as is already the case for some numeric libraries.
14217 if Ada_Version >= Ada_2012 then
14218 Assertions_Enabled := Assertion_Status;
14221 -- SPARK_Mode for an instance is the one applicable at the point of
14224 SPARK_Mode := Save_SPARK_Mode;
14225 SPARK_Mode_Pragma := Save_SPARK_Mode_Pragma;
14228 Current_Instantiated_Parent :=
14229 (Gen_Id => Gen_Unit,
14230 Act_Id => Act_Unit,
14231 Next_In_HTable => Assoc_Null);
14232 end Set_Instance_Env;
14238 procedure Switch_View (T : Entity_Id) is
14239 BT : constant Entity_Id := Base_Type (T);
14240 Priv_Elmt : Elmt_Id := No_Elmt;
14241 Priv_Sub : Entity_Id;
14244 -- T may be private but its base type may have been exchanged through
14245 -- some other occurrence, in which case there is nothing to switch
14246 -- besides T itself. Note that a private dependent subtype of a private
14247 -- type might not have been switched even if the base type has been,
14248 -- because of the last branch of Check_Private_View (see comment there).
14250 if not Is_Private_Type (BT) then
14251 Prepend_Elmt (Full_View (T), Exchanged_Views);
14252 Exchange_Declarations (T);
14256 Priv_Elmt := First_Elmt (Private_Dependents (BT));
14258 if Present (Full_View (BT)) then
14259 Prepend_Elmt (Full_View (BT), Exchanged_Views);
14260 Exchange_Declarations (BT);
14263 while Present (Priv_Elmt) loop
14264 Priv_Sub := (Node (Priv_Elmt));
14266 -- We avoid flipping the subtype if the Etype of its full view is
14267 -- private because this would result in a malformed subtype. This
14268 -- occurs when the Etype of the subtype full view is the full view of
14269 -- the base type (and since the base types were just switched, the
14270 -- subtype is pointing to the wrong view). This is currently the case
14271 -- for tagged record types, access types (maybe more?) and needs to
14272 -- be resolved. ???
14274 if Present (Full_View (Priv_Sub))
14275 and then not Is_Private_Type (Etype (Full_View (Priv_Sub)))
14277 Prepend_Elmt (Full_View (Priv_Sub), Exchanged_Views);
14278 Exchange_Declarations (Priv_Sub);
14281 Next_Elmt (Priv_Elmt);
14289 function True_Parent (N : Node_Id) return Node_Id is
14291 if Nkind (Parent (N)) = N_Subunit then
14292 return Parent (Corresponding_Stub (Parent (N)));
14298 -----------------------------
14299 -- Valid_Default_Attribute --
14300 -----------------------------
14302 procedure Valid_Default_Attribute (Nam : Entity_Id; Def : Node_Id) is
14303 Attr_Id : constant Attribute_Id :=
14304 Get_Attribute_Id (Attribute_Name (Def));
14305 T : constant Entity_Id := Entity (Prefix (Def));
14306 Is_Fun : constant Boolean := (Ekind (Nam) = E_Function);
14319 F := First_Formal (Nam);
14320 while Present (F) loop
14321 Num_F := Num_F + 1;
14326 when Attribute_Adjacent | Attribute_Ceiling | Attribute_Copy_Sign |
14327 Attribute_Floor | Attribute_Fraction | Attribute_Machine |
14328 Attribute_Model | Attribute_Remainder | Attribute_Rounding |
14329 Attribute_Unbiased_Rounding =>
14332 and then Is_Floating_Point_Type (T);
14334 when Attribute_Image | Attribute_Pred | Attribute_Succ |
14335 Attribute_Value | Attribute_Wide_Image |
14336 Attribute_Wide_Value =>
14337 OK := (Is_Fun and then Num_F = 1 and then Is_Scalar_Type (T));
14339 when Attribute_Max | Attribute_Min =>
14340 OK := (Is_Fun and then Num_F = 2 and then Is_Scalar_Type (T));
14342 when Attribute_Input =>
14343 OK := (Is_Fun and then Num_F = 1);
14345 when Attribute_Output | Attribute_Read | Attribute_Write =>
14346 OK := (not Is_Fun and then Num_F = 2);
14353 Error_Msg_N ("attribute reference has wrong profile for subprogram",
14356 end Valid_Default_Attribute;