1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
9 -- Copyright (C) 1992-2009, 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 Atree; use Atree;
27 with Einfo; use Einfo;
28 with Elists; use Elists;
29 with Errout; use Errout;
30 with Expander; use Expander;
31 with Fname; use Fname;
32 with Fname.UF; use Fname.UF;
33 with Freeze; use Freeze;
35 with Itypes; use Itypes;
37 with Lib.Load; use Lib.Load;
38 with Lib.Xref; use Lib.Xref;
39 with Nlists; use Nlists;
40 with Namet; use Namet;
41 with Nmake; use Nmake;
43 with Rident; use Rident;
44 with Restrict; use Restrict;
45 with Rtsfind; use Rtsfind;
47 with Sem_Aux; use Sem_Aux;
48 with Sem_Cat; use Sem_Cat;
49 with Sem_Ch3; use Sem_Ch3;
50 with Sem_Ch6; use Sem_Ch6;
51 with Sem_Ch7; use Sem_Ch7;
52 with Sem_Ch8; use Sem_Ch8;
53 with Sem_Ch10; use Sem_Ch10;
54 with Sem_Ch13; use Sem_Ch13;
55 with Sem_Disp; use Sem_Disp;
56 with Sem_Elab; use Sem_Elab;
57 with Sem_Elim; use Sem_Elim;
58 with Sem_Eval; use Sem_Eval;
59 with Sem_Res; use Sem_Res;
60 with Sem_Type; use Sem_Type;
61 with Sem_Util; use Sem_Util;
62 with Sem_Warn; use Sem_Warn;
63 with Stand; use Stand;
64 with Sinfo; use Sinfo;
65 with Sinfo.CN; use Sinfo.CN;
66 with Sinput; use Sinput;
67 with Sinput.L; use Sinput.L;
68 with Snames; use Snames;
69 with Stringt; use Stringt;
70 with Uname; use Uname;
72 with Tbuild; use Tbuild;
73 with Uintp; use Uintp;
74 with Urealp; use Urealp;
78 package body Sem_Ch12 is
80 ----------------------------------------------------------
81 -- Implementation of Generic Analysis and Instantiation --
82 ----------------------------------------------------------
84 -- GNAT implements generics by macro expansion. No attempt is made to share
85 -- generic instantiations (for now). Analysis of a generic definition does
86 -- not perform any expansion action, but the expander must be called on the
87 -- tree for each instantiation, because the expansion may of course depend
88 -- on the generic actuals. All of this is best achieved as follows:
90 -- a) Semantic analysis of a generic unit is performed on a copy of the
91 -- tree for the generic unit. All tree modifications that follow analysis
92 -- do not affect the original tree. Links are kept between the original
93 -- tree and the copy, in order to recognize non-local references within
94 -- the generic, and propagate them to each instance (recall that name
95 -- resolution is done on the generic declaration: generics are not really
96 -- macros!). This is summarized in the following diagram:
98 -- .-----------. .----------.
99 -- | semantic |<--------------| generic |
101 -- | |==============>| |
102 -- |___________| global |__________|
113 -- b) Each instantiation copies the original tree, and inserts into it a
114 -- series of declarations that describe the mapping between generic formals
115 -- and actuals. For example, a generic In OUT parameter is an object
116 -- renaming of the corresponding actual, etc. Generic IN parameters are
117 -- constant declarations.
119 -- c) In order to give the right visibility for these renamings, we use
120 -- a different scheme for package and subprogram instantiations. For
121 -- packages, the list of renamings is inserted into the package
122 -- specification, before the visible declarations of the package. The
123 -- renamings are analyzed before any of the text of the instance, and are
124 -- thus visible at the right place. Furthermore, outside of the instance,
125 -- the generic parameters are visible and denote their corresponding
128 -- For subprograms, we create a container package to hold the renamings
129 -- and the subprogram instance itself. Analysis of the package makes the
130 -- renaming declarations visible to the subprogram. After analyzing the
131 -- package, the defining entity for the subprogram is touched-up so that
132 -- it appears declared in the current scope, and not inside the container
135 -- If the instantiation is a compilation unit, the container package is
136 -- given the same name as the subprogram instance. This ensures that
137 -- the elaboration procedure called by the binder, using the compilation
138 -- unit name, calls in fact the elaboration procedure for the package.
140 -- Not surprisingly, private types complicate this approach. By saving in
141 -- the original generic object the non-local references, we guarantee that
142 -- the proper entities are referenced at the point of instantiation.
143 -- However, for private types, this by itself does not insure that the
144 -- proper VIEW of the entity is used (the full type may be visible at the
145 -- point of generic definition, but not at instantiation, or vice-versa).
146 -- In order to reference the proper view, we special-case any reference
147 -- to private types in the generic object, by saving both views, one in
148 -- the generic and one in the semantic copy. At time of instantiation, we
149 -- check whether the two views are consistent, and exchange declarations if
150 -- necessary, in order to restore the correct visibility. Similarly, if
151 -- the instance view is private when the generic view was not, we perform
152 -- the exchange. After completing the instantiation, we restore the
153 -- current visibility. The flag Has_Private_View marks identifiers in the
154 -- the generic unit that require checking.
156 -- Visibility within nested generic units requires special handling.
157 -- Consider the following scheme:
159 -- type Global is ... -- outside of generic unit.
163 -- type Semi_Global is ... -- global to inner.
166 -- procedure inner (X1 : Global; X2 : Semi_Global);
168 -- procedure in2 is new inner (...); -- 4
171 -- package New_Outer is new Outer (...); -- 2
172 -- procedure New_Inner is new New_Outer.Inner (...); -- 3
174 -- The semantic analysis of Outer captures all occurrences of Global.
175 -- The semantic analysis of Inner (at 1) captures both occurrences of
176 -- Global and Semi_Global.
178 -- At point 2 (instantiation of Outer), we also produce a generic copy
179 -- of Inner, even though Inner is, at that point, not being instantiated.
180 -- (This is just part of the semantic analysis of New_Outer).
182 -- Critically, references to Global within Inner must be preserved, while
183 -- references to Semi_Global should not preserved, because they must now
184 -- resolve to an entity within New_Outer. To distinguish between these, we
185 -- use a global variable, Current_Instantiated_Parent, which is set when
186 -- performing a generic copy during instantiation (at 2). This variable is
187 -- used when performing a generic copy that is not an instantiation, but
188 -- that is nested within one, as the occurrence of 1 within 2. The analysis
189 -- of a nested generic only preserves references that are global to the
190 -- enclosing Current_Instantiated_Parent. We use the Scope_Depth value to
191 -- determine whether a reference is external to the given parent.
193 -- The instantiation at point 3 requires no special treatment. The method
194 -- works as well for further nestings of generic units, but of course the
195 -- variable Current_Instantiated_Parent must be stacked because nested
196 -- instantiations can occur, e.g. the occurrence of 4 within 2.
198 -- The instantiation of package and subprogram bodies is handled in a
199 -- similar manner, except that it is delayed until after semantic
200 -- analysis is complete. In this fashion complex cross-dependencies
201 -- between several package declarations and bodies containing generics
202 -- can be compiled which otherwise would diagnose spurious circularities.
204 -- For example, it is possible to compile two packages A and B that
205 -- have the following structure:
207 -- package A is package B is
208 -- generic ... generic ...
209 -- package G_A is package G_B is
212 -- package body A is package body B is
213 -- package N_B is new G_B (..) package N_A is new G_A (..)
215 -- The table Pending_Instantiations in package Inline is used to keep
216 -- track of body instantiations that are delayed in this manner. Inline
217 -- handles the actual calls to do the body instantiations. This activity
218 -- is part of Inline, since the processing occurs at the same point, and
219 -- for essentially the same reason, as the handling of inlined routines.
221 ----------------------------------------------
222 -- Detection of Instantiation Circularities --
223 ----------------------------------------------
225 -- If we have a chain of instantiations that is circular, this is static
226 -- error which must be detected at compile time. The detection of these
227 -- circularities is carried out at the point that we insert a generic
228 -- instance spec or body. If there is a circularity, then the analysis of
229 -- the offending spec or body will eventually result in trying to load the
230 -- same unit again, and we detect this problem as we analyze the package
231 -- instantiation for the second time.
233 -- At least in some cases after we have detected the circularity, we get
234 -- into trouble if we try to keep going. The following flag is set if a
235 -- circularity is detected, and used to abandon compilation after the
236 -- messages have been posted.
238 Circularity_Detected : Boolean := False;
239 -- This should really be reset on encountering a new main unit, but in
240 -- practice we are not using multiple main units so it is not critical.
242 -------------------------------------------------
243 -- Formal packages and partial parametrization --
244 -------------------------------------------------
246 -- When compiling a generic, a formal package is a local instantiation. If
247 -- declared with a box, its generic formals are visible in the enclosing
248 -- generic. If declared with a partial list of actuals, those actuals that
249 -- are defaulted (covered by an Others clause, or given an explicit box
250 -- initialization) are also visible in the enclosing generic, while those
251 -- that have a corresponding actual are not.
253 -- In our source model of instantiation, the same visibility must be
254 -- present in the spec and body of an instance: the names of the formals
255 -- that are defaulted must be made visible within the instance, and made
256 -- invisible (hidden) after the instantiation is complete, so that they
257 -- are not accessible outside of the instance.
259 -- In a generic, a formal package is treated like a special instantiation.
260 -- Our Ada95 compiler handled formals with and without box in different
261 -- ways. With partial parametrization, we use a single model for both.
262 -- We create a package declaration that consists of the specification of
263 -- the generic package, and a set of declarations that map the actuals
264 -- into local renamings, just as we do for bona fide instantiations. For
265 -- defaulted parameters and formals with a box, we copy directly the
266 -- declarations of the formal into this local package. The result is a
267 -- a package whose visible declarations may include generic formals. This
268 -- package is only used for type checking and visibility analysis, and
269 -- never reaches the back-end, so it can freely violate the placement
270 -- rules for generic formal declarations.
272 -- The list of declarations (renamings and copies of formals) is built
273 -- by Analyze_Associations, just as for regular instantiations.
275 -- At the point of instantiation, conformance checking must be applied only
276 -- to those parameters that were specified in the formal. We perform this
277 -- checking by creating another internal instantiation, this one including
278 -- only the renamings and the formals (the rest of the package spec is not
279 -- relevant to conformance checking). We can then traverse two lists: the
280 -- list of actuals in the instance that corresponds to the formal package,
281 -- and the list of actuals produced for this bogus instantiation. We apply
282 -- the conformance rules to those actuals that are not defaulted (i.e.
283 -- which still appear as generic formals.
285 -- When we compile an instance body we must make the right parameters
286 -- visible again. The predicate Is_Generic_Formal indicates which of the
287 -- formals should have its Is_Hidden flag reset.
289 -----------------------
290 -- Local subprograms --
291 -----------------------
293 procedure Abandon_Instantiation (N : Node_Id);
294 pragma No_Return (Abandon_Instantiation);
295 -- Posts an error message "instantiation abandoned" at the indicated node
296 -- and then raises the exception Instantiation_Error to do it.
298 procedure Analyze_Formal_Array_Type
299 (T : in out Entity_Id;
301 -- A formal array type is treated like an array type declaration, and
302 -- invokes Array_Type_Declaration (sem_ch3) whose first parameter is
303 -- in-out, because in the case of an anonymous type the entity is
304 -- actually created in the procedure.
306 -- The following procedures treat other kinds of formal parameters
308 procedure Analyze_Formal_Derived_Interface_Type
313 procedure Analyze_Formal_Derived_Type
318 procedure Analyze_Formal_Interface_Type
323 -- The following subprograms create abbreviated declarations for formal
324 -- scalar types. We introduce an anonymous base of the proper class for
325 -- each of them, and define the formals as constrained first subtypes of
326 -- their bases. The bounds are expressions that are non-static in the
329 procedure Analyze_Formal_Decimal_Fixed_Point_Type
330 (T : Entity_Id; Def : Node_Id);
331 procedure Analyze_Formal_Discrete_Type (T : Entity_Id; Def : Node_Id);
332 procedure Analyze_Formal_Floating_Type (T : Entity_Id; Def : Node_Id);
333 procedure Analyze_Formal_Signed_Integer_Type (T : Entity_Id; Def : Node_Id);
334 procedure Analyze_Formal_Modular_Type (T : Entity_Id; Def : Node_Id);
335 procedure Analyze_Formal_Ordinary_Fixed_Point_Type
336 (T : Entity_Id; Def : Node_Id);
338 procedure Analyze_Formal_Private_Type
342 -- Creates a new private type, which does not require completion
344 procedure Analyze_Generic_Formal_Part (N : Node_Id);
346 procedure Analyze_Generic_Access_Type (T : Entity_Id; Def : Node_Id);
347 -- Create a new access type with the given designated type
349 function Analyze_Associations
352 F_Copy : List_Id) return List_Id;
353 -- At instantiation time, build the list of associations between formals
354 -- and actuals. Each association becomes a renaming declaration for the
355 -- formal entity. F_Copy is the analyzed list of formals in the generic
356 -- copy. It is used to apply legality checks to the actuals. I_Node is the
357 -- instantiation node itself.
359 procedure Analyze_Subprogram_Instantiation
363 procedure Build_Instance_Compilation_Unit_Nodes
367 -- This procedure is used in the case where the generic instance of a
368 -- subprogram body or package body is a library unit. In this case, the
369 -- original library unit node for the generic instantiation must be
370 -- replaced by the resulting generic body, and a link made to a new
371 -- compilation unit node for the generic declaration. The argument N is
372 -- the original generic instantiation. Act_Body and Act_Decl are the body
373 -- and declaration of the instance (either package body and declaration
374 -- nodes or subprogram body and declaration nodes depending on the case).
375 -- On return, the node N has been rewritten with the actual body.
377 procedure Check_Access_Definition (N : Node_Id);
378 -- Subsidiary routine to null exclusion processing. Perform an assertion
379 -- check on Ada version and the presence of an access definition in N.
381 procedure Check_Formal_Packages (P_Id : Entity_Id);
382 -- Apply the following to all formal packages in generic associations
384 procedure Check_Formal_Package_Instance
385 (Formal_Pack : Entity_Id;
386 Actual_Pack : Entity_Id);
387 -- Verify that the actuals of the actual instance match the actuals of
388 -- the template for a formal package that is not declared with a box.
390 procedure Check_Forward_Instantiation (Decl : Node_Id);
391 -- If the generic is a local entity and the corresponding body has not
392 -- been seen yet, flag enclosing packages to indicate that it will be
393 -- elaborated after the generic body. Subprograms declared in the same
394 -- package cannot be inlined by the front-end because front-end inlining
395 -- requires a strict linear order of elaboration.
397 procedure Check_Hidden_Child_Unit
399 Gen_Unit : Entity_Id;
400 Act_Decl_Id : Entity_Id);
401 -- If the generic unit is an implicit child instance within a parent
402 -- instance, we need to make an explicit test that it is not hidden by
403 -- a child instance of the same name and parent.
405 procedure Check_Generic_Actuals
406 (Instance : Entity_Id;
407 Is_Formal_Box : Boolean);
408 -- Similar to previous one. Check the actuals in the instantiation,
409 -- whose views can change between the point of instantiation and the point
410 -- of instantiation of the body. In addition, mark the generic renamings
411 -- as generic actuals, so that they are not compatible with other actuals.
412 -- Recurse on an actual that is a formal package whose declaration has
415 function Contains_Instance_Of
418 N : Node_Id) return Boolean;
419 -- Inner is instantiated within the generic Outer. Check whether Inner
420 -- directly or indirectly contains an instance of Outer or of one of its
421 -- parents, in the case of a subunit. Each generic unit holds a list of
422 -- the entities instantiated within (at any depth). This procedure
423 -- determines whether the set of such lists contains a cycle, i.e. an
424 -- illegal circular instantiation.
426 function Denotes_Formal_Package
428 On_Exit : Boolean := False;
429 Instance : Entity_Id := Empty) return Boolean;
430 -- Returns True if E is a formal package of an enclosing generic, or
431 -- the actual for such a formal in an enclosing instantiation. If such
432 -- a package is used as a formal in an nested generic, or as an actual
433 -- in a nested instantiation, the visibility of ITS formals should not
434 -- be modified. When called from within Restore_Private_Views, the flag
435 -- On_Exit is true, to indicate that the search for a possible enclosing
436 -- instance should ignore the current one. In that case Instance denotes
437 -- the declaration for which this is an actual. This declaration may be
438 -- an instantiation in the source, or the internal instantiation that
439 -- corresponds to the actual for a formal package.
441 function Find_Actual_Type
443 Gen_Type : Entity_Id) return Entity_Id;
444 -- When validating the actual types of a child instance, check whether
445 -- the formal is a formal type of the parent unit, and retrieve the current
446 -- actual for it. Typ is the entity in the analyzed formal type declaration
447 -- (component or index type of an array type, or designated type of an
448 -- access formal) and Gen_Type is the enclosing analyzed formal array
449 -- or access type. The desired actual may be a formal of a parent, or may
450 -- be declared in a formal package of a parent. In both cases it is a
451 -- generic actual type because it appears within a visible instance.
452 -- Finally, it may be declared in a parent unit without being a formal
453 -- of that unit, in which case it must be retrieved by visibility.
454 -- Ambiguities may still arise if two homonyms are declared in two formal
455 -- packages, and the prefix of the formal type may be needed to resolve
456 -- the ambiguity in the instance ???
458 function In_Same_Declarative_Part
460 Inst : Node_Id) return Boolean;
461 -- True if the instantiation Inst and the given freeze_node F_Node appear
462 -- within the same declarative part, ignoring subunits, but with no inter-
463 -- vening subprograms or concurrent units. If true, the freeze node
464 -- of the instance can be placed after the freeze node of the parent,
465 -- which it itself an instance.
467 function In_Main_Context (E : Entity_Id) return Boolean;
468 -- Check whether an instantiation is in the context of the main unit.
469 -- Used to determine whether its body should be elaborated to allow
470 -- front-end inlining.
472 function Is_Generic_Formal (E : Entity_Id) return Boolean;
473 -- Utility to determine whether a given entity is declared by means of
474 -- of a formal parameter declaration. Used to set properly the visibility
475 -- of generic formals of a generic package declared with a box or with
476 -- partial parametrization.
478 procedure Mark_Context (Inst_Decl : Node_Id; Gen_Decl : Node_Id);
479 -- If the generic unit comes from a different unit, indicate that the
480 -- unit that contains the instance depends on the body that contains
481 -- the generic body. Used to determine a more precise dependency graph
482 -- for use by CodePeer.
484 procedure Set_Instance_Env
485 (Gen_Unit : Entity_Id;
486 Act_Unit : Entity_Id);
487 -- Save current instance on saved environment, to be used to determine
488 -- the global status of entities in nested instances. Part of Save_Env.
489 -- called after verifying that the generic unit is legal for the instance,
490 -- The procedure also examines whether the generic unit is a predefined
491 -- unit, in order to set configuration switches accordingly. As a result
492 -- the procedure must be called after analyzing and freezing the actuals.
494 procedure Set_Instance_Of (A : Entity_Id; B : Entity_Id);
495 -- Associate analyzed generic parameter with corresponding
496 -- instance. Used for semantic checks at instantiation time.
498 function Has_Been_Exchanged (E : Entity_Id) return Boolean;
499 -- Traverse the Exchanged_Views list to see if a type was private
500 -- and has already been flipped during this phase of instantiation.
502 procedure Hide_Current_Scope;
503 -- When instantiating a generic child unit, the parent context must be
504 -- present, but the instance and all entities that may be generated
505 -- must be inserted in the current scope. We leave the current scope
506 -- on the stack, but make its entities invisible to avoid visibility
507 -- problems. This is reversed at the end of the instantiation. This is
508 -- not done for the instantiation of the bodies, which only require the
509 -- instances of the generic parents to be in scope.
511 procedure Install_Body
516 -- If the instantiation happens textually before the body of the generic,
517 -- the instantiation of the body must be analyzed after the generic body,
518 -- and not at the point of instantiation. Such early instantiations can
519 -- happen if the generic and the instance appear in a package declaration
520 -- because the generic body can only appear in the corresponding package
521 -- body. Early instantiations can also appear if generic, instance and
522 -- body are all in the declarative part of a subprogram or entry. Entities
523 -- of packages that are early instantiations are delayed, and their freeze
524 -- node appears after the generic body.
526 procedure Insert_After_Last_Decl (N : Node_Id; F_Node : Node_Id);
527 -- Insert freeze node at the end of the declarative part that includes the
528 -- instance node N. If N is in the visible part of an enclosing package
529 -- declaration, the freeze node has to be inserted at the end of the
530 -- private declarations, if any.
532 procedure Freeze_Subprogram_Body
533 (Inst_Node : Node_Id;
535 Pack_Id : Entity_Id);
536 -- The generic body may appear textually after the instance, including
537 -- in the proper body of a stub, or within a different package instance.
538 -- Given that the instance can only be elaborated after the generic, we
539 -- place freeze_nodes for the instance and/or for packages that may enclose
540 -- the instance and the generic, so that the back-end can establish the
541 -- proper order of elaboration.
544 -- Establish environment for subsequent instantiation. Separated from
545 -- Save_Env because data-structures for visibility handling must be
546 -- initialized before call to Check_Generic_Child_Unit.
548 procedure Install_Formal_Packages (Par : Entity_Id);
549 -- Install the visible part of any formal of the parent that is a formal
550 -- package. Note that for the case of a formal package with a box, this
551 -- includes the formal part of the formal package (12.7(10/2)).
553 procedure Install_Parent (P : Entity_Id; In_Body : Boolean := False);
554 -- When compiling an instance of a child unit the parent (which is
555 -- itself an instance) is an enclosing scope that must be made
556 -- immediately visible. This procedure is also used to install the non-
557 -- generic parent of a generic child unit when compiling its body, so
558 -- that full views of types in the parent are made visible.
560 procedure Remove_Parent (In_Body : Boolean := False);
561 -- Reverse effect after instantiation of child is complete
563 procedure Inline_Instance_Body
565 Gen_Unit : Entity_Id;
567 -- If front-end inlining is requested, instantiate the package body,
568 -- and preserve the visibility of its compilation unit, to insure
569 -- that successive instantiations succeed.
571 -- The functions Instantiate_XXX perform various legality checks and build
572 -- the declarations for instantiated generic parameters. In all of these
573 -- Formal is the entity in the generic unit, Actual is the entity of
574 -- expression in the generic associations, and Analyzed_Formal is the
575 -- formal in the generic copy, which contains the semantic information to
576 -- be used to validate the actual.
578 function Instantiate_Object
581 Analyzed_Formal : Node_Id) return List_Id;
583 function Instantiate_Type
586 Analyzed_Formal : Node_Id;
587 Actual_Decls : List_Id) return List_Id;
589 function Instantiate_Formal_Subprogram
592 Analyzed_Formal : Node_Id) return Node_Id;
594 function Instantiate_Formal_Package
597 Analyzed_Formal : Node_Id) return List_Id;
598 -- If the formal package is declared with a box, special visibility rules
599 -- apply to its formals: they are in the visible part of the package. This
600 -- is true in the declarative region of the formal package, that is to say
601 -- in the enclosing generic or instantiation. For an instantiation, the
602 -- parameters of the formal package are made visible in an explicit step.
603 -- Furthermore, if the actual has a visible USE clause, these formals must
604 -- be made potentially use-visible as well. On exit from the enclosing
605 -- instantiation, the reverse must be done.
607 -- For a formal package declared without a box, there are conformance rules
608 -- that apply to the actuals in the generic declaration and the actuals of
609 -- the actual package in the enclosing instantiation. The simplest way to
610 -- apply these rules is to repeat the instantiation of the formal package
611 -- in the context of the enclosing instance, and compare the generic
612 -- associations of this instantiation with those of the actual package.
613 -- This internal instantiation only needs to contain the renamings of the
614 -- formals: the visible and private declarations themselves need not be
617 -- In Ada 2005, the formal package may be only partially parametrized. In
618 -- that case the visibility step must make visible those actuals whose
619 -- corresponding formals were given with a box. A final complication
620 -- involves inherited operations from formal derived types, which must be
621 -- visible if the type is.
623 function Is_In_Main_Unit (N : Node_Id) return Boolean;
624 -- Test if given node is in the main unit
626 procedure Load_Parent_Of_Generic
629 Body_Optional : Boolean := False);
630 -- If the generic appears in a separate non-generic library unit, load the
631 -- corresponding body to retrieve the body of the generic. N is the node
632 -- for the generic instantiation, Spec is the generic package declaration.
634 -- Body_Optional is a flag that indicates that the body is being loaded to
635 -- ensure that temporaries are generated consistently when there are other
636 -- instances in the current declarative part that precede the one being
637 -- loaded. In that case a missing body is acceptable.
639 procedure Inherit_Context (Gen_Decl : Node_Id; Inst : Node_Id);
640 -- Add the context clause of the unit containing a generic unit to a
641 -- compilation unit that is, or contains, an instantiation.
643 function Get_Associated_Node (N : Node_Id) return Node_Id;
644 -- In order to propagate semantic information back from the analyzed copy
645 -- to the original generic, we maintain links between selected nodes in the
646 -- generic and their corresponding copies. At the end of generic analysis,
647 -- the routine Save_Global_References traverses the generic tree, examines
648 -- the semantic information, and preserves the links to those nodes that
649 -- contain global information. At instantiation, the information from the
650 -- associated node is placed on the new copy, so that name resolution is
653 -- Three kinds of source nodes have associated nodes:
655 -- a) those that can reference (denote) entities, that is identifiers,
656 -- character literals, expanded_names, operator symbols, operators,
657 -- and attribute reference nodes. These nodes have an Entity field
658 -- and are the set of nodes that are in N_Has_Entity.
660 -- b) aggregates (N_Aggregate and N_Extension_Aggregate)
662 -- c) selected components (N_Selected_Component)
664 -- For the first class, the associated node preserves the entity if it is
665 -- global. If the generic contains nested instantiations, the associated
666 -- node itself has been recopied, and a chain of them must be followed.
668 -- For aggregates, the associated node allows retrieval of the type, which
669 -- may otherwise not appear in the generic. The view of this type may be
670 -- different between generic and instantiation, and the full view can be
671 -- installed before the instantiation is analyzed. For aggregates of type
672 -- extensions, the same view exchange may have to be performed for some of
673 -- the ancestor types, if their view is private at the point of
676 -- Nodes that are selected components in the parse tree may be rewritten
677 -- as expanded names after resolution, and must be treated as potential
678 -- entity holders, which is why they also have an Associated_Node.
680 -- Nodes that do not come from source, such as freeze nodes, do not appear
681 -- in the generic tree, and need not have an associated node.
683 -- The associated node is stored in the Associated_Node field. Note that
684 -- this field overlaps Entity, which is fine, because the whole point is
685 -- that we don't need or want the normal Entity field in this situation.
687 procedure Map_Formal_Package_Entities (Form : Entity_Id; Act : Entity_Id);
688 -- Within the generic part, entities in the formal package are
689 -- visible. To validate subsequent type declarations, indicate
690 -- the correspondence between the entities in the analyzed formal,
691 -- and the entities in the actual package. There are three packages
692 -- involved in the instantiation of a formal package: the parent
693 -- generic P1 which appears in the generic declaration, the fake
694 -- instantiation P2 which appears in the analyzed generic, and whose
695 -- visible entities may be used in subsequent formals, and the actual
696 -- P3 in the instance. To validate subsequent formals, me indicate
697 -- that the entities in P2 are mapped into those of P3. The mapping of
698 -- entities has to be done recursively for nested packages.
700 procedure Move_Freeze_Nodes
704 -- Freeze nodes can be generated in the analysis of a generic unit, but
705 -- will not be seen by the back-end. It is necessary to move those nodes
706 -- to the enclosing scope if they freeze an outer entity. We place them
707 -- at the end of the enclosing generic package, which is semantically
710 procedure Preanalyze_Actuals (N : Node_Id);
711 -- Analyze actuals to perform name resolution. Full resolution is done
712 -- later, when the expected types are known, but names have to be captured
713 -- before installing parents of generics, that are not visible for the
714 -- actuals themselves.
716 procedure Valid_Default_Attribute (Nam : Entity_Id; Def : Node_Id);
717 -- Verify that an attribute that appears as the default for a formal
718 -- subprogram is a function or procedure with the correct profile.
720 -------------------------------------------
721 -- Data Structures for Generic Renamings --
722 -------------------------------------------
724 -- The map Generic_Renamings associates generic entities with their
725 -- corresponding actuals. Currently used to validate type instances. It
726 -- will eventually be used for all generic parameters to eliminate the
727 -- need for overload resolution in the instance.
729 type Assoc_Ptr is new Int;
731 Assoc_Null : constant Assoc_Ptr := -1;
736 Next_In_HTable : Assoc_Ptr;
739 package Generic_Renamings is new Table.Table
740 (Table_Component_Type => Assoc,
741 Table_Index_Type => Assoc_Ptr,
742 Table_Low_Bound => 0,
744 Table_Increment => 100,
745 Table_Name => "Generic_Renamings");
747 -- Variable to hold enclosing instantiation. When the environment is
748 -- saved for a subprogram inlining, the corresponding Act_Id is empty.
750 Current_Instantiated_Parent : Assoc := (Empty, Empty, Assoc_Null);
752 -- Hash table for associations
754 HTable_Size : constant := 37;
755 type HTable_Range is range 0 .. HTable_Size - 1;
757 procedure Set_Next_Assoc (E : Assoc_Ptr; Next : Assoc_Ptr);
758 function Next_Assoc (E : Assoc_Ptr) return Assoc_Ptr;
759 function Get_Gen_Id (E : Assoc_Ptr) return Entity_Id;
760 function Hash (F : Entity_Id) return HTable_Range;
762 package Generic_Renamings_HTable is new GNAT.HTable.Static_HTable (
763 Header_Num => HTable_Range,
765 Elmt_Ptr => Assoc_Ptr,
766 Null_Ptr => Assoc_Null,
767 Set_Next => Set_Next_Assoc,
770 Get_Key => Get_Gen_Id,
774 Exchanged_Views : Elist_Id;
775 -- This list holds the private views that have been exchanged during
776 -- instantiation to restore the visibility of the generic declaration.
777 -- (see comments above). After instantiation, the current visibility is
778 -- reestablished by means of a traversal of this list.
780 Hidden_Entities : Elist_Id;
781 -- This list holds the entities of the current scope that are removed
782 -- from immediate visibility when instantiating a child unit. Their
783 -- visibility is restored in Remove_Parent.
785 -- Because instantiations can be recursive, the following must be saved
786 -- on entry and restored on exit from an instantiation (spec or body).
787 -- This is done by the two procedures Save_Env and Restore_Env. For
788 -- package and subprogram instantiations (but not for the body instances)
789 -- the action of Save_Env is done in two steps: Init_Env is called before
790 -- Check_Generic_Child_Unit, because setting the parent instances requires
791 -- that the visibility data structures be properly initialized. Once the
792 -- generic is unit is validated, Set_Instance_Env completes Save_Env.
794 Parent_Unit_Visible : Boolean := False;
795 -- Parent_Unit_Visible is used when the generic is a child unit, and
796 -- indicates whether the ultimate parent of the generic is visible in the
797 -- instantiation environment. It is used to reset the visibility of the
798 -- parent at the end of the instantiation (see Remove_Parent).
800 Instance_Parent_Unit : Entity_Id := Empty;
801 -- This records the ultimate parent unit of an instance of a generic
802 -- child unit and is used in conjunction with Parent_Unit_Visible to
803 -- indicate the unit to which the Parent_Unit_Visible flag corresponds.
805 type Instance_Env is record
806 Instantiated_Parent : Assoc;
807 Exchanged_Views : Elist_Id;
808 Hidden_Entities : Elist_Id;
809 Current_Sem_Unit : Unit_Number_Type;
810 Parent_Unit_Visible : Boolean := False;
811 Instance_Parent_Unit : Entity_Id := Empty;
812 Switches : Config_Switches_Type;
815 package Instance_Envs is new Table.Table (
816 Table_Component_Type => Instance_Env,
817 Table_Index_Type => Int,
818 Table_Low_Bound => 0,
820 Table_Increment => 100,
821 Table_Name => "Instance_Envs");
823 procedure Restore_Private_Views
824 (Pack_Id : Entity_Id;
825 Is_Package : Boolean := True);
826 -- Restore the private views of external types, and unmark the generic
827 -- renamings of actuals, so that they become compatible subtypes again.
828 -- For subprograms, Pack_Id is the package constructed to hold the
831 procedure Switch_View (T : Entity_Id);
832 -- Switch the partial and full views of a type and its private
833 -- dependents (i.e. its subtypes and derived types).
835 ------------------------------------
836 -- Structures for Error Reporting --
837 ------------------------------------
839 Instantiation_Node : Node_Id;
840 -- Used by subprograms that validate instantiation of formal parameters
841 -- where there might be no actual on which to place the error message.
842 -- Also used to locate the instantiation node for generic subunits.
844 Instantiation_Error : exception;
845 -- When there is a semantic error in the generic parameter matching,
846 -- there is no point in continuing the instantiation, because the
847 -- number of cascaded errors is unpredictable. This exception aborts
848 -- the instantiation process altogether.
850 S_Adjustment : Sloc_Adjustment;
851 -- Offset created for each node in an instantiation, in order to keep
852 -- track of the source position of the instantiation in each of its nodes.
853 -- A subsequent semantic error or warning on a construct of the instance
854 -- points to both places: the original generic node, and the point of
855 -- instantiation. See Sinput and Sinput.L for additional details.
857 ------------------------------------------------------------
858 -- Data structure for keeping track when inside a Generic --
859 ------------------------------------------------------------
861 -- The following table is used to save values of the Inside_A_Generic
862 -- flag (see spec of Sem) when they are saved by Start_Generic.
864 package Generic_Flags is new Table.Table (
865 Table_Component_Type => Boolean,
866 Table_Index_Type => Int,
867 Table_Low_Bound => 0,
869 Table_Increment => 200,
870 Table_Name => "Generic_Flags");
872 ---------------------------
873 -- Abandon_Instantiation --
874 ---------------------------
876 procedure Abandon_Instantiation (N : Node_Id) is
878 Error_Msg_N ("\instantiation abandoned!", N);
879 raise Instantiation_Error;
880 end Abandon_Instantiation;
882 --------------------------
883 -- Analyze_Associations --
884 --------------------------
886 function Analyze_Associations
889 F_Copy : List_Id) return List_Id
892 Actual_Types : constant Elist_Id := New_Elmt_List;
893 Assoc : constant List_Id := New_List;
894 Default_Actuals : constant Elist_Id := New_Elmt_List;
895 Gen_Unit : constant Entity_Id :=
896 Defining_Entity (Parent (F_Copy));
901 Next_Formal : Node_Id;
902 Temp_Formal : Node_Id;
903 Analyzed_Formal : Node_Id;
906 First_Named : Node_Id := Empty;
908 Default_Formals : constant List_Id := New_List;
909 -- If an Others_Choice is present, some of the formals may be defaulted.
910 -- To simplify the treatment of visibility in an instance, we introduce
911 -- individual defaults for each such formal. These defaults are
912 -- appended to the list of associations and replace the Others_Choice.
914 Found_Assoc : Node_Id;
915 -- Association for the current formal being match. Empty if there are
916 -- no remaining actuals, or if there is no named association with the
917 -- name of the formal.
919 Is_Named_Assoc : Boolean;
920 Num_Matched : Int := 0;
921 Num_Actuals : Int := 0;
923 Others_Present : Boolean := False;
924 -- In Ada 2005, indicates partial parametrization of a formal
925 -- package. As usual an other association must be last in the list.
927 function Matching_Actual
929 A_F : Entity_Id) return Node_Id;
930 -- Find actual that corresponds to a given a formal parameter. If the
931 -- actuals are positional, return the next one, if any. If the actuals
932 -- are named, scan the parameter associations to find the right one.
933 -- A_F is the corresponding entity in the analyzed generic,which is
934 -- placed on the selector name for ASIS use.
936 -- In Ada 2005, a named association may be given with a box, in which
937 -- case Matching_Actual sets Found_Assoc to the generic association,
938 -- but return Empty for the actual itself. In this case the code below
939 -- creates a corresponding declaration for the formal.
941 function Partial_Parametrization return Boolean;
942 -- Ada 2005: if no match is found for a given formal, check if the
943 -- association for it includes a box, or whether the associations
944 -- include an Others clause.
946 procedure Process_Default (F : Entity_Id);
947 -- Add a copy of the declaration of generic formal F to the list of
948 -- associations, and add an explicit box association for F if there
949 -- is none yet, and the default comes from an Others_Choice.
951 procedure Set_Analyzed_Formal;
952 -- Find the node in the generic copy that corresponds to a given formal.
953 -- The semantic information on this node is used to perform legality
954 -- checks on the actuals. Because semantic analysis can introduce some
955 -- anonymous entities or modify the declaration node itself, the
956 -- correspondence between the two lists is not one-one. In addition to
957 -- anonymous types, the presence a formal equality will introduce an
958 -- implicit declaration for the corresponding inequality.
960 ---------------------
961 -- Matching_Actual --
962 ---------------------
964 function Matching_Actual
966 A_F : Entity_Id) return Node_Id
972 Is_Named_Assoc := False;
974 -- End of list of purely positional parameters
976 if No (Actual) or else Nkind (Actual) = N_Others_Choice then
977 Found_Assoc := Empty;
980 -- Case of positional parameter corresponding to current formal
982 elsif No (Selector_Name (Actual)) then
983 Found_Assoc := Actual;
984 Act := Explicit_Generic_Actual_Parameter (Actual);
985 Num_Matched := Num_Matched + 1;
988 -- Otherwise scan list of named actuals to find the one with the
989 -- desired name. All remaining actuals have explicit names.
992 Is_Named_Assoc := True;
993 Found_Assoc := Empty;
997 while Present (Actual) loop
998 if Chars (Selector_Name (Actual)) = Chars (F) then
999 Set_Entity (Selector_Name (Actual), A_F);
1000 Set_Etype (Selector_Name (Actual), Etype (A_F));
1001 Generate_Reference (A_F, Selector_Name (Actual));
1002 Found_Assoc := Actual;
1003 Act := Explicit_Generic_Actual_Parameter (Actual);
1004 Num_Matched := Num_Matched + 1;
1012 -- Reset for subsequent searches. In most cases the named
1013 -- associations are in order. If they are not, we reorder them
1014 -- to avoid scanning twice the same actual. This is not just a
1015 -- question of efficiency: there may be multiple defaults with
1016 -- boxes that have the same name. In a nested instantiation we
1017 -- insert actuals for those defaults, and cannot rely on their
1018 -- names to disambiguate them.
1020 if Actual = First_Named then
1023 elsif Present (Actual) then
1024 Insert_Before (First_Named, Remove_Next (Prev));
1027 Actual := First_Named;
1030 if Is_Entity_Name (Act) and then Present (Entity (Act)) then
1031 Set_Used_As_Generic_Actual (Entity (Act));
1035 end Matching_Actual;
1037 -----------------------------
1038 -- Partial_Parametrization --
1039 -----------------------------
1041 function Partial_Parametrization return Boolean is
1043 return Others_Present
1044 or else (Present (Found_Assoc) and then Box_Present (Found_Assoc));
1045 end Partial_Parametrization;
1047 ---------------------
1048 -- Process_Default --
1049 ---------------------
1051 procedure Process_Default (F : Entity_Id) is
1052 Loc : constant Source_Ptr := Sloc (I_Node);
1053 F_Id : constant Entity_Id := Defining_Entity (F);
1059 -- Append copy of formal declaration to associations, and create new
1060 -- defining identifier for it.
1062 Decl := New_Copy_Tree (F);
1063 Id := Make_Defining_Identifier (Sloc (F_Id), Chars => Chars (F_Id));
1065 if Nkind (F) in N_Formal_Subprogram_Declaration then
1066 Set_Defining_Unit_Name (Specification (Decl), Id);
1069 Set_Defining_Identifier (Decl, Id);
1072 Append (Decl, Assoc);
1074 if No (Found_Assoc) then
1076 Make_Generic_Association (Loc,
1077 Selector_Name => New_Occurrence_Of (Id, Loc),
1078 Explicit_Generic_Actual_Parameter => Empty);
1079 Set_Box_Present (Default);
1080 Append (Default, Default_Formals);
1082 end Process_Default;
1084 -------------------------
1085 -- Set_Analyzed_Formal --
1086 -------------------------
1088 procedure Set_Analyzed_Formal is
1092 while Present (Analyzed_Formal) loop
1093 Kind := Nkind (Analyzed_Formal);
1095 case Nkind (Formal) is
1097 when N_Formal_Subprogram_Declaration =>
1098 exit when Kind in N_Formal_Subprogram_Declaration
1101 (Defining_Unit_Name (Specification (Formal))) =
1103 (Defining_Unit_Name (Specification (Analyzed_Formal)));
1105 when N_Formal_Package_Declaration =>
1106 exit when Nkind_In (Kind, N_Formal_Package_Declaration,
1107 N_Generic_Package_Declaration,
1108 N_Package_Declaration);
1110 when N_Use_Package_Clause | N_Use_Type_Clause => exit;
1114 -- Skip freeze nodes, and nodes inserted to replace
1115 -- unrecognized pragmas.
1118 Kind not in N_Formal_Subprogram_Declaration
1119 and then not Nkind_In (Kind, N_Subprogram_Declaration,
1123 and then Chars (Defining_Identifier (Formal)) =
1124 Chars (Defining_Identifier (Analyzed_Formal));
1127 Next (Analyzed_Formal);
1129 end Set_Analyzed_Formal;
1131 -- Start of processing for Analyze_Associations
1134 Actuals := Generic_Associations (I_Node);
1136 if Present (Actuals) then
1138 -- Check for an Others choice, indicating a partial parametrization
1139 -- for a formal package.
1141 Actual := First (Actuals);
1142 while Present (Actual) loop
1143 if Nkind (Actual) = N_Others_Choice then
1144 Others_Present := True;
1146 if Present (Next (Actual)) then
1147 Error_Msg_N ("others must be last association", Actual);
1150 -- This subprogram is used both for formal packages and for
1151 -- instantiations. For the latter, associations must all be
1154 if Nkind (I_Node) /= N_Formal_Package_Declaration
1155 and then Comes_From_Source (I_Node)
1158 ("others association not allowed in an instance",
1162 -- In any case, nothing to do after the others association
1166 elsif Box_Present (Actual)
1167 and then Comes_From_Source (I_Node)
1168 and then Nkind (I_Node) /= N_Formal_Package_Declaration
1171 ("box association not allowed in an instance", Actual);
1177 -- If named associations are present, save first named association
1178 -- (it may of course be Empty) to facilitate subsequent name search.
1180 First_Named := First (Actuals);
1181 while Present (First_Named)
1182 and then Nkind (First_Named) /= N_Others_Choice
1183 and then No (Selector_Name (First_Named))
1185 Num_Actuals := Num_Actuals + 1;
1190 Named := First_Named;
1191 while Present (Named) loop
1192 if Nkind (Named) /= N_Others_Choice
1193 and then No (Selector_Name (Named))
1195 Error_Msg_N ("invalid positional actual after named one", Named);
1196 Abandon_Instantiation (Named);
1199 -- A named association may lack an actual parameter, if it was
1200 -- introduced for a default subprogram that turns out to be local
1201 -- to the outer instantiation.
1203 if Nkind (Named) /= N_Others_Choice
1204 and then Present (Explicit_Generic_Actual_Parameter (Named))
1206 Num_Actuals := Num_Actuals + 1;
1212 if Present (Formals) then
1213 Formal := First_Non_Pragma (Formals);
1214 Analyzed_Formal := First_Non_Pragma (F_Copy);
1216 if Present (Actuals) then
1217 Actual := First (Actuals);
1219 -- All formals should have default values
1225 while Present (Formal) loop
1226 Set_Analyzed_Formal;
1227 Next_Formal := Next_Non_Pragma (Formal);
1229 case Nkind (Formal) is
1230 when N_Formal_Object_Declaration =>
1233 Defining_Identifier (Formal),
1234 Defining_Identifier (Analyzed_Formal));
1236 if No (Match) and then Partial_Parametrization then
1237 Process_Default (Formal);
1240 (Instantiate_Object (Formal, Match, Analyzed_Formal),
1244 when N_Formal_Type_Declaration =>
1247 Defining_Identifier (Formal),
1248 Defining_Identifier (Analyzed_Formal));
1251 if Partial_Parametrization then
1252 Process_Default (Formal);
1255 Error_Msg_Sloc := Sloc (Gen_Unit);
1259 Defining_Identifier (Formal));
1260 Error_Msg_NE ("\in instantiation of & declared#",
1261 Instantiation_Node, Gen_Unit);
1262 Abandon_Instantiation (Instantiation_Node);
1269 (Formal, Match, Analyzed_Formal, Assoc),
1272 -- An instantiation is a freeze point for the actuals,
1273 -- unless this is a rewritten formal package.
1275 if Nkind (I_Node) /= N_Formal_Package_Declaration then
1276 Append_Elmt (Entity (Match), Actual_Types);
1280 -- A remote access-to-class-wide type must not be an
1281 -- actual parameter for a generic formal of an access
1282 -- type (E.2.2 (17)).
1284 if Nkind (Analyzed_Formal) = N_Formal_Type_Declaration
1286 Nkind (Formal_Type_Definition (Analyzed_Formal)) =
1287 N_Access_To_Object_Definition
1289 Validate_Remote_Access_To_Class_Wide_Type (Match);
1292 when N_Formal_Subprogram_Declaration =>
1295 Defining_Unit_Name (Specification (Formal)),
1296 Defining_Unit_Name (Specification (Analyzed_Formal)));
1298 -- If the formal subprogram has the same name as another
1299 -- formal subprogram of the generic, then a named
1300 -- association is illegal (12.3(9)). Exclude named
1301 -- associations that are generated for a nested instance.
1304 and then Is_Named_Assoc
1305 and then Comes_From_Source (Found_Assoc)
1307 Temp_Formal := First (Formals);
1308 while Present (Temp_Formal) loop
1309 if Nkind (Temp_Formal) in
1310 N_Formal_Subprogram_Declaration
1311 and then Temp_Formal /= Formal
1313 Chars (Selector_Name (Found_Assoc)) =
1314 Chars (Defining_Unit_Name
1315 (Specification (Temp_Formal)))
1318 ("name not allowed for overloaded formal",
1320 Abandon_Instantiation (Instantiation_Node);
1327 -- If there is no corresponding actual, this may be case of
1328 -- partial parametrization, or else the formal has a default
1332 and then Partial_Parametrization
1334 Process_Default (Formal);
1337 Instantiate_Formal_Subprogram
1338 (Formal, Match, Analyzed_Formal));
1341 -- If this is a nested generic, preserve default for later
1345 and then Box_Present (Formal)
1348 (Defining_Unit_Name (Specification (Last (Assoc))),
1352 when N_Formal_Package_Declaration =>
1355 Defining_Identifier (Formal),
1356 Defining_Identifier (Original_Node (Analyzed_Formal)));
1359 if Partial_Parametrization then
1360 Process_Default (Formal);
1363 Error_Msg_Sloc := Sloc (Gen_Unit);
1366 Instantiation_Node, Defining_Identifier (Formal));
1367 Error_Msg_NE ("\in instantiation of & declared#",
1368 Instantiation_Node, Gen_Unit);
1370 Abandon_Instantiation (Instantiation_Node);
1376 (Instantiate_Formal_Package
1377 (Formal, Match, Analyzed_Formal),
1381 -- For use type and use package appearing in the generic part,
1382 -- we have already copied them, so we can just move them where
1383 -- they belong (we mustn't recopy them since this would mess up
1384 -- the Sloc values).
1386 when N_Use_Package_Clause |
1387 N_Use_Type_Clause =>
1388 if Nkind (Original_Node (I_Node)) =
1389 N_Formal_Package_Declaration
1391 Append (New_Copy_Tree (Formal), Assoc);
1394 Append (Formal, Assoc);
1398 raise Program_Error;
1402 Formal := Next_Formal;
1403 Next_Non_Pragma (Analyzed_Formal);
1406 if Num_Actuals > Num_Matched then
1407 Error_Msg_Sloc := Sloc (Gen_Unit);
1409 if Present (Selector_Name (Actual)) then
1411 ("unmatched actual&",
1412 Actual, Selector_Name (Actual));
1413 Error_Msg_NE ("\in instantiation of& declared#",
1417 ("unmatched actual in instantiation of& declared#",
1422 elsif Present (Actuals) then
1424 ("too many actuals in generic instantiation", Instantiation_Node);
1428 Elmt : Elmt_Id := First_Elmt (Actual_Types);
1430 while Present (Elmt) loop
1431 Freeze_Before (I_Node, Node (Elmt));
1436 -- If there are default subprograms, normalize the tree by adding
1437 -- explicit associations for them. This is required if the instance
1438 -- appears within a generic.
1446 Elmt := First_Elmt (Default_Actuals);
1447 while Present (Elmt) loop
1448 if No (Actuals) then
1449 Actuals := New_List;
1450 Set_Generic_Associations (I_Node, Actuals);
1453 Subp := Node (Elmt);
1455 Make_Generic_Association (Sloc (Subp),
1456 Selector_Name => New_Occurrence_Of (Subp, Sloc (Subp)),
1457 Explicit_Generic_Actual_Parameter =>
1458 New_Occurrence_Of (Subp, Sloc (Subp)));
1459 Mark_Rewrite_Insertion (New_D);
1460 Append_To (Actuals, New_D);
1465 -- If this is a formal package, normalize the parameter list by adding
1466 -- explicit box associations for the formals that are covered by an
1469 if not Is_Empty_List (Default_Formals) then
1470 Append_List (Default_Formals, Formals);
1474 end Analyze_Associations;
1476 -------------------------------
1477 -- Analyze_Formal_Array_Type --
1478 -------------------------------
1480 procedure Analyze_Formal_Array_Type
1481 (T : in out Entity_Id;
1487 -- Treated like a non-generic array declaration, with additional
1492 if Nkind (Def) = N_Constrained_Array_Definition then
1493 DSS := First (Discrete_Subtype_Definitions (Def));
1494 while Present (DSS) loop
1495 if Nkind_In (DSS, N_Subtype_Indication,
1497 N_Attribute_Reference)
1499 Error_Msg_N ("only a subtype mark is allowed in a formal", DSS);
1506 Array_Type_Declaration (T, Def);
1507 Set_Is_Generic_Type (Base_Type (T));
1509 if Ekind (Component_Type (T)) = E_Incomplete_Type
1510 and then No (Full_View (Component_Type (T)))
1512 Error_Msg_N ("premature usage of incomplete type", Def);
1514 -- Check that range constraint is not allowed on the component type
1515 -- of a generic formal array type (AARM 12.5.3(3))
1517 elsif Is_Internal (Component_Type (T))
1518 and then Present (Subtype_Indication (Component_Definition (Def)))
1519 and then Nkind (Original_Node
1520 (Subtype_Indication (Component_Definition (Def)))) =
1521 N_Subtype_Indication
1524 ("in a formal, a subtype indication can only be "
1525 & "a subtype mark (RM 12.5.3(3))",
1526 Subtype_Indication (Component_Definition (Def)));
1529 end Analyze_Formal_Array_Type;
1531 ---------------------------------------------
1532 -- Analyze_Formal_Decimal_Fixed_Point_Type --
1533 ---------------------------------------------
1535 -- As for other generic types, we create a valid type representation with
1536 -- legal but arbitrary attributes, whose values are never considered
1537 -- static. For all scalar types we introduce an anonymous base type, with
1538 -- the same attributes. We choose the corresponding integer type to be
1539 -- Standard_Integer.
1541 procedure Analyze_Formal_Decimal_Fixed_Point_Type
1545 Loc : constant Source_Ptr := Sloc (Def);
1546 Base : constant Entity_Id :=
1548 (E_Decimal_Fixed_Point_Type,
1549 Current_Scope, Sloc (Def), 'G');
1550 Int_Base : constant Entity_Id := Standard_Integer;
1551 Delta_Val : constant Ureal := Ureal_1;
1552 Digs_Val : constant Uint := Uint_6;
1557 Set_Etype (Base, Base);
1558 Set_Size_Info (Base, Int_Base);
1559 Set_RM_Size (Base, RM_Size (Int_Base));
1560 Set_First_Rep_Item (Base, First_Rep_Item (Int_Base));
1561 Set_Digits_Value (Base, Digs_Val);
1562 Set_Delta_Value (Base, Delta_Val);
1563 Set_Small_Value (Base, Delta_Val);
1564 Set_Scalar_Range (Base,
1566 Low_Bound => Make_Real_Literal (Loc, Ureal_1),
1567 High_Bound => Make_Real_Literal (Loc, Ureal_1)));
1569 Set_Is_Generic_Type (Base);
1570 Set_Parent (Base, Parent (Def));
1572 Set_Ekind (T, E_Decimal_Fixed_Point_Subtype);
1573 Set_Etype (T, Base);
1574 Set_Size_Info (T, Int_Base);
1575 Set_RM_Size (T, RM_Size (Int_Base));
1576 Set_First_Rep_Item (T, First_Rep_Item (Int_Base));
1577 Set_Digits_Value (T, Digs_Val);
1578 Set_Delta_Value (T, Delta_Val);
1579 Set_Small_Value (T, Delta_Val);
1580 Set_Scalar_Range (T, Scalar_Range (Base));
1581 Set_Is_Constrained (T);
1583 Check_Restriction (No_Fixed_Point, Def);
1584 end Analyze_Formal_Decimal_Fixed_Point_Type;
1586 -------------------------------------------
1587 -- Analyze_Formal_Derived_Interface_Type --
1588 -------------------------------------------
1590 procedure Analyze_Formal_Derived_Interface_Type
1595 Loc : constant Source_Ptr := Sloc (Def);
1598 -- Rewrite as a type declaration of a derived type. This ensures that
1599 -- the interface list and primitive operations are properly captured.
1602 Make_Full_Type_Declaration (Loc,
1603 Defining_Identifier => T,
1604 Type_Definition => Def));
1606 Set_Is_Generic_Type (T);
1607 end Analyze_Formal_Derived_Interface_Type;
1609 ---------------------------------
1610 -- Analyze_Formal_Derived_Type --
1611 ---------------------------------
1613 procedure Analyze_Formal_Derived_Type
1618 Loc : constant Source_Ptr := Sloc (Def);
1619 Unk_Disc : constant Boolean := Unknown_Discriminants_Present (N);
1623 Set_Is_Generic_Type (T);
1625 if Private_Present (Def) then
1627 Make_Private_Extension_Declaration (Loc,
1628 Defining_Identifier => T,
1629 Discriminant_Specifications => Discriminant_Specifications (N),
1630 Unknown_Discriminants_Present => Unk_Disc,
1631 Subtype_Indication => Subtype_Mark (Def),
1632 Interface_List => Interface_List (Def));
1634 Set_Abstract_Present (New_N, Abstract_Present (Def));
1635 Set_Limited_Present (New_N, Limited_Present (Def));
1636 Set_Synchronized_Present (New_N, Synchronized_Present (Def));
1640 Make_Full_Type_Declaration (Loc,
1641 Defining_Identifier => T,
1642 Discriminant_Specifications =>
1643 Discriminant_Specifications (Parent (T)),
1645 Make_Derived_Type_Definition (Loc,
1646 Subtype_Indication => Subtype_Mark (Def)));
1648 Set_Abstract_Present
1649 (Type_Definition (New_N), Abstract_Present (Def));
1651 (Type_Definition (New_N), Limited_Present (Def));
1658 if not Is_Composite_Type (T) then
1660 ("unknown discriminants not allowed for elementary types", N);
1662 Set_Has_Unknown_Discriminants (T);
1663 Set_Is_Constrained (T, False);
1667 -- If the parent type has a known size, so does the formal, which makes
1668 -- legal representation clauses that involve the formal.
1670 Set_Size_Known_At_Compile_Time
1671 (T, Size_Known_At_Compile_Time (Entity (Subtype_Mark (Def))));
1672 end Analyze_Formal_Derived_Type;
1674 ----------------------------------
1675 -- Analyze_Formal_Discrete_Type --
1676 ----------------------------------
1678 -- The operations defined for a discrete types are those of an enumeration
1679 -- type. The size is set to an arbitrary value, for use in analyzing the
1682 procedure Analyze_Formal_Discrete_Type (T : Entity_Id; Def : Node_Id) is
1683 Loc : constant Source_Ptr := Sloc (Def);
1687 Base : constant Entity_Id :=
1689 (E_Floating_Point_Type, Current_Scope, Sloc (Def), 'G');
1692 Set_Ekind (T, E_Enumeration_Subtype);
1693 Set_Etype (T, Base);
1696 Set_Is_Generic_Type (T);
1697 Set_Is_Constrained (T);
1699 -- For semantic analysis, the bounds of the type must be set to some
1700 -- non-static value. The simplest is to create attribute nodes for those
1701 -- bounds, that refer to the type itself. These bounds are never
1702 -- analyzed but serve as place-holders.
1705 Make_Attribute_Reference (Loc,
1706 Attribute_Name => Name_First,
1707 Prefix => New_Reference_To (T, Loc));
1711 Make_Attribute_Reference (Loc,
1712 Attribute_Name => Name_Last,
1713 Prefix => New_Reference_To (T, Loc));
1716 Set_Scalar_Range (T,
1721 Set_Ekind (Base, E_Enumeration_Type);
1722 Set_Etype (Base, Base);
1723 Init_Size (Base, 8);
1724 Init_Alignment (Base);
1725 Set_Is_Generic_Type (Base);
1726 Set_Scalar_Range (Base, Scalar_Range (T));
1727 Set_Parent (Base, Parent (Def));
1728 end Analyze_Formal_Discrete_Type;
1730 ----------------------------------
1731 -- Analyze_Formal_Floating_Type --
1732 ---------------------------------
1734 procedure Analyze_Formal_Floating_Type (T : Entity_Id; Def : Node_Id) is
1735 Base : constant Entity_Id :=
1737 (E_Floating_Point_Type, Current_Scope, Sloc (Def), 'G');
1740 -- The various semantic attributes are taken from the predefined type
1741 -- Float, just so that all of them are initialized. Their values are
1742 -- never used because no constant folding or expansion takes place in
1743 -- the generic itself.
1746 Set_Ekind (T, E_Floating_Point_Subtype);
1747 Set_Etype (T, Base);
1748 Set_Size_Info (T, (Standard_Float));
1749 Set_RM_Size (T, RM_Size (Standard_Float));
1750 Set_Digits_Value (T, Digits_Value (Standard_Float));
1751 Set_Scalar_Range (T, Scalar_Range (Standard_Float));
1752 Set_Is_Constrained (T);
1754 Set_Is_Generic_Type (Base);
1755 Set_Etype (Base, Base);
1756 Set_Size_Info (Base, (Standard_Float));
1757 Set_RM_Size (Base, RM_Size (Standard_Float));
1758 Set_Digits_Value (Base, Digits_Value (Standard_Float));
1759 Set_Scalar_Range (Base, Scalar_Range (Standard_Float));
1760 Set_Parent (Base, Parent (Def));
1762 Check_Restriction (No_Floating_Point, Def);
1763 end Analyze_Formal_Floating_Type;
1765 -----------------------------------
1766 -- Analyze_Formal_Interface_Type;--
1767 -----------------------------------
1769 procedure Analyze_Formal_Interface_Type
1774 Loc : constant Source_Ptr := Sloc (N);
1779 Make_Full_Type_Declaration (Loc,
1780 Defining_Identifier => T,
1781 Type_Definition => Def);
1785 Set_Is_Generic_Type (T);
1786 end Analyze_Formal_Interface_Type;
1788 ---------------------------------
1789 -- Analyze_Formal_Modular_Type --
1790 ---------------------------------
1792 procedure Analyze_Formal_Modular_Type (T : Entity_Id; Def : Node_Id) is
1794 -- Apart from their entity kind, generic modular types are treated like
1795 -- signed integer types, and have the same attributes.
1797 Analyze_Formal_Signed_Integer_Type (T, Def);
1798 Set_Ekind (T, E_Modular_Integer_Subtype);
1799 Set_Ekind (Etype (T), E_Modular_Integer_Type);
1801 end Analyze_Formal_Modular_Type;
1803 ---------------------------------------
1804 -- Analyze_Formal_Object_Declaration --
1805 ---------------------------------------
1807 procedure Analyze_Formal_Object_Declaration (N : Node_Id) is
1808 E : constant Node_Id := Default_Expression (N);
1809 Id : constant Node_Id := Defining_Identifier (N);
1816 -- Determine the mode of the formal object
1818 if Out_Present (N) then
1819 K := E_Generic_In_Out_Parameter;
1821 if not In_Present (N) then
1822 Error_Msg_N ("formal generic objects cannot have mode OUT", N);
1826 K := E_Generic_In_Parameter;
1829 if Present (Subtype_Mark (N)) then
1830 Find_Type (Subtype_Mark (N));
1831 T := Entity (Subtype_Mark (N));
1833 -- Verify that there is no redundant null exclusion
1835 if Null_Exclusion_Present (N) then
1836 if not Is_Access_Type (T) then
1838 ("null exclusion can only apply to an access type", N);
1840 elsif Can_Never_Be_Null (T) then
1842 ("`NOT NULL` not allowed (& already excludes null)",
1847 -- Ada 2005 (AI-423): Formal object with an access definition
1850 Check_Access_Definition (N);
1851 T := Access_Definition
1853 N => Access_Definition (N));
1856 if Ekind (T) = E_Incomplete_Type then
1858 Error_Node : Node_Id;
1861 if Present (Subtype_Mark (N)) then
1862 Error_Node := Subtype_Mark (N);
1864 Check_Access_Definition (N);
1865 Error_Node := Access_Definition (N);
1868 Error_Msg_N ("premature usage of incomplete type", Error_Node);
1872 if K = E_Generic_In_Parameter then
1874 -- Ada 2005 (AI-287): Limited aggregates allowed in generic formals
1876 if Ada_Version < Ada_05 and then Is_Limited_Type (T) then
1878 ("generic formal of mode IN must not be of limited type", N);
1879 Explain_Limited_Type (T, N);
1882 if Is_Abstract_Type (T) then
1884 ("generic formal of mode IN must not be of abstract type", N);
1888 Preanalyze_Spec_Expression (E, T);
1890 if Is_Limited_Type (T) and then not OK_For_Limited_Init (T, E) then
1892 ("initialization not allowed for limited types", E);
1893 Explain_Limited_Type (T, E);
1900 -- Case of generic IN OUT parameter
1903 -- If the formal has an unconstrained type, construct its actual
1904 -- subtype, as is done for subprogram formals. In this fashion, all
1905 -- its uses can refer to specific bounds.
1910 if (Is_Array_Type (T)
1911 and then not Is_Constrained (T))
1913 (Ekind (T) = E_Record_Type
1914 and then Has_Discriminants (T))
1917 Non_Freezing_Ref : constant Node_Id :=
1918 New_Reference_To (Id, Sloc (Id));
1922 -- Make sure the actual subtype doesn't generate bogus freezing
1924 Set_Must_Not_Freeze (Non_Freezing_Ref);
1925 Decl := Build_Actual_Subtype (T, Non_Freezing_Ref);
1926 Insert_Before_And_Analyze (N, Decl);
1927 Set_Actual_Subtype (Id, Defining_Identifier (Decl));
1930 Set_Actual_Subtype (Id, T);
1935 ("initialization not allowed for `IN OUT` formals", N);
1938 end Analyze_Formal_Object_Declaration;
1940 ----------------------------------------------
1941 -- Analyze_Formal_Ordinary_Fixed_Point_Type --
1942 ----------------------------------------------
1944 procedure Analyze_Formal_Ordinary_Fixed_Point_Type
1948 Loc : constant Source_Ptr := Sloc (Def);
1949 Base : constant Entity_Id :=
1951 (E_Ordinary_Fixed_Point_Type, Current_Scope, Sloc (Def), 'G');
1953 -- The semantic attributes are set for completeness only, their values
1954 -- will never be used, since all properties of the type are non-static.
1957 Set_Ekind (T, E_Ordinary_Fixed_Point_Subtype);
1958 Set_Etype (T, Base);
1959 Set_Size_Info (T, Standard_Integer);
1960 Set_RM_Size (T, RM_Size (Standard_Integer));
1961 Set_Small_Value (T, Ureal_1);
1962 Set_Delta_Value (T, Ureal_1);
1963 Set_Scalar_Range (T,
1965 Low_Bound => Make_Real_Literal (Loc, Ureal_1),
1966 High_Bound => Make_Real_Literal (Loc, Ureal_1)));
1967 Set_Is_Constrained (T);
1969 Set_Is_Generic_Type (Base);
1970 Set_Etype (Base, Base);
1971 Set_Size_Info (Base, Standard_Integer);
1972 Set_RM_Size (Base, RM_Size (Standard_Integer));
1973 Set_Small_Value (Base, Ureal_1);
1974 Set_Delta_Value (Base, Ureal_1);
1975 Set_Scalar_Range (Base, Scalar_Range (T));
1976 Set_Parent (Base, Parent (Def));
1978 Check_Restriction (No_Fixed_Point, Def);
1979 end Analyze_Formal_Ordinary_Fixed_Point_Type;
1981 ----------------------------
1982 -- Analyze_Formal_Package --
1983 ----------------------------
1985 procedure Analyze_Formal_Package (N : Node_Id) is
1986 Loc : constant Source_Ptr := Sloc (N);
1987 Pack_Id : constant Entity_Id := Defining_Identifier (N);
1989 Gen_Id : constant Node_Id := Name (N);
1991 Gen_Unit : Entity_Id;
1993 Parent_Installed : Boolean := False;
1995 Parent_Instance : Entity_Id;
1996 Renaming_In_Par : Entity_Id;
1997 No_Associations : Boolean := False;
1999 function Build_Local_Package return Node_Id;
2000 -- The formal package is rewritten so that its parameters are replaced
2001 -- with corresponding declarations. For parameters with bona fide
2002 -- associations these declarations are created by Analyze_Associations
2003 -- as for a regular instantiation. For boxed parameters, we preserve
2004 -- the formal declarations and analyze them, in order to introduce
2005 -- entities of the right kind in the environment of the formal.
2007 -------------------------
2008 -- Build_Local_Package --
2009 -------------------------
2011 function Build_Local_Package return Node_Id is
2013 Pack_Decl : Node_Id;
2016 -- Within the formal, the name of the generic package is a renaming
2017 -- of the formal (as for a regular instantiation).
2020 Make_Package_Declaration (Loc,
2023 (Specification (Original_Node (Gen_Decl)),
2024 Empty, Instantiating => True));
2026 Renaming := Make_Package_Renaming_Declaration (Loc,
2027 Defining_Unit_Name =>
2028 Make_Defining_Identifier (Loc, Chars (Gen_Unit)),
2029 Name => New_Occurrence_Of (Formal, Loc));
2031 if Nkind (Gen_Id) = N_Identifier
2032 and then Chars (Gen_Id) = Chars (Pack_Id)
2035 ("& is hidden within declaration of instance", Gen_Id, Gen_Unit);
2038 -- If the formal is declared with a box, or with an others choice,
2039 -- create corresponding declarations for all entities in the formal
2040 -- part, so that names with the proper types are available in the
2041 -- specification of the formal package.
2043 -- On the other hand, if there are no associations, then all the
2044 -- formals must have defaults, and this will be checked by the
2045 -- call to Analyze_Associations.
2048 or else Nkind (First (Generic_Associations (N))) = N_Others_Choice
2051 Formal_Decl : Node_Id;
2054 -- TBA : for a formal package, need to recurse ???
2059 (Generic_Formal_Declarations (Original_Node (Gen_Decl)));
2060 while Present (Formal_Decl) loop
2062 (Decls, Copy_Generic_Node (Formal_Decl, Empty, True));
2067 -- If generic associations are present, use Analyze_Associations to
2068 -- create the proper renaming declarations.
2072 Act_Tree : constant Node_Id :=
2074 (Original_Node (Gen_Decl), Empty,
2075 Instantiating => True);
2078 Generic_Renamings.Set_Last (0);
2079 Generic_Renamings_HTable.Reset;
2080 Instantiation_Node := N;
2083 Analyze_Associations
2085 Generic_Formal_Declarations (Act_Tree),
2086 Generic_Formal_Declarations (Gen_Decl));
2090 Append (Renaming, To => Decls);
2092 -- Add generated declarations ahead of local declarations in
2095 if No (Visible_Declarations (Specification (Pack_Decl))) then
2096 Set_Visible_Declarations (Specification (Pack_Decl), Decls);
2099 (First (Visible_Declarations (Specification (Pack_Decl))),
2104 end Build_Local_Package;
2106 -- Start of processing for Analyze_Formal_Package
2109 Text_IO_Kludge (Gen_Id);
2112 Check_Generic_Child_Unit (Gen_Id, Parent_Installed);
2113 Gen_Unit := Entity (Gen_Id);
2115 -- Check for a formal package that is a package renaming
2117 if Present (Renamed_Object (Gen_Unit)) then
2118 Gen_Unit := Renamed_Object (Gen_Unit);
2121 if Ekind (Gen_Unit) /= E_Generic_Package then
2122 Error_Msg_N ("expect generic package name", Gen_Id);
2126 elsif Gen_Unit = Current_Scope then
2128 ("generic package cannot be used as a formal package of itself",
2133 elsif In_Open_Scopes (Gen_Unit) then
2134 if Is_Compilation_Unit (Gen_Unit)
2135 and then Is_Child_Unit (Current_Scope)
2137 -- Special-case the error when the formal is a parent, and
2138 -- continue analysis to minimize cascaded errors.
2141 ("generic parent cannot be used as formal package "
2142 & "of a child unit",
2147 ("generic package cannot be used as a formal package "
2156 or else No (Generic_Associations (N))
2157 or else Nkind (First (Generic_Associations (N))) = N_Others_Choice
2159 No_Associations := True;
2162 -- If there are no generic associations, the generic parameters appear
2163 -- as local entities and are instantiated like them. We copy the generic
2164 -- package declaration as if it were an instantiation, and analyze it
2165 -- like a regular package, except that we treat the formals as
2166 -- additional visible components.
2168 Gen_Decl := Unit_Declaration_Node (Gen_Unit);
2170 if In_Extended_Main_Source_Unit (N) then
2171 Set_Is_Instantiated (Gen_Unit);
2172 Generate_Reference (Gen_Unit, N);
2175 Formal := New_Copy (Pack_Id);
2176 Create_Instantiation_Source (N, Gen_Unit, False, S_Adjustment);
2179 -- Make local generic without formals. The formals will be replaced
2180 -- with internal declarations.
2182 New_N := Build_Local_Package;
2184 -- If there are errors in the parameter list, Analyze_Associations
2185 -- raises Instantiation_Error. Patch the declaration to prevent
2186 -- further exception propagation.
2189 when Instantiation_Error =>
2191 Enter_Name (Formal);
2192 Set_Ekind (Formal, E_Variable);
2193 Set_Etype (Formal, Any_Type);
2195 if Parent_Installed then
2203 Set_Defining_Unit_Name (Specification (New_N), Formal);
2204 Set_Generic_Parent (Specification (N), Gen_Unit);
2205 Set_Instance_Env (Gen_Unit, Formal);
2206 Set_Is_Generic_Instance (Formal);
2208 Enter_Name (Formal);
2209 Set_Ekind (Formal, E_Package);
2210 Set_Etype (Formal, Standard_Void_Type);
2211 Set_Inner_Instances (Formal, New_Elmt_List);
2212 Push_Scope (Formal);
2214 if Is_Child_Unit (Gen_Unit)
2215 and then Parent_Installed
2217 -- Similarly, we have to make the name of the formal visible in the
2218 -- parent instance, to resolve properly fully qualified names that
2219 -- may appear in the generic unit. The parent instance has been
2220 -- placed on the scope stack ahead of the current scope.
2222 Parent_Instance := Scope_Stack.Table (Scope_Stack.Last - 1).Entity;
2225 Make_Defining_Identifier (Loc, Chars (Gen_Unit));
2226 Set_Ekind (Renaming_In_Par, E_Package);
2227 Set_Etype (Renaming_In_Par, Standard_Void_Type);
2228 Set_Scope (Renaming_In_Par, Parent_Instance);
2229 Set_Parent (Renaming_In_Par, Parent (Formal));
2230 Set_Renamed_Object (Renaming_In_Par, Formal);
2231 Append_Entity (Renaming_In_Par, Parent_Instance);
2234 Analyze (Specification (N));
2236 -- The formals for which associations are provided are not visible
2237 -- outside of the formal package. The others are still declared by a
2238 -- formal parameter declaration.
2240 if not No_Associations then
2245 E := First_Entity (Formal);
2246 while Present (E) loop
2247 exit when Ekind (E) = E_Package
2248 and then Renamed_Entity (E) = Formal;
2250 if not Is_Generic_Formal (E) then
2259 End_Package_Scope (Formal);
2261 if Parent_Installed then
2267 -- Inside the generic unit, the formal package is a regular package, but
2268 -- no body is needed for it. Note that after instantiation, the defining
2269 -- unit name we need is in the new tree and not in the original (see
2270 -- Package_Instantiation). A generic formal package is an instance, and
2271 -- can be used as an actual for an inner instance.
2273 Set_Has_Completion (Formal, True);
2275 -- Add semantic information to the original defining identifier.
2278 Set_Ekind (Pack_Id, E_Package);
2279 Set_Etype (Pack_Id, Standard_Void_Type);
2280 Set_Scope (Pack_Id, Scope (Formal));
2281 Set_Has_Completion (Pack_Id, True);
2282 end Analyze_Formal_Package;
2284 ---------------------------------
2285 -- Analyze_Formal_Private_Type --
2286 ---------------------------------
2288 procedure Analyze_Formal_Private_Type
2294 New_Private_Type (N, T, Def);
2296 -- Set the size to an arbitrary but legal value
2298 Set_Size_Info (T, Standard_Integer);
2299 Set_RM_Size (T, RM_Size (Standard_Integer));
2300 end Analyze_Formal_Private_Type;
2302 ----------------------------------------
2303 -- Analyze_Formal_Signed_Integer_Type --
2304 ----------------------------------------
2306 procedure Analyze_Formal_Signed_Integer_Type
2310 Base : constant Entity_Id :=
2312 (E_Signed_Integer_Type, Current_Scope, Sloc (Def), 'G');
2317 Set_Ekind (T, E_Signed_Integer_Subtype);
2318 Set_Etype (T, Base);
2319 Set_Size_Info (T, Standard_Integer);
2320 Set_RM_Size (T, RM_Size (Standard_Integer));
2321 Set_Scalar_Range (T, Scalar_Range (Standard_Integer));
2322 Set_Is_Constrained (T);
2324 Set_Is_Generic_Type (Base);
2325 Set_Size_Info (Base, Standard_Integer);
2326 Set_RM_Size (Base, RM_Size (Standard_Integer));
2327 Set_Etype (Base, Base);
2328 Set_Scalar_Range (Base, Scalar_Range (Standard_Integer));
2329 Set_Parent (Base, Parent (Def));
2330 end Analyze_Formal_Signed_Integer_Type;
2332 -------------------------------
2333 -- Analyze_Formal_Subprogram --
2334 -------------------------------
2336 procedure Analyze_Formal_Subprogram (N : Node_Id) is
2337 Spec : constant Node_Id := Specification (N);
2338 Def : constant Node_Id := Default_Name (N);
2339 Nam : constant Entity_Id := Defining_Unit_Name (Spec);
2347 if Nkind (Nam) = N_Defining_Program_Unit_Name then
2348 Error_Msg_N ("name of formal subprogram must be a direct name", Nam);
2352 Analyze_Subprogram_Declaration (N);
2353 Set_Is_Formal_Subprogram (Nam);
2354 Set_Has_Completion (Nam);
2356 if Nkind (N) = N_Formal_Abstract_Subprogram_Declaration then
2357 Set_Is_Abstract_Subprogram (Nam);
2358 Set_Is_Dispatching_Operation (Nam);
2361 Ctrl_Type : constant Entity_Id := Find_Dispatching_Type (Nam);
2363 if No (Ctrl_Type) then
2365 ("abstract formal subprogram must have a controlling type",
2368 Check_Controlling_Formals (Ctrl_Type, Nam);
2373 -- Default name is resolved at the point of instantiation
2375 if Box_Present (N) then
2378 -- Else default is bound at the point of generic declaration
2380 elsif Present (Def) then
2381 if Nkind (Def) = N_Operator_Symbol then
2382 Find_Direct_Name (Def);
2384 elsif Nkind (Def) /= N_Attribute_Reference then
2388 -- For an attribute reference, analyze the prefix and verify
2389 -- that it has the proper profile for the subprogram.
2391 Analyze (Prefix (Def));
2392 Valid_Default_Attribute (Nam, Def);
2396 -- Default name may be overloaded, in which case the interpretation
2397 -- with the correct profile must be selected, as for a renaming.
2398 -- If the definition is an indexed component, it must denote a
2399 -- member of an entry family. If it is a selected component, it
2400 -- can be a protected operation.
2402 if Etype (Def) = Any_Type then
2405 elsif Nkind (Def) = N_Selected_Component then
2406 if not Is_Overloadable (Entity (Selector_Name (Def))) then
2407 Error_Msg_N ("expect valid subprogram name as default", Def);
2410 elsif Nkind (Def) = N_Indexed_Component then
2411 if Is_Entity_Name (Prefix (Def)) then
2412 if Ekind (Entity (Prefix (Def))) /= E_Entry_Family then
2413 Error_Msg_N ("expect valid subprogram name as default", Def);
2416 elsif Nkind (Prefix (Def)) = N_Selected_Component then
2417 if Ekind (Entity (Selector_Name (Prefix (Def))))
2420 Error_Msg_N ("expect valid subprogram name as default", Def);
2424 Error_Msg_N ("expect valid subprogram name as default", Def);
2428 elsif Nkind (Def) = N_Character_Literal then
2430 -- Needs some type checks: subprogram should be parameterless???
2432 Resolve (Def, (Etype (Nam)));
2434 elsif not Is_Entity_Name (Def)
2435 or else not Is_Overloadable (Entity (Def))
2437 Error_Msg_N ("expect valid subprogram name as default", Def);
2440 elsif not Is_Overloaded (Def) then
2441 Subp := Entity (Def);
2444 Error_Msg_N ("premature usage of formal subprogram", Def);
2446 elsif not Entity_Matches_Spec (Subp, Nam) then
2447 Error_Msg_N ("no visible entity matches specification", Def);
2450 -- More than one interpretation, so disambiguate as for a renaming
2455 I1 : Interp_Index := 0;
2461 Get_First_Interp (Def, I, It);
2462 while Present (It.Nam) loop
2463 if Entity_Matches_Spec (It.Nam, Nam) then
2464 if Subp /= Any_Id then
2465 It1 := Disambiguate (Def, I1, I, Etype (Subp));
2467 if It1 = No_Interp then
2468 Error_Msg_N ("ambiguous default subprogram", Def);
2481 Get_Next_Interp (I, It);
2485 if Subp /= Any_Id then
2486 Set_Entity (Def, Subp);
2489 Error_Msg_N ("premature usage of formal subprogram", Def);
2491 elsif Ekind (Subp) /= E_Operator then
2492 Check_Mode_Conformant (Subp, Nam);
2496 Error_Msg_N ("no visible subprogram matches specification", N);
2500 end Analyze_Formal_Subprogram;
2502 -------------------------------------
2503 -- Analyze_Formal_Type_Declaration --
2504 -------------------------------------
2506 procedure Analyze_Formal_Type_Declaration (N : Node_Id) is
2507 Def : constant Node_Id := Formal_Type_Definition (N);
2511 T := Defining_Identifier (N);
2513 if Present (Discriminant_Specifications (N))
2514 and then Nkind (Def) /= N_Formal_Private_Type_Definition
2517 ("discriminants not allowed for this formal type", T);
2520 -- Enter the new name, and branch to specific routine
2523 when N_Formal_Private_Type_Definition =>
2524 Analyze_Formal_Private_Type (N, T, Def);
2526 when N_Formal_Derived_Type_Definition =>
2527 Analyze_Formal_Derived_Type (N, T, Def);
2529 when N_Formal_Discrete_Type_Definition =>
2530 Analyze_Formal_Discrete_Type (T, Def);
2532 when N_Formal_Signed_Integer_Type_Definition =>
2533 Analyze_Formal_Signed_Integer_Type (T, Def);
2535 when N_Formal_Modular_Type_Definition =>
2536 Analyze_Formal_Modular_Type (T, Def);
2538 when N_Formal_Floating_Point_Definition =>
2539 Analyze_Formal_Floating_Type (T, Def);
2541 when N_Formal_Ordinary_Fixed_Point_Definition =>
2542 Analyze_Formal_Ordinary_Fixed_Point_Type (T, Def);
2544 when N_Formal_Decimal_Fixed_Point_Definition =>
2545 Analyze_Formal_Decimal_Fixed_Point_Type (T, Def);
2547 when N_Array_Type_Definition =>
2548 Analyze_Formal_Array_Type (T, Def);
2550 when N_Access_To_Object_Definition |
2551 N_Access_Function_Definition |
2552 N_Access_Procedure_Definition =>
2553 Analyze_Generic_Access_Type (T, Def);
2555 -- Ada 2005: a interface declaration is encoded as an abstract
2556 -- record declaration or a abstract type derivation.
2558 when N_Record_Definition =>
2559 Analyze_Formal_Interface_Type (N, T, Def);
2561 when N_Derived_Type_Definition =>
2562 Analyze_Formal_Derived_Interface_Type (N, T, Def);
2568 raise Program_Error;
2572 Set_Is_Generic_Type (T);
2573 end Analyze_Formal_Type_Declaration;
2575 ------------------------------------
2576 -- Analyze_Function_Instantiation --
2577 ------------------------------------
2579 procedure Analyze_Function_Instantiation (N : Node_Id) is
2581 Analyze_Subprogram_Instantiation (N, E_Function);
2582 end Analyze_Function_Instantiation;
2584 ---------------------------------
2585 -- Analyze_Generic_Access_Type --
2586 ---------------------------------
2588 procedure Analyze_Generic_Access_Type (T : Entity_Id; Def : Node_Id) is
2592 if Nkind (Def) = N_Access_To_Object_Definition then
2593 Access_Type_Declaration (T, Def);
2595 if Is_Incomplete_Or_Private_Type (Designated_Type (T))
2596 and then No (Full_View (Designated_Type (T)))
2597 and then not Is_Generic_Type (Designated_Type (T))
2599 Error_Msg_N ("premature usage of incomplete type", Def);
2601 elsif Is_Internal (Designated_Type (T)) then
2603 ("only a subtype mark is allowed in a formal", Def);
2607 Access_Subprogram_Declaration (T, Def);
2609 end Analyze_Generic_Access_Type;
2611 ---------------------------------
2612 -- Analyze_Generic_Formal_Part --
2613 ---------------------------------
2615 procedure Analyze_Generic_Formal_Part (N : Node_Id) is
2616 Gen_Parm_Decl : Node_Id;
2619 -- The generic formals are processed in the scope of the generic unit,
2620 -- where they are immediately visible. The scope is installed by the
2623 Gen_Parm_Decl := First (Generic_Formal_Declarations (N));
2625 while Present (Gen_Parm_Decl) loop
2626 Analyze (Gen_Parm_Decl);
2627 Next (Gen_Parm_Decl);
2630 Generate_Reference_To_Generic_Formals (Current_Scope);
2631 end Analyze_Generic_Formal_Part;
2633 ------------------------------------------
2634 -- Analyze_Generic_Package_Declaration --
2635 ------------------------------------------
2637 procedure Analyze_Generic_Package_Declaration (N : Node_Id) is
2638 Loc : constant Source_Ptr := Sloc (N);
2641 Save_Parent : Node_Id;
2643 Decls : constant List_Id :=
2644 Visible_Declarations (Specification (N));
2648 -- We introduce a renaming of the enclosing package, to have a usable
2649 -- entity as the prefix of an expanded name for a local entity of the
2650 -- form Par.P.Q, where P is the generic package. This is because a local
2651 -- entity named P may hide it, so that the usual visibility rules in
2652 -- the instance will not resolve properly.
2655 Make_Package_Renaming_Declaration (Loc,
2656 Defining_Unit_Name =>
2657 Make_Defining_Identifier (Loc,
2658 Chars => New_External_Name (Chars (Defining_Entity (N)), "GH")),
2659 Name => Make_Identifier (Loc, Chars (Defining_Entity (N))));
2661 if Present (Decls) then
2662 Decl := First (Decls);
2663 while Present (Decl)
2664 and then Nkind (Decl) = N_Pragma
2669 if Present (Decl) then
2670 Insert_Before (Decl, Renaming);
2672 Append (Renaming, Visible_Declarations (Specification (N)));
2676 Set_Visible_Declarations (Specification (N), New_List (Renaming));
2679 -- Create copy of generic unit, and save for instantiation. If the unit
2680 -- is a child unit, do not copy the specifications for the parent, which
2681 -- are not part of the generic tree.
2683 Save_Parent := Parent_Spec (N);
2684 Set_Parent_Spec (N, Empty);
2686 New_N := Copy_Generic_Node (N, Empty, Instantiating => False);
2687 Set_Parent_Spec (New_N, Save_Parent);
2689 Id := Defining_Entity (N);
2690 Generate_Definition (Id);
2692 -- Expansion is not applied to generic units
2697 Set_Ekind (Id, E_Generic_Package);
2698 Set_Etype (Id, Standard_Void_Type);
2700 Enter_Generic_Scope (Id);
2701 Set_Inner_Instances (Id, New_Elmt_List);
2703 Set_Categorization_From_Pragmas (N);
2704 Set_Is_Pure (Id, Is_Pure (Current_Scope));
2706 -- Link the declaration of the generic homonym in the generic copy to
2707 -- the package it renames, so that it is always resolved properly.
2709 Set_Generic_Homonym (Id, Defining_Unit_Name (Renaming));
2710 Set_Entity (Associated_Node (Name (Renaming)), Id);
2712 -- For a library unit, we have reconstructed the entity for the unit,
2713 -- and must reset it in the library tables.
2715 if Nkind (Parent (N)) = N_Compilation_Unit then
2716 Set_Cunit_Entity (Current_Sem_Unit, Id);
2719 Analyze_Generic_Formal_Part (N);
2721 -- After processing the generic formals, analysis proceeds as for a
2722 -- non-generic package.
2724 Analyze (Specification (N));
2726 Validate_Categorization_Dependency (N, Id);
2730 End_Package_Scope (Id);
2731 Exit_Generic_Scope (Id);
2733 if Nkind (Parent (N)) /= N_Compilation_Unit then
2734 Move_Freeze_Nodes (Id, N, Visible_Declarations (Specification (N)));
2735 Move_Freeze_Nodes (Id, N, Private_Declarations (Specification (N)));
2736 Move_Freeze_Nodes (Id, N, Generic_Formal_Declarations (N));
2739 Set_Body_Required (Parent (N), Unit_Requires_Body (Id));
2740 Validate_RT_RAT_Component (N);
2742 -- If this is a spec without a body, check that generic parameters
2745 if not Body_Required (Parent (N)) then
2746 Check_References (Id);
2749 end Analyze_Generic_Package_Declaration;
2751 --------------------------------------------
2752 -- Analyze_Generic_Subprogram_Declaration --
2753 --------------------------------------------
2755 procedure Analyze_Generic_Subprogram_Declaration (N : Node_Id) is
2760 Result_Type : Entity_Id;
2761 Save_Parent : Node_Id;
2765 -- Create copy of generic unit, and save for instantiation. If the unit
2766 -- is a child unit, do not copy the specifications for the parent, which
2767 -- are not part of the generic tree.
2769 Save_Parent := Parent_Spec (N);
2770 Set_Parent_Spec (N, Empty);
2772 New_N := Copy_Generic_Node (N, Empty, Instantiating => False);
2773 Set_Parent_Spec (New_N, Save_Parent);
2776 Spec := Specification (N);
2777 Id := Defining_Entity (Spec);
2778 Generate_Definition (Id);
2780 if Nkind (Id) = N_Defining_Operator_Symbol then
2782 ("operator symbol not allowed for generic subprogram", Id);
2789 Set_Scope_Depth_Value (Id, Scope_Depth (Current_Scope) + 1);
2791 Enter_Generic_Scope (Id);
2792 Set_Inner_Instances (Id, New_Elmt_List);
2793 Set_Is_Pure (Id, Is_Pure (Current_Scope));
2795 Analyze_Generic_Formal_Part (N);
2797 Formals := Parameter_Specifications (Spec);
2799 if Present (Formals) then
2800 Process_Formals (Formals, Spec);
2803 if Nkind (Spec) = N_Function_Specification then
2804 Set_Ekind (Id, E_Generic_Function);
2806 if Nkind (Result_Definition (Spec)) = N_Access_Definition then
2807 Result_Type := Access_Definition (Spec, Result_Definition (Spec));
2808 Set_Etype (Id, Result_Type);
2810 Find_Type (Result_Definition (Spec));
2811 Typ := Entity (Result_Definition (Spec));
2813 -- If a null exclusion is imposed on the result type, then create
2814 -- a null-excluding itype (an access subtype) and use it as the
2815 -- function's Etype.
2817 if Is_Access_Type (Typ)
2818 and then Null_Exclusion_Present (Spec)
2821 Create_Null_Excluding_Itype
2823 Related_Nod => Spec,
2824 Scope_Id => Defining_Unit_Name (Spec)));
2826 Set_Etype (Id, Typ);
2831 Set_Ekind (Id, E_Generic_Procedure);
2832 Set_Etype (Id, Standard_Void_Type);
2835 -- For a library unit, we have reconstructed the entity for the unit,
2836 -- and must reset it in the library tables. We also make sure that
2837 -- Body_Required is set properly in the original compilation unit node.
2839 if Nkind (Parent (N)) = N_Compilation_Unit then
2840 Set_Cunit_Entity (Current_Sem_Unit, Id);
2841 Set_Body_Required (Parent (N), Unit_Requires_Body (Id));
2844 Set_Categorization_From_Pragmas (N);
2845 Validate_Categorization_Dependency (N, Id);
2847 Save_Global_References (Original_Node (N));
2851 Exit_Generic_Scope (Id);
2852 Generate_Reference_To_Formals (Id);
2853 end Analyze_Generic_Subprogram_Declaration;
2855 -----------------------------------
2856 -- Analyze_Package_Instantiation --
2857 -----------------------------------
2859 procedure Analyze_Package_Instantiation (N : Node_Id) is
2860 Loc : constant Source_Ptr := Sloc (N);
2861 Gen_Id : constant Node_Id := Name (N);
2864 Act_Decl_Name : Node_Id;
2865 Act_Decl_Id : Entity_Id;
2870 Gen_Unit : Entity_Id;
2872 Is_Actual_Pack : constant Boolean :=
2873 Is_Internal (Defining_Entity (N));
2875 Env_Installed : Boolean := False;
2876 Parent_Installed : Boolean := False;
2877 Renaming_List : List_Id;
2878 Unit_Renaming : Node_Id;
2879 Needs_Body : Boolean;
2880 Inline_Now : Boolean := False;
2882 procedure Delay_Descriptors (E : Entity_Id);
2883 -- Delay generation of subprogram descriptors for given entity
2885 function Might_Inline_Subp return Boolean;
2886 -- If inlining is active and the generic contains inlined subprograms,
2887 -- we instantiate the body. This may cause superfluous instantiations,
2888 -- but it is simpler than detecting the need for the body at the point
2889 -- of inlining, when the context of the instance is not available.
2891 -----------------------
2892 -- Delay_Descriptors --
2893 -----------------------
2895 procedure Delay_Descriptors (E : Entity_Id) is
2897 if not Delay_Subprogram_Descriptors (E) then
2898 Set_Delay_Subprogram_Descriptors (E);
2899 Pending_Descriptor.Append (E);
2901 end Delay_Descriptors;
2903 -----------------------
2904 -- Might_Inline_Subp --
2905 -----------------------
2907 function Might_Inline_Subp return Boolean is
2911 if not Inline_Processing_Required then
2915 E := First_Entity (Gen_Unit);
2916 while Present (E) loop
2917 if Is_Subprogram (E)
2918 and then Is_Inlined (E)
2928 end Might_Inline_Subp;
2930 -- Start of processing for Analyze_Package_Instantiation
2933 -- Very first thing: apply the special kludge for Text_IO processing
2934 -- in case we are instantiating one of the children of [Wide_]Text_IO.
2936 Text_IO_Kludge (Name (N));
2938 -- Make node global for error reporting
2940 Instantiation_Node := N;
2942 -- Case of instantiation of a generic package
2944 if Nkind (N) = N_Package_Instantiation then
2945 Act_Decl_Id := New_Copy (Defining_Entity (N));
2946 Set_Comes_From_Source (Act_Decl_Id, True);
2948 if Nkind (Defining_Unit_Name (N)) = N_Defining_Program_Unit_Name then
2950 Make_Defining_Program_Unit_Name (Loc,
2951 Name => New_Copy_Tree (Name (Defining_Unit_Name (N))),
2952 Defining_Identifier => Act_Decl_Id);
2954 Act_Decl_Name := Act_Decl_Id;
2957 -- Case of instantiation of a formal package
2960 Act_Decl_Id := Defining_Identifier (N);
2961 Act_Decl_Name := Act_Decl_Id;
2964 Generate_Definition (Act_Decl_Id);
2965 Preanalyze_Actuals (N);
2968 Env_Installed := True;
2970 -- Reset renaming map for formal types. The mapping is established
2971 -- when analyzing the generic associations, but some mappings are
2972 -- inherited from formal packages of parent units, and these are
2973 -- constructed when the parents are installed.
2975 Generic_Renamings.Set_Last (0);
2976 Generic_Renamings_HTable.Reset;
2978 Check_Generic_Child_Unit (Gen_Id, Parent_Installed);
2979 Gen_Unit := Entity (Gen_Id);
2981 -- Verify that it is the name of a generic package
2983 -- A visibility glitch: if the instance is a child unit and the generic
2984 -- is the generic unit of a parent instance (i.e. both the parent and
2985 -- the child units are instances of the same package) the name now
2986 -- denotes the renaming within the parent, not the intended generic
2987 -- unit. See if there is a homonym that is the desired generic. The
2988 -- renaming declaration must be visible inside the instance of the
2989 -- child, but not when analyzing the name in the instantiation itself.
2991 if Ekind (Gen_Unit) = E_Package
2992 and then Present (Renamed_Entity (Gen_Unit))
2993 and then In_Open_Scopes (Renamed_Entity (Gen_Unit))
2994 and then Is_Generic_Instance (Renamed_Entity (Gen_Unit))
2995 and then Present (Homonym (Gen_Unit))
2997 Gen_Unit := Homonym (Gen_Unit);
3000 if Etype (Gen_Unit) = Any_Type then
3004 elsif Ekind (Gen_Unit) /= E_Generic_Package then
3006 -- Ada 2005 (AI-50217): Cannot use instance in limited with_clause
3008 if From_With_Type (Gen_Unit) then
3010 ("cannot instantiate a limited withed package", Gen_Id);
3013 ("expect name of generic package in instantiation", Gen_Id);
3020 if In_Extended_Main_Source_Unit (N) then
3021 Set_Is_Instantiated (Gen_Unit);
3022 Generate_Reference (Gen_Unit, N);
3024 if Present (Renamed_Object (Gen_Unit)) then
3025 Set_Is_Instantiated (Renamed_Object (Gen_Unit));
3026 Generate_Reference (Renamed_Object (Gen_Unit), N);
3030 if Nkind (Gen_Id) = N_Identifier
3031 and then Chars (Gen_Unit) = Chars (Defining_Entity (N))
3034 ("& is hidden within declaration of instance", Gen_Id, Gen_Unit);
3036 elsif Nkind (Gen_Id) = N_Expanded_Name
3037 and then Is_Child_Unit (Gen_Unit)
3038 and then Nkind (Prefix (Gen_Id)) = N_Identifier
3039 and then Chars (Act_Decl_Id) = Chars (Prefix (Gen_Id))
3042 ("& is hidden within declaration of instance ", Prefix (Gen_Id));
3045 Set_Entity (Gen_Id, Gen_Unit);
3047 -- If generic is a renaming, get original generic unit
3049 if Present (Renamed_Object (Gen_Unit))
3050 and then Ekind (Renamed_Object (Gen_Unit)) = E_Generic_Package
3052 Gen_Unit := Renamed_Object (Gen_Unit);
3055 -- Verify that there are no circular instantiations
3057 if In_Open_Scopes (Gen_Unit) then
3058 Error_Msg_NE ("instantiation of & within itself", N, Gen_Unit);
3062 elsif Contains_Instance_Of (Gen_Unit, Current_Scope, Gen_Id) then
3063 Error_Msg_Node_2 := Current_Scope;
3065 ("circular Instantiation: & instantiated in &!", N, Gen_Unit);
3066 Circularity_Detected := True;
3071 Gen_Decl := Unit_Declaration_Node (Gen_Unit);
3073 -- Initialize renamings map, for error checking, and the list that
3074 -- holds private entities whose views have changed between generic
3075 -- definition and instantiation. If this is the instance created to
3076 -- validate an actual package, the instantiation environment is that
3077 -- of the enclosing instance.
3079 Create_Instantiation_Source (N, Gen_Unit, False, S_Adjustment);
3081 -- Copy original generic tree, to produce text for instantiation
3085 (Original_Node (Gen_Decl), Empty, Instantiating => True);
3087 Act_Spec := Specification (Act_Tree);
3089 -- If this is the instance created to validate an actual package,
3090 -- only the formals matter, do not examine the package spec itself.
3092 if Is_Actual_Pack then
3093 Set_Visible_Declarations (Act_Spec, New_List);
3094 Set_Private_Declarations (Act_Spec, New_List);
3098 Analyze_Associations
3100 Generic_Formal_Declarations (Act_Tree),
3101 Generic_Formal_Declarations (Gen_Decl));
3103 Set_Instance_Env (Gen_Unit, Act_Decl_Id);
3104 Set_Defining_Unit_Name (Act_Spec, Act_Decl_Name);
3105 Set_Is_Generic_Instance (Act_Decl_Id);
3107 Set_Generic_Parent (Act_Spec, Gen_Unit);
3109 -- References to the generic in its own declaration or its body are
3110 -- references to the instance. Add a renaming declaration for the
3111 -- generic unit itself. This declaration, as well as the renaming
3112 -- declarations for the generic formals, must remain private to the
3113 -- unit: the formals, because this is the language semantics, and
3114 -- the unit because its use is an artifact of the implementation.
3117 Make_Package_Renaming_Declaration (Loc,
3118 Defining_Unit_Name =>
3119 Make_Defining_Identifier (Loc, Chars (Gen_Unit)),
3120 Name => New_Reference_To (Act_Decl_Id, Loc));
3122 Append (Unit_Renaming, Renaming_List);
3124 -- The renaming declarations are the first local declarations of
3127 if Is_Non_Empty_List (Visible_Declarations (Act_Spec)) then
3129 (First (Visible_Declarations (Act_Spec)), Renaming_List);
3131 Set_Visible_Declarations (Act_Spec, Renaming_List);
3135 Make_Package_Declaration (Loc,
3136 Specification => Act_Spec);
3138 -- Save the instantiation node, for subsequent instantiation of the
3139 -- body, if there is one and we are generating code for the current
3140 -- unit. Mark the unit as having a body, to avoid a premature error
3143 -- We instantiate the body if we are generating code, if we are
3144 -- generating cross-reference information, or if we are building
3145 -- trees for ASIS use.
3148 Enclosing_Body_Present : Boolean := False;
3149 -- If the generic unit is not a compilation unit, then a body may
3150 -- be present in its parent even if none is required. We create a
3151 -- tentative pending instantiation for the body, which will be
3152 -- discarded if none is actually present.
3157 if Scope (Gen_Unit) /= Standard_Standard
3158 and then not Is_Child_Unit (Gen_Unit)
3160 Scop := Scope (Gen_Unit);
3162 while Present (Scop)
3163 and then Scop /= Standard_Standard
3165 if Unit_Requires_Body (Scop) then
3166 Enclosing_Body_Present := True;
3169 elsif In_Open_Scopes (Scop)
3170 and then In_Package_Body (Scop)
3172 Enclosing_Body_Present := True;
3176 exit when Is_Compilation_Unit (Scop);
3177 Scop := Scope (Scop);
3181 -- If front-end inlining is enabled, and this is a unit for which
3182 -- code will be generated, we instantiate the body at once.
3184 -- This is done if the instance is not the main unit, and if the
3185 -- generic is not a child unit of another generic, to avoid scope
3186 -- problems and the reinstallation of parent instances.
3189 and then (not Is_Child_Unit (Gen_Unit)
3190 or else not Is_Generic_Unit (Scope (Gen_Unit)))
3191 and then Might_Inline_Subp
3192 and then not Is_Actual_Pack
3194 if Front_End_Inlining
3195 and then (Is_In_Main_Unit (N)
3196 or else In_Main_Context (Current_Scope))
3197 and then Nkind (Parent (N)) /= N_Compilation_Unit
3201 -- In configurable_run_time mode we force the inlining of
3202 -- predefined subprograms marked Inline_Always, to minimize
3203 -- the use of the run-time library.
3205 elsif Is_Predefined_File_Name
3206 (Unit_File_Name (Get_Source_Unit (Gen_Decl)))
3207 and then Configurable_Run_Time_Mode
3208 and then Nkind (Parent (N)) /= N_Compilation_Unit
3213 -- If the current scope is itself an instance within a child
3214 -- unit, there will be duplications in the scope stack, and the
3215 -- unstacking mechanism in Inline_Instance_Body will fail.
3216 -- This loses some rare cases of optimization, and might be
3217 -- improved some day, if we can find a proper abstraction for
3218 -- "the complete compilation context" that can be saved and
3221 if Is_Generic_Instance (Current_Scope) then
3223 Curr_Unit : constant Entity_Id :=
3224 Cunit_Entity (Current_Sem_Unit);
3226 if Curr_Unit /= Current_Scope
3227 and then Is_Child_Unit (Curr_Unit)
3229 Inline_Now := False;
3236 (Unit_Requires_Body (Gen_Unit)
3237 or else Enclosing_Body_Present
3238 or else Present (Corresponding_Body (Gen_Decl)))
3239 and then (Is_In_Main_Unit (N)
3240 or else Might_Inline_Subp)
3241 and then not Is_Actual_Pack
3242 and then not Inline_Now
3243 and then (Operating_Mode = Generate_Code
3244 or else (Operating_Mode = Check_Semantics
3245 and then ASIS_Mode));
3247 -- If front_end_inlining is enabled, do not instantiate body if
3248 -- within a generic context.
3250 if (Front_End_Inlining
3251 and then not Expander_Active)
3252 or else Is_Generic_Unit (Cunit_Entity (Main_Unit))
3254 Needs_Body := False;
3257 -- If the current context is generic, and the package being
3258 -- instantiated is declared within a formal package, there is no
3259 -- body to instantiate until the enclosing generic is instantiated
3260 -- and there is an actual for the formal package. If the formal
3261 -- package has parameters, we build a regular package instance for
3262 -- it, that precedes the original formal package declaration.
3264 if In_Open_Scopes (Scope (Scope (Gen_Unit))) then
3266 Decl : constant Node_Id :=
3268 (Unit_Declaration_Node (Scope (Gen_Unit)));
3270 if Nkind (Decl) = N_Formal_Package_Declaration
3271 or else (Nkind (Decl) = N_Package_Declaration
3272 and then Is_List_Member (Decl)
3273 and then Present (Next (Decl))
3275 Nkind (Next (Decl)) =
3276 N_Formal_Package_Declaration)
3278 Needs_Body := False;
3284 -- If we are generating the calling stubs from the instantiation of
3285 -- a generic RCI package, we will not use the body of the generic
3288 if Distribution_Stub_Mode = Generate_Caller_Stub_Body
3289 and then Is_Compilation_Unit (Defining_Entity (N))
3291 Needs_Body := False;
3296 -- Here is a defence against a ludicrous number of instantiations
3297 -- caused by a circular set of instantiation attempts.
3299 if Pending_Instantiations.Last >
3300 Hostparm.Max_Instantiations
3302 Error_Msg_N ("too many instantiations", N);
3303 raise Unrecoverable_Error;
3306 -- Indicate that the enclosing scopes contain an instantiation,
3307 -- and that cleanup actions should be delayed until after the
3308 -- instance body is expanded.
3310 Check_Forward_Instantiation (Gen_Decl);
3311 if Nkind (N) = N_Package_Instantiation then
3313 Enclosing_Master : Entity_Id;
3316 -- Loop to search enclosing masters
3318 Enclosing_Master := Current_Scope;
3319 Scope_Loop : while Enclosing_Master /= Standard_Standard loop
3320 if Ekind (Enclosing_Master) = E_Package then
3321 if Is_Compilation_Unit (Enclosing_Master) then
3322 if In_Package_Body (Enclosing_Master) then
3324 (Body_Entity (Enclosing_Master));
3333 Enclosing_Master := Scope (Enclosing_Master);
3336 elsif Ekind (Enclosing_Master) = E_Generic_Package then
3337 Enclosing_Master := Scope (Enclosing_Master);
3339 elsif Is_Generic_Subprogram (Enclosing_Master)
3340 or else Ekind (Enclosing_Master) = E_Void
3342 -- Cleanup actions will eventually be performed on the
3343 -- enclosing instance, if any. Enclosing scope is void
3344 -- in the formal part of a generic subprogram.
3349 if Ekind (Enclosing_Master) = E_Entry
3351 Ekind (Scope (Enclosing_Master)) = E_Protected_Type
3353 if not Expander_Active then
3357 Protected_Body_Subprogram (Enclosing_Master);
3361 Set_Delay_Cleanups (Enclosing_Master);
3363 while Ekind (Enclosing_Master) = E_Block loop
3364 Enclosing_Master := Scope (Enclosing_Master);
3367 if Is_Subprogram (Enclosing_Master) then
3368 Delay_Descriptors (Enclosing_Master);
3370 elsif Is_Task_Type (Enclosing_Master) then
3372 TBP : constant Node_Id :=
3373 Get_Task_Body_Procedure
3376 if Present (TBP) then
3377 Delay_Descriptors (TBP);
3378 Set_Delay_Cleanups (TBP);
3385 end loop Scope_Loop;
3388 -- Make entry in table
3390 Pending_Instantiations.Append
3392 Act_Decl => Act_Decl,
3393 Expander_Status => Expander_Active,
3394 Current_Sem_Unit => Current_Sem_Unit,
3395 Scope_Suppress => Scope_Suppress,
3396 Local_Suppress_Stack_Top => Local_Suppress_Stack_Top));
3400 Set_Categorization_From_Pragmas (Act_Decl);
3402 if Parent_Installed then
3406 Set_Instance_Spec (N, Act_Decl);
3408 -- If not a compilation unit, insert the package declaration before
3409 -- the original instantiation node.
3411 if Nkind (Parent (N)) /= N_Compilation_Unit then
3412 Mark_Rewrite_Insertion (Act_Decl);
3413 Insert_Before (N, Act_Decl);
3416 -- For an instantiation that is a compilation unit, place declaration
3417 -- on current node so context is complete for analysis (including
3418 -- nested instantiations). If this is the main unit, the declaration
3419 -- eventually replaces the instantiation node. If the instance body
3420 -- is created later, it replaces the instance node, and the
3421 -- declaration is attached to it (see
3422 -- Build_Instance_Compilation_Unit_Nodes).
3425 if Cunit_Entity (Current_Sem_Unit) = Defining_Entity (N) then
3427 -- The entity for the current unit is the newly created one,
3428 -- and all semantic information is attached to it.
3430 Set_Cunit_Entity (Current_Sem_Unit, Act_Decl_Id);
3432 -- If this is the main unit, replace the main entity as well
3434 if Current_Sem_Unit = Main_Unit then
3435 Main_Unit_Entity := Act_Decl_Id;
3439 Set_Unit (Parent (N), Act_Decl);
3440 Set_Parent_Spec (Act_Decl, Parent_Spec (N));
3441 Set_Package_Instantiation (Act_Decl_Id, N);
3443 Set_Unit (Parent (N), N);
3444 Set_Body_Required (Parent (N), False);
3446 -- We never need elaboration checks on instantiations, since by
3447 -- definition, the body instantiation is elaborated at the same
3448 -- time as the spec instantiation.
3450 Set_Suppress_Elaboration_Warnings (Act_Decl_Id);
3451 Set_Kill_Elaboration_Checks (Act_Decl_Id);
3454 Check_Elab_Instantiation (N);
3456 if ABE_Is_Certain (N) and then Needs_Body then
3457 Pending_Instantiations.Decrement_Last;
3460 Check_Hidden_Child_Unit (N, Gen_Unit, Act_Decl_Id);
3462 Set_First_Private_Entity (Defining_Unit_Name (Unit_Renaming),
3463 First_Private_Entity (Act_Decl_Id));
3465 -- If the instantiation will receive a body, the unit will be
3466 -- transformed into a package body, and receive its own elaboration
3467 -- entity. Otherwise, the nature of the unit is now a package
3470 if Nkind (Parent (N)) = N_Compilation_Unit
3471 and then not Needs_Body
3473 Rewrite (N, Act_Decl);
3476 if Present (Corresponding_Body (Gen_Decl))
3477 or else Unit_Requires_Body (Gen_Unit)
3479 Set_Has_Completion (Act_Decl_Id);
3482 Check_Formal_Packages (Act_Decl_Id);
3484 Restore_Private_Views (Act_Decl_Id);
3486 Inherit_Context (Gen_Decl, N);
3488 if Parent_Installed then
3493 Env_Installed := False;
3496 Validate_Categorization_Dependency (N, Act_Decl_Id);
3498 -- There used to be a check here to prevent instantiations in local
3499 -- contexts if the No_Local_Allocators restriction was active. This
3500 -- check was removed by a binding interpretation in AI-95-00130/07,
3501 -- but we retain the code for documentation purposes.
3503 -- if Ekind (Act_Decl_Id) /= E_Void
3504 -- and then not Is_Library_Level_Entity (Act_Decl_Id)
3506 -- Check_Restriction (No_Local_Allocators, N);
3510 Inline_Instance_Body (N, Gen_Unit, Act_Decl);
3513 -- The following is a tree patch for ASIS: ASIS needs separate nodes to
3514 -- be used as defining identifiers for a formal package and for the
3515 -- corresponding expanded package.
3517 if Nkind (N) = N_Formal_Package_Declaration then
3518 Act_Decl_Id := New_Copy (Defining_Entity (N));
3519 Set_Comes_From_Source (Act_Decl_Id, True);
3520 Set_Is_Generic_Instance (Act_Decl_Id, False);
3521 Set_Defining_Identifier (N, Act_Decl_Id);
3525 when Instantiation_Error =>
3526 if Parent_Installed then
3530 if Env_Installed then
3533 end Analyze_Package_Instantiation;
3535 --------------------------
3536 -- Inline_Instance_Body --
3537 --------------------------
3539 procedure Inline_Instance_Body
3541 Gen_Unit : Entity_Id;
3545 Gen_Comp : constant Entity_Id :=
3546 Cunit_Entity (Get_Source_Unit (Gen_Unit));
3547 Curr_Comp : constant Node_Id := Cunit (Current_Sem_Unit);
3548 Curr_Scope : Entity_Id := Empty;
3549 Curr_Unit : constant Entity_Id :=
3550 Cunit_Entity (Current_Sem_Unit);
3551 Removed : Boolean := False;
3552 Num_Scopes : Int := 0;
3554 Scope_Stack_Depth : constant Int :=
3555 Scope_Stack.Last - Scope_Stack.First + 1;
3557 Use_Clauses : array (1 .. Scope_Stack_Depth) of Node_Id;
3558 Instances : array (1 .. Scope_Stack_Depth) of Entity_Id;
3559 Inner_Scopes : array (1 .. Scope_Stack_Depth) of Entity_Id;
3560 Num_Inner : Int := 0;
3561 N_Instances : Int := 0;
3565 -- Case of generic unit defined in another unit. We must remove the
3566 -- complete context of the current unit to install that of the generic.
3568 if Gen_Comp /= Cunit_Entity (Current_Sem_Unit) then
3570 -- Add some comments for the following two loops ???
3573 while Present (S) and then S /= Standard_Standard loop
3575 Num_Scopes := Num_Scopes + 1;
3577 Use_Clauses (Num_Scopes) :=
3579 (Scope_Stack.Last - Num_Scopes + 1).
3581 End_Use_Clauses (Use_Clauses (Num_Scopes));
3583 exit when Scope_Stack.Last - Num_Scopes + 1 = Scope_Stack.First
3584 or else Scope_Stack.Table
3585 (Scope_Stack.Last - Num_Scopes).Entity
3589 exit when Is_Generic_Instance (S)
3590 and then (In_Package_Body (S)
3591 or else Ekind (S) = E_Procedure
3592 or else Ekind (S) = E_Function);
3596 Vis := Is_Immediately_Visible (Gen_Comp);
3598 -- Find and save all enclosing instances
3603 and then S /= Standard_Standard
3605 if Is_Generic_Instance (S) then
3606 N_Instances := N_Instances + 1;
3607 Instances (N_Instances) := S;
3609 exit when In_Package_Body (S);
3615 -- Remove context of current compilation unit, unless we are within a
3616 -- nested package instantiation, in which case the context has been
3617 -- removed previously.
3619 -- If current scope is the body of a child unit, remove context of
3620 -- spec as well. If an enclosing scope is an instance body, the
3621 -- context has already been removed, but the entities in the body
3622 -- must be made invisible as well.
3627 and then S /= Standard_Standard
3629 if Is_Generic_Instance (S)
3630 and then (In_Package_Body (S)
3631 or else Ekind (S) = E_Procedure
3632 or else Ekind (S) = E_Function)
3634 -- We still have to remove the entities of the enclosing
3635 -- instance from direct visibility.
3640 E := First_Entity (S);
3641 while Present (E) loop
3642 Set_Is_Immediately_Visible (E, False);
3651 or else (Ekind (Curr_Unit) = E_Package_Body
3652 and then S = Spec_Entity (Curr_Unit))
3653 or else (Ekind (Curr_Unit) = E_Subprogram_Body
3656 (Unit_Declaration_Node (Curr_Unit)))
3660 -- Remove entities in current scopes from visibility, so that
3661 -- instance body is compiled in a clean environment.
3663 Save_Scope_Stack (Handle_Use => False);
3665 if Is_Child_Unit (S) then
3667 -- Remove child unit from stack, as well as inner scopes.
3668 -- Removing the context of a child unit removes parent units
3671 while Current_Scope /= S loop
3672 Num_Inner := Num_Inner + 1;
3673 Inner_Scopes (Num_Inner) := Current_Scope;
3678 Remove_Context (Curr_Comp);
3682 Remove_Context (Curr_Comp);
3685 if Ekind (Curr_Unit) = E_Package_Body then
3686 Remove_Context (Library_Unit (Curr_Comp));
3692 pragma Assert (Num_Inner < Num_Scopes);
3694 Push_Scope (Standard_Standard);
3695 Scope_Stack.Table (Scope_Stack.Last).Is_Active_Stack_Base := True;
3696 Instantiate_Package_Body
3699 Act_Decl => Act_Decl,
3700 Expander_Status => Expander_Active,
3701 Current_Sem_Unit => Current_Sem_Unit,
3702 Scope_Suppress => Scope_Suppress,
3703 Local_Suppress_Stack_Top => Local_Suppress_Stack_Top)),
3704 Inlined_Body => True);
3710 Set_Is_Immediately_Visible (Gen_Comp, Vis);
3712 -- Reset Generic_Instance flag so that use clauses can be installed
3713 -- in the proper order. (See Use_One_Package for effect of enclosing
3714 -- instances on processing of use clauses).
3716 for J in 1 .. N_Instances loop
3717 Set_Is_Generic_Instance (Instances (J), False);
3721 Install_Context (Curr_Comp);
3723 if Present (Curr_Scope)
3724 and then Is_Child_Unit (Curr_Scope)
3726 Push_Scope (Curr_Scope);
3727 Set_Is_Immediately_Visible (Curr_Scope);
3729 -- Finally, restore inner scopes as well
3731 for J in reverse 1 .. Num_Inner loop
3732 Push_Scope (Inner_Scopes (J));
3736 Restore_Scope_Stack (Handle_Use => False);
3738 if Present (Curr_Scope)
3740 (In_Private_Part (Curr_Scope)
3741 or else In_Package_Body (Curr_Scope))
3743 -- Install private declaration of ancestor units, which are
3744 -- currently available. Restore_Scope_Stack and Install_Context
3745 -- only install the visible part of parents.
3750 Par := Scope (Curr_Scope);
3751 while (Present (Par))
3752 and then Par /= Standard_Standard
3754 Install_Private_Declarations (Par);
3761 -- Restore use clauses. For a child unit, use clauses in the parents
3762 -- are restored when installing the context, so only those in inner
3763 -- scopes (and those local to the child unit itself) need to be
3764 -- installed explicitly.
3766 if Is_Child_Unit (Curr_Unit)
3769 for J in reverse 1 .. Num_Inner + 1 loop
3770 Scope_Stack.Table (Scope_Stack.Last - J + 1).First_Use_Clause :=
3772 Install_Use_Clauses (Use_Clauses (J));
3776 for J in reverse 1 .. Num_Scopes loop
3777 Scope_Stack.Table (Scope_Stack.Last - J + 1).First_Use_Clause :=
3779 Install_Use_Clauses (Use_Clauses (J));
3783 -- Restore status of instances. If one of them is a body, make
3784 -- its local entities visible again.
3791 for J in 1 .. N_Instances loop
3792 Inst := Instances (J);
3793 Set_Is_Generic_Instance (Inst, True);
3795 if In_Package_Body (Inst)
3796 or else Ekind (S) = E_Procedure
3797 or else Ekind (S) = E_Function
3799 E := First_Entity (Instances (J));
3800 while Present (E) loop
3801 Set_Is_Immediately_Visible (E);
3808 -- If generic unit is in current unit, current context is correct
3811 Instantiate_Package_Body
3814 Act_Decl => Act_Decl,
3815 Expander_Status => Expander_Active,
3816 Current_Sem_Unit => Current_Sem_Unit,
3817 Scope_Suppress => Scope_Suppress,
3818 Local_Suppress_Stack_Top => Local_Suppress_Stack_Top)),
3819 Inlined_Body => True);
3821 end Inline_Instance_Body;
3823 -------------------------------------
3824 -- Analyze_Procedure_Instantiation --
3825 -------------------------------------
3827 procedure Analyze_Procedure_Instantiation (N : Node_Id) is
3829 Analyze_Subprogram_Instantiation (N, E_Procedure);
3830 end Analyze_Procedure_Instantiation;
3832 -----------------------------------
3833 -- Need_Subprogram_Instance_Body --
3834 -----------------------------------
3836 function Need_Subprogram_Instance_Body
3838 Subp : Entity_Id) return Boolean
3841 if (Is_In_Main_Unit (N)
3842 or else Is_Inlined (Subp)
3843 or else Is_Inlined (Alias (Subp)))
3844 and then (Operating_Mode = Generate_Code
3845 or else (Operating_Mode = Check_Semantics
3846 and then ASIS_Mode))
3847 and then (Expander_Active or else ASIS_Mode)
3848 and then not ABE_Is_Certain (N)
3849 and then not Is_Eliminated (Subp)
3851 Pending_Instantiations.Append
3853 Act_Decl => Unit_Declaration_Node (Subp),
3854 Expander_Status => Expander_Active,
3855 Current_Sem_Unit => Current_Sem_Unit,
3856 Scope_Suppress => Scope_Suppress,
3857 Local_Suppress_Stack_Top => Local_Suppress_Stack_Top));
3862 end Need_Subprogram_Instance_Body;
3864 --------------------------------------
3865 -- Analyze_Subprogram_Instantiation --
3866 --------------------------------------
3868 procedure Analyze_Subprogram_Instantiation
3872 Loc : constant Source_Ptr := Sloc (N);
3873 Gen_Id : constant Node_Id := Name (N);
3875 Anon_Id : constant Entity_Id :=
3876 Make_Defining_Identifier (Sloc (Defining_Entity (N)),
3877 Chars => New_External_Name
3878 (Chars (Defining_Entity (N)), 'R'));
3880 Act_Decl_Id : Entity_Id;
3885 Env_Installed : Boolean := False;
3886 Gen_Unit : Entity_Id;
3888 Pack_Id : Entity_Id;
3889 Parent_Installed : Boolean := False;
3890 Renaming_List : List_Id;
3892 procedure Analyze_Instance_And_Renamings;
3893 -- The instance must be analyzed in a context that includes the mappings
3894 -- of generic parameters into actuals. We create a package declaration
3895 -- for this purpose, and a subprogram with an internal name within the
3896 -- package. The subprogram instance is simply an alias for the internal
3897 -- subprogram, declared in the current scope.
3899 ------------------------------------
3900 -- Analyze_Instance_And_Renamings --
3901 ------------------------------------
3903 procedure Analyze_Instance_And_Renamings is
3904 Def_Ent : constant Entity_Id := Defining_Entity (N);
3905 Pack_Decl : Node_Id;
3908 if Nkind (Parent (N)) = N_Compilation_Unit then
3910 -- For the case of a compilation unit, the container package has
3911 -- the same name as the instantiation, to insure that the binder
3912 -- calls the elaboration procedure with the right name. Copy the
3913 -- entity of the instance, which may have compilation level flags
3914 -- (e.g. Is_Child_Unit) set.
3916 Pack_Id := New_Copy (Def_Ent);
3919 -- Otherwise we use the name of the instantiation concatenated
3920 -- with its source position to ensure uniqueness if there are
3921 -- several instantiations with the same name.
3924 Make_Defining_Identifier (Loc,
3925 Chars => New_External_Name
3926 (Related_Id => Chars (Def_Ent),
3928 Suffix_Index => Source_Offset (Sloc (Def_Ent))));
3931 Pack_Decl := Make_Package_Declaration (Loc,
3932 Specification => Make_Package_Specification (Loc,
3933 Defining_Unit_Name => Pack_Id,
3934 Visible_Declarations => Renaming_List,
3935 End_Label => Empty));
3937 Set_Instance_Spec (N, Pack_Decl);
3938 Set_Is_Generic_Instance (Pack_Id);
3939 Set_Debug_Info_Needed (Pack_Id);
3941 -- Case of not a compilation unit
3943 if Nkind (Parent (N)) /= N_Compilation_Unit then
3944 Mark_Rewrite_Insertion (Pack_Decl);
3945 Insert_Before (N, Pack_Decl);
3946 Set_Has_Completion (Pack_Id);
3948 -- Case of an instantiation that is a compilation unit
3950 -- Place declaration on current node so context is complete for
3951 -- analysis (including nested instantiations), and for use in a
3952 -- context_clause (see Analyze_With_Clause).
3955 Set_Unit (Parent (N), Pack_Decl);
3956 Set_Parent_Spec (Pack_Decl, Parent_Spec (N));
3959 Analyze (Pack_Decl);
3960 Check_Formal_Packages (Pack_Id);
3961 Set_Is_Generic_Instance (Pack_Id, False);
3963 -- Body of the enclosing package is supplied when instantiating the
3964 -- subprogram body, after semantic analysis is completed.
3966 if Nkind (Parent (N)) = N_Compilation_Unit then
3968 -- Remove package itself from visibility, so it does not
3969 -- conflict with subprogram.
3971 Set_Name_Entity_Id (Chars (Pack_Id), Homonym (Pack_Id));
3973 -- Set name and scope of internal subprogram so that the proper
3974 -- external name will be generated. The proper scope is the scope
3975 -- of the wrapper package. We need to generate debugging info for
3976 -- the internal subprogram, so set flag accordingly.
3978 Set_Chars (Anon_Id, Chars (Defining_Entity (N)));
3979 Set_Scope (Anon_Id, Scope (Pack_Id));
3981 -- Mark wrapper package as referenced, to avoid spurious warnings
3982 -- if the instantiation appears in various with_ clauses of
3983 -- subunits of the main unit.
3985 Set_Referenced (Pack_Id);
3988 Set_Is_Generic_Instance (Anon_Id);
3989 Set_Debug_Info_Needed (Anon_Id);
3990 Act_Decl_Id := New_Copy (Anon_Id);
3992 Set_Parent (Act_Decl_Id, Parent (Anon_Id));
3993 Set_Chars (Act_Decl_Id, Chars (Defining_Entity (N)));
3994 Set_Sloc (Act_Decl_Id, Sloc (Defining_Entity (N)));
3995 Set_Comes_From_Source (Act_Decl_Id, True);
3997 -- The signature may involve types that are not frozen yet, but the
3998 -- subprogram will be frozen at the point the wrapper package is
3999 -- frozen, so it does not need its own freeze node. In fact, if one
4000 -- is created, it might conflict with the freezing actions from the
4003 Set_Has_Delayed_Freeze (Anon_Id, False);
4005 -- If the instance is a child unit, mark the Id accordingly. Mark
4006 -- the anonymous entity as well, which is the real subprogram and
4007 -- which is used when the instance appears in a context clause.
4009 Set_Is_Child_Unit (Act_Decl_Id, Is_Child_Unit (Defining_Entity (N)));
4010 Set_Is_Child_Unit (Anon_Id, Is_Child_Unit (Defining_Entity (N)));
4011 New_Overloaded_Entity (Act_Decl_Id);
4012 Check_Eliminated (Act_Decl_Id);
4014 -- In compilation unit case, kill elaboration checks on the
4015 -- instantiation, since they are never needed -- the body is
4016 -- instantiated at the same point as the spec.
4018 if Nkind (Parent (N)) = N_Compilation_Unit then
4019 Set_Suppress_Elaboration_Warnings (Act_Decl_Id);
4020 Set_Kill_Elaboration_Checks (Act_Decl_Id);
4021 Set_Is_Compilation_Unit (Anon_Id);
4023 Set_Cunit_Entity (Current_Sem_Unit, Pack_Id);
4026 -- The instance is not a freezing point for the new subprogram
4028 Set_Is_Frozen (Act_Decl_Id, False);
4030 if Nkind (Defining_Entity (N)) = N_Defining_Operator_Symbol then
4031 Valid_Operator_Definition (Act_Decl_Id);
4034 Set_Alias (Act_Decl_Id, Anon_Id);
4035 Set_Parent (Act_Decl_Id, Parent (Anon_Id));
4036 Set_Has_Completion (Act_Decl_Id);
4037 Set_Related_Instance (Pack_Id, Act_Decl_Id);
4039 if Nkind (Parent (N)) = N_Compilation_Unit then
4040 Set_Body_Required (Parent (N), False);
4042 end Analyze_Instance_And_Renamings;
4044 -- Start of processing for Analyze_Subprogram_Instantiation
4047 -- Very first thing: apply the special kludge for Text_IO processing
4048 -- in case we are instantiating one of the children of [Wide_]Text_IO.
4049 -- Of course such an instantiation is bogus (these are packages, not
4050 -- subprograms), but we get a better error message if we do this.
4052 Text_IO_Kludge (Gen_Id);
4054 -- Make node global for error reporting
4056 Instantiation_Node := N;
4057 Preanalyze_Actuals (N);
4060 Env_Installed := True;
4061 Check_Generic_Child_Unit (Gen_Id, Parent_Installed);
4062 Gen_Unit := Entity (Gen_Id);
4064 Generate_Reference (Gen_Unit, Gen_Id);
4066 if Nkind (Gen_Id) = N_Identifier
4067 and then Chars (Gen_Unit) = Chars (Defining_Entity (N))
4070 ("& is hidden within declaration of instance", Gen_Id, Gen_Unit);
4073 if Etype (Gen_Unit) = Any_Type then
4078 -- Verify that it is a generic subprogram of the right kind, and that
4079 -- it does not lead to a circular instantiation.
4081 if Ekind (Gen_Unit) /= E_Generic_Procedure
4082 and then Ekind (Gen_Unit) /= E_Generic_Function
4084 Error_Msg_N ("expect generic subprogram in instantiation", Gen_Id);
4086 elsif In_Open_Scopes (Gen_Unit) then
4087 Error_Msg_NE ("instantiation of & within itself", N, Gen_Unit);
4089 elsif K = E_Procedure
4090 and then Ekind (Gen_Unit) /= E_Generic_Procedure
4092 if Ekind (Gen_Unit) = E_Generic_Function then
4094 ("cannot instantiate generic function as procedure", Gen_Id);
4097 ("expect name of generic procedure in instantiation", Gen_Id);
4100 elsif K = E_Function
4101 and then Ekind (Gen_Unit) /= E_Generic_Function
4103 if Ekind (Gen_Unit) = E_Generic_Procedure then
4105 ("cannot instantiate generic procedure as function", Gen_Id);
4108 ("expect name of generic function in instantiation", Gen_Id);
4112 Set_Entity (Gen_Id, Gen_Unit);
4113 Set_Is_Instantiated (Gen_Unit);
4115 if In_Extended_Main_Source_Unit (N) then
4116 Generate_Reference (Gen_Unit, N);
4119 -- If renaming, get original unit
4121 if Present (Renamed_Object (Gen_Unit))
4122 and then (Ekind (Renamed_Object (Gen_Unit)) = E_Generic_Procedure
4124 Ekind (Renamed_Object (Gen_Unit)) = E_Generic_Function)
4126 Gen_Unit := Renamed_Object (Gen_Unit);
4127 Set_Is_Instantiated (Gen_Unit);
4128 Generate_Reference (Gen_Unit, N);
4131 if Contains_Instance_Of (Gen_Unit, Current_Scope, Gen_Id) then
4132 Error_Msg_Node_2 := Current_Scope;
4134 ("circular Instantiation: & instantiated in &!", N, Gen_Unit);
4135 Circularity_Detected := True;
4139 Gen_Decl := Unit_Declaration_Node (Gen_Unit);
4141 -- Initialize renamings map, for error checking
4143 Generic_Renamings.Set_Last (0);
4144 Generic_Renamings_HTable.Reset;
4146 Create_Instantiation_Source (N, Gen_Unit, False, S_Adjustment);
4148 -- Copy original generic tree, to produce text for instantiation
4152 (Original_Node (Gen_Decl), Empty, Instantiating => True);
4154 -- Inherit overriding indicator from instance node
4156 Act_Spec := Specification (Act_Tree);
4157 Set_Must_Override (Act_Spec, Must_Override (N));
4158 Set_Must_Not_Override (Act_Spec, Must_Not_Override (N));
4161 Analyze_Associations
4163 Generic_Formal_Declarations (Act_Tree),
4164 Generic_Formal_Declarations (Gen_Decl));
4166 -- The subprogram itself cannot contain a nested instance, so the
4167 -- current parent is left empty.
4169 Set_Instance_Env (Gen_Unit, Empty);
4171 -- Build the subprogram declaration, which does not appear in the
4172 -- generic template, and give it a sloc consistent with that of the
4175 Set_Defining_Unit_Name (Act_Spec, Anon_Id);
4176 Set_Generic_Parent (Act_Spec, Gen_Unit);
4178 Make_Subprogram_Declaration (Sloc (Act_Spec),
4179 Specification => Act_Spec);
4181 Set_Categorization_From_Pragmas (Act_Decl);
4183 if Parent_Installed then
4187 Append (Act_Decl, Renaming_List);
4188 Analyze_Instance_And_Renamings;
4190 -- If the generic is marked Import (Intrinsic), then so is the
4191 -- instance. This indicates that there is no body to instantiate. If
4192 -- generic is marked inline, so it the instance, and the anonymous
4193 -- subprogram it renames. If inlined, or else if inlining is enabled
4194 -- for the compilation, we generate the instance body even if it is
4195 -- not within the main unit.
4197 -- Any other pragmas might also be inherited ???
4199 if Is_Intrinsic_Subprogram (Gen_Unit) then
4200 Set_Is_Intrinsic_Subprogram (Anon_Id);
4201 Set_Is_Intrinsic_Subprogram (Act_Decl_Id);
4203 if Chars (Gen_Unit) = Name_Unchecked_Conversion then
4204 Validate_Unchecked_Conversion (N, Act_Decl_Id);
4208 Generate_Definition (Act_Decl_Id);
4210 Set_Is_Inlined (Act_Decl_Id, Is_Inlined (Gen_Unit));
4211 Set_Is_Inlined (Anon_Id, Is_Inlined (Gen_Unit));
4213 if not Is_Intrinsic_Subprogram (Gen_Unit) then
4214 Check_Elab_Instantiation (N);
4217 if Is_Dispatching_Operation (Act_Decl_Id)
4218 and then Ada_Version >= Ada_05
4224 Formal := First_Formal (Act_Decl_Id);
4225 while Present (Formal) loop
4226 if Ekind (Etype (Formal)) = E_Anonymous_Access_Type
4227 and then Is_Controlling_Formal (Formal)
4228 and then not Can_Never_Be_Null (Formal)
4230 Error_Msg_NE ("access parameter& is controlling,",
4232 Error_Msg_NE ("\corresponding parameter of & must be"
4233 & " explicitly null-excluding", N, Gen_Id);
4236 Next_Formal (Formal);
4241 Check_Hidden_Child_Unit (N, Gen_Unit, Act_Decl_Id);
4243 -- Subject to change, pending on if other pragmas are inherited ???
4245 Validate_Categorization_Dependency (N, Act_Decl_Id);
4247 if not Is_Intrinsic_Subprogram (Act_Decl_Id) then
4248 Inherit_Context (Gen_Decl, N);
4250 Restore_Private_Views (Pack_Id, False);
4252 -- If the context requires a full instantiation, mark node for
4253 -- subsequent construction of the body.
4255 if Need_Subprogram_Instance_Body (N, Act_Decl_Id) then
4257 Check_Forward_Instantiation (Gen_Decl);
4259 -- The wrapper package is always delayed, because it does not
4260 -- constitute a freeze point, but to insure that the freeze
4261 -- node is placed properly, it is created directly when
4262 -- instantiating the body (otherwise the freeze node might
4263 -- appear to early for nested instantiations).
4265 elsif Nkind (Parent (N)) = N_Compilation_Unit then
4267 -- For ASIS purposes, indicate that the wrapper package has
4268 -- replaced the instantiation node.
4270 Rewrite (N, Unit (Parent (N)));
4271 Set_Unit (Parent (N), N);
4274 elsif Nkind (Parent (N)) = N_Compilation_Unit then
4276 -- Replace instance node for library-level instantiations of
4277 -- intrinsic subprograms, for ASIS use.
4279 Rewrite (N, Unit (Parent (N)));
4280 Set_Unit (Parent (N), N);
4283 if Parent_Installed then
4288 Env_Installed := False;
4289 Generic_Renamings.Set_Last (0);
4290 Generic_Renamings_HTable.Reset;
4294 when Instantiation_Error =>
4295 if Parent_Installed then
4299 if Env_Installed then
4302 end Analyze_Subprogram_Instantiation;
4304 -------------------------
4305 -- Get_Associated_Node --
4306 -------------------------
4308 function Get_Associated_Node (N : Node_Id) return Node_Id is
4312 Assoc := Associated_Node (N);
4314 if Nkind (Assoc) /= Nkind (N) then
4317 elsif Nkind_In (Assoc, N_Aggregate, N_Extension_Aggregate) then
4321 -- If the node is part of an inner generic, it may itself have been
4322 -- remapped into a further generic copy. Associated_Node is otherwise
4323 -- used for the entity of the node, and will be of a different node
4324 -- kind, or else N has been rewritten as a literal or function call.
4326 while Present (Associated_Node (Assoc))
4327 and then Nkind (Associated_Node (Assoc)) = Nkind (Assoc)
4329 Assoc := Associated_Node (Assoc);
4332 -- Follow and additional link in case the final node was rewritten.
4333 -- This can only happen with nested generic units.
4335 if (Nkind (Assoc) = N_Identifier or else Nkind (Assoc) in N_Op)
4336 and then Present (Associated_Node (Assoc))
4337 and then (Nkind_In (Associated_Node (Assoc), N_Function_Call,
4338 N_Explicit_Dereference,
4343 Assoc := Associated_Node (Assoc);
4348 end Get_Associated_Node;
4350 -------------------------------------------
4351 -- Build_Instance_Compilation_Unit_Nodes --
4352 -------------------------------------------
4354 procedure Build_Instance_Compilation_Unit_Nodes
4359 Decl_Cunit : Node_Id;
4360 Body_Cunit : Node_Id;
4362 New_Main : constant Entity_Id := Defining_Entity (Act_Decl);
4363 Old_Main : constant Entity_Id := Cunit_Entity (Main_Unit);
4366 -- A new compilation unit node is built for the instance declaration
4369 Make_Compilation_Unit (Sloc (N),
4370 Context_Items => Empty_List,
4373 Make_Compilation_Unit_Aux (Sloc (N)));
4375 Set_Parent_Spec (Act_Decl, Parent_Spec (N));
4377 -- The new compilation unit is linked to its body, but both share the
4378 -- same file, so we do not set Body_Required on the new unit so as not
4379 -- to create a spurious dependency on a non-existent body in the ali.
4380 -- This simplifies CodePeer unit traversal.
4382 -- We use the original instantiation compilation unit as the resulting
4383 -- compilation unit of the instance, since this is the main unit.
4385 Rewrite (N, Act_Body);
4386 Body_Cunit := Parent (N);
4388 -- The two compilation unit nodes are linked by the Library_Unit field
4390 Set_Library_Unit (Decl_Cunit, Body_Cunit);
4391 Set_Library_Unit (Body_Cunit, Decl_Cunit);
4393 -- Preserve the private nature of the package if needed
4395 Set_Private_Present (Decl_Cunit, Private_Present (Body_Cunit));
4397 -- If the instance is not the main unit, its context, categorization
4398 -- and elaboration entity are not relevant to the compilation.
4400 if Body_Cunit /= Cunit (Main_Unit) then
4401 Make_Instance_Unit (Body_Cunit, In_Main => False);
4405 -- The context clause items on the instantiation, which are now attached
4406 -- to the body compilation unit (since the body overwrote the original
4407 -- instantiation node), semantically belong on the spec, so copy them
4408 -- there. It's harmless to leave them on the body as well. In fact one
4409 -- could argue that they belong in both places.
4411 Citem := First (Context_Items (Body_Cunit));
4412 while Present (Citem) loop
4413 Append (New_Copy (Citem), Context_Items (Decl_Cunit));
4417 -- Propagate categorization flags on packages, so that they appear in
4418 -- the ali file for the spec of the unit.
4420 if Ekind (New_Main) = E_Package then
4421 Set_Is_Pure (Old_Main, Is_Pure (New_Main));
4422 Set_Is_Preelaborated (Old_Main, Is_Preelaborated (New_Main));
4423 Set_Is_Remote_Types (Old_Main, Is_Remote_Types (New_Main));
4424 Set_Is_Shared_Passive (Old_Main, Is_Shared_Passive (New_Main));
4425 Set_Is_Remote_Call_Interface
4426 (Old_Main, Is_Remote_Call_Interface (New_Main));
4429 -- Make entry in Units table, so that binder can generate call to
4430 -- elaboration procedure for body, if any.
4432 Make_Instance_Unit (Body_Cunit, In_Main => True);
4433 Main_Unit_Entity := New_Main;
4434 Set_Cunit_Entity (Main_Unit, Main_Unit_Entity);
4436 -- Build elaboration entity, since the instance may certainly generate
4437 -- elaboration code requiring a flag for protection.
4439 Build_Elaboration_Entity (Decl_Cunit, New_Main);
4440 end Build_Instance_Compilation_Unit_Nodes;
4442 -----------------------------
4443 -- Check_Access_Definition --
4444 -----------------------------
4446 procedure Check_Access_Definition (N : Node_Id) is
4449 (Ada_Version >= Ada_05
4450 and then Present (Access_Definition (N)));
4452 end Check_Access_Definition;
4454 -----------------------------------
4455 -- Check_Formal_Package_Instance --
4456 -----------------------------------
4458 -- If the formal has specific parameters, they must match those of the
4459 -- actual. Both of them are instances, and the renaming declarations for
4460 -- their formal parameters appear in the same order in both. The analyzed
4461 -- formal has been analyzed in the context of the current instance.
4463 procedure Check_Formal_Package_Instance
4464 (Formal_Pack : Entity_Id;
4465 Actual_Pack : Entity_Id)
4467 E1 : Entity_Id := First_Entity (Actual_Pack);
4468 E2 : Entity_Id := First_Entity (Formal_Pack);
4473 procedure Check_Mismatch (B : Boolean);
4474 -- Common error routine for mismatch between the parameters of the
4475 -- actual instance and those of the formal package.
4477 function Same_Instantiated_Constant (E1, E2 : Entity_Id) return Boolean;
4478 -- The formal may come from a nested formal package, and the actual may
4479 -- have been constant-folded. To determine whether the two denote the
4480 -- same entity we may have to traverse several definitions to recover
4481 -- the ultimate entity that they refer to.
4483 function Same_Instantiated_Variable (E1, E2 : Entity_Id) return Boolean;
4484 -- Similarly, if the formal comes from a nested formal package, the
4485 -- actual may designate the formal through multiple renamings, which
4486 -- have to be followed to determine the original variable in question.
4488 --------------------
4489 -- Check_Mismatch --
4490 --------------------
4492 procedure Check_Mismatch (B : Boolean) is
4493 Kind : constant Node_Kind := Nkind (Parent (E2));
4496 if Kind = N_Formal_Type_Declaration then
4499 elsif Nkind_In (Kind, N_Formal_Object_Declaration,
4500 N_Formal_Package_Declaration)
4501 or else Kind in N_Formal_Subprogram_Declaration
4507 ("actual for & in actual instance does not match formal",
4508 Parent (Actual_Pack), E1);
4512 --------------------------------
4513 -- Same_Instantiated_Constant --
4514 --------------------------------
4516 function Same_Instantiated_Constant
4517 (E1, E2 : Entity_Id) return Boolean
4523 while Present (Ent) loop
4527 elsif Ekind (Ent) /= E_Constant then
4530 elsif Is_Entity_Name (Constant_Value (Ent)) then
4531 if Entity (Constant_Value (Ent)) = E1 then
4534 Ent := Entity (Constant_Value (Ent));
4537 -- The actual may be a constant that has been folded. Recover
4540 elsif Is_Entity_Name (Original_Node (Constant_Value (Ent))) then
4541 Ent := Entity (Original_Node (Constant_Value (Ent)));
4548 end Same_Instantiated_Constant;
4550 --------------------------------
4551 -- Same_Instantiated_Variable --
4552 --------------------------------
4554 function Same_Instantiated_Variable
4555 (E1, E2 : Entity_Id) return Boolean
4557 function Original_Entity (E : Entity_Id) return Entity_Id;
4558 -- Follow chain of renamings to the ultimate ancestor
4560 ---------------------
4561 -- Original_Entity --
4562 ---------------------
4564 function Original_Entity (E : Entity_Id) return Entity_Id is
4569 while Nkind (Parent (Orig)) = N_Object_Renaming_Declaration
4570 and then Present (Renamed_Object (Orig))
4571 and then Is_Entity_Name (Renamed_Object (Orig))
4573 Orig := Entity (Renamed_Object (Orig));
4577 end Original_Entity;
4579 -- Start of processing for Same_Instantiated_Variable
4582 return Ekind (E1) = Ekind (E2)
4583 and then Original_Entity (E1) = Original_Entity (E2);
4584 end Same_Instantiated_Variable;
4586 -- Start of processing for Check_Formal_Package_Instance
4590 and then Present (E2)
4592 exit when Ekind (E1) = E_Package
4593 and then Renamed_Entity (E1) = Renamed_Entity (Actual_Pack);
4595 -- If the formal is the renaming of the formal package, this
4596 -- is the end of its formal part, which may occur before the
4597 -- end of the formal part in the actual in the presence of
4598 -- defaulted parameters in the formal package.
4600 exit when Nkind (Parent (E2)) = N_Package_Renaming_Declaration
4601 and then Renamed_Entity (E2) = Scope (E2);
4603 -- The analysis of the actual may generate additional internal
4604 -- entities. If the formal is defaulted, there is no corresponding
4605 -- analysis and the internal entities must be skipped, until we
4606 -- find corresponding entities again.
4608 if Comes_From_Source (E2)
4609 and then not Comes_From_Source (E1)
4610 and then Chars (E1) /= Chars (E2)
4613 and then Chars (E1) /= Chars (E2)
4622 -- If the formal entity comes from a formal declaration, it was
4623 -- defaulted in the formal package, and no check is needed on it.
4625 elsif Nkind (Parent (E2)) = N_Formal_Object_Declaration then
4628 elsif Is_Type (E1) then
4630 -- Subtypes must statically match. E1, E2 are the local entities
4631 -- that are subtypes of the actuals. Itypes generated for other
4632 -- parameters need not be checked, the check will be performed
4633 -- on the parameters themselves.
4635 -- If E2 is a formal type declaration, it is a defaulted parameter
4636 -- and needs no checking.
4638 if not Is_Itype (E1)
4639 and then not Is_Itype (E2)
4643 or else Etype (E1) /= Etype (E2)
4644 or else not Subtypes_Statically_Match (E1, E2));
4647 elsif Ekind (E1) = E_Constant then
4649 -- IN parameters must denote the same static value, or the same
4650 -- constant, or the literal null.
4652 Expr1 := Expression (Parent (E1));
4654 if Ekind (E2) /= E_Constant then
4655 Check_Mismatch (True);
4658 Expr2 := Expression (Parent (E2));
4661 if Is_Static_Expression (Expr1) then
4663 if not Is_Static_Expression (Expr2) then
4664 Check_Mismatch (True);
4666 elsif Is_Discrete_Type (Etype (E1)) then
4668 V1 : constant Uint := Expr_Value (Expr1);
4669 V2 : constant Uint := Expr_Value (Expr2);
4671 Check_Mismatch (V1 /= V2);
4674 elsif Is_Real_Type (Etype (E1)) then
4676 V1 : constant Ureal := Expr_Value_R (Expr1);
4677 V2 : constant Ureal := Expr_Value_R (Expr2);
4679 Check_Mismatch (V1 /= V2);
4682 elsif Is_String_Type (Etype (E1))
4683 and then Nkind (Expr1) = N_String_Literal
4685 if Nkind (Expr2) /= N_String_Literal then
4686 Check_Mismatch (True);
4689 (not String_Equal (Strval (Expr1), Strval (Expr2)));
4693 elsif Is_Entity_Name (Expr1) then
4694 if Is_Entity_Name (Expr2) then
4695 if Entity (Expr1) = Entity (Expr2) then
4699 (not Same_Instantiated_Constant
4700 (Entity (Expr1), Entity (Expr2)));
4703 Check_Mismatch (True);
4706 elsif Is_Entity_Name (Original_Node (Expr1))
4707 and then Is_Entity_Name (Expr2)
4709 Same_Instantiated_Constant
4710 (Entity (Original_Node (Expr1)), Entity (Expr2))
4714 elsif Nkind (Expr1) = N_Null then
4715 Check_Mismatch (Nkind (Expr1) /= N_Null);
4718 Check_Mismatch (True);
4721 elsif Ekind (E1) = E_Variable then
4722 Check_Mismatch (not Same_Instantiated_Variable (E1, E2));
4724 elsif Ekind (E1) = E_Package then
4726 (Ekind (E1) /= Ekind (E2)
4727 or else Renamed_Object (E1) /= Renamed_Object (E2));
4729 elsif Is_Overloadable (E1) then
4731 -- Verify that the actual subprograms match. Note that actuals
4732 -- that are attributes are rewritten as subprograms. If the
4733 -- subprogram in the formal package is defaulted, no check is
4734 -- needed. Note that this can only happen in Ada 2005 when the
4735 -- formal package can be partially parametrized.
4737 if Nkind (Unit_Declaration_Node (E1)) =
4738 N_Subprogram_Renaming_Declaration
4739 and then From_Default (Unit_Declaration_Node (E1))
4745 (Ekind (E2) /= Ekind (E1) or else (Alias (E1)) /= Alias (E2));
4749 raise Program_Error;
4756 end Check_Formal_Package_Instance;
4758 ---------------------------
4759 -- Check_Formal_Packages --
4760 ---------------------------
4762 procedure Check_Formal_Packages (P_Id : Entity_Id) is
4764 Formal_P : Entity_Id;
4767 -- Iterate through the declarations in the instance, looking for package
4768 -- renaming declarations that denote instances of formal packages. Stop
4769 -- when we find the renaming of the current package itself. The
4770 -- declaration for a formal package without a box is followed by an
4771 -- internal entity that repeats the instantiation.
4773 E := First_Entity (P_Id);
4774 while Present (E) loop
4775 if Ekind (E) = E_Package then
4776 if Renamed_Object (E) = P_Id then
4779 elsif Nkind (Parent (E)) /= N_Package_Renaming_Declaration then
4782 elsif not Box_Present (Parent (Associated_Formal_Package (E))) then
4783 Formal_P := Next_Entity (E);
4784 Check_Formal_Package_Instance (Formal_P, E);
4786 -- After checking, remove the internal validating package. It
4787 -- is only needed for semantic checks, and as it may contain
4788 -- generic formal declarations it should not reach gigi.
4790 Remove (Unit_Declaration_Node (Formal_P));
4796 end Check_Formal_Packages;
4798 ---------------------------------
4799 -- Check_Forward_Instantiation --
4800 ---------------------------------
4802 procedure Check_Forward_Instantiation (Decl : Node_Id) is
4804 Gen_Comp : Entity_Id := Cunit_Entity (Get_Source_Unit (Decl));
4807 -- The instantiation appears before the generic body if we are in the
4808 -- scope of the unit containing the generic, either in its spec or in
4809 -- the package body, and before the generic body.
4811 if Ekind (Gen_Comp) = E_Package_Body then
4812 Gen_Comp := Spec_Entity (Gen_Comp);
4815 if In_Open_Scopes (Gen_Comp)
4816 and then No (Corresponding_Body (Decl))
4821 and then not Is_Compilation_Unit (S)
4822 and then not Is_Child_Unit (S)
4824 if Ekind (S) = E_Package then
4825 Set_Has_Forward_Instantiation (S);
4831 end Check_Forward_Instantiation;
4833 ---------------------------
4834 -- Check_Generic_Actuals --
4835 ---------------------------
4837 -- The visibility of the actuals may be different between the point of
4838 -- generic instantiation and the instantiation of the body.
4840 procedure Check_Generic_Actuals
4841 (Instance : Entity_Id;
4842 Is_Formal_Box : Boolean)
4847 function Denotes_Previous_Actual (Typ : Entity_Id) return Boolean;
4848 -- For a formal that is an array type, the component type is often a
4849 -- previous formal in the same unit. The privacy status of the component
4850 -- type will have been examined earlier in the traversal of the
4851 -- corresponding actuals, and this status should not be modified for the
4852 -- array type itself.
4854 -- To detect this case we have to rescan the list of formals, which
4855 -- is usually short enough to ignore the resulting inefficiency.
4857 -----------------------------
4858 -- Denotes_Previous_Actual --
4859 -----------------------------
4861 function Denotes_Previous_Actual (Typ : Entity_Id) return Boolean is
4865 Prev := First_Entity (Instance);
4866 while Present (Prev) loop
4868 and then Nkind (Parent (Prev)) = N_Subtype_Declaration
4869 and then Is_Entity_Name (Subtype_Indication (Parent (Prev)))
4870 and then Entity (Subtype_Indication (Parent (Prev))) = Typ
4883 end Denotes_Previous_Actual;
4885 -- Start of processing for Check_Generic_Actuals
4888 E := First_Entity (Instance);
4889 while Present (E) loop
4891 and then Nkind (Parent (E)) = N_Subtype_Declaration
4892 and then Scope (Etype (E)) /= Instance
4893 and then Is_Entity_Name (Subtype_Indication (Parent (E)))
4895 if Is_Array_Type (E)
4896 and then Denotes_Previous_Actual (Component_Type (E))
4900 Check_Private_View (Subtype_Indication (Parent (E)));
4902 Set_Is_Generic_Actual_Type (E, True);
4903 Set_Is_Hidden (E, False);
4904 Set_Is_Potentially_Use_Visible (E,
4907 -- We constructed the generic actual type as a subtype of the
4908 -- supplied type. This means that it normally would not inherit
4909 -- subtype specific attributes of the actual, which is wrong for
4910 -- the generic case.
4912 Astype := Ancestor_Subtype (E);
4916 -- This can happen when E is an itype that is the full view of
4917 -- a private type completed, e.g. with a constrained array. In
4918 -- that case, use the first subtype, which will carry size
4919 -- information. The base type itself is unconstrained and will
4922 Astype := First_Subtype (E);
4925 Set_Size_Info (E, (Astype));
4926 Set_RM_Size (E, RM_Size (Astype));
4927 Set_First_Rep_Item (E, First_Rep_Item (Astype));
4929 if Is_Discrete_Or_Fixed_Point_Type (E) then
4930 Set_RM_Size (E, RM_Size (Astype));
4932 -- In nested instances, the base type of an access actual
4933 -- may itself be private, and need to be exchanged.
4935 elsif Is_Access_Type (E)
4936 and then Is_Private_Type (Etype (E))
4939 (New_Occurrence_Of (Etype (E), Sloc (Instance)));
4942 elsif Ekind (E) = E_Package then
4944 -- If this is the renaming for the current instance, we're done.
4945 -- Otherwise it is a formal package. If the corresponding formal
4946 -- was declared with a box, the (instantiations of the) generic
4947 -- formal part are also visible. Otherwise, ignore the entity
4948 -- created to validate the actuals.
4950 if Renamed_Object (E) = Instance then
4953 elsif Nkind (Parent (E)) /= N_Package_Renaming_Declaration then
4956 -- The visibility of a formal of an enclosing generic is already
4959 elsif Denotes_Formal_Package (E) then
4962 elsif Present (Associated_Formal_Package (E))
4963 and then not Is_Generic_Formal (E)
4965 if Box_Present (Parent (Associated_Formal_Package (E))) then
4966 Check_Generic_Actuals (Renamed_Object (E), True);
4969 Check_Generic_Actuals (Renamed_Object (E), False);
4972 Set_Is_Hidden (E, False);
4975 -- If this is a subprogram instance (in a wrapper package) the
4976 -- actual is fully visible.
4978 elsif Is_Wrapper_Package (Instance) then
4979 Set_Is_Hidden (E, False);
4981 -- If the formal package is declared with a box, or if the formal
4982 -- parameter is defaulted, it is visible in the body.
4985 or else Is_Visible_Formal (E)
4987 Set_Is_Hidden (E, False);
4992 end Check_Generic_Actuals;
4994 ------------------------------
4995 -- Check_Generic_Child_Unit --
4996 ------------------------------
4998 procedure Check_Generic_Child_Unit
5000 Parent_Installed : in out Boolean)
5002 Loc : constant Source_Ptr := Sloc (Gen_Id);
5003 Gen_Par : Entity_Id := Empty;
5005 Inst_Par : Entity_Id;
5008 function Find_Generic_Child
5010 Id : Node_Id) return Entity_Id;
5011 -- Search generic parent for possible child unit with the given name
5013 function In_Enclosing_Instance return Boolean;
5014 -- Within an instance of the parent, the child unit may be denoted
5015 -- by a simple name, or an abbreviated expanded name. Examine enclosing
5016 -- scopes to locate a possible parent instantiation.
5018 ------------------------
5019 -- Find_Generic_Child --
5020 ------------------------
5022 function Find_Generic_Child
5024 Id : Node_Id) return Entity_Id
5029 -- If entity of name is already set, instance has already been
5030 -- resolved, e.g. in an enclosing instantiation.
5032 if Present (Entity (Id)) then
5033 if Scope (Entity (Id)) = Scop then
5040 E := First_Entity (Scop);
5041 while Present (E) loop
5042 if Chars (E) = Chars (Id)
5043 and then Is_Child_Unit (E)
5045 if Is_Child_Unit (E)
5046 and then not Is_Visible_Child_Unit (E)
5049 ("generic child unit& is not visible", Gen_Id, E);
5061 end Find_Generic_Child;
5063 ---------------------------
5064 -- In_Enclosing_Instance --
5065 ---------------------------
5067 function In_Enclosing_Instance return Boolean is
5068 Enclosing_Instance : Node_Id;
5069 Instance_Decl : Node_Id;
5072 -- We do not inline any call that contains instantiations, except
5073 -- for instantiations of Unchecked_Conversion, so if we are within
5074 -- an inlined body the current instance does not require parents.
5076 if In_Inlined_Body then
5077 pragma Assert (Chars (Gen_Id) = Name_Unchecked_Conversion);
5081 -- Loop to check enclosing scopes
5083 Enclosing_Instance := Current_Scope;
5084 while Present (Enclosing_Instance) loop
5085 Instance_Decl := Unit_Declaration_Node (Enclosing_Instance);
5087 if Ekind (Enclosing_Instance) = E_Package
5088 and then Is_Generic_Instance (Enclosing_Instance)
5090 (Generic_Parent (Specification (Instance_Decl)))
5092 -- Check whether the generic we are looking for is a child of
5095 E := Find_Generic_Child
5096 (Generic_Parent (Specification (Instance_Decl)), Gen_Id);
5097 exit when Present (E);
5103 Enclosing_Instance := Scope (Enclosing_Instance);
5115 Make_Expanded_Name (Loc,
5117 Prefix => New_Occurrence_Of (Enclosing_Instance, Loc),
5118 Selector_Name => New_Occurrence_Of (E, Loc)));
5120 Set_Entity (Gen_Id, E);
5121 Set_Etype (Gen_Id, Etype (E));
5122 Parent_Installed := False; -- Already in scope.
5125 end In_Enclosing_Instance;
5127 -- Start of processing for Check_Generic_Child_Unit
5130 -- If the name of the generic is given by a selected component, it may
5131 -- be the name of a generic child unit, and the prefix is the name of an
5132 -- instance of the parent, in which case the child unit must be visible.
5133 -- If this instance is not in scope, it must be placed there and removed
5134 -- after instantiation, because what is being instantiated is not the
5135 -- original child, but the corresponding child present in the instance
5138 -- If the child is instantiated within the parent, it can be given by
5139 -- a simple name. In this case the instance is already in scope, but
5140 -- the child generic must be recovered from the generic parent as well.
5142 if Nkind (Gen_Id) = N_Selected_Component then
5143 S := Selector_Name (Gen_Id);
5144 Analyze (Prefix (Gen_Id));
5145 Inst_Par := Entity (Prefix (Gen_Id));
5147 if Ekind (Inst_Par) = E_Package
5148 and then Present (Renamed_Object (Inst_Par))
5150 Inst_Par := Renamed_Object (Inst_Par);
5153 if Ekind (Inst_Par) = E_Package then
5154 if Nkind (Parent (Inst_Par)) = N_Package_Specification then
5155 Gen_Par := Generic_Parent (Parent (Inst_Par));
5157 elsif Nkind (Parent (Inst_Par)) = N_Defining_Program_Unit_Name
5159 Nkind (Parent (Parent (Inst_Par))) = N_Package_Specification
5161 Gen_Par := Generic_Parent (Parent (Parent (Inst_Par)));
5164 elsif Ekind (Inst_Par) = E_Generic_Package
5165 and then Nkind (Parent (Gen_Id)) = N_Formal_Package_Declaration
5167 -- A formal package may be a real child package, and not the
5168 -- implicit instance within a parent. In this case the child is
5169 -- not visible and has to be retrieved explicitly as well.
5171 Gen_Par := Inst_Par;
5174 if Present (Gen_Par) then
5176 -- The prefix denotes an instantiation. The entity itself may be a
5177 -- nested generic, or a child unit.
5179 E := Find_Generic_Child (Gen_Par, S);
5182 Change_Selected_Component_To_Expanded_Name (Gen_Id);
5183 Set_Entity (Gen_Id, E);
5184 Set_Etype (Gen_Id, Etype (E));
5186 Set_Etype (S, Etype (E));
5188 -- Indicate that this is a reference to the parent
5190 if In_Extended_Main_Source_Unit (Gen_Id) then
5191 Set_Is_Instantiated (Inst_Par);
5194 -- A common mistake is to replicate the naming scheme of a
5195 -- hierarchy by instantiating a generic child directly, rather
5196 -- than the implicit child in a parent instance:
5198 -- generic .. package Gpar is ..
5199 -- generic .. package Gpar.Child is ..
5200 -- package Par is new Gpar ();
5203 -- package Par.Child is new Gpar.Child ();
5204 -- rather than Par.Child
5206 -- In this case the instantiation is within Par, which is an
5207 -- instance, but Gpar does not denote Par because we are not IN
5208 -- the instance of Gpar, so this is illegal. The test below
5209 -- recognizes this particular case.
5211 if Is_Child_Unit (E)
5212 and then not Comes_From_Source (Entity (Prefix (Gen_Id)))
5213 and then (not In_Instance
5214 or else Nkind (Parent (Parent (Gen_Id))) =
5218 ("prefix of generic child unit must be instance of parent",
5222 if not In_Open_Scopes (Inst_Par)
5223 and then Nkind (Parent (Gen_Id)) not in
5224 N_Generic_Renaming_Declaration
5226 Install_Parent (Inst_Par);
5227 Parent_Installed := True;
5229 elsif In_Open_Scopes (Inst_Par) then
5231 -- If the parent is already installed, install the actuals
5232 -- for its formal packages. This is necessary when the
5233 -- child instance is a child of the parent instance:
5234 -- in this case, the parent is placed on the scope stack
5235 -- but the formal packages are not made visible.
5237 Install_Formal_Packages (Inst_Par);
5241 -- If the generic parent does not contain an entity that
5242 -- corresponds to the selector, the instance doesn't either.
5243 -- Analyzing the node will yield the appropriate error message.
5244 -- If the entity is not a child unit, then it is an inner
5245 -- generic in the parent.
5253 if Is_Child_Unit (Entity (Gen_Id))
5255 Nkind (Parent (Gen_Id)) not in N_Generic_Renaming_Declaration
5256 and then not In_Open_Scopes (Inst_Par)
5258 Install_Parent (Inst_Par);
5259 Parent_Installed := True;
5263 elsif Nkind (Gen_Id) = N_Expanded_Name then
5265 -- Entity already present, analyze prefix, whose meaning may be
5266 -- an instance in the current context. If it is an instance of
5267 -- a relative within another, the proper parent may still have
5268 -- to be installed, if they are not of the same generation.
5270 Analyze (Prefix (Gen_Id));
5272 -- In the unlikely case that a local declaration hides the name
5273 -- of the parent package, locate it on the homonym chain. If the
5274 -- context is an instance of the parent, the renaming entity is
5277 Inst_Par := Entity (Prefix (Gen_Id));
5278 while Present (Inst_Par)
5279 and then not Is_Package_Or_Generic_Package (Inst_Par)
5281 Inst_Par := Homonym (Inst_Par);
5284 pragma Assert (Present (Inst_Par));
5285 Set_Entity (Prefix (Gen_Id), Inst_Par);
5287 if In_Enclosing_Instance then
5290 elsif Present (Entity (Gen_Id))
5291 and then Is_Child_Unit (Entity (Gen_Id))
5292 and then not In_Open_Scopes (Inst_Par)
5294 Install_Parent (Inst_Par);
5295 Parent_Installed := True;
5298 elsif In_Enclosing_Instance then
5300 -- The child unit is found in some enclosing scope
5307 -- If this is the renaming of the implicit child in a parent
5308 -- instance, recover the parent name and install it.
5310 if Is_Entity_Name (Gen_Id) then
5311 E := Entity (Gen_Id);
5313 if Is_Generic_Unit (E)
5314 and then Nkind (Parent (E)) in N_Generic_Renaming_Declaration
5315 and then Is_Child_Unit (Renamed_Object (E))
5316 and then Is_Generic_Unit (Scope (Renamed_Object (E)))
5317 and then Nkind (Name (Parent (E))) = N_Expanded_Name
5320 New_Copy_Tree (Name (Parent (E))));
5321 Inst_Par := Entity (Prefix (Gen_Id));
5323 if not In_Open_Scopes (Inst_Par) then
5324 Install_Parent (Inst_Par);
5325 Parent_Installed := True;
5328 -- If it is a child unit of a non-generic parent, it may be
5329 -- use-visible and given by a direct name. Install parent as
5332 elsif Is_Generic_Unit (E)
5333 and then Is_Child_Unit (E)
5335 Nkind (Parent (Gen_Id)) not in N_Generic_Renaming_Declaration
5336 and then not Is_Generic_Unit (Scope (E))
5338 if not In_Open_Scopes (Scope (E)) then
5339 Install_Parent (Scope (E));
5340 Parent_Installed := True;
5345 end Check_Generic_Child_Unit;
5347 -----------------------------
5348 -- Check_Hidden_Child_Unit --
5349 -----------------------------
5351 procedure Check_Hidden_Child_Unit
5353 Gen_Unit : Entity_Id;
5354 Act_Decl_Id : Entity_Id)
5356 Gen_Id : constant Node_Id := Name (N);
5359 if Is_Child_Unit (Gen_Unit)
5360 and then Is_Child_Unit (Act_Decl_Id)
5361 and then Nkind (Gen_Id) = N_Expanded_Name
5362 and then Entity (Prefix (Gen_Id)) = Scope (Act_Decl_Id)
5363 and then Chars (Gen_Unit) = Chars (Act_Decl_Id)
5365 Error_Msg_Node_2 := Scope (Act_Decl_Id);
5367 ("generic unit & is implicitly declared in &",
5368 Defining_Unit_Name (N), Gen_Unit);
5369 Error_Msg_N ("\instance must have different name",
5370 Defining_Unit_Name (N));
5372 end Check_Hidden_Child_Unit;
5374 ------------------------
5375 -- Check_Private_View --
5376 ------------------------
5378 procedure Check_Private_View (N : Node_Id) is
5379 T : constant Entity_Id := Etype (N);
5383 -- Exchange views if the type was not private in the generic but is
5384 -- private at the point of instantiation. Do not exchange views if
5385 -- the scope of the type is in scope. This can happen if both generic
5386 -- and instance are sibling units, or if type is defined in a parent.
5387 -- In this case the visibility of the type will be correct for all
5391 BT := Base_Type (T);
5393 if Is_Private_Type (T)
5394 and then not Has_Private_View (N)
5395 and then Present (Full_View (T))
5396 and then not In_Open_Scopes (Scope (T))
5398 -- In the generic, the full type was visible. Save the private
5399 -- entity, for subsequent exchange.
5403 elsif Has_Private_View (N)
5404 and then not Is_Private_Type (T)
5405 and then not Has_Been_Exchanged (T)
5406 and then Etype (Get_Associated_Node (N)) /= T
5408 -- Only the private declaration was visible in the generic. If
5409 -- the type appears in a subtype declaration, the subtype in the
5410 -- instance must have a view compatible with that of its parent,
5411 -- which must be exchanged (see corresponding code in Restore_
5412 -- Private_Views). Otherwise, if the type is defined in a parent
5413 -- unit, leave full visibility within instance, which is safe.
5415 if In_Open_Scopes (Scope (Base_Type (T)))
5416 and then not Is_Private_Type (Base_Type (T))
5417 and then Comes_From_Source (Base_Type (T))
5421 elsif Nkind (Parent (N)) = N_Subtype_Declaration
5422 or else not In_Private_Part (Scope (Base_Type (T)))
5424 Prepend_Elmt (T, Exchanged_Views);
5425 Exchange_Declarations (Etype (Get_Associated_Node (N)));
5428 -- For composite types with inconsistent representation exchange
5429 -- component types accordingly.
5431 elsif Is_Access_Type (T)
5432 and then Is_Private_Type (Designated_Type (T))
5433 and then not Has_Private_View (N)
5434 and then Present (Full_View (Designated_Type (T)))
5436 Switch_View (Designated_Type (T));
5438 elsif Is_Array_Type (T) then
5439 if Is_Private_Type (Component_Type (T))
5440 and then not Has_Private_View (N)
5441 and then Present (Full_View (Component_Type (T)))
5443 Switch_View (Component_Type (T));
5446 -- The normal exchange mechanism relies on the setting of a
5447 -- flag on the reference in the generic. However, an additional
5448 -- mechanism is needed for types that are not explicitly mentioned
5449 -- in the generic, but may be needed in expanded code in the
5450 -- instance. This includes component types of arrays and
5451 -- designated types of access types. This processing must also
5452 -- include the index types of arrays which we take care of here.
5459 Indx := First_Index (T);
5460 Typ := Base_Type (Etype (Indx));
5461 while Present (Indx) loop
5462 if Is_Private_Type (Typ)
5463 and then Present (Full_View (Typ))
5472 elsif Is_Private_Type (T)
5473 and then Present (Full_View (T))
5474 and then Is_Array_Type (Full_View (T))
5475 and then Is_Private_Type (Component_Type (Full_View (T)))
5479 -- Finally, a non-private subtype may have a private base type, which
5480 -- must be exchanged for consistency. This can happen when a package
5481 -- body is instantiated, when the scope stack is empty but in fact
5482 -- the subtype and the base type are declared in an enclosing scope.
5484 -- Note that in this case we introduce an inconsistency in the view
5485 -- set, because we switch the base type BT, but there could be some
5486 -- private dependent subtypes of BT which remain unswitched. Such
5487 -- subtypes might need to be switched at a later point (see specific
5488 -- provision for that case in Switch_View).
5490 elsif not Is_Private_Type (T)
5491 and then not Has_Private_View (N)
5492 and then Is_Private_Type (BT)
5493 and then Present (Full_View (BT))
5494 and then not Is_Generic_Type (BT)
5495 and then not In_Open_Scopes (BT)
5497 Prepend_Elmt (Full_View (BT), Exchanged_Views);
5498 Exchange_Declarations (BT);
5501 end Check_Private_View;
5503 --------------------------
5504 -- Contains_Instance_Of --
5505 --------------------------
5507 function Contains_Instance_Of
5510 N : Node_Id) return Boolean
5518 -- Verify that there are no circular instantiations. We check whether
5519 -- the unit contains an instance of the current scope or some enclosing
5520 -- scope (in case one of the instances appears in a subunit). Longer
5521 -- circularities involving subunits might seem too pathological to
5522 -- consider, but they were not too pathological for the authors of
5523 -- DEC bc30vsq, so we loop over all enclosing scopes, and mark all
5524 -- enclosing generic scopes as containing an instance.
5527 -- Within a generic subprogram body, the scope is not generic, to
5528 -- allow for recursive subprograms. Use the declaration to determine
5529 -- whether this is a generic unit.
5531 if Ekind (Scop) = E_Generic_Package
5532 or else (Is_Subprogram (Scop)
5533 and then Nkind (Unit_Declaration_Node (Scop)) =
5534 N_Generic_Subprogram_Declaration)
5536 Elmt := First_Elmt (Inner_Instances (Inner));
5538 while Present (Elmt) loop
5539 if Node (Elmt) = Scop then
5540 Error_Msg_Node_2 := Inner;
5542 ("circular Instantiation: & instantiated within &!",
5546 elsif Node (Elmt) = Inner then
5549 elsif Contains_Instance_Of (Node (Elmt), Scop, N) then
5550 Error_Msg_Node_2 := Inner;
5552 ("circular Instantiation: & instantiated within &!",
5560 -- Indicate that Inner is being instantiated within Scop
5562 Append_Elmt (Inner, Inner_Instances (Scop));
5565 if Scop = Standard_Standard then
5568 Scop := Scope (Scop);
5573 end Contains_Instance_Of;
5575 -----------------------
5576 -- Copy_Generic_Node --
5577 -----------------------
5579 function Copy_Generic_Node
5581 Parent_Id : Node_Id;
5582 Instantiating : Boolean) return Node_Id
5587 function Copy_Generic_Descendant (D : Union_Id) return Union_Id;
5588 -- Check the given value of one of the Fields referenced by the
5589 -- current node to determine whether to copy it recursively. The
5590 -- field may hold a Node_Id, a List_Id, or an Elist_Id, or a plain
5591 -- value (Sloc, Uint, Char) in which case it need not be copied.
5593 procedure Copy_Descendants;
5594 -- Common utility for various nodes
5596 function Copy_Generic_Elist (E : Elist_Id) return Elist_Id;
5597 -- Make copy of element list
5599 function Copy_Generic_List
5601 Parent_Id : Node_Id) return List_Id;
5602 -- Apply Copy_Node recursively to the members of a node list
5604 function In_Defining_Unit_Name (Nam : Node_Id) return Boolean;
5605 -- True if an identifier is part of the defining program unit name
5606 -- of a child unit. The entity of such an identifier must be kept
5607 -- (for ASIS use) even though as the name of an enclosing generic
5608 -- it would otherwise not be preserved in the generic tree.
5610 ----------------------
5611 -- Copy_Descendants --
5612 ----------------------
5614 procedure Copy_Descendants is
5616 use Atree.Unchecked_Access;
5617 -- This code section is part of the implementation of an untyped
5618 -- tree traversal, so it needs direct access to node fields.
5621 Set_Field1 (New_N, Copy_Generic_Descendant (Field1 (N)));
5622 Set_Field2 (New_N, Copy_Generic_Descendant (Field2 (N)));
5623 Set_Field3 (New_N, Copy_Generic_Descendant (Field3 (N)));
5624 Set_Field4 (New_N, Copy_Generic_Descendant (Field4 (N)));
5625 Set_Field5 (New_N, Copy_Generic_Descendant (Field5 (N)));
5626 end Copy_Descendants;
5628 -----------------------------
5629 -- Copy_Generic_Descendant --
5630 -----------------------------
5632 function Copy_Generic_Descendant (D : Union_Id) return Union_Id is
5634 if D = Union_Id (Empty) then
5637 elsif D in Node_Range then
5639 (Copy_Generic_Node (Node_Id (D), New_N, Instantiating));
5641 elsif D in List_Range then
5642 return Union_Id (Copy_Generic_List (List_Id (D), New_N));
5644 elsif D in Elist_Range then
5645 return Union_Id (Copy_Generic_Elist (Elist_Id (D)));
5647 -- Nothing else is copyable (e.g. Uint values), return as is
5652 end Copy_Generic_Descendant;
5654 ------------------------
5655 -- Copy_Generic_Elist --
5656 ------------------------
5658 function Copy_Generic_Elist (E : Elist_Id) return Elist_Id is
5665 M := First_Elmt (E);
5666 while Present (M) loop
5668 (Copy_Generic_Node (Node (M), Empty, Instantiating), L);
5677 end Copy_Generic_Elist;
5679 -----------------------
5680 -- Copy_Generic_List --
5681 -----------------------
5683 function Copy_Generic_List
5685 Parent_Id : Node_Id) return List_Id
5693 Set_Parent (New_L, Parent_Id);
5696 while Present (N) loop
5697 Append (Copy_Generic_Node (N, Empty, Instantiating), New_L);
5706 end Copy_Generic_List;
5708 ---------------------------
5709 -- In_Defining_Unit_Name --
5710 ---------------------------
5712 function In_Defining_Unit_Name (Nam : Node_Id) return Boolean is
5714 return Present (Parent (Nam))
5715 and then (Nkind (Parent (Nam)) = N_Defining_Program_Unit_Name
5717 (Nkind (Parent (Nam)) = N_Expanded_Name
5718 and then In_Defining_Unit_Name (Parent (Nam))));
5719 end In_Defining_Unit_Name;
5721 -- Start of processing for Copy_Generic_Node
5728 New_N := New_Copy (N);
5730 if Instantiating then
5731 Adjust_Instantiation_Sloc (New_N, S_Adjustment);
5734 if not Is_List_Member (N) then
5735 Set_Parent (New_N, Parent_Id);
5738 -- If defining identifier, then all fields have been copied already
5740 if Nkind (New_N) in N_Entity then
5743 -- Special casing for identifiers and other entity names and operators
5745 elsif Nkind_In (New_N, N_Identifier,
5746 N_Character_Literal,
5749 or else Nkind (New_N) in N_Op
5751 if not Instantiating then
5753 -- Link both nodes in order to assign subsequently the entity of
5754 -- the copy to the original node, in case this is a global
5757 Set_Associated_Node (N, New_N);
5759 -- If we are within an instantiation, this is a nested generic
5760 -- that has already been analyzed at the point of definition. We
5761 -- must preserve references that were global to the enclosing
5762 -- parent at that point. Other occurrences, whether global or
5763 -- local to the current generic, must be resolved anew, so we
5764 -- reset the entity in the generic copy. A global reference has a
5765 -- smaller depth than the parent, or else the same depth in case
5766 -- both are distinct compilation units.
5767 -- A child unit is implicitly declared within the enclosing parent
5768 -- but is in fact global to it, and must be preserved.
5770 -- It is also possible for Current_Instantiated_Parent to be
5771 -- defined, and for this not to be a nested generic, namely if the
5772 -- unit is loaded through Rtsfind. In that case, the entity of
5773 -- New_N is only a link to the associated node, and not a defining
5776 -- The entities for parent units in the defining_program_unit of a
5777 -- generic child unit are established when the context of the unit
5778 -- is first analyzed, before the generic copy is made. They are
5779 -- preserved in the copy for use in ASIS queries.
5781 Ent := Entity (New_N);
5783 if No (Current_Instantiated_Parent.Gen_Id) then
5785 or else Nkind (Ent) /= N_Defining_Identifier
5786 or else not In_Defining_Unit_Name (N)
5788 Set_Associated_Node (New_N, Empty);
5793 not Nkind_In (Ent, N_Defining_Identifier,
5794 N_Defining_Character_Literal,
5795 N_Defining_Operator_Symbol)
5796 or else No (Scope (Ent))
5798 (Scope (Ent) = Current_Instantiated_Parent.Gen_Id
5799 and then not Is_Child_Unit (Ent))
5801 (Scope_Depth (Scope (Ent)) >
5802 Scope_Depth (Current_Instantiated_Parent.Gen_Id)
5804 Get_Source_Unit (Ent) =
5805 Get_Source_Unit (Current_Instantiated_Parent.Gen_Id))
5807 Set_Associated_Node (New_N, Empty);
5810 -- Case of instantiating identifier or some other name or operator
5813 -- If the associated node is still defined, the entity in it is
5814 -- global, and must be copied to the instance. If this copy is
5815 -- being made for a body to inline, it is applied to an
5816 -- instantiated tree, and the entity is already present and must
5817 -- be also preserved.
5820 Assoc : constant Node_Id := Get_Associated_Node (N);
5823 if Present (Assoc) then
5824 if Nkind (Assoc) = Nkind (N) then
5825 Set_Entity (New_N, Entity (Assoc));
5826 Check_Private_View (N);
5828 elsif Nkind (Assoc) = N_Function_Call then
5829 Set_Entity (New_N, Entity (Name (Assoc)));
5831 elsif Nkind_In (Assoc, N_Defining_Identifier,
5832 N_Defining_Character_Literal,
5833 N_Defining_Operator_Symbol)
5834 and then Expander_Active
5836 -- Inlining case: we are copying a tree that contains
5837 -- global entities, which are preserved in the copy to be
5838 -- used for subsequent inlining.
5843 Set_Entity (New_N, Empty);
5849 -- For expanded name, we must copy the Prefix and Selector_Name
5851 if Nkind (N) = N_Expanded_Name then
5853 (New_N, Copy_Generic_Node (Prefix (N), New_N, Instantiating));
5855 Set_Selector_Name (New_N,
5856 Copy_Generic_Node (Selector_Name (N), New_N, Instantiating));
5858 -- For operators, we must copy the right operand
5860 elsif Nkind (N) in N_Op then
5861 Set_Right_Opnd (New_N,
5862 Copy_Generic_Node (Right_Opnd (N), New_N, Instantiating));
5864 -- And for binary operators, the left operand as well
5866 if Nkind (N) in N_Binary_Op then
5867 Set_Left_Opnd (New_N,
5868 Copy_Generic_Node (Left_Opnd (N), New_N, Instantiating));
5872 -- Special casing for stubs
5874 elsif Nkind (N) in N_Body_Stub then
5876 -- In any case, we must copy the specification or defining
5877 -- identifier as appropriate.
5879 if Nkind (N) = N_Subprogram_Body_Stub then
5880 Set_Specification (New_N,
5881 Copy_Generic_Node (Specification (N), New_N, Instantiating));
5884 Set_Defining_Identifier (New_N,
5886 (Defining_Identifier (N), New_N, Instantiating));
5889 -- If we are not instantiating, then this is where we load and
5890 -- analyze subunits, i.e. at the point where the stub occurs. A
5891 -- more permissive system might defer this analysis to the point
5892 -- of instantiation, but this seems to complicated for now.
5894 if not Instantiating then
5896 Subunit_Name : constant Unit_Name_Type := Get_Unit_Name (N);
5898 Unum : Unit_Number_Type;
5904 (Load_Name => Subunit_Name,
5909 -- If the proper body is not found, a warning message will be
5910 -- emitted when analyzing the stub, or later at the point
5911 -- of instantiation. Here we just leave the stub as is.
5913 if Unum = No_Unit then
5914 Subunits_Missing := True;
5915 goto Subunit_Not_Found;
5918 Subunit := Cunit (Unum);
5920 if Nkind (Unit (Subunit)) /= N_Subunit then
5922 ("found child unit instead of expected SEPARATE subunit",
5924 Error_Msg_Sloc := Sloc (N);
5925 Error_Msg_N ("\to complete stub #", Subunit);
5926 goto Subunit_Not_Found;
5929 -- We must create a generic copy of the subunit, in order to
5930 -- perform semantic analysis on it, and we must replace the
5931 -- stub in the original generic unit with the subunit, in order
5932 -- to preserve non-local references within.
5934 -- Only the proper body needs to be copied. Library_Unit and
5935 -- context clause are simply inherited by the generic copy.
5936 -- Note that the copy (which may be recursive if there are
5937 -- nested subunits) must be done first, before attaching it to
5938 -- the enclosing generic.
5942 (Proper_Body (Unit (Subunit)),
5943 Empty, Instantiating => False);
5945 -- Now place the original proper body in the original generic
5946 -- unit. This is a body, not a compilation unit.
5948 Rewrite (N, Proper_Body (Unit (Subunit)));
5949 Set_Is_Compilation_Unit (Defining_Entity (N), False);
5950 Set_Was_Originally_Stub (N);
5952 -- Finally replace the body of the subunit with its copy, and
5953 -- make this new subunit into the library unit of the generic
5954 -- copy, which does not have stubs any longer.
5956 Set_Proper_Body (Unit (Subunit), New_Body);
5957 Set_Library_Unit (New_N, Subunit);
5958 Inherit_Context (Unit (Subunit), N);
5961 -- If we are instantiating, this must be an error case, since
5962 -- otherwise we would have replaced the stub node by the proper body
5963 -- that corresponds. So just ignore it in the copy (i.e. we have
5964 -- copied it, and that is good enough).
5970 <<Subunit_Not_Found>> null;
5972 -- If the node is a compilation unit, it is the subunit of a stub, which
5973 -- has been loaded already (see code below). In this case, the library
5974 -- unit field of N points to the parent unit (which is a compilation
5975 -- unit) and need not (and cannot!) be copied.
5977 -- When the proper body of the stub is analyzed, the library_unit link
5978 -- is used to establish the proper context (see sem_ch10).
5980 -- The other fields of a compilation unit are copied as usual
5982 elsif Nkind (N) = N_Compilation_Unit then
5984 -- This code can only be executed when not instantiating, because in
5985 -- the copy made for an instantiation, the compilation unit node has
5986 -- disappeared at the point that a stub is replaced by its proper
5989 pragma Assert (not Instantiating);
5991 Set_Context_Items (New_N,
5992 Copy_Generic_List (Context_Items (N), New_N));
5995 Copy_Generic_Node (Unit (N), New_N, False));
5997 Set_First_Inlined_Subprogram (New_N,
5999 (First_Inlined_Subprogram (N), New_N, False));
6001 Set_Aux_Decls_Node (New_N,
6002 Copy_Generic_Node (Aux_Decls_Node (N), New_N, False));
6004 -- For an assignment node, the assignment is known to be semantically
6005 -- legal if we are instantiating the template. This avoids incorrect
6006 -- diagnostics in generated code.
6008 elsif Nkind (N) = N_Assignment_Statement then
6010 -- Copy name and expression fields in usual manner
6013 Copy_Generic_Node (Name (N), New_N, Instantiating));
6015 Set_Expression (New_N,
6016 Copy_Generic_Node (Expression (N), New_N, Instantiating));
6018 if Instantiating then
6019 Set_Assignment_OK (Name (New_N), True);
6022 elsif Nkind_In (N, N_Aggregate, N_Extension_Aggregate) then
6023 if not Instantiating then
6024 Set_Associated_Node (N, New_N);
6027 if Present (Get_Associated_Node (N))
6028 and then Nkind (Get_Associated_Node (N)) = Nkind (N)
6030 -- In the generic the aggregate has some composite type. If at
6031 -- the point of instantiation the type has a private view,
6032 -- install the full view (and that of its ancestors, if any).
6035 T : Entity_Id := (Etype (Get_Associated_Node (New_N)));
6040 and then Is_Private_Type (T)
6046 and then Is_Tagged_Type (T)
6047 and then Is_Derived_Type (T)
6049 Rt := Root_Type (T);
6054 if Is_Private_Type (T) then
6065 -- Do not copy the associated node, which points to
6066 -- the generic copy of the aggregate.
6069 use Atree.Unchecked_Access;
6070 -- This code section is part of the implementation of an untyped
6071 -- tree traversal, so it needs direct access to node fields.
6074 Set_Field1 (New_N, Copy_Generic_Descendant (Field1 (N)));
6075 Set_Field2 (New_N, Copy_Generic_Descendant (Field2 (N)));
6076 Set_Field3 (New_N, Copy_Generic_Descendant (Field3 (N)));
6077 Set_Field5 (New_N, Copy_Generic_Descendant (Field5 (N)));
6080 -- Allocators do not have an identifier denoting the access type,
6081 -- so we must locate it through the expression to check whether
6082 -- the views are consistent.
6084 elsif Nkind (N) = N_Allocator
6085 and then Nkind (Expression (N)) = N_Qualified_Expression
6086 and then Is_Entity_Name (Subtype_Mark (Expression (N)))
6087 and then Instantiating
6090 T : constant Node_Id :=
6091 Get_Associated_Node (Subtype_Mark (Expression (N)));
6097 -- Retrieve the allocator node in the generic copy
6099 Acc_T := Etype (Parent (Parent (T)));
6101 and then Is_Private_Type (Acc_T)
6103 Switch_View (Acc_T);
6110 -- For a proper body, we must catch the case of a proper body that
6111 -- replaces a stub. This represents the point at which a separate
6112 -- compilation unit, and hence template file, may be referenced, so we
6113 -- must make a new source instantiation entry for the template of the
6114 -- subunit, and ensure that all nodes in the subunit are adjusted using
6115 -- this new source instantiation entry.
6117 elsif Nkind (N) in N_Proper_Body then
6119 Save_Adjustment : constant Sloc_Adjustment := S_Adjustment;
6122 if Instantiating and then Was_Originally_Stub (N) then
6123 Create_Instantiation_Source
6124 (Instantiation_Node,
6125 Defining_Entity (N),
6130 -- Now copy the fields of the proper body, using the new
6131 -- adjustment factor if one was needed as per test above.
6135 -- Restore the original adjustment factor in case changed
6137 S_Adjustment := Save_Adjustment;
6140 -- Don't copy Ident or Comment pragmas, since the comment belongs to the
6141 -- generic unit, not to the instantiating unit.
6143 elsif Nkind (N) = N_Pragma
6144 and then Instantiating
6147 Prag_Id : constant Pragma_Id := Get_Pragma_Id (N);
6149 if Prag_Id = Pragma_Ident
6150 or else Prag_Id = Pragma_Comment
6152 New_N := Make_Null_Statement (Sloc (N));
6158 elsif Nkind_In (N, N_Integer_Literal,
6162 -- No descendant fields need traversing
6166 -- For the remaining nodes, copy recursively their descendants
6172 and then Nkind (N) = N_Subprogram_Body
6174 Set_Generic_Parent (Specification (New_N), N);
6179 end Copy_Generic_Node;
6181 ----------------------------
6182 -- Denotes_Formal_Package --
6183 ----------------------------
6185 function Denotes_Formal_Package
6187 On_Exit : Boolean := False;
6188 Instance : Entity_Id := Empty) return Boolean
6191 Scop : constant Entity_Id := Scope (Pack);
6194 function Is_Actual_Of_Previous_Formal (P : Entity_Id) return Boolean;
6195 -- The package in question may be an actual for a previous formal
6196 -- package P of the current instance, so examine its actuals as well.
6197 -- This must be recursive over other formal packages.
6199 ----------------------------------
6200 -- Is_Actual_Of_Previous_Formal --
6201 ----------------------------------
6203 function Is_Actual_Of_Previous_Formal (P : Entity_Id) return Boolean is
6207 E1 := First_Entity (P);
6208 while Present (E1) and then E1 /= Instance loop
6209 if Ekind (E1) = E_Package
6210 and then Nkind (Parent (E1)) = N_Package_Renaming_Declaration
6212 if Renamed_Object (E1) = Pack then
6216 or else Renamed_Object (E1) = P
6220 elsif Is_Actual_Of_Previous_Formal (E1) then
6229 end Is_Actual_Of_Previous_Formal;
6231 -- Start of processing for Denotes_Formal_Package
6237 (Instance_Envs.Last).Instantiated_Parent.Act_Id;
6239 Par := Current_Instantiated_Parent.Act_Id;
6242 if Ekind (Scop) = E_Generic_Package
6243 or else Nkind (Unit_Declaration_Node (Scop)) =
6244 N_Generic_Subprogram_Declaration
6248 elsif Nkind (Original_Node (Unit_Declaration_Node (Pack))) =
6249 N_Formal_Package_Declaration
6257 -- Check whether this package is associated with a formal package of
6258 -- the enclosing instantiation. Iterate over the list of renamings.
6260 E := First_Entity (Par);
6261 while Present (E) loop
6262 if Ekind (E) /= E_Package
6263 or else Nkind (Parent (E)) /= N_Package_Renaming_Declaration
6267 elsif Renamed_Object (E) = Par then
6270 elsif Renamed_Object (E) = Pack then
6273 elsif Is_Actual_Of_Previous_Formal (E) then
6283 end Denotes_Formal_Package;
6289 procedure End_Generic is
6291 -- ??? More things could be factored out in this routine. Should
6292 -- probably be done at a later stage.
6294 Inside_A_Generic := Generic_Flags.Table (Generic_Flags.Last);
6295 Generic_Flags.Decrement_Last;
6297 Expander_Mode_Restore;
6300 ----------------------
6301 -- Find_Actual_Type --
6302 ----------------------
6304 function Find_Actual_Type
6306 Gen_Type : Entity_Id) return Entity_Id
6308 Gen_Scope : constant Entity_Id := Scope (Gen_Type);
6312 -- Special processing only applies to child units
6314 if not Is_Child_Unit (Gen_Scope) then
6315 return Get_Instance_Of (Typ);
6317 -- If designated or component type is itself a formal of the child unit,
6318 -- its instance is available.
6320 elsif Scope (Typ) = Gen_Scope then
6321 return Get_Instance_Of (Typ);
6323 -- If the array or access type is not declared in the parent unit,
6324 -- no special processing needed.
6326 elsif not Is_Generic_Type (Typ)
6327 and then Scope (Gen_Scope) /= Scope (Typ)
6329 return Get_Instance_Of (Typ);
6331 -- Otherwise, retrieve designated or component type by visibility
6334 T := Current_Entity (Typ);
6335 while Present (T) loop
6336 if In_Open_Scopes (Scope (T)) then
6339 elsif Is_Generic_Actual_Type (T) then
6348 end Find_Actual_Type;
6350 ----------------------------
6351 -- Freeze_Subprogram_Body --
6352 ----------------------------
6354 procedure Freeze_Subprogram_Body
6355 (Inst_Node : Node_Id;
6357 Pack_Id : Entity_Id)
6360 Gen_Unit : constant Entity_Id := Get_Generic_Entity (Inst_Node);
6361 Par : constant Entity_Id := Scope (Gen_Unit);
6366 function Earlier (N1, N2 : Node_Id) return Boolean;
6367 -- Yields True if N1 and N2 appear in the same compilation unit,
6368 -- ignoring subunits, and if N1 is to the left of N2 in a left-to-right
6369 -- traversal of the tree for the unit.
6371 function Enclosing_Body (N : Node_Id) return Node_Id;
6372 -- Find innermost package body that encloses the given node, and which
6373 -- is not a compilation unit. Freeze nodes for the instance, or for its
6374 -- enclosing body, may be inserted after the enclosing_body of the
6377 function Package_Freeze_Node (B : Node_Id) return Node_Id;
6378 -- Find entity for given package body, and locate or create a freeze
6381 function True_Parent (N : Node_Id) return Node_Id;
6382 -- For a subunit, return parent of corresponding stub
6388 function Earlier (N1, N2 : Node_Id) return Boolean is
6394 procedure Find_Depth (P : in out Node_Id; D : in out Integer);
6395 -- Find distance from given node to enclosing compilation unit
6401 procedure Find_Depth (P : in out Node_Id; D : in out Integer) is
6404 and then Nkind (P) /= N_Compilation_Unit
6406 P := True_Parent (P);
6411 -- Start of processing for Earlier
6414 Find_Depth (P1, D1);
6415 Find_Depth (P2, D2);
6425 P1 := True_Parent (P1);
6430 P2 := True_Parent (P2);
6434 -- At this point P1 and P2 are at the same distance from the root.
6435 -- We examine their parents until we find a common declarative
6436 -- list, at which point we can establish their relative placement
6437 -- by comparing their ultimate slocs. If we reach the root,
6438 -- N1 and N2 do not descend from the same declarative list (e.g.
6439 -- one is nested in the declarative part and the other is in a block
6440 -- in the statement part) and the earlier one is already frozen.
6442 while not Is_List_Member (P1)
6443 or else not Is_List_Member (P2)
6444 or else List_Containing (P1) /= List_Containing (P2)
6446 P1 := True_Parent (P1);
6447 P2 := True_Parent (P2);
6449 if Nkind (Parent (P1)) = N_Subunit then
6450 P1 := Corresponding_Stub (Parent (P1));
6453 if Nkind (Parent (P2)) = N_Subunit then
6454 P2 := Corresponding_Stub (Parent (P2));
6463 Top_Level_Location (Sloc (P1)) < Top_Level_Location (Sloc (P2));
6466 --------------------
6467 -- Enclosing_Body --
6468 --------------------
6470 function Enclosing_Body (N : Node_Id) return Node_Id is
6471 P : Node_Id := Parent (N);
6475 and then Nkind (Parent (P)) /= N_Compilation_Unit
6477 if Nkind (P) = N_Package_Body then
6479 if Nkind (Parent (P)) = N_Subunit then
6480 return Corresponding_Stub (Parent (P));
6486 P := True_Parent (P);
6492 -------------------------
6493 -- Package_Freeze_Node --
6494 -------------------------
6496 function Package_Freeze_Node (B : Node_Id) return Node_Id is
6500 if Nkind (B) = N_Package_Body then
6501 Id := Corresponding_Spec (B);
6503 else pragma Assert (Nkind (B) = N_Package_Body_Stub);
6504 Id := Corresponding_Spec (Proper_Body (Unit (Library_Unit (B))));
6507 Ensure_Freeze_Node (Id);
6508 return Freeze_Node (Id);
6509 end Package_Freeze_Node;
6515 function True_Parent (N : Node_Id) return Node_Id is
6517 if Nkind (Parent (N)) = N_Subunit then
6518 return Parent (Corresponding_Stub (Parent (N)));
6524 -- Start of processing of Freeze_Subprogram_Body
6527 -- If the instance and the generic body appear within the same unit, and
6528 -- the instance precedes the generic, the freeze node for the instance
6529 -- must appear after that of the generic. If the generic is nested
6530 -- within another instance I2, then current instance must be frozen
6531 -- after I2. In both cases, the freeze nodes are those of enclosing
6532 -- packages. Otherwise, the freeze node is placed at the end of the
6533 -- current declarative part.
6535 Enc_G := Enclosing_Body (Gen_Body);
6536 Enc_I := Enclosing_Body (Inst_Node);
6537 Ensure_Freeze_Node (Pack_Id);
6538 F_Node := Freeze_Node (Pack_Id);
6540 if Is_Generic_Instance (Par)
6541 and then Present (Freeze_Node (Par))
6543 In_Same_Declarative_Part (Freeze_Node (Par), Inst_Node)
6545 if ABE_Is_Certain (Get_Package_Instantiation_Node (Par)) then
6547 -- The parent was a premature instantiation. Insert freeze node at
6548 -- the end the current declarative part.
6550 Insert_After_Last_Decl (Inst_Node, F_Node);
6553 Insert_After (Freeze_Node (Par), F_Node);
6556 -- The body enclosing the instance should be frozen after the body that
6557 -- includes the generic, because the body of the instance may make
6558 -- references to entities therein. If the two are not in the same
6559 -- declarative part, or if the one enclosing the instance is frozen
6560 -- already, freeze the instance at the end of the current declarative
6563 elsif Is_Generic_Instance (Par)
6564 and then Present (Freeze_Node (Par))
6565 and then Present (Enc_I)
6567 if In_Same_Declarative_Part (Freeze_Node (Par), Enc_I)
6569 (Nkind (Enc_I) = N_Package_Body
6571 In_Same_Declarative_Part (Freeze_Node (Par), Parent (Enc_I)))
6573 -- The enclosing package may contain several instances. Rather
6574 -- than computing the earliest point at which to insert its
6575 -- freeze node, we place it at the end of the declarative part
6576 -- of the parent of the generic.
6578 Insert_After_Last_Decl
6579 (Freeze_Node (Par), Package_Freeze_Node (Enc_I));
6582 Insert_After_Last_Decl (Inst_Node, F_Node);
6584 elsif Present (Enc_G)
6585 and then Present (Enc_I)
6586 and then Enc_G /= Enc_I
6587 and then Earlier (Inst_Node, Gen_Body)
6589 if Nkind (Enc_G) = N_Package_Body then
6590 E_G_Id := Corresponding_Spec (Enc_G);
6591 else pragma Assert (Nkind (Enc_G) = N_Package_Body_Stub);
6593 Corresponding_Spec (Proper_Body (Unit (Library_Unit (Enc_G))));
6596 -- Freeze package that encloses instance, and place node after
6597 -- package that encloses generic. If enclosing package is already
6598 -- frozen we have to assume it is at the proper place. This may be
6599 -- a potential ABE that requires dynamic checking. Do not add a
6600 -- freeze node if the package that encloses the generic is inside
6601 -- the body that encloses the instance, because the freeze node
6602 -- would be in the wrong scope. Additional contortions needed if
6603 -- the bodies are within a subunit.
6606 Enclosing_Body : Node_Id;
6609 if Nkind (Enc_I) = N_Package_Body_Stub then
6610 Enclosing_Body := Proper_Body (Unit (Library_Unit (Enc_I)));
6612 Enclosing_Body := Enc_I;
6615 if Parent (List_Containing (Enc_G)) /= Enclosing_Body then
6616 Insert_After_Last_Decl (Enc_G, Package_Freeze_Node (Enc_I));
6620 -- Freeze enclosing subunit before instance
6622 Ensure_Freeze_Node (E_G_Id);
6624 if not Is_List_Member (Freeze_Node (E_G_Id)) then
6625 Insert_After (Enc_G, Freeze_Node (E_G_Id));
6628 Insert_After_Last_Decl (Inst_Node, F_Node);
6631 -- If none of the above, insert freeze node at the end of the current
6632 -- declarative part.
6634 Insert_After_Last_Decl (Inst_Node, F_Node);
6636 end Freeze_Subprogram_Body;
6642 function Get_Gen_Id (E : Assoc_Ptr) return Entity_Id is
6644 return Generic_Renamings.Table (E).Gen_Id;
6647 ---------------------
6648 -- Get_Instance_Of --
6649 ---------------------
6651 function Get_Instance_Of (A : Entity_Id) return Entity_Id is
6652 Res : constant Assoc_Ptr := Generic_Renamings_HTable.Get (A);
6655 if Res /= Assoc_Null then
6656 return Generic_Renamings.Table (Res).Act_Id;
6658 -- On exit, entity is not instantiated: not a generic parameter, or
6659 -- else parameter of an inner generic unit.
6663 end Get_Instance_Of;
6665 ------------------------------------
6666 -- Get_Package_Instantiation_Node --
6667 ------------------------------------
6669 function Get_Package_Instantiation_Node (A : Entity_Id) return Node_Id is
6670 Decl : Node_Id := Unit_Declaration_Node (A);
6674 -- If the Package_Instantiation attribute has been set on the package
6675 -- entity, then use it directly when it (or its Original_Node) refers
6676 -- to an N_Package_Instantiation node. In principle it should be
6677 -- possible to have this field set in all cases, which should be
6678 -- investigated, and would allow this function to be significantly
6681 if Present (Package_Instantiation (A)) then
6682 if Nkind (Package_Instantiation (A)) = N_Package_Instantiation then
6683 return Package_Instantiation (A);
6685 elsif Nkind (Original_Node (Package_Instantiation (A))) =
6686 N_Package_Instantiation
6688 return Original_Node (Package_Instantiation (A));
6692 -- If the instantiation is a compilation unit that does not need body
6693 -- then the instantiation node has been rewritten as a package
6694 -- declaration for the instance, and we return the original node.
6696 -- If it is a compilation unit and the instance node has not been
6697 -- rewritten, then it is still the unit of the compilation. Finally, if
6698 -- a body is present, this is a parent of the main unit whose body has
6699 -- been compiled for inlining purposes, and the instantiation node has
6700 -- been rewritten with the instance body.
6702 -- Otherwise the instantiation node appears after the declaration. If
6703 -- the entity is a formal package, the declaration may have been
6704 -- rewritten as a generic declaration (in the case of a formal with box)
6705 -- or left as a formal package declaration if it has actuals, and is
6706 -- found with a forward search.
6708 if Nkind (Parent (Decl)) = N_Compilation_Unit then
6709 if Nkind (Decl) = N_Package_Declaration
6710 and then Present (Corresponding_Body (Decl))
6712 Decl := Unit_Declaration_Node (Corresponding_Body (Decl));
6715 if Nkind (Original_Node (Decl)) = N_Package_Instantiation then
6716 return Original_Node (Decl);
6718 return Unit (Parent (Decl));
6721 elsif Nkind (Decl) = N_Package_Declaration
6722 and then Nkind (Original_Node (Decl)) = N_Formal_Package_Declaration
6724 return Original_Node (Decl);
6727 Inst := Next (Decl);
6728 while not Nkind_In (Inst, N_Package_Instantiation,
6729 N_Formal_Package_Declaration)
6736 end Get_Package_Instantiation_Node;
6738 ------------------------
6739 -- Has_Been_Exchanged --
6740 ------------------------
6742 function Has_Been_Exchanged (E : Entity_Id) return Boolean is
6746 Next := First_Elmt (Exchanged_Views);
6747 while Present (Next) loop
6748 if Full_View (Node (Next)) = E then
6756 end Has_Been_Exchanged;
6762 function Hash (F : Entity_Id) return HTable_Range is
6764 return HTable_Range (F mod HTable_Size);
6767 ------------------------
6768 -- Hide_Current_Scope --
6769 ------------------------
6771 procedure Hide_Current_Scope is
6772 C : constant Entity_Id := Current_Scope;
6776 Set_Is_Hidden_Open_Scope (C);
6778 E := First_Entity (C);
6779 while Present (E) loop
6780 if Is_Immediately_Visible (E) then
6781 Set_Is_Immediately_Visible (E, False);
6782 Append_Elmt (E, Hidden_Entities);
6788 -- Make the scope name invisible as well. This is necessary, but might
6789 -- conflict with calls to Rtsfind later on, in case the scope is a
6790 -- predefined one. There is no clean solution to this problem, so for
6791 -- now we depend on the user not redefining Standard itself in one of
6792 -- the parent units.
6794 if Is_Immediately_Visible (C)
6795 and then C /= Standard_Standard
6797 Set_Is_Immediately_Visible (C, False);
6798 Append_Elmt (C, Hidden_Entities);
6801 end Hide_Current_Scope;
6807 procedure Init_Env is
6808 Saved : Instance_Env;
6811 Saved.Instantiated_Parent := Current_Instantiated_Parent;
6812 Saved.Exchanged_Views := Exchanged_Views;
6813 Saved.Hidden_Entities := Hidden_Entities;
6814 Saved.Current_Sem_Unit := Current_Sem_Unit;
6815 Saved.Parent_Unit_Visible := Parent_Unit_Visible;
6816 Saved.Instance_Parent_Unit := Instance_Parent_Unit;
6818 -- Save configuration switches. These may be reset if the unit is a
6819 -- predefined unit, and the current mode is not Ada 2005.
6821 Save_Opt_Config_Switches (Saved.Switches);
6823 Instance_Envs.Append (Saved);
6825 Exchanged_Views := New_Elmt_List;
6826 Hidden_Entities := New_Elmt_List;
6828 -- Make dummy entry for Instantiated parent. If generic unit is legal,
6829 -- this is set properly in Set_Instance_Env.
6831 Current_Instantiated_Parent :=
6832 (Current_Scope, Current_Scope, Assoc_Null);
6835 ------------------------------
6836 -- In_Same_Declarative_Part --
6837 ------------------------------
6839 function In_Same_Declarative_Part
6841 Inst : Node_Id) return Boolean
6843 Decls : constant Node_Id := Parent (F_Node);
6844 Nod : Node_Id := Parent (Inst);
6847 while Present (Nod) loop
6851 elsif Nkind_In (Nod, N_Subprogram_Body,
6859 elsif Nkind (Nod) = N_Subunit then
6860 Nod := Corresponding_Stub (Nod);
6862 elsif Nkind (Nod) = N_Compilation_Unit then
6866 Nod := Parent (Nod);
6871 end In_Same_Declarative_Part;
6873 ---------------------
6874 -- In_Main_Context --
6875 ---------------------
6877 function In_Main_Context (E : Entity_Id) return Boolean is
6883 if not Is_Compilation_Unit (E)
6884 or else Ekind (E) /= E_Package
6885 or else In_Private_Part (E)
6890 Context := Context_Items (Cunit (Main_Unit));
6892 Clause := First (Context);
6893 while Present (Clause) loop
6894 if Nkind (Clause) = N_With_Clause then
6895 Nam := Name (Clause);
6897 -- If the current scope is part of the context of the main unit,
6898 -- analysis of the corresponding with_clause is not complete, and
6899 -- the entity is not set. We use the Chars field directly, which
6900 -- might produce false positives in rare cases, but guarantees
6901 -- that we produce all the instance bodies we will need.
6903 if (Is_Entity_Name (Nam)
6904 and then Chars (Nam) = Chars (E))
6905 or else (Nkind (Nam) = N_Selected_Component
6906 and then Chars (Selector_Name (Nam)) = Chars (E))
6916 end In_Main_Context;
6918 ---------------------
6919 -- Inherit_Context --
6920 ---------------------
6922 procedure Inherit_Context (Gen_Decl : Node_Id; Inst : Node_Id) is
6923 Current_Context : List_Id;
6924 Current_Unit : Node_Id;
6929 if Nkind (Parent (Gen_Decl)) = N_Compilation_Unit then
6931 -- The inherited context is attached to the enclosing compilation
6932 -- unit. This is either the main unit, or the declaration for the
6933 -- main unit (in case the instantiation appears within the package
6934 -- declaration and the main unit is its body).
6936 Current_Unit := Parent (Inst);
6937 while Present (Current_Unit)
6938 and then Nkind (Current_Unit) /= N_Compilation_Unit
6940 Current_Unit := Parent (Current_Unit);
6943 Current_Context := Context_Items (Current_Unit);
6945 Item := First (Context_Items (Parent (Gen_Decl)));
6946 while Present (Item) loop
6947 if Nkind (Item) = N_With_Clause then
6949 -- Take care to prevent direct cyclic with's, which can happen
6950 -- if the generic body with's the current unit. Such a case
6951 -- would result in binder errors (or run-time errors if the
6952 -- -gnatE switch is in effect), but we want to prevent it here,
6953 -- because Sem.Walk_Library_Items doesn't like cycles. Note
6954 -- that we don't bother to detect indirect cycles.
6956 if Library_Unit (Item) /= Current_Unit then
6957 New_I := New_Copy (Item);
6958 Set_Implicit_With (New_I, True);
6959 Append (New_I, Current_Context);
6966 end Inherit_Context;
6972 procedure Initialize is
6974 Generic_Renamings.Init;
6977 Generic_Renamings_HTable.Reset;
6978 Circularity_Detected := False;
6979 Exchanged_Views := No_Elist;
6980 Hidden_Entities := No_Elist;
6983 ----------------------------
6984 -- Insert_After_Last_Decl --
6985 ----------------------------
6987 procedure Insert_After_Last_Decl (N : Node_Id; F_Node : Node_Id) is
6988 L : List_Id := List_Containing (N);
6989 P : constant Node_Id := Parent (L);
6992 if not Is_List_Member (F_Node) then
6993 if Nkind (P) = N_Package_Specification
6994 and then L = Visible_Declarations (P)
6995 and then Present (Private_Declarations (P))
6996 and then not Is_Empty_List (Private_Declarations (P))
6998 L := Private_Declarations (P);
7001 Insert_After (Last (L), F_Node);
7003 end Insert_After_Last_Decl;
7009 procedure Install_Body
7010 (Act_Body : Node_Id;
7015 Act_Id : constant Entity_Id := Corresponding_Spec (Act_Body);
7016 Act_Unit : constant Node_Id := Unit (Cunit (Get_Source_Unit (N)));
7017 Gen_Id : constant Entity_Id := Corresponding_Spec (Gen_Body);
7018 Par : constant Entity_Id := Scope (Gen_Id);
7019 Gen_Unit : constant Node_Id :=
7020 Unit (Cunit (Get_Source_Unit (Gen_Decl)));
7021 Orig_Body : Node_Id := Gen_Body;
7023 Body_Unit : Node_Id;
7025 Must_Delay : Boolean;
7027 function Enclosing_Subp (Id : Entity_Id) return Entity_Id;
7028 -- Find subprogram (if any) that encloses instance and/or generic body
7030 function True_Sloc (N : Node_Id) return Source_Ptr;
7031 -- If the instance is nested inside a generic unit, the Sloc of the
7032 -- instance indicates the place of the original definition, not the
7033 -- point of the current enclosing instance. Pending a better usage of
7034 -- Slocs to indicate instantiation places, we determine the place of
7035 -- origin of a node by finding the maximum sloc of any ancestor node.
7036 -- Why is this not equivalent to Top_Level_Location ???
7038 --------------------
7039 -- Enclosing_Subp --
7040 --------------------
7042 function Enclosing_Subp (Id : Entity_Id) return Entity_Id is
7043 Scop : Entity_Id := Scope (Id);
7046 while Scop /= Standard_Standard
7047 and then not Is_Overloadable (Scop)
7049 Scop := Scope (Scop);
7059 function True_Sloc (N : Node_Id) return Source_Ptr is
7066 while Present (N1) and then N1 /= Act_Unit loop
7067 if Sloc (N1) > Res then
7077 -- Start of processing for Install_Body
7081 -- If the body is a subunit, the freeze point is the corresponding
7082 -- stub in the current compilation, not the subunit itself.
7084 if Nkind (Parent (Gen_Body)) = N_Subunit then
7085 Orig_Body := Corresponding_Stub (Parent (Gen_Body));
7087 Orig_Body := Gen_Body;
7090 Body_Unit := Unit (Cunit (Get_Source_Unit (Orig_Body)));
7092 -- If the instantiation and the generic definition appear in the same
7093 -- package declaration, this is an early instantiation. If they appear
7094 -- in the same declarative part, it is an early instantiation only if
7095 -- the generic body appears textually later, and the generic body is
7096 -- also in the main unit.
7098 -- If instance is nested within a subprogram, and the generic body is
7099 -- not, the instance is delayed because the enclosing body is. If
7100 -- instance and body are within the same scope, or the same sub-
7101 -- program body, indicate explicitly that the instance is delayed.
7104 (Gen_Unit = Act_Unit
7105 and then (Nkind_In (Gen_Unit, N_Package_Declaration,
7106 N_Generic_Package_Declaration)
7107 or else (Gen_Unit = Body_Unit
7108 and then True_Sloc (N) < Sloc (Orig_Body)))
7109 and then Is_In_Main_Unit (Gen_Unit)
7110 and then (Scope (Act_Id) = Scope (Gen_Id)
7112 Enclosing_Subp (Act_Id) = Enclosing_Subp (Gen_Id)));
7114 -- If this is an early instantiation, the freeze node is placed after
7115 -- the generic body. Otherwise, if the generic appears in an instance,
7116 -- we cannot freeze the current instance until the outer one is frozen.
7117 -- This is only relevant if the current instance is nested within some
7118 -- inner scope not itself within the outer instance. If this scope is
7119 -- a package body in the same declarative part as the outer instance,
7120 -- then that body needs to be frozen after the outer instance. Finally,
7121 -- if no delay is needed, we place the freeze node at the end of the
7122 -- current declarative part.
7124 if Expander_Active then
7125 Ensure_Freeze_Node (Act_Id);
7126 F_Node := Freeze_Node (Act_Id);
7129 Insert_After (Orig_Body, F_Node);
7131 elsif Is_Generic_Instance (Par)
7132 and then Present (Freeze_Node (Par))
7133 and then Scope (Act_Id) /= Par
7135 -- Freeze instance of inner generic after instance of enclosing
7138 if In_Same_Declarative_Part (Freeze_Node (Par), N) then
7139 Insert_After (Freeze_Node (Par), F_Node);
7141 -- Freeze package enclosing instance of inner generic after
7142 -- instance of enclosing generic.
7144 elsif Nkind (Parent (N)) = N_Package_Body
7145 and then In_Same_Declarative_Part (Freeze_Node (Par), Parent (N))
7149 Enclosing : constant Entity_Id :=
7150 Corresponding_Spec (Parent (N));
7153 Insert_After_Last_Decl (N, F_Node);
7154 Ensure_Freeze_Node (Enclosing);
7156 if not Is_List_Member (Freeze_Node (Enclosing)) then
7157 Insert_After (Freeze_Node (Par), Freeze_Node (Enclosing));
7162 Insert_After_Last_Decl (N, F_Node);
7166 Insert_After_Last_Decl (N, F_Node);
7170 Set_Is_Frozen (Act_Id);
7171 Insert_Before (N, Act_Body);
7172 Mark_Rewrite_Insertion (Act_Body);
7175 -----------------------------
7176 -- Install_Formal_Packages --
7177 -----------------------------
7179 procedure Install_Formal_Packages (Par : Entity_Id) is
7182 Gen_E : Entity_Id := Empty;
7185 E := First_Entity (Par);
7187 -- In we are installing an instance parent, locate the formal packages
7188 -- of its generic parent.
7190 if Is_Generic_Instance (Par) then
7191 Gen := Generic_Parent (Specification (Unit_Declaration_Node (Par)));
7192 Gen_E := First_Entity (Gen);
7195 while Present (E) loop
7196 if Ekind (E) = E_Package
7197 and then Nkind (Parent (E)) = N_Package_Renaming_Declaration
7199 -- If this is the renaming for the parent instance, done
7201 if Renamed_Object (E) = Par then
7204 -- The visibility of a formal of an enclosing generic is already
7207 elsif Denotes_Formal_Package (E) then
7210 elsif Present (Associated_Formal_Package (E)) then
7211 Check_Generic_Actuals (Renamed_Object (E), True);
7212 Set_Is_Hidden (E, False);
7214 -- Find formal package in generic unit that corresponds to
7215 -- (instance of) formal package in instance.
7217 while Present (Gen_E) and then Chars (Gen_E) /= Chars (E) loop
7218 Next_Entity (Gen_E);
7221 if Present (Gen_E) then
7222 Map_Formal_Package_Entities (Gen_E, E);
7228 if Present (Gen_E) then
7229 Next_Entity (Gen_E);
7232 end Install_Formal_Packages;
7234 --------------------
7235 -- Install_Parent --
7236 --------------------
7238 procedure Install_Parent (P : Entity_Id; In_Body : Boolean := False) is
7239 Ancestors : constant Elist_Id := New_Elmt_List;
7240 S : constant Entity_Id := Current_Scope;
7241 Inst_Par : Entity_Id;
7242 First_Par : Entity_Id;
7243 Inst_Node : Node_Id;
7244 Gen_Par : Entity_Id;
7245 First_Gen : Entity_Id;
7248 procedure Install_Noninstance_Specs (Par : Entity_Id);
7249 -- Install the scopes of noninstance parent units ending with Par
7251 procedure Install_Spec (Par : Entity_Id);
7252 -- The child unit is within the declarative part of the parent, so
7253 -- the declarations within the parent are immediately visible.
7255 -------------------------------
7256 -- Install_Noninstance_Specs --
7257 -------------------------------
7259 procedure Install_Noninstance_Specs (Par : Entity_Id) is
7262 and then Par /= Standard_Standard
7263 and then not In_Open_Scopes (Par)
7265 Install_Noninstance_Specs (Scope (Par));
7268 end Install_Noninstance_Specs;
7274 procedure Install_Spec (Par : Entity_Id) is
7275 Spec : constant Node_Id :=
7276 Specification (Unit_Declaration_Node (Par));
7279 -- If this parent of the child instance is a top-level unit,
7280 -- then record the unit and its visibility for later resetting
7281 -- in Remove_Parent. We exclude units that are generic instances,
7282 -- as we only want to record this information for the ultimate
7283 -- top-level noninstance parent (is that always correct???).
7285 if Scope (Par) = Standard_Standard
7286 and then not Is_Generic_Instance (Par)
7288 Parent_Unit_Visible := Is_Immediately_Visible (Par);
7289 Instance_Parent_Unit := Par;
7292 -- Open the parent scope and make it and its declarations visible.
7293 -- If this point is not within a body, then only the visible
7294 -- declarations should be made visible, and installation of the
7295 -- private declarations is deferred until the appropriate point
7296 -- within analysis of the spec being instantiated (see the handling
7297 -- of parent visibility in Analyze_Package_Specification). This is
7298 -- relaxed in the case where the parent unit is Ada.Tags, to avoid
7299 -- private view problems that occur when compiling instantiations of
7300 -- a generic child of that package (Generic_Dispatching_Constructor).
7301 -- If the instance freezes a tagged type, inlinings of operations
7302 -- from Ada.Tags may need the full view of type Tag. If inlining took
7303 -- proper account of establishing visibility of inlined subprograms'
7304 -- parents then it should be possible to remove this
7305 -- special check. ???
7308 Set_Is_Immediately_Visible (Par);
7309 Install_Visible_Declarations (Par);
7310 Set_Use (Visible_Declarations (Spec));
7312 if In_Body or else Is_RTU (Par, Ada_Tags) then
7313 Install_Private_Declarations (Par);
7314 Set_Use (Private_Declarations (Spec));
7318 -- Start of processing for Install_Parent
7321 -- We need to install the parent instance to compile the instantiation
7322 -- of the child, but the child instance must appear in the current
7323 -- scope. Given that we cannot place the parent above the current scope
7324 -- in the scope stack, we duplicate the current scope and unstack both
7325 -- after the instantiation is complete.
7327 -- If the parent is itself the instantiation of a child unit, we must
7328 -- also stack the instantiation of its parent, and so on. Each such
7329 -- ancestor is the prefix of the name in a prior instantiation.
7331 -- If this is a nested instance, the parent unit itself resolves to
7332 -- a renaming of the parent instance, whose declaration we need.
7334 -- Finally, the parent may be a generic (not an instance) when the
7335 -- child unit appears as a formal package.
7339 if Present (Renamed_Entity (Inst_Par)) then
7340 Inst_Par := Renamed_Entity (Inst_Par);
7343 First_Par := Inst_Par;
7346 Generic_Parent (Specification (Unit_Declaration_Node (Inst_Par)));
7348 First_Gen := Gen_Par;
7350 while Present (Gen_Par)
7351 and then Is_Child_Unit (Gen_Par)
7353 -- Load grandparent instance as well
7355 Inst_Node := Get_Package_Instantiation_Node (Inst_Par);
7357 if Nkind (Name (Inst_Node)) = N_Expanded_Name then
7358 Inst_Par := Entity (Prefix (Name (Inst_Node)));
7360 if Present (Renamed_Entity (Inst_Par)) then
7361 Inst_Par := Renamed_Entity (Inst_Par);
7366 (Specification (Unit_Declaration_Node (Inst_Par)));
7368 if Present (Gen_Par) then
7369 Prepend_Elmt (Inst_Par, Ancestors);
7372 -- Parent is not the name of an instantiation
7374 Install_Noninstance_Specs (Inst_Par);
7386 if Present (First_Gen) then
7387 Append_Elmt (First_Par, Ancestors);
7390 Install_Noninstance_Specs (First_Par);
7393 if not Is_Empty_Elmt_List (Ancestors) then
7394 Elmt := First_Elmt (Ancestors);
7396 while Present (Elmt) loop
7397 Install_Spec (Node (Elmt));
7398 Install_Formal_Packages (Node (Elmt));
7409 --------------------------------
7410 -- Instantiate_Formal_Package --
7411 --------------------------------
7413 function Instantiate_Formal_Package
7416 Analyzed_Formal : Node_Id) return List_Id
7418 Loc : constant Source_Ptr := Sloc (Actual);
7419 Actual_Pack : Entity_Id;
7420 Formal_Pack : Entity_Id;
7421 Gen_Parent : Entity_Id;
7424 Parent_Spec : Node_Id;
7426 procedure Find_Matching_Actual
7428 Act : in out Entity_Id);
7429 -- We need to associate each formal entity in the formal package
7430 -- with the corresponding entity in the actual package. The actual
7431 -- package has been analyzed and possibly expanded, and as a result
7432 -- there is no one-to-one correspondence between the two lists (for
7433 -- example, the actual may include subtypes, itypes, and inherited
7434 -- primitive operations, interspersed among the renaming declarations
7435 -- for the actuals) . We retrieve the corresponding actual by name
7436 -- because each actual has the same name as the formal, and they do
7437 -- appear in the same order.
7439 function Get_Formal_Entity (N : Node_Id) return Entity_Id;
7440 -- Retrieve entity of defining entity of generic formal parameter.
7441 -- Only the declarations of formals need to be considered when
7442 -- linking them to actuals, but the declarative list may include
7443 -- internal entities generated during analysis, and those are ignored.
7445 procedure Match_Formal_Entity
7446 (Formal_Node : Node_Id;
7447 Formal_Ent : Entity_Id;
7448 Actual_Ent : Entity_Id);
7449 -- Associates the formal entity with the actual. In the case
7450 -- where Formal_Ent is a formal package, this procedure iterates
7451 -- through all of its formals and enters associations between the
7452 -- actuals occurring in the formal package's corresponding actual
7453 -- package (given by Actual_Ent) and the formal package's formal
7454 -- parameters. This procedure recurses if any of the parameters is
7455 -- itself a package.
7457 function Is_Instance_Of
7458 (Act_Spec : Entity_Id;
7459 Gen_Anc : Entity_Id) return Boolean;
7460 -- The actual can be an instantiation of a generic within another
7461 -- instance, in which case there is no direct link from it to the
7462 -- original generic ancestor. In that case, we recognize that the
7463 -- ultimate ancestor is the same by examining names and scopes.
7465 procedure Process_Nested_Formal (Formal : Entity_Id);
7466 -- If the current formal is declared with a box, its own formals are
7467 -- visible in the instance, as they were in the generic, and their
7468 -- Hidden flag must be reset. If some of these formals are themselves
7469 -- packages declared with a box, the processing must be recursive.
7471 --------------------------
7472 -- Find_Matching_Actual --
7473 --------------------------
7475 procedure Find_Matching_Actual
7477 Act : in out Entity_Id)
7479 Formal_Ent : Entity_Id;
7482 case Nkind (Original_Node (F)) is
7483 when N_Formal_Object_Declaration |
7484 N_Formal_Type_Declaration =>
7485 Formal_Ent := Defining_Identifier (F);
7487 while Chars (Act) /= Chars (Formal_Ent) loop
7491 when N_Formal_Subprogram_Declaration |
7492 N_Formal_Package_Declaration |
7493 N_Package_Declaration |
7494 N_Generic_Package_Declaration =>
7495 Formal_Ent := Defining_Entity (F);
7497 while Chars (Act) /= Chars (Formal_Ent) loop
7502 raise Program_Error;
7504 end Find_Matching_Actual;
7506 -------------------------
7507 -- Match_Formal_Entity --
7508 -------------------------
7510 procedure Match_Formal_Entity
7511 (Formal_Node : Node_Id;
7512 Formal_Ent : Entity_Id;
7513 Actual_Ent : Entity_Id)
7515 Act_Pkg : Entity_Id;
7518 Set_Instance_Of (Formal_Ent, Actual_Ent);
7520 if Ekind (Actual_Ent) = E_Package then
7522 -- Record associations for each parameter
7524 Act_Pkg := Actual_Ent;
7527 A_Ent : Entity_Id := First_Entity (Act_Pkg);
7536 -- Retrieve the actual given in the formal package declaration
7538 Actual := Entity (Name (Original_Node (Formal_Node)));
7540 -- The actual in the formal package declaration may be a
7541 -- renamed generic package, in which case we want to retrieve
7542 -- the original generic in order to traverse its formal part.
7544 if Present (Renamed_Entity (Actual)) then
7545 Gen_Decl := Unit_Declaration_Node (Renamed_Entity (Actual));
7547 Gen_Decl := Unit_Declaration_Node (Actual);
7550 Formals := Generic_Formal_Declarations (Gen_Decl);
7552 if Present (Formals) then
7553 F_Node := First_Non_Pragma (Formals);
7558 while Present (A_Ent)
7559 and then Present (F_Node)
7560 and then A_Ent /= First_Private_Entity (Act_Pkg)
7562 F_Ent := Get_Formal_Entity (F_Node);
7564 if Present (F_Ent) then
7566 -- This is a formal of the original package. Record
7567 -- association and recurse.
7569 Find_Matching_Actual (F_Node, A_Ent);
7570 Match_Formal_Entity (F_Node, F_Ent, A_Ent);
7571 Next_Entity (A_Ent);
7574 Next_Non_Pragma (F_Node);
7578 end Match_Formal_Entity;
7580 -----------------------
7581 -- Get_Formal_Entity --
7582 -----------------------
7584 function Get_Formal_Entity (N : Node_Id) return Entity_Id is
7585 Kind : constant Node_Kind := Nkind (Original_Node (N));
7588 when N_Formal_Object_Declaration =>
7589 return Defining_Identifier (N);
7591 when N_Formal_Type_Declaration =>
7592 return Defining_Identifier (N);
7594 when N_Formal_Subprogram_Declaration =>
7595 return Defining_Unit_Name (Specification (N));
7597 when N_Formal_Package_Declaration =>
7598 return Defining_Identifier (Original_Node (N));
7600 when N_Generic_Package_Declaration =>
7601 return Defining_Identifier (Original_Node (N));
7603 -- All other declarations are introduced by semantic analysis and
7604 -- have no match in the actual.
7609 end Get_Formal_Entity;
7611 --------------------
7612 -- Is_Instance_Of --
7613 --------------------
7615 function Is_Instance_Of
7616 (Act_Spec : Entity_Id;
7617 Gen_Anc : Entity_Id) return Boolean
7619 Gen_Par : constant Entity_Id := Generic_Parent (Act_Spec);
7622 if No (Gen_Par) then
7625 -- Simplest case: the generic parent of the actual is the formal
7627 elsif Gen_Par = Gen_Anc then
7630 elsif Chars (Gen_Par) /= Chars (Gen_Anc) then
7633 -- The actual may be obtained through several instantiations. Its
7634 -- scope must itself be an instance of a generic declared in the
7635 -- same scope as the formal. Any other case is detected above.
7637 elsif not Is_Generic_Instance (Scope (Gen_Par)) then
7641 return Generic_Parent (Parent (Scope (Gen_Par))) = Scope (Gen_Anc);
7645 ---------------------------
7646 -- Process_Nested_Formal --
7647 ---------------------------
7649 procedure Process_Nested_Formal (Formal : Entity_Id) is
7653 if Present (Associated_Formal_Package (Formal))
7654 and then Box_Present (Parent (Associated_Formal_Package (Formal)))
7656 Ent := First_Entity (Formal);
7657 while Present (Ent) loop
7658 Set_Is_Hidden (Ent, False);
7659 Set_Is_Visible_Formal (Ent);
7660 Set_Is_Potentially_Use_Visible
7661 (Ent, Is_Potentially_Use_Visible (Formal));
7663 if Ekind (Ent) = E_Package then
7664 exit when Renamed_Entity (Ent) = Renamed_Entity (Formal);
7665 Process_Nested_Formal (Ent);
7671 end Process_Nested_Formal;
7673 -- Start of processing for Instantiate_Formal_Package
7678 if not Is_Entity_Name (Actual)
7679 or else Ekind (Entity (Actual)) /= E_Package
7682 ("expect package instance to instantiate formal", Actual);
7683 Abandon_Instantiation (Actual);
7684 raise Program_Error;
7687 Actual_Pack := Entity (Actual);
7688 Set_Is_Instantiated (Actual_Pack);
7690 -- The actual may be a renamed package, or an outer generic formal
7691 -- package whose instantiation is converted into a renaming.
7693 if Present (Renamed_Object (Actual_Pack)) then
7694 Actual_Pack := Renamed_Object (Actual_Pack);
7697 if Nkind (Analyzed_Formal) = N_Formal_Package_Declaration then
7698 Gen_Parent := Get_Instance_Of (Entity (Name (Analyzed_Formal)));
7699 Formal_Pack := Defining_Identifier (Analyzed_Formal);
7702 Generic_Parent (Specification (Analyzed_Formal));
7704 Defining_Unit_Name (Specification (Analyzed_Formal));
7707 if Nkind (Parent (Actual_Pack)) = N_Defining_Program_Unit_Name then
7708 Parent_Spec := Specification (Unit_Declaration_Node (Actual_Pack));
7710 Parent_Spec := Parent (Actual_Pack);
7713 if Gen_Parent = Any_Id then
7715 ("previous error in declaration of formal package", Actual);
7716 Abandon_Instantiation (Actual);
7719 Is_Instance_Of (Parent_Spec, Get_Instance_Of (Gen_Parent))
7725 ("actual parameter must be instance of&", Actual, Gen_Parent);
7726 Abandon_Instantiation (Actual);
7729 Set_Instance_Of (Defining_Identifier (Formal), Actual_Pack);
7730 Map_Formal_Package_Entities (Formal_Pack, Actual_Pack);
7733 Make_Package_Renaming_Declaration (Loc,
7734 Defining_Unit_Name => New_Copy (Defining_Identifier (Formal)),
7735 Name => New_Reference_To (Actual_Pack, Loc));
7737 Set_Associated_Formal_Package (Defining_Unit_Name (Nod),
7738 Defining_Identifier (Formal));
7739 Decls := New_List (Nod);
7741 -- If the formal F has a box, then the generic declarations are
7742 -- visible in the generic G. In an instance of G, the corresponding
7743 -- entities in the actual for F (which are the actuals for the
7744 -- instantiation of the generic that F denotes) must also be made
7745 -- visible for analysis of the current instance. On exit from the
7746 -- current instance, those entities are made private again. If the
7747 -- actual is currently in use, these entities are also use-visible.
7749 -- The loop through the actual entities also steps through the formal
7750 -- entities and enters associations from formals to actuals into the
7751 -- renaming map. This is necessary to properly handle checking of
7752 -- actual parameter associations for later formals that depend on
7753 -- actuals declared in the formal package.
7755 -- In Ada 2005, partial parametrization requires that we make visible
7756 -- the actuals corresponding to formals that were defaulted in the
7757 -- formal package. There formals are identified because they remain
7758 -- formal generics within the formal package, rather than being
7759 -- renamings of the actuals supplied.
7762 Gen_Decl : constant Node_Id :=
7763 Unit_Declaration_Node (Gen_Parent);
7764 Formals : constant List_Id :=
7765 Generic_Formal_Declarations (Gen_Decl);
7767 Actual_Ent : Entity_Id;
7768 Actual_Of_Formal : Node_Id;
7769 Formal_Node : Node_Id;
7770 Formal_Ent : Entity_Id;
7773 if Present (Formals) then
7774 Formal_Node := First_Non_Pragma (Formals);
7776 Formal_Node := Empty;
7779 Actual_Ent := First_Entity (Actual_Pack);
7781 First (Visible_Declarations (Specification (Analyzed_Formal)));
7782 while Present (Actual_Ent)
7783 and then Actual_Ent /= First_Private_Entity (Actual_Pack)
7785 if Present (Formal_Node) then
7786 Formal_Ent := Get_Formal_Entity (Formal_Node);
7788 if Present (Formal_Ent) then
7789 Find_Matching_Actual (Formal_Node, Actual_Ent);
7791 (Formal_Node, Formal_Ent, Actual_Ent);
7793 -- We iterate at the same time over the actuals of the
7794 -- local package created for the formal, to determine
7795 -- which one of the formals of the original generic were
7796 -- defaulted in the formal. The corresponding actual
7797 -- entities are visible in the enclosing instance.
7799 if Box_Present (Formal)
7801 (Present (Actual_Of_Formal)
7804 (Get_Formal_Entity (Actual_Of_Formal)))
7806 Set_Is_Hidden (Actual_Ent, False);
7807 Set_Is_Visible_Formal (Actual_Ent);
7808 Set_Is_Potentially_Use_Visible
7809 (Actual_Ent, In_Use (Actual_Pack));
7811 if Ekind (Actual_Ent) = E_Package then
7812 Process_Nested_Formal (Actual_Ent);
7816 Set_Is_Hidden (Actual_Ent);
7817 Set_Is_Potentially_Use_Visible (Actual_Ent, False);
7821 Next_Non_Pragma (Formal_Node);
7822 Next (Actual_Of_Formal);
7825 -- No further formals to match, but the generic part may
7826 -- contain inherited operation that are not hidden in the
7827 -- enclosing instance.
7829 Next_Entity (Actual_Ent);
7833 -- Inherited subprograms generated by formal derived types are
7834 -- also visible if the types are.
7836 Actual_Ent := First_Entity (Actual_Pack);
7837 while Present (Actual_Ent)
7838 and then Actual_Ent /= First_Private_Entity (Actual_Pack)
7840 if Is_Overloadable (Actual_Ent)
7842 Nkind (Parent (Actual_Ent)) = N_Subtype_Declaration
7844 not Is_Hidden (Defining_Identifier (Parent (Actual_Ent)))
7846 Set_Is_Hidden (Actual_Ent, False);
7847 Set_Is_Potentially_Use_Visible
7848 (Actual_Ent, In_Use (Actual_Pack));
7851 Next_Entity (Actual_Ent);
7855 -- If the formal is not declared with a box, reanalyze it as an
7856 -- abbreviated instantiation, to verify the matching rules of 12.7.
7857 -- The actual checks are performed after the generic associations
7858 -- have been analyzed, to guarantee the same visibility for this
7859 -- instantiation and for the actuals.
7861 -- In Ada 2005, the generic associations for the formal can include
7862 -- defaulted parameters. These are ignored during check. This
7863 -- internal instantiation is removed from the tree after conformance
7864 -- checking, because it contains formal declarations for those
7865 -- defaulted parameters, and those should not reach the back-end.
7867 if not Box_Present (Formal) then
7869 I_Pack : constant Entity_Id :=
7870 Make_Defining_Identifier (Sloc (Actual),
7871 Chars => New_Internal_Name ('P'));
7874 Set_Is_Internal (I_Pack);
7877 Make_Package_Instantiation (Sloc (Actual),
7878 Defining_Unit_Name => I_Pack,
7881 (Get_Instance_Of (Gen_Parent), Sloc (Actual)),
7882 Generic_Associations =>
7883 Generic_Associations (Formal)));
7889 end Instantiate_Formal_Package;
7891 -----------------------------------
7892 -- Instantiate_Formal_Subprogram --
7893 -----------------------------------
7895 function Instantiate_Formal_Subprogram
7898 Analyzed_Formal : Node_Id) return Node_Id
7901 Formal_Sub : constant Entity_Id :=
7902 Defining_Unit_Name (Specification (Formal));
7903 Analyzed_S : constant Entity_Id :=
7904 Defining_Unit_Name (Specification (Analyzed_Formal));
7905 Decl_Node : Node_Id;
7909 function From_Parent_Scope (Subp : Entity_Id) return Boolean;
7910 -- If the generic is a child unit, the parent has been installed on the
7911 -- scope stack, but a default subprogram cannot resolve to something on
7912 -- the parent because that parent is not really part of the visible
7913 -- context (it is there to resolve explicit local entities). If the
7914 -- default has resolved in this way, we remove the entity from
7915 -- immediate visibility and analyze the node again to emit an error
7916 -- message or find another visible candidate.
7918 procedure Valid_Actual_Subprogram (Act : Node_Id);
7919 -- Perform legality check and raise exception on failure
7921 -----------------------
7922 -- From_Parent_Scope --
7923 -----------------------
7925 function From_Parent_Scope (Subp : Entity_Id) return Boolean is
7926 Gen_Scope : Node_Id;
7929 Gen_Scope := Scope (Analyzed_S);
7930 while Present (Gen_Scope)
7931 and then Is_Child_Unit (Gen_Scope)
7933 if Scope (Subp) = Scope (Gen_Scope) then
7937 Gen_Scope := Scope (Gen_Scope);
7941 end From_Parent_Scope;
7943 -----------------------------
7944 -- Valid_Actual_Subprogram --
7945 -----------------------------
7947 procedure Valid_Actual_Subprogram (Act : Node_Id) is
7951 if Is_Entity_Name (Act) then
7952 Act_E := Entity (Act);
7954 elsif Nkind (Act) = N_Selected_Component
7955 and then Is_Entity_Name (Selector_Name (Act))
7957 Act_E := Entity (Selector_Name (Act));
7963 if (Present (Act_E) and then Is_Overloadable (Act_E))
7964 or else Nkind_In (Act, N_Attribute_Reference,
7965 N_Indexed_Component,
7966 N_Character_Literal,
7967 N_Explicit_Dereference)
7973 ("expect subprogram or entry name in instantiation of&",
7974 Instantiation_Node, Formal_Sub);
7975 Abandon_Instantiation (Instantiation_Node);
7977 end Valid_Actual_Subprogram;
7979 -- Start of processing for Instantiate_Formal_Subprogram
7982 New_Spec := New_Copy_Tree (Specification (Formal));
7984 -- The tree copy has created the proper instantiation sloc for the
7985 -- new specification. Use this location for all other constructed
7988 Loc := Sloc (Defining_Unit_Name (New_Spec));
7990 -- Create new entity for the actual (New_Copy_Tree does not)
7992 Set_Defining_Unit_Name
7993 (New_Spec, Make_Defining_Identifier (Loc, Chars (Formal_Sub)));
7995 -- Create new entities for the each of the formals in the
7996 -- specification of the renaming declaration built for the actual.
7998 if Present (Parameter_Specifications (New_Spec)) then
8002 F := First (Parameter_Specifications (New_Spec));
8003 while Present (F) loop
8004 Set_Defining_Identifier (F,
8005 Make_Defining_Identifier (Sloc (F),
8006 Chars => Chars (Defining_Identifier (F))));
8012 -- Find entity of actual. If the actual is an attribute reference, it
8013 -- cannot be resolved here (its formal is missing) but is handled
8014 -- instead in Attribute_Renaming. If the actual is overloaded, it is
8015 -- fully resolved subsequently, when the renaming declaration for the
8016 -- formal is analyzed. If it is an explicit dereference, resolve the
8017 -- prefix but not the actual itself, to prevent interpretation as call.
8019 if Present (Actual) then
8020 Loc := Sloc (Actual);
8021 Set_Sloc (New_Spec, Loc);
8023 if Nkind (Actual) = N_Operator_Symbol then
8024 Find_Direct_Name (Actual);
8026 elsif Nkind (Actual) = N_Explicit_Dereference then
8027 Analyze (Prefix (Actual));
8029 elsif Nkind (Actual) /= N_Attribute_Reference then
8033 Valid_Actual_Subprogram (Actual);
8036 elsif Present (Default_Name (Formal)) then
8037 if not Nkind_In (Default_Name (Formal), N_Attribute_Reference,
8038 N_Selected_Component,
8039 N_Indexed_Component,
8040 N_Character_Literal)
8041 and then Present (Entity (Default_Name (Formal)))
8043 Nam := New_Occurrence_Of (Entity (Default_Name (Formal)), Loc);
8045 Nam := New_Copy (Default_Name (Formal));
8046 Set_Sloc (Nam, Loc);
8049 elsif Box_Present (Formal) then
8051 -- Actual is resolved at the point of instantiation. Create an
8052 -- identifier or operator with the same name as the formal.
8054 if Nkind (Formal_Sub) = N_Defining_Operator_Symbol then
8055 Nam := Make_Operator_Symbol (Loc,
8056 Chars => Chars (Formal_Sub),
8057 Strval => No_String);
8059 Nam := Make_Identifier (Loc, Chars (Formal_Sub));
8062 elsif Nkind (Specification (Formal)) = N_Procedure_Specification
8063 and then Null_Present (Specification (Formal))
8065 -- Generate null body for procedure, for use in the instance
8068 Make_Subprogram_Body (Loc,
8069 Specification => New_Spec,
8070 Declarations => New_List,
8071 Handled_Statement_Sequence =>
8072 Make_Handled_Sequence_Of_Statements (Loc,
8073 Statements => New_List (Make_Null_Statement (Loc))));
8075 Set_Is_Intrinsic_Subprogram (Defining_Unit_Name (New_Spec));
8079 Error_Msg_Sloc := Sloc (Scope (Analyzed_S));
8081 ("missing actual&", Instantiation_Node, Formal_Sub);
8083 ("\in instantiation of & declared#",
8084 Instantiation_Node, Scope (Analyzed_S));
8085 Abandon_Instantiation (Instantiation_Node);
8089 Make_Subprogram_Renaming_Declaration (Loc,
8090 Specification => New_Spec,
8093 -- If we do not have an actual and the formal specified <> then set to
8094 -- get proper default.
8096 if No (Actual) and then Box_Present (Formal) then
8097 Set_From_Default (Decl_Node);
8100 -- Gather possible interpretations for the actual before analyzing the
8101 -- instance. If overloaded, it will be resolved when analyzing the
8102 -- renaming declaration.
8104 if Box_Present (Formal)
8105 and then No (Actual)
8109 if Is_Child_Unit (Scope (Analyzed_S))
8110 and then Present (Entity (Nam))
8112 if not Is_Overloaded (Nam) then
8114 if From_Parent_Scope (Entity (Nam)) then
8115 Set_Is_Immediately_Visible (Entity (Nam), False);
8116 Set_Entity (Nam, Empty);
8117 Set_Etype (Nam, Empty);
8121 Set_Is_Immediately_Visible (Entity (Nam));
8130 Get_First_Interp (Nam, I, It);
8132 while Present (It.Nam) loop
8133 if From_Parent_Scope (It.Nam) then
8137 Get_Next_Interp (I, It);
8144 -- The generic instantiation freezes the actual. This can only be done
8145 -- once the actual is resolved, in the analysis of the renaming
8146 -- declaration. To make the formal subprogram entity available, we set
8147 -- Corresponding_Formal_Spec to point to the formal subprogram entity.
8148 -- This is also needed in Analyze_Subprogram_Renaming for the processing
8149 -- of formal abstract subprograms.
8151 Set_Corresponding_Formal_Spec (Decl_Node, Analyzed_S);
8153 -- We cannot analyze the renaming declaration, and thus find the actual,
8154 -- until all the actuals are assembled in the instance. For subsequent
8155 -- checks of other actuals, indicate the node that will hold the
8156 -- instance of this formal.
8158 Set_Instance_Of (Analyzed_S, Nam);
8160 if Nkind (Actual) = N_Selected_Component
8161 and then Is_Task_Type (Etype (Prefix (Actual)))
8162 and then not Is_Frozen (Etype (Prefix (Actual)))
8164 -- The renaming declaration will create a body, which must appear
8165 -- outside of the instantiation, We move the renaming declaration
8166 -- out of the instance, and create an additional renaming inside,
8167 -- to prevent freezing anomalies.
8170 Anon_Id : constant Entity_Id :=
8171 Make_Defining_Identifier
8172 (Loc, New_Internal_Name ('E'));
8174 Set_Defining_Unit_Name (New_Spec, Anon_Id);
8175 Insert_Before (Instantiation_Node, Decl_Node);
8176 Analyze (Decl_Node);
8178 -- Now create renaming within the instance
8181 Make_Subprogram_Renaming_Declaration (Loc,
8182 Specification => New_Copy_Tree (New_Spec),
8183 Name => New_Occurrence_Of (Anon_Id, Loc));
8185 Set_Defining_Unit_Name (Specification (Decl_Node),
8186 Make_Defining_Identifier (Loc, Chars (Formal_Sub)));
8191 end Instantiate_Formal_Subprogram;
8193 ------------------------
8194 -- Instantiate_Object --
8195 ------------------------
8197 function Instantiate_Object
8200 Analyzed_Formal : Node_Id) return List_Id
8202 Acc_Def : Node_Id := Empty;
8203 Act_Assoc : constant Node_Id := Parent (Actual);
8204 Actual_Decl : Node_Id := Empty;
8205 Formal_Id : constant Entity_Id := Defining_Identifier (Formal);
8206 Decl_Node : Node_Id;
8209 List : constant List_Id := New_List;
8210 Loc : constant Source_Ptr := Sloc (Actual);
8211 Orig_Ftyp : constant Entity_Id :=
8212 Etype (Defining_Identifier (Analyzed_Formal));
8213 Subt_Decl : Node_Id := Empty;
8214 Subt_Mark : Node_Id := Empty;
8217 if Present (Subtype_Mark (Formal)) then
8218 Subt_Mark := Subtype_Mark (Formal);
8220 Check_Access_Definition (Formal);
8221 Acc_Def := Access_Definition (Formal);
8224 -- Sloc for error message on missing actual
8226 Error_Msg_Sloc := Sloc (Scope (Defining_Identifier (Analyzed_Formal)));
8228 if Get_Instance_Of (Formal_Id) /= Formal_Id then
8229 Error_Msg_N ("duplicate instantiation of generic parameter", Actual);
8232 Set_Parent (List, Parent (Actual));
8236 if Out_Present (Formal) then
8238 -- An IN OUT generic actual must be a name. The instantiation is a
8239 -- renaming declaration. The actual is the name being renamed. We
8240 -- use the actual directly, rather than a copy, because it is not
8241 -- used further in the list of actuals, and because a copy or a use
8242 -- of relocate_node is incorrect if the instance is nested within a
8243 -- generic. In order to simplify ASIS searches, the Generic_Parent
8244 -- field links the declaration to the generic association.
8249 Instantiation_Node, Formal_Id);
8251 ("\in instantiation of & declared#",
8253 Scope (Defining_Identifier (Analyzed_Formal)));
8254 Abandon_Instantiation (Instantiation_Node);
8257 if Present (Subt_Mark) then
8259 Make_Object_Renaming_Declaration (Loc,
8260 Defining_Identifier => New_Copy (Formal_Id),
8261 Subtype_Mark => New_Copy_Tree (Subt_Mark),
8264 else pragma Assert (Present (Acc_Def));
8266 Make_Object_Renaming_Declaration (Loc,
8267 Defining_Identifier => New_Copy (Formal_Id),
8268 Access_Definition => New_Copy_Tree (Acc_Def),
8272 Set_Corresponding_Generic_Association (Decl_Node, Act_Assoc);
8274 -- The analysis of the actual may produce insert_action nodes, so
8275 -- the declaration must have a context in which to attach them.
8277 Append (Decl_Node, List);
8280 -- Return if the analysis of the actual reported some error
8282 if Etype (Actual) = Any_Type then
8286 -- This check is performed here because Analyze_Object_Renaming will
8287 -- not check it when Comes_From_Source is False. Note though that the
8288 -- check for the actual being the name of an object will be performed
8289 -- in Analyze_Object_Renaming.
8291 if Is_Object_Reference (Actual)
8292 and then Is_Dependent_Component_Of_Mutable_Object (Actual)
8295 ("illegal discriminant-dependent component for in out parameter",
8299 -- The actual has to be resolved in order to check that it is a
8300 -- variable (due to cases such as F(1), where F returns
8301 -- access to an array, and for overloaded prefixes).
8304 Get_Instance_Of (Etype (Defining_Identifier (Analyzed_Formal)));
8306 if Is_Private_Type (Ftyp)
8307 and then not Is_Private_Type (Etype (Actual))
8308 and then (Base_Type (Full_View (Ftyp)) = Base_Type (Etype (Actual))
8309 or else Base_Type (Etype (Actual)) = Ftyp)
8311 -- If the actual has the type of the full view of the formal, or
8312 -- else a non-private subtype of the formal, then the visibility
8313 -- of the formal type has changed. Add to the actuals a subtype
8314 -- declaration that will force the exchange of views in the body
8315 -- of the instance as well.
8318 Make_Subtype_Declaration (Loc,
8319 Defining_Identifier =>
8320 Make_Defining_Identifier (Loc, New_Internal_Name ('P')),
8321 Subtype_Indication => New_Occurrence_Of (Ftyp, Loc));
8323 Prepend (Subt_Decl, List);
8325 Prepend_Elmt (Full_View (Ftyp), Exchanged_Views);
8326 Exchange_Declarations (Ftyp);
8329 Resolve (Actual, Ftyp);
8331 if not Denotes_Variable (Actual) then
8333 ("actual for& must be a variable", Actual, Formal_Id);
8335 elsif Base_Type (Ftyp) /= Base_Type (Etype (Actual)) then
8337 -- Ada 2005 (AI-423): For a generic formal object of mode in out,
8338 -- the type of the actual shall resolve to a specific anonymous
8341 if Ada_Version < Ada_05
8343 Ekind (Base_Type (Ftyp)) /=
8344 E_Anonymous_Access_Type
8346 Ekind (Base_Type (Etype (Actual))) /=
8347 E_Anonymous_Access_Type
8349 Error_Msg_NE ("type of actual does not match type of&",
8354 Note_Possible_Modification (Actual, Sure => True);
8356 -- Check for instantiation of atomic/volatile actual for
8357 -- non-atomic/volatile formal (RM C.6 (12)).
8359 if Is_Atomic_Object (Actual)
8360 and then not Is_Atomic (Orig_Ftyp)
8363 ("cannot instantiate non-atomic formal object " &
8364 "with atomic actual", Actual);
8366 elsif Is_Volatile_Object (Actual)
8367 and then not Is_Volatile (Orig_Ftyp)
8370 ("cannot instantiate non-volatile formal object " &
8371 "with volatile actual", Actual);
8374 -- Formal in-parameter
8377 -- The instantiation of a generic formal in-parameter is constant
8378 -- declaration. The actual is the expression for that declaration.
8380 if Present (Actual) then
8381 if Present (Subt_Mark) then
8383 else pragma Assert (Present (Acc_Def));
8388 Make_Object_Declaration (Loc,
8389 Defining_Identifier => New_Copy (Formal_Id),
8390 Constant_Present => True,
8391 Null_Exclusion_Present => Null_Exclusion_Present (Formal),
8392 Object_Definition => New_Copy_Tree (Def),
8393 Expression => Actual);
8395 Set_Corresponding_Generic_Association (Decl_Node, Act_Assoc);
8397 -- A generic formal object of a tagged type is defined to be
8398 -- aliased so the new constant must also be treated as aliased.
8401 (Etype (Defining_Identifier (Analyzed_Formal)))
8403 Set_Aliased_Present (Decl_Node);
8406 Append (Decl_Node, List);
8408 -- No need to repeat (pre-)analysis of some expression nodes
8409 -- already handled in Preanalyze_Actuals.
8411 if Nkind (Actual) /= N_Allocator then
8414 -- Return if the analysis of the actual reported some error
8416 if Etype (Actual) = Any_Type then
8422 Formal_Object : constant Entity_Id :=
8423 Defining_Identifier (Analyzed_Formal);
8424 Formal_Type : constant Entity_Id := Etype (Formal_Object);
8429 Typ := Get_Instance_Of (Formal_Type);
8431 Freeze_Before (Instantiation_Node, Typ);
8433 -- If the actual is an aggregate, perform name resolution on
8434 -- its components (the analysis of an aggregate does not do it)
8435 -- to capture local names that may be hidden if the generic is
8438 if Nkind (Actual) = N_Aggregate then
8439 Preanalyze_And_Resolve (Actual, Typ);
8442 if Is_Limited_Type (Typ)
8443 and then not OK_For_Limited_Init (Typ, Actual)
8446 ("initialization not allowed for limited types", Actual);
8447 Explain_Limited_Type (Typ, Actual);
8451 elsif Present (Default_Expression (Formal)) then
8453 -- Use default to construct declaration
8455 if Present (Subt_Mark) then
8457 else pragma Assert (Present (Acc_Def));
8462 Make_Object_Declaration (Sloc (Formal),
8463 Defining_Identifier => New_Copy (Formal_Id),
8464 Constant_Present => True,
8465 Null_Exclusion_Present => Null_Exclusion_Present (Formal),
8466 Object_Definition => New_Copy (Def),
8467 Expression => New_Copy_Tree
8468 (Default_Expression (Formal)));
8470 Append (Decl_Node, List);
8471 Set_Analyzed (Expression (Decl_Node), False);
8476 Instantiation_Node, Formal_Id);
8477 Error_Msg_NE ("\in instantiation of & declared#",
8479 Scope (Defining_Identifier (Analyzed_Formal)));
8482 (Etype (Defining_Identifier (Analyzed_Formal)))
8484 -- Create dummy constant declaration so that instance can be
8485 -- analyzed, to minimize cascaded visibility errors.
8487 if Present (Subt_Mark) then
8489 else pragma Assert (Present (Acc_Def));
8494 Make_Object_Declaration (Loc,
8495 Defining_Identifier => New_Copy (Formal_Id),
8496 Constant_Present => True,
8497 Null_Exclusion_Present => Null_Exclusion_Present (Formal),
8498 Object_Definition => New_Copy (Def),
8500 Make_Attribute_Reference (Sloc (Formal_Id),
8501 Attribute_Name => Name_First,
8502 Prefix => New_Copy (Def)));
8504 Append (Decl_Node, List);
8507 Abandon_Instantiation (Instantiation_Node);
8512 if Nkind (Actual) in N_Has_Entity then
8513 Actual_Decl := Parent (Entity (Actual));
8516 -- Ada 2005 (AI-423): For a formal object declaration with a null
8517 -- exclusion or an access definition that has a null exclusion: If the
8518 -- actual matching the formal object declaration denotes a generic
8519 -- formal object of another generic unit G, and the instantiation
8520 -- containing the actual occurs within the body of G or within the body
8521 -- of a generic unit declared within the declarative region of G, then
8522 -- the declaration of the formal object of G must have a null exclusion.
8523 -- Otherwise, the subtype of the actual matching the formal object
8524 -- declaration shall exclude null.
8526 if Ada_Version >= Ada_05
8527 and then Present (Actual_Decl)
8529 Nkind_In (Actual_Decl, N_Formal_Object_Declaration,
8530 N_Object_Declaration)
8531 and then Nkind (Analyzed_Formal) = N_Formal_Object_Declaration
8532 and then not Has_Null_Exclusion (Actual_Decl)
8533 and then Has_Null_Exclusion (Analyzed_Formal)
8535 Error_Msg_Sloc := Sloc (Analyzed_Formal);
8537 ("actual must exclude null to match generic formal#", Actual);
8541 end Instantiate_Object;
8543 ------------------------------
8544 -- Instantiate_Package_Body --
8545 ------------------------------
8547 procedure Instantiate_Package_Body
8548 (Body_Info : Pending_Body_Info;
8549 Inlined_Body : Boolean := False;
8550 Body_Optional : Boolean := False)
8552 Act_Decl : constant Node_Id := Body_Info.Act_Decl;
8553 Inst_Node : constant Node_Id := Body_Info.Inst_Node;
8554 Loc : constant Source_Ptr := Sloc (Inst_Node);
8556 Gen_Id : constant Node_Id := Name (Inst_Node);
8557 Gen_Unit : constant Entity_Id := Get_Generic_Entity (Inst_Node);
8558 Gen_Decl : constant Node_Id := Unit_Declaration_Node (Gen_Unit);
8559 Act_Spec : constant Node_Id := Specification (Act_Decl);
8560 Act_Decl_Id : constant Entity_Id := Defining_Entity (Act_Spec);
8562 Act_Body_Name : Node_Id;
8564 Gen_Body_Id : Node_Id;
8566 Act_Body_Id : Entity_Id;
8568 Parent_Installed : Boolean := False;
8569 Save_Style_Check : constant Boolean := Style_Check;
8571 Par_Ent : Entity_Id := Empty;
8572 Par_Vis : Boolean := False;
8575 Gen_Body_Id := Corresponding_Body (Gen_Decl);
8577 -- The instance body may already have been processed, as the parent of
8578 -- another instance that is inlined (Load_Parent_Of_Generic).
8580 if Present (Corresponding_Body (Instance_Spec (Inst_Node))) then
8584 Expander_Mode_Save_And_Set (Body_Info.Expander_Status);
8586 -- Re-establish the state of information on which checks are suppressed.
8587 -- This information was set in Body_Info at the point of instantiation,
8588 -- and now we restore it so that the instance is compiled using the
8589 -- check status at the instantiation (RM 11.5 (7.2/2), AI95-00224-01).
8591 Local_Suppress_Stack_Top := Body_Info.Local_Suppress_Stack_Top;
8592 Scope_Suppress := Body_Info.Scope_Suppress;
8594 if No (Gen_Body_Id) then
8595 Load_Parent_Of_Generic
8596 (Inst_Node, Specification (Gen_Decl), Body_Optional);
8597 Gen_Body_Id := Corresponding_Body (Gen_Decl);
8600 Mark_Context (Act_Decl, Gen_Decl);
8602 -- Establish global variable for sloc adjustment and for error recovery
8604 Instantiation_Node := Inst_Node;
8606 if Present (Gen_Body_Id) then
8607 Save_Env (Gen_Unit, Act_Decl_Id);
8608 Style_Check := False;
8609 Current_Sem_Unit := Body_Info.Current_Sem_Unit;
8611 Gen_Body := Unit_Declaration_Node (Gen_Body_Id);
8613 Create_Instantiation_Source
8614 (Inst_Node, Gen_Body_Id, False, S_Adjustment);
8618 (Original_Node (Gen_Body), Empty, Instantiating => True);
8620 -- Build new name (possibly qualified) for body declaration
8622 Act_Body_Id := New_Copy (Act_Decl_Id);
8624 -- Some attributes of spec entity are not inherited by body entity
8626 Set_Handler_Records (Act_Body_Id, No_List);
8628 if Nkind (Defining_Unit_Name (Act_Spec)) =
8629 N_Defining_Program_Unit_Name
8632 Make_Defining_Program_Unit_Name (Loc,
8633 Name => New_Copy_Tree (Name (Defining_Unit_Name (Act_Spec))),
8634 Defining_Identifier => Act_Body_Id);
8636 Act_Body_Name := Act_Body_Id;
8639 Set_Defining_Unit_Name (Act_Body, Act_Body_Name);
8641 Set_Corresponding_Spec (Act_Body, Act_Decl_Id);
8642 Check_Generic_Actuals (Act_Decl_Id, False);
8644 -- If it is a child unit, make the parent instance (which is an
8645 -- instance of the parent of the generic) visible. The parent
8646 -- instance is the prefix of the name of the generic unit.
8648 if Ekind (Scope (Gen_Unit)) = E_Generic_Package
8649 and then Nkind (Gen_Id) = N_Expanded_Name
8651 Par_Ent := Entity (Prefix (Gen_Id));
8652 Par_Vis := Is_Immediately_Visible (Par_Ent);
8653 Install_Parent (Par_Ent, In_Body => True);
8654 Parent_Installed := True;
8656 elsif Is_Child_Unit (Gen_Unit) then
8657 Par_Ent := Scope (Gen_Unit);
8658 Par_Vis := Is_Immediately_Visible (Par_Ent);
8659 Install_Parent (Par_Ent, In_Body => True);
8660 Parent_Installed := True;
8663 -- If the instantiation is a library unit, and this is the main unit,
8664 -- then build the resulting compilation unit nodes for the instance.
8665 -- If this is a compilation unit but it is not the main unit, then it
8666 -- is the body of a unit in the context, that is being compiled
8667 -- because it is encloses some inlined unit or another generic unit
8668 -- being instantiated. In that case, this body is not part of the
8669 -- current compilation, and is not attached to the tree, but its
8670 -- parent must be set for analysis.
8672 if Nkind (Parent (Inst_Node)) = N_Compilation_Unit then
8674 -- Replace instance node with body of instance, and create new
8675 -- node for corresponding instance declaration.
8677 Build_Instance_Compilation_Unit_Nodes
8678 (Inst_Node, Act_Body, Act_Decl);
8679 Analyze (Inst_Node);
8681 if Parent (Inst_Node) = Cunit (Main_Unit) then
8683 -- If the instance is a child unit itself, then set the scope
8684 -- of the expanded body to be the parent of the instantiation
8685 -- (ensuring that the fully qualified name will be generated
8686 -- for the elaboration subprogram).
8688 if Nkind (Defining_Unit_Name (Act_Spec)) =
8689 N_Defining_Program_Unit_Name
8692 (Defining_Entity (Inst_Node), Scope (Act_Decl_Id));
8696 -- Case where instantiation is not a library unit
8699 -- If this is an early instantiation, i.e. appears textually
8700 -- before the corresponding body and must be elaborated first,
8701 -- indicate that the body instance is to be delayed.
8703 Install_Body (Act_Body, Inst_Node, Gen_Body, Gen_Decl);
8705 -- Now analyze the body. We turn off all checks if this is an
8706 -- internal unit, since there is no reason to have checks on for
8707 -- any predefined run-time library code. All such code is designed
8708 -- to be compiled with checks off.
8710 -- Note that we do NOT apply this criterion to children of GNAT
8711 -- (or on VMS, children of DEC). The latter units must suppress
8712 -- checks explicitly if this is needed.
8714 if Is_Predefined_File_Name
8715 (Unit_File_Name (Get_Source_Unit (Gen_Decl)))
8717 Analyze (Act_Body, Suppress => All_Checks);
8723 Inherit_Context (Gen_Body, Inst_Node);
8725 -- Remove the parent instances if they have been placed on the scope
8726 -- stack to compile the body.
8728 if Parent_Installed then
8729 Remove_Parent (In_Body => True);
8731 -- Restore the previous visibility of the parent
8733 Set_Is_Immediately_Visible (Par_Ent, Par_Vis);
8736 Restore_Private_Views (Act_Decl_Id);
8738 -- Remove the current unit from visibility if this is an instance
8739 -- that is not elaborated on the fly for inlining purposes.
8741 if not Inlined_Body then
8742 Set_Is_Immediately_Visible (Act_Decl_Id, False);
8746 Style_Check := Save_Style_Check;
8748 -- If we have no body, and the unit requires a body, then complain. This
8749 -- complaint is suppressed if we have detected other errors (since a
8750 -- common reason for missing the body is that it had errors).
8752 elsif Unit_Requires_Body (Gen_Unit)
8753 and then not Body_Optional
8755 if Serious_Errors_Detected = 0 then
8757 ("cannot find body of generic package &", Inst_Node, Gen_Unit);
8759 -- Don't attempt to perform any cleanup actions if some other error
8760 -- was already detected, since this can cause blowups.
8766 -- Case of package that does not need a body
8769 -- If the instantiation of the declaration is a library unit, rewrite
8770 -- the original package instantiation as a package declaration in the
8771 -- compilation unit node.
8773 if Nkind (Parent (Inst_Node)) = N_Compilation_Unit then
8774 Set_Parent_Spec (Act_Decl, Parent_Spec (Inst_Node));
8775 Rewrite (Inst_Node, Act_Decl);
8777 -- Generate elaboration entity, in case spec has elaboration code.
8778 -- This cannot be done when the instance is analyzed, because it
8779 -- is not known yet whether the body exists.
8781 Set_Elaboration_Entity_Required (Act_Decl_Id, False);
8782 Build_Elaboration_Entity (Parent (Inst_Node), Act_Decl_Id);
8784 -- If the instantiation is not a library unit, then append the
8785 -- declaration to the list of implicitly generated entities, unless
8786 -- it is already a list member which means that it was already
8789 elsif not Is_List_Member (Act_Decl) then
8790 Mark_Rewrite_Insertion (Act_Decl);
8791 Insert_Before (Inst_Node, Act_Decl);
8795 Expander_Mode_Restore;
8796 end Instantiate_Package_Body;
8798 ---------------------------------
8799 -- Instantiate_Subprogram_Body --
8800 ---------------------------------
8802 procedure Instantiate_Subprogram_Body
8803 (Body_Info : Pending_Body_Info;
8804 Body_Optional : Boolean := False)
8806 Act_Decl : constant Node_Id := Body_Info.Act_Decl;
8807 Inst_Node : constant Node_Id := Body_Info.Inst_Node;
8808 Loc : constant Source_Ptr := Sloc (Inst_Node);
8809 Gen_Id : constant Node_Id := Name (Inst_Node);
8810 Gen_Unit : constant Entity_Id := Get_Generic_Entity (Inst_Node);
8811 Gen_Decl : constant Node_Id := Unit_Declaration_Node (Gen_Unit);
8812 Anon_Id : constant Entity_Id :=
8813 Defining_Unit_Name (Specification (Act_Decl));
8814 Pack_Id : constant Entity_Id :=
8815 Defining_Unit_Name (Parent (Act_Decl));
8818 Gen_Body_Id : Node_Id;
8820 Pack_Body : Node_Id;
8821 Prev_Formal : Entity_Id;
8823 Unit_Renaming : Node_Id;
8825 Parent_Installed : Boolean := False;
8826 Save_Style_Check : constant Boolean := Style_Check;
8828 Par_Ent : Entity_Id := Empty;
8829 Par_Vis : Boolean := False;
8832 Gen_Body_Id := Corresponding_Body (Gen_Decl);
8834 -- Subprogram body may have been created already because of an inline
8835 -- pragma, or because of multiple elaborations of the enclosing package
8836 -- when several instances of the subprogram appear in the main unit.
8838 if Present (Corresponding_Body (Act_Decl)) then
8842 Expander_Mode_Save_And_Set (Body_Info.Expander_Status);
8844 -- Re-establish the state of information on which checks are suppressed.
8845 -- This information was set in Body_Info at the point of instantiation,
8846 -- and now we restore it so that the instance is compiled using the
8847 -- check status at the instantiation (RM 11.5 (7.2/2), AI95-00224-01).
8849 Local_Suppress_Stack_Top := Body_Info.Local_Suppress_Stack_Top;
8850 Scope_Suppress := Body_Info.Scope_Suppress;
8852 if No (Gen_Body_Id) then
8854 -- For imported generic subprogram, no body to compile, complete
8855 -- the spec entity appropriately.
8857 if Is_Imported (Gen_Unit) then
8858 Set_Is_Imported (Anon_Id);
8859 Set_First_Rep_Item (Anon_Id, First_Rep_Item (Gen_Unit));
8860 Set_Interface_Name (Anon_Id, Interface_Name (Gen_Unit));
8861 Set_Convention (Anon_Id, Convention (Gen_Unit));
8862 Set_Has_Completion (Anon_Id);
8865 -- For other cases, compile the body
8868 Load_Parent_Of_Generic
8869 (Inst_Node, Specification (Gen_Decl), Body_Optional);
8870 Gen_Body_Id := Corresponding_Body (Gen_Decl);
8874 Instantiation_Node := Inst_Node;
8876 if Present (Gen_Body_Id) then
8877 Gen_Body := Unit_Declaration_Node (Gen_Body_Id);
8878 Mark_Context (Inst_Node, Gen_Decl);
8880 if Nkind (Gen_Body) = N_Subprogram_Body_Stub then
8882 -- Either body is not present, or context is non-expanding, as
8883 -- when compiling a subunit. Mark the instance as completed, and
8884 -- diagnose a missing body when needed.
8887 and then Operating_Mode = Generate_Code
8890 ("missing proper body for instantiation", Gen_Body);
8893 Set_Has_Completion (Anon_Id);
8897 Save_Env (Gen_Unit, Anon_Id);
8898 Style_Check := False;
8899 Current_Sem_Unit := Body_Info.Current_Sem_Unit;
8900 Create_Instantiation_Source
8908 (Original_Node (Gen_Body), Empty, Instantiating => True);
8910 -- Create proper defining name for the body, to correspond to
8911 -- the one in the spec.
8913 Set_Defining_Unit_Name (Specification (Act_Body),
8914 Make_Defining_Identifier
8915 (Sloc (Defining_Entity (Inst_Node)), Chars (Anon_Id)));
8916 Set_Corresponding_Spec (Act_Body, Anon_Id);
8917 Set_Has_Completion (Anon_Id);
8918 Check_Generic_Actuals (Pack_Id, False);
8920 -- Generate a reference to link the visible subprogram instance to
8921 -- the generic body, which for navigation purposes is the only
8922 -- available source for the instance.
8925 (Related_Instance (Pack_Id),
8926 Gen_Body_Id, 'b', Set_Ref => False, Force => True);
8928 -- If it is a child unit, make the parent instance (which is an
8929 -- instance of the parent of the generic) visible. The parent
8930 -- instance is the prefix of the name of the generic unit.
8932 if Ekind (Scope (Gen_Unit)) = E_Generic_Package
8933 and then Nkind (Gen_Id) = N_Expanded_Name
8935 Par_Ent := Entity (Prefix (Gen_Id));
8936 Par_Vis := Is_Immediately_Visible (Par_Ent);
8937 Install_Parent (Par_Ent, In_Body => True);
8938 Parent_Installed := True;
8940 elsif Is_Child_Unit (Gen_Unit) then
8941 Par_Ent := Scope (Gen_Unit);
8942 Par_Vis := Is_Immediately_Visible (Par_Ent);
8943 Install_Parent (Par_Ent, In_Body => True);
8944 Parent_Installed := True;
8947 -- Inside its body, a reference to the generic unit is a reference
8948 -- to the instance. The corresponding renaming is the first
8949 -- declaration in the body.
8952 Make_Subprogram_Renaming_Declaration (Loc,
8955 Specification (Original_Node (Gen_Body)),
8957 Instantiating => True),
8958 Name => New_Occurrence_Of (Anon_Id, Loc));
8960 -- If there is a formal subprogram with the same name as the unit
8961 -- itself, do not add this renaming declaration. This is a temporary
8962 -- fix for one ACVC test. ???
8964 Prev_Formal := First_Entity (Pack_Id);
8965 while Present (Prev_Formal) loop
8966 if Chars (Prev_Formal) = Chars (Gen_Unit)
8967 and then Is_Overloadable (Prev_Formal)
8972 Next_Entity (Prev_Formal);
8975 if Present (Prev_Formal) then
8976 Decls := New_List (Act_Body);
8978 Decls := New_List (Unit_Renaming, Act_Body);
8981 -- The subprogram body is placed in the body of a dummy package body,
8982 -- whose spec contains the subprogram declaration as well as the
8983 -- renaming declarations for the generic parameters.
8985 Pack_Body := Make_Package_Body (Loc,
8986 Defining_Unit_Name => New_Copy (Pack_Id),
8987 Declarations => Decls);
8989 Set_Corresponding_Spec (Pack_Body, Pack_Id);
8991 -- If the instantiation is a library unit, then build resulting
8992 -- compilation unit nodes for the instance. The declaration of
8993 -- the enclosing package is the grandparent of the subprogram
8994 -- declaration. First replace the instantiation node as the unit
8995 -- of the corresponding compilation.
8997 if Nkind (Parent (Inst_Node)) = N_Compilation_Unit then
8998 if Parent (Inst_Node) = Cunit (Main_Unit) then
8999 Set_Unit (Parent (Inst_Node), Inst_Node);
9000 Build_Instance_Compilation_Unit_Nodes
9001 (Inst_Node, Pack_Body, Parent (Parent (Act_Decl)));
9002 Analyze (Inst_Node);
9004 Set_Parent (Pack_Body, Parent (Inst_Node));
9005 Analyze (Pack_Body);
9009 Insert_Before (Inst_Node, Pack_Body);
9010 Mark_Rewrite_Insertion (Pack_Body);
9011 Analyze (Pack_Body);
9013 if Expander_Active then
9014 Freeze_Subprogram_Body (Inst_Node, Gen_Body, Pack_Id);
9018 Inherit_Context (Gen_Body, Inst_Node);
9020 Restore_Private_Views (Pack_Id, False);
9022 if Parent_Installed then
9023 Remove_Parent (In_Body => True);
9025 -- Restore the previous visibility of the parent
9027 Set_Is_Immediately_Visible (Par_Ent, Par_Vis);
9031 Style_Check := Save_Style_Check;
9033 -- Body not found. Error was emitted already. If there were no previous
9034 -- errors, this may be an instance whose scope is a premature instance.
9035 -- In that case we must insure that the (legal) program does raise
9036 -- program error if executed. We generate a subprogram body for this
9037 -- purpose. See DEC ac30vso.
9039 -- Should not reference proprietary DEC tests in comments ???
9041 elsif Serious_Errors_Detected = 0
9042 and then Nkind (Parent (Inst_Node)) /= N_Compilation_Unit
9044 if Body_Optional then
9047 elsif Ekind (Anon_Id) = E_Procedure then
9049 Make_Subprogram_Body (Loc,
9051 Make_Procedure_Specification (Loc,
9052 Defining_Unit_Name =>
9053 Make_Defining_Identifier (Loc, Chars (Anon_Id)),
9054 Parameter_Specifications =>
9056 (Parameter_Specifications (Parent (Anon_Id)))),
9058 Declarations => Empty_List,
9059 Handled_Statement_Sequence =>
9060 Make_Handled_Sequence_Of_Statements (Loc,
9063 Make_Raise_Program_Error (Loc,
9065 PE_Access_Before_Elaboration))));
9069 Make_Raise_Program_Error (Loc,
9070 Reason => PE_Access_Before_Elaboration);
9072 Set_Etype (Ret_Expr, (Etype (Anon_Id)));
9073 Set_Analyzed (Ret_Expr);
9076 Make_Subprogram_Body (Loc,
9078 Make_Function_Specification (Loc,
9079 Defining_Unit_Name =>
9080 Make_Defining_Identifier (Loc, Chars (Anon_Id)),
9081 Parameter_Specifications =>
9083 (Parameter_Specifications (Parent (Anon_Id))),
9084 Result_Definition =>
9085 New_Occurrence_Of (Etype (Anon_Id), Loc)),
9087 Declarations => Empty_List,
9088 Handled_Statement_Sequence =>
9089 Make_Handled_Sequence_Of_Statements (Loc,
9092 (Make_Simple_Return_Statement (Loc, Ret_Expr))));
9095 Pack_Body := Make_Package_Body (Loc,
9096 Defining_Unit_Name => New_Copy (Pack_Id),
9097 Declarations => New_List (Act_Body));
9099 Insert_After (Inst_Node, Pack_Body);
9100 Set_Corresponding_Spec (Pack_Body, Pack_Id);
9101 Analyze (Pack_Body);
9104 Expander_Mode_Restore;
9105 end Instantiate_Subprogram_Body;
9107 ----------------------
9108 -- Instantiate_Type --
9109 ----------------------
9111 function Instantiate_Type
9114 Analyzed_Formal : Node_Id;
9115 Actual_Decls : List_Id) return List_Id
9117 Gen_T : constant Entity_Id := Defining_Identifier (Formal);
9118 A_Gen_T : constant Entity_Id :=
9119 Defining_Identifier (Analyzed_Formal);
9120 Ancestor : Entity_Id := Empty;
9121 Def : constant Node_Id := Formal_Type_Definition (Formal);
9123 Decl_Node : Node_Id;
9124 Decl_Nodes : List_Id;
9128 procedure Validate_Array_Type_Instance;
9129 procedure Validate_Access_Subprogram_Instance;
9130 procedure Validate_Access_Type_Instance;
9131 procedure Validate_Derived_Type_Instance;
9132 procedure Validate_Derived_Interface_Type_Instance;
9133 procedure Validate_Interface_Type_Instance;
9134 procedure Validate_Private_Type_Instance;
9135 -- These procedures perform validation tests for the named case
9137 function Subtypes_Match (Gen_T, Act_T : Entity_Id) return Boolean;
9138 -- Check that base types are the same and that the subtypes match
9139 -- statically. Used in several of the above.
9141 --------------------
9142 -- Subtypes_Match --
9143 --------------------
9145 function Subtypes_Match (Gen_T, Act_T : Entity_Id) return Boolean is
9146 T : constant Entity_Id := Get_Instance_Of (Gen_T);
9149 return (Base_Type (T) = Base_Type (Act_T)
9150 and then Subtypes_Statically_Match (T, Act_T))
9152 or else (Is_Class_Wide_Type (Gen_T)
9153 and then Is_Class_Wide_Type (Act_T)
9156 (Get_Instance_Of (Root_Type (Gen_T)),
9160 ((Ekind (Gen_T) = E_Anonymous_Access_Subprogram_Type
9161 or else Ekind (Gen_T) = E_Anonymous_Access_Type)
9162 and then Ekind (Act_T) = Ekind (Gen_T)
9164 Subtypes_Statically_Match
9165 (Designated_Type (Gen_T), Designated_Type (Act_T)));
9168 -----------------------------------------
9169 -- Validate_Access_Subprogram_Instance --
9170 -----------------------------------------
9172 procedure Validate_Access_Subprogram_Instance is
9174 if not Is_Access_Type (Act_T)
9175 or else Ekind (Designated_Type (Act_T)) /= E_Subprogram_Type
9178 ("expect access type in instantiation of &", Actual, Gen_T);
9179 Abandon_Instantiation (Actual);
9182 Check_Mode_Conformant
9183 (Designated_Type (Act_T),
9184 Designated_Type (A_Gen_T),
9188 if Ekind (Base_Type (Act_T)) = E_Access_Protected_Subprogram_Type then
9189 if Ekind (A_Gen_T) = E_Access_Subprogram_Type then
9191 ("protected access type not allowed for formal &",
9195 elsif Ekind (A_Gen_T) = E_Access_Protected_Subprogram_Type then
9197 ("expect protected access type for formal &",
9200 end Validate_Access_Subprogram_Instance;
9202 -----------------------------------
9203 -- Validate_Access_Type_Instance --
9204 -----------------------------------
9206 procedure Validate_Access_Type_Instance is
9207 Desig_Type : constant Entity_Id :=
9208 Find_Actual_Type (Designated_Type (A_Gen_T), A_Gen_T);
9209 Desig_Act : Entity_Id;
9212 if not Is_Access_Type (Act_T) then
9214 ("expect access type in instantiation of &", Actual, Gen_T);
9215 Abandon_Instantiation (Actual);
9218 if Is_Access_Constant (A_Gen_T) then
9219 if not Is_Access_Constant (Act_T) then
9221 ("actual type must be access-to-constant type", Actual);
9222 Abandon_Instantiation (Actual);
9225 if Is_Access_Constant (Act_T) then
9227 ("actual type must be access-to-variable type", Actual);
9228 Abandon_Instantiation (Actual);
9230 elsif Ekind (A_Gen_T) = E_General_Access_Type
9231 and then Ekind (Base_Type (Act_T)) /= E_General_Access_Type
9233 Error_Msg_N ("actual must be general access type!", Actual);
9234 Error_Msg_NE ("add ALL to }!", Actual, Act_T);
9235 Abandon_Instantiation (Actual);
9239 -- The designated subtypes, that is to say the subtypes introduced
9240 -- by an access type declaration (and not by a subtype declaration)
9243 Desig_Act := Designated_Type (Base_Type (Act_T));
9245 -- The designated type may have been introduced through a limited_
9246 -- with clause, in which case retrieve the non-limited view. This
9247 -- applies to incomplete types as well as to class-wide types.
9249 if From_With_Type (Desig_Act) then
9250 Desig_Act := Available_View (Desig_Act);
9253 if not Subtypes_Match
9254 (Desig_Type, Desig_Act) then
9256 ("designated type of actual does not match that of formal &",
9258 Abandon_Instantiation (Actual);
9260 elsif Is_Access_Type (Designated_Type (Act_T))
9261 and then Is_Constrained (Designated_Type (Designated_Type (Act_T)))
9263 Is_Constrained (Designated_Type (Desig_Type))
9266 ("designated type of actual does not match that of formal &",
9268 Abandon_Instantiation (Actual);
9271 -- Ada 2005: null-exclusion indicators of the two types must agree
9273 if Can_Never_Be_Null (A_Gen_T) /= Can_Never_Be_Null (Act_T) then
9275 ("non null exclusion of actual and formal & do not match",
9278 end Validate_Access_Type_Instance;
9280 ----------------------------------
9281 -- Validate_Array_Type_Instance --
9282 ----------------------------------
9284 procedure Validate_Array_Type_Instance is
9289 function Formal_Dimensions return Int;
9290 -- Count number of dimensions in array type formal
9292 -----------------------
9293 -- Formal_Dimensions --
9294 -----------------------
9296 function Formal_Dimensions return Int is
9301 if Nkind (Def) = N_Constrained_Array_Definition then
9302 Index := First (Discrete_Subtype_Definitions (Def));
9304 Index := First (Subtype_Marks (Def));
9307 while Present (Index) loop
9313 end Formal_Dimensions;
9315 -- Start of processing for Validate_Array_Type_Instance
9318 if not Is_Array_Type (Act_T) then
9320 ("expect array type in instantiation of &", Actual, Gen_T);
9321 Abandon_Instantiation (Actual);
9323 elsif Nkind (Def) = N_Constrained_Array_Definition then
9324 if not (Is_Constrained (Act_T)) then
9326 ("expect constrained array in instantiation of &",
9328 Abandon_Instantiation (Actual);
9332 if Is_Constrained (Act_T) then
9334 ("expect unconstrained array in instantiation of &",
9336 Abandon_Instantiation (Actual);
9340 if Formal_Dimensions /= Number_Dimensions (Act_T) then
9342 ("dimensions of actual do not match formal &", Actual, Gen_T);
9343 Abandon_Instantiation (Actual);
9346 I1 := First_Index (A_Gen_T);
9347 I2 := First_Index (Act_T);
9348 for J in 1 .. Formal_Dimensions loop
9350 -- If the indices of the actual were given by a subtype_mark,
9351 -- the index was transformed into a range attribute. Retrieve
9352 -- the original type mark for checking.
9354 if Is_Entity_Name (Original_Node (I2)) then
9355 T2 := Entity (Original_Node (I2));
9360 if not Subtypes_Match
9361 (Find_Actual_Type (Etype (I1), A_Gen_T), T2)
9364 ("index types of actual do not match those of formal &",
9366 Abandon_Instantiation (Actual);
9373 -- Check matching subtypes. Note that there are complex visibility
9374 -- issues when the generic is a child unit and some aspect of the
9375 -- generic type is declared in a parent unit of the generic. We do
9376 -- the test to handle this special case only after a direct check
9377 -- for static matching has failed.
9380 (Component_Type (A_Gen_T), Component_Type (Act_T))
9381 or else Subtypes_Match
9382 (Find_Actual_Type (Component_Type (A_Gen_T), A_Gen_T),
9383 Component_Type (Act_T))
9388 ("component subtype of actual does not match that of formal &",
9390 Abandon_Instantiation (Actual);
9393 if Has_Aliased_Components (A_Gen_T)
9394 and then not Has_Aliased_Components (Act_T)
9397 ("actual must have aliased components to match formal type &",
9400 end Validate_Array_Type_Instance;
9402 -----------------------------------------------
9403 -- Validate_Derived_Interface_Type_Instance --
9404 -----------------------------------------------
9406 procedure Validate_Derived_Interface_Type_Instance is
9407 Par : constant Entity_Id := Entity (Subtype_Indication (Def));
9411 -- First apply interface instance checks
9413 Validate_Interface_Type_Instance;
9415 -- Verify that immediate parent interface is an ancestor of
9419 and then not Interface_Present_In_Ancestor (Act_T, Par)
9422 ("interface actual must include progenitor&", Actual, Par);
9425 -- Now verify that the actual includes all other ancestors of
9428 Elmt := First_Elmt (Interfaces (A_Gen_T));
9429 while Present (Elmt) loop
9430 if not Interface_Present_In_Ancestor
9431 (Act_T, Get_Instance_Of (Node (Elmt)))
9434 ("interface actual must include progenitor&",
9435 Actual, Node (Elmt));
9440 end Validate_Derived_Interface_Type_Instance;
9442 ------------------------------------
9443 -- Validate_Derived_Type_Instance --
9444 ------------------------------------
9446 procedure Validate_Derived_Type_Instance is
9447 Actual_Discr : Entity_Id;
9448 Ancestor_Discr : Entity_Id;
9451 -- If the parent type in the generic declaration is itself a previous
9452 -- formal type, then it is local to the generic and absent from the
9453 -- analyzed generic definition. In that case the ancestor is the
9454 -- instance of the formal (which must have been instantiated
9455 -- previously), unless the ancestor is itself a formal derived type.
9456 -- In this latter case (which is the subject of Corrigendum 8652/0038
9457 -- (AI-202) the ancestor of the formals is the ancestor of its
9458 -- parent. Otherwise, the analyzed generic carries the parent type.
9459 -- If the parent type is defined in a previous formal package, then
9460 -- the scope of that formal package is that of the generic type
9461 -- itself, and it has already been mapped into the corresponding type
9462 -- in the actual package.
9464 -- Common case: parent type defined outside of the generic
9466 if Is_Entity_Name (Subtype_Mark (Def))
9467 and then Present (Entity (Subtype_Mark (Def)))
9469 Ancestor := Get_Instance_Of (Entity (Subtype_Mark (Def)));
9471 -- Check whether parent is defined in a previous formal package
9474 Scope (Scope (Base_Type (Etype (A_Gen_T)))) = Scope (A_Gen_T)
9477 Get_Instance_Of (Base_Type (Etype (A_Gen_T)));
9479 -- The type may be a local derivation, or a type extension of a
9480 -- previous formal, or of a formal of a parent package.
9482 elsif Is_Derived_Type (Get_Instance_Of (A_Gen_T))
9484 Ekind (Get_Instance_Of (A_Gen_T)) = E_Record_Type_With_Private
9486 -- Check whether the parent is another derived formal type in the
9487 -- same generic unit.
9489 if Etype (A_Gen_T) /= A_Gen_T
9490 and then Is_Generic_Type (Etype (A_Gen_T))
9491 and then Scope (Etype (A_Gen_T)) = Scope (A_Gen_T)
9492 and then Etype (Etype (A_Gen_T)) /= Etype (A_Gen_T)
9494 -- Locate ancestor of parent from the subtype declaration
9495 -- created for the actual.
9501 Decl := First (Actual_Decls);
9502 while Present (Decl) loop
9503 if Nkind (Decl) = N_Subtype_Declaration
9504 and then Chars (Defining_Identifier (Decl)) =
9505 Chars (Etype (A_Gen_T))
9507 Ancestor := Generic_Parent_Type (Decl);
9515 pragma Assert (Present (Ancestor));
9519 Get_Instance_Of (Base_Type (Get_Instance_Of (A_Gen_T)));
9523 Ancestor := Get_Instance_Of (Etype (Base_Type (A_Gen_T)));
9526 -- If the formal derived type has pragma Preelaborable_Initialization
9527 -- then the actual type must have preelaborable initialization.
9529 if Known_To_Have_Preelab_Init (A_Gen_T)
9530 and then not Has_Preelaborable_Initialization (Act_T)
9533 ("actual for & must have preelaborable initialization",
9537 -- Ada 2005 (AI-251)
9539 if Ada_Version >= Ada_05
9540 and then Is_Interface (Ancestor)
9542 if not Interface_Present_In_Ancestor (Act_T, Ancestor) then
9544 ("(Ada 2005) expected type implementing & in instantiation",
9548 elsif not Is_Ancestor (Base_Type (Ancestor), Act_T) then
9550 ("expect type derived from & in instantiation",
9551 Actual, First_Subtype (Ancestor));
9552 Abandon_Instantiation (Actual);
9555 -- Ada 2005 (AI-443): Synchronized formal derived type checks. Note
9556 -- that the formal type declaration has been rewritten as a private
9559 if Ada_Version >= Ada_05
9560 and then Nkind (Parent (A_Gen_T)) = N_Private_Extension_Declaration
9561 and then Synchronized_Present (Parent (A_Gen_T))
9563 -- The actual must be a synchronized tagged type
9565 if not Is_Tagged_Type (Act_T) then
9567 ("actual of synchronized type must be tagged", Actual);
9568 Abandon_Instantiation (Actual);
9570 elsif Nkind (Parent (Act_T)) = N_Full_Type_Declaration
9571 and then Nkind (Type_Definition (Parent (Act_T))) =
9572 N_Derived_Type_Definition
9573 and then not Synchronized_Present (Type_Definition
9577 ("actual of synchronized type must be synchronized", Actual);
9578 Abandon_Instantiation (Actual);
9582 -- Perform atomic/volatile checks (RM C.6(12))
9584 if Is_Atomic (Act_T) and then not Is_Atomic (Ancestor) then
9586 ("cannot have atomic actual type for non-atomic formal type",
9589 elsif Is_Volatile (Act_T)
9590 and then not Is_Volatile (Ancestor)
9591 and then Is_By_Reference_Type (Ancestor)
9594 ("cannot have volatile actual type for non-volatile formal type",
9598 -- It should not be necessary to check for unknown discriminants on
9599 -- Formal, but for some reason Has_Unknown_Discriminants is false for
9600 -- A_Gen_T, so Is_Indefinite_Subtype incorrectly returns False. This
9601 -- needs fixing. ???
9603 if not Is_Indefinite_Subtype (A_Gen_T)
9604 and then not Unknown_Discriminants_Present (Formal)
9605 and then Is_Indefinite_Subtype (Act_T)
9608 ("actual subtype must be constrained", Actual);
9609 Abandon_Instantiation (Actual);
9612 if not Unknown_Discriminants_Present (Formal) then
9613 if Is_Constrained (Ancestor) then
9614 if not Is_Constrained (Act_T) then
9616 ("actual subtype must be constrained", Actual);
9617 Abandon_Instantiation (Actual);
9620 -- Ancestor is unconstrained, Check if generic formal and actual
9621 -- agree on constrainedness. The check only applies to array types
9622 -- and discriminated types.
9624 elsif Is_Constrained (Act_T) then
9625 if Ekind (Ancestor) = E_Access_Type
9627 (not Is_Constrained (A_Gen_T)
9628 and then Is_Composite_Type (A_Gen_T))
9631 ("actual subtype must be unconstrained", Actual);
9632 Abandon_Instantiation (Actual);
9635 -- A class-wide type is only allowed if the formal has unknown
9638 elsif Is_Class_Wide_Type (Act_T)
9639 and then not Has_Unknown_Discriminants (Ancestor)
9642 ("actual for & cannot be a class-wide type", Actual, Gen_T);
9643 Abandon_Instantiation (Actual);
9645 -- Otherwise, the formal and actual shall have the same number
9646 -- of discriminants and each discriminant of the actual must
9647 -- correspond to a discriminant of the formal.
9649 elsif Has_Discriminants (Act_T)
9650 and then not Has_Unknown_Discriminants (Act_T)
9651 and then Has_Discriminants (Ancestor)
9653 Actual_Discr := First_Discriminant (Act_T);
9654 Ancestor_Discr := First_Discriminant (Ancestor);
9655 while Present (Actual_Discr)
9656 and then Present (Ancestor_Discr)
9658 if Base_Type (Act_T) /= Base_Type (Ancestor) and then
9659 No (Corresponding_Discriminant (Actual_Discr))
9662 ("discriminant & does not correspond " &
9663 "to ancestor discriminant", Actual, Actual_Discr);
9664 Abandon_Instantiation (Actual);
9667 Next_Discriminant (Actual_Discr);
9668 Next_Discriminant (Ancestor_Discr);
9671 if Present (Actual_Discr) or else Present (Ancestor_Discr) then
9673 ("actual for & must have same number of discriminants",
9675 Abandon_Instantiation (Actual);
9678 -- This case should be caught by the earlier check for
9679 -- constrainedness, but the check here is added for completeness.
9681 elsif Has_Discriminants (Act_T)
9682 and then not Has_Unknown_Discriminants (Act_T)
9685 ("actual for & must not have discriminants", Actual, Gen_T);
9686 Abandon_Instantiation (Actual);
9688 elsif Has_Discriminants (Ancestor) then
9690 ("actual for & must have known discriminants", Actual, Gen_T);
9691 Abandon_Instantiation (Actual);
9694 if not Subtypes_Statically_Compatible (Act_T, Ancestor) then
9696 ("constraint on actual is incompatible with formal", Actual);
9697 Abandon_Instantiation (Actual);
9701 -- If the formal and actual types are abstract, check that there
9702 -- are no abstract primitives of the actual type that correspond to
9703 -- nonabstract primitives of the formal type (second sentence of
9706 if Is_Abstract_Type (A_Gen_T) and then Is_Abstract_Type (Act_T) then
9707 Check_Abstract_Primitives : declare
9708 Gen_Prims : constant Elist_Id :=
9709 Primitive_Operations (A_Gen_T);
9711 Gen_Subp : Entity_Id;
9712 Anc_Subp : Entity_Id;
9713 Anc_Formal : Entity_Id;
9714 Anc_F_Type : Entity_Id;
9716 Act_Prims : constant Elist_Id := Primitive_Operations (Act_T);
9718 Act_Subp : Entity_Id;
9719 Act_Formal : Entity_Id;
9720 Act_F_Type : Entity_Id;
9722 Subprograms_Correspond : Boolean;
9724 function Is_Tagged_Ancestor (T1, T2 : Entity_Id) return Boolean;
9725 -- Returns true if T2 is derived directly or indirectly from
9726 -- T1, including derivations from interfaces. T1 and T2 are
9727 -- required to be specific tagged base types.
9729 ------------------------
9730 -- Is_Tagged_Ancestor --
9731 ------------------------
9733 function Is_Tagged_Ancestor (T1, T2 : Entity_Id) return Boolean
9735 Intfc_Elmt : Elmt_Id;
9738 -- The predicate is satisfied if the types are the same
9743 -- If we've reached the top of the derivation chain then
9744 -- we know that T1 is not an ancestor of T2.
9746 elsif Etype (T2) = T2 then
9749 -- Proceed to check T2's immediate parent
9751 elsif Is_Ancestor (T1, Base_Type (Etype (T2))) then
9754 -- Finally, check to see if T1 is an ancestor of any of T2's
9758 Intfc_Elmt := First_Elmt (Interfaces (T2));
9759 while Present (Intfc_Elmt) loop
9760 if Is_Ancestor (T1, Node (Intfc_Elmt)) then
9764 Next_Elmt (Intfc_Elmt);
9769 end Is_Tagged_Ancestor;
9771 -- Start of processing for Check_Abstract_Primitives
9774 -- Loop over all of the formal derived type's primitives
9776 Gen_Elmt := First_Elmt (Gen_Prims);
9777 while Present (Gen_Elmt) loop
9778 Gen_Subp := Node (Gen_Elmt);
9780 -- If the primitive of the formal is not abstract, then
9781 -- determine whether there is a corresponding primitive of
9782 -- the actual type that's abstract.
9784 if not Is_Abstract_Subprogram (Gen_Subp) then
9785 Act_Elmt := First_Elmt (Act_Prims);
9786 while Present (Act_Elmt) loop
9787 Act_Subp := Node (Act_Elmt);
9789 -- If we find an abstract primitive of the actual,
9790 -- then we need to test whether it corresponds to the
9791 -- subprogram from which the generic formal primitive
9794 if Is_Abstract_Subprogram (Act_Subp) then
9795 Anc_Subp := Alias (Gen_Subp);
9797 -- Test whether we have a corresponding primitive
9798 -- by comparing names, kinds, formal types, and
9801 if Chars (Anc_Subp) = Chars (Act_Subp)
9802 and then Ekind (Anc_Subp) = Ekind (Act_Subp)
9804 Anc_Formal := First_Formal (Anc_Subp);
9805 Act_Formal := First_Formal (Act_Subp);
9806 while Present (Anc_Formal)
9807 and then Present (Act_Formal)
9809 Anc_F_Type := Etype (Anc_Formal);
9810 Act_F_Type := Etype (Act_Formal);
9812 if Ekind (Anc_F_Type)
9813 = E_Anonymous_Access_Type
9815 Anc_F_Type := Designated_Type (Anc_F_Type);
9817 if Ekind (Act_F_Type)
9818 = E_Anonymous_Access_Type
9821 Designated_Type (Act_F_Type);
9827 Ekind (Act_F_Type) = E_Anonymous_Access_Type
9832 Anc_F_Type := Base_Type (Anc_F_Type);
9833 Act_F_Type := Base_Type (Act_F_Type);
9835 -- If the formal is controlling, then the
9836 -- the type of the actual primitive's formal
9837 -- must be derived directly or indirectly
9838 -- from the type of the ancestor primitive's
9841 if Is_Controlling_Formal (Anc_Formal) then
9842 if not Is_Tagged_Ancestor
9843 (Anc_F_Type, Act_F_Type)
9848 -- Otherwise the types of the formals must
9851 elsif Anc_F_Type /= Act_F_Type then
9855 Next_Entity (Anc_Formal);
9856 Next_Entity (Act_Formal);
9859 -- If we traversed through all of the formals
9860 -- then so far the subprograms correspond, so
9861 -- now check that any result types correspond.
9864 and then No (Act_Formal)
9866 Subprograms_Correspond := True;
9868 if Ekind (Act_Subp) = E_Function then
9869 Anc_F_Type := Etype (Anc_Subp);
9870 Act_F_Type := Etype (Act_Subp);
9872 if Ekind (Anc_F_Type)
9873 = E_Anonymous_Access_Type
9876 Designated_Type (Anc_F_Type);
9878 if Ekind (Act_F_Type)
9879 = E_Anonymous_Access_Type
9882 Designated_Type (Act_F_Type);
9884 Subprograms_Correspond := False;
9889 = E_Anonymous_Access_Type
9891 Subprograms_Correspond := False;
9894 Anc_F_Type := Base_Type (Anc_F_Type);
9895 Act_F_Type := Base_Type (Act_F_Type);
9897 -- Now either the result types must be
9898 -- the same or, if the result type is
9899 -- controlling, the result type of the
9900 -- actual primitive must descend from the
9901 -- result type of the ancestor primitive.
9903 if Subprograms_Correspond
9904 and then Anc_F_Type /= Act_F_Type
9906 Has_Controlling_Result (Anc_Subp)
9908 not Is_Tagged_Ancestor
9909 (Anc_F_Type, Act_F_Type)
9911 Subprograms_Correspond := False;
9915 -- Found a matching subprogram belonging to
9916 -- formal ancestor type, so actual subprogram
9917 -- corresponds and this violates 3.9.3(9).
9919 if Subprograms_Correspond then
9921 ("abstract subprogram & overrides " &
9922 "nonabstract subprogram of ancestor",
9930 Next_Elmt (Act_Elmt);
9934 Next_Elmt (Gen_Elmt);
9936 end Check_Abstract_Primitives;
9939 -- Verify that limitedness matches. If parent is a limited
9940 -- interface then the generic formal is not unless declared
9941 -- explicitly so. If not declared limited, the actual cannot be
9942 -- limited (see AI05-0087).
9943 -- Disable check for now, limited interfaces implemented by
9944 -- protected types are common, Need to update tests ???
9946 if Is_Limited_Type (Act_T)
9947 and then not Is_Limited_Type (A_Gen_T)
9951 ("actual for non-limited & cannot be a limited type", Actual,
9953 Explain_Limited_Type (Act_T, Actual);
9954 Abandon_Instantiation (Actual);
9956 end Validate_Derived_Type_Instance;
9958 --------------------------------------
9959 -- Validate_Interface_Type_Instance --
9960 --------------------------------------
9962 procedure Validate_Interface_Type_Instance is
9964 if not Is_Interface (Act_T) then
9966 ("actual for formal interface type must be an interface",
9969 elsif Is_Limited_Type (Act_T) /= Is_Limited_Type (A_Gen_T)
9971 Is_Task_Interface (A_Gen_T) /= Is_Task_Interface (Act_T)
9973 Is_Protected_Interface (A_Gen_T) /=
9974 Is_Protected_Interface (Act_T)
9976 Is_Synchronized_Interface (A_Gen_T) /=
9977 Is_Synchronized_Interface (Act_T)
9980 ("actual for interface& does not match (RM 12.5.5(4))",
9983 end Validate_Interface_Type_Instance;
9985 ------------------------------------
9986 -- Validate_Private_Type_Instance --
9987 ------------------------------------
9989 procedure Validate_Private_Type_Instance is
9990 Formal_Discr : Entity_Id;
9991 Actual_Discr : Entity_Id;
9992 Formal_Subt : Entity_Id;
9995 if Is_Limited_Type (Act_T)
9996 and then not Is_Limited_Type (A_Gen_T)
9999 ("actual for non-limited & cannot be a limited type", Actual,
10001 Explain_Limited_Type (Act_T, Actual);
10002 Abandon_Instantiation (Actual);
10004 elsif Known_To_Have_Preelab_Init (A_Gen_T)
10005 and then not Has_Preelaborable_Initialization (Act_T)
10008 ("actual for & must have preelaborable initialization", Actual,
10011 elsif Is_Indefinite_Subtype (Act_T)
10012 and then not Is_Indefinite_Subtype (A_Gen_T)
10013 and then Ada_Version >= Ada_95
10016 ("actual for & must be a definite subtype", Actual, Gen_T);
10018 elsif not Is_Tagged_Type (Act_T)
10019 and then Is_Tagged_Type (A_Gen_T)
10022 ("actual for & must be a tagged type", Actual, Gen_T);
10024 elsif Has_Discriminants (A_Gen_T) then
10025 if not Has_Discriminants (Act_T) then
10027 ("actual for & must have discriminants", Actual, Gen_T);
10028 Abandon_Instantiation (Actual);
10030 elsif Is_Constrained (Act_T) then
10032 ("actual for & must be unconstrained", Actual, Gen_T);
10033 Abandon_Instantiation (Actual);
10036 Formal_Discr := First_Discriminant (A_Gen_T);
10037 Actual_Discr := First_Discriminant (Act_T);
10038 while Formal_Discr /= Empty loop
10039 if Actual_Discr = Empty then
10041 ("discriminants on actual do not match formal",
10043 Abandon_Instantiation (Actual);
10046 Formal_Subt := Get_Instance_Of (Etype (Formal_Discr));
10048 -- Access discriminants match if designated types do
10050 if Ekind (Base_Type (Formal_Subt)) = E_Anonymous_Access_Type
10051 and then (Ekind (Base_Type (Etype (Actual_Discr)))) =
10052 E_Anonymous_Access_Type
10055 (Designated_Type (Base_Type (Formal_Subt))) =
10056 Designated_Type (Base_Type (Etype (Actual_Discr)))
10060 elsif Base_Type (Formal_Subt) /=
10061 Base_Type (Etype (Actual_Discr))
10064 ("types of actual discriminants must match formal",
10066 Abandon_Instantiation (Actual);
10068 elsif not Subtypes_Statically_Match
10069 (Formal_Subt, Etype (Actual_Discr))
10070 and then Ada_Version >= Ada_95
10073 ("subtypes of actual discriminants must match formal",
10075 Abandon_Instantiation (Actual);
10078 Next_Discriminant (Formal_Discr);
10079 Next_Discriminant (Actual_Discr);
10082 if Actual_Discr /= Empty then
10084 ("discriminants on actual do not match formal",
10086 Abandon_Instantiation (Actual);
10093 end Validate_Private_Type_Instance;
10095 -- Start of processing for Instantiate_Type
10098 if Get_Instance_Of (A_Gen_T) /= A_Gen_T then
10099 Error_Msg_N ("duplicate instantiation of generic type", Actual);
10100 return New_List (Error);
10102 elsif not Is_Entity_Name (Actual)
10103 or else not Is_Type (Entity (Actual))
10106 ("expect valid subtype mark to instantiate &", Actual, Gen_T);
10107 Abandon_Instantiation (Actual);
10110 Act_T := Entity (Actual);
10112 -- Ada 2005 (AI-216): An Unchecked_Union subtype shall only be passed
10113 -- as a generic actual parameter if the corresponding formal type
10114 -- does not have a known_discriminant_part, or is a formal derived
10115 -- type that is an Unchecked_Union type.
10117 if Is_Unchecked_Union (Base_Type (Act_T)) then
10118 if not Has_Discriminants (A_Gen_T)
10120 (Is_Derived_Type (A_Gen_T)
10122 Is_Unchecked_Union (A_Gen_T))
10126 Error_Msg_N ("Unchecked_Union cannot be the actual for a" &
10127 " discriminated formal type", Act_T);
10132 -- Deal with fixed/floating restrictions
10134 if Is_Floating_Point_Type (Act_T) then
10135 Check_Restriction (No_Floating_Point, Actual);
10136 elsif Is_Fixed_Point_Type (Act_T) then
10137 Check_Restriction (No_Fixed_Point, Actual);
10140 -- Deal with error of using incomplete type as generic actual.
10141 -- This includes limited views of a type, even if the non-limited
10142 -- view may be available.
10144 if Ekind (Act_T) = E_Incomplete_Type
10145 or else (Is_Class_Wide_Type (Act_T)
10147 Ekind (Root_Type (Act_T)) = E_Incomplete_Type)
10149 if Is_Class_Wide_Type (Act_T)
10150 or else No (Full_View (Act_T))
10152 Error_Msg_N ("premature use of incomplete type", Actual);
10153 Abandon_Instantiation (Actual);
10155 Act_T := Full_View (Act_T);
10156 Set_Entity (Actual, Act_T);
10158 if Has_Private_Component (Act_T) then
10160 ("premature use of type with private component", Actual);
10164 -- Deal with error of premature use of private type as generic actual
10166 elsif Is_Private_Type (Act_T)
10167 and then Is_Private_Type (Base_Type (Act_T))
10168 and then not Is_Generic_Type (Act_T)
10169 and then not Is_Derived_Type (Act_T)
10170 and then No (Full_View (Root_Type (Act_T)))
10172 Error_Msg_N ("premature use of private type", Actual);
10174 elsif Has_Private_Component (Act_T) then
10176 ("premature use of type with private component", Actual);
10179 Set_Instance_Of (A_Gen_T, Act_T);
10181 -- If the type is generic, the class-wide type may also be used
10183 if Is_Tagged_Type (A_Gen_T)
10184 and then Is_Tagged_Type (Act_T)
10185 and then not Is_Class_Wide_Type (A_Gen_T)
10187 Set_Instance_Of (Class_Wide_Type (A_Gen_T),
10188 Class_Wide_Type (Act_T));
10191 if not Is_Abstract_Type (A_Gen_T)
10192 and then Is_Abstract_Type (Act_T)
10195 ("actual of non-abstract formal cannot be abstract", Actual);
10198 -- A generic scalar type is a first subtype for which we generate
10199 -- an anonymous base type. Indicate that the instance of this base
10200 -- is the base type of the actual.
10202 if Is_Scalar_Type (A_Gen_T) then
10203 Set_Instance_Of (Etype (A_Gen_T), Etype (Act_T));
10207 if Error_Posted (Act_T) then
10210 case Nkind (Def) is
10211 when N_Formal_Private_Type_Definition =>
10212 Validate_Private_Type_Instance;
10214 when N_Formal_Derived_Type_Definition =>
10215 Validate_Derived_Type_Instance;
10217 when N_Formal_Discrete_Type_Definition =>
10218 if not Is_Discrete_Type (Act_T) then
10220 ("expect discrete type in instantiation of&",
10222 Abandon_Instantiation (Actual);
10225 when N_Formal_Signed_Integer_Type_Definition =>
10226 if not Is_Signed_Integer_Type (Act_T) then
10228 ("expect signed integer type in instantiation of&",
10230 Abandon_Instantiation (Actual);
10233 when N_Formal_Modular_Type_Definition =>
10234 if not Is_Modular_Integer_Type (Act_T) then
10236 ("expect modular type in instantiation of &",
10238 Abandon_Instantiation (Actual);
10241 when N_Formal_Floating_Point_Definition =>
10242 if not Is_Floating_Point_Type (Act_T) then
10244 ("expect float type in instantiation of &", Actual, Gen_T);
10245 Abandon_Instantiation (Actual);
10248 when N_Formal_Ordinary_Fixed_Point_Definition =>
10249 if not Is_Ordinary_Fixed_Point_Type (Act_T) then
10251 ("expect ordinary fixed point type in instantiation of &",
10253 Abandon_Instantiation (Actual);
10256 when N_Formal_Decimal_Fixed_Point_Definition =>
10257 if not Is_Decimal_Fixed_Point_Type (Act_T) then
10259 ("expect decimal type in instantiation of &",
10261 Abandon_Instantiation (Actual);
10264 when N_Array_Type_Definition =>
10265 Validate_Array_Type_Instance;
10267 when N_Access_To_Object_Definition =>
10268 Validate_Access_Type_Instance;
10270 when N_Access_Function_Definition |
10271 N_Access_Procedure_Definition =>
10272 Validate_Access_Subprogram_Instance;
10274 when N_Record_Definition =>
10275 Validate_Interface_Type_Instance;
10277 when N_Derived_Type_Definition =>
10278 Validate_Derived_Interface_Type_Instance;
10281 raise Program_Error;
10286 Subt := New_Copy (Gen_T);
10288 -- Use adjusted sloc of subtype name as the location for other nodes in
10289 -- the subtype declaration.
10291 Loc := Sloc (Subt);
10294 Make_Subtype_Declaration (Loc,
10295 Defining_Identifier => Subt,
10296 Subtype_Indication => New_Reference_To (Act_T, Loc));
10298 if Is_Private_Type (Act_T) then
10299 Set_Has_Private_View (Subtype_Indication (Decl_Node));
10301 elsif Is_Access_Type (Act_T)
10302 and then Is_Private_Type (Designated_Type (Act_T))
10304 Set_Has_Private_View (Subtype_Indication (Decl_Node));
10307 Decl_Nodes := New_List (Decl_Node);
10309 -- Flag actual derived types so their elaboration produces the
10310 -- appropriate renamings for the primitive operations of the ancestor.
10311 -- Flag actual for formal private types as well, to determine whether
10312 -- operations in the private part may override inherited operations.
10313 -- If the formal has an interface list, the ancestor is not the
10314 -- parent, but the analyzed formal that includes the interface
10315 -- operations of all its progenitors.
10317 if Nkind (Def) = N_Formal_Derived_Type_Definition then
10318 if Present (Interface_List (Def)) then
10319 Set_Generic_Parent_Type (Decl_Node, A_Gen_T);
10321 Set_Generic_Parent_Type (Decl_Node, Ancestor);
10324 elsif Nkind (Def) = N_Formal_Private_Type_Definition then
10325 Set_Generic_Parent_Type (Decl_Node, Ancestor);
10328 -- If the actual is a synchronized type that implements an interface,
10329 -- the primitive operations are attached to the corresponding record,
10330 -- and we have to treat it as an additional generic actual, so that its
10331 -- primitive operations become visible in the instance. The task or
10332 -- protected type itself does not carry primitive operations.
10334 if Is_Concurrent_Type (Act_T)
10335 and then Is_Tagged_Type (Act_T)
10336 and then Present (Corresponding_Record_Type (Act_T))
10337 and then Present (Ancestor)
10338 and then Is_Interface (Ancestor)
10341 Corr_Rec : constant Entity_Id :=
10342 Corresponding_Record_Type (Act_T);
10343 New_Corr : Entity_Id;
10344 Corr_Decl : Node_Id;
10347 New_Corr := Make_Defining_Identifier (Loc,
10348 Chars => New_Internal_Name ('S'));
10350 Make_Subtype_Declaration (Loc,
10351 Defining_Identifier => New_Corr,
10352 Subtype_Indication =>
10353 New_Reference_To (Corr_Rec, Loc));
10354 Append_To (Decl_Nodes, Corr_Decl);
10356 if Ekind (Act_T) = E_Task_Type then
10357 Set_Ekind (Subt, E_Task_Subtype);
10359 Set_Ekind (Subt, E_Protected_Subtype);
10362 Set_Corresponding_Record_Type (Subt, Corr_Rec);
10363 Set_Generic_Parent_Type (Corr_Decl, Ancestor);
10364 Set_Generic_Parent_Type (Decl_Node, Empty);
10369 end Instantiate_Type;
10371 -----------------------
10372 -- Is_Generic_Formal --
10373 -----------------------
10375 function Is_Generic_Formal (E : Entity_Id) return Boolean is
10381 Kind := Nkind (Parent (E));
10383 Nkind_In (Kind, N_Formal_Object_Declaration,
10384 N_Formal_Package_Declaration,
10385 N_Formal_Type_Declaration)
10387 (Is_Formal_Subprogram (E)
10389 Nkind (Parent (Parent (E))) in
10390 N_Formal_Subprogram_Declaration);
10392 end Is_Generic_Formal;
10398 procedure Mark_Context (Inst_Decl : Node_Id; Gen_Decl : Node_Id) is
10399 Inst_CU : constant Unit_Number_Type := Get_Source_Unit (Inst_Decl);
10400 Gen_CU : constant Unit_Number_Type := Get_Source_Unit (Gen_Decl);
10404 Clause := First (Context_Items (Cunit (Inst_CU)));
10405 while Present (Clause) loop
10406 if Nkind (Clause) = N_With_Clause
10407 and then Library_Unit (Clause) = Cunit (Gen_CU)
10409 Set_Withed_Body (Clause, Cunit (Gen_CU));
10415 -- If the instance appears within another instantiated unit, check
10416 -- whether it appears in the main unit, and indicate the need for
10417 -- the body of the enclosing instance as well.
10419 if In_Extended_Main_Code_Unit (Inst_Decl)
10420 and then Instantiation_Location (Sloc (Inst_Decl)) /= No_Location
10421 and then Present (Library_Unit (Cunit (Main_Unit)))
10422 and then Cunit (Inst_CU) /= Library_Unit (Cunit (Main_Unit))
10424 Clause := First (Context_Items (Library_Unit (Cunit (Main_Unit))));
10425 while Present (Clause) loop
10426 if Nkind (Clause) = N_With_Clause
10427 and then Library_Unit (Clause) = Cunit (Gen_CU)
10429 Set_Withed_Body (Clause, Cunit (Gen_CU));
10437 ---------------------
10438 -- Is_In_Main_Unit --
10439 ---------------------
10441 function Is_In_Main_Unit (N : Node_Id) return Boolean is
10442 Unum : constant Unit_Number_Type := Get_Source_Unit (N);
10443 Current_Unit : Node_Id;
10446 if Unum = Main_Unit then
10449 -- If the current unit is a subunit then it is either the main unit or
10450 -- is being compiled as part of the main unit.
10452 elsif Nkind (N) = N_Compilation_Unit then
10453 return Nkind (Unit (N)) = N_Subunit;
10456 Current_Unit := Parent (N);
10457 while Present (Current_Unit)
10458 and then Nkind (Current_Unit) /= N_Compilation_Unit
10460 Current_Unit := Parent (Current_Unit);
10463 -- The instantiation node is in the main unit, or else the current node
10464 -- (perhaps as the result of nested instantiations) is in the main unit,
10465 -- or in the declaration of the main unit, which in this last case must
10468 return Unum = Main_Unit
10469 or else Current_Unit = Cunit (Main_Unit)
10470 or else Current_Unit = Library_Unit (Cunit (Main_Unit))
10471 or else (Present (Library_Unit (Current_Unit))
10472 and then Is_In_Main_Unit (Library_Unit (Current_Unit)));
10473 end Is_In_Main_Unit;
10475 ----------------------------
10476 -- Load_Parent_Of_Generic --
10477 ----------------------------
10479 procedure Load_Parent_Of_Generic
10482 Body_Optional : Boolean := False)
10484 Comp_Unit : constant Node_Id := Cunit (Get_Source_Unit (Spec));
10485 Save_Style_Check : constant Boolean := Style_Check;
10486 True_Parent : Node_Id;
10487 Inst_Node : Node_Id;
10489 Previous_Instances : constant Elist_Id := New_Elmt_List;
10491 procedure Collect_Previous_Instances (Decls : List_Id);
10492 -- Collect all instantiations in the given list of declarations, that
10493 -- precede the generic that we need to load. If the bodies of these
10494 -- instantiations are available, we must analyze them, to ensure that
10495 -- the public symbols generated are the same when the unit is compiled
10496 -- to generate code, and when it is compiled in the context of a unit
10497 -- that needs a particular nested instance. This process is applied
10498 -- to both package and subprogram instances.
10500 --------------------------------
10501 -- Collect_Previous_Instances --
10502 --------------------------------
10504 procedure Collect_Previous_Instances (Decls : List_Id) is
10508 Decl := First (Decls);
10509 while Present (Decl) loop
10510 if Sloc (Decl) >= Sloc (Inst_Node) then
10513 -- If Decl is an instantiation, then record it as requiring
10514 -- instantiation of the corresponding body, except if it is an
10515 -- abbreviated instantiation generated internally for conformance
10516 -- checking purposes only for the case of a formal package
10517 -- declared without a box (see Instantiate_Formal_Package). Such
10518 -- an instantiation does not generate any code (the actual code
10519 -- comes from actual) and thus does not need to be analyzed here.
10520 -- If the instantiation appears with a generic package body it is
10521 -- not analyzed here either.
10523 elsif Nkind (Decl) = N_Package_Instantiation
10524 and then not Is_Internal (Defining_Entity (Decl))
10526 Append_Elmt (Decl, Previous_Instances);
10528 -- For a subprogram instantiation, omit instantiations intrinsic
10529 -- operations (Unchecked_Conversions, etc.) that have no bodies.
10531 elsif Nkind_In (Decl, N_Function_Instantiation,
10532 N_Procedure_Instantiation)
10533 and then not Is_Intrinsic_Subprogram (Entity (Name (Decl)))
10535 Append_Elmt (Decl, Previous_Instances);
10537 elsif Nkind (Decl) = N_Package_Declaration then
10538 Collect_Previous_Instances
10539 (Visible_Declarations (Specification (Decl)));
10540 Collect_Previous_Instances
10541 (Private_Declarations (Specification (Decl)));
10543 -- Previous non-generic bodies may contain instances as well
10545 elsif Nkind (Decl) = N_Package_Body
10546 and then Ekind (Corresponding_Spec (Decl)) /= E_Generic_Package
10548 Collect_Previous_Instances (Declarations (Decl));
10550 elsif Nkind (Decl) = N_Subprogram_Body
10551 and then not Acts_As_Spec (Decl)
10552 and then not Is_Generic_Subprogram (Corresponding_Spec (Decl))
10554 Collect_Previous_Instances (Declarations (Decl));
10559 end Collect_Previous_Instances;
10561 -- Start of processing for Load_Parent_Of_Generic
10564 if not In_Same_Source_Unit (N, Spec)
10565 or else Nkind (Unit (Comp_Unit)) = N_Package_Declaration
10566 or else (Nkind (Unit (Comp_Unit)) = N_Package_Body
10567 and then not Is_In_Main_Unit (Spec))
10569 -- Find body of parent of spec, and analyze it. A special case arises
10570 -- when the parent is an instantiation, that is to say when we are
10571 -- currently instantiating a nested generic. In that case, there is
10572 -- no separate file for the body of the enclosing instance. Instead,
10573 -- the enclosing body must be instantiated as if it were a pending
10574 -- instantiation, in order to produce the body for the nested generic
10575 -- we require now. Note that in that case the generic may be defined
10576 -- in a package body, the instance defined in the same package body,
10577 -- and the original enclosing body may not be in the main unit.
10579 Inst_Node := Empty;
10581 True_Parent := Parent (Spec);
10582 while Present (True_Parent)
10583 and then Nkind (True_Parent) /= N_Compilation_Unit
10585 if Nkind (True_Parent) = N_Package_Declaration
10587 Nkind (Original_Node (True_Parent)) = N_Package_Instantiation
10589 -- Parent is a compilation unit that is an instantiation.
10590 -- Instantiation node has been replaced with package decl.
10592 Inst_Node := Original_Node (True_Parent);
10595 elsif Nkind (True_Parent) = N_Package_Declaration
10596 and then Present (Generic_Parent (Specification (True_Parent)))
10597 and then Nkind (Parent (True_Parent)) /= N_Compilation_Unit
10599 -- Parent is an instantiation within another specification.
10600 -- Declaration for instance has been inserted before original
10601 -- instantiation node. A direct link would be preferable?
10603 Inst_Node := Next (True_Parent);
10604 while Present (Inst_Node)
10605 and then Nkind (Inst_Node) /= N_Package_Instantiation
10610 -- If the instance appears within a generic, and the generic
10611 -- unit is defined within a formal package of the enclosing
10612 -- generic, there is no generic body available, and none
10613 -- needed. A more precise test should be used ???
10615 if No (Inst_Node) then
10622 True_Parent := Parent (True_Parent);
10626 -- Case where we are currently instantiating a nested generic
10628 if Present (Inst_Node) then
10629 if Nkind (Parent (True_Parent)) = N_Compilation_Unit then
10631 -- Instantiation node and declaration of instantiated package
10632 -- were exchanged when only the declaration was needed.
10633 -- Restore instantiation node before proceeding with body.
10635 Set_Unit (Parent (True_Parent), Inst_Node);
10638 -- Now complete instantiation of enclosing body, if it appears in
10639 -- some other unit. If it appears in the current unit, the body
10640 -- will have been instantiated already.
10642 if No (Corresponding_Body (Instance_Spec (Inst_Node))) then
10644 -- We need to determine the expander mode to instantiate the
10645 -- enclosing body. Because the generic body we need may use
10646 -- global entities declared in the enclosing package (including
10647 -- aggregates) it is in general necessary to compile this body
10648 -- with expansion enabled. The exception is if we are within a
10649 -- generic package, in which case the usual generic rule
10653 Exp_Status : Boolean := True;
10657 -- Loop through scopes looking for generic package
10659 Scop := Scope (Defining_Entity (Instance_Spec (Inst_Node)));
10660 while Present (Scop)
10661 and then Scop /= Standard_Standard
10663 if Ekind (Scop) = E_Generic_Package then
10664 Exp_Status := False;
10668 Scop := Scope (Scop);
10671 -- Collect previous instantiations in the unit that contains
10672 -- the desired generic.
10674 if Nkind (Parent (True_Parent)) /= N_Compilation_Unit
10675 and then not Body_Optional
10679 Info : Pending_Body_Info;
10683 Par := Parent (Inst_Node);
10684 while Present (Par) loop
10685 exit when Nkind (Parent (Par)) = N_Compilation_Unit;
10686 Par := Parent (Par);
10689 pragma Assert (Present (Par));
10691 if Nkind (Par) = N_Package_Body then
10692 Collect_Previous_Instances (Declarations (Par));
10694 elsif Nkind (Par) = N_Package_Declaration then
10695 Collect_Previous_Instances
10696 (Visible_Declarations (Specification (Par)));
10697 Collect_Previous_Instances
10698 (Private_Declarations (Specification (Par)));
10701 -- Enclosing unit is a subprogram body. In this
10702 -- case all instance bodies are processed in order
10703 -- and there is no need to collect them separately.
10708 Decl := First_Elmt (Previous_Instances);
10709 while Present (Decl) loop
10711 (Inst_Node => Node (Decl),
10713 Instance_Spec (Node (Decl)),
10714 Expander_Status => Exp_Status,
10715 Current_Sem_Unit =>
10716 Get_Code_Unit (Sloc (Node (Decl))),
10717 Scope_Suppress => Scope_Suppress,
10718 Local_Suppress_Stack_Top =>
10719 Local_Suppress_Stack_Top);
10721 -- Package instance
10724 Nkind (Node (Decl)) = N_Package_Instantiation
10726 Instantiate_Package_Body
10727 (Info, Body_Optional => True);
10729 -- Subprogram instance
10732 -- The instance_spec is the wrapper package,
10733 -- and the subprogram declaration is the last
10734 -- declaration in the wrapper.
10738 (Visible_Declarations
10739 (Specification (Info.Act_Decl)));
10741 Instantiate_Subprogram_Body
10742 (Info, Body_Optional => True);
10750 Instantiate_Package_Body
10752 ((Inst_Node => Inst_Node,
10753 Act_Decl => True_Parent,
10754 Expander_Status => Exp_Status,
10755 Current_Sem_Unit =>
10756 Get_Code_Unit (Sloc (Inst_Node)),
10757 Scope_Suppress => Scope_Suppress,
10758 Local_Suppress_Stack_Top =>
10759 Local_Suppress_Stack_Top)),
10760 Body_Optional => Body_Optional);
10764 -- Case where we are not instantiating a nested generic
10767 Opt.Style_Check := False;
10768 Expander_Mode_Save_And_Set (True);
10769 Load_Needed_Body (Comp_Unit, OK);
10770 Opt.Style_Check := Save_Style_Check;
10771 Expander_Mode_Restore;
10774 and then Unit_Requires_Body (Defining_Entity (Spec))
10775 and then not Body_Optional
10778 Bname : constant Unit_Name_Type :=
10779 Get_Body_Name (Get_Unit_Name (Unit (Comp_Unit)));
10782 Error_Msg_Unit_1 := Bname;
10783 Error_Msg_N ("this instantiation requires$!", N);
10784 Error_Msg_File_1 := Get_File_Name (Bname, Subunit => False);
10785 Error_Msg_N ("\but file{ was not found!", N);
10786 raise Unrecoverable_Error;
10792 -- If loading parent of the generic caused an instantiation circularity,
10793 -- we abandon compilation at this point, because otherwise in some cases
10794 -- we get into trouble with infinite recursions after this point.
10796 if Circularity_Detected then
10797 raise Unrecoverable_Error;
10799 end Load_Parent_Of_Generic;
10801 ---------------------------------
10802 -- Map_Formal_Package_Entities --
10803 ---------------------------------
10805 procedure Map_Formal_Package_Entities (Form : Entity_Id; Act : Entity_Id) is
10810 Set_Instance_Of (Form, Act);
10812 -- Traverse formal and actual package to map the corresponding entities.
10813 -- We skip over internal entities that may be generated during semantic
10814 -- analysis, and find the matching entities by name, given that they
10815 -- must appear in the same order.
10817 E1 := First_Entity (Form);
10818 E2 := First_Entity (Act);
10819 while Present (E1) and then E1 /= First_Private_Entity (Form) loop
10820 -- Could this test be a single condition???
10821 -- Seems like it could, and isn't FPE (Form) a constant anyway???
10823 if not Is_Internal (E1)
10824 and then Present (Parent (E1))
10825 and then not Is_Class_Wide_Type (E1)
10826 and then not Is_Internal_Name (Chars (E1))
10828 while Present (E2) and then Chars (E2) /= Chars (E1) loop
10835 Set_Instance_Of (E1, E2);
10837 if Is_Type (E1) and then Is_Tagged_Type (E2) then
10838 Set_Instance_Of (Class_Wide_Type (E1), Class_Wide_Type (E2));
10841 if Is_Constrained (E1) then
10842 Set_Instance_Of (Base_Type (E1), Base_Type (E2));
10845 if Ekind (E1) = E_Package and then No (Renamed_Object (E1)) then
10846 Map_Formal_Package_Entities (E1, E2);
10853 end Map_Formal_Package_Entities;
10855 -----------------------
10856 -- Move_Freeze_Nodes --
10857 -----------------------
10859 procedure Move_Freeze_Nodes
10860 (Out_Of : Entity_Id;
10865 Next_Decl : Node_Id;
10866 Next_Node : Node_Id := After;
10869 function Is_Outer_Type (T : Entity_Id) return Boolean;
10870 -- Check whether entity is declared in a scope external to that of the
10873 -------------------
10874 -- Is_Outer_Type --
10875 -------------------
10877 function Is_Outer_Type (T : Entity_Id) return Boolean is
10878 Scop : Entity_Id := Scope (T);
10881 if Scope_Depth (Scop) < Scope_Depth (Out_Of) then
10885 while Scop /= Standard_Standard loop
10886 if Scop = Out_Of then
10889 Scop := Scope (Scop);
10897 -- Start of processing for Move_Freeze_Nodes
10904 -- First remove the freeze nodes that may appear before all other
10908 while Present (Decl)
10909 and then Nkind (Decl) = N_Freeze_Entity
10910 and then Is_Outer_Type (Entity (Decl))
10912 Decl := Remove_Head (L);
10913 Insert_After (Next_Node, Decl);
10914 Set_Analyzed (Decl, False);
10919 -- Next scan the list of declarations and remove each freeze node that
10920 -- appears ahead of the current node.
10922 while Present (Decl) loop
10923 while Present (Next (Decl))
10924 and then Nkind (Next (Decl)) = N_Freeze_Entity
10925 and then Is_Outer_Type (Entity (Next (Decl)))
10927 Next_Decl := Remove_Next (Decl);
10928 Insert_After (Next_Node, Next_Decl);
10929 Set_Analyzed (Next_Decl, False);
10930 Next_Node := Next_Decl;
10933 -- If the declaration is a nested package or concurrent type, then
10934 -- recurse. Nested generic packages will have been processed from the
10937 case Nkind (Decl) is
10938 when N_Package_Declaration =>
10939 Spec := Specification (Decl);
10941 when N_Task_Type_Declaration =>
10942 Spec := Task_Definition (Decl);
10944 when N_Protected_Type_Declaration =>
10945 Spec := Protected_Definition (Decl);
10951 if Present (Spec) then
10952 Move_Freeze_Nodes (Out_Of, Next_Node, Visible_Declarations (Spec));
10953 Move_Freeze_Nodes (Out_Of, Next_Node, Private_Declarations (Spec));
10958 end Move_Freeze_Nodes;
10964 function Next_Assoc (E : Assoc_Ptr) return Assoc_Ptr is
10966 return Generic_Renamings.Table (E).Next_In_HTable;
10969 ------------------------
10970 -- Preanalyze_Actuals --
10971 ------------------------
10973 procedure Preanalyze_Actuals (N : Node_Id) is
10976 Errs : constant Int := Serious_Errors_Detected;
10978 Cur : Entity_Id := Empty;
10979 -- Current homograph of the instance name
10982 -- Saved visibility status of the current homograph
10985 Assoc := First (Generic_Associations (N));
10987 -- If the instance is a child unit, its name may hide an outer homonym,
10988 -- so make it invisible to perform name resolution on the actuals.
10990 if Nkind (Defining_Unit_Name (N)) = N_Defining_Program_Unit_Name
10992 (Current_Entity (Defining_Identifier (Defining_Unit_Name (N))))
10994 Cur := Current_Entity (Defining_Identifier (Defining_Unit_Name (N)));
10996 if Is_Compilation_Unit (Cur) then
10997 Vis := Is_Immediately_Visible (Cur);
10998 Set_Is_Immediately_Visible (Cur, False);
11004 while Present (Assoc) loop
11005 if Nkind (Assoc) /= N_Others_Choice then
11006 Act := Explicit_Generic_Actual_Parameter (Assoc);
11008 -- Within a nested instantiation, a defaulted actual is an empty
11009 -- association, so nothing to analyze. If the subprogram actual
11010 -- is an attribute, analyze prefix only, because actual is not a
11011 -- complete attribute reference.
11013 -- If actual is an allocator, analyze expression only. The full
11014 -- analysis can generate code, and if instance is a compilation
11015 -- unit we have to wait until the package instance is installed
11016 -- to have a proper place to insert this code.
11018 -- String literals may be operators, but at this point we do not
11019 -- know whether the actual is a formal subprogram or a string.
11024 elsif Nkind (Act) = N_Attribute_Reference then
11025 Analyze (Prefix (Act));
11027 elsif Nkind (Act) = N_Explicit_Dereference then
11028 Analyze (Prefix (Act));
11030 elsif Nkind (Act) = N_Allocator then
11032 Expr : constant Node_Id := Expression (Act);
11035 if Nkind (Expr) = N_Subtype_Indication then
11036 Analyze (Subtype_Mark (Expr));
11038 -- Analyze separately each discriminant constraint, when
11039 -- given with a named association.
11045 Constr := First (Constraints (Constraint (Expr)));
11046 while Present (Constr) loop
11047 if Nkind (Constr) = N_Discriminant_Association then
11048 Analyze (Expression (Constr));
11062 elsif Nkind (Act) /= N_Operator_Symbol then
11066 if Errs /= Serious_Errors_Detected then
11068 -- Do a minimal analysis of the generic, to prevent spurious
11069 -- warnings complaining about the generic being unreferenced,
11070 -- before abandoning the instantiation.
11072 Analyze (Name (N));
11074 if Is_Entity_Name (Name (N))
11075 and then Etype (Name (N)) /= Any_Type
11077 Generate_Reference (Entity (Name (N)), Name (N));
11078 Set_Is_Instantiated (Entity (Name (N)));
11081 if Present (Cur) then
11083 -- For the case of a child instance hiding an outer homonym,
11084 -- provide additional warning which might explain the error.
11086 Set_Is_Immediately_Visible (Cur, Vis);
11087 Error_Msg_NE ("& hides outer unit with the same name?",
11088 N, Defining_Unit_Name (N));
11091 Abandon_Instantiation (Act);
11098 if Present (Cur) then
11099 Set_Is_Immediately_Visible (Cur, Vis);
11101 end Preanalyze_Actuals;
11103 -------------------
11104 -- Remove_Parent --
11105 -------------------
11107 procedure Remove_Parent (In_Body : Boolean := False) is
11108 S : Entity_Id := Current_Scope;
11109 -- S is the scope containing the instantiation just completed. The scope
11110 -- stack contains the parent instances of the instantiation, followed by
11118 -- After child instantiation is complete, remove from scope stack the
11119 -- extra copy of the current scope, and then remove parent instances.
11121 if not In_Body then
11124 while Current_Scope /= S loop
11125 P := Current_Scope;
11126 End_Package_Scope (Current_Scope);
11128 if In_Open_Scopes (P) then
11129 E := First_Entity (P);
11130 while Present (E) loop
11131 Set_Is_Immediately_Visible (E, True);
11135 if Is_Generic_Instance (Current_Scope)
11136 and then P /= Current_Scope
11138 -- We are within an instance of some sibling. Retain
11139 -- visibility of parent, for proper subsequent cleanup, and
11140 -- reinstall private declarations as well.
11142 Set_In_Private_Part (P);
11143 Install_Private_Declarations (P);
11146 -- If the ultimate parent is a top-level unit recorded in
11147 -- Instance_Parent_Unit, then reset its visibility to what is was
11148 -- before instantiation. (It's not clear what the purpose is of
11149 -- testing whether Scope (P) is In_Open_Scopes, but that test was
11150 -- present before the ultimate parent test was added.???)
11152 elsif not In_Open_Scopes (Scope (P))
11153 or else (P = Instance_Parent_Unit
11154 and then not Parent_Unit_Visible)
11156 Set_Is_Immediately_Visible (P, False);
11158 -- If the current scope is itself an instantiation of a generic
11159 -- nested within P, and we are in the private part of body of this
11160 -- instantiation, restore the full views of P, that were removed
11161 -- in End_Package_Scope above. This obscure case can occur when a
11162 -- subunit of a generic contains an instance of a child unit of
11163 -- its generic parent unit.
11165 elsif S = Current_Scope and then Is_Generic_Instance (S) then
11167 Par : constant Entity_Id :=
11169 (Specification (Unit_Declaration_Node (S)));
11172 and then P = Scope (Par)
11173 and then (In_Package_Body (S) or else In_Private_Part (S))
11175 Set_In_Private_Part (P);
11176 Install_Private_Declarations (P);
11182 -- Reset visibility of entities in the enclosing scope
11184 Set_Is_Hidden_Open_Scope (Current_Scope, False);
11186 Hidden := First_Elmt (Hidden_Entities);
11187 while Present (Hidden) loop
11188 Set_Is_Immediately_Visible (Node (Hidden), True);
11189 Next_Elmt (Hidden);
11193 -- Each body is analyzed separately, and there is no context that
11194 -- needs preserving from one body instance to the next, so remove all
11195 -- parent scopes that have been installed.
11197 while Present (S) loop
11198 End_Package_Scope (S);
11199 Set_Is_Immediately_Visible (S, False);
11200 S := Current_Scope;
11201 exit when S = Standard_Standard;
11210 procedure Restore_Env is
11211 Saved : Instance_Env renames Instance_Envs.Table (Instance_Envs.Last);
11214 if No (Current_Instantiated_Parent.Act_Id) then
11215 -- Restore environment after subprogram inlining
11217 Restore_Private_Views (Empty);
11220 Current_Instantiated_Parent := Saved.Instantiated_Parent;
11221 Exchanged_Views := Saved.Exchanged_Views;
11222 Hidden_Entities := Saved.Hidden_Entities;
11223 Current_Sem_Unit := Saved.Current_Sem_Unit;
11224 Parent_Unit_Visible := Saved.Parent_Unit_Visible;
11225 Instance_Parent_Unit := Saved.Instance_Parent_Unit;
11227 Restore_Opt_Config_Switches (Saved.Switches);
11229 Instance_Envs.Decrement_Last;
11232 ---------------------------
11233 -- Restore_Private_Views --
11234 ---------------------------
11236 procedure Restore_Private_Views
11237 (Pack_Id : Entity_Id;
11238 Is_Package : Boolean := True)
11243 Dep_Elmt : Elmt_Id;
11246 procedure Restore_Nested_Formal (Formal : Entity_Id);
11247 -- Hide the generic formals of formal packages declared with box which
11248 -- were reachable in the current instantiation.
11250 ---------------------------
11251 -- Restore_Nested_Formal --
11252 ---------------------------
11254 procedure Restore_Nested_Formal (Formal : Entity_Id) is
11258 if Present (Renamed_Object (Formal))
11259 and then Denotes_Formal_Package (Renamed_Object (Formal), True)
11263 elsif Present (Associated_Formal_Package (Formal)) then
11264 Ent := First_Entity (Formal);
11265 while Present (Ent) loop
11266 exit when Ekind (Ent) = E_Package
11267 and then Renamed_Entity (Ent) = Renamed_Entity (Formal);
11269 Set_Is_Hidden (Ent);
11270 Set_Is_Potentially_Use_Visible (Ent, False);
11272 -- If package, then recurse
11274 if Ekind (Ent) = E_Package then
11275 Restore_Nested_Formal (Ent);
11281 end Restore_Nested_Formal;
11283 -- Start of processing for Restore_Private_Views
11286 M := First_Elmt (Exchanged_Views);
11287 while Present (M) loop
11290 -- Subtypes of types whose views have been exchanged, and that
11291 -- are defined within the instance, were not on the list of
11292 -- Private_Dependents on entry to the instance, so they have to be
11293 -- exchanged explicitly now, in order to remain consistent with the
11294 -- view of the parent type.
11296 if Ekind (Typ) = E_Private_Type
11297 or else Ekind (Typ) = E_Limited_Private_Type
11298 or else Ekind (Typ) = E_Record_Type_With_Private
11300 Dep_Elmt := First_Elmt (Private_Dependents (Typ));
11301 while Present (Dep_Elmt) loop
11302 Dep_Typ := Node (Dep_Elmt);
11304 if Scope (Dep_Typ) = Pack_Id
11305 and then Present (Full_View (Dep_Typ))
11307 Replace_Elmt (Dep_Elmt, Full_View (Dep_Typ));
11308 Exchange_Declarations (Dep_Typ);
11311 Next_Elmt (Dep_Elmt);
11315 Exchange_Declarations (Node (M));
11319 if No (Pack_Id) then
11323 -- Make the generic formal parameters private, and make the formal types
11324 -- into subtypes of the actuals again.
11326 E := First_Entity (Pack_Id);
11327 while Present (E) loop
11328 Set_Is_Hidden (E, True);
11331 and then Nkind (Parent (E)) = N_Subtype_Declaration
11333 Set_Is_Generic_Actual_Type (E, False);
11335 -- An unusual case of aliasing: the actual may also be directly
11336 -- visible in the generic, and be private there, while it is fully
11337 -- visible in the context of the instance. The internal subtype
11338 -- is private in the instance, but has full visibility like its
11339 -- parent in the enclosing scope. This enforces the invariant that
11340 -- the privacy status of all private dependents of a type coincide
11341 -- with that of the parent type. This can only happen when a
11342 -- generic child unit is instantiated within sibling.
11344 if Is_Private_Type (E)
11345 and then not Is_Private_Type (Etype (E))
11347 Exchange_Declarations (E);
11350 elsif Ekind (E) = E_Package then
11352 -- The end of the renaming list is the renaming of the generic
11353 -- package itself. If the instance is a subprogram, all entities
11354 -- in the corresponding package are renamings. If this entity is
11355 -- a formal package, make its own formals private as well. The
11356 -- actual in this case is itself the renaming of an instantiation.
11357 -- If the entity is not a package renaming, it is the entity
11358 -- created to validate formal package actuals: ignore.
11360 -- If the actual is itself a formal package for the enclosing
11361 -- generic, or the actual for such a formal package, it remains
11362 -- visible on exit from the instance, and therefore nothing needs
11363 -- to be done either, except to keep it accessible.
11366 and then Renamed_Object (E) = Pack_Id
11370 elsif Nkind (Parent (E)) /= N_Package_Renaming_Declaration then
11374 Denotes_Formal_Package (Renamed_Object (E), True, Pack_Id)
11376 Set_Is_Hidden (E, False);
11380 Act_P : constant Entity_Id := Renamed_Object (E);
11384 Id := First_Entity (Act_P);
11386 and then Id /= First_Private_Entity (Act_P)
11388 exit when Ekind (Id) = E_Package
11389 and then Renamed_Object (Id) = Act_P;
11391 Set_Is_Hidden (Id, True);
11392 Set_Is_Potentially_Use_Visible (Id, In_Use (Act_P));
11394 if Ekind (Id) = E_Package then
11395 Restore_Nested_Formal (Id);
11406 end Restore_Private_Views;
11413 (Gen_Unit : Entity_Id;
11414 Act_Unit : Entity_Id)
11418 Set_Instance_Env (Gen_Unit, Act_Unit);
11421 ----------------------------
11422 -- Save_Global_References --
11423 ----------------------------
11425 procedure Save_Global_References (N : Node_Id) is
11426 Gen_Scope : Entity_Id;
11430 function Is_Global (E : Entity_Id) return Boolean;
11431 -- Check whether entity is defined outside of generic unit. Examine the
11432 -- scope of an entity, and the scope of the scope, etc, until we find
11433 -- either Standard, in which case the entity is global, or the generic
11434 -- unit itself, which indicates that the entity is local. If the entity
11435 -- is the generic unit itself, as in the case of a recursive call, or
11436 -- the enclosing generic unit, if different from the current scope, then
11437 -- it is local as well, because it will be replaced at the point of
11438 -- instantiation. On the other hand, if it is a reference to a child
11439 -- unit of a common ancestor, which appears in an instantiation, it is
11440 -- global because it is used to denote a specific compilation unit at
11441 -- the time the instantiations will be analyzed.
11443 procedure Reset_Entity (N : Node_Id);
11444 -- Save semantic information on global entity so that it is not resolved
11445 -- again at instantiation time.
11447 procedure Save_Entity_Descendants (N : Node_Id);
11448 -- Apply Save_Global_References to the two syntactic descendants of
11449 -- non-terminal nodes that carry an Associated_Node and are processed
11450 -- through Reset_Entity. Once the global entity (if any) has been
11451 -- captured together with its type, only two syntactic descendants need
11452 -- to be traversed to complete the processing of the tree rooted at N.
11453 -- This applies to Selected_Components, Expanded_Names, and to Operator
11454 -- nodes. N can also be a character literal, identifier, or operator
11455 -- symbol node, but the call has no effect in these cases.
11457 procedure Save_Global_Defaults (N1, N2 : Node_Id);
11458 -- Default actuals in nested instances must be handled specially
11459 -- because there is no link to them from the original tree. When an
11460 -- actual subprogram is given by a default, we add an explicit generic
11461 -- association for it in the instantiation node. When we save the
11462 -- global references on the name of the instance, we recover the list
11463 -- of generic associations, and add an explicit one to the original
11464 -- generic tree, through which a global actual can be preserved.
11465 -- Similarly, if a child unit is instantiated within a sibling, in the
11466 -- context of the parent, we must preserve the identifier of the parent
11467 -- so that it can be properly resolved in a subsequent instantiation.
11469 procedure Save_Global_Descendant (D : Union_Id);
11470 -- Apply Save_Global_References recursively to the descendents of the
11473 procedure Save_References (N : Node_Id);
11474 -- This is the recursive procedure that does the work, once the
11475 -- enclosing generic scope has been established.
11481 function Is_Global (E : Entity_Id) return Boolean is
11484 function Is_Instance_Node (Decl : Node_Id) return Boolean;
11485 -- Determine whether the parent node of a reference to a child unit
11486 -- denotes an instantiation or a formal package, in which case the
11487 -- reference to the child unit is global, even if it appears within
11488 -- the current scope (e.g. when the instance appears within the body
11489 -- of an ancestor).
11491 ----------------------
11492 -- Is_Instance_Node --
11493 ----------------------
11495 function Is_Instance_Node (Decl : Node_Id) return Boolean is
11497 return Nkind (Decl) in N_Generic_Instantiation
11499 Nkind (Original_Node (Decl)) = N_Formal_Package_Declaration;
11500 end Is_Instance_Node;
11502 -- Start of processing for Is_Global
11505 if E = Gen_Scope then
11508 elsif E = Standard_Standard then
11511 elsif Is_Child_Unit (E)
11512 and then (Is_Instance_Node (Parent (N2))
11513 or else (Nkind (Parent (N2)) = N_Expanded_Name
11514 and then N2 = Selector_Name (Parent (N2))
11516 Is_Instance_Node (Parent (Parent (N2)))))
11522 while Se /= Gen_Scope loop
11523 if Se = Standard_Standard then
11538 procedure Reset_Entity (N : Node_Id) is
11540 procedure Set_Global_Type (N : Node_Id; N2 : Node_Id);
11541 -- If the type of N2 is global to the generic unit. Save the type in
11542 -- the generic node.
11543 -- What does this comment mean???
11545 function Top_Ancestor (E : Entity_Id) return Entity_Id;
11546 -- Find the ultimate ancestor of the current unit. If it is not a
11547 -- generic unit, then the name of the current unit in the prefix of
11548 -- an expanded name must be replaced with its generic homonym to
11549 -- ensure that it will be properly resolved in an instance.
11551 ---------------------
11552 -- Set_Global_Type --
11553 ---------------------
11555 procedure Set_Global_Type (N : Node_Id; N2 : Node_Id) is
11556 Typ : constant Entity_Id := Etype (N2);
11559 Set_Etype (N, Typ);
11561 if Entity (N) /= N2
11562 and then Has_Private_View (Entity (N))
11564 -- If the entity of N is not the associated node, this is a
11565 -- nested generic and it has an associated node as well, whose
11566 -- type is already the full view (see below). Indicate that the
11567 -- original node has a private view.
11569 Set_Has_Private_View (N);
11572 -- If not a private type, nothing else to do
11574 if not Is_Private_Type (Typ) then
11575 if Is_Array_Type (Typ)
11576 and then Is_Private_Type (Component_Type (Typ))
11578 Set_Has_Private_View (N);
11581 -- If it is a derivation of a private type in a context where no
11582 -- full view is needed, nothing to do either.
11584 elsif No (Full_View (Typ)) and then Typ /= Etype (Typ) then
11587 -- Otherwise mark the type for flipping and use the full view when
11591 Set_Has_Private_View (N);
11593 if Present (Full_View (Typ)) then
11594 Set_Etype (N2, Full_View (Typ));
11597 end Set_Global_Type;
11603 function Top_Ancestor (E : Entity_Id) return Entity_Id is
11608 while Is_Child_Unit (Par) loop
11609 Par := Scope (Par);
11615 -- Start of processing for Reset_Entity
11618 N2 := Get_Associated_Node (N);
11621 if Present (E) then
11622 if Is_Global (E) then
11623 Set_Global_Type (N, N2);
11625 elsif Nkind (N) = N_Op_Concat
11626 and then Is_Generic_Type (Etype (N2))
11628 (Base_Type (Etype (Right_Opnd (N2))) = Etype (N2)
11629 or else Base_Type (Etype (Left_Opnd (N2))) = Etype (N2))
11630 and then Is_Intrinsic_Subprogram (E)
11635 -- Entity is local. Mark generic node as unresolved.
11636 -- Note that now it does not have an entity.
11638 Set_Associated_Node (N, Empty);
11639 Set_Etype (N, Empty);
11642 if Nkind (Parent (N)) in N_Generic_Instantiation
11643 and then N = Name (Parent (N))
11645 Save_Global_Defaults (Parent (N), Parent (N2));
11648 elsif Nkind (Parent (N)) = N_Selected_Component
11649 and then Nkind (Parent (N2)) = N_Expanded_Name
11651 if Is_Global (Entity (Parent (N2))) then
11652 Change_Selected_Component_To_Expanded_Name (Parent (N));
11653 Set_Associated_Node (Parent (N), Parent (N2));
11654 Set_Global_Type (Parent (N), Parent (N2));
11655 Save_Entity_Descendants (N);
11657 -- If this is a reference to the current generic entity, replace
11658 -- by the name of the generic homonym of the current package. This
11659 -- is because in an instantiation Par.P.Q will not resolve to the
11660 -- name of the instance, whose enclosing scope is not necessarily
11661 -- Par. We use the generic homonym rather that the name of the
11662 -- generic itself because it may be hidden by a local declaration.
11664 elsif In_Open_Scopes (Entity (Parent (N2)))
11666 Is_Generic_Unit (Top_Ancestor (Entity (Prefix (Parent (N2)))))
11668 if Ekind (Entity (Parent (N2))) = E_Generic_Package then
11669 Rewrite (Parent (N),
11670 Make_Identifier (Sloc (N),
11672 Chars (Generic_Homonym (Entity (Parent (N2))))));
11674 Rewrite (Parent (N),
11675 Make_Identifier (Sloc (N),
11676 Chars => Chars (Selector_Name (Parent (N2)))));
11680 if Nkind (Parent (Parent (N))) in N_Generic_Instantiation
11681 and then Parent (N) = Name (Parent (Parent (N)))
11683 Save_Global_Defaults
11684 (Parent (Parent (N)), Parent (Parent ((N2))));
11687 -- A selected component may denote a static constant that has been
11688 -- folded. If the static constant is global to the generic, capture
11689 -- its value. Otherwise the folding will happen in any instantiation.
11691 elsif Nkind (Parent (N)) = N_Selected_Component
11692 and then Nkind_In (Parent (N2), N_Integer_Literal, N_Real_Literal)
11694 if Present (Entity (Original_Node (Parent (N2))))
11695 and then Is_Global (Entity (Original_Node (Parent (N2))))
11697 Rewrite (Parent (N), New_Copy (Parent (N2)));
11698 Set_Analyzed (Parent (N), False);
11704 -- A selected component may be transformed into a parameterless
11705 -- function call. If the called entity is global, rewrite the node
11706 -- appropriately, i.e. as an extended name for the global entity.
11708 elsif Nkind (Parent (N)) = N_Selected_Component
11709 and then Nkind (Parent (N2)) = N_Function_Call
11710 and then N = Selector_Name (Parent (N))
11712 if No (Parameter_Associations (Parent (N2))) then
11713 if Is_Global (Entity (Name (Parent (N2)))) then
11714 Change_Selected_Component_To_Expanded_Name (Parent (N));
11715 Set_Associated_Node (Parent (N), Name (Parent (N2)));
11716 Set_Global_Type (Parent (N), Name (Parent (N2)));
11717 Save_Entity_Descendants (N);
11720 Set_Associated_Node (N, Empty);
11721 Set_Etype (N, Empty);
11724 -- In Ada 2005, X.F may be a call to a primitive operation,
11725 -- rewritten as F (X). This rewriting will be done again in an
11726 -- instance, so keep the original node. Global entities will be
11727 -- captured as for other constructs.
11733 -- Entity is local. Reset in generic unit, so that node is resolved
11734 -- anew at the point of instantiation.
11737 Set_Associated_Node (N, Empty);
11738 Set_Etype (N, Empty);
11742 -----------------------------
11743 -- Save_Entity_Descendants --
11744 -----------------------------
11746 procedure Save_Entity_Descendants (N : Node_Id) is
11749 when N_Binary_Op =>
11750 Save_Global_Descendant (Union_Id (Left_Opnd (N)));
11751 Save_Global_Descendant (Union_Id (Right_Opnd (N)));
11754 Save_Global_Descendant (Union_Id (Right_Opnd (N)));
11756 when N_Expanded_Name | N_Selected_Component =>
11757 Save_Global_Descendant (Union_Id (Prefix (N)));
11758 Save_Global_Descendant (Union_Id (Selector_Name (N)));
11760 when N_Identifier | N_Character_Literal | N_Operator_Symbol =>
11764 raise Program_Error;
11766 end Save_Entity_Descendants;
11768 --------------------------
11769 -- Save_Global_Defaults --
11770 --------------------------
11772 procedure Save_Global_Defaults (N1, N2 : Node_Id) is
11773 Loc : constant Source_Ptr := Sloc (N1);
11774 Assoc2 : constant List_Id := Generic_Associations (N2);
11775 Gen_Id : constant Entity_Id := Get_Generic_Entity (N2);
11782 Actual : Entity_Id;
11785 Assoc1 := Generic_Associations (N1);
11787 if Present (Assoc1) then
11788 Act1 := First (Assoc1);
11791 Set_Generic_Associations (N1, New_List);
11792 Assoc1 := Generic_Associations (N1);
11795 if Present (Assoc2) then
11796 Act2 := First (Assoc2);
11801 while Present (Act1) and then Present (Act2) loop
11806 -- Find the associations added for default subprograms
11808 if Present (Act2) then
11809 while Nkind (Act2) /= N_Generic_Association
11810 or else No (Entity (Selector_Name (Act2)))
11811 or else not Is_Overloadable (Entity (Selector_Name (Act2)))
11816 -- Add a similar association if the default is global. The
11817 -- renaming declaration for the actual has been analyzed, and
11818 -- its alias is the program it renames. Link the actual in the
11819 -- original generic tree with the node in the analyzed tree.
11821 while Present (Act2) loop
11822 Subp := Entity (Selector_Name (Act2));
11823 Def := Explicit_Generic_Actual_Parameter (Act2);
11825 -- Following test is defence against rubbish errors
11827 if No (Alias (Subp)) then
11831 -- Retrieve the resolved actual from the renaming declaration
11832 -- created for the instantiated formal.
11834 Actual := Entity (Name (Parent (Parent (Subp))));
11835 Set_Entity (Def, Actual);
11836 Set_Etype (Def, Etype (Actual));
11838 if Is_Global (Actual) then
11840 Make_Generic_Association (Loc,
11841 Selector_Name => New_Occurrence_Of (Subp, Loc),
11842 Explicit_Generic_Actual_Parameter =>
11843 New_Occurrence_Of (Actual, Loc));
11845 Set_Associated_Node
11846 (Explicit_Generic_Actual_Parameter (Ndec), Def);
11848 Append (Ndec, Assoc1);
11850 -- If there are other defaults, add a dummy association in case
11851 -- there are other defaulted formals with the same name.
11853 elsif Present (Next (Act2)) then
11855 Make_Generic_Association (Loc,
11856 Selector_Name => New_Occurrence_Of (Subp, Loc),
11857 Explicit_Generic_Actual_Parameter => Empty);
11859 Append (Ndec, Assoc1);
11866 if Nkind (Name (N1)) = N_Identifier
11867 and then Is_Child_Unit (Gen_Id)
11868 and then Is_Global (Gen_Id)
11869 and then Is_Generic_Unit (Scope (Gen_Id))
11870 and then In_Open_Scopes (Scope (Gen_Id))
11872 -- This is an instantiation of a child unit within a sibling,
11873 -- so that the generic parent is in scope. An eventual instance
11874 -- must occur within the scope of an instance of the parent.
11875 -- Make name in instance into an expanded name, to preserve the
11876 -- identifier of the parent, so it can be resolved subsequently.
11878 Rewrite (Name (N2),
11879 Make_Expanded_Name (Loc,
11880 Chars => Chars (Gen_Id),
11881 Prefix => New_Occurrence_Of (Scope (Gen_Id), Loc),
11882 Selector_Name => New_Occurrence_Of (Gen_Id, Loc)));
11883 Set_Entity (Name (N2), Gen_Id);
11885 Rewrite (Name (N1),
11886 Make_Expanded_Name (Loc,
11887 Chars => Chars (Gen_Id),
11888 Prefix => New_Occurrence_Of (Scope (Gen_Id), Loc),
11889 Selector_Name => New_Occurrence_Of (Gen_Id, Loc)));
11891 Set_Associated_Node (Name (N1), Name (N2));
11892 Set_Associated_Node (Prefix (Name (N1)), Empty);
11893 Set_Associated_Node
11894 (Selector_Name (Name (N1)), Selector_Name (Name (N2)));
11895 Set_Etype (Name (N1), Etype (Gen_Id));
11898 end Save_Global_Defaults;
11900 ----------------------------
11901 -- Save_Global_Descendant --
11902 ----------------------------
11904 procedure Save_Global_Descendant (D : Union_Id) is
11908 if D in Node_Range then
11909 if D = Union_Id (Empty) then
11912 elsif Nkind (Node_Id (D)) /= N_Compilation_Unit then
11913 Save_References (Node_Id (D));
11916 elsif D in List_Range then
11917 if D = Union_Id (No_List)
11918 or else Is_Empty_List (List_Id (D))
11923 N1 := First (List_Id (D));
11924 while Present (N1) loop
11925 Save_References (N1);
11930 -- Element list or other non-node field, nothing to do
11935 end Save_Global_Descendant;
11937 ---------------------
11938 -- Save_References --
11939 ---------------------
11941 -- This is the recursive procedure that does the work once the enclosing
11942 -- generic scope has been established. We have to treat specially a
11943 -- number of node rewritings that are required by semantic processing
11944 -- and which change the kind of nodes in the generic copy: typically
11945 -- constant-folding, replacing an operator node by a string literal, or
11946 -- a selected component by an expanded name. In each of those cases, the
11947 -- transformation is propagated to the generic unit.
11949 procedure Save_References (N : Node_Id) is
11950 Loc : constant Source_Ptr := Sloc (N);
11956 elsif Nkind_In (N, N_Character_Literal, N_Operator_Symbol) then
11957 if Nkind (N) = Nkind (Get_Associated_Node (N)) then
11960 elsif Nkind (N) = N_Operator_Symbol
11961 and then Nkind (Get_Associated_Node (N)) = N_String_Literal
11963 Change_Operator_Symbol_To_String_Literal (N);
11966 elsif Nkind (N) in N_Op then
11967 if Nkind (N) = Nkind (Get_Associated_Node (N)) then
11968 if Nkind (N) = N_Op_Concat then
11969 Set_Is_Component_Left_Opnd (N,
11970 Is_Component_Left_Opnd (Get_Associated_Node (N)));
11972 Set_Is_Component_Right_Opnd (N,
11973 Is_Component_Right_Opnd (Get_Associated_Node (N)));
11979 -- Node may be transformed into call to a user-defined operator
11981 N2 := Get_Associated_Node (N);
11983 if Nkind (N2) = N_Function_Call then
11984 E := Entity (Name (N2));
11987 and then Is_Global (E)
11989 Set_Etype (N, Etype (N2));
11991 Set_Associated_Node (N, Empty);
11992 Set_Etype (N, Empty);
11995 elsif Nkind_In (N2, N_Integer_Literal,
11999 if Present (Original_Node (N2))
12000 and then Nkind (Original_Node (N2)) = Nkind (N)
12003 -- Operation was constant-folded. Whenever possible,
12004 -- recover semantic information from unfolded node,
12007 Set_Associated_Node (N, Original_Node (N2));
12009 if Nkind (N) = N_Op_Concat then
12010 Set_Is_Component_Left_Opnd (N,
12011 Is_Component_Left_Opnd (Get_Associated_Node (N)));
12012 Set_Is_Component_Right_Opnd (N,
12013 Is_Component_Right_Opnd (Get_Associated_Node (N)));
12019 -- If original node is already modified, propagate
12020 -- constant-folding to template.
12022 Rewrite (N, New_Copy (N2));
12023 Set_Analyzed (N, False);
12026 elsif Nkind (N2) = N_Identifier
12027 and then Ekind (Entity (N2)) = E_Enumeration_Literal
12029 -- Same if call was folded into a literal, but in this case
12030 -- retain the entity to avoid spurious ambiguities if it is
12031 -- overloaded at the point of instantiation or inlining.
12033 Rewrite (N, New_Copy (N2));
12034 Set_Analyzed (N, False);
12038 -- Complete operands check if node has not been constant-folded
12040 if Nkind (N) in N_Op then
12041 Save_Entity_Descendants (N);
12044 elsif Nkind (N) = N_Identifier then
12045 if Nkind (N) = Nkind (Get_Associated_Node (N)) then
12047 -- If this is a discriminant reference, always save it. It is
12048 -- used in the instance to find the corresponding discriminant
12049 -- positionally rather than by name.
12051 Set_Original_Discriminant
12052 (N, Original_Discriminant (Get_Associated_Node (N)));
12056 N2 := Get_Associated_Node (N);
12058 if Nkind (N2) = N_Function_Call then
12059 E := Entity (Name (N2));
12061 -- Name resolves to a call to parameterless function. If
12062 -- original entity is global, mark node as resolved.
12065 and then Is_Global (E)
12067 Set_Etype (N, Etype (N2));
12069 Set_Associated_Node (N, Empty);
12070 Set_Etype (N, Empty);
12073 elsif Nkind_In (N2, N_Integer_Literal, N_Real_Literal)
12074 and then Is_Entity_Name (Original_Node (N2))
12076 -- Name resolves to named number that is constant-folded,
12077 -- We must preserve the original name for ASIS use, and
12078 -- undo the constant-folding, which will be repeated in
12081 Set_Associated_Node (N, Original_Node (N2));
12084 elsif Nkind (N2) = N_String_Literal then
12086 -- Name resolves to string literal. Perform the same
12087 -- replacement in generic.
12089 Rewrite (N, New_Copy (N2));
12091 elsif Nkind (N2) = N_Explicit_Dereference then
12093 -- An identifier is rewritten as a dereference if it is the
12094 -- prefix in an implicit dereference (call or attribute).
12095 -- The analysis of an instantiation will expand the node
12096 -- again, so we preserve the original tree but link it to
12097 -- the resolved entity in case it is global.
12099 if Is_Entity_Name (Prefix (N2))
12100 and then Present (Entity (Prefix (N2)))
12101 and then Is_Global (Entity (Prefix (N2)))
12103 Set_Associated_Node (N, Prefix (N2));
12105 elsif Nkind (Prefix (N2)) = N_Function_Call
12106 and then Is_Global (Entity (Name (Prefix (N2))))
12109 Make_Explicit_Dereference (Loc,
12110 Prefix => Make_Function_Call (Loc,
12112 New_Occurrence_Of (Entity (Name (Prefix (N2))),
12116 Set_Associated_Node (N, Empty);
12117 Set_Etype (N, Empty);
12120 -- The subtype mark of a nominally unconstrained object is
12121 -- rewritten as a subtype indication using the bounds of the
12122 -- expression. Recover the original subtype mark.
12124 elsif Nkind (N2) = N_Subtype_Indication
12125 and then Is_Entity_Name (Original_Node (N2))
12127 Set_Associated_Node (N, Original_Node (N2));
12135 elsif Nkind (N) in N_Entity then
12140 Qual : Node_Id := Empty;
12141 Typ : Entity_Id := Empty;
12144 use Atree.Unchecked_Access;
12145 -- This code section is part of implementing an untyped tree
12146 -- traversal, so it needs direct access to node fields.
12149 if Nkind_In (N, N_Aggregate, N_Extension_Aggregate) then
12150 N2 := Get_Associated_Node (N);
12157 -- In an instance within a generic, use the name of the
12158 -- actual and not the original generic parameter. If the
12159 -- actual is global in the current generic it must be
12160 -- preserved for its instantiation.
12162 if Nkind (Parent (Typ)) = N_Subtype_Declaration
12164 Present (Generic_Parent_Type (Parent (Typ)))
12166 Typ := Base_Type (Typ);
12167 Set_Etype (N2, Typ);
12173 or else not Is_Global (Typ)
12175 Set_Associated_Node (N, Empty);
12177 -- If the aggregate is an actual in a call, it has been
12178 -- resolved in the current context, to some local type.
12179 -- The enclosing call may have been disambiguated by the
12180 -- aggregate, and this disambiguation might fail at
12181 -- instantiation time because the type to which the
12182 -- aggregate did resolve is not preserved. In order to
12183 -- preserve some of this information, we wrap the
12184 -- aggregate in a qualified expression, using the id of
12185 -- its type. For further disambiguation we qualify the
12186 -- type name with its scope (if visible) because both
12187 -- id's will have corresponding entities in an instance.
12188 -- This resolves most of the problems with missing type
12189 -- information on aggregates in instances.
12191 if Nkind (N2) = Nkind (N)
12193 Nkind_In (Parent (N2), N_Procedure_Call_Statement,
12195 and then Comes_From_Source (Typ)
12197 if Is_Immediately_Visible (Scope (Typ)) then
12198 Nam := Make_Selected_Component (Loc,
12200 Make_Identifier (Loc, Chars (Scope (Typ))),
12202 Make_Identifier (Loc, Chars (Typ)));
12204 Nam := Make_Identifier (Loc, Chars (Typ));
12208 Make_Qualified_Expression (Loc,
12209 Subtype_Mark => Nam,
12210 Expression => Relocate_Node (N));
12214 Save_Global_Descendant (Field1 (N));
12215 Save_Global_Descendant (Field2 (N));
12216 Save_Global_Descendant (Field3 (N));
12217 Save_Global_Descendant (Field5 (N));
12219 if Present (Qual) then
12223 -- All other cases than aggregates
12227 -- For pragmas, we propagate the Enabled status for the
12228 -- relevant pragmas to the original generic tree. This was
12229 -- originally needed for SCO generation. It is no longer
12230 -- needed there (since we use the Sloc value in calls to
12231 -- Set_SCO_Pragma_Enabled), but it seems a generally good
12232 -- idea to have this flag set properly.
12234 if Nkind (N) = N_Pragma
12236 (Pragma_Name (N) = Name_Precondition
12237 or else Pragma_Name (N) = Name_Postcondition)
12238 and then Present (Associated_Node (Pragma_Identifier (N)))
12240 Set_Pragma_Enabled (N,
12242 (Parent (Associated_Node (Pragma_Identifier (N)))));
12245 Save_Global_Descendant (Field1 (N));
12246 Save_Global_Descendant (Field2 (N));
12247 Save_Global_Descendant (Field3 (N));
12248 Save_Global_Descendant (Field4 (N));
12249 Save_Global_Descendant (Field5 (N));
12253 end Save_References;
12255 -- Start of processing for Save_Global_References
12258 Gen_Scope := Current_Scope;
12260 -- If the generic unit is a child unit, references to entities in the
12261 -- parent are treated as local, because they will be resolved anew in
12262 -- the context of the instance of the parent.
12264 while Is_Child_Unit (Gen_Scope)
12265 and then Ekind (Scope (Gen_Scope)) = E_Generic_Package
12267 Gen_Scope := Scope (Gen_Scope);
12270 Save_References (N);
12271 end Save_Global_References;
12273 --------------------------------------
12274 -- Set_Copied_Sloc_For_Inlined_Body --
12275 --------------------------------------
12277 procedure Set_Copied_Sloc_For_Inlined_Body (N : Node_Id; E : Entity_Id) is
12279 Create_Instantiation_Source (N, E, True, S_Adjustment);
12280 end Set_Copied_Sloc_For_Inlined_Body;
12282 ---------------------
12283 -- Set_Instance_Of --
12284 ---------------------
12286 procedure Set_Instance_Of (A : Entity_Id; B : Entity_Id) is
12288 Generic_Renamings.Table (Generic_Renamings.Last) := (A, B, Assoc_Null);
12289 Generic_Renamings_HTable.Set (Generic_Renamings.Last);
12290 Generic_Renamings.Increment_Last;
12291 end Set_Instance_Of;
12293 --------------------
12294 -- Set_Next_Assoc --
12295 --------------------
12297 procedure Set_Next_Assoc (E : Assoc_Ptr; Next : Assoc_Ptr) is
12299 Generic_Renamings.Table (E).Next_In_HTable := Next;
12300 end Set_Next_Assoc;
12302 -------------------
12303 -- Start_Generic --
12304 -------------------
12306 procedure Start_Generic is
12308 -- ??? More things could be factored out in this routine.
12309 -- Should probably be done at a later stage.
12311 Generic_Flags.Append (Inside_A_Generic);
12312 Inside_A_Generic := True;
12314 Expander_Mode_Save_And_Set (False);
12317 ----------------------
12318 -- Set_Instance_Env --
12319 ----------------------
12321 procedure Set_Instance_Env
12322 (Gen_Unit : Entity_Id;
12323 Act_Unit : Entity_Id)
12326 -- Regardless of the current mode, predefined units are analyzed in
12327 -- the most current Ada mode, and earlier version Ada checks do not
12328 -- apply to predefined units. Nothing needs to be done for non-internal
12329 -- units. These are always analyzed in the current mode.
12331 if Is_Internal_File_Name
12332 (Fname => Unit_File_Name (Get_Source_Unit (Gen_Unit)),
12333 Renamings_Included => True)
12335 Set_Opt_Config_Switches (True, Current_Sem_Unit = Main_Unit);
12338 Current_Instantiated_Parent := (Gen_Unit, Act_Unit, Assoc_Null);
12339 end Set_Instance_Env;
12345 procedure Switch_View (T : Entity_Id) is
12346 BT : constant Entity_Id := Base_Type (T);
12347 Priv_Elmt : Elmt_Id := No_Elmt;
12348 Priv_Sub : Entity_Id;
12351 -- T may be private but its base type may have been exchanged through
12352 -- some other occurrence, in which case there is nothing to switch
12353 -- besides T itself. Note that a private dependent subtype of a private
12354 -- type might not have been switched even if the base type has been,
12355 -- because of the last branch of Check_Private_View (see comment there).
12357 if not Is_Private_Type (BT) then
12358 Prepend_Elmt (Full_View (T), Exchanged_Views);
12359 Exchange_Declarations (T);
12363 Priv_Elmt := First_Elmt (Private_Dependents (BT));
12365 if Present (Full_View (BT)) then
12366 Prepend_Elmt (Full_View (BT), Exchanged_Views);
12367 Exchange_Declarations (BT);
12370 while Present (Priv_Elmt) loop
12371 Priv_Sub := (Node (Priv_Elmt));
12373 -- We avoid flipping the subtype if the Etype of its full view is
12374 -- private because this would result in a malformed subtype. This
12375 -- occurs when the Etype of the subtype full view is the full view of
12376 -- the base type (and since the base types were just switched, the
12377 -- subtype is pointing to the wrong view). This is currently the case
12378 -- for tagged record types, access types (maybe more?) and needs to
12379 -- be resolved. ???
12381 if Present (Full_View (Priv_Sub))
12382 and then not Is_Private_Type (Etype (Full_View (Priv_Sub)))
12384 Prepend_Elmt (Full_View (Priv_Sub), Exchanged_Views);
12385 Exchange_Declarations (Priv_Sub);
12388 Next_Elmt (Priv_Elmt);
12392 -----------------------------
12393 -- Valid_Default_Attribute --
12394 -----------------------------
12396 procedure Valid_Default_Attribute (Nam : Entity_Id; Def : Node_Id) is
12397 Attr_Id : constant Attribute_Id :=
12398 Get_Attribute_Id (Attribute_Name (Def));
12399 T : constant Entity_Id := Entity (Prefix (Def));
12400 Is_Fun : constant Boolean := (Ekind (Nam) = E_Function);
12413 F := First_Formal (Nam);
12414 while Present (F) loop
12415 Num_F := Num_F + 1;
12420 when Attribute_Adjacent | Attribute_Ceiling | Attribute_Copy_Sign |
12421 Attribute_Floor | Attribute_Fraction | Attribute_Machine |
12422 Attribute_Model | Attribute_Remainder | Attribute_Rounding |
12423 Attribute_Unbiased_Rounding =>
12426 and then Is_Floating_Point_Type (T);
12428 when Attribute_Image | Attribute_Pred | Attribute_Succ |
12429 Attribute_Value | Attribute_Wide_Image |
12430 Attribute_Wide_Value =>
12431 OK := (Is_Fun and then Num_F = 1 and then Is_Scalar_Type (T));
12433 when Attribute_Max | Attribute_Min =>
12434 OK := (Is_Fun and then Num_F = 2 and then Is_Scalar_Type (T));
12436 when Attribute_Input =>
12437 OK := (Is_Fun and then Num_F = 1);
12439 when Attribute_Output | Attribute_Read | Attribute_Write =>
12440 OK := (not Is_Fun and then Num_F = 2);
12447 Error_Msg_N ("attribute reference has wrong profile for subprogram",
12450 end Valid_Default_Attribute;