1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
9 -- Copyright (C) 1992-2014, Free Software Foundation, Inc. --
11 -- GNAT is free software; you can redistribute it and/or modify it under --
12 -- terms of the GNU General Public License as published by the Free Soft- --
13 -- ware Foundation; either version 3, or (at your option) any later ver- --
14 -- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
15 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
16 -- or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License --
17 -- for more details. You should have received a copy of the GNU General --
18 -- Public License distributed with GNAT; see file COPYING3. If not, go to --
19 -- http://www.gnu.org/licenses for a complete copy of the license. --
21 -- GNAT was originally developed by the GNAT team at New York University. --
22 -- Extensive contributions were provided by Ada Core Technologies Inc. --
24 ------------------------------------------------------------------------------
26 with Aspects; use Aspects;
27 with Atree; use Atree;
28 with Einfo; use Einfo;
29 with Elists; use Elists;
30 with Errout; use Errout;
31 with Expander; use Expander;
32 with Exp_Disp; use Exp_Disp;
33 with Exp_Util; use Exp_Util;
34 with Fname; use Fname;
35 with Fname.UF; use Fname.UF;
36 with Freeze; use Freeze;
37 with Itypes; use Itypes;
39 with Lib.Load; use Lib.Load;
40 with Lib.Xref; use Lib.Xref;
41 with Nlists; use Nlists;
42 with Namet; use Namet;
43 with Nmake; use Nmake;
45 with Rident; use Rident;
46 with Restrict; use Restrict;
47 with Rtsfind; use Rtsfind;
49 with Sem_Aux; use Sem_Aux;
50 with Sem_Cat; use Sem_Cat;
51 with Sem_Ch3; use Sem_Ch3;
52 with Sem_Ch6; use Sem_Ch6;
53 with Sem_Ch7; use Sem_Ch7;
54 with Sem_Ch8; use Sem_Ch8;
55 with Sem_Ch10; use Sem_Ch10;
56 with Sem_Ch13; use Sem_Ch13;
57 with Sem_Dim; use Sem_Dim;
58 with Sem_Disp; use Sem_Disp;
59 with Sem_Elab; use Sem_Elab;
60 with Sem_Elim; use Sem_Elim;
61 with Sem_Eval; use Sem_Eval;
62 with Sem_Prag; use Sem_Prag;
63 with Sem_Res; use Sem_Res;
64 with Sem_Type; use Sem_Type;
65 with Sem_Util; use Sem_Util;
66 with Sem_Warn; use Sem_Warn;
67 with Stand; use Stand;
68 with Sinfo; use Sinfo;
69 with Sinfo.CN; use Sinfo.CN;
70 with Sinput; use Sinput;
71 with Sinput.L; use Sinput.L;
72 with Snames; use Snames;
73 with Stringt; use Stringt;
74 with Uname; use Uname;
76 with Tbuild; use Tbuild;
77 with Uintp; use Uintp;
78 with Urealp; use Urealp;
79 with Warnsw; use Warnsw;
83 package body Sem_Ch12 is
85 ----------------------------------------------------------
86 -- Implementation of Generic Analysis and Instantiation --
87 ----------------------------------------------------------
89 -- GNAT implements generics by macro expansion. No attempt is made to share
90 -- generic instantiations (for now). Analysis of a generic definition does
91 -- not perform any expansion action, but the expander must be called on the
92 -- tree for each instantiation, because the expansion may of course depend
93 -- on the generic actuals. All of this is best achieved as follows:
95 -- a) Semantic analysis of a generic unit is performed on a copy of the
96 -- tree for the generic unit. All tree modifications that follow analysis
97 -- do not affect the original tree. Links are kept between the original
98 -- tree and the copy, in order to recognize non-local references within
99 -- the generic, and propagate them to each instance (recall that name
100 -- resolution is done on the generic declaration: generics are not really
101 -- macros). This is summarized in the following diagram:
103 -- .-----------. .----------.
104 -- | semantic |<--------------| generic |
106 -- | |==============>| |
107 -- |___________| global |__________|
118 -- b) Each instantiation copies the original tree, and inserts into it a
119 -- series of declarations that describe the mapping between generic formals
120 -- and actuals. For example, a generic In OUT parameter is an object
121 -- renaming of the corresponding actual, etc. Generic IN parameters are
122 -- constant declarations.
124 -- c) In order to give the right visibility for these renamings, we use
125 -- a different scheme for package and subprogram instantiations. For
126 -- packages, the list of renamings is inserted into the package
127 -- specification, before the visible declarations of the package. The
128 -- renamings are analyzed before any of the text of the instance, and are
129 -- thus visible at the right place. Furthermore, outside of the instance,
130 -- the generic parameters are visible and denote their corresponding
133 -- For subprograms, we create a container package to hold the renamings
134 -- and the subprogram instance itself. Analysis of the package makes the
135 -- renaming declarations visible to the subprogram. After analyzing the
136 -- package, the defining entity for the subprogram is touched-up so that
137 -- it appears declared in the current scope, and not inside the container
140 -- If the instantiation is a compilation unit, the container package is
141 -- given the same name as the subprogram instance. This ensures that
142 -- the elaboration procedure called by the binder, using the compilation
143 -- unit name, calls in fact the elaboration procedure for the package.
145 -- Not surprisingly, private types complicate this approach. By saving in
146 -- the original generic object the non-local references, we guarantee that
147 -- the proper entities are referenced at the point of instantiation.
148 -- However, for private types, this by itself does not insure that the
149 -- proper VIEW of the entity is used (the full type may be visible at the
150 -- point of generic definition, but not at instantiation, or vice-versa).
151 -- In order to reference the proper view, we special-case any reference
152 -- to private types in the generic object, by saving both views, one in
153 -- the generic and one in the semantic copy. At time of instantiation, we
154 -- check whether the two views are consistent, and exchange declarations if
155 -- necessary, in order to restore the correct visibility. Similarly, if
156 -- the instance view is private when the generic view was not, we perform
157 -- the exchange. After completing the instantiation, we restore the
158 -- current visibility. The flag Has_Private_View marks identifiers in the
159 -- the generic unit that require checking.
161 -- Visibility within nested generic units requires special handling.
162 -- Consider the following scheme:
164 -- type Global is ... -- outside of generic unit.
168 -- type Semi_Global is ... -- global to inner.
171 -- procedure inner (X1 : Global; X2 : Semi_Global);
173 -- procedure in2 is new inner (...); -- 4
176 -- package New_Outer is new Outer (...); -- 2
177 -- procedure New_Inner is new New_Outer.Inner (...); -- 3
179 -- The semantic analysis of Outer captures all occurrences of Global.
180 -- The semantic analysis of Inner (at 1) captures both occurrences of
181 -- Global and Semi_Global.
183 -- At point 2 (instantiation of Outer), we also produce a generic copy
184 -- of Inner, even though Inner is, at that point, not being instantiated.
185 -- (This is just part of the semantic analysis of New_Outer).
187 -- Critically, references to Global within Inner must be preserved, while
188 -- references to Semi_Global should not preserved, because they must now
189 -- resolve to an entity within New_Outer. To distinguish between these, we
190 -- use a global variable, Current_Instantiated_Parent, which is set when
191 -- performing a generic copy during instantiation (at 2). This variable is
192 -- used when performing a generic copy that is not an instantiation, but
193 -- that is nested within one, as the occurrence of 1 within 2. The analysis
194 -- of a nested generic only preserves references that are global to the
195 -- enclosing Current_Instantiated_Parent. We use the Scope_Depth value to
196 -- determine whether a reference is external to the given parent.
198 -- The instantiation at point 3 requires no special treatment. The method
199 -- works as well for further nestings of generic units, but of course the
200 -- variable Current_Instantiated_Parent must be stacked because nested
201 -- instantiations can occur, e.g. the occurrence of 4 within 2.
203 -- The instantiation of package and subprogram bodies is handled in a
204 -- similar manner, except that it is delayed until after semantic
205 -- analysis is complete. In this fashion complex cross-dependencies
206 -- between several package declarations and bodies containing generics
207 -- can be compiled which otherwise would diagnose spurious circularities.
209 -- For example, it is possible to compile two packages A and B that
210 -- have the following structure:
212 -- package A is package B is
213 -- generic ... generic ...
214 -- package G_A is package G_B is
217 -- package body A is package body B is
218 -- package N_B is new G_B (..) package N_A is new G_A (..)
220 -- The table Pending_Instantiations in package Inline is used to keep
221 -- track of body instantiations that are delayed in this manner. Inline
222 -- handles the actual calls to do the body instantiations. This activity
223 -- is part of Inline, since the processing occurs at the same point, and
224 -- for essentially the same reason, as the handling of inlined routines.
226 ----------------------------------------------
227 -- Detection of Instantiation Circularities --
228 ----------------------------------------------
230 -- If we have a chain of instantiations that is circular, this is static
231 -- error which must be detected at compile time. The detection of these
232 -- circularities is carried out at the point that we insert a generic
233 -- instance spec or body. If there is a circularity, then the analysis of
234 -- the offending spec or body will eventually result in trying to load the
235 -- same unit again, and we detect this problem as we analyze the package
236 -- instantiation for the second time.
238 -- At least in some cases after we have detected the circularity, we get
239 -- into trouble if we try to keep going. The following flag is set if a
240 -- circularity is detected, and used to abandon compilation after the
241 -- messages have been posted.
243 Circularity_Detected : Boolean := False;
244 -- This should really be reset on encountering a new main unit, but in
245 -- practice we are not using multiple main units so it is not critical.
247 --------------------------------------------------
248 -- Formal packages and partial parameterization --
249 --------------------------------------------------
251 -- When compiling a generic, a formal package is a local instantiation. If
252 -- declared with a box, its generic formals are visible in the enclosing
253 -- generic. If declared with a partial list of actuals, those actuals that
254 -- are defaulted (covered by an Others clause, or given an explicit box
255 -- initialization) are also visible in the enclosing generic, while those
256 -- that have a corresponding actual are not.
258 -- In our source model of instantiation, the same visibility must be
259 -- present in the spec and body of an instance: the names of the formals
260 -- that are defaulted must be made visible within the instance, and made
261 -- invisible (hidden) after the instantiation is complete, so that they
262 -- are not accessible outside of the instance.
264 -- In a generic, a formal package is treated like a special instantiation.
265 -- Our Ada 95 compiler handled formals with and without box in different
266 -- ways. With partial parameterization, we use a single model for both.
267 -- We create a package declaration that consists of the specification of
268 -- the generic package, and a set of declarations that map the actuals
269 -- into local renamings, just as we do for bona fide instantiations. For
270 -- defaulted parameters and formals with a box, we copy directly the
271 -- declarations of the formal into this local package. The result is a
272 -- a package whose visible declarations may include generic formals. This
273 -- package is only used for type checking and visibility analysis, and
274 -- never reaches the back-end, so it can freely violate the placement
275 -- rules for generic formal declarations.
277 -- The list of declarations (renamings and copies of formals) is built
278 -- by Analyze_Associations, just as for regular instantiations.
280 -- At the point of instantiation, conformance checking must be applied only
281 -- to those parameters that were specified in the formal. We perform this
282 -- checking by creating another internal instantiation, this one including
283 -- only the renamings and the formals (the rest of the package spec is not
284 -- relevant to conformance checking). We can then traverse two lists: the
285 -- list of actuals in the instance that corresponds to the formal package,
286 -- and the list of actuals produced for this bogus instantiation. We apply
287 -- the conformance rules to those actuals that are not defaulted (i.e.
288 -- which still appear as generic formals.
290 -- When we compile an instance body we must make the right parameters
291 -- visible again. The predicate Is_Generic_Formal indicates which of the
292 -- formals should have its Is_Hidden flag reset.
294 -----------------------
295 -- Local subprograms --
296 -----------------------
298 procedure Abandon_Instantiation (N : Node_Id);
299 pragma No_Return (Abandon_Instantiation);
300 -- Posts an error message "instantiation abandoned" at the indicated node
301 -- and then raises the exception Instantiation_Error to do it.
303 procedure Analyze_Formal_Array_Type
304 (T : in out Entity_Id;
306 -- A formal array type is treated like an array type declaration, and
307 -- invokes Array_Type_Declaration (sem_ch3) whose first parameter is
308 -- in-out, because in the case of an anonymous type the entity is
309 -- actually created in the procedure.
311 -- The following procedures treat other kinds of formal parameters
313 procedure Analyze_Formal_Derived_Interface_Type
318 procedure Analyze_Formal_Derived_Type
323 procedure Analyze_Formal_Interface_Type
328 -- The following subprograms create abbreviated declarations for formal
329 -- scalar types. We introduce an anonymous base of the proper class for
330 -- each of them, and define the formals as constrained first subtypes of
331 -- their bases. The bounds are expressions that are non-static in the
334 procedure Analyze_Formal_Decimal_Fixed_Point_Type
335 (T : Entity_Id; Def : Node_Id);
336 procedure Analyze_Formal_Discrete_Type (T : Entity_Id; Def : Node_Id);
337 procedure Analyze_Formal_Floating_Type (T : Entity_Id; Def : Node_Id);
338 procedure Analyze_Formal_Signed_Integer_Type (T : Entity_Id; Def : Node_Id);
339 procedure Analyze_Formal_Modular_Type (T : Entity_Id; Def : Node_Id);
340 procedure Analyze_Formal_Ordinary_Fixed_Point_Type
341 (T : Entity_Id; Def : Node_Id);
343 procedure Analyze_Formal_Private_Type
347 -- Creates a new private type, which does not require completion
349 procedure Analyze_Formal_Incomplete_Type (T : Entity_Id; Def : Node_Id);
350 -- Ada 2012: Creates a new incomplete type whose actual does not freeze
352 procedure Analyze_Generic_Formal_Part (N : Node_Id);
353 -- Analyze generic formal part
355 procedure Analyze_Generic_Access_Type (T : Entity_Id; Def : Node_Id);
356 -- Create a new access type with the given designated type
358 function Analyze_Associations
361 F_Copy : List_Id) return List_Id;
362 -- At instantiation time, build the list of associations between formals
363 -- and actuals. Each association becomes a renaming declaration for the
364 -- formal entity. F_Copy is the analyzed list of formals in the generic
365 -- copy. It is used to apply legality checks to the actuals. I_Node is the
366 -- instantiation node itself.
368 procedure Analyze_Subprogram_Instantiation
372 procedure Build_Instance_Compilation_Unit_Nodes
376 -- This procedure is used in the case where the generic instance of a
377 -- subprogram body or package body is a library unit. In this case, the
378 -- original library unit node for the generic instantiation must be
379 -- replaced by the resulting generic body, and a link made to a new
380 -- compilation unit node for the generic declaration. The argument N is
381 -- the original generic instantiation. Act_Body and Act_Decl are the body
382 -- and declaration of the instance (either package body and declaration
383 -- nodes or subprogram body and declaration nodes depending on the case).
384 -- On return, the node N has been rewritten with the actual body.
386 procedure Check_Access_Definition (N : Node_Id);
387 -- Subsidiary routine to null exclusion processing. Perform an assertion
388 -- check on Ada version and the presence of an access definition in N.
390 procedure Check_Formal_Packages (P_Id : Entity_Id);
391 -- Apply the following to all formal packages in generic associations
393 procedure Check_Formal_Package_Instance
394 (Formal_Pack : Entity_Id;
395 Actual_Pack : Entity_Id);
396 -- Verify that the actuals of the actual instance match the actuals of
397 -- the template for a formal package that is not declared with a box.
399 procedure Check_Forward_Instantiation (Decl : Node_Id);
400 -- If the generic is a local entity and the corresponding body has not
401 -- been seen yet, flag enclosing packages to indicate that it will be
402 -- elaborated after the generic body. Subprograms declared in the same
403 -- package cannot be inlined by the front-end because front-end inlining
404 -- requires a strict linear order of elaboration.
406 function Check_Hidden_Primitives (Assoc_List : List_Id) return Elist_Id;
407 -- Check if some association between formals and actuals requires to make
408 -- visible primitives of a tagged type, and make those primitives visible.
409 -- Return the list of primitives whose visibility is modified (to restore
410 -- their visibility later through Restore_Hidden_Primitives). If no
411 -- candidate is found then return No_Elist.
413 procedure Check_Hidden_Child_Unit
415 Gen_Unit : Entity_Id;
416 Act_Decl_Id : Entity_Id);
417 -- If the generic unit is an implicit child instance within a parent
418 -- instance, we need to make an explicit test that it is not hidden by
419 -- a child instance of the same name and parent.
421 procedure Check_Generic_Actuals
422 (Instance : Entity_Id;
423 Is_Formal_Box : Boolean);
424 -- Similar to previous one. Check the actuals in the instantiation,
425 -- whose views can change between the point of instantiation and the point
426 -- of instantiation of the body. In addition, mark the generic renamings
427 -- as generic actuals, so that they are not compatible with other actuals.
428 -- Recurse on an actual that is a formal package whose declaration has
431 function Contains_Instance_Of
434 N : Node_Id) return Boolean;
435 -- Inner is instantiated within the generic Outer. Check whether Inner
436 -- directly or indirectly contains an instance of Outer or of one of its
437 -- parents, in the case of a subunit. Each generic unit holds a list of
438 -- the entities instantiated within (at any depth). This procedure
439 -- determines whether the set of such lists contains a cycle, i.e. an
440 -- illegal circular instantiation.
442 function Denotes_Formal_Package
444 On_Exit : Boolean := False;
445 Instance : Entity_Id := Empty) return Boolean;
446 -- Returns True if E is a formal package of an enclosing generic, or
447 -- the actual for such a formal in an enclosing instantiation. If such
448 -- a package is used as a formal in an nested generic, or as an actual
449 -- in a nested instantiation, the visibility of ITS formals should not
450 -- be modified. When called from within Restore_Private_Views, the flag
451 -- On_Exit is true, to indicate that the search for a possible enclosing
452 -- instance should ignore the current one. In that case Instance denotes
453 -- the declaration for which this is an actual. This declaration may be
454 -- an instantiation in the source, or the internal instantiation that
455 -- corresponds to the actual for a formal package.
457 function Earlier (N1, N2 : Node_Id) return Boolean;
458 -- Yields True if N1 and N2 appear in the same compilation unit,
459 -- ignoring subunits, and if N1 is to the left of N2 in a left-to-right
460 -- traversal of the tree for the unit. Used to determine the placement
461 -- of freeze nodes for instance bodies that may depend on other instances.
463 function Find_Actual_Type
465 Gen_Type : Entity_Id) return Entity_Id;
466 -- When validating the actual types of a child instance, check whether
467 -- the formal is a formal type of the parent unit, and retrieve the current
468 -- actual for it. Typ is the entity in the analyzed formal type declaration
469 -- (component or index type of an array type, or designated type of an
470 -- access formal) and Gen_Type is the enclosing analyzed formal array
471 -- or access type. The desired actual may be a formal of a parent, or may
472 -- be declared in a formal package of a parent. In both cases it is a
473 -- generic actual type because it appears within a visible instance.
474 -- Finally, it may be declared in a parent unit without being a formal
475 -- of that unit, in which case it must be retrieved by visibility.
476 -- Ambiguities may still arise if two homonyms are declared in two formal
477 -- packages, and the prefix of the formal type may be needed to resolve
478 -- the ambiguity in the instance ???
480 function In_Same_Declarative_Part
482 Inst : Node_Id) return Boolean;
483 -- True if the instantiation Inst and the given freeze_node F_Node appear
484 -- within the same declarative part, ignoring subunits, but with no inter-
485 -- vening subprograms or concurrent units. Used to find the proper plave
486 -- for the freeze node of an instance, when the generic is declared in a
487 -- previous instance. If predicate is true, the freeze node of the instance
488 -- can be placed after the freeze node of the previous instance, Otherwise
489 -- it has to be placed at the end of the current declarative part.
491 function In_Main_Context (E : Entity_Id) return Boolean;
492 -- Check whether an instantiation is in the context of the main unit.
493 -- Used to determine whether its body should be elaborated to allow
494 -- front-end inlining.
496 procedure Set_Instance_Env
497 (Gen_Unit : Entity_Id;
498 Act_Unit : Entity_Id);
499 -- Save current instance on saved environment, to be used to determine
500 -- the global status of entities in nested instances. Part of Save_Env.
501 -- called after verifying that the generic unit is legal for the instance,
502 -- The procedure also examines whether the generic unit is a predefined
503 -- unit, in order to set configuration switches accordingly. As a result
504 -- the procedure must be called after analyzing and freezing the actuals.
506 procedure Set_Instance_Of (A : Entity_Id; B : Entity_Id);
507 -- Associate analyzed generic parameter with corresponding
508 -- instance. Used for semantic checks at instantiation time.
510 function Has_Been_Exchanged (E : Entity_Id) return Boolean;
511 -- Traverse the Exchanged_Views list to see if a type was private
512 -- and has already been flipped during this phase of instantiation.
514 procedure Hide_Current_Scope;
515 -- When instantiating a generic child unit, the parent context must be
516 -- present, but the instance and all entities that may be generated
517 -- must be inserted in the current scope. We leave the current scope
518 -- on the stack, but make its entities invisible to avoid visibility
519 -- problems. This is reversed at the end of the instantiation. This is
520 -- not done for the instantiation of the bodies, which only require the
521 -- instances of the generic parents to be in scope.
523 procedure Install_Body
528 -- If the instantiation happens textually before the body of the generic,
529 -- the instantiation of the body must be analyzed after the generic body,
530 -- and not at the point of instantiation. Such early instantiations can
531 -- happen if the generic and the instance appear in a package declaration
532 -- because the generic body can only appear in the corresponding package
533 -- body. Early instantiations can also appear if generic, instance and
534 -- body are all in the declarative part of a subprogram or entry. Entities
535 -- of packages that are early instantiations are delayed, and their freeze
536 -- node appears after the generic body.
538 procedure Insert_Freeze_Node_For_Instance
541 -- N denotes a package or a subprogram instantiation and F_Node is the
542 -- associated freeze node. Insert the freeze node before the first source
543 -- body which follows immediately after N. If no such body is found, the
544 -- freeze node is inserted at the end of the declarative region which
547 procedure Freeze_Subprogram_Body
548 (Inst_Node : Node_Id;
550 Pack_Id : Entity_Id);
551 -- The generic body may appear textually after the instance, including
552 -- in the proper body of a stub, or within a different package instance.
553 -- Given that the instance can only be elaborated after the generic, we
554 -- place freeze_nodes for the instance and/or for packages that may enclose
555 -- the instance and the generic, so that the back-end can establish the
556 -- proper order of elaboration.
559 -- Establish environment for subsequent instantiation. Separated from
560 -- Save_Env because data-structures for visibility handling must be
561 -- initialized before call to Check_Generic_Child_Unit.
563 procedure Install_Formal_Packages (Par : Entity_Id);
564 -- Install the visible part of any formal of the parent that is a formal
565 -- package. Note that for the case of a formal package with a box, this
566 -- includes the formal part of the formal package (12.7(10/2)).
568 procedure Install_Parent (P : Entity_Id; In_Body : Boolean := False);
569 -- When compiling an instance of a child unit the parent (which is
570 -- itself an instance) is an enclosing scope that must be made
571 -- immediately visible. This procedure is also used to install the non-
572 -- generic parent of a generic child unit when compiling its body, so
573 -- that full views of types in the parent are made visible.
575 procedure Remove_Parent (In_Body : Boolean := False);
576 -- Reverse effect after instantiation of child is complete
578 procedure Install_Hidden_Primitives
579 (Prims_List : in out Elist_Id;
582 -- Remove suffix 'P' from hidden primitives of Act_T to match the
583 -- visibility of primitives of Gen_T. The list of primitives to which
584 -- the suffix is removed is added to Prims_List to restore them later.
586 procedure Restore_Hidden_Primitives (Prims_List : in out Elist_Id);
587 -- Restore suffix 'P' to primitives of Prims_List and leave Prims_List
590 procedure Inline_Instance_Body
592 Gen_Unit : Entity_Id;
594 -- If front-end inlining is requested, instantiate the package body,
595 -- and preserve the visibility of its compilation unit, to insure
596 -- that successive instantiations succeed.
598 -- The functions Instantiate_XXX perform various legality checks and build
599 -- the declarations for instantiated generic parameters. In all of these
600 -- Formal is the entity in the generic unit, Actual is the entity of
601 -- expression in the generic associations, and Analyzed_Formal is the
602 -- formal in the generic copy, which contains the semantic information to
603 -- be used to validate the actual.
605 function Instantiate_Object
608 Analyzed_Formal : Node_Id) return List_Id;
610 function Instantiate_Type
613 Analyzed_Formal : Node_Id;
614 Actual_Decls : List_Id) return List_Id;
616 function Instantiate_Formal_Subprogram
619 Analyzed_Formal : Node_Id) return Node_Id;
621 function Instantiate_Formal_Package
624 Analyzed_Formal : Node_Id) return List_Id;
625 -- If the formal package is declared with a box, special visibility rules
626 -- apply to its formals: they are in the visible part of the package. This
627 -- is true in the declarative region of the formal package, that is to say
628 -- in the enclosing generic or instantiation. For an instantiation, the
629 -- parameters of the formal package are made visible in an explicit step.
630 -- Furthermore, if the actual has a visible USE clause, these formals must
631 -- be made potentially use-visible as well. On exit from the enclosing
632 -- instantiation, the reverse must be done.
634 -- For a formal package declared without a box, there are conformance rules
635 -- that apply to the actuals in the generic declaration and the actuals of
636 -- the actual package in the enclosing instantiation. The simplest way to
637 -- apply these rules is to repeat the instantiation of the formal package
638 -- in the context of the enclosing instance, and compare the generic
639 -- associations of this instantiation with those of the actual package.
640 -- This internal instantiation only needs to contain the renamings of the
641 -- formals: the visible and private declarations themselves need not be
644 -- In Ada 2005, the formal package may be only partially parameterized.
645 -- In that case the visibility step must make visible those actuals whose
646 -- corresponding formals were given with a box. A final complication
647 -- involves inherited operations from formal derived types, which must
648 -- be visible if the type is.
650 function Is_In_Main_Unit (N : Node_Id) return Boolean;
651 -- Test if given node is in the main unit
653 procedure Load_Parent_Of_Generic
656 Body_Optional : Boolean := False);
657 -- If the generic appears in a separate non-generic library unit, load the
658 -- corresponding body to retrieve the body of the generic. N is the node
659 -- for the generic instantiation, Spec is the generic package declaration.
661 -- Body_Optional is a flag that indicates that the body is being loaded to
662 -- ensure that temporaries are generated consistently when there are other
663 -- instances in the current declarative part that precede the one being
664 -- loaded. In that case a missing body is acceptable.
666 procedure Inherit_Context (Gen_Decl : Node_Id; Inst : Node_Id);
667 -- Add the context clause of the unit containing a generic unit to a
668 -- compilation unit that is, or contains, an instantiation.
670 function Get_Associated_Node (N : Node_Id) return Node_Id;
671 -- In order to propagate semantic information back from the analyzed copy
672 -- to the original generic, we maintain links between selected nodes in the
673 -- generic and their corresponding copies. At the end of generic analysis,
674 -- the routine Save_Global_References traverses the generic tree, examines
675 -- the semantic information, and preserves the links to those nodes that
676 -- contain global information. At instantiation, the information from the
677 -- associated node is placed on the new copy, so that name resolution is
680 -- Three kinds of source nodes have associated nodes:
682 -- a) those that can reference (denote) entities, that is identifiers,
683 -- character literals, expanded_names, operator symbols, operators,
684 -- and attribute reference nodes. These nodes have an Entity field
685 -- and are the set of nodes that are in N_Has_Entity.
687 -- b) aggregates (N_Aggregate and N_Extension_Aggregate)
689 -- c) selected components (N_Selected_Component)
691 -- For the first class, the associated node preserves the entity if it is
692 -- global. If the generic contains nested instantiations, the associated
693 -- node itself has been recopied, and a chain of them must be followed.
695 -- For aggregates, the associated node allows retrieval of the type, which
696 -- may otherwise not appear in the generic. The view of this type may be
697 -- different between generic and instantiation, and the full view can be
698 -- installed before the instantiation is analyzed. For aggregates of type
699 -- extensions, the same view exchange may have to be performed for some of
700 -- the ancestor types, if their view is private at the point of
703 -- Nodes that are selected components in the parse tree may be rewritten
704 -- as expanded names after resolution, and must be treated as potential
705 -- entity holders, which is why they also have an Associated_Node.
707 -- Nodes that do not come from source, such as freeze nodes, do not appear
708 -- in the generic tree, and need not have an associated node.
710 -- The associated node is stored in the Associated_Node field. Note that
711 -- this field overlaps Entity, which is fine, because the whole point is
712 -- that we don't need or want the normal Entity field in this situation.
714 procedure Map_Formal_Package_Entities (Form : Entity_Id; Act : Entity_Id);
715 -- Within the generic part, entities in the formal package are
716 -- visible. To validate subsequent type declarations, indicate
717 -- the correspondence between the entities in the analyzed formal,
718 -- and the entities in the actual package. There are three packages
719 -- involved in the instantiation of a formal package: the parent
720 -- generic P1 which appears in the generic declaration, the fake
721 -- instantiation P2 which appears in the analyzed generic, and whose
722 -- visible entities may be used in subsequent formals, and the actual
723 -- P3 in the instance. To validate subsequent formals, me indicate
724 -- that the entities in P2 are mapped into those of P3. The mapping of
725 -- entities has to be done recursively for nested packages.
727 procedure Move_Freeze_Nodes
731 -- Freeze nodes can be generated in the analysis of a generic unit, but
732 -- will not be seen by the back-end. It is necessary to move those nodes
733 -- to the enclosing scope if they freeze an outer entity. We place them
734 -- at the end of the enclosing generic package, which is semantically
737 procedure Preanalyze_Actuals (N : Node_Id);
738 -- Analyze actuals to perform name resolution. Full resolution is done
739 -- later, when the expected types are known, but names have to be captured
740 -- before installing parents of generics, that are not visible for the
741 -- actuals themselves.
743 function True_Parent (N : Node_Id) return Node_Id;
744 -- For a subunit, return parent of corresponding stub, else return
747 procedure Valid_Default_Attribute (Nam : Entity_Id; Def : Node_Id);
748 -- Verify that an attribute that appears as the default for a formal
749 -- subprogram is a function or procedure with the correct profile.
751 -------------------------------------------
752 -- Data Structures for Generic Renamings --
753 -------------------------------------------
755 -- The map Generic_Renamings associates generic entities with their
756 -- corresponding actuals. Currently used to validate type instances. It
757 -- will eventually be used for all generic parameters to eliminate the
758 -- need for overload resolution in the instance.
760 type Assoc_Ptr is new Int;
762 Assoc_Null : constant Assoc_Ptr := -1;
767 Next_In_HTable : Assoc_Ptr;
770 package Generic_Renamings is new Table.Table
771 (Table_Component_Type => Assoc,
772 Table_Index_Type => Assoc_Ptr,
773 Table_Low_Bound => 0,
775 Table_Increment => 100,
776 Table_Name => "Generic_Renamings");
778 -- Variable to hold enclosing instantiation. When the environment is
779 -- saved for a subprogram inlining, the corresponding Act_Id is empty.
781 Current_Instantiated_Parent : Assoc := (Empty, Empty, Assoc_Null);
783 -- Hash table for associations
785 HTable_Size : constant := 37;
786 type HTable_Range is range 0 .. HTable_Size - 1;
788 procedure Set_Next_Assoc (E : Assoc_Ptr; Next : Assoc_Ptr);
789 function Next_Assoc (E : Assoc_Ptr) return Assoc_Ptr;
790 function Get_Gen_Id (E : Assoc_Ptr) return Entity_Id;
791 function Hash (F : Entity_Id) return HTable_Range;
793 package Generic_Renamings_HTable is new GNAT.HTable.Static_HTable (
794 Header_Num => HTable_Range,
796 Elmt_Ptr => Assoc_Ptr,
797 Null_Ptr => Assoc_Null,
798 Set_Next => Set_Next_Assoc,
801 Get_Key => Get_Gen_Id,
805 Exchanged_Views : Elist_Id;
806 -- This list holds the private views that have been exchanged during
807 -- instantiation to restore the visibility of the generic declaration.
808 -- (see comments above). After instantiation, the current visibility is
809 -- reestablished by means of a traversal of this list.
811 Hidden_Entities : Elist_Id;
812 -- This list holds the entities of the current scope that are removed
813 -- from immediate visibility when instantiating a child unit. Their
814 -- visibility is restored in Remove_Parent.
816 -- Because instantiations can be recursive, the following must be saved
817 -- on entry and restored on exit from an instantiation (spec or body).
818 -- This is done by the two procedures Save_Env and Restore_Env. For
819 -- package and subprogram instantiations (but not for the body instances)
820 -- the action of Save_Env is done in two steps: Init_Env is called before
821 -- Check_Generic_Child_Unit, because setting the parent instances requires
822 -- that the visibility data structures be properly initialized. Once the
823 -- generic is unit is validated, Set_Instance_Env completes Save_Env.
825 Parent_Unit_Visible : Boolean := False;
826 -- Parent_Unit_Visible is used when the generic is a child unit, and
827 -- indicates whether the ultimate parent of the generic is visible in the
828 -- instantiation environment. It is used to reset the visibility of the
829 -- parent at the end of the instantiation (see Remove_Parent).
831 Instance_Parent_Unit : Entity_Id := Empty;
832 -- This records the ultimate parent unit of an instance of a generic
833 -- child unit and is used in conjunction with Parent_Unit_Visible to
834 -- indicate the unit to which the Parent_Unit_Visible flag corresponds.
836 type Instance_Env is record
837 Instantiated_Parent : Assoc;
838 Exchanged_Views : Elist_Id;
839 Hidden_Entities : Elist_Id;
840 Current_Sem_Unit : Unit_Number_Type;
841 Parent_Unit_Visible : Boolean := False;
842 Instance_Parent_Unit : Entity_Id := Empty;
843 Switches : Config_Switches_Type;
846 package Instance_Envs is new Table.Table (
847 Table_Component_Type => Instance_Env,
848 Table_Index_Type => Int,
849 Table_Low_Bound => 0,
851 Table_Increment => 100,
852 Table_Name => "Instance_Envs");
854 procedure Restore_Private_Views
855 (Pack_Id : Entity_Id;
856 Is_Package : Boolean := True);
857 -- Restore the private views of external types, and unmark the generic
858 -- renamings of actuals, so that they become compatible subtypes again.
859 -- For subprograms, Pack_Id is the package constructed to hold the
862 procedure Switch_View (T : Entity_Id);
863 -- Switch the partial and full views of a type and its private
864 -- dependents (i.e. its subtypes and derived types).
866 ------------------------------------
867 -- Structures for Error Reporting --
868 ------------------------------------
870 Instantiation_Node : Node_Id;
871 -- Used by subprograms that validate instantiation of formal parameters
872 -- where there might be no actual on which to place the error message.
873 -- Also used to locate the instantiation node for generic subunits.
875 Instantiation_Error : exception;
876 -- When there is a semantic error in the generic parameter matching,
877 -- there is no point in continuing the instantiation, because the
878 -- number of cascaded errors is unpredictable. This exception aborts
879 -- the instantiation process altogether.
881 S_Adjustment : Sloc_Adjustment;
882 -- Offset created for each node in an instantiation, in order to keep
883 -- track of the source position of the instantiation in each of its nodes.
884 -- A subsequent semantic error or warning on a construct of the instance
885 -- points to both places: the original generic node, and the point of
886 -- instantiation. See Sinput and Sinput.L for additional details.
888 ------------------------------------------------------------
889 -- Data structure for keeping track when inside a Generic --
890 ------------------------------------------------------------
892 -- The following table is used to save values of the Inside_A_Generic
893 -- flag (see spec of Sem) when they are saved by Start_Generic.
895 package Generic_Flags is new Table.Table (
896 Table_Component_Type => Boolean,
897 Table_Index_Type => Int,
898 Table_Low_Bound => 0,
900 Table_Increment => 200,
901 Table_Name => "Generic_Flags");
903 ---------------------------
904 -- Abandon_Instantiation --
905 ---------------------------
907 procedure Abandon_Instantiation (N : Node_Id) is
909 Error_Msg_N ("\instantiation abandoned!", N);
910 raise Instantiation_Error;
911 end Abandon_Instantiation;
913 --------------------------
914 -- Analyze_Associations --
915 --------------------------
917 function Analyze_Associations
920 F_Copy : List_Id) return List_Id
922 Actuals_To_Freeze : constant Elist_Id := New_Elmt_List;
923 Assoc : constant List_Id := New_List;
924 Default_Actuals : constant Elist_Id := New_Elmt_List;
925 Gen_Unit : constant Entity_Id :=
926 Defining_Entity (Parent (F_Copy));
930 Analyzed_Formal : Node_Id;
931 First_Named : Node_Id := Empty;
935 Saved_Formal : Node_Id;
937 Default_Formals : constant List_Id := New_List;
938 -- If an Others_Choice is present, some of the formals may be defaulted.
939 -- To simplify the treatment of visibility in an instance, we introduce
940 -- individual defaults for each such formal. These defaults are
941 -- appended to the list of associations and replace the Others_Choice.
943 Found_Assoc : Node_Id;
944 -- Association for the current formal being match. Empty if there are
945 -- no remaining actuals, or if there is no named association with the
946 -- name of the formal.
948 Is_Named_Assoc : Boolean;
949 Num_Matched : Int := 0;
950 Num_Actuals : Int := 0;
952 Others_Present : Boolean := False;
953 Others_Choice : Node_Id := Empty;
954 -- In Ada 2005, indicates partial parameterization of a formal
955 -- package. As usual an other association must be last in the list.
957 function Build_Wrapper
959 Actual : Entity_Id := Empty) return Node_Id;
960 -- In GNATProve mode, create a wrapper function for actuals that are
961 -- operators, in order to propagate their contract to the renaming
962 -- declarations generated for them. If the actual is absent, this is
963 -- a formal with a default, and the name of the operator is that of the
966 procedure Check_Overloaded_Formal_Subprogram (Formal : Entity_Id);
967 -- Apply RM 12.3 (9): if a formal subprogram is overloaded, the instance
968 -- cannot have a named association for it. AI05-0025 extends this rule
969 -- to formals of formal packages by AI05-0025, and it also applies to
970 -- box-initialized formals.
972 function Has_Fully_Defined_Profile (Subp : Entity_Id) return Boolean;
973 -- Determine whether the parameter types and the return type of Subp
974 -- are fully defined at the point of instantiation.
976 function Matching_Actual
978 A_F : Entity_Id) return Node_Id;
979 -- Find actual that corresponds to a given a formal parameter. If the
980 -- actuals are positional, return the next one, if any. If the actuals
981 -- are named, scan the parameter associations to find the right one.
982 -- A_F is the corresponding entity in the analyzed generic,which is
983 -- placed on the selector name for ASIS use.
985 -- In Ada 2005, a named association may be given with a box, in which
986 -- case Matching_Actual sets Found_Assoc to the generic association,
987 -- but return Empty for the actual itself. In this case the code below
988 -- creates a corresponding declaration for the formal.
990 function Partial_Parameterization return Boolean;
991 -- Ada 2005: if no match is found for a given formal, check if the
992 -- association for it includes a box, or whether the associations
993 -- include an Others clause.
995 procedure Process_Default (F : Entity_Id);
996 -- Add a copy of the declaration of generic formal F to the list of
997 -- associations, and add an explicit box association for F if there
998 -- is none yet, and the default comes from an Others_Choice.
1000 function Renames_Standard_Subprogram (Subp : Entity_Id) return Boolean;
1001 -- Determine whether Subp renames one of the subprograms defined in the
1002 -- generated package Standard.
1004 procedure Set_Analyzed_Formal;
1005 -- Find the node in the generic copy that corresponds to a given formal.
1006 -- The semantic information on this node is used to perform legality
1007 -- checks on the actuals. Because semantic analysis can introduce some
1008 -- anonymous entities or modify the declaration node itself, the
1009 -- correspondence between the two lists is not one-one. In addition to
1010 -- anonymous types, the presence a formal equality will introduce an
1011 -- implicit declaration for the corresponding inequality.
1017 function Build_Wrapper
1018 (Formal : Entity_Id;
1019 Actual : Entity_Id := Empty) return Node_Id
1021 Loc : constant Source_Ptr := Sloc (I_Node);
1022 Typ : constant Entity_Id := Etype (Formal);
1023 Is_Binary : constant Boolean :=
1024 Present (Next_Formal (First_Formal (Formal)));
1037 Op_Name := Chars (Formal);
1039 Op_Name := Chars (Actual);
1042 -- Create entities for wrapper function and its formals
1044 F1 := Make_Temporary (Loc, 'A');
1045 F2 := Make_Temporary (Loc, 'B');
1046 L := New_Occurrence_Of (F1, Loc);
1047 R := New_Occurrence_Of (F2, Loc);
1049 Func := Make_Defining_Identifier (Loc, Chars (Formal));
1050 Set_Ekind (Func, E_Function);
1051 Set_Is_Generic_Actual_Subprogram (Func);
1054 Make_Function_Specification (Loc,
1055 Defining_Unit_Name => Func,
1056 Parameter_Specifications => New_List (
1057 Make_Parameter_Specification (Loc,
1058 Defining_Identifier => F1,
1060 Make_Identifier (Loc,
1061 Chars => Chars (Etype (First_Formal (Formal)))))),
1062 Result_Definition => Make_Identifier (Loc, Chars (Typ)));
1065 Append_To (Parameter_Specifications (Spec),
1066 Make_Parameter_Specification (Loc,
1067 Defining_Identifier => F2,
1069 Make_Identifier (Loc,
1070 Chars (Etype (Next_Formal (First_Formal (Formal)))))));
1073 -- Build expression as a function call, or as an operator node
1074 -- that corresponds to the name of the actual, starting with binary
1077 if Present (Actual) and then Op_Name not in Any_Operator_Name then
1079 Make_Function_Call (Loc,
1081 New_Occurrence_Of (Entity (Actual), Loc),
1082 Parameter_Associations => New_List (L));
1085 Append_To (Parameter_Associations (Expr), R);
1090 elsif Is_Binary then
1091 if Op_Name = Name_Op_And then
1092 Expr := Make_Op_And (Loc, Left_Opnd => L, Right_Opnd => R);
1093 elsif Op_Name = Name_Op_Or then
1094 Expr := Make_Op_Or (Loc, Left_Opnd => L, Right_Opnd => R);
1095 elsif Op_Name = Name_Op_Xor then
1096 Expr := Make_Op_Xor (Loc, Left_Opnd => L, Right_Opnd => R);
1097 elsif Op_Name = Name_Op_Eq then
1098 Expr := Make_Op_Eq (Loc, Left_Opnd => L, Right_Opnd => R);
1099 elsif Op_Name = Name_Op_Ne then
1100 Expr := Make_Op_Ne (Loc, Left_Opnd => L, Right_Opnd => R);
1101 elsif Op_Name = Name_Op_Le then
1102 Expr := Make_Op_Le (Loc, Left_Opnd => L, Right_Opnd => R);
1103 elsif Op_Name = Name_Op_Gt then
1104 Expr := Make_Op_Gt (Loc, Left_Opnd => L, Right_Opnd => R);
1105 elsif Op_Name = Name_Op_Ge then
1106 Expr := Make_Op_Ge (Loc, Left_Opnd => L, Right_Opnd => R);
1107 elsif Op_Name = Name_Op_Lt then
1108 Expr := Make_Op_Lt (Loc, Left_Opnd => L, Right_Opnd => R);
1109 elsif Op_Name = Name_Op_Add then
1110 Expr := Make_Op_Add (Loc, Left_Opnd => L, Right_Opnd => R);
1111 elsif Op_Name = Name_Op_Subtract then
1112 Expr := Make_Op_Subtract (Loc, Left_Opnd => L, Right_Opnd => R);
1113 elsif Op_Name = Name_Op_Concat then
1114 Expr := Make_Op_Concat (Loc, Left_Opnd => L, Right_Opnd => R);
1115 elsif Op_Name = Name_Op_Multiply then
1116 Expr := Make_Op_Multiply (Loc, Left_Opnd => L, Right_Opnd => R);
1117 elsif Op_Name = Name_Op_Divide then
1118 Expr := Make_Op_Divide (Loc, Left_Opnd => L, Right_Opnd => R);
1119 elsif Op_Name = Name_Op_Mod then
1120 Expr := Make_Op_Mod (Loc, Left_Opnd => L, Right_Opnd => R);
1121 elsif Op_Name = Name_Op_Rem then
1122 Expr := Make_Op_Rem (Loc, Left_Opnd => L, Right_Opnd => R);
1123 elsif Op_Name = Name_Op_Expon then
1124 Expr := Make_Op_Expon (Loc, Left_Opnd => L, Right_Opnd => R);
1130 if Op_Name = Name_Op_Add then
1131 Expr := Make_Op_Plus (Loc, Right_Opnd => L);
1132 elsif Op_Name = Name_Op_Subtract then
1133 Expr := Make_Op_Minus (Loc, Right_Opnd => L);
1134 elsif Op_Name = Name_Op_Abs then
1135 Expr := Make_Op_Abs (Loc, Right_Opnd => L);
1136 elsif Op_Name = Name_Op_Not then
1137 Expr := Make_Op_Not (Loc, Right_Opnd => L);
1141 -- Propagate visible entity to operator node, either from a
1142 -- given actual or from a default.
1144 if Is_Entity_Name (Actual) and then Nkind (Expr) in N_Op then
1145 Set_Entity (Expr, Entity (Actual));
1149 Make_Expression_Function (Loc,
1150 Specification => Spec,
1151 Expression => Expr);
1156 ----------------------------------------
1157 -- Check_Overloaded_Formal_Subprogram --
1158 ----------------------------------------
1160 procedure Check_Overloaded_Formal_Subprogram (Formal : Entity_Id) is
1161 Temp_Formal : Entity_Id;
1164 Temp_Formal := First (Formals);
1165 while Present (Temp_Formal) loop
1166 if Nkind (Temp_Formal) in N_Formal_Subprogram_Declaration
1167 and then Temp_Formal /= Formal
1169 Chars (Defining_Unit_Name (Specification (Formal))) =
1170 Chars (Defining_Unit_Name (Specification (Temp_Formal)))
1172 if Present (Found_Assoc) then
1174 ("named association not allowed for overloaded formal",
1179 ("named association not allowed for overloaded formal",
1183 Abandon_Instantiation (Instantiation_Node);
1188 end Check_Overloaded_Formal_Subprogram;
1190 -------------------------------
1191 -- Has_Fully_Defined_Profile --
1192 -------------------------------
1194 function Has_Fully_Defined_Profile (Subp : Entity_Id) return Boolean is
1195 function Is_Fully_Defined_Type (Typ : Entity_Id) return Boolean;
1196 -- Determine whethet type Typ is fully defined
1198 ---------------------------
1199 -- Is_Fully_Defined_Type --
1200 ---------------------------
1202 function Is_Fully_Defined_Type (Typ : Entity_Id) return Boolean is
1204 -- A private type without a full view is not fully defined
1206 if Is_Private_Type (Typ)
1207 and then No (Full_View (Typ))
1211 -- An incomplete type is never fully defined
1213 elsif Is_Incomplete_Type (Typ) then
1216 -- All other types are fully defined
1221 end Is_Fully_Defined_Type;
1223 -- Local declarations
1227 -- Start of processing for Has_Fully_Defined_Profile
1230 -- Check the parameters
1232 Param := First_Formal (Subp);
1233 while Present (Param) loop
1234 if not Is_Fully_Defined_Type (Etype (Param)) then
1238 Next_Formal (Param);
1241 -- Check the return type
1243 return Is_Fully_Defined_Type (Etype (Subp));
1244 end Has_Fully_Defined_Profile;
1246 ---------------------
1247 -- Matching_Actual --
1248 ---------------------
1250 function Matching_Actual
1252 A_F : Entity_Id) return Node_Id
1258 Is_Named_Assoc := False;
1260 -- End of list of purely positional parameters
1262 if No (Actual) or else Nkind (Actual) = N_Others_Choice then
1263 Found_Assoc := Empty;
1266 -- Case of positional parameter corresponding to current formal
1268 elsif No (Selector_Name (Actual)) then
1269 Found_Assoc := Actual;
1270 Act := Explicit_Generic_Actual_Parameter (Actual);
1271 Num_Matched := Num_Matched + 1;
1274 -- Otherwise scan list of named actuals to find the one with the
1275 -- desired name. All remaining actuals have explicit names.
1278 Is_Named_Assoc := True;
1279 Found_Assoc := Empty;
1283 while Present (Actual) loop
1284 if Chars (Selector_Name (Actual)) = Chars (F) then
1285 Set_Entity (Selector_Name (Actual), A_F);
1286 Set_Etype (Selector_Name (Actual), Etype (A_F));
1287 Generate_Reference (A_F, Selector_Name (Actual));
1288 Found_Assoc := Actual;
1289 Act := Explicit_Generic_Actual_Parameter (Actual);
1290 Num_Matched := Num_Matched + 1;
1298 -- Reset for subsequent searches. In most cases the named
1299 -- associations are in order. If they are not, we reorder them
1300 -- to avoid scanning twice the same actual. This is not just a
1301 -- question of efficiency: there may be multiple defaults with
1302 -- boxes that have the same name. In a nested instantiation we
1303 -- insert actuals for those defaults, and cannot rely on their
1304 -- names to disambiguate them.
1306 if Actual = First_Named then
1309 elsif Present (Actual) then
1310 Insert_Before (First_Named, Remove_Next (Prev));
1313 Actual := First_Named;
1316 if Is_Entity_Name (Act) and then Present (Entity (Act)) then
1317 Set_Used_As_Generic_Actual (Entity (Act));
1321 end Matching_Actual;
1323 ------------------------------
1324 -- Partial_Parameterization --
1325 ------------------------------
1327 function Partial_Parameterization return Boolean is
1329 return Others_Present
1330 or else (Present (Found_Assoc) and then Box_Present (Found_Assoc));
1331 end Partial_Parameterization;
1333 ---------------------
1334 -- Process_Default --
1335 ---------------------
1337 procedure Process_Default (F : Entity_Id) is
1338 Loc : constant Source_Ptr := Sloc (I_Node);
1339 F_Id : constant Entity_Id := Defining_Entity (F);
1345 -- Append copy of formal declaration to associations, and create new
1346 -- defining identifier for it.
1348 Decl := New_Copy_Tree (F);
1349 Id := Make_Defining_Identifier (Sloc (F_Id), Chars (F_Id));
1351 if Nkind (F) in N_Formal_Subprogram_Declaration then
1352 Set_Defining_Unit_Name (Specification (Decl), Id);
1355 Set_Defining_Identifier (Decl, Id);
1358 Append (Decl, Assoc);
1360 if No (Found_Assoc) then
1362 Make_Generic_Association (Loc,
1363 Selector_Name => New_Occurrence_Of (Id, Loc),
1364 Explicit_Generic_Actual_Parameter => Empty);
1365 Set_Box_Present (Default);
1366 Append (Default, Default_Formals);
1368 end Process_Default;
1370 ---------------------------------
1371 -- Renames_Standard_Subprogram --
1372 ---------------------------------
1374 function Renames_Standard_Subprogram (Subp : Entity_Id) return Boolean is
1379 while Present (Id) loop
1380 if Scope (Id) = Standard_Standard then
1388 end Renames_Standard_Subprogram;
1390 -------------------------
1391 -- Set_Analyzed_Formal --
1392 -------------------------
1394 procedure Set_Analyzed_Formal is
1398 while Present (Analyzed_Formal) loop
1399 Kind := Nkind (Analyzed_Formal);
1401 case Nkind (Formal) is
1403 when N_Formal_Subprogram_Declaration =>
1404 exit when Kind in N_Formal_Subprogram_Declaration
1407 (Defining_Unit_Name (Specification (Formal))) =
1409 (Defining_Unit_Name (Specification (Analyzed_Formal)));
1411 when N_Formal_Package_Declaration =>
1412 exit when Nkind_In (Kind, N_Formal_Package_Declaration,
1413 N_Generic_Package_Declaration,
1414 N_Package_Declaration);
1416 when N_Use_Package_Clause | N_Use_Type_Clause => exit;
1420 -- Skip freeze nodes, and nodes inserted to replace
1421 -- unrecognized pragmas.
1424 Kind not in N_Formal_Subprogram_Declaration
1425 and then not Nkind_In (Kind, N_Subprogram_Declaration,
1429 and then Chars (Defining_Identifier (Formal)) =
1430 Chars (Defining_Identifier (Analyzed_Formal));
1433 Next (Analyzed_Formal);
1435 end Set_Analyzed_Formal;
1437 -- Start of processing for Analyze_Associations
1440 Actuals := Generic_Associations (I_Node);
1442 if Present (Actuals) then
1444 -- Check for an Others choice, indicating a partial parameterization
1445 -- for a formal package.
1447 Actual := First (Actuals);
1448 while Present (Actual) loop
1449 if Nkind (Actual) = N_Others_Choice then
1450 Others_Present := True;
1451 Others_Choice := Actual;
1453 if Present (Next (Actual)) then
1454 Error_Msg_N ("others must be last association", Actual);
1457 -- This subprogram is used both for formal packages and for
1458 -- instantiations. For the latter, associations must all be
1461 if Nkind (I_Node) /= N_Formal_Package_Declaration
1462 and then Comes_From_Source (I_Node)
1465 ("others association not allowed in an instance",
1469 -- In any case, nothing to do after the others association
1473 elsif Box_Present (Actual)
1474 and then Comes_From_Source (I_Node)
1475 and then Nkind (I_Node) /= N_Formal_Package_Declaration
1478 ("box association not allowed in an instance", Actual);
1484 -- If named associations are present, save first named association
1485 -- (it may of course be Empty) to facilitate subsequent name search.
1487 First_Named := First (Actuals);
1488 while Present (First_Named)
1489 and then Nkind (First_Named) /= N_Others_Choice
1490 and then No (Selector_Name (First_Named))
1492 Num_Actuals := Num_Actuals + 1;
1497 Named := First_Named;
1498 while Present (Named) loop
1499 if Nkind (Named) /= N_Others_Choice
1500 and then No (Selector_Name (Named))
1502 Error_Msg_N ("invalid positional actual after named one", Named);
1503 Abandon_Instantiation (Named);
1506 -- A named association may lack an actual parameter, if it was
1507 -- introduced for a default subprogram that turns out to be local
1508 -- to the outer instantiation.
1510 if Nkind (Named) /= N_Others_Choice
1511 and then Present (Explicit_Generic_Actual_Parameter (Named))
1513 Num_Actuals := Num_Actuals + 1;
1519 if Present (Formals) then
1520 Formal := First_Non_Pragma (Formals);
1521 Analyzed_Formal := First_Non_Pragma (F_Copy);
1523 if Present (Actuals) then
1524 Actual := First (Actuals);
1526 -- All formals should have default values
1532 while Present (Formal) loop
1533 Set_Analyzed_Formal;
1534 Saved_Formal := Next_Non_Pragma (Formal);
1536 case Nkind (Formal) is
1537 when N_Formal_Object_Declaration =>
1540 Defining_Identifier (Formal),
1541 Defining_Identifier (Analyzed_Formal));
1543 if No (Match) and then Partial_Parameterization then
1544 Process_Default (Formal);
1547 (Instantiate_Object (Formal, Match, Analyzed_Formal),
1551 when N_Formal_Type_Declaration =>
1554 Defining_Identifier (Formal),
1555 Defining_Identifier (Analyzed_Formal));
1558 if Partial_Parameterization then
1559 Process_Default (Formal);
1562 Error_Msg_Sloc := Sloc (Gen_Unit);
1566 Defining_Identifier (Formal));
1567 Error_Msg_NE ("\in instantiation of & declared#",
1568 Instantiation_Node, Gen_Unit);
1569 Abandon_Instantiation (Instantiation_Node);
1576 (Formal, Match, Analyzed_Formal, Assoc),
1579 -- An instantiation is a freeze point for the actuals,
1580 -- unless this is a rewritten formal package, or the
1581 -- formal is an Ada 2012 formal incomplete type.
1583 if Nkind (I_Node) = N_Formal_Package_Declaration
1585 (Ada_Version >= Ada_2012
1587 Ekind (Defining_Identifier (Analyzed_Formal)) =
1593 Append_Elmt (Entity (Match), Actuals_To_Freeze);
1597 -- A remote access-to-class-wide type is not a legal actual
1598 -- for a generic formal of an access type (E.2.2(17/2)).
1599 -- In GNAT an exception to this rule is introduced when
1600 -- the formal is marked as remote using implementation
1601 -- defined aspect/pragma Remote_Access_Type. In that case
1602 -- the actual must be remote as well.
1604 -- If the current instantiation is the construction of a
1605 -- local copy for a formal package the actuals may be
1606 -- defaulted, and there is no matching actual to check.
1608 if Nkind (Analyzed_Formal) = N_Formal_Type_Declaration
1610 Nkind (Formal_Type_Definition (Analyzed_Formal)) =
1611 N_Access_To_Object_Definition
1612 and then Present (Match)
1615 Formal_Ent : constant Entity_Id :=
1616 Defining_Identifier (Analyzed_Formal);
1618 if Is_Remote_Access_To_Class_Wide_Type (Entity (Match))
1619 = Is_Remote_Types (Formal_Ent)
1621 -- Remoteness of formal and actual match
1625 elsif Is_Remote_Types (Formal_Ent) then
1627 -- Remote formal, non-remote actual
1630 ("actual for& must be remote", Match, Formal_Ent);
1633 -- Non-remote formal, remote actual
1636 ("actual for& may not be remote",
1642 when N_Formal_Subprogram_Declaration =>
1645 (Defining_Unit_Name (Specification (Formal)),
1646 Defining_Unit_Name (Specification (Analyzed_Formal)));
1648 -- If the formal subprogram has the same name as another
1649 -- formal subprogram of the generic, then a named
1650 -- association is illegal (12.3(9)). Exclude named
1651 -- associations that are generated for a nested instance.
1654 and then Is_Named_Assoc
1655 and then Comes_From_Source (Found_Assoc)
1657 Check_Overloaded_Formal_Subprogram (Formal);
1660 -- If there is no corresponding actual, this may be case
1661 -- of partial parameterization, or else the formal has a
1662 -- default or a box.
1664 if No (Match) and then Partial_Parameterization then
1665 Process_Default (Formal);
1667 if Nkind (I_Node) = N_Formal_Package_Declaration then
1668 Check_Overloaded_Formal_Subprogram (Formal);
1675 (Containing_Package_With_Ext_Axioms
1676 (Defining_Entity (Analyzed_Formal)))
1677 and then Ekind (Defining_Entity (Analyzed_Formal)) =
1680 -- If actual is an entity (function or operator),
1681 -- build wrapper for it.
1683 if Present (Match) then
1684 if Nkind (Match) = N_Operator_Symbol then
1686 -- If the name is a default, find its visible
1687 -- entity at the point of instantiation.
1689 if Is_Entity_Name (Match)
1690 and then No (Entity (Match))
1692 Find_Direct_Name (Match);
1698 (Defining_Entity (Analyzed_Formal), Match));
1702 Instantiate_Formal_Subprogram
1703 (Formal, Match, Analyzed_Formal));
1706 -- Ditto if formal is an operator with a default.
1708 elsif Box_Present (Formal)
1709 and then Nkind (Defining_Entity (Analyzed_Formal)) =
1710 N_Defining_Operator_Symbol
1714 (Defining_Entity (Analyzed_Formal)));
1716 -- Otherwise create renaming declaration.
1720 Instantiate_Formal_Subprogram
1721 (Formal, Match, Analyzed_Formal));
1726 Instantiate_Formal_Subprogram
1727 (Formal, Match, Analyzed_Formal));
1730 -- An instantiation is a freeze point for the actuals,
1731 -- unless this is a rewritten formal package.
1733 if Nkind (I_Node) /= N_Formal_Package_Declaration
1734 and then Nkind (Match) = N_Identifier
1735 and then Is_Subprogram (Entity (Match))
1737 -- The actual subprogram may rename a routine defined
1738 -- in Standard. Avoid freezing such renamings because
1739 -- subprograms coming from Standard cannot be frozen.
1742 not Renames_Standard_Subprogram (Entity (Match))
1744 -- If the actual subprogram comes from a different
1745 -- unit, it is already frozen, either by a body in
1746 -- that unit or by the end of the declarative part
1747 -- of the unit. This check avoids the freezing of
1748 -- subprograms defined in Standard which are used
1749 -- as generic actuals.
1751 and then In_Same_Code_Unit (Entity (Match), I_Node)
1752 and then Has_Fully_Defined_Profile (Entity (Match))
1754 -- Mark the subprogram as having a delayed freeze
1755 -- since this may be an out-of-order action.
1757 Set_Has_Delayed_Freeze (Entity (Match));
1758 Append_Elmt (Entity (Match), Actuals_To_Freeze);
1762 -- If this is a nested generic, preserve default for later
1765 if No (Match) and then Box_Present (Formal) then
1767 (Defining_Unit_Name (Specification (Last (Assoc))),
1771 when N_Formal_Package_Declaration =>
1774 Defining_Identifier (Formal),
1775 Defining_Identifier (Original_Node (Analyzed_Formal)));
1778 if Partial_Parameterization then
1779 Process_Default (Formal);
1782 Error_Msg_Sloc := Sloc (Gen_Unit);
1785 Instantiation_Node, Defining_Identifier (Formal));
1786 Error_Msg_NE ("\in instantiation of & declared#",
1787 Instantiation_Node, Gen_Unit);
1789 Abandon_Instantiation (Instantiation_Node);
1795 (Instantiate_Formal_Package
1796 (Formal, Match, Analyzed_Formal),
1800 -- For use type and use package appearing in the generic part,
1801 -- we have already copied them, so we can just move them where
1802 -- they belong (we mustn't recopy them since this would mess up
1803 -- the Sloc values).
1805 when N_Use_Package_Clause |
1806 N_Use_Type_Clause =>
1807 if Nkind (Original_Node (I_Node)) =
1808 N_Formal_Package_Declaration
1810 Append (New_Copy_Tree (Formal), Assoc);
1813 Append (Formal, Assoc);
1817 raise Program_Error;
1821 Formal := Saved_Formal;
1822 Next_Non_Pragma (Analyzed_Formal);
1825 if Num_Actuals > Num_Matched then
1826 Error_Msg_Sloc := Sloc (Gen_Unit);
1828 if Present (Selector_Name (Actual)) then
1830 ("unmatched actual&",
1831 Actual, Selector_Name (Actual));
1832 Error_Msg_NE ("\in instantiation of& declared#",
1836 ("unmatched actual in instantiation of& declared#",
1841 elsif Present (Actuals) then
1843 ("too many actuals in generic instantiation", Instantiation_Node);
1846 -- An instantiation freezes all generic actuals. The only exceptions
1847 -- to this are incomplete types and subprograms which are not fully
1848 -- defined at the point of instantiation.
1851 Elmt : Elmt_Id := First_Elmt (Actuals_To_Freeze);
1853 while Present (Elmt) loop
1854 Freeze_Before (I_Node, Node (Elmt));
1859 -- If there are default subprograms, normalize the tree by adding
1860 -- explicit associations for them. This is required if the instance
1861 -- appears within a generic.
1869 Elmt := First_Elmt (Default_Actuals);
1870 while Present (Elmt) loop
1871 if No (Actuals) then
1872 Actuals := New_List;
1873 Set_Generic_Associations (I_Node, Actuals);
1876 Subp := Node (Elmt);
1878 Make_Generic_Association (Sloc (Subp),
1879 Selector_Name => New_Occurrence_Of (Subp, Sloc (Subp)),
1880 Explicit_Generic_Actual_Parameter =>
1881 New_Occurrence_Of (Subp, Sloc (Subp)));
1882 Mark_Rewrite_Insertion (New_D);
1883 Append_To (Actuals, New_D);
1888 -- If this is a formal package, normalize the parameter list by adding
1889 -- explicit box associations for the formals that are covered by an
1892 if not Is_Empty_List (Default_Formals) then
1893 Append_List (Default_Formals, Formals);
1897 end Analyze_Associations;
1899 -------------------------------
1900 -- Analyze_Formal_Array_Type --
1901 -------------------------------
1903 procedure Analyze_Formal_Array_Type
1904 (T : in out Entity_Id;
1910 -- Treated like a non-generic array declaration, with additional
1915 if Nkind (Def) = N_Constrained_Array_Definition then
1916 DSS := First (Discrete_Subtype_Definitions (Def));
1917 while Present (DSS) loop
1918 if Nkind_In (DSS, N_Subtype_Indication,
1920 N_Attribute_Reference)
1922 Error_Msg_N ("only a subtype mark is allowed in a formal", DSS);
1929 Array_Type_Declaration (T, Def);
1930 Set_Is_Generic_Type (Base_Type (T));
1932 if Ekind (Component_Type (T)) = E_Incomplete_Type
1933 and then No (Full_View (Component_Type (T)))
1935 Error_Msg_N ("premature usage of incomplete type", Def);
1937 -- Check that range constraint is not allowed on the component type
1938 -- of a generic formal array type (AARM 12.5.3(3))
1940 elsif Is_Internal (Component_Type (T))
1941 and then Present (Subtype_Indication (Component_Definition (Def)))
1942 and then Nkind (Original_Node
1943 (Subtype_Indication (Component_Definition (Def)))) =
1944 N_Subtype_Indication
1947 ("in a formal, a subtype indication can only be "
1948 & "a subtype mark (RM 12.5.3(3))",
1949 Subtype_Indication (Component_Definition (Def)));
1952 end Analyze_Formal_Array_Type;
1954 ---------------------------------------------
1955 -- Analyze_Formal_Decimal_Fixed_Point_Type --
1956 ---------------------------------------------
1958 -- As for other generic types, we create a valid type representation with
1959 -- legal but arbitrary attributes, whose values are never considered
1960 -- static. For all scalar types we introduce an anonymous base type, with
1961 -- the same attributes. We choose the corresponding integer type to be
1962 -- Standard_Integer.
1963 -- Here and in other similar routines, the Sloc of the generated internal
1964 -- type must be the same as the sloc of the defining identifier of the
1965 -- formal type declaration, to provide proper source navigation.
1967 procedure Analyze_Formal_Decimal_Fixed_Point_Type
1971 Loc : constant Source_Ptr := Sloc (Def);
1973 Base : constant Entity_Id :=
1975 (E_Decimal_Fixed_Point_Type,
1977 Sloc (Defining_Identifier (Parent (Def))), 'G');
1979 Int_Base : constant Entity_Id := Standard_Integer;
1980 Delta_Val : constant Ureal := Ureal_1;
1981 Digs_Val : constant Uint := Uint_6;
1983 function Make_Dummy_Bound return Node_Id;
1984 -- Return a properly typed universal real literal to use as a bound
1986 ----------------------
1987 -- Make_Dummy_Bound --
1988 ----------------------
1990 function Make_Dummy_Bound return Node_Id is
1991 Bound : constant Node_Id := Make_Real_Literal (Loc, Ureal_1);
1993 Set_Etype (Bound, Universal_Real);
1995 end Make_Dummy_Bound;
1997 -- Start of processing for Analyze_Formal_Decimal_Fixed_Point_Type
2002 Set_Etype (Base, Base);
2003 Set_Size_Info (Base, Int_Base);
2004 Set_RM_Size (Base, RM_Size (Int_Base));
2005 Set_First_Rep_Item (Base, First_Rep_Item (Int_Base));
2006 Set_Digits_Value (Base, Digs_Val);
2007 Set_Delta_Value (Base, Delta_Val);
2008 Set_Small_Value (Base, Delta_Val);
2009 Set_Scalar_Range (Base,
2011 Low_Bound => Make_Dummy_Bound,
2012 High_Bound => Make_Dummy_Bound));
2014 Set_Is_Generic_Type (Base);
2015 Set_Parent (Base, Parent (Def));
2017 Set_Ekind (T, E_Decimal_Fixed_Point_Subtype);
2018 Set_Etype (T, Base);
2019 Set_Size_Info (T, Int_Base);
2020 Set_RM_Size (T, RM_Size (Int_Base));
2021 Set_First_Rep_Item (T, First_Rep_Item (Int_Base));
2022 Set_Digits_Value (T, Digs_Val);
2023 Set_Delta_Value (T, Delta_Val);
2024 Set_Small_Value (T, Delta_Val);
2025 Set_Scalar_Range (T, Scalar_Range (Base));
2026 Set_Is_Constrained (T);
2028 Check_Restriction (No_Fixed_Point, Def);
2029 end Analyze_Formal_Decimal_Fixed_Point_Type;
2031 -------------------------------------------
2032 -- Analyze_Formal_Derived_Interface_Type --
2033 -------------------------------------------
2035 procedure Analyze_Formal_Derived_Interface_Type
2040 Loc : constant Source_Ptr := Sloc (Def);
2043 -- Rewrite as a type declaration of a derived type. This ensures that
2044 -- the interface list and primitive operations are properly captured.
2047 Make_Full_Type_Declaration (Loc,
2048 Defining_Identifier => T,
2049 Type_Definition => Def));
2051 Set_Is_Generic_Type (T);
2052 end Analyze_Formal_Derived_Interface_Type;
2054 ---------------------------------
2055 -- Analyze_Formal_Derived_Type --
2056 ---------------------------------
2058 procedure Analyze_Formal_Derived_Type
2063 Loc : constant Source_Ptr := Sloc (Def);
2064 Unk_Disc : constant Boolean := Unknown_Discriminants_Present (N);
2068 Set_Is_Generic_Type (T);
2070 if Private_Present (Def) then
2072 Make_Private_Extension_Declaration (Loc,
2073 Defining_Identifier => T,
2074 Discriminant_Specifications => Discriminant_Specifications (N),
2075 Unknown_Discriminants_Present => Unk_Disc,
2076 Subtype_Indication => Subtype_Mark (Def),
2077 Interface_List => Interface_List (Def));
2079 Set_Abstract_Present (New_N, Abstract_Present (Def));
2080 Set_Limited_Present (New_N, Limited_Present (Def));
2081 Set_Synchronized_Present (New_N, Synchronized_Present (Def));
2085 Make_Full_Type_Declaration (Loc,
2086 Defining_Identifier => T,
2087 Discriminant_Specifications =>
2088 Discriminant_Specifications (Parent (T)),
2090 Make_Derived_Type_Definition (Loc,
2091 Subtype_Indication => Subtype_Mark (Def)));
2093 Set_Abstract_Present
2094 (Type_Definition (New_N), Abstract_Present (Def));
2096 (Type_Definition (New_N), Limited_Present (Def));
2103 if not Is_Composite_Type (T) then
2105 ("unknown discriminants not allowed for elementary types", N);
2107 Set_Has_Unknown_Discriminants (T);
2108 Set_Is_Constrained (T, False);
2112 -- If the parent type has a known size, so does the formal, which makes
2113 -- legal representation clauses that involve the formal.
2115 Set_Size_Known_At_Compile_Time
2116 (T, Size_Known_At_Compile_Time (Entity (Subtype_Mark (Def))));
2117 end Analyze_Formal_Derived_Type;
2119 ----------------------------------
2120 -- Analyze_Formal_Discrete_Type --
2121 ----------------------------------
2123 -- The operations defined for a discrete types are those of an enumeration
2124 -- type. The size is set to an arbitrary value, for use in analyzing the
2127 procedure Analyze_Formal_Discrete_Type (T : Entity_Id; Def : Node_Id) is
2128 Loc : constant Source_Ptr := Sloc (Def);
2132 Base : constant Entity_Id :=
2134 (E_Floating_Point_Type, Current_Scope,
2135 Sloc (Defining_Identifier (Parent (Def))), 'G');
2139 Set_Ekind (T, E_Enumeration_Subtype);
2140 Set_Etype (T, Base);
2143 Set_Is_Generic_Type (T);
2144 Set_Is_Constrained (T);
2146 -- For semantic analysis, the bounds of the type must be set to some
2147 -- non-static value. The simplest is to create attribute nodes for those
2148 -- bounds, that refer to the type itself. These bounds are never
2149 -- analyzed but serve as place-holders.
2152 Make_Attribute_Reference (Loc,
2153 Attribute_Name => Name_First,
2154 Prefix => New_Occurrence_Of (T, Loc));
2158 Make_Attribute_Reference (Loc,
2159 Attribute_Name => Name_Last,
2160 Prefix => New_Occurrence_Of (T, Loc));
2163 Set_Scalar_Range (T,
2168 Set_Ekind (Base, E_Enumeration_Type);
2169 Set_Etype (Base, Base);
2170 Init_Size (Base, 8);
2171 Init_Alignment (Base);
2172 Set_Is_Generic_Type (Base);
2173 Set_Scalar_Range (Base, Scalar_Range (T));
2174 Set_Parent (Base, Parent (Def));
2175 end Analyze_Formal_Discrete_Type;
2177 ----------------------------------
2178 -- Analyze_Formal_Floating_Type --
2179 ---------------------------------
2181 procedure Analyze_Formal_Floating_Type (T : Entity_Id; Def : Node_Id) is
2182 Base : constant Entity_Id :=
2184 (E_Floating_Point_Type, Current_Scope,
2185 Sloc (Defining_Identifier (Parent (Def))), 'G');
2188 -- The various semantic attributes are taken from the predefined type
2189 -- Float, just so that all of them are initialized. Their values are
2190 -- never used because no constant folding or expansion takes place in
2191 -- the generic itself.
2194 Set_Ekind (T, E_Floating_Point_Subtype);
2195 Set_Etype (T, Base);
2196 Set_Size_Info (T, (Standard_Float));
2197 Set_RM_Size (T, RM_Size (Standard_Float));
2198 Set_Digits_Value (T, Digits_Value (Standard_Float));
2199 Set_Scalar_Range (T, Scalar_Range (Standard_Float));
2200 Set_Is_Constrained (T);
2202 Set_Is_Generic_Type (Base);
2203 Set_Etype (Base, Base);
2204 Set_Size_Info (Base, (Standard_Float));
2205 Set_RM_Size (Base, RM_Size (Standard_Float));
2206 Set_Digits_Value (Base, Digits_Value (Standard_Float));
2207 Set_Scalar_Range (Base, Scalar_Range (Standard_Float));
2208 Set_Parent (Base, Parent (Def));
2210 Check_Restriction (No_Floating_Point, Def);
2211 end Analyze_Formal_Floating_Type;
2213 -----------------------------------
2214 -- Analyze_Formal_Interface_Type;--
2215 -----------------------------------
2217 procedure Analyze_Formal_Interface_Type
2222 Loc : constant Source_Ptr := Sloc (N);
2227 Make_Full_Type_Declaration (Loc,
2228 Defining_Identifier => T,
2229 Type_Definition => Def);
2233 Set_Is_Generic_Type (T);
2234 end Analyze_Formal_Interface_Type;
2236 ---------------------------------
2237 -- Analyze_Formal_Modular_Type --
2238 ---------------------------------
2240 procedure Analyze_Formal_Modular_Type (T : Entity_Id; Def : Node_Id) is
2242 -- Apart from their entity kind, generic modular types are treated like
2243 -- signed integer types, and have the same attributes.
2245 Analyze_Formal_Signed_Integer_Type (T, Def);
2246 Set_Ekind (T, E_Modular_Integer_Subtype);
2247 Set_Ekind (Etype (T), E_Modular_Integer_Type);
2249 end Analyze_Formal_Modular_Type;
2251 ---------------------------------------
2252 -- Analyze_Formal_Object_Declaration --
2253 ---------------------------------------
2255 procedure Analyze_Formal_Object_Declaration (N : Node_Id) is
2256 E : constant Node_Id := Default_Expression (N);
2257 Id : constant Node_Id := Defining_Identifier (N);
2264 -- Determine the mode of the formal object
2266 if Out_Present (N) then
2267 K := E_Generic_In_Out_Parameter;
2269 if not In_Present (N) then
2270 Error_Msg_N ("formal generic objects cannot have mode OUT", N);
2274 K := E_Generic_In_Parameter;
2277 if Present (Subtype_Mark (N)) then
2278 Find_Type (Subtype_Mark (N));
2279 T := Entity (Subtype_Mark (N));
2281 -- Verify that there is no redundant null exclusion
2283 if Null_Exclusion_Present (N) then
2284 if not Is_Access_Type (T) then
2286 ("null exclusion can only apply to an access type", N);
2288 elsif Can_Never_Be_Null (T) then
2290 ("`NOT NULL` not allowed (& already excludes null)",
2295 -- Ada 2005 (AI-423): Formal object with an access definition
2298 Check_Access_Definition (N);
2299 T := Access_Definition
2301 N => Access_Definition (N));
2304 if Ekind (T) = E_Incomplete_Type then
2306 Error_Node : Node_Id;
2309 if Present (Subtype_Mark (N)) then
2310 Error_Node := Subtype_Mark (N);
2312 Check_Access_Definition (N);
2313 Error_Node := Access_Definition (N);
2316 Error_Msg_N ("premature usage of incomplete type", Error_Node);
2320 if K = E_Generic_In_Parameter then
2322 -- Ada 2005 (AI-287): Limited aggregates allowed in generic formals
2324 if Ada_Version < Ada_2005 and then Is_Limited_Type (T) then
2326 ("generic formal of mode IN must not be of limited type", N);
2327 Explain_Limited_Type (T, N);
2330 if Is_Abstract_Type (T) then
2332 ("generic formal of mode IN must not be of abstract type", N);
2336 Preanalyze_Spec_Expression (E, T);
2338 if Is_Limited_Type (T) and then not OK_For_Limited_Init (T, E) then
2340 ("initialization not allowed for limited types", E);
2341 Explain_Limited_Type (T, E);
2348 -- Case of generic IN OUT parameter
2351 -- If the formal has an unconstrained type, construct its actual
2352 -- subtype, as is done for subprogram formals. In this fashion, all
2353 -- its uses can refer to specific bounds.
2358 if (Is_Array_Type (T)
2359 and then not Is_Constrained (T))
2361 (Ekind (T) = E_Record_Type
2362 and then Has_Discriminants (T))
2365 Non_Freezing_Ref : constant Node_Id :=
2366 New_Occurrence_Of (Id, Sloc (Id));
2370 -- Make sure the actual subtype doesn't generate bogus freezing
2372 Set_Must_Not_Freeze (Non_Freezing_Ref);
2373 Decl := Build_Actual_Subtype (T, Non_Freezing_Ref);
2374 Insert_Before_And_Analyze (N, Decl);
2375 Set_Actual_Subtype (Id, Defining_Identifier (Decl));
2378 Set_Actual_Subtype (Id, T);
2383 ("initialization not allowed for `IN OUT` formals", N);
2387 if Has_Aspects (N) then
2388 Analyze_Aspect_Specifications (N, Id);
2390 end Analyze_Formal_Object_Declaration;
2392 ----------------------------------------------
2393 -- Analyze_Formal_Ordinary_Fixed_Point_Type --
2394 ----------------------------------------------
2396 procedure Analyze_Formal_Ordinary_Fixed_Point_Type
2400 Loc : constant Source_Ptr := Sloc (Def);
2401 Base : constant Entity_Id :=
2403 (E_Ordinary_Fixed_Point_Type, Current_Scope,
2404 Sloc (Defining_Identifier (Parent (Def))), 'G');
2407 -- The semantic attributes are set for completeness only, their values
2408 -- will never be used, since all properties of the type are non-static.
2411 Set_Ekind (T, E_Ordinary_Fixed_Point_Subtype);
2412 Set_Etype (T, Base);
2413 Set_Size_Info (T, Standard_Integer);
2414 Set_RM_Size (T, RM_Size (Standard_Integer));
2415 Set_Small_Value (T, Ureal_1);
2416 Set_Delta_Value (T, Ureal_1);
2417 Set_Scalar_Range (T,
2419 Low_Bound => Make_Real_Literal (Loc, Ureal_1),
2420 High_Bound => Make_Real_Literal (Loc, Ureal_1)));
2421 Set_Is_Constrained (T);
2423 Set_Is_Generic_Type (Base);
2424 Set_Etype (Base, Base);
2425 Set_Size_Info (Base, Standard_Integer);
2426 Set_RM_Size (Base, RM_Size (Standard_Integer));
2427 Set_Small_Value (Base, Ureal_1);
2428 Set_Delta_Value (Base, Ureal_1);
2429 Set_Scalar_Range (Base, Scalar_Range (T));
2430 Set_Parent (Base, Parent (Def));
2432 Check_Restriction (No_Fixed_Point, Def);
2433 end Analyze_Formal_Ordinary_Fixed_Point_Type;
2435 ----------------------------------------
2436 -- Analyze_Formal_Package_Declaration --
2437 ----------------------------------------
2439 procedure Analyze_Formal_Package_Declaration (N : Node_Id) is
2440 Loc : constant Source_Ptr := Sloc (N);
2441 Pack_Id : constant Entity_Id := Defining_Identifier (N);
2443 Gen_Id : constant Node_Id := Name (N);
2445 Gen_Unit : Entity_Id;
2447 Parent_Installed : Boolean := False;
2449 Parent_Instance : Entity_Id;
2450 Renaming_In_Par : Entity_Id;
2451 Associations : Boolean := True;
2453 Vis_Prims_List : Elist_Id := No_Elist;
2454 -- List of primitives made temporarily visible in the instantiation
2455 -- to match the visibility of the formal type
2457 function Build_Local_Package return Node_Id;
2458 -- The formal package is rewritten so that its parameters are replaced
2459 -- with corresponding declarations. For parameters with bona fide
2460 -- associations these declarations are created by Analyze_Associations
2461 -- as for a regular instantiation. For boxed parameters, we preserve
2462 -- the formal declarations and analyze them, in order to introduce
2463 -- entities of the right kind in the environment of the formal.
2465 -------------------------
2466 -- Build_Local_Package --
2467 -------------------------
2469 function Build_Local_Package return Node_Id is
2471 Pack_Decl : Node_Id;
2474 -- Within the formal, the name of the generic package is a renaming
2475 -- of the formal (as for a regular instantiation).
2478 Make_Package_Declaration (Loc,
2481 (Specification (Original_Node (Gen_Decl)),
2482 Empty, Instantiating => True));
2484 Renaming := Make_Package_Renaming_Declaration (Loc,
2485 Defining_Unit_Name =>
2486 Make_Defining_Identifier (Loc, Chars (Gen_Unit)),
2487 Name => New_Occurrence_Of (Formal, Loc));
2489 if Nkind (Gen_Id) = N_Identifier
2490 and then Chars (Gen_Id) = Chars (Pack_Id)
2493 ("& is hidden within declaration of instance", Gen_Id, Gen_Unit);
2496 -- If the formal is declared with a box, or with an others choice,
2497 -- create corresponding declarations for all entities in the formal
2498 -- part, so that names with the proper types are available in the
2499 -- specification of the formal package.
2501 -- On the other hand, if there are no associations, then all the
2502 -- formals must have defaults, and this will be checked by the
2503 -- call to Analyze_Associations.
2506 or else Nkind (First (Generic_Associations (N))) = N_Others_Choice
2509 Formal_Decl : Node_Id;
2512 -- TBA : for a formal package, need to recurse ???
2517 (Generic_Formal_Declarations (Original_Node (Gen_Decl)));
2518 while Present (Formal_Decl) loop
2520 (Decls, Copy_Generic_Node (Formal_Decl, Empty, True));
2525 -- If generic associations are present, use Analyze_Associations to
2526 -- create the proper renaming declarations.
2530 Act_Tree : constant Node_Id :=
2532 (Original_Node (Gen_Decl), Empty,
2533 Instantiating => True);
2536 Generic_Renamings.Set_Last (0);
2537 Generic_Renamings_HTable.Reset;
2538 Instantiation_Node := N;
2541 Analyze_Associations
2542 (I_Node => Original_Node (N),
2543 Formals => Generic_Formal_Declarations (Act_Tree),
2544 F_Copy => Generic_Formal_Declarations (Gen_Decl));
2546 Vis_Prims_List := Check_Hidden_Primitives (Decls);
2550 Append (Renaming, To => Decls);
2552 -- Add generated declarations ahead of local declarations in
2555 if No (Visible_Declarations (Specification (Pack_Decl))) then
2556 Set_Visible_Declarations (Specification (Pack_Decl), Decls);
2559 (First (Visible_Declarations (Specification (Pack_Decl))),
2564 end Build_Local_Package;
2566 -- Start of processing for Analyze_Formal_Package_Declaration
2569 Check_Text_IO_Special_Unit (Gen_Id);
2572 Check_Generic_Child_Unit (Gen_Id, Parent_Installed);
2573 Gen_Unit := Entity (Gen_Id);
2575 -- Check for a formal package that is a package renaming
2577 if Present (Renamed_Object (Gen_Unit)) then
2579 -- Indicate that unit is used, before replacing it with renamed
2580 -- entity for use below.
2582 if In_Extended_Main_Source_Unit (N) then
2583 Set_Is_Instantiated (Gen_Unit);
2584 Generate_Reference (Gen_Unit, N);
2587 Gen_Unit := Renamed_Object (Gen_Unit);
2590 if Ekind (Gen_Unit) /= E_Generic_Package then
2591 Error_Msg_N ("expect generic package name", Gen_Id);
2595 elsif Gen_Unit = Current_Scope then
2597 ("generic package cannot be used as a formal package of itself",
2602 elsif In_Open_Scopes (Gen_Unit) then
2603 if Is_Compilation_Unit (Gen_Unit)
2604 and then Is_Child_Unit (Current_Scope)
2606 -- Special-case the error when the formal is a parent, and
2607 -- continue analysis to minimize cascaded errors.
2610 ("generic parent cannot be used as formal package "
2611 & "of a child unit",
2616 ("generic package cannot be used as a formal package "
2624 -- Check that name of formal package does not hide name of generic,
2625 -- or its leading prefix. This check must be done separately because
2626 -- the name of the generic has already been analyzed.
2629 Gen_Name : Entity_Id;
2633 while Nkind (Gen_Name) = N_Expanded_Name loop
2634 Gen_Name := Prefix (Gen_Name);
2637 if Chars (Gen_Name) = Chars (Pack_Id) then
2639 ("& is hidden within declaration of formal package",
2645 or else No (Generic_Associations (N))
2646 or else Nkind (First (Generic_Associations (N))) = N_Others_Choice
2648 Associations := False;
2651 -- If there are no generic associations, the generic parameters appear
2652 -- as local entities and are instantiated like them. We copy the generic
2653 -- package declaration as if it were an instantiation, and analyze it
2654 -- like a regular package, except that we treat the formals as
2655 -- additional visible components.
2657 Gen_Decl := Unit_Declaration_Node (Gen_Unit);
2659 if In_Extended_Main_Source_Unit (N) then
2660 Set_Is_Instantiated (Gen_Unit);
2661 Generate_Reference (Gen_Unit, N);
2664 Formal := New_Copy (Pack_Id);
2665 Create_Instantiation_Source (N, Gen_Unit, False, S_Adjustment);
2668 -- Make local generic without formals. The formals will be replaced
2669 -- with internal declarations.
2671 New_N := Build_Local_Package;
2673 -- If there are errors in the parameter list, Analyze_Associations
2674 -- raises Instantiation_Error. Patch the declaration to prevent
2675 -- further exception propagation.
2678 when Instantiation_Error =>
2680 Enter_Name (Formal);
2681 Set_Ekind (Formal, E_Variable);
2682 Set_Etype (Formal, Any_Type);
2683 Restore_Hidden_Primitives (Vis_Prims_List);
2685 if Parent_Installed then
2693 Set_Defining_Unit_Name (Specification (New_N), Formal);
2694 Set_Generic_Parent (Specification (N), Gen_Unit);
2695 Set_Instance_Env (Gen_Unit, Formal);
2696 Set_Is_Generic_Instance (Formal);
2698 Enter_Name (Formal);
2699 Set_Ekind (Formal, E_Package);
2700 Set_Etype (Formal, Standard_Void_Type);
2701 Set_Inner_Instances (Formal, New_Elmt_List);
2702 Push_Scope (Formal);
2704 if Is_Child_Unit (Gen_Unit)
2705 and then Parent_Installed
2707 -- Similarly, we have to make the name of the formal visible in the
2708 -- parent instance, to resolve properly fully qualified names that
2709 -- may appear in the generic unit. The parent instance has been
2710 -- placed on the scope stack ahead of the current scope.
2712 Parent_Instance := Scope_Stack.Table (Scope_Stack.Last - 1).Entity;
2715 Make_Defining_Identifier (Loc, Chars (Gen_Unit));
2716 Set_Ekind (Renaming_In_Par, E_Package);
2717 Set_Etype (Renaming_In_Par, Standard_Void_Type);
2718 Set_Scope (Renaming_In_Par, Parent_Instance);
2719 Set_Parent (Renaming_In_Par, Parent (Formal));
2720 Set_Renamed_Object (Renaming_In_Par, Formal);
2721 Append_Entity (Renaming_In_Par, Parent_Instance);
2724 Analyze (Specification (N));
2726 -- The formals for which associations are provided are not visible
2727 -- outside of the formal package. The others are still declared by a
2728 -- formal parameter declaration.
2730 -- If there are no associations, the only local entity to hide is the
2731 -- generated package renaming itself.
2737 E := First_Entity (Formal);
2738 while Present (E) loop
2740 and then not Is_Generic_Formal (E)
2745 if Ekind (E) = E_Package
2746 and then Renamed_Entity (E) = Formal
2756 End_Package_Scope (Formal);
2757 Restore_Hidden_Primitives (Vis_Prims_List);
2759 if Parent_Installed then
2765 -- Inside the generic unit, the formal package is a regular package, but
2766 -- no body is needed for it. Note that after instantiation, the defining
2767 -- unit name we need is in the new tree and not in the original (see
2768 -- Package_Instantiation). A generic formal package is an instance, and
2769 -- can be used as an actual for an inner instance.
2771 Set_Has_Completion (Formal, True);
2773 -- Add semantic information to the original defining identifier.
2776 Set_Ekind (Pack_Id, E_Package);
2777 Set_Etype (Pack_Id, Standard_Void_Type);
2778 Set_Scope (Pack_Id, Scope (Formal));
2779 Set_Has_Completion (Pack_Id, True);
2782 if Has_Aspects (N) then
2783 Analyze_Aspect_Specifications (N, Pack_Id);
2785 end Analyze_Formal_Package_Declaration;
2787 ---------------------------------
2788 -- Analyze_Formal_Private_Type --
2789 ---------------------------------
2791 procedure Analyze_Formal_Private_Type
2797 New_Private_Type (N, T, Def);
2799 -- Set the size to an arbitrary but legal value
2801 Set_Size_Info (T, Standard_Integer);
2802 Set_RM_Size (T, RM_Size (Standard_Integer));
2803 end Analyze_Formal_Private_Type;
2805 ------------------------------------
2806 -- Analyze_Formal_Incomplete_Type --
2807 ------------------------------------
2809 procedure Analyze_Formal_Incomplete_Type
2815 Set_Ekind (T, E_Incomplete_Type);
2817 Set_Private_Dependents (T, New_Elmt_List);
2819 if Tagged_Present (Def) then
2820 Set_Is_Tagged_Type (T);
2821 Make_Class_Wide_Type (T);
2822 Set_Direct_Primitive_Operations (T, New_Elmt_List);
2824 end Analyze_Formal_Incomplete_Type;
2826 ----------------------------------------
2827 -- Analyze_Formal_Signed_Integer_Type --
2828 ----------------------------------------
2830 procedure Analyze_Formal_Signed_Integer_Type
2834 Base : constant Entity_Id :=
2836 (E_Signed_Integer_Type,
2838 Sloc (Defining_Identifier (Parent (Def))), 'G');
2843 Set_Ekind (T, E_Signed_Integer_Subtype);
2844 Set_Etype (T, Base);
2845 Set_Size_Info (T, Standard_Integer);
2846 Set_RM_Size (T, RM_Size (Standard_Integer));
2847 Set_Scalar_Range (T, Scalar_Range (Standard_Integer));
2848 Set_Is_Constrained (T);
2850 Set_Is_Generic_Type (Base);
2851 Set_Size_Info (Base, Standard_Integer);
2852 Set_RM_Size (Base, RM_Size (Standard_Integer));
2853 Set_Etype (Base, Base);
2854 Set_Scalar_Range (Base, Scalar_Range (Standard_Integer));
2855 Set_Parent (Base, Parent (Def));
2856 end Analyze_Formal_Signed_Integer_Type;
2858 -------------------------------------------
2859 -- Analyze_Formal_Subprogram_Declaration --
2860 -------------------------------------------
2862 procedure Analyze_Formal_Subprogram_Declaration (N : Node_Id) is
2863 Spec : constant Node_Id := Specification (N);
2864 Def : constant Node_Id := Default_Name (N);
2865 Nam : constant Entity_Id := Defining_Unit_Name (Spec);
2873 if Nkind (Nam) = N_Defining_Program_Unit_Name then
2874 Error_Msg_N ("name of formal subprogram must be a direct name", Nam);
2878 Analyze_Subprogram_Declaration (N);
2879 Set_Is_Formal_Subprogram (Nam);
2880 Set_Has_Completion (Nam);
2882 if Nkind (N) = N_Formal_Abstract_Subprogram_Declaration then
2883 Set_Is_Abstract_Subprogram (Nam);
2884 Set_Is_Dispatching_Operation (Nam);
2887 Ctrl_Type : constant Entity_Id := Find_Dispatching_Type (Nam);
2889 if No (Ctrl_Type) then
2891 ("abstract formal subprogram must have a controlling type",
2894 elsif Ada_Version >= Ada_2012
2895 and then Is_Incomplete_Type (Ctrl_Type)
2898 ("controlling type of abstract formal subprogram cannot " &
2899 "be incomplete type", N, Ctrl_Type);
2902 Check_Controlling_Formals (Ctrl_Type, Nam);
2907 -- Default name is resolved at the point of instantiation
2909 if Box_Present (N) then
2912 -- Else default is bound at the point of generic declaration
2914 elsif Present (Def) then
2915 if Nkind (Def) = N_Operator_Symbol then
2916 Find_Direct_Name (Def);
2918 elsif Nkind (Def) /= N_Attribute_Reference then
2922 -- For an attribute reference, analyze the prefix and verify
2923 -- that it has the proper profile for the subprogram.
2925 Analyze (Prefix (Def));
2926 Valid_Default_Attribute (Nam, Def);
2930 -- Default name may be overloaded, in which case the interpretation
2931 -- with the correct profile must be selected, as for a renaming.
2932 -- If the definition is an indexed component, it must denote a
2933 -- member of an entry family. If it is a selected component, it
2934 -- can be a protected operation.
2936 if Etype (Def) = Any_Type then
2939 elsif Nkind (Def) = N_Selected_Component then
2940 if not Is_Overloadable (Entity (Selector_Name (Def))) then
2941 Error_Msg_N ("expect valid subprogram name as default", Def);
2944 elsif Nkind (Def) = N_Indexed_Component then
2945 if Is_Entity_Name (Prefix (Def)) then
2946 if Ekind (Entity (Prefix (Def))) /= E_Entry_Family then
2947 Error_Msg_N ("expect valid subprogram name as default", Def);
2950 elsif Nkind (Prefix (Def)) = N_Selected_Component then
2951 if Ekind (Entity (Selector_Name (Prefix (Def)))) /=
2954 Error_Msg_N ("expect valid subprogram name as default", Def);
2958 Error_Msg_N ("expect valid subprogram name as default", Def);
2962 elsif Nkind (Def) = N_Character_Literal then
2964 -- Needs some type checks: subprogram should be parameterless???
2966 Resolve (Def, (Etype (Nam)));
2968 elsif not Is_Entity_Name (Def)
2969 or else not Is_Overloadable (Entity (Def))
2971 Error_Msg_N ("expect valid subprogram name as default", Def);
2974 elsif not Is_Overloaded (Def) then
2975 Subp := Entity (Def);
2978 Error_Msg_N ("premature usage of formal subprogram", Def);
2980 elsif not Entity_Matches_Spec (Subp, Nam) then
2981 Error_Msg_N ("no visible entity matches specification", Def);
2984 -- More than one interpretation, so disambiguate as for a renaming
2989 I1 : Interp_Index := 0;
2995 Get_First_Interp (Def, I, It);
2996 while Present (It.Nam) loop
2997 if Entity_Matches_Spec (It.Nam, Nam) then
2998 if Subp /= Any_Id then
2999 It1 := Disambiguate (Def, I1, I, Etype (Subp));
3001 if It1 = No_Interp then
3002 Error_Msg_N ("ambiguous default subprogram", Def);
3015 Get_Next_Interp (I, It);
3019 if Subp /= Any_Id then
3021 -- Subprogram found, generate reference to it
3023 Set_Entity (Def, Subp);
3024 Generate_Reference (Subp, Def);
3027 Error_Msg_N ("premature usage of formal subprogram", Def);
3029 elsif Ekind (Subp) /= E_Operator then
3030 Check_Mode_Conformant (Subp, Nam);
3034 Error_Msg_N ("no visible subprogram matches specification", N);
3040 if Has_Aspects (N) then
3041 Analyze_Aspect_Specifications (N, Nam);
3044 end Analyze_Formal_Subprogram_Declaration;
3046 -------------------------------------
3047 -- Analyze_Formal_Type_Declaration --
3048 -------------------------------------
3050 procedure Analyze_Formal_Type_Declaration (N : Node_Id) is
3051 Def : constant Node_Id := Formal_Type_Definition (N);
3055 T := Defining_Identifier (N);
3057 if Present (Discriminant_Specifications (N))
3058 and then Nkind (Def) /= N_Formal_Private_Type_Definition
3061 ("discriminants not allowed for this formal type", T);
3064 -- Enter the new name, and branch to specific routine
3067 when N_Formal_Private_Type_Definition =>
3068 Analyze_Formal_Private_Type (N, T, Def);
3070 when N_Formal_Derived_Type_Definition =>
3071 Analyze_Formal_Derived_Type (N, T, Def);
3073 when N_Formal_Incomplete_Type_Definition =>
3074 Analyze_Formal_Incomplete_Type (T, Def);
3076 when N_Formal_Discrete_Type_Definition =>
3077 Analyze_Formal_Discrete_Type (T, Def);
3079 when N_Formal_Signed_Integer_Type_Definition =>
3080 Analyze_Formal_Signed_Integer_Type (T, Def);
3082 when N_Formal_Modular_Type_Definition =>
3083 Analyze_Formal_Modular_Type (T, Def);
3085 when N_Formal_Floating_Point_Definition =>
3086 Analyze_Formal_Floating_Type (T, Def);
3088 when N_Formal_Ordinary_Fixed_Point_Definition =>
3089 Analyze_Formal_Ordinary_Fixed_Point_Type (T, Def);
3091 when N_Formal_Decimal_Fixed_Point_Definition =>
3092 Analyze_Formal_Decimal_Fixed_Point_Type (T, Def);
3094 when N_Array_Type_Definition =>
3095 Analyze_Formal_Array_Type (T, Def);
3097 when N_Access_To_Object_Definition |
3098 N_Access_Function_Definition |
3099 N_Access_Procedure_Definition =>
3100 Analyze_Generic_Access_Type (T, Def);
3102 -- Ada 2005: a interface declaration is encoded as an abstract
3103 -- record declaration or a abstract type derivation.
3105 when N_Record_Definition =>
3106 Analyze_Formal_Interface_Type (N, T, Def);
3108 when N_Derived_Type_Definition =>
3109 Analyze_Formal_Derived_Interface_Type (N, T, Def);
3115 raise Program_Error;
3119 Set_Is_Generic_Type (T);
3121 if Has_Aspects (N) then
3122 Analyze_Aspect_Specifications (N, T);
3124 end Analyze_Formal_Type_Declaration;
3126 ------------------------------------
3127 -- Analyze_Function_Instantiation --
3128 ------------------------------------
3130 procedure Analyze_Function_Instantiation (N : Node_Id) is
3132 Analyze_Subprogram_Instantiation (N, E_Function);
3133 end Analyze_Function_Instantiation;
3135 ---------------------------------
3136 -- Analyze_Generic_Access_Type --
3137 ---------------------------------
3139 procedure Analyze_Generic_Access_Type (T : Entity_Id; Def : Node_Id) is
3143 if Nkind (Def) = N_Access_To_Object_Definition then
3144 Access_Type_Declaration (T, Def);
3146 if Is_Incomplete_Or_Private_Type (Designated_Type (T))
3147 and then No (Full_View (Designated_Type (T)))
3148 and then not Is_Generic_Type (Designated_Type (T))
3150 Error_Msg_N ("premature usage of incomplete type", Def);
3152 elsif not Is_Entity_Name (Subtype_Indication (Def)) then
3154 ("only a subtype mark is allowed in a formal", Def);
3158 Access_Subprogram_Declaration (T, Def);
3160 end Analyze_Generic_Access_Type;
3162 ---------------------------------
3163 -- Analyze_Generic_Formal_Part --
3164 ---------------------------------
3166 procedure Analyze_Generic_Formal_Part (N : Node_Id) is
3167 Gen_Parm_Decl : Node_Id;
3170 -- The generic formals are processed in the scope of the generic unit,
3171 -- where they are immediately visible. The scope is installed by the
3174 Gen_Parm_Decl := First (Generic_Formal_Declarations (N));
3176 while Present (Gen_Parm_Decl) loop
3177 Analyze (Gen_Parm_Decl);
3178 Next (Gen_Parm_Decl);
3181 Generate_Reference_To_Generic_Formals (Current_Scope);
3182 end Analyze_Generic_Formal_Part;
3184 ------------------------------------------
3185 -- Analyze_Generic_Package_Declaration --
3186 ------------------------------------------
3188 procedure Analyze_Generic_Package_Declaration (N : Node_Id) is
3189 Loc : constant Source_Ptr := Sloc (N);
3192 Save_Parent : Node_Id;
3194 Decls : constant List_Id :=
3195 Visible_Declarations (Specification (N));
3199 Check_SPARK_05_Restriction ("generic is not allowed", N);
3201 -- We introduce a renaming of the enclosing package, to have a usable
3202 -- entity as the prefix of an expanded name for a local entity of the
3203 -- form Par.P.Q, where P is the generic package. This is because a local
3204 -- entity named P may hide it, so that the usual visibility rules in
3205 -- the instance will not resolve properly.
3208 Make_Package_Renaming_Declaration (Loc,
3209 Defining_Unit_Name =>
3210 Make_Defining_Identifier (Loc,
3211 Chars => New_External_Name (Chars (Defining_Entity (N)), "GH")),
3212 Name => Make_Identifier (Loc, Chars (Defining_Entity (N))));
3214 if Present (Decls) then
3215 Decl := First (Decls);
3216 while Present (Decl)
3217 and then Nkind (Decl) = N_Pragma
3222 if Present (Decl) then
3223 Insert_Before (Decl, Renaming);
3225 Append (Renaming, Visible_Declarations (Specification (N)));
3229 Set_Visible_Declarations (Specification (N), New_List (Renaming));
3232 -- Create copy of generic unit, and save for instantiation. If the unit
3233 -- is a child unit, do not copy the specifications for the parent, which
3234 -- are not part of the generic tree.
3236 Save_Parent := Parent_Spec (N);
3237 Set_Parent_Spec (N, Empty);
3239 New_N := Copy_Generic_Node (N, Empty, Instantiating => False);
3240 Set_Parent_Spec (New_N, Save_Parent);
3243 -- Once the contents of the generic copy and the template are swapped,
3244 -- do the same for their respective aspect specifications.
3246 Exchange_Aspects (N, New_N);
3247 Id := Defining_Entity (N);
3248 Generate_Definition (Id);
3250 -- Expansion is not applied to generic units
3255 Set_Ekind (Id, E_Generic_Package);
3256 Set_Etype (Id, Standard_Void_Type);
3257 Set_Contract (Id, Make_Contract (Sloc (Id)));
3259 -- Analyze aspects now, so that generated pragmas appear in the
3260 -- declarations before building and analyzing the generic copy.
3262 if Has_Aspects (N) then
3263 Analyze_Aspect_Specifications (N, Id);
3267 Enter_Generic_Scope (Id);
3268 Set_Inner_Instances (Id, New_Elmt_List);
3270 Set_Categorization_From_Pragmas (N);
3271 Set_Is_Pure (Id, Is_Pure (Current_Scope));
3273 -- Link the declaration of the generic homonym in the generic copy to
3274 -- the package it renames, so that it is always resolved properly.
3276 Set_Generic_Homonym (Id, Defining_Unit_Name (Renaming));
3277 Set_Entity (Associated_Node (Name (Renaming)), Id);
3279 -- For a library unit, we have reconstructed the entity for the unit,
3280 -- and must reset it in the library tables.
3282 if Nkind (Parent (N)) = N_Compilation_Unit then
3283 Set_Cunit_Entity (Current_Sem_Unit, Id);
3286 Analyze_Generic_Formal_Part (N);
3288 -- After processing the generic formals, analysis proceeds as for a
3289 -- non-generic package.
3291 Analyze (Specification (N));
3293 Validate_Categorization_Dependency (N, Id);
3297 End_Package_Scope (Id);
3298 Exit_Generic_Scope (Id);
3300 if Nkind (Parent (N)) /= N_Compilation_Unit then
3301 Move_Freeze_Nodes (Id, N, Visible_Declarations (Specification (N)));
3302 Move_Freeze_Nodes (Id, N, Private_Declarations (Specification (N)));
3303 Move_Freeze_Nodes (Id, N, Generic_Formal_Declarations (N));
3306 Set_Body_Required (Parent (N), Unit_Requires_Body (Id));
3307 Validate_RT_RAT_Component (N);
3309 -- If this is a spec without a body, check that generic parameters
3312 if not Body_Required (Parent (N)) then
3313 Check_References (Id);
3316 end Analyze_Generic_Package_Declaration;
3318 --------------------------------------------
3319 -- Analyze_Generic_Subprogram_Declaration --
3320 --------------------------------------------
3322 procedure Analyze_Generic_Subprogram_Declaration (N : Node_Id) is
3327 Result_Type : Entity_Id;
3328 Save_Parent : Node_Id;
3332 Check_SPARK_05_Restriction ("generic is not allowed", N);
3334 -- Create copy of generic unit, and save for instantiation. If the unit
3335 -- is a child unit, do not copy the specifications for the parent, which
3336 -- are not part of the generic tree.
3338 Save_Parent := Parent_Spec (N);
3339 Set_Parent_Spec (N, Empty);
3341 New_N := Copy_Generic_Node (N, Empty, Instantiating => False);
3342 Set_Parent_Spec (New_N, Save_Parent);
3345 -- Once the contents of the generic copy and the template are swapped,
3346 -- do the same for their respective aspect specifications.
3348 Exchange_Aspects (N, New_N);
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);
3363 Set_Scope_Depth_Value (Id, Scope_Depth (Current_Scope) + 1);
3365 -- Analyze the aspects of the generic copy to ensure that all generated
3366 -- pragmas (if any) perform their semantic effects.
3368 if Has_Aspects (N) then
3369 Analyze_Aspect_Specifications (N, Id);
3373 Enter_Generic_Scope (Id);
3374 Set_Inner_Instances (Id, New_Elmt_List);
3375 Set_Is_Pure (Id, Is_Pure (Current_Scope));
3377 Analyze_Generic_Formal_Part (N);
3379 Formals := Parameter_Specifications (Spec);
3381 if Present (Formals) then
3382 Process_Formals (Formals, Spec);
3385 if Nkind (Spec) = N_Function_Specification then
3386 Set_Ekind (Id, E_Generic_Function);
3388 if Nkind (Result_Definition (Spec)) = N_Access_Definition then
3389 Result_Type := Access_Definition (Spec, Result_Definition (Spec));
3390 Set_Etype (Id, Result_Type);
3392 -- Check restriction imposed by AI05-073: a generic function
3393 -- cannot return an abstract type or an access to such.
3395 -- This is a binding interpretation should it apply to earlier
3396 -- versions of Ada as well as Ada 2012???
3398 if Is_Abstract_Type (Designated_Type (Result_Type))
3399 and then Ada_Version >= Ada_2012
3401 Error_Msg_N ("generic function cannot have an access result"
3402 & " that designates an abstract type", Spec);
3406 Find_Type (Result_Definition (Spec));
3407 Typ := Entity (Result_Definition (Spec));
3409 if Is_Abstract_Type (Typ)
3410 and then Ada_Version >= Ada_2012
3413 ("generic function cannot have abstract result type", Spec);
3416 -- If a null exclusion is imposed on the result type, then create
3417 -- a null-excluding itype (an access subtype) and use it as the
3418 -- function's Etype.
3420 if Is_Access_Type (Typ)
3421 and then Null_Exclusion_Present (Spec)
3424 Create_Null_Excluding_Itype
3426 Related_Nod => Spec,
3427 Scope_Id => Defining_Unit_Name (Spec)));
3429 Set_Etype (Id, Typ);
3434 Set_Ekind (Id, E_Generic_Procedure);
3435 Set_Etype (Id, Standard_Void_Type);
3438 -- For a library unit, we have reconstructed the entity for the unit,
3439 -- and must reset it in the library tables. We also make sure that
3440 -- Body_Required is set properly in the original compilation unit node.
3442 if Nkind (Parent (N)) = N_Compilation_Unit then
3443 Set_Cunit_Entity (Current_Sem_Unit, Id);
3444 Set_Body_Required (Parent (N), Unit_Requires_Body (Id));
3447 Set_Categorization_From_Pragmas (N);
3448 Validate_Categorization_Dependency (N, Id);
3450 Save_Global_References (Original_Node (N));
3452 -- For ASIS purposes, convert any postcondition, precondition pragmas
3453 -- into aspects, if N is not a compilation unit by itself, in order to
3454 -- enable the analysis of expressions inside the corresponding PPC
3457 if ASIS_Mode and then Is_List_Member (N) then
3458 Make_Aspect_For_PPC_In_Gen_Sub_Decl (N);
3463 Exit_Generic_Scope (Id);
3464 Generate_Reference_To_Formals (Id);
3466 List_Inherited_Pre_Post_Aspects (Id);
3467 end Analyze_Generic_Subprogram_Declaration;
3469 -----------------------------------
3470 -- Analyze_Package_Instantiation --
3471 -----------------------------------
3473 procedure Analyze_Package_Instantiation (N : Node_Id) is
3474 Loc : constant Source_Ptr := Sloc (N);
3475 Gen_Id : constant Node_Id := Name (N);
3478 Act_Decl_Name : Node_Id;
3479 Act_Decl_Id : Entity_Id;
3484 Gen_Unit : Entity_Id;
3486 Is_Actual_Pack : constant Boolean :=
3487 Is_Internal (Defining_Entity (N));
3489 Env_Installed : Boolean := False;
3490 Parent_Installed : Boolean := False;
3491 Renaming_List : List_Id;
3492 Unit_Renaming : Node_Id;
3493 Needs_Body : Boolean;
3494 Inline_Now : Boolean := False;
3496 Save_IPSM : constant Boolean := Ignore_Pragma_SPARK_Mode;
3497 -- Save flag Ignore_Pragma_SPARK_Mode for restore on exit
3499 Save_SM : constant SPARK_Mode_Type := SPARK_Mode;
3500 Save_SMP : constant Node_Id := SPARK_Mode_Pragma;
3501 -- Save the SPARK_Mode-related data for restore on exit
3503 Save_Style_Check : constant Boolean := Style_Check;
3504 -- Save style check mode for restore on exit
3506 procedure Delay_Descriptors (E : Entity_Id);
3507 -- Delay generation of subprogram descriptors for given entity
3509 function Might_Inline_Subp return Boolean;
3510 -- If inlining is active and the generic contains inlined subprograms,
3511 -- we instantiate the body. This may cause superfluous instantiations,
3512 -- but it is simpler than detecting the need for the body at the point
3513 -- of inlining, when the context of the instance is not available.
3515 function Must_Inline_Subp return Boolean;
3516 -- If inlining is active and the generic contains inlined subprograms,
3517 -- return True if some of the inlined subprograms must be inlined by
3520 -----------------------
3521 -- Delay_Descriptors --
3522 -----------------------
3524 procedure Delay_Descriptors (E : Entity_Id) is
3526 if not Delay_Subprogram_Descriptors (E) then
3527 Set_Delay_Subprogram_Descriptors (E);
3528 Pending_Descriptor.Append (E);
3530 end Delay_Descriptors;
3532 -----------------------
3533 -- Might_Inline_Subp --
3534 -----------------------
3536 function Might_Inline_Subp return Boolean is
3540 if not Inline_Processing_Required then
3544 E := First_Entity (Gen_Unit);
3545 while Present (E) loop
3546 if Is_Subprogram (E) and then Is_Inlined (E) then
3555 end Might_Inline_Subp;
3557 ----------------------
3558 -- Must_Inline_Subp --
3559 ----------------------
3561 function Must_Inline_Subp return Boolean is
3565 if not Inline_Processing_Required then
3569 E := First_Entity (Gen_Unit);
3570 while Present (E) loop
3571 if Is_Subprogram (E)
3572 and then Is_Inlined (E)
3573 and then Must_Inline (E)
3583 end Must_Inline_Subp;
3585 -- Local declarations
3587 Vis_Prims_List : Elist_Id := No_Elist;
3588 -- List of primitives made temporarily visible in the instantiation
3589 -- to match the visibility of the formal type
3591 -- Start of processing for Analyze_Package_Instantiation
3594 Check_SPARK_05_Restriction ("generic is not allowed", N);
3596 -- Very first thing: check for Text_IO sp[ecial unit in case we are
3597 -- instantiating one of the children of [[Wide_]Wide_]Text_IO.
3599 Check_Text_IO_Special_Unit (Name (N));
3601 -- Make node global for error reporting
3603 Instantiation_Node := N;
3605 -- Turn off style checking in instances. If the check is enabled on the
3606 -- generic unit, a warning in an instance would just be noise. If not
3607 -- enabled on the generic, then a warning in an instance is just wrong.
3609 Style_Check := False;
3611 -- Case of instantiation of a generic package
3613 if Nkind (N) = N_Package_Instantiation then
3614 Act_Decl_Id := New_Copy (Defining_Entity (N));
3615 Set_Comes_From_Source (Act_Decl_Id, True);
3617 if Nkind (Defining_Unit_Name (N)) = N_Defining_Program_Unit_Name then
3619 Make_Defining_Program_Unit_Name (Loc,
3620 Name => New_Copy_Tree (Name (Defining_Unit_Name (N))),
3621 Defining_Identifier => Act_Decl_Id);
3623 Act_Decl_Name := Act_Decl_Id;
3626 -- Case of instantiation of a formal package
3629 Act_Decl_Id := Defining_Identifier (N);
3630 Act_Decl_Name := Act_Decl_Id;
3633 Generate_Definition (Act_Decl_Id);
3634 Preanalyze_Actuals (N);
3637 Env_Installed := True;
3639 -- Reset renaming map for formal types. The mapping is established
3640 -- when analyzing the generic associations, but some mappings are
3641 -- inherited from formal packages of parent units, and these are
3642 -- constructed when the parents are installed.
3644 Generic_Renamings.Set_Last (0);
3645 Generic_Renamings_HTable.Reset;
3647 Check_Generic_Child_Unit (Gen_Id, Parent_Installed);
3648 Gen_Unit := Entity (Gen_Id);
3650 -- Verify that it is the name of a generic package
3652 -- A visibility glitch: if the instance is a child unit and the generic
3653 -- is the generic unit of a parent instance (i.e. both the parent and
3654 -- the child units are instances of the same package) the name now
3655 -- denotes the renaming within the parent, not the intended generic
3656 -- unit. See if there is a homonym that is the desired generic. The
3657 -- renaming declaration must be visible inside the instance of the
3658 -- child, but not when analyzing the name in the instantiation itself.
3660 if Ekind (Gen_Unit) = E_Package
3661 and then Present (Renamed_Entity (Gen_Unit))
3662 and then In_Open_Scopes (Renamed_Entity (Gen_Unit))
3663 and then Is_Generic_Instance (Renamed_Entity (Gen_Unit))
3664 and then Present (Homonym (Gen_Unit))
3666 Gen_Unit := Homonym (Gen_Unit);
3669 if Etype (Gen_Unit) = Any_Type then
3673 elsif Ekind (Gen_Unit) /= E_Generic_Package then
3675 -- Ada 2005 (AI-50217): Cannot use instance in limited with_clause
3677 if From_Limited_With (Gen_Unit) then
3679 ("cannot instantiate a limited withed package", Gen_Id);
3682 ("& is not the name of a generic package", Gen_Id, Gen_Unit);
3689 if In_Extended_Main_Source_Unit (N) then
3690 Set_Is_Instantiated (Gen_Unit);
3691 Generate_Reference (Gen_Unit, N);
3693 if Present (Renamed_Object (Gen_Unit)) then
3694 Set_Is_Instantiated (Renamed_Object (Gen_Unit));
3695 Generate_Reference (Renamed_Object (Gen_Unit), N);
3699 if Nkind (Gen_Id) = N_Identifier
3700 and then Chars (Gen_Unit) = Chars (Defining_Entity (N))
3703 ("& is hidden within declaration of instance", Gen_Id, Gen_Unit);
3705 elsif Nkind (Gen_Id) = N_Expanded_Name
3706 and then Is_Child_Unit (Gen_Unit)
3707 and then Nkind (Prefix (Gen_Id)) = N_Identifier
3708 and then Chars (Act_Decl_Id) = Chars (Prefix (Gen_Id))
3711 ("& is hidden within declaration of instance ", Prefix (Gen_Id));
3714 Set_Entity (Gen_Id, Gen_Unit);
3716 -- If generic is a renaming, get original generic unit
3718 if Present (Renamed_Object (Gen_Unit))
3719 and then Ekind (Renamed_Object (Gen_Unit)) = E_Generic_Package
3721 Gen_Unit := Renamed_Object (Gen_Unit);
3724 -- Verify that there are no circular instantiations
3726 if In_Open_Scopes (Gen_Unit) then
3727 Error_Msg_NE ("instantiation of & within itself", N, Gen_Unit);
3731 elsif Contains_Instance_Of (Gen_Unit, Current_Scope, Gen_Id) then
3732 Error_Msg_Node_2 := Current_Scope;
3734 ("circular Instantiation: & instantiated in &!", N, Gen_Unit);
3735 Circularity_Detected := True;
3740 -- If the context of the instance is subject to SPARK_Mode "off",
3741 -- set the global flag which signals Analyze_Pragma to ignore all
3742 -- SPARK_Mode pragmas within the instance.
3744 if SPARK_Mode = Off then
3745 Ignore_Pragma_SPARK_Mode := True;
3748 Gen_Decl := Unit_Declaration_Node (Gen_Unit);
3750 -- Initialize renamings map, for error checking, and the list that
3751 -- holds private entities whose views have changed between generic
3752 -- definition and instantiation. If this is the instance created to
3753 -- validate an actual package, the instantiation environment is that
3754 -- of the enclosing instance.
3756 Create_Instantiation_Source (N, Gen_Unit, False, S_Adjustment);
3758 -- Copy original generic tree, to produce text for instantiation
3762 (Original_Node (Gen_Decl), Empty, Instantiating => True);
3764 Act_Spec := Specification (Act_Tree);
3766 -- If this is the instance created to validate an actual package,
3767 -- only the formals matter, do not examine the package spec itself.
3769 if Is_Actual_Pack then
3770 Set_Visible_Declarations (Act_Spec, New_List);
3771 Set_Private_Declarations (Act_Spec, New_List);
3775 Analyze_Associations
3777 Formals => Generic_Formal_Declarations (Act_Tree),
3778 F_Copy => Generic_Formal_Declarations (Gen_Decl));
3780 Vis_Prims_List := Check_Hidden_Primitives (Renaming_List);
3782 Set_Instance_Env (Gen_Unit, Act_Decl_Id);
3783 Set_Defining_Unit_Name (Act_Spec, Act_Decl_Name);
3784 Set_Is_Generic_Instance (Act_Decl_Id);
3785 Set_Generic_Parent (Act_Spec, Gen_Unit);
3787 -- References to the generic in its own declaration or its body are
3788 -- references to the instance. Add a renaming declaration for the
3789 -- generic unit itself. This declaration, as well as the renaming
3790 -- declarations for the generic formals, must remain private to the
3791 -- unit: the formals, because this is the language semantics, and
3792 -- the unit because its use is an artifact of the implementation.
3795 Make_Package_Renaming_Declaration (Loc,
3796 Defining_Unit_Name =>
3797 Make_Defining_Identifier (Loc, Chars (Gen_Unit)),
3798 Name => New_Occurrence_Of (Act_Decl_Id, Loc));
3800 Append (Unit_Renaming, Renaming_List);
3802 -- The renaming declarations are the first local declarations of the
3805 if Is_Non_Empty_List (Visible_Declarations (Act_Spec)) then
3807 (First (Visible_Declarations (Act_Spec)), Renaming_List);
3809 Set_Visible_Declarations (Act_Spec, Renaming_List);
3812 Act_Decl := Make_Package_Declaration (Loc, Specification => Act_Spec);
3814 -- Propagate the aspect specifications from the package declaration
3815 -- template to the instantiated version of the package declaration.
3817 if Has_Aspects (Act_Tree) then
3818 Set_Aspect_Specifications (Act_Decl,
3819 New_Copy_List_Tree (Aspect_Specifications (Act_Tree)));
3822 -- Save the instantiation node, for subsequent instantiation of the
3823 -- body, if there is one and we are generating code for the current
3824 -- unit. Mark unit as having a body (avoids premature error message).
3826 -- We instantiate the body if we are generating code, if we are
3827 -- generating cross-reference information, or if we are building
3828 -- trees for ASIS use or GNATprove use.
3831 Enclosing_Body_Present : Boolean := False;
3832 -- If the generic unit is not a compilation unit, then a body may
3833 -- be present in its parent even if none is required. We create a
3834 -- tentative pending instantiation for the body, which will be
3835 -- discarded if none is actually present.
3840 if Scope (Gen_Unit) /= Standard_Standard
3841 and then not Is_Child_Unit (Gen_Unit)
3843 Scop := Scope (Gen_Unit);
3845 while Present (Scop)
3846 and then Scop /= Standard_Standard
3848 if Unit_Requires_Body (Scop) then
3849 Enclosing_Body_Present := True;
3852 elsif In_Open_Scopes (Scop)
3853 and then In_Package_Body (Scop)
3855 Enclosing_Body_Present := True;
3859 exit when Is_Compilation_Unit (Scop);
3860 Scop := Scope (Scop);
3864 -- If front-end inlining is enabled, and this is a unit for which
3865 -- code will be generated, we instantiate the body at once.
3867 -- This is done if the instance is not the main unit, and if the
3868 -- generic is not a child unit of another generic, to avoid scope
3869 -- problems and the reinstallation of parent instances.
3872 and then (not Is_Child_Unit (Gen_Unit)
3873 or else not Is_Generic_Unit (Scope (Gen_Unit)))
3874 and then Might_Inline_Subp
3875 and then not Is_Actual_Pack
3877 if not Back_End_Inlining
3878 and then Front_End_Inlining
3879 and then (Is_In_Main_Unit (N)
3880 or else In_Main_Context (Current_Scope))
3881 and then Nkind (Parent (N)) /= N_Compilation_Unit
3885 elsif Back_End_Inlining
3886 and then Must_Inline_Subp
3887 and then (Is_In_Main_Unit (N)
3888 or else In_Main_Context (Current_Scope))
3889 and then Nkind (Parent (N)) /= N_Compilation_Unit
3893 -- In configurable_run_time mode we force the inlining of
3894 -- predefined subprograms marked Inline_Always, to minimize
3895 -- the use of the run-time library.
3897 elsif Is_Predefined_File_Name
3898 (Unit_File_Name (Get_Source_Unit (Gen_Decl)))
3899 and then Configurable_Run_Time_Mode
3900 and then Nkind (Parent (N)) /= N_Compilation_Unit
3905 -- If the current scope is itself an instance within a child
3906 -- unit, there will be duplications in the scope stack, and the
3907 -- unstacking mechanism in Inline_Instance_Body will fail.
3908 -- This loses some rare cases of optimization, and might be
3909 -- improved some day, if we can find a proper abstraction for
3910 -- "the complete compilation context" that can be saved and
3913 if Is_Generic_Instance (Current_Scope) then
3915 Curr_Unit : constant Entity_Id :=
3916 Cunit_Entity (Current_Sem_Unit);
3918 if Curr_Unit /= Current_Scope
3919 and then Is_Child_Unit (Curr_Unit)
3921 Inline_Now := False;
3928 (Unit_Requires_Body (Gen_Unit)
3929 or else Enclosing_Body_Present
3930 or else Present (Corresponding_Body (Gen_Decl)))
3931 and then (Is_In_Main_Unit (N) or else Might_Inline_Subp)
3932 and then not Is_Actual_Pack
3933 and then not Inline_Now
3934 and then (Operating_Mode = Generate_Code
3936 -- Need comment for this check ???
3938 or else (Operating_Mode = Check_Semantics
3939 and then (ASIS_Mode or GNATprove_Mode)));
3941 -- If front_end_inlining is enabled, do not instantiate body if
3942 -- within a generic context.
3944 if (Front_End_Inlining and then not Expander_Active)
3945 or else Is_Generic_Unit (Cunit_Entity (Main_Unit))
3947 Needs_Body := False;
3950 -- If the current context is generic, and the package being
3951 -- instantiated is declared within a formal package, there is no
3952 -- body to instantiate until the enclosing generic is instantiated
3953 -- and there is an actual for the formal package. If the formal
3954 -- package has parameters, we build a regular package instance for
3955 -- it, that precedes the original formal package declaration.
3957 if In_Open_Scopes (Scope (Scope (Gen_Unit))) then
3959 Decl : constant Node_Id :=
3961 (Unit_Declaration_Node (Scope (Gen_Unit)));
3963 if Nkind (Decl) = N_Formal_Package_Declaration
3964 or else (Nkind (Decl) = N_Package_Declaration
3965 and then Is_List_Member (Decl)
3966 and then Present (Next (Decl))
3968 Nkind (Next (Decl)) =
3969 N_Formal_Package_Declaration)
3971 Needs_Body := False;
3977 -- For RCI unit calling stubs, we omit the instance body if the
3978 -- instance is the RCI library unit itself.
3980 -- However there is a special case for nested instances: in this case
3981 -- we do generate the instance body, as it might be required, e.g.
3982 -- because it provides stream attributes for some type used in the
3983 -- profile of a remote subprogram. This is consistent with 12.3(12),
3984 -- which indicates that the instance body occurs at the place of the
3985 -- instantiation, and thus is part of the RCI declaration, which is
3986 -- present on all client partitions (this is E.2.3(18)).
3988 -- Note that AI12-0002 may make it illegal at some point to have
3989 -- stream attributes defined in an RCI unit, in which case this
3990 -- special case will become unnecessary. In the meantime, there
3991 -- is known application code in production that depends on this
3992 -- being possible, so we definitely cannot eliminate the body in
3993 -- the case of nested instances for the time being.
3995 -- When we generate a nested instance body, calling stubs for any
3996 -- relevant subprogram will be be inserted immediately after the
3997 -- subprogram declarations, and will take precedence over the
3998 -- subsequent (original) body. (The stub and original body will be
3999 -- complete homographs, but this is permitted in an instance).
4000 -- (Could we do better and remove the original body???)
4002 if Distribution_Stub_Mode = Generate_Caller_Stub_Body
4003 and then Comes_From_Source (N)
4004 and then Nkind (Parent (N)) = N_Compilation_Unit
4006 Needs_Body := False;
4011 -- Here is a defence against a ludicrous number of instantiations
4012 -- caused by a circular set of instantiation attempts.
4014 if Pending_Instantiations.Last > Maximum_Instantiations then
4015 Error_Msg_Uint_1 := UI_From_Int (Maximum_Instantiations);
4016 Error_Msg_N ("too many instantiations, exceeds max of^", N);
4017 Error_Msg_N ("\limit can be changed using -gnateinn switch", N);
4018 raise Unrecoverable_Error;
4021 -- Indicate that the enclosing scopes contain an instantiation,
4022 -- and that cleanup actions should be delayed until after the
4023 -- instance body is expanded.
4025 Check_Forward_Instantiation (Gen_Decl);
4026 if Nkind (N) = N_Package_Instantiation then
4028 Enclosing_Master : Entity_Id;
4031 -- Loop to search enclosing masters
4033 Enclosing_Master := Current_Scope;
4034 Scope_Loop : while Enclosing_Master /= Standard_Standard loop
4035 if Ekind (Enclosing_Master) = E_Package then
4036 if Is_Compilation_Unit (Enclosing_Master) then
4037 if In_Package_Body (Enclosing_Master) then
4039 (Body_Entity (Enclosing_Master));
4048 Enclosing_Master := Scope (Enclosing_Master);
4051 elsif Is_Generic_Unit (Enclosing_Master)
4052 or else Ekind (Enclosing_Master) = E_Void
4054 -- Cleanup actions will eventually be performed on the
4055 -- enclosing subprogram or package instance, if any.
4056 -- Enclosing scope is void in the formal part of a
4057 -- generic subprogram.
4062 if Ekind (Enclosing_Master) = E_Entry
4064 Ekind (Scope (Enclosing_Master)) = E_Protected_Type
4066 if not Expander_Active then
4070 Protected_Body_Subprogram (Enclosing_Master);
4074 Set_Delay_Cleanups (Enclosing_Master);
4076 while Ekind (Enclosing_Master) = E_Block loop
4077 Enclosing_Master := Scope (Enclosing_Master);
4080 if Is_Subprogram (Enclosing_Master) then
4081 Delay_Descriptors (Enclosing_Master);
4083 elsif Is_Task_Type (Enclosing_Master) then
4085 TBP : constant Node_Id :=
4086 Get_Task_Body_Procedure
4089 if Present (TBP) then
4090 Delay_Descriptors (TBP);
4091 Set_Delay_Cleanups (TBP);
4098 end loop Scope_Loop;
4101 -- Make entry in table
4103 Pending_Instantiations.Append
4105 Act_Decl => Act_Decl,
4106 Expander_Status => Expander_Active,
4107 Current_Sem_Unit => Current_Sem_Unit,
4108 Scope_Suppress => Scope_Suppress,
4109 Local_Suppress_Stack_Top => Local_Suppress_Stack_Top,
4110 Version => Ada_Version,
4111 Version_Pragma => Ada_Version_Pragma,
4112 Warnings => Save_Warnings,
4113 SPARK_Mode => SPARK_Mode,
4114 SPARK_Mode_Pragma => SPARK_Mode_Pragma));
4118 Set_Categorization_From_Pragmas (Act_Decl);
4120 if Parent_Installed then
4124 Set_Instance_Spec (N, Act_Decl);
4126 -- If not a compilation unit, insert the package declaration before
4127 -- the original instantiation node.
4129 if Nkind (Parent (N)) /= N_Compilation_Unit then
4130 Mark_Rewrite_Insertion (Act_Decl);
4131 Insert_Before (N, Act_Decl);
4134 -- For an instantiation that is a compilation unit, place
4135 -- declaration on current node so context is complete for analysis
4136 -- (including nested instantiations). If this is the main unit,
4137 -- the declaration eventually replaces the instantiation node.
4138 -- If the instance body is created later, it replaces the
4139 -- instance node, and the declaration is attached to it
4140 -- (see Build_Instance_Compilation_Unit_Nodes).
4143 if Cunit_Entity (Current_Sem_Unit) = Defining_Entity (N) then
4145 -- The entity for the current unit is the newly created one,
4146 -- and all semantic information is attached to it.
4148 Set_Cunit_Entity (Current_Sem_Unit, Act_Decl_Id);
4150 -- If this is the main unit, replace the main entity as well
4152 if Current_Sem_Unit = Main_Unit then
4153 Main_Unit_Entity := Act_Decl_Id;
4157 Set_Unit (Parent (N), Act_Decl);
4158 Set_Parent_Spec (Act_Decl, Parent_Spec (N));
4159 Set_Package_Instantiation (Act_Decl_Id, N);
4161 -- Process aspect specifications of the instance node, if any, to
4162 -- take into account categorization pragmas before analyzing the
4165 if Has_Aspects (N) then
4166 Analyze_Aspect_Specifications (N, Act_Decl_Id);
4170 Set_Unit (Parent (N), N);
4171 Set_Body_Required (Parent (N), False);
4173 -- We never need elaboration checks on instantiations, since by
4174 -- definition, the body instantiation is elaborated at the same
4175 -- time as the spec instantiation.
4177 Set_Suppress_Elaboration_Warnings (Act_Decl_Id);
4178 Set_Kill_Elaboration_Checks (Act_Decl_Id);
4181 Check_Elab_Instantiation (N);
4183 if ABE_Is_Certain (N) and then Needs_Body then
4184 Pending_Instantiations.Decrement_Last;
4187 Check_Hidden_Child_Unit (N, Gen_Unit, Act_Decl_Id);
4189 Set_First_Private_Entity (Defining_Unit_Name (Unit_Renaming),
4190 First_Private_Entity (Act_Decl_Id));
4192 -- If the instantiation will receive a body, the unit will be
4193 -- transformed into a package body, and receive its own elaboration
4194 -- entity. Otherwise, the nature of the unit is now a package
4197 if Nkind (Parent (N)) = N_Compilation_Unit
4198 and then not Needs_Body
4200 Rewrite (N, Act_Decl);
4203 if Present (Corresponding_Body (Gen_Decl))
4204 or else Unit_Requires_Body (Gen_Unit)
4206 Set_Has_Completion (Act_Decl_Id);
4209 Check_Formal_Packages (Act_Decl_Id);
4211 Restore_Hidden_Primitives (Vis_Prims_List);
4212 Restore_Private_Views (Act_Decl_Id);
4214 Inherit_Context (Gen_Decl, N);
4216 if Parent_Installed then
4221 Env_Installed := False;
4224 Validate_Categorization_Dependency (N, Act_Decl_Id);
4226 -- There used to be a check here to prevent instantiations in local
4227 -- contexts if the No_Local_Allocators restriction was active. This
4228 -- check was removed by a binding interpretation in AI-95-00130/07,
4229 -- but we retain the code for documentation purposes.
4231 -- if Ekind (Act_Decl_Id) /= E_Void
4232 -- and then not Is_Library_Level_Entity (Act_Decl_Id)
4234 -- Check_Restriction (No_Local_Allocators, N);
4238 Inline_Instance_Body (N, Gen_Unit, Act_Decl);
4241 -- The following is a tree patch for ASIS: ASIS needs separate nodes to
4242 -- be used as defining identifiers for a formal package and for the
4243 -- corresponding expanded package.
4245 if Nkind (N) = N_Formal_Package_Declaration then
4246 Act_Decl_Id := New_Copy (Defining_Entity (N));
4247 Set_Comes_From_Source (Act_Decl_Id, True);
4248 Set_Is_Generic_Instance (Act_Decl_Id, False);
4249 Set_Defining_Identifier (N, Act_Decl_Id);
4252 Ignore_Pragma_SPARK_Mode := Save_IPSM;
4253 SPARK_Mode := Save_SM;
4254 SPARK_Mode_Pragma := Save_SMP;
4255 Style_Check := Save_Style_Check;
4257 -- Check that if N is an instantiation of System.Dim_Float_IO or
4258 -- System.Dim_Integer_IO, the formal type has a dimension system.
4260 if Nkind (N) = N_Package_Instantiation
4261 and then Is_Dim_IO_Package_Instantiation (N)
4264 Assoc : constant Node_Id := First (Generic_Associations (N));
4266 if not Has_Dimension_System
4267 (Etype (Explicit_Generic_Actual_Parameter (Assoc)))
4269 Error_Msg_N ("type with a dimension system expected", Assoc);
4275 if Has_Aspects (N) and then Nkind (Parent (N)) /= N_Compilation_Unit then
4276 Analyze_Aspect_Specifications (N, Act_Decl_Id);
4280 when Instantiation_Error =>
4281 if Parent_Installed then
4285 if Env_Installed then
4289 Ignore_Pragma_SPARK_Mode := Save_IPSM;
4290 SPARK_Mode := Save_SM;
4291 SPARK_Mode_Pragma := Save_SMP;
4292 Style_Check := Save_Style_Check;
4293 end Analyze_Package_Instantiation;
4295 --------------------------
4296 -- Inline_Instance_Body --
4297 --------------------------
4299 procedure Inline_Instance_Body
4301 Gen_Unit : Entity_Id;
4305 Gen_Comp : constant Entity_Id :=
4306 Cunit_Entity (Get_Source_Unit (Gen_Unit));
4307 Curr_Comp : constant Node_Id := Cunit (Current_Sem_Unit);
4308 Curr_Scope : Entity_Id := Empty;
4309 Curr_Unit : constant Entity_Id := Cunit_Entity (Current_Sem_Unit);
4310 Removed : Boolean := False;
4311 Num_Scopes : Int := 0;
4313 Scope_Stack_Depth : constant Int :=
4314 Scope_Stack.Last - Scope_Stack.First + 1;
4316 Use_Clauses : array (1 .. Scope_Stack_Depth) of Node_Id;
4317 Instances : array (1 .. Scope_Stack_Depth) of Entity_Id;
4318 Inner_Scopes : array (1 .. Scope_Stack_Depth) of Entity_Id;
4320 Num_Inner : Int := 0;
4321 N_Instances : Int := 0;
4325 -- Case of generic unit defined in another unit. We must remove the
4326 -- complete context of the current unit to install that of the generic.
4328 if Gen_Comp /= Cunit_Entity (Current_Sem_Unit) then
4330 -- Add some comments for the following two loops ???
4333 while Present (S) and then S /= Standard_Standard loop
4335 Num_Scopes := Num_Scopes + 1;
4337 Use_Clauses (Num_Scopes) :=
4339 (Scope_Stack.Last - Num_Scopes + 1).
4341 End_Use_Clauses (Use_Clauses (Num_Scopes));
4343 exit when Scope_Stack.Last - Num_Scopes + 1 = Scope_Stack.First
4344 or else Scope_Stack.Table
4345 (Scope_Stack.Last - Num_Scopes).Entity
4349 exit when Is_Generic_Instance (S)
4350 and then (In_Package_Body (S)
4351 or else Ekind (S) = E_Procedure
4352 or else Ekind (S) = E_Function);
4356 Vis := Is_Immediately_Visible (Gen_Comp);
4358 -- Find and save all enclosing instances
4363 and then S /= Standard_Standard
4365 if Is_Generic_Instance (S) then
4366 N_Instances := N_Instances + 1;
4367 Instances (N_Instances) := S;
4369 exit when In_Package_Body (S);
4375 -- Remove context of current compilation unit, unless we are within a
4376 -- nested package instantiation, in which case the context has been
4377 -- removed previously.
4379 -- If current scope is the body of a child unit, remove context of
4380 -- spec as well. If an enclosing scope is an instance body, the
4381 -- context has already been removed, but the entities in the body
4382 -- must be made invisible as well.
4387 and then S /= Standard_Standard
4389 if Is_Generic_Instance (S)
4390 and then (In_Package_Body (S)
4391 or else Ekind (S) = E_Procedure
4392 or else Ekind (S) = E_Function)
4394 -- We still have to remove the entities of the enclosing
4395 -- instance from direct visibility.
4400 E := First_Entity (S);
4401 while Present (E) loop
4402 Set_Is_Immediately_Visible (E, False);
4411 or else (Ekind (Curr_Unit) = E_Package_Body
4412 and then S = Spec_Entity (Curr_Unit))
4413 or else (Ekind (Curr_Unit) = E_Subprogram_Body
4416 (Unit_Declaration_Node (Curr_Unit)))
4420 -- Remove entities in current scopes from visibility, so that
4421 -- instance body is compiled in a clean environment.
4423 List := Save_Scope_Stack (Handle_Use => False);
4425 if Is_Child_Unit (S) then
4427 -- Remove child unit from stack, as well as inner scopes.
4428 -- Removing the context of a child unit removes parent units
4431 while Current_Scope /= S loop
4432 Num_Inner := Num_Inner + 1;
4433 Inner_Scopes (Num_Inner) := Current_Scope;
4438 Remove_Context (Curr_Comp);
4442 Remove_Context (Curr_Comp);
4445 if Ekind (Curr_Unit) = E_Package_Body then
4446 Remove_Context (Library_Unit (Curr_Comp));
4452 pragma Assert (Num_Inner < Num_Scopes);
4454 Push_Scope (Standard_Standard);
4455 Scope_Stack.Table (Scope_Stack.Last).Is_Active_Stack_Base := True;
4456 Instantiate_Package_Body
4459 Act_Decl => Act_Decl,
4460 Expander_Status => Expander_Active,
4461 Current_Sem_Unit => Current_Sem_Unit,
4462 Scope_Suppress => Scope_Suppress,
4463 Local_Suppress_Stack_Top => Local_Suppress_Stack_Top,
4464 Version => Ada_Version,
4465 Version_Pragma => Ada_Version_Pragma,
4466 Warnings => Save_Warnings,
4467 SPARK_Mode => SPARK_Mode,
4468 SPARK_Mode_Pragma => SPARK_Mode_Pragma)),
4469 Inlined_Body => True);
4475 Set_Is_Immediately_Visible (Gen_Comp, Vis);
4477 -- Reset Generic_Instance flag so that use clauses can be installed
4478 -- in the proper order. (See Use_One_Package for effect of enclosing
4479 -- instances on processing of use clauses).
4481 for J in 1 .. N_Instances loop
4482 Set_Is_Generic_Instance (Instances (J), False);
4486 Install_Context (Curr_Comp);
4488 if Present (Curr_Scope)
4489 and then Is_Child_Unit (Curr_Scope)
4491 Push_Scope (Curr_Scope);
4492 Set_Is_Immediately_Visible (Curr_Scope);
4494 -- Finally, restore inner scopes as well
4496 for J in reverse 1 .. Num_Inner loop
4497 Push_Scope (Inner_Scopes (J));
4501 Restore_Scope_Stack (List, Handle_Use => False);
4503 if Present (Curr_Scope)
4505 (In_Private_Part (Curr_Scope)
4506 or else In_Package_Body (Curr_Scope))
4508 -- Install private declaration of ancestor units, which are
4509 -- currently available. Restore_Scope_Stack and Install_Context
4510 -- only install the visible part of parents.
4515 Par := Scope (Curr_Scope);
4516 while (Present (Par))
4517 and then Par /= Standard_Standard
4519 Install_Private_Declarations (Par);
4526 -- Restore use clauses. For a child unit, use clauses in the parents
4527 -- are restored when installing the context, so only those in inner
4528 -- scopes (and those local to the child unit itself) need to be
4529 -- installed explicitly.
4531 if Is_Child_Unit (Curr_Unit)
4534 for J in reverse 1 .. Num_Inner + 1 loop
4535 Scope_Stack.Table (Scope_Stack.Last - J + 1).First_Use_Clause :=
4537 Install_Use_Clauses (Use_Clauses (J));
4541 for J in reverse 1 .. Num_Scopes loop
4542 Scope_Stack.Table (Scope_Stack.Last - J + 1).First_Use_Clause :=
4544 Install_Use_Clauses (Use_Clauses (J));
4548 -- Restore status of instances. If one of them is a body, make its
4549 -- local entities visible again.
4556 for J in 1 .. N_Instances loop
4557 Inst := Instances (J);
4558 Set_Is_Generic_Instance (Inst, True);
4560 if In_Package_Body (Inst)
4561 or else Ekind (S) = E_Procedure
4562 or else Ekind (S) = E_Function
4564 E := First_Entity (Instances (J));
4565 while Present (E) loop
4566 Set_Is_Immediately_Visible (E);
4573 -- If generic unit is in current unit, current context is correct
4576 Instantiate_Package_Body
4579 Act_Decl => Act_Decl,
4580 Expander_Status => Expander_Active,
4581 Current_Sem_Unit => Current_Sem_Unit,
4582 Scope_Suppress => Scope_Suppress,
4583 Local_Suppress_Stack_Top => Local_Suppress_Stack_Top,
4584 Version => Ada_Version,
4585 Version_Pragma => Ada_Version_Pragma,
4586 Warnings => Save_Warnings,
4587 SPARK_Mode => SPARK_Mode,
4588 SPARK_Mode_Pragma => SPARK_Mode_Pragma)),
4589 Inlined_Body => True);
4591 end Inline_Instance_Body;
4593 -------------------------------------
4594 -- Analyze_Procedure_Instantiation --
4595 -------------------------------------
4597 procedure Analyze_Procedure_Instantiation (N : Node_Id) is
4599 Analyze_Subprogram_Instantiation (N, E_Procedure);
4600 end Analyze_Procedure_Instantiation;
4602 -----------------------------------
4603 -- Need_Subprogram_Instance_Body --
4604 -----------------------------------
4606 function Need_Subprogram_Instance_Body
4608 Subp : Entity_Id) return Boolean
4611 -- Must be inlined (or inlined renaming)
4613 if (Is_In_Main_Unit (N)
4614 or else Is_Inlined (Subp)
4615 or else Is_Inlined (Alias (Subp)))
4617 -- Must be generating code or analyzing code in ASIS/GNATprove mode
4619 and then (Operating_Mode = Generate_Code
4620 or else (Operating_Mode = Check_Semantics
4621 and then (ASIS_Mode or GNATprove_Mode)))
4623 -- The body is needed when generating code (full expansion), in ASIS
4624 -- mode for other tools, and in GNATprove mode (special expansion) for
4625 -- formal verification of the body itself.
4627 and then (Expander_Active or ASIS_Mode or GNATprove_Mode)
4629 -- No point in inlining if ABE is inevitable
4631 and then not ABE_Is_Certain (N)
4633 -- Or if subprogram is eliminated
4635 and then not Is_Eliminated (Subp)
4637 Pending_Instantiations.Append
4639 Act_Decl => Unit_Declaration_Node (Subp),
4640 Expander_Status => Expander_Active,
4641 Current_Sem_Unit => Current_Sem_Unit,
4642 Scope_Suppress => Scope_Suppress,
4643 Local_Suppress_Stack_Top => Local_Suppress_Stack_Top,
4644 Version => Ada_Version,
4645 Version_Pragma => Ada_Version_Pragma,
4646 Warnings => Save_Warnings,
4647 SPARK_Mode => SPARK_Mode,
4648 SPARK_Mode_Pragma => SPARK_Mode_Pragma));
4651 -- Here if not inlined, or we ignore the inlining
4656 end Need_Subprogram_Instance_Body;
4658 --------------------------------------
4659 -- Analyze_Subprogram_Instantiation --
4660 --------------------------------------
4662 procedure Analyze_Subprogram_Instantiation
4666 Loc : constant Source_Ptr := Sloc (N);
4667 Gen_Id : constant Node_Id := Name (N);
4669 Anon_Id : constant Entity_Id :=
4670 Make_Defining_Identifier (Sloc (Defining_Entity (N)),
4671 Chars => New_External_Name
4672 (Chars (Defining_Entity (N)), 'R'));
4674 Act_Decl_Id : Entity_Id;
4679 Env_Installed : Boolean := False;
4680 Gen_Unit : Entity_Id;
4682 Pack_Id : Entity_Id;
4683 Parent_Installed : Boolean := False;
4684 Renaming_List : List_Id;
4686 procedure Analyze_Instance_And_Renamings;
4687 -- The instance must be analyzed in a context that includes the mappings
4688 -- of generic parameters into actuals. We create a package declaration
4689 -- for this purpose, and a subprogram with an internal name within the
4690 -- package. The subprogram instance is simply an alias for the internal
4691 -- subprogram, declared in the current scope.
4693 ------------------------------------
4694 -- Analyze_Instance_And_Renamings --
4695 ------------------------------------
4697 procedure Analyze_Instance_And_Renamings is
4698 Def_Ent : constant Entity_Id := Defining_Entity (N);
4699 Pack_Decl : Node_Id;
4702 if Nkind (Parent (N)) = N_Compilation_Unit then
4704 -- For the case of a compilation unit, the container package has
4705 -- the same name as the instantiation, to insure that the binder
4706 -- calls the elaboration procedure with the right name. Copy the
4707 -- entity of the instance, which may have compilation level flags
4708 -- (e.g. Is_Child_Unit) set.
4710 Pack_Id := New_Copy (Def_Ent);
4713 -- Otherwise we use the name of the instantiation concatenated
4714 -- with its source position to ensure uniqueness if there are
4715 -- several instantiations with the same name.
4718 Make_Defining_Identifier (Loc,
4719 Chars => New_External_Name
4720 (Related_Id => Chars (Def_Ent),
4722 Suffix_Index => Source_Offset (Sloc (Def_Ent))));
4725 Pack_Decl := Make_Package_Declaration (Loc,
4726 Specification => Make_Package_Specification (Loc,
4727 Defining_Unit_Name => Pack_Id,
4728 Visible_Declarations => Renaming_List,
4729 End_Label => Empty));
4731 Set_Instance_Spec (N, Pack_Decl);
4732 Set_Is_Generic_Instance (Pack_Id);
4733 Set_Debug_Info_Needed (Pack_Id);
4735 -- Case of not a compilation unit
4737 if Nkind (Parent (N)) /= N_Compilation_Unit then
4738 Mark_Rewrite_Insertion (Pack_Decl);
4739 Insert_Before (N, Pack_Decl);
4740 Set_Has_Completion (Pack_Id);
4742 -- Case of an instantiation that is a compilation unit
4744 -- Place declaration on current node so context is complete for
4745 -- analysis (including nested instantiations), and for use in a
4746 -- context_clause (see Analyze_With_Clause).
4749 Set_Unit (Parent (N), Pack_Decl);
4750 Set_Parent_Spec (Pack_Decl, Parent_Spec (N));
4753 Analyze (Pack_Decl);
4754 Check_Formal_Packages (Pack_Id);
4755 Set_Is_Generic_Instance (Pack_Id, False);
4757 -- Why do we clear Is_Generic_Instance??? We set it 20 lines
4760 -- Body of the enclosing package is supplied when instantiating the
4761 -- subprogram body, after semantic analysis is completed.
4763 if Nkind (Parent (N)) = N_Compilation_Unit then
4765 -- Remove package itself from visibility, so it does not
4766 -- conflict with subprogram.
4768 Set_Name_Entity_Id (Chars (Pack_Id), Homonym (Pack_Id));
4770 -- Set name and scope of internal subprogram so that the proper
4771 -- external name will be generated. The proper scope is the scope
4772 -- of the wrapper package. We need to generate debugging info for
4773 -- the internal subprogram, so set flag accordingly.
4775 Set_Chars (Anon_Id, Chars (Defining_Entity (N)));
4776 Set_Scope (Anon_Id, Scope (Pack_Id));
4778 -- Mark wrapper package as referenced, to avoid spurious warnings
4779 -- if the instantiation appears in various with_ clauses of
4780 -- subunits of the main unit.
4782 Set_Referenced (Pack_Id);
4785 Set_Is_Generic_Instance (Anon_Id);
4786 Set_Debug_Info_Needed (Anon_Id);
4787 Act_Decl_Id := New_Copy (Anon_Id);
4789 Set_Parent (Act_Decl_Id, Parent (Anon_Id));
4790 Set_Chars (Act_Decl_Id, Chars (Defining_Entity (N)));
4791 Set_Sloc (Act_Decl_Id, Sloc (Defining_Entity (N)));
4792 Set_Comes_From_Source (Act_Decl_Id, True);
4794 -- The signature may involve types that are not frozen yet, but the
4795 -- subprogram will be frozen at the point the wrapper package is
4796 -- frozen, so it does not need its own freeze node. In fact, if one
4797 -- is created, it might conflict with the freezing actions from the
4800 Set_Has_Delayed_Freeze (Anon_Id, False);
4802 -- If the instance is a child unit, mark the Id accordingly. Mark
4803 -- the anonymous entity as well, which is the real subprogram and
4804 -- which is used when the instance appears in a context clause.
4805 -- Similarly, propagate the Is_Eliminated flag to handle properly
4806 -- nested eliminated subprograms.
4808 Set_Is_Child_Unit (Act_Decl_Id, Is_Child_Unit (Defining_Entity (N)));
4809 Set_Is_Child_Unit (Anon_Id, Is_Child_Unit (Defining_Entity (N)));
4810 New_Overloaded_Entity (Act_Decl_Id);
4811 Check_Eliminated (Act_Decl_Id);
4812 Set_Is_Eliminated (Anon_Id, Is_Eliminated (Act_Decl_Id));
4814 -- In compilation unit case, kill elaboration checks on the
4815 -- instantiation, since they are never needed -- the body is
4816 -- instantiated at the same point as the spec.
4818 if Nkind (Parent (N)) = N_Compilation_Unit then
4819 Set_Suppress_Elaboration_Warnings (Act_Decl_Id);
4820 Set_Kill_Elaboration_Checks (Act_Decl_Id);
4821 Set_Is_Compilation_Unit (Anon_Id);
4823 Set_Cunit_Entity (Current_Sem_Unit, Pack_Id);
4826 -- The instance is not a freezing point for the new subprogram
4828 Set_Is_Frozen (Act_Decl_Id, False);
4830 if Nkind (Defining_Entity (N)) = N_Defining_Operator_Symbol then
4831 Valid_Operator_Definition (Act_Decl_Id);
4834 Set_Alias (Act_Decl_Id, Anon_Id);
4835 Set_Parent (Act_Decl_Id, Parent (Anon_Id));
4836 Set_Has_Completion (Act_Decl_Id);
4837 Set_Related_Instance (Pack_Id, Act_Decl_Id);
4839 if Nkind (Parent (N)) = N_Compilation_Unit then
4840 Set_Body_Required (Parent (N), False);
4842 end Analyze_Instance_And_Renamings;
4846 Save_IPSM : constant Boolean := Ignore_Pragma_SPARK_Mode;
4847 -- Save flag Ignore_Pragma_SPARK_Mode for restore on exit
4849 Save_SM : constant SPARK_Mode_Type := SPARK_Mode;
4850 Save_SMP : constant Node_Id := SPARK_Mode_Pragma;
4851 -- Save the SPARK_Mode-related data for restore on exit
4853 Vis_Prims_List : Elist_Id := No_Elist;
4854 -- List of primitives made temporarily visible in the instantiation
4855 -- to match the visibility of the formal type
4857 -- Start of processing for Analyze_Subprogram_Instantiation
4860 Check_SPARK_05_Restriction ("generic is not allowed", N);
4862 -- Very first thing: check for special Text_IO unit in case we are
4863 -- instantiating one of the children of [[Wide_]Wide_]Text_IO. Of course
4864 -- such an instantiation is bogus (these are packages, not subprograms),
4865 -- but we get a better error message if we do this.
4867 Check_Text_IO_Special_Unit (Gen_Id);
4869 -- Make node global for error reporting
4871 Instantiation_Node := N;
4873 -- For package instantiations we turn off style checks, because they
4874 -- will have been emitted in the generic. For subprogram instantiations
4875 -- we want to apply at least the check on overriding indicators so we
4876 -- do not modify the style check status.
4878 -- The renaming declarations for the actuals do not come from source and
4879 -- will not generate spurious warnings.
4881 Preanalyze_Actuals (N);
4884 Env_Installed := True;
4885 Check_Generic_Child_Unit (Gen_Id, Parent_Installed);
4886 Gen_Unit := Entity (Gen_Id);
4888 Generate_Reference (Gen_Unit, Gen_Id);
4890 if Nkind (Gen_Id) = N_Identifier
4891 and then Chars (Gen_Unit) = Chars (Defining_Entity (N))
4894 ("& is hidden within declaration of instance", Gen_Id, Gen_Unit);
4897 if Etype (Gen_Unit) = Any_Type then
4902 -- Verify that it is a generic subprogram of the right kind, and that
4903 -- it does not lead to a circular instantiation.
4905 if K = E_Procedure and then Ekind (Gen_Unit) /= E_Generic_Procedure then
4907 ("& is not the name of a generic procedure", Gen_Id, Gen_Unit);
4909 elsif K = E_Function and then Ekind (Gen_Unit) /= E_Generic_Function then
4911 ("& is not the name of a generic function", Gen_Id, Gen_Unit);
4913 elsif In_Open_Scopes (Gen_Unit) then
4914 Error_Msg_NE ("instantiation of & within itself", N, Gen_Unit);
4917 -- If the context of the instance is subject to SPARK_Mode "off",
4918 -- set the global flag which signals Analyze_Pragma to ignore all
4919 -- SPARK_Mode pragmas within the instance.
4921 if SPARK_Mode = Off then
4922 Ignore_Pragma_SPARK_Mode := True;
4925 Set_Entity (Gen_Id, Gen_Unit);
4926 Set_Is_Instantiated (Gen_Unit);
4928 if In_Extended_Main_Source_Unit (N) then
4929 Generate_Reference (Gen_Unit, N);
4932 -- If renaming, get original unit
4934 if Present (Renamed_Object (Gen_Unit))
4935 and then (Ekind (Renamed_Object (Gen_Unit)) = E_Generic_Procedure
4937 Ekind (Renamed_Object (Gen_Unit)) = E_Generic_Function)
4939 Gen_Unit := Renamed_Object (Gen_Unit);
4940 Set_Is_Instantiated (Gen_Unit);
4941 Generate_Reference (Gen_Unit, N);
4944 if Contains_Instance_Of (Gen_Unit, Current_Scope, Gen_Id) then
4945 Error_Msg_Node_2 := Current_Scope;
4947 ("circular Instantiation: & instantiated in &!", N, Gen_Unit);
4948 Circularity_Detected := True;
4949 Restore_Hidden_Primitives (Vis_Prims_List);
4953 Gen_Decl := Unit_Declaration_Node (Gen_Unit);
4955 -- Initialize renamings map, for error checking
4957 Generic_Renamings.Set_Last (0);
4958 Generic_Renamings_HTable.Reset;
4960 Create_Instantiation_Source (N, Gen_Unit, False, S_Adjustment);
4962 -- Copy original generic tree, to produce text for instantiation
4966 (Original_Node (Gen_Decl), Empty, Instantiating => True);
4968 -- Inherit overriding indicator from instance node
4970 Act_Spec := Specification (Act_Tree);
4971 Set_Must_Override (Act_Spec, Must_Override (N));
4972 Set_Must_Not_Override (Act_Spec, Must_Not_Override (N));
4975 Analyze_Associations
4977 Formals => Generic_Formal_Declarations (Act_Tree),
4978 F_Copy => Generic_Formal_Declarations (Gen_Decl));
4980 Vis_Prims_List := Check_Hidden_Primitives (Renaming_List);
4982 -- The subprogram itself cannot contain a nested instance, so the
4983 -- current parent is left empty.
4985 Set_Instance_Env (Gen_Unit, Empty);
4987 -- Build the subprogram declaration, which does not appear in the
4988 -- generic template, and give it a sloc consistent with that of the
4991 Set_Defining_Unit_Name (Act_Spec, Anon_Id);
4992 Set_Generic_Parent (Act_Spec, Gen_Unit);
4994 Make_Subprogram_Declaration (Sloc (Act_Spec),
4995 Specification => Act_Spec);
4997 -- The aspects have been copied previously, but they have to be
4998 -- linked explicitly to the new subprogram declaration. Explicit
4999 -- pre/postconditions on the instance are analyzed below, in a
5002 Move_Aspects (Act_Tree, To => Act_Decl);
5003 Set_Categorization_From_Pragmas (Act_Decl);
5005 if Parent_Installed then
5009 Append (Act_Decl, Renaming_List);
5010 Analyze_Instance_And_Renamings;
5012 -- If the generic is marked Import (Intrinsic), then so is the
5013 -- instance. This indicates that there is no body to instantiate. If
5014 -- generic is marked inline, so it the instance, and the anonymous
5015 -- subprogram it renames. If inlined, or else if inlining is enabled
5016 -- for the compilation, we generate the instance body even if it is
5017 -- not within the main unit.
5019 if Is_Intrinsic_Subprogram (Gen_Unit) then
5020 Set_Is_Intrinsic_Subprogram (Anon_Id);
5021 Set_Is_Intrinsic_Subprogram (Act_Decl_Id);
5023 if Chars (Gen_Unit) = Name_Unchecked_Conversion then
5024 Validate_Unchecked_Conversion (N, Act_Decl_Id);
5028 -- Inherit convention from generic unit. Intrinsic convention, as for
5029 -- an instance of unchecked conversion, is not inherited because an
5030 -- explicit Ada instance has been created.
5032 if Has_Convention_Pragma (Gen_Unit)
5033 and then Convention (Gen_Unit) /= Convention_Intrinsic
5035 Set_Convention (Act_Decl_Id, Convention (Gen_Unit));
5036 Set_Is_Exported (Act_Decl_Id, Is_Exported (Gen_Unit));
5039 Generate_Definition (Act_Decl_Id);
5040 -- Set_Contract (Anon_Id, Make_Contract (Sloc (Anon_Id)));
5042 Set_Contract (Act_Decl_Id, Make_Contract (Sloc (Act_Decl_Id)));
5044 -- Inherit all inlining-related flags which apply to the generic in
5045 -- the subprogram and its declaration.
5047 Set_Is_Inlined (Act_Decl_Id, Is_Inlined (Gen_Unit));
5048 Set_Is_Inlined (Anon_Id, Is_Inlined (Gen_Unit));
5050 Set_Has_Pragma_Inline (Act_Decl_Id, Has_Pragma_Inline (Gen_Unit));
5051 Set_Has_Pragma_Inline (Anon_Id, Has_Pragma_Inline (Gen_Unit));
5053 Set_Has_Pragma_Inline_Always
5054 (Act_Decl_Id, Has_Pragma_Inline_Always (Gen_Unit));
5055 Set_Has_Pragma_Inline_Always
5056 (Anon_Id, Has_Pragma_Inline_Always (Gen_Unit));
5058 if not Is_Intrinsic_Subprogram (Gen_Unit) then
5059 Check_Elab_Instantiation (N);
5062 if Is_Dispatching_Operation (Act_Decl_Id)
5063 and then Ada_Version >= Ada_2005
5069 Formal := First_Formal (Act_Decl_Id);
5070 while Present (Formal) loop
5071 if Ekind (Etype (Formal)) = E_Anonymous_Access_Type
5072 and then Is_Controlling_Formal (Formal)
5073 and then not Can_Never_Be_Null (Formal)
5075 Error_Msg_NE ("access parameter& is controlling,",
5078 ("\corresponding parameter of & must be"
5079 & " explicitly null-excluding", N, Gen_Id);
5082 Next_Formal (Formal);
5087 Check_Hidden_Child_Unit (N, Gen_Unit, Act_Decl_Id);
5089 Validate_Categorization_Dependency (N, Act_Decl_Id);
5091 if not Is_Intrinsic_Subprogram (Act_Decl_Id) then
5092 Inherit_Context (Gen_Decl, N);
5094 Restore_Private_Views (Pack_Id, False);
5096 -- If the context requires a full instantiation, mark node for
5097 -- subsequent construction of the body.
5099 if Need_Subprogram_Instance_Body (N, Act_Decl_Id) then
5100 Check_Forward_Instantiation (Gen_Decl);
5102 -- The wrapper package is always delayed, because it does not
5103 -- constitute a freeze point, but to insure that the freeze
5104 -- node is placed properly, it is created directly when
5105 -- instantiating the body (otherwise the freeze node might
5106 -- appear to early for nested instantiations).
5108 elsif Nkind (Parent (N)) = N_Compilation_Unit then
5110 -- For ASIS purposes, indicate that the wrapper package has
5111 -- replaced the instantiation node.
5113 Rewrite (N, Unit (Parent (N)));
5114 Set_Unit (Parent (N), N);
5117 elsif Nkind (Parent (N)) = N_Compilation_Unit then
5119 -- Replace instance node for library-level instantiations of
5120 -- intrinsic subprograms, for ASIS use.
5122 Rewrite (N, Unit (Parent (N)));
5123 Set_Unit (Parent (N), N);
5126 if Parent_Installed then
5130 Restore_Hidden_Primitives (Vis_Prims_List);
5132 Env_Installed := False;
5133 Generic_Renamings.Set_Last (0);
5134 Generic_Renamings_HTable.Reset;
5136 Ignore_Pragma_SPARK_Mode := Save_IPSM;
5137 SPARK_Mode := Save_SM;
5138 SPARK_Mode_Pragma := Save_SMP;
5142 if Has_Aspects (N) then
5143 Analyze_Aspect_Specifications (N, Act_Decl_Id);
5147 when Instantiation_Error =>
5148 if Parent_Installed then
5152 if Env_Installed then
5156 Ignore_Pragma_SPARK_Mode := Save_IPSM;
5157 SPARK_Mode := Save_SM;
5158 SPARK_Mode_Pragma := Save_SMP;
5159 end Analyze_Subprogram_Instantiation;
5161 -------------------------
5162 -- Get_Associated_Node --
5163 -------------------------
5165 function Get_Associated_Node (N : Node_Id) return Node_Id is
5169 Assoc := Associated_Node (N);
5171 if Nkind (Assoc) /= Nkind (N) then
5174 elsif Nkind_In (Assoc, N_Aggregate, N_Extension_Aggregate) then
5178 -- If the node is part of an inner generic, it may itself have been
5179 -- remapped into a further generic copy. Associated_Node is otherwise
5180 -- used for the entity of the node, and will be of a different node
5181 -- kind, or else N has been rewritten as a literal or function call.
5183 while Present (Associated_Node (Assoc))
5184 and then Nkind (Associated_Node (Assoc)) = Nkind (Assoc)
5186 Assoc := Associated_Node (Assoc);
5189 -- Follow and additional link in case the final node was rewritten.
5190 -- This can only happen with nested generic units.
5192 if (Nkind (Assoc) = N_Identifier or else Nkind (Assoc) in N_Op)
5193 and then Present (Associated_Node (Assoc))
5194 and then (Nkind_In (Associated_Node (Assoc), N_Function_Call,
5195 N_Explicit_Dereference,
5200 Assoc := Associated_Node (Assoc);
5203 -- An additional special case: an unconstrained type in an object
5204 -- declaration may have been rewritten as a local subtype constrained
5205 -- by the expression in the declaration. We need to recover the
5206 -- original entity which may be global.
5208 if Present (Original_Node (Assoc))
5209 and then Nkind (Parent (N)) = N_Object_Declaration
5211 Assoc := Original_Node (Assoc);
5216 end Get_Associated_Node;
5218 -------------------------------------------
5219 -- Build_Instance_Compilation_Unit_Nodes --
5220 -------------------------------------------
5222 procedure Build_Instance_Compilation_Unit_Nodes
5227 Decl_Cunit : Node_Id;
5228 Body_Cunit : Node_Id;
5230 New_Main : constant Entity_Id := Defining_Entity (Act_Decl);
5231 Old_Main : constant Entity_Id := Cunit_Entity (Main_Unit);
5234 -- A new compilation unit node is built for the instance declaration
5237 Make_Compilation_Unit (Sloc (N),
5238 Context_Items => Empty_List,
5240 Aux_Decls_Node => Make_Compilation_Unit_Aux (Sloc (N)));
5242 Set_Parent_Spec (Act_Decl, Parent_Spec (N));
5244 -- The new compilation unit is linked to its body, but both share the
5245 -- same file, so we do not set Body_Required on the new unit so as not
5246 -- to create a spurious dependency on a non-existent body in the ali.
5247 -- This simplifies CodePeer unit traversal.
5249 -- We use the original instantiation compilation unit as the resulting
5250 -- compilation unit of the instance, since this is the main unit.
5252 Rewrite (N, Act_Body);
5254 -- Propagate the aspect specifications from the package body template to
5255 -- the instantiated version of the package body.
5257 if Has_Aspects (Act_Body) then
5258 Set_Aspect_Specifications
5259 (N, New_Copy_List_Tree (Aspect_Specifications (Act_Body)));
5262 Body_Cunit := Parent (N);
5264 -- The two compilation unit nodes are linked by the Library_Unit field
5266 Set_Library_Unit (Decl_Cunit, Body_Cunit);
5267 Set_Library_Unit (Body_Cunit, Decl_Cunit);
5269 -- Preserve the private nature of the package if needed
5271 Set_Private_Present (Decl_Cunit, Private_Present (Body_Cunit));
5273 -- If the instance is not the main unit, its context, categorization
5274 -- and elaboration entity are not relevant to the compilation.
5276 if Body_Cunit /= Cunit (Main_Unit) then
5277 Make_Instance_Unit (Body_Cunit, In_Main => False);
5281 -- The context clause items on the instantiation, which are now attached
5282 -- to the body compilation unit (since the body overwrote the original
5283 -- instantiation node), semantically belong on the spec, so copy them
5284 -- there. It's harmless to leave them on the body as well. In fact one
5285 -- could argue that they belong in both places.
5287 Citem := First (Context_Items (Body_Cunit));
5288 while Present (Citem) loop
5289 Append (New_Copy (Citem), Context_Items (Decl_Cunit));
5293 -- Propagate categorization flags on packages, so that they appear in
5294 -- the ali file for the spec of the unit.
5296 if Ekind (New_Main) = E_Package then
5297 Set_Is_Pure (Old_Main, Is_Pure (New_Main));
5298 Set_Is_Preelaborated (Old_Main, Is_Preelaborated (New_Main));
5299 Set_Is_Remote_Types (Old_Main, Is_Remote_Types (New_Main));
5300 Set_Is_Shared_Passive (Old_Main, Is_Shared_Passive (New_Main));
5301 Set_Is_Remote_Call_Interface
5302 (Old_Main, Is_Remote_Call_Interface (New_Main));
5305 -- Make entry in Units table, so that binder can generate call to
5306 -- elaboration procedure for body, if any.
5308 Make_Instance_Unit (Body_Cunit, In_Main => True);
5309 Main_Unit_Entity := New_Main;
5310 Set_Cunit_Entity (Main_Unit, Main_Unit_Entity);
5312 -- Build elaboration entity, since the instance may certainly generate
5313 -- elaboration code requiring a flag for protection.
5315 Build_Elaboration_Entity (Decl_Cunit, New_Main);
5316 end Build_Instance_Compilation_Unit_Nodes;
5318 -----------------------------
5319 -- Check_Access_Definition --
5320 -----------------------------
5322 procedure Check_Access_Definition (N : Node_Id) is
5325 (Ada_Version >= Ada_2005 and then Present (Access_Definition (N)));
5327 end Check_Access_Definition;
5329 -----------------------------------
5330 -- Check_Formal_Package_Instance --
5331 -----------------------------------
5333 -- If the formal has specific parameters, they must match those of the
5334 -- actual. Both of them are instances, and the renaming declarations for
5335 -- their formal parameters appear in the same order in both. The analyzed
5336 -- formal has been analyzed in the context of the current instance.
5338 procedure Check_Formal_Package_Instance
5339 (Formal_Pack : Entity_Id;
5340 Actual_Pack : Entity_Id)
5342 E1 : Entity_Id := First_Entity (Actual_Pack);
5343 E2 : Entity_Id := First_Entity (Formal_Pack);
5348 procedure Check_Mismatch (B : Boolean);
5349 -- Common error routine for mismatch between the parameters of the
5350 -- actual instance and those of the formal package.
5352 function Same_Instantiated_Constant (E1, E2 : Entity_Id) return Boolean;
5353 -- The formal may come from a nested formal package, and the actual may
5354 -- have been constant-folded. To determine whether the two denote the
5355 -- same entity we may have to traverse several definitions to recover
5356 -- the ultimate entity that they refer to.
5358 function Same_Instantiated_Variable (E1, E2 : Entity_Id) return Boolean;
5359 -- Similarly, if the formal comes from a nested formal package, the
5360 -- actual may designate the formal through multiple renamings, which
5361 -- have to be followed to determine the original variable in question.
5363 --------------------
5364 -- Check_Mismatch --
5365 --------------------
5367 procedure Check_Mismatch (B : Boolean) is
5368 Kind : constant Node_Kind := Nkind (Parent (E2));
5371 if Kind = N_Formal_Type_Declaration then
5374 elsif Nkind_In (Kind, N_Formal_Object_Declaration,
5375 N_Formal_Package_Declaration)
5376 or else Kind in N_Formal_Subprogram_Declaration
5382 ("actual for & in actual instance does not match formal",
5383 Parent (Actual_Pack), E1);
5387 --------------------------------
5388 -- Same_Instantiated_Constant --
5389 --------------------------------
5391 function Same_Instantiated_Constant
5392 (E1, E2 : Entity_Id) return Boolean
5398 while Present (Ent) loop
5402 elsif Ekind (Ent) /= E_Constant then
5405 elsif Is_Entity_Name (Constant_Value (Ent)) then
5406 if Entity (Constant_Value (Ent)) = E1 then
5409 Ent := Entity (Constant_Value (Ent));
5412 -- The actual may be a constant that has been folded. Recover
5415 elsif Is_Entity_Name (Original_Node (Constant_Value (Ent))) then
5416 Ent := Entity (Original_Node (Constant_Value (Ent)));
5423 end Same_Instantiated_Constant;
5425 --------------------------------
5426 -- Same_Instantiated_Variable --
5427 --------------------------------
5429 function Same_Instantiated_Variable
5430 (E1, E2 : Entity_Id) return Boolean
5432 function Original_Entity (E : Entity_Id) return Entity_Id;
5433 -- Follow chain of renamings to the ultimate ancestor
5435 ---------------------
5436 -- Original_Entity --
5437 ---------------------
5439 function Original_Entity (E : Entity_Id) return Entity_Id is
5444 while Nkind (Parent (Orig)) = N_Object_Renaming_Declaration
5445 and then Present (Renamed_Object (Orig))
5446 and then Is_Entity_Name (Renamed_Object (Orig))
5448 Orig := Entity (Renamed_Object (Orig));
5452 end Original_Entity;
5454 -- Start of processing for Same_Instantiated_Variable
5457 return Ekind (E1) = Ekind (E2)
5458 and then Original_Entity (E1) = Original_Entity (E2);
5459 end Same_Instantiated_Variable;
5461 -- Start of processing for Check_Formal_Package_Instance
5465 and then Present (E2)
5467 exit when Ekind (E1) = E_Package
5468 and then Renamed_Entity (E1) = Renamed_Entity (Actual_Pack);
5470 -- If the formal is the renaming of the formal package, this
5471 -- is the end of its formal part, which may occur before the
5472 -- end of the formal part in the actual in the presence of
5473 -- defaulted parameters in the formal package.
5475 exit when Nkind (Parent (E2)) = N_Package_Renaming_Declaration
5476 and then Renamed_Entity (E2) = Scope (E2);
5478 -- The analysis of the actual may generate additional internal
5479 -- entities. If the formal is defaulted, there is no corresponding
5480 -- analysis and the internal entities must be skipped, until we
5481 -- find corresponding entities again.
5483 if Comes_From_Source (E2)
5484 and then not Comes_From_Source (E1)
5485 and then Chars (E1) /= Chars (E2)
5488 and then Chars (E1) /= Chars (E2)
5497 -- If the formal entity comes from a formal declaration, it was
5498 -- defaulted in the formal package, and no check is needed on it.
5500 elsif Nkind (Parent (E2)) = N_Formal_Object_Declaration then
5503 -- Ditto for defaulted formal subprograms.
5505 elsif Is_Overloadable (E1)
5506 and then Nkind (Unit_Declaration_Node (E2)) in
5507 N_Formal_Subprogram_Declaration
5511 elsif Is_Type (E1) then
5513 -- Subtypes must statically match. E1, E2 are the local entities
5514 -- that are subtypes of the actuals. Itypes generated for other
5515 -- parameters need not be checked, the check will be performed
5516 -- on the parameters themselves.
5518 -- If E2 is a formal type declaration, it is a defaulted parameter
5519 -- and needs no checking.
5521 if not Is_Itype (E1)
5522 and then not Is_Itype (E2)
5526 or else Etype (E1) /= Etype (E2)
5527 or else not Subtypes_Statically_Match (E1, E2));
5530 elsif Ekind (E1) = E_Constant then
5532 -- IN parameters must denote the same static value, or the same
5533 -- constant, or the literal null.
5535 Expr1 := Expression (Parent (E1));
5537 if Ekind (E2) /= E_Constant then
5538 Check_Mismatch (True);
5541 Expr2 := Expression (Parent (E2));
5544 if Is_OK_Static_Expression (Expr1) then
5545 if not Is_OK_Static_Expression (Expr2) then
5546 Check_Mismatch (True);
5548 elsif Is_Discrete_Type (Etype (E1)) then
5550 V1 : constant Uint := Expr_Value (Expr1);
5551 V2 : constant Uint := Expr_Value (Expr2);
5553 Check_Mismatch (V1 /= V2);
5556 elsif Is_Real_Type (Etype (E1)) then
5558 V1 : constant Ureal := Expr_Value_R (Expr1);
5559 V2 : constant Ureal := Expr_Value_R (Expr2);
5561 Check_Mismatch (V1 /= V2);
5564 elsif Is_String_Type (Etype (E1))
5565 and then Nkind (Expr1) = N_String_Literal
5567 if Nkind (Expr2) /= N_String_Literal then
5568 Check_Mismatch (True);
5571 (not String_Equal (Strval (Expr1), Strval (Expr2)));
5575 elsif Is_Entity_Name (Expr1) then
5576 if Is_Entity_Name (Expr2) then
5577 if Entity (Expr1) = Entity (Expr2) then
5581 (not Same_Instantiated_Constant
5582 (Entity (Expr1), Entity (Expr2)));
5585 Check_Mismatch (True);
5588 elsif Is_Entity_Name (Original_Node (Expr1))
5589 and then Is_Entity_Name (Expr2)
5591 Same_Instantiated_Constant
5592 (Entity (Original_Node (Expr1)), Entity (Expr2))
5596 elsif Nkind (Expr1) = N_Null then
5597 Check_Mismatch (Nkind (Expr1) /= N_Null);
5600 Check_Mismatch (True);
5603 elsif Ekind (E1) = E_Variable then
5604 Check_Mismatch (not Same_Instantiated_Variable (E1, E2));
5606 elsif Ekind (E1) = E_Package then
5608 (Ekind (E1) /= Ekind (E2)
5609 or else Renamed_Object (E1) /= Renamed_Object (E2));
5611 elsif Is_Overloadable (E1) then
5613 -- Verify that the actual subprograms match. Note that actuals
5614 -- that are attributes are rewritten as subprograms. If the
5615 -- subprogram in the formal package is defaulted, no check is
5616 -- needed. Note that this can only happen in Ada 2005 when the
5617 -- formal package can be partially parameterized.
5619 if Nkind (Unit_Declaration_Node (E1)) =
5620 N_Subprogram_Renaming_Declaration
5621 and then From_Default (Unit_Declaration_Node (E1))
5625 -- If the formal package has an "others" box association that
5626 -- covers this formal, there is no need for a check either.
5628 elsif Nkind (Unit_Declaration_Node (E2)) in
5629 N_Formal_Subprogram_Declaration
5630 and then Box_Present (Unit_Declaration_Node (E2))
5634 -- No check needed if subprogram is a defaulted null procedure
5636 elsif No (Alias (E2))
5637 and then Ekind (E2) = E_Procedure
5639 Null_Present (Specification (Unit_Declaration_Node (E2)))
5643 -- Otherwise the actual in the formal and the actual in the
5644 -- instantiation of the formal must match, up to renamings.
5648 (Ekind (E2) /= Ekind (E1) or else (Alias (E1)) /= Alias (E2));
5652 raise Program_Error;
5659 end Check_Formal_Package_Instance;
5661 ---------------------------
5662 -- Check_Formal_Packages --
5663 ---------------------------
5665 procedure Check_Formal_Packages (P_Id : Entity_Id) is
5667 Formal_P : Entity_Id;
5670 -- Iterate through the declarations in the instance, looking for package
5671 -- renaming declarations that denote instances of formal packages. Stop
5672 -- when we find the renaming of the current package itself. The
5673 -- declaration for a formal package without a box is followed by an
5674 -- internal entity that repeats the instantiation.
5676 E := First_Entity (P_Id);
5677 while Present (E) loop
5678 if Ekind (E) = E_Package then
5679 if Renamed_Object (E) = P_Id then
5682 elsif Nkind (Parent (E)) /= N_Package_Renaming_Declaration then
5685 elsif not Box_Present (Parent (Associated_Formal_Package (E))) then
5686 Formal_P := Next_Entity (E);
5687 Check_Formal_Package_Instance (Formal_P, E);
5689 -- After checking, remove the internal validating package. It
5690 -- is only needed for semantic checks, and as it may contain
5691 -- generic formal declarations it should not reach gigi.
5693 Remove (Unit_Declaration_Node (Formal_P));
5699 end Check_Formal_Packages;
5701 ---------------------------------
5702 -- Check_Forward_Instantiation --
5703 ---------------------------------
5705 procedure Check_Forward_Instantiation (Decl : Node_Id) is
5707 Gen_Comp : Entity_Id := Cunit_Entity (Get_Source_Unit (Decl));
5710 -- The instantiation appears before the generic body if we are in the
5711 -- scope of the unit containing the generic, either in its spec or in
5712 -- the package body, and before the generic body.
5714 if Ekind (Gen_Comp) = E_Package_Body then
5715 Gen_Comp := Spec_Entity (Gen_Comp);
5718 if In_Open_Scopes (Gen_Comp)
5719 and then No (Corresponding_Body (Decl))
5724 and then not Is_Compilation_Unit (S)
5725 and then not Is_Child_Unit (S)
5727 if Ekind (S) = E_Package then
5728 Set_Has_Forward_Instantiation (S);
5734 end Check_Forward_Instantiation;
5736 ---------------------------
5737 -- Check_Generic_Actuals --
5738 ---------------------------
5740 -- The visibility of the actuals may be different between the point of
5741 -- generic instantiation and the instantiation of the body.
5743 procedure Check_Generic_Actuals
5744 (Instance : Entity_Id;
5745 Is_Formal_Box : Boolean)
5750 function Denotes_Previous_Actual (Typ : Entity_Id) return Boolean;
5751 -- For a formal that is an array type, the component type is often a
5752 -- previous formal in the same unit. The privacy status of the component
5753 -- type will have been examined earlier in the traversal of the
5754 -- corresponding actuals, and this status should not be modified for
5755 -- the array (sub)type itself. However, if the base type of the array
5756 -- (sub)type is private, its full view must be restored in the body to
5757 -- be consistent with subsequent index subtypes, etc.
5759 -- To detect this case we have to rescan the list of formals, which is
5760 -- usually short enough to ignore the resulting inefficiency.
5762 -----------------------------
5763 -- Denotes_Previous_Actual --
5764 -----------------------------
5766 function Denotes_Previous_Actual (Typ : Entity_Id) return Boolean is
5770 Prev := First_Entity (Instance);
5771 while Present (Prev) loop
5773 and then Nkind (Parent (Prev)) = N_Subtype_Declaration
5774 and then Is_Entity_Name (Subtype_Indication (Parent (Prev)))
5775 and then Entity (Subtype_Indication (Parent (Prev))) = Typ
5788 end Denotes_Previous_Actual;
5790 -- Start of processing for Check_Generic_Actuals
5793 E := First_Entity (Instance);
5794 while Present (E) loop
5796 and then Nkind (Parent (E)) = N_Subtype_Declaration
5797 and then Scope (Etype (E)) /= Instance
5798 and then Is_Entity_Name (Subtype_Indication (Parent (E)))
5800 if Is_Array_Type (E)
5801 and then not Is_Private_Type (Etype (E))
5802 and then Denotes_Previous_Actual (Component_Type (E))
5806 Check_Private_View (Subtype_Indication (Parent (E)));
5809 Set_Is_Generic_Actual_Type (E, True);
5810 Set_Is_Hidden (E, False);
5811 Set_Is_Potentially_Use_Visible (E,
5814 -- We constructed the generic actual type as a subtype of the
5815 -- supplied type. This means that it normally would not inherit
5816 -- subtype specific attributes of the actual, which is wrong for
5817 -- the generic case.
5819 Astype := Ancestor_Subtype (E);
5823 -- This can happen when E is an itype that is the full view of
5824 -- a private type completed, e.g. with a constrained array. In
5825 -- that case, use the first subtype, which will carry size
5826 -- information. The base type itself is unconstrained and will
5829 Astype := First_Subtype (E);
5832 Set_Size_Info (E, (Astype));
5833 Set_RM_Size (E, RM_Size (Astype));
5834 Set_First_Rep_Item (E, First_Rep_Item (Astype));
5836 if Is_Discrete_Or_Fixed_Point_Type (E) then
5837 Set_RM_Size (E, RM_Size (Astype));
5839 -- In nested instances, the base type of an access actual may
5840 -- itself be private, and need to be exchanged.
5842 elsif Is_Access_Type (E)
5843 and then Is_Private_Type (Etype (E))
5846 (New_Occurrence_Of (Etype (E), Sloc (Instance)));
5849 elsif Ekind (E) = E_Package then
5851 -- If this is the renaming for the current instance, we're done.
5852 -- Otherwise it is a formal package. If the corresponding formal
5853 -- was declared with a box, the (instantiations of the) generic
5854 -- formal part are also visible. Otherwise, ignore the entity
5855 -- created to validate the actuals.
5857 if Renamed_Object (E) = Instance then
5860 elsif Nkind (Parent (E)) /= N_Package_Renaming_Declaration then
5863 -- The visibility of a formal of an enclosing generic is already
5866 elsif Denotes_Formal_Package (E) then
5869 elsif Present (Associated_Formal_Package (E))
5870 and then not Is_Generic_Formal (E)
5872 if Box_Present (Parent (Associated_Formal_Package (E))) then
5873 Check_Generic_Actuals (Renamed_Object (E), True);
5876 Check_Generic_Actuals (Renamed_Object (E), False);
5879 Set_Is_Hidden (E, False);
5882 -- If this is a subprogram instance (in a wrapper package) the
5883 -- actual is fully visible.
5885 elsif Is_Wrapper_Package (Instance) then
5886 Set_Is_Hidden (E, False);
5888 -- If the formal package is declared with a box, or if the formal
5889 -- parameter is defaulted, it is visible in the body.
5892 or else Is_Visible_Formal (E)
5894 Set_Is_Hidden (E, False);
5897 if Ekind (E) = E_Constant then
5899 -- If the type of the actual is a private type declared in the
5900 -- enclosing scope of the generic unit, the body of the generic
5901 -- sees the full view of the type (because it has to appear in
5902 -- the corresponding package body). If the type is private now,
5903 -- exchange views to restore the proper visiblity in the instance.
5906 Typ : constant Entity_Id := Base_Type (Etype (E));
5907 -- The type of the actual
5912 Parent_Scope : Entity_Id;
5913 -- The enclosing scope of the generic unit
5916 if Is_Wrapper_Package (Instance) then
5920 (Unit_Declaration_Node
5921 (Related_Instance (Instance))));
5924 Generic_Parent (Package_Specification (Instance));
5927 Parent_Scope := Scope (Gen_Id);
5929 -- The exchange is only needed if the generic is defined
5930 -- within a package which is not a common ancestor of the
5931 -- scope of the instance, and is not already in scope.
5933 if Is_Private_Type (Typ)
5934 and then Scope (Typ) = Parent_Scope
5935 and then Scope (Instance) /= Parent_Scope
5936 and then Ekind (Parent_Scope) = E_Package
5937 and then not Is_Child_Unit (Gen_Id)
5941 -- If the type of the entity is a subtype, it may also have
5942 -- to be made visible, together with the base type of its
5943 -- full view, after exchange.
5945 if Is_Private_Type (Etype (E)) then
5946 Switch_View (Etype (E));
5947 Switch_View (Base_Type (Etype (E)));
5955 end Check_Generic_Actuals;
5957 ------------------------------
5958 -- Check_Generic_Child_Unit --
5959 ------------------------------
5961 procedure Check_Generic_Child_Unit
5963 Parent_Installed : in out Boolean)
5965 Loc : constant Source_Ptr := Sloc (Gen_Id);
5966 Gen_Par : Entity_Id := Empty;
5968 Inst_Par : Entity_Id;
5971 function Find_Generic_Child
5973 Id : Node_Id) return Entity_Id;
5974 -- Search generic parent for possible child unit with the given name
5976 function In_Enclosing_Instance return Boolean;
5977 -- Within an instance of the parent, the child unit may be denoted by
5978 -- a simple name, or an abbreviated expanded name. Examine enclosing
5979 -- scopes to locate a possible parent instantiation.
5981 ------------------------
5982 -- Find_Generic_Child --
5983 ------------------------
5985 function Find_Generic_Child
5987 Id : Node_Id) return Entity_Id
5992 -- If entity of name is already set, instance has already been
5993 -- resolved, e.g. in an enclosing instantiation.
5995 if Present (Entity (Id)) then
5996 if Scope (Entity (Id)) = Scop then
6003 E := First_Entity (Scop);
6004 while Present (E) loop
6005 if Chars (E) = Chars (Id)
6006 and then Is_Child_Unit (E)
6008 if Is_Child_Unit (E)
6009 and then not Is_Visible_Lib_Unit (E)
6012 ("generic child unit& is not visible", Gen_Id, E);
6024 end Find_Generic_Child;
6026 ---------------------------
6027 -- In_Enclosing_Instance --
6028 ---------------------------
6030 function In_Enclosing_Instance return Boolean is
6031 Enclosing_Instance : Node_Id;
6032 Instance_Decl : Node_Id;
6035 -- We do not inline any call that contains instantiations, except
6036 -- for instantiations of Unchecked_Conversion, so if we are within
6037 -- an inlined body the current instance does not require parents.
6039 if In_Inlined_Body then
6040 pragma Assert (Chars (Gen_Id) = Name_Unchecked_Conversion);
6044 -- Loop to check enclosing scopes
6046 Enclosing_Instance := Current_Scope;
6047 while Present (Enclosing_Instance) loop
6048 Instance_Decl := Unit_Declaration_Node (Enclosing_Instance);
6050 if Ekind (Enclosing_Instance) = E_Package
6051 and then Is_Generic_Instance (Enclosing_Instance)
6053 (Generic_Parent (Specification (Instance_Decl)))
6055 -- Check whether the generic we are looking for is a child of
6058 E := Find_Generic_Child
6059 (Generic_Parent (Specification (Instance_Decl)), Gen_Id);
6060 exit when Present (E);
6066 Enclosing_Instance := Scope (Enclosing_Instance);
6078 Make_Expanded_Name (Loc,
6080 Prefix => New_Occurrence_Of (Enclosing_Instance, Loc),
6081 Selector_Name => New_Occurrence_Of (E, Loc)));
6083 Set_Entity (Gen_Id, E);
6084 Set_Etype (Gen_Id, Etype (E));
6085 Parent_Installed := False; -- Already in scope.
6088 end In_Enclosing_Instance;
6090 -- Start of processing for Check_Generic_Child_Unit
6093 -- If the name of the generic is given by a selected component, it may
6094 -- be the name of a generic child unit, and the prefix is the name of an
6095 -- instance of the parent, in which case the child unit must be visible.
6096 -- If this instance is not in scope, it must be placed there and removed
6097 -- after instantiation, because what is being instantiated is not the
6098 -- original child, but the corresponding child present in the instance
6101 -- If the child is instantiated within the parent, it can be given by
6102 -- a simple name. In this case the instance is already in scope, but
6103 -- the child generic must be recovered from the generic parent as well.
6105 if Nkind (Gen_Id) = N_Selected_Component then
6106 S := Selector_Name (Gen_Id);
6107 Analyze (Prefix (Gen_Id));
6108 Inst_Par := Entity (Prefix (Gen_Id));
6110 if Ekind (Inst_Par) = E_Package
6111 and then Present (Renamed_Object (Inst_Par))
6113 Inst_Par := Renamed_Object (Inst_Par);
6116 if Ekind (Inst_Par) = E_Package then
6117 if Nkind (Parent (Inst_Par)) = N_Package_Specification then
6118 Gen_Par := Generic_Parent (Parent (Inst_Par));
6120 elsif Nkind (Parent (Inst_Par)) = N_Defining_Program_Unit_Name
6122 Nkind (Parent (Parent (Inst_Par))) = N_Package_Specification
6124 Gen_Par := Generic_Parent (Parent (Parent (Inst_Par)));
6127 elsif Ekind (Inst_Par) = E_Generic_Package
6128 and then Nkind (Parent (Gen_Id)) = N_Formal_Package_Declaration
6130 -- A formal package may be a real child package, and not the
6131 -- implicit instance within a parent. In this case the child is
6132 -- not visible and has to be retrieved explicitly as well.
6134 Gen_Par := Inst_Par;
6137 if Present (Gen_Par) then
6139 -- The prefix denotes an instantiation. The entity itself may be a
6140 -- nested generic, or a child unit.
6142 E := Find_Generic_Child (Gen_Par, S);
6145 Change_Selected_Component_To_Expanded_Name (Gen_Id);
6146 Set_Entity (Gen_Id, E);
6147 Set_Etype (Gen_Id, Etype (E));
6149 Set_Etype (S, Etype (E));
6151 -- Indicate that this is a reference to the parent
6153 if In_Extended_Main_Source_Unit (Gen_Id) then
6154 Set_Is_Instantiated (Inst_Par);
6157 -- A common mistake is to replicate the naming scheme of a
6158 -- hierarchy by instantiating a generic child directly, rather
6159 -- than the implicit child in a parent instance:
6161 -- generic .. package Gpar is ..
6162 -- generic .. package Gpar.Child is ..
6163 -- package Par is new Gpar ();
6166 -- package Par.Child is new Gpar.Child ();
6167 -- rather than Par.Child
6169 -- In this case the instantiation is within Par, which is an
6170 -- instance, but Gpar does not denote Par because we are not IN
6171 -- the instance of Gpar, so this is illegal. The test below
6172 -- recognizes this particular case.
6174 if Is_Child_Unit (E)
6175 and then not Comes_From_Source (Entity (Prefix (Gen_Id)))
6176 and then (not In_Instance
6177 or else Nkind (Parent (Parent (Gen_Id))) =
6181 ("prefix of generic child unit must be instance of parent",
6185 if not In_Open_Scopes (Inst_Par)
6186 and then Nkind (Parent (Gen_Id)) not in
6187 N_Generic_Renaming_Declaration
6189 Install_Parent (Inst_Par);
6190 Parent_Installed := True;
6192 elsif In_Open_Scopes (Inst_Par) then
6194 -- If the parent is already installed, install the actuals
6195 -- for its formal packages. This is necessary when the child
6196 -- instance is a child of the parent instance: in this case,
6197 -- the parent is placed on the scope stack but the formal
6198 -- packages are not made visible.
6200 Install_Formal_Packages (Inst_Par);
6204 -- If the generic parent does not contain an entity that
6205 -- corresponds to the selector, the instance doesn't either.
6206 -- Analyzing the node will yield the appropriate error message.
6207 -- If the entity is not a child unit, then it is an inner
6208 -- generic in the parent.
6216 if Is_Child_Unit (Entity (Gen_Id))
6218 Nkind (Parent (Gen_Id)) not in N_Generic_Renaming_Declaration
6219 and then not In_Open_Scopes (Inst_Par)
6221 Install_Parent (Inst_Par);
6222 Parent_Installed := True;
6224 -- The generic unit may be the renaming of the implicit child
6225 -- present in an instance. In that case the parent instance is
6226 -- obtained from the name of the renamed entity.
6228 elsif Ekind (Entity (Gen_Id)) = E_Generic_Package
6229 and then Present (Renamed_Entity (Entity (Gen_Id)))
6230 and then Is_Child_Unit (Renamed_Entity (Entity (Gen_Id)))
6233 Renamed_Package : constant Node_Id :=
6234 Name (Parent (Entity (Gen_Id)));
6236 if Nkind (Renamed_Package) = N_Expanded_Name then
6237 Inst_Par := Entity (Prefix (Renamed_Package));
6238 Install_Parent (Inst_Par);
6239 Parent_Installed := True;
6245 elsif Nkind (Gen_Id) = N_Expanded_Name then
6247 -- Entity already present, analyze prefix, whose meaning may be
6248 -- an instance in the current context. If it is an instance of
6249 -- a relative within another, the proper parent may still have
6250 -- to be installed, if they are not of the same generation.
6252 Analyze (Prefix (Gen_Id));
6254 -- In the unlikely case that a local declaration hides the name
6255 -- of the parent package, locate it on the homonym chain. If the
6256 -- context is an instance of the parent, the renaming entity is
6259 Inst_Par := Entity (Prefix (Gen_Id));
6260 while Present (Inst_Par)
6261 and then not Is_Package_Or_Generic_Package (Inst_Par)
6263 Inst_Par := Homonym (Inst_Par);
6266 pragma Assert (Present (Inst_Par));
6267 Set_Entity (Prefix (Gen_Id), Inst_Par);
6269 if In_Enclosing_Instance then
6272 elsif Present (Entity (Gen_Id))
6273 and then Is_Child_Unit (Entity (Gen_Id))
6274 and then not In_Open_Scopes (Inst_Par)
6276 Install_Parent (Inst_Par);
6277 Parent_Installed := True;
6280 elsif In_Enclosing_Instance then
6282 -- The child unit is found in some enclosing scope
6289 -- If this is the renaming of the implicit child in a parent
6290 -- instance, recover the parent name and install it.
6292 if Is_Entity_Name (Gen_Id) then
6293 E := Entity (Gen_Id);
6295 if Is_Generic_Unit (E)
6296 and then Nkind (Parent (E)) in N_Generic_Renaming_Declaration
6297 and then Is_Child_Unit (Renamed_Object (E))
6298 and then Is_Generic_Unit (Scope (Renamed_Object (E)))
6299 and then Nkind (Name (Parent (E))) = N_Expanded_Name
6302 New_Copy_Tree (Name (Parent (E))));
6303 Inst_Par := Entity (Prefix (Gen_Id));
6305 if not In_Open_Scopes (Inst_Par) then
6306 Install_Parent (Inst_Par);
6307 Parent_Installed := True;
6310 -- If it is a child unit of a non-generic parent, it may be
6311 -- use-visible and given by a direct name. Install parent as
6314 elsif Is_Generic_Unit (E)
6315 and then Is_Child_Unit (E)
6317 Nkind (Parent (Gen_Id)) not in N_Generic_Renaming_Declaration
6318 and then not Is_Generic_Unit (Scope (E))
6320 if not In_Open_Scopes (Scope (E)) then
6321 Install_Parent (Scope (E));
6322 Parent_Installed := True;
6327 end Check_Generic_Child_Unit;
6329 -----------------------------
6330 -- Check_Hidden_Child_Unit --
6331 -----------------------------
6333 procedure Check_Hidden_Child_Unit
6335 Gen_Unit : Entity_Id;
6336 Act_Decl_Id : Entity_Id)
6338 Gen_Id : constant Node_Id := Name (N);
6341 if Is_Child_Unit (Gen_Unit)
6342 and then Is_Child_Unit (Act_Decl_Id)
6343 and then Nkind (Gen_Id) = N_Expanded_Name
6344 and then Entity (Prefix (Gen_Id)) = Scope (Act_Decl_Id)
6345 and then Chars (Gen_Unit) = Chars (Act_Decl_Id)
6347 Error_Msg_Node_2 := Scope (Act_Decl_Id);
6349 ("generic unit & is implicitly declared in &",
6350 Defining_Unit_Name (N), Gen_Unit);
6351 Error_Msg_N ("\instance must have different name",
6352 Defining_Unit_Name (N));
6354 end Check_Hidden_Child_Unit;
6356 ------------------------
6357 -- Check_Private_View --
6358 ------------------------
6360 procedure Check_Private_View (N : Node_Id) is
6361 T : constant Entity_Id := Etype (N);
6365 -- Exchange views if the type was not private in the generic but is
6366 -- private at the point of instantiation. Do not exchange views if
6367 -- the scope of the type is in scope. This can happen if both generic
6368 -- and instance are sibling units, or if type is defined in a parent.
6369 -- In this case the visibility of the type will be correct for all
6373 BT := Base_Type (T);
6375 if Is_Private_Type (T)
6376 and then not Has_Private_View (N)
6377 and then Present (Full_View (T))
6378 and then not In_Open_Scopes (Scope (T))
6380 -- In the generic, the full type was visible. Save the private
6381 -- entity, for subsequent exchange.
6385 elsif Has_Private_View (N)
6386 and then not Is_Private_Type (T)
6387 and then not Has_Been_Exchanged (T)
6388 and then Etype (Get_Associated_Node (N)) /= T
6390 -- Only the private declaration was visible in the generic. If
6391 -- the type appears in a subtype declaration, the subtype in the
6392 -- instance must have a view compatible with that of its parent,
6393 -- which must be exchanged (see corresponding code in Restore_
6394 -- Private_Views). Otherwise, if the type is defined in a parent
6395 -- unit, leave full visibility within instance, which is safe.
6397 if In_Open_Scopes (Scope (Base_Type (T)))
6398 and then not Is_Private_Type (Base_Type (T))
6399 and then Comes_From_Source (Base_Type (T))
6403 elsif Nkind (Parent (N)) = N_Subtype_Declaration
6404 or else not In_Private_Part (Scope (Base_Type (T)))
6406 Prepend_Elmt (T, Exchanged_Views);
6407 Exchange_Declarations (Etype (Get_Associated_Node (N)));
6410 -- For composite types with inconsistent representation exchange
6411 -- component types accordingly.
6413 elsif Is_Access_Type (T)
6414 and then Is_Private_Type (Designated_Type (T))
6415 and then not Has_Private_View (N)
6416 and then Present (Full_View (Designated_Type (T)))
6418 Switch_View (Designated_Type (T));
6420 elsif Is_Array_Type (T) then
6421 if Is_Private_Type (Component_Type (T))
6422 and then not Has_Private_View (N)
6423 and then Present (Full_View (Component_Type (T)))
6425 Switch_View (Component_Type (T));
6428 -- The normal exchange mechanism relies on the setting of a
6429 -- flag on the reference in the generic. However, an additional
6430 -- mechanism is needed for types that are not explicitly
6431 -- mentioned in the generic, but may be needed in expanded code
6432 -- in the instance. This includes component types of arrays and
6433 -- designated types of access types. This processing must also
6434 -- include the index types of arrays which we take care of here.
6441 Indx := First_Index (T);
6442 while Present (Indx) loop
6443 Typ := Base_Type (Etype (Indx));
6445 if Is_Private_Type (Typ)
6446 and then Present (Full_View (Typ))
6455 elsif Is_Private_Type (T)
6456 and then Present (Full_View (T))
6457 and then Is_Array_Type (Full_View (T))
6458 and then Is_Private_Type (Component_Type (Full_View (T)))
6462 -- Finally, a non-private subtype may have a private base type, which
6463 -- must be exchanged for consistency. This can happen when a package
6464 -- body is instantiated, when the scope stack is empty but in fact
6465 -- the subtype and the base type are declared in an enclosing scope.
6467 -- Note that in this case we introduce an inconsistency in the view
6468 -- set, because we switch the base type BT, but there could be some
6469 -- private dependent subtypes of BT which remain unswitched. Such
6470 -- subtypes might need to be switched at a later point (see specific
6471 -- provision for that case in Switch_View).
6473 elsif not Is_Private_Type (T)
6474 and then not Has_Private_View (N)
6475 and then Is_Private_Type (BT)
6476 and then Present (Full_View (BT))
6477 and then not Is_Generic_Type (BT)
6478 and then not In_Open_Scopes (BT)
6480 Prepend_Elmt (Full_View (BT), Exchanged_Views);
6481 Exchange_Declarations (BT);
6484 end Check_Private_View;
6486 -----------------------------
6487 -- Check_Hidden_Primitives --
6488 -----------------------------
6490 function Check_Hidden_Primitives (Assoc_List : List_Id) return Elist_Id is
6493 Result : Elist_Id := No_Elist;
6496 if No (Assoc_List) then
6500 -- Traverse the list of associations between formals and actuals
6501 -- searching for renamings of tagged types
6503 Actual := First (Assoc_List);
6504 while Present (Actual) loop
6505 if Nkind (Actual) = N_Subtype_Declaration then
6506 Gen_T := Generic_Parent_Type (Actual);
6509 and then Is_Tagged_Type (Gen_T)
6511 -- Traverse the list of primitives of the actual types
6512 -- searching for hidden primitives that are visible in the
6513 -- corresponding generic formal; leave them visible and
6514 -- append them to Result to restore their decoration later.
6516 Install_Hidden_Primitives
6517 (Prims_List => Result,
6519 Act_T => Entity (Subtype_Indication (Actual)));
6527 end Check_Hidden_Primitives;
6529 --------------------------
6530 -- Contains_Instance_Of --
6531 --------------------------
6533 function Contains_Instance_Of
6536 N : Node_Id) return Boolean
6544 -- Verify that there are no circular instantiations. We check whether
6545 -- the unit contains an instance of the current scope or some enclosing
6546 -- scope (in case one of the instances appears in a subunit). Longer
6547 -- circularities involving subunits might seem too pathological to
6548 -- consider, but they were not too pathological for the authors of
6549 -- DEC bc30vsq, so we loop over all enclosing scopes, and mark all
6550 -- enclosing generic scopes as containing an instance.
6553 -- Within a generic subprogram body, the scope is not generic, to
6554 -- allow for recursive subprograms. Use the declaration to determine
6555 -- whether this is a generic unit.
6557 if Ekind (Scop) = E_Generic_Package
6558 or else (Is_Subprogram (Scop)
6559 and then Nkind (Unit_Declaration_Node (Scop)) =
6560 N_Generic_Subprogram_Declaration)
6562 Elmt := First_Elmt (Inner_Instances (Inner));
6564 while Present (Elmt) loop
6565 if Node (Elmt) = Scop then
6566 Error_Msg_Node_2 := Inner;
6568 ("circular Instantiation: & instantiated within &!",
6572 elsif Node (Elmt) = Inner then
6575 elsif Contains_Instance_Of (Node (Elmt), Scop, N) then
6576 Error_Msg_Node_2 := Inner;
6578 ("circular Instantiation: & instantiated within &!",
6586 -- Indicate that Inner is being instantiated within Scop
6588 Append_Elmt (Inner, Inner_Instances (Scop));
6591 if Scop = Standard_Standard then
6594 Scop := Scope (Scop);
6599 end Contains_Instance_Of;
6601 -----------------------
6602 -- Copy_Generic_Node --
6603 -----------------------
6605 function Copy_Generic_Node
6607 Parent_Id : Node_Id;
6608 Instantiating : Boolean) return Node_Id
6613 function Copy_Generic_Descendant (D : Union_Id) return Union_Id;
6614 -- Check the given value of one of the Fields referenced by the current
6615 -- node to determine whether to copy it recursively. The field may hold
6616 -- a Node_Id, a List_Id, or an Elist_Id, or a plain value (Sloc, Uint,
6617 -- Char) in which case it need not be copied.
6619 procedure Copy_Descendants;
6620 -- Common utility for various nodes
6622 function Copy_Generic_Elist (E : Elist_Id) return Elist_Id;
6623 -- Make copy of element list
6625 function Copy_Generic_List
6627 Parent_Id : Node_Id) return List_Id;
6628 -- Apply Copy_Node recursively to the members of a node list
6630 function In_Defining_Unit_Name (Nam : Node_Id) return Boolean;
6631 -- True if an identifier is part of the defining program unit name of
6632 -- a child unit. The entity of such an identifier must be kept (for
6633 -- ASIS use) even though as the name of an enclosing generic it would
6634 -- otherwise not be preserved in the generic tree.
6636 ----------------------
6637 -- Copy_Descendants --
6638 ----------------------
6640 procedure Copy_Descendants is
6642 use Atree.Unchecked_Access;
6643 -- This code section is part of the implementation of an untyped
6644 -- tree traversal, so it needs direct access to node fields.
6647 Set_Field1 (New_N, Copy_Generic_Descendant (Field1 (N)));
6648 Set_Field2 (New_N, Copy_Generic_Descendant (Field2 (N)));
6649 Set_Field3 (New_N, Copy_Generic_Descendant (Field3 (N)));
6650 Set_Field4 (New_N, Copy_Generic_Descendant (Field4 (N)));
6651 Set_Field5 (New_N, Copy_Generic_Descendant (Field5 (N)));
6652 end Copy_Descendants;
6654 -----------------------------
6655 -- Copy_Generic_Descendant --
6656 -----------------------------
6658 function Copy_Generic_Descendant (D : Union_Id) return Union_Id is
6660 if D = Union_Id (Empty) then
6663 elsif D in Node_Range then
6665 (Copy_Generic_Node (Node_Id (D), New_N, Instantiating));
6667 elsif D in List_Range then
6668 return Union_Id (Copy_Generic_List (List_Id (D), New_N));
6670 elsif D in Elist_Range then
6671 return Union_Id (Copy_Generic_Elist (Elist_Id (D)));
6673 -- Nothing else is copyable (e.g. Uint values), return as is
6678 end Copy_Generic_Descendant;
6680 ------------------------
6681 -- Copy_Generic_Elist --
6682 ------------------------
6684 function Copy_Generic_Elist (E : Elist_Id) return Elist_Id is
6691 M := First_Elmt (E);
6692 while Present (M) loop
6694 (Copy_Generic_Node (Node (M), Empty, Instantiating), L);
6703 end Copy_Generic_Elist;
6705 -----------------------
6706 -- Copy_Generic_List --
6707 -----------------------
6709 function Copy_Generic_List
6711 Parent_Id : Node_Id) return List_Id
6719 Set_Parent (New_L, Parent_Id);
6722 while Present (N) loop
6723 Append (Copy_Generic_Node (N, Empty, Instantiating), New_L);
6732 end Copy_Generic_List;
6734 ---------------------------
6735 -- In_Defining_Unit_Name --
6736 ---------------------------
6738 function In_Defining_Unit_Name (Nam : Node_Id) return Boolean is
6740 return Present (Parent (Nam))
6741 and then (Nkind (Parent (Nam)) = N_Defining_Program_Unit_Name
6743 (Nkind (Parent (Nam)) = N_Expanded_Name
6744 and then In_Defining_Unit_Name (Parent (Nam))));
6745 end In_Defining_Unit_Name;
6747 -- Start of processing for Copy_Generic_Node
6754 New_N := New_Copy (N);
6756 -- Copy aspects if present
6758 if Has_Aspects (N) then
6759 Set_Has_Aspects (New_N, False);
6760 Set_Aspect_Specifications
6761 (New_N, Copy_Generic_List (Aspect_Specifications (N), Parent_Id));
6764 if Instantiating then
6765 Adjust_Instantiation_Sloc (New_N, S_Adjustment);
6768 if not Is_List_Member (N) then
6769 Set_Parent (New_N, Parent_Id);
6772 -- If defining identifier, then all fields have been copied already
6774 if Nkind (New_N) in N_Entity then
6777 -- Special casing for identifiers and other entity names and operators
6779 elsif Nkind_In (New_N, N_Identifier,
6780 N_Character_Literal,
6783 or else Nkind (New_N) in N_Op
6785 if not Instantiating then
6787 -- Link both nodes in order to assign subsequently the entity of
6788 -- the copy to the original node, in case this is a global
6791 Set_Associated_Node (N, New_N);
6793 -- If we are within an instantiation, this is a nested generic
6794 -- that has already been analyzed at the point of definition.
6795 -- We must preserve references that were global to the enclosing
6796 -- parent at that point. Other occurrences, whether global or
6797 -- local to the current generic, must be resolved anew, so we
6798 -- reset the entity in the generic copy. A global reference has a
6799 -- smaller depth than the parent, or else the same depth in case
6800 -- both are distinct compilation units.
6802 -- A child unit is implicitly declared within the enclosing parent
6803 -- but is in fact global to it, and must be preserved.
6805 -- It is also possible for Current_Instantiated_Parent to be
6806 -- defined, and for this not to be a nested generic, namely if
6807 -- the unit is loaded through Rtsfind. In that case, the entity of
6808 -- New_N is only a link to the associated node, and not a defining
6811 -- The entities for parent units in the defining_program_unit of a
6812 -- generic child unit are established when the context of the unit
6813 -- is first analyzed, before the generic copy is made. They are
6814 -- preserved in the copy for use in ASIS queries.
6816 Ent := Entity (New_N);
6818 if No (Current_Instantiated_Parent.Gen_Id) then
6820 or else Nkind (Ent) /= N_Defining_Identifier
6821 or else not In_Defining_Unit_Name (N)
6823 Set_Associated_Node (New_N, Empty);
6828 not Nkind_In (Ent, N_Defining_Identifier,
6829 N_Defining_Character_Literal,
6830 N_Defining_Operator_Symbol)
6831 or else No (Scope (Ent))
6833 (Scope (Ent) = Current_Instantiated_Parent.Gen_Id
6834 and then not Is_Child_Unit (Ent))
6836 (Scope_Depth (Scope (Ent)) >
6837 Scope_Depth (Current_Instantiated_Parent.Gen_Id)
6839 Get_Source_Unit (Ent) =
6840 Get_Source_Unit (Current_Instantiated_Parent.Gen_Id))
6842 Set_Associated_Node (New_N, Empty);
6845 -- Case of instantiating identifier or some other name or operator
6848 -- If the associated node is still defined, the entity in it
6849 -- is global, and must be copied to the instance. If this copy
6850 -- is being made for a body to inline, it is applied to an
6851 -- instantiated tree, and the entity is already present and
6852 -- must be also preserved.
6855 Assoc : constant Node_Id := Get_Associated_Node (N);
6858 if Present (Assoc) then
6859 if Nkind (Assoc) = Nkind (N) then
6860 Set_Entity (New_N, Entity (Assoc));
6861 Check_Private_View (N);
6863 -- The name in the call may be a selected component if the
6864 -- call has not been analyzed yet, as may be the case for
6865 -- pre/post conditions in a generic unit.
6867 elsif Nkind (Assoc) = N_Function_Call
6868 and then Is_Entity_Name (Name (Assoc))
6870 Set_Entity (New_N, Entity (Name (Assoc)));
6872 elsif Nkind_In (Assoc, N_Defining_Identifier,
6873 N_Defining_Character_Literal,
6874 N_Defining_Operator_Symbol)
6875 and then Expander_Active
6877 -- Inlining case: we are copying a tree that contains
6878 -- global entities, which are preserved in the copy to be
6879 -- used for subsequent inlining.
6884 Set_Entity (New_N, Empty);
6890 -- For expanded name, we must copy the Prefix and Selector_Name
6892 if Nkind (N) = N_Expanded_Name then
6894 (New_N, Copy_Generic_Node (Prefix (N), New_N, Instantiating));
6896 Set_Selector_Name (New_N,
6897 Copy_Generic_Node (Selector_Name (N), New_N, Instantiating));
6899 -- For operators, we must copy the right operand
6901 elsif Nkind (N) in N_Op then
6902 Set_Right_Opnd (New_N,
6903 Copy_Generic_Node (Right_Opnd (N), New_N, Instantiating));
6905 -- And for binary operators, the left operand as well
6907 if Nkind (N) in N_Binary_Op then
6908 Set_Left_Opnd (New_N,
6909 Copy_Generic_Node (Left_Opnd (N), New_N, Instantiating));
6913 -- Special casing for stubs
6915 elsif Nkind (N) in N_Body_Stub then
6917 -- In any case, we must copy the specification or defining
6918 -- identifier as appropriate.
6920 if Nkind (N) = N_Subprogram_Body_Stub then
6921 Set_Specification (New_N,
6922 Copy_Generic_Node (Specification (N), New_N, Instantiating));
6925 Set_Defining_Identifier (New_N,
6927 (Defining_Identifier (N), New_N, Instantiating));
6930 -- If we are not instantiating, then this is where we load and
6931 -- analyze subunits, i.e. at the point where the stub occurs. A
6932 -- more permissive system might defer this analysis to the point
6933 -- of instantiation, but this seems too complicated for now.
6935 if not Instantiating then
6937 Subunit_Name : constant Unit_Name_Type := Get_Unit_Name (N);
6939 Unum : Unit_Number_Type;
6943 -- Make sure that, if it is a subunit of the main unit that is
6944 -- preprocessed and if -gnateG is specified, the preprocessed
6945 -- file will be written.
6947 Lib.Analysing_Subunit_Of_Main :=
6948 Lib.In_Extended_Main_Source_Unit (N);
6951 (Load_Name => Subunit_Name,
6955 Lib.Analysing_Subunit_Of_Main := False;
6957 -- If the proper body is not found, a warning message will be
6958 -- emitted when analyzing the stub, or later at the point of
6959 -- instantiation. Here we just leave the stub as is.
6961 if Unum = No_Unit then
6962 Subunits_Missing := True;
6963 goto Subunit_Not_Found;
6966 Subunit := Cunit (Unum);
6968 if Nkind (Unit (Subunit)) /= N_Subunit then
6970 ("found child unit instead of expected SEPARATE subunit",
6972 Error_Msg_Sloc := Sloc (N);
6973 Error_Msg_N ("\to complete stub #", Subunit);
6974 goto Subunit_Not_Found;
6977 -- We must create a generic copy of the subunit, in order to
6978 -- perform semantic analysis on it, and we must replace the
6979 -- stub in the original generic unit with the subunit, in order
6980 -- to preserve non-local references within.
6982 -- Only the proper body needs to be copied. Library_Unit and
6983 -- context clause are simply inherited by the generic copy.
6984 -- Note that the copy (which may be recursive if there are
6985 -- nested subunits) must be done first, before attaching it to
6986 -- the enclosing generic.
6990 (Proper_Body (Unit (Subunit)),
6991 Empty, Instantiating => False);
6993 -- Now place the original proper body in the original generic
6994 -- unit. This is a body, not a compilation unit.
6996 Rewrite (N, Proper_Body (Unit (Subunit)));
6997 Set_Is_Compilation_Unit (Defining_Entity (N), False);
6998 Set_Was_Originally_Stub (N);
7000 -- Finally replace the body of the subunit with its copy, and
7001 -- make this new subunit into the library unit of the generic
7002 -- copy, which does not have stubs any longer.
7004 Set_Proper_Body (Unit (Subunit), New_Body);
7005 Set_Library_Unit (New_N, Subunit);
7006 Inherit_Context (Unit (Subunit), N);
7009 -- If we are instantiating, this must be an error case, since
7010 -- otherwise we would have replaced the stub node by the proper body
7011 -- that corresponds. So just ignore it in the copy (i.e. we have
7012 -- copied it, and that is good enough).
7018 <<Subunit_Not_Found>> null;
7020 -- If the node is a compilation unit, it is the subunit of a stub, which
7021 -- has been loaded already (see code below). In this case, the library
7022 -- unit field of N points to the parent unit (which is a compilation
7023 -- unit) and need not (and cannot) be copied.
7025 -- When the proper body of the stub is analyzed, the library_unit link
7026 -- is used to establish the proper context (see sem_ch10).
7028 -- The other fields of a compilation unit are copied as usual
7030 elsif Nkind (N) = N_Compilation_Unit then
7032 -- This code can only be executed when not instantiating, because in
7033 -- the copy made for an instantiation, the compilation unit node has
7034 -- disappeared at the point that a stub is replaced by its proper
7037 pragma Assert (not Instantiating);
7039 Set_Context_Items (New_N,
7040 Copy_Generic_List (Context_Items (N), New_N));
7043 Copy_Generic_Node (Unit (N), New_N, False));
7045 Set_First_Inlined_Subprogram (New_N,
7047 (First_Inlined_Subprogram (N), New_N, False));
7049 Set_Aux_Decls_Node (New_N,
7050 Copy_Generic_Node (Aux_Decls_Node (N), New_N, False));
7052 -- For an assignment node, the assignment is known to be semantically
7053 -- legal if we are instantiating the template. This avoids incorrect
7054 -- diagnostics in generated code.
7056 elsif Nkind (N) = N_Assignment_Statement then
7058 -- Copy name and expression fields in usual manner
7061 Copy_Generic_Node (Name (N), New_N, Instantiating));
7063 Set_Expression (New_N,
7064 Copy_Generic_Node (Expression (N), New_N, Instantiating));
7066 if Instantiating then
7067 Set_Assignment_OK (Name (New_N), True);
7070 elsif Nkind_In (N, N_Aggregate, N_Extension_Aggregate) then
7071 if not Instantiating then
7072 Set_Associated_Node (N, New_N);
7075 if Present (Get_Associated_Node (N))
7076 and then Nkind (Get_Associated_Node (N)) = Nkind (N)
7078 -- In the generic the aggregate has some composite type. If at
7079 -- the point of instantiation the type has a private view,
7080 -- install the full view (and that of its ancestors, if any).
7083 T : Entity_Id := (Etype (Get_Associated_Node (New_N)));
7088 and then Is_Private_Type (T)
7094 and then Is_Tagged_Type (T)
7095 and then Is_Derived_Type (T)
7097 Rt := Root_Type (T);
7102 if Is_Private_Type (T) then
7113 -- Do not copy the associated node, which points to the generic copy
7114 -- of the aggregate.
7117 use Atree.Unchecked_Access;
7118 -- This code section is part of the implementation of an untyped
7119 -- tree traversal, so it needs direct access to node fields.
7122 Set_Field1 (New_N, Copy_Generic_Descendant (Field1 (N)));
7123 Set_Field2 (New_N, Copy_Generic_Descendant (Field2 (N)));
7124 Set_Field3 (New_N, Copy_Generic_Descendant (Field3 (N)));
7125 Set_Field5 (New_N, Copy_Generic_Descendant (Field5 (N)));
7128 -- Allocators do not have an identifier denoting the access type, so we
7129 -- must locate it through the expression to check whether the views are
7132 elsif Nkind (N) = N_Allocator
7133 and then Nkind (Expression (N)) = N_Qualified_Expression
7134 and then Is_Entity_Name (Subtype_Mark (Expression (N)))
7135 and then Instantiating
7138 T : constant Node_Id :=
7139 Get_Associated_Node (Subtype_Mark (Expression (N)));
7145 -- Retrieve the allocator node in the generic copy
7147 Acc_T := Etype (Parent (Parent (T)));
7149 and then Is_Private_Type (Acc_T)
7151 Switch_View (Acc_T);
7158 -- For a proper body, we must catch the case of a proper body that
7159 -- replaces a stub. This represents the point at which a separate
7160 -- compilation unit, and hence template file, may be referenced, so we
7161 -- must make a new source instantiation entry for the template of the
7162 -- subunit, and ensure that all nodes in the subunit are adjusted using
7163 -- this new source instantiation entry.
7165 elsif Nkind (N) in N_Proper_Body then
7167 Save_Adjustment : constant Sloc_Adjustment := S_Adjustment;
7170 if Instantiating and then Was_Originally_Stub (N) then
7171 Create_Instantiation_Source
7172 (Instantiation_Node,
7173 Defining_Entity (N),
7178 -- Now copy the fields of the proper body, using the new
7179 -- adjustment factor if one was needed as per test above.
7183 -- Restore the original adjustment factor in case changed
7185 S_Adjustment := Save_Adjustment;
7188 -- Don't copy Ident or Comment pragmas, since the comment belongs to the
7189 -- generic unit, not to the instantiating unit.
7191 elsif Nkind (N) = N_Pragma and then Instantiating then
7193 Prag_Id : constant Pragma_Id := Get_Pragma_Id (N);
7195 if Prag_Id = Pragma_Ident or else Prag_Id = Pragma_Comment then
7196 New_N := Make_Null_Statement (Sloc (N));
7202 elsif Nkind_In (N, N_Integer_Literal, N_Real_Literal) then
7204 -- No descendant fields need traversing
7208 elsif Nkind (N) = N_String_Literal
7209 and then Present (Etype (N))
7210 and then Instantiating
7212 -- If the string is declared in an outer scope, the string_literal
7213 -- subtype created for it may have the wrong scope. We force the
7214 -- reanalysis of the constant to generate a new itype in the proper
7217 Set_Etype (New_N, Empty);
7218 Set_Analyzed (New_N, False);
7220 -- For the remaining nodes, copy their descendants recursively
7225 if Instantiating and then Nkind (N) = N_Subprogram_Body then
7226 Set_Generic_Parent (Specification (New_N), N);
7228 -- Should preserve Corresponding_Spec??? (12.3(14))
7233 end Copy_Generic_Node;
7235 ----------------------------
7236 -- Denotes_Formal_Package --
7237 ----------------------------
7239 function Denotes_Formal_Package
7241 On_Exit : Boolean := False;
7242 Instance : Entity_Id := Empty) return Boolean
7245 Scop : constant Entity_Id := Scope (Pack);
7248 function Is_Actual_Of_Previous_Formal (P : Entity_Id) return Boolean;
7249 -- The package in question may be an actual for a previous formal
7250 -- package P of the current instance, so examine its actuals as well.
7251 -- This must be recursive over other formal packages.
7253 ----------------------------------
7254 -- Is_Actual_Of_Previous_Formal --
7255 ----------------------------------
7257 function Is_Actual_Of_Previous_Formal (P : Entity_Id) return Boolean is
7261 E1 := First_Entity (P);
7262 while Present (E1) and then E1 /= Instance loop
7263 if Ekind (E1) = E_Package
7264 and then Nkind (Parent (E1)) = N_Package_Renaming_Declaration
7266 if Renamed_Object (E1) = Pack then
7269 elsif E1 = P or else Renamed_Object (E1) = P then
7272 elsif Is_Actual_Of_Previous_Formal (E1) then
7281 end Is_Actual_Of_Previous_Formal;
7283 -- Start of processing for Denotes_Formal_Package
7289 (Instance_Envs.Last).Instantiated_Parent.Act_Id;
7291 Par := Current_Instantiated_Parent.Act_Id;
7294 if Ekind (Scop) = E_Generic_Package
7295 or else Nkind (Unit_Declaration_Node (Scop)) =
7296 N_Generic_Subprogram_Declaration
7300 elsif Nkind (Original_Node (Unit_Declaration_Node (Pack))) =
7301 N_Formal_Package_Declaration
7309 -- Check whether this package is associated with a formal package of
7310 -- the enclosing instantiation. Iterate over the list of renamings.
7312 E := First_Entity (Par);
7313 while Present (E) loop
7314 if Ekind (E) /= E_Package
7315 or else Nkind (Parent (E)) /= N_Package_Renaming_Declaration
7319 elsif Renamed_Object (E) = Par then
7322 elsif Renamed_Object (E) = Pack then
7325 elsif Is_Actual_Of_Previous_Formal (E) then
7335 end Denotes_Formal_Package;
7341 procedure End_Generic is
7343 -- ??? More things could be factored out in this routine. Should
7344 -- probably be done at a later stage.
7346 Inside_A_Generic := Generic_Flags.Table (Generic_Flags.Last);
7347 Generic_Flags.Decrement_Last;
7349 Expander_Mode_Restore;
7356 function Earlier (N1, N2 : Node_Id) return Boolean is
7357 procedure Find_Depth (P : in out Node_Id; D : in out Integer);
7358 -- Find distance from given node to enclosing compilation unit
7364 procedure Find_Depth (P : in out Node_Id; D : in out Integer) is
7367 and then Nkind (P) /= N_Compilation_Unit
7369 P := True_Parent (P);
7374 -- Local declarations
7383 -- Start of processing for Earlier
7386 Find_Depth (P1, D1);
7387 Find_Depth (P2, D2);
7397 P1 := True_Parent (P1);
7402 P2 := True_Parent (P2);
7406 -- At this point P1 and P2 are at the same distance from the root.
7407 -- We examine their parents until we find a common declarative list.
7408 -- If we reach the root, N1 and N2 do not descend from the same
7409 -- declarative list (e.g. one is nested in the declarative part and
7410 -- the other is in a block in the statement part) and the earlier
7411 -- one is already frozen.
7413 while not Is_List_Member (P1)
7414 or else not Is_List_Member (P2)
7415 or else List_Containing (P1) /= List_Containing (P2)
7417 P1 := True_Parent (P1);
7418 P2 := True_Parent (P2);
7420 if Nkind (Parent (P1)) = N_Subunit then
7421 P1 := Corresponding_Stub (Parent (P1));
7424 if Nkind (Parent (P2)) = N_Subunit then
7425 P2 := Corresponding_Stub (Parent (P2));
7433 -- Expanded code usually shares the source location of the original
7434 -- construct it was generated for. This however may not necessarely
7435 -- reflect the true location of the code within the tree.
7437 -- Before comparing the slocs of the two nodes, make sure that we are
7438 -- working with correct source locations. Assume that P1 is to the left
7439 -- of P2. If either one does not come from source, traverse the common
7440 -- list heading towards the other node and locate the first source
7444 -- ----+===+===+--------------+===+===+----
7445 -- expanded code expanded code
7447 if not Comes_From_Source (P1) then
7448 while Present (P1) loop
7450 -- Neither P2 nor a source statement were located during the
7451 -- search. If we reach the end of the list, then P1 does not
7452 -- occur earlier than P2.
7455 -- start --- P2 ----- P1 --- end
7457 if No (Next (P1)) then
7460 -- We encounter P2 while going to the right of the list. This
7461 -- means that P1 does indeed appear earlier.
7464 -- start --- P1 ===== P2 --- end
7465 -- expanded code in between
7470 -- No need to look any further since we have located a source
7473 elsif Comes_From_Source (P1) then
7483 if not Comes_From_Source (P2) then
7484 while Present (P2) loop
7486 -- Neither P1 nor a source statement were located during the
7487 -- search. If we reach the start of the list, then P1 does not
7488 -- occur earlier than P2.
7491 -- start --- P2 --- P1 --- end
7493 if No (Prev (P2)) then
7496 -- We encounter P1 while going to the left of the list. This
7497 -- means that P1 does indeed appear earlier.
7500 -- start --- P1 ===== P2 --- end
7501 -- expanded code in between
7506 -- No need to look any further since we have located a source
7509 elsif Comes_From_Source (P2) then
7519 -- At this point either both nodes came from source or we approximated
7520 -- their source locations through neighbouring source statements.
7522 T1 := Top_Level_Location (Sloc (P1));
7523 T2 := Top_Level_Location (Sloc (P2));
7525 -- When two nodes come from the same instance, they have identical top
7526 -- level locations. To determine proper relation within the tree, check
7527 -- their locations within the template.
7530 return Sloc (P1) < Sloc (P2);
7532 -- The two nodes either come from unrelated instances or do not come
7533 -- from instantiated code at all.
7540 ----------------------
7541 -- Find_Actual_Type --
7542 ----------------------
7544 function Find_Actual_Type
7546 Gen_Type : Entity_Id) return Entity_Id
7548 Gen_Scope : constant Entity_Id := Scope (Gen_Type);
7552 -- Special processing only applies to child units
7554 if not Is_Child_Unit (Gen_Scope) then
7555 return Get_Instance_Of (Typ);
7557 -- If designated or component type is itself a formal of the child unit,
7558 -- its instance is available.
7560 elsif Scope (Typ) = Gen_Scope then
7561 return Get_Instance_Of (Typ);
7563 -- If the array or access type is not declared in the parent unit,
7564 -- no special processing needed.
7566 elsif not Is_Generic_Type (Typ)
7567 and then Scope (Gen_Scope) /= Scope (Typ)
7569 return Get_Instance_Of (Typ);
7571 -- Otherwise, retrieve designated or component type by visibility
7574 T := Current_Entity (Typ);
7575 while Present (T) loop
7576 if In_Open_Scopes (Scope (T)) then
7579 elsif Is_Generic_Actual_Type (T) then
7588 end Find_Actual_Type;
7590 ----------------------------
7591 -- Freeze_Subprogram_Body --
7592 ----------------------------
7594 procedure Freeze_Subprogram_Body
7595 (Inst_Node : Node_Id;
7597 Pack_Id : Entity_Id)
7599 Gen_Unit : constant Entity_Id := Get_Generic_Entity (Inst_Node);
7600 Par : constant Entity_Id := Scope (Gen_Unit);
7606 function Enclosing_Package_Body (N : Node_Id) return Node_Id;
7607 -- Find innermost package body that encloses the given node, and which
7608 -- is not a compilation unit. Freeze nodes for the instance, or for its
7609 -- enclosing body, may be inserted after the enclosing_body of the
7610 -- generic unit. Used to determine proper placement of freeze node for
7611 -- both package and subprogram instances.
7613 function Package_Freeze_Node (B : Node_Id) return Node_Id;
7614 -- Find entity for given package body, and locate or create a freeze
7617 ----------------------------
7618 -- Enclosing_Package_Body --
7619 ----------------------------
7621 function Enclosing_Package_Body (N : Node_Id) return Node_Id is
7627 and then Nkind (Parent (P)) /= N_Compilation_Unit
7629 if Nkind (P) = N_Package_Body then
7630 if Nkind (Parent (P)) = N_Subunit then
7631 return Corresponding_Stub (Parent (P));
7637 P := True_Parent (P);
7641 end Enclosing_Package_Body;
7643 -------------------------
7644 -- Package_Freeze_Node --
7645 -------------------------
7647 function Package_Freeze_Node (B : Node_Id) return Node_Id is
7651 if Nkind (B) = N_Package_Body then
7652 Id := Corresponding_Spec (B);
7653 else pragma Assert (Nkind (B) = N_Package_Body_Stub);
7654 Id := Corresponding_Spec (Proper_Body (Unit (Library_Unit (B))));
7657 Ensure_Freeze_Node (Id);
7658 return Freeze_Node (Id);
7659 end Package_Freeze_Node;
7661 -- Start of processing of Freeze_Subprogram_Body
7664 -- If the instance and the generic body appear within the same unit, and
7665 -- the instance precedes the generic, the freeze node for the instance
7666 -- must appear after that of the generic. If the generic is nested
7667 -- within another instance I2, then current instance must be frozen
7668 -- after I2. In both cases, the freeze nodes are those of enclosing
7669 -- packages. Otherwise, the freeze node is placed at the end of the
7670 -- current declarative part.
7672 Enc_G := Enclosing_Package_Body (Gen_Body);
7673 Enc_I := Enclosing_Package_Body (Inst_Node);
7674 Ensure_Freeze_Node (Pack_Id);
7675 F_Node := Freeze_Node (Pack_Id);
7677 if Is_Generic_Instance (Par)
7678 and then Present (Freeze_Node (Par))
7679 and then In_Same_Declarative_Part (Freeze_Node (Par), Inst_Node)
7681 -- The parent was a premature instantiation. Insert freeze node at
7682 -- the end the current declarative part.
7684 if ABE_Is_Certain (Get_Package_Instantiation_Node (Par)) then
7685 Insert_Freeze_Node_For_Instance (Inst_Node, F_Node);
7687 -- Handle the following case:
7689 -- package Parent_Inst is new ...
7692 -- procedure P ... -- this body freezes Parent_Inst
7694 -- package Inst is new ...
7696 -- In this particular scenario, the freeze node for Inst must be
7697 -- inserted in the same manner as that of Parent_Inst - before the
7698 -- next source body or at the end of the declarative list (body not
7699 -- available). If body P did not exist and Parent_Inst was frozen
7700 -- after Inst, either by a body following Inst or at the end of the
7701 -- declarative region, the freeze node for Inst must be inserted
7702 -- after that of Parent_Inst. This relation is established by
7703 -- comparing the Slocs of Parent_Inst freeze node and Inst.
7705 elsif List_Containing (Get_Package_Instantiation_Node (Par)) =
7706 List_Containing (Inst_Node)
7707 and then Sloc (Freeze_Node (Par)) < Sloc (Inst_Node)
7709 Insert_Freeze_Node_For_Instance (Inst_Node, F_Node);
7712 Insert_After (Freeze_Node (Par), F_Node);
7715 -- The body enclosing the instance should be frozen after the body that
7716 -- includes the generic, because the body of the instance may make
7717 -- references to entities therein. If the two are not in the same
7718 -- declarative part, or if the one enclosing the instance is frozen
7719 -- already, freeze the instance at the end of the current declarative
7722 elsif Is_Generic_Instance (Par)
7723 and then Present (Freeze_Node (Par))
7724 and then Present (Enc_I)
7726 if In_Same_Declarative_Part (Freeze_Node (Par), Enc_I)
7728 (Nkind (Enc_I) = N_Package_Body
7730 In_Same_Declarative_Part (Freeze_Node (Par), Parent (Enc_I)))
7732 -- The enclosing package may contain several instances. Rather
7733 -- than computing the earliest point at which to insert its freeze
7734 -- node, we place it at the end of the declarative part of the
7735 -- parent of the generic.
7737 Insert_Freeze_Node_For_Instance
7738 (Freeze_Node (Par), Package_Freeze_Node (Enc_I));
7741 Insert_Freeze_Node_For_Instance (Inst_Node, F_Node);
7743 elsif Present (Enc_G)
7744 and then Present (Enc_I)
7745 and then Enc_G /= Enc_I
7746 and then Earlier (Inst_Node, Gen_Body)
7748 if Nkind (Enc_G) = N_Package_Body then
7749 E_G_Id := Corresponding_Spec (Enc_G);
7750 else pragma Assert (Nkind (Enc_G) = N_Package_Body_Stub);
7752 Corresponding_Spec (Proper_Body (Unit (Library_Unit (Enc_G))));
7755 -- Freeze package that encloses instance, and place node after the
7756 -- package that encloses generic. If enclosing package is already
7757 -- frozen we have to assume it is at the proper place. This may be a
7758 -- potential ABE that requires dynamic checking. Do not add a freeze
7759 -- node if the package that encloses the generic is inside the body
7760 -- that encloses the instance, because the freeze node would be in
7761 -- the wrong scope. Additional contortions needed if the bodies are
7762 -- within a subunit.
7765 Enclosing_Body : Node_Id;
7768 if Nkind (Enc_I) = N_Package_Body_Stub then
7769 Enclosing_Body := Proper_Body (Unit (Library_Unit (Enc_I)));
7771 Enclosing_Body := Enc_I;
7774 if Parent (List_Containing (Enc_G)) /= Enclosing_Body then
7775 Insert_Freeze_Node_For_Instance
7776 (Enc_G, Package_Freeze_Node (Enc_I));
7780 -- Freeze enclosing subunit before instance
7782 Ensure_Freeze_Node (E_G_Id);
7784 if not Is_List_Member (Freeze_Node (E_G_Id)) then
7785 Insert_After (Enc_G, Freeze_Node (E_G_Id));
7788 Insert_Freeze_Node_For_Instance (Inst_Node, F_Node);
7791 -- If none of the above, insert freeze node at the end of the current
7792 -- declarative part.
7794 Insert_Freeze_Node_For_Instance (Inst_Node, F_Node);
7796 end Freeze_Subprogram_Body;
7802 function Get_Gen_Id (E : Assoc_Ptr) return Entity_Id is
7804 return Generic_Renamings.Table (E).Gen_Id;
7807 ---------------------
7808 -- Get_Instance_Of --
7809 ---------------------
7811 function Get_Instance_Of (A : Entity_Id) return Entity_Id is
7812 Res : constant Assoc_Ptr := Generic_Renamings_HTable.Get (A);
7815 if Res /= Assoc_Null then
7816 return Generic_Renamings.Table (Res).Act_Id;
7818 -- On exit, entity is not instantiated: not a generic parameter, or
7819 -- else parameter of an inner generic unit.
7823 end Get_Instance_Of;
7825 ------------------------------------
7826 -- Get_Package_Instantiation_Node --
7827 ------------------------------------
7829 function Get_Package_Instantiation_Node (A : Entity_Id) return Node_Id is
7830 Decl : Node_Id := Unit_Declaration_Node (A);
7834 -- If the Package_Instantiation attribute has been set on the package
7835 -- entity, then use it directly when it (or its Original_Node) refers
7836 -- to an N_Package_Instantiation node. In principle it should be
7837 -- possible to have this field set in all cases, which should be
7838 -- investigated, and would allow this function to be significantly
7841 Inst := Package_Instantiation (A);
7843 if Present (Inst) then
7844 if Nkind (Inst) = N_Package_Instantiation then
7847 elsif Nkind (Original_Node (Inst)) = N_Package_Instantiation then
7848 return Original_Node (Inst);
7852 -- If the instantiation is a compilation unit that does not need body
7853 -- then the instantiation node has been rewritten as a package
7854 -- declaration for the instance, and we return the original node.
7856 -- If it is a compilation unit and the instance node has not been
7857 -- rewritten, then it is still the unit of the compilation. Finally, if
7858 -- a body is present, this is a parent of the main unit whose body has
7859 -- been compiled for inlining purposes, and the instantiation node has
7860 -- been rewritten with the instance body.
7862 -- Otherwise the instantiation node appears after the declaration. If
7863 -- the entity is a formal package, the declaration may have been
7864 -- rewritten as a generic declaration (in the case of a formal with box)
7865 -- or left as a formal package declaration if it has actuals, and is
7866 -- found with a forward search.
7868 if Nkind (Parent (Decl)) = N_Compilation_Unit then
7869 if Nkind (Decl) = N_Package_Declaration
7870 and then Present (Corresponding_Body (Decl))
7872 Decl := Unit_Declaration_Node (Corresponding_Body (Decl));
7875 if Nkind (Original_Node (Decl)) = N_Package_Instantiation then
7876 return Original_Node (Decl);
7878 return Unit (Parent (Decl));
7881 elsif Nkind (Decl) = N_Package_Declaration
7882 and then Nkind (Original_Node (Decl)) = N_Formal_Package_Declaration
7884 return Original_Node (Decl);
7887 Inst := Next (Decl);
7888 while not Nkind_In (Inst, N_Package_Instantiation,
7889 N_Formal_Package_Declaration)
7896 end Get_Package_Instantiation_Node;
7898 ------------------------
7899 -- Has_Been_Exchanged --
7900 ------------------------
7902 function Has_Been_Exchanged (E : Entity_Id) return Boolean is
7906 Next := First_Elmt (Exchanged_Views);
7907 while Present (Next) loop
7908 if Full_View (Node (Next)) = E then
7916 end Has_Been_Exchanged;
7922 function Hash (F : Entity_Id) return HTable_Range is
7924 return HTable_Range (F mod HTable_Size);
7927 ------------------------
7928 -- Hide_Current_Scope --
7929 ------------------------
7931 procedure Hide_Current_Scope is
7932 C : constant Entity_Id := Current_Scope;
7936 Set_Is_Hidden_Open_Scope (C);
7938 E := First_Entity (C);
7939 while Present (E) loop
7940 if Is_Immediately_Visible (E) then
7941 Set_Is_Immediately_Visible (E, False);
7942 Append_Elmt (E, Hidden_Entities);
7948 -- Make the scope name invisible as well. This is necessary, but might
7949 -- conflict with calls to Rtsfind later on, in case the scope is a
7950 -- predefined one. There is no clean solution to this problem, so for
7951 -- now we depend on the user not redefining Standard itself in one of
7952 -- the parent units.
7954 if Is_Immediately_Visible (C) and then C /= Standard_Standard then
7955 Set_Is_Immediately_Visible (C, False);
7956 Append_Elmt (C, Hidden_Entities);
7959 end Hide_Current_Scope;
7965 procedure Init_Env is
7966 Saved : Instance_Env;
7969 Saved.Instantiated_Parent := Current_Instantiated_Parent;
7970 Saved.Exchanged_Views := Exchanged_Views;
7971 Saved.Hidden_Entities := Hidden_Entities;
7972 Saved.Current_Sem_Unit := Current_Sem_Unit;
7973 Saved.Parent_Unit_Visible := Parent_Unit_Visible;
7974 Saved.Instance_Parent_Unit := Instance_Parent_Unit;
7976 -- Save configuration switches. These may be reset if the unit is a
7977 -- predefined unit, and the current mode is not Ada 2005.
7979 Save_Opt_Config_Switches (Saved.Switches);
7981 Instance_Envs.Append (Saved);
7983 Exchanged_Views := New_Elmt_List;
7984 Hidden_Entities := New_Elmt_List;
7986 -- Make dummy entry for Instantiated parent. If generic unit is legal,
7987 -- this is set properly in Set_Instance_Env.
7989 Current_Instantiated_Parent :=
7990 (Current_Scope, Current_Scope, Assoc_Null);
7993 ------------------------------
7994 -- In_Same_Declarative_Part --
7995 ------------------------------
7997 function In_Same_Declarative_Part
7999 Inst : Node_Id) return Boolean
8001 Decls : constant Node_Id := Parent (F_Node);
8002 Nod : Node_Id := Parent (Inst);
8005 while Present (Nod) loop
8009 elsif Nkind_In (Nod, N_Subprogram_Body,
8011 N_Package_Declaration,
8018 elsif Nkind (Nod) = N_Subunit then
8019 Nod := Corresponding_Stub (Nod);
8021 elsif Nkind (Nod) = N_Compilation_Unit then
8025 Nod := Parent (Nod);
8030 end In_Same_Declarative_Part;
8032 ---------------------
8033 -- In_Main_Context --
8034 ---------------------
8036 function In_Main_Context (E : Entity_Id) return Boolean is
8042 if not Is_Compilation_Unit (E)
8043 or else Ekind (E) /= E_Package
8044 or else In_Private_Part (E)
8049 Context := Context_Items (Cunit (Main_Unit));
8051 Clause := First (Context);
8052 while Present (Clause) loop
8053 if Nkind (Clause) = N_With_Clause then
8054 Nam := Name (Clause);
8056 -- If the current scope is part of the context of the main unit,
8057 -- analysis of the corresponding with_clause is not complete, and
8058 -- the entity is not set. We use the Chars field directly, which
8059 -- might produce false positives in rare cases, but guarantees
8060 -- that we produce all the instance bodies we will need.
8062 if (Is_Entity_Name (Nam) and then Chars (Nam) = Chars (E))
8063 or else (Nkind (Nam) = N_Selected_Component
8064 and then Chars (Selector_Name (Nam)) = Chars (E))
8074 end In_Main_Context;
8076 ---------------------
8077 -- Inherit_Context --
8078 ---------------------
8080 procedure Inherit_Context (Gen_Decl : Node_Id; Inst : Node_Id) is
8081 Current_Context : List_Id;
8082 Current_Unit : Node_Id;
8091 if Nkind (Parent (Gen_Decl)) = N_Compilation_Unit then
8093 -- The inherited context is attached to the enclosing compilation
8094 -- unit. This is either the main unit, or the declaration for the
8095 -- main unit (in case the instantiation appears within the package
8096 -- declaration and the main unit is its body).
8098 Current_Unit := Parent (Inst);
8099 while Present (Current_Unit)
8100 and then Nkind (Current_Unit) /= N_Compilation_Unit
8102 Current_Unit := Parent (Current_Unit);
8105 Current_Context := Context_Items (Current_Unit);
8107 Item := First (Context_Items (Parent (Gen_Decl)));
8108 while Present (Item) loop
8109 if Nkind (Item) = N_With_Clause then
8110 Lib_Unit := Library_Unit (Item);
8112 -- Take care to prevent direct cyclic with's
8114 if Lib_Unit /= Current_Unit then
8116 -- Do not add a unit if it is already in the context
8118 Clause := First (Current_Context);
8120 while Present (Clause) loop
8121 if Nkind (Clause) = N_With_Clause and then
8122 Library_Unit (Clause) = Lib_Unit
8132 New_I := New_Copy (Item);
8133 Set_Implicit_With (New_I, True);
8134 Set_Implicit_With_From_Instantiation (New_I, True);
8135 Append (New_I, Current_Context);
8143 end Inherit_Context;
8149 procedure Initialize is
8151 Generic_Renamings.Init;
8154 Generic_Renamings_HTable.Reset;
8155 Circularity_Detected := False;
8156 Exchanged_Views := No_Elist;
8157 Hidden_Entities := No_Elist;
8160 -------------------------------------
8161 -- Insert_Freeze_Node_For_Instance --
8162 -------------------------------------
8164 procedure Insert_Freeze_Node_For_Instance
8173 function Enclosing_Body (N : Node_Id) return Node_Id;
8174 -- Find enclosing package or subprogram body, if any. Freeze node may
8175 -- be placed at end of current declarative list if previous instance
8176 -- and current one have different enclosing bodies.
8178 function Previous_Instance (Gen : Entity_Id) return Entity_Id;
8179 -- Find the local instance, if any, that declares the generic that is
8180 -- being instantiated. If present, the freeze node for this instance
8181 -- must follow the freeze node for the previous instance.
8183 --------------------
8184 -- Enclosing_Body --
8185 --------------------
8187 function Enclosing_Body (N : Node_Id) return Node_Id is
8193 and then Nkind (Parent (P)) /= N_Compilation_Unit
8195 if Nkind_In (P, N_Package_Body, N_Subprogram_Body) then
8196 if Nkind (Parent (P)) = N_Subunit then
8197 return Corresponding_Stub (Parent (P));
8203 P := True_Parent (P);
8209 -----------------------
8210 -- Previous_Instance --
8211 -----------------------
8213 function Previous_Instance (Gen : Entity_Id) return Entity_Id is
8219 and then S /= Standard_Standard
8221 if Is_Generic_Instance (S)
8222 and then In_Same_Source_Unit (S, N)
8231 end Previous_Instance;
8233 -- Start of processing for Insert_Freeze_Node_For_Instance
8236 if not Is_List_Member (F_Node) then
8238 Decls := List_Containing (N);
8239 Inst := Entity (F_Node);
8240 Par_N := Parent (Decls);
8242 -- When processing a subprogram instantiation, utilize the actual
8243 -- subprogram instantiation rather than its package wrapper as it
8244 -- carries all the context information.
8246 if Is_Wrapper_Package (Inst) then
8247 Inst := Related_Instance (Inst);
8250 -- If this is a package instance, check whether the generic is
8251 -- declared in a previous instance and the current instance is
8252 -- not within the previous one.
8254 if Present (Generic_Parent (Parent (Inst)))
8255 and then Is_In_Main_Unit (N)
8258 Enclosing_N : constant Node_Id := Enclosing_Body (N);
8259 Par_I : constant Entity_Id :=
8261 (Generic_Parent (Parent (Inst)));
8266 and then Earlier (N, Freeze_Node (Par_I))
8268 Scop := Scope (Inst);
8270 -- If the current instance is within the one that contains
8271 -- the generic, the freeze node for the current one must
8272 -- appear in the current declarative part. Ditto, if the
8273 -- current instance is within another package instance or
8274 -- within a body that does not enclose the current instance.
8275 -- In these three cases the freeze node of the previous
8276 -- instance is not relevant.
8278 while Present (Scop)
8279 and then Scop /= Standard_Standard
8281 exit when Scop = Par_I
8283 (Is_Generic_Instance (Scop)
8284 and then Scope_Depth (Scop) > Scope_Depth (Par_I));
8285 Scop := Scope (Scop);
8288 -- Previous instance encloses current instance
8290 if Scop = Par_I then
8293 -- If the next node is a source body we must freeze in
8294 -- the current scope as well.
8296 elsif Present (Next (N))
8297 and then Nkind_In (Next (N),
8298 N_Subprogram_Body, N_Package_Body)
8299 and then Comes_From_Source (Next (N))
8303 -- Current instance is within an unrelated instance
8305 elsif Is_Generic_Instance (Scop) then
8308 -- Current instance is within an unrelated body
8310 elsif Present (Enclosing_N)
8311 and then Enclosing_N /= Enclosing_Body (Par_I)
8316 Insert_After (Freeze_Node (Par_I), F_Node);
8323 -- When the instantiation occurs in a package declaration, append the
8324 -- freeze node to the private declarations (if any).
8326 if Nkind (Par_N) = N_Package_Specification
8327 and then Decls = Visible_Declarations (Par_N)
8328 and then Present (Private_Declarations (Par_N))
8329 and then not Is_Empty_List (Private_Declarations (Par_N))
8331 Decls := Private_Declarations (Par_N);
8332 Decl := First (Decls);
8335 -- Determine the proper freeze point of a package instantiation. We
8336 -- adhere to the general rule of a package or subprogram body causing
8337 -- freezing of anything before it in the same declarative region. In
8338 -- this case, the proper freeze point of a package instantiation is
8339 -- before the first source body which follows, or before a stub. This
8340 -- ensures that entities coming from the instance are already frozen
8341 -- and usable in source bodies.
8343 if Nkind (Par_N) /= N_Package_Declaration
8344 and then Ekind (Inst) = E_Package
8345 and then Is_Generic_Instance (Inst)
8347 not In_Same_Source_Unit (Generic_Parent (Parent (Inst)), Inst)
8349 while Present (Decl) loop
8350 if (Nkind (Decl) in N_Unit_Body
8352 Nkind (Decl) in N_Body_Stub)
8353 and then Comes_From_Source (Decl)
8355 Insert_Before (Decl, F_Node);
8363 -- In a package declaration, or if no previous body, insert at end
8366 Set_Sloc (F_Node, Sloc (Last (Decls)));
8367 Insert_After (Last (Decls), F_Node);
8369 end Insert_Freeze_Node_For_Instance;
8375 procedure Install_Body
8376 (Act_Body : Node_Id;
8381 Act_Id : constant Entity_Id := Corresponding_Spec (Act_Body);
8382 Act_Unit : constant Node_Id := Unit (Cunit (Get_Source_Unit (N)));
8383 Gen_Id : constant Entity_Id := Corresponding_Spec (Gen_Body);
8384 Par : constant Entity_Id := Scope (Gen_Id);
8385 Gen_Unit : constant Node_Id :=
8386 Unit (Cunit (Get_Source_Unit (Gen_Decl)));
8387 Orig_Body : Node_Id := Gen_Body;
8389 Body_Unit : Node_Id;
8391 Must_Delay : Boolean;
8393 function In_Same_Enclosing_Subp return Boolean;
8394 -- Check whether instance and generic body are within same subprogram.
8396 function True_Sloc (N : Node_Id) return Source_Ptr;
8397 -- If the instance is nested inside a generic unit, the Sloc of the
8398 -- instance indicates the place of the original definition, not the
8399 -- point of the current enclosing instance. Pending a better usage of
8400 -- Slocs to indicate instantiation places, we determine the place of
8401 -- origin of a node by finding the maximum sloc of any ancestor node.
8402 -- Why is this not equivalent to Top_Level_Location ???
8404 ----------------------------
8405 -- In_Same_Enclosing_Subp --
8406 ----------------------------
8408 function In_Same_Enclosing_Subp return Boolean is
8413 Scop := Scope (Act_Id);
8414 while Scop /= Standard_Standard
8415 and then not Is_Overloadable (Scop)
8417 Scop := Scope (Scop);
8420 if Scop = Standard_Standard then
8426 Scop := Scope (Gen_Id);
8427 while Scop /= Standard_Standard loop
8431 Scop := Scope (Scop);
8436 end In_Same_Enclosing_Subp;
8442 function True_Sloc (N : Node_Id) return Source_Ptr is
8449 while Present (N1) and then N1 /= Act_Unit loop
8450 if Sloc (N1) > Res then
8460 -- Start of processing for Install_Body
8463 -- If the body is a subunit, the freeze point is the corresponding stub
8464 -- in the current compilation, not the subunit itself.
8466 if Nkind (Parent (Gen_Body)) = N_Subunit then
8467 Orig_Body := Corresponding_Stub (Parent (Gen_Body));
8469 Orig_Body := Gen_Body;
8472 Body_Unit := Unit (Cunit (Get_Source_Unit (Orig_Body)));
8474 -- If the instantiation and the generic definition appear in the same
8475 -- package declaration, this is an early instantiation. If they appear
8476 -- in the same declarative part, it is an early instantiation only if
8477 -- the generic body appears textually later, and the generic body is
8478 -- also in the main unit.
8480 -- If instance is nested within a subprogram, and the generic body
8481 -- is not, the instance is delayed because the enclosing body is. If
8482 -- instance and body are within the same scope, or the same subprogram
8483 -- body, indicate explicitly that the instance is delayed.
8486 (Gen_Unit = Act_Unit
8487 and then (Nkind_In (Gen_Unit, N_Package_Declaration,
8488 N_Generic_Package_Declaration)
8489 or else (Gen_Unit = Body_Unit
8490 and then True_Sloc (N) < Sloc (Orig_Body)))
8491 and then Is_In_Main_Unit (Gen_Unit)
8492 and then (Scope (Act_Id) = Scope (Gen_Id)
8493 or else In_Same_Enclosing_Subp));
8495 -- If this is an early instantiation, the freeze node is placed after
8496 -- the generic body. Otherwise, if the generic appears in an instance,
8497 -- we cannot freeze the current instance until the outer one is frozen.
8498 -- This is only relevant if the current instance is nested within some
8499 -- inner scope not itself within the outer instance. If this scope is
8500 -- a package body in the same declarative part as the outer instance,
8501 -- then that body needs to be frozen after the outer instance. Finally,
8502 -- if no delay is needed, we place the freeze node at the end of the
8503 -- current declarative part.
8505 if Expander_Active then
8506 Ensure_Freeze_Node (Act_Id);
8507 F_Node := Freeze_Node (Act_Id);
8510 Insert_After (Orig_Body, F_Node);
8512 elsif Is_Generic_Instance (Par)
8513 and then Present (Freeze_Node (Par))
8514 and then Scope (Act_Id) /= Par
8516 -- Freeze instance of inner generic after instance of enclosing
8519 if In_Same_Declarative_Part (Freeze_Node (Par), N) then
8521 -- Handle the following case:
8523 -- package Parent_Inst is new ...
8526 -- procedure P ... -- this body freezes Parent_Inst
8528 -- package Inst is new ...
8530 -- In this particular scenario, the freeze node for Inst must
8531 -- be inserted in the same manner as that of Parent_Inst,
8532 -- before the next source body or at the end of the declarative
8533 -- list (body not available). If body P did not exist and
8534 -- Parent_Inst was frozen after Inst, either by a body
8535 -- following Inst or at the end of the declarative region,
8536 -- the freeze node for Inst must be inserted after that of
8537 -- Parent_Inst. This relation is established by comparing
8538 -- the Slocs of Parent_Inst freeze node and Inst.
8540 if List_Containing (Get_Package_Instantiation_Node (Par)) =
8542 and then Sloc (Freeze_Node (Par)) < Sloc (N)
8544 Insert_Freeze_Node_For_Instance (N, F_Node);
8546 Insert_After (Freeze_Node (Par), F_Node);
8549 -- Freeze package enclosing instance of inner generic after
8550 -- instance of enclosing generic.
8552 elsif Nkind_In (Parent (N), N_Package_Body, N_Subprogram_Body)
8553 and then In_Same_Declarative_Part (Freeze_Node (Par), Parent (N))
8556 Enclosing : Entity_Id;
8559 Enclosing := Corresponding_Spec (Parent (N));
8561 if No (Enclosing) then
8562 Enclosing := Defining_Entity (Parent (N));
8565 Insert_Freeze_Node_For_Instance (N, F_Node);
8566 Ensure_Freeze_Node (Enclosing);
8568 if not Is_List_Member (Freeze_Node (Enclosing)) then
8570 -- The enclosing context is a subunit, insert the freeze
8571 -- node after the stub.
8573 if Nkind (Parent (Parent (N))) = N_Subunit then
8574 Insert_Freeze_Node_For_Instance
8575 (Corresponding_Stub (Parent (Parent (N))),
8576 Freeze_Node (Enclosing));
8578 -- The enclosing context is a package with a stub body
8579 -- which has already been replaced by the real body.
8580 -- Insert the freeze node after the actual body.
8582 elsif Ekind (Enclosing) = E_Package
8583 and then Present (Body_Entity (Enclosing))
8584 and then Was_Originally_Stub
8585 (Parent (Body_Entity (Enclosing)))
8587 Insert_Freeze_Node_For_Instance
8588 (Parent (Body_Entity (Enclosing)),
8589 Freeze_Node (Enclosing));
8591 -- The parent instance has been frozen before the body of
8592 -- the enclosing package, insert the freeze node after
8595 elsif List_Containing (Freeze_Node (Par)) =
8596 List_Containing (Parent (N))
8597 and then Sloc (Freeze_Node (Par)) < Sloc (Parent (N))
8599 Insert_Freeze_Node_For_Instance
8600 (Parent (N), Freeze_Node (Enclosing));
8604 (Freeze_Node (Par), Freeze_Node (Enclosing));
8610 Insert_Freeze_Node_For_Instance (N, F_Node);
8614 Insert_Freeze_Node_For_Instance (N, F_Node);
8618 Set_Is_Frozen (Act_Id);
8619 Insert_Before (N, Act_Body);
8620 Mark_Rewrite_Insertion (Act_Body);
8623 -----------------------------
8624 -- Install_Formal_Packages --
8625 -----------------------------
8627 procedure Install_Formal_Packages (Par : Entity_Id) is
8630 Gen_E : Entity_Id := Empty;
8633 E := First_Entity (Par);
8635 -- If we are installing an instance parent, locate the formal packages
8636 -- of its generic parent.
8638 if Is_Generic_Instance (Par) then
8639 Gen := Generic_Parent (Package_Specification (Par));
8640 Gen_E := First_Entity (Gen);
8643 while Present (E) loop
8644 if Ekind (E) = E_Package
8645 and then Nkind (Parent (E)) = N_Package_Renaming_Declaration
8647 -- If this is the renaming for the parent instance, done
8649 if Renamed_Object (E) = Par then
8652 -- The visibility of a formal of an enclosing generic is already
8655 elsif Denotes_Formal_Package (E) then
8658 elsif Present (Associated_Formal_Package (E)) then
8659 Check_Generic_Actuals (Renamed_Object (E), True);
8660 Set_Is_Hidden (E, False);
8662 -- Find formal package in generic unit that corresponds to
8663 -- (instance of) formal package in instance.
8665 while Present (Gen_E) and then Chars (Gen_E) /= Chars (E) loop
8666 Next_Entity (Gen_E);
8669 if Present (Gen_E) then
8670 Map_Formal_Package_Entities (Gen_E, E);
8676 if Present (Gen_E) then
8677 Next_Entity (Gen_E);
8680 end Install_Formal_Packages;
8682 --------------------
8683 -- Install_Parent --
8684 --------------------
8686 procedure Install_Parent (P : Entity_Id; In_Body : Boolean := False) is
8687 Ancestors : constant Elist_Id := New_Elmt_List;
8688 S : constant Entity_Id := Current_Scope;
8689 Inst_Par : Entity_Id;
8690 First_Par : Entity_Id;
8691 Inst_Node : Node_Id;
8692 Gen_Par : Entity_Id;
8693 First_Gen : Entity_Id;
8696 procedure Install_Noninstance_Specs (Par : Entity_Id);
8697 -- Install the scopes of noninstance parent units ending with Par
8699 procedure Install_Spec (Par : Entity_Id);
8700 -- The child unit is within the declarative part of the parent, so the
8701 -- declarations within the parent are immediately visible.
8703 -------------------------------
8704 -- Install_Noninstance_Specs --
8705 -------------------------------
8707 procedure Install_Noninstance_Specs (Par : Entity_Id) is
8710 and then Par /= Standard_Standard
8711 and then not In_Open_Scopes (Par)
8713 Install_Noninstance_Specs (Scope (Par));
8716 end Install_Noninstance_Specs;
8722 procedure Install_Spec (Par : Entity_Id) is
8723 Spec : constant Node_Id := Package_Specification (Par);
8726 -- If this parent of the child instance is a top-level unit,
8727 -- then record the unit and its visibility for later resetting in
8728 -- Remove_Parent. We exclude units that are generic instances, as we
8729 -- only want to record this information for the ultimate top-level
8730 -- noninstance parent (is that always correct???).
8732 if Scope (Par) = Standard_Standard
8733 and then not Is_Generic_Instance (Par)
8735 Parent_Unit_Visible := Is_Immediately_Visible (Par);
8736 Instance_Parent_Unit := Par;
8739 -- Open the parent scope and make it and its declarations visible.
8740 -- If this point is not within a body, then only the visible
8741 -- declarations should be made visible, and installation of the
8742 -- private declarations is deferred until the appropriate point
8743 -- within analysis of the spec being instantiated (see the handling
8744 -- of parent visibility in Analyze_Package_Specification). This is
8745 -- relaxed in the case where the parent unit is Ada.Tags, to avoid
8746 -- private view problems that occur when compiling instantiations of
8747 -- a generic child of that package (Generic_Dispatching_Constructor).
8748 -- If the instance freezes a tagged type, inlinings of operations
8749 -- from Ada.Tags may need the full view of type Tag. If inlining took
8750 -- proper account of establishing visibility of inlined subprograms'
8751 -- parents then it should be possible to remove this
8752 -- special check. ???
8755 Set_Is_Immediately_Visible (Par);
8756 Install_Visible_Declarations (Par);
8757 Set_Use (Visible_Declarations (Spec));
8759 if In_Body or else Is_RTU (Par, Ada_Tags) then
8760 Install_Private_Declarations (Par);
8761 Set_Use (Private_Declarations (Spec));
8765 -- Start of processing for Install_Parent
8768 -- We need to install the parent instance to compile the instantiation
8769 -- of the child, but the child instance must appear in the current
8770 -- scope. Given that we cannot place the parent above the current scope
8771 -- in the scope stack, we duplicate the current scope and unstack both
8772 -- after the instantiation is complete.
8774 -- If the parent is itself the instantiation of a child unit, we must
8775 -- also stack the instantiation of its parent, and so on. Each such
8776 -- ancestor is the prefix of the name in a prior instantiation.
8778 -- If this is a nested instance, the parent unit itself resolves to
8779 -- a renaming of the parent instance, whose declaration we need.
8781 -- Finally, the parent may be a generic (not an instance) when the
8782 -- child unit appears as a formal package.
8786 if Present (Renamed_Entity (Inst_Par)) then
8787 Inst_Par := Renamed_Entity (Inst_Par);
8790 First_Par := Inst_Par;
8792 Gen_Par := Generic_Parent (Package_Specification (Inst_Par));
8794 First_Gen := Gen_Par;
8796 while Present (Gen_Par)
8797 and then Is_Child_Unit (Gen_Par)
8799 -- Load grandparent instance as well
8801 Inst_Node := Get_Package_Instantiation_Node (Inst_Par);
8803 if Nkind (Name (Inst_Node)) = N_Expanded_Name then
8804 Inst_Par := Entity (Prefix (Name (Inst_Node)));
8806 if Present (Renamed_Entity (Inst_Par)) then
8807 Inst_Par := Renamed_Entity (Inst_Par);
8810 Gen_Par := Generic_Parent (Package_Specification (Inst_Par));
8812 if Present (Gen_Par) then
8813 Prepend_Elmt (Inst_Par, Ancestors);
8816 -- Parent is not the name of an instantiation
8818 Install_Noninstance_Specs (Inst_Par);
8829 if Present (First_Gen) then
8830 Append_Elmt (First_Par, Ancestors);
8832 Install_Noninstance_Specs (First_Par);
8835 if not Is_Empty_Elmt_List (Ancestors) then
8836 Elmt := First_Elmt (Ancestors);
8837 while Present (Elmt) loop
8838 Install_Spec (Node (Elmt));
8839 Install_Formal_Packages (Node (Elmt));
8849 -------------------------------
8850 -- Install_Hidden_Primitives --
8851 -------------------------------
8853 procedure Install_Hidden_Primitives
8854 (Prims_List : in out Elist_Id;
8859 List : Elist_Id := No_Elist;
8860 Prim_G_Elmt : Elmt_Id;
8861 Prim_A_Elmt : Elmt_Id;
8866 -- No action needed in case of serious errors because we cannot trust
8867 -- in the order of primitives
8869 if Serious_Errors_Detected > 0 then
8872 -- No action possible if we don't have available the list of primitive
8876 or else not Is_Record_Type (Gen_T)
8877 or else not Is_Tagged_Type (Gen_T)
8878 or else not Is_Record_Type (Act_T)
8879 or else not Is_Tagged_Type (Act_T)
8883 -- There is no need to handle interface types since their primitives
8886 elsif Is_Interface (Gen_T) then
8890 Prim_G_Elmt := First_Elmt (Primitive_Operations (Gen_T));
8892 if not Is_Class_Wide_Type (Act_T) then
8893 Prim_A_Elmt := First_Elmt (Primitive_Operations (Act_T));
8895 Prim_A_Elmt := First_Elmt (Primitive_Operations (Root_Type (Act_T)));
8899 -- Skip predefined primitives in the generic formal
8901 while Present (Prim_G_Elmt)
8902 and then Is_Predefined_Dispatching_Operation (Node (Prim_G_Elmt))
8904 Next_Elmt (Prim_G_Elmt);
8907 -- Skip predefined primitives in the generic actual
8909 while Present (Prim_A_Elmt)
8910 and then Is_Predefined_Dispatching_Operation (Node (Prim_A_Elmt))
8912 Next_Elmt (Prim_A_Elmt);
8915 exit when No (Prim_G_Elmt) or else No (Prim_A_Elmt);
8917 Prim_G := Node (Prim_G_Elmt);
8918 Prim_A := Node (Prim_A_Elmt);
8920 -- There is no need to handle interface primitives because their
8921 -- primitives are not hidden
8923 exit when Present (Interface_Alias (Prim_G));
8925 -- Here we install one hidden primitive
8927 if Chars (Prim_G) /= Chars (Prim_A)
8928 and then Has_Suffix (Prim_A, 'P')
8929 and then Remove_Suffix (Prim_A, 'P') = Chars (Prim_G)
8931 Set_Chars (Prim_A, Chars (Prim_G));
8932 Append_New_Elmt (Prim_A, To => List);
8935 Next_Elmt (Prim_A_Elmt);
8936 Next_Elmt (Prim_G_Elmt);
8939 -- Append the elements to the list of temporarily visible primitives
8940 -- avoiding duplicates.
8942 if Present (List) then
8943 if No (Prims_List) then
8944 Prims_List := New_Elmt_List;
8947 Elmt := First_Elmt (List);
8948 while Present (Elmt) loop
8949 Append_Unique_Elmt (Node (Elmt), Prims_List);
8953 end Install_Hidden_Primitives;
8955 -------------------------------
8956 -- Restore_Hidden_Primitives --
8957 -------------------------------
8959 procedure Restore_Hidden_Primitives (Prims_List : in out Elist_Id) is
8960 Prim_Elmt : Elmt_Id;
8964 if Prims_List /= No_Elist then
8965 Prim_Elmt := First_Elmt (Prims_List);
8966 while Present (Prim_Elmt) loop
8967 Prim := Node (Prim_Elmt);
8968 Set_Chars (Prim, Add_Suffix (Prim, 'P'));
8969 Next_Elmt (Prim_Elmt);
8972 Prims_List := No_Elist;
8974 end Restore_Hidden_Primitives;
8976 --------------------------------
8977 -- Instantiate_Formal_Package --
8978 --------------------------------
8980 function Instantiate_Formal_Package
8983 Analyzed_Formal : Node_Id) return List_Id
8985 Loc : constant Source_Ptr := Sloc (Actual);
8986 Actual_Pack : Entity_Id;
8987 Formal_Pack : Entity_Id;
8988 Gen_Parent : Entity_Id;
8991 Parent_Spec : Node_Id;
8993 procedure Find_Matching_Actual
8995 Act : in out Entity_Id);
8996 -- We need to associate each formal entity in the formal package with
8997 -- the corresponding entity in the actual package. The actual package
8998 -- has been analyzed and possibly expanded, and as a result there is
8999 -- no one-to-one correspondence between the two lists (for example,
9000 -- the actual may include subtypes, itypes, and inherited primitive
9001 -- operations, interspersed among the renaming declarations for the
9002 -- actuals) . We retrieve the corresponding actual by name because each
9003 -- actual has the same name as the formal, and they do appear in the
9006 function Get_Formal_Entity (N : Node_Id) return Entity_Id;
9007 -- Retrieve entity of defining entity of generic formal parameter.
9008 -- Only the declarations of formals need to be considered when
9009 -- linking them to actuals, but the declarative list may include
9010 -- internal entities generated during analysis, and those are ignored.
9012 procedure Match_Formal_Entity
9013 (Formal_Node : Node_Id;
9014 Formal_Ent : Entity_Id;
9015 Actual_Ent : Entity_Id);
9016 -- Associates the formal entity with the actual. In the case where
9017 -- Formal_Ent is a formal package, this procedure iterates through all
9018 -- of its formals and enters associations between the actuals occurring
9019 -- in the formal package's corresponding actual package (given by
9020 -- Actual_Ent) and the formal package's formal parameters. This
9021 -- procedure recurses if any of the parameters is itself a package.
9023 function Is_Instance_Of
9024 (Act_Spec : Entity_Id;
9025 Gen_Anc : Entity_Id) return Boolean;
9026 -- The actual can be an instantiation of a generic within another
9027 -- instance, in which case there is no direct link from it to the
9028 -- original generic ancestor. In that case, we recognize that the
9029 -- ultimate ancestor is the same by examining names and scopes.
9031 procedure Process_Nested_Formal (Formal : Entity_Id);
9032 -- If the current formal is declared with a box, its own formals are
9033 -- visible in the instance, as they were in the generic, and their
9034 -- Hidden flag must be reset. If some of these formals are themselves
9035 -- packages declared with a box, the processing must be recursive.
9037 --------------------------
9038 -- Find_Matching_Actual --
9039 --------------------------
9041 procedure Find_Matching_Actual
9043 Act : in out Entity_Id)
9045 Formal_Ent : Entity_Id;
9048 case Nkind (Original_Node (F)) is
9049 when N_Formal_Object_Declaration |
9050 N_Formal_Type_Declaration =>
9051 Formal_Ent := Defining_Identifier (F);
9053 while Chars (Act) /= Chars (Formal_Ent) loop
9057 when N_Formal_Subprogram_Declaration |
9058 N_Formal_Package_Declaration |
9059 N_Package_Declaration |
9060 N_Generic_Package_Declaration =>
9061 Formal_Ent := Defining_Entity (F);
9063 while Chars (Act) /= Chars (Formal_Ent) loop
9068 raise Program_Error;
9070 end Find_Matching_Actual;
9072 -------------------------
9073 -- Match_Formal_Entity --
9074 -------------------------
9076 procedure Match_Formal_Entity
9077 (Formal_Node : Node_Id;
9078 Formal_Ent : Entity_Id;
9079 Actual_Ent : Entity_Id)
9081 Act_Pkg : Entity_Id;
9084 Set_Instance_Of (Formal_Ent, Actual_Ent);
9086 if Ekind (Actual_Ent) = E_Package then
9088 -- Record associations for each parameter
9090 Act_Pkg := Actual_Ent;
9093 A_Ent : Entity_Id := First_Entity (Act_Pkg);
9102 -- Retrieve the actual given in the formal package declaration
9104 Actual := Entity (Name (Original_Node (Formal_Node)));
9106 -- The actual in the formal package declaration may be a
9107 -- renamed generic package, in which case we want to retrieve
9108 -- the original generic in order to traverse its formal part.
9110 if Present (Renamed_Entity (Actual)) then
9111 Gen_Decl := Unit_Declaration_Node (Renamed_Entity (Actual));
9113 Gen_Decl := Unit_Declaration_Node (Actual);
9116 Formals := Generic_Formal_Declarations (Gen_Decl);
9118 if Present (Formals) then
9119 F_Node := First_Non_Pragma (Formals);
9124 while Present (A_Ent)
9125 and then Present (F_Node)
9126 and then A_Ent /= First_Private_Entity (Act_Pkg)
9128 F_Ent := Get_Formal_Entity (F_Node);
9130 if Present (F_Ent) then
9132 -- This is a formal of the original package. Record
9133 -- association and recurse.
9135 Find_Matching_Actual (F_Node, A_Ent);
9136 Match_Formal_Entity (F_Node, F_Ent, A_Ent);
9137 Next_Entity (A_Ent);
9140 Next_Non_Pragma (F_Node);
9144 end Match_Formal_Entity;
9146 -----------------------
9147 -- Get_Formal_Entity --
9148 -----------------------
9150 function Get_Formal_Entity (N : Node_Id) return Entity_Id is
9151 Kind : constant Node_Kind := Nkind (Original_Node (N));
9154 when N_Formal_Object_Declaration =>
9155 return Defining_Identifier (N);
9157 when N_Formal_Type_Declaration =>
9158 return Defining_Identifier (N);
9160 when N_Formal_Subprogram_Declaration =>
9161 return Defining_Unit_Name (Specification (N));
9163 when N_Formal_Package_Declaration =>
9164 return Defining_Identifier (Original_Node (N));
9166 when N_Generic_Package_Declaration =>
9167 return Defining_Identifier (Original_Node (N));
9169 -- All other declarations are introduced by semantic analysis and
9170 -- have no match in the actual.
9175 end Get_Formal_Entity;
9177 --------------------
9178 -- Is_Instance_Of --
9179 --------------------
9181 function Is_Instance_Of
9182 (Act_Spec : Entity_Id;
9183 Gen_Anc : Entity_Id) return Boolean
9185 Gen_Par : constant Entity_Id := Generic_Parent (Act_Spec);
9188 if No (Gen_Par) then
9191 -- Simplest case: the generic parent of the actual is the formal
9193 elsif Gen_Par = Gen_Anc then
9196 elsif Chars (Gen_Par) /= Chars (Gen_Anc) then
9199 -- The actual may be obtained through several instantiations. Its
9200 -- scope must itself be an instance of a generic declared in the
9201 -- same scope as the formal. Any other case is detected above.
9203 elsif not Is_Generic_Instance (Scope (Gen_Par)) then
9207 return Generic_Parent (Parent (Scope (Gen_Par))) = Scope (Gen_Anc);
9211 ---------------------------
9212 -- Process_Nested_Formal --
9213 ---------------------------
9215 procedure Process_Nested_Formal (Formal : Entity_Id) is
9219 if Present (Associated_Formal_Package (Formal))
9220 and then Box_Present (Parent (Associated_Formal_Package (Formal)))
9222 Ent := First_Entity (Formal);
9223 while Present (Ent) loop
9224 Set_Is_Hidden (Ent, False);
9225 Set_Is_Visible_Formal (Ent);
9226 Set_Is_Potentially_Use_Visible
9227 (Ent, Is_Potentially_Use_Visible (Formal));
9229 if Ekind (Ent) = E_Package then
9230 exit when Renamed_Entity (Ent) = Renamed_Entity (Formal);
9231 Process_Nested_Formal (Ent);
9237 end Process_Nested_Formal;
9239 -- Start of processing for Instantiate_Formal_Package
9244 if not Is_Entity_Name (Actual)
9245 or else Ekind (Entity (Actual)) /= E_Package
9248 ("expect package instance to instantiate formal", Actual);
9249 Abandon_Instantiation (Actual);
9250 raise Program_Error;
9253 Actual_Pack := Entity (Actual);
9254 Set_Is_Instantiated (Actual_Pack);
9256 -- The actual may be a renamed package, or an outer generic formal
9257 -- package whose instantiation is converted into a renaming.
9259 if Present (Renamed_Object (Actual_Pack)) then
9260 Actual_Pack := Renamed_Object (Actual_Pack);
9263 if Nkind (Analyzed_Formal) = N_Formal_Package_Declaration then
9264 Gen_Parent := Get_Instance_Of (Entity (Name (Analyzed_Formal)));
9265 Formal_Pack := Defining_Identifier (Analyzed_Formal);
9268 Generic_Parent (Specification (Analyzed_Formal));
9270 Defining_Unit_Name (Specification (Analyzed_Formal));
9273 if Nkind (Parent (Actual_Pack)) = N_Defining_Program_Unit_Name then
9274 Parent_Spec := Package_Specification (Actual_Pack);
9276 Parent_Spec := Parent (Actual_Pack);
9279 if Gen_Parent = Any_Id then
9281 ("previous error in declaration of formal package", Actual);
9282 Abandon_Instantiation (Actual);
9285 Is_Instance_Of (Parent_Spec, Get_Instance_Of (Gen_Parent))
9291 ("actual parameter must be instance of&", Actual, Gen_Parent);
9292 Abandon_Instantiation (Actual);
9295 Set_Instance_Of (Defining_Identifier (Formal), Actual_Pack);
9296 Map_Formal_Package_Entities (Formal_Pack, Actual_Pack);
9299 Make_Package_Renaming_Declaration (Loc,
9300 Defining_Unit_Name => New_Copy (Defining_Identifier (Formal)),
9301 Name => New_Occurrence_Of (Actual_Pack, Loc));
9303 Set_Associated_Formal_Package (Defining_Unit_Name (Nod),
9304 Defining_Identifier (Formal));
9305 Decls := New_List (Nod);
9307 -- If the formal F has a box, then the generic declarations are
9308 -- visible in the generic G. In an instance of G, the corresponding
9309 -- entities in the actual for F (which are the actuals for the
9310 -- instantiation of the generic that F denotes) must also be made
9311 -- visible for analysis of the current instance. On exit from the
9312 -- current instance, those entities are made private again. If the
9313 -- actual is currently in use, these entities are also use-visible.
9315 -- The loop through the actual entities also steps through the formal
9316 -- entities and enters associations from formals to actuals into the
9317 -- renaming map. This is necessary to properly handle checking of
9318 -- actual parameter associations for later formals that depend on
9319 -- actuals declared in the formal package.
9321 -- In Ada 2005, partial parameterization requires that we make
9322 -- visible the actuals corresponding to formals that were defaulted
9323 -- in the formal package. There formals are identified because they
9324 -- remain formal generics within the formal package, rather than
9325 -- being renamings of the actuals supplied.
9328 Gen_Decl : constant Node_Id :=
9329 Unit_Declaration_Node (Gen_Parent);
9330 Formals : constant List_Id :=
9331 Generic_Formal_Declarations (Gen_Decl);
9333 Actual_Ent : Entity_Id;
9334 Actual_Of_Formal : Node_Id;
9335 Formal_Node : Node_Id;
9336 Formal_Ent : Entity_Id;
9339 if Present (Formals) then
9340 Formal_Node := First_Non_Pragma (Formals);
9342 Formal_Node := Empty;
9345 Actual_Ent := First_Entity (Actual_Pack);
9347 First (Visible_Declarations (Specification (Analyzed_Formal)));
9348 while Present (Actual_Ent)
9349 and then Actual_Ent /= First_Private_Entity (Actual_Pack)
9351 if Present (Formal_Node) then
9352 Formal_Ent := Get_Formal_Entity (Formal_Node);
9354 if Present (Formal_Ent) then
9355 Find_Matching_Actual (Formal_Node, Actual_Ent);
9357 (Formal_Node, Formal_Ent, Actual_Ent);
9359 -- We iterate at the same time over the actuals of the
9360 -- local package created for the formal, to determine
9361 -- which one of the formals of the original generic were
9362 -- defaulted in the formal. The corresponding actual
9363 -- entities are visible in the enclosing instance.
9365 if Box_Present (Formal)
9367 (Present (Actual_Of_Formal)
9370 (Get_Formal_Entity (Actual_Of_Formal)))
9372 Set_Is_Hidden (Actual_Ent, False);
9373 Set_Is_Visible_Formal (Actual_Ent);
9374 Set_Is_Potentially_Use_Visible
9375 (Actual_Ent, In_Use (Actual_Pack));
9377 if Ekind (Actual_Ent) = E_Package then
9378 Process_Nested_Formal (Actual_Ent);
9382 Set_Is_Hidden (Actual_Ent);
9383 Set_Is_Potentially_Use_Visible (Actual_Ent, False);
9387 Next_Non_Pragma (Formal_Node);
9388 Next (Actual_Of_Formal);
9391 -- No further formals to match, but the generic part may
9392 -- contain inherited operation that are not hidden in the
9393 -- enclosing instance.
9395 Next_Entity (Actual_Ent);
9399 -- Inherited subprograms generated by formal derived types are
9400 -- also visible if the types are.
9402 Actual_Ent := First_Entity (Actual_Pack);
9403 while Present (Actual_Ent)
9404 and then Actual_Ent /= First_Private_Entity (Actual_Pack)
9406 if Is_Overloadable (Actual_Ent)
9408 Nkind (Parent (Actual_Ent)) = N_Subtype_Declaration
9410 not Is_Hidden (Defining_Identifier (Parent (Actual_Ent)))
9412 Set_Is_Hidden (Actual_Ent, False);
9413 Set_Is_Potentially_Use_Visible
9414 (Actual_Ent, In_Use (Actual_Pack));
9417 Next_Entity (Actual_Ent);
9421 -- If the formal is not declared with a box, reanalyze it as an
9422 -- abbreviated instantiation, to verify the matching rules of 12.7.
9423 -- The actual checks are performed after the generic associations
9424 -- have been analyzed, to guarantee the same visibility for this
9425 -- instantiation and for the actuals.
9427 -- In Ada 2005, the generic associations for the formal can include
9428 -- defaulted parameters. These are ignored during check. This
9429 -- internal instantiation is removed from the tree after conformance
9430 -- checking, because it contains formal declarations for those
9431 -- defaulted parameters, and those should not reach the back-end.
9433 if not Box_Present (Formal) then
9435 I_Pack : constant Entity_Id :=
9436 Make_Temporary (Sloc (Actual), 'P');
9439 Set_Is_Internal (I_Pack);
9442 Make_Package_Instantiation (Sloc (Actual),
9443 Defining_Unit_Name => I_Pack,
9446 (Get_Instance_Of (Gen_Parent), Sloc (Actual)),
9447 Generic_Associations =>
9448 Generic_Associations (Formal)));
9454 end Instantiate_Formal_Package;
9456 -----------------------------------
9457 -- Instantiate_Formal_Subprogram --
9458 -----------------------------------
9460 function Instantiate_Formal_Subprogram
9463 Analyzed_Formal : Node_Id) return Node_Id
9465 Analyzed_S : constant Entity_Id :=
9466 Defining_Unit_Name (Specification (Analyzed_Formal));
9467 Formal_Sub : constant Entity_Id :=
9468 Defining_Unit_Name (Specification (Formal));
9470 function From_Parent_Scope (Subp : Entity_Id) return Boolean;
9471 -- If the generic is a child unit, the parent has been installed on the
9472 -- scope stack, but a default subprogram cannot resolve to something
9473 -- on the parent because that parent is not really part of the visible
9474 -- context (it is there to resolve explicit local entities). If the
9475 -- default has resolved in this way, we remove the entity from immediate
9476 -- visibility and analyze the node again to emit an error message or
9477 -- find another visible candidate.
9479 procedure Valid_Actual_Subprogram (Act : Node_Id);
9480 -- Perform legality check and raise exception on failure
9482 -----------------------
9483 -- From_Parent_Scope --
9484 -----------------------
9486 function From_Parent_Scope (Subp : Entity_Id) return Boolean is
9487 Gen_Scope : Node_Id;
9490 Gen_Scope := Scope (Analyzed_S);
9491 while Present (Gen_Scope) and then Is_Child_Unit (Gen_Scope) loop
9492 if Scope (Subp) = Scope (Gen_Scope) then
9496 Gen_Scope := Scope (Gen_Scope);
9500 end From_Parent_Scope;
9502 -----------------------------
9503 -- Valid_Actual_Subprogram --
9504 -----------------------------
9506 procedure Valid_Actual_Subprogram (Act : Node_Id) is
9510 if Is_Entity_Name (Act) then
9511 Act_E := Entity (Act);
9513 elsif Nkind (Act) = N_Selected_Component
9514 and then Is_Entity_Name (Selector_Name (Act))
9516 Act_E := Entity (Selector_Name (Act));
9522 if (Present (Act_E) and then Is_Overloadable (Act_E))
9523 or else Nkind_In (Act, N_Attribute_Reference,
9524 N_Indexed_Component,
9525 N_Character_Literal,
9526 N_Explicit_Dereference)
9532 ("expect subprogram or entry name in instantiation of&",
9533 Instantiation_Node, Formal_Sub);
9534 Abandon_Instantiation (Instantiation_Node);
9535 end Valid_Actual_Subprogram;
9539 Decl_Node : Node_Id;
9544 -- Start of processing for Instantiate_Formal_Subprogram
9547 New_Spec := New_Copy_Tree (Specification (Formal));
9549 -- The tree copy has created the proper instantiation sloc for the
9550 -- new specification. Use this location for all other constructed
9553 Loc := Sloc (Defining_Unit_Name (New_Spec));
9555 -- Create new entity for the actual (New_Copy_Tree does not)
9557 Set_Defining_Unit_Name
9558 (New_Spec, Make_Defining_Identifier (Loc, Chars (Formal_Sub)));
9560 -- Create new entities for the each of the formals in the specification
9561 -- of the renaming declaration built for the actual.
9563 if Present (Parameter_Specifications (New_Spec)) then
9569 F := First (Parameter_Specifications (New_Spec));
9570 while Present (F) loop
9571 F_Id := Defining_Identifier (F);
9573 Set_Defining_Identifier (F,
9574 Make_Defining_Identifier (Sloc (F_Id), Chars (F_Id)));
9580 -- Find entity of actual. If the actual is an attribute reference, it
9581 -- cannot be resolved here (its formal is missing) but is handled
9582 -- instead in Attribute_Renaming. If the actual is overloaded, it is
9583 -- fully resolved subsequently, when the renaming declaration for the
9584 -- formal is analyzed. If it is an explicit dereference, resolve the
9585 -- prefix but not the actual itself, to prevent interpretation as call.
9587 if Present (Actual) then
9588 Loc := Sloc (Actual);
9589 Set_Sloc (New_Spec, Loc);
9591 if Nkind (Actual) = N_Operator_Symbol then
9592 Find_Direct_Name (Actual);
9594 elsif Nkind (Actual) = N_Explicit_Dereference then
9595 Analyze (Prefix (Actual));
9597 elsif Nkind (Actual) /= N_Attribute_Reference then
9601 Valid_Actual_Subprogram (Actual);
9604 elsif Present (Default_Name (Formal)) then
9605 if not Nkind_In (Default_Name (Formal), N_Attribute_Reference,
9606 N_Selected_Component,
9607 N_Indexed_Component,
9608 N_Character_Literal)
9609 and then Present (Entity (Default_Name (Formal)))
9611 Nam := New_Occurrence_Of (Entity (Default_Name (Formal)), Loc);
9613 Nam := New_Copy (Default_Name (Formal));
9614 Set_Sloc (Nam, Loc);
9617 elsif Box_Present (Formal) then
9619 -- Actual is resolved at the point of instantiation. Create an
9620 -- identifier or operator with the same name as the formal.
9622 if Nkind (Formal_Sub) = N_Defining_Operator_Symbol then
9624 Make_Operator_Symbol (Loc,
9625 Chars => Chars (Formal_Sub),
9626 Strval => No_String);
9628 Nam := Make_Identifier (Loc, Chars (Formal_Sub));
9631 elsif Nkind (Specification (Formal)) = N_Procedure_Specification
9632 and then Null_Present (Specification (Formal))
9634 -- Generate null body for procedure, for use in the instance
9637 Make_Subprogram_Body (Loc,
9638 Specification => New_Spec,
9639 Declarations => New_List,
9640 Handled_Statement_Sequence =>
9641 Make_Handled_Sequence_Of_Statements (Loc,
9642 Statements => New_List (Make_Null_Statement (Loc))));
9644 Set_Is_Intrinsic_Subprogram (Defining_Unit_Name (New_Spec));
9648 Error_Msg_Sloc := Sloc (Scope (Analyzed_S));
9650 ("missing actual&", Instantiation_Node, Formal_Sub);
9652 ("\in instantiation of & declared#",
9653 Instantiation_Node, Scope (Analyzed_S));
9654 Abandon_Instantiation (Instantiation_Node);
9658 Make_Subprogram_Renaming_Declaration (Loc,
9659 Specification => New_Spec,
9662 -- If we do not have an actual and the formal specified <> then set to
9663 -- get proper default.
9665 if No (Actual) and then Box_Present (Formal) then
9666 Set_From_Default (Decl_Node);
9669 -- Gather possible interpretations for the actual before analyzing the
9670 -- instance. If overloaded, it will be resolved when analyzing the
9671 -- renaming declaration.
9673 if Box_Present (Formal) and then No (Actual) then
9676 if Is_Child_Unit (Scope (Analyzed_S))
9677 and then Present (Entity (Nam))
9679 if not Is_Overloaded (Nam) then
9680 if From_Parent_Scope (Entity (Nam)) then
9681 Set_Is_Immediately_Visible (Entity (Nam), False);
9682 Set_Entity (Nam, Empty);
9683 Set_Etype (Nam, Empty);
9686 Set_Is_Immediately_Visible (Entity (Nam));
9695 Get_First_Interp (Nam, I, It);
9696 while Present (It.Nam) loop
9697 if From_Parent_Scope (It.Nam) then
9701 Get_Next_Interp (I, It);
9708 -- The generic instantiation freezes the actual. This can only be done
9709 -- once the actual is resolved, in the analysis of the renaming
9710 -- declaration. To make the formal subprogram entity available, we set
9711 -- Corresponding_Formal_Spec to point to the formal subprogram entity.
9712 -- This is also needed in Analyze_Subprogram_Renaming for the processing
9713 -- of formal abstract subprograms.
9715 Set_Corresponding_Formal_Spec (Decl_Node, Analyzed_S);
9717 -- We cannot analyze the renaming declaration, and thus find the actual,
9718 -- until all the actuals are assembled in the instance. For subsequent
9719 -- checks of other actuals, indicate the node that will hold the
9720 -- instance of this formal.
9722 Set_Instance_Of (Analyzed_S, Nam);
9724 if Nkind (Actual) = N_Selected_Component
9725 and then Is_Task_Type (Etype (Prefix (Actual)))
9726 and then not Is_Frozen (Etype (Prefix (Actual)))
9728 -- The renaming declaration will create a body, which must appear
9729 -- outside of the instantiation, We move the renaming declaration
9730 -- out of the instance, and create an additional renaming inside,
9731 -- to prevent freezing anomalies.
9734 Anon_Id : constant Entity_Id := Make_Temporary (Loc, 'E');
9737 Set_Defining_Unit_Name (New_Spec, Anon_Id);
9738 Insert_Before (Instantiation_Node, Decl_Node);
9739 Analyze (Decl_Node);
9741 -- Now create renaming within the instance
9744 Make_Subprogram_Renaming_Declaration (Loc,
9745 Specification => New_Copy_Tree (New_Spec),
9746 Name => New_Occurrence_Of (Anon_Id, Loc));
9748 Set_Defining_Unit_Name (Specification (Decl_Node),
9749 Make_Defining_Identifier (Loc, Chars (Formal_Sub)));
9754 end Instantiate_Formal_Subprogram;
9756 ------------------------
9757 -- Instantiate_Object --
9758 ------------------------
9760 function Instantiate_Object
9763 Analyzed_Formal : Node_Id) return List_Id
9765 Gen_Obj : constant Entity_Id := Defining_Identifier (Formal);
9766 A_Gen_Obj : constant Entity_Id :=
9767 Defining_Identifier (Analyzed_Formal);
9768 Acc_Def : Node_Id := Empty;
9769 Act_Assoc : constant Node_Id := Parent (Actual);
9770 Actual_Decl : Node_Id := Empty;
9771 Decl_Node : Node_Id;
9774 List : constant List_Id := New_List;
9775 Loc : constant Source_Ptr := Sloc (Actual);
9776 Orig_Ftyp : constant Entity_Id := Etype (A_Gen_Obj);
9777 Subt_Decl : Node_Id := Empty;
9778 Subt_Mark : Node_Id := Empty;
9781 if Present (Subtype_Mark (Formal)) then
9782 Subt_Mark := Subtype_Mark (Formal);
9784 Check_Access_Definition (Formal);
9785 Acc_Def := Access_Definition (Formal);
9788 -- Sloc for error message on missing actual
9790 Error_Msg_Sloc := Sloc (Scope (A_Gen_Obj));
9792 if Get_Instance_Of (Gen_Obj) /= Gen_Obj then
9793 Error_Msg_N ("duplicate instantiation of generic parameter", Actual);
9796 Set_Parent (List, Parent (Actual));
9800 if Out_Present (Formal) then
9802 -- An IN OUT generic actual must be a name. The instantiation is a
9803 -- renaming declaration. The actual is the name being renamed. We
9804 -- use the actual directly, rather than a copy, because it is not
9805 -- used further in the list of actuals, and because a copy or a use
9806 -- of relocate_node is incorrect if the instance is nested within a
9807 -- generic. In order to simplify ASIS searches, the Generic_Parent
9808 -- field links the declaration to the generic association.
9813 Instantiation_Node, Gen_Obj);
9815 ("\in instantiation of & declared#",
9816 Instantiation_Node, Scope (A_Gen_Obj));
9817 Abandon_Instantiation (Instantiation_Node);
9820 if Present (Subt_Mark) then
9822 Make_Object_Renaming_Declaration (Loc,
9823 Defining_Identifier => New_Copy (Gen_Obj),
9824 Subtype_Mark => New_Copy_Tree (Subt_Mark),
9827 else pragma Assert (Present (Acc_Def));
9829 Make_Object_Renaming_Declaration (Loc,
9830 Defining_Identifier => New_Copy (Gen_Obj),
9831 Access_Definition => New_Copy_Tree (Acc_Def),
9835 Set_Corresponding_Generic_Association (Decl_Node, Act_Assoc);
9837 -- The analysis of the actual may produce Insert_Action nodes, so
9838 -- the declaration must have a context in which to attach them.
9840 Append (Decl_Node, List);
9843 -- Return if the analysis of the actual reported some error
9845 if Etype (Actual) = Any_Type then
9849 -- This check is performed here because Analyze_Object_Renaming will
9850 -- not check it when Comes_From_Source is False. Note though that the
9851 -- check for the actual being the name of an object will be performed
9852 -- in Analyze_Object_Renaming.
9854 if Is_Object_Reference (Actual)
9855 and then Is_Dependent_Component_Of_Mutable_Object (Actual)
9858 ("illegal discriminant-dependent component for in out parameter",
9862 -- The actual has to be resolved in order to check that it is a
9863 -- variable (due to cases such as F (1), where F returns access to
9864 -- an array, and for overloaded prefixes).
9866 Ftyp := Get_Instance_Of (Etype (A_Gen_Obj));
9868 -- If the type of the formal is not itself a formal, and the current
9869 -- unit is a child unit, the formal type must be declared in a
9870 -- parent, and must be retrieved by visibility.
9873 and then Is_Generic_Unit (Scope (Ftyp))
9874 and then Is_Child_Unit (Scope (A_Gen_Obj))
9877 Temp : constant Node_Id :=
9878 New_Copy_Tree (Subtype_Mark (Analyzed_Formal));
9880 Set_Entity (Temp, Empty);
9882 Ftyp := Entity (Temp);
9886 if Is_Private_Type (Ftyp)
9887 and then not Is_Private_Type (Etype (Actual))
9888 and then (Base_Type (Full_View (Ftyp)) = Base_Type (Etype (Actual))
9889 or else Base_Type (Etype (Actual)) = Ftyp)
9891 -- If the actual has the type of the full view of the formal, or
9892 -- else a non-private subtype of the formal, then the visibility
9893 -- of the formal type has changed. Add to the actuals a subtype
9894 -- declaration that will force the exchange of views in the body
9895 -- of the instance as well.
9898 Make_Subtype_Declaration (Loc,
9899 Defining_Identifier => Make_Temporary (Loc, 'P'),
9900 Subtype_Indication => New_Occurrence_Of (Ftyp, Loc));
9902 Prepend (Subt_Decl, List);
9904 Prepend_Elmt (Full_View (Ftyp), Exchanged_Views);
9905 Exchange_Declarations (Ftyp);
9908 Resolve (Actual, Ftyp);
9910 if not Denotes_Variable (Actual) then
9912 ("actual for& must be a variable", Actual, Gen_Obj);
9914 elsif Base_Type (Ftyp) /= Base_Type (Etype (Actual)) then
9916 -- Ada 2005 (AI-423): For a generic formal object of mode in out,
9917 -- the type of the actual shall resolve to a specific anonymous
9920 if Ada_Version < Ada_2005
9922 Ekind (Base_Type (Ftyp)) /=
9923 E_Anonymous_Access_Type
9925 Ekind (Base_Type (Etype (Actual))) /=
9926 E_Anonymous_Access_Type
9928 Error_Msg_NE ("type of actual does not match type of&",
9933 Note_Possible_Modification (Actual, Sure => True);
9935 -- Check for instantiation of atomic/volatile actual for
9936 -- non-atomic/volatile formal (RM C.6 (12)).
9938 if Is_Atomic_Object (Actual)
9939 and then not Is_Atomic (Orig_Ftyp)
9942 ("cannot instantiate non-atomic formal object " &
9943 "with atomic actual", Actual);
9945 elsif Is_Volatile_Object (Actual)
9946 and then not Is_Volatile (Orig_Ftyp)
9949 ("cannot instantiate non-volatile formal object " &
9950 "with volatile actual", Actual);
9953 -- Formal in-parameter
9956 -- The instantiation of a generic formal in-parameter is constant
9957 -- declaration. The actual is the expression for that declaration.
9959 if Present (Actual) then
9960 if Present (Subt_Mark) then
9962 else pragma Assert (Present (Acc_Def));
9967 Make_Object_Declaration (Loc,
9968 Defining_Identifier => New_Copy (Gen_Obj),
9969 Constant_Present => True,
9970 Null_Exclusion_Present => Null_Exclusion_Present (Formal),
9971 Object_Definition => New_Copy_Tree (Def),
9972 Expression => Actual);
9974 Set_Corresponding_Generic_Association (Decl_Node, Act_Assoc);
9976 -- A generic formal object of a tagged type is defined to be
9977 -- aliased so the new constant must also be treated as aliased.
9979 if Is_Tagged_Type (Etype (A_Gen_Obj)) then
9980 Set_Aliased_Present (Decl_Node);
9983 Append (Decl_Node, List);
9985 -- No need to repeat (pre-)analysis of some expression nodes
9986 -- already handled in Preanalyze_Actuals.
9988 if Nkind (Actual) /= N_Allocator then
9991 -- Return if the analysis of the actual reported some error
9993 if Etype (Actual) = Any_Type then
9999 Formal_Type : constant Entity_Id := Etype (A_Gen_Obj);
10003 Typ := Get_Instance_Of (Formal_Type);
10005 Freeze_Before (Instantiation_Node, Typ);
10007 -- If the actual is an aggregate, perform name resolution on
10008 -- its components (the analysis of an aggregate does not do it)
10009 -- to capture local names that may be hidden if the generic is
10012 if Nkind (Actual) = N_Aggregate then
10013 Preanalyze_And_Resolve (Actual, Typ);
10016 if Is_Limited_Type (Typ)
10017 and then not OK_For_Limited_Init (Typ, Actual)
10020 ("initialization not allowed for limited types", Actual);
10021 Explain_Limited_Type (Typ, Actual);
10025 elsif Present (Default_Expression (Formal)) then
10027 -- Use default to construct declaration
10029 if Present (Subt_Mark) then
10031 else pragma Assert (Present (Acc_Def));
10036 Make_Object_Declaration (Sloc (Formal),
10037 Defining_Identifier => New_Copy (Gen_Obj),
10038 Constant_Present => True,
10039 Null_Exclusion_Present => Null_Exclusion_Present (Formal),
10040 Object_Definition => New_Copy (Def),
10041 Expression => New_Copy_Tree
10042 (Default_Expression (Formal)));
10044 Append (Decl_Node, List);
10045 Set_Analyzed (Expression (Decl_Node), False);
10049 ("missing actual&",
10050 Instantiation_Node, Gen_Obj);
10051 Error_Msg_NE ("\in instantiation of & declared#",
10052 Instantiation_Node, Scope (A_Gen_Obj));
10054 if Is_Scalar_Type (Etype (A_Gen_Obj)) then
10056 -- Create dummy constant declaration so that instance can be
10057 -- analyzed, to minimize cascaded visibility errors.
10059 if Present (Subt_Mark) then
10061 else pragma Assert (Present (Acc_Def));
10066 Make_Object_Declaration (Loc,
10067 Defining_Identifier => New_Copy (Gen_Obj),
10068 Constant_Present => True,
10069 Null_Exclusion_Present => Null_Exclusion_Present (Formal),
10070 Object_Definition => New_Copy (Def),
10072 Make_Attribute_Reference (Sloc (Gen_Obj),
10073 Attribute_Name => Name_First,
10074 Prefix => New_Copy (Def)));
10076 Append (Decl_Node, List);
10079 Abandon_Instantiation (Instantiation_Node);
10084 if Nkind (Actual) in N_Has_Entity then
10085 Actual_Decl := Parent (Entity (Actual));
10088 -- Ada 2005 (AI-423): For a formal object declaration with a null
10089 -- exclusion or an access definition that has a null exclusion: If the
10090 -- actual matching the formal object declaration denotes a generic
10091 -- formal object of another generic unit G, and the instantiation
10092 -- containing the actual occurs within the body of G or within the body
10093 -- of a generic unit declared within the declarative region of G, then
10094 -- the declaration of the formal object of G must have a null exclusion.
10095 -- Otherwise, the subtype of the actual matching the formal object
10096 -- declaration shall exclude null.
10098 if Ada_Version >= Ada_2005
10099 and then Present (Actual_Decl)
10101 Nkind_In (Actual_Decl, N_Formal_Object_Declaration,
10102 N_Object_Declaration)
10103 and then Nkind (Analyzed_Formal) = N_Formal_Object_Declaration
10104 and then not Has_Null_Exclusion (Actual_Decl)
10105 and then Has_Null_Exclusion (Analyzed_Formal)
10107 Error_Msg_Sloc := Sloc (Analyzed_Formal);
10109 ("actual must exclude null to match generic formal#", Actual);
10112 -- An effectively volatile object cannot be used as an actual in
10113 -- a generic instance. The following check is only relevant when
10114 -- SPARK_Mode is on as it is not a standard Ada legality rule.
10117 and then Present (Actual)
10118 and then Is_Effectively_Volatile_Object (Actual)
10121 ("volatile object cannot act as actual in generic instantiation "
10122 & "(SPARK RM 7.1.3(8))", Actual);
10126 end Instantiate_Object;
10128 ------------------------------
10129 -- Instantiate_Package_Body --
10130 ------------------------------
10132 procedure Instantiate_Package_Body
10133 (Body_Info : Pending_Body_Info;
10134 Inlined_Body : Boolean := False;
10135 Body_Optional : Boolean := False)
10137 Act_Decl : constant Node_Id := Body_Info.Act_Decl;
10138 Inst_Node : constant Node_Id := Body_Info.Inst_Node;
10139 Loc : constant Source_Ptr := Sloc (Inst_Node);
10141 Gen_Id : constant Node_Id := Name (Inst_Node);
10142 Gen_Unit : constant Entity_Id := Get_Generic_Entity (Inst_Node);
10143 Gen_Decl : constant Node_Id := Unit_Declaration_Node (Gen_Unit);
10144 Act_Spec : constant Node_Id := Specification (Act_Decl);
10145 Act_Decl_Id : constant Entity_Id := Defining_Entity (Act_Spec);
10147 Act_Body_Name : Node_Id;
10148 Gen_Body : Node_Id;
10149 Gen_Body_Id : Node_Id;
10150 Act_Body : Node_Id;
10151 Act_Body_Id : Entity_Id;
10153 Parent_Installed : Boolean := False;
10154 Save_Style_Check : constant Boolean := Style_Check;
10156 Par_Ent : Entity_Id := Empty;
10157 Par_Vis : Boolean := False;
10159 Vis_Prims_List : Elist_Id := No_Elist;
10160 -- List of primitives made temporarily visible in the instantiation
10161 -- to match the visibility of the formal type
10163 procedure Check_Initialized_Types;
10164 -- In a generic package body, an entity of a generic private type may
10165 -- appear uninitialized. This is suspicious, unless the actual is a
10166 -- fully initialized type.
10168 -----------------------------
10169 -- Check_Initialized_Types --
10170 -----------------------------
10172 procedure Check_Initialized_Types is
10174 Formal : Entity_Id;
10175 Actual : Entity_Id;
10176 Uninit_Var : Entity_Id;
10179 Decl := First (Generic_Formal_Declarations (Gen_Decl));
10180 while Present (Decl) loop
10181 Uninit_Var := Empty;
10183 if Nkind (Decl) = N_Private_Extension_Declaration then
10184 Uninit_Var := Uninitialized_Variable (Decl);
10186 elsif Nkind (Decl) = N_Formal_Type_Declaration
10187 and then Nkind (Formal_Type_Definition (Decl)) =
10188 N_Formal_Private_Type_Definition
10191 Uninitialized_Variable (Formal_Type_Definition (Decl));
10194 if Present (Uninit_Var) then
10195 Formal := Defining_Identifier (Decl);
10196 Actual := First_Entity (Act_Decl_Id);
10198 -- For each formal there is a subtype declaration that renames
10199 -- the actual and has the same name as the formal. Locate the
10200 -- formal for warning message about uninitialized variables
10201 -- in the generic, for which the actual type should be a fully
10202 -- initialized type.
10204 while Present (Actual) loop
10205 exit when Ekind (Actual) = E_Package
10206 and then Present (Renamed_Object (Actual));
10208 if Chars (Actual) = Chars (Formal)
10209 and then not Is_Scalar_Type (Actual)
10210 and then not Is_Fully_Initialized_Type (Actual)
10211 and then Warn_On_No_Value_Assigned
10213 Error_Msg_Node_2 := Formal;
10215 ("generic unit has uninitialized variable& of "
10216 & "formal private type &?v?", Actual, Uninit_Var);
10218 ("actual type for& should be fully initialized type?v?",
10223 Next_Entity (Actual);
10229 end Check_Initialized_Types;
10231 -- Start of processing for Instantiate_Package_Body
10234 Gen_Body_Id := Corresponding_Body (Gen_Decl);
10236 -- The instance body may already have been processed, as the parent of
10237 -- another instance that is inlined (Load_Parent_Of_Generic).
10239 if Present (Corresponding_Body (Instance_Spec (Inst_Node))) then
10243 Expander_Mode_Save_And_Set (Body_Info.Expander_Status);
10245 -- Re-establish the state of information on which checks are suppressed.
10246 -- This information was set in Body_Info at the point of instantiation,
10247 -- and now we restore it so that the instance is compiled using the
10248 -- check status at the instantiation (RM 11.5 (7.2/2), AI95-00224-01).
10250 Local_Suppress_Stack_Top := Body_Info.Local_Suppress_Stack_Top;
10251 Scope_Suppress := Body_Info.Scope_Suppress;
10252 Opt.Ada_Version := Body_Info.Version;
10253 Opt.Ada_Version_Pragma := Body_Info.Version_Pragma;
10254 Restore_Warnings (Body_Info.Warnings);
10255 Opt.SPARK_Mode := Body_Info.SPARK_Mode;
10256 Opt.SPARK_Mode_Pragma := Body_Info.SPARK_Mode_Pragma;
10258 if No (Gen_Body_Id) then
10260 -- Do not look for parent of generic body if none is required.
10261 -- This may happen when the routine is called as part of the
10262 -- Pending_Instantiations processing, when nested instances
10263 -- may precede the one generated from the main unit.
10265 if not Unit_Requires_Body (Defining_Entity (Gen_Decl))
10266 and then Body_Optional
10270 Load_Parent_Of_Generic
10271 (Inst_Node, Specification (Gen_Decl), Body_Optional);
10272 Gen_Body_Id := Corresponding_Body (Gen_Decl);
10276 -- Establish global variable for sloc adjustment and for error recovery
10278 Instantiation_Node := Inst_Node;
10280 if Present (Gen_Body_Id) then
10281 Save_Env (Gen_Unit, Act_Decl_Id);
10282 Style_Check := False;
10283 Current_Sem_Unit := Body_Info.Current_Sem_Unit;
10285 Gen_Body := Unit_Declaration_Node (Gen_Body_Id);
10287 Create_Instantiation_Source
10288 (Inst_Node, Gen_Body_Id, False, S_Adjustment);
10292 (Original_Node (Gen_Body), Empty, Instantiating => True);
10294 -- Build new name (possibly qualified) for body declaration
10296 Act_Body_Id := New_Copy (Act_Decl_Id);
10298 -- Some attributes of spec entity are not inherited by body entity
10300 Set_Handler_Records (Act_Body_Id, No_List);
10302 if Nkind (Defining_Unit_Name (Act_Spec)) =
10303 N_Defining_Program_Unit_Name
10306 Make_Defining_Program_Unit_Name (Loc,
10307 Name => New_Copy_Tree (Name (Defining_Unit_Name (Act_Spec))),
10308 Defining_Identifier => Act_Body_Id);
10310 Act_Body_Name := Act_Body_Id;
10313 Set_Defining_Unit_Name (Act_Body, Act_Body_Name);
10315 Set_Corresponding_Spec (Act_Body, Act_Decl_Id);
10316 Check_Generic_Actuals (Act_Decl_Id, False);
10317 Check_Initialized_Types;
10319 -- Install primitives hidden at the point of the instantiation but
10320 -- visible when processing the generic formals
10326 E := First_Entity (Act_Decl_Id);
10327 while Present (E) loop
10329 and then Is_Generic_Actual_Type (E)
10330 and then Is_Tagged_Type (E)
10332 Install_Hidden_Primitives
10333 (Prims_List => Vis_Prims_List,
10334 Gen_T => Generic_Parent_Type (Parent (E)),
10342 -- If it is a child unit, make the parent instance (which is an
10343 -- instance of the parent of the generic) visible. The parent
10344 -- instance is the prefix of the name of the generic unit.
10346 if Ekind (Scope (Gen_Unit)) = E_Generic_Package
10347 and then Nkind (Gen_Id) = N_Expanded_Name
10349 Par_Ent := Entity (Prefix (Gen_Id));
10350 Par_Vis := Is_Immediately_Visible (Par_Ent);
10351 Install_Parent (Par_Ent, In_Body => True);
10352 Parent_Installed := True;
10354 elsif Is_Child_Unit (Gen_Unit) then
10355 Par_Ent := Scope (Gen_Unit);
10356 Par_Vis := Is_Immediately_Visible (Par_Ent);
10357 Install_Parent (Par_Ent, In_Body => True);
10358 Parent_Installed := True;
10361 -- If the instantiation is a library unit, and this is the main unit,
10362 -- then build the resulting compilation unit nodes for the instance.
10363 -- If this is a compilation unit but it is not the main unit, then it
10364 -- is the body of a unit in the context, that is being compiled
10365 -- because it is encloses some inlined unit or another generic unit
10366 -- being instantiated. In that case, this body is not part of the
10367 -- current compilation, and is not attached to the tree, but its
10368 -- parent must be set for analysis.
10370 if Nkind (Parent (Inst_Node)) = N_Compilation_Unit then
10372 -- Replace instance node with body of instance, and create new
10373 -- node for corresponding instance declaration.
10375 Build_Instance_Compilation_Unit_Nodes
10376 (Inst_Node, Act_Body, Act_Decl);
10377 Analyze (Inst_Node);
10379 if Parent (Inst_Node) = Cunit (Main_Unit) then
10381 -- If the instance is a child unit itself, then set the scope
10382 -- of the expanded body to be the parent of the instantiation
10383 -- (ensuring that the fully qualified name will be generated
10384 -- for the elaboration subprogram).
10386 if Nkind (Defining_Unit_Name (Act_Spec)) =
10387 N_Defining_Program_Unit_Name
10390 (Defining_Entity (Inst_Node), Scope (Act_Decl_Id));
10394 -- Case where instantiation is not a library unit
10397 -- If this is an early instantiation, i.e. appears textually
10398 -- before the corresponding body and must be elaborated first,
10399 -- indicate that the body instance is to be delayed.
10401 Install_Body (Act_Body, Inst_Node, Gen_Body, Gen_Decl);
10403 -- Now analyze the body. We turn off all checks if this is an
10404 -- internal unit, since there is no reason to have checks on for
10405 -- any predefined run-time library code. All such code is designed
10406 -- to be compiled with checks off.
10408 -- Note that we do NOT apply this criterion to children of GNAT
10409 -- The latter units must suppress checks explicitly if needed.
10411 if Is_Predefined_File_Name
10412 (Unit_File_Name (Get_Source_Unit (Gen_Decl)))
10414 Analyze (Act_Body, Suppress => All_Checks);
10416 Analyze (Act_Body);
10420 Inherit_Context (Gen_Body, Inst_Node);
10422 -- Remove the parent instances if they have been placed on the scope
10423 -- stack to compile the body.
10425 if Parent_Installed then
10426 Remove_Parent (In_Body => True);
10428 -- Restore the previous visibility of the parent
10430 Set_Is_Immediately_Visible (Par_Ent, Par_Vis);
10433 Restore_Hidden_Primitives (Vis_Prims_List);
10434 Restore_Private_Views (Act_Decl_Id);
10436 -- Remove the current unit from visibility if this is an instance
10437 -- that is not elaborated on the fly for inlining purposes.
10439 if not Inlined_Body then
10440 Set_Is_Immediately_Visible (Act_Decl_Id, False);
10444 Style_Check := Save_Style_Check;
10446 -- If we have no body, and the unit requires a body, then complain. This
10447 -- complaint is suppressed if we have detected other errors (since a
10448 -- common reason for missing the body is that it had errors).
10449 -- In CodePeer mode, a warning has been emitted already, no need for
10450 -- further messages.
10452 elsif Unit_Requires_Body (Gen_Unit)
10453 and then not Body_Optional
10455 if CodePeer_Mode then
10458 elsif Serious_Errors_Detected = 0 then
10460 ("cannot find body of generic package &", Inst_Node, Gen_Unit);
10462 -- Don't attempt to perform any cleanup actions if some other error
10463 -- was already detected, since this can cause blowups.
10469 -- Case of package that does not need a body
10472 -- If the instantiation of the declaration is a library unit, rewrite
10473 -- the original package instantiation as a package declaration in the
10474 -- compilation unit node.
10476 if Nkind (Parent (Inst_Node)) = N_Compilation_Unit then
10477 Set_Parent_Spec (Act_Decl, Parent_Spec (Inst_Node));
10478 Rewrite (Inst_Node, Act_Decl);
10480 -- Generate elaboration entity, in case spec has elaboration code.
10481 -- This cannot be done when the instance is analyzed, because it
10482 -- is not known yet whether the body exists.
10484 Set_Elaboration_Entity_Required (Act_Decl_Id, False);
10485 Build_Elaboration_Entity (Parent (Inst_Node), Act_Decl_Id);
10487 -- If the instantiation is not a library unit, then append the
10488 -- declaration to the list of implicitly generated entities, unless
10489 -- it is already a list member which means that it was already
10492 elsif not Is_List_Member (Act_Decl) then
10493 Mark_Rewrite_Insertion (Act_Decl);
10494 Insert_Before (Inst_Node, Act_Decl);
10498 Expander_Mode_Restore;
10499 end Instantiate_Package_Body;
10501 ---------------------------------
10502 -- Instantiate_Subprogram_Body --
10503 ---------------------------------
10505 procedure Instantiate_Subprogram_Body
10506 (Body_Info : Pending_Body_Info;
10507 Body_Optional : Boolean := False)
10509 Act_Decl : constant Node_Id := Body_Info.Act_Decl;
10510 Inst_Node : constant Node_Id := Body_Info.Inst_Node;
10511 Loc : constant Source_Ptr := Sloc (Inst_Node);
10512 Gen_Id : constant Node_Id := Name (Inst_Node);
10513 Gen_Unit : constant Entity_Id := Get_Generic_Entity (Inst_Node);
10514 Gen_Decl : constant Node_Id := Unit_Declaration_Node (Gen_Unit);
10515 Anon_Id : constant Entity_Id :=
10516 Defining_Unit_Name (Specification (Act_Decl));
10517 Pack_Id : constant Entity_Id :=
10518 Defining_Unit_Name (Parent (Act_Decl));
10520 Gen_Body : Node_Id;
10521 Gen_Body_Id : Node_Id;
10522 Act_Body : Node_Id;
10523 Pack_Body : Node_Id;
10524 Prev_Formal : Entity_Id;
10525 Ret_Expr : Node_Id;
10526 Unit_Renaming : Node_Id;
10528 Parent_Installed : Boolean := False;
10530 Saved_Style_Check : constant Boolean := Style_Check;
10531 Saved_Warnings : constant Warning_Record := Save_Warnings;
10533 Par_Ent : Entity_Id := Empty;
10534 Par_Vis : Boolean := False;
10537 Gen_Body_Id := Corresponding_Body (Gen_Decl);
10539 -- Subprogram body may have been created already because of an inline
10540 -- pragma, or because of multiple elaborations of the enclosing package
10541 -- when several instances of the subprogram appear in the main unit.
10543 if Present (Corresponding_Body (Act_Decl)) then
10547 Expander_Mode_Save_And_Set (Body_Info.Expander_Status);
10549 -- Re-establish the state of information on which checks are suppressed.
10550 -- This information was set in Body_Info at the point of instantiation,
10551 -- and now we restore it so that the instance is compiled using the
10552 -- check status at the instantiation (RM 11.5 (7.2/2), AI95-00224-01).
10554 Local_Suppress_Stack_Top := Body_Info.Local_Suppress_Stack_Top;
10555 Scope_Suppress := Body_Info.Scope_Suppress;
10556 Opt.Ada_Version := Body_Info.Version;
10557 Opt.Ada_Version_Pragma := Body_Info.Version_Pragma;
10558 Restore_Warnings (Body_Info.Warnings);
10559 Opt.SPARK_Mode := Body_Info.SPARK_Mode;
10560 Opt.SPARK_Mode_Pragma := Body_Info.SPARK_Mode_Pragma;
10562 if No (Gen_Body_Id) then
10564 -- For imported generic subprogram, no body to compile, complete
10565 -- the spec entity appropriately.
10567 if Is_Imported (Gen_Unit) then
10568 Set_Is_Imported (Anon_Id);
10569 Set_First_Rep_Item (Anon_Id, First_Rep_Item (Gen_Unit));
10570 Set_Interface_Name (Anon_Id, Interface_Name (Gen_Unit));
10571 Set_Convention (Anon_Id, Convention (Gen_Unit));
10572 Set_Has_Completion (Anon_Id);
10575 -- For other cases, compile the body
10578 Load_Parent_Of_Generic
10579 (Inst_Node, Specification (Gen_Decl), Body_Optional);
10580 Gen_Body_Id := Corresponding_Body (Gen_Decl);
10584 Instantiation_Node := Inst_Node;
10586 if Present (Gen_Body_Id) then
10587 Gen_Body := Unit_Declaration_Node (Gen_Body_Id);
10589 if Nkind (Gen_Body) = N_Subprogram_Body_Stub then
10591 -- Either body is not present, or context is non-expanding, as
10592 -- when compiling a subunit. Mark the instance as completed, and
10593 -- diagnose a missing body when needed.
10596 and then Operating_Mode = Generate_Code
10599 ("missing proper body for instantiation", Gen_Body);
10602 Set_Has_Completion (Anon_Id);
10606 Save_Env (Gen_Unit, Anon_Id);
10607 Style_Check := False;
10608 Current_Sem_Unit := Body_Info.Current_Sem_Unit;
10609 Create_Instantiation_Source
10617 (Original_Node (Gen_Body), Empty, Instantiating => True);
10619 -- Create proper defining name for the body, to correspond to
10620 -- the one in the spec.
10622 Set_Defining_Unit_Name (Specification (Act_Body),
10623 Make_Defining_Identifier
10624 (Sloc (Defining_Entity (Inst_Node)), Chars (Anon_Id)));
10625 Set_Corresponding_Spec (Act_Body, Anon_Id);
10626 Set_Has_Completion (Anon_Id);
10627 Check_Generic_Actuals (Pack_Id, False);
10629 -- Generate a reference to link the visible subprogram instance to
10630 -- the generic body, which for navigation purposes is the only
10631 -- available source for the instance.
10634 (Related_Instance (Pack_Id),
10635 Gen_Body_Id, 'b', Set_Ref => False, Force => True);
10637 -- If it is a child unit, make the parent instance (which is an
10638 -- instance of the parent of the generic) visible. The parent
10639 -- instance is the prefix of the name of the generic unit.
10641 if Ekind (Scope (Gen_Unit)) = E_Generic_Package
10642 and then Nkind (Gen_Id) = N_Expanded_Name
10644 Par_Ent := Entity (Prefix (Gen_Id));
10645 Par_Vis := Is_Immediately_Visible (Par_Ent);
10646 Install_Parent (Par_Ent, In_Body => True);
10647 Parent_Installed := True;
10649 elsif Is_Child_Unit (Gen_Unit) then
10650 Par_Ent := Scope (Gen_Unit);
10651 Par_Vis := Is_Immediately_Visible (Par_Ent);
10652 Install_Parent (Par_Ent, In_Body => True);
10653 Parent_Installed := True;
10656 -- Inside its body, a reference to the generic unit is a reference
10657 -- to the instance. The corresponding renaming is the first
10658 -- declaration in the body.
10661 Make_Subprogram_Renaming_Declaration (Loc,
10663 Copy_Generic_Node (
10664 Specification (Original_Node (Gen_Body)),
10666 Instantiating => True),
10667 Name => New_Occurrence_Of (Anon_Id, Loc));
10669 -- If there is a formal subprogram with the same name as the unit
10670 -- itself, do not add this renaming declaration. This is a temporary
10671 -- fix for one ACVC test. ???
10673 Prev_Formal := First_Entity (Pack_Id);
10674 while Present (Prev_Formal) loop
10675 if Chars (Prev_Formal) = Chars (Gen_Unit)
10676 and then Is_Overloadable (Prev_Formal)
10681 Next_Entity (Prev_Formal);
10684 if Present (Prev_Formal) then
10685 Decls := New_List (Act_Body);
10687 Decls := New_List (Unit_Renaming, Act_Body);
10690 -- The subprogram body is placed in the body of a dummy package body,
10691 -- whose spec contains the subprogram declaration as well as the
10692 -- renaming declarations for the generic parameters.
10694 Pack_Body := Make_Package_Body (Loc,
10695 Defining_Unit_Name => New_Copy (Pack_Id),
10696 Declarations => Decls);
10698 Set_Corresponding_Spec (Pack_Body, Pack_Id);
10700 -- If the instantiation is a library unit, then build resulting
10701 -- compilation unit nodes for the instance. The declaration of
10702 -- the enclosing package is the grandparent of the subprogram
10703 -- declaration. First replace the instantiation node as the unit
10704 -- of the corresponding compilation.
10706 if Nkind (Parent (Inst_Node)) = N_Compilation_Unit then
10707 if Parent (Inst_Node) = Cunit (Main_Unit) then
10708 Set_Unit (Parent (Inst_Node), Inst_Node);
10709 Build_Instance_Compilation_Unit_Nodes
10710 (Inst_Node, Pack_Body, Parent (Parent (Act_Decl)));
10711 Analyze (Inst_Node);
10713 Set_Parent (Pack_Body, Parent (Inst_Node));
10714 Analyze (Pack_Body);
10718 Insert_Before (Inst_Node, Pack_Body);
10719 Mark_Rewrite_Insertion (Pack_Body);
10720 Analyze (Pack_Body);
10722 if Expander_Active then
10723 Freeze_Subprogram_Body (Inst_Node, Gen_Body, Pack_Id);
10727 Inherit_Context (Gen_Body, Inst_Node);
10729 Restore_Private_Views (Pack_Id, False);
10731 if Parent_Installed then
10732 Remove_Parent (In_Body => True);
10734 -- Restore the previous visibility of the parent
10736 Set_Is_Immediately_Visible (Par_Ent, Par_Vis);
10740 Style_Check := Saved_Style_Check;
10741 Restore_Warnings (Saved_Warnings);
10743 -- Body not found. Error was emitted already. If there were no previous
10744 -- errors, this may be an instance whose scope is a premature instance.
10745 -- In that case we must insure that the (legal) program does raise
10746 -- program error if executed. We generate a subprogram body for this
10747 -- purpose. See DEC ac30vso.
10749 -- Should not reference proprietary DEC tests in comments ???
10751 elsif Serious_Errors_Detected = 0
10752 and then Nkind (Parent (Inst_Node)) /= N_Compilation_Unit
10754 if Body_Optional then
10757 elsif Ekind (Anon_Id) = E_Procedure then
10759 Make_Subprogram_Body (Loc,
10761 Make_Procedure_Specification (Loc,
10762 Defining_Unit_Name =>
10763 Make_Defining_Identifier (Loc, Chars (Anon_Id)),
10764 Parameter_Specifications =>
10766 (Parameter_Specifications (Parent (Anon_Id)))),
10768 Declarations => Empty_List,
10769 Handled_Statement_Sequence =>
10770 Make_Handled_Sequence_Of_Statements (Loc,
10773 Make_Raise_Program_Error (Loc,
10775 PE_Access_Before_Elaboration))));
10779 Make_Raise_Program_Error (Loc,
10780 Reason => PE_Access_Before_Elaboration);
10782 Set_Etype (Ret_Expr, (Etype (Anon_Id)));
10783 Set_Analyzed (Ret_Expr);
10786 Make_Subprogram_Body (Loc,
10788 Make_Function_Specification (Loc,
10789 Defining_Unit_Name =>
10790 Make_Defining_Identifier (Loc, Chars (Anon_Id)),
10791 Parameter_Specifications =>
10793 (Parameter_Specifications (Parent (Anon_Id))),
10794 Result_Definition =>
10795 New_Occurrence_Of (Etype (Anon_Id), Loc)),
10797 Declarations => Empty_List,
10798 Handled_Statement_Sequence =>
10799 Make_Handled_Sequence_Of_Statements (Loc,
10802 (Make_Simple_Return_Statement (Loc, Ret_Expr))));
10805 Pack_Body := Make_Package_Body (Loc,
10806 Defining_Unit_Name => New_Copy (Pack_Id),
10807 Declarations => New_List (Act_Body));
10809 Insert_After (Inst_Node, Pack_Body);
10810 Set_Corresponding_Spec (Pack_Body, Pack_Id);
10811 Analyze (Pack_Body);
10814 Expander_Mode_Restore;
10815 end Instantiate_Subprogram_Body;
10817 ----------------------
10818 -- Instantiate_Type --
10819 ----------------------
10821 function Instantiate_Type
10824 Analyzed_Formal : Node_Id;
10825 Actual_Decls : List_Id) return List_Id
10827 Gen_T : constant Entity_Id := Defining_Identifier (Formal);
10828 A_Gen_T : constant Entity_Id :=
10829 Defining_Identifier (Analyzed_Formal);
10830 Ancestor : Entity_Id := Empty;
10831 Def : constant Node_Id := Formal_Type_Definition (Formal);
10833 Decl_Node : Node_Id;
10834 Decl_Nodes : List_Id;
10838 procedure Diagnose_Predicated_Actual;
10839 -- There are a number of constructs in which a discrete type with
10840 -- predicates is illegal, e.g. as an index in an array type declaration.
10841 -- If a generic type is used is such a construct in a generic package
10842 -- declaration, it carries the flag No_Predicate_On_Actual. it is part
10843 -- of the generic contract that the actual cannot have predicates.
10845 procedure Validate_Array_Type_Instance;
10846 procedure Validate_Access_Subprogram_Instance;
10847 procedure Validate_Access_Type_Instance;
10848 procedure Validate_Derived_Type_Instance;
10849 procedure Validate_Derived_Interface_Type_Instance;
10850 procedure Validate_Discriminated_Formal_Type;
10851 procedure Validate_Interface_Type_Instance;
10852 procedure Validate_Private_Type_Instance;
10853 procedure Validate_Incomplete_Type_Instance;
10854 -- These procedures perform validation tests for the named case.
10855 -- Validate_Discriminated_Formal_Type is shared by formal private
10856 -- types and Ada 2012 formal incomplete types.
10858 function Subtypes_Match (Gen_T, Act_T : Entity_Id) return Boolean;
10859 -- Check that base types are the same and that the subtypes match
10860 -- statically. Used in several of the above.
10862 ---------------------------------
10863 -- Diagnose_Predicated_Actual --
10864 ---------------------------------
10866 procedure Diagnose_Predicated_Actual is
10868 if No_Predicate_On_Actual (A_Gen_T)
10869 and then Has_Predicates (Act_T)
10872 ("actual for& cannot be a type with predicate",
10873 Instantiation_Node, A_Gen_T);
10875 elsif No_Dynamic_Predicate_On_Actual (A_Gen_T)
10876 and then Has_Predicates (Act_T)
10877 and then not Has_Static_Predicate_Aspect (Act_T)
10880 ("actual for& cannot be a type with a dynamic predicate",
10881 Instantiation_Node, A_Gen_T);
10883 end Diagnose_Predicated_Actual;
10885 --------------------
10886 -- Subtypes_Match --
10887 --------------------
10889 function Subtypes_Match (Gen_T, Act_T : Entity_Id) return Boolean is
10890 T : constant Entity_Id := Get_Instance_Of (Gen_T);
10893 -- Some detailed comments would be useful here ???
10895 return ((Base_Type (T) = Act_T
10896 or else Base_Type (T) = Base_Type (Act_T))
10897 and then Subtypes_Statically_Match (T, Act_T))
10899 or else (Is_Class_Wide_Type (Gen_T)
10900 and then Is_Class_Wide_Type (Act_T)
10901 and then Subtypes_Match
10902 (Get_Instance_Of (Root_Type (Gen_T)),
10903 Root_Type (Act_T)))
10906 (Ekind_In (Gen_T, E_Anonymous_Access_Subprogram_Type,
10907 E_Anonymous_Access_Type)
10908 and then Ekind (Act_T) = Ekind (Gen_T)
10909 and then Subtypes_Statically_Match
10910 (Designated_Type (Gen_T), Designated_Type (Act_T)));
10911 end Subtypes_Match;
10913 -----------------------------------------
10914 -- Validate_Access_Subprogram_Instance --
10915 -----------------------------------------
10917 procedure Validate_Access_Subprogram_Instance is
10919 if not Is_Access_Type (Act_T)
10920 or else Ekind (Designated_Type (Act_T)) /= E_Subprogram_Type
10923 ("expect access type in instantiation of &", Actual, Gen_T);
10924 Abandon_Instantiation (Actual);
10927 -- According to AI05-288, actuals for access_to_subprograms must be
10928 -- subtype conformant with the generic formal. Previous to AI05-288
10929 -- only mode conformance was required.
10931 -- This is a binding interpretation that applies to previous versions
10932 -- of the language, no need to maintain previous weaker checks.
10934 Check_Subtype_Conformant
10935 (Designated_Type (Act_T),
10936 Designated_Type (A_Gen_T),
10940 if Ekind (Base_Type (Act_T)) = E_Access_Protected_Subprogram_Type then
10941 if Ekind (A_Gen_T) = E_Access_Subprogram_Type then
10943 ("protected access type not allowed for formal &",
10947 elsif Ekind (A_Gen_T) = E_Access_Protected_Subprogram_Type then
10949 ("expect protected access type for formal &",
10952 end Validate_Access_Subprogram_Instance;
10954 -----------------------------------
10955 -- Validate_Access_Type_Instance --
10956 -----------------------------------
10958 procedure Validate_Access_Type_Instance is
10959 Desig_Type : constant Entity_Id :=
10960 Find_Actual_Type (Designated_Type (A_Gen_T), A_Gen_T);
10961 Desig_Act : Entity_Id;
10964 if not Is_Access_Type (Act_T) then
10966 ("expect access type in instantiation of &", Actual, Gen_T);
10967 Abandon_Instantiation (Actual);
10970 if Is_Access_Constant (A_Gen_T) then
10971 if not Is_Access_Constant (Act_T) then
10973 ("actual type must be access-to-constant type", Actual);
10974 Abandon_Instantiation (Actual);
10977 if Is_Access_Constant (Act_T) then
10979 ("actual type must be access-to-variable type", Actual);
10980 Abandon_Instantiation (Actual);
10982 elsif Ekind (A_Gen_T) = E_General_Access_Type
10983 and then Ekind (Base_Type (Act_T)) /= E_General_Access_Type
10985 Error_Msg_N -- CODEFIX
10986 ("actual must be general access type!", Actual);
10987 Error_Msg_NE -- CODEFIX
10988 ("add ALL to }!", Actual, Act_T);
10989 Abandon_Instantiation (Actual);
10993 -- The designated subtypes, that is to say the subtypes introduced
10994 -- by an access type declaration (and not by a subtype declaration)
10997 Desig_Act := Designated_Type (Base_Type (Act_T));
10999 -- The designated type may have been introduced through a limited_
11000 -- with clause, in which case retrieve the non-limited view. This
11001 -- applies to incomplete types as well as to class-wide types.
11003 if From_Limited_With (Desig_Act) then
11004 Desig_Act := Available_View (Desig_Act);
11007 if not Subtypes_Match (Desig_Type, Desig_Act) then
11009 ("designated type of actual does not match that of formal &",
11012 if not Predicates_Match (Desig_Type, Desig_Act) then
11013 Error_Msg_N ("\predicates do not match", Actual);
11016 Abandon_Instantiation (Actual);
11018 elsif Is_Access_Type (Designated_Type (Act_T))
11019 and then Is_Constrained (Designated_Type (Designated_Type (Act_T)))
11021 Is_Constrained (Designated_Type (Desig_Type))
11024 ("designated type of actual does not match that of formal &",
11027 if not Predicates_Match (Desig_Type, Desig_Act) then
11028 Error_Msg_N ("\predicates do not match", Actual);
11031 Abandon_Instantiation (Actual);
11034 -- Ada 2005: null-exclusion indicators of the two types must agree
11036 if Can_Never_Be_Null (A_Gen_T) /= Can_Never_Be_Null (Act_T) then
11038 ("non null exclusion of actual and formal & do not match",
11041 end Validate_Access_Type_Instance;
11043 ----------------------------------
11044 -- Validate_Array_Type_Instance --
11045 ----------------------------------
11047 procedure Validate_Array_Type_Instance is
11052 function Formal_Dimensions return Int;
11053 -- Count number of dimensions in array type formal
11055 -----------------------
11056 -- Formal_Dimensions --
11057 -----------------------
11059 function Formal_Dimensions return Int is
11064 if Nkind (Def) = N_Constrained_Array_Definition then
11065 Index := First (Discrete_Subtype_Definitions (Def));
11067 Index := First (Subtype_Marks (Def));
11070 while Present (Index) loop
11072 Next_Index (Index);
11076 end Formal_Dimensions;
11078 -- Start of processing for Validate_Array_Type_Instance
11081 if not Is_Array_Type (Act_T) then
11083 ("expect array type in instantiation of &", Actual, Gen_T);
11084 Abandon_Instantiation (Actual);
11086 elsif Nkind (Def) = N_Constrained_Array_Definition then
11087 if not (Is_Constrained (Act_T)) then
11089 ("expect constrained array in instantiation of &",
11091 Abandon_Instantiation (Actual);
11095 if Is_Constrained (Act_T) then
11097 ("expect unconstrained array in instantiation of &",
11099 Abandon_Instantiation (Actual);
11103 if Formal_Dimensions /= Number_Dimensions (Act_T) then
11105 ("dimensions of actual do not match formal &", Actual, Gen_T);
11106 Abandon_Instantiation (Actual);
11109 I1 := First_Index (A_Gen_T);
11110 I2 := First_Index (Act_T);
11111 for J in 1 .. Formal_Dimensions loop
11113 -- If the indexes of the actual were given by a subtype_mark,
11114 -- the index was transformed into a range attribute. Retrieve
11115 -- the original type mark for checking.
11117 if Is_Entity_Name (Original_Node (I2)) then
11118 T2 := Entity (Original_Node (I2));
11123 if not Subtypes_Match
11124 (Find_Actual_Type (Etype (I1), A_Gen_T), T2)
11127 ("index types of actual do not match those of formal &",
11129 Abandon_Instantiation (Actual);
11136 -- Check matching subtypes. Note that there are complex visibility
11137 -- issues when the generic is a child unit and some aspect of the
11138 -- generic type is declared in a parent unit of the generic. We do
11139 -- the test to handle this special case only after a direct check
11140 -- for static matching has failed. The case where both the component
11141 -- type and the array type are separate formals, and the component
11142 -- type is a private view may also require special checking in
11146 (Component_Type (A_Gen_T), Component_Type (Act_T))
11147 or else Subtypes_Match
11148 (Find_Actual_Type (Component_Type (A_Gen_T), A_Gen_T),
11149 Component_Type (Act_T))
11154 ("component subtype of actual does not match that of formal &",
11156 Abandon_Instantiation (Actual);
11159 if Has_Aliased_Components (A_Gen_T)
11160 and then not Has_Aliased_Components (Act_T)
11163 ("actual must have aliased components to match formal type &",
11166 end Validate_Array_Type_Instance;
11168 -----------------------------------------------
11169 -- Validate_Derived_Interface_Type_Instance --
11170 -----------------------------------------------
11172 procedure Validate_Derived_Interface_Type_Instance is
11173 Par : constant Entity_Id := Entity (Subtype_Indication (Def));
11177 -- First apply interface instance checks
11179 Validate_Interface_Type_Instance;
11181 -- Verify that immediate parent interface is an ancestor of
11185 and then not Interface_Present_In_Ancestor (Act_T, Par)
11188 ("interface actual must include progenitor&", Actual, Par);
11191 -- Now verify that the actual includes all other ancestors of
11194 Elmt := First_Elmt (Interfaces (A_Gen_T));
11195 while Present (Elmt) loop
11196 if not Interface_Present_In_Ancestor
11197 (Act_T, Get_Instance_Of (Node (Elmt)))
11200 ("interface actual must include progenitor&",
11201 Actual, Node (Elmt));
11206 end Validate_Derived_Interface_Type_Instance;
11208 ------------------------------------
11209 -- Validate_Derived_Type_Instance --
11210 ------------------------------------
11212 procedure Validate_Derived_Type_Instance is
11213 Actual_Discr : Entity_Id;
11214 Ancestor_Discr : Entity_Id;
11217 -- If the parent type in the generic declaration is itself a previous
11218 -- formal type, then it is local to the generic and absent from the
11219 -- analyzed generic definition. In that case the ancestor is the
11220 -- instance of the formal (which must have been instantiated
11221 -- previously), unless the ancestor is itself a formal derived type.
11222 -- In this latter case (which is the subject of Corrigendum 8652/0038
11223 -- (AI-202) the ancestor of the formals is the ancestor of its
11224 -- parent. Otherwise, the analyzed generic carries the parent type.
11225 -- If the parent type is defined in a previous formal package, then
11226 -- the scope of that formal package is that of the generic type
11227 -- itself, and it has already been mapped into the corresponding type
11228 -- in the actual package.
11230 -- Common case: parent type defined outside of the generic
11232 if Is_Entity_Name (Subtype_Mark (Def))
11233 and then Present (Entity (Subtype_Mark (Def)))
11235 Ancestor := Get_Instance_Of (Entity (Subtype_Mark (Def)));
11237 -- Check whether parent is defined in a previous formal package
11240 Scope (Scope (Base_Type (Etype (A_Gen_T)))) = Scope (A_Gen_T)
11243 Get_Instance_Of (Base_Type (Etype (A_Gen_T)));
11245 -- The type may be a local derivation, or a type extension of a
11246 -- previous formal, or of a formal of a parent package.
11248 elsif Is_Derived_Type (Get_Instance_Of (A_Gen_T))
11250 Ekind (Get_Instance_Of (A_Gen_T)) = E_Record_Type_With_Private
11252 -- Check whether the parent is another derived formal type in the
11253 -- same generic unit.
11255 if Etype (A_Gen_T) /= A_Gen_T
11256 and then Is_Generic_Type (Etype (A_Gen_T))
11257 and then Scope (Etype (A_Gen_T)) = Scope (A_Gen_T)
11258 and then Etype (Etype (A_Gen_T)) /= Etype (A_Gen_T)
11260 -- Locate ancestor of parent from the subtype declaration
11261 -- created for the actual.
11267 Decl := First (Actual_Decls);
11268 while Present (Decl) loop
11269 if Nkind (Decl) = N_Subtype_Declaration
11270 and then Chars (Defining_Identifier (Decl)) =
11271 Chars (Etype (A_Gen_T))
11273 Ancestor := Generic_Parent_Type (Decl);
11281 pragma Assert (Present (Ancestor));
11283 -- The ancestor itself may be a previous formal that has been
11286 Ancestor := Get_Instance_Of (Ancestor);
11290 Get_Instance_Of (Base_Type (Get_Instance_Of (A_Gen_T)));
11293 -- An unusual case: the actual is a type declared in a parent unit,
11294 -- but is not a formal type so there is no instance_of for it.
11295 -- Retrieve it by analyzing the record extension.
11297 elsif Is_Child_Unit (Scope (A_Gen_T))
11298 and then In_Open_Scopes (Scope (Act_T))
11299 and then Is_Generic_Instance (Scope (Act_T))
11301 Analyze (Subtype_Mark (Def));
11302 Ancestor := Entity (Subtype_Mark (Def));
11305 Ancestor := Get_Instance_Of (Etype (Base_Type (A_Gen_T)));
11308 -- If the formal derived type has pragma Preelaborable_Initialization
11309 -- then the actual type must have preelaborable initialization.
11311 if Known_To_Have_Preelab_Init (A_Gen_T)
11312 and then not Has_Preelaborable_Initialization (Act_T)
11315 ("actual for & must have preelaborable initialization",
11319 -- Ada 2005 (AI-251)
11321 if Ada_Version >= Ada_2005 and then Is_Interface (Ancestor) then
11322 if not Interface_Present_In_Ancestor (Act_T, Ancestor) then
11324 ("(Ada 2005) expected type implementing & in instantiation",
11328 elsif not Is_Ancestor (Base_Type (Ancestor), Act_T) then
11330 ("expect type derived from & in instantiation",
11331 Actual, First_Subtype (Ancestor));
11332 Abandon_Instantiation (Actual);
11335 -- Ada 2005 (AI-443): Synchronized formal derived type checks. Note
11336 -- that the formal type declaration has been rewritten as a private
11339 if Ada_Version >= Ada_2005
11340 and then Nkind (Parent (A_Gen_T)) = N_Private_Extension_Declaration
11341 and then Synchronized_Present (Parent (A_Gen_T))
11343 -- The actual must be a synchronized tagged type
11345 if not Is_Tagged_Type (Act_T) then
11347 ("actual of synchronized type must be tagged", Actual);
11348 Abandon_Instantiation (Actual);
11350 elsif Nkind (Parent (Act_T)) = N_Full_Type_Declaration
11351 and then Nkind (Type_Definition (Parent (Act_T))) =
11352 N_Derived_Type_Definition
11353 and then not Synchronized_Present (Type_Definition
11357 ("actual of synchronized type must be synchronized", Actual);
11358 Abandon_Instantiation (Actual);
11362 -- Perform atomic/volatile checks (RM C.6(12)). Note that AI05-0218-1
11363 -- removes the second instance of the phrase "or allow pass by copy".
11365 if Is_Atomic (Act_T) and then not Is_Atomic (Ancestor) then
11367 ("cannot have atomic actual type for non-atomic formal type",
11370 elsif Is_Volatile (Act_T) and then not Is_Volatile (Ancestor) then
11372 ("cannot have volatile actual type for non-volatile formal type",
11376 -- It should not be necessary to check for unknown discriminants on
11377 -- Formal, but for some reason Has_Unknown_Discriminants is false for
11378 -- A_Gen_T, so Is_Indefinite_Subtype incorrectly returns False. This
11379 -- needs fixing. ???
11381 if not Is_Indefinite_Subtype (A_Gen_T)
11382 and then not Unknown_Discriminants_Present (Formal)
11383 and then Is_Indefinite_Subtype (Act_T)
11386 ("actual subtype must be constrained", Actual);
11387 Abandon_Instantiation (Actual);
11390 if not Unknown_Discriminants_Present (Formal) then
11391 if Is_Constrained (Ancestor) then
11392 if not Is_Constrained (Act_T) then
11394 ("actual subtype must be constrained", Actual);
11395 Abandon_Instantiation (Actual);
11398 -- Ancestor is unconstrained, Check if generic formal and actual
11399 -- agree on constrainedness. The check only applies to array types
11400 -- and discriminated types.
11402 elsif Is_Constrained (Act_T) then
11403 if Ekind (Ancestor) = E_Access_Type
11405 (not Is_Constrained (A_Gen_T)
11406 and then Is_Composite_Type (A_Gen_T))
11409 ("actual subtype must be unconstrained", Actual);
11410 Abandon_Instantiation (Actual);
11413 -- A class-wide type is only allowed if the formal has unknown
11416 elsif Is_Class_Wide_Type (Act_T)
11417 and then not Has_Unknown_Discriminants (Ancestor)
11420 ("actual for & cannot be a class-wide type", Actual, Gen_T);
11421 Abandon_Instantiation (Actual);
11423 -- Otherwise, the formal and actual must have the same number
11424 -- of discriminants and each discriminant of the actual must
11425 -- correspond to a discriminant of the formal.
11427 elsif Has_Discriminants (Act_T)
11428 and then not Has_Unknown_Discriminants (Act_T)
11429 and then Has_Discriminants (Ancestor)
11431 Actual_Discr := First_Discriminant (Act_T);
11432 Ancestor_Discr := First_Discriminant (Ancestor);
11433 while Present (Actual_Discr)
11434 and then Present (Ancestor_Discr)
11436 if Base_Type (Act_T) /= Base_Type (Ancestor) and then
11437 No (Corresponding_Discriminant (Actual_Discr))
11440 ("discriminant & does not correspond " &
11441 "to ancestor discriminant", Actual, Actual_Discr);
11442 Abandon_Instantiation (Actual);
11445 Next_Discriminant (Actual_Discr);
11446 Next_Discriminant (Ancestor_Discr);
11449 if Present (Actual_Discr) or else Present (Ancestor_Discr) then
11451 ("actual for & must have same number of discriminants",
11453 Abandon_Instantiation (Actual);
11456 -- This case should be caught by the earlier check for
11457 -- constrainedness, but the check here is added for completeness.
11459 elsif Has_Discriminants (Act_T)
11460 and then not Has_Unknown_Discriminants (Act_T)
11463 ("actual for & must not have discriminants", Actual, Gen_T);
11464 Abandon_Instantiation (Actual);
11466 elsif Has_Discriminants (Ancestor) then
11468 ("actual for & must have known discriminants", Actual, Gen_T);
11469 Abandon_Instantiation (Actual);
11472 if not Subtypes_Statically_Compatible
11473 (Act_T, Ancestor, Formal_Derived_Matching => True)
11476 ("constraint on actual is incompatible with formal", Actual);
11477 Abandon_Instantiation (Actual);
11481 -- If the formal and actual types are abstract, check that there
11482 -- are no abstract primitives of the actual type that correspond to
11483 -- nonabstract primitives of the formal type (second sentence of
11486 if Is_Abstract_Type (A_Gen_T) and then Is_Abstract_Type (Act_T) then
11487 Check_Abstract_Primitives : declare
11488 Gen_Prims : constant Elist_Id :=
11489 Primitive_Operations (A_Gen_T);
11490 Gen_Elmt : Elmt_Id;
11491 Gen_Subp : Entity_Id;
11492 Anc_Subp : Entity_Id;
11493 Anc_Formal : Entity_Id;
11494 Anc_F_Type : Entity_Id;
11496 Act_Prims : constant Elist_Id := Primitive_Operations (Act_T);
11497 Act_Elmt : Elmt_Id;
11498 Act_Subp : Entity_Id;
11499 Act_Formal : Entity_Id;
11500 Act_F_Type : Entity_Id;
11502 Subprograms_Correspond : Boolean;
11504 function Is_Tagged_Ancestor (T1, T2 : Entity_Id) return Boolean;
11505 -- Returns true if T2 is derived directly or indirectly from
11506 -- T1, including derivations from interfaces. T1 and T2 are
11507 -- required to be specific tagged base types.
11509 ------------------------
11510 -- Is_Tagged_Ancestor --
11511 ------------------------
11513 function Is_Tagged_Ancestor (T1, T2 : Entity_Id) return Boolean
11515 Intfc_Elmt : Elmt_Id;
11518 -- The predicate is satisfied if the types are the same
11523 -- If we've reached the top of the derivation chain then
11524 -- we know that T1 is not an ancestor of T2.
11526 elsif Etype (T2) = T2 then
11529 -- Proceed to check T2's immediate parent
11531 elsif Is_Ancestor (T1, Base_Type (Etype (T2))) then
11534 -- Finally, check to see if T1 is an ancestor of any of T2's
11538 Intfc_Elmt := First_Elmt (Interfaces (T2));
11539 while Present (Intfc_Elmt) loop
11540 if Is_Ancestor (T1, Node (Intfc_Elmt)) then
11544 Next_Elmt (Intfc_Elmt);
11549 end Is_Tagged_Ancestor;
11551 -- Start of processing for Check_Abstract_Primitives
11554 -- Loop over all of the formal derived type's primitives
11556 Gen_Elmt := First_Elmt (Gen_Prims);
11557 while Present (Gen_Elmt) loop
11558 Gen_Subp := Node (Gen_Elmt);
11560 -- If the primitive of the formal is not abstract, then
11561 -- determine whether there is a corresponding primitive of
11562 -- the actual type that's abstract.
11564 if not Is_Abstract_Subprogram (Gen_Subp) then
11565 Act_Elmt := First_Elmt (Act_Prims);
11566 while Present (Act_Elmt) loop
11567 Act_Subp := Node (Act_Elmt);
11569 -- If we find an abstract primitive of the actual,
11570 -- then we need to test whether it corresponds to the
11571 -- subprogram from which the generic formal primitive
11574 if Is_Abstract_Subprogram (Act_Subp) then
11575 Anc_Subp := Alias (Gen_Subp);
11577 -- Test whether we have a corresponding primitive
11578 -- by comparing names, kinds, formal types, and
11581 if Chars (Anc_Subp) = Chars (Act_Subp)
11582 and then Ekind (Anc_Subp) = Ekind (Act_Subp)
11584 Anc_Formal := First_Formal (Anc_Subp);
11585 Act_Formal := First_Formal (Act_Subp);
11586 while Present (Anc_Formal)
11587 and then Present (Act_Formal)
11589 Anc_F_Type := Etype (Anc_Formal);
11590 Act_F_Type := Etype (Act_Formal);
11592 if Ekind (Anc_F_Type)
11593 = E_Anonymous_Access_Type
11595 Anc_F_Type := Designated_Type (Anc_F_Type);
11597 if Ekind (Act_F_Type)
11598 = E_Anonymous_Access_Type
11601 Designated_Type (Act_F_Type);
11607 Ekind (Act_F_Type) = E_Anonymous_Access_Type
11612 Anc_F_Type := Base_Type (Anc_F_Type);
11613 Act_F_Type := Base_Type (Act_F_Type);
11615 -- If the formal is controlling, then the
11616 -- the type of the actual primitive's formal
11617 -- must be derived directly or indirectly
11618 -- from the type of the ancestor primitive's
11621 if Is_Controlling_Formal (Anc_Formal) then
11622 if not Is_Tagged_Ancestor
11623 (Anc_F_Type, Act_F_Type)
11628 -- Otherwise the types of the formals must
11631 elsif Anc_F_Type /= Act_F_Type then
11635 Next_Entity (Anc_Formal);
11636 Next_Entity (Act_Formal);
11639 -- If we traversed through all of the formals
11640 -- then so far the subprograms correspond, so
11641 -- now check that any result types correspond.
11643 if No (Anc_Formal) and then No (Act_Formal) then
11644 Subprograms_Correspond := True;
11646 if Ekind (Act_Subp) = E_Function then
11647 Anc_F_Type := Etype (Anc_Subp);
11648 Act_F_Type := Etype (Act_Subp);
11650 if Ekind (Anc_F_Type)
11651 = E_Anonymous_Access_Type
11654 Designated_Type (Anc_F_Type);
11656 if Ekind (Act_F_Type)
11657 = E_Anonymous_Access_Type
11660 Designated_Type (Act_F_Type);
11662 Subprograms_Correspond := False;
11667 = E_Anonymous_Access_Type
11669 Subprograms_Correspond := False;
11672 Anc_F_Type := Base_Type (Anc_F_Type);
11673 Act_F_Type := Base_Type (Act_F_Type);
11675 -- Now either the result types must be
11676 -- the same or, if the result type is
11677 -- controlling, the result type of the
11678 -- actual primitive must descend from the
11679 -- result type of the ancestor primitive.
11681 if Subprograms_Correspond
11682 and then Anc_F_Type /= Act_F_Type
11684 Has_Controlling_Result (Anc_Subp)
11686 not Is_Tagged_Ancestor
11687 (Anc_F_Type, Act_F_Type)
11689 Subprograms_Correspond := False;
11693 -- Found a matching subprogram belonging to
11694 -- formal ancestor type, so actual subprogram
11695 -- corresponds and this violates 3.9.3(9).
11697 if Subprograms_Correspond then
11699 ("abstract subprogram & overrides " &
11700 "nonabstract subprogram of ancestor",
11708 Next_Elmt (Act_Elmt);
11712 Next_Elmt (Gen_Elmt);
11714 end Check_Abstract_Primitives;
11717 -- Verify that limitedness matches. If parent is a limited
11718 -- interface then the generic formal is not unless declared
11719 -- explicitly so. If not declared limited, the actual cannot be
11720 -- limited (see AI05-0087).
11722 -- Even though this AI is a binding interpretation, we enable the
11723 -- check only in Ada 2012 mode, because this improper construct
11724 -- shows up in user code and in existing B-tests.
11726 if Is_Limited_Type (Act_T)
11727 and then not Is_Limited_Type (A_Gen_T)
11728 and then Ada_Version >= Ada_2012
11730 if In_Instance then
11734 ("actual for non-limited & cannot be a limited type", Actual,
11736 Explain_Limited_Type (Act_T, Actual);
11737 Abandon_Instantiation (Actual);
11740 end Validate_Derived_Type_Instance;
11742 ----------------------------------------
11743 -- Validate_Discriminated_Formal_Type --
11744 ----------------------------------------
11746 procedure Validate_Discriminated_Formal_Type is
11747 Formal_Discr : Entity_Id;
11748 Actual_Discr : Entity_Id;
11749 Formal_Subt : Entity_Id;
11752 if Has_Discriminants (A_Gen_T) then
11753 if not Has_Discriminants (Act_T) then
11755 ("actual for & must have discriminants", Actual, Gen_T);
11756 Abandon_Instantiation (Actual);
11758 elsif Is_Constrained (Act_T) then
11760 ("actual for & must be unconstrained", Actual, Gen_T);
11761 Abandon_Instantiation (Actual);
11764 Formal_Discr := First_Discriminant (A_Gen_T);
11765 Actual_Discr := First_Discriminant (Act_T);
11766 while Formal_Discr /= Empty loop
11767 if Actual_Discr = Empty then
11769 ("discriminants on actual do not match formal",
11771 Abandon_Instantiation (Actual);
11774 Formal_Subt := Get_Instance_Of (Etype (Formal_Discr));
11776 -- Access discriminants match if designated types do
11778 if Ekind (Base_Type (Formal_Subt)) = E_Anonymous_Access_Type
11779 and then (Ekind (Base_Type (Etype (Actual_Discr)))) =
11780 E_Anonymous_Access_Type
11783 (Designated_Type (Base_Type (Formal_Subt))) =
11784 Designated_Type (Base_Type (Etype (Actual_Discr)))
11788 elsif Base_Type (Formal_Subt) /=
11789 Base_Type (Etype (Actual_Discr))
11792 ("types of actual discriminants must match formal",
11794 Abandon_Instantiation (Actual);
11796 elsif not Subtypes_Statically_Match
11797 (Formal_Subt, Etype (Actual_Discr))
11798 and then Ada_Version >= Ada_95
11801 ("subtypes of actual discriminants must match formal",
11803 Abandon_Instantiation (Actual);
11806 Next_Discriminant (Formal_Discr);
11807 Next_Discriminant (Actual_Discr);
11810 if Actual_Discr /= Empty then
11812 ("discriminants on actual do not match formal",
11814 Abandon_Instantiation (Actual);
11818 end Validate_Discriminated_Formal_Type;
11820 ---------------------------------------
11821 -- Validate_Incomplete_Type_Instance --
11822 ---------------------------------------
11824 procedure Validate_Incomplete_Type_Instance is
11826 if not Is_Tagged_Type (Act_T)
11827 and then Is_Tagged_Type (A_Gen_T)
11830 ("actual for & must be a tagged type", Actual, Gen_T);
11833 Validate_Discriminated_Formal_Type;
11834 end Validate_Incomplete_Type_Instance;
11836 --------------------------------------
11837 -- Validate_Interface_Type_Instance --
11838 --------------------------------------
11840 procedure Validate_Interface_Type_Instance is
11842 if not Is_Interface (Act_T) then
11844 ("actual for formal interface type must be an interface",
11847 elsif Is_Limited_Type (Act_T) /= Is_Limited_Type (A_Gen_T)
11849 Is_Task_Interface (A_Gen_T) /= Is_Task_Interface (Act_T)
11851 Is_Protected_Interface (A_Gen_T) /=
11852 Is_Protected_Interface (Act_T)
11854 Is_Synchronized_Interface (A_Gen_T) /=
11855 Is_Synchronized_Interface (Act_T)
11858 ("actual for interface& does not match (RM 12.5.5(4))",
11861 end Validate_Interface_Type_Instance;
11863 ------------------------------------
11864 -- Validate_Private_Type_Instance --
11865 ------------------------------------
11867 procedure Validate_Private_Type_Instance is
11869 if Is_Limited_Type (Act_T)
11870 and then not Is_Limited_Type (A_Gen_T)
11872 if In_Instance then
11876 ("actual for non-limited & cannot be a limited type", Actual,
11878 Explain_Limited_Type (Act_T, Actual);
11879 Abandon_Instantiation (Actual);
11882 elsif Known_To_Have_Preelab_Init (A_Gen_T)
11883 and then not Has_Preelaborable_Initialization (Act_T)
11886 ("actual for & must have preelaborable initialization", Actual,
11889 elsif Is_Indefinite_Subtype (Act_T)
11890 and then not Is_Indefinite_Subtype (A_Gen_T)
11891 and then Ada_Version >= Ada_95
11894 ("actual for & must be a definite subtype", Actual, Gen_T);
11896 elsif not Is_Tagged_Type (Act_T)
11897 and then Is_Tagged_Type (A_Gen_T)
11900 ("actual for & must be a tagged type", Actual, Gen_T);
11903 Validate_Discriminated_Formal_Type;
11905 end Validate_Private_Type_Instance;
11907 -- Start of processing for Instantiate_Type
11910 if Get_Instance_Of (A_Gen_T) /= A_Gen_T then
11911 Error_Msg_N ("duplicate instantiation of generic type", Actual);
11912 return New_List (Error);
11914 elsif not Is_Entity_Name (Actual)
11915 or else not Is_Type (Entity (Actual))
11918 ("expect valid subtype mark to instantiate &", Actual, Gen_T);
11919 Abandon_Instantiation (Actual);
11922 Act_T := Entity (Actual);
11924 -- Ada 2005 (AI-216): An Unchecked_Union subtype shall only be passed
11925 -- as a generic actual parameter if the corresponding formal type
11926 -- does not have a known_discriminant_part, or is a formal derived
11927 -- type that is an Unchecked_Union type.
11929 if Is_Unchecked_Union (Base_Type (Act_T)) then
11930 if not Has_Discriminants (A_Gen_T)
11932 (Is_Derived_Type (A_Gen_T)
11934 Is_Unchecked_Union (A_Gen_T))
11938 Error_Msg_N ("unchecked union cannot be the actual for a" &
11939 " discriminated formal type", Act_T);
11944 -- Deal with fixed/floating restrictions
11946 if Is_Floating_Point_Type (Act_T) then
11947 Check_Restriction (No_Floating_Point, Actual);
11948 elsif Is_Fixed_Point_Type (Act_T) then
11949 Check_Restriction (No_Fixed_Point, Actual);
11952 -- Deal with error of using incomplete type as generic actual.
11953 -- This includes limited views of a type, even if the non-limited
11954 -- view may be available.
11956 if Ekind (Act_T) = E_Incomplete_Type
11957 or else (Is_Class_Wide_Type (Act_T)
11959 Ekind (Root_Type (Act_T)) = E_Incomplete_Type)
11961 -- If the formal is an incomplete type, the actual can be
11962 -- incomplete as well.
11964 if Ekind (A_Gen_T) = E_Incomplete_Type then
11967 elsif Is_Class_Wide_Type (Act_T)
11968 or else No (Full_View (Act_T))
11970 Error_Msg_N ("premature use of incomplete type", Actual);
11971 Abandon_Instantiation (Actual);
11973 Act_T := Full_View (Act_T);
11974 Set_Entity (Actual, Act_T);
11976 if Has_Private_Component (Act_T) then
11978 ("premature use of type with private component", Actual);
11982 -- Deal with error of premature use of private type as generic actual
11984 elsif Is_Private_Type (Act_T)
11985 and then Is_Private_Type (Base_Type (Act_T))
11986 and then not Is_Generic_Type (Act_T)
11987 and then not Is_Derived_Type (Act_T)
11988 and then No (Full_View (Root_Type (Act_T)))
11990 -- If the formal is an incomplete type, the actual can be
11991 -- private or incomplete as well.
11993 if Ekind (A_Gen_T) = E_Incomplete_Type then
11996 Error_Msg_N ("premature use of private type", Actual);
11999 elsif Has_Private_Component (Act_T) then
12001 ("premature use of type with private component", Actual);
12004 Set_Instance_Of (A_Gen_T, Act_T);
12006 -- If the type is generic, the class-wide type may also be used
12008 if Is_Tagged_Type (A_Gen_T)
12009 and then Is_Tagged_Type (Act_T)
12010 and then not Is_Class_Wide_Type (A_Gen_T)
12012 Set_Instance_Of (Class_Wide_Type (A_Gen_T),
12013 Class_Wide_Type (Act_T));
12016 if not Is_Abstract_Type (A_Gen_T)
12017 and then Is_Abstract_Type (Act_T)
12020 ("actual of non-abstract formal cannot be abstract", Actual);
12023 -- A generic scalar type is a first subtype for which we generate
12024 -- an anonymous base type. Indicate that the instance of this base
12025 -- is the base type of the actual.
12027 if Is_Scalar_Type (A_Gen_T) then
12028 Set_Instance_Of (Etype (A_Gen_T), Etype (Act_T));
12032 if Error_Posted (Act_T) then
12035 case Nkind (Def) is
12036 when N_Formal_Private_Type_Definition =>
12037 Validate_Private_Type_Instance;
12039 when N_Formal_Incomplete_Type_Definition =>
12040 Validate_Incomplete_Type_Instance;
12042 when N_Formal_Derived_Type_Definition =>
12043 Validate_Derived_Type_Instance;
12045 when N_Formal_Discrete_Type_Definition =>
12046 if not Is_Discrete_Type (Act_T) then
12048 ("expect discrete type in instantiation of&",
12050 Abandon_Instantiation (Actual);
12053 Diagnose_Predicated_Actual;
12055 when N_Formal_Signed_Integer_Type_Definition =>
12056 if not Is_Signed_Integer_Type (Act_T) then
12058 ("expect signed integer type in instantiation of&",
12060 Abandon_Instantiation (Actual);
12063 Diagnose_Predicated_Actual;
12065 when N_Formal_Modular_Type_Definition =>
12066 if not Is_Modular_Integer_Type (Act_T) then
12068 ("expect modular type in instantiation of &",
12070 Abandon_Instantiation (Actual);
12073 Diagnose_Predicated_Actual;
12075 when N_Formal_Floating_Point_Definition =>
12076 if not Is_Floating_Point_Type (Act_T) then
12078 ("expect float type in instantiation of &", Actual, Gen_T);
12079 Abandon_Instantiation (Actual);
12082 when N_Formal_Ordinary_Fixed_Point_Definition =>
12083 if not Is_Ordinary_Fixed_Point_Type (Act_T) then
12085 ("expect ordinary fixed point type in instantiation of &",
12087 Abandon_Instantiation (Actual);
12090 when N_Formal_Decimal_Fixed_Point_Definition =>
12091 if not Is_Decimal_Fixed_Point_Type (Act_T) then
12093 ("expect decimal type in instantiation of &",
12095 Abandon_Instantiation (Actual);
12098 when N_Array_Type_Definition =>
12099 Validate_Array_Type_Instance;
12101 when N_Access_To_Object_Definition =>
12102 Validate_Access_Type_Instance;
12104 when N_Access_Function_Definition |
12105 N_Access_Procedure_Definition =>
12106 Validate_Access_Subprogram_Instance;
12108 when N_Record_Definition =>
12109 Validate_Interface_Type_Instance;
12111 when N_Derived_Type_Definition =>
12112 Validate_Derived_Interface_Type_Instance;
12115 raise Program_Error;
12120 Subt := New_Copy (Gen_T);
12122 -- Use adjusted sloc of subtype name as the location for other nodes in
12123 -- the subtype declaration.
12125 Loc := Sloc (Subt);
12128 Make_Subtype_Declaration (Loc,
12129 Defining_Identifier => Subt,
12130 Subtype_Indication => New_Occurrence_Of (Act_T, Loc));
12132 if Is_Private_Type (Act_T) then
12133 Set_Has_Private_View (Subtype_Indication (Decl_Node));
12135 elsif Is_Access_Type (Act_T)
12136 and then Is_Private_Type (Designated_Type (Act_T))
12138 Set_Has_Private_View (Subtype_Indication (Decl_Node));
12141 Decl_Nodes := New_List (Decl_Node);
12143 -- Flag actual derived types so their elaboration produces the
12144 -- appropriate renamings for the primitive operations of the ancestor.
12145 -- Flag actual for formal private types as well, to determine whether
12146 -- operations in the private part may override inherited operations.
12147 -- If the formal has an interface list, the ancestor is not the
12148 -- parent, but the analyzed formal that includes the interface
12149 -- operations of all its progenitors.
12151 -- Same treatment for formal private types, so we can check whether the
12152 -- type is tagged limited when validating derivations in the private
12153 -- part. (See AI05-096).
12155 if Nkind (Def) = N_Formal_Derived_Type_Definition then
12156 if Present (Interface_List (Def)) then
12157 Set_Generic_Parent_Type (Decl_Node, A_Gen_T);
12159 Set_Generic_Parent_Type (Decl_Node, Ancestor);
12162 elsif Nkind_In (Def,
12163 N_Formal_Private_Type_Definition,
12164 N_Formal_Incomplete_Type_Definition)
12166 Set_Generic_Parent_Type (Decl_Node, A_Gen_T);
12169 -- If the actual is a synchronized type that implements an interface,
12170 -- the primitive operations are attached to the corresponding record,
12171 -- and we have to treat it as an additional generic actual, so that its
12172 -- primitive operations become visible in the instance. The task or
12173 -- protected type itself does not carry primitive operations.
12175 if Is_Concurrent_Type (Act_T)
12176 and then Is_Tagged_Type (Act_T)
12177 and then Present (Corresponding_Record_Type (Act_T))
12178 and then Present (Ancestor)
12179 and then Is_Interface (Ancestor)
12182 Corr_Rec : constant Entity_Id :=
12183 Corresponding_Record_Type (Act_T);
12184 New_Corr : Entity_Id;
12185 Corr_Decl : Node_Id;
12188 New_Corr := Make_Temporary (Loc, 'S');
12190 Make_Subtype_Declaration (Loc,
12191 Defining_Identifier => New_Corr,
12192 Subtype_Indication =>
12193 New_Occurrence_Of (Corr_Rec, Loc));
12194 Append_To (Decl_Nodes, Corr_Decl);
12196 if Ekind (Act_T) = E_Task_Type then
12197 Set_Ekind (Subt, E_Task_Subtype);
12199 Set_Ekind (Subt, E_Protected_Subtype);
12202 Set_Corresponding_Record_Type (Subt, Corr_Rec);
12203 Set_Generic_Parent_Type (Corr_Decl, Ancestor);
12204 Set_Generic_Parent_Type (Decl_Node, Empty);
12209 end Instantiate_Type;
12211 ---------------------
12212 -- Is_In_Main_Unit --
12213 ---------------------
12215 function Is_In_Main_Unit (N : Node_Id) return Boolean is
12216 Unum : constant Unit_Number_Type := Get_Source_Unit (N);
12217 Current_Unit : Node_Id;
12220 if Unum = Main_Unit then
12223 -- If the current unit is a subunit then it is either the main unit or
12224 -- is being compiled as part of the main unit.
12226 elsif Nkind (N) = N_Compilation_Unit then
12227 return Nkind (Unit (N)) = N_Subunit;
12230 Current_Unit := Parent (N);
12231 while Present (Current_Unit)
12232 and then Nkind (Current_Unit) /= N_Compilation_Unit
12234 Current_Unit := Parent (Current_Unit);
12237 -- The instantiation node is in the main unit, or else the current node
12238 -- (perhaps as the result of nested instantiations) is in the main unit,
12239 -- or in the declaration of the main unit, which in this last case must
12242 return Unum = Main_Unit
12243 or else Current_Unit = Cunit (Main_Unit)
12244 or else Current_Unit = Library_Unit (Cunit (Main_Unit))
12245 or else (Present (Library_Unit (Current_Unit))
12246 and then Is_In_Main_Unit (Library_Unit (Current_Unit)));
12247 end Is_In_Main_Unit;
12249 ----------------------------
12250 -- Load_Parent_Of_Generic --
12251 ----------------------------
12253 procedure Load_Parent_Of_Generic
12256 Body_Optional : Boolean := False)
12258 Comp_Unit : constant Node_Id := Cunit (Get_Source_Unit (Spec));
12259 Saved_Style_Check : constant Boolean := Style_Check;
12260 Saved_Warnings : constant Warning_Record := Save_Warnings;
12261 True_Parent : Node_Id;
12262 Inst_Node : Node_Id;
12264 Previous_Instances : constant Elist_Id := New_Elmt_List;
12266 procedure Collect_Previous_Instances (Decls : List_Id);
12267 -- Collect all instantiations in the given list of declarations, that
12268 -- precede the generic that we need to load. If the bodies of these
12269 -- instantiations are available, we must analyze them, to ensure that
12270 -- the public symbols generated are the same when the unit is compiled
12271 -- to generate code, and when it is compiled in the context of a unit
12272 -- that needs a particular nested instance. This process is applied to
12273 -- both package and subprogram instances.
12275 --------------------------------
12276 -- Collect_Previous_Instances --
12277 --------------------------------
12279 procedure Collect_Previous_Instances (Decls : List_Id) is
12283 Decl := First (Decls);
12284 while Present (Decl) loop
12285 if Sloc (Decl) >= Sloc (Inst_Node) then
12288 -- If Decl is an instantiation, then record it as requiring
12289 -- instantiation of the corresponding body, except if it is an
12290 -- abbreviated instantiation generated internally for conformance
12291 -- checking purposes only for the case of a formal package
12292 -- declared without a box (see Instantiate_Formal_Package). Such
12293 -- an instantiation does not generate any code (the actual code
12294 -- comes from actual) and thus does not need to be analyzed here.
12295 -- If the instantiation appears with a generic package body it is
12296 -- not analyzed here either.
12298 elsif Nkind (Decl) = N_Package_Instantiation
12299 and then not Is_Internal (Defining_Entity (Decl))
12301 Append_Elmt (Decl, Previous_Instances);
12303 -- For a subprogram instantiation, omit instantiations intrinsic
12304 -- operations (Unchecked_Conversions, etc.) that have no bodies.
12306 elsif Nkind_In (Decl, N_Function_Instantiation,
12307 N_Procedure_Instantiation)
12308 and then not Is_Intrinsic_Subprogram (Entity (Name (Decl)))
12310 Append_Elmt (Decl, Previous_Instances);
12312 elsif Nkind (Decl) = N_Package_Declaration then
12313 Collect_Previous_Instances
12314 (Visible_Declarations (Specification (Decl)));
12315 Collect_Previous_Instances
12316 (Private_Declarations (Specification (Decl)));
12318 -- Previous non-generic bodies may contain instances as well
12320 elsif Nkind (Decl) = N_Package_Body
12321 and then Ekind (Corresponding_Spec (Decl)) /= E_Generic_Package
12323 Collect_Previous_Instances (Declarations (Decl));
12325 elsif Nkind (Decl) = N_Subprogram_Body
12326 and then not Acts_As_Spec (Decl)
12327 and then not Is_Generic_Subprogram (Corresponding_Spec (Decl))
12329 Collect_Previous_Instances (Declarations (Decl));
12334 end Collect_Previous_Instances;
12336 -- Start of processing for Load_Parent_Of_Generic
12339 if not In_Same_Source_Unit (N, Spec)
12340 or else Nkind (Unit (Comp_Unit)) = N_Package_Declaration
12341 or else (Nkind (Unit (Comp_Unit)) = N_Package_Body
12342 and then not Is_In_Main_Unit (Spec))
12344 -- Find body of parent of spec, and analyze it. A special case arises
12345 -- when the parent is an instantiation, that is to say when we are
12346 -- currently instantiating a nested generic. In that case, there is
12347 -- no separate file for the body of the enclosing instance. Instead,
12348 -- the enclosing body must be instantiated as if it were a pending
12349 -- instantiation, in order to produce the body for the nested generic
12350 -- we require now. Note that in that case the generic may be defined
12351 -- in a package body, the instance defined in the same package body,
12352 -- and the original enclosing body may not be in the main unit.
12354 Inst_Node := Empty;
12356 True_Parent := Parent (Spec);
12357 while Present (True_Parent)
12358 and then Nkind (True_Parent) /= N_Compilation_Unit
12360 if Nkind (True_Parent) = N_Package_Declaration
12362 Nkind (Original_Node (True_Parent)) = N_Package_Instantiation
12364 -- Parent is a compilation unit that is an instantiation.
12365 -- Instantiation node has been replaced with package decl.
12367 Inst_Node := Original_Node (True_Parent);
12370 elsif Nkind (True_Parent) = N_Package_Declaration
12371 and then Present (Generic_Parent (Specification (True_Parent)))
12372 and then Nkind (Parent (True_Parent)) /= N_Compilation_Unit
12374 -- Parent is an instantiation within another specification.
12375 -- Declaration for instance has been inserted before original
12376 -- instantiation node. A direct link would be preferable?
12378 Inst_Node := Next (True_Parent);
12379 while Present (Inst_Node)
12380 and then Nkind (Inst_Node) /= N_Package_Instantiation
12385 -- If the instance appears within a generic, and the generic
12386 -- unit is defined within a formal package of the enclosing
12387 -- generic, there is no generic body available, and none
12388 -- needed. A more precise test should be used ???
12390 if No (Inst_Node) then
12397 True_Parent := Parent (True_Parent);
12401 -- Case where we are currently instantiating a nested generic
12403 if Present (Inst_Node) then
12404 if Nkind (Parent (True_Parent)) = N_Compilation_Unit then
12406 -- Instantiation node and declaration of instantiated package
12407 -- were exchanged when only the declaration was needed.
12408 -- Restore instantiation node before proceeding with body.
12410 Set_Unit (Parent (True_Parent), Inst_Node);
12413 -- Now complete instantiation of enclosing body, if it appears in
12414 -- some other unit. If it appears in the current unit, the body
12415 -- will have been instantiated already.
12417 if No (Corresponding_Body (Instance_Spec (Inst_Node))) then
12419 -- We need to determine the expander mode to instantiate the
12420 -- enclosing body. Because the generic body we need may use
12421 -- global entities declared in the enclosing package (including
12422 -- aggregates) it is in general necessary to compile this body
12423 -- with expansion enabled, except if we are within a generic
12424 -- package, in which case the usual generic rule applies.
12427 Exp_Status : Boolean := True;
12431 -- Loop through scopes looking for generic package
12433 Scop := Scope (Defining_Entity (Instance_Spec (Inst_Node)));
12434 while Present (Scop)
12435 and then Scop /= Standard_Standard
12437 if Ekind (Scop) = E_Generic_Package then
12438 Exp_Status := False;
12442 Scop := Scope (Scop);
12445 -- Collect previous instantiations in the unit that contains
12446 -- the desired generic.
12448 if Nkind (Parent (True_Parent)) /= N_Compilation_Unit
12449 and then not Body_Optional
12453 Info : Pending_Body_Info;
12457 Par := Parent (Inst_Node);
12458 while Present (Par) loop
12459 exit when Nkind (Parent (Par)) = N_Compilation_Unit;
12460 Par := Parent (Par);
12463 pragma Assert (Present (Par));
12465 if Nkind (Par) = N_Package_Body then
12466 Collect_Previous_Instances (Declarations (Par));
12468 elsif Nkind (Par) = N_Package_Declaration then
12469 Collect_Previous_Instances
12470 (Visible_Declarations (Specification (Par)));
12471 Collect_Previous_Instances
12472 (Private_Declarations (Specification (Par)));
12475 -- Enclosing unit is a subprogram body. In this
12476 -- case all instance bodies are processed in order
12477 -- and there is no need to collect them separately.
12482 Decl := First_Elmt (Previous_Instances);
12483 while Present (Decl) loop
12485 (Inst_Node => Node (Decl),
12487 Instance_Spec (Node (Decl)),
12488 Expander_Status => Exp_Status,
12489 Current_Sem_Unit =>
12490 Get_Code_Unit (Sloc (Node (Decl))),
12491 Scope_Suppress => Scope_Suppress,
12492 Local_Suppress_Stack_Top =>
12493 Local_Suppress_Stack_Top,
12494 Version => Ada_Version,
12495 Version_Pragma => Ada_Version_Pragma,
12496 Warnings => Save_Warnings,
12497 SPARK_Mode => SPARK_Mode,
12498 SPARK_Mode_Pragma => SPARK_Mode_Pragma);
12500 -- Package instance
12503 Nkind (Node (Decl)) = N_Package_Instantiation
12505 Instantiate_Package_Body
12506 (Info, Body_Optional => True);
12508 -- Subprogram instance
12511 -- The instance_spec is the wrapper package,
12512 -- and the subprogram declaration is the last
12513 -- declaration in the wrapper.
12517 (Visible_Declarations
12518 (Specification (Info.Act_Decl)));
12520 Instantiate_Subprogram_Body
12521 (Info, Body_Optional => True);
12529 Instantiate_Package_Body
12531 ((Inst_Node => Inst_Node,
12532 Act_Decl => True_Parent,
12533 Expander_Status => Exp_Status,
12534 Current_Sem_Unit => Get_Code_Unit
12535 (Sloc (Inst_Node)),
12536 Scope_Suppress => Scope_Suppress,
12537 Local_Suppress_Stack_Top => Local_Suppress_Stack_Top,
12538 Version => Ada_Version,
12539 Version_Pragma => Ada_Version_Pragma,
12540 Warnings => Save_Warnings,
12541 SPARK_Mode => SPARK_Mode,
12542 SPARK_Mode_Pragma => SPARK_Mode_Pragma)),
12543 Body_Optional => Body_Optional);
12547 -- Case where we are not instantiating a nested generic
12550 Opt.Style_Check := False;
12551 Expander_Mode_Save_And_Set (True);
12552 Load_Needed_Body (Comp_Unit, OK);
12553 Opt.Style_Check := Saved_Style_Check;
12554 Restore_Warnings (Saved_Warnings);
12555 Expander_Mode_Restore;
12558 and then Unit_Requires_Body (Defining_Entity (Spec))
12559 and then not Body_Optional
12562 Bname : constant Unit_Name_Type :=
12563 Get_Body_Name (Get_Unit_Name (Unit (Comp_Unit)));
12566 -- In CodePeer mode, the missing body may make the analysis
12567 -- incomplete, but we do not treat it as fatal.
12569 if CodePeer_Mode then
12573 Error_Msg_Unit_1 := Bname;
12574 Error_Msg_N ("this instantiation requires$!", N);
12575 Error_Msg_File_1 :=
12576 Get_File_Name (Bname, Subunit => False);
12577 Error_Msg_N ("\but file{ was not found!", N);
12578 raise Unrecoverable_Error;
12585 -- If loading parent of the generic caused an instantiation circularity,
12586 -- we abandon compilation at this point, because otherwise in some cases
12587 -- we get into trouble with infinite recursions after this point.
12589 if Circularity_Detected then
12590 raise Unrecoverable_Error;
12592 end Load_Parent_Of_Generic;
12594 ---------------------------------
12595 -- Map_Formal_Package_Entities --
12596 ---------------------------------
12598 procedure Map_Formal_Package_Entities (Form : Entity_Id; Act : Entity_Id) is
12603 Set_Instance_Of (Form, Act);
12605 -- Traverse formal and actual package to map the corresponding entities.
12606 -- We skip over internal entities that may be generated during semantic
12607 -- analysis, and find the matching entities by name, given that they
12608 -- must appear in the same order.
12610 E1 := First_Entity (Form);
12611 E2 := First_Entity (Act);
12612 while Present (E1) and then E1 /= First_Private_Entity (Form) loop
12613 -- Could this test be a single condition??? Seems like it could, and
12614 -- isn't FPE (Form) a constant anyway???
12616 if not Is_Internal (E1)
12617 and then Present (Parent (E1))
12618 and then not Is_Class_Wide_Type (E1)
12619 and then not Is_Internal_Name (Chars (E1))
12621 while Present (E2) and then Chars (E2) /= Chars (E1) loop
12628 Set_Instance_Of (E1, E2);
12630 if Is_Type (E1) and then Is_Tagged_Type (E2) then
12631 Set_Instance_Of (Class_Wide_Type (E1), Class_Wide_Type (E2));
12634 if Is_Constrained (E1) then
12635 Set_Instance_Of (Base_Type (E1), Base_Type (E2));
12638 if Ekind (E1) = E_Package and then No (Renamed_Object (E1)) then
12639 Map_Formal_Package_Entities (E1, E2);
12646 end Map_Formal_Package_Entities;
12648 -----------------------
12649 -- Move_Freeze_Nodes --
12650 -----------------------
12652 procedure Move_Freeze_Nodes
12653 (Out_Of : Entity_Id;
12658 Next_Decl : Node_Id;
12659 Next_Node : Node_Id := After;
12662 function Is_Outer_Type (T : Entity_Id) return Boolean;
12663 -- Check whether entity is declared in a scope external to that of the
12666 -------------------
12667 -- Is_Outer_Type --
12668 -------------------
12670 function Is_Outer_Type (T : Entity_Id) return Boolean is
12671 Scop : Entity_Id := Scope (T);
12674 if Scope_Depth (Scop) < Scope_Depth (Out_Of) then
12678 while Scop /= Standard_Standard loop
12679 if Scop = Out_Of then
12682 Scop := Scope (Scop);
12690 -- Start of processing for Move_Freeze_Nodes
12697 -- First remove the freeze nodes that may appear before all other
12701 while Present (Decl)
12702 and then Nkind (Decl) = N_Freeze_Entity
12703 and then Is_Outer_Type (Entity (Decl))
12705 Decl := Remove_Head (L);
12706 Insert_After (Next_Node, Decl);
12707 Set_Analyzed (Decl, False);
12712 -- Next scan the list of declarations and remove each freeze node that
12713 -- appears ahead of the current node.
12715 while Present (Decl) loop
12716 while Present (Next (Decl))
12717 and then Nkind (Next (Decl)) = N_Freeze_Entity
12718 and then Is_Outer_Type (Entity (Next (Decl)))
12720 Next_Decl := Remove_Next (Decl);
12721 Insert_After (Next_Node, Next_Decl);
12722 Set_Analyzed (Next_Decl, False);
12723 Next_Node := Next_Decl;
12726 -- If the declaration is a nested package or concurrent type, then
12727 -- recurse. Nested generic packages will have been processed from the
12730 case Nkind (Decl) is
12731 when N_Package_Declaration =>
12732 Spec := Specification (Decl);
12734 when N_Task_Type_Declaration =>
12735 Spec := Task_Definition (Decl);
12737 when N_Protected_Type_Declaration =>
12738 Spec := Protected_Definition (Decl);
12744 if Present (Spec) then
12745 Move_Freeze_Nodes (Out_Of, Next_Node, Visible_Declarations (Spec));
12746 Move_Freeze_Nodes (Out_Of, Next_Node, Private_Declarations (Spec));
12751 end Move_Freeze_Nodes;
12757 function Next_Assoc (E : Assoc_Ptr) return Assoc_Ptr is
12759 return Generic_Renamings.Table (E).Next_In_HTable;
12762 ------------------------
12763 -- Preanalyze_Actuals --
12764 ------------------------
12766 procedure Preanalyze_Actuals (N : Node_Id) is
12769 Errs : constant Int := Serious_Errors_Detected;
12771 Cur : Entity_Id := Empty;
12772 -- Current homograph of the instance name
12775 -- Saved visibility status of the current homograph
12778 Assoc := First (Generic_Associations (N));
12780 -- If the instance is a child unit, its name may hide an outer homonym,
12781 -- so make it invisible to perform name resolution on the actuals.
12783 if Nkind (Defining_Unit_Name (N)) = N_Defining_Program_Unit_Name
12785 (Current_Entity (Defining_Identifier (Defining_Unit_Name (N))))
12787 Cur := Current_Entity (Defining_Identifier (Defining_Unit_Name (N)));
12789 if Is_Compilation_Unit (Cur) then
12790 Vis := Is_Immediately_Visible (Cur);
12791 Set_Is_Immediately_Visible (Cur, False);
12797 while Present (Assoc) loop
12798 if Nkind (Assoc) /= N_Others_Choice then
12799 Act := Explicit_Generic_Actual_Parameter (Assoc);
12801 -- Within a nested instantiation, a defaulted actual is an empty
12802 -- association, so nothing to analyze. If the subprogram actual
12803 -- is an attribute, analyze prefix only, because actual is not a
12804 -- complete attribute reference.
12806 -- If actual is an allocator, analyze expression only. The full
12807 -- analysis can generate code, and if instance is a compilation
12808 -- unit we have to wait until the package instance is installed
12809 -- to have a proper place to insert this code.
12811 -- String literals may be operators, but at this point we do not
12812 -- know whether the actual is a formal subprogram or a string.
12817 elsif Nkind (Act) = N_Attribute_Reference then
12818 Analyze (Prefix (Act));
12820 elsif Nkind (Act) = N_Explicit_Dereference then
12821 Analyze (Prefix (Act));
12823 elsif Nkind (Act) = N_Allocator then
12825 Expr : constant Node_Id := Expression (Act);
12828 if Nkind (Expr) = N_Subtype_Indication then
12829 Analyze (Subtype_Mark (Expr));
12831 -- Analyze separately each discriminant constraint, when
12832 -- given with a named association.
12838 Constr := First (Constraints (Constraint (Expr)));
12839 while Present (Constr) loop
12840 if Nkind (Constr) = N_Discriminant_Association then
12841 Analyze (Expression (Constr));
12855 elsif Nkind (Act) /= N_Operator_Symbol then
12859 -- Ensure that a ghost subprogram does not act as generic actual
12861 if Is_Entity_Name (Act)
12862 and then Is_Ghost_Subprogram (Entity (Act))
12865 ("ghost subprogram & cannot act as generic actual", Act);
12866 Abandon_Instantiation (Act);
12868 elsif Errs /= Serious_Errors_Detected then
12870 -- Do a minimal analysis of the generic, to prevent spurious
12871 -- warnings complaining about the generic being unreferenced,
12872 -- before abandoning the instantiation.
12874 Analyze (Name (N));
12876 if Is_Entity_Name (Name (N))
12877 and then Etype (Name (N)) /= Any_Type
12879 Generate_Reference (Entity (Name (N)), Name (N));
12880 Set_Is_Instantiated (Entity (Name (N)));
12883 if Present (Cur) then
12885 -- For the case of a child instance hiding an outer homonym,
12886 -- provide additional warning which might explain the error.
12888 Set_Is_Immediately_Visible (Cur, Vis);
12889 Error_Msg_NE ("& hides outer unit with the same name??",
12890 N, Defining_Unit_Name (N));
12893 Abandon_Instantiation (Act);
12900 if Present (Cur) then
12901 Set_Is_Immediately_Visible (Cur, Vis);
12903 end Preanalyze_Actuals;
12905 -------------------
12906 -- Remove_Parent --
12907 -------------------
12909 procedure Remove_Parent (In_Body : Boolean := False) is
12910 S : Entity_Id := Current_Scope;
12911 -- S is the scope containing the instantiation just completed. The scope
12912 -- stack contains the parent instances of the instantiation, followed by
12921 -- After child instantiation is complete, remove from scope stack the
12922 -- extra copy of the current scope, and then remove parent instances.
12924 if not In_Body then
12927 while Current_Scope /= S loop
12928 P := Current_Scope;
12929 End_Package_Scope (Current_Scope);
12931 if In_Open_Scopes (P) then
12932 E := First_Entity (P);
12933 while Present (E) loop
12934 Set_Is_Immediately_Visible (E, True);
12938 -- If instantiation is declared in a block, it is the enclosing
12939 -- scope that might be a parent instance. Note that only one
12940 -- block can be involved, because the parent instances have
12941 -- been installed within it.
12943 if Ekind (P) = E_Block then
12944 Cur_P := Scope (P);
12949 if Is_Generic_Instance (Cur_P) and then P /= Current_Scope then
12950 -- We are within an instance of some sibling. Retain
12951 -- visibility of parent, for proper subsequent cleanup, and
12952 -- reinstall private declarations as well.
12954 Set_In_Private_Part (P);
12955 Install_Private_Declarations (P);
12958 -- If the ultimate parent is a top-level unit recorded in
12959 -- Instance_Parent_Unit, then reset its visibility to what it was
12960 -- before instantiation. (It's not clear what the purpose is of
12961 -- testing whether Scope (P) is In_Open_Scopes, but that test was
12962 -- present before the ultimate parent test was added.???)
12964 elsif not In_Open_Scopes (Scope (P))
12965 or else (P = Instance_Parent_Unit
12966 and then not Parent_Unit_Visible)
12968 Set_Is_Immediately_Visible (P, False);
12970 -- If the current scope is itself an instantiation of a generic
12971 -- nested within P, and we are in the private part of body of this
12972 -- instantiation, restore the full views of P, that were removed
12973 -- in End_Package_Scope above. This obscure case can occur when a
12974 -- subunit of a generic contains an instance of a child unit of
12975 -- its generic parent unit.
12977 elsif S = Current_Scope and then Is_Generic_Instance (S) then
12979 Par : constant Entity_Id :=
12980 Generic_Parent (Package_Specification (S));
12983 and then P = Scope (Par)
12984 and then (In_Package_Body (S) or else In_Private_Part (S))
12986 Set_In_Private_Part (P);
12987 Install_Private_Declarations (P);
12993 -- Reset visibility of entities in the enclosing scope
12995 Set_Is_Hidden_Open_Scope (Current_Scope, False);
12997 Hidden := First_Elmt (Hidden_Entities);
12998 while Present (Hidden) loop
12999 Set_Is_Immediately_Visible (Node (Hidden), True);
13000 Next_Elmt (Hidden);
13004 -- Each body is analyzed separately, and there is no context that
13005 -- needs preserving from one body instance to the next, so remove all
13006 -- parent scopes that have been installed.
13008 while Present (S) loop
13009 End_Package_Scope (S);
13010 Set_Is_Immediately_Visible (S, False);
13011 S := Current_Scope;
13012 exit when S = Standard_Standard;
13021 procedure Restore_Env is
13022 Saved : Instance_Env renames Instance_Envs.Table (Instance_Envs.Last);
13025 if No (Current_Instantiated_Parent.Act_Id) then
13026 -- Restore environment after subprogram inlining
13028 Restore_Private_Views (Empty);
13031 Current_Instantiated_Parent := Saved.Instantiated_Parent;
13032 Exchanged_Views := Saved.Exchanged_Views;
13033 Hidden_Entities := Saved.Hidden_Entities;
13034 Current_Sem_Unit := Saved.Current_Sem_Unit;
13035 Parent_Unit_Visible := Saved.Parent_Unit_Visible;
13036 Instance_Parent_Unit := Saved.Instance_Parent_Unit;
13038 Restore_Opt_Config_Switches (Saved.Switches);
13040 Instance_Envs.Decrement_Last;
13043 ---------------------------
13044 -- Restore_Private_Views --
13045 ---------------------------
13047 procedure Restore_Private_Views
13048 (Pack_Id : Entity_Id;
13049 Is_Package : Boolean := True)
13054 Dep_Elmt : Elmt_Id;
13057 procedure Restore_Nested_Formal (Formal : Entity_Id);
13058 -- Hide the generic formals of formal packages declared with box which
13059 -- were reachable in the current instantiation.
13061 ---------------------------
13062 -- Restore_Nested_Formal --
13063 ---------------------------
13065 procedure Restore_Nested_Formal (Formal : Entity_Id) is
13069 if Present (Renamed_Object (Formal))
13070 and then Denotes_Formal_Package (Renamed_Object (Formal), True)
13074 elsif Present (Associated_Formal_Package (Formal)) then
13075 Ent := First_Entity (Formal);
13076 while Present (Ent) loop
13077 exit when Ekind (Ent) = E_Package
13078 and then Renamed_Entity (Ent) = Renamed_Entity (Formal);
13080 Set_Is_Hidden (Ent);
13081 Set_Is_Potentially_Use_Visible (Ent, False);
13083 -- If package, then recurse
13085 if Ekind (Ent) = E_Package then
13086 Restore_Nested_Formal (Ent);
13092 end Restore_Nested_Formal;
13094 -- Start of processing for Restore_Private_Views
13097 M := First_Elmt (Exchanged_Views);
13098 while Present (M) loop
13101 -- Subtypes of types whose views have been exchanged, and that are
13102 -- defined within the instance, were not on the Private_Dependents
13103 -- list on entry to the instance, so they have to be exchanged
13104 -- explicitly now, in order to remain consistent with the view of the
13107 if Ekind_In (Typ, E_Private_Type,
13108 E_Limited_Private_Type,
13109 E_Record_Type_With_Private)
13111 Dep_Elmt := First_Elmt (Private_Dependents (Typ));
13112 while Present (Dep_Elmt) loop
13113 Dep_Typ := Node (Dep_Elmt);
13115 if Scope (Dep_Typ) = Pack_Id
13116 and then Present (Full_View (Dep_Typ))
13118 Replace_Elmt (Dep_Elmt, Full_View (Dep_Typ));
13119 Exchange_Declarations (Dep_Typ);
13122 Next_Elmt (Dep_Elmt);
13126 Exchange_Declarations (Node (M));
13130 if No (Pack_Id) then
13134 -- Make the generic formal parameters private, and make the formal types
13135 -- into subtypes of the actuals again.
13137 E := First_Entity (Pack_Id);
13138 while Present (E) loop
13139 Set_Is_Hidden (E, True);
13142 and then Nkind (Parent (E)) = N_Subtype_Declaration
13144 -- If the actual for E is itself a generic actual type from
13145 -- an enclosing instance, E is still a generic actual type
13146 -- outside of the current instance. This matter when resolving
13147 -- an overloaded call that may be ambiguous in the enclosing
13148 -- instance, when two of its actuals coincide.
13150 if Is_Entity_Name (Subtype_Indication (Parent (E)))
13151 and then Is_Generic_Actual_Type
13152 (Entity (Subtype_Indication (Parent (E))))
13156 Set_Is_Generic_Actual_Type (E, False);
13159 -- An unusual case of aliasing: the actual may also be directly
13160 -- visible in the generic, and be private there, while it is fully
13161 -- visible in the context of the instance. The internal subtype
13162 -- is private in the instance but has full visibility like its
13163 -- parent in the enclosing scope. This enforces the invariant that
13164 -- the privacy status of all private dependents of a type coincide
13165 -- with that of the parent type. This can only happen when a
13166 -- generic child unit is instantiated within a sibling.
13168 if Is_Private_Type (E)
13169 and then not Is_Private_Type (Etype (E))
13171 Exchange_Declarations (E);
13174 elsif Ekind (E) = E_Package then
13176 -- The end of the renaming list is the renaming of the generic
13177 -- package itself. If the instance is a subprogram, all entities
13178 -- in the corresponding package are renamings. If this entity is
13179 -- a formal package, make its own formals private as well. The
13180 -- actual in this case is itself the renaming of an instantiation.
13181 -- If the entity is not a package renaming, it is the entity
13182 -- created to validate formal package actuals: ignore it.
13184 -- If the actual is itself a formal package for the enclosing
13185 -- generic, or the actual for such a formal package, it remains
13186 -- visible on exit from the instance, and therefore nothing needs
13187 -- to be done either, except to keep it accessible.
13189 if Is_Package and then Renamed_Object (E) = Pack_Id then
13192 elsif Nkind (Parent (E)) /= N_Package_Renaming_Declaration then
13196 Denotes_Formal_Package (Renamed_Object (E), True, Pack_Id)
13198 Set_Is_Hidden (E, False);
13202 Act_P : constant Entity_Id := Renamed_Object (E);
13206 Id := First_Entity (Act_P);
13208 and then Id /= First_Private_Entity (Act_P)
13210 exit when Ekind (Id) = E_Package
13211 and then Renamed_Object (Id) = Act_P;
13213 Set_Is_Hidden (Id, True);
13214 Set_Is_Potentially_Use_Visible (Id, In_Use (Act_P));
13216 if Ekind (Id) = E_Package then
13217 Restore_Nested_Formal (Id);
13228 end Restore_Private_Views;
13235 (Gen_Unit : Entity_Id;
13236 Act_Unit : Entity_Id)
13240 Set_Instance_Env (Gen_Unit, Act_Unit);
13243 ----------------------------
13244 -- Save_Global_References --
13245 ----------------------------
13247 procedure Save_Global_References (N : Node_Id) is
13248 Gen_Scope : Entity_Id;
13252 function Is_Global (E : Entity_Id) return Boolean;
13253 -- Check whether entity is defined outside of generic unit. Examine the
13254 -- scope of an entity, and the scope of the scope, etc, until we find
13255 -- either Standard, in which case the entity is global, or the generic
13256 -- unit itself, which indicates that the entity is local. If the entity
13257 -- is the generic unit itself, as in the case of a recursive call, or
13258 -- the enclosing generic unit, if different from the current scope, then
13259 -- it is local as well, because it will be replaced at the point of
13260 -- instantiation. On the other hand, if it is a reference to a child
13261 -- unit of a common ancestor, which appears in an instantiation, it is
13262 -- global because it is used to denote a specific compilation unit at
13263 -- the time the instantiations will be analyzed.
13265 procedure Reset_Entity (N : Node_Id);
13266 -- Save semantic information on global entity so that it is not resolved
13267 -- again at instantiation time.
13269 procedure Save_Entity_Descendants (N : Node_Id);
13270 -- Apply Save_Global_References to the two syntactic descendants of
13271 -- non-terminal nodes that carry an Associated_Node and are processed
13272 -- through Reset_Entity. Once the global entity (if any) has been
13273 -- captured together with its type, only two syntactic descendants need
13274 -- to be traversed to complete the processing of the tree rooted at N.
13275 -- This applies to Selected_Components, Expanded_Names, and to Operator
13276 -- nodes. N can also be a character literal, identifier, or operator
13277 -- symbol node, but the call has no effect in these cases.
13279 procedure Save_Global_Defaults (N1, N2 : Node_Id);
13280 -- Default actuals in nested instances must be handled specially
13281 -- because there is no link to them from the original tree. When an
13282 -- actual subprogram is given by a default, we add an explicit generic
13283 -- association for it in the instantiation node. When we save the
13284 -- global references on the name of the instance, we recover the list
13285 -- of generic associations, and add an explicit one to the original
13286 -- generic tree, through which a global actual can be preserved.
13287 -- Similarly, if a child unit is instantiated within a sibling, in the
13288 -- context of the parent, we must preserve the identifier of the parent
13289 -- so that it can be properly resolved in a subsequent instantiation.
13291 procedure Save_Global_Descendant (D : Union_Id);
13292 -- Apply Save_Global_References recursively to the descendents of the
13295 procedure Save_References (N : Node_Id);
13296 -- This is the recursive procedure that does the work, once the
13297 -- enclosing generic scope has been established.
13303 function Is_Global (E : Entity_Id) return Boolean is
13306 function Is_Instance_Node (Decl : Node_Id) return Boolean;
13307 -- Determine whether the parent node of a reference to a child unit
13308 -- denotes an instantiation or a formal package, in which case the
13309 -- reference to the child unit is global, even if it appears within
13310 -- the current scope (e.g. when the instance appears within the body
13311 -- of an ancestor).
13313 ----------------------
13314 -- Is_Instance_Node --
13315 ----------------------
13317 function Is_Instance_Node (Decl : Node_Id) return Boolean is
13319 return Nkind (Decl) in N_Generic_Instantiation
13321 Nkind (Original_Node (Decl)) = N_Formal_Package_Declaration;
13322 end Is_Instance_Node;
13324 -- Start of processing for Is_Global
13327 if E = Gen_Scope then
13330 elsif E = Standard_Standard then
13333 elsif Is_Child_Unit (E)
13334 and then (Is_Instance_Node (Parent (N2))
13335 or else (Nkind (Parent (N2)) = N_Expanded_Name
13336 and then N2 = Selector_Name (Parent (N2))
13338 Is_Instance_Node (Parent (Parent (N2)))))
13344 while Se /= Gen_Scope loop
13345 if Se = Standard_Standard then
13360 procedure Reset_Entity (N : Node_Id) is
13362 procedure Set_Global_Type (N : Node_Id; N2 : Node_Id);
13363 -- If the type of N2 is global to the generic unit, save the type in
13364 -- the generic node. Just as we perform name capture for explicit
13365 -- references within the generic, we must capture the global types
13366 -- of local entities because they may participate in resolution in
13369 function Top_Ancestor (E : Entity_Id) return Entity_Id;
13370 -- Find the ultimate ancestor of the current unit. If it is not a
13371 -- generic unit, then the name of the current unit in the prefix of
13372 -- an expanded name must be replaced with its generic homonym to
13373 -- ensure that it will be properly resolved in an instance.
13375 ---------------------
13376 -- Set_Global_Type --
13377 ---------------------
13379 procedure Set_Global_Type (N : Node_Id; N2 : Node_Id) is
13380 Typ : constant Entity_Id := Etype (N2);
13383 Set_Etype (N, Typ);
13385 if Entity (N) /= N2
13386 and then Has_Private_View (Entity (N))
13388 -- If the entity of N is not the associated node, this is a
13389 -- nested generic and it has an associated node as well, whose
13390 -- type is already the full view (see below). Indicate that the
13391 -- original node has a private view.
13393 Set_Has_Private_View (N);
13396 -- If not a private type, nothing else to do
13398 if not Is_Private_Type (Typ) then
13399 if Is_Array_Type (Typ)
13400 and then Is_Private_Type (Component_Type (Typ))
13402 Set_Has_Private_View (N);
13405 -- If it is a derivation of a private type in a context where no
13406 -- full view is needed, nothing to do either.
13408 elsif No (Full_View (Typ)) and then Typ /= Etype (Typ) then
13411 -- Otherwise mark the type for flipping and use the full view when
13415 Set_Has_Private_View (N);
13417 if Present (Full_View (Typ)) then
13418 Set_Etype (N2, Full_View (Typ));
13421 end Set_Global_Type;
13427 function Top_Ancestor (E : Entity_Id) return Entity_Id is
13432 while Is_Child_Unit (Par) loop
13433 Par := Scope (Par);
13439 -- Start of processing for Reset_Entity
13442 N2 := Get_Associated_Node (N);
13445 if Present (E) then
13447 -- If the node is an entry call to an entry in an enclosing task,
13448 -- it is rewritten as a selected component. No global entity to
13449 -- preserve in this case, since the expansion will be redone in
13452 if not Nkind_In (E, N_Defining_Identifier,
13453 N_Defining_Character_Literal,
13454 N_Defining_Operator_Symbol)
13456 Set_Associated_Node (N, Empty);
13457 Set_Etype (N, Empty);
13461 -- If the entity is an itype created as a subtype of an access
13462 -- type with a null exclusion restore source entity for proper
13463 -- visibility. The itype will be created anew in the instance.
13466 and then Ekind (E) = E_Access_Subtype
13467 and then Is_Entity_Name (N)
13468 and then Chars (Etype (E)) = Chars (N)
13471 Set_Entity (N2, E);
13475 if Is_Global (E) then
13477 -- If the entity is a package renaming that is the prefix of
13478 -- an expanded name, it has been rewritten as the renamed
13479 -- package, which is necessary semantically but complicates
13480 -- ASIS tree traversal, so we recover the original entity to
13481 -- expose the renaming. Take into account that the context may
13482 -- be a nested generic, that the original node may itself have
13483 -- an associated node that had better be an entity, and that
13484 -- the current node is still a selected component.
13486 if Ekind (E) = E_Package
13487 and then Nkind (N) = N_Selected_Component
13488 and then Nkind (Parent (N)) = N_Expanded_Name
13489 and then Present (Original_Node (N2))
13490 and then Is_Entity_Name (Original_Node (N2))
13491 and then Present (Entity (Original_Node (N2)))
13493 if Is_Global (Entity (Original_Node (N2))) then
13494 N2 := Original_Node (N2);
13495 Set_Associated_Node (N, N2);
13496 Set_Global_Type (N, N2);
13499 -- Renaming is local, and will be resolved in instance
13501 Set_Associated_Node (N, Empty);
13502 Set_Etype (N, Empty);
13506 Set_Global_Type (N, N2);
13509 elsif Nkind (N) = N_Op_Concat
13510 and then Is_Generic_Type (Etype (N2))
13511 and then (Base_Type (Etype (Right_Opnd (N2))) = Etype (N2)
13513 Base_Type (Etype (Left_Opnd (N2))) = Etype (N2))
13514 and then Is_Intrinsic_Subprogram (E)
13519 -- Entity is local. Mark generic node as unresolved.
13520 -- Note that now it does not have an entity.
13522 Set_Associated_Node (N, Empty);
13523 Set_Etype (N, Empty);
13526 if Nkind (Parent (N)) in N_Generic_Instantiation
13527 and then N = Name (Parent (N))
13529 Save_Global_Defaults (Parent (N), Parent (N2));
13532 elsif Nkind (Parent (N)) = N_Selected_Component
13533 and then Nkind (Parent (N2)) = N_Expanded_Name
13535 if Is_Global (Entity (Parent (N2))) then
13536 Change_Selected_Component_To_Expanded_Name (Parent (N));
13537 Set_Associated_Node (Parent (N), Parent (N2));
13538 Set_Global_Type (Parent (N), Parent (N2));
13539 Save_Entity_Descendants (N);
13541 -- If this is a reference to the current generic entity, replace
13542 -- by the name of the generic homonym of the current package. This
13543 -- is because in an instantiation Par.P.Q will not resolve to the
13544 -- name of the instance, whose enclosing scope is not necessarily
13545 -- Par. We use the generic homonym rather that the name of the
13546 -- generic itself because it may be hidden by a local declaration.
13548 elsif In_Open_Scopes (Entity (Parent (N2)))
13550 Is_Generic_Unit (Top_Ancestor (Entity (Prefix (Parent (N2)))))
13552 if Ekind (Entity (Parent (N2))) = E_Generic_Package then
13553 Rewrite (Parent (N),
13554 Make_Identifier (Sloc (N),
13556 Chars (Generic_Homonym (Entity (Parent (N2))))));
13558 Rewrite (Parent (N),
13559 Make_Identifier (Sloc (N),
13560 Chars => Chars (Selector_Name (Parent (N2)))));
13564 if Nkind (Parent (Parent (N))) in N_Generic_Instantiation
13565 and then Parent (N) = Name (Parent (Parent (N)))
13567 Save_Global_Defaults
13568 (Parent (Parent (N)), Parent (Parent ((N2))));
13571 -- A selected component may denote a static constant that has been
13572 -- folded. If the static constant is global to the generic, capture
13573 -- its value. Otherwise the folding will happen in any instantiation.
13575 elsif Nkind (Parent (N)) = N_Selected_Component
13576 and then Nkind_In (Parent (N2), N_Integer_Literal, N_Real_Literal)
13578 if Present (Entity (Original_Node (Parent (N2))))
13579 and then Is_Global (Entity (Original_Node (Parent (N2))))
13581 Rewrite (Parent (N), New_Copy (Parent (N2)));
13582 Set_Analyzed (Parent (N), False);
13588 -- A selected component may be transformed into a parameterless
13589 -- function call. If the called entity is global, rewrite the node
13590 -- appropriately, i.e. as an extended name for the global entity.
13592 elsif Nkind (Parent (N)) = N_Selected_Component
13593 and then Nkind (Parent (N2)) = N_Function_Call
13594 and then N = Selector_Name (Parent (N))
13596 if No (Parameter_Associations (Parent (N2))) then
13597 if Is_Global (Entity (Name (Parent (N2)))) then
13598 Change_Selected_Component_To_Expanded_Name (Parent (N));
13599 Set_Associated_Node (Parent (N), Name (Parent (N2)));
13600 Set_Global_Type (Parent (N), Name (Parent (N2)));
13601 Save_Entity_Descendants (N);
13604 Set_Is_Prefixed_Call (Parent (N));
13605 Set_Associated_Node (N, Empty);
13606 Set_Etype (N, Empty);
13609 -- In Ada 2005, X.F may be a call to a primitive operation,
13610 -- rewritten as F (X). This rewriting will be done again in an
13611 -- instance, so keep the original node. Global entities will be
13612 -- captured as for other constructs. Indicate that this must
13613 -- resolve as a call, to prevent accidental overloading in the
13614 -- instance, if both a component and a primitive operation appear
13618 Set_Is_Prefixed_Call (Parent (N));
13621 -- Entity is local. Reset in generic unit, so that node is resolved
13622 -- anew at the point of instantiation.
13625 Set_Associated_Node (N, Empty);
13626 Set_Etype (N, Empty);
13630 -----------------------------
13631 -- Save_Entity_Descendants --
13632 -----------------------------
13634 procedure Save_Entity_Descendants (N : Node_Id) is
13637 when N_Binary_Op =>
13638 Save_Global_Descendant (Union_Id (Left_Opnd (N)));
13639 Save_Global_Descendant (Union_Id (Right_Opnd (N)));
13642 Save_Global_Descendant (Union_Id (Right_Opnd (N)));
13644 when N_Expanded_Name | N_Selected_Component =>
13645 Save_Global_Descendant (Union_Id (Prefix (N)));
13646 Save_Global_Descendant (Union_Id (Selector_Name (N)));
13648 when N_Identifier | N_Character_Literal | N_Operator_Symbol =>
13652 raise Program_Error;
13654 end Save_Entity_Descendants;
13656 --------------------------
13657 -- Save_Global_Defaults --
13658 --------------------------
13660 procedure Save_Global_Defaults (N1, N2 : Node_Id) is
13661 Loc : constant Source_Ptr := Sloc (N1);
13662 Assoc2 : constant List_Id := Generic_Associations (N2);
13663 Gen_Id : constant Entity_Id := Get_Generic_Entity (N2);
13670 Actual : Entity_Id;
13673 Assoc1 := Generic_Associations (N1);
13675 if Present (Assoc1) then
13676 Act1 := First (Assoc1);
13679 Set_Generic_Associations (N1, New_List);
13680 Assoc1 := Generic_Associations (N1);
13683 if Present (Assoc2) then
13684 Act2 := First (Assoc2);
13689 while Present (Act1) and then Present (Act2) loop
13694 -- Find the associations added for default subprograms
13696 if Present (Act2) then
13697 while Nkind (Act2) /= N_Generic_Association
13698 or else No (Entity (Selector_Name (Act2)))
13699 or else not Is_Overloadable (Entity (Selector_Name (Act2)))
13704 -- Add a similar association if the default is global. The
13705 -- renaming declaration for the actual has been analyzed, and
13706 -- its alias is the program it renames. Link the actual in the
13707 -- original generic tree with the node in the analyzed tree.
13709 while Present (Act2) loop
13710 Subp := Entity (Selector_Name (Act2));
13711 Def := Explicit_Generic_Actual_Parameter (Act2);
13713 -- Following test is defence against rubbish errors
13715 if No (Alias (Subp)) then
13719 -- Retrieve the resolved actual from the renaming declaration
13720 -- created for the instantiated formal.
13722 Actual := Entity (Name (Parent (Parent (Subp))));
13723 Set_Entity (Def, Actual);
13724 Set_Etype (Def, Etype (Actual));
13726 if Is_Global (Actual) then
13728 Make_Generic_Association (Loc,
13729 Selector_Name => New_Occurrence_Of (Subp, Loc),
13730 Explicit_Generic_Actual_Parameter =>
13731 New_Occurrence_Of (Actual, Loc));
13733 Set_Associated_Node
13734 (Explicit_Generic_Actual_Parameter (Ndec), Def);
13736 Append (Ndec, Assoc1);
13738 -- If there are other defaults, add a dummy association in case
13739 -- there are other defaulted formals with the same name.
13741 elsif Present (Next (Act2)) then
13743 Make_Generic_Association (Loc,
13744 Selector_Name => New_Occurrence_Of (Subp, Loc),
13745 Explicit_Generic_Actual_Parameter => Empty);
13747 Append (Ndec, Assoc1);
13754 if Nkind (Name (N1)) = N_Identifier
13755 and then Is_Child_Unit (Gen_Id)
13756 and then Is_Global (Gen_Id)
13757 and then Is_Generic_Unit (Scope (Gen_Id))
13758 and then In_Open_Scopes (Scope (Gen_Id))
13760 -- This is an instantiation of a child unit within a sibling, so
13761 -- that the generic parent is in scope. An eventual instance must
13762 -- occur within the scope of an instance of the parent. Make name
13763 -- in instance into an expanded name, to preserve the identifier
13764 -- of the parent, so it can be resolved subsequently.
13766 Rewrite (Name (N2),
13767 Make_Expanded_Name (Loc,
13768 Chars => Chars (Gen_Id),
13769 Prefix => New_Occurrence_Of (Scope (Gen_Id), Loc),
13770 Selector_Name => New_Occurrence_Of (Gen_Id, Loc)));
13771 Set_Entity (Name (N2), Gen_Id);
13773 Rewrite (Name (N1),
13774 Make_Expanded_Name (Loc,
13775 Chars => Chars (Gen_Id),
13776 Prefix => New_Occurrence_Of (Scope (Gen_Id), Loc),
13777 Selector_Name => New_Occurrence_Of (Gen_Id, Loc)));
13779 Set_Associated_Node (Name (N1), Name (N2));
13780 Set_Associated_Node (Prefix (Name (N1)), Empty);
13781 Set_Associated_Node
13782 (Selector_Name (Name (N1)), Selector_Name (Name (N2)));
13783 Set_Etype (Name (N1), Etype (Gen_Id));
13786 end Save_Global_Defaults;
13788 ----------------------------
13789 -- Save_Global_Descendant --
13790 ----------------------------
13792 procedure Save_Global_Descendant (D : Union_Id) is
13796 if D in Node_Range then
13797 if D = Union_Id (Empty) then
13800 elsif Nkind (Node_Id (D)) /= N_Compilation_Unit then
13801 Save_References (Node_Id (D));
13804 elsif D in List_Range then
13805 if D = Union_Id (No_List)
13806 or else Is_Empty_List (List_Id (D))
13811 N1 := First (List_Id (D));
13812 while Present (N1) loop
13813 Save_References (N1);
13818 -- Element list or other non-node field, nothing to do
13823 end Save_Global_Descendant;
13825 ---------------------
13826 -- Save_References --
13827 ---------------------
13829 -- This is the recursive procedure that does the work once the enclosing
13830 -- generic scope has been established. We have to treat specially a
13831 -- number of node rewritings that are required by semantic processing
13832 -- and which change the kind of nodes in the generic copy: typically
13833 -- constant-folding, replacing an operator node by a string literal, or
13834 -- a selected component by an expanded name. In each of those cases, the
13835 -- transformation is propagated to the generic unit.
13837 procedure Save_References (N : Node_Id) is
13838 Loc : constant Source_Ptr := Sloc (N);
13844 elsif Nkind_In (N, N_Character_Literal, N_Operator_Symbol) then
13845 if Nkind (N) = Nkind (Get_Associated_Node (N)) then
13848 elsif Nkind (N) = N_Operator_Symbol
13849 and then Nkind (Get_Associated_Node (N)) = N_String_Literal
13851 Change_Operator_Symbol_To_String_Literal (N);
13854 elsif Nkind (N) in N_Op then
13855 if Nkind (N) = Nkind (Get_Associated_Node (N)) then
13856 if Nkind (N) = N_Op_Concat then
13857 Set_Is_Component_Left_Opnd (N,
13858 Is_Component_Left_Opnd (Get_Associated_Node (N)));
13860 Set_Is_Component_Right_Opnd (N,
13861 Is_Component_Right_Opnd (Get_Associated_Node (N)));
13867 -- Node may be transformed into call to a user-defined operator
13869 N2 := Get_Associated_Node (N);
13871 if Nkind (N2) = N_Function_Call then
13872 E := Entity (Name (N2));
13875 and then Is_Global (E)
13877 Set_Etype (N, Etype (N2));
13879 Set_Associated_Node (N, Empty);
13880 Set_Etype (N, Empty);
13883 elsif Nkind_In (N2, N_Integer_Literal,
13887 if Present (Original_Node (N2))
13888 and then Nkind (Original_Node (N2)) = Nkind (N)
13891 -- Operation was constant-folded. Whenever possible,
13892 -- recover semantic information from unfolded node,
13895 Set_Associated_Node (N, Original_Node (N2));
13897 if Nkind (N) = N_Op_Concat then
13898 Set_Is_Component_Left_Opnd (N,
13899 Is_Component_Left_Opnd (Get_Associated_Node (N)));
13900 Set_Is_Component_Right_Opnd (N,
13901 Is_Component_Right_Opnd (Get_Associated_Node (N)));
13907 -- If original node is already modified, propagate
13908 -- constant-folding to template.
13910 Rewrite (N, New_Copy (N2));
13911 Set_Analyzed (N, False);
13914 elsif Nkind (N2) = N_Identifier
13915 and then Ekind (Entity (N2)) = E_Enumeration_Literal
13917 -- Same if call was folded into a literal, but in this case
13918 -- retain the entity to avoid spurious ambiguities if it is
13919 -- overloaded at the point of instantiation or inlining.
13921 Rewrite (N, New_Copy (N2));
13922 Set_Analyzed (N, False);
13926 -- Complete operands check if node has not been constant-folded
13928 if Nkind (N) in N_Op then
13929 Save_Entity_Descendants (N);
13932 elsif Nkind (N) = N_Identifier then
13933 if Nkind (N) = Nkind (Get_Associated_Node (N)) then
13935 -- If this is a discriminant reference, always save it. It is
13936 -- used in the instance to find the corresponding discriminant
13937 -- positionally rather than by name.
13939 Set_Original_Discriminant
13940 (N, Original_Discriminant (Get_Associated_Node (N)));
13944 N2 := Get_Associated_Node (N);
13946 if Nkind (N2) = N_Function_Call then
13947 E := Entity (Name (N2));
13949 -- Name resolves to a call to parameterless function. If
13950 -- original entity is global, mark node as resolved.
13953 and then Is_Global (E)
13955 Set_Etype (N, Etype (N2));
13957 Set_Associated_Node (N, Empty);
13958 Set_Etype (N, Empty);
13961 elsif Nkind_In (N2, N_Integer_Literal, N_Real_Literal)
13962 and then Is_Entity_Name (Original_Node (N2))
13964 -- Name resolves to named number that is constant-folded,
13965 -- We must preserve the original name for ASIS use, and
13966 -- undo the constant-folding, which will be repeated in
13969 Set_Associated_Node (N, Original_Node (N2));
13972 elsif Nkind (N2) = N_String_Literal then
13974 -- Name resolves to string literal. Perform the same
13975 -- replacement in generic.
13977 Rewrite (N, New_Copy (N2));
13979 elsif Nkind (N2) = N_Explicit_Dereference then
13981 -- An identifier is rewritten as a dereference if it is the
13982 -- prefix in an implicit dereference (call or attribute).
13983 -- The analysis of an instantiation will expand the node
13984 -- again, so we preserve the original tree but link it to
13985 -- the resolved entity in case it is global.
13987 if Is_Entity_Name (Prefix (N2))
13988 and then Present (Entity (Prefix (N2)))
13989 and then Is_Global (Entity (Prefix (N2)))
13991 Set_Associated_Node (N, Prefix (N2));
13993 elsif Nkind (Prefix (N2)) = N_Function_Call
13994 and then Is_Global (Entity (Name (Prefix (N2))))
13997 Make_Explicit_Dereference (Loc,
13998 Prefix => Make_Function_Call (Loc,
14000 New_Occurrence_Of (Entity (Name (Prefix (N2))),
14004 Set_Associated_Node (N, Empty);
14005 Set_Etype (N, Empty);
14008 -- The subtype mark of a nominally unconstrained object is
14009 -- rewritten as a subtype indication using the bounds of the
14010 -- expression. Recover the original subtype mark.
14012 elsif Nkind (N2) = N_Subtype_Indication
14013 and then Is_Entity_Name (Original_Node (N2))
14015 Set_Associated_Node (N, Original_Node (N2));
14023 elsif Nkind (N) in N_Entity then
14028 Qual : Node_Id := Empty;
14029 Typ : Entity_Id := Empty;
14032 use Atree.Unchecked_Access;
14033 -- This code section is part of implementing an untyped tree
14034 -- traversal, so it needs direct access to node fields.
14037 if Nkind_In (N, N_Aggregate, N_Extension_Aggregate) then
14038 N2 := Get_Associated_Node (N);
14045 -- In an instance within a generic, use the name of the
14046 -- actual and not the original generic parameter. If the
14047 -- actual is global in the current generic it must be
14048 -- preserved for its instantiation.
14050 if Nkind (Parent (Typ)) = N_Subtype_Declaration
14052 Present (Generic_Parent_Type (Parent (Typ)))
14054 Typ := Base_Type (Typ);
14055 Set_Etype (N2, Typ);
14061 or else not Is_Global (Typ)
14063 Set_Associated_Node (N, Empty);
14065 -- If the aggregate is an actual in a call, it has been
14066 -- resolved in the current context, to some local type.
14067 -- The enclosing call may have been disambiguated by the
14068 -- aggregate, and this disambiguation might fail at
14069 -- instantiation time because the type to which the
14070 -- aggregate did resolve is not preserved. In order to
14071 -- preserve some of this information, we wrap the
14072 -- aggregate in a qualified expression, using the id of
14073 -- its type. For further disambiguation we qualify the
14074 -- type name with its scope (if visible) because both
14075 -- id's will have corresponding entities in an instance.
14076 -- This resolves most of the problems with missing type
14077 -- information on aggregates in instances.
14079 if Nkind (N2) = Nkind (N)
14080 and then Nkind (Parent (N2)) in N_Subprogram_Call
14081 and then Comes_From_Source (Typ)
14083 if Is_Immediately_Visible (Scope (Typ)) then
14084 Nam := Make_Selected_Component (Loc,
14086 Make_Identifier (Loc, Chars (Scope (Typ))),
14088 Make_Identifier (Loc, Chars (Typ)));
14090 Nam := Make_Identifier (Loc, Chars (Typ));
14094 Make_Qualified_Expression (Loc,
14095 Subtype_Mark => Nam,
14096 Expression => Relocate_Node (N));
14100 Save_Global_Descendant (Field1 (N));
14101 Save_Global_Descendant (Field2 (N));
14102 Save_Global_Descendant (Field3 (N));
14103 Save_Global_Descendant (Field5 (N));
14105 if Present (Qual) then
14109 -- All other cases than aggregates
14112 Save_Global_Descendant (Field1 (N));
14113 Save_Global_Descendant (Field2 (N));
14114 Save_Global_Descendant (Field3 (N));
14115 Save_Global_Descendant (Field4 (N));
14116 Save_Global_Descendant (Field5 (N));
14121 -- If a node has aspects, references within their expressions must
14122 -- be saved separately, given they are not directly in the tree.
14124 if Has_Aspects (N) then
14129 Aspect := First (Aspect_Specifications (N));
14130 while Present (Aspect) loop
14131 if Present (Expression (Aspect)) then
14132 Save_Global_References (Expression (Aspect));
14139 end Save_References;
14141 -- Start of processing for Save_Global_References
14144 Gen_Scope := Current_Scope;
14146 -- If the generic unit is a child unit, references to entities in the
14147 -- parent are treated as local, because they will be resolved anew in
14148 -- the context of the instance of the parent.
14150 while Is_Child_Unit (Gen_Scope)
14151 and then Ekind (Scope (Gen_Scope)) = E_Generic_Package
14153 Gen_Scope := Scope (Gen_Scope);
14156 Save_References (N);
14157 end Save_Global_References;
14159 --------------------------------------
14160 -- Set_Copied_Sloc_For_Inlined_Body --
14161 --------------------------------------
14163 procedure Set_Copied_Sloc_For_Inlined_Body (N : Node_Id; E : Entity_Id) is
14165 Create_Instantiation_Source (N, E, True, S_Adjustment);
14166 end Set_Copied_Sloc_For_Inlined_Body;
14168 ---------------------
14169 -- Set_Instance_Of --
14170 ---------------------
14172 procedure Set_Instance_Of (A : Entity_Id; B : Entity_Id) is
14174 Generic_Renamings.Table (Generic_Renamings.Last) := (A, B, Assoc_Null);
14175 Generic_Renamings_HTable.Set (Generic_Renamings.Last);
14176 Generic_Renamings.Increment_Last;
14177 end Set_Instance_Of;
14179 --------------------
14180 -- Set_Next_Assoc --
14181 --------------------
14183 procedure Set_Next_Assoc (E : Assoc_Ptr; Next : Assoc_Ptr) is
14185 Generic_Renamings.Table (E).Next_In_HTable := Next;
14186 end Set_Next_Assoc;
14188 -------------------
14189 -- Start_Generic --
14190 -------------------
14192 procedure Start_Generic is
14194 -- ??? More things could be factored out in this routine.
14195 -- Should probably be done at a later stage.
14197 Generic_Flags.Append (Inside_A_Generic);
14198 Inside_A_Generic := True;
14200 Expander_Mode_Save_And_Set (False);
14203 ----------------------
14204 -- Set_Instance_Env --
14205 ----------------------
14207 procedure Set_Instance_Env
14208 (Gen_Unit : Entity_Id;
14209 Act_Unit : Entity_Id)
14211 Assertion_Status : constant Boolean := Assertions_Enabled;
14212 Save_SPARK_Mode : constant SPARK_Mode_Type := SPARK_Mode;
14213 Save_SPARK_Mode_Pragma : constant Node_Id := SPARK_Mode_Pragma;
14216 -- Regardless of the current mode, predefined units are analyzed in the
14217 -- most current Ada mode, and earlier version Ada checks do not apply
14218 -- to predefined units. Nothing needs to be done for non-internal units.
14219 -- These are always analyzed in the current mode.
14221 if Is_Internal_File_Name
14222 (Fname => Unit_File_Name (Get_Source_Unit (Gen_Unit)),
14223 Renamings_Included => True)
14225 Set_Opt_Config_Switches (True, Current_Sem_Unit = Main_Unit);
14227 -- In Ada2012 we may want to enable assertions in an instance of a
14228 -- predefined unit, in which case we need to preserve the current
14229 -- setting for the Assertions_Enabled flag. This will become more
14230 -- critical when pre/postconditions are added to predefined units,
14231 -- as is already the case for some numeric libraries.
14233 if Ada_Version >= Ada_2012 then
14234 Assertions_Enabled := Assertion_Status;
14237 -- SPARK_Mode for an instance is the one applicable at the point of
14240 SPARK_Mode := Save_SPARK_Mode;
14241 SPARK_Mode_Pragma := Save_SPARK_Mode_Pragma;
14244 Current_Instantiated_Parent :=
14245 (Gen_Id => Gen_Unit,
14246 Act_Id => Act_Unit,
14247 Next_In_HTable => Assoc_Null);
14248 end Set_Instance_Env;
14254 procedure Switch_View (T : Entity_Id) is
14255 BT : constant Entity_Id := Base_Type (T);
14256 Priv_Elmt : Elmt_Id := No_Elmt;
14257 Priv_Sub : Entity_Id;
14260 -- T may be private but its base type may have been exchanged through
14261 -- some other occurrence, in which case there is nothing to switch
14262 -- besides T itself. Note that a private dependent subtype of a private
14263 -- type might not have been switched even if the base type has been,
14264 -- because of the last branch of Check_Private_View (see comment there).
14266 if not Is_Private_Type (BT) then
14267 Prepend_Elmt (Full_View (T), Exchanged_Views);
14268 Exchange_Declarations (T);
14272 Priv_Elmt := First_Elmt (Private_Dependents (BT));
14274 if Present (Full_View (BT)) then
14275 Prepend_Elmt (Full_View (BT), Exchanged_Views);
14276 Exchange_Declarations (BT);
14279 while Present (Priv_Elmt) loop
14280 Priv_Sub := (Node (Priv_Elmt));
14282 -- We avoid flipping the subtype if the Etype of its full view is
14283 -- private because this would result in a malformed subtype. This
14284 -- occurs when the Etype of the subtype full view is the full view of
14285 -- the base type (and since the base types were just switched, the
14286 -- subtype is pointing to the wrong view). This is currently the case
14287 -- for tagged record types, access types (maybe more?) and needs to
14288 -- be resolved. ???
14290 if Present (Full_View (Priv_Sub))
14291 and then not Is_Private_Type (Etype (Full_View (Priv_Sub)))
14293 Prepend_Elmt (Full_View (Priv_Sub), Exchanged_Views);
14294 Exchange_Declarations (Priv_Sub);
14297 Next_Elmt (Priv_Elmt);
14305 function True_Parent (N : Node_Id) return Node_Id is
14307 if Nkind (Parent (N)) = N_Subunit then
14308 return Parent (Corresponding_Stub (Parent (N)));
14314 -----------------------------
14315 -- Valid_Default_Attribute --
14316 -----------------------------
14318 procedure Valid_Default_Attribute (Nam : Entity_Id; Def : Node_Id) is
14319 Attr_Id : constant Attribute_Id :=
14320 Get_Attribute_Id (Attribute_Name (Def));
14321 T : constant Entity_Id := Entity (Prefix (Def));
14322 Is_Fun : constant Boolean := (Ekind (Nam) = E_Function);
14335 F := First_Formal (Nam);
14336 while Present (F) loop
14337 Num_F := Num_F + 1;
14342 when Attribute_Adjacent | Attribute_Ceiling | Attribute_Copy_Sign |
14343 Attribute_Floor | Attribute_Fraction | Attribute_Machine |
14344 Attribute_Model | Attribute_Remainder | Attribute_Rounding |
14345 Attribute_Unbiased_Rounding =>
14348 and then Is_Floating_Point_Type (T);
14350 when Attribute_Image | Attribute_Pred | Attribute_Succ |
14351 Attribute_Value | Attribute_Wide_Image |
14352 Attribute_Wide_Value =>
14353 OK := (Is_Fun and then Num_F = 1 and then Is_Scalar_Type (T));
14355 when Attribute_Max | Attribute_Min =>
14356 OK := (Is_Fun and then Num_F = 2 and then Is_Scalar_Type (T));
14358 when Attribute_Input =>
14359 OK := (Is_Fun and then Num_F = 1);
14361 when Attribute_Output | Attribute_Read | Attribute_Write =>
14362 OK := (not Is_Fun and then Num_F = 2);
14369 Error_Msg_N ("attribute reference has wrong profile for subprogram",
14372 end Valid_Default_Attribute;