1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
9 -- Copyright (C) 1992-2006, 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
311 procedure Analyze_Formal_Derived_Type
316 -- The following subprograms create abbreviated declarations for formal
317 -- scalar types. We introduce an anonymous base of the proper class for
318 -- each of them, and define the formals as constrained first subtypes of
319 -- their bases. The bounds are expressions that are non-static in the
322 procedure Analyze_Formal_Decimal_Fixed_Point_Type
323 (T
: Entity_Id
; Def
: Node_Id
);
324 procedure Analyze_Formal_Discrete_Type
(T
: Entity_Id
; Def
: Node_Id
);
325 procedure Analyze_Formal_Floating_Type
(T
: Entity_Id
; Def
: Node_Id
);
326 procedure Analyze_Formal_Interface_Type
(T
: Entity_Id
; Def
: Node_Id
);
327 procedure Analyze_Formal_Signed_Integer_Type
(T
: Entity_Id
; Def
: Node_Id
);
328 procedure Analyze_Formal_Modular_Type
(T
: Entity_Id
; Def
: Node_Id
);
329 procedure Analyze_Formal_Ordinary_Fixed_Point_Type
330 (T
: Entity_Id
; Def
: Node_Id
);
332 procedure Analyze_Formal_Private_Type
336 -- Creates a new private type, which does not require completion
338 procedure Analyze_Generic_Formal_Part
(N
: Node_Id
);
340 procedure Analyze_Generic_Access_Type
(T
: Entity_Id
; Def
: Node_Id
);
341 -- Create a new access type with the given designated type
343 function Analyze_Associations
346 F_Copy
: List_Id
) return List_Id
;
347 -- At instantiation time, build the list of associations between formals
348 -- and actuals. Each association becomes a renaming declaration for the
349 -- formal entity. F_Copy is the analyzed list of formals in the generic
350 -- copy. It is used to apply legality checks to the actuals. I_Node is the
351 -- instantiation node itself.
353 procedure Analyze_Subprogram_Instantiation
357 procedure Build_Instance_Compilation_Unit_Nodes
361 -- This procedure is used in the case where the generic instance of a
362 -- subprogram body or package body is a library unit. In this case, the
363 -- original library unit node for the generic instantiation must be
364 -- replaced by the resulting generic body, and a link made to a new
365 -- compilation unit node for the generic declaration. The argument N is
366 -- the original generic instantiation. Act_Body and Act_Decl are the body
367 -- and declaration of the instance (either package body and declaration
368 -- nodes or subprogram body and declaration nodes depending on the case).
369 -- On return, the node N has been rewritten with the actual body.
371 procedure Check_Access_Definition
(N
: Node_Id
);
372 -- Subsidiary routine to null exclusion processing. Perform an assertion
373 -- check on Ada version and the presence of an access definition in N.
375 procedure Check_Formal_Packages
(P_Id
: Entity_Id
);
376 -- Apply the following to all formal packages in generic associations
378 procedure Check_Formal_Package_Instance
379 (Formal_Pack
: Entity_Id
;
380 Actual_Pack
: Entity_Id
);
381 -- Verify that the actuals of the actual instance match the actuals of
382 -- the template for a formal package that is not declared with a box.
384 procedure Check_Forward_Instantiation
(Decl
: Node_Id
);
385 -- If the generic is a local entity and the corresponding body has not
386 -- been seen yet, flag enclosing packages to indicate that it will be
387 -- elaborated after the generic body. Subprograms declared in the same
388 -- package cannot be inlined by the front-end because front-end inlining
389 -- requires a strict linear order of elaboration.
391 procedure Check_Hidden_Child_Unit
393 Gen_Unit
: Entity_Id
;
394 Act_Decl_Id
: Entity_Id
);
395 -- If the generic unit is an implicit child instance within a parent
396 -- instance, we need to make an explicit test that it is not hidden by
397 -- a child instance of the same name and parent.
399 procedure Check_Generic_Actuals
400 (Instance
: Entity_Id
;
401 Is_Formal_Box
: Boolean);
402 -- Similar to previous one. Check the actuals in the instantiation,
403 -- whose views can change between the point of instantiation and the point
404 -- of instantiation of the body. In addition, mark the generic renamings
405 -- as generic actuals, so that they are not compatible with other actuals.
406 -- Recurse on an actual that is a formal package whose declaration has
409 function Contains_Instance_Of
412 N
: Node_Id
) return Boolean;
413 -- Inner is instantiated within the generic Outer. Check whether Inner
414 -- directly or indirectly contains an instance of Outer or of one of its
415 -- parents, in the case of a subunit. Each generic unit holds a list of
416 -- the entities instantiated within (at any depth). This procedure
417 -- determines whether the set of such lists contains a cycle, i.e. an
418 -- illegal circular instantiation.
420 function Denotes_Formal_Package
422 On_Exit
: Boolean := False) return Boolean;
423 -- Returns True if E is a formal package of an enclosing generic, or
424 -- the actual for such a formal in an enclosing instantiation. If such
425 -- a package is used as a formal in an nested generic, or as an actual
426 -- in a nested instantiation, the visibility of ITS formals should not
427 -- be modified. When called from within Restore_Private_Views, the flag
428 -- On_Exit is true, to indicate that the search for a possible enclosing
429 -- instance should ignore the current one.
431 function Find_Actual_Type
433 Gen_Scope
: Entity_Id
) return Entity_Id
;
434 -- When validating the actual types of a child instance, check whether
435 -- the formal is a formal type of the parent unit, and retrieve the current
436 -- actual for it. Typ is the entity in the analyzed formal type declaration
437 -- (component or index type of an array type, or designated type of an
438 -- access formal) and Gen_Scope is the scope of the analyzed formal array
439 -- or access type. The desired actual may be a formal of a parent, or may
440 -- be declared in a formal package of a parent. In both cases it is a
441 -- generic actual type because it appears within a visible instance.
442 -- Ambiguities may still arise if two homonyms are declared in two formal
443 -- packages, and the prefix of the formal type may be needed to resolve
444 -- the ambiguity in the instance ???
446 function In_Same_Declarative_Part
448 Inst
: Node_Id
) return Boolean;
449 -- True if the instantiation Inst and the given freeze_node F_Node appear
450 -- within the same declarative part, ignoring subunits, but with no inter-
451 -- vening suprograms or concurrent units. If true, the freeze node
452 -- of the instance can be placed after the freeze node of the parent,
453 -- which it itself an instance.
455 function In_Main_Context
(E
: Entity_Id
) return Boolean;
456 -- Check whether an instantiation is in the context of the main unit.
457 -- Used to determine whether its body should be elaborated to allow
458 -- front-end inlining.
460 function Is_Generic_Formal
(E
: Entity_Id
) return Boolean;
461 -- Utility to determine whether a given entity is declared by means of
462 -- of a formal parameter declaration. Used to set properly the visiblity
463 -- of generic formals of a generic package declared with a box or with
464 -- partial parametrization.
466 procedure Set_Instance_Env
467 (Gen_Unit
: Entity_Id
;
468 Act_Unit
: Entity_Id
);
469 -- Save current instance on saved environment, to be used to determine
470 -- the global status of entities in nested instances. Part of Save_Env.
471 -- called after verifying that the generic unit is legal for the instance.
473 procedure Set_Instance_Of
(A
: Entity_Id
; B
: Entity_Id
);
474 -- Associate analyzed generic parameter with corresponding
475 -- instance. Used for semantic checks at instantiation time.
477 function Has_Been_Exchanged
(E
: Entity_Id
) return Boolean;
478 -- Traverse the Exchanged_Views list to see if a type was private
479 -- and has already been flipped during this phase of instantiation.
481 procedure Hide_Current_Scope
;
482 -- When compiling a generic child unit, the parent context must be
483 -- present, but the instance and all entities that may be generated
484 -- must be inserted in the current scope. We leave the current scope
485 -- on the stack, but make its entities invisible to avoid visibility
486 -- problems. This is reversed at the end of instantiations. This is
487 -- not done for the instantiation of the bodies, which only require the
488 -- instances of the generic parents to be in scope.
490 procedure Install_Body
495 -- If the instantiation happens textually before the body of the generic,
496 -- the instantiation of the body must be analyzed after the generic body,
497 -- and not at the point of instantiation. Such early instantiations can
498 -- happen if the generic and the instance appear in a package declaration
499 -- because the generic body can only appear in the corresponding package
500 -- body. Early instantiations can also appear if generic, instance and
501 -- body are all in the declarative part of a subprogram or entry. Entities
502 -- of packages that are early instantiations are delayed, and their freeze
503 -- node appears after the generic body.
505 procedure Insert_After_Last_Decl
(N
: Node_Id
; F_Node
: Node_Id
);
506 -- Insert freeze node at the end of the declarative part that includes the
507 -- instance node N. If N is in the visible part of an enclosing package
508 -- declaration, the freeze node has to be inserted at the end of the
509 -- private declarations, if any.
511 procedure Freeze_Subprogram_Body
512 (Inst_Node
: Node_Id
;
514 Pack_Id
: Entity_Id
);
515 -- The generic body may appear textually after the instance, including
516 -- in the proper body of a stub, or within a different package instance.
517 -- Given that the instance can only be elaborated after the generic, we
518 -- place freeze_nodes for the instance and/or for packages that may enclose
519 -- the instance and the generic, so that the back-end can establish the
520 -- proper order of elaboration.
523 -- Establish environment for subsequent instantiation. Separated from
524 -- Save_Env because data-structures for visibility handling must be
525 -- initialized before call to Check_Generic_Child_Unit.
527 procedure Install_Parent
(P
: Entity_Id
; In_Body
: Boolean := False);
528 -- When compiling an instance of a child unit the parent (which is
529 -- itself an instance) is an enclosing scope that must be made
530 -- immediately visible. This procedure is also used to install the non-
531 -- generic parent of a generic child unit when compiling its body, so
532 -- that full views of types in the parent are made visible.
534 procedure Remove_Parent
(In_Body
: Boolean := False);
535 -- Reverse effect after instantiation of child is complete
537 procedure Inline_Instance_Body
539 Gen_Unit
: Entity_Id
;
541 -- If front-end inlining is requested, instantiate the package body,
542 -- and preserve the visibility of its compilation unit, to insure
543 -- that successive instantiations succeed.
545 -- The functions Instantiate_XXX perform various legality checks and build
546 -- the declarations for instantiated generic parameters. In all of these
547 -- Formal is the entity in the generic unit, Actual is the entity of
548 -- expression in the generic associations, and Analyzed_Formal is the
549 -- formal in the generic copy, which contains the semantic information to
550 -- be used to validate the actual.
552 function Instantiate_Object
555 Analyzed_Formal
: Node_Id
) return List_Id
;
557 function Instantiate_Type
560 Analyzed_Formal
: Node_Id
;
561 Actual_Decls
: List_Id
) return Node_Id
;
563 function Instantiate_Formal_Subprogram
566 Analyzed_Formal
: Node_Id
) return Node_Id
;
568 function Instantiate_Formal_Package
571 Analyzed_Formal
: Node_Id
) return List_Id
;
572 -- If the formal package is declared with a box, special visibility rules
573 -- apply to its formals: they are in the visible part of the package. This
574 -- is true in the declarative region of the formal package, that is to say
575 -- in the enclosing generic or instantiation. For an instantiation, the
576 -- parameters of the formal package are made visible in an explicit step.
577 -- Furthermore, if the actual is a visible use_clause, these formals must
578 -- be made potentially use_visible as well. On exit from the enclosing
579 -- instantiation, the reverse must be done.
581 -- For a formal package declared without a box, there are conformance rules
582 -- that apply to the actuals in the generic declaration and the actuals of
583 -- the actual package in the enclosing instantiation. The simplest way to
584 -- apply these rules is to repeat the instantiation of the formal package
585 -- in the context of the enclosing instance, and compare the generic
586 -- associations of this instantiation with those of the actual package.
587 -- This internal instantiation only needs to contain the renamings of the
588 -- formals: the visible and private declarations themselves need not be
591 -- In Ada2005, the formal package may be only partially parametrized. In
592 -- that case the visibility step must make visible those actuals whose
593 -- corresponding formals were given with a box. A final complication
594 -- involves inherited operations from formal derived types, which must be
595 -- visible if the type is.
597 function Is_In_Main_Unit
(N
: Node_Id
) return Boolean;
598 -- Test if given node is in the main unit
600 procedure Load_Parent_Of_Generic
(N
: Node_Id
; Spec
: Node_Id
);
601 -- If the generic appears in a separate non-generic library unit,
602 -- load the corresponding body to retrieve the body of the generic.
603 -- N is the node for the generic instantiation, Spec is the generic
604 -- package declaration.
606 procedure Inherit_Context
(Gen_Decl
: Node_Id
; Inst
: Node_Id
);
607 -- Add the context clause of the unit containing a generic unit to
608 -- an instantiation that is a compilation unit.
610 function Get_Associated_Node
(N
: Node_Id
) return Node_Id
;
611 -- In order to propagate semantic information back from the analyzed
612 -- copy to the original generic, we maintain links between selected nodes
613 -- in the generic and their corresponding copies. At the end of generic
614 -- analysis, the routine Save_Global_References traverses the generic
615 -- tree, examines the semantic information, and preserves the links to
616 -- those nodes that contain global information. At instantiation, the
617 -- information from the associated node is placed on the new copy, so
618 -- that name resolution is not repeated.
620 -- Three kinds of source nodes have associated nodes:
622 -- a) those that can reference (denote) entities, that is identifiers,
623 -- character literals, expanded_names, operator symbols, operators,
624 -- and attribute reference nodes. These nodes have an Entity field
625 -- and are the set of nodes that are in N_Has_Entity.
627 -- b) aggregates (N_Aggregate and N_Extension_Aggregate)
629 -- c) selected components (N_Selected_Component)
631 -- For the first class, the associated node preserves the entity if it is
632 -- global. If the generic contains nested instantiations, the associated
633 -- node itself has been recopied, and a chain of them must be followed.
635 -- For aggregates, the associated node allows retrieval of the type, which
636 -- may otherwise not appear in the generic. The view of this type may be
637 -- different between generic and instantiation, and the full view can be
638 -- installed before the instantiation is analyzed. For aggregates of
639 -- type extensions, the same view exchange may have to be performed for
640 -- some of the ancestor types, if their view is private at the point of
643 -- Nodes that are selected components in the parse tree may be rewritten
644 -- as expanded names after resolution, and must be treated as potential
645 -- entity holders. which is why they also have an Associated_Node.
647 -- Nodes that do not come from source, such as freeze nodes, do not appear
648 -- in the generic tree, and need not have an associated node.
650 -- The associated node is stored in the Associated_Node field. Note that
651 -- this field overlaps Entity, which is fine, because the whole point is
652 -- that we don't need or want the normal Entity field in this situation.
654 procedure Move_Freeze_Nodes
658 -- Freeze nodes can be generated in the analysis of a generic unit, but
659 -- will not be seen by the back-end. It is necessary to move those nodes
660 -- to the enclosing scope if they freeze an outer entity. We place them
661 -- at the end of the enclosing generic package, which is semantically
664 procedure Pre_Analyze_Actuals
(N
: Node_Id
);
665 -- Analyze actuals to perform name resolution. Full resolution is done
666 -- later, when the expected types are known, but names have to be captured
667 -- before installing parents of generics, that are not visible for the
668 -- actuals themselves.
670 procedure Valid_Default_Attribute
(Nam
: Entity_Id
; Def
: Node_Id
);
671 -- Verify that an attribute that appears as the default for a formal
672 -- subprogram is a function or procedure with the correct profile.
674 -------------------------------------------
675 -- Data Structures for Generic Renamings --
676 -------------------------------------------
678 -- The map Generic_Renamings associates generic entities with their
679 -- corresponding actuals. Currently used to validate type instances.
680 -- It will eventually be used for all generic parameters to eliminate
681 -- the need for overload resolution in the instance.
683 type Assoc_Ptr
is new Int
;
685 Assoc_Null
: constant Assoc_Ptr
:= -1;
690 Next_In_HTable
: Assoc_Ptr
;
693 package Generic_Renamings
is new Table
.Table
694 (Table_Component_Type
=> Assoc
,
695 Table_Index_Type
=> Assoc_Ptr
,
696 Table_Low_Bound
=> 0,
698 Table_Increment
=> 100,
699 Table_Name
=> "Generic_Renamings");
701 -- Variable to hold enclosing instantiation. When the environment is
702 -- saved for a subprogram inlining, the corresponding Act_Id is empty.
704 Current_Instantiated_Parent
: Assoc
:= (Empty
, Empty
, Assoc_Null
);
706 -- Hash table for associations
708 HTable_Size
: constant := 37;
709 type HTable_Range
is range 0 .. HTable_Size
- 1;
711 procedure Set_Next_Assoc
(E
: Assoc_Ptr
; Next
: Assoc_Ptr
);
712 function Next_Assoc
(E
: Assoc_Ptr
) return Assoc_Ptr
;
713 function Get_Gen_Id
(E
: Assoc_Ptr
) return Entity_Id
;
714 function Hash
(F
: Entity_Id
) return HTable_Range
;
716 package Generic_Renamings_HTable
is new GNAT
.HTable
.Static_HTable
(
717 Header_Num
=> HTable_Range
,
719 Elmt_Ptr
=> Assoc_Ptr
,
720 Null_Ptr
=> Assoc_Null
,
721 Set_Next
=> Set_Next_Assoc
,
724 Get_Key
=> Get_Gen_Id
,
728 Exchanged_Views
: Elist_Id
;
729 -- This list holds the private views that have been exchanged during
730 -- instantiation to restore the visibility of the generic declaration.
731 -- (see comments above). After instantiation, the current visibility is
732 -- reestablished by means of a traversal of this list.
734 Hidden_Entities
: Elist_Id
;
735 -- This list holds the entities of the current scope that are removed
736 -- from immediate visibility when instantiating a child unit. Their
737 -- visibility is restored in Remove_Parent.
739 -- Because instantiations can be recursive, the following must be saved
740 -- on entry and restored on exit from an instantiation (spec or body).
741 -- This is done by the two procedures Save_Env and Restore_Env. For
742 -- package and subprogram instantiations (but not for the body instances)
743 -- the action of Save_Env is done in two steps: Init_Env is called before
744 -- Check_Generic_Child_Unit, because setting the parent instances requires
745 -- that the visibility data structures be properly initialized. Once the
746 -- generic is unit is validated, Set_Instance_Env completes Save_Env.
748 Parent_Unit_Visible
: Boolean := False;
749 -- Parent_Unit_Visible is used when the generic is a child unit, and
750 -- indicates whether the ultimate parent of the generic is visible in the
751 -- instantiation environment. It is used to reset the visibility of the
752 -- parent at the end of the instantiation (see Remove_Parent).
754 Instance_Parent_Unit
: Entity_Id
:= Empty
;
755 -- This records the ultimate parent unit of an instance of a generic
756 -- child unit and is used in conjunction with Parent_Unit_Visible to
757 -- indicate the unit to which the Parent_Unit_Visible flag corresponds.
759 type Instance_Env
is record
760 Ada_Version
: Ada_Version_Type
;
761 Ada_Version_Explicit
: Ada_Version_Type
;
762 Instantiated_Parent
: Assoc
;
763 Exchanged_Views
: Elist_Id
;
764 Hidden_Entities
: Elist_Id
;
765 Current_Sem_Unit
: Unit_Number_Type
;
766 Parent_Unit_Visible
: Boolean := False;
767 Instance_Parent_Unit
: Entity_Id
:= Empty
;
770 package Instance_Envs
is new Table
.Table
(
771 Table_Component_Type
=> Instance_Env
,
772 Table_Index_Type
=> Int
,
773 Table_Low_Bound
=> 0,
775 Table_Increment
=> 100,
776 Table_Name
=> "Instance_Envs");
778 procedure Restore_Private_Views
779 (Pack_Id
: Entity_Id
;
780 Is_Package
: Boolean := True);
781 -- Restore the private views of external types, and unmark the generic
782 -- renamings of actuals, so that they become comptible subtypes again.
783 -- For subprograms, Pack_Id is the package constructed to hold the
786 procedure Switch_View
(T
: Entity_Id
);
787 -- Switch the partial and full views of a type and its private
788 -- dependents (i.e. its subtypes and derived types).
790 ------------------------------------
791 -- Structures for Error Reporting --
792 ------------------------------------
794 Instantiation_Node
: Node_Id
;
795 -- Used by subprograms that validate instantiation of formal parameters
796 -- where there might be no actual on which to place the error message.
797 -- Also used to locate the instantiation node for generic subunits.
799 Instantiation_Error
: exception;
800 -- When there is a semantic error in the generic parameter matching,
801 -- there is no point in continuing the instantiation, because the
802 -- number of cascaded errors is unpredictable. This exception aborts
803 -- the instantiation process altogether.
805 S_Adjustment
: Sloc_Adjustment
;
806 -- Offset created for each node in an instantiation, in order to keep
807 -- track of the source position of the instantiation in each of its nodes.
808 -- A subsequent semantic error or warning on a construct of the instance
809 -- points to both places: the original generic node, and the point of
810 -- instantiation. See Sinput and Sinput.L for additional details.
812 ------------------------------------------------------------
813 -- Data structure for keeping track when inside a Generic --
814 ------------------------------------------------------------
816 -- The following table is used to save values of the Inside_A_Generic
817 -- flag (see spec of Sem) when they are saved by Start_Generic.
819 package Generic_Flags
is new Table
.Table
(
820 Table_Component_Type
=> Boolean,
821 Table_Index_Type
=> Int
,
822 Table_Low_Bound
=> 0,
824 Table_Increment
=> 200,
825 Table_Name
=> "Generic_Flags");
827 ---------------------------
828 -- Abandon_Instantiation --
829 ---------------------------
831 procedure Abandon_Instantiation
(N
: Node_Id
) is
833 Error_Msg_N
("\instantiation abandoned!", N
);
834 raise Instantiation_Error
;
835 end Abandon_Instantiation
;
837 --------------------------
838 -- Analyze_Associations --
839 --------------------------
841 function Analyze_Associations
844 F_Copy
: List_Id
) return List_Id
846 Actual_Types
: constant Elist_Id
:= New_Elmt_List
;
847 Assoc
: constant List_Id
:= New_List
;
848 Default_Actuals
: constant Elist_Id
:= New_Elmt_List
;
849 Gen_Unit
: constant Entity_Id
:= Defining_Entity
(Parent
(F_Copy
));
853 Next_Formal
: Node_Id
;
854 Temp_Formal
: Node_Id
;
855 Analyzed_Formal
: Node_Id
;
858 First_Named
: Node_Id
:= Empty
;
860 Default_Formals
: constant List_Id
:= New_List
;
861 -- If an Other_Choice is present, some of the formals may be defaulted.
862 -- To simplify the treatement of visibility in an instance, we introduce
863 -- individual defaults for each such formal. These defaults are
864 -- appended to the list of associations and replace the Others_Choice.
866 Found_Assoc
: Node_Id
;
867 -- Association for the current formal being match. Empty if there are
868 -- no remaining actuals, or if there is no named association with the
869 -- name of the formal.
871 Is_Named_Assoc
: Boolean;
872 Num_Matched
: Int
:= 0;
873 Num_Actuals
: Int
:= 0;
875 Others_Present
: Boolean := False;
876 -- In Ada 2005, indicates partial parametrization of of a formal
877 -- package. As usual an others association must be last in the list.
879 function Matching_Actual
881 A_F
: Entity_Id
) return Node_Id
;
882 -- Find actual that corresponds to a given a formal parameter. If the
883 -- actuals are positional, return the next one, if any. If the actuals
884 -- are named, scan the parameter associations to find the right one.
885 -- A_F is the corresponding entity in the analyzed generic,which is
886 -- placed on the selector name for ASIS use.
888 -- In Ada 2005, a named association may be given with a box, in which
889 -- case Matching_Actual sets Found_Assoc to the generic association,
890 -- but return Empty for the actual itself. In this case the code below
891 -- creates a corresponding declaration for the formal.
893 function Partial_Parametrization
return Boolean;
894 -- Ada 2005: if no match is found for a given formal, check if the
895 -- association for it includes a box, or whether the associations
896 -- include an Others clause.
898 procedure Process_Default
(F
: Entity_Id
);
899 -- Add a copy of the declaration of generic formal F to the list of
900 -- associations, and add an explicit box association for F if there
901 -- is none yet, and the default comes from an Others_Choice.
903 procedure Set_Analyzed_Formal
;
904 -- Find the node in the generic copy that corresponds to a given formal.
905 -- The semantic information on this node is used to perform legality
906 -- checks on the actuals. Because semantic analysis can introduce some
907 -- anonymous entities or modify the declaration node itself, the
908 -- correspondence between the two lists is not one-one. In addition to
909 -- anonymous types, the presence a formal equality will introduce an
910 -- implicit declaration for the corresponding inequality.
912 ---------------------
913 -- Matching_Actual --
914 ---------------------
916 function Matching_Actual
918 A_F
: Entity_Id
) return Node_Id
924 Is_Named_Assoc
:= False;
926 -- End of list of purely positional parameters
929 Found_Assoc
:= Empty
;
932 -- Case of positional parameter corresponding to current formal
934 elsif No
(Selector_Name
(Actual
)) then
935 Found_Assoc
:= Actual
;
936 Act
:= Explicit_Generic_Actual_Parameter
(Actual
);
937 Num_Matched
:= Num_Matched
+ 1;
940 -- Otherwise scan list of named actuals to find the one with the
941 -- desired name. All remaining actuals have explicit names.
944 Is_Named_Assoc
:= True;
945 Found_Assoc
:= Empty
;
949 while Present
(Actual
) loop
950 if Chars
(Selector_Name
(Actual
)) = Chars
(F
) then
951 Set_Entity
(Selector_Name
(Actual
), A_F
);
952 Set_Etype
(Selector_Name
(Actual
), Etype
(A_F
));
953 Generate_Reference
(A_F
, Selector_Name
(Actual
));
954 Found_Assoc
:= Actual
;
955 Act
:= Explicit_Generic_Actual_Parameter
(Actual
);
956 Num_Matched
:= Num_Matched
+ 1;
964 -- Reset for subsequent searches. In most cases the named
965 -- associations are in order. If they are not, we reorder them
966 -- to avoid scanning twice the same actual. This is not just a
967 -- question of efficiency: there may be multiple defaults with
968 -- boxes that have the same name. In a nested instantiation we
969 -- insert actuals for those defaults, and cannot rely on their
970 -- names to disambiguate them.
972 if Actual
= First_Named
then
975 elsif Present
(Actual
) then
976 Insert_Before
(First_Named
, Remove_Next
(Prev
));
979 Actual
:= First_Named
;
985 -----------------------------
986 -- Partial_Parametrization --
987 -----------------------------
989 function Partial_Parametrization
return Boolean is
991 return Others_Present
992 or else (Present
(Found_Assoc
) and then Box_Present
(Found_Assoc
));
993 end Partial_Parametrization
;
995 ---------------------
996 -- Process_Default --
997 ---------------------
999 procedure Process_Default
(F
: Entity_Id
) is
1000 Loc
: constant Source_Ptr
:= Sloc
(I_Node
);
1004 Append
(Copy_Generic_Node
(F
, Empty
, True), Assoc
);
1006 if No
(Found_Assoc
) then
1008 Make_Generic_Association
(Loc
,
1010 New_Occurrence_Of
(Defining_Identifier
(F
), Loc
),
1011 Explicit_Generic_Actual_Parameter
=> Empty
);
1012 Set_Box_Present
(Default
);
1013 Append
(Default
, Default_Formals
);
1015 end Process_Default
;
1017 -------------------------
1018 -- Set_Analyzed_Formal --
1019 -------------------------
1021 procedure Set_Analyzed_Formal
is
1024 while Present
(Analyzed_Formal
) loop
1025 Kind
:= Nkind
(Analyzed_Formal
);
1027 case Nkind
(Formal
) is
1029 when N_Formal_Subprogram_Declaration
=>
1030 exit when Kind
in N_Formal_Subprogram_Declaration
1033 (Defining_Unit_Name
(Specification
(Formal
))) =
1035 (Defining_Unit_Name
(Specification
(Analyzed_Formal
)));
1037 when N_Formal_Package_Declaration
=>
1039 Kind
= N_Formal_Package_Declaration
1041 Kind
= N_Generic_Package_Declaration
1043 Kind
= N_Package_Declaration
;
1045 when N_Use_Package_Clause | N_Use_Type_Clause
=> exit;
1049 -- Skip freeze nodes, and nodes inserted to replace
1050 -- unrecognized pragmas.
1053 Kind
not in N_Formal_Subprogram_Declaration
1054 and then Kind
/= N_Subprogram_Declaration
1055 and then Kind
/= N_Freeze_Entity
1056 and then Kind
/= N_Null_Statement
1057 and then Kind
/= N_Itype_Reference
1058 and then Chars
(Defining_Identifier
(Formal
)) =
1059 Chars
(Defining_Identifier
(Analyzed_Formal
));
1062 Next
(Analyzed_Formal
);
1064 end Set_Analyzed_Formal
;
1066 -- Start of processing for Analyze_Associations
1069 Actuals
:= Generic_Associations
(I_Node
);
1071 if Present
(Actuals
) then
1073 -- check for an Others choice, indicating a partial parametrization
1074 -- for a formal package.
1076 Actual
:= First
(Actuals
);
1077 while Present
(Actual
) loop
1078 if Nkind
(Actual
) = N_Others_Choice
then
1079 Others_Present
:= True;
1080 if Present
(Next
(Actual
)) then
1081 Error_Msg_N
("others must be last association", Actual
);
1091 -- If named associations are present, save first named association
1092 -- (it may of course be Empty) to facilitate subsequent name search.
1094 First_Named
:= First
(Actuals
);
1095 while Present
(First_Named
)
1096 and then No
(Selector_Name
(First_Named
))
1098 Num_Actuals
:= Num_Actuals
+ 1;
1103 Named
:= First_Named
;
1104 while Present
(Named
) loop
1105 if No
(Selector_Name
(Named
)) then
1106 Error_Msg_N
("invalid positional actual after named one", Named
);
1107 Abandon_Instantiation
(Named
);
1110 -- A named association may lack an actual parameter, if it was
1111 -- introduced for a default subprogram that turns out to be local
1112 -- to the outer instantiation.
1114 if Present
(Explicit_Generic_Actual_Parameter
(Named
)) then
1115 Num_Actuals
:= Num_Actuals
+ 1;
1121 if Present
(Formals
) then
1122 Formal
:= First_Non_Pragma
(Formals
);
1123 Analyzed_Formal
:= First_Non_Pragma
(F_Copy
);
1125 if Present
(Actuals
) then
1126 Actual
:= First
(Actuals
);
1128 -- All formals should have default values
1134 while Present
(Formal
) loop
1135 Set_Analyzed_Formal
;
1136 Next_Formal
:= Next_Non_Pragma
(Formal
);
1138 case Nkind
(Formal
) is
1139 when N_Formal_Object_Declaration
=>
1142 Defining_Identifier
(Formal
),
1143 Defining_Identifier
(Analyzed_Formal
));
1145 if No
(Match
) and then Partial_Parametrization
then
1146 Process_Default
(Formal
);
1149 (Instantiate_Object
(Formal
, Match
, Analyzed_Formal
),
1153 when N_Formal_Type_Declaration
=>
1156 Defining_Identifier
(Formal
),
1157 Defining_Identifier
(Analyzed_Formal
));
1160 if Partial_Parametrization
then
1161 Process_Default
(Formal
);
1164 Error_Msg_Sloc
:= Sloc
(Gen_Unit
);
1168 Defining_Identifier
(Formal
));
1169 Error_Msg_NE
("\in instantiation of & declared#",
1170 Instantiation_Node
, Gen_Unit
);
1171 Abandon_Instantiation
(Instantiation_Node
);
1178 (Formal
, Match
, Analyzed_Formal
, Assoc
));
1180 -- An instantiation is a freeze point for the actuals,
1181 -- unless this is a rewritten formal package.
1183 if Nkind
(I_Node
) /= N_Formal_Package_Declaration
then
1184 Append_Elmt
(Entity
(Match
), Actual_Types
);
1188 -- A remote access-to-class-wide type must not be an
1189 -- actual parameter for a generic formal of an access
1190 -- type (E.2.2 (17)).
1192 if Nkind
(Analyzed_Formal
) = N_Formal_Type_Declaration
1194 Nkind
(Formal_Type_Definition
(Analyzed_Formal
)) =
1195 N_Access_To_Object_Definition
1197 Validate_Remote_Access_To_Class_Wide_Type
(Match
);
1200 when N_Formal_Subprogram_Declaration
=>
1203 Defining_Unit_Name
(Specification
(Formal
)),
1204 Defining_Unit_Name
(Specification
(Analyzed_Formal
)));
1206 -- If the formal subprogram has the same name as
1207 -- another formal subprogram of the generic, then
1208 -- a named association is illegal (12.3(9)). Exclude
1209 -- named associations that are generated for a nested
1213 and then Is_Named_Assoc
1214 and then Comes_From_Source
(Found_Assoc
)
1216 Temp_Formal
:= First
(Formals
);
1217 while Present
(Temp_Formal
) loop
1218 if Nkind
(Temp_Formal
) in
1219 N_Formal_Subprogram_Declaration
1220 and then Temp_Formal
/= Formal
1222 Chars
(Selector_Name
(Found_Assoc
)) =
1223 Chars
(Defining_Unit_Name
1224 (Specification
(Temp_Formal
)))
1227 ("name not allowed for overloaded formal",
1229 Abandon_Instantiation
(Instantiation_Node
);
1237 Instantiate_Formal_Subprogram
1238 (Formal
, Match
, Analyzed_Formal
));
1241 if Partial_Parametrization
then
1242 Process_Default
(Formal
);
1244 elsif Box_Present
(Formal
) then
1246 (Defining_Unit_Name
(Specification
(Last
(Assoc
))),
1251 when N_Formal_Package_Declaration
=>
1254 Defining_Identifier
(Formal
),
1255 Defining_Identifier
(Original_Node
(Analyzed_Formal
)));
1258 if Partial_Parametrization
then
1259 Process_Default
(Formal
);
1262 Error_Msg_Sloc
:= Sloc
(Gen_Unit
);
1265 Instantiation_Node
, Defining_Identifier
(Formal
));
1266 Error_Msg_NE
("\in instantiation of & declared#",
1267 Instantiation_Node
, Gen_Unit
);
1269 Abandon_Instantiation
(Instantiation_Node
);
1275 (Instantiate_Formal_Package
1276 (Formal
, Match
, Analyzed_Formal
),
1280 -- For use type and use package appearing in the generic
1281 -- part, we have already copied them, so we can just
1282 -- move them where they belong (we mustn't recopy them
1283 -- since this would mess up the Sloc values).
1285 when N_Use_Package_Clause |
1286 N_Use_Type_Clause
=>
1287 if Nkind
(Original_Node
(I_Node
)) =
1288 N_Formal_Package_Declaration
1290 Append
(New_Copy_Tree
(Formal
), Assoc
);
1293 Append
(Formal
, Assoc
);
1297 raise Program_Error
;
1301 Formal
:= Next_Formal
;
1302 Next_Non_Pragma
(Analyzed_Formal
);
1305 if Num_Actuals
> Num_Matched
then
1306 Error_Msg_Sloc
:= Sloc
(Gen_Unit
);
1308 if Present
(Selector_Name
(Actual
)) then
1310 ("unmatched actual&",
1311 Actual
, Selector_Name
(Actual
));
1312 Error_Msg_NE
("\in instantiation of& declared#",
1316 ("unmatched actual in instantiation of& declared#",
1321 elsif Present
(Actuals
) then
1323 ("too many actuals in generic instantiation", Instantiation_Node
);
1327 Elmt
: Elmt_Id
:= First_Elmt
(Actual_Types
);
1330 while Present
(Elmt
) loop
1331 Freeze_Before
(I_Node
, Node
(Elmt
));
1336 -- If there are default subprograms, normalize the tree by adding
1337 -- explicit associations for them. This is required if the instance
1338 -- appears within a generic.
1346 Elmt
:= First_Elmt
(Default_Actuals
);
1347 while Present
(Elmt
) loop
1348 if No
(Actuals
) then
1349 Actuals
:= New_List
;
1350 Set_Generic_Associations
(I_Node
, Actuals
);
1353 Subp
:= Node
(Elmt
);
1355 Make_Generic_Association
(Sloc
(Subp
),
1356 Selector_Name
=> New_Occurrence_Of
(Subp
, Sloc
(Subp
)),
1357 Explicit_Generic_Actual_Parameter
=>
1358 New_Occurrence_Of
(Subp
, Sloc
(Subp
)));
1359 Mark_Rewrite_Insertion
(New_D
);
1360 Append_To
(Actuals
, New_D
);
1365 -- If this is a formal package. normalize the parameter list by
1366 -- adding explicit box asssociations for the formals that are covered
1367 -- by an Others_Choice.
1369 if not Is_Empty_List
(Default_Formals
) then
1370 Append_List
(Default_Formals
, Formals
);
1374 end Analyze_Associations
;
1376 -------------------------------
1377 -- Analyze_Formal_Array_Type --
1378 -------------------------------
1380 procedure Analyze_Formal_Array_Type
1381 (T
: in out Entity_Id
;
1387 -- Treated like a non-generic array declaration, with
1388 -- additional semantic checks.
1392 if Nkind
(Def
) = N_Constrained_Array_Definition
then
1393 DSS
:= First
(Discrete_Subtype_Definitions
(Def
));
1394 while Present
(DSS
) loop
1395 if Nkind
(DSS
) = N_Subtype_Indication
1396 or else Nkind
(DSS
) = N_Range
1397 or else Nkind
(DSS
) = N_Attribute_Reference
1399 Error_Msg_N
("only a subtype mark is allowed in a formal", DSS
);
1406 Array_Type_Declaration
(T
, Def
);
1407 Set_Is_Generic_Type
(Base_Type
(T
));
1409 if Ekind
(Component_Type
(T
)) = E_Incomplete_Type
1410 and then No
(Full_View
(Component_Type
(T
)))
1412 Error_Msg_N
("premature usage of incomplete type", Def
);
1414 -- Check that range constraint is not allowed on the component type
1415 -- of a generic formal array type (AARM 12.5.3(3))
1417 elsif Is_Internal
(Component_Type
(T
))
1418 and then Present
(Subtype_Indication
(Component_Definition
(Def
)))
1419 and then Nkind
(Original_Node
1420 (Subtype_Indication
(Component_Definition
(Def
))))
1421 = N_Subtype_Indication
1424 ("in a formal, a subtype indication can only be "
1425 & "a subtype mark ('R'M 12.5.3(3))",
1426 Subtype_Indication
(Component_Definition
(Def
)));
1429 end Analyze_Formal_Array_Type
;
1431 ---------------------------------------------
1432 -- Analyze_Formal_Decimal_Fixed_Point_Type --
1433 ---------------------------------------------
1435 -- As for other generic types, we create a valid type representation
1436 -- with legal but arbitrary attributes, whose values are never considered
1437 -- static. For all scalar types we introduce an anonymous base type, with
1438 -- the same attributes. We choose the corresponding integer type to be
1439 -- Standard_Integer.
1441 procedure Analyze_Formal_Decimal_Fixed_Point_Type
1445 Loc
: constant Source_Ptr
:= Sloc
(Def
);
1446 Base
: constant Entity_Id
:=
1448 (E_Decimal_Fixed_Point_Type
,
1449 Current_Scope
, Sloc
(Def
), 'G');
1450 Int_Base
: constant Entity_Id
:= Standard_Integer
;
1451 Delta_Val
: constant Ureal
:= Ureal_1
;
1452 Digs_Val
: constant Uint
:= Uint_6
;
1457 Set_Etype
(Base
, Base
);
1458 Set_Size_Info
(Base
, Int_Base
);
1459 Set_RM_Size
(Base
, RM_Size
(Int_Base
));
1460 Set_First_Rep_Item
(Base
, First_Rep_Item
(Int_Base
));
1461 Set_Digits_Value
(Base
, Digs_Val
);
1462 Set_Delta_Value
(Base
, Delta_Val
);
1463 Set_Small_Value
(Base
, Delta_Val
);
1464 Set_Scalar_Range
(Base
,
1466 Low_Bound
=> Make_Real_Literal
(Loc
, Ureal_1
),
1467 High_Bound
=> Make_Real_Literal
(Loc
, Ureal_1
)));
1469 Set_Is_Generic_Type
(Base
);
1470 Set_Parent
(Base
, Parent
(Def
));
1472 Set_Ekind
(T
, E_Decimal_Fixed_Point_Subtype
);
1473 Set_Etype
(T
, Base
);
1474 Set_Size_Info
(T
, Int_Base
);
1475 Set_RM_Size
(T
, RM_Size
(Int_Base
));
1476 Set_First_Rep_Item
(T
, First_Rep_Item
(Int_Base
));
1477 Set_Digits_Value
(T
, Digs_Val
);
1478 Set_Delta_Value
(T
, Delta_Val
);
1479 Set_Small_Value
(T
, Delta_Val
);
1480 Set_Scalar_Range
(T
, Scalar_Range
(Base
));
1481 Set_Is_Constrained
(T
);
1483 Check_Restriction
(No_Fixed_Point
, Def
);
1484 end Analyze_Formal_Decimal_Fixed_Point_Type
;
1486 -------------------------------------------
1487 -- Analyze_Formal_Derived_Interface_Type --
1488 -------------------------------------------
1490 procedure Analyze_Formal_Derived_Interface_Type
1494 Ifaces_List
: Elist_Id
;
1498 Set_Ekind
(T
, E_Record_Type
);
1500 Analyze
(Subtype_Indication
(Def
));
1501 Analyze_Interface_Declaration
(T
, Def
);
1502 Make_Class_Wide_Type
(T
);
1503 Analyze_List
(Interface_List
(Def
));
1505 -- Ada 2005 (AI-251): Collect the list of progenitors that are not
1506 -- already covered by the parents.
1508 Collect_Abstract_Interfaces
1510 Ifaces_List
=> Ifaces_List
,
1511 Exclude_Parent_Interfaces
=> True);
1513 Set_Abstract_Interfaces
(T
, Ifaces_List
);
1514 end Analyze_Formal_Derived_Interface_Type
;
1516 ---------------------------------
1517 -- Analyze_Formal_Derived_Type --
1518 ---------------------------------
1520 procedure Analyze_Formal_Derived_Type
1525 Loc
: constant Source_Ptr
:= Sloc
(Def
);
1526 Unk_Disc
: constant Boolean := Unknown_Discriminants_Present
(N
);
1530 Set_Is_Generic_Type
(T
);
1532 if Private_Present
(Def
) then
1534 Make_Private_Extension_Declaration
(Loc
,
1535 Defining_Identifier
=> T
,
1536 Discriminant_Specifications
=> Discriminant_Specifications
(N
),
1537 Unknown_Discriminants_Present
=> Unk_Disc
,
1538 Subtype_Indication
=> Subtype_Mark
(Def
),
1539 Interface_List
=> Interface_List
(Def
));
1541 Set_Abstract_Present
(New_N
, Abstract_Present
(Def
));
1542 Set_Limited_Present
(New_N
, Limited_Present
(Def
));
1543 Set_Synchronized_Present
(New_N
, Synchronized_Present
(Def
));
1547 Make_Full_Type_Declaration
(Loc
,
1548 Defining_Identifier
=> T
,
1549 Discriminant_Specifications
=>
1550 Discriminant_Specifications
(Parent
(T
)),
1552 Make_Derived_Type_Definition
(Loc
,
1553 Subtype_Indication
=> Subtype_Mark
(Def
)));
1555 Set_Abstract_Present
1556 (Type_Definition
(New_N
), Abstract_Present
(Def
));
1558 (Type_Definition
(New_N
), Limited_Present
(Def
));
1565 if not Is_Composite_Type
(T
) then
1567 ("unknown discriminants not allowed for elementary types", N
);
1569 Set_Has_Unknown_Discriminants
(T
);
1570 Set_Is_Constrained
(T
, False);
1574 -- If the parent type has a known size, so does the formal, which
1575 -- makes legal representation clauses that involve the formal.
1577 Set_Size_Known_At_Compile_Time
1578 (T
, Size_Known_At_Compile_Time
(Entity
(Subtype_Mark
(Def
))));
1580 end Analyze_Formal_Derived_Type
;
1582 ----------------------------------
1583 -- Analyze_Formal_Discrete_Type --
1584 ----------------------------------
1586 -- The operations defined for a discrete types are those of an
1587 -- enumeration type. The size is set to an arbitrary value, for use
1588 -- in analyzing the generic unit.
1590 procedure Analyze_Formal_Discrete_Type
(T
: Entity_Id
; Def
: Node_Id
) is
1591 Loc
: constant Source_Ptr
:= Sloc
(Def
);
1595 Base
: constant Entity_Id
:=
1597 (E_Floating_Point_Type
, Current_Scope
, Sloc
(Def
), 'G');
1600 Set_Ekind
(T
, E_Enumeration_Subtype
);
1601 Set_Etype
(T
, Base
);
1604 Set_Is_Generic_Type
(T
);
1605 Set_Is_Constrained
(T
);
1607 -- For semantic analysis, the bounds of the type must be set to some
1608 -- non-static value. The simplest is to create attribute nodes for
1609 -- those bounds, that refer to the type itself. These bounds are never
1610 -- analyzed but serve as place-holders.
1613 Make_Attribute_Reference
(Loc
,
1614 Attribute_Name
=> Name_First
,
1615 Prefix
=> New_Reference_To
(T
, Loc
));
1619 Make_Attribute_Reference
(Loc
,
1620 Attribute_Name
=> Name_Last
,
1621 Prefix
=> New_Reference_To
(T
, Loc
));
1624 Set_Scalar_Range
(T
,
1629 Set_Ekind
(Base
, E_Enumeration_Type
);
1630 Set_Etype
(Base
, Base
);
1631 Init_Size
(Base
, 8);
1632 Init_Alignment
(Base
);
1633 Set_Is_Generic_Type
(Base
);
1634 Set_Scalar_Range
(Base
, Scalar_Range
(T
));
1635 Set_Parent
(Base
, Parent
(Def
));
1637 end Analyze_Formal_Discrete_Type
;
1639 ----------------------------------
1640 -- Analyze_Formal_Floating_Type --
1641 ---------------------------------
1643 procedure Analyze_Formal_Floating_Type
(T
: Entity_Id
; Def
: Node_Id
) is
1644 Base
: constant Entity_Id
:=
1646 (E_Floating_Point_Type
, Current_Scope
, Sloc
(Def
), 'G');
1649 -- The various semantic attributes are taken from the predefined type
1650 -- Float, just so that all of them are initialized. Their values are
1651 -- never used because no constant folding or expansion takes place in
1652 -- the generic itself.
1655 Set_Ekind
(T
, E_Floating_Point_Subtype
);
1656 Set_Etype
(T
, Base
);
1657 Set_Size_Info
(T
, (Standard_Float
));
1658 Set_RM_Size
(T
, RM_Size
(Standard_Float
));
1659 Set_Digits_Value
(T
, Digits_Value
(Standard_Float
));
1660 Set_Scalar_Range
(T
, Scalar_Range
(Standard_Float
));
1661 Set_Is_Constrained
(T
);
1663 Set_Is_Generic_Type
(Base
);
1664 Set_Etype
(Base
, Base
);
1665 Set_Size_Info
(Base
, (Standard_Float
));
1666 Set_RM_Size
(Base
, RM_Size
(Standard_Float
));
1667 Set_Digits_Value
(Base
, Digits_Value
(Standard_Float
));
1668 Set_Scalar_Range
(Base
, Scalar_Range
(Standard_Float
));
1669 Set_Parent
(Base
, Parent
(Def
));
1671 Check_Restriction
(No_Floating_Point
, Def
);
1672 end Analyze_Formal_Floating_Type
;
1674 -----------------------------------
1675 -- Analyze_Formal_Interface_Type;--
1676 -----------------------------------
1678 procedure Analyze_Formal_Interface_Type
(T
: Entity_Id
; Def
: Node_Id
) is
1681 Set_Ekind
(T
, E_Record_Type
);
1683 Analyze_Interface_Declaration
(T
, Def
);
1684 Make_Class_Wide_Type
(T
);
1685 Set_Primitive_Operations
(T
, New_Elmt_List
);
1686 end Analyze_Formal_Interface_Type
;
1688 ---------------------------------
1689 -- Analyze_Formal_Modular_Type --
1690 ---------------------------------
1692 procedure Analyze_Formal_Modular_Type
(T
: Entity_Id
; Def
: Node_Id
) is
1694 -- Apart from their entity kind, generic modular types are treated
1695 -- like signed integer types, and have the same attributes.
1697 Analyze_Formal_Signed_Integer_Type
(T
, Def
);
1698 Set_Ekind
(T
, E_Modular_Integer_Subtype
);
1699 Set_Ekind
(Etype
(T
), E_Modular_Integer_Type
);
1701 end Analyze_Formal_Modular_Type
;
1703 ---------------------------------------
1704 -- Analyze_Formal_Object_Declaration --
1705 ---------------------------------------
1707 procedure Analyze_Formal_Object_Declaration
(N
: Node_Id
) is
1708 E
: constant Node_Id
:= Default_Expression
(N
);
1709 Id
: constant Node_Id
:= Defining_Identifier
(N
);
1716 -- Determine the mode of the formal object
1718 if Out_Present
(N
) then
1719 K
:= E_Generic_In_Out_Parameter
;
1721 if not In_Present
(N
) then
1722 Error_Msg_N
("formal generic objects cannot have mode OUT", N
);
1726 K
:= E_Generic_In_Parameter
;
1729 if Present
(Subtype_Mark
(N
)) then
1730 Find_Type
(Subtype_Mark
(N
));
1731 T
:= Entity
(Subtype_Mark
(N
));
1733 -- Ada 2005 (AI-423): Formal object with an access definition
1736 Check_Access_Definition
(N
);
1737 T
:= Access_Definition
1739 N
=> Access_Definition
(N
));
1742 if Ekind
(T
) = E_Incomplete_Type
then
1744 Error_Node
: Node_Id
;
1747 if Present
(Subtype_Mark
(N
)) then
1748 Error_Node
:= Subtype_Mark
(N
);
1750 Check_Access_Definition
(N
);
1751 Error_Node
:= Access_Definition
(N
);
1754 Error_Msg_N
("premature usage of incomplete type", Error_Node
);
1758 if K
= E_Generic_In_Parameter
then
1760 -- Ada 2005 (AI-287): Limited aggregates allowed in generic formals
1762 if Ada_Version
< Ada_05
and then Is_Limited_Type
(T
) then
1764 ("generic formal of mode IN must not be of limited type", N
);
1765 Explain_Limited_Type
(T
, N
);
1768 if Is_Abstract
(T
) then
1770 ("generic formal of mode IN must not be of abstract type", N
);
1774 Analyze_Per_Use_Expression
(E
, T
);
1780 -- Case of generic IN OUT parameter
1783 -- If the formal has an unconstrained type, construct its
1784 -- actual subtype, as is done for subprogram formals. In this
1785 -- fashion, all its uses can refer to specific bounds.
1790 if (Is_Array_Type
(T
)
1791 and then not Is_Constrained
(T
))
1793 (Ekind
(T
) = E_Record_Type
1794 and then Has_Discriminants
(T
))
1797 Non_Freezing_Ref
: constant Node_Id
:=
1798 New_Reference_To
(Id
, Sloc
(Id
));
1802 -- Make sure that the actual subtype doesn't generate
1805 Set_Must_Not_Freeze
(Non_Freezing_Ref
);
1806 Decl
:= Build_Actual_Subtype
(T
, Non_Freezing_Ref
);
1807 Insert_Before_And_Analyze
(N
, Decl
);
1808 Set_Actual_Subtype
(Id
, Defining_Identifier
(Decl
));
1811 Set_Actual_Subtype
(Id
, T
);
1816 ("initialization not allowed for `IN OUT` formals", N
);
1820 end Analyze_Formal_Object_Declaration
;
1822 ----------------------------------------------
1823 -- Analyze_Formal_Ordinary_Fixed_Point_Type --
1824 ----------------------------------------------
1826 procedure Analyze_Formal_Ordinary_Fixed_Point_Type
1830 Loc
: constant Source_Ptr
:= Sloc
(Def
);
1831 Base
: constant Entity_Id
:=
1833 (E_Ordinary_Fixed_Point_Type
, Current_Scope
, Sloc
(Def
), 'G');
1835 -- The semantic attributes are set for completeness only, their
1836 -- values will never be used, because all properties of the type
1840 Set_Ekind
(T
, E_Ordinary_Fixed_Point_Subtype
);
1841 Set_Etype
(T
, Base
);
1842 Set_Size_Info
(T
, Standard_Integer
);
1843 Set_RM_Size
(T
, RM_Size
(Standard_Integer
));
1844 Set_Small_Value
(T
, Ureal_1
);
1845 Set_Delta_Value
(T
, Ureal_1
);
1846 Set_Scalar_Range
(T
,
1848 Low_Bound
=> Make_Real_Literal
(Loc
, Ureal_1
),
1849 High_Bound
=> Make_Real_Literal
(Loc
, Ureal_1
)));
1850 Set_Is_Constrained
(T
);
1852 Set_Is_Generic_Type
(Base
);
1853 Set_Etype
(Base
, Base
);
1854 Set_Size_Info
(Base
, Standard_Integer
);
1855 Set_RM_Size
(Base
, RM_Size
(Standard_Integer
));
1856 Set_Small_Value
(Base
, Ureal_1
);
1857 Set_Delta_Value
(Base
, Ureal_1
);
1858 Set_Scalar_Range
(Base
, Scalar_Range
(T
));
1859 Set_Parent
(Base
, Parent
(Def
));
1861 Check_Restriction
(No_Fixed_Point
, Def
);
1862 end Analyze_Formal_Ordinary_Fixed_Point_Type
;
1864 ----------------------------
1865 -- Analyze_Formal_Package --
1866 ----------------------------
1868 procedure Analyze_Formal_Package
(N
: Node_Id
) is
1869 Loc
: constant Source_Ptr
:= Sloc
(N
);
1870 Pack_Id
: constant Entity_Id
:= Defining_Identifier
(N
);
1872 Gen_Id
: constant Node_Id
:= Name
(N
);
1874 Gen_Unit
: Entity_Id
;
1876 Parent_Installed
: Boolean := False;
1878 Parent_Instance
: Entity_Id
;
1879 Renaming_In_Par
: Entity_Id
;
1880 No_Associations
: Boolean := False;
1882 function Build_Local_Package
return Node_Id
;
1883 -- The formal package is rewritten so that its parameters are replaced
1884 -- with corresponding declarations. For parameters with bona fide
1885 -- associations these declarations are created by Analyze_Associations
1886 -- as for aa regular instantiation. For boxed parameters, we preserve
1887 -- the formal declarations and analyze them, in order to introduce
1888 -- entities of the right kind in the environment of the formal.
1890 -------------------------
1891 -- Build_Local_Package --
1892 -------------------------
1894 function Build_Local_Package
return Node_Id
is
1896 Pack_Decl
: Node_Id
;
1899 -- Within the formal, the name of the generic package is a renaming
1900 -- of the formal (as for a regular instantiation).
1903 Make_Package_Declaration
(Loc
,
1906 (Specification
(Original_Node
(Gen_Decl
)),
1907 Empty
, Instantiating
=> True));
1909 Renaming
:= Make_Package_Renaming_Declaration
(Loc
,
1910 Defining_Unit_Name
=>
1911 Make_Defining_Identifier
(Loc
, Chars
(Gen_Unit
)),
1912 Name
=> New_Occurrence_Of
(Formal
, Loc
));
1914 if Nkind
(Gen_Id
) = N_Identifier
1915 and then Chars
(Gen_Id
) = Chars
(Pack_Id
)
1918 ("& is hidden within declaration of instance", Gen_Id
, Gen_Unit
);
1921 -- If the formal is declared with a box, or with an others choice,
1922 -- create corresponding declarations for all entities in the formal
1923 -- part, so that names with the proper types are available in the
1924 -- specification of the formal package.
1926 if No_Associations
then
1928 Formal_Decl
: Node_Id
;
1931 -- TBA : for a formal package, need to recurse
1936 (Generic_Formal_Declarations
(Original_Node
(Gen_Decl
)));
1937 while Present
(Formal_Decl
) loop
1939 (Decls
, Copy_Generic_Node
(Formal_Decl
, Empty
, True));
1944 -- If generic associations are present, use Analyze_Associations to
1945 -- create the proper renaming declarations.
1949 Act_Tree
: constant Node_Id
:=
1951 (Original_Node
(Gen_Decl
), Empty
,
1952 Instantiating
=> True);
1955 Generic_Renamings
.Set_Last
(0);
1956 Generic_Renamings_HTable
.Reset
;
1957 Instantiation_Node
:= N
;
1960 Analyze_Associations
1962 Generic_Formal_Declarations
(Act_Tree
),
1963 Generic_Formal_Declarations
(Gen_Decl
));
1967 Append
(Renaming
, To
=> Decls
);
1969 -- Add generated declarations ahead of local declarations in
1972 if No
(Visible_Declarations
(Specification
(Pack_Decl
))) then
1973 Set_Visible_Declarations
(Specification
(Pack_Decl
), Decls
);
1976 (First
(Visible_Declarations
(Specification
(Pack_Decl
))),
1981 end Build_Local_Package
;
1983 -- Start of processing for Analyze_Formal_Package
1986 Text_IO_Kludge
(Gen_Id
);
1989 Check_Generic_Child_Unit
(Gen_Id
, Parent_Installed
);
1990 Gen_Unit
:= Entity
(Gen_Id
);
1992 -- Check for a formal package that is a package renaming
1994 if Present
(Renamed_Object
(Gen_Unit
)) then
1995 Gen_Unit
:= Renamed_Object
(Gen_Unit
);
1998 if Ekind
(Gen_Unit
) /= E_Generic_Package
then
1999 Error_Msg_N
("expect generic package name", Gen_Id
);
2003 elsif Gen_Unit
= Current_Scope
then
2005 ("generic package cannot be used as a formal package of itself",
2010 elsif In_Open_Scopes
(Gen_Unit
) then
2011 if Is_Compilation_Unit
(Gen_Unit
)
2012 and then Is_Child_Unit
(Current_Scope
)
2014 -- Special-case the error when the formal is a parent, and
2015 -- continue analysis to minimize cascaded errors.
2018 ("generic parent cannot be used as formal package "
2019 & "of a child unit",
2024 ("generic package cannot be used as a formal package "
2033 or else No
(Generic_Associations
(N
))
2034 or else Nkind
(First
(Generic_Associations
(N
))) = N_Others_Choice
2036 No_Associations
:= True;
2039 -- If there are no generic associations, the generic parameters
2040 -- appear as local entities and are instantiated like them. We copy
2041 -- the generic package declaration as if it were an instantiation,
2042 -- and analyze it like a regular package, except that we treat the
2043 -- formals as additional visible components.
2045 Gen_Decl
:= Unit_Declaration_Node
(Gen_Unit
);
2047 if In_Extended_Main_Source_Unit
(N
) then
2048 Set_Is_Instantiated
(Gen_Unit
);
2049 Generate_Reference
(Gen_Unit
, N
);
2052 Formal
:= New_Copy
(Pack_Id
);
2053 Create_Instantiation_Source
(N
, Gen_Unit
, False, S_Adjustment
);
2055 -- Make local generic without formals. The formals will be replaced
2056 -- with internal declarations..
2058 New_N
:= Build_Local_Package
;
2060 Set_Defining_Unit_Name
(Specification
(New_N
), Formal
);
2061 Set_Generic_Parent
(Specification
(N
), Gen_Unit
);
2062 Set_Instance_Env
(Gen_Unit
, Formal
);
2063 Set_Is_Generic_Instance
(Formal
);
2065 Enter_Name
(Formal
);
2066 Set_Ekind
(Formal
, E_Package
);
2067 Set_Etype
(Formal
, Standard_Void_Type
);
2068 Set_Inner_Instances
(Formal
, New_Elmt_List
);
2071 if Is_Child_Unit
(Gen_Unit
)
2072 and then Parent_Installed
2074 -- Similarly, we have to make the name of the formal visible in
2075 -- the parent instance, to resolve properly fully qualified names
2076 -- that may appear in the generic unit. The parent instance has
2077 -- been placed on the scope stack ahead of the current scope.
2079 Parent_Instance
:= Scope_Stack
.Table
(Scope_Stack
.Last
- 1).Entity
;
2082 Make_Defining_Identifier
(Loc
, Chars
(Gen_Unit
));
2083 Set_Ekind
(Renaming_In_Par
, E_Package
);
2084 Set_Etype
(Renaming_In_Par
, Standard_Void_Type
);
2085 Set_Scope
(Renaming_In_Par
, Parent_Instance
);
2086 Set_Parent
(Renaming_In_Par
, Parent
(Formal
));
2087 Set_Renamed_Object
(Renaming_In_Par
, Formal
);
2088 Append_Entity
(Renaming_In_Par
, Parent_Instance
);
2091 Analyze
(Specification
(N
));
2093 -- The formals for which associations are provided are not visible
2094 -- outside of the formal package. The others are still declared by
2095 -- a formal parameter declaration.
2097 if not No_Associations
then
2102 E
:= First_Entity
(Formal
);
2103 while Present
(E
) loop
2104 exit when Ekind
(E
) = E_Package
2105 and then Renamed_Entity
(E
) = Formal
;
2107 if not Is_Generic_Formal
(E
) then
2116 End_Package_Scope
(Formal
);
2118 if Parent_Installed
then
2124 -- Inside the generic unit, the formal package is a regular
2125 -- package, but no body is needed for it. Note that after
2126 -- instantiation, the defining_unit_name we need is in the
2127 -- new tree and not in the original. (see Package_Instantiation).
2128 -- A generic formal package is an instance, and can be used as
2129 -- an actual for an inner instance.
2131 Set_Has_Completion
(Formal
, True);
2133 -- Add semantic information to the original defining identifier.
2136 Set_Ekind
(Pack_Id
, E_Package
);
2137 Set_Etype
(Pack_Id
, Standard_Void_Type
);
2138 Set_Scope
(Pack_Id
, Scope
(Formal
));
2139 Set_Has_Completion
(Pack_Id
, True);
2140 end Analyze_Formal_Package
;
2142 ---------------------------------
2143 -- Analyze_Formal_Private_Type --
2144 ---------------------------------
2146 procedure Analyze_Formal_Private_Type
2152 New_Private_Type
(N
, T
, Def
);
2154 -- Set the size to an arbitrary but legal value
2156 Set_Size_Info
(T
, Standard_Integer
);
2157 Set_RM_Size
(T
, RM_Size
(Standard_Integer
));
2158 end Analyze_Formal_Private_Type
;
2160 ----------------------------------------
2161 -- Analyze_Formal_Signed_Integer_Type --
2162 ----------------------------------------
2164 procedure Analyze_Formal_Signed_Integer_Type
2168 Base
: constant Entity_Id
:=
2170 (E_Signed_Integer_Type
, Current_Scope
, Sloc
(Def
), 'G');
2175 Set_Ekind
(T
, E_Signed_Integer_Subtype
);
2176 Set_Etype
(T
, Base
);
2177 Set_Size_Info
(T
, Standard_Integer
);
2178 Set_RM_Size
(T
, RM_Size
(Standard_Integer
));
2179 Set_Scalar_Range
(T
, Scalar_Range
(Standard_Integer
));
2180 Set_Is_Constrained
(T
);
2182 Set_Is_Generic_Type
(Base
);
2183 Set_Size_Info
(Base
, Standard_Integer
);
2184 Set_RM_Size
(Base
, RM_Size
(Standard_Integer
));
2185 Set_Etype
(Base
, Base
);
2186 Set_Scalar_Range
(Base
, Scalar_Range
(Standard_Integer
));
2187 Set_Parent
(Base
, Parent
(Def
));
2188 end Analyze_Formal_Signed_Integer_Type
;
2190 -------------------------------
2191 -- Analyze_Formal_Subprogram --
2192 -------------------------------
2194 procedure Analyze_Formal_Subprogram
(N
: Node_Id
) is
2195 Spec
: constant Node_Id
:= Specification
(N
);
2196 Def
: constant Node_Id
:= Default_Name
(N
);
2197 Nam
: constant Entity_Id
:= Defining_Unit_Name
(Spec
);
2205 if Nkind
(Nam
) = N_Defining_Program_Unit_Name
then
2206 Error_Msg_N
("name of formal subprogram must be a direct name", Nam
);
2210 Analyze_Subprogram_Declaration
(N
);
2211 Set_Is_Formal_Subprogram
(Nam
);
2212 Set_Has_Completion
(Nam
);
2214 if Nkind
(N
) = N_Formal_Abstract_Subprogram_Declaration
then
2215 Set_Is_Abstract
(Nam
);
2216 Set_Is_Dispatching_Operation
(Nam
);
2219 Ctrl_Type
: constant Entity_Id
:= Find_Dispatching_Type
(Nam
);
2222 if No
(Ctrl_Type
) then
2224 ("abstract formal subprogram must have a controlling type",
2228 Check_Controlling_Formals
(Ctrl_Type
, Nam
);
2233 -- Default name is resolved at the point of instantiation
2235 if Box_Present
(N
) then
2238 -- Else default is bound at the point of generic declaration
2240 elsif Present
(Def
) then
2241 if Nkind
(Def
) = N_Operator_Symbol
then
2242 Find_Direct_Name
(Def
);
2244 elsif Nkind
(Def
) /= N_Attribute_Reference
then
2248 -- For an attribute reference, analyze the prefix and verify
2249 -- that it has the proper profile for the subprogram.
2251 Analyze
(Prefix
(Def
));
2252 Valid_Default_Attribute
(Nam
, Def
);
2256 -- Default name may be overloaded, in which case the interpretation
2257 -- with the correct profile must be selected, as for a renaming.
2259 if Etype
(Def
) = Any_Type
then
2262 elsif Nkind
(Def
) = N_Selected_Component
then
2263 Subp
:= Entity
(Selector_Name
(Def
));
2265 if Ekind
(Subp
) /= E_Entry
then
2266 Error_Msg_N
("expect valid subprogram name as default", Def
);
2270 elsif Nkind
(Def
) = N_Indexed_Component
then
2272 if Nkind
(Prefix
(Def
)) /= N_Selected_Component
then
2273 Error_Msg_N
("expect valid subprogram name as default", Def
);
2277 Subp
:= Entity
(Selector_Name
(Prefix
(Def
)));
2279 if Ekind
(Subp
) /= E_Entry_Family
then
2280 Error_Msg_N
("expect valid subprogram name as default", Def
);
2285 elsif Nkind
(Def
) = N_Character_Literal
then
2287 -- Needs some type checks: subprogram should be parameterless???
2289 Resolve
(Def
, (Etype
(Nam
)));
2291 elsif not Is_Entity_Name
(Def
)
2292 or else not Is_Overloadable
(Entity
(Def
))
2294 Error_Msg_N
("expect valid subprogram name as default", Def
);
2297 elsif not Is_Overloaded
(Def
) then
2298 Subp
:= Entity
(Def
);
2301 Error_Msg_N
("premature usage of formal subprogram", Def
);
2303 elsif not Entity_Matches_Spec
(Subp
, Nam
) then
2304 Error_Msg_N
("no visible entity matches specification", Def
);
2310 I1
: Interp_Index
:= 0;
2316 Get_First_Interp
(Def
, I
, It
);
2317 while Present
(It
.Nam
) loop
2319 if Entity_Matches_Spec
(It
.Nam
, Nam
) then
2320 if Subp
/= Any_Id
then
2321 It1
:= Disambiguate
(Def
, I1
, I
, Etype
(Subp
));
2323 if It1
= No_Interp
then
2324 Error_Msg_N
("ambiguous default subprogram", Def
);
2337 Get_Next_Interp
(I
, It
);
2341 if Subp
/= Any_Id
then
2342 Set_Entity
(Def
, Subp
);
2345 Error_Msg_N
("premature usage of formal subprogram", Def
);
2347 elsif Ekind
(Subp
) /= E_Operator
then
2348 Check_Mode_Conformant
(Subp
, Nam
);
2352 Error_Msg_N
("no visible subprogram matches specification", N
);
2356 end Analyze_Formal_Subprogram
;
2358 -------------------------------------
2359 -- Analyze_Formal_Type_Declaration --
2360 -------------------------------------
2362 procedure Analyze_Formal_Type_Declaration
(N
: Node_Id
) is
2363 Def
: constant Node_Id
:= Formal_Type_Definition
(N
);
2367 T
:= Defining_Identifier
(N
);
2369 if Present
(Discriminant_Specifications
(N
))
2370 and then Nkind
(Def
) /= N_Formal_Private_Type_Definition
2373 ("discriminants not allowed for this formal type",
2374 Defining_Identifier
(First
(Discriminant_Specifications
(N
))));
2377 -- Enter the new name, and branch to specific routine
2380 when N_Formal_Private_Type_Definition
=>
2381 Analyze_Formal_Private_Type
(N
, T
, Def
);
2383 when N_Formal_Derived_Type_Definition
=>
2384 Analyze_Formal_Derived_Type
(N
, T
, Def
);
2386 when N_Formal_Discrete_Type_Definition
=>
2387 Analyze_Formal_Discrete_Type
(T
, Def
);
2389 when N_Formal_Signed_Integer_Type_Definition
=>
2390 Analyze_Formal_Signed_Integer_Type
(T
, Def
);
2392 when N_Formal_Modular_Type_Definition
=>
2393 Analyze_Formal_Modular_Type
(T
, Def
);
2395 when N_Formal_Floating_Point_Definition
=>
2396 Analyze_Formal_Floating_Type
(T
, Def
);
2398 when N_Formal_Ordinary_Fixed_Point_Definition
=>
2399 Analyze_Formal_Ordinary_Fixed_Point_Type
(T
, Def
);
2401 when N_Formal_Decimal_Fixed_Point_Definition
=>
2402 Analyze_Formal_Decimal_Fixed_Point_Type
(T
, Def
);
2404 when N_Array_Type_Definition
=>
2405 Analyze_Formal_Array_Type
(T
, Def
);
2407 when N_Access_To_Object_Definition |
2408 N_Access_Function_Definition |
2409 N_Access_Procedure_Definition
=>
2410 Analyze_Generic_Access_Type
(T
, Def
);
2412 -- Ada 2005: a interface declaration is encoded as an abstract
2413 -- record declaration or a abstract type derivation.
2415 when N_Record_Definition
=>
2416 Analyze_Formal_Interface_Type
(T
, Def
);
2418 when N_Derived_Type_Definition
=>
2419 Analyze_Formal_Derived_Interface_Type
(T
, Def
);
2425 raise Program_Error
;
2429 Set_Is_Generic_Type
(T
);
2430 end Analyze_Formal_Type_Declaration
;
2432 ------------------------------------
2433 -- Analyze_Function_Instantiation --
2434 ------------------------------------
2436 procedure Analyze_Function_Instantiation
(N
: Node_Id
) is
2438 Analyze_Subprogram_Instantiation
(N
, E_Function
);
2439 end Analyze_Function_Instantiation
;
2441 ---------------------------------
2442 -- Analyze_Generic_Access_Type --
2443 ---------------------------------
2445 procedure Analyze_Generic_Access_Type
(T
: Entity_Id
; Def
: Node_Id
) is
2449 if Nkind
(Def
) = N_Access_To_Object_Definition
then
2450 Access_Type_Declaration
(T
, Def
);
2452 if Is_Incomplete_Or_Private_Type
(Designated_Type
(T
))
2453 and then No
(Full_View
(Designated_Type
(T
)))
2454 and then not Is_Generic_Type
(Designated_Type
(T
))
2456 Error_Msg_N
("premature usage of incomplete type", Def
);
2458 elsif Is_Internal
(Designated_Type
(T
)) then
2460 ("only a subtype mark is allowed in a formal", Def
);
2464 Access_Subprogram_Declaration
(T
, Def
);
2466 end Analyze_Generic_Access_Type
;
2468 ---------------------------------
2469 -- Analyze_Generic_Formal_Part --
2470 ---------------------------------
2472 procedure Analyze_Generic_Formal_Part
(N
: Node_Id
) is
2473 Gen_Parm_Decl
: Node_Id
;
2476 -- The generic formals are processed in the scope of the generic
2477 -- unit, where they are immediately visible. The scope is installed
2480 Gen_Parm_Decl
:= First
(Generic_Formal_Declarations
(N
));
2482 while Present
(Gen_Parm_Decl
) loop
2483 Analyze
(Gen_Parm_Decl
);
2484 Next
(Gen_Parm_Decl
);
2487 Generate_Reference_To_Generic_Formals
(Current_Scope
);
2488 end Analyze_Generic_Formal_Part
;
2490 ------------------------------------------
2491 -- Analyze_Generic_Package_Declaration --
2492 ------------------------------------------
2494 procedure Analyze_Generic_Package_Declaration
(N
: Node_Id
) is
2495 Loc
: constant Source_Ptr
:= Sloc
(N
);
2498 Save_Parent
: Node_Id
;
2500 Decls
: constant List_Id
:=
2501 Visible_Declarations
(Specification
(N
));
2505 -- We introduce a renaming of the enclosing package, to have a usable
2506 -- entity as the prefix of an expanded name for a local entity of the
2507 -- form Par.P.Q, where P is the generic package. This is because a local
2508 -- entity named P may hide it, so that the usual visibility rules in
2509 -- the instance will not resolve properly.
2512 Make_Package_Renaming_Declaration
(Loc
,
2513 Defining_Unit_Name
=>
2514 Make_Defining_Identifier
(Loc
,
2515 Chars
=> New_External_Name
(Chars
(Defining_Entity
(N
)), "GH")),
2516 Name
=> Make_Identifier
(Loc
, Chars
(Defining_Entity
(N
))));
2518 if Present
(Decls
) then
2519 Decl
:= First
(Decls
);
2520 while Present
(Decl
)
2521 and then Nkind
(Decl
) = N_Pragma
2526 if Present
(Decl
) then
2527 Insert_Before
(Decl
, Renaming
);
2529 Append
(Renaming
, Visible_Declarations
(Specification
(N
)));
2533 Set_Visible_Declarations
(Specification
(N
), New_List
(Renaming
));
2536 -- Create copy of generic unit, and save for instantiation.
2537 -- If the unit is a child unit, do not copy the specifications
2538 -- for the parent, which are not part of the generic tree.
2540 Save_Parent
:= Parent_Spec
(N
);
2541 Set_Parent_Spec
(N
, Empty
);
2543 New_N
:= Copy_Generic_Node
(N
, Empty
, Instantiating
=> False);
2544 Set_Parent_Spec
(New_N
, Save_Parent
);
2546 Id
:= Defining_Entity
(N
);
2547 Generate_Definition
(Id
);
2549 -- Expansion is not applied to generic units
2554 Set_Ekind
(Id
, E_Generic_Package
);
2555 Set_Etype
(Id
, Standard_Void_Type
);
2557 Enter_Generic_Scope
(Id
);
2558 Set_Inner_Instances
(Id
, New_Elmt_List
);
2560 Set_Categorization_From_Pragmas
(N
);
2561 Set_Is_Pure
(Id
, Is_Pure
(Current_Scope
));
2563 -- Link the declaration of the generic homonym in the generic copy
2564 -- to the package it renames, so that it is always resolved properly.
2566 Set_Generic_Homonym
(Id
, Defining_Unit_Name
(Renaming
));
2567 Set_Entity
(Associated_Node
(Name
(Renaming
)), Id
);
2569 -- For a library unit, we have reconstructed the entity for the
2570 -- unit, and must reset it in the library tables.
2572 if Nkind
(Parent
(N
)) = N_Compilation_Unit
then
2573 Set_Cunit_Entity
(Current_Sem_Unit
, Id
);
2576 Analyze_Generic_Formal_Part
(N
);
2578 -- After processing the generic formals, analysis proceeds
2579 -- as for a non-generic package.
2581 Analyze
(Specification
(N
));
2583 Validate_Categorization_Dependency
(N
, Id
);
2587 End_Package_Scope
(Id
);
2588 Exit_Generic_Scope
(Id
);
2590 if Nkind
(Parent
(N
)) /= N_Compilation_Unit
then
2591 Move_Freeze_Nodes
(Id
, N
, Visible_Declarations
(Specification
(N
)));
2592 Move_Freeze_Nodes
(Id
, N
, Private_Declarations
(Specification
(N
)));
2593 Move_Freeze_Nodes
(Id
, N
, Generic_Formal_Declarations
(N
));
2596 Set_Body_Required
(Parent
(N
), Unit_Requires_Body
(Id
));
2597 Validate_RT_RAT_Component
(N
);
2599 -- If this is a spec without a body, check that generic parameters
2602 if not Body_Required
(Parent
(N
)) then
2603 Check_References
(Id
);
2606 end Analyze_Generic_Package_Declaration
;
2608 --------------------------------------------
2609 -- Analyze_Generic_Subprogram_Declaration --
2610 --------------------------------------------
2612 procedure Analyze_Generic_Subprogram_Declaration
(N
: Node_Id
) is
2617 Result_Type
: Entity_Id
;
2618 Save_Parent
: Node_Id
;
2621 -- Create copy of generic unit,and save for instantiation.
2622 -- If the unit is a child unit, do not copy the specifications
2623 -- for the parent, which are not part of the generic tree.
2625 Save_Parent
:= Parent_Spec
(N
);
2626 Set_Parent_Spec
(N
, Empty
);
2628 New_N
:= Copy_Generic_Node
(N
, Empty
, Instantiating
=> False);
2629 Set_Parent_Spec
(New_N
, Save_Parent
);
2632 Spec
:= Specification
(N
);
2633 Id
:= Defining_Entity
(Spec
);
2634 Generate_Definition
(Id
);
2636 if Nkind
(Id
) = N_Defining_Operator_Symbol
then
2638 ("operator symbol not allowed for generic subprogram", Id
);
2645 Set_Scope_Depth_Value
(Id
, Scope_Depth
(Current_Scope
) + 1);
2647 Enter_Generic_Scope
(Id
);
2648 Set_Inner_Instances
(Id
, New_Elmt_List
);
2649 Set_Is_Pure
(Id
, Is_Pure
(Current_Scope
));
2651 Analyze_Generic_Formal_Part
(N
);
2653 Formals
:= Parameter_Specifications
(Spec
);
2655 if Present
(Formals
) then
2656 Process_Formals
(Formals
, Spec
);
2659 if Nkind
(Spec
) = N_Function_Specification
then
2660 Set_Ekind
(Id
, E_Generic_Function
);
2662 if Nkind
(Result_Definition
(Spec
)) = N_Access_Definition
then
2663 Result_Type
:= Access_Definition
(Spec
, Result_Definition
(Spec
));
2664 Set_Etype
(Id
, Result_Type
);
2666 Find_Type
(Result_Definition
(Spec
));
2667 Set_Etype
(Id
, Entity
(Result_Definition
(Spec
)));
2671 Set_Ekind
(Id
, E_Generic_Procedure
);
2672 Set_Etype
(Id
, Standard_Void_Type
);
2675 -- For a library unit, we have reconstructed the entity for the unit,
2676 -- and must reset it in the library tables. We also make sure that
2677 -- Body_Required is set properly in the original compilation unit node.
2679 if Nkind
(Parent
(N
)) = N_Compilation_Unit
then
2680 Set_Cunit_Entity
(Current_Sem_Unit
, Id
);
2681 Set_Body_Required
(Parent
(N
), Unit_Requires_Body
(Id
));
2684 Set_Categorization_From_Pragmas
(N
);
2685 Validate_Categorization_Dependency
(N
, Id
);
2687 Save_Global_References
(Original_Node
(N
));
2691 Exit_Generic_Scope
(Id
);
2692 Generate_Reference_To_Formals
(Id
);
2693 end Analyze_Generic_Subprogram_Declaration
;
2695 -----------------------------------
2696 -- Analyze_Package_Instantiation --
2697 -----------------------------------
2699 procedure Analyze_Package_Instantiation
(N
: Node_Id
) is
2700 Loc
: constant Source_Ptr
:= Sloc
(N
);
2701 Gen_Id
: constant Node_Id
:= Name
(N
);
2704 Act_Decl_Name
: Node_Id
;
2705 Act_Decl_Id
: Entity_Id
;
2710 Gen_Unit
: Entity_Id
;
2712 Is_Actual_Pack
: constant Boolean :=
2713 Is_Internal
(Defining_Entity
(N
));
2715 Env_Installed
: Boolean := False;
2716 Parent_Installed
: Boolean := False;
2717 Renaming_List
: List_Id
;
2718 Unit_Renaming
: Node_Id
;
2719 Needs_Body
: Boolean;
2720 Inline_Now
: Boolean := False;
2722 procedure Delay_Descriptors
(E
: Entity_Id
);
2723 -- Delay generation of subprogram descriptors for given entity
2725 function Might_Inline_Subp
return Boolean;
2726 -- If inlining is active and the generic contains inlined subprograms,
2727 -- we instantiate the body. This may cause superfluous instantiations,
2728 -- but it is simpler than detecting the need for the body at the point
2729 -- of inlining, when the context of the instance is not available.
2731 -----------------------
2732 -- Delay_Descriptors --
2733 -----------------------
2735 procedure Delay_Descriptors
(E
: Entity_Id
) is
2737 if not Delay_Subprogram_Descriptors
(E
) then
2738 Set_Delay_Subprogram_Descriptors
(E
);
2739 Pending_Descriptor
.Increment_Last
;
2740 Pending_Descriptor
.Table
(Pending_Descriptor
.Last
) := E
;
2742 end Delay_Descriptors
;
2744 -----------------------
2745 -- Might_Inline_Subp --
2746 -----------------------
2748 function Might_Inline_Subp
return Boolean is
2752 if not Inline_Processing_Required
then
2756 E
:= First_Entity
(Gen_Unit
);
2757 while Present
(E
) loop
2758 if Is_Subprogram
(E
)
2759 and then Is_Inlined
(E
)
2769 end Might_Inline_Subp
;
2771 -- Start of processing for Analyze_Package_Instantiation
2774 -- Very first thing: apply the special kludge for Text_IO processing
2775 -- in case we are instantiating one of the children of [Wide_]Text_IO.
2777 Text_IO_Kludge
(Name
(N
));
2779 -- Make node global for error reporting
2781 Instantiation_Node
:= N
;
2783 -- Case of instantiation of a generic package
2785 if Nkind
(N
) = N_Package_Instantiation
then
2786 Act_Decl_Id
:= New_Copy
(Defining_Entity
(N
));
2787 Set_Comes_From_Source
(Act_Decl_Id
, True);
2789 if Nkind
(Defining_Unit_Name
(N
)) = N_Defining_Program_Unit_Name
then
2791 Make_Defining_Program_Unit_Name
(Loc
,
2792 Name
=> New_Copy_Tree
(Name
(Defining_Unit_Name
(N
))),
2793 Defining_Identifier
=> Act_Decl_Id
);
2795 Act_Decl_Name
:= Act_Decl_Id
;
2798 -- Case of instantiation of a formal package
2801 Act_Decl_Id
:= Defining_Identifier
(N
);
2802 Act_Decl_Name
:= Act_Decl_Id
;
2805 Generate_Definition
(Act_Decl_Id
);
2806 Pre_Analyze_Actuals
(N
);
2809 Env_Installed
:= True;
2810 Check_Generic_Child_Unit
(Gen_Id
, Parent_Installed
);
2811 Gen_Unit
:= Entity
(Gen_Id
);
2813 -- Verify that it is the name of a generic package
2815 if Etype
(Gen_Unit
) = Any_Type
then
2819 elsif Ekind
(Gen_Unit
) /= E_Generic_Package
then
2821 -- Ada 2005 (AI-50217): Cannot use instance in limited with_clause
2823 if From_With_Type
(Gen_Unit
) then
2825 ("cannot instantiate a limited withed package", Gen_Id
);
2828 ("expect name of generic package in instantiation", Gen_Id
);
2835 if In_Extended_Main_Source_Unit
(N
) then
2836 Set_Is_Instantiated
(Gen_Unit
);
2837 Generate_Reference
(Gen_Unit
, N
);
2839 if Present
(Renamed_Object
(Gen_Unit
)) then
2840 Set_Is_Instantiated
(Renamed_Object
(Gen_Unit
));
2841 Generate_Reference
(Renamed_Object
(Gen_Unit
), N
);
2845 if Nkind
(Gen_Id
) = N_Identifier
2846 and then Chars
(Gen_Unit
) = Chars
(Defining_Entity
(N
))
2849 ("& is hidden within declaration of instance", Gen_Id
, Gen_Unit
);
2851 elsif Nkind
(Gen_Id
) = N_Expanded_Name
2852 and then Is_Child_Unit
(Gen_Unit
)
2853 and then Nkind
(Prefix
(Gen_Id
)) = N_Identifier
2854 and then Chars
(Act_Decl_Id
) = Chars
(Prefix
(Gen_Id
))
2857 ("& is hidden within declaration of instance ", Prefix
(Gen_Id
));
2860 Set_Entity
(Gen_Id
, Gen_Unit
);
2862 -- If generic is a renaming, get original generic unit
2864 if Present
(Renamed_Object
(Gen_Unit
))
2865 and then Ekind
(Renamed_Object
(Gen_Unit
)) = E_Generic_Package
2867 Gen_Unit
:= Renamed_Object
(Gen_Unit
);
2870 -- Verify that there are no circular instantiations
2872 if In_Open_Scopes
(Gen_Unit
) then
2873 Error_Msg_NE
("instantiation of & within itself", N
, Gen_Unit
);
2877 elsif Contains_Instance_Of
(Gen_Unit
, Current_Scope
, Gen_Id
) then
2878 Error_Msg_Node_2
:= Current_Scope
;
2880 ("circular Instantiation: & instantiated in &!", N
, Gen_Unit
);
2881 Circularity_Detected
:= True;
2886 Set_Instance_Env
(Gen_Unit
, Act_Decl_Id
);
2887 Gen_Decl
:= Unit_Declaration_Node
(Gen_Unit
);
2889 -- Initialize renamings map, for error checking, and the list
2890 -- that holds private entities whose views have changed between
2891 -- generic definition and instantiation. If this is the instance
2892 -- created to validate an actual package, the instantiation
2893 -- environment is that of the enclosing instance.
2895 Generic_Renamings
.Set_Last
(0);
2896 Generic_Renamings_HTable
.Reset
;
2898 Create_Instantiation_Source
(N
, Gen_Unit
, False, S_Adjustment
);
2900 -- Copy original generic tree, to produce text for instantiation
2904 (Original_Node
(Gen_Decl
), Empty
, Instantiating
=> True);
2906 Act_Spec
:= Specification
(Act_Tree
);
2908 -- If this is the instance created to validate an actual package,
2909 -- only the formals matter, do not examine the package spec itself.
2911 if Is_Actual_Pack
then
2912 Set_Visible_Declarations
(Act_Spec
, New_List
);
2913 Set_Private_Declarations
(Act_Spec
, New_List
);
2917 Analyze_Associations
2919 Generic_Formal_Declarations
(Act_Tree
),
2920 Generic_Formal_Declarations
(Gen_Decl
));
2922 Set_Defining_Unit_Name
(Act_Spec
, Act_Decl_Name
);
2923 Set_Is_Generic_Instance
(Act_Decl_Id
);
2925 Set_Generic_Parent
(Act_Spec
, Gen_Unit
);
2927 -- References to the generic in its own declaration or its body
2928 -- are references to the instance. Add a renaming declaration for
2929 -- the generic unit itself. This declaration, as well as the renaming
2930 -- declarations for the generic formals, must remain private to the
2931 -- unit: the formals, because this is the language semantics, and
2932 -- the unit because its use is an artifact of the implementation.
2935 Make_Package_Renaming_Declaration
(Loc
,
2936 Defining_Unit_Name
=>
2937 Make_Defining_Identifier
(Loc
, Chars
(Gen_Unit
)),
2938 Name
=> New_Reference_To
(Act_Decl_Id
, Loc
));
2940 Append
(Unit_Renaming
, Renaming_List
);
2942 -- The renaming declarations are the first local declarations of
2945 if Is_Non_Empty_List
(Visible_Declarations
(Act_Spec
)) then
2947 (First
(Visible_Declarations
(Act_Spec
)), Renaming_List
);
2949 Set_Visible_Declarations
(Act_Spec
, Renaming_List
);
2953 Make_Package_Declaration
(Loc
,
2954 Specification
=> Act_Spec
);
2956 -- Save the instantiation node, for subsequent instantiation
2957 -- of the body, if there is one and we are generating code for
2958 -- the current unit. Mark the unit as having a body, to avoid
2959 -- a premature error message.
2961 -- We instantiate the body if we are generating code, if we are
2962 -- generating cross-reference information, or if we are building
2963 -- trees for ASIS use.
2966 Enclosing_Body_Present
: Boolean := False;
2967 -- If the generic unit is not a compilation unit, then a body
2968 -- may be present in its parent even if none is required. We
2969 -- create a tentative pending instantiation for the body, which
2970 -- will be discarded if none is actually present.
2975 if Scope
(Gen_Unit
) /= Standard_Standard
2976 and then not Is_Child_Unit
(Gen_Unit
)
2978 Scop
:= Scope
(Gen_Unit
);
2980 while Present
(Scop
)
2981 and then Scop
/= Standard_Standard
2983 if Unit_Requires_Body
(Scop
) then
2984 Enclosing_Body_Present
:= True;
2987 elsif In_Open_Scopes
(Scop
)
2988 and then In_Package_Body
(Scop
)
2990 Enclosing_Body_Present
:= True;
2994 exit when Is_Compilation_Unit
(Scop
);
2995 Scop
:= Scope
(Scop
);
2999 -- If front-end inlining is enabled, and this is a unit for which
3000 -- code will be generated, we instantiate the body at once.
3001 -- This is done if the instance is not the main unit, and if the
3002 -- generic is not a child unit of another generic, to avoid scope
3003 -- problems and the reinstallation of parent instances.
3006 and then (not Is_Child_Unit
(Gen_Unit
)
3007 or else not Is_Generic_Unit
(Scope
(Gen_Unit
)))
3008 and then Might_Inline_Subp
3009 and then not Is_Actual_Pack
3011 if Front_End_Inlining
3012 and then (Is_In_Main_Unit
(N
)
3013 or else In_Main_Context
(Current_Scope
))
3014 and then Nkind
(Parent
(N
)) /= N_Compilation_Unit
3018 -- In configurable_run_time mode we force the inlining of
3019 -- predefined subprogram marked Inline_Always, to minimize
3020 -- the use of the run-time library.
3022 elsif Is_Predefined_File_Name
3023 (Unit_File_Name
(Get_Source_Unit
(Gen_Decl
)))
3024 and then Configurable_Run_Time_Mode
3025 and then Nkind
(Parent
(N
)) /= N_Compilation_Unit
3030 -- If the current scope is itself an instance within a child
3031 -- unit,there will be duplications in the scope stack, and the
3032 -- unstacking mechanism in Inline_Instance_Body will fail.
3033 -- This loses some rare cases of optimization, and might be
3034 -- improved some day, if we can find a proper abstraction for
3035 -- "the complete compilation context" that can be saved and
3038 if Is_Generic_Instance
(Current_Scope
) then
3040 Curr_Unit
: constant Entity_Id
:=
3041 Cunit_Entity
(Current_Sem_Unit
);
3043 if Curr_Unit
/= Current_Scope
3044 and then Is_Child_Unit
(Curr_Unit
)
3046 Inline_Now
:= False;
3053 (Unit_Requires_Body
(Gen_Unit
)
3054 or else Enclosing_Body_Present
3055 or else Present
(Corresponding_Body
(Gen_Decl
)))
3056 and then (Is_In_Main_Unit
(N
)
3057 or else Might_Inline_Subp
)
3058 and then not Is_Actual_Pack
3059 and then not Inline_Now
3060 and then (Operating_Mode
= Generate_Code
3061 or else (Operating_Mode
= Check_Semantics
3062 and then ASIS_Mode
));
3064 -- If front_end_inlining is enabled, do not instantiate a
3065 -- body if within a generic context.
3067 if (Front_End_Inlining
3068 and then not Expander_Active
)
3069 or else Is_Generic_Unit
(Cunit_Entity
(Main_Unit
))
3071 Needs_Body
:= False;
3074 -- If the current context is generic, and the package being
3075 -- instantiated is declared within a formal package, there is no
3076 -- body to instantiate until the enclosing generic is instantiated
3077 -- and there is an actual for the formal package. If the formal
3078 -- package has parameters, we build regular package instance for
3079 -- it, that preceeds the original formal package declaration.
3081 if In_Open_Scopes
(Scope
(Scope
(Gen_Unit
))) then
3083 Decl
: constant Node_Id
:=
3085 (Unit_Declaration_Node
(Scope
(Gen_Unit
)));
3087 if Nkind
(Decl
) = N_Formal_Package_Declaration
3088 or else (Nkind
(Decl
) = N_Package_Declaration
3089 and then Is_List_Member
(Decl
)
3090 and then Present
(Next
(Decl
))
3092 Nkind
(Next
(Decl
)) = N_Formal_Package_Declaration
)
3094 Needs_Body
:= False;
3100 -- If we are generating the calling stubs from the instantiation of
3101 -- a generic RCI package, we will not use the body of the generic
3104 if Distribution_Stub_Mode
= Generate_Caller_Stub_Body
3105 and then Is_Compilation_Unit
(Defining_Entity
(N
))
3107 Needs_Body
:= False;
3112 -- Here is a defence against a ludicrous number of instantiations
3113 -- caused by a circular set of instantiation attempts.
3115 if Pending_Instantiations
.Last
>
3116 Hostparm
.Max_Instantiations
3118 Error_Msg_N
("too many instantiations", N
);
3119 raise Unrecoverable_Error
;
3122 -- Indicate that the enclosing scopes contain an instantiation,
3123 -- and that cleanup actions should be delayed until after the
3124 -- instance body is expanded.
3126 Check_Forward_Instantiation
(Gen_Decl
);
3127 if Nkind
(N
) = N_Package_Instantiation
then
3129 Enclosing_Master
: Entity_Id
:= Current_Scope
;
3132 while Enclosing_Master
/= Standard_Standard
loop
3134 if Ekind
(Enclosing_Master
) = E_Package
then
3135 if Is_Compilation_Unit
(Enclosing_Master
) then
3136 if In_Package_Body
(Enclosing_Master
) then
3138 (Body_Entity
(Enclosing_Master
));
3147 Enclosing_Master
:= Scope
(Enclosing_Master
);
3150 elsif Ekind
(Enclosing_Master
) = E_Generic_Package
then
3151 Enclosing_Master
:= Scope
(Enclosing_Master
);
3153 elsif Is_Generic_Subprogram
(Enclosing_Master
)
3154 or else Ekind
(Enclosing_Master
) = E_Void
3156 -- Cleanup actions will eventually be performed on
3157 -- the enclosing instance, if any. enclosing scope
3158 -- is void in the formal part of a generic subp.
3163 if Ekind
(Enclosing_Master
) = E_Entry
3165 Ekind
(Scope
(Enclosing_Master
)) = E_Protected_Type
3168 Protected_Body_Subprogram
(Enclosing_Master
);
3171 Set_Delay_Cleanups
(Enclosing_Master
);
3173 while Ekind
(Enclosing_Master
) = E_Block
loop
3174 Enclosing_Master
:= Scope
(Enclosing_Master
);
3177 if Is_Subprogram
(Enclosing_Master
) then
3178 Delay_Descriptors
(Enclosing_Master
);
3180 elsif Is_Task_Type
(Enclosing_Master
) then
3182 TBP
: constant Node_Id
:=
3183 Get_Task_Body_Procedure
3187 if Present
(TBP
) then
3188 Delay_Descriptors
(TBP
);
3189 Set_Delay_Cleanups
(TBP
);
3199 -- Make entry in table
3201 Pending_Instantiations
.Increment_Last
;
3202 Pending_Instantiations
.Table
(Pending_Instantiations
.Last
) :=
3203 (N
, Act_Decl
, Expander_Active
, Current_Sem_Unit
);
3207 Set_Categorization_From_Pragmas
(Act_Decl
);
3209 if Parent_Installed
then
3213 Set_Instance_Spec
(N
, Act_Decl
);
3215 -- If not a compilation unit, insert the package declaration
3216 -- before the original instantiation node.
3218 if Nkind
(Parent
(N
)) /= N_Compilation_Unit
then
3219 Mark_Rewrite_Insertion
(Act_Decl
);
3220 Insert_Before
(N
, Act_Decl
);
3223 -- For an instantiation that is a compilation unit, place
3224 -- declaration on current node so context is complete
3225 -- for analysis (including nested instantiations). It this
3226 -- is the main unit, the declaration eventually replaces the
3227 -- instantiation node. If the instance body is later created, it
3228 -- replaces the instance node, and the declation is attached to
3229 -- it (see Build_Instance_Compilation_Unit_Nodes).
3232 if Cunit_Entity
(Current_Sem_Unit
) = Defining_Entity
(N
) then
3234 -- The entity for the current unit is the newly created one,
3235 -- and all semantic information is attached to it.
3237 Set_Cunit_Entity
(Current_Sem_Unit
, Act_Decl_Id
);
3239 -- If this is the main unit, replace the main entity as well
3241 if Current_Sem_Unit
= Main_Unit
then
3242 Main_Unit_Entity
:= Act_Decl_Id
;
3246 Set_Unit
(Parent
(N
), Act_Decl
);
3247 Set_Parent_Spec
(Act_Decl
, Parent_Spec
(N
));
3248 Set_Package_Instantiation
(Act_Decl_Id
, N
);
3250 Set_Unit
(Parent
(N
), N
);
3251 Set_Body_Required
(Parent
(N
), False);
3253 -- We never need elaboration checks on instantiations, since
3254 -- by definition, the body instantiation is elaborated at the
3255 -- same time as the spec instantiation.
3257 Set_Suppress_Elaboration_Warnings
(Act_Decl_Id
);
3258 Set_Kill_Elaboration_Checks
(Act_Decl_Id
);
3261 Check_Elab_Instantiation
(N
);
3263 if ABE_Is_Certain
(N
) and then Needs_Body
then
3264 Pending_Instantiations
.Decrement_Last
;
3266 Check_Hidden_Child_Unit
(N
, Gen_Unit
, Act_Decl_Id
);
3268 Set_First_Private_Entity
(Defining_Unit_Name
(Unit_Renaming
),
3269 First_Private_Entity
(Act_Decl_Id
));
3271 -- If the instantiation will receive a body, the unit will
3272 -- be transformed into a package body, and receive its own
3273 -- elaboration entity. Otherwise, the nature of the unit is
3274 -- now a package declaration.
3276 if Nkind
(Parent
(N
)) = N_Compilation_Unit
3277 and then not Needs_Body
3279 Rewrite
(N
, Act_Decl
);
3282 if Present
(Corresponding_Body
(Gen_Decl
))
3283 or else Unit_Requires_Body
(Gen_Unit
)
3285 Set_Has_Completion
(Act_Decl_Id
);
3288 Check_Formal_Packages
(Act_Decl_Id
);
3290 Restore_Private_Views
(Act_Decl_Id
);
3292 if not Generic_Separately_Compiled
(Gen_Unit
) then
3293 Inherit_Context
(Gen_Decl
, N
);
3296 if Parent_Installed
then
3301 Env_Installed
:= False;
3304 Validate_Categorization_Dependency
(N
, Act_Decl_Id
);
3306 -- Check restriction, but skip this if something went wrong in
3307 -- the above analysis, indicated by Act_Decl_Id being void.
3309 if Ekind
(Act_Decl_Id
) /= E_Void
3310 and then not Is_Library_Level_Entity
(Act_Decl_Id
)
3312 Check_Restriction
(No_Local_Allocators
, N
);
3316 Inline_Instance_Body
(N
, Gen_Unit
, Act_Decl
);
3319 -- The following is a tree patch for ASIS: ASIS needs separate nodes
3320 -- to be used as defining identifiers for a formal package and for the
3321 -- corresponding expanded package
3323 if Nkind
(N
) = N_Formal_Package_Declaration
then
3324 Act_Decl_Id
:= New_Copy
(Defining_Entity
(N
));
3325 Set_Comes_From_Source
(Act_Decl_Id
, True);
3326 Set_Is_Generic_Instance
(Act_Decl_Id
, False);
3327 Set_Defining_Identifier
(N
, Act_Decl_Id
);
3331 when Instantiation_Error
=>
3332 if Parent_Installed
then
3336 if Env_Installed
then
3339 end Analyze_Package_Instantiation
;
3341 --------------------------
3342 -- Inline_Instance_Body --
3343 --------------------------
3345 procedure Inline_Instance_Body
3347 Gen_Unit
: Entity_Id
;
3351 Gen_Comp
: constant Entity_Id
:=
3352 Cunit_Entity
(Get_Source_Unit
(Gen_Unit
));
3353 Curr_Comp
: constant Node_Id
:= Cunit
(Current_Sem_Unit
);
3354 Curr_Scope
: Entity_Id
:= Empty
;
3355 Curr_Unit
: constant Entity_Id
:=
3356 Cunit_Entity
(Current_Sem_Unit
);
3357 Removed
: Boolean := False;
3358 Num_Scopes
: Int
:= 0;
3360 Scope_Stack_Depth
: constant Int
:=
3361 Scope_Stack
.Last
- Scope_Stack
.First
+ 1;
3363 Use_Clauses
: array (1 .. Scope_Stack_Depth
) of Node_Id
;
3364 Instances
: array (1 .. Scope_Stack_Depth
) of Entity_Id
;
3365 Inner_Scopes
: array (1 .. Scope_Stack_Depth
) of Entity_Id
;
3366 Num_Inner
: Int
:= 0;
3367 N_Instances
: Int
:= 0;
3371 -- Case of generic unit defined in another unit. We must remove the
3372 -- complete context of the current unit to install that of the generic.
3374 if Gen_Comp
/= Cunit_Entity
(Current_Sem_Unit
) then
3376 -- Add some comments for the following two loops ???
3379 while Present
(S
) and then S
/= Standard_Standard
loop
3381 Num_Scopes
:= Num_Scopes
+ 1;
3383 Use_Clauses
(Num_Scopes
) :=
3385 (Scope_Stack
.Last
- Num_Scopes
+ 1).
3387 End_Use_Clauses
(Use_Clauses
(Num_Scopes
));
3389 exit when Scope_Stack
.Last
- Num_Scopes
+ 1 = Scope_Stack
.First
3390 or else Scope_Stack
.Table
3391 (Scope_Stack
.Last
- Num_Scopes
).Entity
3395 exit when Is_Generic_Instance
(S
)
3396 and then (In_Package_Body
(S
)
3397 or else Ekind
(S
) = E_Procedure
3398 or else Ekind
(S
) = E_Function
);
3402 Vis
:= Is_Immediately_Visible
(Gen_Comp
);
3404 -- Find and save all enclosing instances
3409 and then S
/= Standard_Standard
3411 if Is_Generic_Instance
(S
) then
3412 N_Instances
:= N_Instances
+ 1;
3413 Instances
(N_Instances
) := S
;
3415 exit when In_Package_Body
(S
);
3421 -- Remove context of current compilation unit, unless we are within a
3422 -- nested package instantiation, in which case the context has been
3423 -- removed previously.
3425 -- If current scope is the body of a child unit, remove context of
3431 and then S
/= Standard_Standard
3433 exit when Is_Generic_Instance
(S
)
3434 and then (In_Package_Body
(S
)
3435 or else Ekind
(S
) = E_Procedure
3436 or else Ekind
(S
) = E_Function
);
3439 or else (Ekind
(Curr_Unit
) = E_Package_Body
3440 and then S
= Spec_Entity
(Curr_Unit
))
3441 or else (Ekind
(Curr_Unit
) = E_Subprogram_Body
3444 (Unit_Declaration_Node
(Curr_Unit
)))
3448 -- Remove entities in current scopes from visibility, so
3449 -- that instance body is compiled in a clean environment.
3451 Save_Scope_Stack
(Handle_Use
=> False);
3453 if Is_Child_Unit
(S
) then
3455 -- Remove child unit from stack, as well as inner scopes.
3456 -- Removing the context of a child unit removes parent
3459 while Current_Scope
/= S
loop
3460 Num_Inner
:= Num_Inner
+ 1;
3461 Inner_Scopes
(Num_Inner
) := Current_Scope
;
3466 Remove_Context
(Curr_Comp
);
3470 Remove_Context
(Curr_Comp
);
3473 if Ekind
(Curr_Unit
) = E_Package_Body
then
3474 Remove_Context
(Library_Unit
(Curr_Comp
));
3480 pragma Assert
(Num_Inner
< Num_Scopes
);
3482 New_Scope
(Standard_Standard
);
3483 Scope_Stack
.Table
(Scope_Stack
.Last
).Is_Active_Stack_Base
:= True;
3484 Instantiate_Package_Body
3485 ((N
, Act_Decl
, Expander_Active
, Current_Sem_Unit
), True);
3490 Set_Is_Immediately_Visible
(Gen_Comp
, Vis
);
3492 -- Reset Generic_Instance flag so that use clauses can be installed
3493 -- in the proper order. (See Use_One_Package for effect of enclosing
3494 -- instances on processing of use clauses).
3496 for J
in 1 .. N_Instances
loop
3497 Set_Is_Generic_Instance
(Instances
(J
), False);
3501 Install_Context
(Curr_Comp
);
3503 if Present
(Curr_Scope
)
3504 and then Is_Child_Unit
(Curr_Scope
)
3506 New_Scope
(Curr_Scope
);
3507 Set_Is_Immediately_Visible
(Curr_Scope
);
3509 -- Finally, restore inner scopes as well
3511 for J
in reverse 1 .. Num_Inner
loop
3512 New_Scope
(Inner_Scopes
(J
));
3516 Restore_Scope_Stack
(Handle_Use
=> False);
3518 if Present
(Curr_Scope
)
3520 (In_Private_Part
(Curr_Scope
)
3521 or else In_Package_Body
(Curr_Scope
))
3523 -- Install private declaration of ancestor units, which
3524 -- are currently available. Restore_Scope_Stack and
3525 -- Install_Context only install the visible part of parents.
3530 Par
:= Scope
(Curr_Scope
);
3531 while (Present
(Par
))
3532 and then Par
/= Standard_Standard
3534 Install_Private_Declarations
(Par
);
3541 -- Restore use clauses. For a child unit, use clauses in the parents
3542 -- are restored when installing the context, so only those in inner
3543 -- scopes (and those local to the child unit itself) need to be
3544 -- installed explicitly.
3546 if Is_Child_Unit
(Curr_Unit
)
3549 for J
in reverse 1 .. Num_Inner
+ 1 loop
3550 Scope_Stack
.Table
(Scope_Stack
.Last
- J
+ 1).First_Use_Clause
:=
3552 Install_Use_Clauses
(Use_Clauses
(J
));
3556 for J
in reverse 1 .. Num_Scopes
loop
3557 Scope_Stack
.Table
(Scope_Stack
.Last
- J
+ 1).First_Use_Clause
:=
3559 Install_Use_Clauses
(Use_Clauses
(J
));
3563 for J
in 1 .. N_Instances
loop
3564 Set_Is_Generic_Instance
(Instances
(J
), True);
3567 -- If generic unit is in current unit, current context is correct
3570 Instantiate_Package_Body
3571 ((N
, Act_Decl
, Expander_Active
, Current_Sem_Unit
), True);
3573 end Inline_Instance_Body
;
3575 -------------------------------------
3576 -- Analyze_Procedure_Instantiation --
3577 -------------------------------------
3579 procedure Analyze_Procedure_Instantiation
(N
: Node_Id
) is
3581 Analyze_Subprogram_Instantiation
(N
, E_Procedure
);
3582 end Analyze_Procedure_Instantiation
;
3584 --------------------------------------
3585 -- Analyze_Subprogram_Instantiation --
3586 --------------------------------------
3588 procedure Analyze_Subprogram_Instantiation
3592 Loc
: constant Source_Ptr
:= Sloc
(N
);
3593 Gen_Id
: constant Node_Id
:= Name
(N
);
3595 Anon_Id
: constant Entity_Id
:=
3596 Make_Defining_Identifier
(Sloc
(Defining_Entity
(N
)),
3597 Chars
=> New_External_Name
3598 (Chars
(Defining_Entity
(N
)), 'R'));
3600 Act_Decl_Id
: Entity_Id
;
3605 Env_Installed
: Boolean := False;
3606 Gen_Unit
: Entity_Id
;
3608 Pack_Id
: Entity_Id
;
3609 Parent_Installed
: Boolean := False;
3610 Renaming_List
: List_Id
;
3612 procedure Analyze_Instance_And_Renamings
;
3613 -- The instance must be analyzed in a context that includes the
3614 -- mappings of generic parameters into actuals. We create a package
3615 -- declaration for this purpose, and a subprogram with an internal
3616 -- name within the package. The subprogram instance is simply an
3617 -- alias for the internal subprogram, declared in the current scope.
3619 ------------------------------------
3620 -- Analyze_Instance_And_Renamings --
3621 ------------------------------------
3623 procedure Analyze_Instance_And_Renamings
is
3624 Def_Ent
: constant Entity_Id
:= Defining_Entity
(N
);
3625 Pack_Decl
: Node_Id
;
3628 if Nkind
(Parent
(N
)) = N_Compilation_Unit
then
3630 -- For the case of a compilation unit, the container package
3631 -- has the same name as the instantiation, to insure that the
3632 -- binder calls the elaboration procedure with the right name.
3633 -- Copy the entity of the instance, which may have compilation
3634 -- level flags (e.g. Is_Child_Unit) set.
3636 Pack_Id
:= New_Copy
(Def_Ent
);
3639 -- Otherwise we use the name of the instantiation concatenated
3640 -- with its source position to ensure uniqueness if there are
3641 -- several instantiations with the same name.
3644 Make_Defining_Identifier
(Loc
,
3645 Chars
=> New_External_Name
3646 (Related_Id
=> Chars
(Def_Ent
),
3648 Suffix_Index
=> Source_Offset
(Sloc
(Def_Ent
))));
3651 Pack_Decl
:= Make_Package_Declaration
(Loc
,
3652 Specification
=> Make_Package_Specification
(Loc
,
3653 Defining_Unit_Name
=> Pack_Id
,
3654 Visible_Declarations
=> Renaming_List
,
3655 End_Label
=> Empty
));
3657 Set_Instance_Spec
(N
, Pack_Decl
);
3658 Set_Is_Generic_Instance
(Pack_Id
);
3659 Set_Needs_Debug_Info
(Pack_Id
);
3661 -- Case of not a compilation unit
3663 if Nkind
(Parent
(N
)) /= N_Compilation_Unit
then
3664 Mark_Rewrite_Insertion
(Pack_Decl
);
3665 Insert_Before
(N
, Pack_Decl
);
3666 Set_Has_Completion
(Pack_Id
);
3668 -- Case of an instantiation that is a compilation unit
3670 -- Place declaration on current node so context is complete
3671 -- for analysis (including nested instantiations), and for
3672 -- use in a context_clause (see Analyze_With_Clause).
3675 Set_Unit
(Parent
(N
), Pack_Decl
);
3676 Set_Parent_Spec
(Pack_Decl
, Parent_Spec
(N
));
3679 Analyze
(Pack_Decl
);
3680 Check_Formal_Packages
(Pack_Id
);
3681 Set_Is_Generic_Instance
(Pack_Id
, False);
3683 -- Body of the enclosing package is supplied when instantiating
3684 -- the subprogram body, after semantic analysis is completed.
3686 if Nkind
(Parent
(N
)) = N_Compilation_Unit
then
3688 -- Remove package itself from visibility, so it does not
3689 -- conflict with subprogram.
3691 Set_Name_Entity_Id
(Chars
(Pack_Id
), Homonym
(Pack_Id
));
3693 -- Set name and scope of internal subprogram so that the
3694 -- proper external name will be generated. The proper scope
3695 -- is the scope of the wrapper package. We need to generate
3696 -- debugging information for the internal subprogram, so set
3697 -- flag accordingly.
3699 Set_Chars
(Anon_Id
, Chars
(Defining_Entity
(N
)));
3700 Set_Scope
(Anon_Id
, Scope
(Pack_Id
));
3702 -- Mark wrapper package as referenced, to avoid spurious
3703 -- warnings if the instantiation appears in various with_
3704 -- clauses of subunits of the main unit.
3706 Set_Referenced
(Pack_Id
);
3709 Set_Is_Generic_Instance
(Anon_Id
);
3710 Set_Needs_Debug_Info
(Anon_Id
);
3711 Act_Decl_Id
:= New_Copy
(Anon_Id
);
3713 Set_Parent
(Act_Decl_Id
, Parent
(Anon_Id
));
3714 Set_Chars
(Act_Decl_Id
, Chars
(Defining_Entity
(N
)));
3715 Set_Sloc
(Act_Decl_Id
, Sloc
(Defining_Entity
(N
)));
3716 Set_Comes_From_Source
(Act_Decl_Id
, True);
3718 -- The signature may involve types that are not frozen yet, but
3719 -- the subprogram will be frozen at the point the wrapper package
3720 -- is frozen, so it does not need its own freeze node. In fact, if
3721 -- one is created, it might conflict with the freezing actions from
3722 -- the wrapper package (see 7206-013).
3724 Set_Has_Delayed_Freeze
(Anon_Id
, False);
3726 -- If the instance is a child unit, mark the Id accordingly. Mark
3727 -- the anonymous entity as well, which is the real subprogram and
3728 -- which is used when the instance appears in a context clause.
3730 Set_Is_Child_Unit
(Act_Decl_Id
, Is_Child_Unit
(Defining_Entity
(N
)));
3731 Set_Is_Child_Unit
(Anon_Id
, Is_Child_Unit
(Defining_Entity
(N
)));
3732 New_Overloaded_Entity
(Act_Decl_Id
);
3733 Check_Eliminated
(Act_Decl_Id
);
3735 -- In compilation unit case, kill elaboration checks on the
3736 -- instantiation, since they are never needed -- the body is
3737 -- instantiated at the same point as the spec.
3739 if Nkind
(Parent
(N
)) = N_Compilation_Unit
then
3740 Set_Suppress_Elaboration_Warnings
(Act_Decl_Id
);
3741 Set_Kill_Elaboration_Checks
(Act_Decl_Id
);
3742 Set_Is_Compilation_Unit
(Anon_Id
);
3744 Set_Cunit_Entity
(Current_Sem_Unit
, Pack_Id
);
3747 -- The instance is not a freezing point for the new subprogram
3749 Set_Is_Frozen
(Act_Decl_Id
, False);
3751 if Nkind
(Defining_Entity
(N
)) = N_Defining_Operator_Symbol
then
3752 Valid_Operator_Definition
(Act_Decl_Id
);
3755 Set_Alias
(Act_Decl_Id
, Anon_Id
);
3756 Set_Parent
(Act_Decl_Id
, Parent
(Anon_Id
));
3757 Set_Has_Completion
(Act_Decl_Id
);
3758 Set_Related_Instance
(Pack_Id
, Act_Decl_Id
);
3760 if Nkind
(Parent
(N
)) = N_Compilation_Unit
then
3761 Set_Body_Required
(Parent
(N
), False);
3764 end Analyze_Instance_And_Renamings
;
3766 -- Start of processing for Analyze_Subprogram_Instantiation
3769 -- Very first thing: apply the special kludge for Text_IO processing
3770 -- in case we are instantiating one of the children of [Wide_]Text_IO.
3771 -- Of course such an instantiation is bogus (these are packages, not
3772 -- subprograms), but we get a better error message if we do this.
3774 Text_IO_Kludge
(Gen_Id
);
3776 -- Make node global for error reporting
3778 Instantiation_Node
:= N
;
3779 Pre_Analyze_Actuals
(N
);
3782 Env_Installed
:= True;
3783 Check_Generic_Child_Unit
(Gen_Id
, Parent_Installed
);
3784 Gen_Unit
:= Entity
(Gen_Id
);
3786 Generate_Reference
(Gen_Unit
, Gen_Id
);
3788 if Nkind
(Gen_Id
) = N_Identifier
3789 and then Chars
(Gen_Unit
) = Chars
(Defining_Entity
(N
))
3792 ("& is hidden within declaration of instance", Gen_Id
, Gen_Unit
);
3795 if Etype
(Gen_Unit
) = Any_Type
then
3800 -- Verify that it is a generic subprogram of the right kind, and that
3801 -- it does not lead to a circular instantiation.
3803 if Ekind
(Gen_Unit
) /= E_Generic_Procedure
3804 and then Ekind
(Gen_Unit
) /= E_Generic_Function
3806 Error_Msg_N
("expect generic subprogram in instantiation", Gen_Id
);
3808 elsif In_Open_Scopes
(Gen_Unit
) then
3809 Error_Msg_NE
("instantiation of & within itself", N
, Gen_Unit
);
3811 elsif K
= E_Procedure
3812 and then Ekind
(Gen_Unit
) /= E_Generic_Procedure
3814 if Ekind
(Gen_Unit
) = E_Generic_Function
then
3816 ("cannot instantiate generic function as procedure", Gen_Id
);
3819 ("expect name of generic procedure in instantiation", Gen_Id
);
3822 elsif K
= E_Function
3823 and then Ekind
(Gen_Unit
) /= E_Generic_Function
3825 if Ekind
(Gen_Unit
) = E_Generic_Procedure
then
3827 ("cannot instantiate generic procedure as function", Gen_Id
);
3830 ("expect name of generic function in instantiation", Gen_Id
);
3834 Set_Entity
(Gen_Id
, Gen_Unit
);
3835 Set_Is_Instantiated
(Gen_Unit
);
3837 if In_Extended_Main_Source_Unit
(N
) then
3838 Generate_Reference
(Gen_Unit
, N
);
3841 -- If renaming, get original unit
3843 if Present
(Renamed_Object
(Gen_Unit
))
3844 and then (Ekind
(Renamed_Object
(Gen_Unit
)) = E_Generic_Procedure
3846 Ekind
(Renamed_Object
(Gen_Unit
)) = E_Generic_Function
)
3848 Gen_Unit
:= Renamed_Object
(Gen_Unit
);
3849 Set_Is_Instantiated
(Gen_Unit
);
3850 Generate_Reference
(Gen_Unit
, N
);
3853 if Contains_Instance_Of
(Gen_Unit
, Current_Scope
, Gen_Id
) then
3854 Error_Msg_Node_2
:= Current_Scope
;
3856 ("circular Instantiation: & instantiated in &!", N
, Gen_Unit
);
3857 Circularity_Detected
:= True;
3861 Gen_Decl
:= Unit_Declaration_Node
(Gen_Unit
);
3863 -- The subprogram itself cannot contain a nested instance, so
3864 -- the current parent is left empty.
3866 Set_Instance_Env
(Gen_Unit
, Empty
);
3868 -- Initialize renamings map, for error checking
3870 Generic_Renamings
.Set_Last
(0);
3871 Generic_Renamings_HTable
.Reset
;
3873 Create_Instantiation_Source
(N
, Gen_Unit
, False, S_Adjustment
);
3875 -- Copy original generic tree, to produce text for instantiation
3879 (Original_Node
(Gen_Decl
), Empty
, Instantiating
=> True);
3881 Act_Spec
:= Specification
(Act_Tree
);
3883 Analyze_Associations
3885 Generic_Formal_Declarations
(Act_Tree
),
3886 Generic_Formal_Declarations
(Gen_Decl
));
3888 -- Build the subprogram declaration, which does not appear
3889 -- in the generic template, and give it a sloc consistent
3890 -- with that of the template.
3892 Set_Defining_Unit_Name
(Act_Spec
, Anon_Id
);
3893 Set_Generic_Parent
(Act_Spec
, Gen_Unit
);
3895 Make_Subprogram_Declaration
(Sloc
(Act_Spec
),
3896 Specification
=> Act_Spec
);
3898 Set_Categorization_From_Pragmas
(Act_Decl
);
3900 if Parent_Installed
then
3904 Append
(Act_Decl
, Renaming_List
);
3905 Analyze_Instance_And_Renamings
;
3907 -- If the generic is marked Import (Intrinsic), then so is the
3908 -- instance. This indicates that there is no body to instantiate.
3909 -- If generic is marked inline, so it the instance, and the
3910 -- anonymous subprogram it renames. If inlined, or else if inlining
3911 -- is enabled for the compilation, we generate the instance body
3912 -- even if it is not within the main unit.
3914 -- Any other pragmas might also be inherited ???
3916 if Is_Intrinsic_Subprogram
(Gen_Unit
) then
3917 Set_Is_Intrinsic_Subprogram
(Anon_Id
);
3918 Set_Is_Intrinsic_Subprogram
(Act_Decl_Id
);
3920 if Chars
(Gen_Unit
) = Name_Unchecked_Conversion
then
3921 Validate_Unchecked_Conversion
(N
, Act_Decl_Id
);
3925 Generate_Definition
(Act_Decl_Id
);
3927 Set_Is_Inlined
(Act_Decl_Id
, Is_Inlined
(Gen_Unit
));
3928 Set_Is_Inlined
(Anon_Id
, Is_Inlined
(Gen_Unit
));
3930 if not Is_Intrinsic_Subprogram
(Gen_Unit
) then
3931 Check_Elab_Instantiation
(N
);
3934 if Is_Dispatching_Operation
(Act_Decl_Id
)
3935 and then Ada_Version
>= Ada_05
3941 Formal
:= First_Formal
(Act_Decl_Id
);
3942 while Present
(Formal
) loop
3943 if Ekind
(Etype
(Formal
)) = E_Anonymous_Access_Type
3944 and then Is_Controlling_Formal
(Formal
)
3945 and then not Can_Never_Be_Null
(Formal
)
3947 Error_Msg_NE
("access parameter& is controlling,",
3949 Error_Msg_NE
("\corresponding parameter of & must be"
3950 & " explicitly null-excluding", N
, Gen_Id
);
3953 Next_Formal
(Formal
);
3958 Check_Hidden_Child_Unit
(N
, Gen_Unit
, Act_Decl_Id
);
3960 -- Subject to change, pending on if other pragmas are inherited ???
3962 Validate_Categorization_Dependency
(N
, Act_Decl_Id
);
3964 if not Is_Intrinsic_Subprogram
(Act_Decl_Id
) then
3965 if not Generic_Separately_Compiled
(Gen_Unit
) then
3966 Inherit_Context
(Gen_Decl
, N
);
3969 Restore_Private_Views
(Pack_Id
, False);
3971 -- If the context requires a full instantiation, mark node for
3972 -- subsequent construction of the body.
3974 if (Is_In_Main_Unit
(N
)
3975 or else Is_Inlined
(Act_Decl_Id
))
3976 and then (Operating_Mode
= Generate_Code
3977 or else (Operating_Mode
= Check_Semantics
3978 and then ASIS_Mode
))
3979 and then (Expander_Active
or else ASIS_Mode
)
3980 and then not ABE_Is_Certain
(N
)
3981 and then not Is_Eliminated
(Act_Decl_Id
)
3983 Pending_Instantiations
.Increment_Last
;
3984 Pending_Instantiations
.Table
(Pending_Instantiations
.Last
) :=
3985 (N
, Act_Decl
, Expander_Active
, Current_Sem_Unit
);
3986 Check_Forward_Instantiation
(Gen_Decl
);
3988 -- The wrapper package is always delayed, because it does
3989 -- not constitute a freeze point, but to insure that the
3990 -- freeze node is placed properly, it is created directly
3991 -- when instantiating the body (otherwise the freeze node
3992 -- might appear to early for nested instantiations).
3994 elsif Nkind
(Parent
(N
)) = N_Compilation_Unit
then
3996 -- For ASIS purposes, indicate that the wrapper package has
3997 -- replaced the instantiation node.
3999 Rewrite
(N
, Unit
(Parent
(N
)));
4000 Set_Unit
(Parent
(N
), N
);
4003 elsif Nkind
(Parent
(N
)) = N_Compilation_Unit
then
4005 -- Replace instance node for library-level instantiations
4006 -- of intrinsic subprograms, for ASIS use.
4008 Rewrite
(N
, Unit
(Parent
(N
)));
4009 Set_Unit
(Parent
(N
), N
);
4012 if Parent_Installed
then
4017 Env_Installed
:= False;
4018 Generic_Renamings
.Set_Last
(0);
4019 Generic_Renamings_HTable
.Reset
;
4023 when Instantiation_Error
=>
4024 if Parent_Installed
then
4028 if Env_Installed
then
4031 end Analyze_Subprogram_Instantiation
;
4033 -------------------------
4034 -- Get_Associated_Node --
4035 -------------------------
4037 function Get_Associated_Node
(N
: Node_Id
) return Node_Id
is
4038 Assoc
: Node_Id
:= Associated_Node
(N
);
4041 if Nkind
(Assoc
) /= Nkind
(N
) then
4044 elsif Nkind
(Assoc
) = N_Aggregate
4045 or else Nkind
(Assoc
) = N_Extension_Aggregate
4050 -- If the node is part of an inner generic, it may itself have been
4051 -- remapped into a further generic copy. Associated_Node is otherwise
4052 -- used for the entity of the node, and will be of a different node
4053 -- kind, or else N has been rewritten as a literal or function call.
4055 while Present
(Associated_Node
(Assoc
))
4056 and then Nkind
(Associated_Node
(Assoc
)) = Nkind
(Assoc
)
4058 Assoc
:= Associated_Node
(Assoc
);
4061 -- Follow and additional link in case the final node was rewritten.
4062 -- This can only happen with nested generic units.
4064 if (Nkind
(Assoc
) = N_Identifier
or else Nkind
(Assoc
) in N_Op
)
4065 and then Present
(Associated_Node
(Assoc
))
4066 and then (Nkind
(Associated_Node
(Assoc
)) = N_Function_Call
4068 Nkind
(Associated_Node
(Assoc
)) = N_Explicit_Dereference
4070 Nkind
(Associated_Node
(Assoc
)) = N_Integer_Literal
4072 Nkind
(Associated_Node
(Assoc
)) = N_Real_Literal
4074 Nkind
(Associated_Node
(Assoc
)) = N_String_Literal
)
4076 Assoc
:= Associated_Node
(Assoc
);
4081 end Get_Associated_Node
;
4083 -------------------------------------------
4084 -- Build_Instance_Compilation_Unit_Nodes --
4085 -------------------------------------------
4087 procedure Build_Instance_Compilation_Unit_Nodes
4092 Decl_Cunit
: Node_Id
;
4093 Body_Cunit
: Node_Id
;
4095 New_Main
: constant Entity_Id
:= Defining_Entity
(Act_Decl
);
4096 Old_Main
: constant Entity_Id
:= Cunit_Entity
(Main_Unit
);
4099 -- A new compilation unit node is built for the instance declaration
4102 Make_Compilation_Unit
(Sloc
(N
),
4103 Context_Items
=> Empty_List
,
4106 Make_Compilation_Unit_Aux
(Sloc
(N
)));
4108 Set_Parent_Spec
(Act_Decl
, Parent_Spec
(N
));
4109 Set_Body_Required
(Decl_Cunit
, True);
4111 -- We use the original instantiation compilation unit as the resulting
4112 -- compilation unit of the instance, since this is the main unit.
4114 Rewrite
(N
, Act_Body
);
4115 Body_Cunit
:= Parent
(N
);
4117 -- The two compilation unit nodes are linked by the Library_Unit field
4119 Set_Library_Unit
(Decl_Cunit
, Body_Cunit
);
4120 Set_Library_Unit
(Body_Cunit
, Decl_Cunit
);
4122 -- Preserve the private nature of the package if needed
4124 Set_Private_Present
(Decl_Cunit
, Private_Present
(Body_Cunit
));
4126 -- If the instance is not the main unit, its context, categorization,
4127 -- and elaboration entity are not relevant to the compilation.
4129 if Parent
(N
) /= Cunit
(Main_Unit
) then
4133 -- The context clause items on the instantiation, which are now
4134 -- attached to the body compilation unit (since the body overwrote
4135 -- the original instantiation node), semantically belong on the spec,
4136 -- so copy them there. It's harmless to leave them on the body as well.
4137 -- In fact one could argue that they belong in both places.
4139 Citem
:= First
(Context_Items
(Body_Cunit
));
4140 while Present
(Citem
) loop
4141 Append
(New_Copy
(Citem
), Context_Items
(Decl_Cunit
));
4145 -- Propagate categorization flags on packages, so that they appear
4146 -- in ali file for the spec of the unit.
4148 if Ekind
(New_Main
) = E_Package
then
4149 Set_Is_Pure
(Old_Main
, Is_Pure
(New_Main
));
4150 Set_Is_Preelaborated
(Old_Main
, Is_Preelaborated
(New_Main
));
4151 Set_Is_Remote_Types
(Old_Main
, Is_Remote_Types
(New_Main
));
4152 Set_Is_Shared_Passive
(Old_Main
, Is_Shared_Passive
(New_Main
));
4153 Set_Is_Remote_Call_Interface
4154 (Old_Main
, Is_Remote_Call_Interface
(New_Main
));
4157 -- Make entry in Units table, so that binder can generate call to
4158 -- elaboration procedure for body, if any.
4160 Make_Instance_Unit
(Body_Cunit
);
4161 Main_Unit_Entity
:= New_Main
;
4162 Set_Cunit_Entity
(Main_Unit
, Main_Unit_Entity
);
4164 -- Build elaboration entity, since the instance may certainly
4165 -- generate elaboration code requiring a flag for protection.
4167 Build_Elaboration_Entity
(Decl_Cunit
, New_Main
);
4168 end Build_Instance_Compilation_Unit_Nodes
;
4170 -----------------------------
4171 -- Check_Access_Definition --
4172 -----------------------------
4174 procedure Check_Access_Definition
(N
: Node_Id
) is
4177 (Ada_Version
>= Ada_05
4178 and then Present
(Access_Definition
(N
)));
4180 end Check_Access_Definition
;
4182 -----------------------------------
4183 -- Check_Formal_Package_Instance --
4184 -----------------------------------
4186 -- If the formal has specific parameters, they must match those of the
4187 -- actual. Both of them are instances, and the renaming declarations
4188 -- for their formal parameters appear in the same order in both. The
4189 -- analyzed formal has been analyzed in the context of the current
4192 procedure Check_Formal_Package_Instance
4193 (Formal_Pack
: Entity_Id
;
4194 Actual_Pack
: Entity_Id
)
4196 E1
: Entity_Id
:= First_Entity
(Actual_Pack
);
4197 E2
: Entity_Id
:= First_Entity
(Formal_Pack
);
4202 procedure Check_Mismatch
(B
: Boolean);
4203 -- Common error routine for mismatch between the parameters of
4204 -- the actual instance and those of the formal package.
4206 function Same_Instantiated_Constant
(E1
, E2
: Entity_Id
) return Boolean;
4207 -- The formal may come from a nested formal package, and the actual
4208 -- may have been constant-folded. To determine whether the two denote
4209 -- the same entity we may have to traverse several definitions to
4210 -- recover the ultimate entity that they refer to.
4212 function Same_Instantiated_Variable
(E1
, E2
: Entity_Id
) return Boolean;
4213 -- Similarly, if the formal comes from a nested formal package, the
4214 -- actual may designate the formal through multiple renamings, which
4215 -- have to be followed to determine the original variable in question.
4217 --------------------
4218 -- Check_Mismatch --
4219 --------------------
4221 procedure Check_Mismatch
(B
: Boolean) is
4222 Kind
: constant Node_Kind
:= Nkind
(Parent
(E2
));
4225 if Kind
= N_Formal_Type_Declaration
then
4228 elsif Kind
= N_Formal_Object_Declaration
4229 or else Kind
in N_Formal_Subprogram_Declaration
4230 or else Kind
= N_Formal_Package_Declaration
4236 ("actual for & in actual instance does not match formal",
4237 Parent
(Actual_Pack
), E1
);
4241 --------------------------------
4242 -- Same_Instantiated_Constant --
4243 --------------------------------
4245 function Same_Instantiated_Constant
4246 (E1
, E2
: Entity_Id
) return Boolean
4252 while Present
(Ent
) loop
4256 elsif Ekind
(Ent
) /= E_Constant
then
4259 elsif Is_Entity_Name
(Constant_Value
(Ent
)) then
4260 if Entity
(Constant_Value
(Ent
)) = E1
then
4263 Ent
:= Entity
(Constant_Value
(Ent
));
4266 -- The actual may be a constant that has been folded. Recover
4269 elsif Is_Entity_Name
(Original_Node
(Constant_Value
(Ent
))) then
4270 Ent
:= Entity
(Original_Node
(Constant_Value
(Ent
)));
4277 end Same_Instantiated_Constant
;
4279 --------------------------------
4280 -- Same_Instantiated_Variable --
4281 --------------------------------
4283 function Same_Instantiated_Variable
4284 (E1
, E2
: Entity_Id
) return Boolean
4286 function Original_Entity
(E
: Entity_Id
) return Entity_Id
;
4287 -- Follow chain of renamings to the ultimate ancestor
4289 ---------------------
4290 -- Original_Entity --
4291 ---------------------
4293 function Original_Entity
(E
: Entity_Id
) return Entity_Id
is
4298 while Nkind
(Parent
(Orig
)) = N_Object_Renaming_Declaration
4299 and then Present
(Renamed_Object
(Orig
))
4300 and then Is_Entity_Name
(Renamed_Object
(Orig
))
4302 Orig
:= Entity
(Renamed_Object
(Orig
));
4306 end Original_Entity
;
4308 -- Start of processing for Same_Instantiated_Variable
4311 return Ekind
(E1
) = Ekind
(E2
)
4312 and then Original_Entity
(E1
) = Original_Entity
(E2
);
4313 end Same_Instantiated_Variable
;
4315 -- Start of processing for Check_Formal_Package_Instance
4319 and then Present
(E2
)
4321 exit when Ekind
(E1
) = E_Package
4322 and then Renamed_Entity
(E1
) = Renamed_Entity
(Actual_Pack
);
4324 if Is_Type
(E1
) then
4326 -- Subtypes must statically match. E1 and E2 are the
4327 -- local entities that are subtypes of the actuals.
4328 -- Itypes generated for other parameters need not be checked,
4329 -- the check will be performed on the parameters themselves.
4331 -- If E2 is a formal type declaration, it is a defaulted
4332 -- parameter and needs no checking.
4334 if not Is_Itype
(E1
)
4335 and then not Is_Itype
(E2
)
4339 or else Etype
(E1
) /= Etype
(E2
)
4340 or else not Subtypes_Statically_Match
(E1
, E2
));
4343 elsif Ekind
(E1
) = E_Constant
then
4345 -- IN parameters must denote the same static value, or
4346 -- the same constant, or the literal null.
4348 Expr1
:= Expression
(Parent
(E1
));
4350 if Ekind
(E2
) /= E_Constant
then
4351 Check_Mismatch
(True);
4354 Expr2
:= Expression
(Parent
(E2
));
4357 if Is_Static_Expression
(Expr1
) then
4359 if not Is_Static_Expression
(Expr2
) then
4360 Check_Mismatch
(True);
4362 elsif Is_Integer_Type
(Etype
(E1
)) then
4365 V1
: constant Uint
:= Expr_Value
(Expr1
);
4366 V2
: constant Uint
:= Expr_Value
(Expr2
);
4368 Check_Mismatch
(V1
/= V2
);
4371 elsif Is_Real_Type
(Etype
(E1
)) then
4373 V1
: constant Ureal
:= Expr_Value_R
(Expr1
);
4374 V2
: constant Ureal
:= Expr_Value_R
(Expr2
);
4376 Check_Mismatch
(V1
/= V2
);
4379 elsif Is_String_Type
(Etype
(E1
))
4380 and then Nkind
(Expr1
) = N_String_Literal
4383 if Nkind
(Expr2
) /= N_String_Literal
then
4384 Check_Mismatch
(True);
4387 (not String_Equal
(Strval
(Expr1
), Strval
(Expr2
)));
4391 elsif Is_Entity_Name
(Expr1
) then
4392 if Is_Entity_Name
(Expr2
) then
4393 if Entity
(Expr1
) = Entity
(Expr2
) then
4397 (not Same_Instantiated_Constant
4398 (Entity
(Expr1
), Entity
(Expr2
)));
4401 Check_Mismatch
(True);
4404 elsif Is_Entity_Name
(Original_Node
(Expr1
))
4405 and then Is_Entity_Name
(Expr2
)
4407 Same_Instantiated_Constant
4408 (Entity
(Original_Node
(Expr1
)), Entity
(Expr2
))
4412 elsif Nkind
(Expr1
) = N_Null
then
4413 Check_Mismatch
(Nkind
(Expr1
) /= N_Null
);
4416 Check_Mismatch
(True);
4419 elsif Ekind
(E1
) = E_Variable
then
4420 Check_Mismatch
(not Same_Instantiated_Variable
(E1
, E2
));
4422 elsif Ekind
(E1
) = E_Package
then
4424 (Ekind
(E1
) /= Ekind
(E2
)
4425 or else Renamed_Object
(E1
) /= Renamed_Object
(E2
));
4427 elsif Is_Overloadable
(E1
) then
4429 -- Verify that the names of the entities match.
4430 -- Note that actuals that are attributes are rewritten
4434 (Ekind
(E2
) /= Ekind
(E1
) or else (Alias
(E1
)) /= Alias
(E2
));
4437 raise Program_Error
;
4444 end Check_Formal_Package_Instance
;
4446 ---------------------------
4447 -- Check_Formal_Packages --
4448 ---------------------------
4450 procedure Check_Formal_Packages
(P_Id
: Entity_Id
) is
4452 Formal_P
: Entity_Id
;
4455 -- Iterate through the declarations in the instance, looking for
4456 -- package renaming declarations that denote instances of formal
4457 -- packages. Stop when we find the renaming of the current package
4458 -- itself. The declaration for a formal package without a box is
4459 -- followed by an internal entity that repeats the instantiation.
4461 E
:= First_Entity
(P_Id
);
4462 while Present
(E
) loop
4463 if Ekind
(E
) = E_Package
then
4464 if Renamed_Object
(E
) = P_Id
then
4467 elsif Nkind
(Parent
(E
)) /= N_Package_Renaming_Declaration
then
4470 elsif not Box_Present
(Parent
(Associated_Formal_Package
(E
))) then
4471 Formal_P
:= Next_Entity
(E
);
4472 Check_Formal_Package_Instance
(Formal_P
, E
);
4474 -- After checking, remove the internal validating package. It
4475 -- is only needed for semantic checks, and as it may contain
4476 -- generic formal declarations it should not reach gigi.
4478 Remove
(Unit_Declaration_Node
(Formal_P
));
4484 end Check_Formal_Packages
;
4486 ---------------------------------
4487 -- Check_Forward_Instantiation --
4488 ---------------------------------
4490 procedure Check_Forward_Instantiation
(Decl
: Node_Id
) is
4492 Gen_Comp
: Entity_Id
:= Cunit_Entity
(Get_Source_Unit
(Decl
));
4495 -- The instantiation appears before the generic body if we are in the
4496 -- scope of the unit containing the generic, either in its spec or in
4497 -- the package body. and before the generic body.
4499 if Ekind
(Gen_Comp
) = E_Package_Body
then
4500 Gen_Comp
:= Spec_Entity
(Gen_Comp
);
4503 if In_Open_Scopes
(Gen_Comp
)
4504 and then No
(Corresponding_Body
(Decl
))
4509 and then not Is_Compilation_Unit
(S
)
4510 and then not Is_Child_Unit
(S
)
4512 if Ekind
(S
) = E_Package
then
4513 Set_Has_Forward_Instantiation
(S
);
4519 end Check_Forward_Instantiation
;
4521 ---------------------------
4522 -- Check_Generic_Actuals --
4523 ---------------------------
4525 -- The visibility of the actuals may be different between the
4526 -- point of generic instantiation and the instantiation of the body.
4528 procedure Check_Generic_Actuals
4529 (Instance
: Entity_Id
;
4530 Is_Formal_Box
: Boolean)
4535 function Denotes_Previous_Actual
(Typ
: Entity_Id
) return Boolean;
4536 -- For a formal that is an array type, the component type is often
4537 -- a previous formal in the same unit. The privacy status of the
4538 -- component type will have been examined earlier in the traversal
4539 -- of the corresponding actuals, and this status should not be
4540 -- modified for the array type itself.
4541 -- To detect this case we have to rescan the list of formals, which
4542 -- is usually short enough to ignore the resulting inefficiency.
4544 function Denotes_Previous_Actual
(Typ
: Entity_Id
) return Boolean is
4547 Prev
:= First_Entity
(Instance
);
4548 while Present
(Prev
) loop
4550 and then Nkind
(Parent
(Prev
)) = N_Subtype_Declaration
4551 and then Is_Entity_Name
(Subtype_Indication
(Parent
(Prev
)))
4552 and then Entity
(Subtype_Indication
(Parent
(Prev
))) = Typ
4562 end Denotes_Previous_Actual
;
4564 -- Start of processing for Check_Generic_Actuals
4567 E
:= First_Entity
(Instance
);
4568 while Present
(E
) loop
4570 and then Nkind
(Parent
(E
)) = N_Subtype_Declaration
4571 and then Scope
(Etype
(E
)) /= Instance
4572 and then Is_Entity_Name
(Subtype_Indication
(Parent
(E
)))
4574 if Is_Array_Type
(E
)
4575 and then Denotes_Previous_Actual
(Component_Type
(E
))
4579 Check_Private_View
(Subtype_Indication
(Parent
(E
)));
4581 Set_Is_Generic_Actual_Type
(E
, True);
4582 Set_Is_Hidden
(E
, False);
4583 Set_Is_Potentially_Use_Visible
(E
,
4586 -- We constructed the generic actual type as a subtype of
4587 -- the supplied type. This means that it normally would not
4588 -- inherit subtype specific attributes of the actual, which
4589 -- is wrong for the generic case.
4591 Astype
:= Ancestor_Subtype
(E
);
4595 -- can happen when E is an itype that is the full view of
4596 -- a private type completed, e.g. with a constrained array.
4598 Astype
:= Base_Type
(E
);
4601 Set_Size_Info
(E
, (Astype
));
4602 Set_RM_Size
(E
, RM_Size
(Astype
));
4603 Set_First_Rep_Item
(E
, First_Rep_Item
(Astype
));
4605 if Is_Discrete_Or_Fixed_Point_Type
(E
) then
4606 Set_RM_Size
(E
, RM_Size
(Astype
));
4608 -- In nested instances, the base type of an access actual
4609 -- may itself be private, and need to be exchanged.
4611 elsif Is_Access_Type
(E
)
4612 and then Is_Private_Type
(Etype
(E
))
4615 (New_Occurrence_Of
(Etype
(E
), Sloc
(Instance
)));
4618 elsif Ekind
(E
) = E_Package
then
4620 -- If this is the renaming for the current instance, we're done.
4621 -- Otherwise it is a formal package. If the corresponding formal
4622 -- was declared with a box, the (instantiations of the) generic
4623 -- formal part are also visible. Otherwise, ignore the entity
4624 -- created to validate the actuals.
4626 if Renamed_Object
(E
) = Instance
then
4629 elsif Nkind
(Parent
(E
)) /= N_Package_Renaming_Declaration
then
4632 -- The visibility of a formal of an enclosing generic is already
4635 elsif Denotes_Formal_Package
(E
) then
4638 elsif Present
(Associated_Formal_Package
(E
))
4639 and then not Is_Generic_Formal
(E
)
4641 if Box_Present
(Parent
(Associated_Formal_Package
(E
))) then
4642 Check_Generic_Actuals
(Renamed_Object
(E
), True);
4645 Check_Generic_Actuals
(Renamed_Object
(E
), False);
4648 Set_Is_Hidden
(E
, False);
4651 -- If this is a subprogram instance (in a wrapper package) the
4652 -- actual is fully visible.
4654 elsif Is_Wrapper_Package
(Instance
) then
4655 Set_Is_Hidden
(E
, False);
4657 -- If the formal package is declared with a box, or if the formal
4658 -- parameter is defaulted, it is visible in the body.
4661 or else Is_Visible_Formal
(E
)
4663 Set_Is_Hidden
(E
, False);
4668 end Check_Generic_Actuals
;
4670 ------------------------------
4671 -- Check_Generic_Child_Unit --
4672 ------------------------------
4674 procedure Check_Generic_Child_Unit
4676 Parent_Installed
: in out Boolean)
4678 Loc
: constant Source_Ptr
:= Sloc
(Gen_Id
);
4679 Gen_Par
: Entity_Id
:= Empty
;
4680 Inst_Par
: Entity_Id
;
4684 function Find_Generic_Child
4686 Id
: Node_Id
) return Entity_Id
;
4687 -- Search generic parent for possible child unit with the given name
4689 function In_Enclosing_Instance
return Boolean;
4690 -- Within an instance of the parent, the child unit may be denoted
4691 -- by a simple name, or an abbreviated expanded name. Examine enclosing
4692 -- scopes to locate a possible parent instantiation.
4694 ------------------------
4695 -- Find_Generic_Child --
4696 ------------------------
4698 function Find_Generic_Child
4700 Id
: Node_Id
) return Entity_Id
4705 -- If entity of name is already set, instance has already been
4706 -- resolved, e.g. in an enclosing instantiation.
4708 if Present
(Entity
(Id
)) then
4709 if Scope
(Entity
(Id
)) = Scop
then
4716 E
:= First_Entity
(Scop
);
4717 while Present
(E
) loop
4718 if Chars
(E
) = Chars
(Id
)
4719 and then Is_Child_Unit
(E
)
4721 if Is_Child_Unit
(E
)
4722 and then not Is_Visible_Child_Unit
(E
)
4725 ("generic child unit& is not visible", Gen_Id
, E
);
4737 end Find_Generic_Child
;
4739 ---------------------------
4740 -- In_Enclosing_Instance --
4741 ---------------------------
4743 function In_Enclosing_Instance
return Boolean is
4744 Enclosing_Instance
: Node_Id
;
4745 Instance_Decl
: Node_Id
;
4748 -- We do not inline any call that contains instantiations, except
4749 -- for instantiations of Unchecked_Conversion, so if we are within
4750 -- an inlined body the current instance does not require parents.
4752 if In_Inlined_Body
then
4753 pragma Assert
(Chars
(Gen_Id
) = Name_Unchecked_Conversion
);
4757 -- Loop to check enclosing scopes
4759 Enclosing_Instance
:= Current_Scope
;
4760 while Present
(Enclosing_Instance
) loop
4761 Instance_Decl
:= Unit_Declaration_Node
(Enclosing_Instance
);
4763 if Ekind
(Enclosing_Instance
) = E_Package
4764 and then Is_Generic_Instance
(Enclosing_Instance
)
4766 (Generic_Parent
(Specification
(Instance_Decl
)))
4768 -- Check whether the generic we are looking for is a child
4769 -- of this instance.
4771 E
:= Find_Generic_Child
4772 (Generic_Parent
(Specification
(Instance_Decl
)), Gen_Id
);
4773 exit when Present
(E
);
4779 Enclosing_Instance
:= Scope
(Enclosing_Instance
);
4791 Make_Expanded_Name
(Loc
,
4793 Prefix
=> New_Occurrence_Of
(Enclosing_Instance
, Loc
),
4794 Selector_Name
=> New_Occurrence_Of
(E
, Loc
)));
4796 Set_Entity
(Gen_Id
, E
);
4797 Set_Etype
(Gen_Id
, Etype
(E
));
4798 Parent_Installed
:= False; -- Already in scope.
4801 end In_Enclosing_Instance
;
4803 -- Start of processing for Check_Generic_Child_Unit
4806 -- If the name of the generic is given by a selected component, it
4807 -- may be the name of a generic child unit, and the prefix is the name
4808 -- of an instance of the parent, in which case the child unit must be
4809 -- visible. If this instance is not in scope, it must be placed there
4810 -- and removed after instantiation, because what is being instantiated
4811 -- is not the original child, but the corresponding child present in
4812 -- the instance of the parent.
4814 -- If the child is instantiated within the parent, it can be given by
4815 -- a simple name. In this case the instance is already in scope, but
4816 -- the child generic must be recovered from the generic parent as well.
4818 if Nkind
(Gen_Id
) = N_Selected_Component
then
4819 S
:= Selector_Name
(Gen_Id
);
4820 Analyze
(Prefix
(Gen_Id
));
4821 Inst_Par
:= Entity
(Prefix
(Gen_Id
));
4823 if Ekind
(Inst_Par
) = E_Package
4824 and then Present
(Renamed_Object
(Inst_Par
))
4826 Inst_Par
:= Renamed_Object
(Inst_Par
);
4829 if Ekind
(Inst_Par
) = E_Package
then
4830 if Nkind
(Parent
(Inst_Par
)) = N_Package_Specification
then
4831 Gen_Par
:= Generic_Parent
(Parent
(Inst_Par
));
4833 elsif Nkind
(Parent
(Inst_Par
)) = N_Defining_Program_Unit_Name
4835 Nkind
(Parent
(Parent
(Inst_Par
))) = N_Package_Specification
4837 Gen_Par
:= Generic_Parent
(Parent
(Parent
(Inst_Par
)));
4840 elsif Ekind
(Inst_Par
) = E_Generic_Package
4841 and then Nkind
(Parent
(Gen_Id
)) = N_Formal_Package_Declaration
4843 -- A formal package may be a real child package, and not the
4844 -- implicit instance within a parent. In this case the child is
4845 -- not visible and has to be retrieved explicitly as well.
4847 Gen_Par
:= Inst_Par
;
4850 if Present
(Gen_Par
) then
4852 -- The prefix denotes an instantiation. The entity itself
4853 -- may be a nested generic, or a child unit.
4855 E
:= Find_Generic_Child
(Gen_Par
, S
);
4858 Change_Selected_Component_To_Expanded_Name
(Gen_Id
);
4859 Set_Entity
(Gen_Id
, E
);
4860 Set_Etype
(Gen_Id
, Etype
(E
));
4862 Set_Etype
(S
, Etype
(E
));
4864 -- Indicate that this is a reference to the parent
4866 if In_Extended_Main_Source_Unit
(Gen_Id
) then
4867 Set_Is_Instantiated
(Inst_Par
);
4870 -- A common mistake is to replicate the naming scheme of
4871 -- a hierarchy by instantiating a generic child directly,
4872 -- rather than the implicit child in a parent instance:
4874 -- generic .. package Gpar is ..
4875 -- generic .. package Gpar.Child is ..
4876 -- package Par is new Gpar ();
4879 -- package Par.Child is new Gpar.Child ();
4880 -- rather than Par.Child
4882 -- In this case the instantiation is within Par, which is
4883 -- an instance, but Gpar does not denote Par because we are
4884 -- not IN the instance of Gpar, so this is illegal. The test
4885 -- below recognizes this particular case.
4887 if Is_Child_Unit
(E
)
4888 and then not Comes_From_Source
(Entity
(Prefix
(Gen_Id
)))
4889 and then (not In_Instance
4890 or else Nkind
(Parent
(Parent
(Gen_Id
))) =
4894 ("prefix of generic child unit must be instance of parent",
4898 if not In_Open_Scopes
(Inst_Par
)
4899 and then Nkind
(Parent
(Gen_Id
)) not in
4900 N_Generic_Renaming_Declaration
4902 Install_Parent
(Inst_Par
);
4903 Parent_Installed
:= True;
4907 -- If the generic parent does not contain an entity that
4908 -- corresponds to the selector, the instance doesn't either.
4909 -- Analyzing the node will yield the appropriate error message.
4910 -- If the entity is not a child unit, then it is an inner
4911 -- generic in the parent.
4919 if Is_Child_Unit
(Entity
(Gen_Id
))
4921 Nkind
(Parent
(Gen_Id
)) not in N_Generic_Renaming_Declaration
4922 and then not In_Open_Scopes
(Inst_Par
)
4924 Install_Parent
(Inst_Par
);
4925 Parent_Installed
:= True;
4929 elsif Nkind
(Gen_Id
) = N_Expanded_Name
then
4931 -- Entity already present, analyze prefix, whose meaning may be
4932 -- an instance in the current context. If it is an instance of
4933 -- a relative within another, the proper parent may still have
4934 -- to be installed, if they are not of the same generation.
4936 Analyze
(Prefix
(Gen_Id
));
4937 Inst_Par
:= Entity
(Prefix
(Gen_Id
));
4939 if In_Enclosing_Instance
then
4942 elsif Present
(Entity
(Gen_Id
))
4943 and then Is_Child_Unit
(Entity
(Gen_Id
))
4944 and then not In_Open_Scopes
(Inst_Par
)
4946 Install_Parent
(Inst_Par
);
4947 Parent_Installed
:= True;
4950 elsif In_Enclosing_Instance
then
4952 -- The child unit is found in some enclosing scope
4959 -- If this is the renaming of the implicit child in a parent
4960 -- instance, recover the parent name and install it.
4962 if Is_Entity_Name
(Gen_Id
) then
4963 E
:= Entity
(Gen_Id
);
4965 if Is_Generic_Unit
(E
)
4966 and then Nkind
(Parent
(E
)) in N_Generic_Renaming_Declaration
4967 and then Is_Child_Unit
(Renamed_Object
(E
))
4968 and then Is_Generic_Unit
(Scope
(Renamed_Object
(E
)))
4969 and then Nkind
(Name
(Parent
(E
))) = N_Expanded_Name
4972 New_Copy_Tree
(Name
(Parent
(E
))));
4973 Inst_Par
:= Entity
(Prefix
(Gen_Id
));
4975 if not In_Open_Scopes
(Inst_Par
) then
4976 Install_Parent
(Inst_Par
);
4977 Parent_Installed
:= True;
4980 -- If it is a child unit of a non-generic parent, it may be
4981 -- use-visible and given by a direct name. Install parent as
4984 elsif Is_Generic_Unit
(E
)
4985 and then Is_Child_Unit
(E
)
4987 Nkind
(Parent
(Gen_Id
)) not in N_Generic_Renaming_Declaration
4988 and then not Is_Generic_Unit
(Scope
(E
))
4990 if not In_Open_Scopes
(Scope
(E
)) then
4991 Install_Parent
(Scope
(E
));
4992 Parent_Installed
:= True;
4997 end Check_Generic_Child_Unit
;
4999 -----------------------------
5000 -- Check_Hidden_Child_Unit --
5001 -----------------------------
5003 procedure Check_Hidden_Child_Unit
5005 Gen_Unit
: Entity_Id
;
5006 Act_Decl_Id
: Entity_Id
)
5008 Gen_Id
: constant Node_Id
:= Name
(N
);
5011 if Is_Child_Unit
(Gen_Unit
)
5012 and then Is_Child_Unit
(Act_Decl_Id
)
5013 and then Nkind
(Gen_Id
) = N_Expanded_Name
5014 and then Entity
(Prefix
(Gen_Id
)) = Scope
(Act_Decl_Id
)
5015 and then Chars
(Gen_Unit
) = Chars
(Act_Decl_Id
)
5017 Error_Msg_Node_2
:= Scope
(Act_Decl_Id
);
5019 ("generic unit & is implicitly declared in &",
5020 Defining_Unit_Name
(N
), Gen_Unit
);
5021 Error_Msg_N
("\instance must have different name",
5022 Defining_Unit_Name
(N
));
5024 end Check_Hidden_Child_Unit
;
5026 ------------------------
5027 -- Check_Private_View --
5028 ------------------------
5030 procedure Check_Private_View
(N
: Node_Id
) is
5031 T
: constant Entity_Id
:= Etype
(N
);
5035 -- Exchange views if the type was not private in the generic but is
5036 -- private at the point of instantiation. Do not exchange views if
5037 -- the scope of the type is in scope. This can happen if both generic
5038 -- and instance are sibling units, or if type is defined in a parent.
5039 -- In this case the visibility of the type will be correct for all
5043 BT
:= Base_Type
(T
);
5045 if Is_Private_Type
(T
)
5046 and then not Has_Private_View
(N
)
5047 and then Present
(Full_View
(T
))
5048 and then not In_Open_Scopes
(Scope
(T
))
5050 -- In the generic, the full type was visible. Save the
5051 -- private entity, for subsequent exchange.
5055 elsif Has_Private_View
(N
)
5056 and then not Is_Private_Type
(T
)
5057 and then not Has_Been_Exchanged
(T
)
5058 and then Etype
(Get_Associated_Node
(N
)) /= T
5060 -- Only the private declaration was visible in the generic. If
5061 -- the type appears in a subtype declaration, the subtype in the
5062 -- instance must have a view compatible with that of its parent,
5063 -- which must be exchanged (see corresponding code in Restore_
5064 -- Private_Views). Otherwise, if the type is defined in a parent
5065 -- unit, leave full visibility within instance, which is safe.
5067 if In_Open_Scopes
(Scope
(Base_Type
(T
)))
5068 and then not Is_Private_Type
(Base_Type
(T
))
5069 and then Comes_From_Source
(Base_Type
(T
))
5073 elsif Nkind
(Parent
(N
)) = N_Subtype_Declaration
5074 or else not In_Private_Part
(Scope
(Base_Type
(T
)))
5076 Prepend_Elmt
(T
, Exchanged_Views
);
5077 Exchange_Declarations
(Etype
(Get_Associated_Node
(N
)));
5080 -- For composite types with inconsistent representation
5081 -- exchange component types accordingly.
5083 elsif Is_Access_Type
(T
)
5084 and then Is_Private_Type
(Designated_Type
(T
))
5085 and then not Has_Private_View
(N
)
5086 and then Present
(Full_View
(Designated_Type
(T
)))
5088 Switch_View
(Designated_Type
(T
));
5090 elsif Is_Array_Type
(T
)
5091 and then Is_Private_Type
(Component_Type
(T
))
5092 and then not Has_Private_View
(N
)
5093 and then Present
(Full_View
(Component_Type
(T
)))
5095 Switch_View
(Component_Type
(T
));
5097 elsif Is_Private_Type
(T
)
5098 and then Present
(Full_View
(T
))
5099 and then Is_Array_Type
(Full_View
(T
))
5100 and then Is_Private_Type
(Component_Type
(Full_View
(T
)))
5104 -- Finally, a non-private subtype may have a private base type, which
5105 -- must be exchanged for consistency. This can happen when
5106 -- instantiating a package body, when the scope stack is empty but in
5107 -- fact the subtype and the base type are declared in an enclosing
5110 -- Note that in this case we introduce an inconsistency in the view
5111 -- set, because we switch the base type BT, but there could be some
5112 -- private dependent subtypes of BT which remain unswitched. Such
5113 -- subtypes might need to be switched at a later point (see specific
5114 -- provision for that case in Switch_View).
5116 elsif not Is_Private_Type
(T
)
5117 and then not Has_Private_View
(N
)
5118 and then Is_Private_Type
(BT
)
5119 and then Present
(Full_View
(BT
))
5120 and then not Is_Generic_Type
(BT
)
5121 and then not In_Open_Scopes
(BT
)
5123 Prepend_Elmt
(Full_View
(BT
), Exchanged_Views
);
5124 Exchange_Declarations
(BT
);
5127 end Check_Private_View
;
5129 --------------------------
5130 -- Contains_Instance_Of --
5131 --------------------------
5133 function Contains_Instance_Of
5136 N
: Node_Id
) return Boolean
5144 -- Verify that there are no circular instantiations. We check whether
5145 -- the unit contains an instance of the current scope or some enclosing
5146 -- scope (in case one of the instances appears in a subunit). Longer
5147 -- circularities involving subunits might seem too pathological to
5148 -- consider, but they were not too pathological for the authors of
5149 -- DEC bc30vsq, so we loop over all enclosing scopes, and mark all
5150 -- enclosing generic scopes as containing an instance.
5153 -- Within a generic subprogram body, the scope is not generic, to
5154 -- allow for recursive subprograms. Use the declaration to determine
5155 -- whether this is a generic unit.
5157 if Ekind
(Scop
) = E_Generic_Package
5158 or else (Is_Subprogram
(Scop
)
5159 and then Nkind
(Unit_Declaration_Node
(Scop
)) =
5160 N_Generic_Subprogram_Declaration
)
5162 Elmt
:= First_Elmt
(Inner_Instances
(Inner
));
5164 while Present
(Elmt
) loop
5165 if Node
(Elmt
) = Scop
then
5166 Error_Msg_Node_2
:= Inner
;
5168 ("circular Instantiation: & instantiated within &!",
5172 elsif Node
(Elmt
) = Inner
then
5175 elsif Contains_Instance_Of
(Node
(Elmt
), Scop
, N
) then
5176 Error_Msg_Node_2
:= Inner
;
5178 ("circular Instantiation: & instantiated within &!",
5186 -- Indicate that Inner is being instantiated within Scop
5188 Append_Elmt
(Inner
, Inner_Instances
(Scop
));
5191 if Scop
= Standard_Standard
then
5194 Scop
:= Scope
(Scop
);
5199 end Contains_Instance_Of
;
5201 -----------------------
5202 -- Copy_Generic_Node --
5203 -----------------------
5205 function Copy_Generic_Node
5207 Parent_Id
: Node_Id
;
5208 Instantiating
: Boolean) return Node_Id
5213 function Copy_Generic_Descendant
(D
: Union_Id
) return Union_Id
;
5214 -- Check the given value of one of the Fields referenced by the
5215 -- current node to determine whether to copy it recursively. The
5216 -- field may hold a Node_Id, a List_Id, or an Elist_Id, or a plain
5217 -- value (Sloc, Uint, Char) in which case it need not be copied.
5219 procedure Copy_Descendants
;
5220 -- Common utility for various nodes
5222 function Copy_Generic_Elist
(E
: Elist_Id
) return Elist_Id
;
5223 -- Make copy of element list
5225 function Copy_Generic_List
5227 Parent_Id
: Node_Id
) return List_Id
;
5228 -- Apply Copy_Node recursively to the members of a node list
5230 function In_Defining_Unit_Name
(Nam
: Node_Id
) return Boolean;
5231 -- True if an identifier is part of the defining program unit name
5232 -- of a child unit. The entity of such an identifier must be kept
5233 -- (for ASIS use) even though as the name of an enclosing generic
5234 -- it would otherwise not be preserved in the generic tree.
5236 ----------------------
5237 -- Copy_Descendants --
5238 ----------------------
5240 procedure Copy_Descendants
is
5242 use Atree
.Unchecked_Access
;
5243 -- This code section is part of the implementation of an untyped
5244 -- tree traversal, so it needs direct access to node fields.
5247 Set_Field1
(New_N
, Copy_Generic_Descendant
(Field1
(N
)));
5248 Set_Field2
(New_N
, Copy_Generic_Descendant
(Field2
(N
)));
5249 Set_Field3
(New_N
, Copy_Generic_Descendant
(Field3
(N
)));
5250 Set_Field4
(New_N
, Copy_Generic_Descendant
(Field4
(N
)));
5251 Set_Field5
(New_N
, Copy_Generic_Descendant
(Field5
(N
)));
5252 end Copy_Descendants
;
5254 -----------------------------
5255 -- Copy_Generic_Descendant --
5256 -----------------------------
5258 function Copy_Generic_Descendant
(D
: Union_Id
) return Union_Id
is
5260 if D
= Union_Id
(Empty
) then
5263 elsif D
in Node_Range
then
5265 (Copy_Generic_Node
(Node_Id
(D
), New_N
, Instantiating
));
5267 elsif D
in List_Range
then
5268 return Union_Id
(Copy_Generic_List
(List_Id
(D
), New_N
));
5270 elsif D
in Elist_Range
then
5271 return Union_Id
(Copy_Generic_Elist
(Elist_Id
(D
)));
5273 -- Nothing else is copyable (e.g. Uint values), return as is
5278 end Copy_Generic_Descendant
;
5280 ------------------------
5281 -- Copy_Generic_Elist --
5282 ------------------------
5284 function Copy_Generic_Elist
(E
: Elist_Id
) return Elist_Id
is
5291 M
:= First_Elmt
(E
);
5292 while Present
(M
) loop
5294 (Copy_Generic_Node
(Node
(M
), Empty
, Instantiating
), L
);
5303 end Copy_Generic_Elist
;
5305 -----------------------
5306 -- Copy_Generic_List --
5307 -----------------------
5309 function Copy_Generic_List
5311 Parent_Id
: Node_Id
) return List_Id
5319 Set_Parent
(New_L
, Parent_Id
);
5322 while Present
(N
) loop
5323 Append
(Copy_Generic_Node
(N
, Empty
, Instantiating
), New_L
);
5332 end Copy_Generic_List
;
5334 ---------------------------
5335 -- In_Defining_Unit_Name --
5336 ---------------------------
5338 function In_Defining_Unit_Name
(Nam
: Node_Id
) return Boolean is
5340 return Present
(Parent
(Nam
))
5341 and then (Nkind
(Parent
(Nam
)) = N_Defining_Program_Unit_Name
5343 (Nkind
(Parent
(Nam
)) = N_Expanded_Name
5344 and then In_Defining_Unit_Name
(Parent
(Nam
))));
5345 end In_Defining_Unit_Name
;
5347 -- Start of processing for Copy_Generic_Node
5354 New_N
:= New_Copy
(N
);
5356 if Instantiating
then
5357 Adjust_Instantiation_Sloc
(New_N
, S_Adjustment
);
5360 if not Is_List_Member
(N
) then
5361 Set_Parent
(New_N
, Parent_Id
);
5364 -- If defining identifier, then all fields have been copied already
5366 if Nkind
(New_N
) in N_Entity
then
5369 -- Special casing for identifiers and other entity names and operators
5371 elsif Nkind
(New_N
) = N_Identifier
5372 or else Nkind
(New_N
) = N_Character_Literal
5373 or else Nkind
(New_N
) = N_Expanded_Name
5374 or else Nkind
(New_N
) = N_Operator_Symbol
5375 or else Nkind
(New_N
) in N_Op
5377 if not Instantiating
then
5379 -- Link both nodes in order to assign subsequently the
5380 -- entity of the copy to the original node, in case this
5381 -- is a global reference.
5383 Set_Associated_Node
(N
, New_N
);
5385 -- If we are within an instantiation, this is a nested generic
5386 -- that has already been analyzed at the point of definition. We
5387 -- must preserve references that were global to the enclosing
5388 -- parent at that point. Other occurrences, whether global or
5389 -- local to the current generic, must be resolved anew, so we
5390 -- reset the entity in the generic copy. A global reference has
5391 -- a smaller depth than the parent, or else the same depth in
5392 -- case both are distinct compilation units.
5394 -- It is also possible for Current_Instantiated_Parent to be
5395 -- defined, and for this not to be a nested generic, namely
5396 -- if the unit is loaded through Rtsfind. In that case, the
5397 -- entity of New_N is only a link to the associated node, and
5398 -- not a defining occurrence.
5400 -- The entities for parent units in the defining_program_unit
5401 -- of a generic child unit are established when the context of
5402 -- the unit is first analyzed, before the generic copy is made.
5403 -- They are preserved in the copy for use in ASIS queries.
5405 Ent
:= Entity
(New_N
);
5407 if No
(Current_Instantiated_Parent
.Gen_Id
) then
5409 or else Nkind
(Ent
) /= N_Defining_Identifier
5410 or else not In_Defining_Unit_Name
(N
)
5412 Set_Associated_Node
(New_N
, Empty
);
5417 not (Nkind
(Ent
) = N_Defining_Identifier
5419 Nkind
(Ent
) = N_Defining_Character_Literal
5421 Nkind
(Ent
) = N_Defining_Operator_Symbol
)
5422 or else No
(Scope
(Ent
))
5423 or else Scope
(Ent
) = Current_Instantiated_Parent
.Gen_Id
5424 or else (Scope_Depth
(Scope
(Ent
)) >
5425 Scope_Depth
(Current_Instantiated_Parent
.Gen_Id
)
5427 Get_Source_Unit
(Ent
) =
5428 Get_Source_Unit
(Current_Instantiated_Parent
.Gen_Id
))
5430 Set_Associated_Node
(New_N
, Empty
);
5433 -- Case of instantiating identifier or some other name or operator
5436 -- If the associated node is still defined, the entity in
5437 -- it is global, and must be copied to the instance.
5438 -- If this copy is being made for a body to inline, it is
5439 -- applied to an instantiated tree, and the entity is already
5440 -- present and must be also preserved.
5443 Assoc
: constant Node_Id
:= Get_Associated_Node
(N
);
5445 if Present
(Assoc
) then
5446 if Nkind
(Assoc
) = Nkind
(N
) then
5447 Set_Entity
(New_N
, Entity
(Assoc
));
5448 Check_Private_View
(N
);
5450 elsif Nkind
(Assoc
) = N_Function_Call
then
5451 Set_Entity
(New_N
, Entity
(Name
(Assoc
)));
5453 elsif (Nkind
(Assoc
) = N_Defining_Identifier
5454 or else Nkind
(Assoc
) = N_Defining_Character_Literal
5455 or else Nkind
(Assoc
) = N_Defining_Operator_Symbol
)
5456 and then Expander_Active
5458 -- Inlining case: we are copying a tree that contains
5459 -- global entities, which are preserved in the copy
5460 -- to be used for subsequent inlining.
5465 Set_Entity
(New_N
, Empty
);
5471 -- For expanded name, we must copy the Prefix and Selector_Name
5473 if Nkind
(N
) = N_Expanded_Name
then
5475 (New_N
, Copy_Generic_Node
(Prefix
(N
), New_N
, Instantiating
));
5477 Set_Selector_Name
(New_N
,
5478 Copy_Generic_Node
(Selector_Name
(N
), New_N
, Instantiating
));
5480 -- For operators, we must copy the right operand
5482 elsif Nkind
(N
) in N_Op
then
5483 Set_Right_Opnd
(New_N
,
5484 Copy_Generic_Node
(Right_Opnd
(N
), New_N
, Instantiating
));
5486 -- And for binary operators, the left operand as well
5488 if Nkind
(N
) in N_Binary_Op
then
5489 Set_Left_Opnd
(New_N
,
5490 Copy_Generic_Node
(Left_Opnd
(N
), New_N
, Instantiating
));
5494 -- Special casing for stubs
5496 elsif Nkind
(N
) in N_Body_Stub
then
5498 -- In any case, we must copy the specification or defining
5499 -- identifier as appropriate.
5501 if Nkind
(N
) = N_Subprogram_Body_Stub
then
5502 Set_Specification
(New_N
,
5503 Copy_Generic_Node
(Specification
(N
), New_N
, Instantiating
));
5506 Set_Defining_Identifier
(New_N
,
5508 (Defining_Identifier
(N
), New_N
, Instantiating
));
5511 -- If we are not instantiating, then this is where we load and
5512 -- analyze subunits, i.e. at the point where the stub occurs. A
5513 -- more permissivle system might defer this analysis to the point
5514 -- of instantiation, but this seems to complicated for now.
5516 if not Instantiating
then
5518 Subunit_Name
: constant Unit_Name_Type
:= Get_Unit_Name
(N
);
5520 Unum
: Unit_Number_Type
;
5526 (Load_Name
=> Subunit_Name
,
5531 -- If the proper body is not found, a warning message will
5532 -- be emitted when analyzing the stub, or later at the the
5533 -- point of instantiation. Here we just leave the stub as is.
5535 if Unum
= No_Unit
then
5536 Subunits_Missing
:= True;
5537 goto Subunit_Not_Found
;
5540 Subunit
:= Cunit
(Unum
);
5542 if Nkind
(Unit
(Subunit
)) /= N_Subunit
then
5543 Error_Msg_Sloc
:= Sloc
(N
);
5545 ("expected SEPARATE subunit to complete stub at#,"
5546 & " found child unit", Subunit
);
5547 goto Subunit_Not_Found
;
5550 -- We must create a generic copy of the subunit, in order
5551 -- to perform semantic analysis on it, and we must replace
5552 -- the stub in the original generic unit with the subunit,
5553 -- in order to preserve non-local references within.
5555 -- Only the proper body needs to be copied. Library_Unit and
5556 -- context clause are simply inherited by the generic copy.
5557 -- Note that the copy (which may be recursive if there are
5558 -- nested subunits) must be done first, before attaching it
5559 -- to the enclosing generic.
5563 (Proper_Body
(Unit
(Subunit
)),
5564 Empty
, Instantiating
=> False);
5566 -- Now place the original proper body in the original
5567 -- generic unit. This is a body, not a compilation unit.
5569 Rewrite
(N
, Proper_Body
(Unit
(Subunit
)));
5570 Set_Is_Compilation_Unit
(Defining_Entity
(N
), False);
5571 Set_Was_Originally_Stub
(N
);
5573 -- Finally replace the body of the subunit with its copy,
5574 -- and make this new subunit into the library unit of the
5575 -- generic copy, which does not have stubs any longer.
5577 Set_Proper_Body
(Unit
(Subunit
), New_Body
);
5578 Set_Library_Unit
(New_N
, Subunit
);
5579 Inherit_Context
(Unit
(Subunit
), N
);
5582 -- If we are instantiating, this must be an error case, since
5583 -- otherwise we would have replaced the stub node by the proper
5584 -- body that corresponds. So just ignore it in the copy (i.e.
5585 -- we have copied it, and that is good enough).
5591 <<Subunit_Not_Found
>> null;
5593 -- If the node is a compilation unit, it is the subunit of a stub,
5594 -- which has been loaded already (see code below). In this case,
5595 -- the library unit field of N points to the parent unit (which
5596 -- is a compilation unit) and need not (and cannot!) be copied.
5598 -- When the proper body of the stub is analyzed, thie library_unit
5599 -- link is used to establish the proper context (see sem_ch10).
5601 -- The other fields of a compilation unit are copied as usual
5603 elsif Nkind
(N
) = N_Compilation_Unit
then
5605 -- This code can only be executed when not instantiating, because
5606 -- in the copy made for an instantiation, the compilation unit
5607 -- node has disappeared at the point that a stub is replaced by
5610 pragma Assert
(not Instantiating
);
5612 Set_Context_Items
(New_N
,
5613 Copy_Generic_List
(Context_Items
(N
), New_N
));
5616 Copy_Generic_Node
(Unit
(N
), New_N
, False));
5618 Set_First_Inlined_Subprogram
(New_N
,
5620 (First_Inlined_Subprogram
(N
), New_N
, False));
5622 Set_Aux_Decls_Node
(New_N
,
5623 Copy_Generic_Node
(Aux_Decls_Node
(N
), New_N
, False));
5625 -- For an assignment node, the assignment is known to be semantically
5626 -- legal if we are instantiating the template. This avoids incorrect
5627 -- diagnostics in generated code.
5629 elsif Nkind
(N
) = N_Assignment_Statement
then
5631 -- Copy name and expression fields in usual manner
5634 Copy_Generic_Node
(Name
(N
), New_N
, Instantiating
));
5636 Set_Expression
(New_N
,
5637 Copy_Generic_Node
(Expression
(N
), New_N
, Instantiating
));
5639 if Instantiating
then
5640 Set_Assignment_OK
(Name
(New_N
), True);
5643 elsif Nkind
(N
) = N_Aggregate
5644 or else Nkind
(N
) = N_Extension_Aggregate
5647 if not Instantiating
then
5648 Set_Associated_Node
(N
, New_N
);
5651 if Present
(Get_Associated_Node
(N
))
5652 and then Nkind
(Get_Associated_Node
(N
)) = Nkind
(N
)
5654 -- In the generic the aggregate has some composite type. If at
5655 -- the point of instantiation the type has a private view,
5656 -- install the full view (and that of its ancestors, if any).
5659 T
: Entity_Id
:= (Etype
(Get_Associated_Node
(New_N
)));
5664 and then Is_Private_Type
(T
)
5670 and then Is_Tagged_Type
(T
)
5671 and then Is_Derived_Type
(T
)
5673 Rt
:= Root_Type
(T
);
5678 if Is_Private_Type
(T
) then
5689 -- Do not copy the associated node, which points to
5690 -- the generic copy of the aggregate.
5693 use Atree
.Unchecked_Access
;
5694 -- This code section is part of the implementation of an untyped
5695 -- tree traversal, so it needs direct access to node fields.
5698 Set_Field1
(New_N
, Copy_Generic_Descendant
(Field1
(N
)));
5699 Set_Field2
(New_N
, Copy_Generic_Descendant
(Field2
(N
)));
5700 Set_Field3
(New_N
, Copy_Generic_Descendant
(Field3
(N
)));
5701 Set_Field5
(New_N
, Copy_Generic_Descendant
(Field5
(N
)));
5704 -- Allocators do not have an identifier denoting the access type,
5705 -- so we must locate it through the expression to check whether
5706 -- the views are consistent.
5708 elsif Nkind
(N
) = N_Allocator
5709 and then Nkind
(Expression
(N
)) = N_Qualified_Expression
5710 and then Is_Entity_Name
(Subtype_Mark
(Expression
(N
)))
5711 and then Instantiating
5714 T
: constant Node_Id
:=
5715 Get_Associated_Node
(Subtype_Mark
(Expression
(N
)));
5720 -- Retrieve the allocator node in the generic copy
5722 Acc_T
:= Etype
(Parent
(Parent
(T
)));
5724 and then Is_Private_Type
(Acc_T
)
5726 Switch_View
(Acc_T
);
5733 -- For a proper body, we must catch the case of a proper body that
5734 -- replaces a stub. This represents the point at which a separate
5735 -- compilation unit, and hence template file, may be referenced, so
5736 -- we must make a new source instantiation entry for the template
5737 -- of the subunit, and ensure that all nodes in the subunit are
5738 -- adjusted using this new source instantiation entry.
5740 elsif Nkind
(N
) in N_Proper_Body
then
5742 Save_Adjustment
: constant Sloc_Adjustment
:= S_Adjustment
;
5745 if Instantiating
and then Was_Originally_Stub
(N
) then
5746 Create_Instantiation_Source
5747 (Instantiation_Node
,
5748 Defining_Entity
(N
),
5753 -- Now copy the fields of the proper body, using the new
5754 -- adjustment factor if one was needed as per test above.
5758 -- Restore the original adjustment factor in case changed
5760 S_Adjustment
:= Save_Adjustment
;
5763 -- Don't copy Ident or Comment pragmas, since the comment belongs
5764 -- to the generic unit, not to the instantiating unit.
5766 elsif Nkind
(N
) = N_Pragma
5767 and then Instantiating
5770 Prag_Id
: constant Pragma_Id
:= Get_Pragma_Id
(Chars
(N
));
5773 if Prag_Id
= Pragma_Ident
5774 or else Prag_Id
= Pragma_Comment
5776 New_N
:= Make_Null_Statement
(Sloc
(N
));
5783 elsif Nkind
(N
) = N_Integer_Literal
5784 or else Nkind
(N
) = N_Real_Literal
5786 -- No descendant fields need traversing
5790 -- For the remaining nodes, copy recursively their descendants
5796 and then Nkind
(N
) = N_Subprogram_Body
5798 Set_Generic_Parent
(Specification
(New_N
), N
);
5803 end Copy_Generic_Node
;
5805 ----------------------------
5806 -- Denotes_Formal_Package --
5807 ----------------------------
5809 function Denotes_Formal_Package
5811 On_Exit
: Boolean := False) return Boolean
5814 Scop
: constant Entity_Id
:= Scope
(Pack
);
5821 (Instance_Envs
.Last
).Instantiated_Parent
.Act_Id
;
5823 Par
:= Current_Instantiated_Parent
.Act_Id
;
5826 if Ekind
(Scop
) = E_Generic_Package
5827 or else Nkind
(Unit_Declaration_Node
(Scop
)) =
5828 N_Generic_Subprogram_Declaration
5832 elsif Nkind
(Original_Node
(Unit_Declaration_Node
(Pack
))) =
5833 N_Formal_Package_Declaration
5841 -- Check whether this package is associated with a formal
5842 -- package of the enclosing instantiation. Iterate over the
5843 -- list of renamings.
5845 E
:= First_Entity
(Par
);
5846 while Present
(E
) loop
5847 if Ekind
(E
) /= E_Package
5848 or else Nkind
(Parent
(E
)) /= N_Package_Renaming_Declaration
5852 elsif Renamed_Object
(E
) = Par
then
5855 elsif Renamed_Object
(E
) = Pack
then
5864 end Denotes_Formal_Package
;
5870 procedure End_Generic
is
5872 -- ??? More things could be factored out in this
5873 -- routine. Should probably be done at a later stage.
5875 Inside_A_Generic
:= Generic_Flags
.Table
(Generic_Flags
.Last
);
5876 Generic_Flags
.Decrement_Last
;
5878 Expander_Mode_Restore
;
5881 ----------------------
5882 -- Find_Actual_Type --
5883 ----------------------
5885 function Find_Actual_Type
5887 Gen_Scope
: Entity_Id
) return Entity_Id
5892 if not Is_Child_Unit
(Gen_Scope
) then
5893 return Get_Instance_Of
(Typ
);
5895 elsif not Is_Generic_Type
(Typ
)
5896 or else Scope
(Typ
) = Gen_Scope
5898 return Get_Instance_Of
(Typ
);
5901 T
:= Current_Entity
(Typ
);
5902 while Present
(T
) loop
5903 if In_Open_Scopes
(Scope
(T
)) then
5906 elsif Is_Generic_Actual_Type
(T
) then
5915 end Find_Actual_Type
;
5917 ----------------------------
5918 -- Freeze_Subprogram_Body --
5919 ----------------------------
5921 procedure Freeze_Subprogram_Body
5922 (Inst_Node
: Node_Id
;
5924 Pack_Id
: Entity_Id
)
5927 Gen_Unit
: constant Entity_Id
:= Get_Generic_Entity
(Inst_Node
);
5928 Par
: constant Entity_Id
:= Scope
(Gen_Unit
);
5933 function Earlier
(N1
, N2
: Node_Id
) return Boolean;
5934 -- Yields True if N1 and N2 appear in the same compilation unit,
5935 -- ignoring subunits, and if N1 is to the left of N2 in a left-to-right
5936 -- traversal of the tree for the unit.
5938 function Enclosing_Body
(N
: Node_Id
) return Node_Id
;
5939 -- Find innermost package body that encloses the given node, and which
5940 -- is not a compilation unit. Freeze nodes for the instance, or for its
5941 -- enclosing body, may be inserted after the enclosing_body of the
5944 function Package_Freeze_Node
(B
: Node_Id
) return Node_Id
;
5945 -- Find entity for given package body, and locate or create a freeze
5948 function True_Parent
(N
: Node_Id
) return Node_Id
;
5949 -- For a subunit, return parent of corresponding stub
5955 function Earlier
(N1
, N2
: Node_Id
) return Boolean is
5961 procedure Find_Depth
(P
: in out Node_Id
; D
: in out Integer);
5962 -- Find distance from given node to enclosing compilation unit
5968 procedure Find_Depth
(P
: in out Node_Id
; D
: in out Integer) is
5971 and then Nkind
(P
) /= N_Compilation_Unit
5973 P
:= True_Parent
(P
);
5978 -- Start of procesing for Earlier
5981 Find_Depth
(P1
, D1
);
5982 Find_Depth
(P2
, D2
);
5992 P1
:= True_Parent
(P1
);
5997 P2
:= True_Parent
(P2
);
6001 -- At this point P1 and P2 are at the same distance from the root.
6002 -- We examine their parents until we find a common declarative
6003 -- list, at which point we can establish their relative placement
6004 -- by comparing their ultimate slocs. If we reach the root,
6005 -- N1 and N2 do not descend from the same declarative list (e.g.
6006 -- one is nested in the declarative part and the other is in a block
6007 -- in the statement part) and the earlier one is already frozen.
6009 while not Is_List_Member
(P1
)
6010 or else not Is_List_Member
(P2
)
6011 or else List_Containing
(P1
) /= List_Containing
(P2
)
6013 P1
:= True_Parent
(P1
);
6014 P2
:= True_Parent
(P2
);
6016 if Nkind
(Parent
(P1
)) = N_Subunit
then
6017 P1
:= Corresponding_Stub
(Parent
(P1
));
6020 if Nkind
(Parent
(P2
)) = N_Subunit
then
6021 P2
:= Corresponding_Stub
(Parent
(P2
));
6030 Top_Level_Location
(Sloc
(P1
)) < Top_Level_Location
(Sloc
(P2
));
6033 --------------------
6034 -- Enclosing_Body --
6035 --------------------
6037 function Enclosing_Body
(N
: Node_Id
) return Node_Id
is
6038 P
: Node_Id
:= Parent
(N
);
6042 and then Nkind
(Parent
(P
)) /= N_Compilation_Unit
6044 if Nkind
(P
) = N_Package_Body
then
6046 if Nkind
(Parent
(P
)) = N_Subunit
then
6047 return Corresponding_Stub
(Parent
(P
));
6053 P
:= True_Parent
(P
);
6059 -------------------------
6060 -- Package_Freeze_Node --
6061 -------------------------
6063 function Package_Freeze_Node
(B
: Node_Id
) return Node_Id
is
6067 if Nkind
(B
) = N_Package_Body
then
6068 Id
:= Corresponding_Spec
(B
);
6070 else pragma Assert
(Nkind
(B
) = N_Package_Body_Stub
);
6071 Id
:= Corresponding_Spec
(Proper_Body
(Unit
(Library_Unit
(B
))));
6074 Ensure_Freeze_Node
(Id
);
6075 return Freeze_Node
(Id
);
6076 end Package_Freeze_Node
;
6082 function True_Parent
(N
: Node_Id
) return Node_Id
is
6084 if Nkind
(Parent
(N
)) = N_Subunit
then
6085 return Parent
(Corresponding_Stub
(Parent
(N
)));
6091 -- Start of processing of Freeze_Subprogram_Body
6094 -- If the instance and the generic body appear within the same
6095 -- unit, and the instance preceeds the generic, the freeze node for
6096 -- the instance must appear after that of the generic. If the generic
6097 -- is nested within another instance I2, then current instance must
6098 -- be frozen after I2. In both cases, the freeze nodes are those of
6099 -- enclosing packages. Otherwise, the freeze node is placed at the end
6100 -- of the current declarative part.
6102 Enc_G
:= Enclosing_Body
(Gen_Body
);
6103 Enc_I
:= Enclosing_Body
(Inst_Node
);
6104 Ensure_Freeze_Node
(Pack_Id
);
6105 F_Node
:= Freeze_Node
(Pack_Id
);
6107 if Is_Generic_Instance
(Par
)
6108 and then Present
(Freeze_Node
(Par
))
6110 In_Same_Declarative_Part
(Freeze_Node
(Par
), Inst_Node
)
6112 if ABE_Is_Certain
(Get_Package_Instantiation_Node
(Par
)) then
6114 -- The parent was a premature instantiation. Insert freeze
6115 -- node at the end the current declarative part.
6117 Insert_After_Last_Decl
(Inst_Node
, F_Node
);
6120 Insert_After
(Freeze_Node
(Par
), F_Node
);
6123 -- The body enclosing the instance should be frozen after the body
6124 -- that includes the generic, because the body of the instance may
6125 -- make references to entities therein. If the two are not in the
6126 -- same declarative part, or if the one enclosing the instance is
6127 -- frozen already, freeze the instance at the end of the current
6128 -- declarative part.
6130 elsif Is_Generic_Instance
(Par
)
6131 and then Present
(Freeze_Node
(Par
))
6132 and then Present
(Enc_I
)
6134 if In_Same_Declarative_Part
(Freeze_Node
(Par
), Enc_I
)
6136 (Nkind
(Enc_I
) = N_Package_Body
6138 In_Same_Declarative_Part
(Freeze_Node
(Par
), Parent
(Enc_I
)))
6140 -- The enclosing package may contain several instances. Rather
6141 -- than computing the earliest point at which to insert its
6142 -- freeze node, we place it at the end of the declarative part
6143 -- of the parent of the generic.
6145 Insert_After_Last_Decl
6146 (Freeze_Node
(Par
), Package_Freeze_Node
(Enc_I
));
6149 Insert_After_Last_Decl
(Inst_Node
, F_Node
);
6151 elsif Present
(Enc_G
)
6152 and then Present
(Enc_I
)
6153 and then Enc_G
/= Enc_I
6154 and then Earlier
(Inst_Node
, Gen_Body
)
6156 if Nkind
(Enc_G
) = N_Package_Body
then
6157 E_G_Id
:= Corresponding_Spec
(Enc_G
);
6158 else pragma Assert
(Nkind
(Enc_G
) = N_Package_Body_Stub
);
6160 Corresponding_Spec
(Proper_Body
(Unit
(Library_Unit
(Enc_G
))));
6163 -- Freeze package that encloses instance, and place node after
6164 -- package that encloses generic. If enclosing package is already
6165 -- frozen we have to assume it is at the proper place. This may
6166 -- be a potential ABE that requires dynamic checking.
6168 Insert_After_Last_Decl
(Enc_G
, Package_Freeze_Node
(Enc_I
));
6170 -- Freeze enclosing subunit before instance
6172 Ensure_Freeze_Node
(E_G_Id
);
6174 if not Is_List_Member
(Freeze_Node
(E_G_Id
)) then
6175 Insert_After
(Enc_G
, Freeze_Node
(E_G_Id
));
6178 Insert_After_Last_Decl
(Inst_Node
, F_Node
);
6181 -- If none of the above, insert freeze node at the end of the
6182 -- current declarative part.
6184 Insert_After_Last_Decl
(Inst_Node
, F_Node
);
6186 end Freeze_Subprogram_Body
;
6192 function Get_Gen_Id
(E
: Assoc_Ptr
) return Entity_Id
is
6194 return Generic_Renamings
.Table
(E
).Gen_Id
;
6197 ---------------------
6198 -- Get_Instance_Of --
6199 ---------------------
6201 function Get_Instance_Of
(A
: Entity_Id
) return Entity_Id
is
6202 Res
: constant Assoc_Ptr
:= Generic_Renamings_HTable
.Get
(A
);
6205 if Res
/= Assoc_Null
then
6206 return Generic_Renamings
.Table
(Res
).Act_Id
;
6208 -- On exit, entity is not instantiated: not a generic parameter,
6209 -- or else parameter of an inner generic unit.
6213 end Get_Instance_Of
;
6215 ------------------------------------
6216 -- Get_Package_Instantiation_Node --
6217 ------------------------------------
6219 function Get_Package_Instantiation_Node
(A
: Entity_Id
) return Node_Id
is
6220 Decl
: Node_Id
:= Unit_Declaration_Node
(A
);
6224 -- If the Package_Instantiation attribute has been set on the package
6225 -- entity, then use it directly when it (or its Original_Node) refers
6226 -- to an N_Package_Instantiation node. In principle it should be
6227 -- possible to have this field set in all cases, which should be
6228 -- investigated, and would allow this function to be significantly
6231 if Present
(Package_Instantiation
(A
)) then
6232 if Nkind
(Package_Instantiation
(A
)) = N_Package_Instantiation
then
6233 return Package_Instantiation
(A
);
6235 elsif Nkind
(Original_Node
(Package_Instantiation
(A
)))
6236 = N_Package_Instantiation
6238 return Original_Node
(Package_Instantiation
(A
));
6242 -- If the instantiation is a compilation unit that does not need a
6243 -- body then the instantiation node has been rewritten as a package
6244 -- declaration for the instance, and we return the original node.
6246 -- If it is a compilation unit and the instance node has not been
6247 -- rewritten, then it is still the unit of the compilation. Finally,
6248 -- if a body is present, this is a parent of the main unit whose body
6249 -- has been compiled for inlining purposes, and the instantiation node
6250 -- has been rewritten with the instance body.
6252 -- Otherwise the instantiation node appears after the declaration.
6253 -- If the entity is a formal package, the declaration may have been
6254 -- rewritten as a generic declaration (in the case of a formal with a
6255 -- box) or left as a formal package declaration if it has actuals, and
6256 -- is found with a forward search.
6258 if Nkind
(Parent
(Decl
)) = N_Compilation_Unit
then
6259 if Nkind
(Decl
) = N_Package_Declaration
6260 and then Present
(Corresponding_Body
(Decl
))
6262 Decl
:= Unit_Declaration_Node
(Corresponding_Body
(Decl
));
6265 if Nkind
(Original_Node
(Decl
)) = N_Package_Instantiation
then
6266 return Original_Node
(Decl
);
6268 return Unit
(Parent
(Decl
));
6271 elsif Nkind
(Decl
) = N_Package_Declaration
6272 and then Nkind
(Original_Node
(Decl
)) = N_Formal_Package_Declaration
6274 return Original_Node
(Decl
);
6277 Inst
:= Next
(Decl
);
6278 while Nkind
(Inst
) /= N_Package_Instantiation
6279 and then Nkind
(Inst
) /= N_Formal_Package_Declaration
6286 end Get_Package_Instantiation_Node
;
6288 ------------------------
6289 -- Has_Been_Exchanged --
6290 ------------------------
6292 function Has_Been_Exchanged
(E
: Entity_Id
) return Boolean is
6293 Next
: Elmt_Id
:= First_Elmt
(Exchanged_Views
);
6296 while Present
(Next
) loop
6297 if Full_View
(Node
(Next
)) = E
then
6305 end Has_Been_Exchanged
;
6311 function Hash
(F
: Entity_Id
) return HTable_Range
is
6313 return HTable_Range
(F
mod HTable_Size
);
6316 ------------------------
6317 -- Hide_Current_Scope --
6318 ------------------------
6320 procedure Hide_Current_Scope
is
6321 C
: constant Entity_Id
:= Current_Scope
;
6325 Set_Is_Hidden_Open_Scope
(C
);
6326 E
:= First_Entity
(C
);
6328 while Present
(E
) loop
6329 if Is_Immediately_Visible
(E
) then
6330 Set_Is_Immediately_Visible
(E
, False);
6331 Append_Elmt
(E
, Hidden_Entities
);
6337 -- Make the scope name invisible as well. This is necessary, but
6338 -- might conflict with calls to Rtsfind later on, in case the scope
6339 -- is a predefined one. There is no clean solution to this problem, so
6340 -- for now we depend on the user not redefining Standard itself in one
6341 -- of the parent units.
6343 if Is_Immediately_Visible
(C
)
6344 and then C
/= Standard_Standard
6346 Set_Is_Immediately_Visible
(C
, False);
6347 Append_Elmt
(C
, Hidden_Entities
);
6350 end Hide_Current_Scope
;
6356 procedure Init_Env
is
6357 Saved
: Instance_Env
;
6360 Saved
.Ada_Version
:= Ada_Version
;
6361 Saved
.Ada_Version_Explicit
:= Ada_Version_Explicit
;
6362 Saved
.Instantiated_Parent
:= Current_Instantiated_Parent
;
6363 Saved
.Exchanged_Views
:= Exchanged_Views
;
6364 Saved
.Hidden_Entities
:= Hidden_Entities
;
6365 Saved
.Current_Sem_Unit
:= Current_Sem_Unit
;
6366 Saved
.Parent_Unit_Visible
:= Parent_Unit_Visible
;
6367 Saved
.Instance_Parent_Unit
:= Instance_Parent_Unit
;
6368 Instance_Envs
.Increment_Last
;
6369 Instance_Envs
.Table
(Instance_Envs
.Last
) := Saved
;
6371 Exchanged_Views
:= New_Elmt_List
;
6372 Hidden_Entities
:= New_Elmt_List
;
6374 -- Make dummy entry for Instantiated parent. If generic unit is
6375 -- legal, this is set properly in Set_Instance_Env.
6377 Current_Instantiated_Parent
:=
6378 (Current_Scope
, Current_Scope
, Assoc_Null
);
6381 ------------------------------
6382 -- In_Same_Declarative_Part --
6383 ------------------------------
6385 function In_Same_Declarative_Part
6387 Inst
: Node_Id
) return Boolean
6389 Decls
: constant Node_Id
:= Parent
(F_Node
);
6390 Nod
: Node_Id
:= Parent
(Inst
);
6393 while Present
(Nod
) loop
6397 elsif Nkind
(Nod
) = N_Subprogram_Body
6398 or else Nkind
(Nod
) = N_Package_Body
6399 or else Nkind
(Nod
) = N_Task_Body
6400 or else Nkind
(Nod
) = N_Protected_Body
6401 or else Nkind
(Nod
) = N_Block_Statement
6405 elsif Nkind
(Nod
) = N_Subunit
then
6406 Nod
:= Corresponding_Stub
(Nod
);
6408 elsif Nkind
(Nod
) = N_Compilation_Unit
then
6411 Nod
:= Parent
(Nod
);
6416 end In_Same_Declarative_Part
;
6418 ---------------------
6419 -- In_Main_Context --
6420 ---------------------
6422 function In_Main_Context
(E
: Entity_Id
) return Boolean is
6428 if not Is_Compilation_Unit
(E
)
6429 or else Ekind
(E
) /= E_Package
6430 or else In_Private_Part
(E
)
6435 Context
:= Context_Items
(Cunit
(Main_Unit
));
6437 Clause
:= First
(Context
);
6438 while Present
(Clause
) loop
6439 if Nkind
(Clause
) = N_With_Clause
then
6440 Nam
:= Name
(Clause
);
6442 -- If the current scope is part of the context of the main unit,
6443 -- analysis of the corresponding with_clause is not complete, and
6444 -- the entity is not set. We use the Chars field directly, which
6445 -- might produce false positives in rare cases, but guarantees
6446 -- that we produce all the instance bodies we will need.
6448 if (Nkind
(Nam
) = N_Identifier
6449 and then Chars
(Nam
) = Chars
(E
))
6450 or else (Nkind
(Nam
) = N_Selected_Component
6451 and then Chars
(Selector_Name
(Nam
)) = Chars
(E
))
6461 end In_Main_Context
;
6463 ---------------------
6464 -- Inherit_Context --
6465 ---------------------
6467 procedure Inherit_Context
(Gen_Decl
: Node_Id
; Inst
: Node_Id
) is
6468 Current_Context
: List_Id
;
6469 Current_Unit
: Node_Id
;
6474 if Nkind
(Parent
(Gen_Decl
)) = N_Compilation_Unit
then
6476 -- The inherited context is attached to the enclosing compilation
6477 -- unit. This is either the main unit, or the declaration for the
6478 -- main unit (in case the instantation appears within the package
6479 -- declaration and the main unit is its body).
6481 Current_Unit
:= Parent
(Inst
);
6482 while Present
(Current_Unit
)
6483 and then Nkind
(Current_Unit
) /= N_Compilation_Unit
6485 Current_Unit
:= Parent
(Current_Unit
);
6488 Current_Context
:= Context_Items
(Current_Unit
);
6490 Item
:= First
(Context_Items
(Parent
(Gen_Decl
)));
6491 while Present
(Item
) loop
6492 if Nkind
(Item
) = N_With_Clause
then
6493 New_I
:= New_Copy
(Item
);
6494 Set_Implicit_With
(New_I
, True);
6495 Append
(New_I
, Current_Context
);
6501 end Inherit_Context
;
6507 procedure Initialize
is
6509 Generic_Renamings
.Init
;
6512 Generic_Renamings_HTable
.Reset
;
6513 Circularity_Detected
:= False;
6514 Exchanged_Views
:= No_Elist
;
6515 Hidden_Entities
:= No_Elist
;
6518 ----------------------------
6519 -- Insert_After_Last_Decl --
6520 ----------------------------
6522 procedure Insert_After_Last_Decl
(N
: Node_Id
; F_Node
: Node_Id
) is
6523 L
: List_Id
:= List_Containing
(N
);
6524 P
: constant Node_Id
:= Parent
(L
);
6527 if not Is_List_Member
(F_Node
) then
6528 if Nkind
(P
) = N_Package_Specification
6529 and then L
= Visible_Declarations
(P
)
6530 and then Present
(Private_Declarations
(P
))
6531 and then not Is_Empty_List
(Private_Declarations
(P
))
6533 L
:= Private_Declarations
(P
);
6536 Insert_After
(Last
(L
), F_Node
);
6538 end Insert_After_Last_Decl
;
6544 procedure Install_Body
6545 (Act_Body
: Node_Id
;
6550 Act_Id
: constant Entity_Id
:= Corresponding_Spec
(Act_Body
);
6551 Act_Unit
: constant Node_Id
:= Unit
(Cunit
(Get_Source_Unit
(N
)));
6552 Gen_Id
: constant Entity_Id
:= Corresponding_Spec
(Gen_Body
);
6553 Par
: constant Entity_Id
:= Scope
(Gen_Id
);
6554 Gen_Unit
: constant Node_Id
:=
6555 Unit
(Cunit
(Get_Source_Unit
(Gen_Decl
)));
6556 Orig_Body
: Node_Id
:= Gen_Body
;
6558 Body_Unit
: Node_Id
;
6560 Must_Delay
: Boolean;
6562 function Enclosing_Subp
(Id
: Entity_Id
) return Entity_Id
;
6563 -- Find subprogram (if any) that encloses instance and/or generic body
6565 function True_Sloc
(N
: Node_Id
) return Source_Ptr
;
6566 -- If the instance is nested inside a generic unit, the Sloc of the
6567 -- instance indicates the place of the original definition, not the
6568 -- point of the current enclosing instance. Pending a better usage of
6569 -- Slocs to indicate instantiation places, we determine the place of
6570 -- origin of a node by finding the maximum sloc of any ancestor node.
6571 -- Why is this not equivalent to Top_Level_Location ???
6573 --------------------
6574 -- Enclosing_Subp --
6575 --------------------
6577 function Enclosing_Subp
(Id
: Entity_Id
) return Entity_Id
is
6578 Scop
: Entity_Id
:= Scope
(Id
);
6581 while Scop
/= Standard_Standard
6582 and then not Is_Overloadable
(Scop
)
6584 Scop
:= Scope
(Scop
);
6594 function True_Sloc
(N
: Node_Id
) return Source_Ptr
is
6601 while Present
(N1
) and then N1
/= Act_Unit
loop
6602 if Sloc
(N1
) > Res
then
6612 -- Start of processing for Install_Body
6615 -- If the body is a subunit, the freeze point is the corresponding
6616 -- stub in the current compilation, not the subunit itself.
6618 if Nkind
(Parent
(Gen_Body
)) = N_Subunit
then
6619 Orig_Body
:= Corresponding_Stub
(Parent
(Gen_Body
));
6621 Orig_Body
:= Gen_Body
;
6624 Body_Unit
:= Unit
(Cunit
(Get_Source_Unit
(Orig_Body
)));
6626 -- If the instantiation and the generic definition appear in the
6627 -- same package declaration, this is an early instantiation.
6628 -- If they appear in the same declarative part, it is an early
6629 -- instantiation only if the generic body appears textually later,
6630 -- and the generic body is also in the main unit.
6632 -- If instance is nested within a subprogram, and the generic body is
6633 -- not, the instance is delayed because the enclosing body is. If
6634 -- instance and body are within the same scope, or the same sub-
6635 -- program body, indicate explicitly that the instance is delayed.
6638 (Gen_Unit
= Act_Unit
6639 and then ((Nkind
(Gen_Unit
) = N_Package_Declaration
)
6640 or else Nkind
(Gen_Unit
) = N_Generic_Package_Declaration
6641 or else (Gen_Unit
= Body_Unit
6642 and then True_Sloc
(N
) < Sloc
(Orig_Body
)))
6643 and then Is_In_Main_Unit
(Gen_Unit
)
6644 and then (Scope
(Act_Id
) = Scope
(Gen_Id
)
6646 Enclosing_Subp
(Act_Id
) = Enclosing_Subp
(Gen_Id
)));
6648 -- If this is an early instantiation, the freeze node is placed after
6649 -- the generic body. Otherwise, if the generic appears in an instance,
6650 -- we cannot freeze the current instance until the outer one is frozen.
6651 -- This is only relevant if the current instance is nested within some
6652 -- inner scope not itself within the outer instance. If this scope is
6653 -- a package body in the same declarative part as the outer instance,
6654 -- then that body needs to be frozen after the outer instance. Finally,
6655 -- if no delay is needed, we place the freeze node at the end of the
6656 -- current declarative part.
6658 if Expander_Active
then
6659 Ensure_Freeze_Node
(Act_Id
);
6660 F_Node
:= Freeze_Node
(Act_Id
);
6663 Insert_After
(Orig_Body
, F_Node
);
6665 elsif Is_Generic_Instance
(Par
)
6666 and then Present
(Freeze_Node
(Par
))
6667 and then Scope
(Act_Id
) /= Par
6669 -- Freeze instance of inner generic after instance of enclosing
6672 if In_Same_Declarative_Part
(Freeze_Node
(Par
), N
) then
6673 Insert_After
(Freeze_Node
(Par
), F_Node
);
6675 -- Freeze package enclosing instance of inner generic after
6676 -- instance of enclosing generic.
6678 elsif Nkind
(Parent
(N
)) = N_Package_Body
6679 and then In_Same_Declarative_Part
(Freeze_Node
(Par
), Parent
(N
))
6683 Enclosing
: constant Entity_Id
:=
6684 Corresponding_Spec
(Parent
(N
));
6687 Insert_After_Last_Decl
(N
, F_Node
);
6688 Ensure_Freeze_Node
(Enclosing
);
6690 if not Is_List_Member
(Freeze_Node
(Enclosing
)) then
6691 Insert_After
(Freeze_Node
(Par
), Freeze_Node
(Enclosing
));
6696 Insert_After_Last_Decl
(N
, F_Node
);
6700 Insert_After_Last_Decl
(N
, F_Node
);
6704 Set_Is_Frozen
(Act_Id
);
6705 Insert_Before
(N
, Act_Body
);
6706 Mark_Rewrite_Insertion
(Act_Body
);
6709 --------------------
6710 -- Install_Parent --
6711 --------------------
6713 procedure Install_Parent
(P
: Entity_Id
; In_Body
: Boolean := False) is
6714 Ancestors
: constant Elist_Id
:= New_Elmt_List
;
6715 S
: constant Entity_Id
:= Current_Scope
;
6716 Inst_Par
: Entity_Id
;
6717 First_Par
: Entity_Id
;
6718 Inst_Node
: Node_Id
;
6719 Gen_Par
: Entity_Id
;
6720 First_Gen
: Entity_Id
;
6723 procedure Install_Formal_Packages
(Par
: Entity_Id
);
6724 -- If any of the formals of the parent are formal packages with box,
6725 -- their formal parts are visible in the parent and thus in the child
6726 -- unit as well. Analogous to what is done in Check_Generic_Actuals
6727 -- for the unit itself.
6729 procedure Install_Noninstance_Specs
(Par
: Entity_Id
);
6730 -- Install the scopes of noninstance parent units ending with Par
6732 procedure Install_Spec
(Par
: Entity_Id
);
6733 -- The child unit is within the declarative part of the parent, so
6734 -- the declarations within the parent are immediately visible.
6736 -----------------------------
6737 -- Install_Formal_Packages --
6738 -----------------------------
6740 procedure Install_Formal_Packages
(Par
: Entity_Id
) is
6744 E
:= First_Entity
(Par
);
6745 while Present
(E
) loop
6746 if Ekind
(E
) = E_Package
6747 and then Nkind
(Parent
(E
)) = N_Package_Renaming_Declaration
6749 -- If this is the renaming for the parent instance, done
6751 if Renamed_Object
(E
) = Par
then
6754 -- The visibility of a formal of an enclosing generic is
6757 elsif Denotes_Formal_Package
(E
) then
6760 elsif Present
(Associated_Formal_Package
(E
))
6761 and then Box_Present
(Parent
(Associated_Formal_Package
(E
)))
6763 Check_Generic_Actuals
(Renamed_Object
(E
), True);
6764 Set_Is_Hidden
(E
, False);
6770 end Install_Formal_Packages
;
6772 -------------------------------
6773 -- Install_Noninstance_Specs --
6774 -------------------------------
6776 procedure Install_Noninstance_Specs
(Par
: Entity_Id
) is
6779 and then Par
/= Standard_Standard
6780 and then not In_Open_Scopes
(Par
)
6782 Install_Noninstance_Specs
(Scope
(Par
));
6785 end Install_Noninstance_Specs
;
6791 procedure Install_Spec
(Par
: Entity_Id
) is
6792 Spec
: constant Node_Id
:=
6793 Specification
(Unit_Declaration_Node
(Par
));
6796 -- If this parent of the child instance is a top-level unit,
6797 -- then record the unit and its visibility for later resetting
6798 -- in Remove_Parent. We exclude units that are generic instances,
6799 -- as we only want to record this information for the ultimate
6800 -- top-level noninstance parent (is that always correct???).
6802 if Scope
(Par
) = Standard_Standard
6803 and then not Is_Generic_Instance
(Par
)
6805 Parent_Unit_Visible
:= Is_Immediately_Visible
(Par
);
6806 Instance_Parent_Unit
:= Par
;
6809 -- Open the parent scope and make it and its declarations visible.
6810 -- If this point is not within a body, then only the visible
6811 -- declarations should be made visible, and installation of the
6812 -- private declarations is deferred until the appropriate point
6813 -- within analysis of the spec being instantiated (see the handling
6814 -- of parent visibility in Analyze_Package_Specification). This is
6815 -- relaxed in the case where the parent unit is Ada.Tags, to avoid
6816 -- private view problems that occur when compiling instantiations of
6817 -- a generic child of that package (Generic_Dispatching_Constructor).
6818 -- If the instance freezes a tagged type, inlinings of operations
6819 -- from Ada.Tags may need the full view of type Tag. If inlining
6820 -- took proper account of establishing visibility of inlined
6821 -- subprograms' parents then it should be possible to remove this
6822 -- special check. ???
6825 Set_Is_Immediately_Visible
(Par
);
6826 Install_Visible_Declarations
(Par
);
6827 Set_Use
(Visible_Declarations
(Spec
));
6829 if In_Body
or else Is_RTU
(Par
, Ada_Tags
) then
6830 Install_Private_Declarations
(Par
);
6831 Set_Use
(Private_Declarations
(Spec
));
6835 -- Start of processing for Install_Parent
6838 -- We need to install the parent instance to compile the instantiation
6839 -- of the child, but the child instance must appear in the current
6840 -- scope. Given that we cannot place the parent above the current
6841 -- scope in the scope stack, we duplicate the current scope and unstack
6842 -- both after the instantiation is complete.
6844 -- If the parent is itself the instantiation of a child unit, we must
6845 -- also stack the instantiation of its parent, and so on. Each such
6846 -- ancestor is the prefix of the name in a prior instantiation.
6848 -- If this is a nested instance, the parent unit itself resolves to
6849 -- a renaming of the parent instance, whose declaration we need.
6851 -- Finally, the parent may be a generic (not an instance) when the
6852 -- child unit appears as a formal package.
6856 if Present
(Renamed_Entity
(Inst_Par
)) then
6857 Inst_Par
:= Renamed_Entity
(Inst_Par
);
6860 First_Par
:= Inst_Par
;
6863 Generic_Parent
(Specification
(Unit_Declaration_Node
(Inst_Par
)));
6865 First_Gen
:= Gen_Par
;
6867 while Present
(Gen_Par
)
6868 and then Is_Child_Unit
(Gen_Par
)
6870 -- Load grandparent instance as well
6872 Inst_Node
:= Get_Package_Instantiation_Node
(Inst_Par
);
6874 if Nkind
(Name
(Inst_Node
)) = N_Expanded_Name
then
6875 Inst_Par
:= Entity
(Prefix
(Name
(Inst_Node
)));
6877 if Present
(Renamed_Entity
(Inst_Par
)) then
6878 Inst_Par
:= Renamed_Entity
(Inst_Par
);
6883 (Specification
(Unit_Declaration_Node
(Inst_Par
)));
6885 if Present
(Gen_Par
) then
6886 Prepend_Elmt
(Inst_Par
, Ancestors
);
6889 -- Parent is not the name of an instantiation
6891 Install_Noninstance_Specs
(Inst_Par
);
6903 if Present
(First_Gen
) then
6904 Append_Elmt
(First_Par
, Ancestors
);
6907 Install_Noninstance_Specs
(First_Par
);
6910 if not Is_Empty_Elmt_List
(Ancestors
) then
6911 Elmt
:= First_Elmt
(Ancestors
);
6913 while Present
(Elmt
) loop
6914 Install_Spec
(Node
(Elmt
));
6915 Install_Formal_Packages
(Node
(Elmt
));
6926 --------------------------------
6927 -- Instantiate_Formal_Package --
6928 --------------------------------
6930 function Instantiate_Formal_Package
6933 Analyzed_Formal
: Node_Id
) return List_Id
6935 Loc
: constant Source_Ptr
:= Sloc
(Actual
);
6936 Actual_Pack
: Entity_Id
;
6937 Formal_Pack
: Entity_Id
;
6938 Gen_Parent
: Entity_Id
;
6941 Parent_Spec
: Node_Id
;
6943 procedure Find_Matching_Actual
6945 Act
: in out Entity_Id
);
6946 -- We need to associate each formal entity in the formal package
6947 -- with the corresponding entity in the actual package. The actual
6948 -- package has been analyzed and possibly expanded, and as a result
6949 -- there is no one-to-one correspondence between the two lists (for
6950 -- example, the actual may include subtypes, itypes, and inherited
6951 -- primitive operations, interspersed among the renaming declarations
6952 -- for the actuals) . We retrieve the corresponding actual by name
6953 -- because each actual has the same name as the formal, and they do
6954 -- appear in the same order.
6956 function Get_Formal_Entity
(N
: Node_Id
) return Entity_Id
;
6957 -- Retrieve entity of defining entity of generic formal parameter.
6958 -- Only the declarations of formals need to be considered when
6959 -- linking them to actuals, but the declarative list may include
6960 -- internal entities generated during analysis, and those are ignored.
6962 procedure Match_Formal_Entity
6963 (Formal_Node
: Node_Id
;
6964 Formal_Ent
: Entity_Id
;
6965 Actual_Ent
: Entity_Id
);
6966 -- Associates the formal entity with the actual. In the case
6967 -- where Formal_Ent is a formal package, this procedure iterates
6968 -- through all of its formals and enters associations betwen the
6969 -- actuals occurring in the formal package's corresponding actual
6970 -- package (given by Actual_Ent) and the formal package's formal
6971 -- parameters. This procedure recurses if any of the parameters is
6972 -- itself a package.
6974 function Is_Instance_Of
6975 (Act_Spec
: Entity_Id
;
6976 Gen_Anc
: Entity_Id
) return Boolean;
6977 -- The actual can be an instantiation of a generic within another
6978 -- instance, in which case there is no direct link from it to the
6979 -- original generic ancestor. In that case, we recognize that the
6980 -- ultimate ancestor is the same by examining names and scopes.
6982 procedure Map_Entities
(Form
: Entity_Id
; Act
: Entity_Id
);
6983 -- Within the generic part, entities in the formal package are
6984 -- visible. To validate subsequent type declarations, indicate
6985 -- the correspondence betwen the entities in the analyzed formal,
6986 -- and the entities in the actual package. There are three packages
6987 -- involved in the instantiation of a formal package: the parent
6988 -- generic P1 which appears in the generic declaration, the fake
6989 -- instantiation P2 which appears in the analyzed generic, and whose
6990 -- visible entities may be used in subsequent formals, and the actual
6991 -- P3 in the instance. To validate subsequent formals, me indicate
6992 -- that the entities in P2 are mapped into those of P3. The mapping of
6993 -- entities has to be done recursively for nested packages.
6995 procedure Process_Nested_Formal
(Formal
: Entity_Id
);
6996 -- If the current formal is declared with a box, its own formals are
6997 -- visible in the instance, as they were in the generic, and their
6998 -- Hidden flag must be reset. If some of these formals are themselves
6999 -- packages declared with a box, the processing must be recursive.
7001 --------------------------
7002 -- Find_Matching_Actual --
7003 --------------------------
7005 procedure Find_Matching_Actual
7007 Act
: in out Entity_Id
)
7009 Formal_Ent
: Entity_Id
;
7012 case Nkind
(Original_Node
(F
)) is
7013 when N_Formal_Object_Declaration |
7014 N_Formal_Type_Declaration
=>
7015 Formal_Ent
:= Defining_Identifier
(F
);
7017 while Chars
(Act
) /= Chars
(Formal_Ent
) loop
7021 when N_Formal_Subprogram_Declaration |
7022 N_Formal_Package_Declaration |
7023 N_Package_Declaration |
7024 N_Generic_Package_Declaration
=>
7025 Formal_Ent
:= Defining_Entity
(F
);
7027 while Chars
(Act
) /= Chars
(Formal_Ent
) loop
7032 raise Program_Error
;
7034 end Find_Matching_Actual
;
7036 -------------------------
7037 -- Match_Formal_Entity --
7038 -------------------------
7040 procedure Match_Formal_Entity
7041 (Formal_Node
: Node_Id
;
7042 Formal_Ent
: Entity_Id
;
7043 Actual_Ent
: Entity_Id
)
7045 Act_Pkg
: Entity_Id
;
7048 Set_Instance_Of
(Formal_Ent
, Actual_Ent
);
7050 if Ekind
(Actual_Ent
) = E_Package
then
7051 -- Record associations for each parameter
7053 Act_Pkg
:= Actual_Ent
;
7056 A_Ent
: Entity_Id
:= First_Entity
(Act_Pkg
);
7065 -- Retrieve the actual given in the formal package declaration
7067 Actual
:= Entity
(Name
(Original_Node
(Formal_Node
)));
7069 -- The actual in the formal package declaration may be a
7070 -- renamed generic package, in which case we want to retrieve
7071 -- the original generic in order to traverse its formal part.
7073 if Present
(Renamed_Entity
(Actual
)) then
7074 Gen_Decl
:= Unit_Declaration_Node
(Renamed_Entity
(Actual
));
7076 Gen_Decl
:= Unit_Declaration_Node
(Actual
);
7079 Formals
:= Generic_Formal_Declarations
(Gen_Decl
);
7081 if Present
(Formals
) then
7082 F_Node
:= First_Non_Pragma
(Formals
);
7087 while Present
(A_Ent
)
7088 and then Present
(F_Node
)
7089 and then A_Ent
/= First_Private_Entity
(Act_Pkg
)
7091 F_Ent
:= Get_Formal_Entity
(F_Node
);
7093 if Present
(F_Ent
) then
7095 -- This is a formal of the original package. Record
7096 -- association and recurse.
7098 Find_Matching_Actual
(F_Node
, A_Ent
);
7099 Match_Formal_Entity
(F_Node
, F_Ent
, A_Ent
);
7100 Next_Entity
(A_Ent
);
7103 Next_Non_Pragma
(F_Node
);
7107 end Match_Formal_Entity
;
7109 -----------------------
7110 -- Get_Formal_Entity --
7111 -----------------------
7113 function Get_Formal_Entity
(N
: Node_Id
) return Entity_Id
is
7114 Kind
: constant Node_Kind
:= Nkind
(Original_Node
(N
));
7117 when N_Formal_Object_Declaration
=>
7118 return Defining_Identifier
(N
);
7120 when N_Formal_Type_Declaration
=>
7121 return Defining_Identifier
(N
);
7123 when N_Formal_Subprogram_Declaration
=>
7124 return Defining_Unit_Name
(Specification
(N
));
7126 when N_Formal_Package_Declaration
=>
7127 return Defining_Identifier
(Original_Node
(N
));
7129 when N_Generic_Package_Declaration
=>
7130 return Defining_Identifier
(Original_Node
(N
));
7132 -- All other declarations are introduced by semantic analysis
7133 -- and have no match in the actual.
7138 end Get_Formal_Entity
;
7140 --------------------
7141 -- Is_Instance_Of --
7142 --------------------
7144 function Is_Instance_Of
7145 (Act_Spec
: Entity_Id
;
7146 Gen_Anc
: Entity_Id
) return Boolean
7148 Gen_Par
: constant Entity_Id
:= Generic_Parent
(Act_Spec
);
7151 if No
(Gen_Par
) then
7154 -- Simplest case: the generic parent of the actual is the formal
7156 elsif Gen_Par
= Gen_Anc
then
7159 elsif Chars
(Gen_Par
) /= Chars
(Gen_Anc
) then
7162 -- The actual may be obtained through several instantiations. Its
7163 -- scope must itself be an instance of a generic declared in the
7164 -- same scope as the formal. Any other case is detected above.
7166 elsif not Is_Generic_Instance
(Scope
(Gen_Par
)) then
7170 return Generic_Parent
(Parent
(Scope
(Gen_Par
))) = Scope
(Gen_Anc
);
7178 procedure Map_Entities
(Form
: Entity_Id
; Act
: Entity_Id
) is
7183 Set_Instance_Of
(Form
, Act
);
7185 -- Traverse formal and actual package to map the corresponding
7186 -- entities. We skip over internal entities that may be generated
7187 -- during semantic analysis, and find the matching entities by
7188 -- name, given that they must appear in the same order.
7190 E1
:= First_Entity
(Form
);
7191 E2
:= First_Entity
(Act
);
7193 and then E1
/= First_Private_Entity
(Form
)
7195 -- Could this test be a single condition???
7196 -- Seems like it could, and isn't FPE (Form) a constant anyway???
7198 if not Is_Internal
(E1
)
7199 and then Present
(Parent
(E1
))
7200 and then not Is_Class_Wide_Type
(E1
)
7201 and then not Is_Internal_Name
(Chars
(E1
))
7204 and then Chars
(E2
) /= Chars
(E1
)
7212 Set_Instance_Of
(E1
, E2
);
7215 and then Is_Tagged_Type
(E2
)
7218 (Class_Wide_Type
(E1
), Class_Wide_Type
(E2
));
7221 if Ekind
(E1
) = E_Package
7222 and then No
(Renamed_Object
(E1
))
7224 Map_Entities
(E1
, E2
);
7233 ---------------------------
7234 -- Process_Nested_Formal --
7235 ---------------------------
7237 procedure Process_Nested_Formal
(Formal
: Entity_Id
) is
7241 if Present
(Associated_Formal_Package
(Formal
))
7242 and then Box_Present
(Parent
(Associated_Formal_Package
(Formal
)))
7244 Ent
:= First_Entity
(Formal
);
7245 while Present
(Ent
) loop
7246 Set_Is_Hidden
(Ent
, False);
7247 Set_Is_Visible_Formal
(Ent
);
7248 Set_Is_Potentially_Use_Visible
7249 (Ent
, Is_Potentially_Use_Visible
(Formal
));
7251 if Ekind
(Ent
) = E_Package
then
7252 exit when Renamed_Entity
(Ent
) = Renamed_Entity
(Formal
);
7253 Process_Nested_Formal
(Ent
);
7259 end Process_Nested_Formal
;
7261 -- Start of processing for Instantiate_Formal_Package
7266 if not Is_Entity_Name
(Actual
)
7267 or else Ekind
(Entity
(Actual
)) /= E_Package
7270 ("expect package instance to instantiate formal", Actual
);
7271 Abandon_Instantiation
(Actual
);
7272 raise Program_Error
;
7275 Actual_Pack
:= Entity
(Actual
);
7276 Set_Is_Instantiated
(Actual_Pack
);
7278 -- The actual may be a renamed package, or an outer generic
7279 -- formal package whose instantiation is converted into a renaming.
7281 if Present
(Renamed_Object
(Actual_Pack
)) then
7282 Actual_Pack
:= Renamed_Object
(Actual_Pack
);
7285 if Nkind
(Analyzed_Formal
) = N_Formal_Package_Declaration
then
7286 Gen_Parent
:= Get_Instance_Of
(Entity
(Name
(Analyzed_Formal
)));
7287 Formal_Pack
:= Defining_Identifier
(Analyzed_Formal
);
7290 Generic_Parent
(Specification
(Analyzed_Formal
));
7292 Defining_Unit_Name
(Specification
(Analyzed_Formal
));
7295 if Nkind
(Parent
(Actual_Pack
)) = N_Defining_Program_Unit_Name
then
7296 Parent_Spec
:= Specification
(Unit_Declaration_Node
(Actual_Pack
));
7298 Parent_Spec
:= Parent
(Actual_Pack
);
7301 if Gen_Parent
= Any_Id
then
7303 ("previous error in declaration of formal package", Actual
);
7304 Abandon_Instantiation
(Actual
);
7307 Is_Instance_Of
(Parent_Spec
, Get_Instance_Of
(Gen_Parent
))
7313 ("actual parameter must be instance of&", Actual
, Gen_Parent
);
7314 Abandon_Instantiation
(Actual
);
7317 Set_Instance_Of
(Defining_Identifier
(Formal
), Actual_Pack
);
7318 Map_Entities
(Formal_Pack
, Actual_Pack
);
7321 Make_Package_Renaming_Declaration
(Loc
,
7322 Defining_Unit_Name
=> New_Copy
(Defining_Identifier
(Formal
)),
7323 Name
=> New_Reference_To
(Actual_Pack
, Loc
));
7325 Set_Associated_Formal_Package
(Defining_Unit_Name
(Nod
),
7326 Defining_Identifier
(Formal
));
7327 Decls
:= New_List
(Nod
);
7329 -- If the formal F has a box, then the generic declarations are
7330 -- visible in the generic G. In an instance of G, the corresponding
7331 -- entities in the actual for F (which are the actuals for the
7332 -- instantiation of the generic that F denotes) must also be made
7333 -- visible for analysis of the current instance. On exit from the
7334 -- current instance, those entities are made private again. If the
7335 -- actual is currently in use, these entities are also use-visible.
7337 -- The loop through the actual entities also steps through the
7338 -- formal entities and enters associations from formals to
7339 -- actuals into the renaming map. This is necessary to properly
7340 -- handle checking of actual parameter associations for later
7341 -- formals that depend on actuals declared in the formal package.
7343 -- In Ada 2005, partial parametrization requires that we make
7344 -- visible the actuals corresponding to formals that were defaulted
7345 -- in the formal package. There formals are identified because they
7346 -- remain formal generics within the formal package, rather than
7347 -- being renamings of the actuals supplied.
7350 Gen_Decl
: constant Node_Id
:=
7351 Unit_Declaration_Node
(Gen_Parent
);
7352 Formals
: constant List_Id
:=
7353 Generic_Formal_Declarations
(Gen_Decl
);
7354 Actual_Ent
: Entity_Id
;
7355 Formal_Node
: Node_Id
;
7356 Formal_Ent
: Entity_Id
;
7359 if Present
(Formals
) then
7360 Formal_Node
:= First_Non_Pragma
(Formals
);
7362 Formal_Node
:= Empty
;
7365 Actual_Ent
:= First_Entity
(Actual_Pack
);
7366 while Present
(Actual_Ent
)
7367 and then Actual_Ent
/= First_Private_Entity
(Actual_Pack
)
7369 if Present
(Formal_Node
) then
7370 Formal_Ent
:= Get_Formal_Entity
(Formal_Node
);
7372 if Present
(Formal_Ent
) then
7373 Find_Matching_Actual
(Formal_Node
, Actual_Ent
);
7375 (Formal_Node
, Formal_Ent
, Actual_Ent
);
7377 if Box_Present
(Formal
)
7379 (Present
(Formal_Node
)
7380 and then Is_Generic_Formal
(Formal_Ent
))
7382 -- This may make too many formal entities visible,
7383 -- but it's hard to build an example that exposes
7384 -- this excess visibility. If a reference in the
7385 -- generic resolved to a global variable then the
7386 -- extra visibility in an instance does not affect
7387 -- the captured entity. If the reference resolved
7388 -- to a local entity it will resolve again in the
7389 -- instance. Nevertheless, we should build tests
7390 -- to make sure that hidden entities in the generic
7391 -- remain hidden in the instance.
7393 Set_Is_Hidden
(Actual_Ent
, False);
7394 Set_Is_Visible_Formal
(Actual_Ent
);
7395 Set_Is_Potentially_Use_Visible
7396 (Actual_Ent
, In_Use
(Actual_Pack
));
7398 if Ekind
(Actual_Ent
) = E_Package
then
7399 Process_Nested_Formal
(Actual_Ent
);
7404 Next_Non_Pragma
(Formal_Node
);
7407 -- No further formals to match, but the generic
7408 -- part may contain inherited operation that are
7409 -- not hidden in the enclosing instance.
7411 Next_Entity
(Actual_Ent
);
7416 -- Inherited subprograms generated by formal derived types are
7417 -- also visible if the types are.
7419 Actual_Ent
:= First_Entity
(Actual_Pack
);
7420 while Present
(Actual_Ent
)
7421 and then Actual_Ent
/= First_Private_Entity
(Actual_Pack
)
7423 if Is_Overloadable
(Actual_Ent
)
7425 Nkind
(Parent
(Actual_Ent
)) = N_Subtype_Declaration
7427 not Is_Hidden
(Defining_Identifier
(Parent
(Actual_Ent
)))
7429 Set_Is_Hidden
(Actual_Ent
, False);
7430 Set_Is_Potentially_Use_Visible
7431 (Actual_Ent
, In_Use
(Actual_Pack
));
7434 Next_Entity
(Actual_Ent
);
7438 -- If the formal is not declared with a box, reanalyze it as
7439 -- an abbreviated instantiation, to verify the matching rules
7440 -- of 12.7. The actual checks are performed after the generic
7441 -- associations have been analyzed, to guarantee the same
7442 -- visibility for this instantiation and for the actuals.
7444 -- In Ada 2005, the generic associations for the formal can include
7445 -- defaulted parameters. These are ignored during check. This
7446 -- internal instantiation is removed from the tree after conformance
7447 -- checking, because it contains formal declarations for those
7448 -- defaulted parameters, and those should not reach the back-end.
7450 if not Box_Present
(Formal
) then
7452 I_Pack
: constant Entity_Id
:=
7453 Make_Defining_Identifier
(Sloc
(Actual
),
7454 Chars
=> New_Internal_Name
('P'));
7457 Set_Is_Internal
(I_Pack
);
7460 Make_Package_Instantiation
(Sloc
(Actual
),
7461 Defining_Unit_Name
=> I_Pack
,
7464 (Get_Instance_Of
(Gen_Parent
), Sloc
(Actual
)),
7465 Generic_Associations
=>
7466 Generic_Associations
(Formal
)));
7472 end Instantiate_Formal_Package
;
7474 -----------------------------------
7475 -- Instantiate_Formal_Subprogram --
7476 -----------------------------------
7478 function Instantiate_Formal_Subprogram
7481 Analyzed_Formal
: Node_Id
) return Node_Id
7484 Formal_Sub
: constant Entity_Id
:=
7485 Defining_Unit_Name
(Specification
(Formal
));
7486 Analyzed_S
: constant Entity_Id
:=
7487 Defining_Unit_Name
(Specification
(Analyzed_Formal
));
7488 Decl_Node
: Node_Id
;
7492 function From_Parent_Scope
(Subp
: Entity_Id
) return Boolean;
7493 -- If the generic is a child unit, the parent has been installed on the
7494 -- scope stack, but a default subprogram cannot resolve to something on
7495 -- the parent because that parent is not really part of the visible
7496 -- context (it is there to resolve explicit local entities). If the
7497 -- default has resolved in this way, we remove the entity from
7498 -- immediate visibility and analyze the node again to emit an error
7499 -- message or find another visible candidate.
7501 procedure Valid_Actual_Subprogram
(Act
: Node_Id
);
7502 -- Perform legality check and raise exception on failure
7504 -----------------------
7505 -- From_Parent_Scope --
7506 -----------------------
7508 function From_Parent_Scope
(Subp
: Entity_Id
) return Boolean is
7509 Gen_Scope
: Node_Id
:= Scope
(Analyzed_S
);
7512 while Present
(Gen_Scope
)
7513 and then Is_Child_Unit
(Gen_Scope
)
7515 if Scope
(Subp
) = Scope
(Gen_Scope
) then
7519 Gen_Scope
:= Scope
(Gen_Scope
);
7523 end From_Parent_Scope
;
7525 -----------------------------
7526 -- Valid_Actual_Subprogram --
7527 -----------------------------
7529 procedure Valid_Actual_Subprogram
(Act
: Node_Id
) is
7530 Act_E
: Entity_Id
:= Empty
;
7533 if Is_Entity_Name
(Act
) then
7534 Act_E
:= Entity
(Act
);
7535 elsif Nkind
(Act
) = N_Selected_Component
7536 and then Is_Entity_Name
(Selector_Name
(Act
))
7538 Act_E
:= Entity
(Selector_Name
(Act
));
7541 if (Present
(Act_E
) and then Is_Overloadable
(Act_E
))
7542 or else Nkind
(Act
) = N_Attribute_Reference
7543 or else Nkind
(Act
) = N_Indexed_Component
7544 or else Nkind
(Act
) = N_Character_Literal
7545 or else Nkind
(Act
) = N_Explicit_Dereference
7551 ("expect subprogram or entry name in instantiation of&",
7552 Instantiation_Node
, Formal_Sub
);
7553 Abandon_Instantiation
(Instantiation_Node
);
7555 end Valid_Actual_Subprogram
;
7557 -- Start of processing for Instantiate_Formal_Subprogram
7560 New_Spec
:= New_Copy_Tree
(Specification
(Formal
));
7562 -- The tree copy has created the proper instantiation sloc for the
7563 -- new specification. Use this location for all other constructed
7566 Loc
:= Sloc
(Defining_Unit_Name
(New_Spec
));
7568 -- Create new entity for the actual (New_Copy_Tree does not)
7570 Set_Defining_Unit_Name
7571 (New_Spec
, Make_Defining_Identifier
(Loc
, Chars
(Formal_Sub
)));
7573 -- Create new entities for the each of the formals in the
7574 -- specification of the renaming declaration built for the actual.
7576 if Present
(Parameter_Specifications
(New_Spec
)) then
7580 F
:= First
(Parameter_Specifications
(New_Spec
));
7581 while Present
(F
) loop
7582 Set_Defining_Identifier
(F
,
7583 Make_Defining_Identifier
(Loc
,
7584 Chars
=> Chars
(Defining_Identifier
(F
))));
7590 -- Find entity of actual. If the actual is an attribute reference, it
7591 -- cannot be resolved here (its formal is missing) but is handled
7592 -- instead in Attribute_Renaming. If the actual is overloaded, it is
7593 -- fully resolved subsequently, when the renaming declaration for the
7594 -- formal is analyzed. If it is an explicit dereference, resolve the
7595 -- prefix but not the actual itself, to prevent interpretation as a
7598 if Present
(Actual
) then
7599 Loc
:= Sloc
(Actual
);
7600 Set_Sloc
(New_Spec
, Loc
);
7602 if Nkind
(Actual
) = N_Operator_Symbol
then
7603 Find_Direct_Name
(Actual
);
7605 elsif Nkind
(Actual
) = N_Explicit_Dereference
then
7606 Analyze
(Prefix
(Actual
));
7608 elsif Nkind
(Actual
) /= N_Attribute_Reference
then
7612 Valid_Actual_Subprogram
(Actual
);
7615 elsif Present
(Default_Name
(Formal
)) then
7616 if Nkind
(Default_Name
(Formal
)) /= N_Attribute_Reference
7617 and then Nkind
(Default_Name
(Formal
)) /= N_Selected_Component
7618 and then Nkind
(Default_Name
(Formal
)) /= N_Indexed_Component
7619 and then Nkind
(Default_Name
(Formal
)) /= N_Character_Literal
7620 and then Present
(Entity
(Default_Name
(Formal
)))
7622 Nam
:= New_Occurrence_Of
(Entity
(Default_Name
(Formal
)), Loc
);
7624 Nam
:= New_Copy
(Default_Name
(Formal
));
7625 Set_Sloc
(Nam
, Loc
);
7628 elsif Box_Present
(Formal
) then
7630 -- Actual is resolved at the point of instantiation. Create
7631 -- an identifier or operator with the same name as the formal.
7633 if Nkind
(Formal_Sub
) = N_Defining_Operator_Symbol
then
7634 Nam
:= Make_Operator_Symbol
(Loc
,
7635 Chars
=> Chars
(Formal_Sub
),
7636 Strval
=> No_String
);
7638 Nam
:= Make_Identifier
(Loc
, Chars
(Formal_Sub
));
7641 elsif Nkind
(Specification
(Formal
)) = N_Procedure_Specification
7642 and then Null_Present
(Specification
(Formal
))
7644 -- Generate null body for procedure, for use in the instance
7647 Make_Subprogram_Body
(Loc
,
7648 Specification
=> New_Spec
,
7649 Declarations
=> New_List
,
7650 Handled_Statement_Sequence
=>
7651 Make_Handled_Sequence_Of_Statements
(Loc
,
7652 Statements
=> New_List
(Make_Null_Statement
(Loc
))));
7654 Set_Is_Intrinsic_Subprogram
(Defining_Unit_Name
(New_Spec
));
7658 Error_Msg_Sloc
:= Sloc
(Scope
(Analyzed_S
));
7660 ("missing actual&", Instantiation_Node
, Formal_Sub
);
7662 ("\in instantiation of & declared#",
7663 Instantiation_Node
, Scope
(Analyzed_S
));
7664 Abandon_Instantiation
(Instantiation_Node
);
7668 Make_Subprogram_Renaming_Declaration
(Loc
,
7669 Specification
=> New_Spec
,
7672 -- If we do not have an actual and the formal specified <> then
7673 -- set to get proper default.
7675 if No
(Actual
) and then Box_Present
(Formal
) then
7676 Set_From_Default
(Decl_Node
);
7679 -- Gather possible interpretations for the actual before analyzing the
7680 -- instance. If overloaded, it will be resolved when analyzing the
7681 -- renaming declaration.
7683 if Box_Present
(Formal
)
7684 and then No
(Actual
)
7688 if Is_Child_Unit
(Scope
(Analyzed_S
))
7689 and then Present
(Entity
(Nam
))
7691 if not Is_Overloaded
(Nam
) then
7693 if From_Parent_Scope
(Entity
(Nam
)) then
7694 Set_Is_Immediately_Visible
(Entity
(Nam
), False);
7695 Set_Entity
(Nam
, Empty
);
7696 Set_Etype
(Nam
, Empty
);
7700 Set_Is_Immediately_Visible
(Entity
(Nam
));
7709 Get_First_Interp
(Nam
, I
, It
);
7711 while Present
(It
.Nam
) loop
7712 if From_Parent_Scope
(It
.Nam
) then
7716 Get_Next_Interp
(I
, It
);
7723 -- The generic instantiation freezes the actual. This can only be
7724 -- done once the actual is resolved, in the analysis of the renaming
7725 -- declaration. To make the formal subprogram entity available, we set
7726 -- Corresponding_Formal_Spec to point to the formal subprogram entity.
7727 -- This is also needed in Analyze_Subprogram_Renaming for the processing
7728 -- of formal abstract subprograms.
7730 Set_Corresponding_Formal_Spec
(Decl_Node
, Analyzed_S
);
7732 -- We cannot analyze the renaming declaration, and thus find the
7733 -- actual, until the all the actuals are assembled in the instance.
7734 -- For subsequent checks of other actuals, indicate the node that
7735 -- will hold the instance of this formal.
7737 Set_Instance_Of
(Analyzed_S
, Nam
);
7739 if Nkind
(Actual
) = N_Selected_Component
7740 and then Is_Task_Type
(Etype
(Prefix
(Actual
)))
7741 and then not Is_Frozen
(Etype
(Prefix
(Actual
)))
7743 -- The renaming declaration will create a body, which must appear
7744 -- outside of the instantiation, We move the renaming declaration
7745 -- out of the instance, and create an additional renaming inside,
7746 -- to prevent freezing anomalies.
7749 Anon_Id
: constant Entity_Id
:=
7750 Make_Defining_Identifier
7751 (Loc
, New_Internal_Name
('E'));
7753 Set_Defining_Unit_Name
(New_Spec
, Anon_Id
);
7754 Insert_Before
(Instantiation_Node
, Decl_Node
);
7755 Analyze
(Decl_Node
);
7757 -- Now create renaming within the instance
7760 Make_Subprogram_Renaming_Declaration
(Loc
,
7761 Specification
=> New_Copy_Tree
(New_Spec
),
7762 Name
=> New_Occurrence_Of
(Anon_Id
, Loc
));
7764 Set_Defining_Unit_Name
(Specification
(Decl_Node
),
7765 Make_Defining_Identifier
(Loc
, Chars
(Formal_Sub
)));
7770 end Instantiate_Formal_Subprogram
;
7772 ------------------------
7773 -- Instantiate_Object --
7774 ------------------------
7776 function Instantiate_Object
7779 Analyzed_Formal
: Node_Id
) return List_Id
7781 Acc_Def
: Node_Id
:= Empty
;
7782 Act_Assoc
: constant Node_Id
:= Parent
(Actual
);
7783 Actual_Decl
: Node_Id
:= Empty
;
7784 Formal_Id
: constant Entity_Id
:= Defining_Identifier
(Formal
);
7785 Decl_Node
: Node_Id
;
7788 List
: constant List_Id
:= New_List
;
7789 Loc
: constant Source_Ptr
:= Sloc
(Actual
);
7790 Orig_Ftyp
: constant Entity_Id
:=
7791 Etype
(Defining_Identifier
(Analyzed_Formal
));
7792 Subt_Decl
: Node_Id
:= Empty
;
7793 Subt_Mark
: Node_Id
:= Empty
;
7796 if Present
(Subtype_Mark
(Formal
)) then
7797 Subt_Mark
:= Subtype_Mark
(Formal
);
7799 Check_Access_Definition
(Formal
);
7800 Acc_Def
:= Access_Definition
(Formal
);
7803 -- Sloc for error message on missing actual
7805 Error_Msg_Sloc
:= Sloc
(Scope
(Defining_Identifier
(Analyzed_Formal
)));
7807 if Get_Instance_Of
(Formal_Id
) /= Formal_Id
then
7808 Error_Msg_N
("duplicate instantiation of generic parameter", Actual
);
7811 Set_Parent
(List
, Parent
(Actual
));
7815 if Out_Present
(Formal
) then
7817 -- An IN OUT generic actual must be a name. The instantiation is a
7818 -- renaming declaration. The actual is the name being renamed. We
7819 -- use the actual directly, rather than a copy, because it is not
7820 -- used further in the list of actuals, and because a copy or a use
7821 -- of relocate_node is incorrect if the instance is nested within a
7822 -- generic. In order to simplify ASIS searches, the Generic_Parent
7823 -- field links the declaration to the generic association.
7828 Instantiation_Node
, Formal_Id
);
7830 ("\in instantiation of & declared#",
7832 Scope
(Defining_Identifier
(Analyzed_Formal
)));
7833 Abandon_Instantiation
(Instantiation_Node
);
7836 if Present
(Subt_Mark
) then
7838 Make_Object_Renaming_Declaration
(Loc
,
7839 Defining_Identifier
=> New_Copy
(Formal_Id
),
7840 Subtype_Mark
=> New_Copy_Tree
(Subt_Mark
),
7843 else pragma Assert
(Present
(Acc_Def
));
7845 Make_Object_Renaming_Declaration
(Loc
,
7846 Defining_Identifier
=> New_Copy
(Formal_Id
),
7847 Access_Definition
=> New_Copy_Tree
(Acc_Def
),
7851 Set_Corresponding_Generic_Association
(Decl_Node
, Act_Assoc
);
7853 -- The analysis of the actual may produce insert_action nodes, so
7854 -- the declaration must have a context in which to attach them.
7856 Append
(Decl_Node
, List
);
7859 -- Return if the analysis of the actual reported some error
7861 if Etype
(Actual
) = Any_Type
then
7865 -- This check is performed here because Analyze_Object_Renaming
7866 -- will not check it when Comes_From_Source is False. Note
7867 -- though that the check for the actual being the name of an
7868 -- object will be performed in Analyze_Object_Renaming.
7870 if Is_Object_Reference
(Actual
)
7871 and then Is_Dependent_Component_Of_Mutable_Object
(Actual
)
7874 ("illegal discriminant-dependent component for in out parameter",
7878 -- The actual has to be resolved in order to check that it is
7879 -- a variable (due to cases such as F(1), where F returns
7880 -- access to an array, and for overloaded prefixes).
7883 Get_Instance_Of
(Etype
(Defining_Identifier
(Analyzed_Formal
)));
7885 if Is_Private_Type
(Ftyp
)
7886 and then not Is_Private_Type
(Etype
(Actual
))
7887 and then (Base_Type
(Full_View
(Ftyp
)) = Base_Type
(Etype
(Actual
))
7888 or else Base_Type
(Etype
(Actual
)) = Ftyp
)
7890 -- If the actual has the type of the full view of the formal,
7891 -- or else a non-private subtype of the formal, then
7892 -- the visibility of the formal type has changed. Add to the
7893 -- actuals a subtype declaration that will force the exchange
7894 -- of views in the body of the instance as well.
7897 Make_Subtype_Declaration
(Loc
,
7898 Defining_Identifier
=>
7899 Make_Defining_Identifier
(Loc
, New_Internal_Name
('P')),
7900 Subtype_Indication
=> New_Occurrence_Of
(Ftyp
, Loc
));
7902 Prepend
(Subt_Decl
, List
);
7904 Prepend_Elmt
(Full_View
(Ftyp
), Exchanged_Views
);
7905 Exchange_Declarations
(Ftyp
);
7908 Resolve
(Actual
, Ftyp
);
7910 if not Is_Variable
(Actual
) or else Paren_Count
(Actual
) > 0 then
7912 ("actual for& must be a variable", Actual
, Formal_Id
);
7914 elsif Base_Type
(Ftyp
) /= Base_Type
(Etype
(Actual
)) then
7916 -- Ada 2005 (AI-423): For a generic formal object of mode in
7917 -- out, the type of the actual shall resolve to a specific
7918 -- anonymous access type.
7920 if Ada_Version
< Ada_05
7922 Ekind
(Base_Type
(Ftyp
)) /=
7923 E_Anonymous_Access_Type
7925 Ekind
(Base_Type
(Etype
(Actual
))) /=
7926 E_Anonymous_Access_Type
7928 Error_Msg_NE
("type of actual does not match type of&",
7933 Note_Possible_Modification
(Actual
);
7935 -- Check for instantiation of atomic/volatile actual for
7936 -- non-atomic/volatile formal (RM C.6 (12)).
7938 if Is_Atomic_Object
(Actual
)
7939 and then not Is_Atomic
(Orig_Ftyp
)
7942 ("cannot instantiate non-atomic formal object " &
7943 "with atomic actual", Actual
);
7945 elsif Is_Volatile_Object
(Actual
)
7946 and then not Is_Volatile
(Orig_Ftyp
)
7949 ("cannot instantiate non-volatile formal object " &
7950 "with volatile actual", Actual
);
7956 -- The instantiation of a generic formal in-parameter is a
7957 -- constant declaration. The actual is the expression for
7958 -- that declaration.
7960 if Present
(Actual
) then
7961 if Present
(Subt_Mark
) then
7963 else pragma Assert
(Present
(Acc_Def
));
7968 Make_Object_Declaration
(Loc
,
7969 Defining_Identifier
=> New_Copy
(Formal_Id
),
7970 Constant_Present
=> True,
7971 Object_Definition
=> New_Copy_Tree
(Def
),
7972 Expression
=> Actual
);
7974 Set_Corresponding_Generic_Association
(Decl_Node
, Act_Assoc
);
7976 -- A generic formal object of a tagged type is defined
7977 -- to be aliased so the new constant must also be treated
7981 (Etype
(Defining_Identifier
(Analyzed_Formal
)))
7983 Set_Aliased_Present
(Decl_Node
);
7986 Append
(Decl_Node
, List
);
7988 -- No need to repeat (pre-)analysis of some expression nodes
7989 -- already handled in Pre_Analyze_Actuals.
7991 if Nkind
(Actual
) /= N_Allocator
then
7994 -- Return if the analysis of the actual reported some error
7996 if Etype
(Actual
) = Any_Type
then
8002 Typ
: constant Entity_Id
:=
8004 (Etype
(Defining_Identifier
(Analyzed_Formal
)));
8007 Freeze_Before
(Instantiation_Node
, Typ
);
8009 -- If the actual is an aggregate, perform name resolution on
8010 -- its components (the analysis of an aggregate does not do
8011 -- it) to capture local names that may be hidden if the
8012 -- generic is a child unit.
8014 if Nkind
(Actual
) = N_Aggregate
then
8015 Pre_Analyze_And_Resolve
(Actual
, Typ
);
8019 elsif Present
(Default_Expression
(Formal
)) then
8021 -- Use default to construct declaration
8023 if Present
(Subt_Mark
) then
8025 else pragma Assert
(Present
(Acc_Def
));
8030 Make_Object_Declaration
(Sloc
(Formal
),
8031 Defining_Identifier
=> New_Copy
(Formal_Id
),
8032 Constant_Present
=> True,
8033 Object_Definition
=> New_Copy
(Def
),
8034 Expression
=> New_Copy_Tree
(Default_Expression
8037 Append
(Decl_Node
, List
);
8038 Set_Analyzed
(Expression
(Decl_Node
), False);
8043 Instantiation_Node
, Formal_Id
);
8044 Error_Msg_NE
("\in instantiation of & declared#",
8046 Scope
(Defining_Identifier
(Analyzed_Formal
)));
8049 (Etype
(Defining_Identifier
(Analyzed_Formal
)))
8051 -- Create dummy constant declaration so that instance can
8052 -- be analyzed, to minimize cascaded visibility errors.
8054 if Present
(Subt_Mark
) then
8056 else pragma Assert
(Present
(Acc_Def
));
8061 Make_Object_Declaration
(Loc
,
8062 Defining_Identifier
=> New_Copy
(Formal_Id
),
8063 Constant_Present
=> True,
8064 Object_Definition
=> New_Copy
(Def
),
8066 Make_Attribute_Reference
(Sloc
(Formal_Id
),
8067 Attribute_Name
=> Name_First
,
8068 Prefix
=> New_Copy
(Def
)));
8070 Append
(Decl_Node
, List
);
8073 Abandon_Instantiation
(Instantiation_Node
);
8078 if Nkind
(Actual
) in N_Has_Entity
then
8079 Actual_Decl
:= Parent
(Entity
(Actual
));
8082 -- Ada 2005 (AI-423): For a formal object declaration with a null
8083 -- exclusion or an access definition that has a null exclusion: If
8084 -- the actual matching the formal object declaration denotes a generic
8085 -- formal object of another generic unit G, and the instantiation
8086 -- containing the actual occurs within the body of G or within the
8087 -- body of a generic unit declared within the declarative region of G,
8088 -- then the declaration of the formal object of G shall have a null
8089 -- exclusion. Otherwise, the subtype of the actual matching the formal
8090 -- object declaration shall exclude null.
8092 if Ada_Version
>= Ada_05
8093 and then Present
(Actual_Decl
)
8095 (Nkind
(Actual_Decl
) = N_Formal_Object_Declaration
8096 or else Nkind
(Actual_Decl
) = N_Object_Declaration
)
8097 and then Nkind
(Analyzed_Formal
) = N_Formal_Object_Declaration
8098 and then Has_Null_Exclusion
(Actual_Decl
)
8099 and then not Has_Null_Exclusion
(Analyzed_Formal
)
8101 Error_Msg_N
("null-exclusion required in formal object declaration",
8106 end Instantiate_Object
;
8108 ------------------------------
8109 -- Instantiate_Package_Body --
8110 ------------------------------
8112 procedure Instantiate_Package_Body
8113 (Body_Info
: Pending_Body_Info
;
8114 Inlined_Body
: Boolean := False)
8116 Act_Decl
: constant Node_Id
:= Body_Info
.Act_Decl
;
8117 Inst_Node
: constant Node_Id
:= Body_Info
.Inst_Node
;
8118 Loc
: constant Source_Ptr
:= Sloc
(Inst_Node
);
8120 Gen_Id
: constant Node_Id
:= Name
(Inst_Node
);
8121 Gen_Unit
: constant Entity_Id
:= Get_Generic_Entity
(Inst_Node
);
8122 Gen_Decl
: constant Node_Id
:= Unit_Declaration_Node
(Gen_Unit
);
8123 Act_Spec
: constant Node_Id
:= Specification
(Act_Decl
);
8124 Act_Decl_Id
: constant Entity_Id
:= Defining_Entity
(Act_Spec
);
8126 Act_Body_Name
: Node_Id
;
8128 Gen_Body_Id
: Node_Id
;
8130 Act_Body_Id
: Entity_Id
;
8132 Parent_Installed
: Boolean := False;
8133 Save_Style_Check
: constant Boolean := Style_Check
;
8136 Gen_Body_Id
:= Corresponding_Body
(Gen_Decl
);
8138 -- The instance body may already have been processed, as the parent
8139 -- of another instance that is inlined. (Load_Parent_Of_Generic).
8141 if Present
(Corresponding_Body
(Instance_Spec
(Inst_Node
))) then
8145 Expander_Mode_Save_And_Set
(Body_Info
.Expander_Status
);
8147 if No
(Gen_Body_Id
) then
8148 Load_Parent_Of_Generic
(Inst_Node
, Specification
(Gen_Decl
));
8149 Gen_Body_Id
:= Corresponding_Body
(Gen_Decl
);
8152 -- Establish global variable for sloc adjustment and for error
8155 Instantiation_Node
:= Inst_Node
;
8157 if Present
(Gen_Body_Id
) then
8158 Save_Env
(Gen_Unit
, Act_Decl_Id
);
8159 Style_Check
:= False;
8160 Current_Sem_Unit
:= Body_Info
.Current_Sem_Unit
;
8162 Gen_Body
:= Unit_Declaration_Node
(Gen_Body_Id
);
8164 Create_Instantiation_Source
8165 (Inst_Node
, Gen_Body_Id
, False, S_Adjustment
);
8169 (Original_Node
(Gen_Body
), Empty
, Instantiating
=> True);
8171 -- Build new name (possibly qualified) for body declaration
8173 Act_Body_Id
:= New_Copy
(Act_Decl_Id
);
8175 -- Some attributes of the spec entity are not inherited by the
8178 Set_Handler_Records
(Act_Body_Id
, No_List
);
8180 if Nkind
(Defining_Unit_Name
(Act_Spec
)) =
8181 N_Defining_Program_Unit_Name
8184 Make_Defining_Program_Unit_Name
(Loc
,
8185 Name
=> New_Copy_Tree
(Name
(Defining_Unit_Name
(Act_Spec
))),
8186 Defining_Identifier
=> Act_Body_Id
);
8188 Act_Body_Name
:= Act_Body_Id
;
8191 Set_Defining_Unit_Name
(Act_Body
, Act_Body_Name
);
8193 Set_Corresponding_Spec
(Act_Body
, Act_Decl_Id
);
8194 Check_Generic_Actuals
(Act_Decl_Id
, False);
8196 -- If it is a child unit, make the parent instance (which is an
8197 -- instance of the parent of the generic) visible. The parent
8198 -- instance is the prefix of the name of the generic unit.
8200 if Ekind
(Scope
(Gen_Unit
)) = E_Generic_Package
8201 and then Nkind
(Gen_Id
) = N_Expanded_Name
8203 Install_Parent
(Entity
(Prefix
(Gen_Id
)), In_Body
=> True);
8204 Parent_Installed
:= True;
8206 elsif Is_Child_Unit
(Gen_Unit
) then
8207 Install_Parent
(Scope
(Gen_Unit
), In_Body
=> True);
8208 Parent_Installed
:= True;
8211 -- If the instantiation is a library unit, and this is the main
8212 -- unit, then build the resulting compilation unit nodes for the
8213 -- instance. If this is a compilation unit but it is not the main
8214 -- unit, then it is the body of a unit in the context, that is being
8215 -- compiled because it is encloses some inlined unit or another
8216 -- generic unit being instantiated. In that case, this body is not
8217 -- part of the current compilation, and is not attached to the tree,
8218 -- but its parent must be set for analysis.
8220 if Nkind
(Parent
(Inst_Node
)) = N_Compilation_Unit
then
8222 -- Replace instance node with body of instance, and create
8223 -- new node for corresponding instance declaration.
8225 Build_Instance_Compilation_Unit_Nodes
8226 (Inst_Node
, Act_Body
, Act_Decl
);
8227 Analyze
(Inst_Node
);
8229 if Parent
(Inst_Node
) = Cunit
(Main_Unit
) then
8231 -- If the instance is a child unit itself, then set the
8232 -- scope of the expanded body to be the parent of the
8233 -- instantiation (ensuring that the fully qualified name
8234 -- will be generated for the elaboration subprogram).
8236 if Nkind
(Defining_Unit_Name
(Act_Spec
)) =
8237 N_Defining_Program_Unit_Name
8240 (Defining_Entity
(Inst_Node
), Scope
(Act_Decl_Id
));
8244 -- Case where instantiation is not a library unit
8247 -- If this is an early instantiation, i.e. appears textually
8248 -- before the corresponding body and must be elaborated first,
8249 -- indicate that the body instance is to be delayed.
8251 Install_Body
(Act_Body
, Inst_Node
, Gen_Body
, Gen_Decl
);
8253 -- Now analyze the body. We turn off all checks if this is
8254 -- an internal unit, since there is no reason to have checks
8255 -- on for any predefined run-time library code. All such
8256 -- code is designed to be compiled with checks off.
8258 -- Note that we do NOT apply this criterion to children of
8259 -- GNAT (or on VMS, children of DEC). The latter units must
8260 -- suppress checks explicitly if this is needed.
8262 if Is_Predefined_File_Name
8263 (Unit_File_Name
(Get_Source_Unit
(Gen_Decl
)))
8265 Analyze
(Act_Body
, Suppress
=> All_Checks
);
8271 if not Generic_Separately_Compiled
(Gen_Unit
) then
8272 Inherit_Context
(Gen_Body
, Inst_Node
);
8275 -- Remove the parent instances if they have been placed on the
8276 -- scope stack to compile the body.
8278 if Parent_Installed
then
8279 Remove_Parent
(In_Body
=> True);
8282 Restore_Private_Views
(Act_Decl_Id
);
8284 -- Remove the current unit from visibility if this is an instance
8285 -- that is not elaborated on the fly for inlining purposes.
8287 if not Inlined_Body
then
8288 Set_Is_Immediately_Visible
(Act_Decl_Id
, False);
8292 Style_Check
:= Save_Style_Check
;
8294 -- If we have no body, and the unit requires a body, then complain.
8295 -- This complaint is suppressed if we have detected other errors
8296 -- (since a common reason for missing the body is that it had errors).
8298 elsif Unit_Requires_Body
(Gen_Unit
) then
8299 if Serious_Errors_Detected
= 0 then
8301 ("cannot find body of generic package &", Inst_Node
, Gen_Unit
);
8303 -- Don't attempt to perform any cleanup actions if some other
8304 -- error was aready detected, since this can cause blowups.
8310 -- Case of package that does not need a body
8313 -- If the instantiation of the declaration is a library unit,
8314 -- rewrite the original package instantiation as a package
8315 -- declaration in the compilation unit node.
8317 if Nkind
(Parent
(Inst_Node
)) = N_Compilation_Unit
then
8318 Set_Parent_Spec
(Act_Decl
, Parent_Spec
(Inst_Node
));
8319 Rewrite
(Inst_Node
, Act_Decl
);
8321 -- Generate elaboration entity, in case spec has elaboration
8322 -- code. This cannot be done when the instance is analyzed,
8323 -- because it is not known yet whether the body exists.
8325 Set_Elaboration_Entity_Required
(Act_Decl_Id
, False);
8326 Build_Elaboration_Entity
(Parent
(Inst_Node
), Act_Decl_Id
);
8328 -- If the instantiation is not a library unit, then append the
8329 -- declaration to the list of implicitly generated entities.
8330 -- unless it is already a list member which means that it was
8331 -- already processed
8333 elsif not Is_List_Member
(Act_Decl
) then
8334 Mark_Rewrite_Insertion
(Act_Decl
);
8335 Insert_Before
(Inst_Node
, Act_Decl
);
8339 Expander_Mode_Restore
;
8340 end Instantiate_Package_Body
;
8342 ---------------------------------
8343 -- Instantiate_Subprogram_Body --
8344 ---------------------------------
8346 procedure Instantiate_Subprogram_Body
8347 (Body_Info
: Pending_Body_Info
)
8349 Act_Decl
: constant Node_Id
:= Body_Info
.Act_Decl
;
8350 Inst_Node
: constant Node_Id
:= Body_Info
.Inst_Node
;
8351 Loc
: constant Source_Ptr
:= Sloc
(Inst_Node
);
8352 Gen_Id
: constant Node_Id
:= Name
(Inst_Node
);
8353 Gen_Unit
: constant Entity_Id
:= Get_Generic_Entity
(Inst_Node
);
8354 Gen_Decl
: constant Node_Id
:= Unit_Declaration_Node
(Gen_Unit
);
8355 Anon_Id
: constant Entity_Id
:=
8356 Defining_Unit_Name
(Specification
(Act_Decl
));
8357 Pack_Id
: constant Entity_Id
:=
8358 Defining_Unit_Name
(Parent
(Act_Decl
));
8361 Gen_Body_Id
: Node_Id
;
8363 Act_Body_Id
: Entity_Id
;
8364 Pack_Body
: Node_Id
;
8365 Prev_Formal
: Entity_Id
;
8367 Unit_Renaming
: Node_Id
;
8369 Parent_Installed
: Boolean := False;
8370 Save_Style_Check
: constant Boolean := Style_Check
;
8373 Gen_Body_Id
:= Corresponding_Body
(Gen_Decl
);
8375 Expander_Mode_Save_And_Set
(Body_Info
.Expander_Status
);
8377 if No
(Gen_Body_Id
) then
8378 Load_Parent_Of_Generic
(Inst_Node
, Specification
(Gen_Decl
));
8379 Gen_Body_Id
:= Corresponding_Body
(Gen_Decl
);
8382 Instantiation_Node
:= Inst_Node
;
8384 if Present
(Gen_Body_Id
) then
8385 Gen_Body
:= Unit_Declaration_Node
(Gen_Body_Id
);
8387 if Nkind
(Gen_Body
) = N_Subprogram_Body_Stub
then
8389 -- Either body is not present, or context is non-expanding, as
8390 -- when compiling a subunit. Mark the instance as completed, and
8391 -- diagnose a missing body when needed.
8394 and then Operating_Mode
= Generate_Code
8397 ("missing proper body for instantiation", Gen_Body
);
8400 Set_Has_Completion
(Anon_Id
);
8404 Save_Env
(Gen_Unit
, Anon_Id
);
8405 Style_Check
:= False;
8406 Current_Sem_Unit
:= Body_Info
.Current_Sem_Unit
;
8407 Create_Instantiation_Source
8415 (Original_Node
(Gen_Body
), Empty
, Instantiating
=> True);
8416 Act_Body_Id
:= Defining_Entity
(Act_Body
);
8417 Set_Chars
(Act_Body_Id
, Chars
(Anon_Id
));
8418 Set_Sloc
(Act_Body_Id
, Sloc
(Defining_Entity
(Inst_Node
)));
8419 Set_Corresponding_Spec
(Act_Body
, Anon_Id
);
8420 Set_Has_Completion
(Anon_Id
);
8421 Check_Generic_Actuals
(Pack_Id
, False);
8423 -- Generate a reference to link the visible subprogram instance to
8424 -- the the generic body, which for navigation purposes is the only
8425 -- available source for the instance.
8428 (Related_Instance
(Pack_Id
),
8429 Gen_Body_Id
, 'b', Set_Ref
=> False, Force
=> True);
8431 -- If it is a child unit, make the parent instance (which is an
8432 -- instance of the parent of the generic) visible. The parent
8433 -- instance is the prefix of the name of the generic unit.
8435 if Ekind
(Scope
(Gen_Unit
)) = E_Generic_Package
8436 and then Nkind
(Gen_Id
) = N_Expanded_Name
8438 Install_Parent
(Entity
(Prefix
(Gen_Id
)), In_Body
=> True);
8439 Parent_Installed
:= True;
8441 elsif Is_Child_Unit
(Gen_Unit
) then
8442 Install_Parent
(Scope
(Gen_Unit
), In_Body
=> True);
8443 Parent_Installed
:= True;
8446 -- Inside its body, a reference to the generic unit is a reference
8447 -- to the instance. The corresponding renaming is the first
8448 -- declaration in the body.
8451 Make_Subprogram_Renaming_Declaration
(Loc
,
8454 Specification
(Original_Node
(Gen_Body
)),
8456 Instantiating
=> True),
8457 Name
=> New_Occurrence_Of
(Anon_Id
, Loc
));
8459 -- If there is a formal subprogram with the same name as the
8460 -- unit itself, do not add this renaming declaration. This is
8461 -- a temporary fix for one ACVC test. ???
8463 Prev_Formal
:= First_Entity
(Pack_Id
);
8464 while Present
(Prev_Formal
) loop
8465 if Chars
(Prev_Formal
) = Chars
(Gen_Unit
)
8466 and then Is_Overloadable
(Prev_Formal
)
8471 Next_Entity
(Prev_Formal
);
8474 if Present
(Prev_Formal
) then
8475 Decls
:= New_List
(Act_Body
);
8477 Decls
:= New_List
(Unit_Renaming
, Act_Body
);
8480 -- The subprogram body is placed in the body of a dummy package
8481 -- body, whose spec contains the subprogram declaration as well
8482 -- as the renaming declarations for the generic parameters.
8484 Pack_Body
:= Make_Package_Body
(Loc
,
8485 Defining_Unit_Name
=> New_Copy
(Pack_Id
),
8486 Declarations
=> Decls
);
8488 Set_Corresponding_Spec
(Pack_Body
, Pack_Id
);
8490 -- If the instantiation is a library unit, then build resulting
8491 -- compilation unit nodes for the instance. The declaration of
8492 -- the enclosing package is the grandparent of the subprogram
8493 -- declaration. First replace the instantiation node as the unit
8494 -- of the corresponding compilation.
8496 if Nkind
(Parent
(Inst_Node
)) = N_Compilation_Unit
then
8497 if Parent
(Inst_Node
) = Cunit
(Main_Unit
) then
8498 Set_Unit
(Parent
(Inst_Node
), Inst_Node
);
8499 Build_Instance_Compilation_Unit_Nodes
8500 (Inst_Node
, Pack_Body
, Parent
(Parent
(Act_Decl
)));
8501 Analyze
(Inst_Node
);
8503 Set_Parent
(Pack_Body
, Parent
(Inst_Node
));
8504 Analyze
(Pack_Body
);
8508 Insert_Before
(Inst_Node
, Pack_Body
);
8509 Mark_Rewrite_Insertion
(Pack_Body
);
8510 Analyze
(Pack_Body
);
8512 if Expander_Active
then
8513 Freeze_Subprogram_Body
(Inst_Node
, Gen_Body
, Pack_Id
);
8517 if not Generic_Separately_Compiled
(Gen_Unit
) then
8518 Inherit_Context
(Gen_Body
, Inst_Node
);
8521 Restore_Private_Views
(Pack_Id
, False);
8523 if Parent_Installed
then
8524 Remove_Parent
(In_Body
=> True);
8528 Style_Check
:= Save_Style_Check
;
8530 -- Body not found. Error was emitted already. If there were no
8531 -- previous errors, this may be an instance whose scope is a premature
8532 -- instance. In that case we must insure that the (legal) program does
8533 -- raise program error if executed. We generate a subprogram body for
8534 -- this purpose. See DEC ac30vso.
8536 elsif Serious_Errors_Detected
= 0
8537 and then Nkind
(Parent
(Inst_Node
)) /= N_Compilation_Unit
8539 if Ekind
(Anon_Id
) = E_Procedure
then
8541 Make_Subprogram_Body
(Loc
,
8543 Make_Procedure_Specification
(Loc
,
8544 Defining_Unit_Name
=>
8545 Make_Defining_Identifier
(Loc
, Chars
(Anon_Id
)),
8546 Parameter_Specifications
=>
8548 (Parameter_Specifications
(Parent
(Anon_Id
)))),
8550 Declarations
=> Empty_List
,
8551 Handled_Statement_Sequence
=>
8552 Make_Handled_Sequence_Of_Statements
(Loc
,
8555 Make_Raise_Program_Error
(Loc
,
8557 PE_Access_Before_Elaboration
))));
8561 Make_Raise_Program_Error
(Loc
,
8562 Reason
=> PE_Access_Before_Elaboration
);
8564 Set_Etype
(Ret_Expr
, (Etype
(Anon_Id
)));
8565 Set_Analyzed
(Ret_Expr
);
8568 Make_Subprogram_Body
(Loc
,
8570 Make_Function_Specification
(Loc
,
8571 Defining_Unit_Name
=>
8572 Make_Defining_Identifier
(Loc
, Chars
(Anon_Id
)),
8573 Parameter_Specifications
=>
8575 (Parameter_Specifications
(Parent
(Anon_Id
))),
8576 Result_Definition
=>
8577 New_Occurrence_Of
(Etype
(Anon_Id
), Loc
)),
8579 Declarations
=> Empty_List
,
8580 Handled_Statement_Sequence
=>
8581 Make_Handled_Sequence_Of_Statements
(Loc
,
8583 New_List
(Make_Return_Statement
(Loc
, Ret_Expr
))));
8586 Pack_Body
:= Make_Package_Body
(Loc
,
8587 Defining_Unit_Name
=> New_Copy
(Pack_Id
),
8588 Declarations
=> New_List
(Act_Body
));
8590 Insert_After
(Inst_Node
, Pack_Body
);
8591 Set_Corresponding_Spec
(Pack_Body
, Pack_Id
);
8592 Analyze
(Pack_Body
);
8595 Expander_Mode_Restore
;
8596 end Instantiate_Subprogram_Body
;
8598 ----------------------
8599 -- Instantiate_Type --
8600 ----------------------
8602 function Instantiate_Type
8605 Analyzed_Formal
: Node_Id
;
8606 Actual_Decls
: List_Id
) return Node_Id
8608 Gen_T
: constant Entity_Id
:= Defining_Identifier
(Formal
);
8609 A_Gen_T
: constant Entity_Id
:= Defining_Identifier
(Analyzed_Formal
);
8610 Ancestor
: Entity_Id
:= Empty
;
8611 Def
: constant Node_Id
:= Formal_Type_Definition
(Formal
);
8613 Decl_Node
: Node_Id
;
8617 procedure Validate_Array_Type_Instance
;
8618 procedure Validate_Access_Subprogram_Instance
;
8619 procedure Validate_Access_Type_Instance
;
8620 procedure Validate_Derived_Type_Instance
;
8621 procedure Validate_Derived_Interface_Type_Instance
;
8622 procedure Validate_Interface_Type_Instance
;
8623 procedure Validate_Private_Type_Instance
;
8624 -- These procedures perform validation tests for the named case
8626 function Subtypes_Match
(Gen_T
, Act_T
: Entity_Id
) return Boolean;
8627 -- Check that base types are the same and that the subtypes match
8628 -- statically. Used in several of the above.
8630 --------------------
8631 -- Subtypes_Match --
8632 --------------------
8634 function Subtypes_Match
(Gen_T
, Act_T
: Entity_Id
) return Boolean is
8635 T
: constant Entity_Id
:= Get_Instance_Of
(Gen_T
);
8638 return (Base_Type
(T
) = Base_Type
(Act_T
)
8639 and then Subtypes_Statically_Match
(T
, Act_T
))
8641 or else (Is_Class_Wide_Type
(Gen_T
)
8642 and then Is_Class_Wide_Type
(Act_T
)
8645 (Get_Instance_Of
(Root_Type
(Gen_T
)),
8649 ((Ekind
(Gen_T
) = E_Anonymous_Access_Subprogram_Type
8650 or else Ekind
(Gen_T
) = E_Anonymous_Access_Type
)
8651 and then Ekind
(Act_T
) = Ekind
(Gen_T
)
8653 Subtypes_Statically_Match
8654 (Designated_Type
(Gen_T
), Designated_Type
(Act_T
)));
8657 -----------------------------------------
8658 -- Validate_Access_Subprogram_Instance --
8659 -----------------------------------------
8661 procedure Validate_Access_Subprogram_Instance
is
8663 if not Is_Access_Type
(Act_T
)
8664 or else Ekind
(Designated_Type
(Act_T
)) /= E_Subprogram_Type
8667 ("expect access type in instantiation of &", Actual
, Gen_T
);
8668 Abandon_Instantiation
(Actual
);
8671 Check_Mode_Conformant
8672 (Designated_Type
(Act_T
),
8673 Designated_Type
(A_Gen_T
),
8677 if Ekind
(Base_Type
(Act_T
)) = E_Access_Protected_Subprogram_Type
then
8678 if Ekind
(A_Gen_T
) = E_Access_Subprogram_Type
then
8680 ("protected access type not allowed for formal &",
8684 elsif Ekind
(A_Gen_T
) = E_Access_Protected_Subprogram_Type
then
8686 ("expect protected access type for formal &",
8689 end Validate_Access_Subprogram_Instance
;
8691 -----------------------------------
8692 -- Validate_Access_Type_Instance --
8693 -----------------------------------
8695 procedure Validate_Access_Type_Instance
is
8696 Desig_Type
: constant Entity_Id
:=
8698 (Designated_Type
(A_Gen_T
), Scope
(A_Gen_T
));
8701 if not Is_Access_Type
(Act_T
) then
8703 ("expect access type in instantiation of &", Actual
, Gen_T
);
8704 Abandon_Instantiation
(Actual
);
8707 if Is_Access_Constant
(A_Gen_T
) then
8708 if not Is_Access_Constant
(Act_T
) then
8710 ("actual type must be access-to-constant type", Actual
);
8711 Abandon_Instantiation
(Actual
);
8714 if Is_Access_Constant
(Act_T
) then
8716 ("actual type must be access-to-variable type", Actual
);
8717 Abandon_Instantiation
(Actual
);
8719 elsif Ekind
(A_Gen_T
) = E_General_Access_Type
8720 and then Ekind
(Base_Type
(Act_T
)) /= E_General_Access_Type
8722 Error_Msg_N
("actual must be general access type!", Actual
);
8723 Error_Msg_NE
("add ALL to }!", Actual
, Act_T
);
8724 Abandon_Instantiation
(Actual
);
8728 -- The designated subtypes, that is to say the subtypes introduced
8729 -- by an access type declaration (and not by a subtype declaration)
8732 if not Subtypes_Match
8733 (Desig_Type
, Designated_Type
(Base_Type
(Act_T
)))
8736 ("designated type of actual does not match that of formal &",
8738 Abandon_Instantiation
(Actual
);
8740 elsif Is_Access_Type
(Designated_Type
(Act_T
))
8741 and then Is_Constrained
(Designated_Type
(Designated_Type
(Act_T
)))
8743 Is_Constrained
(Designated_Type
(Desig_Type
))
8746 ("designated type of actual does not match that of formal &",
8748 Abandon_Instantiation
(Actual
);
8750 end Validate_Access_Type_Instance
;
8752 ----------------------------------
8753 -- Validate_Array_Type_Instance --
8754 ----------------------------------
8756 procedure Validate_Array_Type_Instance
is
8761 function Formal_Dimensions
return Int
;
8762 -- Count number of dimensions in array type formal
8764 -----------------------
8765 -- Formal_Dimensions --
8766 -----------------------
8768 function Formal_Dimensions
return Int
is
8773 if Nkind
(Def
) = N_Constrained_Array_Definition
then
8774 Index
:= First
(Discrete_Subtype_Definitions
(Def
));
8776 Index
:= First
(Subtype_Marks
(Def
));
8779 while Present
(Index
) loop
8785 end Formal_Dimensions
;
8787 -- Start of processing for Validate_Array_Type_Instance
8790 if not Is_Array_Type
(Act_T
) then
8792 ("expect array type in instantiation of &", Actual
, Gen_T
);
8793 Abandon_Instantiation
(Actual
);
8795 elsif Nkind
(Def
) = N_Constrained_Array_Definition
then
8796 if not (Is_Constrained
(Act_T
)) then
8798 ("expect constrained array in instantiation of &",
8800 Abandon_Instantiation
(Actual
);
8804 if Is_Constrained
(Act_T
) then
8806 ("expect unconstrained array in instantiation of &",
8808 Abandon_Instantiation
(Actual
);
8812 if Formal_Dimensions
/= Number_Dimensions
(Act_T
) then
8814 ("dimensions of actual do not match formal &", Actual
, Gen_T
);
8815 Abandon_Instantiation
(Actual
);
8818 I1
:= First_Index
(A_Gen_T
);
8819 I2
:= First_Index
(Act_T
);
8820 for J
in 1 .. Formal_Dimensions
loop
8822 -- If the indices of the actual were given by a subtype_mark,
8823 -- the index was transformed into a range attribute. Retrieve
8824 -- the original type mark for checking.
8826 if Is_Entity_Name
(Original_Node
(I2
)) then
8827 T2
:= Entity
(Original_Node
(I2
));
8832 if not Subtypes_Match
8833 (Find_Actual_Type
(Etype
(I1
), Scope
(A_Gen_T
)), T2
)
8836 ("index types of actual do not match those of formal &",
8838 Abandon_Instantiation
(Actual
);
8845 if not Subtypes_Match
(
8846 Find_Actual_Type
(Component_Type
(A_Gen_T
), Scope
(A_Gen_T
)),
8847 Component_Type
(Act_T
))
8850 ("component subtype of actual does not match that of formal &",
8852 Abandon_Instantiation
(Actual
);
8855 if Has_Aliased_Components
(A_Gen_T
)
8856 and then not Has_Aliased_Components
(Act_T
)
8859 ("actual must have aliased components to match formal type &",
8863 end Validate_Array_Type_Instance
;
8865 -----------------------------------------------
8866 -- Validate_Derived_Interface_Type_Instance --
8867 -----------------------------------------------
8869 procedure Validate_Derived_Interface_Type_Instance
is
8870 Par
: constant Entity_Id
:= Entity
(Subtype_Indication
(Def
));
8874 -- First apply interface instance checks
8876 Validate_Interface_Type_Instance
;
8878 -- Verify that immediate parent interface is an ancestor of
8882 and then not Interface_Present_In_Ancestor
(Act_T
, Par
)
8885 ("interface actual must include progenitor&", Actual
, Par
);
8888 -- Now verify that the actual includes all other ancestors of
8891 Elmt
:= First_Elmt
(Abstract_Interfaces
(A_Gen_T
));
8892 while Present
(Elmt
) loop
8893 if not Interface_Present_In_Ancestor
(Act_T
, Node
(Elmt
)) then
8895 ("interface actual must include progenitor&",
8896 Actual
, Node
(Elmt
));
8901 end Validate_Derived_Interface_Type_Instance
;
8903 ------------------------------------
8904 -- Validate_Derived_Type_Instance --
8905 ------------------------------------
8907 procedure Validate_Derived_Type_Instance
is
8908 Actual_Discr
: Entity_Id
;
8909 Ancestor_Discr
: Entity_Id
;
8912 -- If the parent type in the generic declaration is itself a previous
8913 -- formal type, then it is local to the generic and absent from the
8914 -- analyzed generic definition. In that case the ancestor is the
8915 -- instance of the formal (which must have been instantiated
8916 -- previously), unless the ancestor is itself a formal derived type.
8917 -- In this latter case (which is the subject of Corrigendum 8652/0038
8918 -- (AI-202) the ancestor of the formals is the ancestor of its
8919 -- parent. Otherwise, the analyzed generic carries the parent type.
8920 -- If the parent type is defined in a previous formal package, then
8921 -- the scope of that formal package is that of the generic type
8922 -- itself, and it has already been mapped into the corresponding type
8923 -- in the actual package.
8925 -- Common case: parent type defined outside of the generic
8927 if Is_Entity_Name
(Subtype_Mark
(Def
))
8928 and then Present
(Entity
(Subtype_Mark
(Def
)))
8930 Ancestor
:= Get_Instance_Of
(Entity
(Subtype_Mark
(Def
)));
8932 -- Check whether parent is defined in a previous formal package
8935 Scope
(Scope
(Base_Type
(Etype
(A_Gen_T
)))) = Scope
(A_Gen_T
)
8938 Get_Instance_Of
(Base_Type
(Etype
(A_Gen_T
)));
8940 -- The type may be a local derivation, or a type extension of
8941 -- a previous formal, or of a formal of a parent package.
8943 elsif Is_Derived_Type
(Get_Instance_Of
(A_Gen_T
))
8945 Ekind
(Get_Instance_Of
(A_Gen_T
)) = E_Record_Type_With_Private
8947 -- Check whether the parent is another derived formal type
8948 -- in the same generic unit.
8950 if Etype
(A_Gen_T
) /= A_Gen_T
8951 and then Is_Generic_Type
(Etype
(A_Gen_T
))
8952 and then Scope
(Etype
(A_Gen_T
)) = Scope
(A_Gen_T
)
8953 and then Etype
(Etype
(A_Gen_T
)) /= Etype
(A_Gen_T
)
8955 -- Locate ancestor of parent from the subtype declaration
8956 -- created for the actual.
8962 Decl
:= First
(Actual_Decls
);
8963 while Present
(Decl
) loop
8964 if Nkind
(Decl
) = N_Subtype_Declaration
8965 and then Chars
(Defining_Identifier
(Decl
)) =
8966 Chars
(Etype
(A_Gen_T
))
8968 Ancestor
:= Generic_Parent_Type
(Decl
);
8976 pragma Assert
(Present
(Ancestor
));
8980 Get_Instance_Of
(Base_Type
(Get_Instance_Of
(A_Gen_T
)));
8984 Ancestor
:= Get_Instance_Of
(Etype
(Base_Type
(A_Gen_T
)));
8987 -- Ada 2005 (AI-251)
8989 if Ada_Version
>= Ada_05
8990 and then Is_Interface
(Ancestor
)
8992 if not Interface_Present_In_Ancestor
(Act_T
, Ancestor
) then
8994 ("(Ada 2005) expected type implementing & in instantiation",
8998 elsif not Is_Ancestor
(Base_Type
(Ancestor
), Act_T
) then
9000 ("expect type derived from & in instantiation",
9001 Actual
, First_Subtype
(Ancestor
));
9002 Abandon_Instantiation
(Actual
);
9005 -- Ada 2005 (AI-443): Synchronized formal derived type ckecks. Note
9006 -- that the formal type declaration has been rewritten as a private
9009 if Ada_Version
>= Ada_05
9010 and then Nkind
(Parent
(A_Gen_T
)) = N_Private_Extension_Declaration
9011 and then Synchronized_Present
(Parent
(A_Gen_T
))
9013 -- The actual must be a synchronized tagged type
9015 if not Is_Tagged_Type
(Act_T
) then
9017 ("actual of synchronized type must be tagged", Actual
);
9018 Abandon_Instantiation
(Actual
);
9020 elsif Nkind
(Parent
(Act_T
)) = N_Full_Type_Declaration
9021 and then Nkind
(Type_Definition
(Parent
(Act_T
))) =
9022 N_Derived_Type_Definition
9023 and then not Synchronized_Present
(Type_Definition
9027 ("actual of synchronized type must be synchronized", Actual
);
9028 Abandon_Instantiation
(Actual
);
9032 -- Perform atomic/volatile checks (RM C.6(12))
9034 if Is_Atomic
(Act_T
) and then not Is_Atomic
(Ancestor
) then
9036 ("cannot have atomic actual type for non-atomic formal type",
9039 elsif Is_Volatile
(Act_T
)
9040 and then not Is_Volatile
(Ancestor
)
9041 and then Is_By_Reference_Type
(Ancestor
)
9044 ("cannot have volatile actual type for non-volatile formal type",
9048 -- It should not be necessary to check for unknown discriminants
9049 -- on Formal, but for some reason Has_Unknown_Discriminants is
9050 -- false for A_Gen_T, so Is_Indefinite_Subtype incorrectly
9051 -- returns False. This needs fixing. ???
9053 if not Is_Indefinite_Subtype
(A_Gen_T
)
9054 and then not Unknown_Discriminants_Present
(Formal
)
9055 and then Is_Indefinite_Subtype
(Act_T
)
9058 ("actual subtype must be constrained", Actual
);
9059 Abandon_Instantiation
(Actual
);
9062 if not Unknown_Discriminants_Present
(Formal
) then
9063 if Is_Constrained
(Ancestor
) then
9064 if not Is_Constrained
(Act_T
) then
9066 ("actual subtype must be constrained", Actual
);
9067 Abandon_Instantiation
(Actual
);
9070 -- Ancestor is unconstrained, Check if generic formal and
9071 -- actual agree on constrainedness. The check only applies
9072 -- to array types and discriminated types.
9074 elsif Is_Constrained
(Act_T
) then
9075 if Ekind
(Ancestor
) = E_Access_Type
9077 (not Is_Constrained
(A_Gen_T
)
9078 and then Is_Composite_Type
(A_Gen_T
))
9081 ("actual subtype must be unconstrained", Actual
);
9082 Abandon_Instantiation
(Actual
);
9085 -- A class-wide type is only allowed if the formal has
9086 -- unknown discriminants.
9088 elsif Is_Class_Wide_Type
(Act_T
)
9089 and then not Has_Unknown_Discriminants
(Ancestor
)
9092 ("actual for & cannot be a class-wide type", Actual
, Gen_T
);
9093 Abandon_Instantiation
(Actual
);
9095 -- Otherwise, the formal and actual shall have the same
9096 -- number of discriminants and each discriminant of the
9097 -- actual must correspond to a discriminant of the formal.
9099 elsif Has_Discriminants
(Act_T
)
9100 and then not Has_Unknown_Discriminants
(Act_T
)
9101 and then Has_Discriminants
(Ancestor
)
9103 Actual_Discr
:= First_Discriminant
(Act_T
);
9104 Ancestor_Discr
:= First_Discriminant
(Ancestor
);
9105 while Present
(Actual_Discr
)
9106 and then Present
(Ancestor_Discr
)
9108 if Base_Type
(Act_T
) /= Base_Type
(Ancestor
) and then
9109 No
(Corresponding_Discriminant
(Actual_Discr
))
9112 ("discriminant & does not correspond " &
9113 "to ancestor discriminant", Actual
, Actual_Discr
);
9114 Abandon_Instantiation
(Actual
);
9117 Next_Discriminant
(Actual_Discr
);
9118 Next_Discriminant
(Ancestor_Discr
);
9121 if Present
(Actual_Discr
) or else Present
(Ancestor_Discr
) then
9123 ("actual for & must have same number of discriminants",
9125 Abandon_Instantiation
(Actual
);
9128 -- This case should be caught by the earlier check for
9129 -- for constrainedness, but the check here is added for
9132 elsif Has_Discriminants
(Act_T
)
9133 and then not Has_Unknown_Discriminants
(Act_T
)
9136 ("actual for & must not have discriminants", Actual
, Gen_T
);
9137 Abandon_Instantiation
(Actual
);
9139 elsif Has_Discriminants
(Ancestor
) then
9141 ("actual for & must have known discriminants", Actual
, Gen_T
);
9142 Abandon_Instantiation
(Actual
);
9145 if not Subtypes_Statically_Compatible
(Act_T
, Ancestor
) then
9147 ("constraint on actual is incompatible with formal", Actual
);
9148 Abandon_Instantiation
(Actual
);
9151 end Validate_Derived_Type_Instance
;
9153 --------------------------------------
9154 -- Validate_Interface_Type_Instance --
9155 --------------------------------------
9157 procedure Validate_Interface_Type_Instance
is
9159 if not Is_Interface
(Act_T
) then
9161 ("actual for formal interface type must be an interface",
9164 elsif Is_Limited_Type
(Act_T
) /= Is_Limited_Type
(A_Gen_T
)
9166 Is_Task_Interface
(A_Gen_T
) /= Is_Task_Interface
(Act_T
)
9168 Is_Protected_Interface
(A_Gen_T
) /=
9169 Is_Protected_Interface
(Act_T
)
9171 Is_Synchronized_Interface
(A_Gen_T
) /=
9172 Is_Synchronized_Interface
(Act_T
)
9175 ("actual for interface& does not match ('R'M 12.5.5(5))",
9178 end Validate_Interface_Type_Instance
;
9180 ------------------------------------
9181 -- Validate_Private_Type_Instance --
9182 ------------------------------------
9184 procedure Validate_Private_Type_Instance
is
9185 Formal_Discr
: Entity_Id
;
9186 Actual_Discr
: Entity_Id
;
9187 Formal_Subt
: Entity_Id
;
9190 if Is_Limited_Type
(Act_T
)
9191 and then not Is_Limited_Type
(A_Gen_T
)
9194 ("actual for non-limited & cannot be a limited type", Actual
,
9196 Explain_Limited_Type
(Act_T
, Actual
);
9197 Abandon_Instantiation
(Actual
);
9199 elsif Known_To_Have_Preelab_Init
(A_Gen_T
)
9200 and then not Has_Preelaborable_Initialization
(Act_T
)
9203 ("actual for & must have preelaborable initialization", Actual
,
9206 elsif Is_Indefinite_Subtype
(Act_T
)
9207 and then not Is_Indefinite_Subtype
(A_Gen_T
)
9208 and then Ada_Version
>= Ada_95
9211 ("actual for & must be a definite subtype", Actual
, Gen_T
);
9213 elsif not Is_Tagged_Type
(Act_T
)
9214 and then Is_Tagged_Type
(A_Gen_T
)
9217 ("actual for & must be a tagged type", Actual
, Gen_T
);
9219 elsif Has_Discriminants
(A_Gen_T
) then
9220 if not Has_Discriminants
(Act_T
) then
9222 ("actual for & must have discriminants", Actual
, Gen_T
);
9223 Abandon_Instantiation
(Actual
);
9225 elsif Is_Constrained
(Act_T
) then
9227 ("actual for & must be unconstrained", Actual
, Gen_T
);
9228 Abandon_Instantiation
(Actual
);
9231 Formal_Discr
:= First_Discriminant
(A_Gen_T
);
9232 Actual_Discr
:= First_Discriminant
(Act_T
);
9233 while Formal_Discr
/= Empty
loop
9234 if Actual_Discr
= Empty
then
9236 ("discriminants on actual do not match formal",
9238 Abandon_Instantiation
(Actual
);
9241 Formal_Subt
:= Get_Instance_Of
(Etype
(Formal_Discr
));
9243 -- Access discriminants match if designated types do
9245 if Ekind
(Base_Type
(Formal_Subt
)) = E_Anonymous_Access_Type
9246 and then (Ekind
(Base_Type
(Etype
(Actual_Discr
)))) =
9247 E_Anonymous_Access_Type
9250 (Designated_Type
(Base_Type
(Formal_Subt
))) =
9251 Designated_Type
(Base_Type
(Etype
(Actual_Discr
)))
9255 elsif Base_Type
(Formal_Subt
) /=
9256 Base_Type
(Etype
(Actual_Discr
))
9259 ("types of actual discriminants must match formal",
9261 Abandon_Instantiation
(Actual
);
9263 elsif not Subtypes_Statically_Match
9264 (Formal_Subt
, Etype
(Actual_Discr
))
9265 and then Ada_Version
>= Ada_95
9268 ("subtypes of actual discriminants must match formal",
9270 Abandon_Instantiation
(Actual
);
9273 Next_Discriminant
(Formal_Discr
);
9274 Next_Discriminant
(Actual_Discr
);
9277 if Actual_Discr
/= Empty
then
9279 ("discriminants on actual do not match formal",
9281 Abandon_Instantiation
(Actual
);
9288 end Validate_Private_Type_Instance
;
9290 -- Start of processing for Instantiate_Type
9293 if Get_Instance_Of
(A_Gen_T
) /= A_Gen_T
then
9294 Error_Msg_N
("duplicate instantiation of generic type", Actual
);
9297 elsif not Is_Entity_Name
(Actual
)
9298 or else not Is_Type
(Entity
(Actual
))
9301 ("expect valid subtype mark to instantiate &", Actual
, Gen_T
);
9302 Abandon_Instantiation
(Actual
);
9305 Act_T
:= Entity
(Actual
);
9307 -- Ada 2005 (AI-216): An Unchecked_Union subtype shall only be passed
9308 -- as a generic actual parameter if the corresponding formal type
9309 -- does not have a known_discriminant_part, or is a formal derived
9310 -- type that is an Unchecked_Union type.
9312 if Is_Unchecked_Union
(Base_Type
(Act_T
)) then
9313 if not Has_Discriminants
(A_Gen_T
)
9315 (Is_Derived_Type
(A_Gen_T
)
9317 Is_Unchecked_Union
(A_Gen_T
))
9321 Error_Msg_N
("Unchecked_Union cannot be the actual for a" &
9322 " discriminated formal type", Act_T
);
9327 -- Deal with fixed/floating restrictions
9329 if Is_Floating_Point_Type
(Act_T
) then
9330 Check_Restriction
(No_Floating_Point
, Actual
);
9331 elsif Is_Fixed_Point_Type
(Act_T
) then
9332 Check_Restriction
(No_Fixed_Point
, Actual
);
9335 -- Deal with error of using incomplete type as generic actual
9337 if Ekind
(Act_T
) = E_Incomplete_Type
9338 or else (Is_Class_Wide_Type
(Act_T
)
9340 Ekind
(Root_Type
(Act_T
)) = E_Incomplete_Type
)
9342 if Is_Class_Wide_Type
(Act_T
)
9343 or else No
(Underlying_Type
(Act_T
))
9345 Error_Msg_N
("premature use of incomplete type", Actual
);
9346 Abandon_Instantiation
(Actual
);
9348 Act_T
:= Full_View
(Act_T
);
9349 Set_Entity
(Actual
, Act_T
);
9351 if Has_Private_Component
(Act_T
) then
9353 ("premature use of type with private component", Actual
);
9357 -- Deal with error of premature use of private type as generic actual
9359 elsif Is_Private_Type
(Act_T
)
9360 and then Is_Private_Type
(Base_Type
(Act_T
))
9361 and then not Is_Generic_Type
(Act_T
)
9362 and then not Is_Derived_Type
(Act_T
)
9363 and then No
(Full_View
(Root_Type
(Act_T
)))
9365 Error_Msg_N
("premature use of private type", Actual
);
9367 elsif Has_Private_Component
(Act_T
) then
9369 ("premature use of type with private component", Actual
);
9372 Set_Instance_Of
(A_Gen_T
, Act_T
);
9374 -- If the type is generic, the class-wide type may also be used
9376 if Is_Tagged_Type
(A_Gen_T
)
9377 and then Is_Tagged_Type
(Act_T
)
9378 and then not Is_Class_Wide_Type
(A_Gen_T
)
9380 Set_Instance_Of
(Class_Wide_Type
(A_Gen_T
),
9381 Class_Wide_Type
(Act_T
));
9384 if not Is_Abstract
(A_Gen_T
)
9385 and then Is_Abstract
(Act_T
)
9388 ("actual of non-abstract formal cannot be abstract", Actual
);
9391 if Is_Scalar_Type
(Gen_T
) then
9392 Set_Instance_Of
(Etype
(A_Gen_T
), Etype
(Act_T
));
9397 when N_Formal_Private_Type_Definition
=>
9398 Validate_Private_Type_Instance
;
9400 when N_Formal_Derived_Type_Definition
=>
9401 Validate_Derived_Type_Instance
;
9403 when N_Formal_Discrete_Type_Definition
=>
9404 if not Is_Discrete_Type
(Act_T
) then
9406 ("expect discrete type in instantiation of&", Actual
, Gen_T
);
9407 Abandon_Instantiation
(Actual
);
9410 when N_Formal_Signed_Integer_Type_Definition
=>
9411 if not Is_Signed_Integer_Type
(Act_T
) then
9413 ("expect signed integer type in instantiation of&",
9415 Abandon_Instantiation
(Actual
);
9418 when N_Formal_Modular_Type_Definition
=>
9419 if not Is_Modular_Integer_Type
(Act_T
) then
9421 ("expect modular type in instantiation of &", Actual
, Gen_T
);
9422 Abandon_Instantiation
(Actual
);
9425 when N_Formal_Floating_Point_Definition
=>
9426 if not Is_Floating_Point_Type
(Act_T
) then
9428 ("expect float type in instantiation of &", Actual
, Gen_T
);
9429 Abandon_Instantiation
(Actual
);
9432 when N_Formal_Ordinary_Fixed_Point_Definition
=>
9433 if not Is_Ordinary_Fixed_Point_Type
(Act_T
) then
9435 ("expect ordinary fixed point type in instantiation of &",
9437 Abandon_Instantiation
(Actual
);
9440 when N_Formal_Decimal_Fixed_Point_Definition
=>
9441 if not Is_Decimal_Fixed_Point_Type
(Act_T
) then
9443 ("expect decimal type in instantiation of &",
9445 Abandon_Instantiation
(Actual
);
9448 when N_Array_Type_Definition
=>
9449 Validate_Array_Type_Instance
;
9451 when N_Access_To_Object_Definition
=>
9452 Validate_Access_Type_Instance
;
9454 when N_Access_Function_Definition |
9455 N_Access_Procedure_Definition
=>
9456 Validate_Access_Subprogram_Instance
;
9458 when N_Record_Definition
=>
9459 Validate_Interface_Type_Instance
;
9461 when N_Derived_Type_Definition
=>
9462 Validate_Derived_Interface_Type_Instance
;
9465 raise Program_Error
;
9469 Subt
:= New_Copy
(Gen_T
);
9471 -- Use adjusted sloc of subtype name as the location for other
9472 -- nodes in the subtype declaration.
9477 Make_Subtype_Declaration
(Loc
,
9478 Defining_Identifier
=> Subt
,
9479 Subtype_Indication
=> New_Reference_To
(Act_T
, Loc
));
9481 if Is_Private_Type
(Act_T
) then
9482 Set_Has_Private_View
(Subtype_Indication
(Decl_Node
));
9484 elsif Is_Access_Type
(Act_T
)
9485 and then Is_Private_Type
(Designated_Type
(Act_T
))
9487 Set_Has_Private_View
(Subtype_Indication
(Decl_Node
));
9490 -- Flag actual derived types so their elaboration produces the
9491 -- appropriate renamings for the primitive operations of the ancestor.
9492 -- Flag actual for formal private types as well, to determine whether
9493 -- operations in the private part may override inherited operations.
9495 if Nkind
(Def
) = N_Formal_Derived_Type_Definition
9496 or else Nkind
(Def
) = N_Formal_Private_Type_Definition
9498 Set_Generic_Parent_Type
(Decl_Node
, Ancestor
);
9502 end Instantiate_Type
;
9504 -----------------------
9505 -- Is_Generic_Formal --
9506 -----------------------
9508 function Is_Generic_Formal
(E
: Entity_Id
) return Boolean is
9509 Kind
: constant Node_Kind
:= Nkind
(Parent
(E
));
9512 Kind
= N_Formal_Object_Declaration
9513 or else Kind
= N_Formal_Package_Declaration
9514 or else Kind
in N_Formal_Subprogram_Declaration
9515 or else Kind
= N_Formal_Type_Declaration
;
9516 end Is_Generic_Formal
;
9518 ---------------------
9519 -- Is_In_Main_Unit --
9520 ---------------------
9522 function Is_In_Main_Unit
(N
: Node_Id
) return Boolean is
9523 Unum
: constant Unit_Number_Type
:= Get_Source_Unit
(N
);
9524 Current_Unit
: Node_Id
;
9527 if Unum
= Main_Unit
then
9530 -- If the current unit is a subunit then it is either the main unit
9531 -- or is being compiled as part of the main unit.
9533 elsif Nkind
(N
) = N_Compilation_Unit
then
9534 return Nkind
(Unit
(N
)) = N_Subunit
;
9537 Current_Unit
:= Parent
(N
);
9538 while Present
(Current_Unit
)
9539 and then Nkind
(Current_Unit
) /= N_Compilation_Unit
9541 Current_Unit
:= Parent
(Current_Unit
);
9544 -- The instantiation node is in the main unit, or else the current
9545 -- node (perhaps as the result of nested instantiations) is in the
9546 -- main unit, or in the declaration of the main unit, which in this
9547 -- last case must be a body.
9549 return Unum
= Main_Unit
9550 or else Current_Unit
= Cunit
(Main_Unit
)
9551 or else Current_Unit
= Library_Unit
(Cunit
(Main_Unit
))
9552 or else (Present
(Library_Unit
(Current_Unit
))
9553 and then Is_In_Main_Unit
(Library_Unit
(Current_Unit
)));
9554 end Is_In_Main_Unit
;
9556 ----------------------------
9557 -- Load_Parent_Of_Generic --
9558 ----------------------------
9560 procedure Load_Parent_Of_Generic
(N
: Node_Id
; Spec
: Node_Id
) is
9561 Comp_Unit
: constant Node_Id
:= Cunit
(Get_Source_Unit
(Spec
));
9562 Save_Style_Check
: constant Boolean := Style_Check
;
9563 True_Parent
: Node_Id
;
9564 Inst_Node
: Node_Id
;
9568 if not In_Same_Source_Unit
(N
, Spec
)
9569 or else Nkind
(Unit
(Comp_Unit
)) = N_Package_Declaration
9570 or else (Nkind
(Unit
(Comp_Unit
)) = N_Package_Body
9571 and then not Is_In_Main_Unit
(Spec
))
9573 -- Find body of parent of spec, and analyze it. A special case
9574 -- arises when the parent is an instantiation, that is to say when
9575 -- we are currently instantiating a nested generic. In that case,
9576 -- there is no separate file for the body of the enclosing instance.
9577 -- Instead, the enclosing body must be instantiated as if it were
9578 -- a pending instantiation, in order to produce the body for the
9579 -- nested generic we require now. Note that in that case the
9580 -- generic may be defined in a package body, the instance defined
9581 -- in the same package body, and the original enclosing body may not
9582 -- be in the main unit.
9584 True_Parent
:= Parent
(Spec
);
9587 while Present
(True_Parent
)
9588 and then Nkind
(True_Parent
) /= N_Compilation_Unit
9590 if Nkind
(True_Parent
) = N_Package_Declaration
9592 Nkind
(Original_Node
(True_Parent
)) = N_Package_Instantiation
9594 -- Parent is a compilation unit that is an instantiation.
9595 -- Instantiation node has been replaced with package decl.
9597 Inst_Node
:= Original_Node
(True_Parent
);
9600 elsif Nkind
(True_Parent
) = N_Package_Declaration
9601 and then Present
(Generic_Parent
(Specification
(True_Parent
)))
9602 and then Nkind
(Parent
(True_Parent
)) /= N_Compilation_Unit
9604 -- Parent is an instantiation within another specification.
9605 -- Declaration for instance has been inserted before original
9606 -- instantiation node. A direct link would be preferable?
9608 Inst_Node
:= Next
(True_Parent
);
9610 while Present
(Inst_Node
)
9611 and then Nkind
(Inst_Node
) /= N_Package_Instantiation
9616 -- If the instance appears within a generic, and the generic
9617 -- unit is defined within a formal package of the enclosing
9618 -- generic, there is no generic body available, and none
9619 -- needed. A more precise test should be used ???
9621 if No
(Inst_Node
) then
9627 True_Parent
:= Parent
(True_Parent
);
9631 -- Case where we are currently instantiating a nested generic
9633 if Present
(Inst_Node
) then
9634 if Nkind
(Parent
(True_Parent
)) = N_Compilation_Unit
then
9636 -- Instantiation node and declaration of instantiated package
9637 -- were exchanged when only the declaration was needed.
9638 -- Restore instantiation node before proceeding with body.
9640 Set_Unit
(Parent
(True_Parent
), Inst_Node
);
9643 -- Now complete instantiation of enclosing body, if it appears
9644 -- in some other unit. If it appears in the current unit, the
9645 -- body will have been instantiated already.
9647 if No
(Corresponding_Body
(Instance_Spec
(Inst_Node
))) then
9649 -- We need to determine the expander mode to instantiate
9650 -- the enclosing body. Because the generic body we need
9651 -- may use global entities declared in the enclosing package
9652 -- (including aggregates) it is in general necessary to
9653 -- compile this body with expansion enabled. The exception
9654 -- is if we are within a generic package, in which case
9655 -- the usual generic rule applies.
9658 Exp_Status
: Boolean := True;
9662 -- Loop through scopes looking for generic package
9664 Scop
:= Scope
(Defining_Entity
(Instance_Spec
(Inst_Node
)));
9665 while Present
(Scop
)
9666 and then Scop
/= Standard_Standard
9668 if Ekind
(Scop
) = E_Generic_Package
then
9669 Exp_Status
:= False;
9673 Scop
:= Scope
(Scop
);
9676 Instantiate_Package_Body
9677 (Pending_Body_Info
'(
9678 Inst_Node, True_Parent, Exp_Status,
9679 Get_Code_Unit (Sloc (Inst_Node))));
9683 -- Case where we are not instantiating a nested generic
9686 Opt.Style_Check := False;
9687 Expander_Mode_Save_And_Set (True);
9688 Load_Needed_Body (Comp_Unit, OK);
9689 Opt.Style_Check := Save_Style_Check;
9690 Expander_Mode_Restore;
9693 and then Unit_Requires_Body (Defining_Entity (Spec))
9696 Bname : constant Unit_Name_Type :=
9697 Get_Body_Name (Get_Unit_Name (Unit (Comp_Unit)));
9700 Error_Msg_Unit_1 := Bname;
9701 Error_Msg_N ("this instantiation requires$!", N);
9703 Get_File_Name (Bname, Subunit => False);
9704 Error_Msg_N ("\but file{ was not found!", N);
9705 raise Unrecoverable_Error;
9711 -- If loading the parent of the generic caused an instantiation
9712 -- circularity, we abandon compilation at this point, because
9713 -- otherwise in some cases we get into trouble with infinite
9714 -- recursions after this point.
9716 if Circularity_Detected then
9717 raise Unrecoverable_Error;
9719 end Load_Parent_Of_Generic;
9721 -----------------------
9722 -- Move_Freeze_Nodes --
9723 -----------------------
9725 procedure Move_Freeze_Nodes
9726 (Out_Of : Entity_Id;
9731 Next_Decl : Node_Id;
9732 Next_Node : Node_Id := After;
9735 function Is_Outer_Type (T : Entity_Id) return Boolean;
9736 -- Check whether entity is declared in a scope external to that
9737 -- of the generic unit.
9743 function Is_Outer_Type (T : Entity_Id) return Boolean is
9744 Scop : Entity_Id := Scope (T);
9747 if Scope_Depth (Scop) < Scope_Depth (Out_Of) then
9751 while Scop /= Standard_Standard loop
9753 if Scop = Out_Of then
9756 Scop := Scope (Scop);
9764 -- Start of processing for Move_Freeze_Nodes
9771 -- First remove the freeze nodes that may appear before all other
9775 while Present (Decl)
9776 and then Nkind (Decl) = N_Freeze_Entity
9777 and then Is_Outer_Type (Entity (Decl))
9779 Decl := Remove_Head (L);
9780 Insert_After (Next_Node, Decl);
9781 Set_Analyzed (Decl, False);
9786 -- Next scan the list of declarations and remove each freeze node that
9787 -- appears ahead of the current node.
9789 while Present (Decl) loop
9790 while Present (Next (Decl))
9791 and then Nkind (Next (Decl)) = N_Freeze_Entity
9792 and then Is_Outer_Type (Entity (Next (Decl)))
9794 Next_Decl := Remove_Next (Decl);
9795 Insert_After (Next_Node, Next_Decl);
9796 Set_Analyzed (Next_Decl, False);
9797 Next_Node := Next_Decl;
9800 -- If the declaration is a nested package or concurrent type, then
9801 -- recurse. Nested generic packages will have been processed from the
9804 if Nkind (Decl) = N_Package_Declaration then
9805 Spec := Specification (Decl);
9807 elsif Nkind (Decl) = N_Task_Type_Declaration then
9808 Spec := Task_Definition (Decl);
9810 elsif Nkind (Decl) = N_Protected_Type_Declaration then
9811 Spec := Protected_Definition (Decl);
9817 if Present (Spec) then
9818 Move_Freeze_Nodes (Out_Of, Next_Node,
9819 Visible_Declarations (Spec));
9820 Move_Freeze_Nodes (Out_Of, Next_Node,
9821 Private_Declarations (Spec));
9826 end Move_Freeze_Nodes;
9832 function Next_Assoc (E : Assoc_Ptr) return Assoc_Ptr is
9834 return Generic_Renamings.Table (E).Next_In_HTable;
9837 ------------------------
9838 -- Preanalyze_Actuals --
9839 ------------------------
9841 procedure Pre_Analyze_Actuals (N : Node_Id) is
9844 Errs : constant Int := Serious_Errors_Detected;
9847 Assoc := First (Generic_Associations (N));
9848 while Present (Assoc) loop
9849 if Nkind (Assoc) /= N_Others_Choice then
9850 Act := Explicit_Generic_Actual_Parameter (Assoc);
9852 -- Within a nested instantiation, a defaulted actual is an empty
9853 -- association, so nothing to analyze. If the subprogram actual
9854 -- isan attribute, analyze prefix only, because actual is not a
9855 -- complete attribute reference.
9857 -- If actual is an allocator, analyze expression only. The full
9858 -- analysis can generate code, and if instance is a compilation
9859 -- unit we have to wait until the package instance is installed
9860 -- to have a proper place to insert this code.
9862 -- String literals may be operators, but at this point we do not
9863 -- know whether the actual is a formal subprogram or a string.
9868 elsif Nkind (Act) = N_Attribute_Reference then
9869 Analyze (Prefix (Act));
9871 elsif Nkind (Act) = N_Explicit_Dereference then
9872 Analyze (Prefix (Act));
9874 elsif Nkind (Act) = N_Allocator then
9876 Expr : constant Node_Id := Expression (Act);
9879 if Nkind (Expr) = N_Subtype_Indication then
9880 Analyze (Subtype_Mark (Expr));
9881 Analyze_List (Constraints (Constraint (Expr)));
9887 elsif Nkind (Act) /= N_Operator_Symbol then
9891 if Errs /= Serious_Errors_Detected then
9892 Abandon_Instantiation (Act);
9898 end Pre_Analyze_Actuals;
9904 procedure Remove_Parent (In_Body : Boolean := False) is
9905 S : Entity_Id := Current_Scope;
9911 -- After child instantiation is complete, remove from scope stack
9912 -- the extra copy of the current scope, and then remove parent
9918 while Current_Scope /= S loop
9920 End_Package_Scope (Current_Scope);
9922 if In_Open_Scopes (P) then
9923 E := First_Entity (P);
9925 while Present (E) loop
9926 Set_Is_Immediately_Visible (E, True);
9930 if Is_Generic_Instance (Current_Scope)
9931 and then P /= Current_Scope
9933 -- We are within an instance of some sibling. Retain
9934 -- visibility of parent, for proper subsequent cleanup,
9935 -- and reinstall private declarations as well.
9937 Set_In_Private_Part (P);
9938 Install_Private_Declarations (P);
9941 -- If the ultimate parent is a top-level unit recorded in
9942 -- Instance_Parent_Unit, then reset its visibility to what
9943 -- it was before instantiation. (It's not clear what the
9944 -- purpose is of testing whether Scope (P) is In_Open_Scopes,
9945 -- but that test was present before the ultimate parent test
9948 elsif not In_Open_Scopes (Scope (P))
9949 or else (P = Instance_Parent_Unit
9950 and then not Parent_Unit_Visible)
9952 Set_Is_Immediately_Visible (P, False);
9956 -- Reset visibility of entities in the enclosing scope
9958 Set_Is_Hidden_Open_Scope (Current_Scope, False);
9959 Hidden := First_Elmt (Hidden_Entities);
9961 while Present (Hidden) loop
9962 Set_Is_Immediately_Visible (Node (Hidden), True);
9967 -- Each body is analyzed separately, and there is no context
9968 -- that needs preserving from one body instance to the next,
9969 -- so remove all parent scopes that have been installed.
9971 while Present (S) loop
9972 End_Package_Scope (S);
9973 Set_Is_Immediately_Visible (S, False);
9975 exit when S = Standard_Standard;
9985 procedure Restore_Env is
9986 Saved : Instance_Env renames Instance_Envs.Table (Instance_Envs.Last);
9989 Ada_Version := Saved.Ada_Version;
9990 Ada_Version_Explicit := Saved.Ada_Version_Explicit;
9992 if No (Current_Instantiated_Parent.Act_Id) then
9994 -- Restore environment after subprogram inlining
9996 Restore_Private_Views (Empty);
9999 Current_Instantiated_Parent := Saved.Instantiated_Parent;
10000 Exchanged_Views := Saved.Exchanged_Views;
10001 Hidden_Entities := Saved.Hidden_Entities;
10002 Current_Sem_Unit := Saved.Current_Sem_Unit;
10003 Parent_Unit_Visible := Saved.Parent_Unit_Visible;
10004 Instance_Parent_Unit := Saved.Instance_Parent_Unit;
10006 Instance_Envs.Decrement_Last;
10009 ---------------------------
10010 -- Restore_Private_Views --
10011 ---------------------------
10013 procedure Restore_Private_Views
10014 (Pack_Id : Entity_Id;
10015 Is_Package : Boolean := True)
10020 Dep_Elmt : Elmt_Id;
10023 procedure Restore_Nested_Formal (Formal : Entity_Id);
10024 -- Hide the generic formals of formal packages declared with box
10025 -- which were reachable in the current instantiation.
10027 procedure Restore_Nested_Formal (Formal : Entity_Id) is
10031 if Present (Renamed_Object (Formal))
10032 and then Denotes_Formal_Package (Renamed_Object (Formal), True)
10036 elsif Present (Associated_Formal_Package (Formal)) then
10038 Ent := First_Entity (Formal);
10039 while Present (Ent) loop
10040 exit when Ekind (Ent) = E_Package
10041 and then Renamed_Entity (Ent) = Renamed_Entity (Formal);
10043 Set_Is_Hidden (Ent);
10044 Set_Is_Potentially_Use_Visible (Ent, False);
10046 -- If package, then recurse
10048 if Ekind (Ent) = E_Package then
10049 Restore_Nested_Formal (Ent);
10055 end Restore_Nested_Formal;
10057 -- Start of processing for Restore_Private_Views
10060 M := First_Elmt (Exchanged_Views);
10061 while Present (M) loop
10064 -- Subtypes of types whose views have been exchanged, and that
10065 -- are defined within the instance, were not on the list of
10066 -- Private_Dependents on entry to the instance, so they have to
10067 -- be exchanged explicitly now, in order to remain consistent with
10068 -- the view of the parent type.
10070 if Ekind (Typ) = E_Private_Type
10071 or else Ekind (Typ) = E_Limited_Private_Type
10072 or else Ekind (Typ) = E_Record_Type_With_Private
10074 Dep_Elmt := First_Elmt (Private_Dependents (Typ));
10075 while Present (Dep_Elmt) loop
10076 Dep_Typ := Node (Dep_Elmt);
10078 if Scope (Dep_Typ) = Pack_Id
10079 and then Present (Full_View (Dep_Typ))
10081 Replace_Elmt (Dep_Elmt, Full_View (Dep_Typ));
10082 Exchange_Declarations (Dep_Typ);
10085 Next_Elmt (Dep_Elmt);
10089 Exchange_Declarations (Node (M));
10093 if No (Pack_Id) then
10097 -- Make the generic formal parameters private, and make the formal
10098 -- types into subtypes of the actuals again.
10100 E := First_Entity (Pack_Id);
10101 while Present (E) loop
10102 Set_Is_Hidden (E, True);
10105 and then Nkind (Parent (E)) = N_Subtype_Declaration
10107 Set_Is_Generic_Actual_Type (E, False);
10109 -- An unusual case of aliasing: the actual may also be directly
10110 -- visible in the generic, and be private there, while it is
10111 -- fully visible in the context of the instance. The internal
10112 -- subtype is private in the instance, but has full visibility
10113 -- like its parent in the enclosing scope. This enforces the
10114 -- invariant that the privacy status of all private dependents of
10115 -- a type coincide with that of the parent type. This can only
10116 -- happen when a generic child unit is instantiated within a
10119 if Is_Private_Type (E)
10120 and then not Is_Private_Type (Etype (E))
10122 Exchange_Declarations (E);
10125 elsif Ekind (E) = E_Package then
10127 -- The end of the renaming list is the renaming of the generic
10128 -- package itself. If the instance is a subprogram, all entities
10129 -- in the corresponding package are renamings. If this entity is
10130 -- a formal package, make its own formals private as well. The
10131 -- actual in this case is itself the renaming of an instantation.
10132 -- If the entity is not a package renaming, it is the entity
10133 -- created to validate formal package actuals: ignore.
10135 -- If the actual is itself a formal package for the enclosing
10136 -- generic, or the actual for such a formal package, it remains
10137 -- visible on exit from the instance, and therefore nothing
10138 -- needs to be done either, except to keep it accessible.
10141 and then Renamed_Object (E) = Pack_Id
10145 elsif Nkind (Parent (E)) /= N_Package_Renaming_Declaration then
10148 elsif Denotes_Formal_Package (Renamed_Object (E), True) then
10149 Set_Is_Hidden (E, False);
10153 Act_P : constant Entity_Id := Renamed_Object (E);
10157 Id := First_Entity (Act_P);
10159 and then Id /= First_Private_Entity (Act_P)
10161 exit when Ekind (Id) = E_Package
10162 and then Renamed_Object (Id) = Act_P;
10164 Set_Is_Hidden (Id, True);
10165 Set_Is_Potentially_Use_Visible (Id, In_Use (Act_P));
10167 if Ekind (Id) = E_Package then
10168 Restore_Nested_Formal (Id);
10179 end Restore_Private_Views;
10186 (Gen_Unit : Entity_Id;
10187 Act_Unit : Entity_Id)
10191 Set_Instance_Env (Gen_Unit, Act_Unit);
10194 ----------------------------
10195 -- Save_Global_References --
10196 ----------------------------
10198 procedure Save_Global_References (N : Node_Id) is
10199 Gen_Scope : Entity_Id;
10203 function Is_Global (E : Entity_Id) return Boolean;
10204 -- Check whether entity is defined outside of generic unit.
10205 -- Examine the scope of an entity, and the scope of the scope,
10206 -- etc, until we find either Standard, in which case the entity
10207 -- is global, or the generic unit itself, which indicates that
10208 -- the entity is local. If the entity is the generic unit itself,
10209 -- as in the case of a recursive call, or the enclosing generic unit,
10210 -- if different from the current scope, then it is local as well,
10211 -- because it will be replaced at the point of instantiation. On
10212 -- the other hand, if it is a reference to a child unit of a common
10213 -- ancestor, which appears in an instantiation, it is global because
10214 -- it is used to denote a specific compilation unit at the time the
10215 -- instantiations will be analyzed.
10217 procedure Reset_Entity (N : Node_Id);
10218 -- Save semantic information on global entity, so that it is not
10219 -- resolved again at instantiation time.
10221 procedure Save_Entity_Descendants (N : Node_Id);
10222 -- Apply Save_Global_References to the two syntactic descendants of
10223 -- non-terminal nodes that carry an Associated_Node and are processed
10224 -- through Reset_Entity. Once the global entity (if any) has been
10225 -- captured together with its type, only two syntactic descendants
10226 -- need to be traversed to complete the processing of the tree rooted
10227 -- at N. This applies to Selected_Components, Expanded_Names, and to
10228 -- Operator nodes. N can also be a character literal, identifier, or
10229 -- operator symbol node, but the call has no effect in these cases.
10231 procedure Save_Global_Defaults (N1, N2 : Node_Id);
10232 -- Default actuals in nested instances must be handled specially
10233 -- because there is no link to them from the original tree. When an
10234 -- actual subprogram is given by a default, we add an explicit generic
10235 -- association for it in the instantiation node. When we save the
10236 -- global references on the name of the instance, we recover the list
10237 -- of generic associations, and add an explicit one to the original
10238 -- generic tree, through which a global actual can be preserved.
10239 -- Similarly, if a child unit is instantiated within a sibling, in the
10240 -- context of the parent, we must preserve the identifier of the parent
10241 -- so that it can be properly resolved in a subsequent instantiation.
10243 procedure Save_Global_Descendant (D : Union_Id);
10244 -- Apply Save_Global_References recursively to the descendents of
10247 procedure Save_References (N : Node_Id);
10248 -- This is the recursive procedure that does the work, once the
10249 -- enclosing generic scope has been established.
10255 function Is_Global (E : Entity_Id) return Boolean is
10256 Se : Entity_Id := Scope (E);
10258 function Is_Instance_Node (Decl : Node_Id) return Boolean;
10259 -- Determine whether the parent node of a reference to a child unit
10260 -- denotes an instantiation or a formal package, in which case the
10261 -- reference to the child unit is global, even if it appears within
10262 -- the current scope (e.g. when the instance appears within the body
10263 -- of an ancestor).
10265 ----------------------
10266 -- Is_Instance_Node --
10267 ----------------------
10269 function Is_Instance_Node (Decl : Node_Id) return Boolean is
10271 return (Nkind (Decl) in N_Generic_Instantiation
10273 Nkind (Original_Node (Decl)) = N_Formal_Package_Declaration);
10274 end Is_Instance_Node;
10276 -- Start of processing for Is_Global
10279 if E = Gen_Scope then
10282 elsif E = Standard_Standard then
10285 elsif Is_Child_Unit (E)
10286 and then (Is_Instance_Node (Parent (N2))
10287 or else (Nkind (Parent (N2)) = N_Expanded_Name
10288 and then N2 = Selector_Name (Parent (N2))
10289 and then Is_Instance_Node (Parent (Parent (N2)))))
10294 while Se /= Gen_Scope loop
10295 if Se = Standard_Standard then
10310 procedure Reset_Entity (N : Node_Id) is
10312 procedure Set_Global_Type (N : Node_Id; N2 : Node_Id);
10313 -- The type of N2 is global to the generic unit. Save the
10314 -- type in the generic node.
10316 function Top_Ancestor (E : Entity_Id) return Entity_Id;
10317 -- Find the ultimate ancestor of the current unit. If it is
10318 -- not a generic unit, then the name of the current unit
10319 -- in the prefix of an expanded name must be replaced with
10320 -- its generic homonym to ensure that it will be properly
10321 -- resolved in an instance.
10323 ---------------------
10324 -- Set_Global_Type --
10325 ---------------------
10327 procedure Set_Global_Type (N : Node_Id; N2 : Node_Id) is
10328 Typ : constant Entity_Id := Etype (N2);
10331 Set_Etype (N, Typ);
10333 if Entity (N) /= N2
10334 and then Has_Private_View (Entity (N))
10336 -- If the entity of N is not the associated node, this is
10337 -- a nested generic and it has an associated node as well,
10338 -- whose type is already the full view (see below). Indicate
10339 -- that the original node has a private view.
10341 Set_Has_Private_View (N);
10344 -- If not a private type, nothing else to do
10346 if not Is_Private_Type (Typ) then
10347 if Is_Array_Type (Typ)
10348 and then Is_Private_Type (Component_Type (Typ))
10350 Set_Has_Private_View (N);
10353 -- If it is a derivation of a private type in a context where
10354 -- no full view is needed, nothing to do either.
10356 elsif No (Full_View (Typ)) and then Typ /= Etype (Typ) then
10359 -- Otherwise mark the type for flipping and use the full_view
10363 Set_Has_Private_View (N);
10365 if Present (Full_View (Typ)) then
10366 Set_Etype (N2, Full_View (Typ));
10369 end Set_Global_Type;
10375 function Top_Ancestor (E : Entity_Id) return Entity_Id is
10376 Par : Entity_Id := E;
10379 while Is_Child_Unit (Par) loop
10380 Par := Scope (Par);
10386 -- Start of processing for Reset_Entity
10389 N2 := Get_Associated_Node (N);
10392 if Present (E) then
10393 if Is_Global (E) then
10394 Set_Global_Type (N, N2);
10396 elsif Nkind (N) = N_Op_Concat
10397 and then Is_Generic_Type (Etype (N2))
10399 (Base_Type (Etype (Right_Opnd (N2))) = Etype (N2)
10400 or else Base_Type (Etype (Left_Opnd (N2))) = Etype (N2))
10401 and then Is_Intrinsic_Subprogram (E)
10406 -- Entity is local. Mark generic node as unresolved.
10407 -- Note that now it does not have an entity.
10409 Set_Associated_Node (N, Empty);
10410 Set_Etype (N, Empty);
10413 if Nkind (Parent (N)) in N_Generic_Instantiation
10414 and then N = Name (Parent (N))
10416 Save_Global_Defaults (Parent (N), Parent (N2));
10419 elsif Nkind (Parent (N)) = N_Selected_Component
10420 and then Nkind (Parent (N2)) = N_Expanded_Name
10422 if Is_Global (Entity (Parent (N2))) then
10423 Change_Selected_Component_To_Expanded_Name (Parent (N));
10424 Set_Associated_Node (Parent (N), Parent (N2));
10425 Set_Global_Type (Parent (N), Parent (N2));
10426 Save_Entity_Descendants (N);
10428 -- If this is a reference to the current generic entity,
10429 -- replace by the name of the generic homonym of the current
10430 -- package. This is because in an instantiation Par.P.Q will
10431 -- not resolve to the name of the instance, whose enclosing
10432 -- scope is not necessarily Par. We use the generic homonym
10433 -- rather that the name of the generic itself, because it may
10434 -- be hidden by a local declaration.
10436 elsif In_Open_Scopes (Entity (Parent (N2)))
10438 Is_Generic_Unit (Top_Ancestor (Entity (Prefix (Parent (N2)))))
10440 if Ekind (Entity (Parent (N2))) = E_Generic_Package then
10441 Rewrite (Parent (N),
10442 Make_Identifier (Sloc (N),
10444 Chars (Generic_Homonym (Entity (Parent (N2))))));
10446 Rewrite (Parent (N),
10447 Make_Identifier (Sloc (N),
10448 Chars => Chars (Selector_Name (Parent (N2)))));
10452 if Nkind (Parent (Parent (N))) in N_Generic_Instantiation
10453 and then Parent (N) = Name (Parent (Parent (N)))
10455 Save_Global_Defaults
10456 (Parent (Parent (N)), Parent (Parent ((N2))));
10459 -- A selected component may denote a static constant that has
10460 -- been folded. Make the same replacement in original tree.
10462 elsif Nkind (Parent (N)) = N_Selected_Component
10463 and then (Nkind (Parent (N2)) = N_Integer_Literal
10464 or else Nkind (Parent (N2)) = N_Real_Literal)
10466 Rewrite (Parent (N),
10467 New_Copy (Parent (N2)));
10468 Set_Analyzed (Parent (N), False);
10470 -- A selected component may be transformed into a parameterless
10471 -- function call. If the called entity is global, rewrite the
10472 -- node appropriately, i.e. as an extended name for the global
10475 elsif Nkind (Parent (N)) = N_Selected_Component
10476 and then Nkind (Parent (N2)) = N_Function_Call
10477 and then Is_Global (Entity (Name (Parent (N2))))
10479 Change_Selected_Component_To_Expanded_Name (Parent (N));
10480 Set_Associated_Node (Parent (N), Name (Parent (N2)));
10481 Set_Global_Type (Parent (N), Name (Parent (N2)));
10482 Save_Entity_Descendants (N);
10485 -- Entity is local. Reset in generic unit, so that node
10486 -- is resolved anew at the point of instantiation.
10488 Set_Associated_Node (N, Empty);
10489 Set_Etype (N, Empty);
10493 -----------------------------
10494 -- Save_Entity_Descendants --
10495 -----------------------------
10497 procedure Save_Entity_Descendants (N : Node_Id) is
10500 when N_Binary_Op =>
10501 Save_Global_Descendant (Union_Id (Left_Opnd (N)));
10502 Save_Global_Descendant (Union_Id (Right_Opnd (N)));
10505 Save_Global_Descendant (Union_Id (Right_Opnd (N)));
10507 when N_Expanded_Name | N_Selected_Component =>
10508 Save_Global_Descendant (Union_Id (Prefix (N)));
10509 Save_Global_Descendant (Union_Id (Selector_Name (N)));
10511 when N_Identifier | N_Character_Literal | N_Operator_Symbol =>
10515 raise Program_Error;
10517 end Save_Entity_Descendants;
10519 --------------------------
10520 -- Save_Global_Defaults --
10521 --------------------------
10523 procedure Save_Global_Defaults (N1, N2 : Node_Id) is
10524 Loc : constant Source_Ptr := Sloc (N1);
10525 Assoc2 : constant List_Id := Generic_Associations (N2);
10526 Gen_Id : constant Entity_Id := Get_Generic_Entity (N2);
10533 Actual : Entity_Id;
10536 Assoc1 := Generic_Associations (N1);
10538 if Present (Assoc1) then
10539 Act1 := First (Assoc1);
10542 Set_Generic_Associations (N1, New_List);
10543 Assoc1 := Generic_Associations (N1);
10546 if Present (Assoc2) then
10547 Act2 := First (Assoc2);
10552 while Present (Act1) and then Present (Act2) loop
10557 -- Find the associations added for default suprograms
10559 if Present (Act2) then
10560 while Nkind (Act2) /= N_Generic_Association
10561 or else No (Entity (Selector_Name (Act2)))
10562 or else not Is_Overloadable (Entity (Selector_Name (Act2)))
10567 -- Add a similar association if the default is global. The
10568 -- renaming declaration for the actual has been analyzed, and
10569 -- its alias is the program it renames. Link the actual in the
10570 -- original generic tree with the node in the analyzed tree.
10572 while Present (Act2) loop
10573 Subp := Entity (Selector_Name (Act2));
10574 Def := Explicit_Generic_Actual_Parameter (Act2);
10576 -- Following test is defence against rubbish errors
10578 if No (Alias (Subp)) then
10582 -- Retrieve the resolved actual from the renaming declaration
10583 -- created for the instantiated formal.
10585 Actual := Entity (Name (Parent (Parent (Subp))));
10586 Set_Entity (Def, Actual);
10587 Set_Etype (Def, Etype (Actual));
10589 if Is_Global (Actual) then
10591 Make_Generic_Association (Loc,
10592 Selector_Name => New_Occurrence_Of (Subp, Loc),
10593 Explicit_Generic_Actual_Parameter =>
10594 New_Occurrence_Of (Actual, Loc));
10596 Set_Associated_Node
10597 (Explicit_Generic_Actual_Parameter (Ndec), Def);
10599 Append (Ndec, Assoc1);
10601 -- If there are other defaults, add a dummy association
10602 -- in case there are other defaulted formals with the same
10605 elsif Present (Next (Act2)) then
10607 Make_Generic_Association (Loc,
10608 Selector_Name => New_Occurrence_Of (Subp, Loc),
10609 Explicit_Generic_Actual_Parameter => Empty);
10611 Append (Ndec, Assoc1);
10618 if Nkind (Name (N1)) = N_Identifier
10619 and then Is_Child_Unit (Gen_Id)
10620 and then Is_Global (Gen_Id)
10621 and then Is_Generic_Unit (Scope (Gen_Id))
10622 and then In_Open_Scopes (Scope (Gen_Id))
10624 -- This is an instantiation of a child unit within a sibling,
10625 -- so that the generic parent is in scope. An eventual instance
10626 -- must occur within the scope of an instance of the parent.
10627 -- Make name in instance into an expanded name, to preserve the
10628 -- identifier of the parent, so it can be resolved subsequently.
10630 Rewrite (Name (N2),
10631 Make_Expanded_Name (Loc,
10632 Chars => Chars (Gen_Id),
10633 Prefix => New_Occurrence_Of (Scope (Gen_Id), Loc),
10634 Selector_Name => New_Occurrence_Of (Gen_Id, Loc)));
10635 Set_Entity (Name (N2), Gen_Id);
10637 Rewrite (Name (N1),
10638 Make_Expanded_Name (Loc,
10639 Chars => Chars (Gen_Id),
10640 Prefix => New_Occurrence_Of (Scope (Gen_Id), Loc),
10641 Selector_Name => New_Occurrence_Of (Gen_Id, Loc)));
10643 Set_Associated_Node (Name (N1), Name (N2));
10644 Set_Associated_Node (Prefix (Name (N1)), Empty);
10645 Set_Associated_Node
10646 (Selector_Name (Name (N1)), Selector_Name (Name (N2)));
10647 Set_Etype (Name (N1), Etype (Gen_Id));
10650 end Save_Global_Defaults;
10652 ----------------------------
10653 -- Save_Global_Descendant --
10654 ----------------------------
10656 procedure Save_Global_Descendant (D : Union_Id) is
10660 if D in Node_Range then
10661 if D = Union_Id (Empty) then
10664 elsif Nkind (Node_Id (D)) /= N_Compilation_Unit then
10665 Save_References (Node_Id (D));
10668 elsif D in List_Range then
10669 if D = Union_Id (No_List)
10670 or else Is_Empty_List (List_Id (D))
10675 N1 := First (List_Id (D));
10676 while Present (N1) loop
10677 Save_References (N1);
10682 -- Element list or other non-node field, nothing to do
10687 end Save_Global_Descendant;
10689 ---------------------
10690 -- Save_References --
10691 ---------------------
10693 -- This is the recursive procedure that does the work, once the
10694 -- enclosing generic scope has been established. We have to treat
10695 -- specially a number of node rewritings that are required by semantic
10696 -- processing and which change the kind of nodes in the generic copy:
10697 -- typically constant-folding, replacing an operator node by a string
10698 -- literal, or a selected component by an expanded name. In each of
10699 -- those cases, the transformation is propagated to the generic unit.
10701 procedure Save_References (N : Node_Id) is
10706 elsif Nkind (N) = N_Character_Literal
10707 or else Nkind (N) = N_Operator_Symbol
10709 if Nkind (N) = Nkind (Get_Associated_Node (N)) then
10712 elsif Nkind (N) = N_Operator_Symbol
10713 and then Nkind (Get_Associated_Node (N)) = N_String_Literal
10715 Change_Operator_Symbol_To_String_Literal (N);
10718 elsif Nkind (N) in N_Op then
10720 if Nkind (N) = Nkind (Get_Associated_Node (N)) then
10722 if Nkind (N) = N_Op_Concat then
10723 Set_Is_Component_Left_Opnd (N,
10724 Is_Component_Left_Opnd (Get_Associated_Node (N)));
10726 Set_Is_Component_Right_Opnd (N,
10727 Is_Component_Right_Opnd (Get_Associated_Node (N)));
10732 -- Node may be transformed into call to a user-defined operator
10734 N2 := Get_Associated_Node (N);
10736 if Nkind (N2) = N_Function_Call then
10737 E := Entity (Name (N2));
10740 and then Is_Global (E)
10742 Set_Etype (N, Etype (N2));
10744 Set_Associated_Node (N, Empty);
10745 Set_Etype (N, Empty);
10748 elsif Nkind (N2) = N_Integer_Literal
10749 or else Nkind (N2) = N_Real_Literal
10750 or else Nkind (N2) = N_String_Literal
10752 if Present (Original_Node (N2))
10753 and then Nkind (Original_Node (N2)) = Nkind (N)
10756 -- Operation was constant-folded. Whenever possible,
10757 -- recover semantic information from unfolded node,
10760 Set_Associated_Node (N, Original_Node (N2));
10762 if Nkind (N) = N_Op_Concat then
10763 Set_Is_Component_Left_Opnd (N,
10764 Is_Component_Left_Opnd (Get_Associated_Node (N)));
10765 Set_Is_Component_Right_Opnd (N,
10766 Is_Component_Right_Opnd (Get_Associated_Node (N)));
10772 -- If original node is already modified, propagate
10773 -- constant-folding to template.
10775 Rewrite (N, New_Copy (N2));
10776 Set_Analyzed (N, False);
10779 elsif Nkind (N2) = N_Identifier
10780 and then Ekind (Entity (N2)) = E_Enumeration_Literal
10782 -- Same if call was folded into a literal, but in this case
10783 -- retain the entity to avoid spurious ambiguities if id is
10784 -- overloaded at the point of instantiation or inlining.
10786 Rewrite (N, New_Copy (N2));
10787 Set_Analyzed (N, False);
10791 -- Complete the check on operands, if node has not been
10792 -- constant-folded.
10794 if Nkind (N) in N_Op then
10795 Save_Entity_Descendants (N);
10798 elsif Nkind (N) = N_Identifier then
10799 if Nkind (N) = Nkind (Get_Associated_Node (N)) then
10801 -- If this is a discriminant reference, always save it. It is
10802 -- used in the instance to find the corresponding discriminant
10803 -- positionally rather than by name.
10805 Set_Original_Discriminant
10806 (N, Original_Discriminant (Get_Associated_Node (N)));
10810 N2 := Get_Associated_Node (N);
10812 if Nkind (N2) = N_Function_Call then
10813 E := Entity (Name (N2));
10815 -- Name resolves to a call to parameterless function. If
10816 -- original entity is global, mark node as resolved.
10819 and then Is_Global (E)
10821 Set_Etype (N, Etype (N2));
10823 Set_Associated_Node (N, Empty);
10824 Set_Etype (N, Empty);
10828 (Nkind (N2) = N_Integer_Literal
10830 Nkind (N2) = N_Real_Literal)
10831 and then Is_Entity_Name (Original_Node (N2))
10833 -- Name resolves to named number that is constant-folded,
10834 -- We must preserve the original name for ASIS use, and
10835 -- undo the constant-folding, which will be repeated in
10838 Set_Associated_Node (N, Original_Node (N2));
10841 elsif Nkind (N2) = N_String_Literal then
10843 -- Name resolves to string literal. Perform the same
10844 -- replacement in generic.
10846 Rewrite (N, New_Copy (N2));
10848 elsif Nkind (N2) = N_Explicit_Dereference then
10850 -- An identifier is rewritten as a dereference if it is
10851 -- the prefix in a selected component, and it denotes an
10852 -- access to a composite type, or a parameterless function
10853 -- call that returns an access type.
10855 -- Check whether corresponding entity in prefix is global
10857 if Is_Entity_Name (Prefix (N2))
10858 and then Present (Entity (Prefix (N2)))
10859 and then Is_Global (Entity (Prefix (N2)))
10862 Make_Explicit_Dereference (Sloc (N),
10863 Prefix => Make_Identifier (Sloc (N),
10864 Chars => Chars (N))));
10865 Set_Associated_Node (Prefix (N), Prefix (N2));
10867 elsif Nkind (Prefix (N2)) = N_Function_Call
10868 and then Is_Global (Entity (Name (Prefix (N2))))
10871 Make_Explicit_Dereference (Sloc (N),
10872 Prefix => Make_Function_Call (Sloc (N),
10874 Make_Identifier (Sloc (N),
10875 Chars => Chars (N)))));
10877 Set_Associated_Node
10878 (Name (Prefix (N)), Name (Prefix (N2)));
10881 Set_Associated_Node (N, Empty);
10882 Set_Etype (N, Empty);
10885 -- The subtype mark of a nominally unconstrained object
10886 -- is rewritten as a subtype indication using the bounds
10887 -- of the expression. Recover the original subtype mark.
10889 elsif Nkind (N2) = N_Subtype_Indication
10890 and then Is_Entity_Name (Original_Node (N2))
10892 Set_Associated_Node (N, Original_Node (N2));
10900 elsif Nkind (N) in N_Entity then
10905 Loc : constant Source_Ptr := Sloc (N);
10906 Qual : Node_Id := Empty;
10907 Typ : Entity_Id := Empty;
10910 use Atree.Unchecked_Access;
10911 -- This code section is part of implementing an untyped tree
10912 -- traversal, so it needs direct access to node fields.
10915 if Nkind (N) = N_Aggregate
10917 Nkind (N) = N_Extension_Aggregate
10919 N2 := Get_Associated_Node (N);
10926 -- In an instance within a generic, use the name of
10927 -- the actual and not the original generic parameter.
10928 -- If the actual is global in the current generic it
10929 -- must be preserved for its instantiation.
10931 if Nkind (Parent (Typ)) = N_Subtype_Declaration
10933 Present (Generic_Parent_Type (Parent (Typ)))
10935 Typ := Base_Type (Typ);
10936 Set_Etype (N2, Typ);
10942 or else not Is_Global (Typ)
10944 Set_Associated_Node (N, Empty);
10946 -- If the aggregate is an actual in a call, it has been
10947 -- resolved in the current context, to some local type.
10948 -- The enclosing call may have been disambiguated by
10949 -- the aggregate, and this disambiguation might fail at
10950 -- instantiation time because the type to which the
10951 -- aggregate did resolve is not preserved. In order to
10952 -- preserve some of this information, we wrap the
10953 -- aggregate in a qualified expression, using the id of
10954 -- its type. For further disambiguation we qualify the
10955 -- type name with its scope (if visible) because both
10956 -- id's will have corresponding entities in an instance.
10957 -- This resolves most of the problems with missing type
10958 -- information on aggregates in instances.
10960 if Nkind (N2) = Nkind (N)
10962 (Nkind (Parent (N2)) = N_Procedure_Call_Statement
10963 or else Nkind (Parent (N2)) = N_Function_Call)
10964 and then Comes_From_Source (Typ)
10966 if Is_Immediately_Visible (Scope (Typ)) then
10967 Nam := Make_Selected_Component (Loc,
10969 Make_Identifier (Loc, Chars (Scope (Typ))),
10971 Make_Identifier (Loc, Chars (Typ)));
10973 Nam := Make_Identifier (Loc, Chars (Typ));
10977 Make_Qualified_Expression (Loc,
10978 Subtype_Mark => Nam,
10979 Expression => Relocate_Node (N));
10983 Save_Global_Descendant (Field1 (N));
10984 Save_Global_Descendant (Field2 (N));
10985 Save_Global_Descendant (Field3 (N));
10986 Save_Global_Descendant (Field5 (N));
10988 if Present (Qual) then
10992 -- All other cases than aggregates
10995 Save_Global_Descendant (Field1 (N));
10996 Save_Global_Descendant (Field2 (N));
10997 Save_Global_Descendant (Field3 (N));
10998 Save_Global_Descendant (Field4 (N));
10999 Save_Global_Descendant (Field5 (N));
11003 end Save_References;
11005 -- Start of processing for Save_Global_References
11008 Gen_Scope := Current_Scope;
11010 -- If the generic unit is a child unit, references to entities in
11011 -- the parent are treated as local, because they will be resolved
11012 -- anew in the context of the instance of the parent.
11014 while Is_Child_Unit (Gen_Scope)
11015 and then Ekind (Scope (Gen_Scope)) = E_Generic_Package
11017 Gen_Scope := Scope (Gen_Scope);
11020 Save_References (N);
11021 end Save_Global_References;
11023 --------------------------------------
11024 -- Set_Copied_Sloc_For_Inlined_Body --
11025 --------------------------------------
11027 procedure Set_Copied_Sloc_For_Inlined_Body (N : Node_Id; E : Entity_Id) is
11029 Create_Instantiation_Source (N, E, True, S_Adjustment);
11030 end Set_Copied_Sloc_For_Inlined_Body;
11032 ---------------------
11033 -- Set_Instance_Of --
11034 ---------------------
11036 procedure Set_Instance_Of (A : Entity_Id; B : Entity_Id) is
11038 Generic_Renamings.Table (Generic_Renamings.Last) := (A, B, Assoc_Null);
11039 Generic_Renamings_HTable.Set (Generic_Renamings.Last);
11040 Generic_Renamings.Increment_Last;
11041 end Set_Instance_Of;
11043 --------------------
11044 -- Set_Next_Assoc --
11045 --------------------
11047 procedure Set_Next_Assoc (E : Assoc_Ptr; Next : Assoc_Ptr) is
11049 Generic_Renamings.Table (E).Next_In_HTable := Next;
11050 end Set_Next_Assoc;
11052 -------------------
11053 -- Start_Generic --
11054 -------------------
11056 procedure Start_Generic is
11058 -- ??? I am sure more things could be factored out in this
11059 -- routine. Should probably be done at a later stage.
11061 Generic_Flags.Increment_Last;
11062 Generic_Flags.Table (Generic_Flags.Last) := Inside_A_Generic;
11063 Inside_A_Generic := True;
11065 Expander_Mode_Save_And_Set (False);
11068 ----------------------
11069 -- Set_Instance_Env --
11070 ----------------------
11072 procedure Set_Instance_Env
11073 (Gen_Unit : Entity_Id;
11074 Act_Unit : Entity_Id)
11077 -- Regardless of the current mode, predefined units are analyzed in
11078 -- the most current Ada mode, and earlier version Ada checks do not
11079 -- apply to predefined units.
11081 -- Why is this not using the routine Opt.Set_Opt_Config_Switches ???
11083 if Is_Internal_File_Name
11084 (Fname => Unit_File_Name (Get_Source_Unit (Gen_Unit)),
11085 Renamings_Included => True) then
11086 Ada_Version := Ada_Version_Type'Last;
11089 Current_Instantiated_Parent := (Gen_Unit, Act_Unit, Assoc_Null);
11090 end Set_Instance_Env;
11096 procedure Switch_View (T : Entity_Id) is
11097 BT : constant Entity_Id := Base_Type (T);
11098 Priv_Elmt : Elmt_Id := No_Elmt;
11099 Priv_Sub : Entity_Id;
11102 -- T may be private but its base type may have been exchanged through
11103 -- some other occurrence, in which case there is nothing to switch
11104 -- besides T itself. Note that a private dependent subtype of a private
11105 -- type might not have been switched even if the base type has been,
11106 -- because of the last branch of Check_Private_View (see comment there).
11108 if not Is_Private_Type (BT) then
11109 Prepend_Elmt (Full_View (T), Exchanged_Views);
11110 Exchange_Declarations (T);
11114 Priv_Elmt := First_Elmt (Private_Dependents (BT));
11116 if Present (Full_View (BT)) then
11117 Prepend_Elmt (Full_View (BT), Exchanged_Views);
11118 Exchange_Declarations (BT);
11121 while Present (Priv_Elmt) loop
11122 Priv_Sub := (Node (Priv_Elmt));
11124 -- We avoid flipping the subtype if the Etype of its full
11125 -- view is private because this would result in a malformed
11126 -- subtype. This occurs when the Etype of the subtype full
11127 -- view is the full view of the base type (and since the
11128 -- base types were just switched, the subtype is pointing
11129 -- to the wrong view). This is currently the case for
11130 -- tagged record types, access types (maybe more?) and
11131 -- needs to be resolved. ???
11133 if Present (Full_View (Priv_Sub))
11134 and then not Is_Private_Type (Etype (Full_View (Priv_Sub)))
11136 Prepend_Elmt (Full_View (Priv_Sub), Exchanged_Views);
11137 Exchange_Declarations (Priv_Sub);
11140 Next_Elmt (Priv_Elmt);
11144 -----------------------------
11145 -- Valid_Default_Attribute --
11146 -----------------------------
11148 procedure Valid_Default_Attribute (Nam : Entity_Id; Def : Node_Id) is
11149 Attr_Id : constant Attribute_Id :=
11150 Get_Attribute_Id (Attribute_Name (Def));
11151 T : constant Entity_Id := Entity (Prefix (Def));
11152 Is_Fun : constant Boolean := (Ekind (Nam) = E_Function);
11165 F := First_Formal (Nam);
11166 while Present (F) loop
11167 Num_F := Num_F + 1;
11172 when Attribute_Adjacent | Attribute_Ceiling | Attribute_Copy_Sign |
11173 Attribute_Floor | Attribute_Fraction | Attribute_Machine |
11174 Attribute_Model | Attribute_Remainder | Attribute_Rounding |
11175 Attribute_Unbiased_Rounding =>
11178 and then Is_Floating_Point_Type (T);
11180 when Attribute_Image | Attribute_Pred | Attribute_Succ |
11181 Attribute_Value | Attribute_Wide_Image |
11182 Attribute_Wide_Value =>
11183 OK := (Is_Fun and then Num_F = 1 and then Is_Scalar_Type (T));
11185 when Attribute_Max | Attribute_Min =>
11186 OK := (Is_Fun and then Num_F = 2 and then Is_Scalar_Type (T));
11188 when Attribute_Input =>
11189 OK := (Is_Fun and then Num_F = 1);
11191 when Attribute_Output | Attribute_Read | Attribute_Write =>
11192 OK := (not Is_Fun and then Num_F = 2);
11199 Error_Msg_N ("attribute reference has wrong profile for subprogram",
11202 end Valid_Default_Attribute;