1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
9 -- Copyright (C) 1992-2008, 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 Set_Instance_Env
479 (Gen_Unit : Entity_Id;
480 Act_Unit : Entity_Id);
481 -- Save current instance on saved environment, to be used to determine
482 -- the global status of entities in nested instances. Part of Save_Env.
483 -- called after verifying that the generic unit is legal for the instance,
484 -- The procedure also examines whether the generic unit is a predefined
485 -- unit, in order to set configuration switches accordingly. As a result
486 -- the procedure must be called after analyzing and freezing the actuals.
488 procedure Set_Instance_Of (A : Entity_Id; B : Entity_Id);
489 -- Associate analyzed generic parameter with corresponding
490 -- instance. Used for semantic checks at instantiation time.
492 function Has_Been_Exchanged (E : Entity_Id) return Boolean;
493 -- Traverse the Exchanged_Views list to see if a type was private
494 -- and has already been flipped during this phase of instantiation.
496 procedure Hide_Current_Scope;
497 -- When instantiating a generic child unit, the parent context must be
498 -- present, but the instance and all entities that may be generated
499 -- must be inserted in the current scope. We leave the current scope
500 -- on the stack, but make its entities invisible to avoid visibility
501 -- problems. This is reversed at the end of the instantiation. This is
502 -- not done for the instantiation of the bodies, which only require the
503 -- instances of the generic parents to be in scope.
505 procedure Install_Body
510 -- If the instantiation happens textually before the body of the generic,
511 -- the instantiation of the body must be analyzed after the generic body,
512 -- and not at the point of instantiation. Such early instantiations can
513 -- happen if the generic and the instance appear in a package declaration
514 -- because the generic body can only appear in the corresponding package
515 -- body. Early instantiations can also appear if generic, instance and
516 -- body are all in the declarative part of a subprogram or entry. Entities
517 -- of packages that are early instantiations are delayed, and their freeze
518 -- node appears after the generic body.
520 procedure Insert_After_Last_Decl (N : Node_Id; F_Node : Node_Id);
521 -- Insert freeze node at the end of the declarative part that includes the
522 -- instance node N. If N is in the visible part of an enclosing package
523 -- declaration, the freeze node has to be inserted at the end of the
524 -- private declarations, if any.
526 procedure Freeze_Subprogram_Body
527 (Inst_Node : Node_Id;
529 Pack_Id : Entity_Id);
530 -- The generic body may appear textually after the instance, including
531 -- in the proper body of a stub, or within a different package instance.
532 -- Given that the instance can only be elaborated after the generic, we
533 -- place freeze_nodes for the instance and/or for packages that may enclose
534 -- the instance and the generic, so that the back-end can establish the
535 -- proper order of elaboration.
538 -- Establish environment for subsequent instantiation. Separated from
539 -- Save_Env because data-structures for visibility handling must be
540 -- initialized before call to Check_Generic_Child_Unit.
542 procedure Install_Formal_Packages (Par : Entity_Id);
543 -- If any of the formals of the parent are formal packages with box,
544 -- their formal parts are visible in the parent and thus in the child
545 -- unit as well. Analogous to what is done in Check_Generic_Actuals
546 -- for the unit itself. This procedure is also used in an instance, to
547 -- make visible the proper entities of the actual for a formal package
548 -- declared with a box.
550 procedure Install_Parent (P : Entity_Id; In_Body : Boolean := False);
551 -- When compiling an instance of a child unit the parent (which is
552 -- itself an instance) is an enclosing scope that must be made
553 -- immediately visible. This procedure is also used to install the non-
554 -- generic parent of a generic child unit when compiling its body, so
555 -- that full views of types in the parent are made visible.
557 procedure Remove_Parent (In_Body : Boolean := False);
558 -- Reverse effect after instantiation of child is complete
560 procedure Inline_Instance_Body
562 Gen_Unit : Entity_Id;
564 -- If front-end inlining is requested, instantiate the package body,
565 -- and preserve the visibility of its compilation unit, to insure
566 -- that successive instantiations succeed.
568 -- The functions Instantiate_XXX perform various legality checks and build
569 -- the declarations for instantiated generic parameters. In all of these
570 -- Formal is the entity in the generic unit, Actual is the entity of
571 -- expression in the generic associations, and Analyzed_Formal is the
572 -- formal in the generic copy, which contains the semantic information to
573 -- be used to validate the actual.
575 function Instantiate_Object
578 Analyzed_Formal : Node_Id) return List_Id;
580 function Instantiate_Type
583 Analyzed_Formal : Node_Id;
584 Actual_Decls : List_Id) return List_Id;
586 function Instantiate_Formal_Subprogram
589 Analyzed_Formal : Node_Id) return Node_Id;
591 function Instantiate_Formal_Package
594 Analyzed_Formal : Node_Id) return List_Id;
595 -- If the formal package is declared with a box, special visibility rules
596 -- apply to its formals: they are in the visible part of the package. This
597 -- is true in the declarative region of the formal package, that is to say
598 -- in the enclosing generic or instantiation. For an instantiation, the
599 -- parameters of the formal package are made visible in an explicit step.
600 -- Furthermore, if the actual has a visible USE clause, these formals must
601 -- be made potentially use-visible as well. On exit from the enclosing
602 -- instantiation, the reverse must be done.
604 -- For a formal package declared without a box, there are conformance rules
605 -- that apply to the actuals in the generic declaration and the actuals of
606 -- the actual package in the enclosing instantiation. The simplest way to
607 -- apply these rules is to repeat the instantiation of the formal package
608 -- in the context of the enclosing instance, and compare the generic
609 -- associations of this instantiation with those of the actual package.
610 -- This internal instantiation only needs to contain the renamings of the
611 -- formals: the visible and private declarations themselves need not be
614 -- In Ada 2005, the formal package may be only partially parametrized. In
615 -- that case the visibility step must make visible those actuals whose
616 -- corresponding formals were given with a box. A final complication
617 -- involves inherited operations from formal derived types, which must be
618 -- visible if the type is.
620 function Is_In_Main_Unit (N : Node_Id) return Boolean;
621 -- Test if given node is in the main unit
623 procedure Load_Parent_Of_Generic
626 Body_Optional : Boolean := False);
627 -- If the generic appears in a separate non-generic library unit, load the
628 -- corresponding body to retrieve the body of the generic. N is the node
629 -- for the generic instantiation, Spec is the generic package declaration.
631 -- Body_Optional is a flag that indicates that the body is being loaded to
632 -- ensure that temporaries are generated consistently when there are other
633 -- instances in the current declarative part that precede the one being
634 -- loaded. In that case a missing body is acceptable.
636 procedure Inherit_Context (Gen_Decl : Node_Id; Inst : Node_Id);
637 -- Add the context clause of the unit containing a generic unit to an
638 -- instantiation that is a compilation unit.
640 function Get_Associated_Node (N : Node_Id) return Node_Id;
641 -- In order to propagate semantic information back from the analyzed copy
642 -- to the original generic, we maintain links between selected nodes in the
643 -- generic and their corresponding copies. At the end of generic analysis,
644 -- the routine Save_Global_References traverses the generic tree, examines
645 -- the semantic information, and preserves the links to those nodes that
646 -- contain global information. At instantiation, the information from the
647 -- associated node is placed on the new copy, so that name resolution is
650 -- Three kinds of source nodes have associated nodes:
652 -- a) those that can reference (denote) entities, that is identifiers,
653 -- character literals, expanded_names, operator symbols, operators,
654 -- and attribute reference nodes. These nodes have an Entity field
655 -- and are the set of nodes that are in N_Has_Entity.
657 -- b) aggregates (N_Aggregate and N_Extension_Aggregate)
659 -- c) selected components (N_Selected_Component)
661 -- For the first class, the associated node preserves the entity if it is
662 -- global. If the generic contains nested instantiations, the associated
663 -- node itself has been recopied, and a chain of them must be followed.
665 -- For aggregates, the associated node allows retrieval of the type, which
666 -- may otherwise not appear in the generic. The view of this type may be
667 -- different between generic and instantiation, and the full view can be
668 -- installed before the instantiation is analyzed. For aggregates of type
669 -- extensions, the same view exchange may have to be performed for some of
670 -- the ancestor types, if their view is private at the point of
673 -- Nodes that are selected components in the parse tree may be rewritten
674 -- as expanded names after resolution, and must be treated as potential
675 -- entity holders, which is why they also have an Associated_Node.
677 -- Nodes that do not come from source, such as freeze nodes, do not appear
678 -- in the generic tree, and need not have an associated node.
680 -- The associated node is stored in the Associated_Node field. Note that
681 -- this field overlaps Entity, which is fine, because the whole point is
682 -- that we don't need or want the normal Entity field in this situation.
684 procedure Move_Freeze_Nodes
688 -- Freeze nodes can be generated in the analysis of a generic unit, but
689 -- will not be seen by the back-end. It is necessary to move those nodes
690 -- to the enclosing scope if they freeze an outer entity. We place them
691 -- at the end of the enclosing generic package, which is semantically
694 procedure Preanalyze_Actuals (N : Node_Id);
695 -- Analyze actuals to perform name resolution. Full resolution is done
696 -- later, when the expected types are known, but names have to be captured
697 -- before installing parents of generics, that are not visible for the
698 -- actuals themselves.
700 procedure Valid_Default_Attribute (Nam : Entity_Id; Def : Node_Id);
701 -- Verify that an attribute that appears as the default for a formal
702 -- subprogram is a function or procedure with the correct profile.
704 -------------------------------------------
705 -- Data Structures for Generic Renamings --
706 -------------------------------------------
708 -- The map Generic_Renamings associates generic entities with their
709 -- corresponding actuals. Currently used to validate type instances. It
710 -- will eventually be used for all generic parameters to eliminate the
711 -- need for overload resolution in the instance.
713 type Assoc_Ptr is new Int;
715 Assoc_Null : constant Assoc_Ptr := -1;
720 Next_In_HTable : Assoc_Ptr;
723 package Generic_Renamings is new Table.Table
724 (Table_Component_Type => Assoc,
725 Table_Index_Type => Assoc_Ptr,
726 Table_Low_Bound => 0,
728 Table_Increment => 100,
729 Table_Name => "Generic_Renamings");
731 -- Variable to hold enclosing instantiation. When the environment is
732 -- saved for a subprogram inlining, the corresponding Act_Id is empty.
734 Current_Instantiated_Parent : Assoc := (Empty, Empty, Assoc_Null);
736 -- Hash table for associations
738 HTable_Size : constant := 37;
739 type HTable_Range is range 0 .. HTable_Size - 1;
741 procedure Set_Next_Assoc (E : Assoc_Ptr; Next : Assoc_Ptr);
742 function Next_Assoc (E : Assoc_Ptr) return Assoc_Ptr;
743 function Get_Gen_Id (E : Assoc_Ptr) return Entity_Id;
744 function Hash (F : Entity_Id) return HTable_Range;
746 package Generic_Renamings_HTable is new GNAT.HTable.Static_HTable (
747 Header_Num => HTable_Range,
749 Elmt_Ptr => Assoc_Ptr,
750 Null_Ptr => Assoc_Null,
751 Set_Next => Set_Next_Assoc,
754 Get_Key => Get_Gen_Id,
758 Exchanged_Views : Elist_Id;
759 -- This list holds the private views that have been exchanged during
760 -- instantiation to restore the visibility of the generic declaration.
761 -- (see comments above). After instantiation, the current visibility is
762 -- reestablished by means of a traversal of this list.
764 Hidden_Entities : Elist_Id;
765 -- This list holds the entities of the current scope that are removed
766 -- from immediate visibility when instantiating a child unit. Their
767 -- visibility is restored in Remove_Parent.
769 -- Because instantiations can be recursive, the following must be saved
770 -- on entry and restored on exit from an instantiation (spec or body).
771 -- This is done by the two procedures Save_Env and Restore_Env. For
772 -- package and subprogram instantiations (but not for the body instances)
773 -- the action of Save_Env is done in two steps: Init_Env is called before
774 -- Check_Generic_Child_Unit, because setting the parent instances requires
775 -- that the visibility data structures be properly initialized. Once the
776 -- generic is unit is validated, Set_Instance_Env completes Save_Env.
778 Parent_Unit_Visible : Boolean := False;
779 -- Parent_Unit_Visible is used when the generic is a child unit, and
780 -- indicates whether the ultimate parent of the generic is visible in the
781 -- instantiation environment. It is used to reset the visibility of the
782 -- parent at the end of the instantiation (see Remove_Parent).
784 Instance_Parent_Unit : Entity_Id := Empty;
785 -- This records the ultimate parent unit of an instance of a generic
786 -- child unit and is used in conjunction with Parent_Unit_Visible to
787 -- indicate the unit to which the Parent_Unit_Visible flag corresponds.
789 type Instance_Env is record
790 Instantiated_Parent : Assoc;
791 Exchanged_Views : Elist_Id;
792 Hidden_Entities : Elist_Id;
793 Current_Sem_Unit : Unit_Number_Type;
794 Parent_Unit_Visible : Boolean := False;
795 Instance_Parent_Unit : Entity_Id := Empty;
796 Switches : Config_Switches_Type;
799 package Instance_Envs is new Table.Table (
800 Table_Component_Type => Instance_Env,
801 Table_Index_Type => Int,
802 Table_Low_Bound => 0,
804 Table_Increment => 100,
805 Table_Name => "Instance_Envs");
807 procedure Restore_Private_Views
808 (Pack_Id : Entity_Id;
809 Is_Package : Boolean := True);
810 -- Restore the private views of external types, and unmark the generic
811 -- renamings of actuals, so that they become compatible subtypes again.
812 -- For subprograms, Pack_Id is the package constructed to hold the
815 procedure Switch_View (T : Entity_Id);
816 -- Switch the partial and full views of a type and its private
817 -- dependents (i.e. its subtypes and derived types).
819 ------------------------------------
820 -- Structures for Error Reporting --
821 ------------------------------------
823 Instantiation_Node : Node_Id;
824 -- Used by subprograms that validate instantiation of formal parameters
825 -- where there might be no actual on which to place the error message.
826 -- Also used to locate the instantiation node for generic subunits.
828 Instantiation_Error : exception;
829 -- When there is a semantic error in the generic parameter matching,
830 -- there is no point in continuing the instantiation, because the
831 -- number of cascaded errors is unpredictable. This exception aborts
832 -- the instantiation process altogether.
834 S_Adjustment : Sloc_Adjustment;
835 -- Offset created for each node in an instantiation, in order to keep
836 -- track of the source position of the instantiation in each of its nodes.
837 -- A subsequent semantic error or warning on a construct of the instance
838 -- points to both places: the original generic node, and the point of
839 -- instantiation. See Sinput and Sinput.L for additional details.
841 ------------------------------------------------------------
842 -- Data structure for keeping track when inside a Generic --
843 ------------------------------------------------------------
845 -- The following table is used to save values of the Inside_A_Generic
846 -- flag (see spec of Sem) when they are saved by Start_Generic.
848 package Generic_Flags is new Table.Table (
849 Table_Component_Type => Boolean,
850 Table_Index_Type => Int,
851 Table_Low_Bound => 0,
853 Table_Increment => 200,
854 Table_Name => "Generic_Flags");
856 ---------------------------
857 -- Abandon_Instantiation --
858 ---------------------------
860 procedure Abandon_Instantiation (N : Node_Id) is
862 Error_Msg_N ("\instantiation abandoned!", N);
863 raise Instantiation_Error;
864 end Abandon_Instantiation;
866 --------------------------
867 -- Analyze_Associations --
868 --------------------------
870 function Analyze_Associations
873 F_Copy : List_Id) return List_Id
875 Actual_Types : constant Elist_Id := New_Elmt_List;
876 Assoc : constant List_Id := New_List;
877 Default_Actuals : constant Elist_Id := New_Elmt_List;
878 Gen_Unit : constant Entity_Id := Defining_Entity (Parent (F_Copy));
882 Next_Formal : Node_Id;
883 Temp_Formal : Node_Id;
884 Analyzed_Formal : Node_Id;
887 First_Named : Node_Id := Empty;
889 Default_Formals : constant List_Id := New_List;
890 -- If an Other_Choice is present, some of the formals may be defaulted.
891 -- To simplify the treatment of visibility in an instance, we introduce
892 -- individual defaults for each such formal. These defaults are
893 -- appended to the list of associations and replace the Others_Choice.
895 Found_Assoc : Node_Id;
896 -- Association for the current formal being match. Empty if there are
897 -- no remaining actuals, or if there is no named association with the
898 -- name of the formal.
900 Is_Named_Assoc : Boolean;
901 Num_Matched : Int := 0;
902 Num_Actuals : Int := 0;
904 Others_Present : Boolean := False;
905 -- In Ada 2005, indicates partial parametrization of a formal
906 -- package. As usual an other association must be last in the list.
908 function Matching_Actual
910 A_F : Entity_Id) return Node_Id;
911 -- Find actual that corresponds to a given a formal parameter. If the
912 -- actuals are positional, return the next one, if any. If the actuals
913 -- are named, scan the parameter associations to find the right one.
914 -- A_F is the corresponding entity in the analyzed generic,which is
915 -- placed on the selector name for ASIS use.
917 -- In Ada 2005, a named association may be given with a box, in which
918 -- case Matching_Actual sets Found_Assoc to the generic association,
919 -- but return Empty for the actual itself. In this case the code below
920 -- creates a corresponding declaration for the formal.
922 function Partial_Parametrization return Boolean;
923 -- Ada 2005: if no match is found for a given formal, check if the
924 -- association for it includes a box, or whether the associations
925 -- include an Others clause.
927 procedure Process_Default (F : Entity_Id);
928 -- Add a copy of the declaration of generic formal F to the list of
929 -- associations, and add an explicit box association for F if there
930 -- is none yet, and the default comes from an Others_Choice.
932 procedure Set_Analyzed_Formal;
933 -- Find the node in the generic copy that corresponds to a given formal.
934 -- The semantic information on this node is used to perform legality
935 -- checks on the actuals. Because semantic analysis can introduce some
936 -- anonymous entities or modify the declaration node itself, the
937 -- correspondence between the two lists is not one-one. In addition to
938 -- anonymous types, the presence a formal equality will introduce an
939 -- implicit declaration for the corresponding inequality.
941 ---------------------
942 -- Matching_Actual --
943 ---------------------
945 function Matching_Actual
947 A_F : Entity_Id) return Node_Id
953 Is_Named_Assoc := False;
955 -- End of list of purely positional parameters
958 or else Nkind (Actual) = N_Others_Choice
960 Found_Assoc := Empty;
963 -- Case of positional parameter corresponding to current formal
965 elsif No (Selector_Name (Actual)) then
966 Found_Assoc := Actual;
967 Act := Explicit_Generic_Actual_Parameter (Actual);
968 Num_Matched := Num_Matched + 1;
971 -- Otherwise scan list of named actuals to find the one with the
972 -- desired name. All remaining actuals have explicit names.
975 Is_Named_Assoc := True;
976 Found_Assoc := Empty;
980 while Present (Actual) loop
981 if Chars (Selector_Name (Actual)) = Chars (F) then
982 Set_Entity (Selector_Name (Actual), A_F);
983 Set_Etype (Selector_Name (Actual), Etype (A_F));
984 Generate_Reference (A_F, Selector_Name (Actual));
985 Found_Assoc := Actual;
986 Act := Explicit_Generic_Actual_Parameter (Actual);
987 Num_Matched := Num_Matched + 1;
995 -- Reset for subsequent searches. In most cases the named
996 -- associations are in order. If they are not, we reorder them
997 -- to avoid scanning twice the same actual. This is not just a
998 -- question of efficiency: there may be multiple defaults with
999 -- boxes that have the same name. In a nested instantiation we
1000 -- insert actuals for those defaults, and cannot rely on their
1001 -- names to disambiguate them.
1003 if Actual = First_Named then
1006 elsif Present (Actual) then
1007 Insert_Before (First_Named, Remove_Next (Prev));
1010 Actual := First_Named;
1013 if Is_Entity_Name (Act) and then Present (Entity (Act)) then
1014 Set_Used_As_Generic_Actual (Entity (Act));
1018 end Matching_Actual;
1020 -----------------------------
1021 -- Partial_Parametrization --
1022 -----------------------------
1024 function Partial_Parametrization return Boolean is
1026 return Others_Present
1027 or else (Present (Found_Assoc) and then Box_Present (Found_Assoc));
1028 end Partial_Parametrization;
1030 ---------------------
1031 -- Process_Default --
1032 ---------------------
1034 procedure Process_Default (F : Entity_Id) is
1035 Loc : constant Source_Ptr := Sloc (I_Node);
1036 F_Id : constant Entity_Id := Defining_Entity (F);
1043 -- Append copy of formal declaration to associations, and create
1044 -- new defining identifier for it.
1046 Decl := New_Copy_Tree (F);
1047 Id := Make_Defining_Identifier (Sloc (F_Id), Chars => Chars (F_Id));
1049 if Nkind (F) in N_Formal_Subprogram_Declaration then
1050 Set_Defining_Unit_Name (Specification (Decl), Id);
1053 Set_Defining_Identifier (Decl, Id);
1056 Append (Decl, Assoc);
1058 if No (Found_Assoc) then
1060 Make_Generic_Association (Loc,
1061 Selector_Name => New_Occurrence_Of (Id, Loc),
1062 Explicit_Generic_Actual_Parameter => Empty);
1063 Set_Box_Present (Default);
1064 Append (Default, Default_Formals);
1066 end Process_Default;
1068 -------------------------
1069 -- Set_Analyzed_Formal --
1070 -------------------------
1072 procedure Set_Analyzed_Formal is
1076 while Present (Analyzed_Formal) loop
1077 Kind := Nkind (Analyzed_Formal);
1079 case Nkind (Formal) is
1081 when N_Formal_Subprogram_Declaration =>
1082 exit when Kind in N_Formal_Subprogram_Declaration
1085 (Defining_Unit_Name (Specification (Formal))) =
1087 (Defining_Unit_Name (Specification (Analyzed_Formal)));
1089 when N_Formal_Package_Declaration =>
1090 exit when Nkind_In (Kind, N_Formal_Package_Declaration,
1091 N_Generic_Package_Declaration,
1092 N_Package_Declaration);
1094 when N_Use_Package_Clause | N_Use_Type_Clause => exit;
1098 -- Skip freeze nodes, and nodes inserted to replace
1099 -- unrecognized pragmas.
1102 Kind not in N_Formal_Subprogram_Declaration
1103 and then not Nkind_In (Kind, N_Subprogram_Declaration,
1107 and then Chars (Defining_Identifier (Formal)) =
1108 Chars (Defining_Identifier (Analyzed_Formal));
1111 Next (Analyzed_Formal);
1113 end Set_Analyzed_Formal;
1115 -- Start of processing for Analyze_Associations
1118 Actuals := Generic_Associations (I_Node);
1120 if Present (Actuals) then
1122 -- check for an Others choice, indicating a partial parametrization
1123 -- for a formal package.
1125 Actual := First (Actuals);
1126 while Present (Actual) loop
1127 if Nkind (Actual) = N_Others_Choice then
1128 Others_Present := True;
1130 if Present (Next (Actual)) then
1131 Error_Msg_N ("others must be last association", Actual);
1134 -- This subprogram is used both for formal packages and for
1135 -- instantiations. For the latter, associations must all be
1138 if Nkind (I_Node) /= N_Formal_Package_Declaration
1139 and then Comes_From_Source (I_Node)
1142 ("others association not allowed in an instance",
1146 -- In any case, nothing to do after the others association
1150 elsif Box_Present (Actual)
1151 and then Comes_From_Source (I_Node)
1152 and then Nkind (I_Node) /= N_Formal_Package_Declaration
1155 ("box association not allowed in an instance", Actual);
1161 -- If named associations are present, save first named association
1162 -- (it may of course be Empty) to facilitate subsequent name search.
1164 First_Named := First (Actuals);
1165 while Present (First_Named)
1166 and then Nkind (First_Named) /= N_Others_Choice
1167 and then No (Selector_Name (First_Named))
1169 Num_Actuals := Num_Actuals + 1;
1174 Named := First_Named;
1175 while Present (Named) loop
1176 if Nkind (Named) /= N_Others_Choice
1177 and then No (Selector_Name (Named))
1179 Error_Msg_N ("invalid positional actual after named one", Named);
1180 Abandon_Instantiation (Named);
1183 -- A named association may lack an actual parameter, if it was
1184 -- introduced for a default subprogram that turns out to be local
1185 -- to the outer instantiation.
1187 if Nkind (Named) /= N_Others_Choice
1188 and then Present (Explicit_Generic_Actual_Parameter (Named))
1190 Num_Actuals := Num_Actuals + 1;
1196 if Present (Formals) then
1197 Formal := First_Non_Pragma (Formals);
1198 Analyzed_Formal := First_Non_Pragma (F_Copy);
1200 if Present (Actuals) then
1201 Actual := First (Actuals);
1203 -- All formals should have default values
1209 while Present (Formal) loop
1210 Set_Analyzed_Formal;
1211 Next_Formal := Next_Non_Pragma (Formal);
1213 case Nkind (Formal) is
1214 when N_Formal_Object_Declaration =>
1217 Defining_Identifier (Formal),
1218 Defining_Identifier (Analyzed_Formal));
1220 if No (Match) and then Partial_Parametrization then
1221 Process_Default (Formal);
1224 (Instantiate_Object (Formal, Match, Analyzed_Formal),
1228 when N_Formal_Type_Declaration =>
1231 Defining_Identifier (Formal),
1232 Defining_Identifier (Analyzed_Formal));
1235 if Partial_Parametrization then
1236 Process_Default (Formal);
1239 Error_Msg_Sloc := Sloc (Gen_Unit);
1243 Defining_Identifier (Formal));
1244 Error_Msg_NE ("\in instantiation of & declared#",
1245 Instantiation_Node, Gen_Unit);
1246 Abandon_Instantiation (Instantiation_Node);
1253 (Formal, Match, Analyzed_Formal, Assoc),
1256 -- An instantiation is a freeze point for the actuals,
1257 -- unless this is a rewritten formal package.
1259 if Nkind (I_Node) /= N_Formal_Package_Declaration then
1260 Append_Elmt (Entity (Match), Actual_Types);
1264 -- A remote access-to-class-wide type must not be an
1265 -- actual parameter for a generic formal of an access
1266 -- type (E.2.2 (17)).
1268 if Nkind (Analyzed_Formal) = N_Formal_Type_Declaration
1270 Nkind (Formal_Type_Definition (Analyzed_Formal)) =
1271 N_Access_To_Object_Definition
1273 Validate_Remote_Access_To_Class_Wide_Type (Match);
1276 when N_Formal_Subprogram_Declaration =>
1279 Defining_Unit_Name (Specification (Formal)),
1280 Defining_Unit_Name (Specification (Analyzed_Formal)));
1282 -- If the formal subprogram has the same name as
1283 -- another formal subprogram of the generic, then
1284 -- a named association is illegal (12.3(9)). Exclude
1285 -- named associations that are generated for a nested
1289 and then Is_Named_Assoc
1290 and then Comes_From_Source (Found_Assoc)
1292 Temp_Formal := First (Formals);
1293 while Present (Temp_Formal) loop
1294 if Nkind (Temp_Formal) in
1295 N_Formal_Subprogram_Declaration
1296 and then Temp_Formal /= Formal
1298 Chars (Selector_Name (Found_Assoc)) =
1299 Chars (Defining_Unit_Name
1300 (Specification (Temp_Formal)))
1303 ("name not allowed for overloaded formal",
1305 Abandon_Instantiation (Instantiation_Node);
1312 -- If there is no corresponding actual, this may be case of
1313 -- partial parametrization, or else the formal has a default
1317 and then Partial_Parametrization
1319 Process_Default (Formal);
1322 Instantiate_Formal_Subprogram
1323 (Formal, Match, Analyzed_Formal));
1326 -- If this is a nested generic, preserve default for later
1330 and then Box_Present (Formal)
1333 (Defining_Unit_Name (Specification (Last (Assoc))),
1337 when N_Formal_Package_Declaration =>
1340 Defining_Identifier (Formal),
1341 Defining_Identifier (Original_Node (Analyzed_Formal)));
1344 if Partial_Parametrization then
1345 Process_Default (Formal);
1348 Error_Msg_Sloc := Sloc (Gen_Unit);
1351 Instantiation_Node, Defining_Identifier (Formal));
1352 Error_Msg_NE ("\in instantiation of & declared#",
1353 Instantiation_Node, Gen_Unit);
1355 Abandon_Instantiation (Instantiation_Node);
1361 (Instantiate_Formal_Package
1362 (Formal, Match, Analyzed_Formal),
1366 -- For use type and use package appearing in the generic part,
1367 -- we have already copied them, so we can just move them where
1368 -- they belong (we mustn't recopy them since this would mess up
1369 -- the Sloc values).
1371 when N_Use_Package_Clause |
1372 N_Use_Type_Clause =>
1373 if Nkind (Original_Node (I_Node)) =
1374 N_Formal_Package_Declaration
1376 Append (New_Copy_Tree (Formal), Assoc);
1379 Append (Formal, Assoc);
1383 raise Program_Error;
1387 Formal := Next_Formal;
1388 Next_Non_Pragma (Analyzed_Formal);
1391 if Num_Actuals > Num_Matched then
1392 Error_Msg_Sloc := Sloc (Gen_Unit);
1394 if Present (Selector_Name (Actual)) then
1396 ("unmatched actual&",
1397 Actual, Selector_Name (Actual));
1398 Error_Msg_NE ("\in instantiation of& declared#",
1402 ("unmatched actual in instantiation of& declared#",
1407 elsif Present (Actuals) then
1409 ("too many actuals in generic instantiation", Instantiation_Node);
1413 Elmt : Elmt_Id := First_Elmt (Actual_Types);
1416 while Present (Elmt) loop
1417 Freeze_Before (I_Node, Node (Elmt));
1422 -- If there are default subprograms, normalize the tree by adding
1423 -- explicit associations for them. This is required if the instance
1424 -- appears within a generic.
1432 Elmt := First_Elmt (Default_Actuals);
1433 while Present (Elmt) loop
1434 if No (Actuals) then
1435 Actuals := New_List;
1436 Set_Generic_Associations (I_Node, Actuals);
1439 Subp := Node (Elmt);
1441 Make_Generic_Association (Sloc (Subp),
1442 Selector_Name => New_Occurrence_Of (Subp, Sloc (Subp)),
1443 Explicit_Generic_Actual_Parameter =>
1444 New_Occurrence_Of (Subp, Sloc (Subp)));
1445 Mark_Rewrite_Insertion (New_D);
1446 Append_To (Actuals, New_D);
1451 -- If this is a formal package, normalize the parameter list by adding
1452 -- explicit box associations for the formals that are covered by an
1455 if not Is_Empty_List (Default_Formals) then
1456 Append_List (Default_Formals, Formals);
1460 end Analyze_Associations;
1462 -------------------------------
1463 -- Analyze_Formal_Array_Type --
1464 -------------------------------
1466 procedure Analyze_Formal_Array_Type
1467 (T : in out Entity_Id;
1473 -- Treated like a non-generic array declaration, with additional
1478 if Nkind (Def) = N_Constrained_Array_Definition then
1479 DSS := First (Discrete_Subtype_Definitions (Def));
1480 while Present (DSS) loop
1481 if Nkind_In (DSS, N_Subtype_Indication,
1483 N_Attribute_Reference)
1485 Error_Msg_N ("only a subtype mark is allowed in a formal", DSS);
1492 Array_Type_Declaration (T, Def);
1493 Set_Is_Generic_Type (Base_Type (T));
1495 if Ekind (Component_Type (T)) = E_Incomplete_Type
1496 and then No (Full_View (Component_Type (T)))
1498 Error_Msg_N ("premature usage of incomplete type", Def);
1500 -- Check that range constraint is not allowed on the component type
1501 -- of a generic formal array type (AARM 12.5.3(3))
1503 elsif Is_Internal (Component_Type (T))
1504 and then Present (Subtype_Indication (Component_Definition (Def)))
1505 and then Nkind (Original_Node
1506 (Subtype_Indication (Component_Definition (Def)))) =
1507 N_Subtype_Indication
1510 ("in a formal, a subtype indication can only be "
1511 & "a subtype mark (RM 12.5.3(3))",
1512 Subtype_Indication (Component_Definition (Def)));
1515 end Analyze_Formal_Array_Type;
1517 ---------------------------------------------
1518 -- Analyze_Formal_Decimal_Fixed_Point_Type --
1519 ---------------------------------------------
1521 -- As for other generic types, we create a valid type representation with
1522 -- legal but arbitrary attributes, whose values are never considered
1523 -- static. For all scalar types we introduce an anonymous base type, with
1524 -- the same attributes. We choose the corresponding integer type to be
1525 -- Standard_Integer.
1527 procedure Analyze_Formal_Decimal_Fixed_Point_Type
1531 Loc : constant Source_Ptr := Sloc (Def);
1532 Base : constant Entity_Id :=
1534 (E_Decimal_Fixed_Point_Type,
1535 Current_Scope, Sloc (Def), 'G');
1536 Int_Base : constant Entity_Id := Standard_Integer;
1537 Delta_Val : constant Ureal := Ureal_1;
1538 Digs_Val : constant Uint := Uint_6;
1543 Set_Etype (Base, Base);
1544 Set_Size_Info (Base, Int_Base);
1545 Set_RM_Size (Base, RM_Size (Int_Base));
1546 Set_First_Rep_Item (Base, First_Rep_Item (Int_Base));
1547 Set_Digits_Value (Base, Digs_Val);
1548 Set_Delta_Value (Base, Delta_Val);
1549 Set_Small_Value (Base, Delta_Val);
1550 Set_Scalar_Range (Base,
1552 Low_Bound => Make_Real_Literal (Loc, Ureal_1),
1553 High_Bound => Make_Real_Literal (Loc, Ureal_1)));
1555 Set_Is_Generic_Type (Base);
1556 Set_Parent (Base, Parent (Def));
1558 Set_Ekind (T, E_Decimal_Fixed_Point_Subtype);
1559 Set_Etype (T, Base);
1560 Set_Size_Info (T, Int_Base);
1561 Set_RM_Size (T, RM_Size (Int_Base));
1562 Set_First_Rep_Item (T, First_Rep_Item (Int_Base));
1563 Set_Digits_Value (T, Digs_Val);
1564 Set_Delta_Value (T, Delta_Val);
1565 Set_Small_Value (T, Delta_Val);
1566 Set_Scalar_Range (T, Scalar_Range (Base));
1567 Set_Is_Constrained (T);
1569 Check_Restriction (No_Fixed_Point, Def);
1570 end Analyze_Formal_Decimal_Fixed_Point_Type;
1572 -------------------------------------------
1573 -- Analyze_Formal_Derived_Interface_Type --
1574 -------------------------------------------
1576 procedure Analyze_Formal_Derived_Interface_Type
1581 Loc : constant Source_Ptr := Sloc (Def);
1584 -- Rewrite as a type declaration of a derived type. This ensures that
1585 -- the interface list and primitive operations are properly captured.
1588 Make_Full_Type_Declaration (Loc,
1589 Defining_Identifier => T,
1590 Type_Definition => Def));
1592 Set_Is_Generic_Type (T);
1593 end Analyze_Formal_Derived_Interface_Type;
1595 ---------------------------------
1596 -- Analyze_Formal_Derived_Type --
1597 ---------------------------------
1599 procedure Analyze_Formal_Derived_Type
1604 Loc : constant Source_Ptr := Sloc (Def);
1605 Unk_Disc : constant Boolean := Unknown_Discriminants_Present (N);
1609 Set_Is_Generic_Type (T);
1611 if Private_Present (Def) then
1613 Make_Private_Extension_Declaration (Loc,
1614 Defining_Identifier => T,
1615 Discriminant_Specifications => Discriminant_Specifications (N),
1616 Unknown_Discriminants_Present => Unk_Disc,
1617 Subtype_Indication => Subtype_Mark (Def),
1618 Interface_List => Interface_List (Def));
1620 Set_Abstract_Present (New_N, Abstract_Present (Def));
1621 Set_Limited_Present (New_N, Limited_Present (Def));
1622 Set_Synchronized_Present (New_N, Synchronized_Present (Def));
1626 Make_Full_Type_Declaration (Loc,
1627 Defining_Identifier => T,
1628 Discriminant_Specifications =>
1629 Discriminant_Specifications (Parent (T)),
1631 Make_Derived_Type_Definition (Loc,
1632 Subtype_Indication => Subtype_Mark (Def)));
1634 Set_Abstract_Present
1635 (Type_Definition (New_N), Abstract_Present (Def));
1637 (Type_Definition (New_N), Limited_Present (Def));
1644 if not Is_Composite_Type (T) then
1646 ("unknown discriminants not allowed for elementary types", N);
1648 Set_Has_Unknown_Discriminants (T);
1649 Set_Is_Constrained (T, False);
1653 -- If the parent type has a known size, so does the formal, which makes
1654 -- legal representation clauses that involve the formal.
1656 Set_Size_Known_At_Compile_Time
1657 (T, Size_Known_At_Compile_Time (Entity (Subtype_Mark (Def))));
1658 end Analyze_Formal_Derived_Type;
1660 ----------------------------------
1661 -- Analyze_Formal_Discrete_Type --
1662 ----------------------------------
1664 -- The operations defined for a discrete types are those of an enumeration
1665 -- type. The size is set to an arbitrary value, for use in analyzing the
1668 procedure Analyze_Formal_Discrete_Type (T : Entity_Id; Def : Node_Id) is
1669 Loc : constant Source_Ptr := Sloc (Def);
1673 Base : constant Entity_Id :=
1675 (E_Floating_Point_Type, Current_Scope, Sloc (Def), 'G');
1678 Set_Ekind (T, E_Enumeration_Subtype);
1679 Set_Etype (T, Base);
1682 Set_Is_Generic_Type (T);
1683 Set_Is_Constrained (T);
1685 -- For semantic analysis, the bounds of the type must be set to some
1686 -- non-static value. The simplest is to create attribute nodes for those
1687 -- bounds, that refer to the type itself. These bounds are never
1688 -- analyzed but serve as place-holders.
1691 Make_Attribute_Reference (Loc,
1692 Attribute_Name => Name_First,
1693 Prefix => New_Reference_To (T, Loc));
1697 Make_Attribute_Reference (Loc,
1698 Attribute_Name => Name_Last,
1699 Prefix => New_Reference_To (T, Loc));
1702 Set_Scalar_Range (T,
1707 Set_Ekind (Base, E_Enumeration_Type);
1708 Set_Etype (Base, Base);
1709 Init_Size (Base, 8);
1710 Init_Alignment (Base);
1711 Set_Is_Generic_Type (Base);
1712 Set_Scalar_Range (Base, Scalar_Range (T));
1713 Set_Parent (Base, Parent (Def));
1714 end Analyze_Formal_Discrete_Type;
1716 ----------------------------------
1717 -- Analyze_Formal_Floating_Type --
1718 ---------------------------------
1720 procedure Analyze_Formal_Floating_Type (T : Entity_Id; Def : Node_Id) is
1721 Base : constant Entity_Id :=
1723 (E_Floating_Point_Type, Current_Scope, Sloc (Def), 'G');
1726 -- The various semantic attributes are taken from the predefined type
1727 -- Float, just so that all of them are initialized. Their values are
1728 -- never used because no constant folding or expansion takes place in
1729 -- the generic itself.
1732 Set_Ekind (T, E_Floating_Point_Subtype);
1733 Set_Etype (T, Base);
1734 Set_Size_Info (T, (Standard_Float));
1735 Set_RM_Size (T, RM_Size (Standard_Float));
1736 Set_Digits_Value (T, Digits_Value (Standard_Float));
1737 Set_Scalar_Range (T, Scalar_Range (Standard_Float));
1738 Set_Is_Constrained (T);
1740 Set_Is_Generic_Type (Base);
1741 Set_Etype (Base, Base);
1742 Set_Size_Info (Base, (Standard_Float));
1743 Set_RM_Size (Base, RM_Size (Standard_Float));
1744 Set_Digits_Value (Base, Digits_Value (Standard_Float));
1745 Set_Scalar_Range (Base, Scalar_Range (Standard_Float));
1746 Set_Parent (Base, Parent (Def));
1748 Check_Restriction (No_Floating_Point, Def);
1749 end Analyze_Formal_Floating_Type;
1751 -----------------------------------
1752 -- Analyze_Formal_Interface_Type;--
1753 -----------------------------------
1755 procedure Analyze_Formal_Interface_Type
1760 Loc : constant Source_Ptr := Sloc (N);
1765 Make_Full_Type_Declaration (Loc,
1766 Defining_Identifier => T,
1767 Type_Definition => Def);
1771 Set_Is_Generic_Type (T);
1772 end Analyze_Formal_Interface_Type;
1774 ---------------------------------
1775 -- Analyze_Formal_Modular_Type --
1776 ---------------------------------
1778 procedure Analyze_Formal_Modular_Type (T : Entity_Id; Def : Node_Id) is
1780 -- Apart from their entity kind, generic modular types are treated like
1781 -- signed integer types, and have the same attributes.
1783 Analyze_Formal_Signed_Integer_Type (T, Def);
1784 Set_Ekind (T, E_Modular_Integer_Subtype);
1785 Set_Ekind (Etype (T), E_Modular_Integer_Type);
1787 end Analyze_Formal_Modular_Type;
1789 ---------------------------------------
1790 -- Analyze_Formal_Object_Declaration --
1791 ---------------------------------------
1793 procedure Analyze_Formal_Object_Declaration (N : Node_Id) is
1794 E : constant Node_Id := Default_Expression (N);
1795 Id : constant Node_Id := Defining_Identifier (N);
1802 -- Determine the mode of the formal object
1804 if Out_Present (N) then
1805 K := E_Generic_In_Out_Parameter;
1807 if not In_Present (N) then
1808 Error_Msg_N ("formal generic objects cannot have mode OUT", N);
1812 K := E_Generic_In_Parameter;
1815 if Present (Subtype_Mark (N)) then
1816 Find_Type (Subtype_Mark (N));
1817 T := Entity (Subtype_Mark (N));
1819 -- Verify that there is no redundant null exclusion
1821 if Null_Exclusion_Present (N) then
1822 if not Is_Access_Type (T) then
1824 ("null exclusion can only apply to an access type", N);
1826 elsif Can_Never_Be_Null (T) then
1828 ("`NOT NULL` not allowed (& already excludes null)",
1833 -- Ada 2005 (AI-423): Formal object with an access definition
1836 Check_Access_Definition (N);
1837 T := Access_Definition
1839 N => Access_Definition (N));
1842 if Ekind (T) = E_Incomplete_Type then
1844 Error_Node : Node_Id;
1847 if Present (Subtype_Mark (N)) then
1848 Error_Node := Subtype_Mark (N);
1850 Check_Access_Definition (N);
1851 Error_Node := Access_Definition (N);
1854 Error_Msg_N ("premature usage of incomplete type", Error_Node);
1858 if K = E_Generic_In_Parameter then
1860 -- Ada 2005 (AI-287): Limited aggregates allowed in generic formals
1862 if Ada_Version < Ada_05 and then Is_Limited_Type (T) then
1864 ("generic formal of mode IN must not be of limited type", N);
1865 Explain_Limited_Type (T, N);
1868 if Is_Abstract_Type (T) then
1870 ("generic formal of mode IN must not be of abstract type", N);
1874 Preanalyze_Spec_Expression (E, T);
1876 if Is_Limited_Type (T) and then not OK_For_Limited_Init (E) then
1878 ("initialization not allowed for limited types", E);
1879 Explain_Limited_Type (T, E);
1886 -- Case of generic IN OUT parameter
1889 -- If the formal has an unconstrained type, construct its actual
1890 -- subtype, as is done for subprogram formals. In this fashion, all
1891 -- its uses can refer to specific bounds.
1896 if (Is_Array_Type (T)
1897 and then not Is_Constrained (T))
1899 (Ekind (T) = E_Record_Type
1900 and then Has_Discriminants (T))
1903 Non_Freezing_Ref : constant Node_Id :=
1904 New_Reference_To (Id, Sloc (Id));
1908 -- Make sure the actual subtype doesn't generate bogus freezing
1910 Set_Must_Not_Freeze (Non_Freezing_Ref);
1911 Decl := Build_Actual_Subtype (T, Non_Freezing_Ref);
1912 Insert_Before_And_Analyze (N, Decl);
1913 Set_Actual_Subtype (Id, Defining_Identifier (Decl));
1916 Set_Actual_Subtype (Id, T);
1921 ("initialization not allowed for `IN OUT` formals", N);
1925 end Analyze_Formal_Object_Declaration;
1927 ----------------------------------------------
1928 -- Analyze_Formal_Ordinary_Fixed_Point_Type --
1929 ----------------------------------------------
1931 procedure Analyze_Formal_Ordinary_Fixed_Point_Type
1935 Loc : constant Source_Ptr := Sloc (Def);
1936 Base : constant Entity_Id :=
1938 (E_Ordinary_Fixed_Point_Type, Current_Scope, Sloc (Def), 'G');
1940 -- The semantic attributes are set for completeness only, their values
1941 -- will never be used, since all properties of the type are non-static.
1944 Set_Ekind (T, E_Ordinary_Fixed_Point_Subtype);
1945 Set_Etype (T, Base);
1946 Set_Size_Info (T, Standard_Integer);
1947 Set_RM_Size (T, RM_Size (Standard_Integer));
1948 Set_Small_Value (T, Ureal_1);
1949 Set_Delta_Value (T, Ureal_1);
1950 Set_Scalar_Range (T,
1952 Low_Bound => Make_Real_Literal (Loc, Ureal_1),
1953 High_Bound => Make_Real_Literal (Loc, Ureal_1)));
1954 Set_Is_Constrained (T);
1956 Set_Is_Generic_Type (Base);
1957 Set_Etype (Base, Base);
1958 Set_Size_Info (Base, Standard_Integer);
1959 Set_RM_Size (Base, RM_Size (Standard_Integer));
1960 Set_Small_Value (Base, Ureal_1);
1961 Set_Delta_Value (Base, Ureal_1);
1962 Set_Scalar_Range (Base, Scalar_Range (T));
1963 Set_Parent (Base, Parent (Def));
1965 Check_Restriction (No_Fixed_Point, Def);
1966 end Analyze_Formal_Ordinary_Fixed_Point_Type;
1968 ----------------------------
1969 -- Analyze_Formal_Package --
1970 ----------------------------
1972 procedure Analyze_Formal_Package (N : Node_Id) is
1973 Loc : constant Source_Ptr := Sloc (N);
1974 Pack_Id : constant Entity_Id := Defining_Identifier (N);
1976 Gen_Id : constant Node_Id := Name (N);
1978 Gen_Unit : Entity_Id;
1980 Parent_Installed : Boolean := False;
1982 Parent_Instance : Entity_Id;
1983 Renaming_In_Par : Entity_Id;
1984 No_Associations : Boolean := False;
1986 function Build_Local_Package return Node_Id;
1987 -- The formal package is rewritten so that its parameters are replaced
1988 -- with corresponding declarations. For parameters with bona fide
1989 -- associations these declarations are created by Analyze_Associations
1990 -- as for a regular instantiation. For boxed parameters, we preserve
1991 -- the formal declarations and analyze them, in order to introduce
1992 -- entities of the right kind in the environment of the formal.
1994 -------------------------
1995 -- Build_Local_Package --
1996 -------------------------
1998 function Build_Local_Package return Node_Id is
2000 Pack_Decl : Node_Id;
2003 -- Within the formal, the name of the generic package is a renaming
2004 -- of the formal (as for a regular instantiation).
2007 Make_Package_Declaration (Loc,
2010 (Specification (Original_Node (Gen_Decl)),
2011 Empty, Instantiating => True));
2013 Renaming := Make_Package_Renaming_Declaration (Loc,
2014 Defining_Unit_Name =>
2015 Make_Defining_Identifier (Loc, Chars (Gen_Unit)),
2016 Name => New_Occurrence_Of (Formal, Loc));
2018 if Nkind (Gen_Id) = N_Identifier
2019 and then Chars (Gen_Id) = Chars (Pack_Id)
2022 ("& is hidden within declaration of instance", Gen_Id, Gen_Unit);
2025 -- If the formal is declared with a box, or with an others choice,
2026 -- create corresponding declarations for all entities in the formal
2027 -- part, so that names with the proper types are available in the
2028 -- specification of the formal package.
2029 -- On the other hand, if there are no associations, then all the
2030 -- formals must have defaults, and this will be checked by the
2031 -- call to Analyze_Associations.
2034 or else Nkind (First (Generic_Associations (N))) = N_Others_Choice
2037 Formal_Decl : Node_Id;
2040 -- TBA : for a formal package, need to recurse ???
2045 (Generic_Formal_Declarations (Original_Node (Gen_Decl)));
2046 while Present (Formal_Decl) loop
2048 (Decls, Copy_Generic_Node (Formal_Decl, Empty, True));
2053 -- If generic associations are present, use Analyze_Associations to
2054 -- create the proper renaming declarations.
2058 Act_Tree : constant Node_Id :=
2060 (Original_Node (Gen_Decl), Empty,
2061 Instantiating => True);
2064 Generic_Renamings.Set_Last (0);
2065 Generic_Renamings_HTable.Reset;
2066 Instantiation_Node := N;
2069 Analyze_Associations
2071 Generic_Formal_Declarations (Act_Tree),
2072 Generic_Formal_Declarations (Gen_Decl));
2076 Append (Renaming, To => Decls);
2078 -- Add generated declarations ahead of local declarations in
2081 if No (Visible_Declarations (Specification (Pack_Decl))) then
2082 Set_Visible_Declarations (Specification (Pack_Decl), Decls);
2085 (First (Visible_Declarations (Specification (Pack_Decl))),
2090 end Build_Local_Package;
2092 -- Start of processing for Analyze_Formal_Package
2095 Text_IO_Kludge (Gen_Id);
2098 Check_Generic_Child_Unit (Gen_Id, Parent_Installed);
2099 Gen_Unit := Entity (Gen_Id);
2101 -- Check for a formal package that is a package renaming
2103 if Present (Renamed_Object (Gen_Unit)) then
2104 Gen_Unit := Renamed_Object (Gen_Unit);
2107 if Ekind (Gen_Unit) /= E_Generic_Package then
2108 Error_Msg_N ("expect generic package name", Gen_Id);
2112 elsif Gen_Unit = Current_Scope then
2114 ("generic package cannot be used as a formal package of itself",
2119 elsif In_Open_Scopes (Gen_Unit) then
2120 if Is_Compilation_Unit (Gen_Unit)
2121 and then Is_Child_Unit (Current_Scope)
2123 -- Special-case the error when the formal is a parent, and
2124 -- continue analysis to minimize cascaded errors.
2127 ("generic parent cannot be used as formal package "
2128 & "of a child unit",
2133 ("generic package cannot be used as a formal package "
2142 or else No (Generic_Associations (N))
2143 or else Nkind (First (Generic_Associations (N))) = N_Others_Choice
2145 No_Associations := True;
2148 -- If there are no generic associations, the generic parameters appear
2149 -- as local entities and are instantiated like them. We copy the generic
2150 -- package declaration as if it were an instantiation, and analyze it
2151 -- like a regular package, except that we treat the formals as
2152 -- additional visible components.
2154 Gen_Decl := Unit_Declaration_Node (Gen_Unit);
2156 if In_Extended_Main_Source_Unit (N) then
2157 Set_Is_Instantiated (Gen_Unit);
2158 Generate_Reference (Gen_Unit, N);
2161 Formal := New_Copy (Pack_Id);
2162 Create_Instantiation_Source (N, Gen_Unit, False, S_Adjustment);
2165 -- Make local generic without formals. The formals will be replaced
2166 -- with internal declarations.
2168 New_N := Build_Local_Package;
2170 -- If there are errors in the parameter list, Analyze_Associations
2171 -- raises Instantiation_Error. Patch the declaration to prevent
2172 -- further exception propagation.
2175 when Instantiation_Error =>
2177 Enter_Name (Formal);
2178 Set_Ekind (Formal, E_Variable);
2179 Set_Etype (Formal, Any_Type);
2181 if Parent_Installed then
2189 Set_Defining_Unit_Name (Specification (New_N), Formal);
2190 Set_Generic_Parent (Specification (N), Gen_Unit);
2191 Set_Instance_Env (Gen_Unit, Formal);
2192 Set_Is_Generic_Instance (Formal);
2194 Enter_Name (Formal);
2195 Set_Ekind (Formal, E_Package);
2196 Set_Etype (Formal, Standard_Void_Type);
2197 Set_Inner_Instances (Formal, New_Elmt_List);
2198 Push_Scope (Formal);
2200 if Is_Child_Unit (Gen_Unit)
2201 and then Parent_Installed
2203 -- Similarly, we have to make the name of the formal visible in the
2204 -- parent instance, to resolve properly fully qualified names that
2205 -- may appear in the generic unit. The parent instance has been
2206 -- placed on the scope stack ahead of the current scope.
2208 Parent_Instance := Scope_Stack.Table (Scope_Stack.Last - 1).Entity;
2211 Make_Defining_Identifier (Loc, Chars (Gen_Unit));
2212 Set_Ekind (Renaming_In_Par, E_Package);
2213 Set_Etype (Renaming_In_Par, Standard_Void_Type);
2214 Set_Scope (Renaming_In_Par, Parent_Instance);
2215 Set_Parent (Renaming_In_Par, Parent (Formal));
2216 Set_Renamed_Object (Renaming_In_Par, Formal);
2217 Append_Entity (Renaming_In_Par, Parent_Instance);
2220 Analyze (Specification (N));
2222 -- The formals for which associations are provided are not visible
2223 -- outside of the formal package. The others are still declared by a
2224 -- formal parameter declaration.
2226 if not No_Associations then
2231 E := First_Entity (Formal);
2232 while Present (E) loop
2233 exit when Ekind (E) = E_Package
2234 and then Renamed_Entity (E) = Formal;
2236 if not Is_Generic_Formal (E) then
2245 End_Package_Scope (Formal);
2247 if Parent_Installed then
2253 -- Inside the generic unit, the formal package is a regular package, but
2254 -- no body is needed for it. Note that after instantiation, the defining
2255 -- unit name we need is in the new tree and not in the original (see
2256 -- Package_Instantiation). A generic formal package is an instance, and
2257 -- can be used as an actual for an inner instance.
2259 Set_Has_Completion (Formal, True);
2261 -- Add semantic information to the original defining identifier.
2264 Set_Ekind (Pack_Id, E_Package);
2265 Set_Etype (Pack_Id, Standard_Void_Type);
2266 Set_Scope (Pack_Id, Scope (Formal));
2267 Set_Has_Completion (Pack_Id, True);
2268 end Analyze_Formal_Package;
2270 ---------------------------------
2271 -- Analyze_Formal_Private_Type --
2272 ---------------------------------
2274 procedure Analyze_Formal_Private_Type
2280 New_Private_Type (N, T, Def);
2282 -- Set the size to an arbitrary but legal value
2284 Set_Size_Info (T, Standard_Integer);
2285 Set_RM_Size (T, RM_Size (Standard_Integer));
2286 end Analyze_Formal_Private_Type;
2288 ----------------------------------------
2289 -- Analyze_Formal_Signed_Integer_Type --
2290 ----------------------------------------
2292 procedure Analyze_Formal_Signed_Integer_Type
2296 Base : constant Entity_Id :=
2298 (E_Signed_Integer_Type, Current_Scope, Sloc (Def), 'G');
2303 Set_Ekind (T, E_Signed_Integer_Subtype);
2304 Set_Etype (T, Base);
2305 Set_Size_Info (T, Standard_Integer);
2306 Set_RM_Size (T, RM_Size (Standard_Integer));
2307 Set_Scalar_Range (T, Scalar_Range (Standard_Integer));
2308 Set_Is_Constrained (T);
2310 Set_Is_Generic_Type (Base);
2311 Set_Size_Info (Base, Standard_Integer);
2312 Set_RM_Size (Base, RM_Size (Standard_Integer));
2313 Set_Etype (Base, Base);
2314 Set_Scalar_Range (Base, Scalar_Range (Standard_Integer));
2315 Set_Parent (Base, Parent (Def));
2316 end Analyze_Formal_Signed_Integer_Type;
2318 -------------------------------
2319 -- Analyze_Formal_Subprogram --
2320 -------------------------------
2322 procedure Analyze_Formal_Subprogram (N : Node_Id) is
2323 Spec : constant Node_Id := Specification (N);
2324 Def : constant Node_Id := Default_Name (N);
2325 Nam : constant Entity_Id := Defining_Unit_Name (Spec);
2333 if Nkind (Nam) = N_Defining_Program_Unit_Name then
2334 Error_Msg_N ("name of formal subprogram must be a direct name", Nam);
2338 Analyze_Subprogram_Declaration (N);
2339 Set_Is_Formal_Subprogram (Nam);
2340 Set_Has_Completion (Nam);
2342 if Nkind (N) = N_Formal_Abstract_Subprogram_Declaration then
2343 Set_Is_Abstract_Subprogram (Nam);
2344 Set_Is_Dispatching_Operation (Nam);
2347 Ctrl_Type : constant Entity_Id := Find_Dispatching_Type (Nam);
2349 if No (Ctrl_Type) then
2351 ("abstract formal subprogram must have a controlling type",
2354 Check_Controlling_Formals (Ctrl_Type, Nam);
2359 -- Default name is resolved at the point of instantiation
2361 if Box_Present (N) then
2364 -- Else default is bound at the point of generic declaration
2366 elsif Present (Def) then
2367 if Nkind (Def) = N_Operator_Symbol then
2368 Find_Direct_Name (Def);
2370 elsif Nkind (Def) /= N_Attribute_Reference then
2374 -- For an attribute reference, analyze the prefix and verify
2375 -- that it has the proper profile for the subprogram.
2377 Analyze (Prefix (Def));
2378 Valid_Default_Attribute (Nam, Def);
2382 -- Default name may be overloaded, in which case the interpretation
2383 -- with the correct profile must be selected, as for a renaming.
2384 -- If the definition is an indexed component, it must denote a
2385 -- member of an entry family. If it is a selected component, it
2386 -- can be a protected operation.
2388 if Etype (Def) = Any_Type then
2391 elsif Nkind (Def) = N_Selected_Component then
2392 if not Is_Overloadable (Entity (Selector_Name (Def))) then
2393 Error_Msg_N ("expect valid subprogram name as default", Def);
2396 elsif Nkind (Def) = N_Indexed_Component then
2397 if Is_Entity_Name (Prefix (Def)) then
2398 if Ekind (Entity (Prefix (Def))) /= E_Entry_Family then
2399 Error_Msg_N ("expect valid subprogram name as default", Def);
2402 elsif Nkind (Prefix (Def)) = N_Selected_Component then
2403 if Ekind (Entity (Selector_Name (Prefix (Def))))
2406 Error_Msg_N ("expect valid subprogram name as default", Def);
2410 Error_Msg_N ("expect valid subprogram name as default", Def);
2414 elsif Nkind (Def) = N_Character_Literal then
2416 -- Needs some type checks: subprogram should be parameterless???
2418 Resolve (Def, (Etype (Nam)));
2420 elsif not Is_Entity_Name (Def)
2421 or else not Is_Overloadable (Entity (Def))
2423 Error_Msg_N ("expect valid subprogram name as default", Def);
2426 elsif not Is_Overloaded (Def) then
2427 Subp := Entity (Def);
2430 Error_Msg_N ("premature usage of formal subprogram", Def);
2432 elsif not Entity_Matches_Spec (Subp, Nam) then
2433 Error_Msg_N ("no visible entity matches specification", Def);
2436 -- More than one interpretation, so disambiguate as for a renaming
2441 I1 : Interp_Index := 0;
2447 Get_First_Interp (Def, I, It);
2448 while Present (It.Nam) loop
2449 if Entity_Matches_Spec (It.Nam, Nam) then
2450 if Subp /= Any_Id then
2451 It1 := Disambiguate (Def, I1, I, Etype (Subp));
2453 if It1 = No_Interp then
2454 Error_Msg_N ("ambiguous default subprogram", Def);
2467 Get_Next_Interp (I, It);
2471 if Subp /= Any_Id then
2472 Set_Entity (Def, Subp);
2475 Error_Msg_N ("premature usage of formal subprogram", Def);
2477 elsif Ekind (Subp) /= E_Operator then
2478 Check_Mode_Conformant (Subp, Nam);
2482 Error_Msg_N ("no visible subprogram matches specification", N);
2486 end Analyze_Formal_Subprogram;
2488 -------------------------------------
2489 -- Analyze_Formal_Type_Declaration --
2490 -------------------------------------
2492 procedure Analyze_Formal_Type_Declaration (N : Node_Id) is
2493 Def : constant Node_Id := Formal_Type_Definition (N);
2497 T := Defining_Identifier (N);
2499 if Present (Discriminant_Specifications (N))
2500 and then Nkind (Def) /= N_Formal_Private_Type_Definition
2503 ("discriminants not allowed for this formal type", T);
2506 -- Enter the new name, and branch to specific routine
2509 when N_Formal_Private_Type_Definition =>
2510 Analyze_Formal_Private_Type (N, T, Def);
2512 when N_Formal_Derived_Type_Definition =>
2513 Analyze_Formal_Derived_Type (N, T, Def);
2515 when N_Formal_Discrete_Type_Definition =>
2516 Analyze_Formal_Discrete_Type (T, Def);
2518 when N_Formal_Signed_Integer_Type_Definition =>
2519 Analyze_Formal_Signed_Integer_Type (T, Def);
2521 when N_Formal_Modular_Type_Definition =>
2522 Analyze_Formal_Modular_Type (T, Def);
2524 when N_Formal_Floating_Point_Definition =>
2525 Analyze_Formal_Floating_Type (T, Def);
2527 when N_Formal_Ordinary_Fixed_Point_Definition =>
2528 Analyze_Formal_Ordinary_Fixed_Point_Type (T, Def);
2530 when N_Formal_Decimal_Fixed_Point_Definition =>
2531 Analyze_Formal_Decimal_Fixed_Point_Type (T, Def);
2533 when N_Array_Type_Definition =>
2534 Analyze_Formal_Array_Type (T, Def);
2536 when N_Access_To_Object_Definition |
2537 N_Access_Function_Definition |
2538 N_Access_Procedure_Definition =>
2539 Analyze_Generic_Access_Type (T, Def);
2541 -- Ada 2005: a interface declaration is encoded as an abstract
2542 -- record declaration or a abstract type derivation.
2544 when N_Record_Definition =>
2545 Analyze_Formal_Interface_Type (N, T, Def);
2547 when N_Derived_Type_Definition =>
2548 Analyze_Formal_Derived_Interface_Type (N, T, Def);
2554 raise Program_Error;
2558 Set_Is_Generic_Type (T);
2559 end Analyze_Formal_Type_Declaration;
2561 ------------------------------------
2562 -- Analyze_Function_Instantiation --
2563 ------------------------------------
2565 procedure Analyze_Function_Instantiation (N : Node_Id) is
2567 Analyze_Subprogram_Instantiation (N, E_Function);
2568 end Analyze_Function_Instantiation;
2570 ---------------------------------
2571 -- Analyze_Generic_Access_Type --
2572 ---------------------------------
2574 procedure Analyze_Generic_Access_Type (T : Entity_Id; Def : Node_Id) is
2578 if Nkind (Def) = N_Access_To_Object_Definition then
2579 Access_Type_Declaration (T, Def);
2581 if Is_Incomplete_Or_Private_Type (Designated_Type (T))
2582 and then No (Full_View (Designated_Type (T)))
2583 and then not Is_Generic_Type (Designated_Type (T))
2585 Error_Msg_N ("premature usage of incomplete type", Def);
2587 elsif Is_Internal (Designated_Type (T)) then
2589 ("only a subtype mark is allowed in a formal", Def);
2593 Access_Subprogram_Declaration (T, Def);
2595 end Analyze_Generic_Access_Type;
2597 ---------------------------------
2598 -- Analyze_Generic_Formal_Part --
2599 ---------------------------------
2601 procedure Analyze_Generic_Formal_Part (N : Node_Id) is
2602 Gen_Parm_Decl : Node_Id;
2605 -- The generic formals are processed in the scope of the generic unit,
2606 -- where they are immediately visible. The scope is installed by the
2609 Gen_Parm_Decl := First (Generic_Formal_Declarations (N));
2611 while Present (Gen_Parm_Decl) loop
2612 Analyze (Gen_Parm_Decl);
2613 Next (Gen_Parm_Decl);
2616 Generate_Reference_To_Generic_Formals (Current_Scope);
2617 end Analyze_Generic_Formal_Part;
2619 ------------------------------------------
2620 -- Analyze_Generic_Package_Declaration --
2621 ------------------------------------------
2623 procedure Analyze_Generic_Package_Declaration (N : Node_Id) is
2624 Loc : constant Source_Ptr := Sloc (N);
2627 Save_Parent : Node_Id;
2629 Decls : constant List_Id :=
2630 Visible_Declarations (Specification (N));
2634 -- We introduce a renaming of the enclosing package, to have a usable
2635 -- entity as the prefix of an expanded name for a local entity of the
2636 -- form Par.P.Q, where P is the generic package. This is because a local
2637 -- entity named P may hide it, so that the usual visibility rules in
2638 -- the instance will not resolve properly.
2641 Make_Package_Renaming_Declaration (Loc,
2642 Defining_Unit_Name =>
2643 Make_Defining_Identifier (Loc,
2644 Chars => New_External_Name (Chars (Defining_Entity (N)), "GH")),
2645 Name => Make_Identifier (Loc, Chars (Defining_Entity (N))));
2647 if Present (Decls) then
2648 Decl := First (Decls);
2649 while Present (Decl)
2650 and then Nkind (Decl) = N_Pragma
2655 if Present (Decl) then
2656 Insert_Before (Decl, Renaming);
2658 Append (Renaming, Visible_Declarations (Specification (N)));
2662 Set_Visible_Declarations (Specification (N), New_List (Renaming));
2665 -- Create copy of generic unit, and save for instantiation. If the unit
2666 -- is a child unit, do not copy the specifications for the parent, which
2667 -- are not part of the generic tree.
2669 Save_Parent := Parent_Spec (N);
2670 Set_Parent_Spec (N, Empty);
2672 New_N := Copy_Generic_Node (N, Empty, Instantiating => False);
2673 Set_Parent_Spec (New_N, Save_Parent);
2675 Id := Defining_Entity (N);
2676 Generate_Definition (Id);
2678 -- Expansion is not applied to generic units
2683 Set_Ekind (Id, E_Generic_Package);
2684 Set_Etype (Id, Standard_Void_Type);
2686 Enter_Generic_Scope (Id);
2687 Set_Inner_Instances (Id, New_Elmt_List);
2689 Set_Categorization_From_Pragmas (N);
2690 Set_Is_Pure (Id, Is_Pure (Current_Scope));
2692 -- Link the declaration of the generic homonym in the generic copy to
2693 -- the package it renames, so that it is always resolved properly.
2695 Set_Generic_Homonym (Id, Defining_Unit_Name (Renaming));
2696 Set_Entity (Associated_Node (Name (Renaming)), Id);
2698 -- For a library unit, we have reconstructed the entity for the unit,
2699 -- and must reset it in the library tables.
2701 if Nkind (Parent (N)) = N_Compilation_Unit then
2702 Set_Cunit_Entity (Current_Sem_Unit, Id);
2705 Analyze_Generic_Formal_Part (N);
2707 -- After processing the generic formals, analysis proceeds as for a
2708 -- non-generic package.
2710 Analyze (Specification (N));
2712 Validate_Categorization_Dependency (N, Id);
2716 End_Package_Scope (Id);
2717 Exit_Generic_Scope (Id);
2719 if Nkind (Parent (N)) /= N_Compilation_Unit then
2720 Move_Freeze_Nodes (Id, N, Visible_Declarations (Specification (N)));
2721 Move_Freeze_Nodes (Id, N, Private_Declarations (Specification (N)));
2722 Move_Freeze_Nodes (Id, N, Generic_Formal_Declarations (N));
2725 Set_Body_Required (Parent (N), Unit_Requires_Body (Id));
2726 Validate_RT_RAT_Component (N);
2728 -- If this is a spec without a body, check that generic parameters
2731 if not Body_Required (Parent (N)) then
2732 Check_References (Id);
2735 end Analyze_Generic_Package_Declaration;
2737 --------------------------------------------
2738 -- Analyze_Generic_Subprogram_Declaration --
2739 --------------------------------------------
2741 procedure Analyze_Generic_Subprogram_Declaration (N : Node_Id) is
2746 Result_Type : Entity_Id;
2747 Save_Parent : Node_Id;
2751 -- Create copy of generic unit, and save for instantiation. If the unit
2752 -- is a child unit, do not copy the specifications for the parent, which
2753 -- are not part of the generic tree.
2755 Save_Parent := Parent_Spec (N);
2756 Set_Parent_Spec (N, Empty);
2758 New_N := Copy_Generic_Node (N, Empty, Instantiating => False);
2759 Set_Parent_Spec (New_N, Save_Parent);
2762 Spec := Specification (N);
2763 Id := Defining_Entity (Spec);
2764 Generate_Definition (Id);
2766 if Nkind (Id) = N_Defining_Operator_Symbol then
2768 ("operator symbol not allowed for generic subprogram", Id);
2775 Set_Scope_Depth_Value (Id, Scope_Depth (Current_Scope) + 1);
2777 Enter_Generic_Scope (Id);
2778 Set_Inner_Instances (Id, New_Elmt_List);
2779 Set_Is_Pure (Id, Is_Pure (Current_Scope));
2781 Analyze_Generic_Formal_Part (N);
2783 Formals := Parameter_Specifications (Spec);
2785 if Present (Formals) then
2786 Process_Formals (Formals, Spec);
2789 if Nkind (Spec) = N_Function_Specification then
2790 Set_Ekind (Id, E_Generic_Function);
2792 if Nkind (Result_Definition (Spec)) = N_Access_Definition then
2793 Result_Type := Access_Definition (Spec, Result_Definition (Spec));
2794 Set_Etype (Id, Result_Type);
2796 Find_Type (Result_Definition (Spec));
2797 Typ := Entity (Result_Definition (Spec));
2799 -- If a null exclusion is imposed on the result type, then create
2800 -- a null-excluding itype (an access subtype) and use it as the
2801 -- function's Etype.
2803 if Is_Access_Type (Typ)
2804 and then Null_Exclusion_Present (Spec)
2807 Create_Null_Excluding_Itype
2809 Related_Nod => Spec,
2810 Scope_Id => Defining_Unit_Name (Spec)));
2812 Set_Etype (Id, Typ);
2817 Set_Ekind (Id, E_Generic_Procedure);
2818 Set_Etype (Id, Standard_Void_Type);
2821 -- For a library unit, we have reconstructed the entity for the unit,
2822 -- and must reset it in the library tables. We also make sure that
2823 -- Body_Required is set properly in the original compilation unit node.
2825 if Nkind (Parent (N)) = N_Compilation_Unit then
2826 Set_Cunit_Entity (Current_Sem_Unit, Id);
2827 Set_Body_Required (Parent (N), Unit_Requires_Body (Id));
2830 Set_Categorization_From_Pragmas (N);
2831 Validate_Categorization_Dependency (N, Id);
2833 Save_Global_References (Original_Node (N));
2837 Exit_Generic_Scope (Id);
2838 Generate_Reference_To_Formals (Id);
2839 end Analyze_Generic_Subprogram_Declaration;
2841 -----------------------------------
2842 -- Analyze_Package_Instantiation --
2843 -----------------------------------
2845 procedure Analyze_Package_Instantiation (N : Node_Id) is
2846 Loc : constant Source_Ptr := Sloc (N);
2847 Gen_Id : constant Node_Id := Name (N);
2850 Act_Decl_Name : Node_Id;
2851 Act_Decl_Id : Entity_Id;
2856 Gen_Unit : Entity_Id;
2858 Is_Actual_Pack : constant Boolean :=
2859 Is_Internal (Defining_Entity (N));
2861 Env_Installed : Boolean := False;
2862 Parent_Installed : Boolean := False;
2863 Renaming_List : List_Id;
2864 Unit_Renaming : Node_Id;
2865 Needs_Body : Boolean;
2866 Inline_Now : Boolean := False;
2868 procedure Delay_Descriptors (E : Entity_Id);
2869 -- Delay generation of subprogram descriptors for given entity
2871 function Might_Inline_Subp return Boolean;
2872 -- If inlining is active and the generic contains inlined subprograms,
2873 -- we instantiate the body. This may cause superfluous instantiations,
2874 -- but it is simpler than detecting the need for the body at the point
2875 -- of inlining, when the context of the instance is not available.
2877 -----------------------
2878 -- Delay_Descriptors --
2879 -----------------------
2881 procedure Delay_Descriptors (E : Entity_Id) is
2883 if not Delay_Subprogram_Descriptors (E) then
2884 Set_Delay_Subprogram_Descriptors (E);
2885 Pending_Descriptor.Append (E);
2887 end Delay_Descriptors;
2889 -----------------------
2890 -- Might_Inline_Subp --
2891 -----------------------
2893 function Might_Inline_Subp return Boolean is
2897 if not Inline_Processing_Required then
2901 E := First_Entity (Gen_Unit);
2902 while Present (E) loop
2903 if Is_Subprogram (E)
2904 and then Is_Inlined (E)
2914 end Might_Inline_Subp;
2916 -- Start of processing for Analyze_Package_Instantiation
2919 -- Very first thing: apply the special kludge for Text_IO processing
2920 -- in case we are instantiating one of the children of [Wide_]Text_IO.
2922 Text_IO_Kludge (Name (N));
2924 -- Make node global for error reporting
2926 Instantiation_Node := N;
2928 -- Case of instantiation of a generic package
2930 if Nkind (N) = N_Package_Instantiation then
2931 Act_Decl_Id := New_Copy (Defining_Entity (N));
2932 Set_Comes_From_Source (Act_Decl_Id, True);
2934 if Nkind (Defining_Unit_Name (N)) = N_Defining_Program_Unit_Name then
2936 Make_Defining_Program_Unit_Name (Loc,
2937 Name => New_Copy_Tree (Name (Defining_Unit_Name (N))),
2938 Defining_Identifier => Act_Decl_Id);
2940 Act_Decl_Name := Act_Decl_Id;
2943 -- Case of instantiation of a formal package
2946 Act_Decl_Id := Defining_Identifier (N);
2947 Act_Decl_Name := Act_Decl_Id;
2950 Generate_Definition (Act_Decl_Id);
2951 Preanalyze_Actuals (N);
2954 Env_Installed := True;
2955 Check_Generic_Child_Unit (Gen_Id, Parent_Installed);
2956 Gen_Unit := Entity (Gen_Id);
2958 -- Verify that it is the name of a generic package
2960 if Etype (Gen_Unit) = Any_Type then
2964 elsif Ekind (Gen_Unit) /= E_Generic_Package then
2966 -- Ada 2005 (AI-50217): Cannot use instance in limited with_clause
2968 if From_With_Type (Gen_Unit) then
2970 ("cannot instantiate a limited withed package", Gen_Id);
2973 ("expect name of generic package in instantiation", Gen_Id);
2980 if In_Extended_Main_Source_Unit (N) then
2981 Set_Is_Instantiated (Gen_Unit);
2982 Generate_Reference (Gen_Unit, N);
2984 if Present (Renamed_Object (Gen_Unit)) then
2985 Set_Is_Instantiated (Renamed_Object (Gen_Unit));
2986 Generate_Reference (Renamed_Object (Gen_Unit), N);
2990 if Nkind (Gen_Id) = N_Identifier
2991 and then Chars (Gen_Unit) = Chars (Defining_Entity (N))
2994 ("& is hidden within declaration of instance", Gen_Id, Gen_Unit);
2996 elsif Nkind (Gen_Id) = N_Expanded_Name
2997 and then Is_Child_Unit (Gen_Unit)
2998 and then Nkind (Prefix (Gen_Id)) = N_Identifier
2999 and then Chars (Act_Decl_Id) = Chars (Prefix (Gen_Id))
3002 ("& is hidden within declaration of instance ", Prefix (Gen_Id));
3005 Set_Entity (Gen_Id, Gen_Unit);
3007 -- If generic is a renaming, get original generic unit
3009 if Present (Renamed_Object (Gen_Unit))
3010 and then Ekind (Renamed_Object (Gen_Unit)) = E_Generic_Package
3012 Gen_Unit := Renamed_Object (Gen_Unit);
3015 -- Verify that there are no circular instantiations
3017 if In_Open_Scopes (Gen_Unit) then
3018 Error_Msg_NE ("instantiation of & within itself", N, Gen_Unit);
3022 elsif Contains_Instance_Of (Gen_Unit, Current_Scope, Gen_Id) then
3023 Error_Msg_Node_2 := Current_Scope;
3025 ("circular Instantiation: & instantiated in &!", N, Gen_Unit);
3026 Circularity_Detected := True;
3031 Gen_Decl := Unit_Declaration_Node (Gen_Unit);
3033 -- Initialize renamings map, for error checking, and the list that
3034 -- holds private entities whose views have changed between generic
3035 -- definition and instantiation. If this is the instance created to
3036 -- validate an actual package, the instantiation environment is that
3037 -- of the enclosing instance.
3039 Generic_Renamings.Set_Last (0);
3040 Generic_Renamings_HTable.Reset;
3042 Create_Instantiation_Source (N, Gen_Unit, False, S_Adjustment);
3044 -- Copy original generic tree, to produce text for instantiation
3048 (Original_Node (Gen_Decl), Empty, Instantiating => True);
3050 Act_Spec := Specification (Act_Tree);
3052 -- If this is the instance created to validate an actual package,
3053 -- only the formals matter, do not examine the package spec itself.
3055 if Is_Actual_Pack then
3056 Set_Visible_Declarations (Act_Spec, New_List);
3057 Set_Private_Declarations (Act_Spec, New_List);
3061 Analyze_Associations
3063 Generic_Formal_Declarations (Act_Tree),
3064 Generic_Formal_Declarations (Gen_Decl));
3066 Set_Instance_Env (Gen_Unit, Act_Decl_Id);
3067 Set_Defining_Unit_Name (Act_Spec, Act_Decl_Name);
3068 Set_Is_Generic_Instance (Act_Decl_Id);
3070 Set_Generic_Parent (Act_Spec, Gen_Unit);
3072 -- References to the generic in its own declaration or its body are
3073 -- references to the instance. Add a renaming declaration for the
3074 -- generic unit itself. This declaration, as well as the renaming
3075 -- declarations for the generic formals, must remain private to the
3076 -- unit: the formals, because this is the language semantics, and
3077 -- the unit because its use is an artifact of the implementation.
3080 Make_Package_Renaming_Declaration (Loc,
3081 Defining_Unit_Name =>
3082 Make_Defining_Identifier (Loc, Chars (Gen_Unit)),
3083 Name => New_Reference_To (Act_Decl_Id, Loc));
3085 Append (Unit_Renaming, Renaming_List);
3087 -- The renaming declarations are the first local declarations of
3090 if Is_Non_Empty_List (Visible_Declarations (Act_Spec)) then
3092 (First (Visible_Declarations (Act_Spec)), Renaming_List);
3094 Set_Visible_Declarations (Act_Spec, Renaming_List);
3098 Make_Package_Declaration (Loc,
3099 Specification => Act_Spec);
3101 -- Save the instantiation node, for subsequent instantiation of the
3102 -- body, if there is one and we are generating code for the current
3103 -- unit. Mark the unit as having a body, to avoid a premature error
3106 -- We instantiate the body if we are generating code, if we are
3107 -- generating cross-reference information, or if we are building
3108 -- trees for ASIS use.
3111 Enclosing_Body_Present : Boolean := False;
3112 -- If the generic unit is not a compilation unit, then a body may
3113 -- be present in its parent even if none is required. We create a
3114 -- tentative pending instantiation for the body, which will be
3115 -- discarded if none is actually present.
3120 if Scope (Gen_Unit) /= Standard_Standard
3121 and then not Is_Child_Unit (Gen_Unit)
3123 Scop := Scope (Gen_Unit);
3125 while Present (Scop)
3126 and then Scop /= Standard_Standard
3128 if Unit_Requires_Body (Scop) then
3129 Enclosing_Body_Present := True;
3132 elsif In_Open_Scopes (Scop)
3133 and then In_Package_Body (Scop)
3135 Enclosing_Body_Present := True;
3139 exit when Is_Compilation_Unit (Scop);
3140 Scop := Scope (Scop);
3144 -- If front-end inlining is enabled, and this is a unit for which
3145 -- code will be generated, we instantiate the body at once.
3147 -- This is done if the instance is not the main unit, and if the
3148 -- generic is not a child unit of another generic, to avoid scope
3149 -- problems and the reinstallation of parent instances.
3152 and then (not Is_Child_Unit (Gen_Unit)
3153 or else not Is_Generic_Unit (Scope (Gen_Unit)))
3154 and then Might_Inline_Subp
3155 and then not Is_Actual_Pack
3157 if Front_End_Inlining
3158 and then (Is_In_Main_Unit (N)
3159 or else In_Main_Context (Current_Scope))
3160 and then Nkind (Parent (N)) /= N_Compilation_Unit
3164 -- In configurable_run_time mode we force the inlining of
3165 -- predefined subprograms marked Inline_Always, to minimize
3166 -- the use of the run-time library.
3168 elsif Is_Predefined_File_Name
3169 (Unit_File_Name (Get_Source_Unit (Gen_Decl)))
3170 and then Configurable_Run_Time_Mode
3171 and then Nkind (Parent (N)) /= N_Compilation_Unit
3176 -- If the current scope is itself an instance within a child
3177 -- unit, there will be duplications in the scope stack, and the
3178 -- unstacking mechanism in Inline_Instance_Body will fail.
3179 -- This loses some rare cases of optimization, and might be
3180 -- improved some day, if we can find a proper abstraction for
3181 -- "the complete compilation context" that can be saved and
3184 if Is_Generic_Instance (Current_Scope) then
3186 Curr_Unit : constant Entity_Id :=
3187 Cunit_Entity (Current_Sem_Unit);
3189 if Curr_Unit /= Current_Scope
3190 and then Is_Child_Unit (Curr_Unit)
3192 Inline_Now := False;
3199 (Unit_Requires_Body (Gen_Unit)
3200 or else Enclosing_Body_Present
3201 or else Present (Corresponding_Body (Gen_Decl)))
3202 and then (Is_In_Main_Unit (N)
3203 or else Might_Inline_Subp)
3204 and then not Is_Actual_Pack
3205 and then not Inline_Now
3206 and then (Operating_Mode = Generate_Code
3207 or else (Operating_Mode = Check_Semantics
3208 and then ASIS_Mode));
3210 -- If front_end_inlining is enabled, do not instantiate body if
3211 -- within a generic context.
3213 if (Front_End_Inlining
3214 and then not Expander_Active)
3215 or else Is_Generic_Unit (Cunit_Entity (Main_Unit))
3217 Needs_Body := False;
3220 -- If the current context is generic, and the package being
3221 -- instantiated is declared within a formal package, there is no
3222 -- body to instantiate until the enclosing generic is instantiated
3223 -- and there is an actual for the formal package. If the formal
3224 -- package has parameters, we build a regular package instance for
3225 -- it, that precedes the original formal package declaration.
3227 if In_Open_Scopes (Scope (Scope (Gen_Unit))) then
3229 Decl : constant Node_Id :=
3231 (Unit_Declaration_Node (Scope (Gen_Unit)));
3233 if Nkind (Decl) = N_Formal_Package_Declaration
3234 or else (Nkind (Decl) = N_Package_Declaration
3235 and then Is_List_Member (Decl)
3236 and then Present (Next (Decl))
3238 Nkind (Next (Decl)) =
3239 N_Formal_Package_Declaration)
3241 Needs_Body := False;
3247 -- If we are generating the calling stubs from the instantiation of
3248 -- a generic RCI package, we will not use the body of the generic
3251 if Distribution_Stub_Mode = Generate_Caller_Stub_Body
3252 and then Is_Compilation_Unit (Defining_Entity (N))
3254 Needs_Body := False;
3259 -- Here is a defence against a ludicrous number of instantiations
3260 -- caused by a circular set of instantiation attempts.
3262 if Pending_Instantiations.Last >
3263 Hostparm.Max_Instantiations
3265 Error_Msg_N ("too many instantiations", N);
3266 raise Unrecoverable_Error;
3269 -- Indicate that the enclosing scopes contain an instantiation,
3270 -- and that cleanup actions should be delayed until after the
3271 -- instance body is expanded.
3273 Check_Forward_Instantiation (Gen_Decl);
3274 if Nkind (N) = N_Package_Instantiation then
3276 Enclosing_Master : Entity_Id;
3279 -- Loop to search enclosing masters
3281 Enclosing_Master := Current_Scope;
3282 Scope_Loop : while Enclosing_Master /= Standard_Standard loop
3283 if Ekind (Enclosing_Master) = E_Package then
3284 if Is_Compilation_Unit (Enclosing_Master) then
3285 if In_Package_Body (Enclosing_Master) then
3287 (Body_Entity (Enclosing_Master));
3296 Enclosing_Master := Scope (Enclosing_Master);
3299 elsif Ekind (Enclosing_Master) = E_Generic_Package then
3300 Enclosing_Master := Scope (Enclosing_Master);
3302 elsif Is_Generic_Subprogram (Enclosing_Master)
3303 or else Ekind (Enclosing_Master) = E_Void
3305 -- Cleanup actions will eventually be performed on the
3306 -- enclosing instance, if any. Enclosing scope is void
3307 -- in the formal part of a generic subprogram.
3312 if Ekind (Enclosing_Master) = E_Entry
3314 Ekind (Scope (Enclosing_Master)) = E_Protected_Type
3316 if not Expander_Active then
3320 Protected_Body_Subprogram (Enclosing_Master);
3324 Set_Delay_Cleanups (Enclosing_Master);
3326 while Ekind (Enclosing_Master) = E_Block loop
3327 Enclosing_Master := Scope (Enclosing_Master);
3330 if Is_Subprogram (Enclosing_Master) then
3331 Delay_Descriptors (Enclosing_Master);
3333 elsif Is_Task_Type (Enclosing_Master) then
3335 TBP : constant Node_Id :=
3336 Get_Task_Body_Procedure
3339 if Present (TBP) then
3340 Delay_Descriptors (TBP);
3341 Set_Delay_Cleanups (TBP);
3348 end loop Scope_Loop;
3351 -- Make entry in table
3353 Pending_Instantiations.Append
3355 Act_Decl => Act_Decl,
3356 Expander_Status => Expander_Active,
3357 Current_Sem_Unit => Current_Sem_Unit,
3358 Scope_Suppress => Scope_Suppress,
3359 Local_Suppress_Stack_Top => Local_Suppress_Stack_Top));
3363 Set_Categorization_From_Pragmas (Act_Decl);
3365 if Parent_Installed then
3369 Set_Instance_Spec (N, Act_Decl);
3371 -- If not a compilation unit, insert the package declaration before
3372 -- the original instantiation node.
3374 if Nkind (Parent (N)) /= N_Compilation_Unit then
3375 Mark_Rewrite_Insertion (Act_Decl);
3376 Insert_Before (N, Act_Decl);
3379 -- For an instantiation that is a compilation unit, place declaration
3380 -- on current node so context is complete for analysis (including
3381 -- nested instantiations). If this is the main unit, the declaration
3382 -- eventually replaces the instantiation node. If the instance body
3383 -- is created later, it replaces the instance node, and the
3384 -- declaration is attached to it (see
3385 -- Build_Instance_Compilation_Unit_Nodes).
3388 if Cunit_Entity (Current_Sem_Unit) = Defining_Entity (N) then
3390 -- The entity for the current unit is the newly created one,
3391 -- and all semantic information is attached to it.
3393 Set_Cunit_Entity (Current_Sem_Unit, Act_Decl_Id);
3395 -- If this is the main unit, replace the main entity as well
3397 if Current_Sem_Unit = Main_Unit then
3398 Main_Unit_Entity := Act_Decl_Id;
3402 Set_Unit (Parent (N), Act_Decl);
3403 Set_Parent_Spec (Act_Decl, Parent_Spec (N));
3404 Set_Package_Instantiation (Act_Decl_Id, N);
3406 Set_Unit (Parent (N), N);
3407 Set_Body_Required (Parent (N), False);
3409 -- We never need elaboration checks on instantiations, since by
3410 -- definition, the body instantiation is elaborated at the same
3411 -- time as the spec instantiation.
3413 Set_Suppress_Elaboration_Warnings (Act_Decl_Id);
3414 Set_Kill_Elaboration_Checks (Act_Decl_Id);
3417 Check_Elab_Instantiation (N);
3419 if ABE_Is_Certain (N) and then Needs_Body then
3420 Pending_Instantiations.Decrement_Last;
3423 Check_Hidden_Child_Unit (N, Gen_Unit, Act_Decl_Id);
3425 Set_First_Private_Entity (Defining_Unit_Name (Unit_Renaming),
3426 First_Private_Entity (Act_Decl_Id));
3428 -- If the instantiation will receive a body, the unit will be
3429 -- transformed into a package body, and receive its own elaboration
3430 -- entity. Otherwise, the nature of the unit is now a package
3433 if Nkind (Parent (N)) = N_Compilation_Unit
3434 and then not Needs_Body
3436 Rewrite (N, Act_Decl);
3439 if Present (Corresponding_Body (Gen_Decl))
3440 or else Unit_Requires_Body (Gen_Unit)
3442 Set_Has_Completion (Act_Decl_Id);
3445 Check_Formal_Packages (Act_Decl_Id);
3447 Restore_Private_Views (Act_Decl_Id);
3449 Inherit_Context (Gen_Decl, N);
3451 if Parent_Installed then
3456 Env_Installed := False;
3459 Validate_Categorization_Dependency (N, Act_Decl_Id);
3461 -- There used to be a check here to prevent instantiations in local
3462 -- contexts if the No_Local_Allocators restriction was active. This
3463 -- check was removed by a binding interpretation in AI-95-00130/07,
3464 -- but we retain the code for documentation purposes.
3466 -- if Ekind (Act_Decl_Id) /= E_Void
3467 -- and then not Is_Library_Level_Entity (Act_Decl_Id)
3469 -- Check_Restriction (No_Local_Allocators, N);
3473 Inline_Instance_Body (N, Gen_Unit, Act_Decl);
3476 -- The following is a tree patch for ASIS: ASIS needs separate nodes to
3477 -- be used as defining identifiers for a formal package and for the
3478 -- corresponding expanded package.
3480 if Nkind (N) = N_Formal_Package_Declaration then
3481 Act_Decl_Id := New_Copy (Defining_Entity (N));
3482 Set_Comes_From_Source (Act_Decl_Id, True);
3483 Set_Is_Generic_Instance (Act_Decl_Id, False);
3484 Set_Defining_Identifier (N, Act_Decl_Id);
3488 when Instantiation_Error =>
3489 if Parent_Installed then
3493 if Env_Installed then
3496 end Analyze_Package_Instantiation;
3498 --------------------------
3499 -- Inline_Instance_Body --
3500 --------------------------
3502 procedure Inline_Instance_Body
3504 Gen_Unit : Entity_Id;
3508 Gen_Comp : constant Entity_Id :=
3509 Cunit_Entity (Get_Source_Unit (Gen_Unit));
3510 Curr_Comp : constant Node_Id := Cunit (Current_Sem_Unit);
3511 Curr_Scope : Entity_Id := Empty;
3512 Curr_Unit : constant Entity_Id :=
3513 Cunit_Entity (Current_Sem_Unit);
3514 Removed : Boolean := False;
3515 Num_Scopes : Int := 0;
3517 Scope_Stack_Depth : constant Int :=
3518 Scope_Stack.Last - Scope_Stack.First + 1;
3520 Use_Clauses : array (1 .. Scope_Stack_Depth) of Node_Id;
3521 Instances : array (1 .. Scope_Stack_Depth) of Entity_Id;
3522 Inner_Scopes : array (1 .. Scope_Stack_Depth) of Entity_Id;
3523 Num_Inner : Int := 0;
3524 N_Instances : Int := 0;
3528 -- Case of generic unit defined in another unit. We must remove the
3529 -- complete context of the current unit to install that of the generic.
3531 if Gen_Comp /= Cunit_Entity (Current_Sem_Unit) then
3533 -- Add some comments for the following two loops ???
3536 while Present (S) and then S /= Standard_Standard loop
3538 Num_Scopes := Num_Scopes + 1;
3540 Use_Clauses (Num_Scopes) :=
3542 (Scope_Stack.Last - Num_Scopes + 1).
3544 End_Use_Clauses (Use_Clauses (Num_Scopes));
3546 exit when Scope_Stack.Last - Num_Scopes + 1 = Scope_Stack.First
3547 or else Scope_Stack.Table
3548 (Scope_Stack.Last - Num_Scopes).Entity
3552 exit when Is_Generic_Instance (S)
3553 and then (In_Package_Body (S)
3554 or else Ekind (S) = E_Procedure
3555 or else Ekind (S) = E_Function);
3559 Vis := Is_Immediately_Visible (Gen_Comp);
3561 -- Find and save all enclosing instances
3566 and then S /= Standard_Standard
3568 if Is_Generic_Instance (S) then
3569 N_Instances := N_Instances + 1;
3570 Instances (N_Instances) := S;
3572 exit when In_Package_Body (S);
3578 -- Remove context of current compilation unit, unless we are within a
3579 -- nested package instantiation, in which case the context has been
3580 -- removed previously.
3582 -- If current scope is the body of a child unit, remove context of
3583 -- spec as well. If an enclosing scope is an instance body, the
3584 -- context has already been removed, but the entities in the body
3585 -- must be made invisible as well.
3590 and then S /= Standard_Standard
3592 if Is_Generic_Instance (S)
3593 and then (In_Package_Body (S)
3594 or else Ekind (S) = E_Procedure
3595 or else Ekind (S) = E_Function)
3597 -- We still have to remove the entities of the enclosing
3598 -- instance from direct visibility.
3603 E := First_Entity (S);
3604 while Present (E) loop
3605 Set_Is_Immediately_Visible (E, False);
3614 or else (Ekind (Curr_Unit) = E_Package_Body
3615 and then S = Spec_Entity (Curr_Unit))
3616 or else (Ekind (Curr_Unit) = E_Subprogram_Body
3619 (Unit_Declaration_Node (Curr_Unit)))
3623 -- Remove entities in current scopes from visibility, so that
3624 -- instance body is compiled in a clean environment.
3626 Save_Scope_Stack (Handle_Use => False);
3628 if Is_Child_Unit (S) then
3630 -- Remove child unit from stack, as well as inner scopes.
3631 -- Removing the context of a child unit removes parent units
3634 while Current_Scope /= S loop
3635 Num_Inner := Num_Inner + 1;
3636 Inner_Scopes (Num_Inner) := Current_Scope;
3641 Remove_Context (Curr_Comp);
3645 Remove_Context (Curr_Comp);
3648 if Ekind (Curr_Unit) = E_Package_Body then
3649 Remove_Context (Library_Unit (Curr_Comp));
3655 pragma Assert (Num_Inner < Num_Scopes);
3657 Push_Scope (Standard_Standard);
3658 Scope_Stack.Table (Scope_Stack.Last).Is_Active_Stack_Base := True;
3659 Instantiate_Package_Body
3662 Act_Decl => Act_Decl,
3663 Expander_Status => Expander_Active,
3664 Current_Sem_Unit => Current_Sem_Unit,
3665 Scope_Suppress => Scope_Suppress,
3666 Local_Suppress_Stack_Top => Local_Suppress_Stack_Top)),
3667 Inlined_Body => True);
3673 Set_Is_Immediately_Visible (Gen_Comp, Vis);
3675 -- Reset Generic_Instance flag so that use clauses can be installed
3676 -- in the proper order. (See Use_One_Package for effect of enclosing
3677 -- instances on processing of use clauses).
3679 for J in 1 .. N_Instances loop
3680 Set_Is_Generic_Instance (Instances (J), False);
3684 Install_Context (Curr_Comp);
3686 if Present (Curr_Scope)
3687 and then Is_Child_Unit (Curr_Scope)
3689 Push_Scope (Curr_Scope);
3690 Set_Is_Immediately_Visible (Curr_Scope);
3692 -- Finally, restore inner scopes as well
3694 for J in reverse 1 .. Num_Inner loop
3695 Push_Scope (Inner_Scopes (J));
3699 Restore_Scope_Stack (Handle_Use => False);
3701 if Present (Curr_Scope)
3703 (In_Private_Part (Curr_Scope)
3704 or else In_Package_Body (Curr_Scope))
3706 -- Install private declaration of ancestor units, which are
3707 -- currently available. Restore_Scope_Stack and Install_Context
3708 -- only install the visible part of parents.
3713 Par := Scope (Curr_Scope);
3714 while (Present (Par))
3715 and then Par /= Standard_Standard
3717 Install_Private_Declarations (Par);
3724 -- Restore use clauses. For a child unit, use clauses in the parents
3725 -- are restored when installing the context, so only those in inner
3726 -- scopes (and those local to the child unit itself) need to be
3727 -- installed explicitly.
3729 if Is_Child_Unit (Curr_Unit)
3732 for J in reverse 1 .. Num_Inner + 1 loop
3733 Scope_Stack.Table (Scope_Stack.Last - J + 1).First_Use_Clause :=
3735 Install_Use_Clauses (Use_Clauses (J));
3739 for J in reverse 1 .. Num_Scopes loop
3740 Scope_Stack.Table (Scope_Stack.Last - J + 1).First_Use_Clause :=
3742 Install_Use_Clauses (Use_Clauses (J));
3746 -- Restore status of instances. If one of them is a body, make
3747 -- its local entities visible again.
3754 for J in 1 .. N_Instances loop
3755 Inst := Instances (J);
3756 Set_Is_Generic_Instance (Inst, True);
3758 if In_Package_Body (Inst)
3759 or else Ekind (S) = E_Procedure
3760 or else Ekind (S) = E_Function
3762 E := First_Entity (Instances (J));
3763 while Present (E) loop
3764 Set_Is_Immediately_Visible (E);
3771 -- If generic unit is in current unit, current context is correct
3774 Instantiate_Package_Body
3777 Act_Decl => Act_Decl,
3778 Expander_Status => Expander_Active,
3779 Current_Sem_Unit => Current_Sem_Unit,
3780 Scope_Suppress => Scope_Suppress,
3781 Local_Suppress_Stack_Top => Local_Suppress_Stack_Top)),
3782 Inlined_Body => True);
3784 end Inline_Instance_Body;
3786 -------------------------------------
3787 -- Analyze_Procedure_Instantiation --
3788 -------------------------------------
3790 procedure Analyze_Procedure_Instantiation (N : Node_Id) is
3792 Analyze_Subprogram_Instantiation (N, E_Procedure);
3793 end Analyze_Procedure_Instantiation;
3795 -----------------------------------
3796 -- Need_Subprogram_Instance_Body --
3797 -----------------------------------
3799 function Need_Subprogram_Instance_Body
3801 Subp : Entity_Id) return Boolean
3804 if (Is_In_Main_Unit (N)
3805 or else Is_Inlined (Subp)
3806 or else Is_Inlined (Alias (Subp)))
3807 and then (Operating_Mode = Generate_Code
3808 or else (Operating_Mode = Check_Semantics
3809 and then ASIS_Mode))
3810 and then (Expander_Active or else ASIS_Mode)
3811 and then not ABE_Is_Certain (N)
3812 and then not Is_Eliminated (Subp)
3814 Pending_Instantiations.Append
3816 Act_Decl => Unit_Declaration_Node (Subp),
3817 Expander_Status => Expander_Active,
3818 Current_Sem_Unit => Current_Sem_Unit,
3819 Scope_Suppress => Scope_Suppress,
3820 Local_Suppress_Stack_Top => Local_Suppress_Stack_Top));
3825 end Need_Subprogram_Instance_Body;
3827 --------------------------------------
3828 -- Analyze_Subprogram_Instantiation --
3829 --------------------------------------
3831 procedure Analyze_Subprogram_Instantiation
3835 Loc : constant Source_Ptr := Sloc (N);
3836 Gen_Id : constant Node_Id := Name (N);
3838 Anon_Id : constant Entity_Id :=
3839 Make_Defining_Identifier (Sloc (Defining_Entity (N)),
3840 Chars => New_External_Name
3841 (Chars (Defining_Entity (N)), 'R'));
3843 Act_Decl_Id : Entity_Id;
3848 Env_Installed : Boolean := False;
3849 Gen_Unit : Entity_Id;
3851 Pack_Id : Entity_Id;
3852 Parent_Installed : Boolean := False;
3853 Renaming_List : List_Id;
3855 procedure Analyze_Instance_And_Renamings;
3856 -- The instance must be analyzed in a context that includes the mappings
3857 -- of generic parameters into actuals. We create a package declaration
3858 -- for this purpose, and a subprogram with an internal name within the
3859 -- package. The subprogram instance is simply an alias for the internal
3860 -- subprogram, declared in the current scope.
3862 ------------------------------------
3863 -- Analyze_Instance_And_Renamings --
3864 ------------------------------------
3866 procedure Analyze_Instance_And_Renamings is
3867 Def_Ent : constant Entity_Id := Defining_Entity (N);
3868 Pack_Decl : Node_Id;
3871 if Nkind (Parent (N)) = N_Compilation_Unit then
3873 -- For the case of a compilation unit, the container package has
3874 -- the same name as the instantiation, to insure that the binder
3875 -- calls the elaboration procedure with the right name. Copy the
3876 -- entity of the instance, which may have compilation level flags
3877 -- (e.g. Is_Child_Unit) set.
3879 Pack_Id := New_Copy (Def_Ent);
3882 -- Otherwise we use the name of the instantiation concatenated
3883 -- with its source position to ensure uniqueness if there are
3884 -- several instantiations with the same name.
3887 Make_Defining_Identifier (Loc,
3888 Chars => New_External_Name
3889 (Related_Id => Chars (Def_Ent),
3891 Suffix_Index => Source_Offset (Sloc (Def_Ent))));
3894 Pack_Decl := Make_Package_Declaration (Loc,
3895 Specification => Make_Package_Specification (Loc,
3896 Defining_Unit_Name => Pack_Id,
3897 Visible_Declarations => Renaming_List,
3898 End_Label => Empty));
3900 Set_Instance_Spec (N, Pack_Decl);
3901 Set_Is_Generic_Instance (Pack_Id);
3902 Set_Debug_Info_Needed (Pack_Id);
3904 -- Case of not a compilation unit
3906 if Nkind (Parent (N)) /= N_Compilation_Unit then
3907 Mark_Rewrite_Insertion (Pack_Decl);
3908 Insert_Before (N, Pack_Decl);
3909 Set_Has_Completion (Pack_Id);
3911 -- Case of an instantiation that is a compilation unit
3913 -- Place declaration on current node so context is complete for
3914 -- analysis (including nested instantiations), and for use in a
3915 -- context_clause (see Analyze_With_Clause).
3918 Set_Unit (Parent (N), Pack_Decl);
3919 Set_Parent_Spec (Pack_Decl, Parent_Spec (N));
3922 Analyze (Pack_Decl);
3923 Check_Formal_Packages (Pack_Id);
3924 Set_Is_Generic_Instance (Pack_Id, False);
3926 -- Body of the enclosing package is supplied when instantiating the
3927 -- subprogram body, after semantic analysis is completed.
3929 if Nkind (Parent (N)) = N_Compilation_Unit then
3931 -- Remove package itself from visibility, so it does not
3932 -- conflict with subprogram.
3934 Set_Name_Entity_Id (Chars (Pack_Id), Homonym (Pack_Id));
3936 -- Set name and scope of internal subprogram so that the proper
3937 -- external name will be generated. The proper scope is the scope
3938 -- of the wrapper package. We need to generate debugging info for
3939 -- the internal subprogram, so set flag accordingly.
3941 Set_Chars (Anon_Id, Chars (Defining_Entity (N)));
3942 Set_Scope (Anon_Id, Scope (Pack_Id));
3944 -- Mark wrapper package as referenced, to avoid spurious warnings
3945 -- if the instantiation appears in various with_ clauses of
3946 -- subunits of the main unit.
3948 Set_Referenced (Pack_Id);
3951 Set_Is_Generic_Instance (Anon_Id);
3952 Set_Debug_Info_Needed (Anon_Id);
3953 Act_Decl_Id := New_Copy (Anon_Id);
3955 Set_Parent (Act_Decl_Id, Parent (Anon_Id));
3956 Set_Chars (Act_Decl_Id, Chars (Defining_Entity (N)));
3957 Set_Sloc (Act_Decl_Id, Sloc (Defining_Entity (N)));
3958 Set_Comes_From_Source (Act_Decl_Id, True);
3960 -- The signature may involve types that are not frozen yet, but the
3961 -- subprogram will be frozen at the point the wrapper package is
3962 -- frozen, so it does not need its own freeze node. In fact, if one
3963 -- is created, it might conflict with the freezing actions from the
3966 Set_Has_Delayed_Freeze (Anon_Id, False);
3968 -- If the instance is a child unit, mark the Id accordingly. Mark
3969 -- the anonymous entity as well, which is the real subprogram and
3970 -- which is used when the instance appears in a context clause.
3972 Set_Is_Child_Unit (Act_Decl_Id, Is_Child_Unit (Defining_Entity (N)));
3973 Set_Is_Child_Unit (Anon_Id, Is_Child_Unit (Defining_Entity (N)));
3974 New_Overloaded_Entity (Act_Decl_Id);
3975 Check_Eliminated (Act_Decl_Id);
3977 -- In compilation unit case, kill elaboration checks on the
3978 -- instantiation, since they are never needed -- the body is
3979 -- instantiated at the same point as the spec.
3981 if Nkind (Parent (N)) = N_Compilation_Unit then
3982 Set_Suppress_Elaboration_Warnings (Act_Decl_Id);
3983 Set_Kill_Elaboration_Checks (Act_Decl_Id);
3984 Set_Is_Compilation_Unit (Anon_Id);
3986 Set_Cunit_Entity (Current_Sem_Unit, Pack_Id);
3989 -- The instance is not a freezing point for the new subprogram
3991 Set_Is_Frozen (Act_Decl_Id, False);
3993 if Nkind (Defining_Entity (N)) = N_Defining_Operator_Symbol then
3994 Valid_Operator_Definition (Act_Decl_Id);
3997 Set_Alias (Act_Decl_Id, Anon_Id);
3998 Set_Parent (Act_Decl_Id, Parent (Anon_Id));
3999 Set_Has_Completion (Act_Decl_Id);
4000 Set_Related_Instance (Pack_Id, Act_Decl_Id);
4002 if Nkind (Parent (N)) = N_Compilation_Unit then
4003 Set_Body_Required (Parent (N), False);
4005 end Analyze_Instance_And_Renamings;
4007 -- Start of processing for Analyze_Subprogram_Instantiation
4010 -- Very first thing: apply the special kludge for Text_IO processing
4011 -- in case we are instantiating one of the children of [Wide_]Text_IO.
4012 -- Of course such an instantiation is bogus (these are packages, not
4013 -- subprograms), but we get a better error message if we do this.
4015 Text_IO_Kludge (Gen_Id);
4017 -- Make node global for error reporting
4019 Instantiation_Node := N;
4020 Preanalyze_Actuals (N);
4023 Env_Installed := True;
4024 Check_Generic_Child_Unit (Gen_Id, Parent_Installed);
4025 Gen_Unit := Entity (Gen_Id);
4027 Generate_Reference (Gen_Unit, Gen_Id);
4029 if Nkind (Gen_Id) = N_Identifier
4030 and then Chars (Gen_Unit) = Chars (Defining_Entity (N))
4033 ("& is hidden within declaration of instance", Gen_Id, Gen_Unit);
4036 if Etype (Gen_Unit) = Any_Type then
4041 -- Verify that it is a generic subprogram of the right kind, and that
4042 -- it does not lead to a circular instantiation.
4044 if Ekind (Gen_Unit) /= E_Generic_Procedure
4045 and then Ekind (Gen_Unit) /= E_Generic_Function
4047 Error_Msg_N ("expect generic subprogram in instantiation", Gen_Id);
4049 elsif In_Open_Scopes (Gen_Unit) then
4050 Error_Msg_NE ("instantiation of & within itself", N, Gen_Unit);
4052 elsif K = E_Procedure
4053 and then Ekind (Gen_Unit) /= E_Generic_Procedure
4055 if Ekind (Gen_Unit) = E_Generic_Function then
4057 ("cannot instantiate generic function as procedure", Gen_Id);
4060 ("expect name of generic procedure in instantiation", Gen_Id);
4063 elsif K = E_Function
4064 and then Ekind (Gen_Unit) /= E_Generic_Function
4066 if Ekind (Gen_Unit) = E_Generic_Procedure then
4068 ("cannot instantiate generic procedure as function", Gen_Id);
4071 ("expect name of generic function in instantiation", Gen_Id);
4075 Set_Entity (Gen_Id, Gen_Unit);
4076 Set_Is_Instantiated (Gen_Unit);
4078 if In_Extended_Main_Source_Unit (N) then
4079 Generate_Reference (Gen_Unit, N);
4082 -- If renaming, get original unit
4084 if Present (Renamed_Object (Gen_Unit))
4085 and then (Ekind (Renamed_Object (Gen_Unit)) = E_Generic_Procedure
4087 Ekind (Renamed_Object (Gen_Unit)) = E_Generic_Function)
4089 Gen_Unit := Renamed_Object (Gen_Unit);
4090 Set_Is_Instantiated (Gen_Unit);
4091 Generate_Reference (Gen_Unit, N);
4094 if Contains_Instance_Of (Gen_Unit, Current_Scope, Gen_Id) then
4095 Error_Msg_Node_2 := Current_Scope;
4097 ("circular Instantiation: & instantiated in &!", N, Gen_Unit);
4098 Circularity_Detected := True;
4102 Gen_Decl := Unit_Declaration_Node (Gen_Unit);
4104 -- Initialize renamings map, for error checking
4106 Generic_Renamings.Set_Last (0);
4107 Generic_Renamings_HTable.Reset;
4109 Create_Instantiation_Source (N, Gen_Unit, False, S_Adjustment);
4111 -- Copy original generic tree, to produce text for instantiation
4115 (Original_Node (Gen_Decl), Empty, Instantiating => True);
4117 -- Inherit overriding indicator from instance node
4119 Act_Spec := Specification (Act_Tree);
4120 Set_Must_Override (Act_Spec, Must_Override (N));
4121 Set_Must_Not_Override (Act_Spec, Must_Not_Override (N));
4124 Analyze_Associations
4126 Generic_Formal_Declarations (Act_Tree),
4127 Generic_Formal_Declarations (Gen_Decl));
4129 -- The subprogram itself cannot contain a nested instance, so the
4130 -- current parent is left empty.
4132 Set_Instance_Env (Gen_Unit, Empty);
4134 -- Build the subprogram declaration, which does not appear in the
4135 -- generic template, and give it a sloc consistent with that of the
4138 Set_Defining_Unit_Name (Act_Spec, Anon_Id);
4139 Set_Generic_Parent (Act_Spec, Gen_Unit);
4141 Make_Subprogram_Declaration (Sloc (Act_Spec),
4142 Specification => Act_Spec);
4144 Set_Categorization_From_Pragmas (Act_Decl);
4146 if Parent_Installed then
4150 Append (Act_Decl, Renaming_List);
4151 Analyze_Instance_And_Renamings;
4153 -- If the generic is marked Import (Intrinsic), then so is the
4154 -- instance. This indicates that there is no body to instantiate. If
4155 -- generic is marked inline, so it the instance, and the anonymous
4156 -- subprogram it renames. If inlined, or else if inlining is enabled
4157 -- for the compilation, we generate the instance body even if it is
4158 -- not within the main unit.
4160 -- Any other pragmas might also be inherited ???
4162 if Is_Intrinsic_Subprogram (Gen_Unit) then
4163 Set_Is_Intrinsic_Subprogram (Anon_Id);
4164 Set_Is_Intrinsic_Subprogram (Act_Decl_Id);
4166 if Chars (Gen_Unit) = Name_Unchecked_Conversion then
4167 Validate_Unchecked_Conversion (N, Act_Decl_Id);
4171 Generate_Definition (Act_Decl_Id);
4173 Set_Is_Inlined (Act_Decl_Id, Is_Inlined (Gen_Unit));
4174 Set_Is_Inlined (Anon_Id, Is_Inlined (Gen_Unit));
4176 if not Is_Intrinsic_Subprogram (Gen_Unit) then
4177 Check_Elab_Instantiation (N);
4180 if Is_Dispatching_Operation (Act_Decl_Id)
4181 and then Ada_Version >= Ada_05
4187 Formal := First_Formal (Act_Decl_Id);
4188 while Present (Formal) loop
4189 if Ekind (Etype (Formal)) = E_Anonymous_Access_Type
4190 and then Is_Controlling_Formal (Formal)
4191 and then not Can_Never_Be_Null (Formal)
4193 Error_Msg_NE ("access parameter& is controlling,",
4195 Error_Msg_NE ("\corresponding parameter of & must be"
4196 & " explicitly null-excluding", N, Gen_Id);
4199 Next_Formal (Formal);
4204 Check_Hidden_Child_Unit (N, Gen_Unit, Act_Decl_Id);
4206 -- Subject to change, pending on if other pragmas are inherited ???
4208 Validate_Categorization_Dependency (N, Act_Decl_Id);
4210 if not Is_Intrinsic_Subprogram (Act_Decl_Id) then
4211 Inherit_Context (Gen_Decl, N);
4213 Restore_Private_Views (Pack_Id, False);
4215 -- If the context requires a full instantiation, mark node for
4216 -- subsequent construction of the body.
4218 if Need_Subprogram_Instance_Body (N, Act_Decl_Id) then
4220 Check_Forward_Instantiation (Gen_Decl);
4222 -- The wrapper package is always delayed, because it does not
4223 -- constitute a freeze point, but to insure that the freeze
4224 -- node is placed properly, it is created directly when
4225 -- instantiating the body (otherwise the freeze node might
4226 -- appear to early for nested instantiations).
4228 elsif Nkind (Parent (N)) = N_Compilation_Unit then
4230 -- For ASIS purposes, indicate that the wrapper package has
4231 -- replaced the instantiation node.
4233 Rewrite (N, Unit (Parent (N)));
4234 Set_Unit (Parent (N), N);
4237 elsif Nkind (Parent (N)) = N_Compilation_Unit then
4239 -- Replace instance node for library-level instantiations of
4240 -- intrinsic subprograms, for ASIS use.
4242 Rewrite (N, Unit (Parent (N)));
4243 Set_Unit (Parent (N), N);
4246 if Parent_Installed then
4251 Env_Installed := False;
4252 Generic_Renamings.Set_Last (0);
4253 Generic_Renamings_HTable.Reset;
4257 when Instantiation_Error =>
4258 if Parent_Installed then
4262 if Env_Installed then
4265 end Analyze_Subprogram_Instantiation;
4267 -------------------------
4268 -- Get_Associated_Node --
4269 -------------------------
4271 function Get_Associated_Node (N : Node_Id) return Node_Id is
4275 Assoc := Associated_Node (N);
4277 if Nkind (Assoc) /= Nkind (N) then
4280 elsif Nkind_In (Assoc, N_Aggregate, N_Extension_Aggregate) then
4284 -- If the node is part of an inner generic, it may itself have been
4285 -- remapped into a further generic copy. Associated_Node is otherwise
4286 -- used for the entity of the node, and will be of a different node
4287 -- kind, or else N has been rewritten as a literal or function call.
4289 while Present (Associated_Node (Assoc))
4290 and then Nkind (Associated_Node (Assoc)) = Nkind (Assoc)
4292 Assoc := Associated_Node (Assoc);
4295 -- Follow and additional link in case the final node was rewritten.
4296 -- This can only happen with nested generic units.
4298 if (Nkind (Assoc) = N_Identifier or else Nkind (Assoc) in N_Op)
4299 and then Present (Associated_Node (Assoc))
4300 and then (Nkind_In (Associated_Node (Assoc), N_Function_Call,
4301 N_Explicit_Dereference,
4306 Assoc := Associated_Node (Assoc);
4311 end Get_Associated_Node;
4313 -------------------------------------------
4314 -- Build_Instance_Compilation_Unit_Nodes --
4315 -------------------------------------------
4317 procedure Build_Instance_Compilation_Unit_Nodes
4322 Decl_Cunit : Node_Id;
4323 Body_Cunit : Node_Id;
4325 New_Main : constant Entity_Id := Defining_Entity (Act_Decl);
4326 Old_Main : constant Entity_Id := Cunit_Entity (Main_Unit);
4329 -- A new compilation unit node is built for the instance declaration
4332 Make_Compilation_Unit (Sloc (N),
4333 Context_Items => Empty_List,
4336 Make_Compilation_Unit_Aux (Sloc (N)));
4338 Set_Parent_Spec (Act_Decl, Parent_Spec (N));
4339 Set_Body_Required (Decl_Cunit, True);
4341 -- We use the original instantiation compilation unit as the resulting
4342 -- compilation unit of the instance, since this is the main unit.
4344 Rewrite (N, Act_Body);
4345 Body_Cunit := Parent (N);
4347 -- The two compilation unit nodes are linked by the Library_Unit field
4349 Set_Library_Unit (Decl_Cunit, Body_Cunit);
4350 Set_Library_Unit (Body_Cunit, Decl_Cunit);
4352 -- Preserve the private nature of the package if needed
4354 Set_Private_Present (Decl_Cunit, Private_Present (Body_Cunit));
4356 -- If the instance is not the main unit, its context, categorization,
4357 -- and elaboration entity are not relevant to the compilation.
4359 if Parent (N) /= Cunit (Main_Unit) then
4363 -- The context clause items on the instantiation, which are now attached
4364 -- to the body compilation unit (since the body overwrote the original
4365 -- instantiation node), semantically belong on the spec, so copy them
4366 -- there. It's harmless to leave them on the body as well. In fact one
4367 -- could argue that they belong in both places.
4369 Citem := First (Context_Items (Body_Cunit));
4370 while Present (Citem) loop
4371 Append (New_Copy (Citem), Context_Items (Decl_Cunit));
4375 -- Propagate categorization flags on packages, so that they appear in
4376 -- the ali file for the spec of the unit.
4378 if Ekind (New_Main) = E_Package then
4379 Set_Is_Pure (Old_Main, Is_Pure (New_Main));
4380 Set_Is_Preelaborated (Old_Main, Is_Preelaborated (New_Main));
4381 Set_Is_Remote_Types (Old_Main, Is_Remote_Types (New_Main));
4382 Set_Is_Shared_Passive (Old_Main, Is_Shared_Passive (New_Main));
4383 Set_Is_Remote_Call_Interface
4384 (Old_Main, Is_Remote_Call_Interface (New_Main));
4387 -- Make entry in Units table, so that binder can generate call to
4388 -- elaboration procedure for body, if any.
4390 Make_Instance_Unit (Body_Cunit);
4391 Main_Unit_Entity := New_Main;
4392 Set_Cunit_Entity (Main_Unit, Main_Unit_Entity);
4394 -- Build elaboration entity, since the instance may certainly generate
4395 -- elaboration code requiring a flag for protection.
4397 Build_Elaboration_Entity (Decl_Cunit, New_Main);
4398 end Build_Instance_Compilation_Unit_Nodes;
4400 -----------------------------
4401 -- Check_Access_Definition --
4402 -----------------------------
4404 procedure Check_Access_Definition (N : Node_Id) is
4407 (Ada_Version >= Ada_05
4408 and then Present (Access_Definition (N)));
4410 end Check_Access_Definition;
4412 -----------------------------------
4413 -- Check_Formal_Package_Instance --
4414 -----------------------------------
4416 -- If the formal has specific parameters, they must match those of the
4417 -- actual. Both of them are instances, and the renaming declarations for
4418 -- their formal parameters appear in the same order in both. The analyzed
4419 -- formal has been analyzed in the context of the current instance.
4421 procedure Check_Formal_Package_Instance
4422 (Formal_Pack : Entity_Id;
4423 Actual_Pack : Entity_Id)
4425 E1 : Entity_Id := First_Entity (Actual_Pack);
4426 E2 : Entity_Id := First_Entity (Formal_Pack);
4431 procedure Check_Mismatch (B : Boolean);
4432 -- Common error routine for mismatch between the parameters of the
4433 -- actual instance and those of the formal package.
4435 function Same_Instantiated_Constant (E1, E2 : Entity_Id) return Boolean;
4436 -- The formal may come from a nested formal package, and the actual may
4437 -- have been constant-folded. To determine whether the two denote the
4438 -- same entity we may have to traverse several definitions to recover
4439 -- the ultimate entity that they refer to.
4441 function Same_Instantiated_Variable (E1, E2 : Entity_Id) return Boolean;
4442 -- Similarly, if the formal comes from a nested formal package, the
4443 -- actual may designate the formal through multiple renamings, which
4444 -- have to be followed to determine the original variable in question.
4446 --------------------
4447 -- Check_Mismatch --
4448 --------------------
4450 procedure Check_Mismatch (B : Boolean) is
4451 Kind : constant Node_Kind := Nkind (Parent (E2));
4454 if Kind = N_Formal_Type_Declaration then
4457 elsif Nkind_In (Kind, N_Formal_Object_Declaration,
4458 N_Formal_Package_Declaration)
4459 or else Kind in N_Formal_Subprogram_Declaration
4465 ("actual for & in actual instance does not match formal",
4466 Parent (Actual_Pack), E1);
4470 --------------------------------
4471 -- Same_Instantiated_Constant --
4472 --------------------------------
4474 function Same_Instantiated_Constant
4475 (E1, E2 : Entity_Id) return Boolean
4481 while Present (Ent) loop
4485 elsif Ekind (Ent) /= E_Constant then
4488 elsif Is_Entity_Name (Constant_Value (Ent)) then
4489 if Entity (Constant_Value (Ent)) = E1 then
4492 Ent := Entity (Constant_Value (Ent));
4495 -- The actual may be a constant that has been folded. Recover
4498 elsif Is_Entity_Name (Original_Node (Constant_Value (Ent))) then
4499 Ent := Entity (Original_Node (Constant_Value (Ent)));
4506 end Same_Instantiated_Constant;
4508 --------------------------------
4509 -- Same_Instantiated_Variable --
4510 --------------------------------
4512 function Same_Instantiated_Variable
4513 (E1, E2 : Entity_Id) return Boolean
4515 function Original_Entity (E : Entity_Id) return Entity_Id;
4516 -- Follow chain of renamings to the ultimate ancestor
4518 ---------------------
4519 -- Original_Entity --
4520 ---------------------
4522 function Original_Entity (E : Entity_Id) return Entity_Id is
4527 while Nkind (Parent (Orig)) = N_Object_Renaming_Declaration
4528 and then Present (Renamed_Object (Orig))
4529 and then Is_Entity_Name (Renamed_Object (Orig))
4531 Orig := Entity (Renamed_Object (Orig));
4535 end Original_Entity;
4537 -- Start of processing for Same_Instantiated_Variable
4540 return Ekind (E1) = Ekind (E2)
4541 and then Original_Entity (E1) = Original_Entity (E2);
4542 end Same_Instantiated_Variable;
4544 -- Start of processing for Check_Formal_Package_Instance
4548 and then Present (E2)
4550 exit when Ekind (E1) = E_Package
4551 and then Renamed_Entity (E1) = Renamed_Entity (Actual_Pack);
4553 -- If the formal is the renaming of the formal package, this
4554 -- is the end of its formal part, which may occur before the
4555 -- end of the formal part in the actual in the presence of
4556 -- defaulted parameters in the formal package.
4558 exit when Nkind (Parent (E2)) = N_Package_Renaming_Declaration
4559 and then Renamed_Entity (E2) = Scope (E2);
4561 -- The analysis of the actual may generate additional internal
4562 -- entities. If the formal is defaulted, there is no corresponding
4563 -- analysis and the internal entities must be skipped, until we
4564 -- find corresponding entities again.
4566 if Comes_From_Source (E2)
4567 and then not Comes_From_Source (E1)
4568 and then Chars (E1) /= Chars (E2)
4571 and then Chars (E1) /= Chars (E2)
4580 -- If the formal entity comes from a formal declaration, it was
4581 -- defaulted in the formal package, and no check is needed on it.
4583 elsif Nkind (Parent (E2)) = N_Formal_Object_Declaration then
4586 elsif Is_Type (E1) then
4588 -- Subtypes must statically match. E1, E2 are the local entities
4589 -- that are subtypes of the actuals. Itypes generated for other
4590 -- parameters need not be checked, the check will be performed
4591 -- on the parameters themselves.
4593 -- If E2 is a formal type declaration, it is a defaulted parameter
4594 -- and needs no checking.
4596 if not Is_Itype (E1)
4597 and then not Is_Itype (E2)
4601 or else Etype (E1) /= Etype (E2)
4602 or else not Subtypes_Statically_Match (E1, E2));
4605 elsif Ekind (E1) = E_Constant then
4607 -- IN parameters must denote the same static value, or the same
4608 -- constant, or the literal null.
4610 Expr1 := Expression (Parent (E1));
4612 if Ekind (E2) /= E_Constant then
4613 Check_Mismatch (True);
4616 Expr2 := Expression (Parent (E2));
4619 if Is_Static_Expression (Expr1) then
4621 if not Is_Static_Expression (Expr2) then
4622 Check_Mismatch (True);
4624 elsif Is_Discrete_Type (Etype (E1)) then
4626 V1 : constant Uint := Expr_Value (Expr1);
4627 V2 : constant Uint := Expr_Value (Expr2);
4629 Check_Mismatch (V1 /= V2);
4632 elsif Is_Real_Type (Etype (E1)) then
4634 V1 : constant Ureal := Expr_Value_R (Expr1);
4635 V2 : constant Ureal := Expr_Value_R (Expr2);
4637 Check_Mismatch (V1 /= V2);
4640 elsif Is_String_Type (Etype (E1))
4641 and then Nkind (Expr1) = N_String_Literal
4643 if Nkind (Expr2) /= N_String_Literal then
4644 Check_Mismatch (True);
4647 (not String_Equal (Strval (Expr1), Strval (Expr2)));
4651 elsif Is_Entity_Name (Expr1) then
4652 if Is_Entity_Name (Expr2) then
4653 if Entity (Expr1) = Entity (Expr2) then
4657 (not Same_Instantiated_Constant
4658 (Entity (Expr1), Entity (Expr2)));
4661 Check_Mismatch (True);
4664 elsif Is_Entity_Name (Original_Node (Expr1))
4665 and then Is_Entity_Name (Expr2)
4667 Same_Instantiated_Constant
4668 (Entity (Original_Node (Expr1)), Entity (Expr2))
4672 elsif Nkind (Expr1) = N_Null then
4673 Check_Mismatch (Nkind (Expr1) /= N_Null);
4676 Check_Mismatch (True);
4679 elsif Ekind (E1) = E_Variable then
4680 Check_Mismatch (not Same_Instantiated_Variable (E1, E2));
4682 elsif Ekind (E1) = E_Package then
4684 (Ekind (E1) /= Ekind (E2)
4685 or else Renamed_Object (E1) /= Renamed_Object (E2));
4687 elsif Is_Overloadable (E1) then
4689 -- Verify that the actual subprograms match. Note that actuals
4690 -- that are attributes are rewritten as subprograms. If the
4691 -- subprogram in the formal package is defaulted, no check is
4692 -- needed. Note that this can only happen in Ada 2005 when the
4693 -- formal package can be partially parametrized.
4695 if Nkind (Unit_Declaration_Node (E1)) =
4696 N_Subprogram_Renaming_Declaration
4697 and then From_Default (Unit_Declaration_Node (E1))
4703 (Ekind (E2) /= Ekind (E1) or else (Alias (E1)) /= Alias (E2));
4707 raise Program_Error;
4714 end Check_Formal_Package_Instance;
4716 ---------------------------
4717 -- Check_Formal_Packages --
4718 ---------------------------
4720 procedure Check_Formal_Packages (P_Id : Entity_Id) is
4722 Formal_P : Entity_Id;
4725 -- Iterate through the declarations in the instance, looking for package
4726 -- renaming declarations that denote instances of formal packages. Stop
4727 -- when we find the renaming of the current package itself. The
4728 -- declaration for a formal package without a box is followed by an
4729 -- internal entity that repeats the instantiation.
4731 E := First_Entity (P_Id);
4732 while Present (E) loop
4733 if Ekind (E) = E_Package then
4734 if Renamed_Object (E) = P_Id then
4737 elsif Nkind (Parent (E)) /= N_Package_Renaming_Declaration then
4740 elsif not Box_Present (Parent (Associated_Formal_Package (E))) then
4741 Formal_P := Next_Entity (E);
4742 Check_Formal_Package_Instance (Formal_P, E);
4744 -- After checking, remove the internal validating package. It
4745 -- is only needed for semantic checks, and as it may contain
4746 -- generic formal declarations it should not reach gigi.
4748 Remove (Unit_Declaration_Node (Formal_P));
4754 end Check_Formal_Packages;
4756 ---------------------------------
4757 -- Check_Forward_Instantiation --
4758 ---------------------------------
4760 procedure Check_Forward_Instantiation (Decl : Node_Id) is
4762 Gen_Comp : Entity_Id := Cunit_Entity (Get_Source_Unit (Decl));
4765 -- The instantiation appears before the generic body if we are in the
4766 -- scope of the unit containing the generic, either in its spec or in
4767 -- the package body, and before the generic body.
4769 if Ekind (Gen_Comp) = E_Package_Body then
4770 Gen_Comp := Spec_Entity (Gen_Comp);
4773 if In_Open_Scopes (Gen_Comp)
4774 and then No (Corresponding_Body (Decl))
4779 and then not Is_Compilation_Unit (S)
4780 and then not Is_Child_Unit (S)
4782 if Ekind (S) = E_Package then
4783 Set_Has_Forward_Instantiation (S);
4789 end Check_Forward_Instantiation;
4791 ---------------------------
4792 -- Check_Generic_Actuals --
4793 ---------------------------
4795 -- The visibility of the actuals may be different between the point of
4796 -- generic instantiation and the instantiation of the body.
4798 procedure Check_Generic_Actuals
4799 (Instance : Entity_Id;
4800 Is_Formal_Box : Boolean)
4805 function Denotes_Previous_Actual (Typ : Entity_Id) return Boolean;
4806 -- For a formal that is an array type, the component type is often a
4807 -- previous formal in the same unit. The privacy status of the component
4808 -- type will have been examined earlier in the traversal of the
4809 -- corresponding actuals, and this status should not be modified for the
4810 -- array type itself.
4812 -- To detect this case we have to rescan the list of formals, which
4813 -- is usually short enough to ignore the resulting inefficiency.
4815 function Denotes_Previous_Actual (Typ : Entity_Id) return Boolean is
4818 Prev := First_Entity (Instance);
4819 while Present (Prev) loop
4821 and then Nkind (Parent (Prev)) = N_Subtype_Declaration
4822 and then Is_Entity_Name (Subtype_Indication (Parent (Prev)))
4823 and then Entity (Subtype_Indication (Parent (Prev))) = Typ
4833 end Denotes_Previous_Actual;
4835 -- Start of processing for Check_Generic_Actuals
4838 E := First_Entity (Instance);
4839 while Present (E) loop
4841 and then Nkind (Parent (E)) = N_Subtype_Declaration
4842 and then Scope (Etype (E)) /= Instance
4843 and then Is_Entity_Name (Subtype_Indication (Parent (E)))
4845 if Is_Array_Type (E)
4846 and then Denotes_Previous_Actual (Component_Type (E))
4850 Check_Private_View (Subtype_Indication (Parent (E)));
4852 Set_Is_Generic_Actual_Type (E, True);
4853 Set_Is_Hidden (E, False);
4854 Set_Is_Potentially_Use_Visible (E,
4857 -- We constructed the generic actual type as a subtype of the
4858 -- supplied type. This means that it normally would not inherit
4859 -- subtype specific attributes of the actual, which is wrong for
4860 -- the generic case.
4862 Astype := Ancestor_Subtype (E);
4866 -- This can happen when E is an itype that is the full view of
4867 -- a private type completed, e.g. with a constrained array. In
4868 -- that case, use the first subtype, which will carry size
4869 -- information. The base type itself is unconstrained and will
4872 Astype := First_Subtype (E);
4875 Set_Size_Info (E, (Astype));
4876 Set_RM_Size (E, RM_Size (Astype));
4877 Set_First_Rep_Item (E, First_Rep_Item (Astype));
4879 if Is_Discrete_Or_Fixed_Point_Type (E) then
4880 Set_RM_Size (E, RM_Size (Astype));
4882 -- In nested instances, the base type of an access actual
4883 -- may itself be private, and need to be exchanged.
4885 elsif Is_Access_Type (E)
4886 and then Is_Private_Type (Etype (E))
4889 (New_Occurrence_Of (Etype (E), Sloc (Instance)));
4892 elsif Ekind (E) = E_Package then
4894 -- If this is the renaming for the current instance, we're done.
4895 -- Otherwise it is a formal package. If the corresponding formal
4896 -- was declared with a box, the (instantiations of the) generic
4897 -- formal part are also visible. Otherwise, ignore the entity
4898 -- created to validate the actuals.
4900 if Renamed_Object (E) = Instance then
4903 elsif Nkind (Parent (E)) /= N_Package_Renaming_Declaration then
4906 -- The visibility of a formal of an enclosing generic is already
4909 elsif Denotes_Formal_Package (E) then
4912 elsif Present (Associated_Formal_Package (E))
4913 and then not Is_Generic_Formal (E)
4915 if Box_Present (Parent (Associated_Formal_Package (E))) then
4916 Check_Generic_Actuals (Renamed_Object (E), True);
4919 Check_Generic_Actuals (Renamed_Object (E), False);
4922 Set_Is_Hidden (E, False);
4925 -- If this is a subprogram instance (in a wrapper package) the
4926 -- actual is fully visible.
4928 elsif Is_Wrapper_Package (Instance) then
4929 Set_Is_Hidden (E, False);
4931 -- If the formal package is declared with a box, or if the formal
4932 -- parameter is defaulted, it is visible in the body.
4935 or else Is_Visible_Formal (E)
4937 Set_Is_Hidden (E, False);
4942 end Check_Generic_Actuals;
4944 ------------------------------
4945 -- Check_Generic_Child_Unit --
4946 ------------------------------
4948 procedure Check_Generic_Child_Unit
4950 Parent_Installed : in out Boolean)
4952 Loc : constant Source_Ptr := Sloc (Gen_Id);
4953 Gen_Par : Entity_Id := Empty;
4955 Inst_Par : Entity_Id;
4958 function Find_Generic_Child
4960 Id : Node_Id) return Entity_Id;
4961 -- Search generic parent for possible child unit with the given name
4963 function In_Enclosing_Instance return Boolean;
4964 -- Within an instance of the parent, the child unit may be denoted
4965 -- by a simple name, or an abbreviated expanded name. Examine enclosing
4966 -- scopes to locate a possible parent instantiation.
4968 ------------------------
4969 -- Find_Generic_Child --
4970 ------------------------
4972 function Find_Generic_Child
4974 Id : Node_Id) return Entity_Id
4979 -- If entity of name is already set, instance has already been
4980 -- resolved, e.g. in an enclosing instantiation.
4982 if Present (Entity (Id)) then
4983 if Scope (Entity (Id)) = Scop then
4990 E := First_Entity (Scop);
4991 while Present (E) loop
4992 if Chars (E) = Chars (Id)
4993 and then Is_Child_Unit (E)
4995 if Is_Child_Unit (E)
4996 and then not Is_Visible_Child_Unit (E)
4999 ("generic child unit& is not visible", Gen_Id, E);
5011 end Find_Generic_Child;
5013 ---------------------------
5014 -- In_Enclosing_Instance --
5015 ---------------------------
5017 function In_Enclosing_Instance return Boolean is
5018 Enclosing_Instance : Node_Id;
5019 Instance_Decl : Node_Id;
5022 -- We do not inline any call that contains instantiations, except
5023 -- for instantiations of Unchecked_Conversion, so if we are within
5024 -- an inlined body the current instance does not require parents.
5026 if In_Inlined_Body then
5027 pragma Assert (Chars (Gen_Id) = Name_Unchecked_Conversion);
5031 -- Loop to check enclosing scopes
5033 Enclosing_Instance := Current_Scope;
5034 while Present (Enclosing_Instance) loop
5035 Instance_Decl := Unit_Declaration_Node (Enclosing_Instance);
5037 if Ekind (Enclosing_Instance) = E_Package
5038 and then Is_Generic_Instance (Enclosing_Instance)
5040 (Generic_Parent (Specification (Instance_Decl)))
5042 -- Check whether the generic we are looking for is a child of
5045 E := Find_Generic_Child
5046 (Generic_Parent (Specification (Instance_Decl)), Gen_Id);
5047 exit when Present (E);
5053 Enclosing_Instance := Scope (Enclosing_Instance);
5065 Make_Expanded_Name (Loc,
5067 Prefix => New_Occurrence_Of (Enclosing_Instance, Loc),
5068 Selector_Name => New_Occurrence_Of (E, Loc)));
5070 Set_Entity (Gen_Id, E);
5071 Set_Etype (Gen_Id, Etype (E));
5072 Parent_Installed := False; -- Already in scope.
5075 end In_Enclosing_Instance;
5077 -- Start of processing for Check_Generic_Child_Unit
5080 -- If the name of the generic is given by a selected component, it may
5081 -- be the name of a generic child unit, and the prefix is the name of an
5082 -- instance of the parent, in which case the child unit must be visible.
5083 -- If this instance is not in scope, it must be placed there and removed
5084 -- after instantiation, because what is being instantiated is not the
5085 -- original child, but the corresponding child present in the instance
5088 -- If the child is instantiated within the parent, it can be given by
5089 -- a simple name. In this case the instance is already in scope, but
5090 -- the child generic must be recovered from the generic parent as well.
5092 if Nkind (Gen_Id) = N_Selected_Component then
5093 S := Selector_Name (Gen_Id);
5094 Analyze (Prefix (Gen_Id));
5095 Inst_Par := Entity (Prefix (Gen_Id));
5097 if Ekind (Inst_Par) = E_Package
5098 and then Present (Renamed_Object (Inst_Par))
5100 Inst_Par := Renamed_Object (Inst_Par);
5103 if Ekind (Inst_Par) = E_Package then
5104 if Nkind (Parent (Inst_Par)) = N_Package_Specification then
5105 Gen_Par := Generic_Parent (Parent (Inst_Par));
5107 elsif Nkind (Parent (Inst_Par)) = N_Defining_Program_Unit_Name
5109 Nkind (Parent (Parent (Inst_Par))) = N_Package_Specification
5111 Gen_Par := Generic_Parent (Parent (Parent (Inst_Par)));
5114 elsif Ekind (Inst_Par) = E_Generic_Package
5115 and then Nkind (Parent (Gen_Id)) = N_Formal_Package_Declaration
5117 -- A formal package may be a real child package, and not the
5118 -- implicit instance within a parent. In this case the child is
5119 -- not visible and has to be retrieved explicitly as well.
5121 Gen_Par := Inst_Par;
5124 if Present (Gen_Par) then
5126 -- The prefix denotes an instantiation. The entity itself may be a
5127 -- nested generic, or a child unit.
5129 E := Find_Generic_Child (Gen_Par, S);
5132 Change_Selected_Component_To_Expanded_Name (Gen_Id);
5133 Set_Entity (Gen_Id, E);
5134 Set_Etype (Gen_Id, Etype (E));
5136 Set_Etype (S, Etype (E));
5138 -- Indicate that this is a reference to the parent
5140 if In_Extended_Main_Source_Unit (Gen_Id) then
5141 Set_Is_Instantiated (Inst_Par);
5144 -- A common mistake is to replicate the naming scheme of a
5145 -- hierarchy by instantiating a generic child directly, rather
5146 -- than the implicit child in a parent instance:
5148 -- generic .. package Gpar is ..
5149 -- generic .. package Gpar.Child is ..
5150 -- package Par is new Gpar ();
5153 -- package Par.Child is new Gpar.Child ();
5154 -- rather than Par.Child
5156 -- In this case the instantiation is within Par, which is an
5157 -- instance, but Gpar does not denote Par because we are not IN
5158 -- the instance of Gpar, so this is illegal. The test below
5159 -- recognizes this particular case.
5161 if Is_Child_Unit (E)
5162 and then not Comes_From_Source (Entity (Prefix (Gen_Id)))
5163 and then (not In_Instance
5164 or else Nkind (Parent (Parent (Gen_Id))) =
5168 ("prefix of generic child unit must be instance of parent",
5172 if not In_Open_Scopes (Inst_Par)
5173 and then Nkind (Parent (Gen_Id)) not in
5174 N_Generic_Renaming_Declaration
5176 Install_Parent (Inst_Par);
5177 Parent_Installed := True;
5179 elsif In_Open_Scopes (Inst_Par) then
5181 -- If the parent is already installed verify that the
5182 -- actuals for its formal packages declared with a box
5183 -- are already installed. This is necessary when the
5184 -- child instance is a child of the parent instance.
5185 -- In this case the parent is placed on the scope stack
5186 -- but the formal packages are not made visible.
5188 Install_Formal_Packages (Inst_Par);
5192 -- If the generic parent does not contain an entity that
5193 -- corresponds to the selector, the instance doesn't either.
5194 -- Analyzing the node will yield the appropriate error message.
5195 -- If the entity is not a child unit, then it is an inner
5196 -- generic in the parent.
5204 if Is_Child_Unit (Entity (Gen_Id))
5206 Nkind (Parent (Gen_Id)) not in N_Generic_Renaming_Declaration
5207 and then not In_Open_Scopes (Inst_Par)
5209 Install_Parent (Inst_Par);
5210 Parent_Installed := True;
5214 elsif Nkind (Gen_Id) = N_Expanded_Name then
5216 -- Entity already present, analyze prefix, whose meaning may be
5217 -- an instance in the current context. If it is an instance of
5218 -- a relative within another, the proper parent may still have
5219 -- to be installed, if they are not of the same generation.
5221 Analyze (Prefix (Gen_Id));
5223 -- In the unlikely case that a local declaration hides the name
5224 -- of the parent package, locate it on the homonym chain. If the
5225 -- context is an instance of the parent, the renaming entity is
5228 Inst_Par := Entity (Prefix (Gen_Id));
5229 while Present (Inst_Par)
5230 and then not Is_Package_Or_Generic_Package (Inst_Par)
5232 Inst_Par := Homonym (Inst_Par);
5235 pragma Assert (Present (Inst_Par));
5236 Set_Entity (Prefix (Gen_Id), Inst_Par);
5238 if In_Enclosing_Instance then
5241 elsif Present (Entity (Gen_Id))
5242 and then Is_Child_Unit (Entity (Gen_Id))
5243 and then not In_Open_Scopes (Inst_Par)
5245 Install_Parent (Inst_Par);
5246 Parent_Installed := True;
5249 elsif In_Enclosing_Instance then
5251 -- The child unit is found in some enclosing scope
5258 -- If this is the renaming of the implicit child in a parent
5259 -- instance, recover the parent name and install it.
5261 if Is_Entity_Name (Gen_Id) then
5262 E := Entity (Gen_Id);
5264 if Is_Generic_Unit (E)
5265 and then Nkind (Parent (E)) in N_Generic_Renaming_Declaration
5266 and then Is_Child_Unit (Renamed_Object (E))
5267 and then Is_Generic_Unit (Scope (Renamed_Object (E)))
5268 and then Nkind (Name (Parent (E))) = N_Expanded_Name
5271 New_Copy_Tree (Name (Parent (E))));
5272 Inst_Par := Entity (Prefix (Gen_Id));
5274 if not In_Open_Scopes (Inst_Par) then
5275 Install_Parent (Inst_Par);
5276 Parent_Installed := True;
5279 -- If it is a child unit of a non-generic parent, it may be
5280 -- use-visible and given by a direct name. Install parent as
5283 elsif Is_Generic_Unit (E)
5284 and then Is_Child_Unit (E)
5286 Nkind (Parent (Gen_Id)) not in N_Generic_Renaming_Declaration
5287 and then not Is_Generic_Unit (Scope (E))
5289 if not In_Open_Scopes (Scope (E)) then
5290 Install_Parent (Scope (E));
5291 Parent_Installed := True;
5296 end Check_Generic_Child_Unit;
5298 -----------------------------
5299 -- Check_Hidden_Child_Unit --
5300 -----------------------------
5302 procedure Check_Hidden_Child_Unit
5304 Gen_Unit : Entity_Id;
5305 Act_Decl_Id : Entity_Id)
5307 Gen_Id : constant Node_Id := Name (N);
5310 if Is_Child_Unit (Gen_Unit)
5311 and then Is_Child_Unit (Act_Decl_Id)
5312 and then Nkind (Gen_Id) = N_Expanded_Name
5313 and then Entity (Prefix (Gen_Id)) = Scope (Act_Decl_Id)
5314 and then Chars (Gen_Unit) = Chars (Act_Decl_Id)
5316 Error_Msg_Node_2 := Scope (Act_Decl_Id);
5318 ("generic unit & is implicitly declared in &",
5319 Defining_Unit_Name (N), Gen_Unit);
5320 Error_Msg_N ("\instance must have different name",
5321 Defining_Unit_Name (N));
5323 end Check_Hidden_Child_Unit;
5325 ------------------------
5326 -- Check_Private_View --
5327 ------------------------
5329 procedure Check_Private_View (N : Node_Id) is
5330 T : constant Entity_Id := Etype (N);
5334 -- Exchange views if the type was not private in the generic but is
5335 -- private at the point of instantiation. Do not exchange views if
5336 -- the scope of the type is in scope. This can happen if both generic
5337 -- and instance are sibling units, or if type is defined in a parent.
5338 -- In this case the visibility of the type will be correct for all
5342 BT := Base_Type (T);
5344 if Is_Private_Type (T)
5345 and then not Has_Private_View (N)
5346 and then Present (Full_View (T))
5347 and then not In_Open_Scopes (Scope (T))
5349 -- In the generic, the full type was visible. Save the private
5350 -- entity, for subsequent exchange.
5354 elsif Has_Private_View (N)
5355 and then not Is_Private_Type (T)
5356 and then not Has_Been_Exchanged (T)
5357 and then Etype (Get_Associated_Node (N)) /= T
5359 -- Only the private declaration was visible in the generic. If
5360 -- the type appears in a subtype declaration, the subtype in the
5361 -- instance must have a view compatible with that of its parent,
5362 -- which must be exchanged (see corresponding code in Restore_
5363 -- Private_Views). Otherwise, if the type is defined in a parent
5364 -- unit, leave full visibility within instance, which is safe.
5366 if In_Open_Scopes (Scope (Base_Type (T)))
5367 and then not Is_Private_Type (Base_Type (T))
5368 and then Comes_From_Source (Base_Type (T))
5372 elsif Nkind (Parent (N)) = N_Subtype_Declaration
5373 or else not In_Private_Part (Scope (Base_Type (T)))
5375 Prepend_Elmt (T, Exchanged_Views);
5376 Exchange_Declarations (Etype (Get_Associated_Node (N)));
5379 -- For composite types with inconsistent representation exchange
5380 -- component types accordingly.
5382 elsif Is_Access_Type (T)
5383 and then Is_Private_Type (Designated_Type (T))
5384 and then not Has_Private_View (N)
5385 and then Present (Full_View (Designated_Type (T)))
5387 Switch_View (Designated_Type (T));
5389 elsif Is_Array_Type (T) then
5390 if Is_Private_Type (Component_Type (T))
5391 and then not Has_Private_View (N)
5392 and then Present (Full_View (Component_Type (T)))
5394 Switch_View (Component_Type (T));
5397 -- The normal exchange mechanism relies on the setting of a
5398 -- flag on the reference in the generic. However, an additional
5399 -- mechanism is needed for types that are not explicitly mentioned
5400 -- in the generic, but may be needed in expanded code in the
5401 -- instance. This includes component types of arrays and
5402 -- designated types of access types. This processing must also
5403 -- include the index types of arrays which we take care of here.
5410 Indx := First_Index (T);
5411 Typ := Base_Type (Etype (Indx));
5412 while Present (Indx) loop
5413 if Is_Private_Type (Typ)
5414 and then Present (Full_View (Typ))
5423 elsif Is_Private_Type (T)
5424 and then Present (Full_View (T))
5425 and then Is_Array_Type (Full_View (T))
5426 and then Is_Private_Type (Component_Type (Full_View (T)))
5430 -- Finally, a non-private subtype may have a private base type, which
5431 -- must be exchanged for consistency. This can happen when a package
5432 -- body is instantiated, when the scope stack is empty but in fact
5433 -- the subtype and the base type are declared in an enclosing scope.
5435 -- Note that in this case we introduce an inconsistency in the view
5436 -- set, because we switch the base type BT, but there could be some
5437 -- private dependent subtypes of BT which remain unswitched. Such
5438 -- subtypes might need to be switched at a later point (see specific
5439 -- provision for that case in Switch_View).
5441 elsif not Is_Private_Type (T)
5442 and then not Has_Private_View (N)
5443 and then Is_Private_Type (BT)
5444 and then Present (Full_View (BT))
5445 and then not Is_Generic_Type (BT)
5446 and then not In_Open_Scopes (BT)
5448 Prepend_Elmt (Full_View (BT), Exchanged_Views);
5449 Exchange_Declarations (BT);
5452 end Check_Private_View;
5454 --------------------------
5455 -- Contains_Instance_Of --
5456 --------------------------
5458 function Contains_Instance_Of
5461 N : Node_Id) return Boolean
5469 -- Verify that there are no circular instantiations. We check whether
5470 -- the unit contains an instance of the current scope or some enclosing
5471 -- scope (in case one of the instances appears in a subunit). Longer
5472 -- circularities involving subunits might seem too pathological to
5473 -- consider, but they were not too pathological for the authors of
5474 -- DEC bc30vsq, so we loop over all enclosing scopes, and mark all
5475 -- enclosing generic scopes as containing an instance.
5478 -- Within a generic subprogram body, the scope is not generic, to
5479 -- allow for recursive subprograms. Use the declaration to determine
5480 -- whether this is a generic unit.
5482 if Ekind (Scop) = E_Generic_Package
5483 or else (Is_Subprogram (Scop)
5484 and then Nkind (Unit_Declaration_Node (Scop)) =
5485 N_Generic_Subprogram_Declaration)
5487 Elmt := First_Elmt (Inner_Instances (Inner));
5489 while Present (Elmt) loop
5490 if Node (Elmt) = Scop then
5491 Error_Msg_Node_2 := Inner;
5493 ("circular Instantiation: & instantiated within &!",
5497 elsif Node (Elmt) = Inner then
5500 elsif Contains_Instance_Of (Node (Elmt), Scop, N) then
5501 Error_Msg_Node_2 := Inner;
5503 ("circular Instantiation: & instantiated within &!",
5511 -- Indicate that Inner is being instantiated within Scop
5513 Append_Elmt (Inner, Inner_Instances (Scop));
5516 if Scop = Standard_Standard then
5519 Scop := Scope (Scop);
5524 end Contains_Instance_Of;
5526 -----------------------
5527 -- Copy_Generic_Node --
5528 -----------------------
5530 function Copy_Generic_Node
5532 Parent_Id : Node_Id;
5533 Instantiating : Boolean) return Node_Id
5538 function Copy_Generic_Descendant (D : Union_Id) return Union_Id;
5539 -- Check the given value of one of the Fields referenced by the
5540 -- current node to determine whether to copy it recursively. The
5541 -- field may hold a Node_Id, a List_Id, or an Elist_Id, or a plain
5542 -- value (Sloc, Uint, Char) in which case it need not be copied.
5544 procedure Copy_Descendants;
5545 -- Common utility for various nodes
5547 function Copy_Generic_Elist (E : Elist_Id) return Elist_Id;
5548 -- Make copy of element list
5550 function Copy_Generic_List
5552 Parent_Id : Node_Id) return List_Id;
5553 -- Apply Copy_Node recursively to the members of a node list
5555 function In_Defining_Unit_Name (Nam : Node_Id) return Boolean;
5556 -- True if an identifier is part of the defining program unit name
5557 -- of a child unit. The entity of such an identifier must be kept
5558 -- (for ASIS use) even though as the name of an enclosing generic
5559 -- it would otherwise not be preserved in the generic tree.
5561 ----------------------
5562 -- Copy_Descendants --
5563 ----------------------
5565 procedure Copy_Descendants is
5567 use Atree.Unchecked_Access;
5568 -- This code section is part of the implementation of an untyped
5569 -- tree traversal, so it needs direct access to node fields.
5572 Set_Field1 (New_N, Copy_Generic_Descendant (Field1 (N)));
5573 Set_Field2 (New_N, Copy_Generic_Descendant (Field2 (N)));
5574 Set_Field3 (New_N, Copy_Generic_Descendant (Field3 (N)));
5575 Set_Field4 (New_N, Copy_Generic_Descendant (Field4 (N)));
5576 Set_Field5 (New_N, Copy_Generic_Descendant (Field5 (N)));
5577 end Copy_Descendants;
5579 -----------------------------
5580 -- Copy_Generic_Descendant --
5581 -----------------------------
5583 function Copy_Generic_Descendant (D : Union_Id) return Union_Id is
5585 if D = Union_Id (Empty) then
5588 elsif D in Node_Range then
5590 (Copy_Generic_Node (Node_Id (D), New_N, Instantiating));
5592 elsif D in List_Range then
5593 return Union_Id (Copy_Generic_List (List_Id (D), New_N));
5595 elsif D in Elist_Range then
5596 return Union_Id (Copy_Generic_Elist (Elist_Id (D)));
5598 -- Nothing else is copyable (e.g. Uint values), return as is
5603 end Copy_Generic_Descendant;
5605 ------------------------
5606 -- Copy_Generic_Elist --
5607 ------------------------
5609 function Copy_Generic_Elist (E : Elist_Id) return Elist_Id is
5616 M := First_Elmt (E);
5617 while Present (M) loop
5619 (Copy_Generic_Node (Node (M), Empty, Instantiating), L);
5628 end Copy_Generic_Elist;
5630 -----------------------
5631 -- Copy_Generic_List --
5632 -----------------------
5634 function Copy_Generic_List
5636 Parent_Id : Node_Id) return List_Id
5644 Set_Parent (New_L, Parent_Id);
5647 while Present (N) loop
5648 Append (Copy_Generic_Node (N, Empty, Instantiating), New_L);
5657 end Copy_Generic_List;
5659 ---------------------------
5660 -- In_Defining_Unit_Name --
5661 ---------------------------
5663 function In_Defining_Unit_Name (Nam : Node_Id) return Boolean is
5665 return Present (Parent (Nam))
5666 and then (Nkind (Parent (Nam)) = N_Defining_Program_Unit_Name
5668 (Nkind (Parent (Nam)) = N_Expanded_Name
5669 and then In_Defining_Unit_Name (Parent (Nam))));
5670 end In_Defining_Unit_Name;
5672 -- Start of processing for Copy_Generic_Node
5679 New_N := New_Copy (N);
5681 if Instantiating then
5682 Adjust_Instantiation_Sloc (New_N, S_Adjustment);
5685 if not Is_List_Member (N) then
5686 Set_Parent (New_N, Parent_Id);
5689 -- If defining identifier, then all fields have been copied already
5691 if Nkind (New_N) in N_Entity then
5694 -- Special casing for identifiers and other entity names and operators
5696 elsif Nkind_In (New_N, N_Identifier,
5697 N_Character_Literal,
5700 or else Nkind (New_N) in N_Op
5702 if not Instantiating then
5704 -- Link both nodes in order to assign subsequently the
5705 -- entity of the copy to the original node, in case this
5706 -- is a global reference.
5708 Set_Associated_Node (N, New_N);
5710 -- If we are within an instantiation, this is a nested generic
5711 -- that has already been analyzed at the point of definition. We
5712 -- must preserve references that were global to the enclosing
5713 -- parent at that point. Other occurrences, whether global or
5714 -- local to the current generic, must be resolved anew, so we
5715 -- reset the entity in the generic copy. A global reference has a
5716 -- smaller depth than the parent, or else the same depth in case
5717 -- both are distinct compilation units.
5718 -- A child unit is implicitly declared within the enclosing parent
5719 -- but is in fact global to it, and must be preserved.
5721 -- It is also possible for Current_Instantiated_Parent to be
5722 -- defined, and for this not to be a nested generic, namely if the
5723 -- unit is loaded through Rtsfind. In that case, the entity of
5724 -- New_N is only a link to the associated node, and not a defining
5727 -- The entities for parent units in the defining_program_unit of a
5728 -- generic child unit are established when the context of the unit
5729 -- is first analyzed, before the generic copy is made. They are
5730 -- preserved in the copy for use in ASIS queries.
5732 Ent := Entity (New_N);
5734 if No (Current_Instantiated_Parent.Gen_Id) then
5736 or else Nkind (Ent) /= N_Defining_Identifier
5737 or else not In_Defining_Unit_Name (N)
5739 Set_Associated_Node (New_N, Empty);
5744 not Nkind_In (Ent, N_Defining_Identifier,
5745 N_Defining_Character_Literal,
5746 N_Defining_Operator_Symbol)
5747 or else No (Scope (Ent))
5749 (Scope (Ent) = Current_Instantiated_Parent.Gen_Id
5750 and then not Is_Child_Unit (Ent))
5752 (Scope_Depth (Scope (Ent)) >
5753 Scope_Depth (Current_Instantiated_Parent.Gen_Id)
5755 Get_Source_Unit (Ent) =
5756 Get_Source_Unit (Current_Instantiated_Parent.Gen_Id))
5758 Set_Associated_Node (New_N, Empty);
5761 -- Case of instantiating identifier or some other name or operator
5764 -- If the associated node is still defined, the entity in it is
5765 -- global, and must be copied to the instance. If this copy is
5766 -- being made for a body to inline, it is applied to an
5767 -- instantiated tree, and the entity is already present and must
5768 -- be also preserved.
5771 Assoc : constant Node_Id := Get_Associated_Node (N);
5774 if Present (Assoc) then
5775 if Nkind (Assoc) = Nkind (N) then
5776 Set_Entity (New_N, Entity (Assoc));
5777 Check_Private_View (N);
5779 elsif Nkind (Assoc) = N_Function_Call then
5780 Set_Entity (New_N, Entity (Name (Assoc)));
5782 elsif Nkind_In (Assoc, N_Defining_Identifier,
5783 N_Defining_Character_Literal,
5784 N_Defining_Operator_Symbol)
5785 and then Expander_Active
5787 -- Inlining case: we are copying a tree that contains
5788 -- global entities, which are preserved in the copy to be
5789 -- used for subsequent inlining.
5794 Set_Entity (New_N, Empty);
5800 -- For expanded name, we must copy the Prefix and Selector_Name
5802 if Nkind (N) = N_Expanded_Name then
5804 (New_N, Copy_Generic_Node (Prefix (N), New_N, Instantiating));
5806 Set_Selector_Name (New_N,
5807 Copy_Generic_Node (Selector_Name (N), New_N, Instantiating));
5809 -- For operators, we must copy the right operand
5811 elsif Nkind (N) in N_Op then
5812 Set_Right_Opnd (New_N,
5813 Copy_Generic_Node (Right_Opnd (N), New_N, Instantiating));
5815 -- And for binary operators, the left operand as well
5817 if Nkind (N) in N_Binary_Op then
5818 Set_Left_Opnd (New_N,
5819 Copy_Generic_Node (Left_Opnd (N), New_N, Instantiating));
5823 -- Special casing for stubs
5825 elsif Nkind (N) in N_Body_Stub then
5827 -- In any case, we must copy the specification or defining
5828 -- identifier as appropriate.
5830 if Nkind (N) = N_Subprogram_Body_Stub then
5831 Set_Specification (New_N,
5832 Copy_Generic_Node (Specification (N), New_N, Instantiating));
5835 Set_Defining_Identifier (New_N,
5837 (Defining_Identifier (N), New_N, Instantiating));
5840 -- If we are not instantiating, then this is where we load and
5841 -- analyze subunits, i.e. at the point where the stub occurs. A
5842 -- more permissible system might defer this analysis to the point
5843 -- of instantiation, but this seems to complicated for now.
5845 if not Instantiating then
5847 Subunit_Name : constant Unit_Name_Type := Get_Unit_Name (N);
5849 Unum : Unit_Number_Type;
5855 (Load_Name => Subunit_Name,
5860 -- If the proper body is not found, a warning message will be
5861 -- emitted when analyzing the stub, or later at the point
5862 -- of instantiation. Here we just leave the stub as is.
5864 if Unum = No_Unit then
5865 Subunits_Missing := True;
5866 goto Subunit_Not_Found;
5869 Subunit := Cunit (Unum);
5871 if Nkind (Unit (Subunit)) /= N_Subunit then
5873 ("found child unit instead of expected SEPARATE subunit",
5875 Error_Msg_Sloc := Sloc (N);
5876 Error_Msg_N ("\to complete stub #", Subunit);
5877 goto Subunit_Not_Found;
5880 -- We must create a generic copy of the subunit, in order to
5881 -- perform semantic analysis on it, and we must replace the
5882 -- stub in the original generic unit with the subunit, in order
5883 -- to preserve non-local references within.
5885 -- Only the proper body needs to be copied. Library_Unit and
5886 -- context clause are simply inherited by the generic copy.
5887 -- Note that the copy (which may be recursive if there are
5888 -- nested subunits) must be done first, before attaching it to
5889 -- the enclosing generic.
5893 (Proper_Body (Unit (Subunit)),
5894 Empty, Instantiating => False);
5896 -- Now place the original proper body in the original generic
5897 -- unit. This is a body, not a compilation unit.
5899 Rewrite (N, Proper_Body (Unit (Subunit)));
5900 Set_Is_Compilation_Unit (Defining_Entity (N), False);
5901 Set_Was_Originally_Stub (N);
5903 -- Finally replace the body of the subunit with its copy, and
5904 -- make this new subunit into the library unit of the generic
5905 -- copy, which does not have stubs any longer.
5907 Set_Proper_Body (Unit (Subunit), New_Body);
5908 Set_Library_Unit (New_N, Subunit);
5909 Inherit_Context (Unit (Subunit), N);
5912 -- If we are instantiating, this must be an error case, since
5913 -- otherwise we would have replaced the stub node by the proper body
5914 -- that corresponds. So just ignore it in the copy (i.e. we have
5915 -- copied it, and that is good enough).
5921 <<Subunit_Not_Found>> null;
5923 -- If the node is a compilation unit, it is the subunit of a stub, which
5924 -- has been loaded already (see code below). In this case, the library
5925 -- unit field of N points to the parent unit (which is a compilation
5926 -- unit) and need not (and cannot!) be copied.
5928 -- When the proper body of the stub is analyzed, the library_unit link
5929 -- is used to establish the proper context (see sem_ch10).
5931 -- The other fields of a compilation unit are copied as usual
5933 elsif Nkind (N) = N_Compilation_Unit then
5935 -- This code can only be executed when not instantiating, because in
5936 -- the copy made for an instantiation, the compilation unit node has
5937 -- disappeared at the point that a stub is replaced by its proper
5940 pragma Assert (not Instantiating);
5942 Set_Context_Items (New_N,
5943 Copy_Generic_List (Context_Items (N), New_N));
5946 Copy_Generic_Node (Unit (N), New_N, False));
5948 Set_First_Inlined_Subprogram (New_N,
5950 (First_Inlined_Subprogram (N), New_N, False));
5952 Set_Aux_Decls_Node (New_N,
5953 Copy_Generic_Node (Aux_Decls_Node (N), New_N, False));
5955 -- For an assignment node, the assignment is known to be semantically
5956 -- legal if we are instantiating the template. This avoids incorrect
5957 -- diagnostics in generated code.
5959 elsif Nkind (N) = N_Assignment_Statement then
5961 -- Copy name and expression fields in usual manner
5964 Copy_Generic_Node (Name (N), New_N, Instantiating));
5966 Set_Expression (New_N,
5967 Copy_Generic_Node (Expression (N), New_N, Instantiating));
5969 if Instantiating then
5970 Set_Assignment_OK (Name (New_N), True);
5973 elsif Nkind_In (N, N_Aggregate, N_Extension_Aggregate) then
5974 if not Instantiating then
5975 Set_Associated_Node (N, New_N);
5978 if Present (Get_Associated_Node (N))
5979 and then Nkind (Get_Associated_Node (N)) = Nkind (N)
5981 -- In the generic the aggregate has some composite type. If at
5982 -- the point of instantiation the type has a private view,
5983 -- install the full view (and that of its ancestors, if any).
5986 T : Entity_Id := (Etype (Get_Associated_Node (New_N)));
5991 and then Is_Private_Type (T)
5997 and then Is_Tagged_Type (T)
5998 and then Is_Derived_Type (T)
6000 Rt := Root_Type (T);
6005 if Is_Private_Type (T) then
6016 -- Do not copy the associated node, which points to
6017 -- the generic copy of the aggregate.
6020 use Atree.Unchecked_Access;
6021 -- This code section is part of the implementation of an untyped
6022 -- tree traversal, so it needs direct access to node fields.
6025 Set_Field1 (New_N, Copy_Generic_Descendant (Field1 (N)));
6026 Set_Field2 (New_N, Copy_Generic_Descendant (Field2 (N)));
6027 Set_Field3 (New_N, Copy_Generic_Descendant (Field3 (N)));
6028 Set_Field5 (New_N, Copy_Generic_Descendant (Field5 (N)));
6031 -- Allocators do not have an identifier denoting the access type,
6032 -- so we must locate it through the expression to check whether
6033 -- the views are consistent.
6035 elsif Nkind (N) = N_Allocator
6036 and then Nkind (Expression (N)) = N_Qualified_Expression
6037 and then Is_Entity_Name (Subtype_Mark (Expression (N)))
6038 and then Instantiating
6041 T : constant Node_Id :=
6042 Get_Associated_Node (Subtype_Mark (Expression (N)));
6048 -- Retrieve the allocator node in the generic copy
6050 Acc_T := Etype (Parent (Parent (T)));
6052 and then Is_Private_Type (Acc_T)
6054 Switch_View (Acc_T);
6061 -- For a proper body, we must catch the case of a proper body that
6062 -- replaces a stub. This represents the point at which a separate
6063 -- compilation unit, and hence template file, may be referenced, so we
6064 -- must make a new source instantiation entry for the template of the
6065 -- subunit, and ensure that all nodes in the subunit are adjusted using
6066 -- this new source instantiation entry.
6068 elsif Nkind (N) in N_Proper_Body then
6070 Save_Adjustment : constant Sloc_Adjustment := S_Adjustment;
6073 if Instantiating and then Was_Originally_Stub (N) then
6074 Create_Instantiation_Source
6075 (Instantiation_Node,
6076 Defining_Entity (N),
6081 -- Now copy the fields of the proper body, using the new
6082 -- adjustment factor if one was needed as per test above.
6086 -- Restore the original adjustment factor in case changed
6088 S_Adjustment := Save_Adjustment;
6091 -- Don't copy Ident or Comment pragmas, since the comment belongs to the
6092 -- generic unit, not to the instantiating unit.
6094 elsif Nkind (N) = N_Pragma
6095 and then Instantiating
6098 Prag_Id : constant Pragma_Id := Get_Pragma_Id (N);
6100 if Prag_Id = Pragma_Ident
6101 or else Prag_Id = Pragma_Comment
6103 New_N := Make_Null_Statement (Sloc (N));
6109 elsif Nkind_In (N, N_Integer_Literal,
6113 -- No descendant fields need traversing
6117 -- For the remaining nodes, copy recursively their descendants
6123 and then Nkind (N) = N_Subprogram_Body
6125 Set_Generic_Parent (Specification (New_N), N);
6130 end Copy_Generic_Node;
6132 ----------------------------
6133 -- Denotes_Formal_Package --
6134 ----------------------------
6136 function Denotes_Formal_Package
6138 On_Exit : Boolean := False;
6139 Instance : Entity_Id := Empty) return Boolean
6142 Scop : constant Entity_Id := Scope (Pack);
6145 function Is_Actual_Of_Previous_Formal (P : Entity_Id) return Boolean;
6146 -- The package in question may be an actual for a previous formal
6147 -- package P of the current instance, so examine its actuals as well.
6149 ----------------------------------
6150 -- Is_Actual_Of_Previous_Formal --
6151 ----------------------------------
6153 function Is_Actual_Of_Previous_Formal (P : Entity_Id) return Boolean is
6157 E1 := First_Entity (E);
6158 while Present (E1) and then E1 /= Instance loop
6159 if Ekind (E1) = E_Package
6160 and then Nkind (Parent (E1)) = N_Package_Renaming_Declaration
6161 and then Renamed_Object (E1) = Pack
6165 elsif Renamed_Object (E1) = P then
6173 end Is_Actual_Of_Previous_Formal;
6175 -- Start processing of Denotes_Formal_Package
6181 (Instance_Envs.Last).Instantiated_Parent.Act_Id;
6183 Par := Current_Instantiated_Parent.Act_Id;
6186 if Ekind (Scop) = E_Generic_Package
6187 or else Nkind (Unit_Declaration_Node (Scop)) =
6188 N_Generic_Subprogram_Declaration
6192 elsif Nkind (Original_Node (Unit_Declaration_Node (Pack))) =
6193 N_Formal_Package_Declaration
6201 -- Check whether this package is associated with a formal package of
6202 -- the enclosing instantiation. Iterate over the list of renamings.
6204 E := First_Entity (Par);
6205 while Present (E) loop
6206 if Ekind (E) /= E_Package
6207 or else Nkind (Parent (E)) /= N_Package_Renaming_Declaration
6211 elsif Renamed_Object (E) = Par then
6214 elsif Renamed_Object (E) = Pack then
6217 elsif Is_Actual_Of_Previous_Formal (E) then
6227 end Denotes_Formal_Package;
6233 procedure End_Generic is
6235 -- ??? More things could be factored out in this routine. Should
6236 -- probably be done at a later stage.
6238 Inside_A_Generic := Generic_Flags.Table (Generic_Flags.Last);
6239 Generic_Flags.Decrement_Last;
6241 Expander_Mode_Restore;
6244 ----------------------
6245 -- Find_Actual_Type --
6246 ----------------------
6248 function Find_Actual_Type
6250 Gen_Type : Entity_Id) return Entity_Id
6252 Gen_Scope : constant Entity_Id := Scope (Gen_Type);
6256 -- Special processing only applies to child units
6258 if not Is_Child_Unit (Gen_Scope) then
6259 return Get_Instance_Of (Typ);
6261 -- If designated or component type is itself a formal of the child unit,
6262 -- its instance is available.
6264 elsif Scope (Typ) = Gen_Scope then
6265 return Get_Instance_Of (Typ);
6267 -- If the array or access type is not declared in the parent unit,
6268 -- no special processing needed.
6270 elsif not Is_Generic_Type (Typ)
6271 and then Scope (Gen_Scope) /= Scope (Typ)
6273 return Get_Instance_Of (Typ);
6275 -- Otherwise, retrieve designated or component type by visibility
6278 T := Current_Entity (Typ);
6279 while Present (T) loop
6280 if In_Open_Scopes (Scope (T)) then
6283 elsif Is_Generic_Actual_Type (T) then
6292 end Find_Actual_Type;
6294 ----------------------------
6295 -- Freeze_Subprogram_Body --
6296 ----------------------------
6298 procedure Freeze_Subprogram_Body
6299 (Inst_Node : Node_Id;
6301 Pack_Id : Entity_Id)
6304 Gen_Unit : constant Entity_Id := Get_Generic_Entity (Inst_Node);
6305 Par : constant Entity_Id := Scope (Gen_Unit);
6310 function Earlier (N1, N2 : Node_Id) return Boolean;
6311 -- Yields True if N1 and N2 appear in the same compilation unit,
6312 -- ignoring subunits, and if N1 is to the left of N2 in a left-to-right
6313 -- traversal of the tree for the unit.
6315 function Enclosing_Body (N : Node_Id) return Node_Id;
6316 -- Find innermost package body that encloses the given node, and which
6317 -- is not a compilation unit. Freeze nodes for the instance, or for its
6318 -- enclosing body, may be inserted after the enclosing_body of the
6321 function Package_Freeze_Node (B : Node_Id) return Node_Id;
6322 -- Find entity for given package body, and locate or create a freeze
6325 function True_Parent (N : Node_Id) return Node_Id;
6326 -- For a subunit, return parent of corresponding stub
6332 function Earlier (N1, N2 : Node_Id) return Boolean is
6338 procedure Find_Depth (P : in out Node_Id; D : in out Integer);
6339 -- Find distance from given node to enclosing compilation unit
6345 procedure Find_Depth (P : in out Node_Id; D : in out Integer) is
6348 and then Nkind (P) /= N_Compilation_Unit
6350 P := True_Parent (P);
6355 -- Start of processing for Earlier
6358 Find_Depth (P1, D1);
6359 Find_Depth (P2, D2);
6369 P1 := True_Parent (P1);
6374 P2 := True_Parent (P2);
6378 -- At this point P1 and P2 are at the same distance from the root.
6379 -- We examine their parents until we find a common declarative
6380 -- list, at which point we can establish their relative placement
6381 -- by comparing their ultimate slocs. If we reach the root,
6382 -- N1 and N2 do not descend from the same declarative list (e.g.
6383 -- one is nested in the declarative part and the other is in a block
6384 -- in the statement part) and the earlier one is already frozen.
6386 while not Is_List_Member (P1)
6387 or else not Is_List_Member (P2)
6388 or else List_Containing (P1) /= List_Containing (P2)
6390 P1 := True_Parent (P1);
6391 P2 := True_Parent (P2);
6393 if Nkind (Parent (P1)) = N_Subunit then
6394 P1 := Corresponding_Stub (Parent (P1));
6397 if Nkind (Parent (P2)) = N_Subunit then
6398 P2 := Corresponding_Stub (Parent (P2));
6407 Top_Level_Location (Sloc (P1)) < Top_Level_Location (Sloc (P2));
6410 --------------------
6411 -- Enclosing_Body --
6412 --------------------
6414 function Enclosing_Body (N : Node_Id) return Node_Id is
6415 P : Node_Id := Parent (N);
6419 and then Nkind (Parent (P)) /= N_Compilation_Unit
6421 if Nkind (P) = N_Package_Body then
6423 if Nkind (Parent (P)) = N_Subunit then
6424 return Corresponding_Stub (Parent (P));
6430 P := True_Parent (P);
6436 -------------------------
6437 -- Package_Freeze_Node --
6438 -------------------------
6440 function Package_Freeze_Node (B : Node_Id) return Node_Id is
6444 if Nkind (B) = N_Package_Body then
6445 Id := Corresponding_Spec (B);
6447 else pragma Assert (Nkind (B) = N_Package_Body_Stub);
6448 Id := Corresponding_Spec (Proper_Body (Unit (Library_Unit (B))));
6451 Ensure_Freeze_Node (Id);
6452 return Freeze_Node (Id);
6453 end Package_Freeze_Node;
6459 function True_Parent (N : Node_Id) return Node_Id is
6461 if Nkind (Parent (N)) = N_Subunit then
6462 return Parent (Corresponding_Stub (Parent (N)));
6468 -- Start of processing of Freeze_Subprogram_Body
6471 -- If the instance and the generic body appear within the same unit, and
6472 -- the instance precedes the generic, the freeze node for the instance
6473 -- must appear after that of the generic. If the generic is nested
6474 -- within another instance I2, then current instance must be frozen
6475 -- after I2. In both cases, the freeze nodes are those of enclosing
6476 -- packages. Otherwise, the freeze node is placed at the end of the
6477 -- current declarative part.
6479 Enc_G := Enclosing_Body (Gen_Body);
6480 Enc_I := Enclosing_Body (Inst_Node);
6481 Ensure_Freeze_Node (Pack_Id);
6482 F_Node := Freeze_Node (Pack_Id);
6484 if Is_Generic_Instance (Par)
6485 and then Present (Freeze_Node (Par))
6487 In_Same_Declarative_Part (Freeze_Node (Par), Inst_Node)
6489 if ABE_Is_Certain (Get_Package_Instantiation_Node (Par)) then
6491 -- The parent was a premature instantiation. Insert freeze node at
6492 -- the end the current declarative part.
6494 Insert_After_Last_Decl (Inst_Node, F_Node);
6497 Insert_After (Freeze_Node (Par), F_Node);
6500 -- The body enclosing the instance should be frozen after the body that
6501 -- includes the generic, because the body of the instance may make
6502 -- references to entities therein. If the two are not in the same
6503 -- declarative part, or if the one enclosing the instance is frozen
6504 -- already, freeze the instance at the end of the current declarative
6507 elsif Is_Generic_Instance (Par)
6508 and then Present (Freeze_Node (Par))
6509 and then Present (Enc_I)
6511 if In_Same_Declarative_Part (Freeze_Node (Par), Enc_I)
6513 (Nkind (Enc_I) = N_Package_Body
6515 In_Same_Declarative_Part (Freeze_Node (Par), Parent (Enc_I)))
6517 -- The enclosing package may contain several instances. Rather
6518 -- than computing the earliest point at which to insert its
6519 -- freeze node, we place it at the end of the declarative part
6520 -- of the parent of the generic.
6522 Insert_After_Last_Decl
6523 (Freeze_Node (Par), Package_Freeze_Node (Enc_I));
6526 Insert_After_Last_Decl (Inst_Node, F_Node);
6528 elsif Present (Enc_G)
6529 and then Present (Enc_I)
6530 and then Enc_G /= Enc_I
6531 and then Earlier (Inst_Node, Gen_Body)
6533 if Nkind (Enc_G) = N_Package_Body then
6534 E_G_Id := Corresponding_Spec (Enc_G);
6535 else pragma Assert (Nkind (Enc_G) = N_Package_Body_Stub);
6537 Corresponding_Spec (Proper_Body (Unit (Library_Unit (Enc_G))));
6540 -- Freeze package that encloses instance, and place node after
6541 -- package that encloses generic. If enclosing package is already
6542 -- frozen we have to assume it is at the proper place. This may be
6543 -- a potential ABE that requires dynamic checking. Do not add a
6544 -- freeze node if the package that encloses the generic is inside
6545 -- the body that encloses the instance, because the freeze node
6546 -- would be in the wrong scope. Additional contortions needed if
6547 -- the bodies are within a subunit.
6550 Enclosing_Body : Node_Id;
6553 if Nkind (Enc_I) = N_Package_Body_Stub then
6554 Enclosing_Body := Proper_Body (Unit (Library_Unit (Enc_I)));
6556 Enclosing_Body := Enc_I;
6559 if Parent (List_Containing (Enc_G)) /= Enclosing_Body then
6560 Insert_After_Last_Decl (Enc_G, Package_Freeze_Node (Enc_I));
6564 -- Freeze enclosing subunit before instance
6566 Ensure_Freeze_Node (E_G_Id);
6568 if not Is_List_Member (Freeze_Node (E_G_Id)) then
6569 Insert_After (Enc_G, Freeze_Node (E_G_Id));
6572 Insert_After_Last_Decl (Inst_Node, F_Node);
6575 -- If none of the above, insert freeze node at the end of the current
6576 -- declarative part.
6578 Insert_After_Last_Decl (Inst_Node, F_Node);
6580 end Freeze_Subprogram_Body;
6586 function Get_Gen_Id (E : Assoc_Ptr) return Entity_Id is
6588 return Generic_Renamings.Table (E).Gen_Id;
6591 ---------------------
6592 -- Get_Instance_Of --
6593 ---------------------
6595 function Get_Instance_Of (A : Entity_Id) return Entity_Id is
6596 Res : constant Assoc_Ptr := Generic_Renamings_HTable.Get (A);
6599 if Res /= Assoc_Null then
6600 return Generic_Renamings.Table (Res).Act_Id;
6602 -- On exit, entity is not instantiated: not a generic parameter, or
6603 -- else parameter of an inner generic unit.
6607 end Get_Instance_Of;
6609 ------------------------------------
6610 -- Get_Package_Instantiation_Node --
6611 ------------------------------------
6613 function Get_Package_Instantiation_Node (A : Entity_Id) return Node_Id is
6614 Decl : Node_Id := Unit_Declaration_Node (A);
6618 -- If the Package_Instantiation attribute has been set on the package
6619 -- entity, then use it directly when it (or its Original_Node) refers
6620 -- to an N_Package_Instantiation node. In principle it should be
6621 -- possible to have this field set in all cases, which should be
6622 -- investigated, and would allow this function to be significantly
6625 if Present (Package_Instantiation (A)) then
6626 if Nkind (Package_Instantiation (A)) = N_Package_Instantiation then
6627 return Package_Instantiation (A);
6629 elsif Nkind (Original_Node (Package_Instantiation (A))) =
6630 N_Package_Instantiation
6632 return Original_Node (Package_Instantiation (A));
6636 -- If the instantiation is a compilation unit that does not need body
6637 -- then the instantiation node has been rewritten as a package
6638 -- declaration for the instance, and we return the original node.
6640 -- If it is a compilation unit and the instance node has not been
6641 -- rewritten, then it is still the unit of the compilation. Finally, if
6642 -- a body is present, this is a parent of the main unit whose body has
6643 -- been compiled for inlining purposes, and the instantiation node has
6644 -- been rewritten with the instance body.
6646 -- Otherwise the instantiation node appears after the declaration. If
6647 -- the entity is a formal package, the declaration may have been
6648 -- rewritten as a generic declaration (in the case of a formal with box)
6649 -- or left as a formal package declaration if it has actuals, and is
6650 -- found with a forward search.
6652 if Nkind (Parent (Decl)) = N_Compilation_Unit then
6653 if Nkind (Decl) = N_Package_Declaration
6654 and then Present (Corresponding_Body (Decl))
6656 Decl := Unit_Declaration_Node (Corresponding_Body (Decl));
6659 if Nkind (Original_Node (Decl)) = N_Package_Instantiation then
6660 return Original_Node (Decl);
6662 return Unit (Parent (Decl));
6665 elsif Nkind (Decl) = N_Package_Declaration
6666 and then Nkind (Original_Node (Decl)) = N_Formal_Package_Declaration
6668 return Original_Node (Decl);
6671 Inst := Next (Decl);
6672 while not Nkind_In (Inst, N_Package_Instantiation,
6673 N_Formal_Package_Declaration)
6680 end Get_Package_Instantiation_Node;
6682 ------------------------
6683 -- Has_Been_Exchanged --
6684 ------------------------
6686 function Has_Been_Exchanged (E : Entity_Id) return Boolean is
6690 Next := First_Elmt (Exchanged_Views);
6691 while Present (Next) loop
6692 if Full_View (Node (Next)) = E then
6700 end Has_Been_Exchanged;
6706 function Hash (F : Entity_Id) return HTable_Range is
6708 return HTable_Range (F mod HTable_Size);
6711 ------------------------
6712 -- Hide_Current_Scope --
6713 ------------------------
6715 procedure Hide_Current_Scope is
6716 C : constant Entity_Id := Current_Scope;
6720 Set_Is_Hidden_Open_Scope (C);
6722 E := First_Entity (C);
6723 while Present (E) loop
6724 if Is_Immediately_Visible (E) then
6725 Set_Is_Immediately_Visible (E, False);
6726 Append_Elmt (E, Hidden_Entities);
6732 -- Make the scope name invisible as well. This is necessary, but might
6733 -- conflict with calls to Rtsfind later on, in case the scope is a
6734 -- predefined one. There is no clean solution to this problem, so for
6735 -- now we depend on the user not redefining Standard itself in one of
6736 -- the parent units.
6738 if Is_Immediately_Visible (C)
6739 and then C /= Standard_Standard
6741 Set_Is_Immediately_Visible (C, False);
6742 Append_Elmt (C, Hidden_Entities);
6745 end Hide_Current_Scope;
6751 procedure Init_Env is
6752 Saved : Instance_Env;
6755 Saved.Instantiated_Parent := Current_Instantiated_Parent;
6756 Saved.Exchanged_Views := Exchanged_Views;
6757 Saved.Hidden_Entities := Hidden_Entities;
6758 Saved.Current_Sem_Unit := Current_Sem_Unit;
6759 Saved.Parent_Unit_Visible := Parent_Unit_Visible;
6760 Saved.Instance_Parent_Unit := Instance_Parent_Unit;
6762 -- Save configuration switches. These may be reset if the unit is a
6763 -- predefined unit, and the current mode is not Ada 2005.
6765 Save_Opt_Config_Switches (Saved.Switches);
6767 Instance_Envs.Append (Saved);
6769 Exchanged_Views := New_Elmt_List;
6770 Hidden_Entities := New_Elmt_List;
6772 -- Make dummy entry for Instantiated parent. If generic unit is legal,
6773 -- this is set properly in Set_Instance_Env.
6775 Current_Instantiated_Parent :=
6776 (Current_Scope, Current_Scope, Assoc_Null);
6779 ------------------------------
6780 -- In_Same_Declarative_Part --
6781 ------------------------------
6783 function In_Same_Declarative_Part
6785 Inst : Node_Id) return Boolean
6787 Decls : constant Node_Id := Parent (F_Node);
6788 Nod : Node_Id := Parent (Inst);
6791 while Present (Nod) loop
6795 elsif Nkind_In (Nod, N_Subprogram_Body,
6803 elsif Nkind (Nod) = N_Subunit then
6804 Nod := Corresponding_Stub (Nod);
6806 elsif Nkind (Nod) = N_Compilation_Unit then
6810 Nod := Parent (Nod);
6815 end In_Same_Declarative_Part;
6817 ---------------------
6818 -- In_Main_Context --
6819 ---------------------
6821 function In_Main_Context (E : Entity_Id) return Boolean is
6827 if not Is_Compilation_Unit (E)
6828 or else Ekind (E) /= E_Package
6829 or else In_Private_Part (E)
6834 Context := Context_Items (Cunit (Main_Unit));
6836 Clause := First (Context);
6837 while Present (Clause) loop
6838 if Nkind (Clause) = N_With_Clause then
6839 Nam := Name (Clause);
6841 -- If the current scope is part of the context of the main unit,
6842 -- analysis of the corresponding with_clause is not complete, and
6843 -- the entity is not set. We use the Chars field directly, which
6844 -- might produce false positives in rare cases, but guarantees
6845 -- that we produce all the instance bodies we will need.
6847 if (Is_Entity_Name (Nam)
6848 and then Chars (Nam) = Chars (E))
6849 or else (Nkind (Nam) = N_Selected_Component
6850 and then Chars (Selector_Name (Nam)) = Chars (E))
6860 end In_Main_Context;
6862 ---------------------
6863 -- Inherit_Context --
6864 ---------------------
6866 procedure Inherit_Context (Gen_Decl : Node_Id; Inst : Node_Id) is
6867 Current_Context : List_Id;
6868 Current_Unit : Node_Id;
6873 if Nkind (Parent (Gen_Decl)) = N_Compilation_Unit then
6875 -- The inherited context is attached to the enclosing compilation
6876 -- unit. This is either the main unit, or the declaration for the
6877 -- main unit (in case the instantiation appears within the package
6878 -- declaration and the main unit is its body).
6880 Current_Unit := Parent (Inst);
6881 while Present (Current_Unit)
6882 and then Nkind (Current_Unit) /= N_Compilation_Unit
6884 Current_Unit := Parent (Current_Unit);
6887 Current_Context := Context_Items (Current_Unit);
6889 Item := First (Context_Items (Parent (Gen_Decl)));
6890 while Present (Item) loop
6891 if Nkind (Item) = N_With_Clause then
6892 New_I := New_Copy (Item);
6893 Set_Implicit_With (New_I, True);
6894 Append (New_I, Current_Context);
6900 end Inherit_Context;
6906 procedure Initialize is
6908 Generic_Renamings.Init;
6911 Generic_Renamings_HTable.Reset;
6912 Circularity_Detected := False;
6913 Exchanged_Views := No_Elist;
6914 Hidden_Entities := No_Elist;
6917 ----------------------------
6918 -- Insert_After_Last_Decl --
6919 ----------------------------
6921 procedure Insert_After_Last_Decl (N : Node_Id; F_Node : Node_Id) is
6922 L : List_Id := List_Containing (N);
6923 P : constant Node_Id := Parent (L);
6926 if not Is_List_Member (F_Node) then
6927 if Nkind (P) = N_Package_Specification
6928 and then L = Visible_Declarations (P)
6929 and then Present (Private_Declarations (P))
6930 and then not Is_Empty_List (Private_Declarations (P))
6932 L := Private_Declarations (P);
6935 Insert_After (Last (L), F_Node);
6937 end Insert_After_Last_Decl;
6943 procedure Install_Body
6944 (Act_Body : Node_Id;
6949 Act_Id : constant Entity_Id := Corresponding_Spec (Act_Body);
6950 Act_Unit : constant Node_Id := Unit (Cunit (Get_Source_Unit (N)));
6951 Gen_Id : constant Entity_Id := Corresponding_Spec (Gen_Body);
6952 Par : constant Entity_Id := Scope (Gen_Id);
6953 Gen_Unit : constant Node_Id :=
6954 Unit (Cunit (Get_Source_Unit (Gen_Decl)));
6955 Orig_Body : Node_Id := Gen_Body;
6957 Body_Unit : Node_Id;
6959 Must_Delay : Boolean;
6961 function Enclosing_Subp (Id : Entity_Id) return Entity_Id;
6962 -- Find subprogram (if any) that encloses instance and/or generic body
6964 function True_Sloc (N : Node_Id) return Source_Ptr;
6965 -- If the instance is nested inside a generic unit, the Sloc of the
6966 -- instance indicates the place of the original definition, not the
6967 -- point of the current enclosing instance. Pending a better usage of
6968 -- Slocs to indicate instantiation places, we determine the place of
6969 -- origin of a node by finding the maximum sloc of any ancestor node.
6970 -- Why is this not equivalent to Top_Level_Location ???
6972 --------------------
6973 -- Enclosing_Subp --
6974 --------------------
6976 function Enclosing_Subp (Id : Entity_Id) return Entity_Id is
6977 Scop : Entity_Id := Scope (Id);
6980 while Scop /= Standard_Standard
6981 and then not Is_Overloadable (Scop)
6983 Scop := Scope (Scop);
6993 function True_Sloc (N : Node_Id) return Source_Ptr is
7000 while Present (N1) and then N1 /= Act_Unit loop
7001 if Sloc (N1) > Res then
7011 -- Start of processing for Install_Body
7015 -- If the body is a subunit, the freeze point is the corresponding
7016 -- stub in the current compilation, not the subunit itself.
7018 if Nkind (Parent (Gen_Body)) = N_Subunit then
7019 Orig_Body := Corresponding_Stub (Parent (Gen_Body));
7021 Orig_Body := Gen_Body;
7024 Body_Unit := Unit (Cunit (Get_Source_Unit (Orig_Body)));
7026 -- If the instantiation and the generic definition appear in the same
7027 -- package declaration, this is an early instantiation. If they appear
7028 -- in the same declarative part, it is an early instantiation only if
7029 -- the generic body appears textually later, and the generic body is
7030 -- also in the main unit.
7032 -- If instance is nested within a subprogram, and the generic body is
7033 -- not, the instance is delayed because the enclosing body is. If
7034 -- instance and body are within the same scope, or the same sub-
7035 -- program body, indicate explicitly that the instance is delayed.
7038 (Gen_Unit = Act_Unit
7039 and then (Nkind_In (Gen_Unit, N_Package_Declaration,
7040 N_Generic_Package_Declaration)
7041 or else (Gen_Unit = Body_Unit
7042 and then True_Sloc (N) < Sloc (Orig_Body)))
7043 and then Is_In_Main_Unit (Gen_Unit)
7044 and then (Scope (Act_Id) = Scope (Gen_Id)
7046 Enclosing_Subp (Act_Id) = Enclosing_Subp (Gen_Id)));
7048 -- If this is an early instantiation, the freeze node is placed after
7049 -- the generic body. Otherwise, if the generic appears in an instance,
7050 -- we cannot freeze the current instance until the outer one is frozen.
7051 -- This is only relevant if the current instance is nested within some
7052 -- inner scope not itself within the outer instance. If this scope is
7053 -- a package body in the same declarative part as the outer instance,
7054 -- then that body needs to be frozen after the outer instance. Finally,
7055 -- if no delay is needed, we place the freeze node at the end of the
7056 -- current declarative part.
7058 if Expander_Active then
7059 Ensure_Freeze_Node (Act_Id);
7060 F_Node := Freeze_Node (Act_Id);
7063 Insert_After (Orig_Body, F_Node);
7065 elsif Is_Generic_Instance (Par)
7066 and then Present (Freeze_Node (Par))
7067 and then Scope (Act_Id) /= Par
7069 -- Freeze instance of inner generic after instance of enclosing
7072 if In_Same_Declarative_Part (Freeze_Node (Par), N) then
7073 Insert_After (Freeze_Node (Par), F_Node);
7075 -- Freeze package enclosing instance of inner generic after
7076 -- instance of enclosing generic.
7078 elsif Nkind (Parent (N)) = N_Package_Body
7079 and then In_Same_Declarative_Part (Freeze_Node (Par), Parent (N))
7083 Enclosing : constant Entity_Id :=
7084 Corresponding_Spec (Parent (N));
7087 Insert_After_Last_Decl (N, F_Node);
7088 Ensure_Freeze_Node (Enclosing);
7090 if not Is_List_Member (Freeze_Node (Enclosing)) then
7091 Insert_After (Freeze_Node (Par), Freeze_Node (Enclosing));
7096 Insert_After_Last_Decl (N, F_Node);
7100 Insert_After_Last_Decl (N, F_Node);
7104 Set_Is_Frozen (Act_Id);
7105 Insert_Before (N, Act_Body);
7106 Mark_Rewrite_Insertion (Act_Body);
7109 -----------------------------
7110 -- Install_Formal_Packages --
7111 -----------------------------
7113 procedure Install_Formal_Packages (Par : Entity_Id) is
7117 E := First_Entity (Par);
7118 while Present (E) loop
7119 if Ekind (E) = E_Package
7120 and then Nkind (Parent (E)) = N_Package_Renaming_Declaration
7122 -- If this is the renaming for the parent instance, done
7124 if Renamed_Object (E) = Par then
7127 -- The visibility of a formal of an enclosing generic is
7130 elsif Denotes_Formal_Package (E) then
7133 elsif Present (Associated_Formal_Package (E))
7134 and then Box_Present (Parent (Associated_Formal_Package (E)))
7136 Check_Generic_Actuals (Renamed_Object (E), True);
7137 Set_Is_Hidden (E, False);
7143 end Install_Formal_Packages;
7145 --------------------
7146 -- Install_Parent --
7147 --------------------
7149 procedure Install_Parent (P : Entity_Id; In_Body : Boolean := False) is
7150 Ancestors : constant Elist_Id := New_Elmt_List;
7151 S : constant Entity_Id := Current_Scope;
7152 Inst_Par : Entity_Id;
7153 First_Par : Entity_Id;
7154 Inst_Node : Node_Id;
7155 Gen_Par : Entity_Id;
7156 First_Gen : Entity_Id;
7159 procedure Install_Noninstance_Specs (Par : Entity_Id);
7160 -- Install the scopes of noninstance parent units ending with Par
7162 procedure Install_Spec (Par : Entity_Id);
7163 -- The child unit is within the declarative part of the parent, so
7164 -- the declarations within the parent are immediately visible.
7166 -------------------------------
7167 -- Install_Noninstance_Specs --
7168 -------------------------------
7170 procedure Install_Noninstance_Specs (Par : Entity_Id) is
7173 and then Par /= Standard_Standard
7174 and then not In_Open_Scopes (Par)
7176 Install_Noninstance_Specs (Scope (Par));
7179 end Install_Noninstance_Specs;
7185 procedure Install_Spec (Par : Entity_Id) is
7186 Spec : constant Node_Id :=
7187 Specification (Unit_Declaration_Node (Par));
7190 -- If this parent of the child instance is a top-level unit,
7191 -- then record the unit and its visibility for later resetting
7192 -- in Remove_Parent. We exclude units that are generic instances,
7193 -- as we only want to record this information for the ultimate
7194 -- top-level noninstance parent (is that always correct???).
7196 if Scope (Par) = Standard_Standard
7197 and then not Is_Generic_Instance (Par)
7199 Parent_Unit_Visible := Is_Immediately_Visible (Par);
7200 Instance_Parent_Unit := Par;
7203 -- Open the parent scope and make it and its declarations visible.
7204 -- If this point is not within a body, then only the visible
7205 -- declarations should be made visible, and installation of the
7206 -- private declarations is deferred until the appropriate point
7207 -- within analysis of the spec being instantiated (see the handling
7208 -- of parent visibility in Analyze_Package_Specification). This is
7209 -- relaxed in the case where the parent unit is Ada.Tags, to avoid
7210 -- private view problems that occur when compiling instantiations of
7211 -- a generic child of that package (Generic_Dispatching_Constructor).
7212 -- If the instance freezes a tagged type, inlinings of operations
7213 -- from Ada.Tags may need the full view of type Tag. If inlining took
7214 -- proper account of establishing visibility of inlined subprograms'
7215 -- parents then it should be possible to remove this
7216 -- special check. ???
7219 Set_Is_Immediately_Visible (Par);
7220 Install_Visible_Declarations (Par);
7221 Set_Use (Visible_Declarations (Spec));
7223 if In_Body or else Is_RTU (Par, Ada_Tags) then
7224 Install_Private_Declarations (Par);
7225 Set_Use (Private_Declarations (Spec));
7229 -- Start of processing for Install_Parent
7232 -- We need to install the parent instance to compile the instantiation
7233 -- of the child, but the child instance must appear in the current
7234 -- scope. Given that we cannot place the parent above the current scope
7235 -- in the scope stack, we duplicate the current scope and unstack both
7236 -- after the instantiation is complete.
7238 -- If the parent is itself the instantiation of a child unit, we must
7239 -- also stack the instantiation of its parent, and so on. Each such
7240 -- ancestor is the prefix of the name in a prior instantiation.
7242 -- If this is a nested instance, the parent unit itself resolves to
7243 -- a renaming of the parent instance, whose declaration we need.
7245 -- Finally, the parent may be a generic (not an instance) when the
7246 -- child unit appears as a formal package.
7250 if Present (Renamed_Entity (Inst_Par)) then
7251 Inst_Par := Renamed_Entity (Inst_Par);
7254 First_Par := Inst_Par;
7257 Generic_Parent (Specification (Unit_Declaration_Node (Inst_Par)));
7259 First_Gen := Gen_Par;
7261 while Present (Gen_Par)
7262 and then Is_Child_Unit (Gen_Par)
7264 -- Load grandparent instance as well
7266 Inst_Node := Get_Package_Instantiation_Node (Inst_Par);
7268 if Nkind (Name (Inst_Node)) = N_Expanded_Name then
7269 Inst_Par := Entity (Prefix (Name (Inst_Node)));
7271 if Present (Renamed_Entity (Inst_Par)) then
7272 Inst_Par := Renamed_Entity (Inst_Par);
7277 (Specification (Unit_Declaration_Node (Inst_Par)));
7279 if Present (Gen_Par) then
7280 Prepend_Elmt (Inst_Par, Ancestors);
7283 -- Parent is not the name of an instantiation
7285 Install_Noninstance_Specs (Inst_Par);
7297 if Present (First_Gen) then
7298 Append_Elmt (First_Par, Ancestors);
7301 Install_Noninstance_Specs (First_Par);
7304 if not Is_Empty_Elmt_List (Ancestors) then
7305 Elmt := First_Elmt (Ancestors);
7307 while Present (Elmt) loop
7308 Install_Spec (Node (Elmt));
7309 Install_Formal_Packages (Node (Elmt));
7320 --------------------------------
7321 -- Instantiate_Formal_Package --
7322 --------------------------------
7324 function Instantiate_Formal_Package
7327 Analyzed_Formal : Node_Id) return List_Id
7329 Loc : constant Source_Ptr := Sloc (Actual);
7330 Actual_Pack : Entity_Id;
7331 Formal_Pack : Entity_Id;
7332 Gen_Parent : Entity_Id;
7335 Parent_Spec : Node_Id;
7337 procedure Find_Matching_Actual
7339 Act : in out Entity_Id);
7340 -- We need to associate each formal entity in the formal package
7341 -- with the corresponding entity in the actual package. The actual
7342 -- package has been analyzed and possibly expanded, and as a result
7343 -- there is no one-to-one correspondence between the two lists (for
7344 -- example, the actual may include subtypes, itypes, and inherited
7345 -- primitive operations, interspersed among the renaming declarations
7346 -- for the actuals) . We retrieve the corresponding actual by name
7347 -- because each actual has the same name as the formal, and they do
7348 -- appear in the same order.
7350 function Get_Formal_Entity (N : Node_Id) return Entity_Id;
7351 -- Retrieve entity of defining entity of generic formal parameter.
7352 -- Only the declarations of formals need to be considered when
7353 -- linking them to actuals, but the declarative list may include
7354 -- internal entities generated during analysis, and those are ignored.
7356 procedure Match_Formal_Entity
7357 (Formal_Node : Node_Id;
7358 Formal_Ent : Entity_Id;
7359 Actual_Ent : Entity_Id);
7360 -- Associates the formal entity with the actual. In the case
7361 -- where Formal_Ent is a formal package, this procedure iterates
7362 -- through all of its formals and enters associations between the
7363 -- actuals occurring in the formal package's corresponding actual
7364 -- package (given by Actual_Ent) and the formal package's formal
7365 -- parameters. This procedure recurses if any of the parameters is
7366 -- itself a package.
7368 function Is_Instance_Of
7369 (Act_Spec : Entity_Id;
7370 Gen_Anc : Entity_Id) return Boolean;
7371 -- The actual can be an instantiation of a generic within another
7372 -- instance, in which case there is no direct link from it to the
7373 -- original generic ancestor. In that case, we recognize that the
7374 -- ultimate ancestor is the same by examining names and scopes.
7376 procedure Map_Entities (Form : Entity_Id; Act : Entity_Id);
7377 -- Within the generic part, entities in the formal package are
7378 -- visible. To validate subsequent type declarations, indicate
7379 -- the correspondence between the entities in the analyzed formal,
7380 -- and the entities in the actual package. There are three packages
7381 -- involved in the instantiation of a formal package: the parent
7382 -- generic P1 which appears in the generic declaration, the fake
7383 -- instantiation P2 which appears in the analyzed generic, and whose
7384 -- visible entities may be used in subsequent formals, and the actual
7385 -- P3 in the instance. To validate subsequent formals, me indicate
7386 -- that the entities in P2 are mapped into those of P3. The mapping of
7387 -- entities has to be done recursively for nested packages.
7389 procedure Process_Nested_Formal (Formal : Entity_Id);
7390 -- If the current formal is declared with a box, its own formals are
7391 -- visible in the instance, as they were in the generic, and their
7392 -- Hidden flag must be reset. If some of these formals are themselves
7393 -- packages declared with a box, the processing must be recursive.
7395 --------------------------
7396 -- Find_Matching_Actual --
7397 --------------------------
7399 procedure Find_Matching_Actual
7401 Act : in out Entity_Id)
7403 Formal_Ent : Entity_Id;
7406 case Nkind (Original_Node (F)) is
7407 when N_Formal_Object_Declaration |
7408 N_Formal_Type_Declaration =>
7409 Formal_Ent := Defining_Identifier (F);
7411 while Chars (Act) /= Chars (Formal_Ent) loop
7415 when N_Formal_Subprogram_Declaration |
7416 N_Formal_Package_Declaration |
7417 N_Package_Declaration |
7418 N_Generic_Package_Declaration =>
7419 Formal_Ent := Defining_Entity (F);
7421 while Chars (Act) /= Chars (Formal_Ent) loop
7426 raise Program_Error;
7428 end Find_Matching_Actual;
7430 -------------------------
7431 -- Match_Formal_Entity --
7432 -------------------------
7434 procedure Match_Formal_Entity
7435 (Formal_Node : Node_Id;
7436 Formal_Ent : Entity_Id;
7437 Actual_Ent : Entity_Id)
7439 Act_Pkg : Entity_Id;
7442 Set_Instance_Of (Formal_Ent, Actual_Ent);
7444 if Ekind (Actual_Ent) = E_Package then
7446 -- Record associations for each parameter
7448 Act_Pkg := Actual_Ent;
7451 A_Ent : Entity_Id := First_Entity (Act_Pkg);
7460 -- Retrieve the actual given in the formal package declaration
7462 Actual := Entity (Name (Original_Node (Formal_Node)));
7464 -- The actual in the formal package declaration may be a
7465 -- renamed generic package, in which case we want to retrieve
7466 -- the original generic in order to traverse its formal part.
7468 if Present (Renamed_Entity (Actual)) then
7469 Gen_Decl := Unit_Declaration_Node (Renamed_Entity (Actual));
7471 Gen_Decl := Unit_Declaration_Node (Actual);
7474 Formals := Generic_Formal_Declarations (Gen_Decl);
7476 if Present (Formals) then
7477 F_Node := First_Non_Pragma (Formals);
7482 while Present (A_Ent)
7483 and then Present (F_Node)
7484 and then A_Ent /= First_Private_Entity (Act_Pkg)
7486 F_Ent := Get_Formal_Entity (F_Node);
7488 if Present (F_Ent) then
7490 -- This is a formal of the original package. Record
7491 -- association and recurse.
7493 Find_Matching_Actual (F_Node, A_Ent);
7494 Match_Formal_Entity (F_Node, F_Ent, A_Ent);
7495 Next_Entity (A_Ent);
7498 Next_Non_Pragma (F_Node);
7502 end Match_Formal_Entity;
7504 -----------------------
7505 -- Get_Formal_Entity --
7506 -----------------------
7508 function Get_Formal_Entity (N : Node_Id) return Entity_Id is
7509 Kind : constant Node_Kind := Nkind (Original_Node (N));
7512 when N_Formal_Object_Declaration =>
7513 return Defining_Identifier (N);
7515 when N_Formal_Type_Declaration =>
7516 return Defining_Identifier (N);
7518 when N_Formal_Subprogram_Declaration =>
7519 return Defining_Unit_Name (Specification (N));
7521 when N_Formal_Package_Declaration =>
7522 return Defining_Identifier (Original_Node (N));
7524 when N_Generic_Package_Declaration =>
7525 return Defining_Identifier (Original_Node (N));
7527 -- All other declarations are introduced by semantic analysis and
7528 -- have no match in the actual.
7533 end Get_Formal_Entity;
7535 --------------------
7536 -- Is_Instance_Of --
7537 --------------------
7539 function Is_Instance_Of
7540 (Act_Spec : Entity_Id;
7541 Gen_Anc : Entity_Id) return Boolean
7543 Gen_Par : constant Entity_Id := Generic_Parent (Act_Spec);
7546 if No (Gen_Par) then
7549 -- Simplest case: the generic parent of the actual is the formal
7551 elsif Gen_Par = Gen_Anc then
7554 elsif Chars (Gen_Par) /= Chars (Gen_Anc) then
7557 -- The actual may be obtained through several instantiations. Its
7558 -- scope must itself be an instance of a generic declared in the
7559 -- same scope as the formal. Any other case is detected above.
7561 elsif not Is_Generic_Instance (Scope (Gen_Par)) then
7565 return Generic_Parent (Parent (Scope (Gen_Par))) = Scope (Gen_Anc);
7573 procedure Map_Entities (Form : Entity_Id; Act : Entity_Id) is
7578 Set_Instance_Of (Form, Act);
7580 -- Traverse formal and actual package to map the corresponding
7581 -- entities. We skip over internal entities that may be generated
7582 -- during semantic analysis, and find the matching entities by
7583 -- name, given that they must appear in the same order.
7585 E1 := First_Entity (Form);
7586 E2 := First_Entity (Act);
7588 and then E1 /= First_Private_Entity (Form)
7590 -- Could this test be a single condition???
7591 -- Seems like it could, and isn't FPE (Form) a constant anyway???
7593 if not Is_Internal (E1)
7594 and then Present (Parent (E1))
7595 and then not Is_Class_Wide_Type (E1)
7596 and then not Is_Internal_Name (Chars (E1))
7599 and then Chars (E2) /= Chars (E1)
7607 Set_Instance_Of (E1, E2);
7610 and then Is_Tagged_Type (E2)
7613 (Class_Wide_Type (E1), Class_Wide_Type (E2));
7616 if Ekind (E1) = E_Package
7617 and then No (Renamed_Object (E1))
7619 Map_Entities (E1, E2);
7628 ---------------------------
7629 -- Process_Nested_Formal --
7630 ---------------------------
7632 procedure Process_Nested_Formal (Formal : Entity_Id) is
7636 if Present (Associated_Formal_Package (Formal))
7637 and then Box_Present (Parent (Associated_Formal_Package (Formal)))
7639 Ent := First_Entity (Formal);
7640 while Present (Ent) loop
7641 Set_Is_Hidden (Ent, False);
7642 Set_Is_Visible_Formal (Ent);
7643 Set_Is_Potentially_Use_Visible
7644 (Ent, Is_Potentially_Use_Visible (Formal));
7646 if Ekind (Ent) = E_Package then
7647 exit when Renamed_Entity (Ent) = Renamed_Entity (Formal);
7648 Process_Nested_Formal (Ent);
7654 end Process_Nested_Formal;
7656 -- Start of processing for Instantiate_Formal_Package
7661 if not Is_Entity_Name (Actual)
7662 or else Ekind (Entity (Actual)) /= E_Package
7665 ("expect package instance to instantiate formal", Actual);
7666 Abandon_Instantiation (Actual);
7667 raise Program_Error;
7670 Actual_Pack := Entity (Actual);
7671 Set_Is_Instantiated (Actual_Pack);
7673 -- The actual may be a renamed package, or an outer generic formal
7674 -- package whose instantiation is converted into a renaming.
7676 if Present (Renamed_Object (Actual_Pack)) then
7677 Actual_Pack := Renamed_Object (Actual_Pack);
7680 if Nkind (Analyzed_Formal) = N_Formal_Package_Declaration then
7681 Gen_Parent := Get_Instance_Of (Entity (Name (Analyzed_Formal)));
7682 Formal_Pack := Defining_Identifier (Analyzed_Formal);
7685 Generic_Parent (Specification (Analyzed_Formal));
7687 Defining_Unit_Name (Specification (Analyzed_Formal));
7690 if Nkind (Parent (Actual_Pack)) = N_Defining_Program_Unit_Name then
7691 Parent_Spec := Specification (Unit_Declaration_Node (Actual_Pack));
7693 Parent_Spec := Parent (Actual_Pack);
7696 if Gen_Parent = Any_Id then
7698 ("previous error in declaration of formal package", Actual);
7699 Abandon_Instantiation (Actual);
7702 Is_Instance_Of (Parent_Spec, Get_Instance_Of (Gen_Parent))
7708 ("actual parameter must be instance of&", Actual, Gen_Parent);
7709 Abandon_Instantiation (Actual);
7712 Set_Instance_Of (Defining_Identifier (Formal), Actual_Pack);
7713 Map_Entities (Formal_Pack, Actual_Pack);
7716 Make_Package_Renaming_Declaration (Loc,
7717 Defining_Unit_Name => New_Copy (Defining_Identifier (Formal)),
7718 Name => New_Reference_To (Actual_Pack, Loc));
7720 Set_Associated_Formal_Package (Defining_Unit_Name (Nod),
7721 Defining_Identifier (Formal));
7722 Decls := New_List (Nod);
7724 -- If the formal F has a box, then the generic declarations are
7725 -- visible in the generic G. In an instance of G, the corresponding
7726 -- entities in the actual for F (which are the actuals for the
7727 -- instantiation of the generic that F denotes) must also be made
7728 -- visible for analysis of the current instance. On exit from the
7729 -- current instance, those entities are made private again. If the
7730 -- actual is currently in use, these entities are also use-visible.
7732 -- The loop through the actual entities also steps through the formal
7733 -- entities and enters associations from formals to actuals into the
7734 -- renaming map. This is necessary to properly handle checking of
7735 -- actual parameter associations for later formals that depend on
7736 -- actuals declared in the formal package.
7738 -- In Ada 2005, partial parametrization requires that we make visible
7739 -- the actuals corresponding to formals that were defaulted in the
7740 -- formal package. There formals are identified because they remain
7741 -- formal generics within the formal package, rather than being
7742 -- renamings of the actuals supplied.
7745 Gen_Decl : constant Node_Id :=
7746 Unit_Declaration_Node (Gen_Parent);
7747 Formals : constant List_Id :=
7748 Generic_Formal_Declarations (Gen_Decl);
7750 Actual_Ent : Entity_Id;
7751 Actual_Of_Formal : Node_Id;
7752 Formal_Node : Node_Id;
7753 Formal_Ent : Entity_Id;
7756 if Present (Formals) then
7757 Formal_Node := First_Non_Pragma (Formals);
7759 Formal_Node := Empty;
7762 Actual_Ent := First_Entity (Actual_Pack);
7764 First (Visible_Declarations (Specification (Analyzed_Formal)));
7765 while Present (Actual_Ent)
7766 and then Actual_Ent /= First_Private_Entity (Actual_Pack)
7768 if Present (Formal_Node) then
7769 Formal_Ent := Get_Formal_Entity (Formal_Node);
7771 if Present (Formal_Ent) then
7772 Find_Matching_Actual (Formal_Node, Actual_Ent);
7774 (Formal_Node, Formal_Ent, Actual_Ent);
7776 -- We iterate at the same time over the actuals of the
7777 -- local package created for the formal, to determine
7778 -- which one of the formals of the original generic were
7779 -- defaulted in the formal. The corresponding actual
7780 -- entities are visible in the enclosing instance.
7782 if Box_Present (Formal)
7784 (Present (Actual_Of_Formal)
7787 (Get_Formal_Entity (Actual_Of_Formal)))
7789 Set_Is_Hidden (Actual_Ent, False);
7790 Set_Is_Visible_Formal (Actual_Ent);
7791 Set_Is_Potentially_Use_Visible
7792 (Actual_Ent, In_Use (Actual_Pack));
7794 if Ekind (Actual_Ent) = E_Package then
7795 Process_Nested_Formal (Actual_Ent);
7799 Set_Is_Hidden (Actual_Ent);
7800 Set_Is_Potentially_Use_Visible (Actual_Ent, False);
7804 Next_Non_Pragma (Formal_Node);
7805 Next (Actual_Of_Formal);
7808 -- No further formals to match, but the generic part may
7809 -- contain inherited operation that are not hidden in the
7810 -- enclosing instance.
7812 Next_Entity (Actual_Ent);
7816 -- Inherited subprograms generated by formal derived types are
7817 -- also visible if the types are.
7819 Actual_Ent := First_Entity (Actual_Pack);
7820 while Present (Actual_Ent)
7821 and then Actual_Ent /= First_Private_Entity (Actual_Pack)
7823 if Is_Overloadable (Actual_Ent)
7825 Nkind (Parent (Actual_Ent)) = N_Subtype_Declaration
7827 not Is_Hidden (Defining_Identifier (Parent (Actual_Ent)))
7829 Set_Is_Hidden (Actual_Ent, False);
7830 Set_Is_Potentially_Use_Visible
7831 (Actual_Ent, In_Use (Actual_Pack));
7834 Next_Entity (Actual_Ent);
7838 -- If the formal is not declared with a box, reanalyze it as an
7839 -- abbreviated instantiation, to verify the matching rules of 12.7.
7840 -- The actual checks are performed after the generic associations
7841 -- have been analyzed, to guarantee the same visibility for this
7842 -- instantiation and for the actuals.
7844 -- In Ada 2005, the generic associations for the formal can include
7845 -- defaulted parameters. These are ignored during check. This
7846 -- internal instantiation is removed from the tree after conformance
7847 -- checking, because it contains formal declarations for those
7848 -- defaulted parameters, and those should not reach the back-end.
7850 if not Box_Present (Formal) then
7852 I_Pack : constant Entity_Id :=
7853 Make_Defining_Identifier (Sloc (Actual),
7854 Chars => New_Internal_Name ('P'));
7857 Set_Is_Internal (I_Pack);
7860 Make_Package_Instantiation (Sloc (Actual),
7861 Defining_Unit_Name => I_Pack,
7864 (Get_Instance_Of (Gen_Parent), Sloc (Actual)),
7865 Generic_Associations =>
7866 Generic_Associations (Formal)));
7872 end Instantiate_Formal_Package;
7874 -----------------------------------
7875 -- Instantiate_Formal_Subprogram --
7876 -----------------------------------
7878 function Instantiate_Formal_Subprogram
7881 Analyzed_Formal : Node_Id) return Node_Id
7884 Formal_Sub : constant Entity_Id :=
7885 Defining_Unit_Name (Specification (Formal));
7886 Analyzed_S : constant Entity_Id :=
7887 Defining_Unit_Name (Specification (Analyzed_Formal));
7888 Decl_Node : Node_Id;
7892 function From_Parent_Scope (Subp : Entity_Id) return Boolean;
7893 -- If the generic is a child unit, the parent has been installed on the
7894 -- scope stack, but a default subprogram cannot resolve to something on
7895 -- the parent because that parent is not really part of the visible
7896 -- context (it is there to resolve explicit local entities). If the
7897 -- default has resolved in this way, we remove the entity from
7898 -- immediate visibility and analyze the node again to emit an error
7899 -- message or find another visible candidate.
7901 procedure Valid_Actual_Subprogram (Act : Node_Id);
7902 -- Perform legality check and raise exception on failure
7904 -----------------------
7905 -- From_Parent_Scope --
7906 -----------------------
7908 function From_Parent_Scope (Subp : Entity_Id) return Boolean is
7909 Gen_Scope : Node_Id;
7912 Gen_Scope := Scope (Analyzed_S);
7913 while Present (Gen_Scope)
7914 and then Is_Child_Unit (Gen_Scope)
7916 if Scope (Subp) = Scope (Gen_Scope) then
7920 Gen_Scope := Scope (Gen_Scope);
7924 end From_Parent_Scope;
7926 -----------------------------
7927 -- Valid_Actual_Subprogram --
7928 -----------------------------
7930 procedure Valid_Actual_Subprogram (Act : Node_Id) is
7934 if Is_Entity_Name (Act) then
7935 Act_E := Entity (Act);
7937 elsif Nkind (Act) = N_Selected_Component
7938 and then Is_Entity_Name (Selector_Name (Act))
7940 Act_E := Entity (Selector_Name (Act));
7946 if (Present (Act_E) and then Is_Overloadable (Act_E))
7947 or else Nkind_In (Act, N_Attribute_Reference,
7948 N_Indexed_Component,
7949 N_Character_Literal,
7950 N_Explicit_Dereference)
7956 ("expect subprogram or entry name in instantiation of&",
7957 Instantiation_Node, Formal_Sub);
7958 Abandon_Instantiation (Instantiation_Node);
7960 end Valid_Actual_Subprogram;
7962 -- Start of processing for Instantiate_Formal_Subprogram
7965 New_Spec := New_Copy_Tree (Specification (Formal));
7967 -- The tree copy has created the proper instantiation sloc for the
7968 -- new specification. Use this location for all other constructed
7971 Loc := Sloc (Defining_Unit_Name (New_Spec));
7973 -- Create new entity for the actual (New_Copy_Tree does not)
7975 Set_Defining_Unit_Name
7976 (New_Spec, Make_Defining_Identifier (Loc, Chars (Formal_Sub)));
7978 -- Create new entities for the each of the formals in the
7979 -- specification of the renaming declaration built for the actual.
7981 if Present (Parameter_Specifications (New_Spec)) then
7985 F := First (Parameter_Specifications (New_Spec));
7986 while Present (F) loop
7987 Set_Defining_Identifier (F,
7988 Make_Defining_Identifier (Sloc (F),
7989 Chars => Chars (Defining_Identifier (F))));
7995 -- Find entity of actual. If the actual is an attribute reference, it
7996 -- cannot be resolved here (its formal is missing) but is handled
7997 -- instead in Attribute_Renaming. If the actual is overloaded, it is
7998 -- fully resolved subsequently, when the renaming declaration for the
7999 -- formal is analyzed. If it is an explicit dereference, resolve the
8000 -- prefix but not the actual itself, to prevent interpretation as call.
8002 if Present (Actual) then
8003 Loc := Sloc (Actual);
8004 Set_Sloc (New_Spec, Loc);
8006 if Nkind (Actual) = N_Operator_Symbol then
8007 Find_Direct_Name (Actual);
8009 elsif Nkind (Actual) = N_Explicit_Dereference then
8010 Analyze (Prefix (Actual));
8012 elsif Nkind (Actual) /= N_Attribute_Reference then
8016 Valid_Actual_Subprogram (Actual);
8019 elsif Present (Default_Name (Formal)) then
8020 if not Nkind_In (Default_Name (Formal), N_Attribute_Reference,
8021 N_Selected_Component,
8022 N_Indexed_Component,
8023 N_Character_Literal)
8024 and then Present (Entity (Default_Name (Formal)))
8026 Nam := New_Occurrence_Of (Entity (Default_Name (Formal)), Loc);
8028 Nam := New_Copy (Default_Name (Formal));
8029 Set_Sloc (Nam, Loc);
8032 elsif Box_Present (Formal) then
8034 -- Actual is resolved at the point of instantiation. Create an
8035 -- identifier or operator with the same name as the formal.
8037 if Nkind (Formal_Sub) = N_Defining_Operator_Symbol then
8038 Nam := Make_Operator_Symbol (Loc,
8039 Chars => Chars (Formal_Sub),
8040 Strval => No_String);
8042 Nam := Make_Identifier (Loc, Chars (Formal_Sub));
8045 elsif Nkind (Specification (Formal)) = N_Procedure_Specification
8046 and then Null_Present (Specification (Formal))
8048 -- Generate null body for procedure, for use in the instance
8051 Make_Subprogram_Body (Loc,
8052 Specification => New_Spec,
8053 Declarations => New_List,
8054 Handled_Statement_Sequence =>
8055 Make_Handled_Sequence_Of_Statements (Loc,
8056 Statements => New_List (Make_Null_Statement (Loc))));
8058 Set_Is_Intrinsic_Subprogram (Defining_Unit_Name (New_Spec));
8062 Error_Msg_Sloc := Sloc (Scope (Analyzed_S));
8064 ("missing actual&", Instantiation_Node, Formal_Sub);
8066 ("\in instantiation of & declared#",
8067 Instantiation_Node, Scope (Analyzed_S));
8068 Abandon_Instantiation (Instantiation_Node);
8072 Make_Subprogram_Renaming_Declaration (Loc,
8073 Specification => New_Spec,
8076 -- If we do not have an actual and the formal specified <> then set to
8077 -- get proper default.
8079 if No (Actual) and then Box_Present (Formal) then
8080 Set_From_Default (Decl_Node);
8083 -- Gather possible interpretations for the actual before analyzing the
8084 -- instance. If overloaded, it will be resolved when analyzing the
8085 -- renaming declaration.
8087 if Box_Present (Formal)
8088 and then No (Actual)
8092 if Is_Child_Unit (Scope (Analyzed_S))
8093 and then Present (Entity (Nam))
8095 if not Is_Overloaded (Nam) then
8097 if From_Parent_Scope (Entity (Nam)) then
8098 Set_Is_Immediately_Visible (Entity (Nam), False);
8099 Set_Entity (Nam, Empty);
8100 Set_Etype (Nam, Empty);
8104 Set_Is_Immediately_Visible (Entity (Nam));
8113 Get_First_Interp (Nam, I, It);
8115 while Present (It.Nam) loop
8116 if From_Parent_Scope (It.Nam) then
8120 Get_Next_Interp (I, It);
8127 -- The generic instantiation freezes the actual. This can only be done
8128 -- once the actual is resolved, in the analysis of the renaming
8129 -- declaration. To make the formal subprogram entity available, we set
8130 -- Corresponding_Formal_Spec to point to the formal subprogram entity.
8131 -- This is also needed in Analyze_Subprogram_Renaming for the processing
8132 -- of formal abstract subprograms.
8134 Set_Corresponding_Formal_Spec (Decl_Node, Analyzed_S);
8136 -- We cannot analyze the renaming declaration, and thus find the actual,
8137 -- until all the actuals are assembled in the instance. For subsequent
8138 -- checks of other actuals, indicate the node that will hold the
8139 -- instance of this formal.
8141 Set_Instance_Of (Analyzed_S, Nam);
8143 if Nkind (Actual) = N_Selected_Component
8144 and then Is_Task_Type (Etype (Prefix (Actual)))
8145 and then not Is_Frozen (Etype (Prefix (Actual)))
8147 -- The renaming declaration will create a body, which must appear
8148 -- outside of the instantiation, We move the renaming declaration
8149 -- out of the instance, and create an additional renaming inside,
8150 -- to prevent freezing anomalies.
8153 Anon_Id : constant Entity_Id :=
8154 Make_Defining_Identifier
8155 (Loc, New_Internal_Name ('E'));
8157 Set_Defining_Unit_Name (New_Spec, Anon_Id);
8158 Insert_Before (Instantiation_Node, Decl_Node);
8159 Analyze (Decl_Node);
8161 -- Now create renaming within the instance
8164 Make_Subprogram_Renaming_Declaration (Loc,
8165 Specification => New_Copy_Tree (New_Spec),
8166 Name => New_Occurrence_Of (Anon_Id, Loc));
8168 Set_Defining_Unit_Name (Specification (Decl_Node),
8169 Make_Defining_Identifier (Loc, Chars (Formal_Sub)));
8174 end Instantiate_Formal_Subprogram;
8176 ------------------------
8177 -- Instantiate_Object --
8178 ------------------------
8180 function Instantiate_Object
8183 Analyzed_Formal : Node_Id) return List_Id
8185 Acc_Def : Node_Id := Empty;
8186 Act_Assoc : constant Node_Id := Parent (Actual);
8187 Actual_Decl : Node_Id := Empty;
8188 Formal_Id : constant Entity_Id := Defining_Identifier (Formal);
8189 Decl_Node : Node_Id;
8192 List : constant List_Id := New_List;
8193 Loc : constant Source_Ptr := Sloc (Actual);
8194 Orig_Ftyp : constant Entity_Id :=
8195 Etype (Defining_Identifier (Analyzed_Formal));
8196 Subt_Decl : Node_Id := Empty;
8197 Subt_Mark : Node_Id := Empty;
8200 if Present (Subtype_Mark (Formal)) then
8201 Subt_Mark := Subtype_Mark (Formal);
8203 Check_Access_Definition (Formal);
8204 Acc_Def := Access_Definition (Formal);
8207 -- Sloc for error message on missing actual
8209 Error_Msg_Sloc := Sloc (Scope (Defining_Identifier (Analyzed_Formal)));
8211 if Get_Instance_Of (Formal_Id) /= Formal_Id then
8212 Error_Msg_N ("duplicate instantiation of generic parameter", Actual);
8215 Set_Parent (List, Parent (Actual));
8219 if Out_Present (Formal) then
8221 -- An IN OUT generic actual must be a name. The instantiation is a
8222 -- renaming declaration. The actual is the name being renamed. We
8223 -- use the actual directly, rather than a copy, because it is not
8224 -- used further in the list of actuals, and because a copy or a use
8225 -- of relocate_node is incorrect if the instance is nested within a
8226 -- generic. In order to simplify ASIS searches, the Generic_Parent
8227 -- field links the declaration to the generic association.
8232 Instantiation_Node, Formal_Id);
8234 ("\in instantiation of & declared#",
8236 Scope (Defining_Identifier (Analyzed_Formal)));
8237 Abandon_Instantiation (Instantiation_Node);
8240 if Present (Subt_Mark) then
8242 Make_Object_Renaming_Declaration (Loc,
8243 Defining_Identifier => New_Copy (Formal_Id),
8244 Subtype_Mark => New_Copy_Tree (Subt_Mark),
8247 else pragma Assert (Present (Acc_Def));
8249 Make_Object_Renaming_Declaration (Loc,
8250 Defining_Identifier => New_Copy (Formal_Id),
8251 Access_Definition => New_Copy_Tree (Acc_Def),
8255 Set_Corresponding_Generic_Association (Decl_Node, Act_Assoc);
8257 -- The analysis of the actual may produce insert_action nodes, so
8258 -- the declaration must have a context in which to attach them.
8260 Append (Decl_Node, List);
8263 -- Return if the analysis of the actual reported some error
8265 if Etype (Actual) = Any_Type then
8269 -- This check is performed here because Analyze_Object_Renaming will
8270 -- not check it when Comes_From_Source is False. Note though that the
8271 -- check for the actual being the name of an object will be performed
8272 -- in Analyze_Object_Renaming.
8274 if Is_Object_Reference (Actual)
8275 and then Is_Dependent_Component_Of_Mutable_Object (Actual)
8278 ("illegal discriminant-dependent component for in out parameter",
8282 -- The actual has to be resolved in order to check that it is a
8283 -- variable (due to cases such as F(1), where F returns
8284 -- access to an array, and for overloaded prefixes).
8287 Get_Instance_Of (Etype (Defining_Identifier (Analyzed_Formal)));
8289 if Is_Private_Type (Ftyp)
8290 and then not Is_Private_Type (Etype (Actual))
8291 and then (Base_Type (Full_View (Ftyp)) = Base_Type (Etype (Actual))
8292 or else Base_Type (Etype (Actual)) = Ftyp)
8294 -- If the actual has the type of the full view of the formal, or
8295 -- else a non-private subtype of the formal, then the visibility
8296 -- of the formal type has changed. Add to the actuals a subtype
8297 -- declaration that will force the exchange of views in the body
8298 -- of the instance as well.
8301 Make_Subtype_Declaration (Loc,
8302 Defining_Identifier =>
8303 Make_Defining_Identifier (Loc, New_Internal_Name ('P')),
8304 Subtype_Indication => New_Occurrence_Of (Ftyp, Loc));
8306 Prepend (Subt_Decl, List);
8308 Prepend_Elmt (Full_View (Ftyp), Exchanged_Views);
8309 Exchange_Declarations (Ftyp);
8312 Resolve (Actual, Ftyp);
8314 if not Denotes_Variable (Actual) then
8316 ("actual for& must be a variable", Actual, Formal_Id);
8318 elsif Base_Type (Ftyp) /= Base_Type (Etype (Actual)) then
8320 -- Ada 2005 (AI-423): For a generic formal object of mode in out,
8321 -- the type of the actual shall resolve to a specific anonymous
8324 if Ada_Version < Ada_05
8326 Ekind (Base_Type (Ftyp)) /=
8327 E_Anonymous_Access_Type
8329 Ekind (Base_Type (Etype (Actual))) /=
8330 E_Anonymous_Access_Type
8332 Error_Msg_NE ("type of actual does not match type of&",
8337 Note_Possible_Modification (Actual, Sure => True);
8339 -- Check for instantiation of atomic/volatile actual for
8340 -- non-atomic/volatile formal (RM C.6 (12)).
8342 if Is_Atomic_Object (Actual)
8343 and then not Is_Atomic (Orig_Ftyp)
8346 ("cannot instantiate non-atomic formal object " &
8347 "with atomic actual", Actual);
8349 elsif Is_Volatile_Object (Actual)
8350 and then not Is_Volatile (Orig_Ftyp)
8353 ("cannot instantiate non-volatile formal object " &
8354 "with volatile actual", Actual);
8360 -- The instantiation of a generic formal in-parameter is constant
8361 -- declaration. The actual is the expression for that declaration.
8363 if Present (Actual) then
8364 if Present (Subt_Mark) then
8366 else pragma Assert (Present (Acc_Def));
8371 Make_Object_Declaration (Loc,
8372 Defining_Identifier => New_Copy (Formal_Id),
8373 Constant_Present => True,
8374 Null_Exclusion_Present => Null_Exclusion_Present (Formal),
8375 Object_Definition => New_Copy_Tree (Def),
8376 Expression => Actual);
8378 Set_Corresponding_Generic_Association (Decl_Node, Act_Assoc);
8380 -- A generic formal object of a tagged type is defined to be
8381 -- aliased so the new constant must also be treated as aliased.
8384 (Etype (Defining_Identifier (Analyzed_Formal)))
8386 Set_Aliased_Present (Decl_Node);
8389 Append (Decl_Node, List);
8391 -- No need to repeat (pre-)analysis of some expression nodes
8392 -- already handled in Preanalyze_Actuals.
8394 if Nkind (Actual) /= N_Allocator then
8397 -- Return if the analysis of the actual reported some error
8399 if Etype (Actual) = Any_Type then
8405 Typ : constant Entity_Id :=
8407 (Etype (Defining_Identifier (Analyzed_Formal)));
8410 Freeze_Before (Instantiation_Node, Typ);
8412 -- If the actual is an aggregate, perform name resolution on
8413 -- its components (the analysis of an aggregate does not do it)
8414 -- to capture local names that may be hidden if the generic is
8417 if Nkind (Actual) = N_Aggregate then
8418 Preanalyze_And_Resolve (Actual, Typ);
8421 if Is_Limited_Type (Typ)
8422 and then not OK_For_Limited_Init (Actual)
8425 ("initialization not allowed for limited types", Actual);
8426 Explain_Limited_Type (Typ, Actual);
8430 elsif Present (Default_Expression (Formal)) then
8432 -- Use default to construct declaration
8434 if Present (Subt_Mark) then
8436 else pragma Assert (Present (Acc_Def));
8441 Make_Object_Declaration (Sloc (Formal),
8442 Defining_Identifier => New_Copy (Formal_Id),
8443 Constant_Present => True,
8444 Null_Exclusion_Present => Null_Exclusion_Present (Formal),
8445 Object_Definition => New_Copy (Def),
8446 Expression => New_Copy_Tree
8447 (Default_Expression (Formal)));
8449 Append (Decl_Node, List);
8450 Set_Analyzed (Expression (Decl_Node), False);
8455 Instantiation_Node, Formal_Id);
8456 Error_Msg_NE ("\in instantiation of & declared#",
8458 Scope (Defining_Identifier (Analyzed_Formal)));
8461 (Etype (Defining_Identifier (Analyzed_Formal)))
8463 -- Create dummy constant declaration so that instance can be
8464 -- analyzed, to minimize cascaded visibility errors.
8466 if Present (Subt_Mark) then
8468 else pragma Assert (Present (Acc_Def));
8473 Make_Object_Declaration (Loc,
8474 Defining_Identifier => New_Copy (Formal_Id),
8475 Constant_Present => True,
8476 Null_Exclusion_Present => Null_Exclusion_Present (Formal),
8477 Object_Definition => New_Copy (Def),
8479 Make_Attribute_Reference (Sloc (Formal_Id),
8480 Attribute_Name => Name_First,
8481 Prefix => New_Copy (Def)));
8483 Append (Decl_Node, List);
8486 Abandon_Instantiation (Instantiation_Node);
8491 if Nkind (Actual) in N_Has_Entity then
8492 Actual_Decl := Parent (Entity (Actual));
8495 -- Ada 2005 (AI-423): For a formal object declaration with a null
8496 -- exclusion or an access definition that has a null exclusion: If the
8497 -- actual matching the formal object declaration denotes a generic
8498 -- formal object of another generic unit G, and the instantiation
8499 -- containing the actual occurs within the body of G or within the body
8500 -- of a generic unit declared within the declarative region of G, then
8501 -- the declaration of the formal object of G must have a null exclusion.
8502 -- Otherwise, the subtype of the actual matching the formal object
8503 -- declaration shall exclude null.
8505 if Ada_Version >= Ada_05
8506 and then Present (Actual_Decl)
8508 Nkind_In (Actual_Decl, N_Formal_Object_Declaration,
8509 N_Object_Declaration)
8510 and then Nkind (Analyzed_Formal) = N_Formal_Object_Declaration
8511 and then not Has_Null_Exclusion (Actual_Decl)
8512 and then Has_Null_Exclusion (Analyzed_Formal)
8514 Error_Msg_Sloc := Sloc (Analyzed_Formal);
8516 ("actual must exclude null to match generic formal#", Actual);
8520 end Instantiate_Object;
8522 ------------------------------
8523 -- Instantiate_Package_Body --
8524 ------------------------------
8526 procedure Instantiate_Package_Body
8527 (Body_Info : Pending_Body_Info;
8528 Inlined_Body : Boolean := False;
8529 Body_Optional : Boolean := False)
8531 Act_Decl : constant Node_Id := Body_Info.Act_Decl;
8532 Inst_Node : constant Node_Id := Body_Info.Inst_Node;
8533 Loc : constant Source_Ptr := Sloc (Inst_Node);
8535 Gen_Id : constant Node_Id := Name (Inst_Node);
8536 Gen_Unit : constant Entity_Id := Get_Generic_Entity (Inst_Node);
8537 Gen_Decl : constant Node_Id := Unit_Declaration_Node (Gen_Unit);
8538 Act_Spec : constant Node_Id := Specification (Act_Decl);
8539 Act_Decl_Id : constant Entity_Id := Defining_Entity (Act_Spec);
8541 Act_Body_Name : Node_Id;
8543 Gen_Body_Id : Node_Id;
8545 Act_Body_Id : Entity_Id;
8547 Parent_Installed : Boolean := False;
8548 Save_Style_Check : constant Boolean := Style_Check;
8551 Gen_Body_Id := Corresponding_Body (Gen_Decl);
8553 -- The instance body may already have been processed, as the parent of
8554 -- another instance that is inlined (Load_Parent_Of_Generic).
8556 if Present (Corresponding_Body (Instance_Spec (Inst_Node))) then
8560 Expander_Mode_Save_And_Set (Body_Info.Expander_Status);
8562 -- Re-establish the state of information on which checks are suppressed.
8563 -- This information was set in Body_Info at the point of instantiation,
8564 -- and now we restore it so that the instance is compiled using the
8565 -- check status at the instantiation (RM 11.5 (7.2/2), AI95-00224-01).
8567 Local_Suppress_Stack_Top := Body_Info.Local_Suppress_Stack_Top;
8568 Scope_Suppress := Body_Info.Scope_Suppress;
8570 if No (Gen_Body_Id) then
8571 Load_Parent_Of_Generic
8572 (Inst_Node, Specification (Gen_Decl), Body_Optional);
8573 Gen_Body_Id := Corresponding_Body (Gen_Decl);
8576 -- Establish global variable for sloc adjustment and for error recovery
8578 Instantiation_Node := Inst_Node;
8580 if Present (Gen_Body_Id) then
8581 Save_Env (Gen_Unit, Act_Decl_Id);
8582 Style_Check := False;
8583 Current_Sem_Unit := Body_Info.Current_Sem_Unit;
8585 Gen_Body := Unit_Declaration_Node (Gen_Body_Id);
8587 Create_Instantiation_Source
8588 (Inst_Node, Gen_Body_Id, False, S_Adjustment);
8592 (Original_Node (Gen_Body), Empty, Instantiating => True);
8594 -- Build new name (possibly qualified) for body declaration
8596 Act_Body_Id := New_Copy (Act_Decl_Id);
8598 -- Some attributes of spec entity are not inherited by body entity
8600 Set_Handler_Records (Act_Body_Id, No_List);
8602 if Nkind (Defining_Unit_Name (Act_Spec)) =
8603 N_Defining_Program_Unit_Name
8606 Make_Defining_Program_Unit_Name (Loc,
8607 Name => New_Copy_Tree (Name (Defining_Unit_Name (Act_Spec))),
8608 Defining_Identifier => Act_Body_Id);
8610 Act_Body_Name := Act_Body_Id;
8613 Set_Defining_Unit_Name (Act_Body, Act_Body_Name);
8615 Set_Corresponding_Spec (Act_Body, Act_Decl_Id);
8616 Check_Generic_Actuals (Act_Decl_Id, False);
8618 -- If it is a child unit, make the parent instance (which is an
8619 -- instance of the parent of the generic) visible. The parent
8620 -- instance is the prefix of the name of the generic unit.
8622 if Ekind (Scope (Gen_Unit)) = E_Generic_Package
8623 and then Nkind (Gen_Id) = N_Expanded_Name
8625 Install_Parent (Entity (Prefix (Gen_Id)), In_Body => True);
8626 Parent_Installed := True;
8628 elsif Is_Child_Unit (Gen_Unit) then
8629 Install_Parent (Scope (Gen_Unit), In_Body => True);
8630 Parent_Installed := True;
8633 -- If the instantiation is a library unit, and this is the main unit,
8634 -- then build the resulting compilation unit nodes for the instance.
8635 -- If this is a compilation unit but it is not the main unit, then it
8636 -- is the body of a unit in the context, that is being compiled
8637 -- because it is encloses some inlined unit or another generic unit
8638 -- being instantiated. In that case, this body is not part of the
8639 -- current compilation, and is not attached to the tree, but its
8640 -- parent must be set for analysis.
8642 if Nkind (Parent (Inst_Node)) = N_Compilation_Unit then
8644 -- Replace instance node with body of instance, and create new
8645 -- node for corresponding instance declaration.
8647 Build_Instance_Compilation_Unit_Nodes
8648 (Inst_Node, Act_Body, Act_Decl);
8649 Analyze (Inst_Node);
8651 if Parent (Inst_Node) = Cunit (Main_Unit) then
8653 -- If the instance is a child unit itself, then set the scope
8654 -- of the expanded body to be the parent of the instantiation
8655 -- (ensuring that the fully qualified name will be generated
8656 -- for the elaboration subprogram).
8658 if Nkind (Defining_Unit_Name (Act_Spec)) =
8659 N_Defining_Program_Unit_Name
8662 (Defining_Entity (Inst_Node), Scope (Act_Decl_Id));
8666 -- Case where instantiation is not a library unit
8669 -- If this is an early instantiation, i.e. appears textually
8670 -- before the corresponding body and must be elaborated first,
8671 -- indicate that the body instance is to be delayed.
8673 Install_Body (Act_Body, Inst_Node, Gen_Body, Gen_Decl);
8675 -- Now analyze the body. We turn off all checks if this is an
8676 -- internal unit, since there is no reason to have checks on for
8677 -- any predefined run-time library code. All such code is designed
8678 -- to be compiled with checks off.
8680 -- Note that we do NOT apply this criterion to children of GNAT
8681 -- (or on VMS, children of DEC). The latter units must suppress
8682 -- checks explicitly if this is needed.
8684 if Is_Predefined_File_Name
8685 (Unit_File_Name (Get_Source_Unit (Gen_Decl)))
8687 Analyze (Act_Body, Suppress => All_Checks);
8693 Inherit_Context (Gen_Body, Inst_Node);
8695 -- Remove the parent instances if they have been placed on the scope
8696 -- stack to compile the body.
8698 if Parent_Installed then
8699 Remove_Parent (In_Body => True);
8702 Restore_Private_Views (Act_Decl_Id);
8704 -- Remove the current unit from visibility if this is an instance
8705 -- that is not elaborated on the fly for inlining purposes.
8707 if not Inlined_Body then
8708 Set_Is_Immediately_Visible (Act_Decl_Id, False);
8712 Style_Check := Save_Style_Check;
8714 -- If we have no body, and the unit requires a body, then complain. This
8715 -- complaint is suppressed if we have detected other errors (since a
8716 -- common reason for missing the body is that it had errors).
8718 elsif Unit_Requires_Body (Gen_Unit)
8719 and then not Body_Optional
8721 if Serious_Errors_Detected = 0 then
8723 ("cannot find body of generic package &", Inst_Node, Gen_Unit);
8725 -- Don't attempt to perform any cleanup actions if some other error
8726 -- was already detected, since this can cause blowups.
8732 -- Case of package that does not need a body
8735 -- If the instantiation of the declaration is a library unit, rewrite
8736 -- the original package instantiation as a package declaration in the
8737 -- compilation unit node.
8739 if Nkind (Parent (Inst_Node)) = N_Compilation_Unit then
8740 Set_Parent_Spec (Act_Decl, Parent_Spec (Inst_Node));
8741 Rewrite (Inst_Node, Act_Decl);
8743 -- Generate elaboration entity, in case spec has elaboration code.
8744 -- This cannot be done when the instance is analyzed, because it
8745 -- is not known yet whether the body exists.
8747 Set_Elaboration_Entity_Required (Act_Decl_Id, False);
8748 Build_Elaboration_Entity (Parent (Inst_Node), Act_Decl_Id);
8750 -- If the instantiation is not a library unit, then append the
8751 -- declaration to the list of implicitly generated entities, unless
8752 -- it is already a list member which means that it was already
8755 elsif not Is_List_Member (Act_Decl) then
8756 Mark_Rewrite_Insertion (Act_Decl);
8757 Insert_Before (Inst_Node, Act_Decl);
8761 Expander_Mode_Restore;
8762 end Instantiate_Package_Body;
8764 ---------------------------------
8765 -- Instantiate_Subprogram_Body --
8766 ---------------------------------
8768 procedure Instantiate_Subprogram_Body
8769 (Body_Info : Pending_Body_Info;
8770 Body_Optional : Boolean := False)
8772 Act_Decl : constant Node_Id := Body_Info.Act_Decl;
8773 Inst_Node : constant Node_Id := Body_Info.Inst_Node;
8774 Loc : constant Source_Ptr := Sloc (Inst_Node);
8775 Gen_Id : constant Node_Id := Name (Inst_Node);
8776 Gen_Unit : constant Entity_Id := Get_Generic_Entity (Inst_Node);
8777 Gen_Decl : constant Node_Id := Unit_Declaration_Node (Gen_Unit);
8778 Anon_Id : constant Entity_Id :=
8779 Defining_Unit_Name (Specification (Act_Decl));
8780 Pack_Id : constant Entity_Id :=
8781 Defining_Unit_Name (Parent (Act_Decl));
8784 Gen_Body_Id : Node_Id;
8786 Pack_Body : Node_Id;
8787 Prev_Formal : Entity_Id;
8789 Unit_Renaming : Node_Id;
8791 Parent_Installed : Boolean := False;
8792 Save_Style_Check : constant Boolean := Style_Check;
8795 Gen_Body_Id := Corresponding_Body (Gen_Decl);
8797 -- Subprogram body may have been created already because of an inline
8798 -- pragma, or because of multiple elaborations of the enclosing package
8799 -- when several instances of the subprogram appear in the main unit.
8801 if Present (Corresponding_Body (Act_Decl)) then
8805 Expander_Mode_Save_And_Set (Body_Info.Expander_Status);
8807 -- Re-establish the state of information on which checks are suppressed.
8808 -- This information was set in Body_Info at the point of instantiation,
8809 -- and now we restore it so that the instance is compiled using the
8810 -- check status at the instantiation (RM 11.5 (7.2/2), AI95-00224-01).
8812 Local_Suppress_Stack_Top := Body_Info.Local_Suppress_Stack_Top;
8813 Scope_Suppress := Body_Info.Scope_Suppress;
8815 if No (Gen_Body_Id) then
8817 -- For imported generic subprogram, no body to compile, complete
8818 -- the spec entity appropriately.
8820 if Is_Imported (Gen_Unit) then
8821 Set_Is_Imported (Anon_Id);
8822 Set_First_Rep_Item (Anon_Id, First_Rep_Item (Gen_Unit));
8823 Set_Interface_Name (Anon_Id, Interface_Name (Gen_Unit));
8824 Set_Convention (Anon_Id, Convention (Gen_Unit));
8825 Set_Has_Completion (Anon_Id);
8828 -- For other cases, compile the body
8831 Load_Parent_Of_Generic
8832 (Inst_Node, Specification (Gen_Decl), Body_Optional);
8833 Gen_Body_Id := Corresponding_Body (Gen_Decl);
8837 Instantiation_Node := Inst_Node;
8839 if Present (Gen_Body_Id) then
8840 Gen_Body := Unit_Declaration_Node (Gen_Body_Id);
8842 if Nkind (Gen_Body) = N_Subprogram_Body_Stub then
8844 -- Either body is not present, or context is non-expanding, as
8845 -- when compiling a subunit. Mark the instance as completed, and
8846 -- diagnose a missing body when needed.
8849 and then Operating_Mode = Generate_Code
8852 ("missing proper body for instantiation", Gen_Body);
8855 Set_Has_Completion (Anon_Id);
8859 Save_Env (Gen_Unit, Anon_Id);
8860 Style_Check := False;
8861 Current_Sem_Unit := Body_Info.Current_Sem_Unit;
8862 Create_Instantiation_Source
8870 (Original_Node (Gen_Body), Empty, Instantiating => True);
8872 -- Create proper defining name for the body, to correspond to
8873 -- the one in the spec.
8875 Set_Defining_Unit_Name (Specification (Act_Body),
8876 Make_Defining_Identifier
8877 (Sloc (Defining_Entity (Inst_Node)), Chars (Anon_Id)));
8878 Set_Corresponding_Spec (Act_Body, Anon_Id);
8879 Set_Has_Completion (Anon_Id);
8880 Check_Generic_Actuals (Pack_Id, False);
8882 -- Generate a reference to link the visible subprogram instance to
8883 -- the generic body, which for navigation purposes is the only
8884 -- available source for the instance.
8887 (Related_Instance (Pack_Id),
8888 Gen_Body_Id, 'b', Set_Ref => False, Force => True);
8890 -- If it is a child unit, make the parent instance (which is an
8891 -- instance of the parent of the generic) visible. The parent
8892 -- instance is the prefix of the name of the generic unit.
8894 if Ekind (Scope (Gen_Unit)) = E_Generic_Package
8895 and then Nkind (Gen_Id) = N_Expanded_Name
8897 Install_Parent (Entity (Prefix (Gen_Id)), In_Body => True);
8898 Parent_Installed := True;
8900 elsif Is_Child_Unit (Gen_Unit) then
8901 Install_Parent (Scope (Gen_Unit), In_Body => True);
8902 Parent_Installed := True;
8905 -- Inside its body, a reference to the generic unit is a reference
8906 -- to the instance. The corresponding renaming is the first
8907 -- declaration in the body.
8910 Make_Subprogram_Renaming_Declaration (Loc,
8913 Specification (Original_Node (Gen_Body)),
8915 Instantiating => True),
8916 Name => New_Occurrence_Of (Anon_Id, Loc));
8918 -- If there is a formal subprogram with the same name as the unit
8919 -- itself, do not add this renaming declaration. This is a temporary
8920 -- fix for one ACVC test. ???
8922 Prev_Formal := First_Entity (Pack_Id);
8923 while Present (Prev_Formal) loop
8924 if Chars (Prev_Formal) = Chars (Gen_Unit)
8925 and then Is_Overloadable (Prev_Formal)
8930 Next_Entity (Prev_Formal);
8933 if Present (Prev_Formal) then
8934 Decls := New_List (Act_Body);
8936 Decls := New_List (Unit_Renaming, Act_Body);
8939 -- The subprogram body is placed in the body of a dummy package body,
8940 -- whose spec contains the subprogram declaration as well as the
8941 -- renaming declarations for the generic parameters.
8943 Pack_Body := Make_Package_Body (Loc,
8944 Defining_Unit_Name => New_Copy (Pack_Id),
8945 Declarations => Decls);
8947 Set_Corresponding_Spec (Pack_Body, Pack_Id);
8949 -- If the instantiation is a library unit, then build resulting
8950 -- compilation unit nodes for the instance. The declaration of
8951 -- the enclosing package is the grandparent of the subprogram
8952 -- declaration. First replace the instantiation node as the unit
8953 -- of the corresponding compilation.
8955 if Nkind (Parent (Inst_Node)) = N_Compilation_Unit then
8956 if Parent (Inst_Node) = Cunit (Main_Unit) then
8957 Set_Unit (Parent (Inst_Node), Inst_Node);
8958 Build_Instance_Compilation_Unit_Nodes
8959 (Inst_Node, Pack_Body, Parent (Parent (Act_Decl)));
8960 Analyze (Inst_Node);
8962 Set_Parent (Pack_Body, Parent (Inst_Node));
8963 Analyze (Pack_Body);
8967 Insert_Before (Inst_Node, Pack_Body);
8968 Mark_Rewrite_Insertion (Pack_Body);
8969 Analyze (Pack_Body);
8971 if Expander_Active then
8972 Freeze_Subprogram_Body (Inst_Node, Gen_Body, Pack_Id);
8976 Inherit_Context (Gen_Body, Inst_Node);
8978 Restore_Private_Views (Pack_Id, False);
8980 if Parent_Installed then
8981 Remove_Parent (In_Body => True);
8985 Style_Check := Save_Style_Check;
8987 -- Body not found. Error was emitted already. If there were no previous
8988 -- errors, this may be an instance whose scope is a premature instance.
8989 -- In that case we must insure that the (legal) program does raise
8990 -- program error if executed. We generate a subprogram body for this
8991 -- purpose. See DEC ac30vso.
8993 -- Should not reference proprietary DEC tests in comments ???
8995 elsif Serious_Errors_Detected = 0
8996 and then Nkind (Parent (Inst_Node)) /= N_Compilation_Unit
8998 if Body_Optional then
9001 elsif Ekind (Anon_Id) = E_Procedure then
9003 Make_Subprogram_Body (Loc,
9005 Make_Procedure_Specification (Loc,
9006 Defining_Unit_Name =>
9007 Make_Defining_Identifier (Loc, Chars (Anon_Id)),
9008 Parameter_Specifications =>
9010 (Parameter_Specifications (Parent (Anon_Id)))),
9012 Declarations => Empty_List,
9013 Handled_Statement_Sequence =>
9014 Make_Handled_Sequence_Of_Statements (Loc,
9017 Make_Raise_Program_Error (Loc,
9019 PE_Access_Before_Elaboration))));
9023 Make_Raise_Program_Error (Loc,
9024 Reason => PE_Access_Before_Elaboration);
9026 Set_Etype (Ret_Expr, (Etype (Anon_Id)));
9027 Set_Analyzed (Ret_Expr);
9030 Make_Subprogram_Body (Loc,
9032 Make_Function_Specification (Loc,
9033 Defining_Unit_Name =>
9034 Make_Defining_Identifier (Loc, Chars (Anon_Id)),
9035 Parameter_Specifications =>
9037 (Parameter_Specifications (Parent (Anon_Id))),
9038 Result_Definition =>
9039 New_Occurrence_Of (Etype (Anon_Id), Loc)),
9041 Declarations => Empty_List,
9042 Handled_Statement_Sequence =>
9043 Make_Handled_Sequence_Of_Statements (Loc,
9046 (Make_Simple_Return_Statement (Loc, Ret_Expr))));
9049 Pack_Body := Make_Package_Body (Loc,
9050 Defining_Unit_Name => New_Copy (Pack_Id),
9051 Declarations => New_List (Act_Body));
9053 Insert_After (Inst_Node, Pack_Body);
9054 Set_Corresponding_Spec (Pack_Body, Pack_Id);
9055 Analyze (Pack_Body);
9058 Expander_Mode_Restore;
9059 end Instantiate_Subprogram_Body;
9061 ----------------------
9062 -- Instantiate_Type --
9063 ----------------------
9065 function Instantiate_Type
9068 Analyzed_Formal : Node_Id;
9069 Actual_Decls : List_Id) return List_Id
9071 Gen_T : constant Entity_Id := Defining_Identifier (Formal);
9072 A_Gen_T : constant Entity_Id :=
9073 Defining_Identifier (Analyzed_Formal);
9074 Ancestor : Entity_Id := Empty;
9075 Def : constant Node_Id := Formal_Type_Definition (Formal);
9077 Decl_Node : Node_Id;
9078 Decl_Nodes : List_Id;
9082 procedure Validate_Array_Type_Instance;
9083 procedure Validate_Access_Subprogram_Instance;
9084 procedure Validate_Access_Type_Instance;
9085 procedure Validate_Derived_Type_Instance;
9086 procedure Validate_Derived_Interface_Type_Instance;
9087 procedure Validate_Interface_Type_Instance;
9088 procedure Validate_Private_Type_Instance;
9089 -- These procedures perform validation tests for the named case
9091 function Subtypes_Match (Gen_T, Act_T : Entity_Id) return Boolean;
9092 -- Check that base types are the same and that the subtypes match
9093 -- statically. Used in several of the above.
9095 --------------------
9096 -- Subtypes_Match --
9097 --------------------
9099 function Subtypes_Match (Gen_T, Act_T : Entity_Id) return Boolean is
9100 T : constant Entity_Id := Get_Instance_Of (Gen_T);
9103 return (Base_Type (T) = Base_Type (Act_T)
9104 and then Subtypes_Statically_Match (T, Act_T))
9106 or else (Is_Class_Wide_Type (Gen_T)
9107 and then Is_Class_Wide_Type (Act_T)
9110 (Get_Instance_Of (Root_Type (Gen_T)),
9114 ((Ekind (Gen_T) = E_Anonymous_Access_Subprogram_Type
9115 or else Ekind (Gen_T) = E_Anonymous_Access_Type)
9116 and then Ekind (Act_T) = Ekind (Gen_T)
9118 Subtypes_Statically_Match
9119 (Designated_Type (Gen_T), Designated_Type (Act_T)));
9122 -----------------------------------------
9123 -- Validate_Access_Subprogram_Instance --
9124 -----------------------------------------
9126 procedure Validate_Access_Subprogram_Instance is
9128 if not Is_Access_Type (Act_T)
9129 or else Ekind (Designated_Type (Act_T)) /= E_Subprogram_Type
9132 ("expect access type in instantiation of &", Actual, Gen_T);
9133 Abandon_Instantiation (Actual);
9136 Check_Mode_Conformant
9137 (Designated_Type (Act_T),
9138 Designated_Type (A_Gen_T),
9142 if Ekind (Base_Type (Act_T)) = E_Access_Protected_Subprogram_Type then
9143 if Ekind (A_Gen_T) = E_Access_Subprogram_Type then
9145 ("protected access type not allowed for formal &",
9149 elsif Ekind (A_Gen_T) = E_Access_Protected_Subprogram_Type then
9151 ("expect protected access type for formal &",
9154 end Validate_Access_Subprogram_Instance;
9156 -----------------------------------
9157 -- Validate_Access_Type_Instance --
9158 -----------------------------------
9160 procedure Validate_Access_Type_Instance is
9161 Desig_Type : constant Entity_Id :=
9162 Find_Actual_Type (Designated_Type (A_Gen_T), A_Gen_T);
9163 Desig_Act : Entity_Id;
9166 if not Is_Access_Type (Act_T) then
9168 ("expect access type in instantiation of &", Actual, Gen_T);
9169 Abandon_Instantiation (Actual);
9172 if Is_Access_Constant (A_Gen_T) then
9173 if not Is_Access_Constant (Act_T) then
9175 ("actual type must be access-to-constant type", Actual);
9176 Abandon_Instantiation (Actual);
9179 if Is_Access_Constant (Act_T) then
9181 ("actual type must be access-to-variable type", Actual);
9182 Abandon_Instantiation (Actual);
9184 elsif Ekind (A_Gen_T) = E_General_Access_Type
9185 and then Ekind (Base_Type (Act_T)) /= E_General_Access_Type
9187 Error_Msg_N ("actual must be general access type!", Actual);
9188 Error_Msg_NE ("add ALL to }!", Actual, Act_T);
9189 Abandon_Instantiation (Actual);
9193 -- The designated subtypes, that is to say the subtypes introduced
9194 -- by an access type declaration (and not by a subtype declaration)
9197 Desig_Act := Designated_Type (Base_Type (Act_T));
9199 -- The designated type may have been introduced through a limited_
9200 -- with clause, in which case retrieve the non-limited view. This
9201 -- applies to incomplete types as well as to class-wide types.
9203 if From_With_Type (Desig_Act) then
9204 Desig_Act := Available_View (Desig_Act);
9207 if not Subtypes_Match
9208 (Desig_Type, Desig_Act) then
9210 ("designated type of actual does not match that of formal &",
9212 Abandon_Instantiation (Actual);
9214 elsif Is_Access_Type (Designated_Type (Act_T))
9215 and then Is_Constrained (Designated_Type (Designated_Type (Act_T)))
9217 Is_Constrained (Designated_Type (Desig_Type))
9220 ("designated type of actual does not match that of formal &",
9222 Abandon_Instantiation (Actual);
9225 -- Ada 2005: null-exclusion indicators of the two types must agree
9227 if Can_Never_Be_Null (A_Gen_T) /= Can_Never_Be_Null (Act_T) then
9229 ("non null exclusion of actual and formal & do not match",
9232 end Validate_Access_Type_Instance;
9234 ----------------------------------
9235 -- Validate_Array_Type_Instance --
9236 ----------------------------------
9238 procedure Validate_Array_Type_Instance is
9243 function Formal_Dimensions return Int;
9244 -- Count number of dimensions in array type formal
9246 -----------------------
9247 -- Formal_Dimensions --
9248 -----------------------
9250 function Formal_Dimensions return Int is
9255 if Nkind (Def) = N_Constrained_Array_Definition then
9256 Index := First (Discrete_Subtype_Definitions (Def));
9258 Index := First (Subtype_Marks (Def));
9261 while Present (Index) loop
9267 end Formal_Dimensions;
9269 -- Start of processing for Validate_Array_Type_Instance
9272 if not Is_Array_Type (Act_T) then
9274 ("expect array type in instantiation of &", Actual, Gen_T);
9275 Abandon_Instantiation (Actual);
9277 elsif Nkind (Def) = N_Constrained_Array_Definition then
9278 if not (Is_Constrained (Act_T)) then
9280 ("expect constrained array in instantiation of &",
9282 Abandon_Instantiation (Actual);
9286 if Is_Constrained (Act_T) then
9288 ("expect unconstrained array in instantiation of &",
9290 Abandon_Instantiation (Actual);
9294 if Formal_Dimensions /= Number_Dimensions (Act_T) then
9296 ("dimensions of actual do not match formal &", Actual, Gen_T);
9297 Abandon_Instantiation (Actual);
9300 I1 := First_Index (A_Gen_T);
9301 I2 := First_Index (Act_T);
9302 for J in 1 .. Formal_Dimensions loop
9304 -- If the indices of the actual were given by a subtype_mark,
9305 -- the index was transformed into a range attribute. Retrieve
9306 -- the original type mark for checking.
9308 if Is_Entity_Name (Original_Node (I2)) then
9309 T2 := Entity (Original_Node (I2));
9314 if not Subtypes_Match
9315 (Find_Actual_Type (Etype (I1), A_Gen_T), T2)
9318 ("index types of actual do not match those of formal &",
9320 Abandon_Instantiation (Actual);
9327 -- Check matching subtypes. Note that there are complex visibility
9328 -- issues when the generic is a child unit and some aspect of the
9329 -- generic type is declared in a parent unit of the generic. We do
9330 -- the test to handle this special case only after a direct check
9331 -- for static matching has failed.
9334 (Component_Type (A_Gen_T), Component_Type (Act_T))
9335 or else Subtypes_Match
9336 (Find_Actual_Type (Component_Type (A_Gen_T), A_Gen_T),
9337 Component_Type (Act_T))
9342 ("component subtype of actual does not match that of formal &",
9344 Abandon_Instantiation (Actual);
9347 if Has_Aliased_Components (A_Gen_T)
9348 and then not Has_Aliased_Components (Act_T)
9351 ("actual must have aliased components to match formal type &",
9354 end Validate_Array_Type_Instance;
9356 -----------------------------------------------
9357 -- Validate_Derived_Interface_Type_Instance --
9358 -----------------------------------------------
9360 procedure Validate_Derived_Interface_Type_Instance is
9361 Par : constant Entity_Id := Entity (Subtype_Indication (Def));
9365 -- First apply interface instance checks
9367 Validate_Interface_Type_Instance;
9369 -- Verify that immediate parent interface is an ancestor of
9373 and then not Interface_Present_In_Ancestor (Act_T, Par)
9376 ("interface actual must include progenitor&", Actual, Par);
9379 -- Now verify that the actual includes all other ancestors of
9382 Elmt := First_Elmt (Interfaces (A_Gen_T));
9383 while Present (Elmt) loop
9384 if not Interface_Present_In_Ancestor
9385 (Act_T, Get_Instance_Of (Node (Elmt)))
9388 ("interface actual must include progenitor&",
9389 Actual, Node (Elmt));
9394 end Validate_Derived_Interface_Type_Instance;
9396 ------------------------------------
9397 -- Validate_Derived_Type_Instance --
9398 ------------------------------------
9400 procedure Validate_Derived_Type_Instance is
9401 Actual_Discr : Entity_Id;
9402 Ancestor_Discr : Entity_Id;
9405 -- If the parent type in the generic declaration is itself a previous
9406 -- formal type, then it is local to the generic and absent from the
9407 -- analyzed generic definition. In that case the ancestor is the
9408 -- instance of the formal (which must have been instantiated
9409 -- previously), unless the ancestor is itself a formal derived type.
9410 -- In this latter case (which is the subject of Corrigendum 8652/0038
9411 -- (AI-202) the ancestor of the formals is the ancestor of its
9412 -- parent. Otherwise, the analyzed generic carries the parent type.
9413 -- If the parent type is defined in a previous formal package, then
9414 -- the scope of that formal package is that of the generic type
9415 -- itself, and it has already been mapped into the corresponding type
9416 -- in the actual package.
9418 -- Common case: parent type defined outside of the generic
9420 if Is_Entity_Name (Subtype_Mark (Def))
9421 and then Present (Entity (Subtype_Mark (Def)))
9423 Ancestor := Get_Instance_Of (Entity (Subtype_Mark (Def)));
9425 -- Check whether parent is defined in a previous formal package
9428 Scope (Scope (Base_Type (Etype (A_Gen_T)))) = Scope (A_Gen_T)
9431 Get_Instance_Of (Base_Type (Etype (A_Gen_T)));
9433 -- The type may be a local derivation, or a type extension of a
9434 -- previous formal, or of a formal of a parent package.
9436 elsif Is_Derived_Type (Get_Instance_Of (A_Gen_T))
9438 Ekind (Get_Instance_Of (A_Gen_T)) = E_Record_Type_With_Private
9440 -- Check whether the parent is another derived formal type in the
9441 -- same generic unit.
9443 if Etype (A_Gen_T) /= A_Gen_T
9444 and then Is_Generic_Type (Etype (A_Gen_T))
9445 and then Scope (Etype (A_Gen_T)) = Scope (A_Gen_T)
9446 and then Etype (Etype (A_Gen_T)) /= Etype (A_Gen_T)
9448 -- Locate ancestor of parent from the subtype declaration
9449 -- created for the actual.
9455 Decl := First (Actual_Decls);
9456 while Present (Decl) loop
9457 if Nkind (Decl) = N_Subtype_Declaration
9458 and then Chars (Defining_Identifier (Decl)) =
9459 Chars (Etype (A_Gen_T))
9461 Ancestor := Generic_Parent_Type (Decl);
9469 pragma Assert (Present (Ancestor));
9473 Get_Instance_Of (Base_Type (Get_Instance_Of (A_Gen_T)));
9477 Ancestor := Get_Instance_Of (Etype (Base_Type (A_Gen_T)));
9480 -- If the formal derived type has pragma Preelaborable_Initialization
9481 -- then the actual type must have preelaborable initialization.
9483 if Known_To_Have_Preelab_Init (A_Gen_T)
9484 and then not Has_Preelaborable_Initialization (Act_T)
9487 ("actual for & must have preelaborable initialization",
9491 -- Ada 2005 (AI-251)
9493 if Ada_Version >= Ada_05
9494 and then Is_Interface (Ancestor)
9496 if not Interface_Present_In_Ancestor (Act_T, Ancestor) then
9498 ("(Ada 2005) expected type implementing & in instantiation",
9502 elsif not Is_Ancestor (Base_Type (Ancestor), Act_T) then
9504 ("expect type derived from & in instantiation",
9505 Actual, First_Subtype (Ancestor));
9506 Abandon_Instantiation (Actual);
9509 -- Ada 2005 (AI-443): Synchronized formal derived type checks. Note
9510 -- that the formal type declaration has been rewritten as a private
9513 if Ada_Version >= Ada_05
9514 and then Nkind (Parent (A_Gen_T)) = N_Private_Extension_Declaration
9515 and then Synchronized_Present (Parent (A_Gen_T))
9517 -- The actual must be a synchronized tagged type
9519 if not Is_Tagged_Type (Act_T) then
9521 ("actual of synchronized type must be tagged", Actual);
9522 Abandon_Instantiation (Actual);
9524 elsif Nkind (Parent (Act_T)) = N_Full_Type_Declaration
9525 and then Nkind (Type_Definition (Parent (Act_T))) =
9526 N_Derived_Type_Definition
9527 and then not Synchronized_Present (Type_Definition
9531 ("actual of synchronized type must be synchronized", Actual);
9532 Abandon_Instantiation (Actual);
9536 -- Perform atomic/volatile checks (RM C.6(12))
9538 if Is_Atomic (Act_T) and then not Is_Atomic (Ancestor) then
9540 ("cannot have atomic actual type for non-atomic formal type",
9543 elsif Is_Volatile (Act_T)
9544 and then not Is_Volatile (Ancestor)
9545 and then Is_By_Reference_Type (Ancestor)
9548 ("cannot have volatile actual type for non-volatile formal type",
9552 -- It should not be necessary to check for unknown discriminants on
9553 -- Formal, but for some reason Has_Unknown_Discriminants is false for
9554 -- A_Gen_T, so Is_Indefinite_Subtype incorrectly returns False. This
9555 -- needs fixing. ???
9557 if not Is_Indefinite_Subtype (A_Gen_T)
9558 and then not Unknown_Discriminants_Present (Formal)
9559 and then Is_Indefinite_Subtype (Act_T)
9562 ("actual subtype must be constrained", Actual);
9563 Abandon_Instantiation (Actual);
9566 if not Unknown_Discriminants_Present (Formal) then
9567 if Is_Constrained (Ancestor) then
9568 if not Is_Constrained (Act_T) then
9570 ("actual subtype must be constrained", Actual);
9571 Abandon_Instantiation (Actual);
9574 -- Ancestor is unconstrained, Check if generic formal and actual
9575 -- agree on constrainedness. The check only applies to array types
9576 -- and discriminated types.
9578 elsif Is_Constrained (Act_T) then
9579 if Ekind (Ancestor) = E_Access_Type
9581 (not Is_Constrained (A_Gen_T)
9582 and then Is_Composite_Type (A_Gen_T))
9585 ("actual subtype must be unconstrained", Actual);
9586 Abandon_Instantiation (Actual);
9589 -- A class-wide type is only allowed if the formal has unknown
9592 elsif Is_Class_Wide_Type (Act_T)
9593 and then not Has_Unknown_Discriminants (Ancestor)
9596 ("actual for & cannot be a class-wide type", Actual, Gen_T);
9597 Abandon_Instantiation (Actual);
9599 -- Otherwise, the formal and actual shall have the same number
9600 -- of discriminants and each discriminant of the actual must
9601 -- correspond to a discriminant of the formal.
9603 elsif Has_Discriminants (Act_T)
9604 and then not Has_Unknown_Discriminants (Act_T)
9605 and then Has_Discriminants (Ancestor)
9607 Actual_Discr := First_Discriminant (Act_T);
9608 Ancestor_Discr := First_Discriminant (Ancestor);
9609 while Present (Actual_Discr)
9610 and then Present (Ancestor_Discr)
9612 if Base_Type (Act_T) /= Base_Type (Ancestor) and then
9613 No (Corresponding_Discriminant (Actual_Discr))
9616 ("discriminant & does not correspond " &
9617 "to ancestor discriminant", Actual, Actual_Discr);
9618 Abandon_Instantiation (Actual);
9621 Next_Discriminant (Actual_Discr);
9622 Next_Discriminant (Ancestor_Discr);
9625 if Present (Actual_Discr) or else Present (Ancestor_Discr) then
9627 ("actual for & must have same number of discriminants",
9629 Abandon_Instantiation (Actual);
9632 -- This case should be caught by the earlier check for
9633 -- constrainedness, but the check here is added for completeness.
9635 elsif Has_Discriminants (Act_T)
9636 and then not Has_Unknown_Discriminants (Act_T)
9639 ("actual for & must not have discriminants", Actual, Gen_T);
9640 Abandon_Instantiation (Actual);
9642 elsif Has_Discriminants (Ancestor) then
9644 ("actual for & must have known discriminants", Actual, Gen_T);
9645 Abandon_Instantiation (Actual);
9648 if not Subtypes_Statically_Compatible (Act_T, Ancestor) then
9650 ("constraint on actual is incompatible with formal", Actual);
9651 Abandon_Instantiation (Actual);
9655 -- If the formal and actual types are abstract, check that there
9656 -- are no abstract primitives of the actual type that correspond to
9657 -- nonabstract primitives of the formal type (second sentence of
9660 if Is_Abstract_Type (A_Gen_T) and then Is_Abstract_Type (Act_T) then
9661 Check_Abstract_Primitives : declare
9662 Gen_Prims : constant Elist_Id :=
9663 Primitive_Operations (A_Gen_T);
9665 Gen_Subp : Entity_Id;
9666 Anc_Subp : Entity_Id;
9667 Anc_Formal : Entity_Id;
9668 Anc_F_Type : Entity_Id;
9670 Act_Prims : constant Elist_Id := Primitive_Operations (Act_T);
9672 Act_Subp : Entity_Id;
9673 Act_Formal : Entity_Id;
9674 Act_F_Type : Entity_Id;
9676 Subprograms_Correspond : Boolean;
9678 function Is_Tagged_Ancestor (T1, T2 : Entity_Id) return Boolean;
9679 -- Returns true if T2 is derived directly or indirectly from
9680 -- T1, including derivations from interfaces. T1 and T2 are
9681 -- required to be specific tagged base types.
9683 ------------------------
9684 -- Is_Tagged_Ancestor --
9685 ------------------------
9687 function Is_Tagged_Ancestor (T1, T2 : Entity_Id) return Boolean
9689 Intfc_Elmt : Elmt_Id;
9692 -- The predicate is satisfied if the types are the same
9697 -- If we've reached the top of the derivation chain then
9698 -- we know that T1 is not an ancestor of T2.
9700 elsif Etype (T2) = T2 then
9703 -- Proceed to check T2's immediate parent
9705 elsif Is_Ancestor (T1, Base_Type (Etype (T2))) then
9708 -- Finally, check to see if T1 is an ancestor of any of T2's
9712 Intfc_Elmt := First_Elmt (Interfaces (T2));
9713 while Present (Intfc_Elmt) loop
9714 if Is_Ancestor (T1, Node (Intfc_Elmt)) then
9718 Next_Elmt (Intfc_Elmt);
9723 end Is_Tagged_Ancestor;
9725 -- Start of processing for Check_Abstract_Primitives
9728 -- Loop over all of the formal derived type's primitives
9730 Gen_Elmt := First_Elmt (Gen_Prims);
9731 while Present (Gen_Elmt) loop
9732 Gen_Subp := Node (Gen_Elmt);
9734 -- If the primitive of the formal is not abstract, then
9735 -- determine whether there is a corresponding primitive of
9736 -- the actual type that's abstract.
9738 if not Is_Abstract_Subprogram (Gen_Subp) then
9739 Act_Elmt := First_Elmt (Act_Prims);
9740 while Present (Act_Elmt) loop
9741 Act_Subp := Node (Act_Elmt);
9743 -- If we find an abstract primitive of the actual,
9744 -- then we need to test whether it corresponds to the
9745 -- subprogram from which the generic formal primitive
9748 if Is_Abstract_Subprogram (Act_Subp) then
9749 Anc_Subp := Alias (Gen_Subp);
9751 -- Test whether we have a corresponding primitive
9752 -- by comparing names, kinds, formal types, and
9755 if Chars (Anc_Subp) = Chars (Act_Subp)
9756 and then Ekind (Anc_Subp) = Ekind (Act_Subp)
9758 Anc_Formal := First_Formal (Anc_Subp);
9759 Act_Formal := First_Formal (Act_Subp);
9760 while Present (Anc_Formal)
9761 and then Present (Act_Formal)
9763 Anc_F_Type := Etype (Anc_Formal);
9764 Act_F_Type := Etype (Act_Formal);
9766 if Ekind (Anc_F_Type)
9767 = E_Anonymous_Access_Type
9769 Anc_F_Type := Designated_Type (Anc_F_Type);
9771 if Ekind (Act_F_Type)
9772 = E_Anonymous_Access_Type
9775 Designated_Type (Act_F_Type);
9781 Ekind (Act_F_Type) = E_Anonymous_Access_Type
9786 Anc_F_Type := Base_Type (Anc_F_Type);
9787 Act_F_Type := Base_Type (Act_F_Type);
9789 -- If the formal is controlling, then the
9790 -- the type of the actual primitive's formal
9791 -- must be derived directly or indirectly
9792 -- from the type of the ancestor primitive's
9795 if Is_Controlling_Formal (Anc_Formal) then
9796 if not Is_Tagged_Ancestor
9797 (Anc_F_Type, Act_F_Type)
9802 -- Otherwise the types of the formals must
9805 elsif Anc_F_Type /= Act_F_Type then
9809 Next_Entity (Anc_Formal);
9810 Next_Entity (Act_Formal);
9813 -- If we traversed through all of the formals
9814 -- then so far the subprograms correspond, so
9815 -- now check that any result types correspond.
9818 and then No (Act_Formal)
9820 Subprograms_Correspond := True;
9822 if Ekind (Act_Subp) = E_Function then
9823 Anc_F_Type := Etype (Anc_Subp);
9824 Act_F_Type := Etype (Act_Subp);
9826 if Ekind (Anc_F_Type)
9827 = E_Anonymous_Access_Type
9830 Designated_Type (Anc_F_Type);
9832 if Ekind (Act_F_Type)
9833 = E_Anonymous_Access_Type
9836 Designated_Type (Act_F_Type);
9838 Subprograms_Correspond := False;
9843 = E_Anonymous_Access_Type
9845 Subprograms_Correspond := False;
9848 Anc_F_Type := Base_Type (Anc_F_Type);
9849 Act_F_Type := Base_Type (Act_F_Type);
9851 -- Now either the result types must be
9852 -- the same or, if the result type is
9853 -- controlling, the result type of the
9854 -- actual primitive must descend from the
9855 -- result type of the ancestor primitive.
9857 if Subprograms_Correspond
9858 and then Anc_F_Type /= Act_F_Type
9860 Has_Controlling_Result (Anc_Subp)
9862 not Is_Tagged_Ancestor
9863 (Anc_F_Type, Act_F_Type)
9865 Subprograms_Correspond := False;
9869 -- Found a matching subprogram belonging to
9870 -- formal ancestor type, so actual subprogram
9871 -- corresponds and this violates 3.9.3(9).
9873 if Subprograms_Correspond then
9875 ("abstract subprogram & overrides " &
9876 "nonabstract subprogram of ancestor",
9884 Next_Elmt (Act_Elmt);
9888 Next_Elmt (Gen_Elmt);
9890 end Check_Abstract_Primitives;
9893 -- Verify that limitedness matches. If parent is a limited
9894 -- interface then the generic formal is not unless declared
9895 -- explicitly so. If not declared limited, the actual cannot be
9896 -- limited (see AI05-0087).
9897 -- Disable check for now, limited interfaces implemented by
9898 -- protected types are common, Need to update tests ???
9900 if Is_Limited_Type (Act_T)
9901 and then not Is_Limited_Type (A_Gen_T)
9905 ("actual for non-limited & cannot be a limited type", Actual,
9907 Explain_Limited_Type (Act_T, Actual);
9908 Abandon_Instantiation (Actual);
9910 end Validate_Derived_Type_Instance;
9912 --------------------------------------
9913 -- Validate_Interface_Type_Instance --
9914 --------------------------------------
9916 procedure Validate_Interface_Type_Instance is
9918 if not Is_Interface (Act_T) then
9920 ("actual for formal interface type must be an interface",
9923 elsif Is_Limited_Type (Act_T) /= Is_Limited_Type (A_Gen_T)
9925 Is_Task_Interface (A_Gen_T) /= Is_Task_Interface (Act_T)
9927 Is_Protected_Interface (A_Gen_T) /=
9928 Is_Protected_Interface (Act_T)
9930 Is_Synchronized_Interface (A_Gen_T) /=
9931 Is_Synchronized_Interface (Act_T)
9934 ("actual for interface& does not match (RM 12.5.5(4))",
9937 end Validate_Interface_Type_Instance;
9939 ------------------------------------
9940 -- Validate_Private_Type_Instance --
9941 ------------------------------------
9943 procedure Validate_Private_Type_Instance is
9944 Formal_Discr : Entity_Id;
9945 Actual_Discr : Entity_Id;
9946 Formal_Subt : Entity_Id;
9949 if Is_Limited_Type (Act_T)
9950 and then not Is_Limited_Type (A_Gen_T)
9953 ("actual for non-limited & cannot be a limited type", Actual,
9955 Explain_Limited_Type (Act_T, Actual);
9956 Abandon_Instantiation (Actual);
9958 elsif Known_To_Have_Preelab_Init (A_Gen_T)
9959 and then not Has_Preelaborable_Initialization (Act_T)
9962 ("actual for & must have preelaborable initialization", Actual,
9965 elsif Is_Indefinite_Subtype (Act_T)
9966 and then not Is_Indefinite_Subtype (A_Gen_T)
9967 and then Ada_Version >= Ada_95
9970 ("actual for & must be a definite subtype", Actual, Gen_T);
9972 elsif not Is_Tagged_Type (Act_T)
9973 and then Is_Tagged_Type (A_Gen_T)
9976 ("actual for & must be a tagged type", Actual, Gen_T);
9978 elsif Has_Discriminants (A_Gen_T) then
9979 if not Has_Discriminants (Act_T) then
9981 ("actual for & must have discriminants", Actual, Gen_T);
9982 Abandon_Instantiation (Actual);
9984 elsif Is_Constrained (Act_T) then
9986 ("actual for & must be unconstrained", Actual, Gen_T);
9987 Abandon_Instantiation (Actual);
9990 Formal_Discr := First_Discriminant (A_Gen_T);
9991 Actual_Discr := First_Discriminant (Act_T);
9992 while Formal_Discr /= Empty loop
9993 if Actual_Discr = Empty then
9995 ("discriminants on actual do not match formal",
9997 Abandon_Instantiation (Actual);
10000 Formal_Subt := Get_Instance_Of (Etype (Formal_Discr));
10002 -- Access discriminants match if designated types do
10004 if Ekind (Base_Type (Formal_Subt)) = E_Anonymous_Access_Type
10005 and then (Ekind (Base_Type (Etype (Actual_Discr)))) =
10006 E_Anonymous_Access_Type
10009 (Designated_Type (Base_Type (Formal_Subt))) =
10010 Designated_Type (Base_Type (Etype (Actual_Discr)))
10014 elsif Base_Type (Formal_Subt) /=
10015 Base_Type (Etype (Actual_Discr))
10018 ("types of actual discriminants must match formal",
10020 Abandon_Instantiation (Actual);
10022 elsif not Subtypes_Statically_Match
10023 (Formal_Subt, Etype (Actual_Discr))
10024 and then Ada_Version >= Ada_95
10027 ("subtypes of actual discriminants must match formal",
10029 Abandon_Instantiation (Actual);
10032 Next_Discriminant (Formal_Discr);
10033 Next_Discriminant (Actual_Discr);
10036 if Actual_Discr /= Empty then
10038 ("discriminants on actual do not match formal",
10040 Abandon_Instantiation (Actual);
10047 end Validate_Private_Type_Instance;
10049 -- Start of processing for Instantiate_Type
10052 if Get_Instance_Of (A_Gen_T) /= A_Gen_T then
10053 Error_Msg_N ("duplicate instantiation of generic type", Actual);
10054 return New_List (Error);
10056 elsif not Is_Entity_Name (Actual)
10057 or else not Is_Type (Entity (Actual))
10060 ("expect valid subtype mark to instantiate &", Actual, Gen_T);
10061 Abandon_Instantiation (Actual);
10064 Act_T := Entity (Actual);
10066 -- Ada 2005 (AI-216): An Unchecked_Union subtype shall only be passed
10067 -- as a generic actual parameter if the corresponding formal type
10068 -- does not have a known_discriminant_part, or is a formal derived
10069 -- type that is an Unchecked_Union type.
10071 if Is_Unchecked_Union (Base_Type (Act_T)) then
10072 if not Has_Discriminants (A_Gen_T)
10074 (Is_Derived_Type (A_Gen_T)
10076 Is_Unchecked_Union (A_Gen_T))
10080 Error_Msg_N ("Unchecked_Union cannot be the actual for a" &
10081 " discriminated formal type", Act_T);
10086 -- Deal with fixed/floating restrictions
10088 if Is_Floating_Point_Type (Act_T) then
10089 Check_Restriction (No_Floating_Point, Actual);
10090 elsif Is_Fixed_Point_Type (Act_T) then
10091 Check_Restriction (No_Fixed_Point, Actual);
10094 -- Deal with error of using incomplete type as generic actual.
10095 -- This includes limited views of a type, even if the non-limited
10096 -- view may be available.
10098 if Ekind (Act_T) = E_Incomplete_Type
10099 or else (Is_Class_Wide_Type (Act_T)
10101 Ekind (Root_Type (Act_T)) = E_Incomplete_Type)
10103 if Is_Class_Wide_Type (Act_T)
10104 or else No (Full_View (Act_T))
10106 Error_Msg_N ("premature use of incomplete type", Actual);
10107 Abandon_Instantiation (Actual);
10109 Act_T := Full_View (Act_T);
10110 Set_Entity (Actual, Act_T);
10112 if Has_Private_Component (Act_T) then
10114 ("premature use of type with private component", Actual);
10118 -- Deal with error of premature use of private type as generic actual
10120 elsif Is_Private_Type (Act_T)
10121 and then Is_Private_Type (Base_Type (Act_T))
10122 and then not Is_Generic_Type (Act_T)
10123 and then not Is_Derived_Type (Act_T)
10124 and then No (Full_View (Root_Type (Act_T)))
10126 Error_Msg_N ("premature use of private type", Actual);
10128 elsif Has_Private_Component (Act_T) then
10130 ("premature use of type with private component", Actual);
10133 Set_Instance_Of (A_Gen_T, Act_T);
10135 -- If the type is generic, the class-wide type may also be used
10137 if Is_Tagged_Type (A_Gen_T)
10138 and then Is_Tagged_Type (Act_T)
10139 and then not Is_Class_Wide_Type (A_Gen_T)
10141 Set_Instance_Of (Class_Wide_Type (A_Gen_T),
10142 Class_Wide_Type (Act_T));
10145 if not Is_Abstract_Type (A_Gen_T)
10146 and then Is_Abstract_Type (Act_T)
10149 ("actual of non-abstract formal cannot be abstract", Actual);
10152 -- A generic scalar type is a first subtype for which we generate
10153 -- an anonymous base type. Indicate that the instance of this base
10154 -- is the base type of the actual.
10156 if Is_Scalar_Type (A_Gen_T) then
10157 Set_Instance_Of (Etype (A_Gen_T), Etype (Act_T));
10161 if Error_Posted (Act_T) then
10164 case Nkind (Def) is
10165 when N_Formal_Private_Type_Definition =>
10166 Validate_Private_Type_Instance;
10168 when N_Formal_Derived_Type_Definition =>
10169 Validate_Derived_Type_Instance;
10171 when N_Formal_Discrete_Type_Definition =>
10172 if not Is_Discrete_Type (Act_T) then
10174 ("expect discrete type in instantiation of&",
10176 Abandon_Instantiation (Actual);
10179 when N_Formal_Signed_Integer_Type_Definition =>
10180 if not Is_Signed_Integer_Type (Act_T) then
10182 ("expect signed integer type in instantiation of&",
10184 Abandon_Instantiation (Actual);
10187 when N_Formal_Modular_Type_Definition =>
10188 if not Is_Modular_Integer_Type (Act_T) then
10190 ("expect modular type in instantiation of &",
10192 Abandon_Instantiation (Actual);
10195 when N_Formal_Floating_Point_Definition =>
10196 if not Is_Floating_Point_Type (Act_T) then
10198 ("expect float type in instantiation of &", Actual, Gen_T);
10199 Abandon_Instantiation (Actual);
10202 when N_Formal_Ordinary_Fixed_Point_Definition =>
10203 if not Is_Ordinary_Fixed_Point_Type (Act_T) then
10205 ("expect ordinary fixed point type in instantiation of &",
10207 Abandon_Instantiation (Actual);
10210 when N_Formal_Decimal_Fixed_Point_Definition =>
10211 if not Is_Decimal_Fixed_Point_Type (Act_T) then
10213 ("expect decimal type in instantiation of &",
10215 Abandon_Instantiation (Actual);
10218 when N_Array_Type_Definition =>
10219 Validate_Array_Type_Instance;
10221 when N_Access_To_Object_Definition =>
10222 Validate_Access_Type_Instance;
10224 when N_Access_Function_Definition |
10225 N_Access_Procedure_Definition =>
10226 Validate_Access_Subprogram_Instance;
10228 when N_Record_Definition =>
10229 Validate_Interface_Type_Instance;
10231 when N_Derived_Type_Definition =>
10232 Validate_Derived_Interface_Type_Instance;
10235 raise Program_Error;
10240 Subt := New_Copy (Gen_T);
10242 -- Use adjusted sloc of subtype name as the location for other nodes in
10243 -- the subtype declaration.
10245 Loc := Sloc (Subt);
10248 Make_Subtype_Declaration (Loc,
10249 Defining_Identifier => Subt,
10250 Subtype_Indication => New_Reference_To (Act_T, Loc));
10252 if Is_Private_Type (Act_T) then
10253 Set_Has_Private_View (Subtype_Indication (Decl_Node));
10255 elsif Is_Access_Type (Act_T)
10256 and then Is_Private_Type (Designated_Type (Act_T))
10258 Set_Has_Private_View (Subtype_Indication (Decl_Node));
10261 Decl_Nodes := New_List (Decl_Node);
10263 -- Flag actual derived types so their elaboration produces the
10264 -- appropriate renamings for the primitive operations of the ancestor.
10265 -- Flag actual for formal private types as well, to determine whether
10266 -- operations in the private part may override inherited operations.
10267 -- If the formal has an interface list, the ancestor is not the
10268 -- parent, but the analyzed formal that includes the interface
10269 -- operations of all its progenitors.
10271 if Nkind (Def) = N_Formal_Derived_Type_Definition then
10272 if Present (Interface_List (Def)) then
10273 Set_Generic_Parent_Type (Decl_Node, A_Gen_T);
10275 Set_Generic_Parent_Type (Decl_Node, Ancestor);
10278 elsif Nkind (Def) = N_Formal_Private_Type_Definition then
10279 Set_Generic_Parent_Type (Decl_Node, Ancestor);
10282 -- If the actual is a synchronized type that implements an interface,
10283 -- the primitive operations are attached to the corresponding record,
10284 -- and we have to treat it as an additional generic actual, so that its
10285 -- primitive operations become visible in the instance. The task or
10286 -- protected type itself does not carry primitive operations.
10288 if Is_Concurrent_Type (Act_T)
10289 and then Is_Tagged_Type (Act_T)
10290 and then Present (Corresponding_Record_Type (Act_T))
10291 and then Present (Ancestor)
10292 and then Is_Interface (Ancestor)
10295 Corr_Rec : constant Entity_Id :=
10296 Corresponding_Record_Type (Act_T);
10297 New_Corr : Entity_Id;
10298 Corr_Decl : Node_Id;
10301 New_Corr := Make_Defining_Identifier (Loc,
10302 Chars => New_Internal_Name ('S'));
10304 Make_Subtype_Declaration (Loc,
10305 Defining_Identifier => New_Corr,
10306 Subtype_Indication =>
10307 New_Reference_To (Corr_Rec, Loc));
10308 Append_To (Decl_Nodes, Corr_Decl);
10310 if Ekind (Act_T) = E_Task_Type then
10311 Set_Ekind (Subt, E_Task_Subtype);
10313 Set_Ekind (Subt, E_Protected_Subtype);
10316 Set_Corresponding_Record_Type (Subt, Corr_Rec);
10317 Set_Generic_Parent_Type (Corr_Decl, Ancestor);
10318 Set_Generic_Parent_Type (Decl_Node, Empty);
10323 end Instantiate_Type;
10325 -----------------------
10326 -- Is_Generic_Formal --
10327 -----------------------
10329 function Is_Generic_Formal (E : Entity_Id) return Boolean is
10335 Kind := Nkind (Parent (E));
10337 Nkind_In (Kind, N_Formal_Object_Declaration,
10338 N_Formal_Package_Declaration,
10339 N_Formal_Type_Declaration)
10341 (Is_Formal_Subprogram (E)
10343 Nkind (Parent (Parent (E))) in
10344 N_Formal_Subprogram_Declaration);
10346 end Is_Generic_Formal;
10348 ---------------------
10349 -- Is_In_Main_Unit --
10350 ---------------------
10352 function Is_In_Main_Unit (N : Node_Id) return Boolean is
10353 Unum : constant Unit_Number_Type := Get_Source_Unit (N);
10354 Current_Unit : Node_Id;
10357 if Unum = Main_Unit then
10360 -- If the current unit is a subunit then it is either the main unit or
10361 -- is being compiled as part of the main unit.
10363 elsif Nkind (N) = N_Compilation_Unit then
10364 return Nkind (Unit (N)) = N_Subunit;
10367 Current_Unit := Parent (N);
10368 while Present (Current_Unit)
10369 and then Nkind (Current_Unit) /= N_Compilation_Unit
10371 Current_Unit := Parent (Current_Unit);
10374 -- The instantiation node is in the main unit, or else the current node
10375 -- (perhaps as the result of nested instantiations) is in the main unit,
10376 -- or in the declaration of the main unit, which in this last case must
10379 return Unum = Main_Unit
10380 or else Current_Unit = Cunit (Main_Unit)
10381 or else Current_Unit = Library_Unit (Cunit (Main_Unit))
10382 or else (Present (Library_Unit (Current_Unit))
10383 and then Is_In_Main_Unit (Library_Unit (Current_Unit)));
10384 end Is_In_Main_Unit;
10386 ----------------------------
10387 -- Load_Parent_Of_Generic --
10388 ----------------------------
10390 procedure Load_Parent_Of_Generic
10393 Body_Optional : Boolean := False)
10395 Comp_Unit : constant Node_Id := Cunit (Get_Source_Unit (Spec));
10396 Save_Style_Check : constant Boolean := Style_Check;
10397 True_Parent : Node_Id;
10398 Inst_Node : Node_Id;
10400 Previous_Instances : constant Elist_Id := New_Elmt_List;
10402 procedure Collect_Previous_Instances (Decls : List_Id);
10403 -- Collect all instantiations in the given list of declarations, that
10404 -- precede the generic that we need to load. If the bodies of these
10405 -- instantiations are available, we must analyze them, to ensure that
10406 -- the public symbols generated are the same when the unit is compiled
10407 -- to generate code, and when it is compiled in the context of a unit
10408 -- that needs a particular nested instance. This process is applied
10409 -- to both package and subprogram instances.
10411 --------------------------------
10412 -- Collect_Previous_Instances --
10413 --------------------------------
10415 procedure Collect_Previous_Instances (Decls : List_Id) is
10419 Decl := First (Decls);
10420 while Present (Decl) loop
10421 if Sloc (Decl) >= Sloc (Inst_Node) then
10424 -- If Decl is an instantiation, then record it as requiring
10425 -- instantiation of the corresponding body, except if it is an
10426 -- abbreviated instantiation generated internally for conformance
10427 -- checking purposes only for the case of a formal package
10428 -- declared without a box (see Instantiate_Formal_Package). Such
10429 -- an instantiation does not generate any code (the actual code
10430 -- comes from actual) and thus does not need to be analyzed here.
10432 elsif Nkind (Decl) = N_Package_Instantiation
10433 and then not Is_Internal (Defining_Entity (Decl))
10435 Append_Elmt (Decl, Previous_Instances);
10437 -- For a subprogram instantiation, omit instantiations of
10438 -- intrinsic operations (Unchecked_Conversions, etc.) that
10441 elsif Nkind_In (Decl, N_Function_Instantiation,
10442 N_Procedure_Instantiation)
10443 and then not Is_Intrinsic_Subprogram (Entity (Name (Decl)))
10445 Append_Elmt (Decl, Previous_Instances);
10447 elsif Nkind (Decl) = N_Package_Declaration then
10448 Collect_Previous_Instances
10449 (Visible_Declarations (Specification (Decl)));
10450 Collect_Previous_Instances
10451 (Private_Declarations (Specification (Decl)));
10453 elsif Nkind (Decl) = N_Package_Body then
10454 Collect_Previous_Instances (Declarations (Decl));
10459 end Collect_Previous_Instances;
10461 -- Start of processing for Load_Parent_Of_Generic
10464 if not In_Same_Source_Unit (N, Spec)
10465 or else Nkind (Unit (Comp_Unit)) = N_Package_Declaration
10466 or else (Nkind (Unit (Comp_Unit)) = N_Package_Body
10467 and then not Is_In_Main_Unit (Spec))
10469 -- Find body of parent of spec, and analyze it. A special case arises
10470 -- when the parent is an instantiation, that is to say when we are
10471 -- currently instantiating a nested generic. In that case, there is
10472 -- no separate file for the body of the enclosing instance. Instead,
10473 -- the enclosing body must be instantiated as if it were a pending
10474 -- instantiation, in order to produce the body for the nested generic
10475 -- we require now. Note that in that case the generic may be defined
10476 -- in a package body, the instance defined in the same package body,
10477 -- and the original enclosing body may not be in the main unit.
10479 Inst_Node := Empty;
10481 True_Parent := Parent (Spec);
10482 while Present (True_Parent)
10483 and then Nkind (True_Parent) /= N_Compilation_Unit
10485 if Nkind (True_Parent) = N_Package_Declaration
10487 Nkind (Original_Node (True_Parent)) = N_Package_Instantiation
10489 -- Parent is a compilation unit that is an instantiation.
10490 -- Instantiation node has been replaced with package decl.
10492 Inst_Node := Original_Node (True_Parent);
10495 elsif Nkind (True_Parent) = N_Package_Declaration
10496 and then Present (Generic_Parent (Specification (True_Parent)))
10497 and then Nkind (Parent (True_Parent)) /= N_Compilation_Unit
10499 -- Parent is an instantiation within another specification.
10500 -- Declaration for instance has been inserted before original
10501 -- instantiation node. A direct link would be preferable?
10503 Inst_Node := Next (True_Parent);
10504 while Present (Inst_Node)
10505 and then Nkind (Inst_Node) /= N_Package_Instantiation
10510 -- If the instance appears within a generic, and the generic
10511 -- unit is defined within a formal package of the enclosing
10512 -- generic, there is no generic body available, and none
10513 -- needed. A more precise test should be used ???
10515 if No (Inst_Node) then
10522 True_Parent := Parent (True_Parent);
10526 -- Case where we are currently instantiating a nested generic
10528 if Present (Inst_Node) then
10529 if Nkind (Parent (True_Parent)) = N_Compilation_Unit then
10531 -- Instantiation node and declaration of instantiated package
10532 -- were exchanged when only the declaration was needed.
10533 -- Restore instantiation node before proceeding with body.
10535 Set_Unit (Parent (True_Parent), Inst_Node);
10538 -- Now complete instantiation of enclosing body, if it appears
10539 -- in some other unit. If it appears in the current unit, the
10540 -- body will have been instantiated already.
10542 if No (Corresponding_Body (Instance_Spec (Inst_Node))) then
10544 -- We need to determine the expander mode to instantiate the
10545 -- enclosing body. Because the generic body we need may use
10546 -- global entities declared in the enclosing package (including
10547 -- aggregates) it is in general necessary to compile this body
10548 -- with expansion enabled. The exception is if we are within a
10549 -- generic package, in which case the usual generic rule
10553 Exp_Status : Boolean := True;
10557 -- Loop through scopes looking for generic package
10559 Scop := Scope (Defining_Entity (Instance_Spec (Inst_Node)));
10560 while Present (Scop)
10561 and then Scop /= Standard_Standard
10563 if Ekind (Scop) = E_Generic_Package then
10564 Exp_Status := False;
10568 Scop := Scope (Scop);
10571 -- Collect previous instantiations in the unit that
10572 -- contains the desired generic.
10574 if Nkind (Parent (True_Parent)) /= N_Compilation_Unit
10575 and then not Body_Optional
10579 Info : Pending_Body_Info;
10583 Par := Parent (Inst_Node);
10584 while Present (Par) loop
10585 exit when Nkind (Parent (Par)) = N_Compilation_Unit;
10586 Par := Parent (Par);
10589 pragma Assert (Present (Par));
10591 if Nkind (Par) = N_Package_Body then
10592 Collect_Previous_Instances (Declarations (Par));
10594 elsif Nkind (Par) = N_Package_Declaration then
10595 Collect_Previous_Instances
10596 (Visible_Declarations (Specification (Par)));
10597 Collect_Previous_Instances
10598 (Private_Declarations (Specification (Par)));
10601 -- Enclosing unit is a subprogram body, In this
10602 -- case all instance bodies are processed in order
10603 -- and there is no need to collect them separately.
10608 Decl := First_Elmt (Previous_Instances);
10609 while Present (Decl) loop
10611 (Inst_Node => Node (Decl),
10613 Instance_Spec (Node (Decl)),
10614 Expander_Status => Exp_Status,
10615 Current_Sem_Unit =>
10616 Get_Code_Unit (Sloc (Node (Decl))),
10617 Scope_Suppress => Scope_Suppress,
10618 Local_Suppress_Stack_Top =>
10619 Local_Suppress_Stack_Top);
10621 -- Package instance
10624 Nkind (Node (Decl)) = N_Package_Instantiation
10626 Instantiate_Package_Body
10627 (Info, Body_Optional => True);
10629 -- Subprogram instance
10632 -- The instance_spec is the wrapper package,
10633 -- and the subprogram declaration is the last
10634 -- declaration in the wrapper.
10638 (Visible_Declarations
10639 (Specification (Info.Act_Decl)));
10641 Instantiate_Subprogram_Body
10642 (Info, Body_Optional => True);
10650 Instantiate_Package_Body
10652 ((Inst_Node => Inst_Node,
10653 Act_Decl => True_Parent,
10654 Expander_Status => Exp_Status,
10655 Current_Sem_Unit =>
10656 Get_Code_Unit (Sloc (Inst_Node)),
10657 Scope_Suppress => Scope_Suppress,
10658 Local_Suppress_Stack_Top =>
10659 Local_Suppress_Stack_Top)),
10660 Body_Optional => Body_Optional);
10664 -- Case where we are not instantiating a nested generic
10667 Opt.Style_Check := False;
10668 Expander_Mode_Save_And_Set (True);
10669 Load_Needed_Body (Comp_Unit, OK);
10670 Opt.Style_Check := Save_Style_Check;
10671 Expander_Mode_Restore;
10674 and then Unit_Requires_Body (Defining_Entity (Spec))
10675 and then not Body_Optional
10678 Bname : constant Unit_Name_Type :=
10679 Get_Body_Name (Get_Unit_Name (Unit (Comp_Unit)));
10682 Error_Msg_Unit_1 := Bname;
10683 Error_Msg_N ("this instantiation requires$!", N);
10684 Error_Msg_File_1 := Get_File_Name (Bname, Subunit => False);
10685 Error_Msg_N ("\but file{ was not found!", N);
10686 raise Unrecoverable_Error;
10692 -- If loading parent of the generic caused an instantiation circularity,
10693 -- we abandon compilation at this point, because otherwise in some cases
10694 -- we get into trouble with infinite recursions after this point.
10696 if Circularity_Detected then
10697 raise Unrecoverable_Error;
10699 end Load_Parent_Of_Generic;
10701 -----------------------
10702 -- Move_Freeze_Nodes --
10703 -----------------------
10705 procedure Move_Freeze_Nodes
10706 (Out_Of : Entity_Id;
10711 Next_Decl : Node_Id;
10712 Next_Node : Node_Id := After;
10715 function Is_Outer_Type (T : Entity_Id) return Boolean;
10716 -- Check whether entity is declared in a scope external to that
10717 -- of the generic unit.
10719 -------------------
10720 -- Is_Outer_Type --
10721 -------------------
10723 function Is_Outer_Type (T : Entity_Id) return Boolean is
10724 Scop : Entity_Id := Scope (T);
10727 if Scope_Depth (Scop) < Scope_Depth (Out_Of) then
10731 while Scop /= Standard_Standard loop
10732 if Scop = Out_Of then
10735 Scop := Scope (Scop);
10743 -- Start of processing for Move_Freeze_Nodes
10750 -- First remove the freeze nodes that may appear before all other
10754 while Present (Decl)
10755 and then Nkind (Decl) = N_Freeze_Entity
10756 and then Is_Outer_Type (Entity (Decl))
10758 Decl := Remove_Head (L);
10759 Insert_After (Next_Node, Decl);
10760 Set_Analyzed (Decl, False);
10765 -- Next scan the list of declarations and remove each freeze node that
10766 -- appears ahead of the current node.
10768 while Present (Decl) loop
10769 while Present (Next (Decl))
10770 and then Nkind (Next (Decl)) = N_Freeze_Entity
10771 and then Is_Outer_Type (Entity (Next (Decl)))
10773 Next_Decl := Remove_Next (Decl);
10774 Insert_After (Next_Node, Next_Decl);
10775 Set_Analyzed (Next_Decl, False);
10776 Next_Node := Next_Decl;
10779 -- If the declaration is a nested package or concurrent type, then
10780 -- recurse. Nested generic packages will have been processed from the
10783 if Nkind (Decl) = N_Package_Declaration then
10784 Spec := Specification (Decl);
10786 elsif Nkind (Decl) = N_Task_Type_Declaration then
10787 Spec := Task_Definition (Decl);
10789 elsif Nkind (Decl) = N_Protected_Type_Declaration then
10790 Spec := Protected_Definition (Decl);
10796 if Present (Spec) then
10797 Move_Freeze_Nodes (Out_Of, Next_Node,
10798 Visible_Declarations (Spec));
10799 Move_Freeze_Nodes (Out_Of, Next_Node,
10800 Private_Declarations (Spec));
10805 end Move_Freeze_Nodes;
10811 function Next_Assoc (E : Assoc_Ptr) return Assoc_Ptr is
10813 return Generic_Renamings.Table (E).Next_In_HTable;
10816 ------------------------
10817 -- Preanalyze_Actuals --
10818 ------------------------
10820 procedure Preanalyze_Actuals (N : Node_Id) is
10823 Errs : constant Int := Serious_Errors_Detected;
10826 Assoc := First (Generic_Associations (N));
10827 while Present (Assoc) loop
10828 if Nkind (Assoc) /= N_Others_Choice then
10829 Act := Explicit_Generic_Actual_Parameter (Assoc);
10831 -- Within a nested instantiation, a defaulted actual is an empty
10832 -- association, so nothing to analyze. If the subprogram actual
10833 -- is an attribute, analyze prefix only, because actual is not a
10834 -- complete attribute reference.
10836 -- If actual is an allocator, analyze expression only. The full
10837 -- analysis can generate code, and if instance is a compilation
10838 -- unit we have to wait until the package instance is installed
10839 -- to have a proper place to insert this code.
10841 -- String literals may be operators, but at this point we do not
10842 -- know whether the actual is a formal subprogram or a string.
10847 elsif Nkind (Act) = N_Attribute_Reference then
10848 Analyze (Prefix (Act));
10850 elsif Nkind (Act) = N_Explicit_Dereference then
10851 Analyze (Prefix (Act));
10853 elsif Nkind (Act) = N_Allocator then
10855 Expr : constant Node_Id := Expression (Act);
10858 if Nkind (Expr) = N_Subtype_Indication then
10859 Analyze (Subtype_Mark (Expr));
10861 -- Analyze separately each discriminant constraint,
10862 -- when given with a named association.
10868 Constr := First (Constraints (Constraint (Expr)));
10869 while Present (Constr) loop
10870 if Nkind (Constr) = N_Discriminant_Association then
10871 Analyze (Expression (Constr));
10885 elsif Nkind (Act) /= N_Operator_Symbol then
10889 if Errs /= Serious_Errors_Detected then
10891 -- Do a minimal analysis of the generic, to prevent spurious
10892 -- warnings complaining about the generic being unreferenced,
10893 -- before abandoning the instantiation.
10895 Analyze (Name (N));
10897 if Is_Entity_Name (Name (N))
10898 and then Etype (Name (N)) /= Any_Type
10900 Generate_Reference (Entity (Name (N)), Name (N));
10901 Set_Is_Instantiated (Entity (Name (N)));
10904 Abandon_Instantiation (Act);
10910 end Preanalyze_Actuals;
10912 -------------------
10913 -- Remove_Parent --
10914 -------------------
10916 procedure Remove_Parent (In_Body : Boolean := False) is
10917 S : Entity_Id := Current_Scope;
10918 -- S is the scope containing the instantiation just completed. The
10919 -- scope stack contains the parent instances of the instantiation,
10920 -- followed by the original S.
10927 -- After child instantiation is complete, remove from scope stack the
10928 -- extra copy of the current scope, and then remove parent instances.
10930 if not In_Body then
10933 while Current_Scope /= S loop
10934 P := Current_Scope;
10935 End_Package_Scope (Current_Scope);
10937 if In_Open_Scopes (P) then
10938 E := First_Entity (P);
10939 while Present (E) loop
10940 Set_Is_Immediately_Visible (E, True);
10944 if Is_Generic_Instance (Current_Scope)
10945 and then P /= Current_Scope
10947 -- We are within an instance of some sibling. Retain
10948 -- visibility of parent, for proper subsequent cleanup,
10949 -- and reinstall private declarations as well.
10951 Set_In_Private_Part (P);
10952 Install_Private_Declarations (P);
10955 -- If the ultimate parent is a top-level unit recorded in
10956 -- Instance_Parent_Unit, then reset its visibility to what
10957 -- it was before instantiation. (It's not clear what the
10958 -- purpose is of testing whether Scope (P) is In_Open_Scopes,
10959 -- but that test was present before the ultimate parent test
10962 elsif not In_Open_Scopes (Scope (P))
10963 or else (P = Instance_Parent_Unit
10964 and then not Parent_Unit_Visible)
10966 Set_Is_Immediately_Visible (P, False);
10968 -- If the current scope is itself an instantiation of a generic
10969 -- nested within P, and we are in the private part of body of this
10970 -- instantiation, restore the full views of P, that were removed
10971 -- in End_Package_Scope above. This obscure case can occur when a
10972 -- subunit of a generic contains an instance of a child unit of
10973 -- its generic parent unit.
10975 elsif S = Current_Scope
10976 and then Is_Generic_Instance (S)
10979 Par : constant Entity_Id :=
10981 (Specification (Unit_Declaration_Node (S)));
10984 and then P = Scope (Par)
10985 and then (In_Package_Body (S) or else In_Private_Part (S))
10987 Set_In_Private_Part (P);
10988 Install_Private_Declarations (P);
10994 -- Reset visibility of entities in the enclosing scope
10996 Set_Is_Hidden_Open_Scope (Current_Scope, False);
10998 Hidden := First_Elmt (Hidden_Entities);
10999 while Present (Hidden) loop
11000 Set_Is_Immediately_Visible (Node (Hidden), True);
11001 Next_Elmt (Hidden);
11005 -- Each body is analyzed separately, and there is no context
11006 -- that needs preserving from one body instance to the next,
11007 -- so remove all parent scopes that have been installed.
11009 while Present (S) loop
11010 End_Package_Scope (S);
11011 Set_Is_Immediately_Visible (S, False);
11012 S := Current_Scope;
11013 exit when S = Standard_Standard;
11022 procedure Restore_Env is
11023 Saved : Instance_Env renames Instance_Envs.Table (Instance_Envs.Last);
11026 if No (Current_Instantiated_Parent.Act_Id) then
11028 -- Restore environment after subprogram inlining
11030 Restore_Private_Views (Empty);
11033 Current_Instantiated_Parent := Saved.Instantiated_Parent;
11034 Exchanged_Views := Saved.Exchanged_Views;
11035 Hidden_Entities := Saved.Hidden_Entities;
11036 Current_Sem_Unit := Saved.Current_Sem_Unit;
11037 Parent_Unit_Visible := Saved.Parent_Unit_Visible;
11038 Instance_Parent_Unit := Saved.Instance_Parent_Unit;
11040 Restore_Opt_Config_Switches (Saved.Switches);
11042 Instance_Envs.Decrement_Last;
11045 ---------------------------
11046 -- Restore_Private_Views --
11047 ---------------------------
11049 procedure Restore_Private_Views
11050 (Pack_Id : Entity_Id;
11051 Is_Package : Boolean := True)
11056 Dep_Elmt : Elmt_Id;
11059 procedure Restore_Nested_Formal (Formal : Entity_Id);
11060 -- Hide the generic formals of formal packages declared with box
11061 -- which were reachable in the current instantiation.
11063 ---------------------------
11064 -- Restore_Nested_Formal --
11065 ---------------------------
11067 procedure Restore_Nested_Formal (Formal : Entity_Id) is
11071 if Present (Renamed_Object (Formal))
11072 and then Denotes_Formal_Package (Renamed_Object (Formal), True)
11076 elsif Present (Associated_Formal_Package (Formal)) then
11077 Ent := First_Entity (Formal);
11078 while Present (Ent) loop
11079 exit when Ekind (Ent) = E_Package
11080 and then Renamed_Entity (Ent) = Renamed_Entity (Formal);
11082 Set_Is_Hidden (Ent);
11083 Set_Is_Potentially_Use_Visible (Ent, False);
11085 -- If package, then recurse
11087 if Ekind (Ent) = E_Package then
11088 Restore_Nested_Formal (Ent);
11094 end Restore_Nested_Formal;
11096 -- Start of processing for Restore_Private_Views
11099 M := First_Elmt (Exchanged_Views);
11100 while Present (M) loop
11103 -- Subtypes of types whose views have been exchanged, and that
11104 -- are defined within the instance, were not on the list of
11105 -- Private_Dependents on entry to the instance, so they have to
11106 -- be exchanged explicitly now, in order to remain consistent with
11107 -- the view of the parent type.
11109 if Ekind (Typ) = E_Private_Type
11110 or else Ekind (Typ) = E_Limited_Private_Type
11111 or else Ekind (Typ) = E_Record_Type_With_Private
11113 Dep_Elmt := First_Elmt (Private_Dependents (Typ));
11114 while Present (Dep_Elmt) loop
11115 Dep_Typ := Node (Dep_Elmt);
11117 if Scope (Dep_Typ) = Pack_Id
11118 and then Present (Full_View (Dep_Typ))
11120 Replace_Elmt (Dep_Elmt, Full_View (Dep_Typ));
11121 Exchange_Declarations (Dep_Typ);
11124 Next_Elmt (Dep_Elmt);
11128 Exchange_Declarations (Node (M));
11132 if No (Pack_Id) then
11136 -- Make the generic formal parameters private, and make the formal
11137 -- types into subtypes of the actuals again.
11139 E := First_Entity (Pack_Id);
11140 while Present (E) loop
11141 Set_Is_Hidden (E, True);
11144 and then Nkind (Parent (E)) = N_Subtype_Declaration
11146 Set_Is_Generic_Actual_Type (E, False);
11148 -- An unusual case of aliasing: the actual may also be directly
11149 -- visible in the generic, and be private there, while it is fully
11150 -- visible in the context of the instance. The internal subtype
11151 -- is private in the instance, but has full visibility like its
11152 -- parent in the enclosing scope. This enforces the invariant that
11153 -- the privacy status of all private dependents of a type coincide
11154 -- with that of the parent type. This can only happen when a
11155 -- generic child unit is instantiated within sibling.
11157 if Is_Private_Type (E)
11158 and then not Is_Private_Type (Etype (E))
11160 Exchange_Declarations (E);
11163 elsif Ekind (E) = E_Package then
11165 -- The end of the renaming list is the renaming of the generic
11166 -- package itself. If the instance is a subprogram, all entities
11167 -- in the corresponding package are renamings. If this entity is
11168 -- a formal package, make its own formals private as well. The
11169 -- actual in this case is itself the renaming of an instantiation.
11170 -- If the entity is not a package renaming, it is the entity
11171 -- created to validate formal package actuals: ignore.
11173 -- If the actual is itself a formal package for the enclosing
11174 -- generic, or the actual for such a formal package, it remains
11175 -- visible on exit from the instance, and therefore nothing needs
11176 -- to be done either, except to keep it accessible.
11179 and then Renamed_Object (E) = Pack_Id
11183 elsif Nkind (Parent (E)) /= N_Package_Renaming_Declaration then
11187 Denotes_Formal_Package (Renamed_Object (E), True, Pack_Id)
11189 Set_Is_Hidden (E, False);
11193 Act_P : constant Entity_Id := Renamed_Object (E);
11197 Id := First_Entity (Act_P);
11199 and then Id /= First_Private_Entity (Act_P)
11201 exit when Ekind (Id) = E_Package
11202 and then Renamed_Object (Id) = Act_P;
11204 Set_Is_Hidden (Id, True);
11205 Set_Is_Potentially_Use_Visible (Id, In_Use (Act_P));
11207 if Ekind (Id) = E_Package then
11208 Restore_Nested_Formal (Id);
11219 end Restore_Private_Views;
11226 (Gen_Unit : Entity_Id;
11227 Act_Unit : Entity_Id)
11231 Set_Instance_Env (Gen_Unit, Act_Unit);
11234 ----------------------------
11235 -- Save_Global_References --
11236 ----------------------------
11238 procedure Save_Global_References (N : Node_Id) is
11239 Gen_Scope : Entity_Id;
11243 function Is_Global (E : Entity_Id) return Boolean;
11244 -- Check whether entity is defined outside of generic unit. Examine the
11245 -- scope of an entity, and the scope of the scope, etc, until we find
11246 -- either Standard, in which case the entity is global, or the generic
11247 -- unit itself, which indicates that the entity is local. If the entity
11248 -- is the generic unit itself, as in the case of a recursive call, or
11249 -- the enclosing generic unit, if different from the current scope, then
11250 -- it is local as well, because it will be replaced at the point of
11251 -- instantiation. On the other hand, if it is a reference to a child
11252 -- unit of a common ancestor, which appears in an instantiation, it is
11253 -- global because it is used to denote a specific compilation unit at
11254 -- the time the instantiations will be analyzed.
11256 procedure Reset_Entity (N : Node_Id);
11257 -- Save semantic information on global entity, so that it is not
11258 -- resolved again at instantiation time.
11260 procedure Save_Entity_Descendants (N : Node_Id);
11261 -- Apply Save_Global_References to the two syntactic descendants of
11262 -- non-terminal nodes that carry an Associated_Node and are processed
11263 -- through Reset_Entity. Once the global entity (if any) has been
11264 -- captured together with its type, only two syntactic descendants need
11265 -- to be traversed to complete the processing of the tree rooted at N.
11266 -- This applies to Selected_Components, Expanded_Names, and to Operator
11267 -- nodes. N can also be a character literal, identifier, or operator
11268 -- symbol node, but the call has no effect in these cases.
11270 procedure Save_Global_Defaults (N1, N2 : Node_Id);
11271 -- Default actuals in nested instances must be handled specially
11272 -- because there is no link to them from the original tree. When an
11273 -- actual subprogram is given by a default, we add an explicit generic
11274 -- association for it in the instantiation node. When we save the
11275 -- global references on the name of the instance, we recover the list
11276 -- of generic associations, and add an explicit one to the original
11277 -- generic tree, through which a global actual can be preserved.
11278 -- Similarly, if a child unit is instantiated within a sibling, in the
11279 -- context of the parent, we must preserve the identifier of the parent
11280 -- so that it can be properly resolved in a subsequent instantiation.
11282 procedure Save_Global_Descendant (D : Union_Id);
11283 -- Apply Save_Global_References recursively to the descendents of the
11286 procedure Save_References (N : Node_Id);
11287 -- This is the recursive procedure that does the work, once the
11288 -- enclosing generic scope has been established.
11294 function Is_Global (E : Entity_Id) return Boolean is
11297 function Is_Instance_Node (Decl : Node_Id) return Boolean;
11298 -- Determine whether the parent node of a reference to a child unit
11299 -- denotes an instantiation or a formal package, in which case the
11300 -- reference to the child unit is global, even if it appears within
11301 -- the current scope (e.g. when the instance appears within the body
11302 -- of an ancestor).
11304 ----------------------
11305 -- Is_Instance_Node --
11306 ----------------------
11308 function Is_Instance_Node (Decl : Node_Id) return Boolean is
11310 return (Nkind (Decl) in N_Generic_Instantiation
11312 Nkind (Original_Node (Decl)) = N_Formal_Package_Declaration);
11313 end Is_Instance_Node;
11315 -- Start of processing for Is_Global
11318 if E = Gen_Scope then
11321 elsif E = Standard_Standard then
11324 elsif Is_Child_Unit (E)
11325 and then (Is_Instance_Node (Parent (N2))
11326 or else (Nkind (Parent (N2)) = N_Expanded_Name
11327 and then N2 = Selector_Name (Parent (N2))
11329 Is_Instance_Node (Parent (Parent (N2)))))
11335 while Se /= Gen_Scope loop
11336 if Se = Standard_Standard then
11351 procedure Reset_Entity (N : Node_Id) is
11353 procedure Set_Global_Type (N : Node_Id; N2 : Node_Id);
11354 -- If the type of N2 is global to the generic unit. Save
11355 -- the type in the generic node.
11357 function Top_Ancestor (E : Entity_Id) return Entity_Id;
11358 -- Find the ultimate ancestor of the current unit. If it is
11359 -- not a generic unit, then the name of the current unit
11360 -- in the prefix of an expanded name must be replaced with
11361 -- its generic homonym to ensure that it will be properly
11362 -- resolved in an instance.
11364 ---------------------
11365 -- Set_Global_Type --
11366 ---------------------
11368 procedure Set_Global_Type (N : Node_Id; N2 : Node_Id) is
11369 Typ : constant Entity_Id := Etype (N2);
11372 Set_Etype (N, Typ);
11374 if Entity (N) /= N2
11375 and then Has_Private_View (Entity (N))
11377 -- If the entity of N is not the associated node, this is
11378 -- a nested generic and it has an associated node as well,
11379 -- whose type is already the full view (see below). Indicate
11380 -- that the original node has a private view.
11382 Set_Has_Private_View (N);
11385 -- If not a private type, nothing else to do
11387 if not Is_Private_Type (Typ) then
11388 if Is_Array_Type (Typ)
11389 and then Is_Private_Type (Component_Type (Typ))
11391 Set_Has_Private_View (N);
11394 -- If it is a derivation of a private type in a context where
11395 -- no full view is needed, nothing to do either.
11397 elsif No (Full_View (Typ)) and then Typ /= Etype (Typ) then
11400 -- Otherwise mark the type for flipping and use the full_view
11404 Set_Has_Private_View (N);
11406 if Present (Full_View (Typ)) then
11407 Set_Etype (N2, Full_View (Typ));
11410 end Set_Global_Type;
11416 function Top_Ancestor (E : Entity_Id) return Entity_Id is
11421 while Is_Child_Unit (Par) loop
11422 Par := Scope (Par);
11428 -- Start of processing for Reset_Entity
11431 N2 := Get_Associated_Node (N);
11434 if Present (E) then
11435 if Is_Global (E) then
11436 Set_Global_Type (N, N2);
11438 elsif Nkind (N) = N_Op_Concat
11439 and then Is_Generic_Type (Etype (N2))
11441 (Base_Type (Etype (Right_Opnd (N2))) = Etype (N2)
11442 or else Base_Type (Etype (Left_Opnd (N2))) = Etype (N2))
11443 and then Is_Intrinsic_Subprogram (E)
11448 -- Entity is local. Mark generic node as unresolved.
11449 -- Note that now it does not have an entity.
11451 Set_Associated_Node (N, Empty);
11452 Set_Etype (N, Empty);
11455 if Nkind (Parent (N)) in N_Generic_Instantiation
11456 and then N = Name (Parent (N))
11458 Save_Global_Defaults (Parent (N), Parent (N2));
11461 elsif Nkind (Parent (N)) = N_Selected_Component
11462 and then Nkind (Parent (N2)) = N_Expanded_Name
11464 if Is_Global (Entity (Parent (N2))) then
11465 Change_Selected_Component_To_Expanded_Name (Parent (N));
11466 Set_Associated_Node (Parent (N), Parent (N2));
11467 Set_Global_Type (Parent (N), Parent (N2));
11468 Save_Entity_Descendants (N);
11470 -- If this is a reference to the current generic entity, replace
11471 -- by the name of the generic homonym of the current package. This
11472 -- is because in an instantiation Par.P.Q will not resolve to the
11473 -- name of the instance, whose enclosing scope is not necessarily
11474 -- Par. We use the generic homonym rather that the name of the
11475 -- generic itself, because it may be hidden by a local
11478 elsif In_Open_Scopes (Entity (Parent (N2)))
11480 Is_Generic_Unit (Top_Ancestor (Entity (Prefix (Parent (N2)))))
11482 if Ekind (Entity (Parent (N2))) = E_Generic_Package then
11483 Rewrite (Parent (N),
11484 Make_Identifier (Sloc (N),
11486 Chars (Generic_Homonym (Entity (Parent (N2))))));
11488 Rewrite (Parent (N),
11489 Make_Identifier (Sloc (N),
11490 Chars => Chars (Selector_Name (Parent (N2)))));
11494 if Nkind (Parent (Parent (N))) in N_Generic_Instantiation
11495 and then Parent (N) = Name (Parent (Parent (N)))
11497 Save_Global_Defaults
11498 (Parent (Parent (N)), Parent (Parent ((N2))));
11501 -- A selected component may denote a static constant that has been
11502 -- folded. If the static constant is global to the generic, capture
11503 -- its value. Otherwise the folding will happen in any instantiation,
11505 elsif Nkind (Parent (N)) = N_Selected_Component
11506 and then Nkind_In (Parent (N2), N_Integer_Literal, N_Real_Literal)
11508 if Present (Entity (Original_Node (Parent (N2))))
11509 and then Is_Global (Entity (Original_Node (Parent (N2))))
11511 Rewrite (Parent (N), New_Copy (Parent (N2)));
11512 Set_Analyzed (Parent (N), False);
11518 -- A selected component may be transformed into a parameterless
11519 -- function call. If the called entity is global, rewrite the node
11520 -- appropriately, i.e. as an extended name for the global entity.
11522 elsif Nkind (Parent (N)) = N_Selected_Component
11523 and then Nkind (Parent (N2)) = N_Function_Call
11524 and then N = Selector_Name (Parent (N))
11526 if No (Parameter_Associations (Parent (N2))) then
11527 if Is_Global (Entity (Name (Parent (N2)))) then
11528 Change_Selected_Component_To_Expanded_Name (Parent (N));
11529 Set_Associated_Node (Parent (N), Name (Parent (N2)));
11530 Set_Global_Type (Parent (N), Name (Parent (N2)));
11531 Save_Entity_Descendants (N);
11534 Set_Associated_Node (N, Empty);
11535 Set_Etype (N, Empty);
11538 -- In Ada 2005, X.F may be a call to a primitive operation,
11539 -- rewritten as F (X). This rewriting will be done again in an
11540 -- instance, so keep the original node. Global entities will be
11541 -- captured as for other constructs.
11547 -- Entity is local. Reset in generic unit, so that node is resolved
11548 -- anew at the point of instantiation.
11551 Set_Associated_Node (N, Empty);
11552 Set_Etype (N, Empty);
11556 -----------------------------
11557 -- Save_Entity_Descendants --
11558 -----------------------------
11560 procedure Save_Entity_Descendants (N : Node_Id) is
11563 when N_Binary_Op =>
11564 Save_Global_Descendant (Union_Id (Left_Opnd (N)));
11565 Save_Global_Descendant (Union_Id (Right_Opnd (N)));
11568 Save_Global_Descendant (Union_Id (Right_Opnd (N)));
11570 when N_Expanded_Name | N_Selected_Component =>
11571 Save_Global_Descendant (Union_Id (Prefix (N)));
11572 Save_Global_Descendant (Union_Id (Selector_Name (N)));
11574 when N_Identifier | N_Character_Literal | N_Operator_Symbol =>
11578 raise Program_Error;
11580 end Save_Entity_Descendants;
11582 --------------------------
11583 -- Save_Global_Defaults --
11584 --------------------------
11586 procedure Save_Global_Defaults (N1, N2 : Node_Id) is
11587 Loc : constant Source_Ptr := Sloc (N1);
11588 Assoc2 : constant List_Id := Generic_Associations (N2);
11589 Gen_Id : constant Entity_Id := Get_Generic_Entity (N2);
11596 Actual : Entity_Id;
11599 Assoc1 := Generic_Associations (N1);
11601 if Present (Assoc1) then
11602 Act1 := First (Assoc1);
11605 Set_Generic_Associations (N1, New_List);
11606 Assoc1 := Generic_Associations (N1);
11609 if Present (Assoc2) then
11610 Act2 := First (Assoc2);
11615 while Present (Act1) and then Present (Act2) loop
11620 -- Find the associations added for default subprograms
11622 if Present (Act2) then
11623 while Nkind (Act2) /= N_Generic_Association
11624 or else No (Entity (Selector_Name (Act2)))
11625 or else not Is_Overloadable (Entity (Selector_Name (Act2)))
11630 -- Add a similar association if the default is global. The
11631 -- renaming declaration for the actual has been analyzed, and
11632 -- its alias is the program it renames. Link the actual in the
11633 -- original generic tree with the node in the analyzed tree.
11635 while Present (Act2) loop
11636 Subp := Entity (Selector_Name (Act2));
11637 Def := Explicit_Generic_Actual_Parameter (Act2);
11639 -- Following test is defence against rubbish errors
11641 if No (Alias (Subp)) then
11645 -- Retrieve the resolved actual from the renaming declaration
11646 -- created for the instantiated formal.
11648 Actual := Entity (Name (Parent (Parent (Subp))));
11649 Set_Entity (Def, Actual);
11650 Set_Etype (Def, Etype (Actual));
11652 if Is_Global (Actual) then
11654 Make_Generic_Association (Loc,
11655 Selector_Name => New_Occurrence_Of (Subp, Loc),
11656 Explicit_Generic_Actual_Parameter =>
11657 New_Occurrence_Of (Actual, Loc));
11659 Set_Associated_Node
11660 (Explicit_Generic_Actual_Parameter (Ndec), Def);
11662 Append (Ndec, Assoc1);
11664 -- If there are other defaults, add a dummy association in case
11665 -- there are other defaulted formals with the same name.
11667 elsif Present (Next (Act2)) then
11669 Make_Generic_Association (Loc,
11670 Selector_Name => New_Occurrence_Of (Subp, Loc),
11671 Explicit_Generic_Actual_Parameter => Empty);
11673 Append (Ndec, Assoc1);
11680 if Nkind (Name (N1)) = N_Identifier
11681 and then Is_Child_Unit (Gen_Id)
11682 and then Is_Global (Gen_Id)
11683 and then Is_Generic_Unit (Scope (Gen_Id))
11684 and then In_Open_Scopes (Scope (Gen_Id))
11686 -- This is an instantiation of a child unit within a sibling,
11687 -- so that the generic parent is in scope. An eventual instance
11688 -- must occur within the scope of an instance of the parent.
11689 -- Make name in instance into an expanded name, to preserve the
11690 -- identifier of the parent, so it can be resolved subsequently.
11692 Rewrite (Name (N2),
11693 Make_Expanded_Name (Loc,
11694 Chars => Chars (Gen_Id),
11695 Prefix => New_Occurrence_Of (Scope (Gen_Id), Loc),
11696 Selector_Name => New_Occurrence_Of (Gen_Id, Loc)));
11697 Set_Entity (Name (N2), Gen_Id);
11699 Rewrite (Name (N1),
11700 Make_Expanded_Name (Loc,
11701 Chars => Chars (Gen_Id),
11702 Prefix => New_Occurrence_Of (Scope (Gen_Id), Loc),
11703 Selector_Name => New_Occurrence_Of (Gen_Id, Loc)));
11705 Set_Associated_Node (Name (N1), Name (N2));
11706 Set_Associated_Node (Prefix (Name (N1)), Empty);
11707 Set_Associated_Node
11708 (Selector_Name (Name (N1)), Selector_Name (Name (N2)));
11709 Set_Etype (Name (N1), Etype (Gen_Id));
11712 end Save_Global_Defaults;
11714 ----------------------------
11715 -- Save_Global_Descendant --
11716 ----------------------------
11718 procedure Save_Global_Descendant (D : Union_Id) is
11722 if D in Node_Range then
11723 if D = Union_Id (Empty) then
11726 elsif Nkind (Node_Id (D)) /= N_Compilation_Unit then
11727 Save_References (Node_Id (D));
11730 elsif D in List_Range then
11731 if D = Union_Id (No_List)
11732 or else Is_Empty_List (List_Id (D))
11737 N1 := First (List_Id (D));
11738 while Present (N1) loop
11739 Save_References (N1);
11744 -- Element list or other non-node field, nothing to do
11749 end Save_Global_Descendant;
11751 ---------------------
11752 -- Save_References --
11753 ---------------------
11755 -- This is the recursive procedure that does the work, once the
11756 -- enclosing generic scope has been established. We have to treat
11757 -- specially a number of node rewritings that are required by semantic
11758 -- processing and which change the kind of nodes in the generic copy:
11759 -- typically constant-folding, replacing an operator node by a string
11760 -- literal, or a selected component by an expanded name. In each of
11761 -- those cases, the transformation is propagated to the generic unit.
11763 procedure Save_References (N : Node_Id) is
11768 elsif Nkind_In (N, N_Character_Literal, N_Operator_Symbol) then
11769 if Nkind (N) = Nkind (Get_Associated_Node (N)) then
11772 elsif Nkind (N) = N_Operator_Symbol
11773 and then Nkind (Get_Associated_Node (N)) = N_String_Literal
11775 Change_Operator_Symbol_To_String_Literal (N);
11778 elsif Nkind (N) in N_Op then
11779 if Nkind (N) = Nkind (Get_Associated_Node (N)) then
11780 if Nkind (N) = N_Op_Concat then
11781 Set_Is_Component_Left_Opnd (N,
11782 Is_Component_Left_Opnd (Get_Associated_Node (N)));
11784 Set_Is_Component_Right_Opnd (N,
11785 Is_Component_Right_Opnd (Get_Associated_Node (N)));
11791 -- Node may be transformed into call to a user-defined operator
11793 N2 := Get_Associated_Node (N);
11795 if Nkind (N2) = N_Function_Call then
11796 E := Entity (Name (N2));
11799 and then Is_Global (E)
11801 Set_Etype (N, Etype (N2));
11803 Set_Associated_Node (N, Empty);
11804 Set_Etype (N, Empty);
11807 elsif Nkind_In (N2, N_Integer_Literal,
11811 if Present (Original_Node (N2))
11812 and then Nkind (Original_Node (N2)) = Nkind (N)
11815 -- Operation was constant-folded. Whenever possible,
11816 -- recover semantic information from unfolded node,
11819 Set_Associated_Node (N, Original_Node (N2));
11821 if Nkind (N) = N_Op_Concat then
11822 Set_Is_Component_Left_Opnd (N,
11823 Is_Component_Left_Opnd (Get_Associated_Node (N)));
11824 Set_Is_Component_Right_Opnd (N,
11825 Is_Component_Right_Opnd (Get_Associated_Node (N)));
11831 -- If original node is already modified, propagate
11832 -- constant-folding to template.
11834 Rewrite (N, New_Copy (N2));
11835 Set_Analyzed (N, False);
11838 elsif Nkind (N2) = N_Identifier
11839 and then Ekind (Entity (N2)) = E_Enumeration_Literal
11841 -- Same if call was folded into a literal, but in this case
11842 -- retain the entity to avoid spurious ambiguities if id is
11843 -- overloaded at the point of instantiation or inlining.
11845 Rewrite (N, New_Copy (N2));
11846 Set_Analyzed (N, False);
11850 -- Complete operands check if node has not been constant-folded
11852 if Nkind (N) in N_Op then
11853 Save_Entity_Descendants (N);
11856 elsif Nkind (N) = N_Identifier then
11857 if Nkind (N) = Nkind (Get_Associated_Node (N)) then
11859 -- If this is a discriminant reference, always save it. It is
11860 -- used in the instance to find the corresponding discriminant
11861 -- positionally rather than by name.
11863 Set_Original_Discriminant
11864 (N, Original_Discriminant (Get_Associated_Node (N)));
11868 N2 := Get_Associated_Node (N);
11870 if Nkind (N2) = N_Function_Call then
11871 E := Entity (Name (N2));
11873 -- Name resolves to a call to parameterless function. If
11874 -- original entity is global, mark node as resolved.
11877 and then Is_Global (E)
11879 Set_Etype (N, Etype (N2));
11881 Set_Associated_Node (N, Empty);
11882 Set_Etype (N, Empty);
11885 elsif Nkind_In (N2, N_Integer_Literal, N_Real_Literal)
11886 and then Is_Entity_Name (Original_Node (N2))
11888 -- Name resolves to named number that is constant-folded,
11889 -- We must preserve the original name for ASIS use, and
11890 -- undo the constant-folding, which will be repeated in
11893 Set_Associated_Node (N, Original_Node (N2));
11896 elsif Nkind (N2) = N_String_Literal then
11898 -- Name resolves to string literal. Perform the same
11899 -- replacement in generic.
11901 Rewrite (N, New_Copy (N2));
11903 elsif Nkind (N2) = N_Explicit_Dereference then
11905 -- An identifier is rewritten as a dereference if it is
11906 -- the prefix in a selected component, and it denotes an
11907 -- access to a composite type, or a parameterless function
11908 -- call that returns an access type.
11910 -- Check whether corresponding entity in prefix is global
11912 if Is_Entity_Name (Prefix (N2))
11913 and then Present (Entity (Prefix (N2)))
11914 and then Is_Global (Entity (Prefix (N2)))
11917 Make_Explicit_Dereference (Sloc (N),
11918 Prefix => Make_Identifier (Sloc (N),
11919 Chars => Chars (N))));
11920 Set_Associated_Node (Prefix (N), Prefix (N2));
11922 elsif Nkind (Prefix (N2)) = N_Function_Call
11923 and then Is_Global (Entity (Name (Prefix (N2))))
11926 Make_Explicit_Dereference (Sloc (N),
11927 Prefix => Make_Function_Call (Sloc (N),
11929 Make_Identifier (Sloc (N),
11930 Chars => Chars (N)))));
11932 Set_Associated_Node
11933 (Name (Prefix (N)), Name (Prefix (N2)));
11936 Set_Associated_Node (N, Empty);
11937 Set_Etype (N, Empty);
11940 -- The subtype mark of a nominally unconstrained object is
11941 -- rewritten as a subtype indication using the bounds of the
11942 -- expression. Recover the original subtype mark.
11944 elsif Nkind (N2) = N_Subtype_Indication
11945 and then Is_Entity_Name (Original_Node (N2))
11947 Set_Associated_Node (N, Original_Node (N2));
11955 elsif Nkind (N) in N_Entity then
11960 Loc : constant Source_Ptr := Sloc (N);
11961 Qual : Node_Id := Empty;
11962 Typ : Entity_Id := Empty;
11965 use Atree.Unchecked_Access;
11966 -- This code section is part of implementing an untyped tree
11967 -- traversal, so it needs direct access to node fields.
11970 if Nkind_In (N, N_Aggregate, N_Extension_Aggregate) then
11971 N2 := Get_Associated_Node (N);
11978 -- In an instance within a generic, use the name of the
11979 -- actual and not the original generic parameter. If the
11980 -- actual is global in the current generic it must be
11981 -- preserved for its instantiation.
11983 if Nkind (Parent (Typ)) = N_Subtype_Declaration
11985 Present (Generic_Parent_Type (Parent (Typ)))
11987 Typ := Base_Type (Typ);
11988 Set_Etype (N2, Typ);
11994 or else not Is_Global (Typ)
11996 Set_Associated_Node (N, Empty);
11998 -- If the aggregate is an actual in a call, it has been
11999 -- resolved in the current context, to some local type.
12000 -- The enclosing call may have been disambiguated by the
12001 -- aggregate, and this disambiguation might fail at
12002 -- instantiation time because the type to which the
12003 -- aggregate did resolve is not preserved. In order to
12004 -- preserve some of this information, we wrap the
12005 -- aggregate in a qualified expression, using the id of
12006 -- its type. For further disambiguation we qualify the
12007 -- type name with its scope (if visible) because both
12008 -- id's will have corresponding entities in an instance.
12009 -- This resolves most of the problems with missing type
12010 -- information on aggregates in instances.
12012 if Nkind (N2) = Nkind (N)
12014 Nkind_In (Parent (N2), N_Procedure_Call_Statement,
12016 and then Comes_From_Source (Typ)
12018 if Is_Immediately_Visible (Scope (Typ)) then
12019 Nam := Make_Selected_Component (Loc,
12021 Make_Identifier (Loc, Chars (Scope (Typ))),
12023 Make_Identifier (Loc, Chars (Typ)));
12025 Nam := Make_Identifier (Loc, Chars (Typ));
12029 Make_Qualified_Expression (Loc,
12030 Subtype_Mark => Nam,
12031 Expression => Relocate_Node (N));
12035 Save_Global_Descendant (Field1 (N));
12036 Save_Global_Descendant (Field2 (N));
12037 Save_Global_Descendant (Field3 (N));
12038 Save_Global_Descendant (Field5 (N));
12040 if Present (Qual) then
12044 -- All other cases than aggregates
12047 Save_Global_Descendant (Field1 (N));
12048 Save_Global_Descendant (Field2 (N));
12049 Save_Global_Descendant (Field3 (N));
12050 Save_Global_Descendant (Field4 (N));
12051 Save_Global_Descendant (Field5 (N));
12055 end Save_References;
12057 -- Start of processing for Save_Global_References
12060 Gen_Scope := Current_Scope;
12062 -- If the generic unit is a child unit, references to entities in the
12063 -- parent are treated as local, because they will be resolved anew in
12064 -- the context of the instance of the parent.
12066 while Is_Child_Unit (Gen_Scope)
12067 and then Ekind (Scope (Gen_Scope)) = E_Generic_Package
12069 Gen_Scope := Scope (Gen_Scope);
12072 Save_References (N);
12073 end Save_Global_References;
12075 --------------------------------------
12076 -- Set_Copied_Sloc_For_Inlined_Body --
12077 --------------------------------------
12079 procedure Set_Copied_Sloc_For_Inlined_Body (N : Node_Id; E : Entity_Id) is
12081 Create_Instantiation_Source (N, E, True, S_Adjustment);
12082 end Set_Copied_Sloc_For_Inlined_Body;
12084 ---------------------
12085 -- Set_Instance_Of --
12086 ---------------------
12088 procedure Set_Instance_Of (A : Entity_Id; B : Entity_Id) is
12090 Generic_Renamings.Table (Generic_Renamings.Last) := (A, B, Assoc_Null);
12091 Generic_Renamings_HTable.Set (Generic_Renamings.Last);
12092 Generic_Renamings.Increment_Last;
12093 end Set_Instance_Of;
12095 --------------------
12096 -- Set_Next_Assoc --
12097 --------------------
12099 procedure Set_Next_Assoc (E : Assoc_Ptr; Next : Assoc_Ptr) is
12101 Generic_Renamings.Table (E).Next_In_HTable := Next;
12102 end Set_Next_Assoc;
12104 -------------------
12105 -- Start_Generic --
12106 -------------------
12108 procedure Start_Generic is
12110 -- ??? More things could be factored out in this routine.
12111 -- Should probably be done at a later stage.
12113 Generic_Flags.Append (Inside_A_Generic);
12114 Inside_A_Generic := True;
12116 Expander_Mode_Save_And_Set (False);
12119 ----------------------
12120 -- Set_Instance_Env --
12121 ----------------------
12123 procedure Set_Instance_Env
12124 (Gen_Unit : Entity_Id;
12125 Act_Unit : Entity_Id)
12128 -- Regardless of the current mode, predefined units are analyzed in
12129 -- the most current Ada mode, and earlier version Ada checks do not
12130 -- apply to predefined units. Nothing needs to be done for non-internal
12131 -- units. These are always analyzed in the current mode.
12133 if Is_Internal_File_Name
12134 (Fname => Unit_File_Name (Get_Source_Unit (Gen_Unit)),
12135 Renamings_Included => True)
12137 Set_Opt_Config_Switches (True, Current_Sem_Unit = Main_Unit);
12140 Current_Instantiated_Parent := (Gen_Unit, Act_Unit, Assoc_Null);
12141 end Set_Instance_Env;
12147 procedure Switch_View (T : Entity_Id) is
12148 BT : constant Entity_Id := Base_Type (T);
12149 Priv_Elmt : Elmt_Id := No_Elmt;
12150 Priv_Sub : Entity_Id;
12153 -- T may be private but its base type may have been exchanged through
12154 -- some other occurrence, in which case there is nothing to switch
12155 -- besides T itself. Note that a private dependent subtype of a private
12156 -- type might not have been switched even if the base type has been,
12157 -- because of the last branch of Check_Private_View (see comment there).
12159 if not Is_Private_Type (BT) then
12160 Prepend_Elmt (Full_View (T), Exchanged_Views);
12161 Exchange_Declarations (T);
12165 Priv_Elmt := First_Elmt (Private_Dependents (BT));
12167 if Present (Full_View (BT)) then
12168 Prepend_Elmt (Full_View (BT), Exchanged_Views);
12169 Exchange_Declarations (BT);
12172 while Present (Priv_Elmt) loop
12173 Priv_Sub := (Node (Priv_Elmt));
12175 -- We avoid flipping the subtype if the Etype of its full view is
12176 -- private because this would result in a malformed subtype. This
12177 -- occurs when the Etype of the subtype full view is the full view of
12178 -- the base type (and since the base types were just switched, the
12179 -- subtype is pointing to the wrong view). This is currently the case
12180 -- for tagged record types, access types (maybe more?) and needs to
12181 -- be resolved. ???
12183 if Present (Full_View (Priv_Sub))
12184 and then not Is_Private_Type (Etype (Full_View (Priv_Sub)))
12186 Prepend_Elmt (Full_View (Priv_Sub), Exchanged_Views);
12187 Exchange_Declarations (Priv_Sub);
12190 Next_Elmt (Priv_Elmt);
12194 -----------------------------
12195 -- Valid_Default_Attribute --
12196 -----------------------------
12198 procedure Valid_Default_Attribute (Nam : Entity_Id; Def : Node_Id) is
12199 Attr_Id : constant Attribute_Id :=
12200 Get_Attribute_Id (Attribute_Name (Def));
12201 T : constant Entity_Id := Entity (Prefix (Def));
12202 Is_Fun : constant Boolean := (Ekind (Nam) = E_Function);
12215 F := First_Formal (Nam);
12216 while Present (F) loop
12217 Num_F := Num_F + 1;
12222 when Attribute_Adjacent | Attribute_Ceiling | Attribute_Copy_Sign |
12223 Attribute_Floor | Attribute_Fraction | Attribute_Machine |
12224 Attribute_Model | Attribute_Remainder | Attribute_Rounding |
12225 Attribute_Unbiased_Rounding =>
12228 and then Is_Floating_Point_Type (T);
12230 when Attribute_Image | Attribute_Pred | Attribute_Succ |
12231 Attribute_Value | Attribute_Wide_Image |
12232 Attribute_Wide_Value =>
12233 OK := (Is_Fun and then Num_F = 1 and then Is_Scalar_Type (T));
12235 when Attribute_Max | Attribute_Min =>
12236 OK := (Is_Fun and then Num_F = 2 and then Is_Scalar_Type (T));
12238 when Attribute_Input =>
12239 OK := (Is_Fun and then Num_F = 1);
12241 when Attribute_Output | Attribute_Read | Attribute_Write =>
12242 OK := (not Is_Fun and then Num_F = 2);
12249 Error_Msg_N ("attribute reference has wrong profile for subprogram",
12252 end Valid_Default_Attribute;