1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
9 -- Copyright (C) 1992-2007, 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 2, 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 COPYING. If not, write --
19 -- to the Free Software Foundation, 51 Franklin Street, Fifth Floor, --
20 -- Boston, MA 02110-1301, USA. --
22 -- GNAT was originally developed by the GNAT team at New York University. --
23 -- Extensive contributions were provided by Ada Core Technologies Inc. --
25 ------------------------------------------------------------------------------
27 with Atree; use Atree;
28 with Einfo; use Einfo;
29 with Elists; use Elists;
30 with Errout; use Errout;
31 with Expander; use Expander;
32 with Fname; use Fname;
33 with Fname.UF; use Fname.UF;
34 with Freeze; use Freeze;
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_Cat; use Sem_Cat;
48 with Sem_Ch3; use Sem_Ch3;
49 with Sem_Ch6; use Sem_Ch6;
50 with Sem_Ch7; use Sem_Ch7;
51 with Sem_Ch8; use Sem_Ch8;
52 with Sem_Ch10; use Sem_Ch10;
53 with Sem_Ch13; use Sem_Ch13;
54 with Sem_Disp; use Sem_Disp;
55 with Sem_Elab; use Sem_Elab;
56 with Sem_Elim; use Sem_Elim;
57 with Sem_Eval; use Sem_Eval;
58 with Sem_Res; use Sem_Res;
59 with Sem_Type; use Sem_Type;
60 with Sem_Util; use Sem_Util;
61 with Sem_Warn; use Sem_Warn;
62 with Stand; use Stand;
63 with Sinfo; use Sinfo;
64 with Sinfo.CN; use Sinfo.CN;
65 with Sinput; use Sinput;
66 with Sinput.L; use Sinput.L;
67 with Snames; use Snames;
68 with Stringt; use Stringt;
69 with Uname; use Uname;
71 with Tbuild; use Tbuild;
72 with Uintp; use Uintp;
73 with Urealp; use Urealp;
77 package body Sem_Ch12 is
79 ----------------------------------------------------------
80 -- Implementation of Generic Analysis and Instantiation --
81 ----------------------------------------------------------
83 -- GNAT implements generics by macro expansion. No attempt is made to share
84 -- generic instantiations (for now). Analysis of a generic definition does
85 -- not perform any expansion action, but the expander must be called on the
86 -- tree for each instantiation, because the expansion may of course depend
87 -- on the generic actuals. All of this is best achieved as follows:
89 -- a) Semantic analysis of a generic unit is performed on a copy of the
90 -- tree for the generic unit. All tree modifications that follow analysis
91 -- do not affect the original tree. Links are kept between the original
92 -- tree and the copy, in order to recognize non-local references within
93 -- the generic, and propagate them to each instance (recall that name
94 -- resolution is done on the generic declaration: generics are not really
95 -- macros!). This is summarized in the following diagram:
97 -- .-----------. .----------.
98 -- | semantic |<--------------| generic |
100 -- | |==============>| |
101 -- |___________| global |__________|
112 -- b) Each instantiation copies the original tree, and inserts into it a
113 -- series of declarations that describe the mapping between generic formals
114 -- and actuals. For example, a generic In OUT parameter is an object
115 -- renaming of the corresponing actual, etc. Generic IN parameters are
116 -- constant declarations.
118 -- c) In order to give the right visibility for these renamings, we use
119 -- a different scheme for package and subprogram instantiations. For
120 -- packages, the list of renamings is inserted into the package
121 -- specification, before the visible declarations of the package. The
122 -- renamings are analyzed before any of the text of the instance, and are
123 -- thus visible at the right place. Furthermore, outside of the instance,
124 -- the generic parameters are visible and denote their corresponding
127 -- For subprograms, we create a container package to hold the renamings
128 -- and the subprogram instance itself. Analysis of the package makes the
129 -- renaming declarations visible to the subprogram. After analyzing the
130 -- package, the defining entity for the subprogram is touched-up so that
131 -- it appears declared in the current scope, and not inside the container
134 -- If the instantiation is a compilation unit, the container package is
135 -- given the same name as the subprogram instance. This ensures that
136 -- the elaboration procedure called by the binder, using the compilation
137 -- unit name, calls in fact the elaboration procedure for the package.
139 -- Not surprisingly, private types complicate this approach. By saving in
140 -- the original generic object the non-local references, we guarantee that
141 -- the proper entities are referenced at the point of instantiation.
142 -- However, for private types, this by itself does not insure that the
143 -- proper VIEW of the entity is used (the full type may be visible at the
144 -- point of generic definition, but not at instantiation, or vice-versa).
145 -- In order to reference the proper view, we special-case any reference
146 -- to private types in the generic object, by saving both views, one in
147 -- the generic and one in the semantic copy. At time of instantiation, we
148 -- check whether the two views are consistent, and exchange declarations if
149 -- necessary, in order to restore the correct visibility. Similarly, if
150 -- the instance view is private when the generic view was not, we perform
151 -- the exchange. After completing the instantiation, we restore the
152 -- current visibility. The flag Has_Private_View marks identifiers in the
153 -- the generic unit that require checking.
155 -- Visibility within nested generic units requires special handling.
156 -- Consider the following scheme:
158 -- type Global is ... -- outside of generic unit.
162 -- type Semi_Global is ... -- global to inner.
165 -- procedure inner (X1 : Global; X2 : Semi_Global);
167 -- procedure in2 is new inner (...); -- 4
170 -- package New_Outer is new Outer (...); -- 2
171 -- procedure New_Inner is new New_Outer.Inner (...); -- 3
173 -- The semantic analysis of Outer captures all occurrences of Global.
174 -- The semantic analysis of Inner (at 1) captures both occurrences of
175 -- Global and Semi_Global.
177 -- At point 2 (instantiation of Outer), we also produce a generic copy
178 -- of Inner, even though Inner is, at that point, not being instantiated.
179 -- (This is just part of the semantic analysis of New_Outer).
181 -- Critically, references to Global within Inner must be preserved, while
182 -- references to Semi_Global should not preserved, because they must now
183 -- resolve to an entity within New_Outer. To distinguish between these, we
184 -- use a global variable, Current_Instantiated_Parent, which is set when
185 -- performing a generic copy during instantiation (at 2). This variable is
186 -- used when performing a generic copy that is not an instantiation, but
187 -- that is nested within one, as the occurrence of 1 within 2. The analysis
188 -- of a nested generic only preserves references that are global to the
189 -- enclosing Current_Instantiated_Parent. We use the Scope_Depth value to
190 -- determine whether a reference is external to the given parent.
192 -- The instantiation at point 3 requires no special treatment. The method
193 -- works as well for further nestings of generic units, but of course the
194 -- variable Current_Instantiated_Parent must be stacked because nested
195 -- instantiations can occur, e.g. the occurrence of 4 within 2.
197 -- The instantiation of package and subprogram bodies is handled in a
198 -- similar manner, except that it is delayed until after semantic
199 -- analysis is complete. In this fashion complex cross-dependencies
200 -- between several package declarations and bodies containing generics
201 -- can be compiled which otherwise would diagnose spurious circularities.
203 -- For example, it is possible to compile two packages A and B that
204 -- have the following structure:
206 -- package A is package B is
207 -- generic ... generic ...
208 -- package G_A is package G_B is
211 -- package body A is package body B is
212 -- package N_B is new G_B (..) package N_A is new G_A (..)
214 -- The table Pending_Instantiations in package Inline is used to keep
215 -- track of body instantiations that are delayed in this manner. Inline
216 -- handles the actual calls to do the body instantiations. This activity
217 -- is part of Inline, since the processing occurs at the same point, and
218 -- for essentially the same reason, as the handling of inlined routines.
220 ----------------------------------------------
221 -- Detection of Instantiation Circularities --
222 ----------------------------------------------
224 -- If we have a chain of instantiations that is circular, this is static
225 -- error which must be detected at compile time. The detection of these
226 -- circularities is carried out at the point that we insert a generic
227 -- instance spec or body. If there is a circularity, then the analysis of
228 -- the offending spec or body will eventually result in trying to load the
229 -- same unit again, and we detect this problem as we analyze the package
230 -- instantiation for the second time.
232 -- At least in some cases after we have detected the circularity, we get
233 -- into trouble if we try to keep going. The following flag is set if a
234 -- circularity is detected, and used to abandon compilation after the
235 -- messages have been posted.
237 Circularity_Detected : Boolean := False;
238 -- This should really be reset on encountering a new main unit, but in
239 -- practice we are not using multiple main units so it is not critical.
241 -------------------------------------------------
242 -- Formal packages and partial parametrization --
243 -------------------------------------------------
245 -- When compiling a generic, a formal package is a local instantiation. If
246 -- declared with a box, its generic formals are visible in the enclosing
247 -- generic. If declared with a partial list of actuals, those actuals that
248 -- are defaulted (covered by an Others clause, or given an explicit box
249 -- initialization) are also visible in the enclosing generic, while those
250 -- that have a corresponding actual are not.
252 -- In our source model of instantiation, the same visibility must be
253 -- present in the spec and body of an instance: the names of the formals
254 -- that are defaulted must be made visible within the instance, and made
255 -- invisible (hidden) after the instantiation is complete, so that they
256 -- are not accessible outside of the instance.
258 -- In a generic, a formal package is treated like a special instantiation.
259 -- Our Ada95 compiler handled formals with and without box in different
260 -- ways. With partial parametrization, we use a single model for both.
261 -- We create a package declaration that consists of the specification of
262 -- the generic package, and a set of declarations that map the actuals
263 -- into local renamings, just as we do for bona fide instantiations. For
264 -- defaulted parameters and formals with a box, we copy directly the
265 -- declarations of the formal into this local package. The result is a
266 -- a package whose visible declarations may include generic formals. This
267 -- package is only used for type checking and visibility analysis, and
268 -- never reaches the back-end, so it can freely violate the placement
269 -- rules for generic formal declarations.
271 -- The list of declarations (renamings and copies of formals) is built
272 -- by Analyze_Associations, just as for regular instantiations.
274 -- At the point of instantiation, conformance checking must be applied only
275 -- to those parameters that were specified in the formal. We perform this
276 -- checking by creating another internal instantiation, this one including
277 -- only the renamings and the formals (the rest of the package spec is not
278 -- relevant to conformance checking). We can then traverse two lists: the
279 -- list of actuals in the instance that corresponds to the formal package,
280 -- and the list of actuals produced for this bogus instantiation. We apply
281 -- the conformance rules to those actuals that are not defaulted (i.e.
282 -- which still appear as generic formals.
284 -- When we compile an instance body we must make the right parameters
285 -- visible again. The predicate Is_Generic_Formal indicates which of the
286 -- formals should have its Is_Hidden flag reset.
288 -----------------------
289 -- Local subprograms --
290 -----------------------
292 procedure Abandon_Instantiation (N : Node_Id);
293 pragma No_Return (Abandon_Instantiation);
294 -- Posts an error message "instantiation abandoned" at the indicated node
295 -- and then raises the exception Instantiation_Error to do it.
297 procedure Analyze_Formal_Array_Type
298 (T : in out Entity_Id;
300 -- A formal array type is treated like an array type declaration, and
301 -- invokes Array_Type_Declaration (sem_ch3) whose first parameter is
302 -- in-out, because in the case of an anonymous type the entity is
303 -- actually created in the procedure.
305 -- The following procedures treat other kinds of formal parameters
307 procedure Analyze_Formal_Derived_Interface_Type
312 procedure Analyze_Formal_Derived_Type
317 procedure Analyze_Formal_Interface_Type
322 -- The following subprograms create abbreviated declarations for formal
323 -- scalar types. We introduce an anonymous base of the proper class for
324 -- each of them, and define the formals as constrained first subtypes of
325 -- their bases. The bounds are expressions that are non-static in the
328 procedure Analyze_Formal_Decimal_Fixed_Point_Type
329 (T : Entity_Id; Def : Node_Id);
330 procedure Analyze_Formal_Discrete_Type (T : Entity_Id; Def : Node_Id);
331 procedure Analyze_Formal_Floating_Type (T : Entity_Id; Def : Node_Id);
332 procedure Analyze_Formal_Signed_Integer_Type (T : Entity_Id; Def : Node_Id);
333 procedure Analyze_Formal_Modular_Type (T : Entity_Id; Def : Node_Id);
334 procedure Analyze_Formal_Ordinary_Fixed_Point_Type
335 (T : Entity_Id; Def : Node_Id);
337 procedure Analyze_Formal_Private_Type
341 -- Creates a new private type, which does not require completion
343 procedure Analyze_Generic_Formal_Part (N : Node_Id);
345 procedure Analyze_Generic_Access_Type (T : Entity_Id; Def : Node_Id);
346 -- Create a new access type with the given designated type
348 function Analyze_Associations
351 F_Copy : List_Id) return List_Id;
352 -- At instantiation time, build the list of associations between formals
353 -- and actuals. Each association becomes a renaming declaration for the
354 -- formal entity. F_Copy is the analyzed list of formals in the generic
355 -- copy. It is used to apply legality checks to the actuals. I_Node is the
356 -- instantiation node itself.
358 procedure Analyze_Subprogram_Instantiation
362 procedure Build_Instance_Compilation_Unit_Nodes
366 -- This procedure is used in the case where the generic instance of a
367 -- subprogram body or package body is a library unit. In this case, the
368 -- original library unit node for the generic instantiation must be
369 -- replaced by the resulting generic body, and a link made to a new
370 -- compilation unit node for the generic declaration. The argument N is
371 -- the original generic instantiation. Act_Body and Act_Decl are the body
372 -- and declaration of the instance (either package body and declaration
373 -- nodes or subprogram body and declaration nodes depending on the case).
374 -- On return, the node N has been rewritten with the actual body.
376 procedure Check_Access_Definition (N : Node_Id);
377 -- Subsidiary routine to null exclusion processing. Perform an assertion
378 -- check on Ada version and the presence of an access definition in N.
380 procedure Check_Formal_Packages (P_Id : Entity_Id);
381 -- Apply the following to all formal packages in generic associations
383 procedure Check_Formal_Package_Instance
384 (Formal_Pack : Entity_Id;
385 Actual_Pack : Entity_Id);
386 -- Verify that the actuals of the actual instance match the actuals of
387 -- the template for a formal package that is not declared with a box.
389 procedure Check_Forward_Instantiation (Decl : Node_Id);
390 -- If the generic is a local entity and the corresponding body has not
391 -- been seen yet, flag enclosing packages to indicate that it will be
392 -- elaborated after the generic body. Subprograms declared in the same
393 -- package cannot be inlined by the front-end because front-end inlining
394 -- requires a strict linear order of elaboration.
396 procedure Check_Hidden_Child_Unit
398 Gen_Unit : Entity_Id;
399 Act_Decl_Id : Entity_Id);
400 -- If the generic unit is an implicit child instance within a parent
401 -- instance, we need to make an explicit test that it is not hidden by
402 -- a child instance of the same name and parent.
404 procedure Check_Generic_Actuals
405 (Instance : Entity_Id;
406 Is_Formal_Box : Boolean);
407 -- Similar to previous one. Check the actuals in the instantiation,
408 -- whose views can change between the point of instantiation and the point
409 -- of instantiation of the body. In addition, mark the generic renamings
410 -- as generic actuals, so that they are not compatible with other actuals.
411 -- Recurse on an actual that is a formal package whose declaration has
414 function Contains_Instance_Of
417 N : Node_Id) return Boolean;
418 -- Inner is instantiated within the generic Outer. Check whether Inner
419 -- directly or indirectly contains an instance of Outer or of one of its
420 -- parents, in the case of a subunit. Each generic unit holds a list of
421 -- the entities instantiated within (at any depth). This procedure
422 -- determines whether the set of such lists contains a cycle, i.e. an
423 -- illegal circular instantiation.
425 function Denotes_Formal_Package
427 On_Exit : Boolean := False) return Boolean;
428 -- Returns True if E is a formal package of an enclosing generic, or
429 -- the actual for such a formal in an enclosing instantiation. If such
430 -- a package is used as a formal in an nested generic, or as an actual
431 -- in a nested instantiation, the visibility of ITS formals should not
432 -- be modified. When called from within Restore_Private_Views, the flag
433 -- On_Exit is true, to indicate that the search for a possible enclosing
434 -- instance should ignore the current one.
436 function Find_Actual_Type
438 Gen_Scope : Entity_Id) return Entity_Id;
439 -- When validating the actual types of a child instance, check whether
440 -- the formal is a formal type of the parent unit, and retrieve the current
441 -- actual for it. Typ is the entity in the analyzed formal type declaration
442 -- (component or index type of an array type, or designated type of an
443 -- access formal) and Gen_Scope is the scope of the analyzed formal array
444 -- or access type. The desired actual may be a formal of a parent, or may
445 -- be declared in a formal package of a parent. In both cases it is a
446 -- generic actual type because it appears within a visible instance.
447 -- Ambiguities may still arise if two homonyms are declared in two formal
448 -- packages, and the prefix of the formal type may be needed to resolve
449 -- the ambiguity in the instance ???
451 function In_Same_Declarative_Part
453 Inst : Node_Id) return Boolean;
454 -- True if the instantiation Inst and the given freeze_node F_Node appear
455 -- within the same declarative part, ignoring subunits, but with no inter-
456 -- vening suprograms or concurrent units. If true, the freeze node
457 -- of the instance can be placed after the freeze node of the parent,
458 -- which it itself an instance.
460 function In_Main_Context (E : Entity_Id) return Boolean;
461 -- Check whether an instantiation is in the context of the main unit.
462 -- Used to determine whether its body should be elaborated to allow
463 -- front-end inlining.
465 function Is_Generic_Formal (E : Entity_Id) return Boolean;
466 -- Utility to determine whether a given entity is declared by means of
467 -- of a formal parameter declaration. Used to set properly the visiblity
468 -- of generic formals of a generic package declared with a box or with
469 -- partial parametrization.
471 procedure Set_Instance_Env
472 (Gen_Unit : Entity_Id;
473 Act_Unit : Entity_Id);
474 -- Save current instance on saved environment, to be used to determine
475 -- the global status of entities in nested instances. Part of Save_Env.
476 -- called after verifying that the generic unit is legal for the instance,
477 -- The procedure also examines whether the generic unit is a predefined
478 -- unit, in order to set configuration switches accordingly. As a result
479 -- the procedure must be called after analyzing and freezing the actuals.
481 procedure Set_Instance_Of (A : Entity_Id; B : Entity_Id);
482 -- Associate analyzed generic parameter with corresponding
483 -- instance. Used for semantic checks at instantiation time.
485 function Has_Been_Exchanged (E : Entity_Id) return Boolean;
486 -- Traverse the Exchanged_Views list to see if a type was private
487 -- and has already been flipped during this phase of instantiation.
489 procedure Hide_Current_Scope;
490 -- When compiling a generic child unit, the parent context must be
491 -- present, but the instance and all entities that may be generated
492 -- must be inserted in the current scope. We leave the current scope
493 -- on the stack, but make its entities invisible to avoid visibility
494 -- problems. This is reversed at the end of instantiations. This is
495 -- not done for the instantiation of the bodies, which only require the
496 -- instances of the generic parents to be in scope.
498 procedure Install_Body
503 -- If the instantiation happens textually before the body of the generic,
504 -- the instantiation of the body must be analyzed after the generic body,
505 -- and not at the point of instantiation. Such early instantiations can
506 -- happen if the generic and the instance appear in a package declaration
507 -- because the generic body can only appear in the corresponding package
508 -- body. Early instantiations can also appear if generic, instance and
509 -- body are all in the declarative part of a subprogram or entry. Entities
510 -- of packages that are early instantiations are delayed, and their freeze
511 -- node appears after the generic body.
513 procedure Insert_After_Last_Decl (N : Node_Id; F_Node : Node_Id);
514 -- Insert freeze node at the end of the declarative part that includes the
515 -- instance node N. If N is in the visible part of an enclosing package
516 -- declaration, the freeze node has to be inserted at the end of the
517 -- private declarations, if any.
519 procedure Freeze_Subprogram_Body
520 (Inst_Node : Node_Id;
522 Pack_Id : Entity_Id);
523 -- The generic body may appear textually after the instance, including
524 -- in the proper body of a stub, or within a different package instance.
525 -- Given that the instance can only be elaborated after the generic, we
526 -- place freeze_nodes for the instance and/or for packages that may enclose
527 -- the instance and the generic, so that the back-end can establish the
528 -- proper order of elaboration.
531 -- Establish environment for subsequent instantiation. Separated from
532 -- Save_Env because data-structures for visibility handling must be
533 -- initialized before call to Check_Generic_Child_Unit.
535 procedure Install_Formal_Packages (Par : Entity_Id);
536 -- If any of the formals of the parent are formal packages with box,
537 -- their formal parts are visible in the parent and thus in the child
538 -- unit as well. Analogous to what is done in Check_Generic_Actuals
539 -- for the unit itself. This procedure is also used in an instance, to
540 -- make visible the proper entities of the actual for a formal package
541 -- declared with a box.
543 procedure Install_Parent (P : Entity_Id; In_Body : Boolean := False);
544 -- When compiling an instance of a child unit the parent (which is
545 -- itself an instance) is an enclosing scope that must be made
546 -- immediately visible. This procedure is also used to install the non-
547 -- generic parent of a generic child unit when compiling its body, so
548 -- that full views of types in the parent are made visible.
550 procedure Remove_Parent (In_Body : Boolean := False);
551 -- Reverse effect after instantiation of child is complete
553 procedure Inline_Instance_Body
555 Gen_Unit : Entity_Id;
557 -- If front-end inlining is requested, instantiate the package body,
558 -- and preserve the visibility of its compilation unit, to insure
559 -- that successive instantiations succeed.
561 -- The functions Instantiate_XXX perform various legality checks and build
562 -- the declarations for instantiated generic parameters. In all of these
563 -- Formal is the entity in the generic unit, Actual is the entity of
564 -- expression in the generic associations, and Analyzed_Formal is the
565 -- formal in the generic copy, which contains the semantic information to
566 -- be used to validate the actual.
568 function Instantiate_Object
571 Analyzed_Formal : Node_Id) return List_Id;
573 function Instantiate_Type
576 Analyzed_Formal : Node_Id;
577 Actual_Decls : List_Id) return List_Id;
579 function Instantiate_Formal_Subprogram
582 Analyzed_Formal : Node_Id) return Node_Id;
584 function Instantiate_Formal_Package
587 Analyzed_Formal : Node_Id) return List_Id;
588 -- If the formal package is declared with a box, special visibility rules
589 -- apply to its formals: they are in the visible part of the package. This
590 -- is true in the declarative region of the formal package, that is to say
591 -- in the enclosing generic or instantiation. For an instantiation, the
592 -- parameters of the formal package are made visible in an explicit step.
593 -- Furthermore, if the actual is a visible use_clause, these formals must
594 -- be made potentially use_visible as well. On exit from the enclosing
595 -- instantiation, the reverse must be done.
597 -- For a formal package declared without a box, there are conformance rules
598 -- that apply to the actuals in the generic declaration and the actuals of
599 -- the actual package in the enclosing instantiation. The simplest way to
600 -- apply these rules is to repeat the instantiation of the formal package
601 -- in the context of the enclosing instance, and compare the generic
602 -- associations of this instantiation with those of the actual package.
603 -- This internal instantiation only needs to contain the renamings of the
604 -- formals: the visible and private declarations themselves need not be
607 -- In Ada2005, the formal package may be only partially parametrized. In
608 -- that case the visibility step must make visible those actuals whose
609 -- corresponding formals were given with a box. A final complication
610 -- involves inherited operations from formal derived types, which must be
611 -- visible if the type is.
613 function Is_In_Main_Unit (N : Node_Id) return Boolean;
614 -- Test if given node is in the main unit
616 procedure Load_Parent_Of_Generic
619 Body_Optional : Boolean := False);
620 -- If the generic appears in a separate non-generic library unit, load the
621 -- corresponding body to retrieve the body of the generic. N is the node
622 -- for the generic instantiation, Spec is the generic package declaration.
624 -- Body_Optional is a flag that indicates that the body is being loaded to
625 -- ensure that temporaries are generated consistently when there are other
626 -- instances in the current declarative part that precede the one being
627 -- loaded. In that case a missing body is acceptable.
629 procedure Inherit_Context (Gen_Decl : Node_Id; Inst : Node_Id);
630 -- Add the context clause of the unit containing a generic unit to an
631 -- instantiation that is a compilation unit.
633 function Get_Associated_Node (N : Node_Id) return Node_Id;
634 -- In order to propagate semantic information back from the analyzed copy
635 -- to the original generic, we maintain links between selected nodes in the
636 -- generic and their corresponding copies. At the end of generic analysis,
637 -- the routine Save_Global_References traverses the generic tree, examines
638 -- the semantic information, and preserves the links to those nodes that
639 -- contain global information. At instantiation, the information from the
640 -- associated node is placed on the new copy, so that name resolution is
643 -- Three kinds of source nodes have associated nodes:
645 -- a) those that can reference (denote) entities, that is identifiers,
646 -- character literals, expanded_names, operator symbols, operators,
647 -- and attribute reference nodes. These nodes have an Entity field
648 -- and are the set of nodes that are in N_Has_Entity.
650 -- b) aggregates (N_Aggregate and N_Extension_Aggregate)
652 -- c) selected components (N_Selected_Component)
654 -- For the first class, the associated node preserves the entity if it is
655 -- global. If the generic contains nested instantiations, the associated
656 -- node itself has been recopied, and a chain of them must be followed.
658 -- For aggregates, the associated node allows retrieval of the type, which
659 -- may otherwise not appear in the generic. The view of this type may be
660 -- different between generic and instantiation, and the full view can be
661 -- installed before the instantiation is analyzed. For aggregates of type
662 -- extensions, the same view exchange may have to be performed for some of
663 -- the ancestor types, if their view is private at the point of
666 -- Nodes that are selected components in the parse tree may be rewritten
667 -- as expanded names after resolution, and must be treated as potential
668 -- entity holders. which is why they also have an Associated_Node.
670 -- Nodes that do not come from source, such as freeze nodes, do not appear
671 -- in the generic tree, and need not have an associated node.
673 -- The associated node is stored in the Associated_Node field. Note that
674 -- this field overlaps Entity, which is fine, because the whole point is
675 -- that we don't need or want the normal Entity field in this situation.
677 procedure Move_Freeze_Nodes
681 -- Freeze nodes can be generated in the analysis of a generic unit, but
682 -- will not be seen by the back-end. It is necessary to move those nodes
683 -- to the enclosing scope if they freeze an outer entity. We place them
684 -- at the end of the enclosing generic package, which is semantically
687 procedure Pre_Analyze_Actuals (N : Node_Id);
688 -- Analyze actuals to perform name resolution. Full resolution is done
689 -- later, when the expected types are known, but names have to be captured
690 -- before installing parents of generics, that are not visible for the
691 -- actuals themselves.
693 procedure Valid_Default_Attribute (Nam : Entity_Id; Def : Node_Id);
694 -- Verify that an attribute that appears as the default for a formal
695 -- subprogram is a function or procedure with the correct profile.
697 -------------------------------------------
698 -- Data Structures for Generic Renamings --
699 -------------------------------------------
701 -- The map Generic_Renamings associates generic entities with their
702 -- corresponding actuals. Currently used to validate type instances. It
703 -- will eventually be used for all generic parameters to eliminate the
704 -- need for overload resolution in the instance.
706 type Assoc_Ptr is new Int;
708 Assoc_Null : constant Assoc_Ptr := -1;
713 Next_In_HTable : Assoc_Ptr;
716 package Generic_Renamings is new Table.Table
717 (Table_Component_Type => Assoc,
718 Table_Index_Type => Assoc_Ptr,
719 Table_Low_Bound => 0,
721 Table_Increment => 100,
722 Table_Name => "Generic_Renamings");
724 -- Variable to hold enclosing instantiation. When the environment is
725 -- saved for a subprogram inlining, the corresponding Act_Id is empty.
727 Current_Instantiated_Parent : Assoc := (Empty, Empty, Assoc_Null);
729 -- Hash table for associations
731 HTable_Size : constant := 37;
732 type HTable_Range is range 0 .. HTable_Size - 1;
734 procedure Set_Next_Assoc (E : Assoc_Ptr; Next : Assoc_Ptr);
735 function Next_Assoc (E : Assoc_Ptr) return Assoc_Ptr;
736 function Get_Gen_Id (E : Assoc_Ptr) return Entity_Id;
737 function Hash (F : Entity_Id) return HTable_Range;
739 package Generic_Renamings_HTable is new GNAT.HTable.Static_HTable (
740 Header_Num => HTable_Range,
742 Elmt_Ptr => Assoc_Ptr,
743 Null_Ptr => Assoc_Null,
744 Set_Next => Set_Next_Assoc,
747 Get_Key => Get_Gen_Id,
751 Exchanged_Views : Elist_Id;
752 -- This list holds the private views that have been exchanged during
753 -- instantiation to restore the visibility of the generic declaration.
754 -- (see comments above). After instantiation, the current visibility is
755 -- reestablished by means of a traversal of this list.
757 Hidden_Entities : Elist_Id;
758 -- This list holds the entities of the current scope that are removed
759 -- from immediate visibility when instantiating a child unit. Their
760 -- visibility is restored in Remove_Parent.
762 -- Because instantiations can be recursive, the following must be saved
763 -- on entry and restored on exit from an instantiation (spec or body).
764 -- This is done by the two procedures Save_Env and Restore_Env. For
765 -- package and subprogram instantiations (but not for the body instances)
766 -- the action of Save_Env is done in two steps: Init_Env is called before
767 -- Check_Generic_Child_Unit, because setting the parent instances requires
768 -- that the visibility data structures be properly initialized. Once the
769 -- generic is unit is validated, Set_Instance_Env completes Save_Env.
771 Parent_Unit_Visible : Boolean := False;
772 -- Parent_Unit_Visible is used when the generic is a child unit, and
773 -- indicates whether the ultimate parent of the generic is visible in the
774 -- instantiation environment. It is used to reset the visibility of the
775 -- parent at the end of the instantiation (see Remove_Parent).
777 Instance_Parent_Unit : Entity_Id := Empty;
778 -- This records the ultimate parent unit of an instance of a generic
779 -- child unit and is used in conjunction with Parent_Unit_Visible to
780 -- indicate the unit to which the Parent_Unit_Visible flag corresponds.
782 type Instance_Env is record
783 Instantiated_Parent : Assoc;
784 Exchanged_Views : Elist_Id;
785 Hidden_Entities : Elist_Id;
786 Current_Sem_Unit : Unit_Number_Type;
787 Parent_Unit_Visible : Boolean := False;
788 Instance_Parent_Unit : Entity_Id := Empty;
789 Switches : Config_Switches_Type;
792 package Instance_Envs is new Table.Table (
793 Table_Component_Type => Instance_Env,
794 Table_Index_Type => Int,
795 Table_Low_Bound => 0,
797 Table_Increment => 100,
798 Table_Name => "Instance_Envs");
800 procedure Restore_Private_Views
801 (Pack_Id : Entity_Id;
802 Is_Package : Boolean := True);
803 -- Restore the private views of external types, and unmark the generic
804 -- renamings of actuals, so that they become comptible subtypes again.
805 -- For subprograms, Pack_Id is the package constructed to hold the
808 procedure Switch_View (T : Entity_Id);
809 -- Switch the partial and full views of a type and its private
810 -- dependents (i.e. its subtypes and derived types).
812 ------------------------------------
813 -- Structures for Error Reporting --
814 ------------------------------------
816 Instantiation_Node : Node_Id;
817 -- Used by subprograms that validate instantiation of formal parameters
818 -- where there might be no actual on which to place the error message.
819 -- Also used to locate the instantiation node for generic subunits.
821 Instantiation_Error : exception;
822 -- When there is a semantic error in the generic parameter matching,
823 -- there is no point in continuing the instantiation, because the
824 -- number of cascaded errors is unpredictable. This exception aborts
825 -- the instantiation process altogether.
827 S_Adjustment : Sloc_Adjustment;
828 -- Offset created for each node in an instantiation, in order to keep
829 -- track of the source position of the instantiation in each of its nodes.
830 -- A subsequent semantic error or warning on a construct of the instance
831 -- points to both places: the original generic node, and the point of
832 -- instantiation. See Sinput and Sinput.L for additional details.
834 ------------------------------------------------------------
835 -- Data structure for keeping track when inside a Generic --
836 ------------------------------------------------------------
838 -- The following table is used to save values of the Inside_A_Generic
839 -- flag (see spec of Sem) when they are saved by Start_Generic.
841 package Generic_Flags is new Table.Table (
842 Table_Component_Type => Boolean,
843 Table_Index_Type => Int,
844 Table_Low_Bound => 0,
846 Table_Increment => 200,
847 Table_Name => "Generic_Flags");
849 ---------------------------
850 -- Abandon_Instantiation --
851 ---------------------------
853 procedure Abandon_Instantiation (N : Node_Id) is
855 Error_Msg_N ("\instantiation abandoned!", N);
856 raise Instantiation_Error;
857 end Abandon_Instantiation;
859 --------------------------
860 -- Analyze_Associations --
861 --------------------------
863 function Analyze_Associations
866 F_Copy : List_Id) return List_Id
868 Actual_Types : constant Elist_Id := New_Elmt_List;
869 Assoc : constant List_Id := New_List;
870 Default_Actuals : constant Elist_Id := New_Elmt_List;
871 Gen_Unit : constant Entity_Id := Defining_Entity (Parent (F_Copy));
875 Next_Formal : Node_Id;
876 Temp_Formal : Node_Id;
877 Analyzed_Formal : Node_Id;
880 First_Named : Node_Id := Empty;
882 Default_Formals : constant List_Id := New_List;
883 -- If an Other_Choice is present, some of the formals may be defaulted.
884 -- To simplify the treatement of visibility in an instance, we introduce
885 -- individual defaults for each such formal. These defaults are
886 -- appended to the list of associations and replace the Others_Choice.
888 Found_Assoc : Node_Id;
889 -- Association for the current formal being match. Empty if there are
890 -- no remaining actuals, or if there is no named association with the
891 -- name of the formal.
893 Is_Named_Assoc : Boolean;
894 Num_Matched : Int := 0;
895 Num_Actuals : Int := 0;
897 Others_Present : Boolean := False;
898 -- In Ada 2005, indicates partial parametrization of of a formal
899 -- package. As usual an others association must be last in the list.
901 function Matching_Actual
903 A_F : Entity_Id) return Node_Id;
904 -- Find actual that corresponds to a given a formal parameter. If the
905 -- actuals are positional, return the next one, if any. If the actuals
906 -- are named, scan the parameter associations to find the right one.
907 -- A_F is the corresponding entity in the analyzed generic,which is
908 -- placed on the selector name for ASIS use.
910 -- In Ada 2005, a named association may be given with a box, in which
911 -- case Matching_Actual sets Found_Assoc to the generic association,
912 -- but return Empty for the actual itself. In this case the code below
913 -- creates a corresponding declaration for the formal.
915 function Partial_Parametrization return Boolean;
916 -- Ada 2005: if no match is found for a given formal, check if the
917 -- association for it includes a box, or whether the associations
918 -- include an Others clause.
920 procedure Process_Default (F : Entity_Id);
921 -- Add a copy of the declaration of generic formal F to the list of
922 -- associations, and add an explicit box association for F if there
923 -- is none yet, and the default comes from an Others_Choice.
925 procedure Set_Analyzed_Formal;
926 -- Find the node in the generic copy that corresponds to a given formal.
927 -- The semantic information on this node is used to perform legality
928 -- checks on the actuals. Because semantic analysis can introduce some
929 -- anonymous entities or modify the declaration node itself, the
930 -- correspondence between the two lists is not one-one. In addition to
931 -- anonymous types, the presence a formal equality will introduce an
932 -- implicit declaration for the corresponding inequality.
934 ---------------------
935 -- Matching_Actual --
936 ---------------------
938 function Matching_Actual
940 A_F : Entity_Id) return Node_Id
946 Is_Named_Assoc := False;
948 -- End of list of purely positional parameters
951 or else Nkind (Actual) = N_Others_Choice
953 Found_Assoc := Empty;
956 -- Case of positional parameter corresponding to current formal
958 elsif No (Selector_Name (Actual)) then
959 Found_Assoc := Actual;
960 Act := Explicit_Generic_Actual_Parameter (Actual);
961 Num_Matched := Num_Matched + 1;
964 -- Otherwise scan list of named actuals to find the one with the
965 -- desired name. All remaining actuals have explicit names.
968 Is_Named_Assoc := True;
969 Found_Assoc := Empty;
973 while Present (Actual) loop
974 if Chars (Selector_Name (Actual)) = Chars (F) then
975 Set_Entity (Selector_Name (Actual), A_F);
976 Set_Etype (Selector_Name (Actual), Etype (A_F));
977 Generate_Reference (A_F, Selector_Name (Actual));
978 Found_Assoc := Actual;
979 Act := Explicit_Generic_Actual_Parameter (Actual);
980 Num_Matched := Num_Matched + 1;
988 -- Reset for subsequent searches. In most cases the named
989 -- associations are in order. If they are not, we reorder them
990 -- to avoid scanning twice the same actual. This is not just a
991 -- question of efficiency: there may be multiple defaults with
992 -- boxes that have the same name. In a nested instantiation we
993 -- insert actuals for those defaults, and cannot rely on their
994 -- names to disambiguate them.
996 if Actual = First_Named then
999 elsif Present (Actual) then
1000 Insert_Before (First_Named, Remove_Next (Prev));
1003 Actual := First_Named;
1006 if Is_Entity_Name (Act) and then Present (Entity (Act)) then
1007 Set_Used_As_Generic_Actual (Entity (Act));
1011 end Matching_Actual;
1013 -----------------------------
1014 -- Partial_Parametrization --
1015 -----------------------------
1017 function Partial_Parametrization return Boolean is
1019 return Others_Present
1020 or else (Present (Found_Assoc) and then Box_Present (Found_Assoc));
1021 end Partial_Parametrization;
1023 ---------------------
1024 -- Process_Default --
1025 ---------------------
1027 procedure Process_Default (F : Entity_Id) is
1028 Loc : constant Source_Ptr := Sloc (I_Node);
1034 -- Append copy of formal declaration to associations, and create
1035 -- new defining identifier for it.
1037 Decl := New_Copy_Tree (F);
1039 if Nkind (F) = N_Formal_Concrete_Subprogram_Declaration then
1041 Make_Defining_Identifier (Sloc (Defining_Entity (F)),
1042 Chars => Chars (Defining_Entity (F)));
1043 Set_Defining_Unit_Name (Specification (Decl), Id);
1047 Make_Defining_Identifier (Sloc (Defining_Entity (F)),
1048 Chars => Chars (Defining_Identifier (F)));
1049 Set_Defining_Identifier (Decl, Id);
1052 Append (Decl, Assoc);
1054 if No (Found_Assoc) then
1056 Make_Generic_Association (Loc,
1057 Selector_Name => New_Occurrence_Of (Id, Loc),
1058 Explicit_Generic_Actual_Parameter => Empty);
1059 Set_Box_Present (Default);
1060 Append (Default, Default_Formals);
1062 end Process_Default;
1064 -------------------------
1065 -- Set_Analyzed_Formal --
1066 -------------------------
1068 procedure Set_Analyzed_Formal is
1071 while Present (Analyzed_Formal) loop
1072 Kind := Nkind (Analyzed_Formal);
1074 case Nkind (Formal) is
1076 when N_Formal_Subprogram_Declaration =>
1077 exit when Kind in N_Formal_Subprogram_Declaration
1080 (Defining_Unit_Name (Specification (Formal))) =
1082 (Defining_Unit_Name (Specification (Analyzed_Formal)));
1084 when N_Formal_Package_Declaration =>
1086 Kind = N_Formal_Package_Declaration
1088 Kind = N_Generic_Package_Declaration
1090 Kind = N_Package_Declaration;
1092 when N_Use_Package_Clause | N_Use_Type_Clause => exit;
1096 -- Skip freeze nodes, and nodes inserted to replace
1097 -- unrecognized pragmas.
1100 Kind not in N_Formal_Subprogram_Declaration
1101 and then Kind /= N_Subprogram_Declaration
1102 and then Kind /= N_Freeze_Entity
1103 and then Kind /= N_Null_Statement
1104 and then Kind /= N_Itype_Reference
1105 and then Chars (Defining_Identifier (Formal)) =
1106 Chars (Defining_Identifier (Analyzed_Formal));
1109 Next (Analyzed_Formal);
1111 end Set_Analyzed_Formal;
1113 -- Start of processing for Analyze_Associations
1116 Actuals := Generic_Associations (I_Node);
1118 if Present (Actuals) then
1120 -- check for an Others choice, indicating a partial parametrization
1121 -- for a formal package.
1123 Actual := First (Actuals);
1124 while Present (Actual) loop
1125 if Nkind (Actual) = N_Others_Choice then
1126 Others_Present := True;
1127 if Present (Next (Actual)) then
1128 Error_Msg_N ("others must be last association", Actual);
1131 -- This subprogram is used both for formal packages and for
1132 -- instantiations. For the latter, associations must all be
1135 if Nkind (I_Node) /= N_Formal_Package_Declaration
1136 and then Comes_From_Source (I_Node)
1139 ("others association not allowed in an instance",
1143 -- In any case, nothing to do after the others association
1147 elsif Box_Present (Actual)
1148 and then Comes_From_Source (I_Node)
1149 and then Nkind (I_Node) /= N_Formal_Package_Declaration
1152 ("box association not allowed in an instance", Actual);
1158 -- If named associations are present, save first named association
1159 -- (it may of course be Empty) to facilitate subsequent name search.
1161 First_Named := First (Actuals);
1162 while Present (First_Named)
1163 and then Nkind (First_Named) /= N_Others_Choice
1164 and then No (Selector_Name (First_Named))
1166 Num_Actuals := Num_Actuals + 1;
1171 Named := First_Named;
1172 while Present (Named) loop
1173 if Nkind (Named) /= N_Others_Choice
1174 and then No (Selector_Name (Named))
1176 Error_Msg_N ("invalid positional actual after named one", Named);
1177 Abandon_Instantiation (Named);
1180 -- A named association may lack an actual parameter, if it was
1181 -- introduced for a default subprogram that turns out to be local
1182 -- to the outer instantiation.
1184 if Nkind (Named) /= N_Others_Choice
1185 and then Present (Explicit_Generic_Actual_Parameter (Named))
1187 Num_Actuals := Num_Actuals + 1;
1193 if Present (Formals) then
1194 Formal := First_Non_Pragma (Formals);
1195 Analyzed_Formal := First_Non_Pragma (F_Copy);
1197 if Present (Actuals) then
1198 Actual := First (Actuals);
1200 -- All formals should have default values
1206 while Present (Formal) loop
1207 Set_Analyzed_Formal;
1208 Next_Formal := Next_Non_Pragma (Formal);
1210 case Nkind (Formal) is
1211 when N_Formal_Object_Declaration =>
1214 Defining_Identifier (Formal),
1215 Defining_Identifier (Analyzed_Formal));
1217 if No (Match) and then Partial_Parametrization then
1218 Process_Default (Formal);
1221 (Instantiate_Object (Formal, Match, Analyzed_Formal),
1225 when N_Formal_Type_Declaration =>
1228 Defining_Identifier (Formal),
1229 Defining_Identifier (Analyzed_Formal));
1232 if Partial_Parametrization then
1233 Process_Default (Formal);
1236 Error_Msg_Sloc := Sloc (Gen_Unit);
1240 Defining_Identifier (Formal));
1241 Error_Msg_NE ("\in instantiation of & declared#",
1242 Instantiation_Node, Gen_Unit);
1243 Abandon_Instantiation (Instantiation_Node);
1250 (Formal, Match, Analyzed_Formal, Assoc),
1253 -- An instantiation is a freeze point for the actuals,
1254 -- unless this is a rewritten formal package.
1256 if Nkind (I_Node) /= N_Formal_Package_Declaration then
1257 Append_Elmt (Entity (Match), Actual_Types);
1261 -- A remote access-to-class-wide type must not be an
1262 -- actual parameter for a generic formal of an access
1263 -- type (E.2.2 (17)).
1265 if Nkind (Analyzed_Formal) = N_Formal_Type_Declaration
1267 Nkind (Formal_Type_Definition (Analyzed_Formal)) =
1268 N_Access_To_Object_Definition
1270 Validate_Remote_Access_To_Class_Wide_Type (Match);
1273 when N_Formal_Subprogram_Declaration =>
1276 Defining_Unit_Name (Specification (Formal)),
1277 Defining_Unit_Name (Specification (Analyzed_Formal)));
1279 -- If the formal subprogram has the same name as
1280 -- another formal subprogram of the generic, then
1281 -- a named association is illegal (12.3(9)). Exclude
1282 -- named associations that are generated for a nested
1286 and then Is_Named_Assoc
1287 and then Comes_From_Source (Found_Assoc)
1289 Temp_Formal := First (Formals);
1290 while Present (Temp_Formal) loop
1291 if Nkind (Temp_Formal) in
1292 N_Formal_Subprogram_Declaration
1293 and then Temp_Formal /= Formal
1295 Chars (Selector_Name (Found_Assoc)) =
1296 Chars (Defining_Unit_Name
1297 (Specification (Temp_Formal)))
1300 ("name not allowed for overloaded formal",
1302 Abandon_Instantiation (Instantiation_Node);
1309 -- If there is no corresponding actual, this may be case of
1310 -- partial parametrization, or else the formal has a default
1314 and then Partial_Parametrization
1316 Process_Default (Formal);
1319 Instantiate_Formal_Subprogram
1320 (Formal, Match, Analyzed_Formal));
1323 -- If this is a nested generic, preserve default for later
1327 and then Box_Present (Formal)
1330 (Defining_Unit_Name (Specification (Last (Assoc))),
1334 when N_Formal_Package_Declaration =>
1337 Defining_Identifier (Formal),
1338 Defining_Identifier (Original_Node (Analyzed_Formal)));
1341 if Partial_Parametrization then
1342 Process_Default (Formal);
1345 Error_Msg_Sloc := Sloc (Gen_Unit);
1348 Instantiation_Node, Defining_Identifier (Formal));
1349 Error_Msg_NE ("\in instantiation of & declared#",
1350 Instantiation_Node, Gen_Unit);
1352 Abandon_Instantiation (Instantiation_Node);
1358 (Instantiate_Formal_Package
1359 (Formal, Match, Analyzed_Formal),
1363 -- For use type and use package appearing in the generic part,
1364 -- we have already copied them, so we can just move them where
1365 -- they belong (we mustn't recopy them since this would mess up
1366 -- the Sloc values).
1368 when N_Use_Package_Clause |
1369 N_Use_Type_Clause =>
1370 if Nkind (Original_Node (I_Node)) =
1371 N_Formal_Package_Declaration
1373 Append (New_Copy_Tree (Formal), Assoc);
1376 Append (Formal, Assoc);
1380 raise Program_Error;
1384 Formal := Next_Formal;
1385 Next_Non_Pragma (Analyzed_Formal);
1388 if Num_Actuals > Num_Matched then
1389 Error_Msg_Sloc := Sloc (Gen_Unit);
1391 if Present (Selector_Name (Actual)) then
1393 ("unmatched actual&",
1394 Actual, Selector_Name (Actual));
1395 Error_Msg_NE ("\in instantiation of& declared#",
1399 ("unmatched actual in instantiation of& declared#",
1404 elsif Present (Actuals) then
1406 ("too many actuals in generic instantiation", Instantiation_Node);
1410 Elmt : Elmt_Id := First_Elmt (Actual_Types);
1413 while Present (Elmt) loop
1414 Freeze_Before (I_Node, Node (Elmt));
1419 -- If there are default subprograms, normalize the tree by adding
1420 -- explicit associations for them. This is required if the instance
1421 -- appears within a generic.
1429 Elmt := First_Elmt (Default_Actuals);
1430 while Present (Elmt) loop
1431 if No (Actuals) then
1432 Actuals := New_List;
1433 Set_Generic_Associations (I_Node, Actuals);
1436 Subp := Node (Elmt);
1438 Make_Generic_Association (Sloc (Subp),
1439 Selector_Name => New_Occurrence_Of (Subp, Sloc (Subp)),
1440 Explicit_Generic_Actual_Parameter =>
1441 New_Occurrence_Of (Subp, Sloc (Subp)));
1442 Mark_Rewrite_Insertion (New_D);
1443 Append_To (Actuals, New_D);
1448 -- If this is a formal package. normalize the parameter list by adding
1449 -- explicit box asssociations for the formals that are covered by an
1452 if not Is_Empty_List (Default_Formals) then
1453 Append_List (Default_Formals, Formals);
1457 end Analyze_Associations;
1459 -------------------------------
1460 -- Analyze_Formal_Array_Type --
1461 -------------------------------
1463 procedure Analyze_Formal_Array_Type
1464 (T : in out Entity_Id;
1470 -- Treated like a non-generic array declaration, with additional
1475 if Nkind (Def) = N_Constrained_Array_Definition then
1476 DSS := First (Discrete_Subtype_Definitions (Def));
1477 while Present (DSS) loop
1478 if Nkind (DSS) = N_Subtype_Indication
1479 or else Nkind (DSS) = N_Range
1480 or else Nkind (DSS) = N_Attribute_Reference
1482 Error_Msg_N ("only a subtype mark is allowed in a formal", DSS);
1489 Array_Type_Declaration (T, Def);
1490 Set_Is_Generic_Type (Base_Type (T));
1492 if Ekind (Component_Type (T)) = E_Incomplete_Type
1493 and then No (Full_View (Component_Type (T)))
1495 Error_Msg_N ("premature usage of incomplete type", Def);
1497 -- Check that range constraint is not allowed on the component type
1498 -- of a generic formal array type (AARM 12.5.3(3))
1500 elsif Is_Internal (Component_Type (T))
1501 and then Present (Subtype_Indication (Component_Definition (Def)))
1502 and then Nkind (Original_Node
1503 (Subtype_Indication (Component_Definition (Def))))
1504 = N_Subtype_Indication
1507 ("in a formal, a subtype indication can only be "
1508 & "a subtype mark (RM 12.5.3(3))",
1509 Subtype_Indication (Component_Definition (Def)));
1512 end Analyze_Formal_Array_Type;
1514 ---------------------------------------------
1515 -- Analyze_Formal_Decimal_Fixed_Point_Type --
1516 ---------------------------------------------
1518 -- As for other generic types, we create a valid type representation with
1519 -- legal but arbitrary attributes, whose values are never considered
1520 -- static. For all scalar types we introduce an anonymous base type, with
1521 -- the same attributes. We choose the corresponding integer type to be
1522 -- Standard_Integer.
1524 procedure Analyze_Formal_Decimal_Fixed_Point_Type
1528 Loc : constant Source_Ptr := Sloc (Def);
1529 Base : constant Entity_Id :=
1531 (E_Decimal_Fixed_Point_Type,
1532 Current_Scope, Sloc (Def), 'G');
1533 Int_Base : constant Entity_Id := Standard_Integer;
1534 Delta_Val : constant Ureal := Ureal_1;
1535 Digs_Val : constant Uint := Uint_6;
1540 Set_Etype (Base, Base);
1541 Set_Size_Info (Base, Int_Base);
1542 Set_RM_Size (Base, RM_Size (Int_Base));
1543 Set_First_Rep_Item (Base, First_Rep_Item (Int_Base));
1544 Set_Digits_Value (Base, Digs_Val);
1545 Set_Delta_Value (Base, Delta_Val);
1546 Set_Small_Value (Base, Delta_Val);
1547 Set_Scalar_Range (Base,
1549 Low_Bound => Make_Real_Literal (Loc, Ureal_1),
1550 High_Bound => Make_Real_Literal (Loc, Ureal_1)));
1552 Set_Is_Generic_Type (Base);
1553 Set_Parent (Base, Parent (Def));
1555 Set_Ekind (T, E_Decimal_Fixed_Point_Subtype);
1556 Set_Etype (T, Base);
1557 Set_Size_Info (T, Int_Base);
1558 Set_RM_Size (T, RM_Size (Int_Base));
1559 Set_First_Rep_Item (T, First_Rep_Item (Int_Base));
1560 Set_Digits_Value (T, Digs_Val);
1561 Set_Delta_Value (T, Delta_Val);
1562 Set_Small_Value (T, Delta_Val);
1563 Set_Scalar_Range (T, Scalar_Range (Base));
1564 Set_Is_Constrained (T);
1566 Check_Restriction (No_Fixed_Point, Def);
1567 end Analyze_Formal_Decimal_Fixed_Point_Type;
1569 -------------------------------------------
1570 -- Analyze_Formal_Derived_Interface_Type --
1571 -------------------------------------------
1573 procedure Analyze_Formal_Derived_Interface_Type
1578 Loc : constant Source_Ptr := Sloc (Def);
1582 -- Rewrite as a type declaration of a derived type. This ensures that
1583 -- the interface list and primitive operations are properly captured.
1586 Make_Full_Type_Declaration (Loc,
1587 Defining_Identifier => T,
1588 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))));
1659 end Analyze_Formal_Derived_Type;
1661 ----------------------------------
1662 -- Analyze_Formal_Discrete_Type --
1663 ----------------------------------
1665 -- The operations defined for a discrete types are those of an enumeration
1666 -- type. The size is set to an arbitrary value, for use in analyzing the
1669 procedure Analyze_Formal_Discrete_Type (T : Entity_Id; Def : Node_Id) is
1670 Loc : constant Source_Ptr := Sloc (Def);
1674 Base : constant Entity_Id :=
1676 (E_Floating_Point_Type, Current_Scope, Sloc (Def), 'G');
1679 Set_Ekind (T, E_Enumeration_Subtype);
1680 Set_Etype (T, Base);
1683 Set_Is_Generic_Type (T);
1684 Set_Is_Constrained (T);
1686 -- For semantic analysis, the bounds of the type must be set to some
1687 -- non-static value. The simplest is to create attribute nodes for those
1688 -- bounds, that refer to the type itself. These bounds are never
1689 -- analyzed but serve as place-holders.
1692 Make_Attribute_Reference (Loc,
1693 Attribute_Name => Name_First,
1694 Prefix => New_Reference_To (T, Loc));
1698 Make_Attribute_Reference (Loc,
1699 Attribute_Name => Name_Last,
1700 Prefix => New_Reference_To (T, Loc));
1703 Set_Scalar_Range (T,
1708 Set_Ekind (Base, E_Enumeration_Type);
1709 Set_Etype (Base, Base);
1710 Init_Size (Base, 8);
1711 Init_Alignment (Base);
1712 Set_Is_Generic_Type (Base);
1713 Set_Scalar_Range (Base, Scalar_Range (T));
1714 Set_Parent (Base, Parent (Def));
1715 end Analyze_Formal_Discrete_Type;
1717 ----------------------------------
1718 -- Analyze_Formal_Floating_Type --
1719 ---------------------------------
1721 procedure Analyze_Formal_Floating_Type (T : Entity_Id; Def : Node_Id) is
1722 Base : constant Entity_Id :=
1724 (E_Floating_Point_Type, Current_Scope, Sloc (Def), 'G');
1727 -- The various semantic attributes are taken from the predefined type
1728 -- Float, just so that all of them are initialized. Their values are
1729 -- never used because no constant folding or expansion takes place in
1730 -- the generic itself.
1733 Set_Ekind (T, E_Floating_Point_Subtype);
1734 Set_Etype (T, Base);
1735 Set_Size_Info (T, (Standard_Float));
1736 Set_RM_Size (T, RM_Size (Standard_Float));
1737 Set_Digits_Value (T, Digits_Value (Standard_Float));
1738 Set_Scalar_Range (T, Scalar_Range (Standard_Float));
1739 Set_Is_Constrained (T);
1741 Set_Is_Generic_Type (Base);
1742 Set_Etype (Base, Base);
1743 Set_Size_Info (Base, (Standard_Float));
1744 Set_RM_Size (Base, RM_Size (Standard_Float));
1745 Set_Digits_Value (Base, Digits_Value (Standard_Float));
1746 Set_Scalar_Range (Base, Scalar_Range (Standard_Float));
1747 Set_Parent (Base, Parent (Def));
1749 Check_Restriction (No_Floating_Point, Def);
1750 end Analyze_Formal_Floating_Type;
1752 -----------------------------------
1753 -- Analyze_Formal_Interface_Type;--
1754 -----------------------------------
1756 procedure Analyze_Formal_Interface_Type
1761 Loc : constant Source_Ptr := Sloc (N);
1766 Make_Full_Type_Declaration (Loc,
1767 Defining_Identifier => T,
1768 Type_Definition => Def);
1772 Set_Is_Generic_Type (T);
1773 end Analyze_Formal_Interface_Type;
1775 ---------------------------------
1776 -- Analyze_Formal_Modular_Type --
1777 ---------------------------------
1779 procedure Analyze_Formal_Modular_Type (T : Entity_Id; Def : Node_Id) is
1781 -- Apart from their entity kind, generic modular types are treated like
1782 -- signed integer types, and have the same attributes.
1784 Analyze_Formal_Signed_Integer_Type (T, Def);
1785 Set_Ekind (T, E_Modular_Integer_Subtype);
1786 Set_Ekind (Etype (T), E_Modular_Integer_Type);
1788 end Analyze_Formal_Modular_Type;
1790 ---------------------------------------
1791 -- Analyze_Formal_Object_Declaration --
1792 ---------------------------------------
1794 procedure Analyze_Formal_Object_Declaration (N : Node_Id) is
1795 E : constant Node_Id := Default_Expression (N);
1796 Id : constant Node_Id := Defining_Identifier (N);
1803 -- Determine the mode of the formal object
1805 if Out_Present (N) then
1806 K := E_Generic_In_Out_Parameter;
1808 if not In_Present (N) then
1809 Error_Msg_N ("formal generic objects cannot have mode OUT", N);
1813 K := E_Generic_In_Parameter;
1816 if Present (Subtype_Mark (N)) then
1817 Find_Type (Subtype_Mark (N));
1818 T := Entity (Subtype_Mark (N));
1820 -- Ada 2005 (AI-423): Formal object with an access definition
1823 Check_Access_Definition (N);
1824 T := Access_Definition
1826 N => Access_Definition (N));
1829 if Ekind (T) = E_Incomplete_Type then
1831 Error_Node : Node_Id;
1834 if Present (Subtype_Mark (N)) then
1835 Error_Node := Subtype_Mark (N);
1837 Check_Access_Definition (N);
1838 Error_Node := Access_Definition (N);
1841 Error_Msg_N ("premature usage of incomplete type", Error_Node);
1845 if K = E_Generic_In_Parameter then
1847 -- Ada 2005 (AI-287): Limited aggregates allowed in generic formals
1849 if Ada_Version < Ada_05 and then Is_Limited_Type (T) then
1851 ("generic formal of mode IN must not be of limited type", N);
1852 Explain_Limited_Type (T, N);
1855 if Is_Abstract_Type (T) then
1857 ("generic formal of mode IN must not be of abstract type", N);
1861 Analyze_Per_Use_Expression (E, T);
1863 if Is_Limited_Type (T) and then not OK_For_Limited_Init (E) then
1865 ("initialization not allowed for limited types", E);
1866 Explain_Limited_Type (T, E);
1873 -- Case of generic IN OUT parameter
1876 -- If the formal has an unconstrained type, construct its actual
1877 -- subtype, as is done for subprogram formals. In this fashion, all
1878 -- its uses can refer to specific bounds.
1883 if (Is_Array_Type (T)
1884 and then not Is_Constrained (T))
1886 (Ekind (T) = E_Record_Type
1887 and then Has_Discriminants (T))
1890 Non_Freezing_Ref : constant Node_Id :=
1891 New_Reference_To (Id, Sloc (Id));
1895 -- Make sure the actual subtype doesn't generate bogus freezing
1897 Set_Must_Not_Freeze (Non_Freezing_Ref);
1898 Decl := Build_Actual_Subtype (T, Non_Freezing_Ref);
1899 Insert_Before_And_Analyze (N, Decl);
1900 Set_Actual_Subtype (Id, Defining_Identifier (Decl));
1903 Set_Actual_Subtype (Id, T);
1908 ("initialization not allowed for `IN OUT` formals", N);
1912 end Analyze_Formal_Object_Declaration;
1914 ----------------------------------------------
1915 -- Analyze_Formal_Ordinary_Fixed_Point_Type --
1916 ----------------------------------------------
1918 procedure Analyze_Formal_Ordinary_Fixed_Point_Type
1922 Loc : constant Source_Ptr := Sloc (Def);
1923 Base : constant Entity_Id :=
1925 (E_Ordinary_Fixed_Point_Type, Current_Scope, Sloc (Def), 'G');
1927 -- The semantic attributes are set for completeness only, their values
1928 -- will never be used, since all properties of the type are non-static.
1931 Set_Ekind (T, E_Ordinary_Fixed_Point_Subtype);
1932 Set_Etype (T, Base);
1933 Set_Size_Info (T, Standard_Integer);
1934 Set_RM_Size (T, RM_Size (Standard_Integer));
1935 Set_Small_Value (T, Ureal_1);
1936 Set_Delta_Value (T, Ureal_1);
1937 Set_Scalar_Range (T,
1939 Low_Bound => Make_Real_Literal (Loc, Ureal_1),
1940 High_Bound => Make_Real_Literal (Loc, Ureal_1)));
1941 Set_Is_Constrained (T);
1943 Set_Is_Generic_Type (Base);
1944 Set_Etype (Base, Base);
1945 Set_Size_Info (Base, Standard_Integer);
1946 Set_RM_Size (Base, RM_Size (Standard_Integer));
1947 Set_Small_Value (Base, Ureal_1);
1948 Set_Delta_Value (Base, Ureal_1);
1949 Set_Scalar_Range (Base, Scalar_Range (T));
1950 Set_Parent (Base, Parent (Def));
1952 Check_Restriction (No_Fixed_Point, Def);
1953 end Analyze_Formal_Ordinary_Fixed_Point_Type;
1955 ----------------------------
1956 -- Analyze_Formal_Package --
1957 ----------------------------
1959 procedure Analyze_Formal_Package (N : Node_Id) is
1960 Loc : constant Source_Ptr := Sloc (N);
1961 Pack_Id : constant Entity_Id := Defining_Identifier (N);
1963 Gen_Id : constant Node_Id := Name (N);
1965 Gen_Unit : Entity_Id;
1967 Parent_Installed : Boolean := False;
1969 Parent_Instance : Entity_Id;
1970 Renaming_In_Par : Entity_Id;
1971 No_Associations : Boolean := False;
1973 function Build_Local_Package return Node_Id;
1974 -- The formal package is rewritten so that its parameters are replaced
1975 -- with corresponding declarations. For parameters with bona fide
1976 -- associations these declarations are created by Analyze_Associations
1977 -- as for aa regular instantiation. For boxed parameters, we preserve
1978 -- the formal declarations and analyze them, in order to introduce
1979 -- entities of the right kind in the environment of the formal.
1981 -------------------------
1982 -- Build_Local_Package --
1983 -------------------------
1985 function Build_Local_Package return Node_Id is
1987 Pack_Decl : Node_Id;
1990 -- Within the formal, the name of the generic package is a renaming
1991 -- of the formal (as for a regular instantiation).
1994 Make_Package_Declaration (Loc,
1997 (Specification (Original_Node (Gen_Decl)),
1998 Empty, Instantiating => True));
2000 Renaming := Make_Package_Renaming_Declaration (Loc,
2001 Defining_Unit_Name =>
2002 Make_Defining_Identifier (Loc, Chars (Gen_Unit)),
2003 Name => New_Occurrence_Of (Formal, Loc));
2005 if Nkind (Gen_Id) = N_Identifier
2006 and then Chars (Gen_Id) = Chars (Pack_Id)
2009 ("& is hidden within declaration of instance", Gen_Id, Gen_Unit);
2012 -- If the formal is declared with a box, or with an others choice,
2013 -- create corresponding declarations for all entities in the formal
2014 -- part, so that names with the proper types are available in the
2015 -- specification of the formal package.
2017 if No_Associations then
2019 Formal_Decl : Node_Id;
2022 -- TBA : for a formal package, need to recurse ???
2027 (Generic_Formal_Declarations (Original_Node (Gen_Decl)));
2028 while Present (Formal_Decl) loop
2030 (Decls, Copy_Generic_Node (Formal_Decl, Empty, True));
2035 -- If generic associations are present, use Analyze_Associations to
2036 -- create the proper renaming declarations.
2040 Act_Tree : constant Node_Id :=
2042 (Original_Node (Gen_Decl), Empty,
2043 Instantiating => True);
2046 Generic_Renamings.Set_Last (0);
2047 Generic_Renamings_HTable.Reset;
2048 Instantiation_Node := N;
2051 Analyze_Associations
2053 Generic_Formal_Declarations (Act_Tree),
2054 Generic_Formal_Declarations (Gen_Decl));
2058 Append (Renaming, To => Decls);
2060 -- Add generated declarations ahead of local declarations in
2063 if No (Visible_Declarations (Specification (Pack_Decl))) then
2064 Set_Visible_Declarations (Specification (Pack_Decl), Decls);
2067 (First (Visible_Declarations (Specification (Pack_Decl))),
2072 end Build_Local_Package;
2074 -- Start of processing for Analyze_Formal_Package
2077 Text_IO_Kludge (Gen_Id);
2080 Check_Generic_Child_Unit (Gen_Id, Parent_Installed);
2081 Gen_Unit := Entity (Gen_Id);
2083 -- Check for a formal package that is a package renaming
2085 if Present (Renamed_Object (Gen_Unit)) then
2086 Gen_Unit := Renamed_Object (Gen_Unit);
2089 if Ekind (Gen_Unit) /= E_Generic_Package then
2090 Error_Msg_N ("expect generic package name", Gen_Id);
2094 elsif Gen_Unit = Current_Scope then
2096 ("generic package cannot be used as a formal package of itself",
2101 elsif In_Open_Scopes (Gen_Unit) then
2102 if Is_Compilation_Unit (Gen_Unit)
2103 and then Is_Child_Unit (Current_Scope)
2105 -- Special-case the error when the formal is a parent, and
2106 -- continue analysis to minimize cascaded errors.
2109 ("generic parent cannot be used as formal package "
2110 & "of a child unit",
2115 ("generic package cannot be used as a formal package "
2124 or else No (Generic_Associations (N))
2125 or else Nkind (First (Generic_Associations (N))) = N_Others_Choice
2127 No_Associations := True;
2130 -- If there are no generic associations, the generic parameters appear
2131 -- as local entities and are instantiated like them. We copy the generic
2132 -- package declaration as if it were an instantiation, and analyze it
2133 -- like a regular package, except that we treat the formals as
2134 -- additional visible components.
2136 Gen_Decl := Unit_Declaration_Node (Gen_Unit);
2138 if In_Extended_Main_Source_Unit (N) then
2139 Set_Is_Instantiated (Gen_Unit);
2140 Generate_Reference (Gen_Unit, N);
2143 Formal := New_Copy (Pack_Id);
2144 Create_Instantiation_Source (N, Gen_Unit, False, S_Adjustment);
2146 -- Make local generic without formals. The formals will be replaced with
2147 -- internal declarations.
2149 New_N := Build_Local_Package;
2151 Set_Defining_Unit_Name (Specification (New_N), Formal);
2152 Set_Generic_Parent (Specification (N), Gen_Unit);
2153 Set_Instance_Env (Gen_Unit, Formal);
2154 Set_Is_Generic_Instance (Formal);
2156 Enter_Name (Formal);
2157 Set_Ekind (Formal, E_Package);
2158 Set_Etype (Formal, Standard_Void_Type);
2159 Set_Inner_Instances (Formal, New_Elmt_List);
2160 Push_Scope (Formal);
2162 if Is_Child_Unit (Gen_Unit)
2163 and then Parent_Installed
2165 -- Similarly, we have to make the name of the formal visible in the
2166 -- parent instance, to resolve properly fully qualified names that
2167 -- may appear in the generic unit. The parent instance has been
2168 -- placed on the scope stack ahead of the current scope.
2170 Parent_Instance := Scope_Stack.Table (Scope_Stack.Last - 1).Entity;
2173 Make_Defining_Identifier (Loc, Chars (Gen_Unit));
2174 Set_Ekind (Renaming_In_Par, E_Package);
2175 Set_Etype (Renaming_In_Par, Standard_Void_Type);
2176 Set_Scope (Renaming_In_Par, Parent_Instance);
2177 Set_Parent (Renaming_In_Par, Parent (Formal));
2178 Set_Renamed_Object (Renaming_In_Par, Formal);
2179 Append_Entity (Renaming_In_Par, Parent_Instance);
2182 Analyze (Specification (N));
2184 -- The formals for which associations are provided are not visible
2185 -- outside of the formal package. The others are still declared by a
2186 -- formal parameter declaration.
2188 if not No_Associations then
2193 E := First_Entity (Formal);
2194 while Present (E) loop
2195 exit when Ekind (E) = E_Package
2196 and then Renamed_Entity (E) = Formal;
2198 if not Is_Generic_Formal (E) then
2207 End_Package_Scope (Formal);
2209 if Parent_Installed then
2215 -- Inside the generic unit, the formal package is a regular package, but
2216 -- no body is needed for it. Note that after instantiation, the defining
2217 -- unit name we need is in the new tree and not in the original (see
2218 -- Package_Instantiation). A generic formal package is an instance, and
2219 -- can be used as an actual for an inner instance.
2221 Set_Has_Completion (Formal, True);
2223 -- Add semantic information to the original defining identifier.
2226 Set_Ekind (Pack_Id, E_Package);
2227 Set_Etype (Pack_Id, Standard_Void_Type);
2228 Set_Scope (Pack_Id, Scope (Formal));
2229 Set_Has_Completion (Pack_Id, True);
2231 -- If there are errors in the parameter list, Analyze_Associations
2232 -- raises Instantiation_Error. Patch the declaration to prevent
2233 -- further exception propagation.
2236 when Instantiation_Error =>
2238 Enter_Name (Formal);
2239 Set_Ekind (Formal, E_Variable);
2240 Set_Etype (Formal, Any_Type);
2242 if Parent_Installed then
2245 end Analyze_Formal_Package;
2247 ---------------------------------
2248 -- Analyze_Formal_Private_Type --
2249 ---------------------------------
2251 procedure Analyze_Formal_Private_Type
2257 New_Private_Type (N, T, Def);
2259 -- Set the size to an arbitrary but legal value
2261 Set_Size_Info (T, Standard_Integer);
2262 Set_RM_Size (T, RM_Size (Standard_Integer));
2263 end Analyze_Formal_Private_Type;
2265 ----------------------------------------
2266 -- Analyze_Formal_Signed_Integer_Type --
2267 ----------------------------------------
2269 procedure Analyze_Formal_Signed_Integer_Type
2273 Base : constant Entity_Id :=
2275 (E_Signed_Integer_Type, Current_Scope, Sloc (Def), 'G');
2280 Set_Ekind (T, E_Signed_Integer_Subtype);
2281 Set_Etype (T, Base);
2282 Set_Size_Info (T, Standard_Integer);
2283 Set_RM_Size (T, RM_Size (Standard_Integer));
2284 Set_Scalar_Range (T, Scalar_Range (Standard_Integer));
2285 Set_Is_Constrained (T);
2287 Set_Is_Generic_Type (Base);
2288 Set_Size_Info (Base, Standard_Integer);
2289 Set_RM_Size (Base, RM_Size (Standard_Integer));
2290 Set_Etype (Base, Base);
2291 Set_Scalar_Range (Base, Scalar_Range (Standard_Integer));
2292 Set_Parent (Base, Parent (Def));
2293 end Analyze_Formal_Signed_Integer_Type;
2295 -------------------------------
2296 -- Analyze_Formal_Subprogram --
2297 -------------------------------
2299 procedure Analyze_Formal_Subprogram (N : Node_Id) is
2300 Spec : constant Node_Id := Specification (N);
2301 Def : constant Node_Id := Default_Name (N);
2302 Nam : constant Entity_Id := Defining_Unit_Name (Spec);
2310 if Nkind (Nam) = N_Defining_Program_Unit_Name then
2311 Error_Msg_N ("name of formal subprogram must be a direct name", Nam);
2315 Analyze_Subprogram_Declaration (N);
2316 Set_Is_Formal_Subprogram (Nam);
2317 Set_Has_Completion (Nam);
2319 if Nkind (N) = N_Formal_Abstract_Subprogram_Declaration then
2320 Set_Is_Abstract_Subprogram (Nam);
2321 Set_Is_Dispatching_Operation (Nam);
2324 Ctrl_Type : constant Entity_Id := Find_Dispatching_Type (Nam);
2326 if No (Ctrl_Type) then
2328 ("abstract formal subprogram must have a controlling type",
2331 Check_Controlling_Formals (Ctrl_Type, Nam);
2336 -- Default name is resolved at the point of instantiation
2338 if Box_Present (N) then
2341 -- Else default is bound at the point of generic declaration
2343 elsif Present (Def) then
2344 if Nkind (Def) = N_Operator_Symbol then
2345 Find_Direct_Name (Def);
2347 elsif Nkind (Def) /= N_Attribute_Reference then
2351 -- For an attribute reference, analyze the prefix and verify
2352 -- that it has the proper profile for the subprogram.
2354 Analyze (Prefix (Def));
2355 Valid_Default_Attribute (Nam, Def);
2359 -- Default name may be overloaded, in which case the interpretation
2360 -- with the correct profile must be selected, as for a renaming.
2362 if Etype (Def) = Any_Type then
2365 elsif Nkind (Def) = N_Selected_Component then
2366 Subp := Entity (Selector_Name (Def));
2368 if Ekind (Subp) /= E_Entry then
2369 Error_Msg_N ("expect valid subprogram name as default", Def);
2373 elsif Nkind (Def) = N_Indexed_Component then
2375 if Nkind (Prefix (Def)) /= N_Selected_Component then
2376 Error_Msg_N ("expect valid subprogram name as default", Def);
2380 Subp := Entity (Selector_Name (Prefix (Def)));
2382 if Ekind (Subp) /= E_Entry_Family then
2383 Error_Msg_N ("expect valid subprogram name as default", Def);
2388 elsif Nkind (Def) = N_Character_Literal then
2390 -- Needs some type checks: subprogram should be parameterless???
2392 Resolve (Def, (Etype (Nam)));
2394 elsif not Is_Entity_Name (Def)
2395 or else not Is_Overloadable (Entity (Def))
2397 Error_Msg_N ("expect valid subprogram name as default", Def);
2400 elsif not Is_Overloaded (Def) then
2401 Subp := Entity (Def);
2404 Error_Msg_N ("premature usage of formal subprogram", Def);
2406 elsif not Entity_Matches_Spec (Subp, Nam) then
2407 Error_Msg_N ("no visible entity matches specification", Def);
2413 I1 : Interp_Index := 0;
2419 Get_First_Interp (Def, I, It);
2420 while Present (It.Nam) loop
2422 if Entity_Matches_Spec (It.Nam, Nam) then
2423 if Subp /= Any_Id then
2424 It1 := Disambiguate (Def, I1, I, Etype (Subp));
2426 if It1 = No_Interp then
2427 Error_Msg_N ("ambiguous default subprogram", Def);
2440 Get_Next_Interp (I, It);
2444 if Subp /= Any_Id then
2445 Set_Entity (Def, Subp);
2448 Error_Msg_N ("premature usage of formal subprogram", Def);
2450 elsif Ekind (Subp) /= E_Operator then
2451 Check_Mode_Conformant (Subp, Nam);
2455 Error_Msg_N ("no visible subprogram matches specification", N);
2459 end Analyze_Formal_Subprogram;
2461 -------------------------------------
2462 -- Analyze_Formal_Type_Declaration --
2463 -------------------------------------
2465 procedure Analyze_Formal_Type_Declaration (N : Node_Id) is
2466 Def : constant Node_Id := Formal_Type_Definition (N);
2470 T := Defining_Identifier (N);
2472 if Present (Discriminant_Specifications (N))
2473 and then Nkind (Def) /= N_Formal_Private_Type_Definition
2476 ("discriminants not allowed for this formal type",
2477 Defining_Identifier (First (Discriminant_Specifications (N))));
2480 -- Enter the new name, and branch to specific routine
2483 when N_Formal_Private_Type_Definition =>
2484 Analyze_Formal_Private_Type (N, T, Def);
2486 when N_Formal_Derived_Type_Definition =>
2487 Analyze_Formal_Derived_Type (N, T, Def);
2489 when N_Formal_Discrete_Type_Definition =>
2490 Analyze_Formal_Discrete_Type (T, Def);
2492 when N_Formal_Signed_Integer_Type_Definition =>
2493 Analyze_Formal_Signed_Integer_Type (T, Def);
2495 when N_Formal_Modular_Type_Definition =>
2496 Analyze_Formal_Modular_Type (T, Def);
2498 when N_Formal_Floating_Point_Definition =>
2499 Analyze_Formal_Floating_Type (T, Def);
2501 when N_Formal_Ordinary_Fixed_Point_Definition =>
2502 Analyze_Formal_Ordinary_Fixed_Point_Type (T, Def);
2504 when N_Formal_Decimal_Fixed_Point_Definition =>
2505 Analyze_Formal_Decimal_Fixed_Point_Type (T, Def);
2507 when N_Array_Type_Definition =>
2508 Analyze_Formal_Array_Type (T, Def);
2510 when N_Access_To_Object_Definition |
2511 N_Access_Function_Definition |
2512 N_Access_Procedure_Definition =>
2513 Analyze_Generic_Access_Type (T, Def);
2515 -- Ada 2005: a interface declaration is encoded as an abstract
2516 -- record declaration or a abstract type derivation.
2518 when N_Record_Definition =>
2519 Analyze_Formal_Interface_Type (N, T, Def);
2521 when N_Derived_Type_Definition =>
2522 Analyze_Formal_Derived_Interface_Type (N, T, Def);
2528 raise Program_Error;
2532 Set_Is_Generic_Type (T);
2533 end Analyze_Formal_Type_Declaration;
2535 ------------------------------------
2536 -- Analyze_Function_Instantiation --
2537 ------------------------------------
2539 procedure Analyze_Function_Instantiation (N : Node_Id) is
2541 Analyze_Subprogram_Instantiation (N, E_Function);
2542 end Analyze_Function_Instantiation;
2544 ---------------------------------
2545 -- Analyze_Generic_Access_Type --
2546 ---------------------------------
2548 procedure Analyze_Generic_Access_Type (T : Entity_Id; Def : Node_Id) is
2552 if Nkind (Def) = N_Access_To_Object_Definition then
2553 Access_Type_Declaration (T, Def);
2555 if Is_Incomplete_Or_Private_Type (Designated_Type (T))
2556 and then No (Full_View (Designated_Type (T)))
2557 and then not Is_Generic_Type (Designated_Type (T))
2559 Error_Msg_N ("premature usage of incomplete type", Def);
2561 elsif Is_Internal (Designated_Type (T)) then
2563 ("only a subtype mark is allowed in a formal", Def);
2567 Access_Subprogram_Declaration (T, Def);
2569 end Analyze_Generic_Access_Type;
2571 ---------------------------------
2572 -- Analyze_Generic_Formal_Part --
2573 ---------------------------------
2575 procedure Analyze_Generic_Formal_Part (N : Node_Id) is
2576 Gen_Parm_Decl : Node_Id;
2579 -- The generic formals are processed in the scope of the generic unit,
2580 -- where they are immediately visible. The scope is installed by the
2583 Gen_Parm_Decl := First (Generic_Formal_Declarations (N));
2585 while Present (Gen_Parm_Decl) loop
2586 Analyze (Gen_Parm_Decl);
2587 Next (Gen_Parm_Decl);
2590 Generate_Reference_To_Generic_Formals (Current_Scope);
2591 end Analyze_Generic_Formal_Part;
2593 ------------------------------------------
2594 -- Analyze_Generic_Package_Declaration --
2595 ------------------------------------------
2597 procedure Analyze_Generic_Package_Declaration (N : Node_Id) is
2598 Loc : constant Source_Ptr := Sloc (N);
2601 Save_Parent : Node_Id;
2603 Decls : constant List_Id :=
2604 Visible_Declarations (Specification (N));
2608 -- We introduce a renaming of the enclosing package, to have a usable
2609 -- entity as the prefix of an expanded name for a local entity of the
2610 -- form Par.P.Q, where P is the generic package. This is because a local
2611 -- entity named P may hide it, so that the usual visibility rules in
2612 -- the instance will not resolve properly.
2615 Make_Package_Renaming_Declaration (Loc,
2616 Defining_Unit_Name =>
2617 Make_Defining_Identifier (Loc,
2618 Chars => New_External_Name (Chars (Defining_Entity (N)), "GH")),
2619 Name => Make_Identifier (Loc, Chars (Defining_Entity (N))));
2621 if Present (Decls) then
2622 Decl := First (Decls);
2623 while Present (Decl)
2624 and then Nkind (Decl) = N_Pragma
2629 if Present (Decl) then
2630 Insert_Before (Decl, Renaming);
2632 Append (Renaming, Visible_Declarations (Specification (N)));
2636 Set_Visible_Declarations (Specification (N), New_List (Renaming));
2639 -- Create copy of generic unit, and save for instantiation. If the unit
2640 -- is a child unit, do not copy the specifications for the parent, which
2641 -- are not part of the generic tree.
2643 Save_Parent := Parent_Spec (N);
2644 Set_Parent_Spec (N, Empty);
2646 New_N := Copy_Generic_Node (N, Empty, Instantiating => False);
2647 Set_Parent_Spec (New_N, Save_Parent);
2649 Id := Defining_Entity (N);
2650 Generate_Definition (Id);
2652 -- Expansion is not applied to generic units
2657 Set_Ekind (Id, E_Generic_Package);
2658 Set_Etype (Id, Standard_Void_Type);
2660 Enter_Generic_Scope (Id);
2661 Set_Inner_Instances (Id, New_Elmt_List);
2663 Set_Categorization_From_Pragmas (N);
2664 Set_Is_Pure (Id, Is_Pure (Current_Scope));
2666 -- Link the declaration of the generic homonym in the generic copy to
2667 -- the package it renames, so that it is always resolved properly.
2669 Set_Generic_Homonym (Id, Defining_Unit_Name (Renaming));
2670 Set_Entity (Associated_Node (Name (Renaming)), Id);
2672 -- For a library unit, we have reconstructed the entity for the unit,
2673 -- and must reset it in the library tables.
2675 if Nkind (Parent (N)) = N_Compilation_Unit then
2676 Set_Cunit_Entity (Current_Sem_Unit, Id);
2679 Analyze_Generic_Formal_Part (N);
2681 -- After processing the generic formals, analysis proceeds as for a
2682 -- non-generic package.
2684 Analyze (Specification (N));
2686 Validate_Categorization_Dependency (N, Id);
2690 End_Package_Scope (Id);
2691 Exit_Generic_Scope (Id);
2693 if Nkind (Parent (N)) /= N_Compilation_Unit then
2694 Move_Freeze_Nodes (Id, N, Visible_Declarations (Specification (N)));
2695 Move_Freeze_Nodes (Id, N, Private_Declarations (Specification (N)));
2696 Move_Freeze_Nodes (Id, N, Generic_Formal_Declarations (N));
2699 Set_Body_Required (Parent (N), Unit_Requires_Body (Id));
2700 Validate_RT_RAT_Component (N);
2702 -- If this is a spec without a body, check that generic parameters
2705 if not Body_Required (Parent (N)) then
2706 Check_References (Id);
2709 end Analyze_Generic_Package_Declaration;
2711 --------------------------------------------
2712 -- Analyze_Generic_Subprogram_Declaration --
2713 --------------------------------------------
2715 procedure Analyze_Generic_Subprogram_Declaration (N : Node_Id) is
2720 Result_Type : Entity_Id;
2721 Save_Parent : Node_Id;
2724 -- Create copy of generic unit,and save for instantiation. If the unit
2725 -- is a child unit, do not copy the specifications for the parent, which
2726 -- are not part of the generic tree.
2728 Save_Parent := Parent_Spec (N);
2729 Set_Parent_Spec (N, Empty);
2731 New_N := Copy_Generic_Node (N, Empty, Instantiating => False);
2732 Set_Parent_Spec (New_N, Save_Parent);
2735 Spec := Specification (N);
2736 Id := Defining_Entity (Spec);
2737 Generate_Definition (Id);
2739 if Nkind (Id) = N_Defining_Operator_Symbol then
2741 ("operator symbol not allowed for generic subprogram", Id);
2748 Set_Scope_Depth_Value (Id, Scope_Depth (Current_Scope) + 1);
2750 Enter_Generic_Scope (Id);
2751 Set_Inner_Instances (Id, New_Elmt_List);
2752 Set_Is_Pure (Id, Is_Pure (Current_Scope));
2754 Analyze_Generic_Formal_Part (N);
2756 Formals := Parameter_Specifications (Spec);
2758 if Present (Formals) then
2759 Process_Formals (Formals, Spec);
2762 if Nkind (Spec) = N_Function_Specification then
2763 Set_Ekind (Id, E_Generic_Function);
2765 if Nkind (Result_Definition (Spec)) = N_Access_Definition then
2766 Result_Type := Access_Definition (Spec, Result_Definition (Spec));
2767 Set_Etype (Id, Result_Type);
2769 Find_Type (Result_Definition (Spec));
2770 Set_Etype (Id, Entity (Result_Definition (Spec)));
2774 Set_Ekind (Id, E_Generic_Procedure);
2775 Set_Etype (Id, Standard_Void_Type);
2778 -- For a library unit, we have reconstructed the entity for the unit,
2779 -- and must reset it in the library tables. We also make sure that
2780 -- Body_Required is set properly in the original compilation unit node.
2782 if Nkind (Parent (N)) = N_Compilation_Unit then
2783 Set_Cunit_Entity (Current_Sem_Unit, Id);
2784 Set_Body_Required (Parent (N), Unit_Requires_Body (Id));
2787 Set_Categorization_From_Pragmas (N);
2788 Validate_Categorization_Dependency (N, Id);
2790 Save_Global_References (Original_Node (N));
2794 Exit_Generic_Scope (Id);
2795 Generate_Reference_To_Formals (Id);
2796 end Analyze_Generic_Subprogram_Declaration;
2798 -----------------------------------
2799 -- Analyze_Package_Instantiation --
2800 -----------------------------------
2802 procedure Analyze_Package_Instantiation (N : Node_Id) is
2803 Loc : constant Source_Ptr := Sloc (N);
2804 Gen_Id : constant Node_Id := Name (N);
2807 Act_Decl_Name : Node_Id;
2808 Act_Decl_Id : Entity_Id;
2813 Gen_Unit : Entity_Id;
2815 Is_Actual_Pack : constant Boolean :=
2816 Is_Internal (Defining_Entity (N));
2818 Env_Installed : Boolean := False;
2819 Parent_Installed : Boolean := False;
2820 Renaming_List : List_Id;
2821 Unit_Renaming : Node_Id;
2822 Needs_Body : Boolean;
2823 Inline_Now : Boolean := False;
2825 procedure Delay_Descriptors (E : Entity_Id);
2826 -- Delay generation of subprogram descriptors for given entity
2828 function Might_Inline_Subp return Boolean;
2829 -- If inlining is active and the generic contains inlined subprograms,
2830 -- we instantiate the body. This may cause superfluous instantiations,
2831 -- but it is simpler than detecting the need for the body at the point
2832 -- of inlining, when the context of the instance is not available.
2834 -----------------------
2835 -- Delay_Descriptors --
2836 -----------------------
2838 procedure Delay_Descriptors (E : Entity_Id) is
2840 if not Delay_Subprogram_Descriptors (E) then
2841 Set_Delay_Subprogram_Descriptors (E);
2842 Pending_Descriptor.Append (E);
2844 end Delay_Descriptors;
2846 -----------------------
2847 -- Might_Inline_Subp --
2848 -----------------------
2850 function Might_Inline_Subp return Boolean is
2854 if not Inline_Processing_Required then
2858 E := First_Entity (Gen_Unit);
2859 while Present (E) loop
2860 if Is_Subprogram (E)
2861 and then Is_Inlined (E)
2871 end Might_Inline_Subp;
2873 -- Start of processing for Analyze_Package_Instantiation
2876 -- Very first thing: apply the special kludge for Text_IO processing
2877 -- in case we are instantiating one of the children of [Wide_]Text_IO.
2879 Text_IO_Kludge (Name (N));
2881 -- Make node global for error reporting
2883 Instantiation_Node := N;
2885 -- Case of instantiation of a generic package
2887 if Nkind (N) = N_Package_Instantiation then
2888 Act_Decl_Id := New_Copy (Defining_Entity (N));
2889 Set_Comes_From_Source (Act_Decl_Id, True);
2891 if Nkind (Defining_Unit_Name (N)) = N_Defining_Program_Unit_Name then
2893 Make_Defining_Program_Unit_Name (Loc,
2894 Name => New_Copy_Tree (Name (Defining_Unit_Name (N))),
2895 Defining_Identifier => Act_Decl_Id);
2897 Act_Decl_Name := Act_Decl_Id;
2900 -- Case of instantiation of a formal package
2903 Act_Decl_Id := Defining_Identifier (N);
2904 Act_Decl_Name := Act_Decl_Id;
2907 Generate_Definition (Act_Decl_Id);
2908 Pre_Analyze_Actuals (N);
2911 Env_Installed := True;
2912 Check_Generic_Child_Unit (Gen_Id, Parent_Installed);
2913 Gen_Unit := Entity (Gen_Id);
2915 -- Verify that it is the name of a generic package
2917 if Etype (Gen_Unit) = Any_Type then
2921 elsif Ekind (Gen_Unit) /= E_Generic_Package then
2923 -- Ada 2005 (AI-50217): Cannot use instance in limited with_clause
2925 if From_With_Type (Gen_Unit) then
2927 ("cannot instantiate a limited withed package", Gen_Id);
2930 ("expect name of generic package in instantiation", Gen_Id);
2937 if In_Extended_Main_Source_Unit (N) then
2938 Set_Is_Instantiated (Gen_Unit);
2939 Generate_Reference (Gen_Unit, N);
2941 if Present (Renamed_Object (Gen_Unit)) then
2942 Set_Is_Instantiated (Renamed_Object (Gen_Unit));
2943 Generate_Reference (Renamed_Object (Gen_Unit), N);
2947 if Nkind (Gen_Id) = N_Identifier
2948 and then Chars (Gen_Unit) = Chars (Defining_Entity (N))
2951 ("& is hidden within declaration of instance", Gen_Id, Gen_Unit);
2953 elsif Nkind (Gen_Id) = N_Expanded_Name
2954 and then Is_Child_Unit (Gen_Unit)
2955 and then Nkind (Prefix (Gen_Id)) = N_Identifier
2956 and then Chars (Act_Decl_Id) = Chars (Prefix (Gen_Id))
2959 ("& is hidden within declaration of instance ", Prefix (Gen_Id));
2962 Set_Entity (Gen_Id, Gen_Unit);
2964 -- If generic is a renaming, get original generic unit
2966 if Present (Renamed_Object (Gen_Unit))
2967 and then Ekind (Renamed_Object (Gen_Unit)) = E_Generic_Package
2969 Gen_Unit := Renamed_Object (Gen_Unit);
2972 -- Verify that there are no circular instantiations
2974 if In_Open_Scopes (Gen_Unit) then
2975 Error_Msg_NE ("instantiation of & within itself", N, Gen_Unit);
2979 elsif Contains_Instance_Of (Gen_Unit, Current_Scope, Gen_Id) then
2980 Error_Msg_Node_2 := Current_Scope;
2982 ("circular Instantiation: & instantiated in &!", N, Gen_Unit);
2983 Circularity_Detected := True;
2988 Gen_Decl := Unit_Declaration_Node (Gen_Unit);
2990 -- Initialize renamings map, for error checking, and the list that
2991 -- holds private entities whose views have changed between generic
2992 -- definition and instantiation. If this is the instance created to
2993 -- validate an actual package, the instantiation environment is that
2994 -- of the enclosing instance.
2996 Generic_Renamings.Set_Last (0);
2997 Generic_Renamings_HTable.Reset;
2999 Create_Instantiation_Source (N, Gen_Unit, False, S_Adjustment);
3001 -- Copy original generic tree, to produce text for instantiation
3005 (Original_Node (Gen_Decl), Empty, Instantiating => True);
3007 Act_Spec := Specification (Act_Tree);
3009 -- If this is the instance created to validate an actual package,
3010 -- only the formals matter, do not examine the package spec itself.
3012 if Is_Actual_Pack then
3013 Set_Visible_Declarations (Act_Spec, New_List);
3014 Set_Private_Declarations (Act_Spec, New_List);
3018 Analyze_Associations
3020 Generic_Formal_Declarations (Act_Tree),
3021 Generic_Formal_Declarations (Gen_Decl));
3023 Set_Instance_Env (Gen_Unit, Act_Decl_Id);
3024 Set_Defining_Unit_Name (Act_Spec, Act_Decl_Name);
3025 Set_Is_Generic_Instance (Act_Decl_Id);
3027 Set_Generic_Parent (Act_Spec, Gen_Unit);
3029 -- References to the generic in its own declaration or its body are
3030 -- references to the instance. Add a renaming declaration for the
3031 -- generic unit itself. This declaration, as well as the renaming
3032 -- declarations for the generic formals, must remain private to the
3033 -- unit: the formals, because this is the language semantics, and
3034 -- the unit because its use is an artifact of the implementation.
3037 Make_Package_Renaming_Declaration (Loc,
3038 Defining_Unit_Name =>
3039 Make_Defining_Identifier (Loc, Chars (Gen_Unit)),
3040 Name => New_Reference_To (Act_Decl_Id, Loc));
3042 Append (Unit_Renaming, Renaming_List);
3044 -- The renaming declarations are the first local declarations of
3047 if Is_Non_Empty_List (Visible_Declarations (Act_Spec)) then
3049 (First (Visible_Declarations (Act_Spec)), Renaming_List);
3051 Set_Visible_Declarations (Act_Spec, Renaming_List);
3055 Make_Package_Declaration (Loc,
3056 Specification => Act_Spec);
3058 -- Save the instantiation node, for subsequent instantiation of the
3059 -- body, if there is one and we are generating code for the current
3060 -- unit. Mark the unit as having a body, to avoid a premature error
3063 -- We instantiate the body if we are generating code, if we are
3064 -- generating cross-reference information, or if we are building
3065 -- trees for ASIS use.
3068 Enclosing_Body_Present : Boolean := False;
3069 -- If the generic unit is not a compilation unit, then a body may
3070 -- be present in its parent even if none is required. We create a
3071 -- tentative pending instantiation for the body, which will be
3072 -- discarded if none is actually present.
3077 if Scope (Gen_Unit) /= Standard_Standard
3078 and then not Is_Child_Unit (Gen_Unit)
3080 Scop := Scope (Gen_Unit);
3082 while Present (Scop)
3083 and then Scop /= Standard_Standard
3085 if Unit_Requires_Body (Scop) then
3086 Enclosing_Body_Present := True;
3089 elsif In_Open_Scopes (Scop)
3090 and then In_Package_Body (Scop)
3092 Enclosing_Body_Present := True;
3096 exit when Is_Compilation_Unit (Scop);
3097 Scop := Scope (Scop);
3101 -- If front-end inlining is enabled, and this is a unit for which
3102 -- code will be generated, we instantiate the body at once.
3104 -- This is done if the instance is not the main unit, and if the
3105 -- generic is not a child unit of another generic, to avoid scope
3106 -- problems and the reinstallation of parent instances.
3109 and then (not Is_Child_Unit (Gen_Unit)
3110 or else not Is_Generic_Unit (Scope (Gen_Unit)))
3111 and then Might_Inline_Subp
3112 and then not Is_Actual_Pack
3114 if Front_End_Inlining
3115 and then (Is_In_Main_Unit (N)
3116 or else In_Main_Context (Current_Scope))
3117 and then Nkind (Parent (N)) /= N_Compilation_Unit
3121 -- In configurable_run_time mode we force the inlining of
3122 -- predefined subprogram marked Inline_Always, to minimize
3123 -- the use of the run-time library.
3125 elsif Is_Predefined_File_Name
3126 (Unit_File_Name (Get_Source_Unit (Gen_Decl)))
3127 and then Configurable_Run_Time_Mode
3128 and then Nkind (Parent (N)) /= N_Compilation_Unit
3133 -- If the current scope is itself an instance within a child
3134 -- unit, there will be duplications in the scope stack, and the
3135 -- unstacking mechanism in Inline_Instance_Body will fail.
3136 -- This loses some rare cases of optimization, and might be
3137 -- improved some day, if we can find a proper abstraction for
3138 -- "the complete compilation context" that can be saved and
3141 if Is_Generic_Instance (Current_Scope) then
3143 Curr_Unit : constant Entity_Id :=
3144 Cunit_Entity (Current_Sem_Unit);
3146 if Curr_Unit /= Current_Scope
3147 and then Is_Child_Unit (Curr_Unit)
3149 Inline_Now := False;
3156 (Unit_Requires_Body (Gen_Unit)
3157 or else Enclosing_Body_Present
3158 or else Present (Corresponding_Body (Gen_Decl)))
3159 and then (Is_In_Main_Unit (N)
3160 or else Might_Inline_Subp)
3161 and then not Is_Actual_Pack
3162 and then not Inline_Now
3163 and then (Operating_Mode = Generate_Code
3164 or else (Operating_Mode = Check_Semantics
3165 and then ASIS_Mode));
3167 -- If front_end_inlining is enabled, do not instantiate body if
3168 -- within a generic context.
3170 if (Front_End_Inlining
3171 and then not Expander_Active)
3172 or else Is_Generic_Unit (Cunit_Entity (Main_Unit))
3174 Needs_Body := False;
3177 -- If the current context is generic, and the package being
3178 -- instantiated is declared within a formal package, there is no
3179 -- body to instantiate until the enclosing generic is instantiated
3180 -- and there is an actual for the formal package. If the formal
3181 -- package has parameters, we build a regular package instance for
3182 -- it, that preceeds the original formal package declaration.
3184 if In_Open_Scopes (Scope (Scope (Gen_Unit))) then
3186 Decl : constant Node_Id :=
3188 (Unit_Declaration_Node (Scope (Gen_Unit)));
3190 if Nkind (Decl) = N_Formal_Package_Declaration
3191 or else (Nkind (Decl) = N_Package_Declaration
3192 and then Is_List_Member (Decl)
3193 and then Present (Next (Decl))
3195 Nkind (Next (Decl)) = N_Formal_Package_Declaration)
3197 Needs_Body := False;
3203 -- If we are generating the calling stubs from the instantiation of
3204 -- a generic RCI package, we will not use the body of the generic
3207 if Distribution_Stub_Mode = Generate_Caller_Stub_Body
3208 and then Is_Compilation_Unit (Defining_Entity (N))
3210 Needs_Body := False;
3215 -- Here is a defence against a ludicrous number of instantiations
3216 -- caused by a circular set of instantiation attempts.
3218 if Pending_Instantiations.Last >
3219 Hostparm.Max_Instantiations
3221 Error_Msg_N ("too many instantiations", N);
3222 raise Unrecoverable_Error;
3225 -- Indicate that the enclosing scopes contain an instantiation,
3226 -- and that cleanup actions should be delayed until after the
3227 -- instance body is expanded.
3229 Check_Forward_Instantiation (Gen_Decl);
3230 if Nkind (N) = N_Package_Instantiation then
3232 Enclosing_Master : Entity_Id;
3235 -- Loop to search enclosing masters
3237 Enclosing_Master := Current_Scope;
3238 Scope_Loop : while Enclosing_Master /= Standard_Standard loop
3239 if Ekind (Enclosing_Master) = E_Package then
3240 if Is_Compilation_Unit (Enclosing_Master) then
3241 if In_Package_Body (Enclosing_Master) then
3243 (Body_Entity (Enclosing_Master));
3252 Enclosing_Master := Scope (Enclosing_Master);
3255 elsif Ekind (Enclosing_Master) = E_Generic_Package then
3256 Enclosing_Master := Scope (Enclosing_Master);
3258 elsif Is_Generic_Subprogram (Enclosing_Master)
3259 or else Ekind (Enclosing_Master) = E_Void
3261 -- Cleanup actions will eventually be performed on the
3262 -- enclosing instance, if any. Enclosing scope is void
3263 -- in the formal part of a generic subprogram.
3268 if Ekind (Enclosing_Master) = E_Entry
3270 Ekind (Scope (Enclosing_Master)) = E_Protected_Type
3272 if not Expander_Active then
3276 Protected_Body_Subprogram (Enclosing_Master);
3280 Set_Delay_Cleanups (Enclosing_Master);
3282 while Ekind (Enclosing_Master) = E_Block loop
3283 Enclosing_Master := Scope (Enclosing_Master);
3286 if Is_Subprogram (Enclosing_Master) then
3287 Delay_Descriptors (Enclosing_Master);
3289 elsif Is_Task_Type (Enclosing_Master) then
3291 TBP : constant Node_Id :=
3292 Get_Task_Body_Procedure
3295 if Present (TBP) then
3296 Delay_Descriptors (TBP);
3297 Set_Delay_Cleanups (TBP);
3304 end loop Scope_Loop;
3307 -- Make entry in table
3309 Pending_Instantiations.Append
3311 Act_Decl => Act_Decl,
3312 Expander_Status => Expander_Active,
3313 Current_Sem_Unit => Current_Sem_Unit,
3314 Scope_Suppress => Scope_Suppress,
3315 Local_Suppress_Stack_Top => Local_Suppress_Stack_Top));
3319 Set_Categorization_From_Pragmas (Act_Decl);
3321 if Parent_Installed then
3325 Set_Instance_Spec (N, Act_Decl);
3327 -- If not a compilation unit, insert the package declaration before
3328 -- the original instantiation node.
3330 if Nkind (Parent (N)) /= N_Compilation_Unit then
3331 Mark_Rewrite_Insertion (Act_Decl);
3332 Insert_Before (N, Act_Decl);
3335 -- For an instantiation that is a compilation unit, place declaration
3336 -- on current node so context is complete for analysis (including
3337 -- nested instantiations). If this is the main unit, the declaration
3338 -- eventually replaces the instantiation node. If the instance body
3339 -- is later created, it replaces the instance node, and the declation
3340 -- is attached to it (see Build_Instance_Compilation_Unit_Nodes).
3343 if Cunit_Entity (Current_Sem_Unit) = Defining_Entity (N) then
3345 -- The entity for the current unit is the newly created one,
3346 -- and all semantic information is attached to it.
3348 Set_Cunit_Entity (Current_Sem_Unit, Act_Decl_Id);
3350 -- If this is the main unit, replace the main entity as well
3352 if Current_Sem_Unit = Main_Unit then
3353 Main_Unit_Entity := Act_Decl_Id;
3357 Set_Unit (Parent (N), Act_Decl);
3358 Set_Parent_Spec (Act_Decl, Parent_Spec (N));
3359 Set_Package_Instantiation (Act_Decl_Id, N);
3361 Set_Unit (Parent (N), N);
3362 Set_Body_Required (Parent (N), False);
3364 -- We never need elaboration checks on instantiations, since by
3365 -- definition, the body instantiation is elaborated at the same
3366 -- time as the spec instantiation.
3368 Set_Suppress_Elaboration_Warnings (Act_Decl_Id);
3369 Set_Kill_Elaboration_Checks (Act_Decl_Id);
3372 Check_Elab_Instantiation (N);
3374 if ABE_Is_Certain (N) and then Needs_Body then
3375 Pending_Instantiations.Decrement_Last;
3378 Check_Hidden_Child_Unit (N, Gen_Unit, Act_Decl_Id);
3380 Set_First_Private_Entity (Defining_Unit_Name (Unit_Renaming),
3381 First_Private_Entity (Act_Decl_Id));
3383 -- If the instantiation will receive a body, the unit will be
3384 -- transformed into a package body, and receive its own elaboration
3385 -- entity. Otherwise, the nature of the unit is now a package
3388 if Nkind (Parent (N)) = N_Compilation_Unit
3389 and then not Needs_Body
3391 Rewrite (N, Act_Decl);
3394 if Present (Corresponding_Body (Gen_Decl))
3395 or else Unit_Requires_Body (Gen_Unit)
3397 Set_Has_Completion (Act_Decl_Id);
3400 Check_Formal_Packages (Act_Decl_Id);
3402 Restore_Private_Views (Act_Decl_Id);
3404 Inherit_Context (Gen_Decl, N);
3406 if Parent_Installed then
3411 Env_Installed := False;
3414 Validate_Categorization_Dependency (N, Act_Decl_Id);
3416 -- Check restriction, but skip this if something went wrong in the above
3417 -- analysis, indicated by Act_Decl_Id being void.
3419 if Ekind (Act_Decl_Id) /= E_Void
3420 and then not Is_Library_Level_Entity (Act_Decl_Id)
3422 Check_Restriction (No_Local_Allocators, N);
3426 Inline_Instance_Body (N, Gen_Unit, Act_Decl);
3429 -- The following is a tree patch for ASIS: ASIS needs separate nodes to
3430 -- be used as defining identifiers for a formal package and for the
3431 -- corresponding expanded package.
3433 if Nkind (N) = N_Formal_Package_Declaration then
3434 Act_Decl_Id := New_Copy (Defining_Entity (N));
3435 Set_Comes_From_Source (Act_Decl_Id, True);
3436 Set_Is_Generic_Instance (Act_Decl_Id, False);
3437 Set_Defining_Identifier (N, Act_Decl_Id);
3441 when Instantiation_Error =>
3442 if Parent_Installed then
3446 if Env_Installed then
3449 end Analyze_Package_Instantiation;
3451 --------------------------
3452 -- Inline_Instance_Body --
3453 --------------------------
3455 procedure Inline_Instance_Body
3457 Gen_Unit : Entity_Id;
3461 Gen_Comp : constant Entity_Id :=
3462 Cunit_Entity (Get_Source_Unit (Gen_Unit));
3463 Curr_Comp : constant Node_Id := Cunit (Current_Sem_Unit);
3464 Curr_Scope : Entity_Id := Empty;
3465 Curr_Unit : constant Entity_Id :=
3466 Cunit_Entity (Current_Sem_Unit);
3467 Removed : Boolean := False;
3468 Num_Scopes : Int := 0;
3470 Scope_Stack_Depth : constant Int :=
3471 Scope_Stack.Last - Scope_Stack.First + 1;
3473 Use_Clauses : array (1 .. Scope_Stack_Depth) of Node_Id;
3474 Instances : array (1 .. Scope_Stack_Depth) of Entity_Id;
3475 Inner_Scopes : array (1 .. Scope_Stack_Depth) of Entity_Id;
3476 Num_Inner : Int := 0;
3477 N_Instances : Int := 0;
3481 -- Case of generic unit defined in another unit. We must remove the
3482 -- complete context of the current unit to install that of the generic.
3484 if Gen_Comp /= Cunit_Entity (Current_Sem_Unit) then
3486 -- Add some comments for the following two loops ???
3489 while Present (S) and then S /= Standard_Standard loop
3491 Num_Scopes := Num_Scopes + 1;
3493 Use_Clauses (Num_Scopes) :=
3495 (Scope_Stack.Last - Num_Scopes + 1).
3497 End_Use_Clauses (Use_Clauses (Num_Scopes));
3499 exit when Scope_Stack.Last - Num_Scopes + 1 = Scope_Stack.First
3500 or else Scope_Stack.Table
3501 (Scope_Stack.Last - Num_Scopes).Entity
3505 exit when Is_Generic_Instance (S)
3506 and then (In_Package_Body (S)
3507 or else Ekind (S) = E_Procedure
3508 or else Ekind (S) = E_Function);
3512 Vis := Is_Immediately_Visible (Gen_Comp);
3514 -- Find and save all enclosing instances
3519 and then S /= Standard_Standard
3521 if Is_Generic_Instance (S) then
3522 N_Instances := N_Instances + 1;
3523 Instances (N_Instances) := S;
3525 exit when In_Package_Body (S);
3531 -- Remove context of current compilation unit, unless we are within a
3532 -- nested package instantiation, in which case the context has been
3533 -- removed previously.
3535 -- If current scope is the body of a child unit, remove context of
3536 -- spec as well. If an enclosing scope is an instance body. the
3537 -- context has already been removed, but the entities in the body
3538 -- must be made invisible as well.
3543 and then S /= Standard_Standard
3545 if Is_Generic_Instance (S)
3546 and then (In_Package_Body (S)
3547 or else Ekind (S) = E_Procedure
3548 or else Ekind (S) = E_Function)
3550 -- We still have to remove the entities of the enclosing
3551 -- instance from direct visibility.
3556 E := First_Entity (S);
3557 while Present (E) loop
3558 Set_Is_Immediately_Visible (E, False);
3567 or else (Ekind (Curr_Unit) = E_Package_Body
3568 and then S = Spec_Entity (Curr_Unit))
3569 or else (Ekind (Curr_Unit) = E_Subprogram_Body
3572 (Unit_Declaration_Node (Curr_Unit)))
3576 -- Remove entities in current scopes from visibility, so that
3577 -- instance body is compiled in a clean environment.
3579 Save_Scope_Stack (Handle_Use => False);
3581 if Is_Child_Unit (S) then
3583 -- Remove child unit from stack, as well as inner scopes.
3584 -- Removing the context of a child unit removes parent units
3587 while Current_Scope /= S loop
3588 Num_Inner := Num_Inner + 1;
3589 Inner_Scopes (Num_Inner) := Current_Scope;
3594 Remove_Context (Curr_Comp);
3598 Remove_Context (Curr_Comp);
3601 if Ekind (Curr_Unit) = E_Package_Body then
3602 Remove_Context (Library_Unit (Curr_Comp));
3608 pragma Assert (Num_Inner < Num_Scopes);
3610 Push_Scope (Standard_Standard);
3611 Scope_Stack.Table (Scope_Stack.Last).Is_Active_Stack_Base := True;
3612 Instantiate_Package_Body
3615 Act_Decl => Act_Decl,
3616 Expander_Status => Expander_Active,
3617 Current_Sem_Unit => Current_Sem_Unit,
3618 Scope_Suppress => Scope_Suppress,
3619 Local_Suppress_Stack_Top => Local_Suppress_Stack_Top)),
3620 Inlined_Body => True);
3626 Set_Is_Immediately_Visible (Gen_Comp, Vis);
3628 -- Reset Generic_Instance flag so that use clauses can be installed
3629 -- in the proper order. (See Use_One_Package for effect of enclosing
3630 -- instances on processing of use clauses).
3632 for J in 1 .. N_Instances loop
3633 Set_Is_Generic_Instance (Instances (J), False);
3637 Install_Context (Curr_Comp);
3639 if Present (Curr_Scope)
3640 and then Is_Child_Unit (Curr_Scope)
3642 Push_Scope (Curr_Scope);
3643 Set_Is_Immediately_Visible (Curr_Scope);
3645 -- Finally, restore inner scopes as well
3647 for J in reverse 1 .. Num_Inner loop
3648 Push_Scope (Inner_Scopes (J));
3652 Restore_Scope_Stack (Handle_Use => False);
3654 if Present (Curr_Scope)
3656 (In_Private_Part (Curr_Scope)
3657 or else In_Package_Body (Curr_Scope))
3659 -- Install private declaration of ancestor units, which are
3660 -- currently available. Restore_Scope_Stack and Install_Context
3661 -- only install the visible part of parents.
3666 Par := Scope (Curr_Scope);
3667 while (Present (Par))
3668 and then Par /= Standard_Standard
3670 Install_Private_Declarations (Par);
3677 -- Restore use clauses. For a child unit, use clauses in the parents
3678 -- are restored when installing the context, so only those in inner
3679 -- scopes (and those local to the child unit itself) need to be
3680 -- installed explicitly.
3682 if Is_Child_Unit (Curr_Unit)
3685 for J in reverse 1 .. Num_Inner + 1 loop
3686 Scope_Stack.Table (Scope_Stack.Last - J + 1).First_Use_Clause :=
3688 Install_Use_Clauses (Use_Clauses (J));
3692 for J in reverse 1 .. Num_Scopes loop
3693 Scope_Stack.Table (Scope_Stack.Last - J + 1).First_Use_Clause :=
3695 Install_Use_Clauses (Use_Clauses (J));
3699 -- Restore status of instances. If one of them is a body, make
3700 -- its local entities visible again.
3707 for J in 1 .. N_Instances loop
3708 Inst := Instances (J);
3709 Set_Is_Generic_Instance (Inst, True);
3711 if In_Package_Body (Inst)
3712 or else Ekind (S) = E_Procedure
3713 or else Ekind (S) = E_Function
3715 E := First_Entity (Instances (J));
3716 while Present (E) loop
3717 Set_Is_Immediately_Visible (E);
3724 -- If generic unit is in current unit, current context is correct
3727 Instantiate_Package_Body
3730 Act_Decl => Act_Decl,
3731 Expander_Status => Expander_Active,
3732 Current_Sem_Unit => Current_Sem_Unit,
3733 Scope_Suppress => Scope_Suppress,
3734 Local_Suppress_Stack_Top => Local_Suppress_Stack_Top)),
3735 Inlined_Body => True);
3737 end Inline_Instance_Body;
3739 -------------------------------------
3740 -- Analyze_Procedure_Instantiation --
3741 -------------------------------------
3743 procedure Analyze_Procedure_Instantiation (N : Node_Id) is
3745 Analyze_Subprogram_Instantiation (N, E_Procedure);
3746 end Analyze_Procedure_Instantiation;
3748 --------------------------------------
3749 -- Analyze_Subprogram_Instantiation --
3750 --------------------------------------
3752 procedure Analyze_Subprogram_Instantiation
3756 Loc : constant Source_Ptr := Sloc (N);
3757 Gen_Id : constant Node_Id := Name (N);
3759 Anon_Id : constant Entity_Id :=
3760 Make_Defining_Identifier (Sloc (Defining_Entity (N)),
3761 Chars => New_External_Name
3762 (Chars (Defining_Entity (N)), 'R'));
3764 Act_Decl_Id : Entity_Id;
3769 Env_Installed : Boolean := False;
3770 Gen_Unit : Entity_Id;
3772 Pack_Id : Entity_Id;
3773 Parent_Installed : Boolean := False;
3774 Renaming_List : List_Id;
3776 procedure Analyze_Instance_And_Renamings;
3777 -- The instance must be analyzed in a context that includes the mappings
3778 -- of generic parameters into actuals. We create a package declaration
3779 -- for this purpose, and a subprogram with an internal name within the
3780 -- package. The subprogram instance is simply an alias for the internal
3781 -- subprogram, declared in the current scope.
3783 ------------------------------------
3784 -- Analyze_Instance_And_Renamings --
3785 ------------------------------------
3787 procedure Analyze_Instance_And_Renamings is
3788 Def_Ent : constant Entity_Id := Defining_Entity (N);
3789 Pack_Decl : Node_Id;
3792 if Nkind (Parent (N)) = N_Compilation_Unit then
3794 -- For the case of a compilation unit, the container package has
3795 -- the same name as the instantiation, to insure that the binder
3796 -- calls the elaboration procedure with the right name. Copy the
3797 -- entity of the instance, which may have compilation level flags
3798 -- (e.g. Is_Child_Unit) set.
3800 Pack_Id := New_Copy (Def_Ent);
3803 -- Otherwise we use the name of the instantiation concatenated
3804 -- with its source position to ensure uniqueness if there are
3805 -- several instantiations with the same name.
3808 Make_Defining_Identifier (Loc,
3809 Chars => New_External_Name
3810 (Related_Id => Chars (Def_Ent),
3812 Suffix_Index => Source_Offset (Sloc (Def_Ent))));
3815 Pack_Decl := Make_Package_Declaration (Loc,
3816 Specification => Make_Package_Specification (Loc,
3817 Defining_Unit_Name => Pack_Id,
3818 Visible_Declarations => Renaming_List,
3819 End_Label => Empty));
3821 Set_Instance_Spec (N, Pack_Decl);
3822 Set_Is_Generic_Instance (Pack_Id);
3823 Set_Needs_Debug_Info (Pack_Id);
3825 -- Case of not a compilation unit
3827 if Nkind (Parent (N)) /= N_Compilation_Unit then
3828 Mark_Rewrite_Insertion (Pack_Decl);
3829 Insert_Before (N, Pack_Decl);
3830 Set_Has_Completion (Pack_Id);
3832 -- Case of an instantiation that is a compilation unit
3834 -- Place declaration on current node so context is complete for
3835 -- analysis (including nested instantiations), and for use in a
3836 -- context_clause (see Analyze_With_Clause).
3839 Set_Unit (Parent (N), Pack_Decl);
3840 Set_Parent_Spec (Pack_Decl, Parent_Spec (N));
3843 Analyze (Pack_Decl);
3844 Check_Formal_Packages (Pack_Id);
3845 Set_Is_Generic_Instance (Pack_Id, False);
3847 -- Body of the enclosing package is supplied when instantiating the
3848 -- subprogram body, after semantic analysis is completed.
3850 if Nkind (Parent (N)) = N_Compilation_Unit then
3852 -- Remove package itself from visibility, so it does not
3853 -- conflict with subprogram.
3855 Set_Name_Entity_Id (Chars (Pack_Id), Homonym (Pack_Id));
3857 -- Set name and scope of internal subprogram so that the proper
3858 -- external name will be generated. The proper scope is the scope
3859 -- of the wrapper package. We need to generate debugging info for
3860 -- the internal subprogram, so set flag accordingly.
3862 Set_Chars (Anon_Id, Chars (Defining_Entity (N)));
3863 Set_Scope (Anon_Id, Scope (Pack_Id));
3865 -- Mark wrapper package as referenced, to avoid spurious warnings
3866 -- if the instantiation appears in various with_ clauses of
3867 -- subunits of the main unit.
3869 Set_Referenced (Pack_Id);
3872 Set_Is_Generic_Instance (Anon_Id);
3873 Set_Needs_Debug_Info (Anon_Id);
3874 Act_Decl_Id := New_Copy (Anon_Id);
3876 Set_Parent (Act_Decl_Id, Parent (Anon_Id));
3877 Set_Chars (Act_Decl_Id, Chars (Defining_Entity (N)));
3878 Set_Sloc (Act_Decl_Id, Sloc (Defining_Entity (N)));
3879 Set_Comes_From_Source (Act_Decl_Id, True);
3881 -- The signature may involve types that are not frozen yet, but the
3882 -- subprogram will be frozen at the point the wrapper package is
3883 -- frozen, so it does not need its own freeze node. In fact, if one
3884 -- is created, it might conflict with the freezing actions from the
3885 -- wrapper package (see 7206-013).
3887 -- Should not really reference non-public TN's in comments ???
3889 Set_Has_Delayed_Freeze (Anon_Id, False);
3891 -- If the instance is a child unit, mark the Id accordingly. Mark
3892 -- the anonymous entity as well, which is the real subprogram and
3893 -- which is used when the instance appears in a context clause.
3895 Set_Is_Child_Unit (Act_Decl_Id, Is_Child_Unit (Defining_Entity (N)));
3896 Set_Is_Child_Unit (Anon_Id, Is_Child_Unit (Defining_Entity (N)));
3897 New_Overloaded_Entity (Act_Decl_Id);
3898 Check_Eliminated (Act_Decl_Id);
3900 -- In compilation unit case, kill elaboration checks on the
3901 -- instantiation, since they are never needed -- the body is
3902 -- instantiated at the same point as the spec.
3904 if Nkind (Parent (N)) = N_Compilation_Unit then
3905 Set_Suppress_Elaboration_Warnings (Act_Decl_Id);
3906 Set_Kill_Elaboration_Checks (Act_Decl_Id);
3907 Set_Is_Compilation_Unit (Anon_Id);
3909 Set_Cunit_Entity (Current_Sem_Unit, Pack_Id);
3912 -- The instance is not a freezing point for the new subprogram
3914 Set_Is_Frozen (Act_Decl_Id, False);
3916 if Nkind (Defining_Entity (N)) = N_Defining_Operator_Symbol then
3917 Valid_Operator_Definition (Act_Decl_Id);
3920 Set_Alias (Act_Decl_Id, Anon_Id);
3921 Set_Parent (Act_Decl_Id, Parent (Anon_Id));
3922 Set_Has_Completion (Act_Decl_Id);
3923 Set_Related_Instance (Pack_Id, Act_Decl_Id);
3925 if Nkind (Parent (N)) = N_Compilation_Unit then
3926 Set_Body_Required (Parent (N), False);
3929 end Analyze_Instance_And_Renamings;
3931 -- Start of processing for Analyze_Subprogram_Instantiation
3934 -- Very first thing: apply the special kludge for Text_IO processing
3935 -- in case we are instantiating one of the children of [Wide_]Text_IO.
3936 -- Of course such an instantiation is bogus (these are packages, not
3937 -- subprograms), but we get a better error message if we do this.
3939 Text_IO_Kludge (Gen_Id);
3941 -- Make node global for error reporting
3943 Instantiation_Node := N;
3944 Pre_Analyze_Actuals (N);
3947 Env_Installed := True;
3948 Check_Generic_Child_Unit (Gen_Id, Parent_Installed);
3949 Gen_Unit := Entity (Gen_Id);
3951 Generate_Reference (Gen_Unit, Gen_Id);
3953 if Nkind (Gen_Id) = N_Identifier
3954 and then Chars (Gen_Unit) = Chars (Defining_Entity (N))
3957 ("& is hidden within declaration of instance", Gen_Id, Gen_Unit);
3960 if Etype (Gen_Unit) = Any_Type then
3965 -- Verify that it is a generic subprogram of the right kind, and that
3966 -- it does not lead to a circular instantiation.
3968 if Ekind (Gen_Unit) /= E_Generic_Procedure
3969 and then Ekind (Gen_Unit) /= E_Generic_Function
3971 Error_Msg_N ("expect generic subprogram in instantiation", Gen_Id);
3973 elsif In_Open_Scopes (Gen_Unit) then
3974 Error_Msg_NE ("instantiation of & within itself", N, Gen_Unit);
3976 elsif K = E_Procedure
3977 and then Ekind (Gen_Unit) /= E_Generic_Procedure
3979 if Ekind (Gen_Unit) = E_Generic_Function then
3981 ("cannot instantiate generic function as procedure", Gen_Id);
3984 ("expect name of generic procedure in instantiation", Gen_Id);
3987 elsif K = E_Function
3988 and then Ekind (Gen_Unit) /= E_Generic_Function
3990 if Ekind (Gen_Unit) = E_Generic_Procedure then
3992 ("cannot instantiate generic procedure as function", Gen_Id);
3995 ("expect name of generic function in instantiation", Gen_Id);
3999 Set_Entity (Gen_Id, Gen_Unit);
4000 Set_Is_Instantiated (Gen_Unit);
4002 if In_Extended_Main_Source_Unit (N) then
4003 Generate_Reference (Gen_Unit, N);
4006 -- If renaming, get original unit
4008 if Present (Renamed_Object (Gen_Unit))
4009 and then (Ekind (Renamed_Object (Gen_Unit)) = E_Generic_Procedure
4011 Ekind (Renamed_Object (Gen_Unit)) = E_Generic_Function)
4013 Gen_Unit := Renamed_Object (Gen_Unit);
4014 Set_Is_Instantiated (Gen_Unit);
4015 Generate_Reference (Gen_Unit, N);
4018 if Contains_Instance_Of (Gen_Unit, Current_Scope, Gen_Id) then
4019 Error_Msg_Node_2 := Current_Scope;
4021 ("circular Instantiation: & instantiated in &!", N, Gen_Unit);
4022 Circularity_Detected := True;
4026 Gen_Decl := Unit_Declaration_Node (Gen_Unit);
4028 -- Initialize renamings map, for error checking
4030 Generic_Renamings.Set_Last (0);
4031 Generic_Renamings_HTable.Reset;
4033 Create_Instantiation_Source (N, Gen_Unit, False, S_Adjustment);
4035 -- Copy original generic tree, to produce text for instantiation
4039 (Original_Node (Gen_Decl), Empty, Instantiating => True);
4041 Act_Spec := Specification (Act_Tree);
4043 Analyze_Associations
4045 Generic_Formal_Declarations (Act_Tree),
4046 Generic_Formal_Declarations (Gen_Decl));
4048 -- The subprogram itself cannot contain a nested instance, so the
4049 -- current parent is left empty.
4051 Set_Instance_Env (Gen_Unit, Empty);
4053 -- Build the subprogram declaration, which does not appear in the
4054 -- generic template, and give it a sloc consistent with that of the
4057 Set_Defining_Unit_Name (Act_Spec, Anon_Id);
4058 Set_Generic_Parent (Act_Spec, Gen_Unit);
4060 Make_Subprogram_Declaration (Sloc (Act_Spec),
4061 Specification => Act_Spec);
4063 Set_Categorization_From_Pragmas (Act_Decl);
4065 if Parent_Installed then
4069 Append (Act_Decl, Renaming_List);
4070 Analyze_Instance_And_Renamings;
4072 -- If the generic is marked Import (Intrinsic), then so is the
4073 -- instance. This indicates that there is no body to instantiate. If
4074 -- generic is marked inline, so it the instance, and the anonymous
4075 -- subprogram it renames. If inlined, or else if inlining is enabled
4076 -- for the compilation, we generate the instance body even if it is
4077 -- not within the main unit.
4079 -- Any other pragmas might also be inherited ???
4081 if Is_Intrinsic_Subprogram (Gen_Unit) then
4082 Set_Is_Intrinsic_Subprogram (Anon_Id);
4083 Set_Is_Intrinsic_Subprogram (Act_Decl_Id);
4085 if Chars (Gen_Unit) = Name_Unchecked_Conversion then
4086 Validate_Unchecked_Conversion (N, Act_Decl_Id);
4090 Generate_Definition (Act_Decl_Id);
4092 Set_Is_Inlined (Act_Decl_Id, Is_Inlined (Gen_Unit));
4093 Set_Is_Inlined (Anon_Id, Is_Inlined (Gen_Unit));
4095 if not Is_Intrinsic_Subprogram (Gen_Unit) then
4096 Check_Elab_Instantiation (N);
4099 if Is_Dispatching_Operation (Act_Decl_Id)
4100 and then Ada_Version >= Ada_05
4106 Formal := First_Formal (Act_Decl_Id);
4107 while Present (Formal) loop
4108 if Ekind (Etype (Formal)) = E_Anonymous_Access_Type
4109 and then Is_Controlling_Formal (Formal)
4110 and then not Can_Never_Be_Null (Formal)
4112 Error_Msg_NE ("access parameter& is controlling,",
4114 Error_Msg_NE ("\corresponding parameter of & must be"
4115 & " explicitly null-excluding", N, Gen_Id);
4118 Next_Formal (Formal);
4123 Check_Hidden_Child_Unit (N, Gen_Unit, Act_Decl_Id);
4125 -- Subject to change, pending on if other pragmas are inherited ???
4127 Validate_Categorization_Dependency (N, Act_Decl_Id);
4129 if not Is_Intrinsic_Subprogram (Act_Decl_Id) then
4130 Inherit_Context (Gen_Decl, N);
4132 Restore_Private_Views (Pack_Id, False);
4134 -- If the context requires a full instantiation, mark node for
4135 -- subsequent construction of the body.
4137 if (Is_In_Main_Unit (N)
4138 or else Is_Inlined (Act_Decl_Id))
4139 and then (Operating_Mode = Generate_Code
4140 or else (Operating_Mode = Check_Semantics
4141 and then ASIS_Mode))
4142 and then (Expander_Active or else ASIS_Mode)
4143 and then not ABE_Is_Certain (N)
4144 and then not Is_Eliminated (Act_Decl_Id)
4146 Pending_Instantiations.Append
4148 Act_Decl => Act_Decl,
4149 Expander_Status => Expander_Active,
4150 Current_Sem_Unit => Current_Sem_Unit,
4151 Scope_Suppress => Scope_Suppress,
4152 Local_Suppress_Stack_Top => Local_Suppress_Stack_Top));
4154 Check_Forward_Instantiation (Gen_Decl);
4156 -- The wrapper package is always delayed, because it does not
4157 -- constitute a freeze point, but to insure that the freeze
4158 -- node is placed properly, it is created directly when
4159 -- instantiating the body (otherwise the freeze node might
4160 -- appear to early for nested instantiations).
4162 elsif Nkind (Parent (N)) = N_Compilation_Unit then
4164 -- For ASIS purposes, indicate that the wrapper package has
4165 -- replaced the instantiation node.
4167 Rewrite (N, Unit (Parent (N)));
4168 Set_Unit (Parent (N), N);
4171 elsif Nkind (Parent (N)) = N_Compilation_Unit then
4173 -- Replace instance node for library-level instantiations of
4174 -- intrinsic subprograms, for ASIS use.
4176 Rewrite (N, Unit (Parent (N)));
4177 Set_Unit (Parent (N), N);
4180 if Parent_Installed then
4185 Env_Installed := False;
4186 Generic_Renamings.Set_Last (0);
4187 Generic_Renamings_HTable.Reset;
4191 when Instantiation_Error =>
4192 if Parent_Installed then
4196 if Env_Installed then
4199 end Analyze_Subprogram_Instantiation;
4201 -------------------------
4202 -- Get_Associated_Node --
4203 -------------------------
4205 function Get_Associated_Node (N : Node_Id) return Node_Id is
4206 Assoc : Node_Id := Associated_Node (N);
4209 if Nkind (Assoc) /= Nkind (N) then
4212 elsif Nkind (Assoc) = N_Aggregate
4213 or else Nkind (Assoc) = N_Extension_Aggregate
4218 -- If the node is part of an inner generic, it may itself have been
4219 -- remapped into a further generic copy. Associated_Node is otherwise
4220 -- used for the entity of the node, and will be of a different node
4221 -- kind, or else N has been rewritten as a literal or function call.
4223 while Present (Associated_Node (Assoc))
4224 and then Nkind (Associated_Node (Assoc)) = Nkind (Assoc)
4226 Assoc := Associated_Node (Assoc);
4229 -- Follow and additional link in case the final node was rewritten.
4230 -- This can only happen with nested generic units.
4232 if (Nkind (Assoc) = N_Identifier or else Nkind (Assoc) in N_Op)
4233 and then Present (Associated_Node (Assoc))
4234 and then (Nkind (Associated_Node (Assoc)) = N_Function_Call
4236 Nkind (Associated_Node (Assoc)) = N_Explicit_Dereference
4238 Nkind (Associated_Node (Assoc)) = N_Integer_Literal
4240 Nkind (Associated_Node (Assoc)) = N_Real_Literal
4242 Nkind (Associated_Node (Assoc)) = N_String_Literal)
4244 Assoc := Associated_Node (Assoc);
4249 end Get_Associated_Node;
4251 -------------------------------------------
4252 -- Build_Instance_Compilation_Unit_Nodes --
4253 -------------------------------------------
4255 procedure Build_Instance_Compilation_Unit_Nodes
4260 Decl_Cunit : Node_Id;
4261 Body_Cunit : Node_Id;
4263 New_Main : constant Entity_Id := Defining_Entity (Act_Decl);
4264 Old_Main : constant Entity_Id := Cunit_Entity (Main_Unit);
4267 -- A new compilation unit node is built for the instance declaration
4270 Make_Compilation_Unit (Sloc (N),
4271 Context_Items => Empty_List,
4274 Make_Compilation_Unit_Aux (Sloc (N)));
4276 Set_Parent_Spec (Act_Decl, Parent_Spec (N));
4277 Set_Body_Required (Decl_Cunit, True);
4279 -- We use the original instantiation compilation unit as the resulting
4280 -- compilation unit of the instance, since this is the main unit.
4282 Rewrite (N, Act_Body);
4283 Body_Cunit := Parent (N);
4285 -- The two compilation unit nodes are linked by the Library_Unit field
4287 Set_Library_Unit (Decl_Cunit, Body_Cunit);
4288 Set_Library_Unit (Body_Cunit, Decl_Cunit);
4290 -- Preserve the private nature of the package if needed
4292 Set_Private_Present (Decl_Cunit, Private_Present (Body_Cunit));
4294 -- If the instance is not the main unit, its context, categorization,
4295 -- and elaboration entity are not relevant to the compilation.
4297 if Parent (N) /= Cunit (Main_Unit) then
4301 -- The context clause items on the instantiation, which are now attached
4302 -- to the body compilation unit (since the body overwrote the original
4303 -- instantiation node), semantically belong on the spec, so copy them
4304 -- there. It's harmless to leave them on the body as well. In fact one
4305 -- could argue that they belong in both places.
4307 Citem := First (Context_Items (Body_Cunit));
4308 while Present (Citem) loop
4309 Append (New_Copy (Citem), Context_Items (Decl_Cunit));
4313 -- Propagate categorization flags on packages, so that they appear in
4314 -- the ali file for the spec of the unit.
4316 if Ekind (New_Main) = E_Package then
4317 Set_Is_Pure (Old_Main, Is_Pure (New_Main));
4318 Set_Is_Preelaborated (Old_Main, Is_Preelaborated (New_Main));
4319 Set_Is_Remote_Types (Old_Main, Is_Remote_Types (New_Main));
4320 Set_Is_Shared_Passive (Old_Main, Is_Shared_Passive (New_Main));
4321 Set_Is_Remote_Call_Interface
4322 (Old_Main, Is_Remote_Call_Interface (New_Main));
4325 -- Make entry in Units table, so that binder can generate call to
4326 -- elaboration procedure for body, if any.
4328 Make_Instance_Unit (Body_Cunit);
4329 Main_Unit_Entity := New_Main;
4330 Set_Cunit_Entity (Main_Unit, Main_Unit_Entity);
4332 -- Build elaboration entity, since the instance may certainly generate
4333 -- elaboration code requiring a flag for protection.
4335 Build_Elaboration_Entity (Decl_Cunit, New_Main);
4336 end Build_Instance_Compilation_Unit_Nodes;
4338 -----------------------------
4339 -- Check_Access_Definition --
4340 -----------------------------
4342 procedure Check_Access_Definition (N : Node_Id) is
4345 (Ada_Version >= Ada_05
4346 and then Present (Access_Definition (N)));
4348 end Check_Access_Definition;
4350 -----------------------------------
4351 -- Check_Formal_Package_Instance --
4352 -----------------------------------
4354 -- If the formal has specific parameters, they must match those of the
4355 -- actual. Both of them are instances, and the renaming declarations for
4356 -- their formal parameters appear in the same order in both. The analyzed
4357 -- formal has been analyzed in the context of the current instance.
4359 procedure Check_Formal_Package_Instance
4360 (Formal_Pack : Entity_Id;
4361 Actual_Pack : Entity_Id)
4363 E1 : Entity_Id := First_Entity (Actual_Pack);
4364 E2 : Entity_Id := First_Entity (Formal_Pack);
4369 procedure Check_Mismatch (B : Boolean);
4370 -- Common error routine for mismatch between the parameters of the
4371 -- actual instance and those of the formal package.
4373 function Same_Instantiated_Constant (E1, E2 : Entity_Id) return Boolean;
4374 -- The formal may come from a nested formal package, and the actual may
4375 -- have been constant-folded. To determine whether the two denote the
4376 -- same entity we may have to traverse several definitions to recover
4377 -- the ultimate entity that they refer to.
4379 function Same_Instantiated_Variable (E1, E2 : Entity_Id) return Boolean;
4380 -- Similarly, if the formal comes from a nested formal package, the
4381 -- actual may designate the formal through multiple renamings, which
4382 -- have to be followed to determine the original variable in question.
4384 --------------------
4385 -- Check_Mismatch --
4386 --------------------
4388 procedure Check_Mismatch (B : Boolean) is
4389 Kind : constant Node_Kind := Nkind (Parent (E2));
4392 if Kind = N_Formal_Type_Declaration then
4395 elsif Kind = N_Formal_Object_Declaration
4396 or else Kind in N_Formal_Subprogram_Declaration
4397 or else Kind = N_Formal_Package_Declaration
4403 ("actual for & in actual instance does not match formal",
4404 Parent (Actual_Pack), E1);
4408 --------------------------------
4409 -- Same_Instantiated_Constant --
4410 --------------------------------
4412 function Same_Instantiated_Constant
4413 (E1, E2 : Entity_Id) return Boolean
4419 while Present (Ent) loop
4423 elsif Ekind (Ent) /= E_Constant then
4426 elsif Is_Entity_Name (Constant_Value (Ent)) then
4427 if Entity (Constant_Value (Ent)) = E1 then
4430 Ent := Entity (Constant_Value (Ent));
4433 -- The actual may be a constant that has been folded. Recover
4436 elsif Is_Entity_Name (Original_Node (Constant_Value (Ent))) then
4437 Ent := Entity (Original_Node (Constant_Value (Ent)));
4444 end Same_Instantiated_Constant;
4446 --------------------------------
4447 -- Same_Instantiated_Variable --
4448 --------------------------------
4450 function Same_Instantiated_Variable
4451 (E1, E2 : Entity_Id) return Boolean
4453 function Original_Entity (E : Entity_Id) return Entity_Id;
4454 -- Follow chain of renamings to the ultimate ancestor
4456 ---------------------
4457 -- Original_Entity --
4458 ---------------------
4460 function Original_Entity (E : Entity_Id) return Entity_Id is
4465 while Nkind (Parent (Orig)) = N_Object_Renaming_Declaration
4466 and then Present (Renamed_Object (Orig))
4467 and then Is_Entity_Name (Renamed_Object (Orig))
4469 Orig := Entity (Renamed_Object (Orig));
4473 end Original_Entity;
4475 -- Start of processing for Same_Instantiated_Variable
4478 return Ekind (E1) = Ekind (E2)
4479 and then Original_Entity (E1) = Original_Entity (E2);
4480 end Same_Instantiated_Variable;
4482 -- Start of processing for Check_Formal_Package_Instance
4486 and then Present (E2)
4488 exit when Ekind (E1) = E_Package
4489 and then Renamed_Entity (E1) = Renamed_Entity (Actual_Pack);
4491 -- If the formal is the renaming of the formal package, this
4492 -- is the end of its formal part, which may occur before the
4493 -- end of the formal part in the actual in the presence of
4494 -- defaulted parameters in the formal package.
4496 exit when Nkind (Parent (E2)) = N_Package_Renaming_Declaration
4497 and then Renamed_Entity (E2) = Scope (E2);
4499 -- The analysis of the actual may generate additional internal
4500 -- entities. If the formal is defaulted, there is no corresponding
4501 -- analysis and the internal entities must be skipped, until we
4502 -- find corresponding entities again.
4504 if Comes_From_Source (E2)
4505 and then not Comes_From_Source (E1)
4506 and then Chars (E1) /= Chars (E2)
4509 and then Chars (E1) /= Chars (E2)
4518 -- If the formal entity comes from a formal declaration. it was
4519 -- defaulted in the formal package, and no check is needed on it.
4521 elsif Nkind (Parent (E2)) = N_Formal_Object_Declaration then
4524 elsif Is_Type (E1) then
4526 -- Subtypes must statically match. E1, E2 are the local entities
4527 -- that are subtypes of the actuals. Itypes generated for other
4528 -- parameters need not be checked, the check will be performed
4529 -- on the parameters themselves.
4531 -- If E2 is a formal type declaration, it is a defaulted parameter
4532 -- and needs no checking.
4534 if not Is_Itype (E1)
4535 and then not Is_Itype (E2)
4539 or else Etype (E1) /= Etype (E2)
4540 or else not Subtypes_Statically_Match (E1, E2));
4543 elsif Ekind (E1) = E_Constant then
4545 -- IN parameters must denote the same static value, or the same
4546 -- constant, or the literal null.
4548 Expr1 := Expression (Parent (E1));
4550 if Ekind (E2) /= E_Constant then
4551 Check_Mismatch (True);
4554 Expr2 := Expression (Parent (E2));
4557 if Is_Static_Expression (Expr1) then
4559 if not Is_Static_Expression (Expr2) then
4560 Check_Mismatch (True);
4562 elsif Is_Discrete_Type (Etype (E1)) then
4564 V1 : constant Uint := Expr_Value (Expr1);
4565 V2 : constant Uint := Expr_Value (Expr2);
4567 Check_Mismatch (V1 /= V2);
4570 elsif Is_Real_Type (Etype (E1)) then
4572 V1 : constant Ureal := Expr_Value_R (Expr1);
4573 V2 : constant Ureal := Expr_Value_R (Expr2);
4575 Check_Mismatch (V1 /= V2);
4578 elsif Is_String_Type (Etype (E1))
4579 and then Nkind (Expr1) = N_String_Literal
4581 if Nkind (Expr2) /= N_String_Literal then
4582 Check_Mismatch (True);
4585 (not String_Equal (Strval (Expr1), Strval (Expr2)));
4589 elsif Is_Entity_Name (Expr1) then
4590 if Is_Entity_Name (Expr2) then
4591 if Entity (Expr1) = Entity (Expr2) then
4595 (not Same_Instantiated_Constant
4596 (Entity (Expr1), Entity (Expr2)));
4599 Check_Mismatch (True);
4602 elsif Is_Entity_Name (Original_Node (Expr1))
4603 and then Is_Entity_Name (Expr2)
4605 Same_Instantiated_Constant
4606 (Entity (Original_Node (Expr1)), Entity (Expr2))
4610 elsif Nkind (Expr1) = N_Null then
4611 Check_Mismatch (Nkind (Expr1) /= N_Null);
4614 Check_Mismatch (True);
4617 elsif Ekind (E1) = E_Variable then
4618 Check_Mismatch (not Same_Instantiated_Variable (E1, E2));
4620 elsif Ekind (E1) = E_Package then
4622 (Ekind (E1) /= Ekind (E2)
4623 or else Renamed_Object (E1) /= Renamed_Object (E2));
4625 elsif Is_Overloadable (E1) then
4627 -- Verify that the names of the entities match. Note that actuals
4628 -- that are attributes are rewritten as subprograms.
4631 (Ekind (E2) /= Ekind (E1) or else (Alias (E1)) /= Alias (E2));
4634 raise Program_Error;
4641 end Check_Formal_Package_Instance;
4643 ---------------------------
4644 -- Check_Formal_Packages --
4645 ---------------------------
4647 procedure Check_Formal_Packages (P_Id : Entity_Id) is
4649 Formal_P : Entity_Id;
4652 -- Iterate through the declarations in the instance, looking for package
4653 -- renaming declarations that denote instances of formal packages. Stop
4654 -- when we find the renaming of the current package itself. The
4655 -- declaration for a formal package without a box is followed by an
4656 -- internal entity that repeats the instantiation.
4658 E := First_Entity (P_Id);
4659 while Present (E) loop
4660 if Ekind (E) = E_Package then
4661 if Renamed_Object (E) = P_Id then
4664 elsif Nkind (Parent (E)) /= N_Package_Renaming_Declaration then
4667 elsif not Box_Present (Parent (Associated_Formal_Package (E))) then
4668 Formal_P := Next_Entity (E);
4669 Check_Formal_Package_Instance (Formal_P, E);
4671 -- After checking, remove the internal validating package. It
4672 -- is only needed for semantic checks, and as it may contain
4673 -- generic formal declarations it should not reach gigi.
4675 Remove (Unit_Declaration_Node (Formal_P));
4681 end Check_Formal_Packages;
4683 ---------------------------------
4684 -- Check_Forward_Instantiation --
4685 ---------------------------------
4687 procedure Check_Forward_Instantiation (Decl : Node_Id) is
4689 Gen_Comp : Entity_Id := Cunit_Entity (Get_Source_Unit (Decl));
4692 -- The instantiation appears before the generic body if we are in the
4693 -- scope of the unit containing the generic, either in its spec or in
4694 -- the package body. and before the generic body.
4696 if Ekind (Gen_Comp) = E_Package_Body then
4697 Gen_Comp := Spec_Entity (Gen_Comp);
4700 if In_Open_Scopes (Gen_Comp)
4701 and then No (Corresponding_Body (Decl))
4706 and then not Is_Compilation_Unit (S)
4707 and then not Is_Child_Unit (S)
4709 if Ekind (S) = E_Package then
4710 Set_Has_Forward_Instantiation (S);
4716 end Check_Forward_Instantiation;
4718 ---------------------------
4719 -- Check_Generic_Actuals --
4720 ---------------------------
4722 -- The visibility of the actuals may be different between the point of
4723 -- generic instantiation and the instantiation of the body.
4725 procedure Check_Generic_Actuals
4726 (Instance : Entity_Id;
4727 Is_Formal_Box : Boolean)
4732 function Denotes_Previous_Actual (Typ : Entity_Id) return Boolean;
4733 -- For a formal that is an array type, the component type is often a
4734 -- previous formal in the same unit. The privacy status of the component
4735 -- type will have been examined earlier in the traversal of the
4736 -- corresponding actuals, and this status should not be modified for the
4737 -- array type itself.
4739 -- To detect this case we have to rescan the list of formals, which
4740 -- is usually short enough to ignore the resulting inefficiency.
4742 function Denotes_Previous_Actual (Typ : Entity_Id) return Boolean is
4745 Prev := First_Entity (Instance);
4746 while Present (Prev) loop
4748 and then Nkind (Parent (Prev)) = N_Subtype_Declaration
4749 and then Is_Entity_Name (Subtype_Indication (Parent (Prev)))
4750 and then Entity (Subtype_Indication (Parent (Prev))) = Typ
4760 end Denotes_Previous_Actual;
4762 -- Start of processing for Check_Generic_Actuals
4765 E := First_Entity (Instance);
4766 while Present (E) loop
4768 and then Nkind (Parent (E)) = N_Subtype_Declaration
4769 and then Scope (Etype (E)) /= Instance
4770 and then Is_Entity_Name (Subtype_Indication (Parent (E)))
4772 if Is_Array_Type (E)
4773 and then Denotes_Previous_Actual (Component_Type (E))
4777 Check_Private_View (Subtype_Indication (Parent (E)));
4779 Set_Is_Generic_Actual_Type (E, True);
4780 Set_Is_Hidden (E, False);
4781 Set_Is_Potentially_Use_Visible (E,
4784 -- We constructed the generic actual type as a subtype of the
4785 -- supplied type. This means that it normally would not inherit
4786 -- subtype specific attributes of the actual, which is wrong for
4787 -- the generic case.
4789 Astype := Ancestor_Subtype (E);
4793 -- This can happen when E is an itype that is the full view of
4794 -- a private type completed, e.g. with a constrained array. In
4795 -- that case, use the first subtype, which will carry size
4796 -- information. The base type itself is unconstrained and will
4799 Astype := First_Subtype (E);
4802 Set_Size_Info (E, (Astype));
4803 Set_RM_Size (E, RM_Size (Astype));
4804 Set_First_Rep_Item (E, First_Rep_Item (Astype));
4806 if Is_Discrete_Or_Fixed_Point_Type (E) then
4807 Set_RM_Size (E, RM_Size (Astype));
4809 -- In nested instances, the base type of an access actual
4810 -- may itself be private, and need to be exchanged.
4812 elsif Is_Access_Type (E)
4813 and then Is_Private_Type (Etype (E))
4816 (New_Occurrence_Of (Etype (E), Sloc (Instance)));
4819 elsif Ekind (E) = E_Package then
4821 -- If this is the renaming for the current instance, we're done.
4822 -- Otherwise it is a formal package. If the corresponding formal
4823 -- was declared with a box, the (instantiations of the) generic
4824 -- formal part are also visible. Otherwise, ignore the entity
4825 -- created to validate the actuals.
4827 if Renamed_Object (E) = Instance then
4830 elsif Nkind (Parent (E)) /= N_Package_Renaming_Declaration then
4833 -- The visibility of a formal of an enclosing generic is already
4836 elsif Denotes_Formal_Package (E) then
4839 elsif Present (Associated_Formal_Package (E))
4840 and then not Is_Generic_Formal (E)
4842 if Box_Present (Parent (Associated_Formal_Package (E))) then
4843 Check_Generic_Actuals (Renamed_Object (E), True);
4846 Check_Generic_Actuals (Renamed_Object (E), False);
4849 Set_Is_Hidden (E, False);
4852 -- If this is a subprogram instance (in a wrapper package) the
4853 -- actual is fully visible.
4855 elsif Is_Wrapper_Package (Instance) then
4856 Set_Is_Hidden (E, False);
4858 -- If the formal package is declared with a box, or if the formal
4859 -- parameter is defaulted, it is visible in the body.
4862 or else Is_Visible_Formal (E)
4864 Set_Is_Hidden (E, False);
4869 end Check_Generic_Actuals;
4871 ------------------------------
4872 -- Check_Generic_Child_Unit --
4873 ------------------------------
4875 procedure Check_Generic_Child_Unit
4877 Parent_Installed : in out Boolean)
4879 Loc : constant Source_Ptr := Sloc (Gen_Id);
4880 Gen_Par : Entity_Id := Empty;
4881 Inst_Par : Entity_Id;
4885 function Find_Generic_Child
4887 Id : Node_Id) return Entity_Id;
4888 -- Search generic parent for possible child unit with the given name
4890 function In_Enclosing_Instance return Boolean;
4891 -- Within an instance of the parent, the child unit may be denoted
4892 -- by a simple name, or an abbreviated expanded name. Examine enclosing
4893 -- scopes to locate a possible parent instantiation.
4895 ------------------------
4896 -- Find_Generic_Child --
4897 ------------------------
4899 function Find_Generic_Child
4901 Id : Node_Id) return Entity_Id
4906 -- If entity of name is already set, instance has already been
4907 -- resolved, e.g. in an enclosing instantiation.
4909 if Present (Entity (Id)) then
4910 if Scope (Entity (Id)) = Scop then
4917 E := First_Entity (Scop);
4918 while Present (E) loop
4919 if Chars (E) = Chars (Id)
4920 and then Is_Child_Unit (E)
4922 if Is_Child_Unit (E)
4923 and then not Is_Visible_Child_Unit (E)
4926 ("generic child unit& is not visible", Gen_Id, E);
4938 end Find_Generic_Child;
4940 ---------------------------
4941 -- In_Enclosing_Instance --
4942 ---------------------------
4944 function In_Enclosing_Instance return Boolean is
4945 Enclosing_Instance : Node_Id;
4946 Instance_Decl : Node_Id;
4949 -- We do not inline any call that contains instantiations, except
4950 -- for instantiations of Unchecked_Conversion, so if we are within
4951 -- an inlined body the current instance does not require parents.
4953 if In_Inlined_Body then
4954 pragma Assert (Chars (Gen_Id) = Name_Unchecked_Conversion);
4958 -- Loop to check enclosing scopes
4960 Enclosing_Instance := Current_Scope;
4961 while Present (Enclosing_Instance) loop
4962 Instance_Decl := Unit_Declaration_Node (Enclosing_Instance);
4964 if Ekind (Enclosing_Instance) = E_Package
4965 and then Is_Generic_Instance (Enclosing_Instance)
4967 (Generic_Parent (Specification (Instance_Decl)))
4969 -- Check whether the generic we are looking for is a child of
4972 E := Find_Generic_Child
4973 (Generic_Parent (Specification (Instance_Decl)), Gen_Id);
4974 exit when Present (E);
4980 Enclosing_Instance := Scope (Enclosing_Instance);
4992 Make_Expanded_Name (Loc,
4994 Prefix => New_Occurrence_Of (Enclosing_Instance, Loc),
4995 Selector_Name => New_Occurrence_Of (E, Loc)));
4997 Set_Entity (Gen_Id, E);
4998 Set_Etype (Gen_Id, Etype (E));
4999 Parent_Installed := False; -- Already in scope.
5002 end In_Enclosing_Instance;
5004 -- Start of processing for Check_Generic_Child_Unit
5007 -- If the name of the generic is given by a selected component, it may
5008 -- be the name of a generic child unit, and the prefix is the name of an
5009 -- instance of the parent, in which case the child unit must be visible.
5010 -- If this instance is not in scope, it must be placed there and removed
5011 -- after instantiation, because what is being instantiated is not the
5012 -- original child, but the corresponding child present in the instance
5015 -- If the child is instantiated within the parent, it can be given by
5016 -- a simple name. In this case the instance is already in scope, but
5017 -- the child generic must be recovered from the generic parent as well.
5019 if Nkind (Gen_Id) = N_Selected_Component then
5020 S := Selector_Name (Gen_Id);
5021 Analyze (Prefix (Gen_Id));
5022 Inst_Par := Entity (Prefix (Gen_Id));
5024 if Ekind (Inst_Par) = E_Package
5025 and then Present (Renamed_Object (Inst_Par))
5027 Inst_Par := Renamed_Object (Inst_Par);
5030 if Ekind (Inst_Par) = E_Package then
5031 if Nkind (Parent (Inst_Par)) = N_Package_Specification then
5032 Gen_Par := Generic_Parent (Parent (Inst_Par));
5034 elsif Nkind (Parent (Inst_Par)) = N_Defining_Program_Unit_Name
5036 Nkind (Parent (Parent (Inst_Par))) = N_Package_Specification
5038 Gen_Par := Generic_Parent (Parent (Parent (Inst_Par)));
5041 elsif Ekind (Inst_Par) = E_Generic_Package
5042 and then Nkind (Parent (Gen_Id)) = N_Formal_Package_Declaration
5044 -- A formal package may be a real child package, and not the
5045 -- implicit instance within a parent. In this case the child is
5046 -- not visible and has to be retrieved explicitly as well.
5048 Gen_Par := Inst_Par;
5051 if Present (Gen_Par) then
5053 -- The prefix denotes an instantiation. The entity itself may be a
5054 -- nested generic, or a child unit.
5056 E := Find_Generic_Child (Gen_Par, S);
5059 Change_Selected_Component_To_Expanded_Name (Gen_Id);
5060 Set_Entity (Gen_Id, E);
5061 Set_Etype (Gen_Id, Etype (E));
5063 Set_Etype (S, Etype (E));
5065 -- Indicate that this is a reference to the parent
5067 if In_Extended_Main_Source_Unit (Gen_Id) then
5068 Set_Is_Instantiated (Inst_Par);
5071 -- A common mistake is to replicate the naming scheme of a
5072 -- hierarchy by instantiating a generic child directly, rather
5073 -- than the implicit child in a parent instance:
5075 -- generic .. package Gpar is ..
5076 -- generic .. package Gpar.Child is ..
5077 -- package Par is new Gpar ();
5080 -- package Par.Child is new Gpar.Child ();
5081 -- rather than Par.Child
5083 -- In this case the instantiation is within Par, which is an
5084 -- instance, but Gpar does not denote Par because we are not IN
5085 -- the instance of Gpar, so this is illegal. The test below
5086 -- recognizes this particular case.
5088 if Is_Child_Unit (E)
5089 and then not Comes_From_Source (Entity (Prefix (Gen_Id)))
5090 and then (not In_Instance
5091 or else Nkind (Parent (Parent (Gen_Id))) =
5095 ("prefix of generic child unit must be instance of parent",
5099 if not In_Open_Scopes (Inst_Par)
5100 and then Nkind (Parent (Gen_Id)) not in
5101 N_Generic_Renaming_Declaration
5103 Install_Parent (Inst_Par);
5104 Parent_Installed := True;
5106 elsif In_Open_Scopes (Inst_Par) then
5108 -- If the parent is already installed verify that the
5109 -- actuals for its formal packages declared with a box
5110 -- are already installed. This is necessary when the
5111 -- child instance is a child of the parent instance.
5112 -- In this case the parent is placed on the scope stack
5113 -- but the formal packages are not made visible.
5115 Install_Formal_Packages (Inst_Par);
5119 -- If the generic parent does not contain an entity that
5120 -- corresponds to the selector, the instance doesn't either.
5121 -- Analyzing the node will yield the appropriate error message.
5122 -- If the entity is not a child unit, then it is an inner
5123 -- generic in the parent.
5131 if Is_Child_Unit (Entity (Gen_Id))
5133 Nkind (Parent (Gen_Id)) not in N_Generic_Renaming_Declaration
5134 and then not In_Open_Scopes (Inst_Par)
5136 Install_Parent (Inst_Par);
5137 Parent_Installed := True;
5141 elsif Nkind (Gen_Id) = N_Expanded_Name then
5143 -- Entity already present, analyze prefix, whose meaning may be
5144 -- an instance in the current context. If it is an instance of
5145 -- a relative within another, the proper parent may still have
5146 -- to be installed, if they are not of the same generation.
5148 Analyze (Prefix (Gen_Id));
5149 Inst_Par := Entity (Prefix (Gen_Id));
5151 if In_Enclosing_Instance then
5154 elsif Present (Entity (Gen_Id))
5155 and then Is_Child_Unit (Entity (Gen_Id))
5156 and then not In_Open_Scopes (Inst_Par)
5158 Install_Parent (Inst_Par);
5159 Parent_Installed := True;
5162 elsif In_Enclosing_Instance then
5164 -- The child unit is found in some enclosing scope
5171 -- If this is the renaming of the implicit child in a parent
5172 -- instance, recover the parent name and install it.
5174 if Is_Entity_Name (Gen_Id) then
5175 E := Entity (Gen_Id);
5177 if Is_Generic_Unit (E)
5178 and then Nkind (Parent (E)) in N_Generic_Renaming_Declaration
5179 and then Is_Child_Unit (Renamed_Object (E))
5180 and then Is_Generic_Unit (Scope (Renamed_Object (E)))
5181 and then Nkind (Name (Parent (E))) = N_Expanded_Name
5184 New_Copy_Tree (Name (Parent (E))));
5185 Inst_Par := Entity (Prefix (Gen_Id));
5187 if not In_Open_Scopes (Inst_Par) then
5188 Install_Parent (Inst_Par);
5189 Parent_Installed := True;
5192 -- If it is a child unit of a non-generic parent, it may be
5193 -- use-visible and given by a direct name. Install parent as
5196 elsif Is_Generic_Unit (E)
5197 and then Is_Child_Unit (E)
5199 Nkind (Parent (Gen_Id)) not in N_Generic_Renaming_Declaration
5200 and then not Is_Generic_Unit (Scope (E))
5202 if not In_Open_Scopes (Scope (E)) then
5203 Install_Parent (Scope (E));
5204 Parent_Installed := True;
5209 end Check_Generic_Child_Unit;
5211 -----------------------------
5212 -- Check_Hidden_Child_Unit --
5213 -----------------------------
5215 procedure Check_Hidden_Child_Unit
5217 Gen_Unit : Entity_Id;
5218 Act_Decl_Id : Entity_Id)
5220 Gen_Id : constant Node_Id := Name (N);
5223 if Is_Child_Unit (Gen_Unit)
5224 and then Is_Child_Unit (Act_Decl_Id)
5225 and then Nkind (Gen_Id) = N_Expanded_Name
5226 and then Entity (Prefix (Gen_Id)) = Scope (Act_Decl_Id)
5227 and then Chars (Gen_Unit) = Chars (Act_Decl_Id)
5229 Error_Msg_Node_2 := Scope (Act_Decl_Id);
5231 ("generic unit & is implicitly declared in &",
5232 Defining_Unit_Name (N), Gen_Unit);
5233 Error_Msg_N ("\instance must have different name",
5234 Defining_Unit_Name (N));
5236 end Check_Hidden_Child_Unit;
5238 ------------------------
5239 -- Check_Private_View --
5240 ------------------------
5242 procedure Check_Private_View (N : Node_Id) is
5243 T : constant Entity_Id := Etype (N);
5247 -- Exchange views if the type was not private in the generic but is
5248 -- private at the point of instantiation. Do not exchange views if
5249 -- the scope of the type is in scope. This can happen if both generic
5250 -- and instance are sibling units, or if type is defined in a parent.
5251 -- In this case the visibility of the type will be correct for all
5255 BT := Base_Type (T);
5257 if Is_Private_Type (T)
5258 and then not Has_Private_View (N)
5259 and then Present (Full_View (T))
5260 and then not In_Open_Scopes (Scope (T))
5262 -- In the generic, the full type was visible. Save the private
5263 -- entity, for subsequent exchange.
5267 elsif Has_Private_View (N)
5268 and then not Is_Private_Type (T)
5269 and then not Has_Been_Exchanged (T)
5270 and then Etype (Get_Associated_Node (N)) /= T
5272 -- Only the private declaration was visible in the generic. If
5273 -- the type appears in a subtype declaration, the subtype in the
5274 -- instance must have a view compatible with that of its parent,
5275 -- which must be exchanged (see corresponding code in Restore_
5276 -- Private_Views). Otherwise, if the type is defined in a parent
5277 -- unit, leave full visibility within instance, which is safe.
5279 if In_Open_Scopes (Scope (Base_Type (T)))
5280 and then not Is_Private_Type (Base_Type (T))
5281 and then Comes_From_Source (Base_Type (T))
5285 elsif Nkind (Parent (N)) = N_Subtype_Declaration
5286 or else not In_Private_Part (Scope (Base_Type (T)))
5288 Prepend_Elmt (T, Exchanged_Views);
5289 Exchange_Declarations (Etype (Get_Associated_Node (N)));
5292 -- For composite types with inconsistent representation exchange
5293 -- component types accordingly.
5295 elsif Is_Access_Type (T)
5296 and then Is_Private_Type (Designated_Type (T))
5297 and then not Has_Private_View (N)
5298 and then Present (Full_View (Designated_Type (T)))
5300 Switch_View (Designated_Type (T));
5302 elsif Is_Array_Type (T) then
5303 if Is_Private_Type (Component_Type (T))
5304 and then not Has_Private_View (N)
5305 and then Present (Full_View (Component_Type (T)))
5307 Switch_View (Component_Type (T));
5310 -- The normal exchange mechanism relies on the setting of a
5311 -- flag on the reference in the generic. However, an additional
5312 -- mechanism is needed for types that are not explicitly mentioned
5313 -- in the generic, but may be needed in expanded code in the
5314 -- instance. This includes component types of arrays and
5315 -- designated types of access types. This processing must also
5316 -- include the index types of arrays which we take care of here.
5323 Indx := First_Index (T);
5324 Typ := Base_Type (Etype (Indx));
5325 while Present (Indx) loop
5326 if Is_Private_Type (Typ)
5327 and then Present (Full_View (Typ))
5336 elsif Is_Private_Type (T)
5337 and then Present (Full_View (T))
5338 and then Is_Array_Type (Full_View (T))
5339 and then Is_Private_Type (Component_Type (Full_View (T)))
5343 -- Finally, a non-private subtype may have a private base type, which
5344 -- must be exchanged for consistency. This can happen when a package
5345 -- body is instantiated, when the scope stack is empty but in fact
5346 -- the subtype and the base type are declared in an enclosing scope.
5348 -- Note that in this case we introduce an inconsistency in the view
5349 -- set, because we switch the base type BT, but there could be some
5350 -- private dependent subtypes of BT which remain unswitched. Such
5351 -- subtypes might need to be switched at a later point (see specific
5352 -- provision for that case in Switch_View).
5354 elsif not Is_Private_Type (T)
5355 and then not Has_Private_View (N)
5356 and then Is_Private_Type (BT)
5357 and then Present (Full_View (BT))
5358 and then not Is_Generic_Type (BT)
5359 and then not In_Open_Scopes (BT)
5361 Prepend_Elmt (Full_View (BT), Exchanged_Views);
5362 Exchange_Declarations (BT);
5365 end Check_Private_View;
5367 --------------------------
5368 -- Contains_Instance_Of --
5369 --------------------------
5371 function Contains_Instance_Of
5374 N : Node_Id) return Boolean
5382 -- Verify that there are no circular instantiations. We check whether
5383 -- the unit contains an instance of the current scope or some enclosing
5384 -- scope (in case one of the instances appears in a subunit). Longer
5385 -- circularities involving subunits might seem too pathological to
5386 -- consider, but they were not too pathological for the authors of
5387 -- DEC bc30vsq, so we loop over all enclosing scopes, and mark all
5388 -- enclosing generic scopes as containing an instance.
5391 -- Within a generic subprogram body, the scope is not generic, to
5392 -- allow for recursive subprograms. Use the declaration to determine
5393 -- whether this is a generic unit.
5395 if Ekind (Scop) = E_Generic_Package
5396 or else (Is_Subprogram (Scop)
5397 and then Nkind (Unit_Declaration_Node (Scop)) =
5398 N_Generic_Subprogram_Declaration)
5400 Elmt := First_Elmt (Inner_Instances (Inner));
5402 while Present (Elmt) loop
5403 if Node (Elmt) = Scop then
5404 Error_Msg_Node_2 := Inner;
5406 ("circular Instantiation: & instantiated within &!",
5410 elsif Node (Elmt) = Inner then
5413 elsif Contains_Instance_Of (Node (Elmt), Scop, N) then
5414 Error_Msg_Node_2 := Inner;
5416 ("circular Instantiation: & instantiated within &!",
5424 -- Indicate that Inner is being instantiated within Scop
5426 Append_Elmt (Inner, Inner_Instances (Scop));
5429 if Scop = Standard_Standard then
5432 Scop := Scope (Scop);
5437 end Contains_Instance_Of;
5439 -----------------------
5440 -- Copy_Generic_Node --
5441 -----------------------
5443 function Copy_Generic_Node
5445 Parent_Id : Node_Id;
5446 Instantiating : Boolean) return Node_Id
5451 function Copy_Generic_Descendant (D : Union_Id) return Union_Id;
5452 -- Check the given value of one of the Fields referenced by the
5453 -- current node to determine whether to copy it recursively. The
5454 -- field may hold a Node_Id, a List_Id, or an Elist_Id, or a plain
5455 -- value (Sloc, Uint, Char) in which case it need not be copied.
5457 procedure Copy_Descendants;
5458 -- Common utility for various nodes
5460 function Copy_Generic_Elist (E : Elist_Id) return Elist_Id;
5461 -- Make copy of element list
5463 function Copy_Generic_List
5465 Parent_Id : Node_Id) return List_Id;
5466 -- Apply Copy_Node recursively to the members of a node list
5468 function In_Defining_Unit_Name (Nam : Node_Id) return Boolean;
5469 -- True if an identifier is part of the defining program unit name
5470 -- of a child unit. The entity of such an identifier must be kept
5471 -- (for ASIS use) even though as the name of an enclosing generic
5472 -- it would otherwise not be preserved in the generic tree.
5474 ----------------------
5475 -- Copy_Descendants --
5476 ----------------------
5478 procedure Copy_Descendants is
5480 use Atree.Unchecked_Access;
5481 -- This code section is part of the implementation of an untyped
5482 -- tree traversal, so it needs direct access to node fields.
5485 Set_Field1 (New_N, Copy_Generic_Descendant (Field1 (N)));
5486 Set_Field2 (New_N, Copy_Generic_Descendant (Field2 (N)));
5487 Set_Field3 (New_N, Copy_Generic_Descendant (Field3 (N)));
5488 Set_Field4 (New_N, Copy_Generic_Descendant (Field4 (N)));
5489 Set_Field5 (New_N, Copy_Generic_Descendant (Field5 (N)));
5490 end Copy_Descendants;
5492 -----------------------------
5493 -- Copy_Generic_Descendant --
5494 -----------------------------
5496 function Copy_Generic_Descendant (D : Union_Id) return Union_Id is
5498 if D = Union_Id (Empty) then
5501 elsif D in Node_Range then
5503 (Copy_Generic_Node (Node_Id (D), New_N, Instantiating));
5505 elsif D in List_Range then
5506 return Union_Id (Copy_Generic_List (List_Id (D), New_N));
5508 elsif D in Elist_Range then
5509 return Union_Id (Copy_Generic_Elist (Elist_Id (D)));
5511 -- Nothing else is copyable (e.g. Uint values), return as is
5516 end Copy_Generic_Descendant;
5518 ------------------------
5519 -- Copy_Generic_Elist --
5520 ------------------------
5522 function Copy_Generic_Elist (E : Elist_Id) return Elist_Id is
5529 M := First_Elmt (E);
5530 while Present (M) loop
5532 (Copy_Generic_Node (Node (M), Empty, Instantiating), L);
5541 end Copy_Generic_Elist;
5543 -----------------------
5544 -- Copy_Generic_List --
5545 -----------------------
5547 function Copy_Generic_List
5549 Parent_Id : Node_Id) return List_Id
5557 Set_Parent (New_L, Parent_Id);
5560 while Present (N) loop
5561 Append (Copy_Generic_Node (N, Empty, Instantiating), New_L);
5570 end Copy_Generic_List;
5572 ---------------------------
5573 -- In_Defining_Unit_Name --
5574 ---------------------------
5576 function In_Defining_Unit_Name (Nam : Node_Id) return Boolean is
5578 return Present (Parent (Nam))
5579 and then (Nkind (Parent (Nam)) = N_Defining_Program_Unit_Name
5581 (Nkind (Parent (Nam)) = N_Expanded_Name
5582 and then In_Defining_Unit_Name (Parent (Nam))));
5583 end In_Defining_Unit_Name;
5585 -- Start of processing for Copy_Generic_Node
5592 New_N := New_Copy (N);
5594 if Instantiating then
5595 Adjust_Instantiation_Sloc (New_N, S_Adjustment);
5598 if not Is_List_Member (N) then
5599 Set_Parent (New_N, Parent_Id);
5602 -- If defining identifier, then all fields have been copied already
5604 if Nkind (New_N) in N_Entity then
5607 -- Special casing for identifiers and other entity names and operators
5609 elsif Nkind (New_N) = N_Identifier
5610 or else Nkind (New_N) = N_Character_Literal
5611 or else Nkind (New_N) = N_Expanded_Name
5612 or else Nkind (New_N) = N_Operator_Symbol
5613 or else Nkind (New_N) in N_Op
5615 if not Instantiating then
5617 -- Link both nodes in order to assign subsequently the
5618 -- entity of the copy to the original node, in case this
5619 -- is a global reference.
5621 Set_Associated_Node (N, New_N);
5623 -- If we are within an instantiation, this is a nested generic
5624 -- that has already been analyzed at the point of definition. We
5625 -- must preserve references that were global to the enclosing
5626 -- parent at that point. Other occurrences, whether global or
5627 -- local to the current generic, must be resolved anew, so we
5628 -- reset the entity in the generic copy. A global reference has a
5629 -- smaller depth than the parent, or else the same depth in case
5630 -- both are distinct compilation units.
5632 -- It is also possible for Current_Instantiated_Parent to be
5633 -- defined, and for this not to be a nested generic, namely if the
5634 -- unit is loaded through Rtsfind. In that case, the entity of
5635 -- New_N is only a link to the associated node, and not a defining
5638 -- The entities for parent units in the defining_program_unit of a
5639 -- generic child unit are established when the context of the unit
5640 -- is first analyzed, before the generic copy is made. They are
5641 -- preserved in the copy for use in ASIS queries.
5643 Ent := Entity (New_N);
5645 if No (Current_Instantiated_Parent.Gen_Id) then
5647 or else Nkind (Ent) /= N_Defining_Identifier
5648 or else not In_Defining_Unit_Name (N)
5650 Set_Associated_Node (New_N, Empty);
5655 not (Nkind (Ent) = N_Defining_Identifier
5657 Nkind (Ent) = N_Defining_Character_Literal
5659 Nkind (Ent) = N_Defining_Operator_Symbol)
5660 or else No (Scope (Ent))
5661 or else Scope (Ent) = Current_Instantiated_Parent.Gen_Id
5662 or else (Scope_Depth (Scope (Ent)) >
5663 Scope_Depth (Current_Instantiated_Parent.Gen_Id)
5665 Get_Source_Unit (Ent) =
5666 Get_Source_Unit (Current_Instantiated_Parent.Gen_Id))
5668 Set_Associated_Node (New_N, Empty);
5671 -- Case of instantiating identifier or some other name or operator
5674 -- If the associated node is still defined, the entity in it is
5675 -- global, and must be copied to the instance. If this copy is
5676 -- being made for a body to inline, it is applied to an
5677 -- instantiated tree, and the entity is already present and must
5678 -- be also preserved.
5681 Assoc : constant Node_Id := Get_Associated_Node (N);
5683 if Present (Assoc) then
5684 if Nkind (Assoc) = Nkind (N) then
5685 Set_Entity (New_N, Entity (Assoc));
5686 Check_Private_View (N);
5688 elsif Nkind (Assoc) = N_Function_Call then
5689 Set_Entity (New_N, Entity (Name (Assoc)));
5691 elsif (Nkind (Assoc) = N_Defining_Identifier
5692 or else Nkind (Assoc) = N_Defining_Character_Literal
5693 or else Nkind (Assoc) = N_Defining_Operator_Symbol)
5694 and then Expander_Active
5696 -- Inlining case: we are copying a tree that contains
5697 -- global entities, which are preserved in the copy to be
5698 -- used for subsequent inlining.
5703 Set_Entity (New_N, Empty);
5709 -- For expanded name, we must copy the Prefix and Selector_Name
5711 if Nkind (N) = N_Expanded_Name then
5713 (New_N, Copy_Generic_Node (Prefix (N), New_N, Instantiating));
5715 Set_Selector_Name (New_N,
5716 Copy_Generic_Node (Selector_Name (N), New_N, Instantiating));
5718 -- For operators, we must copy the right operand
5720 elsif Nkind (N) in N_Op then
5721 Set_Right_Opnd (New_N,
5722 Copy_Generic_Node (Right_Opnd (N), New_N, Instantiating));
5724 -- And for binary operators, the left operand as well
5726 if Nkind (N) in N_Binary_Op then
5727 Set_Left_Opnd (New_N,
5728 Copy_Generic_Node (Left_Opnd (N), New_N, Instantiating));
5732 -- Special casing for stubs
5734 elsif Nkind (N) in N_Body_Stub then
5736 -- In any case, we must copy the specification or defining
5737 -- identifier as appropriate.
5739 if Nkind (N) = N_Subprogram_Body_Stub then
5740 Set_Specification (New_N,
5741 Copy_Generic_Node (Specification (N), New_N, Instantiating));
5744 Set_Defining_Identifier (New_N,
5746 (Defining_Identifier (N), New_N, Instantiating));
5749 -- If we are not instantiating, then this is where we load and
5750 -- analyze subunits, i.e. at the point where the stub occurs. A
5751 -- more permissivle system might defer this analysis to the point
5752 -- of instantiation, but this seems to complicated for now.
5754 if not Instantiating then
5756 Subunit_Name : constant Unit_Name_Type := Get_Unit_Name (N);
5758 Unum : Unit_Number_Type;
5764 (Load_Name => Subunit_Name,
5769 -- If the proper body is not found, a warning message will be
5770 -- emitted when analyzing the stub, or later at the the point
5771 -- of instantiation. Here we just leave the stub as is.
5773 if Unum = No_Unit then
5774 Subunits_Missing := True;
5775 goto Subunit_Not_Found;
5778 Subunit := Cunit (Unum);
5780 if Nkind (Unit (Subunit)) /= N_Subunit then
5782 ("found child unit instead of expected SEPARATE subunit",
5784 Error_Msg_Sloc := Sloc (N);
5785 Error_Msg_N ("\to complete stub #", Subunit);
5786 goto Subunit_Not_Found;
5789 -- We must create a generic copy of the subunit, in order to
5790 -- perform semantic analysis on it, and we must replace the
5791 -- stub in the original generic unit with the subunit, in order
5792 -- to preserve non-local references within.
5794 -- Only the proper body needs to be copied. Library_Unit and
5795 -- context clause are simply inherited by the generic copy.
5796 -- Note that the copy (which may be recursive if there are
5797 -- nested subunits) must be done first, before attaching it to
5798 -- the enclosing generic.
5802 (Proper_Body (Unit (Subunit)),
5803 Empty, Instantiating => False);
5805 -- Now place the original proper body in the original generic
5806 -- unit. This is a body, not a compilation unit.
5808 Rewrite (N, Proper_Body (Unit (Subunit)));
5809 Set_Is_Compilation_Unit (Defining_Entity (N), False);
5810 Set_Was_Originally_Stub (N);
5812 -- Finally replace the body of the subunit with its copy, and
5813 -- make this new subunit into the library unit of the generic
5814 -- copy, which does not have stubs any longer.
5816 Set_Proper_Body (Unit (Subunit), New_Body);
5817 Set_Library_Unit (New_N, Subunit);
5818 Inherit_Context (Unit (Subunit), N);
5821 -- If we are instantiating, this must be an error case, since
5822 -- otherwise we would have replaced the stub node by the proper body
5823 -- that corresponds. So just ignore it in the copy (i.e. we have
5824 -- copied it, and that is good enough).
5830 <<Subunit_Not_Found>> null;
5832 -- If the node is a compilation unit, it is the subunit of a stub, which
5833 -- has been loaded already (see code below). In this case, the library
5834 -- unit field of N points to the parent unit (which is a compilation
5835 -- unit) and need not (and cannot!) be copied.
5837 -- When the proper body of the stub is analyzed, thie library_unit link
5838 -- is used to establish the proper context (see sem_ch10).
5840 -- The other fields of a compilation unit are copied as usual
5842 elsif Nkind (N) = N_Compilation_Unit then
5844 -- This code can only be executed when not instantiating, because in
5845 -- the copy made for an instantiation, the compilation unit node has
5846 -- disappeared at the point that a stub is replaced by its proper
5849 pragma Assert (not Instantiating);
5851 Set_Context_Items (New_N,
5852 Copy_Generic_List (Context_Items (N), New_N));
5855 Copy_Generic_Node (Unit (N), New_N, False));
5857 Set_First_Inlined_Subprogram (New_N,
5859 (First_Inlined_Subprogram (N), New_N, False));
5861 Set_Aux_Decls_Node (New_N,
5862 Copy_Generic_Node (Aux_Decls_Node (N), New_N, False));
5864 -- For an assignment node, the assignment is known to be semantically
5865 -- legal if we are instantiating the template. This avoids incorrect
5866 -- diagnostics in generated code.
5868 elsif Nkind (N) = N_Assignment_Statement then
5870 -- Copy name and expression fields in usual manner
5873 Copy_Generic_Node (Name (N), New_N, Instantiating));
5875 Set_Expression (New_N,
5876 Copy_Generic_Node (Expression (N), New_N, Instantiating));
5878 if Instantiating then
5879 Set_Assignment_OK (Name (New_N), True);
5882 elsif Nkind (N) = N_Aggregate
5883 or else Nkind (N) = N_Extension_Aggregate
5886 if not Instantiating then
5887 Set_Associated_Node (N, New_N);
5890 if Present (Get_Associated_Node (N))
5891 and then Nkind (Get_Associated_Node (N)) = Nkind (N)
5893 -- In the generic the aggregate has some composite type. If at
5894 -- the point of instantiation the type has a private view,
5895 -- install the full view (and that of its ancestors, if any).
5898 T : Entity_Id := (Etype (Get_Associated_Node (New_N)));
5903 and then Is_Private_Type (T)
5909 and then Is_Tagged_Type (T)
5910 and then Is_Derived_Type (T)
5912 Rt := Root_Type (T);
5917 if Is_Private_Type (T) then
5928 -- Do not copy the associated node, which points to
5929 -- the generic copy of the aggregate.
5932 use Atree.Unchecked_Access;
5933 -- This code section is part of the implementation of an untyped
5934 -- tree traversal, so it needs direct access to node fields.
5937 Set_Field1 (New_N, Copy_Generic_Descendant (Field1 (N)));
5938 Set_Field2 (New_N, Copy_Generic_Descendant (Field2 (N)));
5939 Set_Field3 (New_N, Copy_Generic_Descendant (Field3 (N)));
5940 Set_Field5 (New_N, Copy_Generic_Descendant (Field5 (N)));
5943 -- Allocators do not have an identifier denoting the access type,
5944 -- so we must locate it through the expression to check whether
5945 -- the views are consistent.
5947 elsif Nkind (N) = N_Allocator
5948 and then Nkind (Expression (N)) = N_Qualified_Expression
5949 and then Is_Entity_Name (Subtype_Mark (Expression (N)))
5950 and then Instantiating
5953 T : constant Node_Id :=
5954 Get_Associated_Node (Subtype_Mark (Expression (N)));
5960 -- Retrieve the allocator node in the generic copy
5962 Acc_T := Etype (Parent (Parent (T)));
5964 and then Is_Private_Type (Acc_T)
5966 Switch_View (Acc_T);
5973 -- For a proper body, we must catch the case of a proper body that
5974 -- replaces a stub. This represents the point at which a separate
5975 -- compilation unit, and hence template file, may be referenced, so we
5976 -- must make a new source instantiation entry for the template of the
5977 -- subunit, and ensure that all nodes in the subunit are adjusted using
5978 -- this new source instantiation entry.
5980 elsif Nkind (N) in N_Proper_Body then
5982 Save_Adjustment : constant Sloc_Adjustment := S_Adjustment;
5985 if Instantiating and then Was_Originally_Stub (N) then
5986 Create_Instantiation_Source
5987 (Instantiation_Node,
5988 Defining_Entity (N),
5993 -- Now copy the fields of the proper body, using the new
5994 -- adjustment factor if one was needed as per test above.
5998 -- Restore the original adjustment factor in case changed
6000 S_Adjustment := Save_Adjustment;
6003 -- Don't copy Ident or Comment pragmas, since the comment belongs to the
6004 -- generic unit, not to the instantiating unit.
6006 elsif Nkind (N) = N_Pragma
6007 and then Instantiating
6010 Prag_Id : constant Pragma_Id := Get_Pragma_Id (Chars (N));
6013 if Prag_Id = Pragma_Ident
6014 or else Prag_Id = Pragma_Comment
6016 New_N := Make_Null_Statement (Sloc (N));
6023 elsif Nkind (N) = N_Integer_Literal
6024 or else Nkind (N) = N_Real_Literal
6025 or else Nkind (N) = N_String_Literal
6027 -- No descendant fields need traversing
6031 -- For the remaining nodes, copy recursively their descendants
6037 and then Nkind (N) = N_Subprogram_Body
6039 Set_Generic_Parent (Specification (New_N), N);
6044 end Copy_Generic_Node;
6046 ----------------------------
6047 -- Denotes_Formal_Package --
6048 ----------------------------
6050 function Denotes_Formal_Package
6052 On_Exit : Boolean := False) return Boolean
6055 Scop : constant Entity_Id := Scope (Pack);
6062 (Instance_Envs.Last).Instantiated_Parent.Act_Id;
6064 Par := Current_Instantiated_Parent.Act_Id;
6067 if Ekind (Scop) = E_Generic_Package
6068 or else Nkind (Unit_Declaration_Node (Scop)) =
6069 N_Generic_Subprogram_Declaration
6073 elsif Nkind (Original_Node (Unit_Declaration_Node (Pack))) =
6074 N_Formal_Package_Declaration
6082 -- Check whether this package is associated with a formal package of
6083 -- the enclosing instantiation. Iterate over the list of renamings.
6085 E := First_Entity (Par);
6086 while Present (E) loop
6087 if Ekind (E) /= E_Package
6088 or else Nkind (Parent (E)) /= N_Package_Renaming_Declaration
6092 elsif Renamed_Object (E) = Par then
6095 elsif Renamed_Object (E) = Pack then
6104 end Denotes_Formal_Package;
6110 procedure End_Generic is
6112 -- ??? More things could be factored out in this routine. Should
6113 -- probably be done at a later stage.
6115 Inside_A_Generic := Generic_Flags.Table (Generic_Flags.Last);
6116 Generic_Flags.Decrement_Last;
6118 Expander_Mode_Restore;
6121 ----------------------
6122 -- Find_Actual_Type --
6123 ----------------------
6125 function Find_Actual_Type
6127 Gen_Scope : Entity_Id) return Entity_Id
6132 if not Is_Child_Unit (Gen_Scope) then
6133 return Get_Instance_Of (Typ);
6135 elsif not Is_Generic_Type (Typ)
6136 or else Scope (Typ) = Gen_Scope
6138 return Get_Instance_Of (Typ);
6141 T := Current_Entity (Typ);
6142 while Present (T) loop
6143 if In_Open_Scopes (Scope (T)) then
6146 elsif Is_Generic_Actual_Type (T) then
6155 end Find_Actual_Type;
6157 ----------------------------
6158 -- Freeze_Subprogram_Body --
6159 ----------------------------
6161 procedure Freeze_Subprogram_Body
6162 (Inst_Node : Node_Id;
6164 Pack_Id : Entity_Id)
6167 Gen_Unit : constant Entity_Id := Get_Generic_Entity (Inst_Node);
6168 Par : constant Entity_Id := Scope (Gen_Unit);
6173 function Earlier (N1, N2 : Node_Id) return Boolean;
6174 -- Yields True if N1 and N2 appear in the same compilation unit,
6175 -- ignoring subunits, and if N1 is to the left of N2 in a left-to-right
6176 -- traversal of the tree for the unit.
6178 function Enclosing_Body (N : Node_Id) return Node_Id;
6179 -- Find innermost package body that encloses the given node, and which
6180 -- is not a compilation unit. Freeze nodes for the instance, or for its
6181 -- enclosing body, may be inserted after the enclosing_body of the
6184 function Package_Freeze_Node (B : Node_Id) return Node_Id;
6185 -- Find entity for given package body, and locate or create a freeze
6188 function True_Parent (N : Node_Id) return Node_Id;
6189 -- For a subunit, return parent of corresponding stub
6195 function Earlier (N1, N2 : Node_Id) return Boolean is
6201 procedure Find_Depth (P : in out Node_Id; D : in out Integer);
6202 -- Find distance from given node to enclosing compilation unit
6208 procedure Find_Depth (P : in out Node_Id; D : in out Integer) is
6211 and then Nkind (P) /= N_Compilation_Unit
6213 P := True_Parent (P);
6218 -- Start of procesing for Earlier
6221 Find_Depth (P1, D1);
6222 Find_Depth (P2, D2);
6232 P1 := True_Parent (P1);
6237 P2 := True_Parent (P2);
6241 -- At this point P1 and P2 are at the same distance from the root.
6242 -- We examine their parents until we find a common declarative
6243 -- list, at which point we can establish their relative placement
6244 -- by comparing their ultimate slocs. If we reach the root,
6245 -- N1 and N2 do not descend from the same declarative list (e.g.
6246 -- one is nested in the declarative part and the other is in a block
6247 -- in the statement part) and the earlier one is already frozen.
6249 while not Is_List_Member (P1)
6250 or else not Is_List_Member (P2)
6251 or else List_Containing (P1) /= List_Containing (P2)
6253 P1 := True_Parent (P1);
6254 P2 := True_Parent (P2);
6256 if Nkind (Parent (P1)) = N_Subunit then
6257 P1 := Corresponding_Stub (Parent (P1));
6260 if Nkind (Parent (P2)) = N_Subunit then
6261 P2 := Corresponding_Stub (Parent (P2));
6270 Top_Level_Location (Sloc (P1)) < Top_Level_Location (Sloc (P2));
6273 --------------------
6274 -- Enclosing_Body --
6275 --------------------
6277 function Enclosing_Body (N : Node_Id) return Node_Id is
6278 P : Node_Id := Parent (N);
6282 and then Nkind (Parent (P)) /= N_Compilation_Unit
6284 if Nkind (P) = N_Package_Body then
6286 if Nkind (Parent (P)) = N_Subunit then
6287 return Corresponding_Stub (Parent (P));
6293 P := True_Parent (P);
6299 -------------------------
6300 -- Package_Freeze_Node --
6301 -------------------------
6303 function Package_Freeze_Node (B : Node_Id) return Node_Id is
6307 if Nkind (B) = N_Package_Body then
6308 Id := Corresponding_Spec (B);
6310 else pragma Assert (Nkind (B) = N_Package_Body_Stub);
6311 Id := Corresponding_Spec (Proper_Body (Unit (Library_Unit (B))));
6314 Ensure_Freeze_Node (Id);
6315 return Freeze_Node (Id);
6316 end Package_Freeze_Node;
6322 function True_Parent (N : Node_Id) return Node_Id is
6324 if Nkind (Parent (N)) = N_Subunit then
6325 return Parent (Corresponding_Stub (Parent (N)));
6331 -- Start of processing of Freeze_Subprogram_Body
6334 -- If the instance and the generic body appear within the same unit, and
6335 -- the instance preceeds the generic, the freeze node for the instance
6336 -- must appear after that of the generic. If the generic is nested
6337 -- within another instance I2, then current instance must be frozen
6338 -- after I2. In both cases, the freeze nodes are those of enclosing
6339 -- packages. Otherwise, the freeze node is placed at the end of the
6340 -- current declarative part.
6342 Enc_G := Enclosing_Body (Gen_Body);
6343 Enc_I := Enclosing_Body (Inst_Node);
6344 Ensure_Freeze_Node (Pack_Id);
6345 F_Node := Freeze_Node (Pack_Id);
6347 if Is_Generic_Instance (Par)
6348 and then Present (Freeze_Node (Par))
6350 In_Same_Declarative_Part (Freeze_Node (Par), Inst_Node)
6352 if ABE_Is_Certain (Get_Package_Instantiation_Node (Par)) then
6354 -- The parent was a premature instantiation. Insert freeze node at
6355 -- the end the current declarative part.
6357 Insert_After_Last_Decl (Inst_Node, F_Node);
6360 Insert_After (Freeze_Node (Par), F_Node);
6363 -- The body enclosing the instance should be frozen after the body that
6364 -- includes the generic, because the body of the instance may make
6365 -- references to entities therein. If the two are not in the same
6366 -- declarative part, or if the one enclosing the instance is frozen
6367 -- already, freeze the instance at the end of the current declarative
6370 elsif Is_Generic_Instance (Par)
6371 and then Present (Freeze_Node (Par))
6372 and then Present (Enc_I)
6374 if In_Same_Declarative_Part (Freeze_Node (Par), Enc_I)
6376 (Nkind (Enc_I) = N_Package_Body
6378 In_Same_Declarative_Part (Freeze_Node (Par), Parent (Enc_I)))
6380 -- The enclosing package may contain several instances. Rather
6381 -- than computing the earliest point at which to insert its
6382 -- freeze node, we place it at the end of the declarative part
6383 -- of the parent of the generic.
6385 Insert_After_Last_Decl
6386 (Freeze_Node (Par), Package_Freeze_Node (Enc_I));
6389 Insert_After_Last_Decl (Inst_Node, F_Node);
6391 elsif Present (Enc_G)
6392 and then Present (Enc_I)
6393 and then Enc_G /= Enc_I
6394 and then Earlier (Inst_Node, Gen_Body)
6396 if Nkind (Enc_G) = N_Package_Body then
6397 E_G_Id := Corresponding_Spec (Enc_G);
6398 else pragma Assert (Nkind (Enc_G) = N_Package_Body_Stub);
6400 Corresponding_Spec (Proper_Body (Unit (Library_Unit (Enc_G))));
6403 -- Freeze package that encloses instance, and place node after
6404 -- package that encloses generic. If enclosing package is already
6405 -- frozen we have to assume it is at the proper place. This may be
6406 -- a potential ABE that requires dynamic checking.
6408 Insert_After_Last_Decl (Enc_G, Package_Freeze_Node (Enc_I));
6410 -- Freeze enclosing subunit before instance
6412 Ensure_Freeze_Node (E_G_Id);
6414 if not Is_List_Member (Freeze_Node (E_G_Id)) then
6415 Insert_After (Enc_G, Freeze_Node (E_G_Id));
6418 Insert_After_Last_Decl (Inst_Node, F_Node);
6421 -- If none of the above, insert freeze node at the end of the current
6422 -- declarative part.
6424 Insert_After_Last_Decl (Inst_Node, F_Node);
6426 end Freeze_Subprogram_Body;
6432 function Get_Gen_Id (E : Assoc_Ptr) return Entity_Id is
6434 return Generic_Renamings.Table (E).Gen_Id;
6437 ---------------------
6438 -- Get_Instance_Of --
6439 ---------------------
6441 function Get_Instance_Of (A : Entity_Id) return Entity_Id is
6442 Res : constant Assoc_Ptr := Generic_Renamings_HTable.Get (A);
6445 if Res /= Assoc_Null then
6446 return Generic_Renamings.Table (Res).Act_Id;
6448 -- On exit, entity is not instantiated: not a generic parameter, or
6449 -- else parameter of an inner generic unit.
6453 end Get_Instance_Of;
6455 ------------------------------------
6456 -- Get_Package_Instantiation_Node --
6457 ------------------------------------
6459 function Get_Package_Instantiation_Node (A : Entity_Id) return Node_Id is
6460 Decl : Node_Id := Unit_Declaration_Node (A);
6464 -- If the Package_Instantiation attribute has been set on the package
6465 -- entity, then use it directly when it (or its Original_Node) refers
6466 -- to an N_Package_Instantiation node. In principle it should be
6467 -- possible to have this field set in all cases, which should be
6468 -- investigated, and would allow this function to be significantly
6471 if Present (Package_Instantiation (A)) then
6472 if Nkind (Package_Instantiation (A)) = N_Package_Instantiation then
6473 return Package_Instantiation (A);
6475 elsif Nkind (Original_Node (Package_Instantiation (A)))
6476 = N_Package_Instantiation
6478 return Original_Node (Package_Instantiation (A));
6482 -- If the instantiation is a compilation unit that does not need body
6483 -- then the instantiation node has been rewritten as a package
6484 -- declaration for the instance, and we return the original node.
6486 -- If it is a compilation unit and the instance node has not been
6487 -- rewritten, then it is still the unit of the compilation. Finally, if
6488 -- a body is present, this is a parent of the main unit whose body has
6489 -- been compiled for inlining purposes, and the instantiation node has
6490 -- been rewritten with the instance body.
6492 -- Otherwise the instantiation node appears after the declaration. If
6493 -- the entity is a formal package, the declaration may have been
6494 -- rewritten as a generic declaration (in the case of a formal with box)
6495 -- or left as a formal package declaration if it has actuals, and is
6496 -- found with a forward search.
6498 if Nkind (Parent (Decl)) = N_Compilation_Unit then
6499 if Nkind (Decl) = N_Package_Declaration
6500 and then Present (Corresponding_Body (Decl))
6502 Decl := Unit_Declaration_Node (Corresponding_Body (Decl));
6505 if Nkind (Original_Node (Decl)) = N_Package_Instantiation then
6506 return Original_Node (Decl);
6508 return Unit (Parent (Decl));
6511 elsif Nkind (Decl) = N_Package_Declaration
6512 and then Nkind (Original_Node (Decl)) = N_Formal_Package_Declaration
6514 return Original_Node (Decl);
6517 Inst := Next (Decl);
6518 while Nkind (Inst) /= N_Package_Instantiation
6519 and then Nkind (Inst) /= N_Formal_Package_Declaration
6526 end Get_Package_Instantiation_Node;
6528 ------------------------
6529 -- Has_Been_Exchanged --
6530 ------------------------
6532 function Has_Been_Exchanged (E : Entity_Id) return Boolean is
6536 Next := First_Elmt (Exchanged_Views);
6537 while Present (Next) loop
6538 if Full_View (Node (Next)) = E then
6546 end Has_Been_Exchanged;
6552 function Hash (F : Entity_Id) return HTable_Range is
6554 return HTable_Range (F mod HTable_Size);
6557 ------------------------
6558 -- Hide_Current_Scope --
6559 ------------------------
6561 procedure Hide_Current_Scope is
6562 C : constant Entity_Id := Current_Scope;
6566 Set_Is_Hidden_Open_Scope (C);
6568 E := First_Entity (C);
6569 while Present (E) loop
6570 if Is_Immediately_Visible (E) then
6571 Set_Is_Immediately_Visible (E, False);
6572 Append_Elmt (E, Hidden_Entities);
6578 -- Make the scope name invisible as well. This is necessary, but might
6579 -- conflict with calls to Rtsfind later on, in case the scope is a
6580 -- predefined one. There is no clean solution to this problem, so for
6581 -- now we depend on the user not redefining Standard itself in one of
6582 -- the parent units.
6584 if Is_Immediately_Visible (C)
6585 and then C /= Standard_Standard
6587 Set_Is_Immediately_Visible (C, False);
6588 Append_Elmt (C, Hidden_Entities);
6591 end Hide_Current_Scope;
6597 procedure Init_Env is
6598 Saved : Instance_Env;
6601 Saved.Instantiated_Parent := Current_Instantiated_Parent;
6602 Saved.Exchanged_Views := Exchanged_Views;
6603 Saved.Hidden_Entities := Hidden_Entities;
6604 Saved.Current_Sem_Unit := Current_Sem_Unit;
6605 Saved.Parent_Unit_Visible := Parent_Unit_Visible;
6606 Saved.Instance_Parent_Unit := Instance_Parent_Unit;
6608 -- Save configuration switches. These may be reset if the unit is a
6609 -- predefined unit, and the current mode is not Ada 2005.
6611 Save_Opt_Config_Switches (Saved.Switches);
6613 Instance_Envs.Append (Saved);
6615 Exchanged_Views := New_Elmt_List;
6616 Hidden_Entities := New_Elmt_List;
6618 -- Make dummy entry for Instantiated parent. If generic unit is legal,
6619 -- this is set properly in Set_Instance_Env.
6621 Current_Instantiated_Parent :=
6622 (Current_Scope, Current_Scope, Assoc_Null);
6625 ------------------------------
6626 -- In_Same_Declarative_Part --
6627 ------------------------------
6629 function In_Same_Declarative_Part
6631 Inst : Node_Id) return Boolean
6633 Decls : constant Node_Id := Parent (F_Node);
6634 Nod : Node_Id := Parent (Inst);
6637 while Present (Nod) loop
6641 elsif Nkind (Nod) = N_Subprogram_Body
6642 or else Nkind (Nod) = N_Package_Body
6643 or else Nkind (Nod) = N_Task_Body
6644 or else Nkind (Nod) = N_Protected_Body
6645 or else Nkind (Nod) = N_Block_Statement
6649 elsif Nkind (Nod) = N_Subunit then
6650 Nod := Corresponding_Stub (Nod);
6652 elsif Nkind (Nod) = N_Compilation_Unit then
6655 Nod := Parent (Nod);
6660 end In_Same_Declarative_Part;
6662 ---------------------
6663 -- In_Main_Context --
6664 ---------------------
6666 function In_Main_Context (E : Entity_Id) return Boolean is
6672 if not Is_Compilation_Unit (E)
6673 or else Ekind (E) /= E_Package
6674 or else In_Private_Part (E)
6679 Context := Context_Items (Cunit (Main_Unit));
6681 Clause := First (Context);
6682 while Present (Clause) loop
6683 if Nkind (Clause) = N_With_Clause then
6684 Nam := Name (Clause);
6686 -- If the current scope is part of the context of the main unit,
6687 -- analysis of the corresponding with_clause is not complete, and
6688 -- the entity is not set. We use the Chars field directly, which
6689 -- might produce false positives in rare cases, but guarantees
6690 -- that we produce all the instance bodies we will need.
6692 if (Nkind (Nam) = N_Identifier
6693 and then Chars (Nam) = Chars (E))
6694 or else (Nkind (Nam) = N_Selected_Component
6695 and then Chars (Selector_Name (Nam)) = Chars (E))
6705 end In_Main_Context;
6707 ---------------------
6708 -- Inherit_Context --
6709 ---------------------
6711 procedure Inherit_Context (Gen_Decl : Node_Id; Inst : Node_Id) is
6712 Current_Context : List_Id;
6713 Current_Unit : Node_Id;
6718 if Nkind (Parent (Gen_Decl)) = N_Compilation_Unit then
6720 -- The inherited context is attached to the enclosing compilation
6721 -- unit. This is either the main unit, or the declaration for the
6722 -- main unit (in case the instantation appears within the package
6723 -- declaration and the main unit is its body).
6725 Current_Unit := Parent (Inst);
6726 while Present (Current_Unit)
6727 and then Nkind (Current_Unit) /= N_Compilation_Unit
6729 Current_Unit := Parent (Current_Unit);
6732 Current_Context := Context_Items (Current_Unit);
6734 Item := First (Context_Items (Parent (Gen_Decl)));
6735 while Present (Item) loop
6736 if Nkind (Item) = N_With_Clause then
6737 New_I := New_Copy (Item);
6738 Set_Implicit_With (New_I, True);
6739 Append (New_I, Current_Context);
6745 end Inherit_Context;
6751 procedure Initialize is
6753 Generic_Renamings.Init;
6756 Generic_Renamings_HTable.Reset;
6757 Circularity_Detected := False;
6758 Exchanged_Views := No_Elist;
6759 Hidden_Entities := No_Elist;
6762 ----------------------------
6763 -- Insert_After_Last_Decl --
6764 ----------------------------
6766 procedure Insert_After_Last_Decl (N : Node_Id; F_Node : Node_Id) is
6767 L : List_Id := List_Containing (N);
6768 P : constant Node_Id := Parent (L);
6771 if not Is_List_Member (F_Node) then
6772 if Nkind (P) = N_Package_Specification
6773 and then L = Visible_Declarations (P)
6774 and then Present (Private_Declarations (P))
6775 and then not Is_Empty_List (Private_Declarations (P))
6777 L := Private_Declarations (P);
6780 Insert_After (Last (L), F_Node);
6782 end Insert_After_Last_Decl;
6788 procedure Install_Body
6789 (Act_Body : Node_Id;
6794 Act_Id : constant Entity_Id := Corresponding_Spec (Act_Body);
6795 Act_Unit : constant Node_Id := Unit (Cunit (Get_Source_Unit (N)));
6796 Gen_Id : constant Entity_Id := Corresponding_Spec (Gen_Body);
6797 Par : constant Entity_Id := Scope (Gen_Id);
6798 Gen_Unit : constant Node_Id :=
6799 Unit (Cunit (Get_Source_Unit (Gen_Decl)));
6800 Orig_Body : Node_Id := Gen_Body;
6802 Body_Unit : Node_Id;
6804 Must_Delay : Boolean;
6806 function Enclosing_Subp (Id : Entity_Id) return Entity_Id;
6807 -- Find subprogram (if any) that encloses instance and/or generic body
6809 function True_Sloc (N : Node_Id) return Source_Ptr;
6810 -- If the instance is nested inside a generic unit, the Sloc of the
6811 -- instance indicates the place of the original definition, not the
6812 -- point of the current enclosing instance. Pending a better usage of
6813 -- Slocs to indicate instantiation places, we determine the place of
6814 -- origin of a node by finding the maximum sloc of any ancestor node.
6815 -- Why is this not equivalent to Top_Level_Location ???
6817 --------------------
6818 -- Enclosing_Subp --
6819 --------------------
6821 function Enclosing_Subp (Id : Entity_Id) return Entity_Id is
6822 Scop : Entity_Id := Scope (Id);
6825 while Scop /= Standard_Standard
6826 and then not Is_Overloadable (Scop)
6828 Scop := Scope (Scop);
6838 function True_Sloc (N : Node_Id) return Source_Ptr is
6845 while Present (N1) and then N1 /= Act_Unit loop
6846 if Sloc (N1) > Res then
6856 -- Start of processing for Install_Body
6859 -- If the body is a subunit, the freeze point is the corresponding
6860 -- stub in the current compilation, not the subunit itself.
6862 if Nkind (Parent (Gen_Body)) = N_Subunit then
6863 Orig_Body := Corresponding_Stub (Parent (Gen_Body));
6865 Orig_Body := Gen_Body;
6868 Body_Unit := Unit (Cunit (Get_Source_Unit (Orig_Body)));
6870 -- If the instantiation and the generic definition appear in the same
6871 -- package declaration, this is an early instantiation. If they appear
6872 -- in the same declarative part, it is an early instantiation only if
6873 -- the generic body appears textually later, and the generic body is
6874 -- also in the main unit.
6876 -- If instance is nested within a subprogram, and the generic body is
6877 -- not, the instance is delayed because the enclosing body is. If
6878 -- instance and body are within the same scope, or the same sub-
6879 -- program body, indicate explicitly that the instance is delayed.
6882 (Gen_Unit = Act_Unit
6883 and then ((Nkind (Gen_Unit) = N_Package_Declaration)
6884 or else Nkind (Gen_Unit) = N_Generic_Package_Declaration
6885 or else (Gen_Unit = Body_Unit
6886 and then True_Sloc (N) < Sloc (Orig_Body)))
6887 and then Is_In_Main_Unit (Gen_Unit)
6888 and then (Scope (Act_Id) = Scope (Gen_Id)
6890 Enclosing_Subp (Act_Id) = Enclosing_Subp (Gen_Id)));
6892 -- If this is an early instantiation, the freeze node is placed after
6893 -- the generic body. Otherwise, if the generic appears in an instance,
6894 -- we cannot freeze the current instance until the outer one is frozen.
6895 -- This is only relevant if the current instance is nested within some
6896 -- inner scope not itself within the outer instance. If this scope is
6897 -- a package body in the same declarative part as the outer instance,
6898 -- then that body needs to be frozen after the outer instance. Finally,
6899 -- if no delay is needed, we place the freeze node at the end of the
6900 -- current declarative part.
6902 if Expander_Active then
6903 Ensure_Freeze_Node (Act_Id);
6904 F_Node := Freeze_Node (Act_Id);
6907 Insert_After (Orig_Body, F_Node);
6909 elsif Is_Generic_Instance (Par)
6910 and then Present (Freeze_Node (Par))
6911 and then Scope (Act_Id) /= Par
6913 -- Freeze instance of inner generic after instance of enclosing
6916 if In_Same_Declarative_Part (Freeze_Node (Par), N) then
6917 Insert_After (Freeze_Node (Par), F_Node);
6919 -- Freeze package enclosing instance of inner generic after
6920 -- instance of enclosing generic.
6922 elsif Nkind (Parent (N)) = N_Package_Body
6923 and then In_Same_Declarative_Part (Freeze_Node (Par), Parent (N))
6927 Enclosing : constant Entity_Id :=
6928 Corresponding_Spec (Parent (N));
6931 Insert_After_Last_Decl (N, F_Node);
6932 Ensure_Freeze_Node (Enclosing);
6934 if not Is_List_Member (Freeze_Node (Enclosing)) then
6935 Insert_After (Freeze_Node (Par), Freeze_Node (Enclosing));
6940 Insert_After_Last_Decl (N, F_Node);
6944 Insert_After_Last_Decl (N, F_Node);
6948 Set_Is_Frozen (Act_Id);
6949 Insert_Before (N, Act_Body);
6950 Mark_Rewrite_Insertion (Act_Body);
6953 -----------------------------
6954 -- Install_Formal_Packages --
6955 -----------------------------
6957 procedure Install_Formal_Packages (Par : Entity_Id) is
6961 E := First_Entity (Par);
6962 while Present (E) loop
6963 if Ekind (E) = E_Package
6964 and then Nkind (Parent (E)) = N_Package_Renaming_Declaration
6966 -- If this is the renaming for the parent instance, done
6968 if Renamed_Object (E) = Par then
6971 -- The visibility of a formal of an enclosing generic is
6974 elsif Denotes_Formal_Package (E) then
6977 elsif Present (Associated_Formal_Package (E))
6978 and then Box_Present (Parent (Associated_Formal_Package (E)))
6980 Check_Generic_Actuals (Renamed_Object (E), True);
6981 Set_Is_Hidden (E, False);
6987 end Install_Formal_Packages;
6989 --------------------
6990 -- Install_Parent --
6991 --------------------
6993 procedure Install_Parent (P : Entity_Id; In_Body : Boolean := False) is
6994 Ancestors : constant Elist_Id := New_Elmt_List;
6995 S : constant Entity_Id := Current_Scope;
6996 Inst_Par : Entity_Id;
6997 First_Par : Entity_Id;
6998 Inst_Node : Node_Id;
6999 Gen_Par : Entity_Id;
7000 First_Gen : Entity_Id;
7003 procedure Install_Noninstance_Specs (Par : Entity_Id);
7004 -- Install the scopes of noninstance parent units ending with Par
7006 procedure Install_Spec (Par : Entity_Id);
7007 -- The child unit is within the declarative part of the parent, so
7008 -- the declarations within the parent are immediately visible.
7010 -------------------------------
7011 -- Install_Noninstance_Specs --
7012 -------------------------------
7014 procedure Install_Noninstance_Specs (Par : Entity_Id) is
7017 and then Par /= Standard_Standard
7018 and then not In_Open_Scopes (Par)
7020 Install_Noninstance_Specs (Scope (Par));
7023 end Install_Noninstance_Specs;
7029 procedure Install_Spec (Par : Entity_Id) is
7030 Spec : constant Node_Id :=
7031 Specification (Unit_Declaration_Node (Par));
7034 -- If this parent of the child instance is a top-level unit,
7035 -- then record the unit and its visibility for later resetting
7036 -- in Remove_Parent. We exclude units that are generic instances,
7037 -- as we only want to record this information for the ultimate
7038 -- top-level noninstance parent (is that always correct???).
7040 if Scope (Par) = Standard_Standard
7041 and then not Is_Generic_Instance (Par)
7043 Parent_Unit_Visible := Is_Immediately_Visible (Par);
7044 Instance_Parent_Unit := Par;
7047 -- Open the parent scope and make it and its declarations visible.
7048 -- If this point is not within a body, then only the visible
7049 -- declarations should be made visible, and installation of the
7050 -- private declarations is deferred until the appropriate point
7051 -- within analysis of the spec being instantiated (see the handling
7052 -- of parent visibility in Analyze_Package_Specification). This is
7053 -- relaxed in the case where the parent unit is Ada.Tags, to avoid
7054 -- private view problems that occur when compiling instantiations of
7055 -- a generic child of that package (Generic_Dispatching_Constructor).
7056 -- If the instance freezes a tagged type, inlinings of operations
7057 -- from Ada.Tags may need the full view of type Tag. If inlining took
7058 -- proper account of establishing visibility of inlined subprograms'
7059 -- parents then it should be possible to remove this
7060 -- special check. ???
7063 Set_Is_Immediately_Visible (Par);
7064 Install_Visible_Declarations (Par);
7065 Set_Use (Visible_Declarations (Spec));
7067 if In_Body or else Is_RTU (Par, Ada_Tags) then
7068 Install_Private_Declarations (Par);
7069 Set_Use (Private_Declarations (Spec));
7073 -- Start of processing for Install_Parent
7076 -- We need to install the parent instance to compile the instantiation
7077 -- of the child, but the child instance must appear in the current
7078 -- scope. Given that we cannot place the parent above the current scope
7079 -- in the scope stack, we duplicate the current scope and unstack both
7080 -- after the instantiation is complete.
7082 -- If the parent is itself the instantiation of a child unit, we must
7083 -- also stack the instantiation of its parent, and so on. Each such
7084 -- ancestor is the prefix of the name in a prior instantiation.
7086 -- If this is a nested instance, the parent unit itself resolves to
7087 -- a renaming of the parent instance, whose declaration we need.
7089 -- Finally, the parent may be a generic (not an instance) when the
7090 -- child unit appears as a formal package.
7094 if Present (Renamed_Entity (Inst_Par)) then
7095 Inst_Par := Renamed_Entity (Inst_Par);
7098 First_Par := Inst_Par;
7101 Generic_Parent (Specification (Unit_Declaration_Node (Inst_Par)));
7103 First_Gen := Gen_Par;
7105 while Present (Gen_Par)
7106 and then Is_Child_Unit (Gen_Par)
7108 -- Load grandparent instance as well
7110 Inst_Node := Get_Package_Instantiation_Node (Inst_Par);
7112 if Nkind (Name (Inst_Node)) = N_Expanded_Name then
7113 Inst_Par := Entity (Prefix (Name (Inst_Node)));
7115 if Present (Renamed_Entity (Inst_Par)) then
7116 Inst_Par := Renamed_Entity (Inst_Par);
7121 (Specification (Unit_Declaration_Node (Inst_Par)));
7123 if Present (Gen_Par) then
7124 Prepend_Elmt (Inst_Par, Ancestors);
7127 -- Parent is not the name of an instantiation
7129 Install_Noninstance_Specs (Inst_Par);
7141 if Present (First_Gen) then
7142 Append_Elmt (First_Par, Ancestors);
7145 Install_Noninstance_Specs (First_Par);
7148 if not Is_Empty_Elmt_List (Ancestors) then
7149 Elmt := First_Elmt (Ancestors);
7151 while Present (Elmt) loop
7152 Install_Spec (Node (Elmt));
7153 Install_Formal_Packages (Node (Elmt));
7164 --------------------------------
7165 -- Instantiate_Formal_Package --
7166 --------------------------------
7168 function Instantiate_Formal_Package
7171 Analyzed_Formal : Node_Id) return List_Id
7173 Loc : constant Source_Ptr := Sloc (Actual);
7174 Actual_Pack : Entity_Id;
7175 Formal_Pack : Entity_Id;
7176 Gen_Parent : Entity_Id;
7179 Parent_Spec : Node_Id;
7181 procedure Find_Matching_Actual
7183 Act : in out Entity_Id);
7184 -- We need to associate each formal entity in the formal package
7185 -- with the corresponding entity in the actual package. The actual
7186 -- package has been analyzed and possibly expanded, and as a result
7187 -- there is no one-to-one correspondence between the two lists (for
7188 -- example, the actual may include subtypes, itypes, and inherited
7189 -- primitive operations, interspersed among the renaming declarations
7190 -- for the actuals) . We retrieve the corresponding actual by name
7191 -- because each actual has the same name as the formal, and they do
7192 -- appear in the same order.
7194 function Get_Formal_Entity (N : Node_Id) return Entity_Id;
7195 -- Retrieve entity of defining entity of generic formal parameter.
7196 -- Only the declarations of formals need to be considered when
7197 -- linking them to actuals, but the declarative list may include
7198 -- internal entities generated during analysis, and those are ignored.
7200 procedure Match_Formal_Entity
7201 (Formal_Node : Node_Id;
7202 Formal_Ent : Entity_Id;
7203 Actual_Ent : Entity_Id);
7204 -- Associates the formal entity with the actual. In the case
7205 -- where Formal_Ent is a formal package, this procedure iterates
7206 -- through all of its formals and enters associations betwen the
7207 -- actuals occurring in the formal package's corresponding actual
7208 -- package (given by Actual_Ent) and the formal package's formal
7209 -- parameters. This procedure recurses if any of the parameters is
7210 -- itself a package.
7212 function Is_Instance_Of
7213 (Act_Spec : Entity_Id;
7214 Gen_Anc : Entity_Id) return Boolean;
7215 -- The actual can be an instantiation of a generic within another
7216 -- instance, in which case there is no direct link from it to the
7217 -- original generic ancestor. In that case, we recognize that the
7218 -- ultimate ancestor is the same by examining names and scopes.
7220 procedure Map_Entities (Form : Entity_Id; Act : Entity_Id);
7221 -- Within the generic part, entities in the formal package are
7222 -- visible. To validate subsequent type declarations, indicate
7223 -- the correspondence betwen the entities in the analyzed formal,
7224 -- and the entities in the actual package. There are three packages
7225 -- involved in the instantiation of a formal package: the parent
7226 -- generic P1 which appears in the generic declaration, the fake
7227 -- instantiation P2 which appears in the analyzed generic, and whose
7228 -- visible entities may be used in subsequent formals, and the actual
7229 -- P3 in the instance. To validate subsequent formals, me indicate
7230 -- that the entities in P2 are mapped into those of P3. The mapping of
7231 -- entities has to be done recursively for nested packages.
7233 procedure Process_Nested_Formal (Formal : Entity_Id);
7234 -- If the current formal is declared with a box, its own formals are
7235 -- visible in the instance, as they were in the generic, and their
7236 -- Hidden flag must be reset. If some of these formals are themselves
7237 -- packages declared with a box, the processing must be recursive.
7239 --------------------------
7240 -- Find_Matching_Actual --
7241 --------------------------
7243 procedure Find_Matching_Actual
7245 Act : in out Entity_Id)
7247 Formal_Ent : Entity_Id;
7250 case Nkind (Original_Node (F)) is
7251 when N_Formal_Object_Declaration |
7252 N_Formal_Type_Declaration =>
7253 Formal_Ent := Defining_Identifier (F);
7255 while Chars (Act) /= Chars (Formal_Ent) loop
7259 when N_Formal_Subprogram_Declaration |
7260 N_Formal_Package_Declaration |
7261 N_Package_Declaration |
7262 N_Generic_Package_Declaration =>
7263 Formal_Ent := Defining_Entity (F);
7265 while Chars (Act) /= Chars (Formal_Ent) loop
7270 raise Program_Error;
7272 end Find_Matching_Actual;
7274 -------------------------
7275 -- Match_Formal_Entity --
7276 -------------------------
7278 procedure Match_Formal_Entity
7279 (Formal_Node : Node_Id;
7280 Formal_Ent : Entity_Id;
7281 Actual_Ent : Entity_Id)
7283 Act_Pkg : Entity_Id;
7286 Set_Instance_Of (Formal_Ent, Actual_Ent);
7288 if Ekind (Actual_Ent) = E_Package then
7290 -- Record associations for each parameter
7292 Act_Pkg := Actual_Ent;
7295 A_Ent : Entity_Id := First_Entity (Act_Pkg);
7304 -- Retrieve the actual given in the formal package declaration
7306 Actual := Entity (Name (Original_Node (Formal_Node)));
7308 -- The actual in the formal package declaration may be a
7309 -- renamed generic package, in which case we want to retrieve
7310 -- the original generic in order to traverse its formal part.
7312 if Present (Renamed_Entity (Actual)) then
7313 Gen_Decl := Unit_Declaration_Node (Renamed_Entity (Actual));
7315 Gen_Decl := Unit_Declaration_Node (Actual);
7318 Formals := Generic_Formal_Declarations (Gen_Decl);
7320 if Present (Formals) then
7321 F_Node := First_Non_Pragma (Formals);
7326 while Present (A_Ent)
7327 and then Present (F_Node)
7328 and then A_Ent /= First_Private_Entity (Act_Pkg)
7330 F_Ent := Get_Formal_Entity (F_Node);
7332 if Present (F_Ent) then
7334 -- This is a formal of the original package. Record
7335 -- association and recurse.
7337 Find_Matching_Actual (F_Node, A_Ent);
7338 Match_Formal_Entity (F_Node, F_Ent, A_Ent);
7339 Next_Entity (A_Ent);
7342 Next_Non_Pragma (F_Node);
7346 end Match_Formal_Entity;
7348 -----------------------
7349 -- Get_Formal_Entity --
7350 -----------------------
7352 function Get_Formal_Entity (N : Node_Id) return Entity_Id is
7353 Kind : constant Node_Kind := Nkind (Original_Node (N));
7356 when N_Formal_Object_Declaration =>
7357 return Defining_Identifier (N);
7359 when N_Formal_Type_Declaration =>
7360 return Defining_Identifier (N);
7362 when N_Formal_Subprogram_Declaration =>
7363 return Defining_Unit_Name (Specification (N));
7365 when N_Formal_Package_Declaration =>
7366 return Defining_Identifier (Original_Node (N));
7368 when N_Generic_Package_Declaration =>
7369 return Defining_Identifier (Original_Node (N));
7371 -- All other declarations are introduced by semantic analysis and
7372 -- have no match in the actual.
7377 end Get_Formal_Entity;
7379 --------------------
7380 -- Is_Instance_Of --
7381 --------------------
7383 function Is_Instance_Of
7384 (Act_Spec : Entity_Id;
7385 Gen_Anc : Entity_Id) return Boolean
7387 Gen_Par : constant Entity_Id := Generic_Parent (Act_Spec);
7390 if No (Gen_Par) then
7393 -- Simplest case: the generic parent of the actual is the formal
7395 elsif Gen_Par = Gen_Anc then
7398 elsif Chars (Gen_Par) /= Chars (Gen_Anc) then
7401 -- The actual may be obtained through several instantiations. Its
7402 -- scope must itself be an instance of a generic declared in the
7403 -- same scope as the formal. Any other case is detected above.
7405 elsif not Is_Generic_Instance (Scope (Gen_Par)) then
7409 return Generic_Parent (Parent (Scope (Gen_Par))) = Scope (Gen_Anc);
7417 procedure Map_Entities (Form : Entity_Id; Act : Entity_Id) is
7422 Set_Instance_Of (Form, Act);
7424 -- Traverse formal and actual package to map the corresponding
7425 -- entities. We skip over internal entities that may be generated
7426 -- during semantic analysis, and find the matching entities by
7427 -- name, given that they must appear in the same order.
7429 E1 := First_Entity (Form);
7430 E2 := First_Entity (Act);
7432 and then E1 /= First_Private_Entity (Form)
7434 -- Could this test be a single condition???
7435 -- Seems like it could, and isn't FPE (Form) a constant anyway???
7437 if not Is_Internal (E1)
7438 and then Present (Parent (E1))
7439 and then not Is_Class_Wide_Type (E1)
7440 and then not Is_Internal_Name (Chars (E1))
7443 and then Chars (E2) /= Chars (E1)
7451 Set_Instance_Of (E1, E2);
7454 and then Is_Tagged_Type (E2)
7457 (Class_Wide_Type (E1), Class_Wide_Type (E2));
7460 if Ekind (E1) = E_Package
7461 and then No (Renamed_Object (E1))
7463 Map_Entities (E1, E2);
7472 ---------------------------
7473 -- Process_Nested_Formal --
7474 ---------------------------
7476 procedure Process_Nested_Formal (Formal : Entity_Id) is
7480 if Present (Associated_Formal_Package (Formal))
7481 and then Box_Present (Parent (Associated_Formal_Package (Formal)))
7483 Ent := First_Entity (Formal);
7484 while Present (Ent) loop
7485 Set_Is_Hidden (Ent, False);
7486 Set_Is_Visible_Formal (Ent);
7487 Set_Is_Potentially_Use_Visible
7488 (Ent, Is_Potentially_Use_Visible (Formal));
7490 if Ekind (Ent) = E_Package then
7491 exit when Renamed_Entity (Ent) = Renamed_Entity (Formal);
7492 Process_Nested_Formal (Ent);
7498 end Process_Nested_Formal;
7500 -- Start of processing for Instantiate_Formal_Package
7505 if not Is_Entity_Name (Actual)
7506 or else Ekind (Entity (Actual)) /= E_Package
7509 ("expect package instance to instantiate formal", Actual);
7510 Abandon_Instantiation (Actual);
7511 raise Program_Error;
7514 Actual_Pack := Entity (Actual);
7515 Set_Is_Instantiated (Actual_Pack);
7517 -- The actual may be a renamed package, or an outer generic formal
7518 -- package whose instantiation is converted into a renaming.
7520 if Present (Renamed_Object (Actual_Pack)) then
7521 Actual_Pack := Renamed_Object (Actual_Pack);
7524 if Nkind (Analyzed_Formal) = N_Formal_Package_Declaration then
7525 Gen_Parent := Get_Instance_Of (Entity (Name (Analyzed_Formal)));
7526 Formal_Pack := Defining_Identifier (Analyzed_Formal);
7529 Generic_Parent (Specification (Analyzed_Formal));
7531 Defining_Unit_Name (Specification (Analyzed_Formal));
7534 if Nkind (Parent (Actual_Pack)) = N_Defining_Program_Unit_Name then
7535 Parent_Spec := Specification (Unit_Declaration_Node (Actual_Pack));
7537 Parent_Spec := Parent (Actual_Pack);
7540 if Gen_Parent = Any_Id then
7542 ("previous error in declaration of formal package", Actual);
7543 Abandon_Instantiation (Actual);
7546 Is_Instance_Of (Parent_Spec, Get_Instance_Of (Gen_Parent))
7552 ("actual parameter must be instance of&", Actual, Gen_Parent);
7553 Abandon_Instantiation (Actual);
7556 Set_Instance_Of (Defining_Identifier (Formal), Actual_Pack);
7557 Map_Entities (Formal_Pack, Actual_Pack);
7560 Make_Package_Renaming_Declaration (Loc,
7561 Defining_Unit_Name => New_Copy (Defining_Identifier (Formal)),
7562 Name => New_Reference_To (Actual_Pack, Loc));
7564 Set_Associated_Formal_Package (Defining_Unit_Name (Nod),
7565 Defining_Identifier (Formal));
7566 Decls := New_List (Nod);
7568 -- If the formal F has a box, then the generic declarations are
7569 -- visible in the generic G. In an instance of G, the corresponding
7570 -- entities in the actual for F (which are the actuals for the
7571 -- instantiation of the generic that F denotes) must also be made
7572 -- visible for analysis of the current instance. On exit from the
7573 -- current instance, those entities are made private again. If the
7574 -- actual is currently in use, these entities are also use-visible.
7576 -- The loop through the actual entities also steps through the formal
7577 -- entities and enters associations from formals to actuals into the
7578 -- renaming map. This is necessary to properly handle checking of
7579 -- actual parameter associations for later formals that depend on
7580 -- actuals declared in the formal package.
7582 -- In Ada 2005, partial parametrization requires that we make visible
7583 -- the actuals corresponding to formals that were defaulted in the
7584 -- formal package. There formals are identified because they remain
7585 -- formal generics within the formal package, rather than being
7586 -- renamings of the actuals supplied.
7589 Gen_Decl : constant Node_Id :=
7590 Unit_Declaration_Node (Gen_Parent);
7591 Formals : constant List_Id :=
7592 Generic_Formal_Declarations (Gen_Decl);
7594 Actual_Ent : Entity_Id;
7595 Actual_Of_Formal : Node_Id;
7596 Formal_Node : Node_Id;
7597 Formal_Ent : Entity_Id;
7600 if Present (Formals) then
7601 Formal_Node := First_Non_Pragma (Formals);
7603 Formal_Node := Empty;
7606 Actual_Ent := First_Entity (Actual_Pack);
7608 First (Visible_Declarations (Specification (Analyzed_Formal)));
7609 while Present (Actual_Ent)
7610 and then Actual_Ent /= First_Private_Entity (Actual_Pack)
7612 if Present (Formal_Node) then
7613 Formal_Ent := Get_Formal_Entity (Formal_Node);
7615 if Present (Formal_Ent) then
7616 Find_Matching_Actual (Formal_Node, Actual_Ent);
7618 (Formal_Node, Formal_Ent, Actual_Ent);
7620 -- We iterate at the same time over the actuals of the
7621 -- local package created for the formal, to determine
7622 -- which one of the formals of the original generic were
7623 -- defaulted in the formal. The corresponding actual
7624 -- entities are visible in the enclosing instance.
7626 if Box_Present (Formal)
7628 (Present (Actual_Of_Formal)
7631 (Get_Formal_Entity (Actual_Of_Formal)))
7633 Set_Is_Hidden (Actual_Ent, False);
7634 Set_Is_Visible_Formal (Actual_Ent);
7635 Set_Is_Potentially_Use_Visible
7636 (Actual_Ent, In_Use (Actual_Pack));
7638 if Ekind (Actual_Ent) = E_Package then
7639 Process_Nested_Formal (Actual_Ent);
7643 Set_Is_Hidden (Actual_Ent);
7644 Set_Is_Potentially_Use_Visible (Actual_Ent, False);
7648 Next_Non_Pragma (Formal_Node);
7649 Next (Actual_Of_Formal);
7652 -- No further formals to match, but the generic part may
7653 -- contain inherited operation that are not hidden in the
7654 -- enclosing instance.
7656 Next_Entity (Actual_Ent);
7660 -- Inherited subprograms generated by formal derived types are
7661 -- also visible if the types are.
7663 Actual_Ent := First_Entity (Actual_Pack);
7664 while Present (Actual_Ent)
7665 and then Actual_Ent /= First_Private_Entity (Actual_Pack)
7667 if Is_Overloadable (Actual_Ent)
7669 Nkind (Parent (Actual_Ent)) = N_Subtype_Declaration
7671 not Is_Hidden (Defining_Identifier (Parent (Actual_Ent)))
7673 Set_Is_Hidden (Actual_Ent, False);
7674 Set_Is_Potentially_Use_Visible
7675 (Actual_Ent, In_Use (Actual_Pack));
7678 Next_Entity (Actual_Ent);
7682 -- If the formal is not declared with a box, reanalyze it as an
7683 -- abbreviated instantiation, to verify the matching rules of 12.7.
7684 -- The actual checks are performed after the generic associations
7685 -- have been analyzed, to guarantee the same visibility for this
7686 -- instantiation and for the actuals.
7688 -- In Ada 2005, the generic associations for the formal can include
7689 -- defaulted parameters. These are ignored during check. This
7690 -- internal instantiation is removed from the tree after conformance
7691 -- checking, because it contains formal declarations for those
7692 -- defaulted parameters, and those should not reach the back-end.
7694 if not Box_Present (Formal) then
7696 I_Pack : constant Entity_Id :=
7697 Make_Defining_Identifier (Sloc (Actual),
7698 Chars => New_Internal_Name ('P'));
7701 Set_Is_Internal (I_Pack);
7704 Make_Package_Instantiation (Sloc (Actual),
7705 Defining_Unit_Name => I_Pack,
7708 (Get_Instance_Of (Gen_Parent), Sloc (Actual)),
7709 Generic_Associations =>
7710 Generic_Associations (Formal)));
7716 end Instantiate_Formal_Package;
7718 -----------------------------------
7719 -- Instantiate_Formal_Subprogram --
7720 -----------------------------------
7722 function Instantiate_Formal_Subprogram
7725 Analyzed_Formal : Node_Id) return Node_Id
7728 Formal_Sub : constant Entity_Id :=
7729 Defining_Unit_Name (Specification (Formal));
7730 Analyzed_S : constant Entity_Id :=
7731 Defining_Unit_Name (Specification (Analyzed_Formal));
7732 Decl_Node : Node_Id;
7736 function From_Parent_Scope (Subp : Entity_Id) return Boolean;
7737 -- If the generic is a child unit, the parent has been installed on the
7738 -- scope stack, but a default subprogram cannot resolve to something on
7739 -- the parent because that parent is not really part of the visible
7740 -- context (it is there to resolve explicit local entities). If the
7741 -- default has resolved in this way, we remove the entity from
7742 -- immediate visibility and analyze the node again to emit an error
7743 -- message or find another visible candidate.
7745 procedure Valid_Actual_Subprogram (Act : Node_Id);
7746 -- Perform legality check and raise exception on failure
7748 -----------------------
7749 -- From_Parent_Scope --
7750 -----------------------
7752 function From_Parent_Scope (Subp : Entity_Id) return Boolean is
7753 Gen_Scope : Node_Id;
7756 Gen_Scope := Scope (Analyzed_S);
7757 while Present (Gen_Scope)
7758 and then Is_Child_Unit (Gen_Scope)
7760 if Scope (Subp) = Scope (Gen_Scope) then
7764 Gen_Scope := Scope (Gen_Scope);
7768 end From_Parent_Scope;
7770 -----------------------------
7771 -- Valid_Actual_Subprogram --
7772 -----------------------------
7774 procedure Valid_Actual_Subprogram (Act : Node_Id) is
7778 if Is_Entity_Name (Act) then
7779 Act_E := Entity (Act);
7781 elsif Nkind (Act) = N_Selected_Component
7782 and then Is_Entity_Name (Selector_Name (Act))
7784 Act_E := Entity (Selector_Name (Act));
7790 if (Present (Act_E) and then Is_Overloadable (Act_E))
7791 or else Nkind (Act) = N_Attribute_Reference
7792 or else Nkind (Act) = N_Indexed_Component
7793 or else Nkind (Act) = N_Character_Literal
7794 or else Nkind (Act) = N_Explicit_Dereference
7800 ("expect subprogram or entry name in instantiation of&",
7801 Instantiation_Node, Formal_Sub);
7802 Abandon_Instantiation (Instantiation_Node);
7804 end Valid_Actual_Subprogram;
7806 -- Start of processing for Instantiate_Formal_Subprogram
7809 New_Spec := New_Copy_Tree (Specification (Formal));
7811 -- The tree copy has created the proper instantiation sloc for the
7812 -- new specification. Use this location for all other constructed
7815 Loc := Sloc (Defining_Unit_Name (New_Spec));
7817 -- Create new entity for the actual (New_Copy_Tree does not)
7819 Set_Defining_Unit_Name
7820 (New_Spec, Make_Defining_Identifier (Loc, Chars (Formal_Sub)));
7822 -- Create new entities for the each of the formals in the
7823 -- specification of the renaming declaration built for the actual.
7825 if Present (Parameter_Specifications (New_Spec)) then
7829 F := First (Parameter_Specifications (New_Spec));
7830 while Present (F) loop
7831 Set_Defining_Identifier (F,
7832 Make_Defining_Identifier (Loc,
7833 Chars => Chars (Defining_Identifier (F))));
7839 -- Find entity of actual. If the actual is an attribute reference, it
7840 -- cannot be resolved here (its formal is missing) but is handled
7841 -- instead in Attribute_Renaming. If the actual is overloaded, it is
7842 -- fully resolved subsequently, when the renaming declaration for the
7843 -- formal is analyzed. If it is an explicit dereference, resolve the
7844 -- prefix but not the actual itself, to prevent interpretation as call.
7846 if Present (Actual) then
7847 Loc := Sloc (Actual);
7848 Set_Sloc (New_Spec, Loc);
7850 if Nkind (Actual) = N_Operator_Symbol then
7851 Find_Direct_Name (Actual);
7853 elsif Nkind (Actual) = N_Explicit_Dereference then
7854 Analyze (Prefix (Actual));
7856 elsif Nkind (Actual) /= N_Attribute_Reference then
7860 Valid_Actual_Subprogram (Actual);
7863 elsif Present (Default_Name (Formal)) then
7864 if Nkind (Default_Name (Formal)) /= N_Attribute_Reference
7865 and then Nkind (Default_Name (Formal)) /= N_Selected_Component
7866 and then Nkind (Default_Name (Formal)) /= N_Indexed_Component
7867 and then Nkind (Default_Name (Formal)) /= N_Character_Literal
7868 and then Present (Entity (Default_Name (Formal)))
7870 Nam := New_Occurrence_Of (Entity (Default_Name (Formal)), Loc);
7872 Nam := New_Copy (Default_Name (Formal));
7873 Set_Sloc (Nam, Loc);
7876 elsif Box_Present (Formal) then
7878 -- Actual is resolved at the point of instantiation. Create an
7879 -- identifier or operator with the same name as the formal.
7881 if Nkind (Formal_Sub) = N_Defining_Operator_Symbol then
7882 Nam := Make_Operator_Symbol (Loc,
7883 Chars => Chars (Formal_Sub),
7884 Strval => No_String);
7886 Nam := Make_Identifier (Loc, Chars (Formal_Sub));
7889 elsif Nkind (Specification (Formal)) = N_Procedure_Specification
7890 and then Null_Present (Specification (Formal))
7892 -- Generate null body for procedure, for use in the instance
7895 Make_Subprogram_Body (Loc,
7896 Specification => New_Spec,
7897 Declarations => New_List,
7898 Handled_Statement_Sequence =>
7899 Make_Handled_Sequence_Of_Statements (Loc,
7900 Statements => New_List (Make_Null_Statement (Loc))));
7902 Set_Is_Intrinsic_Subprogram (Defining_Unit_Name (New_Spec));
7906 Error_Msg_Sloc := Sloc (Scope (Analyzed_S));
7908 ("missing actual&", Instantiation_Node, Formal_Sub);
7910 ("\in instantiation of & declared#",
7911 Instantiation_Node, Scope (Analyzed_S));
7912 Abandon_Instantiation (Instantiation_Node);
7916 Make_Subprogram_Renaming_Declaration (Loc,
7917 Specification => New_Spec,
7920 -- If we do not have an actual and the formal specified <> then set to
7921 -- get proper default.
7923 if No (Actual) and then Box_Present (Formal) then
7924 Set_From_Default (Decl_Node);
7927 -- Gather possible interpretations for the actual before analyzing the
7928 -- instance. If overloaded, it will be resolved when analyzing the
7929 -- renaming declaration.
7931 if Box_Present (Formal)
7932 and then No (Actual)
7936 if Is_Child_Unit (Scope (Analyzed_S))
7937 and then Present (Entity (Nam))
7939 if not Is_Overloaded (Nam) then
7941 if From_Parent_Scope (Entity (Nam)) then
7942 Set_Is_Immediately_Visible (Entity (Nam), False);
7943 Set_Entity (Nam, Empty);
7944 Set_Etype (Nam, Empty);
7948 Set_Is_Immediately_Visible (Entity (Nam));
7957 Get_First_Interp (Nam, I, It);
7959 while Present (It.Nam) loop
7960 if From_Parent_Scope (It.Nam) then
7964 Get_Next_Interp (I, It);
7971 -- The generic instantiation freezes the actual. This can only be done
7972 -- once the actual is resolved, in the analysis of the renaming
7973 -- declaration. To make the formal subprogram entity available, we set
7974 -- Corresponding_Formal_Spec to point to the formal subprogram entity.
7975 -- This is also needed in Analyze_Subprogram_Renaming for the processing
7976 -- of formal abstract subprograms.
7978 Set_Corresponding_Formal_Spec (Decl_Node, Analyzed_S);
7980 -- We cannot analyze the renaming declaration, and thus find the actual,
7981 -- until all the actuals are assembled in the instance. For subsequent
7982 -- checks of other actuals, indicate the node that will hold the
7983 -- instance of this formal.
7985 Set_Instance_Of (Analyzed_S, Nam);
7987 if Nkind (Actual) = N_Selected_Component
7988 and then Is_Task_Type (Etype (Prefix (Actual)))
7989 and then not Is_Frozen (Etype (Prefix (Actual)))
7991 -- The renaming declaration will create a body, which must appear
7992 -- outside of the instantiation, We move the renaming declaration
7993 -- out of the instance, and create an additional renaming inside,
7994 -- to prevent freezing anomalies.
7997 Anon_Id : constant Entity_Id :=
7998 Make_Defining_Identifier
7999 (Loc, New_Internal_Name ('E'));
8001 Set_Defining_Unit_Name (New_Spec, Anon_Id);
8002 Insert_Before (Instantiation_Node, Decl_Node);
8003 Analyze (Decl_Node);
8005 -- Now create renaming within the instance
8008 Make_Subprogram_Renaming_Declaration (Loc,
8009 Specification => New_Copy_Tree (New_Spec),
8010 Name => New_Occurrence_Of (Anon_Id, Loc));
8012 Set_Defining_Unit_Name (Specification (Decl_Node),
8013 Make_Defining_Identifier (Loc, Chars (Formal_Sub)));
8018 end Instantiate_Formal_Subprogram;
8020 ------------------------
8021 -- Instantiate_Object --
8022 ------------------------
8024 function Instantiate_Object
8027 Analyzed_Formal : Node_Id) return List_Id
8029 Acc_Def : Node_Id := Empty;
8030 Act_Assoc : constant Node_Id := Parent (Actual);
8031 Actual_Decl : Node_Id := Empty;
8032 Formal_Id : constant Entity_Id := Defining_Identifier (Formal);
8033 Decl_Node : Node_Id;
8036 List : constant List_Id := New_List;
8037 Loc : constant Source_Ptr := Sloc (Actual);
8038 Orig_Ftyp : constant Entity_Id :=
8039 Etype (Defining_Identifier (Analyzed_Formal));
8040 Subt_Decl : Node_Id := Empty;
8041 Subt_Mark : Node_Id := Empty;
8044 if Present (Subtype_Mark (Formal)) then
8045 Subt_Mark := Subtype_Mark (Formal);
8047 Check_Access_Definition (Formal);
8048 Acc_Def := Access_Definition (Formal);
8051 -- Sloc for error message on missing actual
8053 Error_Msg_Sloc := Sloc (Scope (Defining_Identifier (Analyzed_Formal)));
8055 if Get_Instance_Of (Formal_Id) /= Formal_Id then
8056 Error_Msg_N ("duplicate instantiation of generic parameter", Actual);
8059 Set_Parent (List, Parent (Actual));
8063 if Out_Present (Formal) then
8065 -- An IN OUT generic actual must be a name. The instantiation is a
8066 -- renaming declaration. The actual is the name being renamed. We
8067 -- use the actual directly, rather than a copy, because it is not
8068 -- used further in the list of actuals, and because a copy or a use
8069 -- of relocate_node is incorrect if the instance is nested within a
8070 -- generic. In order to simplify ASIS searches, the Generic_Parent
8071 -- field links the declaration to the generic association.
8076 Instantiation_Node, Formal_Id);
8078 ("\in instantiation of & declared#",
8080 Scope (Defining_Identifier (Analyzed_Formal)));
8081 Abandon_Instantiation (Instantiation_Node);
8084 if Present (Subt_Mark) then
8086 Make_Object_Renaming_Declaration (Loc,
8087 Defining_Identifier => New_Copy (Formal_Id),
8088 Subtype_Mark => New_Copy_Tree (Subt_Mark),
8091 else pragma Assert (Present (Acc_Def));
8093 Make_Object_Renaming_Declaration (Loc,
8094 Defining_Identifier => New_Copy (Formal_Id),
8095 Access_Definition => New_Copy_Tree (Acc_Def),
8099 Set_Corresponding_Generic_Association (Decl_Node, Act_Assoc);
8101 -- The analysis of the actual may produce insert_action nodes, so
8102 -- the declaration must have a context in which to attach them.
8104 Append (Decl_Node, List);
8107 -- Return if the analysis of the actual reported some error
8109 if Etype (Actual) = Any_Type then
8113 -- This check is performed here because Analyze_Object_Renaming will
8114 -- not check it when Comes_From_Source is False. Note though that the
8115 -- check for the actual being the name of an object will be performed
8116 -- in Analyze_Object_Renaming.
8118 if Is_Object_Reference (Actual)
8119 and then Is_Dependent_Component_Of_Mutable_Object (Actual)
8122 ("illegal discriminant-dependent component for in out parameter",
8126 -- The actual has to be resolved in order to check that it is a
8127 -- variable (due to cases such as F(1), where F returns
8128 -- access to an array, and for overloaded prefixes).
8131 Get_Instance_Of (Etype (Defining_Identifier (Analyzed_Formal)));
8133 if Is_Private_Type (Ftyp)
8134 and then not Is_Private_Type (Etype (Actual))
8135 and then (Base_Type (Full_View (Ftyp)) = Base_Type (Etype (Actual))
8136 or else Base_Type (Etype (Actual)) = Ftyp)
8138 -- If the actual has the type of the full view of the formal, or
8139 -- else a non-private subtype of the formal, then the visibility
8140 -- of the formal type has changed. Add to the actuals a subtype
8141 -- declaration that will force the exchange of views in the body
8142 -- of the instance as well.
8145 Make_Subtype_Declaration (Loc,
8146 Defining_Identifier =>
8147 Make_Defining_Identifier (Loc, New_Internal_Name ('P')),
8148 Subtype_Indication => New_Occurrence_Of (Ftyp, Loc));
8150 Prepend (Subt_Decl, List);
8152 Prepend_Elmt (Full_View (Ftyp), Exchanged_Views);
8153 Exchange_Declarations (Ftyp);
8156 Resolve (Actual, Ftyp);
8158 if not Is_Variable (Actual) or else Paren_Count (Actual) > 0 then
8160 ("actual for& must be a variable", Actual, Formal_Id);
8162 elsif Base_Type (Ftyp) /= Base_Type (Etype (Actual)) then
8164 -- Ada 2005 (AI-423): For a generic formal object of mode in out,
8165 -- the type of the actual shall resolve to a specific anonymous
8168 if Ada_Version < Ada_05
8170 Ekind (Base_Type (Ftyp)) /=
8171 E_Anonymous_Access_Type
8173 Ekind (Base_Type (Etype (Actual))) /=
8174 E_Anonymous_Access_Type
8176 Error_Msg_NE ("type of actual does not match type of&",
8181 Note_Possible_Modification (Actual);
8183 -- Check for instantiation of atomic/volatile actual for
8184 -- non-atomic/volatile formal (RM C.6 (12)).
8186 if Is_Atomic_Object (Actual)
8187 and then not Is_Atomic (Orig_Ftyp)
8190 ("cannot instantiate non-atomic formal object " &
8191 "with atomic actual", Actual);
8193 elsif Is_Volatile_Object (Actual)
8194 and then not Is_Volatile (Orig_Ftyp)
8197 ("cannot instantiate non-volatile formal object " &
8198 "with volatile actual", Actual);
8204 -- The instantiation of a generic formal in-parameter is constant
8205 -- declaration. The actual is the expression for that declaration.
8207 if Present (Actual) then
8208 if Present (Subt_Mark) then
8210 else pragma Assert (Present (Acc_Def));
8215 Make_Object_Declaration (Loc,
8216 Defining_Identifier => New_Copy (Formal_Id),
8217 Constant_Present => True,
8218 Object_Definition => New_Copy_Tree (Def),
8219 Expression => Actual);
8221 Set_Corresponding_Generic_Association (Decl_Node, Act_Assoc);
8223 -- A generic formal object of a tagged type is defined to be
8224 -- aliased so the new constant must also be treated as aliased.
8227 (Etype (Defining_Identifier (Analyzed_Formal)))
8229 Set_Aliased_Present (Decl_Node);
8232 Append (Decl_Node, List);
8234 -- No need to repeat (pre-)analysis of some expression nodes
8235 -- already handled in Pre_Analyze_Actuals.
8237 if Nkind (Actual) /= N_Allocator then
8240 -- Return if the analysis of the actual reported some error
8242 if Etype (Actual) = Any_Type then
8248 Typ : constant Entity_Id :=
8250 (Etype (Defining_Identifier (Analyzed_Formal)));
8253 Freeze_Before (Instantiation_Node, Typ);
8255 -- If the actual is an aggregate, perform name resolution on
8256 -- its components (the analysis of an aggregate does not do it)
8257 -- to capture local names that may be hidden if the generic is
8260 if Nkind (Actual) = N_Aggregate then
8261 Pre_Analyze_And_Resolve (Actual, Typ);
8264 if Is_Limited_Type (Typ)
8265 and then not OK_For_Limited_Init (Actual)
8268 ("initialization not allowed for limited types", Actual);
8269 Explain_Limited_Type (Typ, Actual);
8273 elsif Present (Default_Expression (Formal)) then
8275 -- Use default to construct declaration
8277 if Present (Subt_Mark) then
8279 else pragma Assert (Present (Acc_Def));
8284 Make_Object_Declaration (Sloc (Formal),
8285 Defining_Identifier => New_Copy (Formal_Id),
8286 Constant_Present => True,
8287 Object_Definition => New_Copy (Def),
8288 Expression => New_Copy_Tree (Default_Expression
8291 Append (Decl_Node, List);
8292 Set_Analyzed (Expression (Decl_Node), False);
8297 Instantiation_Node, Formal_Id);
8298 Error_Msg_NE ("\in instantiation of & declared#",
8300 Scope (Defining_Identifier (Analyzed_Formal)));
8303 (Etype (Defining_Identifier (Analyzed_Formal)))
8305 -- Create dummy constant declaration so that instance can be
8306 -- analyzed, to minimize cascaded visibility errors.
8308 if Present (Subt_Mark) then
8310 else pragma Assert (Present (Acc_Def));
8315 Make_Object_Declaration (Loc,
8316 Defining_Identifier => New_Copy (Formal_Id),
8317 Constant_Present => True,
8318 Object_Definition => New_Copy (Def),
8320 Make_Attribute_Reference (Sloc (Formal_Id),
8321 Attribute_Name => Name_First,
8322 Prefix => New_Copy (Def)));
8324 Append (Decl_Node, List);
8327 Abandon_Instantiation (Instantiation_Node);
8332 if Nkind (Actual) in N_Has_Entity then
8333 Actual_Decl := Parent (Entity (Actual));
8336 -- Ada 2005 (AI-423): For a formal object declaration with a null
8337 -- exclusion or an access definition that has a null exclusion: If the
8338 -- actual matching the formal object declaration denotes a generic
8339 -- formal object of another generic unit G, and the instantiation
8340 -- containing the actual occurs within the body of G or within the body
8341 -- of a generic unit declared within the declarative region of G, then
8342 -- the declaration of the formal object of G must have a null exclusion.
8343 -- Otherwise, the subtype of the actual matching the formal object
8344 -- declaration shall exclude null.
8346 if Ada_Version >= Ada_05
8347 and then Present (Actual_Decl)
8349 (Nkind (Actual_Decl) = N_Formal_Object_Declaration
8350 or else Nkind (Actual_Decl) = N_Object_Declaration)
8351 and then Nkind (Analyzed_Formal) = N_Formal_Object_Declaration
8352 and then Has_Null_Exclusion (Actual_Decl)
8353 and then not Has_Null_Exclusion (Analyzed_Formal)
8355 Error_Msg_Sloc := Sloc (Actual_Decl);
8357 ("`NOT NULL` required in formal, to match actual #",
8362 end Instantiate_Object;
8364 ------------------------------
8365 -- Instantiate_Package_Body --
8366 ------------------------------
8368 procedure Instantiate_Package_Body
8369 (Body_Info : Pending_Body_Info;
8370 Inlined_Body : Boolean := False;
8371 Body_Optional : Boolean := False)
8373 Act_Decl : constant Node_Id := Body_Info.Act_Decl;
8374 Inst_Node : constant Node_Id := Body_Info.Inst_Node;
8375 Loc : constant Source_Ptr := Sloc (Inst_Node);
8377 Gen_Id : constant Node_Id := Name (Inst_Node);
8378 Gen_Unit : constant Entity_Id := Get_Generic_Entity (Inst_Node);
8379 Gen_Decl : constant Node_Id := Unit_Declaration_Node (Gen_Unit);
8380 Act_Spec : constant Node_Id := Specification (Act_Decl);
8381 Act_Decl_Id : constant Entity_Id := Defining_Entity (Act_Spec);
8383 Act_Body_Name : Node_Id;
8385 Gen_Body_Id : Node_Id;
8387 Act_Body_Id : Entity_Id;
8389 Parent_Installed : Boolean := False;
8390 Save_Style_Check : constant Boolean := Style_Check;
8393 Gen_Body_Id := Corresponding_Body (Gen_Decl);
8395 -- The instance body may already have been processed, as the parent of
8396 -- another instance that is inlined (Load_Parent_Of_Generic).
8398 if Present (Corresponding_Body (Instance_Spec (Inst_Node))) then
8402 Expander_Mode_Save_And_Set (Body_Info.Expander_Status);
8404 -- Re-establish the state of information on which checks are suppressed.
8405 -- This information was set in Body_Info at the point of instantiation,
8406 -- and now we restore it so that the instance is compiled using the
8407 -- check status at the instantiation (RM 11.5 (7.2/2), AI95-00224-01).
8409 Local_Suppress_Stack_Top := Body_Info.Local_Suppress_Stack_Top;
8410 Scope_Suppress := Body_Info.Scope_Suppress;
8412 if No (Gen_Body_Id) then
8413 Load_Parent_Of_Generic
8414 (Inst_Node, Specification (Gen_Decl), Body_Optional);
8415 Gen_Body_Id := Corresponding_Body (Gen_Decl);
8418 -- Establish global variable for sloc adjustment and for error recovery
8420 Instantiation_Node := Inst_Node;
8422 if Present (Gen_Body_Id) then
8423 Save_Env (Gen_Unit, Act_Decl_Id);
8424 Style_Check := False;
8425 Current_Sem_Unit := Body_Info.Current_Sem_Unit;
8427 Gen_Body := Unit_Declaration_Node (Gen_Body_Id);
8429 Create_Instantiation_Source
8430 (Inst_Node, Gen_Body_Id, False, S_Adjustment);
8434 (Original_Node (Gen_Body), Empty, Instantiating => True);
8436 -- Build new name (possibly qualified) for body declaration
8438 Act_Body_Id := New_Copy (Act_Decl_Id);
8440 -- Some attributes of spec entity are not inherited by body entity
8442 Set_Handler_Records (Act_Body_Id, No_List);
8444 if Nkind (Defining_Unit_Name (Act_Spec)) =
8445 N_Defining_Program_Unit_Name
8448 Make_Defining_Program_Unit_Name (Loc,
8449 Name => New_Copy_Tree (Name (Defining_Unit_Name (Act_Spec))),
8450 Defining_Identifier => Act_Body_Id);
8452 Act_Body_Name := Act_Body_Id;
8455 Set_Defining_Unit_Name (Act_Body, Act_Body_Name);
8457 Set_Corresponding_Spec (Act_Body, Act_Decl_Id);
8458 Check_Generic_Actuals (Act_Decl_Id, False);
8460 -- If it is a child unit, make the parent instance (which is an
8461 -- instance of the parent of the generic) visible. The parent
8462 -- instance is the prefix of the name of the generic unit.
8464 if Ekind (Scope (Gen_Unit)) = E_Generic_Package
8465 and then Nkind (Gen_Id) = N_Expanded_Name
8467 Install_Parent (Entity (Prefix (Gen_Id)), In_Body => True);
8468 Parent_Installed := True;
8470 elsif Is_Child_Unit (Gen_Unit) then
8471 Install_Parent (Scope (Gen_Unit), In_Body => True);
8472 Parent_Installed := True;
8475 -- If the instantiation is a library unit, and this is the main unit,
8476 -- then build the resulting compilation unit nodes for the instance.
8477 -- If this is a compilation unit but it is not the main unit, then it
8478 -- is the body of a unit in the context, that is being compiled
8479 -- because it is encloses some inlined unit or another generic unit
8480 -- being instantiated. In that case, this body is not part of the
8481 -- current compilation, and is not attached to the tree, but its
8482 -- parent must be set for analysis.
8484 if Nkind (Parent (Inst_Node)) = N_Compilation_Unit then
8486 -- Replace instance node with body of instance, and create new
8487 -- node for corresponding instance declaration.
8489 Build_Instance_Compilation_Unit_Nodes
8490 (Inst_Node, Act_Body, Act_Decl);
8491 Analyze (Inst_Node);
8493 if Parent (Inst_Node) = Cunit (Main_Unit) then
8495 -- If the instance is a child unit itself, then set the scope
8496 -- of the expanded body to be the parent of the instantiation
8497 -- (ensuring that the fully qualified name will be generated
8498 -- for the elaboration subprogram).
8500 if Nkind (Defining_Unit_Name (Act_Spec)) =
8501 N_Defining_Program_Unit_Name
8504 (Defining_Entity (Inst_Node), Scope (Act_Decl_Id));
8508 -- Case where instantiation is not a library unit
8511 -- If this is an early instantiation, i.e. appears textually
8512 -- before the corresponding body and must be elaborated first,
8513 -- indicate that the body instance is to be delayed.
8515 Install_Body (Act_Body, Inst_Node, Gen_Body, Gen_Decl);
8517 -- Now analyze the body. We turn off all checks if this is an
8518 -- internal unit, since there is no reason to have checks on for
8519 -- any predefined run-time library code. All such code is designed
8520 -- to be compiled with checks off.
8522 -- Note that we do NOT apply this criterion to children of GNAT
8523 -- (or on VMS, children of DEC). The latter units must suppress
8524 -- checks explicitly if this is needed.
8526 if Is_Predefined_File_Name
8527 (Unit_File_Name (Get_Source_Unit (Gen_Decl)))
8529 Analyze (Act_Body, Suppress => All_Checks);
8535 Inherit_Context (Gen_Body, Inst_Node);
8537 -- Remove the parent instances if they have been placed on the scope
8538 -- stack to compile the body.
8540 if Parent_Installed then
8541 Remove_Parent (In_Body => True);
8544 Restore_Private_Views (Act_Decl_Id);
8546 -- Remove the current unit from visibility if this is an instance
8547 -- that is not elaborated on the fly for inlining purposes.
8549 if not Inlined_Body then
8550 Set_Is_Immediately_Visible (Act_Decl_Id, False);
8554 Style_Check := Save_Style_Check;
8556 -- If we have no body, and the unit requires a body, then complain. This
8557 -- complaint is suppressed if we have detected other errors (since a
8558 -- common reason for missing the body is that it had errors).
8560 elsif Unit_Requires_Body (Gen_Unit)
8561 and then not Body_Optional
8563 if Serious_Errors_Detected = 0 then
8565 ("cannot find body of generic package &", Inst_Node, Gen_Unit);
8567 -- Don't attempt to perform any cleanup actions if some other error
8568 -- was aready detected, since this can cause blowups.
8574 -- Case of package that does not need a body
8577 -- If the instantiation of the declaration is a library unit, rewrite
8578 -- the original package instantiation as a package declaration in the
8579 -- compilation unit node.
8581 if Nkind (Parent (Inst_Node)) = N_Compilation_Unit then
8582 Set_Parent_Spec (Act_Decl, Parent_Spec (Inst_Node));
8583 Rewrite (Inst_Node, Act_Decl);
8585 -- Generate elaboration entity, in case spec has elaboration code.
8586 -- This cannot be done when the instance is analyzed, because it
8587 -- is not known yet whether the body exists.
8589 Set_Elaboration_Entity_Required (Act_Decl_Id, False);
8590 Build_Elaboration_Entity (Parent (Inst_Node), Act_Decl_Id);
8592 -- If the instantiation is not a library unit, then append the
8593 -- declaration to the list of implicitly generated entities. unless
8594 -- it is already a list member which means that it was already
8597 elsif not Is_List_Member (Act_Decl) then
8598 Mark_Rewrite_Insertion (Act_Decl);
8599 Insert_Before (Inst_Node, Act_Decl);
8603 Expander_Mode_Restore;
8604 end Instantiate_Package_Body;
8606 ---------------------------------
8607 -- Instantiate_Subprogram_Body --
8608 ---------------------------------
8610 procedure Instantiate_Subprogram_Body
8611 (Body_Info : Pending_Body_Info)
8613 Act_Decl : constant Node_Id := Body_Info.Act_Decl;
8614 Inst_Node : constant Node_Id := Body_Info.Inst_Node;
8615 Loc : constant Source_Ptr := Sloc (Inst_Node);
8616 Gen_Id : constant Node_Id := Name (Inst_Node);
8617 Gen_Unit : constant Entity_Id := Get_Generic_Entity (Inst_Node);
8618 Gen_Decl : constant Node_Id := Unit_Declaration_Node (Gen_Unit);
8619 Anon_Id : constant Entity_Id :=
8620 Defining_Unit_Name (Specification (Act_Decl));
8621 Pack_Id : constant Entity_Id :=
8622 Defining_Unit_Name (Parent (Act_Decl));
8625 Gen_Body_Id : Node_Id;
8627 Pack_Body : Node_Id;
8628 Prev_Formal : Entity_Id;
8630 Unit_Renaming : Node_Id;
8632 Parent_Installed : Boolean := False;
8633 Save_Style_Check : constant Boolean := Style_Check;
8636 Gen_Body_Id := Corresponding_Body (Gen_Decl);
8638 Expander_Mode_Save_And_Set (Body_Info.Expander_Status);
8640 -- Re-establish the state of information on which checks are suppressed.
8641 -- This information was set in Body_Info at the point of instantiation,
8642 -- and now we restore it so that the instance is compiled using the
8643 -- check status at the instantiation (RM 11.5 (7.2/2), AI95-00224-01).
8645 Local_Suppress_Stack_Top := Body_Info.Local_Suppress_Stack_Top;
8646 Scope_Suppress := Body_Info.Scope_Suppress;
8648 if No (Gen_Body_Id) then
8649 Load_Parent_Of_Generic (Inst_Node, Specification (Gen_Decl));
8650 Gen_Body_Id := Corresponding_Body (Gen_Decl);
8653 Instantiation_Node := Inst_Node;
8655 if Present (Gen_Body_Id) then
8656 Gen_Body := Unit_Declaration_Node (Gen_Body_Id);
8658 if Nkind (Gen_Body) = N_Subprogram_Body_Stub then
8660 -- Either body is not present, or context is non-expanding, as
8661 -- when compiling a subunit. Mark the instance as completed, and
8662 -- diagnose a missing body when needed.
8665 and then Operating_Mode = Generate_Code
8668 ("missing proper body for instantiation", Gen_Body);
8671 Set_Has_Completion (Anon_Id);
8675 Save_Env (Gen_Unit, Anon_Id);
8676 Style_Check := False;
8677 Current_Sem_Unit := Body_Info.Current_Sem_Unit;
8678 Create_Instantiation_Source
8686 (Original_Node (Gen_Body), Empty, Instantiating => True);
8688 -- Create proper defining name for the body, to correspond to
8689 -- the one in the spec.
8691 Set_Defining_Unit_Name (Specification (Act_Body),
8692 Make_Defining_Identifier
8693 (Sloc (Defining_Entity (Inst_Node)), Chars (Anon_Id)));
8694 Set_Corresponding_Spec (Act_Body, Anon_Id);
8695 Set_Has_Completion (Anon_Id);
8696 Check_Generic_Actuals (Pack_Id, False);
8698 -- Generate a reference to link the visible subprogram instance to
8699 -- the the generic body, which for navigation purposes is the only
8700 -- available source for the instance.
8703 (Related_Instance (Pack_Id),
8704 Gen_Body_Id, 'b', Set_Ref => False, Force => True);
8706 -- If it is a child unit, make the parent instance (which is an
8707 -- instance of the parent of the generic) visible. The parent
8708 -- instance is the prefix of the name of the generic unit.
8710 if Ekind (Scope (Gen_Unit)) = E_Generic_Package
8711 and then Nkind (Gen_Id) = N_Expanded_Name
8713 Install_Parent (Entity (Prefix (Gen_Id)), In_Body => True);
8714 Parent_Installed := True;
8716 elsif Is_Child_Unit (Gen_Unit) then
8717 Install_Parent (Scope (Gen_Unit), In_Body => True);
8718 Parent_Installed := True;
8721 -- Inside its body, a reference to the generic unit is a reference
8722 -- to the instance. The corresponding renaming is the first
8723 -- declaration in the body.
8726 Make_Subprogram_Renaming_Declaration (Loc,
8729 Specification (Original_Node (Gen_Body)),
8731 Instantiating => True),
8732 Name => New_Occurrence_Of (Anon_Id, Loc));
8734 -- If there is a formal subprogram with the same name as the unit
8735 -- itself, do not add this renaming declaration. This is a temporary
8736 -- fix for one ACVC test. ???
8738 Prev_Formal := First_Entity (Pack_Id);
8739 while Present (Prev_Formal) loop
8740 if Chars (Prev_Formal) = Chars (Gen_Unit)
8741 and then Is_Overloadable (Prev_Formal)
8746 Next_Entity (Prev_Formal);
8749 if Present (Prev_Formal) then
8750 Decls := New_List (Act_Body);
8752 Decls := New_List (Unit_Renaming, Act_Body);
8755 -- The subprogram body is placed in the body of a dummy package body,
8756 -- whose spec contains the subprogram declaration as well as the
8757 -- renaming declarations for the generic parameters.
8759 Pack_Body := Make_Package_Body (Loc,
8760 Defining_Unit_Name => New_Copy (Pack_Id),
8761 Declarations => Decls);
8763 Set_Corresponding_Spec (Pack_Body, Pack_Id);
8765 -- If the instantiation is a library unit, then build resulting
8766 -- compilation unit nodes for the instance. The declaration of
8767 -- the enclosing package is the grandparent of the subprogram
8768 -- declaration. First replace the instantiation node as the unit
8769 -- of the corresponding compilation.
8771 if Nkind (Parent (Inst_Node)) = N_Compilation_Unit then
8772 if Parent (Inst_Node) = Cunit (Main_Unit) then
8773 Set_Unit (Parent (Inst_Node), Inst_Node);
8774 Build_Instance_Compilation_Unit_Nodes
8775 (Inst_Node, Pack_Body, Parent (Parent (Act_Decl)));
8776 Analyze (Inst_Node);
8778 Set_Parent (Pack_Body, Parent (Inst_Node));
8779 Analyze (Pack_Body);
8783 Insert_Before (Inst_Node, Pack_Body);
8784 Mark_Rewrite_Insertion (Pack_Body);
8785 Analyze (Pack_Body);
8787 if Expander_Active then
8788 Freeze_Subprogram_Body (Inst_Node, Gen_Body, Pack_Id);
8792 Inherit_Context (Gen_Body, Inst_Node);
8794 Restore_Private_Views (Pack_Id, False);
8796 if Parent_Installed then
8797 Remove_Parent (In_Body => True);
8801 Style_Check := Save_Style_Check;
8803 -- Body not found. Error was emitted already. If there were no previous
8804 -- errors, this may be an instance whose scope is a premature instance.
8805 -- In that case we must insure that the (legal) program does raise
8806 -- program error if executed. We generate a subprogram body for this
8807 -- purpose. See DEC ac30vso.
8809 -- Should not reference proprietary DEC tests in comments ???
8811 elsif Serious_Errors_Detected = 0
8812 and then Nkind (Parent (Inst_Node)) /= N_Compilation_Unit
8814 if Ekind (Anon_Id) = E_Procedure then
8816 Make_Subprogram_Body (Loc,
8818 Make_Procedure_Specification (Loc,
8819 Defining_Unit_Name =>
8820 Make_Defining_Identifier (Loc, Chars (Anon_Id)),
8821 Parameter_Specifications =>
8823 (Parameter_Specifications (Parent (Anon_Id)))),
8825 Declarations => Empty_List,
8826 Handled_Statement_Sequence =>
8827 Make_Handled_Sequence_Of_Statements (Loc,
8830 Make_Raise_Program_Error (Loc,
8832 PE_Access_Before_Elaboration))));
8836 Make_Raise_Program_Error (Loc,
8837 Reason => PE_Access_Before_Elaboration);
8839 Set_Etype (Ret_Expr, (Etype (Anon_Id)));
8840 Set_Analyzed (Ret_Expr);
8843 Make_Subprogram_Body (Loc,
8845 Make_Function_Specification (Loc,
8846 Defining_Unit_Name =>
8847 Make_Defining_Identifier (Loc, Chars (Anon_Id)),
8848 Parameter_Specifications =>
8850 (Parameter_Specifications (Parent (Anon_Id))),
8851 Result_Definition =>
8852 New_Occurrence_Of (Etype (Anon_Id), Loc)),
8854 Declarations => Empty_List,
8855 Handled_Statement_Sequence =>
8856 Make_Handled_Sequence_Of_Statements (Loc,
8859 (Make_Simple_Return_Statement (Loc, Ret_Expr))));
8862 Pack_Body := Make_Package_Body (Loc,
8863 Defining_Unit_Name => New_Copy (Pack_Id),
8864 Declarations => New_List (Act_Body));
8866 Insert_After (Inst_Node, Pack_Body);
8867 Set_Corresponding_Spec (Pack_Body, Pack_Id);
8868 Analyze (Pack_Body);
8871 Expander_Mode_Restore;
8872 end Instantiate_Subprogram_Body;
8874 ----------------------
8875 -- Instantiate_Type --
8876 ----------------------
8878 function Instantiate_Type
8881 Analyzed_Formal : Node_Id;
8882 Actual_Decls : List_Id) return List_Id
8884 Gen_T : constant Entity_Id := Defining_Identifier (Formal);
8885 A_Gen_T : constant Entity_Id :=
8886 Defining_Identifier (Analyzed_Formal);
8887 Ancestor : Entity_Id := Empty;
8888 Def : constant Node_Id := Formal_Type_Definition (Formal);
8890 Decl_Node : Node_Id;
8891 Decl_Nodes : List_Id;
8895 procedure Validate_Array_Type_Instance;
8896 procedure Validate_Access_Subprogram_Instance;
8897 procedure Validate_Access_Type_Instance;
8898 procedure Validate_Derived_Type_Instance;
8899 procedure Validate_Derived_Interface_Type_Instance;
8900 procedure Validate_Interface_Type_Instance;
8901 procedure Validate_Private_Type_Instance;
8902 -- These procedures perform validation tests for the named case
8904 function Subtypes_Match (Gen_T, Act_T : Entity_Id) return Boolean;
8905 -- Check that base types are the same and that the subtypes match
8906 -- statically. Used in several of the above.
8908 --------------------
8909 -- Subtypes_Match --
8910 --------------------
8912 function Subtypes_Match (Gen_T, Act_T : Entity_Id) return Boolean is
8913 T : constant Entity_Id := Get_Instance_Of (Gen_T);
8916 return (Base_Type (T) = Base_Type (Act_T)
8917 and then Subtypes_Statically_Match (T, Act_T))
8919 or else (Is_Class_Wide_Type (Gen_T)
8920 and then Is_Class_Wide_Type (Act_T)
8923 (Get_Instance_Of (Root_Type (Gen_T)),
8927 ((Ekind (Gen_T) = E_Anonymous_Access_Subprogram_Type
8928 or else Ekind (Gen_T) = E_Anonymous_Access_Type)
8929 and then Ekind (Act_T) = Ekind (Gen_T)
8931 Subtypes_Statically_Match
8932 (Designated_Type (Gen_T), Designated_Type (Act_T)));
8935 -----------------------------------------
8936 -- Validate_Access_Subprogram_Instance --
8937 -----------------------------------------
8939 procedure Validate_Access_Subprogram_Instance is
8941 if not Is_Access_Type (Act_T)
8942 or else Ekind (Designated_Type (Act_T)) /= E_Subprogram_Type
8945 ("expect access type in instantiation of &", Actual, Gen_T);
8946 Abandon_Instantiation (Actual);
8949 Check_Mode_Conformant
8950 (Designated_Type (Act_T),
8951 Designated_Type (A_Gen_T),
8955 if Ekind (Base_Type (Act_T)) = E_Access_Protected_Subprogram_Type then
8956 if Ekind (A_Gen_T) = E_Access_Subprogram_Type then
8958 ("protected access type not allowed for formal &",
8962 elsif Ekind (A_Gen_T) = E_Access_Protected_Subprogram_Type then
8964 ("expect protected access type for formal &",
8967 end Validate_Access_Subprogram_Instance;
8969 -----------------------------------
8970 -- Validate_Access_Type_Instance --
8971 -----------------------------------
8973 procedure Validate_Access_Type_Instance is
8974 Desig_Type : constant Entity_Id :=
8976 (Designated_Type (A_Gen_T), Scope (A_Gen_T));
8979 if not Is_Access_Type (Act_T) then
8981 ("expect access type in instantiation of &", Actual, Gen_T);
8982 Abandon_Instantiation (Actual);
8985 if Is_Access_Constant (A_Gen_T) then
8986 if not Is_Access_Constant (Act_T) then
8988 ("actual type must be access-to-constant type", Actual);
8989 Abandon_Instantiation (Actual);
8992 if Is_Access_Constant (Act_T) then
8994 ("actual type must be access-to-variable type", Actual);
8995 Abandon_Instantiation (Actual);
8997 elsif Ekind (A_Gen_T) = E_General_Access_Type
8998 and then Ekind (Base_Type (Act_T)) /= E_General_Access_Type
9000 Error_Msg_N ("actual must be general access type!", Actual);
9001 Error_Msg_NE ("add ALL to }!", Actual, Act_T);
9002 Abandon_Instantiation (Actual);
9006 -- The designated subtypes, that is to say the subtypes introduced
9007 -- by an access type declaration (and not by a subtype declaration)
9010 if not Subtypes_Match
9011 (Desig_Type, Designated_Type (Base_Type (Act_T)))
9014 ("designated type of actual does not match that of formal &",
9016 Abandon_Instantiation (Actual);
9018 elsif Is_Access_Type (Designated_Type (Act_T))
9019 and then Is_Constrained (Designated_Type (Designated_Type (Act_T)))
9021 Is_Constrained (Designated_Type (Desig_Type))
9024 ("designated type of actual does not match that of formal &",
9026 Abandon_Instantiation (Actual);
9029 -- Ada 2005: null-exclusion indicators of the two types must agree
9031 if Can_Never_Be_Null (A_Gen_T) /= Can_Never_Be_Null (Act_T) then
9033 ("non null exclusion of actual and formal & do not match",
9036 end Validate_Access_Type_Instance;
9038 ----------------------------------
9039 -- Validate_Array_Type_Instance --
9040 ----------------------------------
9042 procedure Validate_Array_Type_Instance is
9047 function Formal_Dimensions return Int;
9048 -- Count number of dimensions in array type formal
9050 -----------------------
9051 -- Formal_Dimensions --
9052 -----------------------
9054 function Formal_Dimensions return Int is
9059 if Nkind (Def) = N_Constrained_Array_Definition then
9060 Index := First (Discrete_Subtype_Definitions (Def));
9062 Index := First (Subtype_Marks (Def));
9065 while Present (Index) loop
9071 end Formal_Dimensions;
9073 -- Start of processing for Validate_Array_Type_Instance
9076 if not Is_Array_Type (Act_T) then
9078 ("expect array type in instantiation of &", Actual, Gen_T);
9079 Abandon_Instantiation (Actual);
9081 elsif Nkind (Def) = N_Constrained_Array_Definition then
9082 if not (Is_Constrained (Act_T)) then
9084 ("expect constrained array in instantiation of &",
9086 Abandon_Instantiation (Actual);
9090 if Is_Constrained (Act_T) then
9092 ("expect unconstrained array in instantiation of &",
9094 Abandon_Instantiation (Actual);
9098 if Formal_Dimensions /= Number_Dimensions (Act_T) then
9100 ("dimensions of actual do not match formal &", Actual, Gen_T);
9101 Abandon_Instantiation (Actual);
9104 I1 := First_Index (A_Gen_T);
9105 I2 := First_Index (Act_T);
9106 for J in 1 .. Formal_Dimensions loop
9108 -- If the indices of the actual were given by a subtype_mark,
9109 -- the index was transformed into a range attribute. Retrieve
9110 -- the original type mark for checking.
9112 if Is_Entity_Name (Original_Node (I2)) then
9113 T2 := Entity (Original_Node (I2));
9118 if not Subtypes_Match
9119 (Find_Actual_Type (Etype (I1), Scope (A_Gen_T)), T2)
9122 ("index types of actual do not match those of formal &",
9124 Abandon_Instantiation (Actual);
9131 if not Subtypes_Match (
9132 Find_Actual_Type (Component_Type (A_Gen_T), Scope (A_Gen_T)),
9133 Component_Type (Act_T))
9136 ("component subtype of actual does not match that of formal &",
9138 Abandon_Instantiation (Actual);
9141 if Has_Aliased_Components (A_Gen_T)
9142 and then not Has_Aliased_Components (Act_T)
9145 ("actual must have aliased components to match formal type &",
9149 end Validate_Array_Type_Instance;
9151 -----------------------------------------------
9152 -- Validate_Derived_Interface_Type_Instance --
9153 -----------------------------------------------
9155 procedure Validate_Derived_Interface_Type_Instance is
9156 Par : constant Entity_Id := Entity (Subtype_Indication (Def));
9160 -- First apply interface instance checks
9162 Validate_Interface_Type_Instance;
9164 -- Verify that immediate parent interface is an ancestor of
9168 and then not Interface_Present_In_Ancestor (Act_T, Par)
9171 ("interface actual must include progenitor&", Actual, Par);
9174 -- Now verify that the actual includes all other ancestors of
9177 Elmt := First_Elmt (Abstract_Interfaces (A_Gen_T));
9178 while Present (Elmt) loop
9179 if not Interface_Present_In_Ancestor
9180 (Act_T, Get_Instance_Of (Node (Elmt)))
9183 ("interface actual must include progenitor&",
9184 Actual, Node (Elmt));
9189 end Validate_Derived_Interface_Type_Instance;
9191 ------------------------------------
9192 -- Validate_Derived_Type_Instance --
9193 ------------------------------------
9195 procedure Validate_Derived_Type_Instance is
9196 Actual_Discr : Entity_Id;
9197 Ancestor_Discr : Entity_Id;
9200 -- If the parent type in the generic declaration is itself a previous
9201 -- formal type, then it is local to the generic and absent from the
9202 -- analyzed generic definition. In that case the ancestor is the
9203 -- instance of the formal (which must have been instantiated
9204 -- previously), unless the ancestor is itself a formal derived type.
9205 -- In this latter case (which is the subject of Corrigendum 8652/0038
9206 -- (AI-202) the ancestor of the formals is the ancestor of its
9207 -- parent. Otherwise, the analyzed generic carries the parent type.
9208 -- If the parent type is defined in a previous formal package, then
9209 -- the scope of that formal package is that of the generic type
9210 -- itself, and it has already been mapped into the corresponding type
9211 -- in the actual package.
9213 -- Common case: parent type defined outside of the generic
9215 if Is_Entity_Name (Subtype_Mark (Def))
9216 and then Present (Entity (Subtype_Mark (Def)))
9218 Ancestor := Get_Instance_Of (Entity (Subtype_Mark (Def)));
9220 -- Check whether parent is defined in a previous formal package
9223 Scope (Scope (Base_Type (Etype (A_Gen_T)))) = Scope (A_Gen_T)
9226 Get_Instance_Of (Base_Type (Etype (A_Gen_T)));
9228 -- The type may be a local derivation, or a type extension of a
9229 -- previous formal, or of a formal of a parent package.
9231 elsif Is_Derived_Type (Get_Instance_Of (A_Gen_T))
9233 Ekind (Get_Instance_Of (A_Gen_T)) = E_Record_Type_With_Private
9235 -- Check whether the parent is another derived formal type in the
9236 -- same generic unit.
9238 if Etype (A_Gen_T) /= A_Gen_T
9239 and then Is_Generic_Type (Etype (A_Gen_T))
9240 and then Scope (Etype (A_Gen_T)) = Scope (A_Gen_T)
9241 and then Etype (Etype (A_Gen_T)) /= Etype (A_Gen_T)
9243 -- Locate ancestor of parent from the subtype declaration
9244 -- created for the actual.
9250 Decl := First (Actual_Decls);
9251 while Present (Decl) loop
9252 if Nkind (Decl) = N_Subtype_Declaration
9253 and then Chars (Defining_Identifier (Decl)) =
9254 Chars (Etype (A_Gen_T))
9256 Ancestor := Generic_Parent_Type (Decl);
9264 pragma Assert (Present (Ancestor));
9268 Get_Instance_Of (Base_Type (Get_Instance_Of (A_Gen_T)));
9272 Ancestor := Get_Instance_Of (Etype (Base_Type (A_Gen_T)));
9275 -- Ada 2005 (AI-251)
9277 if Ada_Version >= Ada_05
9278 and then Is_Interface (Ancestor)
9280 if not Interface_Present_In_Ancestor (Act_T, Ancestor) then
9282 ("(Ada 2005) expected type implementing & in instantiation",
9286 elsif not Is_Ancestor (Base_Type (Ancestor), Act_T) then
9288 ("expect type derived from & in instantiation",
9289 Actual, First_Subtype (Ancestor));
9290 Abandon_Instantiation (Actual);
9293 -- Ada 2005 (AI-443): Synchronized formal derived type ckecks. Note
9294 -- that the formal type declaration has been rewritten as a private
9297 if Ada_Version >= Ada_05
9298 and then Nkind (Parent (A_Gen_T)) = N_Private_Extension_Declaration
9299 and then Synchronized_Present (Parent (A_Gen_T))
9301 -- The actual must be a synchronized tagged type
9303 if not Is_Tagged_Type (Act_T) then
9305 ("actual of synchronized type must be tagged", Actual);
9306 Abandon_Instantiation (Actual);
9308 elsif Nkind (Parent (Act_T)) = N_Full_Type_Declaration
9309 and then Nkind (Type_Definition (Parent (Act_T))) =
9310 N_Derived_Type_Definition
9311 and then not Synchronized_Present (Type_Definition
9315 ("actual of synchronized type must be synchronized", Actual);
9316 Abandon_Instantiation (Actual);
9320 -- Perform atomic/volatile checks (RM C.6(12))
9322 if Is_Atomic (Act_T) and then not Is_Atomic (Ancestor) then
9324 ("cannot have atomic actual type for non-atomic formal type",
9327 elsif Is_Volatile (Act_T)
9328 and then not Is_Volatile (Ancestor)
9329 and then Is_By_Reference_Type (Ancestor)
9332 ("cannot have volatile actual type for non-volatile formal type",
9336 -- It should not be necessary to check for unknown discriminants on
9337 -- Formal, but for some reason Has_Unknown_Discriminants is false for
9338 -- A_Gen_T, so Is_Indefinite_Subtype incorrectly returns False. This
9339 -- needs fixing. ???
9341 if not Is_Indefinite_Subtype (A_Gen_T)
9342 and then not Unknown_Discriminants_Present (Formal)
9343 and then Is_Indefinite_Subtype (Act_T)
9346 ("actual subtype must be constrained", Actual);
9347 Abandon_Instantiation (Actual);
9350 if not Unknown_Discriminants_Present (Formal) then
9351 if Is_Constrained (Ancestor) then
9352 if not Is_Constrained (Act_T) then
9354 ("actual subtype must be constrained", Actual);
9355 Abandon_Instantiation (Actual);
9358 -- Ancestor is unconstrained, Check if generic formal and actual
9359 -- agree on constrainedness. The check only applies to array types
9360 -- and discriminated types.
9362 elsif Is_Constrained (Act_T) then
9363 if Ekind (Ancestor) = E_Access_Type
9365 (not Is_Constrained (A_Gen_T)
9366 and then Is_Composite_Type (A_Gen_T))
9369 ("actual subtype must be unconstrained", Actual);
9370 Abandon_Instantiation (Actual);
9373 -- A class-wide type is only allowed if the formal has unknown
9376 elsif Is_Class_Wide_Type (Act_T)
9377 and then not Has_Unknown_Discriminants (Ancestor)
9380 ("actual for & cannot be a class-wide type", Actual, Gen_T);
9381 Abandon_Instantiation (Actual);
9383 -- Otherwise, the formal and actual shall have the same number
9384 -- of discriminants and each discriminant of the actual must
9385 -- correspond to a discriminant of the formal.
9387 elsif Has_Discriminants (Act_T)
9388 and then not Has_Unknown_Discriminants (Act_T)
9389 and then Has_Discriminants (Ancestor)
9391 Actual_Discr := First_Discriminant (Act_T);
9392 Ancestor_Discr := First_Discriminant (Ancestor);
9393 while Present (Actual_Discr)
9394 and then Present (Ancestor_Discr)
9396 if Base_Type (Act_T) /= Base_Type (Ancestor) and then
9397 No (Corresponding_Discriminant (Actual_Discr))
9400 ("discriminant & does not correspond " &
9401 "to ancestor discriminant", Actual, Actual_Discr);
9402 Abandon_Instantiation (Actual);
9405 Next_Discriminant (Actual_Discr);
9406 Next_Discriminant (Ancestor_Discr);
9409 if Present (Actual_Discr) or else Present (Ancestor_Discr) then
9411 ("actual for & must have same number of discriminants",
9413 Abandon_Instantiation (Actual);
9416 -- This case should be caught by the earlier check for for
9417 -- constrainedness, but the check here is added for completeness.
9419 elsif Has_Discriminants (Act_T)
9420 and then not Has_Unknown_Discriminants (Act_T)
9423 ("actual for & must not have discriminants", Actual, Gen_T);
9424 Abandon_Instantiation (Actual);
9426 elsif Has_Discriminants (Ancestor) then
9428 ("actual for & must have known discriminants", Actual, Gen_T);
9429 Abandon_Instantiation (Actual);
9432 if not Subtypes_Statically_Compatible (Act_T, Ancestor) then
9434 ("constraint on actual is incompatible with formal", Actual);
9435 Abandon_Instantiation (Actual);
9439 -- If the formal and actual types are abstract, check that there
9440 -- are no abstract primitives of the actual type that correspond to
9441 -- nonabstract primitives of the formal type (second sentence of
9444 if Is_Abstract_Type (A_Gen_T) and then Is_Abstract_Type (Act_T) then
9445 Check_Abstract_Primitives : declare
9446 Gen_Prims : constant Elist_Id :=
9447 Primitive_Operations (A_Gen_T);
9449 Gen_Subp : Entity_Id;
9450 Anc_Subp : Entity_Id;
9451 Anc_Formal : Entity_Id;
9452 Anc_F_Type : Entity_Id;
9454 Act_Prims : constant Elist_Id := Primitive_Operations (Act_T);
9456 Act_Subp : Entity_Id;
9457 Act_Formal : Entity_Id;
9458 Act_F_Type : Entity_Id;
9460 Subprograms_Correspond : Boolean;
9462 function Is_Tagged_Ancestor (T1, T2 : Entity_Id) return Boolean;
9463 -- Returns true if T2 is derived directly or indirectly from
9464 -- T1, including derivations from interfaces. T1 and T2 are
9465 -- required to be specific tagged base types.
9467 ------------------------
9468 -- Is_Tagged_Ancestor --
9469 ------------------------
9471 function Is_Tagged_Ancestor (T1, T2 : Entity_Id) return Boolean
9473 Interfaces : Elist_Id;
9474 Intfc_Elmt : Elmt_Id;
9477 -- The predicate is satisfied if the types are the same
9482 -- If we've reached the top of the derivation chain then
9483 -- we know that T1 is not an ancestor of T2.
9485 elsif Etype (T2) = T2 then
9488 -- Proceed to check T2's immediate parent
9490 elsif Is_Ancestor (T1, Base_Type (Etype (T2))) then
9493 -- Finally, check to see if T1 is an ancestor of any of T2's
9497 Interfaces := Abstract_Interfaces (T2);
9499 Intfc_Elmt := First_Elmt (Interfaces);
9500 while Present (Intfc_Elmt) loop
9501 if Is_Ancestor (T1, Node (Intfc_Elmt)) then
9505 Next_Elmt (Intfc_Elmt);
9510 end Is_Tagged_Ancestor;
9512 -- Start of processing for Check_Abstract_Primitives
9515 -- Loop over all of the formal derived type's primitives
9517 Gen_Elmt := First_Elmt (Gen_Prims);
9518 while Present (Gen_Elmt) loop
9519 Gen_Subp := Node (Gen_Elmt);
9521 -- If the primitive of the formal is not abstract, then
9522 -- determine whether there is a corresponding primitive of
9523 -- the actual type that's abstract.
9525 if not Is_Abstract_Subprogram (Gen_Subp) then
9526 Act_Elmt := First_Elmt (Act_Prims);
9527 while Present (Act_Elmt) loop
9528 Act_Subp := Node (Act_Elmt);
9530 -- If we find an abstract primitive of the actual,
9531 -- then we need to test whether it corresponds to the
9532 -- subprogram from which the generic formal primitive
9535 if Is_Abstract_Subprogram (Act_Subp) then
9536 Anc_Subp := Alias (Gen_Subp);
9538 -- Test whether we have a corresponding primitive
9539 -- by comparing names, kinds, formal types, and
9542 if Chars (Anc_Subp) = Chars (Act_Subp)
9543 and then Ekind (Anc_Subp) = Ekind (Act_Subp)
9545 Anc_Formal := First_Formal (Anc_Subp);
9546 Act_Formal := First_Formal (Act_Subp);
9547 while Present (Anc_Formal)
9548 and then Present (Act_Formal)
9550 Anc_F_Type := Etype (Anc_Formal);
9551 Act_F_Type := Etype (Act_Formal);
9553 if Ekind (Anc_F_Type)
9554 = E_Anonymous_Access_Type
9556 Anc_F_Type := Designated_Type (Anc_F_Type);
9558 if Ekind (Act_F_Type)
9559 = E_Anonymous_Access_Type
9562 Designated_Type (Act_F_Type);
9568 Ekind (Act_F_Type) = E_Anonymous_Access_Type
9573 Anc_F_Type := Base_Type (Anc_F_Type);
9574 Act_F_Type := Base_Type (Act_F_Type);
9576 -- If the formal is controlling, then the
9577 -- the type of the actual primitive's formal
9578 -- must be derived directly or indirectly
9579 -- from the type of the ancestor primitive's
9582 if Is_Controlling_Formal (Anc_Formal) then
9583 if not Is_Tagged_Ancestor
9584 (Anc_F_Type, Act_F_Type)
9589 -- Otherwise the types of the formals must
9592 elsif Anc_F_Type /= Act_F_Type then
9596 Next_Entity (Anc_Formal);
9597 Next_Entity (Act_Formal);
9600 -- If we traversed through all of the formals
9601 -- then so far the subprograms correspond, so
9602 -- now check that any result types correspond.
9605 and then No (Act_Formal)
9607 Subprograms_Correspond := True;
9609 if Ekind (Act_Subp) = E_Function then
9610 Anc_F_Type := Etype (Anc_Subp);
9611 Act_F_Type := Etype (Act_Subp);
9613 if Ekind (Anc_F_Type)
9614 = E_Anonymous_Access_Type
9617 Designated_Type (Anc_F_Type);
9619 if Ekind (Act_F_Type)
9620 = E_Anonymous_Access_Type
9623 Designated_Type (Act_F_Type);
9625 Subprograms_Correspond := False;
9630 = E_Anonymous_Access_Type
9632 Subprograms_Correspond := False;
9635 Anc_F_Type := Base_Type (Anc_F_Type);
9636 Act_F_Type := Base_Type (Act_F_Type);
9638 -- Now either the result types must be
9639 -- the same or, if the result type is
9640 -- controlling, the result type of the
9641 -- actual primitive must descend from the
9642 -- result type of the ancestor primitive.
9644 if Subprograms_Correspond
9645 and then Anc_F_Type /= Act_F_Type
9647 Has_Controlling_Result (Anc_Subp)
9649 not Is_Tagged_Ancestor
9650 (Anc_F_Type, Act_F_Type)
9652 Subprograms_Correspond := False;
9656 -- Found a matching subprogram belonging to
9657 -- formal ancestor type, so actual subprogram
9658 -- corresponds and this violates 3.9.3(9).
9660 if Subprograms_Correspond then
9662 ("abstract subprogram & overrides " &
9663 "nonabstract subprogram of ancestor",
9671 Next_Elmt (Act_Elmt);
9675 Next_Elmt (Gen_Elmt);
9677 end Check_Abstract_Primitives;
9679 end Validate_Derived_Type_Instance;
9681 --------------------------------------
9682 -- Validate_Interface_Type_Instance --
9683 --------------------------------------
9685 procedure Validate_Interface_Type_Instance is
9687 if not Is_Interface (Act_T) then
9689 ("actual for formal interface type must be an interface",
9692 elsif Is_Limited_Type (Act_T) /= Is_Limited_Type (A_Gen_T)
9694 Is_Task_Interface (A_Gen_T) /= Is_Task_Interface (Act_T)
9696 Is_Protected_Interface (A_Gen_T) /=
9697 Is_Protected_Interface (Act_T)
9699 Is_Synchronized_Interface (A_Gen_T) /=
9700 Is_Synchronized_Interface (Act_T)
9703 ("actual for interface& does not match (RM 12.5.5(4))",
9706 end Validate_Interface_Type_Instance;
9708 ------------------------------------
9709 -- Validate_Private_Type_Instance --
9710 ------------------------------------
9712 procedure Validate_Private_Type_Instance is
9713 Formal_Discr : Entity_Id;
9714 Actual_Discr : Entity_Id;
9715 Formal_Subt : Entity_Id;
9718 if Is_Limited_Type (Act_T)
9719 and then not Is_Limited_Type (A_Gen_T)
9722 ("actual for non-limited & cannot be a limited type", Actual,
9724 Explain_Limited_Type (Act_T, Actual);
9725 Abandon_Instantiation (Actual);
9727 elsif Known_To_Have_Preelab_Init (A_Gen_T)
9728 and then not Has_Preelaborable_Initialization (Act_T)
9731 ("actual for & must have preelaborable initialization", Actual,
9734 elsif Is_Indefinite_Subtype (Act_T)
9735 and then not Is_Indefinite_Subtype (A_Gen_T)
9736 and then Ada_Version >= Ada_95
9739 ("actual for & must be a definite subtype", Actual, Gen_T);
9741 elsif not Is_Tagged_Type (Act_T)
9742 and then Is_Tagged_Type (A_Gen_T)
9745 ("actual for & must be a tagged type", Actual, Gen_T);
9747 elsif Has_Discriminants (A_Gen_T) then
9748 if not Has_Discriminants (Act_T) then
9750 ("actual for & must have discriminants", Actual, Gen_T);
9751 Abandon_Instantiation (Actual);
9753 elsif Is_Constrained (Act_T) then
9755 ("actual for & must be unconstrained", Actual, Gen_T);
9756 Abandon_Instantiation (Actual);
9759 Formal_Discr := First_Discriminant (A_Gen_T);
9760 Actual_Discr := First_Discriminant (Act_T);
9761 while Formal_Discr /= Empty loop
9762 if Actual_Discr = Empty then
9764 ("discriminants on actual do not match formal",
9766 Abandon_Instantiation (Actual);
9769 Formal_Subt := Get_Instance_Of (Etype (Formal_Discr));
9771 -- Access discriminants match if designated types do
9773 if Ekind (Base_Type (Formal_Subt)) = E_Anonymous_Access_Type
9774 and then (Ekind (Base_Type (Etype (Actual_Discr)))) =
9775 E_Anonymous_Access_Type
9778 (Designated_Type (Base_Type (Formal_Subt))) =
9779 Designated_Type (Base_Type (Etype (Actual_Discr)))
9783 elsif Base_Type (Formal_Subt) /=
9784 Base_Type (Etype (Actual_Discr))
9787 ("types of actual discriminants must match formal",
9789 Abandon_Instantiation (Actual);
9791 elsif not Subtypes_Statically_Match
9792 (Formal_Subt, Etype (Actual_Discr))
9793 and then Ada_Version >= Ada_95
9796 ("subtypes of actual discriminants must match formal",
9798 Abandon_Instantiation (Actual);
9801 Next_Discriminant (Formal_Discr);
9802 Next_Discriminant (Actual_Discr);
9805 if Actual_Discr /= Empty then
9807 ("discriminants on actual do not match formal",
9809 Abandon_Instantiation (Actual);
9816 end Validate_Private_Type_Instance;
9818 -- Start of processing for Instantiate_Type
9821 if Get_Instance_Of (A_Gen_T) /= A_Gen_T then
9822 Error_Msg_N ("duplicate instantiation of generic type", Actual);
9823 return New_List (Error);
9825 elsif not Is_Entity_Name (Actual)
9826 or else not Is_Type (Entity (Actual))
9829 ("expect valid subtype mark to instantiate &", Actual, Gen_T);
9830 Abandon_Instantiation (Actual);
9833 Act_T := Entity (Actual);
9835 -- Ada 2005 (AI-216): An Unchecked_Union subtype shall only be passed
9836 -- as a generic actual parameter if the corresponding formal type
9837 -- does not have a known_discriminant_part, or is a formal derived
9838 -- type that is an Unchecked_Union type.
9840 if Is_Unchecked_Union (Base_Type (Act_T)) then
9841 if not Has_Discriminants (A_Gen_T)
9843 (Is_Derived_Type (A_Gen_T)
9845 Is_Unchecked_Union (A_Gen_T))
9849 Error_Msg_N ("Unchecked_Union cannot be the actual for a" &
9850 " discriminated formal type", Act_T);
9855 -- Deal with fixed/floating restrictions
9857 if Is_Floating_Point_Type (Act_T) then
9858 Check_Restriction (No_Floating_Point, Actual);
9859 elsif Is_Fixed_Point_Type (Act_T) then
9860 Check_Restriction (No_Fixed_Point, Actual);
9863 -- Deal with error of using incomplete type as generic actual
9865 if Ekind (Act_T) = E_Incomplete_Type
9866 or else (Is_Class_Wide_Type (Act_T)
9868 Ekind (Root_Type (Act_T)) = E_Incomplete_Type)
9870 if Is_Class_Wide_Type (Act_T)
9871 or else No (Underlying_Type (Act_T))
9873 Error_Msg_N ("premature use of incomplete type", Actual);
9874 Abandon_Instantiation (Actual);
9876 Act_T := Full_View (Act_T);
9877 Set_Entity (Actual, Act_T);
9879 if Has_Private_Component (Act_T) then
9881 ("premature use of type with private component", Actual);
9885 -- Deal with error of premature use of private type as generic actual
9887 elsif Is_Private_Type (Act_T)
9888 and then Is_Private_Type (Base_Type (Act_T))
9889 and then not Is_Generic_Type (Act_T)
9890 and then not Is_Derived_Type (Act_T)
9891 and then No (Full_View (Root_Type (Act_T)))
9893 Error_Msg_N ("premature use of private type", Actual);
9895 elsif Has_Private_Component (Act_T) then
9897 ("premature use of type with private component", Actual);
9900 Set_Instance_Of (A_Gen_T, Act_T);
9902 -- If the type is generic, the class-wide type may also be used
9904 if Is_Tagged_Type (A_Gen_T)
9905 and then Is_Tagged_Type (Act_T)
9906 and then not Is_Class_Wide_Type (A_Gen_T)
9908 Set_Instance_Of (Class_Wide_Type (A_Gen_T),
9909 Class_Wide_Type (Act_T));
9912 if not Is_Abstract_Type (A_Gen_T)
9913 and then Is_Abstract_Type (Act_T)
9916 ("actual of non-abstract formal cannot be abstract", Actual);
9919 -- A generic scalar type is a first subtype for which we generate
9920 -- an anonymous base type. Indicate that the instance of this base
9921 -- is the base type of the actual.
9923 if Is_Scalar_Type (A_Gen_T) then
9924 Set_Instance_Of (Etype (A_Gen_T), Etype (Act_T));
9928 if Error_Posted (Act_T) then
9932 when N_Formal_Private_Type_Definition =>
9933 Validate_Private_Type_Instance;
9935 when N_Formal_Derived_Type_Definition =>
9936 Validate_Derived_Type_Instance;
9938 when N_Formal_Discrete_Type_Definition =>
9939 if not Is_Discrete_Type (Act_T) then
9941 ("expect discrete type in instantiation of&",
9943 Abandon_Instantiation (Actual);
9946 when N_Formal_Signed_Integer_Type_Definition =>
9947 if not Is_Signed_Integer_Type (Act_T) then
9949 ("expect signed integer type in instantiation of&",
9951 Abandon_Instantiation (Actual);
9954 when N_Formal_Modular_Type_Definition =>
9955 if not Is_Modular_Integer_Type (Act_T) then
9957 ("expect modular type in instantiation of &",
9959 Abandon_Instantiation (Actual);
9962 when N_Formal_Floating_Point_Definition =>
9963 if not Is_Floating_Point_Type (Act_T) then
9965 ("expect float type in instantiation of &", Actual, Gen_T);
9966 Abandon_Instantiation (Actual);
9969 when N_Formal_Ordinary_Fixed_Point_Definition =>
9970 if not Is_Ordinary_Fixed_Point_Type (Act_T) then
9972 ("expect ordinary fixed point type in instantiation of &",
9974 Abandon_Instantiation (Actual);
9977 when N_Formal_Decimal_Fixed_Point_Definition =>
9978 if not Is_Decimal_Fixed_Point_Type (Act_T) then
9980 ("expect decimal type in instantiation of &",
9982 Abandon_Instantiation (Actual);
9985 when N_Array_Type_Definition =>
9986 Validate_Array_Type_Instance;
9988 when N_Access_To_Object_Definition =>
9989 Validate_Access_Type_Instance;
9991 when N_Access_Function_Definition |
9992 N_Access_Procedure_Definition =>
9993 Validate_Access_Subprogram_Instance;
9995 when N_Record_Definition =>
9996 Validate_Interface_Type_Instance;
9998 when N_Derived_Type_Definition =>
9999 Validate_Derived_Interface_Type_Instance;
10002 raise Program_Error;
10007 Subt := New_Copy (Gen_T);
10009 -- Use adjusted sloc of subtype name as the location for other nodes in
10010 -- the subtype declaration.
10012 Loc := Sloc (Subt);
10015 Make_Subtype_Declaration (Loc,
10016 Defining_Identifier => Subt,
10017 Subtype_Indication => New_Reference_To (Act_T, Loc));
10019 if Is_Private_Type (Act_T) then
10020 Set_Has_Private_View (Subtype_Indication (Decl_Node));
10022 elsif Is_Access_Type (Act_T)
10023 and then Is_Private_Type (Designated_Type (Act_T))
10025 Set_Has_Private_View (Subtype_Indication (Decl_Node));
10028 Decl_Nodes := New_List (Decl_Node);
10030 -- Flag actual derived types so their elaboration produces the
10031 -- appropriate renamings for the primitive operations of the ancestor.
10032 -- Flag actual for formal private types as well, to determine whether
10033 -- operations in the private part may override inherited operations.
10034 -- If the formal has an interface list, the ancestor is not the
10035 -- parent, but the analyzed formal that includes the interface
10036 -- operations of all its progenitors.
10038 if Nkind (Def) = N_Formal_Derived_Type_Definition then
10039 if Present (Interface_List (Def)) then
10040 Set_Generic_Parent_Type (Decl_Node, A_Gen_T);
10042 Set_Generic_Parent_Type (Decl_Node, Ancestor);
10045 elsif Nkind (Def) = N_Formal_Private_Type_Definition then
10046 Set_Generic_Parent_Type (Decl_Node, Ancestor);
10049 -- If the actual is a synchronized type that implements an interface,
10050 -- the primitive operations are attached to the corresponding record,
10051 -- and we have to treat it as an additional generic actual, so that its
10052 -- primitive operations become visible in the instance. The task or
10053 -- protected type itself does not carry primitive operations.
10055 if Is_Concurrent_Type (Act_T)
10056 and then Is_Tagged_Type (Act_T)
10057 and then Present (Corresponding_Record_Type (Act_T))
10058 and then Present (Ancestor)
10059 and then Is_Interface (Ancestor)
10062 Corr_Rec : constant Entity_Id :=
10063 Corresponding_Record_Type (Act_T);
10064 New_Corr : Entity_Id;
10065 Corr_Decl : Node_Id;
10068 New_Corr := Make_Defining_Identifier (Loc,
10069 Chars => New_Internal_Name ('S'));
10071 Make_Subtype_Declaration (Loc,
10072 Defining_Identifier => New_Corr,
10073 Subtype_Indication =>
10074 New_Reference_To (Corr_Rec, Loc));
10075 Append_To (Decl_Nodes, Corr_Decl);
10077 if Ekind (Act_T) = E_Task_Type then
10078 Set_Ekind (Subt, E_Task_Subtype);
10080 Set_Ekind (Subt, E_Protected_Subtype);
10083 Set_Corresponding_Record_Type (Subt, Corr_Rec);
10084 Set_Generic_Parent_Type (Corr_Decl, Ancestor);
10085 Set_Generic_Parent_Type (Decl_Node, Empty);
10090 end Instantiate_Type;
10092 -----------------------
10093 -- Is_Generic_Formal --
10094 -----------------------
10096 function Is_Generic_Formal (E : Entity_Id) return Boolean is
10102 Kind := Nkind (Parent (E));
10104 Kind = N_Formal_Object_Declaration
10105 or else Kind = N_Formal_Package_Declaration
10106 or else Kind = N_Formal_Type_Declaration
10108 (Is_Formal_Subprogram (E)
10110 Nkind (Parent (Parent (E))) in
10111 N_Formal_Subprogram_Declaration);
10113 end Is_Generic_Formal;
10115 ---------------------
10116 -- Is_In_Main_Unit --
10117 ---------------------
10119 function Is_In_Main_Unit (N : Node_Id) return Boolean is
10120 Unum : constant Unit_Number_Type := Get_Source_Unit (N);
10121 Current_Unit : Node_Id;
10124 if Unum = Main_Unit then
10127 -- If the current unit is a subunit then it is either the main unit or
10128 -- is being compiled as part of the main unit.
10130 elsif Nkind (N) = N_Compilation_Unit then
10131 return Nkind (Unit (N)) = N_Subunit;
10134 Current_Unit := Parent (N);
10135 while Present (Current_Unit)
10136 and then Nkind (Current_Unit) /= N_Compilation_Unit
10138 Current_Unit := Parent (Current_Unit);
10141 -- The instantiation node is in the main unit, or else the current node
10142 -- (perhaps as the result of nested instantiations) is in the main unit,
10143 -- or in the declaration of the main unit, which in this last case must
10146 return Unum = Main_Unit
10147 or else Current_Unit = Cunit (Main_Unit)
10148 or else Current_Unit = Library_Unit (Cunit (Main_Unit))
10149 or else (Present (Library_Unit (Current_Unit))
10150 and then Is_In_Main_Unit (Library_Unit (Current_Unit)));
10151 end Is_In_Main_Unit;
10153 ----------------------------
10154 -- Load_Parent_Of_Generic --
10155 ----------------------------
10157 procedure Load_Parent_Of_Generic
10160 Body_Optional : Boolean := False)
10162 Comp_Unit : constant Node_Id := Cunit (Get_Source_Unit (Spec));
10163 Save_Style_Check : constant Boolean := Style_Check;
10164 True_Parent : Node_Id;
10165 Inst_Node : Node_Id;
10167 Previous_Instances : constant Elist_Id := New_Elmt_List;
10169 procedure Collect_Previous_Instances (Decls : List_Id);
10170 -- Collect all instantiations in the given list of declarations,
10171 -- that precedes the generic that we need to load. If the bodies
10172 -- of these instantiations are available, we must analyze them,
10173 -- to ensure that the public symbols generated are the same when
10174 -- the unit is compiled to generate code, and when it is compiled
10175 -- in the context of the unit that needs a particular nested instance.
10177 --------------------------------
10178 -- Collect_Previous_Instances --
10179 --------------------------------
10181 procedure Collect_Previous_Instances (Decls : List_Id) is
10185 Decl := First (Decls);
10186 while Present (Decl) loop
10187 if Sloc (Decl) >= Sloc (Inst_Node) then
10190 elsif Nkind (Decl) = N_Package_Instantiation then
10191 Append_Elmt (Decl, Previous_Instances);
10193 elsif Nkind (Decl) = N_Package_Declaration then
10194 Collect_Previous_Instances
10195 (Visible_Declarations (Specification (Decl)));
10196 Collect_Previous_Instances
10197 (Private_Declarations (Specification (Decl)));
10199 elsif Nkind (Decl) = N_Package_Body then
10200 Collect_Previous_Instances (Declarations (Decl));
10205 end Collect_Previous_Instances;
10207 -- Start of processing for Load_Parent_Of_Generic
10210 if not In_Same_Source_Unit (N, Spec)
10211 or else Nkind (Unit (Comp_Unit)) = N_Package_Declaration
10212 or else (Nkind (Unit (Comp_Unit)) = N_Package_Body
10213 and then not Is_In_Main_Unit (Spec))
10215 -- Find body of parent of spec, and analyze it. A special case arises
10216 -- when the parent is an instantiation, that is to say when we are
10217 -- currently instantiating a nested generic. In that case, there is
10218 -- no separate file for the body of the enclosing instance. Instead,
10219 -- the enclosing body must be instantiated as if it were a pending
10220 -- instantiation, in order to produce the body for the nested generic
10221 -- we require now. Note that in that case the generic may be defined
10222 -- in a package body, the instance defined in the same package body,
10223 -- and the original enclosing body may not be in the main unit.
10225 Inst_Node := Empty;
10227 True_Parent := Parent (Spec);
10228 while Present (True_Parent)
10229 and then Nkind (True_Parent) /= N_Compilation_Unit
10231 if Nkind (True_Parent) = N_Package_Declaration
10233 Nkind (Original_Node (True_Parent)) = N_Package_Instantiation
10235 -- Parent is a compilation unit that is an instantiation.
10236 -- Instantiation node has been replaced with package decl.
10238 Inst_Node := Original_Node (True_Parent);
10241 elsif Nkind (True_Parent) = N_Package_Declaration
10242 and then Present (Generic_Parent (Specification (True_Parent)))
10243 and then Nkind (Parent (True_Parent)) /= N_Compilation_Unit
10245 -- Parent is an instantiation within another specification.
10246 -- Declaration for instance has been inserted before original
10247 -- instantiation node. A direct link would be preferable?
10249 Inst_Node := Next (True_Parent);
10250 while Present (Inst_Node)
10251 and then Nkind (Inst_Node) /= N_Package_Instantiation
10256 -- If the instance appears within a generic, and the generic
10257 -- unit is defined within a formal package of the enclosing
10258 -- generic, there is no generic body available, and none
10259 -- needed. A more precise test should be used ???
10261 if No (Inst_Node) then
10268 True_Parent := Parent (True_Parent);
10272 -- Case where we are currently instantiating a nested generic
10274 if Present (Inst_Node) then
10275 if Nkind (Parent (True_Parent)) = N_Compilation_Unit then
10277 -- Instantiation node and declaration of instantiated package
10278 -- were exchanged when only the declaration was needed.
10279 -- Restore instantiation node before proceeding with body.
10281 Set_Unit (Parent (True_Parent), Inst_Node);
10284 -- Now complete instantiation of enclosing body, if it appears
10285 -- in some other unit. If it appears in the current unit, the
10286 -- body will have been instantiated already.
10288 if No (Corresponding_Body (Instance_Spec (Inst_Node))) then
10290 -- We need to determine the expander mode to instantiate the
10291 -- enclosing body. Because the generic body we need may use
10292 -- global entities declared in the enclosing package (including
10293 -- aggregates) it is in general necessary to compile this body
10294 -- with expansion enabled. The exception is if we are within a
10295 -- generic package, in which case the usual generic rule
10299 Exp_Status : Boolean := True;
10303 -- Loop through scopes looking for generic package
10305 Scop := Scope (Defining_Entity (Instance_Spec (Inst_Node)));
10306 while Present (Scop)
10307 and then Scop /= Standard_Standard
10309 if Ekind (Scop) = E_Generic_Package then
10310 Exp_Status := False;
10314 Scop := Scope (Scop);
10317 -- Collect previous instantiations in the unit that
10318 -- contains the desired generic,
10320 if Nkind (Parent (True_Parent)) /= N_Compilation_Unit
10321 and then not Body_Optional
10328 Par := Parent (Inst_Node);
10329 while Present (Par) loop
10330 exit when Nkind (Parent (Par)) = N_Compilation_Unit;
10331 Par := Parent (Par);
10334 pragma Assert (Present (Par));
10336 if Nkind (Par) = N_Package_Body then
10337 Collect_Previous_Instances (Declarations (Par));
10339 elsif Nkind (Par) = N_Package_Declaration then
10340 Collect_Previous_Instances
10341 (Visible_Declarations (Specification (Par)));
10342 Collect_Previous_Instances
10343 (Private_Declarations (Specification (Par)));
10346 -- Enclosing unit is a subprogram body, In this
10347 -- case all instance bodies are processed in order
10348 -- and there is no need to collect them separately.
10353 Decl := First_Elmt (Previous_Instances);
10354 while Present (Decl) loop
10355 Instantiate_Package_Body
10357 ((Inst_Node => Node (Decl),
10359 Instance_Spec (Node (Decl)),
10360 Expander_Status => Exp_Status,
10361 Current_Sem_Unit =>
10362 Get_Code_Unit (Sloc (Node (Decl))),
10363 Scope_Suppress => Scope_Suppress,
10364 Local_Suppress_Stack_Top =>
10365 Local_Suppress_Stack_Top)),
10366 Body_Optional => True);
10373 Instantiate_Package_Body
10375 ((Inst_Node => Inst_Node,
10376 Act_Decl => True_Parent,
10377 Expander_Status => Exp_Status,
10378 Current_Sem_Unit =>
10379 Get_Code_Unit (Sloc (Inst_Node)),
10380 Scope_Suppress => Scope_Suppress,
10381 Local_Suppress_Stack_Top =>
10382 Local_Suppress_Stack_Top)),
10383 Body_Optional => Body_Optional);
10387 -- Case where we are not instantiating a nested generic
10390 Opt.Style_Check := False;
10391 Expander_Mode_Save_And_Set (True);
10392 Load_Needed_Body (Comp_Unit, OK);
10393 Opt.Style_Check := Save_Style_Check;
10394 Expander_Mode_Restore;
10397 and then Unit_Requires_Body (Defining_Entity (Spec))
10398 and then not Body_Optional
10401 Bname : constant Unit_Name_Type :=
10402 Get_Body_Name (Get_Unit_Name (Unit (Comp_Unit)));
10405 Error_Msg_Unit_1 := Bname;
10406 Error_Msg_N ("this instantiation requires$!", N);
10407 Error_Msg_File_1 := Get_File_Name (Bname, Subunit => False);
10408 Error_Msg_N ("\but file{ was not found!", N);
10409 raise Unrecoverable_Error;
10415 -- If loading parent of the generic caused an instantiation circularity,
10416 -- we abandon compilation at this point, because otherwise in some cases
10417 -- we get into trouble with infinite recursions after this point.
10419 if Circularity_Detected then
10420 raise Unrecoverable_Error;
10422 end Load_Parent_Of_Generic;
10424 -----------------------
10425 -- Move_Freeze_Nodes --
10426 -----------------------
10428 procedure Move_Freeze_Nodes
10429 (Out_Of : Entity_Id;
10434 Next_Decl : Node_Id;
10435 Next_Node : Node_Id := After;
10438 function Is_Outer_Type (T : Entity_Id) return Boolean;
10439 -- Check whether entity is declared in a scope external to that
10440 -- of the generic unit.
10442 -------------------
10443 -- Is_Outer_Type --
10444 -------------------
10446 function Is_Outer_Type (T : Entity_Id) return Boolean is
10447 Scop : Entity_Id := Scope (T);
10450 if Scope_Depth (Scop) < Scope_Depth (Out_Of) then
10454 while Scop /= Standard_Standard loop
10455 if Scop = Out_Of then
10458 Scop := Scope (Scop);
10466 -- Start of processing for Move_Freeze_Nodes
10473 -- First remove the freeze nodes that may appear before all other
10477 while Present (Decl)
10478 and then Nkind (Decl) = N_Freeze_Entity
10479 and then Is_Outer_Type (Entity (Decl))
10481 Decl := Remove_Head (L);
10482 Insert_After (Next_Node, Decl);
10483 Set_Analyzed (Decl, False);
10488 -- Next scan the list of declarations and remove each freeze node that
10489 -- appears ahead of the current node.
10491 while Present (Decl) loop
10492 while Present (Next (Decl))
10493 and then Nkind (Next (Decl)) = N_Freeze_Entity
10494 and then Is_Outer_Type (Entity (Next (Decl)))
10496 Next_Decl := Remove_Next (Decl);
10497 Insert_After (Next_Node, Next_Decl);
10498 Set_Analyzed (Next_Decl, False);
10499 Next_Node := Next_Decl;
10502 -- If the declaration is a nested package or concurrent type, then
10503 -- recurse. Nested generic packages will have been processed from the
10506 if Nkind (Decl) = N_Package_Declaration then
10507 Spec := Specification (Decl);
10509 elsif Nkind (Decl) = N_Task_Type_Declaration then
10510 Spec := Task_Definition (Decl);
10512 elsif Nkind (Decl) = N_Protected_Type_Declaration then
10513 Spec := Protected_Definition (Decl);
10519 if Present (Spec) then
10520 Move_Freeze_Nodes (Out_Of, Next_Node,
10521 Visible_Declarations (Spec));
10522 Move_Freeze_Nodes (Out_Of, Next_Node,
10523 Private_Declarations (Spec));
10528 end Move_Freeze_Nodes;
10534 function Next_Assoc (E : Assoc_Ptr) return Assoc_Ptr is
10536 return Generic_Renamings.Table (E).Next_In_HTable;
10539 ------------------------
10540 -- Preanalyze_Actuals --
10541 ------------------------
10543 procedure Pre_Analyze_Actuals (N : Node_Id) is
10546 Errs : constant Int := Serious_Errors_Detected;
10549 Assoc := First (Generic_Associations (N));
10550 while Present (Assoc) loop
10551 if Nkind (Assoc) /= N_Others_Choice then
10552 Act := Explicit_Generic_Actual_Parameter (Assoc);
10554 -- Within a nested instantiation, a defaulted actual is an empty
10555 -- association, so nothing to analyze. If the subprogram actual
10556 -- isan attribute, analyze prefix only, because actual is not a
10557 -- complete attribute reference.
10559 -- If actual is an allocator, analyze expression only. The full
10560 -- analysis can generate code, and if instance is a compilation
10561 -- unit we have to wait until the package instance is installed
10562 -- to have a proper place to insert this code.
10564 -- String literals may be operators, but at this point we do not
10565 -- know whether the actual is a formal subprogram or a string.
10570 elsif Nkind (Act) = N_Attribute_Reference then
10571 Analyze (Prefix (Act));
10573 elsif Nkind (Act) = N_Explicit_Dereference then
10574 Analyze (Prefix (Act));
10576 elsif Nkind (Act) = N_Allocator then
10578 Expr : constant Node_Id := Expression (Act);
10581 if Nkind (Expr) = N_Subtype_Indication then
10582 Analyze (Subtype_Mark (Expr));
10584 -- Analyze separately each discriminant constraint,
10585 -- when given with a named association.
10591 Constr := First (Constraints (Constraint (Expr)));
10592 while Present (Constr) loop
10593 if Nkind (Constr) = N_Discriminant_Association then
10594 Analyze (Expression (Constr));
10608 elsif Nkind (Act) /= N_Operator_Symbol then
10612 if Errs /= Serious_Errors_Detected then
10613 Abandon_Instantiation (Act);
10619 end Pre_Analyze_Actuals;
10621 -------------------
10622 -- Remove_Parent --
10623 -------------------
10625 procedure Remove_Parent (In_Body : Boolean := False) is
10626 S : Entity_Id := Current_Scope;
10632 -- After child instantiation is complete, remove from scope stack the
10633 -- extra copy of the current scope, and then remove parent instances.
10635 if not In_Body then
10638 while Current_Scope /= S loop
10639 P := Current_Scope;
10640 End_Package_Scope (Current_Scope);
10642 if In_Open_Scopes (P) then
10643 E := First_Entity (P);
10645 while Present (E) loop
10646 Set_Is_Immediately_Visible (E, True);
10650 if Is_Generic_Instance (Current_Scope)
10651 and then P /= Current_Scope
10653 -- We are within an instance of some sibling. Retain
10654 -- visibility of parent, for proper subsequent cleanup,
10655 -- and reinstall private declarations as well.
10657 Set_In_Private_Part (P);
10658 Install_Private_Declarations (P);
10661 -- If the ultimate parent is a top-level unit recorded in
10662 -- Instance_Parent_Unit, then reset its visibility to what
10663 -- it was before instantiation. (It's not clear what the
10664 -- purpose is of testing whether Scope (P) is In_Open_Scopes,
10665 -- but that test was present before the ultimate parent test
10668 elsif not In_Open_Scopes (Scope (P))
10669 or else (P = Instance_Parent_Unit
10670 and then not Parent_Unit_Visible)
10672 Set_Is_Immediately_Visible (P, False);
10676 -- Reset visibility of entities in the enclosing scope
10678 Set_Is_Hidden_Open_Scope (Current_Scope, False);
10679 Hidden := First_Elmt (Hidden_Entities);
10681 while Present (Hidden) loop
10682 Set_Is_Immediately_Visible (Node (Hidden), True);
10683 Next_Elmt (Hidden);
10687 -- Each body is analyzed separately, and there is no context
10688 -- that needs preserving from one body instance to the next,
10689 -- so remove all parent scopes that have been installed.
10691 while Present (S) loop
10692 End_Package_Scope (S);
10693 Set_Is_Immediately_Visible (S, False);
10694 S := Current_Scope;
10695 exit when S = Standard_Standard;
10704 procedure Restore_Env is
10705 Saved : Instance_Env renames Instance_Envs.Table (Instance_Envs.Last);
10708 if No (Current_Instantiated_Parent.Act_Id) then
10710 -- Restore environment after subprogram inlining
10712 Restore_Private_Views (Empty);
10715 Current_Instantiated_Parent := Saved.Instantiated_Parent;
10716 Exchanged_Views := Saved.Exchanged_Views;
10717 Hidden_Entities := Saved.Hidden_Entities;
10718 Current_Sem_Unit := Saved.Current_Sem_Unit;
10719 Parent_Unit_Visible := Saved.Parent_Unit_Visible;
10720 Instance_Parent_Unit := Saved.Instance_Parent_Unit;
10722 Restore_Opt_Config_Switches (Saved.Switches);
10724 Instance_Envs.Decrement_Last;
10727 ---------------------------
10728 -- Restore_Private_Views --
10729 ---------------------------
10731 procedure Restore_Private_Views
10732 (Pack_Id : Entity_Id;
10733 Is_Package : Boolean := True)
10738 Dep_Elmt : Elmt_Id;
10741 procedure Restore_Nested_Formal (Formal : Entity_Id);
10742 -- Hide the generic formals of formal packages declared with box
10743 -- which were reachable in the current instantiation.
10745 ---------------------------
10746 -- Restore_Nested_Formal --
10747 ---------------------------
10749 procedure Restore_Nested_Formal (Formal : Entity_Id) is
10753 if Present (Renamed_Object (Formal))
10754 and then Denotes_Formal_Package (Renamed_Object (Formal), True)
10758 elsif Present (Associated_Formal_Package (Formal)) then
10760 Ent := First_Entity (Formal);
10761 while Present (Ent) loop
10762 exit when Ekind (Ent) = E_Package
10763 and then Renamed_Entity (Ent) = Renamed_Entity (Formal);
10765 Set_Is_Hidden (Ent);
10766 Set_Is_Potentially_Use_Visible (Ent, False);
10768 -- If package, then recurse
10770 if Ekind (Ent) = E_Package then
10771 Restore_Nested_Formal (Ent);
10777 end Restore_Nested_Formal;
10779 -- Start of processing for Restore_Private_Views
10782 M := First_Elmt (Exchanged_Views);
10783 while Present (M) loop
10786 -- Subtypes of types whose views have been exchanged, and that
10787 -- are defined within the instance, were not on the list of
10788 -- Private_Dependents on entry to the instance, so they have to
10789 -- be exchanged explicitly now, in order to remain consistent with
10790 -- the view of the parent type.
10792 if Ekind (Typ) = E_Private_Type
10793 or else Ekind (Typ) = E_Limited_Private_Type
10794 or else Ekind (Typ) = E_Record_Type_With_Private
10796 Dep_Elmt := First_Elmt (Private_Dependents (Typ));
10797 while Present (Dep_Elmt) loop
10798 Dep_Typ := Node (Dep_Elmt);
10800 if Scope (Dep_Typ) = Pack_Id
10801 and then Present (Full_View (Dep_Typ))
10803 Replace_Elmt (Dep_Elmt, Full_View (Dep_Typ));
10804 Exchange_Declarations (Dep_Typ);
10807 Next_Elmt (Dep_Elmt);
10811 Exchange_Declarations (Node (M));
10815 if No (Pack_Id) then
10819 -- Make the generic formal parameters private, and make the formal
10820 -- types into subtypes of the actuals again.
10822 E := First_Entity (Pack_Id);
10823 while Present (E) loop
10824 Set_Is_Hidden (E, True);
10827 and then Nkind (Parent (E)) = N_Subtype_Declaration
10829 Set_Is_Generic_Actual_Type (E, False);
10831 -- An unusual case of aliasing: the actual may also be directly
10832 -- visible in the generic, and be private there, while it is fully
10833 -- visible in the context of the instance. The internal subtype is
10834 -- private in the instance, but has full visibility like its
10835 -- parent in the enclosing scope. This enforces the invariant that
10836 -- the privacy status of all private dependents of a type coincide
10837 -- with that of the parent type. This can only happen when a
10838 -- generic child unit is instantiated within sibling.
10840 if Is_Private_Type (E)
10841 and then not Is_Private_Type (Etype (E))
10843 Exchange_Declarations (E);
10846 elsif Ekind (E) = E_Package then
10848 -- The end of the renaming list is the renaming of the generic
10849 -- package itself. If the instance is a subprogram, all entities
10850 -- in the corresponding package are renamings. If this entity is
10851 -- a formal package, make its own formals private as well. The
10852 -- actual in this case is itself the renaming of an instantation.
10853 -- If the entity is not a package renaming, it is the entity
10854 -- created to validate formal package actuals: ignore.
10856 -- If the actual is itself a formal package for the enclosing
10857 -- generic, or the actual for such a formal package, it remains
10858 -- visible on exit from the instance, and therefore nothing
10859 -- needs to be done either, except to keep it accessible.
10862 and then Renamed_Object (E) = Pack_Id
10866 elsif Nkind (Parent (E)) /= N_Package_Renaming_Declaration then
10869 elsif Denotes_Formal_Package (Renamed_Object (E), True) then
10870 Set_Is_Hidden (E, False);
10874 Act_P : constant Entity_Id := Renamed_Object (E);
10878 Id := First_Entity (Act_P);
10880 and then Id /= First_Private_Entity (Act_P)
10882 exit when Ekind (Id) = E_Package
10883 and then Renamed_Object (Id) = Act_P;
10885 Set_Is_Hidden (Id, True);
10886 Set_Is_Potentially_Use_Visible (Id, In_Use (Act_P));
10888 if Ekind (Id) = E_Package then
10889 Restore_Nested_Formal (Id);
10900 end Restore_Private_Views;
10907 (Gen_Unit : Entity_Id;
10908 Act_Unit : Entity_Id)
10912 Set_Instance_Env (Gen_Unit, Act_Unit);
10915 ----------------------------
10916 -- Save_Global_References --
10917 ----------------------------
10919 procedure Save_Global_References (N : Node_Id) is
10920 Gen_Scope : Entity_Id;
10924 function Is_Global (E : Entity_Id) return Boolean;
10925 -- Check whether entity is defined outside of generic unit. Examine the
10926 -- scope of an entity, and the scope of the scope, etc, until we find
10927 -- either Standard, in which case the entity is global, or the generic
10928 -- unit itself, which indicates that the entity is local. If the entity
10929 -- is the generic unit itself, as in the case of a recursive call, or
10930 -- the enclosing generic unit, if different from the current scope, then
10931 -- it is local as well, because it will be replaced at the point of
10932 -- instantiation. On the other hand, if it is a reference to a child
10933 -- unit of a common ancestor, which appears in an instantiation, it is
10934 -- global because it is used to denote a specific compilation unit at
10935 -- the time the instantiations will be analyzed.
10937 procedure Reset_Entity (N : Node_Id);
10938 -- Save semantic information on global entity, so that it is not
10939 -- resolved again at instantiation time.
10941 procedure Save_Entity_Descendants (N : Node_Id);
10942 -- Apply Save_Global_References to the two syntactic descendants of
10943 -- non-terminal nodes that carry an Associated_Node and are processed
10944 -- through Reset_Entity. Once the global entity (if any) has been
10945 -- captured together with its type, only two syntactic descendants need
10946 -- to be traversed to complete the processing of the tree rooted at N.
10947 -- This applies to Selected_Components, Expanded_Names, and to Operator
10948 -- nodes. N can also be a character literal, identifier, or operator
10949 -- symbol node, but the call has no effect in these cases.
10951 procedure Save_Global_Defaults (N1, N2 : Node_Id);
10952 -- Default actuals in nested instances must be handled specially
10953 -- because there is no link to them from the original tree. When an
10954 -- actual subprogram is given by a default, we add an explicit generic
10955 -- association for it in the instantiation node. When we save the
10956 -- global references on the name of the instance, we recover the list
10957 -- of generic associations, and add an explicit one to the original
10958 -- generic tree, through which a global actual can be preserved.
10959 -- Similarly, if a child unit is instantiated within a sibling, in the
10960 -- context of the parent, we must preserve the identifier of the parent
10961 -- so that it can be properly resolved in a subsequent instantiation.
10963 procedure Save_Global_Descendant (D : Union_Id);
10964 -- Apply Save_Global_References recursively to the descendents of the
10967 procedure Save_References (N : Node_Id);
10968 -- This is the recursive procedure that does the work, once the
10969 -- enclosing generic scope has been established.
10975 function Is_Global (E : Entity_Id) return Boolean is
10976 Se : Entity_Id := Scope (E);
10978 function Is_Instance_Node (Decl : Node_Id) return Boolean;
10979 -- Determine whether the parent node of a reference to a child unit
10980 -- denotes an instantiation or a formal package, in which case the
10981 -- reference to the child unit is global, even if it appears within
10982 -- the current scope (e.g. when the instance appears within the body
10983 -- of an ancestor).
10985 ----------------------
10986 -- Is_Instance_Node --
10987 ----------------------
10989 function Is_Instance_Node (Decl : Node_Id) return Boolean is
10991 return (Nkind (Decl) in N_Generic_Instantiation
10993 Nkind (Original_Node (Decl)) = N_Formal_Package_Declaration);
10994 end Is_Instance_Node;
10996 -- Start of processing for Is_Global
10999 if E = Gen_Scope then
11002 elsif E = Standard_Standard then
11005 elsif Is_Child_Unit (E)
11006 and then (Is_Instance_Node (Parent (N2))
11007 or else (Nkind (Parent (N2)) = N_Expanded_Name
11008 and then N2 = Selector_Name (Parent (N2))
11009 and then Is_Instance_Node (Parent (Parent (N2)))))
11014 while Se /= Gen_Scope loop
11015 if Se = Standard_Standard then
11030 procedure Reset_Entity (N : Node_Id) is
11032 procedure Set_Global_Type (N : Node_Id; N2 : Node_Id);
11033 -- If the type of N2 is global to the generic unit. Save
11034 -- the type in the generic node.
11036 function Top_Ancestor (E : Entity_Id) return Entity_Id;
11037 -- Find the ultimate ancestor of the current unit. If it is
11038 -- not a generic unit, then the name of the current unit
11039 -- in the prefix of an expanded name must be replaced with
11040 -- its generic homonym to ensure that it will be properly
11041 -- resolved in an instance.
11043 ---------------------
11044 -- Set_Global_Type --
11045 ---------------------
11047 procedure Set_Global_Type (N : Node_Id; N2 : Node_Id) is
11048 Typ : constant Entity_Id := Etype (N2);
11051 Set_Etype (N, Typ);
11053 if Entity (N) /= N2
11054 and then Has_Private_View (Entity (N))
11056 -- If the entity of N is not the associated node, this is
11057 -- a nested generic and it has an associated node as well,
11058 -- whose type is already the full view (see below). Indicate
11059 -- that the original node has a private view.
11061 Set_Has_Private_View (N);
11064 -- If not a private type, nothing else to do
11066 if not Is_Private_Type (Typ) then
11067 if Is_Array_Type (Typ)
11068 and then Is_Private_Type (Component_Type (Typ))
11070 Set_Has_Private_View (N);
11073 -- If it is a derivation of a private type in a context where
11074 -- no full view is needed, nothing to do either.
11076 elsif No (Full_View (Typ)) and then Typ /= Etype (Typ) then
11079 -- Otherwise mark the type for flipping and use the full_view
11083 Set_Has_Private_View (N);
11085 if Present (Full_View (Typ)) then
11086 Set_Etype (N2, Full_View (Typ));
11089 end Set_Global_Type;
11095 function Top_Ancestor (E : Entity_Id) return Entity_Id is
11096 Par : Entity_Id := E;
11099 while Is_Child_Unit (Par) loop
11100 Par := Scope (Par);
11106 -- Start of processing for Reset_Entity
11109 N2 := Get_Associated_Node (N);
11112 if Present (E) then
11113 if Is_Global (E) then
11114 Set_Global_Type (N, N2);
11116 elsif Nkind (N) = N_Op_Concat
11117 and then Is_Generic_Type (Etype (N2))
11119 (Base_Type (Etype (Right_Opnd (N2))) = Etype (N2)
11120 or else Base_Type (Etype (Left_Opnd (N2))) = Etype (N2))
11121 and then Is_Intrinsic_Subprogram (E)
11126 -- Entity is local. Mark generic node as unresolved.
11127 -- Note that now it does not have an entity.
11129 Set_Associated_Node (N, Empty);
11130 Set_Etype (N, Empty);
11133 if Nkind (Parent (N)) in N_Generic_Instantiation
11134 and then N = Name (Parent (N))
11136 Save_Global_Defaults (Parent (N), Parent (N2));
11139 elsif Nkind (Parent (N)) = N_Selected_Component
11140 and then Nkind (Parent (N2)) = N_Expanded_Name
11142 if Is_Global (Entity (Parent (N2))) then
11143 Change_Selected_Component_To_Expanded_Name (Parent (N));
11144 Set_Associated_Node (Parent (N), Parent (N2));
11145 Set_Global_Type (Parent (N), Parent (N2));
11146 Save_Entity_Descendants (N);
11148 -- If this is a reference to the current generic entity, replace
11149 -- by the name of the generic homonym of the current package. This
11150 -- is because in an instantiation Par.P.Q will not resolve to the
11151 -- name of the instance, whose enclosing scope is not necessarily
11152 -- Par. We use the generic homonym rather that the name of the
11153 -- generic itself, because it may be hidden by a local
11156 elsif In_Open_Scopes (Entity (Parent (N2)))
11158 Is_Generic_Unit (Top_Ancestor (Entity (Prefix (Parent (N2)))))
11160 if Ekind (Entity (Parent (N2))) = E_Generic_Package then
11161 Rewrite (Parent (N),
11162 Make_Identifier (Sloc (N),
11164 Chars (Generic_Homonym (Entity (Parent (N2))))));
11166 Rewrite (Parent (N),
11167 Make_Identifier (Sloc (N),
11168 Chars => Chars (Selector_Name (Parent (N2)))));
11172 if Nkind (Parent (Parent (N))) in N_Generic_Instantiation
11173 and then Parent (N) = Name (Parent (Parent (N)))
11175 Save_Global_Defaults
11176 (Parent (Parent (N)), Parent (Parent ((N2))));
11179 -- A selected component may denote a static constant that has been
11180 -- folded. If the static constant is global to the generic, capture
11181 -- its value. Otherwise the folding will happen in any instantiation,
11183 elsif Nkind (Parent (N)) = N_Selected_Component
11184 and then (Nkind (Parent (N2)) = N_Integer_Literal
11185 or else Nkind (Parent (N2)) = N_Real_Literal)
11187 if Present (Entity (Original_Node (Parent (N2))))
11188 and then Is_Global (Entity (Original_Node (Parent (N2))))
11190 Rewrite (Parent (N), New_Copy (Parent (N2)));
11191 Set_Analyzed (Parent (N), False);
11197 -- A selected component may be transformed into a parameterless
11198 -- function call. If the called entity is global, rewrite the node
11199 -- appropriately, i.e. as an extended name for the global entity.
11201 elsif Nkind (Parent (N)) = N_Selected_Component
11202 and then Nkind (Parent (N2)) = N_Function_Call
11203 and then N = Selector_Name (Parent (N))
11205 if No (Parameter_Associations (Parent (N2))) then
11206 if Is_Global (Entity (Name (Parent (N2)))) then
11207 Change_Selected_Component_To_Expanded_Name (Parent (N));
11208 Set_Associated_Node (Parent (N), Name (Parent (N2)));
11209 Set_Global_Type (Parent (N), Name (Parent (N2)));
11210 Save_Entity_Descendants (N);
11213 Set_Associated_Node (N, Empty);
11214 Set_Etype (N, Empty);
11217 -- In Ada 2005, X.F may be a call to a primitive operation,
11218 -- rewritten as F (X). This rewriting will be done again in an
11219 -- instance, so keep the original node. Global entities will be
11220 -- captured as for other constructs.
11226 -- Entity is local. Reset in generic unit, so that node is resolved
11227 -- anew at the point of instantiation.
11230 Set_Associated_Node (N, Empty);
11231 Set_Etype (N, Empty);
11235 -----------------------------
11236 -- Save_Entity_Descendants --
11237 -----------------------------
11239 procedure Save_Entity_Descendants (N : Node_Id) is
11242 when N_Binary_Op =>
11243 Save_Global_Descendant (Union_Id (Left_Opnd (N)));
11244 Save_Global_Descendant (Union_Id (Right_Opnd (N)));
11247 Save_Global_Descendant (Union_Id (Right_Opnd (N)));
11249 when N_Expanded_Name | N_Selected_Component =>
11250 Save_Global_Descendant (Union_Id (Prefix (N)));
11251 Save_Global_Descendant (Union_Id (Selector_Name (N)));
11253 when N_Identifier | N_Character_Literal | N_Operator_Symbol =>
11257 raise Program_Error;
11259 end Save_Entity_Descendants;
11261 --------------------------
11262 -- Save_Global_Defaults --
11263 --------------------------
11265 procedure Save_Global_Defaults (N1, N2 : Node_Id) is
11266 Loc : constant Source_Ptr := Sloc (N1);
11267 Assoc2 : constant List_Id := Generic_Associations (N2);
11268 Gen_Id : constant Entity_Id := Get_Generic_Entity (N2);
11275 Actual : Entity_Id;
11278 Assoc1 := Generic_Associations (N1);
11280 if Present (Assoc1) then
11281 Act1 := First (Assoc1);
11284 Set_Generic_Associations (N1, New_List);
11285 Assoc1 := Generic_Associations (N1);
11288 if Present (Assoc2) then
11289 Act2 := First (Assoc2);
11294 while Present (Act1) and then Present (Act2) loop
11299 -- Find the associations added for default suprograms
11301 if Present (Act2) then
11302 while Nkind (Act2) /= N_Generic_Association
11303 or else No (Entity (Selector_Name (Act2)))
11304 or else not Is_Overloadable (Entity (Selector_Name (Act2)))
11309 -- Add a similar association if the default is global. The
11310 -- renaming declaration for the actual has been analyzed, and
11311 -- its alias is the program it renames. Link the actual in the
11312 -- original generic tree with the node in the analyzed tree.
11314 while Present (Act2) loop
11315 Subp := Entity (Selector_Name (Act2));
11316 Def := Explicit_Generic_Actual_Parameter (Act2);
11318 -- Following test is defence against rubbish errors
11320 if No (Alias (Subp)) then
11324 -- Retrieve the resolved actual from the renaming declaration
11325 -- created for the instantiated formal.
11327 Actual := Entity (Name (Parent (Parent (Subp))));
11328 Set_Entity (Def, Actual);
11329 Set_Etype (Def, Etype (Actual));
11331 if Is_Global (Actual) then
11333 Make_Generic_Association (Loc,
11334 Selector_Name => New_Occurrence_Of (Subp, Loc),
11335 Explicit_Generic_Actual_Parameter =>
11336 New_Occurrence_Of (Actual, Loc));
11338 Set_Associated_Node
11339 (Explicit_Generic_Actual_Parameter (Ndec), Def);
11341 Append (Ndec, Assoc1);
11343 -- If there are other defaults, add a dummy association in case
11344 -- there are other defaulted formals with the same name.
11346 elsif Present (Next (Act2)) then
11348 Make_Generic_Association (Loc,
11349 Selector_Name => New_Occurrence_Of (Subp, Loc),
11350 Explicit_Generic_Actual_Parameter => Empty);
11352 Append (Ndec, Assoc1);
11359 if Nkind (Name (N1)) = N_Identifier
11360 and then Is_Child_Unit (Gen_Id)
11361 and then Is_Global (Gen_Id)
11362 and then Is_Generic_Unit (Scope (Gen_Id))
11363 and then In_Open_Scopes (Scope (Gen_Id))
11365 -- This is an instantiation of a child unit within a sibling,
11366 -- so that the generic parent is in scope. An eventual instance
11367 -- must occur within the scope of an instance of the parent.
11368 -- Make name in instance into an expanded name, to preserve the
11369 -- identifier of the parent, so it can be resolved subsequently.
11371 Rewrite (Name (N2),
11372 Make_Expanded_Name (Loc,
11373 Chars => Chars (Gen_Id),
11374 Prefix => New_Occurrence_Of (Scope (Gen_Id), Loc),
11375 Selector_Name => New_Occurrence_Of (Gen_Id, Loc)));
11376 Set_Entity (Name (N2), Gen_Id);
11378 Rewrite (Name (N1),
11379 Make_Expanded_Name (Loc,
11380 Chars => Chars (Gen_Id),
11381 Prefix => New_Occurrence_Of (Scope (Gen_Id), Loc),
11382 Selector_Name => New_Occurrence_Of (Gen_Id, Loc)));
11384 Set_Associated_Node (Name (N1), Name (N2));
11385 Set_Associated_Node (Prefix (Name (N1)), Empty);
11386 Set_Associated_Node
11387 (Selector_Name (Name (N1)), Selector_Name (Name (N2)));
11388 Set_Etype (Name (N1), Etype (Gen_Id));
11391 end Save_Global_Defaults;
11393 ----------------------------
11394 -- Save_Global_Descendant --
11395 ----------------------------
11397 procedure Save_Global_Descendant (D : Union_Id) is
11401 if D in Node_Range then
11402 if D = Union_Id (Empty) then
11405 elsif Nkind (Node_Id (D)) /= N_Compilation_Unit then
11406 Save_References (Node_Id (D));
11409 elsif D in List_Range then
11410 if D = Union_Id (No_List)
11411 or else Is_Empty_List (List_Id (D))
11416 N1 := First (List_Id (D));
11417 while Present (N1) loop
11418 Save_References (N1);
11423 -- Element list or other non-node field, nothing to do
11428 end Save_Global_Descendant;
11430 ---------------------
11431 -- Save_References --
11432 ---------------------
11434 -- This is the recursive procedure that does the work, once the
11435 -- enclosing generic scope has been established. We have to treat
11436 -- specially a number of node rewritings that are required by semantic
11437 -- processing and which change the kind of nodes in the generic copy:
11438 -- typically constant-folding, replacing an operator node by a string
11439 -- literal, or a selected component by an expanded name. In each of
11440 -- those cases, the transformation is propagated to the generic unit.
11442 procedure Save_References (N : Node_Id) is
11447 elsif Nkind (N) = N_Character_Literal
11448 or else Nkind (N) = N_Operator_Symbol
11450 if Nkind (N) = Nkind (Get_Associated_Node (N)) then
11453 elsif Nkind (N) = N_Operator_Symbol
11454 and then Nkind (Get_Associated_Node (N)) = N_String_Literal
11456 Change_Operator_Symbol_To_String_Literal (N);
11459 elsif Nkind (N) in N_Op then
11460 if Nkind (N) = Nkind (Get_Associated_Node (N)) then
11461 if Nkind (N) = N_Op_Concat then
11462 Set_Is_Component_Left_Opnd (N,
11463 Is_Component_Left_Opnd (Get_Associated_Node (N)));
11465 Set_Is_Component_Right_Opnd (N,
11466 Is_Component_Right_Opnd (Get_Associated_Node (N)));
11472 -- Node may be transformed into call to a user-defined operator
11474 N2 := Get_Associated_Node (N);
11476 if Nkind (N2) = N_Function_Call then
11477 E := Entity (Name (N2));
11480 and then Is_Global (E)
11482 Set_Etype (N, Etype (N2));
11484 Set_Associated_Node (N, Empty);
11485 Set_Etype (N, Empty);
11488 elsif Nkind (N2) = N_Integer_Literal
11489 or else Nkind (N2) = N_Real_Literal
11490 or else Nkind (N2) = N_String_Literal
11492 if Present (Original_Node (N2))
11493 and then Nkind (Original_Node (N2)) = Nkind (N)
11496 -- Operation was constant-folded. Whenever possible,
11497 -- recover semantic information from unfolded node,
11500 Set_Associated_Node (N, Original_Node (N2));
11502 if Nkind (N) = N_Op_Concat then
11503 Set_Is_Component_Left_Opnd (N,
11504 Is_Component_Left_Opnd (Get_Associated_Node (N)));
11505 Set_Is_Component_Right_Opnd (N,
11506 Is_Component_Right_Opnd (Get_Associated_Node (N)));
11512 -- If original node is already modified, propagate
11513 -- constant-folding to template.
11515 Rewrite (N, New_Copy (N2));
11516 Set_Analyzed (N, False);
11519 elsif Nkind (N2) = N_Identifier
11520 and then Ekind (Entity (N2)) = E_Enumeration_Literal
11522 -- Same if call was folded into a literal, but in this case
11523 -- retain the entity to avoid spurious ambiguities if id is
11524 -- overloaded at the point of instantiation or inlining.
11526 Rewrite (N, New_Copy (N2));
11527 Set_Analyzed (N, False);
11531 -- Complete the check on operands, if node has not been
11532 -- constant-folded.
11534 if Nkind (N) in N_Op then
11535 Save_Entity_Descendants (N);
11538 elsif Nkind (N) = N_Identifier then
11539 if Nkind (N) = Nkind (Get_Associated_Node (N)) then
11541 -- If this is a discriminant reference, always save it. It is
11542 -- used in the instance to find the corresponding discriminant
11543 -- positionally rather than by name.
11545 Set_Original_Discriminant
11546 (N, Original_Discriminant (Get_Associated_Node (N)));
11550 N2 := Get_Associated_Node (N);
11552 if Nkind (N2) = N_Function_Call then
11553 E := Entity (Name (N2));
11555 -- Name resolves to a call to parameterless function. If
11556 -- original entity is global, mark node as resolved.
11559 and then Is_Global (E)
11561 Set_Etype (N, Etype (N2));
11563 Set_Associated_Node (N, Empty);
11564 Set_Etype (N, Empty);
11568 (Nkind (N2) = N_Integer_Literal
11570 Nkind (N2) = N_Real_Literal)
11571 and then Is_Entity_Name (Original_Node (N2))
11573 -- Name resolves to named number that is constant-folded,
11574 -- We must preserve the original name for ASIS use, and
11575 -- undo the constant-folding, which will be repeated in
11578 Set_Associated_Node (N, Original_Node (N2));
11581 elsif Nkind (N2) = N_String_Literal then
11583 -- Name resolves to string literal. Perform the same
11584 -- replacement in generic.
11586 Rewrite (N, New_Copy (N2));
11588 elsif Nkind (N2) = N_Explicit_Dereference then
11590 -- An identifier is rewritten as a dereference if it is
11591 -- the prefix in a selected component, and it denotes an
11592 -- access to a composite type, or a parameterless function
11593 -- call that returns an access type.
11595 -- Check whether corresponding entity in prefix is global
11597 if Is_Entity_Name (Prefix (N2))
11598 and then Present (Entity (Prefix (N2)))
11599 and then Is_Global (Entity (Prefix (N2)))
11602 Make_Explicit_Dereference (Sloc (N),
11603 Prefix => Make_Identifier (Sloc (N),
11604 Chars => Chars (N))));
11605 Set_Associated_Node (Prefix (N), Prefix (N2));
11607 elsif Nkind (Prefix (N2)) = N_Function_Call
11608 and then Is_Global (Entity (Name (Prefix (N2))))
11611 Make_Explicit_Dereference (Sloc (N),
11612 Prefix => Make_Function_Call (Sloc (N),
11614 Make_Identifier (Sloc (N),
11615 Chars => Chars (N)))));
11617 Set_Associated_Node
11618 (Name (Prefix (N)), Name (Prefix (N2)));
11621 Set_Associated_Node (N, Empty);
11622 Set_Etype (N, Empty);
11625 -- The subtype mark of a nominally unconstrained object is
11626 -- rewritten as a subtype indication using the bounds of the
11627 -- expression. Recover the original subtype mark.
11629 elsif Nkind (N2) = N_Subtype_Indication
11630 and then Is_Entity_Name (Original_Node (N2))
11632 Set_Associated_Node (N, Original_Node (N2));
11640 elsif Nkind (N) in N_Entity then
11645 Loc : constant Source_Ptr := Sloc (N);
11646 Qual : Node_Id := Empty;
11647 Typ : Entity_Id := Empty;
11650 use Atree.Unchecked_Access;
11651 -- This code section is part of implementing an untyped tree
11652 -- traversal, so it needs direct access to node fields.
11655 if Nkind (N) = N_Aggregate
11657 Nkind (N) = N_Extension_Aggregate
11659 N2 := Get_Associated_Node (N);
11666 -- In an instance within a generic, use the name of
11667 -- the actual and not the original generic parameter.
11668 -- If the actual is global in the current generic it
11669 -- must be preserved for its instantiation.
11671 if Nkind (Parent (Typ)) = N_Subtype_Declaration
11673 Present (Generic_Parent_Type (Parent (Typ)))
11675 Typ := Base_Type (Typ);
11676 Set_Etype (N2, Typ);
11682 or else not Is_Global (Typ)
11684 Set_Associated_Node (N, Empty);
11686 -- If the aggregate is an actual in a call, it has been
11687 -- resolved in the current context, to some local type.
11688 -- The enclosing call may have been disambiguated by the
11689 -- aggregate, and this disambiguation might fail at
11690 -- instantiation time because the type to which the
11691 -- aggregate did resolve is not preserved. In order to
11692 -- preserve some of this information, we wrap the
11693 -- aggregate in a qualified expression, using the id of
11694 -- its type. For further disambiguation we qualify the
11695 -- type name with its scope (if visible) because both
11696 -- id's will have corresponding entities in an instance.
11697 -- This resolves most of the problems with missing type
11698 -- information on aggregates in instances.
11700 if Nkind (N2) = Nkind (N)
11702 (Nkind (Parent (N2)) = N_Procedure_Call_Statement
11703 or else Nkind (Parent (N2)) = N_Function_Call)
11704 and then Comes_From_Source (Typ)
11706 if Is_Immediately_Visible (Scope (Typ)) then
11707 Nam := Make_Selected_Component (Loc,
11709 Make_Identifier (Loc, Chars (Scope (Typ))),
11711 Make_Identifier (Loc, Chars (Typ)));
11713 Nam := Make_Identifier (Loc, Chars (Typ));
11717 Make_Qualified_Expression (Loc,
11718 Subtype_Mark => Nam,
11719 Expression => Relocate_Node (N));
11723 Save_Global_Descendant (Field1 (N));
11724 Save_Global_Descendant (Field2 (N));
11725 Save_Global_Descendant (Field3 (N));
11726 Save_Global_Descendant (Field5 (N));
11728 if Present (Qual) then
11732 -- All other cases than aggregates
11735 Save_Global_Descendant (Field1 (N));
11736 Save_Global_Descendant (Field2 (N));
11737 Save_Global_Descendant (Field3 (N));
11738 Save_Global_Descendant (Field4 (N));
11739 Save_Global_Descendant (Field5 (N));
11743 end Save_References;
11745 -- Start of processing for Save_Global_References
11748 Gen_Scope := Current_Scope;
11750 -- If the generic unit is a child unit, references to entities in the
11751 -- parent are treated as local, because they will be resolved anew in
11752 -- the context of the instance of the parent.
11754 while Is_Child_Unit (Gen_Scope)
11755 and then Ekind (Scope (Gen_Scope)) = E_Generic_Package
11757 Gen_Scope := Scope (Gen_Scope);
11760 Save_References (N);
11761 end Save_Global_References;
11763 --------------------------------------
11764 -- Set_Copied_Sloc_For_Inlined_Body --
11765 --------------------------------------
11767 procedure Set_Copied_Sloc_For_Inlined_Body (N : Node_Id; E : Entity_Id) is
11769 Create_Instantiation_Source (N, E, True, S_Adjustment);
11770 end Set_Copied_Sloc_For_Inlined_Body;
11772 ---------------------
11773 -- Set_Instance_Of --
11774 ---------------------
11776 procedure Set_Instance_Of (A : Entity_Id; B : Entity_Id) is
11778 Generic_Renamings.Table (Generic_Renamings.Last) := (A, B, Assoc_Null);
11779 Generic_Renamings_HTable.Set (Generic_Renamings.Last);
11780 Generic_Renamings.Increment_Last;
11781 end Set_Instance_Of;
11783 --------------------
11784 -- Set_Next_Assoc --
11785 --------------------
11787 procedure Set_Next_Assoc (E : Assoc_Ptr; Next : Assoc_Ptr) is
11789 Generic_Renamings.Table (E).Next_In_HTable := Next;
11790 end Set_Next_Assoc;
11792 -------------------
11793 -- Start_Generic --
11794 -------------------
11796 procedure Start_Generic is
11798 -- ??? More things could be factored out in this routine.
11799 -- Should probably be done at a later stage.
11801 Generic_Flags.Append (Inside_A_Generic);
11802 Inside_A_Generic := True;
11804 Expander_Mode_Save_And_Set (False);
11807 ----------------------
11808 -- Set_Instance_Env --
11809 ----------------------
11811 procedure Set_Instance_Env
11812 (Gen_Unit : Entity_Id;
11813 Act_Unit : Entity_Id)
11816 -- Regardless of the current mode, predefined units are analyzed in
11817 -- the most current Ada mode, and earlier version Ada checks do not
11818 -- apply to predefined units. Nothing needs to be done for non-internal
11819 -- units. These are always analyzed in the current mode.
11821 if Is_Internal_File_Name
11822 (Fname => Unit_File_Name (Get_Source_Unit (Gen_Unit)),
11823 Renamings_Included => True)
11825 Set_Opt_Config_Switches (True, Current_Sem_Unit = Main_Unit);
11828 Current_Instantiated_Parent := (Gen_Unit, Act_Unit, Assoc_Null);
11829 end Set_Instance_Env;
11835 procedure Switch_View (T : Entity_Id) is
11836 BT : constant Entity_Id := Base_Type (T);
11837 Priv_Elmt : Elmt_Id := No_Elmt;
11838 Priv_Sub : Entity_Id;
11841 -- T may be private but its base type may have been exchanged through
11842 -- some other occurrence, in which case there is nothing to switch
11843 -- besides T itself. Note that a private dependent subtype of a private
11844 -- type might not have been switched even if the base type has been,
11845 -- because of the last branch of Check_Private_View (see comment there).
11847 if not Is_Private_Type (BT) then
11848 Prepend_Elmt (Full_View (T), Exchanged_Views);
11849 Exchange_Declarations (T);
11853 Priv_Elmt := First_Elmt (Private_Dependents (BT));
11855 if Present (Full_View (BT)) then
11856 Prepend_Elmt (Full_View (BT), Exchanged_Views);
11857 Exchange_Declarations (BT);
11860 while Present (Priv_Elmt) loop
11861 Priv_Sub := (Node (Priv_Elmt));
11863 -- We avoid flipping the subtype if the Etype of its full view is
11864 -- private because this would result in a malformed subtype. This
11865 -- occurs when the Etype of the subtype full view is the full view of
11866 -- the base type (and since the base types were just switched, the
11867 -- subtype is pointing to the wrong view). This is currently the case
11868 -- for tagged record types, access types (maybe more?) and needs to
11869 -- be resolved. ???
11871 if Present (Full_View (Priv_Sub))
11872 and then not Is_Private_Type (Etype (Full_View (Priv_Sub)))
11874 Prepend_Elmt (Full_View (Priv_Sub), Exchanged_Views);
11875 Exchange_Declarations (Priv_Sub);
11878 Next_Elmt (Priv_Elmt);
11882 -----------------------------
11883 -- Valid_Default_Attribute --
11884 -----------------------------
11886 procedure Valid_Default_Attribute (Nam : Entity_Id; Def : Node_Id) is
11887 Attr_Id : constant Attribute_Id :=
11888 Get_Attribute_Id (Attribute_Name (Def));
11889 T : constant Entity_Id := Entity (Prefix (Def));
11890 Is_Fun : constant Boolean := (Ekind (Nam) = E_Function);
11903 F := First_Formal (Nam);
11904 while Present (F) loop
11905 Num_F := Num_F + 1;
11910 when Attribute_Adjacent | Attribute_Ceiling | Attribute_Copy_Sign |
11911 Attribute_Floor | Attribute_Fraction | Attribute_Machine |
11912 Attribute_Model | Attribute_Remainder | Attribute_Rounding |
11913 Attribute_Unbiased_Rounding =>
11916 and then Is_Floating_Point_Type (T);
11918 when Attribute_Image | Attribute_Pred | Attribute_Succ |
11919 Attribute_Value | Attribute_Wide_Image |
11920 Attribute_Wide_Value =>
11921 OK := (Is_Fun and then Num_F = 1 and then Is_Scalar_Type (T));
11923 when Attribute_Max | Attribute_Min =>
11924 OK := (Is_Fun and then Num_F = 2 and then Is_Scalar_Type (T));
11926 when Attribute_Input =>
11927 OK := (Is_Fun and then Num_F = 1);
11929 when Attribute_Output | Attribute_Read | Attribute_Write =>
11930 OK := (not Is_Fun and then Num_F = 2);
11937 Error_Msg_N ("attribute reference has wrong profile for subprogram",
11940 end Valid_Default_Attribute;