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 Contains_Instance_Of
588 N
: Node_Id
) return Boolean;
589 -- Inner is instantiated within the generic Outer. Check whether Inner
590 -- directly or indirectly contains an instance of Outer or of one of its
591 -- parents, in the case of a subunit. Each generic unit holds a list of
592 -- the entities instantiated within (at any depth). This procedure
593 -- determines whether the set of such lists contains a cycle, i.e. an
594 -- illegal circular instantiation.
596 function Denotes_Formal_Package
598 On_Exit
: Boolean := False;
599 Instance
: Entity_Id
:= Empty
) return Boolean;
600 -- Returns True if E is a formal package of an enclosing generic, or
601 -- the actual for such a formal in an enclosing instantiation. If such
602 -- a package is used as a formal in an nested generic, or as an actual
603 -- in a nested instantiation, the visibility of ITS formals should not
604 -- be modified. When called from within Restore_Private_Views, the flag
605 -- On_Exit is true, to indicate that the search for a possible enclosing
606 -- instance should ignore the current one. In that case Instance denotes
607 -- the declaration for which this is an actual. This declaration may be
608 -- an instantiation in the source, or the internal instantiation that
609 -- corresponds to the actual for a formal package.
611 function Earlier
(N1
, N2
: Node_Id
) return Boolean;
612 -- Yields True if N1 and N2 appear in the same compilation unit,
613 -- ignoring subunits, and if N1 is to the left of N2 in a left-to-right
614 -- traversal of the tree for the unit. Used to determine the placement
615 -- of freeze nodes for instance bodies that may depend on other instances.
617 function Find_Actual_Type
619 Gen_Type
: Entity_Id
) return Entity_Id
;
620 -- When validating the actual types of a child instance, check whether
621 -- the formal is a formal type of the parent unit, and retrieve the current
622 -- actual for it. Typ is the entity in the analyzed formal type declaration
623 -- (component or index type of an array type, or designated type of an
624 -- access formal) and Gen_Type is the enclosing analyzed formal array
625 -- or access type. The desired actual may be a formal of a parent, or may
626 -- be declared in a formal package of a parent. In both cases it is a
627 -- generic actual type because it appears within a visible instance.
628 -- Finally, it may be declared in a parent unit without being a formal
629 -- of that unit, in which case it must be retrieved by visibility.
630 -- Ambiguities may still arise if two homonyms are declared in two formal
631 -- packages, and the prefix of the formal type may be needed to resolve
632 -- the ambiguity in the instance ???
634 procedure Freeze_Package_Instance
639 -- If the instantiation happens textually before the body of the generic,
640 -- the instantiation of the body must be analyzed after the generic body,
641 -- and not at the point of instantiation. Such early instantiations can
642 -- happen if the generic and the instance appear in a package declaration
643 -- because the generic body can only appear in the corresponding package
644 -- body. Early instantiations can also appear if generic, instance and
645 -- body are all in the declarative part of a subprogram or entry. Entities
646 -- of packages that are early instantiations are delayed, and their freeze
647 -- node appears after the generic body. This rather complex machinery is
648 -- needed when nested instantiations are present, because the source does
649 -- not carry any indication of where the corresponding instance bodies must
650 -- be installed and frozen.
652 procedure Freeze_Subprogram_Instance
655 Pack_Id
: Entity_Id
);
656 -- The generic body may appear textually after the instance, including
657 -- in the proper body of a stub, or within a different package instance.
658 -- Given that the instance can only be elaborated after the generic, we
659 -- place freeze nodes for the instance and/or for packages that may enclose
660 -- the instance and the generic, so that the back-end can establish the
661 -- proper order of elaboration.
663 function Get_Associated_Node
(N
: Node_Id
) return Node_Id
;
664 -- In order to propagate semantic information back from the analyzed copy
665 -- to the original generic, we maintain links between selected nodes in the
666 -- generic and their corresponding copies. At the end of generic analysis,
667 -- the routine Save_Global_References traverses the generic tree, examines
668 -- the semantic information, and preserves the links to those nodes that
669 -- contain global information. At instantiation, the information from the
670 -- associated node is placed on the new copy, so that name resolution is
673 -- Three kinds of source nodes have associated nodes:
675 -- a) those that can reference (denote) entities, that is identifiers,
676 -- character literals, expanded_names, operator symbols, operators,
677 -- and attribute reference nodes. These nodes have an Entity field
678 -- and are the set of nodes that are in N_Has_Entity.
680 -- b) aggregates (N_Aggregate and N_Extension_Aggregate)
682 -- c) selected components (N_Selected_Component)
684 -- For the first class, the associated node preserves the entity if it is
685 -- global. If the generic contains nested instantiations, the associated
686 -- node itself has been recopied, and a chain of them must be followed.
688 -- For aggregates, the associated node allows retrieval of the type, which
689 -- may otherwise not appear in the generic. The view of this type may be
690 -- different between generic and instantiation, and the full view can be
691 -- installed before the instantiation is analyzed. For aggregates of type
692 -- extensions, the same view exchange may have to be performed for some of
693 -- the ancestor types, if their view is private at the point of
696 -- Nodes that are selected components in the parse tree may be rewritten
697 -- as expanded names after resolution, and must be treated as potential
698 -- entity holders, which is why they also have an Associated_Node.
700 -- Nodes that do not come from source, such as freeze nodes, do not appear
701 -- in the generic tree, and need not have an associated node.
703 -- The associated node is stored in the Associated_Node field. Note that
704 -- this field overlaps Entity, which is fine, because the whole point is
705 -- that we don't need or want the normal Entity field in this situation.
707 function Has_Been_Exchanged
(E
: Entity_Id
) return Boolean;
708 -- Traverse the Exchanged_Views list to see if a type was private
709 -- and has already been flipped during this phase of instantiation.
711 function Has_Contracts
(Decl
: Node_Id
) return Boolean;
712 -- Determine whether a formal subprogram has a Pre- or Postcondition,
713 -- in which case a subprogram wrapper has to be built for the actual.
715 procedure Hide_Current_Scope
;
716 -- When instantiating a generic child unit, the parent context must be
717 -- present, but the instance and all entities that may be generated
718 -- must be inserted in the current scope. We leave the current scope
719 -- on the stack, but make its entities invisible to avoid visibility
720 -- problems. This is reversed at the end of the instantiation. This is
721 -- not done for the instantiation of the bodies, which only require the
722 -- instances of the generic parents to be in scope.
724 function In_Main_Context
(E
: Entity_Id
) return Boolean;
725 -- Check whether an instantiation is in the context of the main unit.
726 -- Used to determine whether its body should be elaborated to allow
727 -- front-end inlining.
729 procedure Inherit_Context
(Gen_Decl
: Node_Id
; Inst
: Node_Id
);
730 -- Add the context clause of the unit containing a generic unit to a
731 -- compilation unit that is, or contains, an instantiation.
734 -- Establish environment for subsequent instantiation. Separated from
735 -- Save_Env because data-structures for visibility handling must be
736 -- initialized before call to Check_Generic_Child_Unit.
738 procedure Inline_Instance_Body
740 Gen_Unit
: Entity_Id
;
742 -- If front-end inlining is requested, instantiate the package body,
743 -- and preserve the visibility of its compilation unit, to insure
744 -- that successive instantiations succeed.
746 procedure Insert_Freeze_Node_For_Instance
749 -- N denotes a package or a subprogram instantiation and F_Node is the
750 -- associated freeze node. Insert the freeze node before the first source
751 -- body which follows immediately after N. If no such body is found, the
752 -- freeze node is inserted at the end of the declarative region which
753 -- contains N, unless the instantiation is done in a package spec that is
754 -- not at library level, in which case it is inserted at the outer level.
755 -- This can also be invoked to insert the freeze node of a package that
756 -- encloses an instantiation, in which case N may denote an arbitrary node.
758 procedure Install_Formal_Packages
(Par
: Entity_Id
);
759 -- Install the visible part of any formal of the parent that is a formal
760 -- package. Note that for the case of a formal package with a box, this
761 -- includes the formal part of the formal package (12.7(10/2)).
763 procedure Install_Hidden_Primitives
764 (Prims_List
: in out Elist_Id
;
767 -- Remove suffix 'P' from hidden primitives of Act_T to match the
768 -- visibility of primitives of Gen_T. The list of primitives to which
769 -- the suffix is removed is added to Prims_List to restore them later.
771 procedure Install_Parent
(P
: Entity_Id
; In_Body
: Boolean := False);
772 -- When compiling an instance of a child unit the parent (which is
773 -- itself an instance) is an enclosing scope that must be made
774 -- immediately visible. This procedure is also used to install the non-
775 -- generic parent of a generic child unit when compiling its body, so
776 -- that full views of types in the parent are made visible.
778 -- The functions Instantiate_XXX perform various legality checks and build
779 -- the declarations for instantiated generic parameters. In all of these
780 -- Formal is the entity in the generic unit, Actual is the entity of
781 -- expression in the generic associations, and Analyzed_Formal is the
782 -- formal in the generic copy, which contains the semantic information to
783 -- be used to validate the actual.
785 function Instantiate_Object
788 Analyzed_Formal
: Node_Id
) return List_Id
;
790 function Instantiate_Type
793 Analyzed_Formal
: Node_Id
;
794 Actual_Decls
: List_Id
) return List_Id
;
796 function Instantiate_Formal_Subprogram
799 Analyzed_Formal
: Node_Id
) return Node_Id
;
801 function Instantiate_Formal_Package
804 Analyzed_Formal
: Node_Id
) return List_Id
;
805 -- If the formal package is declared with a box, special visibility rules
806 -- apply to its formals: they are in the visible part of the package. This
807 -- is true in the declarative region of the formal package, that is to say
808 -- in the enclosing generic or instantiation. For an instantiation, the
809 -- parameters of the formal package are made visible in an explicit step.
810 -- Furthermore, if the actual has a visible USE clause, these formals must
811 -- be made potentially use-visible as well. On exit from the enclosing
812 -- instantiation, the reverse must be done.
814 -- For a formal package declared without a box, there are conformance rules
815 -- that apply to the actuals in the generic declaration and the actuals of
816 -- the actual package in the enclosing instantiation. The simplest way to
817 -- apply these rules is to repeat the instantiation of the formal package
818 -- in the context of the enclosing instance, and compare the generic
819 -- associations of this instantiation with those of the actual package.
820 -- This internal instantiation only needs to contain the renamings of the
821 -- formals: the visible and private declarations themselves need not be
824 -- In Ada 2005, the formal package may be only partially parameterized.
825 -- In that case the visibility step must make visible those actuals whose
826 -- corresponding formals were given with a box. A final complication
827 -- involves inherited operations from formal derived types, which must
828 -- be visible if the type is.
830 function Is_In_Main_Unit
(N
: Node_Id
) return Boolean;
831 -- Test if given node is in the main unit
833 procedure Load_Parent_Of_Generic
836 Body_Optional
: Boolean := False);
837 -- If the generic appears in a separate non-generic library unit, load the
838 -- corresponding body to retrieve the body of the generic. N is the node
839 -- for the generic instantiation, Spec is the generic package declaration.
841 -- Body_Optional is a flag that indicates that the body is being loaded to
842 -- ensure that temporaries are generated consistently when there are other
843 -- instances in the current declarative part that precede the one being
844 -- loaded. In that case a missing body is acceptable.
846 procedure Map_Formal_Package_Entities
(Form
: Entity_Id
; Act
: Entity_Id
);
847 -- Within the generic part, entities in the formal package are
848 -- visible. To validate subsequent type declarations, indicate
849 -- the correspondence between the entities in the analyzed formal,
850 -- and the entities in the actual package. There are three packages
851 -- involved in the instantiation of a formal package: the parent
852 -- generic P1 which appears in the generic declaration, the fake
853 -- instantiation P2 which appears in the analyzed generic, and whose
854 -- visible entities may be used in subsequent formals, and the actual
855 -- P3 in the instance. To validate subsequent formals, me indicate
856 -- that the entities in P2 are mapped into those of P3. The mapping of
857 -- entities has to be done recursively for nested packages.
859 procedure Move_Freeze_Nodes
863 -- Freeze nodes can be generated in the analysis of a generic unit, but
864 -- will not be seen by the back-end. It is necessary to move those nodes
865 -- to the enclosing scope if they freeze an outer entity. We place them
866 -- at the end of the enclosing generic package, which is semantically
869 procedure Preanalyze_Actuals
(N
: Node_Id
; Inst
: Entity_Id
:= Empty
);
870 -- Analyze actuals to perform name resolution. Full resolution is done
871 -- later, when the expected types are known, but names have to be captured
872 -- before installing parents of generics, that are not visible for the
873 -- actuals themselves.
875 -- If Inst is present, it is the entity of the package instance. This
876 -- entity is marked as having a limited_view actual when some actual is
877 -- a limited view. This is used to place the instance body properly.
879 procedure Provide_Completing_Bodies
(N
: Node_Id
);
880 -- Generate completing bodies for all subprograms found within package or
881 -- subprogram declaration N.
883 procedure Remove_Parent
(In_Body
: Boolean := False);
884 -- Reverse effect after instantiation of child is complete
886 function Requires_Conformance_Checking
(N
: Node_Id
) return Boolean;
887 -- Determine whether the formal package declaration N requires conformance
888 -- checking with actuals in instantiations.
890 procedure Restore_Hidden_Primitives
(Prims_List
: in out Elist_Id
);
891 -- Restore suffix 'P' to primitives of Prims_List and leave Prims_List
894 procedure Set_Instance_Env
895 (Gen_Unit
: Entity_Id
;
896 Act_Unit
: Entity_Id
);
897 -- Save current instance on saved environment, to be used to determine
898 -- the global status of entities in nested instances. Part of Save_Env.
899 -- called after verifying that the generic unit is legal for the instance,
900 -- The procedure also examines whether the generic unit is a predefined
901 -- unit, in order to set configuration switches accordingly. As a result
902 -- the procedure must be called after analyzing and freezing the actuals.
904 procedure Set_Instance_Of
(A
: Entity_Id
; B
: Entity_Id
);
905 -- Associate analyzed generic parameter with corresponding instance. Used
906 -- for semantic checks at instantiation time.
908 function True_Parent
(N
: Node_Id
) return Node_Id
;
909 -- For a subunit, return parent of corresponding stub, else return
912 procedure Valid_Default_Attribute
(Nam
: Entity_Id
; Def
: Node_Id
);
913 -- Verify that an attribute that appears as the default for a formal
914 -- subprogram is a function or procedure with the correct profile.
916 procedure Validate_Formal_Type_Default
(Decl
: Node_Id
);
917 -- Ada_2022 AI12-205: if a default subtype_mark is present, verify
918 -- that it is the name of a type in the same class as the formal.
919 -- The treatment parallels what is done in Instantiate_Type but differs
920 -- in a few ways so that this machinery cannot be reused as is: on one
921 -- hand there are no visibility issues for a default, because it is
922 -- analyzed in the same context as the formal type definition; on the
923 -- other hand the check needs to take into acount the use of a previous
924 -- formal type in the current formal type definition (see details in
927 -------------------------------------------
928 -- Data Structures for Generic Renamings --
929 -------------------------------------------
931 -- The map Generic_Renamings associates generic entities with their
932 -- corresponding actuals. Currently used to validate type instances. It
933 -- will eventually be used for all generic parameters to eliminate the
934 -- need for overload resolution in the instance.
936 type Assoc_Ptr
is new Int
;
938 Assoc_Null
: constant Assoc_Ptr
:= -1;
943 Next_In_HTable
: Assoc_Ptr
;
946 package Generic_Renamings
is new Table
.Table
947 (Table_Component_Type
=> Assoc
,
948 Table_Index_Type
=> Assoc_Ptr
,
949 Table_Low_Bound
=> 0,
951 Table_Increment
=> 100,
952 Table_Name
=> "Generic_Renamings");
954 -- Variable to hold enclosing instantiation. When the environment is
955 -- saved for a subprogram inlining, the corresponding Act_Id is empty.
957 Current_Instantiated_Parent
: Assoc
:= (Empty
, Empty
, Assoc_Null
);
959 -- Hash table for associations
961 HTable_Size
: constant := 37;
962 type HTable_Range
is range 0 .. HTable_Size
- 1;
964 procedure Set_Next_Assoc
(E
: Assoc_Ptr
; Next
: Assoc_Ptr
);
965 function Next_Assoc
(E
: Assoc_Ptr
) return Assoc_Ptr
;
966 function Get_Gen_Id
(E
: Assoc_Ptr
) return Entity_Id
;
967 function Hash
(F
: Entity_Id
) return HTable_Range
;
969 package Generic_Renamings_HTable
is new GNAT
.HTable
.Static_HTable
(
970 Header_Num
=> HTable_Range
,
972 Elmt_Ptr
=> Assoc_Ptr
,
973 Null_Ptr
=> Assoc_Null
,
974 Set_Next
=> Set_Next_Assoc
,
977 Get_Key
=> Get_Gen_Id
,
981 Exchanged_Views
: Elist_Id
;
982 -- This list holds the private views that have been exchanged during
983 -- instantiation to restore the visibility of the generic declaration.
984 -- (see comments above). After instantiation, the current visibility is
985 -- reestablished by means of a traversal of this list.
987 Hidden_Entities
: Elist_Id
;
988 -- This list holds the entities of the current scope that are removed
989 -- from immediate visibility when instantiating a child unit. Their
990 -- visibility is restored in Remove_Parent.
992 -- Because instantiations can be recursive, the following must be saved
993 -- on entry and restored on exit from an instantiation (spec or body).
994 -- This is done by the two procedures Save_Env and Restore_Env. For
995 -- package and subprogram instantiations (but not for the body instances)
996 -- the action of Save_Env is done in two steps: Init_Env is called before
997 -- Check_Generic_Child_Unit, because setting the parent instances requires
998 -- that the visibility data structures be properly initialized. Once the
999 -- generic is unit is validated, Set_Instance_Env completes Save_Env.
1001 Parent_Unit_Visible
: Boolean := False;
1002 -- Parent_Unit_Visible is used when the generic is a child unit, and
1003 -- indicates whether the ultimate parent of the generic is visible in the
1004 -- instantiation environment. It is used to reset the visibility of the
1005 -- parent at the end of the instantiation (see Remove_Parent).
1007 Instance_Parent_Unit
: Entity_Id
:= Empty
;
1008 -- This records the ultimate parent unit of an instance of a generic
1009 -- child unit and is used in conjunction with Parent_Unit_Visible to
1010 -- indicate the unit to which the Parent_Unit_Visible flag corresponds.
1012 type Instance_Env
is record
1013 Instantiated_Parent
: Assoc
;
1014 Exchanged_Views
: Elist_Id
;
1015 Hidden_Entities
: Elist_Id
;
1016 Current_Sem_Unit
: Unit_Number_Type
;
1017 Parent_Unit_Visible
: Boolean := False;
1018 Instance_Parent_Unit
: Entity_Id
:= Empty
;
1019 Switches
: Config_Switches_Type
;
1022 package Instance_Envs
is new Table
.Table
(
1023 Table_Component_Type
=> Instance_Env
,
1024 Table_Index_Type
=> Int
,
1025 Table_Low_Bound
=> 0,
1026 Table_Initial
=> 32,
1027 Table_Increment
=> 100,
1028 Table_Name
=> "Instance_Envs");
1030 procedure Restore_Private_Views
1031 (Pack_Id
: Entity_Id
;
1032 Is_Package
: Boolean := True);
1033 -- Restore the private views of external types, and unmark the generic
1034 -- renamings of actuals, so that they become compatible subtypes again.
1035 -- For subprograms, Pack_Id is the package constructed to hold the
1038 procedure Switch_View
(T
: Entity_Id
);
1039 -- Switch the partial and full views of a type and its private
1040 -- dependents (i.e. its subtypes and derived types).
1042 ------------------------------------
1043 -- Structures for Error Reporting --
1044 ------------------------------------
1046 Instantiation_Node
: Node_Id
;
1047 -- Used by subprograms that validate instantiation of formal parameters
1048 -- where there might be no actual on which to place the error message.
1049 -- Also used to locate the instantiation node for generic subunits.
1051 Instantiation_Error
: exception;
1052 -- When there is a semantic error in the generic parameter matching,
1053 -- there is no point in continuing the instantiation, because the
1054 -- number of cascaded errors is unpredictable. This exception aborts
1055 -- the instantiation process altogether.
1057 S_Adjustment
: Sloc_Adjustment
;
1058 -- Offset created for each node in an instantiation, in order to keep
1059 -- track of the source position of the instantiation in each of its nodes.
1060 -- A subsequent semantic error or warning on a construct of the instance
1061 -- points to both places: the original generic node, and the point of
1062 -- instantiation. See Sinput and Sinput.L for additional details.
1064 ------------------------------------------------------------
1065 -- Data structure for keeping track when inside a Generic --
1066 ------------------------------------------------------------
1068 -- The following table is used to save values of the Inside_A_Generic
1069 -- flag (see spec of Sem) when they are saved by Start_Generic.
1071 package Generic_Flags
is new Table
.Table
(
1072 Table_Component_Type
=> Boolean,
1073 Table_Index_Type
=> Int
,
1074 Table_Low_Bound
=> 0,
1075 Table_Initial
=> 32,
1076 Table_Increment
=> 200,
1077 Table_Name
=> "Generic_Flags");
1079 ---------------------------
1080 -- Abandon_Instantiation --
1081 ---------------------------
1083 procedure Abandon_Instantiation
(N
: Node_Id
) is
1085 Error_Msg_N
("\instantiation abandoned!", N
);
1086 raise Instantiation_Error
;
1087 end Abandon_Instantiation
;
1089 ----------------------------------
1090 -- Adjust_Inherited_Pragma_Sloc --
1091 ----------------------------------
1093 procedure Adjust_Inherited_Pragma_Sloc
(N
: Node_Id
) is
1095 Adjust_Instantiation_Sloc
(N
, S_Adjustment
);
1096 end Adjust_Inherited_Pragma_Sloc
;
1098 --------------------------
1099 -- Analyze_Associations --
1100 --------------------------
1102 function Analyze_Associations
1105 F_Copy
: List_Id
) return List_Id
1107 Actuals_To_Freeze
: constant Elist_Id
:= New_Elmt_List
;
1108 Assoc_List
: constant List_Id
:= New_List
;
1109 Default_Actuals
: constant List_Id
:= New_List
;
1110 Gen_Unit
: constant Entity_Id
:=
1111 Defining_Entity
(Parent
(F_Copy
));
1115 Analyzed_Formal
: Node_Id
;
1116 First_Named
: Node_Id
:= Empty
;
1118 Match
: Node_Id
:= Empty
;
1120 Saved_Formal
: Node_Id
;
1122 Default_Formals
: constant List_Id
:= New_List
;
1123 -- If an Others_Choice is present, some of the formals may be defaulted.
1124 -- To simplify the treatment of visibility in an instance, we introduce
1125 -- individual defaults for each such formal. These defaults are
1126 -- appended to the list of associations and replace the Others_Choice.
1128 Found_Assoc
: Node_Id
;
1129 -- Association for the current formal being match. Empty if there are
1130 -- no remaining actuals, or if there is no named association with the
1131 -- name of the formal.
1133 Is_Named_Assoc
: Boolean;
1134 Num_Matched
: Nat
:= 0;
1135 Num_Actuals
: Nat
:= 0;
1137 Others_Present
: Boolean := False;
1138 Others_Choice
: Node_Id
:= Empty
;
1139 -- In Ada 2005, indicates partial parameterization of a formal
1140 -- package. As usual an other association must be last in the list.
1142 procedure Build_Subprogram_Wrappers
;
1143 -- Ada 2022: AI12-0272 introduces pre/postconditions for formal
1144 -- subprograms. The implementation of making the formal into a renaming
1145 -- of the actual does not work, given that subprogram renaming cannot
1146 -- carry aspect specifications. Instead we must create subprogram
1147 -- wrappers whose body is a call to the actual, and whose declaration
1148 -- carries the aspects of the formal.
1150 procedure Check_Fixed_Point_Actual
(Actual
: Node_Id
);
1151 -- Warn if an actual fixed-point type has user-defined arithmetic
1152 -- operations, but there is no corresponding formal in the generic,
1153 -- in which case the predefined operations will be used. This merits
1154 -- a warning because of the special semantics of fixed point ops.
1156 procedure Check_Overloaded_Formal_Subprogram
(Formal
: Node_Id
);
1157 -- Apply RM 12.3(9): if a formal subprogram is overloaded, the instance
1158 -- cannot have a named association for it. AI05-0025 extends this rule
1159 -- to formals of formal packages by AI05-0025, and it also applies to
1160 -- box-initialized formals.
1162 function Has_Fully_Defined_Profile
(Subp
: Entity_Id
) return Boolean;
1163 -- Determine whether the parameter types and the return type of Subp
1164 -- are fully defined at the point of instantiation.
1166 function Matching_Actual
1168 A_F
: Entity_Id
) return Node_Id
;
1169 -- Find actual that corresponds to a given formal parameter. If the
1170 -- actuals are positional, return the next one, if any. If the actuals
1171 -- are named, scan the parameter associations to find the right one.
1172 -- A_F is the corresponding entity in the analyzed generic, which is
1173 -- placed on the selector name.
1175 -- In Ada 2005, a named association may be given with a box, in which
1176 -- case Matching_Actual sets Found_Assoc to the generic association,
1177 -- but return Empty for the actual itself. In this case the code below
1178 -- creates a corresponding declaration for the formal.
1180 function Partial_Parameterization
return Boolean;
1181 -- Ada 2005: if no match is found for a given formal, check if the
1182 -- association for it includes a box, or whether the associations
1183 -- include an Others clause.
1185 procedure Process_Default
(Formal
: Node_Id
);
1186 -- Add a copy of the declaration of a generic formal to the list of
1187 -- associations, and add an explicit box association for its entity
1188 -- if there is none yet, and the default comes from an Others_Choice.
1190 function Renames_Standard_Subprogram
(Subp
: Entity_Id
) return Boolean;
1191 -- Determine whether Subp renames one of the subprograms defined in the
1192 -- generated package Standard.
1194 procedure Set_Analyzed_Formal
;
1195 -- Find the node in the generic copy that corresponds to a given formal.
1196 -- The semantic information on this node is used to perform legality
1197 -- checks on the actuals. Because semantic analysis can introduce some
1198 -- anonymous entities or modify the declaration node itself, the
1199 -- correspondence between the two lists is not one-one. In addition to
1200 -- anonymous types, the presence a formal equality will introduce an
1201 -- implicit declaration for the corresponding inequality.
1203 -------------------------------
1204 -- Build_Subprogram_Wrappers --
1205 -------------------------------
1207 procedure Build_Subprogram_Wrappers
is
1208 function Adjust_Aspect_Sloc
(N
: Node_Id
) return Traverse_Result
;
1209 -- Adjust sloc so that errors located at N will be reported with
1210 -- information about the instance and not just about the generic.
1212 ------------------------
1213 -- Adjust_Aspect_Sloc --
1214 ------------------------
1216 function Adjust_Aspect_Sloc
(N
: Node_Id
) return Traverse_Result
is
1218 Adjust_Instantiation_Sloc
(N
, S_Adjustment
);
1220 end Adjust_Aspect_Sloc
;
1222 procedure Adjust_Aspect_Slocs
is new
1223 Traverse_Proc
(Adjust_Aspect_Sloc
);
1225 Formal
: constant Entity_Id
:=
1226 Defining_Unit_Name
(Specification
(Analyzed_Formal
));
1227 Aspect_Spec
: Node_Id
;
1228 Decl_Node
: Node_Id
;
1229 Actual_Name
: Node_Id
;
1231 -- Start of processing for Build_Subprogram_Wrappers
1234 -- Create declaration for wrapper subprogram
1235 -- The actual can be overloaded, in which case it will be
1236 -- resolved when the call in the wrapper body is analyzed.
1237 -- We attach the possible interpretations of the actual to
1238 -- the name to be used in the call in the wrapper body.
1240 if Is_Entity_Name
(Match
) then
1241 Actual_Name
:= New_Occurrence_Of
(Entity
(Match
), Sloc
(Match
));
1243 if Is_Overloaded
(Match
) then
1244 Save_Interps
(Match
, Actual_Name
);
1248 -- Use renaming declaration created when analyzing actual.
1249 -- This may be incomplete if there are several formal
1250 -- subprograms whose actual is an attribute ???
1253 Renaming_Decl
: constant Node_Id
:= Last
(Assoc_List
);
1256 Actual_Name
:= New_Occurrence_Of
1257 (Defining_Entity
(Renaming_Decl
), Sloc
(Match
));
1258 Set_Etype
(Actual_Name
, Get_Instance_Of
(Etype
(Formal
)));
1262 Decl_Node
:= Build_Subprogram_Decl_Wrapper
(Formal
);
1264 -- Transfer aspect specifications from formal subprogram to wrapper
1266 Set_Aspect_Specifications
(Decl_Node
,
1267 New_Copy_List_Tree
(Aspect_Specifications
(Analyzed_Formal
)));
1269 Aspect_Spec
:= First
(Aspect_Specifications
(Decl_Node
));
1270 while Present
(Aspect_Spec
) loop
1271 Adjust_Aspect_Slocs
(Aspect_Spec
);
1272 Set_Analyzed
(Aspect_Spec
, False);
1276 Append_To
(Assoc_List
, Decl_Node
);
1278 -- Create corresponding body, and append it to association list
1279 -- that appears at the head of the declarations in the instance.
1280 -- The subprogram may be called in the analysis of subsequent
1283 Append_To
(Assoc_List
,
1284 Build_Subprogram_Body_Wrapper
(Formal
, Actual_Name
));
1285 end Build_Subprogram_Wrappers
;
1287 ----------------------------------------
1288 -- Check_Overloaded_Formal_Subprogram --
1289 ----------------------------------------
1291 procedure Check_Overloaded_Formal_Subprogram
(Formal
: Node_Id
) is
1292 Temp_Formal
: Node_Id
;
1295 Temp_Formal
:= First
(Formals
);
1296 while Present
(Temp_Formal
) loop
1297 if Nkind
(Temp_Formal
) in N_Formal_Subprogram_Declaration
1298 and then Temp_Formal
/= Formal
1300 Chars
(Defining_Unit_Name
(Specification
(Formal
))) =
1301 Chars
(Defining_Unit_Name
(Specification
(Temp_Formal
)))
1303 if Present
(Found_Assoc
) then
1305 ("named association not allowed for overloaded formal",
1310 ("named association not allowed for overloaded formal",
1314 Abandon_Instantiation
(Instantiation_Node
);
1319 end Check_Overloaded_Formal_Subprogram
;
1321 -------------------------------
1322 -- Check_Fixed_Point_Actual --
1323 -------------------------------
1325 procedure Check_Fixed_Point_Actual
(Actual
: Node_Id
) is
1326 Typ
: constant Entity_Id
:= Entity
(Actual
);
1327 Prims
: constant Elist_Id
:= Collect_Primitive_Operations
(Typ
);
1333 -- Locate primitive operations of the type that are arithmetic
1336 Elem
:= First_Elmt
(Prims
);
1337 while Present
(Elem
) loop
1338 if Nkind
(Node
(Elem
)) = N_Defining_Operator_Symbol
then
1340 -- Check whether the generic unit has a formal subprogram of
1341 -- the same name. This does not check types but is good enough
1342 -- to justify a warning.
1344 Formal
:= First_Non_Pragma
(Formals
);
1345 Op
:= Alias
(Node
(Elem
));
1347 while Present
(Formal
) loop
1348 if Nkind
(Formal
) = N_Formal_Concrete_Subprogram_Declaration
1349 and then Chars
(Defining_Entity
(Formal
)) =
1354 elsif Nkind
(Formal
) = N_Formal_Package_Declaration
then
1360 -- Locate corresponding actual, and check whether it
1361 -- includes a fixed-point type.
1363 Assoc
:= First
(Assoc_List
);
1364 while Present
(Assoc
) loop
1366 Nkind
(Assoc
) = N_Package_Renaming_Declaration
1367 and then Chars
(Defining_Unit_Name
(Assoc
)) =
1368 Chars
(Defining_Identifier
(Formal
));
1373 if Present
(Assoc
) then
1375 -- If formal package declares a fixed-point type,
1376 -- and the user-defined operator is derived from
1377 -- a generic instance package, the fixed-point type
1378 -- does not use the corresponding predefined op.
1380 Ent
:= First_Entity
(Entity
(Name
(Assoc
)));
1381 while Present
(Ent
) loop
1382 if Is_Fixed_Point_Type
(Ent
)
1383 and then Present
(Op
)
1384 and then Is_Generic_Instance
(Scope
(Op
))
1399 Error_Msg_Sloc
:= Sloc
(Node
(Elem
));
1401 ("?instance uses predefined operation, not primitive "
1402 & "operation&#", Actual
, Node
(Elem
));
1408 end Check_Fixed_Point_Actual
;
1410 -------------------------------
1411 -- Has_Fully_Defined_Profile --
1412 -------------------------------
1414 function Has_Fully_Defined_Profile
(Subp
: Entity_Id
) return Boolean is
1415 function Is_Fully_Defined_Type
(Typ
: Entity_Id
) return Boolean;
1416 -- Determine whethet type Typ is fully defined
1418 ---------------------------
1419 -- Is_Fully_Defined_Type --
1420 ---------------------------
1422 function Is_Fully_Defined_Type
(Typ
: Entity_Id
) return Boolean is
1424 -- A private type without a full view is not fully defined
1426 if Is_Private_Type
(Typ
)
1427 and then No
(Full_View
(Typ
))
1431 -- An incomplete type is never fully defined
1433 elsif Is_Incomplete_Type
(Typ
) then
1436 -- All other types are fully defined
1441 end Is_Fully_Defined_Type
;
1443 -- Local declarations
1447 -- Start of processing for Has_Fully_Defined_Profile
1450 -- Check the parameters
1452 Param
:= First_Formal
(Subp
);
1453 while Present
(Param
) loop
1454 if not Is_Fully_Defined_Type
(Etype
(Param
)) then
1458 Next_Formal
(Param
);
1461 -- Check the return type
1463 return Is_Fully_Defined_Type
(Etype
(Subp
));
1464 end Has_Fully_Defined_Profile
;
1466 ---------------------
1467 -- Matching_Actual --
1468 ---------------------
1470 function Matching_Actual
1472 A_F
: Entity_Id
) return Node_Id
1478 Is_Named_Assoc
:= False;
1480 -- End of list of purely positional parameters
1482 if No
(Actual
) or else Nkind
(Actual
) = N_Others_Choice
then
1483 Found_Assoc
:= Empty
;
1486 -- Case of positional parameter corresponding to current formal
1488 elsif No
(Selector_Name
(Actual
)) then
1489 Found_Assoc
:= Actual
;
1490 Act
:= Explicit_Generic_Actual_Parameter
(Actual
);
1491 Num_Matched
:= Num_Matched
+ 1;
1494 -- Otherwise scan list of named actuals to find the one with the
1495 -- desired name. All remaining actuals have explicit names.
1498 Is_Named_Assoc
:= True;
1499 Found_Assoc
:= Empty
;
1503 while Present
(Actual
) loop
1504 if Nkind
(Actual
) = N_Others_Choice
then
1505 Found_Assoc
:= Empty
;
1508 elsif Chars
(Selector_Name
(Actual
)) = Chars
(F
) then
1509 Set_Entity
(Selector_Name
(Actual
), A_F
);
1510 Set_Etype
(Selector_Name
(Actual
), Etype
(A_F
));
1511 Generate_Reference
(A_F
, Selector_Name
(Actual
));
1513 Found_Assoc
:= Actual
;
1514 Act
:= Explicit_Generic_Actual_Parameter
(Actual
);
1515 Num_Matched
:= Num_Matched
+ 1;
1523 -- Reset for subsequent searches. In most cases the named
1524 -- associations are in order. If they are not, we reorder them
1525 -- to avoid scanning twice the same actual. This is not just a
1526 -- question of efficiency: there may be multiple defaults with
1527 -- boxes that have the same name. In a nested instantiation we
1528 -- insert actuals for those defaults, and cannot rely on their
1529 -- names to disambiguate them.
1531 if Actual
= First_Named
then
1534 elsif Present
(Actual
) then
1535 Insert_Before
(First_Named
, Remove_Next
(Prev
));
1538 Actual
:= First_Named
;
1541 if Is_Entity_Name
(Act
) and then Present
(Entity
(Act
)) then
1542 Set_Used_As_Generic_Actual
(Entity
(Act
));
1546 end Matching_Actual
;
1548 ------------------------------
1549 -- Partial_Parameterization --
1550 ------------------------------
1552 function Partial_Parameterization
return Boolean is
1554 return Others_Present
1555 or else (Present
(Found_Assoc
) and then Box_Present
(Found_Assoc
));
1556 end Partial_Parameterization
;
1558 ---------------------
1559 -- Process_Default --
1560 ---------------------
1562 procedure Process_Default
(Formal
: Node_Id
) is
1563 Loc
: constant Source_Ptr
:= Sloc
(I_Node
);
1564 F_Id
: constant Entity_Id
:= Defining_Entity
(Formal
);
1570 -- Append copy of formal declaration to associations, and create new
1571 -- defining identifier for it.
1573 Decl
:= New_Copy_Tree
(Formal
);
1574 Id
:= Make_Defining_Identifier
(Sloc
(F_Id
), Chars
(F_Id
));
1576 if Nkind
(Formal
) in N_Formal_Subprogram_Declaration
then
1577 Set_Defining_Unit_Name
(Specification
(Decl
), Id
);
1580 Set_Defining_Identifier
(Decl
, Id
);
1583 Append
(Decl
, Assoc_List
);
1585 if No
(Found_Assoc
) then
1587 Make_Generic_Association
(Loc
,
1589 New_Occurrence_Of
(Id
, Loc
),
1590 Explicit_Generic_Actual_Parameter
=> Empty
);
1591 Set_Box_Present
(Default
);
1592 Append
(Default
, Default_Formals
);
1594 end Process_Default
;
1596 ---------------------------------
1597 -- Renames_Standard_Subprogram --
1598 ---------------------------------
1600 function Renames_Standard_Subprogram
(Subp
: Entity_Id
) return Boolean is
1605 while Present
(Id
) loop
1606 if Scope
(Id
) = Standard_Standard
then
1614 end Renames_Standard_Subprogram
;
1616 -------------------------
1617 -- Set_Analyzed_Formal --
1618 -------------------------
1620 procedure Set_Analyzed_Formal
is
1624 while Present
(Analyzed_Formal
) loop
1625 Kind
:= Nkind
(Analyzed_Formal
);
1627 case Nkind
(Formal
) is
1628 when N_Formal_Subprogram_Declaration
=>
1629 exit when Kind
in N_Formal_Subprogram_Declaration
1632 (Defining_Unit_Name
(Specification
(Formal
))) =
1634 (Defining_Unit_Name
(Specification
(Analyzed_Formal
)));
1636 when N_Formal_Package_Declaration
=>
1637 exit when Kind
in N_Formal_Package_Declaration
1638 | N_Generic_Package_Declaration
1639 | N_Package_Declaration
;
1641 when N_Use_Package_Clause
1648 -- Skip freeze nodes, and nodes inserted to replace
1649 -- unrecognized pragmas.
1652 Kind
not in N_Formal_Subprogram_Declaration
1653 and then Kind
not in N_Subprogram_Declaration
1657 and then Chars
(Defining_Identifier
(Formal
)) =
1658 Chars
(Defining_Identifier
(Analyzed_Formal
));
1661 Next
(Analyzed_Formal
);
1663 end Set_Analyzed_Formal
;
1665 -- Start of processing for Analyze_Associations
1668 Actuals
:= Generic_Associations
(I_Node
);
1670 if Present
(Actuals
) then
1672 -- Check for an Others choice, indicating a partial parameterization
1673 -- for a formal package.
1675 Actual
:= First
(Actuals
);
1676 while Present
(Actual
) loop
1677 if Nkind
(Actual
) = N_Others_Choice
then
1678 Others_Present
:= True;
1679 Others_Choice
:= Actual
;
1681 if Present
(Next
(Actual
)) then
1682 Error_Msg_N
("OTHERS must be last association", Actual
);
1685 -- This subprogram is used both for formal packages and for
1686 -- instantiations. For the latter, associations must all be
1689 if Nkind
(I_Node
) /= N_Formal_Package_Declaration
1690 and then Comes_From_Source
(I_Node
)
1693 ("OTHERS association not allowed in an instance",
1697 -- In any case, nothing to do after the others association
1701 elsif Box_Present
(Actual
)
1702 and then Comes_From_Source
(I_Node
)
1703 and then Nkind
(I_Node
) /= N_Formal_Package_Declaration
1706 ("box association not allowed in an instance", Actual
);
1712 -- If named associations are present, save first named association
1713 -- (it may of course be Empty) to facilitate subsequent name search.
1715 First_Named
:= First
(Actuals
);
1716 while Present
(First_Named
)
1717 and then Nkind
(First_Named
) /= N_Others_Choice
1718 and then No
(Selector_Name
(First_Named
))
1720 Num_Actuals
:= Num_Actuals
+ 1;
1725 Named
:= First_Named
;
1726 while Present
(Named
) loop
1727 if Nkind
(Named
) /= N_Others_Choice
1728 and then No
(Selector_Name
(Named
))
1730 Error_Msg_N
("invalid positional actual after named one", Named
);
1731 Abandon_Instantiation
(Named
);
1734 -- A named association may lack an actual parameter, if it was
1735 -- introduced for a default subprogram that turns out to be local
1736 -- to the outer instantiation. If it has a box association it must
1737 -- correspond to some formal in the generic.
1739 if Nkind
(Named
) /= N_Others_Choice
1740 and then (Present
(Explicit_Generic_Actual_Parameter
(Named
))
1741 or else Box_Present
(Named
))
1743 Num_Actuals
:= Num_Actuals
+ 1;
1749 if Present
(Formals
) then
1750 Formal
:= First_Non_Pragma
(Formals
);
1751 Analyzed_Formal
:= First_Non_Pragma
(F_Copy
);
1753 if Present
(Actuals
) then
1754 Actual
:= First
(Actuals
);
1756 -- All formals should have default values
1762 while Present
(Formal
) loop
1763 Set_Analyzed_Formal
;
1764 Saved_Formal
:= Next_Non_Pragma
(Formal
);
1766 case Nkind
(Formal
) is
1767 when N_Formal_Object_Declaration
=>
1770 (Defining_Identifier
(Formal
),
1771 Defining_Identifier
(Analyzed_Formal
));
1773 if No
(Match
) and then Partial_Parameterization
then
1774 Process_Default
(Formal
);
1778 (Instantiate_Object
(Formal
, Match
, Analyzed_Formal
),
1781 -- For a defaulted in_parameter, create an entry in the
1782 -- the list of defaulted actuals, for GNATprove use. Do
1783 -- not included these defaults for an instance nested
1784 -- within a generic, because the defaults are also used
1785 -- in the analysis of the enclosing generic, and only
1786 -- defaulted subprograms are relevant there.
1788 if No
(Match
) and then not Inside_A_Generic
then
1789 Append_To
(Default_Actuals
,
1790 Make_Generic_Association
(Sloc
(I_Node
),
1793 (Defining_Identifier
(Formal
), Sloc
(I_Node
)),
1794 Explicit_Generic_Actual_Parameter
=>
1795 New_Copy_Tree
(Default_Expression
(Formal
))));
1799 -- If the object is a call to an expression function, this
1800 -- is a freezing point for it.
1802 if Is_Entity_Name
(Match
)
1803 and then Present
(Entity
(Match
))
1805 (Original_Node
(Unit_Declaration_Node
(Entity
(Match
))))
1806 = N_Expression_Function
1808 Append_Elmt
(Entity
(Match
), Actuals_To_Freeze
);
1811 when N_Formal_Type_Declaration
=>
1814 (Defining_Identifier
(Formal
),
1815 Defining_Identifier
(Analyzed_Formal
));
1818 if Partial_Parameterization
then
1819 Process_Default
(Formal
);
1821 elsif Present
(Default_Subtype_Mark
(Formal
)) then
1822 Match
:= New_Copy
(Default_Subtype_Mark
(Formal
));
1825 (Formal
, Match
, Analyzed_Formal
, Assoc_List
),
1827 Append_Elmt
(Entity
(Match
), Actuals_To_Freeze
);
1830 Error_Msg_Sloc
:= Sloc
(Gen_Unit
);
1833 Instantiation_Node
, Defining_Identifier
(Formal
));
1835 ("\in instantiation of & declared#",
1836 Instantiation_Node
, Gen_Unit
);
1837 Abandon_Instantiation
(Instantiation_Node
);
1844 (Formal
, Match
, Analyzed_Formal
, Assoc_List
),
1847 -- Warn when an actual is a fixed-point with user-
1848 -- defined promitives. The warning is superfluous
1849 -- if the formal is private, because there can be
1850 -- no arithmetic operations in the generic so there
1851 -- no danger of confusion.
1853 if Is_Fixed_Point_Type
(Entity
(Match
))
1854 and then not Is_Private_Type
1855 (Defining_Identifier
(Analyzed_Formal
))
1857 Check_Fixed_Point_Actual
(Match
);
1860 -- An instantiation is a freeze point for the actuals,
1861 -- unless this is a rewritten formal package, or the
1862 -- formal is an Ada 2012 formal incomplete type.
1864 if Nkind
(I_Node
) = N_Formal_Package_Declaration
1866 (Ada_Version
>= Ada_2012
1868 Ekind
(Defining_Identifier
(Analyzed_Formal
)) =
1874 Append_Elmt
(Entity
(Match
), Actuals_To_Freeze
);
1878 -- A remote access-to-class-wide type is not a legal actual
1879 -- for a generic formal of an access type (E.2.2(17/2)).
1880 -- In GNAT an exception to this rule is introduced when
1881 -- the formal is marked as remote using implementation
1882 -- defined aspect/pragma Remote_Access_Type. In that case
1883 -- the actual must be remote as well.
1885 -- If the current instantiation is the construction of a
1886 -- local copy for a formal package the actuals may be
1887 -- defaulted, and there is no matching actual to check.
1889 if Nkind
(Analyzed_Formal
) = N_Formal_Type_Declaration
1891 Nkind
(Formal_Type_Definition
(Analyzed_Formal
)) =
1892 N_Access_To_Object_Definition
1893 and then Present
(Match
)
1896 Formal_Ent
: constant Entity_Id
:=
1897 Defining_Identifier
(Analyzed_Formal
);
1899 if Is_Remote_Access_To_Class_Wide_Type
(Entity
(Match
))
1900 = Is_Remote_Types
(Formal_Ent
)
1902 -- Remoteness of formal and actual match
1906 elsif Is_Remote_Types
(Formal_Ent
) then
1908 -- Remote formal, non-remote actual
1911 ("actual for& must be remote", Match
, Formal_Ent
);
1914 -- Non-remote formal, remote actual
1917 ("actual for& may not be remote",
1923 when N_Formal_Subprogram_Declaration
=>
1926 (Defining_Unit_Name
(Specification
(Formal
)),
1927 Defining_Unit_Name
(Specification
(Analyzed_Formal
)));
1929 -- If the formal subprogram has the same name as another
1930 -- formal subprogram of the generic, then a named
1931 -- association is illegal (12.3(9)). Exclude named
1932 -- associations that are generated for a nested instance.
1935 and then Is_Named_Assoc
1936 and then Comes_From_Source
(Found_Assoc
)
1938 Check_Overloaded_Formal_Subprogram
(Formal
);
1941 -- If there is no corresponding actual, this may be case
1942 -- of partial parameterization, or else the formal has a
1943 -- default or a box.
1945 if No
(Match
) and then Partial_Parameterization
then
1946 Process_Default
(Formal
);
1948 if Nkind
(I_Node
) = N_Formal_Package_Declaration
then
1949 Check_Overloaded_Formal_Subprogram
(Formal
);
1953 Append_To
(Assoc_List
,
1954 Instantiate_Formal_Subprogram
1955 (Formal
, Match
, Analyzed_Formal
));
1957 -- If formal subprogram has contracts, create wrappers
1958 -- for it. This is an expansion activity that cannot
1959 -- take place e.g. within an enclosing generic unit.
1961 if Has_Contracts
(Analyzed_Formal
)
1962 and then (Expander_Active
or GNATprove_Mode
)
1964 Build_Subprogram_Wrappers
;
1967 -- An instantiation is a freeze point for the actuals,
1968 -- unless this is a rewritten formal package.
1970 if Nkind
(I_Node
) /= N_Formal_Package_Declaration
1971 and then Nkind
(Match
) = N_Identifier
1972 and then Is_Subprogram
(Entity
(Match
))
1974 -- The actual subprogram may rename a routine defined
1975 -- in Standard. Avoid freezing such renamings because
1976 -- subprograms coming from Standard cannot be frozen.
1979 not Renames_Standard_Subprogram
(Entity
(Match
))
1981 -- If the actual subprogram comes from a different
1982 -- unit, it is already frozen, either by a body in
1983 -- that unit or by the end of the declarative part
1984 -- of the unit. This check avoids the freezing of
1985 -- subprograms defined in Standard which are used
1986 -- as generic actuals.
1988 and then In_Same_Code_Unit
(Entity
(Match
), I_Node
)
1989 and then Has_Fully_Defined_Profile
(Entity
(Match
))
1991 -- Mark the subprogram as having a delayed freeze
1992 -- since this may be an out-of-order action.
1994 Set_Has_Delayed_Freeze
(Entity
(Match
));
1995 Append_Elmt
(Entity
(Match
), Actuals_To_Freeze
);
1999 -- If this is a nested generic, preserve default for later
2000 -- instantiations. We do this as well for GNATprove use,
2001 -- so that the list of generic associations is complete.
2003 if No
(Match
) and then Box_Present
(Formal
) then
2005 Subp
: constant Entity_Id
:=
2007 (Specification
(Last
(Assoc_List
)));
2010 Append_To
(Default_Actuals
,
2011 Make_Generic_Association
(Sloc
(I_Node
),
2013 New_Occurrence_Of
(Subp
, Sloc
(I_Node
)),
2014 Explicit_Generic_Actual_Parameter
=>
2015 New_Occurrence_Of
(Subp
, Sloc
(I_Node
))));
2019 when N_Formal_Package_Declaration
=>
2020 -- The name of the formal package may be hidden by the
2021 -- formal parameter itself.
2023 if Error_Posted
(Analyzed_Formal
) then
2024 Abandon_Instantiation
(Instantiation_Node
);
2029 (Defining_Identifier
(Formal
),
2031 (Original_Node
(Analyzed_Formal
)));
2035 if Partial_Parameterization
then
2036 Process_Default
(Formal
);
2039 Error_Msg_Sloc
:= Sloc
(Gen_Unit
);
2042 Instantiation_Node
, Defining_Identifier
(Formal
));
2044 ("\in instantiation of & declared#",
2045 Instantiation_Node
, Gen_Unit
);
2047 Abandon_Instantiation
(Instantiation_Node
);
2053 (Instantiate_Formal_Package
2054 (Formal
, Match
, Analyzed_Formal
),
2057 -- Determine whether the actual package needs an explicit
2058 -- freeze node. This is only the case if the actual is
2059 -- declared in the same unit and has a body. Normally
2060 -- packages do not have explicit freeze nodes, and gigi
2061 -- only uses them to elaborate entities in a package
2064 Explicit_Freeze_Check
: declare
2065 Actual
: constant Entity_Id
:= Entity
(Match
);
2066 Gen_Par
: Entity_Id
;
2068 Needs_Freezing
: Boolean;
2071 procedure Check_Generic_Parent
;
2072 -- The actual may be an instantiation of a unit
2073 -- declared in a previous instantiation. If that
2074 -- one is also in the current compilation, it must
2075 -- itself be frozen before the actual. The actual
2076 -- may be an instantiation of a generic child unit,
2077 -- in which case the same applies to the instance
2078 -- of the parent which must be frozen before the
2080 -- Should this itself be recursive ???
2082 --------------------------
2083 -- Check_Generic_Parent --
2084 --------------------------
2086 procedure Check_Generic_Parent
is
2087 Inst
: constant Node_Id
:=
2088 Get_Unit_Instantiation_Node
(Actual
);
2094 if Nkind
(Parent
(Actual
)) = N_Package_Specification
2096 Par
:= Scope
(Generic_Parent
(Parent
(Actual
)));
2098 if Is_Generic_Instance
(Par
) then
2101 -- If the actual is a child generic unit, check
2102 -- whether the instantiation of the parent is
2103 -- also local and must also be frozen now. We
2104 -- must retrieve the instance node to locate the
2105 -- parent instance if any.
2107 elsif Ekind
(Par
) = E_Generic_Package
2108 and then Is_Child_Unit
(Gen_Par
)
2109 and then Ekind
(Scope
(Gen_Par
)) =
2112 if Nkind
(Inst
) = N_Package_Instantiation
2113 and then Nkind
(Name
(Inst
)) =
2116 -- Retrieve entity of parent instance
2118 Par
:= Entity
(Prefix
(Name
(Inst
)));
2127 and then Is_Generic_Instance
(Par
)
2128 and then Scope
(Par
) = Current_Scope
2130 (No
(Freeze_Node
(Par
))
2132 not Is_List_Member
(Freeze_Node
(Par
)))
2134 Set_Has_Delayed_Freeze
(Par
);
2135 Append_Elmt
(Par
, Actuals_To_Freeze
);
2137 end Check_Generic_Parent
;
2139 -- Start of processing for Explicit_Freeze_Check
2142 if Present
(Renamed_Entity
(Actual
)) then
2144 Generic_Parent
(Specification
2145 (Unit_Declaration_Node
2146 (Renamed_Entity
(Actual
))));
2149 Generic_Parent
(Specification
2150 (Unit_Declaration_Node
(Actual
)));
2153 if not Expander_Active
2154 or else not Has_Completion
(Actual
)
2155 or else not In_Same_Source_Unit
(I_Node
, Actual
)
2156 or else Is_Frozen
(Actual
)
2158 (Present
(Renamed_Entity
(Actual
))
2160 not In_Same_Source_Unit
2161 (I_Node
, (Renamed_Entity
(Actual
))))
2166 -- Finally we want to exclude such freeze nodes
2167 -- from statement sequences, which freeze
2168 -- everything before them.
2169 -- Is this strictly necessary ???
2171 Needs_Freezing
:= True;
2173 P
:= Parent
(I_Node
);
2174 while Nkind
(P
) /= N_Compilation_Unit
loop
2175 if Nkind
(P
) = N_Handled_Sequence_Of_Statements
2177 Needs_Freezing
:= False;
2184 if Needs_Freezing
then
2185 Check_Generic_Parent
;
2187 -- If the actual is a renaming of a proper
2188 -- instance of the formal package, indicate
2189 -- that it is the instance that must be frozen.
2191 if Nkind
(Parent
(Actual
)) =
2192 N_Package_Renaming_Declaration
2194 Set_Has_Delayed_Freeze
2195 (Renamed_Entity
(Actual
));
2197 (Renamed_Entity
(Actual
),
2200 Set_Has_Delayed_Freeze
(Actual
);
2201 Append_Elmt
(Actual
, Actuals_To_Freeze
);
2205 end Explicit_Freeze_Check
;
2208 -- For use type and use package appearing in the generic part,
2209 -- we have already copied them, so we can just move them where
2210 -- they belong (we mustn't recopy them since this would mess up
2211 -- the Sloc values).
2213 when N_Use_Package_Clause
2216 if Nkind
(Original_Node
(I_Node
)) =
2217 N_Formal_Package_Declaration
2219 Append
(New_Copy_Tree
(Formal
), Assoc_List
);
2222 Append
(Formal
, Assoc_List
);
2226 raise Program_Error
;
2229 -- Check here the correct use of Ghost entities in generic
2230 -- instantiations, as now the generic has been resolved and
2231 -- we know which formal generic parameters are ghost (SPARK
2234 if Nkind
(Formal
) not in N_Use_Package_Clause
2237 Check_Ghost_Context_In_Generic_Association
2239 Formal
=> Defining_Entity
(Analyzed_Formal
));
2242 Formal
:= Saved_Formal
;
2243 Next_Non_Pragma
(Analyzed_Formal
);
2246 if Num_Actuals
> Num_Matched
then
2247 Error_Msg_Sloc
:= Sloc
(Gen_Unit
);
2249 if Present
(Selector_Name
(Actual
)) then
2251 ("unmatched actual &", Actual
, Selector_Name
(Actual
));
2253 ("\in instantiation of & declared#", Actual
, Gen_Unit
);
2256 ("unmatched actual in instantiation of & declared#",
2261 elsif Present
(Actuals
) then
2263 ("too many actuals in generic instantiation", Instantiation_Node
);
2266 -- An instantiation freezes all generic actuals. The only exceptions
2267 -- to this are incomplete types and subprograms which are not fully
2268 -- defined at the point of instantiation.
2271 Elmt
: Elmt_Id
:= First_Elmt
(Actuals_To_Freeze
);
2273 while Present
(Elmt
) loop
2274 Freeze_Before
(I_Node
, Node
(Elmt
));
2279 -- If there are default subprograms, normalize the tree by adding
2280 -- explicit associations for them. This is required if the instance
2281 -- appears within a generic.
2283 if not Is_Empty_List
(Default_Actuals
) then
2288 Default
:= First
(Default_Actuals
);
2289 while Present
(Default
) loop
2290 Mark_Rewrite_Insertion
(Default
);
2294 if No
(Actuals
) then
2295 Set_Generic_Associations
(I_Node
, Default_Actuals
);
2297 Append_List_To
(Actuals
, Default_Actuals
);
2302 -- If this is a formal package, normalize the parameter list by adding
2303 -- explicit box associations for the formals that are covered by an
2306 Append_List
(Default_Formals
, Formals
);
2309 end Analyze_Associations
;
2311 -------------------------------
2312 -- Analyze_Formal_Array_Type --
2313 -------------------------------
2315 procedure Analyze_Formal_Array_Type
2316 (T
: in out Entity_Id
;
2322 -- Treated like a non-generic array declaration, with additional
2327 if Nkind
(Def
) = N_Constrained_Array_Definition
then
2328 DSS
:= First
(Discrete_Subtype_Definitions
(Def
));
2329 while Present
(DSS
) loop
2330 if Nkind
(DSS
) in N_Subtype_Indication
2332 | N_Attribute_Reference
2334 Error_Msg_N
("only a subtype mark is allowed in a formal", DSS
);
2341 Array_Type_Declaration
(T
, Def
);
2342 Set_Is_Generic_Type
(Base_Type
(T
));
2344 if Ekind
(Component_Type
(T
)) = E_Incomplete_Type
2345 and then No
(Full_View
(Component_Type
(T
)))
2347 Error_Msg_N
("premature usage of incomplete type", Def
);
2349 -- Check that range constraint is not allowed on the component type
2350 -- of a generic formal array type (AARM 12.5.3(3))
2352 elsif Is_Internal
(Component_Type
(T
))
2353 and then Present
(Subtype_Indication
(Component_Definition
(Def
)))
2354 and then Nkind
(Original_Node
2355 (Subtype_Indication
(Component_Definition
(Def
)))) =
2356 N_Subtype_Indication
2359 ("in a formal, a subtype indication can only be "
2360 & "a subtype mark (RM 12.5.3(3))",
2361 Subtype_Indication
(Component_Definition
(Def
)));
2364 end Analyze_Formal_Array_Type
;
2366 ---------------------------------------------
2367 -- Analyze_Formal_Decimal_Fixed_Point_Type --
2368 ---------------------------------------------
2370 -- As for other generic types, we create a valid type representation with
2371 -- legal but arbitrary attributes, whose values are never considered
2372 -- static. For all scalar types we introduce an anonymous base type, with
2373 -- the same attributes. We choose the corresponding integer type to be
2374 -- Standard_Integer.
2375 -- Here and in other similar routines, the Sloc of the generated internal
2376 -- type must be the same as the sloc of the defining identifier of the
2377 -- formal type declaration, to provide proper source navigation.
2379 procedure Analyze_Formal_Decimal_Fixed_Point_Type
2383 Loc
: constant Source_Ptr
:= Sloc
(Def
);
2385 Base
: constant Entity_Id
:=
2387 (E_Decimal_Fixed_Point_Type
,
2389 Sloc
(Defining_Identifier
(Parent
(Def
))), 'G');
2391 Int_Base
: constant Entity_Id
:= Standard_Integer
;
2392 Delta_Val
: constant Ureal
:= Ureal_1
;
2393 Digs_Val
: constant Uint
:= Uint_6
;
2395 function Make_Dummy_Bound
return Node_Id
;
2396 -- Return a properly typed universal real literal to use as a bound
2398 ----------------------
2399 -- Make_Dummy_Bound --
2400 ----------------------
2402 function Make_Dummy_Bound
return Node_Id
is
2403 Bound
: constant Node_Id
:= Make_Real_Literal
(Loc
, Ureal_1
);
2405 Set_Etype
(Bound
, Universal_Real
);
2407 end Make_Dummy_Bound
;
2409 -- Start of processing for Analyze_Formal_Decimal_Fixed_Point_Type
2414 Set_Etype
(Base
, Base
);
2415 Set_Size_Info
(Base
, Int_Base
);
2416 Set_RM_Size
(Base
, RM_Size
(Int_Base
));
2417 Set_First_Rep_Item
(Base
, First_Rep_Item
(Int_Base
));
2418 Set_Digits_Value
(Base
, Digs_Val
);
2419 Set_Delta_Value
(Base
, Delta_Val
);
2420 Set_Small_Value
(Base
, Delta_Val
);
2421 Set_Scalar_Range
(Base
,
2423 Low_Bound
=> Make_Dummy_Bound
,
2424 High_Bound
=> Make_Dummy_Bound
));
2426 Set_Is_Generic_Type
(Base
);
2427 Set_Parent
(Base
, Parent
(Def
));
2429 Mutate_Ekind
(T
, E_Decimal_Fixed_Point_Subtype
);
2430 Set_Etype
(T
, Base
);
2431 Set_Size_Info
(T
, Int_Base
);
2432 Set_RM_Size
(T
, RM_Size
(Int_Base
));
2433 Set_First_Rep_Item
(T
, First_Rep_Item
(Int_Base
));
2434 Set_Digits_Value
(T
, Digs_Val
);
2435 Set_Delta_Value
(T
, Delta_Val
);
2436 Set_Small_Value
(T
, Delta_Val
);
2437 Set_Scalar_Range
(T
, Scalar_Range
(Base
));
2438 Set_Is_Constrained
(T
);
2440 Check_Restriction
(No_Fixed_Point
, Def
);
2441 end Analyze_Formal_Decimal_Fixed_Point_Type
;
2443 -------------------------------------------
2444 -- Analyze_Formal_Derived_Interface_Type --
2445 -------------------------------------------
2447 procedure Analyze_Formal_Derived_Interface_Type
2452 Loc
: constant Source_Ptr
:= Sloc
(Def
);
2455 -- Rewrite as a type declaration of a derived type. This ensures that
2456 -- the interface list and primitive operations are properly captured.
2459 Make_Full_Type_Declaration
(Loc
,
2460 Defining_Identifier
=> T
,
2461 Type_Definition
=> Def
));
2463 Set_Is_Generic_Type
(T
);
2464 end Analyze_Formal_Derived_Interface_Type
;
2466 ---------------------------------
2467 -- Analyze_Formal_Derived_Type --
2468 ---------------------------------
2470 procedure Analyze_Formal_Derived_Type
2475 Loc
: constant Source_Ptr
:= Sloc
(Def
);
2476 Unk_Disc
: constant Boolean := Unknown_Discriminants_Present
(N
);
2480 Set_Is_Generic_Type
(T
);
2482 if Private_Present
(Def
) then
2484 Make_Private_Extension_Declaration
(Loc
,
2485 Defining_Identifier
=> T
,
2486 Discriminant_Specifications
=> Discriminant_Specifications
(N
),
2487 Unknown_Discriminants_Present
=> Unk_Disc
,
2488 Subtype_Indication
=> Subtype_Mark
(Def
),
2489 Interface_List
=> Interface_List
(Def
));
2491 Set_Abstract_Present
(New_N
, Abstract_Present
(Def
));
2492 Set_Limited_Present
(New_N
, Limited_Present
(Def
));
2493 Set_Synchronized_Present
(New_N
, Synchronized_Present
(Def
));
2497 Make_Full_Type_Declaration
(Loc
,
2498 Defining_Identifier
=> T
,
2499 Discriminant_Specifications
=>
2500 Discriminant_Specifications
(Parent
(T
)),
2502 Make_Derived_Type_Definition
(Loc
,
2503 Subtype_Indication
=> Subtype_Mark
(Def
)));
2505 Set_Abstract_Present
2506 (Type_Definition
(New_N
), Abstract_Present
(Def
));
2508 (Type_Definition
(New_N
), Limited_Present
(Def
));
2515 if not Is_Composite_Type
(T
) then
2517 ("unknown discriminants not allowed for elementary types", N
);
2519 Set_Has_Unknown_Discriminants
(T
);
2520 Set_Is_Constrained
(T
, False);
2524 -- If the parent type has a known size, so does the formal, which makes
2525 -- legal representation clauses that involve the formal.
2527 Set_Size_Known_At_Compile_Time
2528 (T
, Size_Known_At_Compile_Time
(Entity
(Subtype_Mark
(Def
))));
2529 end Analyze_Formal_Derived_Type
;
2531 ----------------------------------
2532 -- Analyze_Formal_Discrete_Type --
2533 ----------------------------------
2535 -- The operations defined for a discrete types are those of an enumeration
2536 -- type. The size is set to an arbitrary value, for use in analyzing the
2539 procedure Analyze_Formal_Discrete_Type
(T
: Entity_Id
; Def
: Node_Id
) is
2540 Loc
: constant Source_Ptr
:= Sloc
(Def
);
2544 Base
: constant Entity_Id
:=
2546 (E_Floating_Point_Type
, Current_Scope
,
2547 Sloc
(Defining_Identifier
(Parent
(Def
))), 'G');
2551 Mutate_Ekind
(T
, E_Enumeration_Subtype
);
2552 Set_Etype
(T
, Base
);
2554 Reinit_Alignment
(T
);
2555 Set_Is_Generic_Type
(T
);
2556 Set_Is_Constrained
(T
);
2558 -- For semantic analysis, the bounds of the type must be set to some
2559 -- non-static value. The simplest is to create attribute nodes for those
2560 -- bounds, that refer to the type itself. These bounds are never
2561 -- analyzed but serve as place-holders.
2564 Make_Attribute_Reference
(Loc
,
2565 Attribute_Name
=> Name_First
,
2566 Prefix
=> New_Occurrence_Of
(T
, Loc
));
2570 Make_Attribute_Reference
(Loc
,
2571 Attribute_Name
=> Name_Last
,
2572 Prefix
=> New_Occurrence_Of
(T
, Loc
));
2575 Set_Scalar_Range
(T
,
2580 Mutate_Ekind
(Base
, E_Enumeration_Type
);
2581 Set_Etype
(Base
, Base
);
2582 Init_Size
(Base
, 8);
2583 Reinit_Alignment
(Base
);
2584 Set_Is_Generic_Type
(Base
);
2585 Set_Scalar_Range
(Base
, Scalar_Range
(T
));
2586 Set_Parent
(Base
, Parent
(Def
));
2587 end Analyze_Formal_Discrete_Type
;
2589 ----------------------------------
2590 -- Analyze_Formal_Floating_Type --
2591 ---------------------------------
2593 procedure Analyze_Formal_Floating_Type
(T
: Entity_Id
; Def
: Node_Id
) is
2594 Base
: constant Entity_Id
:=
2596 (E_Floating_Point_Type
, Current_Scope
,
2597 Sloc
(Defining_Identifier
(Parent
(Def
))), 'G');
2600 -- The various semantic attributes are taken from the predefined type
2601 -- Float, just so that all of them are initialized. Their values are
2602 -- never used because no constant folding or expansion takes place in
2603 -- the generic itself.
2606 Mutate_Ekind
(T
, E_Floating_Point_Subtype
);
2607 Set_Etype
(T
, Base
);
2608 Set_Size_Info
(T
, (Standard_Float
));
2609 Set_RM_Size
(T
, RM_Size
(Standard_Float
));
2610 Set_Digits_Value
(T
, Digits_Value
(Standard_Float
));
2611 Set_Scalar_Range
(T
, Scalar_Range
(Standard_Float
));
2612 Set_Is_Constrained
(T
);
2614 Set_Is_Generic_Type
(Base
);
2615 Set_Etype
(Base
, Base
);
2616 Set_Size_Info
(Base
, (Standard_Float
));
2617 Set_RM_Size
(Base
, RM_Size
(Standard_Float
));
2618 Set_Digits_Value
(Base
, Digits_Value
(Standard_Float
));
2619 Set_Scalar_Range
(Base
, Scalar_Range
(Standard_Float
));
2620 Set_Parent
(Base
, Parent
(Def
));
2622 Check_Restriction
(No_Floating_Point
, Def
);
2623 end Analyze_Formal_Floating_Type
;
2625 -----------------------------------
2626 -- Analyze_Formal_Interface_Type;--
2627 -----------------------------------
2629 procedure Analyze_Formal_Interface_Type
2634 Loc
: constant Source_Ptr
:= Sloc
(N
);
2639 Make_Full_Type_Declaration
(Loc
,
2640 Defining_Identifier
=> T
,
2641 Type_Definition
=> Def
);
2645 Set_Is_Generic_Type
(T
);
2646 end Analyze_Formal_Interface_Type
;
2648 ---------------------------------
2649 -- Analyze_Formal_Modular_Type --
2650 ---------------------------------
2652 procedure Analyze_Formal_Modular_Type
(T
: Entity_Id
; Def
: Node_Id
) is
2654 -- Apart from their entity kind, generic modular types are treated like
2655 -- signed integer types, and have the same attributes.
2657 Analyze_Formal_Signed_Integer_Type
(T
, Def
);
2658 Mutate_Ekind
(T
, E_Modular_Integer_Subtype
);
2659 Mutate_Ekind
(Etype
(T
), E_Modular_Integer_Type
);
2661 end Analyze_Formal_Modular_Type
;
2663 ---------------------------------------
2664 -- Analyze_Formal_Object_Declaration --
2665 ---------------------------------------
2667 procedure Analyze_Formal_Object_Declaration
(N
: Node_Id
) is
2668 E
: constant Node_Id
:= Default_Expression
(N
);
2669 Id
: constant Node_Id
:= Defining_Identifier
(N
);
2672 Parent_Installed
: Boolean := False;
2678 Check_Abbreviated_Instance
(Parent
(N
), Parent_Installed
);
2680 -- Determine the mode of the formal object
2682 if Out_Present
(N
) then
2683 K
:= E_Generic_In_Out_Parameter
;
2685 if not In_Present
(N
) then
2686 Error_Msg_N
("formal generic objects cannot have mode OUT", N
);
2690 K
:= E_Generic_In_Parameter
;
2693 if Present
(Subtype_Mark
(N
)) then
2694 Find_Type
(Subtype_Mark
(N
));
2695 T
:= Entity
(Subtype_Mark
(N
));
2697 -- Verify that there is no redundant null exclusion
2699 if Null_Exclusion_Present
(N
) then
2700 if not Is_Access_Type
(T
) then
2702 ("null exclusion can only apply to an access type", N
);
2704 elsif Can_Never_Be_Null
(T
) then
2706 ("`NOT NULL` not allowed (& already excludes null)", N
, T
);
2710 -- Ada 2005 (AI-423): Formal object with an access definition
2713 Check_Access_Definition
(N
);
2714 T
:= Access_Definition
2716 N
=> Access_Definition
(N
));
2719 if Ekind
(T
) = E_Incomplete_Type
then
2721 Error_Node
: Node_Id
;
2724 if Present
(Subtype_Mark
(N
)) then
2725 Error_Node
:= Subtype_Mark
(N
);
2727 Check_Access_Definition
(N
);
2728 Error_Node
:= Access_Definition
(N
);
2731 Error_Msg_N
("premature usage of incomplete type", Error_Node
);
2735 if K
= E_Generic_In_Parameter
then
2737 -- Ada 2005 (AI-287): Limited aggregates allowed in generic formals
2739 if Ada_Version
< Ada_2005
and then Is_Limited_Type
(T
) then
2741 ("generic formal of mode IN must not be of limited type", N
);
2742 Explain_Limited_Type
(T
, N
);
2745 if Is_Abstract_Type
(T
) then
2747 ("generic formal of mode IN must not be of abstract type", N
);
2751 Preanalyze_Spec_Expression
(E
, T
);
2753 -- The default for a ghost generic formal IN parameter of
2754 -- access-to-variable type should be a ghost object (SPARK
2757 if Is_Access_Variable
(T
) then
2758 Check_Ghost_Formal_Variable
2761 Is_Default
=> True);
2764 if Is_Limited_Type
(T
) and then not OK_For_Limited_Init
(T
, E
) then
2766 ("initialization not allowed for limited types", E
);
2767 Explain_Limited_Type
(T
, E
);
2771 Mutate_Ekind
(Id
, K
);
2774 -- Case of generic IN OUT parameter
2777 -- If the formal has an unconstrained type, construct its actual
2778 -- subtype, as is done for subprogram formals. In this fashion, all
2779 -- its uses can refer to specific bounds.
2781 Mutate_Ekind
(Id
, K
);
2784 if (Is_Array_Type
(T
) and then not Is_Constrained
(T
))
2785 or else (Ekind
(T
) = E_Record_Type
and then Has_Discriminants
(T
))
2788 Non_Freezing_Ref
: constant Node_Id
:=
2789 New_Occurrence_Of
(Id
, Sloc
(Id
));
2793 -- Make sure the actual subtype doesn't generate bogus freezing
2795 Set_Must_Not_Freeze
(Non_Freezing_Ref
);
2796 Decl
:= Build_Actual_Subtype
(T
, Non_Freezing_Ref
);
2797 Insert_Before_And_Analyze
(N
, Decl
);
2798 Set_Actual_Subtype
(Id
, Defining_Identifier
(Decl
));
2801 Set_Actual_Subtype
(Id
, T
);
2806 ("initialization not allowed for `IN OUT` formals", N
);
2810 if Has_Aspects
(N
) then
2811 Analyze_Aspect_Specifications
(N
, Id
);
2814 if Parent_Installed
then
2817 end Analyze_Formal_Object_Declaration
;
2819 ----------------------------------------------
2820 -- Analyze_Formal_Ordinary_Fixed_Point_Type --
2821 ----------------------------------------------
2823 procedure Analyze_Formal_Ordinary_Fixed_Point_Type
2827 Loc
: constant Source_Ptr
:= Sloc
(Def
);
2828 Base
: constant Entity_Id
:=
2830 (E_Ordinary_Fixed_Point_Type
, Current_Scope
,
2831 Sloc
(Defining_Identifier
(Parent
(Def
))), 'G');
2834 -- The semantic attributes are set for completeness only, their values
2835 -- will never be used, since all properties of the type are non-static.
2838 Mutate_Ekind
(T
, E_Ordinary_Fixed_Point_Subtype
);
2839 Set_Etype
(T
, Base
);
2840 Set_Size_Info
(T
, Standard_Integer
);
2841 Set_RM_Size
(T
, RM_Size
(Standard_Integer
));
2842 Set_Small_Value
(T
, Ureal_1
);
2843 Set_Delta_Value
(T
, Ureal_1
);
2844 Set_Scalar_Range
(T
,
2846 Low_Bound
=> Make_Real_Literal
(Loc
, Ureal_1
),
2847 High_Bound
=> Make_Real_Literal
(Loc
, Ureal_1
)));
2848 Set_Is_Constrained
(T
);
2850 Set_Is_Generic_Type
(Base
);
2851 Set_Etype
(Base
, Base
);
2852 Set_Size_Info
(Base
, Standard_Integer
);
2853 Set_RM_Size
(Base
, RM_Size
(Standard_Integer
));
2854 Set_Small_Value
(Base
, Ureal_1
);
2855 Set_Delta_Value
(Base
, Ureal_1
);
2856 Set_Scalar_Range
(Base
, Scalar_Range
(T
));
2857 Set_Parent
(Base
, Parent
(Def
));
2859 Check_Restriction
(No_Fixed_Point
, Def
);
2860 end Analyze_Formal_Ordinary_Fixed_Point_Type
;
2862 ----------------------------------------
2863 -- Analyze_Formal_Package_Declaration --
2864 ----------------------------------------
2866 procedure Analyze_Formal_Package_Declaration
(N
: Node_Id
) is
2867 Gen_Id
: constant Node_Id
:= Name
(N
);
2868 Loc
: constant Source_Ptr
:= Sloc
(N
);
2869 Pack_Id
: constant Entity_Id
:= Defining_Identifier
(N
);
2872 Gen_Unit
: Entity_Id
;
2875 Vis_Prims_List
: Elist_Id
:= No_Elist
;
2876 -- List of primitives made temporarily visible in the instantiation
2877 -- to match the visibility of the formal type.
2879 function Build_Local_Package
return Node_Id
;
2880 -- The formal package is rewritten so that its parameters are replaced
2881 -- with corresponding declarations. For parameters with bona fide
2882 -- associations these declarations are created by Analyze_Associations
2883 -- as for a regular instantiation. For boxed parameters, we preserve
2884 -- the formal declarations and analyze them, in order to introduce
2885 -- entities of the right kind in the environment of the formal.
2887 -------------------------
2888 -- Build_Local_Package --
2889 -------------------------
2891 function Build_Local_Package
return Node_Id
is
2893 Pack_Decl
: Node_Id
;
2896 -- Within the formal, the name of the generic package is a renaming
2897 -- of the formal (as for a regular instantiation).
2900 Make_Package_Declaration
(Loc
,
2903 (Specification
(Original_Node
(Gen_Decl
)),
2904 Empty
, Instantiating
=> True));
2907 Make_Package_Renaming_Declaration
(Loc
,
2908 Defining_Unit_Name
=>
2909 Make_Defining_Identifier
(Loc
, Chars
(Gen_Unit
)),
2910 Name
=> New_Occurrence_Of
(Formal
, Loc
));
2912 if Nkind
(Gen_Id
) = N_Identifier
2913 and then Chars
(Gen_Id
) = Chars
(Pack_Id
)
2916 ("& is hidden within declaration of instance", Gen_Id
, Gen_Unit
);
2919 -- If the formal is declared with a box, or with an others choice,
2920 -- create corresponding declarations for all entities in the formal
2921 -- part, so that names with the proper types are available in the
2922 -- specification of the formal package.
2924 -- On the other hand, if there are no associations, then all the
2925 -- formals must have defaults, and this will be checked by the
2926 -- call to Analyze_Associations.
2929 or else Nkind
(First
(Generic_Associations
(N
))) = N_Others_Choice
2932 Formal_Decl
: Node_Id
;
2935 -- TBA : for a formal package, need to recurse ???
2940 (Generic_Formal_Declarations
(Original_Node
(Gen_Decl
)));
2941 while Present
(Formal_Decl
) loop
2945 (Formal_Decl
, Empty
, Instantiating
=> True));
2950 -- If generic associations are present, use Analyze_Associations to
2951 -- create the proper renaming declarations.
2955 Act_Tree
: constant Node_Id
:=
2957 (Original_Node
(Gen_Decl
), Empty
,
2958 Instantiating
=> True);
2961 Generic_Renamings
.Set_Last
(0);
2962 Generic_Renamings_HTable
.Reset
;
2963 Instantiation_Node
:= N
;
2966 Analyze_Associations
2967 (I_Node
=> Original_Node
(N
),
2968 Formals
=> Generic_Formal_Declarations
(Act_Tree
),
2969 F_Copy
=> Generic_Formal_Declarations
(Gen_Decl
));
2971 Vis_Prims_List
:= Check_Hidden_Primitives
(Decls
);
2975 Append
(Renaming
, To
=> Decls
);
2977 -- Add generated declarations ahead of local declarations in
2980 if No
(Visible_Declarations
(Specification
(Pack_Decl
))) then
2981 Set_Visible_Declarations
(Specification
(Pack_Decl
), Decls
);
2984 (First
(Visible_Declarations
(Specification
(Pack_Decl
))),
2989 end Build_Local_Package
;
2993 Save_ISMP
: constant Boolean := Ignore_SPARK_Mode_Pragmas_In_Instance
;
2994 -- Save flag Ignore_SPARK_Mode_Pragmas_In_Instance for restore on exit
2996 Associations
: Boolean := True;
2998 Parent_Installed
: Boolean := False;
2999 Parent_Instance
: Entity_Id
;
3000 Renaming_In_Par
: Entity_Id
;
3002 -- Start of processing for Analyze_Formal_Package_Declaration
3005 Check_Text_IO_Special_Unit
(Gen_Id
);
3008 Check_Generic_Child_Unit
(Gen_Id
, Parent_Installed
);
3009 Gen_Unit
:= Entity
(Gen_Id
);
3011 -- Check for a formal package that is a package renaming
3013 if Present
(Renamed_Entity
(Gen_Unit
)) then
3015 -- Indicate that unit is used, before replacing it with renamed
3016 -- entity for use below.
3018 if In_Extended_Main_Source_Unit
(N
) then
3019 Set_Is_Instantiated
(Gen_Unit
);
3020 Generate_Reference
(Gen_Unit
, N
);
3023 Gen_Unit
:= Renamed_Entity
(Gen_Unit
);
3026 if Ekind
(Gen_Unit
) /= E_Generic_Package
then
3027 Error_Msg_N
("expect generic package name", Gen_Id
);
3031 elsif Gen_Unit
= Current_Scope
then
3033 ("generic package cannot be used as a formal package of itself",
3038 elsif In_Open_Scopes
(Gen_Unit
) then
3039 if Is_Compilation_Unit
(Gen_Unit
)
3040 and then Is_Child_Unit
(Current_Scope
)
3042 -- Special-case the error when the formal is a parent, and
3043 -- continue analysis to minimize cascaded errors.
3046 ("generic parent cannot be used as formal package of a child "
3051 ("generic package cannot be used as a formal package within "
3052 & "itself", Gen_Id
);
3058 -- Check that name of formal package does not hide name of generic,
3059 -- or its leading prefix. This check must be done separately because
3060 -- the name of the generic has already been analyzed.
3063 Gen_Name
: Entity_Id
;
3067 while Nkind
(Gen_Name
) = N_Expanded_Name
loop
3068 Gen_Name
:= Prefix
(Gen_Name
);
3071 if Chars
(Gen_Name
) = Chars
(Pack_Id
) then
3073 ("& is hidden within declaration of formal package",
3079 or else No
(Generic_Associations
(N
))
3080 or else Nkind
(First
(Generic_Associations
(N
))) = N_Others_Choice
3082 Associations
:= False;
3085 -- If there are no generic associations, the generic parameters appear
3086 -- as local entities and are instantiated like them. We copy the generic
3087 -- package declaration as if it were an instantiation, and analyze it
3088 -- like a regular package, except that we treat the formals as
3089 -- additional visible components.
3091 Gen_Decl
:= Unit_Declaration_Node
(Gen_Unit
);
3093 if In_Extended_Main_Source_Unit
(N
) then
3094 Set_Is_Instantiated
(Gen_Unit
);
3095 Generate_Reference
(Gen_Unit
, N
);
3098 Formal
:= New_Copy
(Pack_Id
);
3099 Create_Instantiation_Source
(N
, Gen_Unit
, S_Adjustment
);
3101 -- Make local generic without formals. The formals will be replaced with
3102 -- internal declarations.
3105 New_N
:= Build_Local_Package
;
3107 -- If there are errors in the parameter list, Analyze_Associations
3108 -- raises Instantiation_Error. Patch the declaration to prevent further
3109 -- exception propagation.
3112 when Instantiation_Error
=>
3113 Enter_Name
(Formal
);
3114 Mutate_Ekind
(Formal
, E_Variable
);
3115 Set_Etype
(Formal
, Any_Type
);
3116 Restore_Hidden_Primitives
(Vis_Prims_List
);
3118 if Parent_Installed
then
3126 Set_Defining_Unit_Name
(Specification
(New_N
), Formal
);
3127 Set_Generic_Parent
(Specification
(N
), Gen_Unit
);
3128 Set_Instance_Env
(Gen_Unit
, Formal
);
3129 Set_Is_Generic_Instance
(Formal
);
3131 Enter_Name
(Formal
);
3132 Mutate_Ekind
(Formal
, E_Package
);
3133 Set_Etype
(Formal
, Standard_Void_Type
);
3134 Set_Inner_Instances
(Formal
, New_Elmt_List
);
3136 -- It is unclear that any aspects can apply to a formal package
3137 -- declaration, given that they look like a hidden conformance
3138 -- requirement on the corresponding actual. However, Abstract_State
3139 -- must be treated specially because it generates declarations that
3140 -- must appear before other declarations in the specification and
3141 -- must be analyzed at once.
3143 if Present
(Aspect_Specifications
(Gen_Decl
)) then
3144 if No
(Aspect_Specifications
(N
)) then
3145 Set_Aspect_Specifications
(N
, New_List
);
3149 ASN
: Node_Id
:= First
(Aspect_Specifications
(Gen_Decl
));
3153 while Present
(ASN
) loop
3154 if Get_Aspect_Id
(ASN
) = Aspect_Abstract_State
then
3156 Copy_Generic_Node
(ASN
, Empty
, Instantiating
=> True);
3157 Set_Entity
(New_A
, Formal
);
3158 Set_Analyzed
(New_A
, False);
3159 Append
(New_A
, Aspect_Specifications
(N
));
3160 Analyze_Aspect_Specifications
(N
, Formal
);
3169 Push_Scope
(Formal
);
3171 -- Manually set the SPARK_Mode from the context because the package
3172 -- declaration is never analyzed.
3174 Set_SPARK_Pragma
(Formal
, SPARK_Mode_Pragma
);
3175 Set_SPARK_Aux_Pragma
(Formal
, SPARK_Mode_Pragma
);
3176 Set_SPARK_Pragma_Inherited
(Formal
);
3177 Set_SPARK_Aux_Pragma_Inherited
(Formal
);
3179 if Is_Child_Unit
(Gen_Unit
) and then Parent_Installed
then
3181 -- Similarly, we have to make the name of the formal visible in the
3182 -- parent instance, to resolve properly fully qualified names that
3183 -- may appear in the generic unit. The parent instance has been
3184 -- placed on the scope stack ahead of the current scope.
3186 Parent_Instance
:= Scope_Stack
.Table
(Scope_Stack
.Last
- 1).Entity
;
3189 Make_Defining_Identifier
(Loc
, Chars
(Gen_Unit
));
3190 Mutate_Ekind
(Renaming_In_Par
, E_Package
);
3191 Set_Is_Not_Self_Hidden
(Renaming_In_Par
);
3192 Set_Etype
(Renaming_In_Par
, Standard_Void_Type
);
3193 Set_Scope
(Renaming_In_Par
, Parent_Instance
);
3194 Set_Parent
(Renaming_In_Par
, Parent
(Formal
));
3195 Set_Renamed_Entity
(Renaming_In_Par
, Formal
);
3196 Append_Entity
(Renaming_In_Par
, Parent_Instance
);
3199 -- A formal package declaration behaves as a package instantiation with
3200 -- respect to SPARK_Mode "off". If the annotation is "off" or altogether
3201 -- missing, set the global flag which signals Analyze_Pragma to ingnore
3202 -- all SPARK_Mode pragmas within the generic_package_name.
3204 if SPARK_Mode
/= On
then
3205 Ignore_SPARK_Mode_Pragmas_In_Instance
:= True;
3207 -- Mark the formal spec in case the body is instantiated at a later
3208 -- pass. This preserves the original context in effect for the body.
3210 Set_Ignore_SPARK_Mode_Pragmas
(Formal
);
3213 Analyze
(Specification
(N
));
3215 -- The formals for which associations are provided are not visible
3216 -- outside of the formal package. The others are still declared by a
3217 -- formal parameter declaration.
3219 -- If there are no associations, the only local entity to hide is the
3220 -- generated package renaming itself.
3226 E
:= First_Entity
(Formal
);
3227 while Present
(E
) loop
3228 if Associations
and then not Is_Generic_Formal
(E
) then
3232 if Ekind
(E
) = E_Package
and then Renamed_Entity
(E
) = Formal
then
3241 End_Package_Scope
(Formal
);
3242 Restore_Hidden_Primitives
(Vis_Prims_List
);
3244 if Parent_Installed
then
3250 -- Inside the generic unit, the formal package is a regular package, but
3251 -- no body is needed for it. Note that after instantiation, the defining
3252 -- unit name we need is in the new tree and not in the original (see
3253 -- Package_Instantiation). A generic formal package is an instance, and
3254 -- can be used as an actual for an inner instance.
3256 Set_Has_Completion
(Formal
, True);
3258 -- Add semantic information to the original defining identifier.
3260 Mutate_Ekind
(Pack_Id
, E_Package
);
3261 Set_Etype
(Pack_Id
, Standard_Void_Type
);
3262 Set_Scope
(Pack_Id
, Scope
(Formal
));
3263 Set_Has_Completion
(Pack_Id
, True);
3266 if Has_Aspects
(N
) then
3267 -- Unclear that any other aspects may appear here, analyze them
3268 -- for completion, given that the grammar allows their appearance.
3270 Analyze_Aspect_Specifications
(N
, Pack_Id
);
3273 Ignore_SPARK_Mode_Pragmas_In_Instance
:= Save_ISMP
;
3274 end Analyze_Formal_Package_Declaration
;
3276 ---------------------------------
3277 -- Analyze_Formal_Private_Type --
3278 ---------------------------------
3280 procedure Analyze_Formal_Private_Type
3286 New_Private_Type
(N
, T
, Def
);
3288 -- Set the size to an arbitrary but legal value
3290 Set_Size_Info
(T
, Standard_Integer
);
3291 Set_RM_Size
(T
, RM_Size
(Standard_Integer
));
3292 end Analyze_Formal_Private_Type
;
3294 ------------------------------------
3295 -- Analyze_Formal_Incomplete_Type --
3296 ------------------------------------
3298 procedure Analyze_Formal_Incomplete_Type
3304 Mutate_Ekind
(T
, E_Incomplete_Type
);
3306 Set_Private_Dependents
(T
, New_Elmt_List
);
3308 if Tagged_Present
(Def
) then
3309 Set_Is_Tagged_Type
(T
);
3310 Make_Class_Wide_Type
(T
);
3311 Set_Direct_Primitive_Operations
(T
, New_Elmt_List
);
3313 end Analyze_Formal_Incomplete_Type
;
3315 ----------------------------------------
3316 -- Analyze_Formal_Signed_Integer_Type --
3317 ----------------------------------------
3319 procedure Analyze_Formal_Signed_Integer_Type
3323 Base
: constant Entity_Id
:=
3325 (E_Signed_Integer_Type
,
3327 Sloc
(Defining_Identifier
(Parent
(Def
))), 'G');
3332 Mutate_Ekind
(T
, E_Signed_Integer_Subtype
);
3333 Set_Etype
(T
, Base
);
3334 Set_Size_Info
(T
, Standard_Integer
);
3335 Set_RM_Size
(T
, RM_Size
(Standard_Integer
));
3336 Set_Scalar_Range
(T
, Scalar_Range
(Standard_Integer
));
3337 Set_Is_Constrained
(T
);
3339 Set_Is_Generic_Type
(Base
);
3340 Set_Size_Info
(Base
, Standard_Integer
);
3341 Set_RM_Size
(Base
, RM_Size
(Standard_Integer
));
3342 Set_Etype
(Base
, Base
);
3343 Set_Scalar_Range
(Base
, Scalar_Range
(Standard_Integer
));
3344 Set_Parent
(Base
, Parent
(Def
));
3345 end Analyze_Formal_Signed_Integer_Type
;
3347 -------------------------------------------
3348 -- Analyze_Formal_Subprogram_Declaration --
3349 -------------------------------------------
3351 procedure Analyze_Formal_Subprogram_Declaration
(N
: Node_Id
) is
3352 Spec
: constant Node_Id
:= Specification
(N
);
3353 Def
: constant Node_Id
:= Default_Name
(N
);
3354 Expr
: constant Node_Id
:= Expression
(N
);
3355 Nam
: constant Entity_Id
:= Defining_Unit_Name
(Spec
);
3357 Parent_Installed
: Boolean := False;
3365 if Nkind
(Nam
) = N_Defining_Program_Unit_Name
then
3366 Error_Msg_N
("name of formal subprogram must be a direct name", Nam
);
3370 Check_Abbreviated_Instance
(Parent
(N
), Parent_Installed
);
3372 Analyze_Subprogram_Declaration
(N
);
3373 Set_Is_Formal_Subprogram
(Nam
);
3374 Set_Has_Completion
(Nam
);
3376 if Nkind
(N
) = N_Formal_Abstract_Subprogram_Declaration
then
3377 Set_Is_Abstract_Subprogram
(Nam
);
3379 Set_Is_Dispatching_Operation
(Nam
);
3381 -- A formal abstract procedure cannot have a null default
3382 -- (RM 12.6(4.1/2)).
3384 if Nkind
(Spec
) = N_Procedure_Specification
3385 and then Null_Present
(Spec
)
3388 ("a formal abstract subprogram cannot default to null", Spec
);
3391 -- A formal abstract function cannot have an expression default
3392 -- (expression defaults are allowed for nonabstract formal functions
3393 -- when extensions are enabled).
3395 if Nkind
(Spec
) = N_Function_Specification
3396 and then Present
(Expr
)
3399 ("a formal abstract subprogram cannot default to an expression",
3404 Ctrl_Type
: constant Entity_Id
:= Find_Dispatching_Type
(Nam
);
3406 if No
(Ctrl_Type
) then
3408 ("abstract formal subprogram must have a controlling type",
3411 elsif Ada_Version
>= Ada_2012
3412 and then Is_Incomplete_Type
(Ctrl_Type
)
3415 ("controlling type of abstract formal subprogram cannot "
3416 & "be incomplete type", N
, Ctrl_Type
);
3419 Check_Controlling_Formals
(Ctrl_Type
, Nam
);
3424 -- Default name is resolved at the point of instantiation
3426 if Box_Present
(N
) then
3429 -- Default name is bound at the point of generic declaration
3431 elsif Present
(Def
) then
3432 if Nkind
(Def
) = N_Operator_Symbol
then
3433 Find_Direct_Name
(Def
);
3435 elsif Nkind
(Def
) /= N_Attribute_Reference
then
3439 -- For an attribute reference, analyze the prefix and verify
3440 -- that it has the proper profile for the subprogram.
3442 Analyze
(Prefix
(Def
));
3443 Valid_Default_Attribute
(Nam
, Def
);
3447 -- The default for a ghost generic formal procedure should be a ghost
3448 -- procedure (SPARK RM 6.9(13)).
3450 if Ekind
(Nam
) = E_Procedure
then
3452 Def_E
: Entity_Id
:= Empty
;
3454 if Nkind
(Def
) in N_Has_Entity
then
3455 Def_E
:= Entity
(Def
);
3458 Check_Ghost_Formal_Procedure_Or_Package
3462 Is_Default
=> True);
3466 -- Default name may be overloaded, in which case the interpretation
3467 -- with the correct profile must be selected, as for a renaming.
3468 -- If the definition is an indexed component, it must denote a
3469 -- member of an entry family. If it is a selected component, it
3470 -- can be a protected operation.
3472 if Etype
(Def
) = Any_Type
then
3475 elsif Nkind
(Def
) = N_Selected_Component
then
3476 if not Is_Overloadable
(Entity
(Selector_Name
(Def
))) then
3477 Error_Msg_N
("expect valid subprogram name as default", Def
);
3480 elsif Nkind
(Def
) = N_Indexed_Component
then
3481 if Is_Entity_Name
(Prefix
(Def
)) then
3482 if Ekind
(Entity
(Prefix
(Def
))) /= E_Entry_Family
then
3483 Error_Msg_N
("expect valid subprogram name as default", Def
);
3486 elsif Nkind
(Prefix
(Def
)) = N_Selected_Component
then
3487 if Ekind
(Entity
(Selector_Name
(Prefix
(Def
)))) /=
3490 Error_Msg_N
("expect valid subprogram name as default", Def
);
3494 Error_Msg_N
("expect valid subprogram name as default", Def
);
3498 elsif Nkind
(Def
) = N_Character_Literal
then
3500 -- Needs some type checks: subprogram should be parameterless???
3502 Resolve
(Def
, (Etype
(Nam
)));
3504 elsif not Is_Entity_Name
(Def
)
3505 or else not Is_Overloadable
(Entity
(Def
))
3507 Error_Msg_N
("expect valid subprogram name as default", Def
);
3510 elsif not Is_Overloaded
(Def
) then
3511 Subp
:= Entity
(Def
);
3514 Error_Msg_N
("premature usage of formal subprogram", Def
);
3516 elsif not Entity_Matches_Spec
(Subp
, Nam
) then
3517 Error_Msg_N
("no visible entity matches specification", Def
);
3520 -- More than one interpretation, so disambiguate as for a renaming
3525 I1
: Interp_Index
:= 0;
3531 Get_First_Interp
(Def
, I
, It
);
3532 while Present
(It
.Nam
) loop
3533 if Entity_Matches_Spec
(It
.Nam
, Nam
) then
3534 if Subp
/= Any_Id
then
3535 It1
:= Disambiguate
(Def
, I1
, I
, Etype
(Subp
));
3537 if It1
= No_Interp
then
3538 Error_Msg_N
("ambiguous default subprogram", Def
);
3551 Get_Next_Interp
(I
, It
);
3555 if Subp
/= Any_Id
then
3557 -- Subprogram found, generate reference to it
3559 Set_Entity
(Def
, Subp
);
3560 Generate_Reference
(Subp
, Def
);
3563 Error_Msg_N
("premature usage of formal subprogram", Def
);
3565 elsif Ekind
(Subp
) /= E_Operator
then
3566 Check_Mode_Conformant
(Subp
, Nam
);
3570 Error_Msg_N
("no visible subprogram matches specification", N
);
3574 -- When extensions are enabled, an expression can be given as default
3575 -- for a formal function. The expression must be of the function result
3576 -- type and can reference formal parameters of the function.
3578 elsif Present
(Expr
) then
3580 Install_Formals
(Nam
);
3581 Preanalyze_Spec_Expression
(Expr
, Etype
(Nam
));
3586 if Has_Aspects
(N
) then
3587 Analyze_Aspect_Specifications
(N
, Nam
);
3590 if Parent_Installed
then
3593 end Analyze_Formal_Subprogram_Declaration
;
3595 -------------------------------------
3596 -- Analyze_Formal_Type_Declaration --
3597 -------------------------------------
3599 procedure Analyze_Formal_Type_Declaration
(N
: Node_Id
) is
3600 Def
: constant Node_Id
:= Formal_Type_Definition
(N
);
3602 Parent_Installed
: Boolean := False;
3606 T
:= Defining_Identifier
(N
);
3608 if Present
(Discriminant_Specifications
(N
))
3609 and then Nkind
(Def
) /= N_Formal_Private_Type_Definition
3612 ("discriminants not allowed for this formal type", T
);
3615 Check_Abbreviated_Instance
(Parent
(N
), Parent_Installed
);
3617 -- Enter the new name, and branch to specific routine
3620 when N_Formal_Private_Type_Definition
=>
3621 Analyze_Formal_Private_Type
(N
, T
, Def
);
3623 when N_Formal_Derived_Type_Definition
=>
3624 Analyze_Formal_Derived_Type
(N
, T
, Def
);
3626 when N_Formal_Incomplete_Type_Definition
=>
3627 Analyze_Formal_Incomplete_Type
(T
, Def
);
3629 when N_Formal_Discrete_Type_Definition
=>
3630 Analyze_Formal_Discrete_Type
(T
, Def
);
3632 when N_Formal_Signed_Integer_Type_Definition
=>
3633 Analyze_Formal_Signed_Integer_Type
(T
, Def
);
3635 when N_Formal_Modular_Type_Definition
=>
3636 Analyze_Formal_Modular_Type
(T
, Def
);
3638 when N_Formal_Floating_Point_Definition
=>
3639 Analyze_Formal_Floating_Type
(T
, Def
);
3641 when N_Formal_Ordinary_Fixed_Point_Definition
=>
3642 Analyze_Formal_Ordinary_Fixed_Point_Type
(T
, Def
);
3644 when N_Formal_Decimal_Fixed_Point_Definition
=>
3645 Analyze_Formal_Decimal_Fixed_Point_Type
(T
, Def
);
3647 when N_Array_Type_Definition
=>
3648 Analyze_Formal_Array_Type
(T
, Def
);
3650 when N_Access_Function_Definition
3651 | N_Access_Procedure_Definition
3652 | N_Access_To_Object_Definition
3654 Analyze_Generic_Access_Type
(T
, Def
);
3656 -- Ada 2005: a interface declaration is encoded as an abstract
3657 -- record declaration or a abstract type derivation.
3659 when N_Record_Definition
=>
3660 Analyze_Formal_Interface_Type
(N
, T
, Def
);
3662 when N_Derived_Type_Definition
=>
3663 Analyze_Formal_Derived_Interface_Type
(N
, T
, Def
);
3669 raise Program_Error
;
3672 -- A formal type declaration declares a type and its first
3675 Set_Is_Generic_Type
(T
);
3676 Set_Is_First_Subtype
(T
);
3678 if Present
(Default_Subtype_Mark
(Original_Node
(N
))) then
3679 Validate_Formal_Type_Default
(N
);
3682 if Has_Aspects
(N
) then
3683 Analyze_Aspect_Specifications
(N
, T
);
3686 if Parent_Installed
then
3689 end Analyze_Formal_Type_Declaration
;
3691 ------------------------------------
3692 -- Analyze_Function_Instantiation --
3693 ------------------------------------
3695 procedure Analyze_Function_Instantiation
(N
: Node_Id
) is
3697 Analyze_Subprogram_Instantiation
(N
, E_Function
);
3698 end Analyze_Function_Instantiation
;
3700 ---------------------------------
3701 -- Analyze_Generic_Access_Type --
3702 ---------------------------------
3704 procedure Analyze_Generic_Access_Type
(T
: Entity_Id
; Def
: Node_Id
) is
3708 if Nkind
(Def
) = N_Access_To_Object_Definition
then
3709 Access_Type_Declaration
(T
, Def
);
3711 if Is_Incomplete_Or_Private_Type
(Designated_Type
(T
))
3712 and then No
(Full_View
(Designated_Type
(T
)))
3713 and then not Is_Generic_Type
(Designated_Type
(T
))
3715 Error_Msg_N
("premature usage of incomplete type", Def
);
3717 elsif not Is_Entity_Name
(Subtype_Indication
(Def
)) then
3719 ("only a subtype mark is allowed in a formal", Def
);
3723 Access_Subprogram_Declaration
(T
, Def
);
3725 end Analyze_Generic_Access_Type
;
3727 ---------------------------------
3728 -- Analyze_Generic_Formal_Part --
3729 ---------------------------------
3731 procedure Analyze_Generic_Formal_Part
(N
: Node_Id
) is
3732 Gen_Parm_Decl
: Node_Id
;
3735 -- The generic formals are processed in the scope of the generic unit,
3736 -- where they are immediately visible. The scope is installed by the
3739 Gen_Parm_Decl
:= First
(Generic_Formal_Declarations
(N
));
3740 while Present
(Gen_Parm_Decl
) loop
3741 Analyze
(Gen_Parm_Decl
);
3742 Next
(Gen_Parm_Decl
);
3745 Generate_Reference_To_Generic_Formals
(Current_Scope
);
3747 -- For Ada 2022, some formal parameters can carry aspects, which must
3748 -- be name-resolved at the end of the list of formal parameters (which
3749 -- has the semantics of a declaration list).
3751 Analyze_Contracts
(Generic_Formal_Declarations
(N
));
3752 end Analyze_Generic_Formal_Part
;
3754 ------------------------------------------
3755 -- Analyze_Generic_Package_Declaration --
3756 ------------------------------------------
3758 procedure Analyze_Generic_Package_Declaration
(N
: Node_Id
) is
3759 Decls
: constant List_Id
:= Visible_Declarations
(Specification
(N
));
3760 Loc
: constant Source_Ptr
:= Sloc
(N
);
3766 Save_Parent
: Node_Id
;
3769 -- A generic may grant access to its private enclosing context depending
3770 -- on the placement of its corresponding body. From elaboration point of
3771 -- view, the flow of execution may enter this private context, and then
3772 -- reach an external unit, thus producing a dependency on that external
3773 -- unit. For such a path to be properly discovered and encoded in the
3774 -- ALI file of the main unit, let the ABE mechanism process the body of
3775 -- the main unit, and encode all relevant invocation constructs and the
3776 -- relations between them.
3778 Mark_Save_Invocation_Graph_Of_Body
;
3780 -- We introduce a renaming of the enclosing package, to have a usable
3781 -- entity as the prefix of an expanded name for a local entity of the
3782 -- form Par.P.Q, where P is the generic package. This is because a local
3783 -- entity named P may hide it, so that the usual visibility rules in
3784 -- the instance will not resolve properly.
3787 Make_Package_Renaming_Declaration
(Loc
,
3788 Defining_Unit_Name
=>
3789 Make_Defining_Identifier
(Loc
,
3790 Chars
=> New_External_Name
(Chars
(Defining_Entity
(N
)), "GH")),
3792 Make_Identifier
(Loc
, Chars
(Defining_Entity
(N
))));
3794 -- The declaration is inserted before other declarations, but before
3795 -- pragmas that may be library-unit pragmas and must appear before other
3796 -- declarations. The pragma Compile_Time_Error is not in this class, and
3797 -- may contain an expression that includes such a qualified name, so the
3798 -- renaming declaration must appear before it.
3800 -- Are there other pragmas that require this special handling ???
3802 if Present
(Decls
) then
3803 Decl
:= First
(Decls
);
3804 while Present
(Decl
)
3805 and then Nkind
(Decl
) = N_Pragma
3806 and then Get_Pragma_Id
(Decl
) /= Pragma_Compile_Time_Error
3811 if Present
(Decl
) then
3812 Insert_Before
(Decl
, Renaming
);
3814 Append
(Renaming
, Visible_Declarations
(Specification
(N
)));
3818 Set_Visible_Declarations
(Specification
(N
), New_List
(Renaming
));
3821 -- Create copy of generic unit, and save for instantiation. If the unit
3822 -- is a child unit, do not copy the specifications for the parent, which
3823 -- are not part of the generic tree.
3825 Save_Parent
:= Parent_Spec
(N
);
3826 Set_Parent_Spec
(N
, Empty
);
3828 New_N
:= Copy_Generic_Node
(N
, Empty
, Instantiating
=> False);
3829 Set_Parent_Spec
(New_N
, Save_Parent
);
3832 -- Once the contents of the generic copy and the template are swapped,
3833 -- do the same for their respective aspect specifications.
3835 Exchange_Aspects
(N
, New_N
);
3837 -- Collect all contract-related source pragmas found within the template
3838 -- and attach them to the contract of the package spec. This contract is
3839 -- used in the capture of global references within annotations.
3841 Create_Generic_Contract
(N
);
3843 Id
:= Defining_Entity
(N
);
3844 Generate_Definition
(Id
);
3846 -- Expansion is not applied to generic units
3851 Mutate_Ekind
(Id
, E_Generic_Package
);
3852 Set_Is_Not_Self_Hidden
(Id
);
3853 Set_Etype
(Id
, Standard_Void_Type
);
3855 -- Set SPARK_Mode from context
3857 Set_SPARK_Pragma
(Id
, SPARK_Mode_Pragma
);
3858 Set_SPARK_Aux_Pragma
(Id
, SPARK_Mode_Pragma
);
3859 Set_SPARK_Pragma_Inherited
(Id
);
3860 Set_SPARK_Aux_Pragma_Inherited
(Id
);
3862 -- Preserve relevant elaboration-related attributes of the context which
3863 -- are no longer available or very expensive to recompute once analysis,
3864 -- resolution, and expansion are over.
3866 Mark_Elaboration_Attributes
3871 -- Analyze aspects now, so that generated pragmas appear in the
3872 -- declarations before building and analyzing the generic copy.
3874 if Has_Aspects
(N
) then
3875 Analyze_Aspect_Specifications
(N
, Id
);
3879 Enter_Generic_Scope
(Id
);
3880 Set_Inner_Instances
(Id
, New_Elmt_List
);
3882 Set_Categorization_From_Pragmas
(N
);
3883 Set_Is_Pure
(Id
, Is_Pure
(Current_Scope
));
3885 -- Link the declaration of the generic homonym in the generic copy to
3886 -- the package it renames, so that it is always resolved properly.
3888 Set_Generic_Homonym
(Id
, Defining_Unit_Name
(Renaming
));
3889 Set_Entity
(Associated_Node
(Name
(Renaming
)), Id
);
3891 -- For a library unit, we have reconstructed the entity for the unit,
3892 -- and must reset it in the library tables.
3894 if Nkind
(Parent
(N
)) = N_Compilation_Unit
then
3895 Set_Cunit_Entity
(Current_Sem_Unit
, Id
);
3898 Analyze_Generic_Formal_Part
(N
);
3900 -- After processing the generic formals, analysis proceeds as for a
3901 -- non-generic package.
3903 Analyze
(Specification
(N
));
3905 Validate_Categorization_Dependency
(N
, Id
);
3909 End_Package_Scope
(Id
);
3910 Exit_Generic_Scope
(Id
);
3912 -- If the generic appears within a package unit, the body of that unit
3913 -- has to be present for instantiation and inlining.
3915 if Nkind
(Unit
(Cunit
(Current_Sem_Unit
))) = N_Package_Declaration
then
3916 Set_Body_Needed_For_Inlining
3917 (Defining_Entity
(Unit
(Cunit
(Current_Sem_Unit
))));
3920 if Nkind
(Parent
(N
)) /= N_Compilation_Unit
then
3921 Move_Freeze_Nodes
(Id
, N
, Visible_Declarations
(Specification
(N
)));
3922 Move_Freeze_Nodes
(Id
, N
, Private_Declarations
(Specification
(N
)));
3923 Move_Freeze_Nodes
(Id
, N
, Generic_Formal_Declarations
(N
));
3926 Set_Body_Required
(Parent
(N
), Unit_Requires_Body
(Id
));
3927 Validate_RT_RAT_Component
(N
);
3929 -- If this is a spec without a body, check that generic parameters
3932 if not Body_Required
(Parent
(N
)) then
3933 Check_References
(Id
);
3937 -- If there is a specified storage pool in the context, create an
3938 -- aspect on the package declaration, so that it is used in any
3939 -- instance that does not override it.
3941 if Present
(Default_Pool
) then
3947 Make_Aspect_Specification
(Loc
,
3948 Identifier
=> Make_Identifier
(Loc
, Name_Default_Storage_Pool
),
3949 Expression
=> New_Copy
(Default_Pool
));
3951 if No
(Aspect_Specifications
(Specification
(N
))) then
3952 Set_Aspect_Specifications
(Specification
(N
), New_List
(ASN
));
3954 Append
(ASN
, Aspect_Specifications
(Specification
(N
)));
3958 end Analyze_Generic_Package_Declaration
;
3960 --------------------------------------------
3961 -- Analyze_Generic_Subprogram_Declaration --
3962 --------------------------------------------
3964 procedure Analyze_Generic_Subprogram_Declaration
(N
: Node_Id
) is
3968 Result_Type
: Entity_Id
;
3969 Save_Parent
: Node_Id
;
3974 -- A generic may grant access to its private enclosing context depending
3975 -- on the placement of its corresponding body. From elaboration point of
3976 -- view, the flow of execution may enter this private context, and then
3977 -- reach an external unit, thus producing a dependency on that external
3978 -- unit. For such a path to be properly discovered and encoded in the
3979 -- ALI file of the main unit, let the ABE mechanism process the body of
3980 -- the main unit, and encode all relevant invocation constructs and the
3981 -- relations between them.
3983 Mark_Save_Invocation_Graph_Of_Body
;
3985 -- Create copy of generic unit, and save for instantiation. If the unit
3986 -- is a child unit, do not copy the specifications for the parent, which
3987 -- are not part of the generic tree.
3989 Save_Parent
:= Parent_Spec
(N
);
3990 Set_Parent_Spec
(N
, Empty
);
3992 New_N
:= Copy_Generic_Node
(N
, Empty
, Instantiating
=> False);
3993 Set_Parent_Spec
(New_N
, Save_Parent
);
3996 -- Once the contents of the generic copy and the template are swapped,
3997 -- do the same for their respective aspect specifications.
3999 Exchange_Aspects
(N
, New_N
);
4001 -- Collect all contract-related source pragmas found within the template
4002 -- and attach them to the contract of the subprogram spec. This contract
4003 -- is used in the capture of global references within annotations.
4005 Create_Generic_Contract
(N
);
4007 Spec
:= Specification
(N
);
4008 Id
:= Defining_Entity
(Spec
);
4009 Generate_Definition
(Id
);
4011 if Nkind
(Id
) = N_Defining_Operator_Symbol
then
4013 ("operator symbol not allowed for generic subprogram", Id
);
4019 Set_Scope_Depth_Value
(Id
, Scope_Depth
(Current_Scope
) + 1);
4022 Enter_Generic_Scope
(Id
);
4023 Set_Inner_Instances
(Id
, New_Elmt_List
);
4024 Set_Is_Pure
(Id
, Is_Pure
(Current_Scope
));
4026 Analyze_Generic_Formal_Part
(N
);
4028 if Nkind
(Spec
) = N_Function_Specification
then
4029 Mutate_Ekind
(Id
, E_Generic_Function
);
4031 Mutate_Ekind
(Id
, E_Generic_Procedure
);
4034 -- Set SPARK_Mode from context
4036 Set_SPARK_Pragma
(Id
, SPARK_Mode_Pragma
);
4037 Set_SPARK_Pragma_Inherited
(Id
);
4039 -- Preserve relevant elaboration-related attributes of the context which
4040 -- are no longer available or very expensive to recompute once analysis,
4041 -- resolution, and expansion are over.
4043 Mark_Elaboration_Attributes
4048 Formals
:= Parameter_Specifications
(Spec
);
4050 if Present
(Formals
) then
4051 Process_Formals
(Formals
, Spec
);
4054 if Nkind
(Spec
) = N_Function_Specification
then
4055 if Nkind
(Result_Definition
(Spec
)) = N_Access_Definition
then
4056 Result_Type
:= Access_Definition
(Spec
, Result_Definition
(Spec
));
4057 Set_Etype
(Id
, Result_Type
);
4059 -- Check restriction imposed by AI05-073: a generic function
4060 -- cannot return an abstract type or an access to such.
4062 if Is_Abstract_Type
(Designated_Type
(Result_Type
)) then
4064 ("generic function cannot have an access result "
4065 & "that designates an abstract type", Spec
);
4069 Find_Type
(Result_Definition
(Spec
));
4070 Typ
:= Entity
(Result_Definition
(Spec
));
4072 if Is_Abstract_Type
(Typ
)
4073 and then Ada_Version
>= Ada_2012
4076 ("generic function cannot have abstract result type", Spec
);
4079 -- If a null exclusion is imposed on the result type, then create
4080 -- a null-excluding itype (an access subtype) and use it as the
4081 -- function's Etype.
4083 if Is_Access_Type
(Typ
)
4084 and then Null_Exclusion_Present
(Spec
)
4087 Create_Null_Excluding_Itype
4089 Related_Nod
=> Spec
,
4090 Scope_Id
=> Defining_Unit_Name
(Spec
)));
4092 Set_Etype
(Id
, Typ
);
4097 Set_Etype
(Id
, Standard_Void_Type
);
4100 Set_Is_Not_Self_Hidden
(Id
);
4102 -- Analyze the aspects of the generic copy to ensure that all generated
4103 -- pragmas (if any) perform their semantic effects.
4105 if Has_Aspects
(N
) then
4106 Analyze_Aspect_Specifications
(N
, Id
);
4109 -- For a library unit, we have reconstructed the entity for the unit,
4110 -- and must reset it in the library tables. We also make sure that
4111 -- Body_Required is set properly in the original compilation unit node.
4113 if Nkind
(Parent
(N
)) = N_Compilation_Unit
then
4114 Set_Cunit_Entity
(Current_Sem_Unit
, Id
);
4115 Set_Body_Required
(Parent
(N
), Unit_Requires_Body
(Id
));
4118 -- If the generic appears within a package unit, the body of that unit
4119 -- has to be present for instantiation and inlining.
4121 if Nkind
(Unit
(Cunit
(Current_Sem_Unit
))) = N_Package_Declaration
4122 and then Unit_Requires_Body
(Id
)
4124 Set_Body_Needed_For_Inlining
4125 (Defining_Entity
(Unit
(Cunit
(Current_Sem_Unit
))));
4128 Set_Categorization_From_Pragmas
(N
);
4129 Validate_Categorization_Dependency
(N
, Id
);
4131 -- Capture all global references that occur within the profile of the
4132 -- generic subprogram. Aspects are not part of this processing because
4133 -- they must be delayed. If processed now, Save_Global_References will
4134 -- destroy the Associated_Node links and prevent the capture of global
4135 -- references when the contract of the generic subprogram is analyzed.
4137 Save_Global_References
(Original_Node
(N
));
4141 Exit_Generic_Scope
(Id
);
4142 Generate_Reference_To_Formals
(Id
);
4144 List_Inherited_Pre_Post_Aspects
(Id
);
4145 end Analyze_Generic_Subprogram_Declaration
;
4147 -----------------------------------
4148 -- Analyze_Package_Instantiation --
4149 -----------------------------------
4151 -- WARNING: This routine manages Ghost and SPARK regions. Return statements
4152 -- must be replaced by gotos which jump to the end of the routine in order
4153 -- to restore the Ghost and SPARK modes.
4155 procedure Analyze_Package_Instantiation
(N
: Node_Id
) is
4156 Has_Inline_Always
: Boolean := False;
4157 -- Set if the generic unit contains any subprograms with Inline_Always.
4158 -- Only relevant when back-end inlining is not enabled.
4160 function Might_Inline_Subp
(Gen_Unit
: Entity_Id
) return Boolean;
4161 -- Return True if inlining is active and Gen_Unit contains inlined
4162 -- subprograms. In this case, we may either instantiate the body when
4163 -- front-end inlining is enabled, or add a pending instantiation when
4164 -- back-end inlining is enabled. In the former case, this may cause
4165 -- superfluous instantiations, but in either case we need to perform
4166 -- the instantiation of the body in the context of the instance and
4167 -- not in that of the point of inlining.
4169 function Needs_Body_Instantiated
(Gen_Unit
: Entity_Id
) return Boolean;
4170 -- Return True if Gen_Unit needs to have its body instantiated in the
4171 -- context of N. This in particular excludes generic contexts.
4173 -----------------------
4174 -- Might_Inline_Subp --
4175 -----------------------
4177 function Might_Inline_Subp
(Gen_Unit
: Entity_Id
) return Boolean is
4181 if Inline_Processing_Required
then
4182 -- No need to recompute the answer if we know it is positive
4183 -- and back-end inlining is enabled.
4185 if Is_Inlined
(Gen_Unit
) and then Back_End_Inlining
then
4189 E
:= First_Entity
(Gen_Unit
);
4190 while Present
(E
) loop
4191 if Is_Subprogram
(E
) and then Is_Inlined
(E
) then
4192 -- Remember if there are any subprograms with Inline_Always
4194 if Has_Pragma_Inline_Always
(E
) then
4195 Has_Inline_Always
:= True;
4198 Set_Is_Inlined
(Gen_Unit
);
4207 end Might_Inline_Subp
;
4209 -------------------------------
4210 -- Needs_Body_Instantiated --
4211 -------------------------------
4213 function Needs_Body_Instantiated
(Gen_Unit
: Entity_Id
) return Boolean is
4215 -- No need to instantiate bodies in generic units
4217 if Is_Generic_Unit
(Cunit_Entity
(Main_Unit
)) then
4221 -- If the instantiation is in the main unit, then the body is needed
4223 if Is_In_Main_Unit
(N
) then
4227 -- In GNATprove mode, never instantiate bodies outside of the main
4228 -- unit, as it does not use frontend/backend inlining in the way that
4229 -- GNAT does, so does not benefit from such instantiations. On the
4230 -- contrary, such instantiations may bring artificial constraints,
4231 -- as for example such bodies may require preprocessing.
4233 if GNATprove_Mode
then
4237 -- If not, then again no need to instantiate bodies in generic units
4239 if Is_Generic_Unit
(Cunit_Entity
(Get_Code_Unit
(N
))) then
4243 -- Here we have a special handling for back-end inlining: if inline
4244 -- processing is required, then we unconditionally want to have the
4245 -- body instantiated. The reason is that Might_Inline_Subp does not
4246 -- catch all the cases (as it does not recurse into nested packages)
4247 -- so this avoids the need to patch things up afterwards. Moreover,
4248 -- these instantiations are only performed on demand when back-end
4249 -- inlining is enabled, so this causes very little extra work.
4251 if Inline_Processing_Required
and then Back_End_Inlining
then
4255 -- We want to have the bodies instantiated in non-main units if
4256 -- they might contribute inlined subprograms.
4258 return Might_Inline_Subp
(Gen_Unit
);
4259 end Needs_Body_Instantiated
;
4261 -- Local declarations
4263 Gen_Id
: constant Node_Id
:= Name
(N
);
4264 Inst_Id
: constant Entity_Id
:= Defining_Entity
(N
);
4265 Is_Actual_Pack
: constant Boolean := Is_Internal
(Inst_Id
);
4266 Loc
: constant Source_Ptr
:= Sloc
(N
);
4268 Saved_GM
: constant Ghost_Mode_Type
:= Ghost_Mode
;
4269 Saved_IGR
: constant Node_Id
:= Ignored_Ghost_Region
;
4270 Saved_ISMP
: constant Boolean :=
4271 Ignore_SPARK_Mode_Pragmas_In_Instance
;
4272 Saved_SM
: constant SPARK_Mode_Type
:= SPARK_Mode
;
4273 Saved_SMP
: constant Node_Id
:= SPARK_Mode_Pragma
;
4274 -- Save the Ghost and SPARK mode-related data to restore on exit
4276 Saved_Style_Check
: constant Boolean := Style_Check
;
4277 -- Save style check mode for restore on exit
4280 Act_Decl_Name
: Node_Id
;
4281 Act_Decl_Id
: Entity_Id
;
4284 Env_Installed
: Boolean := False;
4287 Gen_Unit
: Entity_Id
;
4288 Inline_Now
: Boolean := False;
4289 Needs_Body
: Boolean;
4290 Parent_Installed
: Boolean := False;
4291 Renaming_List
: List_Id
;
4292 Unit_Renaming
: Node_Id
;
4294 Vis_Prims_List
: Elist_Id
:= No_Elist
;
4295 -- List of primitives made temporarily visible in the instantiation
4296 -- to match the visibility of the formal type
4298 -- Start of processing for Analyze_Package_Instantiation
4301 -- Preserve relevant elaboration-related attributes of the context which
4302 -- are no longer available or very expensive to recompute once analysis,
4303 -- resolution, and expansion are over.
4305 Mark_Elaboration_Attributes
4312 -- Very first thing: check for Text_IO special unit in case we are
4313 -- instantiating one of the children of [[Wide_]Wide_]Text_IO.
4315 Check_Text_IO_Special_Unit
(Name
(N
));
4317 -- Make node global for error reporting
4319 Instantiation_Node
:= N
;
4321 -- Case of instantiation of a generic package
4323 if Nkind
(N
) = N_Package_Instantiation
then
4324 Act_Decl_Id
:= New_Copy
(Defining_Entity
(N
));
4326 if Nkind
(Defining_Unit_Name
(N
)) = N_Defining_Program_Unit_Name
then
4328 Make_Defining_Program_Unit_Name
(Loc
,
4330 New_Copy_Tree
(Name
(Defining_Unit_Name
(N
))),
4331 Defining_Identifier
=> Act_Decl_Id
);
4333 Act_Decl_Name
:= Act_Decl_Id
;
4336 -- Case of instantiation of a formal package
4339 Act_Decl_Id
:= Defining_Identifier
(N
);
4340 Act_Decl_Name
:= Act_Decl_Id
;
4343 Generate_Definition
(Act_Decl_Id
);
4344 Mutate_Ekind
(Act_Decl_Id
, E_Package
);
4345 Set_Is_Not_Self_Hidden
(Act_Decl_Id
);
4347 -- Initialize list of incomplete actuals before analysis
4349 Set_Incomplete_Actuals
(Act_Decl_Id
, New_Elmt_List
);
4351 Preanalyze_Actuals
(N
, Act_Decl_Id
);
4353 -- Turn off style checking in instances. If the check is enabled on the
4354 -- generic unit, a warning in an instance would just be noise. If not
4355 -- enabled on the generic, then a warning in an instance is just wrong.
4356 -- This must be done after analyzing the actuals, which do come from
4357 -- source and are subject to style checking.
4359 Style_Check
:= False;
4362 Env_Installed
:= True;
4364 -- Reset renaming map for formal types. The mapping is established
4365 -- when analyzing the generic associations, but some mappings are
4366 -- inherited from formal packages of parent units, and these are
4367 -- constructed when the parents are installed.
4369 Generic_Renamings
.Set_Last
(0);
4370 Generic_Renamings_HTable
.Reset
;
4372 -- Except for an abbreviated instance created to check a formal package,
4373 -- install the parent if this is a generic child unit.
4375 if not Is_Abbreviated_Instance
(Inst_Id
) then
4376 Check_Generic_Child_Unit
(Gen_Id
, Parent_Installed
);
4379 Gen_Unit
:= Entity
(Gen_Id
);
4381 -- A package instantiation is Ghost when it is subject to pragma Ghost
4382 -- or the generic template is Ghost. Set the mode now to ensure that
4383 -- any nodes generated during analysis and expansion are marked as
4386 Mark_And_Set_Ghost_Instantiation
(N
, Gen_Unit
);
4388 -- Verify that it is the name of a generic package
4390 -- A visibility glitch: if the instance is a child unit and the generic
4391 -- is the generic unit of a parent instance (i.e. both the parent and
4392 -- the child units are instances of the same package) the name now
4393 -- denotes the renaming within the parent, not the intended generic
4394 -- unit. See if there is a homonym that is the desired generic. The
4395 -- renaming declaration must be visible inside the instance of the
4396 -- child, but not when analyzing the name in the instantiation itself.
4398 if Ekind
(Gen_Unit
) = E_Package
4399 and then Present
(Renamed_Entity
(Gen_Unit
))
4400 and then In_Open_Scopes
(Renamed_Entity
(Gen_Unit
))
4401 and then Is_Generic_Instance
(Renamed_Entity
(Gen_Unit
))
4402 and then Present
(Homonym
(Gen_Unit
))
4404 Gen_Unit
:= Homonym
(Gen_Unit
);
4407 if Etype
(Gen_Unit
) = Any_Type
then
4411 elsif Ekind
(Gen_Unit
) /= E_Generic_Package
then
4413 -- Ada 2005 (AI-50217): Cannot use instance in limited with_clause
4415 if From_Limited_With
(Gen_Unit
) then
4417 ("cannot instantiate a limited withed package", Gen_Id
);
4420 ("& is not the name of a generic package", Gen_Id
, Gen_Unit
);
4427 if In_Extended_Main_Source_Unit
(N
) then
4428 Set_Is_Instantiated
(Gen_Unit
);
4429 Generate_Reference
(Gen_Unit
, N
);
4431 if Present
(Renamed_Entity
(Gen_Unit
)) then
4432 Set_Is_Instantiated
(Renamed_Entity
(Gen_Unit
));
4433 Generate_Reference
(Renamed_Entity
(Gen_Unit
), N
);
4437 if Nkind
(Gen_Id
) = N_Identifier
4438 and then Chars
(Gen_Unit
) = Chars
(Defining_Entity
(N
))
4441 ("& is hidden within declaration of instance", Gen_Id
, Gen_Unit
);
4443 elsif Nkind
(Gen_Id
) = N_Expanded_Name
4444 and then Is_Child_Unit
(Gen_Unit
)
4445 and then Nkind
(Prefix
(Gen_Id
)) = N_Identifier
4446 and then Chars
(Act_Decl_Id
) = Chars
(Prefix
(Gen_Id
))
4449 ("& is hidden within declaration of instance", Prefix
(Gen_Id
));
4452 Set_Entity
(Gen_Id
, Gen_Unit
);
4454 -- If generic is a renaming, get original generic unit
4456 if Present
(Renamed_Entity
(Gen_Unit
))
4457 and then Ekind
(Renamed_Entity
(Gen_Unit
)) = E_Generic_Package
4459 Gen_Unit
:= Renamed_Entity
(Gen_Unit
);
4462 -- Verify that there are no circular instantiations
4464 if In_Open_Scopes
(Gen_Unit
) then
4465 Error_Msg_NE
("instantiation of & within itself", N
, Gen_Unit
);
4469 elsif Contains_Instance_Of
(Gen_Unit
, Current_Scope
, Gen_Id
) then
4470 Error_Msg_Node_2
:= Current_Scope
;
4472 ("circular instantiation: & instantiated in &!", N
, Gen_Unit
);
4473 Circularity_Detected
:= True;
4478 Mutate_Ekind
(Inst_Id
, E_Package
);
4479 Set_Scope
(Inst_Id
, Current_Scope
);
4481 -- If the context of the instance is subject to SPARK_Mode "off" or
4482 -- the annotation is altogether missing, set the global flag which
4483 -- signals Analyze_Pragma to ignore all SPARK_Mode pragmas within
4486 if SPARK_Mode
/= On
then
4487 Ignore_SPARK_Mode_Pragmas_In_Instance
:= True;
4489 -- Mark the instance spec in case the body is instantiated at a
4490 -- later pass. This preserves the original context in effect for
4493 Set_Ignore_SPARK_Mode_Pragmas
(Act_Decl_Id
);
4496 Gen_Decl
:= Unit_Declaration_Node
(Gen_Unit
);
4497 Gen_Spec
:= Specification
(Gen_Decl
);
4499 -- Initialize renamings map, for error checking, and the list that
4500 -- holds private entities whose views have changed between generic
4501 -- definition and instantiation. If this is the instance created to
4502 -- validate an actual package, the instantiation environment is that
4503 -- of the enclosing instance.
4505 Create_Instantiation_Source
(N
, Gen_Unit
, S_Adjustment
);
4507 -- Copy original generic tree, to produce text for instantiation
4511 (Original_Node
(Gen_Decl
), Empty
, Instantiating
=> True);
4513 Act_Spec
:= Specification
(Act_Tree
);
4515 -- If this is the instance created to validate an actual package,
4516 -- only the formals matter, do not examine the package spec itself.
4518 if Is_Actual_Pack
then
4519 Set_Visible_Declarations
(Act_Spec
, New_List
);
4520 Set_Private_Declarations
(Act_Spec
, New_List
);
4524 Analyze_Associations
4526 Formals
=> Generic_Formal_Declarations
(Act_Tree
),
4527 F_Copy
=> Generic_Formal_Declarations
(Gen_Decl
));
4529 Vis_Prims_List
:= Check_Hidden_Primitives
(Renaming_List
);
4531 Set_Instance_Env
(Gen_Unit
, Act_Decl_Id
);
4532 Set_Defining_Unit_Name
(Act_Spec
, Act_Decl_Name
);
4533 Set_Is_Generic_Instance
(Act_Decl_Id
);
4534 Set_Generic_Parent
(Act_Spec
, Gen_Unit
);
4536 -- References to the generic in its own declaration or its body are
4537 -- references to the instance. Add a renaming declaration for the
4538 -- generic unit itself. This declaration, as well as the renaming
4539 -- declarations for the generic formals, must remain private to the
4540 -- unit: the formals, because this is the language semantics, and
4541 -- the unit because its use is an artifact of the implementation.
4544 Make_Package_Renaming_Declaration
(Loc
,
4545 Defining_Unit_Name
=>
4546 Make_Defining_Identifier
(Loc
, Chars
(Gen_Unit
)),
4547 Name
=> New_Occurrence_Of
(Act_Decl_Id
, Loc
));
4549 Append
(Unit_Renaming
, Renaming_List
);
4551 -- The renaming declarations are the first local declarations of the
4554 if Is_Non_Empty_List
(Visible_Declarations
(Act_Spec
)) then
4556 (First
(Visible_Declarations
(Act_Spec
)), Renaming_List
);
4558 Set_Visible_Declarations
(Act_Spec
, Renaming_List
);
4561 Act_Decl
:= Make_Package_Declaration
(Loc
, Specification
=> Act_Spec
);
4563 -- Propagate the aspect specifications from the package declaration
4564 -- template to the instantiated version of the package declaration.
4566 if Has_Aspects
(Act_Tree
) then
4567 Set_Aspect_Specifications
(Act_Decl
,
4568 New_Copy_List_Tree
(Aspect_Specifications
(Act_Tree
)));
4571 -- The generic may have a generated Default_Storage_Pool aspect,
4572 -- set at the point of generic declaration. If the instance has
4573 -- that aspect, it overrides the one inherited from the generic.
4575 if Has_Aspects
(Gen_Spec
) then
4576 if No
(Aspect_Specifications
(N
)) then
4577 Set_Aspect_Specifications
(N
,
4579 (Aspect_Specifications
(Gen_Spec
))));
4583 Inherited_Aspects
: constant List_Id
:=
4585 (Aspect_Specifications
(Gen_Spec
));
4589 Pool_Present
: Boolean := False;
4592 ASN1
:= First
(Aspect_Specifications
(N
));
4593 while Present
(ASN1
) loop
4594 if Chars
(Identifier
(ASN1
)) =
4595 Name_Default_Storage_Pool
4597 Pool_Present
:= True;
4604 if Pool_Present
then
4606 -- If generic carries a default storage pool, remove it
4607 -- in favor of the instance one.
4609 ASN2
:= First
(Inherited_Aspects
);
4610 while Present
(ASN2
) loop
4611 if Chars
(Identifier
(ASN2
)) =
4612 Name_Default_Storage_Pool
4623 (Aspect_Specifications
(N
), Inherited_Aspects
);
4628 -- Save the instantiation node for a subsequent instantiation of the
4629 -- body if there is one and it needs to be instantiated here.
4631 -- We instantiate the body only if we are generating code, or if we
4632 -- are generating cross-reference information, or for GNATprove use.
4635 Enclosing_Body_Present
: Boolean := False;
4636 -- If the generic unit is not a compilation unit, then a body may
4637 -- be present in its parent even if none is required. We create a
4638 -- tentative pending instantiation for the body, which will be
4639 -- discarded if none is actually present.
4644 if Scope
(Gen_Unit
) /= Standard_Standard
4645 and then not Is_Child_Unit
(Gen_Unit
)
4647 Scop
:= Scope
(Gen_Unit
);
4648 while Present
(Scop
) and then Scop
/= Standard_Standard
loop
4649 if Unit_Requires_Body
(Scop
) then
4650 Enclosing_Body_Present
:= True;
4653 elsif In_Open_Scopes
(Scop
)
4654 and then In_Package_Body
(Scop
)
4656 Enclosing_Body_Present
:= True;
4660 exit when Is_Compilation_Unit
(Scop
);
4661 Scop
:= Scope
(Scop
);
4665 -- If front-end inlining is enabled or there are any subprograms
4666 -- marked with Inline_Always, and this is a unit for which code
4667 -- will be generated, we instantiate the body at once.
4669 -- This is done if the instance is not the main unit, and if the
4670 -- generic is not a child unit of another generic, to avoid scope
4671 -- problems and the reinstallation of parent instances.
4674 and then (not Is_Child_Unit
(Gen_Unit
)
4675 or else not Is_Generic_Unit
(Scope
(Gen_Unit
)))
4676 and then Might_Inline_Subp
(Gen_Unit
)
4677 and then not Is_Actual_Pack
4679 if not Back_End_Inlining
4680 and then (Front_End_Inlining
or else Has_Inline_Always
)
4681 and then (Is_In_Main_Unit
(N
)
4682 or else In_Main_Context
(Current_Scope
))
4683 and then Nkind
(Parent
(N
)) /= N_Compilation_Unit
4687 -- In configurable_run_time mode we force the inlining of
4688 -- predefined subprograms marked Inline_Always, to minimize
4689 -- the use of the run-time library.
4691 elsif In_Predefined_Unit
(Gen_Decl
)
4692 and then Configurable_Run_Time_Mode
4693 and then Nkind
(Parent
(N
)) /= N_Compilation_Unit
4698 -- If the current scope is itself an instance within a child
4699 -- unit, there will be duplications in the scope stack, and the
4700 -- unstacking mechanism in Inline_Instance_Body will fail.
4701 -- This loses some rare cases of optimization.
4703 if Is_Generic_Instance
(Current_Scope
) then
4705 Curr_Unit
: constant Entity_Id
:=
4706 Cunit_Entity
(Current_Sem_Unit
);
4708 if Curr_Unit
/= Current_Scope
4709 and then Is_Child_Unit
(Curr_Unit
)
4711 Inline_Now
:= False;
4718 (Unit_Requires_Body
(Gen_Unit
)
4719 or else Enclosing_Body_Present
4720 or else Present
(Corresponding_Body
(Gen_Decl
)))
4721 and then Needs_Body_Instantiated
(Gen_Unit
)
4722 and then not Is_Actual_Pack
4723 and then not Inline_Now
4724 and then (Operating_Mode
= Generate_Code
4725 or else (Operating_Mode
= Check_Semantics
4726 and then GNATprove_Mode
));
4728 -- If front-end inlining is enabled or there are any subprograms
4729 -- marked with Inline_Always, do not instantiate body when within
4730 -- a generic context.
4732 if not Back_End_Inlining
4733 and then (Front_End_Inlining
or else Has_Inline_Always
)
4734 and then not Expander_Active
4736 Needs_Body
:= False;
4739 -- If the current context is generic, and the package being
4740 -- instantiated is declared within a formal package, there is no
4741 -- body to instantiate until the enclosing generic is instantiated
4742 -- and there is an actual for the formal package. If the formal
4743 -- package has parameters, we build a regular package instance for
4744 -- it, that precedes the original formal package declaration.
4746 if In_Open_Scopes
(Scope
(Scope
(Gen_Unit
))) then
4748 Decl
: constant Node_Id
:=
4750 (Unit_Declaration_Node
(Scope
(Gen_Unit
)));
4752 if Nkind
(Decl
) = N_Formal_Package_Declaration
4753 or else (Nkind
(Decl
) = N_Package_Declaration
4754 and then Is_List_Member
(Decl
)
4755 and then Present
(Next
(Decl
))
4757 Nkind
(Next
(Decl
)) =
4758 N_Formal_Package_Declaration
)
4760 Needs_Body
:= False;
4766 -- For RCI unit calling stubs, we omit the instance body if the
4767 -- instance is the RCI library unit itself.
4769 -- However there is a special case for nested instances: in this case
4770 -- we do generate the instance body, as it might be required, e.g.
4771 -- because it provides stream attributes for some type used in the
4772 -- profile of a remote subprogram. This is consistent with 12.3(12),
4773 -- which indicates that the instance body occurs at the place of the
4774 -- instantiation, and thus is part of the RCI declaration, which is
4775 -- present on all client partitions (this is E.2.3(18)).
4777 -- Note that AI12-0002 may make it illegal at some point to have
4778 -- stream attributes defined in an RCI unit, in which case this
4779 -- special case will become unnecessary. In the meantime, there
4780 -- is known application code in production that depends on this
4781 -- being possible, so we definitely cannot eliminate the body in
4782 -- the case of nested instances for the time being.
4784 -- When we generate a nested instance body, calling stubs for any
4785 -- relevant subprogram will be inserted immediately after the
4786 -- subprogram declarations, and will take precedence over the
4787 -- subsequent (original) body. (The stub and original body will be
4788 -- complete homographs, but this is permitted in an instance).
4789 -- (Could we do better and remove the original body???)
4791 if Distribution_Stub_Mode
= Generate_Caller_Stub_Body
4792 and then Comes_From_Source
(N
)
4793 and then Nkind
(Parent
(N
)) = N_Compilation_Unit
4795 Needs_Body
:= False;
4798 -- If the context requires a full instantiation, set things up for
4799 -- subsequent construction of the body.
4803 Fin_Scop
, S
: Entity_Id
;
4806 Check_Forward_Instantiation
(Gen_Decl
);
4810 -- For a package instantiation that is not a compilation unit,
4811 -- indicate that cleanup actions of the innermost enclosing
4812 -- scope for which they are generated should be delayed until
4813 -- after the package body is instantiated.
4815 if Nkind
(N
) = N_Package_Instantiation
4816 and then not Is_Compilation_Unit
(Act_Decl_Id
)
4820 while S
/= Standard_Standard
loop
4821 -- Cleanup actions are not generated within generic units
4822 -- or in the formal part of generic units.
4825 or else Is_Generic_Unit
(S
)
4826 or else Ekind
(S
) = E_Void
4830 -- For package scopes, cleanup actions are generated only
4831 -- for compilation units, for spec and body separately.
4833 elsif Ekind
(S
) = E_Package
then
4834 if Is_Compilation_Unit
(S
) then
4835 if In_Package_Body
(S
) then
4836 Fin_Scop
:= Body_Entity
(S
);
4841 Set_Delay_Cleanups
(Fin_Scop
);
4848 -- Cleanup actions are generated for all dynamic scopes
4852 Set_Delay_Cleanups
(Fin_Scop
);
4858 Add_Pending_Instantiation
(N
, Act_Decl
, Fin_Scop
);
4862 Set_Categorization_From_Pragmas
(Act_Decl
);
4864 if Parent_Installed
then
4868 Set_Instance_Spec
(N
, Act_Decl
);
4870 -- If not a compilation unit, insert the package declaration before
4871 -- the original instantiation node.
4873 if Nkind
(Parent
(N
)) /= N_Compilation_Unit
then
4874 Mark_Rewrite_Insertion
(Act_Decl
);
4875 Insert_Before
(N
, Act_Decl
);
4877 if Has_Aspects
(N
) then
4878 Analyze_Aspect_Specifications
(N
, Act_Decl_Id
);
4880 -- The pragma created for a Default_Storage_Pool aspect must
4881 -- appear ahead of the declarations in the instance spec.
4882 -- Analysis has placed it after the instance node, so remove
4883 -- it and reinsert it properly now.
4886 ASN
: constant Node_Id
:= First
(Aspect_Specifications
(N
));
4887 A_Name
: constant Name_Id
:= Chars
(Identifier
(ASN
));
4891 if A_Name
= Name_Default_Storage_Pool
then
4892 if No
(Visible_Declarations
(Act_Spec
)) then
4893 Set_Visible_Declarations
(Act_Spec
, New_List
);
4897 while Present
(Decl
) loop
4898 if Nkind
(Decl
) = N_Pragma
then
4900 Prepend
(Decl
, Visible_Declarations
(Act_Spec
));
4912 -- For an instantiation that is a compilation unit, place
4913 -- declaration on current node so context is complete for analysis
4914 -- (including nested instantiations). If this is the main unit,
4915 -- the declaration eventually replaces the instantiation node.
4916 -- If the instance body is created later, it replaces the
4917 -- instance node, and the declaration is attached to it
4918 -- (see Build_Instance_Compilation_Unit_Nodes).
4921 if Cunit_Entity
(Current_Sem_Unit
) = Defining_Entity
(N
) then
4923 -- The entity for the current unit is the newly created one,
4924 -- and all semantic information is attached to it.
4926 Set_Cunit_Entity
(Current_Sem_Unit
, Act_Decl_Id
);
4928 -- If this is the main unit, replace the main entity as well
4930 if Current_Sem_Unit
= Main_Unit
then
4931 Main_Unit_Entity
:= Act_Decl_Id
;
4935 Set_Unit
(Parent
(N
), Act_Decl
);
4936 Set_Parent_Spec
(Act_Decl
, Parent_Spec
(N
));
4937 Set_Package_Instantiation
(Act_Decl_Id
, N
);
4939 -- Process aspect specifications of the instance node, if any, to
4940 -- take into account categorization pragmas before analyzing the
4943 if Has_Aspects
(N
) then
4944 Analyze_Aspect_Specifications
(N
, Act_Decl_Id
);
4948 Set_Unit
(Parent
(N
), N
);
4949 Set_Body_Required
(Parent
(N
), False);
4951 -- We never need elaboration checks on instantiations, since by
4952 -- definition, the body instantiation is elaborated at the same
4953 -- time as the spec instantiation.
4955 if Legacy_Elaboration_Checks
then
4956 Set_Kill_Elaboration_Checks
(Act_Decl_Id
);
4957 Set_Suppress_Elaboration_Warnings
(Act_Decl_Id
);
4961 if Legacy_Elaboration_Checks
then
4962 Check_Elab_Instantiation
(N
);
4965 -- Save the scenario for later examination by the ABE Processing
4968 Record_Elaboration_Scenario
(N
);
4970 -- The instantiation results in a guaranteed ABE
4972 if Is_Known_Guaranteed_ABE
(N
) and then Needs_Body
then
4973 -- Do not instantiate the corresponding body because gigi cannot
4974 -- handle certain types of premature instantiations.
4976 Remove_Dead_Instance
(N
);
4978 -- Create completing bodies for all subprogram declarations since
4979 -- their real bodies will not be instantiated.
4981 Provide_Completing_Bodies
(Instance_Spec
(N
));
4984 Check_Hidden_Child_Unit
(N
, Gen_Unit
, Act_Decl_Id
);
4986 Set_First_Private_Entity
(Defining_Unit_Name
(Unit_Renaming
),
4987 First_Private_Entity
(Act_Decl_Id
));
4989 -- If the instantiation needs a body, the unit will be turned into
4990 -- a package body and receive its own elaboration entity. Otherwise,
4991 -- the nature of the unit is now a package declaration.
4993 -- Note that the below rewriting means that Act_Decl, which has been
4994 -- analyzed and expanded, will be re-expanded as the rewritten N.
4996 if Nkind
(Parent
(N
)) = N_Compilation_Unit
4997 and then not Needs_Body
4999 Rewrite
(N
, Act_Decl
);
5002 if Present
(Corresponding_Body
(Gen_Decl
))
5003 or else Unit_Requires_Body
(Gen_Unit
)
5005 Set_Has_Completion
(Act_Decl_Id
);
5008 Check_Formal_Packages
(Act_Decl_Id
);
5010 Restore_Hidden_Primitives
(Vis_Prims_List
);
5011 Restore_Private_Views
(Act_Decl_Id
);
5013 Inherit_Context
(Gen_Decl
, N
);
5015 if Parent_Installed
then
5020 Env_Installed
:= False;
5023 Validate_Categorization_Dependency
(N
, Act_Decl_Id
);
5025 -- There used to be a check here to prevent instantiations in local
5026 -- contexts if the No_Local_Allocators restriction was active. This
5027 -- check was removed by a binding interpretation in AI-95-00130/07,
5028 -- but we retain the code for documentation purposes.
5030 -- if Ekind (Act_Decl_Id) /= E_Void
5031 -- and then not Is_Library_Level_Entity (Act_Decl_Id)
5033 -- Check_Restriction (No_Local_Allocators, N);
5037 Inline_Instance_Body
(N
, Gen_Unit
, Act_Decl
);
5040 -- Check that if N is an instantiation of System.Dim_Float_IO or
5041 -- System.Dim_Integer_IO, the formal type has a dimension system.
5043 if Nkind
(N
) = N_Package_Instantiation
5044 and then Is_Dim_IO_Package_Instantiation
(N
)
5047 Assoc
: constant Node_Id
:= First
(Generic_Associations
(N
));
5049 if not Has_Dimension_System
5050 (Etype
(Explicit_Generic_Actual_Parameter
(Assoc
)))
5052 Error_Msg_N
("type with a dimension system expected", Assoc
);
5058 if Has_Aspects
(N
) and then Nkind
(Parent
(N
)) /= N_Compilation_Unit
then
5059 Analyze_Aspect_Specifications
(N
, Act_Decl_Id
);
5062 Ignore_SPARK_Mode_Pragmas_In_Instance
:= Saved_ISMP
;
5063 Restore_Ghost_Region
(Saved_GM
, Saved_IGR
);
5064 Restore_SPARK_Mode
(Saved_SM
, Saved_SMP
);
5065 Style_Check
:= Saved_Style_Check
;
5068 when Instantiation_Error
=>
5069 if Parent_Installed
then
5073 if Env_Installed
then
5077 Ignore_SPARK_Mode_Pragmas_In_Instance
:= Saved_ISMP
;
5078 Restore_Ghost_Region
(Saved_GM
, Saved_IGR
);
5079 Restore_SPARK_Mode
(Saved_SM
, Saved_SMP
);
5080 Style_Check
:= Saved_Style_Check
;
5081 end Analyze_Package_Instantiation
;
5083 --------------------------
5084 -- Inline_Instance_Body --
5085 --------------------------
5087 -- WARNING: This routine manages SPARK regions. Return statements must be
5088 -- replaced by gotos which jump to the end of the routine and restore the
5091 procedure Inline_Instance_Body
5093 Gen_Unit
: Entity_Id
;
5096 Config_Attrs
: constant Config_Switches_Type
:= Save_Config_Switches
;
5098 Curr_Comp
: constant Node_Id
:= Cunit
(Current_Sem_Unit
);
5099 Curr_Unit
: constant Entity_Id
:= Cunit_Entity
(Current_Sem_Unit
);
5100 Gen_Comp
: constant Entity_Id
:=
5101 Cunit_Entity
(Get_Source_Unit
(Gen_Unit
));
5103 Scope_Stack_Depth
: constant Pos
:=
5104 Scope_Stack
.Last
- Scope_Stack
.First
+ 1;
5106 Inner_Scopes
: array (1 .. Scope_Stack_Depth
) of Entity_Id
;
5107 Instances
: array (1 .. Scope_Stack_Depth
) of Entity_Id
;
5108 Use_Clauses
: array (1 .. Scope_Stack_Depth
) of Node_Id
;
5110 Curr_Scope
: Entity_Id
:= Empty
;
5111 List
: Elist_Id
:= No_Elist
; -- init to avoid warning
5112 N_Instances
: Nat
:= 0;
5113 Num_Inner
: Nat
:= 0;
5114 Num_Scopes
: Nat
:= 0;
5115 Removed
: Boolean := False;
5120 -- Case of generic unit defined in another unit. We must remove the
5121 -- complete context of the current unit to install that of the generic.
5123 if Gen_Comp
/= Cunit_Entity
(Current_Sem_Unit
) then
5125 -- Loop through enclosing scopes until we reach a generic instance,
5126 -- package body, or subprogram.
5129 while Present
(S
) and then S
/= Standard_Standard
loop
5131 -- Save use clauses from enclosing scopes into Use_Clauses
5134 Num_Scopes
:= Num_Scopes
+ 1;
5136 Use_Clauses
(Num_Scopes
) :=
5138 (Scope_Stack
.Last
- Num_Scopes
+ 1).First_Use_Clause
);
5139 End_Use_Clauses
(Use_Clauses
(Num_Scopes
));
5141 exit when Scope_Stack
.Last
- Num_Scopes
+ 1 = Scope_Stack
.First
5142 or else Scope_Stack
.Table
5143 (Scope_Stack
.Last
- Num_Scopes
).Entity
= Scope
(S
);
5146 exit when Is_Generic_Instance
(S
)
5147 and then (In_Package_Body
(S
)
5148 or else Ekind
(S
) = E_Procedure
5149 or else Ekind
(S
) = E_Function
);
5153 Vis
:= Is_Immediately_Visible
(Gen_Comp
);
5155 -- Find and save all enclosing instances
5160 and then S
/= Standard_Standard
5162 if Is_Generic_Instance
(S
) then
5163 N_Instances
:= N_Instances
+ 1;
5164 Instances
(N_Instances
) := S
;
5166 exit when In_Package_Body
(S
);
5172 -- Remove context of current compilation unit, unless we are within a
5173 -- nested package instantiation, in which case the context has been
5174 -- removed previously.
5176 -- If current scope is the body of a child unit, remove context of
5177 -- spec as well. If an enclosing scope is an instance body, the
5178 -- context has already been removed, but the entities in the body
5179 -- must be made invisible as well.
5182 while Present
(S
) and then S
/= Standard_Standard
loop
5183 if Is_Generic_Instance
(S
)
5184 and then (In_Package_Body
(S
)
5185 or else Ekind
(S
) in E_Procedure | E_Function
)
5187 -- We still have to remove the entities of the enclosing
5188 -- instance from direct visibility.
5193 E
:= First_Entity
(S
);
5194 while Present
(E
) loop
5195 Set_Is_Immediately_Visible
(E
, False);
5204 or else (Ekind
(Curr_Unit
) = E_Package_Body
5205 and then S
= Spec_Entity
(Curr_Unit
))
5206 or else (Ekind
(Curr_Unit
) = E_Subprogram_Body
5207 and then S
= Corresponding_Spec
5208 (Unit_Declaration_Node
(Curr_Unit
)))
5212 -- Remove entities in current scopes from visibility, so that
5213 -- instance body is compiled in a clean environment.
5215 List
:= Save_Scope_Stack
(Handle_Use
=> False);
5217 if Is_Child_Unit
(S
) then
5219 -- Remove child unit from stack, as well as inner scopes.
5220 -- Removing the context of a child unit removes parent units
5223 while Current_Scope
/= S
loop
5224 Num_Inner
:= Num_Inner
+ 1;
5225 Inner_Scopes
(Num_Inner
) := Current_Scope
;
5230 Remove_Context
(Curr_Comp
);
5234 Remove_Context
(Curr_Comp
);
5237 if Ekind
(Curr_Unit
) = E_Package_Body
then
5238 Remove_Context
(Library_Unit
(Curr_Comp
));
5245 pragma Assert
(Num_Inner
< Num_Scopes
);
5247 Push_Scope
(Standard_Standard
);
5248 Scope_Stack
.Table
(Scope_Stack
.Last
).Is_Active_Stack_Base
:= True;
5250 -- The inlined package body is analyzed with the configuration state
5251 -- of the context prior to the scope manipulations performed above.
5253 -- ??? shouldn't this also use the warning state of the context prior
5254 -- to the scope manipulations?
5256 Instantiate_Package_Body
5259 Act_Decl
=> Act_Decl
,
5261 Config_Switches
=> Config_Attrs
,
5262 Current_Sem_Unit
=> Current_Sem_Unit
,
5263 Expander_Status
=> Expander_Active
,
5264 Local_Suppress_Stack_Top
=> Local_Suppress_Stack_Top
,
5265 Scope_Suppress
=> Scope_Suppress
,
5266 Warnings
=> Save_Warnings
)),
5267 Inlined_Body
=> True);
5273 Set_Is_Immediately_Visible
(Gen_Comp
, Vis
);
5275 -- Reset Generic_Instance flag so that use clauses can be installed
5276 -- in the proper order. (See Use_One_Package for effect of enclosing
5277 -- instances on processing of use clauses).
5279 for J
in 1 .. N_Instances
loop
5280 Set_Is_Generic_Instance
(Instances
(J
), False);
5284 Install_Context
(Curr_Comp
, Chain
=> False);
5286 if Present
(Curr_Scope
)
5287 and then Is_Child_Unit
(Curr_Scope
)
5289 Push_Scope
(Curr_Scope
);
5290 Set_Is_Immediately_Visible
(Curr_Scope
);
5292 -- Finally, restore inner scopes as well
5294 for J
in reverse 1 .. Num_Inner
loop
5295 Push_Scope
(Inner_Scopes
(J
));
5299 Restore_Scope_Stack
(List
, Handle_Use
=> False);
5301 if Present
(Curr_Scope
)
5303 (In_Private_Part
(Curr_Scope
)
5304 or else In_Package_Body
(Curr_Scope
))
5306 -- Install private declaration of ancestor units, which are
5307 -- currently available. Restore_Scope_Stack and Install_Context
5308 -- only install the visible part of parents.
5313 Par
:= Scope
(Curr_Scope
);
5314 while Present
(Par
) and then Par
/= Standard_Standard
loop
5315 Install_Private_Declarations
(Par
);
5322 -- Restore use clauses. For a child unit, use clauses in the parents
5323 -- are restored when installing the context, so only those in inner
5324 -- scopes (and those local to the child unit itself) need to be
5325 -- installed explicitly.
5327 if Is_Child_Unit
(Curr_Unit
) and then Removed
then
5328 for J
in reverse 1 .. Num_Inner
+ 1 loop
5329 Scope_Stack
.Table
(Scope_Stack
.Last
- J
+ 1).First_Use_Clause
:=
5331 Install_Use_Clauses
(Use_Clauses
(J
));
5335 for J
in reverse 1 .. Num_Scopes
loop
5336 Scope_Stack
.Table
(Scope_Stack
.Last
- J
+ 1).First_Use_Clause
:=
5338 Install_Use_Clauses
(Use_Clauses
(J
));
5342 -- Restore status of instances. If one of them is a body, make its
5343 -- local entities visible again.
5350 for J
in 1 .. N_Instances
loop
5351 Inst
:= Instances
(J
);
5352 Set_Is_Generic_Instance
(Inst
, True);
5354 if In_Package_Body
(Inst
)
5355 or else Ekind
(S
) in E_Procedure | E_Function
5357 E
:= First_Entity
(Instances
(J
));
5358 while Present
(E
) loop
5359 Set_Is_Immediately_Visible
(E
);
5366 -- If generic unit is in current unit, current context is correct. Note
5367 -- that the context is guaranteed to carry the correct SPARK_Mode as no
5368 -- enclosing scopes were removed.
5371 Instantiate_Package_Body
5374 Act_Decl
=> Act_Decl
,
5376 Config_Switches
=> Save_Config_Switches
,
5377 Current_Sem_Unit
=> Current_Sem_Unit
,
5378 Expander_Status
=> Expander_Active
,
5379 Local_Suppress_Stack_Top
=> Local_Suppress_Stack_Top
,
5380 Scope_Suppress
=> Scope_Suppress
,
5381 Warnings
=> Save_Warnings
)),
5382 Inlined_Body
=> True);
5384 end Inline_Instance_Body
;
5386 -------------------------------------
5387 -- Analyze_Procedure_Instantiation --
5388 -------------------------------------
5390 procedure Analyze_Procedure_Instantiation
(N
: Node_Id
) is
5392 Analyze_Subprogram_Instantiation
(N
, E_Procedure
);
5393 end Analyze_Procedure_Instantiation
;
5395 -----------------------------------
5396 -- Need_Subprogram_Instance_Body --
5397 -----------------------------------
5399 function Need_Subprogram_Instance_Body
5401 Subp
: Entity_Id
) return Boolean
5403 function Is_Inlined_Or_Child_Of_Inlined
(E
: Entity_Id
) return Boolean;
5404 -- Return True if E is an inlined subprogram, an inlined renaming or a
5405 -- subprogram nested in an inlined subprogram. The inlining machinery
5406 -- totally disregards nested subprograms since it considers that they
5407 -- will always be compiled if the parent is (see Inline.Is_Nested).
5409 ------------------------------------
5410 -- Is_Inlined_Or_Child_Of_Inlined --
5411 ------------------------------------
5413 function Is_Inlined_Or_Child_Of_Inlined
(E
: Entity_Id
) return Boolean is
5417 if Is_Inlined
(E
) or else Is_Inlined
(Alias
(E
)) then
5422 while Scop
/= Standard_Standard
loop
5423 if Is_Subprogram
(Scop
) and then Is_Inlined
(Scop
) then
5427 Scop
:= Scope
(Scop
);
5431 end Is_Inlined_Or_Child_Of_Inlined
;
5434 -- Must be in the main unit or inlined (or child of inlined)
5436 if (Is_In_Main_Unit
(N
) or else Is_Inlined_Or_Child_Of_Inlined
(Subp
))
5438 -- Must be generating code or analyzing code in GNATprove mode
5440 and then (Operating_Mode
= Generate_Code
5441 or else (Operating_Mode
= Check_Semantics
5442 and then GNATprove_Mode
))
5444 -- The body is needed when generating code (full expansion) and in
5445 -- in GNATprove mode (special expansion) for formal verification of
5448 and then (Expander_Active
or GNATprove_Mode
)
5450 -- No point in inlining if ABE is inevitable
5452 and then not Is_Known_Guaranteed_ABE
(N
)
5454 -- Or if subprogram is eliminated
5456 and then not Is_Eliminated
(Subp
)
5458 Add_Pending_Instantiation
(N
, Unit_Declaration_Node
(Subp
));
5461 -- Here if not inlined, or we ignore the inlining
5466 end Need_Subprogram_Instance_Body
;
5468 --------------------------------------
5469 -- Analyze_Subprogram_Instantiation --
5470 --------------------------------------
5472 -- WARNING: This routine manages Ghost and SPARK regions. Return statements
5473 -- must be replaced by gotos which jump to the end of the routine in order
5474 -- to restore the Ghost and SPARK modes.
5476 procedure Analyze_Subprogram_Instantiation
5480 Errs
: constant Nat
:= Serious_Errors_Detected
;
5481 Gen_Id
: constant Node_Id
:= Name
(N
);
5482 Inst_Id
: constant Entity_Id
:= Defining_Entity
(N
);
5483 Anon_Id
: constant Entity_Id
:=
5484 Make_Defining_Identifier
(Sloc
(Inst_Id
),
5485 Chars
=> New_External_Name
(Chars
(Inst_Id
), 'R'));
5486 Loc
: constant Source_Ptr
:= Sloc
(N
);
5488 Act_Decl_Id
: Entity_Id
:= Empty
; -- init to avoid warning
5493 Env_Installed
: Boolean := False;
5494 Gen_Unit
: Entity_Id
;
5496 Pack_Id
: Entity_Id
;
5497 Parent_Installed
: Boolean := False;
5499 Renaming_List
: List_Id
;
5500 -- The list of declarations that link formals and actuals of the
5501 -- instance. These are subtype declarations for formal types, and
5502 -- renaming declarations for other formals. The subprogram declaration
5503 -- for the instance is then appended to the list, and the last item on
5504 -- the list is the renaming declaration for the instance.
5506 procedure Analyze_Instance_And_Renamings
;
5507 -- The instance must be analyzed in a context that includes the mappings
5508 -- of generic parameters into actuals. We create a package declaration
5509 -- for this purpose, and a subprogram with an internal name within the
5510 -- package. The subprogram instance is simply an alias for the internal
5511 -- subprogram, declared in the current scope.
5513 procedure Build_Subprogram_Renaming
;
5514 -- If the subprogram is recursive, there are occurrences of the name of
5515 -- the generic within the body, which must resolve to the current
5516 -- instance. We add a renaming declaration after the declaration, which
5517 -- is available in the instance body, as well as in the analysis of
5518 -- aspects that appear in the generic. This renaming declaration is
5519 -- inserted after the instance declaration which it renames.
5521 ------------------------------------
5522 -- Analyze_Instance_And_Renamings --
5523 ------------------------------------
5525 procedure Analyze_Instance_And_Renamings
is
5526 Def_Ent
: constant Entity_Id
:= Defining_Entity
(N
);
5527 Pack_Decl
: Node_Id
;
5530 if Nkind
(Parent
(N
)) = N_Compilation_Unit
then
5532 -- For the case of a compilation unit, the container package has
5533 -- the same name as the instantiation, to insure that the binder
5534 -- calls the elaboration procedure with the right name. Copy the
5535 -- entity of the instance, which may have compilation level flags
5536 -- (e.g. Is_Child_Unit) set.
5538 Pack_Id
:= New_Copy
(Def_Ent
);
5541 -- Otherwise we use the name of the instantiation concatenated
5542 -- with its source position to ensure uniqueness if there are
5543 -- several instantiations with the same name.
5546 Make_Defining_Identifier
(Loc
,
5547 Chars
=> New_External_Name
5548 (Related_Id
=> Chars
(Def_Ent
),
5550 Suffix_Index
=> Source_Offset
(Sloc
(Def_Ent
))));
5554 Make_Package_Declaration
(Loc
,
5555 Specification
=> Make_Package_Specification
(Loc
,
5556 Defining_Unit_Name
=> Pack_Id
,
5557 Visible_Declarations
=> Renaming_List
,
5558 End_Label
=> Empty
));
5560 Set_Instance_Spec
(N
, Pack_Decl
);
5561 Set_Is_Generic_Instance
(Pack_Id
);
5562 Set_Debug_Info_Needed
(Pack_Id
);
5564 -- Case of not a compilation unit
5566 if Nkind
(Parent
(N
)) /= N_Compilation_Unit
then
5567 Mark_Rewrite_Insertion
(Pack_Decl
);
5568 Insert_Before
(N
, Pack_Decl
);
5569 Set_Has_Completion
(Pack_Id
);
5571 -- Case of an instantiation that is a compilation unit
5573 -- Place declaration on current node so context is complete for
5574 -- analysis (including nested instantiations), and for use in a
5575 -- context_clause (see Analyze_With_Clause).
5578 Set_Unit
(Parent
(N
), Pack_Decl
);
5579 Set_Parent_Spec
(Pack_Decl
, Parent_Spec
(N
));
5582 Analyze
(Pack_Decl
);
5583 Check_Formal_Packages
(Pack_Id
);
5585 -- Body of the enclosing package is supplied when instantiating the
5586 -- subprogram body, after semantic analysis is completed.
5588 if Nkind
(Parent
(N
)) = N_Compilation_Unit
then
5590 -- Remove package itself from visibility, so it does not
5591 -- conflict with subprogram.
5593 Set_Name_Entity_Id
(Chars
(Pack_Id
), Homonym
(Pack_Id
));
5595 -- Set name and scope of internal subprogram so that the proper
5596 -- external name will be generated. The proper scope is the scope
5597 -- of the wrapper package. We need to generate debugging info for
5598 -- the internal subprogram, so set flag accordingly.
5600 Set_Chars
(Anon_Id
, Chars
(Defining_Entity
(N
)));
5601 Set_Scope
(Anon_Id
, Scope
(Pack_Id
));
5603 -- Mark wrapper package as referenced, to avoid spurious warnings
5604 -- if the instantiation appears in various with_ clauses of
5605 -- subunits of the main unit.
5607 Set_Referenced
(Pack_Id
);
5610 Set_Is_Generic_Instance
(Anon_Id
);
5611 Set_Debug_Info_Needed
(Anon_Id
);
5612 Act_Decl_Id
:= New_Copy
(Anon_Id
);
5614 Set_Parent
(Act_Decl_Id
, Parent
(Anon_Id
));
5615 Set_Chars
(Act_Decl_Id
, Chars
(Defining_Entity
(N
)));
5616 Set_Sloc
(Act_Decl_Id
, Sloc
(Defining_Entity
(N
)));
5618 -- Subprogram instance comes from source only if generic does
5620 Preserve_Comes_From_Source
(Act_Decl_Id
, Gen_Unit
);
5622 -- If the instance is a child unit, mark the Id accordingly. Mark
5623 -- the anonymous entity as well, which is the real subprogram and
5624 -- which is used when the instance appears in a context clause.
5625 -- Similarly, propagate the Is_Eliminated flag to handle properly
5626 -- nested eliminated subprograms.
5628 Set_Is_Child_Unit
(Act_Decl_Id
, Is_Child_Unit
(Defining_Entity
(N
)));
5629 Set_Is_Child_Unit
(Anon_Id
, Is_Child_Unit
(Defining_Entity
(N
)));
5630 New_Overloaded_Entity
(Act_Decl_Id
);
5631 Check_Eliminated
(Act_Decl_Id
);
5632 Set_Is_Eliminated
(Anon_Id
, Is_Eliminated
(Act_Decl_Id
));
5634 if Nkind
(Parent
(N
)) = N_Compilation_Unit
then
5636 -- In compilation unit case, kill elaboration checks on the
5637 -- instantiation, since they are never needed - the body is
5638 -- instantiated at the same point as the spec.
5640 if Legacy_Elaboration_Checks
then
5641 Set_Kill_Elaboration_Checks
(Act_Decl_Id
);
5642 Set_Suppress_Elaboration_Warnings
(Act_Decl_Id
);
5645 Set_Is_Compilation_Unit
(Anon_Id
);
5646 Set_Cunit_Entity
(Current_Sem_Unit
, Pack_Id
);
5649 -- The instance is not a freezing point for the new subprogram.
5650 -- The anonymous subprogram may have a freeze node, created for
5651 -- some delayed aspects. This freeze node must not be inherited
5652 -- by the visible subprogram entity.
5654 Set_Is_Frozen
(Act_Decl_Id
, False);
5655 Set_Freeze_Node
(Act_Decl_Id
, Empty
);
5657 if Nkind
(Defining_Entity
(N
)) = N_Defining_Operator_Symbol
then
5658 Valid_Operator_Definition
(Act_Decl_Id
);
5661 Set_Alias
(Act_Decl_Id
, Anon_Id
);
5662 Set_Has_Completion
(Act_Decl_Id
);
5663 Set_Related_Instance
(Pack_Id
, Act_Decl_Id
);
5665 if Nkind
(Parent
(N
)) = N_Compilation_Unit
then
5666 Set_Body_Required
(Parent
(N
), False);
5668 end Analyze_Instance_And_Renamings
;
5670 -------------------------------
5671 -- Build_Subprogram_Renaming --
5672 -------------------------------
5674 procedure Build_Subprogram_Renaming
is
5675 Renaming_Decl
: Node_Id
;
5676 Unit_Renaming
: Node_Id
;
5680 Make_Subprogram_Renaming_Declaration
(Loc
,
5683 (Specification
(Original_Node
(Gen_Decl
)),
5685 Instantiating
=> True),
5686 Name
=> New_Occurrence_Of
(Anon_Id
, Loc
));
5688 -- The generic may be a child unit. The renaming needs an identifier
5689 -- with the proper name.
5691 Set_Defining_Unit_Name
(Specification
(Unit_Renaming
),
5692 Make_Defining_Identifier
(Loc
, Chars
(Gen_Unit
)));
5694 -- If there is a formal subprogram with the same name as the unit
5695 -- itself, do not add this renaming declaration, to prevent
5696 -- ambiguities when there is a call with that name in the body.
5698 Renaming_Decl
:= First
(Renaming_List
);
5699 while Present
(Renaming_Decl
) loop
5700 if Nkind
(Renaming_Decl
) = N_Subprogram_Renaming_Declaration
5702 Chars
(Defining_Entity
(Renaming_Decl
)) = Chars
(Gen_Unit
)
5707 Next
(Renaming_Decl
);
5710 if No
(Renaming_Decl
) then
5711 Append
(Unit_Renaming
, Renaming_List
);
5713 end Build_Subprogram_Renaming
;
5717 Saved_GM
: constant Ghost_Mode_Type
:= Ghost_Mode
;
5718 Saved_IGR
: constant Node_Id
:= Ignored_Ghost_Region
;
5719 Saved_ISMP
: constant Boolean :=
5720 Ignore_SPARK_Mode_Pragmas_In_Instance
;
5721 Saved_SM
: constant SPARK_Mode_Type
:= SPARK_Mode
;
5722 Saved_SMP
: constant Node_Id
:= SPARK_Mode_Pragma
;
5723 -- Save the Ghost and SPARK mode-related data to restore on exit
5725 Vis_Prims_List
: Elist_Id
:= No_Elist
;
5726 -- List of primitives made temporarily visible in the instantiation
5727 -- to match the visibility of the formal type
5729 -- Start of processing for Analyze_Subprogram_Instantiation
5732 -- Preserve relevant elaboration-related attributes of the context which
5733 -- are no longer available or very expensive to recompute once analysis,
5734 -- resolution, and expansion are over.
5736 Mark_Elaboration_Attributes
5743 -- Very first thing: check for special Text_IO unit in case we are
5744 -- instantiating one of the children of [[Wide_]Wide_]Text_IO. Of course
5745 -- such an instantiation is bogus (these are packages, not subprograms),
5746 -- but we get a better error message if we do this.
5748 Check_Text_IO_Special_Unit
(Gen_Id
);
5750 -- Make node global for error reporting
5752 Instantiation_Node
:= N
;
5754 -- For package instantiations we turn off style checks, because they
5755 -- will have been emitted in the generic. For subprogram instantiations
5756 -- we want to apply at least the check on overriding indicators so we
5757 -- do not modify the style check status.
5759 -- The renaming declarations for the actuals do not come from source and
5760 -- will not generate spurious warnings.
5762 Preanalyze_Actuals
(N
);
5765 Env_Installed
:= True;
5766 Check_Generic_Child_Unit
(Gen_Id
, Parent_Installed
);
5767 Gen_Unit
:= Entity
(Gen_Id
);
5769 -- A subprogram instantiation is Ghost when it is subject to pragma
5770 -- Ghost or the generic template is Ghost. Set the mode now to ensure
5771 -- that any nodes generated during analysis and expansion are marked as
5774 Mark_And_Set_Ghost_Instantiation
(N
, Gen_Unit
);
5776 Generate_Reference
(Gen_Unit
, Gen_Id
);
5778 if Nkind
(Gen_Id
) = N_Identifier
5779 and then Chars
(Gen_Unit
) = Chars
(Defining_Entity
(N
))
5782 ("& is hidden within declaration of instance", Gen_Id
, Gen_Unit
);
5785 if Etype
(Gen_Unit
) = Any_Type
then
5790 -- Verify that it is a generic subprogram of the right kind, and that
5791 -- it does not lead to a circular instantiation.
5793 if K
= E_Procedure
and then Ekind
(Gen_Unit
) /= E_Generic_Procedure
then
5795 ("& is not the name of a generic procedure", Gen_Id
, Gen_Unit
);
5797 elsif K
= E_Function
and then Ekind
(Gen_Unit
) /= E_Generic_Function
then
5799 ("& is not the name of a generic function", Gen_Id
, Gen_Unit
);
5801 elsif In_Open_Scopes
(Gen_Unit
) then
5802 Error_Msg_NE
("instantiation of & within itself", N
, Gen_Unit
);
5805 Mutate_Ekind
(Inst_Id
, K
);
5806 Set_Scope
(Inst_Id
, Current_Scope
);
5808 Set_Entity
(Gen_Id
, Gen_Unit
);
5810 if In_Extended_Main_Source_Unit
(N
) then
5811 Set_Is_Instantiated
(Gen_Unit
);
5812 Generate_Reference
(Gen_Unit
, N
);
5815 -- If renaming, get original unit
5817 if Present
(Renamed_Entity
(Gen_Unit
))
5818 and then Is_Generic_Subprogram
(Renamed_Entity
(Gen_Unit
))
5820 Gen_Unit
:= Renamed_Entity
(Gen_Unit
);
5821 Set_Is_Instantiated
(Gen_Unit
);
5822 Generate_Reference
(Gen_Unit
, N
);
5825 if Contains_Instance_Of
(Gen_Unit
, Current_Scope
, Gen_Id
) then
5826 Error_Msg_Node_2
:= Current_Scope
;
5828 ("circular instantiation: & instantiated in &!", N
, Gen_Unit
);
5829 Circularity_Detected
:= True;
5830 Restore_Hidden_Primitives
(Vis_Prims_List
);
5834 Gen_Decl
:= Unit_Declaration_Node
(Gen_Unit
);
5836 -- Initialize renamings map, for error checking
5838 Generic_Renamings
.Set_Last
(0);
5839 Generic_Renamings_HTable
.Reset
;
5841 Create_Instantiation_Source
(N
, Gen_Unit
, S_Adjustment
);
5843 -- Copy original generic tree, to produce text for instantiation
5847 (Original_Node
(Gen_Decl
), Empty
, Instantiating
=> True);
5849 -- Inherit overriding indicator from instance node
5851 Act_Spec
:= Specification
(Act_Tree
);
5852 Set_Must_Override
(Act_Spec
, Must_Override
(N
));
5853 Set_Must_Not_Override
(Act_Spec
, Must_Not_Override
(N
));
5856 Analyze_Associations
5858 Formals
=> Generic_Formal_Declarations
(Act_Tree
),
5859 F_Copy
=> Generic_Formal_Declarations
(Gen_Decl
));
5861 Vis_Prims_List
:= Check_Hidden_Primitives
(Renaming_List
);
5863 -- The subprogram itself cannot contain a nested instance, so the
5864 -- current parent is left empty.
5866 Set_Instance_Env
(Gen_Unit
, Empty
);
5868 -- Build the subprogram declaration, which does not appear in the
5869 -- generic template, and give it a sloc consistent with that of the
5872 Set_Defining_Unit_Name
(Act_Spec
, Anon_Id
);
5873 Set_Generic_Parent
(Act_Spec
, Gen_Unit
);
5875 Make_Subprogram_Declaration
(Sloc
(Act_Spec
),
5876 Specification
=> Act_Spec
);
5878 -- The aspects have been copied previously, but they have to be
5879 -- linked explicitly to the new subprogram declaration. Explicit
5880 -- pre/postconditions on the instance are analyzed below, in a
5883 Move_Aspects
(Act_Tree
, To
=> Act_Decl
);
5884 Set_Categorization_From_Pragmas
(Act_Decl
);
5886 if Parent_Installed
then
5890 Append
(Act_Decl
, Renaming_List
);
5892 -- Contract-related source pragmas that follow a generic subprogram
5893 -- must be instantiated explicitly because they are not part of the
5894 -- subprogram template.
5896 Instantiate_Subprogram_Contract
5897 (Original_Node
(Gen_Decl
), Renaming_List
);
5899 Build_Subprogram_Renaming
;
5901 -- If the context of the instance is subject to SPARK_Mode "off" or
5902 -- the annotation is altogether missing, set the global flag which
5903 -- signals Analyze_Pragma to ignore all SPARK_Mode pragmas within
5904 -- the instance. This should be done prior to analyzing the instance.
5906 if SPARK_Mode
/= On
then
5907 Ignore_SPARK_Mode_Pragmas_In_Instance
:= True;
5910 -- If the context of an instance is not subject to SPARK_Mode "off",
5911 -- and the generic spec is subject to an explicit SPARK_Mode pragma,
5912 -- the latter should be the one applicable to the instance.
5914 if not Ignore_SPARK_Mode_Pragmas_In_Instance
5915 and then Saved_SM
/= Off
5916 and then Present
(SPARK_Pragma
(Gen_Unit
))
5918 Set_SPARK_Mode
(Gen_Unit
);
5921 -- Need to mark Anon_Id intrinsic before calling
5922 -- Analyze_Instance_And_Renamings because this flag may be propagated
5925 if Is_Intrinsic_Subprogram
(Gen_Unit
) then
5926 Set_Is_Intrinsic_Subprogram
(Anon_Id
);
5927 Set_Interface_Name
(Anon_Id
, Interface_Name
(Gen_Unit
));
5930 Analyze_Instance_And_Renamings
;
5932 -- Restore SPARK_Mode from the context after analysis of the package
5933 -- declaration, so that the SPARK_Mode on the generic spec does not
5934 -- apply to the pending instance for the instance body.
5936 if not Ignore_SPARK_Mode_Pragmas_In_Instance
5937 and then Saved_SM
/= Off
5938 and then Present
(SPARK_Pragma
(Gen_Unit
))
5940 Restore_SPARK_Mode
(Saved_SM
, Saved_SMP
);
5943 -- If the generic is marked Import (Intrinsic), then so is the
5944 -- instance; this indicates that there is no body to instantiate.
5945 -- We also copy the interface name in case this is handled by the
5946 -- back-end and deal with an instance of unchecked conversion.
5948 if Is_Intrinsic_Subprogram
(Gen_Unit
) then
5949 Set_Is_Intrinsic_Subprogram
(Act_Decl_Id
);
5950 Set_Interface_Name
(Act_Decl_Id
, Interface_Name
(Gen_Unit
));
5952 if Chars
(Gen_Unit
) = Name_Unchecked_Conversion
then
5953 Validate_Unchecked_Conversion
(N
, Act_Decl_Id
);
5957 -- Inherit convention from generic unit. Intrinsic convention, as for
5958 -- an instance of unchecked conversion, is not inherited because an
5959 -- explicit Ada instance has been created.
5961 if Has_Convention_Pragma
(Gen_Unit
)
5962 and then Convention
(Gen_Unit
) /= Convention_Intrinsic
5964 Set_Convention
(Act_Decl_Id
, Convention
(Gen_Unit
));
5965 Set_Is_Exported
(Act_Decl_Id
, Is_Exported
(Gen_Unit
));
5968 Generate_Definition
(Act_Decl_Id
);
5970 -- Inherit all inlining-related flags which apply to the generic in
5971 -- the subprogram and its declaration.
5973 Set_Is_Inlined
(Act_Decl_Id
, Is_Inlined
(Gen_Unit
));
5974 Set_Is_Inlined
(Anon_Id
, Is_Inlined
(Gen_Unit
));
5976 Set_Has_Pragma_Inline
(Act_Decl_Id
, Has_Pragma_Inline
(Gen_Unit
));
5977 Set_Has_Pragma_Inline
(Anon_Id
, Has_Pragma_Inline
(Gen_Unit
));
5979 Set_Has_Pragma_Inline_Always
5980 (Act_Decl_Id
, Has_Pragma_Inline_Always
(Gen_Unit
));
5981 Set_Has_Pragma_Inline_Always
5982 (Anon_Id
, Has_Pragma_Inline_Always
(Gen_Unit
));
5984 Set_Has_Pragma_No_Inline
5985 (Act_Decl_Id
, Has_Pragma_No_Inline
(Gen_Unit
));
5986 Set_Has_Pragma_No_Inline
5987 (Anon_Id
, Has_Pragma_No_Inline
(Gen_Unit
));
5989 -- Propagate No_Return if pragma applied to generic unit. This must
5990 -- be done explicitly because pragma does not appear in generic
5991 -- declaration (unlike the aspect case).
5993 if No_Return
(Gen_Unit
) then
5994 Set_No_Return
(Act_Decl_Id
);
5995 Set_No_Return
(Anon_Id
);
5998 -- Mark both the instance spec and the anonymous package in case the
5999 -- body is instantiated at a later pass. This preserves the original
6000 -- context in effect for the body.
6002 if SPARK_Mode
/= On
then
6003 Set_Ignore_SPARK_Mode_Pragmas
(Act_Decl_Id
);
6004 Set_Ignore_SPARK_Mode_Pragmas
(Anon_Id
);
6007 if Legacy_Elaboration_Checks
6008 and then not Is_Intrinsic_Subprogram
(Gen_Unit
)
6010 Check_Elab_Instantiation
(N
);
6013 -- Save the scenario for later examination by the ABE Processing
6016 Record_Elaboration_Scenario
(N
);
6018 -- The instantiation results in a guaranteed ABE. Create a completing
6019 -- body for the subprogram declaration because the real body will not
6022 if Is_Known_Guaranteed_ABE
(N
) then
6023 Provide_Completing_Bodies
(Instance_Spec
(N
));
6026 if Is_Dispatching_Operation
(Act_Decl_Id
)
6027 and then Ada_Version
>= Ada_2005
6033 Formal
:= First_Formal
(Act_Decl_Id
);
6034 while Present
(Formal
) loop
6035 if Ekind
(Etype
(Formal
)) = E_Anonymous_Access_Type
6036 and then Is_Controlling_Formal
(Formal
)
6037 and then not Can_Never_Be_Null
(Formal
)
6040 ("access parameter& is controlling,", N
, Formal
);
6042 ("\corresponding parameter of & must be explicitly "
6043 & "null-excluding", N
, Gen_Id
);
6046 Next_Formal
(Formal
);
6051 Check_Hidden_Child_Unit
(N
, Gen_Unit
, Act_Decl_Id
);
6053 Validate_Categorization_Dependency
(N
, Act_Decl_Id
);
6055 if not Is_Intrinsic_Subprogram
(Act_Decl_Id
) then
6056 Inherit_Context
(Gen_Decl
, N
);
6058 Restore_Private_Views
(Pack_Id
, False);
6060 -- If the context requires a full instantiation, mark node for
6061 -- subsequent construction of the body.
6063 if Need_Subprogram_Instance_Body
(N
, Act_Decl_Id
) then
6064 Check_Forward_Instantiation
(Gen_Decl
);
6066 -- The wrapper package is always delayed, because it does not
6067 -- constitute a freeze point, but to insure that the freeze node
6068 -- is placed properly, it is created directly when instantiating
6069 -- the body (otherwise the freeze node might appear to early for
6070 -- nested instantiations).
6072 elsif Nkind
(Parent
(N
)) = N_Compilation_Unit
then
6073 Rewrite
(N
, Unit
(Parent
(N
)));
6074 Set_Unit
(Parent
(N
), N
);
6077 -- Replace instance node for library-level instantiations of
6078 -- intrinsic subprograms.
6080 elsif Nkind
(Parent
(N
)) = N_Compilation_Unit
then
6081 Rewrite
(N
, Unit
(Parent
(N
)));
6082 Set_Unit
(Parent
(N
), N
);
6085 if Parent_Installed
then
6089 Restore_Hidden_Primitives
(Vis_Prims_List
);
6091 Env_Installed
:= False;
6092 Generic_Renamings
.Set_Last
(0);
6093 Generic_Renamings_HTable
.Reset
;
6097 -- Analyze aspects in declaration if no errors appear in the instance.
6099 if Has_Aspects
(N
) and then Serious_Errors_Detected
= Errs
then
6100 Analyze_Aspect_Specifications
(N
, Act_Decl_Id
);
6103 Ignore_SPARK_Mode_Pragmas_In_Instance
:= Saved_ISMP
;
6104 Restore_Ghost_Region
(Saved_GM
, Saved_IGR
);
6105 Restore_SPARK_Mode
(Saved_SM
, Saved_SMP
);
6108 when Instantiation_Error
=>
6109 if Parent_Installed
then
6113 if Env_Installed
then
6117 Ignore_SPARK_Mode_Pragmas_In_Instance
:= Saved_ISMP
;
6118 Restore_Ghost_Region
(Saved_GM
, Saved_IGR
);
6119 Restore_SPARK_Mode
(Saved_SM
, Saved_SMP
);
6120 end Analyze_Subprogram_Instantiation
;
6122 -------------------------
6123 -- Get_Associated_Node --
6124 -------------------------
6126 function Get_Associated_Node
(N
: Node_Id
) return Node_Id
is
6130 Assoc
:= Associated_Node
(N
);
6132 if Nkind
(Assoc
) /= Nkind
(N
) then
6135 elsif Nkind
(Assoc
) in N_Aggregate | N_Extension_Aggregate
then
6139 -- If the node is part of an inner generic, it may itself have been
6140 -- remapped into a further generic copy. Associated_Node is otherwise
6141 -- used for the entity of the node, and will be of a different node
6142 -- kind, or else N has been rewritten as a literal or function call.
6144 while Present
(Associated_Node
(Assoc
))
6145 and then Nkind
(Associated_Node
(Assoc
)) = Nkind
(Assoc
)
6147 Assoc
:= Associated_Node
(Assoc
);
6150 -- Follow an additional link in case the final node was rewritten.
6151 -- This can only happen with nested generic units.
6153 if (Nkind
(Assoc
) = N_Identifier
or else Nkind
(Assoc
) in N_Op
)
6154 and then Present
(Associated_Node
(Assoc
))
6155 and then Nkind
(Associated_Node
(Assoc
)) in N_Function_Call
6156 | N_Explicit_Dereference
6161 Assoc
:= Associated_Node
(Assoc
);
6164 -- An additional special case: an unconstrained type in an object
6165 -- declaration may have been rewritten as a local subtype constrained
6166 -- by the expression in the declaration. We need to recover the
6167 -- original entity, which may be global.
6169 if Present
(Original_Node
(Assoc
))
6170 and then Nkind
(Parent
(N
)) = N_Object_Declaration
6172 Assoc
:= Original_Node
(Assoc
);
6177 end Get_Associated_Node
;
6179 -----------------------------------
6180 -- Build_Subprogram_Decl_Wrapper --
6181 -----------------------------------
6183 function Build_Subprogram_Decl_Wrapper
6184 (Formal_Subp
: Entity_Id
) return Node_Id
6186 Loc
: constant Source_Ptr
:= Sloc
(Current_Scope
);
6187 Ret_Type
: constant Entity_Id
:= Get_Instance_Of
(Etype
(Formal_Subp
));
6190 Parm_Spec
: Node_Id
;
6191 Profile
: List_Id
:= New_List
;
6198 Subp
:= Make_Defining_Identifier
(Loc
, Chars
(Formal_Subp
));
6199 Mutate_Ekind
(Subp
, Ekind
(Formal_Subp
));
6200 Set_Is_Generic_Actual_Subprogram
(Subp
);
6202 Profile
:= Parameter_Specifications
(
6204 (Specification
(Unit_Declaration_Node
(Formal_Subp
))));
6206 Form_F
:= First_Formal
(Formal_Subp
);
6207 Parm_Spec
:= First
(Profile
);
6209 -- Create new entities for the formals. Reset entities so that
6210 -- parameter types are properly resolved when wrapper declaration
6213 while Present
(Parm_Spec
) loop
6214 New_F
:= Make_Defining_Identifier
(Loc
, Chars
(Form_F
));
6215 Set_Defining_Identifier
(Parm_Spec
, New_F
);
6216 Set_Entity
(Parameter_Type
(Parm_Spec
), Empty
);
6218 Next_Formal
(Form_F
);
6221 if Ret_Type
= Standard_Void_Type
then
6223 Make_Procedure_Specification
(Loc
,
6224 Defining_Unit_Name
=> Subp
,
6225 Parameter_Specifications
=> Profile
);
6228 Make_Function_Specification
(Loc
,
6229 Defining_Unit_Name
=> Subp
,
6230 Parameter_Specifications
=> Profile
,
6231 Result_Definition
=> New_Occurrence_Of
(Ret_Type
, Loc
));
6235 Make_Subprogram_Declaration
(Loc
, Specification
=> Spec
);
6238 end Build_Subprogram_Decl_Wrapper
;
6240 -----------------------------------
6241 -- Build_Subprogram_Body_Wrapper --
6242 -----------------------------------
6244 function Build_Subprogram_Body_Wrapper
6245 (Formal_Subp
: Entity_Id
;
6246 Actual_Name
: Node_Id
) return Node_Id
6248 Loc
: constant Source_Ptr
:= Sloc
(Current_Scope
);
6249 Ret_Type
: constant Entity_Id
:= Get_Instance_Of
(Etype
(Formal_Subp
));
6250 Spec_Node
: constant Node_Id
:=
6252 (Build_Subprogram_Decl_Wrapper
(Formal_Subp
));
6255 Body_Node
: Node_Id
;
6258 Actuals
:= New_List
;
6259 Act
:= First
(Parameter_Specifications
(Spec_Node
));
6261 while Present
(Act
) loop
6263 Make_Identifier
(Loc
, Chars
(Defining_Identifier
(Act
))));
6267 if Ret_Type
= Standard_Void_Type
then
6268 Stmt
:= Make_Procedure_Call_Statement
(Loc
,
6269 Name
=> Actual_Name
,
6270 Parameter_Associations
=> Actuals
);
6273 Stmt
:= Make_Simple_Return_Statement
(Loc
,
6275 Make_Function_Call
(Loc
,
6276 Name
=> Actual_Name
,
6277 Parameter_Associations
=> Actuals
));
6280 Body_Node
:= Make_Subprogram_Body
(Loc
,
6281 Specification
=> Spec_Node
,
6282 Declarations
=> New_List
,
6283 Handled_Statement_Sequence
=>
6284 Make_Handled_Sequence_Of_Statements
(Loc
,
6285 Statements
=> New_List
(Stmt
)));
6288 end Build_Subprogram_Body_Wrapper
;
6290 -------------------------------------------
6291 -- Build_Instance_Compilation_Unit_Nodes --
6292 -------------------------------------------
6294 procedure Build_Instance_Compilation_Unit_Nodes
6299 Decl_Cunit
: Node_Id
;
6300 Body_Cunit
: Node_Id
;
6302 New_Main
: constant Entity_Id
:= Defining_Entity
(Act_Decl
);
6303 Old_Main
: constant Entity_Id
:= Cunit_Entity
(Main_Unit
);
6306 -- A new compilation unit node is built for the instance declaration.
6307 -- It relocates the auxiliary declaration node from the compilation unit
6308 -- where the instance appeared, so that declarations that originally
6309 -- followed the instance will be attached to the spec compilation unit.
6312 Make_Compilation_Unit
(Sloc
(N
),
6313 Context_Items
=> Empty_List
,
6315 Aux_Decls_Node
=> Relocate_Node
(Aux_Decls_Node
(Parent
(N
))));
6317 Set_Parent_Spec
(Act_Decl
, Parent_Spec
(N
));
6319 -- The new compilation unit is linked to its body, but both share the
6320 -- same file, so we do not set Body_Required on the new unit so as not
6321 -- to create a spurious dependency on a non-existent body in the ali.
6322 -- This simplifies CodePeer unit traversal.
6324 -- We use the original instantiation compilation unit as the resulting
6325 -- compilation unit of the instance, since this is the main unit.
6327 Rewrite
(N
, Act_Body
);
6329 -- Propagate the aspect specifications from the package body template to
6330 -- the instantiated version of the package body.
6332 if Has_Aspects
(Act_Body
) then
6333 Set_Aspect_Specifications
6334 (N
, New_Copy_List_Tree
(Aspect_Specifications
(Act_Body
)));
6337 Body_Cunit
:= Parent
(N
);
6339 -- The two compilation unit nodes are linked by the Library_Unit field
6341 Set_Library_Unit
(Decl_Cunit
, Body_Cunit
);
6342 Set_Library_Unit
(Body_Cunit
, Decl_Cunit
);
6344 -- Preserve the private nature of the package if needed
6346 Set_Private_Present
(Decl_Cunit
, Private_Present
(Body_Cunit
));
6348 -- If the instance is not the main unit, its context, categorization
6349 -- and elaboration entity are not relevant to the compilation.
6351 if Body_Cunit
/= Cunit
(Main_Unit
) then
6352 Make_Instance_Unit
(Body_Cunit
, In_Main
=> False);
6356 -- The context clause items on the instantiation, which are now attached
6357 -- to the body compilation unit (since the body overwrote the original
6358 -- instantiation node), semantically belong on the spec, so copy them
6359 -- there. It's harmless to leave them on the body as well. In fact one
6360 -- could argue that they belong in both places.
6362 Citem
:= First
(Context_Items
(Body_Cunit
));
6363 while Present
(Citem
) loop
6364 Append
(New_Copy
(Citem
), Context_Items
(Decl_Cunit
));
6368 -- Propagate categorization flags on packages, so that they appear in
6369 -- the ali file for the spec of the unit.
6371 if Ekind
(New_Main
) = E_Package
then
6372 Set_Is_Pure
(Old_Main
, Is_Pure
(New_Main
));
6373 Set_Is_Preelaborated
(Old_Main
, Is_Preelaborated
(New_Main
));
6374 Set_Is_Remote_Types
(Old_Main
, Is_Remote_Types
(New_Main
));
6375 Set_Is_Shared_Passive
(Old_Main
, Is_Shared_Passive
(New_Main
));
6376 Set_Is_Remote_Call_Interface
6377 (Old_Main
, Is_Remote_Call_Interface
(New_Main
));
6380 -- Make entry in Units table, so that binder can generate call to
6381 -- elaboration procedure for body, if any.
6383 Make_Instance_Unit
(Body_Cunit
, In_Main
=> True);
6384 Main_Unit_Entity
:= New_Main
;
6385 Set_Cunit_Entity
(Main_Unit
, Main_Unit_Entity
);
6387 -- Build elaboration entity, since the instance may certainly generate
6388 -- elaboration code requiring a flag for protection.
6390 Build_Elaboration_Entity
(Decl_Cunit
, New_Main
);
6391 end Build_Instance_Compilation_Unit_Nodes
;
6393 --------------------------------
6394 -- Check_Abbreviated_Instance --
6395 --------------------------------
6397 procedure Check_Abbreviated_Instance
6399 Parent_Installed
: in out Boolean)
6401 Inst_Node
: Node_Id
;
6404 if Nkind
(N
) = N_Package_Specification
6405 and then Is_Abbreviated_Instance
(Defining_Entity
(N
))
6407 Inst_Node
:= Get_Unit_Instantiation_Node
(Defining_Entity
(N
));
6408 Check_Generic_Child_Unit
(Name
(Inst_Node
), Parent_Installed
);
6410 end Check_Abbreviated_Instance
;
6412 -----------------------------
6413 -- Check_Access_Definition --
6414 -----------------------------
6416 procedure Check_Access_Definition
(N
: Node_Id
) is
6419 (Ada_Version
>= Ada_2005
and then Present
(Access_Definition
(N
)));
6421 end Check_Access_Definition
;
6423 -----------------------------------
6424 -- Check_Formal_Package_Instance --
6425 -----------------------------------
6427 -- If the formal has specific parameters, they must match those of the
6428 -- actual. Both of them are instances, and the renaming declarations for
6429 -- their formal parameters appear in the same order in both. The analyzed
6430 -- formal has been analyzed in the context of the current instance.
6432 procedure Check_Formal_Package_Instance
6433 (Formal_Pack
: Entity_Id
;
6434 Actual_Pack
: Entity_Id
)
6436 E1
: Entity_Id
:= First_Entity
(Actual_Pack
);
6437 E2
: Entity_Id
:= First_Entity
(Formal_Pack
);
6438 Prev_E1
: Entity_Id
;
6443 procedure Check_Mismatch
(B
: Boolean);
6444 -- Common error routine for mismatch between the parameters of the
6445 -- actual instance and those of the formal package.
6447 function Is_Defaulted
(Param
: Entity_Id
) return Boolean;
6448 -- If the formal package has partly box-initialized formals, skip
6449 -- conformance check for these formals. Previously the code assumed
6450 -- that box initialization for a formal package applied to all its
6451 -- formal parameters.
6453 function Same_Instantiated_Constant
(E1
, E2
: Entity_Id
) return Boolean;
6454 -- The formal may come from a nested formal package, and the actual may
6455 -- have been constant-folded. To determine whether the two denote the
6456 -- same entity we may have to traverse several definitions to recover
6457 -- the ultimate entity that they refer to.
6459 function Same_Instantiated_Function
(E1
, E2
: Entity_Id
) return Boolean;
6460 -- The formal and the actual must be identical, but if both are
6461 -- given by attributes they end up renaming different generated bodies,
6462 -- and we must verify that the attributes themselves match.
6464 function Same_Instantiated_Variable
(E1
, E2
: Entity_Id
) return Boolean;
6465 -- Similarly, if the formal comes from a nested formal package, the
6466 -- actual may designate the formal through multiple renamings, which
6467 -- have to be followed to determine the original variable in question.
6469 --------------------
6470 -- Check_Mismatch --
6471 --------------------
6473 procedure Check_Mismatch
(B
: Boolean) is
6474 -- A Formal_Type_Declaration for a derived private type is rewritten
6475 -- as a private extension decl. (see Analyze_Formal_Derived_Type),
6476 -- which is why we examine the original node.
6478 Kind
: constant Node_Kind
:= Nkind
(Original_Node
(Parent
(E2
)));
6481 if Kind
= N_Formal_Type_Declaration
then
6484 elsif Kind
in N_Formal_Object_Declaration
6485 | N_Formal_Package_Declaration
6486 | N_Formal_Subprogram_Declaration
6490 -- Ada 2012: If both formal and actual are incomplete types they
6493 elsif Is_Incomplete_Type
(E1
) and then Is_Incomplete_Type
(E2
) then
6498 ("actual for & in actual instance does not match formal",
6499 Parent
(Actual_Pack
), E1
);
6507 function Is_Defaulted
(Param
: Entity_Id
) return Boolean is
6512 First
(Generic_Associations
(Parent
6513 (Associated_Formal_Package
(Actual_Pack
))));
6515 while Present
(Assoc
) loop
6516 if Nkind
(Assoc
) = N_Others_Choice
then
6519 elsif Nkind
(Assoc
) = N_Generic_Association
6520 and then Chars
(Selector_Name
(Assoc
)) = Chars
(Param
)
6522 return Box_Present
(Assoc
);
6531 --------------------------------
6532 -- Same_Instantiated_Constant --
6533 --------------------------------
6535 function Same_Instantiated_Constant
6536 (E1
, E2
: Entity_Id
) return Boolean
6542 while Present
(Ent
) loop
6546 elsif Ekind
(Ent
) /= E_Constant
then
6549 elsif Is_Entity_Name
(Constant_Value
(Ent
)) then
6550 if Entity
(Constant_Value
(Ent
)) = E1
then
6553 Ent
:= Entity
(Constant_Value
(Ent
));
6556 -- The actual may be a constant that has been folded. Recover
6559 elsif Is_Entity_Name
(Original_Node
(Constant_Value
(Ent
))) then
6560 Ent
:= Entity
(Original_Node
(Constant_Value
(Ent
)));
6568 end Same_Instantiated_Constant
;
6570 --------------------------------
6571 -- Same_Instantiated_Function --
6572 --------------------------------
6574 function Same_Instantiated_Function
6575 (E1
, E2
: Entity_Id
) return Boolean
6579 if Alias
(E1
) = Alias
(E2
) then
6582 elsif Present
(Alias
(E2
)) then
6583 U1
:= Original_Node
(Unit_Declaration_Node
(E1
));
6584 U2
:= Original_Node
(Unit_Declaration_Node
(Alias
(E2
)));
6586 return Nkind
(U1
) = N_Subprogram_Renaming_Declaration
6587 and then Nkind
(Name
(U1
)) = N_Attribute_Reference
6589 and then Nkind
(U2
) = N_Subprogram_Renaming_Declaration
6590 and then Nkind
(Name
(U2
)) = N_Attribute_Reference
6593 Attribute_Name
(Name
(U1
)) = Attribute_Name
(Name
(U2
));
6597 end Same_Instantiated_Function
;
6599 --------------------------------
6600 -- Same_Instantiated_Variable --
6601 --------------------------------
6603 function Same_Instantiated_Variable
6604 (E1
, E2
: Entity_Id
) return Boolean
6606 function Original_Entity
(E
: Entity_Id
) return Entity_Id
;
6607 -- Follow chain of renamings to the ultimate ancestor
6609 ---------------------
6610 -- Original_Entity --
6611 ---------------------
6613 function Original_Entity
(E
: Entity_Id
) return Entity_Id
is
6618 while Nkind
(Parent
(Orig
)) = N_Object_Renaming_Declaration
6619 and then Present
(Renamed_Object
(Orig
))
6620 and then Is_Entity_Name
(Renamed_Object
(Orig
))
6622 Orig
:= Entity
(Renamed_Object
(Orig
));
6626 end Original_Entity
;
6628 -- Start of processing for Same_Instantiated_Variable
6631 return Ekind
(E1
) = Ekind
(E2
)
6632 and then Original_Entity
(E1
) = Original_Entity
(E2
);
6633 end Same_Instantiated_Variable
;
6635 -- Start of processing for Check_Formal_Package_Instance
6639 while Present
(E1
) and then Present
(E2
) loop
6640 exit when Ekind
(E1
) = E_Package
6641 and then Renamed_Entity
(E1
) = Renamed_Entity
(Actual_Pack
);
6643 -- If the formal is the renaming of the formal package, this
6644 -- is the end of its formal part, which may occur before the
6645 -- end of the formal part in the actual in the presence of
6646 -- defaulted parameters in the formal package.
6648 exit when Nkind
(Parent
(E2
)) = N_Package_Renaming_Declaration
6649 and then Renamed_Entity
(E2
) = Scope
(E2
);
6651 -- The analysis of the actual may generate additional internal
6652 -- entities. If the formal is defaulted, there is no corresponding
6653 -- analysis and the internal entities must be skipped, until we
6654 -- find corresponding entities again.
6656 if Comes_From_Source
(E2
)
6657 and then not Comes_From_Source
(E1
)
6658 and then Chars
(E1
) /= Chars
(E2
)
6660 while Present
(E1
) and then Chars
(E1
) /= Chars
(E2
) loop
6668 -- Entities may be declared without full declaration, such as
6669 -- itypes and predefined operators (concatenation for arrays, eg).
6670 -- Skip it and keep the formal entity to find a later match for it.
6672 elsif No
(Parent
(E2
)) and then Ekind
(E1
) /= Ekind
(E2
) then
6676 -- If the formal entity comes from a formal declaration, it was
6677 -- defaulted in the formal package, and no check is needed on it.
6679 elsif Nkind
(Original_Node
(Parent
(E2
))) in
6680 N_Formal_Object_Declaration | N_Formal_Type_Declaration
6682 -- If the formal is a tagged type the corresponding class-wide
6683 -- type has been generated as well, and it must be skipped.
6685 if Is_Type
(E2
) and then Is_Tagged_Type
(E2
) then
6691 -- Ditto for defaulted formal subprograms.
6693 elsif Is_Overloadable
(E1
)
6694 and then Nkind
(Unit_Declaration_Node
(E2
)) in
6695 N_Formal_Subprogram_Declaration
6699 elsif Is_Defaulted
(E1
) then
6702 elsif Is_Type
(E1
) then
6704 -- Subtypes must statically match. E1, E2 are the local entities
6705 -- that are subtypes of the actuals. Itypes generated for other
6706 -- parameters need not be checked, the check will be performed
6707 -- on the parameters themselves.
6709 -- If E2 is a formal type declaration, it is a defaulted parameter
6710 -- and needs no checking.
6712 if not Is_Itype
(E1
) and then not Is_Itype
(E2
) then
6715 or else Etype
(E1
) /= Etype
(E2
)
6716 or else not Subtypes_Statically_Match
(E1
, E2
));
6719 elsif Ekind
(E1
) = E_Constant
then
6721 -- IN parameters must denote the same static value, or the same
6722 -- constant, or the literal null.
6724 Expr1
:= Expression
(Parent
(E1
));
6726 if Ekind
(E2
) /= E_Constant
then
6727 Check_Mismatch
(True);
6730 Expr2
:= Expression
(Parent
(E2
));
6733 if Is_OK_Static_Expression
(Expr1
) then
6734 if not Is_OK_Static_Expression
(Expr2
) then
6735 Check_Mismatch
(True);
6737 elsif Is_Discrete_Type
(Etype
(E1
)) then
6739 V1
: constant Uint
:= Expr_Value
(Expr1
);
6740 V2
: constant Uint
:= Expr_Value
(Expr2
);
6742 Check_Mismatch
(V1
/= V2
);
6745 elsif Is_Real_Type
(Etype
(E1
)) then
6747 V1
: constant Ureal
:= Expr_Value_R
(Expr1
);
6748 V2
: constant Ureal
:= Expr_Value_R
(Expr2
);
6750 Check_Mismatch
(V1
/= V2
);
6753 elsif Is_String_Type
(Etype
(E1
))
6754 and then Nkind
(Expr1
) = N_String_Literal
6756 if Nkind
(Expr2
) /= N_String_Literal
then
6757 Check_Mismatch
(True);
6760 (not String_Equal
(Strval
(Expr1
), Strval
(Expr2
)));
6764 elsif Is_Entity_Name
(Expr1
) then
6765 if Is_Entity_Name
(Expr2
) then
6766 if Entity
(Expr1
) = Entity
(Expr2
) then
6770 (not Same_Instantiated_Constant
6771 (Entity
(Expr1
), Entity
(Expr2
)));
6775 Check_Mismatch
(True);
6778 elsif Is_Entity_Name
(Original_Node
(Expr1
))
6779 and then Is_Entity_Name
(Expr2
)
6780 and then Same_Instantiated_Constant
6781 (Entity
(Original_Node
(Expr1
)), Entity
(Expr2
))
6785 elsif Nkind
(Expr1
) = N_Null
then
6786 Check_Mismatch
(Nkind
(Expr1
) /= N_Null
);
6789 Check_Mismatch
(True);
6792 elsif Ekind
(E1
) = E_Variable
then
6793 Check_Mismatch
(not Same_Instantiated_Variable
(E1
, E2
));
6795 elsif Ekind
(E1
) = E_Package
then
6797 (Ekind
(E1
) /= Ekind
(E2
)
6798 or else (Present
(Renamed_Entity
(E2
))
6799 and then Renamed_Entity
(E1
) /=
6800 Renamed_Entity
(E2
)));
6802 elsif Is_Overloadable
(E1
) then
6803 -- Verify that the actual subprograms match. Note that actuals
6804 -- that are attributes are rewritten as subprograms. If the
6805 -- subprogram in the formal package is defaulted, no check is
6806 -- needed. Note that this can only happen in Ada 2005 when the
6807 -- formal package can be partially parameterized.
6809 if Nkind
(Unit_Declaration_Node
(E1
)) =
6810 N_Subprogram_Renaming_Declaration
6811 and then From_Default
(Unit_Declaration_Node
(E1
))
6815 -- If the formal package has an "others" box association that
6816 -- covers this formal, there is no need for a check either.
6818 elsif Nkind
(Unit_Declaration_Node
(E2
)) in
6819 N_Formal_Subprogram_Declaration
6820 and then Box_Present
(Unit_Declaration_Node
(E2
))
6824 -- No check needed if subprogram is a defaulted null procedure
6826 elsif No
(Alias
(E2
))
6827 and then Ekind
(E2
) = E_Procedure
6829 Null_Present
(Specification
(Unit_Declaration_Node
(E2
)))
6833 -- Otherwise the actual in the formal and the actual in the
6834 -- instantiation of the formal must match, up to renamings.
6838 (Ekind
(E2
) /= Ekind
(E1
)
6839 or else not Same_Instantiated_Function
(E1
, E2
));
6843 raise Program_Error
;
6851 end Check_Formal_Package_Instance
;
6853 ---------------------------
6854 -- Check_Formal_Packages --
6855 ---------------------------
6857 procedure Check_Formal_Packages
(P_Id
: Entity_Id
) is
6859 Formal_P
: Entity_Id
;
6860 Formal_Decl
: Node_Id
;
6863 -- Iterate through the declarations in the instance, looking for package
6864 -- renaming declarations that denote instances of formal packages, until
6865 -- we find the renaming of the current package itself. The declaration
6866 -- of a formal package that requires conformance checking is followed by
6867 -- an internal entity that is the abbreviated instance.
6869 E
:= First_Entity
(P_Id
);
6870 while Present
(E
) loop
6871 if Ekind
(E
) = E_Package
then
6872 exit when Renamed_Entity
(E
) = P_Id
;
6874 if Nkind
(Parent
(E
)) = N_Package_Renaming_Declaration
then
6875 Formal_Decl
:= Parent
(Associated_Formal_Package
(E
));
6877 if Requires_Conformance_Checking
(Formal_Decl
) then
6878 Formal_P
:= Next_Entity
(E
);
6880 -- If the instance is within an enclosing instance body
6881 -- there is no need to verify the legality of current formal
6882 -- packages because they were legal in the generic body.
6883 -- This optimization may be applicable elsewhere, and it
6884 -- also removes spurious errors that may arise with
6885 -- on-the-fly inlining and confusion between private and
6888 if not In_Instance_Body
then
6889 Check_Formal_Package_Instance
(Formal_P
, E
);
6892 -- Restore the visibility of formals of the formal instance
6893 -- that are not defaulted, and are hidden within the current
6894 -- generic. These formals may be visible within an enclosing
6900 Elmt
:= First_Elmt
(Hidden_In_Formal_Instance
(Formal_P
));
6901 while Present
(Elmt
) loop
6902 Set_Is_Hidden
(Node
(Elmt
), False);
6907 -- After checking, remove the internal validating package.
6908 -- It is only needed for semantic checks, and as it may
6909 -- contain generic formal declarations it should not reach
6912 Remove
(Unit_Declaration_Node
(Formal_P
));
6919 end Check_Formal_Packages
;
6921 ---------------------------------
6922 -- Check_Forward_Instantiation --
6923 ---------------------------------
6925 procedure Check_Forward_Instantiation
(Decl
: Node_Id
) is
6927 Gen_Comp
: Entity_Id
:= Cunit_Entity
(Get_Source_Unit
(Decl
));
6930 -- The instantiation appears before the generic body if we are in the
6931 -- scope of the unit containing the generic, either in its spec or in
6932 -- the package body, and before the generic body.
6934 if Ekind
(Gen_Comp
) = E_Package_Body
then
6935 Gen_Comp
:= Spec_Entity
(Gen_Comp
);
6938 if In_Open_Scopes
(Gen_Comp
)
6939 and then No
(Corresponding_Body
(Decl
))
6944 and then not Is_Compilation_Unit
(S
)
6945 and then not Is_Child_Unit
(S
)
6947 if Ekind
(S
) = E_Package
then
6948 Set_Has_Forward_Instantiation
(S
);
6954 end Check_Forward_Instantiation
;
6956 ---------------------------
6957 -- Check_Generic_Actuals --
6958 ---------------------------
6960 -- The visibility of the actuals may be different between the point of
6961 -- generic instantiation and the instantiation of the body.
6963 procedure Check_Generic_Actuals
6964 (Instance
: Entity_Id
;
6965 Is_Formal_Box
: Boolean)
6967 Gen_Id
: constant Entity_Id
6968 := (if Is_Generic_Unit
(Instance
) then
6970 elsif Is_Wrapper_Package
(Instance
) then
6973 (Unit_Declaration_Node
(Related_Instance
(Instance
))))
6975 Generic_Parent
(Package_Specification
(Instance
)));
6978 Parent_Scope
: constant Entity_Id
:= Scope
(Gen_Id
);
6979 -- The enclosing scope of the generic unit
6981 procedure Check_Actual_Type
(Typ
: Entity_Id
);
6982 -- If the type of the actual is a private type declared in the
6983 -- enclosing scope of the generic unit, but not a derived type
6984 -- of a private type declared elsewhere, the body of the generic
6985 -- sees the full view of the type (because it has to appear in
6986 -- the corresponding package body). If the type is private now,
6987 -- exchange views to restore the proper visibility in the instance.
6989 -----------------------
6990 -- Check_Actual_Type --
6991 -----------------------
6993 procedure Check_Actual_Type
(Typ
: Entity_Id
) is
6994 Btyp
: constant Entity_Id
:= Base_Type
(Typ
);
6997 -- The exchange is only needed if the generic is defined
6998 -- within a package which is not a common ancestor of the
6999 -- scope of the instance, and is not already in scope.
7001 if Is_Private_Type
(Btyp
)
7002 and then Scope
(Btyp
) = Parent_Scope
7003 and then not Has_Private_Ancestor
(Btyp
)
7004 and then Ekind
(Parent_Scope
) in E_Package | E_Generic_Package
7005 and then Scope
(Instance
) /= Parent_Scope
7006 and then not Is_Child_Unit
(Gen_Id
)
7010 -- If the type of the entity is a subtype, it may also have
7011 -- to be made visible, together with the base type of its
7012 -- full view, after exchange.
7014 if Is_Private_Type
(Typ
) then
7016 Switch_View
(Base_Type
(Typ
));
7019 end Check_Actual_Type
;
7026 E
:= First_Entity
(Instance
);
7027 while Present
(E
) loop
7029 and then Nkind
(Parent
(E
)) = N_Subtype_Declaration
7030 and then Scope
(Etype
(E
)) /= Instance
7031 and then Is_Entity_Name
(Subtype_Indication
(Parent
(E
)))
7033 -- Restore the proper view of the actual from the information
7034 -- saved earlier by Instantiate_Type.
7036 Check_Private_View
(Subtype_Indication
(Parent
(E
)));
7038 -- If the actual is itself the formal of a parent instance,
7039 -- then also restore the proper view of its actual and so on.
7040 -- That's necessary for nested instantiations of the form
7043 -- type Component is private;
7044 -- type Array_Type is array (Positive range <>) of Component;
7047 -- when the outermost actuals have inconsistent views, because
7048 -- the Component_Type of Array_Type of the inner instantiations
7049 -- is the actual of Component of the outermost one and not that
7050 -- of the corresponding inner instantiations.
7052 Astype
:= Ancestor_Subtype
(E
);
7053 while Present
(Astype
)
7054 and then Nkind
(Parent
(Astype
)) = N_Subtype_Declaration
7055 and then Present
(Generic_Parent_Type
(Parent
(Astype
)))
7056 and then Is_Entity_Name
(Subtype_Indication
(Parent
(Astype
)))
7058 Check_Private_View
(Subtype_Indication
(Parent
(Astype
)));
7059 Astype
:= Ancestor_Subtype
(Astype
);
7062 Set_Is_Generic_Actual_Type
(E
);
7064 if Is_Private_Type
(E
) and then Present
(Full_View
(E
)) then
7065 Set_Is_Generic_Actual_Type
(Full_View
(E
));
7068 Set_Is_Hidden
(E
, False);
7069 Set_Is_Potentially_Use_Visible
(E
, In_Use
(Instance
));
7071 -- We constructed the generic actual type as a subtype of the
7072 -- supplied type. This means that it normally would not inherit
7073 -- subtype specific attributes of the actual, which is wrong for
7074 -- the generic case.
7076 Astype
:= Ancestor_Subtype
(E
);
7080 -- This can happen when E is an itype that is the full view of
7081 -- a private type completed, e.g. with a constrained array. In
7082 -- that case, use the first subtype, which will carry size
7083 -- information. The base type itself is unconstrained and will
7086 Astype
:= First_Subtype
(E
);
7089 Set_Size_Info
(E
, Astype
);
7090 Copy_RM_Size
(To
=> E
, From
=> Astype
);
7091 Set_First_Rep_Item
(E
, First_Rep_Item
(Astype
));
7093 if Is_Discrete_Or_Fixed_Point_Type
(E
) then
7094 Set_RM_Size
(E
, RM_Size
(Astype
));
7097 elsif Ekind
(E
) = E_Package
then
7099 -- If this is the renaming for the current instance, we're done.
7100 -- Otherwise it is a formal package. If the corresponding formal
7101 -- was declared with a box, the (instantiations of the) generic
7102 -- formal part are also visible. Otherwise, ignore the entity
7103 -- created to validate the actuals.
7105 if Renamed_Entity
(E
) = Instance
then
7108 elsif Nkind
(Parent
(E
)) /= N_Package_Renaming_Declaration
then
7111 -- The visibility of a formal of an enclosing generic is already
7114 elsif Denotes_Formal_Package
(E
) then
7117 elsif Present
(Associated_Formal_Package
(E
))
7118 and then not Is_Generic_Formal
(E
)
7120 Check_Generic_Actuals
7121 (Renamed_Entity
(E
),
7123 Box_Present
(Parent
(Associated_Formal_Package
(E
))));
7125 Set_Is_Hidden
(E
, False);
7128 -- If this is a subprogram instance (in a wrapper package) the
7129 -- actual is fully visible.
7131 elsif Is_Wrapper_Package
(Instance
) then
7132 Set_Is_Hidden
(E
, False);
7134 -- If the formal package is declared with a box, or if the formal
7135 -- parameter is defaulted, it is visible in the body.
7137 elsif Is_Formal_Box
or else Is_Visible_Formal
(E
) then
7138 Set_Is_Hidden
(E
, False);
7141 -- Check directly the type of the actual objects
7143 if Ekind
(E
) in E_Constant | E_Variable
then
7144 Check_Actual_Type
(Etype
(E
));
7146 -- As well as the type of formal parameters of actual subprograms
7148 elsif Ekind
(E
) in E_Function | E_Procedure
7149 and then Is_Generic_Actual_Subprogram
(E
)
7150 and then Present
(Alias
(E
))
7152 Formal
:= First_Formal
(Alias
(E
));
7153 while Present
(Formal
) loop
7154 Check_Actual_Type
(Etype
(Formal
));
7155 Next_Formal
(Formal
);
7161 end Check_Generic_Actuals
;
7163 ------------------------------
7164 -- Check_Generic_Child_Unit --
7165 ------------------------------
7167 procedure Check_Generic_Child_Unit
7169 Parent_Installed
: in out Boolean)
7171 Loc
: constant Source_Ptr
:= Sloc
(Gen_Id
);
7172 Gen_Par
: Entity_Id
:= Empty
;
7174 Inst_Par
: Entity_Id
;
7177 function Find_Generic_Child
7179 Id
: Node_Id
) return Entity_Id
;
7180 -- Search generic parent for possible child unit with the given name
7182 function In_Enclosing_Instance
return Boolean;
7183 -- Within an instance of the parent, the child unit may be denoted by
7184 -- a simple name, or an abbreviated expanded name. Examine enclosing
7185 -- scopes to locate a possible parent instantiation.
7187 ------------------------
7188 -- Find_Generic_Child --
7189 ------------------------
7191 function Find_Generic_Child
7193 Id
: Node_Id
) return Entity_Id
7198 -- If entity of name is already set, instance has already been
7199 -- resolved, e.g. in an enclosing instantiation.
7201 if Present
(Entity
(Id
)) then
7202 if Scope
(Entity
(Id
)) = Scop
then
7209 E
:= First_Entity
(Scop
);
7210 while Present
(E
) loop
7211 if Chars
(E
) = Chars
(Id
)
7212 and then Is_Child_Unit
(E
)
7214 if Is_Child_Unit
(E
)
7215 and then not Is_Visible_Lib_Unit
(E
)
7218 ("generic child unit& is not visible", Gen_Id
, E
);
7230 end Find_Generic_Child
;
7232 ---------------------------
7233 -- In_Enclosing_Instance --
7234 ---------------------------
7236 function In_Enclosing_Instance
return Boolean is
7237 Enclosing_Instance
: Node_Id
;
7238 Instance_Decl
: Node_Id
;
7241 -- We do not inline any call that contains instantiations, except
7242 -- for instantiations of Unchecked_Conversion, so if we are within
7243 -- an inlined body the current instance does not require parents.
7245 if In_Inlined_Body
then
7246 pragma Assert
(Chars
(Gen_Id
) = Name_Unchecked_Conversion
);
7250 -- Loop to check enclosing scopes
7252 Enclosing_Instance
:= Current_Scope
;
7253 while Present
(Enclosing_Instance
) loop
7254 Instance_Decl
:= Unit_Declaration_Node
(Enclosing_Instance
);
7256 if Ekind
(Enclosing_Instance
) = E_Package
7257 and then Is_Generic_Instance
(Enclosing_Instance
)
7259 (Generic_Parent
(Specification
(Instance_Decl
)))
7261 -- Check whether the generic we are looking for is a child of
7264 E
:= Find_Generic_Child
7265 (Generic_Parent
(Specification
(Instance_Decl
)), Gen_Id
);
7266 exit when Present
(E
);
7272 Enclosing_Instance
:= Scope
(Enclosing_Instance
);
7284 Make_Expanded_Name
(Loc
,
7286 Prefix
=> New_Occurrence_Of
(Enclosing_Instance
, Loc
),
7287 Selector_Name
=> New_Occurrence_Of
(E
, Loc
)));
7289 Set_Entity
(Gen_Id
, E
);
7290 Set_Etype
(Gen_Id
, Etype
(E
));
7291 Parent_Installed
:= False; -- Already in scope.
7294 end In_Enclosing_Instance
;
7296 -- Start of processing for Check_Generic_Child_Unit
7299 -- If the name of the generic is given by a selected component, it may
7300 -- be the name of a generic child unit, and the prefix is the name of an
7301 -- instance of the parent, in which case the child unit must be visible.
7302 -- If this instance is not in scope, it must be placed there and removed
7303 -- after instantiation, because what is being instantiated is not the
7304 -- original child, but the corresponding child present in the instance
7307 -- If the child is instantiated within the parent, it can be given by
7308 -- a simple name. In this case the instance is already in scope, but
7309 -- the child generic must be recovered from the generic parent as well.
7311 if Nkind
(Gen_Id
) = N_Selected_Component
then
7312 S
:= Selector_Name
(Gen_Id
);
7313 Analyze
(Prefix
(Gen_Id
));
7314 Inst_Par
:= Entity
(Prefix
(Gen_Id
));
7316 if Ekind
(Inst_Par
) = E_Package
7317 and then Present
(Renamed_Entity
(Inst_Par
))
7319 Inst_Par
:= Renamed_Entity
(Inst_Par
);
7322 if Ekind
(Inst_Par
) = E_Package
then
7323 if Nkind
(Parent
(Inst_Par
)) = N_Package_Specification
then
7324 Gen_Par
:= Generic_Parent
(Parent
(Inst_Par
));
7326 elsif Nkind
(Parent
(Inst_Par
)) = N_Defining_Program_Unit_Name
7328 Nkind
(Parent
(Parent
(Inst_Par
))) = N_Package_Specification
7330 Gen_Par
:= Generic_Parent
(Parent
(Parent
(Inst_Par
)));
7333 elsif Ekind
(Inst_Par
) = E_Generic_Package
7334 and then Nkind
(Parent
(Gen_Id
)) = N_Formal_Package_Declaration
7336 -- A formal package may be a real child package, and not the
7337 -- implicit instance within a parent. In this case the child is
7338 -- not visible and has to be retrieved explicitly as well.
7340 Gen_Par
:= Inst_Par
;
7343 if Present
(Gen_Par
) then
7345 -- The prefix denotes an instantiation. The entity itself may be a
7346 -- nested generic, or a child unit.
7348 E
:= Find_Generic_Child
(Gen_Par
, S
);
7351 Change_Selected_Component_To_Expanded_Name
(Gen_Id
);
7352 Set_Entity
(Gen_Id
, E
);
7353 Set_Etype
(Gen_Id
, Etype
(E
));
7355 Set_Etype
(S
, Etype
(E
));
7357 -- Indicate that this is a reference to the parent
7359 if In_Extended_Main_Source_Unit
(Gen_Id
) then
7360 Set_Is_Instantiated
(Inst_Par
);
7363 -- A common mistake is to replicate the naming scheme of a
7364 -- hierarchy by instantiating a generic child directly, rather
7365 -- than the implicit child in a parent instance:
7367 -- generic .. package Gpar is ..
7368 -- generic .. package Gpar.Child is ..
7369 -- package Par is new Gpar ();
7372 -- package Par.Child is new Gpar.Child ();
7373 -- rather than Par.Child
7375 -- In this case the instantiation is within Par, which is an
7376 -- instance, but Gpar does not denote Par because we are not IN
7377 -- the instance of Gpar, so this is illegal. The test below
7378 -- recognizes this particular case.
7380 if Is_Child_Unit
(E
)
7381 and then not Comes_From_Source
(Entity
(Prefix
(Gen_Id
)))
7382 and then (not In_Instance
7383 or else Nkind
(Parent
(Parent
(Gen_Id
))) =
7387 ("prefix of generic child unit must be instance of parent",
7391 if not In_Open_Scopes
(Inst_Par
)
7392 and then Nkind
(Parent
(Gen_Id
)) not in
7393 N_Generic_Renaming_Declaration
7395 Install_Parent
(Inst_Par
);
7396 Parent_Installed
:= True;
7398 elsif In_Open_Scopes
(Inst_Par
) then
7400 -- If the parent is already installed, install the actuals
7401 -- for its formal packages. This is necessary when the child
7402 -- instance is a child of the parent instance: in this case,
7403 -- the parent is placed on the scope stack but the formal
7404 -- packages are not made visible.
7406 Install_Formal_Packages
(Inst_Par
);
7410 -- If the generic parent does not contain an entity that
7411 -- corresponds to the selector, the instance doesn't either.
7412 -- Analyzing the node will yield the appropriate error message.
7413 -- If the entity is not a child unit, then it is an inner
7414 -- generic in the parent.
7422 if Is_Child_Unit
(Entity
(Gen_Id
))
7424 Nkind
(Parent
(Gen_Id
)) not in N_Generic_Renaming_Declaration
7425 and then not In_Open_Scopes
(Inst_Par
)
7427 Install_Parent
(Inst_Par
);
7428 Parent_Installed
:= True;
7430 -- The generic unit may be the renaming of the implicit child
7431 -- present in an instance. In that case the parent instance is
7432 -- obtained from the name of the renamed entity.
7434 elsif Ekind
(Entity
(Gen_Id
)) = E_Generic_Package
7435 and then Present
(Renamed_Entity
(Entity
(Gen_Id
)))
7436 and then Is_Child_Unit
(Renamed_Entity
(Entity
(Gen_Id
)))
7439 Renamed_Package
: constant Node_Id
:=
7440 Name
(Parent
(Entity
(Gen_Id
)));
7442 if Nkind
(Renamed_Package
) = N_Expanded_Name
then
7443 Inst_Par
:= Entity
(Prefix
(Renamed_Package
));
7444 Install_Parent
(Inst_Par
);
7445 Parent_Installed
:= True;
7451 elsif Nkind
(Gen_Id
) = N_Expanded_Name
then
7453 -- Entity already present, analyze prefix, whose meaning may be an
7454 -- instance in the current context. If it is an instance of a
7455 -- relative within another, the proper parent may still have to be
7456 -- installed, if they are not of the same generation.
7458 Analyze
(Prefix
(Gen_Id
));
7460 -- Prevent cascaded errors
7462 if Etype
(Prefix
(Gen_Id
)) = Any_Type
then
7466 -- In the unlikely case that a local declaration hides the name of
7467 -- the parent package, locate it on the homonym chain. If the context
7468 -- is an instance of the parent, the renaming entity is flagged as
7471 Inst_Par
:= Entity
(Prefix
(Gen_Id
));
7472 while Present
(Inst_Par
)
7473 and then not Is_Package_Or_Generic_Package
(Inst_Par
)
7475 Inst_Par
:= Homonym
(Inst_Par
);
7478 pragma Assert
(Present
(Inst_Par
));
7479 Set_Entity
(Prefix
(Gen_Id
), Inst_Par
);
7481 if In_Enclosing_Instance
then
7484 elsif Present
(Entity
(Gen_Id
))
7485 and then No
(Renamed_Entity
(Entity
(Gen_Id
)))
7486 and then Is_Child_Unit
(Entity
(Gen_Id
))
7487 and then not In_Open_Scopes
(Inst_Par
)
7489 Install_Parent
(Inst_Par
);
7490 Parent_Installed
:= True;
7492 -- Handle renaming of generic child unit
7494 elsif Present
(Entity
(Gen_Id
))
7495 and then Present
(Renamed_Entity
(Entity
(Gen_Id
)))
7496 and then Is_Child_Unit
(Renamed_Entity
(Entity
(Gen_Id
)))
7503 -- The entity of the renamed generic child unit does not
7504 -- have any reference to the instantiated parent. In order to
7505 -- locate it we traverse the scope containing the renaming
7506 -- declaration; the instance of the parent is available in
7507 -- the prefix of the renaming declaration. For example:
7510 -- package Inst_Par is new ...
7511 -- generic package Ren_Child renames Ins_Par.Child;
7516 -- package Inst_Child is new A.Ren_Child;
7519 E
:= First_Entity
(Entity
(Prefix
(Gen_Id
)));
7520 while Present
(E
) loop
7521 if not Is_Object
(E
)
7522 and then Present
(Renamed_Entity
(E
))
7524 Renamed_Entity
(E
) = Renamed_Entity
(Entity
(Gen_Id
))
7526 Ren_Decl
:= Parent
(E
);
7527 Inst_Par
:= Entity
(Prefix
(Name
(Ren_Decl
)));
7529 if not In_Open_Scopes
(Inst_Par
) then
7530 Install_Parent
(Inst_Par
);
7531 Parent_Installed
:= True;
7537 E
:= Next_Entity
(E
);
7542 elsif In_Enclosing_Instance
then
7544 -- The child unit is found in some enclosing scope
7551 -- If this is the renaming of the implicit child in a parent
7552 -- instance, recover the parent name and install it.
7554 if Is_Entity_Name
(Gen_Id
) then
7555 E
:= Entity
(Gen_Id
);
7557 if Is_Generic_Unit
(E
)
7558 and then Nkind
(Parent
(E
)) in N_Generic_Renaming_Declaration
7559 and then Is_Child_Unit
(Renamed_Entity
(E
))
7560 and then Is_Generic_Unit
(Scope
(Renamed_Entity
(E
)))
7561 and then Nkind
(Name
(Parent
(E
))) = N_Expanded_Name
7563 Rewrite
(Gen_Id
, New_Copy_Tree
(Name
(Parent
(E
))));
7564 Inst_Par
:= Entity
(Prefix
(Gen_Id
));
7566 if not In_Open_Scopes
(Inst_Par
) then
7567 Install_Parent
(Inst_Par
);
7568 Parent_Installed
:= True;
7571 -- If it is a child unit of a non-generic parent, it may be
7572 -- use-visible and given by a direct name. Install parent as
7575 elsif Is_Generic_Unit
(E
)
7576 and then Is_Child_Unit
(E
)
7578 Nkind
(Parent
(Gen_Id
)) not in N_Generic_Renaming_Declaration
7579 and then not Is_Generic_Unit
(Scope
(E
))
7581 if not In_Open_Scopes
(Scope
(E
)) then
7582 Install_Parent
(Scope
(E
));
7583 Parent_Installed
:= True;
7588 end Check_Generic_Child_Unit
;
7590 -----------------------------
7591 -- Check_Hidden_Child_Unit --
7592 -----------------------------
7594 procedure Check_Hidden_Child_Unit
7596 Gen_Unit
: Entity_Id
;
7597 Act_Decl_Id
: Entity_Id
)
7599 Gen_Id
: constant Node_Id
:= Name
(N
);
7602 if Is_Child_Unit
(Gen_Unit
)
7603 and then Is_Child_Unit
(Act_Decl_Id
)
7604 and then Nkind
(Gen_Id
) = N_Expanded_Name
7605 and then Entity
(Prefix
(Gen_Id
)) = Scope
(Act_Decl_Id
)
7606 and then Chars
(Gen_Unit
) = Chars
(Act_Decl_Id
)
7608 Error_Msg_Node_2
:= Scope
(Act_Decl_Id
);
7610 ("generic unit & is implicitly declared in &",
7611 Defining_Unit_Name
(N
), Gen_Unit
);
7612 Error_Msg_N
("\instance must have different name",
7613 Defining_Unit_Name
(N
));
7615 end Check_Hidden_Child_Unit
;
7617 ------------------------
7618 -- Check_Private_View --
7619 ------------------------
7621 procedure Check_Private_View
(N
: Node_Id
) is
7622 T
: constant Entity_Id
:= Etype
(N
);
7626 -- Exchange views if the type was not private in the generic but is
7627 -- private at the point of instantiation. Do not exchange views if
7628 -- the scope of the type is in scope. This can happen if both generic
7629 -- and instance are sibling units, or if type is defined in a parent.
7630 -- In this case the visibility of the type will be correct for all
7634 BT
:= Base_Type
(T
);
7636 if Is_Private_Type
(T
)
7637 and then not Has_Private_View
(N
)
7638 and then Present
(Full_View
(T
))
7639 and then not In_Open_Scopes
(Scope
(T
))
7641 -- In the generic, the full declaration was visible
7645 elsif Has_Private_View
(N
)
7646 and then not Is_Private_Type
(T
)
7647 and then not Has_Been_Exchanged
(T
)
7648 and then (not In_Open_Scopes
(Scope
(T
))
7649 or else Nkind
(Parent
(N
)) = N_Subtype_Declaration
)
7651 -- In the generic, only the private declaration was visible
7653 -- If the type appears in a subtype declaration, the subtype in
7654 -- instance must have a view compatible with that of its parent,
7655 -- which must be exchanged (see corresponding code in Restore_
7656 -- Private_Views) so we make an exception to the open scope rule.
7658 Prepend_Elmt
(T
, Exchanged_Views
);
7659 Exchange_Declarations
(Etype
(Get_Associated_Node
(N
)));
7661 -- Finally, a non-private subtype may have a private base type, which
7662 -- must be exchanged for consistency. This can happen when a package
7663 -- body is instantiated, when the scope stack is empty but in fact
7664 -- the subtype and the base type are declared in an enclosing scope.
7666 -- Note that in this case we introduce an inconsistency in the view
7667 -- set, because we switch the base type BT, but there could be some
7668 -- private dependent subtypes of BT which remain unswitched. Such
7669 -- subtypes might need to be switched at a later point (see specific
7670 -- provision for that case in Switch_View).
7672 elsif not Is_Private_Type
(T
)
7673 and then not Has_Private_View
(N
)
7674 and then Is_Private_Type
(BT
)
7675 and then Present
(Full_View
(BT
))
7676 and then not Is_Generic_Type
(BT
)
7677 and then not In_Open_Scopes
(BT
)
7679 Prepend_Elmt
(Full_View
(BT
), Exchanged_Views
);
7680 Exchange_Declarations
(BT
);
7683 end Check_Private_View
;
7685 -----------------------------
7686 -- Check_Hidden_Primitives --
7687 -----------------------------
7689 function Check_Hidden_Primitives
(Assoc_List
: List_Id
) return Elist_Id
is
7692 Result
: Elist_Id
:= No_Elist
;
7695 if No
(Assoc_List
) then
7699 -- Traverse the list of associations between formals and actuals
7700 -- searching for renamings of tagged types
7702 Actual
:= First
(Assoc_List
);
7703 while Present
(Actual
) loop
7704 if Nkind
(Actual
) = N_Subtype_Declaration
then
7705 Gen_T
:= Generic_Parent_Type
(Actual
);
7707 if Present
(Gen_T
) and then Is_Tagged_Type
(Gen_T
) then
7709 -- Traverse the list of primitives of the actual types
7710 -- searching for hidden primitives that are visible in the
7711 -- corresponding generic formal; leave them visible and
7712 -- append them to Result to restore their decoration later.
7714 Install_Hidden_Primitives
7715 (Prims_List
=> Result
,
7717 Act_T
=> Entity
(Subtype_Indication
(Actual
)));
7725 end Check_Hidden_Primitives
;
7727 --------------------------
7728 -- Contains_Instance_Of --
7729 --------------------------
7731 function Contains_Instance_Of
7734 N
: Node_Id
) return Boolean
7742 -- Verify that there are no circular instantiations. We check whether
7743 -- the unit contains an instance of the current scope or some enclosing
7744 -- scope (in case one of the instances appears in a subunit). Longer
7745 -- circularities involving subunits might seem too pathological to
7746 -- consider, but they were not too pathological for the authors of
7747 -- DEC bc30vsq, so we loop over all enclosing scopes, and mark all
7748 -- enclosing generic scopes as containing an instance.
7751 -- Within a generic subprogram body, the scope is not generic, to
7752 -- allow for recursive subprograms. Use the declaration to determine
7753 -- whether this is a generic unit.
7755 if Ekind
(Scop
) = E_Generic_Package
7756 or else (Is_Subprogram
(Scop
)
7757 and then Nkind
(Unit_Declaration_Node
(Scop
)) =
7758 N_Generic_Subprogram_Declaration
)
7760 Elmt
:= First_Elmt
(Inner_Instances
(Inner
));
7762 while Present
(Elmt
) loop
7763 if Node
(Elmt
) = Scop
then
7764 Error_Msg_Node_2
:= Inner
;
7766 ("circular instantiation: & instantiated within &!",
7770 elsif Node
(Elmt
) = Inner
then
7773 elsif Contains_Instance_Of
(Node
(Elmt
), Scop
, N
) then
7774 Error_Msg_Node_2
:= Inner
;
7776 ("circular instantiation: & instantiated within &!",
7784 -- Indicate that Inner is being instantiated within Scop
7786 Append_Elmt
(Inner
, Inner_Instances
(Scop
));
7789 if Scop
= Standard_Standard
then
7792 Scop
:= Scope
(Scop
);
7797 end Contains_Instance_Of
;
7799 -----------------------
7800 -- Copy_Generic_Node --
7801 -----------------------
7803 function Copy_Generic_Node
7805 Parent_Id
: Node_Id
;
7806 Instantiating
: Boolean) return Node_Id
7811 function Copy_Generic_Descendant
(D
: Union_Id
) return Union_Id
;
7812 -- Check the given value of one of the Fields referenced by the current
7813 -- node to determine whether to copy it recursively. The field may hold
7814 -- a Node_Id, a List_Id, or an Elist_Id, or a plain value (Sloc, Uint,
7815 -- Char) in which case it need not be copied.
7817 procedure Copy_Descendants
;
7818 -- Common utility for various nodes
7820 function Copy_Generic_Elist
(E
: Elist_Id
) return Elist_Id
;
7821 -- Make copy of element list
7823 function Copy_Generic_List
7825 Parent_Id
: Node_Id
) return List_Id
;
7826 -- Apply Copy_Generic_Node recursively to the members of a node list
7828 function In_Defining_Unit_Name
(Nam
: Node_Id
) return Boolean;
7829 -- True if an identifier is part of the defining program unit name of
7831 -- Consider removing this subprogram now that ASIS no longer uses it.
7833 ----------------------
7834 -- Copy_Descendants --
7835 ----------------------
7837 procedure Copy_Descendants
is
7838 procedure Walk
is new
7839 Walk_Sinfo_Fields_Pairwise
(Copy_Generic_Descendant
);
7842 end Copy_Descendants
;
7844 -----------------------------
7845 -- Copy_Generic_Descendant --
7846 -----------------------------
7848 function Copy_Generic_Descendant
(D
: Union_Id
) return Union_Id
is
7850 if D
= Union_Id
(Empty
) then
7853 elsif D
in Node_Range
then
7855 (Copy_Generic_Node
(Node_Id
(D
), New_N
, Instantiating
));
7857 elsif D
in List_Range
then
7858 return Union_Id
(Copy_Generic_List
(List_Id
(D
), New_N
));
7860 elsif D
in Elist_Range
then
7861 return Union_Id
(Copy_Generic_Elist
(Elist_Id
(D
)));
7863 -- Nothing else is copyable (e.g. Uint values), return as is
7868 end Copy_Generic_Descendant
;
7870 ------------------------
7871 -- Copy_Generic_Elist --
7872 ------------------------
7874 function Copy_Generic_Elist
(E
: Elist_Id
) return Elist_Id
is
7881 M
:= First_Elmt
(E
);
7882 while Present
(M
) loop
7884 (Copy_Generic_Node
(Node
(M
), Empty
, Instantiating
), L
);
7893 end Copy_Generic_Elist
;
7895 -----------------------
7896 -- Copy_Generic_List --
7897 -----------------------
7899 function Copy_Generic_List
7901 Parent_Id
: Node_Id
) return List_Id
7909 Set_Parent
(New_L
, Parent_Id
);
7912 while Present
(N
) loop
7913 Append
(Copy_Generic_Node
(N
, Empty
, Instantiating
), New_L
);
7922 end Copy_Generic_List
;
7924 ---------------------------
7925 -- In_Defining_Unit_Name --
7926 ---------------------------
7928 function In_Defining_Unit_Name
(Nam
: Node_Id
) return Boolean is
7931 Present
(Parent
(Nam
))
7932 and then (Nkind
(Parent
(Nam
)) = N_Defining_Program_Unit_Name
7934 (Nkind
(Parent
(Nam
)) = N_Expanded_Name
7935 and then In_Defining_Unit_Name
(Parent
(Nam
))));
7936 end In_Defining_Unit_Name
;
7938 -- Start of processing for Copy_Generic_Node
7945 New_N
:= New_Copy
(N
);
7947 -- Copy aspects if present
7949 if Has_Aspects
(N
) then
7950 Set_Has_Aspects
(New_N
, False);
7951 Set_Aspect_Specifications
7952 (New_N
, Copy_Generic_List
(Aspect_Specifications
(N
), Parent_Id
));
7955 -- If we are instantiating, we want to adjust the sloc based on the
7956 -- current S_Adjustment. However, if this is the root node of a subunit,
7957 -- we need to defer that adjustment to below (see "elsif Instantiating
7958 -- and Was_Stub"), so it comes after Create_Instantiation_Source has
7959 -- computed the adjustment.
7962 and then not (Nkind
(N
) in N_Proper_Body
7963 and then Was_Originally_Stub
(N
))
7965 Adjust_Instantiation_Sloc
(New_N
, S_Adjustment
);
7968 if not Is_List_Member
(N
) then
7969 Set_Parent
(New_N
, Parent_Id
);
7972 -- Special casing for identifiers and other entity names and operators
7974 if Nkind
(N
) in N_Character_Literal
7980 if not Instantiating
then
7982 -- Link both nodes in order to assign subsequently the entity of
7983 -- the copy to the original node, in case this is a global
7986 Set_Associated_Node
(N
, New_N
);
7988 -- If we are within an instantiation, this is a nested generic
7989 -- that has already been analyzed at the point of definition.
7990 -- We must preserve references that were global to the enclosing
7991 -- parent at that point. Other occurrences, whether global or
7992 -- local to the current generic, must be resolved anew, so we
7993 -- reset the entity in the generic copy. A global reference has a
7994 -- smaller depth than the parent, or else the same depth in case
7995 -- both are distinct compilation units.
7997 -- A child unit is implicitly declared within the enclosing parent
7998 -- but is in fact global to it, and must be preserved.
8000 -- It is also possible for Current_Instantiated_Parent to be
8001 -- defined, and for this not to be a nested generic, namely if
8002 -- the unit is loaded through Rtsfind. In that case, the entity of
8003 -- New_N is only a link to the associated node, and not a defining
8006 -- The entities for parent units in the defining_program_unit of a
8007 -- generic child unit are established when the context of the unit
8008 -- is first analyzed, before the generic copy is made. They are
8009 -- preserved in the copy for use in e.g. ASIS queries.
8011 Ent
:= Entity
(New_N
);
8013 if No
(Current_Instantiated_Parent
.Gen_Id
) then
8015 or else Nkind
(Ent
) /= N_Defining_Identifier
8016 or else not In_Defining_Unit_Name
(N
)
8018 Set_Associated_Node
(New_N
, Empty
);
8022 or else Nkind
(Ent
) not in N_Entity
8023 or else No
(Scope
(Ent
))
8025 (Scope
(Ent
) = Current_Instantiated_Parent
.Gen_Id
8026 and then not Is_Child_Unit
(Ent
))
8028 (Scope_Depth_Set
(Scope
(Ent
))
8030 Scope_Depth
(Scope
(Ent
)) >
8031 Scope_Depth
(Current_Instantiated_Parent
.Gen_Id
)
8033 Get_Source_Unit
(Ent
) =
8034 Get_Source_Unit
(Current_Instantiated_Parent
.Gen_Id
))
8036 Set_Associated_Node
(New_N
, Empty
);
8039 -- Case of instantiating identifier or some other name or operator
8042 -- If the associated node is still defined, the entity in it
8043 -- is global, and must be copied to the instance. If this copy
8044 -- is being made for a body to inline, it is applied to an
8045 -- instantiated tree, and the entity is already present and
8046 -- must be also preserved.
8049 Assoc
: constant Node_Id
:= Get_Associated_Node
(N
);
8052 if Present
(Assoc
) then
8053 if Nkind
(Assoc
) = Nkind
(N
) then
8054 Set_Entity
(New_N
, Entity
(Assoc
));
8055 Check_Private_View
(N
);
8057 -- Here we deal with a very peculiar case for which the
8058 -- Has_Private_View mechanism is not sufficient, because
8059 -- the reference to the type is implicit in the tree,
8060 -- that is to say, it's not referenced from a node but
8061 -- only from another type, namely through Component_Type.
8065 -- type Pt is private;
8068 -- type Ft is array (Positive range <>) of Pt;
8070 -- procedure Check (F1, F2 : Ft; Lt : Boolean);
8074 -- type Pt is new Boolean;
8077 -- package body P is
8078 -- package body G is
8079 -- procedure Check (F1, F2 : Ft; Lt : Boolean) is
8081 -- if (F1 < F2) /= Lt then
8088 -- type Arr is array (Positive range <>) of P.Pt;
8090 -- package Inst is new P.G (Arr);
8092 -- Pt is a global type for the generic package G and it
8093 -- is not referenced in its body, but only as component
8094 -- type of Ft, which is a local type. This means that no
8095 -- references to Pt or Ft are seen during the copy of the
8096 -- body, the only reference to Pt being seen is when the
8097 -- actuals are checked by Check_Generic_Actuals, but Pt
8098 -- is still private at this point. In the end, the views
8099 -- of Pt are not switched in the body and, therefore, the
8100 -- array comparison is rejected because the component is
8103 -- Adding e.g. a dummy variable of type Pt in the body is
8104 -- sufficient to make everything work, so we generate an
8105 -- artificial reference to Pt on the fly and thus force
8106 -- the switching of views on the grounds that, if the
8107 -- comparison was accepted during the semantic analysis
8108 -- of the generic, this means that the component cannot
8109 -- have been private (see Sem_Type.Valid_Comparison_Arg).
8111 if Nkind
(Assoc
) in N_Op_Compare
8112 and then Present
(Etype
(Left_Opnd
(Assoc
)))
8113 and then Is_Array_Type
(Etype
(Left_Opnd
(Assoc
)))
8114 and then Present
(Etype
(Right_Opnd
(Assoc
)))
8115 and then Is_Array_Type
(Etype
(Right_Opnd
(Assoc
)))
8118 Ltyp
: constant Entity_Id
:=
8119 Etype
(Left_Opnd
(Assoc
));
8120 Rtyp
: constant Entity_Id
:=
8121 Etype
(Right_Opnd
(Assoc
));
8123 if Is_Private_Type
(Component_Type
(Ltyp
)) then
8125 (New_Occurrence_Of
(Component_Type
(Ltyp
),
8128 if Is_Private_Type
(Component_Type
(Rtyp
)) then
8130 (New_Occurrence_Of
(Component_Type
(Rtyp
),
8135 -- Here is a similar case, for the Designated_Type of an
8136 -- access type that is present as target type in a type
8137 -- conversion from another access type. In this case, if
8138 -- the base types of the designated types are different
8139 -- and the conversion was accepted during the semantic
8140 -- analysis of the generic, this means that the target
8141 -- type cannot have been private (see Valid_Conversion).
8143 elsif Nkind
(Assoc
) = N_Identifier
8144 and then Nkind
(Parent
(Assoc
)) = N_Type_Conversion
8145 and then Subtype_Mark
(Parent
(Assoc
)) = Assoc
8146 and then Present
(Etype
(Assoc
))
8147 and then Is_Access_Type
(Etype
(Assoc
))
8148 and then Present
(Etype
(Expression
(Parent
(Assoc
))))
8150 Is_Access_Type
(Etype
(Expression
(Parent
(Assoc
))))
8153 Targ_Desig
: constant Entity_Id
:=
8154 Designated_Type
(Etype
(Assoc
));
8155 Expr_Desig
: constant Entity_Id
:=
8157 (Etype
(Expression
(Parent
(Assoc
))));
8159 if Base_Type
(Targ_Desig
) /= Base_Type
(Expr_Desig
)
8160 and then Is_Private_Type
(Targ_Desig
)
8163 (New_Occurrence_Of
(Targ_Desig
, Sloc
(N
)));
8168 -- The node is a reference to a global type and acts as the
8169 -- subtype mark of a qualified expression created in order
8170 -- to aid resolution of accidental overloading in instances.
8171 -- Since N is a reference to a type, the Associated_Node of
8172 -- N denotes an entity rather than another identifier. See
8173 -- Qualify_Universal_Operands for details.
8175 elsif Nkind
(N
) = N_Identifier
8176 and then Nkind
(Parent
(N
)) = N_Qualified_Expression
8177 and then Subtype_Mark
(Parent
(N
)) = N
8178 and then Is_Qualified_Universal_Literal
(Parent
(N
))
8180 Set_Entity
(New_N
, Assoc
);
8182 -- Cope with the rewriting into expanded name that may have
8183 -- occurred in between, e.g. in Check_Generic_Child_Unit for
8184 -- generic renaming declarations.
8186 elsif Nkind
(Assoc
) = N_Expanded_Name
then
8187 Rewrite
(N
, New_Copy_Tree
(Assoc
));
8188 Set_Associated_Node
(N
, Assoc
);
8189 return Copy_Generic_Node
(N
, Parent_Id
, Instantiating
);
8191 -- The name in the call may be a selected component if the
8192 -- call has not been analyzed yet, as may be the case for
8193 -- pre/post conditions in a generic unit.
8195 elsif Nkind
(Assoc
) = N_Function_Call
8196 and then Is_Entity_Name
(Name
(Assoc
))
8198 Set_Entity
(New_N
, Entity
(Name
(Assoc
)));
8199 Check_Private_View
(N
);
8201 elsif Nkind
(Assoc
) in N_Entity
8202 and then (Expander_Active
8203 or else (GNATprove_Mode
8204 and then not In_Spec_Expression
8205 and then not Inside_A_Generic
))
8207 -- Inlining case: we are copying a tree that contains
8208 -- global entities, which are preserved in the copy to be
8209 -- used for subsequent inlining.
8214 Set_Entity
(New_N
, Empty
);
8220 -- For expanded name, we must copy the Prefix and Selector_Name
8222 if Nkind
(N
) = N_Expanded_Name
then
8224 (New_N
, Copy_Generic_Node
(Prefix
(N
), New_N
, Instantiating
));
8226 Set_Selector_Name
(New_N
,
8227 Copy_Generic_Node
(Selector_Name
(N
), New_N
, Instantiating
));
8229 -- For operators, copy the operands
8231 elsif Nkind
(N
) in N_Op
then
8232 if Nkind
(N
) in N_Binary_Op
then
8233 Set_Left_Opnd
(New_N
,
8234 Copy_Generic_Node
(Left_Opnd
(N
), New_N
, Instantiating
));
8237 Set_Right_Opnd
(New_N
,
8238 Copy_Generic_Node
(Right_Opnd
(N
), New_N
, Instantiating
));
8241 -- Establish a link between an entity from the generic template and the
8242 -- corresponding entity in the generic copy to be analyzed.
8244 elsif Nkind
(N
) in N_Entity
then
8245 if not Instantiating
then
8246 Set_Associated_Entity
(N
, New_N
);
8249 -- Clear any existing link the copy may inherit from the replicated
8250 -- generic template entity.
8252 Set_Associated_Entity
(New_N
, Empty
);
8254 -- Special casing for stubs
8256 elsif Nkind
(N
) in N_Body_Stub
then
8258 -- In any case, we must copy the specification or defining
8259 -- identifier as appropriate.
8261 if Nkind
(N
) = N_Subprogram_Body_Stub
then
8262 Set_Specification
(New_N
,
8263 Copy_Generic_Node
(Specification
(N
), New_N
, Instantiating
));
8266 Set_Defining_Identifier
(New_N
,
8268 (Defining_Identifier
(N
), New_N
, Instantiating
));
8271 -- If we are not instantiating, then this is where we load and
8272 -- analyze subunits, i.e. at the point where the stub occurs. A
8273 -- more permissive system might defer this analysis to the point
8274 -- of instantiation, but this seems too complicated for now.
8276 if not Instantiating
then
8278 Subunit_Name
: constant Unit_Name_Type
:= Get_Unit_Name
(N
);
8280 Unum
: Unit_Number_Type
;
8284 -- Make sure that, if it is a subunit of the main unit that is
8285 -- preprocessed and if -gnateG is specified, the preprocessed
8286 -- file will be written.
8288 Lib
.Analysing_Subunit_Of_Main
:=
8289 Lib
.In_Extended_Main_Source_Unit
(N
);
8292 (Load_Name
=> Subunit_Name
,
8296 Lib
.Analysing_Subunit_Of_Main
:= False;
8298 -- If the proper body is not found, a warning message will be
8299 -- emitted when analyzing the stub, or later at the point of
8300 -- instantiation. Here we just leave the stub as is.
8302 if Unum
= No_Unit
then
8303 Subunits_Missing
:= True;
8304 goto Subunit_Not_Found
;
8307 Subunit
:= Cunit
(Unum
);
8309 if Nkind
(Unit
(Subunit
)) /= N_Subunit
then
8311 ("found child unit instead of expected SEPARATE subunit",
8313 Error_Msg_Sloc
:= Sloc
(N
);
8314 Error_Msg_N
("\to complete stub #", Subunit
);
8315 goto Subunit_Not_Found
;
8318 -- We must create a generic copy of the subunit, in order to
8319 -- perform semantic analysis on it, and we must replace the
8320 -- stub in the original generic unit with the subunit, in order
8321 -- to preserve non-local references within.
8323 -- Only the proper body needs to be copied. Library_Unit and
8324 -- context clause are simply inherited by the generic copy.
8325 -- Note that the copy (which may be recursive if there are
8326 -- nested subunits) must be done first, before attaching it to
8327 -- the enclosing generic.
8331 (Proper_Body
(Unit
(Subunit
)),
8332 Empty
, Instantiating
=> False);
8334 -- Now place the original proper body in the original generic
8335 -- unit. This is a body, not a compilation unit.
8337 Rewrite
(N
, Proper_Body
(Unit
(Subunit
)));
8338 Set_Is_Compilation_Unit
(Defining_Entity
(N
), False);
8339 Set_Was_Originally_Stub
(N
);
8341 -- Finally replace the body of the subunit with its copy, and
8342 -- make this new subunit into the library unit of the generic
8343 -- copy, which does not have stubs any longer.
8345 Set_Proper_Body
(Unit
(Subunit
), New_Body
);
8346 Set_Library_Unit
(New_N
, Subunit
);
8347 Inherit_Context
(Unit
(Subunit
), N
);
8350 -- If we are instantiating, this must be an error case, since
8351 -- otherwise we would have replaced the stub node by the proper body
8352 -- that corresponds. So just ignore it in the copy (i.e. we have
8353 -- copied it, and that is good enough).
8359 <<Subunit_Not_Found
>> null;
8361 -- If the node is a compilation unit, it is the subunit of a stub, which
8362 -- has been loaded already (see code below). In this case, the library
8363 -- unit field of N points to the parent unit (which is a compilation
8364 -- unit) and need not (and cannot) be copied.
8366 -- When the proper body of the stub is analyzed, the library_unit link
8367 -- is used to establish the proper context (see sem_ch10).
8369 -- The other fields of a compilation unit are copied as usual
8371 elsif Nkind
(N
) = N_Compilation_Unit
then
8373 -- This code can only be executed when not instantiating, because in
8374 -- the copy made for an instantiation, the compilation unit node has
8375 -- disappeared at the point that a stub is replaced by its proper
8378 pragma Assert
(not Instantiating
);
8380 Set_Context_Items
(New_N
,
8381 Copy_Generic_List
(Context_Items
(N
), New_N
));
8384 Copy_Generic_Node
(Unit
(N
), New_N
, Instantiating
=> False));
8386 Set_First_Inlined_Subprogram
(New_N
,
8388 (First_Inlined_Subprogram
(N
), New_N
, Instantiating
=> False));
8393 (Aux_Decls_Node
(N
), New_N
, Instantiating
=> False));
8395 -- For an assignment node, the assignment is known to be semantically
8396 -- legal if we are instantiating the template. This avoids incorrect
8397 -- diagnostics in generated code.
8399 elsif Nkind
(N
) = N_Assignment_Statement
then
8401 -- Copy name and expression fields in usual manner
8404 Copy_Generic_Node
(Name
(N
), New_N
, Instantiating
));
8406 Set_Expression
(New_N
,
8407 Copy_Generic_Node
(Expression
(N
), New_N
, Instantiating
));
8409 if Instantiating
then
8410 Set_Assignment_OK
(Name
(New_N
), True);
8413 elsif Nkind
(N
) in N_Aggregate | N_Extension_Aggregate
then
8414 if not Instantiating
then
8415 Set_Associated_Node
(N
, New_N
);
8418 if Present
(Get_Associated_Node
(N
))
8419 and then Nkind
(Get_Associated_Node
(N
)) = Nkind
(N
)
8421 -- In the generic the aggregate has some composite type. If at
8422 -- the point of instantiation the type has a private view,
8423 -- install the full view (and that of its ancestors, if any).
8426 T
: Entity_Id
:= (Etype
(Get_Associated_Node
(New_N
)));
8430 if Present
(T
) and then Is_Private_Type
(T
) then
8435 and then Is_Tagged_Type
(T
)
8436 and then Is_Derived_Type
(T
)
8438 Rt
:= Root_Type
(T
);
8443 if Is_Private_Type
(T
) then
8454 -- Do not copy the associated node, which points to the generic copy
8455 -- of the aggregate.
8457 if Nkind
(N
) = N_Aggregate
then
8458 Set_Aggregate_Bounds
8460 Node_Id
(Copy_Generic_Descendant
8461 (Union_Id
(Aggregate_Bounds
(N
)))));
8463 elsif Nkind
(N
) = N_Extension_Aggregate
then
8466 Node_Id
(Copy_Generic_Descendant
8467 (Union_Id
(Ancestor_Part
(N
)))));
8470 pragma Assert
(False);
8475 List_Id
(Copy_Generic_Descendant
(Union_Id
(Expressions
(N
)))));
8476 Set_Component_Associations
8478 List_Id
(Copy_Generic_Descendant
8479 (Union_Id
(Component_Associations
(N
)))));
8481 (New_N
, Node_Id
(Copy_Generic_Descendant
(Union_Id
(Etype
(N
)))));
8483 -- Allocators do not have an identifier denoting the access type, so we
8484 -- must locate it through the expression to check whether the views are
8487 elsif Nkind
(N
) = N_Allocator
8488 and then Nkind
(Expression
(N
)) = N_Qualified_Expression
8489 and then Is_Entity_Name
(Subtype_Mark
(Expression
(N
)))
8490 and then Instantiating
8493 T
: constant Node_Id
:=
8494 Get_Associated_Node
(Subtype_Mark
(Expression
(N
)));
8500 -- Retrieve the allocator node in the generic copy
8502 Acc_T
:= Etype
(Parent
(Parent
(T
)));
8504 if Present
(Acc_T
) and then Is_Private_Type
(Acc_T
) then
8505 Switch_View
(Acc_T
);
8512 -- For a proper body, we must catch the case of a proper body that
8513 -- replaces a stub. This represents the point at which a separate
8514 -- compilation unit, and hence template file, may be referenced, so we
8515 -- must make a new source instantiation entry for the template of the
8516 -- subunit, and ensure that all nodes in the subunit are adjusted using
8517 -- this new source instantiation entry.
8519 elsif Nkind
(N
) in N_Proper_Body
then
8521 Save_Adjustment
: constant Sloc_Adjustment
:= S_Adjustment
;
8523 if Instantiating
and then Was_Originally_Stub
(N
) then
8524 Create_Instantiation_Source
8525 (Instantiation_Node
,
8526 Defining_Entity
(N
),
8529 Adjust_Instantiation_Sloc
(New_N
, S_Adjustment
);
8532 -- Now copy the fields of the proper body, using the new
8533 -- adjustment factor if one was needed as per test above.
8537 -- Restore the original adjustment factor
8539 S_Adjustment
:= Save_Adjustment
;
8542 elsif Nkind
(N
) = N_Pragma
and then Instantiating
then
8544 -- Do not copy Comment or Ident pragmas their content is relevant to
8545 -- the generic unit, not to the instantiating unit.
8547 if Pragma_Name_Unmapped
(N
) in Name_Comment | Name_Ident
then
8548 New_N
:= Make_Null_Statement
(Sloc
(N
));
8550 -- Do not copy pragmas generated from aspects because the pragmas do
8551 -- not carry any semantic information, plus they will be regenerated
8554 -- However, generating C we need to copy them since postconditions
8555 -- are inlined by the front end, and the front-end inlining machinery
8556 -- relies on this routine to perform inlining.
8558 elsif From_Aspect_Specification
(N
)
8559 and then not Modify_Tree_For_C
8561 New_N
:= Make_Null_Statement
(Sloc
(N
));
8567 elsif Nkind
(N
) in N_Integer_Literal | N_Real_Literal
then
8569 -- No descendant fields need traversing
8573 elsif Nkind
(N
) = N_String_Literal
8574 and then Present
(Etype
(N
))
8575 and then Instantiating
8577 -- If the string is declared in an outer scope, the string_literal
8578 -- subtype created for it may have the wrong scope. Force reanalysis
8579 -- of the constant to generate a new itype in the proper context.
8581 Set_Etype
(New_N
, Empty
);
8582 Set_Analyzed
(New_N
, False);
8584 -- For the remaining nodes, copy their descendants recursively
8589 if Instantiating
and then Nkind
(N
) = N_Subprogram_Body
then
8590 Set_Generic_Parent
(Specification
(New_N
), N
);
8592 -- Should preserve Corresponding_Spec??? (12.3(14))
8596 -- Propagate dimensions if present, so that they are reflected in the
8599 if Nkind
(N
) in N_Has_Etype
8600 and then (Nkind
(N
) in N_Op
or else Is_Entity_Name
(N
))
8601 and then Present
(Etype
(N
))
8602 and then Is_Floating_Point_Type
(Etype
(N
))
8603 and then Has_Dimension_System
(Etype
(N
))
8605 Copy_Dimensions
(N
, New_N
);
8609 end Copy_Generic_Node
;
8611 ----------------------------
8612 -- Denotes_Formal_Package --
8613 ----------------------------
8615 function Denotes_Formal_Package
8617 On_Exit
: Boolean := False;
8618 Instance
: Entity_Id
:= Empty
) return Boolean
8621 Scop
: constant Entity_Id
:= Scope
(Pack
);
8624 function Is_Actual_Of_Previous_Formal
(P
: Entity_Id
) return Boolean;
8625 -- The package in question may be an actual for a previous formal
8626 -- package P of the current instance, so examine its actuals as well.
8627 -- This must be recursive over other formal packages.
8629 ----------------------------------
8630 -- Is_Actual_Of_Previous_Formal --
8631 ----------------------------------
8633 function Is_Actual_Of_Previous_Formal
(P
: Entity_Id
) return Boolean is
8637 E1
:= First_Entity
(P
);
8638 while Present
(E1
) and then E1
/= Instance
loop
8639 if Ekind
(E1
) = E_Package
8640 and then Nkind
(Parent
(E1
)) = N_Package_Renaming_Declaration
8642 if Renamed_Entity
(E1
) = Pack
then
8645 elsif E1
= P
or else Renamed_Entity
(E1
) = P
then
8648 elsif Is_Actual_Of_Previous_Formal
(E1
) then
8657 end Is_Actual_Of_Previous_Formal
;
8659 -- Start of processing for Denotes_Formal_Package
8665 (Instance_Envs
.Last
).Instantiated_Parent
.Act_Id
;
8667 Par
:= Current_Instantiated_Parent
.Act_Id
;
8670 if Ekind
(Scop
) = E_Generic_Package
8671 or else Nkind
(Unit_Declaration_Node
(Scop
)) =
8672 N_Generic_Subprogram_Declaration
8676 elsif Nkind
(Original_Node
(Unit_Declaration_Node
(Pack
))) =
8677 N_Formal_Package_Declaration
8685 -- Check whether this package is associated with a formal package of
8686 -- the enclosing instantiation. Iterate over the list of renamings.
8688 E
:= First_Entity
(Par
);
8689 while Present
(E
) loop
8690 if Ekind
(E
) /= E_Package
8691 or else Nkind
(Parent
(E
)) /= N_Package_Renaming_Declaration
8695 elsif Renamed_Entity
(E
) = Par
then
8698 elsif Renamed_Entity
(E
) = Pack
then
8701 elsif Is_Actual_Of_Previous_Formal
(E
) then
8711 end Denotes_Formal_Package
;
8717 procedure End_Generic
is
8719 -- ??? More things could be factored out in this routine. Should
8720 -- probably be done at a later stage.
8722 Inside_A_Generic
:= Generic_Flags
.Table
(Generic_Flags
.Last
);
8723 Generic_Flags
.Decrement_Last
;
8725 Expander_Mode_Restore
;
8732 function Earlier
(N1
, N2
: Node_Id
) return Boolean is
8733 procedure Find_Depth
(P
: in out Node_Id
; D
: in out Integer);
8734 -- Find distance from given node to enclosing compilation unit
8740 procedure Find_Depth
(P
: in out Node_Id
; D
: in out Integer) is
8743 and then Nkind
(P
) /= N_Compilation_Unit
8745 P
:= True_Parent
(P
);
8750 -- Local declarations
8759 -- Start of processing for Earlier
8762 Find_Depth
(P1
, D1
);
8763 Find_Depth
(P2
, D2
);
8773 P1
:= True_Parent
(P1
);
8778 P2
:= True_Parent
(P2
);
8782 -- At this point P1 and P2 are at the same distance from the root.
8783 -- We examine their parents until we find a common declarative list.
8784 -- If we reach the root, N1 and N2 do not descend from the same
8785 -- declarative list (e.g. one is nested in the declarative part and
8786 -- the other is in a block in the statement part) and the earlier
8787 -- one is already frozen.
8789 while not Is_List_Member
(P1
)
8790 or else not Is_List_Member
(P2
)
8791 or else not In_Same_List
(P1
, P2
)
8793 P1
:= True_Parent
(P1
);
8794 P2
:= True_Parent
(P2
);
8796 if Nkind
(Parent
(P1
)) = N_Subunit
then
8797 P1
:= Corresponding_Stub
(Parent
(P1
));
8800 if Nkind
(Parent
(P2
)) = N_Subunit
then
8801 P2
:= Corresponding_Stub
(Parent
(P2
));
8809 -- Expanded code usually shares the source location of the original
8810 -- construct it was generated for. This however may not necessarily
8811 -- reflect the true location of the code within the tree.
8813 -- Before comparing the slocs of the two nodes, make sure that we are
8814 -- working with correct source locations. Assume that P1 is to the left
8815 -- of P2. If either one does not come from source, traverse the common
8816 -- list heading towards the other node and locate the first source
8820 -- ----+===+===+--------------+===+===+----
8821 -- expanded code expanded code
8823 if not Comes_From_Source
(P1
) then
8824 while Present
(P1
) loop
8826 -- Neither P2 nor a source statement were located during the
8827 -- search. If we reach the end of the list, then P1 does not
8828 -- occur earlier than P2.
8831 -- start --- P2 ----- P1 --- end
8833 if No
(Next
(P1
)) then
8836 -- We encounter P2 while going to the right of the list. This
8837 -- means that P1 does indeed appear earlier.
8840 -- start --- P1 ===== P2 --- end
8841 -- expanded code in between
8846 -- No need to look any further since we have located a source
8849 elsif Comes_From_Source
(P1
) then
8859 if not Comes_From_Source
(P2
) then
8860 while Present
(P2
) loop
8862 -- Neither P1 nor a source statement were located during the
8863 -- search. If we reach the start of the list, then P1 does not
8864 -- occur earlier than P2.
8867 -- start --- P2 --- P1 --- end
8869 if No
(Prev
(P2
)) then
8872 -- We encounter P1 while going to the left of the list. This
8873 -- means that P1 does indeed appear earlier.
8876 -- start --- P1 ===== P2 --- end
8877 -- expanded code in between
8882 -- No need to look any further since we have located a source
8885 elsif Comes_From_Source
(P2
) then
8895 -- At this point either both nodes came from source or we approximated
8896 -- their source locations through neighboring source statements.
8898 T1
:= Top_Level_Location
(Sloc
(P1
));
8899 T2
:= Top_Level_Location
(Sloc
(P2
));
8901 -- When two nodes come from the same instance, they have identical top
8902 -- level locations. To determine proper relation within the tree, check
8903 -- their locations within the template.
8906 return Sloc
(P1
) < Sloc
(P2
);
8908 -- The two nodes either come from unrelated instances or do not come
8909 -- from instantiated code at all.
8916 ----------------------
8917 -- Find_Actual_Type --
8918 ----------------------
8920 function Find_Actual_Type
8922 Gen_Type
: Entity_Id
) return Entity_Id
8924 Gen_Scope
: constant Entity_Id
:= Scope
(Gen_Type
);
8928 -- Special processing only applies to child units
8930 if not Is_Child_Unit
(Gen_Scope
) then
8931 return Get_Instance_Of
(Typ
);
8933 -- If designated or component type is itself a formal of the child unit,
8934 -- its instance is available.
8936 elsif Scope
(Typ
) = Gen_Scope
then
8937 return Get_Instance_Of
(Typ
);
8939 -- If the array or access type is not declared in the parent unit,
8940 -- no special processing needed.
8942 elsif not Is_Generic_Type
(Typ
)
8943 and then Scope
(Gen_Scope
) /= Scope
(Typ
)
8945 return Get_Instance_Of
(Typ
);
8947 -- Otherwise, retrieve designated or component type by visibility
8950 T
:= Current_Entity
(Typ
);
8951 while Present
(T
) loop
8952 if In_Open_Scopes
(Scope
(T
)) then
8954 elsif Is_Generic_Actual_Type
(T
) then
8963 end Find_Actual_Type
;
8965 -----------------------------
8966 -- Freeze_Package_Instance --
8967 -----------------------------
8969 procedure Freeze_Package_Instance
8975 function In_Same_Scope
(Gen_Id
, Act_Id
: Node_Id
) return Boolean;
8976 -- Check if the generic definition and the instantiation come from
8977 -- a common scope, in which case the instance must be frozen after
8978 -- the generic body.
8980 function True_Sloc
(N
, Act_Unit
: Node_Id
) return Source_Ptr
;
8981 -- If the instance is nested inside a generic unit, the Sloc of the
8982 -- instance indicates the place of the original definition, not the
8983 -- point of the current enclosing instance. Pending a better usage of
8984 -- Slocs to indicate instantiation places, we determine the place of
8985 -- origin of a node by finding the maximum sloc of any ancestor node.
8987 -- Why is this not equivalent to Top_Level_Location ???
8993 function In_Same_Scope
(Gen_Id
, Act_Id
: Node_Id
) return Boolean is
8994 Act_Scop
: Entity_Id
:= Scope
(Act_Id
);
8995 Gen_Scop
: Entity_Id
:= Scope
(Gen_Id
);
8998 while Act_Scop
/= Standard_Standard
8999 and then Gen_Scop
/= Standard_Standard
9001 if Act_Scop
= Gen_Scop
then
9005 Act_Scop
:= Scope
(Act_Scop
);
9006 Gen_Scop
:= Scope
(Gen_Scop
);
9016 function True_Sloc
(N
, Act_Unit
: Node_Id
) return Source_Ptr
is
9023 while Present
(N1
) and then N1
/= Act_Unit
loop
9024 if Sloc
(N1
) > Res
then
9036 Gen_Id
: constant Entity_Id
:= Get_Generic_Entity
(N
);
9037 Par_Id
: constant Entity_Id
:= Scope
(Gen_Id
);
9038 Act_Unit
: constant Node_Id
:= Unit
(Cunit
(Get_Source_Unit
(N
)));
9039 Gen_Unit
: constant Node_Id
:=
9040 Unit
(Cunit
(Get_Source_Unit
(Gen_Decl
)));
9042 Body_Unit
: Node_Id
;
9044 Must_Delay
: Boolean;
9045 Orig_Body
: Node_Id
;
9047 -- Start of processing for Freeze_Package_Instance
9050 -- If the body is a subunit, the freeze point is the corresponding stub
9051 -- in the current compilation, not the subunit itself.
9053 if Nkind
(Parent
(Gen_Body
)) = N_Subunit
then
9054 Orig_Body
:= Corresponding_Stub
(Parent
(Gen_Body
));
9056 Orig_Body
:= Gen_Body
;
9059 Body_Unit
:= Unit
(Cunit
(Get_Source_Unit
(Orig_Body
)));
9061 -- If the instantiation and the generic definition appear in the same
9062 -- package declaration, this is an early instantiation. If they appear
9063 -- in the same declarative part, it is an early instantiation only if
9064 -- the generic body appears textually later, and the generic body is
9065 -- also in the main unit.
9067 -- If instance is nested within a subprogram, and the generic body
9068 -- is not, the instance is delayed because the enclosing body is. If
9069 -- instance and body are within the same scope, or the same subprogram
9070 -- body, indicate explicitly that the instance is delayed.
9073 (Gen_Unit
= Act_Unit
9074 and then (Nkind
(Gen_Unit
) in N_Generic_Package_Declaration
9075 | N_Package_Declaration
9076 or else (Gen_Unit
= Body_Unit
9078 True_Sloc
(N
, Act_Unit
) < Sloc
(Orig_Body
)))
9079 and then Is_In_Main_Unit
(Original_Node
(Gen_Unit
))
9080 and then In_Same_Scope
(Gen_Id
, Act_Id
));
9082 -- If this is an early instantiation, the freeze node is placed after
9083 -- the generic body. Otherwise, if the generic appears in an instance,
9084 -- we cannot freeze the current instance until the outer one is frozen.
9085 -- This is only relevant if the current instance is nested within some
9086 -- inner scope not itself within the outer instance. If this scope is
9087 -- a package body in the same declarative part as the outer instance,
9088 -- then that body needs to be frozen after the outer instance. Finally,
9089 -- if no delay is needed, we place the freeze node at the end of the
9090 -- current declarative part.
9092 if No
(Freeze_Node
(Act_Id
))
9093 or else not Is_List_Member
(Freeze_Node
(Act_Id
))
9095 Ensure_Freeze_Node
(Act_Id
);
9096 F_Node
:= Freeze_Node
(Act_Id
);
9099 Insert_After
(Orig_Body
, F_Node
);
9101 elsif Is_Generic_Instance
(Par_Id
)
9102 and then Present
(Freeze_Node
(Par_Id
))
9103 and then Scope
(Act_Id
) /= Par_Id
9105 -- Freeze instance of inner generic after instance of enclosing
9108 if In_Same_Declarative_Part
(Parent
(Freeze_Node
(Par_Id
)), N
) then
9110 -- Handle the following case:
9112 -- package Parent_Inst is new ...
9113 -- freeze Parent_Inst []
9115 -- procedure P ... -- this body freezes Parent_Inst
9117 -- package Inst is new ...
9119 -- In this particular scenario, the freeze node for Inst must
9120 -- be inserted in the same manner as that of Parent_Inst,
9121 -- before the next source body or at the end of the declarative
9122 -- list (body not available). If body P did not exist and
9123 -- Parent_Inst was frozen after Inst, either by a body
9124 -- following Inst or at the end of the declarative region,
9125 -- the freeze node for Inst must be inserted after that of
9126 -- Parent_Inst. This relation is established by comparing
9127 -- the Slocs of Parent_Inst freeze node and Inst.
9128 -- We examine the parents of the enclosing lists to handle
9129 -- the case where the parent instance is in the visible part
9130 -- of a package declaration, and the inner instance is in
9131 -- the corresponding private part.
9133 if Parent
(List_Containing
(Freeze_Node
(Par_Id
)))
9134 = Parent
(List_Containing
(N
))
9135 and then Sloc
(Freeze_Node
(Par_Id
)) <= Sloc
(N
)
9137 Insert_Freeze_Node_For_Instance
(N
, F_Node
);
9139 Insert_After
(Freeze_Node
(Par_Id
), F_Node
);
9142 -- Freeze package enclosing instance of inner generic after
9143 -- instance of enclosing generic.
9145 elsif Nkind
(Parent
(N
)) in N_Package_Body | N_Subprogram_Body
9146 and then In_Same_Declarative_Part
9147 (Parent
(Freeze_Node
(Par_Id
)), Parent
(N
))
9150 Enclosing
: Entity_Id
;
9153 Enclosing
:= Corresponding_Spec
(Parent
(N
));
9155 if No
(Enclosing
) then
9156 Enclosing
:= Defining_Entity
(Parent
(N
));
9159 Insert_Freeze_Node_For_Instance
(N
, F_Node
);
9160 Ensure_Freeze_Node
(Enclosing
);
9162 if not Is_List_Member
(Freeze_Node
(Enclosing
)) then
9164 -- The enclosing context is a subunit, insert the freeze
9165 -- node after the stub.
9167 if Nkind
(Parent
(Parent
(N
))) = N_Subunit
then
9168 Insert_Freeze_Node_For_Instance
9169 (Corresponding_Stub
(Parent
(Parent
(N
))),
9170 Freeze_Node
(Enclosing
));
9172 -- The enclosing context is a package with a stub body
9173 -- which has already been replaced by the real body.
9174 -- Insert the freeze node after the actual body.
9176 elsif Ekind
(Enclosing
) = E_Package
9177 and then Present
(Body_Entity
(Enclosing
))
9178 and then Was_Originally_Stub
9179 (Parent
(Body_Entity
(Enclosing
)))
9181 Insert_Freeze_Node_For_Instance
9182 (Parent
(Body_Entity
(Enclosing
)),
9183 Freeze_Node
(Enclosing
));
9185 -- The parent instance has been frozen before the body of
9186 -- the enclosing package, insert the freeze node after
9189 elsif In_Same_List
(Freeze_Node
(Par_Id
), Parent
(N
))
9191 Sloc
(Freeze_Node
(Par_Id
)) <= Sloc
(Parent
(N
))
9193 Insert_Freeze_Node_For_Instance
9194 (Parent
(N
), Freeze_Node
(Enclosing
));
9198 (Freeze_Node
(Par_Id
), Freeze_Node
(Enclosing
));
9204 Insert_Freeze_Node_For_Instance
(N
, F_Node
);
9208 Insert_Freeze_Node_For_Instance
(N
, F_Node
);
9211 end Freeze_Package_Instance
;
9213 --------------------------------
9214 -- Freeze_Subprogram_Instance --
9215 --------------------------------
9217 procedure Freeze_Subprogram_Instance
9220 Pack_Id
: Entity_Id
)
9222 function Enclosing_Package_Body
(N
: Node_Id
) return Node_Id
;
9223 -- Find innermost package body that encloses the given node, and which
9224 -- is not a compilation unit. Freeze nodes for the instance, or for its
9225 -- enclosing body, may be inserted after the enclosing_body of the
9226 -- generic unit. Used to determine proper placement of freeze node for
9227 -- both package and subprogram instances.
9229 function Package_Freeze_Node
(B
: Node_Id
) return Node_Id
;
9230 -- Find entity for given package body, and locate or create a freeze
9233 ----------------------------
9234 -- Enclosing_Package_Body --
9235 ----------------------------
9237 function Enclosing_Package_Body
(N
: Node_Id
) return Node_Id
is
9243 and then Nkind
(Parent
(P
)) /= N_Compilation_Unit
9245 if Nkind
(P
) = N_Package_Body
then
9246 if Nkind
(Parent
(P
)) = N_Subunit
then
9247 return Corresponding_Stub
(Parent
(P
));
9253 P
:= True_Parent
(P
);
9257 end Enclosing_Package_Body
;
9259 -------------------------
9260 -- Package_Freeze_Node --
9261 -------------------------
9263 function Package_Freeze_Node
(B
: Node_Id
) return Node_Id
is
9267 if Nkind
(B
) = N_Package_Body
then
9268 Id
:= Corresponding_Spec
(B
);
9269 else pragma Assert
(Nkind
(B
) = N_Package_Body_Stub
);
9270 Id
:= Corresponding_Spec
(Proper_Body
(Unit
(Library_Unit
(B
))));
9273 Ensure_Freeze_Node
(Id
);
9274 return Freeze_Node
(Id
);
9275 end Package_Freeze_Node
;
9279 Enc_G
: constant Node_Id
:= Enclosing_Package_Body
(Gen_Body
);
9280 Enc_N
: constant Node_Id
:= Enclosing_Package_Body
(N
);
9281 Par_Id
: constant Entity_Id
:= Scope
(Get_Generic_Entity
(N
));
9286 -- Start of processing for Freeze_Subprogram_Instance
9289 -- If the instance and the generic body appear within the same unit, and
9290 -- the instance precedes the generic, the freeze node for the instance
9291 -- must appear after that of the generic. If the generic is nested
9292 -- within another instance I2, then current instance must be frozen
9293 -- after I2. In both cases, the freeze nodes are those of enclosing
9294 -- packages. Otherwise, the freeze node is placed at the end of the
9295 -- current declarative part.
9297 Ensure_Freeze_Node
(Pack_Id
);
9298 F_Node
:= Freeze_Node
(Pack_Id
);
9300 if Is_Generic_Instance
(Par_Id
)
9301 and then Present
(Freeze_Node
(Par_Id
))
9302 and then In_Same_Declarative_Part
(Parent
(Freeze_Node
(Par_Id
)), N
)
9304 -- The parent was a premature instantiation. Insert freeze node at
9305 -- the end the current declarative part.
9307 if Is_Known_Guaranteed_ABE
(Get_Unit_Instantiation_Node
(Par_Id
)) then
9308 Insert_Freeze_Node_For_Instance
(N
, F_Node
);
9310 -- Handle the following case:
9312 -- package Parent_Inst is new ...
9313 -- freeze Parent_Inst []
9315 -- procedure P ... -- this body freezes Parent_Inst
9317 -- procedure Inst is new ...
9319 -- In this particular scenario, the freeze node for Inst must be
9320 -- inserted in the same manner as that of Parent_Inst - before the
9321 -- next source body or at the end of the declarative list (body not
9322 -- available). If body P did not exist and Parent_Inst was frozen
9323 -- after Inst, either by a body following Inst or at the end of the
9324 -- declarative region, the freeze node for Inst must be inserted
9325 -- after that of Parent_Inst. This relation is established by
9326 -- comparing the Slocs of Parent_Inst freeze node and Inst.
9328 elsif In_Same_List
(Freeze_Node
(Par_Id
), N
)
9329 and then Sloc
(Freeze_Node
(Par_Id
)) <= Sloc
(N
)
9331 Insert_Freeze_Node_For_Instance
(N
, F_Node
);
9334 Insert_After
(Freeze_Node
(Par_Id
), F_Node
);
9337 -- The body enclosing the instance should be frozen after the body that
9338 -- includes the generic, because the body of the instance may make
9339 -- references to entities therein. If the two are not in the same
9340 -- declarative part, or if the one enclosing the instance is frozen
9341 -- already, freeze the instance at the end of the current declarative
9344 elsif Is_Generic_Instance
(Par_Id
)
9345 and then Present
(Freeze_Node
(Par_Id
))
9346 and then Present
(Enc_N
)
9348 if In_Same_Declarative_Part
(Parent
(Freeze_Node
(Par_Id
)), Enc_N
)
9350 -- The enclosing package may contain several instances. Rather
9351 -- than computing the earliest point at which to insert its freeze
9352 -- node, we place it at the end of the declarative part of the
9353 -- parent of the generic.
9355 Insert_Freeze_Node_For_Instance
9356 (Freeze_Node
(Par_Id
), Package_Freeze_Node
(Enc_N
));
9359 Insert_Freeze_Node_For_Instance
(N
, F_Node
);
9361 elsif Present
(Enc_G
)
9362 and then Present
(Enc_N
)
9363 and then Enc_G
/= Enc_N
9364 and then Earlier
(N
, Gen_Body
)
9366 -- Freeze package that encloses instance, and place node after the
9367 -- package that encloses generic. If enclosing package is already
9368 -- frozen we have to assume it is at the proper place. This may be a
9369 -- potential ABE that requires dynamic checking. Do not add a freeze
9370 -- node if the package that encloses the generic is inside the body
9371 -- that encloses the instance, because the freeze node would be in
9372 -- the wrong scope. Additional contortions needed if the bodies are
9373 -- within a subunit.
9376 Enclosing_Body
: Node_Id
;
9379 if Nkind
(Enc_N
) = N_Package_Body_Stub
then
9380 Enclosing_Body
:= Proper_Body
(Unit
(Library_Unit
(Enc_N
)));
9382 Enclosing_Body
:= Enc_N
;
9385 if Parent
(List_Containing
(Enc_G
)) /= Enclosing_Body
then
9386 Insert_Freeze_Node_For_Instance
9387 (Enc_G
, Package_Freeze_Node
(Enc_N
));
9391 -- Freeze enclosing subunit before instance
9393 Enc_G_F
:= Package_Freeze_Node
(Enc_G
);
9395 if not Is_List_Member
(Enc_G_F
) then
9396 Insert_After
(Enc_G
, Enc_G_F
);
9399 Insert_Freeze_Node_For_Instance
(N
, F_Node
);
9402 -- If none of the above, insert freeze node at the end of the current
9403 -- declarative part.
9405 Insert_Freeze_Node_For_Instance
(N
, F_Node
);
9407 end Freeze_Subprogram_Instance
;
9413 function Get_Gen_Id
(E
: Assoc_Ptr
) return Entity_Id
is
9415 return Generic_Renamings
.Table
(E
).Gen_Id
;
9418 ---------------------
9419 -- Get_Instance_Of --
9420 ---------------------
9422 function Get_Instance_Of
(A
: Entity_Id
) return Entity_Id
is
9423 Res
: constant Assoc_Ptr
:= Generic_Renamings_HTable
.Get
(A
);
9426 if Res
/= Assoc_Null
then
9427 return Generic_Renamings
.Table
(Res
).Act_Id
;
9430 -- On exit, entity is not instantiated: not a generic parameter, or
9431 -- else parameter of an inner generic unit.
9435 end Get_Instance_Of
;
9437 ---------------------------------
9438 -- Get_Unit_Instantiation_Node --
9439 ---------------------------------
9441 function Get_Unit_Instantiation_Node
(A
: Entity_Id
) return Node_Id
is
9442 Decl
: Node_Id
:= Unit_Declaration_Node
(A
);
9446 -- If the Package_Instantiation attribute has been set on the package
9447 -- entity, then use it directly when it (or its Original_Node) refers
9448 -- to an N_Package_Instantiation node. In principle it should be
9449 -- possible to have this field set in all cases, which should be
9450 -- investigated, and would allow this function to be significantly
9453 Inst
:= Package_Instantiation
(A
);
9455 if Present
(Inst
) then
9456 if Nkind
(Inst
) = N_Package_Instantiation
then
9459 elsif Nkind
(Original_Node
(Inst
)) = N_Package_Instantiation
then
9460 return Original_Node
(Inst
);
9464 -- If the instantiation is a compilation unit that does not need body
9465 -- then the instantiation node has been rewritten as a package
9466 -- declaration for the instance, and we return the original node.
9468 -- If it is a compilation unit and the instance node has not been
9469 -- rewritten, then it is still the unit of the compilation. Finally, if
9470 -- a body is present, this is a parent of the main unit whose body has
9471 -- been compiled for inlining purposes, and the instantiation node has
9472 -- been rewritten with the instance body.
9474 -- Otherwise the instantiation node appears after the declaration. If
9475 -- the entity is a formal package, the declaration may have been
9476 -- rewritten as a generic declaration (in the case of a formal with box)
9477 -- or left as a formal package declaration if it has actuals, and is
9478 -- found with a forward search.
9480 if Nkind
(Parent
(Decl
)) = N_Compilation_Unit
then
9481 if Nkind
(Decl
) = N_Package_Declaration
9482 and then Present
(Corresponding_Body
(Decl
))
9484 Decl
:= Unit_Declaration_Node
(Corresponding_Body
(Decl
));
9487 if Nkind
(Original_Node
(Decl
)) in N_Generic_Instantiation
then
9488 return Original_Node
(Decl
);
9490 return Unit
(Parent
(Decl
));
9493 elsif Nkind
(Decl
) = N_Package_Declaration
9494 and then Nkind
(Original_Node
(Decl
)) = N_Formal_Package_Declaration
9496 return Original_Node
(Decl
);
9499 Inst
:= Next
(Decl
);
9500 while Nkind
(Inst
) not in N_Formal_Package_Declaration
9501 | N_Function_Instantiation
9502 | N_Package_Instantiation
9503 | N_Procedure_Instantiation
9510 end Get_Unit_Instantiation_Node
;
9512 ------------------------
9513 -- Has_Been_Exchanged --
9514 ------------------------
9516 function Has_Been_Exchanged
(E
: Entity_Id
) return Boolean is
9520 Next
:= First_Elmt
(Exchanged_Views
);
9521 while Present
(Next
) loop
9522 if Full_View
(Node
(Next
)) = E
then
9530 end Has_Been_Exchanged
;
9536 function Has_Contracts
(Decl
: Node_Id
) return Boolean is
9537 A_List
: constant List_Id
:= Aspect_Specifications
(Decl
);
9544 A_Spec
:= First
(A_List
);
9545 while Present
(A_Spec
) loop
9546 A_Id
:= Get_Aspect_Id
(A_Spec
);
9547 if A_Id
= Aspect_Pre
or else A_Id
= Aspect_Post
then
9562 function Hash
(F
: Entity_Id
) return HTable_Range
is
9564 return HTable_Range
(F
mod HTable_Size
);
9567 ------------------------
9568 -- Hide_Current_Scope --
9569 ------------------------
9571 procedure Hide_Current_Scope
is
9572 C
: constant Entity_Id
:= Current_Scope
;
9576 Set_Is_Hidden_Open_Scope
(C
);
9578 E
:= First_Entity
(C
);
9579 while Present
(E
) loop
9580 if Is_Immediately_Visible
(E
) then
9581 Set_Is_Immediately_Visible
(E
, False);
9582 Append_Elmt
(E
, Hidden_Entities
);
9588 -- Make the scope name invisible as well. This is necessary, but might
9589 -- conflict with calls to Rtsfind later on, in case the scope is a
9590 -- predefined one. There is no clean solution to this problem, so for
9591 -- now we depend on the user not redefining Standard itself in one of
9592 -- the parent units.
9594 if Is_Immediately_Visible
(C
) and then C
/= Standard_Standard
then
9595 Set_Is_Immediately_Visible
(C
, False);
9596 Append_Elmt
(C
, Hidden_Entities
);
9599 end Hide_Current_Scope
;
9605 procedure Init_Env
is
9606 Saved
: Instance_Env
;
9609 Saved
.Instantiated_Parent
:= Current_Instantiated_Parent
;
9610 Saved
.Exchanged_Views
:= Exchanged_Views
;
9611 Saved
.Hidden_Entities
:= Hidden_Entities
;
9612 Saved
.Current_Sem_Unit
:= Current_Sem_Unit
;
9613 Saved
.Parent_Unit_Visible
:= Parent_Unit_Visible
;
9614 Saved
.Instance_Parent_Unit
:= Instance_Parent_Unit
;
9616 -- Save configuration switches. These may be reset if the unit is a
9617 -- predefined unit, and the current mode is not Ada 2005.
9619 Saved
.Switches
:= Save_Config_Switches
;
9621 Instance_Envs
.Append
(Saved
);
9623 Exchanged_Views
:= New_Elmt_List
;
9624 Hidden_Entities
:= New_Elmt_List
;
9626 -- Make dummy entry for Instantiated parent. If generic unit is legal,
9627 -- this is set properly in Set_Instance_Env.
9629 Current_Instantiated_Parent
:=
9630 (Current_Scope
, Current_Scope
, Assoc_Null
);
9633 ---------------------
9634 -- In_Main_Context --
9635 ---------------------
9637 function In_Main_Context
(E
: Entity_Id
) return Boolean is
9643 if not Is_Compilation_Unit
(E
)
9644 or else Ekind
(E
) /= E_Package
9645 or else In_Private_Part
(E
)
9650 Context
:= Context_Items
(Cunit
(Main_Unit
));
9652 Clause
:= First
(Context
);
9653 while Present
(Clause
) loop
9654 if Nkind
(Clause
) = N_With_Clause
then
9655 Nam
:= Name
(Clause
);
9657 -- If the current scope is part of the context of the main unit,
9658 -- analysis of the corresponding with_clause is not complete, and
9659 -- the entity is not set. We use the Chars field directly, which
9660 -- might produce false positives in rare cases, but guarantees
9661 -- that we produce all the instance bodies we will need.
9663 if (Is_Entity_Name
(Nam
) and then Chars
(Nam
) = Chars
(E
))
9664 or else (Nkind
(Nam
) = N_Selected_Component
9665 and then Chars
(Selector_Name
(Nam
)) = Chars
(E
))
9675 end In_Main_Context
;
9677 ---------------------
9678 -- Inherit_Context --
9679 ---------------------
9681 procedure Inherit_Context
(Gen_Decl
: Node_Id
; Inst
: Node_Id
) is
9682 Current_Context
: List_Id
;
9683 Current_Unit
: Node_Id
;
9692 if Nkind
(Parent
(Gen_Decl
)) = N_Compilation_Unit
then
9694 -- The inherited context is attached to the enclosing compilation
9695 -- unit. This is either the main unit, or the declaration for the
9696 -- main unit (in case the instantiation appears within the package
9697 -- declaration and the main unit is its body).
9699 Current_Unit
:= Parent
(Inst
);
9700 while Present
(Current_Unit
)
9701 and then Nkind
(Current_Unit
) /= N_Compilation_Unit
9703 Current_Unit
:= Parent
(Current_Unit
);
9706 Current_Context
:= Context_Items
(Current_Unit
);
9708 Item
:= First
(Context_Items
(Parent
(Gen_Decl
)));
9709 while Present
(Item
) loop
9710 if Nkind
(Item
) = N_With_Clause
then
9711 Lib_Unit
:= Library_Unit
(Item
);
9713 -- Take care to prevent direct cyclic with's
9715 if Lib_Unit
/= Current_Unit
then
9717 -- Do not add a unit if it is already in the context
9719 Clause
:= First
(Current_Context
);
9721 while Present
(Clause
) loop
9722 if Nkind
(Clause
) = N_With_Clause
9723 and then Library_Unit
(Clause
) = Lib_Unit
9733 New_I
:= New_Copy
(Item
);
9734 Set_Implicit_With
(New_I
);
9736 Append
(New_I
, Current_Context
);
9744 end Inherit_Context
;
9750 procedure Initialize
is
9752 Generic_Renamings
.Init
;
9755 Generic_Renamings_HTable
.Reset
;
9756 Circularity_Detected
:= False;
9757 Exchanged_Views
:= No_Elist
;
9758 Hidden_Entities
:= No_Elist
;
9761 -------------------------------------
9762 -- Insert_Freeze_Node_For_Instance --
9763 -------------------------------------
9765 procedure Insert_Freeze_Node_For_Instance
9769 function Enclosing_Body
(N
: Node_Id
) return Node_Id
;
9770 -- Find enclosing package or subprogram body, if any. Freeze node may
9771 -- be placed at end of current declarative list if previous instance
9772 -- and current one have different enclosing bodies.
9774 function Previous_Instance
(Gen
: Entity_Id
) return Entity_Id
;
9775 -- Find the local instance, if any, that declares the generic that is
9776 -- being instantiated. If present, the freeze node for this instance
9777 -- must follow the freeze node for the previous instance.
9779 --------------------
9780 -- Enclosing_Body --
9781 --------------------
9783 function Enclosing_Body
(N
: Node_Id
) return Node_Id
is
9789 and then Nkind
(Parent
(P
)) /= N_Compilation_Unit
9791 if Nkind
(P
) in N_Package_Body | N_Subprogram_Body
then
9792 if Nkind
(Parent
(P
)) = N_Subunit
then
9793 return Corresponding_Stub
(Parent
(P
));
9799 P
:= True_Parent
(P
);
9805 -----------------------
9806 -- Previous_Instance --
9807 -----------------------
9809 function Previous_Instance
(Gen
: Entity_Id
) return Entity_Id
is
9814 while Present
(S
) and then S
/= Standard_Standard
loop
9815 if Is_Generic_Instance
(S
)
9816 and then In_Same_Source_Unit
(S
, N
)
9825 end Previous_Instance
;
9836 -- Start of processing for Insert_Freeze_Node_For_Instance
9839 -- Nothing to do if the freeze node has already been inserted
9841 if Is_List_Member
(F_Node
) then
9845 Inst
:= Entity
(F_Node
);
9847 -- When processing a subprogram instantiation, utilize the actual
9848 -- subprogram instantiation rather than its package wrapper as it
9849 -- carries all the context information.
9851 if Is_Wrapper_Package
(Inst
) then
9852 Inst
:= Related_Instance
(Inst
);
9855 Par_Inst
:= Parent
(Inst
);
9857 -- If this is a package instance, check whether the generic is declared
9858 -- in a previous instance and the current instance is not within the
9861 if Present
(Generic_Parent
(Par_Inst
)) and then Is_In_Main_Unit
(N
) then
9863 Enclosing_N
: constant Node_Id
:= Enclosing_Body
(N
);
9864 Par_I
: constant Entity_Id
:=
9865 Previous_Instance
(Generic_Parent
(Par_Inst
));
9869 if Present
(Par_I
) and then Earlier
(N
, Freeze_Node
(Par_I
)) then
9870 Scop
:= Scope
(Inst
);
9872 -- If the current instance is within the one that contains
9873 -- the generic, the freeze node for the current one must
9874 -- appear in the current declarative part. Ditto, if the
9875 -- current instance is within another package instance or
9876 -- within a body that does not enclose the current instance.
9877 -- In these three cases the freeze node of the previous
9878 -- instance is not relevant.
9880 while Present
(Scop
) and then Scop
/= Standard_Standard
loop
9881 exit when Scop
= Par_I
9883 (Is_Generic_Instance
(Scop
)
9884 and then Scope_Depth
(Scop
) > Scope_Depth
(Par_I
));
9885 Scop
:= Scope
(Scop
);
9888 -- Previous instance encloses current instance
9890 if Scop
= Par_I
then
9893 -- If the next node is a source body we must freeze in the
9894 -- current scope as well.
9896 elsif Present
(Next
(N
))
9897 and then Nkind
(Next
(N
)) in N_Subprogram_Body
9899 and then Comes_From_Source
(Next
(N
))
9903 -- Current instance is within an unrelated instance
9905 elsif Is_Generic_Instance
(Scop
) then
9908 -- Current instance is within an unrelated body
9910 elsif Present
(Enclosing_N
)
9911 and then Enclosing_N
/= Enclosing_Body
(Par_I
)
9916 Insert_After
(Freeze_Node
(Par_I
), F_Node
);
9924 Decls
:= List_Containing
(N
);
9925 Par_N
:= Parent
(Decls
);
9928 -- Determine the proper freeze point of an instantiation
9930 if Is_Generic_Instance
(Inst
) then
9932 -- When the instantiation occurs in a package spec, append the
9933 -- freeze node to the private declarations (if any).
9935 if Nkind
(Par_N
) = N_Package_Specification
9936 and then Decls
= Visible_Declarations
(Par_N
)
9937 and then not Is_Empty_List
(Private_Declarations
(Par_N
))
9939 Decls
:= Private_Declarations
(Par_N
);
9940 Decl
:= First
(Decls
);
9943 -- We adhere to the general rule of a package or subprogram body
9944 -- causing freezing of anything before it in the same declarative
9945 -- region. In this respect, the proper freeze point of a package
9946 -- instantiation is before the first source body which follows, or
9947 -- before a stub. This ensures that entities from the instance are
9948 -- already frozen and therefore usable in source bodies.
9950 if Nkind
(Par_N
) /= N_Package_Declaration
9952 not In_Same_Source_Unit
(Generic_Parent
(Par_Inst
), Inst
)
9954 while Present
(Decl
) loop
9955 if ((Nkind
(Decl
) in N_Unit_Body
9957 Nkind
(Decl
) in N_Body_Stub
)
9958 and then Comes_From_Source
(Decl
))
9959 or else (Present
(Origin
)
9960 and then Nkind
(Decl
) in N_Generic_Instantiation
9961 and then Instance_Spec
(Decl
) /= Origin
)
9963 Set_Sloc
(F_Node
, Sloc
(Decl
));
9964 Insert_Before
(Decl
, F_Node
);
9972 -- When the instantiation occurs in a package spec and there is
9973 -- no source body which follows, and the package has a body but
9974 -- is delayed, then insert immediately before its freeze node.
9976 if Nkind
(Par_N
) = N_Package_Specification
9977 and then Present
(Corresponding_Body
(Parent
(Par_N
)))
9978 and then Present
(Freeze_Node
(Defining_Entity
(Par_N
)))
9980 Set_Sloc
(F_Node
, Sloc
(Freeze_Node
(Defining_Entity
(Par_N
))));
9981 Insert_Before
(Freeze_Node
(Defining_Entity
(Par_N
)), F_Node
);
9984 -- When the instantiation occurs in a package spec and there is
9985 -- no source body which follows, not even of the package itself,
9986 -- then insert into the declaration list of the outer level, but
9987 -- do not jump over following instantiations in this list because
9988 -- they may have a body that has not materialized yet, see above.
9990 elsif Nkind
(Par_N
) = N_Package_Specification
9991 and then No
(Corresponding_Body
(Parent
(Par_N
)))
9992 and then Is_List_Member
(Parent
(Par_N
))
9994 Decl
:= Parent
(Par_N
);
9995 Decls
:= List_Containing
(Decl
);
9996 Par_N
:= Parent
(Decls
);
9999 -- In a package declaration, or if no source body which follows
10000 -- and at library level, then insert at end of list.
10008 -- Insert and adjust the Sloc of the freeze node
10010 Set_Sloc
(F_Node
, Sloc
(Last
(Decls
)));
10011 Insert_After
(Last
(Decls
), F_Node
);
10012 end Insert_Freeze_Node_For_Instance
;
10014 -----------------------------
10015 -- Install_Formal_Packages --
10016 -----------------------------
10018 procedure Install_Formal_Packages
(Par
: Entity_Id
) is
10021 Gen_E
: Entity_Id
:= Empty
;
10024 E
:= First_Entity
(Par
);
10026 -- If we are installing an instance parent, locate the formal packages
10027 -- of its generic parent.
10029 if Is_Generic_Instance
(Par
) then
10030 Gen
:= Generic_Parent
(Package_Specification
(Par
));
10031 Gen_E
:= First_Entity
(Gen
);
10034 while Present
(E
) loop
10035 if Ekind
(E
) = E_Package
10036 and then Nkind
(Parent
(E
)) = N_Package_Renaming_Declaration
10038 -- If this is the renaming for the parent instance, done
10040 if Renamed_Entity
(E
) = Par
then
10043 -- The visibility of a formal of an enclosing generic is already
10046 elsif Denotes_Formal_Package
(E
) then
10049 elsif Present
(Associated_Formal_Package
(E
)) then
10050 Check_Generic_Actuals
(Renamed_Entity
(E
), True);
10051 Set_Is_Hidden
(E
, False);
10053 -- Find formal package in generic unit that corresponds to
10054 -- (instance of) formal package in instance.
10056 while Present
(Gen_E
) and then Chars
(Gen_E
) /= Chars
(E
) loop
10057 Next_Entity
(Gen_E
);
10060 if Present
(Gen_E
) then
10061 Map_Formal_Package_Entities
(Gen_E
, E
);
10068 if Present
(Gen_E
) then
10069 Next_Entity
(Gen_E
);
10072 end Install_Formal_Packages
;
10074 --------------------
10075 -- Install_Parent --
10076 --------------------
10078 procedure Install_Parent
(P
: Entity_Id
; In_Body
: Boolean := False) is
10079 Ancestors
: constant Elist_Id
:= New_Elmt_List
;
10080 S
: constant Entity_Id
:= Current_Scope
;
10081 Inst_Par
: Entity_Id
;
10082 First_Par
: Entity_Id
;
10083 Inst_Node
: Node_Id
;
10084 Gen_Par
: Entity_Id
;
10085 First_Gen
: Entity_Id
;
10088 procedure Install_Noninstance_Specs
(Par
: Entity_Id
);
10089 -- Install the scopes of noninstance parent units ending with Par
10091 procedure Install_Spec
(Par
: Entity_Id
);
10092 -- The child unit is within the declarative part of the parent, so the
10093 -- declarations within the parent are immediately visible.
10095 -------------------------------
10096 -- Install_Noninstance_Specs --
10097 -------------------------------
10099 procedure Install_Noninstance_Specs
(Par
: Entity_Id
) is
10102 and then Par
/= Standard_Standard
10103 and then not In_Open_Scopes
(Par
)
10105 Install_Noninstance_Specs
(Scope
(Par
));
10106 Install_Spec
(Par
);
10108 end Install_Noninstance_Specs
;
10114 procedure Install_Spec
(Par
: Entity_Id
) is
10115 Spec
: constant Node_Id
:= Package_Specification
(Par
);
10118 -- If this parent of the child instance is a top-level unit,
10119 -- then record the unit and its visibility for later resetting in
10120 -- Remove_Parent. We exclude units that are generic instances, as we
10121 -- only want to record this information for the ultimate top-level
10122 -- noninstance parent (is that always correct???).
10124 if Scope
(Par
) = Standard_Standard
10125 and then not Is_Generic_Instance
(Par
)
10127 Parent_Unit_Visible
:= Is_Immediately_Visible
(Par
);
10128 Instance_Parent_Unit
:= Par
;
10131 -- Open the parent scope and make it and its declarations visible.
10132 -- If this point is not within a body, then only the visible
10133 -- declarations should be made visible, and installation of the
10134 -- private declarations is deferred until the appropriate point
10135 -- within analysis of the spec being instantiated (see the handling
10136 -- of parent visibility in Analyze_Package_Specification). This is
10137 -- relaxed in the case where the parent unit is Ada.Tags, to avoid
10138 -- private view problems that occur when compiling instantiations of
10139 -- a generic child of that package (Generic_Dispatching_Constructor).
10140 -- If the instance freezes a tagged type, inlinings of operations
10141 -- from Ada.Tags may need the full view of type Tag. If inlining took
10142 -- proper account of establishing visibility of inlined subprograms'
10143 -- parents then it should be possible to remove this
10144 -- special check. ???
10147 Set_Is_Immediately_Visible
(Par
);
10148 Install_Visible_Declarations
(Par
);
10149 Set_Use
(Visible_Declarations
(Spec
));
10151 if In_Body
or else Is_RTU
(Par
, Ada_Tags
) then
10152 Install_Private_Declarations
(Par
);
10153 Set_Use
(Private_Declarations
(Spec
));
10157 -- Start of processing for Install_Parent
10160 -- We need to install the parent instance to compile the instantiation
10161 -- of the child, but the child instance must appear in the current
10162 -- scope. Given that we cannot place the parent above the current scope
10163 -- in the scope stack, we duplicate the current scope and unstack both
10164 -- after the instantiation is complete.
10166 -- If the parent is itself the instantiation of a child unit, we must
10167 -- also stack the instantiation of its parent, and so on. Each such
10168 -- ancestor is the prefix of the name in a prior instantiation.
10170 -- If this is a nested instance, the parent unit itself resolves to
10171 -- a renaming of the parent instance, whose declaration we need.
10173 -- Finally, the parent may be a generic (not an instance) when the
10174 -- child unit appears as a formal package.
10178 if Present
(Renamed_Entity
(Inst_Par
)) then
10179 Inst_Par
:= Renamed_Entity
(Inst_Par
);
10182 First_Par
:= Inst_Par
;
10184 Gen_Par
:= Generic_Parent
(Package_Specification
(Inst_Par
));
10186 First_Gen
:= Gen_Par
;
10188 while Present
(Gen_Par
) and then Is_Child_Unit
(Gen_Par
) loop
10190 -- Load grandparent instance as well
10192 Inst_Node
:= Get_Unit_Instantiation_Node
(Inst_Par
);
10194 if Nkind
(Name
(Inst_Node
)) = N_Expanded_Name
then
10195 Inst_Par
:= Entity
(Prefix
(Name
(Inst_Node
)));
10197 if Present
(Renamed_Entity
(Inst_Par
)) then
10198 Inst_Par
:= Renamed_Entity
(Inst_Par
);
10201 Gen_Par
:= Generic_Parent
(Package_Specification
(Inst_Par
));
10203 if Present
(Gen_Par
) then
10204 Prepend_Elmt
(Inst_Par
, Ancestors
);
10207 -- Parent is not the name of an instantiation
10209 Install_Noninstance_Specs
(Inst_Par
);
10220 if Present
(First_Gen
) then
10221 Append_Elmt
(First_Par
, Ancestors
);
10223 Install_Noninstance_Specs
(First_Par
);
10226 if not Is_Empty_Elmt_List
(Ancestors
) then
10227 Elmt
:= First_Elmt
(Ancestors
);
10228 while Present
(Elmt
) loop
10229 Install_Spec
(Node
(Elmt
));
10230 Install_Formal_Packages
(Node
(Elmt
));
10235 if not In_Body
then
10238 end Install_Parent
;
10240 -------------------------------
10241 -- Install_Hidden_Primitives --
10242 -------------------------------
10244 procedure Install_Hidden_Primitives
10245 (Prims_List
: in out Elist_Id
;
10250 List
: Elist_Id
:= No_Elist
;
10251 Prim_G_Elmt
: Elmt_Id
;
10252 Prim_A_Elmt
: Elmt_Id
;
10257 -- No action needed in case of serious errors because we cannot trust
10258 -- in the order of primitives
10260 if Serious_Errors_Detected
> 0 then
10263 -- No action possible if we don't have available the list of primitive
10267 or else not Is_Record_Type
(Gen_T
)
10268 or else not Is_Tagged_Type
(Gen_T
)
10269 or else not Is_Record_Type
(Act_T
)
10270 or else not Is_Tagged_Type
(Act_T
)
10274 -- There is no need to handle interface types since their primitives
10275 -- cannot be hidden
10277 elsif Is_Interface
(Gen_T
) then
10281 Prim_G_Elmt
:= First_Elmt
(Primitive_Operations
(Gen_T
));
10283 if not Is_Class_Wide_Type
(Act_T
) then
10284 Prim_A_Elmt
:= First_Elmt
(Primitive_Operations
(Act_T
));
10286 Prim_A_Elmt
:= First_Elmt
(Primitive_Operations
(Root_Type
(Act_T
)));
10290 -- Skip predefined primitives in the generic formal
10292 while Present
(Prim_G_Elmt
)
10293 and then Is_Predefined_Dispatching_Operation
(Node
(Prim_G_Elmt
))
10295 Next_Elmt
(Prim_G_Elmt
);
10298 -- Skip predefined primitives in the generic actual
10300 while Present
(Prim_A_Elmt
)
10301 and then Is_Predefined_Dispatching_Operation
(Node
(Prim_A_Elmt
))
10303 Next_Elmt
(Prim_A_Elmt
);
10306 exit when No
(Prim_G_Elmt
) or else No
(Prim_A_Elmt
);
10308 Prim_G
:= Node
(Prim_G_Elmt
);
10309 Prim_A
:= Node
(Prim_A_Elmt
);
10311 -- There is no need to handle interface primitives because their
10312 -- primitives are not hidden
10314 exit when Present
(Interface_Alias
(Prim_G
));
10316 -- Here we install one hidden primitive
10318 if Chars
(Prim_G
) /= Chars
(Prim_A
)
10319 and then Has_Suffix
(Prim_A
, 'P')
10320 and then Remove_Suffix
(Prim_A
, 'P') = Chars
(Prim_G
)
10322 Set_Chars
(Prim_A
, Chars
(Prim_G
));
10323 Append_New_Elmt
(Prim_A
, To
=> List
);
10326 Next_Elmt
(Prim_A_Elmt
);
10327 Next_Elmt
(Prim_G_Elmt
);
10330 -- Append the elements to the list of temporarily visible primitives
10331 -- avoiding duplicates.
10333 if Present
(List
) then
10334 if No
(Prims_List
) then
10335 Prims_List
:= New_Elmt_List
;
10338 Elmt
:= First_Elmt
(List
);
10339 while Present
(Elmt
) loop
10340 Append_Unique_Elmt
(Node
(Elmt
), Prims_List
);
10344 end Install_Hidden_Primitives
;
10346 -------------------------------
10347 -- Restore_Hidden_Primitives --
10348 -------------------------------
10350 procedure Restore_Hidden_Primitives
(Prims_List
: in out Elist_Id
) is
10351 Prim_Elmt
: Elmt_Id
;
10355 if Present
(Prims_List
) then
10356 Prim_Elmt
:= First_Elmt
(Prims_List
);
10357 while Present
(Prim_Elmt
) loop
10358 Prim
:= Node
(Prim_Elmt
);
10359 Set_Chars
(Prim
, Add_Suffix
(Prim
, 'P'));
10360 Next_Elmt
(Prim_Elmt
);
10363 Prims_List
:= No_Elist
;
10365 end Restore_Hidden_Primitives
;
10367 --------------------------------
10368 -- Instantiate_Formal_Package --
10369 --------------------------------
10371 function Instantiate_Formal_Package
10374 Analyzed_Formal
: Node_Id
) return List_Id
10376 Loc
: constant Source_Ptr
:= Sloc
(Actual
);
10377 Hidden_Formals
: constant Elist_Id
:= New_Elmt_List
;
10379 Actual_Pack
: Entity_Id
;
10380 Formal_Pack
: Entity_Id
;
10381 Gen_Parent
: Entity_Id
;
10384 Parent_Spec
: Node_Id
;
10386 procedure Find_Matching_Actual
10388 Act
: in out Entity_Id
);
10389 -- We need to associate each formal entity in the formal package with
10390 -- the corresponding entity in the actual package. The actual package
10391 -- has been analyzed and possibly expanded, and as a result there is
10392 -- no one-to-one correspondence between the two lists (for example,
10393 -- the actual may include subtypes, itypes, and inherited primitive
10394 -- operations, interspersed among the renaming declarations for the
10395 -- actuals). We retrieve the corresponding actual by name because each
10396 -- actual has the same name as the formal, and they do appear in the
10399 function Get_Formal_Entity
(N
: Node_Id
) return Entity_Id
;
10400 -- Retrieve entity of defining entity of generic formal parameter.
10401 -- Only the declarations of formals need to be considered when
10402 -- linking them to actuals, but the declarative list may include
10403 -- internal entities generated during analysis, and those are ignored.
10405 procedure Match_Formal_Entity
10406 (Formal_Node
: Node_Id
;
10407 Formal_Ent
: Entity_Id
;
10408 Actual_Ent
: Entity_Id
);
10409 -- Associates the formal entity with the actual. In the case where
10410 -- Formal_Ent is a formal package, this procedure iterates through all
10411 -- of its formals and enters associations between the actuals occurring
10412 -- in the formal package's corresponding actual package (given by
10413 -- Actual_Ent) and the formal package's formal parameters. This
10414 -- procedure recurses if any of the parameters is itself a package.
10416 function Is_Instance_Of
10417 (Act_Spec
: Entity_Id
;
10418 Gen_Anc
: Entity_Id
) return Boolean;
10419 -- The actual can be an instantiation of a generic within another
10420 -- instance, in which case there is no direct link from it to the
10421 -- original generic ancestor. In that case, we recognize that the
10422 -- ultimate ancestor is the same by examining names and scopes.
10424 procedure Process_Nested_Formal
(Formal
: Entity_Id
);
10425 -- If the current formal is declared with a box, its own formals are
10426 -- visible in the instance, as they were in the generic, and their
10427 -- Hidden flag must be reset. If some of these formals are themselves
10428 -- packages declared with a box, the processing must be recursive.
10430 --------------------------
10431 -- Find_Matching_Actual --
10432 --------------------------
10434 procedure Find_Matching_Actual
10436 Act
: in out Entity_Id
)
10438 Formal_Ent
: Entity_Id
;
10441 case Nkind
(Original_Node
(F
)) is
10442 when N_Formal_Object_Declaration
10443 | N_Formal_Type_Declaration
10445 Formal_Ent
:= Defining_Identifier
(F
);
10447 while Present
(Act
)
10448 and then Chars
(Act
) /= Chars
(Formal_Ent
)
10453 when N_Formal_Package_Declaration
10454 | N_Formal_Subprogram_Declaration
10455 | N_Generic_Package_Declaration
10456 | N_Package_Declaration
10458 Formal_Ent
:= Defining_Entity
(F
);
10460 while Present
(Act
)
10461 and then Chars
(Act
) /= Chars
(Formal_Ent
)
10467 raise Program_Error
;
10469 end Find_Matching_Actual
;
10471 -------------------------
10472 -- Match_Formal_Entity --
10473 -------------------------
10475 procedure Match_Formal_Entity
10476 (Formal_Node
: Node_Id
;
10477 Formal_Ent
: Entity_Id
;
10478 Actual_Ent
: Entity_Id
)
10480 Act_Pkg
: Entity_Id
;
10483 Set_Instance_Of
(Formal_Ent
, Actual_Ent
);
10485 if Ekind
(Actual_Ent
) = E_Package
then
10487 -- Record associations for each parameter
10489 Act_Pkg
:= Actual_Ent
;
10492 A_Ent
: Entity_Id
:= First_Entity
(Act_Pkg
);
10496 Gen_Decl
: Node_Id
;
10498 Actual
: Entity_Id
;
10501 -- Retrieve the actual given in the formal package declaration
10503 Actual
:= Entity
(Name
(Original_Node
(Formal_Node
)));
10505 -- The actual in the formal package declaration may be a
10506 -- renamed generic package, in which case we want to retrieve
10507 -- the original generic in order to traverse its formal part.
10509 if Present
(Renamed_Entity
(Actual
)) then
10510 Gen_Decl
:= Unit_Declaration_Node
(Renamed_Entity
(Actual
));
10512 Gen_Decl
:= Unit_Declaration_Node
(Actual
);
10515 Formals
:= Generic_Formal_Declarations
(Gen_Decl
);
10517 if Present
(Formals
) then
10518 F_Node
:= First_Non_Pragma
(Formals
);
10523 while Present
(A_Ent
)
10524 and then Present
(F_Node
)
10525 and then A_Ent
/= First_Private_Entity
(Act_Pkg
)
10527 F_Ent
:= Get_Formal_Entity
(F_Node
);
10529 if Present
(F_Ent
) then
10531 -- This is a formal of the original package. Record
10532 -- association and recurse.
10534 Find_Matching_Actual
(F_Node
, A_Ent
);
10535 Match_Formal_Entity
(F_Node
, F_Ent
, A_Ent
);
10536 Next_Entity
(A_Ent
);
10539 Next_Non_Pragma
(F_Node
);
10543 end Match_Formal_Entity
;
10545 -----------------------
10546 -- Get_Formal_Entity --
10547 -----------------------
10549 function Get_Formal_Entity
(N
: Node_Id
) return Entity_Id
is
10550 Kind
: constant Node_Kind
:= Nkind
(Original_Node
(N
));
10553 when N_Formal_Object_Declaration
=>
10554 return Defining_Identifier
(N
);
10556 when N_Formal_Type_Declaration
=>
10557 return Defining_Identifier
(N
);
10559 when N_Formal_Subprogram_Declaration
=>
10560 return Defining_Unit_Name
(Specification
(N
));
10562 when N_Formal_Package_Declaration
=>
10563 return Defining_Identifier
(Original_Node
(N
));
10565 when N_Generic_Package_Declaration
=>
10566 return Defining_Identifier
(Original_Node
(N
));
10568 -- All other declarations are introduced by semantic analysis and
10569 -- have no match in the actual.
10574 end Get_Formal_Entity
;
10576 --------------------
10577 -- Is_Instance_Of --
10578 --------------------
10580 function Is_Instance_Of
10581 (Act_Spec
: Entity_Id
;
10582 Gen_Anc
: Entity_Id
) return Boolean
10584 Gen_Par
: constant Entity_Id
:= Generic_Parent
(Act_Spec
);
10587 if No
(Gen_Par
) then
10590 -- Simplest case: the generic parent of the actual is the formal
10592 elsif Gen_Par
= Gen_Anc
then
10595 elsif Chars
(Gen_Par
) /= Chars
(Gen_Anc
) then
10598 -- The actual may be obtained through several instantiations. Its
10599 -- scope must itself be an instance of a generic declared in the
10600 -- same scope as the formal. Any other case is detected above.
10602 elsif not Is_Generic_Instance
(Scope
(Gen_Par
)) then
10606 return Generic_Parent
(Parent
(Scope
(Gen_Par
))) = Scope
(Gen_Anc
);
10608 end Is_Instance_Of
;
10610 ---------------------------
10611 -- Process_Nested_Formal --
10612 ---------------------------
10614 procedure Process_Nested_Formal
(Formal
: Entity_Id
) is
10618 if Present
(Associated_Formal_Package
(Formal
))
10619 and then Box_Present
(Parent
(Associated_Formal_Package
(Formal
)))
10621 Ent
:= First_Entity
(Formal
);
10622 while Present
(Ent
) loop
10623 Set_Is_Hidden
(Ent
, False);
10624 Set_Is_Visible_Formal
(Ent
);
10625 Set_Is_Potentially_Use_Visible
10626 (Ent
, Is_Potentially_Use_Visible
(Formal
));
10628 if Ekind
(Ent
) = E_Package
then
10629 exit when Renamed_Entity
(Ent
) = Renamed_Entity
(Formal
);
10630 Process_Nested_Formal
(Ent
);
10636 end Process_Nested_Formal
;
10638 -- Start of processing for Instantiate_Formal_Package
10643 -- The actual must be a package instance, or else a current instance
10644 -- such as a parent generic within the body of a generic child.
10646 if not Is_Entity_Name
(Actual
)
10647 or else not Is_Package_Or_Generic_Package
(Entity
(Actual
))
10650 ("expect package instance to instantiate formal", Actual
);
10651 Abandon_Instantiation
(Actual
);
10654 Actual_Pack
:= Entity
(Actual
);
10655 Set_Is_Instantiated
(Actual_Pack
);
10657 -- The actual may be a renamed package, or an outer generic formal
10658 -- package whose instantiation is converted into a renaming.
10660 if Present
(Renamed_Entity
(Actual_Pack
)) then
10661 Actual_Pack
:= Renamed_Entity
(Actual_Pack
);
10664 -- The analyzed formal is expected to be the result of the rewriting
10665 -- of the formal package into a regular package by analysis.
10667 pragma Assert
(Nkind
(Analyzed_Formal
) = N_Package_Declaration
10668 and then Nkind
(Original_Node
(Analyzed_Formal
)) =
10669 N_Formal_Package_Declaration
);
10671 Gen_Parent
:= Generic_Parent
(Specification
(Analyzed_Formal
));
10672 Formal_Pack
:= Defining_Unit_Name
(Specification
(Analyzed_Formal
));
10674 -- The actual for a ghost generic formal package should be a ghost
10675 -- package (SPARK RM 6.9(14)).
10677 Check_Ghost_Formal_Procedure_Or_Package
10679 Actual
=> Actual_Pack
,
10680 Formal
=> Formal_Pack
);
10682 if Nkind
(Parent
(Actual_Pack
)) = N_Defining_Program_Unit_Name
then
10683 Parent_Spec
:= Package_Specification
(Actual_Pack
);
10685 Parent_Spec
:= Parent
(Actual_Pack
);
10688 if Gen_Parent
= Any_Id
then
10690 ("previous error in declaration of formal package", Actual
);
10691 Abandon_Instantiation
(Actual
);
10693 elsif Is_Instance_Of
(Parent_Spec
, Get_Instance_Of
(Gen_Parent
)) then
10696 -- If this is the current instance of an enclosing generic, that unit
10697 -- is the generic package we need.
10699 elsif In_Open_Scopes
(Actual_Pack
)
10700 and then Ekind
(Actual_Pack
) = E_Generic_Package
10706 ("actual parameter must be instance of&", Actual
, Gen_Parent
);
10707 Abandon_Instantiation
(Actual
);
10710 Set_Instance_Of
(Defining_Identifier
(Formal
), Actual_Pack
);
10711 Map_Formal_Package_Entities
(Formal_Pack
, Actual_Pack
);
10714 Make_Package_Renaming_Declaration
(Loc
,
10715 Defining_Unit_Name
=> New_Copy
(Defining_Identifier
(Formal
)),
10716 Name
=> New_Occurrence_Of
(Actual_Pack
, Loc
));
10718 Set_Associated_Formal_Package
10719 (Defining_Unit_Name
(Nod
), Defining_Identifier
(Formal
));
10720 Decls
:= New_List
(Nod
);
10722 -- If the formal F has a box, then the generic declarations are
10723 -- visible in the generic G. In an instance of G, the corresponding
10724 -- entities in the actual for F (which are the actuals for the
10725 -- instantiation of the generic that F denotes) must also be made
10726 -- visible for analysis of the current instance. On exit from the
10727 -- current instance, those entities are made private again. If the
10728 -- actual is currently in use, these entities are also use-visible.
10730 -- The loop through the actual entities also steps through the formal
10731 -- entities and enters associations from formals to actuals into the
10732 -- renaming map. This is necessary to properly handle checking of
10733 -- actual parameter associations for later formals that depend on
10734 -- actuals declared in the formal package.
10736 -- In Ada 2005, partial parameterization requires that we make
10737 -- visible the actuals corresponding to formals that were defaulted
10738 -- in the formal package. There formals are identified because they
10739 -- remain formal generics within the formal package, rather than
10740 -- being renamings of the actuals supplied.
10743 Gen_Decl
: constant Node_Id
:=
10744 Unit_Declaration_Node
(Gen_Parent
);
10745 Formals
: constant List_Id
:=
10746 Generic_Formal_Declarations
(Gen_Decl
);
10748 Actual_Ent
: Entity_Id
;
10749 Actual_Of_Formal
: Node_Id
;
10750 Formal_Node
: Node_Id
;
10751 Formal_Ent
: Entity_Id
;
10754 if Present
(Formals
) then
10755 Formal_Node
:= First_Non_Pragma
(Formals
);
10757 Formal_Node
:= Empty
;
10760 Actual_Ent
:= First_Entity
(Actual_Pack
);
10761 Actual_Of_Formal
:=
10762 First
(Visible_Declarations
(Specification
(Analyzed_Formal
)));
10763 while Present
(Actual_Ent
)
10764 and then Actual_Ent
/= First_Private_Entity
(Actual_Pack
)
10766 if Present
(Formal_Node
) then
10767 Formal_Ent
:= Get_Formal_Entity
(Formal_Node
);
10769 if Present
(Formal_Ent
) then
10770 Find_Matching_Actual
(Formal_Node
, Actual_Ent
);
10771 Match_Formal_Entity
(Formal_Node
, Formal_Ent
, Actual_Ent
);
10773 -- We iterate at the same time over the actuals of the
10774 -- local package created for the formal, to determine
10775 -- which one of the formals of the original generic were
10776 -- defaulted in the formal. The corresponding actual
10777 -- entities are visible in the enclosing instance.
10779 if Box_Present
(Formal
)
10781 (Present
(Actual_Of_Formal
)
10784 (Get_Formal_Entity
(Actual_Of_Formal
)))
10786 Set_Is_Hidden
(Actual_Ent
, False);
10787 Set_Is_Visible_Formal
(Actual_Ent
);
10788 Set_Is_Potentially_Use_Visible
10789 (Actual_Ent
, In_Use
(Actual_Pack
));
10791 if Ekind
(Actual_Ent
) = E_Package
then
10792 Process_Nested_Formal
(Actual_Ent
);
10796 if not Is_Hidden
(Actual_Ent
) then
10797 Append_Elmt
(Actual_Ent
, Hidden_Formals
);
10800 Set_Is_Hidden
(Actual_Ent
);
10801 Set_Is_Potentially_Use_Visible
(Actual_Ent
, False);
10805 Next_Non_Pragma
(Formal_Node
);
10806 Next
(Actual_Of_Formal
);
10808 -- A formal subprogram may be overloaded, so advance in
10809 -- the list of actuals to make sure we do not match two
10810 -- successive formals to the same actual. This is only
10811 -- relevant for overloadable entities, others have
10814 if Is_Overloadable
(Actual_Ent
) then
10815 Next_Entity
(Actual_Ent
);
10819 -- No further formals to match, but the generic part may
10820 -- contain inherited operation that are not hidden in the
10821 -- enclosing instance.
10823 Next_Entity
(Actual_Ent
);
10827 -- Inherited subprograms generated by formal derived types are
10828 -- also visible if the types are.
10830 Actual_Ent
:= First_Entity
(Actual_Pack
);
10831 while Present
(Actual_Ent
)
10832 and then Actual_Ent
/= First_Private_Entity
(Actual_Pack
)
10834 if Is_Overloadable
(Actual_Ent
)
10836 Nkind
(Parent
(Actual_Ent
)) = N_Subtype_Declaration
10838 not Is_Hidden
(Defining_Identifier
(Parent
(Actual_Ent
)))
10840 Set_Is_Hidden
(Actual_Ent
, False);
10841 Set_Is_Potentially_Use_Visible
10842 (Actual_Ent
, In_Use
(Actual_Pack
));
10845 Next_Entity
(Actual_Ent
);
10849 -- If the formal requires conformance checking, reanalyze it as an
10850 -- abbreviated instantiation, to verify the matching rules of 12.7.
10851 -- The actual checks are performed after the generic associations
10852 -- have been analyzed, to guarantee the same visibility for this
10853 -- instantiation and for the actuals.
10855 -- In Ada 2005, the generic associations for the formal can include
10856 -- defaulted parameters. These are ignored during check. This
10857 -- internal instantiation is removed from the tree after conformance
10858 -- checking, because it contains formal declarations for those
10859 -- defaulted parameters, and those should not reach the back-end.
10861 if Requires_Conformance_Checking
(Formal
) then
10863 I_Pack
: constant Entity_Id
:= Make_Temporary
(Loc
, 'P');
10868 Set_Is_Internal
(I_Pack
);
10869 Mutate_Ekind
(I_Pack
, E_Package
);
10871 -- Insert the package into the list of its hidden entities so
10872 -- that the list is not empty for Is_Abbreviated_Instance.
10874 Append_Elmt
(I_Pack
, Hidden_Formals
);
10876 Set_Hidden_In_Formal_Instance
(I_Pack
, Hidden_Formals
);
10878 -- If the generic is a child unit, Check_Generic_Child_Unit
10879 -- needs its original name in case it is qualified.
10881 if Is_Child_Unit
(Gen_Parent
) then
10883 New_Copy_Tree
(Name
(Original_Node
(Analyzed_Formal
)));
10884 pragma Assert
(Entity
(I_Nam
) = Gen_Parent
);
10888 New_Occurrence_Of
(Get_Instance_Of
(Gen_Parent
), Loc
);
10892 Make_Package_Instantiation
(Loc
,
10893 Defining_Unit_Name
=> I_Pack
,
10895 Generic_Associations
=> Generic_Associations
(Formal
)));
10901 end Instantiate_Formal_Package
;
10903 -----------------------------------
10904 -- Instantiate_Formal_Subprogram --
10905 -----------------------------------
10907 function Instantiate_Formal_Subprogram
10910 Analyzed_Formal
: Node_Id
) return Node_Id
10912 Analyzed_S
: constant Entity_Id
:=
10913 Defining_Unit_Name
(Specification
(Analyzed_Formal
));
10914 Formal_Sub
: constant Entity_Id
:=
10915 Defining_Unit_Name
(Specification
(Formal
));
10917 function From_Parent_Scope
(Subp
: Entity_Id
) return Boolean;
10918 -- If the generic is a child unit, the parent has been installed on the
10919 -- scope stack, but a default subprogram cannot resolve to something
10920 -- on the parent because that parent is not really part of the visible
10921 -- context (it is there to resolve explicit local entities). If the
10922 -- default has resolved in this way, we remove the entity from immediate
10923 -- visibility and analyze the node again to emit an error message or
10924 -- find another visible candidate.
10926 procedure Valid_Actual_Subprogram
(Act
: Node_Id
);
10927 -- Perform legality check and raise exception on failure
10929 -----------------------
10930 -- From_Parent_Scope --
10931 -----------------------
10933 function From_Parent_Scope
(Subp
: Entity_Id
) return Boolean is
10934 Gen_Scope
: Node_Id
;
10937 Gen_Scope
:= Scope
(Analyzed_S
);
10938 while Present
(Gen_Scope
) and then Is_Child_Unit
(Gen_Scope
) loop
10939 if Scope
(Subp
) = Scope
(Gen_Scope
) then
10943 Gen_Scope
:= Scope
(Gen_Scope
);
10947 end From_Parent_Scope
;
10949 -----------------------------
10950 -- Valid_Actual_Subprogram --
10951 -----------------------------
10953 procedure Valid_Actual_Subprogram
(Act
: Node_Id
) is
10957 if Is_Entity_Name
(Act
) then
10958 Act_E
:= Entity
(Act
);
10960 elsif Nkind
(Act
) = N_Selected_Component
10961 and then Is_Entity_Name
(Selector_Name
(Act
))
10963 Act_E
:= Entity
(Selector_Name
(Act
));
10969 -- The actual for a ghost generic formal procedure should be a ghost
10970 -- procedure (SPARK RM 6.9(14)).
10973 and then Ekind
(Act_E
) = E_Procedure
10975 Check_Ghost_Formal_Procedure_Or_Package
10978 Formal
=> Analyzed_S
);
10981 if (Present
(Act_E
) and then Is_Overloadable
(Act_E
))
10982 or else Nkind
(Act
) in N_Attribute_Reference
10983 | N_Indexed_Component
10984 | N_Character_Literal
10985 | N_Explicit_Dereference
10991 ("expect subprogram or entry name in instantiation of &",
10992 Instantiation_Node
, Formal_Sub
);
10993 Abandon_Instantiation
(Instantiation_Node
);
10994 end Valid_Actual_Subprogram
;
10998 Decl_Node
: Node_Id
;
11001 New_Spec
: Node_Id
;
11002 New_Subp
: Entity_Id
;
11004 -- Start of processing for Instantiate_Formal_Subprogram
11007 New_Spec
:= New_Copy_Tree
(Specification
(Formal
));
11009 -- The tree copy has created the proper instantiation sloc for the
11010 -- new specification. Use this location for all other constructed
11013 Loc
:= Sloc
(Defining_Unit_Name
(New_Spec
));
11015 -- Create new entity for the actual (New_Copy_Tree does not), and
11016 -- indicate that it is an actual.
11018 -- If the actual is not an entity (i.e. an attribute reference)
11019 -- and the formal includes aspect specifications for contracts,
11020 -- we create an internal name for the renaming declaration. The
11021 -- constructed wrapper contains a call to the entity in the renaming.
11022 -- This is an expansion activity, as is the wrapper creation.
11024 if Ada_Version
>= Ada_2022
11025 and then Has_Contracts
(Analyzed_Formal
)
11026 and then not Is_Entity_Name
(Actual
)
11027 and then Expander_Active
11029 New_Subp
:= Make_Temporary
(Sloc
(Actual
), 'S');
11031 New_Subp
:= Make_Defining_Identifier
(Loc
, Chars
(Formal_Sub
));
11034 Mutate_Ekind
(New_Subp
, Ekind
(Analyzed_S
));
11035 Set_Is_Generic_Actual_Subprogram
(New_Subp
);
11036 Set_Defining_Unit_Name
(New_Spec
, New_Subp
);
11038 -- Create new entities for the each of the formals in the specification
11039 -- of the renaming declaration built for the actual.
11041 if Present
(Parameter_Specifications
(New_Spec
)) then
11047 F
:= First
(Parameter_Specifications
(New_Spec
));
11048 while Present
(F
) loop
11049 F_Id
:= Defining_Identifier
(F
);
11051 Set_Defining_Identifier
(F
,
11052 Make_Defining_Identifier
(Sloc
(F_Id
), Chars
(F_Id
)));
11058 -- Find entity of actual. If the actual is an attribute reference, it
11059 -- cannot be resolved here (its formal is missing) but is handled
11060 -- instead in Attribute_Renaming. If the actual is overloaded, it is
11061 -- fully resolved subsequently, when the renaming declaration for the
11062 -- formal is analyzed. If it is an explicit dereference, resolve the
11063 -- prefix but not the actual itself, to prevent interpretation as call.
11065 if Present
(Actual
) then
11066 Loc
:= Sloc
(Actual
);
11067 Set_Sloc
(New_Spec
, Loc
);
11069 if Nkind
(Actual
) = N_Operator_Symbol
then
11070 Find_Direct_Name
(Actual
);
11072 elsif Nkind
(Actual
) = N_Explicit_Dereference
then
11073 Analyze
(Prefix
(Actual
));
11075 elsif Nkind
(Actual
) /= N_Attribute_Reference
then
11079 Valid_Actual_Subprogram
(Actual
);
11082 elsif Present
(Default_Name
(Formal
)) then
11083 if Nkind
(Default_Name
(Formal
)) not in N_Attribute_Reference
11084 | N_Selected_Component
11085 | N_Indexed_Component
11086 | N_Character_Literal
11087 and then Present
(Entity
(Default_Name
(Formal
)))
11089 Nam
:= New_Occurrence_Of
(Entity
(Default_Name
(Formal
)), Loc
);
11091 Nam
:= New_Copy
(Default_Name
(Formal
));
11092 Set_Sloc
(Nam
, Loc
);
11095 elsif Box_Present
(Formal
) then
11097 -- Actual is resolved at the point of instantiation. Create an
11098 -- identifier or operator with the same name as the formal.
11100 if Nkind
(Formal_Sub
) = N_Defining_Operator_Symbol
then
11102 Make_Operator_Symbol
(Loc
,
11103 Chars
=> Chars
(Formal_Sub
),
11104 Strval
=> No_String
);
11106 Nam
:= Make_Identifier
(Loc
, Chars
(Formal_Sub
));
11109 elsif Nkind
(Specification
(Formal
)) = N_Procedure_Specification
11110 and then Null_Present
(Specification
(Formal
))
11112 -- Generate null body for procedure, for use in the instance
11115 Make_Subprogram_Body
(Loc
,
11116 Specification
=> New_Spec
,
11117 Declarations
=> New_List
,
11118 Handled_Statement_Sequence
=>
11119 Make_Handled_Sequence_Of_Statements
(Loc
,
11120 Statements
=> New_List
(Make_Null_Statement
(Loc
))));
11122 -- RM 12.6 (16.2/2): The procedure has convention Intrinsic
11124 Set_Convention
(Defining_Unit_Name
(New_Spec
), Convention_Intrinsic
);
11126 Copy_Ghost_Aspect
(Formal
, To
=> Decl_Node
);
11128 -- Eliminate the calls to it when optimization is enabled
11130 Set_Is_Inlined
(Defining_Unit_Name
(New_Spec
));
11133 -- Handle case of a formal function with an expression default (allowed
11134 -- when extensions are enabled).
11136 elsif Nkind
(Specification
(Formal
)) = N_Function_Specification
11137 and then Present
(Expression
(Formal
))
11139 -- Generate body for function, for use in the instance
11142 Expr
: constant Node_Id
:= New_Copy
(Expression
(Formal
));
11143 Stmt
: constant Node_Id
:= Make_Simple_Return_Statement
(Loc
);
11145 Set_Sloc
(Expr
, Loc
);
11146 Set_Expression
(Stmt
, Expr
);
11149 Make_Subprogram_Body
(Loc
,
11150 Specification
=> New_Spec
,
11151 Declarations
=> New_List
,
11152 Handled_Statement_Sequence
=>
11153 Make_Handled_Sequence_Of_Statements
(Loc
,
11154 Statements
=> New_List
(Stmt
)));
11157 -- RM 12.6 (16.2/2): Like a null procedure default, the function
11158 -- has convention Intrinsic.
11160 Set_Convention
(Defining_Unit_Name
(New_Spec
), Convention_Intrinsic
);
11162 -- Inline calls to it when optimization is enabled
11164 Set_Is_Inlined
(Defining_Unit_Name
(New_Spec
));
11168 Error_Msg_Sloc
:= Sloc
(Scope
(Analyzed_S
));
11170 ("missing actual&", Instantiation_Node
, Formal_Sub
);
11172 ("\in instantiation of & declared#",
11173 Instantiation_Node
, Scope
(Analyzed_S
));
11174 Abandon_Instantiation
(Instantiation_Node
);
11178 Make_Subprogram_Renaming_Declaration
(Loc
,
11179 Specification
=> New_Spec
,
11182 -- If we do not have an actual and the formal specified <> then set to
11183 -- get proper default.
11185 if No
(Actual
) and then Box_Present
(Formal
) then
11186 Set_From_Default
(Decl_Node
);
11189 -- Gather possible interpretations for the actual before analyzing the
11190 -- instance. If overloaded, it will be resolved when analyzing the
11191 -- renaming declaration.
11193 if Box_Present
(Formal
) and then No
(Actual
) then
11196 if Is_Child_Unit
(Scope
(Analyzed_S
))
11197 and then Present
(Entity
(Nam
))
11199 if not Is_Overloaded
(Nam
) then
11200 if From_Parent_Scope
(Entity
(Nam
)) then
11201 Set_Is_Immediately_Visible
(Entity
(Nam
), False);
11202 Set_Entity
(Nam
, Empty
);
11203 Set_Etype
(Nam
, Empty
);
11206 Set_Is_Immediately_Visible
(Entity
(Nam
));
11215 Get_First_Interp
(Nam
, I
, It
);
11216 while Present
(It
.Nam
) loop
11217 if From_Parent_Scope
(It
.Nam
) then
11221 Get_Next_Interp
(I
, It
);
11228 -- The generic instantiation freezes the actual. This can only be done
11229 -- once the actual is resolved, in the analysis of the renaming
11230 -- declaration. To make the formal subprogram entity available, we set
11231 -- Corresponding_Formal_Spec to point to the formal subprogram entity.
11232 -- This is also needed in Analyze_Subprogram_Renaming for the processing
11233 -- of formal abstract subprograms.
11235 Set_Corresponding_Formal_Spec
(Decl_Node
, Analyzed_S
);
11237 -- We cannot analyze the renaming declaration, and thus find the actual,
11238 -- until all the actuals are assembled in the instance. For subsequent
11239 -- checks of other actuals, indicate the node that will hold the
11240 -- instance of this formal.
11242 Set_Instance_Of
(Analyzed_S
, Nam
);
11244 if Nkind
(Actual
) = N_Selected_Component
11245 and then Is_Task_Type
(Etype
(Prefix
(Actual
)))
11246 and then not Is_Frozen
(Etype
(Prefix
(Actual
)))
11248 -- The renaming declaration will create a body, which must appear
11249 -- outside of the instantiation, We move the renaming declaration
11250 -- out of the instance, and create an additional renaming inside,
11251 -- to prevent freezing anomalies.
11254 Anon_Id
: constant Entity_Id
:= Make_Temporary
(Loc
, 'E');
11257 Set_Defining_Unit_Name
(New_Spec
, Anon_Id
);
11258 Insert_Before
(Instantiation_Node
, Decl_Node
);
11259 Analyze
(Decl_Node
);
11261 -- Now create renaming within the instance
11264 Make_Subprogram_Renaming_Declaration
(Loc
,
11265 Specification
=> New_Copy_Tree
(New_Spec
),
11266 Name
=> New_Occurrence_Of
(Anon_Id
, Loc
));
11268 Set_Defining_Unit_Name
(Specification
(Decl_Node
),
11269 Make_Defining_Identifier
(Loc
, Chars
(Formal_Sub
)));
11274 end Instantiate_Formal_Subprogram
;
11276 ------------------------
11277 -- Instantiate_Object --
11278 ------------------------
11280 function Instantiate_Object
11283 Analyzed_Formal
: Node_Id
) return List_Id
11285 Gen_Obj
: constant Entity_Id
:= Defining_Identifier
(Formal
);
11286 A_Gen_Obj
: constant Entity_Id
:=
11287 Defining_Identifier
(Analyzed_Formal
);
11288 Acc_Def
: Node_Id
:= Empty
;
11289 Act_Assoc
: constant Node_Id
:=
11290 (if No
(Actual
) then Empty
else Parent
(Actual
));
11291 Actual_Decl
: Node_Id
:= Empty
;
11292 Decl_Node
: Node_Id
;
11295 List
: constant List_Id
:= New_List
;
11296 Loc
: constant Source_Ptr
:= Sloc
(Actual
);
11297 Orig_Ftyp
: constant Entity_Id
:= Etype
(A_Gen_Obj
);
11298 Subt_Decl
: Node_Id
:= Empty
;
11299 Subt_Mark
: Node_Id
:= Empty
;
11301 -- Start of processing for Instantiate_Object
11304 -- Formal may be an anonymous access
11306 if Present
(Subtype_Mark
(Formal
)) then
11307 Subt_Mark
:= Subtype_Mark
(Formal
);
11309 Check_Access_Definition
(Formal
);
11310 Acc_Def
:= Access_Definition
(Formal
);
11313 -- Sloc for error message on missing actual
11315 Error_Msg_Sloc
:= Sloc
(Scope
(A_Gen_Obj
));
11317 if Get_Instance_Of
(Gen_Obj
) /= Gen_Obj
then
11318 Error_Msg_N
("duplicate instantiation of generic parameter", Actual
);
11321 Set_Parent
(List
, Act_Assoc
);
11325 if Out_Present
(Formal
) then
11327 -- An IN OUT generic actual must be a name. The instantiation is a
11328 -- renaming declaration. The actual is the name being renamed. We
11329 -- use the actual directly, rather than a copy, because it is not
11330 -- used further in the list of actuals, and because a copy or a use
11331 -- of relocate_node is incorrect if the instance is nested within a
11332 -- generic. In order to simplify e.g. ASIS queries, the
11333 -- Generic_Parent field links the declaration to the generic
11336 if No
(Actual
) then
11338 ("missing actual &",
11339 Instantiation_Node
, Gen_Obj
);
11341 ("\in instantiation of & declared#",
11342 Instantiation_Node
, Scope
(A_Gen_Obj
));
11343 Abandon_Instantiation
(Instantiation_Node
);
11346 if Present
(Subt_Mark
) then
11348 Make_Object_Renaming_Declaration
(Loc
,
11349 Defining_Identifier
=> New_Copy
(Gen_Obj
),
11350 Subtype_Mark
=> New_Copy_Tree
(Subt_Mark
),
11353 else pragma Assert
(Present
(Acc_Def
));
11355 Make_Object_Renaming_Declaration
(Loc
,
11356 Defining_Identifier
=> New_Copy
(Gen_Obj
),
11357 Access_Definition
=> New_Copy_Tree
(Acc_Def
),
11361 Set_Corresponding_Generic_Association
(Decl_Node
, Act_Assoc
);
11363 -- The analysis of the actual may produce Insert_Action nodes, so
11364 -- the declaration must have a context in which to attach them.
11366 Append
(Decl_Node
, List
);
11369 -- Return if the analysis of the actual reported some error
11371 if Etype
(Actual
) = Any_Type
then
11375 -- This check is performed here because Analyze_Object_Renaming will
11376 -- not check it when Comes_From_Source is False. Note though that the
11377 -- check for the actual being the name of an object will be performed
11378 -- in Analyze_Object_Renaming.
11380 if Is_Object_Reference
(Actual
)
11381 and then Is_Dependent_Component_Of_Mutable_Object
(Actual
)
11384 ("illegal discriminant-dependent component for in out parameter",
11388 -- The actual has to be resolved in order to check that it is a
11389 -- variable (due to cases such as F (1), where F returns access to
11390 -- an array, and for overloaded prefixes).
11392 Ftyp
:= Get_Instance_Of
(Etype
(A_Gen_Obj
));
11394 -- If the type of the formal is not itself a formal, and the current
11395 -- unit is a child unit, the formal type must be declared in a
11396 -- parent, and must be retrieved by visibility.
11398 if Ftyp
= Orig_Ftyp
11399 and then Is_Generic_Unit
(Scope
(Ftyp
))
11400 and then Is_Child_Unit
(Scope
(A_Gen_Obj
))
11403 Temp
: constant Node_Id
:=
11404 New_Copy_Tree
(Subtype_Mark
(Analyzed_Formal
));
11406 Set_Entity
(Temp
, Empty
);
11408 Ftyp
:= Entity
(Temp
);
11412 if Is_Private_Type
(Ftyp
)
11413 and then not Is_Private_Type
(Etype
(Actual
))
11414 and then (Base_Type
(Full_View
(Ftyp
)) = Base_Type
(Etype
(Actual
))
11415 or else Base_Type
(Etype
(Actual
)) = Ftyp
)
11417 -- If the actual has the type of the full view of the formal, or
11418 -- else a non-private subtype of the formal, then the visibility
11419 -- of the formal type has changed. Add to the actuals a subtype
11420 -- declaration that will force the exchange of views in the body
11421 -- of the instance as well.
11424 Make_Subtype_Declaration
(Loc
,
11425 Defining_Identifier
=> Make_Temporary
(Loc
, 'P'),
11426 Subtype_Indication
=> New_Occurrence_Of
(Ftyp
, Loc
));
11428 Prepend
(Subt_Decl
, List
);
11430 Prepend_Elmt
(Full_View
(Ftyp
), Exchanged_Views
);
11431 Exchange_Declarations
(Ftyp
);
11434 Resolve
(Actual
, Ftyp
);
11436 if not Denotes_Variable
(Actual
) then
11437 Error_Msg_NE
("actual for& must be a variable", Actual
, Gen_Obj
);
11439 elsif Base_Type
(Ftyp
) /= Base_Type
(Etype
(Actual
)) then
11441 -- Ada 2005 (AI-423): For a generic formal object of mode in out,
11442 -- the type of the actual shall resolve to a specific anonymous
11445 if Ada_Version
< Ada_2005
11446 or else not Is_Anonymous_Access_Type
(Base_Type
(Ftyp
))
11447 or else not Is_Anonymous_Access_Type
(Base_Type
(Etype
(Actual
)))
11450 ("type of actual does not match type of&", Actual
, Gen_Obj
);
11454 Note_Possible_Modification
(Actual
, Sure
=> True);
11456 -- Check for instantiation with atomic/volatile/VFA object actual for
11457 -- nonatomic/nonvolatile/nonVFA formal (RM C.6 (12)).
11459 if Is_Atomic_Object
(Actual
) and then not Is_Atomic
(Orig_Ftyp
) then
11461 ("cannot instantiate nonatomic formal & of mode in out",
11463 Error_Msg_N
("\with atomic object actual (RM C.6(12))", Actual
);
11465 elsif Is_Volatile_Object_Ref
(Actual
)
11466 and then not Is_Volatile
(Orig_Ftyp
)
11469 ("cannot instantiate nonvolatile formal & of mode in out",
11471 Error_Msg_N
("\with volatile object actual (RM C.6(12))", Actual
);
11473 elsif Is_Volatile_Full_Access_Object_Ref
(Actual
)
11474 and then not Is_Volatile_Full_Access
(Orig_Ftyp
)
11477 ("cannot instantiate nonfull access formal & of mode in out",
11480 ("\with full access object actual (RM C.6(12))", Actual
);
11483 -- Check for instantiation on nonatomic subcomponent of a full access
11484 -- object in Ada 2022 (RM C.6 (12)).
11486 if Ada_Version
>= Ada_2022
11487 and then Is_Subcomponent_Of_Full_Access_Object
(Actual
)
11488 and then not Is_Atomic_Object
(Actual
)
11491 ("cannot instantiate formal & of mode in out with actual",
11494 ("\nonatomic subcomponent of full access object (RM C.6(12))",
11498 -- Check actual/formal compatibility with respect to the four
11499 -- volatility refinement aspects.
11502 Actual_Obj
: constant Entity_Id
:=
11503 Get_Enclosing_Deep_Object
(Actual
);
11505 Check_Volatility_Compatibility
11506 (Actual_Obj
, A_Gen_Obj
, "actual object",
11507 "its corresponding formal object of mode in out",
11508 Srcpos_Bearer
=> Actual
);
11511 -- The actual for a ghost generic formal IN OUT parameter should be a
11512 -- ghost object (SPARK RM 6.9(14)).
11514 Check_Ghost_Formal_Variable
11516 Formal
=> A_Gen_Obj
);
11518 -- Formal in-parameter
11521 -- The instantiation of a generic formal in-parameter is constant
11522 -- declaration. The actual is the expression for that declaration.
11523 -- Its type is a full copy of the type of the formal. This may be
11524 -- an access to subprogram, for which we need to generate entities
11525 -- for the formals in the new signature.
11527 if Present
(Actual
) then
11528 if Present
(Subt_Mark
) then
11529 Def
:= New_Copy_Tree
(Subt_Mark
);
11531 pragma Assert
(Present
(Acc_Def
));
11532 Def
:= New_Copy_Tree
(Acc_Def
);
11536 Make_Object_Declaration
(Loc
,
11537 Defining_Identifier
=> New_Copy
(Gen_Obj
),
11538 Constant_Present
=> True,
11539 Null_Exclusion_Present
=> Null_Exclusion_Present
(Formal
),
11540 Object_Definition
=> Def
,
11541 Expression
=> Actual
);
11543 Copy_Ghost_Aspect
(Formal
, To
=> Decl_Node
);
11544 Set_Corresponding_Generic_Association
(Decl_Node
, Act_Assoc
);
11546 -- A generic formal object of a tagged type is defined to be
11547 -- aliased so the new constant must also be treated as aliased.
11549 if Is_Tagged_Type
(Etype
(A_Gen_Obj
)) then
11550 Set_Aliased_Present
(Decl_Node
);
11553 Append
(Decl_Node
, List
);
11555 -- The actual for a ghost generic formal IN parameter of
11556 -- access-to-variable type should be a ghost object (SPARK
11559 if Is_Access_Variable
(Etype
(A_Gen_Obj
)) then
11560 Check_Ghost_Formal_Variable
11562 Formal
=> A_Gen_Obj
);
11565 -- No need to repeat (pre-)analysis of some expression nodes
11566 -- already handled in Preanalyze_Actuals.
11568 if Nkind
(Actual
) /= N_Allocator
then
11571 -- Return if the analysis of the actual reported some error
11573 if Etype
(Actual
) = Any_Type
then
11579 Formal_Type
: constant Entity_Id
:= Etype
(A_Gen_Obj
);
11583 Typ
:= Get_Instance_Of
(Formal_Type
);
11585 -- If the actual appears in the current or an enclosing scope,
11586 -- use its type directly. This is relevant if it has an actual
11587 -- subtype that is distinct from its nominal one. This cannot
11588 -- be done in general because the type of the actual may
11589 -- depend on other actuals, and only be fully determined when
11590 -- the enclosing instance is analyzed.
11592 if Present
(Etype
(Actual
))
11593 and then Is_Constr_Subt_For_U_Nominal
(Etype
(Actual
))
11595 Freeze_Before
(Instantiation_Node
, Etype
(Actual
));
11597 Freeze_Before
(Instantiation_Node
, Typ
);
11600 -- If the actual is an aggregate, perform name resolution on
11601 -- its components (the analysis of an aggregate does not do it)
11602 -- to capture local names that may be hidden if the generic is
11605 if Nkind
(Actual
) = N_Aggregate
then
11606 Preanalyze_And_Resolve
(Actual
, Typ
);
11609 if Is_Limited_Type
(Typ
)
11610 and then not OK_For_Limited_Init
(Typ
, Actual
)
11613 ("initialization not allowed for limited types", Actual
);
11614 Explain_Limited_Type
(Typ
, Actual
);
11618 elsif Present
(Default_Expression
(Formal
)) then
11620 -- Use default to construct declaration
11622 if Present
(Subt_Mark
) then
11623 Def
:= New_Copy_Tree
(Subt_Mark
);
11625 pragma Assert
(Present
(Acc_Def
));
11626 Def
:= New_Copy_Tree
(Acc_Def
);
11630 Make_Object_Declaration
(Sloc
(Formal
),
11631 Defining_Identifier
=> New_Copy
(Gen_Obj
),
11632 Constant_Present
=> True,
11633 Null_Exclusion_Present
=> Null_Exclusion_Present
(Formal
),
11634 Object_Definition
=> Def
,
11635 Expression
=> New_Copy_Tree
11636 (Default_Expression
(Formal
)));
11638 Copy_Ghost_Aspect
(Formal
, To
=> Decl_Node
);
11639 Set_Corresponding_Generic_Association
11640 (Decl_Node
, Expression
(Decl_Node
));
11642 Append
(Decl_Node
, List
);
11643 Set_Analyzed
(Expression
(Decl_Node
), False);
11646 Error_Msg_NE
("missing actual&", Instantiation_Node
, Gen_Obj
);
11647 Error_Msg_NE
("\in instantiation of & declared#",
11648 Instantiation_Node
, Scope
(A_Gen_Obj
));
11650 if Is_Scalar_Type
(Etype
(A_Gen_Obj
)) then
11652 -- Create dummy constant declaration so that instance can be
11653 -- analyzed, to minimize cascaded visibility errors.
11655 if Present
(Subt_Mark
) then
11657 else pragma Assert
(Present
(Acc_Def
));
11662 Make_Object_Declaration
(Loc
,
11663 Defining_Identifier
=> New_Copy
(Gen_Obj
),
11664 Constant_Present
=> True,
11665 Null_Exclusion_Present
=> Null_Exclusion_Present
(Formal
),
11666 Object_Definition
=> New_Copy
(Def
),
11668 Make_Attribute_Reference
(Sloc
(Gen_Obj
),
11669 Attribute_Name
=> Name_First
,
11670 Prefix
=> New_Copy
(Def
)));
11672 Append
(Decl_Node
, List
);
11675 Abandon_Instantiation
(Instantiation_Node
);
11680 if Nkind
(Actual
) in N_Has_Entity
11681 and then Present
(Entity
(Actual
))
11683 Actual_Decl
:= Parent
(Entity
(Actual
));
11686 -- Ada 2005 (AI-423) refined by AI12-0287:
11687 -- For an object_renaming_declaration with a null_exclusion or an
11688 -- access_definition that has a null_exclusion, the subtype of the
11689 -- object_name shall exclude null. In addition, if the
11690 -- object_renaming_declaration occurs within the body of a generic unit
11691 -- G or within the body of a generic unit declared within the
11692 -- declarative region of generic unit G, then:
11693 -- * if the object_name statically denotes a generic formal object of
11694 -- mode in out of G, then the declaration of that object shall have a
11696 -- * if the object_name statically denotes a call of a generic formal
11697 -- function of G, then the declaration of the result of that function
11698 -- shall have a null_exclusion.
11700 if Ada_Version
>= Ada_2005
11701 and then Present
(Actual_Decl
)
11702 and then Nkind
(Actual_Decl
) in N_Formal_Object_Declaration
11703 | N_Object_Declaration
11704 and then Nkind
(Analyzed_Formal
) = N_Formal_Object_Declaration
11705 and then not Has_Null_Exclusion
(Actual_Decl
)
11706 and then Has_Null_Exclusion
(Analyzed_Formal
)
11707 and then Ekind
(Defining_Identifier
(Analyzed_Formal
))
11708 = E_Generic_In_Out_Parameter
11709 and then ((In_Generic_Scope
(Entity
(Actual
))
11710 and then In_Package_Body
(Scope
(Entity
(Actual
))))
11711 or else not Can_Never_Be_Null
(Etype
(Actual
)))
11713 Error_Msg_Sloc
:= Sloc
(Analyzed_Formal
);
11715 ("actual must exclude null to match generic formal#", Actual
);
11718 -- An effectively volatile object cannot be used as an actual in a
11719 -- generic instantiation (SPARK RM 7.1.3(7)). The following check is
11720 -- relevant only when SPARK_Mode is on as it is not a standard Ada
11721 -- legality rule, and also verifies that the actual is an object.
11724 and then Present
(Actual
)
11725 and then Is_Object_Reference
(Actual
)
11726 and then Is_Effectively_Volatile_Object
(Actual
)
11727 and then not Is_Effectively_Volatile
(A_Gen_Obj
)
11730 ("volatile object cannot act as actual in generic instantiation",
11735 end Instantiate_Object
;
11737 ------------------------------
11738 -- Instantiate_Package_Body --
11739 ------------------------------
11741 -- WARNING: This routine manages Ghost and SPARK regions. Return statements
11742 -- must be replaced by gotos which jump to the end of the routine in order
11743 -- to restore the Ghost and SPARK modes.
11745 procedure Instantiate_Package_Body
11746 (Body_Info
: Pending_Body_Info
;
11747 Inlined_Body
: Boolean := False;
11748 Body_Optional
: Boolean := False)
11750 Act_Decl
: constant Node_Id
:= Body_Info
.Act_Decl
;
11751 Act_Decl_Id
: constant Entity_Id
:= Defining_Entity
(Act_Decl
);
11752 Act_Spec
: constant Node_Id
:= Specification
(Act_Decl
);
11753 Ctx_Parents
: Elist_Id
:= No_Elist
;
11754 Ctx_Top
: Int
:= 0;
11755 Inst_Node
: constant Node_Id
:= Body_Info
.Inst_Node
;
11756 Gen_Id
: constant Node_Id
:= Name
(Inst_Node
);
11757 Gen_Unit
: constant Entity_Id
:= Get_Generic_Entity
(Inst_Node
);
11758 Gen_Decl
: constant Node_Id
:= Unit_Declaration_Node
(Gen_Unit
);
11759 Loc
: constant Source_Ptr
:= Sloc
(Inst_Node
);
11761 procedure Check_Initialized_Types
;
11762 -- In a generic package body, an entity of a generic private type may
11763 -- appear uninitialized. This is suspicious, unless the actual is a
11764 -- fully initialized type.
11766 procedure Install_Parents_Of_Generic_Context
11767 (Inst_Scope
: Entity_Id
;
11768 Ctx_Parents
: out Elist_Id
);
11769 -- Inst_Scope is the scope where the instance appears within; when it
11770 -- appears within a generic child package G, this routine collects and
11771 -- installs the enclosing packages of G in the scopes stack; installed
11772 -- packages are returned in Ctx_Parents.
11774 procedure Remove_Parents_Of_Generic_Context
(Ctx_Parents
: Elist_Id
);
11775 -- Reverse effect after instantiation is complete
11777 -----------------------------
11778 -- Check_Initialized_Types --
11779 -----------------------------
11781 procedure Check_Initialized_Types
is
11783 Formal
: Entity_Id
;
11784 Actual
: Entity_Id
;
11785 Uninit_Var
: Entity_Id
;
11788 Decl
:= First
(Generic_Formal_Declarations
(Gen_Decl
));
11789 while Present
(Decl
) loop
11790 Uninit_Var
:= Empty
;
11792 if Nkind
(Decl
) = N_Private_Extension_Declaration
then
11793 Uninit_Var
:= Uninitialized_Variable
(Decl
);
11795 elsif Nkind
(Decl
) = N_Formal_Type_Declaration
11796 and then Nkind
(Formal_Type_Definition
(Decl
)) =
11797 N_Formal_Private_Type_Definition
11800 Uninitialized_Variable
(Formal_Type_Definition
(Decl
));
11803 if Present
(Uninit_Var
) then
11804 Formal
:= Defining_Identifier
(Decl
);
11805 Actual
:= First_Entity
(Act_Decl_Id
);
11807 -- For each formal there is a subtype declaration that renames
11808 -- the actual and has the same name as the formal. Locate the
11809 -- formal for warning message about uninitialized variables
11810 -- in the generic, for which the actual type should be a fully
11811 -- initialized type.
11813 while Present
(Actual
) loop
11814 exit when Ekind
(Actual
) = E_Package
11815 and then Present
(Renamed_Entity
(Actual
));
11817 if Chars
(Actual
) = Chars
(Formal
)
11818 and then not Is_Scalar_Type
(Actual
)
11819 and then not Is_Fully_Initialized_Type
(Actual
)
11820 and then Warn_On_No_Value_Assigned
11822 Error_Msg_Node_2
:= Formal
;
11824 ("generic unit has uninitialized variable& of "
11825 & "formal private type &?v?", Actual
, Uninit_Var
);
11827 ("actual type for& should be fully initialized type?v?",
11832 Next_Entity
(Actual
);
11838 end Check_Initialized_Types
;
11840 ----------------------------------------
11841 -- Install_Parents_Of_Generic_Context --
11842 ----------------------------------------
11844 procedure Install_Parents_Of_Generic_Context
11845 (Inst_Scope
: Entity_Id
;
11846 Ctx_Parents
: out Elist_Id
)
11852 Ctx_Parents
:= New_Elmt_List
;
11854 -- Collect context parents (ie. parents where the instantiation
11855 -- appears within).
11858 while S
/= Standard_Standard
loop
11859 Prepend_Elmt
(S
, Ctx_Parents
);
11863 -- Install enclosing parents
11865 Elmt
:= First_Elmt
(Ctx_Parents
);
11866 while Present
(Elmt
) loop
11867 Push_Scope
(Node
(Elmt
));
11868 Set_Is_Immediately_Visible
(Node
(Elmt
));
11871 end Install_Parents_Of_Generic_Context
;
11873 ---------------------------------------
11874 -- Remove_Parents_Of_Generic_Context --
11875 ---------------------------------------
11877 procedure Remove_Parents_Of_Generic_Context
(Ctx_Parents
: Elist_Id
) is
11881 -- Traverse Ctx_Parents in LIFO order to check the removed scopes
11883 Elmt
:= Last_Elmt
(Ctx_Parents
);
11884 while Present
(Elmt
) loop
11885 pragma Assert
(Current_Scope
= Node
(Elmt
));
11886 Set_Is_Immediately_Visible
(Current_Scope
, False);
11889 Remove_Last_Elmt
(Ctx_Parents
);
11890 Elmt
:= Last_Elmt
(Ctx_Parents
);
11892 end Remove_Parents_Of_Generic_Context
;
11896 -- The following constants capture the context prior to instantiating
11897 -- the package body.
11899 Saved_CS
: constant Config_Switches_Type
:= Save_Config_Switches
;
11900 Saved_GM
: constant Ghost_Mode_Type
:= Ghost_Mode
;
11901 Saved_IGR
: constant Node_Id
:= Ignored_Ghost_Region
;
11902 Saved_ISMP
: constant Boolean :=
11903 Ignore_SPARK_Mode_Pragmas_In_Instance
;
11904 Saved_LSST
: constant Suppress_Stack_Entry_Ptr
:=
11905 Local_Suppress_Stack_Top
;
11906 Saved_SC
: constant Boolean := Style_Check
;
11907 Saved_SM
: constant SPARK_Mode_Type
:= SPARK_Mode
;
11908 Saved_SMP
: constant Node_Id
:= SPARK_Mode_Pragma
;
11909 Saved_SS
: constant Suppress_Record
:= Scope_Suppress
;
11910 Saved_Warn
: constant Warnings_State
:= Save_Warnings
;
11912 Act_Body
: Node_Id
;
11913 Act_Body_Id
: Entity_Id
;
11914 Act_Body_Name
: Node_Id
;
11915 Gen_Body
: Node_Id
;
11916 Gen_Body_Id
: Node_Id
;
11917 Par_Ent
: Entity_Id
:= Empty
;
11918 Par_Installed
: Boolean := False;
11919 Par_Vis
: Boolean := False;
11921 Scope_Check_Id
: Entity_Id
;
11922 Scope_Check_Last
: Nat
;
11923 -- Value of Current_Scope before calls to Install_Parents; used to check
11924 -- that scopes are correctly removed after instantiation.
11926 Vis_Prims_List
: Elist_Id
:= No_Elist
;
11927 -- List of primitives made temporarily visible in the instantiation
11928 -- to match the visibility of the formal type.
11930 -- Start of processing for Instantiate_Package_Body
11933 Gen_Body_Id
:= Corresponding_Body
(Gen_Decl
);
11935 -- The instance body may already have been processed, as the parent of
11936 -- another instance that is inlined (Load_Parent_Of_Generic).
11938 if Present
(Corresponding_Body
(Instance_Spec
(Inst_Node
))) then
11942 Expander_Mode_Save_And_Set
(Body_Info
.Expander_Status
);
11944 -- Re-establish the state of information on which checks are suppressed.
11945 -- This information was set in Body_Info at the point of instantiation,
11946 -- and now we restore it so that the instance is compiled using the
11947 -- check status at the instantiation (RM 11.5(7.2/2), AI95-00224-01).
11949 Local_Suppress_Stack_Top
:= Body_Info
.Local_Suppress_Stack_Top
;
11950 Scope_Suppress
:= Body_Info
.Scope_Suppress
;
11952 Restore_Config_Switches
(Body_Info
.Config_Switches
);
11953 Restore_Warnings
(Body_Info
.Warnings
);
11955 if No
(Gen_Body_Id
) then
11957 -- Do not look for parent of generic body if none is required.
11958 -- This may happen when the routine is called as part of the
11959 -- Pending_Instantiations processing, when nested instances
11960 -- may precede the one generated from the main unit.
11962 if not Unit_Requires_Body
(Defining_Entity
(Gen_Decl
))
11963 and then Body_Optional
11967 Load_Parent_Of_Generic
11968 (Inst_Node
, Specification
(Gen_Decl
), Body_Optional
);
11970 -- Surprisingly enough, loading the body of the parent can cause
11971 -- the body to be instantiated and the double instantiation needs
11972 -- to be prevented in order to avoid giving bogus semantic errors.
11974 -- This case can occur because of the Collect_Previous_Instances
11975 -- machinery of Load_Parent_Of_Generic, which will instantiate
11976 -- bodies that are deemed to be ahead of the body of the parent
11977 -- in the compilation unit. But the relative position of these
11978 -- bodies is computed using the mere comparison of their Sloc.
11980 -- Now suppose that you have two generic packages G and H, with
11981 -- G containing a mere instantiation of H:
11987 -- package Nested_G is
11998 -- package My_H is new H;
12002 -- and a third package Q instantiating G and Nested_G:
12008 -- package My_G is new G;
12010 -- package My_Nested_G is new My_G.My_H.Nested_G;
12014 -- The body to be instantiated is that of My_Nested_G and its
12015 -- parent is the instance My_G.My_H. This latter instantiation
12016 -- is done when My_G is analyzed, i.e. after the declarations
12017 -- of My_G and My_Nested_G have been parsed; as a result, the
12018 -- Sloc of My_G.My_H is greater than the Sloc of My_Nested_G.
12020 -- Therefore loading the body of My_G.My_H will cause the body
12021 -- of My_Nested_G to be instantiated because it is deemed to be
12022 -- ahead of My_G.My_H. This means that Load_Parent_Of_Generic
12023 -- will again be invoked on My_G.My_H, but this time with the
12024 -- Collect_Previous_Instances machinery disabled, so there is
12025 -- no endless mutual recursion and things are done in order.
12027 if Present
(Corresponding_Body
(Instance_Spec
(Inst_Node
))) then
12031 Gen_Body_Id
:= Corresponding_Body
(Gen_Decl
);
12035 -- Establish global variable for sloc adjustment and for error recovery
12036 -- In the case of an instance body for an instantiation with actuals
12037 -- from a limited view, the instance body is placed at the beginning
12038 -- of the enclosing package body: use the body entity as the source
12039 -- location for nodes of the instance body.
12041 if not Is_Empty_Elmt_List
(Incomplete_Actuals
(Act_Decl_Id
)) then
12043 Scop
: constant Entity_Id
:= Scope
(Act_Decl_Id
);
12044 Body_Id
: constant Node_Id
:=
12045 Corresponding_Body
(Unit_Declaration_Node
(Scop
));
12048 Instantiation_Node
:= Body_Id
;
12051 Instantiation_Node
:= Inst_Node
;
12054 -- The package being instantiated may be subject to pragma Ghost. Set
12055 -- the mode now to ensure that any nodes generated during instantiation
12056 -- are properly marked as Ghost.
12058 Set_Ghost_Mode
(Act_Decl_Id
);
12060 if Present
(Gen_Body_Id
) then
12061 Save_Env
(Gen_Unit
, Act_Decl_Id
);
12062 Style_Check
:= False;
12064 -- If the context of the instance is subject to SPARK_Mode "off", the
12065 -- annotation is missing, or the body is instantiated at a later pass
12066 -- and its spec ignored SPARK_Mode pragma, set the global flag which
12067 -- signals Analyze_Pragma to ignore all SPARK_Mode pragmas within the
12070 if SPARK_Mode
/= On
12071 or else Ignore_SPARK_Mode_Pragmas
(Act_Decl_Id
)
12073 Ignore_SPARK_Mode_Pragmas_In_Instance
:= True;
12076 Current_Sem_Unit
:= Body_Info
.Current_Sem_Unit
;
12077 Gen_Body
:= Unit_Declaration_Node
(Gen_Body_Id
);
12079 Create_Instantiation_Source
12080 (Inst_Node
, Gen_Body_Id
, S_Adjustment
);
12084 (Original_Node
(Gen_Body
), Empty
, Instantiating
=> True);
12086 -- Create proper (possibly qualified) defining name for the body, to
12087 -- correspond to the one in the spec.
12090 Make_Defining_Identifier
(Sloc
(Act_Decl_Id
), Chars
(Act_Decl_Id
));
12091 Preserve_Comes_From_Source
(Act_Body_Id
, Act_Decl_Id
);
12093 -- Some attributes of spec entity are not inherited by body entity
12095 Set_Handler_Records
(Act_Body_Id
, No_List
);
12097 if Nkind
(Defining_Unit_Name
(Act_Spec
)) =
12098 N_Defining_Program_Unit_Name
12101 Make_Defining_Program_Unit_Name
(Loc
,
12103 New_Copy_Tree
(Name
(Defining_Unit_Name
(Act_Spec
))),
12104 Defining_Identifier
=> Act_Body_Id
);
12106 Act_Body_Name
:= Act_Body_Id
;
12109 Set_Defining_Unit_Name
(Act_Body
, Act_Body_Name
);
12111 Set_Corresponding_Spec
(Act_Body
, Act_Decl_Id
);
12112 Check_Generic_Actuals
(Act_Decl_Id
, False);
12113 Check_Initialized_Types
;
12115 -- Install primitives hidden at the point of the instantiation but
12116 -- visible when processing the generic formals
12122 E
:= First_Entity
(Act_Decl_Id
);
12123 while Present
(E
) loop
12125 and then not Is_Itype
(E
)
12126 and then Is_Generic_Actual_Type
(E
)
12127 and then Is_Tagged_Type
(E
)
12129 Install_Hidden_Primitives
12130 (Prims_List
=> Vis_Prims_List
,
12131 Gen_T
=> Generic_Parent_Type
(Parent
(E
)),
12139 Scope_Check_Id
:= Current_Scope
;
12140 Scope_Check_Last
:= Scope_Stack
.Last
;
12142 -- If the instantiation appears within a generic child some actual
12143 -- parameter may be the current instance of the enclosing generic
12147 Inst_Scope
: constant Entity_Id
:= Scope
(Act_Decl_Id
);
12150 if Is_Child_Unit
(Inst_Scope
)
12151 and then Ekind
(Inst_Scope
) = E_Generic_Package
12152 and then Present
(Generic_Associations
(Inst_Node
))
12154 Install_Parents_Of_Generic_Context
(Inst_Scope
, Ctx_Parents
);
12156 -- Hide them from visibility; required to avoid conflicts
12157 -- installing the parent instance.
12159 if Present
(Ctx_Parents
) then
12160 Push_Scope
(Standard_Standard
);
12161 Ctx_Top
:= Scope_Stack
.Last
;
12162 Scope_Stack
.Table
(Ctx_Top
).Is_Active_Stack_Base
:= True;
12167 -- If it is a child unit, make the parent instance (which is an
12168 -- instance of the parent of the generic) visible.
12170 -- 1) The child unit's parent is an explicit parent instance (the
12171 -- prefix of the name of the generic unit):
12173 -- package Child_Package is new Parent_Instance.Child_Unit;
12175 -- 2) The child unit's parent is an implicit parent instance (e.g.
12176 -- when instantiating a sibling package):
12179 -- package Parent.Second_Child is
12183 -- package Parent.First_Child is
12184 -- package Sibling_Package is new Second_Child;
12186 -- 3) The child unit's parent is not an instance, so the scope is
12187 -- simply the one of the unit.
12189 if Ekind
(Scope
(Gen_Unit
)) = E_Generic_Package
12190 and then Nkind
(Gen_Id
) = N_Expanded_Name
12192 Par_Ent
:= Entity
(Prefix
(Gen_Id
));
12194 elsif Ekind
(Scope
(Gen_Unit
)) = E_Generic_Package
12195 and then Ekind
(Scope
(Act_Decl_Id
)) = E_Package
12196 and then Is_Generic_Instance
(Scope
(Act_Decl_Id
))
12198 (Name
(Get_Unit_Instantiation_Node
12199 (Scope
(Act_Decl_Id
)))) = N_Expanded_Name
12202 (Prefix
(Name
(Get_Unit_Instantiation_Node
12203 (Scope
(Act_Decl_Id
)))));
12205 elsif Is_Child_Unit
(Gen_Unit
) then
12206 Par_Ent
:= Scope
(Gen_Unit
);
12209 if Present
(Par_Ent
) then
12210 Par_Vis
:= Is_Immediately_Visible
(Par_Ent
);
12211 Install_Parent
(Par_Ent
, In_Body
=> True);
12212 Par_Installed
:= True;
12215 -- If the instantiation is a library unit, and this is the main unit,
12216 -- then build the resulting compilation unit nodes for the instance.
12217 -- If this is a compilation unit but it is not the main unit, then it
12218 -- is the body of a unit in the context, that is being compiled
12219 -- because it is encloses some inlined unit or another generic unit
12220 -- being instantiated. In that case, this body is not part of the
12221 -- current compilation, and is not attached to the tree, but its
12222 -- parent must be set for analysis.
12224 if Nkind
(Parent
(Inst_Node
)) = N_Compilation_Unit
then
12226 -- Replace instance node with body of instance, and create new
12227 -- node for corresponding instance declaration.
12229 Build_Instance_Compilation_Unit_Nodes
12230 (Inst_Node
, Act_Body
, Act_Decl
);
12232 -- If the instantiation appears within a generic child package
12233 -- enable visibility of current instance of enclosing generic
12236 if Present
(Ctx_Parents
) then
12237 Scope_Stack
.Table
(Ctx_Top
).Is_Active_Stack_Base
:= False;
12238 Analyze
(Inst_Node
);
12239 Scope_Stack
.Table
(Ctx_Top
).Is_Active_Stack_Base
:= True;
12241 Analyze
(Inst_Node
);
12244 if Parent
(Inst_Node
) = Cunit
(Main_Unit
) then
12246 -- If the instance is a child unit itself, then set the scope
12247 -- of the expanded body to be the parent of the instantiation
12248 -- (ensuring that the fully qualified name will be generated
12249 -- for the elaboration subprogram).
12251 if Nkind
(Defining_Unit_Name
(Act_Spec
)) =
12252 N_Defining_Program_Unit_Name
12254 Set_Scope
(Defining_Entity
(Inst_Node
), Scope
(Act_Decl_Id
));
12258 -- Case where instantiation is not a library unit
12261 -- Handle the case of an instance with incomplete actual types.
12262 -- The instance body cannot be placed just after the declaration
12263 -- because full views have not been seen yet. Any use of the non-
12264 -- limited views in the instance body requires the presence of a
12265 -- regular with_clause in the enclosing unit. Therefore we place
12266 -- the instance body at the beginning of the enclosing body, and
12267 -- the freeze node for the instance is then placed after the body.
12269 if not Is_Empty_Elmt_List
(Incomplete_Actuals
(Act_Decl_Id
))
12270 and then Ekind
(Scope
(Act_Decl_Id
)) = E_Package
12273 Scop
: constant Entity_Id
:= Scope
(Act_Decl_Id
);
12274 Body_Id
: constant Node_Id
:=
12275 Corresponding_Body
(Unit_Declaration_Node
(Scop
));
12280 pragma Assert
(Present
(Body_Id
));
12282 Prepend
(Act_Body
, Declarations
(Parent
(Body_Id
)));
12284 if Expander_Active
then
12285 Ensure_Freeze_Node
(Act_Decl_Id
);
12286 F_Node
:= Freeze_Node
(Act_Decl_Id
);
12287 Set_Is_Frozen
(Act_Decl_Id
, False);
12288 if Is_List_Member
(F_Node
) then
12292 Insert_After
(Act_Body
, F_Node
);
12297 Insert_Before
(Inst_Node
, Act_Body
);
12298 Mark_Rewrite_Insertion
(Act_Body
);
12300 -- Insert the freeze node for the instance if need be
12302 if Expander_Active
then
12303 Freeze_Package_Instance
12304 (Inst_Node
, Gen_Body
, Gen_Decl
, Act_Decl_Id
);
12305 Set_Is_Frozen
(Act_Decl_Id
);
12309 -- If the instantiation appears within a generic child package
12310 -- enable visibility of current instance of enclosing generic
12313 if Present
(Ctx_Parents
) then
12314 Scope_Stack
.Table
(Ctx_Top
).Is_Active_Stack_Base
:= False;
12315 Analyze
(Act_Body
);
12316 Scope_Stack
.Table
(Ctx_Top
).Is_Active_Stack_Base
:= True;
12318 Analyze
(Act_Body
);
12322 Inherit_Context
(Gen_Body
, Inst_Node
);
12324 if Par_Installed
then
12325 Remove_Parent
(In_Body
=> True);
12327 -- Restore the previous visibility of the parent
12329 Set_Is_Immediately_Visible
(Par_Ent
, Par_Vis
);
12332 -- Remove the parent instances if they have been placed on the scope
12333 -- stack to compile the body.
12335 if Present
(Ctx_Parents
) then
12336 pragma Assert
(Scope_Stack
.Last
= Ctx_Top
12337 and then Current_Scope
= Standard_Standard
);
12340 Remove_Parents_Of_Generic_Context
(Ctx_Parents
);
12343 pragma Assert
(Current_Scope
= Scope_Check_Id
);
12344 pragma Assert
(Scope_Stack
.Last
= Scope_Check_Last
);
12346 Restore_Hidden_Primitives
(Vis_Prims_List
);
12348 -- Restore the private views that were made visible when the body of
12349 -- the instantiation was created. Note that, in the case where one of
12350 -- these private views is declared in the parent, there is a nesting
12351 -- issue with the calls to Install_Parent and Remove_Parent made in
12352 -- between above with In_Body set to True, because these calls also
12353 -- want to swap and restore this private view respectively. In this
12354 -- case, the call to Install_Parent does nothing, but the call to
12355 -- Remove_Parent does restore the private view, thus undercutting the
12356 -- call to Restore_Private_Views. That's OK under the condition that
12357 -- the two mechanisms swap exactly the same entities, in particular
12358 -- the private entities dependent on the primary private entities.
12360 Restore_Private_Views
(Act_Decl_Id
);
12362 -- Remove the current unit from visibility if this is an instance
12363 -- that is not elaborated on the fly for inlining purposes.
12365 if not Inlined_Body
then
12366 Set_Is_Immediately_Visible
(Act_Decl_Id
, False);
12371 -- If we have no body, and the unit requires a body, then complain. This
12372 -- complaint is suppressed if we have detected other errors (since a
12373 -- common reason for missing the body is that it had errors).
12374 -- In CodePeer mode, a warning has been emitted already, no need for
12375 -- further messages.
12377 elsif Unit_Requires_Body
(Gen_Unit
)
12378 and then not Body_Optional
12380 if CodePeer_Mode
then
12383 elsif Serious_Errors_Detected
= 0 then
12385 ("cannot find body of generic package &", Inst_Node
, Gen_Unit
);
12387 -- Don't attempt to perform any cleanup actions if some other error
12388 -- was already detected, since this can cause blowups.
12394 -- Case of package that does not need a body
12397 -- If the instantiation of the declaration is a library unit, rewrite
12398 -- the original package instantiation as a package declaration in the
12399 -- compilation unit node.
12401 if Nkind
(Parent
(Inst_Node
)) = N_Compilation_Unit
then
12402 Set_Parent_Spec
(Act_Decl
, Parent_Spec
(Inst_Node
));
12403 Rewrite
(Inst_Node
, Act_Decl
);
12405 -- Generate elaboration entity, in case spec has elaboration code.
12406 -- This cannot be done when the instance is analyzed, because it
12407 -- is not known yet whether the body exists.
12409 Set_Elaboration_Entity_Required
(Act_Decl_Id
, False);
12410 Build_Elaboration_Entity
(Parent
(Inst_Node
), Act_Decl_Id
);
12412 -- If the instantiation is not a library unit, then append the
12413 -- declaration to the list of implicitly generated entities, unless
12414 -- it is already a list member which means that it was already
12417 elsif not Is_List_Member
(Act_Decl
) then
12418 Mark_Rewrite_Insertion
(Act_Decl
);
12419 Insert_Before
(Inst_Node
, Act_Decl
);
12425 -- Restore the context that was in effect prior to instantiating the
12428 Ignore_SPARK_Mode_Pragmas_In_Instance
:= Saved_ISMP
;
12429 Local_Suppress_Stack_Top
:= Saved_LSST
;
12430 Scope_Suppress
:= Saved_SS
;
12431 Style_Check
:= Saved_SC
;
12433 Expander_Mode_Restore
;
12434 Restore_Config_Switches
(Saved_CS
);
12435 Restore_Ghost_Region
(Saved_GM
, Saved_IGR
);
12436 Restore_SPARK_Mode
(Saved_SM
, Saved_SMP
);
12437 Restore_Warnings
(Saved_Warn
);
12438 end Instantiate_Package_Body
;
12440 ---------------------------------
12441 -- Instantiate_Subprogram_Body --
12442 ---------------------------------
12444 -- WARNING: This routine manages Ghost and SPARK regions. Return statements
12445 -- must be replaced by gotos which jump to the end of the routine in order
12446 -- to restore the Ghost and SPARK modes.
12448 procedure Instantiate_Subprogram_Body
12449 (Body_Info
: Pending_Body_Info
;
12450 Body_Optional
: Boolean := False)
12452 Act_Decl
: constant Node_Id
:= Body_Info
.Act_Decl
;
12453 Act_Decl_Id
: constant Entity_Id
:= Defining_Entity
(Act_Decl
);
12454 Inst_Node
: constant Node_Id
:= Body_Info
.Inst_Node
;
12455 Gen_Id
: constant Node_Id
:= Name
(Inst_Node
);
12456 Gen_Unit
: constant Entity_Id
:= Get_Generic_Entity
(Inst_Node
);
12457 Gen_Decl
: constant Node_Id
:= Unit_Declaration_Node
(Gen_Unit
);
12458 Loc
: constant Source_Ptr
:= Sloc
(Inst_Node
);
12459 Pack_Id
: constant Entity_Id
:=
12460 Defining_Unit_Name
(Parent
(Act_Decl
));
12462 -- The following constants capture the context prior to instantiating
12463 -- the subprogram body.
12465 Saved_CS
: constant Config_Switches_Type
:= Save_Config_Switches
;
12466 Saved_GM
: constant Ghost_Mode_Type
:= Ghost_Mode
;
12467 Saved_IGR
: constant Node_Id
:= Ignored_Ghost_Region
;
12468 Saved_ISMP
: constant Boolean :=
12469 Ignore_SPARK_Mode_Pragmas_In_Instance
;
12470 Saved_LSST
: constant Suppress_Stack_Entry_Ptr
:=
12471 Local_Suppress_Stack_Top
;
12472 Saved_SC
: constant Boolean := Style_Check
;
12473 Saved_SM
: constant SPARK_Mode_Type
:= SPARK_Mode
;
12474 Saved_SMP
: constant Node_Id
:= SPARK_Mode_Pragma
;
12475 Saved_SS
: constant Suppress_Record
:= Scope_Suppress
;
12476 Saved_Warn
: constant Warnings_State
:= Save_Warnings
;
12478 Act_Body
: Node_Id
;
12479 Act_Body_Id
: Entity_Id
;
12480 Gen_Body
: Node_Id
;
12481 Gen_Body_Id
: Node_Id
;
12482 Pack_Body
: Node_Id
;
12483 Par_Ent
: Entity_Id
:= Empty
;
12484 Par_Installed
: Boolean := False;
12485 Par_Vis
: Boolean := False;
12486 Ret_Expr
: Node_Id
;
12489 Gen_Body_Id
:= Corresponding_Body
(Gen_Decl
);
12491 -- Subprogram body may have been created already because of an inline
12492 -- pragma, or because of multiple elaborations of the enclosing package
12493 -- when several instances of the subprogram appear in the main unit.
12495 if Present
(Corresponding_Body
(Act_Decl
)) then
12499 Expander_Mode_Save_And_Set
(Body_Info
.Expander_Status
);
12501 -- Re-establish the state of information on which checks are suppressed.
12502 -- This information was set in Body_Info at the point of instantiation,
12503 -- and now we restore it so that the instance is compiled using the
12504 -- check status at the instantiation (RM 11.5(7.2/2), AI95-00224-01).
12506 Local_Suppress_Stack_Top
:= Body_Info
.Local_Suppress_Stack_Top
;
12507 Scope_Suppress
:= Body_Info
.Scope_Suppress
;
12509 Restore_Config_Switches
(Body_Info
.Config_Switches
);
12510 Restore_Warnings
(Body_Info
.Warnings
);
12512 if No
(Gen_Body_Id
) then
12514 -- For imported generic subprogram, no body to compile, complete
12515 -- the spec entity appropriately.
12517 if Is_Imported
(Gen_Unit
) then
12518 Set_Is_Imported
(Act_Decl_Id
);
12519 Set_First_Rep_Item
(Act_Decl_Id
, First_Rep_Item
(Gen_Unit
));
12520 Set_Interface_Name
(Act_Decl_Id
, Interface_Name
(Gen_Unit
));
12521 Set_Convention
(Act_Decl_Id
, Convention
(Gen_Unit
));
12522 Set_Has_Completion
(Act_Decl_Id
);
12525 -- For other cases, compile the body
12528 Load_Parent_Of_Generic
12529 (Inst_Node
, Specification
(Gen_Decl
), Body_Optional
);
12530 Gen_Body_Id
:= Corresponding_Body
(Gen_Decl
);
12534 Instantiation_Node
:= Inst_Node
;
12536 -- The subprogram being instantiated may be subject to pragma Ghost. Set
12537 -- the mode now to ensure that any nodes generated during instantiation
12538 -- are properly marked as Ghost.
12540 Set_Ghost_Mode
(Act_Decl_Id
);
12542 if Present
(Gen_Body_Id
) then
12543 Gen_Body
:= Unit_Declaration_Node
(Gen_Body_Id
);
12545 if Nkind
(Gen_Body
) = N_Subprogram_Body_Stub
then
12547 -- Either body is not present, or context is non-expanding, as
12548 -- when compiling a subunit. Mark the instance as completed, and
12549 -- diagnose a missing body when needed.
12552 and then Operating_Mode
= Generate_Code
12554 Error_Msg_N
("missing proper body for instantiation", Gen_Body
);
12557 Set_Has_Completion
(Act_Decl_Id
);
12561 Save_Env
(Gen_Unit
, Act_Decl_Id
);
12562 Style_Check
:= False;
12564 -- If the context of the instance is subject to SPARK_Mode "off", the
12565 -- annotation is missing, or the body is instantiated at a later pass
12566 -- and its spec ignored SPARK_Mode pragma, set the global flag which
12567 -- signals Analyze_Pragma to ignore all SPARK_Mode pragmas within the
12570 if SPARK_Mode
/= On
12571 or else Ignore_SPARK_Mode_Pragmas
(Act_Decl_Id
)
12573 Ignore_SPARK_Mode_Pragmas_In_Instance
:= True;
12576 -- If the context of an instance is not subject to SPARK_Mode "off",
12577 -- and the generic body is subject to an explicit SPARK_Mode pragma,
12578 -- the latter should be the one applicable to the instance.
12580 if not Ignore_SPARK_Mode_Pragmas_In_Instance
12581 and then SPARK_Mode
/= Off
12582 and then Present
(SPARK_Pragma
(Gen_Body_Id
))
12584 Set_SPARK_Mode
(Gen_Body_Id
);
12587 Current_Sem_Unit
:= Body_Info
.Current_Sem_Unit
;
12588 Create_Instantiation_Source
12595 (Original_Node
(Gen_Body
), Empty
, Instantiating
=> True);
12597 -- Create proper defining name for the body, to correspond to the one
12601 Make_Defining_Identifier
(Sloc
(Act_Decl_Id
), Chars
(Act_Decl_Id
));
12603 Preserve_Comes_From_Source
(Act_Body_Id
, Act_Decl_Id
);
12604 Set_Defining_Unit_Name
(Specification
(Act_Body
), Act_Body_Id
);
12606 Set_Corresponding_Spec
(Act_Body
, Act_Decl_Id
);
12607 Set_Has_Completion
(Act_Decl_Id
);
12608 Check_Generic_Actuals
(Pack_Id
, False);
12610 -- Generate a reference to link the visible subprogram instance to
12611 -- the generic body, which for navigation purposes is the only
12612 -- available source for the instance.
12615 (Related_Instance
(Pack_Id
),
12616 Gen_Body_Id
, 'b', Set_Ref
=> False, Force
=> True);
12618 -- If it is a child unit, make the parent instance (which is an
12619 -- instance of the parent of the generic) visible. The parent
12620 -- instance is the prefix of the name of the generic unit.
12622 if Ekind
(Scope
(Gen_Unit
)) = E_Generic_Package
12623 and then Nkind
(Gen_Id
) = N_Expanded_Name
12625 Par_Ent
:= Entity
(Prefix
(Gen_Id
));
12626 elsif Is_Child_Unit
(Gen_Unit
) then
12627 Par_Ent
:= Scope
(Gen_Unit
);
12630 if Present
(Par_Ent
) then
12631 Par_Vis
:= Is_Immediately_Visible
(Par_Ent
);
12632 Install_Parent
(Par_Ent
, In_Body
=> True);
12633 Par_Installed
:= True;
12636 -- Subprogram body is placed in the body of wrapper package,
12637 -- whose spec contains the subprogram declaration as well as
12638 -- the renaming declarations for the generic parameters.
12641 Make_Package_Body
(Loc
,
12642 Defining_Unit_Name
=> New_Copy
(Pack_Id
),
12643 Declarations
=> New_List
(Act_Body
));
12645 Set_Corresponding_Spec
(Pack_Body
, Pack_Id
);
12647 -- If the instantiation is a library unit, then build resulting
12648 -- compilation unit nodes for the instance. The declaration of
12649 -- the enclosing package is the grandparent of the subprogram
12650 -- declaration. First replace the instantiation node as the unit
12651 -- of the corresponding compilation.
12653 if Nkind
(Parent
(Inst_Node
)) = N_Compilation_Unit
then
12654 if Parent
(Inst_Node
) = Cunit
(Main_Unit
) then
12655 Set_Unit
(Parent
(Inst_Node
), Inst_Node
);
12656 Build_Instance_Compilation_Unit_Nodes
12657 (Inst_Node
, Pack_Body
, Parent
(Parent
(Act_Decl
)));
12658 Analyze
(Inst_Node
);
12660 Set_Parent
(Pack_Body
, Parent
(Inst_Node
));
12661 Analyze
(Pack_Body
);
12665 Insert_Before
(Inst_Node
, Pack_Body
);
12666 Mark_Rewrite_Insertion
(Pack_Body
);
12668 -- Insert the freeze node for the instance if need be
12670 if Expander_Active
then
12671 Freeze_Subprogram_Instance
(Inst_Node
, Gen_Body
, Pack_Id
);
12674 Analyze
(Pack_Body
);
12677 Inherit_Context
(Gen_Body
, Inst_Node
);
12679 Restore_Private_Views
(Pack_Id
, False);
12681 if Par_Installed
then
12682 Remove_Parent
(In_Body
=> True);
12684 -- Restore the previous visibility of the parent
12686 Set_Is_Immediately_Visible
(Par_Ent
, Par_Vis
);
12691 -- Body not found. Error was emitted already. If there were no previous
12692 -- errors, this may be an instance whose scope is a premature instance.
12693 -- In that case we must insure that the (legal) program does raise
12694 -- program error if executed. We generate a subprogram body for this
12697 elsif Serious_Errors_Detected
= 0
12698 and then Nkind
(Parent
(Inst_Node
)) /= N_Compilation_Unit
12700 if Body_Optional
then
12703 elsif Ekind
(Act_Decl_Id
) = E_Procedure
then
12705 Make_Subprogram_Body
(Loc
,
12707 Make_Procedure_Specification
(Loc
,
12708 Defining_Unit_Name
=>
12709 Make_Defining_Identifier
(Loc
, Chars
(Act_Decl_Id
)),
12710 Parameter_Specifications
=>
12712 (Parameter_Specifications
(Parent
(Act_Decl_Id
)))),
12714 Declarations
=> Empty_List
,
12715 Handled_Statement_Sequence
=>
12716 Make_Handled_Sequence_Of_Statements
(Loc
,
12717 Statements
=> New_List
(
12718 Make_Raise_Program_Error
(Loc
,
12719 Reason
=> PE_Access_Before_Elaboration
))));
12723 Make_Raise_Program_Error
(Loc
,
12724 Reason
=> PE_Access_Before_Elaboration
);
12726 Set_Etype
(Ret_Expr
, (Etype
(Act_Decl_Id
)));
12727 Set_Analyzed
(Ret_Expr
);
12730 Make_Subprogram_Body
(Loc
,
12732 Make_Function_Specification
(Loc
,
12733 Defining_Unit_Name
=>
12734 Make_Defining_Identifier
(Loc
, Chars
(Act_Decl_Id
)),
12735 Parameter_Specifications
=>
12737 (Parameter_Specifications
(Parent
(Act_Decl_Id
))),
12738 Result_Definition
=>
12739 New_Occurrence_Of
(Etype
(Act_Decl_Id
), Loc
)),
12741 Declarations
=> Empty_List
,
12742 Handled_Statement_Sequence
=>
12743 Make_Handled_Sequence_Of_Statements
(Loc
,
12744 Statements
=> New_List
(
12745 Make_Simple_Return_Statement
(Loc
, Ret_Expr
))));
12749 Make_Package_Body
(Loc
,
12750 Defining_Unit_Name
=> New_Copy
(Pack_Id
),
12751 Declarations
=> New_List
(Act_Body
));
12753 Insert_After
(Inst_Node
, Pack_Body
);
12754 Set_Corresponding_Spec
(Pack_Body
, Pack_Id
);
12755 Analyze
(Pack_Body
);
12760 -- Restore the context that was in effect prior to instantiating the
12761 -- subprogram body.
12763 Ignore_SPARK_Mode_Pragmas_In_Instance
:= Saved_ISMP
;
12764 Local_Suppress_Stack_Top
:= Saved_LSST
;
12765 Scope_Suppress
:= Saved_SS
;
12766 Style_Check
:= Saved_SC
;
12768 Expander_Mode_Restore
;
12769 Restore_Config_Switches
(Saved_CS
);
12770 Restore_Ghost_Region
(Saved_GM
, Saved_IGR
);
12771 Restore_SPARK_Mode
(Saved_SM
, Saved_SMP
);
12772 Restore_Warnings
(Saved_Warn
);
12773 end Instantiate_Subprogram_Body
;
12775 ----------------------
12776 -- Instantiate_Type --
12777 ----------------------
12779 function Instantiate_Type
12782 Analyzed_Formal
: Node_Id
;
12783 Actual_Decls
: List_Id
) return List_Id
12785 A_Gen_T
: constant Entity_Id
:=
12786 Defining_Identifier
(Analyzed_Formal
);
12787 Def
: constant Node_Id
:= Formal_Type_Definition
(Formal
);
12788 Gen_T
: constant Entity_Id
:= Defining_Identifier
(Formal
);
12790 Ancestor
: Entity_Id
:= Empty
;
12791 Decl_Node
: Node_Id
;
12792 Decl_Nodes
: List_Id
;
12796 procedure Check_Shared_Variable_Control_Aspects
;
12797 -- Ada 2022: Verify that shared variable control aspects (RM C.6)
12798 -- that may be specified for a formal type are obeyed by the actual.
12800 procedure Diagnose_Predicated_Actual
;
12801 -- There are a number of constructs in which a discrete type with
12802 -- predicates is illegal, e.g. as an index in an array type declaration.
12803 -- If a generic type is used is such a construct in a generic package
12804 -- declaration, it carries the flag No_Predicate_On_Actual. it is part
12805 -- of the generic contract that the actual cannot have predicates.
12807 function Subtypes_Match
(Gen_T
, Act_T
: Entity_Id
) return Boolean;
12808 -- Check that base types are the same and that the subtypes match
12809 -- statically. Used in several of the validation subprograms for
12810 -- actuals in instantiations.
12812 procedure Validate_Array_Type_Instance
;
12813 procedure Validate_Access_Subprogram_Instance
;
12814 procedure Validate_Access_Type_Instance
;
12815 procedure Validate_Derived_Type_Instance
;
12816 procedure Validate_Derived_Interface_Type_Instance
;
12817 procedure Validate_Discriminated_Formal_Type
;
12818 procedure Validate_Interface_Type_Instance
;
12819 procedure Validate_Private_Type_Instance
;
12820 procedure Validate_Incomplete_Type_Instance
;
12821 -- These procedures perform validation tests for the named case.
12822 -- Validate_Discriminated_Formal_Type is shared by formal private
12823 -- types and Ada 2012 formal incomplete types.
12825 --------------------------------------------
12826 -- Check_Shared_Variable_Control_Aspects --
12827 --------------------------------------------
12829 -- Ada 2022: Verify that shared variable control aspects (RM C.6)
12830 -- that may be specified for the formal are obeyed by the actual.
12831 -- If the formal is a derived type the aspect specifications must match.
12832 -- NOTE: AI12-0282 implies that matching of aspects is required between
12833 -- formal and actual in all cases, but this is too restrictive.
12834 -- In particular it violates a language design rule: a limited private
12835 -- indefinite formal can be matched by any actual. The current code
12836 -- reflects an older and more permissive version of RM C.6 (12/5).
12838 procedure Check_Shared_Variable_Control_Aspects
is
12840 if Ada_Version
>= Ada_2022
then
12841 if Is_Atomic
(A_Gen_T
) and then not Is_Atomic
(Act_T
) then
12843 ("actual for& must have Atomic aspect", Actual
, A_Gen_T
);
12845 elsif Is_Derived_Type
(A_Gen_T
)
12846 and then Is_Atomic
(A_Gen_T
) /= Is_Atomic
(Act_T
)
12849 ("actual for& has different Atomic aspect", Actual
, A_Gen_T
);
12852 if Is_Volatile
(A_Gen_T
) and then not Is_Volatile
(Act_T
) then
12854 ("actual for& must have Volatile aspect",
12857 elsif Is_Derived_Type
(A_Gen_T
)
12858 and then Is_Volatile
(A_Gen_T
) /= Is_Volatile
(Act_T
)
12861 ("actual for& has different Volatile aspect",
12865 -- We assume that an array type whose atomic component type
12866 -- is Atomic is equivalent to an array type with the explicit
12867 -- aspect Has_Atomic_Components. This is a reasonable inference
12868 -- from the intent of AI12-0282, and makes it legal to use an
12869 -- actual that does not have the identical aspect as the formal.
12870 -- Ditto for volatile components.
12873 Actual_Atomic_Comp
: constant Boolean :=
12874 Has_Atomic_Components
(Act_T
)
12875 or else (Is_Array_Type
(Act_T
)
12876 and then Is_Atomic
(Component_Type
(Act_T
)));
12878 if Has_Atomic_Components
(A_Gen_T
) /= Actual_Atomic_Comp
then
12880 ("formal and actual for& must agree on atomic components",
12886 Actual_Volatile_Comp
: constant Boolean :=
12887 Has_Volatile_Components
(Act_T
)
12888 or else (Is_Array_Type
(Act_T
)
12889 and then Is_Volatile
(Component_Type
(Act_T
)));
12891 if Has_Volatile_Components
(A_Gen_T
) /= Actual_Volatile_Comp
12894 ("actual for& must have volatile components",
12899 -- The following two aspects do not require exact matching,
12900 -- but only one-way agreement. See RM C.6.
12902 if Is_Independent
(A_Gen_T
) and then not Is_Independent
(Act_T
)
12905 ("actual for& must have Independent aspect specified",
12909 if Has_Independent_Components
(A_Gen_T
)
12910 and then not Has_Independent_Components
(Act_T
)
12913 ("actual for& must have Independent_Components specified",
12917 -- Check actual/formal compatibility with respect to the four
12918 -- volatility refinement aspects.
12920 Check_Volatility_Compatibility
12922 "actual type", "its corresponding formal type",
12923 Srcpos_Bearer
=> Actual
);
12925 end Check_Shared_Variable_Control_Aspects
;
12927 ---------------------------------
12928 -- Diagnose_Predicated_Actual --
12929 ---------------------------------
12931 procedure Diagnose_Predicated_Actual
is
12933 if No_Predicate_On_Actual
(A_Gen_T
)
12934 and then Has_Predicates
(Act_T
)
12937 ("actual for& cannot be a type with predicate",
12938 Instantiation_Node
, A_Gen_T
);
12940 elsif No_Dynamic_Predicate_On_Actual
(A_Gen_T
)
12941 and then Has_Predicates
(Act_T
)
12942 and then not Has_Static_Predicate_Aspect
(Act_T
)
12945 ("actual for& cannot be a type with a dynamic predicate",
12946 Instantiation_Node
, A_Gen_T
);
12948 end Diagnose_Predicated_Actual
;
12950 --------------------
12951 -- Subtypes_Match --
12952 --------------------
12954 function Subtypes_Match
(Gen_T
, Act_T
: Entity_Id
) return Boolean is
12955 T
: constant Entity_Id
:= Get_Instance_Of
(Gen_T
);
12958 -- Check that the base types, root types (when dealing with class
12959 -- wide types), or designated types (when dealing with anonymous
12960 -- access types) of Gen_T and Act_T are statically matching subtypes.
12962 return ((Base_Type
(T
) = Act_T
12963 or else Base_Type
(T
) = Base_Type
(Act_T
))
12964 and then Subtypes_Statically_Match
(T
, Act_T
))
12966 or else (Is_Class_Wide_Type
(Gen_T
)
12967 and then Is_Class_Wide_Type
(Act_T
)
12968 and then Subtypes_Match
12969 (Get_Instance_Of
(Root_Type
(Gen_T
)),
12970 Root_Type
(Act_T
)))
12972 or else (Is_Anonymous_Access_Type
(Gen_T
)
12973 and then Ekind
(Act_T
) = Ekind
(Gen_T
)
12974 and then Subtypes_Statically_Match
12975 (Designated_Type
(Gen_T
), Designated_Type
(Act_T
)));
12976 end Subtypes_Match
;
12978 -----------------------------------------
12979 -- Validate_Access_Subprogram_Instance --
12980 -----------------------------------------
12982 procedure Validate_Access_Subprogram_Instance
is
12984 if not Is_Access_Type
(Act_T
)
12985 or else Ekind
(Designated_Type
(Act_T
)) /= E_Subprogram_Type
12988 ("expect access type in instantiation of &", Actual
, Gen_T
);
12989 Abandon_Instantiation
(Actual
);
12992 -- According to AI05-288, actuals for access_to_subprograms must be
12993 -- subtype conformant with the generic formal. Previous to AI05-288
12994 -- only mode conformance was required.
12996 -- This is a binding interpretation that applies to previous versions
12997 -- of the language, no need to maintain previous weaker checks.
12999 Check_Subtype_Conformant
13000 (Designated_Type
(Act_T
),
13001 Designated_Type
(A_Gen_T
),
13005 if Ekind
(Base_Type
(Act_T
)) = E_Access_Protected_Subprogram_Type
then
13006 if Ekind
(A_Gen_T
) = E_Access_Subprogram_Type
then
13008 ("protected access type not allowed for formal &",
13012 elsif Ekind
(A_Gen_T
) = E_Access_Protected_Subprogram_Type
then
13014 ("expect protected access type for formal &",
13018 -- If the formal has a specified convention (which in most cases
13019 -- will be StdCall) verify that the actual has the same convention.
13021 if Has_Convention_Pragma
(A_Gen_T
)
13022 and then Convention
(A_Gen_T
) /= Convention
(Act_T
)
13024 Error_Msg_Name_1
:= Get_Convention_Name
(Convention
(A_Gen_T
));
13026 ("actual for formal & must have convention %", Actual
, Gen_T
);
13029 if Can_Never_Be_Null
(A_Gen_T
) /= Can_Never_Be_Null
(Act_T
) then
13031 ("non null exclusion of actual and formal & do not match",
13034 end Validate_Access_Subprogram_Instance
;
13036 -----------------------------------
13037 -- Validate_Access_Type_Instance --
13038 -----------------------------------
13040 procedure Validate_Access_Type_Instance
is
13041 Desig_Type
: constant Entity_Id
:=
13042 Find_Actual_Type
(Designated_Type
(A_Gen_T
), A_Gen_T
);
13043 Desig_Act
: Entity_Id
;
13046 if not Is_Access_Type
(Act_T
) then
13048 ("expect access type in instantiation of &", Actual
, Gen_T
);
13049 Abandon_Instantiation
(Actual
);
13052 if Is_Access_Constant
(A_Gen_T
) then
13053 if not Is_Access_Constant
(Act_T
) then
13055 ("actual type must be access-to-constant type", Actual
);
13056 Abandon_Instantiation
(Actual
);
13059 if Is_Access_Constant
(Act_T
) then
13061 ("actual type must be access-to-variable type", Actual
);
13062 Abandon_Instantiation
(Actual
);
13064 elsif Ekind
(A_Gen_T
) = E_General_Access_Type
13065 and then Ekind
(Base_Type
(Act_T
)) /= E_General_Access_Type
13068 ("actual must be general access type!", Actual
);
13069 Error_Msg_NE
-- CODEFIX
13070 ("\add ALL to }!", Actual
, Act_T
);
13071 Abandon_Instantiation
(Actual
);
13075 -- The designated subtypes, that is to say the subtypes introduced
13076 -- by an access type declaration (and not by a subtype declaration)
13079 Desig_Act
:= Designated_Type
(Base_Type
(Act_T
));
13081 -- The designated type may have been introduced through a limited_
13082 -- with clause, in which case retrieve the non-limited view. This
13083 -- applies to incomplete types as well as to class-wide types.
13085 if From_Limited_With
(Desig_Act
) then
13086 Desig_Act
:= Available_View
(Desig_Act
);
13089 if not Subtypes_Match
(Desig_Type
, Desig_Act
) then
13091 ("designated type of actual does not match that of formal &",
13094 if not Predicates_Match
(Desig_Type
, Desig_Act
) then
13095 Error_Msg_N
("\predicates do not match", Actual
);
13098 Abandon_Instantiation
(Actual
);
13101 -- Ada 2005: null-exclusion indicators of the two types must agree
13103 if Can_Never_Be_Null
(A_Gen_T
) /= Can_Never_Be_Null
(Act_T
) then
13105 ("non null exclusion of actual and formal & do not match",
13108 end Validate_Access_Type_Instance
;
13110 ----------------------------------
13111 -- Validate_Array_Type_Instance --
13112 ----------------------------------
13114 procedure Validate_Array_Type_Instance
is
13119 function Formal_Dimensions
return Nat
;
13120 -- Count number of dimensions in array type formal
13122 -----------------------
13123 -- Formal_Dimensions --
13124 -----------------------
13126 function Formal_Dimensions
return Nat
is
13131 if Nkind
(Def
) = N_Constrained_Array_Definition
then
13132 Index
:= First
(Discrete_Subtype_Definitions
(Def
));
13134 Index
:= First
(Subtype_Marks
(Def
));
13137 while Present
(Index
) loop
13143 end Formal_Dimensions
;
13145 -- Start of processing for Validate_Array_Type_Instance
13148 if not Is_Array_Type
(Act_T
) then
13150 ("expect array type in instantiation of &", Actual
, Gen_T
);
13151 Abandon_Instantiation
(Actual
);
13153 elsif Nkind
(Def
) = N_Constrained_Array_Definition
then
13154 if not (Is_Constrained
(Act_T
)) then
13156 ("expect constrained array in instantiation of &",
13158 Abandon_Instantiation
(Actual
);
13162 if Is_Constrained
(Act_T
) then
13164 ("expect unconstrained array in instantiation of &",
13166 Abandon_Instantiation
(Actual
);
13170 if Formal_Dimensions
/= Number_Dimensions
(Act_T
) then
13172 ("dimensions of actual do not match formal &", Actual
, Gen_T
);
13173 Abandon_Instantiation
(Actual
);
13176 I1
:= First_Index
(A_Gen_T
);
13177 I2
:= First_Index
(Act_T
);
13178 for J
in 1 .. Formal_Dimensions
loop
13180 -- If the indexes of the actual were given by a subtype_mark,
13181 -- the index was transformed into a range attribute. Retrieve
13182 -- the original type mark for checking.
13184 if Is_Entity_Name
(Original_Node
(I2
)) then
13185 T2
:= Entity
(Original_Node
(I2
));
13190 if not Subtypes_Match
13191 (Find_Actual_Type
(Etype
(I1
), A_Gen_T
), T2
)
13194 ("index types of actual do not match those of formal &",
13196 Abandon_Instantiation
(Actual
);
13203 -- Check matching subtypes. Note that there are complex visibility
13204 -- issues when the generic is a child unit and some aspect of the
13205 -- generic type is declared in a parent unit of the generic. We do
13206 -- the test to handle this special case only after a direct check
13207 -- for static matching has failed. The case where both the component
13208 -- type and the array type are separate formals, and the component
13209 -- type is a private view may also require special checking in
13210 -- Subtypes_Match. Finally, we assume that a child instance where
13211 -- the component type comes from a formal of a parent instance is
13212 -- correct because the generic was correct. A more precise check
13213 -- seems too complex to install???
13216 (Component_Type
(A_Gen_T
), Component_Type
(Act_T
))
13219 (Find_Actual_Type
(Component_Type
(A_Gen_T
), A_Gen_T
),
13220 Component_Type
(Act_T
))
13222 (not Inside_A_Generic
13223 and then Is_Child_Unit
(Scope
(Component_Type
(A_Gen_T
))))
13228 ("component subtype of actual does not match that of formal &",
13230 Abandon_Instantiation
(Actual
);
13233 if Has_Aliased_Components
(A_Gen_T
)
13234 and then not Has_Aliased_Components
(Act_T
)
13237 ("actual must have aliased components to match formal type &",
13240 end Validate_Array_Type_Instance
;
13242 -----------------------------------------------
13243 -- Validate_Derived_Interface_Type_Instance --
13244 -----------------------------------------------
13246 procedure Validate_Derived_Interface_Type_Instance
is
13247 Par
: constant Entity_Id
:= Entity
(Subtype_Indication
(Def
));
13251 -- First apply interface instance checks
13253 Validate_Interface_Type_Instance
;
13255 -- Verify that immediate parent interface is an ancestor of
13259 and then not Interface_Present_In_Ancestor
(Act_T
, Par
)
13262 ("interface actual must include progenitor&", Actual
, Par
);
13265 -- Now verify that the actual includes all other ancestors of
13268 Elmt
:= First_Elmt
(Interfaces
(A_Gen_T
));
13269 while Present
(Elmt
) loop
13270 if not Interface_Present_In_Ancestor
13271 (Act_T
, Get_Instance_Of
(Node
(Elmt
)))
13274 ("interface actual must include progenitor&",
13275 Actual
, Node
(Elmt
));
13280 end Validate_Derived_Interface_Type_Instance
;
13282 ------------------------------------
13283 -- Validate_Derived_Type_Instance --
13284 ------------------------------------
13286 procedure Validate_Derived_Type_Instance
is
13287 Actual_Discr
: Entity_Id
;
13288 Ancestor_Discr
: Entity_Id
;
13291 -- Verify that the actual includes the progenitors of the formal,
13292 -- if any. The formal may depend on previous formals and their
13293 -- instance, so we must examine instance of interfaces if present.
13294 -- The actual may be an extension of an interface, in which case
13295 -- it does not appear in the interface list, so this must be
13296 -- checked separately.
13298 if Present
(Interface_List
(Def
)) then
13299 if not Has_Interfaces
(Act_T
) then
13301 ("actual must implement all interfaces of formal&",
13306 Act_Iface_List
: Elist_Id
;
13308 Iface_Ent
: Entity_Id
;
13310 function Instance_Exists
(I
: Entity_Id
) return Boolean;
13311 -- If the interface entity is declared in a generic unit,
13312 -- this can only be legal if we are within an instantiation
13313 -- of a child of that generic. There is currently no
13314 -- mechanism to relate an interface declared within a
13315 -- generic to the corresponding interface in an instance,
13316 -- so we traverse the list of interfaces of the actual,
13317 -- looking for a name match.
13319 ---------------------
13320 -- Instance_Exists --
13321 ---------------------
13323 function Instance_Exists
(I
: Entity_Id
) return Boolean is
13324 Iface_Elmt
: Elmt_Id
;
13327 Iface_Elmt
:= First_Elmt
(Act_Iface_List
);
13328 while Present
(Iface_Elmt
) loop
13329 if Is_Generic_Instance
(Scope
(Node
(Iface_Elmt
)))
13330 and then Chars
(Node
(Iface_Elmt
)) = Chars
(I
)
13335 Next_Elmt
(Iface_Elmt
);
13339 end Instance_Exists
;
13342 Iface
:= First
(Abstract_Interface_List
(A_Gen_T
));
13343 Collect_Interfaces
(Act_T
, Act_Iface_List
);
13345 while Present
(Iface
) loop
13346 Iface_Ent
:= Get_Instance_Of
(Entity
(Iface
));
13348 if Is_Ancestor
(Iface_Ent
, Act_T
)
13349 or else Is_Progenitor
(Iface_Ent
, Act_T
)
13353 elsif Ekind
(Scope
(Iface_Ent
)) = E_Generic_Package
13354 and then Instance_Exists
(Iface_Ent
)
13359 Error_Msg_Name_1
:= Chars
(Act_T
);
13361 ("actual% must implement interface&",
13362 Actual
, Etype
(Iface
));
13371 -- If the parent type in the generic declaration is itself a previous
13372 -- formal type, then it is local to the generic and absent from the
13373 -- analyzed generic definition. In that case the ancestor is the
13374 -- instance of the formal (which must have been instantiated
13375 -- previously), unless the ancestor is itself a formal derived type.
13376 -- In this latter case (which is the subject of Corrigendum 8652/0038
13377 -- (AI-202) the ancestor of the formals is the ancestor of its
13378 -- parent. Otherwise, the analyzed generic carries the parent type.
13379 -- If the parent type is defined in a previous formal package, then
13380 -- the scope of that formal package is that of the generic type
13381 -- itself, and it has already been mapped into the corresponding type
13382 -- in the actual package.
13384 -- Common case: parent type defined outside of the generic
13386 if Is_Entity_Name
(Subtype_Mark
(Def
))
13387 and then Present
(Entity
(Subtype_Mark
(Def
)))
13389 Ancestor
:= Get_Instance_Of
(Entity
(Subtype_Mark
(Def
)));
13391 -- Check whether parent is defined in a previous formal package
13394 Scope
(Scope
(Base_Type
(Etype
(A_Gen_T
)))) = Scope
(A_Gen_T
)
13397 Get_Instance_Of
(Base_Type
(Etype
(A_Gen_T
)));
13399 -- The type may be a local derivation, or a type extension of a
13400 -- previous formal, or of a formal of a parent package.
13402 elsif Is_Derived_Type
(Get_Instance_Of
(A_Gen_T
))
13404 Ekind
(Get_Instance_Of
(A_Gen_T
)) = E_Record_Type_With_Private
13406 -- Check whether the parent is another derived formal type in the
13407 -- same generic unit.
13409 if Etype
(A_Gen_T
) /= A_Gen_T
13410 and then Is_Generic_Type
(Etype
(A_Gen_T
))
13411 and then Scope
(Etype
(A_Gen_T
)) = Scope
(A_Gen_T
)
13412 and then Etype
(Etype
(A_Gen_T
)) /= Etype
(A_Gen_T
)
13414 -- Locate ancestor of parent from the subtype declaration
13415 -- created for the actual.
13421 Decl
:= First
(Actual_Decls
);
13422 while Present
(Decl
) loop
13423 if Nkind
(Decl
) = N_Subtype_Declaration
13424 and then Chars
(Defining_Identifier
(Decl
)) =
13425 Chars
(Etype
(A_Gen_T
))
13427 Ancestor
:= Generic_Parent_Type
(Decl
);
13435 pragma Assert
(Present
(Ancestor
));
13437 -- The ancestor itself may be a previous formal that has been
13440 Ancestor
:= Get_Instance_Of
(Ancestor
);
13444 Get_Instance_Of
(Base_Type
(Get_Instance_Of
(A_Gen_T
)));
13447 -- Check whether parent is a previous formal of the current generic
13449 elsif Is_Derived_Type
(A_Gen_T
)
13450 and then Is_Generic_Type
(Etype
(A_Gen_T
))
13451 and then Scope
(A_Gen_T
) = Scope
(Etype
(A_Gen_T
))
13453 Ancestor
:= Get_Instance_Of
(First_Subtype
(Etype
(A_Gen_T
)));
13455 -- An unusual case: the actual is a type declared in a parent unit,
13456 -- but is not a formal type so there is no instance_of for it.
13457 -- Retrieve it by analyzing the record extension.
13459 elsif Is_Child_Unit
(Scope
(A_Gen_T
))
13460 and then In_Open_Scopes
(Scope
(Act_T
))
13461 and then Is_Generic_Instance
(Scope
(Act_T
))
13463 Analyze
(Subtype_Mark
(Def
));
13464 Ancestor
:= Entity
(Subtype_Mark
(Def
));
13467 Ancestor
:= Get_Instance_Of
(Etype
(Base_Type
(A_Gen_T
)));
13470 -- If the formal derived type has pragma Preelaborable_Initialization
13471 -- then the actual type must have preelaborable initialization.
13473 if Known_To_Have_Preelab_Init
(A_Gen_T
)
13474 and then not Has_Preelaborable_Initialization
(Act_T
)
13477 ("actual for & must have preelaborable initialization",
13481 -- Ada 2005 (AI-251)
13483 if Ada_Version
>= Ada_2005
and then Is_Interface
(Ancestor
) then
13484 if not Interface_Present_In_Ancestor
(Act_T
, Ancestor
) then
13486 ("(Ada 2005) expected type implementing & in instantiation",
13490 -- Finally verify that the (instance of) the ancestor is an ancestor
13493 elsif not Is_Ancestor
(Base_Type
(Ancestor
), Act_T
) then
13495 ("expect type derived from & in instantiation",
13496 Actual
, First_Subtype
(Ancestor
));
13497 Abandon_Instantiation
(Actual
);
13500 -- Ada 2005 (AI-443): Synchronized formal derived type checks. Note
13501 -- that the formal type declaration has been rewritten as a private
13504 if Ada_Version
>= Ada_2005
13505 and then Nkind
(Parent
(A_Gen_T
)) = N_Private_Extension_Declaration
13506 and then Synchronized_Present
(Parent
(A_Gen_T
))
13508 -- The actual must be a synchronized tagged type
13510 if not Is_Tagged_Type
(Act_T
) then
13512 ("actual of synchronized type must be tagged", Actual
);
13513 Abandon_Instantiation
(Actual
);
13515 elsif Nkind
(Parent
(Act_T
)) = N_Full_Type_Declaration
13516 and then Nkind
(Type_Definition
(Parent
(Act_T
))) =
13517 N_Derived_Type_Definition
13518 and then not Synchronized_Present
13519 (Type_Definition
(Parent
(Act_T
)))
13522 ("actual of synchronized type must be synchronized", Actual
);
13523 Abandon_Instantiation
(Actual
);
13527 -- Perform atomic/volatile checks (RM C.6(12)). Note that AI05-0218-1
13528 -- removes the second instance of the phrase "or allow pass by copy".
13530 -- For Ada 2022, the aspect may be specified explicitly for the
13531 -- formal regardless of whether an ancestor obeys it.
13533 if Is_Atomic
(Act_T
)
13534 and then not Is_Atomic
(Ancestor
)
13535 and then not Is_Atomic
(A_Gen_T
)
13538 ("cannot have atomic actual type for non-atomic formal type",
13541 elsif Is_Volatile
(Act_T
)
13542 and then not Is_Volatile
(Ancestor
)
13543 and then not Is_Volatile
(A_Gen_T
)
13546 ("cannot have volatile actual type for non-volatile formal type",
13550 -- It should not be necessary to check for unknown discriminants on
13551 -- Formal, but for some reason Has_Unknown_Discriminants is false for
13552 -- A_Gen_T, so Is_Definite_Subtype incorrectly returns True. This
13553 -- needs fixing. ???
13555 if Is_Definite_Subtype
(A_Gen_T
)
13556 and then not Unknown_Discriminants_Present
(Formal
)
13557 and then not Is_Definite_Subtype
(Act_T
)
13559 Error_Msg_N
("actual subtype must be constrained", Actual
);
13560 Abandon_Instantiation
(Actual
);
13563 if not Unknown_Discriminants_Present
(Formal
) then
13564 if Is_Constrained
(Ancestor
) then
13565 if not Is_Constrained
(Act_T
) then
13566 Error_Msg_N
("actual subtype must be constrained", Actual
);
13567 Abandon_Instantiation
(Actual
);
13570 -- Ancestor is unconstrained, Check if generic formal and actual
13571 -- agree on constrainedness. The check only applies to array types
13572 -- and discriminated types.
13574 elsif Is_Constrained
(Act_T
) then
13575 if Ekind
(Ancestor
) = E_Access_Type
13576 or else (not Is_Constrained
(A_Gen_T
)
13577 and then Is_Composite_Type
(A_Gen_T
))
13579 Error_Msg_N
("actual subtype must be unconstrained", Actual
);
13580 Abandon_Instantiation
(Actual
);
13583 -- A class-wide type is only allowed if the formal has unknown
13586 elsif Is_Class_Wide_Type
(Act_T
)
13587 and then not Has_Unknown_Discriminants
(Ancestor
)
13590 ("actual for & cannot be a class-wide type", Actual
, Gen_T
);
13591 Abandon_Instantiation
(Actual
);
13593 -- Otherwise, the formal and actual must have the same number
13594 -- of discriminants and each discriminant of the actual must
13595 -- correspond to a discriminant of the formal.
13597 elsif Has_Discriminants
(Act_T
)
13598 and then not Has_Unknown_Discriminants
(Act_T
)
13599 and then Has_Discriminants
(Ancestor
)
13601 Actual_Discr
:= First_Discriminant
(Act_T
);
13602 Ancestor_Discr
:= First_Discriminant
(Ancestor
);
13603 while Present
(Actual_Discr
)
13604 and then Present
(Ancestor_Discr
)
13606 if Base_Type
(Act_T
) /= Base_Type
(Ancestor
) and then
13607 No
(Corresponding_Discriminant
(Actual_Discr
))
13610 ("discriminant & does not correspond "
13611 & "to ancestor discriminant", Actual
, Actual_Discr
);
13612 Abandon_Instantiation
(Actual
);
13615 Next_Discriminant
(Actual_Discr
);
13616 Next_Discriminant
(Ancestor_Discr
);
13619 if Present
(Actual_Discr
) or else Present
(Ancestor_Discr
) then
13621 ("actual for & must have same number of discriminants",
13623 Abandon_Instantiation
(Actual
);
13626 -- This case should be caught by the earlier check for
13627 -- constrainedness, but the check here is added for completeness.
13629 elsif Has_Discriminants
(Act_T
)
13630 and then not Has_Unknown_Discriminants
(Act_T
)
13633 ("actual for & must not have discriminants", Actual
, Gen_T
);
13634 Abandon_Instantiation
(Actual
);
13636 elsif Has_Discriminants
(Ancestor
) then
13638 ("actual for & must have known discriminants", Actual
, Gen_T
);
13639 Abandon_Instantiation
(Actual
);
13642 if not Subtypes_Statically_Compatible
13643 (Act_T
, Ancestor
, Formal_Derived_Matching
=> True)
13646 ("actual for & must be statically compatible with ancestor",
13649 if not Predicates_Compatible
(Act_T
, Ancestor
) then
13651 ("\predicate on actual is not compatible with ancestor",
13655 Abandon_Instantiation
(Actual
);
13659 -- If the formal and actual types are abstract, check that there
13660 -- are no abstract primitives of the actual type that correspond to
13661 -- nonabstract primitives of the formal type (second sentence of
13664 if Is_Abstract_Type
(A_Gen_T
) and then Is_Abstract_Type
(Act_T
) then
13665 Check_Abstract_Primitives
: declare
13666 Gen_Prims
: constant Elist_Id
:=
13667 Primitive_Operations
(A_Gen_T
);
13668 Gen_Elmt
: Elmt_Id
;
13669 Gen_Subp
: Entity_Id
;
13670 Anc_Subp
: Entity_Id
;
13671 Anc_Formal
: Entity_Id
;
13672 Anc_F_Type
: Entity_Id
;
13674 Act_Prims
: constant Elist_Id
:= Primitive_Operations
(Act_T
);
13675 Act_Elmt
: Elmt_Id
;
13676 Act_Subp
: Entity_Id
;
13677 Act_Formal
: Entity_Id
;
13678 Act_F_Type
: Entity_Id
;
13680 Subprograms_Correspond
: Boolean;
13682 function Is_Tagged_Ancestor
(T1
, T2
: Entity_Id
) return Boolean;
13683 -- Returns true if T2 is derived directly or indirectly from
13684 -- T1, including derivations from interfaces. T1 and T2 are
13685 -- required to be specific tagged base types.
13687 ------------------------
13688 -- Is_Tagged_Ancestor --
13689 ------------------------
13691 function Is_Tagged_Ancestor
(T1
, T2
: Entity_Id
) return Boolean
13693 Intfc_Elmt
: Elmt_Id
;
13696 -- The predicate is satisfied if the types are the same
13701 -- If we've reached the top of the derivation chain then
13702 -- we know that T1 is not an ancestor of T2.
13704 elsif Etype
(T2
) = T2
then
13707 -- Proceed to check T2's immediate parent
13709 elsif Is_Ancestor
(T1
, Base_Type
(Etype
(T2
))) then
13712 -- Finally, check to see if T1 is an ancestor of any of T2's
13716 Intfc_Elmt
:= First_Elmt
(Interfaces
(T2
));
13717 while Present
(Intfc_Elmt
) loop
13718 if Is_Ancestor
(T1
, Node
(Intfc_Elmt
)) then
13722 Next_Elmt
(Intfc_Elmt
);
13727 end Is_Tagged_Ancestor
;
13729 -- Start of processing for Check_Abstract_Primitives
13732 -- Loop over all of the formal derived type's primitives
13734 Gen_Elmt
:= First_Elmt
(Gen_Prims
);
13735 while Present
(Gen_Elmt
) loop
13736 Gen_Subp
:= Node
(Gen_Elmt
);
13738 -- If the primitive of the formal is not abstract, then
13739 -- determine whether there is a corresponding primitive of
13740 -- the actual type that's abstract.
13742 if not Is_Abstract_Subprogram
(Gen_Subp
) then
13743 Act_Elmt
:= First_Elmt
(Act_Prims
);
13744 while Present
(Act_Elmt
) loop
13745 Act_Subp
:= Node
(Act_Elmt
);
13747 -- If we find an abstract primitive of the actual,
13748 -- then we need to test whether it corresponds to the
13749 -- subprogram from which the generic formal primitive
13752 if Is_Abstract_Subprogram
(Act_Subp
) then
13753 Anc_Subp
:= Alias
(Gen_Subp
);
13755 -- Test whether we have a corresponding primitive
13756 -- by comparing names, kinds, formal types, and
13759 if Chars
(Anc_Subp
) = Chars
(Act_Subp
)
13760 and then Ekind
(Anc_Subp
) = Ekind
(Act_Subp
)
13762 Anc_Formal
:= First_Formal
(Anc_Subp
);
13763 Act_Formal
:= First_Formal
(Act_Subp
);
13764 while Present
(Anc_Formal
)
13765 and then Present
(Act_Formal
)
13767 Anc_F_Type
:= Etype
(Anc_Formal
);
13768 Act_F_Type
:= Etype
(Act_Formal
);
13770 if Ekind
(Anc_F_Type
) =
13771 E_Anonymous_Access_Type
13773 Anc_F_Type
:= Designated_Type
(Anc_F_Type
);
13775 if Ekind
(Act_F_Type
) =
13776 E_Anonymous_Access_Type
13779 Designated_Type
(Act_F_Type
);
13785 Ekind
(Act_F_Type
) = E_Anonymous_Access_Type
13790 Anc_F_Type
:= Base_Type
(Anc_F_Type
);
13791 Act_F_Type
:= Base_Type
(Act_F_Type
);
13793 -- If the formal is controlling, then the
13794 -- the type of the actual primitive's formal
13795 -- must be derived directly or indirectly
13796 -- from the type of the ancestor primitive's
13799 if Is_Controlling_Formal
(Anc_Formal
) then
13800 if not Is_Tagged_Ancestor
13801 (Anc_F_Type
, Act_F_Type
)
13806 -- Otherwise the types of the formals must
13809 elsif Anc_F_Type
/= Act_F_Type
then
13813 Next_Formal
(Anc_Formal
);
13814 Next_Formal
(Act_Formal
);
13817 -- If we traversed through all of the formals
13818 -- then so far the subprograms correspond, so
13819 -- now check that any result types correspond.
13821 if No
(Anc_Formal
) and then No
(Act_Formal
) then
13822 Subprograms_Correspond
:= True;
13824 if Ekind
(Act_Subp
) = E_Function
then
13825 Anc_F_Type
:= Etype
(Anc_Subp
);
13826 Act_F_Type
:= Etype
(Act_Subp
);
13828 if Ekind
(Anc_F_Type
) =
13829 E_Anonymous_Access_Type
13832 Designated_Type
(Anc_F_Type
);
13834 if Ekind
(Act_F_Type
) =
13835 E_Anonymous_Access_Type
13838 Designated_Type
(Act_F_Type
);
13840 Subprograms_Correspond
:= False;
13845 = E_Anonymous_Access_Type
13847 Subprograms_Correspond
:= False;
13850 Anc_F_Type
:= Base_Type
(Anc_F_Type
);
13851 Act_F_Type
:= Base_Type
(Act_F_Type
);
13853 -- Now either the result types must be
13854 -- the same or, if the result type is
13855 -- controlling, the result type of the
13856 -- actual primitive must descend from the
13857 -- result type of the ancestor primitive.
13859 if Subprograms_Correspond
13860 and then Anc_F_Type
/= Act_F_Type
13862 Has_Controlling_Result
(Anc_Subp
)
13863 and then not Is_Tagged_Ancestor
13864 (Anc_F_Type
, Act_F_Type
)
13866 Subprograms_Correspond
:= False;
13870 -- Found a matching subprogram belonging to
13871 -- formal ancestor type, so actual subprogram
13872 -- corresponds and this violates 3.9.3(9).
13874 if Subprograms_Correspond
then
13876 ("abstract subprogram & overrides "
13877 & "nonabstract subprogram of ancestor",
13884 Next_Elmt
(Act_Elmt
);
13888 Next_Elmt
(Gen_Elmt
);
13890 end Check_Abstract_Primitives
;
13893 -- Verify that limitedness matches. If parent is a limited
13894 -- interface then the generic formal is not unless declared
13895 -- explicitly so. If not declared limited, the actual cannot be
13896 -- limited (see AI05-0087).
13898 if Is_Limited_Type
(Act_T
) and then not Is_Limited_Type
(A_Gen_T
) then
13899 if not In_Instance
then
13901 ("actual for non-limited & cannot be a limited type",
13903 Explain_Limited_Type
(Act_T
, Actual
);
13904 Abandon_Instantiation
(Actual
);
13908 -- Check for AI12-0036
13911 Formal_Is_Private_Extension
: constant Boolean :=
13912 Nkind
(Parent
(A_Gen_T
)) = N_Private_Extension_Declaration
;
13914 Actual_Is_Tagged
: constant Boolean := Is_Tagged_Type
(Act_T
);
13917 if Actual_Is_Tagged
/= Formal_Is_Private_Extension
then
13918 if not In_Instance
then
13919 if Actual_Is_Tagged
then
13921 ("actual for & cannot be a tagged type", Actual
, Gen_T
);
13924 ("actual for & must be a tagged type", Actual
, Gen_T
);
13927 Abandon_Instantiation
(Actual
);
13931 end Validate_Derived_Type_Instance
;
13933 ----------------------------------------
13934 -- Validate_Discriminated_Formal_Type --
13935 ----------------------------------------
13937 procedure Validate_Discriminated_Formal_Type
is
13938 Formal_Discr
: Entity_Id
;
13939 Actual_Discr
: Entity_Id
;
13940 Formal_Subt
: Entity_Id
;
13943 if Has_Discriminants
(A_Gen_T
) then
13944 if not Has_Discriminants
(Act_T
) then
13946 ("actual for & must have discriminants", Actual
, Gen_T
);
13947 Abandon_Instantiation
(Actual
);
13949 elsif Is_Constrained
(Act_T
) then
13951 ("actual for & must be unconstrained", Actual
, Gen_T
);
13952 Abandon_Instantiation
(Actual
);
13955 Formal_Discr
:= First_Discriminant
(A_Gen_T
);
13956 Actual_Discr
:= First_Discriminant
(Act_T
);
13957 while Formal_Discr
/= Empty
loop
13958 if Actual_Discr
= Empty
then
13960 ("discriminants on actual do not match formal",
13962 Abandon_Instantiation
(Actual
);
13965 Formal_Subt
:= Get_Instance_Of
(Etype
(Formal_Discr
));
13967 -- Access discriminants match if designated types do
13969 if Ekind
(Base_Type
(Formal_Subt
)) = E_Anonymous_Access_Type
13970 and then (Ekind
(Base_Type
(Etype
(Actual_Discr
)))) =
13971 E_Anonymous_Access_Type
13974 (Designated_Type
(Base_Type
(Formal_Subt
))) =
13975 Designated_Type
(Base_Type
(Etype
(Actual_Discr
)))
13979 elsif Base_Type
(Formal_Subt
) /=
13980 Base_Type
(Etype
(Actual_Discr
))
13983 ("types of actual discriminants must match formal",
13985 Abandon_Instantiation
(Actual
);
13987 elsif not Subtypes_Statically_Match
13988 (Formal_Subt
, Etype
(Actual_Discr
))
13989 and then Ada_Version
>= Ada_95
13992 ("subtypes of actual discriminants must match formal",
13994 Abandon_Instantiation
(Actual
);
13997 Next_Discriminant
(Formal_Discr
);
13998 Next_Discriminant
(Actual_Discr
);
14001 if Actual_Discr
/= Empty
then
14003 ("discriminants on actual do not match formal",
14005 Abandon_Instantiation
(Actual
);
14009 end Validate_Discriminated_Formal_Type
;
14011 ---------------------------------------
14012 -- Validate_Incomplete_Type_Instance --
14013 ---------------------------------------
14015 procedure Validate_Incomplete_Type_Instance
is
14017 if not Is_Tagged_Type
(Act_T
)
14018 and then Is_Tagged_Type
(A_Gen_T
)
14021 ("actual for & must be a tagged type", Actual
, Gen_T
);
14024 Validate_Discriminated_Formal_Type
;
14025 end Validate_Incomplete_Type_Instance
;
14027 --------------------------------------
14028 -- Validate_Interface_Type_Instance --
14029 --------------------------------------
14031 procedure Validate_Interface_Type_Instance
is
14033 if not Is_Interface
(Act_T
) then
14035 ("actual for formal interface type must be an interface",
14038 elsif Is_Limited_Type
(Act_T
) /= Is_Limited_Type
(A_Gen_T
)
14039 or else Is_Task_Interface
(A_Gen_T
) /= Is_Task_Interface
(Act_T
)
14040 or else Is_Protected_Interface
(A_Gen_T
) /=
14041 Is_Protected_Interface
(Act_T
)
14042 or else Is_Synchronized_Interface
(A_Gen_T
) /=
14043 Is_Synchronized_Interface
(Act_T
)
14046 ("actual for interface& does not match (RM 12.5.5(4))",
14049 end Validate_Interface_Type_Instance
;
14051 ------------------------------------
14052 -- Validate_Private_Type_Instance --
14053 ------------------------------------
14055 procedure Validate_Private_Type_Instance
is
14057 if Is_Limited_Type
(Act_T
)
14058 and then not Is_Limited_Type
(A_Gen_T
)
14060 if In_Instance
then
14064 ("actual for non-limited & cannot be a limited type", Actual
,
14066 Explain_Limited_Type
(Act_T
, Actual
);
14067 Abandon_Instantiation
(Actual
);
14070 elsif Known_To_Have_Preelab_Init
(A_Gen_T
)
14071 and then not Has_Preelaborable_Initialization
(Act_T
)
14074 ("actual for & must have preelaborable initialization", Actual
,
14077 elsif not Is_Definite_Subtype
(Act_T
)
14078 and then Is_Definite_Subtype
(A_Gen_T
)
14079 and then Ada_Version
>= Ada_95
14082 ("actual for & must be a definite subtype", Actual
, Gen_T
);
14084 elsif not Is_Tagged_Type
(Act_T
)
14085 and then Is_Tagged_Type
(A_Gen_T
)
14088 ("actual for & must be a tagged type", Actual
, Gen_T
);
14091 Validate_Discriminated_Formal_Type
;
14093 end Validate_Private_Type_Instance
;
14095 -- Start of processing for Instantiate_Type
14098 if Get_Instance_Of
(A_Gen_T
) /= A_Gen_T
then
14099 Error_Msg_N
("duplicate instantiation of generic type", Actual
);
14100 return New_List
(Error
);
14102 elsif not Is_Entity_Name
(Actual
)
14103 or else not Is_Type
(Entity
(Actual
))
14106 ("expect valid subtype mark to instantiate &", Actual
, Gen_T
);
14107 Abandon_Instantiation
(Actual
);
14110 Act_T
:= Entity
(Actual
);
14112 -- Ada 2005 (AI-216): An Unchecked_Union subtype shall only be passed
14113 -- as a generic actual parameter if the corresponding formal type
14114 -- does not have a known_discriminant_part, or is a formal derived
14115 -- type that is an Unchecked_Union type.
14117 if Is_Unchecked_Union
(Base_Type
(Act_T
)) then
14118 if not Has_Discriminants
(A_Gen_T
)
14119 or else (Is_Derived_Type
(A_Gen_T
)
14120 and then Is_Unchecked_Union
(A_Gen_T
))
14124 Error_Msg_N
("unchecked union cannot be the actual for a "
14125 & "discriminated formal type", Act_T
);
14130 -- Deal with fixed/floating restrictions
14132 if Is_Floating_Point_Type
(Act_T
) then
14133 Check_Restriction
(No_Floating_Point
, Actual
);
14134 elsif Is_Fixed_Point_Type
(Act_T
) then
14135 Check_Restriction
(No_Fixed_Point
, Actual
);
14138 -- Deal with error of using incomplete type as generic actual.
14139 -- This includes limited views of a type, even if the non-limited
14140 -- view may be available.
14142 if Ekind
(Act_T
) = E_Incomplete_Type
14143 or else (Is_Class_Wide_Type
(Act_T
)
14144 and then Ekind
(Root_Type
(Act_T
)) = E_Incomplete_Type
)
14146 -- If the formal is an incomplete type, the actual can be
14147 -- incomplete as well, but if an actual incomplete type has
14148 -- a full view, then we'll retrieve that.
14150 if Ekind
(A_Gen_T
) = E_Incomplete_Type
14151 and then No
(Full_View
(Act_T
))
14155 elsif Is_Class_Wide_Type
(Act_T
)
14156 or else No
(Full_View
(Act_T
))
14158 Error_Msg_N
("premature use of incomplete type", Actual
);
14159 Abandon_Instantiation
(Actual
);
14162 Act_T
:= Full_View
(Act_T
);
14163 Set_Entity
(Actual
, Act_T
);
14165 if Has_Private_Component
(Act_T
) then
14167 ("premature use of type with private component", Actual
);
14171 -- Deal with error of premature use of private type as generic actual
14173 elsif Is_Private_Type
(Act_T
)
14174 and then Is_Private_Type
(Base_Type
(Act_T
))
14175 and then not Is_Generic_Type
(Act_T
)
14176 and then not Is_Derived_Type
(Act_T
)
14177 and then No
(Full_View
(Root_Type
(Act_T
)))
14179 -- If the formal is an incomplete type, the actual can be
14180 -- private or incomplete as well.
14182 if Ekind
(A_Gen_T
) = E_Incomplete_Type
then
14185 Error_Msg_N
("premature use of private type", Actual
);
14188 elsif Has_Private_Component
(Act_T
) then
14190 ("premature use of type with private component", Actual
);
14193 Set_Instance_Of
(A_Gen_T
, Act_T
);
14195 -- If the type is generic, the class-wide type may also be used
14197 if Is_Tagged_Type
(A_Gen_T
)
14198 and then Is_Tagged_Type
(Act_T
)
14199 and then not Is_Class_Wide_Type
(A_Gen_T
)
14201 Set_Instance_Of
(Class_Wide_Type
(A_Gen_T
),
14202 Class_Wide_Type
(Act_T
));
14205 if not Is_Abstract_Type
(A_Gen_T
)
14206 and then Is_Abstract_Type
(Act_T
)
14209 ("actual of non-abstract formal cannot be abstract", Actual
);
14212 -- A generic scalar type is a first subtype for which we generate
14213 -- an anonymous base type. Indicate that the instance of this base
14214 -- is the base type of the actual.
14216 if Is_Scalar_Type
(A_Gen_T
) then
14217 Set_Instance_Of
(Etype
(A_Gen_T
), Etype
(Act_T
));
14221 Check_Shared_Variable_Control_Aspects
;
14223 if Error_Posted
(Act_T
) then
14226 case Nkind
(Def
) is
14227 when N_Formal_Private_Type_Definition
=>
14228 Validate_Private_Type_Instance
;
14230 when N_Formal_Incomplete_Type_Definition
=>
14231 Validate_Incomplete_Type_Instance
;
14233 when N_Formal_Derived_Type_Definition
=>
14234 Validate_Derived_Type_Instance
;
14236 when N_Formal_Discrete_Type_Definition
=>
14237 if not Is_Discrete_Type
(Act_T
) then
14239 ("expect discrete type in instantiation of&",
14241 Abandon_Instantiation
(Actual
);
14244 Diagnose_Predicated_Actual
;
14246 when N_Formal_Signed_Integer_Type_Definition
=>
14247 if not Is_Signed_Integer_Type
(Act_T
) then
14249 ("expect signed integer type in instantiation of&",
14251 Abandon_Instantiation
(Actual
);
14254 Diagnose_Predicated_Actual
;
14256 when N_Formal_Modular_Type_Definition
=>
14257 if not Is_Modular_Integer_Type
(Act_T
) then
14259 ("expect modular type in instantiation of &",
14261 Abandon_Instantiation
(Actual
);
14264 Diagnose_Predicated_Actual
;
14266 when N_Formal_Floating_Point_Definition
=>
14267 if not Is_Floating_Point_Type
(Act_T
) then
14269 ("expect float type in instantiation of &", Actual
, Gen_T
);
14270 Abandon_Instantiation
(Actual
);
14273 when N_Formal_Ordinary_Fixed_Point_Definition
=>
14274 if not Is_Ordinary_Fixed_Point_Type
(Act_T
) then
14276 ("expect ordinary fixed point type in instantiation of &",
14278 Abandon_Instantiation
(Actual
);
14281 when N_Formal_Decimal_Fixed_Point_Definition
=>
14282 if not Is_Decimal_Fixed_Point_Type
(Act_T
) then
14284 ("expect decimal type in instantiation of &",
14286 Abandon_Instantiation
(Actual
);
14289 when N_Array_Type_Definition
=>
14290 Validate_Array_Type_Instance
;
14292 when N_Access_To_Object_Definition
=>
14293 Validate_Access_Type_Instance
;
14295 when N_Access_Function_Definition
14296 | N_Access_Procedure_Definition
14298 Validate_Access_Subprogram_Instance
;
14300 when N_Record_Definition
=>
14301 Validate_Interface_Type_Instance
;
14303 when N_Derived_Type_Definition
=>
14304 Validate_Derived_Interface_Type_Instance
;
14307 raise Program_Error
;
14311 Subt
:= New_Copy
(Gen_T
);
14313 -- Use adjusted sloc of subtype name as the location for other nodes in
14314 -- the subtype declaration.
14316 Loc
:= Sloc
(Subt
);
14319 Make_Subtype_Declaration
(Loc
,
14320 Defining_Identifier
=> Subt
,
14321 Subtype_Indication
=> New_Occurrence_Of
(Act_T
, Loc
));
14323 Copy_Ghost_Aspect
(Formal
, To
=> Decl_Node
);
14325 -- Record whether the actual is private at this point, so that
14326 -- Check_Generic_Actuals can restore its proper view before the
14327 -- semantic analysis of the instance.
14329 if Is_Private_Type
(Act_T
) then
14330 Set_Has_Private_View
(Subtype_Indication
(Decl_Node
));
14333 -- In Ada 2012 the actual may be a limited view. Indicate that
14334 -- the local subtype must be treated as such.
14336 if From_Limited_With
(Act_T
) then
14337 Mutate_Ekind
(Subt
, E_Incomplete_Subtype
);
14338 Set_From_Limited_With
(Subt
);
14341 Decl_Nodes
:= New_List
(Decl_Node
);
14343 -- Flag actual derived types so their elaboration produces the
14344 -- appropriate renamings for the primitive operations of the ancestor.
14345 -- Flag actual for formal private types as well, to determine whether
14346 -- operations in the private part may override inherited operations.
14347 -- If the formal has an interface list, the ancestor is not the
14348 -- parent, but the analyzed formal that includes the interface
14349 -- operations of all its progenitors.
14351 -- Same treatment for formal private types, so we can check whether the
14352 -- type is tagged limited when validating derivations in the private
14353 -- part. (See AI05-096).
14355 if Nkind
(Def
) = N_Formal_Derived_Type_Definition
then
14356 if Present
(Interface_List
(Def
)) then
14357 Set_Generic_Parent_Type
(Decl_Node
, A_Gen_T
);
14359 Set_Generic_Parent_Type
(Decl_Node
, Ancestor
);
14362 elsif Nkind
(Def
) in N_Formal_Private_Type_Definition
14363 | N_Formal_Incomplete_Type_Definition
14365 Set_Generic_Parent_Type
(Decl_Node
, A_Gen_T
);
14368 -- If the actual is a synchronized type that implements an interface,
14369 -- the primitive operations are attached to the corresponding record,
14370 -- and we have to treat it as an additional generic actual, so that its
14371 -- primitive operations become visible in the instance. The task or
14372 -- protected type itself does not carry primitive operations.
14374 if Is_Concurrent_Type
(Act_T
)
14375 and then Is_Tagged_Type
(Act_T
)
14376 and then Present
(Corresponding_Record_Type
(Act_T
))
14377 and then Present
(Ancestor
)
14378 and then Is_Interface
(Ancestor
)
14381 Corr_Rec
: constant Entity_Id
:=
14382 Corresponding_Record_Type
(Act_T
);
14383 New_Corr
: Entity_Id
;
14384 Corr_Decl
: Node_Id
;
14387 New_Corr
:= Make_Temporary
(Loc
, 'S');
14389 Make_Subtype_Declaration
(Loc
,
14390 Defining_Identifier
=> New_Corr
,
14391 Subtype_Indication
=>
14392 New_Occurrence_Of
(Corr_Rec
, Loc
));
14393 Append_To
(Decl_Nodes
, Corr_Decl
);
14395 if Ekind
(Act_T
) = E_Task_Type
then
14396 Mutate_Ekind
(Subt
, E_Task_Subtype
);
14398 Mutate_Ekind
(Subt
, E_Protected_Subtype
);
14401 Set_Corresponding_Record_Type
(Subt
, Corr_Rec
);
14402 Set_Generic_Parent_Type
(Corr_Decl
, Ancestor
);
14403 Set_Generic_Parent_Type
(Decl_Node
, Empty
);
14407 -- For a floating-point type, capture dimension info if any, because
14408 -- the generated subtype declaration does not come from source and
14409 -- will not process dimensions.
14411 if Is_Floating_Point_Type
(Act_T
) then
14412 Copy_Dimensions
(Act_T
, Subt
);
14416 end Instantiate_Type
;
14418 -----------------------------
14419 -- Is_Abbreviated_Instance --
14420 -----------------------------
14422 function Is_Abbreviated_Instance
(E
: Entity_Id
) return Boolean is
14424 return Ekind
(E
) = E_Package
14425 and then Present
(Hidden_In_Formal_Instance
(E
));
14426 end Is_Abbreviated_Instance
;
14428 ---------------------
14429 -- Is_In_Main_Unit --
14430 ---------------------
14432 function Is_In_Main_Unit
(N
: Node_Id
) return Boolean is
14433 Unum
: constant Unit_Number_Type
:= Get_Source_Unit
(N
);
14434 Current_Unit
: Node_Id
;
14437 if Unum
= Main_Unit
then
14440 -- If the current unit is a subunit then it is either the main unit or
14441 -- is being compiled as part of the main unit.
14443 elsif Nkind
(N
) = N_Compilation_Unit
then
14444 return Nkind
(Unit
(N
)) = N_Subunit
;
14447 Current_Unit
:= Parent
(N
);
14448 while Present
(Current_Unit
)
14449 and then Nkind
(Current_Unit
) /= N_Compilation_Unit
14451 Current_Unit
:= Parent
(Current_Unit
);
14454 -- The instantiation node is in the main unit, or else the current node
14455 -- (perhaps as the result of nested instantiations) is in the main unit,
14456 -- or in the declaration of the main unit, which in this last case must
14460 Current_Unit
= Cunit
(Main_Unit
)
14461 or else Current_Unit
= Library_Unit
(Cunit
(Main_Unit
))
14462 or else (Present
(Current_Unit
)
14463 and then Present
(Library_Unit
(Current_Unit
))
14464 and then Is_In_Main_Unit
(Library_Unit
(Current_Unit
)));
14465 end Is_In_Main_Unit
;
14467 ----------------------------
14468 -- Load_Parent_Of_Generic --
14469 ----------------------------
14471 procedure Load_Parent_Of_Generic
14474 Body_Optional
: Boolean := False)
14476 Comp_Unit
: constant Node_Id
:= Cunit
(Get_Source_Unit
(Spec
));
14477 Saved_Style_Check
: constant Boolean := Style_Check
;
14478 Saved_Warn
: constant Warnings_State
:= Save_Warnings
;
14479 True_Parent
: Node_Id
;
14480 Inst_Node
: Node_Id
;
14482 Previous_Instances
: constant Elist_Id
:= New_Elmt_List
;
14484 procedure Collect_Previous_Instances
(Decls
: List_Id
);
14485 -- Collect all instantiations in the given list of declarations, that
14486 -- precede the generic that we need to load. If the bodies of these
14487 -- instantiations are available, we must analyze them, to ensure that
14488 -- the public symbols generated are the same when the unit is compiled
14489 -- to generate code, and when it is compiled in the context of a unit
14490 -- that needs a particular nested instance. This process is applied to
14491 -- both package and subprogram instances.
14493 --------------------------------
14494 -- Collect_Previous_Instances --
14495 --------------------------------
14497 procedure Collect_Previous_Instances
(Decls
: List_Id
) is
14501 Decl
:= First
(Decls
);
14502 while Present
(Decl
) loop
14503 if Sloc
(Decl
) >= Sloc
(Inst_Node
) then
14506 -- If Decl is an instantiation, then record it as requiring
14507 -- instantiation of the corresponding body, except if it is an
14508 -- abbreviated instantiation generated internally for conformance
14509 -- checking purposes only for the case of a formal package
14510 -- declared without a box (see Instantiate_Formal_Package). Such
14511 -- an instantiation does not generate any code (the actual code
14512 -- comes from actual) and thus does not need to be analyzed here.
14513 -- If the instantiation appears with a generic package body it is
14514 -- not analyzed here either.
14516 elsif Nkind
(Decl
) = N_Package_Instantiation
14517 and then not Is_Abbreviated_Instance
(Defining_Entity
(Decl
))
14519 Append_Elmt
(Decl
, Previous_Instances
);
14521 -- For a subprogram instantiation, omit instantiations intrinsic
14522 -- operations (Unchecked_Conversions, etc.) that have no bodies.
14524 elsif Nkind
(Decl
) in N_Function_Instantiation
14525 | N_Procedure_Instantiation
14526 and then not Is_Intrinsic_Subprogram
(Entity
(Name
(Decl
)))
14528 Append_Elmt
(Decl
, Previous_Instances
);
14530 elsif Nkind
(Decl
) = N_Package_Declaration
then
14531 Collect_Previous_Instances
14532 (Visible_Declarations
(Specification
(Decl
)));
14533 Collect_Previous_Instances
14534 (Private_Declarations
(Specification
(Decl
)));
14536 -- Previous non-generic bodies may contain instances as well
14538 elsif Nkind
(Decl
) = N_Package_Body
14539 and then Ekind
(Corresponding_Spec
(Decl
)) /= E_Generic_Package
14541 Collect_Previous_Instances
(Declarations
(Decl
));
14543 elsif Nkind
(Decl
) = N_Subprogram_Body
14544 and then not Acts_As_Spec
(Decl
)
14545 and then not Is_Generic_Subprogram
(Corresponding_Spec
(Decl
))
14547 Collect_Previous_Instances
(Declarations
(Decl
));
14552 end Collect_Previous_Instances
;
14554 -- Start of processing for Load_Parent_Of_Generic
14557 if not In_Same_Source_Unit
(N
, Spec
)
14558 or else Nkind
(Unit
(Comp_Unit
)) = N_Package_Declaration
14559 or else (Nkind
(Unit
(Comp_Unit
)) = N_Package_Body
14560 and then not Is_In_Main_Unit
(Spec
))
14562 -- Find body of parent of spec, and analyze it. A special case arises
14563 -- when the parent is an instantiation, that is to say when we are
14564 -- currently instantiating a nested generic. In that case, there is
14565 -- no separate file for the body of the enclosing instance. Instead,
14566 -- the enclosing body must be instantiated as if it were a pending
14567 -- instantiation, in order to produce the body for the nested generic
14568 -- we require now. Note that in that case the generic may be defined
14569 -- in a package body, the instance defined in the same package body,
14570 -- and the original enclosing body may not be in the main unit.
14572 Inst_Node
:= Empty
;
14574 True_Parent
:= Parent
(Spec
);
14575 while Present
(True_Parent
)
14576 and then Nkind
(True_Parent
) /= N_Compilation_Unit
14578 if Nkind
(True_Parent
) = N_Package_Declaration
14580 Nkind
(Original_Node
(True_Parent
)) = N_Package_Instantiation
14582 -- Parent is a compilation unit that is an instantiation, and
14583 -- instantiation node has been replaced with package decl.
14585 Inst_Node
:= Original_Node
(True_Parent
);
14588 elsif Nkind
(True_Parent
) = N_Package_Declaration
14589 and then Nkind
(Parent
(True_Parent
)) = N_Compilation_Unit
14591 Nkind
(Unit
(Parent
(True_Parent
))) = N_Package_Instantiation
14593 -- Parent is a compilation unit that is an instantiation, but
14594 -- instantiation node has not been replaced with package decl.
14596 Inst_Node
:= Unit
(Parent
(True_Parent
));
14599 elsif Nkind
(True_Parent
) = N_Package_Declaration
14600 and then Nkind
(Parent
(True_Parent
)) /= N_Compilation_Unit
14601 and then Present
(Generic_Parent
(Specification
(True_Parent
)))
14603 -- Parent is an instantiation within another specification.
14604 -- Declaration for instance has been inserted before original
14605 -- instantiation node. A direct link would be preferable?
14607 Inst_Node
:= Next
(True_Parent
);
14608 while Present
(Inst_Node
)
14609 and then Nkind
(Inst_Node
) /= N_Package_Instantiation
14614 -- If the instance appears within a generic, and the generic
14615 -- unit is defined within a formal package of the enclosing
14616 -- generic, there is no generic body available, and none
14617 -- needed. A more precise test should be used ???
14619 if No
(Inst_Node
) then
14625 -- If an ancestor of the generic comes from a formal package
14626 -- there is no source for the ancestor body. This is detected
14627 -- by examining the scope of the ancestor and its declaration.
14628 -- The body, if any is needed, will be available when the
14629 -- current unit (containing a formal package) is instantiated.
14631 elsif Nkind
(True_Parent
) = N_Package_Specification
14632 and then Present
(Generic_Parent
(True_Parent
))
14634 (Original_Node
(Unit_Declaration_Node
14635 (Scope
(Generic_Parent
(True_Parent
)))))
14636 = N_Formal_Package_Declaration
14641 True_Parent
:= Parent
(True_Parent
);
14645 -- Case where we are currently instantiating a nested generic
14647 if Present
(Inst_Node
) then
14648 if Nkind
(Parent
(True_Parent
)) = N_Compilation_Unit
then
14650 -- Instantiation node and declaration of instantiated package
14651 -- were exchanged when only the declaration was needed.
14652 -- Restore instantiation node before proceeding with body.
14654 Set_Unit
(Parent
(True_Parent
), Inst_Node
);
14657 -- Now complete instantiation of enclosing body, if it appears in
14658 -- some other unit. If it appears in the current unit, the body
14659 -- will have been instantiated already.
14661 if No
(Corresponding_Body
(Instance_Spec
(Inst_Node
))) then
14663 -- We need to determine the expander mode to instantiate the
14664 -- enclosing body. Because the generic body we need may use
14665 -- global entities declared in the enclosing package (including
14666 -- aggregates) it is in general necessary to compile this body
14667 -- with expansion enabled, except if we are within a generic
14668 -- package, in which case the usual generic rule applies.
14671 Exp_Status
: Boolean := True;
14675 -- Loop through scopes looking for generic package
14677 Scop
:= Scope
(Defining_Entity
(Instance_Spec
(Inst_Node
)));
14678 while Present
(Scop
)
14679 and then Scop
/= Standard_Standard
14681 if Ekind
(Scop
) = E_Generic_Package
then
14682 Exp_Status
:= False;
14686 Scop
:= Scope
(Scop
);
14689 -- Collect previous instantiations in the unit that contains
14690 -- the desired generic.
14692 if Nkind
(Parent
(True_Parent
)) /= N_Compilation_Unit
14693 and then not Body_Optional
14697 Info
: Pending_Body_Info
;
14701 Par
:= Parent
(Inst_Node
);
14702 while Present
(Par
) loop
14703 exit when Nkind
(Parent
(Par
)) = N_Compilation_Unit
;
14704 Par
:= Parent
(Par
);
14707 pragma Assert
(Present
(Par
));
14709 if Nkind
(Par
) = N_Package_Body
then
14710 Collect_Previous_Instances
(Declarations
(Par
));
14712 elsif Nkind
(Par
) = N_Package_Declaration
then
14713 Collect_Previous_Instances
14714 (Visible_Declarations
(Specification
(Par
)));
14715 Collect_Previous_Instances
14716 (Private_Declarations
(Specification
(Par
)));
14719 -- Enclosing unit is a subprogram body. In this
14720 -- case all instance bodies are processed in order
14721 -- and there is no need to collect them separately.
14726 Decl
:= First_Elmt
(Previous_Instances
);
14727 while Present
(Decl
) loop
14729 (Inst_Node
=> Node
(Decl
),
14731 Instance_Spec
(Node
(Decl
)),
14733 Config_Switches
=> Save_Config_Switches
,
14734 Current_Sem_Unit
=>
14735 Get_Code_Unit
(Sloc
(Node
(Decl
))),
14736 Expander_Status
=> Exp_Status
,
14737 Local_Suppress_Stack_Top
=>
14738 Local_Suppress_Stack_Top
,
14739 Scope_Suppress
=> Scope_Suppress
,
14740 Warnings
=> Save_Warnings
);
14742 -- Package instance
14744 if Nkind
(Node
(Decl
)) = N_Package_Instantiation
14746 Instantiate_Package_Body
14747 (Info
, Body_Optional
=> True);
14749 -- Subprogram instance
14752 -- The instance_spec is in the wrapper package,
14753 -- usually followed by its local renaming
14754 -- declaration. See Build_Subprogram_Renaming
14755 -- for details. If the instance carries aspects,
14756 -- these result in the corresponding pragmas,
14757 -- inserted after the subprogram declaration.
14758 -- They must be skipped as well when retrieving
14759 -- the desired spec. Some of them may have been
14760 -- rewritten as null statements.
14761 -- A direct link would be more robust ???
14765 (Last
(Visible_Declarations
14766 (Specification
(Info
.Act_Decl
))));
14768 while Nkind
(Decl
) in
14771 N_Subprogram_Renaming_Declaration
14773 Decl
:= Prev
(Decl
);
14776 Info
.Act_Decl
:= Decl
;
14779 Instantiate_Subprogram_Body
14780 (Info
, Body_Optional
=> True);
14788 Instantiate_Package_Body
14790 ((Inst_Node
=> Inst_Node
,
14791 Act_Decl
=> True_Parent
,
14793 Config_Switches
=> Save_Config_Switches
,
14794 Current_Sem_Unit
=>
14795 Get_Code_Unit
(Sloc
(Inst_Node
)),
14796 Expander_Status
=> Exp_Status
,
14797 Local_Suppress_Stack_Top
=> Local_Suppress_Stack_Top
,
14798 Scope_Suppress
=> Scope_Suppress
,
14799 Warnings
=> Save_Warnings
)),
14800 Body_Optional
=> Body_Optional
);
14804 -- Case where we are not instantiating a nested generic
14807 Opt
.Style_Check
:= False;
14808 Expander_Mode_Save_And_Set
(True);
14809 Load_Needed_Body
(Comp_Unit
, OK
);
14810 Opt
.Style_Check
:= Saved_Style_Check
;
14811 Restore_Warnings
(Saved_Warn
);
14812 Expander_Mode_Restore
;
14815 and then Unit_Requires_Body
(Defining_Entity
(Spec
))
14816 and then not Body_Optional
14819 Bname
: constant Unit_Name_Type
:=
14820 Get_Body_Name
(Get_Unit_Name
(Unit
(Comp_Unit
)));
14823 -- In CodePeer mode, the missing body may make the analysis
14824 -- incomplete, but we do not treat it as fatal.
14826 if CodePeer_Mode
then
14830 Error_Msg_Unit_1
:= Bname
;
14831 Error_Msg_N
("this instantiation requires$!", N
);
14832 Error_Msg_File_1
:=
14833 Get_File_Name
(Bname
, Subunit
=> False);
14834 Error_Msg_N
("\but file{ was not found!", N
);
14835 raise Unrecoverable_Error
;
14842 -- If loading parent of the generic caused an instantiation circularity,
14843 -- we abandon compilation at this point, because otherwise in some cases
14844 -- we get into trouble with infinite recursions after this point.
14846 if Circularity_Detected
then
14847 raise Unrecoverable_Error
;
14849 end Load_Parent_Of_Generic
;
14851 ---------------------------------
14852 -- Map_Formal_Package_Entities --
14853 ---------------------------------
14855 procedure Map_Formal_Package_Entities
(Form
: Entity_Id
; Act
: Entity_Id
) is
14860 Set_Instance_Of
(Form
, Act
);
14862 -- Traverse formal and actual package to map the corresponding entities.
14863 -- We skip over internal entities that may be generated during semantic
14864 -- analysis, and find the matching entities by name, given that they
14865 -- must appear in the same order.
14867 E1
:= First_Entity
(Form
);
14868 E2
:= First_Entity
(Act
);
14869 while Present
(E1
) and then E1
/= First_Private_Entity
(Form
) loop
14870 -- Could this test be a single condition??? Seems like it could, and
14871 -- isn't FPE (Form) a constant anyway???
14873 if not Is_Internal
(E1
)
14874 and then Present
(Parent
(E1
))
14875 and then not Is_Class_Wide_Type
(E1
)
14876 and then not Is_Internal_Name
(Chars
(E1
))
14878 while Present
(E2
) and then Chars
(E2
) /= Chars
(E1
) loop
14885 Set_Instance_Of
(E1
, E2
);
14887 if Is_Type
(E1
) and then Is_Tagged_Type
(E2
) then
14888 Set_Instance_Of
(Class_Wide_Type
(E1
), Class_Wide_Type
(E2
));
14891 if Is_Constrained
(E1
) then
14892 Set_Instance_Of
(Base_Type
(E1
), Base_Type
(E2
));
14895 if Ekind
(E1
) = E_Package
and then No
(Renamed_Entity
(E1
)) then
14896 Map_Formal_Package_Entities
(E1
, E2
);
14903 end Map_Formal_Package_Entities
;
14905 -----------------------
14906 -- Move_Freeze_Nodes --
14907 -----------------------
14909 procedure Move_Freeze_Nodes
14910 (Out_Of
: Entity_Id
;
14915 Next_Decl
: Node_Id
;
14916 Next_Node
: Node_Id
:= After
;
14919 function Is_Outer_Type
(T
: Entity_Id
) return Boolean;
14920 -- Check whether entity is declared in a scope external to that of the
14923 -------------------
14924 -- Is_Outer_Type --
14925 -------------------
14927 function Is_Outer_Type
(T
: Entity_Id
) return Boolean is
14928 Scop
: Entity_Id
:= Scope
(T
);
14931 if Scope_Depth
(Scop
) < Scope_Depth
(Out_Of
) then
14935 while Scop
/= Standard_Standard
loop
14936 if Scop
= Out_Of
then
14939 Scop
:= Scope
(Scop
);
14947 -- Start of processing for Move_Freeze_Nodes
14954 -- First remove the freeze nodes that may appear before all other
14958 while Present
(Decl
)
14959 and then Nkind
(Decl
) = N_Freeze_Entity
14960 and then Is_Outer_Type
(Entity
(Decl
))
14962 Decl
:= Remove_Head
(L
);
14963 Insert_After
(Next_Node
, Decl
);
14964 Set_Analyzed
(Decl
, False);
14969 -- Next scan the list of declarations and remove each freeze node that
14970 -- appears ahead of the current node.
14972 while Present
(Decl
) loop
14973 while Present
(Next
(Decl
))
14974 and then Nkind
(Next
(Decl
)) = N_Freeze_Entity
14975 and then Is_Outer_Type
(Entity
(Next
(Decl
)))
14977 Next_Decl
:= Remove_Next
(Decl
);
14978 Insert_After
(Next_Node
, Next_Decl
);
14979 Set_Analyzed
(Next_Decl
, False);
14980 Next_Node
:= Next_Decl
;
14983 -- If the declaration is a nested package or concurrent type, then
14984 -- recurse. Nested generic packages will have been processed from the
14987 case Nkind
(Decl
) is
14988 when N_Package_Declaration
=>
14989 Spec
:= Specification
(Decl
);
14991 when N_Task_Type_Declaration
=>
14992 Spec
:= Task_Definition
(Decl
);
14994 when N_Protected_Type_Declaration
=>
14995 Spec
:= Protected_Definition
(Decl
);
15001 if Present
(Spec
) then
15002 Move_Freeze_Nodes
(Out_Of
, Next_Node
, Visible_Declarations
(Spec
));
15003 Move_Freeze_Nodes
(Out_Of
, Next_Node
, Private_Declarations
(Spec
));
15008 end Move_Freeze_Nodes
;
15014 function Next_Assoc
(E
: Assoc_Ptr
) return Assoc_Ptr
is
15016 return Generic_Renamings
.Table
(E
).Next_In_HTable
;
15019 ------------------------
15020 -- Preanalyze_Actuals --
15021 ------------------------
15023 procedure Preanalyze_Actuals
(N
: Node_Id
; Inst
: Entity_Id
:= Empty
) is
15024 procedure Perform_Appropriate_Analysis
(N
: Node_Id
);
15025 -- Determine if the actuals we are analyzing come from a generic
15026 -- instantiation that is a library unit and dispatch accordingly.
15028 ----------------------------------
15029 -- Perform_Appropriate_Analysis --
15030 ----------------------------------
15032 procedure Perform_Appropriate_Analysis
(N
: Node_Id
) is
15034 -- When we have a library instantiation we cannot allow any expansion
15035 -- to occur, since there may be no place to put it. Instead, in that
15036 -- case we perform a preanalysis of the actual.
15038 if Present
(Inst
) and then Is_Compilation_Unit
(Inst
) then
15043 end Perform_Appropriate_Analysis
;
15047 Errs
: constant Nat
:= Serious_Errors_Detected
;
15052 Cur
: Entity_Id
:= Empty
;
15053 -- Current homograph of the instance name
15055 Vis
: Boolean := False;
15056 -- Saved visibility status of the current homograph
15058 -- Start of processing for Preanalyze_Actuals
15061 Assoc
:= First
(Generic_Associations
(N
));
15063 -- If the instance is a child unit, its name may hide an outer homonym,
15064 -- so make it invisible to perform name resolution on the actuals.
15066 if Nkind
(Defining_Unit_Name
(N
)) = N_Defining_Program_Unit_Name
15068 (Current_Entity
(Defining_Identifier
(Defining_Unit_Name
(N
))))
15070 Cur
:= Current_Entity
(Defining_Identifier
(Defining_Unit_Name
(N
)));
15072 if Is_Compilation_Unit
(Cur
) then
15073 Vis
:= Is_Immediately_Visible
(Cur
);
15074 Set_Is_Immediately_Visible
(Cur
, False);
15080 while Present
(Assoc
) loop
15081 if Nkind
(Assoc
) /= N_Others_Choice
then
15082 Act
:= Explicit_Generic_Actual_Parameter
(Assoc
);
15084 -- Within a nested instantiation, a defaulted actual is an empty
15085 -- association, so nothing to analyze. If the subprogram actual
15086 -- is an attribute, analyze prefix only, because actual is not a
15087 -- complete attribute reference.
15089 -- If actual is an allocator, analyze expression only. The full
15090 -- analysis can generate code, and if instance is a compilation
15091 -- unit we have to wait until the package instance is installed
15092 -- to have a proper place to insert this code.
15094 -- String literals may be operators, but at this point we do not
15095 -- know whether the actual is a formal subprogram or a string.
15100 elsif Nkind
(Act
) = N_Attribute_Reference
then
15101 Perform_Appropriate_Analysis
(Prefix
(Act
));
15103 elsif Nkind
(Act
) = N_Explicit_Dereference
then
15104 Perform_Appropriate_Analysis
(Prefix
(Act
));
15106 elsif Nkind
(Act
) = N_Allocator
then
15108 Expr
: constant Node_Id
:= Expression
(Act
);
15111 if Nkind
(Expr
) = N_Subtype_Indication
then
15112 Perform_Appropriate_Analysis
(Subtype_Mark
(Expr
));
15114 -- Analyze separately each discriminant constraint, when
15115 -- given with a named association.
15121 Constr
:= First
(Constraints
(Constraint
(Expr
)));
15122 while Present
(Constr
) loop
15123 if Nkind
(Constr
) = N_Discriminant_Association
then
15124 Perform_Appropriate_Analysis
15125 (Expression
(Constr
));
15127 Perform_Appropriate_Analysis
(Constr
);
15135 Perform_Appropriate_Analysis
(Expr
);
15139 elsif Nkind
(Act
) /= N_Operator_Symbol
then
15140 Perform_Appropriate_Analysis
(Act
);
15142 -- Within a package instance, mark actuals that are limited
15143 -- views, so their use can be moved to the body of the
15146 if Is_Entity_Name
(Act
)
15147 and then Is_Type
(Entity
(Act
))
15148 and then From_Limited_With
(Entity
(Act
))
15149 and then Present
(Inst
)
15151 Append_Elmt
(Entity
(Act
), Incomplete_Actuals
(Inst
));
15155 if Errs
/= Serious_Errors_Detected
then
15157 -- Do a minimal analysis of the generic, to prevent spurious
15158 -- warnings complaining about the generic being unreferenced,
15159 -- before abandoning the instantiation.
15161 Perform_Appropriate_Analysis
(Name
(N
));
15163 if Is_Entity_Name
(Name
(N
))
15164 and then Etype
(Name
(N
)) /= Any_Type
15166 Generate_Reference
(Entity
(Name
(N
)), Name
(N
));
15167 Set_Is_Instantiated
(Entity
(Name
(N
)));
15170 if Present
(Cur
) then
15172 -- For the case of a child instance hiding an outer homonym,
15173 -- provide additional warning which might explain the error.
15175 Set_Is_Immediately_Visible
(Cur
, Vis
);
15177 ("& hides outer unit with the same name??",
15178 N
, Defining_Unit_Name
(N
));
15181 Abandon_Instantiation
(Act
);
15188 if Present
(Cur
) then
15189 Set_Is_Immediately_Visible
(Cur
, Vis
);
15191 end Preanalyze_Actuals
;
15193 -------------------------------
15194 -- Provide_Completing_Bodies --
15195 -------------------------------
15197 procedure Provide_Completing_Bodies
(N
: Node_Id
) is
15198 procedure Build_Completing_Body
(Subp_Decl
: Node_Id
);
15199 -- Generate the completing body for subprogram declaration Subp_Decl
15201 procedure Provide_Completing_Bodies_In
(Decls
: List_Id
);
15202 -- Generating completing bodies for all subprograms found in declarative
15205 ---------------------------
15206 -- Build_Completing_Body --
15207 ---------------------------
15209 procedure Build_Completing_Body
(Subp_Decl
: Node_Id
) is
15210 Loc
: constant Source_Ptr
:= Sloc
(Subp_Decl
);
15211 Subp_Id
: constant Entity_Id
:= Defining_Entity
(Subp_Decl
);
15215 -- Nothing to do if the subprogram already has a completing body
15217 if Present
(Corresponding_Body
(Subp_Decl
)) then
15220 -- Mark the function as having a valid return statement even though
15221 -- the body contains a single raise statement.
15223 elsif Ekind
(Subp_Id
) = E_Function
then
15224 Set_Return_Present
(Subp_Id
);
15227 -- Clone the specification to obtain new entities and reset the only
15230 Spec
:= Copy_Subprogram_Spec
(Specification
(Subp_Decl
));
15231 Set_Generic_Parent
(Spec
, Empty
);
15234 -- function Func ... return ... is
15236 -- procedure Proc ... is
15238 -- raise Program_Error with "access before elaboration";
15241 Insert_After_And_Analyze
(Subp_Decl
,
15242 Make_Subprogram_Body
(Loc
,
15243 Specification
=> Spec
,
15244 Declarations
=> New_List
,
15245 Handled_Statement_Sequence
=>
15246 Make_Handled_Sequence_Of_Statements
(Loc
,
15247 Statements
=> New_List
(
15248 Make_Raise_Program_Error
(Loc
,
15249 Reason
=> PE_Access_Before_Elaboration
)))));
15250 end Build_Completing_Body
;
15252 ----------------------------------
15253 -- Provide_Completing_Bodies_In --
15254 ----------------------------------
15256 procedure Provide_Completing_Bodies_In
(Decls
: List_Id
) is
15260 if Present
(Decls
) then
15261 Decl
:= First
(Decls
);
15262 while Present
(Decl
) loop
15263 Provide_Completing_Bodies
(Decl
);
15267 end Provide_Completing_Bodies_In
;
15273 -- Start of processing for Provide_Completing_Bodies
15276 if Nkind
(N
) = N_Package_Declaration
then
15277 Spec
:= Specification
(N
);
15279 Push_Scope
(Defining_Entity
(N
));
15280 Provide_Completing_Bodies_In
(Visible_Declarations
(Spec
));
15281 Provide_Completing_Bodies_In
(Private_Declarations
(Spec
));
15284 elsif Nkind
(N
) = N_Subprogram_Declaration
then
15285 Build_Completing_Body
(N
);
15287 end Provide_Completing_Bodies
;
15289 -------------------
15290 -- Remove_Parent --
15291 -------------------
15293 procedure Remove_Parent
(In_Body
: Boolean := False) is
15294 S
: Entity_Id
:= Current_Scope
;
15295 -- S is the scope containing the instantiation just completed. The scope
15296 -- stack contains the parent instances of the instantiation, followed by
15305 -- After child instantiation is complete, remove from scope stack the
15306 -- extra copy of the current scope, and then remove parent instances.
15308 if not In_Body
then
15311 while Current_Scope
/= S
loop
15312 P
:= Current_Scope
;
15313 End_Package_Scope
(Current_Scope
);
15315 if In_Open_Scopes
(P
) then
15316 E
:= First_Entity
(P
);
15317 while Present
(E
) loop
15318 Set_Is_Immediately_Visible
(E
, True);
15322 -- If instantiation is declared in a block, it is the enclosing
15323 -- scope that might be a parent instance. Note that only one
15324 -- block can be involved, because the parent instances have
15325 -- been installed within it.
15327 if Ekind
(P
) = E_Block
then
15328 Cur_P
:= Scope
(P
);
15333 if Is_Generic_Instance
(Cur_P
) and then P
/= Current_Scope
then
15334 -- We are within an instance of some sibling. Retain
15335 -- visibility of parent, for proper subsequent cleanup, and
15336 -- reinstall private declarations as well.
15338 Set_In_Private_Part
(P
);
15339 Install_Private_Declarations
(P
);
15342 -- If the ultimate parent is a top-level unit recorded in
15343 -- Instance_Parent_Unit, then reset its visibility to what it was
15344 -- before instantiation. (It's not clear what the purpose is of
15345 -- testing whether Scope (P) is In_Open_Scopes, but that test was
15346 -- present before the ultimate parent test was added.???)
15348 elsif not In_Open_Scopes
(Scope
(P
))
15349 or else (P
= Instance_Parent_Unit
15350 and then not Parent_Unit_Visible
)
15352 Set_Is_Immediately_Visible
(P
, False);
15354 -- If the current scope is itself an instantiation of a generic
15355 -- nested within P, and we are in the private part of body of this
15356 -- instantiation, restore the full views of P, that were removed
15357 -- in End_Package_Scope above. This obscure case can occur when a
15358 -- subunit of a generic contains an instance of a child unit of
15359 -- its generic parent unit.
15361 elsif S
= Current_Scope
and then Is_Generic_Instance
(S
)
15362 and then (In_Package_Body
(S
) or else In_Private_Part
(S
))
15365 Par
: constant Entity_Id
:=
15366 Generic_Parent
(Package_Specification
(S
));
15369 and then P
= Scope
(Par
)
15371 Set_In_Private_Part
(P
);
15372 Install_Private_Declarations
(P
);
15378 -- Reset visibility of entities in the enclosing scope
15380 Set_Is_Hidden_Open_Scope
(Current_Scope
, False);
15382 Hidden
:= First_Elmt
(Hidden_Entities
);
15383 while Present
(Hidden
) loop
15384 Set_Is_Immediately_Visible
(Node
(Hidden
), True);
15385 Next_Elmt
(Hidden
);
15389 -- Each body is analyzed separately, and there is no context that
15390 -- needs preserving from one body instance to the next, so remove all
15391 -- parent scopes that have been installed.
15393 while Present
(S
) loop
15394 End_Package_Scope
(S
);
15395 Set_Is_Immediately_Visible
(S
, False);
15396 S
:= Current_Scope
;
15397 exit when S
= Standard_Standard
;
15402 -----------------------------------
15403 -- Requires_Conformance_Checking --
15404 -----------------------------------
15406 function Requires_Conformance_Checking
(N
: Node_Id
) return Boolean is
15408 -- No conformance checking required if the generic actual part is empty,
15409 -- or is a box or an others_clause (necessarily with a box).
15411 return Present
(Generic_Associations
(N
))
15412 and then not Box_Present
(N
)
15413 and then Nkind
(First
(Generic_Associations
(N
))) /= N_Others_Choice
;
15414 end Requires_Conformance_Checking
;
15420 procedure Restore_Env
is
15421 Saved
: Instance_Env
renames Instance_Envs
.Table
(Instance_Envs
.Last
);
15424 if No
(Current_Instantiated_Parent
.Act_Id
) then
15425 -- Restore environment after subprogram inlining
15427 Restore_Private_Views
(Empty
);
15430 Current_Instantiated_Parent
:= Saved
.Instantiated_Parent
;
15431 Exchanged_Views
:= Saved
.Exchanged_Views
;
15432 Hidden_Entities
:= Saved
.Hidden_Entities
;
15433 Current_Sem_Unit
:= Saved
.Current_Sem_Unit
;
15434 Parent_Unit_Visible
:= Saved
.Parent_Unit_Visible
;
15435 Instance_Parent_Unit
:= Saved
.Instance_Parent_Unit
;
15437 Restore_Config_Switches
(Saved
.Switches
);
15439 Instance_Envs
.Decrement_Last
;
15442 ---------------------------
15443 -- Restore_Private_Views --
15444 ---------------------------
15446 procedure Restore_Private_Views
15447 (Pack_Id
: Entity_Id
;
15448 Is_Package
: Boolean := True)
15453 Dep_Elmt
: Elmt_Id
;
15456 procedure Restore_Nested_Formal
(Formal
: Entity_Id
);
15457 -- Hide the generic formals of formal packages declared with box which
15458 -- were reachable in the current instantiation.
15460 ---------------------------
15461 -- Restore_Nested_Formal --
15462 ---------------------------
15464 procedure Restore_Nested_Formal
(Formal
: Entity_Id
) is
15465 pragma Assert
(Ekind
(Formal
) = E_Package
);
15468 if Present
(Renamed_Entity
(Formal
))
15469 and then Denotes_Formal_Package
(Renamed_Entity
(Formal
), True)
15473 elsif Present
(Associated_Formal_Package
(Formal
)) then
15474 Ent
:= First_Entity
(Formal
);
15475 while Present
(Ent
) loop
15476 exit when Ekind
(Ent
) = E_Package
15477 and then Renamed_Entity
(Ent
) = Renamed_Entity
(Formal
);
15479 Set_Is_Hidden
(Ent
);
15480 Set_Is_Potentially_Use_Visible
(Ent
, False);
15482 -- If package, then recurse
15484 if Ekind
(Ent
) = E_Package
then
15485 Restore_Nested_Formal
(Ent
);
15491 end Restore_Nested_Formal
;
15493 -- Start of processing for Restore_Private_Views
15496 M
:= First_Elmt
(Exchanged_Views
);
15497 while Present
(M
) loop
15500 -- Subtypes of types whose views have been exchanged, and that are
15501 -- defined within the instance, were not on the Private_Dependents
15502 -- list on entry to the instance, so they have to be exchanged
15503 -- explicitly now, in order to remain consistent with the view of the
15506 if Ekind
(Typ
) in E_Private_Type
15507 | E_Limited_Private_Type
15508 | E_Record_Type_With_Private
15510 Dep_Elmt
:= First_Elmt
(Private_Dependents
(Typ
));
15511 while Present
(Dep_Elmt
) loop
15512 Dep_Typ
:= Node
(Dep_Elmt
);
15514 if Scope
(Dep_Typ
) = Pack_Id
15515 and then Present
(Full_View
(Dep_Typ
))
15517 Replace_Elmt
(Dep_Elmt
, Full_View
(Dep_Typ
));
15518 Exchange_Declarations
(Dep_Typ
);
15521 Next_Elmt
(Dep_Elmt
);
15525 Exchange_Declarations
(Typ
);
15529 if No
(Pack_Id
) then
15533 -- Make the generic formal parameters private, and make the formal types
15534 -- into subtypes of the actuals again.
15536 E
:= First_Entity
(Pack_Id
);
15537 while Present
(E
) loop
15538 Set_Is_Hidden
(E
, True);
15541 and then Nkind
(Parent
(E
)) = N_Subtype_Declaration
15543 -- Always preserve the flag Is_Generic_Actual_Type for GNATprove,
15544 -- as it is needed to identify the subtype with the type it
15545 -- renames, when there are conversions between access types
15548 if GNATprove_Mode
then
15551 -- If the actual for E is itself a generic actual type from
15552 -- an enclosing instance, E is still a generic actual type
15553 -- outside of the current instance. This matter when resolving
15554 -- an overloaded call that may be ambiguous in the enclosing
15555 -- instance, when two of its actuals coincide.
15557 elsif Is_Entity_Name
(Subtype_Indication
(Parent
(E
)))
15558 and then Is_Generic_Actual_Type
15559 (Entity
(Subtype_Indication
(Parent
(E
))))
15563 Set_Is_Generic_Actual_Type
(E
, False);
15565 -- It might seem reasonable to clear the Is_Generic_Actual_Type
15566 -- flag also on the Full_View if the type is private, since it
15567 -- was set also on this Full_View. However, this flag is relied
15568 -- upon by Covers to spot "types exported from instantiations"
15569 -- which are implicit Full_Views built for instantiations made
15570 -- on private types and we get type mismatches if we do it when
15571 -- the block exchanging the declarations below triggers ???
15573 -- if Is_Private_Type (E) and then Present (Full_View (E)) then
15574 -- Set_Is_Generic_Actual_Type (Full_View (E), False);
15578 -- An unusual case of aliasing: the actual may also be directly
15579 -- visible in the generic, and be private there, while it is fully
15580 -- visible in the context of the instance. The internal subtype
15581 -- is private in the instance but has full visibility like its
15582 -- parent in the enclosing scope. This enforces the invariant that
15583 -- the privacy status of all private dependents of a type coincide
15584 -- with that of the parent type. This can only happen when a
15585 -- generic child unit is instantiated within a sibling.
15587 if Is_Private_Type
(E
)
15588 and then not Is_Private_Type
(Etype
(E
))
15590 Exchange_Declarations
(E
);
15593 elsif Ekind
(E
) = E_Package
then
15595 -- The end of the renaming list is the renaming of the generic
15596 -- package itself. If the instance is a subprogram, all entities
15597 -- in the corresponding package are renamings. If this entity is
15598 -- a formal package, make its own formals private as well. The
15599 -- actual in this case is itself the renaming of an instantiation.
15600 -- If the entity is not a package renaming, it is the entity
15601 -- created to validate formal package actuals: ignore it.
15603 -- If the actual is itself a formal package for the enclosing
15604 -- generic, or the actual for such a formal package, it remains
15605 -- visible on exit from the instance, and therefore nothing needs
15606 -- to be done either, except to keep it accessible.
15608 if Is_Package
and then Renamed_Entity
(E
) = Pack_Id
then
15611 elsif Nkind
(Parent
(E
)) /= N_Package_Renaming_Declaration
then
15615 Denotes_Formal_Package
(Renamed_Entity
(E
), True, Pack_Id
)
15617 Set_Is_Hidden
(E
, False);
15621 Act_P
: constant Entity_Id
:= Renamed_Entity
(E
);
15625 Id
:= First_Entity
(Act_P
);
15627 and then Id
/= First_Private_Entity
(Act_P
)
15629 exit when Ekind
(Id
) = E_Package
15630 and then Renamed_Entity
(Id
) = Act_P
;
15632 Set_Is_Hidden
(Id
, True);
15633 Set_Is_Potentially_Use_Visible
(Id
, In_Use
(Act_P
));
15635 if Ekind
(Id
) = E_Package
then
15636 Restore_Nested_Formal
(Id
);
15647 end Restore_Private_Views
;
15654 (Gen_Unit
: Entity_Id
;
15655 Act_Unit
: Entity_Id
)
15659 Set_Instance_Env
(Gen_Unit
, Act_Unit
);
15662 ----------------------------
15663 -- Save_Global_References --
15664 ----------------------------
15666 procedure Save_Global_References
(Templ
: Node_Id
) is
15668 -- ??? it is horrible to use global variables in highly recursive code
15671 -- The entity of the current associated node
15673 Gen_Scope
: Entity_Id
;
15674 -- The scope of the generic for which references are being saved
15677 -- The current associated node
15679 function Is_Global
(E
: Entity_Id
) return Boolean;
15680 -- Check whether entity is defined outside of generic unit. Examine the
15681 -- scope of an entity, and the scope of the scope, etc, until we find
15682 -- either Standard, in which case the entity is global, or the generic
15683 -- unit itself, which indicates that the entity is local. If the entity
15684 -- is the generic unit itself, as in the case of a recursive call, or
15685 -- the enclosing generic unit, if different from the current scope, then
15686 -- it is local as well, because it will be replaced at the point of
15687 -- instantiation. On the other hand, if it is a reference to a child
15688 -- unit of a common ancestor, which appears in an instantiation, it is
15689 -- global because it is used to denote a specific compilation unit at
15690 -- the time the instantiations will be analyzed.
15692 procedure Qualify_Universal_Operands
15694 Func_Call
: Node_Id
);
15695 -- Op denotes a binary or unary operator in generic template Templ. Node
15696 -- Func_Call is the function call alternative of the operator within the
15697 -- the analyzed copy of the template. Change each operand which yields a
15698 -- universal type by wrapping it into a qualified expression
15700 -- Actual_Typ'(Operand)
15702 -- where Actual_Typ is the type of corresponding actual parameter of
15703 -- Operand in Func_Call.
15705 procedure Reset_Entity
(N
: Node_Id
);
15706 -- Save semantic information on global entity so that it is not resolved
15707 -- again at instantiation time.
15709 procedure Save_Entity_Descendants
(N
: Node_Id
);
15710 -- Apply Save_Global_References to the two syntactic descendants of
15711 -- non-terminal nodes that carry an Associated_Node and are processed
15712 -- through Reset_Entity. Once the global entity (if any) has been
15713 -- captured together with its type, only two syntactic descendants need
15714 -- to be traversed to complete the processing of the tree rooted at N.
15715 -- This applies to Selected_Components, Expanded_Names, and to Operator
15716 -- nodes. N can also be a character literal, identifier, or operator
15717 -- symbol node, but the call has no effect in these cases.
15719 procedure Save_Global_Defaults
(N1
: Node_Id
; N2
: Node_Id
);
15720 -- Default actuals in nested instances must be handled specially
15721 -- because there is no link to them from the original tree. When an
15722 -- actual subprogram is given by a default, we add an explicit generic
15723 -- association for it in the instantiation node. When we save the
15724 -- global references on the name of the instance, we recover the list
15725 -- of generic associations, and add an explicit one to the original
15726 -- generic tree, through which a global actual can be preserved.
15727 -- Similarly, if a child unit is instantiated within a sibling, in the
15728 -- context of the parent, we must preserve the identifier of the parent
15729 -- so that it can be properly resolved in a subsequent instantiation.
15731 procedure Save_Global_Descendant
(D
: Union_Id
);
15732 -- Apply Save_References recursively to the descendants of node D
15734 procedure Save_References
(N
: Node_Id
);
15735 -- This is the recursive procedure that does the work, once the
15736 -- enclosing generic scope has been established.
15738 procedure Set_Global_Type
(N
: Node_Id
; N2
: Node_Id
);
15739 -- If the type of N2 is global to the generic unit, save the type in
15740 -- the generic node. Just as we perform name capture for explicit
15741 -- references within the generic, we must capture the global types
15742 -- of local entities because they may participate in resolution in
15749 function Is_Global
(E
: Entity_Id
) return Boolean is
15752 function Is_Instance_Node
(Decl
: Node_Id
) return Boolean;
15753 -- Determine whether the parent node of a reference to a child unit
15754 -- denotes an instantiation or a formal package, in which case the
15755 -- reference to the child unit is global, even if it appears within
15756 -- the current scope (e.g. when the instance appears within the body
15757 -- of an ancestor).
15759 ----------------------
15760 -- Is_Instance_Node --
15761 ----------------------
15763 function Is_Instance_Node
(Decl
: Node_Id
) return Boolean is
15765 return Nkind
(Decl
) in N_Generic_Instantiation
15767 Nkind
(Original_Node
(Decl
)) = N_Formal_Package_Declaration
;
15768 end Is_Instance_Node
;
15770 -- Start of processing for Is_Global
15773 if E
= Gen_Scope
then
15776 elsif E
= Standard_Standard
then
15779 -- E should be an entity, but it is not always
15781 elsif Nkind
(E
) not in N_Entity
then
15784 elsif Nkind
(E
) /= N_Expanded_Name
15785 and then Is_Child_Unit
(E
)
15786 and then (Is_Instance_Node
(Parent
(N2
))
15787 or else (Nkind
(Parent
(N2
)) = N_Expanded_Name
15788 and then N2
= Selector_Name
(Parent
(N2
))
15790 Is_Instance_Node
(Parent
(Parent
(N2
)))))
15795 -- E may be an expanded name - typically an operator - in which
15796 -- case we must find its enclosing scope since expanded names
15797 -- don't have corresponding scopes.
15799 if Nkind
(E
) = N_Expanded_Name
then
15800 Se
:= Find_Enclosing_Scope
(E
);
15802 -- Otherwise, E is an entity and will have Scope set
15808 while Se
/= Gen_Scope
loop
15809 if Se
= Standard_Standard
then
15820 --------------------------------
15821 -- Qualify_Universal_Operands --
15822 --------------------------------
15824 procedure Qualify_Universal_Operands
15826 Func_Call
: Node_Id
)
15828 procedure Qualify_Operand
(Opnd
: Node_Id
; Actual
: Node_Id
);
15829 -- Rewrite operand Opnd as a qualified expression of the form
15831 -- Actual_Typ'(Opnd)
15833 -- where Actual is the corresponding actual parameter of Opnd in
15834 -- function call Func_Call.
15836 function Qualify_Type
15838 Typ
: Entity_Id
) return Node_Id
;
15839 -- Qualify type Typ by creating a selected component of the form
15841 -- Scope_Of_Typ.Typ
15843 ---------------------
15844 -- Qualify_Operand --
15845 ---------------------
15847 procedure Qualify_Operand
(Opnd
: Node_Id
; Actual
: Node_Id
) is
15848 Loc
: constant Source_Ptr
:= Sloc
(Opnd
);
15849 Typ
: constant Entity_Id
:= Etype
(Actual
);
15854 -- Qualify the operand when it is of a universal type. Note that
15855 -- the template is unanalyzed and it is not possible to directly
15856 -- query the type. This transformation is not done when the type
15857 -- of the actual is internally generated because the type will be
15858 -- regenerated in the instance.
15860 if Yields_Universal_Type
(Opnd
)
15861 and then Comes_From_Source
(Typ
)
15862 and then not Is_Hidden
(Typ
)
15864 -- The type of the actual may be a global reference. Save this
15865 -- information by creating a reference to it.
15867 if Is_Global
(Typ
) then
15868 Mark
:= New_Occurrence_Of
(Typ
, Loc
);
15870 -- Otherwise rely on resolution to find the proper type within
15874 Mark
:= Qualify_Type
(Loc
, Typ
);
15878 Make_Qualified_Expression
(Loc
,
15879 Subtype_Mark
=> Mark
,
15880 Expression
=> Relocate_Node
(Opnd
));
15882 -- Mark the qualification to distinguish it from other source
15883 -- constructs and signal the instantiation mechanism that this
15884 -- node requires special processing. See Copy_Generic_Node for
15887 Set_Is_Qualified_Universal_Literal
(Qual
);
15889 Rewrite
(Opnd
, Qual
);
15891 end Qualify_Operand
;
15897 function Qualify_Type
15899 Typ
: Entity_Id
) return Node_Id
15901 Scop
: constant Entity_Id
:= Scope
(Typ
);
15905 Result
:= Make_Identifier
(Loc
, Chars
(Typ
));
15907 if Present
(Scop
) and then not Is_Generic_Unit
(Scop
) then
15909 Make_Selected_Component
(Loc
,
15910 Prefix
=> Make_Identifier
(Loc
, Chars
(Scop
)),
15911 Selector_Name
=> Result
);
15919 Actuals
: constant List_Id
:= Parameter_Associations
(Func_Call
);
15921 -- Start of processing for Qualify_Universal_Operands
15924 if Nkind
(Op
) in N_Binary_Op
then
15925 Qualify_Operand
(Left_Opnd
(Op
), First
(Actuals
));
15926 Qualify_Operand
(Right_Opnd
(Op
), Next
(First
(Actuals
)));
15928 elsif Nkind
(Op
) in N_Unary_Op
then
15929 Qualify_Operand
(Right_Opnd
(Op
), First
(Actuals
));
15931 end Qualify_Universal_Operands
;
15937 procedure Reset_Entity
(N
: Node_Id
) is
15938 function Top_Ancestor
(E
: Entity_Id
) return Entity_Id
;
15939 -- Find the ultimate ancestor of the current unit. If it is not a
15940 -- generic unit, then the name of the current unit in the prefix of
15941 -- an expanded name must be replaced with its generic homonym to
15942 -- ensure that it will be properly resolved in an instance.
15948 function Top_Ancestor
(E
: Entity_Id
) return Entity_Id
is
15953 while Is_Child_Unit
(Par
) loop
15954 Par
:= Scope
(Par
);
15960 -- Start of processing for Reset_Entity
15963 N2
:= Get_Associated_Node
(N
);
15966 if Present
(E
) then
15968 -- If the node is an entry call to an entry in an enclosing task,
15969 -- it is rewritten as a selected component. No global entity to
15970 -- preserve in this case, since the expansion will be redone in
15973 if Nkind
(E
) not in N_Entity
then
15974 Set_Associated_Node
(N
, Empty
);
15975 Set_Etype
(N
, Empty
);
15979 -- If the entity is an itype created as a subtype of an access
15980 -- type with a null exclusion restore source entity for proper
15981 -- visibility. The itype will be created anew in the instance.
15984 and then Ekind
(E
) = E_Access_Subtype
15985 and then Is_Entity_Name
(N
)
15986 and then Chars
(Etype
(E
)) = Chars
(N
)
15989 Set_Entity
(N2
, E
);
15993 if Is_Global
(E
) then
15994 Set_Global_Type
(N
, N2
);
15996 elsif Nkind
(N
) = N_Op_Concat
15997 and then Is_Generic_Type
(Etype
(N2
))
15998 and then (Base_Type
(Etype
(Right_Opnd
(N2
))) = Etype
(N2
)
16000 Base_Type
(Etype
(Left_Opnd
(N2
))) = Etype
(N2
))
16001 and then Is_Intrinsic_Subprogram
(E
)
16005 -- Entity is local. Mark generic node as unresolved. Note that now
16006 -- it does not have an entity.
16009 Set_Associated_Node
(N
, Empty
);
16010 Set_Etype
(N
, Empty
);
16013 if Nkind
(Parent
(N
)) in N_Generic_Instantiation
16014 and then N
= Name
(Parent
(N
))
16016 Save_Global_Defaults
(Parent
(N
), Parent
(N2
));
16019 elsif Nkind
(Parent
(N
)) = N_Selected_Component
16020 and then Nkind
(Parent
(N2
)) = N_Expanded_Name
16022 -- In case of previous errors, the tree might be malformed
16024 if No
(Entity
(Parent
(N2
))) then
16027 elsif Is_Global
(Entity
(Parent
(N2
))) then
16028 Change_Selected_Component_To_Expanded_Name
(Parent
(N
));
16029 Set_Associated_Node
(Parent
(N
), Parent
(N2
));
16030 Set_Global_Type
(Parent
(N
), Parent
(N2
));
16031 Save_Entity_Descendants
(N
);
16033 -- If this is a reference to the current generic entity, replace
16034 -- by the name of the generic homonym of the current package. This
16035 -- is because in an instantiation Par.P.Q will not resolve to the
16036 -- name of the instance, whose enclosing scope is not necessarily
16037 -- Par. We use the generic homonym rather that the name of the
16038 -- generic itself because it may be hidden by a local declaration.
16040 elsif In_Open_Scopes
(Entity
(Parent
(N2
)))
16042 Is_Generic_Unit
(Top_Ancestor
(Entity
(Prefix
(Parent
(N2
)))))
16044 if Ekind
(Entity
(Parent
(N2
))) = E_Generic_Package
then
16045 Rewrite
(Parent
(N
),
16046 Make_Identifier
(Sloc
(N
),
16048 Chars
(Generic_Homonym
(Entity
(Parent
(N2
))))));
16050 Rewrite
(Parent
(N
),
16051 Make_Identifier
(Sloc
(N
),
16052 Chars
=> Chars
(Selector_Name
(Parent
(N2
)))));
16056 if Nkind
(Parent
(Parent
(N
))) in N_Generic_Instantiation
16057 and then Parent
(N
) = Name
(Parent
(Parent
(N
)))
16059 Save_Global_Defaults
16060 (Parent
(Parent
(N
)), Parent
(Parent
(N2
)));
16063 -- A selected component may denote a static constant that has been
16064 -- folded. If the static constant is global to the generic, capture
16065 -- its value. Otherwise the folding will happen in any instantiation.
16067 elsif Nkind
(Parent
(N
)) = N_Selected_Component
16068 and then Nkind
(Parent
(N2
)) in N_Integer_Literal | N_Real_Literal
16070 if Present
(Entity
(Original_Node
(Parent
(N2
))))
16071 and then Is_Global
(Entity
(Original_Node
(Parent
(N2
))))
16073 Rewrite
(Parent
(N
), New_Copy
(Parent
(N2
)));
16074 Set_Analyzed
(Parent
(N
), False);
16077 -- A selected component may be transformed into a parameterless
16078 -- function call. If the called entity is global, rewrite the node
16079 -- appropriately, i.e. as an extended name for the global entity.
16081 elsif Nkind
(Parent
(N
)) = N_Selected_Component
16082 and then Nkind
(Parent
(N2
)) = N_Function_Call
16083 and then N
= Selector_Name
(Parent
(N
))
16085 if No
(Parameter_Associations
(Parent
(N2
))) then
16086 if Is_Global
(Entity
(Name
(Parent
(N2
)))) then
16087 Change_Selected_Component_To_Expanded_Name
(Parent
(N
));
16088 Set_Associated_Node
(Parent
(N
), Name
(Parent
(N2
)));
16089 Set_Global_Type
(Parent
(N
), Name
(Parent
(N2
)));
16090 Save_Entity_Descendants
(N
);
16093 Set_Is_Prefixed_Call
(Parent
(N
));
16094 Set_Associated_Node
(N
, Empty
);
16095 Set_Etype
(N
, Empty
);
16098 -- In Ada 2005, X.F may be a call to a primitive operation,
16099 -- rewritten as F (X). This rewriting will be done again in an
16100 -- instance, so keep the original node. Global entities will be
16101 -- captured as for other constructs. Indicate that this must
16102 -- resolve as a call, to prevent accidental overloading in the
16103 -- instance, if both a component and a primitive operation appear
16107 Set_Is_Prefixed_Call
(Parent
(N
));
16110 -- Entity is local. Reset in generic unit, so that node is resolved
16111 -- anew at the point of instantiation.
16114 Set_Associated_Node
(N
, Empty
);
16115 Set_Etype
(N
, Empty
);
16119 -----------------------------
16120 -- Save_Entity_Descendants --
16121 -----------------------------
16123 procedure Save_Entity_Descendants
(N
: Node_Id
) is
16126 when N_Binary_Op
=>
16127 Save_Global_Descendant
(Union_Id
(Left_Opnd
(N
)));
16128 Save_Global_Descendant
(Union_Id
(Right_Opnd
(N
)));
16131 Save_Global_Descendant
(Union_Id
(Right_Opnd
(N
)));
16133 when N_Expanded_Name
16134 | N_Selected_Component
16136 Save_Global_Descendant
(Union_Id
(Prefix
(N
)));
16137 Save_Global_Descendant
(Union_Id
(Selector_Name
(N
)));
16139 when N_Character_Literal
16141 | N_Operator_Symbol
16146 raise Program_Error
;
16148 end Save_Entity_Descendants
;
16150 --------------------------
16151 -- Save_Global_Defaults --
16152 --------------------------
16154 procedure Save_Global_Defaults
(N1
: Node_Id
; N2
: Node_Id
) is
16155 Loc
: constant Source_Ptr
:= Sloc
(N1
);
16156 Assoc2
: constant List_Id
:= Generic_Associations
(N2
);
16157 Gen_Id
: constant Entity_Id
:= Get_Generic_Entity
(N2
);
16164 Actual
: Entity_Id
;
16167 Assoc1
:= Generic_Associations
(N1
);
16169 if Present
(Assoc1
) then
16170 Act1
:= First
(Assoc1
);
16173 Set_Generic_Associations
(N1
, New_List
);
16174 Assoc1
:= Generic_Associations
(N1
);
16177 if Present
(Assoc2
) then
16178 Act2
:= First
(Assoc2
);
16183 while Present
(Act1
) and then Present
(Act2
) loop
16188 -- Find the associations added for default subprograms
16190 if Present
(Act2
) then
16191 while Nkind
(Act2
) /= N_Generic_Association
16192 or else No
(Entity
(Selector_Name
(Act2
)))
16193 or else not Is_Overloadable
(Entity
(Selector_Name
(Act2
)))
16198 -- Add a similar association if the default is global. The
16199 -- renaming declaration for the actual has been analyzed, and
16200 -- its alias is the program it renames. Link the actual in the
16201 -- original generic tree with the node in the analyzed tree.
16203 while Present
(Act2
) loop
16204 Subp
:= Entity
(Selector_Name
(Act2
));
16205 Def
:= Explicit_Generic_Actual_Parameter
(Act2
);
16207 -- Following test is defence against rubbish errors
16209 if No
(Alias
(Subp
)) then
16213 -- Retrieve the resolved actual from the renaming declaration
16214 -- created for the instantiated formal.
16216 Actual
:= Entity
(Name
(Parent
(Parent
(Subp
))));
16217 Set_Entity
(Def
, Actual
);
16218 Set_Etype
(Def
, Etype
(Actual
));
16220 if Is_Global
(Actual
) then
16222 Make_Generic_Association
(Loc
,
16224 New_Occurrence_Of
(Subp
, Loc
),
16225 Explicit_Generic_Actual_Parameter
=>
16226 New_Occurrence_Of
(Actual
, Loc
));
16228 Set_Associated_Node
16229 (Explicit_Generic_Actual_Parameter
(Ndec
), Def
);
16231 Append
(Ndec
, Assoc1
);
16233 -- If there are other defaults, add a dummy association in case
16234 -- there are other defaulted formals with the same name.
16236 elsif Present
(Next
(Act2
)) then
16238 Make_Generic_Association
(Loc
,
16240 New_Occurrence_Of
(Subp
, Loc
),
16241 Explicit_Generic_Actual_Parameter
=> Empty
);
16243 Append
(Ndec
, Assoc1
);
16250 if Nkind
(Name
(N1
)) = N_Identifier
16251 and then Is_Child_Unit
(Gen_Id
)
16252 and then Is_Global
(Gen_Id
)
16253 and then Is_Generic_Unit
(Scope
(Gen_Id
))
16254 and then In_Open_Scopes
(Scope
(Gen_Id
))
16256 -- This is an instantiation of a child unit within a sibling, so
16257 -- that the generic parent is in scope. An eventual instance must
16258 -- occur within the scope of an instance of the parent. Make name
16259 -- in instance into an expanded name, to preserve the identifier
16260 -- of the parent, so it can be resolved subsequently.
16262 Rewrite
(Name
(N2
),
16263 Make_Expanded_Name
(Loc
,
16264 Chars
=> Chars
(Gen_Id
),
16265 Prefix
=> New_Occurrence_Of
(Scope
(Gen_Id
), Loc
),
16266 Selector_Name
=> New_Occurrence_Of
(Gen_Id
, Loc
)));
16267 Set_Entity
(Name
(N2
), Gen_Id
);
16269 Rewrite
(Name
(N1
),
16270 Make_Expanded_Name
(Loc
,
16271 Chars
=> Chars
(Gen_Id
),
16272 Prefix
=> New_Occurrence_Of
(Scope
(Gen_Id
), Loc
),
16273 Selector_Name
=> New_Occurrence_Of
(Gen_Id
, Loc
)));
16275 Set_Associated_Node
(Name
(N1
), Name
(N2
));
16276 Set_Associated_Node
(Prefix
(Name
(N1
)), Empty
);
16277 Set_Associated_Node
16278 (Selector_Name
(Name
(N1
)), Selector_Name
(Name
(N2
)));
16279 Set_Etype
(Name
(N1
), Etype
(Gen_Id
));
16281 end Save_Global_Defaults
;
16283 ----------------------------
16284 -- Save_Global_Descendant --
16285 ----------------------------
16287 procedure Save_Global_Descendant
(D
: Union_Id
) is
16291 if D
in Node_Range
then
16292 if D
= Union_Id
(Empty
) then
16295 elsif Nkind
(Node_Id
(D
)) /= N_Compilation_Unit
then
16296 Save_References
(Node_Id
(D
));
16299 elsif D
in List_Range
then
16300 pragma Assert
(D
/= Union_Id
(No_List
));
16301 -- Because No_List = Empty, which is in Node_Range above
16303 N1
:= First
(List_Id
(D
));
16304 while Present
(N1
) loop
16305 Save_References
(N1
);
16309 -- Element list or other non-node field, nothing to do
16314 end Save_Global_Descendant
;
16316 ---------------------
16317 -- Save_References --
16318 ---------------------
16320 -- This is the recursive procedure that does the work once the enclosing
16321 -- generic scope has been established. We have to treat specially a
16322 -- number of node rewritings that are required by semantic processing
16323 -- and which change the kind of nodes in the generic copy: typically
16324 -- constant-folding, replacing an operator node by a string literal, or
16325 -- a selected component by an expanded name. In each of those cases, the
16326 -- transformation is propagated to the generic unit.
16328 procedure Save_References
(N
: Node_Id
) is
16329 Loc
: constant Source_Ptr
:= Sloc
(N
);
16331 function Requires_Delayed_Save
(Nod
: Node_Id
) return Boolean;
16332 -- Determine whether arbitrary node Nod requires delayed capture of
16333 -- global references within its aspect specifications.
16335 procedure Save_References_In_Aggregate
(N
: Node_Id
);
16336 -- Save all global references in [extension] aggregate node N
16338 procedure Save_References_In_Char_Lit_Or_Op_Symbol
(N
: Node_Id
);
16339 -- Save all global references in a character literal or operator
16340 -- symbol denoted by N.
16342 procedure Save_References_In_Descendants
(N
: Node_Id
);
16343 -- Save all global references in all descendants of node N
16345 procedure Save_References_In_Identifier
(N
: Node_Id
);
16346 -- Save all global references in identifier node N
16348 procedure Save_References_In_Operator
(N
: Node_Id
);
16349 -- Save all global references in operator node N
16351 procedure Save_References_In_Pragma
(Prag
: Node_Id
);
16352 -- Save all global references found within the expression of pragma
16355 ---------------------------
16356 -- Requires_Delayed_Save --
16357 ---------------------------
16359 function Requires_Delayed_Save
(Nod
: Node_Id
) return Boolean is
16361 -- Generic packages and subprograms require delayed capture of
16362 -- global references within their aspects due to the timing of
16363 -- annotation analysis.
16365 if Nkind
(Nod
) in N_Generic_Package_Declaration
16366 | N_Generic_Subprogram_Declaration
16368 | N_Package_Body_Stub
16369 | N_Subprogram_Body
16370 | N_Subprogram_Body_Stub
16372 -- Since the capture of global references is done on the
16373 -- unanalyzed generic template, there is no information around
16374 -- to infer the context. Use the Associated_Entity linkages to
16375 -- peek into the analyzed generic copy and determine what the
16376 -- template corresponds to.
16378 if Nod
= Templ
then
16380 Is_Generic_Declaration_Or_Body
16381 (Unit_Declaration_Node
16382 (Associated_Entity
(Defining_Entity
(Nod
))));
16384 -- Otherwise the generic unit being processed is not the top
16385 -- level template. It is safe to capture of global references
16386 -- within the generic unit because at this point the top level
16387 -- copy is fully analyzed.
16393 -- Otherwise capture the global references without interference
16398 end Requires_Delayed_Save
;
16400 ----------------------------------
16401 -- Save_References_In_Aggregate --
16402 ----------------------------------
16404 procedure Save_References_In_Aggregate
(N
: Node_Id
) is
16406 Qual
: Node_Id
:= Empty
;
16407 Typ
: Entity_Id
:= Empty
;
16410 N2
:= Get_Associated_Node
(N
);
16412 if Present
(N2
) then
16415 -- In an instance within a generic, use the name of the actual
16416 -- and not the original generic parameter. If the actual is
16417 -- global in the current generic it must be preserved for its
16420 if Parent_Kind
(Typ
) = N_Subtype_Declaration
16421 and then Present
(Generic_Parent_Type
(Parent
(Typ
)))
16423 Typ
:= Base_Type
(Typ
);
16424 Set_Etype
(N2
, Typ
);
16428 if No
(N2
) or else No
(Typ
) or else not Is_Global
(Typ
) then
16429 Set_Associated_Node
(N
, Empty
);
16431 -- If the aggregate is an actual in a call, it has been
16432 -- resolved in the current context, to some local type. The
16433 -- enclosing call may have been disambiguated by the aggregate,
16434 -- and this disambiguation might fail at instantiation time
16435 -- because the type to which the aggregate did resolve is not
16436 -- preserved. In order to preserve some of this information,
16437 -- wrap the aggregate in a qualified expression, using the id
16438 -- of its type. For further disambiguation we qualify the type
16439 -- name with its scope (if visible and not hidden by a local
16440 -- homograph) because both id's will have corresponding
16441 -- entities in an instance. This resolves most of the problems
16442 -- with missing type information on aggregates in instances.
16445 and then Nkind
(N2
) = Nkind
(N
)
16446 and then Nkind
(Parent
(N2
)) in N_Subprogram_Call
16447 and then Present
(Typ
)
16448 and then Comes_From_Source
(Typ
)
16450 Nam
:= Make_Identifier
(Loc
, Chars
(Typ
));
16452 if Is_Immediately_Visible
(Scope
(Typ
))
16454 (not In_Open_Scopes
(Scope
(Typ
))
16455 or else Current_Entity
(Scope
(Typ
)) = Scope
(Typ
))
16458 Make_Selected_Component
(Loc
,
16460 Make_Identifier
(Loc
, Chars
(Scope
(Typ
))),
16461 Selector_Name
=> Nam
);
16465 Make_Qualified_Expression
(Loc
,
16466 Subtype_Mark
=> Nam
,
16467 Expression
=> Relocate_Node
(N
));
16471 if Nkind
(N
) = N_Aggregate
then
16472 Save_Global_Descendant
(Union_Id
(Aggregate_Bounds
(N
)));
16474 elsif Nkind
(N
) = N_Extension_Aggregate
then
16475 Save_Global_Descendant
(Union_Id
(Ancestor_Part
(N
)));
16478 pragma Assert
(False);
16481 Save_Global_Descendant
(Union_Id
(Expressions
(N
)));
16482 Save_Global_Descendant
(Union_Id
(Component_Associations
(N
)));
16483 Save_Global_Descendant
(Union_Id
(Etype
(N
)));
16485 if Present
(Qual
) then
16488 end Save_References_In_Aggregate
;
16490 ----------------------------------------------
16491 -- Save_References_In_Char_Lit_Or_Op_Symbol --
16492 ----------------------------------------------
16494 procedure Save_References_In_Char_Lit_Or_Op_Symbol
(N
: Node_Id
) is
16496 if Nkind
(N
) = Nkind
(Get_Associated_Node
(N
)) then
16499 elsif Nkind
(N
) = N_Operator_Symbol
16500 and then Nkind
(Get_Associated_Node
(N
)) = N_String_Literal
16502 Change_Operator_Symbol_To_String_Literal
(N
);
16504 end Save_References_In_Char_Lit_Or_Op_Symbol
;
16506 ------------------------------------
16507 -- Save_References_In_Descendants --
16508 ------------------------------------
16510 procedure Save_References_In_Descendants
(N
: Node_Id
) is
16511 procedure Walk
is new Walk_Sinfo_Fields
(Save_Global_Descendant
);
16514 end Save_References_In_Descendants
;
16516 -----------------------------------
16517 -- Save_References_In_Identifier --
16518 -----------------------------------
16520 procedure Save_References_In_Identifier
(N
: Node_Id
) is
16522 -- The node did not undergo a transformation
16524 if Nkind
(N
) = Nkind
(Get_Associated_Node
(N
)) then
16525 -- If this is a discriminant reference, always save it.
16526 -- It is used in the instance to find the corresponding
16527 -- discriminant positionally rather than by name.
16529 Set_Original_Discriminant
16530 (N
, Original_Discriminant
(Get_Associated_Node
(N
)));
16534 -- The analysis of the generic copy transformed the identifier
16535 -- into another construct. Propagate the changes to the template.
16538 N2
:= Get_Associated_Node
(N
);
16540 -- The identifier denotes a call to a parameterless function.
16541 -- Mark the node as resolved when the function is external.
16543 if Nkind
(N2
) = N_Function_Call
then
16544 E
:= Entity
(Name
(N2
));
16546 if Present
(E
) and then Is_Global
(E
) then
16547 Set_Global_Type
(N
, N2
);
16549 Set_Associated_Node
(N
, Empty
);
16550 Set_Etype
(N
, Empty
);
16553 -- The identifier denotes a named number that was constant
16554 -- folded. Preserve the original name for ASIS and undo the
16555 -- constant folding which will be repeated in the instance.
16556 -- Is this still needed???
16558 elsif Nkind
(N2
) in N_Integer_Literal | N_Real_Literal
16559 and then Is_Entity_Name
(Original_Node
(N2
))
16561 Set_Associated_Node
(N
, Original_Node
(N2
));
16564 -- The identifier resolved to a string literal. Propagate this
16565 -- information to the generic template.
16567 elsif Nkind
(N2
) = N_String_Literal
then
16568 Rewrite
(N
, New_Copy
(N2
));
16570 -- The identifier is rewritten as a dereference if it is the
16571 -- prefix of an implicit dereference. Preserve the original
16572 -- tree as the analysis of the instance will expand the node
16573 -- again, but preserve the resolved entity if it is global.
16575 elsif Nkind
(N2
) = N_Explicit_Dereference
then
16576 if Is_Entity_Name
(Prefix
(N2
))
16577 and then Present
(Entity
(Prefix
(N2
)))
16578 and then Is_Global
(Entity
(Prefix
(N2
)))
16580 Set_Associated_Node
(N
, Prefix
(N2
));
16581 Set_Global_Type
(N
, Prefix
(N2
));
16583 elsif Nkind
(Prefix
(N2
)) = N_Function_Call
16584 and then Is_Entity_Name
(Name
(Prefix
(N2
)))
16585 and then Present
(Entity
(Name
(Prefix
(N2
))))
16586 and then Is_Global
(Entity
(Name
(Prefix
(N2
))))
16589 Make_Explicit_Dereference
(Loc
,
16591 Make_Function_Call
(Loc
,
16594 (Entity
(Name
(Prefix
(N2
))), Loc
))));
16595 Set_Associated_Node
16596 (Name
(Prefix
(N
)), Name
(Prefix
(N2
)));
16597 Set_Global_Type
(Name
(Prefix
(N
)), Name
(Prefix
(N2
)));
16600 Set_Associated_Node
(N
, Empty
);
16601 Set_Etype
(N
, Empty
);
16604 -- The subtype mark of a nominally unconstrained object is
16605 -- rewritten as a subtype indication using the bounds of the
16606 -- expression. Recover the original subtype mark.
16608 elsif Nkind
(N2
) = N_Subtype_Indication
16609 and then Is_Entity_Name
(Original_Node
(N2
))
16611 Set_Associated_Node
(N
, Original_Node
(N2
));
16615 end Save_References_In_Identifier
;
16617 ---------------------------------
16618 -- Save_References_In_Operator --
16619 ---------------------------------
16621 procedure Save_References_In_Operator
(N
: Node_Id
) is
16623 N2
:= Get_Associated_Node
(N
);
16625 -- The node did not undergo a transformation
16627 if Nkind
(N
) = Nkind
(N2
) then
16628 if Nkind
(N
) = N_Op_Concat
then
16629 Set_Is_Component_Left_Opnd
16630 (N
, Is_Component_Left_Opnd
(N2
));
16631 Set_Is_Component_Right_Opnd
16632 (N
, Is_Component_Right_Opnd
(N2
));
16637 -- The analysis of the generic copy transformed the operator into
16638 -- some other construct. Propagate the changes to the template if
16642 -- The operator resoved to a function call
16644 if Nkind
(N2
) = N_Function_Call
then
16646 -- Add explicit qualifications in the generic template for
16647 -- all operands of universal type. This aids resolution by
16648 -- preserving the actual type of a literal or an attribute
16649 -- that yields a universal result.
16651 Qualify_Universal_Operands
(N
, N2
);
16653 E
:= Entity
(Name
(N2
));
16655 if Present
(E
) and then Is_Global
(E
) then
16656 Set_Global_Type
(N
, N2
);
16658 Set_Associated_Node
(N
, Empty
);
16659 Set_Etype
(N
, Empty
);
16662 -- The operator was folded into a literal
16664 elsif Nkind
(N2
) in N_Integer_Literal
16668 if Present
(Original_Node
(N2
))
16669 and then Nkind
(Original_Node
(N2
)) = Nkind
(N
)
16671 -- Operation was constant-folded. Whenever possible,
16672 -- recover semantic information from unfolded node.
16673 -- This was initially done for ASIS but is apparently
16674 -- needed also for e.g. compiling a-nbnbin.adb.
16676 Set_Associated_Node
(N
, Original_Node
(N2
));
16678 if Nkind
(N
) = N_Op_Concat
then
16679 Set_Is_Component_Left_Opnd
(N
,
16680 Is_Component_Left_Opnd
(Get_Associated_Node
(N
)));
16681 Set_Is_Component_Right_Opnd
(N
,
16682 Is_Component_Right_Opnd
(Get_Associated_Node
(N
)));
16687 -- Propagate the constant folding back to the template
16690 Rewrite
(N
, New_Copy
(N2
));
16691 Set_Analyzed
(N
, False);
16694 -- The operator was folded into an enumeration literal. Retain
16695 -- the entity to avoid spurious ambiguities if it is overloaded
16696 -- at the point of instantiation or inlining.
16698 elsif Nkind
(N2
) = N_Identifier
16699 and then Ekind
(Entity
(N2
)) = E_Enumeration_Literal
16701 Rewrite
(N
, New_Copy
(N2
));
16702 Set_Analyzed
(N
, False);
16706 -- Complete the operands check if node has not been constant
16709 if Nkind
(N
) in N_Op
then
16710 Save_Entity_Descendants
(N
);
16712 end Save_References_In_Operator
;
16714 -------------------------------
16715 -- Save_References_In_Pragma --
16716 -------------------------------
16718 procedure Save_References_In_Pragma
(Prag
: Node_Id
) is
16720 Do_Save
: Boolean := True;
16723 -- Do not save global references in pragmas generated from aspects
16724 -- because the pragmas will be regenerated at instantiation time.
16726 if From_Aspect_Specification
(Prag
) then
16729 -- The capture of global references within contract-related source
16730 -- pragmas associated with generic packages, subprograms or their
16731 -- respective bodies must be delayed due to timing of annotation
16732 -- analysis. Global references are still captured in routine
16733 -- Save_Global_References_In_Contract.
16735 elsif Is_Generic_Contract_Pragma
(Prag
) and then Prag
/= Templ
then
16736 if Is_Package_Contract_Annotation
(Prag
) then
16737 Context
:= Find_Related_Package_Or_Body
(Prag
);
16739 pragma Assert
(Is_Subprogram_Contract_Annotation
(Prag
));
16740 Context
:= Find_Related_Declaration_Or_Body
(Prag
);
16743 -- The use of Original_Node accounts for the case when the
16744 -- related context is generic template.
16746 if Requires_Delayed_Save
(Original_Node
(Context
)) then
16751 -- For all other cases, save all global references within the
16752 -- descendants, but skip the following semantic fields:
16753 -- Next_Pragma, Corresponding_Aspect, Next_Rep_Item.
16756 Save_Global_Descendant
16757 (Union_Id
(Pragma_Argument_Associations
(N
)));
16758 Save_Global_Descendant
(Union_Id
(Pragma_Identifier
(N
)));
16760 end Save_References_In_Pragma
;
16762 -- Start of processing for Save_References
16770 elsif Nkind
(N
) in N_Aggregate | N_Extension_Aggregate
then
16771 Save_References_In_Aggregate
(N
);
16773 -- Character literals, operator symbols
16775 elsif Nkind
(N
) in N_Character_Literal | N_Operator_Symbol
then
16776 Save_References_In_Char_Lit_Or_Op_Symbol
(N
);
16778 -- Defining identifiers
16780 elsif Nkind
(N
) in N_Entity
then
16785 elsif Nkind
(N
) = N_Identifier
then
16786 Save_References_In_Identifier
(N
);
16790 elsif Nkind
(N
) in N_Op
then
16791 Save_References_In_Operator
(N
);
16795 elsif Nkind
(N
) = N_Pragma
then
16796 Save_References_In_Pragma
(N
);
16799 Save_References_In_Descendants
(N
);
16802 -- Save all global references found within the aspect specifications
16803 -- of the related node.
16805 if Permits_Aspect_Specifications
(N
) and then Has_Aspects
(N
) then
16807 -- The capture of global references within aspects associated with
16808 -- generic packages, subprograms or their bodies must be delayed
16809 -- due to timing of annotation analysis. Global references are
16810 -- still captured in routine Save_Global_References_In_Contract.
16812 if Requires_Delayed_Save
(N
) then
16815 -- Otherwise save all global references within the aspects
16818 Save_Global_References_In_Aspects
(N
);
16821 end Save_References
;
16823 ---------------------
16824 -- Set_Global_Type --
16825 ---------------------
16827 procedure Set_Global_Type
(N
: Node_Id
; N2
: Node_Id
) is
16828 Typ
: constant Entity_Id
:= Etype
(N2
);
16831 Set_Etype
(N
, Typ
);
16833 -- If the entity of N is not the associated node, this is a
16834 -- nested generic and it has an associated node as well, whose
16835 -- type is already the full view (see below). Indicate that the
16836 -- original node has a private view.
16838 if Entity
(N
) /= N2
and then Has_Private_View
(Entity
(N
)) then
16839 Set_Has_Private_View
(N
);
16842 -- If not a private type, nothing else to do
16844 if not Is_Private_Type
(Typ
) then
16847 -- If it is a derivation of a private type in a context where no
16848 -- full view is needed, nothing to do either.
16850 elsif No
(Full_View
(Typ
)) and then Typ
/= Etype
(Typ
) then
16853 -- Otherwise mark the type for flipping and use the full view when
16857 Set_Has_Private_View
(N
);
16859 if Present
(Full_View
(Typ
)) then
16860 Set_Etype
(N2
, Full_View
(Typ
));
16864 if Is_Floating_Point_Type
(Typ
)
16865 and then Has_Dimension_System
(Typ
)
16867 Copy_Dimensions
(N2
, N
);
16869 end Set_Global_Type
;
16871 -- Start of processing for Save_Global_References
16874 Gen_Scope
:= Current_Scope
;
16876 -- If the generic unit is a child unit, references to entities in the
16877 -- parent are treated as local, because they will be resolved anew in
16878 -- the context of the instance of the parent.
16880 while Is_Child_Unit
(Gen_Scope
)
16881 and then Ekind
(Scope
(Gen_Scope
)) = E_Generic_Package
16883 Gen_Scope
:= Scope
(Gen_Scope
);
16886 Save_References
(Templ
);
16887 end Save_Global_References
;
16889 ---------------------------------------
16890 -- Save_Global_References_In_Aspects --
16891 ---------------------------------------
16893 procedure Save_Global_References_In_Aspects
(N
: Node_Id
) is
16898 Asp
:= First
(Aspect_Specifications
(N
));
16899 while Present
(Asp
) loop
16900 Expr
:= Expression
(Asp
);
16902 if Present
(Expr
) then
16903 Save_Global_References
(Expr
);
16908 end Save_Global_References_In_Aspects
;
16910 ------------------------------------------
16911 -- Set_Copied_Sloc_For_Inherited_Pragma --
16912 ------------------------------------------
16914 procedure Set_Copied_Sloc_For_Inherited_Pragma
16919 Create_Instantiation_Source
(N
, E
,
16920 Inlined_Body
=> False,
16921 Inherited_Pragma
=> True,
16922 Factor
=> S_Adjustment
);
16923 end Set_Copied_Sloc_For_Inherited_Pragma
;
16925 --------------------------------------
16926 -- Set_Copied_Sloc_For_Inlined_Body --
16927 --------------------------------------
16929 procedure Set_Copied_Sloc_For_Inlined_Body
(N
: Node_Id
; E
: Entity_Id
) is
16931 Create_Instantiation_Source
(N
, E
,
16932 Inlined_Body
=> True,
16933 Inherited_Pragma
=> False,
16934 Factor
=> S_Adjustment
);
16935 end Set_Copied_Sloc_For_Inlined_Body
;
16937 ---------------------
16938 -- Set_Instance_Of --
16939 ---------------------
16941 procedure Set_Instance_Of
(A
: Entity_Id
; B
: Entity_Id
) is
16943 Generic_Renamings
.Table
(Generic_Renamings
.Last
) := (A
, B
, Assoc_Null
);
16944 Generic_Renamings_HTable
.Set
(Generic_Renamings
.Last
);
16945 Generic_Renamings
.Increment_Last
;
16946 end Set_Instance_Of
;
16948 --------------------
16949 -- Set_Next_Assoc --
16950 --------------------
16952 procedure Set_Next_Assoc
(E
: Assoc_Ptr
; Next
: Assoc_Ptr
) is
16954 Generic_Renamings
.Table
(E
).Next_In_HTable
:= Next
;
16955 end Set_Next_Assoc
;
16957 -------------------
16958 -- Start_Generic --
16959 -------------------
16961 procedure Start_Generic
is
16963 -- ??? More things could be factored out in this routine.
16964 -- Should probably be done at a later stage.
16966 Generic_Flags
.Append
(Inside_A_Generic
);
16967 Inside_A_Generic
:= True;
16969 Expander_Mode_Save_And_Set
(False);
16972 ----------------------
16973 -- Set_Instance_Env --
16974 ----------------------
16976 -- WARNING: This routine manages SPARK regions
16978 procedure Set_Instance_Env
16979 (Gen_Unit
: Entity_Id
;
16980 Act_Unit
: Entity_Id
)
16982 Saved_AE
: constant Boolean := Assertions_Enabled
;
16983 Saved_CPL
: constant Node_Id
:= Check_Policy_List
;
16984 Saved_DEC
: constant Boolean := Dynamic_Elaboration_Checks
;
16985 Saved_SM
: constant SPARK_Mode_Type
:= SPARK_Mode
;
16986 Saved_SMP
: constant Node_Id
:= SPARK_Mode_Pragma
;
16989 -- Regardless of the current mode, predefined units are analyzed in the
16990 -- most current Ada mode, and earlier version Ada checks do not apply
16991 -- to predefined units. Nothing needs to be done for non-internal units.
16992 -- These are always analyzed in the current mode.
16994 if In_Internal_Unit
(Gen_Unit
) then
16996 -- The following call resets all configuration attributes to default
16997 -- or the xxx_Config versions of the attributes when the current sem
16998 -- unit is the main unit. At the same time, internal units must also
16999 -- inherit certain configuration attributes from their context. It
17000 -- is unclear what these two sets are.
17002 Set_Config_Switches
(True, Current_Sem_Unit
= Main_Unit
);
17004 -- Reinstall relevant configuration attributes of the context
17006 Assertions_Enabled
:= Saved_AE
;
17007 Check_Policy_List
:= Saved_CPL
;
17008 Dynamic_Elaboration_Checks
:= Saved_DEC
;
17010 Install_SPARK_Mode
(Saved_SM
, Saved_SMP
);
17013 Current_Instantiated_Parent
:=
17014 (Gen_Id
=> Gen_Unit
,
17015 Act_Id
=> Act_Unit
,
17016 Next_In_HTable
=> Assoc_Null
);
17017 end Set_Instance_Env
;
17023 procedure Switch_View
(T
: Entity_Id
) is
17024 BT
: constant Entity_Id
:= Base_Type
(T
);
17025 Priv_Elmt
: Elmt_Id
:= No_Elmt
;
17026 Priv_Sub
: Entity_Id
;
17029 -- T may be private but its base type may have been exchanged through
17030 -- some other occurrence, in which case there is nothing to switch
17031 -- besides T itself. Note that a private dependent subtype of a private
17032 -- type might not have been switched even if the base type has been,
17033 -- because of the last branch of Check_Private_View (see comment there).
17035 if not Is_Private_Type
(BT
) then
17036 Prepend_Elmt
(Full_View
(T
), Exchanged_Views
);
17037 Exchange_Declarations
(T
);
17041 Priv_Elmt
:= First_Elmt
(Private_Dependents
(BT
));
17043 if Present
(Full_View
(BT
)) then
17044 Prepend_Elmt
(Full_View
(BT
), Exchanged_Views
);
17045 Exchange_Declarations
(BT
);
17048 while Present
(Priv_Elmt
) loop
17049 Priv_Sub
:= Node
(Priv_Elmt
);
17051 if Present
(Full_View
(Priv_Sub
)) then
17052 Prepend_Elmt
(Full_View
(Priv_Sub
), Exchanged_Views
);
17053 Exchange_Declarations
(Priv_Sub
);
17056 Next_Elmt
(Priv_Elmt
);
17064 function True_Parent
(N
: Node_Id
) return Node_Id
is
17066 if Nkind
(Parent
(N
)) = N_Subunit
then
17067 return Parent
(Corresponding_Stub
(Parent
(N
)));
17073 -----------------------------
17074 -- Valid_Default_Attribute --
17075 -----------------------------
17077 procedure Valid_Default_Attribute
(Nam
: Entity_Id
; Def
: Node_Id
) is
17078 Attr_Id
: constant Attribute_Id
:=
17079 Get_Attribute_Id
(Attribute_Name
(Def
));
17080 T
: constant Entity_Id
:= Entity
(Prefix
(Def
));
17081 Is_Fun
: constant Boolean := (Ekind
(Nam
) = E_Function
);
17087 if No
(T
) or else T
= Any_Id
then
17092 F
:= First_Formal
(Nam
);
17093 while Present
(F
) loop
17094 Num_F
:= Num_F
+ 1;
17099 when Attribute_Adjacent
17100 | Attribute_Ceiling
17101 | Attribute_Copy_Sign
17103 | Attribute_Fraction
17104 | Attribute_Machine
17106 | Attribute_Remainder
17107 | Attribute_Rounding
17108 | Attribute_Unbiased_Rounding
17112 and then Is_Floating_Point_Type
(T
);
17114 when Attribute_Image
17118 | Attribute_Wide_Image
17119 | Attribute_Wide_Value
17121 OK
:= Is_Fun
and then Num_F
= 1 and then Is_Scalar_Type
(T
);
17126 OK
:= Is_Fun
and then Num_F
= 2 and then Is_Scalar_Type
(T
);
17128 when Attribute_Input
=>
17129 OK
:= (Is_Fun
and then Num_F
= 1);
17131 when Attribute_Output
17132 | Attribute_Put_Image
17136 OK
:= not Is_Fun
and then Num_F
= 2;
17144 ("attribute reference has wrong profile for subprogram", Def
);
17146 end Valid_Default_Attribute
;
17148 ----------------------------------
17149 -- Validate_Formal_Type_Default --
17150 ----------------------------------
17152 procedure Validate_Formal_Type_Default
(Decl
: Node_Id
) is
17153 Default
: constant Node_Id
:=
17154 Default_Subtype_Mark
(Original_Node
(Decl
));
17155 Formal
: constant Entity_Id
:= Defining_Identifier
(Decl
);
17157 Def_Sub
: Entity_Id
; -- Default subtype mark
17158 Type_Def
: Node_Id
;
17160 procedure Check_Discriminated_Formal
;
17161 -- Check that discriminants of default for private or incomplete
17162 -- type match those of formal type.
17164 function Reference_Formal
(N
: Node_Id
) return Traverse_Result
;
17165 -- Check whether formal type definition mentions a previous formal
17166 -- type of the same generic.
17168 ----------------------
17169 -- Reference_Formal --
17170 ----------------------
17172 function Reference_Formal
(N
: Node_Id
) return Traverse_Result
is
17174 if Is_Entity_Name
(N
)
17175 and then Scope
(Entity
(N
)) = Current_Scope
17181 end Reference_Formal
;
17183 function Depends_On_Other_Formals
is
17184 new Traverse_Func
(Reference_Formal
);
17186 function Default_Subtype_Matches
17187 (Gen_T
, Def_T
: Entity_Id
) return Boolean;
17189 procedure Validate_Array_Type_Default
;
17190 -- Verify that dimension, indices, and component types of default
17191 -- are compatible with formal array type definition.
17193 procedure Validate_Derived_Type_Default
;
17194 -- Verify that ancestor and progenitor types match.
17196 ---------------------------------
17197 -- Check_Discriminated_Formal --
17198 ---------------------------------
17200 procedure Check_Discriminated_Formal
is
17201 Formal_Discr
: Entity_Id
;
17202 Actual_Discr
: Entity_Id
;
17203 Formal_Subt
: Entity_Id
;
17206 if Has_Discriminants
(Formal
) then
17207 if not Has_Discriminants
(Def_Sub
) then
17209 ("default for & must have discriminants", Default
, Formal
);
17211 elsif Is_Constrained
(Def_Sub
) then
17213 ("default for & must be unconstrained", Default
, Formal
);
17216 Formal_Discr
:= First_Discriminant
(Formal
);
17217 Actual_Discr
:= First_Discriminant
(Def_Sub
);
17218 while Formal_Discr
/= Empty
loop
17219 if Actual_Discr
= Empty
then
17221 ("discriminants on Formal do not match formal",
17225 Formal_Subt
:= Etype
(Formal_Discr
);
17227 -- Access discriminants match if designated types do
17229 if Ekind
(Base_Type
(Formal_Subt
)) = E_Anonymous_Access_Type
17230 and then (Ekind
(Base_Type
(Etype
(Actual_Discr
)))) =
17231 E_Anonymous_Access_Type
17233 Designated_Type
(Base_Type
(Formal_Subt
)) =
17234 Designated_Type
(Base_Type
(Etype
(Actual_Discr
)))
17238 elsif Base_Type
(Formal_Subt
) /=
17239 Base_Type
(Etype
(Actual_Discr
))
17242 ("types of discriminants of default must match formal",
17245 elsif not Subtypes_Statically_Match
17246 (Formal_Subt
, Etype
(Actual_Discr
))
17247 and then Ada_Version
>= Ada_95
17250 ("subtypes of discriminants of default "
17251 & "must match formal",
17255 Next_Discriminant
(Formal_Discr
);
17256 Next_Discriminant
(Actual_Discr
);
17259 if Actual_Discr
/= Empty
then
17261 ("discriminants on default do not match formal",
17266 end Check_Discriminated_Formal
;
17268 ---------------------------
17269 -- Default_Subtype_Matches --
17270 ---------------------------
17272 function Default_Subtype_Matches
17273 (Gen_T
, Def_T
: Entity_Id
) return Boolean
17276 -- Check that the base types, root types (when dealing with class
17277 -- wide types), or designated types (when dealing with anonymous
17278 -- access types) of Gen_T and Def_T are statically matching subtypes.
17280 return (Base_Type
(Gen_T
) = Base_Type
(Def_T
)
17281 and then Subtypes_Statically_Match
(Gen_T
, Def_T
))
17283 or else (Is_Class_Wide_Type
(Gen_T
)
17284 and then Is_Class_Wide_Type
(Def_T
)
17285 and then Default_Subtype_Matches
17286 (Root_Type
(Gen_T
), Root_Type
(Def_T
)))
17288 or else (Is_Anonymous_Access_Type
(Gen_T
)
17289 and then Ekind
(Def_T
) = Ekind
(Gen_T
)
17290 and then Subtypes_Statically_Match
17291 (Designated_Type
(Gen_T
), Designated_Type
(Def_T
)));
17293 end Default_Subtype_Matches
;
17295 ----------------------------------
17296 -- Validate_Array_Type_Default --
17297 ----------------------------------
17299 procedure Validate_Array_Type_Default
is
17303 if not Is_Array_Type
(Def_Sub
) then
17304 Error_Msg_NE
("default for& must be an array type ",
17308 elsif Number_Dimensions
(Def_Sub
) /= Number_Dimensions
(Formal
)
17309 or else Is_Constrained
(Def_Sub
) /=
17310 Is_Constrained
(Formal
)
17312 Error_Msg_NE
("default array type does not match&",
17317 I1
:= First_Index
(Formal
);
17318 I2
:= First_Index
(Def_Sub
);
17319 for J
in 1 .. Number_Dimensions
(Formal
) loop
17321 -- If the indexes of the actual were given by a subtype_mark,
17322 -- the index was transformed into a range attribute. Retrieve
17323 -- the original type mark for checking.
17325 if Is_Entity_Name
(Original_Node
(I2
)) then
17326 T2
:= Entity
(Original_Node
(I2
));
17331 if not Subtypes_Statically_Match
(Etype
(I1
), T2
) then
17333 ("index types of default do not match those of formal &",
17341 if not Default_Subtype_Matches
17342 (Component_Type
(Formal
), Component_Type
(Def_Sub
))
17345 ("component subtype of default does not match that of formal &",
17349 if Has_Aliased_Components
(Formal
)
17350 and then not Has_Aliased_Components
(Default
)
17353 ("default must have aliased components to match formal type &",
17356 end Validate_Array_Type_Default
;
17358 -----------------------------------
17359 -- Validate_Derived_Type_Default --
17360 -----------------------------------
17362 procedure Validate_Derived_Type_Default
is
17364 if not Is_Ancestor
(Etype
(Formal
), Def_Sub
) then
17365 Error_Msg_NE
("default must be a descendent of&",
17366 Default
, Etype
(Formal
));
17369 if Has_Interfaces
(Formal
) then
17370 if not Has_Interfaces
(Def_Sub
) then
17372 ("default must implement all interfaces of formal&",
17378 Iface_Ent
: Entity_Id
;
17381 Iface
:= First
(Abstract_Interface_List
(Formal
));
17383 while Present
(Iface
) loop
17384 Iface_Ent
:= Entity
(Iface
);
17386 if Is_Ancestor
(Iface_Ent
, Def_Sub
)
17387 or else Is_Progenitor
(Iface_Ent
, Def_Sub
)
17393 ("Default must implement interface&",
17394 Default
, Etype
(Iface
));
17402 end Validate_Derived_Type_Default
;
17404 -- Start of processing for Validate_Formal_Type_Default
17408 if not Is_Entity_Name
(Default
)
17409 or else not Is_Type
(Entity
(Default
))
17412 ("Expect type name for default of formal type", Default
);
17415 Def_Sub
:= Entity
(Default
);
17418 -- Formal derived_type declarations are transformed into full
17419 -- type declarations or Private_Type_Extensions for ease of processing.
17421 if Nkind
(Decl
) = N_Full_Type_Declaration
then
17422 Type_Def
:= Type_Definition
(Decl
);
17424 elsif Nkind
(Decl
) = N_Private_Extension_Declaration
then
17425 Type_Def
:= Subtype_Indication
(Decl
);
17428 Type_Def
:= Formal_Type_Definition
(Decl
);
17431 if Depends_On_Other_Formals
(Type_Def
) = Abandon
17432 and then Scope
(Def_Sub
) /= Current_Scope
17434 Error_Msg_N
("default of formal type that depends on "
17435 & "other formals must be a previous formal type", Default
);
17438 elsif Def_Sub
= Formal
then
17440 ("default for formal type cannot be formal itsef", Default
);
17444 case Nkind
(Type_Def
) is
17446 when N_Formal_Private_Type_Definition
=>
17447 if (Is_Abstract_Type
(Formal
)
17448 and then not Is_Abstract_Type
(Def_Sub
))
17449 or else (Is_Limited_Type
(Formal
)
17450 and then not Is_Limited_Type
(Def_Sub
))
17453 ("default for private type$ does not match",
17457 Check_Discriminated_Formal
;
17459 when N_Formal_Derived_Type_Definition
=>
17460 Check_Discriminated_Formal
;
17461 Validate_Derived_Type_Default
;
17463 when N_Formal_Incomplete_Type_Definition
=>
17464 if Is_Tagged_Type
(Formal
)
17465 and then not Is_Tagged_Type
(Def_Sub
)
17468 ("default for & must be a tagged type", Default
, Formal
);
17471 Check_Discriminated_Formal
;
17473 when N_Formal_Discrete_Type_Definition
=>
17474 if not Is_Discrete_Type
(Def_Sub
) then
17475 Error_Msg_NE
("default for& must be a discrete type",
17479 when N_Formal_Signed_Integer_Type_Definition
=>
17480 if not Is_Integer_Type
(Def_Sub
) then
17481 Error_Msg_NE
("default for& must be a discrete type",
17485 when N_Formal_Modular_Type_Definition
=>
17486 if not Is_Modular_Integer_Type
(Def_Sub
) then
17487 Error_Msg_NE
("default for& must be a modular_integer Type",
17491 when N_Formal_Floating_Point_Definition
=>
17492 if not Is_Floating_Point_Type
(Def_Sub
) then
17493 Error_Msg_NE
("default for& must be a floating_point type",
17497 when N_Formal_Ordinary_Fixed_Point_Definition
=>
17498 if not Is_Ordinary_Fixed_Point_Type
(Def_Sub
) then
17499 Error_Msg_NE
("default for& must be "
17500 & "an ordinary_fixed_point type ",
17504 when N_Formal_Decimal_Fixed_Point_Definition
=>
17505 if not Is_Decimal_Fixed_Point_Type
(Def_Sub
) then
17506 Error_Msg_NE
("default for& must be "
17507 & "an Decimal_fixed_point type ",
17511 when N_Array_Type_Definition
=>
17512 Validate_Array_Type_Default
;
17514 when N_Access_Function_Definition |
17515 N_Access_Procedure_Definition
=>
17516 if Ekind
(Def_Sub
) /= E_Access_Subprogram_Type
then
17517 Error_Msg_NE
("default for& must be an Access_To_Subprogram",
17520 Check_Subtype_Conformant
17521 (Designated_Type
(Formal
), Designated_Type
(Def_Sub
));
17523 when N_Access_To_Object_Definition
=>
17524 if not Is_Access_Object_Type
(Def_Sub
) then
17525 Error_Msg_NE
("default for& must be an Access_To_Object",
17528 elsif not Default_Subtype_Matches
17529 (Designated_Type
(Formal
), Designated_Type
(Def_Sub
))
17531 Error_Msg_NE
("designated type of defaul does not match "
17532 & "designated type of formal type",
17536 when N_Record_Definition
=> -- Formal interface type
17537 if not Is_Interface
(Def_Sub
) then
17539 ("default for formal interface type must be an interface",
17542 elsif Is_Limited_Type
(Def_Sub
) /= Is_Limited_Type
(Formal
)
17543 or else Is_Task_Interface
(Formal
) /= Is_Task_Interface
(Def_Sub
)
17544 or else Is_Protected_Interface
(Formal
) /=
17545 Is_Protected_Interface
(Def_Sub
)
17546 or else Is_Synchronized_Interface
(Formal
) /=
17547 Is_Synchronized_Interface
(Def_Sub
)
17550 ("default for interface& does not match", Def_Sub
, Formal
);
17553 when N_Derived_Type_Definition
=>
17554 Validate_Derived_Type_Default
;
17556 when N_Identifier
=> -- case of a private extension
17557 Validate_Derived_Type_Default
;
17563 raise Program_Error
;
17565 end Validate_Formal_Type_Default
;