1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
9 -- Copyright (C) 1992-2023, Free Software Foundation, Inc. --
11 -- GNAT is free software; you can redistribute it and/or modify it under --
12 -- terms of the GNU General Public License as published by the Free Soft- --
13 -- ware Foundation; either version 3, or (at your option) any later ver- --
14 -- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
15 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
16 -- or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License --
17 -- for more details. You should have received a copy of the GNU General --
18 -- Public License distributed with GNAT; see file COPYING3. If not, go to --
19 -- http://www.gnu.org/licenses for a complete copy of the license. --
21 -- GNAT was originally developed by the GNAT team at New York University. --
22 -- Extensive contributions were provided by Ada Core Technologies Inc. --
24 ------------------------------------------------------------------------------
26 with Aspects
; use Aspects
;
27 with Atree
; use Atree
;
28 with Contracts
; use Contracts
;
29 with Einfo
; use Einfo
;
30 with Einfo
.Entities
; use Einfo
.Entities
;
31 with Einfo
.Utils
; use Einfo
.Utils
;
32 with Elists
; use Elists
;
33 with Errout
; use Errout
;
34 with Expander
; use Expander
;
35 with Fname
; use Fname
;
36 with Fname
.UF
; use Fname
.UF
;
37 with Freeze
; use Freeze
;
38 with Ghost
; use Ghost
;
39 with Itypes
; use Itypes
;
41 with Lib
.Load
; use Lib
.Load
;
42 with Lib
.Xref
; use Lib
.Xref
;
43 with Nlists
; use Nlists
;
44 with Namet
; use Namet
;
45 with Nmake
; use Nmake
;
47 with Rident
; use Rident
;
48 with Restrict
; use Restrict
;
49 with Rtsfind
; use Rtsfind
;
51 with Sem_Aux
; use Sem_Aux
;
52 with Sem_Cat
; use Sem_Cat
;
53 with Sem_Ch3
; use Sem_Ch3
;
54 with Sem_Ch6
; use Sem_Ch6
;
55 with Sem_Ch7
; use Sem_Ch7
;
56 with Sem_Ch8
; use Sem_Ch8
;
57 with Sem_Ch10
; use Sem_Ch10
;
58 with Sem_Ch13
; use Sem_Ch13
;
59 with Sem_Dim
; use Sem_Dim
;
60 with Sem_Disp
; use Sem_Disp
;
61 with Sem_Elab
; use Sem_Elab
;
62 with Sem_Elim
; use Sem_Elim
;
63 with Sem_Eval
; use Sem_Eval
;
64 with Sem_Prag
; use Sem_Prag
;
65 with Sem_Res
; use Sem_Res
;
66 with Sem_Type
; use Sem_Type
;
67 with Sem_Util
; use Sem_Util
;
68 with Sem_Warn
; use Sem_Warn
;
69 with Stand
; use Stand
;
70 with Sinfo
; use Sinfo
;
71 with Sinfo
.Nodes
; use Sinfo
.Nodes
;
72 with Sinfo
.Utils
; use Sinfo
.Utils
;
73 with Sinfo
.CN
; use Sinfo
.CN
;
74 with Sinput
; use Sinput
;
75 with Sinput
.L
; use Sinput
.L
;
76 with Snames
; use Snames
;
77 with Stringt
; use Stringt
;
78 with Uname
; use Uname
;
80 with Tbuild
; use Tbuild
;
81 with Uintp
; use Uintp
;
82 with Urealp
; use Urealp
;
83 with Warnsw
; use Warnsw
;
87 package body Sem_Ch12
is
89 ----------------------------------------------------------
90 -- Implementation of Generic Analysis and Instantiation --
91 ----------------------------------------------------------
93 -- GNAT implements generics by macro expansion. No attempt is made to share
94 -- generic instantiations (for now). Analysis of a generic definition does
95 -- not perform any expansion action, but the expander must be called on the
96 -- tree for each instantiation, because the expansion may of course depend
97 -- on the generic actuals. All of this is best achieved as follows:
99 -- a) Semantic analysis of a generic unit is performed on a copy of the
100 -- tree for the generic unit. All tree modifications that follow analysis
101 -- do not affect the original tree. Links are kept between the original
102 -- tree and the copy, in order to recognize non-local references within
103 -- the generic, and propagate them to each instance (recall that name
104 -- resolution is done on the generic declaration: generics are not really
105 -- macros). This is summarized in the following diagram:
107 -- .-----------. .----------.
108 -- | semantic |<--------------| generic |
110 -- | |==============>| |
111 -- |___________| global |__________|
122 -- b) Each instantiation copies the original tree, and inserts into it a
123 -- series of declarations that describe the mapping between generic formals
124 -- and actuals. For example, a generic In OUT parameter is an object
125 -- renaming of the corresponding actual, etc. Generic IN parameters are
126 -- constant declarations.
128 -- c) In order to give the right visibility for these renamings, we use
129 -- a different scheme for package and subprogram instantiations. For
130 -- packages, the list of renamings is inserted into the package
131 -- specification, before the visible declarations of the package. The
132 -- renamings are analyzed before any of the text of the instance, and are
133 -- thus visible at the right place. Furthermore, outside of the instance,
134 -- the generic parameters are visible and denote their corresponding
137 -- For subprograms, we create a container package to hold the renamings
138 -- and the subprogram instance itself. Analysis of the package makes the
139 -- renaming declarations visible to the subprogram. After analyzing the
140 -- package, the defining entity for the subprogram is touched-up so that
141 -- it appears declared in the current scope, and not inside the container
144 -- If the instantiation is a compilation unit, the container package is
145 -- given the same name as the subprogram instance. This ensures that
146 -- the elaboration procedure called by the binder, using the compilation
147 -- unit name, calls in fact the elaboration procedure for the package.
149 -- Not surprisingly, private types complicate this approach. By saving in
150 -- the original generic object the non-local references, we guarantee that
151 -- the proper entities are referenced at the point of instantiation.
152 -- However, for private types, this by itself does not insure that the
153 -- proper VIEW of the entity is used (the full type may be visible at the
154 -- point of generic definition, but not at instantiation, or vice-versa).
155 -- In order to reference the proper view, we special-case any reference
156 -- to private types in the generic object, by saving both views, one in
157 -- the generic and one in the semantic copy. At time of instantiation, we
158 -- check whether the two views are consistent, and exchange declarations if
159 -- necessary, in order to restore the correct visibility. Similarly, if
160 -- the instance view is private when the generic view was not, we perform
161 -- the exchange. After completing the instantiation, we restore the
162 -- current visibility. The flag Has_Private_View marks identifiers in the
163 -- the generic unit that require checking.
165 -- Visibility within nested generic units requires special handling.
166 -- Consider the following scheme:
168 -- type Global is ... -- outside of generic unit.
172 -- type Semi_Global is ... -- global to inner.
175 -- procedure inner (X1 : Global; X2 : Semi_Global);
177 -- procedure in2 is new inner (...); -- 4
180 -- package New_Outer is new Outer (...); -- 2
181 -- procedure New_Inner is new New_Outer.Inner (...); -- 3
183 -- The semantic analysis of Outer captures all occurrences of Global.
184 -- The semantic analysis of Inner (at 1) captures both occurrences of
185 -- Global and Semi_Global.
187 -- At point 2 (instantiation of Outer), we also produce a generic copy
188 -- of Inner, even though Inner is, at that point, not being instantiated.
189 -- (This is just part of the semantic analysis of New_Outer).
191 -- Critically, references to Global within Inner must be preserved, while
192 -- references to Semi_Global should not preserved, because they must now
193 -- resolve to an entity within New_Outer. To distinguish between these, we
194 -- use a global variable, Current_Instantiated_Parent, which is set when
195 -- performing a generic copy during instantiation (at 2). This variable is
196 -- used when performing a generic copy that is not an instantiation, but
197 -- that is nested within one, as the occurrence of 1 within 2. The analysis
198 -- of a nested generic only preserves references that are global to the
199 -- enclosing Current_Instantiated_Parent. We use the Scope_Depth value to
200 -- determine whether a reference is external to the given parent.
202 -- The instantiation at point 3 requires no special treatment. The method
203 -- works as well for further nestings of generic units, but of course the
204 -- variable Current_Instantiated_Parent must be stacked because nested
205 -- instantiations can occur, e.g. the occurrence of 4 within 2.
207 -- The instantiation of package and subprogram bodies is handled in a
208 -- similar manner, except that it is delayed until after semantic
209 -- analysis is complete. In this fashion complex cross-dependencies
210 -- between several package declarations and bodies containing generics
211 -- can be compiled which otherwise would diagnose spurious circularities.
213 -- For example, it is possible to compile two packages A and B that
214 -- have the following structure:
216 -- package A is package B is
217 -- generic ... generic ...
218 -- package G_A is package G_B is
221 -- package body A is package body B is
222 -- package N_B is new G_B (..) package N_A is new G_A (..)
224 -- The table Pending_Instantiations in package Inline is used to keep
225 -- track of body instantiations that are delayed in this manner. Inline
226 -- handles the actual calls to do the body instantiations. This activity
227 -- is part of Inline, since the processing occurs at the same point, and
228 -- for essentially the same reason, as the handling of inlined routines.
230 ----------------------------------------------
231 -- Detection of Instantiation Circularities --
232 ----------------------------------------------
234 -- If we have a chain of instantiations that is circular, this is static
235 -- error which must be detected at compile time. The detection of these
236 -- circularities is carried out at the point that we insert a generic
237 -- instance spec or body. If there is a circularity, then the analysis of
238 -- the offending spec or body will eventually result in trying to load the
239 -- same unit again, and we detect this problem as we analyze the package
240 -- instantiation for the second time.
242 -- At least in some cases after we have detected the circularity, we get
243 -- into trouble if we try to keep going. The following flag is set if a
244 -- circularity is detected, and used to abandon compilation after the
245 -- messages have been posted.
247 Circularity_Detected
: Boolean := False;
248 -- It should really be reset upon encountering a new main unit, but in
249 -- practice we do not use multiple main units so this is not critical.
251 -----------------------------------------
252 -- Implementation of Generic Contracts --
253 -----------------------------------------
255 -- A "contract" is a collection of aspects and pragmas that either verify a
256 -- property of a construct at runtime or classify the data flow to and from
257 -- the construct in some fashion.
259 -- Generic packages, subprograms and their respective bodies may be subject
260 -- to the following contract-related aspects or pragmas collectively known
263 -- package subprogram [body]
264 -- Abstract_State Always_Terminates
265 -- Initial_Condition Contract_Cases
266 -- Initializes Depends
268 -- Extensions_Visible
271 -- Refined_State Post_Class
279 -- Subprogram_Variant
282 -- Most package contract annotations utilize forward references to classify
283 -- data declared within the package [body]. Subprogram annotations then use
284 -- the classifications to further refine them. These inter dependencies are
285 -- problematic with respect to the implementation of generics because their
286 -- analysis, capture of global references and instantiation does not mesh
287 -- well with the existing mechanism.
289 -- 1) Analysis of generic contracts is carried out the same way non-generic
290 -- contracts are analyzed:
292 -- 1.1) General rule - a contract is analyzed after all related aspects
293 -- and pragmas are analyzed. This is done by routines
295 -- Analyze_Package_Body_Contract
296 -- Analyze_Package_Contract
297 -- Analyze_Subprogram_Body_Contract
298 -- Analyze_Subprogram_Contract
300 -- 1.2) Compilation unit - the contract is analyzed after Pragmas_After
303 -- 1.3) Compilation unit body - the contract is analyzed at the end of
304 -- the body declaration list.
306 -- 1.4) Package - the contract is analyzed at the end of the private or
307 -- visible declarations, prior to analyzing the contracts of any nested
308 -- packages or subprograms.
310 -- 1.5) Package body - the contract is analyzed at the end of the body
311 -- declaration list, prior to analyzing the contracts of any nested
312 -- packages or subprograms.
314 -- 1.6) Subprogram - if the subprogram is declared inside a block, a
315 -- package or a subprogram, then its contract is analyzed at the end of
316 -- the enclosing declarations, otherwise the subprogram is a compilation
319 -- 1.7) Subprogram body - if the subprogram body is declared inside a
320 -- block, a package body or a subprogram body, then its contract is
321 -- analyzed at the end of the enclosing declarations, otherwise the
322 -- subprogram is a compilation unit 1.3).
324 -- 2) Capture of global references within contracts is done after capturing
325 -- global references within the generic template. There are two reasons for
326 -- this delay - pragma annotations are not part of the generic template in
327 -- the case of a generic subprogram declaration, and analysis of contracts
330 -- Contract-related source pragmas within generic templates are prepared
331 -- for delayed capture of global references by routine
333 -- Create_Generic_Contract
335 -- The routine associates these pragmas with the contract of the template.
336 -- In the case of a generic subprogram declaration, the routine creates
337 -- generic templates for the pragmas declared after the subprogram because
338 -- they are not part of the template.
340 -- generic -- template starts
341 -- procedure Gen_Proc (Input : Integer); -- template ends
342 -- pragma Precondition (Input > 0); -- requires own template
344 -- 2.1) The capture of global references with aspect specifications and
345 -- source pragmas that apply to a generic unit must be suppressed when
346 -- the generic template is being processed because the contracts have not
347 -- been analyzed yet. Any attempts to capture global references at that
348 -- point will destroy the Associated_Node linkages and leave the template
349 -- undecorated. This delay is controlled by routine
351 -- Requires_Delayed_Save
353 -- 2.2) The real capture of global references within a contract is done
354 -- after the contract has been analyzed, by routine
356 -- Save_Global_References_In_Contract
358 -- 3) The instantiation of a generic contract occurs as part of the
359 -- instantiation of the contract owner. Generic subprogram declarations
360 -- require additional processing when the contract is specified by pragmas
361 -- because the pragmas are not part of the generic template. This is done
364 -- Instantiate_Subprogram_Contract
366 --------------------------------------------------
367 -- Formal packages and partial parameterization --
368 --------------------------------------------------
370 -- When compiling a generic, a formal package is a local instantiation. If
371 -- declared with a box, its generic formals are visible in the enclosing
372 -- generic. If declared with a partial list of actuals, those actuals that
373 -- are defaulted (covered by an Others clause, or given an explicit box
374 -- initialization) are also visible in the enclosing generic, while those
375 -- that have a corresponding actual are not.
377 -- In our source model of instantiation, the same visibility must be
378 -- present in the spec and body of an instance: the names of the formals
379 -- that are defaulted must be made visible within the instance, and made
380 -- invisible (hidden) after the instantiation is complete, so that they
381 -- are not accessible outside of the instance.
383 -- In a generic, a formal package is treated like a special instantiation.
384 -- Our Ada 95 compiler handled formals with and without box in different
385 -- ways. With partial parameterization, we use a single model for both.
386 -- We create a package declaration that consists of the specification of
387 -- the generic package, and a set of declarations that map the actuals
388 -- into local renamings, just as we do for bona fide instantiations. For
389 -- defaulted parameters and formals with a box, we copy directly the
390 -- declarations of the formals into this local package. The result is a
391 -- package whose visible declarations may include generic formals. This
392 -- package is only used for type checking and visibility analysis, and
393 -- never reaches the back end, so it can freely violate the placement
394 -- rules for generic formal declarations.
396 -- The list of declarations (renamings and copies of formals) is built
397 -- by Analyze_Associations, just as for regular instantiations.
399 -- At the point of instantiation, conformance checking must be applied only
400 -- to those parameters that were specified in the formals. We perform this
401 -- checking by creating another internal instantiation, this one including
402 -- only the renamings and the formals (the rest of the package spec is not
403 -- relevant to conformance checking). We can then traverse two lists: the
404 -- list of actuals in the instance that corresponds to the formal package,
405 -- and the list of actuals produced for this bogus instantiation. We apply
406 -- the conformance rules to those actuals that are not defaulted, i.e.
407 -- which still appear as generic formals.
409 -- When we compile an instance body we must make the right parameters
410 -- visible again. The predicate Is_Generic_Formal indicates which of the
411 -- formals should have its Is_Hidden flag reset.
413 -----------------------
414 -- Local subprograms --
415 -----------------------
417 procedure Abandon_Instantiation
(N
: Node_Id
);
418 pragma No_Return
(Abandon_Instantiation
);
419 -- Posts an error message "instantiation abandoned" at the indicated node
420 -- and then raises the exception Instantiation_Error to do it.
422 procedure Analyze_Formal_Array_Type
423 (T
: in out Entity_Id
;
425 -- A formal array type is treated like an array type declaration, and
426 -- invokes Array_Type_Declaration (sem_ch3) whose first parameter is
427 -- in-out, because in the case of an anonymous type the entity is
428 -- actually created in the procedure.
430 -- The following procedures treat other kinds of formal parameters
432 procedure Analyze_Formal_Derived_Interface_Type
437 procedure Analyze_Formal_Derived_Type
442 procedure Analyze_Formal_Interface_Type
447 -- The following subprograms create abbreviated declarations for formal
448 -- scalar types. We introduce an anonymous base of the proper class for
449 -- each of them, and define the formals as constrained first subtypes of
450 -- their bases. The bounds are expressions that are non-static in the
453 procedure Analyze_Formal_Decimal_Fixed_Point_Type
454 (T
: Entity_Id
; Def
: Node_Id
);
455 procedure Analyze_Formal_Discrete_Type
(T
: Entity_Id
; Def
: Node_Id
);
456 procedure Analyze_Formal_Floating_Type
(T
: Entity_Id
; Def
: Node_Id
);
457 procedure Analyze_Formal_Signed_Integer_Type
(T
: Entity_Id
; Def
: Node_Id
);
458 procedure Analyze_Formal_Modular_Type
(T
: Entity_Id
; Def
: Node_Id
);
459 procedure Analyze_Formal_Ordinary_Fixed_Point_Type
460 (T
: Entity_Id
; Def
: Node_Id
);
462 procedure Analyze_Formal_Private_Type
466 -- Creates a new private type, which does not require completion
468 procedure Analyze_Formal_Incomplete_Type
(T
: Entity_Id
; Def
: Node_Id
);
469 -- Ada 2012: Creates a new incomplete type whose actual does not freeze
471 procedure Analyze_Generic_Formal_Part
(N
: Node_Id
);
472 -- Analyze generic formal part
474 procedure Analyze_Generic_Access_Type
(T
: Entity_Id
; Def
: Node_Id
);
475 -- Create a new access type with the given designated type
477 function Analyze_Associations
480 F_Copy
: List_Id
) return List_Id
;
481 -- At instantiation time, build the list of associations between formals
482 -- and actuals. Each association becomes a renaming declaration for the
483 -- formal entity. F_Copy is the analyzed list of formals in the generic
484 -- copy. It is used to apply legality checks to the actuals. I_Node is the
485 -- instantiation node itself.
487 procedure Analyze_Subprogram_Instantiation
491 procedure Build_Instance_Compilation_Unit_Nodes
495 -- This procedure is used in the case where the generic instance of a
496 -- subprogram body or package body is a library unit. In this case, the
497 -- original library unit node for the generic instantiation must be
498 -- replaced by the resulting generic body, and a link made to a new
499 -- compilation unit node for the generic declaration. The argument N is
500 -- the original generic instantiation. Act_Body and Act_Decl are the body
501 -- and declaration of the instance (either package body and declaration
502 -- nodes or subprogram body and declaration nodes depending on the case).
503 -- On return, the node N has been rewritten with the actual body.
505 function Build_Subprogram_Decl_Wrapper
506 (Formal_Subp
: Entity_Id
) return Node_Id
;
507 -- Ada 2022 allows formal subprograms to carry pre/postconditions.
508 -- At the point of instantiation these contracts apply to uses of
509 -- the actual subprogram. This is implemented by creating wrapper
510 -- subprograms instead of the renamings previously used to link
511 -- formal subprograms and the corresponding actuals. If the actual
512 -- is not an entity (e.g. an attribute reference) a renaming is
513 -- created to handle the expansion of the attribute.
515 function Build_Subprogram_Body_Wrapper
516 (Formal_Subp
: Entity_Id
;
517 Actual_Name
: Node_Id
) return Node_Id
;
518 -- The body of the wrapper is a call to the actual, with the generated
519 -- pre/postconditon checks added.
521 procedure Check_Abbreviated_Instance
523 Parent_Installed
: in out Boolean);
524 -- If the name of the generic unit in an abbreviated instantiation is an
525 -- expanded name, then the prefix may be an instance and the selector may
526 -- designate a child unit. If the parent is installed as a result of this
527 -- call, then Parent_Installed is set True, otherwise Parent_Installed is
528 -- unchanged by the call.
530 -- This routine needs to be called for declaration nodes of formal objects,
531 -- types and subprograms to check whether they are the copy, present in the
532 -- visible part of the abbreviated instantiation of formal packages, of the
533 -- declaration node of their corresponding formal parameter in the template
534 -- of the formal package, as specified by RM 12.7(10/2), so as to establish
535 -- the proper context for their analysis.
537 procedure Check_Access_Definition
(N
: Node_Id
);
538 -- Subsidiary routine to null exclusion processing. Perform an assertion
539 -- check on Ada version and the presence of an access definition in N.
541 procedure Check_Formal_Packages
(P_Id
: Entity_Id
);
542 -- Apply the following to all formal packages in generic associations.
543 -- Restore the visibility of the formals of the instance that are not
544 -- defaulted (see RM 12.7 (10)). Remove the anonymous package declaration
545 -- created for formal instances that are not defaulted.
547 procedure Check_Formal_Package_Instance
548 (Formal_Pack
: Entity_Id
;
549 Actual_Pack
: Entity_Id
);
550 -- Verify that the actuals of the actual instance match the actuals of
551 -- the template for a formal package that is not declared with a box.
553 procedure Check_Forward_Instantiation
(Decl
: Node_Id
);
554 -- If the generic is a local entity and the corresponding body has not
555 -- been seen yet, flag enclosing packages to indicate that it will be
556 -- elaborated after the generic body. Subprograms declared in the same
557 -- package cannot be inlined by the front end because front-end inlining
558 -- requires a strict linear order of elaboration.
560 function Check_Hidden_Primitives
(Assoc_List
: List_Id
) return Elist_Id
;
561 -- Check if some association between formals and actuals requires to make
562 -- visible primitives of a tagged type, and make those primitives visible.
563 -- Return the list of primitives whose visibility is modified (to restore
564 -- their visibility later through Restore_Hidden_Primitives). If no
565 -- candidate is found then return No_Elist.
567 procedure Check_Hidden_Child_Unit
569 Gen_Unit
: Entity_Id
;
570 Act_Decl_Id
: Entity_Id
);
571 -- If the generic unit is an implicit child instance within a parent
572 -- instance, we need to make an explicit test that it is not hidden by
573 -- a child instance of the same name and parent.
575 procedure Check_Generic_Actuals
576 (Instance
: Entity_Id
;
577 Is_Formal_Box
: Boolean);
578 -- Similar to previous one. Check the actuals in the instantiation,
579 -- whose views can change between the point of instantiation and the point
580 -- of instantiation of the body. In addition, mark the generic renamings
581 -- as generic actuals, so that they are not compatible with other actuals.
582 -- Recurse on an actual that is a formal package whose declaration has
585 function Component_Type_For_Private_View
(T
: Entity_Id
) return Entity_Id
;
586 -- Return the component type of array type T, with the following addition:
587 -- if this component type itself is an array type which has not been first
588 -- declared as private, then recurse on it. This makes it possible to deal
589 -- with arrays of arrays the same way as multi-dimensional arrays in the
590 -- mechanism handling private views.
592 function Contains_Instance_Of
595 N
: Node_Id
) return Boolean;
596 -- Inner is instantiated within the generic Outer. Check whether Inner
597 -- directly or indirectly contains an instance of Outer or of one of its
598 -- parents, in the case of a subunit. Each generic unit holds a list of
599 -- the entities instantiated within (at any depth). This procedure
600 -- determines whether the set of such lists contains a cycle, i.e. an
601 -- illegal circular instantiation.
603 function Denotes_Formal_Package
605 On_Exit
: Boolean := False;
606 Instance
: Entity_Id
:= Empty
) return Boolean;
607 -- Returns True if E is a formal package of an enclosing generic, or
608 -- the actual for such a formal in an enclosing instantiation. If such
609 -- a package is used as a formal in an nested generic, or as an actual
610 -- in a nested instantiation, the visibility of ITS formals should not
611 -- be modified. When called from within Restore_Private_Views, the flag
612 -- On_Exit is true, to indicate that the search for a possible enclosing
613 -- instance should ignore the current one. In that case Instance denotes
614 -- the declaration for which this is an actual. This declaration may be
615 -- an instantiation in the source, or the internal instantiation that
616 -- corresponds to the actual for a formal package.
618 function Earlier
(N1
, N2
: Node_Id
) return Boolean;
619 -- Yields True if N1 and N2 appear in the same compilation unit,
620 -- ignoring subunits, and if N1 is to the left of N2 in a left-to-right
621 -- traversal of the tree for the unit. Used to determine the placement
622 -- of freeze nodes for instance bodies that may depend on other instances.
624 function Find_Actual_Type
626 Gen_Type
: Entity_Id
) return Entity_Id
;
627 -- When validating the actual types of a child instance, check whether
628 -- the formal is a formal type of the parent unit, and retrieve the current
629 -- actual for it. Typ is the entity in the analyzed formal type declaration
630 -- (component or index type of an array type, or designated type of an
631 -- access formal) and Gen_Type is the enclosing analyzed formal array
632 -- or access type. The desired actual may be a formal of a parent, or may
633 -- be declared in a formal package of a parent. In both cases it is a
634 -- generic actual type because it appears within a visible instance.
635 -- Finally, it may be declared in a parent unit without being a formal
636 -- of that unit, in which case it must be retrieved by visibility.
637 -- Ambiguities may still arise if two homonyms are declared in two formal
638 -- packages, and the prefix of the formal type may be needed to resolve
639 -- the ambiguity in the instance ???
641 procedure Freeze_Package_Instance
646 -- If the instantiation happens textually before the body of the generic,
647 -- the instantiation of the body must be analyzed after the generic body,
648 -- and not at the point of instantiation. Such early instantiations can
649 -- happen if the generic and the instance appear in a package declaration
650 -- because the generic body can only appear in the corresponding package
651 -- body. Early instantiations can also appear if generic, instance and
652 -- body are all in the declarative part of a subprogram or entry. Entities
653 -- of packages that are early instantiations are delayed, and their freeze
654 -- node appears after the generic body. This rather complex machinery is
655 -- needed when nested instantiations are present, because the source does
656 -- not carry any indication of where the corresponding instance bodies must
657 -- be installed and frozen.
659 procedure Freeze_Subprogram_Instance
662 Pack_Id
: Entity_Id
);
663 -- The generic body may appear textually after the instance, including
664 -- in the proper body of a stub, or within a different package instance.
665 -- Given that the instance can only be elaborated after the generic, we
666 -- place freeze nodes for the instance and/or for packages that may enclose
667 -- the instance and the generic, so that the back-end can establish the
668 -- proper order of elaboration.
670 function Get_Associated_Entity
(Id
: Entity_Id
) return Entity_Id
;
671 -- Similar to Get_Associated_Node below, but for entities
673 function Get_Associated_Node
(N
: Node_Id
) return Node_Id
;
674 -- In order to propagate semantic information back from the analyzed copy
675 -- to the original generic, we maintain links between selected nodes in the
676 -- generic and their corresponding copies. At the end of generic analysis,
677 -- the routine Save_Global_References traverses the generic tree, examines
678 -- the semantic information, and preserves the links to those nodes that
679 -- contain global information. At instantiation, the information from the
680 -- associated node is placed on the new copy, so that name resolution is
683 -- Three kinds of source nodes have associated nodes:
685 -- a) those that can reference (denote) entities, that is identifiers,
686 -- character literals, expanded_names, operator symbols, operators,
687 -- and attribute reference nodes. These nodes have an Entity field
688 -- and are the set of nodes that are in N_Has_Entity.
690 -- b) aggregates (N_Aggregate and N_Extension_Aggregate)
692 -- c) selected components (N_Selected_Component)
694 -- For the first class, the associated node preserves the entity if it is
695 -- global. If the generic contains nested instantiations, the associated
696 -- node itself has been recopied, and a chain of them must be followed.
698 -- For aggregates, the associated node allows retrieval of the type, which
699 -- may otherwise not appear in the generic. The view of this type may be
700 -- different between generic and instantiation, and the full view can be
701 -- installed before the instantiation is analyzed. For aggregates of type
702 -- extensions, the same view exchange may have to be performed for some of
703 -- the ancestor types, if their view is private at the point of
706 -- Nodes that are selected components in the parse tree may be rewritten
707 -- as expanded names after resolution, and must be treated as potential
708 -- entity holders, which is why they also have an Associated_Node.
710 -- Nodes that do not come from source, such as freeze nodes, do not appear
711 -- in the generic tree, and need not have an associated node.
713 -- The associated node is stored in the Associated_Node field. Note that
714 -- this field overlaps Entity, which is fine, because the whole point is
715 -- that we don't need or want the normal Entity field in this situation.
717 function Has_Been_Exchanged
(E
: Entity_Id
) return Boolean;
718 -- Traverse the Exchanged_Views list to see if a type was private
719 -- and has already been flipped during this phase of instantiation.
721 function Has_Contracts
(Decl
: Node_Id
) return Boolean;
722 -- Determine whether a formal subprogram has a Pre- or Postcondition,
723 -- in which case a subprogram wrapper has to be built for the actual.
725 procedure Hide_Current_Scope
;
726 -- When instantiating a generic child unit, the parent context must be
727 -- present, but the instance and all entities that may be generated
728 -- must be inserted in the current scope. We leave the current scope
729 -- on the stack, but make its entities invisible to avoid visibility
730 -- problems. This is reversed at the end of the instantiation. This is
731 -- not done for the instantiation of the bodies, which only require the
732 -- instances of the generic parents to be in scope.
734 function In_Main_Context
(E
: Entity_Id
) return Boolean;
735 -- Check whether an instantiation is in the context of the main unit.
736 -- Used to determine whether its body should be elaborated to allow
737 -- front-end inlining.
739 procedure Inherit_Context
(Gen_Decl
: Node_Id
; Inst
: Node_Id
);
740 -- Add the context clause of the unit containing a generic unit to a
741 -- compilation unit that is, or contains, an instantiation.
744 -- Establish environment for subsequent instantiation. Separated from
745 -- Save_Env because data-structures for visibility handling must be
746 -- initialized before call to Check_Generic_Child_Unit.
748 procedure Inline_Instance_Body
750 Gen_Unit
: Entity_Id
;
752 -- If front-end inlining is requested, instantiate the package body,
753 -- and preserve the visibility of its compilation unit, to insure
754 -- that successive instantiations succeed.
756 procedure Insert_Freeze_Node_For_Instance
759 -- N denotes a package or a subprogram instantiation and F_Node is the
760 -- associated freeze node. Insert the freeze node before the first source
761 -- body which follows immediately after N. If no such body is found, the
762 -- freeze node is inserted at the end of the declarative region which
763 -- contains N, unless the instantiation is done in a package spec that is
764 -- not at library level, in which case it is inserted at the outer level.
765 -- This can also be invoked to insert the freeze node of a package that
766 -- encloses an instantiation, in which case N may denote an arbitrary node.
768 procedure Install_Formal_Packages
(Par
: Entity_Id
);
769 -- Install the visible part of any formal of the parent that is a formal
770 -- package. Note that for the case of a formal package with a box, this
771 -- includes the formal part of the formal package (12.7(10/2)).
773 procedure Install_Hidden_Primitives
774 (Prims_List
: in out Elist_Id
;
777 -- Remove suffix 'P' from hidden primitives of Act_T to match the
778 -- visibility of primitives of Gen_T. The list of primitives to which
779 -- the suffix is removed is added to Prims_List to restore them later.
781 procedure Install_Parent
(P
: Entity_Id
; In_Body
: Boolean := False);
782 -- When compiling an instance of a child unit the parent (which is
783 -- itself an instance) is an enclosing scope that must be made
784 -- immediately visible. This procedure is also used to install the non-
785 -- generic parent of a generic child unit when compiling its body, so
786 -- that full views of types in the parent are made visible.
788 -- The functions Instantiate_XXX perform various legality checks and build
789 -- the declarations for instantiated generic parameters. In all of these
790 -- Formal is the entity in the generic unit, Actual is the entity of
791 -- expression in the generic associations, and Analyzed_Formal is the
792 -- formal in the generic copy, which contains the semantic information to
793 -- be used to validate the actual.
795 function Instantiate_Object
798 Analyzed_Formal
: Node_Id
) return List_Id
;
800 function Instantiate_Type
803 Analyzed_Formal
: Node_Id
;
804 Actual_Decls
: List_Id
) return List_Id
;
806 function Instantiate_Formal_Subprogram
809 Analyzed_Formal
: Node_Id
) return Node_Id
;
811 function Instantiate_Formal_Package
814 Analyzed_Formal
: Node_Id
) return List_Id
;
815 -- If the formal package is declared with a box, special visibility rules
816 -- apply to its formals: they are in the visible part of the package. This
817 -- is true in the declarative region of the formal package, that is to say
818 -- in the enclosing generic or instantiation. For an instantiation, the
819 -- parameters of the formal package are made visible in an explicit step.
820 -- Furthermore, if the actual has a visible USE clause, these formals must
821 -- be made potentially use-visible as well. On exit from the enclosing
822 -- instantiation, the reverse must be done.
824 -- For a formal package declared without a box, there are conformance rules
825 -- that apply to the actuals in the generic declaration and the actuals of
826 -- the actual package in the enclosing instantiation. The simplest way to
827 -- apply these rules is to repeat the instantiation of the formal package
828 -- in the context of the enclosing instance, and compare the generic
829 -- associations of this instantiation with those of the actual package.
830 -- This internal instantiation only needs to contain the renamings of the
831 -- formals: the visible and private declarations themselves need not be
834 -- In Ada 2005, the formal package may be only partially parameterized.
835 -- In that case the visibility step must make visible those actuals whose
836 -- corresponding formals were given with a box. A final complication
837 -- involves inherited operations from formal derived types, which must
838 -- be visible if the type is.
840 function Is_In_Main_Unit
(N
: Node_Id
) return Boolean;
841 -- Test if given node is in the main unit
843 procedure Load_Parent_Of_Generic
846 Body_Optional
: Boolean := False);
847 -- If the generic appears in a separate non-generic library unit, load the
848 -- corresponding body to retrieve the body of the generic. N is the node
849 -- for the generic instantiation, Spec is the generic package declaration.
851 -- Body_Optional is a flag that indicates that the body is being loaded to
852 -- ensure that temporaries are generated consistently when there are other
853 -- instances in the current declarative part that precede the one being
854 -- loaded. In that case a missing body is acceptable.
856 procedure Map_Formal_Package_Entities
(Form
: Entity_Id
; Act
: Entity_Id
);
857 -- Within the generic part, entities in the formal package are
858 -- visible. To validate subsequent type declarations, indicate
859 -- the correspondence between the entities in the analyzed formal,
860 -- and the entities in the actual package. There are three packages
861 -- involved in the instantiation of a formal package: the parent
862 -- generic P1 which appears in the generic declaration, the fake
863 -- instantiation P2 which appears in the analyzed generic, and whose
864 -- visible entities may be used in subsequent formals, and the actual
865 -- P3 in the instance. To validate subsequent formals, me indicate
866 -- that the entities in P2 are mapped into those of P3. The mapping of
867 -- entities has to be done recursively for nested packages.
869 procedure Move_Freeze_Nodes
873 -- Freeze nodes can be generated in the analysis of a generic unit, but
874 -- will not be seen by the back-end. It is necessary to move those nodes
875 -- to the enclosing scope if they freeze an outer entity. We place them
876 -- at the end of the enclosing generic package, which is semantically
879 procedure Preanalyze_Actuals
(N
: Node_Id
; Inst
: Entity_Id
:= Empty
);
880 -- Analyze actuals to perform name resolution. Full resolution is done
881 -- later, when the expected types are known, but names have to be captured
882 -- before installing parents of generics, that are not visible for the
883 -- actuals themselves.
885 -- If Inst is present, it is the entity of the package instance. This
886 -- entity is marked as having a limited_view actual when some actual is
887 -- a limited view. This is used to place the instance body properly.
889 procedure Provide_Completing_Bodies
(N
: Node_Id
);
890 -- Generate completing bodies for all subprograms found within package or
891 -- subprogram declaration N.
893 procedure Remove_Parent
(In_Body
: Boolean := False);
894 -- Reverse effect after instantiation of child is complete
896 function Requires_Conformance_Checking
(N
: Node_Id
) return Boolean;
897 -- Determine whether the formal package declaration N requires conformance
898 -- checking with actuals in instantiations.
900 procedure Restore_Hidden_Primitives
(Prims_List
: in out Elist_Id
);
901 -- Restore suffix 'P' to primitives of Prims_List and leave Prims_List
904 procedure Set_Instance_Env
905 (Gen_Unit
: Entity_Id
;
906 Act_Unit
: Entity_Id
);
907 -- Save current instance on saved environment, to be used to determine
908 -- the global status of entities in nested instances. Part of Save_Env.
909 -- called after verifying that the generic unit is legal for the instance,
910 -- The procedure also examines whether the generic unit is a predefined
911 -- unit, in order to set configuration switches accordingly. As a result
912 -- the procedure must be called after analyzing and freezing the actuals.
914 procedure Set_Instance_Of
(A
: Entity_Id
; B
: Entity_Id
);
915 -- Associate analyzed generic parameter with corresponding instance. Used
916 -- for semantic checks at instantiation time.
918 function True_Parent
(N
: Node_Id
) return Node_Id
;
919 -- For a subunit, return parent of corresponding stub, else return
922 procedure Valid_Default_Attribute
(Nam
: Entity_Id
; Def
: Node_Id
);
923 -- Verify that an attribute that appears as the default for a formal
924 -- subprogram is a function or procedure with the correct profile.
926 procedure Validate_Formal_Type_Default
(Decl
: Node_Id
);
927 -- Ada_2022 AI12-205: if a default subtype_mark is present, verify
928 -- that it is the name of a type in the same class as the formal.
929 -- The treatment parallels what is done in Instantiate_Type but differs
930 -- in a few ways so that this machinery cannot be reused as is: on one
931 -- hand there are no visibility issues for a default, because it is
932 -- analyzed in the same context as the formal type definition; on the
933 -- other hand the check needs to take into acount the use of a previous
934 -- formal type in the current formal type definition (see details in
937 -------------------------------------------
938 -- Data Structures for Generic Renamings --
939 -------------------------------------------
941 -- The map Generic_Renamings associates generic entities with their
942 -- corresponding actuals. Currently used to validate type instances. It
943 -- will eventually be used for all generic parameters to eliminate the
944 -- need for overload resolution in the instance.
946 type Assoc_Ptr
is new Int
;
948 Assoc_Null
: constant Assoc_Ptr
:= -1;
953 Next_In_HTable
: Assoc_Ptr
;
956 package Generic_Renamings
is new Table
.Table
957 (Table_Component_Type
=> Assoc
,
958 Table_Index_Type
=> Assoc_Ptr
,
959 Table_Low_Bound
=> 0,
961 Table_Increment
=> 100,
962 Table_Name
=> "Generic_Renamings");
964 -- Variable to hold enclosing instantiation. When the environment is
965 -- saved for a subprogram inlining, the corresponding Act_Id is empty.
967 Current_Instantiated_Parent
: Assoc
:= (Empty
, Empty
, Assoc_Null
);
969 -- Hash table for associations
971 HTable_Size
: constant := 37;
972 type HTable_Range
is range 0 .. HTable_Size
- 1;
974 procedure Set_Next_Assoc
(E
: Assoc_Ptr
; Next
: Assoc_Ptr
);
975 function Next_Assoc
(E
: Assoc_Ptr
) return Assoc_Ptr
;
976 function Get_Gen_Id
(E
: Assoc_Ptr
) return Entity_Id
;
977 function Hash
(F
: Entity_Id
) return HTable_Range
;
979 package Generic_Renamings_HTable
is new GNAT
.HTable
.Static_HTable
(
980 Header_Num
=> HTable_Range
,
982 Elmt_Ptr
=> Assoc_Ptr
,
983 Null_Ptr
=> Assoc_Null
,
984 Set_Next
=> Set_Next_Assoc
,
987 Get_Key
=> Get_Gen_Id
,
991 Exchanged_Views
: Elist_Id
;
992 -- This list holds the private views that have been exchanged during
993 -- instantiation to restore the visibility of the generic declaration.
994 -- (see comments above). After instantiation, the current visibility is
995 -- reestablished by means of a traversal of this list.
997 Hidden_Entities
: Elist_Id
;
998 -- This list holds the entities of the current scope that are removed
999 -- from immediate visibility when instantiating a child unit. Their
1000 -- visibility is restored in Remove_Parent.
1002 -- Because instantiations can be recursive, the following must be saved
1003 -- on entry and restored on exit from an instantiation (spec or body).
1004 -- This is done by the two procedures Save_Env and Restore_Env. For
1005 -- package and subprogram instantiations (but not for the body instances)
1006 -- the action of Save_Env is done in two steps: Init_Env is called before
1007 -- Check_Generic_Child_Unit, because setting the parent instances requires
1008 -- that the visibility data structures be properly initialized. Once the
1009 -- generic is unit is validated, Set_Instance_Env completes Save_Env.
1011 Parent_Unit_Visible
: Boolean := False;
1012 -- Parent_Unit_Visible is used when the generic is a child unit, and
1013 -- indicates whether the ultimate parent of the generic is visible in the
1014 -- instantiation environment. It is used to reset the visibility of the
1015 -- parent at the end of the instantiation (see Remove_Parent).
1017 Instance_Parent_Unit
: Entity_Id
:= Empty
;
1018 -- This records the ultimate parent unit of an instance of a generic
1019 -- child unit and is used in conjunction with Parent_Unit_Visible to
1020 -- indicate the unit to which the Parent_Unit_Visible flag corresponds.
1022 type Instance_Env
is record
1023 Instantiated_Parent
: Assoc
;
1024 Exchanged_Views
: Elist_Id
;
1025 Hidden_Entities
: Elist_Id
;
1026 Current_Sem_Unit
: Unit_Number_Type
;
1027 Parent_Unit_Visible
: Boolean := False;
1028 Instance_Parent_Unit
: Entity_Id
:= Empty
;
1029 Switches
: Config_Switches_Type
;
1032 package Instance_Envs
is new Table
.Table
(
1033 Table_Component_Type
=> Instance_Env
,
1034 Table_Index_Type
=> Int
,
1035 Table_Low_Bound
=> 0,
1036 Table_Initial
=> 32,
1037 Table_Increment
=> 100,
1038 Table_Name
=> "Instance_Envs");
1040 procedure Restore_Private_Views
1041 (Pack_Id
: Entity_Id
;
1042 Is_Package
: Boolean := True);
1043 -- Restore the private views of external types, and unmark the generic
1044 -- renamings of actuals, so that they become compatible subtypes again.
1045 -- For subprograms, Pack_Id is the package constructed to hold the
1048 procedure Switch_View
(T
: Entity_Id
);
1049 -- Switch the partial and full views of a type and its private
1050 -- dependents (i.e. its subtypes and derived types).
1052 ------------------------------------
1053 -- Structures for Error Reporting --
1054 ------------------------------------
1056 Instantiation_Node
: Node_Id
;
1057 -- Used by subprograms that validate instantiation of formal parameters
1058 -- where there might be no actual on which to place the error message.
1059 -- Also used to locate the instantiation node for generic subunits.
1061 Instantiation_Error
: exception;
1062 -- When there is a semantic error in the generic parameter matching,
1063 -- there is no point in continuing the instantiation, because the
1064 -- number of cascaded errors is unpredictable. This exception aborts
1065 -- the instantiation process altogether.
1067 S_Adjustment
: Sloc_Adjustment
;
1068 -- Offset created for each node in an instantiation, in order to keep
1069 -- track of the source position of the instantiation in each of its nodes.
1070 -- A subsequent semantic error or warning on a construct of the instance
1071 -- points to both places: the original generic node, and the point of
1072 -- instantiation. See Sinput and Sinput.L for additional details.
1074 ------------------------------------------------------------
1075 -- Data structure for keeping track when inside a Generic --
1076 ------------------------------------------------------------
1078 -- The following table is used to save values of the Inside_A_Generic
1079 -- flag (see spec of Sem) when they are saved by Start_Generic.
1081 package Generic_Flags
is new Table
.Table
(
1082 Table_Component_Type
=> Boolean,
1083 Table_Index_Type
=> Int
,
1084 Table_Low_Bound
=> 0,
1085 Table_Initial
=> 32,
1086 Table_Increment
=> 200,
1087 Table_Name
=> "Generic_Flags");
1089 ---------------------------
1090 -- Abandon_Instantiation --
1091 ---------------------------
1093 procedure Abandon_Instantiation
(N
: Node_Id
) is
1095 Error_Msg_N
("\instantiation abandoned!", N
);
1096 raise Instantiation_Error
;
1097 end Abandon_Instantiation
;
1099 ----------------------------------
1100 -- Adjust_Inherited_Pragma_Sloc --
1101 ----------------------------------
1103 procedure Adjust_Inherited_Pragma_Sloc
(N
: Node_Id
) is
1105 Adjust_Instantiation_Sloc
(N
, S_Adjustment
);
1106 end Adjust_Inherited_Pragma_Sloc
;
1108 --------------------------
1109 -- Analyze_Associations --
1110 --------------------------
1112 function Analyze_Associations
1115 F_Copy
: List_Id
) return List_Id
1117 Actuals_To_Freeze
: constant Elist_Id
:= New_Elmt_List
;
1118 Assoc_List
: constant List_Id
:= New_List
;
1119 Default_Actuals
: constant List_Id
:= New_List
;
1120 Gen_Unit
: constant Entity_Id
:=
1121 Defining_Entity
(Parent
(F_Copy
));
1125 Analyzed_Formal
: Node_Id
;
1126 First_Named
: Node_Id
:= Empty
;
1128 Match
: Node_Id
:= Empty
;
1130 Saved_Formal
: Node_Id
;
1132 Default_Formals
: constant List_Id
:= New_List
;
1133 -- If an Others_Choice is present, some of the formals may be defaulted.
1134 -- To simplify the treatment of visibility in an instance, we introduce
1135 -- individual defaults for each such formal. These defaults are
1136 -- appended to the list of associations and replace the Others_Choice.
1138 Found_Assoc
: Node_Id
;
1139 -- Association for the current formal being match. Empty if there are
1140 -- no remaining actuals, or if there is no named association with the
1141 -- name of the formal.
1143 Is_Named_Assoc
: Boolean;
1144 Num_Matched
: Nat
:= 0;
1145 Num_Actuals
: Nat
:= 0;
1147 Others_Present
: Boolean := False;
1148 Others_Choice
: Node_Id
:= Empty
;
1149 -- In Ada 2005, indicates partial parameterization of a formal
1150 -- package. As usual an other association must be last in the list.
1152 procedure Build_Subprogram_Wrappers
;
1153 -- Ada 2022: AI12-0272 introduces pre/postconditions for formal
1154 -- subprograms. The implementation of making the formal into a renaming
1155 -- of the actual does not work, given that subprogram renaming cannot
1156 -- carry aspect specifications. Instead we must create subprogram
1157 -- wrappers whose body is a call to the actual, and whose declaration
1158 -- carries the aspects of the formal.
1160 procedure Check_Fixed_Point_Actual
(Actual
: Node_Id
);
1161 -- Warn if an actual fixed-point type has user-defined arithmetic
1162 -- operations, but there is no corresponding formal in the generic,
1163 -- in which case the predefined operations will be used. This merits
1164 -- a warning because of the special semantics of fixed point ops.
1166 procedure Check_Overloaded_Formal_Subprogram
(Formal
: Node_Id
);
1167 -- Apply RM 12.3(9): if a formal subprogram is overloaded, the instance
1168 -- cannot have a named association for it. AI05-0025 extends this rule
1169 -- to formals of formal packages by AI05-0025, and it also applies to
1170 -- box-initialized formals.
1172 function Has_Fully_Defined_Profile
(Subp
: Entity_Id
) return Boolean;
1173 -- Determine whether the parameter types and the return type of Subp
1174 -- are fully defined at the point of instantiation.
1176 function Matching_Actual
1178 A_F
: Entity_Id
) return Node_Id
;
1179 -- Find actual that corresponds to a given formal parameter. If the
1180 -- actuals are positional, return the next one, if any. If the actuals
1181 -- are named, scan the parameter associations to find the right one.
1182 -- A_F is the corresponding entity in the analyzed generic, which is
1183 -- placed on the selector name.
1185 -- In Ada 2005, a named association may be given with a box, in which
1186 -- case Matching_Actual sets Found_Assoc to the generic association,
1187 -- but return Empty for the actual itself. In this case the code below
1188 -- creates a corresponding declaration for the formal.
1190 function Partial_Parameterization
return Boolean;
1191 -- Ada 2005: if no match is found for a given formal, check if the
1192 -- association for it includes a box, or whether the associations
1193 -- include an Others clause.
1195 procedure Process_Default
(Formal
: Node_Id
);
1196 -- Add a copy of the declaration of a generic formal to the list of
1197 -- associations, and add an explicit box association for its entity
1198 -- if there is none yet, and the default comes from an Others_Choice.
1200 function Renames_Standard_Subprogram
(Subp
: Entity_Id
) return Boolean;
1201 -- Determine whether Subp renames one of the subprograms defined in the
1202 -- generated package Standard.
1204 procedure Set_Analyzed_Formal
;
1205 -- Find the node in the generic copy that corresponds to a given formal.
1206 -- The semantic information on this node is used to perform legality
1207 -- checks on the actuals. Because semantic analysis can introduce some
1208 -- anonymous entities or modify the declaration node itself, the
1209 -- correspondence between the two lists is not one-one. In addition to
1210 -- anonymous types, the presence a formal equality will introduce an
1211 -- implicit declaration for the corresponding inequality.
1213 -------------------------------
1214 -- Build_Subprogram_Wrappers --
1215 -------------------------------
1217 procedure Build_Subprogram_Wrappers
is
1218 function Adjust_Aspect_Sloc
(N
: Node_Id
) return Traverse_Result
;
1219 -- Adjust sloc so that errors located at N will be reported with
1220 -- information about the instance and not just about the generic.
1222 ------------------------
1223 -- Adjust_Aspect_Sloc --
1224 ------------------------
1226 function Adjust_Aspect_Sloc
(N
: Node_Id
) return Traverse_Result
is
1228 Adjust_Instantiation_Sloc
(N
, S_Adjustment
);
1230 end Adjust_Aspect_Sloc
;
1232 procedure Adjust_Aspect_Slocs
is new
1233 Traverse_Proc
(Adjust_Aspect_Sloc
);
1235 Formal
: constant Entity_Id
:=
1236 Defining_Unit_Name
(Specification
(Analyzed_Formal
));
1237 Aspect_Spec
: Node_Id
;
1238 Decl_Node
: Node_Id
;
1239 Actual_Name
: Node_Id
;
1241 -- Start of processing for Build_Subprogram_Wrappers
1244 -- Create declaration for wrapper subprogram
1245 -- The actual can be overloaded, in which case it will be
1246 -- resolved when the call in the wrapper body is analyzed.
1247 -- We attach the possible interpretations of the actual to
1248 -- the name to be used in the call in the wrapper body.
1250 if Is_Entity_Name
(Match
) then
1251 Actual_Name
:= New_Occurrence_Of
(Entity
(Match
), Sloc
(Match
));
1253 if Is_Overloaded
(Match
) then
1254 Save_Interps
(Match
, Actual_Name
);
1258 -- Use renaming declaration created when analyzing actual.
1259 -- This may be incomplete if there are several formal
1260 -- subprograms whose actual is an attribute ???
1263 Renaming_Decl
: constant Node_Id
:= Last
(Assoc_List
);
1266 Actual_Name
:= New_Occurrence_Of
1267 (Defining_Entity
(Renaming_Decl
), Sloc
(Match
));
1268 Set_Etype
(Actual_Name
, Get_Instance_Of
(Etype
(Formal
)));
1272 Decl_Node
:= Build_Subprogram_Decl_Wrapper
(Formal
);
1274 -- Transfer aspect specifications from formal subprogram to wrapper
1276 Set_Aspect_Specifications
(Decl_Node
,
1277 New_Copy_List_Tree
(Aspect_Specifications
(Analyzed_Formal
)));
1279 Aspect_Spec
:= First
(Aspect_Specifications
(Decl_Node
));
1280 while Present
(Aspect_Spec
) loop
1281 Adjust_Aspect_Slocs
(Aspect_Spec
);
1282 Set_Analyzed
(Aspect_Spec
, False);
1286 Append_To
(Assoc_List
, Decl_Node
);
1288 -- Create corresponding body, and append it to association list
1289 -- that appears at the head of the declarations in the instance.
1290 -- The subprogram may be called in the analysis of subsequent
1293 Append_To
(Assoc_List
,
1294 Build_Subprogram_Body_Wrapper
(Formal
, Actual_Name
));
1295 end Build_Subprogram_Wrappers
;
1297 ----------------------------------------
1298 -- Check_Overloaded_Formal_Subprogram --
1299 ----------------------------------------
1301 procedure Check_Overloaded_Formal_Subprogram
(Formal
: Node_Id
) is
1302 Temp_Formal
: Node_Id
;
1305 Temp_Formal
:= First
(Formals
);
1306 while Present
(Temp_Formal
) loop
1307 if Nkind
(Temp_Formal
) in N_Formal_Subprogram_Declaration
1308 and then Temp_Formal
/= Formal
1310 Chars
(Defining_Unit_Name
(Specification
(Formal
))) =
1311 Chars
(Defining_Unit_Name
(Specification
(Temp_Formal
)))
1313 if Present
(Found_Assoc
) then
1315 ("named association not allowed for overloaded formal",
1320 ("named association not allowed for overloaded formal",
1324 Abandon_Instantiation
(Instantiation_Node
);
1329 end Check_Overloaded_Formal_Subprogram
;
1331 -------------------------------
1332 -- Check_Fixed_Point_Actual --
1333 -------------------------------
1335 procedure Check_Fixed_Point_Actual
(Actual
: Node_Id
) is
1336 Typ
: constant Entity_Id
:= Entity
(Actual
);
1337 Prims
: constant Elist_Id
:= Collect_Primitive_Operations
(Typ
);
1343 -- Locate primitive operations of the type that are arithmetic
1346 Elem
:= First_Elmt
(Prims
);
1347 while Present
(Elem
) loop
1348 if Nkind
(Node
(Elem
)) = N_Defining_Operator_Symbol
then
1350 -- Check whether the generic unit has a formal subprogram of
1351 -- the same name. This does not check types but is good enough
1352 -- to justify a warning.
1354 Formal
:= First_Non_Pragma
(Formals
);
1355 Op
:= Alias
(Node
(Elem
));
1357 while Present
(Formal
) loop
1358 if Nkind
(Formal
) = N_Formal_Concrete_Subprogram_Declaration
1359 and then Chars
(Defining_Entity
(Formal
)) =
1364 elsif Nkind
(Formal
) = N_Formal_Package_Declaration
then
1370 -- Locate corresponding actual, and check whether it
1371 -- includes a fixed-point type.
1373 Assoc
:= First
(Assoc_List
);
1374 while Present
(Assoc
) loop
1376 Nkind
(Assoc
) = N_Package_Renaming_Declaration
1377 and then Chars
(Defining_Unit_Name
(Assoc
)) =
1378 Chars
(Defining_Identifier
(Formal
));
1383 if Present
(Assoc
) then
1385 -- If formal package declares a fixed-point type,
1386 -- and the user-defined operator is derived from
1387 -- a generic instance package, the fixed-point type
1388 -- does not use the corresponding predefined op.
1390 Ent
:= First_Entity
(Entity
(Name
(Assoc
)));
1391 while Present
(Ent
) loop
1392 if Is_Fixed_Point_Type
(Ent
)
1393 and then Present
(Op
)
1394 and then Is_Generic_Instance
(Scope
(Op
))
1409 Error_Msg_Sloc
:= Sloc
(Node
(Elem
));
1411 ("?instance uses predefined operation, not primitive "
1412 & "operation&#", Actual
, Node
(Elem
));
1418 end Check_Fixed_Point_Actual
;
1420 -------------------------------
1421 -- Has_Fully_Defined_Profile --
1422 -------------------------------
1424 function Has_Fully_Defined_Profile
(Subp
: Entity_Id
) return Boolean is
1425 function Is_Fully_Defined_Type
(Typ
: Entity_Id
) return Boolean;
1426 -- Determine whethet type Typ is fully defined
1428 ---------------------------
1429 -- Is_Fully_Defined_Type --
1430 ---------------------------
1432 function Is_Fully_Defined_Type
(Typ
: Entity_Id
) return Boolean is
1434 -- A private type without a full view is not fully defined
1436 if Is_Private_Type
(Typ
)
1437 and then No
(Full_View
(Typ
))
1441 -- An incomplete type is never fully defined
1443 elsif Is_Incomplete_Type
(Typ
) then
1446 -- All other types are fully defined
1451 end Is_Fully_Defined_Type
;
1453 -- Local declarations
1457 -- Start of processing for Has_Fully_Defined_Profile
1460 -- Check the parameters
1462 Param
:= First_Formal
(Subp
);
1463 while Present
(Param
) loop
1464 if not Is_Fully_Defined_Type
(Etype
(Param
)) then
1468 Next_Formal
(Param
);
1471 -- Check the return type
1473 return Is_Fully_Defined_Type
(Etype
(Subp
));
1474 end Has_Fully_Defined_Profile
;
1476 ---------------------
1477 -- Matching_Actual --
1478 ---------------------
1480 function Matching_Actual
1482 A_F
: Entity_Id
) return Node_Id
1488 Is_Named_Assoc
:= False;
1490 -- End of list of purely positional parameters
1492 if No
(Actual
) or else Nkind
(Actual
) = N_Others_Choice
then
1493 Found_Assoc
:= Empty
;
1496 -- Case of positional parameter corresponding to current formal
1498 elsif No
(Selector_Name
(Actual
)) then
1499 Found_Assoc
:= Actual
;
1500 Act
:= Explicit_Generic_Actual_Parameter
(Actual
);
1501 Num_Matched
:= Num_Matched
+ 1;
1504 -- Otherwise scan list of named actuals to find the one with the
1505 -- desired name. All remaining actuals have explicit names.
1508 Is_Named_Assoc
:= True;
1509 Found_Assoc
:= Empty
;
1513 while Present
(Actual
) loop
1514 if Nkind
(Actual
) = N_Others_Choice
then
1515 Found_Assoc
:= Empty
;
1518 elsif Chars
(Selector_Name
(Actual
)) = Chars
(F
) then
1519 Set_Entity
(Selector_Name
(Actual
), A_F
);
1520 Set_Etype
(Selector_Name
(Actual
), Etype
(A_F
));
1521 Generate_Reference
(A_F
, Selector_Name
(Actual
));
1523 Found_Assoc
:= Actual
;
1524 Act
:= Explicit_Generic_Actual_Parameter
(Actual
);
1525 Num_Matched
:= Num_Matched
+ 1;
1533 -- Reset for subsequent searches. In most cases the named
1534 -- associations are in order. If they are not, we reorder them
1535 -- to avoid scanning twice the same actual. This is not just a
1536 -- question of efficiency: there may be multiple defaults with
1537 -- boxes that have the same name. In a nested instantiation we
1538 -- insert actuals for those defaults, and cannot rely on their
1539 -- names to disambiguate them.
1541 if Actual
= First_Named
then
1544 elsif Present
(Actual
) then
1545 Insert_Before
(First_Named
, Remove_Next
(Prev
));
1548 Actual
:= First_Named
;
1551 if Is_Entity_Name
(Act
) and then Present
(Entity
(Act
)) then
1552 Set_Used_As_Generic_Actual
(Entity
(Act
));
1556 end Matching_Actual
;
1558 ------------------------------
1559 -- Partial_Parameterization --
1560 ------------------------------
1562 function Partial_Parameterization
return Boolean is
1564 return Others_Present
1565 or else (Present
(Found_Assoc
) and then Box_Present
(Found_Assoc
));
1566 end Partial_Parameterization
;
1568 ---------------------
1569 -- Process_Default --
1570 ---------------------
1572 procedure Process_Default
(Formal
: Node_Id
) is
1573 Loc
: constant Source_Ptr
:= Sloc
(I_Node
);
1574 F_Id
: constant Entity_Id
:= Defining_Entity
(Formal
);
1580 -- Append copy of formal declaration to associations, and create new
1581 -- defining identifier for it.
1583 Decl
:= New_Copy_Tree
(Formal
);
1584 Id
:= Make_Defining_Identifier
(Sloc
(F_Id
), Chars
(F_Id
));
1586 if Nkind
(Formal
) in N_Formal_Subprogram_Declaration
then
1587 Set_Defining_Unit_Name
(Specification
(Decl
), Id
);
1590 Set_Defining_Identifier
(Decl
, Id
);
1593 Append
(Decl
, Assoc_List
);
1595 if No
(Found_Assoc
) then
1597 Make_Generic_Association
(Loc
,
1599 New_Occurrence_Of
(Id
, Loc
),
1600 Explicit_Generic_Actual_Parameter
=> Empty
);
1601 Set_Box_Present
(Default
);
1602 Append
(Default
, Default_Formals
);
1604 end Process_Default
;
1606 ---------------------------------
1607 -- Renames_Standard_Subprogram --
1608 ---------------------------------
1610 function Renames_Standard_Subprogram
(Subp
: Entity_Id
) return Boolean is
1615 while Present
(Id
) loop
1616 if Scope
(Id
) = Standard_Standard
then
1624 end Renames_Standard_Subprogram
;
1626 -------------------------
1627 -- Set_Analyzed_Formal --
1628 -------------------------
1630 procedure Set_Analyzed_Formal
is
1634 while Present
(Analyzed_Formal
) loop
1635 Kind
:= Nkind
(Analyzed_Formal
);
1637 case Nkind
(Formal
) is
1638 when N_Formal_Subprogram_Declaration
=>
1639 exit when Kind
in N_Formal_Subprogram_Declaration
1642 (Defining_Unit_Name
(Specification
(Formal
))) =
1644 (Defining_Unit_Name
(Specification
(Analyzed_Formal
)));
1646 when N_Formal_Package_Declaration
=>
1647 exit when Kind
in N_Formal_Package_Declaration
1648 | N_Generic_Package_Declaration
1649 | N_Package_Declaration
;
1651 when N_Use_Package_Clause
1658 -- Skip freeze nodes, and nodes inserted to replace
1659 -- unrecognized pragmas.
1662 Kind
not in N_Formal_Subprogram_Declaration
1663 and then Kind
not in N_Subprogram_Declaration
1667 and then Chars
(Defining_Identifier
(Formal
)) =
1668 Chars
(Defining_Identifier
(Analyzed_Formal
));
1671 Next
(Analyzed_Formal
);
1673 end Set_Analyzed_Formal
;
1675 -- Start of processing for Analyze_Associations
1678 Actuals
:= Generic_Associations
(I_Node
);
1680 if Present
(Actuals
) then
1682 -- Check for an Others choice, indicating a partial parameterization
1683 -- for a formal package.
1685 Actual
:= First
(Actuals
);
1686 while Present
(Actual
) loop
1687 if Nkind
(Actual
) = N_Others_Choice
then
1688 Others_Present
:= True;
1689 Others_Choice
:= Actual
;
1691 if Present
(Next
(Actual
)) then
1692 Error_Msg_N
("OTHERS must be last association", Actual
);
1695 -- This subprogram is used both for formal packages and for
1696 -- instantiations. For the latter, associations must all be
1699 if Nkind
(I_Node
) /= N_Formal_Package_Declaration
1700 and then Comes_From_Source
(I_Node
)
1703 ("OTHERS association not allowed in an instance",
1707 -- In any case, nothing to do after the others association
1711 elsif Box_Present
(Actual
)
1712 and then Comes_From_Source
(I_Node
)
1713 and then Nkind
(I_Node
) /= N_Formal_Package_Declaration
1716 ("box association not allowed in an instance", Actual
);
1722 -- If named associations are present, save first named association
1723 -- (it may of course be Empty) to facilitate subsequent name search.
1725 First_Named
:= First
(Actuals
);
1726 while Present
(First_Named
)
1727 and then Nkind
(First_Named
) /= N_Others_Choice
1728 and then No
(Selector_Name
(First_Named
))
1730 Num_Actuals
:= Num_Actuals
+ 1;
1735 Named
:= First_Named
;
1736 while Present
(Named
) loop
1737 if Nkind
(Named
) /= N_Others_Choice
1738 and then No
(Selector_Name
(Named
))
1740 Error_Msg_N
("invalid positional actual after named one", Named
);
1741 Abandon_Instantiation
(Named
);
1744 -- A named association may lack an actual parameter, if it was
1745 -- introduced for a default subprogram that turns out to be local
1746 -- to the outer instantiation. If it has a box association it must
1747 -- correspond to some formal in the generic.
1749 if Nkind
(Named
) /= N_Others_Choice
1750 and then (Present
(Explicit_Generic_Actual_Parameter
(Named
))
1751 or else Box_Present
(Named
))
1753 Num_Actuals
:= Num_Actuals
+ 1;
1759 if Present
(Formals
) then
1760 Formal
:= First_Non_Pragma
(Formals
);
1761 Analyzed_Formal
:= First_Non_Pragma
(F_Copy
);
1763 if Present
(Actuals
) then
1764 Actual
:= First
(Actuals
);
1766 -- All formals should have default values
1772 while Present
(Formal
) loop
1773 Set_Analyzed_Formal
;
1774 Saved_Formal
:= Next_Non_Pragma
(Formal
);
1776 case Nkind
(Formal
) is
1777 when N_Formal_Object_Declaration
=>
1780 (Defining_Identifier
(Formal
),
1781 Defining_Identifier
(Analyzed_Formal
));
1783 if No
(Match
) and then Partial_Parameterization
then
1784 Process_Default
(Formal
);
1788 (Instantiate_Object
(Formal
, Match
, Analyzed_Formal
),
1791 -- For a defaulted in_parameter, create an entry in the
1792 -- the list of defaulted actuals, for GNATprove use. Do
1793 -- not included these defaults for an instance nested
1794 -- within a generic, because the defaults are also used
1795 -- in the analysis of the enclosing generic, and only
1796 -- defaulted subprograms are relevant there.
1798 if No
(Match
) and then not Inside_A_Generic
then
1799 Append_To
(Default_Actuals
,
1800 Make_Generic_Association
(Sloc
(I_Node
),
1803 (Defining_Identifier
(Formal
), Sloc
(I_Node
)),
1804 Explicit_Generic_Actual_Parameter
=>
1805 New_Copy_Tree
(Default_Expression
(Formal
))));
1809 -- If the object is a call to an expression function, this
1810 -- is a freezing point for it.
1812 if Is_Entity_Name
(Match
)
1813 and then Present
(Entity
(Match
))
1815 (Original_Node
(Unit_Declaration_Node
(Entity
(Match
))))
1816 = N_Expression_Function
1818 Append_Elmt
(Entity
(Match
), Actuals_To_Freeze
);
1821 when N_Formal_Type_Declaration
=>
1824 (Defining_Identifier
(Formal
),
1825 Defining_Identifier
(Analyzed_Formal
));
1828 if Partial_Parameterization
then
1829 Process_Default
(Formal
);
1831 elsif Present
(Default_Subtype_Mark
(Formal
)) then
1832 Match
:= New_Copy
(Default_Subtype_Mark
(Formal
));
1835 (Formal
, Match
, Analyzed_Formal
, Assoc_List
),
1837 Append_Elmt
(Entity
(Match
), Actuals_To_Freeze
);
1840 Error_Msg_Sloc
:= Sloc
(Gen_Unit
);
1843 Instantiation_Node
, Defining_Identifier
(Formal
));
1845 ("\in instantiation of & declared#",
1846 Instantiation_Node
, Gen_Unit
);
1847 Abandon_Instantiation
(Instantiation_Node
);
1854 (Formal
, Match
, Analyzed_Formal
, Assoc_List
),
1857 -- Warn when an actual is a fixed-point with user-
1858 -- defined promitives. The warning is superfluous
1859 -- if the formal is private, because there can be
1860 -- no arithmetic operations in the generic so there
1861 -- no danger of confusion.
1863 if Is_Fixed_Point_Type
(Entity
(Match
))
1864 and then not Is_Private_Type
1865 (Defining_Identifier
(Analyzed_Formal
))
1867 Check_Fixed_Point_Actual
(Match
);
1870 -- An instantiation is a freeze point for the actuals,
1871 -- unless this is a rewritten formal package, or the
1872 -- formal is an Ada 2012 formal incomplete type.
1874 if Nkind
(I_Node
) = N_Formal_Package_Declaration
1876 (Ada_Version
>= Ada_2012
1878 Ekind
(Defining_Identifier
(Analyzed_Formal
)) =
1884 Append_Elmt
(Entity
(Match
), Actuals_To_Freeze
);
1888 -- A remote access-to-class-wide type is not a legal actual
1889 -- for a generic formal of an access type (E.2.2(17/2)).
1890 -- In GNAT an exception to this rule is introduced when
1891 -- the formal is marked as remote using implementation
1892 -- defined aspect/pragma Remote_Access_Type. In that case
1893 -- the actual must be remote as well.
1895 -- If the current instantiation is the construction of a
1896 -- local copy for a formal package the actuals may be
1897 -- defaulted, and there is no matching actual to check.
1899 if Nkind
(Analyzed_Formal
) = N_Formal_Type_Declaration
1901 Nkind
(Formal_Type_Definition
(Analyzed_Formal
)) =
1902 N_Access_To_Object_Definition
1903 and then Present
(Match
)
1906 Formal_Ent
: constant Entity_Id
:=
1907 Defining_Identifier
(Analyzed_Formal
);
1909 if Is_Remote_Access_To_Class_Wide_Type
(Entity
(Match
))
1910 = Is_Remote_Types
(Formal_Ent
)
1912 -- Remoteness of formal and actual match
1916 elsif Is_Remote_Types
(Formal_Ent
) then
1918 -- Remote formal, non-remote actual
1921 ("actual for& must be remote", Match
, Formal_Ent
);
1924 -- Non-remote formal, remote actual
1927 ("actual for& may not be remote",
1933 when N_Formal_Subprogram_Declaration
=>
1936 (Defining_Unit_Name
(Specification
(Formal
)),
1937 Defining_Unit_Name
(Specification
(Analyzed_Formal
)));
1939 -- If the formal subprogram has the same name as another
1940 -- formal subprogram of the generic, then a named
1941 -- association is illegal (12.3(9)). Exclude named
1942 -- associations that are generated for a nested instance.
1945 and then Is_Named_Assoc
1946 and then Comes_From_Source
(Found_Assoc
)
1948 Check_Overloaded_Formal_Subprogram
(Formal
);
1951 -- If there is no corresponding actual, this may be case
1952 -- of partial parameterization, or else the formal has a
1953 -- default or a box.
1955 if No
(Match
) and then Partial_Parameterization
then
1956 Process_Default
(Formal
);
1958 if Nkind
(I_Node
) = N_Formal_Package_Declaration
then
1959 Check_Overloaded_Formal_Subprogram
(Formal
);
1963 Append_To
(Assoc_List
,
1964 Instantiate_Formal_Subprogram
1965 (Formal
, Match
, Analyzed_Formal
));
1967 -- If formal subprogram has contracts, create wrappers
1968 -- for it. This is an expansion activity that cannot
1969 -- take place e.g. within an enclosing generic unit.
1971 if Has_Contracts
(Analyzed_Formal
)
1972 and then (Expander_Active
or GNATprove_Mode
)
1974 Build_Subprogram_Wrappers
;
1977 -- An instantiation is a freeze point for the actuals,
1978 -- unless this is a rewritten formal package.
1980 if Nkind
(I_Node
) /= N_Formal_Package_Declaration
1981 and then Nkind
(Match
) = N_Identifier
1982 and then Is_Subprogram
(Entity
(Match
))
1984 -- The actual subprogram may rename a routine defined
1985 -- in Standard. Avoid freezing such renamings because
1986 -- subprograms coming from Standard cannot be frozen.
1989 not Renames_Standard_Subprogram
(Entity
(Match
))
1991 -- If the actual subprogram comes from a different
1992 -- unit, it is already frozen, either by a body in
1993 -- that unit or by the end of the declarative part
1994 -- of the unit. This check avoids the freezing of
1995 -- subprograms defined in Standard which are used
1996 -- as generic actuals.
1998 and then In_Same_Code_Unit
(Entity
(Match
), I_Node
)
1999 and then Has_Fully_Defined_Profile
(Entity
(Match
))
2001 -- Mark the subprogram as having a delayed freeze
2002 -- since this may be an out-of-order action.
2004 Set_Has_Delayed_Freeze
(Entity
(Match
));
2005 Append_Elmt
(Entity
(Match
), Actuals_To_Freeze
);
2009 -- If this is a nested generic, preserve default for later
2010 -- instantiations. We do this as well for GNATprove use,
2011 -- so that the list of generic associations is complete.
2013 if No
(Match
) and then Box_Present
(Formal
) then
2015 Subp
: constant Entity_Id
:=
2017 (Specification
(Last
(Assoc_List
)));
2020 Append_To
(Default_Actuals
,
2021 Make_Generic_Association
(Sloc
(I_Node
),
2023 New_Occurrence_Of
(Subp
, Sloc
(I_Node
)),
2024 Explicit_Generic_Actual_Parameter
=>
2025 New_Occurrence_Of
(Subp
, Sloc
(I_Node
))));
2029 when N_Formal_Package_Declaration
=>
2030 -- The name of the formal package may be hidden by the
2031 -- formal parameter itself.
2033 if Error_Posted
(Analyzed_Formal
) then
2034 Abandon_Instantiation
(Instantiation_Node
);
2039 (Defining_Identifier
(Formal
),
2041 (Original_Node
(Analyzed_Formal
)));
2045 if Partial_Parameterization
then
2046 Process_Default
(Formal
);
2049 Error_Msg_Sloc
:= Sloc
(Gen_Unit
);
2052 Instantiation_Node
, Defining_Identifier
(Formal
));
2054 ("\in instantiation of & declared#",
2055 Instantiation_Node
, Gen_Unit
);
2057 Abandon_Instantiation
(Instantiation_Node
);
2063 (Instantiate_Formal_Package
2064 (Formal
, Match
, Analyzed_Formal
),
2067 -- Determine whether the actual package needs an explicit
2068 -- freeze node. This is only the case if the actual is
2069 -- declared in the same unit and has a body. Normally
2070 -- packages do not have explicit freeze nodes, and gigi
2071 -- only uses them to elaborate entities in a package
2074 Explicit_Freeze_Check
: declare
2075 Actual
: constant Entity_Id
:= Entity
(Match
);
2076 Gen_Par
: Entity_Id
;
2078 Needs_Freezing
: Boolean;
2081 procedure Check_Generic_Parent
;
2082 -- The actual may be an instantiation of a unit
2083 -- declared in a previous instantiation. If that
2084 -- one is also in the current compilation, it must
2085 -- itself be frozen before the actual. The actual
2086 -- may be an instantiation of a generic child unit,
2087 -- in which case the same applies to the instance
2088 -- of the parent which must be frozen before the
2090 -- Should this itself be recursive ???
2092 --------------------------
2093 -- Check_Generic_Parent --
2094 --------------------------
2096 procedure Check_Generic_Parent
is
2097 Inst
: constant Node_Id
:=
2098 Get_Unit_Instantiation_Node
(Actual
);
2104 if Nkind
(Parent
(Actual
)) = N_Package_Specification
2106 Par
:= Scope
(Generic_Parent
(Parent
(Actual
)));
2108 if Is_Generic_Instance
(Par
) then
2111 -- If the actual is a child generic unit, check
2112 -- whether the instantiation of the parent is
2113 -- also local and must also be frozen now. We
2114 -- must retrieve the instance node to locate the
2115 -- parent instance if any.
2117 elsif Ekind
(Par
) = E_Generic_Package
2118 and then Is_Child_Unit
(Gen_Par
)
2119 and then Ekind
(Scope
(Gen_Par
)) =
2122 if Nkind
(Inst
) = N_Package_Instantiation
2123 and then Nkind
(Name
(Inst
)) =
2126 -- Retrieve entity of parent instance
2128 Par
:= Entity
(Prefix
(Name
(Inst
)));
2137 and then Is_Generic_Instance
(Par
)
2138 and then Scope
(Par
) = Current_Scope
2140 (No
(Freeze_Node
(Par
))
2142 not Is_List_Member
(Freeze_Node
(Par
)))
2144 Set_Has_Delayed_Freeze
(Par
);
2145 Append_Elmt
(Par
, Actuals_To_Freeze
);
2147 end Check_Generic_Parent
;
2149 -- Start of processing for Explicit_Freeze_Check
2152 if Present
(Renamed_Entity
(Actual
)) then
2154 Generic_Parent
(Specification
2155 (Unit_Declaration_Node
2156 (Renamed_Entity
(Actual
))));
2159 Generic_Parent
(Specification
2160 (Unit_Declaration_Node
(Actual
)));
2163 if not Expander_Active
2164 or else not Has_Completion
(Actual
)
2165 or else not In_Same_Source_Unit
(I_Node
, Actual
)
2166 or else Is_Frozen
(Actual
)
2168 (Present
(Renamed_Entity
(Actual
))
2170 not In_Same_Source_Unit
2171 (I_Node
, (Renamed_Entity
(Actual
))))
2176 -- Finally we want to exclude such freeze nodes
2177 -- from statement sequences, which freeze
2178 -- everything before them.
2179 -- Is this strictly necessary ???
2181 Needs_Freezing
:= True;
2183 P
:= Parent
(I_Node
);
2184 while Nkind
(P
) /= N_Compilation_Unit
loop
2185 if Nkind
(P
) = N_Handled_Sequence_Of_Statements
2187 Needs_Freezing
:= False;
2194 if Needs_Freezing
then
2195 Check_Generic_Parent
;
2197 -- If the actual is a renaming of a proper
2198 -- instance of the formal package, indicate
2199 -- that it is the instance that must be frozen.
2201 if Nkind
(Parent
(Actual
)) =
2202 N_Package_Renaming_Declaration
2204 Set_Has_Delayed_Freeze
2205 (Renamed_Entity
(Actual
));
2207 (Renamed_Entity
(Actual
),
2210 Set_Has_Delayed_Freeze
(Actual
);
2211 Append_Elmt
(Actual
, Actuals_To_Freeze
);
2215 end Explicit_Freeze_Check
;
2218 -- For use type and use package appearing in the generic part,
2219 -- we have already copied them, so we can just move them where
2220 -- they belong (we mustn't recopy them since this would mess up
2221 -- the Sloc values).
2223 when N_Use_Package_Clause
2226 if Nkind
(Original_Node
(I_Node
)) =
2227 N_Formal_Package_Declaration
2229 Append
(New_Copy_Tree
(Formal
), Assoc_List
);
2232 Append
(Formal
, Assoc_List
);
2236 raise Program_Error
;
2239 -- Check here the correct use of Ghost entities in generic
2240 -- instantiations, as now the generic has been resolved and
2241 -- we know which formal generic parameters are ghost (SPARK
2244 if Nkind
(Formal
) not in N_Use_Package_Clause
2247 Check_Ghost_Context_In_Generic_Association
2249 Formal
=> Defining_Entity
(Analyzed_Formal
));
2252 Formal
:= Saved_Formal
;
2253 Next_Non_Pragma
(Analyzed_Formal
);
2256 if Num_Actuals
> Num_Matched
then
2257 Error_Msg_Sloc
:= Sloc
(Gen_Unit
);
2259 if Present
(Selector_Name
(Actual
)) then
2261 ("unmatched actual &", Actual
, Selector_Name
(Actual
));
2263 ("\in instantiation of & declared#", Actual
, Gen_Unit
);
2266 ("unmatched actual in instantiation of & declared#",
2271 elsif Present
(Actuals
) then
2273 ("too many actuals in generic instantiation", Instantiation_Node
);
2276 -- An instantiation freezes all generic actuals. The only exceptions
2277 -- to this are incomplete types and subprograms which are not fully
2278 -- defined at the point of instantiation.
2281 Elmt
: Elmt_Id
:= First_Elmt
(Actuals_To_Freeze
);
2283 while Present
(Elmt
) loop
2284 Freeze_Before
(I_Node
, Node
(Elmt
));
2289 -- If there are default subprograms, normalize the tree by adding
2290 -- explicit associations for them. This is required if the instance
2291 -- appears within a generic.
2293 if not Is_Empty_List
(Default_Actuals
) then
2298 Default
:= First
(Default_Actuals
);
2299 while Present
(Default
) loop
2300 Mark_Rewrite_Insertion
(Default
);
2304 if No
(Actuals
) then
2305 Set_Generic_Associations
(I_Node
, Default_Actuals
);
2307 Append_List_To
(Actuals
, Default_Actuals
);
2312 -- If this is a formal package, normalize the parameter list by adding
2313 -- explicit box associations for the formals that are covered by an
2316 Append_List
(Default_Formals
, Formals
);
2319 end Analyze_Associations
;
2321 -------------------------------
2322 -- Analyze_Formal_Array_Type --
2323 -------------------------------
2325 procedure Analyze_Formal_Array_Type
2326 (T
: in out Entity_Id
;
2332 -- Treated like a non-generic array declaration, with additional
2337 if Nkind
(Def
) = N_Constrained_Array_Definition
then
2338 DSS
:= First
(Discrete_Subtype_Definitions
(Def
));
2339 while Present
(DSS
) loop
2340 if Nkind
(DSS
) in N_Subtype_Indication
2342 | N_Attribute_Reference
2344 Error_Msg_N
("only a subtype mark is allowed in a formal", DSS
);
2351 Array_Type_Declaration
(T
, Def
);
2352 Set_Is_Generic_Type
(Base_Type
(T
));
2354 if Ekind
(Component_Type
(T
)) = E_Incomplete_Type
2355 and then No
(Full_View
(Component_Type
(T
)))
2357 Error_Msg_N
("premature usage of incomplete type", Def
);
2359 -- Check that range constraint is not allowed on the component type
2360 -- of a generic formal array type (AARM 12.5.3(3))
2362 elsif Is_Internal
(Component_Type
(T
))
2363 and then Present
(Subtype_Indication
(Component_Definition
(Def
)))
2364 and then Nkind
(Original_Node
2365 (Subtype_Indication
(Component_Definition
(Def
)))) =
2366 N_Subtype_Indication
2369 ("in a formal, a subtype indication can only be "
2370 & "a subtype mark (RM 12.5.3(3))",
2371 Subtype_Indication
(Component_Definition
(Def
)));
2374 end Analyze_Formal_Array_Type
;
2376 ---------------------------------------------
2377 -- Analyze_Formal_Decimal_Fixed_Point_Type --
2378 ---------------------------------------------
2380 -- As for other generic types, we create a valid type representation with
2381 -- legal but arbitrary attributes, whose values are never considered
2382 -- static. For all scalar types we introduce an anonymous base type, with
2383 -- the same attributes. We choose the corresponding integer type to be
2384 -- Standard_Integer.
2385 -- Here and in other similar routines, the Sloc of the generated internal
2386 -- type must be the same as the sloc of the defining identifier of the
2387 -- formal type declaration, to provide proper source navigation.
2389 procedure Analyze_Formal_Decimal_Fixed_Point_Type
2393 Loc
: constant Source_Ptr
:= Sloc
(Def
);
2395 Base
: constant Entity_Id
:=
2397 (E_Decimal_Fixed_Point_Type
,
2399 Sloc
(Defining_Identifier
(Parent
(Def
))), 'G');
2401 Int_Base
: constant Entity_Id
:= Standard_Integer
;
2402 Delta_Val
: constant Ureal
:= Ureal_1
;
2403 Digs_Val
: constant Uint
:= Uint_6
;
2405 function Make_Dummy_Bound
return Node_Id
;
2406 -- Return a properly typed universal real literal to use as a bound
2408 ----------------------
2409 -- Make_Dummy_Bound --
2410 ----------------------
2412 function Make_Dummy_Bound
return Node_Id
is
2413 Bound
: constant Node_Id
:= Make_Real_Literal
(Loc
, Ureal_1
);
2415 Set_Etype
(Bound
, Universal_Real
);
2417 end Make_Dummy_Bound
;
2419 -- Start of processing for Analyze_Formal_Decimal_Fixed_Point_Type
2424 Set_Etype
(Base
, Base
);
2425 Set_Size_Info
(Base
, Int_Base
);
2426 Set_RM_Size
(Base
, RM_Size
(Int_Base
));
2427 Set_First_Rep_Item
(Base
, First_Rep_Item
(Int_Base
));
2428 Set_Digits_Value
(Base
, Digs_Val
);
2429 Set_Delta_Value
(Base
, Delta_Val
);
2430 Set_Small_Value
(Base
, Delta_Val
);
2431 Set_Scalar_Range
(Base
,
2433 Low_Bound
=> Make_Dummy_Bound
,
2434 High_Bound
=> Make_Dummy_Bound
));
2436 Set_Is_Generic_Type
(Base
);
2437 Set_Parent
(Base
, Parent
(Def
));
2439 Mutate_Ekind
(T
, E_Decimal_Fixed_Point_Subtype
);
2440 Set_Etype
(T
, Base
);
2441 Set_Size_Info
(T
, Int_Base
);
2442 Set_RM_Size
(T
, RM_Size
(Int_Base
));
2443 Set_First_Rep_Item
(T
, First_Rep_Item
(Int_Base
));
2444 Set_Digits_Value
(T
, Digs_Val
);
2445 Set_Delta_Value
(T
, Delta_Val
);
2446 Set_Small_Value
(T
, Delta_Val
);
2447 Set_Scalar_Range
(T
, Scalar_Range
(Base
));
2448 Set_Is_Constrained
(T
);
2450 Check_Restriction
(No_Fixed_Point
, Def
);
2451 end Analyze_Formal_Decimal_Fixed_Point_Type
;
2453 -------------------------------------------
2454 -- Analyze_Formal_Derived_Interface_Type --
2455 -------------------------------------------
2457 procedure Analyze_Formal_Derived_Interface_Type
2462 Loc
: constant Source_Ptr
:= Sloc
(Def
);
2465 -- Rewrite as a type declaration of a derived type. This ensures that
2466 -- the interface list and primitive operations are properly captured.
2469 Make_Full_Type_Declaration
(Loc
,
2470 Defining_Identifier
=> T
,
2471 Type_Definition
=> Def
));
2473 -- Keep the aspects from the original node
2475 Move_Aspects
(Original_Node
(N
), N
);
2478 Set_Is_Generic_Type
(T
);
2479 end Analyze_Formal_Derived_Interface_Type
;
2481 ---------------------------------
2482 -- Analyze_Formal_Derived_Type --
2483 ---------------------------------
2485 procedure Analyze_Formal_Derived_Type
2490 Loc
: constant Source_Ptr
:= Sloc
(Def
);
2491 Unk_Disc
: constant Boolean := Unknown_Discriminants_Present
(N
);
2495 Set_Is_Generic_Type
(T
);
2497 if Private_Present
(Def
) then
2499 Make_Private_Extension_Declaration
(Loc
,
2500 Defining_Identifier
=> T
,
2501 Discriminant_Specifications
=> Discriminant_Specifications
(N
),
2502 Unknown_Discriminants_Present
=> Unk_Disc
,
2503 Subtype_Indication
=> Subtype_Mark
(Def
),
2504 Interface_List
=> Interface_List
(Def
));
2506 Set_Abstract_Present
(New_N
, Abstract_Present
(Def
));
2507 Set_Limited_Present
(New_N
, Limited_Present
(Def
));
2508 Set_Synchronized_Present
(New_N
, Synchronized_Present
(Def
));
2512 Make_Full_Type_Declaration
(Loc
,
2513 Defining_Identifier
=> T
,
2514 Discriminant_Specifications
=>
2515 Discriminant_Specifications
(Parent
(T
)),
2517 Make_Derived_Type_Definition
(Loc
,
2518 Subtype_Indication
=> Subtype_Mark
(Def
)));
2520 Set_Abstract_Present
2521 (Type_Definition
(New_N
), Abstract_Present
(Def
));
2523 (Type_Definition
(New_N
), Limited_Present
(Def
));
2528 -- Keep the aspects from the original node
2530 Move_Aspects
(Original_Node
(N
), N
);
2535 if not Is_Composite_Type
(T
) then
2537 ("unknown discriminants not allowed for elementary types", N
);
2539 Set_Has_Unknown_Discriminants
(T
);
2540 Set_Is_Constrained
(T
, False);
2544 if Subtype_Mark
(Def
) <= Empty_Or_Error
then
2545 pragma Assert
(Serious_Errors_Detected
> 0);
2546 -- avoid passing bad argument to Entity
2550 -- If the parent type has a known size, so does the formal, which makes
2551 -- legal representation clauses that involve the formal.
2553 Set_Size_Known_At_Compile_Time
2554 (T
, Size_Known_At_Compile_Time
(Entity
(Subtype_Mark
(Def
))));
2555 end Analyze_Formal_Derived_Type
;
2557 ----------------------------------
2558 -- Analyze_Formal_Discrete_Type --
2559 ----------------------------------
2561 -- The operations defined for a discrete types are those of an enumeration
2562 -- type. The size is set to an arbitrary value, for use in analyzing the
2565 procedure Analyze_Formal_Discrete_Type
(T
: Entity_Id
; Def
: Node_Id
) is
2566 Loc
: constant Source_Ptr
:= Sloc
(Def
);
2570 Base
: constant Entity_Id
:=
2572 (E_Floating_Point_Type
, Current_Scope
,
2573 Sloc
(Defining_Identifier
(Parent
(Def
))), 'G');
2577 Mutate_Ekind
(T
, E_Enumeration_Subtype
);
2578 Set_Etype
(T
, Base
);
2580 Reinit_Alignment
(T
);
2581 Set_Is_Generic_Type
(T
);
2582 Set_Is_Constrained
(T
);
2584 -- For semantic analysis, the bounds of the type must be set to some
2585 -- non-static value. The simplest is to create attribute nodes for those
2586 -- bounds, that refer to the type itself. These bounds are never
2587 -- analyzed but serve as place-holders.
2590 Make_Attribute_Reference
(Loc
,
2591 Attribute_Name
=> Name_First
,
2592 Prefix
=> New_Occurrence_Of
(T
, Loc
));
2596 Make_Attribute_Reference
(Loc
,
2597 Attribute_Name
=> Name_Last
,
2598 Prefix
=> New_Occurrence_Of
(T
, Loc
));
2601 Set_Scalar_Range
(T
,
2606 Mutate_Ekind
(Base
, E_Enumeration_Type
);
2607 Set_Etype
(Base
, Base
);
2608 Init_Size
(Base
, 8);
2609 Reinit_Alignment
(Base
);
2610 Set_Is_Generic_Type
(Base
);
2611 Set_Scalar_Range
(Base
, Scalar_Range
(T
));
2612 Set_Parent
(Base
, Parent
(Def
));
2613 end Analyze_Formal_Discrete_Type
;
2615 ----------------------------------
2616 -- Analyze_Formal_Floating_Type --
2617 ---------------------------------
2619 procedure Analyze_Formal_Floating_Type
(T
: Entity_Id
; Def
: Node_Id
) is
2620 Base
: constant Entity_Id
:=
2622 (E_Floating_Point_Type
, Current_Scope
,
2623 Sloc
(Defining_Identifier
(Parent
(Def
))), 'G');
2626 -- The various semantic attributes are taken from the predefined type
2627 -- Float, just so that all of them are initialized. Their values are
2628 -- never used because no constant folding or expansion takes place in
2629 -- the generic itself.
2632 Mutate_Ekind
(T
, E_Floating_Point_Subtype
);
2633 Set_Etype
(T
, Base
);
2634 Set_Size_Info
(T
, (Standard_Float
));
2635 Set_RM_Size
(T
, RM_Size
(Standard_Float
));
2636 Set_Digits_Value
(T
, Digits_Value
(Standard_Float
));
2637 Set_Scalar_Range
(T
, Scalar_Range
(Standard_Float
));
2638 Set_Is_Constrained
(T
);
2640 Set_Is_Generic_Type
(Base
);
2641 Set_Etype
(Base
, Base
);
2642 Set_Size_Info
(Base
, (Standard_Float
));
2643 Set_RM_Size
(Base
, RM_Size
(Standard_Float
));
2644 Set_Digits_Value
(Base
, Digits_Value
(Standard_Float
));
2645 Set_Scalar_Range
(Base
, Scalar_Range
(Standard_Float
));
2646 Set_Parent
(Base
, Parent
(Def
));
2648 Check_Restriction
(No_Floating_Point
, Def
);
2649 end Analyze_Formal_Floating_Type
;
2651 -----------------------------------
2652 -- Analyze_Formal_Interface_Type;--
2653 -----------------------------------
2655 procedure Analyze_Formal_Interface_Type
2660 Loc
: constant Source_Ptr
:= Sloc
(N
);
2665 Make_Full_Type_Declaration
(Loc
,
2666 Defining_Identifier
=> T
,
2667 Type_Definition
=> Def
);
2671 -- Keep the aspects from the original node
2673 Move_Aspects
(Original_Node
(N
), N
);
2676 Set_Is_Generic_Type
(T
);
2677 end Analyze_Formal_Interface_Type
;
2679 ---------------------------------
2680 -- Analyze_Formal_Modular_Type --
2681 ---------------------------------
2683 procedure Analyze_Formal_Modular_Type
(T
: Entity_Id
; Def
: Node_Id
) is
2685 -- Apart from their entity kind, generic modular types are treated like
2686 -- signed integer types, and have the same attributes.
2688 Analyze_Formal_Signed_Integer_Type
(T
, Def
);
2689 Mutate_Ekind
(T
, E_Modular_Integer_Subtype
);
2690 Mutate_Ekind
(Etype
(T
), E_Modular_Integer_Type
);
2692 end Analyze_Formal_Modular_Type
;
2694 ---------------------------------------
2695 -- Analyze_Formal_Object_Declaration --
2696 ---------------------------------------
2698 procedure Analyze_Formal_Object_Declaration
(N
: Node_Id
) is
2699 E
: constant Node_Id
:= Default_Expression
(N
);
2700 Id
: constant Node_Id
:= Defining_Identifier
(N
);
2703 Parent_Installed
: Boolean := False;
2709 Check_Abbreviated_Instance
(Parent
(N
), Parent_Installed
);
2711 -- Determine the mode of the formal object
2713 if Out_Present
(N
) then
2714 K
:= E_Generic_In_Out_Parameter
;
2716 if not In_Present
(N
) then
2717 Error_Msg_N
("formal generic objects cannot have mode OUT", N
);
2721 K
:= E_Generic_In_Parameter
;
2724 if Present
(Subtype_Mark
(N
)) then
2725 Find_Type
(Subtype_Mark
(N
));
2726 T
:= Entity
(Subtype_Mark
(N
));
2728 -- Verify that there is no redundant null exclusion
2730 if Null_Exclusion_Present
(N
) then
2731 if not Is_Access_Type
(T
) then
2733 ("null exclusion can only apply to an access type", N
);
2735 elsif Can_Never_Be_Null
(T
) then
2737 ("`NOT NULL` not allowed (& already excludes null)", N
, T
);
2741 -- Ada 2005 (AI-423): Formal object with an access definition
2744 Check_Access_Definition
(N
);
2745 T
:= Access_Definition
2747 N
=> Access_Definition
(N
));
2750 if Ekind
(T
) = E_Incomplete_Type
then
2752 Error_Node
: Node_Id
;
2755 if Present
(Subtype_Mark
(N
)) then
2756 Error_Node
:= Subtype_Mark
(N
);
2758 Check_Access_Definition
(N
);
2759 Error_Node
:= Access_Definition
(N
);
2762 Error_Msg_N
("premature usage of incomplete type", Error_Node
);
2766 if K
= E_Generic_In_Parameter
then
2768 -- Ada 2005 (AI-287): Limited aggregates allowed in generic formals
2770 if Ada_Version
< Ada_2005
and then Is_Limited_Type
(T
) then
2772 ("generic formal of mode IN must not be of limited type", N
);
2773 Explain_Limited_Type
(T
, N
);
2776 if Is_Abstract_Type
(T
) then
2778 ("generic formal of mode IN must not be of abstract type", N
);
2782 Preanalyze_Spec_Expression
(E
, T
);
2784 -- The default for a ghost generic formal IN parameter of
2785 -- access-to-variable type should be a ghost object (SPARK
2788 if Is_Access_Variable
(T
) then
2789 Check_Ghost_Formal_Variable
2792 Is_Default
=> True);
2795 if Is_Limited_Type
(T
) and then not OK_For_Limited_Init
(T
, E
) then
2797 ("initialization not allowed for limited types", E
);
2798 Explain_Limited_Type
(T
, E
);
2802 Mutate_Ekind
(Id
, K
);
2805 -- Case of generic IN OUT parameter
2808 -- If the formal has an unconstrained type, construct its actual
2809 -- subtype, as is done for subprogram formals. In this fashion, all
2810 -- its uses can refer to specific bounds.
2812 Mutate_Ekind
(Id
, K
);
2815 if (Is_Array_Type
(T
) and then not Is_Constrained
(T
))
2816 or else (Ekind
(T
) = E_Record_Type
and then Has_Discriminants
(T
))
2819 Non_Freezing_Ref
: constant Node_Id
:=
2820 New_Occurrence_Of
(Id
, Sloc
(Id
));
2824 -- Make sure the actual subtype doesn't generate bogus freezing
2826 Set_Must_Not_Freeze
(Non_Freezing_Ref
);
2827 Decl
:= Build_Actual_Subtype
(T
, Non_Freezing_Ref
);
2828 Insert_Before_And_Analyze
(N
, Decl
);
2829 Set_Actual_Subtype
(Id
, Defining_Identifier
(Decl
));
2832 Set_Actual_Subtype
(Id
, T
);
2837 ("initialization not allowed for `IN OUT` formals", N
);
2841 Analyze_Aspect_Specifications
(N
, Id
);
2843 if Parent_Installed
then
2846 end Analyze_Formal_Object_Declaration
;
2848 ----------------------------------------------
2849 -- Analyze_Formal_Ordinary_Fixed_Point_Type --
2850 ----------------------------------------------
2852 procedure Analyze_Formal_Ordinary_Fixed_Point_Type
2856 Loc
: constant Source_Ptr
:= Sloc
(Def
);
2857 Base
: constant Entity_Id
:=
2859 (E_Ordinary_Fixed_Point_Type
, Current_Scope
,
2860 Sloc
(Defining_Identifier
(Parent
(Def
))), 'G');
2863 -- The semantic attributes are set for completeness only, their values
2864 -- will never be used, since all properties of the type are non-static.
2867 Mutate_Ekind
(T
, E_Ordinary_Fixed_Point_Subtype
);
2868 Set_Etype
(T
, Base
);
2869 Set_Size_Info
(T
, Standard_Integer
);
2870 Set_RM_Size
(T
, RM_Size
(Standard_Integer
));
2871 Set_Small_Value
(T
, Ureal_1
);
2872 Set_Delta_Value
(T
, Ureal_1
);
2873 Set_Scalar_Range
(T
,
2875 Low_Bound
=> Make_Real_Literal
(Loc
, Ureal_1
),
2876 High_Bound
=> Make_Real_Literal
(Loc
, Ureal_1
)));
2877 Set_Is_Constrained
(T
);
2879 Set_Is_Generic_Type
(Base
);
2880 Set_Etype
(Base
, Base
);
2881 Set_Size_Info
(Base
, Standard_Integer
);
2882 Set_RM_Size
(Base
, RM_Size
(Standard_Integer
));
2883 Set_Small_Value
(Base
, Ureal_1
);
2884 Set_Delta_Value
(Base
, Ureal_1
);
2885 Set_Scalar_Range
(Base
, Scalar_Range
(T
));
2886 Set_Parent
(Base
, Parent
(Def
));
2888 Check_Restriction
(No_Fixed_Point
, Def
);
2889 end Analyze_Formal_Ordinary_Fixed_Point_Type
;
2891 ----------------------------------------
2892 -- Analyze_Formal_Package_Declaration --
2893 ----------------------------------------
2895 procedure Analyze_Formal_Package_Declaration
(N
: Node_Id
) is
2896 Gen_Id
: constant Node_Id
:= Name
(N
);
2897 Loc
: constant Source_Ptr
:= Sloc
(N
);
2898 Pack_Id
: constant Entity_Id
:= Defining_Identifier
(N
);
2901 Gen_Unit
: Entity_Id
;
2904 Vis_Prims_List
: Elist_Id
:= No_Elist
;
2905 -- List of primitives made temporarily visible in the instantiation
2906 -- to match the visibility of the formal type.
2908 function Build_Local_Package
return Node_Id
;
2909 -- The formal package is rewritten so that its parameters are replaced
2910 -- with corresponding declarations. For parameters with bona fide
2911 -- associations these declarations are created by Analyze_Associations
2912 -- as for a regular instantiation. For boxed parameters, we preserve
2913 -- the formal declarations and analyze them, in order to introduce
2914 -- entities of the right kind in the environment of the formal.
2916 -------------------------
2917 -- Build_Local_Package --
2918 -------------------------
2920 function Build_Local_Package
return Node_Id
is
2922 Pack_Decl
: Node_Id
;
2925 -- Within the formal, the name of the generic package is a renaming
2926 -- of the formal (as for a regular instantiation).
2929 Make_Package_Declaration
(Loc
,
2932 (Specification
(Original_Node
(Gen_Decl
)),
2933 Empty
, Instantiating
=> True));
2936 Make_Package_Renaming_Declaration
(Loc
,
2937 Defining_Unit_Name
=>
2938 Make_Defining_Identifier
(Loc
, Chars
(Gen_Unit
)),
2939 Name
=> New_Occurrence_Of
(Formal
, Loc
));
2941 if Nkind
(Gen_Id
) = N_Identifier
2942 and then Chars
(Gen_Id
) = Chars
(Pack_Id
)
2945 ("& is hidden within declaration of instance", Gen_Id
, Gen_Unit
);
2948 -- If the formal is declared with a box, or with an others choice,
2949 -- create corresponding declarations for all entities in the formal
2950 -- part, so that names with the proper types are available in the
2951 -- specification of the formal package.
2953 -- On the other hand, if there are no associations, then all the
2954 -- formals must have defaults, and this will be checked by the
2955 -- call to Analyze_Associations.
2958 or else Nkind
(First
(Generic_Associations
(N
))) = N_Others_Choice
2961 Formal_Decl
: Node_Id
;
2964 -- TBA : for a formal package, need to recurse ???
2969 (Generic_Formal_Declarations
(Original_Node
(Gen_Decl
)));
2970 while Present
(Formal_Decl
) loop
2974 (Formal_Decl
, Empty
, Instantiating
=> True));
2979 -- If generic associations are present, use Analyze_Associations to
2980 -- create the proper renaming declarations.
2984 Act_Tree
: constant Node_Id
:=
2986 (Original_Node
(Gen_Decl
), Empty
,
2987 Instantiating
=> True);
2990 Generic_Renamings
.Set_Last
(0);
2991 Generic_Renamings_HTable
.Reset
;
2992 Instantiation_Node
:= N
;
2995 Analyze_Associations
2996 (I_Node
=> Original_Node
(N
),
2997 Formals
=> Generic_Formal_Declarations
(Act_Tree
),
2998 F_Copy
=> Generic_Formal_Declarations
(Gen_Decl
));
3000 Vis_Prims_List
:= Check_Hidden_Primitives
(Decls
);
3004 Append
(Renaming
, To
=> Decls
);
3006 -- Add generated declarations ahead of local declarations in
3009 if No
(Visible_Declarations
(Specification
(Pack_Decl
))) then
3010 Set_Visible_Declarations
(Specification
(Pack_Decl
), Decls
);
3013 (First
(Visible_Declarations
(Specification
(Pack_Decl
))),
3018 end Build_Local_Package
;
3022 Save_ISMP
: constant Boolean := Ignore_SPARK_Mode_Pragmas_In_Instance
;
3023 -- Save flag Ignore_SPARK_Mode_Pragmas_In_Instance for restore on exit
3025 Associations
: Boolean := True;
3027 Parent_Installed
: Boolean := False;
3028 Parent_Instance
: Entity_Id
;
3029 Renaming_In_Par
: Entity_Id
;
3031 -- Start of processing for Analyze_Formal_Package_Declaration
3034 Check_Text_IO_Special_Unit
(Gen_Id
);
3037 Check_Generic_Child_Unit
(Gen_Id
, Parent_Installed
);
3038 Gen_Unit
:= Entity
(Gen_Id
);
3040 -- Check for a formal package that is a package renaming
3042 if Present
(Renamed_Entity
(Gen_Unit
)) then
3044 -- Indicate that unit is used, before replacing it with renamed
3045 -- entity for use below.
3047 if In_Extended_Main_Source_Unit
(N
) then
3048 Set_Is_Instantiated
(Gen_Unit
);
3049 Generate_Reference
(Gen_Unit
, N
);
3052 Gen_Unit
:= Renamed_Entity
(Gen_Unit
);
3055 if Ekind
(Gen_Unit
) /= E_Generic_Package
then
3056 Error_Msg_N
("expect generic package name", Gen_Id
);
3060 elsif Gen_Unit
= Current_Scope
then
3062 ("generic package cannot be used as a formal package of itself",
3067 elsif In_Open_Scopes
(Gen_Unit
) then
3068 if Is_Compilation_Unit
(Gen_Unit
)
3069 and then Is_Child_Unit
(Current_Scope
)
3071 -- Special-case the error when the formal is a parent, and
3072 -- continue analysis to minimize cascaded errors.
3075 ("generic parent cannot be used as formal package of a child "
3080 ("generic package cannot be used as a formal package within "
3081 & "itself", Gen_Id
);
3087 -- Check that name of formal package does not hide name of generic,
3088 -- or its leading prefix. This check must be done separately because
3089 -- the name of the generic has already been analyzed.
3092 Gen_Name
: Entity_Id
;
3096 while Nkind
(Gen_Name
) = N_Expanded_Name
loop
3097 Gen_Name
:= Prefix
(Gen_Name
);
3100 if Chars
(Gen_Name
) = Chars
(Pack_Id
) then
3102 ("& is hidden within declaration of formal package",
3108 or else No
(Generic_Associations
(N
))
3109 or else Nkind
(First
(Generic_Associations
(N
))) = N_Others_Choice
3111 Associations
:= False;
3114 -- If there are no generic associations, the generic parameters appear
3115 -- as local entities and are instantiated like them. We copy the generic
3116 -- package declaration as if it were an instantiation, and analyze it
3117 -- like a regular package, except that we treat the formals as
3118 -- additional visible components.
3120 Gen_Decl
:= Unit_Declaration_Node
(Gen_Unit
);
3122 if In_Extended_Main_Source_Unit
(N
) then
3123 Set_Is_Instantiated
(Gen_Unit
);
3124 Generate_Reference
(Gen_Unit
, N
);
3127 Formal
:= New_Copy
(Pack_Id
);
3128 Create_Instantiation_Source
(N
, Gen_Unit
, S_Adjustment
);
3130 -- Make local generic without formals. The formals will be replaced with
3131 -- internal declarations.
3134 New_N
:= Build_Local_Package
;
3136 -- If there are errors in the parameter list, Analyze_Associations
3137 -- raises Instantiation_Error. Patch the declaration to prevent further
3138 -- exception propagation.
3141 when Instantiation_Error
=>
3142 Enter_Name
(Formal
);
3143 Mutate_Ekind
(Formal
, E_Variable
);
3144 Set_Etype
(Formal
, Any_Type
);
3145 Restore_Hidden_Primitives
(Vis_Prims_List
);
3147 if Parent_Installed
then
3155 Set_Defining_Unit_Name
(Specification
(New_N
), Formal
);
3156 Set_Generic_Parent
(Specification
(N
), Gen_Unit
);
3157 Set_Instance_Env
(Gen_Unit
, Formal
);
3158 Set_Is_Generic_Instance
(Formal
);
3160 Enter_Name
(Formal
);
3161 Mutate_Ekind
(Formal
, E_Package
);
3162 Set_Etype
(Formal
, Standard_Void_Type
);
3163 Set_Inner_Instances
(Formal
, New_Elmt_List
);
3165 -- It is unclear that any aspects can apply to a formal package
3166 -- declaration, given that they look like a hidden conformance
3167 -- requirement on the corresponding actual. However, Abstract_State
3168 -- must be treated specially because it generates declarations that
3169 -- must appear before other declarations in the specification and
3170 -- must be analyzed at once.
3172 if Present
(Aspect_Specifications
(Gen_Decl
)) then
3173 if No
(Aspect_Specifications
(N
)) then
3174 Set_Aspect_Specifications
(N
, New_List
);
3178 ASN
: Node_Id
:= First
(Aspect_Specifications
(Gen_Decl
));
3182 while Present
(ASN
) loop
3183 if Get_Aspect_Id
(ASN
) = Aspect_Abstract_State
then
3185 Copy_Generic_Node
(ASN
, Empty
, Instantiating
=> True);
3186 Set_Entity
(New_A
, Formal
);
3187 Set_Analyzed
(New_A
, False);
3188 Append
(New_A
, Aspect_Specifications
(N
));
3189 Analyze_Aspect_Specifications
(N
, Formal
);
3198 Push_Scope
(Formal
);
3200 -- Manually set the SPARK_Mode from the context because the package
3201 -- declaration is never analyzed.
3203 Set_SPARK_Pragma
(Formal
, SPARK_Mode_Pragma
);
3204 Set_SPARK_Aux_Pragma
(Formal
, SPARK_Mode_Pragma
);
3205 Set_SPARK_Pragma_Inherited
(Formal
);
3206 Set_SPARK_Aux_Pragma_Inherited
(Formal
);
3208 if Is_Child_Unit
(Gen_Unit
) and then Parent_Installed
then
3210 -- Similarly, we have to make the name of the formal visible in the
3211 -- parent instance, to resolve properly fully qualified names that
3212 -- may appear in the generic unit. The parent instance has been
3213 -- placed on the scope stack ahead of the current scope.
3215 Parent_Instance
:= Scope_Stack
.Table
(Scope_Stack
.Last
- 1).Entity
;
3218 Make_Defining_Identifier
(Loc
, Chars
(Gen_Unit
));
3219 Mutate_Ekind
(Renaming_In_Par
, E_Package
);
3220 Set_Is_Not_Self_Hidden
(Renaming_In_Par
);
3221 Set_Etype
(Renaming_In_Par
, Standard_Void_Type
);
3222 Set_Scope
(Renaming_In_Par
, Parent_Instance
);
3223 Set_Parent
(Renaming_In_Par
, Parent
(Formal
));
3224 Set_Renamed_Entity
(Renaming_In_Par
, Formal
);
3225 Append_Entity
(Renaming_In_Par
, Parent_Instance
);
3228 -- A formal package declaration behaves as a package instantiation with
3229 -- respect to SPARK_Mode "off". If the annotation is "off" or altogether
3230 -- missing, set the global flag which signals Analyze_Pragma to ingnore
3231 -- all SPARK_Mode pragmas within the generic_package_name.
3233 if SPARK_Mode
/= On
then
3234 Ignore_SPARK_Mode_Pragmas_In_Instance
:= True;
3236 -- Mark the formal spec in case the body is instantiated at a later
3237 -- pass. This preserves the original context in effect for the body.
3239 Set_Ignore_SPARK_Mode_Pragmas
(Formal
);
3242 Analyze
(Specification
(N
));
3244 -- The formals for which associations are provided are not visible
3245 -- outside of the formal package. The others are still declared by a
3246 -- formal parameter declaration.
3248 -- If there are no associations, the only local entity to hide is the
3249 -- generated package renaming itself.
3255 E
:= First_Entity
(Formal
);
3256 while Present
(E
) loop
3257 if Associations
and then not Is_Generic_Formal
(E
) then
3261 if Ekind
(E
) = E_Package
and then Renamed_Entity
(E
) = Formal
then
3270 End_Package_Scope
(Formal
);
3271 Restore_Hidden_Primitives
(Vis_Prims_List
);
3273 if Parent_Installed
then
3279 -- Inside the generic unit, the formal package is a regular package, but
3280 -- no body is needed for it. Note that after instantiation, the defining
3281 -- unit name we need is in the new tree and not in the original (see
3282 -- Package_Instantiation). A generic formal package is an instance, and
3283 -- can be used as an actual for an inner instance.
3285 Set_Has_Completion
(Formal
, True);
3287 -- Add semantic information to the original defining identifier.
3289 Mutate_Ekind
(Pack_Id
, E_Package
);
3290 Set_Etype
(Pack_Id
, Standard_Void_Type
);
3291 Set_Scope
(Pack_Id
, Scope
(Formal
));
3292 Set_Has_Completion
(Pack_Id
, True);
3295 -- Unclear that any other aspects may appear here, analyze them
3296 -- for completion, given that the grammar allows their appearance.
3298 Analyze_Aspect_Specifications
(N
, Pack_Id
);
3300 Ignore_SPARK_Mode_Pragmas_In_Instance
:= Save_ISMP
;
3301 end Analyze_Formal_Package_Declaration
;
3303 ---------------------------------
3304 -- Analyze_Formal_Private_Type --
3305 ---------------------------------
3307 procedure Analyze_Formal_Private_Type
3313 New_Private_Type
(N
, T
, Def
);
3315 -- Set the size to an arbitrary but legal value
3317 Set_Size_Info
(T
, Standard_Integer
);
3318 Set_RM_Size
(T
, RM_Size
(Standard_Integer
));
3319 end Analyze_Formal_Private_Type
;
3321 ------------------------------------
3322 -- Analyze_Formal_Incomplete_Type --
3323 ------------------------------------
3325 procedure Analyze_Formal_Incomplete_Type
3331 Mutate_Ekind
(T
, E_Incomplete_Type
);
3333 Set_Private_Dependents
(T
, New_Elmt_List
);
3335 if Tagged_Present
(Def
) then
3336 Set_Is_Tagged_Type
(T
);
3337 Make_Class_Wide_Type
(T
);
3338 Set_Direct_Primitive_Operations
(T
, New_Elmt_List
);
3340 end Analyze_Formal_Incomplete_Type
;
3342 ----------------------------------------
3343 -- Analyze_Formal_Signed_Integer_Type --
3344 ----------------------------------------
3346 procedure Analyze_Formal_Signed_Integer_Type
3350 Base
: constant Entity_Id
:=
3352 (E_Signed_Integer_Type
,
3354 Sloc
(Defining_Identifier
(Parent
(Def
))), 'G');
3359 Mutate_Ekind
(T
, E_Signed_Integer_Subtype
);
3360 Set_Etype
(T
, Base
);
3361 Set_Size_Info
(T
, Standard_Integer
);
3362 Set_RM_Size
(T
, RM_Size
(Standard_Integer
));
3363 Set_Scalar_Range
(T
, Scalar_Range
(Standard_Integer
));
3364 Set_Is_Constrained
(T
);
3366 Set_Is_Generic_Type
(Base
);
3367 Set_Size_Info
(Base
, Standard_Integer
);
3368 Set_RM_Size
(Base
, RM_Size
(Standard_Integer
));
3369 Set_Etype
(Base
, Base
);
3370 Set_Scalar_Range
(Base
, Scalar_Range
(Standard_Integer
));
3371 Set_Parent
(Base
, Parent
(Def
));
3372 end Analyze_Formal_Signed_Integer_Type
;
3374 -------------------------------------------
3375 -- Analyze_Formal_Subprogram_Declaration --
3376 -------------------------------------------
3378 procedure Analyze_Formal_Subprogram_Declaration
(N
: Node_Id
) is
3379 Spec
: constant Node_Id
:= Specification
(N
);
3380 Def
: constant Node_Id
:= Default_Name
(N
);
3381 Expr
: constant Node_Id
:= Expression
(N
);
3382 Nam
: constant Entity_Id
:= Defining_Unit_Name
(Spec
);
3384 Parent_Installed
: Boolean := False;
3392 if Nkind
(Nam
) = N_Defining_Program_Unit_Name
then
3393 Error_Msg_N
("name of formal subprogram must be a direct name", Nam
);
3397 Check_Abbreviated_Instance
(Parent
(N
), Parent_Installed
);
3399 Analyze_Subprogram_Declaration
(N
);
3400 Set_Is_Formal_Subprogram
(Nam
);
3401 Set_Has_Completion
(Nam
);
3403 if Nkind
(N
) = N_Formal_Abstract_Subprogram_Declaration
then
3404 Set_Is_Abstract_Subprogram
(Nam
);
3406 Set_Is_Dispatching_Operation
(Nam
);
3408 -- A formal abstract procedure cannot have a null default
3409 -- (RM 12.6(4.1/2)).
3411 if Nkind
(Spec
) = N_Procedure_Specification
3412 and then Null_Present
(Spec
)
3415 ("a formal abstract subprogram cannot default to null", Spec
);
3418 -- A formal abstract function cannot have an expression default
3419 -- (expression defaults are allowed for nonabstract formal functions
3420 -- when extensions are enabled).
3422 if Nkind
(Spec
) = N_Function_Specification
3423 and then Present
(Expr
)
3426 ("a formal abstract subprogram cannot default to an expression",
3431 Ctrl_Type
: constant Entity_Id
:= Find_Dispatching_Type
(Nam
);
3433 if No
(Ctrl_Type
) then
3435 ("abstract formal subprogram must have a controlling type",
3438 elsif Ada_Version
>= Ada_2012
3439 and then Is_Incomplete_Type
(Ctrl_Type
)
3442 ("controlling type of abstract formal subprogram cannot "
3443 & "be incomplete type", N
, Ctrl_Type
);
3446 Check_Controlling_Formals
(Ctrl_Type
, Nam
);
3451 -- Default name is resolved at the point of instantiation
3453 if Box_Present
(N
) then
3456 -- Default name is bound at the point of generic declaration
3458 elsif Present
(Def
) then
3459 if Nkind
(Def
) = N_Operator_Symbol
then
3460 Find_Direct_Name
(Def
);
3462 elsif Nkind
(Def
) /= N_Attribute_Reference
then
3466 -- For an attribute reference, analyze the prefix and verify
3467 -- that it has the proper profile for the subprogram.
3469 Analyze
(Prefix
(Def
));
3470 Valid_Default_Attribute
(Nam
, Def
);
3474 -- The default for a ghost generic formal procedure should be a ghost
3475 -- procedure (SPARK RM 6.9(13)).
3477 if Ekind
(Nam
) = E_Procedure
then
3479 Def_E
: Entity_Id
:= Empty
;
3481 if Nkind
(Def
) in N_Has_Entity
then
3482 Def_E
:= Entity
(Def
);
3485 Check_Ghost_Formal_Procedure_Or_Package
3489 Is_Default
=> True);
3493 -- Default name may be overloaded, in which case the interpretation
3494 -- with the correct profile must be selected, as for a renaming.
3495 -- If the definition is an indexed component, it must denote a
3496 -- member of an entry family. If it is a selected component, it
3497 -- can be a protected operation.
3499 if Etype
(Def
) = Any_Type
then
3502 elsif Nkind
(Def
) = N_Selected_Component
then
3503 if not Is_Overloadable
(Entity
(Selector_Name
(Def
))) then
3504 Error_Msg_N
("expect valid subprogram name as default", Def
);
3507 elsif Nkind
(Def
) = N_Indexed_Component
then
3508 if Is_Entity_Name
(Prefix
(Def
)) then
3509 if Ekind
(Entity
(Prefix
(Def
))) /= E_Entry_Family
then
3510 Error_Msg_N
("expect valid subprogram name as default", Def
);
3513 elsif Nkind
(Prefix
(Def
)) = N_Selected_Component
then
3514 if Ekind
(Entity
(Selector_Name
(Prefix
(Def
)))) /=
3517 Error_Msg_N
("expect valid subprogram name as default", Def
);
3521 Error_Msg_N
("expect valid subprogram name as default", Def
);
3525 elsif Nkind
(Def
) = N_Character_Literal
then
3527 -- Needs some type checks: subprogram should be parameterless???
3529 Resolve
(Def
, (Etype
(Nam
)));
3531 elsif not Is_Entity_Name
(Def
)
3532 or else not Is_Overloadable
(Entity
(Def
))
3534 Error_Msg_N
("expect valid subprogram name as default", Def
);
3537 elsif not Is_Overloaded
(Def
) then
3538 Subp
:= Entity
(Def
);
3541 Error_Msg_N
("premature usage of formal subprogram", Def
);
3543 elsif not Entity_Matches_Spec
(Subp
, Nam
) then
3544 Error_Msg_N
("no visible entity matches specification", Def
);
3547 -- More than one interpretation, so disambiguate as for a renaming
3552 I1
: Interp_Index
:= 0;
3558 Get_First_Interp
(Def
, I
, It
);
3559 while Present
(It
.Nam
) loop
3560 if Entity_Matches_Spec
(It
.Nam
, Nam
) then
3561 if Subp
/= Any_Id
then
3562 It1
:= Disambiguate
(Def
, I1
, I
, Etype
(Subp
));
3564 if It1
= No_Interp
then
3565 Error_Msg_N
("ambiguous default subprogram", Def
);
3578 Get_Next_Interp
(I
, It
);
3582 if Subp
/= Any_Id
then
3584 -- Subprogram found, generate reference to it
3586 Set_Entity
(Def
, Subp
);
3587 Generate_Reference
(Subp
, Def
);
3590 Error_Msg_N
("premature usage of formal subprogram", Def
);
3592 elsif Ekind
(Subp
) /= E_Operator
then
3593 Check_Mode_Conformant
(Subp
, Nam
);
3597 Error_Msg_N
("no visible subprogram matches specification", N
);
3601 -- When extensions are enabled, an expression can be given as default
3602 -- for a formal function. The expression must be of the function result
3603 -- type and can reference formal parameters of the function.
3605 elsif Present
(Expr
) then
3607 Install_Formals
(Nam
);
3608 Preanalyze_Spec_Expression
(Expr
, Etype
(Nam
));
3613 Analyze_Aspect_Specifications
(N
, Nam
);
3615 if Parent_Installed
then
3618 end Analyze_Formal_Subprogram_Declaration
;
3620 -------------------------------------
3621 -- Analyze_Formal_Type_Declaration --
3622 -------------------------------------
3624 procedure Analyze_Formal_Type_Declaration
(N
: Node_Id
) is
3625 Def
: constant Node_Id
:= Formal_Type_Definition
(N
);
3627 Parent_Installed
: Boolean := False;
3631 T
:= Defining_Identifier
(N
);
3633 if Present
(Discriminant_Specifications
(N
))
3634 and then Nkind
(Def
) /= N_Formal_Private_Type_Definition
3637 ("discriminants not allowed for this formal type", T
);
3640 Check_Abbreviated_Instance
(Parent
(N
), Parent_Installed
);
3642 -- Enter the new name, and branch to specific routine
3645 when N_Formal_Private_Type_Definition
=>
3646 Analyze_Formal_Private_Type
(N
, T
, Def
);
3648 when N_Formal_Derived_Type_Definition
=>
3649 Analyze_Formal_Derived_Type
(N
, T
, Def
);
3651 when N_Formal_Incomplete_Type_Definition
=>
3652 Analyze_Formal_Incomplete_Type
(T
, Def
);
3654 when N_Formal_Discrete_Type_Definition
=>
3655 Analyze_Formal_Discrete_Type
(T
, Def
);
3657 when N_Formal_Signed_Integer_Type_Definition
=>
3658 Analyze_Formal_Signed_Integer_Type
(T
, Def
);
3660 when N_Formal_Modular_Type_Definition
=>
3661 Analyze_Formal_Modular_Type
(T
, Def
);
3663 when N_Formal_Floating_Point_Definition
=>
3664 Analyze_Formal_Floating_Type
(T
, Def
);
3666 when N_Formal_Ordinary_Fixed_Point_Definition
=>
3667 Analyze_Formal_Ordinary_Fixed_Point_Type
(T
, Def
);
3669 when N_Formal_Decimal_Fixed_Point_Definition
=>
3670 Analyze_Formal_Decimal_Fixed_Point_Type
(T
, Def
);
3672 when N_Array_Type_Definition
=>
3673 Analyze_Formal_Array_Type
(T
, Def
);
3675 when N_Access_Function_Definition
3676 | N_Access_Procedure_Definition
3677 | N_Access_To_Object_Definition
3679 Analyze_Generic_Access_Type
(T
, Def
);
3681 -- Ada 2005: a interface declaration is encoded as an abstract
3682 -- record declaration or a abstract type derivation.
3684 when N_Record_Definition
=>
3685 Analyze_Formal_Interface_Type
(N
, T
, Def
);
3687 when N_Derived_Type_Definition
=>
3688 Analyze_Formal_Derived_Interface_Type
(N
, T
, Def
);
3694 raise Program_Error
;
3697 -- A formal type declaration declares a type and its first
3700 Set_Is_Generic_Type
(T
);
3701 Set_Is_First_Subtype
(T
);
3703 if Present
(Default_Subtype_Mark
(Original_Node
(N
))) then
3704 Validate_Formal_Type_Default
(N
);
3707 Analyze_Aspect_Specifications
(N
, T
);
3709 if Parent_Installed
then
3712 end Analyze_Formal_Type_Declaration
;
3714 ------------------------------------
3715 -- Analyze_Function_Instantiation --
3716 ------------------------------------
3718 procedure Analyze_Function_Instantiation
(N
: Node_Id
) is
3720 Analyze_Subprogram_Instantiation
(N
, E_Function
);
3721 end Analyze_Function_Instantiation
;
3723 ---------------------------------
3724 -- Analyze_Generic_Access_Type --
3725 ---------------------------------
3727 procedure Analyze_Generic_Access_Type
(T
: Entity_Id
; Def
: Node_Id
) is
3731 if Nkind
(Def
) = N_Access_To_Object_Definition
then
3732 Access_Type_Declaration
(T
, Def
);
3734 if Is_Incomplete_Or_Private_Type
(Designated_Type
(T
))
3735 and then No
(Full_View
(Designated_Type
(T
)))
3736 and then not Is_Generic_Type
(Designated_Type
(T
))
3738 Error_Msg_N
("premature usage of incomplete type", Def
);
3740 elsif not Is_Entity_Name
(Subtype_Indication
(Def
)) then
3742 ("only a subtype mark is allowed in a formal", Def
);
3746 Access_Subprogram_Declaration
(T
, Def
);
3748 end Analyze_Generic_Access_Type
;
3750 ---------------------------------
3751 -- Analyze_Generic_Formal_Part --
3752 ---------------------------------
3754 procedure Analyze_Generic_Formal_Part
(N
: Node_Id
) is
3755 Gen_Parm_Decl
: Node_Id
;
3758 -- The generic formals are processed in the scope of the generic unit,
3759 -- where they are immediately visible. The scope is installed by the
3762 Gen_Parm_Decl
:= First
(Generic_Formal_Declarations
(N
));
3763 while Present
(Gen_Parm_Decl
) loop
3764 Analyze
(Gen_Parm_Decl
);
3765 Next
(Gen_Parm_Decl
);
3768 Generate_Reference_To_Generic_Formals
(Current_Scope
);
3770 -- For Ada 2022, some formal parameters can carry aspects, which must
3771 -- be name-resolved at the end of the list of formal parameters (which
3772 -- has the semantics of a declaration list).
3774 Analyze_Contracts
(Generic_Formal_Declarations
(N
));
3775 end Analyze_Generic_Formal_Part
;
3777 ------------------------------------------
3778 -- Analyze_Generic_Package_Declaration --
3779 ------------------------------------------
3781 procedure Analyze_Generic_Package_Declaration
(N
: Node_Id
) is
3782 Decls
: constant List_Id
:= Visible_Declarations
(Specification
(N
));
3783 Loc
: constant Source_Ptr
:= Sloc
(N
);
3789 Save_Parent
: Node_Id
;
3792 -- A generic may grant access to its private enclosing context depending
3793 -- on the placement of its corresponding body. From elaboration point of
3794 -- view, the flow of execution may enter this private context, and then
3795 -- reach an external unit, thus producing a dependency on that external
3796 -- unit. For such a path to be properly discovered and encoded in the
3797 -- ALI file of the main unit, let the ABE mechanism process the body of
3798 -- the main unit, and encode all relevant invocation constructs and the
3799 -- relations between them.
3801 Mark_Save_Invocation_Graph_Of_Body
;
3803 -- We introduce a renaming of the enclosing package, to have a usable
3804 -- entity as the prefix of an expanded name for a local entity of the
3805 -- form Par.P.Q, where P is the generic package. This is because a local
3806 -- entity named P may hide it, so that the usual visibility rules in
3807 -- the instance will not resolve properly.
3810 Make_Package_Renaming_Declaration
(Loc
,
3811 Defining_Unit_Name
=>
3812 Make_Defining_Identifier
(Loc
,
3813 Chars
=> New_External_Name
(Chars
(Defining_Entity
(N
)), "GH")),
3815 Make_Identifier
(Loc
, Chars
(Defining_Entity
(N
))));
3817 -- The declaration is inserted before other declarations, but before
3818 -- pragmas that may be library-unit pragmas and must appear before other
3819 -- declarations. The pragma Compile_Time_Error is not in this class, and
3820 -- may contain an expression that includes such a qualified name, so the
3821 -- renaming declaration must appear before it.
3823 -- Are there other pragmas that require this special handling ???
3825 if Present
(Decls
) then
3826 Decl
:= First
(Decls
);
3827 while Present
(Decl
)
3828 and then Nkind
(Decl
) = N_Pragma
3829 and then Get_Pragma_Id
(Decl
) /= Pragma_Compile_Time_Error
3834 if Present
(Decl
) then
3835 Insert_Before
(Decl
, Renaming
);
3837 Append
(Renaming
, Visible_Declarations
(Specification
(N
)));
3841 Set_Visible_Declarations
(Specification
(N
), New_List
(Renaming
));
3844 -- Create copy of generic unit, and save for instantiation. If the unit
3845 -- is a child unit, do not copy the specifications for the parent, which
3846 -- are not part of the generic tree.
3848 Save_Parent
:= Parent_Spec
(N
);
3849 Set_Parent_Spec
(N
, Empty
);
3851 New_N
:= Copy_Generic_Node
(N
, Empty
, Instantiating
=> False);
3852 Set_Parent_Spec
(New_N
, Save_Parent
);
3855 -- Collect all contract-related source pragmas found within the template
3856 -- and attach them to the contract of the package spec. This contract is
3857 -- used in the capture of global references within annotations.
3859 Create_Generic_Contract
(N
);
3861 Id
:= Defining_Entity
(N
);
3862 Generate_Definition
(Id
);
3864 -- Expansion is not applied to generic units
3869 Mutate_Ekind
(Id
, E_Generic_Package
);
3870 Set_Is_Not_Self_Hidden
(Id
);
3871 Set_Etype
(Id
, Standard_Void_Type
);
3873 -- Set SPARK_Mode from context
3875 Set_SPARK_Pragma
(Id
, SPARK_Mode_Pragma
);
3876 Set_SPARK_Aux_Pragma
(Id
, SPARK_Mode_Pragma
);
3877 Set_SPARK_Pragma_Inherited
(Id
);
3878 Set_SPARK_Aux_Pragma_Inherited
(Id
);
3880 -- Preserve relevant elaboration-related attributes of the context which
3881 -- are no longer available or very expensive to recompute once analysis,
3882 -- resolution, and expansion are over.
3884 Mark_Elaboration_Attributes
3889 -- Analyze aspects now, so that generated pragmas appear in the
3890 -- declarations before building and analyzing the generic copy.
3892 Analyze_Aspect_Specifications
(N
, Id
);
3895 Enter_Generic_Scope
(Id
);
3896 Set_Inner_Instances
(Id
, New_Elmt_List
);
3898 Set_Categorization_From_Pragmas
(N
);
3899 Set_Is_Pure
(Id
, Is_Pure
(Current_Scope
));
3901 -- Link the declaration of the generic homonym in the generic copy to
3902 -- the package it renames, so that it is always resolved properly.
3904 Set_Generic_Homonym
(Id
, Defining_Unit_Name
(Renaming
));
3905 Set_Entity
(Associated_Node
(Name
(Renaming
)), Id
);
3907 -- For a library unit, we have reconstructed the entity for the unit,
3908 -- and must reset it in the library tables.
3910 if Nkind
(Parent
(N
)) = N_Compilation_Unit
then
3911 Set_Cunit_Entity
(Current_Sem_Unit
, Id
);
3914 Analyze_Generic_Formal_Part
(N
);
3916 -- After processing the generic formals, analysis proceeds as for a
3917 -- non-generic package.
3919 Analyze
(Specification
(N
));
3921 Validate_Categorization_Dependency
(N
, Id
);
3925 End_Package_Scope
(Id
);
3926 Exit_Generic_Scope
(Id
);
3928 -- If the generic appears within a package unit, the body of that unit
3929 -- has to be present for instantiation and inlining.
3931 if Nkind
(Unit
(Cunit
(Current_Sem_Unit
))) = N_Package_Declaration
then
3932 Set_Body_Needed_For_Inlining
3933 (Defining_Entity
(Unit
(Cunit
(Current_Sem_Unit
))));
3936 if Nkind
(Parent
(N
)) /= N_Compilation_Unit
then
3937 Move_Freeze_Nodes
(Id
, N
, Visible_Declarations
(Specification
(N
)));
3938 Move_Freeze_Nodes
(Id
, N
, Private_Declarations
(Specification
(N
)));
3939 Move_Freeze_Nodes
(Id
, N
, Generic_Formal_Declarations
(N
));
3942 Set_Body_Required
(Parent
(N
), Unit_Requires_Body
(Id
));
3943 Validate_RT_RAT_Component
(N
);
3945 -- If this is a spec without a body, check that generic parameters
3948 if not Body_Required
(Parent
(N
)) then
3949 Check_References
(Id
);
3953 -- If there is a specified storage pool in the context, create an
3954 -- aspect on the package declaration, so that it is used in any
3955 -- instance that does not override it.
3957 if Present
(Default_Pool
) then
3963 Make_Aspect_Specification
(Loc
,
3964 Identifier
=> Make_Identifier
(Loc
, Name_Default_Storage_Pool
),
3965 Expression
=> New_Copy
(Default_Pool
));
3967 if No
(Aspect_Specifications
(Specification
(N
))) then
3968 Set_Aspect_Specifications
(Specification
(N
), New_List
(ASN
));
3970 Append
(ASN
, Aspect_Specifications
(Specification
(N
)));
3974 end Analyze_Generic_Package_Declaration
;
3976 --------------------------------------------
3977 -- Analyze_Generic_Subprogram_Declaration --
3978 --------------------------------------------
3980 procedure Analyze_Generic_Subprogram_Declaration
(N
: Node_Id
) is
3984 Result_Type
: Entity_Id
;
3985 Save_Parent
: Node_Id
;
3990 -- A generic may grant access to its private enclosing context depending
3991 -- on the placement of its corresponding body. From elaboration point of
3992 -- view, the flow of execution may enter this private context, and then
3993 -- reach an external unit, thus producing a dependency on that external
3994 -- unit. For such a path to be properly discovered and encoded in the
3995 -- ALI file of the main unit, let the ABE mechanism process the body of
3996 -- the main unit, and encode all relevant invocation constructs and the
3997 -- relations between them.
3999 Mark_Save_Invocation_Graph_Of_Body
;
4001 -- Create copy of generic unit, and save for instantiation. If the unit
4002 -- is a child unit, do not copy the specifications for the parent, which
4003 -- are not part of the generic tree.
4005 Save_Parent
:= Parent_Spec
(N
);
4006 Set_Parent_Spec
(N
, Empty
);
4008 New_N
:= Copy_Generic_Node
(N
, Empty
, Instantiating
=> False);
4009 Set_Parent_Spec
(New_N
, Save_Parent
);
4012 -- Collect all contract-related source pragmas found within the template
4013 -- and attach them to the contract of the subprogram spec. This contract
4014 -- is used in the capture of global references within annotations.
4016 Create_Generic_Contract
(N
);
4018 Spec
:= Specification
(N
);
4019 Id
:= Defining_Entity
(Spec
);
4020 Generate_Definition
(Id
);
4022 if Nkind
(Id
) = N_Defining_Operator_Symbol
then
4024 ("operator symbol not allowed for generic subprogram", Id
);
4030 Set_Scope_Depth_Value
(Id
, Scope_Depth
(Current_Scope
) + 1);
4033 Enter_Generic_Scope
(Id
);
4034 Set_Inner_Instances
(Id
, New_Elmt_List
);
4035 Set_Is_Pure
(Id
, Is_Pure
(Current_Scope
));
4037 Analyze_Generic_Formal_Part
(N
);
4039 if Nkind
(Spec
) = N_Function_Specification
then
4040 Mutate_Ekind
(Id
, E_Generic_Function
);
4042 Mutate_Ekind
(Id
, E_Generic_Procedure
);
4045 -- Set SPARK_Mode from context
4047 Set_SPARK_Pragma
(Id
, SPARK_Mode_Pragma
);
4048 Set_SPARK_Pragma_Inherited
(Id
);
4050 -- Preserve relevant elaboration-related attributes of the context which
4051 -- are no longer available or very expensive to recompute once analysis,
4052 -- resolution, and expansion are over.
4054 Mark_Elaboration_Attributes
4059 Formals
:= Parameter_Specifications
(Spec
);
4061 if Present
(Formals
) then
4062 Process_Formals
(Formals
, Spec
);
4065 if Nkind
(Spec
) = N_Function_Specification
then
4066 if Nkind
(Result_Definition
(Spec
)) = N_Access_Definition
then
4067 Result_Type
:= Access_Definition
(Spec
, Result_Definition
(Spec
));
4068 Set_Etype
(Id
, Result_Type
);
4070 -- Check restriction imposed by AI05-073: a generic function
4071 -- cannot return an abstract type or an access to such.
4073 if Is_Abstract_Type
(Designated_Type
(Result_Type
)) then
4075 ("generic function cannot have an access result "
4076 & "that designates an abstract type", Spec
);
4080 Find_Type
(Result_Definition
(Spec
));
4081 Typ
:= Entity
(Result_Definition
(Spec
));
4083 if Is_Abstract_Type
(Typ
)
4084 and then Ada_Version
>= Ada_2012
4087 ("generic function cannot have abstract result type", Spec
);
4090 -- If a null exclusion is imposed on the result type, then create
4091 -- a null-excluding itype (an access subtype) and use it as the
4092 -- function's Etype.
4094 if Is_Access_Type
(Typ
)
4095 and then Null_Exclusion_Present
(Spec
)
4098 Create_Null_Excluding_Itype
4100 Related_Nod
=> Spec
,
4101 Scope_Id
=> Defining_Unit_Name
(Spec
)));
4103 Set_Etype
(Id
, Typ
);
4108 Set_Etype
(Id
, Standard_Void_Type
);
4111 Set_Is_Not_Self_Hidden
(Id
);
4113 -- Analyze the aspects of the generic copy to ensure that all generated
4114 -- pragmas (if any) perform their semantic effects.
4116 Analyze_Aspect_Specifications
(N
, Id
);
4118 -- For a library unit, we have reconstructed the entity for the unit,
4119 -- and must reset it in the library tables. We also make sure that
4120 -- Body_Required is set properly in the original compilation unit node.
4122 if Nkind
(Parent
(N
)) = N_Compilation_Unit
then
4123 Set_Cunit_Entity
(Current_Sem_Unit
, Id
);
4124 Set_Body_Required
(Parent
(N
), Unit_Requires_Body
(Id
));
4127 -- If the generic appears within a package unit, the body of that unit
4128 -- has to be present for instantiation and inlining.
4130 if Nkind
(Unit
(Cunit
(Current_Sem_Unit
))) = N_Package_Declaration
4131 and then Unit_Requires_Body
(Id
)
4133 Set_Body_Needed_For_Inlining
4134 (Defining_Entity
(Unit
(Cunit
(Current_Sem_Unit
))));
4137 Set_Categorization_From_Pragmas
(N
);
4138 Validate_Categorization_Dependency
(N
, Id
);
4140 -- Capture all global references that occur within the profile of the
4141 -- generic subprogram. Aspects are not part of this processing because
4142 -- they must be delayed. If processed now, Save_Global_References will
4143 -- destroy the Associated_Node links and prevent the capture of global
4144 -- references when the contract of the generic subprogram is analyzed.
4146 Save_Global_References
(Original_Node
(N
));
4150 Exit_Generic_Scope
(Id
);
4151 Generate_Reference_To_Formals
(Id
);
4153 List_Inherited_Pre_Post_Aspects
(Id
);
4154 end Analyze_Generic_Subprogram_Declaration
;
4156 -----------------------------------
4157 -- Analyze_Package_Instantiation --
4158 -----------------------------------
4160 -- WARNING: This routine manages Ghost and SPARK regions. Return statements
4161 -- must be replaced by gotos which jump to the end of the routine in order
4162 -- to restore the Ghost and SPARK modes.
4164 procedure Analyze_Package_Instantiation
(N
: Node_Id
) is
4165 Has_Inline_Always
: Boolean := False;
4166 -- Set if the generic unit contains any subprograms with Inline_Always.
4167 -- Only relevant when back-end inlining is not enabled.
4169 function Might_Inline_Subp
(Gen_Unit
: Entity_Id
) return Boolean;
4170 -- Return True if inlining is active and Gen_Unit contains inlined
4171 -- subprograms. In this case, we may either instantiate the body when
4172 -- front-end inlining is enabled, or add a pending instantiation when
4173 -- back-end inlining is enabled. In the former case, this may cause
4174 -- superfluous instantiations, but in either case we need to perform
4175 -- the instantiation of the body in the context of the instance and
4176 -- not in that of the point of inlining.
4178 function Needs_Body_Instantiated
(Gen_Unit
: Entity_Id
) return Boolean;
4179 -- Return True if Gen_Unit needs to have its body instantiated in the
4180 -- context of N. This in particular excludes generic contexts.
4182 -----------------------
4183 -- Might_Inline_Subp --
4184 -----------------------
4186 function Might_Inline_Subp
(Gen_Unit
: Entity_Id
) return Boolean is
4190 if Inline_Processing_Required
then
4191 -- No need to recompute the answer if we know it is positive
4192 -- and back-end inlining is enabled.
4194 if Is_Inlined
(Gen_Unit
) and then Back_End_Inlining
then
4198 E
:= First_Entity
(Gen_Unit
);
4199 while Present
(E
) loop
4200 if Is_Subprogram
(E
) and then Is_Inlined
(E
) then
4201 -- Remember if there are any subprograms with Inline_Always
4203 if Has_Pragma_Inline_Always
(E
) then
4204 Has_Inline_Always
:= True;
4207 Set_Is_Inlined
(Gen_Unit
);
4216 end Might_Inline_Subp
;
4218 -------------------------------
4219 -- Needs_Body_Instantiated --
4220 -------------------------------
4222 function Needs_Body_Instantiated
(Gen_Unit
: Entity_Id
) return Boolean is
4224 -- No need to instantiate bodies in generic units
4226 if Is_Generic_Unit
(Cunit_Entity
(Main_Unit
)) then
4230 -- If the instantiation is in the main unit, then the body is needed
4232 if Is_In_Main_Unit
(N
) then
4236 -- In GNATprove mode, never instantiate bodies outside of the main
4237 -- unit, as it does not use frontend/backend inlining in the way that
4238 -- GNAT does, so does not benefit from such instantiations. On the
4239 -- contrary, such instantiations may bring artificial constraints,
4240 -- as for example such bodies may require preprocessing.
4242 if GNATprove_Mode
then
4246 -- If not, then again no need to instantiate bodies in generic units
4248 if Is_Generic_Unit
(Cunit_Entity
(Get_Code_Unit
(N
))) then
4252 -- Here we have a special handling for back-end inlining: if inline
4253 -- processing is required, then we unconditionally want to have the
4254 -- body instantiated. The reason is that Might_Inline_Subp does not
4255 -- catch all the cases (as it does not recurse into nested packages)
4256 -- so this avoids the need to patch things up afterwards. Moreover,
4257 -- these instantiations are only performed on demand when back-end
4258 -- inlining is enabled, so this causes very little extra work.
4260 if Inline_Processing_Required
and then Back_End_Inlining
then
4264 -- We want to have the bodies instantiated in non-main units if
4265 -- they might contribute inlined subprograms.
4267 return Might_Inline_Subp
(Gen_Unit
);
4268 end Needs_Body_Instantiated
;
4270 -- Local declarations
4272 Gen_Id
: constant Node_Id
:= Name
(N
);
4273 Inst_Id
: constant Entity_Id
:= Defining_Entity
(N
);
4274 Is_Actual_Pack
: constant Boolean := Is_Internal
(Inst_Id
);
4275 Loc
: constant Source_Ptr
:= Sloc
(N
);
4277 Saved_GM
: constant Ghost_Mode_Type
:= Ghost_Mode
;
4278 Saved_IGR
: constant Node_Id
:= Ignored_Ghost_Region
;
4279 Saved_ISMP
: constant Boolean :=
4280 Ignore_SPARK_Mode_Pragmas_In_Instance
;
4281 Saved_SM
: constant SPARK_Mode_Type
:= SPARK_Mode
;
4282 Saved_SMP
: constant Node_Id
:= SPARK_Mode_Pragma
;
4283 -- Save the Ghost and SPARK mode-related data to restore on exit
4285 Saved_Style_Check
: constant Boolean := Style_Check
;
4286 -- Save style check mode for restore on exit
4289 Act_Decl_Name
: Node_Id
;
4290 Act_Decl_Id
: Entity_Id
;
4293 Env_Installed
: Boolean := False;
4296 Gen_Unit
: Entity_Id
;
4297 Inline_Now
: Boolean := False;
4298 Needs_Body
: Boolean;
4299 Parent_Installed
: Boolean := False;
4300 Renaming_List
: List_Id
;
4301 Unit_Renaming
: Node_Id
;
4303 Vis_Prims_List
: Elist_Id
:= No_Elist
;
4304 -- List of primitives made temporarily visible in the instantiation
4305 -- to match the visibility of the formal type
4307 -- Start of processing for Analyze_Package_Instantiation
4310 -- Preserve relevant elaboration-related attributes of the context which
4311 -- are no longer available or very expensive to recompute once analysis,
4312 -- resolution, and expansion are over.
4314 Mark_Elaboration_Attributes
4321 -- Very first thing: check for Text_IO special unit in case we are
4322 -- instantiating one of the children of [[Wide_]Wide_]Text_IO.
4324 Check_Text_IO_Special_Unit
(Name
(N
));
4326 -- Make node global for error reporting
4328 Instantiation_Node
:= N
;
4330 -- Case of instantiation of a generic package
4332 if Nkind
(N
) = N_Package_Instantiation
then
4333 Act_Decl_Id
:= New_Copy
(Defining_Entity
(N
));
4335 if Nkind
(Defining_Unit_Name
(N
)) = N_Defining_Program_Unit_Name
then
4337 Make_Defining_Program_Unit_Name
(Loc
,
4339 New_Copy_Tree
(Name
(Defining_Unit_Name
(N
))),
4340 Defining_Identifier
=> Act_Decl_Id
);
4342 Act_Decl_Name
:= Act_Decl_Id
;
4345 -- Case of instantiation of a formal package
4348 Act_Decl_Id
:= Defining_Identifier
(N
);
4349 Act_Decl_Name
:= Act_Decl_Id
;
4352 Generate_Definition
(Act_Decl_Id
);
4353 Mutate_Ekind
(Act_Decl_Id
, E_Package
);
4354 Set_Is_Not_Self_Hidden
(Act_Decl_Id
);
4356 -- Initialize list of incomplete actuals before analysis
4358 Set_Incomplete_Actuals
(Act_Decl_Id
, New_Elmt_List
);
4360 Preanalyze_Actuals
(N
, Act_Decl_Id
);
4362 -- Turn off style checking in instances. If the check is enabled on the
4363 -- generic unit, a warning in an instance would just be noise. If not
4364 -- enabled on the generic, then a warning in an instance is just wrong.
4365 -- This must be done after analyzing the actuals, which do come from
4366 -- source and are subject to style checking.
4368 Style_Check
:= False;
4371 Env_Installed
:= True;
4373 -- Reset renaming map for formal types. The mapping is established
4374 -- when analyzing the generic associations, but some mappings are
4375 -- inherited from formal packages of parent units, and these are
4376 -- constructed when the parents are installed.
4378 Generic_Renamings
.Set_Last
(0);
4379 Generic_Renamings_HTable
.Reset
;
4381 -- Except for an abbreviated instance created to check a formal package,
4382 -- install the parent if this is a generic child unit.
4384 if not Is_Abbreviated_Instance
(Inst_Id
) then
4385 Check_Generic_Child_Unit
(Gen_Id
, Parent_Installed
);
4388 Gen_Unit
:= Entity
(Gen_Id
);
4390 -- A package instantiation is Ghost when it is subject to pragma Ghost
4391 -- or the generic template is Ghost. Set the mode now to ensure that
4392 -- any nodes generated during analysis and expansion are marked as
4395 Mark_And_Set_Ghost_Instantiation
(N
, Gen_Unit
);
4397 -- Verify that it is the name of a generic package
4399 -- A visibility glitch: if the instance is a child unit and the generic
4400 -- is the generic unit of a parent instance (i.e. both the parent and
4401 -- the child units are instances of the same package) the name now
4402 -- denotes the renaming within the parent, not the intended generic
4403 -- unit. See if there is a homonym that is the desired generic. The
4404 -- renaming declaration must be visible inside the instance of the
4405 -- child, but not when analyzing the name in the instantiation itself.
4407 if Ekind
(Gen_Unit
) = E_Package
4408 and then Present
(Renamed_Entity
(Gen_Unit
))
4409 and then In_Open_Scopes
(Renamed_Entity
(Gen_Unit
))
4410 and then Is_Generic_Instance
(Renamed_Entity
(Gen_Unit
))
4411 and then Present
(Homonym
(Gen_Unit
))
4413 Gen_Unit
:= Homonym
(Gen_Unit
);
4416 if Etype
(Gen_Unit
) = Any_Type
then
4420 elsif Ekind
(Gen_Unit
) /= E_Generic_Package
then
4422 -- Ada 2005 (AI-50217): Cannot use instance in limited with_clause
4424 if From_Limited_With
(Gen_Unit
) then
4426 ("cannot instantiate a limited withed package", Gen_Id
);
4429 ("& is not the name of a generic package", Gen_Id
, Gen_Unit
);
4436 if In_Extended_Main_Source_Unit
(N
) then
4437 Set_Is_Instantiated
(Gen_Unit
);
4438 Generate_Reference
(Gen_Unit
, N
);
4440 if Present
(Renamed_Entity
(Gen_Unit
)) then
4441 Set_Is_Instantiated
(Renamed_Entity
(Gen_Unit
));
4442 Generate_Reference
(Renamed_Entity
(Gen_Unit
), N
);
4446 if Nkind
(Gen_Id
) = N_Identifier
4447 and then Chars
(Gen_Unit
) = Chars
(Defining_Entity
(N
))
4450 ("& is hidden within declaration of instance", Gen_Id
, Gen_Unit
);
4452 elsif Nkind
(Gen_Id
) = N_Expanded_Name
4453 and then Is_Child_Unit
(Gen_Unit
)
4454 and then Nkind
(Prefix
(Gen_Id
)) = N_Identifier
4455 and then Chars
(Act_Decl_Id
) = Chars
(Prefix
(Gen_Id
))
4458 ("& is hidden within declaration of instance", Prefix
(Gen_Id
));
4461 Set_Entity
(Gen_Id
, Gen_Unit
);
4463 -- If generic is a renaming, get original generic unit
4465 if Present
(Renamed_Entity
(Gen_Unit
))
4466 and then Ekind
(Renamed_Entity
(Gen_Unit
)) = E_Generic_Package
4468 Gen_Unit
:= Renamed_Entity
(Gen_Unit
);
4471 -- Verify that there are no circular instantiations
4473 if In_Open_Scopes
(Gen_Unit
) then
4474 Error_Msg_NE
("instantiation of & within itself", N
, Gen_Unit
);
4478 elsif Contains_Instance_Of
(Gen_Unit
, Current_Scope
, Gen_Id
) then
4479 Error_Msg_Node_2
:= Current_Scope
;
4481 ("circular instantiation: & instantiated in &!", N
, Gen_Unit
);
4482 Circularity_Detected
:= True;
4487 Mutate_Ekind
(Inst_Id
, E_Package
);
4488 Set_Scope
(Inst_Id
, Current_Scope
);
4490 -- If the context of the instance is subject to SPARK_Mode "off" or
4491 -- the annotation is altogether missing, set the global flag which
4492 -- signals Analyze_Pragma to ignore all SPARK_Mode pragmas within
4495 if SPARK_Mode
/= On
then
4496 Ignore_SPARK_Mode_Pragmas_In_Instance
:= True;
4498 -- Mark the instance spec in case the body is instantiated at a
4499 -- later pass. This preserves the original context in effect for
4502 Set_Ignore_SPARK_Mode_Pragmas
(Act_Decl_Id
);
4505 Gen_Decl
:= Unit_Declaration_Node
(Gen_Unit
);
4506 Gen_Spec
:= Specification
(Gen_Decl
);
4508 -- Initialize renamings map, for error checking, and the list that
4509 -- holds private entities whose views have changed between generic
4510 -- definition and instantiation. If this is the instance created to
4511 -- validate an actual package, the instantiation environment is that
4512 -- of the enclosing instance.
4514 Create_Instantiation_Source
(N
, Gen_Unit
, S_Adjustment
);
4516 -- Copy original generic tree, to produce text for instantiation
4520 (Original_Node
(Gen_Decl
), Empty
, Instantiating
=> True);
4522 Act_Spec
:= Specification
(Act_Tree
);
4524 -- If this is the instance created to validate an actual package,
4525 -- only the formals matter, do not examine the package spec itself.
4527 if Is_Actual_Pack
then
4528 Set_Visible_Declarations
(Act_Spec
, New_List
);
4529 Set_Private_Declarations
(Act_Spec
, New_List
);
4533 Analyze_Associations
4535 Formals
=> Generic_Formal_Declarations
(Act_Tree
),
4536 F_Copy
=> Generic_Formal_Declarations
(Gen_Decl
));
4538 Vis_Prims_List
:= Check_Hidden_Primitives
(Renaming_List
);
4540 Set_Instance_Env
(Gen_Unit
, Act_Decl_Id
);
4541 Set_Defining_Unit_Name
(Act_Spec
, Act_Decl_Name
);
4542 Set_Is_Generic_Instance
(Act_Decl_Id
);
4543 Set_Generic_Parent
(Act_Spec
, Gen_Unit
);
4545 -- References to the generic in its own declaration or its body are
4546 -- references to the instance. Add a renaming declaration for the
4547 -- generic unit itself. This declaration, as well as the renaming
4548 -- declarations for the generic formals, must remain private to the
4549 -- unit: the formals, because this is the language semantics, and
4550 -- the unit because its use is an artifact of the implementation.
4553 Make_Package_Renaming_Declaration
(Loc
,
4554 Defining_Unit_Name
=>
4555 Make_Defining_Identifier
(Loc
, Chars
(Gen_Unit
)),
4556 Name
=> New_Occurrence_Of
(Act_Decl_Id
, Loc
));
4558 Append
(Unit_Renaming
, Renaming_List
);
4560 -- The renaming declarations are the first local declarations of the
4563 if Is_Non_Empty_List
(Visible_Declarations
(Act_Spec
)) then
4565 (First
(Visible_Declarations
(Act_Spec
)), Renaming_List
);
4567 Set_Visible_Declarations
(Act_Spec
, Renaming_List
);
4570 Act_Decl
:= Make_Package_Declaration
(Loc
, Specification
=> Act_Spec
);
4572 -- Propagate the aspect specifications from the package declaration
4573 -- template to the instantiated version of the package declaration.
4575 if Has_Aspects
(Act_Tree
) then
4576 Set_Aspect_Specifications
(Act_Decl
,
4577 New_Copy_List_Tree
(Aspect_Specifications
(Act_Tree
)));
4580 -- The generic may have a generated Default_Storage_Pool aspect,
4581 -- set at the point of generic declaration. If the instance has
4582 -- that aspect, it overrides the one inherited from the generic.
4584 if Has_Aspects
(Gen_Spec
) then
4585 if No
(Aspect_Specifications
(N
)) then
4586 Set_Aspect_Specifications
(N
,
4588 (Aspect_Specifications
(Gen_Spec
))));
4592 Inherited_Aspects
: constant List_Id
:=
4594 (Aspect_Specifications
(Gen_Spec
));
4598 Pool_Present
: Boolean := False;
4601 ASN1
:= First
(Aspect_Specifications
(N
));
4602 while Present
(ASN1
) loop
4603 if Chars
(Identifier
(ASN1
)) =
4604 Name_Default_Storage_Pool
4606 Pool_Present
:= True;
4613 if Pool_Present
then
4615 -- If generic carries a default storage pool, remove it
4616 -- in favor of the instance one.
4618 ASN2
:= First
(Inherited_Aspects
);
4619 while Present
(ASN2
) loop
4620 if Chars
(Identifier
(ASN2
)) =
4621 Name_Default_Storage_Pool
4632 (Aspect_Specifications
(N
), Inherited_Aspects
);
4637 -- Save the instantiation node for a subsequent instantiation of the
4638 -- body if there is one and it needs to be instantiated here.
4640 -- We instantiate the body only if we are generating code, or if we
4641 -- are generating cross-reference information, or for GNATprove use.
4644 Enclosing_Body_Present
: Boolean := False;
4645 -- If the generic unit is not a compilation unit, then a body may
4646 -- be present in its parent even if none is required. We create a
4647 -- tentative pending instantiation for the body, which will be
4648 -- discarded if none is actually present.
4653 if Scope
(Gen_Unit
) /= Standard_Standard
4654 and then not Is_Child_Unit
(Gen_Unit
)
4656 Scop
:= Scope
(Gen_Unit
);
4657 while Present
(Scop
) and then Scop
/= Standard_Standard
loop
4658 if Unit_Requires_Body
(Scop
) then
4659 Enclosing_Body_Present
:= True;
4662 elsif In_Open_Scopes
(Scop
)
4663 and then In_Package_Body
(Scop
)
4665 Enclosing_Body_Present
:= True;
4669 exit when Is_Compilation_Unit
(Scop
);
4670 Scop
:= Scope
(Scop
);
4674 -- If front-end inlining is enabled or there are any subprograms
4675 -- marked with Inline_Always, and this is a unit for which code
4676 -- will be generated, we instantiate the body at once.
4678 -- This is done if the instance is not the main unit, and if the
4679 -- generic is not a child unit of another generic, to avoid scope
4680 -- problems and the reinstallation of parent instances.
4683 and then (not Is_Child_Unit
(Gen_Unit
)
4684 or else not Is_Generic_Unit
(Scope
(Gen_Unit
)))
4685 and then Might_Inline_Subp
(Gen_Unit
)
4686 and then not Is_Actual_Pack
4688 if not Back_End_Inlining
4689 and then (Front_End_Inlining
or else Has_Inline_Always
)
4690 and then (Is_In_Main_Unit
(N
)
4691 or else In_Main_Context
(Current_Scope
))
4692 and then Nkind
(Parent
(N
)) /= N_Compilation_Unit
4696 -- In configurable_run_time mode we force the inlining of
4697 -- predefined subprograms marked Inline_Always, to minimize
4698 -- the use of the run-time library.
4700 elsif In_Predefined_Unit
(Gen_Decl
)
4701 and then Configurable_Run_Time_Mode
4702 and then Nkind
(Parent
(N
)) /= N_Compilation_Unit
4707 -- If the current scope is itself an instance within a child
4708 -- unit, there will be duplications in the scope stack, and the
4709 -- unstacking mechanism in Inline_Instance_Body will fail.
4710 -- This loses some rare cases of optimization.
4712 if Is_Generic_Instance
(Current_Scope
) then
4714 Curr_Unit
: constant Entity_Id
:=
4715 Cunit_Entity
(Current_Sem_Unit
);
4717 if Curr_Unit
/= Current_Scope
4718 and then Is_Child_Unit
(Curr_Unit
)
4720 Inline_Now
:= False;
4727 (Unit_Requires_Body
(Gen_Unit
)
4728 or else Enclosing_Body_Present
4729 or else Present
(Corresponding_Body
(Gen_Decl
)))
4730 and then Needs_Body_Instantiated
(Gen_Unit
)
4731 and then not Is_Actual_Pack
4732 and then not Inline_Now
4733 and then (Operating_Mode
= Generate_Code
4734 or else (Operating_Mode
= Check_Semantics
4735 and then GNATprove_Mode
));
4737 -- If front-end inlining is enabled or there are any subprograms
4738 -- marked with Inline_Always, do not instantiate body when within
4739 -- a generic context.
4741 if not Back_End_Inlining
4742 and then (Front_End_Inlining
or else Has_Inline_Always
)
4743 and then not Expander_Active
4745 Needs_Body
:= False;
4748 -- If the current context is generic, and the package being
4749 -- instantiated is declared within a formal package, there is no
4750 -- body to instantiate until the enclosing generic is instantiated
4751 -- and there is an actual for the formal package. If the formal
4752 -- package has parameters, we build a regular package instance for
4753 -- it, that precedes the original formal package declaration.
4755 if In_Open_Scopes
(Scope
(Scope
(Gen_Unit
))) then
4757 Decl
: constant Node_Id
:=
4759 (Unit_Declaration_Node
(Scope
(Gen_Unit
)));
4761 if Nkind
(Decl
) = N_Formal_Package_Declaration
4762 or else (Nkind
(Decl
) = N_Package_Declaration
4763 and then Is_List_Member
(Decl
)
4764 and then Present
(Next
(Decl
))
4766 Nkind
(Next
(Decl
)) =
4767 N_Formal_Package_Declaration
)
4769 Needs_Body
:= False;
4775 -- For RCI unit calling stubs, we omit the instance body if the
4776 -- instance is the RCI library unit itself.
4778 -- However there is a special case for nested instances: in this case
4779 -- we do generate the instance body, as it might be required, e.g.
4780 -- because it provides stream attributes for some type used in the
4781 -- profile of a remote subprogram. This is consistent with 12.3(12),
4782 -- which indicates that the instance body occurs at the place of the
4783 -- instantiation, and thus is part of the RCI declaration, which is
4784 -- present on all client partitions (this is E.2.3(18)).
4786 -- Note that AI12-0002 may make it illegal at some point to have
4787 -- stream attributes defined in an RCI unit, in which case this
4788 -- special case will become unnecessary. In the meantime, there
4789 -- is known application code in production that depends on this
4790 -- being possible, so we definitely cannot eliminate the body in
4791 -- the case of nested instances for the time being.
4793 -- When we generate a nested instance body, calling stubs for any
4794 -- relevant subprogram will be inserted immediately after the
4795 -- subprogram declarations, and will take precedence over the
4796 -- subsequent (original) body. (The stub and original body will be
4797 -- complete homographs, but this is permitted in an instance).
4798 -- (Could we do better and remove the original body???)
4800 if Distribution_Stub_Mode
= Generate_Caller_Stub_Body
4801 and then Comes_From_Source
(N
)
4802 and then Nkind
(Parent
(N
)) = N_Compilation_Unit
4804 Needs_Body
:= False;
4807 -- If the context requires a full instantiation, set things up for
4808 -- subsequent construction of the body.
4812 Fin_Scop
, S
: Entity_Id
;
4815 Check_Forward_Instantiation
(Gen_Decl
);
4819 -- For a package instantiation that is not a compilation unit,
4820 -- indicate that cleanup actions of the innermost enclosing
4821 -- scope for which they are generated should be delayed until
4822 -- after the package body is instantiated.
4824 if Nkind
(N
) = N_Package_Instantiation
4825 and then not Is_Compilation_Unit
(Act_Decl_Id
)
4829 while S
/= Standard_Standard
loop
4830 -- Cleanup actions are not generated within generic units
4831 -- or in the formal part of generic units.
4833 if not Expander_Active
then
4836 -- For package scopes, cleanup actions are generated only
4837 -- for compilation units, for spec and body separately.
4839 elsif Ekind
(S
) = E_Package
then
4840 if Is_Compilation_Unit
(S
) then
4841 if In_Package_Body
(S
) then
4842 Fin_Scop
:= Body_Entity
(S
);
4847 Set_Delay_Cleanups
(Fin_Scop
);
4854 -- Cleanup actions are generated for all dynamic scopes
4858 Set_Delay_Cleanups
(Fin_Scop
);
4864 Add_Pending_Instantiation
(N
, Act_Decl
, Fin_Scop
);
4868 Set_Categorization_From_Pragmas
(Act_Decl
);
4870 if Parent_Installed
then
4874 Set_Instance_Spec
(N
, Act_Decl
);
4876 -- If not a compilation unit, insert the package declaration before
4877 -- the original instantiation node.
4879 if Nkind
(Parent
(N
)) /= N_Compilation_Unit
then
4880 Mark_Rewrite_Insertion
(Act_Decl
);
4881 Insert_Before
(N
, Act_Decl
);
4883 if Has_Aspects
(N
) then
4884 Analyze_Aspect_Specifications
(N
, Act_Decl_Id
);
4886 -- The pragma created for a Default_Storage_Pool aspect must
4887 -- appear ahead of the declarations in the instance spec.
4888 -- Analysis has placed it after the instance node, so remove
4889 -- it and reinsert it properly now.
4892 ASN
: constant Node_Id
:= First
(Aspect_Specifications
(N
));
4893 A_Name
: constant Name_Id
:= Chars
(Identifier
(ASN
));
4897 if A_Name
= Name_Default_Storage_Pool
then
4898 if No
(Visible_Declarations
(Act_Spec
)) then
4899 Set_Visible_Declarations
(Act_Spec
, New_List
);
4903 while Present
(Decl
) loop
4904 if Nkind
(Decl
) = N_Pragma
then
4906 Prepend
(Decl
, Visible_Declarations
(Act_Spec
));
4918 -- For an instantiation that is a compilation unit, place
4919 -- declaration on current node so context is complete for analysis
4920 -- (including nested instantiations). If this is the main unit,
4921 -- the declaration eventually replaces the instantiation node.
4922 -- If the instance body is created later, it replaces the
4923 -- instance node, and the declaration is attached to it
4924 -- (see Build_Instance_Compilation_Unit_Nodes).
4927 if Cunit_Entity
(Current_Sem_Unit
) = Defining_Entity
(N
) then
4929 -- The entity for the current unit is the newly created one,
4930 -- and all semantic information is attached to it.
4932 Set_Cunit_Entity
(Current_Sem_Unit
, Act_Decl_Id
);
4934 -- If this is the main unit, replace the main entity as well
4936 if Current_Sem_Unit
= Main_Unit
then
4937 Main_Unit_Entity
:= Act_Decl_Id
;
4941 Set_Unit
(Parent
(N
), Act_Decl
);
4942 Set_Parent_Spec
(Act_Decl
, Parent_Spec
(N
));
4943 Set_Package_Instantiation
(Act_Decl_Id
, N
);
4945 -- Process aspect specifications of the instance node, if any, to
4946 -- take into account categorization pragmas before analyzing the
4949 Analyze_Aspect_Specifications
(N
, Act_Decl_Id
);
4952 Set_Unit
(Parent
(N
), N
);
4953 Set_Body_Required
(Parent
(N
), False);
4955 -- We never need elaboration checks on instantiations, since by
4956 -- definition, the body instantiation is elaborated at the same
4957 -- time as the spec instantiation.
4959 if Legacy_Elaboration_Checks
then
4960 Set_Kill_Elaboration_Checks
(Act_Decl_Id
);
4961 Set_Suppress_Elaboration_Warnings
(Act_Decl_Id
);
4965 if Legacy_Elaboration_Checks
then
4966 Check_Elab_Instantiation
(N
);
4969 -- Save the scenario for later examination by the ABE Processing
4972 Record_Elaboration_Scenario
(N
);
4974 -- The instantiation results in a guaranteed ABE
4976 if Is_Known_Guaranteed_ABE
(N
) and then Needs_Body
then
4977 -- Do not instantiate the corresponding body because gigi cannot
4978 -- handle certain types of premature instantiations.
4980 Remove_Dead_Instance
(N
);
4982 -- Create completing bodies for all subprogram declarations since
4983 -- their real bodies will not be instantiated.
4985 Provide_Completing_Bodies
(Instance_Spec
(N
));
4988 Check_Hidden_Child_Unit
(N
, Gen_Unit
, Act_Decl_Id
);
4990 Set_First_Private_Entity
(Defining_Unit_Name
(Unit_Renaming
),
4991 First_Private_Entity
(Act_Decl_Id
));
4993 -- If the instantiation needs a body, the unit will be turned into
4994 -- a package body and receive its own elaboration entity. Otherwise,
4995 -- the nature of the unit is now a package declaration.
4997 -- Note that the below rewriting means that Act_Decl, which has been
4998 -- analyzed and expanded, will be re-expanded as the rewritten N.
5000 if Nkind
(Parent
(N
)) = N_Compilation_Unit
5001 and then not Needs_Body
5003 Rewrite
(N
, Act_Decl
);
5006 if Present
(Corresponding_Body
(Gen_Decl
))
5007 or else Unit_Requires_Body
(Gen_Unit
)
5009 Set_Has_Completion
(Act_Decl_Id
);
5012 Check_Formal_Packages
(Act_Decl_Id
);
5014 Restore_Hidden_Primitives
(Vis_Prims_List
);
5015 Restore_Private_Views
(Act_Decl_Id
);
5017 Inherit_Context
(Gen_Decl
, N
);
5019 if Parent_Installed
then
5024 Env_Installed
:= False;
5027 Validate_Categorization_Dependency
(N
, Act_Decl_Id
);
5029 -- There used to be a check here to prevent instantiations in local
5030 -- contexts if the No_Local_Allocators restriction was active. This
5031 -- check was removed by a binding interpretation in AI-95-00130/07,
5032 -- but we retain the code for documentation purposes.
5034 -- if Ekind (Act_Decl_Id) /= E_Void
5035 -- and then not Is_Library_Level_Entity (Act_Decl_Id)
5037 -- Check_Restriction (No_Local_Allocators, N);
5041 Inline_Instance_Body
(N
, Gen_Unit
, Act_Decl
);
5044 -- Check that if N is an instantiation of System.Dim_Float_IO or
5045 -- System.Dim_Integer_IO, the formal type has a dimension system.
5047 if Nkind
(N
) = N_Package_Instantiation
5048 and then Is_Dim_IO_Package_Instantiation
(N
)
5051 Assoc
: constant Node_Id
:= First
(Generic_Associations
(N
));
5053 if not Has_Dimension_System
5054 (Etype
(Explicit_Generic_Actual_Parameter
(Assoc
)))
5056 Error_Msg_N
("type with a dimension system expected", Assoc
);
5062 if Nkind
(Parent
(N
)) /= N_Compilation_Unit
then
5063 Analyze_Aspect_Specifications
(N
, Act_Decl_Id
);
5066 Ignore_SPARK_Mode_Pragmas_In_Instance
:= Saved_ISMP
;
5067 Restore_Ghost_Region
(Saved_GM
, Saved_IGR
);
5068 Restore_SPARK_Mode
(Saved_SM
, Saved_SMP
);
5069 Style_Check
:= Saved_Style_Check
;
5072 when Instantiation_Error
=>
5073 if Parent_Installed
then
5077 if Env_Installed
then
5081 Ignore_SPARK_Mode_Pragmas_In_Instance
:= Saved_ISMP
;
5082 Restore_Ghost_Region
(Saved_GM
, Saved_IGR
);
5083 Restore_SPARK_Mode
(Saved_SM
, Saved_SMP
);
5084 Style_Check
:= Saved_Style_Check
;
5085 end Analyze_Package_Instantiation
;
5087 --------------------------
5088 -- Inline_Instance_Body --
5089 --------------------------
5091 -- WARNING: This routine manages SPARK regions. Return statements must be
5092 -- replaced by gotos which jump to the end of the routine and restore the
5095 procedure Inline_Instance_Body
5097 Gen_Unit
: Entity_Id
;
5100 Config_Attrs
: constant Config_Switches_Type
:= Save_Config_Switches
;
5102 Curr_Comp
: constant Node_Id
:= Cunit
(Current_Sem_Unit
);
5103 Curr_Unit
: constant Entity_Id
:= Cunit_Entity
(Current_Sem_Unit
);
5104 Gen_Comp
: constant Entity_Id
:=
5105 Cunit_Entity
(Get_Source_Unit
(Gen_Unit
));
5107 Scope_Stack_Depth
: constant Pos
:=
5108 Scope_Stack
.Last
- Scope_Stack
.First
+ 1;
5110 Inner_Scopes
: array (1 .. Scope_Stack_Depth
) of Entity_Id
;
5111 Instances
: array (1 .. Scope_Stack_Depth
) of Entity_Id
;
5112 Use_Clauses
: array (1 .. Scope_Stack_Depth
) of Node_Id
;
5114 Curr_Scope
: Entity_Id
:= Empty
;
5115 List
: Elist_Id
:= No_Elist
; -- init to avoid warning
5116 N_Instances
: Nat
:= 0;
5117 Num_Inner
: Nat
:= 0;
5118 Num_Scopes
: Nat
:= 0;
5119 Removed
: Boolean := False;
5124 -- Case of generic unit defined in another unit. We must remove the
5125 -- complete context of the current unit to install that of the generic.
5127 if Gen_Comp
/= Cunit_Entity
(Current_Sem_Unit
) then
5129 -- Loop through enclosing scopes until we reach a generic instance,
5130 -- package body, or subprogram.
5133 while Present
(S
) and then S
/= Standard_Standard
loop
5135 -- Save use clauses from enclosing scopes into Use_Clauses
5138 Num_Scopes
:= Num_Scopes
+ 1;
5140 Use_Clauses
(Num_Scopes
) :=
5142 (Scope_Stack
.Last
- Num_Scopes
+ 1).First_Use_Clause
);
5143 End_Use_Clauses
(Use_Clauses
(Num_Scopes
));
5145 exit when Scope_Stack
.Last
- Num_Scopes
+ 1 = Scope_Stack
.First
5146 or else Scope_Stack
.Table
5147 (Scope_Stack
.Last
- Num_Scopes
).Entity
= Scope
(S
);
5150 exit when Is_Generic_Instance
(S
)
5151 and then (In_Package_Body
(S
)
5152 or else Ekind
(S
) = E_Procedure
5153 or else Ekind
(S
) = E_Function
);
5157 Vis
:= Is_Immediately_Visible
(Gen_Comp
);
5159 -- Find and save all enclosing instances
5164 and then S
/= Standard_Standard
5166 if Is_Generic_Instance
(S
) then
5167 N_Instances
:= N_Instances
+ 1;
5168 Instances
(N_Instances
) := S
;
5170 exit when In_Package_Body
(S
);
5176 -- Remove context of current compilation unit, unless we are within a
5177 -- nested package instantiation, in which case the context has been
5178 -- removed previously.
5180 -- If current scope is the body of a child unit, remove context of
5181 -- spec as well. If an enclosing scope is an instance body, the
5182 -- context has already been removed, but the entities in the body
5183 -- must be made invisible as well.
5186 while Present
(S
) and then S
/= Standard_Standard
loop
5187 if Is_Generic_Instance
(S
)
5188 and then (In_Package_Body
(S
)
5189 or else Ekind
(S
) in E_Procedure | E_Function
)
5191 -- We still have to remove the entities of the enclosing
5192 -- instance from direct visibility.
5197 E
:= First_Entity
(S
);
5198 while Present
(E
) loop
5199 Set_Is_Immediately_Visible
(E
, False);
5208 or else (Ekind
(Curr_Unit
) = E_Package_Body
5209 and then S
= Spec_Entity
(Curr_Unit
))
5210 or else (Ekind
(Curr_Unit
) = E_Subprogram_Body
5211 and then S
= Corresponding_Spec
5212 (Unit_Declaration_Node
(Curr_Unit
)))
5216 -- Remove entities in current scopes from visibility, so that
5217 -- instance body is compiled in a clean environment.
5219 List
:= Save_Scope_Stack
(Handle_Use
=> False);
5221 if Is_Child_Unit
(S
) then
5223 -- Remove child unit from stack, as well as inner scopes.
5224 -- Removing the context of a child unit removes parent units
5227 while Current_Scope
/= S
loop
5228 Num_Inner
:= Num_Inner
+ 1;
5229 Inner_Scopes
(Num_Inner
) := Current_Scope
;
5234 Remove_Context
(Curr_Comp
);
5238 Remove_Context
(Curr_Comp
);
5241 if Ekind
(Curr_Unit
) = E_Package_Body
then
5242 Remove_Context
(Library_Unit
(Curr_Comp
));
5249 pragma Assert
(Num_Inner
< Num_Scopes
);
5251 Push_Scope
(Standard_Standard
);
5252 Scope_Stack
.Table
(Scope_Stack
.Last
).Is_Active_Stack_Base
:= True;
5254 -- The inlined package body is analyzed with the configuration state
5255 -- of the context prior to the scope manipulations performed above.
5257 -- ??? shouldn't this also use the warning state of the context prior
5258 -- to the scope manipulations?
5260 Instantiate_Package_Body
5263 Act_Decl
=> Act_Decl
,
5265 Config_Switches
=> Config_Attrs
,
5266 Current_Sem_Unit
=> Current_Sem_Unit
,
5267 Expander_Status
=> Expander_Active
,
5268 Local_Suppress_Stack_Top
=> Local_Suppress_Stack_Top
,
5269 Scope_Suppress
=> Scope_Suppress
,
5270 Warnings
=> Save_Warnings
)),
5271 Inlined_Body
=> True);
5277 Set_Is_Immediately_Visible
(Gen_Comp
, Vis
);
5279 -- Reset Generic_Instance flag so that use clauses can be installed
5280 -- in the proper order. (See Use_One_Package for effect of enclosing
5281 -- instances on processing of use clauses).
5283 for J
in 1 .. N_Instances
loop
5284 Set_Is_Generic_Instance
(Instances
(J
), False);
5288 Install_Context
(Curr_Comp
, Chain
=> False);
5290 if Present
(Curr_Scope
)
5291 and then Is_Child_Unit
(Curr_Scope
)
5293 Push_Scope
(Curr_Scope
);
5294 Set_Is_Immediately_Visible
(Curr_Scope
);
5296 -- Finally, restore inner scopes as well
5298 for J
in reverse 1 .. Num_Inner
loop
5299 Push_Scope
(Inner_Scopes
(J
));
5303 Restore_Scope_Stack
(List
, Handle_Use
=> False);
5305 if Present
(Curr_Scope
)
5307 (In_Private_Part
(Curr_Scope
)
5308 or else In_Package_Body
(Curr_Scope
))
5310 -- Install private declaration of ancestor units, which are
5311 -- currently available. Restore_Scope_Stack and Install_Context
5312 -- only install the visible part of parents.
5317 Par
:= Scope
(Curr_Scope
);
5318 while Present
(Par
) and then Par
/= Standard_Standard
loop
5319 Install_Private_Declarations
(Par
);
5326 -- Restore use clauses. For a child unit, use clauses in the parents
5327 -- are restored when installing the context, so only those in inner
5328 -- scopes (and those local to the child unit itself) need to be
5329 -- installed explicitly.
5331 if Is_Child_Unit
(Curr_Unit
) and then Removed
then
5332 for J
in reverse 1 .. Num_Inner
+ 1 loop
5333 Scope_Stack
.Table
(Scope_Stack
.Last
- J
+ 1).First_Use_Clause
:=
5335 Install_Use_Clauses
(Use_Clauses
(J
));
5339 for J
in reverse 1 .. Num_Scopes
loop
5340 Scope_Stack
.Table
(Scope_Stack
.Last
- J
+ 1).First_Use_Clause
:=
5342 Install_Use_Clauses
(Use_Clauses
(J
));
5346 -- Restore status of instances. If one of them is a body, make its
5347 -- local entities visible again.
5354 for J
in 1 .. N_Instances
loop
5355 Inst
:= Instances
(J
);
5356 Set_Is_Generic_Instance
(Inst
, True);
5358 if In_Package_Body
(Inst
)
5359 or else Ekind
(S
) in E_Procedure | E_Function
5361 E
:= First_Entity
(Instances
(J
));
5362 while Present
(E
) loop
5363 Set_Is_Immediately_Visible
(E
);
5370 -- If generic unit is in current unit, current context is correct. Note
5371 -- that the context is guaranteed to carry the correct SPARK_Mode as no
5372 -- enclosing scopes were removed.
5375 Instantiate_Package_Body
5378 Act_Decl
=> Act_Decl
,
5380 Config_Switches
=> Save_Config_Switches
,
5381 Current_Sem_Unit
=> Current_Sem_Unit
,
5382 Expander_Status
=> Expander_Active
,
5383 Local_Suppress_Stack_Top
=> Local_Suppress_Stack_Top
,
5384 Scope_Suppress
=> Scope_Suppress
,
5385 Warnings
=> Save_Warnings
)),
5386 Inlined_Body
=> True);
5388 end Inline_Instance_Body
;
5390 -------------------------------------
5391 -- Analyze_Procedure_Instantiation --
5392 -------------------------------------
5394 procedure Analyze_Procedure_Instantiation
(N
: Node_Id
) is
5396 Analyze_Subprogram_Instantiation
(N
, E_Procedure
);
5397 end Analyze_Procedure_Instantiation
;
5399 -----------------------------------
5400 -- Need_Subprogram_Instance_Body --
5401 -----------------------------------
5403 function Need_Subprogram_Instance_Body
5405 Subp
: Entity_Id
) return Boolean
5407 function Is_Inlined_Or_Child_Of_Inlined
(E
: Entity_Id
) return Boolean;
5408 -- Return True if E is an inlined subprogram, an inlined renaming or a
5409 -- subprogram nested in an inlined subprogram. The inlining machinery
5410 -- totally disregards nested subprograms since it considers that they
5411 -- will always be compiled if the parent is (see Inline.Is_Nested).
5413 ------------------------------------
5414 -- Is_Inlined_Or_Child_Of_Inlined --
5415 ------------------------------------
5417 function Is_Inlined_Or_Child_Of_Inlined
(E
: Entity_Id
) return Boolean is
5421 if Is_Inlined
(E
) or else Is_Inlined
(Alias
(E
)) then
5426 while Scop
/= Standard_Standard
loop
5427 if Is_Subprogram
(Scop
) and then Is_Inlined
(Scop
) then
5431 Scop
:= Scope
(Scop
);
5435 end Is_Inlined_Or_Child_Of_Inlined
;
5438 -- Must be in the main unit or inlined (or child of inlined)
5440 if (Is_In_Main_Unit
(N
) or else Is_Inlined_Or_Child_Of_Inlined
(Subp
))
5442 -- Must be generating code or analyzing code in GNATprove mode
5444 and then (Operating_Mode
= Generate_Code
5445 or else (Operating_Mode
= Check_Semantics
5446 and then GNATprove_Mode
))
5448 -- The body is needed when generating code (full expansion) and in
5449 -- in GNATprove mode (special expansion) for formal verification of
5452 and then (Expander_Active
or GNATprove_Mode
)
5454 -- No point in inlining if ABE is inevitable
5456 and then not Is_Known_Guaranteed_ABE
(N
)
5458 -- Or if subprogram is eliminated
5460 and then not Is_Eliminated
(Subp
)
5462 Add_Pending_Instantiation
(N
, Unit_Declaration_Node
(Subp
));
5465 -- Here if not inlined, or we ignore the inlining
5470 end Need_Subprogram_Instance_Body
;
5472 --------------------------------------
5473 -- Analyze_Subprogram_Instantiation --
5474 --------------------------------------
5476 -- WARNING: This routine manages Ghost and SPARK regions. Return statements
5477 -- must be replaced by gotos which jump to the end of the routine in order
5478 -- to restore the Ghost and SPARK modes.
5480 procedure Analyze_Subprogram_Instantiation
5484 Errs
: constant Nat
:= Serious_Errors_Detected
;
5485 Gen_Id
: constant Node_Id
:= Name
(N
);
5486 Inst_Id
: constant Entity_Id
:= Defining_Entity
(N
);
5487 Anon_Id
: constant Entity_Id
:=
5488 Make_Defining_Identifier
(Sloc
(Inst_Id
),
5489 Chars
=> New_External_Name
(Chars
(Inst_Id
), 'R'));
5490 Loc
: constant Source_Ptr
:= Sloc
(N
);
5492 Act_Decl_Id
: Entity_Id
:= Empty
; -- init to avoid warning
5497 Env_Installed
: Boolean := False;
5498 Gen_Unit
: Entity_Id
;
5500 Pack_Id
: Entity_Id
;
5501 Parent_Installed
: Boolean := False;
5503 Renaming_List
: List_Id
;
5504 -- The list of declarations that link formals and actuals of the
5505 -- instance. These are subtype declarations for formal types, and
5506 -- renaming declarations for other formals. The subprogram declaration
5507 -- for the instance is then appended to the list, and the last item on
5508 -- the list is the renaming declaration for the instance.
5510 procedure Analyze_Instance_And_Renamings
;
5511 -- The instance must be analyzed in a context that includes the mappings
5512 -- of generic parameters into actuals. We create a package declaration
5513 -- for this purpose, and a subprogram with an internal name within the
5514 -- package. The subprogram instance is simply an alias for the internal
5515 -- subprogram, declared in the current scope.
5517 procedure Build_Subprogram_Renaming
;
5518 -- If the subprogram is recursive, there are occurrences of the name of
5519 -- the generic within the body, which must resolve to the current
5520 -- instance. We add a renaming declaration after the declaration, which
5521 -- is available in the instance body, as well as in the analysis of
5522 -- aspects that appear in the generic. This renaming declaration is
5523 -- inserted after the instance declaration which it renames.
5525 ------------------------------------
5526 -- Analyze_Instance_And_Renamings --
5527 ------------------------------------
5529 procedure Analyze_Instance_And_Renamings
is
5530 Def_Ent
: constant Entity_Id
:= Defining_Entity
(N
);
5531 Pack_Decl
: Node_Id
;
5534 if Nkind
(Parent
(N
)) = N_Compilation_Unit
then
5536 -- For the case of a compilation unit, the container package has
5537 -- the same name as the instantiation, to insure that the binder
5538 -- calls the elaboration procedure with the right name. Copy the
5539 -- entity of the instance, which may have compilation level flags
5540 -- (e.g. Is_Child_Unit) set.
5542 Pack_Id
:= New_Copy
(Def_Ent
);
5545 -- Otherwise we use the name of the instantiation concatenated
5546 -- with its source position to ensure uniqueness if there are
5547 -- several instantiations with the same name.
5550 Make_Defining_Identifier
(Loc
,
5551 Chars
=> New_External_Name
5552 (Related_Id
=> Chars
(Def_Ent
),
5554 Suffix_Index
=> Source_Offset
(Sloc
(Def_Ent
))));
5558 Make_Package_Declaration
(Loc
,
5559 Specification
=> Make_Package_Specification
(Loc
,
5560 Defining_Unit_Name
=> Pack_Id
,
5561 Visible_Declarations
=> Renaming_List
,
5562 End_Label
=> Empty
));
5564 Set_Instance_Spec
(N
, Pack_Decl
);
5565 Set_Is_Generic_Instance
(Pack_Id
);
5566 Set_Debug_Info_Needed
(Pack_Id
);
5568 -- Case of not a compilation unit
5570 if Nkind
(Parent
(N
)) /= N_Compilation_Unit
then
5571 Mark_Rewrite_Insertion
(Pack_Decl
);
5572 Insert_Before
(N
, Pack_Decl
);
5573 Set_Has_Completion
(Pack_Id
);
5575 -- Case of an instantiation that is a compilation unit
5577 -- Place declaration on current node so context is complete for
5578 -- analysis (including nested instantiations), and for use in a
5579 -- context_clause (see Analyze_With_Clause).
5582 Set_Unit
(Parent
(N
), Pack_Decl
);
5583 Set_Parent_Spec
(Pack_Decl
, Parent_Spec
(N
));
5586 Analyze
(Pack_Decl
);
5587 Check_Formal_Packages
(Pack_Id
);
5589 -- Body of the enclosing package is supplied when instantiating the
5590 -- subprogram body, after semantic analysis is completed.
5592 if Nkind
(Parent
(N
)) = N_Compilation_Unit
then
5594 -- Remove package itself from visibility, so it does not
5595 -- conflict with subprogram.
5597 Set_Name_Entity_Id
(Chars
(Pack_Id
), Homonym
(Pack_Id
));
5599 -- Set name and scope of internal subprogram so that the proper
5600 -- external name will be generated. The proper scope is the scope
5601 -- of the wrapper package. We need to generate debugging info for
5602 -- the internal subprogram, so set flag accordingly.
5604 Set_Chars
(Anon_Id
, Chars
(Defining_Entity
(N
)));
5605 Set_Scope
(Anon_Id
, Scope
(Pack_Id
));
5607 -- Mark wrapper package as referenced, to avoid spurious warnings
5608 -- if the instantiation appears in various with_ clauses of
5609 -- subunits of the main unit.
5611 Set_Referenced
(Pack_Id
);
5614 Set_Is_Generic_Instance
(Anon_Id
);
5615 Set_Debug_Info_Needed
(Anon_Id
);
5616 Act_Decl_Id
:= New_Copy
(Anon_Id
);
5618 Set_Parent
(Act_Decl_Id
, Parent
(Anon_Id
));
5619 Set_Chars
(Act_Decl_Id
, Chars
(Defining_Entity
(N
)));
5620 Set_Sloc
(Act_Decl_Id
, Sloc
(Defining_Entity
(N
)));
5622 -- Subprogram instance comes from source only if generic does
5624 Preserve_Comes_From_Source
(Act_Decl_Id
, Gen_Unit
);
5626 -- If the instance is a child unit, mark the Id accordingly. Mark
5627 -- the anonymous entity as well, which is the real subprogram and
5628 -- which is used when the instance appears in a context clause.
5629 -- Similarly, propagate the Is_Eliminated flag to handle properly
5630 -- nested eliminated subprograms.
5632 Set_Is_Child_Unit
(Act_Decl_Id
, Is_Child_Unit
(Defining_Entity
(N
)));
5633 Set_Is_Child_Unit
(Anon_Id
, Is_Child_Unit
(Defining_Entity
(N
)));
5634 New_Overloaded_Entity
(Act_Decl_Id
);
5635 Check_Eliminated
(Act_Decl_Id
);
5636 Set_Is_Eliminated
(Anon_Id
, Is_Eliminated
(Act_Decl_Id
));
5638 if Nkind
(Parent
(N
)) = N_Compilation_Unit
then
5640 -- In compilation unit case, kill elaboration checks on the
5641 -- instantiation, since they are never needed - the body is
5642 -- instantiated at the same point as the spec.
5644 if Legacy_Elaboration_Checks
then
5645 Set_Kill_Elaboration_Checks
(Act_Decl_Id
);
5646 Set_Suppress_Elaboration_Warnings
(Act_Decl_Id
);
5649 Set_Is_Compilation_Unit
(Anon_Id
);
5650 Set_Cunit_Entity
(Current_Sem_Unit
, Pack_Id
);
5653 -- The instance is not a freezing point for the new subprogram.
5654 -- The anonymous subprogram may have a freeze node, created for
5655 -- some delayed aspects. This freeze node must not be inherited
5656 -- by the visible subprogram entity.
5658 Set_Is_Frozen
(Act_Decl_Id
, False);
5659 Set_Freeze_Node
(Act_Decl_Id
, Empty
);
5661 if Nkind
(Defining_Entity
(N
)) = N_Defining_Operator_Symbol
then
5662 Valid_Operator_Definition
(Act_Decl_Id
);
5665 Set_Alias
(Act_Decl_Id
, Anon_Id
);
5666 Set_Has_Completion
(Act_Decl_Id
);
5667 Set_Related_Instance
(Pack_Id
, Act_Decl_Id
);
5669 if Nkind
(Parent
(N
)) = N_Compilation_Unit
then
5670 Set_Body_Required
(Parent
(N
), False);
5672 end Analyze_Instance_And_Renamings
;
5674 -------------------------------
5675 -- Build_Subprogram_Renaming --
5676 -------------------------------
5678 procedure Build_Subprogram_Renaming
is
5679 Renaming_Decl
: Node_Id
;
5680 Unit_Renaming
: Node_Id
;
5684 Make_Subprogram_Renaming_Declaration
(Loc
,
5687 (Specification
(Original_Node
(Gen_Decl
)),
5689 Instantiating
=> True),
5690 Name
=> New_Occurrence_Of
(Anon_Id
, Loc
));
5692 -- The generic may be a child unit. The renaming needs an identifier
5693 -- with the proper name.
5695 Set_Defining_Unit_Name
(Specification
(Unit_Renaming
),
5696 Make_Defining_Identifier
(Loc
, Chars
(Gen_Unit
)));
5698 -- If there is a formal subprogram with the same name as the unit
5699 -- itself, do not add this renaming declaration, to prevent
5700 -- ambiguities when there is a call with that name in the body.
5702 Renaming_Decl
:= First
(Renaming_List
);
5703 while Present
(Renaming_Decl
) loop
5704 if Nkind
(Renaming_Decl
) = N_Subprogram_Renaming_Declaration
5706 Chars
(Defining_Entity
(Renaming_Decl
)) = Chars
(Gen_Unit
)
5711 Next
(Renaming_Decl
);
5714 if No
(Renaming_Decl
) then
5715 Append
(Unit_Renaming
, Renaming_List
);
5717 end Build_Subprogram_Renaming
;
5721 Saved_GM
: constant Ghost_Mode_Type
:= Ghost_Mode
;
5722 Saved_IGR
: constant Node_Id
:= Ignored_Ghost_Region
;
5723 Saved_ISMP
: constant Boolean :=
5724 Ignore_SPARK_Mode_Pragmas_In_Instance
;
5725 Saved_SM
: constant SPARK_Mode_Type
:= SPARK_Mode
;
5726 Saved_SMP
: constant Node_Id
:= SPARK_Mode_Pragma
;
5727 -- Save the Ghost and SPARK mode-related data to restore on exit
5729 Vis_Prims_List
: Elist_Id
:= No_Elist
;
5730 -- List of primitives made temporarily visible in the instantiation
5731 -- to match the visibility of the formal type
5733 -- Start of processing for Analyze_Subprogram_Instantiation
5736 -- Preserve relevant elaboration-related attributes of the context which
5737 -- are no longer available or very expensive to recompute once analysis,
5738 -- resolution, and expansion are over.
5740 Mark_Elaboration_Attributes
5747 -- Very first thing: check for special Text_IO unit in case we are
5748 -- instantiating one of the children of [[Wide_]Wide_]Text_IO. Of course
5749 -- such an instantiation is bogus (these are packages, not subprograms),
5750 -- but we get a better error message if we do this.
5752 Check_Text_IO_Special_Unit
(Gen_Id
);
5754 -- Make node global for error reporting
5756 Instantiation_Node
:= N
;
5758 -- For package instantiations we turn off style checks, because they
5759 -- will have been emitted in the generic. For subprogram instantiations
5760 -- we want to apply at least the check on overriding indicators so we
5761 -- do not modify the style check status.
5763 -- The renaming declarations for the actuals do not come from source and
5764 -- will not generate spurious warnings.
5766 Preanalyze_Actuals
(N
);
5769 Env_Installed
:= True;
5770 Check_Generic_Child_Unit
(Gen_Id
, Parent_Installed
);
5771 Gen_Unit
:= Entity
(Gen_Id
);
5773 -- A subprogram instantiation is Ghost when it is subject to pragma
5774 -- Ghost or the generic template is Ghost. Set the mode now to ensure
5775 -- that any nodes generated during analysis and expansion are marked as
5778 Mark_And_Set_Ghost_Instantiation
(N
, Gen_Unit
);
5780 Generate_Reference
(Gen_Unit
, Gen_Id
);
5782 if Nkind
(Gen_Id
) = N_Identifier
5783 and then Chars
(Gen_Unit
) = Chars
(Defining_Entity
(N
))
5786 ("& is hidden within declaration of instance", Gen_Id
, Gen_Unit
);
5789 if Etype
(Gen_Unit
) = Any_Type
then
5794 -- Verify that it is a generic subprogram of the right kind, and that
5795 -- it does not lead to a circular instantiation.
5797 if K
= E_Procedure
and then Ekind
(Gen_Unit
) /= E_Generic_Procedure
then
5799 ("& is not the name of a generic procedure", Gen_Id
, Gen_Unit
);
5801 elsif K
= E_Function
and then Ekind
(Gen_Unit
) /= E_Generic_Function
then
5803 ("& is not the name of a generic function", Gen_Id
, Gen_Unit
);
5805 elsif In_Open_Scopes
(Gen_Unit
) then
5806 Error_Msg_NE
("instantiation of & within itself", N
, Gen_Unit
);
5809 Mutate_Ekind
(Inst_Id
, K
);
5810 Set_Scope
(Inst_Id
, Current_Scope
);
5812 Set_Entity
(Gen_Id
, Gen_Unit
);
5814 if In_Extended_Main_Source_Unit
(N
) then
5815 Set_Is_Instantiated
(Gen_Unit
);
5816 Generate_Reference
(Gen_Unit
, N
);
5819 -- If renaming, get original unit
5821 if Present
(Renamed_Entity
(Gen_Unit
))
5822 and then Is_Generic_Subprogram
(Renamed_Entity
(Gen_Unit
))
5824 Gen_Unit
:= Renamed_Entity
(Gen_Unit
);
5825 Set_Is_Instantiated
(Gen_Unit
);
5826 Generate_Reference
(Gen_Unit
, N
);
5829 if Contains_Instance_Of
(Gen_Unit
, Current_Scope
, Gen_Id
) then
5830 Error_Msg_Node_2
:= Current_Scope
;
5832 ("circular instantiation: & instantiated in &!", N
, Gen_Unit
);
5833 Circularity_Detected
:= True;
5834 Restore_Hidden_Primitives
(Vis_Prims_List
);
5838 Gen_Decl
:= Unit_Declaration_Node
(Gen_Unit
);
5840 -- Initialize renamings map, for error checking
5842 Generic_Renamings
.Set_Last
(0);
5843 Generic_Renamings_HTable
.Reset
;
5845 Create_Instantiation_Source
(N
, Gen_Unit
, S_Adjustment
);
5847 -- Copy original generic tree, to produce text for instantiation
5851 (Original_Node
(Gen_Decl
), Empty
, Instantiating
=> True);
5853 -- Inherit overriding indicator from instance node
5855 Act_Spec
:= Specification
(Act_Tree
);
5856 Set_Must_Override
(Act_Spec
, Must_Override
(N
));
5857 Set_Must_Not_Override
(Act_Spec
, Must_Not_Override
(N
));
5860 Analyze_Associations
5862 Formals
=> Generic_Formal_Declarations
(Act_Tree
),
5863 F_Copy
=> Generic_Formal_Declarations
(Gen_Decl
));
5865 Vis_Prims_List
:= Check_Hidden_Primitives
(Renaming_List
);
5867 -- The subprogram itself cannot contain a nested instance, so the
5868 -- current parent is left empty.
5870 Set_Instance_Env
(Gen_Unit
, Empty
);
5872 -- Build the subprogram declaration, which does not appear in the
5873 -- generic template, and give it a sloc consistent with that of the
5876 Set_Defining_Unit_Name
(Act_Spec
, Anon_Id
);
5877 Set_Generic_Parent
(Act_Spec
, Gen_Unit
);
5879 Make_Subprogram_Declaration
(Sloc
(Act_Spec
),
5880 Specification
=> Act_Spec
);
5882 -- The aspects have been copied previously, but they have to be
5883 -- linked explicitly to the new subprogram declaration. Explicit
5884 -- pre/postconditions on the instance are analyzed below, in a
5887 Move_Aspects
(Act_Tree
, To
=> Act_Decl
);
5888 Set_Categorization_From_Pragmas
(Act_Decl
);
5890 if Parent_Installed
then
5894 Append
(Act_Decl
, Renaming_List
);
5896 -- Contract-related source pragmas that follow a generic subprogram
5897 -- must be instantiated explicitly because they are not part of the
5898 -- subprogram template.
5900 Instantiate_Subprogram_Contract
5901 (Original_Node
(Gen_Decl
), Renaming_List
);
5903 Build_Subprogram_Renaming
;
5905 -- If the context of the instance is subject to SPARK_Mode "off" or
5906 -- the annotation is altogether missing, set the global flag which
5907 -- signals Analyze_Pragma to ignore all SPARK_Mode pragmas within
5908 -- the instance. This should be done prior to analyzing the instance.
5910 if SPARK_Mode
/= On
then
5911 Ignore_SPARK_Mode_Pragmas_In_Instance
:= True;
5914 -- If the context of an instance is not subject to SPARK_Mode "off",
5915 -- and the generic spec is subject to an explicit SPARK_Mode pragma,
5916 -- the latter should be the one applicable to the instance.
5918 if not Ignore_SPARK_Mode_Pragmas_In_Instance
5919 and then Saved_SM
/= Off
5920 and then Present
(SPARK_Pragma
(Gen_Unit
))
5922 Set_SPARK_Mode
(Gen_Unit
);
5925 -- Need to mark Anon_Id intrinsic before calling
5926 -- Analyze_Instance_And_Renamings because this flag may be propagated
5929 if Is_Intrinsic_Subprogram
(Gen_Unit
) then
5930 Set_Is_Intrinsic_Subprogram
(Anon_Id
);
5931 Set_Interface_Name
(Anon_Id
, Interface_Name
(Gen_Unit
));
5934 Analyze_Instance_And_Renamings
;
5936 -- Restore SPARK_Mode from the context after analysis of the package
5937 -- declaration, so that the SPARK_Mode on the generic spec does not
5938 -- apply to the pending instance for the instance body.
5940 if not Ignore_SPARK_Mode_Pragmas_In_Instance
5941 and then Saved_SM
/= Off
5942 and then Present
(SPARK_Pragma
(Gen_Unit
))
5944 Restore_SPARK_Mode
(Saved_SM
, Saved_SMP
);
5947 -- If the generic is marked Import (Intrinsic), then so is the
5948 -- instance; this indicates that there is no body to instantiate.
5949 -- We also copy the interface name in case this is handled by the
5950 -- back-end and deal with an instance of unchecked conversion.
5952 if Is_Intrinsic_Subprogram
(Gen_Unit
) then
5953 Set_Is_Intrinsic_Subprogram
(Act_Decl_Id
);
5954 Set_Interface_Name
(Act_Decl_Id
, Interface_Name
(Gen_Unit
));
5956 if Chars
(Gen_Unit
) = Name_Unchecked_Conversion
then
5957 Validate_Unchecked_Conversion
(N
, Act_Decl_Id
);
5961 -- Inherit convention from generic unit. Intrinsic convention, as for
5962 -- an instance of unchecked conversion, is not inherited because an
5963 -- explicit Ada instance has been created.
5965 if Has_Convention_Pragma
(Gen_Unit
)
5966 and then Convention
(Gen_Unit
) /= Convention_Intrinsic
5968 Set_Convention
(Act_Decl_Id
, Convention
(Gen_Unit
));
5969 Set_Is_Exported
(Act_Decl_Id
, Is_Exported
(Gen_Unit
));
5972 Generate_Definition
(Act_Decl_Id
);
5974 -- Inherit all inlining-related flags which apply to the generic in
5975 -- the subprogram and its declaration.
5977 Set_Is_Inlined
(Act_Decl_Id
, Is_Inlined
(Gen_Unit
));
5978 Set_Is_Inlined
(Anon_Id
, Is_Inlined
(Gen_Unit
));
5980 Set_Has_Pragma_Inline
(Act_Decl_Id
, Has_Pragma_Inline
(Gen_Unit
));
5981 Set_Has_Pragma_Inline
(Anon_Id
, Has_Pragma_Inline
(Gen_Unit
));
5983 Set_Has_Pragma_Inline_Always
5984 (Act_Decl_Id
, Has_Pragma_Inline_Always
(Gen_Unit
));
5985 Set_Has_Pragma_Inline_Always
5986 (Anon_Id
, Has_Pragma_Inline_Always
(Gen_Unit
));
5988 Set_Has_Pragma_No_Inline
5989 (Act_Decl_Id
, Has_Pragma_No_Inline
(Gen_Unit
));
5990 Set_Has_Pragma_No_Inline
5991 (Anon_Id
, Has_Pragma_No_Inline
(Gen_Unit
));
5993 -- Propagate No_Return if pragma applied to generic unit. This must
5994 -- be done explicitly because pragma does not appear in generic
5995 -- declaration (unlike the aspect case).
5997 if No_Return
(Gen_Unit
) then
5998 Set_No_Return
(Act_Decl_Id
);
5999 Set_No_Return
(Anon_Id
);
6002 -- Mark both the instance spec and the anonymous package in case the
6003 -- body is instantiated at a later pass. This preserves the original
6004 -- context in effect for the body.
6006 if SPARK_Mode
/= On
then
6007 Set_Ignore_SPARK_Mode_Pragmas
(Act_Decl_Id
);
6008 Set_Ignore_SPARK_Mode_Pragmas
(Anon_Id
);
6011 if Legacy_Elaboration_Checks
6012 and then not Is_Intrinsic_Subprogram
(Gen_Unit
)
6014 Check_Elab_Instantiation
(N
);
6017 -- Save the scenario for later examination by the ABE Processing
6020 Record_Elaboration_Scenario
(N
);
6022 -- The instantiation results in a guaranteed ABE. Create a completing
6023 -- body for the subprogram declaration because the real body will not
6026 if Is_Known_Guaranteed_ABE
(N
) then
6027 Provide_Completing_Bodies
(Instance_Spec
(N
));
6030 if Is_Dispatching_Operation
(Act_Decl_Id
)
6031 and then Ada_Version
>= Ada_2005
6037 Formal
:= First_Formal
(Act_Decl_Id
);
6038 while Present
(Formal
) loop
6039 if Ekind
(Etype
(Formal
)) = E_Anonymous_Access_Type
6040 and then Is_Controlling_Formal
(Formal
)
6041 and then not Can_Never_Be_Null
(Formal
)
6044 ("access parameter& is controlling,", N
, Formal
);
6046 ("\corresponding parameter of & must be explicitly "
6047 & "null-excluding", N
, Gen_Id
);
6050 Next_Formal
(Formal
);
6055 Check_Hidden_Child_Unit
(N
, Gen_Unit
, Act_Decl_Id
);
6057 Validate_Categorization_Dependency
(N
, Act_Decl_Id
);
6059 if not Is_Intrinsic_Subprogram
(Act_Decl_Id
) then
6060 Inherit_Context
(Gen_Decl
, N
);
6062 Restore_Private_Views
(Pack_Id
, False);
6064 -- If the context requires a full instantiation, mark node for
6065 -- subsequent construction of the body.
6067 if Need_Subprogram_Instance_Body
(N
, Act_Decl_Id
) then
6068 Check_Forward_Instantiation
(Gen_Decl
);
6070 -- The wrapper package is always delayed, because it does not
6071 -- constitute a freeze point, but to insure that the freeze node
6072 -- is placed properly, it is created directly when instantiating
6073 -- the body (otherwise the freeze node might appear to early for
6074 -- nested instantiations).
6076 elsif Nkind
(Parent
(N
)) = N_Compilation_Unit
then
6077 Rewrite
(N
, Unit
(Parent
(N
)));
6078 Set_Unit
(Parent
(N
), N
);
6081 -- Replace instance node for library-level instantiations of
6082 -- intrinsic subprograms.
6084 elsif Nkind
(Parent
(N
)) = N_Compilation_Unit
then
6085 Rewrite
(N
, Unit
(Parent
(N
)));
6086 Set_Unit
(Parent
(N
), N
);
6089 if Parent_Installed
then
6093 Restore_Hidden_Primitives
(Vis_Prims_List
);
6095 Env_Installed
:= False;
6096 Generic_Renamings
.Set_Last
(0);
6097 Generic_Renamings_HTable
.Reset
;
6101 -- Analyze aspects in declaration if no errors appear in the instance.
6103 if Has_Aspects
(N
) and then Serious_Errors_Detected
= Errs
then
6104 Analyze_Aspect_Specifications
(N
, Act_Decl_Id
);
6107 Ignore_SPARK_Mode_Pragmas_In_Instance
:= Saved_ISMP
;
6108 Restore_Ghost_Region
(Saved_GM
, Saved_IGR
);
6109 Restore_SPARK_Mode
(Saved_SM
, Saved_SMP
);
6112 when Instantiation_Error
=>
6113 if Parent_Installed
then
6117 if Env_Installed
then
6121 Ignore_SPARK_Mode_Pragmas_In_Instance
:= Saved_ISMP
;
6122 Restore_Ghost_Region
(Saved_GM
, Saved_IGR
);
6123 Restore_SPARK_Mode
(Saved_SM
, Saved_SMP
);
6124 end Analyze_Subprogram_Instantiation
;
6126 ---------------------------
6127 -- Get_Associated_Entity --
6128 ---------------------------
6130 function Get_Associated_Entity
(Id
: Entity_Id
) return Entity_Id
is
6134 Assoc
:= Associated_Entity
(Id
);
6136 if Present
(Assoc
) then
6137 while Present
(Associated_Entity
(Assoc
)) loop
6138 Assoc
:= Associated_Entity
(Assoc
);
6143 end Get_Associated_Entity
;
6145 -------------------------
6146 -- Get_Associated_Node --
6147 -------------------------
6149 function Get_Associated_Node
(N
: Node_Id
) return Node_Id
is
6153 Assoc
:= Associated_Node
(N
);
6155 if Nkind
(Assoc
) /= Nkind
(N
) then
6158 elsif Nkind
(Assoc
) in N_Aggregate | N_Extension_Aggregate
then
6162 -- If the node is part of an inner generic, it may itself have been
6163 -- remapped into a further generic copy. Associated_Node is otherwise
6164 -- used for the entity of the node, and will be of a different node
6165 -- kind, or else N has been rewritten as a literal or function call.
6167 while Present
(Associated_Node
(Assoc
))
6168 and then Nkind
(Associated_Node
(Assoc
)) = Nkind
(Assoc
)
6170 Assoc
:= Associated_Node
(Assoc
);
6173 -- Follow an additional link in case the final node was rewritten.
6174 -- This can only happen with nested generic units.
6176 if (Nkind
(Assoc
) = N_Identifier
or else Nkind
(Assoc
) in N_Op
)
6177 and then Present
(Associated_Node
(Assoc
))
6178 and then Nkind
(Associated_Node
(Assoc
)) in N_Function_Call
6179 | N_Explicit_Dereference
6184 Assoc
:= Associated_Node
(Assoc
);
6187 -- An additional special case: an unconstrained type in an object
6188 -- declaration may have been rewritten as a local subtype constrained
6189 -- by the expression in the declaration. We need to recover the
6190 -- original entity, which may be global.
6192 if Present
(Original_Node
(Assoc
))
6193 and then Nkind
(Parent
(N
)) = N_Object_Declaration
6195 Assoc
:= Original_Node
(Assoc
);
6200 end Get_Associated_Node
;
6202 -----------------------------------
6203 -- Build_Subprogram_Decl_Wrapper --
6204 -----------------------------------
6206 function Build_Subprogram_Decl_Wrapper
6207 (Formal_Subp
: Entity_Id
) return Node_Id
6209 Loc
: constant Source_Ptr
:= Sloc
(Current_Scope
);
6210 Ret_Type
: constant Entity_Id
:= Get_Instance_Of
(Etype
(Formal_Subp
));
6213 Parm_Spec
: Node_Id
;
6214 Profile
: List_Id
:= New_List
;
6221 Subp
:= Make_Defining_Identifier
(Loc
, Chars
(Formal_Subp
));
6222 Mutate_Ekind
(Subp
, Ekind
(Formal_Subp
));
6223 Set_Is_Generic_Actual_Subprogram
(Subp
);
6225 Profile
:= Parameter_Specifications
(
6227 (Specification
(Unit_Declaration_Node
(Formal_Subp
))));
6229 Form_F
:= First_Formal
(Formal_Subp
);
6230 Parm_Spec
:= First
(Profile
);
6232 -- Create new entities for the formals. Reset entities so that
6233 -- parameter types are properly resolved when wrapper declaration
6236 while Present
(Parm_Spec
) loop
6237 New_F
:= Make_Defining_Identifier
(Loc
, Chars
(Form_F
));
6238 Set_Defining_Identifier
(Parm_Spec
, New_F
);
6239 Set_Entity
(Parameter_Type
(Parm_Spec
), Empty
);
6241 Next_Formal
(Form_F
);
6244 if Ret_Type
= Standard_Void_Type
then
6246 Make_Procedure_Specification
(Loc
,
6247 Defining_Unit_Name
=> Subp
,
6248 Parameter_Specifications
=> Profile
);
6251 Make_Function_Specification
(Loc
,
6252 Defining_Unit_Name
=> Subp
,
6253 Parameter_Specifications
=> Profile
,
6254 Result_Definition
=> New_Occurrence_Of
(Ret_Type
, Loc
));
6258 Make_Subprogram_Declaration
(Loc
, Specification
=> Spec
);
6261 end Build_Subprogram_Decl_Wrapper
;
6263 -----------------------------------
6264 -- Build_Subprogram_Body_Wrapper --
6265 -----------------------------------
6267 function Build_Subprogram_Body_Wrapper
6268 (Formal_Subp
: Entity_Id
;
6269 Actual_Name
: Node_Id
) return Node_Id
6271 Loc
: constant Source_Ptr
:= Sloc
(Current_Scope
);
6272 Ret_Type
: constant Entity_Id
:= Get_Instance_Of
(Etype
(Formal_Subp
));
6273 Spec_Node
: constant Node_Id
:=
6275 (Build_Subprogram_Decl_Wrapper
(Formal_Subp
));
6278 Body_Node
: Node_Id
;
6281 Actuals
:= New_List
;
6282 Act
:= First
(Parameter_Specifications
(Spec_Node
));
6284 while Present
(Act
) loop
6286 Make_Identifier
(Loc
, Chars
(Defining_Identifier
(Act
))));
6290 if Ret_Type
= Standard_Void_Type
then
6291 Stmt
:= Make_Procedure_Call_Statement
(Loc
,
6292 Name
=> Actual_Name
,
6293 Parameter_Associations
=> Actuals
);
6296 Stmt
:= Make_Simple_Return_Statement
(Loc
,
6298 Make_Function_Call
(Loc
,
6299 Name
=> Actual_Name
,
6300 Parameter_Associations
=> Actuals
));
6303 Body_Node
:= Make_Subprogram_Body
(Loc
,
6304 Specification
=> Spec_Node
,
6305 Declarations
=> New_List
,
6306 Handled_Statement_Sequence
=>
6307 Make_Handled_Sequence_Of_Statements
(Loc
,
6308 Statements
=> New_List
(Stmt
)));
6311 end Build_Subprogram_Body_Wrapper
;
6313 -------------------------------------------
6314 -- Build_Instance_Compilation_Unit_Nodes --
6315 -------------------------------------------
6317 procedure Build_Instance_Compilation_Unit_Nodes
6322 Decl_Cunit
: Node_Id
;
6323 Body_Cunit
: Node_Id
;
6325 New_Main
: constant Entity_Id
:= Defining_Entity
(Act_Decl
);
6326 Old_Main
: constant Entity_Id
:= Cunit_Entity
(Main_Unit
);
6329 -- A new compilation unit node is built for the instance declaration.
6330 -- It relocates the auxiliary declaration node from the compilation unit
6331 -- where the instance appeared, so that declarations that originally
6332 -- followed the instance will be attached to the spec compilation unit.
6335 Make_Compilation_Unit
(Sloc
(N
),
6336 Context_Items
=> Empty_List
,
6338 Aux_Decls_Node
=> Relocate_Node
(Aux_Decls_Node
(Parent
(N
))));
6340 Set_Parent_Spec
(Act_Decl
, Parent_Spec
(N
));
6342 -- The new compilation unit is linked to its body, but both share the
6343 -- same file, so we do not set Body_Required on the new unit so as not
6344 -- to create a spurious dependency on a non-existent body in the ali.
6345 -- This simplifies CodePeer unit traversal.
6347 -- We use the original instantiation compilation unit as the resulting
6348 -- compilation unit of the instance, since this is the main unit.
6350 Rewrite
(N
, Act_Body
);
6352 Body_Cunit
:= Parent
(N
);
6354 -- The two compilation unit nodes are linked by the Library_Unit field
6356 Set_Library_Unit
(Decl_Cunit
, Body_Cunit
);
6357 Set_Library_Unit
(Body_Cunit
, Decl_Cunit
);
6359 -- Preserve the private nature of the package if needed
6361 Set_Private_Present
(Decl_Cunit
, Private_Present
(Body_Cunit
));
6363 -- If the instance is not the main unit, its context, categorization
6364 -- and elaboration entity are not relevant to the compilation.
6366 if Body_Cunit
/= Cunit
(Main_Unit
) then
6367 Make_Instance_Unit
(Body_Cunit
, In_Main
=> False);
6371 -- The context clause items on the instantiation, which are now attached
6372 -- to the body compilation unit (since the body overwrote the original
6373 -- instantiation node), semantically belong on the spec, so copy them
6374 -- there. It's harmless to leave them on the body as well. In fact one
6375 -- could argue that they belong in both places.
6377 Citem
:= First
(Context_Items
(Body_Cunit
));
6378 while Present
(Citem
) loop
6379 Append
(New_Copy
(Citem
), Context_Items
(Decl_Cunit
));
6383 -- Propagate categorization flags on packages, so that they appear in
6384 -- the ali file for the spec of the unit.
6386 if Ekind
(New_Main
) = E_Package
then
6387 Set_Is_Pure
(Old_Main
, Is_Pure
(New_Main
));
6388 Set_Is_Preelaborated
(Old_Main
, Is_Preelaborated
(New_Main
));
6389 Set_Is_Remote_Types
(Old_Main
, Is_Remote_Types
(New_Main
));
6390 Set_Is_Shared_Passive
(Old_Main
, Is_Shared_Passive
(New_Main
));
6391 Set_Is_Remote_Call_Interface
6392 (Old_Main
, Is_Remote_Call_Interface
(New_Main
));
6395 -- Make entry in Units table, so that binder can generate call to
6396 -- elaboration procedure for body, if any.
6398 Make_Instance_Unit
(Body_Cunit
, In_Main
=> True);
6399 Main_Unit_Entity
:= New_Main
;
6400 Set_Cunit_Entity
(Main_Unit
, Main_Unit_Entity
);
6402 -- Build elaboration entity, since the instance may certainly generate
6403 -- elaboration code requiring a flag for protection.
6405 Build_Elaboration_Entity
(Decl_Cunit
, New_Main
);
6406 end Build_Instance_Compilation_Unit_Nodes
;
6408 --------------------------------
6409 -- Check_Abbreviated_Instance --
6410 --------------------------------
6412 procedure Check_Abbreviated_Instance
6414 Parent_Installed
: in out Boolean)
6416 Inst_Node
: Node_Id
;
6419 if Nkind
(N
) = N_Package_Specification
6420 and then Is_Abbreviated_Instance
(Defining_Entity
(N
))
6422 Inst_Node
:= Get_Unit_Instantiation_Node
(Defining_Entity
(N
));
6423 Check_Generic_Child_Unit
(Name
(Inst_Node
), Parent_Installed
);
6425 end Check_Abbreviated_Instance
;
6427 -----------------------------
6428 -- Check_Access_Definition --
6429 -----------------------------
6431 procedure Check_Access_Definition
(N
: Node_Id
) is
6434 (Ada_Version
>= Ada_2005
and then Present
(Access_Definition
(N
)));
6436 end Check_Access_Definition
;
6438 -----------------------------------
6439 -- Check_Formal_Package_Instance --
6440 -----------------------------------
6442 -- If the formal has specific parameters, they must match those of the
6443 -- actual. Both of them are instances, and the renaming declarations for
6444 -- their formal parameters appear in the same order in both. The analyzed
6445 -- formal has been analyzed in the context of the current instance.
6447 procedure Check_Formal_Package_Instance
6448 (Formal_Pack
: Entity_Id
;
6449 Actual_Pack
: Entity_Id
)
6451 E1
: Entity_Id
:= First_Entity
(Actual_Pack
);
6452 E2
: Entity_Id
:= First_Entity
(Formal_Pack
);
6453 Prev_E1
: Entity_Id
;
6458 procedure Check_Mismatch
(B
: Boolean);
6459 -- Common error routine for mismatch between the parameters of the
6460 -- actual instance and those of the formal package.
6462 function Is_Defaulted
(Param
: Entity_Id
) return Boolean;
6463 -- If the formal package has partly box-initialized formals, skip
6464 -- conformance check for these formals. Previously the code assumed
6465 -- that box initialization for a formal package applied to all its
6466 -- formal parameters.
6468 function Same_Instantiated_Constant
(E1
, E2
: Entity_Id
) return Boolean;
6469 -- The formal may come from a nested formal package, and the actual may
6470 -- have been constant-folded. To determine whether the two denote the
6471 -- same entity we may have to traverse several definitions to recover
6472 -- the ultimate entity that they refer to.
6474 function Same_Instantiated_Function
(E1
, E2
: Entity_Id
) return Boolean;
6475 -- The formal and the actual must be identical, but if both are
6476 -- given by attributes they end up renaming different generated bodies,
6477 -- and we must verify that the attributes themselves match.
6479 function Same_Instantiated_Variable
(E1
, E2
: Entity_Id
) return Boolean;
6480 -- Similarly, if the formal comes from a nested formal package, the
6481 -- actual may designate the formal through multiple renamings, which
6482 -- have to be followed to determine the original variable in question.
6484 --------------------
6485 -- Check_Mismatch --
6486 --------------------
6488 procedure Check_Mismatch
(B
: Boolean) is
6489 -- A Formal_Type_Declaration for a derived private type is rewritten
6490 -- as a private extension decl. (see Analyze_Formal_Derived_Type),
6491 -- which is why we examine the original node.
6493 Kind
: constant Node_Kind
:= Nkind
(Original_Node
(Parent
(E2
)));
6496 if Kind
= N_Formal_Type_Declaration
then
6499 elsif Kind
in N_Formal_Object_Declaration
6500 | N_Formal_Package_Declaration
6501 | N_Formal_Subprogram_Declaration
6505 -- Ada 2012: If both formal and actual are incomplete types they
6508 elsif Is_Incomplete_Type
(E1
) and then Is_Incomplete_Type
(E2
) then
6513 ("actual for & in actual instance does not match formal",
6514 Parent
(Actual_Pack
), E1
);
6522 function Is_Defaulted
(Param
: Entity_Id
) return Boolean is
6527 First
(Generic_Associations
(Parent
6528 (Associated_Formal_Package
(Actual_Pack
))));
6530 while Present
(Assoc
) loop
6531 if Nkind
(Assoc
) = N_Others_Choice
then
6534 elsif Nkind
(Assoc
) = N_Generic_Association
6535 and then Chars
(Selector_Name
(Assoc
)) = Chars
(Param
)
6537 return Box_Present
(Assoc
);
6546 --------------------------------
6547 -- Same_Instantiated_Constant --
6548 --------------------------------
6550 function Same_Instantiated_Constant
6551 (E1
, E2
: Entity_Id
) return Boolean
6557 while Present
(Ent
) loop
6561 elsif Ekind
(Ent
) /= E_Constant
then
6564 elsif Is_Entity_Name
(Constant_Value
(Ent
)) then
6565 if Entity
(Constant_Value
(Ent
)) = E1
then
6568 Ent
:= Entity
(Constant_Value
(Ent
));
6571 -- The actual may be a constant that has been folded. Recover
6574 elsif Is_Entity_Name
(Original_Node
(Constant_Value
(Ent
))) then
6575 Ent
:= Entity
(Original_Node
(Constant_Value
(Ent
)));
6583 end Same_Instantiated_Constant
;
6585 --------------------------------
6586 -- Same_Instantiated_Function --
6587 --------------------------------
6589 function Same_Instantiated_Function
6590 (E1
, E2
: Entity_Id
) return Boolean
6594 if Alias
(E1
) = Alias
(E2
) then
6597 elsif Present
(Alias
(E2
)) then
6598 U1
:= Original_Node
(Unit_Declaration_Node
(E1
));
6599 U2
:= Original_Node
(Unit_Declaration_Node
(Alias
(E2
)));
6601 return Nkind
(U1
) = N_Subprogram_Renaming_Declaration
6602 and then Nkind
(Name
(U1
)) = N_Attribute_Reference
6604 and then Nkind
(U2
) = N_Subprogram_Renaming_Declaration
6605 and then Nkind
(Name
(U2
)) = N_Attribute_Reference
6608 Attribute_Name
(Name
(U1
)) = Attribute_Name
(Name
(U2
));
6612 end Same_Instantiated_Function
;
6614 --------------------------------
6615 -- Same_Instantiated_Variable --
6616 --------------------------------
6618 function Same_Instantiated_Variable
6619 (E1
, E2
: Entity_Id
) return Boolean
6621 function Original_Entity
(E
: Entity_Id
) return Entity_Id
;
6622 -- Follow chain of renamings to the ultimate ancestor
6624 ---------------------
6625 -- Original_Entity --
6626 ---------------------
6628 function Original_Entity
(E
: Entity_Id
) return Entity_Id
is
6633 while Nkind
(Parent
(Orig
)) = N_Object_Renaming_Declaration
6634 and then Present
(Renamed_Object
(Orig
))
6635 and then Is_Entity_Name
(Renamed_Object
(Orig
))
6637 Orig
:= Entity
(Renamed_Object
(Orig
));
6641 end Original_Entity
;
6643 -- Start of processing for Same_Instantiated_Variable
6646 return Ekind
(E1
) = Ekind
(E2
)
6647 and then Original_Entity
(E1
) = Original_Entity
(E2
);
6648 end Same_Instantiated_Variable
;
6650 -- Start of processing for Check_Formal_Package_Instance
6654 while Present
(E1
) and then Present
(E2
) loop
6655 exit when Ekind
(E1
) = E_Package
6656 and then Renamed_Entity
(E1
) = Renamed_Entity
(Actual_Pack
);
6658 -- If the formal is the renaming of the formal package, this
6659 -- is the end of its formal part, which may occur before the
6660 -- end of the formal part in the actual in the presence of
6661 -- defaulted parameters in the formal package.
6663 exit when Nkind
(Parent
(E2
)) = N_Package_Renaming_Declaration
6664 and then Renamed_Entity
(E2
) = Scope
(E2
);
6666 -- The analysis of the actual may generate additional internal
6667 -- entities. If the formal is defaulted, there is no corresponding
6668 -- analysis and the internal entities must be skipped, until we
6669 -- find corresponding entities again.
6671 if Comes_From_Source
(E2
)
6672 and then not Comes_From_Source
(E1
)
6673 and then Chars
(E1
) /= Chars
(E2
)
6675 while Present
(E1
) and then Chars
(E1
) /= Chars
(E2
) loop
6683 -- Entities may be declared without full declaration, such as
6684 -- itypes and predefined operators (concatenation for arrays, eg).
6685 -- Skip it and keep the formal entity to find a later match for it.
6687 elsif No
(Parent
(E2
)) and then Ekind
(E1
) /= Ekind
(E2
) then
6691 -- If the formal entity comes from a formal declaration, it was
6692 -- defaulted in the formal package, and no check is needed on it.
6694 elsif Nkind
(Original_Node
(Parent
(E2
))) in
6695 N_Formal_Object_Declaration | N_Formal_Type_Declaration
6697 -- If the formal is a tagged type the corresponding class-wide
6698 -- type has been generated as well, and it must be skipped.
6700 if Is_Type
(E2
) and then Is_Tagged_Type
(E2
) then
6706 -- Ditto for defaulted formal subprograms.
6708 elsif Is_Overloadable
(E1
)
6709 and then Nkind
(Unit_Declaration_Node
(E2
)) in
6710 N_Formal_Subprogram_Declaration
6714 elsif Is_Defaulted
(E1
) then
6717 elsif Is_Type
(E1
) then
6719 -- Subtypes must statically match. E1, E2 are the local entities
6720 -- that are subtypes of the actuals. Itypes generated for other
6721 -- parameters need not be checked, the check will be performed
6722 -- on the parameters themselves.
6724 -- If E2 is a formal type declaration, it is a defaulted parameter
6725 -- and needs no checking.
6727 if not Is_Itype
(E1
) and then not Is_Itype
(E2
) then
6730 or else Etype
(E1
) /= Etype
(E2
)
6731 or else not Subtypes_Statically_Match
(E1
, E2
));
6734 elsif Ekind
(E1
) = E_Constant
then
6736 -- IN parameters must denote the same static value, or the same
6737 -- constant, or the literal null.
6739 Expr1
:= Expression
(Parent
(E1
));
6741 if Ekind
(E2
) /= E_Constant
then
6742 Check_Mismatch
(True);
6745 Expr2
:= Expression
(Parent
(E2
));
6748 if Is_OK_Static_Expression
(Expr1
) then
6749 if not Is_OK_Static_Expression
(Expr2
) then
6750 Check_Mismatch
(True);
6752 elsif Is_Discrete_Type
(Etype
(E1
)) then
6754 V1
: constant Uint
:= Expr_Value
(Expr1
);
6755 V2
: constant Uint
:= Expr_Value
(Expr2
);
6757 Check_Mismatch
(V1
/= V2
);
6760 elsif Is_Real_Type
(Etype
(E1
)) then
6762 V1
: constant Ureal
:= Expr_Value_R
(Expr1
);
6763 V2
: constant Ureal
:= Expr_Value_R
(Expr2
);
6765 Check_Mismatch
(V1
/= V2
);
6768 elsif Is_String_Type
(Etype
(E1
))
6769 and then Nkind
(Expr1
) = N_String_Literal
6771 if Nkind
(Expr2
) /= N_String_Literal
then
6772 Check_Mismatch
(True);
6775 (not String_Equal
(Strval
(Expr1
), Strval
(Expr2
)));
6779 elsif Is_Entity_Name
(Expr1
) then
6780 if Is_Entity_Name
(Expr2
) then
6781 if Entity
(Expr1
) = Entity
(Expr2
) then
6785 (not Same_Instantiated_Constant
6786 (Entity
(Expr1
), Entity
(Expr2
)));
6790 Check_Mismatch
(True);
6793 elsif Is_Entity_Name
(Original_Node
(Expr1
))
6794 and then Is_Entity_Name
(Expr2
)
6795 and then Same_Instantiated_Constant
6796 (Entity
(Original_Node
(Expr1
)), Entity
(Expr2
))
6800 elsif Nkind
(Expr1
) = N_Null
then
6801 Check_Mismatch
(Nkind
(Expr1
) /= N_Null
);
6804 Check_Mismatch
(True);
6807 elsif Ekind
(E1
) = E_Variable
then
6808 Check_Mismatch
(not Same_Instantiated_Variable
(E1
, E2
));
6810 elsif Ekind
(E1
) = E_Package
then
6812 (Ekind
(E1
) /= Ekind
(E2
)
6813 or else (Present
(Renamed_Entity
(E2
))
6814 and then Renamed_Entity
(E1
) /=
6815 Renamed_Entity
(E2
)));
6817 elsif Is_Overloadable
(E1
) then
6818 -- Verify that the actual subprograms match. Note that actuals
6819 -- that are attributes are rewritten as subprograms. If the
6820 -- subprogram in the formal package is defaulted, no check is
6821 -- needed. Note that this can only happen in Ada 2005 when the
6822 -- formal package can be partially parameterized.
6824 if Nkind
(Unit_Declaration_Node
(E1
)) =
6825 N_Subprogram_Renaming_Declaration
6826 and then From_Default
(Unit_Declaration_Node
(E1
))
6830 -- If the formal package has an "others" box association that
6831 -- covers this formal, there is no need for a check either.
6833 elsif Nkind
(Unit_Declaration_Node
(E2
)) in
6834 N_Formal_Subprogram_Declaration
6835 and then Box_Present
(Unit_Declaration_Node
(E2
))
6839 -- No check needed if subprogram is a defaulted null procedure
6841 elsif No
(Alias
(E2
))
6842 and then Ekind
(E2
) = E_Procedure
6844 Null_Present
(Specification
(Unit_Declaration_Node
(E2
)))
6848 -- Otherwise the actual in the formal and the actual in the
6849 -- instantiation of the formal must match, up to renamings.
6853 (Ekind
(E2
) /= Ekind
(E1
)
6854 or else not Same_Instantiated_Function
(E1
, E2
));
6858 raise Program_Error
;
6866 end Check_Formal_Package_Instance
;
6868 ---------------------------
6869 -- Check_Formal_Packages --
6870 ---------------------------
6872 procedure Check_Formal_Packages
(P_Id
: Entity_Id
) is
6874 Formal_P
: Entity_Id
;
6875 Formal_Decl
: Node_Id
;
6878 -- Iterate through the declarations in the instance, looking for package
6879 -- renaming declarations that denote instances of formal packages, until
6880 -- we find the renaming of the current package itself. The declaration
6881 -- of a formal package that requires conformance checking is followed by
6882 -- an internal entity that is the abbreviated instance.
6884 E
:= First_Entity
(P_Id
);
6885 while Present
(E
) loop
6886 if Ekind
(E
) = E_Package
then
6887 exit when Renamed_Entity
(E
) = P_Id
;
6889 if Nkind
(Parent
(E
)) = N_Package_Renaming_Declaration
then
6890 Formal_Decl
:= Parent
(Associated_Formal_Package
(E
));
6892 if Requires_Conformance_Checking
(Formal_Decl
) then
6893 Formal_P
:= Next_Entity
(E
);
6895 -- If the instance is within an enclosing instance body
6896 -- there is no need to verify the legality of current formal
6897 -- packages because they were legal in the generic body.
6898 -- This optimization may be applicable elsewhere, and it
6899 -- also removes spurious errors that may arise with
6900 -- on-the-fly inlining and confusion between private and
6903 if not In_Instance_Body
then
6904 Check_Formal_Package_Instance
(Formal_P
, E
);
6907 -- Restore the visibility of formals of the formal instance
6908 -- that are not defaulted, and are hidden within the current
6909 -- generic. These formals may be visible within an enclosing
6915 Elmt
:= First_Elmt
(Hidden_In_Formal_Instance
(Formal_P
));
6916 while Present
(Elmt
) loop
6917 Set_Is_Hidden
(Node
(Elmt
), False);
6922 -- After checking, remove the internal validating package.
6923 -- It is only needed for semantic checks, and as it may
6924 -- contain generic formal declarations it should not reach
6927 Remove
(Unit_Declaration_Node
(Formal_P
));
6934 end Check_Formal_Packages
;
6936 ---------------------------------
6937 -- Check_Forward_Instantiation --
6938 ---------------------------------
6940 procedure Check_Forward_Instantiation
(Decl
: Node_Id
) is
6942 Gen_Comp
: Entity_Id
:= Cunit_Entity
(Get_Source_Unit
(Decl
));
6945 -- The instantiation appears before the generic body if we are in the
6946 -- scope of the unit containing the generic, either in its spec or in
6947 -- the package body, and before the generic body.
6949 if Ekind
(Gen_Comp
) = E_Package_Body
then
6950 Gen_Comp
:= Spec_Entity
(Gen_Comp
);
6953 if In_Open_Scopes
(Gen_Comp
)
6954 and then No
(Corresponding_Body
(Decl
))
6959 and then not Is_Compilation_Unit
(S
)
6960 and then not Is_Child_Unit
(S
)
6962 if Ekind
(S
) = E_Package
then
6963 Set_Has_Forward_Instantiation
(S
);
6969 end Check_Forward_Instantiation
;
6971 ---------------------------
6972 -- Check_Generic_Actuals --
6973 ---------------------------
6975 -- The visibility of the actuals may be different between the point of
6976 -- generic instantiation and the instantiation of the body.
6978 procedure Check_Generic_Actuals
6979 (Instance
: Entity_Id
;
6980 Is_Formal_Box
: Boolean)
6982 Gen_Id
: constant Entity_Id
6983 := (if Is_Generic_Unit
(Instance
) then
6985 elsif Is_Wrapper_Package
(Instance
) then
6988 (Unit_Declaration_Node
(Related_Instance
(Instance
))))
6990 Generic_Parent
(Package_Specification
(Instance
)));
6993 Parent_Scope
: constant Entity_Id
:= Scope
(Gen_Id
);
6994 -- The enclosing scope of the generic unit
6996 procedure Check_Actual_Type
(Typ
: Entity_Id
);
6997 -- If the type of the actual is a private type declared in the enclosing
6998 -- scope of the generic, either directly or through packages nested in
6999 -- bodies, but not a derived type of a private type declared elsewhere,
7000 -- then the body of the generic sees the full view of the type because
7001 -- it has to appear in the package body. If the type is private now then
7002 -- exchange views to restore the proper visibility in the instance.
7004 -----------------------
7005 -- Check_Actual_Type --
7006 -----------------------
7008 procedure Check_Actual_Type
(Typ
: Entity_Id
) is
7009 Btyp
: constant Entity_Id
:= Base_Type
(Typ
);
7011 function Scope_Within_Body_Or_Same
7013 Outer
: Entity_Id
) return Boolean;
7014 -- Determine whether scope Inner is within the body of scope Outer
7015 -- or is Outer itself.
7017 -------------------------------
7018 -- Scope_Within_Body_Or_Same --
7019 -------------------------------
7021 function Scope_Within_Body_Or_Same
7023 Outer
: Entity_Id
) return Boolean
7025 Curr
: Entity_Id
:= Inner
;
7028 while Curr
/= Standard_Standard
loop
7029 if Curr
= Outer
then
7032 elsif Is_Package_Body_Entity
(Curr
) then
7033 Curr
:= Scope
(Curr
);
7041 end Scope_Within_Body_Or_Same
;
7044 -- The exchange is only needed if the generic is defined
7045 -- within a package which is not a common ancestor of the
7046 -- scope of the instance, and is not already in scope.
7048 if Is_Private_Type
(Btyp
)
7049 and then not Has_Private_Ancestor
(Btyp
)
7050 and then Ekind
(Parent_Scope
) in E_Package | E_Generic_Package
7051 and then Scope_Within_Body_Or_Same
(Parent_Scope
, Scope
(Btyp
))
7052 and then Parent_Scope
/= Scope
(Instance
)
7053 and then not Is_Child_Unit
(Gen_Id
)
7057 -- If the type of the entity is a subtype, it may also have
7058 -- to be made visible, together with the base type of its
7059 -- full view, after exchange.
7061 if Is_Private_Type
(Typ
) then
7063 Switch_View
(Base_Type
(Typ
));
7066 end Check_Actual_Type
;
7074 -- Start of processing for Check_Generic_Actuals
7077 E
:= First_Entity
(Instance
);
7078 while Present
(E
) loop
7080 and then Nkind
(Parent
(E
)) = N_Subtype_Declaration
7081 and then Scope
(Etype
(E
)) /= Instance
7082 and then Is_Entity_Name
(Subtype_Indication
(Parent
(E
)))
7085 Indic
: constant Node_Id
:= Subtype_Indication
(Parent
(E
));
7088 -- Restore the proper view of the actual from the information
7089 -- saved earlier by Instantiate_Type.
7091 Check_Private_View
(Indic
);
7093 -- If this view is an array type, check its component type.
7094 -- This handles the case of an array type whose component
7095 -- type is private, used as the actual in an instantiation
7096 -- of a generic construct declared in the same package as
7097 -- the component type and taking an array type with this
7098 -- component type as formal type parameter.
7100 if Is_Array_Type
(Etype
(Indic
)) then
7102 (Component_Type_For_Private_View
(Etype
(Indic
)));
7106 -- If the actual is itself the formal of a parent instance,
7107 -- then also restore the proper view of its actual and so on.
7108 -- That's necessary for nested instantiations of the form
7111 -- type Component is private;
7112 -- type Array_Type is array (Positive range <>) of Component;
7115 -- when the outermost actuals have inconsistent views, because
7116 -- the Component_Type of Array_Type of the inner instantiations
7117 -- is the actual of Component of the outermost one and not that
7118 -- of the corresponding inner instantiations.
7120 Astype
:= Ancestor_Subtype
(E
);
7121 while Present
(Astype
)
7122 and then Nkind
(Parent
(Astype
)) = N_Subtype_Declaration
7123 and then Present
(Generic_Parent_Type
(Parent
(Astype
)))
7124 and then Is_Entity_Name
(Subtype_Indication
(Parent
(Astype
)))
7126 Check_Private_View
(Subtype_Indication
(Parent
(Astype
)));
7127 Astype
:= Ancestor_Subtype
(Astype
);
7130 Set_Is_Generic_Actual_Type
(E
);
7132 if Is_Private_Type
(E
) and then Present
(Full_View
(E
)) then
7133 Set_Is_Generic_Actual_Type
(Full_View
(E
));
7136 Set_Is_Hidden
(E
, False);
7137 Set_Is_Potentially_Use_Visible
(E
, In_Use
(Instance
));
7139 -- We constructed the generic actual type as a subtype of the
7140 -- supplied type. This means that it normally would not inherit
7141 -- subtype specific attributes of the actual, which is wrong for
7142 -- the generic case.
7144 Astype
:= Ancestor_Subtype
(E
);
7148 -- This can happen when E is an itype that is the full view of
7149 -- a private type completed, e.g. with a constrained array. In
7150 -- that case, use the first subtype, which will carry size
7151 -- information. The base type itself is unconstrained and will
7154 Astype
:= First_Subtype
(E
);
7157 Set_Size_Info
(E
, Astype
);
7158 Copy_RM_Size
(To
=> E
, From
=> Astype
);
7159 Set_First_Rep_Item
(E
, First_Rep_Item
(Astype
));
7161 if Is_Discrete_Or_Fixed_Point_Type
(E
) then
7162 Set_RM_Size
(E
, RM_Size
(Astype
));
7165 elsif Ekind
(E
) = E_Package
then
7167 -- If this is the renaming for the current instance, we're done.
7168 -- Otherwise it is a formal package. If the corresponding formal
7169 -- was declared with a box, the (instantiations of the) generic
7170 -- formal part are also visible. Otherwise, ignore the entity
7171 -- created to validate the actuals.
7173 if Renamed_Entity
(E
) = Instance
then
7176 elsif Nkind
(Parent
(E
)) /= N_Package_Renaming_Declaration
then
7179 -- The visibility of a formal of an enclosing generic is already
7182 elsif Denotes_Formal_Package
(E
) then
7185 elsif Present
(Associated_Formal_Package
(E
))
7186 and then not Is_Generic_Formal
(E
)
7188 Check_Generic_Actuals
7189 (Renamed_Entity
(E
),
7191 Box_Present
(Parent
(Associated_Formal_Package
(E
))));
7193 Set_Is_Hidden
(E
, False);
7196 -- If this is a subprogram instance (in a wrapper package) the
7197 -- actual is fully visible.
7199 elsif Is_Wrapper_Package
(Instance
) then
7200 Set_Is_Hidden
(E
, False);
7202 -- If the formal package is declared with a box, or if the formal
7203 -- parameter is defaulted, it is visible in the body.
7205 elsif Is_Formal_Box
or else Is_Visible_Formal
(E
) then
7206 Set_Is_Hidden
(E
, False);
7209 -- Check directly the type of the actual objects, including the
7210 -- component type for array types.
7212 if Ekind
(E
) in E_Constant | E_Variable
then
7213 Check_Actual_Type
(Etype
(E
));
7215 if Is_Array_Type
(Etype
(E
)) then
7216 Check_Actual_Type
(Component_Type
(Etype
(E
)));
7219 -- As well as the type of formal parameters of actual subprograms
7221 elsif Ekind
(E
) in E_Function | E_Procedure
7222 and then Is_Generic_Actual_Subprogram
(E
)
7223 and then Present
(Alias
(E
))
7225 Formal
:= First_Formal
(Alias
(E
));
7226 while Present
(Formal
) loop
7227 Check_Actual_Type
(Etype
(Formal
));
7228 Next_Formal
(Formal
);
7234 end Check_Generic_Actuals
;
7236 ------------------------------
7237 -- Check_Generic_Child_Unit --
7238 ------------------------------
7240 procedure Check_Generic_Child_Unit
7242 Parent_Installed
: in out Boolean)
7244 Loc
: constant Source_Ptr
:= Sloc
(Gen_Id
);
7245 Gen_Par
: Entity_Id
:= Empty
;
7247 Inst_Par
: Entity_Id
:= Empty
;
7250 function Find_Generic_Child
7252 Id
: Node_Id
) return Entity_Id
;
7253 -- Search generic parent for possible child unit with the given name
7255 function In_Enclosing_Instance
return Boolean;
7256 -- Within an instance of the parent, the child unit may be denoted by
7257 -- a simple name, or an abbreviated expanded name. Examine enclosing
7258 -- scopes to locate a possible parent instantiation.
7260 ------------------------
7261 -- Find_Generic_Child --
7262 ------------------------
7264 function Find_Generic_Child
7266 Id
: Node_Id
) return Entity_Id
7271 -- If entity of name is already set, instance has already been
7272 -- resolved, e.g. in an enclosing instantiation.
7274 if Present
(Entity
(Id
)) then
7275 if Scope
(Entity
(Id
)) = Scop
then
7282 E
:= First_Entity
(Scop
);
7283 while Present
(E
) loop
7284 if Chars
(E
) = Chars
(Id
)
7285 and then Is_Child_Unit
(E
)
7287 if Is_Child_Unit
(E
)
7288 and then not Is_Visible_Lib_Unit
(E
)
7291 ("generic child unit& is not visible", Gen_Id
, E
);
7303 end Find_Generic_Child
;
7305 ---------------------------
7306 -- In_Enclosing_Instance --
7307 ---------------------------
7309 function In_Enclosing_Instance
return Boolean is
7310 Enclosing_Instance
: Node_Id
;
7311 Instance_Decl
: Node_Id
;
7314 -- We do not inline any call that contains instantiations, except
7315 -- for instantiations of Unchecked_Conversion, so if we are within
7316 -- an inlined body the current instance does not require parents.
7318 if In_Inlined_Body
then
7319 pragma Assert
(Chars
(Gen_Id
) = Name_Unchecked_Conversion
);
7323 -- Loop to check enclosing scopes
7325 Enclosing_Instance
:= Current_Scope
;
7326 while Present
(Enclosing_Instance
) loop
7327 Instance_Decl
:= Unit_Declaration_Node
(Enclosing_Instance
);
7329 if Ekind
(Enclosing_Instance
) = E_Package
7330 and then Is_Generic_Instance
(Enclosing_Instance
)
7332 (Generic_Parent
(Specification
(Instance_Decl
)))
7334 -- Check whether the generic we are looking for is a child of
7337 E
:= Find_Generic_Child
7338 (Generic_Parent
(Specification
(Instance_Decl
)), Gen_Id
);
7339 exit when Present
(E
);
7345 Enclosing_Instance
:= Scope
(Enclosing_Instance
);
7357 Make_Expanded_Name
(Loc
,
7359 Prefix
=> New_Occurrence_Of
(Enclosing_Instance
, Loc
),
7360 Selector_Name
=> New_Occurrence_Of
(E
, Loc
)));
7362 Set_Entity
(Gen_Id
, E
);
7363 Set_Etype
(Gen_Id
, Etype
(E
));
7364 Parent_Installed
:= False; -- Already in scope.
7367 end In_Enclosing_Instance
;
7369 -- Start of processing for Check_Generic_Child_Unit
7372 -- If the name of the generic is given by a selected component, it may
7373 -- be the name of a generic child unit, and the prefix is the name of an
7374 -- instance of the parent, in which case the child unit must be visible.
7375 -- If this instance is not in scope, it must be placed there and removed
7376 -- after instantiation, because what is being instantiated is not the
7377 -- original child, but the corresponding child present in the instance
7380 -- If the child is instantiated within the parent, it can be given by
7381 -- a simple name. In this case the instance is already in scope, but
7382 -- the child generic must be recovered from the generic parent as well.
7384 if Nkind
(Gen_Id
) = N_Selected_Component
then
7385 S
:= Selector_Name
(Gen_Id
);
7386 Analyze
(Prefix
(Gen_Id
));
7387 Inst_Par
:= Entity
(Prefix
(Gen_Id
));
7389 if Ekind
(Inst_Par
) = E_Package
7390 and then Present
(Renamed_Entity
(Inst_Par
))
7392 Inst_Par
:= Renamed_Entity
(Inst_Par
);
7395 if Ekind
(Inst_Par
) = E_Package
then
7396 if Nkind
(Parent
(Inst_Par
)) = N_Package_Specification
then
7397 Gen_Par
:= Generic_Parent
(Parent
(Inst_Par
));
7399 elsif Nkind
(Parent
(Inst_Par
)) = N_Defining_Program_Unit_Name
7401 Nkind
(Parent
(Parent
(Inst_Par
))) = N_Package_Specification
7403 Gen_Par
:= Generic_Parent
(Parent
(Parent
(Inst_Par
)));
7406 elsif Ekind
(Inst_Par
) = E_Generic_Package
7407 and then Nkind
(Parent
(Gen_Id
)) = N_Formal_Package_Declaration
7409 -- A formal package may be a real child package, and not the
7410 -- implicit instance within a parent. In this case the child is
7411 -- not visible and has to be retrieved explicitly as well.
7413 Gen_Par
:= Inst_Par
;
7416 if Present
(Gen_Par
) then
7418 -- The prefix denotes an instantiation. The entity itself may be a
7419 -- nested generic, or a child unit.
7421 E
:= Find_Generic_Child
(Gen_Par
, S
);
7424 Change_Selected_Component_To_Expanded_Name
(Gen_Id
);
7425 Set_Entity
(Gen_Id
, E
);
7426 Set_Etype
(Gen_Id
, Etype
(E
));
7428 Set_Etype
(S
, Etype
(E
));
7430 -- Indicate that this is a reference to the parent
7432 if In_Extended_Main_Source_Unit
(Gen_Id
) then
7433 Set_Is_Instantiated
(Inst_Par
);
7436 -- A common mistake is to replicate the naming scheme of a
7437 -- hierarchy by instantiating a generic child directly, rather
7438 -- than the implicit child in a parent instance:
7440 -- generic .. package Gpar is ..
7441 -- generic .. package Gpar.Child is ..
7442 -- package Par is new Gpar ();
7445 -- package Par.Child is new Gpar.Child ();
7446 -- rather than Par.Child
7448 -- In this case the instantiation is within Par, which is an
7449 -- instance, but Gpar does not denote Par because we are not IN
7450 -- the instance of Gpar, so this is illegal. The test below
7451 -- recognizes this particular case.
7454 -- We want to reject the final instantiation in
7455 -- generic package G1 is end G1;
7456 -- generic package G1.G2 is end G1.G2;
7457 -- with G1; package I1 is new G1;
7458 -- with G1.G2; package I1.I2 is new G1.G2;
7459 -- because the use of G1.G2 should instead be either
7460 -- I1.G2 or simply G2. However, the tree that is built
7461 -- in this case is wrong. In the expanded copy
7462 -- of G2, we need (and therefore generate) a renaming
7463 -- package G1 renames I1;
7464 -- but this renaming should not participate in resolving
7465 -- this occurrence of the name "G1.G2"; unfortunately,
7466 -- it does. Rather than correct this error, we compensate
7467 -- for it in this function.
7469 -- We also perform another adjustment here. If we are
7470 -- currently inside a generic package, then that
7471 -- generic package needs to be treated as a package.
7472 -- For example, if a generic Aaa declares a nested generic
7473 -- Bbb (perhaps as a child unit) then Aaa can also legally
7474 -- declare an instance of Aaa.Bbb.
7476 function Adjusted_Inst_Par_Ekind
return Entity_Kind
;
7478 -----------------------------
7479 -- Adjusted_Inst_Par_Ekind --
7480 -----------------------------
7482 function Adjusted_Inst_Par_Ekind
return Entity_Kind
is
7483 Prefix_Entity
: Entity_Id
;
7484 Inst_Par_GP
: Node_Id
;
7485 Inst_Par_Parent
: Node_Id
:= Parent
(Inst_Par
);
7487 if Nkind
(Inst_Par_Parent
) = N_Defining_Program_Unit_Name
7489 Inst_Par_Parent
:= Parent
(Inst_Par_Parent
);
7492 Inst_Par_GP
:= Generic_Parent
(Inst_Par_Parent
);
7494 if Nkind
(Gen_Id
) = N_Expanded_Name
7495 and then Present
(Inst_Par_GP
)
7496 and then Ekind
(Inst_Par_GP
) = E_Generic_Package
7498 Prefix_Entity
:= Entity
(Prefix
(Gen_Id
));
7500 if Present
(Prefix_Entity
)
7501 and then not Comes_From_Source
(Prefix_Entity
)
7502 and then Nkind
(Parent
(Prefix_Entity
)) =
7503 N_Package_Renaming_Declaration
7504 and then Chars
(Prefix_Entity
) = Chars
(Inst_Par_GP
)
7506 return E_Generic_Package
;
7510 if Ekind
(Inst_Par
) = E_Generic_Package
7511 and then In_Open_Scopes
(Inst_Par
)
7513 -- If we are inside a generic package then
7514 -- treat it as a package.
7519 return Ekind
(Inst_Par
);
7520 end Adjusted_Inst_Par_Ekind
;
7523 if Is_Child_Unit
(E
)
7524 and then (No
(Inst_Par
)
7525 or else Adjusted_Inst_Par_Ekind
=
7527 and then (not In_Instance
7528 or else Nkind
(Parent
(Parent
(Gen_Id
))) =
7532 ("prefix of generic child unit must be " &
7533 "instance of parent",
7538 if not In_Open_Scopes
(Inst_Par
)
7539 and then Nkind
(Parent
(Gen_Id
)) not in
7540 N_Generic_Renaming_Declaration
7542 Install_Parent
(Inst_Par
);
7543 Parent_Installed
:= True;
7545 elsif In_Open_Scopes
(Inst_Par
) then
7547 -- If the parent is already installed, install the actuals
7548 -- for its formal packages. This is necessary when the child
7549 -- instance is a child of the parent instance: in this case,
7550 -- the parent is placed on the scope stack but the formal
7551 -- packages are not made visible.
7553 Install_Formal_Packages
(Inst_Par
);
7557 -- If the generic parent does not contain an entity that
7558 -- corresponds to the selector, the instance doesn't either.
7559 -- Analyzing the node will yield the appropriate error message.
7560 -- If the entity is not a child unit, then it is an inner
7561 -- generic in the parent.
7569 if Is_Child_Unit
(Entity
(Gen_Id
))
7571 Nkind
(Parent
(Gen_Id
)) not in N_Generic_Renaming_Declaration
7572 and then not In_Open_Scopes
(Inst_Par
)
7574 Install_Parent
(Inst_Par
);
7575 Parent_Installed
:= True;
7577 -- The generic unit may be the renaming of the implicit child
7578 -- present in an instance. In that case the parent instance is
7579 -- obtained from the name of the renamed entity.
7581 elsif Ekind
(Entity
(Gen_Id
)) = E_Generic_Package
7582 and then Present
(Renamed_Entity
(Entity
(Gen_Id
)))
7583 and then Is_Child_Unit
(Renamed_Entity
(Entity
(Gen_Id
)))
7586 Renamed_Package
: constant Node_Id
:=
7587 Name
(Parent
(Entity
(Gen_Id
)));
7589 if Nkind
(Renamed_Package
) = N_Expanded_Name
then
7590 Inst_Par
:= Entity
(Prefix
(Renamed_Package
));
7591 Install_Parent
(Inst_Par
);
7592 Parent_Installed
:= True;
7598 elsif Nkind
(Gen_Id
) = N_Expanded_Name
then
7600 -- Entity already present, analyze prefix, whose meaning may be an
7601 -- instance in the current context. If it is an instance of a
7602 -- relative within another, the proper parent may still have to be
7603 -- installed, if they are not of the same generation.
7605 Analyze
(Prefix
(Gen_Id
));
7607 -- Prevent cascaded errors
7609 if Etype
(Prefix
(Gen_Id
)) = Any_Type
then
7613 -- In the unlikely case that a local declaration hides the name of
7614 -- the parent package, locate it on the homonym chain. If the context
7615 -- is an instance of the parent, the renaming entity is flagged as
7618 Inst_Par
:= Entity
(Prefix
(Gen_Id
));
7619 while Present
(Inst_Par
)
7620 and then not Is_Package_Or_Generic_Package
(Inst_Par
)
7622 Inst_Par
:= Homonym
(Inst_Par
);
7625 pragma Assert
(Present
(Inst_Par
));
7626 Set_Entity
(Prefix
(Gen_Id
), Inst_Par
);
7628 if In_Enclosing_Instance
then
7631 elsif Present
(Entity
(Gen_Id
))
7632 and then No
(Renamed_Entity
(Entity
(Gen_Id
)))
7633 and then Is_Child_Unit
(Entity
(Gen_Id
))
7634 and then not In_Open_Scopes
(Inst_Par
)
7636 Install_Parent
(Inst_Par
);
7637 Parent_Installed
:= True;
7639 -- Handle renaming of generic child unit
7641 elsif Present
(Entity
(Gen_Id
))
7642 and then Present
(Renamed_Entity
(Entity
(Gen_Id
)))
7643 and then Is_Child_Unit
(Renamed_Entity
(Entity
(Gen_Id
)))
7650 -- The entity of the renamed generic child unit does not
7651 -- have any reference to the instantiated parent. In order to
7652 -- locate it we traverse the scope containing the renaming
7653 -- declaration; the instance of the parent is available in
7654 -- the prefix of the renaming declaration. For example:
7657 -- package Inst_Par is new ...
7658 -- generic package Ren_Child renames Ins_Par.Child;
7663 -- package Inst_Child is new A.Ren_Child;
7666 E
:= First_Entity
(Entity
(Prefix
(Gen_Id
)));
7667 while Present
(E
) loop
7668 if not Is_Object
(E
)
7669 and then Present
(Renamed_Entity
(E
))
7671 Renamed_Entity
(E
) = Renamed_Entity
(Entity
(Gen_Id
))
7673 Ren_Decl
:= Parent
(E
);
7674 Inst_Par
:= Entity
(Prefix
(Name
(Ren_Decl
)));
7676 if not In_Open_Scopes
(Inst_Par
) then
7677 Install_Parent
(Inst_Par
);
7678 Parent_Installed
:= True;
7684 E
:= Next_Entity
(E
);
7689 elsif In_Enclosing_Instance
then
7691 -- The child unit is found in some enclosing scope
7698 -- If this is the renaming of the implicit child in a parent
7699 -- instance, recover the parent name and install it.
7701 if Is_Entity_Name
(Gen_Id
) then
7702 E
:= Entity
(Gen_Id
);
7704 if Is_Generic_Unit
(E
)
7705 and then Nkind
(Parent
(E
)) in N_Generic_Renaming_Declaration
7706 and then Is_Child_Unit
(Renamed_Entity
(E
))
7707 and then Is_Generic_Unit
(Scope
(Renamed_Entity
(E
)))
7708 and then Nkind
(Name
(Parent
(E
))) = N_Expanded_Name
7710 Rewrite
(Gen_Id
, New_Copy_Tree
(Name
(Parent
(E
))));
7711 Inst_Par
:= Entity
(Prefix
(Gen_Id
));
7713 if not In_Open_Scopes
(Inst_Par
) then
7714 Install_Parent
(Inst_Par
);
7715 Parent_Installed
:= True;
7718 -- If it is a child unit of a non-generic parent, it may be
7719 -- use-visible and given by a direct name. Install parent as
7722 elsif Is_Generic_Unit
(E
)
7723 and then Is_Child_Unit
(E
)
7725 Nkind
(Parent
(Gen_Id
)) not in N_Generic_Renaming_Declaration
7726 and then not Is_Generic_Unit
(Scope
(E
))
7728 if not In_Open_Scopes
(Scope
(E
)) then
7729 Install_Parent
(Scope
(E
));
7730 Parent_Installed
:= True;
7735 end Check_Generic_Child_Unit
;
7737 -----------------------------
7738 -- Check_Hidden_Child_Unit --
7739 -----------------------------
7741 procedure Check_Hidden_Child_Unit
7743 Gen_Unit
: Entity_Id
;
7744 Act_Decl_Id
: Entity_Id
)
7746 Gen_Id
: constant Node_Id
:= Name
(N
);
7749 if Is_Child_Unit
(Gen_Unit
)
7750 and then Is_Child_Unit
(Act_Decl_Id
)
7751 and then Nkind
(Gen_Id
) = N_Expanded_Name
7752 and then Entity
(Prefix
(Gen_Id
)) = Scope
(Act_Decl_Id
)
7753 and then Chars
(Gen_Unit
) = Chars
(Act_Decl_Id
)
7755 Error_Msg_Node_2
:= Scope
(Act_Decl_Id
);
7757 ("generic unit & is implicitly declared in &",
7758 Defining_Unit_Name
(N
), Gen_Unit
);
7759 Error_Msg_N
("\instance must have different name",
7760 Defining_Unit_Name
(N
));
7762 end Check_Hidden_Child_Unit
;
7764 ------------------------
7765 -- Check_Private_View --
7766 ------------------------
7768 procedure Check_Private_View
(N
: Node_Id
) is
7769 Comparison
: constant Boolean := Nkind
(N
) in N_Op_Compare
;
7770 Typ
: constant Entity_Id
:=
7771 (if Comparison
then Compare_Type
(N
) else Etype
(N
));
7773 procedure Check_Private_Type
(T
: Entity_Id
; Private_View
: Boolean);
7774 -- Check that the available view of T matches Private_View and, if not,
7775 -- switch the view of T or of its base type.
7777 procedure Check_Private_Type
(T
: Entity_Id
; Private_View
: Boolean) is
7778 BT
: constant Entity_Id
:= Base_Type
(T
);
7781 -- If the full declaration was not visible in the generic, stop here
7783 if Private_View
then
7787 -- Exchange views if the type was not private in the generic but is
7788 -- private at the point of instantiation. Do not exchange views if
7789 -- the scope of the type is in scope. This can happen if both generic
7790 -- and instance are sibling units, or if type is defined in a parent.
7791 -- In this case the visibility of the type will be correct for all
7794 if Is_Private_Type
(T
)
7795 and then Present
(Full_View
(T
))
7796 and then not In_Open_Scopes
(Scope
(T
))
7800 -- Finally, a nonprivate subtype may have a private base type, which
7801 -- must be exchanged for consistency. This can happen when a package
7802 -- body is instantiated, when the scope stack is empty but in fact
7803 -- the subtype and the base type are declared in an enclosing scope.
7805 -- Note that in this case we introduce an inconsistency in the view
7806 -- set, because we switch the base type BT, but there could be some
7807 -- private dependent subtypes of BT which remain unswitched. Such
7808 -- subtypes might need to be switched at a later point (see specific
7809 -- provision for that case in Switch_View).
7811 elsif not Is_Private_Type
(T
)
7812 and then Is_Private_Type
(BT
)
7813 and then Present
(Full_View
(BT
))
7814 and then not In_Open_Scopes
(BT
)
7816 Prepend_Elmt
(Full_View
(BT
), Exchanged_Views
);
7817 Exchange_Declarations
(BT
);
7819 end Check_Private_Type
;
7822 if Present
(Typ
) then
7823 -- If the type appears in a subtype declaration, the subtype in
7824 -- instance must have a view compatible with that of its parent,
7825 -- which must be exchanged (see corresponding code in Restore_
7826 -- Private_Views) so we make an exception to the open scope rule
7827 -- implemented by Check_Private_Type above.
7829 if Has_Private_View
(N
)
7830 and then not Is_Private_Type
(Typ
)
7831 and then not Has_Been_Exchanged
(Typ
)
7832 and then (not In_Open_Scopes
(Scope
(Typ
))
7833 or else Nkind
(Parent
(N
)) = N_Subtype_Declaration
)
7836 Assoc
: constant Node_Id
:= Get_Associated_Node
(N
);
7839 -- In the generic, only the private declaration was visible
7841 Prepend_Elmt
(Typ
, Exchanged_Views
);
7842 Exchange_Declarations
7843 (if Comparison
then Compare_Type
(Assoc
) else Etype
(Assoc
));
7846 -- Check that the available views of Typ match their respective flag.
7847 -- Note that the type of a visible discriminant is never private.
7850 Check_Private_Type
(Typ
, Has_Private_View
(N
));
7852 if Is_Access_Type
(Typ
) then
7854 (Designated_Type
(Typ
), Has_Secondary_Private_View
(N
));
7856 elsif Is_Array_Type
(Typ
) then
7858 (Component_Type_For_Private_View
(Typ
),
7859 Has_Secondary_Private_View
(N
));
7861 elsif (Is_Record_Type
(Typ
) or else Is_Concurrent_Type
(Typ
))
7862 and then Has_Discriminants
(Typ
)
7868 Disc
:= First_Discriminant
(Typ
);
7869 while Present
(Disc
) loop
7870 Check_Private_Type
(Etype
(Disc
), False);
7871 Next_Discriminant
(Disc
);
7877 end Check_Private_View
;
7879 -----------------------------
7880 -- Check_Hidden_Primitives --
7881 -----------------------------
7883 function Check_Hidden_Primitives
(Assoc_List
: List_Id
) return Elist_Id
is
7886 Result
: Elist_Id
:= No_Elist
;
7889 if No
(Assoc_List
) then
7893 -- Traverse the list of associations between formals and actuals
7894 -- searching for renamings of tagged types
7896 Actual
:= First
(Assoc_List
);
7897 while Present
(Actual
) loop
7898 if Nkind
(Actual
) = N_Subtype_Declaration
then
7899 Gen_T
:= Generic_Parent_Type
(Actual
);
7901 if Present
(Gen_T
) and then Is_Tagged_Type
(Gen_T
) then
7903 -- Traverse the list of primitives of the actual types
7904 -- searching for hidden primitives that are visible in the
7905 -- corresponding generic formal; leave them visible and
7906 -- append them to Result to restore their decoration later.
7908 Install_Hidden_Primitives
7909 (Prims_List
=> Result
,
7911 Act_T
=> Entity
(Subtype_Indication
(Actual
)));
7919 end Check_Hidden_Primitives
;
7921 -------------------------------------
7922 -- Component_Type_For_Private_View --
7923 -------------------------------------
7925 function Component_Type_For_Private_View
(T
: Entity_Id
) return Entity_Id
is
7926 Typ
: constant Entity_Id
:= Component_Type
(T
);
7929 if Is_Array_Type
(Typ
) and then not Has_Private_Declaration
(Typ
) then
7930 return Component_Type_For_Private_View
(Typ
);
7934 end Component_Type_For_Private_View
;
7936 --------------------------
7937 -- Contains_Instance_Of --
7938 --------------------------
7940 function Contains_Instance_Of
7943 N
: Node_Id
) return Boolean
7951 -- Verify that there are no circular instantiations. We check whether
7952 -- the unit contains an instance of the current scope or some enclosing
7953 -- scope (in case one of the instances appears in a subunit). Longer
7954 -- circularities involving subunits might seem too pathological to
7955 -- consider, but they were not too pathological for the authors of
7956 -- DEC bc30vsq, so we loop over all enclosing scopes, and mark all
7957 -- enclosing generic scopes as containing an instance.
7960 -- Within a generic subprogram body, the scope is not generic, to
7961 -- allow for recursive subprograms. Use the declaration to determine
7962 -- whether this is a generic unit.
7964 if Ekind
(Scop
) = E_Generic_Package
7965 or else (Is_Subprogram
(Scop
)
7966 and then Nkind
(Unit_Declaration_Node
(Scop
)) =
7967 N_Generic_Subprogram_Declaration
)
7969 Elmt
:= First_Elmt
(Inner_Instances
(Inner
));
7971 while Present
(Elmt
) loop
7972 if Node
(Elmt
) = Scop
then
7973 Error_Msg_Node_2
:= Inner
;
7975 ("circular instantiation: & instantiated within &!",
7979 elsif Node
(Elmt
) = Inner
then
7982 elsif Contains_Instance_Of
(Node
(Elmt
), Scop
, N
) then
7983 Error_Msg_Node_2
:= Inner
;
7985 ("circular instantiation: & instantiated within &!",
7993 -- Indicate that Inner is being instantiated within Scop
7995 Append_Elmt
(Inner
, Inner_Instances
(Scop
));
7998 if Scop
= Standard_Standard
then
8001 Scop
:= Scope
(Scop
);
8006 end Contains_Instance_Of
;
8008 -----------------------
8009 -- Copy_Generic_Node --
8010 -----------------------
8012 function Copy_Generic_Node
8014 Parent_Id
: Node_Id
;
8015 Instantiating
: Boolean) return Node_Id
8020 function Copy_Generic_Descendant
(D
: Union_Id
) return Union_Id
;
8021 -- Check the given value of one of the Fields referenced by the current
8022 -- node to determine whether to copy it recursively. The field may hold
8023 -- a Node_Id, a List_Id, or an Elist_Id, or a plain value (Sloc, Uint,
8024 -- Char) in which case it need not be copied.
8026 procedure Copy_Descendants
;
8027 -- Common utility for various nodes
8029 function Copy_Generic_Elist
(E
: Elist_Id
) return Elist_Id
;
8030 -- Make copy of element list
8032 function Copy_Generic_List
8034 Parent_Id
: Node_Id
) return List_Id
;
8035 -- Apply Copy_Generic_Node recursively to the members of a node list
8037 function In_Defining_Unit_Name
(Nam
: Node_Id
) return Boolean;
8038 -- True if an identifier is part of the defining program unit name of
8040 -- Consider removing this subprogram now that ASIS no longer uses it.
8042 ----------------------
8043 -- Copy_Descendants --
8044 ----------------------
8046 procedure Copy_Descendants
is
8047 procedure Walk
is new
8048 Walk_Sinfo_Fields_Pairwise
(Copy_Generic_Descendant
);
8051 end Copy_Descendants
;
8053 -----------------------------
8054 -- Copy_Generic_Descendant --
8055 -----------------------------
8057 function Copy_Generic_Descendant
(D
: Union_Id
) return Union_Id
is
8059 if D
= Union_Id
(Empty
) then
8062 elsif D
in Node_Range
then
8064 (Copy_Generic_Node
(Node_Id
(D
), New_N
, Instantiating
));
8066 elsif D
in List_Range
then
8067 return Union_Id
(Copy_Generic_List
(List_Id
(D
), New_N
));
8069 elsif D
in Elist_Range
then
8070 return Union_Id
(Copy_Generic_Elist
(Elist_Id
(D
)));
8072 -- Nothing else is copyable (e.g. Uint values), return as is
8077 end Copy_Generic_Descendant
;
8079 ------------------------
8080 -- Copy_Generic_Elist --
8081 ------------------------
8083 function Copy_Generic_Elist
(E
: Elist_Id
) return Elist_Id
is
8090 M
:= First_Elmt
(E
);
8091 while Present
(M
) loop
8093 (Copy_Generic_Node
(Node
(M
), Empty
, Instantiating
), L
);
8102 end Copy_Generic_Elist
;
8104 -----------------------
8105 -- Copy_Generic_List --
8106 -----------------------
8108 function Copy_Generic_List
8110 Parent_Id
: Node_Id
) return List_Id
8118 Set_Parent
(New_L
, Parent_Id
);
8121 while Present
(N
) loop
8122 Append
(Copy_Generic_Node
(N
, Empty
, Instantiating
), New_L
);
8131 end Copy_Generic_List
;
8133 ---------------------------
8134 -- In_Defining_Unit_Name --
8135 ---------------------------
8137 function In_Defining_Unit_Name
(Nam
: Node_Id
) return Boolean is
8140 Present
(Parent
(Nam
))
8141 and then (Nkind
(Parent
(Nam
)) = N_Defining_Program_Unit_Name
8143 (Nkind
(Parent
(Nam
)) = N_Expanded_Name
8144 and then In_Defining_Unit_Name
(Parent
(Nam
))));
8145 end In_Defining_Unit_Name
;
8147 -- Start of processing for Copy_Generic_Node
8154 New_N
:= New_Copy
(N
);
8156 -- If we are instantiating, we want to adjust the sloc based on the
8157 -- current S_Adjustment. However, if this is the root node of a subunit,
8158 -- we need to defer that adjustment to below (see "elsif Instantiating
8159 -- and Was_Stub"), so it comes after Create_Instantiation_Source has
8160 -- computed the adjustment.
8163 and then not (Nkind
(N
) in N_Proper_Body
8164 and then Was_Originally_Stub
(N
))
8166 Adjust_Instantiation_Sloc
(New_N
, S_Adjustment
);
8169 if not Is_List_Member
(N
) then
8170 Set_Parent
(New_N
, Parent_Id
);
8173 -- Special casing for identifiers and other entity names and operators
8175 if Nkind
(N
) in N_Character_Literal
8181 if not Instantiating
then
8183 -- Link both nodes in order to assign subsequently the entity of
8184 -- the copy to the original node, in case this is a global
8187 Set_Associated_Node
(N
, New_N
);
8189 -- If we are within an instantiation, this is a nested generic
8190 -- that has already been analyzed at the point of definition.
8191 -- We must preserve references that were global to the enclosing
8192 -- parent at that point. Other occurrences, whether global or
8193 -- local to the current generic, must be resolved anew, so we
8194 -- reset the entity in the generic copy. A global reference has a
8195 -- smaller depth than the parent, or else the same depth in case
8196 -- both are distinct compilation units.
8198 -- A child unit is implicitly declared within the enclosing parent
8199 -- but is in fact global to it, and must be preserved.
8201 -- It is also possible for Current_Instantiated_Parent to be
8202 -- defined, and for this not to be a nested generic, namely if
8203 -- the unit is loaded through Rtsfind. In that case, the entity of
8204 -- New_N is only a link to the associated node, and not a defining
8207 -- The entities for parent units in the defining_program_unit of a
8208 -- generic child unit are established when the context of the unit
8209 -- is first analyzed, before the generic copy is made. They are
8210 -- preserved in the copy for use in e.g. ASIS queries.
8212 Ent
:= Entity
(New_N
);
8214 if No
(Current_Instantiated_Parent
.Gen_Id
) then
8216 or else Nkind
(Ent
) /= N_Defining_Identifier
8217 or else not In_Defining_Unit_Name
(N
)
8219 Set_Associated_Node
(New_N
, Empty
);
8223 or else Nkind
(Ent
) not in N_Entity
8224 or else No
(Scope
(Ent
))
8226 (Scope
(Ent
) = Current_Instantiated_Parent
.Gen_Id
8227 and then not Is_Child_Unit
(Ent
))
8229 (Scope_Depth_Set
(Scope
(Ent
))
8231 Scope_Depth
(Scope
(Ent
)) >
8232 Scope_Depth
(Current_Instantiated_Parent
.Gen_Id
)
8234 Get_Source_Unit
(Ent
) =
8235 Get_Source_Unit
(Current_Instantiated_Parent
.Gen_Id
))
8237 Set_Associated_Node
(New_N
, Empty
);
8240 -- Case of instantiating identifier or some other name or operator
8243 -- If the associated node is still defined, the entity in it
8244 -- is global, and must be copied to the instance. If this copy
8245 -- is being made for a body to inline, it is applied to an
8246 -- instantiated tree, and the entity is already present and
8247 -- must be also preserved.
8250 Assoc
: constant Node_Id
:= Get_Associated_Node
(N
);
8253 if Present
(Assoc
) then
8254 if Nkind
(Assoc
) = Nkind
(N
) then
8255 Set_Entity
(New_N
, Entity
(Assoc
));
8256 Check_Private_View
(N
);
8258 -- The node is a reference to a global type and acts as the
8259 -- subtype mark of a qualified expression created in order
8260 -- to aid resolution of accidental overloading in instances.
8261 -- Since N is a reference to a type, the Associated_Node of
8262 -- N denotes an entity rather than another identifier. See
8263 -- Qualify_Universal_Operands for details.
8265 elsif Nkind
(N
) = N_Identifier
8266 and then Nkind
(Parent
(N
)) = N_Qualified_Expression
8267 and then Subtype_Mark
(Parent
(N
)) = N
8268 and then Is_Qualified_Universal_Literal
(Parent
(N
))
8270 Set_Entity
(New_N
, Assoc
);
8272 -- Cope with the rewriting into expanded name that may have
8273 -- occurred in between, e.g. in Check_Generic_Child_Unit for
8274 -- generic renaming declarations.
8276 elsif Nkind
(Assoc
) = N_Expanded_Name
then
8277 Rewrite
(N
, New_Copy_Tree
(Assoc
));
8278 Set_Associated_Node
(N
, Assoc
);
8279 return Copy_Generic_Node
(N
, Parent_Id
, Instantiating
);
8281 -- The name in the call may be a selected component if the
8282 -- call has not been analyzed yet, as may be the case for
8283 -- pre/post conditions in a generic unit.
8285 elsif Nkind
(Assoc
) = N_Function_Call
8286 and then Is_Entity_Name
(Name
(Assoc
))
8288 Set_Entity
(New_N
, Entity
(Name
(Assoc
)));
8289 Check_Private_View
(N
);
8291 elsif Nkind
(Assoc
) in N_Entity
8292 and then (Expander_Active
8293 or else (GNATprove_Mode
8294 and then not In_Spec_Expression
8295 and then not Inside_A_Generic
))
8297 -- Inlining case: we are copying a tree that contains
8298 -- global entities, which are preserved in the copy to be
8299 -- used for subsequent inlining.
8304 Set_Entity
(New_N
, Empty
);
8310 -- For expanded name, we must copy the Prefix and Selector_Name
8312 if Nkind
(N
) = N_Expanded_Name
then
8314 (New_N
, Copy_Generic_Node
(Prefix
(N
), New_N
, Instantiating
));
8316 Set_Selector_Name
(New_N
,
8317 Copy_Generic_Node
(Selector_Name
(N
), New_N
, Instantiating
));
8319 -- For operators, copy the operands
8321 elsif Nkind
(N
) in N_Op
then
8322 if Nkind
(N
) in N_Binary_Op
then
8323 Set_Left_Opnd
(New_N
,
8324 Copy_Generic_Node
(Left_Opnd
(N
), New_N
, Instantiating
));
8327 Set_Right_Opnd
(New_N
,
8328 Copy_Generic_Node
(Right_Opnd
(N
), New_N
, Instantiating
));
8331 -- Establish a link between an entity from the generic template and the
8332 -- corresponding entity in the generic copy to be analyzed.
8334 elsif Nkind
(N
) in N_Entity
then
8335 if not Instantiating
then
8336 Set_Associated_Entity
(N
, New_N
);
8339 -- Clear any existing link the copy may inherit from the replicated
8340 -- generic template entity.
8342 Set_Associated_Entity
(New_N
, Empty
);
8344 -- Special casing for stubs
8346 elsif Nkind
(N
) in N_Body_Stub
then
8348 -- In any case, we must copy the specification or defining
8349 -- identifier as appropriate.
8351 if Nkind
(N
) = N_Subprogram_Body_Stub
then
8352 Set_Specification
(New_N
,
8353 Copy_Generic_Node
(Specification
(N
), New_N
, Instantiating
));
8356 Set_Defining_Identifier
(New_N
,
8358 (Defining_Identifier
(N
), New_N
, Instantiating
));
8361 -- If we are not instantiating, then this is where we load and
8362 -- analyze subunits, i.e. at the point where the stub occurs. A
8363 -- more permissive system might defer this analysis to the point
8364 -- of instantiation, but this seems too complicated for now.
8366 if not Instantiating
then
8368 Subunit_Name
: constant Unit_Name_Type
:= Get_Unit_Name
(N
);
8370 Unum
: Unit_Number_Type
;
8374 -- Make sure that, if it is a subunit of the main unit that is
8375 -- preprocessed and if -gnateG is specified, the preprocessed
8376 -- file will be written.
8378 Lib
.Analysing_Subunit_Of_Main
:=
8379 Lib
.In_Extended_Main_Source_Unit
(N
);
8382 (Load_Name
=> Subunit_Name
,
8386 Lib
.Analysing_Subunit_Of_Main
:= False;
8388 -- If the proper body is not found, a warning message will be
8389 -- emitted when analyzing the stub, or later at the point of
8390 -- instantiation. Here we just leave the stub as is.
8392 if Unum
= No_Unit
then
8393 Subunits_Missing
:= True;
8394 goto Subunit_Not_Found
;
8397 Subunit
:= Cunit
(Unum
);
8399 if Nkind
(Unit
(Subunit
)) /= N_Subunit
then
8401 ("found child unit instead of expected SEPARATE subunit",
8403 Error_Msg_Sloc
:= Sloc
(N
);
8404 Error_Msg_N
("\to complete stub #", Subunit
);
8405 goto Subunit_Not_Found
;
8408 -- We must create a generic copy of the subunit, in order to
8409 -- perform semantic analysis on it, and we must replace the
8410 -- stub in the original generic unit with the subunit, in order
8411 -- to preserve non-local references within.
8413 -- Only the proper body needs to be copied. Library_Unit and
8414 -- context clause are simply inherited by the generic copy.
8415 -- Note that the copy (which may be recursive if there are
8416 -- nested subunits) must be done first, before attaching it to
8417 -- the enclosing generic.
8421 (Proper_Body
(Unit
(Subunit
)),
8422 Empty
, Instantiating
=> False);
8424 -- Now place the original proper body in the original generic
8425 -- unit. This is a body, not a compilation unit.
8427 Rewrite
(N
, Proper_Body
(Unit
(Subunit
)));
8428 Set_Is_Compilation_Unit
(Defining_Entity
(N
), False);
8429 Set_Was_Originally_Stub
(N
);
8431 -- Finally replace the body of the subunit with its copy, and
8432 -- make this new subunit into the library unit of the generic
8433 -- copy, which does not have stubs any longer.
8435 Set_Proper_Body
(Unit
(Subunit
), New_Body
);
8436 Set_Library_Unit
(New_N
, Subunit
);
8437 Inherit_Context
(Unit
(Subunit
), N
);
8440 -- If we are instantiating, this must be an error case, since
8441 -- otherwise we would have replaced the stub node by the proper body
8442 -- that corresponds. So just ignore it in the copy (i.e. we have
8443 -- copied it, and that is good enough).
8449 <<Subunit_Not_Found
>> null;
8451 -- If the node is a compilation unit, it is the subunit of a stub, which
8452 -- has been loaded already (see code below). In this case, the library
8453 -- unit field of N points to the parent unit (which is a compilation
8454 -- unit) and need not (and cannot) be copied.
8456 -- When the proper body of the stub is analyzed, the library_unit link
8457 -- is used to establish the proper context (see sem_ch10).
8459 -- The other fields of a compilation unit are copied as usual
8461 elsif Nkind
(N
) = N_Compilation_Unit
then
8463 -- This code can only be executed when not instantiating, because in
8464 -- the copy made for an instantiation, the compilation unit node has
8465 -- disappeared at the point that a stub is replaced by its proper
8468 pragma Assert
(not Instantiating
);
8470 Set_Context_Items
(New_N
,
8471 Copy_Generic_List
(Context_Items
(N
), New_N
));
8474 Copy_Generic_Node
(Unit
(N
), New_N
, Instantiating
=> False));
8476 Set_First_Inlined_Subprogram
(New_N
,
8478 (First_Inlined_Subprogram
(N
), New_N
, Instantiating
=> False));
8483 (Aux_Decls_Node
(N
), New_N
, Instantiating
=> False));
8485 -- For an assignment node, the assignment is known to be semantically
8486 -- legal if we are instantiating the template. This avoids incorrect
8487 -- diagnostics in generated code.
8489 elsif Nkind
(N
) = N_Assignment_Statement
then
8491 -- Copy name and expression fields in usual manner
8494 Copy_Generic_Node
(Name
(N
), New_N
, Instantiating
));
8496 Set_Expression
(New_N
,
8497 Copy_Generic_Node
(Expression
(N
), New_N
, Instantiating
));
8499 if Instantiating
then
8500 Set_Assignment_OK
(Name
(New_N
), True);
8503 elsif Nkind
(N
) in N_Aggregate | N_Extension_Aggregate
then
8504 if not Instantiating
then
8505 Set_Associated_Node
(N
, New_N
);
8508 -- If, in the generic, the aggregate has a global composite type
8509 -- and, at the point of instantiation, the type has a private view
8510 -- then install the full view.
8513 Assoc
: constant Node_Id
:= Get_Associated_Node
(N
);
8517 and then Nkind
(Assoc
) = Nkind
(N
)
8518 and then Present
(Etype
(Assoc
))
8519 and then Is_Private_Type
(Etype
(Assoc
))
8521 Switch_View
(Etype
(Assoc
));
8525 -- Moreover, for a full aggregate, if the type is a derived tagged
8526 -- type and has a global ancestor, then also restore the full view
8527 -- of this ancestor and do so up to the root type. Beware that the
8528 -- Ancestor_Type field is overloaded, so test that it's an entity.
8530 if Nkind
(N
) = N_Aggregate
8531 and then Present
(Ancestor_Type
(N
))
8532 and then Nkind
(Ancestor_Type
(N
)) in N_Entity
8535 Root_Typ
: constant Entity_Id
:=
8536 Root_Type
(Ancestor_Type
(N
));
8538 Typ
: Entity_Id
:= Ancestor_Type
(N
);
8542 if Is_Private_Type
(Typ
) then
8546 exit when Typ
= Root_Typ
;
8554 -- Do not copy the associated node, which points to the generic copy
8555 -- of the aggregate.
8557 if Nkind
(N
) = N_Aggregate
then
8558 Set_Aggregate_Bounds
8560 Node_Id
(Copy_Generic_Descendant
8561 (Union_Id
(Aggregate_Bounds
(N
)))));
8563 elsif Nkind
(N
) = N_Extension_Aggregate
then
8566 Node_Id
(Copy_Generic_Descendant
8567 (Union_Id
(Ancestor_Part
(N
)))));
8570 pragma Assert
(False);
8575 List_Id
(Copy_Generic_Descendant
(Union_Id
(Expressions
(N
)))));
8576 Set_Component_Associations
8578 List_Id
(Copy_Generic_Descendant
8579 (Union_Id
(Component_Associations
(N
)))));
8581 (New_N
, Node_Id
(Copy_Generic_Descendant
(Union_Id
(Etype
(N
)))));
8583 -- Allocators do not have an identifier denoting the access type, so we
8584 -- must locate it through the expression to check whether the views are
8587 elsif Nkind
(N
) = N_Allocator
8588 and then Nkind
(Expression
(N
)) = N_Qualified_Expression
8589 and then Is_Entity_Name
(Subtype_Mark
(Expression
(N
)))
8590 and then Instantiating
8593 T
: constant Node_Id
:=
8594 Get_Associated_Node
(Subtype_Mark
(Expression
(N
)));
8600 -- Retrieve the allocator node in the generic copy
8602 Acc_T
:= Etype
(Parent
(Parent
(T
)));
8604 if Present
(Acc_T
) and then Is_Private_Type
(Acc_T
) then
8605 Switch_View
(Acc_T
);
8612 -- Loop parameter specifications do not have an identifier denoting the
8613 -- index type, so we must locate it through the defining identifier to
8614 -- check whether the views are consistent.
8616 elsif Nkind
(N
) = N_Loop_Parameter_Specification
8617 and then Instantiating
8620 Id
: constant Entity_Id
:=
8621 Get_Associated_Entity
(Defining_Identifier
(N
));
8623 Index_T
: Entity_Id
;
8626 if Present
(Id
) and then Present
(Etype
(Id
)) then
8627 Index_T
:= First_Subtype
(Etype
(Id
));
8629 if Present
(Index_T
) and then Is_Private_Type
(Index_T
) then
8630 Switch_View
(Index_T
);
8637 -- For a proper body, we must catch the case of a proper body that
8638 -- replaces a stub. This represents the point at which a separate
8639 -- compilation unit, and hence template file, may be referenced, so we
8640 -- must make a new source instantiation entry for the template of the
8641 -- subunit, and ensure that all nodes in the subunit are adjusted using
8642 -- this new source instantiation entry.
8644 elsif Nkind
(N
) in N_Proper_Body
then
8646 Save_Adjustment
: constant Sloc_Adjustment
:= S_Adjustment
;
8648 if Instantiating
and then Was_Originally_Stub
(N
) then
8649 Create_Instantiation_Source
8650 (Instantiation_Node
,
8651 Defining_Entity
(N
),
8654 Adjust_Instantiation_Sloc
(New_N
, S_Adjustment
);
8657 -- Now copy the fields of the proper body, using the new
8658 -- adjustment factor if one was needed as per test above.
8662 -- Restore the original adjustment factor
8664 S_Adjustment
:= Save_Adjustment
;
8667 elsif Nkind
(N
) = N_Pragma
and then Instantiating
then
8669 -- Do not copy Comment or Ident pragmas their content is relevant to
8670 -- the generic unit, not to the instantiating unit.
8672 if Pragma_Name_Unmapped
(N
) in Name_Comment | Name_Ident
then
8673 New_N
:= Make_Null_Statement
(Sloc
(N
));
8675 -- Do not copy pragmas generated from aspects because the pragmas do
8676 -- not carry any semantic information, plus they will be regenerated
8679 -- However, generating C we need to copy them since postconditions
8680 -- are inlined by the front end, and the front-end inlining machinery
8681 -- relies on this routine to perform inlining.
8683 elsif From_Aspect_Specification
(N
)
8684 and then not Modify_Tree_For_C
8686 New_N
:= Make_Null_Statement
(Sloc
(N
));
8692 elsif Nkind
(N
) in N_Integer_Literal | N_Real_Literal
then
8694 -- No descendant fields need traversing
8698 elsif Nkind
(N
) = N_String_Literal
8699 and then Present
(Etype
(N
))
8700 and then Instantiating
8702 -- If the string is declared in an outer scope, the string_literal
8703 -- subtype created for it may have the wrong scope. Force reanalysis
8704 -- of the constant to generate a new itype in the proper context.
8706 Set_Etype
(New_N
, Empty
);
8707 Set_Analyzed
(New_N
, False);
8709 -- For the remaining nodes, copy their descendants recursively
8714 if Instantiating
and then Nkind
(N
) = N_Subprogram_Body
then
8715 Set_Generic_Parent
(Specification
(New_N
), N
);
8717 -- Should preserve Corresponding_Spec??? (12.3(14))
8721 -- Propagate dimensions if present, so that they are reflected in the
8724 if Nkind
(N
) in N_Has_Etype
8725 and then (Nkind
(N
) in N_Op
or else Is_Entity_Name
(N
))
8726 and then Present
(Etype
(N
))
8727 and then Is_Floating_Point_Type
(Etype
(N
))
8728 and then Has_Dimension_System
(Etype
(N
))
8730 Copy_Dimensions
(N
, New_N
);
8734 end Copy_Generic_Node
;
8736 ----------------------------
8737 -- Denotes_Formal_Package --
8738 ----------------------------
8740 function Denotes_Formal_Package
8742 On_Exit
: Boolean := False;
8743 Instance
: Entity_Id
:= Empty
) return Boolean
8746 Scop
: constant Entity_Id
:= Scope
(Pack
);
8749 function Is_Actual_Of_Previous_Formal
(P
: Entity_Id
) return Boolean;
8750 -- The package in question may be an actual for a previous formal
8751 -- package P of the current instance, so examine its actuals as well.
8752 -- This must be recursive over other formal packages.
8754 ----------------------------------
8755 -- Is_Actual_Of_Previous_Formal --
8756 ----------------------------------
8758 function Is_Actual_Of_Previous_Formal
(P
: Entity_Id
) return Boolean is
8762 E1
:= First_Entity
(P
);
8763 while Present
(E1
) and then E1
/= Instance
loop
8764 if Ekind
(E1
) = E_Package
8765 and then Nkind
(Parent
(E1
)) = N_Package_Renaming_Declaration
8767 if Renamed_Entity
(E1
) = Pack
then
8770 elsif E1
= P
or else Renamed_Entity
(E1
) = P
then
8773 elsif Is_Actual_Of_Previous_Formal
(E1
) then
8782 end Is_Actual_Of_Previous_Formal
;
8784 -- Start of processing for Denotes_Formal_Package
8790 (Instance_Envs
.Last
).Instantiated_Parent
.Act_Id
;
8792 Par
:= Current_Instantiated_Parent
.Act_Id
;
8795 if Ekind
(Scop
) = E_Generic_Package
8796 or else Nkind
(Unit_Declaration_Node
(Scop
)) =
8797 N_Generic_Subprogram_Declaration
8801 elsif Nkind
(Original_Node
(Unit_Declaration_Node
(Pack
))) =
8802 N_Formal_Package_Declaration
8810 -- Check whether this package is associated with a formal package of
8811 -- the enclosing instantiation. Iterate over the list of renamings.
8813 E
:= First_Entity
(Par
);
8814 while Present
(E
) loop
8815 if Ekind
(E
) /= E_Package
8816 or else Nkind
(Parent
(E
)) /= N_Package_Renaming_Declaration
8820 elsif Renamed_Entity
(E
) = Par
then
8823 elsif Renamed_Entity
(E
) = Pack
then
8826 elsif Is_Actual_Of_Previous_Formal
(E
) then
8836 end Denotes_Formal_Package
;
8842 procedure End_Generic
is
8844 -- ??? More things could be factored out in this routine. Should
8845 -- probably be done at a later stage.
8847 Inside_A_Generic
:= Generic_Flags
.Table
(Generic_Flags
.Last
);
8848 Generic_Flags
.Decrement_Last
;
8850 Expander_Mode_Restore
;
8857 function Earlier
(N1
, N2
: Node_Id
) return Boolean is
8858 procedure Find_Depth
(P
: in out Node_Id
; D
: in out Integer);
8859 -- Find distance from given node to enclosing compilation unit
8865 procedure Find_Depth
(P
: in out Node_Id
; D
: in out Integer) is
8868 and then Nkind
(P
) /= N_Compilation_Unit
8870 P
:= True_Parent
(P
);
8875 -- Local declarations
8884 -- Start of processing for Earlier
8887 Find_Depth
(P1
, D1
);
8888 Find_Depth
(P2
, D2
);
8898 P1
:= True_Parent
(P1
);
8903 P2
:= True_Parent
(P2
);
8907 -- At this point P1 and P2 are at the same distance from the root.
8908 -- We examine their parents until we find a common declarative list.
8909 -- If we reach the root, N1 and N2 do not descend from the same
8910 -- declarative list (e.g. one is nested in the declarative part and
8911 -- the other is in a block in the statement part) and the earlier
8912 -- one is already frozen.
8914 while not Is_List_Member
(P1
)
8915 or else not Is_List_Member
(P2
)
8916 or else not In_Same_List
(P1
, P2
)
8918 P1
:= True_Parent
(P1
);
8919 P2
:= True_Parent
(P2
);
8921 if Nkind
(Parent
(P1
)) = N_Subunit
then
8922 P1
:= Corresponding_Stub
(Parent
(P1
));
8925 if Nkind
(Parent
(P2
)) = N_Subunit
then
8926 P2
:= Corresponding_Stub
(Parent
(P2
));
8934 -- Expanded code usually shares the source location of the original
8935 -- construct it was generated for. This however may not necessarily
8936 -- reflect the true location of the code within the tree.
8938 -- Before comparing the slocs of the two nodes, make sure that we are
8939 -- working with correct source locations. Assume that P1 is to the left
8940 -- of P2. If either one does not come from source, traverse the common
8941 -- list heading towards the other node and locate the first source
8945 -- ----+===+===+--------------+===+===+----
8946 -- expanded code expanded code
8948 if not Comes_From_Source
(P1
) then
8949 while Present
(P1
) loop
8951 -- Neither P2 nor a source statement were located during the
8952 -- search. If we reach the end of the list, then P1 does not
8953 -- occur earlier than P2.
8956 -- start --- P2 ----- P1 --- end
8958 if No
(Next
(P1
)) then
8961 -- We encounter P2 while going to the right of the list. This
8962 -- means that P1 does indeed appear earlier.
8965 -- start --- P1 ===== P2 --- end
8966 -- expanded code in between
8971 -- No need to look any further since we have located a source
8974 elsif Comes_From_Source
(P1
) then
8984 if not Comes_From_Source
(P2
) then
8985 while Present
(P2
) loop
8987 -- Neither P1 nor a source statement were located during the
8988 -- search. If we reach the start of the list, then P1 does not
8989 -- occur earlier than P2.
8992 -- start --- P2 --- P1 --- end
8994 if No
(Prev
(P2
)) then
8997 -- We encounter P1 while going to the left of the list. This
8998 -- means that P1 does indeed appear earlier.
9001 -- start --- P1 ===== P2 --- end
9002 -- expanded code in between
9007 -- No need to look any further since we have located a source
9010 elsif Comes_From_Source
(P2
) then
9020 -- At this point either both nodes came from source or we approximated
9021 -- their source locations through neighboring source statements.
9023 T1
:= Top_Level_Location
(Sloc
(P1
));
9024 T2
:= Top_Level_Location
(Sloc
(P2
));
9026 -- When two nodes come from the same instance, they have identical top
9027 -- level locations. To determine proper relation within the tree, check
9028 -- their locations within the template.
9031 return Sloc
(P1
) < Sloc
(P2
);
9033 -- The two nodes either come from unrelated instances or do not come
9034 -- from instantiated code at all.
9041 ----------------------
9042 -- Find_Actual_Type --
9043 ----------------------
9045 function Find_Actual_Type
9047 Gen_Type
: Entity_Id
) return Entity_Id
9049 Gen_Scope
: constant Entity_Id
:= Scope
(Gen_Type
);
9053 -- Special processing only applies to child units
9055 if not Is_Child_Unit
(Gen_Scope
) then
9056 return Get_Instance_Of
(Typ
);
9058 -- If designated or component type is itself a formal of the child unit,
9059 -- its instance is available.
9061 elsif Scope
(Typ
) = Gen_Scope
then
9062 return Get_Instance_Of
(Typ
);
9064 -- If the array or access type is not declared in the parent unit,
9065 -- no special processing needed.
9067 elsif not Is_Generic_Type
(Typ
)
9068 and then Scope
(Gen_Scope
) /= Scope
(Typ
)
9070 return Get_Instance_Of
(Typ
);
9072 -- Otherwise, retrieve designated or component type by visibility
9075 T
:= Current_Entity
(Typ
);
9076 while Present
(T
) loop
9077 if In_Open_Scopes
(Scope
(T
)) then
9079 elsif Is_Generic_Actual_Type
(T
) then
9088 end Find_Actual_Type
;
9090 -----------------------------
9091 -- Freeze_Package_Instance --
9092 -----------------------------
9094 procedure Freeze_Package_Instance
9100 function In_Same_Scope
(Gen_Id
, Act_Id
: Node_Id
) return Boolean;
9101 -- Check if the generic definition and the instantiation come from
9102 -- a common scope, in which case the instance must be frozen after
9103 -- the generic body.
9105 function True_Sloc
(N
, Act_Unit
: Node_Id
) return Source_Ptr
;
9106 -- If the instance is nested inside a generic unit, the Sloc of the
9107 -- instance indicates the place of the original definition, not the
9108 -- point of the current enclosing instance. Pending a better usage of
9109 -- Slocs to indicate instantiation places, we determine the place of
9110 -- origin of a node by finding the maximum sloc of any ancestor node.
9112 -- Why is this not equivalent to Top_Level_Location ???
9118 function In_Same_Scope
(Gen_Id
, Act_Id
: Node_Id
) return Boolean is
9119 Act_Scop
: Entity_Id
:= Scope
(Act_Id
);
9120 Gen_Scop
: Entity_Id
:= Scope
(Gen_Id
);
9123 while Act_Scop
/= Standard_Standard
9124 and then Gen_Scop
/= Standard_Standard
9126 if Act_Scop
= Gen_Scop
then
9130 Act_Scop
:= Scope
(Act_Scop
);
9131 Gen_Scop
:= Scope
(Gen_Scop
);
9141 function True_Sloc
(N
, Act_Unit
: Node_Id
) return Source_Ptr
is
9148 while Present
(N1
) and then N1
/= Act_Unit
loop
9149 if Sloc
(N1
) > Res
then
9161 Gen_Id
: constant Entity_Id
:= Get_Generic_Entity
(N
);
9162 Par_Id
: constant Entity_Id
:= Scope
(Gen_Id
);
9163 Act_Unit
: constant Node_Id
:= Unit
(Cunit
(Get_Source_Unit
(N
)));
9164 Gen_Unit
: constant Node_Id
:=
9165 Unit
(Cunit
(Get_Source_Unit
(Gen_Decl
)));
9167 Body_Unit
: Node_Id
;
9169 Must_Delay
: Boolean;
9170 Orig_Body
: Node_Id
;
9172 -- Start of processing for Freeze_Package_Instance
9175 -- If the body is a subunit, the freeze point is the corresponding stub
9176 -- in the current compilation, not the subunit itself.
9178 if Nkind
(Parent
(Gen_Body
)) = N_Subunit
then
9179 Orig_Body
:= Corresponding_Stub
(Parent
(Gen_Body
));
9181 Orig_Body
:= Gen_Body
;
9184 Body_Unit
:= Unit
(Cunit
(Get_Source_Unit
(Orig_Body
)));
9186 -- If the instantiation and the generic definition appear in the same
9187 -- package declaration, this is an early instantiation. If they appear
9188 -- in the same declarative part, it is an early instantiation only if
9189 -- the generic body appears textually later, and the generic body is
9190 -- also in the main unit.
9192 -- If instance is nested within a subprogram, and the generic body
9193 -- is not, the instance is delayed because the enclosing body is. If
9194 -- instance and body are within the same scope, or the same subprogram
9195 -- body, indicate explicitly that the instance is delayed.
9198 (Gen_Unit
= Act_Unit
9199 and then (Nkind
(Gen_Unit
) in N_Generic_Package_Declaration
9200 | N_Package_Declaration
9201 or else (Gen_Unit
= Body_Unit
9203 True_Sloc
(N
, Act_Unit
) < Sloc
(Orig_Body
)))
9204 and then Is_In_Main_Unit
(Original_Node
(Gen_Unit
))
9205 and then In_Same_Scope
(Gen_Id
, Act_Id
));
9207 -- If this is an early instantiation, the freeze node is placed after
9208 -- the generic body. Otherwise, if the generic appears in an instance,
9209 -- we cannot freeze the current instance until the outer one is frozen.
9210 -- This is only relevant if the current instance is nested within some
9211 -- inner scope not itself within the outer instance. If this scope is
9212 -- a package body in the same declarative part as the outer instance,
9213 -- then that body needs to be frozen after the outer instance. Finally,
9214 -- if no delay is needed, we place the freeze node at the end of the
9215 -- current declarative part.
9217 if No
(Freeze_Node
(Act_Id
))
9218 or else not Is_List_Member
(Freeze_Node
(Act_Id
))
9220 Ensure_Freeze_Node
(Act_Id
);
9221 F_Node
:= Freeze_Node
(Act_Id
);
9224 Insert_After
(Orig_Body
, F_Node
);
9226 elsif Is_Generic_Instance
(Par_Id
)
9227 and then Present
(Freeze_Node
(Par_Id
))
9228 and then Scope
(Act_Id
) /= Par_Id
9230 -- Freeze instance of inner generic after instance of enclosing
9233 if In_Same_Declarative_Part
(Parent
(Freeze_Node
(Par_Id
)), N
) then
9235 -- Handle the following case:
9237 -- package Parent_Inst is new ...
9238 -- freeze Parent_Inst []
9240 -- procedure P ... -- this body freezes Parent_Inst
9242 -- package Inst is new ...
9244 -- In this particular scenario, the freeze node for Inst must
9245 -- be inserted in the same manner as that of Parent_Inst,
9246 -- before the next source body or at the end of the declarative
9247 -- list (body not available). If body P did not exist and
9248 -- Parent_Inst was frozen after Inst, either by a body
9249 -- following Inst or at the end of the declarative region,
9250 -- the freeze node for Inst must be inserted after that of
9251 -- Parent_Inst. This relation is established by comparing
9252 -- the Slocs of Parent_Inst freeze node and Inst.
9253 -- We examine the parents of the enclosing lists to handle
9254 -- the case where the parent instance is in the visible part
9255 -- of a package declaration, and the inner instance is in
9256 -- the corresponding private part.
9258 if Parent
(List_Containing
(Freeze_Node
(Par_Id
)))
9259 = Parent
(List_Containing
(N
))
9260 and then Sloc
(Freeze_Node
(Par_Id
)) <= Sloc
(N
)
9262 Insert_Freeze_Node_For_Instance
(N
, F_Node
);
9264 Insert_After
(Freeze_Node
(Par_Id
), F_Node
);
9267 -- Freeze package enclosing instance of inner generic after
9268 -- instance of enclosing generic.
9270 elsif Nkind
(Parent
(N
)) in N_Package_Body | N_Subprogram_Body
9271 and then In_Same_Declarative_Part
9272 (Parent
(Freeze_Node
(Par_Id
)), Parent
(N
))
9275 Enclosing
: Entity_Id
;
9278 Enclosing
:= Corresponding_Spec
(Parent
(N
));
9280 if No
(Enclosing
) then
9281 Enclosing
:= Defining_Entity
(Parent
(N
));
9284 Insert_Freeze_Node_For_Instance
(N
, F_Node
);
9285 Ensure_Freeze_Node
(Enclosing
);
9287 if not Is_List_Member
(Freeze_Node
(Enclosing
)) then
9289 -- The enclosing context is a subunit, insert the freeze
9290 -- node after the stub.
9292 if Nkind
(Parent
(Parent
(N
))) = N_Subunit
then
9293 Insert_Freeze_Node_For_Instance
9294 (Corresponding_Stub
(Parent
(Parent
(N
))),
9295 Freeze_Node
(Enclosing
));
9297 -- The enclosing context is a package with a stub body
9298 -- which has already been replaced by the real body.
9299 -- Insert the freeze node after the actual body.
9301 elsif Ekind
(Enclosing
) = E_Package
9302 and then Present
(Body_Entity
(Enclosing
))
9303 and then Was_Originally_Stub
9304 (Parent
(Body_Entity
(Enclosing
)))
9306 Insert_Freeze_Node_For_Instance
9307 (Parent
(Body_Entity
(Enclosing
)),
9308 Freeze_Node
(Enclosing
));
9310 -- The parent instance has been frozen before the body of
9311 -- the enclosing package, insert the freeze node after
9314 elsif In_Same_List
(Freeze_Node
(Par_Id
), Parent
(N
))
9316 Sloc
(Freeze_Node
(Par_Id
)) <= Sloc
(Parent
(N
))
9318 Insert_Freeze_Node_For_Instance
9319 (Parent
(N
), Freeze_Node
(Enclosing
));
9323 (Freeze_Node
(Par_Id
), Freeze_Node
(Enclosing
));
9329 Insert_Freeze_Node_For_Instance
(N
, F_Node
);
9333 Insert_Freeze_Node_For_Instance
(N
, F_Node
);
9336 end Freeze_Package_Instance
;
9338 --------------------------------
9339 -- Freeze_Subprogram_Instance --
9340 --------------------------------
9342 procedure Freeze_Subprogram_Instance
9345 Pack_Id
: Entity_Id
)
9347 function Enclosing_Package_Body
(N
: Node_Id
) return Node_Id
;
9348 -- Find innermost package body that encloses the given node, and which
9349 -- is not a compilation unit. Freeze nodes for the instance, or for its
9350 -- enclosing body, may be inserted after the enclosing_body of the
9351 -- generic unit. Used to determine proper placement of freeze node for
9352 -- both package and subprogram instances.
9354 function Package_Freeze_Node
(B
: Node_Id
) return Node_Id
;
9355 -- Find entity for given package body, and locate or create a freeze
9358 ----------------------------
9359 -- Enclosing_Package_Body --
9360 ----------------------------
9362 function Enclosing_Package_Body
(N
: Node_Id
) return Node_Id
is
9368 and then Nkind
(Parent
(P
)) /= N_Compilation_Unit
9370 if Nkind
(P
) = N_Package_Body
then
9371 if Nkind
(Parent
(P
)) = N_Subunit
then
9372 return Corresponding_Stub
(Parent
(P
));
9378 P
:= True_Parent
(P
);
9382 end Enclosing_Package_Body
;
9384 -------------------------
9385 -- Package_Freeze_Node --
9386 -------------------------
9388 function Package_Freeze_Node
(B
: Node_Id
) return Node_Id
is
9392 if Nkind
(B
) = N_Package_Body
then
9393 Id
:= Corresponding_Spec
(B
);
9394 else pragma Assert
(Nkind
(B
) = N_Package_Body_Stub
);
9395 Id
:= Corresponding_Spec
(Proper_Body
(Unit
(Library_Unit
(B
))));
9398 Ensure_Freeze_Node
(Id
);
9399 return Freeze_Node
(Id
);
9400 end Package_Freeze_Node
;
9404 Enc_G
: constant Node_Id
:= Enclosing_Package_Body
(Gen_Body
);
9405 Enc_N
: constant Node_Id
:= Enclosing_Package_Body
(N
);
9406 Par_Id
: constant Entity_Id
:= Scope
(Get_Generic_Entity
(N
));
9411 -- Start of processing for Freeze_Subprogram_Instance
9414 -- If the instance and the generic body appear within the same unit, and
9415 -- the instance precedes the generic, the freeze node for the instance
9416 -- must appear after that of the generic. If the generic is nested
9417 -- within another instance I2, then current instance must be frozen
9418 -- after I2. In both cases, the freeze nodes are those of enclosing
9419 -- packages. Otherwise, the freeze node is placed at the end of the
9420 -- current declarative part.
9422 Ensure_Freeze_Node
(Pack_Id
);
9423 F_Node
:= Freeze_Node
(Pack_Id
);
9425 if Is_Generic_Instance
(Par_Id
)
9426 and then Present
(Freeze_Node
(Par_Id
))
9427 and then In_Same_Declarative_Part
(Parent
(Freeze_Node
(Par_Id
)), N
)
9429 -- The parent was a premature instantiation. Insert freeze node at
9430 -- the end the current declarative part.
9432 if Is_Known_Guaranteed_ABE
(Get_Unit_Instantiation_Node
(Par_Id
)) then
9433 Insert_Freeze_Node_For_Instance
(N
, F_Node
);
9435 -- Handle the following case:
9437 -- package Parent_Inst is new ...
9438 -- freeze Parent_Inst []
9440 -- procedure P ... -- this body freezes Parent_Inst
9442 -- procedure Inst is new ...
9444 -- In this particular scenario, the freeze node for Inst must be
9445 -- inserted in the same manner as that of Parent_Inst - before the
9446 -- next source body or at the end of the declarative list (body not
9447 -- available). If body P did not exist and Parent_Inst was frozen
9448 -- after Inst, either by a body following Inst or at the end of the
9449 -- declarative region, the freeze node for Inst must be inserted
9450 -- after that of Parent_Inst. This relation is established by
9451 -- comparing the Slocs of Parent_Inst freeze node and Inst.
9453 elsif In_Same_List
(Freeze_Node
(Par_Id
), N
)
9454 and then Sloc
(Freeze_Node
(Par_Id
)) <= Sloc
(N
)
9456 Insert_Freeze_Node_For_Instance
(N
, F_Node
);
9459 Insert_After
(Freeze_Node
(Par_Id
), F_Node
);
9462 -- The body enclosing the instance should be frozen after the body that
9463 -- includes the generic, because the body of the instance may make
9464 -- references to entities therein. If the two are not in the same
9465 -- declarative part, or if the one enclosing the instance is frozen
9466 -- already, freeze the instance at the end of the current declarative
9469 elsif Is_Generic_Instance
(Par_Id
)
9470 and then Present
(Freeze_Node
(Par_Id
))
9471 and then Present
(Enc_N
)
9473 if In_Same_Declarative_Part
(Parent
(Freeze_Node
(Par_Id
)), Enc_N
)
9475 -- The enclosing package may contain several instances. Rather
9476 -- than computing the earliest point at which to insert its freeze
9477 -- node, we place it at the end of the declarative part of the
9478 -- parent of the generic.
9480 Insert_Freeze_Node_For_Instance
9481 (Freeze_Node
(Par_Id
), Package_Freeze_Node
(Enc_N
));
9484 Insert_Freeze_Node_For_Instance
(N
, F_Node
);
9486 elsif Present
(Enc_G
)
9487 and then Present
(Enc_N
)
9488 and then Enc_G
/= Enc_N
9489 and then Earlier
(N
, Gen_Body
)
9491 -- Freeze package that encloses instance, and place node after the
9492 -- package that encloses generic. If enclosing package is already
9493 -- frozen we have to assume it is at the proper place. This may be a
9494 -- potential ABE that requires dynamic checking. Do not add a freeze
9495 -- node if the package that encloses the generic is inside the body
9496 -- that encloses the instance, because the freeze node would be in
9497 -- the wrong scope. Additional contortions needed if the bodies are
9498 -- within a subunit.
9501 Enclosing_Body
: Node_Id
;
9504 if Nkind
(Enc_N
) = N_Package_Body_Stub
then
9505 Enclosing_Body
:= Proper_Body
(Unit
(Library_Unit
(Enc_N
)));
9507 Enclosing_Body
:= Enc_N
;
9510 if Parent
(List_Containing
(Enc_G
)) /= Enclosing_Body
then
9511 Insert_Freeze_Node_For_Instance
9512 (Enc_G
, Package_Freeze_Node
(Enc_N
));
9516 -- Freeze enclosing subunit before instance
9518 Enc_G_F
:= Package_Freeze_Node
(Enc_G
);
9520 if not Is_List_Member
(Enc_G_F
) then
9521 Insert_After
(Enc_G
, Enc_G_F
);
9524 Insert_Freeze_Node_For_Instance
(N
, F_Node
);
9527 -- If none of the above, insert freeze node at the end of the current
9528 -- declarative part.
9530 Insert_Freeze_Node_For_Instance
(N
, F_Node
);
9532 end Freeze_Subprogram_Instance
;
9538 function Get_Gen_Id
(E
: Assoc_Ptr
) return Entity_Id
is
9540 return Generic_Renamings
.Table
(E
).Gen_Id
;
9543 ---------------------
9544 -- Get_Instance_Of --
9545 ---------------------
9547 function Get_Instance_Of
(A
: Entity_Id
) return Entity_Id
is
9548 Res
: constant Assoc_Ptr
:= Generic_Renamings_HTable
.Get
(A
);
9551 if Res
/= Assoc_Null
then
9552 return Generic_Renamings
.Table
(Res
).Act_Id
;
9555 -- On exit, entity is not instantiated: not a generic parameter, or
9556 -- else parameter of an inner generic unit.
9560 end Get_Instance_Of
;
9562 ---------------------------------
9563 -- Get_Unit_Instantiation_Node --
9564 ---------------------------------
9566 function Get_Unit_Instantiation_Node
(A
: Entity_Id
) return Node_Id
is
9567 Decl
: Node_Id
:= Unit_Declaration_Node
(A
);
9571 -- If the Package_Instantiation attribute has been set on the package
9572 -- entity, then use it directly when it (or its Original_Node) refers
9573 -- to an N_Package_Instantiation node. In principle it should be
9574 -- possible to have this field set in all cases, which should be
9575 -- investigated, and would allow this function to be significantly
9578 Inst
:= Package_Instantiation
(A
);
9580 if Present
(Inst
) then
9581 if Nkind
(Inst
) = N_Package_Instantiation
then
9584 elsif Nkind
(Original_Node
(Inst
)) = N_Package_Instantiation
then
9585 return Original_Node
(Inst
);
9589 -- If the instantiation is a compilation unit that does not need body
9590 -- then the instantiation node has been rewritten as a package
9591 -- declaration for the instance, and we return the original node.
9593 -- If it is a compilation unit and the instance node has not been
9594 -- rewritten, then it is still the unit of the compilation. Finally, if
9595 -- a body is present, this is a parent of the main unit whose body has
9596 -- been compiled for inlining purposes, and the instantiation node has
9597 -- been rewritten with the instance body.
9599 -- Otherwise the instantiation node appears after the declaration. If
9600 -- the entity is a formal package, the declaration may have been
9601 -- rewritten as a generic declaration (in the case of a formal with box)
9602 -- or left as a formal package declaration if it has actuals, and is
9603 -- found with a forward search.
9605 if Nkind
(Parent
(Decl
)) = N_Compilation_Unit
then
9606 if Nkind
(Decl
) = N_Package_Declaration
9607 and then Present
(Corresponding_Body
(Decl
))
9609 Decl
:= Unit_Declaration_Node
(Corresponding_Body
(Decl
));
9612 if Nkind
(Original_Node
(Decl
)) in N_Generic_Instantiation
then
9613 return Original_Node
(Decl
);
9615 return Unit
(Parent
(Decl
));
9618 elsif Nkind
(Decl
) = N_Package_Declaration
9619 and then Nkind
(Original_Node
(Decl
)) = N_Formal_Package_Declaration
9621 return Original_Node
(Decl
);
9624 Inst
:= Next
(Decl
);
9625 while Nkind
(Inst
) not in N_Formal_Package_Declaration
9626 | N_Function_Instantiation
9627 | N_Package_Instantiation
9628 | N_Procedure_Instantiation
9635 end Get_Unit_Instantiation_Node
;
9637 ------------------------
9638 -- Has_Been_Exchanged --
9639 ------------------------
9641 function Has_Been_Exchanged
(E
: Entity_Id
) return Boolean is
9645 Next
:= First_Elmt
(Exchanged_Views
);
9646 while Present
(Next
) loop
9647 if Full_View
(Node
(Next
)) = E
then
9655 end Has_Been_Exchanged
;
9661 function Has_Contracts
(Decl
: Node_Id
) return Boolean is
9662 A_List
: constant List_Id
:= Aspect_Specifications
(Decl
);
9669 A_Spec
:= First
(A_List
);
9670 while Present
(A_Spec
) loop
9671 A_Id
:= Get_Aspect_Id
(A_Spec
);
9672 if A_Id
= Aspect_Pre
or else A_Id
= Aspect_Post
then
9687 function Hash
(F
: Entity_Id
) return HTable_Range
is
9689 return HTable_Range
(F
mod HTable_Size
);
9692 ------------------------
9693 -- Hide_Current_Scope --
9694 ------------------------
9696 procedure Hide_Current_Scope
is
9697 C
: constant Entity_Id
:= Current_Scope
;
9701 Set_Is_Hidden_Open_Scope
(C
);
9703 E
:= First_Entity
(C
);
9704 while Present
(E
) loop
9705 if Is_Immediately_Visible
(E
) then
9706 Set_Is_Immediately_Visible
(E
, False);
9707 Append_Elmt
(E
, Hidden_Entities
);
9713 -- Make the scope name invisible as well. This is necessary, but might
9714 -- conflict with calls to Rtsfind later on, in case the scope is a
9715 -- predefined one. There is no clean solution to this problem, so for
9716 -- now we depend on the user not redefining Standard itself in one of
9717 -- the parent units.
9719 if Is_Immediately_Visible
(C
) and then C
/= Standard_Standard
then
9720 Set_Is_Immediately_Visible
(C
, False);
9721 Append_Elmt
(C
, Hidden_Entities
);
9724 end Hide_Current_Scope
;
9730 procedure Init_Env
is
9731 Saved
: Instance_Env
;
9734 Saved
.Instantiated_Parent
:= Current_Instantiated_Parent
;
9735 Saved
.Exchanged_Views
:= Exchanged_Views
;
9736 Saved
.Hidden_Entities
:= Hidden_Entities
;
9737 Saved
.Current_Sem_Unit
:= Current_Sem_Unit
;
9738 Saved
.Parent_Unit_Visible
:= Parent_Unit_Visible
;
9739 Saved
.Instance_Parent_Unit
:= Instance_Parent_Unit
;
9741 -- Save configuration switches. These may be reset if the unit is a
9742 -- predefined unit, and the current mode is not Ada 2005.
9744 Saved
.Switches
:= Save_Config_Switches
;
9746 Instance_Envs
.Append
(Saved
);
9748 Exchanged_Views
:= New_Elmt_List
;
9749 Hidden_Entities
:= New_Elmt_List
;
9751 -- Make dummy entry for Instantiated parent. If generic unit is legal,
9752 -- this is set properly in Set_Instance_Env.
9754 Current_Instantiated_Parent
:=
9755 (Current_Scope
, Current_Scope
, Assoc_Null
);
9758 ---------------------
9759 -- In_Main_Context --
9760 ---------------------
9762 function In_Main_Context
(E
: Entity_Id
) return Boolean is
9768 if not Is_Compilation_Unit
(E
)
9769 or else Ekind
(E
) /= E_Package
9770 or else In_Private_Part
(E
)
9775 Context
:= Context_Items
(Cunit
(Main_Unit
));
9777 Clause
:= First
(Context
);
9778 while Present
(Clause
) loop
9779 if Nkind
(Clause
) = N_With_Clause
then
9780 Nam
:= Name
(Clause
);
9782 -- If the current scope is part of the context of the main unit,
9783 -- analysis of the corresponding with_clause is not complete, and
9784 -- the entity is not set. We use the Chars field directly, which
9785 -- might produce false positives in rare cases, but guarantees
9786 -- that we produce all the instance bodies we will need.
9788 if (Is_Entity_Name
(Nam
) and then Chars
(Nam
) = Chars
(E
))
9789 or else (Nkind
(Nam
) = N_Selected_Component
9790 and then Chars
(Selector_Name
(Nam
)) = Chars
(E
))
9800 end In_Main_Context
;
9802 ---------------------
9803 -- Inherit_Context --
9804 ---------------------
9806 procedure Inherit_Context
(Gen_Decl
: Node_Id
; Inst
: Node_Id
) is
9807 Current_Context
: List_Id
;
9808 Current_Unit
: Node_Id
;
9817 if Nkind
(Parent
(Gen_Decl
)) = N_Compilation_Unit
then
9819 -- The inherited context is attached to the enclosing compilation
9820 -- unit. This is either the main unit, or the declaration for the
9821 -- main unit (in case the instantiation appears within the package
9822 -- declaration and the main unit is its body).
9824 Current_Unit
:= Parent
(Inst
);
9825 while Present
(Current_Unit
)
9826 and then Nkind
(Current_Unit
) /= N_Compilation_Unit
9828 Current_Unit
:= Parent
(Current_Unit
);
9831 Current_Context
:= Context_Items
(Current_Unit
);
9833 Item
:= First
(Context_Items
(Parent
(Gen_Decl
)));
9834 while Present
(Item
) loop
9835 if Nkind
(Item
) = N_With_Clause
then
9836 Lib_Unit
:= Library_Unit
(Item
);
9838 -- Take care to prevent direct cyclic with's
9840 if Lib_Unit
/= Current_Unit
then
9842 -- Do not add a unit if it is already in the context
9844 Clause
:= First
(Current_Context
);
9846 while Present
(Clause
) loop
9847 if Nkind
(Clause
) = N_With_Clause
9848 and then Library_Unit
(Clause
) = Lib_Unit
9858 New_I
:= New_Copy
(Item
);
9859 Set_Implicit_With
(New_I
);
9861 Append
(New_I
, Current_Context
);
9869 end Inherit_Context
;
9875 procedure Initialize
is
9877 Generic_Renamings
.Init
;
9880 Generic_Renamings_HTable
.Reset
;
9881 Circularity_Detected
:= False;
9882 Exchanged_Views
:= No_Elist
;
9883 Hidden_Entities
:= No_Elist
;
9886 -------------------------------------
9887 -- Insert_Freeze_Node_For_Instance --
9888 -------------------------------------
9890 procedure Insert_Freeze_Node_For_Instance
9894 function Enclosing_Body
(N
: Node_Id
) return Node_Id
;
9895 -- Find enclosing package or subprogram body, if any. Freeze node may
9896 -- be placed at end of current declarative list if previous instance
9897 -- and current one have different enclosing bodies.
9899 function Previous_Instance
(Gen
: Entity_Id
) return Entity_Id
;
9900 -- Find the local instance, if any, that declares the generic that is
9901 -- being instantiated. If present, the freeze node for this instance
9902 -- must follow the freeze node for the previous instance.
9904 --------------------
9905 -- Enclosing_Body --
9906 --------------------
9908 function Enclosing_Body
(N
: Node_Id
) return Node_Id
is
9914 and then Nkind
(Parent
(P
)) /= N_Compilation_Unit
9916 if Nkind
(P
) in N_Package_Body | N_Subprogram_Body
then
9917 if Nkind
(Parent
(P
)) = N_Subunit
then
9918 return Corresponding_Stub
(Parent
(P
));
9924 P
:= True_Parent
(P
);
9930 -----------------------
9931 -- Previous_Instance --
9932 -----------------------
9934 function Previous_Instance
(Gen
: Entity_Id
) return Entity_Id
is
9939 while Present
(S
) and then S
/= Standard_Standard
loop
9940 if Is_Generic_Instance
(S
)
9941 and then In_Same_Source_Unit
(S
, N
)
9950 end Previous_Instance
;
9961 -- Start of processing for Insert_Freeze_Node_For_Instance
9964 -- Nothing to do if the freeze node has already been inserted
9966 if Is_List_Member
(F_Node
) then
9970 Inst
:= Entity
(F_Node
);
9972 -- When processing a subprogram instantiation, utilize the actual
9973 -- subprogram instantiation rather than its package wrapper as it
9974 -- carries all the context information.
9976 if Is_Wrapper_Package
(Inst
) then
9977 Inst
:= Related_Instance
(Inst
);
9980 Par_Inst
:= Parent
(Inst
);
9982 -- If this is a package instance, check whether the generic is declared
9983 -- in a previous instance and the current instance is not within the
9986 if Present
(Generic_Parent
(Par_Inst
)) and then Is_In_Main_Unit
(N
) then
9988 Enclosing_N
: constant Node_Id
:= Enclosing_Body
(N
);
9989 Par_I
: constant Entity_Id
:=
9990 Previous_Instance
(Generic_Parent
(Par_Inst
));
9994 if Present
(Par_I
) and then Earlier
(N
, Freeze_Node
(Par_I
)) then
9995 Scop
:= Scope
(Inst
);
9997 -- If the current instance is within the one that contains
9998 -- the generic, the freeze node for the current one must
9999 -- appear in the current declarative part. Ditto, if the
10000 -- current instance is within another package instance or
10001 -- within a body that does not enclose the current instance.
10002 -- In these three cases the freeze node of the previous
10003 -- instance is not relevant.
10005 while Present
(Scop
) and then Scop
/= Standard_Standard
loop
10006 exit when Scop
= Par_I
10008 (Is_Generic_Instance
(Scop
)
10009 and then Scope_Depth
(Scop
) > Scope_Depth
(Par_I
));
10010 Scop
:= Scope
(Scop
);
10013 -- Previous instance encloses current instance
10015 if Scop
= Par_I
then
10018 -- If the next node is a source body we must freeze in the
10019 -- current scope as well.
10021 elsif Present
(Next
(N
))
10022 and then Nkind
(Next
(N
)) in N_Subprogram_Body
10024 and then Comes_From_Source
(Next
(N
))
10028 -- Current instance is within an unrelated instance
10030 elsif Is_Generic_Instance
(Scop
) then
10033 -- Current instance is within an unrelated body
10035 elsif Present
(Enclosing_N
)
10036 and then Enclosing_N
/= Enclosing_Body
(Par_I
)
10041 Insert_After
(Freeze_Node
(Par_I
), F_Node
);
10049 Decls
:= List_Containing
(N
);
10050 Par_N
:= Parent
(Decls
);
10053 -- Determine the proper freeze point of an instantiation
10055 if Is_Generic_Instance
(Inst
) then
10057 -- When the instantiation occurs in a package spec, append the
10058 -- freeze node to the private declarations (if any).
10060 if Nkind
(Par_N
) = N_Package_Specification
10061 and then Decls
= Visible_Declarations
(Par_N
)
10062 and then not Is_Empty_List
(Private_Declarations
(Par_N
))
10064 Decls
:= Private_Declarations
(Par_N
);
10065 Decl
:= First
(Decls
);
10068 -- We adhere to the general rule of a package or subprogram body
10069 -- causing freezing of anything before it in the same declarative
10070 -- region. In this respect, the proper freeze point of a package
10071 -- instantiation is before the first source body which follows, or
10072 -- before a stub. This ensures that entities from the instance are
10073 -- already frozen and therefore usable in source bodies.
10075 if Nkind
(Par_N
) /= N_Package_Declaration
10077 not In_Same_Source_Unit
(Generic_Parent
(Par_Inst
), Inst
)
10079 while Present
(Decl
) loop
10080 if ((Nkind
(Decl
) in N_Unit_Body
10082 Nkind
(Decl
) in N_Body_Stub
)
10083 and then Comes_From_Source
(Decl
))
10084 or else (Present
(Origin
)
10085 and then Nkind
(Decl
) in N_Generic_Instantiation
10086 and then Instance_Spec
(Decl
) /= Origin
)
10088 Set_Sloc
(F_Node
, Sloc
(Decl
));
10089 Insert_Before
(Decl
, F_Node
);
10097 -- When the instantiation occurs in a package spec and there is
10098 -- no source body which follows, and the package has a body but
10099 -- is delayed, then insert immediately before its freeze node.
10101 if Nkind
(Par_N
) = N_Package_Specification
10102 and then Present
(Corresponding_Body
(Parent
(Par_N
)))
10103 and then Present
(Freeze_Node
(Defining_Entity
(Par_N
)))
10105 Set_Sloc
(F_Node
, Sloc
(Freeze_Node
(Defining_Entity
(Par_N
))));
10106 Insert_Before
(Freeze_Node
(Defining_Entity
(Par_N
)), F_Node
);
10109 -- When the instantiation occurs in a package spec and there is
10110 -- no source body which follows, not even of the package itself,
10111 -- then insert into the declaration list of the outer level, but
10112 -- do not jump over following instantiations in this list because
10113 -- they may have a body that has not materialized yet, see above.
10115 elsif Nkind
(Par_N
) = N_Package_Specification
10116 and then No
(Corresponding_Body
(Parent
(Par_N
)))
10117 and then Is_List_Member
(Parent
(Par_N
))
10119 Decl
:= Parent
(Par_N
);
10120 Decls
:= List_Containing
(Decl
);
10121 Par_N
:= Parent
(Decls
);
10124 -- In a package declaration, or if no source body which follows
10125 -- and at library level, then insert at end of list.
10133 -- Insert and adjust the Sloc of the freeze node
10135 Set_Sloc
(F_Node
, Sloc
(Last
(Decls
)));
10136 Insert_After
(Last
(Decls
), F_Node
);
10137 end Insert_Freeze_Node_For_Instance
;
10139 -----------------------------
10140 -- Install_Formal_Packages --
10141 -----------------------------
10143 procedure Install_Formal_Packages
(Par
: Entity_Id
) is
10146 Gen_E
: Entity_Id
:= Empty
;
10149 E
:= First_Entity
(Par
);
10151 -- If we are installing an instance parent, locate the formal packages
10152 -- of its generic parent.
10154 if Is_Generic_Instance
(Par
) then
10155 Gen
:= Generic_Parent
(Package_Specification
(Par
));
10156 Gen_E
:= First_Entity
(Gen
);
10159 while Present
(E
) loop
10160 if Ekind
(E
) = E_Package
10161 and then Nkind
(Parent
(E
)) = N_Package_Renaming_Declaration
10163 -- If this is the renaming for the parent instance, done
10165 if Renamed_Entity
(E
) = Par
then
10168 -- The visibility of a formal of an enclosing generic is already
10171 elsif Denotes_Formal_Package
(E
) then
10174 elsif Present
(Associated_Formal_Package
(E
)) then
10175 Check_Generic_Actuals
(Renamed_Entity
(E
), True);
10176 Set_Is_Hidden
(E
, False);
10178 -- Find formal package in generic unit that corresponds to
10179 -- (instance of) formal package in instance.
10181 while Present
(Gen_E
) and then Chars
(Gen_E
) /= Chars
(E
) loop
10182 Next_Entity
(Gen_E
);
10185 if Present
(Gen_E
) then
10186 Map_Formal_Package_Entities
(Gen_E
, E
);
10193 if Present
(Gen_E
) then
10194 Next_Entity
(Gen_E
);
10197 end Install_Formal_Packages
;
10199 --------------------
10200 -- Install_Parent --
10201 --------------------
10203 procedure Install_Parent
(P
: Entity_Id
; In_Body
: Boolean := False) is
10204 Ancestors
: constant Elist_Id
:= New_Elmt_List
;
10205 S
: constant Entity_Id
:= Current_Scope
;
10206 Inst_Par
: Entity_Id
;
10207 First_Par
: Entity_Id
;
10208 Inst_Node
: Node_Id
;
10209 Gen_Par
: Entity_Id
;
10210 First_Gen
: Entity_Id
;
10213 procedure Install_Noninstance_Specs
(Par
: Entity_Id
);
10214 -- Install the scopes of noninstance parent units ending with Par
10216 procedure Install_Spec
(Par
: Entity_Id
);
10217 -- The child unit is within the declarative part of the parent, so the
10218 -- declarations within the parent are immediately visible.
10220 -------------------------------
10221 -- Install_Noninstance_Specs --
10222 -------------------------------
10224 procedure Install_Noninstance_Specs
(Par
: Entity_Id
) is
10227 and then Par
/= Standard_Standard
10228 and then not In_Open_Scopes
(Par
)
10230 Install_Noninstance_Specs
(Scope
(Par
));
10231 Install_Spec
(Par
);
10233 end Install_Noninstance_Specs
;
10239 procedure Install_Spec
(Par
: Entity_Id
) is
10240 Spec
: constant Node_Id
:= Package_Specification
(Par
);
10243 -- If this parent of the child instance is a top-level unit,
10244 -- then record the unit and its visibility for later resetting in
10245 -- Remove_Parent. We exclude units that are generic instances, as we
10246 -- only want to record this information for the ultimate top-level
10247 -- noninstance parent (is that always correct???).
10249 if Scope
(Par
) = Standard_Standard
10250 and then not Is_Generic_Instance
(Par
)
10252 Parent_Unit_Visible
:= Is_Immediately_Visible
(Par
);
10253 Instance_Parent_Unit
:= Par
;
10256 -- Open the parent scope and make it and its declarations visible.
10257 -- If this point is not within a body, then only the visible
10258 -- declarations should be made visible, and installation of the
10259 -- private declarations is deferred until the appropriate point
10260 -- within analysis of the spec being instantiated (see the handling
10261 -- of parent visibility in Analyze_Package_Specification). This is
10262 -- relaxed in the case where the parent unit is Ada.Tags, to avoid
10263 -- private view problems that occur when compiling instantiations of
10264 -- a generic child of that package (Generic_Dispatching_Constructor).
10265 -- If the instance freezes a tagged type, inlinings of operations
10266 -- from Ada.Tags may need the full view of type Tag. If inlining took
10267 -- proper account of establishing visibility of inlined subprograms'
10268 -- parents then it should be possible to remove this
10269 -- special check. ???
10272 Set_Is_Immediately_Visible
(Par
);
10273 Install_Visible_Declarations
(Par
);
10274 Set_Use
(Visible_Declarations
(Spec
));
10276 if In_Body
or else Is_RTU
(Par
, Ada_Tags
) then
10277 Install_Private_Declarations
(Par
);
10278 Set_Use
(Private_Declarations
(Spec
));
10282 -- Start of processing for Install_Parent
10285 -- We need to install the parent instance to compile the instantiation
10286 -- of the child, but the child instance must appear in the current
10287 -- scope. Given that we cannot place the parent above the current scope
10288 -- in the scope stack, we duplicate the current scope and unstack both
10289 -- after the instantiation is complete.
10291 -- If the parent is itself the instantiation of a child unit, we must
10292 -- also stack the instantiation of its parent, and so on. Each such
10293 -- ancestor is the prefix of the name in a prior instantiation.
10295 -- If this is a nested instance, the parent unit itself resolves to
10296 -- a renaming of the parent instance, whose declaration we need.
10298 -- Finally, the parent may be a generic (not an instance) when the
10299 -- child unit appears as a formal package.
10303 if Present
(Renamed_Entity
(Inst_Par
)) then
10304 Inst_Par
:= Renamed_Entity
(Inst_Par
);
10307 First_Par
:= Inst_Par
;
10309 Gen_Par
:= Generic_Parent
(Package_Specification
(Inst_Par
));
10311 First_Gen
:= Gen_Par
;
10313 while Present
(Gen_Par
) and then Is_Child_Unit
(Gen_Par
) loop
10315 -- Load grandparent instance as well
10317 Inst_Node
:= Get_Unit_Instantiation_Node
(Inst_Par
);
10319 if Nkind
(Name
(Inst_Node
)) = N_Expanded_Name
then
10320 Inst_Par
:= Entity
(Prefix
(Name
(Inst_Node
)));
10322 if Present
(Renamed_Entity
(Inst_Par
)) then
10323 Inst_Par
:= Renamed_Entity
(Inst_Par
);
10326 Gen_Par
:= Generic_Parent
(Package_Specification
(Inst_Par
));
10328 if Present
(Gen_Par
) then
10329 Prepend_Elmt
(Inst_Par
, Ancestors
);
10332 -- Parent is not the name of an instantiation
10334 Install_Noninstance_Specs
(Inst_Par
);
10345 if Present
(First_Gen
) then
10346 Append_Elmt
(First_Par
, Ancestors
);
10348 Install_Noninstance_Specs
(First_Par
);
10351 if not Is_Empty_Elmt_List
(Ancestors
) then
10352 Elmt
:= First_Elmt
(Ancestors
);
10353 while Present
(Elmt
) loop
10354 Install_Spec
(Node
(Elmt
));
10355 Install_Formal_Packages
(Node
(Elmt
));
10360 if not In_Body
then
10363 end Install_Parent
;
10365 -------------------------------
10366 -- Install_Hidden_Primitives --
10367 -------------------------------
10369 procedure Install_Hidden_Primitives
10370 (Prims_List
: in out Elist_Id
;
10375 List
: Elist_Id
:= No_Elist
;
10376 Prim_G_Elmt
: Elmt_Id
;
10377 Prim_A_Elmt
: Elmt_Id
;
10382 -- No action needed in case of serious errors because we cannot trust
10383 -- in the order of primitives
10385 if Serious_Errors_Detected
> 0 then
10388 -- No action possible if we don't have available the list of primitive
10392 or else not Is_Record_Type
(Gen_T
)
10393 or else not Is_Tagged_Type
(Gen_T
)
10394 or else not Is_Record_Type
(Act_T
)
10395 or else not Is_Tagged_Type
(Act_T
)
10399 -- There is no need to handle interface types since their primitives
10400 -- cannot be hidden
10402 elsif Is_Interface
(Gen_T
) then
10406 Prim_G_Elmt
:= First_Elmt
(Primitive_Operations
(Gen_T
));
10408 if not Is_Class_Wide_Type
(Act_T
) then
10409 Prim_A_Elmt
:= First_Elmt
(Primitive_Operations
(Act_T
));
10411 Prim_A_Elmt
:= First_Elmt
(Primitive_Operations
(Root_Type
(Act_T
)));
10415 -- Skip predefined primitives in the generic formal
10417 while Present
(Prim_G_Elmt
)
10418 and then Is_Predefined_Dispatching_Operation
(Node
(Prim_G_Elmt
))
10420 Next_Elmt
(Prim_G_Elmt
);
10423 -- Skip predefined primitives in the generic actual
10425 while Present
(Prim_A_Elmt
)
10426 and then Is_Predefined_Dispatching_Operation
(Node
(Prim_A_Elmt
))
10428 Next_Elmt
(Prim_A_Elmt
);
10431 exit when No
(Prim_G_Elmt
) or else No
(Prim_A_Elmt
);
10433 Prim_G
:= Node
(Prim_G_Elmt
);
10434 Prim_A
:= Node
(Prim_A_Elmt
);
10436 -- There is no need to handle interface primitives because their
10437 -- primitives are not hidden
10439 exit when Present
(Interface_Alias
(Prim_G
));
10441 -- Here we install one hidden primitive
10443 if Chars
(Prim_G
) /= Chars
(Prim_A
)
10444 and then Has_Suffix
(Prim_A
, 'P')
10445 and then Remove_Suffix
(Prim_A
, 'P') = Chars
(Prim_G
)
10447 Set_Chars
(Prim_A
, Chars
(Prim_G
));
10448 Append_New_Elmt
(Prim_A
, To
=> List
);
10451 Next_Elmt
(Prim_A_Elmt
);
10452 Next_Elmt
(Prim_G_Elmt
);
10455 -- Append the elements to the list of temporarily visible primitives
10456 -- avoiding duplicates.
10458 if Present
(List
) then
10459 if No
(Prims_List
) then
10460 Prims_List
:= New_Elmt_List
;
10463 Elmt
:= First_Elmt
(List
);
10464 while Present
(Elmt
) loop
10465 Append_Unique_Elmt
(Node
(Elmt
), Prims_List
);
10469 end Install_Hidden_Primitives
;
10471 -------------------------------
10472 -- Restore_Hidden_Primitives --
10473 -------------------------------
10475 procedure Restore_Hidden_Primitives
(Prims_List
: in out Elist_Id
) is
10476 Prim_Elmt
: Elmt_Id
;
10480 if Present
(Prims_List
) then
10481 Prim_Elmt
:= First_Elmt
(Prims_List
);
10482 while Present
(Prim_Elmt
) loop
10483 Prim
:= Node
(Prim_Elmt
);
10484 Set_Chars
(Prim
, Add_Suffix
(Prim
, 'P'));
10485 Next_Elmt
(Prim_Elmt
);
10488 Prims_List
:= No_Elist
;
10490 end Restore_Hidden_Primitives
;
10492 --------------------------------
10493 -- Instantiate_Formal_Package --
10494 --------------------------------
10496 function Instantiate_Formal_Package
10499 Analyzed_Formal
: Node_Id
) return List_Id
10501 Loc
: constant Source_Ptr
:= Sloc
(Actual
);
10502 Hidden_Formals
: constant Elist_Id
:= New_Elmt_List
;
10504 Actual_Pack
: Entity_Id
;
10505 Formal_Pack
: Entity_Id
;
10506 Gen_Parent
: Entity_Id
;
10509 Parent_Spec
: Node_Id
;
10511 procedure Find_Matching_Actual
10513 Act
: in out Entity_Id
);
10514 -- We need to associate each formal entity in the formal package with
10515 -- the corresponding entity in the actual package. The actual package
10516 -- has been analyzed and possibly expanded, and as a result there is
10517 -- no one-to-one correspondence between the two lists (for example,
10518 -- the actual may include subtypes, itypes, and inherited primitive
10519 -- operations, interspersed among the renaming declarations for the
10520 -- actuals). We retrieve the corresponding actual by name because each
10521 -- actual has the same name as the formal, and they do appear in the
10524 function Get_Formal_Entity
(N
: Node_Id
) return Entity_Id
;
10525 -- Retrieve entity of defining entity of generic formal parameter.
10526 -- Only the declarations of formals need to be considered when
10527 -- linking them to actuals, but the declarative list may include
10528 -- internal entities generated during analysis, and those are ignored.
10530 procedure Match_Formal_Entity
10531 (Formal_Node
: Node_Id
;
10532 Formal_Ent
: Entity_Id
;
10533 Actual_Ent
: Entity_Id
);
10534 -- Associates the formal entity with the actual. In the case where
10535 -- Formal_Ent is a formal package, this procedure iterates through all
10536 -- of its formals and enters associations between the actuals occurring
10537 -- in the formal package's corresponding actual package (given by
10538 -- Actual_Ent) and the formal package's formal parameters. This
10539 -- procedure recurses if any of the parameters is itself a package.
10541 function Is_Instance_Of
10542 (Act_Spec
: Entity_Id
;
10543 Gen_Anc
: Entity_Id
) return Boolean;
10544 -- The actual can be an instantiation of a generic within another
10545 -- instance, in which case there is no direct link from it to the
10546 -- original generic ancestor. In that case, we recognize that the
10547 -- ultimate ancestor is the same by examining names and scopes.
10549 procedure Process_Nested_Formal
(Formal
: Entity_Id
);
10550 -- If the current formal is declared with a box, its own formals are
10551 -- visible in the instance, as they were in the generic, and their
10552 -- Hidden flag must be reset. If some of these formals are themselves
10553 -- packages declared with a box, the processing must be recursive.
10555 --------------------------
10556 -- Find_Matching_Actual --
10557 --------------------------
10559 procedure Find_Matching_Actual
10561 Act
: in out Entity_Id
)
10563 Formal_Ent
: Entity_Id
;
10566 case Nkind
(Original_Node
(F
)) is
10567 when N_Formal_Object_Declaration
10568 | N_Formal_Type_Declaration
10570 Formal_Ent
:= Defining_Identifier
(F
);
10572 while Present
(Act
)
10573 and then Chars
(Act
) /= Chars
(Formal_Ent
)
10578 when N_Formal_Package_Declaration
10579 | N_Formal_Subprogram_Declaration
10580 | N_Generic_Package_Declaration
10581 | N_Package_Declaration
10583 Formal_Ent
:= Defining_Entity
(F
);
10585 while Present
(Act
)
10586 and then Chars
(Act
) /= Chars
(Formal_Ent
)
10592 raise Program_Error
;
10594 end Find_Matching_Actual
;
10596 -------------------------
10597 -- Match_Formal_Entity --
10598 -------------------------
10600 procedure Match_Formal_Entity
10601 (Formal_Node
: Node_Id
;
10602 Formal_Ent
: Entity_Id
;
10603 Actual_Ent
: Entity_Id
)
10605 Act_Pkg
: Entity_Id
;
10608 Set_Instance_Of
(Formal_Ent
, Actual_Ent
);
10610 if Ekind
(Actual_Ent
) = E_Package
then
10612 -- Record associations for each parameter
10614 Act_Pkg
:= Actual_Ent
;
10617 A_Ent
: Entity_Id
:= First_Entity
(Act_Pkg
);
10621 Gen_Decl
: Node_Id
;
10623 Actual
: Entity_Id
;
10626 -- Retrieve the actual given in the formal package declaration
10628 Actual
:= Entity
(Name
(Original_Node
(Formal_Node
)));
10630 -- The actual in the formal package declaration may be a
10631 -- renamed generic package, in which case we want to retrieve
10632 -- the original generic in order to traverse its formal part.
10634 if Present
(Renamed_Entity
(Actual
)) then
10635 Gen_Decl
:= Unit_Declaration_Node
(Renamed_Entity
(Actual
));
10637 Gen_Decl
:= Unit_Declaration_Node
(Actual
);
10640 Formals
:= Generic_Formal_Declarations
(Gen_Decl
);
10642 if Present
(Formals
) then
10643 F_Node
:= First_Non_Pragma
(Formals
);
10648 while Present
(A_Ent
)
10649 and then Present
(F_Node
)
10650 and then A_Ent
/= First_Private_Entity
(Act_Pkg
)
10652 F_Ent
:= Get_Formal_Entity
(F_Node
);
10654 if Present
(F_Ent
) then
10656 -- This is a formal of the original package. Record
10657 -- association and recurse.
10659 Find_Matching_Actual
(F_Node
, A_Ent
);
10660 Match_Formal_Entity
(F_Node
, F_Ent
, A_Ent
);
10661 Next_Entity
(A_Ent
);
10664 Next_Non_Pragma
(F_Node
);
10668 end Match_Formal_Entity
;
10670 -----------------------
10671 -- Get_Formal_Entity --
10672 -----------------------
10674 function Get_Formal_Entity
(N
: Node_Id
) return Entity_Id
is
10675 Kind
: constant Node_Kind
:= Nkind
(Original_Node
(N
));
10678 when N_Formal_Object_Declaration
=>
10679 return Defining_Identifier
(N
);
10681 when N_Formal_Type_Declaration
=>
10682 return Defining_Identifier
(N
);
10684 when N_Formal_Subprogram_Declaration
=>
10685 return Defining_Unit_Name
(Specification
(N
));
10687 when N_Formal_Package_Declaration
=>
10688 return Defining_Identifier
(Original_Node
(N
));
10690 when N_Generic_Package_Declaration
=>
10691 return Defining_Identifier
(Original_Node
(N
));
10693 -- All other declarations are introduced by semantic analysis and
10694 -- have no match in the actual.
10699 end Get_Formal_Entity
;
10701 --------------------
10702 -- Is_Instance_Of --
10703 --------------------
10705 function Is_Instance_Of
10706 (Act_Spec
: Entity_Id
;
10707 Gen_Anc
: Entity_Id
) return Boolean
10709 Gen_Par
: constant Entity_Id
:= Generic_Parent
(Act_Spec
);
10712 if No
(Gen_Par
) then
10715 -- Simplest case: the generic parent of the actual is the formal
10717 elsif Gen_Par
= Gen_Anc
then
10720 elsif Chars
(Gen_Par
) /= Chars
(Gen_Anc
) then
10723 -- The actual may be obtained through several instantiations. Its
10724 -- scope must itself be an instance of a generic declared in the
10725 -- same scope as the formal. Any other case is detected above.
10727 elsif not Is_Generic_Instance
(Scope
(Gen_Par
)) then
10731 return Generic_Parent
(Parent
(Scope
(Gen_Par
))) = Scope
(Gen_Anc
);
10733 end Is_Instance_Of
;
10735 ---------------------------
10736 -- Process_Nested_Formal --
10737 ---------------------------
10739 procedure Process_Nested_Formal
(Formal
: Entity_Id
) is
10743 if Present
(Associated_Formal_Package
(Formal
))
10744 and then Box_Present
(Parent
(Associated_Formal_Package
(Formal
)))
10746 Ent
:= First_Entity
(Formal
);
10747 while Present
(Ent
) loop
10748 Set_Is_Hidden
(Ent
, False);
10749 Set_Is_Visible_Formal
(Ent
);
10750 Set_Is_Potentially_Use_Visible
10751 (Ent
, Is_Potentially_Use_Visible
(Formal
));
10753 if Ekind
(Ent
) = E_Package
then
10754 exit when Renamed_Entity
(Ent
) = Renamed_Entity
(Formal
);
10755 Process_Nested_Formal
(Ent
);
10761 end Process_Nested_Formal
;
10763 -- Start of processing for Instantiate_Formal_Package
10768 -- The actual must be a package instance, or else a current instance
10769 -- such as a parent generic within the body of a generic child.
10771 if not Is_Entity_Name
(Actual
)
10772 or else not Is_Package_Or_Generic_Package
(Entity
(Actual
))
10775 ("expect package instance to instantiate formal", Actual
);
10776 Abandon_Instantiation
(Actual
);
10779 Actual_Pack
:= Entity
(Actual
);
10780 Set_Is_Instantiated
(Actual_Pack
);
10782 -- The actual may be a renamed package, or an outer generic formal
10783 -- package whose instantiation is converted into a renaming.
10785 if Present
(Renamed_Entity
(Actual_Pack
)) then
10786 Actual_Pack
:= Renamed_Entity
(Actual_Pack
);
10789 -- The analyzed formal is expected to be the result of the rewriting
10790 -- of the formal package into a regular package by analysis.
10792 pragma Assert
(Nkind
(Analyzed_Formal
) = N_Package_Declaration
10793 and then Nkind
(Original_Node
(Analyzed_Formal
)) =
10794 N_Formal_Package_Declaration
);
10796 Gen_Parent
:= Generic_Parent
(Specification
(Analyzed_Formal
));
10797 Formal_Pack
:= Defining_Unit_Name
(Specification
(Analyzed_Formal
));
10799 -- The actual for a ghost generic formal package should be a ghost
10800 -- package (SPARK RM 6.9(14)).
10802 Check_Ghost_Formal_Procedure_Or_Package
10804 Actual
=> Actual_Pack
,
10805 Formal
=> Formal_Pack
);
10807 if Nkind
(Parent
(Actual_Pack
)) = N_Defining_Program_Unit_Name
then
10808 Parent_Spec
:= Package_Specification
(Actual_Pack
);
10810 Parent_Spec
:= Parent
(Actual_Pack
);
10813 if Gen_Parent
= Any_Id
then
10815 ("previous error in declaration of formal package", Actual
);
10816 Abandon_Instantiation
(Actual
);
10818 elsif Is_Instance_Of
(Parent_Spec
, Get_Instance_Of
(Gen_Parent
)) then
10821 -- If this is the current instance of an enclosing generic, that unit
10822 -- is the generic package we need.
10824 elsif In_Open_Scopes
(Actual_Pack
)
10825 and then Ekind
(Actual_Pack
) = E_Generic_Package
10831 ("actual parameter must be instance of&", Actual
, Gen_Parent
);
10832 Abandon_Instantiation
(Actual
);
10835 Set_Instance_Of
(Defining_Identifier
(Formal
), Actual_Pack
);
10836 Map_Formal_Package_Entities
(Formal_Pack
, Actual_Pack
);
10839 Make_Package_Renaming_Declaration
(Loc
,
10840 Defining_Unit_Name
=> New_Copy
(Defining_Identifier
(Formal
)),
10841 Name
=> New_Occurrence_Of
(Actual_Pack
, Loc
));
10843 Set_Associated_Formal_Package
10844 (Defining_Unit_Name
(Nod
), Defining_Identifier
(Formal
));
10845 Decls
:= New_List
(Nod
);
10847 -- If the formal F has a box, then the generic declarations are
10848 -- visible in the generic G. In an instance of G, the corresponding
10849 -- entities in the actual for F (which are the actuals for the
10850 -- instantiation of the generic that F denotes) must also be made
10851 -- visible for analysis of the current instance. On exit from the
10852 -- current instance, those entities are made private again. If the
10853 -- actual is currently in use, these entities are also use-visible.
10855 -- The loop through the actual entities also steps through the formal
10856 -- entities and enters associations from formals to actuals into the
10857 -- renaming map. This is necessary to properly handle checking of
10858 -- actual parameter associations for later formals that depend on
10859 -- actuals declared in the formal package.
10861 -- In Ada 2005, partial parameterization requires that we make
10862 -- visible the actuals corresponding to formals that were defaulted
10863 -- in the formal package. There formals are identified because they
10864 -- remain formal generics within the formal package, rather than
10865 -- being renamings of the actuals supplied.
10868 Gen_Decl
: constant Node_Id
:=
10869 Unit_Declaration_Node
(Gen_Parent
);
10870 Formals
: constant List_Id
:=
10871 Generic_Formal_Declarations
(Gen_Decl
);
10873 Actual_Ent
: Entity_Id
;
10874 Actual_Of_Formal
: Node_Id
;
10875 Formal_Node
: Node_Id
;
10876 Formal_Ent
: Entity_Id
;
10879 if Present
(Formals
) then
10880 Formal_Node
:= First_Non_Pragma
(Formals
);
10882 Formal_Node
:= Empty
;
10885 Actual_Ent
:= First_Entity
(Actual_Pack
);
10886 Actual_Of_Formal
:=
10887 First
(Visible_Declarations
(Specification
(Analyzed_Formal
)));
10888 while Present
(Actual_Ent
)
10889 and then Actual_Ent
/= First_Private_Entity
(Actual_Pack
)
10891 if Present
(Formal_Node
) then
10892 Formal_Ent
:= Get_Formal_Entity
(Formal_Node
);
10894 if Present
(Formal_Ent
) then
10895 Find_Matching_Actual
(Formal_Node
, Actual_Ent
);
10896 Match_Formal_Entity
(Formal_Node
, Formal_Ent
, Actual_Ent
);
10898 -- We iterate at the same time over the actuals of the
10899 -- local package created for the formal, to determine
10900 -- which one of the formals of the original generic were
10901 -- defaulted in the formal. The corresponding actual
10902 -- entities are visible in the enclosing instance.
10904 if Box_Present
(Formal
)
10906 (Present
(Actual_Of_Formal
)
10909 (Get_Formal_Entity
(Actual_Of_Formal
)))
10911 Set_Is_Hidden
(Actual_Ent
, False);
10912 Set_Is_Visible_Formal
(Actual_Ent
);
10913 Set_Is_Potentially_Use_Visible
10914 (Actual_Ent
, In_Use
(Actual_Pack
));
10916 if Ekind
(Actual_Ent
) = E_Package
then
10917 Process_Nested_Formal
(Actual_Ent
);
10921 if not Is_Hidden
(Actual_Ent
) then
10922 Append_Elmt
(Actual_Ent
, Hidden_Formals
);
10925 Set_Is_Hidden
(Actual_Ent
);
10926 Set_Is_Potentially_Use_Visible
(Actual_Ent
, False);
10930 Next_Non_Pragma
(Formal_Node
);
10931 Next
(Actual_Of_Formal
);
10933 -- A formal subprogram may be overloaded, so advance in
10934 -- the list of actuals to make sure we do not match two
10935 -- successive formals to the same actual. This is only
10936 -- relevant for overloadable entities, others have
10939 if Is_Overloadable
(Actual_Ent
) then
10940 Next_Entity
(Actual_Ent
);
10944 -- No further formals to match, but the generic part may
10945 -- contain inherited operation that are not hidden in the
10946 -- enclosing instance.
10948 Next_Entity
(Actual_Ent
);
10952 -- Inherited subprograms generated by formal derived types are
10953 -- also visible if the types are.
10955 Actual_Ent
:= First_Entity
(Actual_Pack
);
10956 while Present
(Actual_Ent
)
10957 and then Actual_Ent
/= First_Private_Entity
(Actual_Pack
)
10959 if Is_Overloadable
(Actual_Ent
)
10961 Nkind
(Parent
(Actual_Ent
)) = N_Subtype_Declaration
10963 not Is_Hidden
(Defining_Identifier
(Parent
(Actual_Ent
)))
10965 Set_Is_Hidden
(Actual_Ent
, False);
10966 Set_Is_Potentially_Use_Visible
10967 (Actual_Ent
, In_Use
(Actual_Pack
));
10970 Next_Entity
(Actual_Ent
);
10974 -- If the formal requires conformance checking, reanalyze it as an
10975 -- abbreviated instantiation, to verify the matching rules of 12.7.
10976 -- The actual checks are performed after the generic associations
10977 -- have been analyzed, to guarantee the same visibility for this
10978 -- instantiation and for the actuals.
10980 -- In Ada 2005, the generic associations for the formal can include
10981 -- defaulted parameters. These are ignored during check. This
10982 -- internal instantiation is removed from the tree after conformance
10983 -- checking, because it contains formal declarations for those
10984 -- defaulted parameters, and those should not reach the back-end.
10986 if Requires_Conformance_Checking
(Formal
) then
10988 I_Pack
: constant Entity_Id
:= Make_Temporary
(Loc
, 'P');
10993 Set_Is_Internal
(I_Pack
);
10994 Mutate_Ekind
(I_Pack
, E_Package
);
10996 -- Insert the package into the list of its hidden entities so
10997 -- that the list is not empty for Is_Abbreviated_Instance.
10999 Append_Elmt
(I_Pack
, Hidden_Formals
);
11001 Set_Hidden_In_Formal_Instance
(I_Pack
, Hidden_Formals
);
11003 -- If the generic is a child unit, Check_Generic_Child_Unit
11004 -- needs its original name in case it is qualified.
11006 if Is_Child_Unit
(Gen_Parent
) then
11008 New_Copy_Tree
(Name
(Original_Node
(Analyzed_Formal
)));
11009 pragma Assert
(Entity
(I_Nam
) = Gen_Parent
);
11013 New_Occurrence_Of
(Get_Instance_Of
(Gen_Parent
), Loc
);
11017 Make_Package_Instantiation
(Loc
,
11018 Defining_Unit_Name
=> I_Pack
,
11020 Generic_Associations
=> Generic_Associations
(Formal
)));
11026 end Instantiate_Formal_Package
;
11028 -----------------------------------
11029 -- Instantiate_Formal_Subprogram --
11030 -----------------------------------
11032 function Instantiate_Formal_Subprogram
11035 Analyzed_Formal
: Node_Id
) return Node_Id
11037 Analyzed_S
: constant Entity_Id
:=
11038 Defining_Unit_Name
(Specification
(Analyzed_Formal
));
11039 Formal_Sub
: constant Entity_Id
:=
11040 Defining_Unit_Name
(Specification
(Formal
));
11042 function From_Parent_Scope
(Subp
: Entity_Id
) return Boolean;
11043 -- If the generic is a child unit, the parent has been installed on the
11044 -- scope stack, but a default subprogram cannot resolve to something
11045 -- on the parent because that parent is not really part of the visible
11046 -- context (it is there to resolve explicit local entities). If the
11047 -- default has resolved in this way, we remove the entity from immediate
11048 -- visibility and analyze the node again to emit an error message or
11049 -- find another visible candidate.
11051 procedure Valid_Actual_Subprogram
(Act
: Node_Id
);
11052 -- Perform legality check and raise exception on failure
11054 -----------------------
11055 -- From_Parent_Scope --
11056 -----------------------
11058 function From_Parent_Scope
(Subp
: Entity_Id
) return Boolean is
11059 Gen_Scope
: Node_Id
;
11062 Gen_Scope
:= Scope
(Analyzed_S
);
11063 while Present
(Gen_Scope
) and then Is_Child_Unit
(Gen_Scope
) loop
11064 if Scope
(Subp
) = Scope
(Gen_Scope
) then
11068 Gen_Scope
:= Scope
(Gen_Scope
);
11072 end From_Parent_Scope
;
11074 -----------------------------
11075 -- Valid_Actual_Subprogram --
11076 -----------------------------
11078 procedure Valid_Actual_Subprogram
(Act
: Node_Id
) is
11082 if Is_Entity_Name
(Act
) then
11083 Act_E
:= Entity
(Act
);
11085 elsif Nkind
(Act
) = N_Selected_Component
11086 and then Is_Entity_Name
(Selector_Name
(Act
))
11088 Act_E
:= Entity
(Selector_Name
(Act
));
11094 -- The actual for a ghost generic formal procedure should be a ghost
11095 -- procedure (SPARK RM 6.9(14)).
11098 and then Ekind
(Act_E
) = E_Procedure
11100 Check_Ghost_Formal_Procedure_Or_Package
11103 Formal
=> Analyzed_S
);
11106 if (Present
(Act_E
) and then Is_Overloadable
(Act_E
))
11107 or else Nkind
(Act
) in N_Attribute_Reference
11108 | N_Indexed_Component
11109 | N_Character_Literal
11110 | N_Explicit_Dereference
11116 ("expect subprogram or entry name in instantiation of &",
11117 Instantiation_Node
, Formal_Sub
);
11118 Abandon_Instantiation
(Instantiation_Node
);
11119 end Valid_Actual_Subprogram
;
11123 Decl_Node
: Node_Id
;
11126 New_Spec
: Node_Id
;
11127 New_Subp
: Entity_Id
;
11129 -- Start of processing for Instantiate_Formal_Subprogram
11132 New_Spec
:= New_Copy_Tree
(Specification
(Formal
));
11134 -- The tree copy has created the proper instantiation sloc for the
11135 -- new specification. Use this location for all other constructed
11138 Loc
:= Sloc
(Defining_Unit_Name
(New_Spec
));
11140 -- Create new entity for the actual (New_Copy_Tree does not), and
11141 -- indicate that it is an actual.
11143 -- If the actual is not an entity (i.e. an attribute reference)
11144 -- and the formal includes aspect specifications for contracts,
11145 -- we create an internal name for the renaming declaration. The
11146 -- constructed wrapper contains a call to the entity in the renaming.
11147 -- This is an expansion activity, as is the wrapper creation.
11149 if Ada_Version
>= Ada_2022
11150 and then Has_Contracts
(Analyzed_Formal
)
11151 and then not Is_Entity_Name
(Actual
)
11152 and then Expander_Active
11154 New_Subp
:= Make_Temporary
(Sloc
(Actual
), 'S');
11156 New_Subp
:= Make_Defining_Identifier
(Loc
, Chars
(Formal_Sub
));
11159 Mutate_Ekind
(New_Subp
, Ekind
(Analyzed_S
));
11160 Set_Is_Generic_Actual_Subprogram
(New_Subp
);
11161 Set_Defining_Unit_Name
(New_Spec
, New_Subp
);
11163 -- Create new entities for the each of the formals in the specification
11164 -- of the renaming declaration built for the actual.
11166 if Present
(Parameter_Specifications
(New_Spec
)) then
11172 F
:= First
(Parameter_Specifications
(New_Spec
));
11173 while Present
(F
) loop
11174 F_Id
:= Defining_Identifier
(F
);
11176 Set_Defining_Identifier
(F
,
11177 Make_Defining_Identifier
(Sloc
(F_Id
), Chars
(F_Id
)));
11183 -- Find entity of actual. If the actual is an attribute reference, it
11184 -- cannot be resolved here (its formal is missing) but is handled
11185 -- instead in Attribute_Renaming. If the actual is overloaded, it is
11186 -- fully resolved subsequently, when the renaming declaration for the
11187 -- formal is analyzed. If it is an explicit dereference, resolve the
11188 -- prefix but not the actual itself, to prevent interpretation as call.
11190 if Present
(Actual
) then
11191 Loc
:= Sloc
(Actual
);
11192 Set_Sloc
(New_Spec
, Loc
);
11194 if Nkind
(Actual
) = N_Operator_Symbol
then
11195 Find_Direct_Name
(Actual
);
11197 elsif Nkind
(Actual
) = N_Explicit_Dereference
then
11198 Analyze
(Prefix
(Actual
));
11200 elsif Nkind
(Actual
) /= N_Attribute_Reference
then
11204 Valid_Actual_Subprogram
(Actual
);
11207 elsif Present
(Default_Name
(Formal
)) then
11208 if Nkind
(Default_Name
(Formal
)) not in N_Attribute_Reference
11209 | N_Selected_Component
11210 | N_Indexed_Component
11211 | N_Character_Literal
11212 and then Present
(Entity
(Default_Name
(Formal
)))
11214 Nam
:= New_Occurrence_Of
(Entity
(Default_Name
(Formal
)), Loc
);
11216 Nam
:= New_Copy
(Default_Name
(Formal
));
11217 Set_Sloc
(Nam
, Loc
);
11220 elsif Box_Present
(Formal
) then
11222 -- Actual is resolved at the point of instantiation. Create an
11223 -- identifier or operator with the same name as the formal.
11225 if Nkind
(Formal_Sub
) = N_Defining_Operator_Symbol
then
11227 Make_Operator_Symbol
(Loc
,
11228 Chars
=> Chars
(Formal_Sub
),
11229 Strval
=> No_String
);
11231 Nam
:= Make_Identifier
(Loc
, Chars
(Formal_Sub
));
11234 elsif Nkind
(Specification
(Formal
)) = N_Procedure_Specification
11235 and then Null_Present
(Specification
(Formal
))
11237 -- Generate null body for procedure, for use in the instance
11240 Make_Subprogram_Body
(Loc
,
11241 Specification
=> New_Spec
,
11242 Declarations
=> New_List
,
11243 Handled_Statement_Sequence
=>
11244 Make_Handled_Sequence_Of_Statements
(Loc
,
11245 Statements
=> New_List
(Make_Null_Statement
(Loc
))));
11247 -- RM 12.6 (16.2/2): The procedure has convention Intrinsic
11249 Set_Convention
(Defining_Unit_Name
(New_Spec
), Convention_Intrinsic
);
11251 Copy_Ghost_Aspect
(Formal
, To
=> Decl_Node
);
11253 -- Eliminate the calls to it when optimization is enabled
11255 Set_Is_Inlined
(Defining_Unit_Name
(New_Spec
));
11258 -- Handle case of a formal function with an expression default (allowed
11259 -- when extensions are enabled).
11261 elsif Nkind
(Specification
(Formal
)) = N_Function_Specification
11262 and then Present
(Expression
(Formal
))
11264 -- Generate body for function, for use in the instance
11267 Expr
: constant Node_Id
:= New_Copy
(Expression
(Formal
));
11268 Stmt
: constant Node_Id
:= Make_Simple_Return_Statement
(Loc
);
11270 Set_Sloc
(Expr
, Loc
);
11271 Set_Expression
(Stmt
, Expr
);
11274 Make_Subprogram_Body
(Loc
,
11275 Specification
=> New_Spec
,
11276 Declarations
=> New_List
,
11277 Handled_Statement_Sequence
=>
11278 Make_Handled_Sequence_Of_Statements
(Loc
,
11279 Statements
=> New_List
(Stmt
)));
11282 -- RM 12.6 (16.2/2): Like a null procedure default, the function
11283 -- has convention Intrinsic.
11285 Set_Convention
(Defining_Unit_Name
(New_Spec
), Convention_Intrinsic
);
11287 -- Inline calls to it when optimization is enabled
11289 Set_Is_Inlined
(Defining_Unit_Name
(New_Spec
));
11293 Error_Msg_Sloc
:= Sloc
(Scope
(Analyzed_S
));
11295 ("missing actual&", Instantiation_Node
, Formal_Sub
);
11297 ("\in instantiation of & declared#",
11298 Instantiation_Node
, Scope
(Analyzed_S
));
11299 Abandon_Instantiation
(Instantiation_Node
);
11303 Make_Subprogram_Renaming_Declaration
(Loc
,
11304 Specification
=> New_Spec
,
11307 -- If we do not have an actual and the formal specified <> then set to
11308 -- get proper default.
11310 if No
(Actual
) and then Box_Present
(Formal
) then
11311 Set_From_Default
(Decl_Node
);
11314 -- Gather possible interpretations for the actual before analyzing the
11315 -- instance. If overloaded, it will be resolved when analyzing the
11316 -- renaming declaration.
11318 if Box_Present
(Formal
) and then No
(Actual
) then
11321 if Is_Child_Unit
(Scope
(Analyzed_S
))
11322 and then Present
(Entity
(Nam
))
11324 if not Is_Overloaded
(Nam
) then
11325 if From_Parent_Scope
(Entity
(Nam
)) then
11326 Set_Is_Immediately_Visible
(Entity
(Nam
), False);
11327 Set_Entity
(Nam
, Empty
);
11328 Set_Etype
(Nam
, Empty
);
11331 Set_Is_Immediately_Visible
(Entity
(Nam
));
11340 Get_First_Interp
(Nam
, I
, It
);
11341 while Present
(It
.Nam
) loop
11342 if From_Parent_Scope
(It
.Nam
) then
11346 Get_Next_Interp
(I
, It
);
11353 -- The generic instantiation freezes the actual. This can only be done
11354 -- once the actual is resolved, in the analysis of the renaming
11355 -- declaration. To make the formal subprogram entity available, we set
11356 -- Corresponding_Formal_Spec to point to the formal subprogram entity.
11357 -- This is also needed in Analyze_Subprogram_Renaming for the processing
11358 -- of formal abstract subprograms.
11360 Set_Corresponding_Formal_Spec
(Decl_Node
, Analyzed_S
);
11362 -- We cannot analyze the renaming declaration, and thus find the actual,
11363 -- until all the actuals are assembled in the instance. For subsequent
11364 -- checks of other actuals, indicate the node that will hold the
11365 -- instance of this formal.
11367 Set_Instance_Of
(Analyzed_S
, Nam
);
11369 if Nkind
(Actual
) = N_Selected_Component
11370 and then Is_Task_Type
(Etype
(Prefix
(Actual
)))
11371 and then not Is_Frozen
(Etype
(Prefix
(Actual
)))
11373 -- The renaming declaration will create a body, which must appear
11374 -- outside of the instantiation, We move the renaming declaration
11375 -- out of the instance, and create an additional renaming inside,
11376 -- to prevent freezing anomalies.
11379 Anon_Id
: constant Entity_Id
:= Make_Temporary
(Loc
, 'E');
11382 Set_Defining_Unit_Name
(New_Spec
, Anon_Id
);
11383 Insert_Before
(Instantiation_Node
, Decl_Node
);
11384 Analyze
(Decl_Node
);
11386 -- Now create renaming within the instance
11389 Make_Subprogram_Renaming_Declaration
(Loc
,
11390 Specification
=> New_Copy_Tree
(New_Spec
),
11391 Name
=> New_Occurrence_Of
(Anon_Id
, Loc
));
11393 Set_Defining_Unit_Name
(Specification
(Decl_Node
),
11394 Make_Defining_Identifier
(Loc
, Chars
(Formal_Sub
)));
11399 end Instantiate_Formal_Subprogram
;
11401 ------------------------
11402 -- Instantiate_Object --
11403 ------------------------
11405 function Instantiate_Object
11408 Analyzed_Formal
: Node_Id
) return List_Id
11410 Gen_Obj
: constant Entity_Id
:= Defining_Identifier
(Formal
);
11411 A_Gen_Obj
: constant Entity_Id
:=
11412 Defining_Identifier
(Analyzed_Formal
);
11413 Acc_Def
: Node_Id
:= Empty
;
11414 Act_Assoc
: constant Node_Id
:=
11415 (if No
(Actual
) then Empty
else Parent
(Actual
));
11416 Actual_Decl
: Node_Id
:= Empty
;
11417 Decl_Node
: Node_Id
;
11420 List
: constant List_Id
:= New_List
;
11421 Loc
: constant Source_Ptr
:= Sloc
(Actual
);
11422 Orig_Ftyp
: constant Entity_Id
:= Etype
(A_Gen_Obj
);
11423 Subt_Decl
: Node_Id
:= Empty
;
11424 Subt_Mark
: Node_Id
:= Empty
;
11426 -- Start of processing for Instantiate_Object
11429 -- Formal may be an anonymous access
11431 if Present
(Subtype_Mark
(Formal
)) then
11432 Subt_Mark
:= Subtype_Mark
(Formal
);
11434 Check_Access_Definition
(Formal
);
11435 Acc_Def
:= Access_Definition
(Formal
);
11438 -- Sloc for error message on missing actual
11440 Error_Msg_Sloc
:= Sloc
(Scope
(A_Gen_Obj
));
11442 if Get_Instance_Of
(Gen_Obj
) /= Gen_Obj
then
11443 Error_Msg_N
("duplicate instantiation of generic parameter", Actual
);
11446 Set_Parent
(List
, Act_Assoc
);
11450 if Out_Present
(Formal
) then
11452 -- An IN OUT generic actual must be a name. The instantiation is a
11453 -- renaming declaration. The actual is the name being renamed. We
11454 -- use the actual directly, rather than a copy, because it is not
11455 -- used further in the list of actuals, and because a copy or a use
11456 -- of relocate_node is incorrect if the instance is nested within a
11457 -- generic. In order to simplify e.g. ASIS queries, the
11458 -- Generic_Parent field links the declaration to the generic
11461 if No
(Actual
) then
11463 ("missing actual &",
11464 Instantiation_Node
, Gen_Obj
);
11466 ("\in instantiation of & declared#",
11467 Instantiation_Node
, Scope
(A_Gen_Obj
));
11468 Abandon_Instantiation
(Instantiation_Node
);
11471 if Present
(Subt_Mark
) then
11473 Make_Object_Renaming_Declaration
(Loc
,
11474 Defining_Identifier
=> New_Copy
(Gen_Obj
),
11475 Subtype_Mark
=> New_Copy_Tree
(Subt_Mark
),
11478 else pragma Assert
(Present
(Acc_Def
));
11480 Make_Object_Renaming_Declaration
(Loc
,
11481 Defining_Identifier
=> New_Copy
(Gen_Obj
),
11482 Access_Definition
=> New_Copy_Tree
(Acc_Def
),
11486 Set_Corresponding_Generic_Association
(Decl_Node
, Act_Assoc
);
11488 -- The analysis of the actual may produce Insert_Action nodes, so
11489 -- the declaration must have a context in which to attach them.
11491 Append
(Decl_Node
, List
);
11494 -- Return if the analysis of the actual reported some error
11496 if Etype
(Actual
) = Any_Type
then
11500 -- This check is performed here because Analyze_Object_Renaming will
11501 -- not check it when Comes_From_Source is False. Note though that the
11502 -- check for the actual being the name of an object will be performed
11503 -- in Analyze_Object_Renaming.
11505 if Is_Object_Reference
(Actual
)
11506 and then Is_Dependent_Component_Of_Mutable_Object
(Actual
)
11509 ("illegal discriminant-dependent component for in out parameter",
11513 -- The actual has to be resolved in order to check that it is a
11514 -- variable (due to cases such as F (1), where F returns access to
11515 -- an array, and for overloaded prefixes).
11517 Ftyp
:= Get_Instance_Of
(Etype
(A_Gen_Obj
));
11519 -- If the type of the formal is not itself a formal, and the current
11520 -- unit is a child unit, the formal type must be declared in a
11521 -- parent, and must be retrieved by visibility.
11523 if Ftyp
= Orig_Ftyp
11524 and then Is_Generic_Unit
(Scope
(Ftyp
))
11525 and then Is_Child_Unit
(Scope
(A_Gen_Obj
))
11528 Temp
: constant Node_Id
:=
11529 New_Copy_Tree
(Subtype_Mark
(Analyzed_Formal
));
11531 Set_Entity
(Temp
, Empty
);
11533 Ftyp
:= Entity
(Temp
);
11537 if Is_Private_Type
(Ftyp
)
11538 and then not Is_Private_Type
(Etype
(Actual
))
11539 and then (Base_Type
(Full_View
(Ftyp
)) = Base_Type
(Etype
(Actual
))
11540 or else Base_Type
(Etype
(Actual
)) = Ftyp
)
11542 -- If the actual has the type of the full view of the formal, or
11543 -- else a non-private subtype of the formal, then the visibility
11544 -- of the formal type has changed. Add to the actuals a subtype
11545 -- declaration that will force the exchange of views in the body
11546 -- of the instance as well.
11549 Make_Subtype_Declaration
(Loc
,
11550 Defining_Identifier
=> Make_Temporary
(Loc
, 'P'),
11551 Subtype_Indication
=> New_Occurrence_Of
(Ftyp
, Loc
));
11553 Prepend
(Subt_Decl
, List
);
11555 Prepend_Elmt
(Full_View
(Ftyp
), Exchanged_Views
);
11556 Exchange_Declarations
(Ftyp
);
11559 Resolve
(Actual
, Ftyp
);
11561 if not Denotes_Variable
(Actual
) then
11562 Error_Msg_NE
("actual for& must be a variable", Actual
, Gen_Obj
);
11564 elsif Base_Type
(Ftyp
) /= Base_Type
(Etype
(Actual
)) then
11566 -- Ada 2005 (AI-423): For a generic formal object of mode in out,
11567 -- the type of the actual shall resolve to a specific anonymous
11570 if Ada_Version
< Ada_2005
11571 or else not Is_Anonymous_Access_Type
(Base_Type
(Ftyp
))
11572 or else not Is_Anonymous_Access_Type
(Base_Type
(Etype
(Actual
)))
11575 ("type of actual does not match type of&", Actual
, Gen_Obj
);
11579 Note_Possible_Modification
(Actual
, Sure
=> True);
11581 -- Check for instantiation with atomic/volatile/VFA object actual for
11582 -- nonatomic/nonvolatile/nonVFA formal (RM C.6 (12)).
11584 if Is_Atomic_Object
(Actual
) and then not Is_Atomic
(Orig_Ftyp
) then
11586 ("cannot instantiate nonatomic formal & of mode in out",
11588 Error_Msg_N
("\with atomic object actual (RM C.6(12))", Actual
);
11590 elsif Is_Volatile_Object_Ref
(Actual
)
11591 and then not Is_Volatile
(Orig_Ftyp
)
11594 ("cannot instantiate nonvolatile formal & of mode in out",
11596 Error_Msg_N
("\with volatile object actual (RM C.6(12))", Actual
);
11598 elsif Is_Volatile_Full_Access_Object_Ref
(Actual
)
11599 and then not Is_Volatile_Full_Access
(Orig_Ftyp
)
11602 ("cannot instantiate nonfull access formal & of mode in out",
11605 ("\with full access object actual (RM C.6(12))", Actual
);
11608 -- Check for instantiation on nonatomic subcomponent of a full access
11609 -- object in Ada 2022 (RM C.6 (12)).
11611 if Ada_Version
>= Ada_2022
11612 and then Is_Subcomponent_Of_Full_Access_Object
(Actual
)
11613 and then not Is_Atomic_Object
(Actual
)
11616 ("cannot instantiate formal & of mode in out with actual",
11619 ("\nonatomic subcomponent of full access object (RM C.6(12))",
11623 -- The actual for a ghost generic formal IN OUT parameter should be a
11624 -- ghost object (SPARK RM 6.9(14)).
11626 Check_Ghost_Formal_Variable
11628 Formal
=> A_Gen_Obj
);
11630 -- Formal in-parameter
11633 -- The instantiation of a generic formal in-parameter is constant
11634 -- declaration. The actual is the expression for that declaration.
11635 -- Its type is a full copy of the type of the formal. This may be
11636 -- an access to subprogram, for which we need to generate entities
11637 -- for the formals in the new signature.
11639 if Present
(Actual
) then
11640 if Present
(Subt_Mark
) then
11641 Def
:= New_Copy_Tree
(Subt_Mark
);
11643 pragma Assert
(Present
(Acc_Def
));
11644 Def
:= New_Copy_Tree
(Acc_Def
);
11648 Make_Object_Declaration
(Loc
,
11649 Defining_Identifier
=> New_Copy
(Gen_Obj
),
11650 Constant_Present
=> True,
11651 Null_Exclusion_Present
=> Null_Exclusion_Present
(Formal
),
11652 Object_Definition
=> Def
,
11653 Expression
=> Actual
);
11655 Copy_Ghost_Aspect
(Formal
, To
=> Decl_Node
);
11656 Set_Corresponding_Generic_Association
(Decl_Node
, Act_Assoc
);
11658 -- A generic formal object of a tagged type is defined to be
11659 -- aliased so the new constant must also be treated as aliased.
11661 if Is_Tagged_Type
(Etype
(A_Gen_Obj
)) then
11662 Set_Aliased_Present
(Decl_Node
);
11665 Append
(Decl_Node
, List
);
11667 -- The actual for a ghost generic formal IN parameter of
11668 -- access-to-variable type should be a ghost object (SPARK
11671 if Is_Access_Variable
(Etype
(A_Gen_Obj
)) then
11672 Check_Ghost_Formal_Variable
11674 Formal
=> A_Gen_Obj
);
11677 -- No need to repeat (pre-)analysis of some expression nodes
11678 -- already handled in Preanalyze_Actuals.
11680 if Nkind
(Actual
) /= N_Allocator
then
11683 -- Return if the analysis of the actual reported some error
11685 if Etype
(Actual
) = Any_Type
then
11691 Formal_Type
: constant Entity_Id
:= Etype
(A_Gen_Obj
);
11695 Typ
:= Get_Instance_Of
(Formal_Type
);
11697 -- If the actual appears in the current or an enclosing scope,
11698 -- use its type directly. This is relevant if it has an actual
11699 -- subtype that is distinct from its nominal one. This cannot
11700 -- be done in general because the type of the actual may
11701 -- depend on other actuals, and only be fully determined when
11702 -- the enclosing instance is analyzed.
11704 if Present
(Etype
(Actual
))
11705 and then Is_Constr_Subt_For_U_Nominal
(Etype
(Actual
))
11707 Freeze_Before
(Instantiation_Node
, Etype
(Actual
));
11709 Freeze_Before
(Instantiation_Node
, Typ
);
11712 -- If the actual is an aggregate, perform name resolution on
11713 -- its components (the analysis of an aggregate does not do it)
11714 -- to capture local names that may be hidden if the generic is
11717 if Nkind
(Actual
) = N_Aggregate
then
11718 Preanalyze_And_Resolve
(Actual
, Typ
);
11721 if Is_Limited_Type
(Typ
)
11722 and then not OK_For_Limited_Init
(Typ
, Actual
)
11725 ("initialization not allowed for limited types", Actual
);
11726 Explain_Limited_Type
(Typ
, Actual
);
11730 elsif Present
(Default_Expression
(Formal
)) then
11732 -- Use default to construct declaration
11734 if Present
(Subt_Mark
) then
11735 Def
:= New_Copy_Tree
(Subt_Mark
);
11737 pragma Assert
(Present
(Acc_Def
));
11738 Def
:= New_Copy_Tree
(Acc_Def
);
11742 Make_Object_Declaration
(Sloc
(Formal
),
11743 Defining_Identifier
=> New_Copy
(Gen_Obj
),
11744 Constant_Present
=> True,
11745 Null_Exclusion_Present
=> Null_Exclusion_Present
(Formal
),
11746 Object_Definition
=> Def
,
11747 Expression
=> New_Copy_Tree
11748 (Default_Expression
(Formal
)));
11750 Copy_Ghost_Aspect
(Formal
, To
=> Decl_Node
);
11751 Set_Corresponding_Generic_Association
11752 (Decl_Node
, Expression
(Decl_Node
));
11754 Append
(Decl_Node
, List
);
11755 Set_Analyzed
(Expression
(Decl_Node
), False);
11758 Error_Msg_NE
("missing actual&", Instantiation_Node
, Gen_Obj
);
11759 Error_Msg_NE
("\in instantiation of & declared#",
11760 Instantiation_Node
, Scope
(A_Gen_Obj
));
11762 if Is_Scalar_Type
(Etype
(A_Gen_Obj
)) then
11764 -- Create dummy constant declaration so that instance can be
11765 -- analyzed, to minimize cascaded visibility errors.
11767 if Present
(Subt_Mark
) then
11769 else pragma Assert
(Present
(Acc_Def
));
11774 Make_Object_Declaration
(Loc
,
11775 Defining_Identifier
=> New_Copy
(Gen_Obj
),
11776 Constant_Present
=> True,
11777 Null_Exclusion_Present
=> Null_Exclusion_Present
(Formal
),
11778 Object_Definition
=> New_Copy
(Def
),
11780 Make_Attribute_Reference
(Sloc
(Gen_Obj
),
11781 Attribute_Name
=> Name_First
,
11782 Prefix
=> New_Copy
(Def
)));
11784 Append
(Decl_Node
, List
);
11787 Abandon_Instantiation
(Instantiation_Node
);
11792 if Nkind
(Actual
) in N_Has_Entity
11793 and then Present
(Entity
(Actual
))
11795 Actual_Decl
:= Parent
(Entity
(Actual
));
11798 -- Ada 2005 (AI-423) refined by AI12-0287:
11799 -- For an object_renaming_declaration with a null_exclusion or an
11800 -- access_definition that has a null_exclusion, the subtype of the
11801 -- object_name shall exclude null. In addition, if the
11802 -- object_renaming_declaration occurs within the body of a generic unit
11803 -- G or within the body of a generic unit declared within the
11804 -- declarative region of generic unit G, then:
11805 -- * if the object_name statically denotes a generic formal object of
11806 -- mode in out of G, then the declaration of that object shall have a
11808 -- * if the object_name statically denotes a call of a generic formal
11809 -- function of G, then the declaration of the result of that function
11810 -- shall have a null_exclusion.
11812 if Ada_Version
>= Ada_2005
11813 and then Present
(Actual_Decl
)
11814 and then Nkind
(Actual_Decl
) in N_Formal_Object_Declaration
11815 | N_Object_Declaration
11816 and then Nkind
(Analyzed_Formal
) = N_Formal_Object_Declaration
11817 and then not Has_Null_Exclusion
(Actual_Decl
)
11818 and then Has_Null_Exclusion
(Analyzed_Formal
)
11819 and then Ekind
(Defining_Identifier
(Analyzed_Formal
))
11820 = E_Generic_In_Out_Parameter
11821 and then ((In_Generic_Scope
(Entity
(Actual
))
11822 and then In_Package_Body
(Scope
(Entity
(Actual
))))
11823 or else not Can_Never_Be_Null
(Etype
(Actual
)))
11825 Error_Msg_Sloc
:= Sloc
(Analyzed_Formal
);
11827 ("actual must exclude null to match generic formal#", Actual
);
11831 end Instantiate_Object
;
11833 ------------------------------
11834 -- Instantiate_Package_Body --
11835 ------------------------------
11837 -- WARNING: This routine manages Ghost and SPARK regions. Return statements
11838 -- must be replaced by gotos which jump to the end of the routine in order
11839 -- to restore the Ghost and SPARK modes.
11841 procedure Instantiate_Package_Body
11842 (Body_Info
: Pending_Body_Info
;
11843 Inlined_Body
: Boolean := False;
11844 Body_Optional
: Boolean := False)
11846 Act_Decl
: constant Node_Id
:= Body_Info
.Act_Decl
;
11847 Act_Decl_Id
: constant Entity_Id
:= Defining_Entity
(Act_Decl
);
11848 Act_Spec
: constant Node_Id
:= Specification
(Act_Decl
);
11849 Ctx_Parents
: Elist_Id
:= No_Elist
;
11850 Ctx_Top
: Int
:= 0;
11851 Inst_Node
: constant Node_Id
:= Body_Info
.Inst_Node
;
11852 Gen_Id
: constant Node_Id
:= Name
(Inst_Node
);
11853 Gen_Unit
: constant Entity_Id
:= Get_Generic_Entity
(Inst_Node
);
11854 Gen_Decl
: constant Node_Id
:= Unit_Declaration_Node
(Gen_Unit
);
11855 Loc
: constant Source_Ptr
:= Sloc
(Inst_Node
);
11857 procedure Check_Initialized_Types
;
11858 -- In a generic package body, an entity of a generic private type may
11859 -- appear uninitialized. This is suspicious, unless the actual is a
11860 -- fully initialized type.
11862 procedure Install_Parents_Of_Generic_Context
11863 (Inst_Scope
: Entity_Id
;
11864 Ctx_Parents
: out Elist_Id
);
11865 -- Inst_Scope is the scope where the instance appears within; when it
11866 -- appears within a generic child package G, this routine collects and
11867 -- installs the enclosing packages of G in the scopes stack; installed
11868 -- packages are returned in Ctx_Parents.
11870 procedure Remove_Parents_Of_Generic_Context
(Ctx_Parents
: Elist_Id
);
11871 -- Reverse effect after instantiation is complete
11873 -----------------------------
11874 -- Check_Initialized_Types --
11875 -----------------------------
11877 procedure Check_Initialized_Types
is
11879 Formal
: Entity_Id
;
11880 Actual
: Entity_Id
;
11881 Uninit_Var
: Entity_Id
;
11884 Decl
:= First
(Generic_Formal_Declarations
(Gen_Decl
));
11885 while Present
(Decl
) loop
11886 Uninit_Var
:= Empty
;
11888 if Nkind
(Decl
) = N_Private_Extension_Declaration
then
11889 Uninit_Var
:= Uninitialized_Variable
(Decl
);
11891 elsif Nkind
(Decl
) = N_Formal_Type_Declaration
11892 and then Nkind
(Formal_Type_Definition
(Decl
)) =
11893 N_Formal_Private_Type_Definition
11896 Uninitialized_Variable
(Formal_Type_Definition
(Decl
));
11899 if Present
(Uninit_Var
) then
11900 Formal
:= Defining_Identifier
(Decl
);
11901 Actual
:= First_Entity
(Act_Decl_Id
);
11903 -- For each formal there is a subtype declaration that renames
11904 -- the actual and has the same name as the formal. Locate the
11905 -- formal for warning message about uninitialized variables
11906 -- in the generic, for which the actual type should be a fully
11907 -- initialized type.
11909 while Present
(Actual
) loop
11910 exit when Ekind
(Actual
) = E_Package
11911 and then Present
(Renamed_Entity
(Actual
));
11913 if Chars
(Actual
) = Chars
(Formal
)
11914 and then not Is_Scalar_Type
(Actual
)
11915 and then not Is_Fully_Initialized_Type
(Actual
)
11916 and then Warn_On_No_Value_Assigned
11918 Error_Msg_Node_2
:= Formal
;
11920 ("generic unit has uninitialized variable& of "
11921 & "formal private type &?v?", Actual
, Uninit_Var
);
11923 ("actual type for& should be fully initialized type?v?",
11928 Next_Entity
(Actual
);
11934 end Check_Initialized_Types
;
11936 ----------------------------------------
11937 -- Install_Parents_Of_Generic_Context --
11938 ----------------------------------------
11940 procedure Install_Parents_Of_Generic_Context
11941 (Inst_Scope
: Entity_Id
;
11942 Ctx_Parents
: out Elist_Id
)
11948 Ctx_Parents
:= New_Elmt_List
;
11950 -- Collect context parents (ie. parents where the instantiation
11951 -- appears within).
11954 while S
/= Standard_Standard
loop
11955 Prepend_Elmt
(S
, Ctx_Parents
);
11959 -- Install enclosing parents
11961 Elmt
:= First_Elmt
(Ctx_Parents
);
11962 while Present
(Elmt
) loop
11963 Push_Scope
(Node
(Elmt
));
11964 Set_Is_Immediately_Visible
(Node
(Elmt
));
11967 end Install_Parents_Of_Generic_Context
;
11969 ---------------------------------------
11970 -- Remove_Parents_Of_Generic_Context --
11971 ---------------------------------------
11973 procedure Remove_Parents_Of_Generic_Context
(Ctx_Parents
: Elist_Id
) is
11977 -- Traverse Ctx_Parents in LIFO order to check the removed scopes
11979 Elmt
:= Last_Elmt
(Ctx_Parents
);
11980 while Present
(Elmt
) loop
11981 pragma Assert
(Current_Scope
= Node
(Elmt
));
11982 Set_Is_Immediately_Visible
(Current_Scope
, False);
11985 Remove_Last_Elmt
(Ctx_Parents
);
11986 Elmt
:= Last_Elmt
(Ctx_Parents
);
11988 end Remove_Parents_Of_Generic_Context
;
11992 -- The following constants capture the context prior to instantiating
11993 -- the package body.
11995 Saved_CS
: constant Config_Switches_Type
:= Save_Config_Switches
;
11996 Saved_GM
: constant Ghost_Mode_Type
:= Ghost_Mode
;
11997 Saved_IGR
: constant Node_Id
:= Ignored_Ghost_Region
;
11998 Saved_ISMP
: constant Boolean :=
11999 Ignore_SPARK_Mode_Pragmas_In_Instance
;
12000 Saved_LSST
: constant Suppress_Stack_Entry_Ptr
:=
12001 Local_Suppress_Stack_Top
;
12002 Saved_SC
: constant Boolean := Style_Check
;
12003 Saved_SM
: constant SPARK_Mode_Type
:= SPARK_Mode
;
12004 Saved_SMP
: constant Node_Id
:= SPARK_Mode_Pragma
;
12005 Saved_SS
: constant Suppress_Record
:= Scope_Suppress
;
12006 Saved_Warn
: constant Warnings_State
:= Save_Warnings
;
12008 Act_Body
: Node_Id
;
12009 Act_Body_Id
: Entity_Id
;
12010 Act_Body_Name
: Node_Id
;
12011 Gen_Body
: Node_Id
;
12012 Gen_Body_Id
: Node_Id
;
12013 Par_Ent
: Entity_Id
:= Empty
;
12014 Par_Installed
: Boolean := False;
12015 Par_Vis
: Boolean := False;
12017 Scope_Check_Id
: Entity_Id
;
12018 Scope_Check_Last
: Nat
;
12019 -- Value of Current_Scope before calls to Install_Parents; used to check
12020 -- that scopes are correctly removed after instantiation.
12022 Vis_Prims_List
: Elist_Id
:= No_Elist
;
12023 -- List of primitives made temporarily visible in the instantiation
12024 -- to match the visibility of the formal type.
12026 -- Start of processing for Instantiate_Package_Body
12029 Gen_Body_Id
:= Corresponding_Body
(Gen_Decl
);
12031 -- The instance body may already have been processed, as the parent of
12032 -- another instance that is inlined (Load_Parent_Of_Generic).
12034 if Present
(Corresponding_Body
(Instance_Spec
(Inst_Node
))) then
12038 Expander_Mode_Save_And_Set
(Body_Info
.Expander_Status
);
12040 -- Re-establish the state of information on which checks are suppressed.
12041 -- This information was set in Body_Info at the point of instantiation,
12042 -- and now we restore it so that the instance is compiled using the
12043 -- check status at the instantiation (RM 11.5(7.2/2), AI95-00224-01).
12045 Local_Suppress_Stack_Top
:= Body_Info
.Local_Suppress_Stack_Top
;
12046 Scope_Suppress
:= Body_Info
.Scope_Suppress
;
12048 Restore_Config_Switches
(Body_Info
.Config_Switches
);
12049 Restore_Warnings
(Body_Info
.Warnings
);
12051 if No
(Gen_Body_Id
) then
12053 -- Do not look for parent of generic body if none is required.
12054 -- This may happen when the routine is called as part of the
12055 -- Pending_Instantiations processing, when nested instances
12056 -- may precede the one generated from the main unit.
12058 if not Unit_Requires_Body
(Defining_Entity
(Gen_Decl
))
12059 and then Body_Optional
12063 Load_Parent_Of_Generic
12064 (Inst_Node
, Specification
(Gen_Decl
), Body_Optional
);
12066 -- Surprisingly enough, loading the body of the parent can cause
12067 -- the body to be instantiated and the double instantiation needs
12068 -- to be prevented in order to avoid giving bogus semantic errors.
12070 -- This case can occur because of the Collect_Previous_Instances
12071 -- machinery of Load_Parent_Of_Generic, which will instantiate
12072 -- bodies that are deemed to be ahead of the body of the parent
12073 -- in the compilation unit. But the relative position of these
12074 -- bodies is computed using the mere comparison of their Sloc.
12076 -- Now suppose that you have two generic packages G and H, with
12077 -- G containing a mere instantiation of H:
12083 -- package Nested_G is
12094 -- package My_H is new H;
12098 -- and a third package Q instantiating G and Nested_G:
12104 -- package My_G is new G;
12106 -- package My_Nested_G is new My_G.My_H.Nested_G;
12110 -- The body to be instantiated is that of My_Nested_G and its
12111 -- parent is the instance My_G.My_H. This latter instantiation
12112 -- is done when My_G is analyzed, i.e. after the declarations
12113 -- of My_G and My_Nested_G have been parsed; as a result, the
12114 -- Sloc of My_G.My_H is greater than the Sloc of My_Nested_G.
12116 -- Therefore loading the body of My_G.My_H will cause the body
12117 -- of My_Nested_G to be instantiated because it is deemed to be
12118 -- ahead of My_G.My_H. This means that Load_Parent_Of_Generic
12119 -- will again be invoked on My_G.My_H, but this time with the
12120 -- Collect_Previous_Instances machinery disabled, so there is
12121 -- no endless mutual recursion and things are done in order.
12123 if Present
(Corresponding_Body
(Instance_Spec
(Inst_Node
))) then
12127 Gen_Body_Id
:= Corresponding_Body
(Gen_Decl
);
12131 -- Establish global variable for sloc adjustment and for error recovery
12132 -- In the case of an instance body for an instantiation with actuals
12133 -- from a limited view, the instance body is placed at the beginning
12134 -- of the enclosing package body: use the body entity as the source
12135 -- location for nodes of the instance body.
12137 if not Is_Empty_Elmt_List
(Incomplete_Actuals
(Act_Decl_Id
)) then
12139 Scop
: constant Entity_Id
:= Scope
(Act_Decl_Id
);
12140 Body_Id
: constant Node_Id
:=
12141 Corresponding_Body
(Unit_Declaration_Node
(Scop
));
12144 Instantiation_Node
:= Body_Id
;
12147 Instantiation_Node
:= Inst_Node
;
12150 -- The package being instantiated may be subject to pragma Ghost. Set
12151 -- the mode now to ensure that any nodes generated during instantiation
12152 -- are properly marked as Ghost.
12154 Set_Ghost_Mode
(Act_Decl_Id
);
12156 if Present
(Gen_Body_Id
) then
12157 Save_Env
(Gen_Unit
, Act_Decl_Id
);
12158 Style_Check
:= False;
12160 -- If the context of the instance is subject to SPARK_Mode "off", the
12161 -- annotation is missing, or the body is instantiated at a later pass
12162 -- and its spec ignored SPARK_Mode pragma, set the global flag which
12163 -- signals Analyze_Pragma to ignore all SPARK_Mode pragmas within the
12166 if SPARK_Mode
/= On
12167 or else Ignore_SPARK_Mode_Pragmas
(Act_Decl_Id
)
12169 Ignore_SPARK_Mode_Pragmas_In_Instance
:= True;
12172 Current_Sem_Unit
:= Body_Info
.Current_Sem_Unit
;
12173 Gen_Body
:= Unit_Declaration_Node
(Gen_Body_Id
);
12175 Create_Instantiation_Source
12176 (Inst_Node
, Gen_Body_Id
, S_Adjustment
);
12180 (Original_Node
(Gen_Body
), Empty
, Instantiating
=> True);
12182 -- Create proper (possibly qualified) defining name for the body, to
12183 -- correspond to the one in the spec.
12186 Make_Defining_Identifier
(Sloc
(Act_Decl_Id
), Chars
(Act_Decl_Id
));
12187 Preserve_Comes_From_Source
(Act_Body_Id
, Act_Decl_Id
);
12189 -- Some attributes of spec entity are not inherited by body entity
12191 Set_Handler_Records
(Act_Body_Id
, No_List
);
12193 if Nkind
(Defining_Unit_Name
(Act_Spec
)) =
12194 N_Defining_Program_Unit_Name
12197 Make_Defining_Program_Unit_Name
(Loc
,
12199 New_Copy_Tree
(Name
(Defining_Unit_Name
(Act_Spec
))),
12200 Defining_Identifier
=> Act_Body_Id
);
12202 Act_Body_Name
:= Act_Body_Id
;
12205 Set_Defining_Unit_Name
(Act_Body
, Act_Body_Name
);
12207 Set_Corresponding_Spec
(Act_Body
, Act_Decl_Id
);
12208 Check_Generic_Actuals
(Act_Decl_Id
, False);
12209 Check_Initialized_Types
;
12211 -- Install primitives hidden at the point of the instantiation but
12212 -- visible when processing the generic formals
12218 E
:= First_Entity
(Act_Decl_Id
);
12219 while Present
(E
) loop
12221 and then not Is_Itype
(E
)
12222 and then Is_Generic_Actual_Type
(E
)
12223 and then Is_Tagged_Type
(E
)
12225 Install_Hidden_Primitives
12226 (Prims_List
=> Vis_Prims_List
,
12227 Gen_T
=> Generic_Parent_Type
(Parent
(E
)),
12235 Scope_Check_Id
:= Current_Scope
;
12236 Scope_Check_Last
:= Scope_Stack
.Last
;
12238 -- If the instantiation appears within a generic child some actual
12239 -- parameter may be the current instance of the enclosing generic
12243 Inst_Scope
: constant Entity_Id
:= Scope
(Act_Decl_Id
);
12246 if Is_Child_Unit
(Inst_Scope
)
12247 and then Ekind
(Inst_Scope
) = E_Generic_Package
12248 and then Present
(Generic_Associations
(Inst_Node
))
12250 Install_Parents_Of_Generic_Context
(Inst_Scope
, Ctx_Parents
);
12252 -- Hide them from visibility; required to avoid conflicts
12253 -- installing the parent instance.
12255 if Present
(Ctx_Parents
) then
12256 Push_Scope
(Standard_Standard
);
12257 Ctx_Top
:= Scope_Stack
.Last
;
12258 Scope_Stack
.Table
(Ctx_Top
).Is_Active_Stack_Base
:= True;
12263 -- If it is a child unit, make the parent instance (which is an
12264 -- instance of the parent of the generic) visible.
12266 -- 1) The child unit's parent is an explicit parent instance (the
12267 -- prefix of the name of the generic unit):
12269 -- package Child_Package is new Parent_Instance.Child_Unit;
12271 -- 2) The child unit's parent is an implicit parent instance (e.g.
12272 -- when instantiating a sibling package):
12275 -- package Parent.Second_Child is
12279 -- package Parent.First_Child is
12280 -- package Sibling_Package is new Second_Child;
12282 -- 3) The child unit's parent is not an instance, so the scope is
12283 -- simply the one of the unit.
12285 if Ekind
(Scope
(Gen_Unit
)) = E_Generic_Package
12286 and then Nkind
(Gen_Id
) = N_Expanded_Name
12288 Par_Ent
:= Entity
(Prefix
(Gen_Id
));
12290 elsif Ekind
(Scope
(Gen_Unit
)) = E_Generic_Package
12291 and then Ekind
(Scope
(Act_Decl_Id
)) = E_Package
12292 and then Is_Generic_Instance
(Scope
(Act_Decl_Id
))
12294 (Name
(Get_Unit_Instantiation_Node
12295 (Scope
(Act_Decl_Id
)))) = N_Expanded_Name
12298 (Prefix
(Name
(Get_Unit_Instantiation_Node
12299 (Scope
(Act_Decl_Id
)))));
12301 elsif Is_Child_Unit
(Gen_Unit
) then
12302 Par_Ent
:= Scope
(Gen_Unit
);
12305 if Present
(Par_Ent
) then
12306 Par_Vis
:= Is_Immediately_Visible
(Par_Ent
);
12307 Install_Parent
(Par_Ent
, In_Body
=> True);
12308 Par_Installed
:= True;
12311 -- If the instantiation is a library unit, and this is the main unit,
12312 -- then build the resulting compilation unit nodes for the instance.
12313 -- If this is a compilation unit but it is not the main unit, then it
12314 -- is the body of a unit in the context, that is being compiled
12315 -- because it is encloses some inlined unit or another generic unit
12316 -- being instantiated. In that case, this body is not part of the
12317 -- current compilation, and is not attached to the tree, but its
12318 -- parent must be set for analysis.
12320 if Nkind
(Parent
(Inst_Node
)) = N_Compilation_Unit
then
12322 -- Replace instance node with body of instance, and create new
12323 -- node for corresponding instance declaration.
12325 Build_Instance_Compilation_Unit_Nodes
12326 (Inst_Node
, Act_Body
, Act_Decl
);
12328 -- If the instantiation appears within a generic child package
12329 -- enable visibility of current instance of enclosing generic
12332 if Present
(Ctx_Parents
) then
12333 Scope_Stack
.Table
(Ctx_Top
).Is_Active_Stack_Base
:= False;
12334 Analyze
(Inst_Node
);
12335 Scope_Stack
.Table
(Ctx_Top
).Is_Active_Stack_Base
:= True;
12337 Analyze
(Inst_Node
);
12340 if Parent
(Inst_Node
) = Cunit
(Main_Unit
) then
12342 -- If the instance is a child unit itself, then set the scope
12343 -- of the expanded body to be the parent of the instantiation
12344 -- (ensuring that the fully qualified name will be generated
12345 -- for the elaboration subprogram).
12347 if Nkind
(Defining_Unit_Name
(Act_Spec
)) =
12348 N_Defining_Program_Unit_Name
12350 Set_Scope
(Defining_Entity
(Inst_Node
), Scope
(Act_Decl_Id
));
12354 -- Case where instantiation is not a library unit
12357 -- Handle the case of an instance with incomplete actual types.
12358 -- The instance body cannot be placed just after the declaration
12359 -- because full views have not been seen yet. Any use of the non-
12360 -- limited views in the instance body requires the presence of a
12361 -- regular with_clause in the enclosing unit. Therefore we place
12362 -- the instance body at the beginning of the enclosing body, and
12363 -- the freeze node for the instance is then placed after the body.
12365 if not Is_Empty_Elmt_List
(Incomplete_Actuals
(Act_Decl_Id
))
12366 and then Ekind
(Scope
(Act_Decl_Id
)) = E_Package
12369 Scop
: constant Entity_Id
:= Scope
(Act_Decl_Id
);
12370 Body_Id
: constant Node_Id
:=
12371 Corresponding_Body
(Unit_Declaration_Node
(Scop
));
12376 pragma Assert
(Present
(Body_Id
));
12378 Prepend
(Act_Body
, Declarations
(Parent
(Body_Id
)));
12380 if Expander_Active
then
12381 Ensure_Freeze_Node
(Act_Decl_Id
);
12382 F_Node
:= Freeze_Node
(Act_Decl_Id
);
12383 Set_Is_Frozen
(Act_Decl_Id
, False);
12384 if Is_List_Member
(F_Node
) then
12388 Insert_After
(Act_Body
, F_Node
);
12393 Insert_Before
(Inst_Node
, Act_Body
);
12394 Mark_Rewrite_Insertion
(Act_Body
);
12396 -- Insert the freeze node for the instance if need be
12398 if Expander_Active
then
12399 Freeze_Package_Instance
12400 (Inst_Node
, Gen_Body
, Gen_Decl
, Act_Decl_Id
);
12401 Set_Is_Frozen
(Act_Decl_Id
);
12405 -- If the instantiation appears within a generic child package
12406 -- enable visibility of current instance of enclosing generic
12409 if Present
(Ctx_Parents
) then
12410 Scope_Stack
.Table
(Ctx_Top
).Is_Active_Stack_Base
:= False;
12411 Analyze
(Act_Body
);
12412 Scope_Stack
.Table
(Ctx_Top
).Is_Active_Stack_Base
:= True;
12414 Analyze
(Act_Body
);
12418 Inherit_Context
(Gen_Body
, Inst_Node
);
12420 if Par_Installed
then
12421 Remove_Parent
(In_Body
=> True);
12423 -- Restore the previous visibility of the parent
12425 Set_Is_Immediately_Visible
(Par_Ent
, Par_Vis
);
12428 -- Remove the parent instances if they have been placed on the scope
12429 -- stack to compile the body.
12431 if Present
(Ctx_Parents
) then
12432 pragma Assert
(Scope_Stack
.Last
= Ctx_Top
12433 and then Current_Scope
= Standard_Standard
);
12436 Remove_Parents_Of_Generic_Context
(Ctx_Parents
);
12439 pragma Assert
(Current_Scope
= Scope_Check_Id
);
12440 pragma Assert
(Scope_Stack
.Last
= Scope_Check_Last
);
12442 Restore_Hidden_Primitives
(Vis_Prims_List
);
12444 -- Restore the private views that were made visible when the body of
12445 -- the instantiation was created. Note that, in the case where one of
12446 -- these private views is declared in the parent, there is a nesting
12447 -- issue with the calls to Install_Parent and Remove_Parent made in
12448 -- between above with In_Body set to True, because these calls also
12449 -- want to swap and restore this private view respectively. In this
12450 -- case, the call to Install_Parent does nothing, but the call to
12451 -- Remove_Parent does restore the private view, thus undercutting the
12452 -- call to Restore_Private_Views. That's OK under the condition that
12453 -- the two mechanisms swap exactly the same entities, in particular
12454 -- the private entities dependent on the primary private entities.
12456 Restore_Private_Views
(Act_Decl_Id
);
12458 -- Remove the current unit from visibility if this is an instance
12459 -- that is not elaborated on the fly for inlining purposes.
12461 if not Inlined_Body
then
12462 Set_Is_Immediately_Visible
(Act_Decl_Id
, False);
12467 -- If we have no body, and the unit requires a body, then complain. This
12468 -- complaint is suppressed if we have detected other errors (since a
12469 -- common reason for missing the body is that it had errors).
12470 -- In CodePeer mode, a warning has been emitted already, no need for
12471 -- further messages.
12473 elsif Unit_Requires_Body
(Gen_Unit
)
12474 and then not Body_Optional
12476 if CodePeer_Mode
then
12479 elsif Serious_Errors_Detected
= 0 then
12481 ("cannot find body of generic package &", Inst_Node
, Gen_Unit
);
12483 -- Don't attempt to perform any cleanup actions if some other error
12484 -- was already detected, since this can cause blowups.
12490 -- Case of package that does not need a body
12493 -- If the instantiation of the declaration is a library unit, rewrite
12494 -- the original package instantiation as a package declaration in the
12495 -- compilation unit node.
12497 if Nkind
(Parent
(Inst_Node
)) = N_Compilation_Unit
then
12498 Set_Parent_Spec
(Act_Decl
, Parent_Spec
(Inst_Node
));
12499 Rewrite
(Inst_Node
, Act_Decl
);
12501 -- Generate elaboration entity, in case spec has elaboration code.
12502 -- This cannot be done when the instance is analyzed, because it
12503 -- is not known yet whether the body exists.
12505 Set_Elaboration_Entity_Required
(Act_Decl_Id
, False);
12506 Build_Elaboration_Entity
(Parent
(Inst_Node
), Act_Decl_Id
);
12508 -- If the instantiation is not a library unit, then append the
12509 -- declaration to the list of implicitly generated entities, unless
12510 -- it is already a list member which means that it was already
12513 elsif not Is_List_Member
(Act_Decl
) then
12514 Mark_Rewrite_Insertion
(Act_Decl
);
12515 Insert_Before
(Inst_Node
, Act_Decl
);
12521 -- Restore the context that was in effect prior to instantiating the
12524 Ignore_SPARK_Mode_Pragmas_In_Instance
:= Saved_ISMP
;
12525 Local_Suppress_Stack_Top
:= Saved_LSST
;
12526 Scope_Suppress
:= Saved_SS
;
12527 Style_Check
:= Saved_SC
;
12529 Expander_Mode_Restore
;
12530 Restore_Config_Switches
(Saved_CS
);
12531 Restore_Ghost_Region
(Saved_GM
, Saved_IGR
);
12532 Restore_SPARK_Mode
(Saved_SM
, Saved_SMP
);
12533 Restore_Warnings
(Saved_Warn
);
12534 end Instantiate_Package_Body
;
12536 ---------------------------------
12537 -- Instantiate_Subprogram_Body --
12538 ---------------------------------
12540 -- WARNING: This routine manages Ghost and SPARK regions. Return statements
12541 -- must be replaced by gotos which jump to the end of the routine in order
12542 -- to restore the Ghost and SPARK modes.
12544 procedure Instantiate_Subprogram_Body
12545 (Body_Info
: Pending_Body_Info
;
12546 Body_Optional
: Boolean := False)
12548 Act_Decl
: constant Node_Id
:= Body_Info
.Act_Decl
;
12549 Act_Decl_Id
: constant Entity_Id
:= Defining_Entity
(Act_Decl
);
12550 Inst_Node
: constant Node_Id
:= Body_Info
.Inst_Node
;
12551 Gen_Id
: constant Node_Id
:= Name
(Inst_Node
);
12552 Gen_Unit
: constant Entity_Id
:= Get_Generic_Entity
(Inst_Node
);
12553 Gen_Decl
: constant Node_Id
:= Unit_Declaration_Node
(Gen_Unit
);
12554 Loc
: constant Source_Ptr
:= Sloc
(Inst_Node
);
12555 Pack_Id
: constant Entity_Id
:=
12556 Defining_Unit_Name
(Parent
(Act_Decl
));
12558 -- The following constants capture the context prior to instantiating
12559 -- the subprogram body.
12561 Saved_CS
: constant Config_Switches_Type
:= Save_Config_Switches
;
12562 Saved_GM
: constant Ghost_Mode_Type
:= Ghost_Mode
;
12563 Saved_IGR
: constant Node_Id
:= Ignored_Ghost_Region
;
12564 Saved_ISMP
: constant Boolean :=
12565 Ignore_SPARK_Mode_Pragmas_In_Instance
;
12566 Saved_LSST
: constant Suppress_Stack_Entry_Ptr
:=
12567 Local_Suppress_Stack_Top
;
12568 Saved_SC
: constant Boolean := Style_Check
;
12569 Saved_SM
: constant SPARK_Mode_Type
:= SPARK_Mode
;
12570 Saved_SMP
: constant Node_Id
:= SPARK_Mode_Pragma
;
12571 Saved_SS
: constant Suppress_Record
:= Scope_Suppress
;
12572 Saved_Warn
: constant Warnings_State
:= Save_Warnings
;
12574 Act_Body
: Node_Id
;
12575 Act_Body_Id
: Entity_Id
;
12576 Gen_Body
: Node_Id
;
12577 Gen_Body_Id
: Node_Id
;
12578 Pack_Body
: Node_Id
;
12579 Par_Ent
: Entity_Id
:= Empty
;
12580 Par_Installed
: Boolean := False;
12581 Par_Vis
: Boolean := False;
12582 Ret_Expr
: Node_Id
;
12585 Gen_Body_Id
:= Corresponding_Body
(Gen_Decl
);
12587 -- Subprogram body may have been created already because of an inline
12588 -- pragma, or because of multiple elaborations of the enclosing package
12589 -- when several instances of the subprogram appear in the main unit.
12591 if Present
(Corresponding_Body
(Act_Decl
)) then
12595 Expander_Mode_Save_And_Set
(Body_Info
.Expander_Status
);
12597 -- Re-establish the state of information on which checks are suppressed.
12598 -- This information was set in Body_Info at the point of instantiation,
12599 -- and now we restore it so that the instance is compiled using the
12600 -- check status at the instantiation (RM 11.5(7.2/2), AI95-00224-01).
12602 Local_Suppress_Stack_Top
:= Body_Info
.Local_Suppress_Stack_Top
;
12603 Scope_Suppress
:= Body_Info
.Scope_Suppress
;
12605 Restore_Config_Switches
(Body_Info
.Config_Switches
);
12606 Restore_Warnings
(Body_Info
.Warnings
);
12608 if No
(Gen_Body_Id
) then
12610 -- For imported generic subprogram, no body to compile, complete
12611 -- the spec entity appropriately.
12613 if Is_Imported
(Gen_Unit
) then
12614 Set_Is_Imported
(Act_Decl_Id
);
12615 Set_First_Rep_Item
(Act_Decl_Id
, First_Rep_Item
(Gen_Unit
));
12616 Set_Interface_Name
(Act_Decl_Id
, Interface_Name
(Gen_Unit
));
12617 Set_Convention
(Act_Decl_Id
, Convention
(Gen_Unit
));
12618 Set_Has_Completion
(Act_Decl_Id
);
12621 -- For other cases, compile the body
12624 Load_Parent_Of_Generic
12625 (Inst_Node
, Specification
(Gen_Decl
), Body_Optional
);
12626 Gen_Body_Id
:= Corresponding_Body
(Gen_Decl
);
12630 Instantiation_Node
:= Inst_Node
;
12632 -- The subprogram being instantiated may be subject to pragma Ghost. Set
12633 -- the mode now to ensure that any nodes generated during instantiation
12634 -- are properly marked as Ghost.
12636 Set_Ghost_Mode
(Act_Decl_Id
);
12638 if Present
(Gen_Body_Id
) then
12639 Gen_Body
:= Unit_Declaration_Node
(Gen_Body_Id
);
12641 if Nkind
(Gen_Body
) = N_Subprogram_Body_Stub
then
12643 -- Either body is not present, or context is non-expanding, as
12644 -- when compiling a subunit. Mark the instance as completed, and
12645 -- diagnose a missing body when needed.
12648 and then Operating_Mode
= Generate_Code
12650 Error_Msg_N
("missing proper body for instantiation", Gen_Body
);
12653 Set_Has_Completion
(Act_Decl_Id
);
12657 Save_Env
(Gen_Unit
, Act_Decl_Id
);
12658 Style_Check
:= False;
12660 -- If the context of the instance is subject to SPARK_Mode "off", the
12661 -- annotation is missing, or the body is instantiated at a later pass
12662 -- and its spec ignored SPARK_Mode pragma, set the global flag which
12663 -- signals Analyze_Pragma to ignore all SPARK_Mode pragmas within the
12666 if SPARK_Mode
/= On
12667 or else Ignore_SPARK_Mode_Pragmas
(Act_Decl_Id
)
12669 Ignore_SPARK_Mode_Pragmas_In_Instance
:= True;
12672 -- If the context of an instance is not subject to SPARK_Mode "off",
12673 -- and the generic body is subject to an explicit SPARK_Mode pragma,
12674 -- the latter should be the one applicable to the instance.
12676 if not Ignore_SPARK_Mode_Pragmas_In_Instance
12677 and then SPARK_Mode
/= Off
12678 and then Present
(SPARK_Pragma
(Gen_Body_Id
))
12680 Set_SPARK_Mode
(Gen_Body_Id
);
12683 Current_Sem_Unit
:= Body_Info
.Current_Sem_Unit
;
12684 Create_Instantiation_Source
12691 (Original_Node
(Gen_Body
), Empty
, Instantiating
=> True);
12693 -- Create proper defining name for the body, to correspond to the one
12697 Make_Defining_Identifier
(Sloc
(Act_Decl_Id
), Chars
(Act_Decl_Id
));
12699 Preserve_Comes_From_Source
(Act_Body_Id
, Act_Decl_Id
);
12700 Set_Defining_Unit_Name
(Specification
(Act_Body
), Act_Body_Id
);
12702 Set_Corresponding_Spec
(Act_Body
, Act_Decl_Id
);
12703 Set_Has_Completion
(Act_Decl_Id
);
12704 Check_Generic_Actuals
(Pack_Id
, False);
12706 -- Generate a reference to link the visible subprogram instance to
12707 -- the generic body, which for navigation purposes is the only
12708 -- available source for the instance.
12711 (Related_Instance
(Pack_Id
),
12712 Gen_Body_Id
, 'b', Set_Ref
=> False, Force
=> True);
12714 -- If it is a child unit, make the parent instance (which is an
12715 -- instance of the parent of the generic) visible. The parent
12716 -- instance is the prefix of the name of the generic unit.
12718 if Ekind
(Scope
(Gen_Unit
)) = E_Generic_Package
12719 and then Nkind
(Gen_Id
) = N_Expanded_Name
12721 Par_Ent
:= Entity
(Prefix
(Gen_Id
));
12722 elsif Is_Child_Unit
(Gen_Unit
) then
12723 Par_Ent
:= Scope
(Gen_Unit
);
12726 if Present
(Par_Ent
) then
12727 Par_Vis
:= Is_Immediately_Visible
(Par_Ent
);
12728 Install_Parent
(Par_Ent
, In_Body
=> True);
12729 Par_Installed
:= True;
12732 -- Subprogram body is placed in the body of wrapper package,
12733 -- whose spec contains the subprogram declaration as well as
12734 -- the renaming declarations for the generic parameters.
12737 Make_Package_Body
(Loc
,
12738 Defining_Unit_Name
=> New_Copy
(Pack_Id
),
12739 Declarations
=> New_List
(Act_Body
));
12741 Set_Corresponding_Spec
(Pack_Body
, Pack_Id
);
12743 -- If the instantiation is a library unit, then build resulting
12744 -- compilation unit nodes for the instance. The declaration of
12745 -- the enclosing package is the grandparent of the subprogram
12746 -- declaration. First replace the instantiation node as the unit
12747 -- of the corresponding compilation.
12749 if Nkind
(Parent
(Inst_Node
)) = N_Compilation_Unit
then
12750 if Parent
(Inst_Node
) = Cunit
(Main_Unit
) then
12751 Set_Unit
(Parent
(Inst_Node
), Inst_Node
);
12752 Build_Instance_Compilation_Unit_Nodes
12753 (Inst_Node
, Pack_Body
, Parent
(Parent
(Act_Decl
)));
12754 Analyze
(Inst_Node
);
12756 Set_Parent
(Pack_Body
, Parent
(Inst_Node
));
12757 Analyze
(Pack_Body
);
12761 Insert_Before
(Inst_Node
, Pack_Body
);
12762 Mark_Rewrite_Insertion
(Pack_Body
);
12764 -- Insert the freeze node for the instance if need be
12766 if Expander_Active
then
12767 Freeze_Subprogram_Instance
(Inst_Node
, Gen_Body
, Pack_Id
);
12770 Analyze
(Pack_Body
);
12773 Inherit_Context
(Gen_Body
, Inst_Node
);
12775 Restore_Private_Views
(Pack_Id
, False);
12777 if Par_Installed
then
12778 Remove_Parent
(In_Body
=> True);
12780 -- Restore the previous visibility of the parent
12782 Set_Is_Immediately_Visible
(Par_Ent
, Par_Vis
);
12787 -- Body not found. Error was emitted already. If there were no previous
12788 -- errors, this may be an instance whose scope is a premature instance.
12789 -- In that case we must insure that the (legal) program does raise
12790 -- program error if executed. We generate a subprogram body for this
12793 elsif Serious_Errors_Detected
= 0
12794 and then Nkind
(Parent
(Inst_Node
)) /= N_Compilation_Unit
12796 if Body_Optional
then
12799 elsif Ekind
(Act_Decl_Id
) = E_Procedure
then
12801 Make_Subprogram_Body
(Loc
,
12803 Make_Procedure_Specification
(Loc
,
12804 Defining_Unit_Name
=>
12805 Make_Defining_Identifier
(Loc
, Chars
(Act_Decl_Id
)),
12806 Parameter_Specifications
=>
12808 (Parameter_Specifications
(Parent
(Act_Decl_Id
)))),
12810 Declarations
=> Empty_List
,
12811 Handled_Statement_Sequence
=>
12812 Make_Handled_Sequence_Of_Statements
(Loc
,
12813 Statements
=> New_List
(
12814 Make_Raise_Program_Error
(Loc
,
12815 Reason
=> PE_Access_Before_Elaboration
))));
12819 Make_Raise_Program_Error
(Loc
,
12820 Reason
=> PE_Access_Before_Elaboration
);
12822 Set_Etype
(Ret_Expr
, (Etype
(Act_Decl_Id
)));
12823 Set_Analyzed
(Ret_Expr
);
12826 Make_Subprogram_Body
(Loc
,
12828 Make_Function_Specification
(Loc
,
12829 Defining_Unit_Name
=>
12830 Make_Defining_Identifier
(Loc
, Chars
(Act_Decl_Id
)),
12831 Parameter_Specifications
=>
12833 (Parameter_Specifications
(Parent
(Act_Decl_Id
))),
12834 Result_Definition
=>
12835 New_Occurrence_Of
(Etype
(Act_Decl_Id
), Loc
)),
12837 Declarations
=> Empty_List
,
12838 Handled_Statement_Sequence
=>
12839 Make_Handled_Sequence_Of_Statements
(Loc
,
12840 Statements
=> New_List
(
12841 Make_Simple_Return_Statement
(Loc
, Ret_Expr
))));
12845 Make_Package_Body
(Loc
,
12846 Defining_Unit_Name
=> New_Copy
(Pack_Id
),
12847 Declarations
=> New_List
(Act_Body
));
12849 Insert_After
(Inst_Node
, Pack_Body
);
12850 Set_Corresponding_Spec
(Pack_Body
, Pack_Id
);
12851 Analyze
(Pack_Body
);
12856 -- Restore the context that was in effect prior to instantiating the
12857 -- subprogram body.
12859 Ignore_SPARK_Mode_Pragmas_In_Instance
:= Saved_ISMP
;
12860 Local_Suppress_Stack_Top
:= Saved_LSST
;
12861 Scope_Suppress
:= Saved_SS
;
12862 Style_Check
:= Saved_SC
;
12864 Expander_Mode_Restore
;
12865 Restore_Config_Switches
(Saved_CS
);
12866 Restore_Ghost_Region
(Saved_GM
, Saved_IGR
);
12867 Restore_SPARK_Mode
(Saved_SM
, Saved_SMP
);
12868 Restore_Warnings
(Saved_Warn
);
12869 end Instantiate_Subprogram_Body
;
12871 ----------------------
12872 -- Instantiate_Type --
12873 ----------------------
12875 function Instantiate_Type
12878 Analyzed_Formal
: Node_Id
;
12879 Actual_Decls
: List_Id
) return List_Id
12881 A_Gen_T
: constant Entity_Id
:=
12882 Defining_Identifier
(Analyzed_Formal
);
12883 Def
: constant Node_Id
:= Formal_Type_Definition
(Formal
);
12884 Gen_T
: constant Entity_Id
:= Defining_Identifier
(Formal
);
12886 Ancestor
: Entity_Id
:= Empty
;
12887 Decl_Node
: Node_Id
;
12888 Decl_Nodes
: List_Id
;
12892 procedure Check_Shared_Variable_Control_Aspects
;
12893 -- Ada 2022: Verify that shared variable control aspects (RM C.6)
12894 -- that may be specified for a formal type are obeyed by the actual.
12896 procedure Diagnose_Predicated_Actual
;
12897 -- There are a number of constructs in which a discrete type with
12898 -- predicates is illegal, e.g. as an index in an array type declaration.
12899 -- If a generic type is used is such a construct in a generic package
12900 -- declaration, it carries the flag No_Predicate_On_Actual. it is part
12901 -- of the generic contract that the actual cannot have predicates.
12903 function Subtypes_Match
(Gen_T
, Act_T
: Entity_Id
) return Boolean;
12904 -- Check that base types are the same and that the subtypes match
12905 -- statically. Used in several of the validation subprograms for
12906 -- actuals in instantiations.
12908 procedure Validate_Array_Type_Instance
;
12909 procedure Validate_Access_Subprogram_Instance
;
12910 procedure Validate_Access_Type_Instance
;
12911 procedure Validate_Derived_Type_Instance
;
12912 procedure Validate_Derived_Interface_Type_Instance
;
12913 procedure Validate_Discriminated_Formal_Type
;
12914 procedure Validate_Interface_Type_Instance
;
12915 procedure Validate_Private_Type_Instance
;
12916 procedure Validate_Incomplete_Type_Instance
;
12917 -- These procedures perform validation tests for the named case.
12918 -- Validate_Discriminated_Formal_Type is shared by formal private
12919 -- types and Ada 2012 formal incomplete types.
12921 --------------------------------------------
12922 -- Check_Shared_Variable_Control_Aspects --
12923 --------------------------------------------
12925 -- Ada 2022: Verify that shared variable control aspects (RM C.6)
12926 -- that may be specified for the formal are obeyed by the actual.
12927 -- If the formal is a derived type the aspect specifications must match.
12928 -- NOTE: AI12-0282 implies that matching of aspects is required between
12929 -- formal and actual in all cases, but this is too restrictive.
12930 -- In particular it violates a language design rule: a limited private
12931 -- indefinite formal can be matched by any actual. The current code
12932 -- reflects an older and more permissive version of RM C.6 (12/5).
12934 procedure Check_Shared_Variable_Control_Aspects
is
12936 if Ada_Version
>= Ada_2022
then
12937 if Is_Atomic
(A_Gen_T
) and then not Is_Atomic
(Act_T
) then
12939 ("actual for& must have Atomic aspect", Actual
, A_Gen_T
);
12941 elsif Is_Derived_Type
(A_Gen_T
)
12942 and then Is_Atomic
(A_Gen_T
) /= Is_Atomic
(Act_T
)
12945 ("actual for& has different Atomic aspect", Actual
, A_Gen_T
);
12948 if Is_Volatile
(A_Gen_T
) and then not Is_Volatile
(Act_T
) then
12950 ("actual for& must have Volatile aspect",
12953 elsif Is_Derived_Type
(A_Gen_T
)
12954 and then Is_Volatile
(A_Gen_T
) /= Is_Volatile
(Act_T
)
12957 ("actual for& has different Volatile aspect",
12961 -- We assume that an array type whose atomic component type
12962 -- is Atomic is equivalent to an array type with the explicit
12963 -- aspect Has_Atomic_Components. This is a reasonable inference
12964 -- from the intent of AI12-0282, and makes it legal to use an
12965 -- actual that does not have the identical aspect as the formal.
12966 -- Ditto for volatile components.
12969 Actual_Atomic_Comp
: constant Boolean :=
12970 Has_Atomic_Components
(Act_T
)
12971 or else (Is_Array_Type
(Act_T
)
12972 and then Is_Atomic
(Component_Type
(Act_T
)));
12974 if Has_Atomic_Components
(A_Gen_T
) /= Actual_Atomic_Comp
then
12976 ("formal and actual for& must agree on atomic components",
12982 Actual_Volatile_Comp
: constant Boolean :=
12983 Has_Volatile_Components
(Act_T
)
12984 or else (Is_Array_Type
(Act_T
)
12985 and then Is_Volatile
(Component_Type
(Act_T
)));
12987 if Has_Volatile_Components
(A_Gen_T
) /= Actual_Volatile_Comp
12990 ("actual for& must have volatile components",
12995 -- The following two aspects do not require exact matching,
12996 -- but only one-way agreement. See RM C.6.
12998 if Is_Independent
(A_Gen_T
) and then not Is_Independent
(Act_T
)
13001 ("actual for& must have Independent aspect specified",
13005 if Has_Independent_Components
(A_Gen_T
)
13006 and then not Has_Independent_Components
(Act_T
)
13009 ("actual for& must have Independent_Components specified",
13013 end Check_Shared_Variable_Control_Aspects
;
13015 ---------------------------------
13016 -- Diagnose_Predicated_Actual --
13017 ---------------------------------
13019 procedure Diagnose_Predicated_Actual
is
13021 if No_Predicate_On_Actual
(A_Gen_T
)
13022 and then Has_Predicates
(Act_T
)
13025 ("actual for& cannot be a type with predicate",
13026 Instantiation_Node
, A_Gen_T
);
13028 elsif No_Dynamic_Predicate_On_Actual
(A_Gen_T
)
13029 and then Has_Predicates
(Act_T
)
13030 and then not Has_Static_Predicate_Aspect
(Act_T
)
13033 ("actual for& cannot be a type with a dynamic predicate",
13034 Instantiation_Node
, A_Gen_T
);
13036 end Diagnose_Predicated_Actual
;
13038 --------------------
13039 -- Subtypes_Match --
13040 --------------------
13042 function Subtypes_Match
(Gen_T
, Act_T
: Entity_Id
) return Boolean is
13043 T
: constant Entity_Id
:= Get_Instance_Of
(Gen_T
);
13046 -- Check that the base types, root types (when dealing with class
13047 -- wide types), or designated types (when dealing with anonymous
13048 -- access types) of Gen_T and Act_T are statically matching subtypes.
13050 return ((Base_Type
(T
) = Act_T
13051 or else Base_Type
(T
) = Base_Type
(Act_T
))
13052 and then Subtypes_Statically_Match
(T
, Act_T
))
13054 or else (Is_Class_Wide_Type
(Gen_T
)
13055 and then Is_Class_Wide_Type
(Act_T
)
13056 and then Subtypes_Match
13057 (Get_Instance_Of
(Root_Type
(Gen_T
)),
13058 Root_Type
(Act_T
)))
13060 or else (Is_Anonymous_Access_Type
(Gen_T
)
13061 and then Ekind
(Act_T
) = Ekind
(Gen_T
)
13062 and then Subtypes_Statically_Match
13063 (Designated_Type
(Gen_T
), Designated_Type
(Act_T
)));
13064 end Subtypes_Match
;
13066 -----------------------------------------
13067 -- Validate_Access_Subprogram_Instance --
13068 -----------------------------------------
13070 procedure Validate_Access_Subprogram_Instance
is
13072 if not Is_Access_Type
(Act_T
)
13073 or else Ekind
(Designated_Type
(Act_T
)) /= E_Subprogram_Type
13076 ("expect access type in instantiation of &", Actual
, Gen_T
);
13077 Abandon_Instantiation
(Actual
);
13080 -- According to AI05-288, actuals for access_to_subprograms must be
13081 -- subtype conformant with the generic formal. Previous to AI05-288
13082 -- only mode conformance was required.
13084 -- This is a binding interpretation that applies to previous versions
13085 -- of the language, no need to maintain previous weaker checks.
13087 Check_Subtype_Conformant
13088 (Designated_Type
(Act_T
),
13089 Designated_Type
(A_Gen_T
),
13093 if Ekind
(Base_Type
(Act_T
)) = E_Access_Protected_Subprogram_Type
then
13094 if Ekind
(A_Gen_T
) = E_Access_Subprogram_Type
then
13096 ("protected access type not allowed for formal &",
13100 elsif Ekind
(A_Gen_T
) = E_Access_Protected_Subprogram_Type
then
13102 ("expect protected access type for formal &",
13106 -- If the formal has a specified convention (which in most cases
13107 -- will be StdCall) verify that the actual has the same convention.
13109 if Has_Convention_Pragma
(A_Gen_T
)
13110 and then Convention
(A_Gen_T
) /= Convention
(Act_T
)
13112 Error_Msg_Name_1
:= Get_Convention_Name
(Convention
(A_Gen_T
));
13114 ("actual for formal & must have convention %", Actual
, Gen_T
);
13117 if Can_Never_Be_Null
(A_Gen_T
) /= Can_Never_Be_Null
(Act_T
) then
13119 ("non null exclusion of actual and formal & do not match",
13122 end Validate_Access_Subprogram_Instance
;
13124 -----------------------------------
13125 -- Validate_Access_Type_Instance --
13126 -----------------------------------
13128 procedure Validate_Access_Type_Instance
is
13129 Desig_Type
: constant Entity_Id
:=
13130 Find_Actual_Type
(Designated_Type
(A_Gen_T
), A_Gen_T
);
13131 Desig_Act
: Entity_Id
;
13134 if not Is_Access_Type
(Act_T
) then
13136 ("expect access type in instantiation of &", Actual
, Gen_T
);
13137 Abandon_Instantiation
(Actual
);
13140 if Is_Access_Constant
(A_Gen_T
) then
13141 if not Is_Access_Constant
(Act_T
) then
13143 ("actual type must be access-to-constant type", Actual
);
13144 Abandon_Instantiation
(Actual
);
13147 if Is_Access_Constant
(Act_T
) then
13149 ("actual type must be access-to-variable type", Actual
);
13150 Abandon_Instantiation
(Actual
);
13152 elsif Ekind
(A_Gen_T
) = E_General_Access_Type
13153 and then Ekind
(Base_Type
(Act_T
)) /= E_General_Access_Type
13156 ("actual must be general access type!", Actual
);
13157 Error_Msg_NE
-- CODEFIX
13158 ("\add ALL to }!", Actual
, Act_T
);
13159 Abandon_Instantiation
(Actual
);
13163 -- The designated subtypes, that is to say the subtypes introduced
13164 -- by an access type declaration (and not by a subtype declaration)
13167 Desig_Act
:= Designated_Type
(Base_Type
(Act_T
));
13169 -- The designated type may have been introduced through a limited_
13170 -- with clause, in which case retrieve the non-limited view. This
13171 -- applies to incomplete types as well as to class-wide types.
13173 if From_Limited_With
(Desig_Act
) then
13174 Desig_Act
:= Available_View
(Desig_Act
);
13177 if not Subtypes_Match
(Desig_Type
, Desig_Act
) then
13179 ("designated type of actual does not match that of formal &",
13182 if not Predicates_Match
(Desig_Type
, Desig_Act
) then
13183 Error_Msg_N
("\predicates do not match", Actual
);
13186 Abandon_Instantiation
(Actual
);
13189 -- Ada 2005: null-exclusion indicators of the two types must agree
13191 if Can_Never_Be_Null
(A_Gen_T
) /= Can_Never_Be_Null
(Act_T
) then
13193 ("non null exclusion of actual and formal & do not match",
13196 end Validate_Access_Type_Instance
;
13198 ----------------------------------
13199 -- Validate_Array_Type_Instance --
13200 ----------------------------------
13202 procedure Validate_Array_Type_Instance
is
13207 function Formal_Dimensions
return Nat
;
13208 -- Count number of dimensions in array type formal
13210 -----------------------
13211 -- Formal_Dimensions --
13212 -----------------------
13214 function Formal_Dimensions
return Nat
is
13219 if Nkind
(Def
) = N_Constrained_Array_Definition
then
13220 Index
:= First
(Discrete_Subtype_Definitions
(Def
));
13222 Index
:= First
(Subtype_Marks
(Def
));
13225 while Present
(Index
) loop
13231 end Formal_Dimensions
;
13233 -- Start of processing for Validate_Array_Type_Instance
13236 if not Is_Array_Type
(Act_T
) then
13238 ("expect array type in instantiation of &", Actual
, Gen_T
);
13239 Abandon_Instantiation
(Actual
);
13241 elsif Nkind
(Def
) = N_Constrained_Array_Definition
then
13242 if not (Is_Constrained
(Act_T
)) then
13244 ("expect constrained array in instantiation of &",
13246 Abandon_Instantiation
(Actual
);
13250 if Is_Constrained
(Act_T
) then
13252 ("expect unconstrained array in instantiation of &",
13254 Abandon_Instantiation
(Actual
);
13258 if Formal_Dimensions
/= Number_Dimensions
(Act_T
) then
13260 ("dimensions of actual do not match formal &", Actual
, Gen_T
);
13261 Abandon_Instantiation
(Actual
);
13264 I1
:= First_Index
(A_Gen_T
);
13265 I2
:= First_Index
(Act_T
);
13266 for J
in 1 .. Formal_Dimensions
loop
13268 -- If the indexes of the actual were given by a subtype_mark,
13269 -- the index was transformed into a range attribute. Retrieve
13270 -- the original type mark for checking.
13272 if Is_Entity_Name
(Original_Node
(I2
)) then
13273 T2
:= Entity
(Original_Node
(I2
));
13278 if not Subtypes_Match
13279 (Find_Actual_Type
(Etype
(I1
), A_Gen_T
), T2
)
13282 ("index types of actual do not match those of formal &",
13284 Abandon_Instantiation
(Actual
);
13291 -- Check matching subtypes. Note that there are complex visibility
13292 -- issues when the generic is a child unit and some aspect of the
13293 -- generic type is declared in a parent unit of the generic. We do
13294 -- the test to handle this special case only after a direct check
13295 -- for static matching has failed. The case where both the component
13296 -- type and the array type are separate formals, and the component
13297 -- type is a private view may also require special checking in
13298 -- Subtypes_Match. Finally, we assume that a child instance where
13299 -- the component type comes from a formal of a parent instance is
13300 -- correct because the generic was correct. A more precise check
13301 -- seems too complex to install???
13304 (Component_Type
(A_Gen_T
), Component_Type
(Act_T
))
13307 (Find_Actual_Type
(Component_Type
(A_Gen_T
), A_Gen_T
),
13308 Component_Type
(Act_T
))
13310 (not Inside_A_Generic
13311 and then Is_Child_Unit
(Scope
(Component_Type
(A_Gen_T
))))
13316 ("component subtype of actual does not match that of formal &",
13318 Abandon_Instantiation
(Actual
);
13321 if Has_Aliased_Components
(A_Gen_T
)
13322 and then not Has_Aliased_Components
(Act_T
)
13325 ("actual must have aliased components to match formal type &",
13328 end Validate_Array_Type_Instance
;
13330 -----------------------------------------------
13331 -- Validate_Derived_Interface_Type_Instance --
13332 -----------------------------------------------
13334 procedure Validate_Derived_Interface_Type_Instance
is
13335 Par
: constant Entity_Id
:= Entity
(Subtype_Indication
(Def
));
13339 -- First apply interface instance checks
13341 Validate_Interface_Type_Instance
;
13343 -- Verify that immediate parent interface is an ancestor of
13347 and then not Interface_Present_In_Ancestor
(Act_T
, Par
)
13350 ("interface actual must include progenitor&", Actual
, Par
);
13353 -- Now verify that the actual includes all other ancestors of
13356 Elmt
:= First_Elmt
(Interfaces
(A_Gen_T
));
13357 while Present
(Elmt
) loop
13358 if not Interface_Present_In_Ancestor
13359 (Act_T
, Get_Instance_Of
(Node
(Elmt
)))
13362 ("interface actual must include progenitor&",
13363 Actual
, Node
(Elmt
));
13368 end Validate_Derived_Interface_Type_Instance
;
13370 ------------------------------------
13371 -- Validate_Derived_Type_Instance --
13372 ------------------------------------
13374 procedure Validate_Derived_Type_Instance
is
13375 Actual_Discr
: Entity_Id
;
13376 Ancestor_Discr
: Entity_Id
;
13379 -- Verify that the actual includes the progenitors of the formal,
13380 -- if any. The formal may depend on previous formals and their
13381 -- instance, so we must examine instance of interfaces if present.
13382 -- The actual may be an extension of an interface, in which case
13383 -- it does not appear in the interface list, so this must be
13384 -- checked separately.
13386 if Present
(Interface_List
(Def
)) then
13387 if not Has_Interfaces
(Act_T
) then
13389 ("actual must implement all interfaces of formal&",
13394 Act_Iface_List
: Elist_Id
;
13396 Iface_Ent
: Entity_Id
;
13398 function Instance_Exists
(I
: Entity_Id
) return Boolean;
13399 -- If the interface entity is declared in a generic unit,
13400 -- this can only be legal if we are within an instantiation
13401 -- of a child of that generic. There is currently no
13402 -- mechanism to relate an interface declared within a
13403 -- generic to the corresponding interface in an instance,
13404 -- so we traverse the list of interfaces of the actual,
13405 -- looking for a name match.
13407 ---------------------
13408 -- Instance_Exists --
13409 ---------------------
13411 function Instance_Exists
(I
: Entity_Id
) return Boolean is
13412 Iface_Elmt
: Elmt_Id
;
13415 Iface_Elmt
:= First_Elmt
(Act_Iface_List
);
13416 while Present
(Iface_Elmt
) loop
13417 if Is_Generic_Instance
(Scope
(Node
(Iface_Elmt
)))
13418 and then Chars
(Node
(Iface_Elmt
)) = Chars
(I
)
13423 Next_Elmt
(Iface_Elmt
);
13427 end Instance_Exists
;
13430 Iface
:= First
(Abstract_Interface_List
(A_Gen_T
));
13431 Collect_Interfaces
(Act_T
, Act_Iface_List
);
13433 while Present
(Iface
) loop
13434 Iface_Ent
:= Get_Instance_Of
(Entity
(Iface
));
13436 if Is_Ancestor
(Iface_Ent
, Act_T
)
13437 or else Is_Progenitor
(Iface_Ent
, Act_T
)
13441 elsif Ekind
(Scope
(Iface_Ent
)) = E_Generic_Package
13442 and then Instance_Exists
(Iface_Ent
)
13447 Error_Msg_Name_1
:= Chars
(Act_T
);
13449 ("actual% must implement interface&",
13450 Actual
, Etype
(Iface
));
13459 -- If the parent type in the generic declaration is itself a previous
13460 -- formal type, then it is local to the generic and absent from the
13461 -- analyzed generic definition. In that case the ancestor is the
13462 -- instance of the formal (which must have been instantiated
13463 -- previously), unless the ancestor is itself a formal derived type.
13464 -- In this latter case (which is the subject of Corrigendum 8652/0038
13465 -- (AI-202) the ancestor of the formals is the ancestor of its
13466 -- parent. Otherwise, the analyzed generic carries the parent type.
13467 -- If the parent type is defined in a previous formal package, then
13468 -- the scope of that formal package is that of the generic type
13469 -- itself, and it has already been mapped into the corresponding type
13470 -- in the actual package.
13472 -- Common case: parent type defined outside of the generic
13474 if Is_Entity_Name
(Subtype_Mark
(Def
))
13475 and then Present
(Entity
(Subtype_Mark
(Def
)))
13477 Ancestor
:= Get_Instance_Of
(Entity
(Subtype_Mark
(Def
)));
13479 -- Check whether parent is defined in a previous formal package
13482 Scope
(Scope
(Base_Type
(Etype
(A_Gen_T
)))) = Scope
(A_Gen_T
)
13485 Get_Instance_Of
(Base_Type
(Etype
(A_Gen_T
)));
13487 -- The type may be a local derivation, or a type extension of a
13488 -- previous formal, or of a formal of a parent package.
13490 elsif Is_Derived_Type
(Get_Instance_Of
(A_Gen_T
))
13492 Ekind
(Get_Instance_Of
(A_Gen_T
)) = E_Record_Type_With_Private
13494 -- Check whether the parent is another derived formal type in the
13495 -- same generic unit.
13497 if Etype
(A_Gen_T
) /= A_Gen_T
13498 and then Is_Generic_Type
(Etype
(A_Gen_T
))
13499 and then Scope
(Etype
(A_Gen_T
)) = Scope
(A_Gen_T
)
13500 and then Etype
(Etype
(A_Gen_T
)) /= Etype
(A_Gen_T
)
13502 -- Locate ancestor of parent from the subtype declaration
13503 -- created for the actual.
13509 Decl
:= First
(Actual_Decls
);
13510 while Present
(Decl
) loop
13511 if Nkind
(Decl
) = N_Subtype_Declaration
13512 and then Chars
(Defining_Identifier
(Decl
)) =
13513 Chars
(Etype
(A_Gen_T
))
13515 Ancestor
:= Generic_Parent_Type
(Decl
);
13523 pragma Assert
(Present
(Ancestor
));
13525 -- The ancestor itself may be a previous formal that has been
13528 Ancestor
:= Get_Instance_Of
(Ancestor
);
13531 Ancestor
:= Get_Instance_Of
(Etype
(Get_Instance_Of
(A_Gen_T
)));
13534 -- Check whether parent is a previous formal of the current generic
13536 elsif Is_Derived_Type
(A_Gen_T
)
13537 and then Is_Generic_Type
(Etype
(A_Gen_T
))
13538 and then Scope
(A_Gen_T
) = Scope
(Etype
(A_Gen_T
))
13540 Ancestor
:= Get_Instance_Of
(First_Subtype
(Etype
(A_Gen_T
)));
13542 -- An unusual case: the actual is a type declared in a parent unit,
13543 -- but is not a formal type so there is no instance_of for it.
13544 -- Retrieve it by analyzing the record extension.
13546 elsif Is_Child_Unit
(Scope
(A_Gen_T
))
13547 and then In_Open_Scopes
(Scope
(Act_T
))
13548 and then Is_Generic_Instance
(Scope
(Act_T
))
13550 Analyze
(Subtype_Mark
(Def
));
13551 Ancestor
:= Entity
(Subtype_Mark
(Def
));
13554 Ancestor
:= Get_Instance_Of
(Etype
(Base_Type
(A_Gen_T
)));
13557 -- If the formal derived type has pragma Preelaborable_Initialization
13558 -- then the actual type must have preelaborable initialization.
13560 if Known_To_Have_Preelab_Init
(A_Gen_T
)
13561 and then not Has_Preelaborable_Initialization
(Act_T
)
13564 ("actual for & must have preelaborable initialization",
13568 -- Ada 2005 (AI-251)
13570 if Ada_Version
>= Ada_2005
and then Is_Interface
(Ancestor
) then
13571 if not Interface_Present_In_Ancestor
(Act_T
, Ancestor
) then
13573 ("(Ada 2005) expected type implementing & in instantiation",
13577 -- Finally verify that the (instance of) the ancestor is an ancestor
13580 elsif not Is_Ancestor
(Base_Type
(Ancestor
), Act_T
) then
13582 ("expect type derived from & in instantiation",
13583 Actual
, First_Subtype
(Ancestor
));
13584 Abandon_Instantiation
(Actual
);
13587 -- Ada 2005 (AI-443): Synchronized formal derived type checks. Note
13588 -- that the formal type declaration has been rewritten as a private
13591 if Ada_Version
>= Ada_2005
13592 and then Nkind
(Parent
(A_Gen_T
)) = N_Private_Extension_Declaration
13593 and then Synchronized_Present
(Parent
(A_Gen_T
))
13595 -- The actual must be a synchronized tagged type
13597 if not Is_Tagged_Type
(Act_T
) then
13599 ("actual of synchronized type must be tagged", Actual
);
13600 Abandon_Instantiation
(Actual
);
13602 elsif Nkind
(Parent
(Act_T
)) = N_Full_Type_Declaration
13603 and then Nkind
(Type_Definition
(Parent
(Act_T
))) =
13604 N_Derived_Type_Definition
13605 and then not Synchronized_Present
13606 (Type_Definition
(Parent
(Act_T
)))
13609 ("actual of synchronized type must be synchronized", Actual
);
13610 Abandon_Instantiation
(Actual
);
13614 -- Perform atomic/volatile checks (RM C.6(12)). Note that AI05-0218-1
13615 -- removes the second instance of the phrase "or allow pass by copy".
13617 -- For Ada 2022, the aspect may be specified explicitly for the
13618 -- formal regardless of whether an ancestor obeys it.
13620 if Is_Atomic
(Act_T
)
13621 and then not Is_Atomic
(Ancestor
)
13622 and then not Is_Atomic
(A_Gen_T
)
13625 ("cannot have atomic actual type for non-atomic formal type",
13628 elsif Is_Volatile
(Act_T
)
13629 and then not Is_Volatile
(Ancestor
)
13630 and then not Is_Volatile
(A_Gen_T
)
13633 ("cannot have volatile actual type for non-volatile formal type",
13637 -- It should not be necessary to check for unknown discriminants on
13638 -- Formal, but for some reason Has_Unknown_Discriminants is false for
13639 -- A_Gen_T, so Is_Definite_Subtype incorrectly returns True. This
13640 -- needs fixing. ???
13642 if Is_Definite_Subtype
(A_Gen_T
)
13643 and then not Unknown_Discriminants_Present
(Formal
)
13644 and then not Is_Definite_Subtype
(Act_T
)
13646 Error_Msg_N
("actual subtype must be constrained", Actual
);
13647 Abandon_Instantiation
(Actual
);
13650 if not Unknown_Discriminants_Present
(Formal
) then
13651 if Is_Constrained
(Ancestor
) then
13652 if not Is_Constrained
(Act_T
) then
13653 Error_Msg_N
("actual subtype must be constrained", Actual
);
13654 Abandon_Instantiation
(Actual
);
13657 -- Ancestor is unconstrained, Check if generic formal and actual
13658 -- agree on constrainedness. The check only applies to array types
13659 -- and discriminated types.
13661 elsif Is_Constrained
(Act_T
) then
13662 if Ekind
(Ancestor
) = E_Access_Type
13663 or else (not Is_Constrained
(A_Gen_T
)
13664 and then Is_Composite_Type
(A_Gen_T
))
13666 Error_Msg_N
("actual subtype must be unconstrained", Actual
);
13667 Abandon_Instantiation
(Actual
);
13670 -- A class-wide type is only allowed if the formal has unknown
13673 elsif Is_Class_Wide_Type
(Act_T
)
13674 and then not Has_Unknown_Discriminants
(Ancestor
)
13677 ("actual for & cannot be a class-wide type", Actual
, Gen_T
);
13678 Abandon_Instantiation
(Actual
);
13680 -- Otherwise, the formal and actual must have the same number
13681 -- of discriminants and each discriminant of the actual must
13682 -- correspond to a discriminant of the formal.
13684 elsif Has_Discriminants
(Act_T
)
13685 and then not Has_Unknown_Discriminants
(Act_T
)
13686 and then Has_Discriminants
(Ancestor
)
13688 Actual_Discr
:= First_Discriminant
(Act_T
);
13689 Ancestor_Discr
:= First_Discriminant
(Ancestor
);
13690 while Present
(Actual_Discr
)
13691 and then Present
(Ancestor_Discr
)
13693 if Base_Type
(Act_T
) /= Base_Type
(Ancestor
) and then
13694 No
(Corresponding_Discriminant
(Actual_Discr
))
13697 ("discriminant & does not correspond "
13698 & "to ancestor discriminant", Actual
, Actual_Discr
);
13699 Abandon_Instantiation
(Actual
);
13702 Next_Discriminant
(Actual_Discr
);
13703 Next_Discriminant
(Ancestor_Discr
);
13706 if Present
(Actual_Discr
) or else Present
(Ancestor_Discr
) then
13708 ("actual for & must have same number of discriminants",
13710 Abandon_Instantiation
(Actual
);
13713 -- This case should be caught by the earlier check for
13714 -- constrainedness, but the check here is added for completeness.
13716 elsif Has_Discriminants
(Act_T
)
13717 and then not Has_Unknown_Discriminants
(Act_T
)
13720 ("actual for & must not have discriminants", Actual
, Gen_T
);
13721 Abandon_Instantiation
(Actual
);
13723 elsif Has_Discriminants
(Ancestor
) then
13725 ("actual for & must have known discriminants", Actual
, Gen_T
);
13726 Abandon_Instantiation
(Actual
);
13729 if not Subtypes_Statically_Compatible
13730 (Act_T
, Ancestor
, Formal_Derived_Matching
=> True)
13733 ("actual for & must be statically compatible with ancestor",
13736 if not Predicates_Compatible
(Act_T
, Ancestor
) then
13738 ("\predicate on actual is not compatible with ancestor",
13742 Abandon_Instantiation
(Actual
);
13746 -- If the formal and actual types are abstract, check that there
13747 -- are no abstract primitives of the actual type that correspond to
13748 -- nonabstract primitives of the formal type (second sentence of
13751 if Is_Abstract_Type
(A_Gen_T
) and then Is_Abstract_Type
(Act_T
) then
13752 Check_Abstract_Primitives
: declare
13753 Gen_Prims
: constant Elist_Id
:=
13754 Primitive_Operations
(A_Gen_T
);
13755 Gen_Elmt
: Elmt_Id
;
13756 Gen_Subp
: Entity_Id
;
13757 Anc_Subp
: Entity_Id
;
13758 Anc_Formal
: Entity_Id
;
13759 Anc_F_Type
: Entity_Id
;
13761 Act_Prims
: constant Elist_Id
:= Primitive_Operations
(Act_T
);
13762 Act_Elmt
: Elmt_Id
;
13763 Act_Subp
: Entity_Id
;
13764 Act_Formal
: Entity_Id
;
13765 Act_F_Type
: Entity_Id
;
13767 Subprograms_Correspond
: Boolean;
13769 function Is_Tagged_Ancestor
(T1
, T2
: Entity_Id
) return Boolean;
13770 -- Returns true if T2 is derived directly or indirectly from
13771 -- T1, including derivations from interfaces. T1 and T2 are
13772 -- required to be specific tagged base types.
13774 ------------------------
13775 -- Is_Tagged_Ancestor --
13776 ------------------------
13778 function Is_Tagged_Ancestor
(T1
, T2
: Entity_Id
) return Boolean
13780 Intfc_Elmt
: Elmt_Id
;
13783 -- The predicate is satisfied if the types are the same
13788 -- If we've reached the top of the derivation chain then
13789 -- we know that T1 is not an ancestor of T2.
13791 elsif Etype
(T2
) = T2
then
13794 -- Proceed to check T2's immediate parent
13796 elsif Is_Ancestor
(T1
, Base_Type
(Etype
(T2
))) then
13799 -- Finally, check to see if T1 is an ancestor of any of T2's
13803 Intfc_Elmt
:= First_Elmt
(Interfaces
(T2
));
13804 while Present
(Intfc_Elmt
) loop
13805 if Is_Ancestor
(T1
, Node
(Intfc_Elmt
)) then
13809 Next_Elmt
(Intfc_Elmt
);
13814 end Is_Tagged_Ancestor
;
13816 -- Start of processing for Check_Abstract_Primitives
13819 -- Loop over all of the formal derived type's primitives
13821 Gen_Elmt
:= First_Elmt
(Gen_Prims
);
13822 while Present
(Gen_Elmt
) loop
13823 Gen_Subp
:= Node
(Gen_Elmt
);
13825 -- If the primitive of the formal is not abstract, then
13826 -- determine whether there is a corresponding primitive of
13827 -- the actual type that's abstract.
13829 if not Is_Abstract_Subprogram
(Gen_Subp
) then
13830 Act_Elmt
:= First_Elmt
(Act_Prims
);
13831 while Present
(Act_Elmt
) loop
13832 Act_Subp
:= Node
(Act_Elmt
);
13834 -- If we find an abstract primitive of the actual,
13835 -- then we need to test whether it corresponds to the
13836 -- subprogram from which the generic formal primitive
13839 if Is_Abstract_Subprogram
(Act_Subp
) then
13840 Anc_Subp
:= Alias
(Gen_Subp
);
13842 -- Test whether we have a corresponding primitive
13843 -- by comparing names, kinds, formal types, and
13846 if Chars
(Anc_Subp
) = Chars
(Act_Subp
)
13847 and then Ekind
(Anc_Subp
) = Ekind
(Act_Subp
)
13849 Anc_Formal
:= First_Formal
(Anc_Subp
);
13850 Act_Formal
:= First_Formal
(Act_Subp
);
13851 while Present
(Anc_Formal
)
13852 and then Present
(Act_Formal
)
13854 Anc_F_Type
:= Etype
(Anc_Formal
);
13855 Act_F_Type
:= Etype
(Act_Formal
);
13857 if Ekind
(Anc_F_Type
) =
13858 E_Anonymous_Access_Type
13860 Anc_F_Type
:= Designated_Type
(Anc_F_Type
);
13862 if Ekind
(Act_F_Type
) =
13863 E_Anonymous_Access_Type
13866 Designated_Type
(Act_F_Type
);
13872 Ekind
(Act_F_Type
) = E_Anonymous_Access_Type
13877 Anc_F_Type
:= Base_Type
(Anc_F_Type
);
13878 Act_F_Type
:= Base_Type
(Act_F_Type
);
13880 -- If the formal is controlling, then the
13881 -- the type of the actual primitive's formal
13882 -- must be derived directly or indirectly
13883 -- from the type of the ancestor primitive's
13886 if Is_Controlling_Formal
(Anc_Formal
) then
13887 if not Is_Tagged_Ancestor
13888 (Anc_F_Type
, Act_F_Type
)
13893 -- Otherwise the types of the formals must
13896 elsif Anc_F_Type
/= Act_F_Type
then
13900 Next_Formal
(Anc_Formal
);
13901 Next_Formal
(Act_Formal
);
13904 -- If we traversed through all of the formals
13905 -- then so far the subprograms correspond, so
13906 -- now check that any result types correspond.
13908 if No
(Anc_Formal
) and then No
(Act_Formal
) then
13909 Subprograms_Correspond
:= True;
13911 if Ekind
(Act_Subp
) = E_Function
then
13912 Anc_F_Type
:= Etype
(Anc_Subp
);
13913 Act_F_Type
:= Etype
(Act_Subp
);
13915 if Ekind
(Anc_F_Type
) =
13916 E_Anonymous_Access_Type
13919 Designated_Type
(Anc_F_Type
);
13921 if Ekind
(Act_F_Type
) =
13922 E_Anonymous_Access_Type
13925 Designated_Type
(Act_F_Type
);
13927 Subprograms_Correspond
:= False;
13932 = E_Anonymous_Access_Type
13934 Subprograms_Correspond
:= False;
13937 Anc_F_Type
:= Base_Type
(Anc_F_Type
);
13938 Act_F_Type
:= Base_Type
(Act_F_Type
);
13940 -- Now either the result types must be
13941 -- the same or, if the result type is
13942 -- controlling, the result type of the
13943 -- actual primitive must descend from the
13944 -- result type of the ancestor primitive.
13946 if Subprograms_Correspond
13947 and then Anc_F_Type
/= Act_F_Type
13949 Has_Controlling_Result
(Anc_Subp
)
13950 and then not Is_Tagged_Ancestor
13951 (Anc_F_Type
, Act_F_Type
)
13953 Subprograms_Correspond
:= False;
13957 -- Found a matching subprogram belonging to
13958 -- formal ancestor type, so actual subprogram
13959 -- corresponds and this violates 3.9.3(9).
13961 if Subprograms_Correspond
then
13963 ("abstract subprogram & overrides "
13964 & "nonabstract subprogram of ancestor",
13971 Next_Elmt
(Act_Elmt
);
13975 Next_Elmt
(Gen_Elmt
);
13977 end Check_Abstract_Primitives
;
13980 -- Verify that limitedness matches. If parent is a limited
13981 -- interface then the generic formal is not unless declared
13982 -- explicitly so. If not declared limited, the actual cannot be
13983 -- limited (see AI05-0087).
13985 if Is_Limited_Type
(Act_T
) and then not Is_Limited_Type
(A_Gen_T
) then
13986 if not In_Instance
then
13988 ("actual for non-limited & cannot be a limited type",
13990 Explain_Limited_Type
(Act_T
, Actual
);
13991 Abandon_Instantiation
(Actual
);
13995 -- Check for AI12-0036
13998 Formal_Is_Private_Extension
: constant Boolean :=
13999 Nkind
(Parent
(A_Gen_T
)) = N_Private_Extension_Declaration
;
14001 Actual_Is_Tagged
: constant Boolean := Is_Tagged_Type
(Act_T
);
14004 if Actual_Is_Tagged
/= Formal_Is_Private_Extension
then
14005 if not In_Instance
then
14006 if Actual_Is_Tagged
then
14008 ("actual for & cannot be a tagged type", Actual
, Gen_T
);
14011 ("actual for & must be a tagged type", Actual
, Gen_T
);
14014 Abandon_Instantiation
(Actual
);
14018 end Validate_Derived_Type_Instance
;
14020 ----------------------------------------
14021 -- Validate_Discriminated_Formal_Type --
14022 ----------------------------------------
14024 procedure Validate_Discriminated_Formal_Type
is
14025 Formal_Discr
: Entity_Id
;
14026 Actual_Discr
: Entity_Id
;
14027 Formal_Subt
: Entity_Id
;
14030 if Has_Discriminants
(A_Gen_T
) then
14031 if not Has_Discriminants
(Act_T
) then
14033 ("actual for & must have discriminants", Actual
, Gen_T
);
14034 Abandon_Instantiation
(Actual
);
14036 elsif Is_Constrained
(Act_T
) then
14038 ("actual for & must be unconstrained", Actual
, Gen_T
);
14039 Abandon_Instantiation
(Actual
);
14042 Formal_Discr
:= First_Discriminant
(A_Gen_T
);
14043 Actual_Discr
:= First_Discriminant
(Act_T
);
14044 while Formal_Discr
/= Empty
loop
14045 if Actual_Discr
= Empty
then
14047 ("discriminants on actual do not match formal",
14049 Abandon_Instantiation
(Actual
);
14052 Formal_Subt
:= Get_Instance_Of
(Etype
(Formal_Discr
));
14054 -- Access discriminants match if designated types do
14056 if Ekind
(Base_Type
(Formal_Subt
)) = E_Anonymous_Access_Type
14057 and then (Ekind
(Base_Type
(Etype
(Actual_Discr
)))) =
14058 E_Anonymous_Access_Type
14062 (Designated_Type
(Base_Type
(Formal_Subt
))),
14063 Designated_Type
(Base_Type
(Etype
(Actual_Discr
))))
14067 elsif Base_Type
(Formal_Subt
) /=
14068 Base_Type
(Etype
(Actual_Discr
))
14071 ("types of actual discriminants must match formal",
14073 Abandon_Instantiation
(Actual
);
14075 elsif not Subtypes_Statically_Match
14076 (Formal_Subt
, Etype
(Actual_Discr
))
14077 and then Ada_Version
>= Ada_95
14080 ("subtypes of actual discriminants must match formal",
14082 Abandon_Instantiation
(Actual
);
14085 Next_Discriminant
(Formal_Discr
);
14086 Next_Discriminant
(Actual_Discr
);
14089 if Actual_Discr
/= Empty
then
14091 ("discriminants on actual do not match formal",
14093 Abandon_Instantiation
(Actual
);
14097 end Validate_Discriminated_Formal_Type
;
14099 ---------------------------------------
14100 -- Validate_Incomplete_Type_Instance --
14101 ---------------------------------------
14103 procedure Validate_Incomplete_Type_Instance
is
14105 if not Is_Tagged_Type
(Act_T
)
14106 and then Is_Tagged_Type
(A_Gen_T
)
14109 ("actual for & must be a tagged type", Actual
, Gen_T
);
14112 Validate_Discriminated_Formal_Type
;
14113 end Validate_Incomplete_Type_Instance
;
14115 --------------------------------------
14116 -- Validate_Interface_Type_Instance --
14117 --------------------------------------
14119 procedure Validate_Interface_Type_Instance
is
14121 if not Is_Interface
(Act_T
) then
14123 ("actual for formal interface type must be an interface",
14126 elsif Is_Limited_Type
(Act_T
) /= Is_Limited_Type
(A_Gen_T
)
14127 or else Is_Task_Interface
(A_Gen_T
) /= Is_Task_Interface
(Act_T
)
14128 or else Is_Protected_Interface
(A_Gen_T
) /=
14129 Is_Protected_Interface
(Act_T
)
14130 or else Is_Synchronized_Interface
(A_Gen_T
) /=
14131 Is_Synchronized_Interface
(Act_T
)
14134 ("actual for interface& does not match (RM 12.5.5(4))",
14137 end Validate_Interface_Type_Instance
;
14139 ------------------------------------
14140 -- Validate_Private_Type_Instance --
14141 ------------------------------------
14143 procedure Validate_Private_Type_Instance
is
14145 if Is_Limited_Type
(Act_T
)
14146 and then not Is_Limited_Type
(A_Gen_T
)
14148 if In_Instance
then
14152 ("actual for non-limited & cannot be a limited type", Actual
,
14154 Explain_Limited_Type
(Act_T
, Actual
);
14155 Abandon_Instantiation
(Actual
);
14158 elsif Known_To_Have_Preelab_Init
(A_Gen_T
)
14159 and then not Has_Preelaborable_Initialization
(Act_T
)
14162 ("actual for & must have preelaborable initialization", Actual
,
14165 elsif not Is_Definite_Subtype
(Act_T
)
14166 and then Is_Definite_Subtype
(A_Gen_T
)
14167 and then Ada_Version
>= Ada_95
14170 ("actual for & must be a definite subtype", Actual
, Gen_T
);
14172 elsif not Is_Tagged_Type
(Act_T
)
14173 and then Is_Tagged_Type
(A_Gen_T
)
14176 ("actual for & must be a tagged type", Actual
, Gen_T
);
14179 Validate_Discriminated_Formal_Type
;
14181 end Validate_Private_Type_Instance
;
14183 -- Start of processing for Instantiate_Type
14186 if Get_Instance_Of
(A_Gen_T
) /= A_Gen_T
then
14187 Error_Msg_N
("duplicate instantiation of generic type", Actual
);
14188 return New_List
(Error
);
14191 if not Is_Entity_Name
(Actual
)
14192 or else not Is_Type
(Entity
(Actual
))
14195 ("expect valid subtype mark to instantiate &", Actual
, Gen_T
);
14196 Abandon_Instantiation
(Actual
);
14199 Act_T
:= Entity
(Actual
);
14201 -- Ada 2005 (AI-216): An Unchecked_Union subtype shall only be passed
14202 -- as a generic actual parameter if the corresponding formal type
14203 -- does not have a known_discriminant_part, or is a formal derived
14204 -- type that is an Unchecked_Union type.
14206 if Is_Unchecked_Union
(Base_Type
(Act_T
)) then
14207 if not Has_Discriminants
(A_Gen_T
)
14208 or else (Is_Derived_Type
(A_Gen_T
)
14209 and then Is_Unchecked_Union
(A_Gen_T
))
14213 Error_Msg_N
("unchecked union cannot be the actual for a "
14214 & "discriminated formal type", Act_T
);
14219 -- Deal with fixed/floating restrictions
14221 if Is_Floating_Point_Type
(Act_T
) then
14222 Check_Restriction
(No_Floating_Point
, Actual
);
14223 elsif Is_Fixed_Point_Type
(Act_T
) then
14224 Check_Restriction
(No_Fixed_Point
, Actual
);
14227 -- Deal with error of using incomplete type as generic actual.
14228 -- This includes limited views of a type, even if the non-limited
14229 -- view may be available.
14231 if Ekind
(Act_T
) = E_Incomplete_Type
14232 or else (Is_Class_Wide_Type
(Act_T
)
14233 and then Ekind
(Root_Type
(Act_T
)) = E_Incomplete_Type
)
14235 -- If the formal is an incomplete type, the actual can be
14236 -- incomplete as well, but if an actual incomplete type has
14237 -- a full view, then we'll retrieve that.
14239 if Ekind
(A_Gen_T
) = E_Incomplete_Type
14240 and then No
(Full_View
(Act_T
))
14244 elsif Is_Class_Wide_Type
(Act_T
)
14245 or else No
(Full_View
(Act_T
))
14247 Error_Msg_N
("premature use of incomplete type", Actual
);
14248 Abandon_Instantiation
(Actual
);
14251 Act_T
:= Full_View
(Act_T
);
14252 Set_Entity
(Actual
, Act_T
);
14254 if Has_Private_Component
(Act_T
) then
14256 ("premature use of type with private component", Actual
);
14260 -- Deal with error of premature use of private type as generic actual,
14261 -- which is allowed for incomplete formals.
14263 elsif Ekind
(A_Gen_T
) /= E_Incomplete_Type
then
14264 if Is_Private_Type
(Act_T
)
14265 and then Is_Private_Type
(Base_Type
(Act_T
))
14266 and then not Is_Generic_Type
(Act_T
)
14267 and then not Is_Derived_Type
(Act_T
)
14268 and then No
(Full_View
(Root_Type
(Act_T
)))
14270 Error_Msg_N
("premature use of private type", Actual
);
14272 elsif Has_Private_Component
(Act_T
) then
14274 ("premature use of type with private component", Actual
);
14278 Set_Instance_Of
(A_Gen_T
, Act_T
);
14280 -- If the type is generic, the class-wide type may also be used
14282 if Is_Tagged_Type
(A_Gen_T
)
14283 and then Is_Tagged_Type
(Act_T
)
14284 and then not Is_Class_Wide_Type
(A_Gen_T
)
14286 Set_Instance_Of
(Class_Wide_Type
(A_Gen_T
),
14287 Class_Wide_Type
(Act_T
));
14290 if not Is_Abstract_Type
(A_Gen_T
)
14291 and then Is_Abstract_Type
(Act_T
)
14294 ("actual of non-abstract formal cannot be abstract", Actual
);
14297 -- A generic scalar type is a first subtype for which we generate
14298 -- an anonymous base type. Indicate that the instance of this base
14299 -- is the base type of the actual.
14301 if Is_Scalar_Type
(A_Gen_T
) then
14302 Set_Instance_Of
(Etype
(A_Gen_T
), Etype
(Act_T
));
14305 Check_Shared_Variable_Control_Aspects
;
14307 if Error_Posted
(Act_T
) then
14310 case Nkind
(Def
) is
14311 when N_Formal_Private_Type_Definition
=>
14312 Validate_Private_Type_Instance
;
14314 when N_Formal_Incomplete_Type_Definition
=>
14315 Validate_Incomplete_Type_Instance
;
14317 when N_Formal_Derived_Type_Definition
=>
14318 Validate_Derived_Type_Instance
;
14320 when N_Formal_Discrete_Type_Definition
=>
14321 if not Is_Discrete_Type
(Act_T
) then
14323 ("expect discrete type in instantiation of&",
14325 Abandon_Instantiation
(Actual
);
14328 Diagnose_Predicated_Actual
;
14330 when N_Formal_Signed_Integer_Type_Definition
=>
14331 if not Is_Signed_Integer_Type
(Act_T
) then
14333 ("expect signed integer type in instantiation of&",
14335 Abandon_Instantiation
(Actual
);
14338 Diagnose_Predicated_Actual
;
14340 when N_Formal_Modular_Type_Definition
=>
14341 if not Is_Modular_Integer_Type
(Act_T
) then
14343 ("expect modular type in instantiation of &",
14345 Abandon_Instantiation
(Actual
);
14348 Diagnose_Predicated_Actual
;
14350 when N_Formal_Floating_Point_Definition
=>
14351 if not Is_Floating_Point_Type
(Act_T
) then
14353 ("expect float type in instantiation of &", Actual
, Gen_T
);
14354 Abandon_Instantiation
(Actual
);
14357 when N_Formal_Ordinary_Fixed_Point_Definition
=>
14358 if not Is_Ordinary_Fixed_Point_Type
(Act_T
) then
14360 ("expect ordinary fixed point type in instantiation of &",
14362 Abandon_Instantiation
(Actual
);
14365 when N_Formal_Decimal_Fixed_Point_Definition
=>
14366 if not Is_Decimal_Fixed_Point_Type
(Act_T
) then
14368 ("expect decimal type in instantiation of &",
14370 Abandon_Instantiation
(Actual
);
14373 when N_Array_Type_Definition
=>
14374 Validate_Array_Type_Instance
;
14376 when N_Access_To_Object_Definition
=>
14377 Validate_Access_Type_Instance
;
14379 when N_Access_Function_Definition
14380 | N_Access_Procedure_Definition
14382 Validate_Access_Subprogram_Instance
;
14384 when N_Record_Definition
=>
14385 Validate_Interface_Type_Instance
;
14387 when N_Derived_Type_Definition
=>
14388 Validate_Derived_Interface_Type_Instance
;
14391 raise Program_Error
;
14395 Subt
:= New_Copy
(Gen_T
);
14397 -- Use adjusted sloc of subtype name as the location for other nodes in
14398 -- the subtype declaration.
14400 Loc
:= Sloc
(Subt
);
14403 Make_Subtype_Declaration
(Loc
,
14404 Defining_Identifier
=> Subt
,
14405 Subtype_Indication
=> New_Occurrence_Of
(Act_T
, Loc
));
14407 Copy_Ghost_Aspect
(Formal
, To
=> Decl_Node
);
14409 -- Record whether the actual is private at this point, so that
14410 -- Check_Generic_Actuals can restore its proper view before the
14411 -- semantic analysis of the instance.
14413 if Is_Private_Type
(Act_T
) then
14414 Set_Has_Private_View
(Subtype_Indication
(Decl_Node
));
14416 elsif (Is_Access_Type
(Act_T
)
14417 and then Is_Private_Type
(Designated_Type
(Act_T
)))
14418 or else (Is_Array_Type
(Act_T
)
14420 Is_Private_Type
(Component_Type_For_Private_View
(Act_T
)))
14422 Set_Has_Secondary_Private_View
(Subtype_Indication
(Decl_Node
));
14425 -- In Ada 2012 the actual may be a limited view. Indicate that
14426 -- the local subtype must be treated as such.
14428 if From_Limited_With
(Act_T
) then
14429 Mutate_Ekind
(Subt
, E_Incomplete_Subtype
);
14430 Set_From_Limited_With
(Subt
);
14433 Decl_Nodes
:= New_List
(Decl_Node
);
14435 -- Flag actual derived types so their elaboration produces the
14436 -- appropriate renamings for the primitive operations of the ancestor.
14437 -- Flag actual for formal private types as well, to determine whether
14438 -- operations in the private part may override inherited operations.
14439 -- If the formal has an interface list, the ancestor is not the
14440 -- parent, but the analyzed formal that includes the interface
14441 -- operations of all its progenitors.
14443 -- Same treatment for formal private types, so we can check whether the
14444 -- type is tagged limited when validating derivations in the private
14445 -- part. (See AI05-096).
14447 if Nkind
(Def
) = N_Formal_Derived_Type_Definition
then
14448 if Present
(Interface_List
(Def
)) then
14449 Set_Generic_Parent_Type
(Decl_Node
, A_Gen_T
);
14451 Set_Generic_Parent_Type
(Decl_Node
, Ancestor
);
14454 elsif Nkind
(Def
) in N_Formal_Private_Type_Definition
14455 | N_Formal_Incomplete_Type_Definition
14457 Set_Generic_Parent_Type
(Decl_Node
, A_Gen_T
);
14460 -- If the actual is a synchronized type that implements an interface,
14461 -- the primitive operations are attached to the corresponding record,
14462 -- and we have to treat it as an additional generic actual, so that its
14463 -- primitive operations become visible in the instance. The task or
14464 -- protected type itself does not carry primitive operations.
14466 if Is_Concurrent_Type
(Act_T
)
14467 and then Is_Tagged_Type
(Act_T
)
14468 and then Present
(Corresponding_Record_Type
(Act_T
))
14469 and then Present
(Ancestor
)
14470 and then Is_Interface
(Ancestor
)
14473 Corr_Rec
: constant Entity_Id
:=
14474 Corresponding_Record_Type
(Act_T
);
14475 New_Corr
: Entity_Id
;
14476 Corr_Decl
: Node_Id
;
14479 New_Corr
:= Make_Temporary
(Loc
, 'S');
14481 Make_Subtype_Declaration
(Loc
,
14482 Defining_Identifier
=> New_Corr
,
14483 Subtype_Indication
=>
14484 New_Occurrence_Of
(Corr_Rec
, Loc
));
14485 Append_To
(Decl_Nodes
, Corr_Decl
);
14487 if Ekind
(Act_T
) = E_Task_Type
then
14488 Mutate_Ekind
(Subt
, E_Task_Subtype
);
14490 Mutate_Ekind
(Subt
, E_Protected_Subtype
);
14493 Set_Corresponding_Record_Type
(Subt
, Corr_Rec
);
14494 Set_Generic_Parent_Type
(Corr_Decl
, Ancestor
);
14495 Set_Generic_Parent_Type
(Decl_Node
, Empty
);
14499 -- For a floating-point type, capture dimension info if any, because
14500 -- the generated subtype declaration does not come from source and
14501 -- will not process dimensions.
14503 if Is_Floating_Point_Type
(Act_T
) then
14504 Copy_Dimensions
(Act_T
, Subt
);
14508 end Instantiate_Type
;
14510 -----------------------------
14511 -- Is_Abbreviated_Instance --
14512 -----------------------------
14514 function Is_Abbreviated_Instance
(E
: Entity_Id
) return Boolean is
14516 return Ekind
(E
) = E_Package
14517 and then Present
(Hidden_In_Formal_Instance
(E
));
14518 end Is_Abbreviated_Instance
;
14520 ---------------------
14521 -- Is_In_Main_Unit --
14522 ---------------------
14524 function Is_In_Main_Unit
(N
: Node_Id
) return Boolean is
14525 Unum
: constant Unit_Number_Type
:= Get_Source_Unit
(N
);
14526 Current_Unit
: Node_Id
;
14529 if Unum
= Main_Unit
then
14532 -- If the current unit is a subunit then it is either the main unit or
14533 -- is being compiled as part of the main unit.
14535 elsif Nkind
(N
) = N_Compilation_Unit
then
14536 return Nkind
(Unit
(N
)) = N_Subunit
;
14539 Current_Unit
:= Parent
(N
);
14540 while Present
(Current_Unit
)
14541 and then Nkind
(Current_Unit
) /= N_Compilation_Unit
14543 Current_Unit
:= Parent
(Current_Unit
);
14546 -- The instantiation node is in the main unit, or else the current node
14547 -- (perhaps as the result of nested instantiations) is in the main unit,
14548 -- or in the declaration of the main unit, which in this last case must
14552 Current_Unit
= Cunit
(Main_Unit
)
14553 or else Current_Unit
= Library_Unit
(Cunit
(Main_Unit
))
14554 or else (Present
(Current_Unit
)
14555 and then Present
(Library_Unit
(Current_Unit
))
14556 and then Is_In_Main_Unit
(Library_Unit
(Current_Unit
)));
14557 end Is_In_Main_Unit
;
14559 ----------------------------
14560 -- Load_Parent_Of_Generic --
14561 ----------------------------
14563 procedure Load_Parent_Of_Generic
14566 Body_Optional
: Boolean := False)
14568 Comp_Unit
: constant Node_Id
:= Cunit
(Get_Source_Unit
(Spec
));
14569 Saved_Style_Check
: constant Boolean := Style_Check
;
14570 Saved_Warn
: constant Warnings_State
:= Save_Warnings
;
14571 True_Parent
: Node_Id
;
14572 Inst_Node
: Node_Id
;
14574 Previous_Instances
: constant Elist_Id
:= New_Elmt_List
;
14576 procedure Collect_Previous_Instances
(Decls
: List_Id
);
14577 -- Collect all instantiations in the given list of declarations, that
14578 -- precede the generic that we need to load. If the bodies of these
14579 -- instantiations are available, we must analyze them, to ensure that
14580 -- the public symbols generated are the same when the unit is compiled
14581 -- to generate code, and when it is compiled in the context of a unit
14582 -- that needs a particular nested instance. This process is applied to
14583 -- both package and subprogram instances.
14585 --------------------------------
14586 -- Collect_Previous_Instances --
14587 --------------------------------
14589 procedure Collect_Previous_Instances
(Decls
: List_Id
) is
14593 Decl
:= First
(Decls
);
14594 while Present
(Decl
) loop
14595 if Sloc
(Decl
) >= Sloc
(Inst_Node
) then
14598 -- If Decl is an instantiation, then record it as requiring
14599 -- instantiation of the corresponding body, except if it is an
14600 -- abbreviated instantiation generated internally for conformance
14601 -- checking purposes only for the case of a formal package
14602 -- declared without a box (see Instantiate_Formal_Package). Such
14603 -- an instantiation does not generate any code (the actual code
14604 -- comes from actual) and thus does not need to be analyzed here.
14605 -- If the instantiation appears with a generic package body it is
14606 -- not analyzed here either.
14608 elsif Nkind
(Decl
) = N_Package_Instantiation
14609 and then not Is_Abbreviated_Instance
(Defining_Entity
(Decl
))
14611 Append_Elmt
(Decl
, Previous_Instances
);
14613 -- For a subprogram instantiation, omit instantiations intrinsic
14614 -- operations (Unchecked_Conversions, etc.) that have no bodies.
14616 elsif Nkind
(Decl
) in N_Function_Instantiation
14617 | N_Procedure_Instantiation
14618 and then not Is_Intrinsic_Subprogram
(Entity
(Name
(Decl
)))
14620 Append_Elmt
(Decl
, Previous_Instances
);
14622 elsif Nkind
(Decl
) = N_Package_Declaration
then
14623 Collect_Previous_Instances
14624 (Visible_Declarations
(Specification
(Decl
)));
14625 Collect_Previous_Instances
14626 (Private_Declarations
(Specification
(Decl
)));
14628 -- Previous non-generic bodies may contain instances as well
14630 elsif Nkind
(Decl
) = N_Package_Body
14631 and then Ekind
(Corresponding_Spec
(Decl
)) /= E_Generic_Package
14633 Collect_Previous_Instances
(Declarations
(Decl
));
14635 elsif Nkind
(Decl
) = N_Subprogram_Body
14636 and then not Acts_As_Spec
(Decl
)
14637 and then not Is_Generic_Subprogram
(Corresponding_Spec
(Decl
))
14639 Collect_Previous_Instances
(Declarations
(Decl
));
14644 end Collect_Previous_Instances
;
14646 -- Start of processing for Load_Parent_Of_Generic
14649 if not In_Same_Source_Unit
(N
, Spec
)
14650 or else Nkind
(Unit
(Comp_Unit
)) = N_Package_Declaration
14651 or else (Nkind
(Unit
(Comp_Unit
)) = N_Package_Body
14652 and then not Is_In_Main_Unit
(Spec
))
14654 -- Find body of parent of spec, and analyze it. A special case arises
14655 -- when the parent is an instantiation, that is to say when we are
14656 -- currently instantiating a nested generic. In that case, there is
14657 -- no separate file for the body of the enclosing instance. Instead,
14658 -- the enclosing body must be instantiated as if it were a pending
14659 -- instantiation, in order to produce the body for the nested generic
14660 -- we require now. Note that in that case the generic may be defined
14661 -- in a package body, the instance defined in the same package body,
14662 -- and the original enclosing body may not be in the main unit.
14664 Inst_Node
:= Empty
;
14666 True_Parent
:= Parent
(Spec
);
14667 while Present
(True_Parent
)
14668 and then Nkind
(True_Parent
) /= N_Compilation_Unit
14670 if Nkind
(True_Parent
) = N_Package_Declaration
14672 Nkind
(Original_Node
(True_Parent
)) = N_Package_Instantiation
14674 -- Parent is a compilation unit that is an instantiation, and
14675 -- instantiation node has been replaced with package decl.
14677 Inst_Node
:= Original_Node
(True_Parent
);
14680 elsif Nkind
(True_Parent
) = N_Package_Declaration
14681 and then Nkind
(Parent
(True_Parent
)) = N_Compilation_Unit
14683 Nkind
(Unit
(Parent
(True_Parent
))) = N_Package_Instantiation
14685 -- Parent is a compilation unit that is an instantiation, but
14686 -- instantiation node has not been replaced with package decl.
14688 Inst_Node
:= Unit
(Parent
(True_Parent
));
14691 elsif Nkind
(True_Parent
) = N_Package_Declaration
14692 and then Nkind
(Parent
(True_Parent
)) /= N_Compilation_Unit
14693 and then Present
(Generic_Parent
(Specification
(True_Parent
)))
14695 -- Parent is an instantiation within another specification.
14696 -- Declaration for instance has been inserted before original
14697 -- instantiation node. A direct link would be preferable?
14699 Inst_Node
:= Next
(True_Parent
);
14700 while Present
(Inst_Node
)
14701 and then Nkind
(Inst_Node
) /= N_Package_Instantiation
14706 -- If the instance appears within a generic, and the generic
14707 -- unit is defined within a formal package of the enclosing
14708 -- generic, there is no generic body available, and none
14709 -- needed. A more precise test should be used ???
14711 if No
(Inst_Node
) then
14717 -- If an ancestor of the generic comes from a formal package
14718 -- there is no source for the ancestor body. This is detected
14719 -- by examining the scope of the ancestor and its declaration.
14720 -- The body, if any is needed, will be available when the
14721 -- current unit (containing a formal package) is instantiated.
14723 elsif Nkind
(True_Parent
) = N_Package_Specification
14724 and then Present
(Generic_Parent
(True_Parent
))
14726 (Original_Node
(Unit_Declaration_Node
14727 (Scope
(Generic_Parent
(True_Parent
)))))
14728 = N_Formal_Package_Declaration
14733 True_Parent
:= Parent
(True_Parent
);
14737 -- Case where we are currently instantiating a nested generic
14739 if Present
(Inst_Node
) then
14740 if Nkind
(Parent
(True_Parent
)) = N_Compilation_Unit
then
14742 -- Instantiation node and declaration of instantiated package
14743 -- were exchanged when only the declaration was needed.
14744 -- Restore instantiation node before proceeding with body.
14746 Set_Unit
(Parent
(True_Parent
), Inst_Node
);
14749 -- Now complete instantiation of enclosing body, if it appears in
14750 -- some other unit. If it appears in the current unit, the body
14751 -- will have been instantiated already.
14753 if No
(Corresponding_Body
(Instance_Spec
(Inst_Node
))) then
14755 -- We need to determine the expander mode to instantiate the
14756 -- enclosing body. Because the generic body we need may use
14757 -- global entities declared in the enclosing package (including
14758 -- aggregates) it is in general necessary to compile this body
14759 -- with expansion enabled, except if we are within a generic
14760 -- package, in which case the usual generic rule applies.
14763 Exp_Status
: Boolean := True;
14767 -- Loop through scopes looking for generic package
14769 Scop
:= Scope
(Defining_Entity
(Instance_Spec
(Inst_Node
)));
14770 while Present
(Scop
)
14771 and then Scop
/= Standard_Standard
14773 if Ekind
(Scop
) = E_Generic_Package
then
14774 Exp_Status
:= False;
14778 Scop
:= Scope
(Scop
);
14781 -- Collect previous instantiations in the unit that contains
14782 -- the desired generic.
14784 if Nkind
(Parent
(True_Parent
)) /= N_Compilation_Unit
14785 and then not Body_Optional
14789 Info
: Pending_Body_Info
;
14793 Par
:= Parent
(Inst_Node
);
14794 while Present
(Par
) loop
14795 exit when Nkind
(Parent
(Par
)) = N_Compilation_Unit
;
14796 Par
:= Parent
(Par
);
14799 pragma Assert
(Present
(Par
));
14801 if Nkind
(Par
) = N_Package_Body
then
14802 Collect_Previous_Instances
(Declarations
(Par
));
14804 elsif Nkind
(Par
) = N_Package_Declaration
then
14805 Collect_Previous_Instances
14806 (Visible_Declarations
(Specification
(Par
)));
14807 Collect_Previous_Instances
14808 (Private_Declarations
(Specification
(Par
)));
14811 -- Enclosing unit is a subprogram body. In this
14812 -- case all instance bodies are processed in order
14813 -- and there is no need to collect them separately.
14818 Decl
:= First_Elmt
(Previous_Instances
);
14819 while Present
(Decl
) loop
14821 (Inst_Node
=> Node
(Decl
),
14823 Instance_Spec
(Node
(Decl
)),
14825 Config_Switches
=> Save_Config_Switches
,
14826 Current_Sem_Unit
=>
14827 Get_Code_Unit
(Sloc
(Node
(Decl
))),
14828 Expander_Status
=> Exp_Status
,
14829 Local_Suppress_Stack_Top
=>
14830 Local_Suppress_Stack_Top
,
14831 Scope_Suppress
=> Scope_Suppress
,
14832 Warnings
=> Save_Warnings
);
14834 -- Package instance
14836 if Nkind
(Node
(Decl
)) = N_Package_Instantiation
14838 Instantiate_Package_Body
14839 (Info
, Body_Optional
=> True);
14841 -- Subprogram instance
14844 -- The instance_spec is in the wrapper package,
14845 -- usually followed by its local renaming
14846 -- declaration. See Build_Subprogram_Renaming
14847 -- for details. If the instance carries aspects,
14848 -- these result in the corresponding pragmas,
14849 -- inserted after the subprogram declaration.
14850 -- They must be skipped as well when retrieving
14851 -- the desired spec. Some of them may have been
14852 -- rewritten as null statements.
14853 -- A direct link would be more robust ???
14857 (Last
(Visible_Declarations
14858 (Specification
(Info
.Act_Decl
))));
14860 while Nkind
(Decl
) in
14863 N_Subprogram_Renaming_Declaration
14865 Decl
:= Prev
(Decl
);
14868 Info
.Act_Decl
:= Decl
;
14871 Instantiate_Subprogram_Body
14872 (Info
, Body_Optional
=> True);
14880 Instantiate_Package_Body
14882 ((Inst_Node
=> Inst_Node
,
14883 Act_Decl
=> True_Parent
,
14885 Config_Switches
=> Save_Config_Switches
,
14886 Current_Sem_Unit
=>
14887 Get_Code_Unit
(Sloc
(Inst_Node
)),
14888 Expander_Status
=> Exp_Status
,
14889 Local_Suppress_Stack_Top
=> Local_Suppress_Stack_Top
,
14890 Scope_Suppress
=> Scope_Suppress
,
14891 Warnings
=> Save_Warnings
)),
14892 Body_Optional
=> Body_Optional
);
14896 -- Case where we are not instantiating a nested generic
14899 Opt
.Style_Check
:= False;
14900 Expander_Mode_Save_And_Set
(True);
14901 Load_Needed_Body
(Comp_Unit
, OK
);
14902 Opt
.Style_Check
:= Saved_Style_Check
;
14903 Restore_Warnings
(Saved_Warn
);
14904 Expander_Mode_Restore
;
14907 and then Unit_Requires_Body
(Defining_Entity
(Spec
))
14908 and then not Body_Optional
14911 Bname
: constant Unit_Name_Type
:=
14912 Get_Body_Name
(Get_Unit_Name
(Unit
(Comp_Unit
)));
14915 -- In CodePeer mode, the missing body may make the analysis
14916 -- incomplete, but we do not treat it as fatal.
14918 if CodePeer_Mode
then
14922 Error_Msg_Unit_1
:= Bname
;
14923 Error_Msg_N
("this instantiation requires$!", N
);
14924 Error_Msg_File_1
:=
14925 Get_File_Name
(Bname
, Subunit
=> False);
14926 Error_Msg_N
("\but file{ was not found!", N
);
14927 raise Unrecoverable_Error
;
14934 -- If loading parent of the generic caused an instantiation circularity,
14935 -- we abandon compilation at this point, because otherwise in some cases
14936 -- we get into trouble with infinite recursions after this point.
14938 if Circularity_Detected
then
14939 raise Unrecoverable_Error
;
14941 end Load_Parent_Of_Generic
;
14943 ---------------------------------
14944 -- Map_Formal_Package_Entities --
14945 ---------------------------------
14947 procedure Map_Formal_Package_Entities
(Form
: Entity_Id
; Act
: Entity_Id
) is
14952 Set_Instance_Of
(Form
, Act
);
14954 -- Traverse formal and actual package to map the corresponding entities.
14955 -- We skip over internal entities that may be generated during semantic
14956 -- analysis, and find the matching entities by name, given that they
14957 -- must appear in the same order.
14959 E1
:= First_Entity
(Form
);
14960 E2
:= First_Entity
(Act
);
14961 while Present
(E1
) and then E1
/= First_Private_Entity
(Form
) loop
14962 -- Could this test be a single condition??? Seems like it could, and
14963 -- isn't FPE (Form) a constant anyway???
14965 if not Is_Internal
(E1
)
14966 and then Present
(Parent
(E1
))
14967 and then not Is_Class_Wide_Type
(E1
)
14968 and then not Is_Internal_Name
(Chars
(E1
))
14970 while Present
(E2
) and then Chars
(E2
) /= Chars
(E1
) loop
14977 Set_Instance_Of
(E1
, E2
);
14979 if Is_Type
(E1
) and then Is_Tagged_Type
(E2
) then
14980 Set_Instance_Of
(Class_Wide_Type
(E1
), Class_Wide_Type
(E2
));
14983 if Is_Constrained
(E1
) then
14984 Set_Instance_Of
(Base_Type
(E1
), Base_Type
(E2
));
14987 if Ekind
(E1
) = E_Package
and then No
(Renamed_Entity
(E1
)) then
14988 Map_Formal_Package_Entities
(E1
, E2
);
14995 end Map_Formal_Package_Entities
;
14997 -----------------------
14998 -- Move_Freeze_Nodes --
14999 -----------------------
15001 procedure Move_Freeze_Nodes
15002 (Out_Of
: Entity_Id
;
15007 Next_Decl
: Node_Id
;
15008 Next_Node
: Node_Id
:= After
;
15011 function Is_Outer_Type
(T
: Entity_Id
) return Boolean;
15012 -- Check whether entity is declared in a scope external to that of the
15015 -------------------
15016 -- Is_Outer_Type --
15017 -------------------
15019 function Is_Outer_Type
(T
: Entity_Id
) return Boolean is
15020 Scop
: Entity_Id
:= Scope
(T
);
15023 if Scope_Depth
(Scop
) < Scope_Depth
(Out_Of
) then
15027 while Scop
/= Standard_Standard
loop
15028 if Scop
= Out_Of
then
15031 Scop
:= Scope
(Scop
);
15039 -- Start of processing for Move_Freeze_Nodes
15046 -- First remove the freeze nodes that may appear before all other
15050 while Present
(Decl
)
15051 and then Nkind
(Decl
) = N_Freeze_Entity
15052 and then Is_Outer_Type
(Entity
(Decl
))
15054 Decl
:= Remove_Head
(L
);
15055 Insert_After
(Next_Node
, Decl
);
15056 Set_Analyzed
(Decl
, False);
15061 -- Next scan the list of declarations and remove each freeze node that
15062 -- appears ahead of the current node.
15064 while Present
(Decl
) loop
15065 while Present
(Next
(Decl
))
15066 and then Nkind
(Next
(Decl
)) = N_Freeze_Entity
15067 and then Is_Outer_Type
(Entity
(Next
(Decl
)))
15069 Next_Decl
:= Remove_Next
(Decl
);
15070 Insert_After
(Next_Node
, Next_Decl
);
15071 Set_Analyzed
(Next_Decl
, False);
15072 Next_Node
:= Next_Decl
;
15075 -- If the declaration is a nested package or concurrent type, then
15076 -- recurse. Nested generic packages will have been processed from the
15079 case Nkind
(Decl
) is
15080 when N_Package_Declaration
=>
15081 Spec
:= Specification
(Decl
);
15083 when N_Task_Type_Declaration
=>
15084 Spec
:= Task_Definition
(Decl
);
15086 when N_Protected_Type_Declaration
=>
15087 Spec
:= Protected_Definition
(Decl
);
15093 if Present
(Spec
) then
15094 Move_Freeze_Nodes
(Out_Of
, Next_Node
, Visible_Declarations
(Spec
));
15095 Move_Freeze_Nodes
(Out_Of
, Next_Node
, Private_Declarations
(Spec
));
15100 end Move_Freeze_Nodes
;
15106 function Next_Assoc
(E
: Assoc_Ptr
) return Assoc_Ptr
is
15108 return Generic_Renamings
.Table
(E
).Next_In_HTable
;
15111 ------------------------
15112 -- Preanalyze_Actuals --
15113 ------------------------
15115 procedure Preanalyze_Actuals
(N
: Node_Id
; Inst
: Entity_Id
:= Empty
) is
15116 procedure Perform_Appropriate_Analysis
(N
: Node_Id
);
15117 -- Determine if the actuals we are analyzing come from a generic
15118 -- instantiation that is a library unit and dispatch accordingly.
15120 ----------------------------------
15121 -- Perform_Appropriate_Analysis --
15122 ----------------------------------
15124 procedure Perform_Appropriate_Analysis
(N
: Node_Id
) is
15126 -- When we have a library instantiation we cannot allow any expansion
15127 -- to occur, since there may be no place to put it. Instead, in that
15128 -- case we perform a preanalysis of the actual.
15130 if Present
(Inst
) and then Is_Compilation_Unit
(Inst
) then
15135 end Perform_Appropriate_Analysis
;
15139 Errs
: constant Nat
:= Serious_Errors_Detected
;
15144 Cur
: Entity_Id
:= Empty
;
15145 -- Current homograph of the instance name
15147 Vis
: Boolean := False;
15148 -- Saved visibility status of the current homograph
15150 -- Start of processing for Preanalyze_Actuals
15153 Assoc
:= First
(Generic_Associations
(N
));
15155 -- If the instance is a child unit, its name may hide an outer homonym,
15156 -- so make it invisible to perform name resolution on the actuals.
15158 if Nkind
(Defining_Unit_Name
(N
)) = N_Defining_Program_Unit_Name
15160 (Current_Entity
(Defining_Identifier
(Defining_Unit_Name
(N
))))
15162 Cur
:= Current_Entity
(Defining_Identifier
(Defining_Unit_Name
(N
)));
15164 if Is_Compilation_Unit
(Cur
) then
15165 Vis
:= Is_Immediately_Visible
(Cur
);
15166 Set_Is_Immediately_Visible
(Cur
, False);
15172 while Present
(Assoc
) loop
15173 if Nkind
(Assoc
) /= N_Others_Choice
then
15174 Act
:= Explicit_Generic_Actual_Parameter
(Assoc
);
15176 -- Within a nested instantiation, a defaulted actual is an empty
15177 -- association, so nothing to analyze. If the subprogram actual
15178 -- is an attribute, analyze prefix only, because actual is not a
15179 -- complete attribute reference.
15181 -- If actual is an allocator, analyze expression only. The full
15182 -- analysis can generate code, and if instance is a compilation
15183 -- unit we have to wait until the package instance is installed
15184 -- to have a proper place to insert this code.
15186 -- String literals may be operators, but at this point we do not
15187 -- know whether the actual is a formal subprogram or a string.
15192 elsif Nkind
(Act
) = N_Attribute_Reference
then
15193 Perform_Appropriate_Analysis
(Prefix
(Act
));
15195 elsif Nkind
(Act
) = N_Explicit_Dereference
then
15196 Perform_Appropriate_Analysis
(Prefix
(Act
));
15198 elsif Nkind
(Act
) = N_Allocator
then
15200 Expr
: constant Node_Id
:= Expression
(Act
);
15203 if Nkind
(Expr
) = N_Subtype_Indication
then
15204 Perform_Appropriate_Analysis
(Subtype_Mark
(Expr
));
15206 -- Analyze separately each discriminant constraint, when
15207 -- given with a named association.
15213 Constr
:= First
(Constraints
(Constraint
(Expr
)));
15214 while Present
(Constr
) loop
15215 if Nkind
(Constr
) = N_Discriminant_Association
then
15216 Perform_Appropriate_Analysis
15217 (Expression
(Constr
));
15219 Perform_Appropriate_Analysis
(Constr
);
15227 Perform_Appropriate_Analysis
(Expr
);
15231 elsif Nkind
(Act
) /= N_Operator_Symbol
then
15232 Perform_Appropriate_Analysis
(Act
);
15234 -- Within a package instance, mark actuals that are limited
15235 -- views, so their use can be moved to the body of the
15238 if Is_Entity_Name
(Act
)
15239 and then Is_Type
(Entity
(Act
))
15240 and then From_Limited_With
(Entity
(Act
))
15241 and then Present
(Inst
)
15243 Append_Elmt
(Entity
(Act
), Incomplete_Actuals
(Inst
));
15247 if Errs
/= Serious_Errors_Detected
then
15249 -- Do a minimal analysis of the generic, to prevent spurious
15250 -- warnings complaining about the generic being unreferenced,
15251 -- before abandoning the instantiation.
15253 Perform_Appropriate_Analysis
(Name
(N
));
15255 if Is_Entity_Name
(Name
(N
))
15256 and then Etype
(Name
(N
)) /= Any_Type
15258 Generate_Reference
(Entity
(Name
(N
)), Name
(N
));
15259 Set_Is_Instantiated
(Entity
(Name
(N
)));
15262 if Present
(Cur
) then
15264 -- For the case of a child instance hiding an outer homonym,
15265 -- provide additional warning which might explain the error.
15267 Set_Is_Immediately_Visible
(Cur
, Vis
);
15269 ("& hides outer unit with the same name??",
15270 N
, Defining_Unit_Name
(N
));
15273 Abandon_Instantiation
(Act
);
15280 if Present
(Cur
) then
15281 Set_Is_Immediately_Visible
(Cur
, Vis
);
15283 end Preanalyze_Actuals
;
15285 -------------------------------
15286 -- Provide_Completing_Bodies --
15287 -------------------------------
15289 procedure Provide_Completing_Bodies
(N
: Node_Id
) is
15290 procedure Build_Completing_Body
(Subp_Decl
: Node_Id
);
15291 -- Generate the completing body for subprogram declaration Subp_Decl
15293 procedure Provide_Completing_Bodies_In
(Decls
: List_Id
);
15294 -- Generating completing bodies for all subprograms found in declarative
15297 ---------------------------
15298 -- Build_Completing_Body --
15299 ---------------------------
15301 procedure Build_Completing_Body
(Subp_Decl
: Node_Id
) is
15302 Loc
: constant Source_Ptr
:= Sloc
(Subp_Decl
);
15303 Subp_Id
: constant Entity_Id
:= Defining_Entity
(Subp_Decl
);
15307 -- Nothing to do if the subprogram already has a completing body
15309 if Present
(Corresponding_Body
(Subp_Decl
)) then
15312 -- Mark the function as having a valid return statement even though
15313 -- the body contains a single raise statement.
15315 elsif Ekind
(Subp_Id
) = E_Function
then
15316 Set_Return_Present
(Subp_Id
);
15319 -- Clone the specification to obtain new entities and reset the only
15322 Spec
:= Copy_Subprogram_Spec
(Specification
(Subp_Decl
));
15323 Set_Generic_Parent
(Spec
, Empty
);
15326 -- function Func ... return ... is
15328 -- procedure Proc ... is
15330 -- raise Program_Error with "access before elaboration";
15333 Insert_After_And_Analyze
(Subp_Decl
,
15334 Make_Subprogram_Body
(Loc
,
15335 Specification
=> Spec
,
15336 Declarations
=> New_List
,
15337 Handled_Statement_Sequence
=>
15338 Make_Handled_Sequence_Of_Statements
(Loc
,
15339 Statements
=> New_List
(
15340 Make_Raise_Program_Error
(Loc
,
15341 Reason
=> PE_Access_Before_Elaboration
)))));
15342 end Build_Completing_Body
;
15344 ----------------------------------
15345 -- Provide_Completing_Bodies_In --
15346 ----------------------------------
15348 procedure Provide_Completing_Bodies_In
(Decls
: List_Id
) is
15352 if Present
(Decls
) then
15353 Decl
:= First
(Decls
);
15354 while Present
(Decl
) loop
15355 Provide_Completing_Bodies
(Decl
);
15359 end Provide_Completing_Bodies_In
;
15365 -- Start of processing for Provide_Completing_Bodies
15368 if Nkind
(N
) = N_Package_Declaration
then
15369 Spec
:= Specification
(N
);
15371 Push_Scope
(Defining_Entity
(N
));
15372 Provide_Completing_Bodies_In
(Visible_Declarations
(Spec
));
15373 Provide_Completing_Bodies_In
(Private_Declarations
(Spec
));
15376 elsif Nkind
(N
) = N_Subprogram_Declaration
then
15377 Build_Completing_Body
(N
);
15379 end Provide_Completing_Bodies
;
15381 -------------------
15382 -- Remove_Parent --
15383 -------------------
15385 procedure Remove_Parent
(In_Body
: Boolean := False) is
15386 S
: Entity_Id
:= Current_Scope
;
15387 -- S is the scope containing the instantiation just completed. The scope
15388 -- stack contains the parent instances of the instantiation, followed by
15397 -- After child instantiation is complete, remove from scope stack the
15398 -- extra copy of the current scope, and then remove parent instances.
15400 if not In_Body
then
15403 while Current_Scope
/= S
loop
15404 P
:= Current_Scope
;
15405 End_Package_Scope
(Current_Scope
);
15407 if In_Open_Scopes
(P
) then
15408 E
:= First_Entity
(P
);
15409 while Present
(E
) loop
15410 Set_Is_Immediately_Visible
(E
, True);
15414 -- If instantiation is declared in a block, it is the enclosing
15415 -- scope that might be a parent instance. Note that only one
15416 -- block can be involved, because the parent instances have
15417 -- been installed within it.
15419 if Ekind
(P
) = E_Block
then
15420 Cur_P
:= Scope
(P
);
15425 if Is_Generic_Instance
(Cur_P
) and then P
/= Current_Scope
then
15426 -- We are within an instance of some sibling. Retain
15427 -- visibility of parent, for proper subsequent cleanup, and
15428 -- reinstall private declarations as well.
15430 Set_In_Private_Part
(P
);
15431 Install_Private_Declarations
(P
);
15434 -- If the ultimate parent is a top-level unit recorded in
15435 -- Instance_Parent_Unit, then reset its visibility to what it was
15436 -- before instantiation. (It's not clear what the purpose is of
15437 -- testing whether Scope (P) is In_Open_Scopes, but that test was
15438 -- present before the ultimate parent test was added.???)
15440 elsif not In_Open_Scopes
(Scope
(P
))
15441 or else (P
= Instance_Parent_Unit
15442 and then not Parent_Unit_Visible
)
15444 Set_Is_Immediately_Visible
(P
, False);
15446 -- If the current scope is itself an instantiation of a generic
15447 -- nested within P, and we are in the private part of body of this
15448 -- instantiation, restore the full views of P, that were removed
15449 -- in End_Package_Scope above. This obscure case can occur when a
15450 -- subunit of a generic contains an instance of a child unit of
15451 -- its generic parent unit.
15453 elsif S
= Current_Scope
and then Is_Generic_Instance
(S
)
15454 and then (In_Package_Body
(S
) or else In_Private_Part
(S
))
15457 Par
: constant Entity_Id
:=
15458 Generic_Parent
(Package_Specification
(S
));
15461 and then P
= Scope
(Par
)
15463 Set_In_Private_Part
(P
);
15464 Install_Private_Declarations
(P
);
15470 -- Reset visibility of entities in the enclosing scope
15472 Set_Is_Hidden_Open_Scope
(Current_Scope
, False);
15474 Hidden
:= First_Elmt
(Hidden_Entities
);
15475 while Present
(Hidden
) loop
15476 Set_Is_Immediately_Visible
(Node
(Hidden
), True);
15477 Next_Elmt
(Hidden
);
15481 -- Each body is analyzed separately, and there is no context that
15482 -- needs preserving from one body instance to the next, so remove all
15483 -- parent scopes that have been installed.
15485 while Present
(S
) loop
15486 End_Package_Scope
(S
);
15487 Set_Is_Immediately_Visible
(S
, False);
15488 S
:= Current_Scope
;
15489 exit when S
= Standard_Standard
;
15494 -----------------------------------
15495 -- Requires_Conformance_Checking --
15496 -----------------------------------
15498 function Requires_Conformance_Checking
(N
: Node_Id
) return Boolean is
15500 -- No conformance checking required if the generic actual part is empty,
15501 -- or is a box or an others_clause (necessarily with a box).
15503 return Present
(Generic_Associations
(N
))
15504 and then not Box_Present
(N
)
15505 and then Nkind
(First
(Generic_Associations
(N
))) /= N_Others_Choice
;
15506 end Requires_Conformance_Checking
;
15512 procedure Restore_Env
is
15513 Saved
: Instance_Env
renames Instance_Envs
.Table
(Instance_Envs
.Last
);
15516 if No
(Current_Instantiated_Parent
.Act_Id
) then
15517 -- Restore environment after subprogram inlining
15519 Restore_Private_Views
(Empty
);
15522 Current_Instantiated_Parent
:= Saved
.Instantiated_Parent
;
15523 Exchanged_Views
:= Saved
.Exchanged_Views
;
15524 Hidden_Entities
:= Saved
.Hidden_Entities
;
15525 Current_Sem_Unit
:= Saved
.Current_Sem_Unit
;
15526 Parent_Unit_Visible
:= Saved
.Parent_Unit_Visible
;
15527 Instance_Parent_Unit
:= Saved
.Instance_Parent_Unit
;
15529 Restore_Config_Switches
(Saved
.Switches
);
15531 Instance_Envs
.Decrement_Last
;
15534 ---------------------------
15535 -- Restore_Private_Views --
15536 ---------------------------
15538 procedure Restore_Private_Views
15539 (Pack_Id
: Entity_Id
;
15540 Is_Package
: Boolean := True)
15545 Dep_Elmt
: Elmt_Id
;
15548 procedure Restore_Nested_Formal
(Formal
: Entity_Id
);
15549 -- Hide the generic formals of formal packages declared with box which
15550 -- were reachable in the current instantiation.
15552 ---------------------------
15553 -- Restore_Nested_Formal --
15554 ---------------------------
15556 procedure Restore_Nested_Formal
(Formal
: Entity_Id
) is
15557 pragma Assert
(Ekind
(Formal
) = E_Package
);
15560 if Present
(Renamed_Entity
(Formal
))
15561 and then Denotes_Formal_Package
(Renamed_Entity
(Formal
), True)
15565 elsif Present
(Associated_Formal_Package
(Formal
)) then
15566 Ent
:= First_Entity
(Formal
);
15567 while Present
(Ent
) loop
15568 exit when Ekind
(Ent
) = E_Package
15569 and then Renamed_Entity
(Ent
) = Renamed_Entity
(Formal
);
15571 Set_Is_Hidden
(Ent
);
15572 Set_Is_Potentially_Use_Visible
(Ent
, False);
15574 -- If package, then recurse
15576 if Ekind
(Ent
) = E_Package
then
15577 Restore_Nested_Formal
(Ent
);
15583 end Restore_Nested_Formal
;
15585 -- Start of processing for Restore_Private_Views
15588 M
:= First_Elmt
(Exchanged_Views
);
15589 while Present
(M
) loop
15592 -- Subtypes of types whose views have been exchanged, and that are
15593 -- defined within the instance, were not on the Private_Dependents
15594 -- list on entry to the instance, so they have to be exchanged
15595 -- explicitly now, in order to remain consistent with the view of the
15598 if Ekind
(Typ
) in E_Private_Type
15599 | E_Limited_Private_Type
15600 | E_Record_Type_With_Private
15602 Dep_Elmt
:= First_Elmt
(Private_Dependents
(Typ
));
15603 while Present
(Dep_Elmt
) loop
15604 Dep_Typ
:= Node
(Dep_Elmt
);
15606 if Scope
(Dep_Typ
) = Pack_Id
15607 and then Present
(Full_View
(Dep_Typ
))
15609 Replace_Elmt
(Dep_Elmt
, Full_View
(Dep_Typ
));
15610 Exchange_Declarations
(Dep_Typ
);
15613 Next_Elmt
(Dep_Elmt
);
15617 Exchange_Declarations
(Typ
);
15621 if No
(Pack_Id
) then
15625 -- Make the generic formal parameters private, and make the formal types
15626 -- into subtypes of the actuals again.
15628 E
:= First_Entity
(Pack_Id
);
15629 while Present
(E
) loop
15630 Set_Is_Hidden
(E
, True);
15633 and then Nkind
(Parent
(E
)) = N_Subtype_Declaration
15635 -- Always preserve the flag Is_Generic_Actual_Type for GNATprove,
15636 -- as it is needed to identify the subtype with the type it
15637 -- renames, when there are conversions between access types
15640 if GNATprove_Mode
then
15643 -- If the actual for E is itself a generic actual type from
15644 -- an enclosing instance, E is still a generic actual type
15645 -- outside of the current instance. This matter when resolving
15646 -- an overloaded call that may be ambiguous in the enclosing
15647 -- instance, when two of its actuals coincide.
15649 elsif Is_Entity_Name
(Subtype_Indication
(Parent
(E
)))
15650 and then Is_Generic_Actual_Type
15651 (Entity
(Subtype_Indication
(Parent
(E
))))
15655 Set_Is_Generic_Actual_Type
(E
, False);
15657 -- It might seem reasonable to clear the Is_Generic_Actual_Type
15658 -- flag also on the Full_View if the type is private, since it
15659 -- was set also on this Full_View. However, this flag is relied
15660 -- upon by Covers to spot "types exported from instantiations"
15661 -- which are implicit Full_Views built for instantiations made
15662 -- on private types and we get type mismatches if we do it when
15663 -- the block exchanging the declarations below triggers ???
15665 -- if Is_Private_Type (E) and then Present (Full_View (E)) then
15666 -- Set_Is_Generic_Actual_Type (Full_View (E), False);
15670 -- An unusual case of aliasing: the actual may also be directly
15671 -- visible in the generic, and be private there, while it is fully
15672 -- visible in the context of the instance. The internal subtype
15673 -- is private in the instance but has full visibility like its
15674 -- parent in the enclosing scope. This enforces the invariant that
15675 -- the privacy status of all private dependents of a type coincide
15676 -- with that of the parent type. This can only happen when a
15677 -- generic child unit is instantiated within a sibling.
15679 if Is_Private_Type
(E
)
15680 and then not Is_Private_Type
(Etype
(E
))
15682 Exchange_Declarations
(E
);
15685 elsif Ekind
(E
) = E_Package
then
15687 -- The end of the renaming list is the renaming of the generic
15688 -- package itself. If the instance is a subprogram, all entities
15689 -- in the corresponding package are renamings. If this entity is
15690 -- a formal package, make its own formals private as well. The
15691 -- actual in this case is itself the renaming of an instantiation.
15692 -- If the entity is not a package renaming, it is the entity
15693 -- created to validate formal package actuals: ignore it.
15695 -- If the actual is itself a formal package for the enclosing
15696 -- generic, or the actual for such a formal package, it remains
15697 -- visible on exit from the instance, and therefore nothing needs
15698 -- to be done either, except to keep it accessible.
15700 if Is_Package
and then Renamed_Entity
(E
) = Pack_Id
then
15703 elsif Nkind
(Parent
(E
)) /= N_Package_Renaming_Declaration
then
15707 Denotes_Formal_Package
(Renamed_Entity
(E
), True, Pack_Id
)
15709 Set_Is_Hidden
(E
, False);
15713 Act_P
: constant Entity_Id
:= Renamed_Entity
(E
);
15717 Id
:= First_Entity
(Act_P
);
15719 and then Id
/= First_Private_Entity
(Act_P
)
15721 exit when Ekind
(Id
) = E_Package
15722 and then Renamed_Entity
(Id
) = Act_P
;
15724 Set_Is_Hidden
(Id
, True);
15725 Set_Is_Potentially_Use_Visible
(Id
, In_Use
(Act_P
));
15727 if Ekind
(Id
) = E_Package
then
15728 Restore_Nested_Formal
(Id
);
15739 end Restore_Private_Views
;
15746 (Gen_Unit
: Entity_Id
;
15747 Act_Unit
: Entity_Id
)
15751 Set_Instance_Env
(Gen_Unit
, Act_Unit
);
15754 ----------------------------
15755 -- Save_Global_References --
15756 ----------------------------
15758 procedure Save_Global_References
(Templ
: Node_Id
) is
15760 -- ??? it is horrible to use global variables in highly recursive code
15763 -- The entity of the current associated node
15765 Gen_Scope
: Entity_Id
;
15766 -- The scope of the generic for which references are being saved
15769 -- The current associated node
15771 function Is_Global
(E
: Entity_Id
) return Boolean;
15772 -- Check whether entity is defined outside of generic unit. Examine the
15773 -- scope of an entity, and the scope of the scope, etc, until we find
15774 -- either Standard, in which case the entity is global, or the generic
15775 -- unit itself, which indicates that the entity is local. If the entity
15776 -- is the generic unit itself, as in the case of a recursive call, or
15777 -- the enclosing generic unit, if different from the current scope, then
15778 -- it is local as well, because it will be replaced at the point of
15779 -- instantiation. On the other hand, if it is a reference to a child
15780 -- unit of a common ancestor, which appears in an instantiation, it is
15781 -- global because it is used to denote a specific compilation unit at
15782 -- the time the instantiations will be analyzed.
15784 procedure Qualify_Universal_Operands
15786 Func_Call
: Node_Id
);
15787 -- Op denotes a binary or unary operator in generic template Templ. Node
15788 -- Func_Call is the function call alternative of the operator within the
15789 -- the analyzed copy of the template. Change each operand which yields a
15790 -- universal type by wrapping it into a qualified expression
15792 -- Actual_Typ'(Operand)
15794 -- where Actual_Typ is the type of corresponding actual parameter of
15795 -- Operand in Func_Call.
15797 procedure Reset_Entity
(N
: Node_Id
);
15798 -- Save semantic information on global entity so that it is not resolved
15799 -- again at instantiation time.
15801 procedure Save_Entity_Descendants
(N
: Node_Id
);
15802 -- Apply Save_Global_References to the two syntactic descendants of
15803 -- non-terminal nodes that carry an Associated_Node and are processed
15804 -- through Reset_Entity. Once the global entity (if any) has been
15805 -- captured together with its type, only two syntactic descendants need
15806 -- to be traversed to complete the processing of the tree rooted at N.
15807 -- This applies to Selected_Components, Expanded_Names, and to Operator
15808 -- nodes. N can also be a character literal, identifier, or operator
15809 -- symbol node, but the call has no effect in these cases.
15811 procedure Save_Global_Defaults
(N1
: Node_Id
; N2
: Node_Id
);
15812 -- Default actuals in nested instances must be handled specially
15813 -- because there is no link to them from the original tree. When an
15814 -- actual subprogram is given by a default, we add an explicit generic
15815 -- association for it in the instantiation node. When we save the
15816 -- global references on the name of the instance, we recover the list
15817 -- of generic associations, and add an explicit one to the original
15818 -- generic tree, through which a global actual can be preserved.
15819 -- Similarly, if a child unit is instantiated within a sibling, in the
15820 -- context of the parent, we must preserve the identifier of the parent
15821 -- so that it can be properly resolved in a subsequent instantiation.
15823 procedure Save_Global_Descendant
(D
: Union_Id
);
15824 -- Apply Save_References recursively to the descendants of node D
15826 procedure Save_References
(N
: Node_Id
);
15827 -- This is the recursive procedure that does the work, once the
15828 -- enclosing generic scope has been established.
15830 procedure Set_Global_Type
(N
: Node_Id
; N2
: Node_Id
);
15831 -- If the type of N2 is global to the generic unit, save the type in
15832 -- the generic node. Just as we perform name capture for explicit
15833 -- references within the generic, we must capture the global types
15834 -- of local entities because they may participate in resolution in
15841 function Is_Global
(E
: Entity_Id
) return Boolean is
15844 function Is_Instance_Node
(Decl
: Node_Id
) return Boolean;
15845 -- Determine whether the parent node of a reference to a child unit
15846 -- denotes an instantiation or a formal package, in which case the
15847 -- reference to the child unit is global, even if it appears within
15848 -- the current scope (e.g. when the instance appears within the body
15849 -- of an ancestor).
15851 ----------------------
15852 -- Is_Instance_Node --
15853 ----------------------
15855 function Is_Instance_Node
(Decl
: Node_Id
) return Boolean is
15857 return Nkind
(Decl
) in N_Generic_Instantiation
15859 Nkind
(Original_Node
(Decl
)) = N_Formal_Package_Declaration
;
15860 end Is_Instance_Node
;
15862 -- Start of processing for Is_Global
15865 if E
= Gen_Scope
then
15868 elsif E
= Standard_Standard
then
15871 -- E should be an entity, but it is not always
15873 elsif Nkind
(E
) not in N_Entity
then
15876 elsif Nkind
(E
) /= N_Expanded_Name
15877 and then Is_Child_Unit
(E
)
15878 and then (Is_Instance_Node
(Parent
(N2
))
15879 or else (Nkind
(Parent
(N2
)) = N_Expanded_Name
15880 and then N2
= Selector_Name
(Parent
(N2
))
15882 Is_Instance_Node
(Parent
(Parent
(N2
)))))
15887 -- E may be an expanded name - typically an operator - in which
15888 -- case we must find its enclosing scope since expanded names
15889 -- don't have corresponding scopes.
15891 if Nkind
(E
) = N_Expanded_Name
then
15892 Se
:= Find_Enclosing_Scope
(E
);
15894 -- Otherwise, E is an entity and will have Scope set
15900 while Se
/= Gen_Scope
loop
15901 if Se
= Standard_Standard
then
15912 --------------------------------
15913 -- Qualify_Universal_Operands --
15914 --------------------------------
15916 procedure Qualify_Universal_Operands
15918 Func_Call
: Node_Id
)
15920 procedure Qualify_Operand
(Opnd
: Node_Id
; Actual
: Node_Id
);
15921 -- Rewrite operand Opnd as a qualified expression of the form
15923 -- Actual_Typ'(Opnd)
15925 -- where Actual is the corresponding actual parameter of Opnd in
15926 -- function call Func_Call.
15928 function Qualify_Type
15930 Typ
: Entity_Id
) return Node_Id
;
15931 -- Qualify type Typ by creating a selected component of the form
15933 -- Scope_Of_Typ.Typ
15935 ---------------------
15936 -- Qualify_Operand --
15937 ---------------------
15939 procedure Qualify_Operand
(Opnd
: Node_Id
; Actual
: Node_Id
) is
15940 Loc
: constant Source_Ptr
:= Sloc
(Opnd
);
15941 Typ
: constant Entity_Id
:= Etype
(Actual
);
15946 -- Qualify the operand when it is of a universal type. Note that
15947 -- the template is unanalyzed and it is not possible to directly
15948 -- query the type. This transformation is not done when the type
15949 -- of the actual is internally generated because the type will be
15950 -- regenerated in the instance.
15952 if Yields_Universal_Type
(Opnd
)
15953 and then Comes_From_Source
(Typ
)
15954 and then not Is_Hidden
(Typ
)
15956 -- The type of the actual may be a global reference. Save this
15957 -- information by creating a reference to it.
15959 if Is_Global
(Typ
) then
15960 Mark
:= New_Occurrence_Of
(Typ
, Loc
);
15962 -- Otherwise rely on resolution to find the proper type within
15966 Mark
:= Qualify_Type
(Loc
, Typ
);
15970 Make_Qualified_Expression
(Loc
,
15971 Subtype_Mark
=> Mark
,
15972 Expression
=> Relocate_Node
(Opnd
));
15974 -- Mark the qualification to distinguish it from other source
15975 -- constructs and signal the instantiation mechanism that this
15976 -- node requires special processing. See Copy_Generic_Node for
15979 Set_Is_Qualified_Universal_Literal
(Qual
);
15981 Rewrite
(Opnd
, Qual
);
15983 end Qualify_Operand
;
15989 function Qualify_Type
15991 Typ
: Entity_Id
) return Node_Id
15993 Scop
: constant Entity_Id
:= Scope
(Typ
);
15997 Result
:= Make_Identifier
(Loc
, Chars
(Typ
));
15999 if Present
(Scop
) and then not Is_Generic_Unit
(Scop
) then
16001 Make_Selected_Component
(Loc
,
16002 Prefix
=> Make_Identifier
(Loc
, Chars
(Scop
)),
16003 Selector_Name
=> Result
);
16011 Actuals
: constant List_Id
:= Parameter_Associations
(Func_Call
);
16013 -- Start of processing for Qualify_Universal_Operands
16016 if Nkind
(Op
) in N_Binary_Op
then
16017 Qualify_Operand
(Left_Opnd
(Op
), First
(Actuals
));
16018 Qualify_Operand
(Right_Opnd
(Op
), Next
(First
(Actuals
)));
16020 elsif Nkind
(Op
) in N_Unary_Op
then
16021 Qualify_Operand
(Right_Opnd
(Op
), First
(Actuals
));
16023 end Qualify_Universal_Operands
;
16029 procedure Reset_Entity
(N
: Node_Id
) is
16030 function Top_Ancestor
(E
: Entity_Id
) return Entity_Id
;
16031 -- Find the ultimate ancestor of the current unit. If it is not a
16032 -- generic unit, then the name of the current unit in the prefix of
16033 -- an expanded name must be replaced with its generic homonym to
16034 -- ensure that it will be properly resolved in an instance.
16040 function Top_Ancestor
(E
: Entity_Id
) return Entity_Id
is
16045 while Is_Child_Unit
(Par
) loop
16046 Par
:= Scope
(Par
);
16052 -- Start of processing for Reset_Entity
16055 N2
:= Get_Associated_Node
(N
);
16058 if Present
(E
) then
16060 -- If the node is an entry call to an entry in an enclosing task,
16061 -- it is rewritten as a selected component. No global entity to
16062 -- preserve in this case, since the expansion will be redone in
16065 if Nkind
(E
) not in N_Entity
then
16066 Set_Associated_Node
(N
, Empty
);
16067 Set_Etype
(N
, Empty
);
16071 -- If the entity is an itype created as a subtype of an access
16072 -- type with a null exclusion restore source entity for proper
16073 -- visibility. The itype will be created anew in the instance.
16076 and then Ekind
(E
) = E_Access_Subtype
16077 and then Is_Entity_Name
(N
)
16078 and then Chars
(Etype
(E
)) = Chars
(N
)
16081 Set_Entity
(N2
, E
);
16085 if Is_Global
(E
) then
16086 Set_Global_Type
(N
, N2
);
16088 elsif Nkind
(N
) = N_Op_Concat
16089 and then Is_Generic_Type
(Etype
(N2
))
16090 and then (Base_Type
(Etype
(Right_Opnd
(N2
))) = Etype
(N2
)
16092 Base_Type
(Etype
(Left_Opnd
(N2
))) = Etype
(N2
))
16093 and then Is_Intrinsic_Subprogram
(E
)
16097 -- Entity is local. Mark generic node as unresolved. Note that now
16098 -- it does not have an entity.
16101 Set_Associated_Node
(N
, Empty
);
16102 Set_Etype
(N
, Empty
);
16105 if Nkind
(Parent
(N
)) in N_Generic_Instantiation
16106 and then N
= Name
(Parent
(N
))
16108 Save_Global_Defaults
(Parent
(N
), Parent
(N2
));
16111 elsif Nkind
(Parent
(N
)) = N_Selected_Component
16112 and then Nkind
(Parent
(N2
)) = N_Expanded_Name
16114 -- In case of previous errors, the tree might be malformed
16116 if No
(Entity
(Parent
(N2
))) then
16119 elsif Is_Global
(Entity
(Parent
(N2
))) then
16120 Change_Selected_Component_To_Expanded_Name
(Parent
(N
));
16121 Set_Associated_Node
(Parent
(N
), Parent
(N2
));
16122 Set_Global_Type
(Parent
(N
), Parent
(N2
));
16123 Save_Entity_Descendants
(N
);
16125 -- If this is a reference to the current generic entity, replace
16126 -- by the name of the generic homonym of the current package. This
16127 -- is because in an instantiation Par.P.Q will not resolve to the
16128 -- name of the instance, whose enclosing scope is not necessarily
16129 -- Par. We use the generic homonym rather that the name of the
16130 -- generic itself because it may be hidden by a local declaration.
16132 elsif In_Open_Scopes
(Entity
(Parent
(N2
)))
16134 Is_Generic_Unit
(Top_Ancestor
(Entity
(Prefix
(Parent
(N2
)))))
16136 if Ekind
(Entity
(Parent
(N2
))) = E_Generic_Package
then
16137 Rewrite
(Parent
(N
),
16138 Make_Identifier
(Sloc
(N
),
16140 Chars
(Generic_Homonym
(Entity
(Parent
(N2
))))));
16142 Rewrite
(Parent
(N
),
16143 Make_Identifier
(Sloc
(N
),
16144 Chars
=> Chars
(Selector_Name
(Parent
(N2
)))));
16148 if Nkind
(Parent
(Parent
(N
))) in N_Generic_Instantiation
16149 and then Parent
(N
) = Name
(Parent
(Parent
(N
)))
16151 Save_Global_Defaults
16152 (Parent
(Parent
(N
)), Parent
(Parent
(N2
)));
16155 -- A selected component may denote a static constant that has been
16156 -- folded. If the static constant is global to the generic, capture
16157 -- its value. Otherwise the folding will happen in any instantiation.
16159 elsif Nkind
(Parent
(N
)) = N_Selected_Component
16160 and then Nkind
(Parent
(N2
)) in N_Integer_Literal | N_Real_Literal
16162 if Present
(Entity
(Original_Node
(Parent
(N2
))))
16163 and then Is_Global
(Entity
(Original_Node
(Parent
(N2
))))
16165 Rewrite
(Parent
(N
), New_Copy
(Parent
(N2
)));
16166 Set_Analyzed
(Parent
(N
), False);
16169 -- A selected component may be transformed into a parameterless
16170 -- function call. If the called entity is global, rewrite the node
16171 -- appropriately, i.e. as an extended name for the global entity.
16173 elsif Nkind
(Parent
(N
)) = N_Selected_Component
16174 and then Nkind
(Parent
(N2
)) = N_Function_Call
16175 and then N
= Selector_Name
(Parent
(N
))
16177 if No
(Parameter_Associations
(Parent
(N2
))) then
16178 if Is_Global
(Entity
(Name
(Parent
(N2
)))) then
16179 Change_Selected_Component_To_Expanded_Name
(Parent
(N
));
16180 Set_Associated_Node
(Parent
(N
), Name
(Parent
(N2
)));
16181 Set_Global_Type
(Parent
(N
), Name
(Parent
(N2
)));
16182 Save_Entity_Descendants
(N
);
16185 Set_Is_Prefixed_Call
(Parent
(N
));
16186 Set_Associated_Node
(N
, Empty
);
16187 Set_Etype
(N
, Empty
);
16190 -- In Ada 2005, X.F may be a call to a primitive operation,
16191 -- rewritten as F (X). This rewriting will be done again in an
16192 -- instance, so keep the original node. Global entities will be
16193 -- captured as for other constructs. Indicate that this must
16194 -- resolve as a call, to prevent accidental overloading in the
16195 -- instance, if both a component and a primitive operation appear
16199 Set_Is_Prefixed_Call
(Parent
(N
));
16202 -- Entity is local. Reset in generic unit, so that node is resolved
16203 -- anew at the point of instantiation.
16206 Set_Associated_Node
(N
, Empty
);
16207 Set_Etype
(N
, Empty
);
16211 -----------------------------
16212 -- Save_Entity_Descendants --
16213 -----------------------------
16215 procedure Save_Entity_Descendants
(N
: Node_Id
) is
16218 when N_Binary_Op
=>
16219 Save_Global_Descendant
(Union_Id
(Left_Opnd
(N
)));
16220 Save_Global_Descendant
(Union_Id
(Right_Opnd
(N
)));
16223 Save_Global_Descendant
(Union_Id
(Right_Opnd
(N
)));
16225 when N_Expanded_Name
16226 | N_Selected_Component
16228 Save_Global_Descendant
(Union_Id
(Prefix
(N
)));
16229 Save_Global_Descendant
(Union_Id
(Selector_Name
(N
)));
16231 when N_Character_Literal
16233 | N_Operator_Symbol
16238 raise Program_Error
;
16240 end Save_Entity_Descendants
;
16242 --------------------------
16243 -- Save_Global_Defaults --
16244 --------------------------
16246 procedure Save_Global_Defaults
(N1
: Node_Id
; N2
: Node_Id
) is
16247 Loc
: constant Source_Ptr
:= Sloc
(N1
);
16248 Assoc2
: constant List_Id
:= Generic_Associations
(N2
);
16249 Gen_Id
: constant Entity_Id
:= Get_Generic_Entity
(N2
);
16256 Actual
: Entity_Id
;
16259 Assoc1
:= Generic_Associations
(N1
);
16261 if Present
(Assoc1
) then
16262 Act1
:= First
(Assoc1
);
16265 Set_Generic_Associations
(N1
, New_List
);
16266 Assoc1
:= Generic_Associations
(N1
);
16269 if Present
(Assoc2
) then
16270 Act2
:= First
(Assoc2
);
16275 while Present
(Act1
) and then Present
(Act2
) loop
16280 -- Find the associations added for default subprograms
16282 if Present
(Act2
) then
16283 while Nkind
(Act2
) /= N_Generic_Association
16284 or else No
(Entity
(Selector_Name
(Act2
)))
16285 or else not Is_Overloadable
(Entity
(Selector_Name
(Act2
)))
16290 -- Add a similar association if the default is global. The
16291 -- renaming declaration for the actual has been analyzed, and
16292 -- its alias is the program it renames. Link the actual in the
16293 -- original generic tree with the node in the analyzed tree.
16295 while Present
(Act2
) loop
16296 Subp
:= Entity
(Selector_Name
(Act2
));
16297 Def
:= Explicit_Generic_Actual_Parameter
(Act2
);
16299 -- Following test is defence against rubbish errors
16301 if No
(Alias
(Subp
)) then
16305 -- Retrieve the resolved actual from the renaming declaration
16306 -- created for the instantiated formal.
16308 Actual
:= Entity
(Name
(Parent
(Parent
(Subp
))));
16309 Set_Entity
(Def
, Actual
);
16310 Set_Etype
(Def
, Etype
(Actual
));
16312 if Is_Global
(Actual
) then
16314 Make_Generic_Association
(Loc
,
16316 New_Occurrence_Of
(Subp
, Loc
),
16317 Explicit_Generic_Actual_Parameter
=>
16318 New_Occurrence_Of
(Actual
, Loc
));
16320 Set_Associated_Node
16321 (Explicit_Generic_Actual_Parameter
(Ndec
), Def
);
16323 Append
(Ndec
, Assoc1
);
16325 -- If there are other defaults, add a dummy association in case
16326 -- there are other defaulted formals with the same name.
16328 elsif Present
(Next
(Act2
)) then
16330 Make_Generic_Association
(Loc
,
16332 New_Occurrence_Of
(Subp
, Loc
),
16333 Explicit_Generic_Actual_Parameter
=> Empty
);
16335 Append
(Ndec
, Assoc1
);
16342 if Nkind
(Name
(N1
)) = N_Identifier
16343 and then Is_Child_Unit
(Gen_Id
)
16344 and then Is_Global
(Gen_Id
)
16345 and then Is_Generic_Unit
(Scope
(Gen_Id
))
16346 and then In_Open_Scopes
(Scope
(Gen_Id
))
16348 -- This is an instantiation of a child unit within a sibling, so
16349 -- that the generic parent is in scope. An eventual instance must
16350 -- occur within the scope of an instance of the parent. Make name
16351 -- in instance into an expanded name, to preserve the identifier
16352 -- of the parent, so it can be resolved subsequently.
16354 Rewrite
(Name
(N2
),
16355 Make_Expanded_Name
(Loc
,
16356 Chars
=> Chars
(Gen_Id
),
16357 Prefix
=> New_Occurrence_Of
(Scope
(Gen_Id
), Loc
),
16358 Selector_Name
=> New_Occurrence_Of
(Gen_Id
, Loc
)));
16359 Set_Entity
(Name
(N2
), Gen_Id
);
16361 Rewrite
(Name
(N1
),
16362 Make_Expanded_Name
(Loc
,
16363 Chars
=> Chars
(Gen_Id
),
16364 Prefix
=> New_Occurrence_Of
(Scope
(Gen_Id
), Loc
),
16365 Selector_Name
=> New_Occurrence_Of
(Gen_Id
, Loc
)));
16367 Set_Associated_Node
(Name
(N1
), Name
(N2
));
16368 Set_Associated_Node
(Prefix
(Name
(N1
)), Empty
);
16369 Set_Associated_Node
16370 (Selector_Name
(Name
(N1
)), Selector_Name
(Name
(N2
)));
16371 Set_Etype
(Name
(N1
), Etype
(Gen_Id
));
16373 end Save_Global_Defaults
;
16375 ----------------------------
16376 -- Save_Global_Descendant --
16377 ----------------------------
16379 procedure Save_Global_Descendant
(D
: Union_Id
) is
16383 if D
in Node_Range
then
16384 if D
= Union_Id
(Empty
) then
16387 elsif Nkind
(Node_Id
(D
)) /= N_Compilation_Unit
then
16388 Save_References
(Node_Id
(D
));
16391 elsif D
in List_Range
then
16392 pragma Assert
(D
/= Union_Id
(No_List
));
16393 -- Because No_List = Empty, which is in Node_Range above
16395 N1
:= First
(List_Id
(D
));
16396 while Present
(N1
) loop
16397 Save_References
(N1
);
16401 -- Element list or other non-node field, nothing to do
16406 end Save_Global_Descendant
;
16408 ---------------------
16409 -- Save_References --
16410 ---------------------
16412 -- This is the recursive procedure that does the work once the enclosing
16413 -- generic scope has been established. We have to treat specially a
16414 -- number of node rewritings that are required by semantic processing
16415 -- and which change the kind of nodes in the generic copy: typically
16416 -- constant-folding, replacing an operator node by a string literal, or
16417 -- a selected component by an expanded name. In each of those cases, the
16418 -- transformation is propagated to the generic unit.
16420 procedure Save_References
(N
: Node_Id
) is
16421 Loc
: constant Source_Ptr
:= Sloc
(N
);
16423 function Requires_Delayed_Save
(Nod
: Node_Id
) return Boolean;
16424 -- Determine whether arbitrary node Nod requires delayed capture of
16425 -- global references within its aspect specifications.
16427 procedure Save_References_In_Aggregate
(N
: Node_Id
);
16428 -- Save all global references in [extension] aggregate node N
16430 procedure Save_References_In_Char_Lit_Or_Op_Symbol
(N
: Node_Id
);
16431 -- Save all global references in a character literal or operator
16432 -- symbol denoted by N.
16434 procedure Save_References_In_Descendants
(N
: Node_Id
);
16435 -- Save all global references in all descendants of node N
16437 procedure Save_References_In_Identifier
(N
: Node_Id
);
16438 -- Save all global references in identifier node N
16440 procedure Save_References_In_Operator
(N
: Node_Id
);
16441 -- Save all global references in operator node N
16443 procedure Save_References_In_Pragma
(Prag
: Node_Id
);
16444 -- Save all global references found within the expression of pragma
16447 ---------------------------
16448 -- Requires_Delayed_Save --
16449 ---------------------------
16451 function Requires_Delayed_Save
(Nod
: Node_Id
) return Boolean is
16453 -- Generic packages and subprograms require delayed capture of
16454 -- global references within their aspects due to the timing of
16455 -- annotation analysis.
16457 if Nkind
(Nod
) in N_Generic_Package_Declaration
16458 | N_Generic_Subprogram_Declaration
16460 | N_Package_Body_Stub
16461 | N_Subprogram_Body
16462 | N_Subprogram_Body_Stub
16464 -- Since the capture of global references is done on the
16465 -- unanalyzed generic template, there is no information around
16466 -- to infer the context. Use the Associated_Entity linkages to
16467 -- peek into the analyzed generic copy and determine what the
16468 -- template corresponds to.
16470 if Nod
= Templ
then
16472 Is_Generic_Declaration_Or_Body
16473 (Unit_Declaration_Node
16474 (Get_Associated_Entity
(Defining_Entity
(Nod
))));
16476 -- Otherwise the generic unit being processed is not the top
16477 -- level template. It is safe to capture of global references
16478 -- within the generic unit because at this point the top level
16479 -- copy is fully analyzed.
16485 -- Otherwise capture the global references without interference
16490 end Requires_Delayed_Save
;
16492 ----------------------------------
16493 -- Save_References_In_Aggregate --
16494 ----------------------------------
16496 procedure Save_References_In_Aggregate
(N
: Node_Id
) is
16498 Qual
: Node_Id
:= Empty
;
16499 Typ
: Entity_Id
:= Empty
;
16502 N2
:= Get_Associated_Node
(N
);
16504 if Present
(N2
) then
16507 -- In an instance within a generic, use the name of the actual
16508 -- and not the original generic parameter. If the actual is
16509 -- global in the current generic it must be preserved for its
16512 if Parent_Kind
(Typ
) = N_Subtype_Declaration
16513 and then Present
(Generic_Parent_Type
(Parent
(Typ
)))
16515 Typ
:= Base_Type
(Typ
);
16516 Set_Etype
(N2
, Typ
);
16520 if No
(N2
) or else No
(Typ
) or else not Is_Global
(Typ
) then
16521 Set_Associated_Node
(N
, Empty
);
16523 -- For a full aggregate, if the type is local but is a derived
16524 -- tagged type of a global ancestor, we will need to have the
16525 -- full view of this global ancestor available in the instance
16526 -- in order to analyze the full aggregate.
16529 and then Nkind
(N2
) = N_Aggregate
16530 and then Present
(Typ
)
16531 and then Is_Tagged_Type
(Typ
)
16532 and then Is_Derived_Type
(Typ
)
16535 Root_Typ
: constant Entity_Id
:= Root_Type
(Typ
);
16537 Parent_Typ
: Entity_Id
:= Typ
;
16541 Parent_Typ
:= Etype
(Parent_Typ
);
16543 if Is_Global
(Parent_Typ
) then
16544 Set_Ancestor_Type
(N
, Parent_Typ
);
16548 exit when Parent_Typ
= Root_Typ
;
16553 -- If the aggregate is an actual in a call, it has been
16554 -- resolved in the current context, to some local type. The
16555 -- enclosing call may have been disambiguated by the aggregate,
16556 -- and this disambiguation might fail at instantiation time
16557 -- because the type to which the aggregate did resolve is not
16558 -- preserved. In order to preserve some of this information,
16559 -- wrap the aggregate in a qualified expression, using the id
16560 -- of its type. For further disambiguation we qualify the type
16561 -- name with its scope (if visible and not hidden by a local
16562 -- homograph) because both id's will have corresponding
16563 -- entities in an instance. This resolves most of the problems
16564 -- with missing type information on aggregates in instances.
16567 and then Nkind
(N2
) = Nkind
(N
)
16568 and then Nkind
(Parent
(N2
)) in N_Subprogram_Call
16569 and then Present
(Typ
)
16570 and then Comes_From_Source
(Typ
)
16572 Nam
:= Make_Identifier
(Loc
, Chars
(Typ
));
16574 if Is_Immediately_Visible
(Scope
(Typ
))
16576 (not In_Open_Scopes
(Scope
(Typ
))
16577 or else Current_Entity
(Scope
(Typ
)) = Scope
(Typ
))
16580 Make_Selected_Component
(Loc
,
16582 Make_Identifier
(Loc
, Chars
(Scope
(Typ
))),
16583 Selector_Name
=> Nam
);
16587 Make_Qualified_Expression
(Loc
,
16588 Subtype_Mark
=> Nam
,
16589 Expression
=> Relocate_Node
(N
));
16592 -- For a full aggregate, if the type is global and a derived
16593 -- tagged type, we will also need to have the full view of its
16594 -- ancestor available in the instance in order to analyze the
16598 and then Nkind
(N2
) = N_Aggregate
16599 and then Present
(Typ
)
16600 and then Is_Tagged_Type
(Typ
)
16601 and then Is_Derived_Type
(Typ
)
16603 Set_Ancestor_Type
(N
, Etype
(Typ
));
16606 if Nkind
(N
) = N_Aggregate
then
16607 Save_Global_Descendant
(Union_Id
(Aggregate_Bounds
(N
)));
16609 elsif Nkind
(N
) = N_Extension_Aggregate
then
16610 Save_Global_Descendant
(Union_Id
(Ancestor_Part
(N
)));
16613 pragma Assert
(False);
16616 Save_Global_Descendant
(Union_Id
(Expressions
(N
)));
16617 Save_Global_Descendant
(Union_Id
(Component_Associations
(N
)));
16618 Save_Global_Descendant
(Union_Id
(Etype
(N
)));
16620 if Present
(Qual
) then
16623 end Save_References_In_Aggregate
;
16625 ----------------------------------------------
16626 -- Save_References_In_Char_Lit_Or_Op_Symbol --
16627 ----------------------------------------------
16629 procedure Save_References_In_Char_Lit_Or_Op_Symbol
(N
: Node_Id
) is
16631 if Nkind
(N
) = Nkind
(Get_Associated_Node
(N
)) then
16634 elsif Nkind
(N
) = N_Operator_Symbol
16635 and then Nkind
(Get_Associated_Node
(N
)) = N_String_Literal
16637 Change_Operator_Symbol_To_String_Literal
(N
);
16639 end Save_References_In_Char_Lit_Or_Op_Symbol
;
16641 ------------------------------------
16642 -- Save_References_In_Descendants --
16643 ------------------------------------
16645 procedure Save_References_In_Descendants
(N
: Node_Id
) is
16646 procedure Walk
is new Walk_Sinfo_Fields
(Save_Global_Descendant
);
16649 end Save_References_In_Descendants
;
16651 -----------------------------------
16652 -- Save_References_In_Identifier --
16653 -----------------------------------
16655 procedure Save_References_In_Identifier
(N
: Node_Id
) is
16657 -- The node did not undergo a transformation
16659 if Nkind
(N
) = Nkind
(Get_Associated_Node
(N
)) then
16660 -- If this is a discriminant reference, always save it.
16661 -- It is used in the instance to find the corresponding
16662 -- discriminant positionally rather than by name.
16664 Set_Original_Discriminant
16665 (N
, Original_Discriminant
(Get_Associated_Node
(N
)));
16669 -- The analysis of the generic copy transformed the identifier
16670 -- into another construct. Propagate the changes to the template.
16673 N2
:= Get_Associated_Node
(N
);
16675 -- The identifier denotes a call to a parameterless function.
16676 -- Mark the node as resolved when the function is external.
16678 if Nkind
(N2
) = N_Function_Call
then
16679 E
:= Entity
(Name
(N2
));
16681 if Present
(E
) and then Is_Global
(E
) then
16682 Set_Global_Type
(N
, N2
);
16684 Set_Associated_Node
(N
, Empty
);
16685 Set_Etype
(N
, Empty
);
16688 -- The identifier denotes a named number that was constant
16689 -- folded. Preserve the original name for ASIS and undo the
16690 -- constant folding which will be repeated in the instance.
16691 -- Is this still needed???
16693 elsif Nkind
(N2
) in N_Integer_Literal | N_Real_Literal
16694 and then Is_Entity_Name
(Original_Node
(N2
))
16696 Set_Associated_Node
(N
, Original_Node
(N2
));
16699 -- The identifier resolved to a string literal. Propagate this
16700 -- information to the generic template.
16702 elsif Nkind
(N2
) = N_String_Literal
then
16703 Rewrite
(N
, New_Copy
(N2
));
16705 -- The identifier is rewritten as a dereference if it is the
16706 -- prefix of an implicit dereference. Preserve the original
16707 -- tree as the analysis of the instance will expand the node
16708 -- again, but preserve the resolved entity if it is global.
16710 elsif Nkind
(N2
) = N_Explicit_Dereference
then
16711 if Is_Entity_Name
(Prefix
(N2
))
16712 and then Present
(Entity
(Prefix
(N2
)))
16713 and then Is_Global
(Entity
(Prefix
(N2
)))
16715 Set_Associated_Node
(N
, Prefix
(N2
));
16716 Set_Global_Type
(N
, Prefix
(N2
));
16718 elsif Nkind
(Prefix
(N2
)) = N_Function_Call
16719 and then Is_Entity_Name
(Name
(Prefix
(N2
)))
16720 and then Present
(Entity
(Name
(Prefix
(N2
))))
16721 and then Is_Global
(Entity
(Name
(Prefix
(N2
))))
16724 Make_Explicit_Dereference
(Loc
,
16726 Make_Function_Call
(Loc
,
16729 (Entity
(Name
(Prefix
(N2
))), Loc
))));
16730 Set_Associated_Node
16731 (Name
(Prefix
(N
)), Name
(Prefix
(N2
)));
16732 Set_Global_Type
(Name
(Prefix
(N
)), Name
(Prefix
(N2
)));
16735 Set_Associated_Node
(N
, Empty
);
16736 Set_Etype
(N
, Empty
);
16739 -- The subtype mark of a nominally unconstrained object is
16740 -- rewritten as a subtype indication using the bounds of the
16741 -- expression. Recover the original subtype mark.
16743 elsif Nkind
(N2
) = N_Subtype_Indication
16744 and then Is_Entity_Name
(Original_Node
(N2
))
16746 Set_Associated_Node
(N
, Original_Node
(N2
));
16750 end Save_References_In_Identifier
;
16752 ---------------------------------
16753 -- Save_References_In_Operator --
16754 ---------------------------------
16756 procedure Save_References_In_Operator
(N
: Node_Id
) is
16758 N2
:= Get_Associated_Node
(N
);
16760 -- The node did not undergo a transformation
16762 if Nkind
(N
) = Nkind
(N2
) then
16763 if Nkind
(N
) = N_Op_Concat
then
16764 Set_Is_Component_Left_Opnd
16765 (N
, Is_Component_Left_Opnd
(N2
));
16766 Set_Is_Component_Right_Opnd
16767 (N
, Is_Component_Right_Opnd
(N2
));
16772 -- The analysis of the generic copy transformed the operator into
16773 -- some other construct. Propagate the changes to the template if
16777 -- The operator resoved to a function call
16779 if Nkind
(N2
) = N_Function_Call
then
16781 -- Add explicit qualifications in the generic template for
16782 -- all operands of universal type. This aids resolution by
16783 -- preserving the actual type of a literal or an attribute
16784 -- that yields a universal result.
16786 Qualify_Universal_Operands
(N
, N2
);
16788 E
:= Entity
(Name
(N2
));
16790 if Present
(E
) and then Is_Global
(E
) then
16791 Set_Global_Type
(N
, N2
);
16793 Set_Associated_Node
(N
, Empty
);
16794 Set_Etype
(N
, Empty
);
16797 -- The operator was folded into a literal
16799 elsif Nkind
(N2
) in N_Integer_Literal
16803 if Present
(Original_Node
(N2
))
16804 and then Nkind
(Original_Node
(N2
)) = Nkind
(N
)
16806 -- Operation was constant-folded. Whenever possible,
16807 -- recover semantic information from unfolded node.
16808 -- This was initially done for ASIS but is apparently
16809 -- needed also for e.g. compiling a-nbnbin.adb.
16811 Set_Associated_Node
(N
, Original_Node
(N2
));
16813 if Nkind
(N
) = N_Op_Concat
then
16814 Set_Is_Component_Left_Opnd
(N
,
16815 Is_Component_Left_Opnd
(Get_Associated_Node
(N
)));
16816 Set_Is_Component_Right_Opnd
(N
,
16817 Is_Component_Right_Opnd
(Get_Associated_Node
(N
)));
16822 -- Propagate the constant folding back to the template
16825 Rewrite
(N
, New_Copy
(N2
));
16826 Set_Analyzed
(N
, False);
16829 -- The operator was folded into an enumeration literal. Retain
16830 -- the entity to avoid spurious ambiguities if it is overloaded
16831 -- at the point of instantiation or inlining.
16833 elsif Nkind
(N2
) = N_Identifier
16834 and then Ekind
(Entity
(N2
)) = E_Enumeration_Literal
16836 Rewrite
(N
, New_Copy
(N2
));
16837 Set_Analyzed
(N
, False);
16841 -- Complete the operands check if node has not been constant
16844 if Nkind
(N
) in N_Op
then
16845 Save_Entity_Descendants
(N
);
16847 end Save_References_In_Operator
;
16849 -------------------------------
16850 -- Save_References_In_Pragma --
16851 -------------------------------
16853 procedure Save_References_In_Pragma
(Prag
: Node_Id
) is
16855 Do_Save
: Boolean := True;
16858 -- Do not save global references in pragmas generated from aspects
16859 -- because the pragmas will be regenerated at instantiation time.
16861 if From_Aspect_Specification
(Prag
) then
16864 -- The capture of global references within contract-related source
16865 -- pragmas associated with generic packages, subprograms or their
16866 -- respective bodies must be delayed due to timing of annotation
16867 -- analysis. Global references are still captured in routine
16868 -- Save_Global_References_In_Contract.
16870 elsif Is_Generic_Contract_Pragma
(Prag
) and then Prag
/= Templ
then
16871 if Is_Package_Contract_Annotation
(Prag
) then
16872 Context
:= Find_Related_Package_Or_Body
(Prag
);
16874 pragma Assert
(Is_Subprogram_Contract_Annotation
(Prag
));
16875 Context
:= Find_Related_Declaration_Or_Body
(Prag
);
16878 -- The use of Original_Node accounts for the case when the
16879 -- related context is generic template.
16881 if Requires_Delayed_Save
(Original_Node
(Context
)) then
16886 -- For all other cases, save all global references within the
16887 -- descendants, but skip the following semantic fields:
16888 -- Next_Pragma, Corresponding_Aspect, Next_Rep_Item.
16891 Save_Global_Descendant
16892 (Union_Id
(Pragma_Argument_Associations
(N
)));
16893 Save_Global_Descendant
(Union_Id
(Pragma_Identifier
(N
)));
16895 end Save_References_In_Pragma
;
16897 -- Start of processing for Save_References
16905 elsif Nkind
(N
) in N_Aggregate | N_Extension_Aggregate
then
16906 Save_References_In_Aggregate
(N
);
16908 -- Character literals, operator symbols
16910 elsif Nkind
(N
) in N_Character_Literal | N_Operator_Symbol
then
16911 Save_References_In_Char_Lit_Or_Op_Symbol
(N
);
16913 -- Defining identifiers
16915 elsif Nkind
(N
) in N_Entity
then
16920 elsif Nkind
(N
) = N_Identifier
then
16921 Save_References_In_Identifier
(N
);
16925 elsif Nkind
(N
) in N_Op
then
16926 Save_References_In_Operator
(N
);
16930 elsif Nkind
(N
) = N_Pragma
then
16931 Save_References_In_Pragma
(N
);
16933 elsif Nkind
(N
) = N_Aspect_Specification
then
16935 P
: constant Node_Id
:= Parent
(N
);
16939 if Permits_Aspect_Specifications
(P
) then
16941 -- The capture of global references within aspects
16942 -- associated with generic packages, subprograms or
16943 -- their bodies must be delayed due to timing of
16944 -- annotation analysis. Global references are still
16945 -- captured in routine Save_Global_References_In_Contract.
16947 if Requires_Delayed_Save
(Original_Node
(P
)) then
16950 -- Otherwise save all global references within the
16954 Expr
:= Expression
(N
);
16956 if Present
(Expr
) then
16957 Save_Global_References
(Expr
);
16963 -- Do not walk the node pointed to by Label_Construct twice
16965 elsif Nkind
(N
) = N_Implicit_Label_Declaration
then
16969 Save_References_In_Descendants
(N
);
16972 end Save_References
;
16974 ---------------------
16975 -- Set_Global_Type --
16976 ---------------------
16978 procedure Set_Global_Type
(N
: Node_Id
; N2
: Node_Id
) is
16979 Comparison
: constant Boolean := Nkind
(N2
) in N_Op_Compare
;
16980 Typ
: constant Entity_Id
:=
16981 (if Comparison
then Compare_Type
(N2
) else Etype
(N2
));
16984 -- For a comparison (or equality) operator, the Etype is Boolean, so
16985 -- it is always global. But the type subject to the Has_Private_View
16986 -- processing is the Compare_Type, so we must specifically check it.
16989 Set_Etype
(N
, Etype
(N2
));
16991 if not Is_Global
(Typ
) then
16995 Set_Compare_Type
(N
, Typ
);
16998 Set_Etype
(N
, Typ
);
17001 -- If the entity of N is not the associated node, this is a
17002 -- nested generic and it has an associated node as well, whose
17003 -- type is already the full view (see below). Indicate that the
17004 -- original node has a private view.
17006 if Entity
(N
) /= N2
then
17007 if Has_Private_View
(Entity
(N
)) then
17008 Set_Has_Private_View
(N
);
17011 if Has_Secondary_Private_View
(Entity
(N
)) then
17012 Set_Has_Secondary_Private_View
(N
);
17016 -- If not a private type, deal with a secondary private view
17018 if not Is_Private_Type
(Typ
) then
17019 if (Is_Access_Type
(Typ
)
17020 and then Is_Private_Type
(Designated_Type
(Typ
)))
17021 or else (Is_Array_Type
(Typ
)
17023 Is_Private_Type
(Component_Type_For_Private_View
(Typ
)))
17025 Set_Has_Secondary_Private_View
(N
);
17028 -- If it is a derivation of a private type in a context where no
17029 -- full view is needed, nothing to do either.
17031 elsif No
(Full_View
(Typ
)) and then Typ
/= Etype
(Typ
) then
17034 -- Otherwise mark the type for flipping and set the full view on N2
17035 -- when available, which is necessary for Check_Private_View to swap
17036 -- back the views in case the full declaration of Typ is visible in
17037 -- the instantiation context. Note that this will be problematic if
17038 -- N2 is re-analyzed later, e.g. if it's a default value in a call.
17041 Set_Has_Private_View
(N
);
17043 if Present
(Full_View
(Typ
)) then
17045 Set_Compare_Type
(N2
, Full_View
(Typ
));
17047 Set_Etype
(N2
, Full_View
(Typ
));
17052 if Is_Floating_Point_Type
(Typ
)
17053 and then Has_Dimension_System
(Typ
)
17055 Copy_Dimensions
(N2
, N
);
17057 end Set_Global_Type
;
17059 -- Start of processing for Save_Global_References
17062 Gen_Scope
:= Current_Scope
;
17064 -- If the generic unit is a child unit, references to entities in the
17065 -- parent are treated as local, because they will be resolved anew in
17066 -- the context of the instance of the parent.
17068 while Is_Child_Unit
(Gen_Scope
)
17069 and then Ekind
(Scope
(Gen_Scope
)) = E_Generic_Package
17071 Gen_Scope
:= Scope
(Gen_Scope
);
17074 Save_References
(Templ
);
17075 end Save_Global_References
;
17077 ---------------------------------------
17078 -- Save_Global_References_In_Aspects --
17079 ---------------------------------------
17081 procedure Save_Global_References_In_Aspects
(N
: Node_Id
) is
17086 Asp
:= First
(Aspect_Specifications
(N
));
17087 while Present
(Asp
) loop
17088 Expr
:= Expression
(Asp
);
17090 if Present
(Expr
) then
17091 Save_Global_References
(Expr
);
17096 end Save_Global_References_In_Aspects
;
17098 ------------------------------------------
17099 -- Set_Copied_Sloc_For_Inherited_Pragma --
17100 ------------------------------------------
17102 procedure Set_Copied_Sloc_For_Inherited_Pragma
17107 Create_Instantiation_Source
(N
, E
,
17108 Inlined_Body
=> False,
17109 Inherited_Pragma
=> True,
17110 Factor
=> S_Adjustment
);
17111 end Set_Copied_Sloc_For_Inherited_Pragma
;
17113 --------------------------------------
17114 -- Set_Copied_Sloc_For_Inlined_Body --
17115 --------------------------------------
17117 procedure Set_Copied_Sloc_For_Inlined_Body
(N
: Node_Id
; E
: Entity_Id
) is
17119 Create_Instantiation_Source
(N
, E
,
17120 Inlined_Body
=> True,
17121 Inherited_Pragma
=> False,
17122 Factor
=> S_Adjustment
);
17123 end Set_Copied_Sloc_For_Inlined_Body
;
17125 ---------------------
17126 -- Set_Instance_Of --
17127 ---------------------
17129 procedure Set_Instance_Of
(A
: Entity_Id
; B
: Entity_Id
) is
17131 Generic_Renamings
.Table
(Generic_Renamings
.Last
) := (A
, B
, Assoc_Null
);
17132 Generic_Renamings_HTable
.Set
(Generic_Renamings
.Last
);
17133 Generic_Renamings
.Increment_Last
;
17134 end Set_Instance_Of
;
17136 --------------------
17137 -- Set_Next_Assoc --
17138 --------------------
17140 procedure Set_Next_Assoc
(E
: Assoc_Ptr
; Next
: Assoc_Ptr
) is
17142 Generic_Renamings
.Table
(E
).Next_In_HTable
:= Next
;
17143 end Set_Next_Assoc
;
17145 -------------------
17146 -- Start_Generic --
17147 -------------------
17149 procedure Start_Generic
is
17151 -- ??? More things could be factored out in this routine.
17152 -- Should probably be done at a later stage.
17154 Generic_Flags
.Append
(Inside_A_Generic
);
17155 Inside_A_Generic
:= True;
17157 Expander_Mode_Save_And_Set
(False);
17160 ----------------------
17161 -- Set_Instance_Env --
17162 ----------------------
17164 -- WARNING: This routine manages SPARK regions
17166 procedure Set_Instance_Env
17167 (Gen_Unit
: Entity_Id
;
17168 Act_Unit
: Entity_Id
)
17170 Saved_AE
: constant Boolean := Assertions_Enabled
;
17171 Saved_CPL
: constant Node_Id
:= Check_Policy_List
;
17172 Saved_DEC
: constant Boolean := Dynamic_Elaboration_Checks
;
17173 Saved_SM
: constant SPARK_Mode_Type
:= SPARK_Mode
;
17174 Saved_SMP
: constant Node_Id
:= SPARK_Mode_Pragma
;
17177 -- Regardless of the current mode, predefined units are analyzed in the
17178 -- most current Ada mode, and earlier version Ada checks do not apply
17179 -- to predefined units. Nothing needs to be done for non-internal units.
17180 -- These are always analyzed in the current mode.
17182 if In_Internal_Unit
(Gen_Unit
) then
17184 -- The following call resets all configuration attributes to default
17185 -- or the xxx_Config versions of the attributes when the current sem
17186 -- unit is the main unit. At the same time, internal units must also
17187 -- inherit certain configuration attributes from their context. It
17188 -- is unclear what these two sets are.
17190 Set_Config_Switches
(True, Current_Sem_Unit
= Main_Unit
);
17192 -- Reinstall relevant configuration attributes of the context
17194 Assertions_Enabled
:= Saved_AE
;
17195 Check_Policy_List
:= Saved_CPL
;
17196 Dynamic_Elaboration_Checks
:= Saved_DEC
;
17198 Install_SPARK_Mode
(Saved_SM
, Saved_SMP
);
17201 Current_Instantiated_Parent
:=
17202 (Gen_Id
=> Gen_Unit
,
17203 Act_Id
=> Act_Unit
,
17204 Next_In_HTable
=> Assoc_Null
);
17205 end Set_Instance_Env
;
17211 procedure Switch_View
(T
: Entity_Id
) is
17212 BT
: constant Entity_Id
:= Base_Type
(T
);
17213 Priv_Elmt
: Elmt_Id
:= No_Elmt
;
17214 Priv_Sub
: Entity_Id
;
17217 -- T may be private but its base type may have been exchanged through
17218 -- some other occurrence, in which case there is nothing to switch
17219 -- besides T itself. Note that a private dependent subtype of a private
17220 -- type might not have been switched even if the base type has been,
17221 -- because of the last branch of Check_Private_View (see comment there).
17223 if not Is_Private_Type
(BT
) then
17224 Prepend_Elmt
(Full_View
(T
), Exchanged_Views
);
17225 Exchange_Declarations
(T
);
17229 Priv_Elmt
:= First_Elmt
(Private_Dependents
(BT
));
17231 if Present
(Full_View
(BT
)) then
17232 Prepend_Elmt
(Full_View
(BT
), Exchanged_Views
);
17233 Exchange_Declarations
(BT
);
17236 while Present
(Priv_Elmt
) loop
17237 Priv_Sub
:= Node
(Priv_Elmt
);
17239 if Present
(Full_View
(Priv_Sub
)) then
17240 Prepend_Elmt
(Full_View
(Priv_Sub
), Exchanged_Views
);
17241 Exchange_Declarations
(Priv_Sub
);
17244 Next_Elmt
(Priv_Elmt
);
17252 function True_Parent
(N
: Node_Id
) return Node_Id
is
17254 if Nkind
(Parent
(N
)) = N_Subunit
then
17255 return Parent
(Corresponding_Stub
(Parent
(N
)));
17261 -----------------------------
17262 -- Valid_Default_Attribute --
17263 -----------------------------
17265 procedure Valid_Default_Attribute
(Nam
: Entity_Id
; Def
: Node_Id
) is
17266 Attr_Id
: constant Attribute_Id
:=
17267 Get_Attribute_Id
(Attribute_Name
(Def
));
17268 T
: constant Entity_Id
:= Entity
(Prefix
(Def
));
17269 Is_Fun
: constant Boolean := (Ekind
(Nam
) = E_Function
);
17275 if No
(T
) or else T
= Any_Id
then
17280 F
:= First_Formal
(Nam
);
17281 while Present
(F
) loop
17282 Num_F
:= Num_F
+ 1;
17287 when Attribute_Adjacent
17288 | Attribute_Ceiling
17289 | Attribute_Copy_Sign
17291 | Attribute_Fraction
17292 | Attribute_Machine
17294 | Attribute_Remainder
17295 | Attribute_Rounding
17296 | Attribute_Unbiased_Rounding
17300 and then Is_Floating_Point_Type
(T
);
17302 when Attribute_Image
17306 | Attribute_Wide_Image
17307 | Attribute_Wide_Value
17309 OK
:= Is_Fun
and then Num_F
= 1 and then Is_Scalar_Type
(T
);
17314 OK
:= Is_Fun
and then Num_F
= 2 and then Is_Scalar_Type
(T
);
17316 when Attribute_Input
=>
17317 OK
:= (Is_Fun
and then Num_F
= 1);
17319 when Attribute_Output
17320 | Attribute_Put_Image
17324 OK
:= not Is_Fun
and then Num_F
= 2;
17332 ("attribute reference has wrong profile for subprogram", Def
);
17334 end Valid_Default_Attribute
;
17336 ----------------------------------
17337 -- Validate_Formal_Type_Default --
17338 ----------------------------------
17340 procedure Validate_Formal_Type_Default
(Decl
: Node_Id
) is
17341 Default
: constant Node_Id
:=
17342 Default_Subtype_Mark
(Original_Node
(Decl
));
17343 Formal
: constant Entity_Id
:= Defining_Identifier
(Decl
);
17345 Def_Sub
: Entity_Id
; -- Default subtype mark
17346 Type_Def
: Node_Id
;
17348 procedure Check_Discriminated_Formal
;
17349 -- Check that discriminants of default for private or incomplete
17350 -- type match those of formal type.
17352 function Reference_Formal
(N
: Node_Id
) return Traverse_Result
;
17353 -- Check whether formal type definition mentions a previous formal
17354 -- type of the same generic.
17356 ----------------------
17357 -- Reference_Formal --
17358 ----------------------
17360 function Reference_Formal
(N
: Node_Id
) return Traverse_Result
is
17362 if Is_Entity_Name
(N
)
17363 and then Scope
(Entity
(N
)) = Current_Scope
17369 end Reference_Formal
;
17371 function Depends_On_Other_Formals
is
17372 new Traverse_Func
(Reference_Formal
);
17374 function Default_Subtype_Matches
17375 (Gen_T
, Def_T
: Entity_Id
) return Boolean;
17377 procedure Validate_Array_Type_Default
;
17378 -- Verify that dimension, indices, and component types of default
17379 -- are compatible with formal array type definition.
17381 procedure Validate_Derived_Type_Default
;
17382 -- Verify that ancestor and progenitor types match.
17384 ---------------------------------
17385 -- Check_Discriminated_Formal --
17386 ---------------------------------
17388 procedure Check_Discriminated_Formal
is
17389 Formal_Discr
: Entity_Id
;
17390 Actual_Discr
: Entity_Id
;
17391 Formal_Subt
: Entity_Id
;
17394 if Has_Discriminants
(Formal
) then
17395 if not Has_Discriminants
(Def_Sub
) then
17397 ("default for & must have discriminants", Default
, Formal
);
17399 elsif Is_Constrained
(Def_Sub
) then
17401 ("default for & must be unconstrained", Default
, Formal
);
17404 Formal_Discr
:= First_Discriminant
(Formal
);
17405 Actual_Discr
:= First_Discriminant
(Def_Sub
);
17406 while Formal_Discr
/= Empty
loop
17407 if Actual_Discr
= Empty
then
17409 ("discriminants on Formal do not match formal",
17413 Formal_Subt
:= Etype
(Formal_Discr
);
17415 -- Access discriminants match if designated types do
17417 if Ekind
(Base_Type
(Formal_Subt
)) = E_Anonymous_Access_Type
17418 and then (Ekind
(Base_Type
(Etype
(Actual_Discr
)))) =
17419 E_Anonymous_Access_Type
17422 (Designated_Type
(Base_Type
(Formal_Subt
))) =
17424 (Designated_Type
(Base_Type
(Etype
(Actual_Discr
))))
17426 Subtypes_Statically_Match
17427 (Designated_Type
(Base_Type
(Formal_Subt
)),
17428 Designated_Type
(Base_Type
(Etype
(Actual_Discr
))))
17432 elsif Base_Type
(Formal_Subt
) /=
17433 Base_Type
(Etype
(Actual_Discr
))
17436 ("types of discriminants of default must match formal",
17439 elsif not Subtypes_Statically_Match
17440 (Formal_Subt
, Etype
(Actual_Discr
))
17441 and then Ada_Version
>= Ada_95
17444 ("subtypes of discriminants of default "
17445 & "must match formal",
17449 Next_Discriminant
(Formal_Discr
);
17450 Next_Discriminant
(Actual_Discr
);
17453 if Actual_Discr
/= Empty
then
17455 ("discriminants on default do not match formal",
17460 end Check_Discriminated_Formal
;
17462 ---------------------------
17463 -- Default_Subtype_Matches --
17464 ---------------------------
17466 function Default_Subtype_Matches
17467 (Gen_T
, Def_T
: Entity_Id
) return Boolean
17470 -- Check that the base types, root types (when dealing with class
17471 -- wide types), or designated types (when dealing with anonymous
17472 -- access types) of Gen_T and Def_T are statically matching subtypes.
17474 return (Base_Type
(Gen_T
) = Base_Type
(Def_T
)
17475 and then Subtypes_Statically_Match
(Gen_T
, Def_T
))
17477 or else (Is_Class_Wide_Type
(Gen_T
)
17478 and then Is_Class_Wide_Type
(Def_T
)
17479 and then Default_Subtype_Matches
17480 (Root_Type
(Gen_T
), Root_Type
(Def_T
)))
17482 or else (Is_Anonymous_Access_Type
(Gen_T
)
17483 and then Ekind
(Def_T
) = Ekind
(Gen_T
)
17484 and then Subtypes_Statically_Match
17485 (Designated_Type
(Gen_T
), Designated_Type
(Def_T
)));
17487 end Default_Subtype_Matches
;
17489 ----------------------------------
17490 -- Validate_Array_Type_Default --
17491 ----------------------------------
17493 procedure Validate_Array_Type_Default
is
17497 if not Is_Array_Type
(Def_Sub
) then
17498 Error_Msg_NE
("default for& must be an array type ",
17502 elsif Number_Dimensions
(Def_Sub
) /= Number_Dimensions
(Formal
)
17503 or else Is_Constrained
(Def_Sub
) /=
17504 Is_Constrained
(Formal
)
17506 Error_Msg_NE
("default array type does not match&",
17511 I1
:= First_Index
(Formal
);
17512 I2
:= First_Index
(Def_Sub
);
17513 for J
in 1 .. Number_Dimensions
(Formal
) loop
17515 -- If the indexes of the actual were given by a subtype_mark,
17516 -- the index was transformed into a range attribute. Retrieve
17517 -- the original type mark for checking.
17519 if Is_Entity_Name
(Original_Node
(I2
)) then
17520 T2
:= Entity
(Original_Node
(I2
));
17525 if not Subtypes_Statically_Match
(Etype
(I1
), T2
) then
17527 ("index types of default do not match those of formal &",
17535 if not Default_Subtype_Matches
17536 (Component_Type
(Formal
), Component_Type
(Def_Sub
))
17539 ("component subtype of default does not match that of formal &",
17543 if Has_Aliased_Components
(Formal
)
17544 and then not Has_Aliased_Components
(Default
)
17547 ("default must have aliased components to match formal type &",
17550 end Validate_Array_Type_Default
;
17552 -----------------------------------
17553 -- Validate_Derived_Type_Default --
17554 -----------------------------------
17556 procedure Validate_Derived_Type_Default
is
17558 if not Is_Ancestor
(Etype
(Formal
), Def_Sub
) then
17559 Error_Msg_NE
("default must be a descendent of&",
17560 Default
, Etype
(Formal
));
17563 if Has_Interfaces
(Formal
) then
17564 if not Has_Interfaces
(Def_Sub
) then
17566 ("default must implement all interfaces of formal&",
17572 Iface_Ent
: Entity_Id
;
17575 Iface
:= First
(Abstract_Interface_List
(Formal
));
17577 while Present
(Iface
) loop
17578 Iface_Ent
:= Entity
(Iface
);
17580 if Is_Ancestor
(Iface_Ent
, Def_Sub
)
17581 or else Is_Progenitor
(Iface_Ent
, Def_Sub
)
17587 ("Default must implement interface&",
17588 Default
, Etype
(Iface
));
17596 end Validate_Derived_Type_Default
;
17598 -- Start of processing for Validate_Formal_Type_Default
17602 if not Is_Entity_Name
(Default
)
17603 or else not Is_Type
(Entity
(Default
))
17606 ("Expect type name for default of formal type", Default
);
17609 Def_Sub
:= Entity
(Default
);
17612 -- Formal derived_type declarations are transformed into full
17613 -- type declarations or Private_Type_Extensions for ease of processing.
17615 if Nkind
(Decl
) = N_Full_Type_Declaration
then
17616 Type_Def
:= Type_Definition
(Decl
);
17618 elsif Nkind
(Decl
) = N_Private_Extension_Declaration
then
17619 Type_Def
:= Subtype_Indication
(Decl
);
17622 Type_Def
:= Formal_Type_Definition
(Decl
);
17625 if Depends_On_Other_Formals
(Type_Def
) = Abandon
17626 and then Scope
(Def_Sub
) /= Current_Scope
17628 Error_Msg_N
("default of formal type that depends on "
17629 & "other formals must be a previous formal type", Default
);
17632 elsif Def_Sub
= Formal
then
17634 ("default for formal type cannot be formal itsef", Default
);
17638 case Nkind
(Type_Def
) is
17640 when N_Formal_Private_Type_Definition
=>
17641 if (Is_Abstract_Type
(Formal
)
17642 and then not Is_Abstract_Type
(Def_Sub
))
17643 or else (Is_Limited_Type
(Formal
)
17644 and then not Is_Limited_Type
(Def_Sub
))
17647 ("default for private type$ does not match",
17651 Check_Discriminated_Formal
;
17653 when N_Formal_Derived_Type_Definition
=>
17654 Check_Discriminated_Formal
;
17655 Validate_Derived_Type_Default
;
17657 when N_Formal_Incomplete_Type_Definition
=>
17658 if Is_Tagged_Type
(Formal
)
17659 and then not Is_Tagged_Type
(Def_Sub
)
17662 ("default for & must be a tagged type", Default
, Formal
);
17665 Check_Discriminated_Formal
;
17667 when N_Formal_Discrete_Type_Definition
=>
17668 if not Is_Discrete_Type
(Def_Sub
) then
17669 Error_Msg_NE
("default for& must be a discrete type",
17673 when N_Formal_Signed_Integer_Type_Definition
=>
17674 if not Is_Integer_Type
(Def_Sub
) then
17675 Error_Msg_NE
("default for& must be a discrete type",
17679 when N_Formal_Modular_Type_Definition
=>
17680 if not Is_Modular_Integer_Type
(Def_Sub
) then
17681 Error_Msg_NE
("default for& must be a modular_integer Type",
17685 when N_Formal_Floating_Point_Definition
=>
17686 if not Is_Floating_Point_Type
(Def_Sub
) then
17687 Error_Msg_NE
("default for& must be a floating_point type",
17691 when N_Formal_Ordinary_Fixed_Point_Definition
=>
17692 if not Is_Ordinary_Fixed_Point_Type
(Def_Sub
) then
17693 Error_Msg_NE
("default for& must be "
17694 & "an ordinary_fixed_point type ",
17698 when N_Formal_Decimal_Fixed_Point_Definition
=>
17699 if not Is_Decimal_Fixed_Point_Type
(Def_Sub
) then
17700 Error_Msg_NE
("default for& must be "
17701 & "an Decimal_fixed_point type ",
17705 when N_Array_Type_Definition
=>
17706 Validate_Array_Type_Default
;
17708 when N_Access_Function_Definition |
17709 N_Access_Procedure_Definition
=>
17710 if Ekind
(Def_Sub
) /= E_Access_Subprogram_Type
then
17711 Error_Msg_NE
("default for& must be an Access_To_Subprogram",
17714 Check_Subtype_Conformant
17715 (Designated_Type
(Formal
), Designated_Type
(Def_Sub
));
17717 when N_Access_To_Object_Definition
=>
17718 if not Is_Access_Object_Type
(Def_Sub
) then
17719 Error_Msg_NE
("default for& must be an Access_To_Object",
17722 elsif not Default_Subtype_Matches
17723 (Designated_Type
(Formal
), Designated_Type
(Def_Sub
))
17725 Error_Msg_NE
("designated type of defaul does not match "
17726 & "designated type of formal type",
17730 when N_Record_Definition
=> -- Formal interface type
17731 if not Is_Interface
(Def_Sub
) then
17733 ("default for formal interface type must be an interface",
17736 elsif Is_Limited_Type
(Def_Sub
) /= Is_Limited_Type
(Formal
)
17737 or else Is_Task_Interface
(Formal
) /= Is_Task_Interface
(Def_Sub
)
17738 or else Is_Protected_Interface
(Formal
) /=
17739 Is_Protected_Interface
(Def_Sub
)
17740 or else Is_Synchronized_Interface
(Formal
) /=
17741 Is_Synchronized_Interface
(Def_Sub
)
17744 ("default for interface& does not match", Def_Sub
, Formal
);
17747 when N_Derived_Type_Definition
=>
17748 Validate_Derived_Type_Default
;
17750 when N_Identifier
=> -- case of a private extension
17751 Validate_Derived_Type_Default
;
17757 raise Program_Error
;
17759 end Validate_Formal_Type_Default
;