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
84 -- share generic instantiations (for now). Analysis of a generic definition
85 -- does not perform any expansion action, but the expander must be called
86 -- on the tree for each instantiation, because the expansion may of course
87 -- depend 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 a
225 -- static error which must be detected at compile time. The detection
226 -- of these circularities is carried out at the point that we insert
227 -- a generic instance spec or body. If there is a circularity, then
228 -- the analysis of the offending spec or body will eventually result
229 -- in trying to load the same unit again, and we detect this problem
230 -- as we analyze the package instantiation for the second time.
232 -- At least in some cases after we have detected the circularity, we
233 -- get into trouble if we try to keep going. The following flag is
234 -- set if a circularity is detected, and used to abandon compilation
235 -- after the 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 -- Local subprograms --
243 -----------------------
245 procedure Abandon_Instantiation
(N
: Node_Id
);
246 pragma No_Return
(Abandon_Instantiation
);
247 -- Posts an error message "instantiation abandoned" at the indicated
248 -- node and then raises the exception Instantiation_Error to do it.
250 procedure Analyze_Formal_Array_Type
251 (T
: in out Entity_Id
;
253 -- A formal array type is treated like an array type declaration, and
254 -- invokes Array_Type_Declaration (sem_ch3) whose first parameter is
255 -- in-out, because in the case of an anonymous type the entity is
256 -- actually created in the procedure.
258 -- The following procedures treat other kinds of formal parameters
260 procedure Analyze_Formal_Derived_Interface_Type
264 procedure Analyze_Formal_Derived_Type
269 -- The following subprograms create abbreviated declarations for formal
270 -- scalar types. We introduce an anonymous base of the proper class for
271 -- each of them, and define the formals as constrained first subtypes of
272 -- their bases. The bounds are expressions that are non-static in the
275 procedure Analyze_Formal_Decimal_Fixed_Point_Type
276 (T
: Entity_Id
; Def
: Node_Id
);
277 procedure Analyze_Formal_Discrete_Type
(T
: Entity_Id
; Def
: Node_Id
);
278 procedure Analyze_Formal_Floating_Type
(T
: Entity_Id
; Def
: Node_Id
);
279 procedure Analyze_Formal_Interface_Type
(T
: Entity_Id
; Def
: Node_Id
);
280 procedure Analyze_Formal_Signed_Integer_Type
(T
: Entity_Id
; Def
: Node_Id
);
281 procedure Analyze_Formal_Modular_Type
(T
: Entity_Id
; Def
: Node_Id
);
282 procedure Analyze_Formal_Ordinary_Fixed_Point_Type
283 (T
: Entity_Id
; Def
: Node_Id
);
285 procedure Analyze_Formal_Private_Type
289 -- This needs comments???
291 procedure Analyze_Generic_Formal_Part
(N
: Node_Id
);
293 procedure Analyze_Generic_Access_Type
(T
: Entity_Id
; Def
: Node_Id
);
294 -- This needs comments ???
296 function Analyze_Associations
299 F_Copy
: List_Id
) return List_Id
;
300 -- At instantiation time, build the list of associations between formals
301 -- and actuals. Each association becomes a renaming declaration for the
302 -- formal entity. F_Copy is the analyzed list of formals in the generic
303 -- copy. It is used to apply legality checks to the actuals. I_Node is the
304 -- instantiation node itself.
306 procedure Analyze_Subprogram_Instantiation
310 procedure Build_Instance_Compilation_Unit_Nodes
314 -- This procedure is used in the case where the generic instance of a
315 -- subprogram body or package body is a library unit. In this case, the
316 -- original library unit node for the generic instantiation must be
317 -- replaced by the resulting generic body, and a link made to a new
318 -- compilation unit node for the generic declaration. The argument N is
319 -- the original generic instantiation. Act_Body and Act_Decl are the body
320 -- and declaration of the instance (either package body and declaration
321 -- nodes or subprogram body and declaration nodes depending on the case).
322 -- On return, the node N has been rewritten with the actual body.
324 procedure Check_Formal_Packages
(P_Id
: Entity_Id
);
325 -- Apply the following to all formal packages in generic associations
327 procedure Check_Formal_Package_Instance
328 (Formal_Pack
: Entity_Id
;
329 Actual_Pack
: Entity_Id
);
330 -- Verify that the actuals of the actual instance match the actuals of
331 -- the template for a formal package that is not declared with a box.
333 procedure Check_Forward_Instantiation
(Decl
: Node_Id
);
334 -- If the generic is a local entity and the corresponding body has not
335 -- been seen yet, flag enclosing packages to indicate that it will be
336 -- elaborated after the generic body. Subprograms declared in the same
337 -- package cannot be inlined by the front-end because front-end inlining
338 -- requires a strict linear order of elaboration.
340 procedure Check_Hidden_Child_Unit
342 Gen_Unit
: Entity_Id
;
343 Act_Decl_Id
: Entity_Id
);
344 -- If the generic unit is an implicit child instance within a parent
345 -- instance, we need to make an explicit test that it is not hidden by
346 -- a child instance of the same name and parent.
348 procedure Check_Private_View
(N
: Node_Id
);
349 -- Check whether the type of a generic entity has a different view between
350 -- the point of generic analysis and the point of instantiation. If the
351 -- view has changed, then at the point of instantiation we restore the
352 -- correct view to perform semantic analysis of the instance, and reset
353 -- the current view after instantiation. The processing is driven by the
354 -- current private status of the type of the node, and Has_Private_View,
355 -- a flag that is set at the point of generic compilation. If view and
356 -- flag are inconsistent then the type is updated appropriately.
358 procedure Check_Generic_Actuals
359 (Instance
: Entity_Id
;
360 Is_Formal_Box
: Boolean);
361 -- Similar to previous one. Check the actuals in the instantiation,
362 -- whose views can change between the point of instantiation and the point
363 -- of instantiation of the body. In addition, mark the generic renamings
364 -- as generic actuals, so that they are not compatible with other actuals.
365 -- Recurse on an actual that is a formal package whose declaration has
368 function Contains_Instance_Of
371 N
: Node_Id
) return Boolean;
372 -- Inner is instantiated within the generic Outer. Check whether Inner
373 -- directly or indirectly contains an instance of Outer or of one of its
374 -- parents, in the case of a subunit. Each generic unit holds a list of
375 -- the entities instantiated within (at any depth). This procedure
376 -- determines whether the set of such lists contains a cycle, i.e. an
377 -- illegal circular instantiation.
379 function Denotes_Formal_Package
381 On_Exit
: Boolean := False) return Boolean;
382 -- Returns True if E is a formal package of an enclosing generic, or
383 -- the actual for such a formal in an enclosing instantiation. If such
384 -- a package is used as a formal in an nested generic, or as an actual
385 -- in a nested instantiation, the visibility of ITS formals should not
386 -- be modified. When called from within Restore_Private_Views, the flag
387 -- On_Exit is true, to indicate that the search for a possible enclosing
388 -- instance should ignore the current one.
390 function Find_Actual_Type
392 Gen_Scope
: Entity_Id
) return Entity_Id
;
393 -- When validating the actual types of a child instance, check whether
394 -- the formal is a formal type of the parent unit, and retrieve the current
395 -- actual for it. Typ is the entity in the analyzed formal type declaration
396 -- (component or index type of an array type) and Gen_Scope is the scope of
397 -- the analyzed formal array type.
399 function In_Same_Declarative_Part
401 Inst
: Node_Id
) return Boolean;
402 -- True if the instantiation Inst and the given freeze_node F_Node appear
403 -- within the same declarative part, ignoring subunits, but with no inter-
404 -- vening suprograms or concurrent units. If true, the freeze node
405 -- of the instance can be placed after the freeze node of the parent,
406 -- which it itself an instance.
408 function In_Main_Context
(E
: Entity_Id
) return Boolean;
409 -- Check whether an instantiation is in the context of the main unit.
410 -- Used to determine whether its body should be elaborated to allow
411 -- front-end inlining.
413 procedure Set_Instance_Env
414 (Gen_Unit
: Entity_Id
;
415 Act_Unit
: Entity_Id
);
416 -- Save current instance on saved environment, to be used to determine
417 -- the global status of entities in nested instances. Part of Save_Env.
418 -- called after verifying that the generic unit is legal for the instance.
420 procedure Set_Instance_Of
(A
: Entity_Id
; B
: Entity_Id
);
421 -- Associate analyzed generic parameter with corresponding
422 -- instance. Used for semantic checks at instantiation time.
424 function Has_Been_Exchanged
(E
: Entity_Id
) return Boolean;
425 -- Traverse the Exchanged_Views list to see if a type was private
426 -- and has already been flipped during this phase of instantiation.
428 procedure Hide_Current_Scope
;
429 -- When compiling a generic child unit, the parent context must be
430 -- present, but the instance and all entities that may be generated
431 -- must be inserted in the current scope. We leave the current scope
432 -- on the stack, but make its entities invisible to avoid visibility
433 -- problems. This is reversed at the end of instantiations. This is
434 -- not done for the instantiation of the bodies, which only require the
435 -- instances of the generic parents to be in scope.
437 procedure Install_Body
442 -- If the instantiation happens textually before the body of the generic,
443 -- the instantiation of the body must be analyzed after the generic body,
444 -- and not at the point of instantiation. Such early instantiations can
445 -- happen if the generic and the instance appear in a package declaration
446 -- because the generic body can only appear in the corresponding package
447 -- body. Early instantiations can also appear if generic, instance and
448 -- body are all in the declarative part of a subprogram or entry. Entities
449 -- of packages that are early instantiations are delayed, and their freeze
450 -- node appears after the generic body.
452 procedure Insert_After_Last_Decl
(N
: Node_Id
; F_Node
: Node_Id
);
453 -- Insert freeze node at the end of the declarative part that includes the
454 -- instance node N. If N is in the visible part of an enclosing package
455 -- declaration, the freeze node has to be inserted at the end of the
456 -- private declarations, if any.
458 procedure Freeze_Subprogram_Body
459 (Inst_Node
: Node_Id
;
461 Pack_Id
: Entity_Id
);
462 -- The generic body may appear textually after the instance, including
463 -- in the proper body of a stub, or within a different package instance.
464 -- Given that the instance can only be elaborated after the generic, we
465 -- place freeze_nodes for the instance and/or for packages that may enclose
466 -- the instance and the generic, so that the back-end can establish the
467 -- proper order of elaboration.
470 -- Establish environment for subsequent instantiation. Separated from
471 -- Save_Env because data-structures for visibility handling must be
472 -- initialized before call to Check_Generic_Child_Unit.
474 procedure Install_Parent
(P
: Entity_Id
; In_Body
: Boolean := False);
475 -- When compiling an instance of a child unit the parent (which is
476 -- itself an instance) is an enclosing scope that must be made
477 -- immediately visible. This procedure is also used to install the non-
478 -- generic parent of a generic child unit when compiling its body, so
479 -- that full views of types in the parent are made visible.
481 procedure Remove_Parent
(In_Body
: Boolean := False);
482 -- Reverse effect after instantiation of child is complete
484 procedure Inline_Instance_Body
486 Gen_Unit
: Entity_Id
;
488 -- If front-end inlining is requested, instantiate the package body,
489 -- and preserve the visibility of its compilation unit, to insure
490 -- that successive instantiations succeed.
492 -- The functions Instantiate_XXX perform various legality checks and build
493 -- the declarations for instantiated generic parameters. In all of these
494 -- Formal is the entity in the generic unit, Actual is the entity of
495 -- expression in the generic associations, and Analyzed_Formal is the
496 -- formal in the generic copy, which contains the semantic information to
497 -- be used to validate the actual.
499 function Instantiate_Object
502 Analyzed_Formal
: Node_Id
) return List_Id
;
504 function Instantiate_Type
507 Analyzed_Formal
: Node_Id
;
508 Actual_Decls
: List_Id
) return Node_Id
;
510 function Instantiate_Formal_Subprogram
513 Analyzed_Formal
: Node_Id
) return Node_Id
;
515 function Instantiate_Formal_Package
518 Analyzed_Formal
: Node_Id
) return List_Id
;
519 -- If the formal package is declared with a box, special visibility rules
520 -- apply to its formals: they are in the visible part of the package. This
521 -- is true in the declarative region of the formal package, that is to say
522 -- in the enclosing generic or instantiation. For an instantiation, the
523 -- parameters of the formal package are made visible in an explicit step.
524 -- Furthermore, if the actual is a visible use_clause, these formals must
525 -- be made potentially use_visible as well. On exit from the enclosing
526 -- instantiation, the reverse must be done.
528 -- For a formal package declared without a box, there are conformance rules
529 -- that apply to the actuals in the generic declaration and the actuals of
530 -- the actual package in the enclosing instantiation. The simplest way to
531 -- apply these rules is to repeat the instantiation of the formal package
532 -- in the context of the enclosing instance, and compare the generic
533 -- associations of this instantiation with those of the actual package.
535 function Is_In_Main_Unit
(N
: Node_Id
) return Boolean;
536 -- Test if given node is in the main unit
538 procedure Load_Parent_Of_Generic
(N
: Node_Id
; Spec
: Node_Id
);
539 -- If the generic appears in a separate non-generic library unit,
540 -- load the corresponding body to retrieve the body of the generic.
541 -- N is the node for the generic instantiation, Spec is the generic
542 -- package declaration.
544 procedure Inherit_Context
(Gen_Decl
: Node_Id
; Inst
: Node_Id
);
545 -- Add the context clause of the unit containing a generic unit to
546 -- an instantiation that is a compilation unit.
548 function Get_Associated_Node
(N
: Node_Id
) return Node_Id
;
549 -- In order to propagate semantic information back from the analyzed
550 -- copy to the original generic, we maintain links between selected nodes
551 -- in the generic and their corresponding copies. At the end of generic
552 -- analysis, the routine Save_Global_References traverses the generic
553 -- tree, examines the semantic information, and preserves the links to
554 -- those nodes that contain global information. At instantiation, the
555 -- information from the associated node is placed on the new copy, so
556 -- that name resolution is not repeated.
558 -- Three kinds of source nodes have associated nodes:
560 -- a) those that can reference (denote) entities, that is identifiers,
561 -- character literals, expanded_names, operator symbols, operators,
562 -- and attribute reference nodes. These nodes have an Entity field
563 -- and are the set of nodes that are in N_Has_Entity.
565 -- b) aggregates (N_Aggregate and N_Extension_Aggregate)
567 -- c) selected components (N_Selected_Component)
569 -- For the first class, the associated node preserves the entity if it is
570 -- global. If the generic contains nested instantiations, the associated
571 -- node itself has been recopied, and a chain of them must be followed.
573 -- For aggregates, the associated node allows retrieval of the type, which
574 -- may otherwise not appear in the generic. The view of this type may be
575 -- different between generic and instantiation, and the full view can be
576 -- installed before the instantiation is analyzed. For aggregates of
577 -- type extensions, the same view exchange may have to be performed for
578 -- some of the ancestor types, if their view is private at the point of
581 -- Nodes that are selected components in the parse tree may be rewritten
582 -- as expanded names after resolution, and must be treated as potential
583 -- entity holders. which is why they also have an Associated_Node.
585 -- Nodes that do not come from source, such as freeze nodes, do not appear
586 -- in the generic tree, and need not have an associated node.
588 -- The associated node is stored in the Associated_Node field. Note that
589 -- this field overlaps Entity, which is fine, because the whole point is
590 -- that we don't need or want the normal Entity field in this situation.
592 procedure Move_Freeze_Nodes
596 -- Freeze nodes can be generated in the analysis of a generic unit, but
597 -- will not be seen by the back-end. It is necessary to move those nodes
598 -- to the enclosing scope if they freeze an outer entity. We place them
599 -- at the end of the enclosing generic package, which is semantically
602 procedure Pre_Analyze_Actuals
(N
: Node_Id
);
603 -- Analyze actuals to perform name resolution. Full resolution is done
604 -- later, when the expected types are known, but names have to be captured
605 -- before installing parents of generics, that are not visible for the
606 -- actuals themselves.
608 procedure Valid_Default_Attribute
(Nam
: Entity_Id
; Def
: Node_Id
);
609 -- Verify that an attribute that appears as the default for a formal
610 -- subprogram is a function or procedure with the correct profile.
612 -------------------------------------------
613 -- Data Structures for Generic Renamings --
614 -------------------------------------------
616 -- The map Generic_Renamings associates generic entities with their
617 -- corresponding actuals. Currently used to validate type instances.
618 -- It will eventually be used for all generic parameters to eliminate
619 -- the need for overload resolution in the instance.
621 type Assoc_Ptr
is new Int
;
623 Assoc_Null
: constant Assoc_Ptr
:= -1;
628 Next_In_HTable
: Assoc_Ptr
;
631 package Generic_Renamings
is new Table
.Table
632 (Table_Component_Type
=> Assoc
,
633 Table_Index_Type
=> Assoc_Ptr
,
634 Table_Low_Bound
=> 0,
636 Table_Increment
=> 100,
637 Table_Name
=> "Generic_Renamings");
639 -- Variable to hold enclosing instantiation. When the environment is
640 -- saved for a subprogram inlining, the corresponding Act_Id is empty.
642 Current_Instantiated_Parent
: Assoc
:= (Empty
, Empty
, Assoc_Null
);
644 -- Hash table for associations
646 HTable_Size
: constant := 37;
647 type HTable_Range
is range 0 .. HTable_Size
- 1;
649 procedure Set_Next_Assoc
(E
: Assoc_Ptr
; Next
: Assoc_Ptr
);
650 function Next_Assoc
(E
: Assoc_Ptr
) return Assoc_Ptr
;
651 function Get_Gen_Id
(E
: Assoc_Ptr
) return Entity_Id
;
652 function Hash
(F
: Entity_Id
) return HTable_Range
;
654 package Generic_Renamings_HTable
is new GNAT
.HTable
.Static_HTable
(
655 Header_Num
=> HTable_Range
,
657 Elmt_Ptr
=> Assoc_Ptr
,
658 Null_Ptr
=> Assoc_Null
,
659 Set_Next
=> Set_Next_Assoc
,
662 Get_Key
=> Get_Gen_Id
,
666 Exchanged_Views
: Elist_Id
;
667 -- This list holds the private views that have been exchanged during
668 -- instantiation to restore the visibility of the generic declaration.
669 -- (see comments above). After instantiation, the current visibility is
670 -- reestablished by means of a traversal of this list.
672 Hidden_Entities
: Elist_Id
;
673 -- This list holds the entities of the current scope that are removed
674 -- from immediate visibility when instantiating a child unit. Their
675 -- visibility is restored in Remove_Parent.
677 -- Because instantiations can be recursive, the following must be saved
678 -- on entry and restored on exit from an instantiation (spec or body).
679 -- This is done by the two procedures Save_Env and Restore_Env. For
680 -- package and subprogram instantiations (but not for the body instances)
681 -- the action of Save_Env is done in two steps: Init_Env is called before
682 -- Check_Generic_Child_Unit, because setting the parent instances requires
683 -- that the visibility data structures be properly initialized. Once the
684 -- generic is unit is validated, Set_Instance_Env completes Save_Env.
686 Parent_Unit_Visible
: Boolean := False;
687 -- Parent_Unit_Visible is used when the generic is a child unit, and
688 -- indicates whether the ultimate parent of the generic is visible in the
689 -- instantiation environment. It is used to reset the visibility of the
690 -- parent at the end of the instantiation (see Remove_Parent).
692 Instance_Parent_Unit
: Entity_Id
:= Empty
;
693 -- This records the ultimate parent unit of an instance of a generic
694 -- child unit and is used in conjunction with Parent_Unit_Visible to
695 -- indicate the unit to which the Parent_Unit_Visible flag corresponds.
697 type Instance_Env
is record
698 Ada_Version
: Ada_Version_Type
;
699 Ada_Version_Explicit
: Ada_Version_Type
;
700 Instantiated_Parent
: Assoc
;
701 Exchanged_Views
: Elist_Id
;
702 Hidden_Entities
: Elist_Id
;
703 Current_Sem_Unit
: Unit_Number_Type
;
704 Parent_Unit_Visible
: Boolean := False;
705 Instance_Parent_Unit
: Entity_Id
:= Empty
;
708 package Instance_Envs
is new Table
.Table
(
709 Table_Component_Type
=> Instance_Env
,
710 Table_Index_Type
=> Int
,
711 Table_Low_Bound
=> 0,
713 Table_Increment
=> 100,
714 Table_Name
=> "Instance_Envs");
716 procedure Restore_Private_Views
717 (Pack_Id
: Entity_Id
;
718 Is_Package
: Boolean := True);
719 -- Restore the private views of external types, and unmark the generic
720 -- renamings of actuals, so that they become comptible subtypes again.
721 -- For subprograms, Pack_Id is the package constructed to hold the
724 procedure Switch_View
(T
: Entity_Id
);
725 -- Switch the partial and full views of a type and its private
726 -- dependents (i.e. its subtypes and derived types).
728 ------------------------------------
729 -- Structures for Error Reporting --
730 ------------------------------------
732 Instantiation_Node
: Node_Id
;
733 -- Used by subprograms that validate instantiation of formal parameters
734 -- where there might be no actual on which to place the error message.
735 -- Also used to locate the instantiation node for generic subunits.
737 Instantiation_Error
: exception;
738 -- When there is a semantic error in the generic parameter matching,
739 -- there is no point in continuing the instantiation, because the
740 -- number of cascaded errors is unpredictable. This exception aborts
741 -- the instantiation process altogether.
743 S_Adjustment
: Sloc_Adjustment
;
744 -- Offset created for each node in an instantiation, in order to keep
745 -- track of the source position of the instantiation in each of its nodes.
746 -- A subsequent semantic error or warning on a construct of the instance
747 -- points to both places: the original generic node, and the point of
748 -- instantiation. See Sinput and Sinput.L for additional details.
750 ------------------------------------------------------------
751 -- Data structure for keeping track when inside a Generic --
752 ------------------------------------------------------------
754 -- The following table is used to save values of the Inside_A_Generic
755 -- flag (see spec of Sem) when they are saved by Start_Generic.
757 package Generic_Flags
is new Table
.Table
(
758 Table_Component_Type
=> Boolean,
759 Table_Index_Type
=> Int
,
760 Table_Low_Bound
=> 0,
762 Table_Increment
=> 200,
763 Table_Name
=> "Generic_Flags");
765 ---------------------------
766 -- Abandon_Instantiation --
767 ---------------------------
769 procedure Abandon_Instantiation
(N
: Node_Id
) is
771 Error_Msg_N
("instantiation abandoned!", N
);
772 raise Instantiation_Error
;
773 end Abandon_Instantiation
;
775 --------------------------
776 -- Analyze_Associations --
777 --------------------------
779 function Analyze_Associations
782 F_Copy
: List_Id
) return List_Id
784 Actual_Types
: constant Elist_Id
:= New_Elmt_List
;
785 Assoc
: constant List_Id
:= New_List
;
786 Defaults
: constant Elist_Id
:= New_Elmt_List
;
787 Gen_Unit
: constant Entity_Id
:= Defining_Entity
(Parent
(F_Copy
));
791 Next_Formal
: Node_Id
;
792 Temp_Formal
: Node_Id
;
793 Analyzed_Formal
: Node_Id
;
796 First_Named
: Node_Id
:= Empty
;
797 Found_Assoc
: Node_Id
;
798 Is_Named_Assoc
: Boolean;
799 Num_Matched
: Int
:= 0;
800 Num_Actuals
: Int
:= 0;
802 function Matching_Actual
804 A_F
: Entity_Id
) return Node_Id
;
805 -- Find actual that corresponds to a given a formal parameter. If the
806 -- actuals are positional, return the next one, if any. If the actuals
807 -- are named, scan the parameter associations to find the right one.
808 -- A_F is the corresponding entity in the analyzed generic,which is
809 -- placed on the selector name for ASIS use.
811 procedure Set_Analyzed_Formal
;
812 -- Find the node in the generic copy that corresponds to a given formal.
813 -- The semantic information on this node is used to perform legality
814 -- checks on the actuals. Because semantic analysis can introduce some
815 -- anonymous entities or modify the declaration node itself, the
816 -- correspondence between the two lists is not one-one. In addition to
817 -- anonymous types, the presence a formal equality will introduce an
818 -- implicit declaration for the corresponding inequality.
820 ---------------------
821 -- Matching_Actual --
822 ---------------------
824 function Matching_Actual
826 A_F
: Entity_Id
) return Node_Id
832 Is_Named_Assoc
:= False;
834 -- End of list of purely positional parameters
839 -- Case of positional parameter corresponding to current formal
841 elsif No
(Selector_Name
(Actual
)) then
842 Found
:= Explicit_Generic_Actual_Parameter
(Actual
);
843 Found_Assoc
:= Actual
;
844 Num_Matched
:= Num_Matched
+ 1;
847 -- Otherwise scan list of named actuals to find the one with the
848 -- desired name. All remaining actuals have explicit names.
851 Is_Named_Assoc
:= True;
855 while Present
(Actual
) loop
856 if Chars
(Selector_Name
(Actual
)) = Chars
(F
) then
857 Found
:= Explicit_Generic_Actual_Parameter
(Actual
);
858 Set_Entity
(Selector_Name
(Actual
), A_F
);
859 Set_Etype
(Selector_Name
(Actual
), Etype
(A_F
));
860 Generate_Reference
(A_F
, Selector_Name
(Actual
));
861 Found_Assoc
:= Actual
;
862 Num_Matched
:= Num_Matched
+ 1;
870 -- Reset for subsequent searches. In most cases the named
871 -- associations are in order. If they are not, we reorder them
872 -- to avoid scanning twice the same actual. This is not just a
873 -- question of efficiency: there may be multiple defaults with
874 -- boxes that have the same name. In a nested instantiation we
875 -- insert actuals for those defaults, and cannot rely on their
876 -- names to disambiguate them.
878 if Actual
= First_Named
then
881 elsif Present
(Actual
) then
882 Insert_Before
(First_Named
, Remove_Next
(Prev
));
885 Actual
:= First_Named
;
891 -------------------------
892 -- Set_Analyzed_Formal --
893 -------------------------
895 procedure Set_Analyzed_Formal
is
898 while Present
(Analyzed_Formal
) loop
899 Kind
:= Nkind
(Analyzed_Formal
);
901 case Nkind
(Formal
) is
903 when N_Formal_Subprogram_Declaration
=>
904 exit when Kind
in N_Formal_Subprogram_Declaration
907 (Defining_Unit_Name
(Specification
(Formal
))) =
909 (Defining_Unit_Name
(Specification
(Analyzed_Formal
)));
911 when N_Formal_Package_Declaration
=>
913 Kind
= N_Formal_Package_Declaration
915 Kind
= N_Generic_Package_Declaration
;
917 when N_Use_Package_Clause | N_Use_Type_Clause
=> exit;
921 -- Skip freeze nodes, and nodes inserted to replace
922 -- unrecognized pragmas.
925 Kind
not in N_Formal_Subprogram_Declaration
926 and then Kind
/= N_Subprogram_Declaration
927 and then Kind
/= N_Freeze_Entity
928 and then Kind
/= N_Null_Statement
929 and then Kind
/= N_Itype_Reference
930 and then Chars
(Defining_Identifier
(Formal
)) =
931 Chars
(Defining_Identifier
(Analyzed_Formal
));
934 Next
(Analyzed_Formal
);
937 end Set_Analyzed_Formal
;
939 -- Start of processing for Analyze_Associations
942 -- If named associations are present, save the first named association
943 -- (it may of course be Empty) to facilitate subsequent name search.
945 Actuals
:= Generic_Associations
(I_Node
);
947 if Present
(Actuals
) then
948 First_Named
:= First
(Actuals
);
950 while Present
(First_Named
)
951 and then No
(Selector_Name
(First_Named
))
953 Num_Actuals
:= Num_Actuals
+ 1;
958 Named
:= First_Named
;
959 while Present
(Named
) loop
960 if No
(Selector_Name
(Named
)) then
961 Error_Msg_N
("invalid positional actual after named one", Named
);
962 Abandon_Instantiation
(Named
);
965 -- A named association may lack an actual parameter, if it was
966 -- introduced for a default subprogram that turns out to be local
967 -- to the outer instantiation.
969 if Present
(Explicit_Generic_Actual_Parameter
(Named
)) then
970 Num_Actuals
:= Num_Actuals
+ 1;
976 if Present
(Formals
) then
977 Formal
:= First_Non_Pragma
(Formals
);
978 Analyzed_Formal
:= First_Non_Pragma
(F_Copy
);
980 if Present
(Actuals
) then
981 Actual
:= First
(Actuals
);
983 -- All formals should have default values
989 while Present
(Formal
) loop
991 Next_Formal
:= Next_Non_Pragma
(Formal
);
993 case Nkind
(Formal
) is
994 when N_Formal_Object_Declaration
=>
997 Defining_Identifier
(Formal
),
998 Defining_Identifier
(Analyzed_Formal
));
1001 (Instantiate_Object
(Formal
, Match
, Analyzed_Formal
),
1004 when N_Formal_Type_Declaration
=>
1007 Defining_Identifier
(Formal
),
1008 Defining_Identifier
(Analyzed_Formal
));
1011 Error_Msg_Sloc
:= Sloc
(Gen_Unit
);
1014 Instantiation_Node
, Defining_Identifier
(Formal
));
1015 Error_Msg_NE
("\in instantiation of & declared#",
1016 Instantiation_Node
, Gen_Unit
);
1017 Abandon_Instantiation
(Instantiation_Node
);
1023 (Formal
, Match
, Analyzed_Formal
, Assoc
));
1025 -- An instantiation is a freeze point for the actuals,
1026 -- unless this is a rewritten formal package.
1028 if Nkind
(I_Node
) /= N_Formal_Package_Declaration
then
1029 Append_Elmt
(Entity
(Match
), Actual_Types
);
1033 -- A remote access-to-class-wide type must not be an
1034 -- actual parameter for a generic formal of an access
1035 -- type (E.2.2 (17)).
1037 if Nkind
(Analyzed_Formal
) = N_Formal_Type_Declaration
1039 Nkind
(Formal_Type_Definition
(Analyzed_Formal
)) =
1040 N_Access_To_Object_Definition
1042 Validate_Remote_Access_To_Class_Wide_Type
(Match
);
1045 when N_Formal_Subprogram_Declaration
=>
1048 Defining_Unit_Name
(Specification
(Formal
)),
1049 Defining_Unit_Name
(Specification
(Analyzed_Formal
)));
1051 -- If the formal subprogram has the same name as
1052 -- another formal subprogram of the generic, then
1053 -- a named association is illegal (12.3(9)). Exclude
1054 -- named associations that are generated for a nested
1058 and then Is_Named_Assoc
1059 and then Comes_From_Source
(Found_Assoc
)
1061 Temp_Formal
:= First
(Formals
);
1062 while Present
(Temp_Formal
) loop
1063 if Nkind
(Temp_Formal
) in
1064 N_Formal_Subprogram_Declaration
1065 and then Temp_Formal
/= Formal
1067 Chars
(Selector_Name
(Found_Assoc
)) =
1068 Chars
(Defining_Unit_Name
1069 (Specification
(Temp_Formal
)))
1072 ("name not allowed for overloaded formal",
1074 Abandon_Instantiation
(Instantiation_Node
);
1082 Instantiate_Formal_Subprogram
1083 (Formal
, Match
, Analyzed_Formal
));
1086 and then Box_Present
(Formal
)
1089 (Defining_Unit_Name
(Specification
(Last
(Assoc
))),
1093 when N_Formal_Package_Declaration
=>
1096 Defining_Identifier
(Formal
),
1097 Defining_Identifier
(Original_Node
(Analyzed_Formal
)));
1100 Error_Msg_Sloc
:= Sloc
(Gen_Unit
);
1103 Instantiation_Node
, Defining_Identifier
(Formal
));
1104 Error_Msg_NE
("\in instantiation of & declared#",
1105 Instantiation_Node
, Gen_Unit
);
1107 Abandon_Instantiation
(Instantiation_Node
);
1112 (Instantiate_Formal_Package
1113 (Formal
, Match
, Analyzed_Formal
),
1117 -- For use type and use package appearing in the context
1118 -- clause, we have already copied them, so we can just
1119 -- move them where they belong (we mustn't recopy them
1120 -- since this would mess up the Sloc values).
1122 when N_Use_Package_Clause |
1123 N_Use_Type_Clause
=>
1125 Append
(Formal
, Assoc
);
1128 raise Program_Error
;
1132 Formal
:= Next_Formal
;
1133 Next_Non_Pragma
(Analyzed_Formal
);
1136 if Num_Actuals
> Num_Matched
then
1137 Error_Msg_Sloc
:= Sloc
(Gen_Unit
);
1139 if Present
(Selector_Name
(Actual
)) then
1141 ("unmatched actual&",
1142 Actual
, Selector_Name
(Actual
));
1143 Error_Msg_NE
("\in instantiation of& declared#",
1147 ("unmatched actual in instantiation of& declared#",
1152 elsif Present
(Actuals
) then
1154 ("too many actuals in generic instantiation", Instantiation_Node
);
1158 Elmt
: Elmt_Id
:= First_Elmt
(Actual_Types
);
1161 while Present
(Elmt
) loop
1162 Freeze_Before
(I_Node
, Node
(Elmt
));
1167 -- If there are default subprograms, normalize the tree by adding
1168 -- explicit associations for them. This is required if the instance
1169 -- appears within a generic.
1177 Elmt
:= First_Elmt
(Defaults
);
1178 while Present
(Elmt
) loop
1179 if No
(Actuals
) then
1180 Actuals
:= New_List
;
1181 Set_Generic_Associations
(I_Node
, Actuals
);
1184 Subp
:= Node
(Elmt
);
1186 Make_Generic_Association
(Sloc
(Subp
),
1187 Selector_Name
=> New_Occurrence_Of
(Subp
, Sloc
(Subp
)),
1188 Explicit_Generic_Actual_Parameter
=>
1189 New_Occurrence_Of
(Subp
, Sloc
(Subp
)));
1190 Mark_Rewrite_Insertion
(New_D
);
1191 Append_To
(Actuals
, New_D
);
1197 end Analyze_Associations
;
1199 -------------------------------
1200 -- Analyze_Formal_Array_Type --
1201 -------------------------------
1203 procedure Analyze_Formal_Array_Type
1204 (T
: in out Entity_Id
;
1210 -- Treated like a non-generic array declaration, with
1211 -- additional semantic checks.
1215 if Nkind
(Def
) = N_Constrained_Array_Definition
then
1216 DSS
:= First
(Discrete_Subtype_Definitions
(Def
));
1217 while Present
(DSS
) loop
1218 if Nkind
(DSS
) = N_Subtype_Indication
1219 or else Nkind
(DSS
) = N_Range
1220 or else Nkind
(DSS
) = N_Attribute_Reference
1222 Error_Msg_N
("only a subtype mark is allowed in a formal", DSS
);
1229 Array_Type_Declaration
(T
, Def
);
1230 Set_Is_Generic_Type
(Base_Type
(T
));
1232 if Ekind
(Component_Type
(T
)) = E_Incomplete_Type
1233 and then No
(Full_View
(Component_Type
(T
)))
1235 Error_Msg_N
("premature usage of incomplete type", Def
);
1237 -- Check that range constraint is not allowed on the component type
1238 -- of a generic formal array type (AARM 12.5.3(3))
1240 elsif Is_Internal
(Component_Type
(T
))
1241 and then Present
(Subtype_Indication
(Component_Definition
(Def
)))
1242 and then Nkind
(Original_Node
1243 (Subtype_Indication
(Component_Definition
(Def
))))
1244 = N_Subtype_Indication
1247 ("in a formal, a subtype indication can only be "
1248 & "a subtype mark ('R'M 12.5.3(3))",
1249 Subtype_Indication
(Component_Definition
(Def
)));
1252 end Analyze_Formal_Array_Type
;
1254 ---------------------------------------------
1255 -- Analyze_Formal_Decimal_Fixed_Point_Type --
1256 ---------------------------------------------
1258 -- As for other generic types, we create a valid type representation
1259 -- with legal but arbitrary attributes, whose values are never considered
1260 -- static. For all scalar types we introduce an anonymous base type, with
1261 -- the same attributes. We choose the corresponding integer type to be
1262 -- Standard_Integer.
1264 procedure Analyze_Formal_Decimal_Fixed_Point_Type
1268 Loc
: constant Source_Ptr
:= Sloc
(Def
);
1269 Base
: constant Entity_Id
:=
1271 (E_Decimal_Fixed_Point_Type
,
1272 Current_Scope
, Sloc
(Def
), 'G');
1273 Int_Base
: constant Entity_Id
:= Standard_Integer
;
1274 Delta_Val
: constant Ureal
:= Ureal_1
;
1275 Digs_Val
: constant Uint
:= Uint_6
;
1280 Set_Etype
(Base
, Base
);
1281 Set_Size_Info
(Base
, Int_Base
);
1282 Set_RM_Size
(Base
, RM_Size
(Int_Base
));
1283 Set_First_Rep_Item
(Base
, First_Rep_Item
(Int_Base
));
1284 Set_Digits_Value
(Base
, Digs_Val
);
1285 Set_Delta_Value
(Base
, Delta_Val
);
1286 Set_Small_Value
(Base
, Delta_Val
);
1287 Set_Scalar_Range
(Base
,
1289 Low_Bound
=> Make_Real_Literal
(Loc
, Ureal_1
),
1290 High_Bound
=> Make_Real_Literal
(Loc
, Ureal_1
)));
1292 Set_Is_Generic_Type
(Base
);
1293 Set_Parent
(Base
, Parent
(Def
));
1295 Set_Ekind
(T
, E_Decimal_Fixed_Point_Subtype
);
1296 Set_Etype
(T
, Base
);
1297 Set_Size_Info
(T
, Int_Base
);
1298 Set_RM_Size
(T
, RM_Size
(Int_Base
));
1299 Set_First_Rep_Item
(T
, First_Rep_Item
(Int_Base
));
1300 Set_Digits_Value
(T
, Digs_Val
);
1301 Set_Delta_Value
(T
, Delta_Val
);
1302 Set_Small_Value
(T
, Delta_Val
);
1303 Set_Scalar_Range
(T
, Scalar_Range
(Base
));
1304 Set_Is_Constrained
(T
);
1306 Check_Restriction
(No_Fixed_Point
, Def
);
1307 end Analyze_Formal_Decimal_Fixed_Point_Type
;
1309 -------------------------------------------
1310 -- Analyze_Formal_Derived_Interface_Type --
1311 -------------------------------------------
1313 procedure Analyze_Formal_Derived_Interface_Type
1319 Set_Ekind
(T
, E_Record_Type
);
1321 Analyze
(Subtype_Indication
(Def
));
1322 Analyze_Interface_Declaration
(T
, Def
);
1323 Make_Class_Wide_Type
(T
);
1324 Set_Primitive_Operations
(T
, New_Elmt_List
);
1325 Analyze_List
(Interface_List
(Def
));
1326 Collect_Interfaces
(Def
, T
);
1327 end Analyze_Formal_Derived_Interface_Type
;
1329 ---------------------------------
1330 -- Analyze_Formal_Derived_Type --
1331 ---------------------------------
1333 procedure Analyze_Formal_Derived_Type
1338 Loc
: constant Source_Ptr
:= Sloc
(Def
);
1339 Unk_Disc
: constant Boolean := Unknown_Discriminants_Present
(N
);
1343 Set_Is_Generic_Type
(T
);
1345 if Private_Present
(Def
) then
1347 Make_Private_Extension_Declaration
(Loc
,
1348 Defining_Identifier
=> T
,
1349 Discriminant_Specifications
=> Discriminant_Specifications
(N
),
1350 Unknown_Discriminants_Present
=> Unk_Disc
,
1351 Subtype_Indication
=> Subtype_Mark
(Def
));
1353 Set_Abstract_Present
(New_N
, Abstract_Present
(Def
));
1354 Set_Limited_Present
(New_N
, Limited_Present
(Def
));
1358 Make_Full_Type_Declaration
(Loc
,
1359 Defining_Identifier
=> T
,
1360 Discriminant_Specifications
=>
1361 Discriminant_Specifications
(Parent
(T
)),
1363 Make_Derived_Type_Definition
(Loc
,
1364 Subtype_Indication
=> Subtype_Mark
(Def
)));
1366 Set_Abstract_Present
1367 (Type_Definition
(New_N
), Abstract_Present
(Def
));
1369 (Type_Definition
(New_N
), Limited_Present
(Def
));
1376 if not Is_Composite_Type
(T
) then
1378 ("unknown discriminants not allowed for elementary types", N
);
1380 Set_Has_Unknown_Discriminants
(T
);
1381 Set_Is_Constrained
(T
, False);
1385 -- If the parent type has a known size, so does the formal, which
1386 -- makes legal representation clauses that involve the formal.
1388 Set_Size_Known_At_Compile_Time
1389 (T
, Size_Known_At_Compile_Time
(Entity
(Subtype_Mark
(Def
))));
1391 end Analyze_Formal_Derived_Type
;
1393 ----------------------------------
1394 -- Analyze_Formal_Discrete_Type --
1395 ----------------------------------
1397 -- The operations defined for a discrete types are those of an
1398 -- enumeration type. The size is set to an arbitrary value, for use
1399 -- in analyzing the generic unit.
1401 procedure Analyze_Formal_Discrete_Type
(T
: Entity_Id
; Def
: Node_Id
) is
1402 Loc
: constant Source_Ptr
:= Sloc
(Def
);
1406 Base
: constant Entity_Id
:=
1408 (E_Floating_Point_Type
, Current_Scope
, Sloc
(Def
), 'G');
1411 Set_Ekind
(T
, E_Enumeration_Subtype
);
1412 Set_Etype
(T
, Base
);
1415 Set_Is_Generic_Type
(T
);
1416 Set_Is_Constrained
(T
);
1418 -- For semantic analysis, the bounds of the type must be set to some
1419 -- non-static value. The simplest is to create attribute nodes for
1420 -- those bounds, that refer to the type itself. These bounds are never
1421 -- analyzed but serve as place-holders.
1424 Make_Attribute_Reference
(Loc
,
1425 Attribute_Name
=> Name_First
,
1426 Prefix
=> New_Reference_To
(T
, Loc
));
1430 Make_Attribute_Reference
(Loc
,
1431 Attribute_Name
=> Name_Last
,
1432 Prefix
=> New_Reference_To
(T
, Loc
));
1435 Set_Scalar_Range
(T
,
1440 Set_Ekind
(Base
, E_Enumeration_Type
);
1441 Set_Etype
(Base
, Base
);
1442 Init_Size
(Base
, 8);
1443 Init_Alignment
(Base
);
1444 Set_Is_Generic_Type
(Base
);
1445 Set_Scalar_Range
(Base
, Scalar_Range
(T
));
1446 Set_Parent
(Base
, Parent
(Def
));
1448 end Analyze_Formal_Discrete_Type
;
1450 ----------------------------------
1451 -- Analyze_Formal_Floating_Type --
1452 ---------------------------------
1454 procedure Analyze_Formal_Floating_Type
(T
: Entity_Id
; Def
: Node_Id
) is
1455 Base
: constant Entity_Id
:=
1457 (E_Floating_Point_Type
, Current_Scope
, Sloc
(Def
), 'G');
1460 -- The various semantic attributes are taken from the predefined type
1461 -- Float, just so that all of them are initialized. Their values are
1462 -- never used because no constant folding or expansion takes place in
1463 -- the generic itself.
1466 Set_Ekind
(T
, E_Floating_Point_Subtype
);
1467 Set_Etype
(T
, Base
);
1468 Set_Size_Info
(T
, (Standard_Float
));
1469 Set_RM_Size
(T
, RM_Size
(Standard_Float
));
1470 Set_Digits_Value
(T
, Digits_Value
(Standard_Float
));
1471 Set_Scalar_Range
(T
, Scalar_Range
(Standard_Float
));
1472 Set_Is_Constrained
(T
);
1474 Set_Is_Generic_Type
(Base
);
1475 Set_Etype
(Base
, Base
);
1476 Set_Size_Info
(Base
, (Standard_Float
));
1477 Set_RM_Size
(Base
, RM_Size
(Standard_Float
));
1478 Set_Digits_Value
(Base
, Digits_Value
(Standard_Float
));
1479 Set_Scalar_Range
(Base
, Scalar_Range
(Standard_Float
));
1480 Set_Parent
(Base
, Parent
(Def
));
1482 Check_Restriction
(No_Floating_Point
, Def
);
1483 end Analyze_Formal_Floating_Type
;
1485 -----------------------------------
1486 -- Analyze_Formal_Interface_Type;--
1487 -----------------------------------
1489 procedure Analyze_Formal_Interface_Type
(T
: Entity_Id
; Def
: Node_Id
) is
1492 Set_Ekind
(T
, E_Record_Type
);
1494 Analyze_Interface_Declaration
(T
, Def
);
1495 Make_Class_Wide_Type
(T
);
1496 Set_Primitive_Operations
(T
, New_Elmt_List
);
1497 end Analyze_Formal_Interface_Type
;
1499 ---------------------------------
1500 -- Analyze_Formal_Modular_Type --
1501 ---------------------------------
1503 procedure Analyze_Formal_Modular_Type
(T
: Entity_Id
; Def
: Node_Id
) is
1505 -- Apart from their entity kind, generic modular types are treated
1506 -- like signed integer types, and have the same attributes.
1508 Analyze_Formal_Signed_Integer_Type
(T
, Def
);
1509 Set_Ekind
(T
, E_Modular_Integer_Subtype
);
1510 Set_Ekind
(Etype
(T
), E_Modular_Integer_Type
);
1512 end Analyze_Formal_Modular_Type
;
1514 ---------------------------------------
1515 -- Analyze_Formal_Object_Declaration --
1516 ---------------------------------------
1518 procedure Analyze_Formal_Object_Declaration
(N
: Node_Id
) is
1519 E
: constant Node_Id
:= Expression
(N
);
1520 Id
: constant Node_Id
:= Defining_Identifier
(N
);
1527 -- Determine the mode of the formal object
1529 if Out_Present
(N
) then
1530 K
:= E_Generic_In_Out_Parameter
;
1532 if not In_Present
(N
) then
1533 Error_Msg_N
("formal generic objects cannot have mode OUT", N
);
1537 K
:= E_Generic_In_Parameter
;
1540 Find_Type
(Subtype_Mark
(N
));
1541 T
:= Entity
(Subtype_Mark
(N
));
1543 if Ekind
(T
) = E_Incomplete_Type
then
1544 Error_Msg_N
("premature usage of incomplete type", Subtype_Mark
(N
));
1547 if K
= E_Generic_In_Parameter
then
1549 -- Ada 2005 (AI-287): Limited aggregates allowed in generic formals
1551 if Ada_Version
< Ada_05
and then Is_Limited_Type
(T
) then
1553 ("generic formal of mode IN must not be of limited type", N
);
1554 Explain_Limited_Type
(T
, N
);
1557 if Is_Abstract
(T
) then
1559 ("generic formal of mode IN must not be of abstract type", N
);
1563 Analyze_Per_Use_Expression
(E
, T
);
1569 -- Case of generic IN OUT parameter
1572 -- If the formal has an unconstrained type, construct its
1573 -- actual subtype, as is done for subprogram formals. In this
1574 -- fashion, all its uses can refer to specific bounds.
1579 if (Is_Array_Type
(T
)
1580 and then not Is_Constrained
(T
))
1582 (Ekind
(T
) = E_Record_Type
1583 and then Has_Discriminants
(T
))
1586 Non_Freezing_Ref
: constant Node_Id
:=
1587 New_Reference_To
(Id
, Sloc
(Id
));
1591 -- Make sure that the actual subtype doesn't generate
1594 Set_Must_Not_Freeze
(Non_Freezing_Ref
);
1595 Decl
:= Build_Actual_Subtype
(T
, Non_Freezing_Ref
);
1596 Insert_Before_And_Analyze
(N
, Decl
);
1597 Set_Actual_Subtype
(Id
, Defining_Identifier
(Decl
));
1600 Set_Actual_Subtype
(Id
, T
);
1605 ("initialization not allowed for `IN OUT` formals", N
);
1609 end Analyze_Formal_Object_Declaration
;
1611 ----------------------------------------------
1612 -- Analyze_Formal_Ordinary_Fixed_Point_Type --
1613 ----------------------------------------------
1615 procedure Analyze_Formal_Ordinary_Fixed_Point_Type
1619 Loc
: constant Source_Ptr
:= Sloc
(Def
);
1620 Base
: constant Entity_Id
:=
1622 (E_Ordinary_Fixed_Point_Type
, Current_Scope
, Sloc
(Def
), 'G');
1624 -- The semantic attributes are set for completeness only, their
1625 -- values will never be used, because all properties of the type
1629 Set_Ekind
(T
, E_Ordinary_Fixed_Point_Subtype
);
1630 Set_Etype
(T
, Base
);
1631 Set_Size_Info
(T
, Standard_Integer
);
1632 Set_RM_Size
(T
, RM_Size
(Standard_Integer
));
1633 Set_Small_Value
(T
, Ureal_1
);
1634 Set_Delta_Value
(T
, Ureal_1
);
1635 Set_Scalar_Range
(T
,
1637 Low_Bound
=> Make_Real_Literal
(Loc
, Ureal_1
),
1638 High_Bound
=> Make_Real_Literal
(Loc
, Ureal_1
)));
1639 Set_Is_Constrained
(T
);
1641 Set_Is_Generic_Type
(Base
);
1642 Set_Etype
(Base
, Base
);
1643 Set_Size_Info
(Base
, Standard_Integer
);
1644 Set_RM_Size
(Base
, RM_Size
(Standard_Integer
));
1645 Set_Small_Value
(Base
, Ureal_1
);
1646 Set_Delta_Value
(Base
, Ureal_1
);
1647 Set_Scalar_Range
(Base
, Scalar_Range
(T
));
1648 Set_Parent
(Base
, Parent
(Def
));
1650 Check_Restriction
(No_Fixed_Point
, Def
);
1651 end Analyze_Formal_Ordinary_Fixed_Point_Type
;
1653 ----------------------------
1654 -- Analyze_Formal_Package --
1655 ----------------------------
1657 procedure Analyze_Formal_Package
(N
: Node_Id
) is
1658 Loc
: constant Source_Ptr
:= Sloc
(N
);
1659 Pack_Id
: constant Entity_Id
:= Defining_Identifier
(N
);
1661 Gen_Id
: constant Node_Id
:= Name
(N
);
1663 Gen_Unit
: Entity_Id
;
1665 Parent_Installed
: Boolean := False;
1667 Parent_Instance
: Entity_Id
;
1668 Renaming_In_Par
: Entity_Id
;
1671 Text_IO_Kludge
(Gen_Id
);
1674 Check_Generic_Child_Unit
(Gen_Id
, Parent_Installed
);
1675 Gen_Unit
:= Entity
(Gen_Id
);
1677 -- Check for a formal package that is a package renaming
1679 if Present
(Renamed_Object
(Gen_Unit
)) then
1680 Gen_Unit
:= Renamed_Object
(Gen_Unit
);
1683 if Ekind
(Gen_Unit
) /= E_Generic_Package
then
1684 Error_Msg_N
("expect generic package name", Gen_Id
);
1688 elsif Gen_Unit
= Current_Scope
then
1690 ("generic package cannot be used as a formal package of itself",
1695 elsif In_Open_Scopes
(Gen_Unit
) then
1696 if Is_Compilation_Unit
(Gen_Unit
)
1697 and then Is_Child_Unit
(Current_Scope
)
1699 -- Special-case the error when the formal is a parent, and
1700 -- continue analysis to minimize cascaded errors.
1703 ("generic parent cannot be used as formal package "
1704 & "of a child unit",
1709 ("generic package cannot be used as a formal package "
1717 -- The formal package is treated like a regular instance, but only
1718 -- the specification needs to be instantiated, to make entities visible.
1720 if not Box_Present
(N
) then
1721 Hidden_Entities
:= New_Elmt_List
;
1722 Analyze_Package_Instantiation
(N
);
1724 if Parent_Installed
then
1729 -- If there are no generic associations, the generic parameters
1730 -- appear as local entities and are instantiated like them. We copy
1731 -- the generic package declaration as if it were an instantiation,
1732 -- and analyze it like a regular package, except that we treat the
1733 -- formals as additional visible components.
1735 Gen_Decl
:= Unit_Declaration_Node
(Gen_Unit
);
1737 if In_Extended_Main_Source_Unit
(N
) then
1738 Set_Is_Instantiated
(Gen_Unit
);
1739 Generate_Reference
(Gen_Unit
, N
);
1742 Formal
:= New_Copy
(Pack_Id
);
1743 Create_Instantiation_Source
(N
, Gen_Unit
, False, S_Adjustment
);
1747 (Original_Node
(Gen_Decl
), Empty
, Instantiating
=> True);
1749 Set_Defining_Unit_Name
(Specification
(New_N
), Formal
);
1750 Set_Generic_Parent
(Specification
(N
), Gen_Unit
);
1751 Set_Instance_Env
(Gen_Unit
, Formal
);
1753 Enter_Name
(Formal
);
1754 Set_Ekind
(Formal
, E_Generic_Package
);
1755 Set_Etype
(Formal
, Standard_Void_Type
);
1756 Set_Inner_Instances
(Formal
, New_Elmt_List
);
1759 -- Within the formal, the name of the generic package is a renaming
1760 -- of the formal (as for a regular instantiation).
1762 Renaming
:= Make_Package_Renaming_Declaration
(Loc
,
1763 Defining_Unit_Name
=>
1764 Make_Defining_Identifier
(Loc
, Chars
(Gen_Unit
)),
1765 Name
=> New_Reference_To
(Formal
, Loc
));
1767 if Present
(Visible_Declarations
(Specification
(N
))) then
1768 Prepend
(Renaming
, To
=> Visible_Declarations
(Specification
(N
)));
1769 elsif Present
(Private_Declarations
(Specification
(N
))) then
1770 Prepend
(Renaming
, To
=> Private_Declarations
(Specification
(N
)));
1773 if Is_Child_Unit
(Gen_Unit
)
1774 and then Parent_Installed
1776 -- Similarly, we have to make the name of the formal visible in
1777 -- the parent instance, to resolve properly fully qualified names
1778 -- that may appear in the generic unit. The parent instance has
1779 -- been placed on the scope stack ahead of the current scope.
1781 Parent_Instance
:= Scope_Stack
.Table
(Scope_Stack
.Last
- 1).Entity
;
1784 Make_Defining_Identifier
(Loc
, Chars
(Gen_Unit
));
1785 Set_Ekind
(Renaming_In_Par
, E_Package
);
1786 Set_Etype
(Renaming_In_Par
, Standard_Void_Type
);
1787 Set_Scope
(Renaming_In_Par
, Parent_Instance
);
1788 Set_Parent
(Renaming_In_Par
, Parent
(Formal
));
1789 Set_Renamed_Object
(Renaming_In_Par
, Formal
);
1790 Append_Entity
(Renaming_In_Par
, Parent_Instance
);
1793 Analyze_Generic_Formal_Part
(N
);
1794 Analyze
(Specification
(N
));
1795 End_Package_Scope
(Formal
);
1797 if Parent_Installed
then
1803 -- Inside the generic unit, the formal package is a regular
1804 -- package, but no body is needed for it. Note that after
1805 -- instantiation, the defining_unit_name we need is in the
1806 -- new tree and not in the original. (see Package_Instantiation).
1807 -- A generic formal package is an instance, and can be used as
1808 -- an actual for an inner instance.
1810 Set_Ekind
(Formal
, E_Package
);
1811 Set_Has_Completion
(Formal
, True);
1813 Set_Ekind
(Pack_Id
, E_Package
);
1814 Set_Etype
(Pack_Id
, Standard_Void_Type
);
1815 Set_Scope
(Pack_Id
, Scope
(Formal
));
1816 Set_Has_Completion
(Pack_Id
, True);
1818 end Analyze_Formal_Package
;
1820 ---------------------------------
1821 -- Analyze_Formal_Private_Type --
1822 ---------------------------------
1824 procedure Analyze_Formal_Private_Type
1830 New_Private_Type
(N
, T
, Def
);
1832 -- Set the size to an arbitrary but legal value
1834 Set_Size_Info
(T
, Standard_Integer
);
1835 Set_RM_Size
(T
, RM_Size
(Standard_Integer
));
1836 end Analyze_Formal_Private_Type
;
1838 ----------------------------------------
1839 -- Analyze_Formal_Signed_Integer_Type --
1840 ----------------------------------------
1842 procedure Analyze_Formal_Signed_Integer_Type
1846 Base
: constant Entity_Id
:=
1848 (E_Signed_Integer_Type
, Current_Scope
, Sloc
(Def
), 'G');
1853 Set_Ekind
(T
, E_Signed_Integer_Subtype
);
1854 Set_Etype
(T
, Base
);
1855 Set_Size_Info
(T
, Standard_Integer
);
1856 Set_RM_Size
(T
, RM_Size
(Standard_Integer
));
1857 Set_Scalar_Range
(T
, Scalar_Range
(Standard_Integer
));
1858 Set_Is_Constrained
(T
);
1860 Set_Is_Generic_Type
(Base
);
1861 Set_Size_Info
(Base
, Standard_Integer
);
1862 Set_RM_Size
(Base
, RM_Size
(Standard_Integer
));
1863 Set_Etype
(Base
, Base
);
1864 Set_Scalar_Range
(Base
, Scalar_Range
(Standard_Integer
));
1865 Set_Parent
(Base
, Parent
(Def
));
1866 end Analyze_Formal_Signed_Integer_Type
;
1868 -------------------------------
1869 -- Analyze_Formal_Subprogram --
1870 -------------------------------
1872 procedure Analyze_Formal_Subprogram
(N
: Node_Id
) is
1873 Spec
: constant Node_Id
:= Specification
(N
);
1874 Def
: constant Node_Id
:= Default_Name
(N
);
1875 Nam
: constant Entity_Id
:= Defining_Unit_Name
(Spec
);
1883 if Nkind
(Nam
) = N_Defining_Program_Unit_Name
then
1884 Error_Msg_N
("name of formal subprogram must be a direct name", Nam
);
1888 Analyze_Subprogram_Declaration
(N
);
1889 Set_Is_Formal_Subprogram
(Nam
);
1890 Set_Has_Completion
(Nam
);
1892 if Nkind
(N
) = N_Formal_Abstract_Subprogram_Declaration
then
1893 Set_Is_Abstract
(Nam
);
1894 Set_Is_Dispatching_Operation
(Nam
);
1897 Ctrl_Type
: constant Entity_Id
:= Find_Dispatching_Type
(Nam
);
1900 if No
(Ctrl_Type
) then
1902 ("abstract formal subprogram must have a controlling type",
1906 Check_Controlling_Formals
(Ctrl_Type
, Nam
);
1911 -- Default name is resolved at the point of instantiation
1913 if Box_Present
(N
) then
1916 -- Else default is bound at the point of generic declaration
1918 elsif Present
(Def
) then
1919 if Nkind
(Def
) = N_Operator_Symbol
then
1920 Find_Direct_Name
(Def
);
1922 elsif Nkind
(Def
) /= N_Attribute_Reference
then
1926 -- For an attribute reference, analyze the prefix and verify
1927 -- that it has the proper profile for the subprogram.
1929 Analyze
(Prefix
(Def
));
1930 Valid_Default_Attribute
(Nam
, Def
);
1934 -- Default name may be overloaded, in which case the interpretation
1935 -- with the correct profile must be selected, as for a renaming.
1937 if Etype
(Def
) = Any_Type
then
1940 elsif Nkind
(Def
) = N_Selected_Component
then
1941 Subp
:= Entity
(Selector_Name
(Def
));
1943 if Ekind
(Subp
) /= E_Entry
then
1944 Error_Msg_N
("expect valid subprogram name as default", Def
);
1948 elsif Nkind
(Def
) = N_Indexed_Component
then
1950 if Nkind
(Prefix
(Def
)) /= N_Selected_Component
then
1951 Error_Msg_N
("expect valid subprogram name as default", Def
);
1955 Subp
:= Entity
(Selector_Name
(Prefix
(Def
)));
1957 if Ekind
(Subp
) /= E_Entry_Family
then
1958 Error_Msg_N
("expect valid subprogram name as default", Def
);
1963 elsif Nkind
(Def
) = N_Character_Literal
then
1965 -- Needs some type checks: subprogram should be parameterless???
1967 Resolve
(Def
, (Etype
(Nam
)));
1969 elsif not Is_Entity_Name
(Def
)
1970 or else not Is_Overloadable
(Entity
(Def
))
1972 Error_Msg_N
("expect valid subprogram name as default", Def
);
1975 elsif not Is_Overloaded
(Def
) then
1976 Subp
:= Entity
(Def
);
1979 Error_Msg_N
("premature usage of formal subprogram", Def
);
1981 elsif not Entity_Matches_Spec
(Subp
, Nam
) then
1982 Error_Msg_N
("no visible entity matches specification", Def
);
1988 I1
: Interp_Index
:= 0;
1994 Get_First_Interp
(Def
, I
, It
);
1995 while Present
(It
.Nam
) loop
1997 if Entity_Matches_Spec
(It
.Nam
, Nam
) then
1998 if Subp
/= Any_Id
then
1999 It1
:= Disambiguate
(Def
, I1
, I
, Etype
(Subp
));
2001 if It1
= No_Interp
then
2002 Error_Msg_N
("ambiguous default subprogram", Def
);
2015 Get_Next_Interp
(I
, It
);
2019 if Subp
/= Any_Id
then
2020 Set_Entity
(Def
, Subp
);
2023 Error_Msg_N
("premature usage of formal subprogram", Def
);
2025 elsif Ekind
(Subp
) /= E_Operator
then
2026 Check_Mode_Conformant
(Subp
, Nam
);
2030 Error_Msg_N
("no visible subprogram matches specification", N
);
2034 end Analyze_Formal_Subprogram
;
2036 -------------------------------------
2037 -- Analyze_Formal_Type_Declaration --
2038 -------------------------------------
2040 procedure Analyze_Formal_Type_Declaration
(N
: Node_Id
) is
2041 Def
: constant Node_Id
:= Formal_Type_Definition
(N
);
2045 T
:= Defining_Identifier
(N
);
2047 if Present
(Discriminant_Specifications
(N
))
2048 and then Nkind
(Def
) /= N_Formal_Private_Type_Definition
2051 ("discriminants not allowed for this formal type",
2052 Defining_Identifier
(First
(Discriminant_Specifications
(N
))));
2055 -- Enter the new name, and branch to specific routine
2058 when N_Formal_Private_Type_Definition
=>
2059 Analyze_Formal_Private_Type
(N
, T
, Def
);
2061 when N_Formal_Derived_Type_Definition
=>
2062 Analyze_Formal_Derived_Type
(N
, T
, Def
);
2064 when N_Formal_Discrete_Type_Definition
=>
2065 Analyze_Formal_Discrete_Type
(T
, Def
);
2067 when N_Formal_Signed_Integer_Type_Definition
=>
2068 Analyze_Formal_Signed_Integer_Type
(T
, Def
);
2070 when N_Formal_Modular_Type_Definition
=>
2071 Analyze_Formal_Modular_Type
(T
, Def
);
2073 when N_Formal_Floating_Point_Definition
=>
2074 Analyze_Formal_Floating_Type
(T
, Def
);
2076 when N_Formal_Ordinary_Fixed_Point_Definition
=>
2077 Analyze_Formal_Ordinary_Fixed_Point_Type
(T
, Def
);
2079 when N_Formal_Decimal_Fixed_Point_Definition
=>
2080 Analyze_Formal_Decimal_Fixed_Point_Type
(T
, Def
);
2082 when N_Array_Type_Definition
=>
2083 Analyze_Formal_Array_Type
(T
, Def
);
2085 when N_Access_To_Object_Definition |
2086 N_Access_Function_Definition |
2087 N_Access_Procedure_Definition
=>
2088 Analyze_Generic_Access_Type
(T
, Def
);
2090 -- Ada 2005: a interface declaration is encoded as an abstract
2091 -- record declaration or a abstract type derivation.
2093 when N_Record_Definition
=>
2094 Analyze_Formal_Interface_Type
(T
, Def
);
2096 when N_Derived_Type_Definition
=>
2097 Analyze_Formal_Derived_Interface_Type
(T
, Def
);
2103 raise Program_Error
;
2107 Set_Is_Generic_Type
(T
);
2108 end Analyze_Formal_Type_Declaration
;
2110 ------------------------------------
2111 -- Analyze_Function_Instantiation --
2112 ------------------------------------
2114 procedure Analyze_Function_Instantiation
(N
: Node_Id
) is
2116 Analyze_Subprogram_Instantiation
(N
, E_Function
);
2117 end Analyze_Function_Instantiation
;
2119 ---------------------------------
2120 -- Analyze_Generic_Access_Type --
2121 ---------------------------------
2123 procedure Analyze_Generic_Access_Type
(T
: Entity_Id
; Def
: Node_Id
) is
2127 if Nkind
(Def
) = N_Access_To_Object_Definition
then
2128 Access_Type_Declaration
(T
, Def
);
2130 if Is_Incomplete_Or_Private_Type
(Designated_Type
(T
))
2131 and then No
(Full_View
(Designated_Type
(T
)))
2132 and then not Is_Generic_Type
(Designated_Type
(T
))
2134 Error_Msg_N
("premature usage of incomplete type", Def
);
2136 elsif Is_Internal
(Designated_Type
(T
)) then
2138 ("only a subtype mark is allowed in a formal", Def
);
2142 Access_Subprogram_Declaration
(T
, Def
);
2144 end Analyze_Generic_Access_Type
;
2146 ---------------------------------
2147 -- Analyze_Generic_Formal_Part --
2148 ---------------------------------
2150 procedure Analyze_Generic_Formal_Part
(N
: Node_Id
) is
2151 Gen_Parm_Decl
: Node_Id
;
2154 -- The generic formals are processed in the scope of the generic
2155 -- unit, where they are immediately visible. The scope is installed
2158 Gen_Parm_Decl
:= First
(Generic_Formal_Declarations
(N
));
2160 while Present
(Gen_Parm_Decl
) loop
2161 Analyze
(Gen_Parm_Decl
);
2162 Next
(Gen_Parm_Decl
);
2165 Generate_Reference_To_Generic_Formals
(Current_Scope
);
2166 end Analyze_Generic_Formal_Part
;
2168 ------------------------------------------
2169 -- Analyze_Generic_Package_Declaration --
2170 ------------------------------------------
2172 procedure Analyze_Generic_Package_Declaration
(N
: Node_Id
) is
2173 Loc
: constant Source_Ptr
:= Sloc
(N
);
2176 Save_Parent
: Node_Id
;
2178 Decls
: constant List_Id
:=
2179 Visible_Declarations
(Specification
(N
));
2183 -- We introduce a renaming of the enclosing package, to have a usable
2184 -- entity as the prefix of an expanded name for a local entity of the
2185 -- form Par.P.Q, where P is the generic package. This is because a local
2186 -- entity named P may hide it, so that the usual visibility rules in
2187 -- the instance will not resolve properly.
2190 Make_Package_Renaming_Declaration
(Loc
,
2191 Defining_Unit_Name
=>
2192 Make_Defining_Identifier
(Loc
,
2193 Chars
=> New_External_Name
(Chars
(Defining_Entity
(N
)), "GH")),
2194 Name
=> Make_Identifier
(Loc
, Chars
(Defining_Entity
(N
))));
2196 if Present
(Decls
) then
2197 Decl
:= First
(Decls
);
2198 while Present
(Decl
)
2199 and then Nkind
(Decl
) = N_Pragma
2204 if Present
(Decl
) then
2205 Insert_Before
(Decl
, Renaming
);
2207 Append
(Renaming
, Visible_Declarations
(Specification
(N
)));
2211 Set_Visible_Declarations
(Specification
(N
), New_List
(Renaming
));
2214 -- Create copy of generic unit, and save for instantiation.
2215 -- If the unit is a child unit, do not copy the specifications
2216 -- for the parent, which are not part of the generic tree.
2218 Save_Parent
:= Parent_Spec
(N
);
2219 Set_Parent_Spec
(N
, Empty
);
2221 New_N
:= Copy_Generic_Node
(N
, Empty
, Instantiating
=> False);
2222 Set_Parent_Spec
(New_N
, Save_Parent
);
2224 Id
:= Defining_Entity
(N
);
2225 Generate_Definition
(Id
);
2227 -- Expansion is not applied to generic units
2232 Set_Ekind
(Id
, E_Generic_Package
);
2233 Set_Etype
(Id
, Standard_Void_Type
);
2235 Enter_Generic_Scope
(Id
);
2236 Set_Inner_Instances
(Id
, New_Elmt_List
);
2238 Set_Categorization_From_Pragmas
(N
);
2239 Set_Is_Pure
(Id
, Is_Pure
(Current_Scope
));
2241 -- Link the declaration of the generic homonym in the generic copy
2242 -- to the package it renames, so that it is always resolved properly.
2244 Set_Generic_Homonym
(Id
, Defining_Unit_Name
(Renaming
));
2245 Set_Entity
(Associated_Node
(Name
(Renaming
)), Id
);
2247 -- For a library unit, we have reconstructed the entity for the
2248 -- unit, and must reset it in the library tables.
2250 if Nkind
(Parent
(N
)) = N_Compilation_Unit
then
2251 Set_Cunit_Entity
(Current_Sem_Unit
, Id
);
2254 Analyze_Generic_Formal_Part
(N
);
2256 -- After processing the generic formals, analysis proceeds
2257 -- as for a non-generic package.
2259 Analyze
(Specification
(N
));
2261 Validate_Categorization_Dependency
(N
, Id
);
2265 End_Package_Scope
(Id
);
2266 Exit_Generic_Scope
(Id
);
2268 if Nkind
(Parent
(N
)) /= N_Compilation_Unit
then
2269 Move_Freeze_Nodes
(Id
, N
, Visible_Declarations
(Specification
(N
)));
2270 Move_Freeze_Nodes
(Id
, N
, Private_Declarations
(Specification
(N
)));
2271 Move_Freeze_Nodes
(Id
, N
, Generic_Formal_Declarations
(N
));
2274 Set_Body_Required
(Parent
(N
), Unit_Requires_Body
(Id
));
2275 Validate_RT_RAT_Component
(N
);
2277 -- If this is a spec without a body, check that generic parameters
2280 if not Body_Required
(Parent
(N
)) then
2281 Check_References
(Id
);
2284 end Analyze_Generic_Package_Declaration
;
2286 --------------------------------------------
2287 -- Analyze_Generic_Subprogram_Declaration --
2288 --------------------------------------------
2290 procedure Analyze_Generic_Subprogram_Declaration
(N
: Node_Id
) is
2295 Result_Type
: Entity_Id
;
2296 Save_Parent
: Node_Id
;
2299 -- Create copy of generic unit,and save for instantiation.
2300 -- If the unit is a child unit, do not copy the specifications
2301 -- for the parent, which are not part of the generic tree.
2303 Save_Parent
:= Parent_Spec
(N
);
2304 Set_Parent_Spec
(N
, Empty
);
2306 New_N
:= Copy_Generic_Node
(N
, Empty
, Instantiating
=> False);
2307 Set_Parent_Spec
(New_N
, Save_Parent
);
2310 Spec
:= Specification
(N
);
2311 Id
:= Defining_Entity
(Spec
);
2312 Generate_Definition
(Id
);
2314 if Nkind
(Id
) = N_Defining_Operator_Symbol
then
2316 ("operator symbol not allowed for generic subprogram", Id
);
2323 Set_Scope_Depth_Value
(Id
, Scope_Depth
(Current_Scope
) + 1);
2325 Enter_Generic_Scope
(Id
);
2326 Set_Inner_Instances
(Id
, New_Elmt_List
);
2327 Set_Is_Pure
(Id
, Is_Pure
(Current_Scope
));
2329 Analyze_Generic_Formal_Part
(N
);
2331 Formals
:= Parameter_Specifications
(Spec
);
2333 if Present
(Formals
) then
2334 Process_Formals
(Formals
, Spec
);
2337 if Nkind
(Spec
) = N_Function_Specification
then
2338 Set_Ekind
(Id
, E_Generic_Function
);
2340 if Nkind
(Result_Definition
(Spec
)) = N_Access_Definition
then
2341 Result_Type
:= Access_Definition
(Spec
, Result_Definition
(Spec
));
2342 Set_Etype
(Id
, Result_Type
);
2344 Find_Type
(Result_Definition
(Spec
));
2345 Set_Etype
(Id
, Entity
(Result_Definition
(Spec
)));
2349 Set_Ekind
(Id
, E_Generic_Procedure
);
2350 Set_Etype
(Id
, Standard_Void_Type
);
2353 -- For a library unit, we have reconstructed the entity for the unit,
2354 -- and must reset it in the library tables. We also make sure that
2355 -- Body_Required is set properly in the original compilation unit node.
2357 if Nkind
(Parent
(N
)) = N_Compilation_Unit
then
2358 Set_Cunit_Entity
(Current_Sem_Unit
, Id
);
2359 Set_Body_Required
(Parent
(N
), Unit_Requires_Body
(Id
));
2362 Set_Categorization_From_Pragmas
(N
);
2363 Validate_Categorization_Dependency
(N
, Id
);
2365 Save_Global_References
(Original_Node
(N
));
2369 Exit_Generic_Scope
(Id
);
2370 Generate_Reference_To_Formals
(Id
);
2371 end Analyze_Generic_Subprogram_Declaration
;
2373 -----------------------------------
2374 -- Analyze_Package_Instantiation --
2375 -----------------------------------
2377 -- Note: this procedure is also used for formal package declarations, in
2378 -- which case the argument N is an N_Formal_Package_Declaration node.
2379 -- This should really be noted in the spec! ???
2381 procedure Analyze_Package_Instantiation
(N
: Node_Id
) is
2382 Loc
: constant Source_Ptr
:= Sloc
(N
);
2383 Gen_Id
: constant Node_Id
:= Name
(N
);
2386 Act_Decl_Name
: Node_Id
;
2387 Act_Decl_Id
: Entity_Id
;
2392 Gen_Unit
: Entity_Id
;
2394 Is_Actual_Pack
: constant Boolean :=
2395 Is_Internal
(Defining_Entity
(N
));
2397 Env_Installed
: Boolean := False;
2398 Parent_Installed
: Boolean := False;
2399 Renaming_List
: List_Id
;
2400 Unit_Renaming
: Node_Id
;
2401 Needs_Body
: Boolean;
2402 Inline_Now
: Boolean := False;
2404 procedure Delay_Descriptors
(E
: Entity_Id
);
2405 -- Delay generation of subprogram descriptors for given entity
2407 function Might_Inline_Subp
return Boolean;
2408 -- If inlining is active and the generic contains inlined subprograms,
2409 -- we instantiate the body. This may cause superfluous instantiations,
2410 -- but it is simpler than detecting the need for the body at the point
2411 -- of inlining, when the context of the instance is not available.
2413 -----------------------
2414 -- Delay_Descriptors --
2415 -----------------------
2417 procedure Delay_Descriptors
(E
: Entity_Id
) is
2419 if not Delay_Subprogram_Descriptors
(E
) then
2420 Set_Delay_Subprogram_Descriptors
(E
);
2421 Pending_Descriptor
.Increment_Last
;
2422 Pending_Descriptor
.Table
(Pending_Descriptor
.Last
) := E
;
2424 end Delay_Descriptors
;
2426 -----------------------
2427 -- Might_Inline_Subp --
2428 -----------------------
2430 function Might_Inline_Subp
return Boolean is
2434 if not Inline_Processing_Required
then
2438 E
:= First_Entity
(Gen_Unit
);
2439 while Present
(E
) loop
2440 if Is_Subprogram
(E
)
2441 and then Is_Inlined
(E
)
2451 end Might_Inline_Subp
;
2453 -- Start of processing for Analyze_Package_Instantiation
2456 -- Very first thing: apply the special kludge for Text_IO processing
2457 -- in case we are instantiating one of the children of [Wide_]Text_IO.
2459 Text_IO_Kludge
(Name
(N
));
2461 -- Make node global for error reporting
2463 Instantiation_Node
:= N
;
2465 -- Case of instantiation of a generic package
2467 if Nkind
(N
) = N_Package_Instantiation
then
2468 Act_Decl_Id
:= New_Copy
(Defining_Entity
(N
));
2469 Set_Comes_From_Source
(Act_Decl_Id
, True);
2471 if Nkind
(Defining_Unit_Name
(N
)) = N_Defining_Program_Unit_Name
then
2473 Make_Defining_Program_Unit_Name
(Loc
,
2474 Name
=> New_Copy_Tree
(Name
(Defining_Unit_Name
(N
))),
2475 Defining_Identifier
=> Act_Decl_Id
);
2477 Act_Decl_Name
:= Act_Decl_Id
;
2480 -- Case of instantiation of a formal package
2483 Act_Decl_Id
:= Defining_Identifier
(N
);
2484 Act_Decl_Name
:= Act_Decl_Id
;
2487 Generate_Definition
(Act_Decl_Id
);
2488 Pre_Analyze_Actuals
(N
);
2491 Env_Installed
:= True;
2492 Check_Generic_Child_Unit
(Gen_Id
, Parent_Installed
);
2493 Gen_Unit
:= Entity
(Gen_Id
);
2495 -- Verify that it is the name of a generic package
2497 if Etype
(Gen_Unit
) = Any_Type
then
2501 elsif Ekind
(Gen_Unit
) /= E_Generic_Package
then
2503 -- Ada 2005 (AI-50217): Cannot use instance in limited with_clause
2505 if From_With_Type
(Gen_Unit
) then
2507 ("cannot instantiate a limited withed package", Gen_Id
);
2510 ("expect name of generic package in instantiation", Gen_Id
);
2517 if In_Extended_Main_Source_Unit
(N
) then
2518 Set_Is_Instantiated
(Gen_Unit
);
2519 Generate_Reference
(Gen_Unit
, N
);
2521 if Present
(Renamed_Object
(Gen_Unit
)) then
2522 Set_Is_Instantiated
(Renamed_Object
(Gen_Unit
));
2523 Generate_Reference
(Renamed_Object
(Gen_Unit
), N
);
2527 if Nkind
(Gen_Id
) = N_Identifier
2528 and then Chars
(Gen_Unit
) = Chars
(Defining_Entity
(N
))
2531 ("& is hidden within declaration of instance", Gen_Id
, Gen_Unit
);
2533 elsif Nkind
(Gen_Id
) = N_Expanded_Name
2534 and then Is_Child_Unit
(Gen_Unit
)
2535 and then Nkind
(Prefix
(Gen_Id
)) = N_Identifier
2536 and then Chars
(Act_Decl_Id
) = Chars
(Prefix
(Gen_Id
))
2539 ("& is hidden within declaration of instance ", Prefix
(Gen_Id
));
2542 Set_Entity
(Gen_Id
, Gen_Unit
);
2544 -- If generic is a renaming, get original generic unit
2546 if Present
(Renamed_Object
(Gen_Unit
))
2547 and then Ekind
(Renamed_Object
(Gen_Unit
)) = E_Generic_Package
2549 Gen_Unit
:= Renamed_Object
(Gen_Unit
);
2552 -- Verify that there are no circular instantiations
2554 if In_Open_Scopes
(Gen_Unit
) then
2555 Error_Msg_NE
("instantiation of & within itself", N
, Gen_Unit
);
2559 elsif Contains_Instance_Of
(Gen_Unit
, Current_Scope
, Gen_Id
) then
2560 Error_Msg_Node_2
:= Current_Scope
;
2562 ("circular Instantiation: & instantiated in &!", N
, Gen_Unit
);
2563 Circularity_Detected
:= True;
2568 Set_Instance_Env
(Gen_Unit
, Act_Decl_Id
);
2569 Gen_Decl
:= Unit_Declaration_Node
(Gen_Unit
);
2571 -- Initialize renamings map, for error checking, and the list
2572 -- that holds private entities whose views have changed between
2573 -- generic definition and instantiation. If this is the instance
2574 -- created to validate an actual package, the instantiation
2575 -- environment is that of the enclosing instance.
2577 Generic_Renamings
.Set_Last
(0);
2578 Generic_Renamings_HTable
.Reset
;
2580 Create_Instantiation_Source
(N
, Gen_Unit
, False, S_Adjustment
);
2582 -- Copy original generic tree, to produce text for instantiation
2586 (Original_Node
(Gen_Decl
), Empty
, Instantiating
=> True);
2588 Act_Spec
:= Specification
(Act_Tree
);
2590 -- If this is the instance created to validate an actual package,
2591 -- only the formals matter, do not examine the package spec itself.
2593 if Is_Actual_Pack
then
2594 Set_Visible_Declarations
(Act_Spec
, New_List
);
2595 Set_Private_Declarations
(Act_Spec
, New_List
);
2599 Analyze_Associations
2601 Generic_Formal_Declarations
(Act_Tree
),
2602 Generic_Formal_Declarations
(Gen_Decl
));
2604 Set_Defining_Unit_Name
(Act_Spec
, Act_Decl_Name
);
2605 Set_Is_Generic_Instance
(Act_Decl_Id
);
2607 Set_Generic_Parent
(Act_Spec
, Gen_Unit
);
2609 -- References to the generic in its own declaration or its body
2610 -- are references to the instance. Add a renaming declaration for
2611 -- the generic unit itself. This declaration, as well as the renaming
2612 -- declarations for the generic formals, must remain private to the
2613 -- unit: the formals, because this is the language semantics, and
2614 -- the unit because its use is an artifact of the implementation.
2617 Make_Package_Renaming_Declaration
(Loc
,
2618 Defining_Unit_Name
=>
2619 Make_Defining_Identifier
(Loc
, Chars
(Gen_Unit
)),
2620 Name
=> New_Reference_To
(Act_Decl_Id
, Loc
));
2622 Append
(Unit_Renaming
, Renaming_List
);
2624 -- The renaming declarations are the first local declarations of
2627 if Is_Non_Empty_List
(Visible_Declarations
(Act_Spec
)) then
2629 (First
(Visible_Declarations
(Act_Spec
)), Renaming_List
);
2631 Set_Visible_Declarations
(Act_Spec
, Renaming_List
);
2635 Make_Package_Declaration
(Loc
,
2636 Specification
=> Act_Spec
);
2638 -- Save the instantiation node, for subsequent instantiation
2639 -- of the body, if there is one and we are generating code for
2640 -- the current unit. Mark the unit as having a body, to avoid
2641 -- a premature error message.
2643 -- We instantiate the body if we are generating code, if we are
2644 -- generating cross-reference information, or if we are building
2645 -- trees for ASIS use.
2648 Enclosing_Body_Present
: Boolean := False;
2649 -- If the generic unit is not a compilation unit, then a body
2650 -- may be present in its parent even if none is required. We
2651 -- create a tentative pending instantiation for the body, which
2652 -- will be discarded if none is actually present.
2657 if Scope
(Gen_Unit
) /= Standard_Standard
2658 and then not Is_Child_Unit
(Gen_Unit
)
2660 Scop
:= Scope
(Gen_Unit
);
2662 while Present
(Scop
)
2663 and then Scop
/= Standard_Standard
2665 if Unit_Requires_Body
(Scop
) then
2666 Enclosing_Body_Present
:= True;
2669 elsif In_Open_Scopes
(Scop
)
2670 and then In_Package_Body
(Scop
)
2672 Enclosing_Body_Present
:= True;
2676 exit when Is_Compilation_Unit
(Scop
);
2677 Scop
:= Scope
(Scop
);
2681 -- If front-end inlining is enabled, and this is a unit for which
2682 -- code will be generated, we instantiate the body at once.
2683 -- This is done if the instance is not the main unit, and if the
2684 -- generic is not a child unit of another generic, to avoid scope
2685 -- problems and the reinstallation of parent instances.
2688 and then (not Is_Child_Unit
(Gen_Unit
)
2689 or else not Is_Generic_Unit
(Scope
(Gen_Unit
)))
2690 and then Might_Inline_Subp
2691 and then not Is_Actual_Pack
2693 if Front_End_Inlining
2694 and then (Is_In_Main_Unit
(N
)
2695 or else In_Main_Context
(Current_Scope
))
2696 and then Nkind
(Parent
(N
)) /= N_Compilation_Unit
2700 -- In configurable_run_time mode we force the inlining of
2701 -- predefined subprogram marked Inline_Always, to minimize
2702 -- the use of the run-time library.
2704 elsif Is_Predefined_File_Name
2705 (Unit_File_Name
(Get_Source_Unit
(Gen_Decl
)))
2706 and then Configurable_Run_Time_Mode
2707 and then Nkind
(Parent
(N
)) /= N_Compilation_Unit
2712 -- If the current scope is itself an instance within a child
2713 -- unit,there will be duplications in the scope stack, and the
2714 -- unstacking mechanism in Inline_Instance_Body will fail.
2715 -- This loses some rare cases of optimization, and might be
2716 -- improved some day, if we can find a proper abstraction for
2717 -- "the complete compilation context" that can be saved and
2720 if Is_Generic_Instance
(Current_Scope
) then
2722 Curr_Unit
: constant Entity_Id
:=
2723 Cunit_Entity
(Current_Sem_Unit
);
2725 if Curr_Unit
/= Current_Scope
2726 and then Is_Child_Unit
(Curr_Unit
)
2728 Inline_Now
:= False;
2735 (Unit_Requires_Body
(Gen_Unit
)
2736 or else Enclosing_Body_Present
2737 or else Present
(Corresponding_Body
(Gen_Decl
)))
2738 and then (Is_In_Main_Unit
(N
)
2739 or else Might_Inline_Subp
)
2740 and then not Is_Actual_Pack
2741 and then not Inline_Now
2742 and then (Operating_Mode
= Generate_Code
2743 or else (Operating_Mode
= Check_Semantics
2744 and then ASIS_Mode
));
2746 -- If front_end_inlining is enabled, do not instantiate a
2747 -- body if within a generic context.
2749 if (Front_End_Inlining
2750 and then not Expander_Active
)
2751 or else Is_Generic_Unit
(Cunit_Entity
(Main_Unit
))
2753 Needs_Body
:= False;
2756 -- If the current context is generic, and the package being
2757 -- instantiated is declared within a formal package, there is no
2758 -- body to instantiate until the enclosing generic is instantiated
2759 -- and there is an actual for the formal package. If the formal
2760 -- package has parameters, we build regular package instance for
2761 -- it, that preceeds the original formal package declaration.
2763 if In_Open_Scopes
(Scope
(Scope
(Gen_Unit
))) then
2765 Decl
: constant Node_Id
:=
2767 (Unit_Declaration_Node
(Scope
(Gen_Unit
)));
2769 if Nkind
(Decl
) = N_Formal_Package_Declaration
2770 or else (Nkind
(Decl
) = N_Package_Declaration
2771 and then Is_List_Member
(Decl
)
2772 and then Present
(Next
(Decl
))
2774 Nkind
(Next
(Decl
)) = N_Formal_Package_Declaration
)
2776 Needs_Body
:= False;
2782 -- If we are generating the calling stubs from the instantiation of
2783 -- a generic RCI package, we will not use the body of the generic
2786 if Distribution_Stub_Mode
= Generate_Caller_Stub_Body
2787 and then Is_Compilation_Unit
(Defining_Entity
(N
))
2789 Needs_Body
:= False;
2794 -- Here is a defence against a ludicrous number of instantiations
2795 -- caused by a circular set of instantiation attempts.
2797 if Pending_Instantiations
.Last
>
2798 Hostparm
.Max_Instantiations
2800 Error_Msg_N
("too many instantiations", N
);
2801 raise Unrecoverable_Error
;
2804 -- Indicate that the enclosing scopes contain an instantiation,
2805 -- and that cleanup actions should be delayed until after the
2806 -- instance body is expanded.
2808 Check_Forward_Instantiation
(Gen_Decl
);
2809 if Nkind
(N
) = N_Package_Instantiation
then
2811 Enclosing_Master
: Entity_Id
:= Current_Scope
;
2814 while Enclosing_Master
/= Standard_Standard
loop
2816 if Ekind
(Enclosing_Master
) = E_Package
then
2817 if Is_Compilation_Unit
(Enclosing_Master
) then
2818 if In_Package_Body
(Enclosing_Master
) then
2820 (Body_Entity
(Enclosing_Master
));
2829 Enclosing_Master
:= Scope
(Enclosing_Master
);
2832 elsif Ekind
(Enclosing_Master
) = E_Generic_Package
then
2833 Enclosing_Master
:= Scope
(Enclosing_Master
);
2835 elsif Is_Generic_Subprogram
(Enclosing_Master
)
2836 or else Ekind
(Enclosing_Master
) = E_Void
2838 -- Cleanup actions will eventually be performed on
2839 -- the enclosing instance, if any. enclosing scope
2840 -- is void in the formal part of a generic subp.
2845 if Ekind
(Enclosing_Master
) = E_Entry
2847 Ekind
(Scope
(Enclosing_Master
)) = E_Protected_Type
2850 Protected_Body_Subprogram
(Enclosing_Master
);
2853 Set_Delay_Cleanups
(Enclosing_Master
);
2855 while Ekind
(Enclosing_Master
) = E_Block
loop
2856 Enclosing_Master
:= Scope
(Enclosing_Master
);
2859 if Is_Subprogram
(Enclosing_Master
) then
2860 Delay_Descriptors
(Enclosing_Master
);
2862 elsif Is_Task_Type
(Enclosing_Master
) then
2864 TBP
: constant Node_Id
:=
2865 Get_Task_Body_Procedure
2869 if Present
(TBP
) then
2870 Delay_Descriptors
(TBP
);
2871 Set_Delay_Cleanups
(TBP
);
2881 -- Make entry in table
2883 Pending_Instantiations
.Increment_Last
;
2884 Pending_Instantiations
.Table
(Pending_Instantiations
.Last
) :=
2885 (N
, Act_Decl
, Expander_Active
, Current_Sem_Unit
);
2889 Set_Categorization_From_Pragmas
(Act_Decl
);
2891 if Parent_Installed
then
2895 Set_Instance_Spec
(N
, Act_Decl
);
2897 -- If not a compilation unit, insert the package declaration
2898 -- before the original instantiation node.
2900 if Nkind
(Parent
(N
)) /= N_Compilation_Unit
then
2901 Mark_Rewrite_Insertion
(Act_Decl
);
2902 Insert_Before
(N
, Act_Decl
);
2905 -- For an instantiation that is a compilation unit, place
2906 -- declaration on current node so context is complete
2907 -- for analysis (including nested instantiations). It this
2908 -- is the main unit, the declaration eventually replaces the
2909 -- instantiation node. If the instance body is later created, it
2910 -- replaces the instance node, and the declation is attached to
2911 -- it (see Build_Instance_Compilation_Unit_Nodes).
2914 if Cunit_Entity
(Current_Sem_Unit
) = Defining_Entity
(N
) then
2916 -- The entity for the current unit is the newly created one,
2917 -- and all semantic information is attached to it.
2919 Set_Cunit_Entity
(Current_Sem_Unit
, Act_Decl_Id
);
2921 -- If this is the main unit, replace the main entity as well
2923 if Current_Sem_Unit
= Main_Unit
then
2924 Main_Unit_Entity
:= Act_Decl_Id
;
2928 -- There is a problem with inlining here
2929 -- More comments needed??? what problem
2931 Set_Unit
(Parent
(N
), Act_Decl
);
2932 Set_Parent_Spec
(Act_Decl
, Parent_Spec
(N
));
2933 Set_Package_Instantiation
(Act_Decl_Id
, N
);
2935 Set_Unit
(Parent
(N
), N
);
2936 Set_Body_Required
(Parent
(N
), False);
2938 -- We never need elaboration checks on instantiations, since
2939 -- by definition, the body instantiation is elaborated at the
2940 -- same time as the spec instantiation.
2942 Set_Suppress_Elaboration_Warnings
(Act_Decl_Id
);
2943 Set_Kill_Elaboration_Checks
(Act_Decl_Id
);
2946 Check_Elab_Instantiation
(N
);
2948 if ABE_Is_Certain
(N
) and then Needs_Body
then
2949 Pending_Instantiations
.Decrement_Last
;
2951 Check_Hidden_Child_Unit
(N
, Gen_Unit
, Act_Decl_Id
);
2953 Set_First_Private_Entity
(Defining_Unit_Name
(Unit_Renaming
),
2954 First_Private_Entity
(Act_Decl_Id
));
2956 -- If the instantiation will receive a body, the unit will
2957 -- be transformed into a package body, and receive its own
2958 -- elaboration entity. Otherwise, the nature of the unit is
2959 -- now a package declaration.
2961 if Nkind
(Parent
(N
)) = N_Compilation_Unit
2962 and then not Needs_Body
2964 Rewrite
(N
, Act_Decl
);
2967 if Present
(Corresponding_Body
(Gen_Decl
))
2968 or else Unit_Requires_Body
(Gen_Unit
)
2970 Set_Has_Completion
(Act_Decl_Id
);
2973 Check_Formal_Packages
(Act_Decl_Id
);
2975 Restore_Private_Views
(Act_Decl_Id
);
2977 if not Generic_Separately_Compiled
(Gen_Unit
) then
2978 Inherit_Context
(Gen_Decl
, N
);
2981 if Parent_Installed
then
2986 Env_Installed
:= False;
2989 Validate_Categorization_Dependency
(N
, Act_Decl_Id
);
2991 -- Check restriction, but skip this if something went wrong in
2992 -- the above analysis, indicated by Act_Decl_Id being void.
2994 if Ekind
(Act_Decl_Id
) /= E_Void
2995 and then not Is_Library_Level_Entity
(Act_Decl_Id
)
2997 Check_Restriction
(No_Local_Allocators
, N
);
3001 Inline_Instance_Body
(N
, Gen_Unit
, Act_Decl
);
3004 -- The following is a tree patch for ASIS: ASIS needs separate nodes
3005 -- to be used as defining identifiers for a formal package and for the
3006 -- corresponding expanded package
3008 if Nkind
(N
) = N_Formal_Package_Declaration
then
3009 Act_Decl_Id
:= New_Copy
(Defining_Entity
(N
));
3010 Set_Comes_From_Source
(Act_Decl_Id
, True);
3011 Set_Is_Generic_Instance
(Act_Decl_Id
, False);
3012 Set_Defining_Identifier
(N
, Act_Decl_Id
);
3016 when Instantiation_Error
=>
3017 if Parent_Installed
then
3021 if Env_Installed
then
3024 end Analyze_Package_Instantiation
;
3026 --------------------------
3027 -- Inline_Instance_Body --
3028 --------------------------
3030 procedure Inline_Instance_Body
3032 Gen_Unit
: Entity_Id
;
3036 Gen_Comp
: constant Entity_Id
:=
3037 Cunit_Entity
(Get_Source_Unit
(Gen_Unit
));
3038 Curr_Comp
: constant Node_Id
:= Cunit
(Current_Sem_Unit
);
3039 Curr_Scope
: Entity_Id
:= Empty
;
3040 Curr_Unit
: constant Entity_Id
:=
3041 Cunit_Entity
(Current_Sem_Unit
);
3042 Removed
: Boolean := False;
3043 Num_Scopes
: Int
:= 0;
3045 Scope_Stack_Depth
: constant Int
:=
3046 Scope_Stack
.Last
- Scope_Stack
.First
+ 1;
3048 Use_Clauses
: array (1 .. Scope_Stack_Depth
) of Node_Id
;
3049 Instances
: array (1 .. Scope_Stack_Depth
) of Entity_Id
;
3050 Inner_Scopes
: array (1 .. Scope_Stack_Depth
) of Entity_Id
;
3051 Num_Inner
: Int
:= 0;
3052 N_Instances
: Int
:= 0;
3056 -- Case of generic unit defined in another unit. We must remove the
3057 -- complete context of the current unit to install that of the generic.
3059 if Gen_Comp
/= Cunit_Entity
(Current_Sem_Unit
) then
3061 -- Add some comments for the following two loops ???
3064 while Present
(S
) and then S
/= Standard_Standard
loop
3066 Num_Scopes
:= Num_Scopes
+ 1;
3068 Use_Clauses
(Num_Scopes
) :=
3070 (Scope_Stack
.Last
- Num_Scopes
+ 1).
3072 End_Use_Clauses
(Use_Clauses
(Num_Scopes
));
3074 exit when Scope_Stack
.Last
- Num_Scopes
+ 1 = Scope_Stack
.First
3075 or else Scope_Stack
.Table
3076 (Scope_Stack
.Last
- Num_Scopes
).Entity
3080 exit when Is_Generic_Instance
(S
)
3081 and then (In_Package_Body
(S
)
3082 or else Ekind
(S
) = E_Procedure
3083 or else Ekind
(S
) = E_Function
);
3087 Vis
:= Is_Immediately_Visible
(Gen_Comp
);
3089 -- Find and save all enclosing instances
3094 and then S
/= Standard_Standard
3096 if Is_Generic_Instance
(S
) then
3097 N_Instances
:= N_Instances
+ 1;
3098 Instances
(N_Instances
) := S
;
3100 exit when In_Package_Body
(S
);
3106 -- Remove context of current compilation unit, unless we are within a
3107 -- nested package instantiation, in which case the context has been
3108 -- removed previously.
3110 -- If current scope is the body of a child unit, remove context of
3116 and then S
/= Standard_Standard
3118 exit when Is_Generic_Instance
(S
)
3119 and then (In_Package_Body
(S
)
3120 or else Ekind
(S
) = E_Procedure
3121 or else Ekind
(S
) = E_Function
);
3124 or else (Ekind
(Curr_Unit
) = E_Package_Body
3125 and then S
= Spec_Entity
(Curr_Unit
))
3126 or else (Ekind
(Curr_Unit
) = E_Subprogram_Body
3129 (Unit_Declaration_Node
(Curr_Unit
)))
3133 -- Remove entities in current scopes from visibility, so
3134 -- that instance body is compiled in a clean environment.
3136 Save_Scope_Stack
(Handle_Use
=> False);
3138 if Is_Child_Unit
(S
) then
3140 -- Remove child unit from stack, as well as inner scopes.
3141 -- Removing the context of a child unit removes parent
3144 while Current_Scope
/= S
loop
3145 Num_Inner
:= Num_Inner
+ 1;
3146 Inner_Scopes
(Num_Inner
) := Current_Scope
;
3151 Remove_Context
(Curr_Comp
);
3155 Remove_Context
(Curr_Comp
);
3158 if Ekind
(Curr_Unit
) = E_Package_Body
then
3159 Remove_Context
(Library_Unit
(Curr_Comp
));
3165 pragma Assert
(Num_Inner
< Num_Scopes
);
3167 New_Scope
(Standard_Standard
);
3168 Scope_Stack
.Table
(Scope_Stack
.Last
).Is_Active_Stack_Base
:= True;
3169 Instantiate_Package_Body
3170 ((N
, Act_Decl
, Expander_Active
, Current_Sem_Unit
), True);
3175 Set_Is_Immediately_Visible
(Gen_Comp
, Vis
);
3177 -- Reset Generic_Instance flag so that use clauses can be installed
3178 -- in the proper order. (See Use_One_Package for effect of enclosing
3179 -- instances on processing of use clauses).
3181 for J
in 1 .. N_Instances
loop
3182 Set_Is_Generic_Instance
(Instances
(J
), False);
3186 Install_Context
(Curr_Comp
);
3188 if Present
(Curr_Scope
)
3189 and then Is_Child_Unit
(Curr_Scope
)
3191 New_Scope
(Curr_Scope
);
3192 Set_Is_Immediately_Visible
(Curr_Scope
);
3194 -- Finally, restore inner scopes as well
3196 for J
in reverse 1 .. Num_Inner
loop
3197 New_Scope
(Inner_Scopes
(J
));
3201 Restore_Scope_Stack
(Handle_Use
=> False);
3203 if Present
(Curr_Scope
)
3205 (In_Private_Part
(Curr_Scope
)
3206 or else In_Package_Body
(Curr_Scope
))
3208 -- Install private declaration of ancestor units, which
3209 -- are currently available. Restore_Scope_Stack and
3210 -- Install_Context only install the visible part of parents.
3215 Par
:= Scope
(Curr_Scope
);
3216 while (Present
(Par
))
3217 and then Par
/= Standard_Standard
3219 Install_Private_Declarations
(Par
);
3226 -- Restore use clauses. For a child unit, use clauses in the parents
3227 -- are restored when installing the context, so only those in inner
3228 -- scopes (and those local to the child unit itself) need to be
3229 -- installed explicitly.
3231 if Is_Child_Unit
(Curr_Unit
)
3234 for J
in reverse 1 .. Num_Inner
+ 1 loop
3235 Scope_Stack
.Table
(Scope_Stack
.Last
- J
+ 1).First_Use_Clause
:=
3237 Install_Use_Clauses
(Use_Clauses
(J
));
3241 for J
in reverse 1 .. Num_Scopes
loop
3242 Scope_Stack
.Table
(Scope_Stack
.Last
- J
+ 1).First_Use_Clause
:=
3244 Install_Use_Clauses
(Use_Clauses
(J
));
3248 for J
in 1 .. N_Instances
loop
3249 Set_Is_Generic_Instance
(Instances
(J
), True);
3252 -- If generic unit is in current unit, current context is correct
3255 Instantiate_Package_Body
3256 ((N
, Act_Decl
, Expander_Active
, Current_Sem_Unit
), True);
3258 end Inline_Instance_Body
;
3260 -------------------------------------
3261 -- Analyze_Procedure_Instantiation --
3262 -------------------------------------
3264 procedure Analyze_Procedure_Instantiation
(N
: Node_Id
) is
3266 Analyze_Subprogram_Instantiation
(N
, E_Procedure
);
3267 end Analyze_Procedure_Instantiation
;
3269 --------------------------------------
3270 -- Analyze_Subprogram_Instantiation --
3271 --------------------------------------
3273 procedure Analyze_Subprogram_Instantiation
3277 Loc
: constant Source_Ptr
:= Sloc
(N
);
3278 Gen_Id
: constant Node_Id
:= Name
(N
);
3280 Anon_Id
: constant Entity_Id
:=
3281 Make_Defining_Identifier
(Sloc
(Defining_Entity
(N
)),
3282 Chars
=> New_External_Name
3283 (Chars
(Defining_Entity
(N
)), 'R'));
3285 Act_Decl_Id
: Entity_Id
;
3290 Env_Installed
: Boolean := False;
3291 Gen_Unit
: Entity_Id
;
3293 Pack_Id
: Entity_Id
;
3294 Parent_Installed
: Boolean := False;
3295 Renaming_List
: List_Id
;
3297 procedure Analyze_Instance_And_Renamings
;
3298 -- The instance must be analyzed in a context that includes the
3299 -- mappings of generic parameters into actuals. We create a package
3300 -- declaration for this purpose, and a subprogram with an internal
3301 -- name within the package. The subprogram instance is simply an
3302 -- alias for the internal subprogram, declared in the current scope.
3304 ------------------------------------
3305 -- Analyze_Instance_And_Renamings --
3306 ------------------------------------
3308 procedure Analyze_Instance_And_Renamings
is
3309 Def_Ent
: constant Entity_Id
:= Defining_Entity
(N
);
3310 Pack_Decl
: Node_Id
;
3313 if Nkind
(Parent
(N
)) = N_Compilation_Unit
then
3315 -- For the case of a compilation unit, the container package
3316 -- has the same name as the instantiation, to insure that the
3317 -- binder calls the elaboration procedure with the right name.
3318 -- Copy the entity of the instance, which may have compilation
3319 -- level flags (e.g. Is_Child_Unit) set.
3321 Pack_Id
:= New_Copy
(Def_Ent
);
3324 -- Otherwise we use the name of the instantiation concatenated
3325 -- with its source position to ensure uniqueness if there are
3326 -- several instantiations with the same name.
3329 Make_Defining_Identifier
(Loc
,
3330 Chars
=> New_External_Name
3331 (Related_Id
=> Chars
(Def_Ent
),
3333 Suffix_Index
=> Source_Offset
(Sloc
(Def_Ent
))));
3336 Pack_Decl
:= Make_Package_Declaration
(Loc
,
3337 Specification
=> Make_Package_Specification
(Loc
,
3338 Defining_Unit_Name
=> Pack_Id
,
3339 Visible_Declarations
=> Renaming_List
,
3340 End_Label
=> Empty
));
3342 Set_Instance_Spec
(N
, Pack_Decl
);
3343 Set_Is_Generic_Instance
(Pack_Id
);
3344 Set_Needs_Debug_Info
(Pack_Id
);
3346 -- Case of not a compilation unit
3348 if Nkind
(Parent
(N
)) /= N_Compilation_Unit
then
3349 Mark_Rewrite_Insertion
(Pack_Decl
);
3350 Insert_Before
(N
, Pack_Decl
);
3351 Set_Has_Completion
(Pack_Id
);
3353 -- Case of an instantiation that is a compilation unit
3355 -- Place declaration on current node so context is complete
3356 -- for analysis (including nested instantiations), and for
3357 -- use in a context_clause (see Analyze_With_Clause).
3360 Set_Unit
(Parent
(N
), Pack_Decl
);
3361 Set_Parent_Spec
(Pack_Decl
, Parent_Spec
(N
));
3364 Analyze
(Pack_Decl
);
3365 Check_Formal_Packages
(Pack_Id
);
3366 Set_Is_Generic_Instance
(Pack_Id
, False);
3368 -- Body of the enclosing package is supplied when instantiating
3369 -- the subprogram body, after semantic analysis is completed.
3371 if Nkind
(Parent
(N
)) = N_Compilation_Unit
then
3373 -- Remove package itself from visibility, so it does not
3374 -- conflict with subprogram.
3376 Set_Name_Entity_Id
(Chars
(Pack_Id
), Homonym
(Pack_Id
));
3378 -- Set name and scope of internal subprogram so that the
3379 -- proper external name will be generated. The proper scope
3380 -- is the scope of the wrapper package. We need to generate
3381 -- debugging information for the internal subprogram, so set
3382 -- flag accordingly.
3384 Set_Chars
(Anon_Id
, Chars
(Defining_Entity
(N
)));
3385 Set_Scope
(Anon_Id
, Scope
(Pack_Id
));
3387 -- Mark wrapper package as referenced, to avoid spurious
3388 -- warnings if the instantiation appears in various with_
3389 -- clauses of subunits of the main unit.
3391 Set_Referenced
(Pack_Id
);
3394 Set_Is_Generic_Instance
(Anon_Id
);
3395 Set_Needs_Debug_Info
(Anon_Id
);
3396 Act_Decl_Id
:= New_Copy
(Anon_Id
);
3398 Set_Parent
(Act_Decl_Id
, Parent
(Anon_Id
));
3399 Set_Chars
(Act_Decl_Id
, Chars
(Defining_Entity
(N
)));
3400 Set_Sloc
(Act_Decl_Id
, Sloc
(Defining_Entity
(N
)));
3401 Set_Comes_From_Source
(Act_Decl_Id
, True);
3403 -- The signature may involve types that are not frozen yet, but
3404 -- the subprogram will be frozen at the point the wrapper package
3405 -- is frozen, so it does not need its own freeze node. In fact, if
3406 -- one is created, it might conflict with the freezing actions from
3407 -- the wrapper package (see 7206-013).
3409 Set_Has_Delayed_Freeze
(Anon_Id
, False);
3411 -- If the instance is a child unit, mark the Id accordingly. Mark
3412 -- the anonymous entity as well, which is the real subprogram and
3413 -- which is used when the instance appears in a context clause.
3415 Set_Is_Child_Unit
(Act_Decl_Id
, Is_Child_Unit
(Defining_Entity
(N
)));
3416 Set_Is_Child_Unit
(Anon_Id
, Is_Child_Unit
(Defining_Entity
(N
)));
3417 New_Overloaded_Entity
(Act_Decl_Id
);
3418 Check_Eliminated
(Act_Decl_Id
);
3420 -- In compilation unit case, kill elaboration checks on the
3421 -- instantiation, since they are never needed -- the body is
3422 -- instantiated at the same point as the spec.
3424 if Nkind
(Parent
(N
)) = N_Compilation_Unit
then
3425 Set_Suppress_Elaboration_Warnings
(Act_Decl_Id
);
3426 Set_Kill_Elaboration_Checks
(Act_Decl_Id
);
3427 Set_Is_Compilation_Unit
(Anon_Id
);
3429 Set_Cunit_Entity
(Current_Sem_Unit
, Pack_Id
);
3432 -- The instance is not a freezing point for the new subprogram
3434 Set_Is_Frozen
(Act_Decl_Id
, False);
3436 if Nkind
(Defining_Entity
(N
)) = N_Defining_Operator_Symbol
then
3437 Valid_Operator_Definition
(Act_Decl_Id
);
3440 Set_Alias
(Act_Decl_Id
, Anon_Id
);
3441 Set_Parent
(Act_Decl_Id
, Parent
(Anon_Id
));
3442 Set_Has_Completion
(Act_Decl_Id
);
3443 Set_Related_Instance
(Pack_Id
, Act_Decl_Id
);
3445 if Nkind
(Parent
(N
)) = N_Compilation_Unit
then
3446 Set_Body_Required
(Parent
(N
), False);
3449 end Analyze_Instance_And_Renamings
;
3451 -- Start of processing for Analyze_Subprogram_Instantiation
3454 -- Very first thing: apply the special kludge for Text_IO processing
3455 -- in case we are instantiating one of the children of [Wide_]Text_IO.
3456 -- Of course such an instantiation is bogus (these are packages, not
3457 -- subprograms), but we get a better error message if we do this.
3459 Text_IO_Kludge
(Gen_Id
);
3461 -- Make node global for error reporting
3463 Instantiation_Node
:= N
;
3464 Pre_Analyze_Actuals
(N
);
3467 Env_Installed
:= True;
3468 Check_Generic_Child_Unit
(Gen_Id
, Parent_Installed
);
3469 Gen_Unit
:= Entity
(Gen_Id
);
3471 Generate_Reference
(Gen_Unit
, Gen_Id
);
3473 if Nkind
(Gen_Id
) = N_Identifier
3474 and then Chars
(Gen_Unit
) = Chars
(Defining_Entity
(N
))
3477 ("& is hidden within declaration of instance", Gen_Id
, Gen_Unit
);
3480 if Etype
(Gen_Unit
) = Any_Type
then
3485 -- Verify that it is a generic subprogram of the right kind, and that
3486 -- it does not lead to a circular instantiation.
3488 if Ekind
(Gen_Unit
) /= E_Generic_Procedure
3489 and then Ekind
(Gen_Unit
) /= E_Generic_Function
3491 Error_Msg_N
("expect generic subprogram in instantiation", Gen_Id
);
3493 elsif In_Open_Scopes
(Gen_Unit
) then
3494 Error_Msg_NE
("instantiation of & within itself", N
, Gen_Unit
);
3496 elsif K
= E_Procedure
3497 and then Ekind
(Gen_Unit
) /= E_Generic_Procedure
3499 if Ekind
(Gen_Unit
) = E_Generic_Function
then
3501 ("cannot instantiate generic function as procedure", Gen_Id
);
3504 ("expect name of generic procedure in instantiation", Gen_Id
);
3507 elsif K
= E_Function
3508 and then Ekind
(Gen_Unit
) /= E_Generic_Function
3510 if Ekind
(Gen_Unit
) = E_Generic_Procedure
then
3512 ("cannot instantiate generic procedure as function", Gen_Id
);
3515 ("expect name of generic function in instantiation", Gen_Id
);
3519 Set_Entity
(Gen_Id
, Gen_Unit
);
3520 Set_Is_Instantiated
(Gen_Unit
);
3522 if In_Extended_Main_Source_Unit
(N
) then
3523 Generate_Reference
(Gen_Unit
, N
);
3526 -- If renaming, get original unit
3528 if Present
(Renamed_Object
(Gen_Unit
))
3529 and then (Ekind
(Renamed_Object
(Gen_Unit
)) = E_Generic_Procedure
3531 Ekind
(Renamed_Object
(Gen_Unit
)) = E_Generic_Function
)
3533 Gen_Unit
:= Renamed_Object
(Gen_Unit
);
3534 Set_Is_Instantiated
(Gen_Unit
);
3535 Generate_Reference
(Gen_Unit
, N
);
3538 if Contains_Instance_Of
(Gen_Unit
, Current_Scope
, Gen_Id
) then
3539 Error_Msg_Node_2
:= Current_Scope
;
3541 ("circular Instantiation: & instantiated in &!", N
, Gen_Unit
);
3542 Circularity_Detected
:= True;
3546 Gen_Decl
:= Unit_Declaration_Node
(Gen_Unit
);
3548 -- The subprogram itself cannot contain a nested instance, so
3549 -- the current parent is left empty.
3551 Set_Instance_Env
(Gen_Unit
, Empty
);
3553 -- Initialize renamings map, for error checking
3555 Generic_Renamings
.Set_Last
(0);
3556 Generic_Renamings_HTable
.Reset
;
3558 Create_Instantiation_Source
(N
, Gen_Unit
, False, S_Adjustment
);
3560 -- Copy original generic tree, to produce text for instantiation
3564 (Original_Node
(Gen_Decl
), Empty
, Instantiating
=> True);
3566 Act_Spec
:= Specification
(Act_Tree
);
3568 Analyze_Associations
3570 Generic_Formal_Declarations
(Act_Tree
),
3571 Generic_Formal_Declarations
(Gen_Decl
));
3573 -- Build the subprogram declaration, which does not appear
3574 -- in the generic template, and give it a sloc consistent
3575 -- with that of the template.
3577 Set_Defining_Unit_Name
(Act_Spec
, Anon_Id
);
3578 Set_Generic_Parent
(Act_Spec
, Gen_Unit
);
3580 Make_Subprogram_Declaration
(Sloc
(Act_Spec
),
3581 Specification
=> Act_Spec
);
3583 Set_Categorization_From_Pragmas
(Act_Decl
);
3585 if Parent_Installed
then
3589 Append
(Act_Decl
, Renaming_List
);
3590 Analyze_Instance_And_Renamings
;
3592 -- If the generic is marked Import (Intrinsic), then so is the
3593 -- instance. This indicates that there is no body to instantiate.
3594 -- If generic is marked inline, so it the instance, and the
3595 -- anonymous subprogram it renames. If inlined, or else if inlining
3596 -- is enabled for the compilation, we generate the instance body
3597 -- even if it is not within the main unit.
3599 -- Any other pragmas might also be inherited ???
3601 if Is_Intrinsic_Subprogram
(Gen_Unit
) then
3602 Set_Is_Intrinsic_Subprogram
(Anon_Id
);
3603 Set_Is_Intrinsic_Subprogram
(Act_Decl_Id
);
3605 if Chars
(Gen_Unit
) = Name_Unchecked_Conversion
then
3606 Validate_Unchecked_Conversion
(N
, Act_Decl_Id
);
3610 Generate_Definition
(Act_Decl_Id
);
3612 Set_Is_Inlined
(Act_Decl_Id
, Is_Inlined
(Gen_Unit
));
3613 Set_Is_Inlined
(Anon_Id
, Is_Inlined
(Gen_Unit
));
3615 if not Is_Intrinsic_Subprogram
(Gen_Unit
) then
3616 Check_Elab_Instantiation
(N
);
3619 if Is_Dispatching_Operation
(Act_Decl_Id
)
3620 and then Ada_Version
>= Ada_05
3626 Formal
:= First_Formal
(Act_Decl_Id
);
3627 while Present
(Formal
) loop
3628 if Ekind
(Etype
(Formal
)) = E_Anonymous_Access_Type
3629 and then Is_Controlling_Formal
(Formal
)
3630 and then not Can_Never_Be_Null
(Formal
)
3632 Error_Msg_NE
("access parameter& is controlling,",
3634 Error_Msg_NE
("\corresponding parameter of & must be"
3635 & " explicitly null-excluding", N
, Gen_Id
);
3638 Next_Formal
(Formal
);
3643 Check_Hidden_Child_Unit
(N
, Gen_Unit
, Act_Decl_Id
);
3645 -- Subject to change, pending on if other pragmas are inherited ???
3647 Validate_Categorization_Dependency
(N
, Act_Decl_Id
);
3649 if not Is_Intrinsic_Subprogram
(Act_Decl_Id
) then
3650 if not Generic_Separately_Compiled
(Gen_Unit
) then
3651 Inherit_Context
(Gen_Decl
, N
);
3654 Restore_Private_Views
(Pack_Id
, False);
3656 -- If the context requires a full instantiation, mark node for
3657 -- subsequent construction of the body.
3659 if (Is_In_Main_Unit
(N
)
3660 or else Is_Inlined
(Act_Decl_Id
))
3661 and then (Operating_Mode
= Generate_Code
3662 or else (Operating_Mode
= Check_Semantics
3663 and then ASIS_Mode
))
3664 and then (Expander_Active
or else ASIS_Mode
)
3665 and then not ABE_Is_Certain
(N
)
3666 and then not Is_Eliminated
(Act_Decl_Id
)
3668 Pending_Instantiations
.Increment_Last
;
3669 Pending_Instantiations
.Table
(Pending_Instantiations
.Last
) :=
3670 (N
, Act_Decl
, Expander_Active
, Current_Sem_Unit
);
3671 Check_Forward_Instantiation
(Gen_Decl
);
3673 -- The wrapper package is always delayed, because it does
3674 -- not constitute a freeze point, but to insure that the
3675 -- freeze node is placed properly, it is created directly
3676 -- when instantiating the body (otherwise the freeze node
3677 -- might appear to early for nested instantiations).
3679 elsif Nkind
(Parent
(N
)) = N_Compilation_Unit
then
3681 -- For ASIS purposes, indicate that the wrapper package has
3682 -- replaced the instantiation node.
3684 Rewrite
(N
, Unit
(Parent
(N
)));
3685 Set_Unit
(Parent
(N
), N
);
3688 elsif Nkind
(Parent
(N
)) = N_Compilation_Unit
then
3690 -- Replace instance node for library-level instantiations
3691 -- of intrinsic subprograms, for ASIS use.
3693 Rewrite
(N
, Unit
(Parent
(N
)));
3694 Set_Unit
(Parent
(N
), N
);
3697 if Parent_Installed
then
3702 Env_Installed
:= False;
3703 Generic_Renamings
.Set_Last
(0);
3704 Generic_Renamings_HTable
.Reset
;
3708 when Instantiation_Error
=>
3709 if Parent_Installed
then
3713 if Env_Installed
then
3716 end Analyze_Subprogram_Instantiation
;
3718 -------------------------
3719 -- Get_Associated_Node --
3720 -------------------------
3722 function Get_Associated_Node
(N
: Node_Id
) return Node_Id
is
3723 Assoc
: Node_Id
:= Associated_Node
(N
);
3726 if Nkind
(Assoc
) /= Nkind
(N
) then
3729 elsif Nkind
(Assoc
) = N_Aggregate
3730 or else Nkind
(Assoc
) = N_Extension_Aggregate
3735 -- If the node is part of an inner generic, it may itself have been
3736 -- remapped into a further generic copy. Associated_Node is otherwise
3737 -- used for the entity of the node, and will be of a different node
3738 -- kind, or else N has been rewritten as a literal or function call.
3740 while Present
(Associated_Node
(Assoc
))
3741 and then Nkind
(Associated_Node
(Assoc
)) = Nkind
(Assoc
)
3743 Assoc
:= Associated_Node
(Assoc
);
3746 -- Follow and additional link in case the final node was rewritten.
3747 -- This can only happen with nested generic units.
3749 if (Nkind
(Assoc
) = N_Identifier
or else Nkind
(Assoc
) in N_Op
)
3750 and then Present
(Associated_Node
(Assoc
))
3751 and then (Nkind
(Associated_Node
(Assoc
)) = N_Function_Call
3753 Nkind
(Associated_Node
(Assoc
)) = N_Explicit_Dereference
3755 Nkind
(Associated_Node
(Assoc
)) = N_Integer_Literal
3757 Nkind
(Associated_Node
(Assoc
)) = N_Real_Literal
3759 Nkind
(Associated_Node
(Assoc
)) = N_String_Literal
)
3761 Assoc
:= Associated_Node
(Assoc
);
3766 end Get_Associated_Node
;
3768 -------------------------------------------
3769 -- Build_Instance_Compilation_Unit_Nodes --
3770 -------------------------------------------
3772 procedure Build_Instance_Compilation_Unit_Nodes
3777 Decl_Cunit
: Node_Id
;
3778 Body_Cunit
: Node_Id
;
3780 New_Main
: constant Entity_Id
:= Defining_Entity
(Act_Decl
);
3781 Old_Main
: constant Entity_Id
:= Cunit_Entity
(Main_Unit
);
3784 -- A new compilation unit node is built for the instance declaration
3787 Make_Compilation_Unit
(Sloc
(N
),
3788 Context_Items
=> Empty_List
,
3791 Make_Compilation_Unit_Aux
(Sloc
(N
)));
3793 Set_Parent_Spec
(Act_Decl
, Parent_Spec
(N
));
3794 Set_Body_Required
(Decl_Cunit
, True);
3796 -- We use the original instantiation compilation unit as the resulting
3797 -- compilation unit of the instance, since this is the main unit.
3799 Rewrite
(N
, Act_Body
);
3800 Body_Cunit
:= Parent
(N
);
3802 -- The two compilation unit nodes are linked by the Library_Unit field
3804 Set_Library_Unit
(Decl_Cunit
, Body_Cunit
);
3805 Set_Library_Unit
(Body_Cunit
, Decl_Cunit
);
3807 -- Preserve the private nature of the package if needed
3809 Set_Private_Present
(Decl_Cunit
, Private_Present
(Body_Cunit
));
3811 -- If the instance is not the main unit, its context, categorization,
3812 -- and elaboration entity are not relevant to the compilation.
3814 if Parent
(N
) /= Cunit
(Main_Unit
) then
3818 -- The context clause items on the instantiation, which are now
3819 -- attached to the body compilation unit (since the body overwrote
3820 -- the original instantiation node), semantically belong on the spec,
3821 -- so copy them there. It's harmless to leave them on the body as well.
3822 -- In fact one could argue that they belong in both places.
3824 Citem
:= First
(Context_Items
(Body_Cunit
));
3825 while Present
(Citem
) loop
3826 Append
(New_Copy
(Citem
), Context_Items
(Decl_Cunit
));
3830 -- Propagate categorization flags on packages, so that they appear
3831 -- in ali file for the spec of the unit.
3833 if Ekind
(New_Main
) = E_Package
then
3834 Set_Is_Pure
(Old_Main
, Is_Pure
(New_Main
));
3835 Set_Is_Preelaborated
(Old_Main
, Is_Preelaborated
(New_Main
));
3836 Set_Is_Remote_Types
(Old_Main
, Is_Remote_Types
(New_Main
));
3837 Set_Is_Shared_Passive
(Old_Main
, Is_Shared_Passive
(New_Main
));
3838 Set_Is_Remote_Call_Interface
3839 (Old_Main
, Is_Remote_Call_Interface
(New_Main
));
3842 -- Make entry in Units table, so that binder can generate call to
3843 -- elaboration procedure for body, if any.
3845 Make_Instance_Unit
(Body_Cunit
);
3846 Main_Unit_Entity
:= New_Main
;
3847 Set_Cunit_Entity
(Main_Unit
, Main_Unit_Entity
);
3849 -- Build elaboration entity, since the instance may certainly
3850 -- generate elaboration code requiring a flag for protection.
3852 Build_Elaboration_Entity
(Decl_Cunit
, New_Main
);
3853 end Build_Instance_Compilation_Unit_Nodes
;
3855 -----------------------------------
3856 -- Check_Formal_Package_Instance --
3857 -----------------------------------
3859 -- If the formal has specific parameters, they must match those of the
3860 -- actual. Both of them are instances, and the renaming declarations
3861 -- for their formal parameters appear in the same order in both. The
3862 -- analyzed formal has been analyzed in the context of the current
3865 procedure Check_Formal_Package_Instance
3866 (Formal_Pack
: Entity_Id
;
3867 Actual_Pack
: Entity_Id
)
3869 E1
: Entity_Id
:= First_Entity
(Actual_Pack
);
3870 E2
: Entity_Id
:= First_Entity
(Formal_Pack
);
3875 procedure Check_Mismatch
(B
: Boolean);
3876 -- Common error routine for mismatch between the parameters of
3877 -- the actual instance and those of the formal package.
3879 function Same_Instantiated_Constant
(E1
, E2
: Entity_Id
) return Boolean;
3880 -- The formal may come from a nested formal package, and the actual
3881 -- may have been constant-folded. To determine whether the two denote
3882 -- the same entity we may have to traverse several definitions to
3883 -- recover the ultimate entity that they refer to.
3885 function Same_Instantiated_Variable
(E1
, E2
: Entity_Id
) return Boolean;
3886 -- Similarly, if the formal comes from a nested formal package, the
3887 -- actual may designate the formal through multiple renamings, which
3888 -- have to be followed to determine the original variable in question.
3890 --------------------
3891 -- Check_Mismatch --
3892 --------------------
3894 procedure Check_Mismatch
(B
: Boolean) is
3898 ("actual for & in actual instance does not match formal",
3899 Parent
(Actual_Pack
), E1
);
3903 --------------------------------
3904 -- Same_Instantiated_Constant --
3905 --------------------------------
3907 function Same_Instantiated_Constant
3908 (E1
, E2
: Entity_Id
) return Boolean
3914 while Present
(Ent
) loop
3918 elsif Ekind
(Ent
) /= E_Constant
then
3921 elsif Is_Entity_Name
(Constant_Value
(Ent
)) then
3922 if Entity
(Constant_Value
(Ent
)) = E1
then
3925 Ent
:= Entity
(Constant_Value
(Ent
));
3928 -- The actual may be a constant that has been folded. Recover
3931 elsif Is_Entity_Name
(Original_Node
(Constant_Value
(Ent
))) then
3932 Ent
:= Entity
(Original_Node
(Constant_Value
(Ent
)));
3939 end Same_Instantiated_Constant
;
3941 --------------------------------
3942 -- Same_Instantiated_Variable --
3943 --------------------------------
3945 function Same_Instantiated_Variable
3946 (E1
, E2
: Entity_Id
) return Boolean
3948 function Original_Entity
(E
: Entity_Id
) return Entity_Id
;
3949 -- Follow chain of renamings to the ultimate ancestor
3951 ---------------------
3952 -- Original_Entity --
3953 ---------------------
3955 function Original_Entity
(E
: Entity_Id
) return Entity_Id
is
3960 while Nkind
(Parent
(Orig
)) = N_Object_Renaming_Declaration
3961 and then Present
(Renamed_Object
(Orig
))
3962 and then Is_Entity_Name
(Renamed_Object
(Orig
))
3964 Orig
:= Entity
(Renamed_Object
(Orig
));
3968 end Original_Entity
;
3970 -- Start of processing for Same_Instantiated_Variable
3973 return Ekind
(E1
) = Ekind
(E2
)
3974 and then Original_Entity
(E1
) = Original_Entity
(E2
);
3975 end Same_Instantiated_Variable
;
3977 -- Start of processing for Check_Formal_Package_Instance
3981 and then Present
(E2
)
3983 exit when Ekind
(E1
) = E_Package
3984 and then Renamed_Entity
(E1
) = Renamed_Entity
(Actual_Pack
);
3986 if Is_Type
(E1
) then
3988 -- Subtypes must statically match. E1 and E2 are the
3989 -- local entities that are subtypes of the actuals.
3990 -- Itypes generated for other parameters need not be checked,
3991 -- the check will be performed on the parameters themselves.
3993 if not Is_Itype
(E1
)
3994 and then not Is_Itype
(E2
)
3998 or else Etype
(E1
) /= Etype
(E2
)
3999 or else not Subtypes_Statically_Match
(E1
, E2
));
4002 elsif Ekind
(E1
) = E_Constant
then
4004 -- IN parameters must denote the same static value, or
4005 -- the same constant, or the literal null.
4007 Expr1
:= Expression
(Parent
(E1
));
4009 if Ekind
(E2
) /= E_Constant
then
4010 Check_Mismatch
(True);
4013 Expr2
:= Expression
(Parent
(E2
));
4016 if Is_Static_Expression
(Expr1
) then
4018 if not Is_Static_Expression
(Expr2
) then
4019 Check_Mismatch
(True);
4021 elsif Is_Integer_Type
(Etype
(E1
)) then
4024 V1
: constant Uint
:= Expr_Value
(Expr1
);
4025 V2
: constant Uint
:= Expr_Value
(Expr2
);
4027 Check_Mismatch
(V1
/= V2
);
4030 elsif Is_Real_Type
(Etype
(E1
)) then
4032 V1
: constant Ureal
:= Expr_Value_R
(Expr1
);
4033 V2
: constant Ureal
:= Expr_Value_R
(Expr2
);
4035 Check_Mismatch
(V1
/= V2
);
4038 elsif Is_String_Type
(Etype
(E1
))
4039 and then Nkind
(Expr1
) = N_String_Literal
4042 if Nkind
(Expr2
) /= N_String_Literal
then
4043 Check_Mismatch
(True);
4046 (not String_Equal
(Strval
(Expr1
), Strval
(Expr2
)));
4050 elsif Is_Entity_Name
(Expr1
) then
4051 if Is_Entity_Name
(Expr2
) then
4052 if Entity
(Expr1
) = Entity
(Expr2
) then
4056 (not Same_Instantiated_Constant
4057 (Entity
(Expr1
), Entity
(Expr2
)));
4060 Check_Mismatch
(True);
4063 elsif Is_Entity_Name
(Original_Node
(Expr1
))
4064 and then Is_Entity_Name
(Expr2
)
4066 Same_Instantiated_Constant
4067 (Entity
(Original_Node
(Expr1
)), Entity
(Expr2
))
4071 elsif Nkind
(Expr1
) = N_Null
then
4072 Check_Mismatch
(Nkind
(Expr1
) /= N_Null
);
4075 Check_Mismatch
(True);
4078 elsif Ekind
(E1
) = E_Variable
then
4079 Check_Mismatch
(not Same_Instantiated_Variable
(E1
, E2
));
4081 elsif Ekind
(E1
) = E_Package
then
4083 (Ekind
(E1
) /= Ekind
(E2
)
4084 or else Renamed_Object
(E1
) /= Renamed_Object
(E2
));
4086 elsif Is_Overloadable
(E1
) then
4088 -- Verify that the names of the entities match.
4089 -- What if actual is an attribute ???
4092 (Ekind
(E2
) /= Ekind
(E1
) or else (Alias
(E1
)) /= Alias
(E2
));
4095 raise Program_Error
;
4102 end Check_Formal_Package_Instance
;
4104 ---------------------------
4105 -- Check_Formal_Packages --
4106 ---------------------------
4108 procedure Check_Formal_Packages
(P_Id
: Entity_Id
) is
4110 Formal_P
: Entity_Id
;
4113 -- Iterate through the declarations in the instance, looking for
4114 -- package renaming declarations that denote instances of formal
4115 -- packages. Stop when we find the renaming of the current package
4116 -- itself. The declaration for a formal package without a box is
4117 -- followed by an internal entity that repeats the instantiation.
4119 E
:= First_Entity
(P_Id
);
4120 while Present
(E
) loop
4121 if Ekind
(E
) = E_Package
then
4122 if Renamed_Object
(E
) = P_Id
then
4125 elsif Nkind
(Parent
(E
)) /= N_Package_Renaming_Declaration
then
4128 elsif not Box_Present
(Parent
(Associated_Formal_Package
(E
))) then
4129 Formal_P
:= Next_Entity
(E
);
4130 Check_Formal_Package_Instance
(Formal_P
, E
);
4136 end Check_Formal_Packages
;
4138 ---------------------------------
4139 -- Check_Forward_Instantiation --
4140 ---------------------------------
4142 procedure Check_Forward_Instantiation
(Decl
: Node_Id
) is
4144 Gen_Comp
: Entity_Id
:= Cunit_Entity
(Get_Source_Unit
(Decl
));
4147 -- The instantiation appears before the generic body if we are in the
4148 -- scope of the unit containing the generic, either in its spec or in
4149 -- the package body. and before the generic body.
4151 if Ekind
(Gen_Comp
) = E_Package_Body
then
4152 Gen_Comp
:= Spec_Entity
(Gen_Comp
);
4155 if In_Open_Scopes
(Gen_Comp
)
4156 and then No
(Corresponding_Body
(Decl
))
4161 and then not Is_Compilation_Unit
(S
)
4162 and then not Is_Child_Unit
(S
)
4164 if Ekind
(S
) = E_Package
then
4165 Set_Has_Forward_Instantiation
(S
);
4171 end Check_Forward_Instantiation
;
4173 ---------------------------
4174 -- Check_Generic_Actuals --
4175 ---------------------------
4177 -- The visibility of the actuals may be different between the
4178 -- point of generic instantiation and the instantiation of the body.
4180 procedure Check_Generic_Actuals
4181 (Instance
: Entity_Id
;
4182 Is_Formal_Box
: Boolean)
4187 function Denotes_Previous_Actual
(Typ
: Entity_Id
) return Boolean;
4188 -- For a formal that is an array type, the component type is often
4189 -- a previous formal in the same unit. The privacy status of the
4190 -- component type will have been examined earlier in the traversal
4191 -- of the corresponding actuals, and this status should not be
4192 -- modified for the array type itself.
4193 -- To detect this case we have to rescan the list of formals, which
4194 -- is usually short enough to ignore the resulting inefficiency.
4196 function Denotes_Previous_Actual
(Typ
: Entity_Id
) return Boolean is
4199 Prev
:= First_Entity
(Instance
);
4200 while Present
(Prev
) loop
4202 and then Nkind
(Parent
(Prev
)) = N_Subtype_Declaration
4203 and then Is_Entity_Name
(Subtype_Indication
(Parent
(Prev
)))
4204 and then Entity
(Subtype_Indication
(Parent
(Prev
))) = Typ
4214 end Denotes_Previous_Actual
;
4216 -- Start of processing for Check_Generic_Actuals
4219 E
:= First_Entity
(Instance
);
4220 while Present
(E
) loop
4222 and then Nkind
(Parent
(E
)) = N_Subtype_Declaration
4223 and then Scope
(Etype
(E
)) /= Instance
4224 and then Is_Entity_Name
(Subtype_Indication
(Parent
(E
)))
4226 if Is_Array_Type
(E
)
4227 and then Denotes_Previous_Actual
(Component_Type
(E
))
4231 Check_Private_View
(Subtype_Indication
(Parent
(E
)));
4233 Set_Is_Generic_Actual_Type
(E
, True);
4234 Set_Is_Hidden
(E
, False);
4235 Set_Is_Potentially_Use_Visible
(E
,
4238 -- We constructed the generic actual type as a subtype of
4239 -- the supplied type. This means that it normally would not
4240 -- inherit subtype specific attributes of the actual, which
4241 -- is wrong for the generic case.
4243 Astype
:= Ancestor_Subtype
(E
);
4247 -- can happen when E is an itype that is the full view of
4248 -- a private type completed, e.g. with a constrained array.
4250 Astype
:= Base_Type
(E
);
4253 Set_Size_Info
(E
, (Astype
));
4254 Set_RM_Size
(E
, RM_Size
(Astype
));
4255 Set_First_Rep_Item
(E
, First_Rep_Item
(Astype
));
4257 if Is_Discrete_Or_Fixed_Point_Type
(E
) then
4258 Set_RM_Size
(E
, RM_Size
(Astype
));
4260 -- In nested instances, the base type of an access actual
4261 -- may itself be private, and need to be exchanged.
4263 elsif Is_Access_Type
(E
)
4264 and then Is_Private_Type
(Etype
(E
))
4267 (New_Occurrence_Of
(Etype
(E
), Sloc
(Instance
)));
4270 elsif Ekind
(E
) = E_Package
then
4272 -- If this is the renaming for the current instance, we're done.
4273 -- Otherwise it is a formal package. If the corresponding formal
4274 -- was declared with a box, the (instantiations of the) generic
4275 -- formal part are also visible. Otherwise, ignore the entity
4276 -- created to validate the actuals.
4278 if Renamed_Object
(E
) = Instance
then
4281 elsif Nkind
(Parent
(E
)) /= N_Package_Renaming_Declaration
then
4284 -- The visibility of a formal of an enclosing generic is already
4287 elsif Denotes_Formal_Package
(E
) then
4290 elsif Present
(Associated_Formal_Package
(E
)) then
4291 if Box_Present
(Parent
(Associated_Formal_Package
(E
))) then
4292 Check_Generic_Actuals
(Renamed_Object
(E
), True);
4295 Set_Is_Hidden
(E
, False);
4298 -- If this is a subprogram instance (in a wrapper package) the
4299 -- actual is fully visible.
4301 elsif Is_Wrapper_Package
(Instance
) then
4302 Set_Is_Hidden
(E
, False);
4305 Set_Is_Hidden
(E
, not Is_Formal_Box
);
4310 end Check_Generic_Actuals
;
4312 ------------------------------
4313 -- Check_Generic_Child_Unit --
4314 ------------------------------
4316 procedure Check_Generic_Child_Unit
4318 Parent_Installed
: in out Boolean)
4320 Loc
: constant Source_Ptr
:= Sloc
(Gen_Id
);
4321 Gen_Par
: Entity_Id
:= Empty
;
4322 Inst_Par
: Entity_Id
;
4326 function Find_Generic_Child
4328 Id
: Node_Id
) return Entity_Id
;
4329 -- Search generic parent for possible child unit with the given name
4331 function In_Enclosing_Instance
return Boolean;
4332 -- Within an instance of the parent, the child unit may be denoted
4333 -- by a simple name, or an abbreviated expanded name. Examine enclosing
4334 -- scopes to locate a possible parent instantiation.
4336 ------------------------
4337 -- Find_Generic_Child --
4338 ------------------------
4340 function Find_Generic_Child
4342 Id
: Node_Id
) return Entity_Id
4347 -- If entity of name is already set, instance has already been
4348 -- resolved, e.g. in an enclosing instantiation.
4350 if Present
(Entity
(Id
)) then
4351 if Scope
(Entity
(Id
)) = Scop
then
4358 E
:= First_Entity
(Scop
);
4359 while Present
(E
) loop
4360 if Chars
(E
) = Chars
(Id
)
4361 and then Is_Child_Unit
(E
)
4363 if Is_Child_Unit
(E
)
4364 and then not Is_Visible_Child_Unit
(E
)
4367 ("generic child unit& is not visible", Gen_Id
, E
);
4379 end Find_Generic_Child
;
4381 ---------------------------
4382 -- In_Enclosing_Instance --
4383 ---------------------------
4385 function In_Enclosing_Instance
return Boolean is
4386 Enclosing_Instance
: Node_Id
;
4387 Instance_Decl
: Node_Id
;
4390 -- We do not inline any call that contains instantiations, except
4391 -- for instantiations of Unchecked_Conversion, so if we are within
4392 -- an inlined body the current instance does not require parents.
4394 if In_Inlined_Body
then
4395 pragma Assert
(Chars
(Gen_Id
) = Name_Unchecked_Conversion
);
4399 -- Loop to check enclosing scopes
4401 Enclosing_Instance
:= Current_Scope
;
4402 while Present
(Enclosing_Instance
) loop
4403 Instance_Decl
:= Unit_Declaration_Node
(Enclosing_Instance
);
4405 if Ekind
(Enclosing_Instance
) = E_Package
4406 and then Is_Generic_Instance
(Enclosing_Instance
)
4408 (Generic_Parent
(Specification
(Instance_Decl
)))
4410 -- Check whether the generic we are looking for is a child
4411 -- of this instance.
4413 E
:= Find_Generic_Child
4414 (Generic_Parent
(Specification
(Instance_Decl
)), Gen_Id
);
4415 exit when Present
(E
);
4421 Enclosing_Instance
:= Scope
(Enclosing_Instance
);
4433 Make_Expanded_Name
(Loc
,
4435 Prefix
=> New_Occurrence_Of
(Enclosing_Instance
, Loc
),
4436 Selector_Name
=> New_Occurrence_Of
(E
, Loc
)));
4438 Set_Entity
(Gen_Id
, E
);
4439 Set_Etype
(Gen_Id
, Etype
(E
));
4440 Parent_Installed
:= False; -- Already in scope.
4443 end In_Enclosing_Instance
;
4445 -- Start of processing for Check_Generic_Child_Unit
4448 -- If the name of the generic is given by a selected component, it
4449 -- may be the name of a generic child unit, and the prefix is the name
4450 -- of an instance of the parent, in which case the child unit must be
4451 -- visible. If this instance is not in scope, it must be placed there
4452 -- and removed after instantiation, because what is being instantiated
4453 -- is not the original child, but the corresponding child present in
4454 -- the instance of the parent.
4456 -- If the child is instantiated within the parent, it can be given by
4457 -- a simple name. In this case the instance is already in scope, but
4458 -- the child generic must be recovered from the generic parent as well.
4460 if Nkind
(Gen_Id
) = N_Selected_Component
then
4461 S
:= Selector_Name
(Gen_Id
);
4462 Analyze
(Prefix
(Gen_Id
));
4463 Inst_Par
:= Entity
(Prefix
(Gen_Id
));
4465 if Ekind
(Inst_Par
) = E_Package
4466 and then Present
(Renamed_Object
(Inst_Par
))
4468 Inst_Par
:= Renamed_Object
(Inst_Par
);
4471 if Ekind
(Inst_Par
) = E_Package
then
4472 if Nkind
(Parent
(Inst_Par
)) = N_Package_Specification
then
4473 Gen_Par
:= Generic_Parent
(Parent
(Inst_Par
));
4475 elsif Nkind
(Parent
(Inst_Par
)) = N_Defining_Program_Unit_Name
4477 Nkind
(Parent
(Parent
(Inst_Par
))) = N_Package_Specification
4479 Gen_Par
:= Generic_Parent
(Parent
(Parent
(Inst_Par
)));
4482 elsif Ekind
(Inst_Par
) = E_Generic_Package
4483 and then Nkind
(Parent
(Gen_Id
)) = N_Formal_Package_Declaration
4485 -- A formal package may be a real child package, and not the
4486 -- implicit instance within a parent. In this case the child is
4487 -- not visible and has to be retrieved explicitly as well.
4489 Gen_Par
:= Inst_Par
;
4492 if Present
(Gen_Par
) then
4494 -- The prefix denotes an instantiation. The entity itself
4495 -- may be a nested generic, or a child unit.
4497 E
:= Find_Generic_Child
(Gen_Par
, S
);
4500 Change_Selected_Component_To_Expanded_Name
(Gen_Id
);
4501 Set_Entity
(Gen_Id
, E
);
4502 Set_Etype
(Gen_Id
, Etype
(E
));
4504 Set_Etype
(S
, Etype
(E
));
4506 -- Indicate that this is a reference to the parent
4508 if In_Extended_Main_Source_Unit
(Gen_Id
) then
4509 Set_Is_Instantiated
(Inst_Par
);
4512 -- A common mistake is to replicate the naming scheme of
4513 -- a hierarchy by instantiating a generic child directly,
4514 -- rather than the implicit child in a parent instance:
4516 -- generic .. package Gpar is ..
4517 -- generic .. package Gpar.Child is ..
4518 -- package Par is new Gpar ();
4521 -- package Par.Child is new Gpar.Child ();
4522 -- rather than Par.Child
4524 -- In this case the instantiation is within Par, which is
4525 -- an instance, but Gpar does not denote Par because we are
4526 -- not IN the instance of Gpar, so this is illegal. The test
4527 -- below recognizes this particular case.
4529 if Is_Child_Unit
(E
)
4530 and then not Comes_From_Source
(Entity
(Prefix
(Gen_Id
)))
4531 and then (not In_Instance
4532 or else Nkind
(Parent
(Parent
(Gen_Id
))) =
4536 ("prefix of generic child unit must be instance of parent",
4540 if not In_Open_Scopes
(Inst_Par
)
4541 and then Nkind
(Parent
(Gen_Id
)) not in
4542 N_Generic_Renaming_Declaration
4544 Install_Parent
(Inst_Par
);
4545 Parent_Installed
:= True;
4549 -- If the generic parent does not contain an entity that
4550 -- corresponds to the selector, the instance doesn't either.
4551 -- Analyzing the node will yield the appropriate error message.
4552 -- If the entity is not a child unit, then it is an inner
4553 -- generic in the parent.
4561 if Is_Child_Unit
(Entity
(Gen_Id
))
4563 Nkind
(Parent
(Gen_Id
)) not in N_Generic_Renaming_Declaration
4564 and then not In_Open_Scopes
(Inst_Par
)
4566 Install_Parent
(Inst_Par
);
4567 Parent_Installed
:= True;
4571 elsif Nkind
(Gen_Id
) = N_Expanded_Name
then
4573 -- Entity already present, analyze prefix, whose meaning may be
4574 -- an instance in the current context. If it is an instance of
4575 -- a relative within another, the proper parent may still have
4576 -- to be installed, if they are not of the same generation.
4578 Analyze
(Prefix
(Gen_Id
));
4579 Inst_Par
:= Entity
(Prefix
(Gen_Id
));
4581 if In_Enclosing_Instance
then
4584 elsif Present
(Entity
(Gen_Id
))
4585 and then Is_Child_Unit
(Entity
(Gen_Id
))
4586 and then not In_Open_Scopes
(Inst_Par
)
4588 Install_Parent
(Inst_Par
);
4589 Parent_Installed
:= True;
4592 elsif In_Enclosing_Instance
then
4594 -- The child unit is found in some enclosing scope
4601 -- If this is the renaming of the implicit child in a parent
4602 -- instance, recover the parent name and install it.
4604 if Is_Entity_Name
(Gen_Id
) then
4605 E
:= Entity
(Gen_Id
);
4607 if Is_Generic_Unit
(E
)
4608 and then Nkind
(Parent
(E
)) in N_Generic_Renaming_Declaration
4609 and then Is_Child_Unit
(Renamed_Object
(E
))
4610 and then Is_Generic_Unit
(Scope
(Renamed_Object
(E
)))
4611 and then Nkind
(Name
(Parent
(E
))) = N_Expanded_Name
4614 New_Copy_Tree
(Name
(Parent
(E
))));
4615 Inst_Par
:= Entity
(Prefix
(Gen_Id
));
4617 if not In_Open_Scopes
(Inst_Par
) then
4618 Install_Parent
(Inst_Par
);
4619 Parent_Installed
:= True;
4622 -- If it is a child unit of a non-generic parent, it may be
4623 -- use-visible and given by a direct name. Install parent as
4626 elsif Is_Generic_Unit
(E
)
4627 and then Is_Child_Unit
(E
)
4629 Nkind
(Parent
(Gen_Id
)) not in N_Generic_Renaming_Declaration
4630 and then not Is_Generic_Unit
(Scope
(E
))
4632 if not In_Open_Scopes
(Scope
(E
)) then
4633 Install_Parent
(Scope
(E
));
4634 Parent_Installed
:= True;
4639 end Check_Generic_Child_Unit
;
4641 -----------------------------
4642 -- Check_Hidden_Child_Unit --
4643 -----------------------------
4645 procedure Check_Hidden_Child_Unit
4647 Gen_Unit
: Entity_Id
;
4648 Act_Decl_Id
: Entity_Id
)
4650 Gen_Id
: constant Node_Id
:= Name
(N
);
4653 if Is_Child_Unit
(Gen_Unit
)
4654 and then Is_Child_Unit
(Act_Decl_Id
)
4655 and then Nkind
(Gen_Id
) = N_Expanded_Name
4656 and then Entity
(Prefix
(Gen_Id
)) = Scope
(Act_Decl_Id
)
4657 and then Chars
(Gen_Unit
) = Chars
(Act_Decl_Id
)
4659 Error_Msg_Node_2
:= Scope
(Act_Decl_Id
);
4661 ("generic unit & is implicitly declared in &",
4662 Defining_Unit_Name
(N
), Gen_Unit
);
4663 Error_Msg_N
("\instance must have different name",
4664 Defining_Unit_Name
(N
));
4666 end Check_Hidden_Child_Unit
;
4668 ------------------------
4669 -- Check_Private_View --
4670 ------------------------
4672 procedure Check_Private_View
(N
: Node_Id
) is
4673 T
: constant Entity_Id
:= Etype
(N
);
4677 -- Exchange views if the type was not private in the generic but is
4678 -- private at the point of instantiation. Do not exchange views if
4679 -- the scope of the type is in scope. This can happen if both generic
4680 -- and instance are sibling units, or if type is defined in a parent.
4681 -- In this case the visibility of the type will be correct for all
4685 BT
:= Base_Type
(T
);
4687 if Is_Private_Type
(T
)
4688 and then not Has_Private_View
(N
)
4689 and then Present
(Full_View
(T
))
4690 and then not In_Open_Scopes
(Scope
(T
))
4692 -- In the generic, the full type was visible. Save the
4693 -- private entity, for subsequent exchange.
4697 elsif Has_Private_View
(N
)
4698 and then not Is_Private_Type
(T
)
4699 and then not Has_Been_Exchanged
(T
)
4700 and then Etype
(Get_Associated_Node
(N
)) /= T
4702 -- Only the private declaration was visible in the generic. If
4703 -- the type appears in a subtype declaration, the subtype in the
4704 -- instance must have a view compatible with that of its parent,
4705 -- which must be exchanged (see corresponding code in Restore_
4706 -- Private_Views). Otherwise, if the type is defined in a parent
4707 -- unit, leave full visibility within instance, which is safe.
4709 if In_Open_Scopes
(Scope
(Base_Type
(T
)))
4710 and then not Is_Private_Type
(Base_Type
(T
))
4711 and then Comes_From_Source
(Base_Type
(T
))
4715 elsif Nkind
(Parent
(N
)) = N_Subtype_Declaration
4716 or else not In_Private_Part
(Scope
(Base_Type
(T
)))
4718 Prepend_Elmt
(T
, Exchanged_Views
);
4719 Exchange_Declarations
(Etype
(Get_Associated_Node
(N
)));
4722 -- For composite types with inconsistent representation
4723 -- exchange component types accordingly.
4725 elsif Is_Access_Type
(T
)
4726 and then Is_Private_Type
(Designated_Type
(T
))
4727 and then not Has_Private_View
(N
)
4728 and then Present
(Full_View
(Designated_Type
(T
)))
4730 Switch_View
(Designated_Type
(T
));
4732 elsif Is_Array_Type
(T
)
4733 and then Is_Private_Type
(Component_Type
(T
))
4734 and then not Has_Private_View
(N
)
4735 and then Present
(Full_View
(Component_Type
(T
)))
4737 Switch_View
(Component_Type
(T
));
4739 elsif Is_Private_Type
(T
)
4740 and then Present
(Full_View
(T
))
4741 and then Is_Array_Type
(Full_View
(T
))
4742 and then Is_Private_Type
(Component_Type
(Full_View
(T
)))
4746 -- Finally, a non-private subtype may have a private base type,
4747 -- which must be exchanged for consistency. This can happen when
4748 -- instantiating a package body, when the scope stack is empty
4749 -- but in fact the subtype and the base type are declared in an
4752 elsif not Is_Private_Type
(T
)
4753 and then not Has_Private_View
(N
)
4754 and then Is_Private_Type
(Base_Type
(T
))
4755 and then Present
(Full_View
(BT
))
4756 and then not Is_Generic_Type
(BT
)
4757 and then not In_Open_Scopes
(BT
)
4759 Prepend_Elmt
(Full_View
(BT
), Exchanged_Views
);
4760 Exchange_Declarations
(BT
);
4763 end Check_Private_View
;
4765 --------------------------
4766 -- Contains_Instance_Of --
4767 --------------------------
4769 function Contains_Instance_Of
4772 N
: Node_Id
) return Boolean
4780 -- Verify that there are no circular instantiations. We check whether
4781 -- the unit contains an instance of the current scope or some enclosing
4782 -- scope (in case one of the instances appears in a subunit). Longer
4783 -- circularities involving subunits might seem too pathological to
4784 -- consider, but they were not too pathological for the authors of
4785 -- DEC bc30vsq, so we loop over all enclosing scopes, and mark all
4786 -- enclosing generic scopes as containing an instance.
4789 -- Within a generic subprogram body, the scope is not generic, to
4790 -- allow for recursive subprograms. Use the declaration to determine
4791 -- whether this is a generic unit.
4793 if Ekind
(Scop
) = E_Generic_Package
4794 or else (Is_Subprogram
(Scop
)
4795 and then Nkind
(Unit_Declaration_Node
(Scop
)) =
4796 N_Generic_Subprogram_Declaration
)
4798 Elmt
:= First_Elmt
(Inner_Instances
(Inner
));
4800 while Present
(Elmt
) loop
4801 if Node
(Elmt
) = Scop
then
4802 Error_Msg_Node_2
:= Inner
;
4804 ("circular Instantiation: & instantiated within &!",
4808 elsif Node
(Elmt
) = Inner
then
4811 elsif Contains_Instance_Of
(Node
(Elmt
), Scop
, N
) then
4812 Error_Msg_Node_2
:= Inner
;
4814 ("circular Instantiation: & instantiated within &!",
4822 -- Indicate that Inner is being instantiated within Scop
4824 Append_Elmt
(Inner
, Inner_Instances
(Scop
));
4827 if Scop
= Standard_Standard
then
4830 Scop
:= Scope
(Scop
);
4835 end Contains_Instance_Of
;
4837 -----------------------
4838 -- Copy_Generic_Node --
4839 -----------------------
4841 function Copy_Generic_Node
4843 Parent_Id
: Node_Id
;
4844 Instantiating
: Boolean) return Node_Id
4849 function Copy_Generic_Descendant
(D
: Union_Id
) return Union_Id
;
4850 -- Check the given value of one of the Fields referenced by the
4851 -- current node to determine whether to copy it recursively. The
4852 -- field may hold a Node_Id, a List_Id, or an Elist_Id, or a plain
4853 -- value (Sloc, Uint, Char) in which case it need not be copied.
4855 procedure Copy_Descendants
;
4856 -- Common utility for various nodes
4858 function Copy_Generic_Elist
(E
: Elist_Id
) return Elist_Id
;
4859 -- Make copy of element list
4861 function Copy_Generic_List
4863 Parent_Id
: Node_Id
) return List_Id
;
4864 -- Apply Copy_Node recursively to the members of a node list
4866 function In_Defining_Unit_Name
(Nam
: Node_Id
) return Boolean;
4867 -- True if an identifier is part of the defining program unit name
4868 -- of a child unit. The entity of such an identifier must be kept
4869 -- (for ASIS use) even though as the name of an enclosing generic
4870 -- it would otherwise not be preserved in the generic tree.
4872 ----------------------
4873 -- Copy_Descendants --
4874 ----------------------
4876 procedure Copy_Descendants
is
4878 use Atree
.Unchecked_Access
;
4879 -- This code section is part of the implementation of an untyped
4880 -- tree traversal, so it needs direct access to node fields.
4883 Set_Field1
(New_N
, Copy_Generic_Descendant
(Field1
(N
)));
4884 Set_Field2
(New_N
, Copy_Generic_Descendant
(Field2
(N
)));
4885 Set_Field3
(New_N
, Copy_Generic_Descendant
(Field3
(N
)));
4886 Set_Field4
(New_N
, Copy_Generic_Descendant
(Field4
(N
)));
4887 Set_Field5
(New_N
, Copy_Generic_Descendant
(Field5
(N
)));
4888 end Copy_Descendants
;
4890 -----------------------------
4891 -- Copy_Generic_Descendant --
4892 -----------------------------
4894 function Copy_Generic_Descendant
(D
: Union_Id
) return Union_Id
is
4896 if D
= Union_Id
(Empty
) then
4899 elsif D
in Node_Range
then
4901 (Copy_Generic_Node
(Node_Id
(D
), New_N
, Instantiating
));
4903 elsif D
in List_Range
then
4904 return Union_Id
(Copy_Generic_List
(List_Id
(D
), New_N
));
4906 elsif D
in Elist_Range
then
4907 return Union_Id
(Copy_Generic_Elist
(Elist_Id
(D
)));
4909 -- Nothing else is copyable (e.g. Uint values), return as is
4914 end Copy_Generic_Descendant
;
4916 ------------------------
4917 -- Copy_Generic_Elist --
4918 ------------------------
4920 function Copy_Generic_Elist
(E
: Elist_Id
) return Elist_Id
is
4927 M
:= First_Elmt
(E
);
4928 while Present
(M
) loop
4930 (Copy_Generic_Node
(Node
(M
), Empty
, Instantiating
), L
);
4939 end Copy_Generic_Elist
;
4941 -----------------------
4942 -- Copy_Generic_List --
4943 -----------------------
4945 function Copy_Generic_List
4947 Parent_Id
: Node_Id
) return List_Id
4955 Set_Parent
(New_L
, Parent_Id
);
4958 while Present
(N
) loop
4959 Append
(Copy_Generic_Node
(N
, Empty
, Instantiating
), New_L
);
4968 end Copy_Generic_List
;
4970 ---------------------------
4971 -- In_Defining_Unit_Name --
4972 ---------------------------
4974 function In_Defining_Unit_Name
(Nam
: Node_Id
) return Boolean is
4976 return Present
(Parent
(Nam
))
4977 and then (Nkind
(Parent
(Nam
)) = N_Defining_Program_Unit_Name
4979 (Nkind
(Parent
(Nam
)) = N_Expanded_Name
4980 and then In_Defining_Unit_Name
(Parent
(Nam
))));
4981 end In_Defining_Unit_Name
;
4983 -- Start of processing for Copy_Generic_Node
4990 New_N
:= New_Copy
(N
);
4992 if Instantiating
then
4993 Adjust_Instantiation_Sloc
(New_N
, S_Adjustment
);
4996 if not Is_List_Member
(N
) then
4997 Set_Parent
(New_N
, Parent_Id
);
5000 -- If defining identifier, then all fields have been copied already
5002 if Nkind
(New_N
) in N_Entity
then
5005 -- Special casing for identifiers and other entity names and operators
5007 elsif Nkind
(New_N
) = N_Identifier
5008 or else Nkind
(New_N
) = N_Character_Literal
5009 or else Nkind
(New_N
) = N_Expanded_Name
5010 or else Nkind
(New_N
) = N_Operator_Symbol
5011 or else Nkind
(New_N
) in N_Op
5013 if not Instantiating
then
5015 -- Link both nodes in order to assign subsequently the
5016 -- entity of the copy to the original node, in case this
5017 -- is a global reference.
5019 Set_Associated_Node
(N
, New_N
);
5021 -- If we are within an instantiation, this is a nested generic
5022 -- that has already been analyzed at the point of definition. We
5023 -- must preserve references that were global to the enclosing
5024 -- parent at that point. Other occurrences, whether global or
5025 -- local to the current generic, must be resolved anew, so we
5026 -- reset the entity in the generic copy. A global reference has
5027 -- a smaller depth than the parent, or else the same depth in
5028 -- case both are distinct compilation units.
5030 -- It is also possible for Current_Instantiated_Parent to be
5031 -- defined, and for this not to be a nested generic, namely
5032 -- if the unit is loaded through Rtsfind. In that case, the
5033 -- entity of New_N is only a link to the associated node, and
5034 -- not a defining occurrence.
5036 -- The entities for parent units in the defining_program_unit
5037 -- of a generic child unit are established when the context of
5038 -- the unit is first analyzed, before the generic copy is made.
5039 -- They are preserved in the copy for use in ASIS queries.
5041 Ent
:= Entity
(New_N
);
5043 if No
(Current_Instantiated_Parent
.Gen_Id
) then
5045 or else Nkind
(Ent
) /= N_Defining_Identifier
5046 or else not In_Defining_Unit_Name
(N
)
5048 Set_Associated_Node
(New_N
, Empty
);
5053 not (Nkind
(Ent
) = N_Defining_Identifier
5055 Nkind
(Ent
) = N_Defining_Character_Literal
5057 Nkind
(Ent
) = N_Defining_Operator_Symbol
)
5058 or else No
(Scope
(Ent
))
5059 or else Scope
(Ent
) = Current_Instantiated_Parent
.Gen_Id
5060 or else (Scope_Depth
(Scope
(Ent
)) >
5061 Scope_Depth
(Current_Instantiated_Parent
.Gen_Id
)
5063 Get_Source_Unit
(Ent
) =
5064 Get_Source_Unit
(Current_Instantiated_Parent
.Gen_Id
))
5066 Set_Associated_Node
(New_N
, Empty
);
5069 -- Case of instantiating identifier or some other name or operator
5072 -- If the associated node is still defined, the entity in
5073 -- it is global, and must be copied to the instance.
5074 -- If this copy is being made for a body to inline, it is
5075 -- applied to an instantiated tree, and the entity is already
5076 -- present and must be also preserved.
5079 Assoc
: constant Node_Id
:= Get_Associated_Node
(N
);
5081 if Present
(Assoc
) then
5082 if Nkind
(Assoc
) = Nkind
(N
) then
5083 Set_Entity
(New_N
, Entity
(Assoc
));
5084 Check_Private_View
(N
);
5086 elsif Nkind
(Assoc
) = N_Function_Call
then
5087 Set_Entity
(New_N
, Entity
(Name
(Assoc
)));
5089 elsif (Nkind
(Assoc
) = N_Defining_Identifier
5090 or else Nkind
(Assoc
) = N_Defining_Character_Literal
5091 or else Nkind
(Assoc
) = N_Defining_Operator_Symbol
)
5092 and then Expander_Active
5094 -- Inlining case: we are copying a tree that contains
5095 -- global entities, which are preserved in the copy
5096 -- to be used for subsequent inlining.
5101 Set_Entity
(New_N
, Empty
);
5107 -- For expanded name, we must copy the Prefix and Selector_Name
5109 if Nkind
(N
) = N_Expanded_Name
then
5111 (New_N
, Copy_Generic_Node
(Prefix
(N
), New_N
, Instantiating
));
5113 Set_Selector_Name
(New_N
,
5114 Copy_Generic_Node
(Selector_Name
(N
), New_N
, Instantiating
));
5116 -- For operators, we must copy the right operand
5118 elsif Nkind
(N
) in N_Op
then
5119 Set_Right_Opnd
(New_N
,
5120 Copy_Generic_Node
(Right_Opnd
(N
), New_N
, Instantiating
));
5122 -- And for binary operators, the left operand as well
5124 if Nkind
(N
) in N_Binary_Op
then
5125 Set_Left_Opnd
(New_N
,
5126 Copy_Generic_Node
(Left_Opnd
(N
), New_N
, Instantiating
));
5130 -- Special casing for stubs
5132 elsif Nkind
(N
) in N_Body_Stub
then
5134 -- In any case, we must copy the specification or defining
5135 -- identifier as appropriate.
5137 if Nkind
(N
) = N_Subprogram_Body_Stub
then
5138 Set_Specification
(New_N
,
5139 Copy_Generic_Node
(Specification
(N
), New_N
, Instantiating
));
5142 Set_Defining_Identifier
(New_N
,
5144 (Defining_Identifier
(N
), New_N
, Instantiating
));
5147 -- If we are not instantiating, then this is where we load and
5148 -- analyze subunits, i.e. at the point where the stub occurs. A
5149 -- more permissivle system might defer this analysis to the point
5150 -- of instantiation, but this seems to complicated for now.
5152 if not Instantiating
then
5154 Subunit_Name
: constant Unit_Name_Type
:= Get_Unit_Name
(N
);
5156 Unum
: Unit_Number_Type
;
5162 (Load_Name
=> Subunit_Name
,
5167 -- If the proper body is not found, a warning message will
5168 -- be emitted when analyzing the stub, or later at the the
5169 -- point of instantiation. Here we just leave the stub as is.
5171 if Unum
= No_Unit
then
5172 Subunits_Missing
:= True;
5173 goto Subunit_Not_Found
;
5176 Subunit
:= Cunit
(Unum
);
5178 if Nkind
(Unit
(Subunit
)) /= N_Subunit
then
5179 Error_Msg_Sloc
:= Sloc
(N
);
5181 ("expected SEPARATE subunit to complete stub at#,"
5182 & " found child unit", Subunit
);
5183 goto Subunit_Not_Found
;
5186 -- We must create a generic copy of the subunit, in order
5187 -- to perform semantic analysis on it, and we must replace
5188 -- the stub in the original generic unit with the subunit,
5189 -- in order to preserve non-local references within.
5191 -- Only the proper body needs to be copied. Library_Unit and
5192 -- context clause are simply inherited by the generic copy.
5193 -- Note that the copy (which may be recursive if there are
5194 -- nested subunits) must be done first, before attaching it
5195 -- to the enclosing generic.
5199 (Proper_Body
(Unit
(Subunit
)),
5200 Empty
, Instantiating
=> False);
5202 -- Now place the original proper body in the original
5203 -- generic unit. This is a body, not a compilation unit.
5205 Rewrite
(N
, Proper_Body
(Unit
(Subunit
)));
5206 Set_Is_Compilation_Unit
(Defining_Entity
(N
), False);
5207 Set_Was_Originally_Stub
(N
);
5209 -- Finally replace the body of the subunit with its copy,
5210 -- and make this new subunit into the library unit of the
5211 -- generic copy, which does not have stubs any longer.
5213 Set_Proper_Body
(Unit
(Subunit
), New_Body
);
5214 Set_Library_Unit
(New_N
, Subunit
);
5215 Inherit_Context
(Unit
(Subunit
), N
);
5218 -- If we are instantiating, this must be an error case, since
5219 -- otherwise we would have replaced the stub node by the proper
5220 -- body that corresponds. So just ignore it in the copy (i.e.
5221 -- we have copied it, and that is good enough).
5227 <<Subunit_Not_Found
>> null;
5229 -- If the node is a compilation unit, it is the subunit of a stub,
5230 -- which has been loaded already (see code below). In this case,
5231 -- the library unit field of N points to the parent unit (which
5232 -- is a compilation unit) and need not (and cannot!) be copied.
5234 -- When the proper body of the stub is analyzed, thie library_unit
5235 -- link is used to establish the proper context (see sem_ch10).
5237 -- The other fields of a compilation unit are copied as usual
5239 elsif Nkind
(N
) = N_Compilation_Unit
then
5241 -- This code can only be executed when not instantiating, because
5242 -- in the copy made for an instantiation, the compilation unit
5243 -- node has disappeared at the point that a stub is replaced by
5246 pragma Assert
(not Instantiating
);
5248 Set_Context_Items
(New_N
,
5249 Copy_Generic_List
(Context_Items
(N
), New_N
));
5252 Copy_Generic_Node
(Unit
(N
), New_N
, False));
5254 Set_First_Inlined_Subprogram
(New_N
,
5256 (First_Inlined_Subprogram
(N
), New_N
, False));
5258 Set_Aux_Decls_Node
(New_N
,
5259 Copy_Generic_Node
(Aux_Decls_Node
(N
), New_N
, False));
5261 -- For an assignment node, the assignment is known to be semantically
5262 -- legal if we are instantiating the template. This avoids incorrect
5263 -- diagnostics in generated code.
5265 elsif Nkind
(N
) = N_Assignment_Statement
then
5267 -- Copy name and expression fields in usual manner
5270 Copy_Generic_Node
(Name
(N
), New_N
, Instantiating
));
5272 Set_Expression
(New_N
,
5273 Copy_Generic_Node
(Expression
(N
), New_N
, Instantiating
));
5275 if Instantiating
then
5276 Set_Assignment_OK
(Name
(New_N
), True);
5279 elsif Nkind
(N
) = N_Aggregate
5280 or else Nkind
(N
) = N_Extension_Aggregate
5283 if not Instantiating
then
5284 Set_Associated_Node
(N
, New_N
);
5287 if Present
(Get_Associated_Node
(N
))
5288 and then Nkind
(Get_Associated_Node
(N
)) = Nkind
(N
)
5290 -- In the generic the aggregate has some composite type. If at
5291 -- the point of instantiation the type has a private view,
5292 -- install the full view (and that of its ancestors, if any).
5295 T
: Entity_Id
:= (Etype
(Get_Associated_Node
(New_N
)));
5300 and then Is_Private_Type
(T
)
5306 and then Is_Tagged_Type
(T
)
5307 and then Is_Derived_Type
(T
)
5309 Rt
:= Root_Type
(T
);
5314 if Is_Private_Type
(T
) then
5325 -- Do not copy the associated node, which points to
5326 -- the generic copy of the aggregate.
5329 use Atree
.Unchecked_Access
;
5330 -- This code section is part of the implementation of an untyped
5331 -- tree traversal, so it needs direct access to node fields.
5334 Set_Field1
(New_N
, Copy_Generic_Descendant
(Field1
(N
)));
5335 Set_Field2
(New_N
, Copy_Generic_Descendant
(Field2
(N
)));
5336 Set_Field3
(New_N
, Copy_Generic_Descendant
(Field3
(N
)));
5337 Set_Field5
(New_N
, Copy_Generic_Descendant
(Field5
(N
)));
5340 -- Allocators do not have an identifier denoting the access type,
5341 -- so we must locate it through the expression to check whether
5342 -- the views are consistent.
5344 elsif Nkind
(N
) = N_Allocator
5345 and then Nkind
(Expression
(N
)) = N_Qualified_Expression
5346 and then Is_Entity_Name
(Subtype_Mark
(Expression
(N
)))
5347 and then Instantiating
5350 T
: constant Node_Id
:=
5351 Get_Associated_Node
(Subtype_Mark
(Expression
(N
)));
5356 -- Retrieve the allocator node in the generic copy
5358 Acc_T
:= Etype
(Parent
(Parent
(T
)));
5360 and then Is_Private_Type
(Acc_T
)
5362 Switch_View
(Acc_T
);
5369 -- For a proper body, we must catch the case of a proper body that
5370 -- replaces a stub. This represents the point at which a separate
5371 -- compilation unit, and hence template file, may be referenced, so
5372 -- we must make a new source instantiation entry for the template
5373 -- of the subunit, and ensure that all nodes in the subunit are
5374 -- adjusted using this new source instantiation entry.
5376 elsif Nkind
(N
) in N_Proper_Body
then
5378 Save_Adjustment
: constant Sloc_Adjustment
:= S_Adjustment
;
5381 if Instantiating
and then Was_Originally_Stub
(N
) then
5382 Create_Instantiation_Source
5383 (Instantiation_Node
,
5384 Defining_Entity
(N
),
5389 -- Now copy the fields of the proper body, using the new
5390 -- adjustment factor if one was needed as per test above.
5394 -- Restore the original adjustment factor in case changed
5396 S_Adjustment
:= Save_Adjustment
;
5399 -- Don't copy Ident or Comment pragmas, since the comment belongs
5400 -- to the generic unit, not to the instantiating unit.
5402 elsif Nkind
(N
) = N_Pragma
5403 and then Instantiating
5406 Prag_Id
: constant Pragma_Id
:= Get_Pragma_Id
(Chars
(N
));
5409 if Prag_Id
= Pragma_Ident
5410 or else Prag_Id
= Pragma_Comment
5412 New_N
:= Make_Null_Statement
(Sloc
(N
));
5419 elsif Nkind
(N
) = N_Integer_Literal
5420 or else Nkind
(N
) = N_Real_Literal
5422 -- No descendant fields need traversing
5426 -- For the remaining nodes, copy recursively their descendants
5432 and then Nkind
(N
) = N_Subprogram_Body
5434 Set_Generic_Parent
(Specification
(New_N
), N
);
5439 end Copy_Generic_Node
;
5441 ----------------------------
5442 -- Denotes_Formal_Package --
5443 ----------------------------
5445 function Denotes_Formal_Package
5447 On_Exit
: Boolean := False) return Boolean
5450 Scop
: constant Entity_Id
:= Scope
(Pack
);
5457 (Instance_Envs
.Last
).Instantiated_Parent
.Act_Id
;
5459 Par
:= Current_Instantiated_Parent
.Act_Id
;
5462 if Ekind
(Scop
) = E_Generic_Package
5463 or else Nkind
(Unit_Declaration_Node
(Scop
)) =
5464 N_Generic_Subprogram_Declaration
5468 elsif Nkind
(Parent
(Pack
)) = N_Formal_Package_Declaration
then
5475 -- Check whether this package is associated with a formal
5476 -- package of the enclosing instantiation. Iterate over the
5477 -- list of renamings.
5479 E
:= First_Entity
(Par
);
5480 while Present
(E
) loop
5481 if Ekind
(E
) /= E_Package
5482 or else Nkind
(Parent
(E
)) /= N_Package_Renaming_Declaration
5485 elsif Renamed_Object
(E
) = Par
then
5488 elsif Renamed_Object
(E
) = Pack
then
5497 end Denotes_Formal_Package
;
5503 procedure End_Generic
is
5505 -- ??? More things could be factored out in this
5506 -- routine. Should probably be done at a later stage.
5508 Inside_A_Generic
:= Generic_Flags
.Table
(Generic_Flags
.Last
);
5509 Generic_Flags
.Decrement_Last
;
5511 Expander_Mode_Restore
;
5514 ----------------------
5515 -- Find_Actual_Type --
5516 ----------------------
5518 function Find_Actual_Type
5520 Gen_Scope
: Entity_Id
) return Entity_Id
5525 if not Is_Child_Unit
(Gen_Scope
) then
5526 return Get_Instance_Of
(Typ
);
5528 elsif not Is_Generic_Type
(Typ
)
5529 or else Scope
(Typ
) = Gen_Scope
5531 return Get_Instance_Of
(Typ
);
5534 T
:= Current_Entity
(Typ
);
5535 while Present
(T
) loop
5536 if In_Open_Scopes
(Scope
(T
)) then
5545 end Find_Actual_Type
;
5547 ----------------------------
5548 -- Freeze_Subprogram_Body --
5549 ----------------------------
5551 procedure Freeze_Subprogram_Body
5552 (Inst_Node
: Node_Id
;
5554 Pack_Id
: Entity_Id
)
5557 Gen_Unit
: constant Entity_Id
:= Get_Generic_Entity
(Inst_Node
);
5558 Par
: constant Entity_Id
:= Scope
(Gen_Unit
);
5563 function Earlier
(N1
, N2
: Node_Id
) return Boolean;
5564 -- Yields True if N1 and N2 appear in the same compilation unit,
5565 -- ignoring subunits, and if N1 is to the left of N2 in a left-to-right
5566 -- traversal of the tree for the unit.
5568 function Enclosing_Body
(N
: Node_Id
) return Node_Id
;
5569 -- Find innermost package body that encloses the given node, and which
5570 -- is not a compilation unit. Freeze nodes for the instance, or for its
5571 -- enclosing body, may be inserted after the enclosing_body of the
5574 function Package_Freeze_Node
(B
: Node_Id
) return Node_Id
;
5575 -- Find entity for given package body, and locate or create a freeze
5578 function True_Parent
(N
: Node_Id
) return Node_Id
;
5579 -- For a subunit, return parent of corresponding stub
5585 function Earlier
(N1
, N2
: Node_Id
) return Boolean is
5591 procedure Find_Depth
(P
: in out Node_Id
; D
: in out Integer);
5592 -- Find distance from given node to enclosing compilation unit
5598 procedure Find_Depth
(P
: in out Node_Id
; D
: in out Integer) is
5601 and then Nkind
(P
) /= N_Compilation_Unit
5603 P
:= True_Parent
(P
);
5608 -- Start of procesing for Earlier
5611 Find_Depth
(P1
, D1
);
5612 Find_Depth
(P2
, D2
);
5622 P1
:= True_Parent
(P1
);
5627 P2
:= True_Parent
(P2
);
5631 -- At this point P1 and P2 are at the same distance from the root.
5632 -- We examine their parents until we find a common declarative
5633 -- list, at which point we can establish their relative placement
5634 -- by comparing their ultimate slocs. If we reach the root,
5635 -- N1 and N2 do not descend from the same declarative list (e.g.
5636 -- one is nested in the declarative part and the other is in a block
5637 -- in the statement part) and the earlier one is already frozen.
5639 while not Is_List_Member
(P1
)
5640 or else not Is_List_Member
(P2
)
5641 or else List_Containing
(P1
) /= List_Containing
(P2
)
5643 P1
:= True_Parent
(P1
);
5644 P2
:= True_Parent
(P2
);
5646 if Nkind
(Parent
(P1
)) = N_Subunit
then
5647 P1
:= Corresponding_Stub
(Parent
(P1
));
5650 if Nkind
(Parent
(P2
)) = N_Subunit
then
5651 P2
:= Corresponding_Stub
(Parent
(P2
));
5660 Top_Level_Location
(Sloc
(P1
)) < Top_Level_Location
(Sloc
(P2
));
5663 --------------------
5664 -- Enclosing_Body --
5665 --------------------
5667 function Enclosing_Body
(N
: Node_Id
) return Node_Id
is
5668 P
: Node_Id
:= Parent
(N
);
5672 and then Nkind
(Parent
(P
)) /= N_Compilation_Unit
5674 if Nkind
(P
) = N_Package_Body
then
5676 if Nkind
(Parent
(P
)) = N_Subunit
then
5677 return Corresponding_Stub
(Parent
(P
));
5683 P
:= True_Parent
(P
);
5689 -------------------------
5690 -- Package_Freeze_Node --
5691 -------------------------
5693 function Package_Freeze_Node
(B
: Node_Id
) return Node_Id
is
5697 if Nkind
(B
) = N_Package_Body
then
5698 Id
:= Corresponding_Spec
(B
);
5700 else pragma Assert
(Nkind
(B
) = N_Package_Body_Stub
);
5701 Id
:= Corresponding_Spec
(Proper_Body
(Unit
(Library_Unit
(B
))));
5704 Ensure_Freeze_Node
(Id
);
5705 return Freeze_Node
(Id
);
5706 end Package_Freeze_Node
;
5712 function True_Parent
(N
: Node_Id
) return Node_Id
is
5714 if Nkind
(Parent
(N
)) = N_Subunit
then
5715 return Parent
(Corresponding_Stub
(Parent
(N
)));
5721 -- Start of processing of Freeze_Subprogram_Body
5724 -- If the instance and the generic body appear within the same
5725 -- unit, and the instance preceeds the generic, the freeze node for
5726 -- the instance must appear after that of the generic. If the generic
5727 -- is nested within another instance I2, then current instance must
5728 -- be frozen after I2. In both cases, the freeze nodes are those of
5729 -- enclosing packages. Otherwise, the freeze node is placed at the end
5730 -- of the current declarative part.
5732 Enc_G
:= Enclosing_Body
(Gen_Body
);
5733 Enc_I
:= Enclosing_Body
(Inst_Node
);
5734 Ensure_Freeze_Node
(Pack_Id
);
5735 F_Node
:= Freeze_Node
(Pack_Id
);
5737 if Is_Generic_Instance
(Par
)
5738 and then Present
(Freeze_Node
(Par
))
5740 In_Same_Declarative_Part
(Freeze_Node
(Par
), Inst_Node
)
5742 if ABE_Is_Certain
(Get_Package_Instantiation_Node
(Par
)) then
5744 -- The parent was a premature instantiation. Insert freeze
5745 -- node at the end the current declarative part.
5747 Insert_After_Last_Decl
(Inst_Node
, F_Node
);
5750 Insert_After
(Freeze_Node
(Par
), F_Node
);
5753 -- The body enclosing the instance should be frozen after the body
5754 -- that includes the generic, because the body of the instance may
5755 -- make references to entities therein. If the two are not in the
5756 -- same declarative part, or if the one enclosing the instance is
5757 -- frozen already, freeze the instance at the end of the current
5758 -- declarative part.
5760 elsif Is_Generic_Instance
(Par
)
5761 and then Present
(Freeze_Node
(Par
))
5762 and then Present
(Enc_I
)
5764 if In_Same_Declarative_Part
(Freeze_Node
(Par
), Enc_I
)
5766 (Nkind
(Enc_I
) = N_Package_Body
5768 In_Same_Declarative_Part
(Freeze_Node
(Par
), Parent
(Enc_I
)))
5770 -- The enclosing package may contain several instances. Rather
5771 -- than computing the earliest point at which to insert its
5772 -- freeze node, we place it at the end of the declarative part
5773 -- of the parent of the generic.
5775 Insert_After_Last_Decl
5776 (Freeze_Node
(Par
), Package_Freeze_Node
(Enc_I
));
5779 Insert_After_Last_Decl
(Inst_Node
, F_Node
);
5781 elsif Present
(Enc_G
)
5782 and then Present
(Enc_I
)
5783 and then Enc_G
/= Enc_I
5784 and then Earlier
(Inst_Node
, Gen_Body
)
5786 if Nkind
(Enc_G
) = N_Package_Body
then
5787 E_G_Id
:= Corresponding_Spec
(Enc_G
);
5788 else pragma Assert
(Nkind
(Enc_G
) = N_Package_Body_Stub
);
5790 Corresponding_Spec
(Proper_Body
(Unit
(Library_Unit
(Enc_G
))));
5793 -- Freeze package that encloses instance, and place node after
5794 -- package that encloses generic. If enclosing package is already
5795 -- frozen we have to assume it is at the proper place. This may
5796 -- be a potential ABE that requires dynamic checking.
5798 Insert_After_Last_Decl
(Enc_G
, Package_Freeze_Node
(Enc_I
));
5800 -- Freeze enclosing subunit before instance
5802 Ensure_Freeze_Node
(E_G_Id
);
5804 if not Is_List_Member
(Freeze_Node
(E_G_Id
)) then
5805 Insert_After
(Enc_G
, Freeze_Node
(E_G_Id
));
5808 Insert_After_Last_Decl
(Inst_Node
, F_Node
);
5811 -- If none of the above, insert freeze node at the end of the
5812 -- current declarative part.
5814 Insert_After_Last_Decl
(Inst_Node
, F_Node
);
5816 end Freeze_Subprogram_Body
;
5822 function Get_Gen_Id
(E
: Assoc_Ptr
) return Entity_Id
is
5824 return Generic_Renamings
.Table
(E
).Gen_Id
;
5827 ---------------------
5828 -- Get_Instance_Of --
5829 ---------------------
5831 function Get_Instance_Of
(A
: Entity_Id
) return Entity_Id
is
5832 Res
: constant Assoc_Ptr
:= Generic_Renamings_HTable
.Get
(A
);
5835 if Res
/= Assoc_Null
then
5836 return Generic_Renamings
.Table
(Res
).Act_Id
;
5838 -- On exit, entity is not instantiated: not a generic parameter,
5839 -- or else parameter of an inner generic unit.
5843 end Get_Instance_Of
;
5845 ------------------------------------
5846 -- Get_Package_Instantiation_Node --
5847 ------------------------------------
5849 function Get_Package_Instantiation_Node
(A
: Entity_Id
) return Node_Id
is
5850 Decl
: Node_Id
:= Unit_Declaration_Node
(A
);
5854 -- If the Package_Instantiation attribute has been set on the package
5855 -- entity, then use it directly when it (or its Original_Node) refers
5856 -- to an N_Package_Instantiation node. In principle it should be
5857 -- possible to have this field set in all cases, which should be
5858 -- investigated, and would allow this function to be significantly
5861 if Present
(Package_Instantiation
(A
)) then
5862 if Nkind
(Package_Instantiation
(A
)) = N_Package_Instantiation
then
5863 return Package_Instantiation
(A
);
5865 elsif Nkind
(Original_Node
(Package_Instantiation
(A
)))
5866 = N_Package_Instantiation
5868 return Original_Node
(Package_Instantiation
(A
));
5872 -- If the instantiation is a compilation unit that does not need a
5873 -- body then the instantiation node has been rewritten as a package
5874 -- declaration for the instance, and we return the original node.
5876 -- If it is a compilation unit and the instance node has not been
5877 -- rewritten, then it is still the unit of the compilation. Finally,
5878 -- if a body is present, this is a parent of the main unit whose body
5879 -- has been compiled for inlining purposes, and the instantiation node
5880 -- has been rewritten with the instance body.
5882 -- Otherwise the instantiation node appears after the declaration.
5883 -- If the entity is a formal package, the declaration may have been
5884 -- rewritten as a generic declaration (in the case of a formal with a
5885 -- box) or left as a formal package declaration if it has actuals, and
5886 -- is found with a forward search.
5888 if Nkind
(Parent
(Decl
)) = N_Compilation_Unit
then
5889 if Nkind
(Decl
) = N_Package_Declaration
5890 and then Present
(Corresponding_Body
(Decl
))
5892 Decl
:= Unit_Declaration_Node
(Corresponding_Body
(Decl
));
5895 if Nkind
(Original_Node
(Decl
)) = N_Package_Instantiation
then
5896 return Original_Node
(Decl
);
5898 return Unit
(Parent
(Decl
));
5901 elsif Nkind
(Decl
) = N_Generic_Package_Declaration
5902 and then Nkind
(Original_Node
(Decl
)) = N_Formal_Package_Declaration
5904 return Original_Node
(Decl
);
5907 Inst
:= Next
(Decl
);
5908 while Nkind
(Inst
) /= N_Package_Instantiation
5909 and then Nkind
(Inst
) /= N_Formal_Package_Declaration
5916 end Get_Package_Instantiation_Node
;
5918 ------------------------
5919 -- Has_Been_Exchanged --
5920 ------------------------
5922 function Has_Been_Exchanged
(E
: Entity_Id
) return Boolean is
5923 Next
: Elmt_Id
:= First_Elmt
(Exchanged_Views
);
5926 while Present
(Next
) loop
5927 if Full_View
(Node
(Next
)) = E
then
5935 end Has_Been_Exchanged
;
5941 function Hash
(F
: Entity_Id
) return HTable_Range
is
5943 return HTable_Range
(F
mod HTable_Size
);
5946 ------------------------
5947 -- Hide_Current_Scope --
5948 ------------------------
5950 procedure Hide_Current_Scope
is
5951 C
: constant Entity_Id
:= Current_Scope
;
5955 Set_Is_Hidden_Open_Scope
(C
);
5956 E
:= First_Entity
(C
);
5958 while Present
(E
) loop
5959 if Is_Immediately_Visible
(E
) then
5960 Set_Is_Immediately_Visible
(E
, False);
5961 Append_Elmt
(E
, Hidden_Entities
);
5967 -- Make the scope name invisible as well. This is necessary, but
5968 -- might conflict with calls to Rtsfind later on, in case the scope
5969 -- is a predefined one. There is no clean solution to this problem, so
5970 -- for now we depend on the user not redefining Standard itself in one
5971 -- of the parent units.
5973 if Is_Immediately_Visible
(C
)
5974 and then C
/= Standard_Standard
5976 Set_Is_Immediately_Visible
(C
, False);
5977 Append_Elmt
(C
, Hidden_Entities
);
5980 end Hide_Current_Scope
;
5986 procedure Init_Env
is
5987 Saved
: Instance_Env
;
5990 Saved
.Ada_Version
:= Ada_Version
;
5991 Saved
.Ada_Version_Explicit
:= Ada_Version_Explicit
;
5992 Saved
.Instantiated_Parent
:= Current_Instantiated_Parent
;
5993 Saved
.Exchanged_Views
:= Exchanged_Views
;
5994 Saved
.Hidden_Entities
:= Hidden_Entities
;
5995 Saved
.Current_Sem_Unit
:= Current_Sem_Unit
;
5996 Saved
.Parent_Unit_Visible
:= Parent_Unit_Visible
;
5997 Saved
.Instance_Parent_Unit
:= Instance_Parent_Unit
;
5998 Instance_Envs
.Increment_Last
;
5999 Instance_Envs
.Table
(Instance_Envs
.Last
) := Saved
;
6001 Exchanged_Views
:= New_Elmt_List
;
6002 Hidden_Entities
:= New_Elmt_List
;
6004 -- Make dummy entry for Instantiated parent. If generic unit is
6005 -- legal, this is set properly in Set_Instance_Env.
6007 Current_Instantiated_Parent
:=
6008 (Current_Scope
, Current_Scope
, Assoc_Null
);
6011 ------------------------------
6012 -- In_Same_Declarative_Part --
6013 ------------------------------
6015 function In_Same_Declarative_Part
6017 Inst
: Node_Id
) return Boolean
6019 Decls
: constant Node_Id
:= Parent
(F_Node
);
6020 Nod
: Node_Id
:= Parent
(Inst
);
6023 while Present
(Nod
) loop
6027 elsif Nkind
(Nod
) = N_Subprogram_Body
6028 or else Nkind
(Nod
) = N_Package_Body
6029 or else Nkind
(Nod
) = N_Task_Body
6030 or else Nkind
(Nod
) = N_Protected_Body
6031 or else Nkind
(Nod
) = N_Block_Statement
6035 elsif Nkind
(Nod
) = N_Subunit
then
6036 Nod
:= Corresponding_Stub
(Nod
);
6038 elsif Nkind
(Nod
) = N_Compilation_Unit
then
6041 Nod
:= Parent
(Nod
);
6046 end In_Same_Declarative_Part
;
6048 ---------------------
6049 -- In_Main_Context --
6050 ---------------------
6052 function In_Main_Context
(E
: Entity_Id
) return Boolean is
6058 if not Is_Compilation_Unit
(E
)
6059 or else Ekind
(E
) /= E_Package
6060 or else In_Private_Part
(E
)
6065 Context
:= Context_Items
(Cunit
(Main_Unit
));
6067 Clause
:= First
(Context
);
6068 while Present
(Clause
) loop
6069 if Nkind
(Clause
) = N_With_Clause
then
6070 Nam
:= Name
(Clause
);
6072 -- If the current scope is part of the context of the main unit,
6073 -- analysis of the corresponding with_clause is not complete, and
6074 -- the entity is not set. We use the Chars field directly, which
6075 -- might produce false positives in rare cases, but guarantees
6076 -- that we produce all the instance bodies we will need.
6078 if (Nkind
(Nam
) = N_Identifier
6079 and then Chars
(Nam
) = Chars
(E
))
6080 or else (Nkind
(Nam
) = N_Selected_Component
6081 and then Chars
(Selector_Name
(Nam
)) = Chars
(E
))
6091 end In_Main_Context
;
6093 ---------------------
6094 -- Inherit_Context --
6095 ---------------------
6097 procedure Inherit_Context
(Gen_Decl
: Node_Id
; Inst
: Node_Id
) is
6098 Current_Context
: List_Id
;
6099 Current_Unit
: Node_Id
;
6104 if Nkind
(Parent
(Gen_Decl
)) = N_Compilation_Unit
then
6106 -- The inherited context is attached to the enclosing compilation
6107 -- unit. This is either the main unit, or the declaration for the
6108 -- main unit (in case the instantation appears within the package
6109 -- declaration and the main unit is its body).
6111 Current_Unit
:= Parent
(Inst
);
6112 while Present
(Current_Unit
)
6113 and then Nkind
(Current_Unit
) /= N_Compilation_Unit
6115 Current_Unit
:= Parent
(Current_Unit
);
6118 Current_Context
:= Context_Items
(Current_Unit
);
6120 Item
:= First
(Context_Items
(Parent
(Gen_Decl
)));
6121 while Present
(Item
) loop
6122 if Nkind
(Item
) = N_With_Clause
then
6123 New_I
:= New_Copy
(Item
);
6124 Set_Implicit_With
(New_I
, True);
6125 Append
(New_I
, Current_Context
);
6131 end Inherit_Context
;
6137 procedure Initialize
is
6139 Generic_Renamings
.Init
;
6142 Generic_Renamings_HTable
.Reset
;
6143 Circularity_Detected
:= False;
6144 Exchanged_Views
:= No_Elist
;
6145 Hidden_Entities
:= No_Elist
;
6148 ----------------------------
6149 -- Insert_After_Last_Decl --
6150 ----------------------------
6152 procedure Insert_After_Last_Decl
(N
: Node_Id
; F_Node
: Node_Id
) is
6153 L
: List_Id
:= List_Containing
(N
);
6154 P
: constant Node_Id
:= Parent
(L
);
6157 if not Is_List_Member
(F_Node
) then
6158 if Nkind
(P
) = N_Package_Specification
6159 and then L
= Visible_Declarations
(P
)
6160 and then Present
(Private_Declarations
(P
))
6161 and then not Is_Empty_List
(Private_Declarations
(P
))
6163 L
:= Private_Declarations
(P
);
6166 Insert_After
(Last
(L
), F_Node
);
6168 end Insert_After_Last_Decl
;
6174 procedure Install_Body
6175 (Act_Body
: Node_Id
;
6180 Act_Id
: constant Entity_Id
:= Corresponding_Spec
(Act_Body
);
6181 Act_Unit
: constant Node_Id
:= Unit
(Cunit
(Get_Source_Unit
(N
)));
6182 Gen_Id
: constant Entity_Id
:= Corresponding_Spec
(Gen_Body
);
6183 Par
: constant Entity_Id
:= Scope
(Gen_Id
);
6184 Gen_Unit
: constant Node_Id
:=
6185 Unit
(Cunit
(Get_Source_Unit
(Gen_Decl
)));
6186 Orig_Body
: Node_Id
:= Gen_Body
;
6188 Body_Unit
: Node_Id
;
6190 Must_Delay
: Boolean;
6192 function Enclosing_Subp
(Id
: Entity_Id
) return Entity_Id
;
6193 -- Find subprogram (if any) that encloses instance and/or generic body
6195 function True_Sloc
(N
: Node_Id
) return Source_Ptr
;
6196 -- If the instance is nested inside a generic unit, the Sloc of the
6197 -- instance indicates the place of the original definition, not the
6198 -- point of the current enclosing instance. Pending a better usage of
6199 -- Slocs to indicate instantiation places, we determine the place of
6200 -- origin of a node by finding the maximum sloc of any ancestor node.
6201 -- Why is this not equivalent to Top_Level_Location ???
6203 --------------------
6204 -- Enclosing_Subp --
6205 --------------------
6207 function Enclosing_Subp
(Id
: Entity_Id
) return Entity_Id
is
6208 Scop
: Entity_Id
:= Scope
(Id
);
6211 while Scop
/= Standard_Standard
6212 and then not Is_Overloadable
(Scop
)
6214 Scop
:= Scope
(Scop
);
6224 function True_Sloc
(N
: Node_Id
) return Source_Ptr
is
6231 while Present
(N1
) and then N1
/= Act_Unit
loop
6232 if Sloc
(N1
) > Res
then
6242 -- Start of processing for Install_Body
6245 -- If the body is a subunit, the freeze point is the corresponding
6246 -- stub in the current compilation, not the subunit itself.
6248 if Nkind
(Parent
(Gen_Body
)) = N_Subunit
then
6249 Orig_Body
:= Corresponding_Stub
(Parent
(Gen_Body
));
6251 Orig_Body
:= Gen_Body
;
6254 Body_Unit
:= Unit
(Cunit
(Get_Source_Unit
(Orig_Body
)));
6256 -- If the instantiation and the generic definition appear in the
6257 -- same package declaration, this is an early instantiation.
6258 -- If they appear in the same declarative part, it is an early
6259 -- instantiation only if the generic body appears textually later,
6260 -- and the generic body is also in the main unit.
6262 -- If instance is nested within a subprogram, and the generic body is
6263 -- not, the instance is delayed because the enclosing body is. If
6264 -- instance and body are within the same scope, or the same sub-
6265 -- program body, indicate explicitly that the instance is delayed.
6268 (Gen_Unit
= Act_Unit
6269 and then ((Nkind
(Gen_Unit
) = N_Package_Declaration
)
6270 or else Nkind
(Gen_Unit
) = N_Generic_Package_Declaration
6271 or else (Gen_Unit
= Body_Unit
6272 and then True_Sloc
(N
) < Sloc
(Orig_Body
)))
6273 and then Is_In_Main_Unit
(Gen_Unit
)
6274 and then (Scope
(Act_Id
) = Scope
(Gen_Id
)
6276 Enclosing_Subp
(Act_Id
) = Enclosing_Subp
(Gen_Id
)));
6278 -- If this is an early instantiation, the freeze node is placed after
6279 -- the generic body. Otherwise, if the generic appears in an instance,
6280 -- we cannot freeze the current instance until the outer one is frozen.
6281 -- This is only relevant if the current instance is nested within some
6282 -- inner scope not itself within the outer instance. If this scope is
6283 -- a package body in the same declarative part as the outer instance,
6284 -- then that body needs to be frozen after the outer instance. Finally,
6285 -- if no delay is needed, we place the freeze node at the end of the
6286 -- current declarative part.
6288 if Expander_Active
then
6289 Ensure_Freeze_Node
(Act_Id
);
6290 F_Node
:= Freeze_Node
(Act_Id
);
6293 Insert_After
(Orig_Body
, F_Node
);
6295 elsif Is_Generic_Instance
(Par
)
6296 and then Present
(Freeze_Node
(Par
))
6297 and then Scope
(Act_Id
) /= Par
6299 -- Freeze instance of inner generic after instance of enclosing
6302 if In_Same_Declarative_Part
(Freeze_Node
(Par
), N
) then
6303 Insert_After
(Freeze_Node
(Par
), F_Node
);
6305 -- Freeze package enclosing instance of inner generic after
6306 -- instance of enclosing generic.
6308 elsif Nkind
(Parent
(N
)) = N_Package_Body
6309 and then In_Same_Declarative_Part
(Freeze_Node
(Par
), Parent
(N
))
6313 Enclosing
: constant Entity_Id
:=
6314 Corresponding_Spec
(Parent
(N
));
6317 Insert_After_Last_Decl
(N
, F_Node
);
6318 Ensure_Freeze_Node
(Enclosing
);
6320 if not Is_List_Member
(Freeze_Node
(Enclosing
)) then
6321 Insert_After
(Freeze_Node
(Par
), Freeze_Node
(Enclosing
));
6326 Insert_After_Last_Decl
(N
, F_Node
);
6330 Insert_After_Last_Decl
(N
, F_Node
);
6334 Set_Is_Frozen
(Act_Id
);
6335 Insert_Before
(N
, Act_Body
);
6336 Mark_Rewrite_Insertion
(Act_Body
);
6339 --------------------
6340 -- Install_Parent --
6341 --------------------
6343 procedure Install_Parent
(P
: Entity_Id
; In_Body
: Boolean := False) is
6344 Ancestors
: constant Elist_Id
:= New_Elmt_List
;
6345 S
: constant Entity_Id
:= Current_Scope
;
6346 Inst_Par
: Entity_Id
;
6347 First_Par
: Entity_Id
;
6348 Inst_Node
: Node_Id
;
6349 Gen_Par
: Entity_Id
;
6350 First_Gen
: Entity_Id
;
6353 procedure Install_Formal_Packages
(Par
: Entity_Id
);
6354 -- If any of the formals of the parent are formal packages with box,
6355 -- their formal parts are visible in the parent and thus in the child
6356 -- unit as well. Analogous to what is done in Check_Generic_Actuals
6357 -- for the unit itself.
6359 procedure Install_Noninstance_Specs
(Par
: Entity_Id
);
6360 -- Install the scopes of noninstance parent units ending with Par
6362 procedure Install_Spec
(Par
: Entity_Id
);
6363 -- The child unit is within the declarative part of the parent, so
6364 -- the declarations within the parent are immediately visible.
6366 -----------------------------
6367 -- Install_Formal_Packages --
6368 -----------------------------
6370 procedure Install_Formal_Packages
(Par
: Entity_Id
) is
6374 E
:= First_Entity
(Par
);
6375 while Present
(E
) loop
6376 if Ekind
(E
) = E_Package
6377 and then Nkind
(Parent
(E
)) = N_Package_Renaming_Declaration
6379 -- If this is the renaming for the parent instance, done
6381 if Renamed_Object
(E
) = Par
then
6384 -- The visibility of a formal of an enclosing generic is
6387 elsif Denotes_Formal_Package
(E
) then
6390 elsif Present
(Associated_Formal_Package
(E
))
6391 and then Box_Present
(Parent
(Associated_Formal_Package
(E
)))
6393 Check_Generic_Actuals
(Renamed_Object
(E
), True);
6394 Set_Is_Hidden
(E
, False);
6400 end Install_Formal_Packages
;
6402 -------------------------------
6403 -- Install_Noninstance_Specs --
6404 -------------------------------
6406 procedure Install_Noninstance_Specs
(Par
: Entity_Id
) is
6409 and then Par
/= Standard_Standard
6410 and then not In_Open_Scopes
(Par
)
6412 Install_Noninstance_Specs
(Scope
(Par
));
6415 end Install_Noninstance_Specs
;
6421 procedure Install_Spec
(Par
: Entity_Id
) is
6422 Spec
: constant Node_Id
:=
6423 Specification
(Unit_Declaration_Node
(Par
));
6426 -- If this parent of the child instance is a top-level unit,
6427 -- then record the unit and its visibility for later resetting
6428 -- in Remove_Parent. We exclude units that are generic instances,
6429 -- as we only want to record this information for the ultimate
6430 -- top-level noninstance parent (is that always correct???).
6432 if Scope
(Par
) = Standard_Standard
6433 and then not Is_Generic_Instance
(Par
)
6435 Parent_Unit_Visible
:= Is_Immediately_Visible
(Par
);
6436 Instance_Parent_Unit
:= Par
;
6439 -- Open the parent scope and make it and its declarations visible.
6440 -- If this point is not within a body, then only the visible
6441 -- declarations should be made visible, and installation of the
6442 -- private declarations is deferred until the appropriate point
6443 -- within analysis of the spec being instantiated (see the handling
6444 -- of parent visibility in Analyze_Package_Specification). This is
6445 -- relaxed in the case where the parent unit is Ada.Tags, to avoid
6446 -- private view problems that occur when compiling instantiations of
6447 -- a generic child of that package (Generic_Dispatching_Constructor).
6448 -- If the instance freezes a tagged type, inlinings of operations
6449 -- from Ada.Tags may need the full view of type Tag. If inlining
6450 -- took proper account of establishing visibility of inlined
6451 -- subprograms' parents then it should be possible to remove this
6452 -- special check. ???
6455 Set_Is_Immediately_Visible
(Par
);
6456 Install_Visible_Declarations
(Par
);
6457 Set_Use
(Visible_Declarations
(Spec
));
6459 if In_Body
or else Is_RTU
(Par
, Ada_Tags
) then
6460 Install_Private_Declarations
(Par
);
6461 Set_Use
(Private_Declarations
(Spec
));
6465 -- Start of processing for Install_Parent
6468 -- We need to install the parent instance to compile the instantiation
6469 -- of the child, but the child instance must appear in the current
6470 -- scope. Given that we cannot place the parent above the current
6471 -- scope in the scope stack, we duplicate the current scope and unstack
6472 -- both after the instantiation is complete.
6474 -- If the parent is itself the instantiation of a child unit, we must
6475 -- also stack the instantiation of its parent, and so on. Each such
6476 -- ancestor is the prefix of the name in a prior instantiation.
6478 -- If this is a nested instance, the parent unit itself resolves to
6479 -- a renaming of the parent instance, whose declaration we need.
6481 -- Finally, the parent may be a generic (not an instance) when the
6482 -- child unit appears as a formal package.
6486 if Present
(Renamed_Entity
(Inst_Par
)) then
6487 Inst_Par
:= Renamed_Entity
(Inst_Par
);
6490 First_Par
:= Inst_Par
;
6493 Generic_Parent
(Specification
(Unit_Declaration_Node
(Inst_Par
)));
6495 First_Gen
:= Gen_Par
;
6497 while Present
(Gen_Par
)
6498 and then Is_Child_Unit
(Gen_Par
)
6500 -- Load grandparent instance as well
6502 Inst_Node
:= Get_Package_Instantiation_Node
(Inst_Par
);
6504 if Nkind
(Name
(Inst_Node
)) = N_Expanded_Name
then
6505 Inst_Par
:= Entity
(Prefix
(Name
(Inst_Node
)));
6507 if Present
(Renamed_Entity
(Inst_Par
)) then
6508 Inst_Par
:= Renamed_Entity
(Inst_Par
);
6513 (Specification
(Unit_Declaration_Node
(Inst_Par
)));
6515 if Present
(Gen_Par
) then
6516 Prepend_Elmt
(Inst_Par
, Ancestors
);
6519 -- Parent is not the name of an instantiation
6521 Install_Noninstance_Specs
(Inst_Par
);
6533 if Present
(First_Gen
) then
6534 Append_Elmt
(First_Par
, Ancestors
);
6537 Install_Noninstance_Specs
(First_Par
);
6540 if not Is_Empty_Elmt_List
(Ancestors
) then
6541 Elmt
:= First_Elmt
(Ancestors
);
6543 while Present
(Elmt
) loop
6544 Install_Spec
(Node
(Elmt
));
6545 Install_Formal_Packages
(Node
(Elmt
));
6556 --------------------------------
6557 -- Instantiate_Formal_Package --
6558 --------------------------------
6560 function Instantiate_Formal_Package
6563 Analyzed_Formal
: Node_Id
) return List_Id
6565 Loc
: constant Source_Ptr
:= Sloc
(Actual
);
6566 Actual_Pack
: Entity_Id
;
6567 Formal_Pack
: Entity_Id
;
6568 Gen_Parent
: Entity_Id
;
6571 Parent_Spec
: Node_Id
;
6573 procedure Find_Matching_Actual
6575 Act
: in out Entity_Id
);
6576 -- We need to associate each formal entity in the formal package
6577 -- with the corresponding entity in the actual package. The actual
6578 -- package has been analyzed and possibly expanded, and as a result
6579 -- there is no one-to-one correspondence between the two lists (for
6580 -- example, the actual may include subtypes, itypes, and inherited
6581 -- primitive operations, interspersed among the renaming declarations
6582 -- for the actuals) . We retrieve the corresponding actual by name
6583 -- because each actual has the same name as the formal, and they do
6584 -- appear in the same order.
6586 function Get_Formal_Entity
(N
: Node_Id
) return Entity_Id
;
6587 -- Retrieve entity of defining entity of generic formal parameter.
6588 -- Only the declarations of formals need to be considered when
6589 -- linking them to actuals, but the declarative list may include
6590 -- internal entities generated during analysis, and those are ignored.
6592 procedure Match_Formal_Entity
6593 (Formal_Node
: Node_Id
;
6594 Formal_Ent
: Entity_Id
;
6595 Actual_Ent
: Entity_Id
);
6596 -- Associates the formal entity with the actual. In the case
6597 -- where Formal_Ent is a formal package, this procedure iterates
6598 -- through all of its formals and enters associations betwen the
6599 -- actuals occurring in the formal package's corresponding actual
6600 -- package (given by Actual_Ent) and the formal package's formal
6601 -- parameters. This procedure recurses if any of the parameters is
6602 -- itself a package.
6604 function Is_Instance_Of
6605 (Act_Spec
: Entity_Id
;
6606 Gen_Anc
: Entity_Id
) return Boolean;
6607 -- The actual can be an instantiation of a generic within another
6608 -- instance, in which case there is no direct link from it to the
6609 -- original generic ancestor. In that case, we recognize that the
6610 -- ultimate ancestor is the same by examining names and scopes.
6612 procedure Map_Entities
(Form
: Entity_Id
; Act
: Entity_Id
);
6613 -- Within the generic part, entities in the formal package are
6614 -- visible. To validate subsequent type declarations, indicate
6615 -- the correspondence betwen the entities in the analyzed formal,
6616 -- and the entities in the actual package. There are three packages
6617 -- involved in the instantiation of a formal package: the parent
6618 -- generic P1 which appears in the generic declaration, the fake
6619 -- instantiation P2 which appears in the analyzed generic, and whose
6620 -- visible entities may be used in subsequent formals, and the actual
6621 -- P3 in the instance. To validate subsequent formals, me indicate
6622 -- that the entities in P2 are mapped into those of P3. The mapping of
6623 -- entities has to be done recursively for nested packages.
6625 procedure Process_Nested_Formal
(Formal
: Entity_Id
);
6626 -- If the current formal is declared with a box, its own formals are
6627 -- visible in the instance, as they were in the generic, and their
6628 -- Hidden flag must be reset. If some of these formals are themselves
6629 -- packages declared with a box, the processing must be recursive.
6631 --------------------------
6632 -- Find_Matching_Actual --
6633 --------------------------
6635 procedure Find_Matching_Actual
6637 Act
: in out Entity_Id
)
6639 Formal_Ent
: Entity_Id
;
6642 case Nkind
(Original_Node
(F
)) is
6643 when N_Formal_Object_Declaration |
6644 N_Formal_Type_Declaration
=>
6645 Formal_Ent
:= Defining_Identifier
(F
);
6647 while Chars
(Act
) /= Chars
(Formal_Ent
) loop
6651 when N_Formal_Subprogram_Declaration |
6652 N_Formal_Package_Declaration |
6653 N_Package_Declaration |
6654 N_Generic_Package_Declaration
=>
6655 Formal_Ent
:= Defining_Entity
(F
);
6657 while Chars
(Act
) /= Chars
(Formal_Ent
) loop
6662 raise Program_Error
;
6664 end Find_Matching_Actual
;
6666 -------------------------
6667 -- Match_Formal_Entity --
6668 -------------------------
6670 procedure Match_Formal_Entity
6671 (Formal_Node
: Node_Id
;
6672 Formal_Ent
: Entity_Id
;
6673 Actual_Ent
: Entity_Id
)
6675 Act_Pkg
: Entity_Id
;
6678 Set_Instance_Of
(Formal_Ent
, Actual_Ent
);
6680 if Ekind
(Actual_Ent
) = E_Package
then
6681 -- Record associations for each parameter
6683 Act_Pkg
:= Actual_Ent
;
6686 A_Ent
: Entity_Id
:= First_Entity
(Act_Pkg
);
6695 -- Retrieve the actual given in the formal package declaration
6697 Actual
:= Entity
(Name
(Original_Node
(Formal_Node
)));
6699 -- The actual in the formal package declaration may be a
6700 -- renamed generic package, in which case we want to retrieve
6701 -- the original generic in order to traverse its formal part.
6703 if Present
(Renamed_Entity
(Actual
)) then
6704 Gen_Decl
:= Unit_Declaration_Node
(Renamed_Entity
(Actual
));
6706 Gen_Decl
:= Unit_Declaration_Node
(Actual
);
6709 Formals
:= Generic_Formal_Declarations
(Gen_Decl
);
6711 if Present
(Formals
) then
6712 F_Node
:= First_Non_Pragma
(Formals
);
6717 while Present
(A_Ent
)
6718 and then Present
(F_Node
)
6719 and then A_Ent
/= First_Private_Entity
(Act_Pkg
)
6721 F_Ent
:= Get_Formal_Entity
(F_Node
);
6723 if Present
(F_Ent
) then
6725 -- This is a formal of the original package. Record
6726 -- association and recurse.
6728 Find_Matching_Actual
(F_Node
, A_Ent
);
6729 Match_Formal_Entity
(F_Node
, F_Ent
, A_Ent
);
6730 Next_Entity
(A_Ent
);
6733 Next_Non_Pragma
(F_Node
);
6737 end Match_Formal_Entity
;
6739 -----------------------
6740 -- Get_Formal_Entity --
6741 -----------------------
6743 function Get_Formal_Entity
(N
: Node_Id
) return Entity_Id
is
6744 Kind
: constant Node_Kind
:= Nkind
(Original_Node
(N
));
6747 when N_Formal_Object_Declaration
=>
6748 return Defining_Identifier
(N
);
6750 when N_Formal_Type_Declaration
=>
6751 return Defining_Identifier
(N
);
6753 when N_Formal_Subprogram_Declaration
=>
6754 return Defining_Unit_Name
(Specification
(N
));
6756 when N_Formal_Package_Declaration
=>
6757 return Defining_Identifier
(Original_Node
(N
));
6759 when N_Generic_Package_Declaration
=>
6760 return Defining_Identifier
(Original_Node
(N
));
6762 -- All other declarations are introduced by semantic analysis
6763 -- and have no match in the actual.
6768 end Get_Formal_Entity
;
6770 --------------------
6771 -- Is_Instance_Of --
6772 --------------------
6774 function Is_Instance_Of
6775 (Act_Spec
: Entity_Id
;
6776 Gen_Anc
: Entity_Id
) return Boolean
6778 Gen_Par
: constant Entity_Id
:= Generic_Parent
(Act_Spec
);
6781 if No
(Gen_Par
) then
6784 -- Simplest case: the generic parent of the actual is the formal
6786 elsif Gen_Par
= Gen_Anc
then
6789 elsif Chars
(Gen_Par
) /= Chars
(Gen_Anc
) then
6792 -- The actual may be obtained through several instantiations. Its
6793 -- scope must itself be an instance of a generic declared in the
6794 -- same scope as the formal. Any other case is detected above.
6796 elsif not Is_Generic_Instance
(Scope
(Gen_Par
)) then
6800 return Generic_Parent
(Parent
(Scope
(Gen_Par
))) = Scope
(Gen_Anc
);
6808 procedure Map_Entities
(Form
: Entity_Id
; Act
: Entity_Id
) is
6813 Set_Instance_Of
(Form
, Act
);
6815 -- Traverse formal and actual package to map the corresponding
6816 -- entities. We skip over internal entities that may be generated
6817 -- during semantic analysis, and find the matching entities by
6818 -- name, given that they must appear in the same order.
6820 E1
:= First_Entity
(Form
);
6821 E2
:= First_Entity
(Act
);
6823 and then E1
/= First_Private_Entity
(Form
)
6825 -- Could this test be a single condition???
6826 -- Seems like it could, and isn't FPE (Form) a constant anyway???
6828 if not Is_Internal
(E1
)
6829 and then Present
(Parent
(E1
))
6830 and then not Is_Class_Wide_Type
(E1
)
6831 and then not Is_Internal_Name
(Chars
(E1
))
6834 and then Chars
(E2
) /= Chars
(E1
)
6842 Set_Instance_Of
(E1
, E2
);
6845 and then Is_Tagged_Type
(E2
)
6848 (Class_Wide_Type
(E1
), Class_Wide_Type
(E2
));
6851 if Ekind
(E1
) = E_Package
6852 and then No
(Renamed_Object
(E1
))
6854 Map_Entities
(E1
, E2
);
6863 ---------------------------
6864 -- Process_Nested_Formal --
6865 ---------------------------
6867 procedure Process_Nested_Formal
(Formal
: Entity_Id
) is
6871 if Present
(Associated_Formal_Package
(Formal
))
6872 and then Box_Present
(Parent
(Associated_Formal_Package
(Formal
)))
6874 Ent
:= First_Entity
(Formal
);
6875 while Present
(Ent
) loop
6876 Set_Is_Hidden
(Ent
, False);
6877 Set_Is_Potentially_Use_Visible
6878 (Ent
, Is_Potentially_Use_Visible
(Formal
));
6880 if Ekind
(Ent
) = E_Package
then
6881 exit when Renamed_Entity
(Ent
) = Renamed_Entity
(Formal
);
6882 Process_Nested_Formal
(Ent
);
6888 end Process_Nested_Formal
;
6890 -- Start of processing for Instantiate_Formal_Package
6895 if not Is_Entity_Name
(Actual
)
6896 or else Ekind
(Entity
(Actual
)) /= E_Package
6899 ("expect package instance to instantiate formal", Actual
);
6900 Abandon_Instantiation
(Actual
);
6901 raise Program_Error
;
6904 Actual_Pack
:= Entity
(Actual
);
6905 Set_Is_Instantiated
(Actual_Pack
);
6907 -- The actual may be a renamed package, or an outer generic
6908 -- formal package whose instantiation is converted into a renaming.
6910 if Present
(Renamed_Object
(Actual_Pack
)) then
6911 Actual_Pack
:= Renamed_Object
(Actual_Pack
);
6914 if Nkind
(Analyzed_Formal
) = N_Formal_Package_Declaration
then
6915 Gen_Parent
:= Get_Instance_Of
(Entity
(Name
(Analyzed_Formal
)));
6916 Formal_Pack
:= Defining_Identifier
(Analyzed_Formal
);
6919 Generic_Parent
(Specification
(Analyzed_Formal
));
6921 Defining_Unit_Name
(Specification
(Analyzed_Formal
));
6924 if Nkind
(Parent
(Actual_Pack
)) = N_Defining_Program_Unit_Name
then
6925 Parent_Spec
:= Specification
(Unit_Declaration_Node
(Actual_Pack
));
6927 Parent_Spec
:= Parent
(Actual_Pack
);
6930 if Gen_Parent
= Any_Id
then
6932 ("previous error in declaration of formal package", Actual
);
6933 Abandon_Instantiation
(Actual
);
6936 Is_Instance_Of
(Parent_Spec
, Get_Instance_Of
(Gen_Parent
))
6942 ("actual parameter must be instance of&", Actual
, Gen_Parent
);
6943 Abandon_Instantiation
(Actual
);
6946 Set_Instance_Of
(Defining_Identifier
(Formal
), Actual_Pack
);
6947 Map_Entities
(Formal_Pack
, Actual_Pack
);
6950 Make_Package_Renaming_Declaration
(Loc
,
6951 Defining_Unit_Name
=> New_Copy
(Defining_Identifier
(Formal
)),
6952 Name
=> New_Reference_To
(Actual_Pack
, Loc
));
6954 Set_Associated_Formal_Package
(Defining_Unit_Name
(Nod
),
6955 Defining_Identifier
(Formal
));
6956 Decls
:= New_List
(Nod
);
6958 -- If the formal F has a box, then the generic declarations are
6959 -- visible in the generic G. In an instance of G, the corresponding
6960 -- entities in the actual for F (which are the actuals for the
6961 -- instantiation of the generic that F denotes) must also be made
6962 -- visible for analysis of the current instance. On exit from the
6963 -- current instance, those entities are made private again. If the
6964 -- actual is currently in use, these entities are also use-visible.
6966 -- The loop through the actual entities also steps through the
6967 -- formal entities and enters associations from formals to
6968 -- actuals into the renaming map. This is necessary to properly
6969 -- handle checking of actual parameter associations for later
6970 -- formals that depend on actuals declared in the formal package.
6972 if Box_Present
(Formal
) then
6974 Gen_Decl
: constant Node_Id
:=
6975 Unit_Declaration_Node
(Gen_Parent
);
6976 Formals
: constant List_Id
:=
6977 Generic_Formal_Declarations
(Gen_Decl
);
6978 Actual_Ent
: Entity_Id
;
6979 Formal_Node
: Node_Id
;
6980 Formal_Ent
: Entity_Id
;
6983 if Present
(Formals
) then
6984 Formal_Node
:= First_Non_Pragma
(Formals
);
6986 Formal_Node
:= Empty
;
6989 Actual_Ent
:= First_Entity
(Actual_Pack
);
6991 while Present
(Actual_Ent
)
6992 and then Actual_Ent
/= First_Private_Entity
(Actual_Pack
)
6994 Set_Is_Hidden
(Actual_Ent
, False);
6995 Set_Is_Potentially_Use_Visible
6996 (Actual_Ent
, In_Use
(Actual_Pack
));
6998 if Ekind
(Actual_Ent
) = E_Package
then
6999 Process_Nested_Formal
(Actual_Ent
);
7002 if Present
(Formal_Node
) then
7003 Formal_Ent
:= Get_Formal_Entity
(Formal_Node
);
7005 if Present
(Formal_Ent
) then
7006 Find_Matching_Actual
(Formal_Node
, Actual_Ent
);
7008 (Formal_Node
, Formal_Ent
, Actual_Ent
);
7011 Next_Non_Pragma
(Formal_Node
);
7014 -- No further formals to match, but the generic
7015 -- part may contain inherited operation that are
7016 -- not hidden in the enclosing instance.
7018 Next_Entity
(Actual_Ent
);
7024 -- If the formal is not declared with a box, reanalyze it as
7025 -- an instantiation, to verify the matching rules of 12.7. The
7026 -- actual checks are performed after the generic associations
7031 I_Pack
: constant Entity_Id
:=
7032 Make_Defining_Identifier
(Sloc
(Actual
),
7033 Chars
=> New_Internal_Name
('P'));
7036 Set_Is_Internal
(I_Pack
);
7039 Make_Package_Instantiation
(Sloc
(Actual
),
7040 Defining_Unit_Name
=> I_Pack
,
7041 Name
=> New_Occurrence_Of
(Gen_Parent
, Sloc
(Actual
)),
7042 Generic_Associations
=>
7043 Generic_Associations
(Formal
)));
7049 end Instantiate_Formal_Package
;
7051 -----------------------------------
7052 -- Instantiate_Formal_Subprogram --
7053 -----------------------------------
7055 function Instantiate_Formal_Subprogram
7058 Analyzed_Formal
: Node_Id
) return Node_Id
7060 Loc
: Source_Ptr
:= Sloc
(Instantiation_Node
);
7061 Formal_Sub
: constant Entity_Id
:=
7062 Defining_Unit_Name
(Specification
(Formal
));
7063 Analyzed_S
: constant Entity_Id
:=
7064 Defining_Unit_Name
(Specification
(Analyzed_Formal
));
7065 Decl_Node
: Node_Id
;
7069 function From_Parent_Scope
(Subp
: Entity_Id
) return Boolean;
7070 -- If the generic is a child unit, the parent has been installed on the
7071 -- scope stack, but a default subprogram cannot resolve to something on
7072 -- the parent because that parent is not really part of the visible
7073 -- context (it is there to resolve explicit local entities). If the
7074 -- default has resolved in this way, we remove the entity from
7075 -- immediate visibility and analyze the node again to emit an error
7076 -- message or find another visible candidate.
7078 procedure Valid_Actual_Subprogram
(Act
: Node_Id
);
7079 -- Perform legality check and raise exception on failure
7081 -----------------------
7082 -- From_Parent_Scope --
7083 -----------------------
7085 function From_Parent_Scope
(Subp
: Entity_Id
) return Boolean is
7086 Gen_Scope
: Node_Id
:= Scope
(Analyzed_S
);
7089 while Present
(Gen_Scope
)
7090 and then Is_Child_Unit
(Gen_Scope
)
7092 if Scope
(Subp
) = Scope
(Gen_Scope
) then
7096 Gen_Scope
:= Scope
(Gen_Scope
);
7100 end From_Parent_Scope
;
7102 -----------------------------
7103 -- Valid_Actual_Subprogram --
7104 -----------------------------
7106 procedure Valid_Actual_Subprogram
(Act
: Node_Id
) is
7107 Act_E
: Entity_Id
:= Empty
;
7110 if Is_Entity_Name
(Act
) then
7111 Act_E
:= Entity
(Act
);
7112 elsif Nkind
(Act
) = N_Selected_Component
7113 and then Is_Entity_Name
(Selector_Name
(Act
))
7115 Act_E
:= Entity
(Selector_Name
(Act
));
7118 if (Present
(Act_E
) and then Is_Overloadable
(Act_E
))
7119 or else Nkind
(Act
) = N_Attribute_Reference
7120 or else Nkind
(Act
) = N_Indexed_Component
7121 or else Nkind
(Act
) = N_Character_Literal
7122 or else Nkind
(Act
) = N_Explicit_Dereference
7128 ("expect subprogram or entry name in instantiation of&",
7129 Instantiation_Node
, Formal_Sub
);
7130 Abandon_Instantiation
(Instantiation_Node
);
7132 end Valid_Actual_Subprogram
;
7134 -- Start of processing for Instantiate_Formal_Subprogram
7137 New_Spec
:= New_Copy_Tree
(Specification
(Formal
));
7139 -- Create new entity for the actual (New_Copy_Tree does not)
7141 Set_Defining_Unit_Name
7142 (New_Spec
, Make_Defining_Identifier
(Loc
, Chars
(Formal_Sub
)));
7144 -- Find entity of actual. If the actual is an attribute reference, it
7145 -- cannot be resolved here (its formal is missing) but is handled
7146 -- instead in Attribute_Renaming. If the actual is overloaded, it is
7147 -- fully resolved subsequently, when the renaming declaration for the
7148 -- formal is analyzed. If it is an explicit dereference, resolve the
7149 -- prefix but not the actual itself, to prevent interpretation as a
7152 if Present
(Actual
) then
7153 Loc
:= Sloc
(Actual
);
7154 Set_Sloc
(New_Spec
, Loc
);
7156 if Nkind
(Actual
) = N_Operator_Symbol
then
7157 Find_Direct_Name
(Actual
);
7159 elsif Nkind
(Actual
) = N_Explicit_Dereference
then
7160 Analyze
(Prefix
(Actual
));
7162 elsif Nkind
(Actual
) /= N_Attribute_Reference
then
7166 Valid_Actual_Subprogram
(Actual
);
7169 elsif Present
(Default_Name
(Formal
)) then
7170 if Nkind
(Default_Name
(Formal
)) /= N_Attribute_Reference
7171 and then Nkind
(Default_Name
(Formal
)) /= N_Selected_Component
7172 and then Nkind
(Default_Name
(Formal
)) /= N_Indexed_Component
7173 and then Nkind
(Default_Name
(Formal
)) /= N_Character_Literal
7174 and then Present
(Entity
(Default_Name
(Formal
)))
7176 Nam
:= New_Occurrence_Of
(Entity
(Default_Name
(Formal
)), Loc
);
7178 Nam
:= New_Copy
(Default_Name
(Formal
));
7179 Set_Sloc
(Nam
, Loc
);
7182 elsif Box_Present
(Formal
) then
7184 -- Actual is resolved at the point of instantiation. Create
7185 -- an identifier or operator with the same name as the formal.
7187 if Nkind
(Formal_Sub
) = N_Defining_Operator_Symbol
then
7188 Nam
:= Make_Operator_Symbol
(Loc
,
7189 Chars
=> Chars
(Formal_Sub
),
7190 Strval
=> No_String
);
7192 Nam
:= Make_Identifier
(Loc
, Chars
(Formal_Sub
));
7195 elsif Nkind
(Specification
(Formal
)) = N_Procedure_Specification
7196 and then Null_Present
(Specification
(Formal
))
7198 -- Generate null body for procedure, for use in the instance
7201 Make_Subprogram_Body
(Loc
,
7202 Specification
=> New_Spec
,
7203 Declarations
=> New_List
,
7204 Handled_Statement_Sequence
=>
7205 Make_Handled_Sequence_Of_Statements
(Loc
,
7206 Statements
=> New_List
(Make_Null_Statement
(Loc
))));
7208 Set_Is_Intrinsic_Subprogram
(Defining_Unit_Name
(New_Spec
));
7212 Error_Msg_Sloc
:= Sloc
(Scope
(Analyzed_S
));
7214 ("missing actual&", Instantiation_Node
, Formal_Sub
);
7216 ("\in instantiation of & declared#",
7217 Instantiation_Node
, Scope
(Analyzed_S
));
7218 Abandon_Instantiation
(Instantiation_Node
);
7222 Make_Subprogram_Renaming_Declaration
(Loc
,
7223 Specification
=> New_Spec
,
7226 -- If we do not have an actual and the formal specified <> then
7227 -- set to get proper default.
7229 if No
(Actual
) and then Box_Present
(Formal
) then
7230 Set_From_Default
(Decl_Node
);
7233 -- Gather possible interpretations for the actual before analyzing the
7234 -- instance. If overloaded, it will be resolved when analyzing the
7235 -- renaming declaration.
7237 if Box_Present
(Formal
)
7238 and then No
(Actual
)
7242 if Is_Child_Unit
(Scope
(Analyzed_S
))
7243 and then Present
(Entity
(Nam
))
7245 if not Is_Overloaded
(Nam
) then
7247 if From_Parent_Scope
(Entity
(Nam
)) then
7248 Set_Is_Immediately_Visible
(Entity
(Nam
), False);
7249 Set_Entity
(Nam
, Empty
);
7250 Set_Etype
(Nam
, Empty
);
7254 Set_Is_Immediately_Visible
(Entity
(Nam
));
7263 Get_First_Interp
(Nam
, I
, It
);
7265 while Present
(It
.Nam
) loop
7266 if From_Parent_Scope
(It
.Nam
) then
7270 Get_Next_Interp
(I
, It
);
7277 -- The generic instantiation freezes the actual. This can only be
7278 -- done once the actual is resolved, in the analysis of the renaming
7279 -- declaration. To make the formal subprogram entity available, we set
7280 -- Corresponding_Formal_Spec to point to the formal subprogram entity.
7281 -- This is also needed in Analyze_Subprogram_Renaming for the processing
7282 -- of formal abstract subprograms.
7284 Set_Corresponding_Formal_Spec
(Decl_Node
, Analyzed_S
);
7286 -- We cannot analyze the renaming declaration, and thus find the
7287 -- actual, until the all the actuals are assembled in the instance.
7288 -- For subsequent checks of other actuals, indicate the node that
7289 -- will hold the instance of this formal.
7291 Set_Instance_Of
(Analyzed_S
, Nam
);
7293 if Nkind
(Actual
) = N_Selected_Component
7294 and then Is_Task_Type
(Etype
(Prefix
(Actual
)))
7295 and then not Is_Frozen
(Etype
(Prefix
(Actual
)))
7297 -- The renaming declaration will create a body, which must appear
7298 -- outside of the instantiation, We move the renaming declaration
7299 -- out of the instance, and create an additional renaming inside,
7300 -- to prevent freezing anomalies.
7303 Anon_Id
: constant Entity_Id
:=
7304 Make_Defining_Identifier
7305 (Loc
, New_Internal_Name
('E'));
7307 Set_Defining_Unit_Name
(New_Spec
, Anon_Id
);
7308 Insert_Before
(Instantiation_Node
, Decl_Node
);
7309 Analyze
(Decl_Node
);
7311 -- Now create renaming within the instance
7314 Make_Subprogram_Renaming_Declaration
(Loc
,
7315 Specification
=> New_Copy_Tree
(New_Spec
),
7316 Name
=> New_Occurrence_Of
(Anon_Id
, Loc
));
7318 Set_Defining_Unit_Name
(Specification
(Decl_Node
),
7319 Make_Defining_Identifier
(Loc
, Chars
(Formal_Sub
)));
7324 end Instantiate_Formal_Subprogram
;
7326 ------------------------
7327 -- Instantiate_Object --
7328 ------------------------
7330 function Instantiate_Object
7333 Analyzed_Formal
: Node_Id
) return List_Id
7335 Formal_Id
: constant Entity_Id
:= Defining_Identifier
(Formal
);
7336 Type_Id
: constant Node_Id
:= Subtype_Mark
(Formal
);
7337 Loc
: constant Source_Ptr
:= Sloc
(Actual
);
7338 Act_Assoc
: constant Node_Id
:= Parent
(Actual
);
7339 Orig_Ftyp
: constant Entity_Id
:=
7340 Etype
(Defining_Identifier
(Analyzed_Formal
));
7341 List
: constant List_Id
:= New_List
;
7343 Decl_Node
: Node_Id
;
7344 Subt_Decl
: Node_Id
:= Empty
;
7347 -- Sloc for error message on missing actual
7349 Error_Msg_Sloc
:= Sloc
(Scope
(Defining_Identifier
(Analyzed_Formal
)));
7351 if Get_Instance_Of
(Formal_Id
) /= Formal_Id
then
7352 Error_Msg_N
("duplicate instantiation of generic parameter", Actual
);
7355 Set_Parent
(List
, Parent
(Actual
));
7359 if Out_Present
(Formal
) then
7361 -- An IN OUT generic actual must be a name. The instantiation is a
7362 -- renaming declaration. The actual is the name being renamed. We
7363 -- use the actual directly, rather than a copy, because it is not
7364 -- used further in the list of actuals, and because a copy or a use
7365 -- of relocate_node is incorrect if the instance is nested within a
7366 -- generic. In order to simplify ASIS searches, the Generic_Parent
7367 -- field links the declaration to the generic association.
7372 Instantiation_Node
, Formal_Id
);
7374 ("\in instantiation of & declared#",
7376 Scope
(Defining_Identifier
(Analyzed_Formal
)));
7377 Abandon_Instantiation
(Instantiation_Node
);
7381 Make_Object_Renaming_Declaration
(Loc
,
7382 Defining_Identifier
=> New_Copy
(Formal_Id
),
7383 Subtype_Mark
=> New_Copy_Tree
(Type_Id
),
7386 Set_Corresponding_Generic_Association
(Decl_Node
, Act_Assoc
);
7388 -- The analysis of the actual may produce insert_action nodes, so
7389 -- the declaration must have a context in which to attach them.
7391 Append
(Decl_Node
, List
);
7394 -- Return if the analysis of the actual reported some error
7396 if Etype
(Actual
) = Any_Type
then
7400 -- This check is performed here because Analyze_Object_Renaming
7401 -- will not check it when Comes_From_Source is False. Note
7402 -- though that the check for the actual being the name of an
7403 -- object will be performed in Analyze_Object_Renaming.
7405 if Is_Object_Reference
(Actual
)
7406 and then Is_Dependent_Component_Of_Mutable_Object
(Actual
)
7409 ("illegal discriminant-dependent component for in out parameter",
7413 -- The actual has to be resolved in order to check that it is
7414 -- a variable (due to cases such as F(1), where F returns
7415 -- access to an array, and for overloaded prefixes).
7418 Get_Instance_Of
(Etype
(Defining_Identifier
(Analyzed_Formal
)));
7420 if Is_Private_Type
(Ftyp
)
7421 and then not Is_Private_Type
(Etype
(Actual
))
7422 and then (Base_Type
(Full_View
(Ftyp
)) = Base_Type
(Etype
(Actual
))
7423 or else Base_Type
(Etype
(Actual
)) = Ftyp
)
7425 -- If the actual has the type of the full view of the formal,
7426 -- or else a non-private subtype of the formal, then
7427 -- the visibility of the formal type has changed. Add to the
7428 -- actuals a subtype declaration that will force the exchange
7429 -- of views in the body of the instance as well.
7432 Make_Subtype_Declaration
(Loc
,
7433 Defining_Identifier
=>
7434 Make_Defining_Identifier
(Loc
, New_Internal_Name
('P')),
7435 Subtype_Indication
=> New_Occurrence_Of
(Ftyp
, Loc
));
7437 Prepend
(Subt_Decl
, List
);
7439 Prepend_Elmt
(Full_View
(Ftyp
), Exchanged_Views
);
7440 Exchange_Declarations
(Ftyp
);
7443 Resolve
(Actual
, Ftyp
);
7445 if not Is_Variable
(Actual
) or else Paren_Count
(Actual
) > 0 then
7447 ("actual for& must be a variable", Actual
, Formal_Id
);
7449 elsif Base_Type
(Ftyp
) /= Base_Type
(Etype
(Actual
)) then
7451 "type of actual does not match type of&", Actual
, Formal_Id
);
7455 Note_Possible_Modification
(Actual
);
7457 -- Check for instantiation of atomic/volatile actual for
7458 -- non-atomic/volatile formal (RM C.6 (12)).
7460 if Is_Atomic_Object
(Actual
)
7461 and then not Is_Atomic
(Orig_Ftyp
)
7464 ("cannot instantiate non-atomic formal object " &
7465 "with atomic actual", Actual
);
7467 elsif Is_Volatile_Object
(Actual
)
7468 and then not Is_Volatile
(Orig_Ftyp
)
7471 ("cannot instantiate non-volatile formal object " &
7472 "with volatile actual", Actual
);
7478 -- The instantiation of a generic formal in-parameter
7479 -- is a constant declaration. The actual is the expression for
7480 -- that declaration.
7482 if Present
(Actual
) then
7484 Decl_Node
:= Make_Object_Declaration
(Loc
,
7485 Defining_Identifier
=> New_Copy
(Formal_Id
),
7486 Constant_Present
=> True,
7487 Object_Definition
=> New_Copy_Tree
(Type_Id
),
7488 Expression
=> Actual
);
7490 Set_Corresponding_Generic_Association
(Decl_Node
, Act_Assoc
);
7492 -- A generic formal object of a tagged type is defined
7493 -- to be aliased so the new constant must also be treated
7497 (Etype
(Defining_Identifier
(Analyzed_Formal
)))
7499 Set_Aliased_Present
(Decl_Node
);
7502 Append
(Decl_Node
, List
);
7504 -- No need to repeat (pre-)analysis of some expression nodes
7505 -- already handled in Pre_Analyze_Actuals.
7507 if Nkind
(Actual
) /= N_Allocator
then
7510 -- Return if the analysis of the actual reported some error
7512 if Etype
(Actual
) = Any_Type
then
7518 Typ
: constant Entity_Id
:=
7520 (Etype
(Defining_Identifier
(Analyzed_Formal
)));
7523 Freeze_Before
(Instantiation_Node
, Typ
);
7525 -- If the actual is an aggregate, perform name resolution on
7526 -- its components (the analysis of an aggregate does not do
7527 -- it) to capture local names that may be hidden if the
7528 -- generic is a child unit.
7530 if Nkind
(Actual
) = N_Aggregate
then
7531 Pre_Analyze_And_Resolve
(Actual
, Typ
);
7535 elsif Present
(Expression
(Formal
)) then
7537 -- Use default to construct declaration
7540 Make_Object_Declaration
(Sloc
(Formal
),
7541 Defining_Identifier
=> New_Copy
(Formal_Id
),
7542 Constant_Present
=> True,
7543 Object_Definition
=> New_Copy
(Type_Id
),
7544 Expression
=> New_Copy_Tree
(Expression
(Formal
)));
7546 Append
(Decl_Node
, List
);
7547 Set_Analyzed
(Expression
(Decl_Node
), False);
7552 Instantiation_Node
, Formal_Id
);
7553 Error_Msg_NE
("\in instantiation of & declared#",
7555 Scope
(Defining_Identifier
(Analyzed_Formal
)));
7558 (Etype
(Defining_Identifier
(Analyzed_Formal
)))
7560 -- Create dummy constant declaration so that instance can
7561 -- be analyzed, to minimize cascaded visibility errors.
7564 Make_Object_Declaration
(Loc
,
7565 Defining_Identifier
=> New_Copy
(Formal_Id
),
7566 Constant_Present
=> True,
7567 Object_Definition
=> New_Copy
(Type_Id
),
7569 Make_Attribute_Reference
(Sloc
(Formal_Id
),
7570 Attribute_Name
=> Name_First
,
7571 Prefix
=> New_Copy
(Type_Id
)));
7573 Append
(Decl_Node
, List
);
7576 Abandon_Instantiation
(Instantiation_Node
);
7583 end Instantiate_Object
;
7585 ------------------------------
7586 -- Instantiate_Package_Body --
7587 ------------------------------
7589 procedure Instantiate_Package_Body
7590 (Body_Info
: Pending_Body_Info
;
7591 Inlined_Body
: Boolean := False)
7593 Act_Decl
: constant Node_Id
:= Body_Info
.Act_Decl
;
7594 Inst_Node
: constant Node_Id
:= Body_Info
.Inst_Node
;
7595 Loc
: constant Source_Ptr
:= Sloc
(Inst_Node
);
7597 Gen_Id
: constant Node_Id
:= Name
(Inst_Node
);
7598 Gen_Unit
: constant Entity_Id
:= Get_Generic_Entity
(Inst_Node
);
7599 Gen_Decl
: constant Node_Id
:= Unit_Declaration_Node
(Gen_Unit
);
7600 Act_Spec
: constant Node_Id
:= Specification
(Act_Decl
);
7601 Act_Decl_Id
: constant Entity_Id
:= Defining_Entity
(Act_Spec
);
7603 Act_Body_Name
: Node_Id
;
7605 Gen_Body_Id
: Node_Id
;
7607 Act_Body_Id
: Entity_Id
;
7609 Parent_Installed
: Boolean := False;
7610 Save_Style_Check
: constant Boolean := Style_Check
;
7613 Gen_Body_Id
:= Corresponding_Body
(Gen_Decl
);
7615 -- The instance body may already have been processed, as the parent
7616 -- of another instance that is inlined. (Load_Parent_Of_Generic).
7618 if Present
(Corresponding_Body
(Instance_Spec
(Inst_Node
))) then
7622 Expander_Mode_Save_And_Set
(Body_Info
.Expander_Status
);
7624 if No
(Gen_Body_Id
) then
7625 Load_Parent_Of_Generic
(Inst_Node
, Specification
(Gen_Decl
));
7626 Gen_Body_Id
:= Corresponding_Body
(Gen_Decl
);
7629 -- Establish global variable for sloc adjustment and for error
7632 Instantiation_Node
:= Inst_Node
;
7634 if Present
(Gen_Body_Id
) then
7635 Save_Env
(Gen_Unit
, Act_Decl_Id
);
7636 Style_Check
:= False;
7637 Current_Sem_Unit
:= Body_Info
.Current_Sem_Unit
;
7639 Gen_Body
:= Unit_Declaration_Node
(Gen_Body_Id
);
7641 Create_Instantiation_Source
7642 (Inst_Node
, Gen_Body_Id
, False, S_Adjustment
);
7646 (Original_Node
(Gen_Body
), Empty
, Instantiating
=> True);
7648 -- Build new name (possibly qualified) for body declaration
7650 Act_Body_Id
:= New_Copy
(Act_Decl_Id
);
7652 -- Some attributes of the spec entity are not inherited by the
7655 Set_Handler_Records
(Act_Body_Id
, No_List
);
7657 if Nkind
(Defining_Unit_Name
(Act_Spec
)) =
7658 N_Defining_Program_Unit_Name
7661 Make_Defining_Program_Unit_Name
(Loc
,
7662 Name
=> New_Copy_Tree
(Name
(Defining_Unit_Name
(Act_Spec
))),
7663 Defining_Identifier
=> Act_Body_Id
);
7665 Act_Body_Name
:= Act_Body_Id
;
7668 Set_Defining_Unit_Name
(Act_Body
, Act_Body_Name
);
7670 Set_Corresponding_Spec
(Act_Body
, Act_Decl_Id
);
7671 Check_Generic_Actuals
(Act_Decl_Id
, False);
7673 -- If it is a child unit, make the parent instance (which is an
7674 -- instance of the parent of the generic) visible. The parent
7675 -- instance is the prefix of the name of the generic unit.
7677 if Ekind
(Scope
(Gen_Unit
)) = E_Generic_Package
7678 and then Nkind
(Gen_Id
) = N_Expanded_Name
7680 Install_Parent
(Entity
(Prefix
(Gen_Id
)), In_Body
=> True);
7681 Parent_Installed
:= True;
7683 elsif Is_Child_Unit
(Gen_Unit
) then
7684 Install_Parent
(Scope
(Gen_Unit
), In_Body
=> True);
7685 Parent_Installed
:= True;
7688 -- If the instantiation is a library unit, and this is the main
7689 -- unit, then build the resulting compilation unit nodes for the
7690 -- instance. If this is a compilation unit but it is not the main
7691 -- unit, then it is the body of a unit in the context, that is being
7692 -- compiled because it is encloses some inlined unit or another
7693 -- generic unit being instantiated. In that case, this body is not
7694 -- part of the current compilation, and is not attached to the tree,
7695 -- but its parent must be set for analysis.
7697 if Nkind
(Parent
(Inst_Node
)) = N_Compilation_Unit
then
7699 -- Replace instance node with body of instance, and create
7700 -- new node for corresponding instance declaration.
7702 Build_Instance_Compilation_Unit_Nodes
7703 (Inst_Node
, Act_Body
, Act_Decl
);
7704 Analyze
(Inst_Node
);
7706 if Parent
(Inst_Node
) = Cunit
(Main_Unit
) then
7708 -- If the instance is a child unit itself, then set the
7709 -- scope of the expanded body to be the parent of the
7710 -- instantiation (ensuring that the fully qualified name
7711 -- will be generated for the elaboration subprogram).
7713 if Nkind
(Defining_Unit_Name
(Act_Spec
)) =
7714 N_Defining_Program_Unit_Name
7717 (Defining_Entity
(Inst_Node
), Scope
(Act_Decl_Id
));
7721 -- Case where instantiation is not a library unit
7724 -- If this is an early instantiation, i.e. appears textually
7725 -- before the corresponding body and must be elaborated first,
7726 -- indicate that the body instance is to be delayed.
7728 Install_Body
(Act_Body
, Inst_Node
, Gen_Body
, Gen_Decl
);
7730 -- Now analyze the body. We turn off all checks if this is
7731 -- an internal unit, since there is no reason to have checks
7732 -- on for any predefined run-time library code. All such
7733 -- code is designed to be compiled with checks off.
7735 -- Note that we do NOT apply this criterion to children of
7736 -- GNAT (or on VMS, children of DEC). The latter units must
7737 -- suppress checks explicitly if this is needed.
7739 if Is_Predefined_File_Name
7740 (Unit_File_Name
(Get_Source_Unit
(Gen_Decl
)))
7742 Analyze
(Act_Body
, Suppress
=> All_Checks
);
7748 if not Generic_Separately_Compiled
(Gen_Unit
) then
7749 Inherit_Context
(Gen_Body
, Inst_Node
);
7752 -- Remove the parent instances if they have been placed on the
7753 -- scope stack to compile the body.
7755 if Parent_Installed
then
7756 Remove_Parent
(In_Body
=> True);
7759 Restore_Private_Views
(Act_Decl_Id
);
7761 -- Remove the current unit from visibility if this is an instance
7762 -- that is not elaborated on the fly for inlining purposes.
7764 if not Inlined_Body
then
7765 Set_Is_Immediately_Visible
(Act_Decl_Id
, False);
7769 Style_Check
:= Save_Style_Check
;
7771 -- If we have no body, and the unit requires a body, then complain.
7772 -- This complaint is suppressed if we have detected other errors
7773 -- (since a common reason for missing the body is that it had errors).
7775 elsif Unit_Requires_Body
(Gen_Unit
) then
7776 if Serious_Errors_Detected
= 0 then
7778 ("cannot find body of generic package &", Inst_Node
, Gen_Unit
);
7780 -- Don't attempt to perform any cleanup actions if some other
7781 -- error was aready detected, since this can cause blowups.
7787 -- Case of package that does not need a body
7790 -- If the instantiation of the declaration is a library unit,
7791 -- rewrite the original package instantiation as a package
7792 -- declaration in the compilation unit node.
7794 if Nkind
(Parent
(Inst_Node
)) = N_Compilation_Unit
then
7795 Set_Parent_Spec
(Act_Decl
, Parent_Spec
(Inst_Node
));
7796 Rewrite
(Inst_Node
, Act_Decl
);
7798 -- Generate elaboration entity, in case spec has elaboration
7799 -- code. This cannot be done when the instance is analyzed,
7800 -- because it is not known yet whether the body exists.
7802 Set_Elaboration_Entity_Required
(Act_Decl_Id
, False);
7803 Build_Elaboration_Entity
(Parent
(Inst_Node
), Act_Decl_Id
);
7805 -- If the instantiation is not a library unit, then append the
7806 -- declaration to the list of implicitly generated entities.
7807 -- unless it is already a list member which means that it was
7808 -- already processed
7810 elsif not Is_List_Member
(Act_Decl
) then
7811 Mark_Rewrite_Insertion
(Act_Decl
);
7812 Insert_Before
(Inst_Node
, Act_Decl
);
7816 Expander_Mode_Restore
;
7817 end Instantiate_Package_Body
;
7819 ---------------------------------
7820 -- Instantiate_Subprogram_Body --
7821 ---------------------------------
7823 procedure Instantiate_Subprogram_Body
7824 (Body_Info
: Pending_Body_Info
)
7826 Act_Decl
: constant Node_Id
:= Body_Info
.Act_Decl
;
7827 Inst_Node
: constant Node_Id
:= Body_Info
.Inst_Node
;
7828 Loc
: constant Source_Ptr
:= Sloc
(Inst_Node
);
7829 Gen_Id
: constant Node_Id
:= Name
(Inst_Node
);
7830 Gen_Unit
: constant Entity_Id
:= Get_Generic_Entity
(Inst_Node
);
7831 Gen_Decl
: constant Node_Id
:= Unit_Declaration_Node
(Gen_Unit
);
7832 Anon_Id
: constant Entity_Id
:=
7833 Defining_Unit_Name
(Specification
(Act_Decl
));
7834 Pack_Id
: constant Entity_Id
:=
7835 Defining_Unit_Name
(Parent
(Act_Decl
));
7838 Gen_Body_Id
: Node_Id
;
7840 Act_Body_Id
: Entity_Id
;
7841 Pack_Body
: Node_Id
;
7842 Prev_Formal
: Entity_Id
;
7844 Unit_Renaming
: Node_Id
;
7846 Parent_Installed
: Boolean := False;
7847 Save_Style_Check
: constant Boolean := Style_Check
;
7850 Gen_Body_Id
:= Corresponding_Body
(Gen_Decl
);
7852 Expander_Mode_Save_And_Set
(Body_Info
.Expander_Status
);
7854 if No
(Gen_Body_Id
) then
7855 Load_Parent_Of_Generic
(Inst_Node
, Specification
(Gen_Decl
));
7856 Gen_Body_Id
:= Corresponding_Body
(Gen_Decl
);
7859 Instantiation_Node
:= Inst_Node
;
7861 if Present
(Gen_Body_Id
) then
7862 Gen_Body
:= Unit_Declaration_Node
(Gen_Body_Id
);
7864 if Nkind
(Gen_Body
) = N_Subprogram_Body_Stub
then
7866 -- Either body is not present, or context is non-expanding, as
7867 -- when compiling a subunit. Mark the instance as completed, and
7868 -- diagnose a missing body when needed.
7871 and then Operating_Mode
= Generate_Code
7874 ("missing proper body for instantiation", Gen_Body
);
7877 Set_Has_Completion
(Anon_Id
);
7881 Save_Env
(Gen_Unit
, Anon_Id
);
7882 Style_Check
:= False;
7883 Current_Sem_Unit
:= Body_Info
.Current_Sem_Unit
;
7884 Create_Instantiation_Source
7892 (Original_Node
(Gen_Body
), Empty
, Instantiating
=> True);
7893 Act_Body_Id
:= Defining_Entity
(Act_Body
);
7894 Set_Chars
(Act_Body_Id
, Chars
(Anon_Id
));
7895 Set_Sloc
(Act_Body_Id
, Sloc
(Defining_Entity
(Inst_Node
)));
7896 Set_Corresponding_Spec
(Act_Body
, Anon_Id
);
7897 Set_Has_Completion
(Anon_Id
);
7898 Check_Generic_Actuals
(Pack_Id
, False);
7900 -- If it is a child unit, make the parent instance (which is an
7901 -- instance of the parent of the generic) visible. The parent
7902 -- instance is the prefix of the name of the generic unit.
7904 if Ekind
(Scope
(Gen_Unit
)) = E_Generic_Package
7905 and then Nkind
(Gen_Id
) = N_Expanded_Name
7907 Install_Parent
(Entity
(Prefix
(Gen_Id
)), In_Body
=> True);
7908 Parent_Installed
:= True;
7910 elsif Is_Child_Unit
(Gen_Unit
) then
7911 Install_Parent
(Scope
(Gen_Unit
), In_Body
=> True);
7912 Parent_Installed
:= True;
7915 -- Inside its body, a reference to the generic unit is a reference
7916 -- to the instance. The corresponding renaming is the first
7917 -- declaration in the body.
7920 Make_Subprogram_Renaming_Declaration
(Loc
,
7923 Specification
(Original_Node
(Gen_Body
)),
7925 Instantiating
=> True),
7926 Name
=> New_Occurrence_Of
(Anon_Id
, Loc
));
7928 -- If there is a formal subprogram with the same name as the
7929 -- unit itself, do not add this renaming declaration. This is
7930 -- a temporary fix for one ACVC test. ???
7932 Prev_Formal
:= First_Entity
(Pack_Id
);
7933 while Present
(Prev_Formal
) loop
7934 if Chars
(Prev_Formal
) = Chars
(Gen_Unit
)
7935 and then Is_Overloadable
(Prev_Formal
)
7940 Next_Entity
(Prev_Formal
);
7943 if Present
(Prev_Formal
) then
7944 Decls
:= New_List
(Act_Body
);
7946 Decls
:= New_List
(Unit_Renaming
, Act_Body
);
7949 -- The subprogram body is placed in the body of a dummy package
7950 -- body, whose spec contains the subprogram declaration as well
7951 -- as the renaming declarations for the generic parameters.
7953 Pack_Body
:= Make_Package_Body
(Loc
,
7954 Defining_Unit_Name
=> New_Copy
(Pack_Id
),
7955 Declarations
=> Decls
);
7957 Set_Corresponding_Spec
(Pack_Body
, Pack_Id
);
7959 -- If the instantiation is a library unit, then build resulting
7960 -- compilation unit nodes for the instance. The declaration of
7961 -- the enclosing package is the grandparent of the subprogram
7962 -- declaration. First replace the instantiation node as the unit
7963 -- of the corresponding compilation.
7965 if Nkind
(Parent
(Inst_Node
)) = N_Compilation_Unit
then
7966 if Parent
(Inst_Node
) = Cunit
(Main_Unit
) then
7967 Set_Unit
(Parent
(Inst_Node
), Inst_Node
);
7968 Build_Instance_Compilation_Unit_Nodes
7969 (Inst_Node
, Pack_Body
, Parent
(Parent
(Act_Decl
)));
7970 Analyze
(Inst_Node
);
7972 Set_Parent
(Pack_Body
, Parent
(Inst_Node
));
7973 Analyze
(Pack_Body
);
7977 Insert_Before
(Inst_Node
, Pack_Body
);
7978 Mark_Rewrite_Insertion
(Pack_Body
);
7979 Analyze
(Pack_Body
);
7981 if Expander_Active
then
7982 Freeze_Subprogram_Body
(Inst_Node
, Gen_Body
, Pack_Id
);
7986 if not Generic_Separately_Compiled
(Gen_Unit
) then
7987 Inherit_Context
(Gen_Body
, Inst_Node
);
7990 Restore_Private_Views
(Pack_Id
, False);
7992 if Parent_Installed
then
7993 Remove_Parent
(In_Body
=> True);
7997 Style_Check
:= Save_Style_Check
;
7999 -- Body not found. Error was emitted already. If there were no
8000 -- previous errors, this may be an instance whose scope is a premature
8001 -- instance. In that case we must insure that the (legal) program does
8002 -- raise program error if executed. We generate a subprogram body for
8003 -- this purpose. See DEC ac30vso.
8005 elsif Serious_Errors_Detected
= 0
8006 and then Nkind
(Parent
(Inst_Node
)) /= N_Compilation_Unit
8008 if Ekind
(Anon_Id
) = E_Procedure
then
8010 Make_Subprogram_Body
(Loc
,
8012 Make_Procedure_Specification
(Loc
,
8013 Defining_Unit_Name
=>
8014 Make_Defining_Identifier
(Loc
, Chars
(Anon_Id
)),
8015 Parameter_Specifications
=>
8017 (Parameter_Specifications
(Parent
(Anon_Id
)))),
8019 Declarations
=> Empty_List
,
8020 Handled_Statement_Sequence
=>
8021 Make_Handled_Sequence_Of_Statements
(Loc
,
8024 Make_Raise_Program_Error
(Loc
,
8026 PE_Access_Before_Elaboration
))));
8030 Make_Raise_Program_Error
(Loc
,
8031 Reason
=> PE_Access_Before_Elaboration
);
8033 Set_Etype
(Ret_Expr
, (Etype
(Anon_Id
)));
8034 Set_Analyzed
(Ret_Expr
);
8037 Make_Subprogram_Body
(Loc
,
8039 Make_Function_Specification
(Loc
,
8040 Defining_Unit_Name
=>
8041 Make_Defining_Identifier
(Loc
, Chars
(Anon_Id
)),
8042 Parameter_Specifications
=>
8044 (Parameter_Specifications
(Parent
(Anon_Id
))),
8045 Result_Definition
=>
8046 New_Occurrence_Of
(Etype
(Anon_Id
), Loc
)),
8048 Declarations
=> Empty_List
,
8049 Handled_Statement_Sequence
=>
8050 Make_Handled_Sequence_Of_Statements
(Loc
,
8052 New_List
(Make_Return_Statement
(Loc
, Ret_Expr
))));
8055 Pack_Body
:= Make_Package_Body
(Loc
,
8056 Defining_Unit_Name
=> New_Copy
(Pack_Id
),
8057 Declarations
=> New_List
(Act_Body
));
8059 Insert_After
(Inst_Node
, Pack_Body
);
8060 Set_Corresponding_Spec
(Pack_Body
, Pack_Id
);
8061 Analyze
(Pack_Body
);
8064 Expander_Mode_Restore
;
8065 end Instantiate_Subprogram_Body
;
8067 ----------------------
8068 -- Instantiate_Type --
8069 ----------------------
8071 function Instantiate_Type
8074 Analyzed_Formal
: Node_Id
;
8075 Actual_Decls
: List_Id
) return Node_Id
8077 Loc
: constant Source_Ptr
:= Sloc
(Actual
);
8078 Gen_T
: constant Entity_Id
:= Defining_Identifier
(Formal
);
8079 A_Gen_T
: constant Entity_Id
:= Defining_Identifier
(Analyzed_Formal
);
8080 Ancestor
: Entity_Id
:= Empty
;
8081 Def
: constant Node_Id
:= Formal_Type_Definition
(Formal
);
8083 Decl_Node
: Node_Id
;
8085 procedure Validate_Array_Type_Instance
;
8086 procedure Validate_Access_Subprogram_Instance
;
8087 procedure Validate_Access_Type_Instance
;
8088 procedure Validate_Derived_Type_Instance
;
8089 procedure Validate_Derived_Interface_Type_Instance
;
8090 procedure Validate_Interface_Type_Instance
;
8091 procedure Validate_Private_Type_Instance
;
8092 -- These procedures perform validation tests for the named case
8094 function Subtypes_Match
(Gen_T
, Act_T
: Entity_Id
) return Boolean;
8095 -- Check that base types are the same and that the subtypes match
8096 -- statically. Used in several of the above.
8098 --------------------
8099 -- Subtypes_Match --
8100 --------------------
8102 function Subtypes_Match
(Gen_T
, Act_T
: Entity_Id
) return Boolean is
8103 T
: constant Entity_Id
:= Get_Instance_Of
(Gen_T
);
8106 return (Base_Type
(T
) = Base_Type
(Act_T
)
8107 and then Subtypes_Statically_Match
(T
, Act_T
))
8109 or else (Is_Class_Wide_Type
(Gen_T
)
8110 and then Is_Class_Wide_Type
(Act_T
)
8113 (Get_Instance_Of
(Root_Type
(Gen_T
)),
8117 ((Ekind
(Gen_T
) = E_Anonymous_Access_Subprogram_Type
8118 or else Ekind
(Gen_T
) = E_Anonymous_Access_Type
)
8119 and then Ekind
(Act_T
) = Ekind
(Gen_T
)
8121 Subtypes_Statically_Match
8122 (Designated_Type
(Gen_T
), Designated_Type
(Act_T
)));
8125 -----------------------------------------
8126 -- Validate_Access_Subprogram_Instance --
8127 -----------------------------------------
8129 procedure Validate_Access_Subprogram_Instance
is
8131 if not Is_Access_Type
(Act_T
)
8132 or else Ekind
(Designated_Type
(Act_T
)) /= E_Subprogram_Type
8135 ("expect access type in instantiation of &", Actual
, Gen_T
);
8136 Abandon_Instantiation
(Actual
);
8139 Check_Mode_Conformant
8140 (Designated_Type
(Act_T
),
8141 Designated_Type
(A_Gen_T
),
8145 if Ekind
(Base_Type
(Act_T
)) = E_Access_Protected_Subprogram_Type
then
8146 if Ekind
(A_Gen_T
) = E_Access_Subprogram_Type
then
8148 ("protected access type not allowed for formal &",
8152 elsif Ekind
(A_Gen_T
) = E_Access_Protected_Subprogram_Type
then
8154 ("expect protected access type for formal &",
8157 end Validate_Access_Subprogram_Instance
;
8159 -----------------------------------
8160 -- Validate_Access_Type_Instance --
8161 -----------------------------------
8163 procedure Validate_Access_Type_Instance
is
8164 Desig_Type
: constant Entity_Id
:=
8166 (Designated_Type
(A_Gen_T
), Scope
(A_Gen_T
));
8169 if not Is_Access_Type
(Act_T
) then
8171 ("expect access type in instantiation of &", Actual
, Gen_T
);
8172 Abandon_Instantiation
(Actual
);
8175 if Is_Access_Constant
(A_Gen_T
) then
8176 if not Is_Access_Constant
(Act_T
) then
8178 ("actual type must be access-to-constant type", Actual
);
8179 Abandon_Instantiation
(Actual
);
8182 if Is_Access_Constant
(Act_T
) then
8184 ("actual type must be access-to-variable type", Actual
);
8185 Abandon_Instantiation
(Actual
);
8187 elsif Ekind
(A_Gen_T
) = E_General_Access_Type
8188 and then Ekind
(Base_Type
(Act_T
)) /= E_General_Access_Type
8190 Error_Msg_N
("actual must be general access type!", Actual
);
8191 Error_Msg_NE
("add ALL to }!", Actual
, Act_T
);
8192 Abandon_Instantiation
(Actual
);
8196 -- The designated subtypes, that is to say the subtypes introduced
8197 -- by an access type declaration (and not by a subtype declaration)
8200 if not Subtypes_Match
8201 (Desig_Type
, Designated_Type
(Base_Type
(Act_T
)))
8204 ("designated type of actual does not match that of formal &",
8206 Abandon_Instantiation
(Actual
);
8208 elsif Is_Access_Type
(Designated_Type
(Act_T
))
8209 and then Is_Constrained
(Designated_Type
(Designated_Type
(Act_T
)))
8211 Is_Constrained
(Designated_Type
(Desig_Type
))
8214 ("designated type of actual does not match that of formal &",
8216 Abandon_Instantiation
(Actual
);
8218 end Validate_Access_Type_Instance
;
8220 ----------------------------------
8221 -- Validate_Array_Type_Instance --
8222 ----------------------------------
8224 procedure Validate_Array_Type_Instance
is
8229 function Formal_Dimensions
return Int
;
8230 -- Count number of dimensions in array type formal
8232 -----------------------
8233 -- Formal_Dimensions --
8234 -----------------------
8236 function Formal_Dimensions
return Int
is
8241 if Nkind
(Def
) = N_Constrained_Array_Definition
then
8242 Index
:= First
(Discrete_Subtype_Definitions
(Def
));
8244 Index
:= First
(Subtype_Marks
(Def
));
8247 while Present
(Index
) loop
8253 end Formal_Dimensions
;
8255 -- Start of processing for Validate_Array_Type_Instance
8258 if not Is_Array_Type
(Act_T
) then
8260 ("expect array type in instantiation of &", Actual
, Gen_T
);
8261 Abandon_Instantiation
(Actual
);
8263 elsif Nkind
(Def
) = N_Constrained_Array_Definition
then
8264 if not (Is_Constrained
(Act_T
)) then
8266 ("expect constrained array in instantiation of &",
8268 Abandon_Instantiation
(Actual
);
8272 if Is_Constrained
(Act_T
) then
8274 ("expect unconstrained array in instantiation of &",
8276 Abandon_Instantiation
(Actual
);
8280 if Formal_Dimensions
/= Number_Dimensions
(Act_T
) then
8282 ("dimensions of actual do not match formal &", Actual
, Gen_T
);
8283 Abandon_Instantiation
(Actual
);
8286 I1
:= First_Index
(A_Gen_T
);
8287 I2
:= First_Index
(Act_T
);
8288 for J
in 1 .. Formal_Dimensions
loop
8290 -- If the indices of the actual were given by a subtype_mark,
8291 -- the index was transformed into a range attribute. Retrieve
8292 -- the original type mark for checking.
8294 if Is_Entity_Name
(Original_Node
(I2
)) then
8295 T2
:= Entity
(Original_Node
(I2
));
8300 if not Subtypes_Match
8301 (Find_Actual_Type
(Etype
(I1
), Scope
(A_Gen_T
)), T2
)
8304 ("index types of actual do not match those of formal &",
8306 Abandon_Instantiation
(Actual
);
8313 if not Subtypes_Match
(
8314 Find_Actual_Type
(Component_Type
(A_Gen_T
), Scope
(A_Gen_T
)),
8315 Component_Type
(Act_T
))
8318 ("component subtype of actual does not match that of formal &",
8320 Abandon_Instantiation
(Actual
);
8323 if Has_Aliased_Components
(A_Gen_T
)
8324 and then not Has_Aliased_Components
(Act_T
)
8327 ("actual must have aliased components to match formal type &",
8331 end Validate_Array_Type_Instance
;
8333 -----------------------------------------------
8334 -- Validate_Derived_Interface_Type_Instance --
8335 -----------------------------------------------
8337 procedure Validate_Derived_Interface_Type_Instance
is
8338 Par
: constant Entity_Id
:= Entity
(Subtype_Indication
(Def
));
8342 -- First apply interface instance checks
8344 Validate_Interface_Type_Instance
;
8346 -- Verify that immediate parent interface is an ancestor of
8350 and then not Interface_Present_In_Ancestor
(Act_T
, Par
)
8353 ("interface actual must include progenitor&", Actual
, Par
);
8356 -- Now verify that the actual includes all other ancestors of
8359 Elmt
:= First_Elmt
(Abstract_Interfaces
(A_Gen_T
));
8360 while Present
(Elmt
) loop
8361 if not Interface_Present_In_Ancestor
(Act_T
, Node
(Elmt
)) then
8363 ("interface actual must include progenitor&",
8364 Actual
, Node
(Elmt
));
8369 end Validate_Derived_Interface_Type_Instance
;
8371 ------------------------------------
8372 -- Validate_Derived_Type_Instance --
8373 ------------------------------------
8375 procedure Validate_Derived_Type_Instance
is
8376 Actual_Discr
: Entity_Id
;
8377 Ancestor_Discr
: Entity_Id
;
8380 -- If the parent type in the generic declaration is itself a previous
8381 -- formal type, then it is local to the generic and absent from the
8382 -- analyzed generic definition. In that case the ancestor is the
8383 -- instance of the formal (which must have been instantiated
8384 -- previously), unless the ancestor is itself a formal derived type.
8385 -- In this latter case (which is the subject of Corrigendum 8652/0038
8386 -- (AI-202) the ancestor of the formals is the ancestor of its
8387 -- parent. Otherwise, the analyzed generic carries the parent type.
8388 -- If the parent type is defined in a previous formal package, then
8389 -- the scope of that formal package is that of the generic type
8390 -- itself, and it has already been mapped into the corresponding type
8391 -- in the actual package.
8393 -- Common case: parent type defined outside of the generic
8395 if Is_Entity_Name
(Subtype_Mark
(Def
))
8396 and then Present
(Entity
(Subtype_Mark
(Def
)))
8398 Ancestor
:= Get_Instance_Of
(Entity
(Subtype_Mark
(Def
)));
8400 -- Check whether parent is defined in a previous formal package
8403 Scope
(Scope
(Base_Type
(Etype
(A_Gen_T
)))) = Scope
(A_Gen_T
)
8406 Get_Instance_Of
(Base_Type
(Etype
(A_Gen_T
)));
8408 -- The type may be a local derivation, or a type extension of
8409 -- a previous formal, or of a formal of a parent package.
8411 elsif Is_Derived_Type
(Get_Instance_Of
(A_Gen_T
))
8413 Ekind
(Get_Instance_Of
(A_Gen_T
)) = E_Record_Type_With_Private
8415 -- Check whether the parent is another derived formal type
8416 -- in the same generic unit.
8418 if Etype
(A_Gen_T
) /= A_Gen_T
8419 and then Is_Generic_Type
(Etype
(A_Gen_T
))
8420 and then Scope
(Etype
(A_Gen_T
)) = Scope
(A_Gen_T
)
8421 and then Etype
(Etype
(A_Gen_T
)) /= Etype
(A_Gen_T
)
8423 -- Locate ancestor of parent from the subtype declaration
8424 -- created for the actual.
8430 Decl
:= First
(Actual_Decls
);
8431 while Present
(Decl
) loop
8432 if Nkind
(Decl
) = N_Subtype_Declaration
8433 and then Chars
(Defining_Identifier
(Decl
)) =
8434 Chars
(Etype
(A_Gen_T
))
8436 Ancestor
:= Generic_Parent_Type
(Decl
);
8444 pragma Assert
(Present
(Ancestor
));
8448 Get_Instance_Of
(Base_Type
(Get_Instance_Of
(A_Gen_T
)));
8452 Ancestor
:= Get_Instance_Of
(Etype
(Base_Type
(A_Gen_T
)));
8455 -- Ada 2005 (AI-251)
8457 if Ada_Version
>= Ada_05
8458 and then Is_Interface
(Ancestor
)
8460 if not Interface_Present_In_Ancestor
(Act_T
, Ancestor
) then
8462 ("(Ada 2005) expected type implementing & in instantiation",
8466 elsif not Is_Ancestor
(Base_Type
(Ancestor
), Act_T
) then
8468 ("expect type derived from & in instantiation",
8469 Actual
, First_Subtype
(Ancestor
));
8470 Abandon_Instantiation
(Actual
);
8473 -- Perform atomic/volatile checks (RM C.6(12))
8475 if Is_Atomic
(Act_T
) and then not Is_Atomic
(Ancestor
) then
8477 ("cannot have atomic actual type for non-atomic formal type",
8480 elsif Is_Volatile
(Act_T
)
8481 and then not Is_Volatile
(Ancestor
)
8482 and then Is_By_Reference_Type
(Ancestor
)
8485 ("cannot have volatile actual type for non-volatile formal type",
8489 -- It should not be necessary to check for unknown discriminants
8490 -- on Formal, but for some reason Has_Unknown_Discriminants is
8491 -- false for A_Gen_T, so Is_Indefinite_Subtype incorrectly
8492 -- returns False. This needs fixing. ???
8494 if not Is_Indefinite_Subtype
(A_Gen_T
)
8495 and then not Unknown_Discriminants_Present
(Formal
)
8496 and then Is_Indefinite_Subtype
(Act_T
)
8499 ("actual subtype must be constrained", Actual
);
8500 Abandon_Instantiation
(Actual
);
8503 if not Unknown_Discriminants_Present
(Formal
) then
8504 if Is_Constrained
(Ancestor
) then
8505 if not Is_Constrained
(Act_T
) then
8507 ("actual subtype must be constrained", Actual
);
8508 Abandon_Instantiation
(Actual
);
8511 -- Ancestor is unconstrained
8513 elsif Is_Constrained
(Act_T
) then
8514 if Ekind
(Ancestor
) = E_Access_Type
8515 or else Is_Composite_Type
(Ancestor
)
8518 ("actual subtype must be unconstrained", Actual
);
8519 Abandon_Instantiation
(Actual
);
8522 -- A class-wide type is only allowed if the formal has
8523 -- unknown discriminants.
8525 elsif Is_Class_Wide_Type
(Act_T
)
8526 and then not Has_Unknown_Discriminants
(Ancestor
)
8529 ("actual for & cannot be a class-wide type", Actual
, Gen_T
);
8530 Abandon_Instantiation
(Actual
);
8532 -- Otherwise, the formal and actual shall have the same
8533 -- number of discriminants and each discriminant of the
8534 -- actual must correspond to a discriminant of the formal.
8536 elsif Has_Discriminants
(Act_T
)
8537 and then not Has_Unknown_Discriminants
(Act_T
)
8538 and then Has_Discriminants
(Ancestor
)
8540 Actual_Discr
:= First_Discriminant
(Act_T
);
8541 Ancestor_Discr
:= First_Discriminant
(Ancestor
);
8542 while Present
(Actual_Discr
)
8543 and then Present
(Ancestor_Discr
)
8545 if Base_Type
(Act_T
) /= Base_Type
(Ancestor
) and then
8546 No
(Corresponding_Discriminant
(Actual_Discr
))
8549 ("discriminant & does not correspond " &
8550 "to ancestor discriminant", Actual
, Actual_Discr
);
8551 Abandon_Instantiation
(Actual
);
8554 Next_Discriminant
(Actual_Discr
);
8555 Next_Discriminant
(Ancestor_Discr
);
8558 if Present
(Actual_Discr
) or else Present
(Ancestor_Discr
) then
8560 ("actual for & must have same number of discriminants",
8562 Abandon_Instantiation
(Actual
);
8565 -- This case should be caught by the earlier check for
8566 -- for constrainedness, but the check here is added for
8569 elsif Has_Discriminants
(Act_T
)
8570 and then not Has_Unknown_Discriminants
(Act_T
)
8573 ("actual for & must not have discriminants", Actual
, Gen_T
);
8574 Abandon_Instantiation
(Actual
);
8576 elsif Has_Discriminants
(Ancestor
) then
8578 ("actual for & must have known discriminants", Actual
, Gen_T
);
8579 Abandon_Instantiation
(Actual
);
8582 if not Subtypes_Statically_Compatible
(Act_T
, Ancestor
) then
8584 ("constraint on actual is incompatible with formal", Actual
);
8585 Abandon_Instantiation
(Actual
);
8588 end Validate_Derived_Type_Instance
;
8590 --------------------------------------
8591 -- Validate_Interface_Type_Instance --
8592 --------------------------------------
8594 procedure Validate_Interface_Type_Instance
is
8596 if not Is_Interface
(Act_T
) then
8598 ("actual for formal interface type must be an interface",
8601 elsif Is_Limited_Type
(Act_T
) /= Is_Limited_Type
(A_Gen_T
)
8603 Is_Task_Interface
(A_Gen_T
) /= Is_Task_Interface
(Act_T
)
8605 Is_Protected_Interface
(A_Gen_T
) /=
8606 Is_Protected_Interface
(Act_T
)
8608 Is_Synchronized_Interface
(A_Gen_T
) /=
8609 Is_Synchronized_Interface
(Act_T
)
8612 ("actual for interface& does not match ('R'M 12.5.5(5))",
8615 end Validate_Interface_Type_Instance
;
8617 ------------------------------------
8618 -- Validate_Private_Type_Instance --
8619 ------------------------------------
8621 procedure Validate_Private_Type_Instance
is
8622 Formal_Discr
: Entity_Id
;
8623 Actual_Discr
: Entity_Id
;
8624 Formal_Subt
: Entity_Id
;
8627 if Is_Limited_Type
(Act_T
)
8628 and then not Is_Limited_Type
(A_Gen_T
)
8631 ("actual for non-limited & cannot be a limited type", Actual
,
8633 Explain_Limited_Type
(Act_T
, Actual
);
8634 Abandon_Instantiation
(Actual
);
8636 elsif Is_Indefinite_Subtype
(Act_T
)
8637 and then not Is_Indefinite_Subtype
(A_Gen_T
)
8638 and then Ada_Version
>= Ada_95
8641 ("actual for & must be a definite subtype", Actual
, Gen_T
);
8643 elsif not Is_Tagged_Type
(Act_T
)
8644 and then Is_Tagged_Type
(A_Gen_T
)
8647 ("actual for & must be a tagged type", Actual
, Gen_T
);
8649 elsif Has_Discriminants
(A_Gen_T
) then
8650 if not Has_Discriminants
(Act_T
) then
8652 ("actual for & must have discriminants", Actual
, Gen_T
);
8653 Abandon_Instantiation
(Actual
);
8655 elsif Is_Constrained
(Act_T
) then
8657 ("actual for & must be unconstrained", Actual
, Gen_T
);
8658 Abandon_Instantiation
(Actual
);
8661 Formal_Discr
:= First_Discriminant
(A_Gen_T
);
8662 Actual_Discr
:= First_Discriminant
(Act_T
);
8663 while Formal_Discr
/= Empty
loop
8664 if Actual_Discr
= Empty
then
8666 ("discriminants on actual do not match formal",
8668 Abandon_Instantiation
(Actual
);
8671 Formal_Subt
:= Get_Instance_Of
(Etype
(Formal_Discr
));
8673 -- Access discriminants match if designated types do
8675 if Ekind
(Base_Type
(Formal_Subt
)) = E_Anonymous_Access_Type
8676 and then (Ekind
(Base_Type
(Etype
(Actual_Discr
)))) =
8677 E_Anonymous_Access_Type
8680 (Designated_Type
(Base_Type
(Formal_Subt
))) =
8681 Designated_Type
(Base_Type
(Etype
(Actual_Discr
)))
8685 elsif Base_Type
(Formal_Subt
) /=
8686 Base_Type
(Etype
(Actual_Discr
))
8689 ("types of actual discriminants must match formal",
8691 Abandon_Instantiation
(Actual
);
8693 elsif not Subtypes_Statically_Match
8694 (Formal_Subt
, Etype
(Actual_Discr
))
8695 and then Ada_Version
>= Ada_95
8698 ("subtypes of actual discriminants must match formal",
8700 Abandon_Instantiation
(Actual
);
8703 Next_Discriminant
(Formal_Discr
);
8704 Next_Discriminant
(Actual_Discr
);
8707 if Actual_Discr
/= Empty
then
8709 ("discriminants on actual do not match formal",
8711 Abandon_Instantiation
(Actual
);
8718 end Validate_Private_Type_Instance
;
8720 -- Start of processing for Instantiate_Type
8723 if Get_Instance_Of
(A_Gen_T
) /= A_Gen_T
then
8724 Error_Msg_N
("duplicate instantiation of generic type", Actual
);
8727 elsif not Is_Entity_Name
(Actual
)
8728 or else not Is_Type
(Entity
(Actual
))
8731 ("expect valid subtype mark to instantiate &", Actual
, Gen_T
);
8732 Abandon_Instantiation
(Actual
);
8735 Act_T
:= Entity
(Actual
);
8737 -- Ada 2005 (AI-216): An Unchecked_Union subtype shall only be passed
8738 -- as a generic actual parameter if the corresponding formal type
8739 -- does not have a known_discriminant_part, or is a formal derived
8740 -- type that is an Unchecked_Union type.
8742 if Is_Unchecked_Union
(Base_Type
(Act_T
)) then
8743 if not Has_Discriminants
(A_Gen_T
)
8745 (Is_Derived_Type
(A_Gen_T
)
8747 Is_Unchecked_Union
(A_Gen_T
))
8751 Error_Msg_N
("Unchecked_Union cannot be the actual for a" &
8752 " discriminated formal type", Act_T
);
8757 -- Deal with fixed/floating restrictions
8759 if Is_Floating_Point_Type
(Act_T
) then
8760 Check_Restriction
(No_Floating_Point
, Actual
);
8761 elsif Is_Fixed_Point_Type
(Act_T
) then
8762 Check_Restriction
(No_Fixed_Point
, Actual
);
8765 -- Deal with error of using incomplete type as generic actual
8767 if Ekind
(Act_T
) = E_Incomplete_Type
then
8768 if No
(Underlying_Type
(Act_T
)) then
8769 Error_Msg_N
("premature use of incomplete type", Actual
);
8770 Abandon_Instantiation
(Actual
);
8772 Act_T
:= Full_View
(Act_T
);
8773 Set_Entity
(Actual
, Act_T
);
8775 if Has_Private_Component
(Act_T
) then
8777 ("premature use of type with private component", Actual
);
8781 -- Deal with error of premature use of private type as generic actual
8783 elsif Is_Private_Type
(Act_T
)
8784 and then Is_Private_Type
(Base_Type
(Act_T
))
8785 and then not Is_Generic_Type
(Act_T
)
8786 and then not Is_Derived_Type
(Act_T
)
8787 and then No
(Full_View
(Root_Type
(Act_T
)))
8789 Error_Msg_N
("premature use of private type", Actual
);
8791 elsif Has_Private_Component
(Act_T
) then
8793 ("premature use of type with private component", Actual
);
8796 Set_Instance_Of
(A_Gen_T
, Act_T
);
8798 -- If the type is generic, the class-wide type may also be used
8800 if Is_Tagged_Type
(A_Gen_T
)
8801 and then Is_Tagged_Type
(Act_T
)
8802 and then not Is_Class_Wide_Type
(A_Gen_T
)
8804 Set_Instance_Of
(Class_Wide_Type
(A_Gen_T
),
8805 Class_Wide_Type
(Act_T
));
8808 if not Is_Abstract
(A_Gen_T
)
8809 and then Is_Abstract
(Act_T
)
8812 ("actual of non-abstract formal cannot be abstract", Actual
);
8815 if Is_Scalar_Type
(Gen_T
) then
8816 Set_Instance_Of
(Etype
(A_Gen_T
), Etype
(Act_T
));
8821 when N_Formal_Private_Type_Definition
=>
8822 Validate_Private_Type_Instance
;
8824 when N_Formal_Derived_Type_Definition
=>
8825 Validate_Derived_Type_Instance
;
8827 when N_Formal_Discrete_Type_Definition
=>
8828 if not Is_Discrete_Type
(Act_T
) then
8830 ("expect discrete type in instantiation of&", Actual
, Gen_T
);
8831 Abandon_Instantiation
(Actual
);
8834 when N_Formal_Signed_Integer_Type_Definition
=>
8835 if not Is_Signed_Integer_Type
(Act_T
) then
8837 ("expect signed integer type in instantiation of&",
8839 Abandon_Instantiation
(Actual
);
8842 when N_Formal_Modular_Type_Definition
=>
8843 if not Is_Modular_Integer_Type
(Act_T
) then
8845 ("expect modular type in instantiation of &", Actual
, Gen_T
);
8846 Abandon_Instantiation
(Actual
);
8849 when N_Formal_Floating_Point_Definition
=>
8850 if not Is_Floating_Point_Type
(Act_T
) then
8852 ("expect float type in instantiation of &", Actual
, Gen_T
);
8853 Abandon_Instantiation
(Actual
);
8856 when N_Formal_Ordinary_Fixed_Point_Definition
=>
8857 if not Is_Ordinary_Fixed_Point_Type
(Act_T
) then
8859 ("expect ordinary fixed point type in instantiation of &",
8861 Abandon_Instantiation
(Actual
);
8864 when N_Formal_Decimal_Fixed_Point_Definition
=>
8865 if not Is_Decimal_Fixed_Point_Type
(Act_T
) then
8867 ("expect decimal type in instantiation of &",
8869 Abandon_Instantiation
(Actual
);
8872 when N_Array_Type_Definition
=>
8873 Validate_Array_Type_Instance
;
8875 when N_Access_To_Object_Definition
=>
8876 Validate_Access_Type_Instance
;
8878 when N_Access_Function_Definition |
8879 N_Access_Procedure_Definition
=>
8880 Validate_Access_Subprogram_Instance
;
8882 when N_Record_Definition
=>
8883 Validate_Interface_Type_Instance
;
8885 when N_Derived_Type_Definition
=>
8886 Validate_Derived_Interface_Type_Instance
;
8889 raise Program_Error
;
8894 Make_Subtype_Declaration
(Loc
,
8895 Defining_Identifier
=> New_Copy
(Gen_T
),
8896 Subtype_Indication
=> New_Reference_To
(Act_T
, Loc
));
8898 if Is_Private_Type
(Act_T
) then
8899 Set_Has_Private_View
(Subtype_Indication
(Decl_Node
));
8901 elsif Is_Access_Type
(Act_T
)
8902 and then Is_Private_Type
(Designated_Type
(Act_T
))
8904 Set_Has_Private_View
(Subtype_Indication
(Decl_Node
));
8907 -- Flag actual derived types so their elaboration produces the
8908 -- appropriate renamings for the primitive operations of the ancestor.
8909 -- Flag actual for formal private types as well, to determine whether
8910 -- operations in the private part may override inherited operations.
8912 if Nkind
(Def
) = N_Formal_Derived_Type_Definition
8913 or else Nkind
(Def
) = N_Formal_Private_Type_Definition
8915 Set_Generic_Parent_Type
(Decl_Node
, Ancestor
);
8919 end Instantiate_Type
;
8921 ---------------------
8922 -- Is_In_Main_Unit --
8923 ---------------------
8925 function Is_In_Main_Unit
(N
: Node_Id
) return Boolean is
8926 Unum
: constant Unit_Number_Type
:= Get_Source_Unit
(N
);
8927 Current_Unit
: Node_Id
;
8930 if Unum
= Main_Unit
then
8933 -- If the current unit is a subunit then it is either the main unit
8934 -- or is being compiled as part of the main unit.
8936 elsif Nkind
(N
) = N_Compilation_Unit
then
8937 return Nkind
(Unit
(N
)) = N_Subunit
;
8940 Current_Unit
:= Parent
(N
);
8941 while Present
(Current_Unit
)
8942 and then Nkind
(Current_Unit
) /= N_Compilation_Unit
8944 Current_Unit
:= Parent
(Current_Unit
);
8947 -- The instantiation node is in the main unit, or else the current
8948 -- node (perhaps as the result of nested instantiations) is in the
8949 -- main unit, or in the declaration of the main unit, which in this
8950 -- last case must be a body.
8952 return Unum
= Main_Unit
8953 or else Current_Unit
= Cunit
(Main_Unit
)
8954 or else Current_Unit
= Library_Unit
(Cunit
(Main_Unit
))
8955 or else (Present
(Library_Unit
(Current_Unit
))
8956 and then Is_In_Main_Unit
(Library_Unit
(Current_Unit
)));
8957 end Is_In_Main_Unit
;
8959 ----------------------------
8960 -- Load_Parent_Of_Generic --
8961 ----------------------------
8963 procedure Load_Parent_Of_Generic
(N
: Node_Id
; Spec
: Node_Id
) is
8964 Comp_Unit
: constant Node_Id
:= Cunit
(Get_Source_Unit
(Spec
));
8965 Save_Style_Check
: constant Boolean := Style_Check
;
8966 True_Parent
: Node_Id
;
8967 Inst_Node
: Node_Id
;
8971 if not In_Same_Source_Unit
(N
, Spec
)
8972 or else Nkind
(Unit
(Comp_Unit
)) = N_Package_Declaration
8973 or else (Nkind
(Unit
(Comp_Unit
)) = N_Package_Body
8974 and then not Is_In_Main_Unit
(Spec
))
8976 -- Find body of parent of spec, and analyze it. A special case
8977 -- arises when the parent is an instantiation, that is to say when
8978 -- we are currently instantiating a nested generic. In that case,
8979 -- there is no separate file for the body of the enclosing instance.
8980 -- Instead, the enclosing body must be instantiated as if it were
8981 -- a pending instantiation, in order to produce the body for the
8982 -- nested generic we require now. Note that in that case the
8983 -- generic may be defined in a package body, the instance defined
8984 -- in the same package body, and the original enclosing body may not
8985 -- be in the main unit.
8987 True_Parent
:= Parent
(Spec
);
8990 while Present
(True_Parent
)
8991 and then Nkind
(True_Parent
) /= N_Compilation_Unit
8993 if Nkind
(True_Parent
) = N_Package_Declaration
8995 Nkind
(Original_Node
(True_Parent
)) = N_Package_Instantiation
8997 -- Parent is a compilation unit that is an instantiation.
8998 -- Instantiation node has been replaced with package decl.
9000 Inst_Node
:= Original_Node
(True_Parent
);
9003 elsif Nkind
(True_Parent
) = N_Package_Declaration
9004 and then Present
(Generic_Parent
(Specification
(True_Parent
)))
9005 and then Nkind
(Parent
(True_Parent
)) /= N_Compilation_Unit
9007 -- Parent is an instantiation within another specification.
9008 -- Declaration for instance has been inserted before original
9009 -- instantiation node. A direct link would be preferable?
9011 Inst_Node
:= Next
(True_Parent
);
9013 while Present
(Inst_Node
)
9014 and then Nkind
(Inst_Node
) /= N_Package_Instantiation
9019 -- If the instance appears within a generic, and the generic
9020 -- unit is defined within a formal package of the enclosing
9021 -- generic, there is no generic body available, and none
9022 -- needed. A more precise test should be used ???
9024 if No
(Inst_Node
) then
9030 True_Parent
:= Parent
(True_Parent
);
9034 -- Case where we are currently instantiating a nested generic
9036 if Present
(Inst_Node
) then
9037 if Nkind
(Parent
(True_Parent
)) = N_Compilation_Unit
then
9039 -- Instantiation node and declaration of instantiated package
9040 -- were exchanged when only the declaration was needed.
9041 -- Restore instantiation node before proceeding with body.
9043 Set_Unit
(Parent
(True_Parent
), Inst_Node
);
9046 -- Now complete instantiation of enclosing body, if it appears
9047 -- in some other unit. If it appears in the current unit, the
9048 -- body will have been instantiated already.
9050 if No
(Corresponding_Body
(Instance_Spec
(Inst_Node
))) then
9052 -- We need to determine the expander mode to instantiate
9053 -- the enclosing body. Because the generic body we need
9054 -- may use global entities declared in the enclosing package
9055 -- (including aggregates) it is in general necessary to
9056 -- compile this body with expansion enabled. The exception
9057 -- is if we are within a generic package, in which case
9058 -- the usual generic rule applies.
9061 Exp_Status
: Boolean := True;
9065 -- Loop through scopes looking for generic package
9067 Scop
:= Scope
(Defining_Entity
(Instance_Spec
(Inst_Node
)));
9068 while Present
(Scop
)
9069 and then Scop
/= Standard_Standard
9071 if Ekind
(Scop
) = E_Generic_Package
then
9072 Exp_Status
:= False;
9076 Scop
:= Scope
(Scop
);
9079 Instantiate_Package_Body
9080 (Pending_Body_Info
'(
9081 Inst_Node, True_Parent, Exp_Status,
9082 Get_Code_Unit (Sloc (Inst_Node))));
9086 -- Case where we are not instantiating a nested generic
9089 Opt.Style_Check := False;
9090 Expander_Mode_Save_And_Set (True);
9091 Load_Needed_Body (Comp_Unit, OK);
9092 Opt.Style_Check := Save_Style_Check;
9093 Expander_Mode_Restore;
9096 and then Unit_Requires_Body (Defining_Entity (Spec))
9099 Bname : constant Unit_Name_Type :=
9100 Get_Body_Name (Get_Unit_Name (Unit (Comp_Unit)));
9103 Error_Msg_Unit_1 := Bname;
9104 Error_Msg_N ("this instantiation requires$!", N);
9106 Get_File_Name (Bname, Subunit => False);
9107 Error_Msg_N ("\but file{ was not found!", N);
9108 raise Unrecoverable_Error;
9114 -- If loading the parent of the generic caused an instantiation
9115 -- circularity, we abandon compilation at this point, because
9116 -- otherwise in some cases we get into trouble with infinite
9117 -- recursions after this point.
9119 if Circularity_Detected then
9120 raise Unrecoverable_Error;
9122 end Load_Parent_Of_Generic;
9124 -----------------------
9125 -- Move_Freeze_Nodes --
9126 -----------------------
9128 procedure Move_Freeze_Nodes
9129 (Out_Of : Entity_Id;
9134 Next_Decl : Node_Id;
9135 Next_Node : Node_Id := After;
9138 function Is_Outer_Type (T : Entity_Id) return Boolean;
9139 -- Check whether entity is declared in a scope external to that
9140 -- of the generic unit.
9146 function Is_Outer_Type (T : Entity_Id) return Boolean is
9147 Scop : Entity_Id := Scope (T);
9150 if Scope_Depth (Scop) < Scope_Depth (Out_Of) then
9154 while Scop /= Standard_Standard loop
9156 if Scop = Out_Of then
9159 Scop := Scope (Scop);
9167 -- Start of processing for Move_Freeze_Nodes
9174 -- First remove the freeze nodes that may appear before all other
9178 while Present (Decl)
9179 and then Nkind (Decl) = N_Freeze_Entity
9180 and then Is_Outer_Type (Entity (Decl))
9182 Decl := Remove_Head (L);
9183 Insert_After (Next_Node, Decl);
9184 Set_Analyzed (Decl, False);
9189 -- Next scan the list of declarations and remove each freeze node that
9190 -- appears ahead of the current node.
9192 while Present (Decl) loop
9193 while Present (Next (Decl))
9194 and then Nkind (Next (Decl)) = N_Freeze_Entity
9195 and then Is_Outer_Type (Entity (Next (Decl)))
9197 Next_Decl := Remove_Next (Decl);
9198 Insert_After (Next_Node, Next_Decl);
9199 Set_Analyzed (Next_Decl, False);
9200 Next_Node := Next_Decl;
9203 -- If the declaration is a nested package or concurrent type, then
9204 -- recurse. Nested generic packages will have been processed from the
9207 if Nkind (Decl) = N_Package_Declaration then
9208 Spec := Specification (Decl);
9210 elsif Nkind (Decl) = N_Task_Type_Declaration then
9211 Spec := Task_Definition (Decl);
9213 elsif Nkind (Decl) = N_Protected_Type_Declaration then
9214 Spec := Protected_Definition (Decl);
9220 if Present (Spec) then
9221 Move_Freeze_Nodes (Out_Of, Next_Node,
9222 Visible_Declarations (Spec));
9223 Move_Freeze_Nodes (Out_Of, Next_Node,
9224 Private_Declarations (Spec));
9229 end Move_Freeze_Nodes;
9235 function Next_Assoc (E : Assoc_Ptr) return Assoc_Ptr is
9237 return Generic_Renamings.Table (E).Next_In_HTable;
9240 ------------------------
9241 -- Preanalyze_Actuals --
9242 ------------------------
9244 procedure Pre_Analyze_Actuals (N : Node_Id) is
9247 Errs : constant Int := Serious_Errors_Detected;
9250 Assoc := First (Generic_Associations (N));
9252 while Present (Assoc) loop
9253 Act := Explicit_Generic_Actual_Parameter (Assoc);
9255 -- Within a nested instantiation, a defaulted actual is an
9256 -- empty association, so nothing to analyze. If the actual for
9257 -- a subprogram is an attribute, analyze prefix only, because
9258 -- actual is not a complete attribute reference.
9260 -- If actual is an allocator, analyze expression only. The full
9261 -- analysis can generate code, and if the instance is a compilation
9262 -- unit we have to wait until the package instance is installed to
9263 -- have a proper place to insert this code.
9265 -- String literals may be operators, but at this point we do not
9266 -- know whether the actual is a formal subprogram or a string.
9271 elsif Nkind (Act) = N_Attribute_Reference then
9272 Analyze (Prefix (Act));
9274 elsif Nkind (Act) = N_Explicit_Dereference then
9275 Analyze (Prefix (Act));
9277 elsif Nkind (Act) = N_Allocator then
9279 Expr : constant Node_Id := Expression (Act);
9282 if Nkind (Expr) = N_Subtype_Indication then
9283 Analyze (Subtype_Mark (Expr));
9284 Analyze_List (Constraints (Constraint (Expr)));
9290 elsif Nkind (Act) /= N_Operator_Symbol then
9294 if Errs /= Serious_Errors_Detected then
9295 Abandon_Instantiation (Act);
9300 end Pre_Analyze_Actuals;
9306 procedure Remove_Parent (In_Body : Boolean := False) is
9307 S : Entity_Id := Current_Scope;
9313 -- After child instantiation is complete, remove from scope stack
9314 -- the extra copy of the current scope, and then remove parent
9320 while Current_Scope /= S loop
9322 End_Package_Scope (Current_Scope);
9324 if In_Open_Scopes (P) then
9325 E := First_Entity (P);
9327 while Present (E) loop
9328 Set_Is_Immediately_Visible (E, True);
9332 if Is_Generic_Instance (Current_Scope)
9333 and then P /= Current_Scope
9335 -- We are within an instance of some sibling. Retain
9336 -- visibility of parent, for proper subsequent cleanup,
9337 -- and reinstall private declarations as well.
9339 Set_In_Private_Part (P);
9340 Install_Private_Declarations (P);
9343 -- If the ultimate parent is a top-level unit recorded in
9344 -- Instance_Parent_Unit, then reset its visibility to what
9345 -- it was before instantiation. (It's not clear what the
9346 -- purpose is of testing whether Scope (P) is In_Open_Scopes,
9347 -- but that test was present before the ultimate parent test
9350 elsif not In_Open_Scopes (Scope (P))
9351 or else (P = Instance_Parent_Unit
9352 and then not Parent_Unit_Visible)
9354 Set_Is_Immediately_Visible (P, False);
9358 -- Reset visibility of entities in the enclosing scope
9360 Set_Is_Hidden_Open_Scope (Current_Scope, False);
9361 Hidden := First_Elmt (Hidden_Entities);
9363 while Present (Hidden) loop
9364 Set_Is_Immediately_Visible (Node (Hidden), True);
9369 -- Each body is analyzed separately, and there is no context
9370 -- that needs preserving from one body instance to the next,
9371 -- so remove all parent scopes that have been installed.
9373 while Present (S) loop
9374 End_Package_Scope (S);
9375 Set_Is_Immediately_Visible (S, False);
9377 exit when S = Standard_Standard;
9387 procedure Restore_Env is
9388 Saved : Instance_Env renames Instance_Envs.Table (Instance_Envs.Last);
9391 Ada_Version := Saved.Ada_Version;
9392 Ada_Version_Explicit := Saved.Ada_Version_Explicit;
9394 if No (Current_Instantiated_Parent.Act_Id) then
9396 -- Restore environment after subprogram inlining
9398 Restore_Private_Views (Empty);
9401 Current_Instantiated_Parent := Saved.Instantiated_Parent;
9402 Exchanged_Views := Saved.Exchanged_Views;
9403 Hidden_Entities := Saved.Hidden_Entities;
9404 Current_Sem_Unit := Saved.Current_Sem_Unit;
9405 Parent_Unit_Visible := Saved.Parent_Unit_Visible;
9406 Instance_Parent_Unit := Saved.Instance_Parent_Unit;
9408 Instance_Envs.Decrement_Last;
9411 ---------------------------
9412 -- Restore_Private_Views --
9413 ---------------------------
9415 procedure Restore_Private_Views
9416 (Pack_Id : Entity_Id;
9417 Is_Package : Boolean := True)
9425 procedure Restore_Nested_Formal (Formal : Entity_Id);
9426 -- Hide the generic formals of formal packages declared with box
9427 -- which were reachable in the current instantiation.
9429 procedure Restore_Nested_Formal (Formal : Entity_Id) is
9432 if Present (Renamed_Object (Formal))
9433 and then Denotes_Formal_Package (Renamed_Object (Formal), True)
9437 elsif Present (Associated_Formal_Package (Formal))
9438 and then Box_Present (Parent (Associated_Formal_Package (Formal)))
9440 Ent := First_Entity (Formal);
9442 while Present (Ent) loop
9443 exit when Ekind (Ent) = E_Package
9444 and then Renamed_Entity (Ent) = Renamed_Entity (Formal);
9446 Set_Is_Hidden (Ent);
9447 Set_Is_Potentially_Use_Visible (Ent, False);
9449 -- If package, then recurse
9451 if Ekind (Ent) = E_Package then
9452 Restore_Nested_Formal (Ent);
9458 end Restore_Nested_Formal;
9461 M := First_Elmt (Exchanged_Views);
9462 while Present (M) loop
9465 -- Subtypes of types whose views have been exchanged, and that
9466 -- are defined within the instance, were not on the list of
9467 -- Private_Dependents on entry to the instance, so they have to
9468 -- be exchanged explicitly now, in order to remain consistent with
9469 -- the view of the parent type.
9471 if Ekind (Typ) = E_Private_Type
9472 or else Ekind (Typ) = E_Limited_Private_Type
9473 or else Ekind (Typ) = E_Record_Type_With_Private
9475 Dep_Elmt := First_Elmt (Private_Dependents (Typ));
9477 while Present (Dep_Elmt) loop
9478 Dep_Typ := Node (Dep_Elmt);
9480 if Scope (Dep_Typ) = Pack_Id
9481 and then Present (Full_View (Dep_Typ))
9483 Replace_Elmt (Dep_Elmt, Full_View (Dep_Typ));
9484 Exchange_Declarations (Dep_Typ);
9487 Next_Elmt (Dep_Elmt);
9491 Exchange_Declarations (Node (M));
9495 if No (Pack_Id) then
9499 -- Make the generic formal parameters private, and make the formal
9500 -- types into subtypes of the actuals again.
9502 E := First_Entity (Pack_Id);
9504 while Present (E) loop
9505 Set_Is_Hidden (E, True);
9508 and then Nkind (Parent (E)) = N_Subtype_Declaration
9510 Set_Is_Generic_Actual_Type (E, False);
9512 -- An unusual case of aliasing: the actual may also be directly
9513 -- visible in the generic, and be private there, while it is
9514 -- fully visible in the context of the instance. The internal
9515 -- subtype is private in the instance, but has full visibility
9516 -- like its parent in the enclosing scope. This enforces the
9517 -- invariant that the privacy status of all private dependents of
9518 -- a type coincide with that of the parent type. This can only
9519 -- happen when a generic child unit is instantiated within a
9522 if Is_Private_Type (E)
9523 and then not Is_Private_Type (Etype (E))
9525 Exchange_Declarations (E);
9528 elsif Ekind (E) = E_Package then
9530 -- The end of the renaming list is the renaming of the generic
9531 -- package itself. If the instance is a subprogram, all entities
9532 -- in the corresponding package are renamings. If this entity is
9533 -- a formal package, make its own formals private as well. The
9534 -- actual in this case is itself the renaming of an instantation.
9535 -- If the entity is not a package renaming, it is the entity
9536 -- created to validate formal package actuals: ignore.
9538 -- If the actual is itself a formal package for the enclosing
9539 -- generic, or the actual for such a formal package, it remains
9540 -- visible on exit from the instance, and therefore nothing
9541 -- needs to be done either, except to keep it accessible.
9544 and then Renamed_Object (E) = Pack_Id
9548 elsif Nkind (Parent (E)) /= N_Package_Renaming_Declaration then
9551 elsif Denotes_Formal_Package (Renamed_Object (E), True) then
9552 Set_Is_Hidden (E, False);
9556 Act_P : constant Entity_Id := Renamed_Object (E);
9560 Id := First_Entity (Act_P);
9562 and then Id /= First_Private_Entity (Act_P)
9564 exit when Ekind (Id) = E_Package
9565 and then Renamed_Object (Id) = Act_P;
9567 Set_Is_Hidden (Id, True);
9568 Set_Is_Potentially_Use_Visible (Id, In_Use (Act_P));
9570 if Ekind (Id) = E_Package then
9571 Restore_Nested_Formal (Id);
9582 end Restore_Private_Views;
9589 (Gen_Unit : Entity_Id;
9590 Act_Unit : Entity_Id)
9594 Set_Instance_Env (Gen_Unit, Act_Unit);
9597 ----------------------------
9598 -- Save_Global_References --
9599 ----------------------------
9601 procedure Save_Global_References (N : Node_Id) is
9602 Gen_Scope : Entity_Id;
9606 function Is_Global (E : Entity_Id) return Boolean;
9607 -- Check whether entity is defined outside of generic unit.
9608 -- Examine the scope of an entity, and the scope of the scope,
9609 -- etc, until we find either Standard, in which case the entity
9610 -- is global, or the generic unit itself, which indicates that
9611 -- the entity is local. If the entity is the generic unit itself,
9612 -- as in the case of a recursive call, or the enclosing generic unit,
9613 -- if different from the current scope, then it is local as well,
9614 -- because it will be replaced at the point of instantiation. On
9615 -- the other hand, if it is a reference to a child unit of a common
9616 -- ancestor, which appears in an instantiation, it is global because
9617 -- it is used to denote a specific compilation unit at the time the
9618 -- instantiations will be analyzed.
9620 procedure Reset_Entity (N : Node_Id);
9621 -- Save semantic information on global entity, so that it is not
9622 -- resolved again at instantiation time.
9624 procedure Save_Entity_Descendants (N : Node_Id);
9625 -- Apply Save_Global_References to the two syntactic descendants of
9626 -- non-terminal nodes that carry an Associated_Node and are processed
9627 -- through Reset_Entity. Once the global entity (if any) has been
9628 -- captured together with its type, only two syntactic descendants
9629 -- need to be traversed to complete the processing of the tree rooted
9630 -- at N. This applies to Selected_Components, Expanded_Names, and to
9631 -- Operator nodes. N can also be a character literal, identifier, or
9632 -- operator symbol node, but the call has no effect in these cases.
9634 procedure Save_Global_Defaults (N1, N2 : Node_Id);
9635 -- Default actuals in nested instances must be handled specially
9636 -- because there is no link to them from the original tree. When an
9637 -- actual subprogram is given by a default, we add an explicit generic
9638 -- association for it in the instantiation node. When we save the
9639 -- global references on the name of the instance, we recover the list
9640 -- of generic associations, and add an explicit one to the original
9641 -- generic tree, through which a global actual can be preserved.
9642 -- Similarly, if a child unit is instantiated within a sibling, in the
9643 -- context of the parent, we must preserve the identifier of the parent
9644 -- so that it can be properly resolved in a subsequent instantiation.
9646 procedure Save_Global_Descendant (D : Union_Id);
9647 -- Apply Save_Global_References recursively to the descendents of
9650 procedure Save_References (N : Node_Id);
9651 -- This is the recursive procedure that does the work, once the
9652 -- enclosing generic scope has been established.
9658 function Is_Global (E : Entity_Id) return Boolean is
9659 Se : Entity_Id := Scope (E);
9661 function Is_Instance_Node (Decl : Node_Id) return Boolean;
9662 -- Determine whether the parent node of a reference to a child unit
9663 -- denotes an instantiation or a formal package, in which case the
9664 -- reference to the child unit is global, even if it appears within
9665 -- the current scope (e.g. when the instance appears within the body
9668 ----------------------
9669 -- Is_Instance_Node --
9670 ----------------------
9672 function Is_Instance_Node (Decl : Node_Id) return Boolean is
9674 return (Nkind (Decl) in N_Generic_Instantiation
9676 Nkind (Original_Node (Decl)) = N_Formal_Package_Declaration);
9677 end Is_Instance_Node;
9679 -- Start of processing for Is_Global
9682 if E = Gen_Scope then
9685 elsif E = Standard_Standard then
9688 elsif Is_Child_Unit (E)
9689 and then (Is_Instance_Node (Parent (N2))
9690 or else (Nkind (Parent (N2)) = N_Expanded_Name
9691 and then N2 = Selector_Name (Parent (N2))
9692 and then Is_Instance_Node (Parent (Parent (N2)))))
9697 while Se /= Gen_Scope loop
9698 if Se = Standard_Standard then
9713 procedure Reset_Entity (N : Node_Id) is
9715 procedure Set_Global_Type (N : Node_Id; N2 : Node_Id);
9716 -- The type of N2 is global to the generic unit. Save the
9717 -- type in the generic node.
9719 function Top_Ancestor (E : Entity_Id) return Entity_Id;
9720 -- Find the ultimate ancestor of the current unit. If it is
9721 -- not a generic unit, then the name of the current unit
9722 -- in the prefix of an expanded name must be replaced with
9723 -- its generic homonym to ensure that it will be properly
9724 -- resolved in an instance.
9726 ---------------------
9727 -- Set_Global_Type --
9728 ---------------------
9730 procedure Set_Global_Type (N : Node_Id; N2 : Node_Id) is
9731 Typ : constant Entity_Id := Etype (N2);
9737 and then Has_Private_View (Entity (N))
9739 -- If the entity of N is not the associated node, this is
9740 -- a nested generic and it has an associated node as well,
9741 -- whose type is already the full view (see below). Indicate
9742 -- that the original node has a private view.
9744 Set_Has_Private_View (N);
9747 -- If not a private type, nothing else to do
9749 if not Is_Private_Type (Typ) then
9750 if Is_Array_Type (Typ)
9751 and then Is_Private_Type (Component_Type (Typ))
9753 Set_Has_Private_View (N);
9756 -- If it is a derivation of a private type in a context where
9757 -- no full view is needed, nothing to do either.
9759 elsif No (Full_View (Typ)) and then Typ /= Etype (Typ) then
9762 -- Otherwise mark the type for flipping and use the full_view
9766 Set_Has_Private_View (N);
9768 if Present (Full_View (Typ)) then
9769 Set_Etype (N2, Full_View (Typ));
9772 end Set_Global_Type;
9778 function Top_Ancestor (E : Entity_Id) return Entity_Id is
9779 Par : Entity_Id := E;
9782 while Is_Child_Unit (Par) loop
9789 -- Start of processing for Reset_Entity
9792 N2 := Get_Associated_Node (N);
9796 if Is_Global (E) then
9797 Set_Global_Type (N, N2);
9799 elsif Nkind (N) = N_Op_Concat
9800 and then Is_Generic_Type (Etype (N2))
9802 (Base_Type (Etype (Right_Opnd (N2))) = Etype (N2)
9803 or else Base_Type (Etype (Left_Opnd (N2))) = Etype (N2))
9804 and then Is_Intrinsic_Subprogram (E)
9809 -- Entity is local. Mark generic node as unresolved.
9810 -- Note that now it does not have an entity.
9812 Set_Associated_Node (N, Empty);
9813 Set_Etype (N, Empty);
9816 if Nkind (Parent (N)) in N_Generic_Instantiation
9817 and then N = Name (Parent (N))
9819 Save_Global_Defaults (Parent (N), Parent (N2));
9822 elsif Nkind (Parent (N)) = N_Selected_Component
9823 and then Nkind (Parent (N2)) = N_Expanded_Name
9825 if Is_Global (Entity (Parent (N2))) then
9826 Change_Selected_Component_To_Expanded_Name (Parent (N));
9827 Set_Associated_Node (Parent (N), Parent (N2));
9828 Set_Global_Type (Parent (N), Parent (N2));
9829 Save_Entity_Descendants (N);
9831 -- If this is a reference to the current generic entity,
9832 -- replace by the name of the generic homonym of the current
9833 -- package. This is because in an instantiation Par.P.Q will
9834 -- not resolve to the name of the instance, whose enclosing
9835 -- scope is not necessarily Par. We use the generic homonym
9836 -- rather that the name of the generic itself, because it may
9837 -- be hidden by a local declaration.
9839 elsif In_Open_Scopes (Entity (Parent (N2)))
9841 Is_Generic_Unit (Top_Ancestor (Entity (Prefix (Parent (N2)))))
9843 if Ekind (Entity (Parent (N2))) = E_Generic_Package then
9844 Rewrite (Parent (N),
9845 Make_Identifier (Sloc (N),
9847 Chars (Generic_Homonym (Entity (Parent (N2))))));
9849 Rewrite (Parent (N),
9850 Make_Identifier (Sloc (N),
9851 Chars => Chars (Selector_Name (Parent (N2)))));
9855 if Nkind (Parent (Parent (N))) in N_Generic_Instantiation
9856 and then Parent (N) = Name (Parent (Parent (N)))
9858 Save_Global_Defaults
9859 (Parent (Parent (N)), Parent (Parent ((N2))));
9862 -- A selected component may denote a static constant that has
9863 -- been folded. Make the same replacement in original tree.
9865 elsif Nkind (Parent (N)) = N_Selected_Component
9866 and then (Nkind (Parent (N2)) = N_Integer_Literal
9867 or else Nkind (Parent (N2)) = N_Real_Literal)
9869 Rewrite (Parent (N),
9870 New_Copy (Parent (N2)));
9871 Set_Analyzed (Parent (N), False);
9873 -- A selected component may be transformed into a parameterless
9874 -- function call. If the called entity is global, rewrite the
9875 -- node appropriately, i.e. as an extended name for the global
9878 elsif Nkind (Parent (N)) = N_Selected_Component
9879 and then Nkind (Parent (N2)) = N_Function_Call
9880 and then Is_Global (Entity (Name (Parent (N2))))
9882 Change_Selected_Component_To_Expanded_Name (Parent (N));
9883 Set_Associated_Node (Parent (N), Name (Parent (N2)));
9884 Set_Global_Type (Parent (N), Name (Parent (N2)));
9885 Save_Entity_Descendants (N);
9888 -- Entity is local. Reset in generic unit, so that node
9889 -- is resolved anew at the point of instantiation.
9891 Set_Associated_Node (N, Empty);
9892 Set_Etype (N, Empty);
9896 -----------------------------
9897 -- Save_Entity_Descendants --
9898 -----------------------------
9900 procedure Save_Entity_Descendants (N : Node_Id) is
9904 Save_Global_Descendant (Union_Id (Left_Opnd (N)));
9905 Save_Global_Descendant (Union_Id (Right_Opnd (N)));
9908 Save_Global_Descendant (Union_Id (Right_Opnd (N)));
9910 when N_Expanded_Name | N_Selected_Component =>
9911 Save_Global_Descendant (Union_Id (Prefix (N)));
9912 Save_Global_Descendant (Union_Id (Selector_Name (N)));
9914 when N_Identifier | N_Character_Literal | N_Operator_Symbol =>
9918 raise Program_Error;
9920 end Save_Entity_Descendants;
9922 --------------------------
9923 -- Save_Global_Defaults --
9924 --------------------------
9926 procedure Save_Global_Defaults (N1, N2 : Node_Id) is
9927 Loc : constant Source_Ptr := Sloc (N1);
9928 Assoc2 : constant List_Id := Generic_Associations (N2);
9929 Gen_Id : constant Entity_Id := Get_Generic_Entity (N2);
9939 Assoc1 := Generic_Associations (N1);
9941 if Present (Assoc1) then
9942 Act1 := First (Assoc1);
9945 Set_Generic_Associations (N1, New_List);
9946 Assoc1 := Generic_Associations (N1);
9949 if Present (Assoc2) then
9950 Act2 := First (Assoc2);
9955 while Present (Act1) and then Present (Act2) loop
9960 -- Find the associations added for default suprograms
9962 if Present (Act2) then
9963 while Nkind (Act2) /= N_Generic_Association
9964 or else No (Entity (Selector_Name (Act2)))
9965 or else not Is_Overloadable (Entity (Selector_Name (Act2)))
9970 -- Add a similar association if the default is global. The
9971 -- renaming declaration for the actual has been analyzed, and
9972 -- its alias is the program it renames. Link the actual in the
9973 -- original generic tree with the node in the analyzed tree.
9975 while Present (Act2) loop
9976 Subp := Entity (Selector_Name (Act2));
9977 Def := Explicit_Generic_Actual_Parameter (Act2);
9979 -- Following test is defence against rubbish errors
9981 if No (Alias (Subp)) then
9985 -- Retrieve the resolved actual from the renaming declaration
9986 -- created for the instantiated formal.
9988 Actual := Entity (Name (Parent (Parent (Subp))));
9989 Set_Entity (Def, Actual);
9990 Set_Etype (Def, Etype (Actual));
9992 if Is_Global (Actual) then
9994 Make_Generic_Association (Loc,
9995 Selector_Name => New_Occurrence_Of (Subp, Loc),
9996 Explicit_Generic_Actual_Parameter =>
9997 New_Occurrence_Of (Actual, Loc));
10000 (Explicit_Generic_Actual_Parameter (Ndec), Def);
10002 Append (Ndec, Assoc1);
10004 -- If there are other defaults, add a dummy association
10005 -- in case there are other defaulted formals with the same
10008 elsif Present (Next (Act2)) then
10010 Make_Generic_Association (Loc,
10011 Selector_Name => New_Occurrence_Of (Subp, Loc),
10012 Explicit_Generic_Actual_Parameter => Empty);
10014 Append (Ndec, Assoc1);
10021 if Nkind (Name (N1)) = N_Identifier
10022 and then Is_Child_Unit (Gen_Id)
10023 and then Is_Global (Gen_Id)
10024 and then Is_Generic_Unit (Scope (Gen_Id))
10025 and then In_Open_Scopes (Scope (Gen_Id))
10027 -- This is an instantiation of a child unit within a sibling,
10028 -- so that the generic parent is in scope. An eventual instance
10029 -- must occur within the scope of an instance of the parent.
10030 -- Make name in instance into an expanded name, to preserve the
10031 -- identifier of the parent, so it can be resolved subsequently.
10033 Rewrite (Name (N2),
10034 Make_Expanded_Name (Loc,
10035 Chars => Chars (Gen_Id),
10036 Prefix => New_Occurrence_Of (Scope (Gen_Id), Loc),
10037 Selector_Name => New_Occurrence_Of (Gen_Id, Loc)));
10038 Set_Entity (Name (N2), Gen_Id);
10040 Rewrite (Name (N1),
10041 Make_Expanded_Name (Loc,
10042 Chars => Chars (Gen_Id),
10043 Prefix => New_Occurrence_Of (Scope (Gen_Id), Loc),
10044 Selector_Name => New_Occurrence_Of (Gen_Id, Loc)));
10046 Set_Associated_Node (Name (N1), Name (N2));
10047 Set_Associated_Node (Prefix (Name (N1)), Empty);
10048 Set_Associated_Node
10049 (Selector_Name (Name (N1)), Selector_Name (Name (N2)));
10050 Set_Etype (Name (N1), Etype (Gen_Id));
10053 end Save_Global_Defaults;
10055 ----------------------------
10056 -- Save_Global_Descendant --
10057 ----------------------------
10059 procedure Save_Global_Descendant (D : Union_Id) is
10063 if D in Node_Range then
10064 if D = Union_Id (Empty) then
10067 elsif Nkind (Node_Id (D)) /= N_Compilation_Unit then
10068 Save_References (Node_Id (D));
10071 elsif D in List_Range then
10072 if D = Union_Id (No_List)
10073 or else Is_Empty_List (List_Id (D))
10078 N1 := First (List_Id (D));
10079 while Present (N1) loop
10080 Save_References (N1);
10085 -- Element list or other non-node field, nothing to do
10090 end Save_Global_Descendant;
10092 ---------------------
10093 -- Save_References --
10094 ---------------------
10096 -- This is the recursive procedure that does the work, once the
10097 -- enclosing generic scope has been established. We have to treat
10098 -- specially a number of node rewritings that are required by semantic
10099 -- processing and which change the kind of nodes in the generic copy:
10100 -- typically constant-folding, replacing an operator node by a string
10101 -- literal, or a selected component by an expanded name. In each of
10102 -- those cases, the transformation is propagated to the generic unit.
10104 procedure Save_References (N : Node_Id) is
10109 elsif Nkind (N) = N_Character_Literal
10110 or else Nkind (N) = N_Operator_Symbol
10112 if Nkind (N) = Nkind (Get_Associated_Node (N)) then
10115 elsif Nkind (N) = N_Operator_Symbol
10116 and then Nkind (Get_Associated_Node (N)) = N_String_Literal
10118 Change_Operator_Symbol_To_String_Literal (N);
10121 elsif Nkind (N) in N_Op then
10123 if Nkind (N) = Nkind (Get_Associated_Node (N)) then
10125 if Nkind (N) = N_Op_Concat then
10126 Set_Is_Component_Left_Opnd (N,
10127 Is_Component_Left_Opnd (Get_Associated_Node (N)));
10129 Set_Is_Component_Right_Opnd (N,
10130 Is_Component_Right_Opnd (Get_Associated_Node (N)));
10135 -- Node may be transformed into call to a user-defined operator
10137 N2 := Get_Associated_Node (N);
10139 if Nkind (N2) = N_Function_Call then
10140 E := Entity (Name (N2));
10143 and then Is_Global (E)
10145 Set_Etype (N, Etype (N2));
10147 Set_Associated_Node (N, Empty);
10148 Set_Etype (N, Empty);
10151 elsif Nkind (N2) = N_Integer_Literal
10152 or else Nkind (N2) = N_Real_Literal
10153 or else Nkind (N2) = N_String_Literal
10155 -- Operation was constant-folded, perform the same
10156 -- replacement in generic.
10158 Rewrite (N, New_Copy (N2));
10159 Set_Analyzed (N, False);
10161 elsif Nkind (N2) = N_Identifier
10162 and then Ekind (Entity (N2)) = E_Enumeration_Literal
10164 -- Same if call was folded into a literal, but in this
10165 -- case retain the entity to avoid spurious ambiguities
10166 -- if id is overloaded at the point of instantiation or
10169 Rewrite (N, New_Copy (N2));
10170 Set_Analyzed (N, False);
10174 -- Complete the check on operands, if node has not been
10175 -- constant-folded.
10177 if Nkind (N) in N_Op then
10178 Save_Entity_Descendants (N);
10181 elsif Nkind (N) = N_Identifier then
10182 if Nkind (N) = Nkind (Get_Associated_Node (N)) then
10184 -- If this is a discriminant reference, always save it.
10185 -- It is used in the instance to find the corresponding
10186 -- discriminant positionally rather than by name.
10188 Set_Original_Discriminant
10189 (N, Original_Discriminant (Get_Associated_Node (N)));
10193 N2 := Get_Associated_Node (N);
10195 if Nkind (N2) = N_Function_Call then
10196 E := Entity (Name (N2));
10198 -- Name resolves to a call to parameterless function.
10199 -- If original entity is global, mark node as resolved.
10202 and then Is_Global (E)
10204 Set_Etype (N, Etype (N2));
10206 Set_Associated_Node (N, Empty);
10207 Set_Etype (N, Empty);
10211 Nkind (N2) = N_Integer_Literal or else
10212 Nkind (N2) = N_Real_Literal or else
10213 Nkind (N2) = N_String_Literal
10215 -- Name resolves to named number that is constant-folded,
10216 -- or to string literal from concatenation.
10217 -- Perform the same replacement in generic.
10219 Rewrite (N, New_Copy (N2));
10220 Set_Analyzed (N, False);
10222 elsif Nkind (N2) = N_Explicit_Dereference then
10224 -- An identifier is rewritten as a dereference if it is
10225 -- the prefix in a selected component, and it denotes an
10226 -- access to a composite type, or a parameterless function
10227 -- call that returns an access type.
10229 -- Check whether corresponding entity in prefix is global
10231 if Is_Entity_Name (Prefix (N2))
10232 and then Present (Entity (Prefix (N2)))
10233 and then Is_Global (Entity (Prefix (N2)))
10236 Make_Explicit_Dereference (Sloc (N),
10237 Prefix => Make_Identifier (Sloc (N),
10238 Chars => Chars (N))));
10239 Set_Associated_Node (Prefix (N), Prefix (N2));
10241 elsif Nkind (Prefix (N2)) = N_Function_Call
10242 and then Is_Global (Entity (Name (Prefix (N2))))
10245 Make_Explicit_Dereference (Sloc (N),
10246 Prefix => Make_Function_Call (Sloc (N),
10248 Make_Identifier (Sloc (N),
10249 Chars => Chars (N)))));
10251 Set_Associated_Node
10252 (Name (Prefix (N)), Name (Prefix (N2)));
10255 Set_Associated_Node (N, Empty);
10256 Set_Etype (N, Empty);
10259 -- The subtype mark of a nominally unconstrained object
10260 -- is rewritten as a subtype indication using the bounds
10261 -- of the expression. Recover the original subtype mark.
10263 elsif Nkind (N2) = N_Subtype_Indication
10264 and then Is_Entity_Name (Original_Node (N2))
10266 Set_Associated_Node (N, Original_Node (N2));
10274 elsif Nkind (N) in N_Entity then
10279 Loc : constant Source_Ptr := Sloc (N);
10280 Qual : Node_Id := Empty;
10281 Typ : Entity_Id := Empty;
10284 use Atree.Unchecked_Access;
10285 -- This code section is part of implementing an untyped tree
10286 -- traversal, so it needs direct access to node fields.
10289 if Nkind (N) = N_Aggregate
10291 Nkind (N) = N_Extension_Aggregate
10293 N2 := Get_Associated_Node (N);
10300 -- In an instance within a generic, use the name of
10301 -- the actual and not the original generic parameter.
10302 -- If the actual is global in the current generic it
10303 -- must be preserved for its instantiation.
10305 if Nkind (Parent (Typ)) = N_Subtype_Declaration
10307 Present (Generic_Parent_Type (Parent (Typ)))
10309 Typ := Base_Type (Typ);
10310 Set_Etype (N2, Typ);
10316 or else not Is_Global (Typ)
10318 Set_Associated_Node (N, Empty);
10320 -- If the aggregate is an actual in a call, it has been
10321 -- resolved in the current context, to some local type.
10322 -- The enclosing call may have been disambiguated by
10323 -- the aggregate, and this disambiguation might fail at
10324 -- instantiation time because the type to which the
10325 -- aggregate did resolve is not preserved. In order to
10326 -- preserve some of this information, we wrap the
10327 -- aggregate in a qualified expression, using the id of
10328 -- its type. For further disambiguation we qualify the
10329 -- type name with its scope (if visible) because both
10330 -- id's will have corresponding entities in an instance.
10331 -- This resolves most of the problems with missing type
10332 -- information on aggregates in instances.
10334 if Nkind (N2) = Nkind (N)
10336 (Nkind (Parent (N2)) = N_Procedure_Call_Statement
10337 or else Nkind (Parent (N2)) = N_Function_Call)
10338 and then Comes_From_Source (Typ)
10340 if Is_Immediately_Visible (Scope (Typ)) then
10341 Nam := Make_Selected_Component (Loc,
10343 Make_Identifier (Loc, Chars (Scope (Typ))),
10345 Make_Identifier (Loc, Chars (Typ)));
10347 Nam := Make_Identifier (Loc, Chars (Typ));
10351 Make_Qualified_Expression (Loc,
10352 Subtype_Mark => Nam,
10353 Expression => Relocate_Node (N));
10357 Save_Global_Descendant (Field1 (N));
10358 Save_Global_Descendant (Field2 (N));
10359 Save_Global_Descendant (Field3 (N));
10360 Save_Global_Descendant (Field5 (N));
10362 if Present (Qual) then
10366 -- All other cases than aggregates
10369 Save_Global_Descendant (Field1 (N));
10370 Save_Global_Descendant (Field2 (N));
10371 Save_Global_Descendant (Field3 (N));
10372 Save_Global_Descendant (Field4 (N));
10373 Save_Global_Descendant (Field5 (N));
10377 end Save_References;
10379 -- Start of processing for Save_Global_References
10382 Gen_Scope := Current_Scope;
10384 -- If the generic unit is a child unit, references to entities in
10385 -- the parent are treated as local, because they will be resolved
10386 -- anew in the context of the instance of the parent.
10388 while Is_Child_Unit (Gen_Scope)
10389 and then Ekind (Scope (Gen_Scope)) = E_Generic_Package
10391 Gen_Scope := Scope (Gen_Scope);
10394 Save_References (N);
10395 end Save_Global_References;
10397 --------------------------------------
10398 -- Set_Copied_Sloc_For_Inlined_Body --
10399 --------------------------------------
10401 procedure Set_Copied_Sloc_For_Inlined_Body (N : Node_Id; E : Entity_Id) is
10403 Create_Instantiation_Source (N, E, True, S_Adjustment);
10404 end Set_Copied_Sloc_For_Inlined_Body;
10406 ---------------------
10407 -- Set_Instance_Of --
10408 ---------------------
10410 procedure Set_Instance_Of (A : Entity_Id; B : Entity_Id) is
10412 Generic_Renamings.Table (Generic_Renamings.Last) := (A, B, Assoc_Null);
10413 Generic_Renamings_HTable.Set (Generic_Renamings.Last);
10414 Generic_Renamings.Increment_Last;
10415 end Set_Instance_Of;
10417 --------------------
10418 -- Set_Next_Assoc --
10419 --------------------
10421 procedure Set_Next_Assoc (E : Assoc_Ptr; Next : Assoc_Ptr) is
10423 Generic_Renamings.Table (E).Next_In_HTable := Next;
10424 end Set_Next_Assoc;
10426 -------------------
10427 -- Start_Generic --
10428 -------------------
10430 procedure Start_Generic is
10432 -- ??? I am sure more things could be factored out in this
10433 -- routine. Should probably be done at a later stage.
10435 Generic_Flags.Increment_Last;
10436 Generic_Flags.Table (Generic_Flags.Last) := Inside_A_Generic;
10437 Inside_A_Generic := True;
10439 Expander_Mode_Save_And_Set (False);
10442 ----------------------
10443 -- Set_Instance_Env --
10444 ----------------------
10446 procedure Set_Instance_Env
10447 (Gen_Unit : Entity_Id;
10448 Act_Unit : Entity_Id)
10451 -- Regardless of the current mode, predefined units are analyzed in
10452 -- the most current Ada mode, and earlier version Ada checks do not
10453 -- apply to predefined units.
10455 -- Why is this not using the routine Opt.Set_Opt_Config_Switches ???
10457 if Is_Internal_File_Name
10458 (Fname => Unit_File_Name (Get_Source_Unit (Gen_Unit)),
10459 Renamings_Included => True) then
10460 Ada_Version := Ada_Version_Type'Last;
10463 Current_Instantiated_Parent := (Gen_Unit, Act_Unit, Assoc_Null);
10464 end Set_Instance_Env;
10470 procedure Switch_View (T : Entity_Id) is
10471 BT : constant Entity_Id := Base_Type (T);
10472 Priv_Elmt : Elmt_Id := No_Elmt;
10473 Priv_Sub : Entity_Id;
10476 -- T may be private but its base type may have been exchanged through
10477 -- some other occurrence, in which case there is nothing to switch.
10479 if not Is_Private_Type (BT) then
10483 Priv_Elmt := First_Elmt (Private_Dependents (BT));
10485 if Present (Full_View (BT)) then
10486 Prepend_Elmt (Full_View (BT), Exchanged_Views);
10487 Exchange_Declarations (BT);
10490 while Present (Priv_Elmt) loop
10491 Priv_Sub := (Node (Priv_Elmt));
10493 -- We avoid flipping the subtype if the Etype of its full
10494 -- view is private because this would result in a malformed
10495 -- subtype. This occurs when the Etype of the subtype full
10496 -- view is the full view of the base type (and since the
10497 -- base types were just switched, the subtype is pointing
10498 -- to the wrong view). This is currently the case for
10499 -- tagged record types, access types (maybe more?) and
10500 -- needs to be resolved. ???
10502 if Present (Full_View (Priv_Sub))
10503 and then not Is_Private_Type (Etype (Full_View (Priv_Sub)))
10505 Prepend_Elmt (Full_View (Priv_Sub), Exchanged_Views);
10506 Exchange_Declarations (Priv_Sub);
10509 Next_Elmt (Priv_Elmt);
10513 -----------------------------
10514 -- Valid_Default_Attribute --
10515 -----------------------------
10517 procedure Valid_Default_Attribute (Nam : Entity_Id; Def : Node_Id) is
10518 Attr_Id : constant Attribute_Id :=
10519 Get_Attribute_Id (Attribute_Name (Def));
10520 T : constant Entity_Id := Entity (Prefix (Def));
10521 Is_Fun : constant Boolean := (Ekind (Nam) = E_Function);
10534 F := First_Formal (Nam);
10535 while Present (F) loop
10536 Num_F := Num_F + 1;
10541 when Attribute_Adjacent | Attribute_Ceiling | Attribute_Copy_Sign |
10542 Attribute_Floor | Attribute_Fraction | Attribute_Machine |
10543 Attribute_Model | Attribute_Remainder | Attribute_Rounding |
10544 Attribute_Unbiased_Rounding =>
10547 and then Is_Floating_Point_Type (T);
10549 when Attribute_Image | Attribute_Pred | Attribute_Succ |
10550 Attribute_Value | Attribute_Wide_Image |
10551 Attribute_Wide_Value =>
10552 OK := (Is_Fun and then Num_F = 1 and then Is_Scalar_Type (T));
10554 when Attribute_Max | Attribute_Min =>
10555 OK := (Is_Fun and then Num_F = 2 and then Is_Scalar_Type (T));
10557 when Attribute_Input =>
10558 OK := (Is_Fun and then Num_F = 1);
10560 when Attribute_Output | Attribute_Read | Attribute_Write =>
10561 OK := (not Is_Fun and then Num_F = 2);
10568 Error_Msg_N ("attribute reference has wrong profile for subprogram",
10571 end Valid_Default_Attribute;