1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
9 -- Copyright (C) 1992-2022, 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 Contract_Cases
265 -- Initial_Condition Depends
266 -- Initializes Extensions_Visible
269 -- Refined_State Post_Class
277 -- Subprogram_Variant
280 -- Most package contract annotations utilize forward references to classify
281 -- data declared within the package [body]. Subprogram annotations then use
282 -- the classifications to further refine them. These inter dependencies are
283 -- problematic with respect to the implementation of generics because their
284 -- analysis, capture of global references and instantiation does not mesh
285 -- well with the existing mechanism.
287 -- 1) Analysis of generic contracts is carried out the same way non-generic
288 -- contracts are analyzed:
290 -- 1.1) General rule - a contract is analyzed after all related aspects
291 -- and pragmas are analyzed. This is done by routines
293 -- Analyze_Package_Body_Contract
294 -- Analyze_Package_Contract
295 -- Analyze_Subprogram_Body_Contract
296 -- Analyze_Subprogram_Contract
298 -- 1.2) Compilation unit - the contract is analyzed after Pragmas_After
301 -- 1.3) Compilation unit body - the contract is analyzed at the end of
302 -- the body declaration list.
304 -- 1.4) Package - the contract is analyzed at the end of the private or
305 -- visible declarations, prior to analyzing the contracts of any nested
306 -- packages or subprograms.
308 -- 1.5) Package body - the contract is analyzed at the end of the body
309 -- declaration list, prior to analyzing the contracts of any nested
310 -- packages or subprograms.
312 -- 1.6) Subprogram - if the subprogram is declared inside a block, a
313 -- package or a subprogram, then its contract is analyzed at the end of
314 -- the enclosing declarations, otherwise the subprogram is a compilation
317 -- 1.7) Subprogram body - if the subprogram body is declared inside a
318 -- block, a package body or a subprogram body, then its contract is
319 -- analyzed at the end of the enclosing declarations, otherwise the
320 -- subprogram is a compilation unit 1.3).
322 -- 2) Capture of global references within contracts is done after capturing
323 -- global references within the generic template. There are two reasons for
324 -- this delay - pragma annotations are not part of the generic template in
325 -- the case of a generic subprogram declaration, and analysis of contracts
328 -- Contract-related source pragmas within generic templates are prepared
329 -- for delayed capture of global references by routine
331 -- Create_Generic_Contract
333 -- The routine associates these pragmas with the contract of the template.
334 -- In the case of a generic subprogram declaration, the routine creates
335 -- generic templates for the pragmas declared after the subprogram because
336 -- they are not part of the template.
338 -- generic -- template starts
339 -- procedure Gen_Proc (Input : Integer); -- template ends
340 -- pragma Precondition (Input > 0); -- requires own template
342 -- 2.1) The capture of global references with aspect specifications and
343 -- source pragmas that apply to a generic unit must be suppressed when
344 -- the generic template is being processed because the contracts have not
345 -- been analyzed yet. Any attempts to capture global references at that
346 -- point will destroy the Associated_Node linkages and leave the template
347 -- undecorated. This delay is controlled by routine
349 -- Requires_Delayed_Save
351 -- 2.2) The real capture of global references within a contract is done
352 -- after the contract has been analyzed, by routine
354 -- Save_Global_References_In_Contract
356 -- 3) The instantiation of a generic contract occurs as part of the
357 -- instantiation of the contract owner. Generic subprogram declarations
358 -- require additional processing when the contract is specified by pragmas
359 -- because the pragmas are not part of the generic template. This is done
362 -- Instantiate_Subprogram_Contract
364 --------------------------------------------------
365 -- Formal packages and partial parameterization --
366 --------------------------------------------------
368 -- When compiling a generic, a formal package is a local instantiation. If
369 -- declared with a box, its generic formals are visible in the enclosing
370 -- generic. If declared with a partial list of actuals, those actuals that
371 -- are defaulted (covered by an Others clause, or given an explicit box
372 -- initialization) are also visible in the enclosing generic, while those
373 -- that have a corresponding actual are not.
375 -- In our source model of instantiation, the same visibility must be
376 -- present in the spec and body of an instance: the names of the formals
377 -- that are defaulted must be made visible within the instance, and made
378 -- invisible (hidden) after the instantiation is complete, so that they
379 -- are not accessible outside of the instance.
381 -- In a generic, a formal package is treated like a special instantiation.
382 -- Our Ada 95 compiler handled formals with and without box in different
383 -- ways. With partial parameterization, we use a single model for both.
384 -- We create a package declaration that consists of the specification of
385 -- the generic package, and a set of declarations that map the actuals
386 -- into local renamings, just as we do for bona fide instantiations. For
387 -- defaulted parameters and formals with a box, we copy directly the
388 -- declarations of the formals into this local package. The result is a
389 -- package whose visible declarations may include generic formals. This
390 -- package is only used for type checking and visibility analysis, and
391 -- never reaches the back end, so it can freely violate the placement
392 -- rules for generic formal declarations.
394 -- The list of declarations (renamings and copies of formals) is built
395 -- by Analyze_Associations, just as for regular instantiations.
397 -- At the point of instantiation, conformance checking must be applied only
398 -- to those parameters that were specified in the formals. We perform this
399 -- checking by creating another internal instantiation, this one including
400 -- only the renamings and the formals (the rest of the package spec is not
401 -- relevant to conformance checking). We can then traverse two lists: the
402 -- list of actuals in the instance that corresponds to the formal package,
403 -- and the list of actuals produced for this bogus instantiation. We apply
404 -- the conformance rules to those actuals that are not defaulted, i.e.
405 -- which still appear as generic formals.
407 -- When we compile an instance body we must make the right parameters
408 -- visible again. The predicate Is_Generic_Formal indicates which of the
409 -- formals should have its Is_Hidden flag reset.
411 -----------------------
412 -- Local subprograms --
413 -----------------------
415 procedure Abandon_Instantiation
(N
: Node_Id
);
416 pragma No_Return
(Abandon_Instantiation
);
417 -- Posts an error message "instantiation abandoned" at the indicated node
418 -- and then raises the exception Instantiation_Error to do it.
420 procedure Analyze_Formal_Array_Type
421 (T
: in out Entity_Id
;
423 -- A formal array type is treated like an array type declaration, and
424 -- invokes Array_Type_Declaration (sem_ch3) whose first parameter is
425 -- in-out, because in the case of an anonymous type the entity is
426 -- actually created in the procedure.
428 -- The following procedures treat other kinds of formal parameters
430 procedure Analyze_Formal_Derived_Interface_Type
435 procedure Analyze_Formal_Derived_Type
440 procedure Analyze_Formal_Interface_Type
445 -- The following subprograms create abbreviated declarations for formal
446 -- scalar types. We introduce an anonymous base of the proper class for
447 -- each of them, and define the formals as constrained first subtypes of
448 -- their bases. The bounds are expressions that are non-static in the
451 procedure Analyze_Formal_Decimal_Fixed_Point_Type
452 (T
: Entity_Id
; Def
: Node_Id
);
453 procedure Analyze_Formal_Discrete_Type
(T
: Entity_Id
; Def
: Node_Id
);
454 procedure Analyze_Formal_Floating_Type
(T
: Entity_Id
; Def
: Node_Id
);
455 procedure Analyze_Formal_Signed_Integer_Type
(T
: Entity_Id
; Def
: Node_Id
);
456 procedure Analyze_Formal_Modular_Type
(T
: Entity_Id
; Def
: Node_Id
);
457 procedure Analyze_Formal_Ordinary_Fixed_Point_Type
458 (T
: Entity_Id
; Def
: Node_Id
);
460 procedure Analyze_Formal_Private_Type
464 -- Creates a new private type, which does not require completion
466 procedure Analyze_Formal_Incomplete_Type
(T
: Entity_Id
; Def
: Node_Id
);
467 -- Ada 2012: Creates a new incomplete type whose actual does not freeze
469 procedure Analyze_Generic_Formal_Part
(N
: Node_Id
);
470 -- Analyze generic formal part
472 procedure Analyze_Generic_Access_Type
(T
: Entity_Id
; Def
: Node_Id
);
473 -- Create a new access type with the given designated type
475 function Analyze_Associations
478 F_Copy
: List_Id
) return List_Id
;
479 -- At instantiation time, build the list of associations between formals
480 -- and actuals. Each association becomes a renaming declaration for the
481 -- formal entity. F_Copy is the analyzed list of formals in the generic
482 -- copy. It is used to apply legality checks to the actuals. I_Node is the
483 -- instantiation node itself.
485 procedure Analyze_Subprogram_Instantiation
489 procedure Build_Instance_Compilation_Unit_Nodes
493 -- This procedure is used in the case where the generic instance of a
494 -- subprogram body or package body is a library unit. In this case, the
495 -- original library unit node for the generic instantiation must be
496 -- replaced by the resulting generic body, and a link made to a new
497 -- compilation unit node for the generic declaration. The argument N is
498 -- the original generic instantiation. Act_Body and Act_Decl are the body
499 -- and declaration of the instance (either package body and declaration
500 -- nodes or subprogram body and declaration nodes depending on the case).
501 -- On return, the node N has been rewritten with the actual body.
503 function Build_Subprogram_Decl_Wrapper
504 (Formal_Subp
: Entity_Id
) return Node_Id
;
505 -- Ada 2022 allows formal subprograms to carry pre/postconditions.
506 -- At the point of instantiation these contracts apply to uses of
507 -- the actual subprogram. This is implemented by creating wrapper
508 -- subprograms instead of the renamings previously used to link
509 -- formal subprograms and the corresponding actuals. If the actual
510 -- is not an entity (e.g. an attribute reference) a renaming is
511 -- created to handle the expansion of the attribute.
513 function Build_Subprogram_Body_Wrapper
514 (Formal_Subp
: Entity_Id
;
515 Actual_Name
: Node_Id
) return Node_Id
;
516 -- The body of the wrapper is a call to the actual, with the generated
517 -- pre/postconditon checks added.
519 procedure Check_Abbreviated_Instance
521 Parent_Installed
: in out Boolean);
522 -- If the name of the generic unit in an abbreviated instantiation is an
523 -- expanded name, then the prefix may be an instance and the selector may
524 -- designate a child unit. If the parent is installed as a result of this
525 -- call, then Parent_Installed is set True, otherwise Parent_Installed is
526 -- unchanged by the call.
528 -- This routine needs to be called for declaration nodes of formal objects,
529 -- types and subprograms to check whether they are the copy, present in the
530 -- visible part of the abbreviated instantiation of formal packages, of the
531 -- declaration node of their corresponding formal parameter in the template
532 -- of the formal package, as specified by RM 12.7(10/2), so as to establish
533 -- the proper context for their analysis.
535 procedure Check_Access_Definition
(N
: Node_Id
);
536 -- Subsidiary routine to null exclusion processing. Perform an assertion
537 -- check on Ada version and the presence of an access definition in N.
539 procedure Check_Formal_Packages
(P_Id
: Entity_Id
);
540 -- Apply the following to all formal packages in generic associations.
541 -- Restore the visibility of the formals of the instance that are not
542 -- defaulted (see RM 12.7 (10)). Remove the anonymous package declaration
543 -- created for formal instances that are not defaulted.
545 procedure Check_Formal_Package_Instance
546 (Formal_Pack
: Entity_Id
;
547 Actual_Pack
: Entity_Id
);
548 -- Verify that the actuals of the actual instance match the actuals of
549 -- the template for a formal package that is not declared with a box.
551 procedure Check_Forward_Instantiation
(Decl
: Node_Id
);
552 -- If the generic is a local entity and the corresponding body has not
553 -- been seen yet, flag enclosing packages to indicate that it will be
554 -- elaborated after the generic body. Subprograms declared in the same
555 -- package cannot be inlined by the front end because front-end inlining
556 -- requires a strict linear order of elaboration.
558 function Check_Hidden_Primitives
(Assoc_List
: List_Id
) return Elist_Id
;
559 -- Check if some association between formals and actuals requires to make
560 -- visible primitives of a tagged type, and make those primitives visible.
561 -- Return the list of primitives whose visibility is modified (to restore
562 -- their visibility later through Restore_Hidden_Primitives). If no
563 -- candidate is found then return No_Elist.
565 procedure Check_Hidden_Child_Unit
567 Gen_Unit
: Entity_Id
;
568 Act_Decl_Id
: Entity_Id
);
569 -- If the generic unit is an implicit child instance within a parent
570 -- instance, we need to make an explicit test that it is not hidden by
571 -- a child instance of the same name and parent.
573 procedure Check_Generic_Actuals
574 (Instance
: Entity_Id
;
575 Is_Formal_Box
: Boolean);
576 -- Similar to previous one. Check the actuals in the instantiation,
577 -- whose views can change between the point of instantiation and the point
578 -- of instantiation of the body. In addition, mark the generic renamings
579 -- as generic actuals, so that they are not compatible with other actuals.
580 -- Recurse on an actual that is a formal package whose declaration has
583 function Contains_Instance_Of
586 N
: Node_Id
) return Boolean;
587 -- Inner is instantiated within the generic Outer. Check whether Inner
588 -- directly or indirectly contains an instance of Outer or of one of its
589 -- parents, in the case of a subunit. Each generic unit holds a list of
590 -- the entities instantiated within (at any depth). This procedure
591 -- determines whether the set of such lists contains a cycle, i.e. an
592 -- illegal circular instantiation.
594 function Denotes_Formal_Package
596 On_Exit
: Boolean := False;
597 Instance
: Entity_Id
:= Empty
) return Boolean;
598 -- Returns True if E is a formal package of an enclosing generic, or
599 -- the actual for such a formal in an enclosing instantiation. If such
600 -- a package is used as a formal in an nested generic, or as an actual
601 -- in a nested instantiation, the visibility of ITS formals should not
602 -- be modified. When called from within Restore_Private_Views, the flag
603 -- On_Exit is true, to indicate that the search for a possible enclosing
604 -- instance should ignore the current one. In that case Instance denotes
605 -- the declaration for which this is an actual. This declaration may be
606 -- an instantiation in the source, or the internal instantiation that
607 -- corresponds to the actual for a formal package.
609 function Earlier
(N1
, N2
: Node_Id
) return Boolean;
610 -- Yields True if N1 and N2 appear in the same compilation unit,
611 -- ignoring subunits, and if N1 is to the left of N2 in a left-to-right
612 -- traversal of the tree for the unit. Used to determine the placement
613 -- of freeze nodes for instance bodies that may depend on other instances.
615 function Find_Actual_Type
617 Gen_Type
: Entity_Id
) return Entity_Id
;
618 -- When validating the actual types of a child instance, check whether
619 -- the formal is a formal type of the parent unit, and retrieve the current
620 -- actual for it. Typ is the entity in the analyzed formal type declaration
621 -- (component or index type of an array type, or designated type of an
622 -- access formal) and Gen_Type is the enclosing analyzed formal array
623 -- or access type. The desired actual may be a formal of a parent, or may
624 -- be declared in a formal package of a parent. In both cases it is a
625 -- generic actual type because it appears within a visible instance.
626 -- Finally, it may be declared in a parent unit without being a formal
627 -- of that unit, in which case it must be retrieved by visibility.
628 -- Ambiguities may still arise if two homonyms are declared in two formal
629 -- packages, and the prefix of the formal type may be needed to resolve
630 -- the ambiguity in the instance ???
632 procedure Freeze_Package_Instance
637 -- If the instantiation happens textually before the body of the generic,
638 -- the instantiation of the body must be analyzed after the generic body,
639 -- and not at the point of instantiation. Such early instantiations can
640 -- happen if the generic and the instance appear in a package declaration
641 -- because the generic body can only appear in the corresponding package
642 -- body. Early instantiations can also appear if generic, instance and
643 -- body are all in the declarative part of a subprogram or entry. Entities
644 -- of packages that are early instantiations are delayed, and their freeze
645 -- node appears after the generic body. This rather complex machinery is
646 -- needed when nested instantiations are present, because the source does
647 -- not carry any indication of where the corresponding instance bodies must
648 -- be installed and frozen.
650 procedure Freeze_Subprogram_Instance
653 Pack_Id
: Entity_Id
);
654 -- The generic body may appear textually after the instance, including
655 -- in the proper body of a stub, or within a different package instance.
656 -- Given that the instance can only be elaborated after the generic, we
657 -- place freeze nodes for the instance and/or for packages that may enclose
658 -- the instance and the generic, so that the back-end can establish the
659 -- proper order of elaboration.
661 function Get_Associated_Node
(N
: Node_Id
) return Node_Id
;
662 -- In order to propagate semantic information back from the analyzed copy
663 -- to the original generic, we maintain links between selected nodes in the
664 -- generic and their corresponding copies. At the end of generic analysis,
665 -- the routine Save_Global_References traverses the generic tree, examines
666 -- the semantic information, and preserves the links to those nodes that
667 -- contain global information. At instantiation, the information from the
668 -- associated node is placed on the new copy, so that name resolution is
671 -- Three kinds of source nodes have associated nodes:
673 -- a) those that can reference (denote) entities, that is identifiers,
674 -- character literals, expanded_names, operator symbols, operators,
675 -- and attribute reference nodes. These nodes have an Entity field
676 -- and are the set of nodes that are in N_Has_Entity.
678 -- b) aggregates (N_Aggregate and N_Extension_Aggregate)
680 -- c) selected components (N_Selected_Component)
682 -- For the first class, the associated node preserves the entity if it is
683 -- global. If the generic contains nested instantiations, the associated
684 -- node itself has been recopied, and a chain of them must be followed.
686 -- For aggregates, the associated node allows retrieval of the type, which
687 -- may otherwise not appear in the generic. The view of this type may be
688 -- different between generic and instantiation, and the full view can be
689 -- installed before the instantiation is analyzed. For aggregates of type
690 -- extensions, the same view exchange may have to be performed for some of
691 -- the ancestor types, if their view is private at the point of
694 -- Nodes that are selected components in the parse tree may be rewritten
695 -- as expanded names after resolution, and must be treated as potential
696 -- entity holders, which is why they also have an Associated_Node.
698 -- Nodes that do not come from source, such as freeze nodes, do not appear
699 -- in the generic tree, and need not have an associated node.
701 -- The associated node is stored in the Associated_Node field. Note that
702 -- this field overlaps Entity, which is fine, because the whole point is
703 -- that we don't need or want the normal Entity field in this situation.
705 function Has_Been_Exchanged
(E
: Entity_Id
) return Boolean;
706 -- Traverse the Exchanged_Views list to see if a type was private
707 -- and has already been flipped during this phase of instantiation.
709 function Has_Contracts
(Decl
: Node_Id
) return Boolean;
710 -- Determine whether a formal subprogram has a Pre- or Postcondition,
711 -- in which case a subprogram wrapper has to be built for the actual.
713 procedure Hide_Current_Scope
;
714 -- When instantiating a generic child unit, the parent context must be
715 -- present, but the instance and all entities that may be generated
716 -- must be inserted in the current scope. We leave the current scope
717 -- on the stack, but make its entities invisible to avoid visibility
718 -- problems. This is reversed at the end of the instantiation. This is
719 -- not done for the instantiation of the bodies, which only require the
720 -- instances of the generic parents to be in scope.
722 function In_Main_Context
(E
: Entity_Id
) return Boolean;
723 -- Check whether an instantiation is in the context of the main unit.
724 -- Used to determine whether its body should be elaborated to allow
725 -- front-end inlining.
727 procedure Inherit_Context
(Gen_Decl
: Node_Id
; Inst
: Node_Id
);
728 -- Add the context clause of the unit containing a generic unit to a
729 -- compilation unit that is, or contains, an instantiation.
732 -- Establish environment for subsequent instantiation. Separated from
733 -- Save_Env because data-structures for visibility handling must be
734 -- initialized before call to Check_Generic_Child_Unit.
736 procedure Inline_Instance_Body
738 Gen_Unit
: Entity_Id
;
740 -- If front-end inlining is requested, instantiate the package body,
741 -- and preserve the visibility of its compilation unit, to insure
742 -- that successive instantiations succeed.
744 procedure Insert_Freeze_Node_For_Instance
747 -- N denotes a package or a subprogram instantiation and F_Node is the
748 -- associated freeze node. Insert the freeze node before the first source
749 -- body which follows immediately after N. If no such body is found, the
750 -- freeze node is inserted at the end of the declarative region which
751 -- contains N, unless the instantiation is done in a package spec that is
752 -- not at library level, in which case it is inserted at the outer level.
753 -- This can also be invoked to insert the freeze node of a package that
754 -- encloses an instantiation, in which case N may denote an arbitrary node.
756 procedure Install_Formal_Packages
(Par
: Entity_Id
);
757 -- Install the visible part of any formal of the parent that is a formal
758 -- package. Note that for the case of a formal package with a box, this
759 -- includes the formal part of the formal package (12.7(10/2)).
761 procedure Install_Hidden_Primitives
762 (Prims_List
: in out Elist_Id
;
765 -- Remove suffix 'P' from hidden primitives of Act_T to match the
766 -- visibility of primitives of Gen_T. The list of primitives to which
767 -- the suffix is removed is added to Prims_List to restore them later.
769 procedure Install_Parent
(P
: Entity_Id
; In_Body
: Boolean := False);
770 -- When compiling an instance of a child unit the parent (which is
771 -- itself an instance) is an enclosing scope that must be made
772 -- immediately visible. This procedure is also used to install the non-
773 -- generic parent of a generic child unit when compiling its body, so
774 -- that full views of types in the parent are made visible.
776 -- The functions Instantiate_XXX perform various legality checks and build
777 -- the declarations for instantiated generic parameters. In all of these
778 -- Formal is the entity in the generic unit, Actual is the entity of
779 -- expression in the generic associations, and Analyzed_Formal is the
780 -- formal in the generic copy, which contains the semantic information to
781 -- be used to validate the actual.
783 function Instantiate_Object
786 Analyzed_Formal
: Node_Id
) return List_Id
;
788 function Instantiate_Type
791 Analyzed_Formal
: Node_Id
;
792 Actual_Decls
: List_Id
) return List_Id
;
794 function Instantiate_Formal_Subprogram
797 Analyzed_Formal
: Node_Id
) return Node_Id
;
799 function Instantiate_Formal_Package
802 Analyzed_Formal
: Node_Id
) return List_Id
;
803 -- If the formal package is declared with a box, special visibility rules
804 -- apply to its formals: they are in the visible part of the package. This
805 -- is true in the declarative region of the formal package, that is to say
806 -- in the enclosing generic or instantiation. For an instantiation, the
807 -- parameters of the formal package are made visible in an explicit step.
808 -- Furthermore, if the actual has a visible USE clause, these formals must
809 -- be made potentially use-visible as well. On exit from the enclosing
810 -- instantiation, the reverse must be done.
812 -- For a formal package declared without a box, there are conformance rules
813 -- that apply to the actuals in the generic declaration and the actuals of
814 -- the actual package in the enclosing instantiation. The simplest way to
815 -- apply these rules is to repeat the instantiation of the formal package
816 -- in the context of the enclosing instance, and compare the generic
817 -- associations of this instantiation with those of the actual package.
818 -- This internal instantiation only needs to contain the renamings of the
819 -- formals: the visible and private declarations themselves need not be
822 -- In Ada 2005, the formal package may be only partially parameterized.
823 -- In that case the visibility step must make visible those actuals whose
824 -- corresponding formals were given with a box. A final complication
825 -- involves inherited operations from formal derived types, which must
826 -- be visible if the type is.
828 function Is_In_Main_Unit
(N
: Node_Id
) return Boolean;
829 -- Test if given node is in the main unit
831 procedure Load_Parent_Of_Generic
834 Body_Optional
: Boolean := False);
835 -- If the generic appears in a separate non-generic library unit, load the
836 -- corresponding body to retrieve the body of the generic. N is the node
837 -- for the generic instantiation, Spec is the generic package declaration.
839 -- Body_Optional is a flag that indicates that the body is being loaded to
840 -- ensure that temporaries are generated consistently when there are other
841 -- instances in the current declarative part that precede the one being
842 -- loaded. In that case a missing body is acceptable.
844 procedure Map_Formal_Package_Entities
(Form
: Entity_Id
; Act
: Entity_Id
);
845 -- Within the generic part, entities in the formal package are
846 -- visible. To validate subsequent type declarations, indicate
847 -- the correspondence between the entities in the analyzed formal,
848 -- and the entities in the actual package. There are three packages
849 -- involved in the instantiation of a formal package: the parent
850 -- generic P1 which appears in the generic declaration, the fake
851 -- instantiation P2 which appears in the analyzed generic, and whose
852 -- visible entities may be used in subsequent formals, and the actual
853 -- P3 in the instance. To validate subsequent formals, me indicate
854 -- that the entities in P2 are mapped into those of P3. The mapping of
855 -- entities has to be done recursively for nested packages.
857 procedure Move_Freeze_Nodes
861 -- Freeze nodes can be generated in the analysis of a generic unit, but
862 -- will not be seen by the back-end. It is necessary to move those nodes
863 -- to the enclosing scope if they freeze an outer entity. We place them
864 -- at the end of the enclosing generic package, which is semantically
867 procedure Preanalyze_Actuals
(N
: Node_Id
; Inst
: Entity_Id
:= Empty
);
868 -- Analyze actuals to perform name resolution. Full resolution is done
869 -- later, when the expected types are known, but names have to be captured
870 -- before installing parents of generics, that are not visible for the
871 -- actuals themselves.
873 -- If Inst is present, it is the entity of the package instance. This
874 -- entity is marked as having a limited_view actual when some actual is
875 -- a limited view. This is used to place the instance body properly.
877 procedure Provide_Completing_Bodies
(N
: Node_Id
);
878 -- Generate completing bodies for all subprograms found within package or
879 -- subprogram declaration N.
881 procedure Remove_Parent
(In_Body
: Boolean := False);
882 -- Reverse effect after instantiation of child is complete
884 function Requires_Conformance_Checking
(N
: Node_Id
) return Boolean;
885 -- Determine whether the formal package declaration N requires conformance
886 -- checking with actuals in instantiations.
888 procedure Restore_Hidden_Primitives
(Prims_List
: in out Elist_Id
);
889 -- Restore suffix 'P' to primitives of Prims_List and leave Prims_List
892 procedure Set_Instance_Env
893 (Gen_Unit
: Entity_Id
;
894 Act_Unit
: Entity_Id
);
895 -- Save current instance on saved environment, to be used to determine
896 -- the global status of entities in nested instances. Part of Save_Env.
897 -- called after verifying that the generic unit is legal for the instance,
898 -- The procedure also examines whether the generic unit is a predefined
899 -- unit, in order to set configuration switches accordingly. As a result
900 -- the procedure must be called after analyzing and freezing the actuals.
902 procedure Set_Instance_Of
(A
: Entity_Id
; B
: Entity_Id
);
903 -- Associate analyzed generic parameter with corresponding instance. Used
904 -- for semantic checks at instantiation time.
906 function True_Parent
(N
: Node_Id
) return Node_Id
;
907 -- For a subunit, return parent of corresponding stub, else return
910 procedure Valid_Default_Attribute
(Nam
: Entity_Id
; Def
: Node_Id
);
911 -- Verify that an attribute that appears as the default for a formal
912 -- subprogram is a function or procedure with the correct profile.
914 procedure Validate_Formal_Type_Default
(Decl
: Node_Id
);
915 -- Ada_2022 AI12-205: if a default subtype_mark is present, verify
916 -- that it is the name of a type in the same class as the formal.
917 -- The treatment parallels what is done in Instantiate_Type but differs
918 -- in a few ways so that this machinery cannot be reused as is: on one
919 -- hand there are no visibility issues for a default, because it is
920 -- analyzed in the same context as the formal type definition; on the
921 -- other hand the check needs to take into acount the use of a previous
922 -- formal type in the current formal type definition (see details in
925 -------------------------------------------
926 -- Data Structures for Generic Renamings --
927 -------------------------------------------
929 -- The map Generic_Renamings associates generic entities with their
930 -- corresponding actuals. Currently used to validate type instances. It
931 -- will eventually be used for all generic parameters to eliminate the
932 -- need for overload resolution in the instance.
934 type Assoc_Ptr
is new Int
;
936 Assoc_Null
: constant Assoc_Ptr
:= -1;
941 Next_In_HTable
: Assoc_Ptr
;
944 package Generic_Renamings
is new Table
.Table
945 (Table_Component_Type
=> Assoc
,
946 Table_Index_Type
=> Assoc_Ptr
,
947 Table_Low_Bound
=> 0,
949 Table_Increment
=> 100,
950 Table_Name
=> "Generic_Renamings");
952 -- Variable to hold enclosing instantiation. When the environment is
953 -- saved for a subprogram inlining, the corresponding Act_Id is empty.
955 Current_Instantiated_Parent
: Assoc
:= (Empty
, Empty
, Assoc_Null
);
957 -- Hash table for associations
959 HTable_Size
: constant := 37;
960 type HTable_Range
is range 0 .. HTable_Size
- 1;
962 procedure Set_Next_Assoc
(E
: Assoc_Ptr
; Next
: Assoc_Ptr
);
963 function Next_Assoc
(E
: Assoc_Ptr
) return Assoc_Ptr
;
964 function Get_Gen_Id
(E
: Assoc_Ptr
) return Entity_Id
;
965 function Hash
(F
: Entity_Id
) return HTable_Range
;
967 package Generic_Renamings_HTable
is new GNAT
.HTable
.Static_HTable
(
968 Header_Num
=> HTable_Range
,
970 Elmt_Ptr
=> Assoc_Ptr
,
971 Null_Ptr
=> Assoc_Null
,
972 Set_Next
=> Set_Next_Assoc
,
975 Get_Key
=> Get_Gen_Id
,
979 Exchanged_Views
: Elist_Id
;
980 -- This list holds the private views that have been exchanged during
981 -- instantiation to restore the visibility of the generic declaration.
982 -- (see comments above). After instantiation, the current visibility is
983 -- reestablished by means of a traversal of this list.
985 Hidden_Entities
: Elist_Id
;
986 -- This list holds the entities of the current scope that are removed
987 -- from immediate visibility when instantiating a child unit. Their
988 -- visibility is restored in Remove_Parent.
990 -- Because instantiations can be recursive, the following must be saved
991 -- on entry and restored on exit from an instantiation (spec or body).
992 -- This is done by the two procedures Save_Env and Restore_Env. For
993 -- package and subprogram instantiations (but not for the body instances)
994 -- the action of Save_Env is done in two steps: Init_Env is called before
995 -- Check_Generic_Child_Unit, because setting the parent instances requires
996 -- that the visibility data structures be properly initialized. Once the
997 -- generic is unit is validated, Set_Instance_Env completes Save_Env.
999 Parent_Unit_Visible
: Boolean := False;
1000 -- Parent_Unit_Visible is used when the generic is a child unit, and
1001 -- indicates whether the ultimate parent of the generic is visible in the
1002 -- instantiation environment. It is used to reset the visibility of the
1003 -- parent at the end of the instantiation (see Remove_Parent).
1005 Instance_Parent_Unit
: Entity_Id
:= Empty
;
1006 -- This records the ultimate parent unit of an instance of a generic
1007 -- child unit and is used in conjunction with Parent_Unit_Visible to
1008 -- indicate the unit to which the Parent_Unit_Visible flag corresponds.
1010 type Instance_Env
is record
1011 Instantiated_Parent
: Assoc
;
1012 Exchanged_Views
: Elist_Id
;
1013 Hidden_Entities
: Elist_Id
;
1014 Current_Sem_Unit
: Unit_Number_Type
;
1015 Parent_Unit_Visible
: Boolean := False;
1016 Instance_Parent_Unit
: Entity_Id
:= Empty
;
1017 Switches
: Config_Switches_Type
;
1020 package Instance_Envs
is new Table
.Table
(
1021 Table_Component_Type
=> Instance_Env
,
1022 Table_Index_Type
=> Int
,
1023 Table_Low_Bound
=> 0,
1024 Table_Initial
=> 32,
1025 Table_Increment
=> 100,
1026 Table_Name
=> "Instance_Envs");
1028 procedure Restore_Private_Views
1029 (Pack_Id
: Entity_Id
;
1030 Is_Package
: Boolean := True);
1031 -- Restore the private views of external types, and unmark the generic
1032 -- renamings of actuals, so that they become compatible subtypes again.
1033 -- For subprograms, Pack_Id is the package constructed to hold the
1036 procedure Switch_View
(T
: Entity_Id
);
1037 -- Switch the partial and full views of a type and its private
1038 -- dependents (i.e. its subtypes and derived types).
1040 ------------------------------------
1041 -- Structures for Error Reporting --
1042 ------------------------------------
1044 Instantiation_Node
: Node_Id
;
1045 -- Used by subprograms that validate instantiation of formal parameters
1046 -- where there might be no actual on which to place the error message.
1047 -- Also used to locate the instantiation node for generic subunits.
1049 Instantiation_Error
: exception;
1050 -- When there is a semantic error in the generic parameter matching,
1051 -- there is no point in continuing the instantiation, because the
1052 -- number of cascaded errors is unpredictable. This exception aborts
1053 -- the instantiation process altogether.
1055 S_Adjustment
: Sloc_Adjustment
;
1056 -- Offset created for each node in an instantiation, in order to keep
1057 -- track of the source position of the instantiation in each of its nodes.
1058 -- A subsequent semantic error or warning on a construct of the instance
1059 -- points to both places: the original generic node, and the point of
1060 -- instantiation. See Sinput and Sinput.L for additional details.
1062 ------------------------------------------------------------
1063 -- Data structure for keeping track when inside a Generic --
1064 ------------------------------------------------------------
1066 -- The following table is used to save values of the Inside_A_Generic
1067 -- flag (see spec of Sem) when they are saved by Start_Generic.
1069 package Generic_Flags
is new Table
.Table
(
1070 Table_Component_Type
=> Boolean,
1071 Table_Index_Type
=> Int
,
1072 Table_Low_Bound
=> 0,
1073 Table_Initial
=> 32,
1074 Table_Increment
=> 200,
1075 Table_Name
=> "Generic_Flags");
1077 ---------------------------
1078 -- Abandon_Instantiation --
1079 ---------------------------
1081 procedure Abandon_Instantiation
(N
: Node_Id
) is
1083 Error_Msg_N
("\instantiation abandoned!", N
);
1084 raise Instantiation_Error
;
1085 end Abandon_Instantiation
;
1087 ----------------------------------
1088 -- Adjust_Inherited_Pragma_Sloc --
1089 ----------------------------------
1091 procedure Adjust_Inherited_Pragma_Sloc
(N
: Node_Id
) is
1093 Adjust_Instantiation_Sloc
(N
, S_Adjustment
);
1094 end Adjust_Inherited_Pragma_Sloc
;
1096 --------------------------
1097 -- Analyze_Associations --
1098 --------------------------
1100 function Analyze_Associations
1103 F_Copy
: List_Id
) return List_Id
1105 Actuals_To_Freeze
: constant Elist_Id
:= New_Elmt_List
;
1106 Assoc_List
: constant List_Id
:= New_List
;
1107 Default_Actuals
: constant List_Id
:= New_List
;
1108 Gen_Unit
: constant Entity_Id
:=
1109 Defining_Entity
(Parent
(F_Copy
));
1113 Analyzed_Formal
: Node_Id
;
1114 First_Named
: Node_Id
:= Empty
;
1118 Saved_Formal
: Node_Id
;
1120 Default_Formals
: constant List_Id
:= New_List
;
1121 -- If an Others_Choice is present, some of the formals may be defaulted.
1122 -- To simplify the treatment of visibility in an instance, we introduce
1123 -- individual defaults for each such formal. These defaults are
1124 -- appended to the list of associations and replace the Others_Choice.
1126 Found_Assoc
: Node_Id
;
1127 -- Association for the current formal being match. Empty if there are
1128 -- no remaining actuals, or if there is no named association with the
1129 -- name of the formal.
1131 Is_Named_Assoc
: Boolean;
1132 Num_Matched
: Nat
:= 0;
1133 Num_Actuals
: Nat
:= 0;
1135 Others_Present
: Boolean := False;
1136 Others_Choice
: Node_Id
:= Empty
;
1137 -- In Ada 2005, indicates partial parameterization of a formal
1138 -- package. As usual an other association must be last in the list.
1140 procedure Build_Subprogram_Wrappers
;
1141 -- Ada 2022: AI12-0272 introduces pre/postconditions for formal
1142 -- subprograms. The implementation of making the formal into a renaming
1143 -- of the actual does not work, given that subprogram renaming cannot
1144 -- carry aspect specifications. Instead we must create subprogram
1145 -- wrappers whose body is a call to the actual, and whose declaration
1146 -- carries the aspects of the formal.
1148 procedure Check_Fixed_Point_Actual
(Actual
: Node_Id
);
1149 -- Warn if an actual fixed-point type has user-defined arithmetic
1150 -- operations, but there is no corresponding formal in the generic,
1151 -- in which case the predefined operations will be used. This merits
1152 -- a warning because of the special semantics of fixed point ops.
1154 procedure Check_Overloaded_Formal_Subprogram
(Formal
: Entity_Id
);
1155 -- Apply RM 12.3(9): if a formal subprogram is overloaded, the instance
1156 -- cannot have a named association for it. AI05-0025 extends this rule
1157 -- to formals of formal packages by AI05-0025, and it also applies to
1158 -- box-initialized formals.
1160 function Has_Fully_Defined_Profile
(Subp
: Entity_Id
) return Boolean;
1161 -- Determine whether the parameter types and the return type of Subp
1162 -- are fully defined at the point of instantiation.
1164 function Matching_Actual
1166 A_F
: Entity_Id
) return Node_Id
;
1167 -- Find actual that corresponds to a given a formal parameter. If the
1168 -- actuals are positional, return the next one, if any. If the actuals
1169 -- are named, scan the parameter associations to find the right one.
1170 -- A_F is the corresponding entity in the analyzed generic, which is
1171 -- placed on the selector name.
1173 -- In Ada 2005, a named association may be given with a box, in which
1174 -- case Matching_Actual sets Found_Assoc to the generic association,
1175 -- but return Empty for the actual itself. In this case the code below
1176 -- creates a corresponding declaration for the formal.
1178 function Partial_Parameterization
return Boolean;
1179 -- Ada 2005: if no match is found for a given formal, check if the
1180 -- association for it includes a box, or whether the associations
1181 -- include an Others clause.
1183 procedure Process_Default
(Formal
: Node_Id
);
1184 -- Add a copy of the declaration of a generic formal to the list of
1185 -- associations, and add an explicit box association for its entity
1186 -- if there is none yet, and the default comes from an Others_Choice.
1188 function Renames_Standard_Subprogram
(Subp
: Entity_Id
) return Boolean;
1189 -- Determine whether Subp renames one of the subprograms defined in the
1190 -- generated package Standard.
1192 procedure Set_Analyzed_Formal
;
1193 -- Find the node in the generic copy that corresponds to a given formal.
1194 -- The semantic information on this node is used to perform legality
1195 -- checks on the actuals. Because semantic analysis can introduce some
1196 -- anonymous entities or modify the declaration node itself, the
1197 -- correspondence between the two lists is not one-one. In addition to
1198 -- anonymous types, the presence a formal equality will introduce an
1199 -- implicit declaration for the corresponding inequality.
1201 -------------------------------
1202 -- Build_Subprogram_Wrappers --
1203 -------------------------------
1205 procedure Build_Subprogram_Wrappers
is
1206 Formal
: constant Entity_Id
:=
1207 Defining_Unit_Name
(Specification
(Analyzed_Formal
));
1208 Aspect_Spec
: Node_Id
;
1209 Decl_Node
: Node_Id
;
1210 Actual_Name
: Node_Id
;
1213 -- Create declaration for wrapper subprogram
1214 -- The actual can be overloaded, in which case it will be
1215 -- resolved when the call in the wrapper body is analyzed.
1216 -- We attach the possible interpretations of the actual to
1217 -- the name to be used in the call in the wrapper body.
1219 if Is_Entity_Name
(Match
) then
1220 Actual_Name
:= New_Occurrence_Of
(Entity
(Match
), Sloc
(Match
));
1222 if Is_Overloaded
(Match
) then
1223 Save_Interps
(Match
, Actual_Name
);
1227 -- Use renaming declaration created when analyzing actual.
1228 -- This may be incomplete if there are several formal
1229 -- subprograms whose actual is an attribute ???
1232 Renaming_Decl
: constant Node_Id
:= Last
(Assoc_List
);
1235 Actual_Name
:= New_Occurrence_Of
1236 (Defining_Entity
(Renaming_Decl
), Sloc
(Match
));
1237 Set_Etype
(Actual_Name
, Get_Instance_Of
(Etype
(Formal
)));
1241 Decl_Node
:= Build_Subprogram_Decl_Wrapper
(Formal
);
1243 -- Transfer aspect specifications from formal subprogram to wrapper
1245 Set_Aspect_Specifications
(Decl_Node
,
1246 New_Copy_List_Tree
(Aspect_Specifications
(Analyzed_Formal
)));
1248 Aspect_Spec
:= First
(Aspect_Specifications
(Decl_Node
));
1249 while Present
(Aspect_Spec
) loop
1250 Set_Analyzed
(Aspect_Spec
, False);
1254 Append_To
(Assoc_List
, Decl_Node
);
1256 -- Create corresponding body, and append it to association list
1257 -- that appears at the head of the declarations in the instance.
1258 -- The subprogram may be called in the analysis of subsequent
1261 Append_To
(Assoc_List
,
1262 Build_Subprogram_Body_Wrapper
(Formal
, Actual_Name
));
1263 end Build_Subprogram_Wrappers
;
1265 ----------------------------------------
1266 -- Check_Overloaded_Formal_Subprogram --
1267 ----------------------------------------
1269 procedure Check_Overloaded_Formal_Subprogram
(Formal
: Entity_Id
) is
1270 Temp_Formal
: Entity_Id
;
1273 Temp_Formal
:= First
(Formals
);
1274 while Present
(Temp_Formal
) loop
1275 if Nkind
(Temp_Formal
) in N_Formal_Subprogram_Declaration
1276 and then Temp_Formal
/= Formal
1278 Chars
(Defining_Unit_Name
(Specification
(Formal
))) =
1279 Chars
(Defining_Unit_Name
(Specification
(Temp_Formal
)))
1281 if Present
(Found_Assoc
) then
1283 ("named association not allowed for overloaded formal",
1288 ("named association not allowed for overloaded formal",
1292 Abandon_Instantiation
(Instantiation_Node
);
1297 end Check_Overloaded_Formal_Subprogram
;
1299 -------------------------------
1300 -- Check_Fixed_Point_Actual --
1301 -------------------------------
1303 procedure Check_Fixed_Point_Actual
(Actual
: Node_Id
) is
1304 Typ
: constant Entity_Id
:= Entity
(Actual
);
1305 Prims
: constant Elist_Id
:= Collect_Primitive_Operations
(Typ
);
1311 -- Locate primitive operations of the type that are arithmetic
1314 Elem
:= First_Elmt
(Prims
);
1315 while Present
(Elem
) loop
1316 if Nkind
(Node
(Elem
)) = N_Defining_Operator_Symbol
then
1318 -- Check whether the generic unit has a formal subprogram of
1319 -- the same name. This does not check types but is good enough
1320 -- to justify a warning.
1322 Formal
:= First_Non_Pragma
(Formals
);
1323 Op
:= Alias
(Node
(Elem
));
1325 while Present
(Formal
) loop
1326 if Nkind
(Formal
) = N_Formal_Concrete_Subprogram_Declaration
1327 and then Chars
(Defining_Entity
(Formal
)) =
1332 elsif Nkind
(Formal
) = N_Formal_Package_Declaration
then
1338 -- Locate corresponding actual, and check whether it
1339 -- includes a fixed-point type.
1341 Assoc
:= First
(Assoc_List
);
1342 while Present
(Assoc
) loop
1344 Nkind
(Assoc
) = N_Package_Renaming_Declaration
1345 and then Chars
(Defining_Unit_Name
(Assoc
)) =
1346 Chars
(Defining_Identifier
(Formal
));
1351 if Present
(Assoc
) then
1353 -- If formal package declares a fixed-point type,
1354 -- and the user-defined operator is derived from
1355 -- a generic instance package, the fixed-point type
1356 -- does not use the corresponding predefined op.
1358 Ent
:= First_Entity
(Entity
(Name
(Assoc
)));
1359 while Present
(Ent
) loop
1360 if Is_Fixed_Point_Type
(Ent
)
1361 and then Present
(Op
)
1362 and then Is_Generic_Instance
(Scope
(Op
))
1377 Error_Msg_Sloc
:= Sloc
(Node
(Elem
));
1379 ("?instance uses predefined operation, not primitive "
1380 & "operation&#", Actual
, Node
(Elem
));
1386 end Check_Fixed_Point_Actual
;
1388 -------------------------------
1389 -- Has_Fully_Defined_Profile --
1390 -------------------------------
1392 function Has_Fully_Defined_Profile
(Subp
: Entity_Id
) return Boolean is
1393 function Is_Fully_Defined_Type
(Typ
: Entity_Id
) return Boolean;
1394 -- Determine whethet type Typ is fully defined
1396 ---------------------------
1397 -- Is_Fully_Defined_Type --
1398 ---------------------------
1400 function Is_Fully_Defined_Type
(Typ
: Entity_Id
) return Boolean is
1402 -- A private type without a full view is not fully defined
1404 if Is_Private_Type
(Typ
)
1405 and then No
(Full_View
(Typ
))
1409 -- An incomplete type is never fully defined
1411 elsif Is_Incomplete_Type
(Typ
) then
1414 -- All other types are fully defined
1419 end Is_Fully_Defined_Type
;
1421 -- Local declarations
1425 -- Start of processing for Has_Fully_Defined_Profile
1428 -- Check the parameters
1430 Param
:= First_Formal
(Subp
);
1431 while Present
(Param
) loop
1432 if not Is_Fully_Defined_Type
(Etype
(Param
)) then
1436 Next_Formal
(Param
);
1439 -- Check the return type
1441 return Is_Fully_Defined_Type
(Etype
(Subp
));
1442 end Has_Fully_Defined_Profile
;
1444 ---------------------
1445 -- Matching_Actual --
1446 ---------------------
1448 function Matching_Actual
1450 A_F
: Entity_Id
) return Node_Id
1456 Is_Named_Assoc
:= False;
1458 -- End of list of purely positional parameters
1460 if No
(Actual
) or else Nkind
(Actual
) = N_Others_Choice
then
1461 Found_Assoc
:= Empty
;
1464 -- Case of positional parameter corresponding to current formal
1466 elsif No
(Selector_Name
(Actual
)) then
1467 Found_Assoc
:= Actual
;
1468 Act
:= Explicit_Generic_Actual_Parameter
(Actual
);
1469 Num_Matched
:= Num_Matched
+ 1;
1472 -- Otherwise scan list of named actuals to find the one with the
1473 -- desired name. All remaining actuals have explicit names.
1476 Is_Named_Assoc
:= True;
1477 Found_Assoc
:= Empty
;
1481 while Present
(Actual
) loop
1482 if Nkind
(Actual
) = N_Others_Choice
then
1483 Found_Assoc
:= Empty
;
1486 elsif Chars
(Selector_Name
(Actual
)) = Chars
(F
) then
1487 Set_Entity
(Selector_Name
(Actual
), A_F
);
1488 Set_Etype
(Selector_Name
(Actual
), Etype
(A_F
));
1489 Generate_Reference
(A_F
, Selector_Name
(Actual
));
1491 Found_Assoc
:= Actual
;
1492 Act
:= Explicit_Generic_Actual_Parameter
(Actual
);
1493 Num_Matched
:= Num_Matched
+ 1;
1501 -- Reset for subsequent searches. In most cases the named
1502 -- associations are in order. If they are not, we reorder them
1503 -- to avoid scanning twice the same actual. This is not just a
1504 -- question of efficiency: there may be multiple defaults with
1505 -- boxes that have the same name. In a nested instantiation we
1506 -- insert actuals for those defaults, and cannot rely on their
1507 -- names to disambiguate them.
1509 if Actual
= First_Named
then
1512 elsif Present
(Actual
) then
1513 Insert_Before
(First_Named
, Remove_Next
(Prev
));
1516 Actual
:= First_Named
;
1519 if Is_Entity_Name
(Act
) and then Present
(Entity
(Act
)) then
1520 Set_Used_As_Generic_Actual
(Entity
(Act
));
1524 end Matching_Actual
;
1526 ------------------------------
1527 -- Partial_Parameterization --
1528 ------------------------------
1530 function Partial_Parameterization
return Boolean is
1532 return Others_Present
1533 or else (Present
(Found_Assoc
) and then Box_Present
(Found_Assoc
));
1534 end Partial_Parameterization
;
1536 ---------------------
1537 -- Process_Default --
1538 ---------------------
1540 procedure Process_Default
(Formal
: Node_Id
) is
1541 Loc
: constant Source_Ptr
:= Sloc
(I_Node
);
1542 F_Id
: constant Entity_Id
:= Defining_Entity
(Formal
);
1548 -- Append copy of formal declaration to associations, and create new
1549 -- defining identifier for it.
1551 Decl
:= New_Copy_Tree
(Formal
);
1552 Id
:= Make_Defining_Identifier
(Sloc
(F_Id
), Chars
(F_Id
));
1554 if Nkind
(Formal
) in N_Formal_Subprogram_Declaration
then
1555 Set_Defining_Unit_Name
(Specification
(Decl
), Id
);
1558 Set_Defining_Identifier
(Decl
, Id
);
1561 Append
(Decl
, Assoc_List
);
1563 if No
(Found_Assoc
) then
1565 Make_Generic_Association
(Loc
,
1567 New_Occurrence_Of
(Id
, Loc
),
1568 Explicit_Generic_Actual_Parameter
=> Empty
);
1569 Set_Box_Present
(Default
);
1570 Append
(Default
, Default_Formals
);
1572 end Process_Default
;
1574 ---------------------------------
1575 -- Renames_Standard_Subprogram --
1576 ---------------------------------
1578 function Renames_Standard_Subprogram
(Subp
: Entity_Id
) return Boolean is
1583 while Present
(Id
) loop
1584 if Scope
(Id
) = Standard_Standard
then
1592 end Renames_Standard_Subprogram
;
1594 -------------------------
1595 -- Set_Analyzed_Formal --
1596 -------------------------
1598 procedure Set_Analyzed_Formal
is
1602 while Present
(Analyzed_Formal
) loop
1603 Kind
:= Nkind
(Analyzed_Formal
);
1605 case Nkind
(Formal
) is
1606 when N_Formal_Subprogram_Declaration
=>
1607 exit when Kind
in N_Formal_Subprogram_Declaration
1610 (Defining_Unit_Name
(Specification
(Formal
))) =
1612 (Defining_Unit_Name
(Specification
(Analyzed_Formal
)));
1614 when N_Formal_Package_Declaration
=>
1615 exit when Kind
in N_Formal_Package_Declaration
1616 | N_Generic_Package_Declaration
1617 | N_Package_Declaration
;
1619 when N_Use_Package_Clause
1626 -- Skip freeze nodes, and nodes inserted to replace
1627 -- unrecognized pragmas.
1630 Kind
not in N_Formal_Subprogram_Declaration
1631 and then Kind
not in N_Subprogram_Declaration
1635 and then Chars
(Defining_Identifier
(Formal
)) =
1636 Chars
(Defining_Identifier
(Analyzed_Formal
));
1639 Next
(Analyzed_Formal
);
1641 end Set_Analyzed_Formal
;
1643 -- Start of processing for Analyze_Associations
1646 Actuals
:= Generic_Associations
(I_Node
);
1648 if Present
(Actuals
) then
1650 -- Check for an Others choice, indicating a partial parameterization
1651 -- for a formal package.
1653 Actual
:= First
(Actuals
);
1654 while Present
(Actual
) loop
1655 if Nkind
(Actual
) = N_Others_Choice
then
1656 Others_Present
:= True;
1657 Others_Choice
:= Actual
;
1659 if Present
(Next
(Actual
)) then
1660 Error_Msg_N
("OTHERS must be last association", Actual
);
1663 -- This subprogram is used both for formal packages and for
1664 -- instantiations. For the latter, associations must all be
1667 if Nkind
(I_Node
) /= N_Formal_Package_Declaration
1668 and then Comes_From_Source
(I_Node
)
1671 ("OTHERS association not allowed in an instance",
1675 -- In any case, nothing to do after the others association
1679 elsif Box_Present
(Actual
)
1680 and then Comes_From_Source
(I_Node
)
1681 and then Nkind
(I_Node
) /= N_Formal_Package_Declaration
1684 ("box association not allowed in an instance", Actual
);
1690 -- If named associations are present, save first named association
1691 -- (it may of course be Empty) to facilitate subsequent name search.
1693 First_Named
:= First
(Actuals
);
1694 while Present
(First_Named
)
1695 and then Nkind
(First_Named
) /= N_Others_Choice
1696 and then No
(Selector_Name
(First_Named
))
1698 Num_Actuals
:= Num_Actuals
+ 1;
1703 Named
:= First_Named
;
1704 while Present
(Named
) loop
1705 if Nkind
(Named
) /= N_Others_Choice
1706 and then No
(Selector_Name
(Named
))
1708 Error_Msg_N
("invalid positional actual after named one", Named
);
1709 Abandon_Instantiation
(Named
);
1712 -- A named association may lack an actual parameter, if it was
1713 -- introduced for a default subprogram that turns out to be local
1714 -- to the outer instantiation. If it has a box association it must
1715 -- correspond to some formal in the generic.
1717 if Nkind
(Named
) /= N_Others_Choice
1718 and then (Present
(Explicit_Generic_Actual_Parameter
(Named
))
1719 or else Box_Present
(Named
))
1721 Num_Actuals
:= Num_Actuals
+ 1;
1727 if Present
(Formals
) then
1728 Formal
:= First_Non_Pragma
(Formals
);
1729 Analyzed_Formal
:= First_Non_Pragma
(F_Copy
);
1731 if Present
(Actuals
) then
1732 Actual
:= First
(Actuals
);
1734 -- All formals should have default values
1740 while Present
(Formal
) loop
1741 Set_Analyzed_Formal
;
1742 Saved_Formal
:= Next_Non_Pragma
(Formal
);
1744 case Nkind
(Formal
) is
1745 when N_Formal_Object_Declaration
=>
1748 (Defining_Identifier
(Formal
),
1749 Defining_Identifier
(Analyzed_Formal
));
1751 if No
(Match
) and then Partial_Parameterization
then
1752 Process_Default
(Formal
);
1756 (Instantiate_Object
(Formal
, Match
, Analyzed_Formal
),
1759 -- For a defaulted in_parameter, create an entry in the
1760 -- the list of defaulted actuals, for GNATprove use. Do
1761 -- not included these defaults for an instance nested
1762 -- within a generic, because the defaults are also used
1763 -- in the analysis of the enclosing generic, and only
1764 -- defaulted subprograms are relevant there.
1766 if No
(Match
) and then not Inside_A_Generic
then
1767 Append_To
(Default_Actuals
,
1768 Make_Generic_Association
(Sloc
(I_Node
),
1771 (Defining_Identifier
(Formal
), Sloc
(I_Node
)),
1772 Explicit_Generic_Actual_Parameter
=>
1773 New_Copy_Tree
(Default_Expression
(Formal
))));
1777 -- If the object is a call to an expression function, this
1778 -- is a freezing point for it.
1780 if Is_Entity_Name
(Match
)
1781 and then Present
(Entity
(Match
))
1783 (Original_Node
(Unit_Declaration_Node
(Entity
(Match
))))
1784 = N_Expression_Function
1786 Append_Elmt
(Entity
(Match
), Actuals_To_Freeze
);
1789 when N_Formal_Type_Declaration
=>
1792 (Defining_Identifier
(Formal
),
1793 Defining_Identifier
(Analyzed_Formal
));
1796 if Partial_Parameterization
then
1797 Process_Default
(Formal
);
1799 elsif Present
(Default_Subtype_Mark
(Formal
)) then
1800 Match
:= New_Copy
(Default_Subtype_Mark
(Formal
));
1803 (Formal
, Match
, Analyzed_Formal
, Assoc_List
),
1805 Append_Elmt
(Entity
(Match
), Actuals_To_Freeze
);
1808 Error_Msg_Sloc
:= Sloc
(Gen_Unit
);
1811 Instantiation_Node
, Defining_Identifier
(Formal
));
1813 ("\in instantiation of & declared#",
1814 Instantiation_Node
, Gen_Unit
);
1815 Abandon_Instantiation
(Instantiation_Node
);
1822 (Formal
, Match
, Analyzed_Formal
, Assoc_List
),
1825 -- Warn when an actual is a fixed-point with user-
1826 -- defined promitives. The warning is superfluous
1827 -- if the formal is private, because there can be
1828 -- no arithmetic operations in the generic so there
1829 -- no danger of confusion.
1831 if Is_Fixed_Point_Type
(Entity
(Match
))
1832 and then not Is_Private_Type
1833 (Defining_Identifier
(Analyzed_Formal
))
1835 Check_Fixed_Point_Actual
(Match
);
1838 -- An instantiation is a freeze point for the actuals,
1839 -- unless this is a rewritten formal package, or the
1840 -- formal is an Ada 2012 formal incomplete type.
1842 if Nkind
(I_Node
) = N_Formal_Package_Declaration
1844 (Ada_Version
>= Ada_2012
1846 Ekind
(Defining_Identifier
(Analyzed_Formal
)) =
1852 Append_Elmt
(Entity
(Match
), Actuals_To_Freeze
);
1856 -- A remote access-to-class-wide type is not a legal actual
1857 -- for a generic formal of an access type (E.2.2(17/2)).
1858 -- In GNAT an exception to this rule is introduced when
1859 -- the formal is marked as remote using implementation
1860 -- defined aspect/pragma Remote_Access_Type. In that case
1861 -- the actual must be remote as well.
1863 -- If the current instantiation is the construction of a
1864 -- local copy for a formal package the actuals may be
1865 -- defaulted, and there is no matching actual to check.
1867 if Nkind
(Analyzed_Formal
) = N_Formal_Type_Declaration
1869 Nkind
(Formal_Type_Definition
(Analyzed_Formal
)) =
1870 N_Access_To_Object_Definition
1871 and then Present
(Match
)
1874 Formal_Ent
: constant Entity_Id
:=
1875 Defining_Identifier
(Analyzed_Formal
);
1877 if Is_Remote_Access_To_Class_Wide_Type
(Entity
(Match
))
1878 = Is_Remote_Types
(Formal_Ent
)
1880 -- Remoteness of formal and actual match
1884 elsif Is_Remote_Types
(Formal_Ent
) then
1886 -- Remote formal, non-remote actual
1889 ("actual for& must be remote", Match
, Formal_Ent
);
1892 -- Non-remote formal, remote actual
1895 ("actual for& may not be remote",
1901 when N_Formal_Subprogram_Declaration
=>
1904 (Defining_Unit_Name
(Specification
(Formal
)),
1905 Defining_Unit_Name
(Specification
(Analyzed_Formal
)));
1907 -- If the formal subprogram has the same name as another
1908 -- formal subprogram of the generic, then a named
1909 -- association is illegal (12.3(9)). Exclude named
1910 -- associations that are generated for a nested instance.
1913 and then Is_Named_Assoc
1914 and then Comes_From_Source
(Found_Assoc
)
1916 Check_Overloaded_Formal_Subprogram
(Formal
);
1919 -- If there is no corresponding actual, this may be case
1920 -- of partial parameterization, or else the formal has a
1921 -- default or a box.
1923 if No
(Match
) and then Partial_Parameterization
then
1924 Process_Default
(Formal
);
1926 if Nkind
(I_Node
) = N_Formal_Package_Declaration
then
1927 Check_Overloaded_Formal_Subprogram
(Formal
);
1931 Append_To
(Assoc_List
,
1932 Instantiate_Formal_Subprogram
1933 (Formal
, Match
, Analyzed_Formal
));
1935 -- If formal subprogram has contracts, create wrappers
1936 -- for it. This is an expansion activity that cannot
1937 -- take place e.g. within an enclosing generic unit.
1939 if Has_Contracts
(Analyzed_Formal
)
1940 and then Expander_Active
1942 Build_Subprogram_Wrappers
;
1945 -- An instantiation is a freeze point for the actuals,
1946 -- unless this is a rewritten formal package.
1948 if Nkind
(I_Node
) /= N_Formal_Package_Declaration
1949 and then Nkind
(Match
) = N_Identifier
1950 and then Is_Subprogram
(Entity
(Match
))
1952 -- The actual subprogram may rename a routine defined
1953 -- in Standard. Avoid freezing such renamings because
1954 -- subprograms coming from Standard cannot be frozen.
1957 not Renames_Standard_Subprogram
(Entity
(Match
))
1959 -- If the actual subprogram comes from a different
1960 -- unit, it is already frozen, either by a body in
1961 -- that unit or by the end of the declarative part
1962 -- of the unit. This check avoids the freezing of
1963 -- subprograms defined in Standard which are used
1964 -- as generic actuals.
1966 and then In_Same_Code_Unit
(Entity
(Match
), I_Node
)
1967 and then Has_Fully_Defined_Profile
(Entity
(Match
))
1969 -- Mark the subprogram as having a delayed freeze
1970 -- since this may be an out-of-order action.
1972 Set_Has_Delayed_Freeze
(Entity
(Match
));
1973 Append_Elmt
(Entity
(Match
), Actuals_To_Freeze
);
1977 -- If this is a nested generic, preserve default for later
1978 -- instantiations. We do this as well for GNATprove use,
1979 -- so that the list of generic associations is complete.
1981 if No
(Match
) and then Box_Present
(Formal
) then
1983 Subp
: constant Entity_Id
:=
1985 (Specification
(Last
(Assoc_List
)));
1988 Append_To
(Default_Actuals
,
1989 Make_Generic_Association
(Sloc
(I_Node
),
1991 New_Occurrence_Of
(Subp
, Sloc
(I_Node
)),
1992 Explicit_Generic_Actual_Parameter
=>
1993 New_Occurrence_Of
(Subp
, Sloc
(I_Node
))));
1997 when N_Formal_Package_Declaration
=>
1998 -- The name of the formal package may be hidden by the
1999 -- formal parameter itself.
2001 if Error_Posted
(Analyzed_Formal
) then
2002 Abandon_Instantiation
(Instantiation_Node
);
2007 (Defining_Identifier
(Formal
),
2009 (Original_Node
(Analyzed_Formal
)));
2013 if Partial_Parameterization
then
2014 Process_Default
(Formal
);
2017 Error_Msg_Sloc
:= Sloc
(Gen_Unit
);
2020 Instantiation_Node
, Defining_Identifier
(Formal
));
2022 ("\in instantiation of & declared#",
2023 Instantiation_Node
, Gen_Unit
);
2025 Abandon_Instantiation
(Instantiation_Node
);
2031 (Instantiate_Formal_Package
2032 (Formal
, Match
, Analyzed_Formal
),
2035 -- Determine whether the actual package needs an explicit
2036 -- freeze node. This is only the case if the actual is
2037 -- declared in the same unit and has a body. Normally
2038 -- packages do not have explicit freeze nodes, and gigi
2039 -- only uses them to elaborate entities in a package
2042 Explicit_Freeze_Check
: declare
2043 Actual
: constant Entity_Id
:= Entity
(Match
);
2044 Gen_Par
: Entity_Id
;
2046 Needs_Freezing
: Boolean;
2049 procedure Check_Generic_Parent
;
2050 -- The actual may be an instantiation of a unit
2051 -- declared in a previous instantiation. If that
2052 -- one is also in the current compilation, it must
2053 -- itself be frozen before the actual. The actual
2054 -- may be an instantiation of a generic child unit,
2055 -- in which case the same applies to the instance
2056 -- of the parent which must be frozen before the
2058 -- Should this itself be recursive ???
2060 --------------------------
2061 -- Check_Generic_Parent --
2062 --------------------------
2064 procedure Check_Generic_Parent
is
2065 Inst
: constant Node_Id
:=
2066 Next
(Unit_Declaration_Node
(Actual
));
2072 if Nkind
(Parent
(Actual
)) = N_Package_Specification
2074 Par
:= Scope
(Generic_Parent
(Parent
(Actual
)));
2076 if Is_Generic_Instance
(Par
) then
2079 -- If the actual is a child generic unit, check
2080 -- whether the instantiation of the parent is
2081 -- also local and must also be frozen now. We
2082 -- must retrieve the instance node to locate the
2083 -- parent instance if any.
2085 elsif Ekind
(Par
) = E_Generic_Package
2086 and then Is_Child_Unit
(Gen_Par
)
2087 and then Ekind
(Scope
(Gen_Par
)) =
2090 if Nkind
(Inst
) = N_Package_Instantiation
2091 and then Nkind
(Name
(Inst
)) =
2094 -- Retrieve entity of parent instance
2096 Par
:= Entity
(Prefix
(Name
(Inst
)));
2105 and then Is_Generic_Instance
(Par
)
2106 and then Scope
(Par
) = Current_Scope
2108 (No
(Freeze_Node
(Par
))
2110 not Is_List_Member
(Freeze_Node
(Par
)))
2112 Set_Has_Delayed_Freeze
(Par
);
2113 Append_Elmt
(Par
, Actuals_To_Freeze
);
2115 end Check_Generic_Parent
;
2117 -- Start of processing for Explicit_Freeze_Check
2120 if Present
(Renamed_Entity
(Actual
)) then
2122 Generic_Parent
(Specification
2123 (Unit_Declaration_Node
2124 (Renamed_Entity
(Actual
))));
2127 Generic_Parent
(Specification
2128 (Unit_Declaration_Node
(Actual
)));
2131 if not Expander_Active
2132 or else not Has_Completion
(Actual
)
2133 or else not In_Same_Source_Unit
(I_Node
, Actual
)
2134 or else Is_Frozen
(Actual
)
2136 (Present
(Renamed_Entity
(Actual
))
2138 not In_Same_Source_Unit
2139 (I_Node
, (Renamed_Entity
(Actual
))))
2144 -- Finally we want to exclude such freeze nodes
2145 -- from statement sequences, which freeze
2146 -- everything before them.
2147 -- Is this strictly necessary ???
2149 Needs_Freezing
:= True;
2151 P
:= Parent
(I_Node
);
2152 while Nkind
(P
) /= N_Compilation_Unit
loop
2153 if Nkind
(P
) = N_Handled_Sequence_Of_Statements
2155 Needs_Freezing
:= False;
2162 if Needs_Freezing
then
2163 Check_Generic_Parent
;
2165 -- If the actual is a renaming of a proper
2166 -- instance of the formal package, indicate
2167 -- that it is the instance that must be frozen.
2169 if Nkind
(Parent
(Actual
)) =
2170 N_Package_Renaming_Declaration
2172 Set_Has_Delayed_Freeze
2173 (Renamed_Entity
(Actual
));
2175 (Renamed_Entity
(Actual
),
2178 Set_Has_Delayed_Freeze
(Actual
);
2179 Append_Elmt
(Actual
, Actuals_To_Freeze
);
2183 end Explicit_Freeze_Check
;
2186 -- For use type and use package appearing in the generic part,
2187 -- we have already copied them, so we can just move them where
2188 -- they belong (we mustn't recopy them since this would mess up
2189 -- the Sloc values).
2191 when N_Use_Package_Clause
2194 if Nkind
(Original_Node
(I_Node
)) =
2195 N_Formal_Package_Declaration
2197 Append
(New_Copy_Tree
(Formal
), Assoc_List
);
2200 Append
(Formal
, Assoc_List
);
2204 raise Program_Error
;
2207 Formal
:= Saved_Formal
;
2208 Next_Non_Pragma
(Analyzed_Formal
);
2211 if Num_Actuals
> Num_Matched
then
2212 Error_Msg_Sloc
:= Sloc
(Gen_Unit
);
2214 if Present
(Selector_Name
(Actual
)) then
2216 ("unmatched actual &", Actual
, Selector_Name
(Actual
));
2218 ("\in instantiation of & declared#", Actual
, Gen_Unit
);
2221 ("unmatched actual in instantiation of & declared#",
2226 elsif Present
(Actuals
) then
2228 ("too many actuals in generic instantiation", Instantiation_Node
);
2231 -- An instantiation freezes all generic actuals. The only exceptions
2232 -- to this are incomplete types and subprograms which are not fully
2233 -- defined at the point of instantiation.
2236 Elmt
: Elmt_Id
:= First_Elmt
(Actuals_To_Freeze
);
2238 while Present
(Elmt
) loop
2239 Freeze_Before
(I_Node
, Node
(Elmt
));
2244 -- If there are default subprograms, normalize the tree by adding
2245 -- explicit associations for them. This is required if the instance
2246 -- appears within a generic.
2248 if not Is_Empty_List
(Default_Actuals
) then
2253 Default
:= First
(Default_Actuals
);
2254 while Present
(Default
) loop
2255 Mark_Rewrite_Insertion
(Default
);
2259 if No
(Actuals
) then
2260 Set_Generic_Associations
(I_Node
, Default_Actuals
);
2262 Append_List_To
(Actuals
, Default_Actuals
);
2267 -- If this is a formal package, normalize the parameter list by adding
2268 -- explicit box associations for the formals that are covered by an
2271 Append_List
(Default_Formals
, Formals
);
2274 end Analyze_Associations
;
2276 -------------------------------
2277 -- Analyze_Formal_Array_Type --
2278 -------------------------------
2280 procedure Analyze_Formal_Array_Type
2281 (T
: in out Entity_Id
;
2287 -- Treated like a non-generic array declaration, with additional
2292 if Nkind
(Def
) = N_Constrained_Array_Definition
then
2293 DSS
:= First
(Discrete_Subtype_Definitions
(Def
));
2294 while Present
(DSS
) loop
2295 if Nkind
(DSS
) in N_Subtype_Indication
2297 | N_Attribute_Reference
2299 Error_Msg_N
("only a subtype mark is allowed in a formal", DSS
);
2306 Array_Type_Declaration
(T
, Def
);
2307 Set_Is_Generic_Type
(Base_Type
(T
));
2309 if Ekind
(Component_Type
(T
)) = E_Incomplete_Type
2310 and then No
(Full_View
(Component_Type
(T
)))
2312 Error_Msg_N
("premature usage of incomplete type", Def
);
2314 -- Check that range constraint is not allowed on the component type
2315 -- of a generic formal array type (AARM 12.5.3(3))
2317 elsif Is_Internal
(Component_Type
(T
))
2318 and then Present
(Subtype_Indication
(Component_Definition
(Def
)))
2319 and then Nkind
(Original_Node
2320 (Subtype_Indication
(Component_Definition
(Def
)))) =
2321 N_Subtype_Indication
2324 ("in a formal, a subtype indication can only be "
2325 & "a subtype mark (RM 12.5.3(3))",
2326 Subtype_Indication
(Component_Definition
(Def
)));
2329 end Analyze_Formal_Array_Type
;
2331 ---------------------------------------------
2332 -- Analyze_Formal_Decimal_Fixed_Point_Type --
2333 ---------------------------------------------
2335 -- As for other generic types, we create a valid type representation with
2336 -- legal but arbitrary attributes, whose values are never considered
2337 -- static. For all scalar types we introduce an anonymous base type, with
2338 -- the same attributes. We choose the corresponding integer type to be
2339 -- Standard_Integer.
2340 -- Here and in other similar routines, the Sloc of the generated internal
2341 -- type must be the same as the sloc of the defining identifier of the
2342 -- formal type declaration, to provide proper source navigation.
2344 procedure Analyze_Formal_Decimal_Fixed_Point_Type
2348 Loc
: constant Source_Ptr
:= Sloc
(Def
);
2350 Base
: constant Entity_Id
:=
2352 (E_Decimal_Fixed_Point_Type
,
2354 Sloc
(Defining_Identifier
(Parent
(Def
))), 'G');
2356 Int_Base
: constant Entity_Id
:= Standard_Integer
;
2357 Delta_Val
: constant Ureal
:= Ureal_1
;
2358 Digs_Val
: constant Uint
:= Uint_6
;
2360 function Make_Dummy_Bound
return Node_Id
;
2361 -- Return a properly typed universal real literal to use as a bound
2363 ----------------------
2364 -- Make_Dummy_Bound --
2365 ----------------------
2367 function Make_Dummy_Bound
return Node_Id
is
2368 Bound
: constant Node_Id
:= Make_Real_Literal
(Loc
, Ureal_1
);
2370 Set_Etype
(Bound
, Universal_Real
);
2372 end Make_Dummy_Bound
;
2374 -- Start of processing for Analyze_Formal_Decimal_Fixed_Point_Type
2379 Set_Etype
(Base
, Base
);
2380 Set_Size_Info
(Base
, Int_Base
);
2381 Set_RM_Size
(Base
, RM_Size
(Int_Base
));
2382 Set_First_Rep_Item
(Base
, First_Rep_Item
(Int_Base
));
2383 Set_Digits_Value
(Base
, Digs_Val
);
2384 Set_Delta_Value
(Base
, Delta_Val
);
2385 Set_Small_Value
(Base
, Delta_Val
);
2386 Set_Scalar_Range
(Base
,
2388 Low_Bound
=> Make_Dummy_Bound
,
2389 High_Bound
=> Make_Dummy_Bound
));
2391 Set_Is_Generic_Type
(Base
);
2392 Set_Parent
(Base
, Parent
(Def
));
2394 Mutate_Ekind
(T
, E_Decimal_Fixed_Point_Subtype
);
2395 Set_Etype
(T
, Base
);
2396 Set_Size_Info
(T
, Int_Base
);
2397 Set_RM_Size
(T
, RM_Size
(Int_Base
));
2398 Set_First_Rep_Item
(T
, First_Rep_Item
(Int_Base
));
2399 Set_Digits_Value
(T
, Digs_Val
);
2400 Set_Delta_Value
(T
, Delta_Val
);
2401 Set_Small_Value
(T
, Delta_Val
);
2402 Set_Scalar_Range
(T
, Scalar_Range
(Base
));
2403 Set_Is_Constrained
(T
);
2405 Check_Restriction
(No_Fixed_Point
, Def
);
2406 end Analyze_Formal_Decimal_Fixed_Point_Type
;
2408 -------------------------------------------
2409 -- Analyze_Formal_Derived_Interface_Type --
2410 -------------------------------------------
2412 procedure Analyze_Formal_Derived_Interface_Type
2417 Loc
: constant Source_Ptr
:= Sloc
(Def
);
2420 -- Rewrite as a type declaration of a derived type. This ensures that
2421 -- the interface list and primitive operations are properly captured.
2424 Make_Full_Type_Declaration
(Loc
,
2425 Defining_Identifier
=> T
,
2426 Type_Definition
=> Def
));
2428 Set_Is_Generic_Type
(T
);
2429 end Analyze_Formal_Derived_Interface_Type
;
2431 ---------------------------------
2432 -- Analyze_Formal_Derived_Type --
2433 ---------------------------------
2435 procedure Analyze_Formal_Derived_Type
2440 Loc
: constant Source_Ptr
:= Sloc
(Def
);
2441 Unk_Disc
: constant Boolean := Unknown_Discriminants_Present
(N
);
2445 Set_Is_Generic_Type
(T
);
2447 if Private_Present
(Def
) then
2449 Make_Private_Extension_Declaration
(Loc
,
2450 Defining_Identifier
=> T
,
2451 Discriminant_Specifications
=> Discriminant_Specifications
(N
),
2452 Unknown_Discriminants_Present
=> Unk_Disc
,
2453 Subtype_Indication
=> Subtype_Mark
(Def
),
2454 Interface_List
=> Interface_List
(Def
));
2456 Set_Abstract_Present
(New_N
, Abstract_Present
(Def
));
2457 Set_Limited_Present
(New_N
, Limited_Present
(Def
));
2458 Set_Synchronized_Present
(New_N
, Synchronized_Present
(Def
));
2462 Make_Full_Type_Declaration
(Loc
,
2463 Defining_Identifier
=> T
,
2464 Discriminant_Specifications
=>
2465 Discriminant_Specifications
(Parent
(T
)),
2467 Make_Derived_Type_Definition
(Loc
,
2468 Subtype_Indication
=> Subtype_Mark
(Def
)));
2470 Set_Abstract_Present
2471 (Type_Definition
(New_N
), Abstract_Present
(Def
));
2473 (Type_Definition
(New_N
), Limited_Present
(Def
));
2480 if not Is_Composite_Type
(T
) then
2482 ("unknown discriminants not allowed for elementary types", N
);
2484 Set_Has_Unknown_Discriminants
(T
);
2485 Set_Is_Constrained
(T
, False);
2489 -- If the parent type has a known size, so does the formal, which makes
2490 -- legal representation clauses that involve the formal.
2492 Set_Size_Known_At_Compile_Time
2493 (T
, Size_Known_At_Compile_Time
(Entity
(Subtype_Mark
(Def
))));
2494 end Analyze_Formal_Derived_Type
;
2496 ----------------------------------
2497 -- Analyze_Formal_Discrete_Type --
2498 ----------------------------------
2500 -- The operations defined for a discrete types are those of an enumeration
2501 -- type. The size is set to an arbitrary value, for use in analyzing the
2504 procedure Analyze_Formal_Discrete_Type
(T
: Entity_Id
; Def
: Node_Id
) is
2505 Loc
: constant Source_Ptr
:= Sloc
(Def
);
2509 Base
: constant Entity_Id
:=
2511 (E_Floating_Point_Type
, Current_Scope
,
2512 Sloc
(Defining_Identifier
(Parent
(Def
))), 'G');
2516 Mutate_Ekind
(T
, E_Enumeration_Subtype
);
2517 Set_Etype
(T
, Base
);
2519 Reinit_Alignment
(T
);
2520 Set_Is_Generic_Type
(T
);
2521 Set_Is_Constrained
(T
);
2523 -- For semantic analysis, the bounds of the type must be set to some
2524 -- non-static value. The simplest is to create attribute nodes for those
2525 -- bounds, that refer to the type itself. These bounds are never
2526 -- analyzed but serve as place-holders.
2529 Make_Attribute_Reference
(Loc
,
2530 Attribute_Name
=> Name_First
,
2531 Prefix
=> New_Occurrence_Of
(T
, Loc
));
2535 Make_Attribute_Reference
(Loc
,
2536 Attribute_Name
=> Name_Last
,
2537 Prefix
=> New_Occurrence_Of
(T
, Loc
));
2540 Set_Scalar_Range
(T
,
2545 Mutate_Ekind
(Base
, E_Enumeration_Type
);
2546 Set_Etype
(Base
, Base
);
2547 Init_Size
(Base
, 8);
2548 Reinit_Alignment
(Base
);
2549 Set_Is_Generic_Type
(Base
);
2550 Set_Scalar_Range
(Base
, Scalar_Range
(T
));
2551 Set_Parent
(Base
, Parent
(Def
));
2552 end Analyze_Formal_Discrete_Type
;
2554 ----------------------------------
2555 -- Analyze_Formal_Floating_Type --
2556 ---------------------------------
2558 procedure Analyze_Formal_Floating_Type
(T
: Entity_Id
; Def
: Node_Id
) is
2559 Base
: constant Entity_Id
:=
2561 (E_Floating_Point_Type
, Current_Scope
,
2562 Sloc
(Defining_Identifier
(Parent
(Def
))), 'G');
2565 -- The various semantic attributes are taken from the predefined type
2566 -- Float, just so that all of them are initialized. Their values are
2567 -- never used because no constant folding or expansion takes place in
2568 -- the generic itself.
2571 Mutate_Ekind
(T
, E_Floating_Point_Subtype
);
2572 Set_Etype
(T
, Base
);
2573 Set_Size_Info
(T
, (Standard_Float
));
2574 Set_RM_Size
(T
, RM_Size
(Standard_Float
));
2575 Set_Digits_Value
(T
, Digits_Value
(Standard_Float
));
2576 Set_Scalar_Range
(T
, Scalar_Range
(Standard_Float
));
2577 Set_Is_Constrained
(T
);
2579 Set_Is_Generic_Type
(Base
);
2580 Set_Etype
(Base
, Base
);
2581 Set_Size_Info
(Base
, (Standard_Float
));
2582 Set_RM_Size
(Base
, RM_Size
(Standard_Float
));
2583 Set_Digits_Value
(Base
, Digits_Value
(Standard_Float
));
2584 Set_Scalar_Range
(Base
, Scalar_Range
(Standard_Float
));
2585 Set_Parent
(Base
, Parent
(Def
));
2587 Check_Restriction
(No_Floating_Point
, Def
);
2588 end Analyze_Formal_Floating_Type
;
2590 -----------------------------------
2591 -- Analyze_Formal_Interface_Type;--
2592 -----------------------------------
2594 procedure Analyze_Formal_Interface_Type
2599 Loc
: constant Source_Ptr
:= Sloc
(N
);
2604 Make_Full_Type_Declaration
(Loc
,
2605 Defining_Identifier
=> T
,
2606 Type_Definition
=> Def
);
2610 Set_Is_Generic_Type
(T
);
2611 end Analyze_Formal_Interface_Type
;
2613 ---------------------------------
2614 -- Analyze_Formal_Modular_Type --
2615 ---------------------------------
2617 procedure Analyze_Formal_Modular_Type
(T
: Entity_Id
; Def
: Node_Id
) is
2619 -- Apart from their entity kind, generic modular types are treated like
2620 -- signed integer types, and have the same attributes.
2622 Analyze_Formal_Signed_Integer_Type
(T
, Def
);
2623 Mutate_Ekind
(T
, E_Modular_Integer_Subtype
);
2624 Mutate_Ekind
(Etype
(T
), E_Modular_Integer_Type
);
2626 end Analyze_Formal_Modular_Type
;
2628 ---------------------------------------
2629 -- Analyze_Formal_Object_Declaration --
2630 ---------------------------------------
2632 procedure Analyze_Formal_Object_Declaration
(N
: Node_Id
) is
2633 E
: constant Node_Id
:= Default_Expression
(N
);
2634 Id
: constant Node_Id
:= Defining_Identifier
(N
);
2637 Parent_Installed
: Boolean := False;
2643 Check_Abbreviated_Instance
(Parent
(N
), Parent_Installed
);
2645 -- Determine the mode of the formal object
2647 if Out_Present
(N
) then
2648 K
:= E_Generic_In_Out_Parameter
;
2650 if not In_Present
(N
) then
2651 Error_Msg_N
("formal generic objects cannot have mode OUT", N
);
2655 K
:= E_Generic_In_Parameter
;
2658 if Present
(Subtype_Mark
(N
)) then
2659 Find_Type
(Subtype_Mark
(N
));
2660 T
:= Entity
(Subtype_Mark
(N
));
2662 -- Verify that there is no redundant null exclusion
2664 if Null_Exclusion_Present
(N
) then
2665 if not Is_Access_Type
(T
) then
2667 ("null exclusion can only apply to an access type", N
);
2669 elsif Can_Never_Be_Null
(T
) then
2671 ("`NOT NULL` not allowed (& already excludes null)", N
, T
);
2675 -- Ada 2005 (AI-423): Formal object with an access definition
2678 Check_Access_Definition
(N
);
2679 T
:= Access_Definition
2681 N
=> Access_Definition
(N
));
2684 if Ekind
(T
) = E_Incomplete_Type
then
2686 Error_Node
: Node_Id
;
2689 if Present
(Subtype_Mark
(N
)) then
2690 Error_Node
:= Subtype_Mark
(N
);
2692 Check_Access_Definition
(N
);
2693 Error_Node
:= Access_Definition
(N
);
2696 Error_Msg_N
("premature usage of incomplete type", Error_Node
);
2700 if K
= E_Generic_In_Parameter
then
2702 -- Ada 2005 (AI-287): Limited aggregates allowed in generic formals
2704 if Ada_Version
< Ada_2005
and then Is_Limited_Type
(T
) then
2706 ("generic formal of mode IN must not be of limited type", N
);
2707 Explain_Limited_Type
(T
, N
);
2710 if Is_Abstract_Type
(T
) then
2712 ("generic formal of mode IN must not be of abstract type", N
);
2716 Preanalyze_Spec_Expression
(E
, T
);
2718 if Is_Limited_Type
(T
) and then not OK_For_Limited_Init
(T
, E
) then
2720 ("initialization not allowed for limited types", E
);
2721 Explain_Limited_Type
(T
, E
);
2725 Mutate_Ekind
(Id
, K
);
2728 -- Case of generic IN OUT parameter
2731 -- If the formal has an unconstrained type, construct its actual
2732 -- subtype, as is done for subprogram formals. In this fashion, all
2733 -- its uses can refer to specific bounds.
2735 Mutate_Ekind
(Id
, K
);
2738 if (Is_Array_Type
(T
) and then not Is_Constrained
(T
))
2739 or else (Ekind
(T
) = E_Record_Type
and then Has_Discriminants
(T
))
2742 Non_Freezing_Ref
: constant Node_Id
:=
2743 New_Occurrence_Of
(Id
, Sloc
(Id
));
2747 -- Make sure the actual subtype doesn't generate bogus freezing
2749 Set_Must_Not_Freeze
(Non_Freezing_Ref
);
2750 Decl
:= Build_Actual_Subtype
(T
, Non_Freezing_Ref
);
2751 Insert_Before_And_Analyze
(N
, Decl
);
2752 Set_Actual_Subtype
(Id
, Defining_Identifier
(Decl
));
2755 Set_Actual_Subtype
(Id
, T
);
2760 ("initialization not allowed for `IN OUT` formals", N
);
2764 if Has_Aspects
(N
) then
2765 Analyze_Aspect_Specifications
(N
, Id
);
2768 if Parent_Installed
then
2771 end Analyze_Formal_Object_Declaration
;
2773 ----------------------------------------------
2774 -- Analyze_Formal_Ordinary_Fixed_Point_Type --
2775 ----------------------------------------------
2777 procedure Analyze_Formal_Ordinary_Fixed_Point_Type
2781 Loc
: constant Source_Ptr
:= Sloc
(Def
);
2782 Base
: constant Entity_Id
:=
2784 (E_Ordinary_Fixed_Point_Type
, Current_Scope
,
2785 Sloc
(Defining_Identifier
(Parent
(Def
))), 'G');
2788 -- The semantic attributes are set for completeness only, their values
2789 -- will never be used, since all properties of the type are non-static.
2792 Mutate_Ekind
(T
, E_Ordinary_Fixed_Point_Subtype
);
2793 Set_Etype
(T
, Base
);
2794 Set_Size_Info
(T
, Standard_Integer
);
2795 Set_RM_Size
(T
, RM_Size
(Standard_Integer
));
2796 Set_Small_Value
(T
, Ureal_1
);
2797 Set_Delta_Value
(T
, Ureal_1
);
2798 Set_Scalar_Range
(T
,
2800 Low_Bound
=> Make_Real_Literal
(Loc
, Ureal_1
),
2801 High_Bound
=> Make_Real_Literal
(Loc
, Ureal_1
)));
2802 Set_Is_Constrained
(T
);
2804 Set_Is_Generic_Type
(Base
);
2805 Set_Etype
(Base
, Base
);
2806 Set_Size_Info
(Base
, Standard_Integer
);
2807 Set_RM_Size
(Base
, RM_Size
(Standard_Integer
));
2808 Set_Small_Value
(Base
, Ureal_1
);
2809 Set_Delta_Value
(Base
, Ureal_1
);
2810 Set_Scalar_Range
(Base
, Scalar_Range
(T
));
2811 Set_Parent
(Base
, Parent
(Def
));
2813 Check_Restriction
(No_Fixed_Point
, Def
);
2814 end Analyze_Formal_Ordinary_Fixed_Point_Type
;
2816 ----------------------------------------
2817 -- Analyze_Formal_Package_Declaration --
2818 ----------------------------------------
2820 procedure Analyze_Formal_Package_Declaration
(N
: Node_Id
) is
2821 Gen_Id
: constant Node_Id
:= Name
(N
);
2822 Loc
: constant Source_Ptr
:= Sloc
(N
);
2823 Pack_Id
: constant Entity_Id
:= Defining_Identifier
(N
);
2826 Gen_Unit
: Entity_Id
;
2829 Vis_Prims_List
: Elist_Id
:= No_Elist
;
2830 -- List of primitives made temporarily visible in the instantiation
2831 -- to match the visibility of the formal type.
2833 function Build_Local_Package
return Node_Id
;
2834 -- The formal package is rewritten so that its parameters are replaced
2835 -- with corresponding declarations. For parameters with bona fide
2836 -- associations these declarations are created by Analyze_Associations
2837 -- as for a regular instantiation. For boxed parameters, we preserve
2838 -- the formal declarations and analyze them, in order to introduce
2839 -- entities of the right kind in the environment of the formal.
2841 -------------------------
2842 -- Build_Local_Package --
2843 -------------------------
2845 function Build_Local_Package
return Node_Id
is
2847 Pack_Decl
: Node_Id
;
2850 -- Within the formal, the name of the generic package is a renaming
2851 -- of the formal (as for a regular instantiation).
2854 Make_Package_Declaration
(Loc
,
2857 (Specification
(Original_Node
(Gen_Decl
)),
2858 Empty
, Instantiating
=> True));
2861 Make_Package_Renaming_Declaration
(Loc
,
2862 Defining_Unit_Name
=>
2863 Make_Defining_Identifier
(Loc
, Chars
(Gen_Unit
)),
2864 Name
=> New_Occurrence_Of
(Formal
, Loc
));
2866 if Nkind
(Gen_Id
) = N_Identifier
2867 and then Chars
(Gen_Id
) = Chars
(Pack_Id
)
2870 ("& is hidden within declaration of instance", Gen_Id
, Gen_Unit
);
2873 -- If the formal is declared with a box, or with an others choice,
2874 -- create corresponding declarations for all entities in the formal
2875 -- part, so that names with the proper types are available in the
2876 -- specification of the formal package.
2878 -- On the other hand, if there are no associations, then all the
2879 -- formals must have defaults, and this will be checked by the
2880 -- call to Analyze_Associations.
2883 or else Nkind
(First
(Generic_Associations
(N
))) = N_Others_Choice
2886 Formal_Decl
: Node_Id
;
2889 -- TBA : for a formal package, need to recurse ???
2894 (Generic_Formal_Declarations
(Original_Node
(Gen_Decl
)));
2895 while Present
(Formal_Decl
) loop
2899 (Formal_Decl
, Empty
, Instantiating
=> True));
2904 -- If generic associations are present, use Analyze_Associations to
2905 -- create the proper renaming declarations.
2909 Act_Tree
: constant Node_Id
:=
2911 (Original_Node
(Gen_Decl
), Empty
,
2912 Instantiating
=> True);
2915 Generic_Renamings
.Set_Last
(0);
2916 Generic_Renamings_HTable
.Reset
;
2917 Instantiation_Node
:= N
;
2920 Analyze_Associations
2921 (I_Node
=> Original_Node
(N
),
2922 Formals
=> Generic_Formal_Declarations
(Act_Tree
),
2923 F_Copy
=> Generic_Formal_Declarations
(Gen_Decl
));
2925 Vis_Prims_List
:= Check_Hidden_Primitives
(Decls
);
2929 Append
(Renaming
, To
=> Decls
);
2931 -- Add generated declarations ahead of local declarations in
2934 if No
(Visible_Declarations
(Specification
(Pack_Decl
))) then
2935 Set_Visible_Declarations
(Specification
(Pack_Decl
), Decls
);
2938 (First
(Visible_Declarations
(Specification
(Pack_Decl
))),
2943 end Build_Local_Package
;
2947 Save_ISMP
: constant Boolean := Ignore_SPARK_Mode_Pragmas_In_Instance
;
2948 -- Save flag Ignore_SPARK_Mode_Pragmas_In_Instance for restore on exit
2950 Associations
: Boolean := True;
2952 Parent_Installed
: Boolean := False;
2953 Parent_Instance
: Entity_Id
;
2954 Renaming_In_Par
: Entity_Id
;
2956 -- Start of processing for Analyze_Formal_Package_Declaration
2959 Check_Text_IO_Special_Unit
(Gen_Id
);
2962 Check_Generic_Child_Unit
(Gen_Id
, Parent_Installed
);
2963 Gen_Unit
:= Entity
(Gen_Id
);
2965 -- Check for a formal package that is a package renaming
2967 if Present
(Renamed_Entity
(Gen_Unit
)) then
2969 -- Indicate that unit is used, before replacing it with renamed
2970 -- entity for use below.
2972 if In_Extended_Main_Source_Unit
(N
) then
2973 Set_Is_Instantiated
(Gen_Unit
);
2974 Generate_Reference
(Gen_Unit
, N
);
2977 Gen_Unit
:= Renamed_Entity
(Gen_Unit
);
2980 if Ekind
(Gen_Unit
) /= E_Generic_Package
then
2981 Error_Msg_N
("expect generic package name", Gen_Id
);
2985 elsif Gen_Unit
= Current_Scope
then
2987 ("generic package cannot be used as a formal package of itself",
2992 elsif In_Open_Scopes
(Gen_Unit
) then
2993 if Is_Compilation_Unit
(Gen_Unit
)
2994 and then Is_Child_Unit
(Current_Scope
)
2996 -- Special-case the error when the formal is a parent, and
2997 -- continue analysis to minimize cascaded errors.
3000 ("generic parent cannot be used as formal package of a child "
3005 ("generic package cannot be used as a formal package within "
3006 & "itself", Gen_Id
);
3012 -- Check that name of formal package does not hide name of generic,
3013 -- or its leading prefix. This check must be done separately because
3014 -- the name of the generic has already been analyzed.
3017 Gen_Name
: Entity_Id
;
3021 while Nkind
(Gen_Name
) = N_Expanded_Name
loop
3022 Gen_Name
:= Prefix
(Gen_Name
);
3025 if Chars
(Gen_Name
) = Chars
(Pack_Id
) then
3027 ("& is hidden within declaration of formal package",
3033 or else No
(Generic_Associations
(N
))
3034 or else Nkind
(First
(Generic_Associations
(N
))) = N_Others_Choice
3036 Associations
:= False;
3039 -- If there are no generic associations, the generic parameters appear
3040 -- as local entities and are instantiated like them. We copy the generic
3041 -- package declaration as if it were an instantiation, and analyze it
3042 -- like a regular package, except that we treat the formals as
3043 -- additional visible components.
3045 Gen_Decl
:= Unit_Declaration_Node
(Gen_Unit
);
3047 if In_Extended_Main_Source_Unit
(N
) then
3048 Set_Is_Instantiated
(Gen_Unit
);
3049 Generate_Reference
(Gen_Unit
, N
);
3052 Formal
:= New_Copy
(Pack_Id
);
3053 Create_Instantiation_Source
(N
, Gen_Unit
, S_Adjustment
);
3055 -- Make local generic without formals. The formals will be replaced with
3056 -- internal declarations.
3059 New_N
:= Build_Local_Package
;
3061 -- If there are errors in the parameter list, Analyze_Associations
3062 -- raises Instantiation_Error. Patch the declaration to prevent further
3063 -- exception propagation.
3066 when Instantiation_Error
=>
3067 Enter_Name
(Formal
);
3068 Mutate_Ekind
(Formal
, E_Variable
);
3069 Set_Etype
(Formal
, Any_Type
);
3070 Restore_Hidden_Primitives
(Vis_Prims_List
);
3072 if Parent_Installed
then
3080 Set_Defining_Unit_Name
(Specification
(New_N
), Formal
);
3081 Set_Generic_Parent
(Specification
(N
), Gen_Unit
);
3082 Set_Instance_Env
(Gen_Unit
, Formal
);
3083 Set_Is_Generic_Instance
(Formal
);
3085 Enter_Name
(Formal
);
3086 Mutate_Ekind
(Formal
, E_Package
);
3087 Set_Etype
(Formal
, Standard_Void_Type
);
3088 Set_Inner_Instances
(Formal
, New_Elmt_List
);
3090 -- It is unclear that any aspects can apply to a formal package
3091 -- declaration, given that they look like a hidden conformance
3092 -- requirement on the corresponding actual. However, Abstract_State
3093 -- must be treated specially because it generates declarations that
3094 -- must appear before other declarations in the specification and
3095 -- must be analyzed at once.
3097 if Present
(Aspect_Specifications
(Gen_Decl
)) then
3098 if No
(Aspect_Specifications
(N
)) then
3099 Set_Aspect_Specifications
(N
, New_List
);
3100 Set_Has_Aspects
(N
);
3104 ASN
: Node_Id
:= First
(Aspect_Specifications
(Gen_Decl
));
3108 while Present
(ASN
) loop
3109 if Get_Aspect_Id
(ASN
) = Aspect_Abstract_State
then
3111 Copy_Generic_Node
(ASN
, Empty
, Instantiating
=> True);
3112 Set_Entity
(New_A
, Formal
);
3113 Set_Analyzed
(New_A
, False);
3114 Append
(New_A
, Aspect_Specifications
(N
));
3115 Analyze_Aspect_Specifications
(N
, Formal
);
3124 Push_Scope
(Formal
);
3126 -- Manually set the SPARK_Mode from the context because the package
3127 -- declaration is never analyzed.
3129 Set_SPARK_Pragma
(Formal
, SPARK_Mode_Pragma
);
3130 Set_SPARK_Aux_Pragma
(Formal
, SPARK_Mode_Pragma
);
3131 Set_SPARK_Pragma_Inherited
(Formal
);
3132 Set_SPARK_Aux_Pragma_Inherited
(Formal
);
3134 if Is_Child_Unit
(Gen_Unit
) and then Parent_Installed
then
3136 -- Similarly, we have to make the name of the formal visible in the
3137 -- parent instance, to resolve properly fully qualified names that
3138 -- may appear in the generic unit. The parent instance has been
3139 -- placed on the scope stack ahead of the current scope.
3141 Parent_Instance
:= Scope_Stack
.Table
(Scope_Stack
.Last
- 1).Entity
;
3144 Make_Defining_Identifier
(Loc
, Chars
(Gen_Unit
));
3145 Mutate_Ekind
(Renaming_In_Par
, E_Package
);
3146 Set_Etype
(Renaming_In_Par
, Standard_Void_Type
);
3147 Set_Scope
(Renaming_In_Par
, Parent_Instance
);
3148 Set_Parent
(Renaming_In_Par
, Parent
(Formal
));
3149 Set_Renamed_Entity
(Renaming_In_Par
, Formal
);
3150 Append_Entity
(Renaming_In_Par
, Parent_Instance
);
3153 -- A formal package declaration behaves as a package instantiation with
3154 -- respect to SPARK_Mode "off". If the annotation is "off" or altogether
3155 -- missing, set the global flag which signals Analyze_Pragma to ingnore
3156 -- all SPARK_Mode pragmas within the generic_package_name.
3158 if SPARK_Mode
/= On
then
3159 Ignore_SPARK_Mode_Pragmas_In_Instance
:= True;
3161 -- Mark the formal spec in case the body is instantiated at a later
3162 -- pass. This preserves the original context in effect for the body.
3164 Set_Ignore_SPARK_Mode_Pragmas
(Formal
);
3167 Analyze
(Specification
(N
));
3169 -- The formals for which associations are provided are not visible
3170 -- outside of the formal package. The others are still declared by a
3171 -- formal parameter declaration.
3173 -- If there are no associations, the only local entity to hide is the
3174 -- generated package renaming itself.
3180 E
:= First_Entity
(Formal
);
3181 while Present
(E
) loop
3182 if Associations
and then not Is_Generic_Formal
(E
) then
3186 if Ekind
(E
) = E_Package
and then Renamed_Entity
(E
) = Formal
then
3195 End_Package_Scope
(Formal
);
3196 Restore_Hidden_Primitives
(Vis_Prims_List
);
3198 if Parent_Installed
then
3204 -- Inside the generic unit, the formal package is a regular package, but
3205 -- no body is needed for it. Note that after instantiation, the defining
3206 -- unit name we need is in the new tree and not in the original (see
3207 -- Package_Instantiation). A generic formal package is an instance, and
3208 -- can be used as an actual for an inner instance.
3210 Set_Has_Completion
(Formal
, True);
3212 -- Add semantic information to the original defining identifier.
3214 Mutate_Ekind
(Pack_Id
, E_Package
);
3215 Set_Etype
(Pack_Id
, Standard_Void_Type
);
3216 Set_Scope
(Pack_Id
, Scope
(Formal
));
3217 Set_Has_Completion
(Pack_Id
, True);
3220 if Has_Aspects
(N
) then
3221 -- Unclear that any other aspects may appear here, analyze them
3222 -- for completion, given that the grammar allows their appearance.
3224 Analyze_Aspect_Specifications
(N
, Pack_Id
);
3227 Ignore_SPARK_Mode_Pragmas_In_Instance
:= Save_ISMP
;
3228 end Analyze_Formal_Package_Declaration
;
3230 ---------------------------------
3231 -- Analyze_Formal_Private_Type --
3232 ---------------------------------
3234 procedure Analyze_Formal_Private_Type
3240 New_Private_Type
(N
, T
, Def
);
3242 -- Set the size to an arbitrary but legal value
3244 Set_Size_Info
(T
, Standard_Integer
);
3245 Set_RM_Size
(T
, RM_Size
(Standard_Integer
));
3246 end Analyze_Formal_Private_Type
;
3248 ------------------------------------
3249 -- Analyze_Formal_Incomplete_Type --
3250 ------------------------------------
3252 procedure Analyze_Formal_Incomplete_Type
3258 Mutate_Ekind
(T
, E_Incomplete_Type
);
3260 Set_Private_Dependents
(T
, New_Elmt_List
);
3262 if Tagged_Present
(Def
) then
3263 Set_Is_Tagged_Type
(T
);
3264 Make_Class_Wide_Type
(T
);
3265 Set_Direct_Primitive_Operations
(T
, New_Elmt_List
);
3267 end Analyze_Formal_Incomplete_Type
;
3269 ----------------------------------------
3270 -- Analyze_Formal_Signed_Integer_Type --
3271 ----------------------------------------
3273 procedure Analyze_Formal_Signed_Integer_Type
3277 Base
: constant Entity_Id
:=
3279 (E_Signed_Integer_Type
,
3281 Sloc
(Defining_Identifier
(Parent
(Def
))), 'G');
3286 Mutate_Ekind
(T
, E_Signed_Integer_Subtype
);
3287 Set_Etype
(T
, Base
);
3288 Set_Size_Info
(T
, Standard_Integer
);
3289 Set_RM_Size
(T
, RM_Size
(Standard_Integer
));
3290 Set_Scalar_Range
(T
, Scalar_Range
(Standard_Integer
));
3291 Set_Is_Constrained
(T
);
3293 Set_Is_Generic_Type
(Base
);
3294 Set_Size_Info
(Base
, Standard_Integer
);
3295 Set_RM_Size
(Base
, RM_Size
(Standard_Integer
));
3296 Set_Etype
(Base
, Base
);
3297 Set_Scalar_Range
(Base
, Scalar_Range
(Standard_Integer
));
3298 Set_Parent
(Base
, Parent
(Def
));
3299 end Analyze_Formal_Signed_Integer_Type
;
3301 -------------------------------------------
3302 -- Analyze_Formal_Subprogram_Declaration --
3303 -------------------------------------------
3305 procedure Analyze_Formal_Subprogram_Declaration
(N
: Node_Id
) is
3306 Spec
: constant Node_Id
:= Specification
(N
);
3307 Def
: constant Node_Id
:= Default_Name
(N
);
3308 Expr
: constant Node_Id
:= Expression
(N
);
3309 Nam
: constant Entity_Id
:= Defining_Unit_Name
(Spec
);
3311 Parent_Installed
: Boolean := False;
3319 if Nkind
(Nam
) = N_Defining_Program_Unit_Name
then
3320 Error_Msg_N
("name of formal subprogram must be a direct name", Nam
);
3324 Check_Abbreviated_Instance
(Parent
(N
), Parent_Installed
);
3326 Analyze_Subprogram_Declaration
(N
);
3327 Set_Is_Formal_Subprogram
(Nam
);
3328 Set_Has_Completion
(Nam
);
3330 if Nkind
(N
) = N_Formal_Abstract_Subprogram_Declaration
then
3331 Set_Is_Abstract_Subprogram
(Nam
);
3333 Set_Is_Dispatching_Operation
(Nam
);
3335 -- A formal abstract procedure cannot have a null default
3336 -- (RM 12.6(4.1/2)).
3338 if Nkind
(Spec
) = N_Procedure_Specification
3339 and then Null_Present
(Spec
)
3342 ("a formal abstract subprogram cannot default to null", Spec
);
3345 -- A formal abstract function cannot have an expression default
3346 -- (expression defaults are allowed for nonabstract formal functions
3347 -- when extensions are enabled).
3349 if Nkind
(Spec
) = N_Function_Specification
3350 and then Present
(Expr
)
3353 ("a formal abstract subprogram cannot default to an expression",
3358 Ctrl_Type
: constant Entity_Id
:= Find_Dispatching_Type
(Nam
);
3360 if No
(Ctrl_Type
) then
3362 ("abstract formal subprogram must have a controlling type",
3365 elsif Ada_Version
>= Ada_2012
3366 and then Is_Incomplete_Type
(Ctrl_Type
)
3369 ("controlling type of abstract formal subprogram cannot "
3370 & "be incomplete type", N
, Ctrl_Type
);
3373 Check_Controlling_Formals
(Ctrl_Type
, Nam
);
3378 -- Default name is resolved at the point of instantiation
3380 if Box_Present
(N
) then
3383 -- Default name is bound at the point of generic declaration
3385 elsif Present
(Def
) then
3386 if Nkind
(Def
) = N_Operator_Symbol
then
3387 Find_Direct_Name
(Def
);
3389 elsif Nkind
(Def
) /= N_Attribute_Reference
then
3393 -- For an attribute reference, analyze the prefix and verify
3394 -- that it has the proper profile for the subprogram.
3396 Analyze
(Prefix
(Def
));
3397 Valid_Default_Attribute
(Nam
, Def
);
3401 -- Default name may be overloaded, in which case the interpretation
3402 -- with the correct profile must be selected, as for a renaming.
3403 -- If the definition is an indexed component, it must denote a
3404 -- member of an entry family. If it is a selected component, it
3405 -- can be a protected operation.
3407 if Etype
(Def
) = Any_Type
then
3410 elsif Nkind
(Def
) = N_Selected_Component
then
3411 if not Is_Overloadable
(Entity
(Selector_Name
(Def
))) then
3412 Error_Msg_N
("expect valid subprogram name as default", Def
);
3415 elsif Nkind
(Def
) = N_Indexed_Component
then
3416 if Is_Entity_Name
(Prefix
(Def
)) then
3417 if Ekind
(Entity
(Prefix
(Def
))) /= E_Entry_Family
then
3418 Error_Msg_N
("expect valid subprogram name as default", Def
);
3421 elsif Nkind
(Prefix
(Def
)) = N_Selected_Component
then
3422 if Ekind
(Entity
(Selector_Name
(Prefix
(Def
)))) /=
3425 Error_Msg_N
("expect valid subprogram name as default", Def
);
3429 Error_Msg_N
("expect valid subprogram name as default", Def
);
3433 elsif Nkind
(Def
) = N_Character_Literal
then
3435 -- Needs some type checks: subprogram should be parameterless???
3437 Resolve
(Def
, (Etype
(Nam
)));
3439 elsif not Is_Entity_Name
(Def
)
3440 or else not Is_Overloadable
(Entity
(Def
))
3442 Error_Msg_N
("expect valid subprogram name as default", Def
);
3445 elsif not Is_Overloaded
(Def
) then
3446 Subp
:= Entity
(Def
);
3449 Error_Msg_N
("premature usage of formal subprogram", Def
);
3451 elsif not Entity_Matches_Spec
(Subp
, Nam
) then
3452 Error_Msg_N
("no visible entity matches specification", Def
);
3455 -- More than one interpretation, so disambiguate as for a renaming
3460 I1
: Interp_Index
:= 0;
3466 Get_First_Interp
(Def
, I
, It
);
3467 while Present
(It
.Nam
) loop
3468 if Entity_Matches_Spec
(It
.Nam
, Nam
) then
3469 if Subp
/= Any_Id
then
3470 It1
:= Disambiguate
(Def
, I1
, I
, Etype
(Subp
));
3472 if It1
= No_Interp
then
3473 Error_Msg_N
("ambiguous default subprogram", Def
);
3486 Get_Next_Interp
(I
, It
);
3490 if Subp
/= Any_Id
then
3492 -- Subprogram found, generate reference to it
3494 Set_Entity
(Def
, Subp
);
3495 Generate_Reference
(Subp
, Def
);
3498 Error_Msg_N
("premature usage of formal subprogram", Def
);
3500 elsif Ekind
(Subp
) /= E_Operator
then
3501 Check_Mode_Conformant
(Subp
, Nam
);
3505 Error_Msg_N
("no visible subprogram matches specification", N
);
3509 -- When extensions are enabled, an expression can be given as default
3510 -- for a formal function. The expression must be of the function result
3511 -- type and can reference formal parameters of the function.
3513 elsif Present
(Expr
) then
3515 Install_Formals
(Nam
);
3516 Preanalyze_Spec_Expression
(Expr
, Etype
(Nam
));
3521 if Has_Aspects
(N
) then
3522 Analyze_Aspect_Specifications
(N
, Nam
);
3525 if Parent_Installed
then
3528 end Analyze_Formal_Subprogram_Declaration
;
3530 -------------------------------------
3531 -- Analyze_Formal_Type_Declaration --
3532 -------------------------------------
3534 procedure Analyze_Formal_Type_Declaration
(N
: Node_Id
) is
3535 Def
: constant Node_Id
:= Formal_Type_Definition
(N
);
3537 Parent_Installed
: Boolean := False;
3541 T
:= Defining_Identifier
(N
);
3543 if Present
(Discriminant_Specifications
(N
))
3544 and then Nkind
(Def
) /= N_Formal_Private_Type_Definition
3547 ("discriminants not allowed for this formal type", T
);
3550 Check_Abbreviated_Instance
(Parent
(N
), Parent_Installed
);
3552 -- Enter the new name, and branch to specific routine
3555 when N_Formal_Private_Type_Definition
=>
3556 Analyze_Formal_Private_Type
(N
, T
, Def
);
3558 when N_Formal_Derived_Type_Definition
=>
3559 Analyze_Formal_Derived_Type
(N
, T
, Def
);
3561 when N_Formal_Incomplete_Type_Definition
=>
3562 Analyze_Formal_Incomplete_Type
(T
, Def
);
3564 when N_Formal_Discrete_Type_Definition
=>
3565 Analyze_Formal_Discrete_Type
(T
, Def
);
3567 when N_Formal_Signed_Integer_Type_Definition
=>
3568 Analyze_Formal_Signed_Integer_Type
(T
, Def
);
3570 when N_Formal_Modular_Type_Definition
=>
3571 Analyze_Formal_Modular_Type
(T
, Def
);
3573 when N_Formal_Floating_Point_Definition
=>
3574 Analyze_Formal_Floating_Type
(T
, Def
);
3576 when N_Formal_Ordinary_Fixed_Point_Definition
=>
3577 Analyze_Formal_Ordinary_Fixed_Point_Type
(T
, Def
);
3579 when N_Formal_Decimal_Fixed_Point_Definition
=>
3580 Analyze_Formal_Decimal_Fixed_Point_Type
(T
, Def
);
3582 when N_Array_Type_Definition
=>
3583 Analyze_Formal_Array_Type
(T
, Def
);
3585 when N_Access_Function_Definition
3586 | N_Access_Procedure_Definition
3587 | N_Access_To_Object_Definition
3589 Analyze_Generic_Access_Type
(T
, Def
);
3591 -- Ada 2005: a interface declaration is encoded as an abstract
3592 -- record declaration or a abstract type derivation.
3594 when N_Record_Definition
=>
3595 Analyze_Formal_Interface_Type
(N
, T
, Def
);
3597 when N_Derived_Type_Definition
=>
3598 Analyze_Formal_Derived_Interface_Type
(N
, T
, Def
);
3604 raise Program_Error
;
3607 -- A formal type declaration declares a type and its first
3610 Set_Is_Generic_Type
(T
);
3611 Set_Is_First_Subtype
(T
);
3613 if Present
(Default_Subtype_Mark
(Original_Node
(N
))) then
3614 Validate_Formal_Type_Default
(N
);
3617 if Has_Aspects
(N
) then
3618 Analyze_Aspect_Specifications
(N
, T
);
3621 if Parent_Installed
then
3624 end Analyze_Formal_Type_Declaration
;
3626 ------------------------------------
3627 -- Analyze_Function_Instantiation --
3628 ------------------------------------
3630 procedure Analyze_Function_Instantiation
(N
: Node_Id
) is
3632 Analyze_Subprogram_Instantiation
(N
, E_Function
);
3633 end Analyze_Function_Instantiation
;
3635 ---------------------------------
3636 -- Analyze_Generic_Access_Type --
3637 ---------------------------------
3639 procedure Analyze_Generic_Access_Type
(T
: Entity_Id
; Def
: Node_Id
) is
3643 if Nkind
(Def
) = N_Access_To_Object_Definition
then
3644 Access_Type_Declaration
(T
, Def
);
3646 if Is_Incomplete_Or_Private_Type
(Designated_Type
(T
))
3647 and then No
(Full_View
(Designated_Type
(T
)))
3648 and then not Is_Generic_Type
(Designated_Type
(T
))
3650 Error_Msg_N
("premature usage of incomplete type", Def
);
3652 elsif not Is_Entity_Name
(Subtype_Indication
(Def
)) then
3654 ("only a subtype mark is allowed in a formal", Def
);
3658 Access_Subprogram_Declaration
(T
, Def
);
3660 end Analyze_Generic_Access_Type
;
3662 ---------------------------------
3663 -- Analyze_Generic_Formal_Part --
3664 ---------------------------------
3666 procedure Analyze_Generic_Formal_Part
(N
: Node_Id
) is
3667 Gen_Parm_Decl
: Node_Id
;
3670 -- The generic formals are processed in the scope of the generic unit,
3671 -- where they are immediately visible. The scope is installed by the
3674 Gen_Parm_Decl
:= First
(Generic_Formal_Declarations
(N
));
3675 while Present
(Gen_Parm_Decl
) loop
3676 Analyze
(Gen_Parm_Decl
);
3677 Next
(Gen_Parm_Decl
);
3680 Generate_Reference_To_Generic_Formals
(Current_Scope
);
3682 -- For Ada 2022, some formal parameters can carry aspects, which must
3683 -- be name-resolved at the end of the list of formal parameters (which
3684 -- has the semantics of a declaration list).
3686 Analyze_Contracts
(Generic_Formal_Declarations
(N
));
3687 end Analyze_Generic_Formal_Part
;
3689 ------------------------------------------
3690 -- Analyze_Generic_Package_Declaration --
3691 ------------------------------------------
3693 procedure Analyze_Generic_Package_Declaration
(N
: Node_Id
) is
3694 Decls
: constant List_Id
:= Visible_Declarations
(Specification
(N
));
3695 Loc
: constant Source_Ptr
:= Sloc
(N
);
3701 Save_Parent
: Node_Id
;
3704 -- A generic may grant access to its private enclosing context depending
3705 -- on the placement of its corresponding body. From elaboration point of
3706 -- view, the flow of execution may enter this private context, and then
3707 -- reach an external unit, thus producing a dependency on that external
3708 -- unit. For such a path to be properly discovered and encoded in the
3709 -- ALI file of the main unit, let the ABE mechanism process the body of
3710 -- the main unit, and encode all relevant invocation constructs and the
3711 -- relations between them.
3713 Mark_Save_Invocation_Graph_Of_Body
;
3715 -- We introduce a renaming of the enclosing package, to have a usable
3716 -- entity as the prefix of an expanded name for a local entity of the
3717 -- form Par.P.Q, where P is the generic package. This is because a local
3718 -- entity named P may hide it, so that the usual visibility rules in
3719 -- the instance will not resolve properly.
3722 Make_Package_Renaming_Declaration
(Loc
,
3723 Defining_Unit_Name
=>
3724 Make_Defining_Identifier
(Loc
,
3725 Chars
=> New_External_Name
(Chars
(Defining_Entity
(N
)), "GH")),
3727 Make_Identifier
(Loc
, Chars
(Defining_Entity
(N
))));
3729 -- The declaration is inserted before other declarations, but before
3730 -- pragmas that may be library-unit pragmas and must appear before other
3731 -- declarations. The pragma Compile_Time_Error is not in this class, and
3732 -- may contain an expression that includes such a qualified name, so the
3733 -- renaming declaration must appear before it.
3735 -- Are there other pragmas that require this special handling ???
3737 if Present
(Decls
) then
3738 Decl
:= First
(Decls
);
3739 while Present
(Decl
)
3740 and then Nkind
(Decl
) = N_Pragma
3741 and then Get_Pragma_Id
(Decl
) /= Pragma_Compile_Time_Error
3746 if Present
(Decl
) then
3747 Insert_Before
(Decl
, Renaming
);
3749 Append
(Renaming
, Visible_Declarations
(Specification
(N
)));
3753 Set_Visible_Declarations
(Specification
(N
), New_List
(Renaming
));
3756 -- Create copy of generic unit, and save for instantiation. If the unit
3757 -- is a child unit, do not copy the specifications for the parent, which
3758 -- are not part of the generic tree.
3760 Save_Parent
:= Parent_Spec
(N
);
3761 Set_Parent_Spec
(N
, Empty
);
3763 New_N
:= Copy_Generic_Node
(N
, Empty
, Instantiating
=> False);
3764 Set_Parent_Spec
(New_N
, Save_Parent
);
3767 -- Once the contents of the generic copy and the template are swapped,
3768 -- do the same for their respective aspect specifications.
3770 Exchange_Aspects
(N
, New_N
);
3772 -- Collect all contract-related source pragmas found within the template
3773 -- and attach them to the contract of the package spec. This contract is
3774 -- used in the capture of global references within annotations.
3776 Create_Generic_Contract
(N
);
3778 Id
:= Defining_Entity
(N
);
3779 Generate_Definition
(Id
);
3781 -- Expansion is not applied to generic units
3786 Mutate_Ekind
(Id
, E_Generic_Package
);
3787 Set_Etype
(Id
, Standard_Void_Type
);
3789 -- Set SPARK_Mode from context
3791 Set_SPARK_Pragma
(Id
, SPARK_Mode_Pragma
);
3792 Set_SPARK_Aux_Pragma
(Id
, SPARK_Mode_Pragma
);
3793 Set_SPARK_Pragma_Inherited
(Id
);
3794 Set_SPARK_Aux_Pragma_Inherited
(Id
);
3796 -- Preserve relevant elaboration-related attributes of the context which
3797 -- are no longer available or very expensive to recompute once analysis,
3798 -- resolution, and expansion are over.
3800 Mark_Elaboration_Attributes
3805 -- Analyze aspects now, so that generated pragmas appear in the
3806 -- declarations before building and analyzing the generic copy.
3808 if Has_Aspects
(N
) then
3809 Analyze_Aspect_Specifications
(N
, Id
);
3813 Enter_Generic_Scope
(Id
);
3814 Set_Inner_Instances
(Id
, New_Elmt_List
);
3816 Set_Categorization_From_Pragmas
(N
);
3817 Set_Is_Pure
(Id
, Is_Pure
(Current_Scope
));
3819 -- Link the declaration of the generic homonym in the generic copy to
3820 -- the package it renames, so that it is always resolved properly.
3822 Set_Generic_Homonym
(Id
, Defining_Unit_Name
(Renaming
));
3823 Set_Entity
(Associated_Node
(Name
(Renaming
)), Id
);
3825 -- For a library unit, we have reconstructed the entity for the unit,
3826 -- and must reset it in the library tables.
3828 if Nkind
(Parent
(N
)) = N_Compilation_Unit
then
3829 Set_Cunit_Entity
(Current_Sem_Unit
, Id
);
3832 Analyze_Generic_Formal_Part
(N
);
3834 -- After processing the generic formals, analysis proceeds as for a
3835 -- non-generic package.
3837 Analyze
(Specification
(N
));
3839 Validate_Categorization_Dependency
(N
, Id
);
3843 End_Package_Scope
(Id
);
3844 Exit_Generic_Scope
(Id
);
3846 -- If the generic appears within a package unit, the body of that unit
3847 -- has to be present for instantiation and inlining.
3849 if Nkind
(Unit
(Cunit
(Current_Sem_Unit
))) = N_Package_Declaration
then
3850 Set_Body_Needed_For_Inlining
3851 (Defining_Entity
(Unit
(Cunit
(Current_Sem_Unit
))));
3854 if Nkind
(Parent
(N
)) /= N_Compilation_Unit
then
3855 Move_Freeze_Nodes
(Id
, N
, Visible_Declarations
(Specification
(N
)));
3856 Move_Freeze_Nodes
(Id
, N
, Private_Declarations
(Specification
(N
)));
3857 Move_Freeze_Nodes
(Id
, N
, Generic_Formal_Declarations
(N
));
3860 Set_Body_Required
(Parent
(N
), Unit_Requires_Body
(Id
));
3861 Validate_RT_RAT_Component
(N
);
3863 -- If this is a spec without a body, check that generic parameters
3866 if not Body_Required
(Parent
(N
)) then
3867 Check_References
(Id
);
3871 -- If there is a specified storage pool in the context, create an
3872 -- aspect on the package declaration, so that it is used in any
3873 -- instance that does not override it.
3875 if Present
(Default_Pool
) then
3881 Make_Aspect_Specification
(Loc
,
3882 Identifier
=> Make_Identifier
(Loc
, Name_Default_Storage_Pool
),
3883 Expression
=> New_Copy
(Default_Pool
));
3885 if No
(Aspect_Specifications
(Specification
(N
))) then
3886 Set_Aspect_Specifications
(Specification
(N
), New_List
(ASN
));
3888 Append
(ASN
, Aspect_Specifications
(Specification
(N
)));
3892 end Analyze_Generic_Package_Declaration
;
3894 --------------------------------------------
3895 -- Analyze_Generic_Subprogram_Declaration --
3896 --------------------------------------------
3898 procedure Analyze_Generic_Subprogram_Declaration
(N
: Node_Id
) is
3902 Result_Type
: Entity_Id
;
3903 Save_Parent
: Node_Id
;
3908 -- A generic may grant access to its private enclosing context depending
3909 -- on the placement of its corresponding body. From elaboration point of
3910 -- view, the flow of execution may enter this private context, and then
3911 -- reach an external unit, thus producing a dependency on that external
3912 -- unit. For such a path to be properly discovered and encoded in the
3913 -- ALI file of the main unit, let the ABE mechanism process the body of
3914 -- the main unit, and encode all relevant invocation constructs and the
3915 -- relations between them.
3917 Mark_Save_Invocation_Graph_Of_Body
;
3919 -- Create copy of generic unit, and save for instantiation. If the unit
3920 -- is a child unit, do not copy the specifications for the parent, which
3921 -- are not part of the generic tree.
3923 Save_Parent
:= Parent_Spec
(N
);
3924 Set_Parent_Spec
(N
, Empty
);
3926 New_N
:= Copy_Generic_Node
(N
, Empty
, Instantiating
=> False);
3927 Set_Parent_Spec
(New_N
, Save_Parent
);
3930 -- Once the contents of the generic copy and the template are swapped,
3931 -- do the same for their respective aspect specifications.
3933 Exchange_Aspects
(N
, New_N
);
3935 -- Collect all contract-related source pragmas found within the template
3936 -- and attach them to the contract of the subprogram spec. This contract
3937 -- is used in the capture of global references within annotations.
3939 Create_Generic_Contract
(N
);
3941 Spec
:= Specification
(N
);
3942 Id
:= Defining_Entity
(Spec
);
3943 Generate_Definition
(Id
);
3945 if Nkind
(Id
) = N_Defining_Operator_Symbol
then
3947 ("operator symbol not allowed for generic subprogram", Id
);
3953 Set_Scope_Depth_Value
(Id
, Scope_Depth
(Current_Scope
) + 1);
3956 Enter_Generic_Scope
(Id
);
3957 Set_Inner_Instances
(Id
, New_Elmt_List
);
3958 Set_Is_Pure
(Id
, Is_Pure
(Current_Scope
));
3960 Analyze_Generic_Formal_Part
(N
);
3962 if Nkind
(Spec
) = N_Function_Specification
then
3963 Mutate_Ekind
(Id
, E_Generic_Function
);
3965 Mutate_Ekind
(Id
, E_Generic_Procedure
);
3968 -- Set SPARK_Mode from context
3970 Set_SPARK_Pragma
(Id
, SPARK_Mode_Pragma
);
3971 Set_SPARK_Pragma_Inherited
(Id
);
3973 -- Preserve relevant elaboration-related attributes of the context which
3974 -- are no longer available or very expensive to recompute once analysis,
3975 -- resolution, and expansion are over.
3977 Mark_Elaboration_Attributes
3982 Formals
:= Parameter_Specifications
(Spec
);
3984 if Present
(Formals
) then
3985 Process_Formals
(Formals
, Spec
);
3988 if Nkind
(Spec
) = N_Function_Specification
then
3989 if Nkind
(Result_Definition
(Spec
)) = N_Access_Definition
then
3990 Result_Type
:= Access_Definition
(Spec
, Result_Definition
(Spec
));
3991 Set_Etype
(Id
, Result_Type
);
3993 -- Check restriction imposed by AI05-073: a generic function
3994 -- cannot return an abstract type or an access to such.
3996 if Is_Abstract_Type
(Designated_Type
(Result_Type
)) then
3998 ("generic function cannot have an access result "
3999 & "that designates an abstract type", Spec
);
4003 Find_Type
(Result_Definition
(Spec
));
4004 Typ
:= Entity
(Result_Definition
(Spec
));
4006 if Is_Abstract_Type
(Typ
)
4007 and then Ada_Version
>= Ada_2012
4010 ("generic function cannot have abstract result type", Spec
);
4013 -- If a null exclusion is imposed on the result type, then create
4014 -- a null-excluding itype (an access subtype) and use it as the
4015 -- function's Etype.
4017 if Is_Access_Type
(Typ
)
4018 and then Null_Exclusion_Present
(Spec
)
4021 Create_Null_Excluding_Itype
4023 Related_Nod
=> Spec
,
4024 Scope_Id
=> Defining_Unit_Name
(Spec
)));
4026 Set_Etype
(Id
, Typ
);
4031 Set_Etype
(Id
, Standard_Void_Type
);
4034 -- Analyze the aspects of the generic copy to ensure that all generated
4035 -- pragmas (if any) perform their semantic effects.
4037 if Has_Aspects
(N
) then
4038 Analyze_Aspect_Specifications
(N
, Id
);
4041 -- For a library unit, we have reconstructed the entity for the unit,
4042 -- and must reset it in the library tables. We also make sure that
4043 -- Body_Required is set properly in the original compilation unit node.
4045 if Nkind
(Parent
(N
)) = N_Compilation_Unit
then
4046 Set_Cunit_Entity
(Current_Sem_Unit
, Id
);
4047 Set_Body_Required
(Parent
(N
), Unit_Requires_Body
(Id
));
4050 -- If the generic appears within a package unit, the body of that unit
4051 -- has to be present for instantiation and inlining.
4053 if Nkind
(Unit
(Cunit
(Current_Sem_Unit
))) = N_Package_Declaration
4054 and then Unit_Requires_Body
(Id
)
4056 Set_Body_Needed_For_Inlining
4057 (Defining_Entity
(Unit
(Cunit
(Current_Sem_Unit
))));
4060 Set_Categorization_From_Pragmas
(N
);
4061 Validate_Categorization_Dependency
(N
, Id
);
4063 -- Capture all global references that occur within the profile of the
4064 -- generic subprogram. Aspects are not part of this processing because
4065 -- they must be delayed. If processed now, Save_Global_References will
4066 -- destroy the Associated_Node links and prevent the capture of global
4067 -- references when the contract of the generic subprogram is analyzed.
4069 Save_Global_References
(Original_Node
(N
));
4073 Exit_Generic_Scope
(Id
);
4074 Generate_Reference_To_Formals
(Id
);
4076 List_Inherited_Pre_Post_Aspects
(Id
);
4077 end Analyze_Generic_Subprogram_Declaration
;
4079 -----------------------------------
4080 -- Analyze_Package_Instantiation --
4081 -----------------------------------
4083 -- WARNING: This routine manages Ghost and SPARK regions. Return statements
4084 -- must be replaced by gotos which jump to the end of the routine in order
4085 -- to restore the Ghost and SPARK modes.
4087 procedure Analyze_Package_Instantiation
(N
: Node_Id
) is
4088 Has_Inline_Always
: Boolean := False;
4089 -- Set if the generic unit contains any subprograms with Inline_Always.
4090 -- Only relevant when back-end inlining is not enabled.
4092 function Might_Inline_Subp
(Gen_Unit
: Entity_Id
) return Boolean;
4093 -- Return True if inlining is active and Gen_Unit contains inlined
4094 -- subprograms. In this case, we may either instantiate the body when
4095 -- front-end inlining is enabled, or add a pending instantiation when
4096 -- back-end inlining is enabled. In the former case, this may cause
4097 -- superfluous instantiations, but in either case we need to perform
4098 -- the instantiation of the body in the context of the instance and
4099 -- not in that of the point of inlining.
4101 function Needs_Body_Instantiated
(Gen_Unit
: Entity_Id
) return Boolean;
4102 -- Return True if Gen_Unit needs to have its body instantiated in the
4103 -- context of N. This in particular excludes generic contexts.
4105 -----------------------
4106 -- Might_Inline_Subp --
4107 -----------------------
4109 function Might_Inline_Subp
(Gen_Unit
: Entity_Id
) return Boolean is
4113 if Inline_Processing_Required
then
4114 -- No need to recompute the answer if we know it is positive
4115 -- and back-end inlining is enabled.
4117 if Is_Inlined
(Gen_Unit
) and then Back_End_Inlining
then
4121 E
:= First_Entity
(Gen_Unit
);
4122 while Present
(E
) loop
4123 if Is_Subprogram
(E
) and then Is_Inlined
(E
) then
4124 -- Remember if there are any subprograms with Inline_Always
4126 if Has_Pragma_Inline_Always
(E
) then
4127 Has_Inline_Always
:= True;
4130 Set_Is_Inlined
(Gen_Unit
);
4139 end Might_Inline_Subp
;
4141 -------------------------------
4142 -- Needs_Body_Instantiated --
4143 -------------------------------
4145 function Needs_Body_Instantiated
(Gen_Unit
: Entity_Id
) return Boolean is
4147 -- No need to instantiate bodies in generic units
4149 if Is_Generic_Unit
(Cunit_Entity
(Main_Unit
)) then
4153 -- If the instantiation is in the main unit, then the body is needed
4155 if Is_In_Main_Unit
(N
) then
4159 -- In GNATprove mode, never instantiate bodies outside of the main
4160 -- unit, as it does not use frontend/backend inlining in the way that
4161 -- GNAT does, so does not benefit from such instantiations. On the
4162 -- contrary, such instantiations may bring artificial constraints,
4163 -- as for example such bodies may require preprocessing.
4165 if GNATprove_Mode
then
4169 -- If not, then again no need to instantiate bodies in generic units
4171 if Is_Generic_Unit
(Cunit_Entity
(Get_Code_Unit
(N
))) then
4175 -- Here we have a special handling for back-end inlining: if inline
4176 -- processing is required, then we unconditionally want to have the
4177 -- body instantiated. The reason is that Might_Inline_Subp does not
4178 -- catch all the cases (as it does not recurse into nested packages)
4179 -- so this avoids the need to patch things up afterwards. Moreover,
4180 -- these instantiations are only performed on demand when back-end
4181 -- inlining is enabled, so this causes very little extra work.
4183 if Inline_Processing_Required
and then Back_End_Inlining
then
4187 -- We want to have the bodies instantiated in non-main units if
4188 -- they might contribute inlined subprograms.
4190 return Might_Inline_Subp
(Gen_Unit
);
4191 end Needs_Body_Instantiated
;
4193 -- Local declarations
4195 Gen_Id
: constant Node_Id
:= Name
(N
);
4196 Inst_Id
: constant Entity_Id
:= Defining_Entity
(N
);
4197 Is_Actual_Pack
: constant Boolean := Is_Internal
(Inst_Id
);
4198 Loc
: constant Source_Ptr
:= Sloc
(N
);
4200 Saved_GM
: constant Ghost_Mode_Type
:= Ghost_Mode
;
4201 Saved_IGR
: constant Node_Id
:= Ignored_Ghost_Region
;
4202 Saved_ISMP
: constant Boolean :=
4203 Ignore_SPARK_Mode_Pragmas_In_Instance
;
4204 Saved_SM
: constant SPARK_Mode_Type
:= SPARK_Mode
;
4205 Saved_SMP
: constant Node_Id
:= SPARK_Mode_Pragma
;
4206 -- Save the Ghost and SPARK mode-related data to restore on exit
4208 Saved_Style_Check
: constant Boolean := Style_Check
;
4209 -- Save style check mode for restore on exit
4212 Act_Decl_Name
: Node_Id
;
4213 Act_Decl_Id
: Entity_Id
;
4216 Env_Installed
: Boolean := False;
4219 Gen_Unit
: Entity_Id
;
4220 Inline_Now
: Boolean := False;
4221 Needs_Body
: Boolean;
4222 Parent_Installed
: Boolean := False;
4223 Renaming_List
: List_Id
;
4224 Unit_Renaming
: Node_Id
;
4226 Vis_Prims_List
: Elist_Id
:= No_Elist
;
4227 -- List of primitives made temporarily visible in the instantiation
4228 -- to match the visibility of the formal type
4230 -- Start of processing for Analyze_Package_Instantiation
4233 -- Preserve relevant elaboration-related attributes of the context which
4234 -- are no longer available or very expensive to recompute once analysis,
4235 -- resolution, and expansion are over.
4237 Mark_Elaboration_Attributes
4244 -- Very first thing: check for Text_IO special unit in case we are
4245 -- instantiating one of the children of [[Wide_]Wide_]Text_IO.
4247 Check_Text_IO_Special_Unit
(Name
(N
));
4249 -- Make node global for error reporting
4251 Instantiation_Node
:= N
;
4253 -- Case of instantiation of a generic package
4255 if Nkind
(N
) = N_Package_Instantiation
then
4256 Act_Decl_Id
:= New_Copy
(Defining_Entity
(N
));
4257 Set_Comes_From_Source
(Act_Decl_Id
, True);
4259 if Nkind
(Defining_Unit_Name
(N
)) = N_Defining_Program_Unit_Name
then
4261 Make_Defining_Program_Unit_Name
(Loc
,
4263 New_Copy_Tree
(Name
(Defining_Unit_Name
(N
))),
4264 Defining_Identifier
=> Act_Decl_Id
);
4266 Act_Decl_Name
:= Act_Decl_Id
;
4269 -- Case of instantiation of a formal package
4272 Act_Decl_Id
:= Defining_Identifier
(N
);
4273 Act_Decl_Name
:= Act_Decl_Id
;
4276 Generate_Definition
(Act_Decl_Id
);
4277 Mutate_Ekind
(Act_Decl_Id
, E_Package
);
4279 -- Initialize list of incomplete actuals before analysis
4281 Set_Incomplete_Actuals
(Act_Decl_Id
, New_Elmt_List
);
4283 Preanalyze_Actuals
(N
, Act_Decl_Id
);
4285 -- Turn off style checking in instances. If the check is enabled on the
4286 -- generic unit, a warning in an instance would just be noise. If not
4287 -- enabled on the generic, then a warning in an instance is just wrong.
4288 -- This must be done after analyzing the actuals, which do come from
4289 -- source and are subject to style checking.
4291 Style_Check
:= False;
4294 Env_Installed
:= True;
4296 -- Reset renaming map for formal types. The mapping is established
4297 -- when analyzing the generic associations, but some mappings are
4298 -- inherited from formal packages of parent units, and these are
4299 -- constructed when the parents are installed.
4301 Generic_Renamings
.Set_Last
(0);
4302 Generic_Renamings_HTable
.Reset
;
4304 -- Except for an abbreviated instance created to check a formal package,
4305 -- install the parent if this is a generic child unit.
4307 if not Is_Abbreviated_Instance
(Inst_Id
) then
4308 Check_Generic_Child_Unit
(Gen_Id
, Parent_Installed
);
4311 Gen_Unit
:= Entity
(Gen_Id
);
4313 -- A package instantiation is Ghost when it is subject to pragma Ghost
4314 -- or the generic template is Ghost. Set the mode now to ensure that
4315 -- any nodes generated during analysis and expansion are marked as
4318 Mark_And_Set_Ghost_Instantiation
(N
, Gen_Unit
);
4320 -- Verify that it is the name of a generic package
4322 -- A visibility glitch: if the instance is a child unit and the generic
4323 -- is the generic unit of a parent instance (i.e. both the parent and
4324 -- the child units are instances of the same package) the name now
4325 -- denotes the renaming within the parent, not the intended generic
4326 -- unit. See if there is a homonym that is the desired generic. The
4327 -- renaming declaration must be visible inside the instance of the
4328 -- child, but not when analyzing the name in the instantiation itself.
4330 if Ekind
(Gen_Unit
) = E_Package
4331 and then Present
(Renamed_Entity
(Gen_Unit
))
4332 and then In_Open_Scopes
(Renamed_Entity
(Gen_Unit
))
4333 and then Is_Generic_Instance
(Renamed_Entity
(Gen_Unit
))
4334 and then Present
(Homonym
(Gen_Unit
))
4336 Gen_Unit
:= Homonym
(Gen_Unit
);
4339 if Etype
(Gen_Unit
) = Any_Type
then
4343 elsif Ekind
(Gen_Unit
) /= E_Generic_Package
then
4345 -- Ada 2005 (AI-50217): Cannot use instance in limited with_clause
4347 if From_Limited_With
(Gen_Unit
) then
4349 ("cannot instantiate a limited withed package", Gen_Id
);
4352 ("& is not the name of a generic package", Gen_Id
, Gen_Unit
);
4359 if In_Extended_Main_Source_Unit
(N
) then
4360 Set_Is_Instantiated
(Gen_Unit
);
4361 Generate_Reference
(Gen_Unit
, N
);
4363 if Present
(Renamed_Entity
(Gen_Unit
)) then
4364 Set_Is_Instantiated
(Renamed_Entity
(Gen_Unit
));
4365 Generate_Reference
(Renamed_Entity
(Gen_Unit
), N
);
4369 if Nkind
(Gen_Id
) = N_Identifier
4370 and then Chars
(Gen_Unit
) = Chars
(Defining_Entity
(N
))
4373 ("& is hidden within declaration of instance", Gen_Id
, Gen_Unit
);
4375 elsif Nkind
(Gen_Id
) = N_Expanded_Name
4376 and then Is_Child_Unit
(Gen_Unit
)
4377 and then Nkind
(Prefix
(Gen_Id
)) = N_Identifier
4378 and then Chars
(Act_Decl_Id
) = Chars
(Prefix
(Gen_Id
))
4381 ("& is hidden within declaration of instance", Prefix
(Gen_Id
));
4384 Set_Entity
(Gen_Id
, Gen_Unit
);
4386 -- If generic is a renaming, get original generic unit
4388 if Present
(Renamed_Entity
(Gen_Unit
))
4389 and then Ekind
(Renamed_Entity
(Gen_Unit
)) = E_Generic_Package
4391 Gen_Unit
:= Renamed_Entity
(Gen_Unit
);
4394 -- Verify that there are no circular instantiations
4396 if In_Open_Scopes
(Gen_Unit
) then
4397 Error_Msg_NE
("instantiation of & within itself", N
, Gen_Unit
);
4401 elsif Contains_Instance_Of
(Gen_Unit
, Current_Scope
, Gen_Id
) then
4402 Error_Msg_Node_2
:= Current_Scope
;
4404 ("circular instantiation: & instantiated in &!", N
, Gen_Unit
);
4405 Circularity_Detected
:= True;
4410 Mutate_Ekind
(Inst_Id
, E_Package
);
4411 Set_Scope
(Inst_Id
, Current_Scope
);
4413 -- If the context of the instance is subject to SPARK_Mode "off" or
4414 -- the annotation is altogether missing, set the global flag which
4415 -- signals Analyze_Pragma to ignore all SPARK_Mode pragmas within
4418 if SPARK_Mode
/= On
then
4419 Ignore_SPARK_Mode_Pragmas_In_Instance
:= True;
4421 -- Mark the instance spec in case the body is instantiated at a
4422 -- later pass. This preserves the original context in effect for
4425 Set_Ignore_SPARK_Mode_Pragmas
(Act_Decl_Id
);
4428 Gen_Decl
:= Unit_Declaration_Node
(Gen_Unit
);
4429 Gen_Spec
:= Specification
(Gen_Decl
);
4431 -- Initialize renamings map, for error checking, and the list that
4432 -- holds private entities whose views have changed between generic
4433 -- definition and instantiation. If this is the instance created to
4434 -- validate an actual package, the instantiation environment is that
4435 -- of the enclosing instance.
4437 Create_Instantiation_Source
(N
, Gen_Unit
, S_Adjustment
);
4439 -- Copy original generic tree, to produce text for instantiation
4443 (Original_Node
(Gen_Decl
), Empty
, Instantiating
=> True);
4445 Act_Spec
:= Specification
(Act_Tree
);
4447 -- If this is the instance created to validate an actual package,
4448 -- only the formals matter, do not examine the package spec itself.
4450 if Is_Actual_Pack
then
4451 Set_Visible_Declarations
(Act_Spec
, New_List
);
4452 Set_Private_Declarations
(Act_Spec
, New_List
);
4456 Analyze_Associations
4458 Formals
=> Generic_Formal_Declarations
(Act_Tree
),
4459 F_Copy
=> Generic_Formal_Declarations
(Gen_Decl
));
4461 Vis_Prims_List
:= Check_Hidden_Primitives
(Renaming_List
);
4463 Set_Instance_Env
(Gen_Unit
, Act_Decl_Id
);
4464 Set_Defining_Unit_Name
(Act_Spec
, Act_Decl_Name
);
4465 Set_Is_Generic_Instance
(Act_Decl_Id
);
4466 Set_Generic_Parent
(Act_Spec
, Gen_Unit
);
4468 -- References to the generic in its own declaration or its body are
4469 -- references to the instance. Add a renaming declaration for the
4470 -- generic unit itself. This declaration, as well as the renaming
4471 -- declarations for the generic formals, must remain private to the
4472 -- unit: the formals, because this is the language semantics, and
4473 -- the unit because its use is an artifact of the implementation.
4476 Make_Package_Renaming_Declaration
(Loc
,
4477 Defining_Unit_Name
=>
4478 Make_Defining_Identifier
(Loc
, Chars
(Gen_Unit
)),
4479 Name
=> New_Occurrence_Of
(Act_Decl_Id
, Loc
));
4481 Append
(Unit_Renaming
, Renaming_List
);
4483 -- The renaming declarations are the first local declarations of the
4486 if Is_Non_Empty_List
(Visible_Declarations
(Act_Spec
)) then
4488 (First
(Visible_Declarations
(Act_Spec
)), Renaming_List
);
4490 Set_Visible_Declarations
(Act_Spec
, Renaming_List
);
4493 Act_Decl
:= Make_Package_Declaration
(Loc
, Specification
=> Act_Spec
);
4495 -- Propagate the aspect specifications from the package declaration
4496 -- template to the instantiated version of the package declaration.
4498 if Has_Aspects
(Act_Tree
) then
4499 Set_Aspect_Specifications
(Act_Decl
,
4500 New_Copy_List_Tree
(Aspect_Specifications
(Act_Tree
)));
4503 -- The generic may have a generated Default_Storage_Pool aspect,
4504 -- set at the point of generic declaration. If the instance has
4505 -- that aspect, it overrides the one inherited from the generic.
4507 if Has_Aspects
(Gen_Spec
) then
4508 if No
(Aspect_Specifications
(N
)) then
4509 Set_Aspect_Specifications
(N
,
4511 (Aspect_Specifications
(Gen_Spec
))));
4515 Inherited_Aspects
: constant List_Id
:=
4517 (Aspect_Specifications
(Gen_Spec
));
4521 Pool_Present
: Boolean := False;
4524 ASN1
:= First
(Aspect_Specifications
(N
));
4525 while Present
(ASN1
) loop
4526 if Chars
(Identifier
(ASN1
)) =
4527 Name_Default_Storage_Pool
4529 Pool_Present
:= True;
4536 if Pool_Present
then
4538 -- If generic carries a default storage pool, remove it
4539 -- in favor of the instance one.
4541 ASN2
:= First
(Inherited_Aspects
);
4542 while Present
(ASN2
) loop
4543 if Chars
(Identifier
(ASN2
)) =
4544 Name_Default_Storage_Pool
4555 (Aspect_Specifications
(N
), Inherited_Aspects
);
4560 -- Save the instantiation node for a subsequent instantiation of the
4561 -- body if there is one and it needs to be instantiated here.
4563 -- We instantiate the body only if we are generating code, or if we
4564 -- are generating cross-reference information, or for GNATprove use.
4567 Enclosing_Body_Present
: Boolean := False;
4568 -- If the generic unit is not a compilation unit, then a body may
4569 -- be present in its parent even if none is required. We create a
4570 -- tentative pending instantiation for the body, which will be
4571 -- discarded if none is actually present.
4576 if Scope
(Gen_Unit
) /= Standard_Standard
4577 and then not Is_Child_Unit
(Gen_Unit
)
4579 Scop
:= Scope
(Gen_Unit
);
4580 while Present
(Scop
) and then Scop
/= Standard_Standard
loop
4581 if Unit_Requires_Body
(Scop
) then
4582 Enclosing_Body_Present
:= True;
4585 elsif In_Open_Scopes
(Scop
)
4586 and then In_Package_Body
(Scop
)
4588 Enclosing_Body_Present
:= True;
4592 exit when Is_Compilation_Unit
(Scop
);
4593 Scop
:= Scope
(Scop
);
4597 -- If front-end inlining is enabled or there are any subprograms
4598 -- marked with Inline_Always, and this is a unit for which code
4599 -- will be generated, we instantiate the body at once.
4601 -- This is done if the instance is not the main unit, and if the
4602 -- generic is not a child unit of another generic, to avoid scope
4603 -- problems and the reinstallation of parent instances.
4606 and then (not Is_Child_Unit
(Gen_Unit
)
4607 or else not Is_Generic_Unit
(Scope
(Gen_Unit
)))
4608 and then Might_Inline_Subp
(Gen_Unit
)
4609 and then not Is_Actual_Pack
4611 if not Back_End_Inlining
4612 and then (Front_End_Inlining
or else Has_Inline_Always
)
4613 and then (Is_In_Main_Unit
(N
)
4614 or else In_Main_Context
(Current_Scope
))
4615 and then Nkind
(Parent
(N
)) /= N_Compilation_Unit
4619 -- In configurable_run_time mode we force the inlining of
4620 -- predefined subprograms marked Inline_Always, to minimize
4621 -- the use of the run-time library.
4623 elsif In_Predefined_Unit
(Gen_Decl
)
4624 and then Configurable_Run_Time_Mode
4625 and then Nkind
(Parent
(N
)) /= N_Compilation_Unit
4630 -- If the current scope is itself an instance within a child
4631 -- unit, there will be duplications in the scope stack, and the
4632 -- unstacking mechanism in Inline_Instance_Body will fail.
4633 -- This loses some rare cases of optimization.
4635 if Is_Generic_Instance
(Current_Scope
) then
4637 Curr_Unit
: constant Entity_Id
:=
4638 Cunit_Entity
(Current_Sem_Unit
);
4640 if Curr_Unit
/= Current_Scope
4641 and then Is_Child_Unit
(Curr_Unit
)
4643 Inline_Now
:= False;
4650 (Unit_Requires_Body
(Gen_Unit
)
4651 or else Enclosing_Body_Present
4652 or else Present
(Corresponding_Body
(Gen_Decl
)))
4653 and then Needs_Body_Instantiated
(Gen_Unit
)
4654 and then not Is_Actual_Pack
4655 and then not Inline_Now
4656 and then (Operating_Mode
= Generate_Code
4657 or else (Operating_Mode
= Check_Semantics
4658 and then GNATprove_Mode
));
4660 -- If front-end inlining is enabled or there are any subprograms
4661 -- marked with Inline_Always, do not instantiate body when within
4662 -- a generic context.
4664 if not Back_End_Inlining
4665 and then (Front_End_Inlining
or else Has_Inline_Always
)
4666 and then not Expander_Active
4668 Needs_Body
:= False;
4671 -- If the current context is generic, and the package being
4672 -- instantiated is declared within a formal package, there is no
4673 -- body to instantiate until the enclosing generic is instantiated
4674 -- and there is an actual for the formal package. If the formal
4675 -- package has parameters, we build a regular package instance for
4676 -- it, that precedes the original formal package declaration.
4678 if In_Open_Scopes
(Scope
(Scope
(Gen_Unit
))) then
4680 Decl
: constant Node_Id
:=
4682 (Unit_Declaration_Node
(Scope
(Gen_Unit
)));
4684 if Nkind
(Decl
) = N_Formal_Package_Declaration
4685 or else (Nkind
(Decl
) = N_Package_Declaration
4686 and then Is_List_Member
(Decl
)
4687 and then Present
(Next
(Decl
))
4689 Nkind
(Next
(Decl
)) =
4690 N_Formal_Package_Declaration
)
4692 Needs_Body
:= False;
4698 -- For RCI unit calling stubs, we omit the instance body if the
4699 -- instance is the RCI library unit itself.
4701 -- However there is a special case for nested instances: in this case
4702 -- we do generate the instance body, as it might be required, e.g.
4703 -- because it provides stream attributes for some type used in the
4704 -- profile of a remote subprogram. This is consistent with 12.3(12),
4705 -- which indicates that the instance body occurs at the place of the
4706 -- instantiation, and thus is part of the RCI declaration, which is
4707 -- present on all client partitions (this is E.2.3(18)).
4709 -- Note that AI12-0002 may make it illegal at some point to have
4710 -- stream attributes defined in an RCI unit, in which case this
4711 -- special case will become unnecessary. In the meantime, there
4712 -- is known application code in production that depends on this
4713 -- being possible, so we definitely cannot eliminate the body in
4714 -- the case of nested instances for the time being.
4716 -- When we generate a nested instance body, calling stubs for any
4717 -- relevant subprogram will be inserted immediately after the
4718 -- subprogram declarations, and will take precedence over the
4719 -- subsequent (original) body. (The stub and original body will be
4720 -- complete homographs, but this is permitted in an instance).
4721 -- (Could we do better and remove the original body???)
4723 if Distribution_Stub_Mode
= Generate_Caller_Stub_Body
4724 and then Comes_From_Source
(N
)
4725 and then Nkind
(Parent
(N
)) = N_Compilation_Unit
4727 Needs_Body
:= False;
4731 -- Indicate that the enclosing scopes contain an instantiation,
4732 -- and that cleanup actions should be delayed until after the
4733 -- instance body is expanded.
4735 Check_Forward_Instantiation
(Gen_Decl
);
4736 if Nkind
(N
) = N_Package_Instantiation
then
4738 Enclosing_Master
: Entity_Id
;
4741 -- Loop to search enclosing masters
4743 Enclosing_Master
:= Current_Scope
;
4744 Scope_Loop
: while Enclosing_Master
/= Standard_Standard
loop
4745 if Ekind
(Enclosing_Master
) = E_Package
then
4746 if Is_Compilation_Unit
(Enclosing_Master
) then
4747 if In_Package_Body
(Enclosing_Master
) then
4748 Set_Delay_Subprogram_Descriptors
4749 (Body_Entity
(Enclosing_Master
));
4751 Set_Delay_Subprogram_Descriptors
4758 Enclosing_Master
:= Scope
(Enclosing_Master
);
4761 elsif Is_Generic_Unit
(Enclosing_Master
)
4762 or else Ekind
(Enclosing_Master
) = E_Void
4764 -- Cleanup actions will eventually be performed on the
4765 -- enclosing subprogram or package instance, if any.
4766 -- Enclosing scope is void in the formal part of a
4767 -- generic subprogram.
4772 if Ekind
(Enclosing_Master
) = E_Entry
4774 Ekind
(Scope
(Enclosing_Master
)) = E_Protected_Type
4776 if not Expander_Active
then
4780 Protected_Body_Subprogram
(Enclosing_Master
);
4784 Set_Delay_Cleanups
(Enclosing_Master
);
4786 while Ekind
(Enclosing_Master
) = E_Block
loop
4787 Enclosing_Master
:= Scope
(Enclosing_Master
);
4790 if Is_Subprogram
(Enclosing_Master
) then
4791 Set_Delay_Subprogram_Descriptors
(Enclosing_Master
);
4793 elsif Is_Task_Type
(Enclosing_Master
) then
4795 TBP
: constant Node_Id
:=
4796 Get_Task_Body_Procedure
4799 if Present
(TBP
) then
4800 Set_Delay_Subprogram_Descriptors
(TBP
);
4801 Set_Delay_Cleanups
(TBP
);
4808 end loop Scope_Loop
;
4811 -- Make entry in table
4813 Add_Pending_Instantiation
(N
, Act_Decl
);
4817 Set_Categorization_From_Pragmas
(Act_Decl
);
4819 if Parent_Installed
then
4823 Set_Instance_Spec
(N
, Act_Decl
);
4825 -- If not a compilation unit, insert the package declaration before
4826 -- the original instantiation node.
4828 if Nkind
(Parent
(N
)) /= N_Compilation_Unit
then
4829 Mark_Rewrite_Insertion
(Act_Decl
);
4830 Insert_Before
(N
, Act_Decl
);
4832 if Has_Aspects
(N
) then
4833 Analyze_Aspect_Specifications
(N
, Act_Decl_Id
);
4835 -- The pragma created for a Default_Storage_Pool aspect must
4836 -- appear ahead of the declarations in the instance spec.
4837 -- Analysis has placed it after the instance node, so remove
4838 -- it and reinsert it properly now.
4841 ASN
: constant Node_Id
:= First
(Aspect_Specifications
(N
));
4842 A_Name
: constant Name_Id
:= Chars
(Identifier
(ASN
));
4846 if A_Name
= Name_Default_Storage_Pool
then
4847 if No
(Visible_Declarations
(Act_Spec
)) then
4848 Set_Visible_Declarations
(Act_Spec
, New_List
);
4852 while Present
(Decl
) loop
4853 if Nkind
(Decl
) = N_Pragma
then
4855 Prepend
(Decl
, Visible_Declarations
(Act_Spec
));
4867 -- For an instantiation that is a compilation unit, place
4868 -- declaration on current node so context is complete for analysis
4869 -- (including nested instantiations). If this is the main unit,
4870 -- the declaration eventually replaces the instantiation node.
4871 -- If the instance body is created later, it replaces the
4872 -- instance node, and the declaration is attached to it
4873 -- (see Build_Instance_Compilation_Unit_Nodes).
4876 if Cunit_Entity
(Current_Sem_Unit
) = Defining_Entity
(N
) then
4878 -- The entity for the current unit is the newly created one,
4879 -- and all semantic information is attached to it.
4881 Set_Cunit_Entity
(Current_Sem_Unit
, Act_Decl_Id
);
4883 -- If this is the main unit, replace the main entity as well
4885 if Current_Sem_Unit
= Main_Unit
then
4886 Main_Unit_Entity
:= Act_Decl_Id
;
4890 Set_Unit
(Parent
(N
), Act_Decl
);
4891 Set_Parent_Spec
(Act_Decl
, Parent_Spec
(N
));
4892 Set_Package_Instantiation
(Act_Decl_Id
, N
);
4894 -- Process aspect specifications of the instance node, if any, to
4895 -- take into account categorization pragmas before analyzing the
4898 if Has_Aspects
(N
) then
4899 Analyze_Aspect_Specifications
(N
, Act_Decl_Id
);
4903 Set_Unit
(Parent
(N
), N
);
4904 Set_Body_Required
(Parent
(N
), False);
4906 -- We never need elaboration checks on instantiations, since by
4907 -- definition, the body instantiation is elaborated at the same
4908 -- time as the spec instantiation.
4910 if Legacy_Elaboration_Checks
then
4911 Set_Kill_Elaboration_Checks
(Act_Decl_Id
);
4912 Set_Suppress_Elaboration_Warnings
(Act_Decl_Id
);
4916 if Legacy_Elaboration_Checks
then
4917 Check_Elab_Instantiation
(N
);
4920 -- Save the scenario for later examination by the ABE Processing
4923 Record_Elaboration_Scenario
(N
);
4925 -- The instantiation results in a guaranteed ABE
4927 if Is_Known_Guaranteed_ABE
(N
) and then Needs_Body
then
4928 -- Do not instantiate the corresponding body because gigi cannot
4929 -- handle certain types of premature instantiations.
4931 Remove_Dead_Instance
(N
);
4933 -- Create completing bodies for all subprogram declarations since
4934 -- their real bodies will not be instantiated.
4936 Provide_Completing_Bodies
(Instance_Spec
(N
));
4939 Check_Hidden_Child_Unit
(N
, Gen_Unit
, Act_Decl_Id
);
4941 Set_First_Private_Entity
(Defining_Unit_Name
(Unit_Renaming
),
4942 First_Private_Entity
(Act_Decl_Id
));
4944 -- If the instantiation will receive a body, the unit will be
4945 -- transformed into a package body, and receive its own elaboration
4946 -- entity. Otherwise, the nature of the unit is now a package
4949 if Nkind
(Parent
(N
)) = N_Compilation_Unit
4950 and then not Needs_Body
4952 Rewrite
(N
, Act_Decl
);
4955 if Present
(Corresponding_Body
(Gen_Decl
))
4956 or else Unit_Requires_Body
(Gen_Unit
)
4958 Set_Has_Completion
(Act_Decl_Id
);
4961 Check_Formal_Packages
(Act_Decl_Id
);
4963 Restore_Hidden_Primitives
(Vis_Prims_List
);
4964 Restore_Private_Views
(Act_Decl_Id
);
4966 Inherit_Context
(Gen_Decl
, N
);
4968 if Parent_Installed
then
4973 Env_Installed
:= False;
4976 Validate_Categorization_Dependency
(N
, Act_Decl_Id
);
4978 -- There used to be a check here to prevent instantiations in local
4979 -- contexts if the No_Local_Allocators restriction was active. This
4980 -- check was removed by a binding interpretation in AI-95-00130/07,
4981 -- but we retain the code for documentation purposes.
4983 -- if Ekind (Act_Decl_Id) /= E_Void
4984 -- and then not Is_Library_Level_Entity (Act_Decl_Id)
4986 -- Check_Restriction (No_Local_Allocators, N);
4990 Inline_Instance_Body
(N
, Gen_Unit
, Act_Decl
);
4993 -- Check that if N is an instantiation of System.Dim_Float_IO or
4994 -- System.Dim_Integer_IO, the formal type has a dimension system.
4996 if Nkind
(N
) = N_Package_Instantiation
4997 and then Is_Dim_IO_Package_Instantiation
(N
)
5000 Assoc
: constant Node_Id
:= First
(Generic_Associations
(N
));
5002 if not Has_Dimension_System
5003 (Etype
(Explicit_Generic_Actual_Parameter
(Assoc
)))
5005 Error_Msg_N
("type with a dimension system expected", Assoc
);
5011 if Has_Aspects
(N
) and then Nkind
(Parent
(N
)) /= N_Compilation_Unit
then
5012 Analyze_Aspect_Specifications
(N
, Act_Decl_Id
);
5015 Ignore_SPARK_Mode_Pragmas_In_Instance
:= Saved_ISMP
;
5016 Restore_Ghost_Region
(Saved_GM
, Saved_IGR
);
5017 Restore_SPARK_Mode
(Saved_SM
, Saved_SMP
);
5018 Style_Check
:= Saved_Style_Check
;
5021 when Instantiation_Error
=>
5022 if Parent_Installed
then
5026 if Env_Installed
then
5030 Ignore_SPARK_Mode_Pragmas_In_Instance
:= Saved_ISMP
;
5031 Restore_Ghost_Region
(Saved_GM
, Saved_IGR
);
5032 Restore_SPARK_Mode
(Saved_SM
, Saved_SMP
);
5033 Style_Check
:= Saved_Style_Check
;
5034 end Analyze_Package_Instantiation
;
5036 --------------------------
5037 -- Inline_Instance_Body --
5038 --------------------------
5040 -- WARNING: This routine manages SPARK regions. Return statements must be
5041 -- replaced by gotos which jump to the end of the routine and restore the
5044 procedure Inline_Instance_Body
5046 Gen_Unit
: Entity_Id
;
5049 Config_Attrs
: constant Config_Switches_Type
:= Save_Config_Switches
;
5051 Curr_Comp
: constant Node_Id
:= Cunit
(Current_Sem_Unit
);
5052 Curr_Unit
: constant Entity_Id
:= Cunit_Entity
(Current_Sem_Unit
);
5053 Gen_Comp
: constant Entity_Id
:=
5054 Cunit_Entity
(Get_Source_Unit
(Gen_Unit
));
5056 Scope_Stack_Depth
: constant Pos
:=
5057 Scope_Stack
.Last
- Scope_Stack
.First
+ 1;
5059 Inner_Scopes
: array (1 .. Scope_Stack_Depth
) of Entity_Id
;
5060 Instances
: array (1 .. Scope_Stack_Depth
) of Entity_Id
;
5061 Use_Clauses
: array (1 .. Scope_Stack_Depth
) of Node_Id
;
5063 Curr_Scope
: Entity_Id
:= Empty
;
5064 List
: Elist_Id
:= No_Elist
; -- init to avoid warning
5065 N_Instances
: Nat
:= 0;
5066 Num_Inner
: Nat
:= 0;
5067 Num_Scopes
: Nat
:= 0;
5068 Removed
: Boolean := False;
5073 -- Case of generic unit defined in another unit. We must remove the
5074 -- complete context of the current unit to install that of the generic.
5076 if Gen_Comp
/= Cunit_Entity
(Current_Sem_Unit
) then
5078 -- Loop through enclosing scopes until we reach a generic instance,
5079 -- package body, or subprogram.
5082 while Present
(S
) and then S
/= Standard_Standard
loop
5084 -- Save use clauses from enclosing scopes into Use_Clauses
5087 Num_Scopes
:= Num_Scopes
+ 1;
5089 Use_Clauses
(Num_Scopes
) :=
5091 (Scope_Stack
.Last
- Num_Scopes
+ 1).First_Use_Clause
);
5092 End_Use_Clauses
(Use_Clauses
(Num_Scopes
));
5094 exit when Scope_Stack
.Last
- Num_Scopes
+ 1 = Scope_Stack
.First
5095 or else Scope_Stack
.Table
5096 (Scope_Stack
.Last
- Num_Scopes
).Entity
= Scope
(S
);
5099 exit when Is_Generic_Instance
(S
)
5100 and then (In_Package_Body
(S
)
5101 or else Ekind
(S
) = E_Procedure
5102 or else Ekind
(S
) = E_Function
);
5106 Vis
:= Is_Immediately_Visible
(Gen_Comp
);
5108 -- Find and save all enclosing instances
5113 and then S
/= Standard_Standard
5115 if Is_Generic_Instance
(S
) then
5116 N_Instances
:= N_Instances
+ 1;
5117 Instances
(N_Instances
) := S
;
5119 exit when In_Package_Body
(S
);
5125 -- Remove context of current compilation unit, unless we are within a
5126 -- nested package instantiation, in which case the context has been
5127 -- removed previously.
5129 -- If current scope is the body of a child unit, remove context of
5130 -- spec as well. If an enclosing scope is an instance body, the
5131 -- context has already been removed, but the entities in the body
5132 -- must be made invisible as well.
5135 while Present
(S
) and then S
/= Standard_Standard
loop
5136 if Is_Generic_Instance
(S
)
5137 and then (In_Package_Body
(S
)
5138 or else Ekind
(S
) in E_Procedure | E_Function
)
5140 -- We still have to remove the entities of the enclosing
5141 -- instance from direct visibility.
5146 E
:= First_Entity
(S
);
5147 while Present
(E
) loop
5148 Set_Is_Immediately_Visible
(E
, False);
5157 or else (Ekind
(Curr_Unit
) = E_Package_Body
5158 and then S
= Spec_Entity
(Curr_Unit
))
5159 or else (Ekind
(Curr_Unit
) = E_Subprogram_Body
5160 and then S
= Corresponding_Spec
5161 (Unit_Declaration_Node
(Curr_Unit
)))
5165 -- Remove entities in current scopes from visibility, so that
5166 -- instance body is compiled in a clean environment.
5168 List
:= Save_Scope_Stack
(Handle_Use
=> False);
5170 if Is_Child_Unit
(S
) then
5172 -- Remove child unit from stack, as well as inner scopes.
5173 -- Removing the context of a child unit removes parent units
5176 while Current_Scope
/= S
loop
5177 Num_Inner
:= Num_Inner
+ 1;
5178 Inner_Scopes
(Num_Inner
) := Current_Scope
;
5183 Remove_Context
(Curr_Comp
);
5187 Remove_Context
(Curr_Comp
);
5190 if Ekind
(Curr_Unit
) = E_Package_Body
then
5191 Remove_Context
(Library_Unit
(Curr_Comp
));
5198 pragma Assert
(Num_Inner
< Num_Scopes
);
5200 Push_Scope
(Standard_Standard
);
5201 Scope_Stack
.Table
(Scope_Stack
.Last
).Is_Active_Stack_Base
:= True;
5203 -- The inlined package body is analyzed with the configuration state
5204 -- of the context prior to the scope manipulations performed above.
5206 -- ??? shouldn't this also use the warning state of the context prior
5207 -- to the scope manipulations?
5209 Instantiate_Package_Body
5211 ((Act_Decl
=> Act_Decl
,
5212 Config_Switches
=> Config_Attrs
,
5213 Current_Sem_Unit
=> Current_Sem_Unit
,
5214 Expander_Status
=> Expander_Active
,
5216 Local_Suppress_Stack_Top
=> Local_Suppress_Stack_Top
,
5217 Scope_Suppress
=> Scope_Suppress
,
5218 Warnings
=> Save_Warnings
)),
5219 Inlined_Body
=> True);
5225 Set_Is_Immediately_Visible
(Gen_Comp
, Vis
);
5227 -- Reset Generic_Instance flag so that use clauses can be installed
5228 -- in the proper order. (See Use_One_Package for effect of enclosing
5229 -- instances on processing of use clauses).
5231 for J
in 1 .. N_Instances
loop
5232 Set_Is_Generic_Instance
(Instances
(J
), False);
5236 Install_Context
(Curr_Comp
, Chain
=> False);
5238 if Present
(Curr_Scope
)
5239 and then Is_Child_Unit
(Curr_Scope
)
5241 Push_Scope
(Curr_Scope
);
5242 Set_Is_Immediately_Visible
(Curr_Scope
);
5244 -- Finally, restore inner scopes as well
5246 for J
in reverse 1 .. Num_Inner
loop
5247 Push_Scope
(Inner_Scopes
(J
));
5251 Restore_Scope_Stack
(List
, Handle_Use
=> False);
5253 if Present
(Curr_Scope
)
5255 (In_Private_Part
(Curr_Scope
)
5256 or else In_Package_Body
(Curr_Scope
))
5258 -- Install private declaration of ancestor units, which are
5259 -- currently available. Restore_Scope_Stack and Install_Context
5260 -- only install the visible part of parents.
5265 Par
:= Scope
(Curr_Scope
);
5266 while (Present
(Par
)) and then Par
/= Standard_Standard
loop
5267 Install_Private_Declarations
(Par
);
5274 -- Restore use clauses. For a child unit, use clauses in the parents
5275 -- are restored when installing the context, so only those in inner
5276 -- scopes (and those local to the child unit itself) need to be
5277 -- installed explicitly.
5279 if Is_Child_Unit
(Curr_Unit
) and then Removed
then
5280 for J
in reverse 1 .. Num_Inner
+ 1 loop
5281 Scope_Stack
.Table
(Scope_Stack
.Last
- J
+ 1).First_Use_Clause
:=
5283 Install_Use_Clauses
(Use_Clauses
(J
));
5287 for J
in reverse 1 .. Num_Scopes
loop
5288 Scope_Stack
.Table
(Scope_Stack
.Last
- J
+ 1).First_Use_Clause
:=
5290 Install_Use_Clauses
(Use_Clauses
(J
));
5294 -- Restore status of instances. If one of them is a body, make its
5295 -- local entities visible again.
5302 for J
in 1 .. N_Instances
loop
5303 Inst
:= Instances
(J
);
5304 Set_Is_Generic_Instance
(Inst
, True);
5306 if In_Package_Body
(Inst
)
5307 or else Ekind
(S
) in E_Procedure | E_Function
5309 E
:= First_Entity
(Instances
(J
));
5310 while Present
(E
) loop
5311 Set_Is_Immediately_Visible
(E
);
5318 -- If generic unit is in current unit, current context is correct. Note
5319 -- that the context is guaranteed to carry the correct SPARK_Mode as no
5320 -- enclosing scopes were removed.
5323 Instantiate_Package_Body
5325 ((Act_Decl
=> Act_Decl
,
5326 Config_Switches
=> Save_Config_Switches
,
5327 Current_Sem_Unit
=> Current_Sem_Unit
,
5328 Expander_Status
=> Expander_Active
,
5330 Local_Suppress_Stack_Top
=> Local_Suppress_Stack_Top
,
5331 Scope_Suppress
=> Scope_Suppress
,
5332 Warnings
=> Save_Warnings
)),
5333 Inlined_Body
=> True);
5335 end Inline_Instance_Body
;
5337 -------------------------------------
5338 -- Analyze_Procedure_Instantiation --
5339 -------------------------------------
5341 procedure Analyze_Procedure_Instantiation
(N
: Node_Id
) is
5343 Analyze_Subprogram_Instantiation
(N
, E_Procedure
);
5344 end Analyze_Procedure_Instantiation
;
5346 -----------------------------------
5347 -- Need_Subprogram_Instance_Body --
5348 -----------------------------------
5350 function Need_Subprogram_Instance_Body
5352 Subp
: Entity_Id
) return Boolean
5354 function Is_Inlined_Or_Child_Of_Inlined
(E
: Entity_Id
) return Boolean;
5355 -- Return True if E is an inlined subprogram, an inlined renaming or a
5356 -- subprogram nested in an inlined subprogram. The inlining machinery
5357 -- totally disregards nested subprograms since it considers that they
5358 -- will always be compiled if the parent is (see Inline.Is_Nested).
5360 ------------------------------------
5361 -- Is_Inlined_Or_Child_Of_Inlined --
5362 ------------------------------------
5364 function Is_Inlined_Or_Child_Of_Inlined
(E
: Entity_Id
) return Boolean is
5368 if Is_Inlined
(E
) or else Is_Inlined
(Alias
(E
)) then
5373 while Scop
/= Standard_Standard
loop
5374 if Is_Subprogram
(Scop
) and then Is_Inlined
(Scop
) then
5378 Scop
:= Scope
(Scop
);
5382 end Is_Inlined_Or_Child_Of_Inlined
;
5385 -- Must be in the main unit or inlined (or child of inlined)
5387 if (Is_In_Main_Unit
(N
) or else Is_Inlined_Or_Child_Of_Inlined
(Subp
))
5389 -- Must be generating code or analyzing code in GNATprove mode
5391 and then (Operating_Mode
= Generate_Code
5392 or else (Operating_Mode
= Check_Semantics
5393 and then GNATprove_Mode
))
5395 -- The body is needed when generating code (full expansion) and in
5396 -- in GNATprove mode (special expansion) for formal verification of
5399 and then (Expander_Active
or GNATprove_Mode
)
5401 -- No point in inlining if ABE is inevitable
5403 and then not Is_Known_Guaranteed_ABE
(N
)
5405 -- Or if subprogram is eliminated
5407 and then not Is_Eliminated
(Subp
)
5409 Add_Pending_Instantiation
(N
, Unit_Declaration_Node
(Subp
));
5412 -- Here if not inlined, or we ignore the inlining
5417 end Need_Subprogram_Instance_Body
;
5419 --------------------------------------
5420 -- Analyze_Subprogram_Instantiation --
5421 --------------------------------------
5423 -- WARNING: This routine manages Ghost and SPARK regions. Return statements
5424 -- must be replaced by gotos which jump to the end of the routine in order
5425 -- to restore the Ghost and SPARK modes.
5427 procedure Analyze_Subprogram_Instantiation
5431 Errs
: constant Nat
:= Serious_Errors_Detected
;
5432 Gen_Id
: constant Node_Id
:= Name
(N
);
5433 Inst_Id
: constant Entity_Id
:= Defining_Entity
(N
);
5434 Anon_Id
: constant Entity_Id
:=
5435 Make_Defining_Identifier
(Sloc
(Inst_Id
),
5436 Chars
=> New_External_Name
(Chars
(Inst_Id
), 'R'));
5437 Loc
: constant Source_Ptr
:= Sloc
(N
);
5439 Act_Decl_Id
: Entity_Id
:= Empty
; -- init to avoid warning
5444 Env_Installed
: Boolean := False;
5445 Gen_Unit
: Entity_Id
;
5447 Pack_Id
: Entity_Id
;
5448 Parent_Installed
: Boolean := False;
5450 Renaming_List
: List_Id
;
5451 -- The list of declarations that link formals and actuals of the
5452 -- instance. These are subtype declarations for formal types, and
5453 -- renaming declarations for other formals. The subprogram declaration
5454 -- for the instance is then appended to the list, and the last item on
5455 -- the list is the renaming declaration for the instance.
5457 procedure Analyze_Instance_And_Renamings
;
5458 -- The instance must be analyzed in a context that includes the mappings
5459 -- of generic parameters into actuals. We create a package declaration
5460 -- for this purpose, and a subprogram with an internal name within the
5461 -- package. The subprogram instance is simply an alias for the internal
5462 -- subprogram, declared in the current scope.
5464 procedure Build_Subprogram_Renaming
;
5465 -- If the subprogram is recursive, there are occurrences of the name of
5466 -- the generic within the body, which must resolve to the current
5467 -- instance. We add a renaming declaration after the declaration, which
5468 -- is available in the instance body, as well as in the analysis of
5469 -- aspects that appear in the generic. This renaming declaration is
5470 -- inserted after the instance declaration which it renames.
5472 ------------------------------------
5473 -- Analyze_Instance_And_Renamings --
5474 ------------------------------------
5476 procedure Analyze_Instance_And_Renamings
is
5477 Def_Ent
: constant Entity_Id
:= Defining_Entity
(N
);
5478 Pack_Decl
: Node_Id
;
5481 if Nkind
(Parent
(N
)) = N_Compilation_Unit
then
5483 -- For the case of a compilation unit, the container package has
5484 -- the same name as the instantiation, to insure that the binder
5485 -- calls the elaboration procedure with the right name. Copy the
5486 -- entity of the instance, which may have compilation level flags
5487 -- (e.g. Is_Child_Unit) set.
5489 Pack_Id
:= New_Copy
(Def_Ent
);
5492 -- Otherwise we use the name of the instantiation concatenated
5493 -- with its source position to ensure uniqueness if there are
5494 -- several instantiations with the same name.
5497 Make_Defining_Identifier
(Loc
,
5498 Chars
=> New_External_Name
5499 (Related_Id
=> Chars
(Def_Ent
),
5501 Suffix_Index
=> Source_Offset
(Sloc
(Def_Ent
))));
5505 Make_Package_Declaration
(Loc
,
5506 Specification
=> Make_Package_Specification
(Loc
,
5507 Defining_Unit_Name
=> Pack_Id
,
5508 Visible_Declarations
=> Renaming_List
,
5509 End_Label
=> Empty
));
5511 Set_Instance_Spec
(N
, Pack_Decl
);
5512 Set_Is_Generic_Instance
(Pack_Id
);
5513 Set_Debug_Info_Needed
(Pack_Id
);
5515 -- Case of not a compilation unit
5517 if Nkind
(Parent
(N
)) /= N_Compilation_Unit
then
5518 Mark_Rewrite_Insertion
(Pack_Decl
);
5519 Insert_Before
(N
, Pack_Decl
);
5520 Set_Has_Completion
(Pack_Id
);
5522 -- Case of an instantiation that is a compilation unit
5524 -- Place declaration on current node so context is complete for
5525 -- analysis (including nested instantiations), and for use in a
5526 -- context_clause (see Analyze_With_Clause).
5529 Set_Unit
(Parent
(N
), Pack_Decl
);
5530 Set_Parent_Spec
(Pack_Decl
, Parent_Spec
(N
));
5533 Analyze
(Pack_Decl
);
5534 Check_Formal_Packages
(Pack_Id
);
5536 -- Body of the enclosing package is supplied when instantiating the
5537 -- subprogram body, after semantic analysis is completed.
5539 if Nkind
(Parent
(N
)) = N_Compilation_Unit
then
5541 -- Remove package itself from visibility, so it does not
5542 -- conflict with subprogram.
5544 Set_Name_Entity_Id
(Chars
(Pack_Id
), Homonym
(Pack_Id
));
5546 -- Set name and scope of internal subprogram so that the proper
5547 -- external name will be generated. The proper scope is the scope
5548 -- of the wrapper package. We need to generate debugging info for
5549 -- the internal subprogram, so set flag accordingly.
5551 Set_Chars
(Anon_Id
, Chars
(Defining_Entity
(N
)));
5552 Set_Scope
(Anon_Id
, Scope
(Pack_Id
));
5554 -- Mark wrapper package as referenced, to avoid spurious warnings
5555 -- if the instantiation appears in various with_ clauses of
5556 -- subunits of the main unit.
5558 Set_Referenced
(Pack_Id
);
5561 Set_Is_Generic_Instance
(Anon_Id
);
5562 Set_Debug_Info_Needed
(Anon_Id
);
5563 Act_Decl_Id
:= New_Copy
(Anon_Id
);
5565 Set_Parent
(Act_Decl_Id
, Parent
(Anon_Id
));
5566 Set_Chars
(Act_Decl_Id
, Chars
(Defining_Entity
(N
)));
5567 Set_Sloc
(Act_Decl_Id
, Sloc
(Defining_Entity
(N
)));
5569 -- Subprogram instance comes from source only if generic does
5571 Preserve_Comes_From_Source
(Act_Decl_Id
, Gen_Unit
);
5573 -- If the instance is a child unit, mark the Id accordingly. Mark
5574 -- the anonymous entity as well, which is the real subprogram and
5575 -- which is used when the instance appears in a context clause.
5576 -- Similarly, propagate the Is_Eliminated flag to handle properly
5577 -- nested eliminated subprograms.
5579 Set_Is_Child_Unit
(Act_Decl_Id
, Is_Child_Unit
(Defining_Entity
(N
)));
5580 Set_Is_Child_Unit
(Anon_Id
, Is_Child_Unit
(Defining_Entity
(N
)));
5581 New_Overloaded_Entity
(Act_Decl_Id
);
5582 Check_Eliminated
(Act_Decl_Id
);
5583 Set_Is_Eliminated
(Anon_Id
, Is_Eliminated
(Act_Decl_Id
));
5585 if Nkind
(Parent
(N
)) = N_Compilation_Unit
then
5587 -- In compilation unit case, kill elaboration checks on the
5588 -- instantiation, since they are never needed - the body is
5589 -- instantiated at the same point as the spec.
5591 if Legacy_Elaboration_Checks
then
5592 Set_Kill_Elaboration_Checks
(Act_Decl_Id
);
5593 Set_Suppress_Elaboration_Warnings
(Act_Decl_Id
);
5596 Set_Is_Compilation_Unit
(Anon_Id
);
5597 Set_Cunit_Entity
(Current_Sem_Unit
, Pack_Id
);
5600 -- The instance is not a freezing point for the new subprogram.
5601 -- The anonymous subprogram may have a freeze node, created for
5602 -- some delayed aspects. This freeze node must not be inherited
5603 -- by the visible subprogram entity.
5605 Set_Is_Frozen
(Act_Decl_Id
, False);
5606 Set_Freeze_Node
(Act_Decl_Id
, Empty
);
5608 if Nkind
(Defining_Entity
(N
)) = N_Defining_Operator_Symbol
then
5609 Valid_Operator_Definition
(Act_Decl_Id
);
5612 Set_Alias
(Act_Decl_Id
, Anon_Id
);
5613 Set_Has_Completion
(Act_Decl_Id
);
5614 Set_Related_Instance
(Pack_Id
, Act_Decl_Id
);
5616 if Nkind
(Parent
(N
)) = N_Compilation_Unit
then
5617 Set_Body_Required
(Parent
(N
), False);
5619 end Analyze_Instance_And_Renamings
;
5621 -------------------------------
5622 -- Build_Subprogram_Renaming --
5623 -------------------------------
5625 procedure Build_Subprogram_Renaming
is
5626 Renaming_Decl
: Node_Id
;
5627 Unit_Renaming
: Node_Id
;
5631 Make_Subprogram_Renaming_Declaration
(Loc
,
5634 (Specification
(Original_Node
(Gen_Decl
)),
5636 Instantiating
=> True),
5637 Name
=> New_Occurrence_Of
(Anon_Id
, Loc
));
5639 -- The generic may be a child unit. The renaming needs an identifier
5640 -- with the proper name.
5642 Set_Defining_Unit_Name
(Specification
(Unit_Renaming
),
5643 Make_Defining_Identifier
(Loc
, Chars
(Gen_Unit
)));
5645 -- If there is a formal subprogram with the same name as the unit
5646 -- itself, do not add this renaming declaration, to prevent
5647 -- ambiguities when there is a call with that name in the body.
5649 Renaming_Decl
:= First
(Renaming_List
);
5650 while Present
(Renaming_Decl
) loop
5651 if Nkind
(Renaming_Decl
) = N_Subprogram_Renaming_Declaration
5653 Chars
(Defining_Entity
(Renaming_Decl
)) = Chars
(Gen_Unit
)
5658 Next
(Renaming_Decl
);
5661 if No
(Renaming_Decl
) then
5662 Append
(Unit_Renaming
, Renaming_List
);
5664 end Build_Subprogram_Renaming
;
5668 Saved_GM
: constant Ghost_Mode_Type
:= Ghost_Mode
;
5669 Saved_IGR
: constant Node_Id
:= Ignored_Ghost_Region
;
5670 Saved_ISMP
: constant Boolean :=
5671 Ignore_SPARK_Mode_Pragmas_In_Instance
;
5672 Saved_SM
: constant SPARK_Mode_Type
:= SPARK_Mode
;
5673 Saved_SMP
: constant Node_Id
:= SPARK_Mode_Pragma
;
5674 -- Save the Ghost and SPARK mode-related data to restore on exit
5676 Vis_Prims_List
: Elist_Id
:= No_Elist
;
5677 -- List of primitives made temporarily visible in the instantiation
5678 -- to match the visibility of the formal type
5680 -- Start of processing for Analyze_Subprogram_Instantiation
5683 -- Preserve relevant elaboration-related attributes of the context which
5684 -- are no longer available or very expensive to recompute once analysis,
5685 -- resolution, and expansion are over.
5687 Mark_Elaboration_Attributes
5694 -- Very first thing: check for special Text_IO unit in case we are
5695 -- instantiating one of the children of [[Wide_]Wide_]Text_IO. Of course
5696 -- such an instantiation is bogus (these are packages, not subprograms),
5697 -- but we get a better error message if we do this.
5699 Check_Text_IO_Special_Unit
(Gen_Id
);
5701 -- Make node global for error reporting
5703 Instantiation_Node
:= N
;
5705 -- For package instantiations we turn off style checks, because they
5706 -- will have been emitted in the generic. For subprogram instantiations
5707 -- we want to apply at least the check on overriding indicators so we
5708 -- do not modify the style check status.
5710 -- The renaming declarations for the actuals do not come from source and
5711 -- will not generate spurious warnings.
5713 Preanalyze_Actuals
(N
);
5716 Env_Installed
:= True;
5717 Check_Generic_Child_Unit
(Gen_Id
, Parent_Installed
);
5718 Gen_Unit
:= Entity
(Gen_Id
);
5720 -- A subprogram instantiation is Ghost when it is subject to pragma
5721 -- Ghost or the generic template is Ghost. Set the mode now to ensure
5722 -- that any nodes generated during analysis and expansion are marked as
5725 Mark_And_Set_Ghost_Instantiation
(N
, Gen_Unit
);
5727 Generate_Reference
(Gen_Unit
, Gen_Id
);
5729 if Nkind
(Gen_Id
) = N_Identifier
5730 and then Chars
(Gen_Unit
) = Chars
(Defining_Entity
(N
))
5733 ("& is hidden within declaration of instance", Gen_Id
, Gen_Unit
);
5736 if Etype
(Gen_Unit
) = Any_Type
then
5741 -- Verify that it is a generic subprogram of the right kind, and that
5742 -- it does not lead to a circular instantiation.
5744 if K
= E_Procedure
and then Ekind
(Gen_Unit
) /= E_Generic_Procedure
then
5746 ("& is not the name of a generic procedure", Gen_Id
, Gen_Unit
);
5748 elsif K
= E_Function
and then Ekind
(Gen_Unit
) /= E_Generic_Function
then
5750 ("& is not the name of a generic function", Gen_Id
, Gen_Unit
);
5752 elsif In_Open_Scopes
(Gen_Unit
) then
5753 Error_Msg_NE
("instantiation of & within itself", N
, Gen_Unit
);
5756 Mutate_Ekind
(Inst_Id
, K
);
5757 Set_Scope
(Inst_Id
, Current_Scope
);
5759 Set_Entity
(Gen_Id
, Gen_Unit
);
5761 if In_Extended_Main_Source_Unit
(N
) then
5762 Set_Is_Instantiated
(Gen_Unit
);
5763 Generate_Reference
(Gen_Unit
, N
);
5766 -- If renaming, get original unit
5768 if Present
(Renamed_Entity
(Gen_Unit
))
5769 and then Is_Generic_Subprogram
(Renamed_Entity
(Gen_Unit
))
5771 Gen_Unit
:= Renamed_Entity
(Gen_Unit
);
5772 Set_Is_Instantiated
(Gen_Unit
);
5773 Generate_Reference
(Gen_Unit
, N
);
5776 if Contains_Instance_Of
(Gen_Unit
, Current_Scope
, Gen_Id
) then
5777 Error_Msg_Node_2
:= Current_Scope
;
5779 ("circular instantiation: & instantiated in &!", N
, Gen_Unit
);
5780 Circularity_Detected
:= True;
5781 Restore_Hidden_Primitives
(Vis_Prims_List
);
5785 Gen_Decl
:= Unit_Declaration_Node
(Gen_Unit
);
5787 -- Initialize renamings map, for error checking
5789 Generic_Renamings
.Set_Last
(0);
5790 Generic_Renamings_HTable
.Reset
;
5792 Create_Instantiation_Source
(N
, Gen_Unit
, S_Adjustment
);
5794 -- Copy original generic tree, to produce text for instantiation
5798 (Original_Node
(Gen_Decl
), Empty
, Instantiating
=> True);
5800 -- Inherit overriding indicator from instance node
5802 Act_Spec
:= Specification
(Act_Tree
);
5803 Set_Must_Override
(Act_Spec
, Must_Override
(N
));
5804 Set_Must_Not_Override
(Act_Spec
, Must_Not_Override
(N
));
5807 Analyze_Associations
5809 Formals
=> Generic_Formal_Declarations
(Act_Tree
),
5810 F_Copy
=> Generic_Formal_Declarations
(Gen_Decl
));
5812 Vis_Prims_List
:= Check_Hidden_Primitives
(Renaming_List
);
5814 -- The subprogram itself cannot contain a nested instance, so the
5815 -- current parent is left empty.
5817 Set_Instance_Env
(Gen_Unit
, Empty
);
5819 -- Build the subprogram declaration, which does not appear in the
5820 -- generic template, and give it a sloc consistent with that of the
5823 Set_Defining_Unit_Name
(Act_Spec
, Anon_Id
);
5824 Set_Generic_Parent
(Act_Spec
, Gen_Unit
);
5826 Make_Subprogram_Declaration
(Sloc
(Act_Spec
),
5827 Specification
=> Act_Spec
);
5829 -- The aspects have been copied previously, but they have to be
5830 -- linked explicitly to the new subprogram declaration. Explicit
5831 -- pre/postconditions on the instance are analyzed below, in a
5834 Move_Aspects
(Act_Tree
, To
=> Act_Decl
);
5835 Set_Categorization_From_Pragmas
(Act_Decl
);
5837 if Parent_Installed
then
5841 Append
(Act_Decl
, Renaming_List
);
5843 -- Contract-related source pragmas that follow a generic subprogram
5844 -- must be instantiated explicitly because they are not part of the
5845 -- subprogram template.
5847 Instantiate_Subprogram_Contract
5848 (Original_Node
(Gen_Decl
), Renaming_List
);
5850 Build_Subprogram_Renaming
;
5852 -- If the context of the instance is subject to SPARK_Mode "off" or
5853 -- the annotation is altogether missing, set the global flag which
5854 -- signals Analyze_Pragma to ignore all SPARK_Mode pragmas within
5855 -- the instance. This should be done prior to analyzing the instance.
5857 if SPARK_Mode
/= On
then
5858 Ignore_SPARK_Mode_Pragmas_In_Instance
:= True;
5861 -- If the context of an instance is not subject to SPARK_Mode "off",
5862 -- and the generic spec is subject to an explicit SPARK_Mode pragma,
5863 -- the latter should be the one applicable to the instance.
5865 if not Ignore_SPARK_Mode_Pragmas_In_Instance
5866 and then Saved_SM
/= Off
5867 and then Present
(SPARK_Pragma
(Gen_Unit
))
5869 Set_SPARK_Mode
(Gen_Unit
);
5872 -- Need to mark Anon_Id intrinsic before calling
5873 -- Analyze_Instance_And_Renamings because this flag may be propagated
5876 if Is_Intrinsic_Subprogram
(Gen_Unit
) then
5877 Set_Is_Intrinsic_Subprogram
(Anon_Id
);
5878 Set_Interface_Name
(Anon_Id
, Interface_Name
(Gen_Unit
));
5881 Analyze_Instance_And_Renamings
;
5883 -- Restore SPARK_Mode from the context after analysis of the package
5884 -- declaration, so that the SPARK_Mode on the generic spec does not
5885 -- apply to the pending instance for the instance body.
5887 if not Ignore_SPARK_Mode_Pragmas_In_Instance
5888 and then Saved_SM
/= Off
5889 and then Present
(SPARK_Pragma
(Gen_Unit
))
5891 Restore_SPARK_Mode
(Saved_SM
, Saved_SMP
);
5894 -- If the generic is marked Import (Intrinsic), then so is the
5895 -- instance; this indicates that there is no body to instantiate.
5896 -- We also copy the interface name in case this is handled by the
5897 -- back-end and deal with an instance of unchecked conversion.
5899 if Is_Intrinsic_Subprogram
(Gen_Unit
) then
5900 Set_Is_Intrinsic_Subprogram
(Act_Decl_Id
);
5901 Set_Interface_Name
(Act_Decl_Id
, Interface_Name
(Gen_Unit
));
5903 if Chars
(Gen_Unit
) = Name_Unchecked_Conversion
then
5904 Validate_Unchecked_Conversion
(N
, Act_Decl_Id
);
5908 -- Inherit convention from generic unit. Intrinsic convention, as for
5909 -- an instance of unchecked conversion, is not inherited because an
5910 -- explicit Ada instance has been created.
5912 if Has_Convention_Pragma
(Gen_Unit
)
5913 and then Convention
(Gen_Unit
) /= Convention_Intrinsic
5915 Set_Convention
(Act_Decl_Id
, Convention
(Gen_Unit
));
5916 Set_Is_Exported
(Act_Decl_Id
, Is_Exported
(Gen_Unit
));
5919 Generate_Definition
(Act_Decl_Id
);
5921 -- Inherit all inlining-related flags which apply to the generic in
5922 -- the subprogram and its declaration.
5924 Set_Is_Inlined
(Act_Decl_Id
, Is_Inlined
(Gen_Unit
));
5925 Set_Is_Inlined
(Anon_Id
, Is_Inlined
(Gen_Unit
));
5927 Set_Has_Pragma_Inline
(Act_Decl_Id
, Has_Pragma_Inline
(Gen_Unit
));
5928 Set_Has_Pragma_Inline
(Anon_Id
, Has_Pragma_Inline
(Gen_Unit
));
5930 Set_Has_Pragma_Inline_Always
5931 (Act_Decl_Id
, Has_Pragma_Inline_Always
(Gen_Unit
));
5932 Set_Has_Pragma_Inline_Always
5933 (Anon_Id
, Has_Pragma_Inline_Always
(Gen_Unit
));
5935 Set_Has_Pragma_No_Inline
5936 (Act_Decl_Id
, Has_Pragma_No_Inline
(Gen_Unit
));
5937 Set_Has_Pragma_No_Inline
5938 (Anon_Id
, Has_Pragma_No_Inline
(Gen_Unit
));
5940 -- Propagate No_Return if pragma applied to generic unit. This must
5941 -- be done explicitly because pragma does not appear in generic
5942 -- declaration (unlike the aspect case).
5944 if No_Return
(Gen_Unit
) then
5945 Set_No_Return
(Act_Decl_Id
);
5946 Set_No_Return
(Anon_Id
);
5949 -- Mark both the instance spec and the anonymous package in case the
5950 -- body is instantiated at a later pass. This preserves the original
5951 -- context in effect for the body.
5953 if SPARK_Mode
/= On
then
5954 Set_Ignore_SPARK_Mode_Pragmas
(Act_Decl_Id
);
5955 Set_Ignore_SPARK_Mode_Pragmas
(Anon_Id
);
5958 if Legacy_Elaboration_Checks
5959 and then not Is_Intrinsic_Subprogram
(Gen_Unit
)
5961 Check_Elab_Instantiation
(N
);
5964 -- Save the scenario for later examination by the ABE Processing
5967 Record_Elaboration_Scenario
(N
);
5969 -- The instantiation results in a guaranteed ABE. Create a completing
5970 -- body for the subprogram declaration because the real body will not
5973 if Is_Known_Guaranteed_ABE
(N
) then
5974 Provide_Completing_Bodies
(Instance_Spec
(N
));
5977 if Is_Dispatching_Operation
(Act_Decl_Id
)
5978 and then Ada_Version
>= Ada_2005
5984 Formal
:= First_Formal
(Act_Decl_Id
);
5985 while Present
(Formal
) loop
5986 if Ekind
(Etype
(Formal
)) = E_Anonymous_Access_Type
5987 and then Is_Controlling_Formal
(Formal
)
5988 and then not Can_Never_Be_Null
(Formal
)
5991 ("access parameter& is controlling,", N
, Formal
);
5993 ("\corresponding parameter of & must be explicitly "
5994 & "null-excluding", N
, Gen_Id
);
5997 Next_Formal
(Formal
);
6002 Check_Hidden_Child_Unit
(N
, Gen_Unit
, Act_Decl_Id
);
6004 Validate_Categorization_Dependency
(N
, Act_Decl_Id
);
6006 if not Is_Intrinsic_Subprogram
(Act_Decl_Id
) then
6007 Inherit_Context
(Gen_Decl
, N
);
6009 Restore_Private_Views
(Pack_Id
, False);
6011 -- If the context requires a full instantiation, mark node for
6012 -- subsequent construction of the body.
6014 if Need_Subprogram_Instance_Body
(N
, Act_Decl_Id
) then
6015 Check_Forward_Instantiation
(Gen_Decl
);
6017 -- The wrapper package is always delayed, because it does not
6018 -- constitute a freeze point, but to insure that the freeze node
6019 -- is placed properly, it is created directly when instantiating
6020 -- the body (otherwise the freeze node might appear to early for
6021 -- nested instantiations).
6023 elsif Nkind
(Parent
(N
)) = N_Compilation_Unit
then
6024 Rewrite
(N
, Unit
(Parent
(N
)));
6025 Set_Unit
(Parent
(N
), N
);
6028 -- Replace instance node for library-level instantiations of
6029 -- intrinsic subprograms.
6031 elsif Nkind
(Parent
(N
)) = N_Compilation_Unit
then
6032 Rewrite
(N
, Unit
(Parent
(N
)));
6033 Set_Unit
(Parent
(N
), N
);
6036 if Parent_Installed
then
6040 Restore_Hidden_Primitives
(Vis_Prims_List
);
6042 Env_Installed
:= False;
6043 Generic_Renamings
.Set_Last
(0);
6044 Generic_Renamings_HTable
.Reset
;
6048 -- Analyze aspects in declaration if no errors appear in the instance.
6050 if Has_Aspects
(N
) and then Serious_Errors_Detected
= Errs
then
6051 Analyze_Aspect_Specifications
(N
, Act_Decl_Id
);
6054 Ignore_SPARK_Mode_Pragmas_In_Instance
:= Saved_ISMP
;
6055 Restore_Ghost_Region
(Saved_GM
, Saved_IGR
);
6056 Restore_SPARK_Mode
(Saved_SM
, Saved_SMP
);
6059 when Instantiation_Error
=>
6060 if Parent_Installed
then
6064 if Env_Installed
then
6068 Ignore_SPARK_Mode_Pragmas_In_Instance
:= Saved_ISMP
;
6069 Restore_Ghost_Region
(Saved_GM
, Saved_IGR
);
6070 Restore_SPARK_Mode
(Saved_SM
, Saved_SMP
);
6071 end Analyze_Subprogram_Instantiation
;
6073 -------------------------
6074 -- Get_Associated_Node --
6075 -------------------------
6077 function Get_Associated_Node
(N
: Node_Id
) return Node_Id
is
6081 Assoc
:= Associated_Node
(N
);
6083 if Nkind
(Assoc
) /= Nkind
(N
) then
6086 elsif Nkind
(Assoc
) in N_Aggregate | N_Extension_Aggregate
then
6090 -- If the node is part of an inner generic, it may itself have been
6091 -- remapped into a further generic copy. Associated_Node is otherwise
6092 -- used for the entity of the node, and will be of a different node
6093 -- kind, or else N has been rewritten as a literal or function call.
6095 while Present
(Associated_Node
(Assoc
))
6096 and then Nkind
(Associated_Node
(Assoc
)) = Nkind
(Assoc
)
6098 Assoc
:= Associated_Node
(Assoc
);
6101 -- Follow an additional link in case the final node was rewritten.
6102 -- This can only happen with nested generic units.
6104 if (Nkind
(Assoc
) = N_Identifier
or else Nkind
(Assoc
) in N_Op
)
6105 and then Present
(Associated_Node
(Assoc
))
6106 and then Nkind
(Associated_Node
(Assoc
)) in N_Function_Call
6107 | N_Explicit_Dereference
6112 Assoc
:= Associated_Node
(Assoc
);
6115 -- An additional special case: an unconstrained type in an object
6116 -- declaration may have been rewritten as a local subtype constrained
6117 -- by the expression in the declaration. We need to recover the
6118 -- original entity, which may be global.
6120 if Present
(Original_Node
(Assoc
))
6121 and then Nkind
(Parent
(N
)) = N_Object_Declaration
6123 Assoc
:= Original_Node
(Assoc
);
6128 end Get_Associated_Node
;
6130 -----------------------------------
6131 -- Build_Subprogram_Decl_Wrapper --
6132 -----------------------------------
6134 function Build_Subprogram_Decl_Wrapper
6135 (Formal_Subp
: Entity_Id
) return Node_Id
6137 Loc
: constant Source_Ptr
:= Sloc
(Current_Scope
);
6138 Ret_Type
: constant Entity_Id
:= Get_Instance_Of
(Etype
(Formal_Subp
));
6141 Parm_Spec
: Node_Id
;
6142 Profile
: List_Id
:= New_List
;
6149 Subp
:= Make_Defining_Identifier
(Loc
, Chars
(Formal_Subp
));
6150 Mutate_Ekind
(Subp
, Ekind
(Formal_Subp
));
6151 Set_Is_Generic_Actual_Subprogram
(Subp
);
6153 Profile
:= Parameter_Specifications
(
6155 (Specification
(Unit_Declaration_Node
(Formal_Subp
))));
6157 Form_F
:= First_Formal
(Formal_Subp
);
6158 Parm_Spec
:= First
(Profile
);
6160 -- Create new entities for the formals. Reset entities so that
6161 -- parameter types are properly resolved when wrapper declaration
6164 while Present
(Parm_Spec
) loop
6165 New_F
:= Make_Defining_Identifier
(Loc
, Chars
(Form_F
));
6166 Set_Defining_Identifier
(Parm_Spec
, New_F
);
6167 Set_Entity
(Parameter_Type
(Parm_Spec
), Empty
);
6169 Next_Formal
(Form_F
);
6172 if Ret_Type
= Standard_Void_Type
then
6174 Make_Procedure_Specification
(Loc
,
6175 Defining_Unit_Name
=> Subp
,
6176 Parameter_Specifications
=> Profile
);
6179 Make_Function_Specification
(Loc
,
6180 Defining_Unit_Name
=> Subp
,
6181 Parameter_Specifications
=> Profile
,
6182 Result_Definition
=> New_Occurrence_Of
(Ret_Type
, Loc
));
6186 Make_Subprogram_Declaration
(Loc
, Specification
=> Spec
);
6189 end Build_Subprogram_Decl_Wrapper
;
6191 -----------------------------------
6192 -- Build_Subprogram_Body_Wrapper --
6193 -----------------------------------
6195 function Build_Subprogram_Body_Wrapper
6196 (Formal_Subp
: Entity_Id
;
6197 Actual_Name
: Node_Id
) return Node_Id
6199 Loc
: constant Source_Ptr
:= Sloc
(Current_Scope
);
6200 Ret_Type
: constant Entity_Id
:= Get_Instance_Of
(Etype
(Formal_Subp
));
6201 Spec_Node
: constant Node_Id
:=
6203 (Build_Subprogram_Decl_Wrapper
(Formal_Subp
));
6206 Body_Node
: Node_Id
;
6209 Actuals
:= New_List
;
6210 Act
:= First
(Parameter_Specifications
(Spec_Node
));
6212 while Present
(Act
) loop
6214 Make_Identifier
(Loc
, Chars
(Defining_Identifier
(Act
))));
6218 if Ret_Type
= Standard_Void_Type
then
6219 Stmt
:= Make_Procedure_Call_Statement
(Loc
,
6220 Name
=> Actual_Name
,
6221 Parameter_Associations
=> Actuals
);
6224 Stmt
:= Make_Simple_Return_Statement
(Loc
,
6226 Make_Function_Call
(Loc
,
6227 Name
=> Actual_Name
,
6228 Parameter_Associations
=> Actuals
));
6231 Body_Node
:= Make_Subprogram_Body
(Loc
,
6232 Specification
=> Spec_Node
,
6233 Declarations
=> New_List
,
6234 Handled_Statement_Sequence
=>
6235 Make_Handled_Sequence_Of_Statements
(Loc
,
6236 Statements
=> New_List
(Stmt
)));
6239 end Build_Subprogram_Body_Wrapper
;
6241 -------------------------------------------
6242 -- Build_Instance_Compilation_Unit_Nodes --
6243 -------------------------------------------
6245 procedure Build_Instance_Compilation_Unit_Nodes
6250 Decl_Cunit
: Node_Id
;
6251 Body_Cunit
: Node_Id
;
6253 New_Main
: constant Entity_Id
:= Defining_Entity
(Act_Decl
);
6254 Old_Main
: constant Entity_Id
:= Cunit_Entity
(Main_Unit
);
6257 -- A new compilation unit node is built for the instance declaration
6260 Make_Compilation_Unit
(Sloc
(N
),
6261 Context_Items
=> Empty_List
,
6263 Aux_Decls_Node
=> Make_Compilation_Unit_Aux
(Sloc
(N
)));
6265 Set_Parent_Spec
(Act_Decl
, Parent_Spec
(N
));
6267 -- The new compilation unit is linked to its body, but both share the
6268 -- same file, so we do not set Body_Required on the new unit so as not
6269 -- to create a spurious dependency on a non-existent body in the ali.
6270 -- This simplifies CodePeer unit traversal.
6272 -- We use the original instantiation compilation unit as the resulting
6273 -- compilation unit of the instance, since this is the main unit.
6275 Rewrite
(N
, Act_Body
);
6277 -- Propagate the aspect specifications from the package body template to
6278 -- the instantiated version of the package body.
6280 if Has_Aspects
(Act_Body
) then
6281 Set_Aspect_Specifications
6282 (N
, New_Copy_List_Tree
(Aspect_Specifications
(Act_Body
)));
6285 Body_Cunit
:= Parent
(N
);
6287 -- The two compilation unit nodes are linked by the Library_Unit field
6289 Set_Library_Unit
(Decl_Cunit
, Body_Cunit
);
6290 Set_Library_Unit
(Body_Cunit
, Decl_Cunit
);
6292 -- Preserve the private nature of the package if needed
6294 Set_Private_Present
(Decl_Cunit
, Private_Present
(Body_Cunit
));
6296 -- If the instance is not the main unit, its context, categorization
6297 -- and elaboration entity are not relevant to the compilation.
6299 if Body_Cunit
/= Cunit
(Main_Unit
) then
6300 Make_Instance_Unit
(Body_Cunit
, In_Main
=> False);
6304 -- The context clause items on the instantiation, which are now attached
6305 -- to the body compilation unit (since the body overwrote the original
6306 -- instantiation node), semantically belong on the spec, so copy them
6307 -- there. It's harmless to leave them on the body as well. In fact one
6308 -- could argue that they belong in both places.
6310 Citem
:= First
(Context_Items
(Body_Cunit
));
6311 while Present
(Citem
) loop
6312 Append
(New_Copy
(Citem
), Context_Items
(Decl_Cunit
));
6316 -- Propagate categorization flags on packages, so that they appear in
6317 -- the ali file for the spec of the unit.
6319 if Ekind
(New_Main
) = E_Package
then
6320 Set_Is_Pure
(Old_Main
, Is_Pure
(New_Main
));
6321 Set_Is_Preelaborated
(Old_Main
, Is_Preelaborated
(New_Main
));
6322 Set_Is_Remote_Types
(Old_Main
, Is_Remote_Types
(New_Main
));
6323 Set_Is_Shared_Passive
(Old_Main
, Is_Shared_Passive
(New_Main
));
6324 Set_Is_Remote_Call_Interface
6325 (Old_Main
, Is_Remote_Call_Interface
(New_Main
));
6328 -- Make entry in Units table, so that binder can generate call to
6329 -- elaboration procedure for body, if any.
6331 Make_Instance_Unit
(Body_Cunit
, In_Main
=> True);
6332 Main_Unit_Entity
:= New_Main
;
6333 Set_Cunit_Entity
(Main_Unit
, Main_Unit_Entity
);
6335 -- Build elaboration entity, since the instance may certainly generate
6336 -- elaboration code requiring a flag for protection.
6338 Build_Elaboration_Entity
(Decl_Cunit
, New_Main
);
6339 end Build_Instance_Compilation_Unit_Nodes
;
6341 --------------------------------
6342 -- Check_Abbreviated_Instance --
6343 --------------------------------
6345 procedure Check_Abbreviated_Instance
6347 Parent_Installed
: in out Boolean)
6349 Inst_Node
: Node_Id
;
6352 if Nkind
(N
) = N_Package_Specification
6353 and then Is_Abbreviated_Instance
(Defining_Entity
(N
))
6355 Inst_Node
:= Get_Unit_Instantiation_Node
(Defining_Entity
(N
));
6356 Check_Generic_Child_Unit
(Name
(Inst_Node
), Parent_Installed
);
6358 end Check_Abbreviated_Instance
;
6360 -----------------------------
6361 -- Check_Access_Definition --
6362 -----------------------------
6364 procedure Check_Access_Definition
(N
: Node_Id
) is
6367 (Ada_Version
>= Ada_2005
and then Present
(Access_Definition
(N
)));
6369 end Check_Access_Definition
;
6371 -----------------------------------
6372 -- Check_Formal_Package_Instance --
6373 -----------------------------------
6375 -- If the formal has specific parameters, they must match those of the
6376 -- actual. Both of them are instances, and the renaming declarations for
6377 -- their formal parameters appear in the same order in both. The analyzed
6378 -- formal has been analyzed in the context of the current instance.
6380 procedure Check_Formal_Package_Instance
6381 (Formal_Pack
: Entity_Id
;
6382 Actual_Pack
: Entity_Id
)
6384 E1
: Entity_Id
:= First_Entity
(Actual_Pack
);
6385 E2
: Entity_Id
:= First_Entity
(Formal_Pack
);
6386 Prev_E1
: Entity_Id
;
6391 procedure Check_Mismatch
(B
: Boolean);
6392 -- Common error routine for mismatch between the parameters of the
6393 -- actual instance and those of the formal package.
6395 function Is_Defaulted
(Param
: Entity_Id
) return Boolean;
6396 -- If the formal package has partly box-initialized formals, skip
6397 -- conformance check for these formals. Previously the code assumed
6398 -- that box initialization for a formal package applied to all its
6399 -- formal parameters.
6401 function Same_Instantiated_Constant
(E1
, E2
: Entity_Id
) return Boolean;
6402 -- The formal may come from a nested formal package, and the actual may
6403 -- have been constant-folded. To determine whether the two denote the
6404 -- same entity we may have to traverse several definitions to recover
6405 -- the ultimate entity that they refer to.
6407 function Same_Instantiated_Function
(E1
, E2
: Entity_Id
) return Boolean;
6408 -- The formal and the actual must be identical, but if both are
6409 -- given by attributes they end up renaming different generated bodies,
6410 -- and we must verify that the attributes themselves match.
6412 function Same_Instantiated_Variable
(E1
, E2
: Entity_Id
) return Boolean;
6413 -- Similarly, if the formal comes from a nested formal package, the
6414 -- actual may designate the formal through multiple renamings, which
6415 -- have to be followed to determine the original variable in question.
6417 --------------------
6418 -- Check_Mismatch --
6419 --------------------
6421 procedure Check_Mismatch
(B
: Boolean) is
6422 -- A Formal_Type_Declaration for a derived private type is rewritten
6423 -- as a private extension decl. (see Analyze_Formal_Derived_Type),
6424 -- which is why we examine the original node.
6426 Kind
: constant Node_Kind
:= Nkind
(Original_Node
(Parent
(E2
)));
6429 if Kind
= N_Formal_Type_Declaration
then
6432 elsif Kind
in N_Formal_Object_Declaration
6433 | N_Formal_Package_Declaration
6434 | N_Formal_Subprogram_Declaration
6438 -- Ada 2012: If both formal and actual are incomplete types they
6441 elsif Is_Incomplete_Type
(E1
) and then Is_Incomplete_Type
(E2
) then
6446 ("actual for & in actual instance does not match formal",
6447 Parent
(Actual_Pack
), E1
);
6455 function Is_Defaulted
(Param
: Entity_Id
) return Boolean is
6460 First
(Generic_Associations
(Parent
6461 (Associated_Formal_Package
(Actual_Pack
))));
6463 while Present
(Assoc
) loop
6464 if Nkind
(Assoc
) = N_Others_Choice
then
6467 elsif Nkind
(Assoc
) = N_Generic_Association
6468 and then Chars
(Selector_Name
(Assoc
)) = Chars
(Param
)
6470 return Box_Present
(Assoc
);
6479 --------------------------------
6480 -- Same_Instantiated_Constant --
6481 --------------------------------
6483 function Same_Instantiated_Constant
6484 (E1
, E2
: Entity_Id
) return Boolean
6490 while Present
(Ent
) loop
6494 elsif Ekind
(Ent
) /= E_Constant
then
6497 elsif Is_Entity_Name
(Constant_Value
(Ent
)) then
6498 if Entity
(Constant_Value
(Ent
)) = E1
then
6501 Ent
:= Entity
(Constant_Value
(Ent
));
6504 -- The actual may be a constant that has been folded. Recover
6507 elsif Is_Entity_Name
(Original_Node
(Constant_Value
(Ent
))) then
6508 Ent
:= Entity
(Original_Node
(Constant_Value
(Ent
)));
6516 end Same_Instantiated_Constant
;
6518 --------------------------------
6519 -- Same_Instantiated_Function --
6520 --------------------------------
6522 function Same_Instantiated_Function
6523 (E1
, E2
: Entity_Id
) return Boolean
6527 if Alias
(E1
) = Alias
(E2
) then
6530 elsif Present
(Alias
(E2
)) then
6531 U1
:= Original_Node
(Unit_Declaration_Node
(E1
));
6532 U2
:= Original_Node
(Unit_Declaration_Node
(Alias
(E2
)));
6534 return Nkind
(U1
) = N_Subprogram_Renaming_Declaration
6535 and then Nkind
(Name
(U1
)) = N_Attribute_Reference
6537 and then Nkind
(U2
) = N_Subprogram_Renaming_Declaration
6538 and then Nkind
(Name
(U2
)) = N_Attribute_Reference
6541 Attribute_Name
(Name
(U1
)) = Attribute_Name
(Name
(U2
));
6545 end Same_Instantiated_Function
;
6547 --------------------------------
6548 -- Same_Instantiated_Variable --
6549 --------------------------------
6551 function Same_Instantiated_Variable
6552 (E1
, E2
: Entity_Id
) return Boolean
6554 function Original_Entity
(E
: Entity_Id
) return Entity_Id
;
6555 -- Follow chain of renamings to the ultimate ancestor
6557 ---------------------
6558 -- Original_Entity --
6559 ---------------------
6561 function Original_Entity
(E
: Entity_Id
) return Entity_Id
is
6566 while Nkind
(Parent
(Orig
)) = N_Object_Renaming_Declaration
6567 and then Present
(Renamed_Object
(Orig
))
6568 and then Is_Entity_Name
(Renamed_Object
(Orig
))
6570 Orig
:= Entity
(Renamed_Object
(Orig
));
6574 end Original_Entity
;
6576 -- Start of processing for Same_Instantiated_Variable
6579 return Ekind
(E1
) = Ekind
(E2
)
6580 and then Original_Entity
(E1
) = Original_Entity
(E2
);
6581 end Same_Instantiated_Variable
;
6583 -- Start of processing for Check_Formal_Package_Instance
6587 while Present
(E1
) and then Present
(E2
) loop
6588 exit when Ekind
(E1
) = E_Package
6589 and then Renamed_Entity
(E1
) = Renamed_Entity
(Actual_Pack
);
6591 -- If the formal is the renaming of the formal package, this
6592 -- is the end of its formal part, which may occur before the
6593 -- end of the formal part in the actual in the presence of
6594 -- defaulted parameters in the formal package.
6596 exit when Nkind
(Parent
(E2
)) = N_Package_Renaming_Declaration
6597 and then Renamed_Entity
(E2
) = Scope
(E2
);
6599 -- The analysis of the actual may generate additional internal
6600 -- entities. If the formal is defaulted, there is no corresponding
6601 -- analysis and the internal entities must be skipped, until we
6602 -- find corresponding entities again.
6604 if Comes_From_Source
(E2
)
6605 and then not Comes_From_Source
(E1
)
6606 and then Chars
(E1
) /= Chars
(E2
)
6608 while Present
(E1
) and then Chars
(E1
) /= Chars
(E2
) loop
6616 -- Entities may be declared without full declaration, such as
6617 -- itypes and predefined operators (concatenation for arrays, eg).
6618 -- Skip it and keep the formal entity to find a later match for it.
6620 elsif No
(Parent
(E2
)) and then Ekind
(E1
) /= Ekind
(E2
) then
6624 -- If the formal entity comes from a formal declaration, it was
6625 -- defaulted in the formal package, and no check is needed on it.
6627 elsif Nkind
(Original_Node
(Parent
(E2
))) in
6628 N_Formal_Object_Declaration | N_Formal_Type_Declaration
6630 -- If the formal is a tagged type the corresponding class-wide
6631 -- type has been generated as well, and it must be skipped.
6633 if Is_Type
(E2
) and then Is_Tagged_Type
(E2
) then
6639 -- Ditto for defaulted formal subprograms.
6641 elsif Is_Overloadable
(E1
)
6642 and then Nkind
(Unit_Declaration_Node
(E2
)) in
6643 N_Formal_Subprogram_Declaration
6647 elsif Is_Defaulted
(E1
) then
6650 elsif Is_Type
(E1
) then
6652 -- Subtypes must statically match. E1, E2 are the local entities
6653 -- that are subtypes of the actuals. Itypes generated for other
6654 -- parameters need not be checked, the check will be performed
6655 -- on the parameters themselves.
6657 -- If E2 is a formal type declaration, it is a defaulted parameter
6658 -- and needs no checking.
6660 if not Is_Itype
(E1
) and then not Is_Itype
(E2
) then
6663 or else Etype
(E1
) /= Etype
(E2
)
6664 or else not Subtypes_Statically_Match
(E1
, E2
));
6667 elsif Ekind
(E1
) = E_Constant
then
6669 -- IN parameters must denote the same static value, or the same
6670 -- constant, or the literal null.
6672 Expr1
:= Expression
(Parent
(E1
));
6674 if Ekind
(E2
) /= E_Constant
then
6675 Check_Mismatch
(True);
6678 Expr2
:= Expression
(Parent
(E2
));
6681 if Is_OK_Static_Expression
(Expr1
) then
6682 if not Is_OK_Static_Expression
(Expr2
) then
6683 Check_Mismatch
(True);
6685 elsif Is_Discrete_Type
(Etype
(E1
)) then
6687 V1
: constant Uint
:= Expr_Value
(Expr1
);
6688 V2
: constant Uint
:= Expr_Value
(Expr2
);
6690 Check_Mismatch
(V1
/= V2
);
6693 elsif Is_Real_Type
(Etype
(E1
)) then
6695 V1
: constant Ureal
:= Expr_Value_R
(Expr1
);
6696 V2
: constant Ureal
:= Expr_Value_R
(Expr2
);
6698 Check_Mismatch
(V1
/= V2
);
6701 elsif Is_String_Type
(Etype
(E1
))
6702 and then Nkind
(Expr1
) = N_String_Literal
6704 if Nkind
(Expr2
) /= N_String_Literal
then
6705 Check_Mismatch
(True);
6708 (not String_Equal
(Strval
(Expr1
), Strval
(Expr2
)));
6712 elsif Is_Entity_Name
(Expr1
) then
6713 if Is_Entity_Name
(Expr2
) then
6714 if Entity
(Expr1
) = Entity
(Expr2
) then
6718 (not Same_Instantiated_Constant
6719 (Entity
(Expr1
), Entity
(Expr2
)));
6723 Check_Mismatch
(True);
6726 elsif Is_Entity_Name
(Original_Node
(Expr1
))
6727 and then Is_Entity_Name
(Expr2
)
6728 and then Same_Instantiated_Constant
6729 (Entity
(Original_Node
(Expr1
)), Entity
(Expr2
))
6733 elsif Nkind
(Expr1
) = N_Null
then
6734 Check_Mismatch
(Nkind
(Expr1
) /= N_Null
);
6737 Check_Mismatch
(True);
6740 elsif Ekind
(E1
) = E_Variable
then
6741 Check_Mismatch
(not Same_Instantiated_Variable
(E1
, E2
));
6743 elsif Ekind
(E1
) = E_Package
then
6745 (Ekind
(E1
) /= Ekind
(E2
)
6746 or else (Present
(Renamed_Entity
(E2
))
6747 and then Renamed_Entity
(E1
) /=
6748 Renamed_Entity
(E2
)));
6750 elsif Is_Overloadable
(E1
) then
6751 -- Verify that the actual subprograms match. Note that actuals
6752 -- that are attributes are rewritten as subprograms. If the
6753 -- subprogram in the formal package is defaulted, no check is
6754 -- needed. Note that this can only happen in Ada 2005 when the
6755 -- formal package can be partially parameterized.
6757 if Nkind
(Unit_Declaration_Node
(E1
)) =
6758 N_Subprogram_Renaming_Declaration
6759 and then From_Default
(Unit_Declaration_Node
(E1
))
6763 -- If the formal package has an "others" box association that
6764 -- covers this formal, there is no need for a check either.
6766 elsif Nkind
(Unit_Declaration_Node
(E2
)) in
6767 N_Formal_Subprogram_Declaration
6768 and then Box_Present
(Unit_Declaration_Node
(E2
))
6772 -- No check needed if subprogram is a defaulted null procedure
6774 elsif No
(Alias
(E2
))
6775 and then Ekind
(E2
) = E_Procedure
6777 Null_Present
(Specification
(Unit_Declaration_Node
(E2
)))
6781 -- Otherwise the actual in the formal and the actual in the
6782 -- instantiation of the formal must match, up to renamings.
6786 (Ekind
(E2
) /= Ekind
(E1
)
6787 or else not Same_Instantiated_Function
(E1
, E2
));
6791 raise Program_Error
;
6799 end Check_Formal_Package_Instance
;
6801 ---------------------------
6802 -- Check_Formal_Packages --
6803 ---------------------------
6805 procedure Check_Formal_Packages
(P_Id
: Entity_Id
) is
6807 Formal_P
: Entity_Id
;
6808 Formal_Decl
: Node_Id
;
6811 -- Iterate through the declarations in the instance, looking for package
6812 -- renaming declarations that denote instances of formal packages, until
6813 -- we find the renaming of the current package itself. The declaration
6814 -- of a formal package that requires conformance checking is followed by
6815 -- an internal entity that is the abbreviated instance.
6817 E
:= First_Entity
(P_Id
);
6818 while Present
(E
) loop
6819 if Ekind
(E
) = E_Package
then
6820 exit when Renamed_Entity
(E
) = P_Id
;
6822 if Nkind
(Parent
(E
)) = N_Package_Renaming_Declaration
then
6823 Formal_Decl
:= Parent
(Associated_Formal_Package
(E
));
6825 if Requires_Conformance_Checking
(Formal_Decl
) then
6826 Formal_P
:= Next_Entity
(E
);
6828 -- If the instance is within an enclosing instance body
6829 -- there is no need to verify the legality of current formal
6830 -- packages because they were legal in the generic body.
6831 -- This optimization may be applicable elsewhere, and it
6832 -- also removes spurious errors that may arise with
6833 -- on-the-fly inlining and confusion between private and
6836 if not In_Instance_Body
then
6837 Check_Formal_Package_Instance
(Formal_P
, E
);
6840 -- Restore the visibility of formals of the formal instance
6841 -- that are not defaulted, and are hidden within the current
6842 -- generic. These formals may be visible within an enclosing
6848 Elmt
:= First_Elmt
(Hidden_In_Formal_Instance
(Formal_P
));
6849 while Present
(Elmt
) loop
6850 Set_Is_Hidden
(Node
(Elmt
), False);
6855 -- After checking, remove the internal validating package.
6856 -- It is only needed for semantic checks, and as it may
6857 -- contain generic formal declarations it should not reach
6860 Remove
(Unit_Declaration_Node
(Formal_P
));
6867 end Check_Formal_Packages
;
6869 ---------------------------------
6870 -- Check_Forward_Instantiation --
6871 ---------------------------------
6873 procedure Check_Forward_Instantiation
(Decl
: Node_Id
) is
6875 Gen_Comp
: Entity_Id
:= Cunit_Entity
(Get_Source_Unit
(Decl
));
6878 -- The instantiation appears before the generic body if we are in the
6879 -- scope of the unit containing the generic, either in its spec or in
6880 -- the package body, and before the generic body.
6882 if Ekind
(Gen_Comp
) = E_Package_Body
then
6883 Gen_Comp
:= Spec_Entity
(Gen_Comp
);
6886 if In_Open_Scopes
(Gen_Comp
)
6887 and then No
(Corresponding_Body
(Decl
))
6892 and then not Is_Compilation_Unit
(S
)
6893 and then not Is_Child_Unit
(S
)
6895 if Ekind
(S
) = E_Package
then
6896 Set_Has_Forward_Instantiation
(S
);
6902 end Check_Forward_Instantiation
;
6904 ---------------------------
6905 -- Check_Generic_Actuals --
6906 ---------------------------
6908 -- The visibility of the actuals may be different between the point of
6909 -- generic instantiation and the instantiation of the body.
6911 procedure Check_Generic_Actuals
6912 (Instance
: Entity_Id
;
6913 Is_Formal_Box
: Boolean)
6919 E
:= First_Entity
(Instance
);
6920 while Present
(E
) loop
6922 and then Nkind
(Parent
(E
)) = N_Subtype_Declaration
6923 and then Scope
(Etype
(E
)) /= Instance
6924 and then Is_Entity_Name
(Subtype_Indication
(Parent
(E
)))
6926 -- Restore the proper view of the actual from the information
6927 -- saved earlier by Instantiate_Type.
6929 Check_Private_View
(Subtype_Indication
(Parent
(E
)));
6931 -- If the actual is itself the formal of a parent instance,
6932 -- then also restore the proper view of its actual and so on.
6933 -- That's necessary for nested instantiations of the form
6936 -- type Component is private;
6937 -- type Array_Type is array (Positive range <>) of Component;
6940 -- when the outermost actuals have inconsistent views, because
6941 -- the Component_Type of Array_Type of the inner instantiations
6942 -- is the actual of Component of the outermost one and not that
6943 -- of the corresponding inner instantiations.
6945 Astype
:= Ancestor_Subtype
(E
);
6946 while Present
(Astype
)
6947 and then Nkind
(Parent
(Astype
)) = N_Subtype_Declaration
6948 and then Present
(Generic_Parent_Type
(Parent
(Astype
)))
6949 and then Is_Entity_Name
(Subtype_Indication
(Parent
(Astype
)))
6951 Check_Private_View
(Subtype_Indication
(Parent
(Astype
)));
6952 Astype
:= Ancestor_Subtype
(Astype
);
6955 Set_Is_Generic_Actual_Type
(E
);
6957 if Is_Private_Type
(E
) and then Present
(Full_View
(E
)) then
6958 Set_Is_Generic_Actual_Type
(Full_View
(E
));
6961 Set_Is_Hidden
(E
, False);
6962 Set_Is_Potentially_Use_Visible
(E
, In_Use
(Instance
));
6964 -- We constructed the generic actual type as a subtype of the
6965 -- supplied type. This means that it normally would not inherit
6966 -- subtype specific attributes of the actual, which is wrong for
6967 -- the generic case.
6969 Astype
:= Ancestor_Subtype
(E
);
6973 -- This can happen when E is an itype that is the full view of
6974 -- a private type completed, e.g. with a constrained array. In
6975 -- that case, use the first subtype, which will carry size
6976 -- information. The base type itself is unconstrained and will
6979 Astype
:= First_Subtype
(E
);
6982 Set_Size_Info
(E
, (Astype
));
6983 Copy_RM_Size
(To
=> E
, From
=> Astype
);
6984 Set_First_Rep_Item
(E
, First_Rep_Item
(Astype
));
6986 if Is_Discrete_Or_Fixed_Point_Type
(E
) then
6987 Set_RM_Size
(E
, RM_Size
(Astype
));
6990 elsif Ekind
(E
) = E_Package
then
6992 -- If this is the renaming for the current instance, we're done.
6993 -- Otherwise it is a formal package. If the corresponding formal
6994 -- was declared with a box, the (instantiations of the) generic
6995 -- formal part are also visible. Otherwise, ignore the entity
6996 -- created to validate the actuals.
6998 if Renamed_Entity
(E
) = Instance
then
7001 elsif Nkind
(Parent
(E
)) /= N_Package_Renaming_Declaration
then
7004 -- The visibility of a formal of an enclosing generic is already
7007 elsif Denotes_Formal_Package
(E
) then
7010 elsif Present
(Associated_Formal_Package
(E
))
7011 and then not Is_Generic_Formal
(E
)
7013 if Box_Present
(Parent
(Associated_Formal_Package
(E
))) then
7014 Check_Generic_Actuals
(Renamed_Entity
(E
), True);
7017 Check_Generic_Actuals
(Renamed_Entity
(E
), False);
7020 Set_Is_Hidden
(E
, False);
7023 -- If this is a subprogram instance (in a wrapper package) the
7024 -- actual is fully visible.
7026 elsif Is_Wrapper_Package
(Instance
) then
7027 Set_Is_Hidden
(E
, False);
7029 -- If the formal package is declared with a box, or if the formal
7030 -- parameter is defaulted, it is visible in the body.
7032 elsif Is_Formal_Box
or else Is_Visible_Formal
(E
) then
7033 Set_Is_Hidden
(E
, False);
7036 if Ekind
(E
) = E_Constant
then
7038 -- If the type of the actual is a private type declared in the
7039 -- enclosing scope of the generic unit, the body of the generic
7040 -- sees the full view of the type (because it has to appear in
7041 -- the corresponding package body). If the type is private now,
7042 -- exchange views to restore the proper visiblity in the instance.
7045 Typ
: constant Entity_Id
:= Base_Type
(Etype
(E
));
7046 -- The type of the actual
7051 Parent_Scope
: Entity_Id
;
7052 -- The enclosing scope of the generic unit
7055 if Is_Wrapper_Package
(Instance
) then
7059 (Unit_Declaration_Node
7060 (Related_Instance
(Instance
))));
7063 Generic_Parent
(Package_Specification
(Instance
));
7066 Parent_Scope
:= Scope
(Gen_Id
);
7068 -- The exchange is only needed if the generic is defined
7069 -- within a package which is not a common ancestor of the
7070 -- scope of the instance, and is not already in scope.
7072 if Is_Private_Type
(Typ
)
7073 and then Scope
(Typ
) = Parent_Scope
7074 and then Scope
(Instance
) /= Parent_Scope
7075 and then Ekind
(Parent_Scope
) = E_Package
7076 and then not Is_Child_Unit
(Gen_Id
)
7080 -- If the type of the entity is a subtype, it may also have
7081 -- to be made visible, together with the base type of its
7082 -- full view, after exchange.
7084 if Is_Private_Type
(Etype
(E
)) then
7085 Switch_View
(Etype
(E
));
7086 Switch_View
(Base_Type
(Etype
(E
)));
7094 end Check_Generic_Actuals
;
7096 ------------------------------
7097 -- Check_Generic_Child_Unit --
7098 ------------------------------
7100 procedure Check_Generic_Child_Unit
7102 Parent_Installed
: in out Boolean)
7104 Loc
: constant Source_Ptr
:= Sloc
(Gen_Id
);
7105 Gen_Par
: Entity_Id
:= Empty
;
7107 Inst_Par
: Entity_Id
;
7110 function Find_Generic_Child
7112 Id
: Node_Id
) return Entity_Id
;
7113 -- Search generic parent for possible child unit with the given name
7115 function In_Enclosing_Instance
return Boolean;
7116 -- Within an instance of the parent, the child unit may be denoted by
7117 -- a simple name, or an abbreviated expanded name. Examine enclosing
7118 -- scopes to locate a possible parent instantiation.
7120 ------------------------
7121 -- Find_Generic_Child --
7122 ------------------------
7124 function Find_Generic_Child
7126 Id
: Node_Id
) return Entity_Id
7131 -- If entity of name is already set, instance has already been
7132 -- resolved, e.g. in an enclosing instantiation.
7134 if Present
(Entity
(Id
)) then
7135 if Scope
(Entity
(Id
)) = Scop
then
7142 E
:= First_Entity
(Scop
);
7143 while Present
(E
) loop
7144 if Chars
(E
) = Chars
(Id
)
7145 and then Is_Child_Unit
(E
)
7147 if Is_Child_Unit
(E
)
7148 and then not Is_Visible_Lib_Unit
(E
)
7151 ("generic child unit& is not visible", Gen_Id
, E
);
7163 end Find_Generic_Child
;
7165 ---------------------------
7166 -- In_Enclosing_Instance --
7167 ---------------------------
7169 function In_Enclosing_Instance
return Boolean is
7170 Enclosing_Instance
: Node_Id
;
7171 Instance_Decl
: Node_Id
;
7174 -- We do not inline any call that contains instantiations, except
7175 -- for instantiations of Unchecked_Conversion, so if we are within
7176 -- an inlined body the current instance does not require parents.
7178 if In_Inlined_Body
then
7179 pragma Assert
(Chars
(Gen_Id
) = Name_Unchecked_Conversion
);
7183 -- Loop to check enclosing scopes
7185 Enclosing_Instance
:= Current_Scope
;
7186 while Present
(Enclosing_Instance
) loop
7187 Instance_Decl
:= Unit_Declaration_Node
(Enclosing_Instance
);
7189 if Ekind
(Enclosing_Instance
) = E_Package
7190 and then Is_Generic_Instance
(Enclosing_Instance
)
7192 (Generic_Parent
(Specification
(Instance_Decl
)))
7194 -- Check whether the generic we are looking for is a child of
7197 E
:= Find_Generic_Child
7198 (Generic_Parent
(Specification
(Instance_Decl
)), Gen_Id
);
7199 exit when Present
(E
);
7205 Enclosing_Instance
:= Scope
(Enclosing_Instance
);
7217 Make_Expanded_Name
(Loc
,
7219 Prefix
=> New_Occurrence_Of
(Enclosing_Instance
, Loc
),
7220 Selector_Name
=> New_Occurrence_Of
(E
, Loc
)));
7222 Set_Entity
(Gen_Id
, E
);
7223 Set_Etype
(Gen_Id
, Etype
(E
));
7224 Parent_Installed
:= False; -- Already in scope.
7227 end In_Enclosing_Instance
;
7229 -- Start of processing for Check_Generic_Child_Unit
7232 -- If the name of the generic is given by a selected component, it may
7233 -- be the name of a generic child unit, and the prefix is the name of an
7234 -- instance of the parent, in which case the child unit must be visible.
7235 -- If this instance is not in scope, it must be placed there and removed
7236 -- after instantiation, because what is being instantiated is not the
7237 -- original child, but the corresponding child present in the instance
7240 -- If the child is instantiated within the parent, it can be given by
7241 -- a simple name. In this case the instance is already in scope, but
7242 -- the child generic must be recovered from the generic parent as well.
7244 if Nkind
(Gen_Id
) = N_Selected_Component
then
7245 S
:= Selector_Name
(Gen_Id
);
7246 Analyze
(Prefix
(Gen_Id
));
7247 Inst_Par
:= Entity
(Prefix
(Gen_Id
));
7249 if Ekind
(Inst_Par
) = E_Package
7250 and then Present
(Renamed_Entity
(Inst_Par
))
7252 Inst_Par
:= Renamed_Entity
(Inst_Par
);
7255 if Ekind
(Inst_Par
) = E_Package
then
7256 if Nkind
(Parent
(Inst_Par
)) = N_Package_Specification
then
7257 Gen_Par
:= Generic_Parent
(Parent
(Inst_Par
));
7259 elsif Nkind
(Parent
(Inst_Par
)) = N_Defining_Program_Unit_Name
7261 Nkind
(Parent
(Parent
(Inst_Par
))) = N_Package_Specification
7263 Gen_Par
:= Generic_Parent
(Parent
(Parent
(Inst_Par
)));
7266 elsif Ekind
(Inst_Par
) = E_Generic_Package
7267 and then Nkind
(Parent
(Gen_Id
)) = N_Formal_Package_Declaration
7269 -- A formal package may be a real child package, and not the
7270 -- implicit instance within a parent. In this case the child is
7271 -- not visible and has to be retrieved explicitly as well.
7273 Gen_Par
:= Inst_Par
;
7276 if Present
(Gen_Par
) then
7278 -- The prefix denotes an instantiation. The entity itself may be a
7279 -- nested generic, or a child unit.
7281 E
:= Find_Generic_Child
(Gen_Par
, S
);
7284 Change_Selected_Component_To_Expanded_Name
(Gen_Id
);
7285 Set_Entity
(Gen_Id
, E
);
7286 Set_Etype
(Gen_Id
, Etype
(E
));
7288 Set_Etype
(S
, Etype
(E
));
7290 -- Indicate that this is a reference to the parent
7292 if In_Extended_Main_Source_Unit
(Gen_Id
) then
7293 Set_Is_Instantiated
(Inst_Par
);
7296 -- A common mistake is to replicate the naming scheme of a
7297 -- hierarchy by instantiating a generic child directly, rather
7298 -- than the implicit child in a parent instance:
7300 -- generic .. package Gpar is ..
7301 -- generic .. package Gpar.Child is ..
7302 -- package Par is new Gpar ();
7305 -- package Par.Child is new Gpar.Child ();
7306 -- rather than Par.Child
7308 -- In this case the instantiation is within Par, which is an
7309 -- instance, but Gpar does not denote Par because we are not IN
7310 -- the instance of Gpar, so this is illegal. The test below
7311 -- recognizes this particular case.
7313 if Is_Child_Unit
(E
)
7314 and then not Comes_From_Source
(Entity
(Prefix
(Gen_Id
)))
7315 and then (not In_Instance
7316 or else Nkind
(Parent
(Parent
(Gen_Id
))) =
7320 ("prefix of generic child unit must be instance of parent",
7324 if not In_Open_Scopes
(Inst_Par
)
7325 and then Nkind
(Parent
(Gen_Id
)) not in
7326 N_Generic_Renaming_Declaration
7328 Install_Parent
(Inst_Par
);
7329 Parent_Installed
:= True;
7331 elsif In_Open_Scopes
(Inst_Par
) then
7333 -- If the parent is already installed, install the actuals
7334 -- for its formal packages. This is necessary when the child
7335 -- instance is a child of the parent instance: in this case,
7336 -- the parent is placed on the scope stack but the formal
7337 -- packages are not made visible.
7339 Install_Formal_Packages
(Inst_Par
);
7343 -- If the generic parent does not contain an entity that
7344 -- corresponds to the selector, the instance doesn't either.
7345 -- Analyzing the node will yield the appropriate error message.
7346 -- If the entity is not a child unit, then it is an inner
7347 -- generic in the parent.
7355 if Is_Child_Unit
(Entity
(Gen_Id
))
7357 Nkind
(Parent
(Gen_Id
)) not in N_Generic_Renaming_Declaration
7358 and then not In_Open_Scopes
(Inst_Par
)
7360 Install_Parent
(Inst_Par
);
7361 Parent_Installed
:= True;
7363 -- The generic unit may be the renaming of the implicit child
7364 -- present in an instance. In that case the parent instance is
7365 -- obtained from the name of the renamed entity.
7367 elsif Ekind
(Entity
(Gen_Id
)) = E_Generic_Package
7368 and then Present
(Renamed_Entity
(Entity
(Gen_Id
)))
7369 and then Is_Child_Unit
(Renamed_Entity
(Entity
(Gen_Id
)))
7372 Renamed_Package
: constant Node_Id
:=
7373 Name
(Parent
(Entity
(Gen_Id
)));
7375 if Nkind
(Renamed_Package
) = N_Expanded_Name
then
7376 Inst_Par
:= Entity
(Prefix
(Renamed_Package
));
7377 Install_Parent
(Inst_Par
);
7378 Parent_Installed
:= True;
7384 elsif Nkind
(Gen_Id
) = N_Expanded_Name
then
7386 -- Entity already present, analyze prefix, whose meaning may be an
7387 -- instance in the current context. If it is an instance of a
7388 -- relative within another, the proper parent may still have to be
7389 -- installed, if they are not of the same generation.
7391 Analyze
(Prefix
(Gen_Id
));
7393 -- Prevent cascaded errors
7395 if Etype
(Prefix
(Gen_Id
)) = Any_Type
then
7399 -- In the unlikely case that a local declaration hides the name of
7400 -- the parent package, locate it on the homonym chain. If the context
7401 -- is an instance of the parent, the renaming entity is flagged as
7404 Inst_Par
:= Entity
(Prefix
(Gen_Id
));
7405 while Present
(Inst_Par
)
7406 and then not Is_Package_Or_Generic_Package
(Inst_Par
)
7408 Inst_Par
:= Homonym
(Inst_Par
);
7411 pragma Assert
(Present
(Inst_Par
));
7412 Set_Entity
(Prefix
(Gen_Id
), Inst_Par
);
7414 if In_Enclosing_Instance
then
7417 elsif Present
(Entity
(Gen_Id
))
7418 and then No
(Renamed_Entity
(Entity
(Gen_Id
)))
7419 and then Is_Child_Unit
(Entity
(Gen_Id
))
7420 and then not In_Open_Scopes
(Inst_Par
)
7422 Install_Parent
(Inst_Par
);
7423 Parent_Installed
:= True;
7425 -- Handle renaming of generic child unit
7427 elsif Present
(Entity
(Gen_Id
))
7428 and then Present
(Renamed_Entity
(Entity
(Gen_Id
)))
7429 and then Is_Child_Unit
(Renamed_Entity
(Entity
(Gen_Id
)))
7436 -- The entity of the renamed generic child unit does not
7437 -- have any reference to the instantiated parent. In order to
7438 -- locate it we traverse the scope containing the renaming
7439 -- declaration; the instance of the parent is available in
7440 -- the prefix of the renaming declaration. For example:
7443 -- package Inst_Par is new ...
7444 -- generic package Ren_Child renames Ins_Par.Child;
7449 -- package Inst_Child is new A.Ren_Child;
7452 E
:= First_Entity
(Entity
(Prefix
(Gen_Id
)));
7453 while Present
(E
) loop
7454 if not Is_Object
(E
)
7455 and then Present
(Renamed_Entity
(E
))
7457 Renamed_Entity
(E
) = Renamed_Entity
(Entity
(Gen_Id
))
7459 Ren_Decl
:= Parent
(E
);
7460 Inst_Par
:= Entity
(Prefix
(Name
(Ren_Decl
)));
7462 if not In_Open_Scopes
(Inst_Par
) then
7463 Install_Parent
(Inst_Par
);
7464 Parent_Installed
:= True;
7470 E
:= Next_Entity
(E
);
7475 elsif In_Enclosing_Instance
then
7477 -- The child unit is found in some enclosing scope
7484 -- If this is the renaming of the implicit child in a parent
7485 -- instance, recover the parent name and install it.
7487 if Is_Entity_Name
(Gen_Id
) then
7488 E
:= Entity
(Gen_Id
);
7490 if Is_Generic_Unit
(E
)
7491 and then Nkind
(Parent
(E
)) in N_Generic_Renaming_Declaration
7492 and then Is_Child_Unit
(Renamed_Entity
(E
))
7493 and then Is_Generic_Unit
(Scope
(Renamed_Entity
(E
)))
7494 and then Nkind
(Name
(Parent
(E
))) = N_Expanded_Name
7496 Rewrite
(Gen_Id
, New_Copy_Tree
(Name
(Parent
(E
))));
7497 Inst_Par
:= Entity
(Prefix
(Gen_Id
));
7499 if not In_Open_Scopes
(Inst_Par
) then
7500 Install_Parent
(Inst_Par
);
7501 Parent_Installed
:= True;
7504 -- If it is a child unit of a non-generic parent, it may be
7505 -- use-visible and given by a direct name. Install parent as
7508 elsif Is_Generic_Unit
(E
)
7509 and then Is_Child_Unit
(E
)
7511 Nkind
(Parent
(Gen_Id
)) not in N_Generic_Renaming_Declaration
7512 and then not Is_Generic_Unit
(Scope
(E
))
7514 if not In_Open_Scopes
(Scope
(E
)) then
7515 Install_Parent
(Scope
(E
));
7516 Parent_Installed
:= True;
7521 end Check_Generic_Child_Unit
;
7523 -----------------------------
7524 -- Check_Hidden_Child_Unit --
7525 -----------------------------
7527 procedure Check_Hidden_Child_Unit
7529 Gen_Unit
: Entity_Id
;
7530 Act_Decl_Id
: Entity_Id
)
7532 Gen_Id
: constant Node_Id
:= Name
(N
);
7535 if Is_Child_Unit
(Gen_Unit
)
7536 and then Is_Child_Unit
(Act_Decl_Id
)
7537 and then Nkind
(Gen_Id
) = N_Expanded_Name
7538 and then Entity
(Prefix
(Gen_Id
)) = Scope
(Act_Decl_Id
)
7539 and then Chars
(Gen_Unit
) = Chars
(Act_Decl_Id
)
7541 Error_Msg_Node_2
:= Scope
(Act_Decl_Id
);
7543 ("generic unit & is implicitly declared in &",
7544 Defining_Unit_Name
(N
), Gen_Unit
);
7545 Error_Msg_N
("\instance must have different name",
7546 Defining_Unit_Name
(N
));
7548 end Check_Hidden_Child_Unit
;
7550 ------------------------
7551 -- Check_Private_View --
7552 ------------------------
7554 procedure Check_Private_View
(N
: Node_Id
) is
7555 T
: constant Entity_Id
:= Etype
(N
);
7559 -- Exchange views if the type was not private in the generic but is
7560 -- private at the point of instantiation. Do not exchange views if
7561 -- the scope of the type is in scope. This can happen if both generic
7562 -- and instance are sibling units, or if type is defined in a parent.
7563 -- In this case the visibility of the type will be correct for all
7567 BT
:= Base_Type
(T
);
7569 if Is_Private_Type
(T
)
7570 and then not Has_Private_View
(N
)
7571 and then Present
(Full_View
(T
))
7572 and then not In_Open_Scopes
(Scope
(T
))
7574 -- In the generic, the full declaration was visible
7578 elsif Has_Private_View
(N
)
7579 and then not Is_Private_Type
(T
)
7580 and then not Has_Been_Exchanged
(T
)
7581 and then (not In_Open_Scopes
(Scope
(T
))
7582 or else Nkind
(Parent
(N
)) = N_Subtype_Declaration
)
7584 -- In the generic, only the private declaration was visible
7586 -- If the type appears in a subtype declaration, the subtype in
7587 -- instance must have a view compatible with that of its parent,
7588 -- which must be exchanged (see corresponding code in Restore_
7589 -- Private_Views) so we make an exception to the open scope rule.
7591 Prepend_Elmt
(T
, Exchanged_Views
);
7592 Exchange_Declarations
(Etype
(Get_Associated_Node
(N
)));
7594 -- Finally, a non-private subtype may have a private base type, which
7595 -- must be exchanged for consistency. This can happen when a package
7596 -- body is instantiated, when the scope stack is empty but in fact
7597 -- the subtype and the base type are declared in an enclosing scope.
7599 -- Note that in this case we introduce an inconsistency in the view
7600 -- set, because we switch the base type BT, but there could be some
7601 -- private dependent subtypes of BT which remain unswitched. Such
7602 -- subtypes might need to be switched at a later point (see specific
7603 -- provision for that case in Switch_View).
7605 elsif not Is_Private_Type
(T
)
7606 and then not Has_Private_View
(N
)
7607 and then Is_Private_Type
(BT
)
7608 and then Present
(Full_View
(BT
))
7609 and then not Is_Generic_Type
(BT
)
7610 and then not In_Open_Scopes
(BT
)
7612 Prepend_Elmt
(Full_View
(BT
), Exchanged_Views
);
7613 Exchange_Declarations
(BT
);
7616 end Check_Private_View
;
7618 -----------------------------
7619 -- Check_Hidden_Primitives --
7620 -----------------------------
7622 function Check_Hidden_Primitives
(Assoc_List
: List_Id
) return Elist_Id
is
7625 Result
: Elist_Id
:= No_Elist
;
7628 if No
(Assoc_List
) then
7632 -- Traverse the list of associations between formals and actuals
7633 -- searching for renamings of tagged types
7635 Actual
:= First
(Assoc_List
);
7636 while Present
(Actual
) loop
7637 if Nkind
(Actual
) = N_Subtype_Declaration
then
7638 Gen_T
:= Generic_Parent_Type
(Actual
);
7640 if Present
(Gen_T
) and then Is_Tagged_Type
(Gen_T
) then
7642 -- Traverse the list of primitives of the actual types
7643 -- searching for hidden primitives that are visible in the
7644 -- corresponding generic formal; leave them visible and
7645 -- append them to Result to restore their decoration later.
7647 Install_Hidden_Primitives
7648 (Prims_List
=> Result
,
7650 Act_T
=> Entity
(Subtype_Indication
(Actual
)));
7658 end Check_Hidden_Primitives
;
7660 --------------------------
7661 -- Contains_Instance_Of --
7662 --------------------------
7664 function Contains_Instance_Of
7667 N
: Node_Id
) return Boolean
7675 -- Verify that there are no circular instantiations. We check whether
7676 -- the unit contains an instance of the current scope or some enclosing
7677 -- scope (in case one of the instances appears in a subunit). Longer
7678 -- circularities involving subunits might seem too pathological to
7679 -- consider, but they were not too pathological for the authors of
7680 -- DEC bc30vsq, so we loop over all enclosing scopes, and mark all
7681 -- enclosing generic scopes as containing an instance.
7684 -- Within a generic subprogram body, the scope is not generic, to
7685 -- allow for recursive subprograms. Use the declaration to determine
7686 -- whether this is a generic unit.
7688 if Ekind
(Scop
) = E_Generic_Package
7689 or else (Is_Subprogram
(Scop
)
7690 and then Nkind
(Unit_Declaration_Node
(Scop
)) =
7691 N_Generic_Subprogram_Declaration
)
7693 Elmt
:= First_Elmt
(Inner_Instances
(Inner
));
7695 while Present
(Elmt
) loop
7696 if Node
(Elmt
) = Scop
then
7697 Error_Msg_Node_2
:= Inner
;
7699 ("circular instantiation: & instantiated within &!",
7703 elsif Node
(Elmt
) = Inner
then
7706 elsif Contains_Instance_Of
(Node
(Elmt
), Scop
, N
) then
7707 Error_Msg_Node_2
:= Inner
;
7709 ("circular instantiation: & instantiated within &!",
7717 -- Indicate that Inner is being instantiated within Scop
7719 Append_Elmt
(Inner
, Inner_Instances
(Scop
));
7722 if Scop
= Standard_Standard
then
7725 Scop
:= Scope
(Scop
);
7730 end Contains_Instance_Of
;
7732 -----------------------
7733 -- Copy_Generic_Node --
7734 -----------------------
7736 function Copy_Generic_Node
7738 Parent_Id
: Node_Id
;
7739 Instantiating
: Boolean) return Node_Id
7744 function Copy_Generic_Descendant
(D
: Union_Id
) return Union_Id
;
7745 -- Check the given value of one of the Fields referenced by the current
7746 -- node to determine whether to copy it recursively. The field may hold
7747 -- a Node_Id, a List_Id, or an Elist_Id, or a plain value (Sloc, Uint,
7748 -- Char) in which case it need not be copied.
7750 procedure Copy_Descendants
;
7751 -- Common utility for various nodes
7753 function Copy_Generic_Elist
(E
: Elist_Id
) return Elist_Id
;
7754 -- Make copy of element list
7756 function Copy_Generic_List
7758 Parent_Id
: Node_Id
) return List_Id
;
7759 -- Apply Copy_Generic_Node recursively to the members of a node list
7761 function In_Defining_Unit_Name
(Nam
: Node_Id
) return Boolean;
7762 -- True if an identifier is part of the defining program unit name of
7764 -- Consider removing this subprogram now that ASIS no longer uses it.
7766 ----------------------
7767 -- Copy_Descendants --
7768 ----------------------
7770 procedure Copy_Descendants
is
7771 procedure Walk
is new
7772 Walk_Sinfo_Fields_Pairwise
(Copy_Generic_Descendant
);
7775 end Copy_Descendants
;
7777 -----------------------------
7778 -- Copy_Generic_Descendant --
7779 -----------------------------
7781 function Copy_Generic_Descendant
(D
: Union_Id
) return Union_Id
is
7783 if D
= Union_Id
(Empty
) then
7786 elsif D
in Node_Range
then
7788 (Copy_Generic_Node
(Node_Id
(D
), New_N
, Instantiating
));
7790 elsif D
in List_Range
then
7791 return Union_Id
(Copy_Generic_List
(List_Id
(D
), New_N
));
7793 elsif D
in Elist_Range
then
7794 return Union_Id
(Copy_Generic_Elist
(Elist_Id
(D
)));
7796 -- Nothing else is copyable (e.g. Uint values), return as is
7801 end Copy_Generic_Descendant
;
7803 ------------------------
7804 -- Copy_Generic_Elist --
7805 ------------------------
7807 function Copy_Generic_Elist
(E
: Elist_Id
) return Elist_Id
is
7814 M
:= First_Elmt
(E
);
7815 while Present
(M
) loop
7817 (Copy_Generic_Node
(Node
(M
), Empty
, Instantiating
), L
);
7826 end Copy_Generic_Elist
;
7828 -----------------------
7829 -- Copy_Generic_List --
7830 -----------------------
7832 function Copy_Generic_List
7834 Parent_Id
: Node_Id
) return List_Id
7842 Set_Parent
(New_L
, Parent_Id
);
7845 while Present
(N
) loop
7846 Append
(Copy_Generic_Node
(N
, Empty
, Instantiating
), New_L
);
7855 end Copy_Generic_List
;
7857 ---------------------------
7858 -- In_Defining_Unit_Name --
7859 ---------------------------
7861 function In_Defining_Unit_Name
(Nam
: Node_Id
) return Boolean is
7864 Present
(Parent
(Nam
))
7865 and then (Nkind
(Parent
(Nam
)) = N_Defining_Program_Unit_Name
7867 (Nkind
(Parent
(Nam
)) = N_Expanded_Name
7868 and then In_Defining_Unit_Name
(Parent
(Nam
))));
7869 end In_Defining_Unit_Name
;
7871 -- Start of processing for Copy_Generic_Node
7878 New_N
:= New_Copy
(N
);
7880 -- Copy aspects if present
7882 if Has_Aspects
(N
) then
7883 Set_Has_Aspects
(New_N
, False);
7884 Set_Aspect_Specifications
7885 (New_N
, Copy_Generic_List
(Aspect_Specifications
(N
), Parent_Id
));
7888 -- If we are instantiating, we want to adjust the sloc based on the
7889 -- current S_Adjustment. However, if this is the root node of a subunit,
7890 -- we need to defer that adjustment to below (see "elsif Instantiating
7891 -- and Was_Stub"), so it comes after Create_Instantiation_Source has
7892 -- computed the adjustment.
7895 and then not (Nkind
(N
) in N_Proper_Body
7896 and then Was_Originally_Stub
(N
))
7898 Adjust_Instantiation_Sloc
(New_N
, S_Adjustment
);
7901 if not Is_List_Member
(N
) then
7902 Set_Parent
(New_N
, Parent_Id
);
7905 -- Special casing for identifiers and other entity names and operators
7907 if Nkind
(New_N
) in N_Character_Literal
7913 if not Instantiating
then
7915 -- Link both nodes in order to assign subsequently the entity of
7916 -- the copy to the original node, in case this is a global
7919 Set_Associated_Node
(N
, New_N
);
7921 -- If we are within an instantiation, this is a nested generic
7922 -- that has already been analyzed at the point of definition.
7923 -- We must preserve references that were global to the enclosing
7924 -- parent at that point. Other occurrences, whether global or
7925 -- local to the current generic, must be resolved anew, so we
7926 -- reset the entity in the generic copy. A global reference has a
7927 -- smaller depth than the parent, or else the same depth in case
7928 -- both are distinct compilation units.
7930 -- A child unit is implicitly declared within the enclosing parent
7931 -- but is in fact global to it, and must be preserved.
7933 -- It is also possible for Current_Instantiated_Parent to be
7934 -- defined, and for this not to be a nested generic, namely if
7935 -- the unit is loaded through Rtsfind. In that case, the entity of
7936 -- New_N is only a link to the associated node, and not a defining
7939 -- The entities for parent units in the defining_program_unit of a
7940 -- generic child unit are established when the context of the unit
7941 -- is first analyzed, before the generic copy is made. They are
7942 -- preserved in the copy for use in e.g. ASIS queries.
7944 Ent
:= Entity
(New_N
);
7946 if No
(Current_Instantiated_Parent
.Gen_Id
) then
7948 or else Nkind
(Ent
) /= N_Defining_Identifier
7949 or else not In_Defining_Unit_Name
(N
)
7951 Set_Associated_Node
(New_N
, Empty
);
7955 or else Nkind
(Ent
) not in N_Entity
7956 or else No
(Scope
(Ent
))
7958 (Scope
(Ent
) = Current_Instantiated_Parent
.Gen_Id
7959 and then not Is_Child_Unit
(Ent
))
7961 (Scope_Depth_Set
(Scope
(Ent
))
7963 Scope_Depth
(Scope
(Ent
)) >
7964 Scope_Depth
(Current_Instantiated_Parent
.Gen_Id
)
7966 Get_Source_Unit
(Ent
) =
7967 Get_Source_Unit
(Current_Instantiated_Parent
.Gen_Id
))
7969 Set_Associated_Node
(New_N
, Empty
);
7972 -- Case of instantiating identifier or some other name or operator
7975 -- If the associated node is still defined, the entity in it
7976 -- is global, and must be copied to the instance. If this copy
7977 -- is being made for a body to inline, it is applied to an
7978 -- instantiated tree, and the entity is already present and
7979 -- must be also preserved.
7982 Assoc
: constant Node_Id
:= Get_Associated_Node
(N
);
7985 if Present
(Assoc
) then
7986 if Nkind
(Assoc
) = Nkind
(N
) then
7987 Set_Entity
(New_N
, Entity
(Assoc
));
7988 Check_Private_View
(N
);
7990 -- Here we deal with a very peculiar case for which the
7991 -- Has_Private_View mechanism is not sufficient, because
7992 -- the reference to the type is implicit in the tree,
7993 -- that is to say, it's not referenced from a node but
7994 -- only from another type, namely through Component_Type.
7998 -- type Pt is private;
8001 -- type Ft is array (Positive range <>) of Pt;
8003 -- procedure Check (F1, F2 : Ft; Lt : Boolean);
8007 -- type Pt is new Boolean;
8010 -- package body P is
8011 -- package body G is
8012 -- procedure Check (F1, F2 : Ft; Lt : Boolean) is
8014 -- if (F1 < F2) /= Lt then
8021 -- type Arr is array (Positive range <>) of P.Pt;
8023 -- package Inst is new P.G (Arr);
8025 -- Pt is a global type for the generic package G and it
8026 -- is not referenced in its body, but only as component
8027 -- type of Ft, which is a local type. This means that no
8028 -- references to Pt or Ft are seen during the copy of the
8029 -- body, the only reference to Pt being seen is when the
8030 -- actuals are checked by Check_Generic_Actuals, but Pt
8031 -- is still private at this point. In the end, the views
8032 -- of Pt are not switched in the body and, therefore, the
8033 -- array comparison is rejected because the component is
8036 -- Adding e.g. a dummy variable of type Pt in the body is
8037 -- sufficient to make everything work, so we generate an
8038 -- artificial reference to Pt on the fly and thus force
8039 -- the switching of views on the grounds that, if the
8040 -- comparison was accepted during the semantic analysis
8041 -- of the generic, this means that the component cannot
8042 -- have been private (see Sem_Type.Valid_Comparison_Arg).
8044 if Nkind
(Assoc
) in N_Op_Compare
8045 and then Present
(Etype
(Left_Opnd
(Assoc
)))
8046 and then Is_Array_Type
(Etype
(Left_Opnd
(Assoc
)))
8047 and then Present
(Etype
(Right_Opnd
(Assoc
)))
8048 and then Is_Array_Type
(Etype
(Right_Opnd
(Assoc
)))
8051 Ltyp
: constant Entity_Id
:=
8052 Etype
(Left_Opnd
(Assoc
));
8053 Rtyp
: constant Entity_Id
:=
8054 Etype
(Right_Opnd
(Assoc
));
8056 if Is_Private_Type
(Component_Type
(Ltyp
)) then
8058 (New_Occurrence_Of
(Component_Type
(Ltyp
),
8061 if Is_Private_Type
(Component_Type
(Rtyp
)) then
8063 (New_Occurrence_Of
(Component_Type
(Rtyp
),
8068 -- Here is a similar case, for the Designated_Type of an
8069 -- access type that is present as target type in a type
8070 -- conversion from another access type. In this case, if
8071 -- the base types of the designated types are different
8072 -- and the conversion was accepted during the semantic
8073 -- analysis of the generic, this means that the target
8074 -- type cannot have been private (see Valid_Conversion).
8076 elsif Nkind
(Assoc
) = N_Identifier
8077 and then Nkind
(Parent
(Assoc
)) = N_Type_Conversion
8078 and then Subtype_Mark
(Parent
(Assoc
)) = Assoc
8079 and then Present
(Etype
(Assoc
))
8080 and then Is_Access_Type
(Etype
(Assoc
))
8081 and then Present
(Etype
(Expression
(Parent
(Assoc
))))
8083 Is_Access_Type
(Etype
(Expression
(Parent
(Assoc
))))
8086 Targ_Desig
: constant Entity_Id
:=
8087 Designated_Type
(Etype
(Assoc
));
8088 Expr_Desig
: constant Entity_Id
:=
8090 (Etype
(Expression
(Parent
(Assoc
))));
8092 if Base_Type
(Targ_Desig
) /= Base_Type
(Expr_Desig
)
8093 and then Is_Private_Type
(Targ_Desig
)
8096 (New_Occurrence_Of
(Targ_Desig
, Sloc
(N
)));
8101 -- The node is a reference to a global type and acts as the
8102 -- subtype mark of a qualified expression created in order
8103 -- to aid resolution of accidental overloading in instances.
8104 -- Since N is a reference to a type, the Associated_Node of
8105 -- N denotes an entity rather than another identifier. See
8106 -- Qualify_Universal_Operands for details.
8108 elsif Nkind
(N
) = N_Identifier
8109 and then Nkind
(Parent
(N
)) = N_Qualified_Expression
8110 and then Subtype_Mark
(Parent
(N
)) = N
8111 and then Is_Qualified_Universal_Literal
(Parent
(N
))
8113 Set_Entity
(New_N
, Assoc
);
8115 -- The name in the call may be a selected component if the
8116 -- call has not been analyzed yet, as may be the case for
8117 -- pre/post conditions in a generic unit.
8119 elsif Nkind
(Assoc
) = N_Function_Call
8120 and then Is_Entity_Name
(Name
(Assoc
))
8122 Set_Entity
(New_N
, Entity
(Name
(Assoc
)));
8124 elsif Nkind
(Assoc
) in N_Entity
8125 and then Expander_Active
8127 -- Inlining case: we are copying a tree that contains
8128 -- global entities, which are preserved in the copy to be
8129 -- used for subsequent inlining.
8134 Set_Entity
(New_N
, Empty
);
8140 -- For expanded name, we must copy the Prefix and Selector_Name
8142 if Nkind
(N
) = N_Expanded_Name
then
8144 (New_N
, Copy_Generic_Node
(Prefix
(N
), New_N
, Instantiating
));
8146 Set_Selector_Name
(New_N
,
8147 Copy_Generic_Node
(Selector_Name
(N
), New_N
, Instantiating
));
8149 -- For operators, copy the operands
8151 elsif Nkind
(N
) in N_Op
then
8152 if Nkind
(N
) in N_Binary_Op
then
8153 Set_Left_Opnd
(New_N
,
8154 Copy_Generic_Node
(Left_Opnd
(N
), New_N
, Instantiating
));
8157 Set_Right_Opnd
(New_N
,
8158 Copy_Generic_Node
(Right_Opnd
(N
), New_N
, Instantiating
));
8161 -- Establish a link between an entity from the generic template and the
8162 -- corresponding entity in the generic copy to be analyzed.
8164 elsif Nkind
(N
) in N_Entity
then
8165 if not Instantiating
then
8166 Set_Associated_Entity
(N
, New_N
);
8169 -- Clear any existing link the copy may inherit from the replicated
8170 -- generic template entity.
8172 Set_Associated_Entity
(New_N
, Empty
);
8174 -- Special casing for stubs
8176 elsif Nkind
(N
) in N_Body_Stub
then
8178 -- In any case, we must copy the specification or defining
8179 -- identifier as appropriate.
8181 if Nkind
(N
) = N_Subprogram_Body_Stub
then
8182 Set_Specification
(New_N
,
8183 Copy_Generic_Node
(Specification
(N
), New_N
, Instantiating
));
8186 Set_Defining_Identifier
(New_N
,
8188 (Defining_Identifier
(N
), New_N
, Instantiating
));
8191 -- If we are not instantiating, then this is where we load and
8192 -- analyze subunits, i.e. at the point where the stub occurs. A
8193 -- more permissive system might defer this analysis to the point
8194 -- of instantiation, but this seems too complicated for now.
8196 if not Instantiating
then
8198 Subunit_Name
: constant Unit_Name_Type
:= Get_Unit_Name
(N
);
8200 Unum
: Unit_Number_Type
;
8204 -- Make sure that, if it is a subunit of the main unit that is
8205 -- preprocessed and if -gnateG is specified, the preprocessed
8206 -- file will be written.
8208 Lib
.Analysing_Subunit_Of_Main
:=
8209 Lib
.In_Extended_Main_Source_Unit
(N
);
8212 (Load_Name
=> Subunit_Name
,
8216 Lib
.Analysing_Subunit_Of_Main
:= False;
8218 -- If the proper body is not found, a warning message will be
8219 -- emitted when analyzing the stub, or later at the point of
8220 -- instantiation. Here we just leave the stub as is.
8222 if Unum
= No_Unit
then
8223 Subunits_Missing
:= True;
8224 goto Subunit_Not_Found
;
8227 Subunit
:= Cunit
(Unum
);
8229 if Nkind
(Unit
(Subunit
)) /= N_Subunit
then
8231 ("found child unit instead of expected SEPARATE subunit",
8233 Error_Msg_Sloc
:= Sloc
(N
);
8234 Error_Msg_N
("\to complete stub #", Subunit
);
8235 goto Subunit_Not_Found
;
8238 -- We must create a generic copy of the subunit, in order to
8239 -- perform semantic analysis on it, and we must replace the
8240 -- stub in the original generic unit with the subunit, in order
8241 -- to preserve non-local references within.
8243 -- Only the proper body needs to be copied. Library_Unit and
8244 -- context clause are simply inherited by the generic copy.
8245 -- Note that the copy (which may be recursive if there are
8246 -- nested subunits) must be done first, before attaching it to
8247 -- the enclosing generic.
8251 (Proper_Body
(Unit
(Subunit
)),
8252 Empty
, Instantiating
=> False);
8254 -- Now place the original proper body in the original generic
8255 -- unit. This is a body, not a compilation unit.
8257 Rewrite
(N
, Proper_Body
(Unit
(Subunit
)));
8258 Set_Is_Compilation_Unit
(Defining_Entity
(N
), False);
8259 Set_Was_Originally_Stub
(N
);
8261 -- Finally replace the body of the subunit with its copy, and
8262 -- make this new subunit into the library unit of the generic
8263 -- copy, which does not have stubs any longer.
8265 Set_Proper_Body
(Unit
(Subunit
), New_Body
);
8266 Set_Library_Unit
(New_N
, Subunit
);
8267 Inherit_Context
(Unit
(Subunit
), N
);
8270 -- If we are instantiating, this must be an error case, since
8271 -- otherwise we would have replaced the stub node by the proper body
8272 -- that corresponds. So just ignore it in the copy (i.e. we have
8273 -- copied it, and that is good enough).
8279 <<Subunit_Not_Found
>> null;
8281 -- If the node is a compilation unit, it is the subunit of a stub, which
8282 -- has been loaded already (see code below). In this case, the library
8283 -- unit field of N points to the parent unit (which is a compilation
8284 -- unit) and need not (and cannot) be copied.
8286 -- When the proper body of the stub is analyzed, the library_unit link
8287 -- is used to establish the proper context (see sem_ch10).
8289 -- The other fields of a compilation unit are copied as usual
8291 elsif Nkind
(N
) = N_Compilation_Unit
then
8293 -- This code can only be executed when not instantiating, because in
8294 -- the copy made for an instantiation, the compilation unit node has
8295 -- disappeared at the point that a stub is replaced by its proper
8298 pragma Assert
(not Instantiating
);
8300 Set_Context_Items
(New_N
,
8301 Copy_Generic_List
(Context_Items
(N
), New_N
));
8304 Copy_Generic_Node
(Unit
(N
), New_N
, Instantiating
=> False));
8306 Set_First_Inlined_Subprogram
(New_N
,
8308 (First_Inlined_Subprogram
(N
), New_N
, Instantiating
=> False));
8313 (Aux_Decls_Node
(N
), New_N
, Instantiating
=> False));
8315 -- For an assignment node, the assignment is known to be semantically
8316 -- legal if we are instantiating the template. This avoids incorrect
8317 -- diagnostics in generated code.
8319 elsif Nkind
(N
) = N_Assignment_Statement
then
8321 -- Copy name and expression fields in usual manner
8324 Copy_Generic_Node
(Name
(N
), New_N
, Instantiating
));
8326 Set_Expression
(New_N
,
8327 Copy_Generic_Node
(Expression
(N
), New_N
, Instantiating
));
8329 if Instantiating
then
8330 Set_Assignment_OK
(Name
(New_N
), True);
8333 elsif Nkind
(N
) in N_Aggregate | N_Extension_Aggregate
then
8334 if not Instantiating
then
8335 Set_Associated_Node
(N
, New_N
);
8338 if Present
(Get_Associated_Node
(N
))
8339 and then Nkind
(Get_Associated_Node
(N
)) = Nkind
(N
)
8341 -- In the generic the aggregate has some composite type. If at
8342 -- the point of instantiation the type has a private view,
8343 -- install the full view (and that of its ancestors, if any).
8346 T
: Entity_Id
:= (Etype
(Get_Associated_Node
(New_N
)));
8350 if Present
(T
) and then Is_Private_Type
(T
) then
8355 and then Is_Tagged_Type
(T
)
8356 and then Is_Derived_Type
(T
)
8358 Rt
:= Root_Type
(T
);
8363 if Is_Private_Type
(T
) then
8374 -- Do not copy the associated node, which points to the generic copy
8375 -- of the aggregate.
8377 if Nkind
(N
) = N_Aggregate
then
8378 Set_Aggregate_Bounds
8380 Node_Id
(Copy_Generic_Descendant
8381 (Union_Id
(Aggregate_Bounds
(N
)))));
8383 elsif Nkind
(N
) = N_Extension_Aggregate
then
8386 Node_Id
(Copy_Generic_Descendant
8387 (Union_Id
(Ancestor_Part
(N
)))));
8390 pragma Assert
(False);
8395 List_Id
(Copy_Generic_Descendant
(Union_Id
(Expressions
(N
)))));
8396 Set_Component_Associations
8398 List_Id
(Copy_Generic_Descendant
8399 (Union_Id
(Component_Associations
(N
)))));
8401 (New_N
, Node_Id
(Copy_Generic_Descendant
(Union_Id
(Etype
(N
)))));
8403 -- Allocators do not have an identifier denoting the access type, so we
8404 -- must locate it through the expression to check whether the views are
8407 elsif Nkind
(N
) = N_Allocator
8408 and then Nkind
(Expression
(N
)) = N_Qualified_Expression
8409 and then Is_Entity_Name
(Subtype_Mark
(Expression
(N
)))
8410 and then Instantiating
8413 T
: constant Node_Id
:=
8414 Get_Associated_Node
(Subtype_Mark
(Expression
(N
)));
8420 -- Retrieve the allocator node in the generic copy
8422 Acc_T
:= Etype
(Parent
(Parent
(T
)));
8424 if Present
(Acc_T
) and then Is_Private_Type
(Acc_T
) then
8425 Switch_View
(Acc_T
);
8432 -- For a proper body, we must catch the case of a proper body that
8433 -- replaces a stub. This represents the point at which a separate
8434 -- compilation unit, and hence template file, may be referenced, so we
8435 -- must make a new source instantiation entry for the template of the
8436 -- subunit, and ensure that all nodes in the subunit are adjusted using
8437 -- this new source instantiation entry.
8439 elsif Nkind
(N
) in N_Proper_Body
then
8441 Save_Adjustment
: constant Sloc_Adjustment
:= S_Adjustment
;
8443 if Instantiating
and then Was_Originally_Stub
(N
) then
8444 Create_Instantiation_Source
8445 (Instantiation_Node
,
8446 Defining_Entity
(N
),
8449 Adjust_Instantiation_Sloc
(New_N
, S_Adjustment
);
8452 -- Now copy the fields of the proper body, using the new
8453 -- adjustment factor if one was needed as per test above.
8457 -- Restore the original adjustment factor
8459 S_Adjustment
:= Save_Adjustment
;
8462 elsif Nkind
(N
) = N_Pragma
and then Instantiating
then
8464 -- Do not copy Comment or Ident pragmas their content is relevant to
8465 -- the generic unit, not to the instantiating unit.
8467 if Pragma_Name_Unmapped
(N
) in Name_Comment | Name_Ident
then
8468 New_N
:= Make_Null_Statement
(Sloc
(N
));
8470 -- Do not copy pragmas generated from aspects because the pragmas do
8471 -- not carry any semantic information, plus they will be regenerated
8474 -- However, generating C we need to copy them since postconditions
8475 -- are inlined by the front end, and the front-end inlining machinery
8476 -- relies on this routine to perform inlining.
8478 elsif From_Aspect_Specification
(N
)
8479 and then not Modify_Tree_For_C
8481 New_N
:= Make_Null_Statement
(Sloc
(N
));
8487 elsif Nkind
(N
) in N_Integer_Literal | N_Real_Literal
then
8489 -- No descendant fields need traversing
8493 elsif Nkind
(N
) = N_String_Literal
8494 and then Present
(Etype
(N
))
8495 and then Instantiating
8497 -- If the string is declared in an outer scope, the string_literal
8498 -- subtype created for it may have the wrong scope. Force reanalysis
8499 -- of the constant to generate a new itype in the proper context.
8501 Set_Etype
(New_N
, Empty
);
8502 Set_Analyzed
(New_N
, False);
8504 -- For the remaining nodes, copy their descendants recursively
8509 if Instantiating
and then Nkind
(N
) = N_Subprogram_Body
then
8510 Set_Generic_Parent
(Specification
(New_N
), N
);
8512 -- Should preserve Corresponding_Spec??? (12.3(14))
8516 -- Propagate dimensions if present, so that they are reflected in the
8519 if Nkind
(N
) in N_Has_Etype
8520 and then (Nkind
(N
) in N_Op
or else Is_Entity_Name
(N
))
8521 and then Present
(Etype
(N
))
8522 and then Is_Floating_Point_Type
(Etype
(N
))
8523 and then Has_Dimension_System
(Etype
(N
))
8525 Copy_Dimensions
(N
, New_N
);
8529 end Copy_Generic_Node
;
8531 ----------------------------
8532 -- Denotes_Formal_Package --
8533 ----------------------------
8535 function Denotes_Formal_Package
8537 On_Exit
: Boolean := False;
8538 Instance
: Entity_Id
:= Empty
) return Boolean
8541 Scop
: constant Entity_Id
:= Scope
(Pack
);
8544 function Is_Actual_Of_Previous_Formal
(P
: Entity_Id
) return Boolean;
8545 -- The package in question may be an actual for a previous formal
8546 -- package P of the current instance, so examine its actuals as well.
8547 -- This must be recursive over other formal packages.
8549 ----------------------------------
8550 -- Is_Actual_Of_Previous_Formal --
8551 ----------------------------------
8553 function Is_Actual_Of_Previous_Formal
(P
: Entity_Id
) return Boolean is
8557 E1
:= First_Entity
(P
);
8558 while Present
(E1
) and then E1
/= Instance
loop
8559 if Ekind
(E1
) = E_Package
8560 and then Nkind
(Parent
(E1
)) = N_Package_Renaming_Declaration
8562 if Renamed_Entity
(E1
) = Pack
then
8565 elsif E1
= P
or else Renamed_Entity
(E1
) = P
then
8568 elsif Is_Actual_Of_Previous_Formal
(E1
) then
8577 end Is_Actual_Of_Previous_Formal
;
8579 -- Start of processing for Denotes_Formal_Package
8585 (Instance_Envs
.Last
).Instantiated_Parent
.Act_Id
;
8587 Par
:= Current_Instantiated_Parent
.Act_Id
;
8590 if Ekind
(Scop
) = E_Generic_Package
8591 or else Nkind
(Unit_Declaration_Node
(Scop
)) =
8592 N_Generic_Subprogram_Declaration
8596 elsif Nkind
(Original_Node
(Unit_Declaration_Node
(Pack
))) =
8597 N_Formal_Package_Declaration
8605 -- Check whether this package is associated with a formal package of
8606 -- the enclosing instantiation. Iterate over the list of renamings.
8608 E
:= First_Entity
(Par
);
8609 while Present
(E
) loop
8610 if Ekind
(E
) /= E_Package
8611 or else Nkind
(Parent
(E
)) /= N_Package_Renaming_Declaration
8615 elsif Renamed_Entity
(E
) = Par
then
8618 elsif Renamed_Entity
(E
) = Pack
then
8621 elsif Is_Actual_Of_Previous_Formal
(E
) then
8631 end Denotes_Formal_Package
;
8637 procedure End_Generic
is
8639 -- ??? More things could be factored out in this routine. Should
8640 -- probably be done at a later stage.
8642 Inside_A_Generic
:= Generic_Flags
.Table
(Generic_Flags
.Last
);
8643 Generic_Flags
.Decrement_Last
;
8645 Expander_Mode_Restore
;
8652 function Earlier
(N1
, N2
: Node_Id
) return Boolean is
8653 procedure Find_Depth
(P
: in out Node_Id
; D
: in out Integer);
8654 -- Find distance from given node to enclosing compilation unit
8660 procedure Find_Depth
(P
: in out Node_Id
; D
: in out Integer) is
8663 and then Nkind
(P
) /= N_Compilation_Unit
8665 P
:= True_Parent
(P
);
8670 -- Local declarations
8679 -- Start of processing for Earlier
8682 Find_Depth
(P1
, D1
);
8683 Find_Depth
(P2
, D2
);
8693 P1
:= True_Parent
(P1
);
8698 P2
:= True_Parent
(P2
);
8702 -- At this point P1 and P2 are at the same distance from the root.
8703 -- We examine their parents until we find a common declarative list.
8704 -- If we reach the root, N1 and N2 do not descend from the same
8705 -- declarative list (e.g. one is nested in the declarative part and
8706 -- the other is in a block in the statement part) and the earlier
8707 -- one is already frozen.
8709 while not Is_List_Member
(P1
)
8710 or else not Is_List_Member
(P2
)
8711 or else not In_Same_List
(P1
, P2
)
8713 P1
:= True_Parent
(P1
);
8714 P2
:= True_Parent
(P2
);
8716 if Nkind
(Parent
(P1
)) = N_Subunit
then
8717 P1
:= Corresponding_Stub
(Parent
(P1
));
8720 if Nkind
(Parent
(P2
)) = N_Subunit
then
8721 P2
:= Corresponding_Stub
(Parent
(P2
));
8729 -- Expanded code usually shares the source location of the original
8730 -- construct it was generated for. This however may not necessarily
8731 -- reflect the true location of the code within the tree.
8733 -- Before comparing the slocs of the two nodes, make sure that we are
8734 -- working with correct source locations. Assume that P1 is to the left
8735 -- of P2. If either one does not come from source, traverse the common
8736 -- list heading towards the other node and locate the first source
8740 -- ----+===+===+--------------+===+===+----
8741 -- expanded code expanded code
8743 if not Comes_From_Source
(P1
) then
8744 while Present
(P1
) loop
8746 -- Neither P2 nor a source statement were located during the
8747 -- search. If we reach the end of the list, then P1 does not
8748 -- occur earlier than P2.
8751 -- start --- P2 ----- P1 --- end
8753 if No
(Next
(P1
)) then
8756 -- We encounter P2 while going to the right of the list. This
8757 -- means that P1 does indeed appear earlier.
8760 -- start --- P1 ===== P2 --- end
8761 -- expanded code in between
8766 -- No need to look any further since we have located a source
8769 elsif Comes_From_Source
(P1
) then
8779 if not Comes_From_Source
(P2
) then
8780 while Present
(P2
) loop
8782 -- Neither P1 nor a source statement were located during the
8783 -- search. If we reach the start of the list, then P1 does not
8784 -- occur earlier than P2.
8787 -- start --- P2 --- P1 --- end
8789 if No
(Prev
(P2
)) then
8792 -- We encounter P1 while going to the left of the list. This
8793 -- means that P1 does indeed appear earlier.
8796 -- start --- P1 ===== P2 --- end
8797 -- expanded code in between
8802 -- No need to look any further since we have located a source
8805 elsif Comes_From_Source
(P2
) then
8815 -- At this point either both nodes came from source or we approximated
8816 -- their source locations through neighboring source statements.
8818 T1
:= Top_Level_Location
(Sloc
(P1
));
8819 T2
:= Top_Level_Location
(Sloc
(P2
));
8821 -- When two nodes come from the same instance, they have identical top
8822 -- level locations. To determine proper relation within the tree, check
8823 -- their locations within the template.
8826 return Sloc
(P1
) < Sloc
(P2
);
8828 -- The two nodes either come from unrelated instances or do not come
8829 -- from instantiated code at all.
8836 ----------------------
8837 -- Find_Actual_Type --
8838 ----------------------
8840 function Find_Actual_Type
8842 Gen_Type
: Entity_Id
) return Entity_Id
8844 Gen_Scope
: constant Entity_Id
:= Scope
(Gen_Type
);
8848 -- Special processing only applies to child units
8850 if not Is_Child_Unit
(Gen_Scope
) then
8851 return Get_Instance_Of
(Typ
);
8853 -- If designated or component type is itself a formal of the child unit,
8854 -- its instance is available.
8856 elsif Scope
(Typ
) = Gen_Scope
then
8857 return Get_Instance_Of
(Typ
);
8859 -- If the array or access type is not declared in the parent unit,
8860 -- no special processing needed.
8862 elsif not Is_Generic_Type
(Typ
)
8863 and then Scope
(Gen_Scope
) /= Scope
(Typ
)
8865 return Get_Instance_Of
(Typ
);
8867 -- Otherwise, retrieve designated or component type by visibility
8870 T
:= Current_Entity
(Typ
);
8871 while Present
(T
) loop
8872 if In_Open_Scopes
(Scope
(T
)) then
8874 elsif Is_Generic_Actual_Type
(T
) then
8883 end Find_Actual_Type
;
8885 -----------------------------
8886 -- Freeze_Package_Instance --
8887 -----------------------------
8889 procedure Freeze_Package_Instance
8895 function In_Same_Scope
(Gen_Id
, Act_Id
: Node_Id
) return Boolean;
8896 -- Check if the generic definition and the instantiation come from
8897 -- a common scope, in which case the instance must be frozen after
8898 -- the generic body.
8900 function True_Sloc
(N
, Act_Unit
: Node_Id
) return Source_Ptr
;
8901 -- If the instance is nested inside a generic unit, the Sloc of the
8902 -- instance indicates the place of the original definition, not the
8903 -- point of the current enclosing instance. Pending a better usage of
8904 -- Slocs to indicate instantiation places, we determine the place of
8905 -- origin of a node by finding the maximum sloc of any ancestor node.
8907 -- Why is this not equivalent to Top_Level_Location ???
8913 function In_Same_Scope
(Gen_Id
, Act_Id
: Node_Id
) return Boolean is
8914 Act_Scop
: Entity_Id
:= Scope
(Act_Id
);
8915 Gen_Scop
: Entity_Id
:= Scope
(Gen_Id
);
8918 while Act_Scop
/= Standard_Standard
8919 and then Gen_Scop
/= Standard_Standard
8921 if Act_Scop
= Gen_Scop
then
8925 Act_Scop
:= Scope
(Act_Scop
);
8926 Gen_Scop
:= Scope
(Gen_Scop
);
8936 function True_Sloc
(N
, Act_Unit
: Node_Id
) return Source_Ptr
is
8943 while Present
(N1
) and then N1
/= Act_Unit
loop
8944 if Sloc
(N1
) > Res
then
8956 Gen_Id
: constant Entity_Id
:= Get_Generic_Entity
(N
);
8957 Par_Id
: constant Entity_Id
:= Scope
(Gen_Id
);
8958 Act_Unit
: constant Node_Id
:= Unit
(Cunit
(Get_Source_Unit
(N
)));
8959 Gen_Unit
: constant Node_Id
:=
8960 Unit
(Cunit
(Get_Source_Unit
(Gen_Decl
)));
8962 Body_Unit
: Node_Id
;
8964 Must_Delay
: Boolean;
8965 Orig_Body
: Node_Id
;
8967 -- Start of processing for Freeze_Package_Instance
8970 -- If the body is a subunit, the freeze point is the corresponding stub
8971 -- in the current compilation, not the subunit itself.
8973 if Nkind
(Parent
(Gen_Body
)) = N_Subunit
then
8974 Orig_Body
:= Corresponding_Stub
(Parent
(Gen_Body
));
8976 Orig_Body
:= Gen_Body
;
8979 Body_Unit
:= Unit
(Cunit
(Get_Source_Unit
(Orig_Body
)));
8981 -- If the instantiation and the generic definition appear in the same
8982 -- package declaration, this is an early instantiation. If they appear
8983 -- in the same declarative part, it is an early instantiation only if
8984 -- the generic body appears textually later, and the generic body is
8985 -- also in the main unit.
8987 -- If instance is nested within a subprogram, and the generic body
8988 -- is not, the instance is delayed because the enclosing body is. If
8989 -- instance and body are within the same scope, or the same subprogram
8990 -- body, indicate explicitly that the instance is delayed.
8993 (Gen_Unit
= Act_Unit
8994 and then (Nkind
(Gen_Unit
) in N_Generic_Package_Declaration
8995 | N_Package_Declaration
8996 or else (Gen_Unit
= Body_Unit
8998 True_Sloc
(N
, Act_Unit
) < Sloc
(Orig_Body
)))
8999 and then Is_In_Main_Unit
(Original_Node
(Gen_Unit
))
9000 and then In_Same_Scope
(Gen_Id
, Act_Id
));
9002 -- If this is an early instantiation, the freeze node is placed after
9003 -- the generic body. Otherwise, if the generic appears in an instance,
9004 -- we cannot freeze the current instance until the outer one is frozen.
9005 -- This is only relevant if the current instance is nested within some
9006 -- inner scope not itself within the outer instance. If this scope is
9007 -- a package body in the same declarative part as the outer instance,
9008 -- then that body needs to be frozen after the outer instance. Finally,
9009 -- if no delay is needed, we place the freeze node at the end of the
9010 -- current declarative part.
9012 if No
(Freeze_Node
(Act_Id
))
9013 or else not Is_List_Member
(Freeze_Node
(Act_Id
))
9015 Ensure_Freeze_Node
(Act_Id
);
9016 F_Node
:= Freeze_Node
(Act_Id
);
9019 Insert_After
(Orig_Body
, F_Node
);
9021 elsif Is_Generic_Instance
(Par_Id
)
9022 and then Present
(Freeze_Node
(Par_Id
))
9023 and then Scope
(Act_Id
) /= Par_Id
9025 -- Freeze instance of inner generic after instance of enclosing
9028 if In_Same_Declarative_Part
(Parent
(Freeze_Node
(Par_Id
)), N
) then
9030 -- Handle the following case:
9032 -- package Parent_Inst is new ...
9033 -- freeze Parent_Inst []
9035 -- procedure P ... -- this body freezes Parent_Inst
9037 -- package Inst is new ...
9039 -- In this particular scenario, the freeze node for Inst must
9040 -- be inserted in the same manner as that of Parent_Inst,
9041 -- before the next source body or at the end of the declarative
9042 -- list (body not available). If body P did not exist and
9043 -- Parent_Inst was frozen after Inst, either by a body
9044 -- following Inst or at the end of the declarative region,
9045 -- the freeze node for Inst must be inserted after that of
9046 -- Parent_Inst. This relation is established by comparing
9047 -- the Slocs of Parent_Inst freeze node and Inst.
9048 -- We examine the parents of the enclosing lists to handle
9049 -- the case where the parent instance is in the visible part
9050 -- of a package declaration, and the inner instance is in
9051 -- the corresponding private part.
9053 if Parent
(List_Containing
(Freeze_Node
(Par_Id
)))
9054 = Parent
(List_Containing
(N
))
9055 and then Sloc
(Freeze_Node
(Par_Id
)) <= Sloc
(N
)
9057 Insert_Freeze_Node_For_Instance
(N
, F_Node
);
9059 Insert_After
(Freeze_Node
(Par_Id
), F_Node
);
9062 -- Freeze package enclosing instance of inner generic after
9063 -- instance of enclosing generic.
9065 elsif Nkind
(Parent
(N
)) in N_Package_Body | N_Subprogram_Body
9066 and then In_Same_Declarative_Part
9067 (Parent
(Freeze_Node
(Par_Id
)), Parent
(N
))
9070 Enclosing
: Entity_Id
;
9073 Enclosing
:= Corresponding_Spec
(Parent
(N
));
9075 if No
(Enclosing
) then
9076 Enclosing
:= Defining_Entity
(Parent
(N
));
9079 Insert_Freeze_Node_For_Instance
(N
, F_Node
);
9080 Ensure_Freeze_Node
(Enclosing
);
9082 if not Is_List_Member
(Freeze_Node
(Enclosing
)) then
9084 -- The enclosing context is a subunit, insert the freeze
9085 -- node after the stub.
9087 if Nkind
(Parent
(Parent
(N
))) = N_Subunit
then
9088 Insert_Freeze_Node_For_Instance
9089 (Corresponding_Stub
(Parent
(Parent
(N
))),
9090 Freeze_Node
(Enclosing
));
9092 -- The enclosing context is a package with a stub body
9093 -- which has already been replaced by the real body.
9094 -- Insert the freeze node after the actual body.
9096 elsif Ekind
(Enclosing
) = E_Package
9097 and then Present
(Body_Entity
(Enclosing
))
9098 and then Was_Originally_Stub
9099 (Parent
(Body_Entity
(Enclosing
)))
9101 Insert_Freeze_Node_For_Instance
9102 (Parent
(Body_Entity
(Enclosing
)),
9103 Freeze_Node
(Enclosing
));
9105 -- The parent instance has been frozen before the body of
9106 -- the enclosing package, insert the freeze node after
9109 elsif In_Same_List
(Freeze_Node
(Par_Id
), Parent
(N
))
9111 Sloc
(Freeze_Node
(Par_Id
)) <= Sloc
(Parent
(N
))
9113 Insert_Freeze_Node_For_Instance
9114 (Parent
(N
), Freeze_Node
(Enclosing
));
9118 (Freeze_Node
(Par_Id
), Freeze_Node
(Enclosing
));
9124 Insert_Freeze_Node_For_Instance
(N
, F_Node
);
9128 Insert_Freeze_Node_For_Instance
(N
, F_Node
);
9131 end Freeze_Package_Instance
;
9133 --------------------------------
9134 -- Freeze_Subprogram_Instance --
9135 --------------------------------
9137 procedure Freeze_Subprogram_Instance
9140 Pack_Id
: Entity_Id
)
9142 function Enclosing_Package_Body
(N
: Node_Id
) return Node_Id
;
9143 -- Find innermost package body that encloses the given node, and which
9144 -- is not a compilation unit. Freeze nodes for the instance, or for its
9145 -- enclosing body, may be inserted after the enclosing_body of the
9146 -- generic unit. Used to determine proper placement of freeze node for
9147 -- both package and subprogram instances.
9149 function Package_Freeze_Node
(B
: Node_Id
) return Node_Id
;
9150 -- Find entity for given package body, and locate or create a freeze
9153 ----------------------------
9154 -- Enclosing_Package_Body --
9155 ----------------------------
9157 function Enclosing_Package_Body
(N
: Node_Id
) return Node_Id
is
9163 and then Nkind
(Parent
(P
)) /= N_Compilation_Unit
9165 if Nkind
(P
) = N_Package_Body
then
9166 if Nkind
(Parent
(P
)) = N_Subunit
then
9167 return Corresponding_Stub
(Parent
(P
));
9173 P
:= True_Parent
(P
);
9177 end Enclosing_Package_Body
;
9179 -------------------------
9180 -- Package_Freeze_Node --
9181 -------------------------
9183 function Package_Freeze_Node
(B
: Node_Id
) return Node_Id
is
9187 if Nkind
(B
) = N_Package_Body
then
9188 Id
:= Corresponding_Spec
(B
);
9189 else pragma Assert
(Nkind
(B
) = N_Package_Body_Stub
);
9190 Id
:= Corresponding_Spec
(Proper_Body
(Unit
(Library_Unit
(B
))));
9193 Ensure_Freeze_Node
(Id
);
9194 return Freeze_Node
(Id
);
9195 end Package_Freeze_Node
;
9199 Enc_G
: constant Node_Id
:= Enclosing_Package_Body
(Gen_Body
);
9200 Enc_N
: constant Node_Id
:= Enclosing_Package_Body
(N
);
9201 Par_Id
: constant Entity_Id
:= Scope
(Get_Generic_Entity
(N
));
9206 -- Start of processing for Freeze_Subprogram_Instance
9209 -- If the instance and the generic body appear within the same unit, and
9210 -- the instance precedes the generic, the freeze node for the instance
9211 -- must appear after that of the generic. If the generic is nested
9212 -- within another instance I2, then current instance must be frozen
9213 -- after I2. In both cases, the freeze nodes are those of enclosing
9214 -- packages. Otherwise, the freeze node is placed at the end of the
9215 -- current declarative part.
9217 Ensure_Freeze_Node
(Pack_Id
);
9218 F_Node
:= Freeze_Node
(Pack_Id
);
9220 if Is_Generic_Instance
(Par_Id
)
9221 and then Present
(Freeze_Node
(Par_Id
))
9222 and then In_Same_Declarative_Part
(Parent
(Freeze_Node
(Par_Id
)), N
)
9224 -- The parent was a premature instantiation. Insert freeze node at
9225 -- the end the current declarative part.
9227 if Is_Known_Guaranteed_ABE
(Get_Unit_Instantiation_Node
(Par_Id
)) then
9228 Insert_Freeze_Node_For_Instance
(N
, F_Node
);
9230 -- Handle the following case:
9232 -- package Parent_Inst is new ...
9233 -- freeze Parent_Inst []
9235 -- procedure P ... -- this body freezes Parent_Inst
9237 -- procedure Inst is new ...
9239 -- In this particular scenario, the freeze node for Inst must be
9240 -- inserted in the same manner as that of Parent_Inst - before the
9241 -- next source body or at the end of the declarative list (body not
9242 -- available). If body P did not exist and Parent_Inst was frozen
9243 -- after Inst, either by a body following Inst or at the end of the
9244 -- declarative region, the freeze node for Inst must be inserted
9245 -- after that of Parent_Inst. This relation is established by
9246 -- comparing the Slocs of Parent_Inst freeze node and Inst.
9248 elsif In_Same_List
(Freeze_Node
(Par_Id
), N
)
9249 and then Sloc
(Freeze_Node
(Par_Id
)) <= Sloc
(N
)
9251 Insert_Freeze_Node_For_Instance
(N
, F_Node
);
9254 Insert_After
(Freeze_Node
(Par_Id
), F_Node
);
9257 -- The body enclosing the instance should be frozen after the body that
9258 -- includes the generic, because the body of the instance may make
9259 -- references to entities therein. If the two are not in the same
9260 -- declarative part, or if the one enclosing the instance is frozen
9261 -- already, freeze the instance at the end of the current declarative
9264 elsif Is_Generic_Instance
(Par_Id
)
9265 and then Present
(Freeze_Node
(Par_Id
))
9266 and then Present
(Enc_N
)
9268 if In_Same_Declarative_Part
(Parent
(Freeze_Node
(Par_Id
)), Enc_N
)
9270 -- The enclosing package may contain several instances. Rather
9271 -- than computing the earliest point at which to insert its freeze
9272 -- node, we place it at the end of the declarative part of the
9273 -- parent of the generic.
9275 Insert_Freeze_Node_For_Instance
9276 (Freeze_Node
(Par_Id
), Package_Freeze_Node
(Enc_N
));
9279 Insert_Freeze_Node_For_Instance
(N
, F_Node
);
9281 elsif Present
(Enc_G
)
9282 and then Present
(Enc_N
)
9283 and then Enc_G
/= Enc_N
9284 and then Earlier
(N
, Gen_Body
)
9286 -- Freeze package that encloses instance, and place node after the
9287 -- package that encloses generic. If enclosing package is already
9288 -- frozen we have to assume it is at the proper place. This may be a
9289 -- potential ABE that requires dynamic checking. Do not add a freeze
9290 -- node if the package that encloses the generic is inside the body
9291 -- that encloses the instance, because the freeze node would be in
9292 -- the wrong scope. Additional contortions needed if the bodies are
9293 -- within a subunit.
9296 Enclosing_Body
: Node_Id
;
9299 if Nkind
(Enc_N
) = N_Package_Body_Stub
then
9300 Enclosing_Body
:= Proper_Body
(Unit
(Library_Unit
(Enc_N
)));
9302 Enclosing_Body
:= Enc_N
;
9305 if Parent
(List_Containing
(Enc_G
)) /= Enclosing_Body
then
9306 Insert_Freeze_Node_For_Instance
9307 (Enc_G
, Package_Freeze_Node
(Enc_N
));
9311 -- Freeze enclosing subunit before instance
9313 Enc_G_F
:= Package_Freeze_Node
(Enc_G
);
9315 if not Is_List_Member
(Enc_G_F
) then
9316 Insert_After
(Enc_G
, Enc_G_F
);
9319 Insert_Freeze_Node_For_Instance
(N
, F_Node
);
9322 -- If none of the above, insert freeze node at the end of the current
9323 -- declarative part.
9325 Insert_Freeze_Node_For_Instance
(N
, F_Node
);
9327 end Freeze_Subprogram_Instance
;
9333 function Get_Gen_Id
(E
: Assoc_Ptr
) return Entity_Id
is
9335 return Generic_Renamings
.Table
(E
).Gen_Id
;
9338 ---------------------
9339 -- Get_Instance_Of --
9340 ---------------------
9342 function Get_Instance_Of
(A
: Entity_Id
) return Entity_Id
is
9343 Res
: constant Assoc_Ptr
:= Generic_Renamings_HTable
.Get
(A
);
9346 if Res
/= Assoc_Null
then
9347 return Generic_Renamings
.Table
(Res
).Act_Id
;
9350 -- On exit, entity is not instantiated: not a generic parameter, or
9351 -- else parameter of an inner generic unit.
9355 end Get_Instance_Of
;
9357 ---------------------------------
9358 -- Get_Unit_Instantiation_Node --
9359 ---------------------------------
9361 function Get_Unit_Instantiation_Node
(A
: Entity_Id
) return Node_Id
is
9362 Decl
: Node_Id
:= Unit_Declaration_Node
(A
);
9366 -- If the Package_Instantiation attribute has been set on the package
9367 -- entity, then use it directly when it (or its Original_Node) refers
9368 -- to an N_Package_Instantiation node. In principle it should be
9369 -- possible to have this field set in all cases, which should be
9370 -- investigated, and would allow this function to be significantly
9373 Inst
:= Package_Instantiation
(A
);
9375 if Present
(Inst
) then
9376 if Nkind
(Inst
) = N_Package_Instantiation
then
9379 elsif Nkind
(Original_Node
(Inst
)) = N_Package_Instantiation
then
9380 return Original_Node
(Inst
);
9384 -- If the instantiation is a compilation unit that does not need body
9385 -- then the instantiation node has been rewritten as a package
9386 -- declaration for the instance, and we return the original node.
9388 -- If it is a compilation unit and the instance node has not been
9389 -- rewritten, then it is still the unit of the compilation. Finally, if
9390 -- a body is present, this is a parent of the main unit whose body has
9391 -- been compiled for inlining purposes, and the instantiation node has
9392 -- been rewritten with the instance body.
9394 -- Otherwise the instantiation node appears after the declaration. If
9395 -- the entity is a formal package, the declaration may have been
9396 -- rewritten as a generic declaration (in the case of a formal with box)
9397 -- or left as a formal package declaration if it has actuals, and is
9398 -- found with a forward search.
9400 if Nkind
(Parent
(Decl
)) = N_Compilation_Unit
then
9401 if Nkind
(Decl
) = N_Package_Declaration
9402 and then Present
(Corresponding_Body
(Decl
))
9404 Decl
:= Unit_Declaration_Node
(Corresponding_Body
(Decl
));
9407 if Nkind
(Original_Node
(Decl
)) in N_Generic_Instantiation
then
9408 return Original_Node
(Decl
);
9410 return Unit
(Parent
(Decl
));
9413 elsif Nkind
(Decl
) = N_Package_Declaration
9414 and then Nkind
(Original_Node
(Decl
)) = N_Formal_Package_Declaration
9416 return Original_Node
(Decl
);
9419 Inst
:= Next
(Decl
);
9420 while Nkind
(Inst
) not in N_Formal_Package_Declaration
9421 | N_Function_Instantiation
9422 | N_Package_Instantiation
9423 | N_Procedure_Instantiation
9430 end Get_Unit_Instantiation_Node
;
9432 ------------------------
9433 -- Has_Been_Exchanged --
9434 ------------------------
9436 function Has_Been_Exchanged
(E
: Entity_Id
) return Boolean is
9440 Next
:= First_Elmt
(Exchanged_Views
);
9441 while Present
(Next
) loop
9442 if Full_View
(Node
(Next
)) = E
then
9450 end Has_Been_Exchanged
;
9456 function Has_Contracts
(Decl
: Node_Id
) return Boolean is
9457 A_List
: constant List_Id
:= Aspect_Specifications
(Decl
);
9464 A_Spec
:= First
(A_List
);
9465 while Present
(A_Spec
) loop
9466 A_Id
:= Get_Aspect_Id
(A_Spec
);
9467 if A_Id
= Aspect_Pre
or else A_Id
= Aspect_Post
then
9482 function Hash
(F
: Entity_Id
) return HTable_Range
is
9484 return HTable_Range
(F
mod HTable_Size
);
9487 ------------------------
9488 -- Hide_Current_Scope --
9489 ------------------------
9491 procedure Hide_Current_Scope
is
9492 C
: constant Entity_Id
:= Current_Scope
;
9496 Set_Is_Hidden_Open_Scope
(C
);
9498 E
:= First_Entity
(C
);
9499 while Present
(E
) loop
9500 if Is_Immediately_Visible
(E
) then
9501 Set_Is_Immediately_Visible
(E
, False);
9502 Append_Elmt
(E
, Hidden_Entities
);
9508 -- Make the scope name invisible as well. This is necessary, but might
9509 -- conflict with calls to Rtsfind later on, in case the scope is a
9510 -- predefined one. There is no clean solution to this problem, so for
9511 -- now we depend on the user not redefining Standard itself in one of
9512 -- the parent units.
9514 if Is_Immediately_Visible
(C
) and then C
/= Standard_Standard
then
9515 Set_Is_Immediately_Visible
(C
, False);
9516 Append_Elmt
(C
, Hidden_Entities
);
9519 end Hide_Current_Scope
;
9525 procedure Init_Env
is
9526 Saved
: Instance_Env
;
9529 Saved
.Instantiated_Parent
:= Current_Instantiated_Parent
;
9530 Saved
.Exchanged_Views
:= Exchanged_Views
;
9531 Saved
.Hidden_Entities
:= Hidden_Entities
;
9532 Saved
.Current_Sem_Unit
:= Current_Sem_Unit
;
9533 Saved
.Parent_Unit_Visible
:= Parent_Unit_Visible
;
9534 Saved
.Instance_Parent_Unit
:= Instance_Parent_Unit
;
9536 -- Save configuration switches. These may be reset if the unit is a
9537 -- predefined unit, and the current mode is not Ada 2005.
9539 Saved
.Switches
:= Save_Config_Switches
;
9541 Instance_Envs
.Append
(Saved
);
9543 Exchanged_Views
:= New_Elmt_List
;
9544 Hidden_Entities
:= New_Elmt_List
;
9546 -- Make dummy entry for Instantiated parent. If generic unit is legal,
9547 -- this is set properly in Set_Instance_Env.
9549 Current_Instantiated_Parent
:=
9550 (Current_Scope
, Current_Scope
, Assoc_Null
);
9553 ---------------------
9554 -- In_Main_Context --
9555 ---------------------
9557 function In_Main_Context
(E
: Entity_Id
) return Boolean is
9563 if not Is_Compilation_Unit
(E
)
9564 or else Ekind
(E
) /= E_Package
9565 or else In_Private_Part
(E
)
9570 Context
:= Context_Items
(Cunit
(Main_Unit
));
9572 Clause
:= First
(Context
);
9573 while Present
(Clause
) loop
9574 if Nkind
(Clause
) = N_With_Clause
then
9575 Nam
:= Name
(Clause
);
9577 -- If the current scope is part of the context of the main unit,
9578 -- analysis of the corresponding with_clause is not complete, and
9579 -- the entity is not set. We use the Chars field directly, which
9580 -- might produce false positives in rare cases, but guarantees
9581 -- that we produce all the instance bodies we will need.
9583 if (Is_Entity_Name
(Nam
) and then Chars
(Nam
) = Chars
(E
))
9584 or else (Nkind
(Nam
) = N_Selected_Component
9585 and then Chars
(Selector_Name
(Nam
)) = Chars
(E
))
9595 end In_Main_Context
;
9597 ---------------------
9598 -- Inherit_Context --
9599 ---------------------
9601 procedure Inherit_Context
(Gen_Decl
: Node_Id
; Inst
: Node_Id
) is
9602 Current_Context
: List_Id
;
9603 Current_Unit
: Node_Id
;
9612 if Nkind
(Parent
(Gen_Decl
)) = N_Compilation_Unit
then
9614 -- The inherited context is attached to the enclosing compilation
9615 -- unit. This is either the main unit, or the declaration for the
9616 -- main unit (in case the instantiation appears within the package
9617 -- declaration and the main unit is its body).
9619 Current_Unit
:= Parent
(Inst
);
9620 while Present
(Current_Unit
)
9621 and then Nkind
(Current_Unit
) /= N_Compilation_Unit
9623 Current_Unit
:= Parent
(Current_Unit
);
9626 Current_Context
:= Context_Items
(Current_Unit
);
9628 Item
:= First
(Context_Items
(Parent
(Gen_Decl
)));
9629 while Present
(Item
) loop
9630 if Nkind
(Item
) = N_With_Clause
then
9631 Lib_Unit
:= Library_Unit
(Item
);
9633 -- Take care to prevent direct cyclic with's
9635 if Lib_Unit
/= Current_Unit
then
9637 -- Do not add a unit if it is already in the context
9639 Clause
:= First
(Current_Context
);
9641 while Present
(Clause
) loop
9642 if Nkind
(Clause
) = N_With_Clause
9643 and then Library_Unit
(Clause
) = Lib_Unit
9653 New_I
:= New_Copy
(Item
);
9654 Set_Implicit_With
(New_I
);
9656 Append
(New_I
, Current_Context
);
9664 end Inherit_Context
;
9670 procedure Initialize
is
9672 Generic_Renamings
.Init
;
9675 Generic_Renamings_HTable
.Reset
;
9676 Circularity_Detected
:= False;
9677 Exchanged_Views
:= No_Elist
;
9678 Hidden_Entities
:= No_Elist
;
9681 -------------------------------------
9682 -- Insert_Freeze_Node_For_Instance --
9683 -------------------------------------
9685 procedure Insert_Freeze_Node_For_Instance
9689 function Enclosing_Body
(N
: Node_Id
) return Node_Id
;
9690 -- Find enclosing package or subprogram body, if any. Freeze node may
9691 -- be placed at end of current declarative list if previous instance
9692 -- and current one have different enclosing bodies.
9694 function Previous_Instance
(Gen
: Entity_Id
) return Entity_Id
;
9695 -- Find the local instance, if any, that declares the generic that is
9696 -- being instantiated. If present, the freeze node for this instance
9697 -- must follow the freeze node for the previous instance.
9699 --------------------
9700 -- Enclosing_Body --
9701 --------------------
9703 function Enclosing_Body
(N
: Node_Id
) return Node_Id
is
9709 and then Nkind
(Parent
(P
)) /= N_Compilation_Unit
9711 if Nkind
(P
) in N_Package_Body | N_Subprogram_Body
then
9712 if Nkind
(Parent
(P
)) = N_Subunit
then
9713 return Corresponding_Stub
(Parent
(P
));
9719 P
:= True_Parent
(P
);
9725 -----------------------
9726 -- Previous_Instance --
9727 -----------------------
9729 function Previous_Instance
(Gen
: Entity_Id
) return Entity_Id
is
9734 while Present
(S
) and then S
/= Standard_Standard
loop
9735 if Is_Generic_Instance
(S
)
9736 and then In_Same_Source_Unit
(S
, N
)
9745 end Previous_Instance
;
9756 -- Start of processing for Insert_Freeze_Node_For_Instance
9759 -- Nothing to do if the freeze node has already been inserted
9761 if Is_List_Member
(F_Node
) then
9765 Inst
:= Entity
(F_Node
);
9767 -- When processing a subprogram instantiation, utilize the actual
9768 -- subprogram instantiation rather than its package wrapper as it
9769 -- carries all the context information.
9771 if Is_Wrapper_Package
(Inst
) then
9772 Inst
:= Related_Instance
(Inst
);
9775 Par_Inst
:= Parent
(Inst
);
9777 -- If this is a package instance, check whether the generic is declared
9778 -- in a previous instance and the current instance is not within the
9781 if Present
(Generic_Parent
(Par_Inst
)) and then Is_In_Main_Unit
(N
) then
9783 Enclosing_N
: constant Node_Id
:= Enclosing_Body
(N
);
9784 Par_I
: constant Entity_Id
:=
9785 Previous_Instance
(Generic_Parent
(Par_Inst
));
9789 if Present
(Par_I
) and then Earlier
(N
, Freeze_Node
(Par_I
)) then
9790 Scop
:= Scope
(Inst
);
9792 -- If the current instance is within the one that contains
9793 -- the generic, the freeze node for the current one must
9794 -- appear in the current declarative part. Ditto, if the
9795 -- current instance is within another package instance or
9796 -- within a body that does not enclose the current instance.
9797 -- In these three cases the freeze node of the previous
9798 -- instance is not relevant.
9800 while Present
(Scop
) and then Scop
/= Standard_Standard
loop
9801 exit when Scop
= Par_I
9803 (Is_Generic_Instance
(Scop
)
9804 and then Scope_Depth
(Scop
) > Scope_Depth
(Par_I
));
9805 Scop
:= Scope
(Scop
);
9808 -- Previous instance encloses current instance
9810 if Scop
= Par_I
then
9813 -- If the next node is a source body we must freeze in the
9814 -- current scope as well.
9816 elsif Present
(Next
(N
))
9817 and then Nkind
(Next
(N
)) in N_Subprogram_Body
9819 and then Comes_From_Source
(Next
(N
))
9823 -- Current instance is within an unrelated instance
9825 elsif Is_Generic_Instance
(Scop
) then
9828 -- Current instance is within an unrelated body
9830 elsif Present
(Enclosing_N
)
9831 and then Enclosing_N
/= Enclosing_Body
(Par_I
)
9836 Insert_After
(Freeze_Node
(Par_I
), F_Node
);
9844 Decls
:= List_Containing
(N
);
9845 Par_N
:= Parent
(Decls
);
9848 -- Determine the proper freeze point of an instantiation
9850 if Is_Generic_Instance
(Inst
) then
9852 -- When the instantiation occurs in a package spec, append the
9853 -- freeze node to the private declarations (if any).
9855 if Nkind
(Par_N
) = N_Package_Specification
9856 and then Decls
= Visible_Declarations
(Par_N
)
9857 and then not Is_Empty_List
(Private_Declarations
(Par_N
))
9859 Decls
:= Private_Declarations
(Par_N
);
9860 Decl
:= First
(Decls
);
9863 -- We adhere to the general rule of a package or subprogram body
9864 -- causing freezing of anything before it in the same declarative
9865 -- region. In this respect, the proper freeze point of a package
9866 -- instantiation is before the first source body which follows, or
9867 -- before a stub. This ensures that entities from the instance are
9868 -- already frozen and therefore usable in source bodies.
9870 if Nkind
(Par_N
) /= N_Package_Declaration
9872 not In_Same_Source_Unit
(Generic_Parent
(Par_Inst
), Inst
)
9874 while Present
(Decl
) loop
9875 if ((Nkind
(Decl
) in N_Unit_Body
9877 Nkind
(Decl
) in N_Body_Stub
)
9878 and then Comes_From_Source
(Decl
))
9879 or else (Present
(Origin
)
9880 and then Nkind
(Decl
) in N_Generic_Instantiation
9881 and then Instance_Spec
(Decl
) /= Origin
)
9883 Set_Sloc
(F_Node
, Sloc
(Decl
));
9884 Insert_Before
(Decl
, F_Node
);
9892 -- When the instantiation occurs in a package spec and there is
9893 -- no source body which follows, and the package has a body but
9894 -- is delayed, then insert immediately before its freeze node.
9896 if Nkind
(Par_N
) = N_Package_Specification
9897 and then Present
(Corresponding_Body
(Parent
(Par_N
)))
9898 and then Present
(Freeze_Node
(Defining_Entity
(Par_N
)))
9900 Set_Sloc
(F_Node
, Sloc
(Freeze_Node
(Defining_Entity
(Par_N
))));
9901 Insert_Before
(Freeze_Node
(Defining_Entity
(Par_N
)), F_Node
);
9904 -- When the instantiation occurs in a package spec and there is
9905 -- no source body which follows, not even of the package itself,
9906 -- then insert into the declaration list of the outer level, but
9907 -- do not jump over following instantiations in this list because
9908 -- they may have a body that has not materialized yet, see above.
9910 elsif Nkind
(Par_N
) = N_Package_Specification
9911 and then No
(Corresponding_Body
(Parent
(Par_N
)))
9912 and then Is_List_Member
(Parent
(Par_N
))
9914 Decl
:= Parent
(Par_N
);
9915 Decls
:= List_Containing
(Decl
);
9916 Par_N
:= Parent
(Decls
);
9919 -- In a package declaration, or if no source body which follows
9920 -- and at library level, then insert at end of list.
9928 -- Insert and adjust the Sloc of the freeze node
9930 Set_Sloc
(F_Node
, Sloc
(Last
(Decls
)));
9931 Insert_After
(Last
(Decls
), F_Node
);
9932 end Insert_Freeze_Node_For_Instance
;
9934 -----------------------------
9935 -- Install_Formal_Packages --
9936 -----------------------------
9938 procedure Install_Formal_Packages
(Par
: Entity_Id
) is
9941 Gen_E
: Entity_Id
:= Empty
;
9944 E
:= First_Entity
(Par
);
9946 -- If we are installing an instance parent, locate the formal packages
9947 -- of its generic parent.
9949 if Is_Generic_Instance
(Par
) then
9950 Gen
:= Generic_Parent
(Package_Specification
(Par
));
9951 Gen_E
:= First_Entity
(Gen
);
9954 while Present
(E
) loop
9955 if Ekind
(E
) = E_Package
9956 and then Nkind
(Parent
(E
)) = N_Package_Renaming_Declaration
9958 -- If this is the renaming for the parent instance, done
9960 if Renamed_Entity
(E
) = Par
then
9963 -- The visibility of a formal of an enclosing generic is already
9966 elsif Denotes_Formal_Package
(E
) then
9969 elsif Present
(Associated_Formal_Package
(E
)) then
9970 Check_Generic_Actuals
(Renamed_Entity
(E
), True);
9971 Set_Is_Hidden
(E
, False);
9973 -- Find formal package in generic unit that corresponds to
9974 -- (instance of) formal package in instance.
9976 while Present
(Gen_E
) and then Chars
(Gen_E
) /= Chars
(E
) loop
9977 Next_Entity
(Gen_E
);
9980 if Present
(Gen_E
) then
9981 Map_Formal_Package_Entities
(Gen_E
, E
);
9988 if Present
(Gen_E
) then
9989 Next_Entity
(Gen_E
);
9992 end Install_Formal_Packages
;
9994 --------------------
9995 -- Install_Parent --
9996 --------------------
9998 procedure Install_Parent
(P
: Entity_Id
; In_Body
: Boolean := False) is
9999 Ancestors
: constant Elist_Id
:= New_Elmt_List
;
10000 S
: constant Entity_Id
:= Current_Scope
;
10001 Inst_Par
: Entity_Id
;
10002 First_Par
: Entity_Id
;
10003 Inst_Node
: Node_Id
;
10004 Gen_Par
: Entity_Id
;
10005 First_Gen
: Entity_Id
;
10008 procedure Install_Noninstance_Specs
(Par
: Entity_Id
);
10009 -- Install the scopes of noninstance parent units ending with Par
10011 procedure Install_Spec
(Par
: Entity_Id
);
10012 -- The child unit is within the declarative part of the parent, so the
10013 -- declarations within the parent are immediately visible.
10015 -------------------------------
10016 -- Install_Noninstance_Specs --
10017 -------------------------------
10019 procedure Install_Noninstance_Specs
(Par
: Entity_Id
) is
10022 and then Par
/= Standard_Standard
10023 and then not In_Open_Scopes
(Par
)
10025 Install_Noninstance_Specs
(Scope
(Par
));
10026 Install_Spec
(Par
);
10028 end Install_Noninstance_Specs
;
10034 procedure Install_Spec
(Par
: Entity_Id
) is
10035 Spec
: constant Node_Id
:= Package_Specification
(Par
);
10038 -- If this parent of the child instance is a top-level unit,
10039 -- then record the unit and its visibility for later resetting in
10040 -- Remove_Parent. We exclude units that are generic instances, as we
10041 -- only want to record this information for the ultimate top-level
10042 -- noninstance parent (is that always correct???).
10044 if Scope
(Par
) = Standard_Standard
10045 and then not Is_Generic_Instance
(Par
)
10047 Parent_Unit_Visible
:= Is_Immediately_Visible
(Par
);
10048 Instance_Parent_Unit
:= Par
;
10051 -- Open the parent scope and make it and its declarations visible.
10052 -- If this point is not within a body, then only the visible
10053 -- declarations should be made visible, and installation of the
10054 -- private declarations is deferred until the appropriate point
10055 -- within analysis of the spec being instantiated (see the handling
10056 -- of parent visibility in Analyze_Package_Specification). This is
10057 -- relaxed in the case where the parent unit is Ada.Tags, to avoid
10058 -- private view problems that occur when compiling instantiations of
10059 -- a generic child of that package (Generic_Dispatching_Constructor).
10060 -- If the instance freezes a tagged type, inlinings of operations
10061 -- from Ada.Tags may need the full view of type Tag. If inlining took
10062 -- proper account of establishing visibility of inlined subprograms'
10063 -- parents then it should be possible to remove this
10064 -- special check. ???
10067 Set_Is_Immediately_Visible
(Par
);
10068 Install_Visible_Declarations
(Par
);
10069 Set_Use
(Visible_Declarations
(Spec
));
10071 if In_Body
or else Is_RTU
(Par
, Ada_Tags
) then
10072 Install_Private_Declarations
(Par
);
10073 Set_Use
(Private_Declarations
(Spec
));
10077 -- Start of processing for Install_Parent
10080 -- We need to install the parent instance to compile the instantiation
10081 -- of the child, but the child instance must appear in the current
10082 -- scope. Given that we cannot place the parent above the current scope
10083 -- in the scope stack, we duplicate the current scope and unstack both
10084 -- after the instantiation is complete.
10086 -- If the parent is itself the instantiation of a child unit, we must
10087 -- also stack the instantiation of its parent, and so on. Each such
10088 -- ancestor is the prefix of the name in a prior instantiation.
10090 -- If this is a nested instance, the parent unit itself resolves to
10091 -- a renaming of the parent instance, whose declaration we need.
10093 -- Finally, the parent may be a generic (not an instance) when the
10094 -- child unit appears as a formal package.
10098 if Present
(Renamed_Entity
(Inst_Par
)) then
10099 Inst_Par
:= Renamed_Entity
(Inst_Par
);
10102 First_Par
:= Inst_Par
;
10104 Gen_Par
:= Generic_Parent
(Package_Specification
(Inst_Par
));
10106 First_Gen
:= Gen_Par
;
10108 while Present
(Gen_Par
) and then Is_Child_Unit
(Gen_Par
) loop
10110 -- Load grandparent instance as well
10112 Inst_Node
:= Get_Unit_Instantiation_Node
(Inst_Par
);
10114 if Nkind
(Name
(Inst_Node
)) = N_Expanded_Name
then
10115 Inst_Par
:= Entity
(Prefix
(Name
(Inst_Node
)));
10117 if Present
(Renamed_Entity
(Inst_Par
)) then
10118 Inst_Par
:= Renamed_Entity
(Inst_Par
);
10121 Gen_Par
:= Generic_Parent
(Package_Specification
(Inst_Par
));
10123 if Present
(Gen_Par
) then
10124 Prepend_Elmt
(Inst_Par
, Ancestors
);
10127 -- Parent is not the name of an instantiation
10129 Install_Noninstance_Specs
(Inst_Par
);
10140 if Present
(First_Gen
) then
10141 Append_Elmt
(First_Par
, Ancestors
);
10143 Install_Noninstance_Specs
(First_Par
);
10146 if not Is_Empty_Elmt_List
(Ancestors
) then
10147 Elmt
:= First_Elmt
(Ancestors
);
10148 while Present
(Elmt
) loop
10149 Install_Spec
(Node
(Elmt
));
10150 Install_Formal_Packages
(Node
(Elmt
));
10155 if not In_Body
then
10158 end Install_Parent
;
10160 -------------------------------
10161 -- Install_Hidden_Primitives --
10162 -------------------------------
10164 procedure Install_Hidden_Primitives
10165 (Prims_List
: in out Elist_Id
;
10170 List
: Elist_Id
:= No_Elist
;
10171 Prim_G_Elmt
: Elmt_Id
;
10172 Prim_A_Elmt
: Elmt_Id
;
10177 -- No action needed in case of serious errors because we cannot trust
10178 -- in the order of primitives
10180 if Serious_Errors_Detected
> 0 then
10183 -- No action possible if we don't have available the list of primitive
10187 or else not Is_Record_Type
(Gen_T
)
10188 or else not Is_Tagged_Type
(Gen_T
)
10189 or else not Is_Record_Type
(Act_T
)
10190 or else not Is_Tagged_Type
(Act_T
)
10194 -- There is no need to handle interface types since their primitives
10195 -- cannot be hidden
10197 elsif Is_Interface
(Gen_T
) then
10201 Prim_G_Elmt
:= First_Elmt
(Primitive_Operations
(Gen_T
));
10203 if not Is_Class_Wide_Type
(Act_T
) then
10204 Prim_A_Elmt
:= First_Elmt
(Primitive_Operations
(Act_T
));
10206 Prim_A_Elmt
:= First_Elmt
(Primitive_Operations
(Root_Type
(Act_T
)));
10210 -- Skip predefined primitives in the generic formal
10212 while Present
(Prim_G_Elmt
)
10213 and then Is_Predefined_Dispatching_Operation
(Node
(Prim_G_Elmt
))
10215 Next_Elmt
(Prim_G_Elmt
);
10218 -- Skip predefined primitives in the generic actual
10220 while Present
(Prim_A_Elmt
)
10221 and then Is_Predefined_Dispatching_Operation
(Node
(Prim_A_Elmt
))
10223 Next_Elmt
(Prim_A_Elmt
);
10226 exit when No
(Prim_G_Elmt
) or else No
(Prim_A_Elmt
);
10228 Prim_G
:= Node
(Prim_G_Elmt
);
10229 Prim_A
:= Node
(Prim_A_Elmt
);
10231 -- There is no need to handle interface primitives because their
10232 -- primitives are not hidden
10234 exit when Present
(Interface_Alias
(Prim_G
));
10236 -- Here we install one hidden primitive
10238 if Chars
(Prim_G
) /= Chars
(Prim_A
)
10239 and then Has_Suffix
(Prim_A
, 'P')
10240 and then Remove_Suffix
(Prim_A
, 'P') = Chars
(Prim_G
)
10242 Set_Chars
(Prim_A
, Chars
(Prim_G
));
10243 Append_New_Elmt
(Prim_A
, To
=> List
);
10246 Next_Elmt
(Prim_A_Elmt
);
10247 Next_Elmt
(Prim_G_Elmt
);
10250 -- Append the elements to the list of temporarily visible primitives
10251 -- avoiding duplicates.
10253 if Present
(List
) then
10254 if No
(Prims_List
) then
10255 Prims_List
:= New_Elmt_List
;
10258 Elmt
:= First_Elmt
(List
);
10259 while Present
(Elmt
) loop
10260 Append_Unique_Elmt
(Node
(Elmt
), Prims_List
);
10264 end Install_Hidden_Primitives
;
10266 -------------------------------
10267 -- Restore_Hidden_Primitives --
10268 -------------------------------
10270 procedure Restore_Hidden_Primitives
(Prims_List
: in out Elist_Id
) is
10271 Prim_Elmt
: Elmt_Id
;
10275 if Present
(Prims_List
) then
10276 Prim_Elmt
:= First_Elmt
(Prims_List
);
10277 while Present
(Prim_Elmt
) loop
10278 Prim
:= Node
(Prim_Elmt
);
10279 Set_Chars
(Prim
, Add_Suffix
(Prim
, 'P'));
10280 Next_Elmt
(Prim_Elmt
);
10283 Prims_List
:= No_Elist
;
10285 end Restore_Hidden_Primitives
;
10287 --------------------------------
10288 -- Instantiate_Formal_Package --
10289 --------------------------------
10291 function Instantiate_Formal_Package
10294 Analyzed_Formal
: Node_Id
) return List_Id
10296 Loc
: constant Source_Ptr
:= Sloc
(Actual
);
10297 Hidden_Formals
: constant Elist_Id
:= New_Elmt_List
;
10299 Actual_Pack
: Entity_Id
;
10300 Formal_Pack
: Entity_Id
;
10301 Gen_Parent
: Entity_Id
;
10304 Parent_Spec
: Node_Id
;
10306 procedure Find_Matching_Actual
10308 Act
: in out Entity_Id
);
10309 -- We need to associate each formal entity in the formal package with
10310 -- the corresponding entity in the actual package. The actual package
10311 -- has been analyzed and possibly expanded, and as a result there is
10312 -- no one-to-one correspondence between the two lists (for example,
10313 -- the actual may include subtypes, itypes, and inherited primitive
10314 -- operations, interspersed among the renaming declarations for the
10315 -- actuals). We retrieve the corresponding actual by name because each
10316 -- actual has the same name as the formal, and they do appear in the
10319 function Get_Formal_Entity
(N
: Node_Id
) return Entity_Id
;
10320 -- Retrieve entity of defining entity of generic formal parameter.
10321 -- Only the declarations of formals need to be considered when
10322 -- linking them to actuals, but the declarative list may include
10323 -- internal entities generated during analysis, and those are ignored.
10325 procedure Match_Formal_Entity
10326 (Formal_Node
: Node_Id
;
10327 Formal_Ent
: Entity_Id
;
10328 Actual_Ent
: Entity_Id
);
10329 -- Associates the formal entity with the actual. In the case where
10330 -- Formal_Ent is a formal package, this procedure iterates through all
10331 -- of its formals and enters associations between the actuals occurring
10332 -- in the formal package's corresponding actual package (given by
10333 -- Actual_Ent) and the formal package's formal parameters. This
10334 -- procedure recurses if any of the parameters is itself a package.
10336 function Is_Instance_Of
10337 (Act_Spec
: Entity_Id
;
10338 Gen_Anc
: Entity_Id
) return Boolean;
10339 -- The actual can be an instantiation of a generic within another
10340 -- instance, in which case there is no direct link from it to the
10341 -- original generic ancestor. In that case, we recognize that the
10342 -- ultimate ancestor is the same by examining names and scopes.
10344 procedure Process_Nested_Formal
(Formal
: Entity_Id
);
10345 -- If the current formal is declared with a box, its own formals are
10346 -- visible in the instance, as they were in the generic, and their
10347 -- Hidden flag must be reset. If some of these formals are themselves
10348 -- packages declared with a box, the processing must be recursive.
10350 --------------------------
10351 -- Find_Matching_Actual --
10352 --------------------------
10354 procedure Find_Matching_Actual
10356 Act
: in out Entity_Id
)
10358 Formal_Ent
: Entity_Id
;
10361 case Nkind
(Original_Node
(F
)) is
10362 when N_Formal_Object_Declaration
10363 | N_Formal_Type_Declaration
10365 Formal_Ent
:= Defining_Identifier
(F
);
10367 while Present
(Act
)
10368 and then Chars
(Act
) /= Chars
(Formal_Ent
)
10373 when N_Formal_Package_Declaration
10374 | N_Formal_Subprogram_Declaration
10375 | N_Generic_Package_Declaration
10376 | N_Package_Declaration
10378 Formal_Ent
:= Defining_Entity
(F
);
10380 while Present
(Act
)
10381 and then Chars
(Act
) /= Chars
(Formal_Ent
)
10387 raise Program_Error
;
10389 end Find_Matching_Actual
;
10391 -------------------------
10392 -- Match_Formal_Entity --
10393 -------------------------
10395 procedure Match_Formal_Entity
10396 (Formal_Node
: Node_Id
;
10397 Formal_Ent
: Entity_Id
;
10398 Actual_Ent
: Entity_Id
)
10400 Act_Pkg
: Entity_Id
;
10403 Set_Instance_Of
(Formal_Ent
, Actual_Ent
);
10405 if Ekind
(Actual_Ent
) = E_Package
then
10407 -- Record associations for each parameter
10409 Act_Pkg
:= Actual_Ent
;
10412 A_Ent
: Entity_Id
:= First_Entity
(Act_Pkg
);
10416 Gen_Decl
: Node_Id
;
10418 Actual
: Entity_Id
;
10421 -- Retrieve the actual given in the formal package declaration
10423 Actual
:= Entity
(Name
(Original_Node
(Formal_Node
)));
10425 -- The actual in the formal package declaration may be a
10426 -- renamed generic package, in which case we want to retrieve
10427 -- the original generic in order to traverse its formal part.
10429 if Present
(Renamed_Entity
(Actual
)) then
10430 Gen_Decl
:= Unit_Declaration_Node
(Renamed_Entity
(Actual
));
10432 Gen_Decl
:= Unit_Declaration_Node
(Actual
);
10435 Formals
:= Generic_Formal_Declarations
(Gen_Decl
);
10437 if Present
(Formals
) then
10438 F_Node
:= First_Non_Pragma
(Formals
);
10443 while Present
(A_Ent
)
10444 and then Present
(F_Node
)
10445 and then A_Ent
/= First_Private_Entity
(Act_Pkg
)
10447 F_Ent
:= Get_Formal_Entity
(F_Node
);
10449 if Present
(F_Ent
) then
10451 -- This is a formal of the original package. Record
10452 -- association and recurse.
10454 Find_Matching_Actual
(F_Node
, A_Ent
);
10455 Match_Formal_Entity
(F_Node
, F_Ent
, A_Ent
);
10456 Next_Entity
(A_Ent
);
10459 Next_Non_Pragma
(F_Node
);
10463 end Match_Formal_Entity
;
10465 -----------------------
10466 -- Get_Formal_Entity --
10467 -----------------------
10469 function Get_Formal_Entity
(N
: Node_Id
) return Entity_Id
is
10470 Kind
: constant Node_Kind
:= Nkind
(Original_Node
(N
));
10473 when N_Formal_Object_Declaration
=>
10474 return Defining_Identifier
(N
);
10476 when N_Formal_Type_Declaration
=>
10477 return Defining_Identifier
(N
);
10479 when N_Formal_Subprogram_Declaration
=>
10480 return Defining_Unit_Name
(Specification
(N
));
10482 when N_Formal_Package_Declaration
=>
10483 return Defining_Identifier
(Original_Node
(N
));
10485 when N_Generic_Package_Declaration
=>
10486 return Defining_Identifier
(Original_Node
(N
));
10488 -- All other declarations are introduced by semantic analysis and
10489 -- have no match in the actual.
10494 end Get_Formal_Entity
;
10496 --------------------
10497 -- Is_Instance_Of --
10498 --------------------
10500 function Is_Instance_Of
10501 (Act_Spec
: Entity_Id
;
10502 Gen_Anc
: Entity_Id
) return Boolean
10504 Gen_Par
: constant Entity_Id
:= Generic_Parent
(Act_Spec
);
10507 if No
(Gen_Par
) then
10510 -- Simplest case: the generic parent of the actual is the formal
10512 elsif Gen_Par
= Gen_Anc
then
10515 elsif Chars
(Gen_Par
) /= Chars
(Gen_Anc
) then
10518 -- The actual may be obtained through several instantiations. Its
10519 -- scope must itself be an instance of a generic declared in the
10520 -- same scope as the formal. Any other case is detected above.
10522 elsif not Is_Generic_Instance
(Scope
(Gen_Par
)) then
10526 return Generic_Parent
(Parent
(Scope
(Gen_Par
))) = Scope
(Gen_Anc
);
10528 end Is_Instance_Of
;
10530 ---------------------------
10531 -- Process_Nested_Formal --
10532 ---------------------------
10534 procedure Process_Nested_Formal
(Formal
: Entity_Id
) is
10538 if Present
(Associated_Formal_Package
(Formal
))
10539 and then Box_Present
(Parent
(Associated_Formal_Package
(Formal
)))
10541 Ent
:= First_Entity
(Formal
);
10542 while Present
(Ent
) loop
10543 Set_Is_Hidden
(Ent
, False);
10544 Set_Is_Visible_Formal
(Ent
);
10545 Set_Is_Potentially_Use_Visible
10546 (Ent
, Is_Potentially_Use_Visible
(Formal
));
10548 if Ekind
(Ent
) = E_Package
then
10549 exit when Renamed_Entity
(Ent
) = Renamed_Entity
(Formal
);
10550 Process_Nested_Formal
(Ent
);
10556 end Process_Nested_Formal
;
10558 -- Start of processing for Instantiate_Formal_Package
10563 -- The actual must be a package instance, or else a current instance
10564 -- such as a parent generic within the body of a generic child.
10566 if not Is_Entity_Name
(Actual
)
10567 or else not Is_Package_Or_Generic_Package
(Entity
(Actual
))
10570 ("expect package instance to instantiate formal", Actual
);
10571 Abandon_Instantiation
(Actual
);
10572 raise Program_Error
;
10575 Actual_Pack
:= Entity
(Actual
);
10576 Set_Is_Instantiated
(Actual_Pack
);
10578 -- The actual may be a renamed package, or an outer generic formal
10579 -- package whose instantiation is converted into a renaming.
10581 if Present
(Renamed_Entity
(Actual_Pack
)) then
10582 Actual_Pack
:= Renamed_Entity
(Actual_Pack
);
10585 -- The analyzed formal is expected to be the result of the rewriting
10586 -- of the formal package into a regular package by analysis.
10588 pragma Assert
(Nkind
(Analyzed_Formal
) = N_Package_Declaration
10589 and then Nkind
(Original_Node
(Analyzed_Formal
)) =
10590 N_Formal_Package_Declaration
);
10592 Gen_Parent
:= Generic_Parent
(Specification
(Analyzed_Formal
));
10593 Formal_Pack
:= Defining_Unit_Name
(Specification
(Analyzed_Formal
));
10595 if Nkind
(Parent
(Actual_Pack
)) = N_Defining_Program_Unit_Name
then
10596 Parent_Spec
:= Package_Specification
(Actual_Pack
);
10598 Parent_Spec
:= Parent
(Actual_Pack
);
10601 if Gen_Parent
= Any_Id
then
10603 ("previous error in declaration of formal package", Actual
);
10604 Abandon_Instantiation
(Actual
);
10606 elsif Is_Instance_Of
(Parent_Spec
, Get_Instance_Of
(Gen_Parent
)) then
10609 -- If this is the current instance of an enclosing generic, that unit
10610 -- is the generic package we need.
10612 elsif In_Open_Scopes
(Actual_Pack
)
10613 and then Ekind
(Actual_Pack
) = E_Generic_Package
10619 ("actual parameter must be instance of&", Actual
, Gen_Parent
);
10620 Abandon_Instantiation
(Actual
);
10623 Set_Instance_Of
(Defining_Identifier
(Formal
), Actual_Pack
);
10624 Map_Formal_Package_Entities
(Formal_Pack
, Actual_Pack
);
10627 Make_Package_Renaming_Declaration
(Loc
,
10628 Defining_Unit_Name
=> New_Copy
(Defining_Identifier
(Formal
)),
10629 Name
=> New_Occurrence_Of
(Actual_Pack
, Loc
));
10631 Set_Associated_Formal_Package
10632 (Defining_Unit_Name
(Nod
), Defining_Identifier
(Formal
));
10633 Decls
:= New_List
(Nod
);
10635 -- If the formal F has a box, then the generic declarations are
10636 -- visible in the generic G. In an instance of G, the corresponding
10637 -- entities in the actual for F (which are the actuals for the
10638 -- instantiation of the generic that F denotes) must also be made
10639 -- visible for analysis of the current instance. On exit from the
10640 -- current instance, those entities are made private again. If the
10641 -- actual is currently in use, these entities are also use-visible.
10643 -- The loop through the actual entities also steps through the formal
10644 -- entities and enters associations from formals to actuals into the
10645 -- renaming map. This is necessary to properly handle checking of
10646 -- actual parameter associations for later formals that depend on
10647 -- actuals declared in the formal package.
10649 -- In Ada 2005, partial parameterization requires that we make
10650 -- visible the actuals corresponding to formals that were defaulted
10651 -- in the formal package. There formals are identified because they
10652 -- remain formal generics within the formal package, rather than
10653 -- being renamings of the actuals supplied.
10656 Gen_Decl
: constant Node_Id
:=
10657 Unit_Declaration_Node
(Gen_Parent
);
10658 Formals
: constant List_Id
:=
10659 Generic_Formal_Declarations
(Gen_Decl
);
10661 Actual_Ent
: Entity_Id
;
10662 Actual_Of_Formal
: Node_Id
;
10663 Formal_Node
: Node_Id
;
10664 Formal_Ent
: Entity_Id
;
10667 if Present
(Formals
) then
10668 Formal_Node
:= First_Non_Pragma
(Formals
);
10670 Formal_Node
:= Empty
;
10673 Actual_Ent
:= First_Entity
(Actual_Pack
);
10674 Actual_Of_Formal
:=
10675 First
(Visible_Declarations
(Specification
(Analyzed_Formal
)));
10676 while Present
(Actual_Ent
)
10677 and then Actual_Ent
/= First_Private_Entity
(Actual_Pack
)
10679 if Present
(Formal_Node
) then
10680 Formal_Ent
:= Get_Formal_Entity
(Formal_Node
);
10682 if Present
(Formal_Ent
) then
10683 Find_Matching_Actual
(Formal_Node
, Actual_Ent
);
10684 Match_Formal_Entity
(Formal_Node
, Formal_Ent
, Actual_Ent
);
10686 -- We iterate at the same time over the actuals of the
10687 -- local package created for the formal, to determine
10688 -- which one of the formals of the original generic were
10689 -- defaulted in the formal. The corresponding actual
10690 -- entities are visible in the enclosing instance.
10692 if Box_Present
(Formal
)
10694 (Present
(Actual_Of_Formal
)
10697 (Get_Formal_Entity
(Actual_Of_Formal
)))
10699 Set_Is_Hidden
(Actual_Ent
, False);
10700 Set_Is_Visible_Formal
(Actual_Ent
);
10701 Set_Is_Potentially_Use_Visible
10702 (Actual_Ent
, In_Use
(Actual_Pack
));
10704 if Ekind
(Actual_Ent
) = E_Package
then
10705 Process_Nested_Formal
(Actual_Ent
);
10709 if not Is_Hidden
(Actual_Ent
) then
10710 Append_Elmt
(Actual_Ent
, Hidden_Formals
);
10713 Set_Is_Hidden
(Actual_Ent
);
10714 Set_Is_Potentially_Use_Visible
(Actual_Ent
, False);
10718 Next_Non_Pragma
(Formal_Node
);
10719 Next
(Actual_Of_Formal
);
10721 -- A formal subprogram may be overloaded, so advance in
10722 -- the list of actuals to make sure we do not match two
10723 -- successive formals to the same actual. This is only
10724 -- relevant for overloadable entities, others have
10727 if Is_Overloadable
(Actual_Ent
) then
10728 Next_Entity
(Actual_Ent
);
10732 -- No further formals to match, but the generic part may
10733 -- contain inherited operation that are not hidden in the
10734 -- enclosing instance.
10736 Next_Entity
(Actual_Ent
);
10740 -- Inherited subprograms generated by formal derived types are
10741 -- also visible if the types are.
10743 Actual_Ent
:= First_Entity
(Actual_Pack
);
10744 while Present
(Actual_Ent
)
10745 and then Actual_Ent
/= First_Private_Entity
(Actual_Pack
)
10747 if Is_Overloadable
(Actual_Ent
)
10749 Nkind
(Parent
(Actual_Ent
)) = N_Subtype_Declaration
10751 not Is_Hidden
(Defining_Identifier
(Parent
(Actual_Ent
)))
10753 Set_Is_Hidden
(Actual_Ent
, False);
10754 Set_Is_Potentially_Use_Visible
10755 (Actual_Ent
, In_Use
(Actual_Pack
));
10758 Next_Entity
(Actual_Ent
);
10762 -- If the formal requires conformance checking, reanalyze it as an
10763 -- abbreviated instantiation, to verify the matching rules of 12.7.
10764 -- The actual checks are performed after the generic associations
10765 -- have been analyzed, to guarantee the same visibility for this
10766 -- instantiation and for the actuals.
10768 -- In Ada 2005, the generic associations for the formal can include
10769 -- defaulted parameters. These are ignored during check. This
10770 -- internal instantiation is removed from the tree after conformance
10771 -- checking, because it contains formal declarations for those
10772 -- defaulted parameters, and those should not reach the back-end.
10774 if Requires_Conformance_Checking
(Formal
) then
10776 I_Pack
: constant Entity_Id
:= Make_Temporary
(Loc
, 'P');
10781 Set_Is_Internal
(I_Pack
);
10782 Mutate_Ekind
(I_Pack
, E_Package
);
10784 -- Insert the package into the list of its hidden entities so
10785 -- that the list is not empty for Is_Abbreviated_Instance.
10787 Append_Elmt
(I_Pack
, Hidden_Formals
);
10789 Set_Hidden_In_Formal_Instance
(I_Pack
, Hidden_Formals
);
10791 -- If the generic is a child unit, Check_Generic_Child_Unit
10792 -- needs its original name in case it is qualified.
10794 if Is_Child_Unit
(Gen_Parent
) then
10796 New_Copy_Tree
(Name
(Original_Node
(Analyzed_Formal
)));
10797 pragma Assert
(Entity
(I_Nam
) = Gen_Parent
);
10801 New_Occurrence_Of
(Get_Instance_Of
(Gen_Parent
), Loc
);
10805 Make_Package_Instantiation
(Loc
,
10806 Defining_Unit_Name
=> I_Pack
,
10808 Generic_Associations
=> Generic_Associations
(Formal
)));
10814 end Instantiate_Formal_Package
;
10816 -----------------------------------
10817 -- Instantiate_Formal_Subprogram --
10818 -----------------------------------
10820 function Instantiate_Formal_Subprogram
10823 Analyzed_Formal
: Node_Id
) return Node_Id
10825 Analyzed_S
: constant Entity_Id
:=
10826 Defining_Unit_Name
(Specification
(Analyzed_Formal
));
10827 Formal_Sub
: constant Entity_Id
:=
10828 Defining_Unit_Name
(Specification
(Formal
));
10830 function From_Parent_Scope
(Subp
: Entity_Id
) return Boolean;
10831 -- If the generic is a child unit, the parent has been installed on the
10832 -- scope stack, but a default subprogram cannot resolve to something
10833 -- on the parent because that parent is not really part of the visible
10834 -- context (it is there to resolve explicit local entities). If the
10835 -- default has resolved in this way, we remove the entity from immediate
10836 -- visibility and analyze the node again to emit an error message or
10837 -- find another visible candidate.
10839 procedure Valid_Actual_Subprogram
(Act
: Node_Id
);
10840 -- Perform legality check and raise exception on failure
10842 -----------------------
10843 -- From_Parent_Scope --
10844 -----------------------
10846 function From_Parent_Scope
(Subp
: Entity_Id
) return Boolean is
10847 Gen_Scope
: Node_Id
;
10850 Gen_Scope
:= Scope
(Analyzed_S
);
10851 while Present
(Gen_Scope
) and then Is_Child_Unit
(Gen_Scope
) loop
10852 if Scope
(Subp
) = Scope
(Gen_Scope
) then
10856 Gen_Scope
:= Scope
(Gen_Scope
);
10860 end From_Parent_Scope
;
10862 -----------------------------
10863 -- Valid_Actual_Subprogram --
10864 -----------------------------
10866 procedure Valid_Actual_Subprogram
(Act
: Node_Id
) is
10870 if Is_Entity_Name
(Act
) then
10871 Act_E
:= Entity
(Act
);
10873 elsif Nkind
(Act
) = N_Selected_Component
10874 and then Is_Entity_Name
(Selector_Name
(Act
))
10876 Act_E
:= Entity
(Selector_Name
(Act
));
10882 if (Present
(Act_E
) and then Is_Overloadable
(Act_E
))
10883 or else Nkind
(Act
) in N_Attribute_Reference
10884 | N_Indexed_Component
10885 | N_Character_Literal
10886 | N_Explicit_Dereference
10892 ("expect subprogram or entry name in instantiation of &",
10893 Instantiation_Node
, Formal_Sub
);
10894 Abandon_Instantiation
(Instantiation_Node
);
10895 end Valid_Actual_Subprogram
;
10899 Decl_Node
: Node_Id
;
10902 New_Spec
: Node_Id
;
10903 New_Subp
: Entity_Id
;
10905 -- Start of processing for Instantiate_Formal_Subprogram
10908 New_Spec
:= New_Copy_Tree
(Specification
(Formal
));
10910 -- The tree copy has created the proper instantiation sloc for the
10911 -- new specification. Use this location for all other constructed
10914 Loc
:= Sloc
(Defining_Unit_Name
(New_Spec
));
10916 -- Create new entity for the actual (New_Copy_Tree does not), and
10917 -- indicate that it is an actual.
10919 -- If the actual is not an entity (i.e. an attribute reference)
10920 -- and the formal includes aspect specifications for contracts,
10921 -- we create an internal name for the renaming declaration. The
10922 -- constructed wrapper contains a call to the entity in the renaming.
10923 -- This is an expansion activity, as is the wrapper creation.
10925 if Ada_Version
>= Ada_2022
10926 and then Has_Contracts
(Analyzed_Formal
)
10927 and then not Is_Entity_Name
(Actual
)
10928 and then Expander_Active
10930 New_Subp
:= Make_Temporary
(Sloc
(Actual
), 'S');
10932 New_Subp
:= Make_Defining_Identifier
(Loc
, Chars
(Formal_Sub
));
10935 Mutate_Ekind
(New_Subp
, Ekind
(Analyzed_S
));
10936 Set_Is_Generic_Actual_Subprogram
(New_Subp
);
10937 Set_Defining_Unit_Name
(New_Spec
, New_Subp
);
10939 -- Create new entities for the each of the formals in the specification
10940 -- of the renaming declaration built for the actual.
10942 if Present
(Parameter_Specifications
(New_Spec
)) then
10948 F
:= First
(Parameter_Specifications
(New_Spec
));
10949 while Present
(F
) loop
10950 F_Id
:= Defining_Identifier
(F
);
10952 Set_Defining_Identifier
(F
,
10953 Make_Defining_Identifier
(Sloc
(F_Id
), Chars
(F_Id
)));
10959 -- Find entity of actual. If the actual is an attribute reference, it
10960 -- cannot be resolved here (its formal is missing) but is handled
10961 -- instead in Attribute_Renaming. If the actual is overloaded, it is
10962 -- fully resolved subsequently, when the renaming declaration for the
10963 -- formal is analyzed. If it is an explicit dereference, resolve the
10964 -- prefix but not the actual itself, to prevent interpretation as call.
10966 if Present
(Actual
) then
10967 Loc
:= Sloc
(Actual
);
10968 Set_Sloc
(New_Spec
, Loc
);
10970 if Nkind
(Actual
) = N_Operator_Symbol
then
10971 Find_Direct_Name
(Actual
);
10973 elsif Nkind
(Actual
) = N_Explicit_Dereference
then
10974 Analyze
(Prefix
(Actual
));
10976 elsif Nkind
(Actual
) /= N_Attribute_Reference
then
10980 Valid_Actual_Subprogram
(Actual
);
10983 elsif Present
(Default_Name
(Formal
)) then
10984 if Nkind
(Default_Name
(Formal
)) not in N_Attribute_Reference
10985 | N_Selected_Component
10986 | N_Indexed_Component
10987 | N_Character_Literal
10988 and then Present
(Entity
(Default_Name
(Formal
)))
10990 Nam
:= New_Occurrence_Of
(Entity
(Default_Name
(Formal
)), Loc
);
10992 Nam
:= New_Copy
(Default_Name
(Formal
));
10993 Set_Sloc
(Nam
, Loc
);
10996 elsif Box_Present
(Formal
) then
10998 -- Actual is resolved at the point of instantiation. Create an
10999 -- identifier or operator with the same name as the formal.
11001 if Nkind
(Formal_Sub
) = N_Defining_Operator_Symbol
then
11003 Make_Operator_Symbol
(Loc
,
11004 Chars
=> Chars
(Formal_Sub
),
11005 Strval
=> No_String
);
11007 Nam
:= Make_Identifier
(Loc
, Chars
(Formal_Sub
));
11010 elsif Nkind
(Specification
(Formal
)) = N_Procedure_Specification
11011 and then Null_Present
(Specification
(Formal
))
11013 -- Generate null body for procedure, for use in the instance
11016 Make_Subprogram_Body
(Loc
,
11017 Specification
=> New_Spec
,
11018 Declarations
=> New_List
,
11019 Handled_Statement_Sequence
=>
11020 Make_Handled_Sequence_Of_Statements
(Loc
,
11021 Statements
=> New_List
(Make_Null_Statement
(Loc
))));
11023 -- RM 12.6 (16.2/2): The procedure has convention Intrinsic
11025 Set_Convention
(Defining_Unit_Name
(New_Spec
), Convention_Intrinsic
);
11027 -- Eliminate the calls to it when optimization is enabled
11029 Set_Is_Inlined
(Defining_Unit_Name
(New_Spec
));
11032 -- Handle case of a formal function with an expression default (allowed
11033 -- when extensions are enabled).
11035 elsif Nkind
(Specification
(Formal
)) = N_Function_Specification
11036 and then Present
(Expression
(Formal
))
11038 -- Generate body for function, for use in the instance
11041 Expr
: constant Node_Id
:= New_Copy
(Expression
(Formal
));
11042 Stmt
: constant Node_Id
:= Make_Simple_Return_Statement
(Loc
);
11044 Set_Sloc
(Expr
, Loc
);
11045 Set_Expression
(Stmt
, Expr
);
11048 Make_Subprogram_Body
(Loc
,
11049 Specification
=> New_Spec
,
11050 Declarations
=> New_List
,
11051 Handled_Statement_Sequence
=>
11052 Make_Handled_Sequence_Of_Statements
(Loc
,
11053 Statements
=> New_List
(Stmt
)));
11056 -- RM 12.6 (16.2/2): Like a null procedure default, the function
11057 -- has convention Intrinsic.
11059 Set_Convention
(Defining_Unit_Name
(New_Spec
), Convention_Intrinsic
);
11061 -- Inline calls to it when optimization is enabled
11063 Set_Is_Inlined
(Defining_Unit_Name
(New_Spec
));
11067 Error_Msg_Sloc
:= Sloc
(Scope
(Analyzed_S
));
11069 ("missing actual&", Instantiation_Node
, Formal_Sub
);
11071 ("\in instantiation of & declared#",
11072 Instantiation_Node
, Scope
(Analyzed_S
));
11073 Abandon_Instantiation
(Instantiation_Node
);
11077 Make_Subprogram_Renaming_Declaration
(Loc
,
11078 Specification
=> New_Spec
,
11081 -- If we do not have an actual and the formal specified <> then set to
11082 -- get proper default.
11084 if No
(Actual
) and then Box_Present
(Formal
) then
11085 Set_From_Default
(Decl_Node
);
11088 -- Gather possible interpretations for the actual before analyzing the
11089 -- instance. If overloaded, it will be resolved when analyzing the
11090 -- renaming declaration.
11092 if Box_Present
(Formal
) and then No
(Actual
) then
11095 if Is_Child_Unit
(Scope
(Analyzed_S
))
11096 and then Present
(Entity
(Nam
))
11098 if not Is_Overloaded
(Nam
) then
11099 if From_Parent_Scope
(Entity
(Nam
)) then
11100 Set_Is_Immediately_Visible
(Entity
(Nam
), False);
11101 Set_Entity
(Nam
, Empty
);
11102 Set_Etype
(Nam
, Empty
);
11105 Set_Is_Immediately_Visible
(Entity
(Nam
));
11114 Get_First_Interp
(Nam
, I
, It
);
11115 while Present
(It
.Nam
) loop
11116 if From_Parent_Scope
(It
.Nam
) then
11120 Get_Next_Interp
(I
, It
);
11127 -- The generic instantiation freezes the actual. This can only be done
11128 -- once the actual is resolved, in the analysis of the renaming
11129 -- declaration. To make the formal subprogram entity available, we set
11130 -- Corresponding_Formal_Spec to point to the formal subprogram entity.
11131 -- This is also needed in Analyze_Subprogram_Renaming for the processing
11132 -- of formal abstract subprograms.
11134 Set_Corresponding_Formal_Spec
(Decl_Node
, Analyzed_S
);
11136 -- We cannot analyze the renaming declaration, and thus find the actual,
11137 -- until all the actuals are assembled in the instance. For subsequent
11138 -- checks of other actuals, indicate the node that will hold the
11139 -- instance of this formal.
11141 Set_Instance_Of
(Analyzed_S
, Nam
);
11143 if Nkind
(Actual
) = N_Selected_Component
11144 and then Is_Task_Type
(Etype
(Prefix
(Actual
)))
11145 and then not Is_Frozen
(Etype
(Prefix
(Actual
)))
11147 -- The renaming declaration will create a body, which must appear
11148 -- outside of the instantiation, We move the renaming declaration
11149 -- out of the instance, and create an additional renaming inside,
11150 -- to prevent freezing anomalies.
11153 Anon_Id
: constant Entity_Id
:= Make_Temporary
(Loc
, 'E');
11156 Set_Defining_Unit_Name
(New_Spec
, Anon_Id
);
11157 Insert_Before
(Instantiation_Node
, Decl_Node
);
11158 Analyze
(Decl_Node
);
11160 -- Now create renaming within the instance
11163 Make_Subprogram_Renaming_Declaration
(Loc
,
11164 Specification
=> New_Copy_Tree
(New_Spec
),
11165 Name
=> New_Occurrence_Of
(Anon_Id
, Loc
));
11167 Set_Defining_Unit_Name
(Specification
(Decl_Node
),
11168 Make_Defining_Identifier
(Loc
, Chars
(Formal_Sub
)));
11173 end Instantiate_Formal_Subprogram
;
11175 ------------------------
11176 -- Instantiate_Object --
11177 ------------------------
11179 function Instantiate_Object
11182 Analyzed_Formal
: Node_Id
) return List_Id
11184 Gen_Obj
: constant Entity_Id
:= Defining_Identifier
(Formal
);
11185 A_Gen_Obj
: constant Entity_Id
:=
11186 Defining_Identifier
(Analyzed_Formal
);
11187 Acc_Def
: Node_Id
:= Empty
;
11188 Act_Assoc
: constant Node_Id
:=
11189 (if No
(Actual
) then Empty
else Parent
(Actual
));
11190 Actual_Decl
: Node_Id
:= Empty
;
11191 Decl_Node
: Node_Id
;
11194 List
: constant List_Id
:= New_List
;
11195 Loc
: constant Source_Ptr
:= Sloc
(Actual
);
11196 Orig_Ftyp
: constant Entity_Id
:= Etype
(A_Gen_Obj
);
11197 Subt_Decl
: Node_Id
:= Empty
;
11198 Subt_Mark
: Node_Id
:= Empty
;
11200 -- Start of processing for Instantiate_Object
11203 -- Formal may be an anonymous access
11205 if Present
(Subtype_Mark
(Formal
)) then
11206 Subt_Mark
:= Subtype_Mark
(Formal
);
11208 Check_Access_Definition
(Formal
);
11209 Acc_Def
:= Access_Definition
(Formal
);
11212 -- Sloc for error message on missing actual
11214 Error_Msg_Sloc
:= Sloc
(Scope
(A_Gen_Obj
));
11216 if Get_Instance_Of
(Gen_Obj
) /= Gen_Obj
then
11217 Error_Msg_N
("duplicate instantiation of generic parameter", Actual
);
11220 Set_Parent
(List
, Act_Assoc
);
11224 if Out_Present
(Formal
) then
11226 -- An IN OUT generic actual must be a name. The instantiation is a
11227 -- renaming declaration. The actual is the name being renamed. We
11228 -- use the actual directly, rather than a copy, because it is not
11229 -- used further in the list of actuals, and because a copy or a use
11230 -- of relocate_node is incorrect if the instance is nested within a
11231 -- generic. In order to simplify e.g. ASIS queries, the
11232 -- Generic_Parent field links the declaration to the generic
11235 if No
(Actual
) then
11237 ("missing actual &",
11238 Instantiation_Node
, Gen_Obj
);
11240 ("\in instantiation of & declared#",
11241 Instantiation_Node
, Scope
(A_Gen_Obj
));
11242 Abandon_Instantiation
(Instantiation_Node
);
11245 if Present
(Subt_Mark
) then
11247 Make_Object_Renaming_Declaration
(Loc
,
11248 Defining_Identifier
=> New_Copy
(Gen_Obj
),
11249 Subtype_Mark
=> New_Copy_Tree
(Subt_Mark
),
11252 else pragma Assert
(Present
(Acc_Def
));
11254 Make_Object_Renaming_Declaration
(Loc
,
11255 Defining_Identifier
=> New_Copy
(Gen_Obj
),
11256 Access_Definition
=> New_Copy_Tree
(Acc_Def
),
11260 Set_Corresponding_Generic_Association
(Decl_Node
, Act_Assoc
);
11262 -- The analysis of the actual may produce Insert_Action nodes, so
11263 -- the declaration must have a context in which to attach them.
11265 Append
(Decl_Node
, List
);
11268 -- Return if the analysis of the actual reported some error
11270 if Etype
(Actual
) = Any_Type
then
11274 -- This check is performed here because Analyze_Object_Renaming will
11275 -- not check it when Comes_From_Source is False. Note though that the
11276 -- check for the actual being the name of an object will be performed
11277 -- in Analyze_Object_Renaming.
11279 if Is_Object_Reference
(Actual
)
11280 and then Is_Dependent_Component_Of_Mutable_Object
(Actual
)
11283 ("illegal discriminant-dependent component for in out parameter",
11287 -- The actual has to be resolved in order to check that it is a
11288 -- variable (due to cases such as F (1), where F returns access to
11289 -- an array, and for overloaded prefixes).
11291 Ftyp
:= Get_Instance_Of
(Etype
(A_Gen_Obj
));
11293 -- If the type of the formal is not itself a formal, and the current
11294 -- unit is a child unit, the formal type must be declared in a
11295 -- parent, and must be retrieved by visibility.
11297 if Ftyp
= Orig_Ftyp
11298 and then Is_Generic_Unit
(Scope
(Ftyp
))
11299 and then Is_Child_Unit
(Scope
(A_Gen_Obj
))
11302 Temp
: constant Node_Id
:=
11303 New_Copy_Tree
(Subtype_Mark
(Analyzed_Formal
));
11305 Set_Entity
(Temp
, Empty
);
11307 Ftyp
:= Entity
(Temp
);
11311 if Is_Private_Type
(Ftyp
)
11312 and then not Is_Private_Type
(Etype
(Actual
))
11313 and then (Base_Type
(Full_View
(Ftyp
)) = Base_Type
(Etype
(Actual
))
11314 or else Base_Type
(Etype
(Actual
)) = Ftyp
)
11316 -- If the actual has the type of the full view of the formal, or
11317 -- else a non-private subtype of the formal, then the visibility
11318 -- of the formal type has changed. Add to the actuals a subtype
11319 -- declaration that will force the exchange of views in the body
11320 -- of the instance as well.
11323 Make_Subtype_Declaration
(Loc
,
11324 Defining_Identifier
=> Make_Temporary
(Loc
, 'P'),
11325 Subtype_Indication
=> New_Occurrence_Of
(Ftyp
, Loc
));
11327 Prepend
(Subt_Decl
, List
);
11329 Prepend_Elmt
(Full_View
(Ftyp
), Exchanged_Views
);
11330 Exchange_Declarations
(Ftyp
);
11333 Resolve
(Actual
, Ftyp
);
11335 if not Denotes_Variable
(Actual
) then
11336 Error_Msg_NE
("actual for& must be a variable", Actual
, Gen_Obj
);
11338 elsif Base_Type
(Ftyp
) /= Base_Type
(Etype
(Actual
)) then
11340 -- Ada 2005 (AI-423): For a generic formal object of mode in out,
11341 -- the type of the actual shall resolve to a specific anonymous
11344 if Ada_Version
< Ada_2005
11345 or else not Is_Anonymous_Access_Type
(Base_Type
(Ftyp
))
11346 or else not Is_Anonymous_Access_Type
(Base_Type
(Etype
(Actual
)))
11349 ("type of actual does not match type of&", Actual
, Gen_Obj
);
11353 Note_Possible_Modification
(Actual
, Sure
=> True);
11355 -- Check for instantiation with atomic/volatile/VFA object actual for
11356 -- nonatomic/nonvolatile/nonVFA formal (RM C.6 (12)).
11358 if Is_Atomic_Object
(Actual
) and then not Is_Atomic
(Orig_Ftyp
) then
11360 ("cannot instantiate nonatomic formal & of mode in out",
11362 Error_Msg_N
("\with atomic object actual (RM C.6(12))", Actual
);
11364 elsif Is_Volatile_Object_Ref
(Actual
)
11365 and then not Is_Volatile
(Orig_Ftyp
)
11368 ("cannot instantiate nonvolatile formal & of mode in out",
11370 Error_Msg_N
("\with volatile object actual (RM C.6(12))", Actual
);
11372 elsif Is_Volatile_Full_Access_Object_Ref
(Actual
)
11373 and then not Is_Volatile_Full_Access
(Orig_Ftyp
)
11376 ("cannot instantiate nonfull access formal & of mode in out",
11379 ("\with full access object actual (RM C.6(12))", Actual
);
11382 -- Check for instantiation on nonatomic subcomponent of a full access
11383 -- object in Ada 2022 (RM C.6 (12)).
11385 if Ada_Version
>= Ada_2022
11386 and then Is_Subcomponent_Of_Full_Access_Object
(Actual
)
11387 and then not Is_Atomic_Object
(Actual
)
11390 ("cannot instantiate formal & of mode in out with actual",
11393 ("\nonatomic subcomponent of full access object (RM C.6(12))",
11397 -- Check actual/formal compatibility with respect to the four
11398 -- volatility refinement aspects.
11401 Actual_Obj
: Entity_Id
;
11402 N
: Node_Id
:= Actual
;
11404 -- Similar to Sem_Util.Get_Enclosing_Object, but treat
11405 -- pointer dereference like component selection.
11407 if Is_Entity_Name
(N
) then
11408 Actual_Obj
:= Entity
(N
);
11413 when N_Indexed_Component
11414 | N_Selected_Component
11416 | N_Explicit_Dereference
11420 when N_Type_Conversion
=>
11421 N
:= Expression
(N
);
11424 Actual_Obj
:= Etype
(N
);
11429 Check_Volatility_Compatibility
11430 (Actual_Obj
, A_Gen_Obj
, "actual object",
11431 "its corresponding formal object of mode in out",
11432 Srcpos_Bearer
=> Actual
);
11435 -- Formal in-parameter
11438 -- The instantiation of a generic formal in-parameter is constant
11439 -- declaration. The actual is the expression for that declaration.
11440 -- Its type is a full copy of the type of the formal. This may be
11441 -- an access to subprogram, for which we need to generate entities
11442 -- for the formals in the new signature.
11444 if Present
(Actual
) then
11445 if Present
(Subt_Mark
) then
11446 Def
:= New_Copy_Tree
(Subt_Mark
);
11448 pragma Assert
(Present
(Acc_Def
));
11449 Def
:= New_Copy_Tree
(Acc_Def
);
11453 Make_Object_Declaration
(Loc
,
11454 Defining_Identifier
=> New_Copy
(Gen_Obj
),
11455 Constant_Present
=> True,
11456 Null_Exclusion_Present
=> Null_Exclusion_Present
(Formal
),
11457 Object_Definition
=> Def
,
11458 Expression
=> Actual
);
11460 Set_Corresponding_Generic_Association
(Decl_Node
, Act_Assoc
);
11462 -- A generic formal object of a tagged type is defined to be
11463 -- aliased so the new constant must also be treated as aliased.
11465 if Is_Tagged_Type
(Etype
(A_Gen_Obj
)) then
11466 Set_Aliased_Present
(Decl_Node
);
11469 Append
(Decl_Node
, List
);
11471 -- No need to repeat (pre-)analysis of some expression nodes
11472 -- already handled in Preanalyze_Actuals.
11474 if Nkind
(Actual
) /= N_Allocator
then
11477 -- Return if the analysis of the actual reported some error
11479 if Etype
(Actual
) = Any_Type
then
11485 Formal_Type
: constant Entity_Id
:= Etype
(A_Gen_Obj
);
11489 Typ
:= Get_Instance_Of
(Formal_Type
);
11491 -- If the actual appears in the current or an enclosing scope,
11492 -- use its type directly. This is relevant if it has an actual
11493 -- subtype that is distinct from its nominal one. This cannot
11494 -- be done in general because the type of the actual may
11495 -- depend on other actuals, and only be fully determined when
11496 -- the enclosing instance is analyzed.
11498 if Present
(Etype
(Actual
))
11499 and then Is_Constr_Subt_For_U_Nominal
(Etype
(Actual
))
11501 Freeze_Before
(Instantiation_Node
, Etype
(Actual
));
11503 Freeze_Before
(Instantiation_Node
, Typ
);
11506 -- If the actual is an aggregate, perform name resolution on
11507 -- its components (the analysis of an aggregate does not do it)
11508 -- to capture local names that may be hidden if the generic is
11511 if Nkind
(Actual
) = N_Aggregate
then
11512 Preanalyze_And_Resolve
(Actual
, Typ
);
11515 if Is_Limited_Type
(Typ
)
11516 and then not OK_For_Limited_Init
(Typ
, Actual
)
11519 ("initialization not allowed for limited types", Actual
);
11520 Explain_Limited_Type
(Typ
, Actual
);
11524 elsif Present
(Default_Expression
(Formal
)) then
11526 -- Use default to construct declaration
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
(Sloc
(Formal
),
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
=> New_Copy_Tree
11542 (Default_Expression
(Formal
)));
11544 Set_Corresponding_Generic_Association
11545 (Decl_Node
, Expression
(Decl_Node
));
11547 Append
(Decl_Node
, List
);
11548 Set_Analyzed
(Expression
(Decl_Node
), False);
11551 Error_Msg_NE
("missing actual&", Instantiation_Node
, Gen_Obj
);
11552 Error_Msg_NE
("\in instantiation of & declared#",
11553 Instantiation_Node
, Scope
(A_Gen_Obj
));
11555 if Is_Scalar_Type
(Etype
(A_Gen_Obj
)) then
11557 -- Create dummy constant declaration so that instance can be
11558 -- analyzed, to minimize cascaded visibility errors.
11560 if Present
(Subt_Mark
) then
11562 else pragma Assert
(Present
(Acc_Def
));
11567 Make_Object_Declaration
(Loc
,
11568 Defining_Identifier
=> New_Copy
(Gen_Obj
),
11569 Constant_Present
=> True,
11570 Null_Exclusion_Present
=> Null_Exclusion_Present
(Formal
),
11571 Object_Definition
=> New_Copy
(Def
),
11573 Make_Attribute_Reference
(Sloc
(Gen_Obj
),
11574 Attribute_Name
=> Name_First
,
11575 Prefix
=> New_Copy
(Def
)));
11577 Append
(Decl_Node
, List
);
11580 Abandon_Instantiation
(Instantiation_Node
);
11585 if Nkind
(Actual
) in N_Has_Entity
11586 and then Present
(Entity
(Actual
))
11588 Actual_Decl
:= Parent
(Entity
(Actual
));
11591 -- Ada 2005 (AI-423) refined by AI12-0287:
11592 -- For an object_renaming_declaration with a null_exclusion or an
11593 -- access_definition that has a null_exclusion, the subtype of the
11594 -- object_name shall exclude null. In addition, if the
11595 -- object_renaming_declaration occurs within the body of a generic unit
11596 -- G or within the body of a generic unit declared within the
11597 -- declarative region of generic unit G, then:
11598 -- * if the object_name statically denotes a generic formal object of
11599 -- mode in out of G, then the declaration of that object shall have a
11601 -- * if the object_name statically denotes a call of a generic formal
11602 -- function of G, then the declaration of the result of that function
11603 -- shall have a null_exclusion.
11605 if Ada_Version
>= Ada_2005
11606 and then Present
(Actual_Decl
)
11607 and then Nkind
(Actual_Decl
) in N_Formal_Object_Declaration
11608 | N_Object_Declaration
11609 and then Nkind
(Analyzed_Formal
) = N_Formal_Object_Declaration
11610 and then not Has_Null_Exclusion
(Actual_Decl
)
11611 and then Has_Null_Exclusion
(Analyzed_Formal
)
11612 and then Ekind
(Defining_Identifier
(Analyzed_Formal
))
11613 = E_Generic_In_Out_Parameter
11614 and then ((In_Generic_Scope
(Entity
(Actual
))
11615 and then In_Package_Body
(Scope
(Entity
(Actual
))))
11616 or else not Can_Never_Be_Null
(Etype
(Actual
)))
11618 Error_Msg_Sloc
:= Sloc
(Analyzed_Formal
);
11620 ("actual must exclude null to match generic formal#", Actual
);
11623 -- An effectively volatile object cannot be used as an actual in a
11624 -- generic instantiation (SPARK RM 7.1.3(7)). The following check is
11625 -- relevant only when SPARK_Mode is on as it is not a standard Ada
11626 -- legality rule, and also verifies that the actual is an object.
11629 and then Present
(Actual
)
11630 and then Is_Object_Reference
(Actual
)
11631 and then Is_Effectively_Volatile_Object
(Actual
)
11632 and then not Is_Effectively_Volatile
(A_Gen_Obj
)
11635 ("volatile object cannot act as actual in generic instantiation",
11640 end Instantiate_Object
;
11642 ------------------------------
11643 -- Instantiate_Package_Body --
11644 ------------------------------
11646 -- WARNING: This routine manages Ghost and SPARK regions. Return statements
11647 -- must be replaced by gotos which jump to the end of the routine in order
11648 -- to restore the Ghost and SPARK modes.
11650 procedure Instantiate_Package_Body
11651 (Body_Info
: Pending_Body_Info
;
11652 Inlined_Body
: Boolean := False;
11653 Body_Optional
: Boolean := False)
11655 Act_Decl
: constant Node_Id
:= Body_Info
.Act_Decl
;
11656 Act_Decl_Id
: constant Entity_Id
:= Defining_Entity
(Act_Decl
);
11657 Act_Spec
: constant Node_Id
:= Specification
(Act_Decl
);
11658 Ctx_Parents
: Elist_Id
:= No_Elist
;
11659 Ctx_Top
: Int
:= 0;
11660 Inst_Node
: constant Node_Id
:= Body_Info
.Inst_Node
;
11661 Gen_Id
: constant Node_Id
:= Name
(Inst_Node
);
11662 Gen_Unit
: constant Entity_Id
:= Get_Generic_Entity
(Inst_Node
);
11663 Gen_Decl
: constant Node_Id
:= Unit_Declaration_Node
(Gen_Unit
);
11664 Loc
: constant Source_Ptr
:= Sloc
(Inst_Node
);
11666 procedure Check_Initialized_Types
;
11667 -- In a generic package body, an entity of a generic private type may
11668 -- appear uninitialized. This is suspicious, unless the actual is a
11669 -- fully initialized type.
11671 procedure Install_Parents_Of_Generic_Context
11672 (Inst_Scope
: Entity_Id
;
11673 Ctx_Parents
: out Elist_Id
);
11674 -- Inst_Scope is the scope where the instance appears within; when it
11675 -- appears within a generic child package G, this routine collects and
11676 -- installs the enclosing packages of G in the scopes stack; installed
11677 -- packages are returned in Ctx_Parents.
11679 procedure Remove_Parents_Of_Generic_Context
(Ctx_Parents
: Elist_Id
);
11680 -- Reverse effect after instantiation is complete
11682 -----------------------------
11683 -- Check_Initialized_Types --
11684 -----------------------------
11686 procedure Check_Initialized_Types
is
11688 Formal
: Entity_Id
;
11689 Actual
: Entity_Id
;
11690 Uninit_Var
: Entity_Id
;
11693 Decl
:= First
(Generic_Formal_Declarations
(Gen_Decl
));
11694 while Present
(Decl
) loop
11695 Uninit_Var
:= Empty
;
11697 if Nkind
(Decl
) = N_Private_Extension_Declaration
then
11698 Uninit_Var
:= Uninitialized_Variable
(Decl
);
11700 elsif Nkind
(Decl
) = N_Formal_Type_Declaration
11701 and then Nkind
(Formal_Type_Definition
(Decl
)) =
11702 N_Formal_Private_Type_Definition
11705 Uninitialized_Variable
(Formal_Type_Definition
(Decl
));
11708 if Present
(Uninit_Var
) then
11709 Formal
:= Defining_Identifier
(Decl
);
11710 Actual
:= First_Entity
(Act_Decl_Id
);
11712 -- For each formal there is a subtype declaration that renames
11713 -- the actual and has the same name as the formal. Locate the
11714 -- formal for warning message about uninitialized variables
11715 -- in the generic, for which the actual type should be a fully
11716 -- initialized type.
11718 while Present
(Actual
) loop
11719 exit when Ekind
(Actual
) = E_Package
11720 and then Present
(Renamed_Entity
(Actual
));
11722 if Chars
(Actual
) = Chars
(Formal
)
11723 and then not Is_Scalar_Type
(Actual
)
11724 and then not Is_Fully_Initialized_Type
(Actual
)
11725 and then Warn_On_No_Value_Assigned
11727 Error_Msg_Node_2
:= Formal
;
11729 ("generic unit has uninitialized variable& of "
11730 & "formal private type &?v?", Actual
, Uninit_Var
);
11732 ("actual type for& should be fully initialized type?v?",
11737 Next_Entity
(Actual
);
11743 end Check_Initialized_Types
;
11745 ----------------------------------------
11746 -- Install_Parents_Of_Generic_Context --
11747 ----------------------------------------
11749 procedure Install_Parents_Of_Generic_Context
11750 (Inst_Scope
: Entity_Id
;
11751 Ctx_Parents
: out Elist_Id
)
11757 Ctx_Parents
:= New_Elmt_List
;
11759 -- Collect context parents (ie. parents where the instantiation
11760 -- appears within).
11763 while S
/= Standard_Standard
loop
11764 Prepend_Elmt
(S
, Ctx_Parents
);
11768 -- Install enclosing parents
11770 Elmt
:= First_Elmt
(Ctx_Parents
);
11771 while Present
(Elmt
) loop
11772 Push_Scope
(Node
(Elmt
));
11773 Set_Is_Immediately_Visible
(Node
(Elmt
));
11776 end Install_Parents_Of_Generic_Context
;
11778 ---------------------------------------
11779 -- Remove_Parents_Of_Generic_Context --
11780 ---------------------------------------
11782 procedure Remove_Parents_Of_Generic_Context
(Ctx_Parents
: Elist_Id
) is
11786 -- Traverse Ctx_Parents in LIFO order to check the removed scopes
11788 Elmt
:= Last_Elmt
(Ctx_Parents
);
11789 while Present
(Elmt
) loop
11790 pragma Assert
(Current_Scope
= Node
(Elmt
));
11791 Set_Is_Immediately_Visible
(Current_Scope
, False);
11794 Remove_Last_Elmt
(Ctx_Parents
);
11795 Elmt
:= Last_Elmt
(Ctx_Parents
);
11797 end Remove_Parents_Of_Generic_Context
;
11801 -- The following constants capture the context prior to instantiating
11802 -- the package body.
11804 Saved_CS
: constant Config_Switches_Type
:= Save_Config_Switches
;
11805 Saved_GM
: constant Ghost_Mode_Type
:= Ghost_Mode
;
11806 Saved_IGR
: constant Node_Id
:= Ignored_Ghost_Region
;
11807 Saved_ISMP
: constant Boolean :=
11808 Ignore_SPARK_Mode_Pragmas_In_Instance
;
11809 Saved_LSST
: constant Suppress_Stack_Entry_Ptr
:=
11810 Local_Suppress_Stack_Top
;
11811 Saved_SC
: constant Boolean := Style_Check
;
11812 Saved_SM
: constant SPARK_Mode_Type
:= SPARK_Mode
;
11813 Saved_SMP
: constant Node_Id
:= SPARK_Mode_Pragma
;
11814 Saved_SS
: constant Suppress_Record
:= Scope_Suppress
;
11815 Saved_Warn
: constant Warning_Record
:= Save_Warnings
;
11817 Act_Body
: Node_Id
;
11818 Act_Body_Id
: Entity_Id
;
11819 Act_Body_Name
: Node_Id
;
11820 Gen_Body
: Node_Id
;
11821 Gen_Body_Id
: Node_Id
;
11822 Par_Ent
: Entity_Id
:= Empty
;
11823 Par_Installed
: Boolean := False;
11824 Par_Vis
: Boolean := False;
11826 Scope_Check_Id
: Entity_Id
;
11827 Scope_Check_Last
: Nat
;
11828 -- Value of Current_Scope before calls to Install_Parents; used to check
11829 -- that scopes are correctly removed after instantiation.
11831 Vis_Prims_List
: Elist_Id
:= No_Elist
;
11832 -- List of primitives made temporarily visible in the instantiation
11833 -- to match the visibility of the formal type.
11835 -- Start of processing for Instantiate_Package_Body
11838 Gen_Body_Id
:= Corresponding_Body
(Gen_Decl
);
11840 -- The instance body may already have been processed, as the parent of
11841 -- another instance that is inlined (Load_Parent_Of_Generic).
11843 if Present
(Corresponding_Body
(Instance_Spec
(Inst_Node
))) then
11847 -- The package being instantiated may be subject to pragma Ghost. Set
11848 -- the mode now to ensure that any nodes generated during instantiation
11849 -- are properly marked as Ghost.
11851 Set_Ghost_Mode
(Act_Decl_Id
);
11853 Expander_Mode_Save_And_Set
(Body_Info
.Expander_Status
);
11855 -- Re-establish the state of information on which checks are suppressed.
11856 -- This information was set in Body_Info at the point of instantiation,
11857 -- and now we restore it so that the instance is compiled using the
11858 -- check status at the instantiation (RM 11.5(7.2/2), AI95-00224-01).
11860 Local_Suppress_Stack_Top
:= Body_Info
.Local_Suppress_Stack_Top
;
11861 Scope_Suppress
:= Body_Info
.Scope_Suppress
;
11863 Restore_Config_Switches
(Body_Info
.Config_Switches
);
11864 Restore_Warnings
(Body_Info
.Warnings
);
11866 if No
(Gen_Body_Id
) then
11868 -- Do not look for parent of generic body if none is required.
11869 -- This may happen when the routine is called as part of the
11870 -- Pending_Instantiations processing, when nested instances
11871 -- may precede the one generated from the main unit.
11873 if not Unit_Requires_Body
(Defining_Entity
(Gen_Decl
))
11874 and then Body_Optional
11878 Load_Parent_Of_Generic
11879 (Inst_Node
, Specification
(Gen_Decl
), Body_Optional
);
11881 -- Surprisingly enough, loading the body of the parent can cause
11882 -- the body to be instantiated and the double instantiation needs
11883 -- to be prevented in order to avoid giving bogus semantic errors.
11885 -- This case can occur because of the Collect_Previous_Instances
11886 -- machinery of Load_Parent_Of_Generic, which will instantiate
11887 -- bodies that are deemed to be ahead of the body of the parent
11888 -- in the compilation unit. But the relative position of these
11889 -- bodies is computed using the mere comparison of their Sloc.
11891 -- Now suppose that you have two generic packages G and H, with
11892 -- G containing a mere instantiation of H:
11898 -- package Nested_G is
11909 -- package My_H is new H;
11913 -- and a third package Q instantiating G and Nested_G:
11919 -- package My_G is new G;
11921 -- package My_Nested_G is new My_G.My_H.Nested_G;
11925 -- The body to be instantiated is that of My_Nested_G and its
11926 -- parent is the instance My_G.My_H. This latter instantiation
11927 -- is done when My_G is analyzed, i.e. after the declarations
11928 -- of My_G and My_Nested_G have been parsed; as a result, the
11929 -- Sloc of My_G.My_H is greater than the Sloc of My_Nested_G.
11931 -- Therefore loading the body of My_G.My_H will cause the body
11932 -- of My_Nested_G to be instantiated because it is deemed to be
11933 -- ahead of My_G.My_H. This means that Load_Parent_Of_Generic
11934 -- will again be invoked on My_G.My_H, but this time with the
11935 -- Collect_Previous_Instances machinery disabled, so there is
11936 -- no endless mutual recursion and things are done in order.
11938 if Present
(Corresponding_Body
(Instance_Spec
(Inst_Node
))) then
11942 Gen_Body_Id
:= Corresponding_Body
(Gen_Decl
);
11946 -- Establish global variable for sloc adjustment and for error recovery
11947 -- In the case of an instance body for an instantiation with actuals
11948 -- from a limited view, the instance body is placed at the beginning
11949 -- of the enclosing package body: use the body entity as the source
11950 -- location for nodes of the instance body.
11952 if not Is_Empty_Elmt_List
(Incomplete_Actuals
(Act_Decl_Id
)) then
11954 Scop
: constant Entity_Id
:= Scope
(Act_Decl_Id
);
11955 Body_Id
: constant Node_Id
:=
11956 Corresponding_Body
(Unit_Declaration_Node
(Scop
));
11959 Instantiation_Node
:= Body_Id
;
11962 Instantiation_Node
:= Inst_Node
;
11965 if Present
(Gen_Body_Id
) then
11966 Save_Env
(Gen_Unit
, Act_Decl_Id
);
11967 Style_Check
:= False;
11969 -- If the context of the instance is subject to SPARK_Mode "off", the
11970 -- annotation is missing, or the body is instantiated at a later pass
11971 -- and its spec ignored SPARK_Mode pragma, set the global flag which
11972 -- signals Analyze_Pragma to ignore all SPARK_Mode pragmas within the
11975 if SPARK_Mode
/= On
11976 or else Ignore_SPARK_Mode_Pragmas
(Act_Decl_Id
)
11978 Ignore_SPARK_Mode_Pragmas_In_Instance
:= True;
11981 Current_Sem_Unit
:= Body_Info
.Current_Sem_Unit
;
11982 Gen_Body
:= Unit_Declaration_Node
(Gen_Body_Id
);
11984 Create_Instantiation_Source
11985 (Inst_Node
, Gen_Body_Id
, S_Adjustment
);
11989 (Original_Node
(Gen_Body
), Empty
, Instantiating
=> True);
11991 -- Create proper (possibly qualified) defining name for the body, to
11992 -- correspond to the one in the spec.
11995 Make_Defining_Identifier
(Sloc
(Act_Decl_Id
), Chars
(Act_Decl_Id
));
11996 Preserve_Comes_From_Source
(Act_Body_Id
, Act_Decl_Id
);
11998 -- Some attributes of spec entity are not inherited by body entity
12000 Set_Handler_Records
(Act_Body_Id
, No_List
);
12002 if Nkind
(Defining_Unit_Name
(Act_Spec
)) =
12003 N_Defining_Program_Unit_Name
12006 Make_Defining_Program_Unit_Name
(Loc
,
12008 New_Copy_Tree
(Name
(Defining_Unit_Name
(Act_Spec
))),
12009 Defining_Identifier
=> Act_Body_Id
);
12011 Act_Body_Name
:= Act_Body_Id
;
12014 Set_Defining_Unit_Name
(Act_Body
, Act_Body_Name
);
12016 Set_Corresponding_Spec
(Act_Body
, Act_Decl_Id
);
12017 Check_Generic_Actuals
(Act_Decl_Id
, False);
12018 Check_Initialized_Types
;
12020 -- Install primitives hidden at the point of the instantiation but
12021 -- visible when processing the generic formals
12027 E
:= First_Entity
(Act_Decl_Id
);
12028 while Present
(E
) loop
12030 and then not Is_Itype
(E
)
12031 and then Is_Generic_Actual_Type
(E
)
12032 and then Is_Tagged_Type
(E
)
12034 Install_Hidden_Primitives
12035 (Prims_List
=> Vis_Prims_List
,
12036 Gen_T
=> Generic_Parent_Type
(Parent
(E
)),
12044 Scope_Check_Id
:= Current_Scope
;
12045 Scope_Check_Last
:= Scope_Stack
.Last
;
12047 -- If the instantiation appears within a generic child some actual
12048 -- parameter may be the current instance of the enclosing generic
12052 Inst_Scope
: constant Entity_Id
:= Scope
(Act_Decl_Id
);
12055 if Is_Child_Unit
(Inst_Scope
)
12056 and then Ekind
(Inst_Scope
) = E_Generic_Package
12057 and then Present
(Generic_Associations
(Inst_Node
))
12059 Install_Parents_Of_Generic_Context
(Inst_Scope
, Ctx_Parents
);
12061 -- Hide them from visibility; required to avoid conflicts
12062 -- installing the parent instance.
12064 if Present
(Ctx_Parents
) then
12065 Push_Scope
(Standard_Standard
);
12066 Ctx_Top
:= Scope_Stack
.Last
;
12067 Scope_Stack
.Table
(Ctx_Top
).Is_Active_Stack_Base
:= True;
12072 -- If it is a child unit, make the parent instance (which is an
12073 -- instance of the parent of the generic) visible. The parent
12074 -- instance is the prefix of the name of the generic unit.
12076 if Ekind
(Scope
(Gen_Unit
)) = E_Generic_Package
12077 and then Nkind
(Gen_Id
) = N_Expanded_Name
12079 Par_Ent
:= Entity
(Prefix
(Gen_Id
));
12080 Par_Vis
:= Is_Immediately_Visible
(Par_Ent
);
12081 Install_Parent
(Par_Ent
, In_Body
=> True);
12082 Par_Installed
:= True;
12084 elsif Is_Child_Unit
(Gen_Unit
) then
12085 Par_Ent
:= Scope
(Gen_Unit
);
12086 Par_Vis
:= Is_Immediately_Visible
(Par_Ent
);
12087 Install_Parent
(Par_Ent
, In_Body
=> True);
12088 Par_Installed
:= True;
12091 -- If the instantiation is a library unit, and this is the main unit,
12092 -- then build the resulting compilation unit nodes for the instance.
12093 -- If this is a compilation unit but it is not the main unit, then it
12094 -- is the body of a unit in the context, that is being compiled
12095 -- because it is encloses some inlined unit or another generic unit
12096 -- being instantiated. In that case, this body is not part of the
12097 -- current compilation, and is not attached to the tree, but its
12098 -- parent must be set for analysis.
12100 if Nkind
(Parent
(Inst_Node
)) = N_Compilation_Unit
then
12102 -- Replace instance node with body of instance, and create new
12103 -- node for corresponding instance declaration.
12105 Build_Instance_Compilation_Unit_Nodes
12106 (Inst_Node
, Act_Body
, Act_Decl
);
12108 -- If the instantiation appears within a generic child package
12109 -- enable visibility of current instance of enclosing generic
12112 if Present
(Ctx_Parents
) then
12113 Scope_Stack
.Table
(Ctx_Top
).Is_Active_Stack_Base
:= False;
12114 Analyze
(Inst_Node
);
12115 Scope_Stack
.Table
(Ctx_Top
).Is_Active_Stack_Base
:= True;
12117 Analyze
(Inst_Node
);
12120 if Parent
(Inst_Node
) = Cunit
(Main_Unit
) then
12122 -- If the instance is a child unit itself, then set the scope
12123 -- of the expanded body to be the parent of the instantiation
12124 -- (ensuring that the fully qualified name will be generated
12125 -- for the elaboration subprogram).
12127 if Nkind
(Defining_Unit_Name
(Act_Spec
)) =
12128 N_Defining_Program_Unit_Name
12130 Set_Scope
(Defining_Entity
(Inst_Node
), Scope
(Act_Decl_Id
));
12134 -- Case where instantiation is not a library unit
12137 -- Handle the case of an instance with incomplete actual types.
12138 -- The instance body cannot be placed just after the declaration
12139 -- because full views have not been seen yet. Any use of the non-
12140 -- limited views in the instance body requires the presence of a
12141 -- regular with_clause in the enclosing unit. Therefore we place
12142 -- the instance body at the beginning of the enclosing body, and
12143 -- the freeze node for the instance is then placed after the body.
12145 if not Is_Empty_Elmt_List
(Incomplete_Actuals
(Act_Decl_Id
))
12146 and then Ekind
(Scope
(Act_Decl_Id
)) = E_Package
12149 Scop
: constant Entity_Id
:= Scope
(Act_Decl_Id
);
12150 Body_Id
: constant Node_Id
:=
12151 Corresponding_Body
(Unit_Declaration_Node
(Scop
));
12156 pragma Assert
(Present
(Body_Id
));
12158 Prepend
(Act_Body
, Declarations
(Parent
(Body_Id
)));
12160 if Expander_Active
then
12161 Ensure_Freeze_Node
(Act_Decl_Id
);
12162 F_Node
:= Freeze_Node
(Act_Decl_Id
);
12163 Set_Is_Frozen
(Act_Decl_Id
, False);
12164 if Is_List_Member
(F_Node
) then
12168 Insert_After
(Act_Body
, F_Node
);
12173 Insert_Before
(Inst_Node
, Act_Body
);
12174 Mark_Rewrite_Insertion
(Act_Body
);
12176 -- Insert the freeze node for the instance if need be
12178 if Expander_Active
then
12179 Freeze_Package_Instance
12180 (Inst_Node
, Gen_Body
, Gen_Decl
, Act_Decl_Id
);
12181 Set_Is_Frozen
(Act_Decl_Id
);
12185 -- If the instantiation appears within a generic child package
12186 -- enable visibility of current instance of enclosing generic
12189 if Present
(Ctx_Parents
) then
12190 Scope_Stack
.Table
(Ctx_Top
).Is_Active_Stack_Base
:= False;
12191 Analyze
(Act_Body
);
12192 Scope_Stack
.Table
(Ctx_Top
).Is_Active_Stack_Base
:= True;
12194 Analyze
(Act_Body
);
12198 Inherit_Context
(Gen_Body
, Inst_Node
);
12200 if Par_Installed
then
12201 Remove_Parent
(In_Body
=> True);
12203 -- Restore the previous visibility of the parent
12205 Set_Is_Immediately_Visible
(Par_Ent
, Par_Vis
);
12208 -- Remove the parent instances if they have been placed on the scope
12209 -- stack to compile the body.
12211 if Present
(Ctx_Parents
) then
12212 pragma Assert
(Scope_Stack
.Last
= Ctx_Top
12213 and then Current_Scope
= Standard_Standard
);
12216 Remove_Parents_Of_Generic_Context
(Ctx_Parents
);
12219 pragma Assert
(Current_Scope
= Scope_Check_Id
);
12220 pragma Assert
(Scope_Stack
.Last
= Scope_Check_Last
);
12222 Restore_Hidden_Primitives
(Vis_Prims_List
);
12224 -- Restore the private views that were made visible when the body of
12225 -- the instantiation was created. Note that, in the case where one of
12226 -- these private views is declared in the parent, there is a nesting
12227 -- issue with the calls to Install_Parent and Remove_Parent made in
12228 -- between above with In_Body set to True, because these calls also
12229 -- want to swap and restore this private view respectively. In this
12230 -- case, the call to Install_Parent does nothing, but the call to
12231 -- Remove_Parent does restore the private view, thus undercutting the
12232 -- call to Restore_Private_Views. That's OK under the condition that
12233 -- the two mechanisms swap exactly the same entities, in particular
12234 -- the private entities dependent on the primary private entities.
12236 Restore_Private_Views
(Act_Decl_Id
);
12238 -- Remove the current unit from visibility if this is an instance
12239 -- that is not elaborated on the fly for inlining purposes.
12241 if not Inlined_Body
then
12242 Set_Is_Immediately_Visible
(Act_Decl_Id
, False);
12247 -- If we have no body, and the unit requires a body, then complain. This
12248 -- complaint is suppressed if we have detected other errors (since a
12249 -- common reason for missing the body is that it had errors).
12250 -- In CodePeer mode, a warning has been emitted already, no need for
12251 -- further messages.
12253 elsif Unit_Requires_Body
(Gen_Unit
)
12254 and then not Body_Optional
12256 if CodePeer_Mode
then
12259 elsif Serious_Errors_Detected
= 0 then
12261 ("cannot find body of generic package &", Inst_Node
, Gen_Unit
);
12263 -- Don't attempt to perform any cleanup actions if some other error
12264 -- was already detected, since this can cause blowups.
12270 -- Case of package that does not need a body
12273 -- If the instantiation of the declaration is a library unit, rewrite
12274 -- the original package instantiation as a package declaration in the
12275 -- compilation unit node.
12277 if Nkind
(Parent
(Inst_Node
)) = N_Compilation_Unit
then
12278 Set_Parent_Spec
(Act_Decl
, Parent_Spec
(Inst_Node
));
12279 Rewrite
(Inst_Node
, Act_Decl
);
12281 -- Generate elaboration entity, in case spec has elaboration code.
12282 -- This cannot be done when the instance is analyzed, because it
12283 -- is not known yet whether the body exists.
12285 Set_Elaboration_Entity_Required
(Act_Decl_Id
, False);
12286 Build_Elaboration_Entity
(Parent
(Inst_Node
), Act_Decl_Id
);
12288 -- If the instantiation is not a library unit, then append the
12289 -- declaration to the list of implicitly generated entities, unless
12290 -- it is already a list member which means that it was already
12293 elsif not Is_List_Member
(Act_Decl
) then
12294 Mark_Rewrite_Insertion
(Act_Decl
);
12295 Insert_Before
(Inst_Node
, Act_Decl
);
12301 -- Restore the context that was in effect prior to instantiating the
12304 Ignore_SPARK_Mode_Pragmas_In_Instance
:= Saved_ISMP
;
12305 Local_Suppress_Stack_Top
:= Saved_LSST
;
12306 Scope_Suppress
:= Saved_SS
;
12307 Style_Check
:= Saved_SC
;
12309 Expander_Mode_Restore
;
12310 Restore_Config_Switches
(Saved_CS
);
12311 Restore_Ghost_Region
(Saved_GM
, Saved_IGR
);
12312 Restore_SPARK_Mode
(Saved_SM
, Saved_SMP
);
12313 Restore_Warnings
(Saved_Warn
);
12314 end Instantiate_Package_Body
;
12316 ---------------------------------
12317 -- Instantiate_Subprogram_Body --
12318 ---------------------------------
12320 -- WARNING: This routine manages Ghost and SPARK regions. Return statements
12321 -- must be replaced by gotos which jump to the end of the routine in order
12322 -- to restore the Ghost and SPARK modes.
12324 procedure Instantiate_Subprogram_Body
12325 (Body_Info
: Pending_Body_Info
;
12326 Body_Optional
: Boolean := False)
12328 Act_Decl
: constant Node_Id
:= Body_Info
.Act_Decl
;
12329 Act_Decl_Id
: constant Entity_Id
:= Defining_Entity
(Act_Decl
);
12330 Inst_Node
: constant Node_Id
:= Body_Info
.Inst_Node
;
12331 Gen_Id
: constant Node_Id
:= Name
(Inst_Node
);
12332 Gen_Unit
: constant Entity_Id
:= Get_Generic_Entity
(Inst_Node
);
12333 Gen_Decl
: constant Node_Id
:= Unit_Declaration_Node
(Gen_Unit
);
12334 Loc
: constant Source_Ptr
:= Sloc
(Inst_Node
);
12335 Pack_Id
: constant Entity_Id
:=
12336 Defining_Unit_Name
(Parent
(Act_Decl
));
12338 -- The following constants capture the context prior to instantiating
12339 -- the subprogram body.
12341 Saved_CS
: constant Config_Switches_Type
:= Save_Config_Switches
;
12342 Saved_GM
: constant Ghost_Mode_Type
:= Ghost_Mode
;
12343 Saved_IGR
: constant Node_Id
:= Ignored_Ghost_Region
;
12344 Saved_ISMP
: constant Boolean :=
12345 Ignore_SPARK_Mode_Pragmas_In_Instance
;
12346 Saved_LSST
: constant Suppress_Stack_Entry_Ptr
:=
12347 Local_Suppress_Stack_Top
;
12348 Saved_SC
: constant Boolean := Style_Check
;
12349 Saved_SM
: constant SPARK_Mode_Type
:= SPARK_Mode
;
12350 Saved_SMP
: constant Node_Id
:= SPARK_Mode_Pragma
;
12351 Saved_SS
: constant Suppress_Record
:= Scope_Suppress
;
12352 Saved_Warn
: constant Warning_Record
:= Save_Warnings
;
12354 Act_Body
: Node_Id
;
12355 Act_Body_Id
: Entity_Id
;
12356 Gen_Body
: Node_Id
;
12357 Gen_Body_Id
: Node_Id
;
12358 Pack_Body
: Node_Id
;
12359 Par_Ent
: Entity_Id
:= Empty
;
12360 Par_Installed
: Boolean := False;
12361 Par_Vis
: Boolean := False;
12362 Ret_Expr
: Node_Id
;
12365 Gen_Body_Id
:= Corresponding_Body
(Gen_Decl
);
12367 -- Subprogram body may have been created already because of an inline
12368 -- pragma, or because of multiple elaborations of the enclosing package
12369 -- when several instances of the subprogram appear in the main unit.
12371 if Present
(Corresponding_Body
(Act_Decl
)) then
12375 -- The subprogram being instantiated may be subject to pragma Ghost. Set
12376 -- the mode now to ensure that any nodes generated during instantiation
12377 -- are properly marked as Ghost.
12379 Set_Ghost_Mode
(Act_Decl_Id
);
12381 Expander_Mode_Save_And_Set
(Body_Info
.Expander_Status
);
12383 -- Re-establish the state of information on which checks are suppressed.
12384 -- This information was set in Body_Info at the point of instantiation,
12385 -- and now we restore it so that the instance is compiled using the
12386 -- check status at the instantiation (RM 11.5(7.2/2), AI95-00224-01).
12388 Local_Suppress_Stack_Top
:= Body_Info
.Local_Suppress_Stack_Top
;
12389 Scope_Suppress
:= Body_Info
.Scope_Suppress
;
12391 Restore_Config_Switches
(Body_Info
.Config_Switches
);
12392 Restore_Warnings
(Body_Info
.Warnings
);
12394 if No
(Gen_Body_Id
) then
12396 -- For imported generic subprogram, no body to compile, complete
12397 -- the spec entity appropriately.
12399 if Is_Imported
(Gen_Unit
) then
12400 Set_Is_Imported
(Act_Decl_Id
);
12401 Set_First_Rep_Item
(Act_Decl_Id
, First_Rep_Item
(Gen_Unit
));
12402 Set_Interface_Name
(Act_Decl_Id
, Interface_Name
(Gen_Unit
));
12403 Set_Convention
(Act_Decl_Id
, Convention
(Gen_Unit
));
12404 Set_Has_Completion
(Act_Decl_Id
);
12407 -- For other cases, compile the body
12410 Load_Parent_Of_Generic
12411 (Inst_Node
, Specification
(Gen_Decl
), Body_Optional
);
12412 Gen_Body_Id
:= Corresponding_Body
(Gen_Decl
);
12416 Instantiation_Node
:= Inst_Node
;
12418 if Present
(Gen_Body_Id
) then
12419 Gen_Body
:= Unit_Declaration_Node
(Gen_Body_Id
);
12421 if Nkind
(Gen_Body
) = N_Subprogram_Body_Stub
then
12423 -- Either body is not present, or context is non-expanding, as
12424 -- when compiling a subunit. Mark the instance as completed, and
12425 -- diagnose a missing body when needed.
12428 and then Operating_Mode
= Generate_Code
12430 Error_Msg_N
("missing proper body for instantiation", Gen_Body
);
12433 Set_Has_Completion
(Act_Decl_Id
);
12437 Save_Env
(Gen_Unit
, Act_Decl_Id
);
12438 Style_Check
:= False;
12440 -- If the context of the instance is subject to SPARK_Mode "off", the
12441 -- annotation is missing, or the body is instantiated at a later pass
12442 -- and its spec ignored SPARK_Mode pragma, set the global flag which
12443 -- signals Analyze_Pragma to ignore all SPARK_Mode pragmas within the
12446 if SPARK_Mode
/= On
12447 or else Ignore_SPARK_Mode_Pragmas
(Act_Decl_Id
)
12449 Ignore_SPARK_Mode_Pragmas_In_Instance
:= True;
12452 -- If the context of an instance is not subject to SPARK_Mode "off",
12453 -- and the generic body is subject to an explicit SPARK_Mode pragma,
12454 -- the latter should be the one applicable to the instance.
12456 if not Ignore_SPARK_Mode_Pragmas_In_Instance
12457 and then SPARK_Mode
/= Off
12458 and then Present
(SPARK_Pragma
(Gen_Body_Id
))
12460 Set_SPARK_Mode
(Gen_Body_Id
);
12463 Current_Sem_Unit
:= Body_Info
.Current_Sem_Unit
;
12464 Create_Instantiation_Source
12471 (Original_Node
(Gen_Body
), Empty
, Instantiating
=> True);
12473 -- Create proper defining name for the body, to correspond to the one
12477 Make_Defining_Identifier
(Sloc
(Act_Decl_Id
), Chars
(Act_Decl_Id
));
12479 Preserve_Comes_From_Source
(Act_Body_Id
, Act_Decl_Id
);
12480 Set_Defining_Unit_Name
(Specification
(Act_Body
), Act_Body_Id
);
12482 Set_Corresponding_Spec
(Act_Body
, Act_Decl_Id
);
12483 Set_Has_Completion
(Act_Decl_Id
);
12484 Check_Generic_Actuals
(Pack_Id
, False);
12486 -- Generate a reference to link the visible subprogram instance to
12487 -- the generic body, which for navigation purposes is the only
12488 -- available source for the instance.
12491 (Related_Instance
(Pack_Id
),
12492 Gen_Body_Id
, 'b', Set_Ref
=> False, Force
=> True);
12494 -- If it is a child unit, make the parent instance (which is an
12495 -- instance of the parent of the generic) visible. The parent
12496 -- instance is the prefix of the name of the generic unit.
12498 if Ekind
(Scope
(Gen_Unit
)) = E_Generic_Package
12499 and then Nkind
(Gen_Id
) = N_Expanded_Name
12501 Par_Ent
:= Entity
(Prefix
(Gen_Id
));
12502 Par_Vis
:= Is_Immediately_Visible
(Par_Ent
);
12503 Install_Parent
(Par_Ent
, In_Body
=> True);
12504 Par_Installed
:= True;
12506 elsif Is_Child_Unit
(Gen_Unit
) then
12507 Par_Ent
:= Scope
(Gen_Unit
);
12508 Par_Vis
:= Is_Immediately_Visible
(Par_Ent
);
12509 Install_Parent
(Par_Ent
, In_Body
=> True);
12510 Par_Installed
:= True;
12513 -- Subprogram body is placed in the body of wrapper package,
12514 -- whose spec contains the subprogram declaration as well as
12515 -- the renaming declarations for the generic parameters.
12518 Make_Package_Body
(Loc
,
12519 Defining_Unit_Name
=> New_Copy
(Pack_Id
),
12520 Declarations
=> New_List
(Act_Body
));
12522 Set_Corresponding_Spec
(Pack_Body
, Pack_Id
);
12524 -- If the instantiation is a library unit, then build resulting
12525 -- compilation unit nodes for the instance. The declaration of
12526 -- the enclosing package is the grandparent of the subprogram
12527 -- declaration. First replace the instantiation node as the unit
12528 -- of the corresponding compilation.
12530 if Nkind
(Parent
(Inst_Node
)) = N_Compilation_Unit
then
12531 if Parent
(Inst_Node
) = Cunit
(Main_Unit
) then
12532 Set_Unit
(Parent
(Inst_Node
), Inst_Node
);
12533 Build_Instance_Compilation_Unit_Nodes
12534 (Inst_Node
, Pack_Body
, Parent
(Parent
(Act_Decl
)));
12535 Analyze
(Inst_Node
);
12537 Set_Parent
(Pack_Body
, Parent
(Inst_Node
));
12538 Analyze
(Pack_Body
);
12542 Insert_Before
(Inst_Node
, Pack_Body
);
12543 Mark_Rewrite_Insertion
(Pack_Body
);
12545 -- Insert the freeze node for the instance if need be
12547 if Expander_Active
then
12548 Freeze_Subprogram_Instance
(Inst_Node
, Gen_Body
, Pack_Id
);
12551 Analyze
(Pack_Body
);
12554 Inherit_Context
(Gen_Body
, Inst_Node
);
12556 Restore_Private_Views
(Pack_Id
, False);
12558 if Par_Installed
then
12559 Remove_Parent
(In_Body
=> True);
12561 -- Restore the previous visibility of the parent
12563 Set_Is_Immediately_Visible
(Par_Ent
, Par_Vis
);
12568 -- Body not found. Error was emitted already. If there were no previous
12569 -- errors, this may be an instance whose scope is a premature instance.
12570 -- In that case we must insure that the (legal) program does raise
12571 -- program error if executed. We generate a subprogram body for this
12574 elsif Serious_Errors_Detected
= 0
12575 and then Nkind
(Parent
(Inst_Node
)) /= N_Compilation_Unit
12577 if Body_Optional
then
12580 elsif Ekind
(Act_Decl_Id
) = E_Procedure
then
12582 Make_Subprogram_Body
(Loc
,
12584 Make_Procedure_Specification
(Loc
,
12585 Defining_Unit_Name
=>
12586 Make_Defining_Identifier
(Loc
, Chars
(Act_Decl_Id
)),
12587 Parameter_Specifications
=>
12589 (Parameter_Specifications
(Parent
(Act_Decl_Id
)))),
12591 Declarations
=> Empty_List
,
12592 Handled_Statement_Sequence
=>
12593 Make_Handled_Sequence_Of_Statements
(Loc
,
12594 Statements
=> New_List
(
12595 Make_Raise_Program_Error
(Loc
,
12596 Reason
=> PE_Access_Before_Elaboration
))));
12600 Make_Raise_Program_Error
(Loc
,
12601 Reason
=> PE_Access_Before_Elaboration
);
12603 Set_Etype
(Ret_Expr
, (Etype
(Act_Decl_Id
)));
12604 Set_Analyzed
(Ret_Expr
);
12607 Make_Subprogram_Body
(Loc
,
12609 Make_Function_Specification
(Loc
,
12610 Defining_Unit_Name
=>
12611 Make_Defining_Identifier
(Loc
, Chars
(Act_Decl_Id
)),
12612 Parameter_Specifications
=>
12614 (Parameter_Specifications
(Parent
(Act_Decl_Id
))),
12615 Result_Definition
=>
12616 New_Occurrence_Of
(Etype
(Act_Decl_Id
), Loc
)),
12618 Declarations
=> Empty_List
,
12619 Handled_Statement_Sequence
=>
12620 Make_Handled_Sequence_Of_Statements
(Loc
,
12621 Statements
=> New_List
(
12622 Make_Simple_Return_Statement
(Loc
, Ret_Expr
))));
12626 Make_Package_Body
(Loc
,
12627 Defining_Unit_Name
=> New_Copy
(Pack_Id
),
12628 Declarations
=> New_List
(Act_Body
));
12630 Insert_After
(Inst_Node
, Pack_Body
);
12631 Set_Corresponding_Spec
(Pack_Body
, Pack_Id
);
12632 Analyze
(Pack_Body
);
12637 -- Restore the context that was in effect prior to instantiating the
12638 -- subprogram body.
12640 Ignore_SPARK_Mode_Pragmas_In_Instance
:= Saved_ISMP
;
12641 Local_Suppress_Stack_Top
:= Saved_LSST
;
12642 Scope_Suppress
:= Saved_SS
;
12643 Style_Check
:= Saved_SC
;
12645 Expander_Mode_Restore
;
12646 Restore_Config_Switches
(Saved_CS
);
12647 Restore_Ghost_Region
(Saved_GM
, Saved_IGR
);
12648 Restore_SPARK_Mode
(Saved_SM
, Saved_SMP
);
12649 Restore_Warnings
(Saved_Warn
);
12650 end Instantiate_Subprogram_Body
;
12652 ----------------------
12653 -- Instantiate_Type --
12654 ----------------------
12656 function Instantiate_Type
12659 Analyzed_Formal
: Node_Id
;
12660 Actual_Decls
: List_Id
) return List_Id
12662 A_Gen_T
: constant Entity_Id
:=
12663 Defining_Identifier
(Analyzed_Formal
);
12664 Def
: constant Node_Id
:= Formal_Type_Definition
(Formal
);
12665 Gen_T
: constant Entity_Id
:= Defining_Identifier
(Formal
);
12667 Ancestor
: Entity_Id
:= Empty
;
12668 Decl_Node
: Node_Id
;
12669 Decl_Nodes
: List_Id
;
12673 procedure Check_Shared_Variable_Control_Aspects
;
12674 -- Ada 2022: Verify that shared variable control aspects (RM C.6)
12675 -- that may be specified for a formal type are obeyed by the actual.
12677 procedure Diagnose_Predicated_Actual
;
12678 -- There are a number of constructs in which a discrete type with
12679 -- predicates is illegal, e.g. as an index in an array type declaration.
12680 -- If a generic type is used is such a construct in a generic package
12681 -- declaration, it carries the flag No_Predicate_On_Actual. it is part
12682 -- of the generic contract that the actual cannot have predicates.
12684 function Subtypes_Match
(Gen_T
, Act_T
: Entity_Id
) return Boolean;
12685 -- Check that base types are the same and that the subtypes match
12686 -- statically. Used in several of the validation subprograms for
12687 -- actuals in instantiations.
12689 procedure Validate_Array_Type_Instance
;
12690 procedure Validate_Access_Subprogram_Instance
;
12691 procedure Validate_Access_Type_Instance
;
12692 procedure Validate_Derived_Type_Instance
;
12693 procedure Validate_Derived_Interface_Type_Instance
;
12694 procedure Validate_Discriminated_Formal_Type
;
12695 procedure Validate_Interface_Type_Instance
;
12696 procedure Validate_Private_Type_Instance
;
12697 procedure Validate_Incomplete_Type_Instance
;
12698 -- These procedures perform validation tests for the named case.
12699 -- Validate_Discriminated_Formal_Type is shared by formal private
12700 -- types and Ada 2012 formal incomplete types.
12702 --------------------------------------------
12703 -- Check_Shared_Variable_Control_Aspects --
12704 --------------------------------------------
12706 -- Ada 2022: Verify that shared variable control aspects (RM C.6)
12707 -- that may be specified for the formal are obeyed by the actual.
12708 -- If the formal is a derived type the aspect specifications must match.
12709 -- NOTE: AI12-0282 implies that matching of aspects is required between
12710 -- formal and actual in all cases, but this is too restrictive.
12711 -- In particular it violates a language design rule: a limited private
12712 -- indefinite formal can be matched by any actual. The current code
12713 -- reflects an older and more permissive version of RM C.6 (12/5).
12715 procedure Check_Shared_Variable_Control_Aspects
is
12717 if Ada_Version
>= Ada_2022
then
12718 if Is_Atomic
(A_Gen_T
) and then not Is_Atomic
(Act_T
) then
12720 ("actual for& must have Atomic aspect", Actual
, A_Gen_T
);
12722 elsif Is_Derived_Type
(A_Gen_T
)
12723 and then Is_Atomic
(A_Gen_T
) /= Is_Atomic
(Act_T
)
12726 ("actual for& has different Atomic aspect", Actual
, A_Gen_T
);
12729 if Is_Volatile
(A_Gen_T
) and then not Is_Volatile
(Act_T
) then
12731 ("actual for& must have Volatile aspect",
12734 elsif Is_Derived_Type
(A_Gen_T
)
12735 and then Is_Volatile
(A_Gen_T
) /= Is_Volatile
(Act_T
)
12738 ("actual for& has different Volatile aspect",
12742 -- We assume that an array type whose atomic component type
12743 -- is Atomic is equivalent to an array type with the explicit
12744 -- aspect Has_Atomic_Components. This is a reasonable inference
12745 -- from the intent of AI12-0282, and makes it legal to use an
12746 -- actual that does not have the identical aspect as the formal.
12747 -- Ditto for volatile components.
12750 Actual_Atomic_Comp
: constant Boolean :=
12751 Has_Atomic_Components
(Act_T
)
12752 or else (Is_Array_Type
(Act_T
)
12753 and then Is_Atomic
(Component_Type
(Act_T
)));
12755 if Has_Atomic_Components
(A_Gen_T
) /= Actual_Atomic_Comp
then
12757 ("formal and actual for& must agree on atomic components",
12763 Actual_Volatile_Comp
: constant Boolean :=
12764 Has_Volatile_Components
(Act_T
)
12765 or else (Is_Array_Type
(Act_T
)
12766 and then Is_Volatile
(Component_Type
(Act_T
)));
12768 if Has_Volatile_Components
(A_Gen_T
) /= Actual_Volatile_Comp
12771 ("actual for& must have volatile components",
12776 -- The following two aspects do not require exact matching,
12777 -- but only one-way agreement. See RM C.6.
12779 if Is_Independent
(A_Gen_T
) and then not Is_Independent
(Act_T
)
12782 ("actual for& must have Independent aspect specified",
12786 if Has_Independent_Components
(A_Gen_T
)
12787 and then not Has_Independent_Components
(Act_T
)
12790 ("actual for& must have Independent_Components specified",
12794 -- Check actual/formal compatibility with respect to the four
12795 -- volatility refinement aspects.
12797 Check_Volatility_Compatibility
12799 "actual type", "its corresponding formal type",
12800 Srcpos_Bearer
=> Actual
);
12802 end Check_Shared_Variable_Control_Aspects
;
12804 ---------------------------------
12805 -- Diagnose_Predicated_Actual --
12806 ---------------------------------
12808 procedure Diagnose_Predicated_Actual
is
12810 if No_Predicate_On_Actual
(A_Gen_T
)
12811 and then Has_Predicates
(Act_T
)
12814 ("actual for& cannot be a type with predicate",
12815 Instantiation_Node
, A_Gen_T
);
12817 elsif No_Dynamic_Predicate_On_Actual
(A_Gen_T
)
12818 and then Has_Predicates
(Act_T
)
12819 and then not Has_Static_Predicate_Aspect
(Act_T
)
12822 ("actual for& cannot be a type with a dynamic predicate",
12823 Instantiation_Node
, A_Gen_T
);
12825 end Diagnose_Predicated_Actual
;
12827 --------------------
12828 -- Subtypes_Match --
12829 --------------------
12831 function Subtypes_Match
(Gen_T
, Act_T
: Entity_Id
) return Boolean is
12832 T
: constant Entity_Id
:= Get_Instance_Of
(Gen_T
);
12835 -- Check that the base types, root types (when dealing with class
12836 -- wide types), or designated types (when dealing with anonymous
12837 -- access types) of Gen_T and Act_T are statically matching subtypes.
12839 return ((Base_Type
(T
) = Act_T
12840 or else Base_Type
(T
) = Base_Type
(Act_T
))
12841 and then Subtypes_Statically_Match
(T
, Act_T
))
12843 or else (Is_Class_Wide_Type
(Gen_T
)
12844 and then Is_Class_Wide_Type
(Act_T
)
12845 and then Subtypes_Match
12846 (Get_Instance_Of
(Root_Type
(Gen_T
)),
12847 Root_Type
(Act_T
)))
12849 or else (Is_Anonymous_Access_Type
(Gen_T
)
12850 and then Ekind
(Act_T
) = Ekind
(Gen_T
)
12851 and then Subtypes_Statically_Match
12852 (Designated_Type
(Gen_T
), Designated_Type
(Act_T
)));
12853 end Subtypes_Match
;
12855 -----------------------------------------
12856 -- Validate_Access_Subprogram_Instance --
12857 -----------------------------------------
12859 procedure Validate_Access_Subprogram_Instance
is
12861 if not Is_Access_Type
(Act_T
)
12862 or else Ekind
(Designated_Type
(Act_T
)) /= E_Subprogram_Type
12865 ("expect access type in instantiation of &", Actual
, Gen_T
);
12866 Abandon_Instantiation
(Actual
);
12869 -- According to AI05-288, actuals for access_to_subprograms must be
12870 -- subtype conformant with the generic formal. Previous to AI05-288
12871 -- only mode conformance was required.
12873 -- This is a binding interpretation that applies to previous versions
12874 -- of the language, no need to maintain previous weaker checks.
12876 Check_Subtype_Conformant
12877 (Designated_Type
(Act_T
),
12878 Designated_Type
(A_Gen_T
),
12882 if Ekind
(Base_Type
(Act_T
)) = E_Access_Protected_Subprogram_Type
then
12883 if Ekind
(A_Gen_T
) = E_Access_Subprogram_Type
then
12885 ("protected access type not allowed for formal &",
12889 elsif Ekind
(A_Gen_T
) = E_Access_Protected_Subprogram_Type
then
12891 ("expect protected access type for formal &",
12895 -- If the formal has a specified convention (which in most cases
12896 -- will be StdCall) verify that the actual has the same convention.
12898 if Has_Convention_Pragma
(A_Gen_T
)
12899 and then Convention
(A_Gen_T
) /= Convention
(Act_T
)
12901 Error_Msg_Name_1
:= Get_Convention_Name
(Convention
(A_Gen_T
));
12903 ("actual for formal & must have convention %", Actual
, Gen_T
);
12906 if Can_Never_Be_Null
(A_Gen_T
) /= Can_Never_Be_Null
(Act_T
) then
12908 ("non null exclusion of actual and formal & do not match",
12911 end Validate_Access_Subprogram_Instance
;
12913 -----------------------------------
12914 -- Validate_Access_Type_Instance --
12915 -----------------------------------
12917 procedure Validate_Access_Type_Instance
is
12918 Desig_Type
: constant Entity_Id
:=
12919 Find_Actual_Type
(Designated_Type
(A_Gen_T
), A_Gen_T
);
12920 Desig_Act
: Entity_Id
;
12923 if not Is_Access_Type
(Act_T
) then
12925 ("expect access type in instantiation of &", Actual
, Gen_T
);
12926 Abandon_Instantiation
(Actual
);
12929 if Is_Access_Constant
(A_Gen_T
) then
12930 if not Is_Access_Constant
(Act_T
) then
12932 ("actual type must be access-to-constant type", Actual
);
12933 Abandon_Instantiation
(Actual
);
12936 if Is_Access_Constant
(Act_T
) then
12938 ("actual type must be access-to-variable type", Actual
);
12939 Abandon_Instantiation
(Actual
);
12941 elsif Ekind
(A_Gen_T
) = E_General_Access_Type
12942 and then Ekind
(Base_Type
(Act_T
)) /= E_General_Access_Type
12945 ("actual must be general access type!", Actual
);
12946 Error_Msg_NE
-- CODEFIX
12947 ("\add ALL to }!", Actual
, Act_T
);
12948 Abandon_Instantiation
(Actual
);
12952 -- The designated subtypes, that is to say the subtypes introduced
12953 -- by an access type declaration (and not by a subtype declaration)
12956 Desig_Act
:= Designated_Type
(Base_Type
(Act_T
));
12958 -- The designated type may have been introduced through a limited_
12959 -- with clause, in which case retrieve the non-limited view. This
12960 -- applies to incomplete types as well as to class-wide types.
12962 if From_Limited_With
(Desig_Act
) then
12963 Desig_Act
:= Available_View
(Desig_Act
);
12966 if not Subtypes_Match
(Desig_Type
, Desig_Act
) then
12968 ("designated type of actual does not match that of formal &",
12971 if not Predicates_Match
(Desig_Type
, Desig_Act
) then
12972 Error_Msg_N
("\predicates do not match", Actual
);
12975 Abandon_Instantiation
(Actual
);
12978 -- Ada 2005: null-exclusion indicators of the two types must agree
12980 if Can_Never_Be_Null
(A_Gen_T
) /= Can_Never_Be_Null
(Act_T
) then
12982 ("non null exclusion of actual and formal & do not match",
12985 end Validate_Access_Type_Instance
;
12987 ----------------------------------
12988 -- Validate_Array_Type_Instance --
12989 ----------------------------------
12991 procedure Validate_Array_Type_Instance
is
12996 function Formal_Dimensions
return Nat
;
12997 -- Count number of dimensions in array type formal
12999 -----------------------
13000 -- Formal_Dimensions --
13001 -----------------------
13003 function Formal_Dimensions
return Nat
is
13008 if Nkind
(Def
) = N_Constrained_Array_Definition
then
13009 Index
:= First
(Discrete_Subtype_Definitions
(Def
));
13011 Index
:= First
(Subtype_Marks
(Def
));
13014 while Present
(Index
) loop
13020 end Formal_Dimensions
;
13022 -- Start of processing for Validate_Array_Type_Instance
13025 if not Is_Array_Type
(Act_T
) then
13027 ("expect array type in instantiation of &", Actual
, Gen_T
);
13028 Abandon_Instantiation
(Actual
);
13030 elsif Nkind
(Def
) = N_Constrained_Array_Definition
then
13031 if not (Is_Constrained
(Act_T
)) then
13033 ("expect constrained array in instantiation of &",
13035 Abandon_Instantiation
(Actual
);
13039 if Is_Constrained
(Act_T
) then
13041 ("expect unconstrained array in instantiation of &",
13043 Abandon_Instantiation
(Actual
);
13047 if Formal_Dimensions
/= Number_Dimensions
(Act_T
) then
13049 ("dimensions of actual do not match formal &", Actual
, Gen_T
);
13050 Abandon_Instantiation
(Actual
);
13053 I1
:= First_Index
(A_Gen_T
);
13054 I2
:= First_Index
(Act_T
);
13055 for J
in 1 .. Formal_Dimensions
loop
13057 -- If the indexes of the actual were given by a subtype_mark,
13058 -- the index was transformed into a range attribute. Retrieve
13059 -- the original type mark for checking.
13061 if Is_Entity_Name
(Original_Node
(I2
)) then
13062 T2
:= Entity
(Original_Node
(I2
));
13067 if not Subtypes_Match
13068 (Find_Actual_Type
(Etype
(I1
), A_Gen_T
), T2
)
13071 ("index types of actual do not match those of formal &",
13073 Abandon_Instantiation
(Actual
);
13080 -- Check matching subtypes. Note that there are complex visibility
13081 -- issues when the generic is a child unit and some aspect of the
13082 -- generic type is declared in a parent unit of the generic. We do
13083 -- the test to handle this special case only after a direct check
13084 -- for static matching has failed. The case where both the component
13085 -- type and the array type are separate formals, and the component
13086 -- type is a private view may also require special checking in
13087 -- Subtypes_Match. Finally, we assume that a child instance where
13088 -- the component type comes from a formal of a parent instance is
13089 -- correct because the generic was correct. A more precise check
13090 -- seems too complex to install???
13093 (Component_Type
(A_Gen_T
), Component_Type
(Act_T
))
13096 (Find_Actual_Type
(Component_Type
(A_Gen_T
), A_Gen_T
),
13097 Component_Type
(Act_T
))
13099 (not Inside_A_Generic
13100 and then Is_Child_Unit
(Scope
(Component_Type
(A_Gen_T
))))
13105 ("component subtype of actual does not match that of formal &",
13107 Abandon_Instantiation
(Actual
);
13110 if Has_Aliased_Components
(A_Gen_T
)
13111 and then not Has_Aliased_Components
(Act_T
)
13114 ("actual must have aliased components to match formal type &",
13117 end Validate_Array_Type_Instance
;
13119 -----------------------------------------------
13120 -- Validate_Derived_Interface_Type_Instance --
13121 -----------------------------------------------
13123 procedure Validate_Derived_Interface_Type_Instance
is
13124 Par
: constant Entity_Id
:= Entity
(Subtype_Indication
(Def
));
13128 -- First apply interface instance checks
13130 Validate_Interface_Type_Instance
;
13132 -- Verify that immediate parent interface is an ancestor of
13136 and then not Interface_Present_In_Ancestor
(Act_T
, Par
)
13139 ("interface actual must include progenitor&", Actual
, Par
);
13142 -- Now verify that the actual includes all other ancestors of
13145 Elmt
:= First_Elmt
(Interfaces
(A_Gen_T
));
13146 while Present
(Elmt
) loop
13147 if not Interface_Present_In_Ancestor
13148 (Act_T
, Get_Instance_Of
(Node
(Elmt
)))
13151 ("interface actual must include progenitor&",
13152 Actual
, Node
(Elmt
));
13157 end Validate_Derived_Interface_Type_Instance
;
13159 ------------------------------------
13160 -- Validate_Derived_Type_Instance --
13161 ------------------------------------
13163 procedure Validate_Derived_Type_Instance
is
13164 Actual_Discr
: Entity_Id
;
13165 Ancestor_Discr
: Entity_Id
;
13168 -- Verify that the actual includes the progenitors of the formal,
13169 -- if any. The formal may depend on previous formals and their
13170 -- instance, so we must examine instance of interfaces if present.
13171 -- The actual may be an extension of an interface, in which case
13172 -- it does not appear in the interface list, so this must be
13173 -- checked separately.
13175 if Present
(Interface_List
(Def
)) then
13176 if not Has_Interfaces
(Act_T
) then
13178 ("actual must implement all interfaces of formal&",
13183 Act_Iface_List
: Elist_Id
;
13185 Iface_Ent
: Entity_Id
;
13187 function Instance_Exists
(I
: Entity_Id
) return Boolean;
13188 -- If the interface entity is declared in a generic unit,
13189 -- this can only be legal if we are within an instantiation
13190 -- of a child of that generic. There is currently no
13191 -- mechanism to relate an interface declared within a
13192 -- generic to the corresponding interface in an instance,
13193 -- so we traverse the list of interfaces of the actual,
13194 -- looking for a name match.
13196 ---------------------
13197 -- Instance_Exists --
13198 ---------------------
13200 function Instance_Exists
(I
: Entity_Id
) return Boolean is
13201 Iface_Elmt
: Elmt_Id
;
13204 Iface_Elmt
:= First_Elmt
(Act_Iface_List
);
13205 while Present
(Iface_Elmt
) loop
13206 if Is_Generic_Instance
(Scope
(Node
(Iface_Elmt
)))
13207 and then Chars
(Node
(Iface_Elmt
)) = Chars
(I
)
13212 Next_Elmt
(Iface_Elmt
);
13216 end Instance_Exists
;
13219 Iface
:= First
(Abstract_Interface_List
(A_Gen_T
));
13220 Collect_Interfaces
(Act_T
, Act_Iface_List
);
13222 while Present
(Iface
) loop
13223 Iface_Ent
:= Get_Instance_Of
(Entity
(Iface
));
13225 if Is_Ancestor
(Iface_Ent
, Act_T
)
13226 or else Is_Progenitor
(Iface_Ent
, Act_T
)
13230 elsif Ekind
(Scope
(Iface_Ent
)) = E_Generic_Package
13231 and then Instance_Exists
(Iface_Ent
)
13236 Error_Msg_Name_1
:= Chars
(Act_T
);
13238 ("actual% must implement interface&",
13239 Actual
, Etype
(Iface
));
13248 -- If the parent type in the generic declaration is itself a previous
13249 -- formal type, then it is local to the generic and absent from the
13250 -- analyzed generic definition. In that case the ancestor is the
13251 -- instance of the formal (which must have been instantiated
13252 -- previously), unless the ancestor is itself a formal derived type.
13253 -- In this latter case (which is the subject of Corrigendum 8652/0038
13254 -- (AI-202) the ancestor of the formals is the ancestor of its
13255 -- parent. Otherwise, the analyzed generic carries the parent type.
13256 -- If the parent type is defined in a previous formal package, then
13257 -- the scope of that formal package is that of the generic type
13258 -- itself, and it has already been mapped into the corresponding type
13259 -- in the actual package.
13261 -- Common case: parent type defined outside of the generic
13263 if Is_Entity_Name
(Subtype_Mark
(Def
))
13264 and then Present
(Entity
(Subtype_Mark
(Def
)))
13266 Ancestor
:= Get_Instance_Of
(Entity
(Subtype_Mark
(Def
)));
13268 -- Check whether parent is defined in a previous formal package
13271 Scope
(Scope
(Base_Type
(Etype
(A_Gen_T
)))) = Scope
(A_Gen_T
)
13274 Get_Instance_Of
(Base_Type
(Etype
(A_Gen_T
)));
13276 -- The type may be a local derivation, or a type extension of a
13277 -- previous formal, or of a formal of a parent package.
13279 elsif Is_Derived_Type
(Get_Instance_Of
(A_Gen_T
))
13281 Ekind
(Get_Instance_Of
(A_Gen_T
)) = E_Record_Type_With_Private
13283 -- Check whether the parent is another derived formal type in the
13284 -- same generic unit.
13286 if Etype
(A_Gen_T
) /= A_Gen_T
13287 and then Is_Generic_Type
(Etype
(A_Gen_T
))
13288 and then Scope
(Etype
(A_Gen_T
)) = Scope
(A_Gen_T
)
13289 and then Etype
(Etype
(A_Gen_T
)) /= Etype
(A_Gen_T
)
13291 -- Locate ancestor of parent from the subtype declaration
13292 -- created for the actual.
13298 Decl
:= First
(Actual_Decls
);
13299 while Present
(Decl
) loop
13300 if Nkind
(Decl
) = N_Subtype_Declaration
13301 and then Chars
(Defining_Identifier
(Decl
)) =
13302 Chars
(Etype
(A_Gen_T
))
13304 Ancestor
:= Generic_Parent_Type
(Decl
);
13312 pragma Assert
(Present
(Ancestor
));
13314 -- The ancestor itself may be a previous formal that has been
13317 Ancestor
:= Get_Instance_Of
(Ancestor
);
13321 Get_Instance_Of
(Base_Type
(Get_Instance_Of
(A_Gen_T
)));
13324 -- Check whether parent is a previous formal of the current generic
13326 elsif Is_Derived_Type
(A_Gen_T
)
13327 and then Is_Generic_Type
(Etype
(A_Gen_T
))
13328 and then Scope
(A_Gen_T
) = Scope
(Etype
(A_Gen_T
))
13330 Ancestor
:= Get_Instance_Of
(First_Subtype
(Etype
(A_Gen_T
)));
13332 -- An unusual case: the actual is a type declared in a parent unit,
13333 -- but is not a formal type so there is no instance_of for it.
13334 -- Retrieve it by analyzing the record extension.
13336 elsif Is_Child_Unit
(Scope
(A_Gen_T
))
13337 and then In_Open_Scopes
(Scope
(Act_T
))
13338 and then Is_Generic_Instance
(Scope
(Act_T
))
13340 Analyze
(Subtype_Mark
(Def
));
13341 Ancestor
:= Entity
(Subtype_Mark
(Def
));
13344 Ancestor
:= Get_Instance_Of
(Etype
(Base_Type
(A_Gen_T
)));
13347 -- If the formal derived type has pragma Preelaborable_Initialization
13348 -- then the actual type must have preelaborable initialization.
13350 if Known_To_Have_Preelab_Init
(A_Gen_T
)
13351 and then not Has_Preelaborable_Initialization
(Act_T
)
13354 ("actual for & must have preelaborable initialization",
13358 -- Ada 2005 (AI-251)
13360 if Ada_Version
>= Ada_2005
and then Is_Interface
(Ancestor
) then
13361 if not Interface_Present_In_Ancestor
(Act_T
, Ancestor
) then
13363 ("(Ada 2005) expected type implementing & in instantiation",
13367 -- Finally verify that the (instance of) the ancestor is an ancestor
13370 elsif not Is_Ancestor
(Base_Type
(Ancestor
), Act_T
) then
13372 ("expect type derived from & in instantiation",
13373 Actual
, First_Subtype
(Ancestor
));
13374 Abandon_Instantiation
(Actual
);
13377 -- Ada 2005 (AI-443): Synchronized formal derived type checks. Note
13378 -- that the formal type declaration has been rewritten as a private
13381 if Ada_Version
>= Ada_2005
13382 and then Nkind
(Parent
(A_Gen_T
)) = N_Private_Extension_Declaration
13383 and then Synchronized_Present
(Parent
(A_Gen_T
))
13385 -- The actual must be a synchronized tagged type
13387 if not Is_Tagged_Type
(Act_T
) then
13389 ("actual of synchronized type must be tagged", Actual
);
13390 Abandon_Instantiation
(Actual
);
13392 elsif Nkind
(Parent
(Act_T
)) = N_Full_Type_Declaration
13393 and then Nkind
(Type_Definition
(Parent
(Act_T
))) =
13394 N_Derived_Type_Definition
13395 and then not Synchronized_Present
13396 (Type_Definition
(Parent
(Act_T
)))
13399 ("actual of synchronized type must be synchronized", Actual
);
13400 Abandon_Instantiation
(Actual
);
13404 -- Perform atomic/volatile checks (RM C.6(12)). Note that AI05-0218-1
13405 -- removes the second instance of the phrase "or allow pass by copy".
13407 -- For Ada 2022, the aspect may be specified explicitly for the
13408 -- formal regardless of whether an ancestor obeys it.
13410 if Is_Atomic
(Act_T
)
13411 and then not Is_Atomic
(Ancestor
)
13412 and then not Is_Atomic
(A_Gen_T
)
13415 ("cannot have atomic actual type for non-atomic formal type",
13418 elsif Is_Volatile
(Act_T
)
13419 and then not Is_Volatile
(Ancestor
)
13420 and then not Is_Volatile
(A_Gen_T
)
13423 ("cannot have volatile actual type for non-volatile formal type",
13427 -- It should not be necessary to check for unknown discriminants on
13428 -- Formal, but for some reason Has_Unknown_Discriminants is false for
13429 -- A_Gen_T, so Is_Definite_Subtype incorrectly returns True. This
13430 -- needs fixing. ???
13432 if Is_Definite_Subtype
(A_Gen_T
)
13433 and then not Unknown_Discriminants_Present
(Formal
)
13434 and then not Is_Definite_Subtype
(Act_T
)
13436 Error_Msg_N
("actual subtype must be constrained", Actual
);
13437 Abandon_Instantiation
(Actual
);
13440 if not Unknown_Discriminants_Present
(Formal
) then
13441 if Is_Constrained
(Ancestor
) then
13442 if not Is_Constrained
(Act_T
) then
13443 Error_Msg_N
("actual subtype must be constrained", Actual
);
13444 Abandon_Instantiation
(Actual
);
13447 -- Ancestor is unconstrained, Check if generic formal and actual
13448 -- agree on constrainedness. The check only applies to array types
13449 -- and discriminated types.
13451 elsif Is_Constrained
(Act_T
) then
13452 if Ekind
(Ancestor
) = E_Access_Type
13453 or else (not Is_Constrained
(A_Gen_T
)
13454 and then Is_Composite_Type
(A_Gen_T
))
13456 Error_Msg_N
("actual subtype must be unconstrained", Actual
);
13457 Abandon_Instantiation
(Actual
);
13460 -- A class-wide type is only allowed if the formal has unknown
13463 elsif Is_Class_Wide_Type
(Act_T
)
13464 and then not Has_Unknown_Discriminants
(Ancestor
)
13467 ("actual for & cannot be a class-wide type", Actual
, Gen_T
);
13468 Abandon_Instantiation
(Actual
);
13470 -- Otherwise, the formal and actual must have the same number
13471 -- of discriminants and each discriminant of the actual must
13472 -- correspond to a discriminant of the formal.
13474 elsif Has_Discriminants
(Act_T
)
13475 and then not Has_Unknown_Discriminants
(Act_T
)
13476 and then Has_Discriminants
(Ancestor
)
13478 Actual_Discr
:= First_Discriminant
(Act_T
);
13479 Ancestor_Discr
:= First_Discriminant
(Ancestor
);
13480 while Present
(Actual_Discr
)
13481 and then Present
(Ancestor_Discr
)
13483 if Base_Type
(Act_T
) /= Base_Type
(Ancestor
) and then
13484 No
(Corresponding_Discriminant
(Actual_Discr
))
13487 ("discriminant & does not correspond "
13488 & "to ancestor discriminant", Actual
, Actual_Discr
);
13489 Abandon_Instantiation
(Actual
);
13492 Next_Discriminant
(Actual_Discr
);
13493 Next_Discriminant
(Ancestor_Discr
);
13496 if Present
(Actual_Discr
) or else Present
(Ancestor_Discr
) then
13498 ("actual for & must have same number of discriminants",
13500 Abandon_Instantiation
(Actual
);
13503 -- This case should be caught by the earlier check for
13504 -- constrainedness, but the check here is added for completeness.
13506 elsif Has_Discriminants
(Act_T
)
13507 and then not Has_Unknown_Discriminants
(Act_T
)
13510 ("actual for & must not have discriminants", Actual
, Gen_T
);
13511 Abandon_Instantiation
(Actual
);
13513 elsif Has_Discriminants
(Ancestor
) then
13515 ("actual for & must have known discriminants", Actual
, Gen_T
);
13516 Abandon_Instantiation
(Actual
);
13519 if not Subtypes_Statically_Compatible
13520 (Act_T
, Ancestor
, Formal_Derived_Matching
=> True)
13523 ("actual for & must be statically compatible with ancestor",
13526 if not Predicates_Compatible
(Act_T
, Ancestor
) then
13528 ("\predicate on actual is not compatible with ancestor",
13532 Abandon_Instantiation
(Actual
);
13536 -- If the formal and actual types are abstract, check that there
13537 -- are no abstract primitives of the actual type that correspond to
13538 -- nonabstract primitives of the formal type (second sentence of
13541 if Is_Abstract_Type
(A_Gen_T
) and then Is_Abstract_Type
(Act_T
) then
13542 Check_Abstract_Primitives
: declare
13543 Gen_Prims
: constant Elist_Id
:=
13544 Primitive_Operations
(A_Gen_T
);
13545 Gen_Elmt
: Elmt_Id
;
13546 Gen_Subp
: Entity_Id
;
13547 Anc_Subp
: Entity_Id
;
13548 Anc_Formal
: Entity_Id
;
13549 Anc_F_Type
: Entity_Id
;
13551 Act_Prims
: constant Elist_Id
:= Primitive_Operations
(Act_T
);
13552 Act_Elmt
: Elmt_Id
;
13553 Act_Subp
: Entity_Id
;
13554 Act_Formal
: Entity_Id
;
13555 Act_F_Type
: Entity_Id
;
13557 Subprograms_Correspond
: Boolean;
13559 function Is_Tagged_Ancestor
(T1
, T2
: Entity_Id
) return Boolean;
13560 -- Returns true if T2 is derived directly or indirectly from
13561 -- T1, including derivations from interfaces. T1 and T2 are
13562 -- required to be specific tagged base types.
13564 ------------------------
13565 -- Is_Tagged_Ancestor --
13566 ------------------------
13568 function Is_Tagged_Ancestor
(T1
, T2
: Entity_Id
) return Boolean
13570 Intfc_Elmt
: Elmt_Id
;
13573 -- The predicate is satisfied if the types are the same
13578 -- If we've reached the top of the derivation chain then
13579 -- we know that T1 is not an ancestor of T2.
13581 elsif Etype
(T2
) = T2
then
13584 -- Proceed to check T2's immediate parent
13586 elsif Is_Ancestor
(T1
, Base_Type
(Etype
(T2
))) then
13589 -- Finally, check to see if T1 is an ancestor of any of T2's
13593 Intfc_Elmt
:= First_Elmt
(Interfaces
(T2
));
13594 while Present
(Intfc_Elmt
) loop
13595 if Is_Ancestor
(T1
, Node
(Intfc_Elmt
)) then
13599 Next_Elmt
(Intfc_Elmt
);
13604 end Is_Tagged_Ancestor
;
13606 -- Start of processing for Check_Abstract_Primitives
13609 -- Loop over all of the formal derived type's primitives
13611 Gen_Elmt
:= First_Elmt
(Gen_Prims
);
13612 while Present
(Gen_Elmt
) loop
13613 Gen_Subp
:= Node
(Gen_Elmt
);
13615 -- If the primitive of the formal is not abstract, then
13616 -- determine whether there is a corresponding primitive of
13617 -- the actual type that's abstract.
13619 if not Is_Abstract_Subprogram
(Gen_Subp
) then
13620 Act_Elmt
:= First_Elmt
(Act_Prims
);
13621 while Present
(Act_Elmt
) loop
13622 Act_Subp
:= Node
(Act_Elmt
);
13624 -- If we find an abstract primitive of the actual,
13625 -- then we need to test whether it corresponds to the
13626 -- subprogram from which the generic formal primitive
13629 if Is_Abstract_Subprogram
(Act_Subp
) then
13630 Anc_Subp
:= Alias
(Gen_Subp
);
13632 -- Test whether we have a corresponding primitive
13633 -- by comparing names, kinds, formal types, and
13636 if Chars
(Anc_Subp
) = Chars
(Act_Subp
)
13637 and then Ekind
(Anc_Subp
) = Ekind
(Act_Subp
)
13639 Anc_Formal
:= First_Formal
(Anc_Subp
);
13640 Act_Formal
:= First_Formal
(Act_Subp
);
13641 while Present
(Anc_Formal
)
13642 and then Present
(Act_Formal
)
13644 Anc_F_Type
:= Etype
(Anc_Formal
);
13645 Act_F_Type
:= Etype
(Act_Formal
);
13647 if Ekind
(Anc_F_Type
) =
13648 E_Anonymous_Access_Type
13650 Anc_F_Type
:= Designated_Type
(Anc_F_Type
);
13652 if Ekind
(Act_F_Type
) =
13653 E_Anonymous_Access_Type
13656 Designated_Type
(Act_F_Type
);
13662 Ekind
(Act_F_Type
) = E_Anonymous_Access_Type
13667 Anc_F_Type
:= Base_Type
(Anc_F_Type
);
13668 Act_F_Type
:= Base_Type
(Act_F_Type
);
13670 -- If the formal is controlling, then the
13671 -- the type of the actual primitive's formal
13672 -- must be derived directly or indirectly
13673 -- from the type of the ancestor primitive's
13676 if Is_Controlling_Formal
(Anc_Formal
) then
13677 if not Is_Tagged_Ancestor
13678 (Anc_F_Type
, Act_F_Type
)
13683 -- Otherwise the types of the formals must
13686 elsif Anc_F_Type
/= Act_F_Type
then
13690 Next_Formal
(Anc_Formal
);
13691 Next_Formal
(Act_Formal
);
13694 -- If we traversed through all of the formals
13695 -- then so far the subprograms correspond, so
13696 -- now check that any result types correspond.
13698 if No
(Anc_Formal
) and then No
(Act_Formal
) then
13699 Subprograms_Correspond
:= True;
13701 if Ekind
(Act_Subp
) = E_Function
then
13702 Anc_F_Type
:= Etype
(Anc_Subp
);
13703 Act_F_Type
:= Etype
(Act_Subp
);
13705 if Ekind
(Anc_F_Type
) =
13706 E_Anonymous_Access_Type
13709 Designated_Type
(Anc_F_Type
);
13711 if Ekind
(Act_F_Type
) =
13712 E_Anonymous_Access_Type
13715 Designated_Type
(Act_F_Type
);
13717 Subprograms_Correspond
:= False;
13722 = E_Anonymous_Access_Type
13724 Subprograms_Correspond
:= False;
13727 Anc_F_Type
:= Base_Type
(Anc_F_Type
);
13728 Act_F_Type
:= Base_Type
(Act_F_Type
);
13730 -- Now either the result types must be
13731 -- the same or, if the result type is
13732 -- controlling, the result type of the
13733 -- actual primitive must descend from the
13734 -- result type of the ancestor primitive.
13736 if Subprograms_Correspond
13737 and then Anc_F_Type
/= Act_F_Type
13739 Has_Controlling_Result
(Anc_Subp
)
13740 and then not Is_Tagged_Ancestor
13741 (Anc_F_Type
, Act_F_Type
)
13743 Subprograms_Correspond
:= False;
13747 -- Found a matching subprogram belonging to
13748 -- formal ancestor type, so actual subprogram
13749 -- corresponds and this violates 3.9.3(9).
13751 if Subprograms_Correspond
then
13753 ("abstract subprogram & overrides "
13754 & "nonabstract subprogram of ancestor",
13761 Next_Elmt
(Act_Elmt
);
13765 Next_Elmt
(Gen_Elmt
);
13767 end Check_Abstract_Primitives
;
13770 -- Verify that limitedness matches. If parent is a limited
13771 -- interface then the generic formal is not unless declared
13772 -- explicitly so. If not declared limited, the actual cannot be
13773 -- limited (see AI05-0087).
13775 if Is_Limited_Type
(Act_T
) and then not Is_Limited_Type
(A_Gen_T
) then
13776 if not In_Instance
then
13778 ("actual for non-limited & cannot be a limited type",
13780 Explain_Limited_Type
(Act_T
, Actual
);
13781 Abandon_Instantiation
(Actual
);
13785 -- Check for AI12-0036
13788 Formal_Is_Private_Extension
: constant Boolean :=
13789 Nkind
(Parent
(A_Gen_T
)) = N_Private_Extension_Declaration
;
13791 Actual_Is_Tagged
: constant Boolean := Is_Tagged_Type
(Act_T
);
13794 if Actual_Is_Tagged
/= Formal_Is_Private_Extension
then
13795 if not In_Instance
then
13796 if Actual_Is_Tagged
then
13798 ("actual for & cannot be a tagged type", Actual
, Gen_T
);
13801 ("actual for & must be a tagged type", Actual
, Gen_T
);
13804 Abandon_Instantiation
(Actual
);
13808 end Validate_Derived_Type_Instance
;
13810 ----------------------------------------
13811 -- Validate_Discriminated_Formal_Type --
13812 ----------------------------------------
13814 procedure Validate_Discriminated_Formal_Type
is
13815 Formal_Discr
: Entity_Id
;
13816 Actual_Discr
: Entity_Id
;
13817 Formal_Subt
: Entity_Id
;
13820 if Has_Discriminants
(A_Gen_T
) then
13821 if not Has_Discriminants
(Act_T
) then
13823 ("actual for & must have discriminants", Actual
, Gen_T
);
13824 Abandon_Instantiation
(Actual
);
13826 elsif Is_Constrained
(Act_T
) then
13828 ("actual for & must be unconstrained", Actual
, Gen_T
);
13829 Abandon_Instantiation
(Actual
);
13832 Formal_Discr
:= First_Discriminant
(A_Gen_T
);
13833 Actual_Discr
:= First_Discriminant
(Act_T
);
13834 while Formal_Discr
/= Empty
loop
13835 if Actual_Discr
= Empty
then
13837 ("discriminants on actual do not match formal",
13839 Abandon_Instantiation
(Actual
);
13842 Formal_Subt
:= Get_Instance_Of
(Etype
(Formal_Discr
));
13844 -- Access discriminants match if designated types do
13846 if Ekind
(Base_Type
(Formal_Subt
)) = E_Anonymous_Access_Type
13847 and then (Ekind
(Base_Type
(Etype
(Actual_Discr
)))) =
13848 E_Anonymous_Access_Type
13851 (Designated_Type
(Base_Type
(Formal_Subt
))) =
13852 Designated_Type
(Base_Type
(Etype
(Actual_Discr
)))
13856 elsif Base_Type
(Formal_Subt
) /=
13857 Base_Type
(Etype
(Actual_Discr
))
13860 ("types of actual discriminants must match formal",
13862 Abandon_Instantiation
(Actual
);
13864 elsif not Subtypes_Statically_Match
13865 (Formal_Subt
, Etype
(Actual_Discr
))
13866 and then Ada_Version
>= Ada_95
13869 ("subtypes of actual discriminants must match formal",
13871 Abandon_Instantiation
(Actual
);
13874 Next_Discriminant
(Formal_Discr
);
13875 Next_Discriminant
(Actual_Discr
);
13878 if Actual_Discr
/= Empty
then
13880 ("discriminants on actual do not match formal",
13882 Abandon_Instantiation
(Actual
);
13886 end Validate_Discriminated_Formal_Type
;
13888 ---------------------------------------
13889 -- Validate_Incomplete_Type_Instance --
13890 ---------------------------------------
13892 procedure Validate_Incomplete_Type_Instance
is
13894 if not Is_Tagged_Type
(Act_T
)
13895 and then Is_Tagged_Type
(A_Gen_T
)
13898 ("actual for & must be a tagged type", Actual
, Gen_T
);
13901 Validate_Discriminated_Formal_Type
;
13902 end Validate_Incomplete_Type_Instance
;
13904 --------------------------------------
13905 -- Validate_Interface_Type_Instance --
13906 --------------------------------------
13908 procedure Validate_Interface_Type_Instance
is
13910 if not Is_Interface
(Act_T
) then
13912 ("actual for formal interface type must be an interface",
13915 elsif Is_Limited_Type
(Act_T
) /= Is_Limited_Type
(A_Gen_T
)
13916 or else Is_Task_Interface
(A_Gen_T
) /= Is_Task_Interface
(Act_T
)
13917 or else Is_Protected_Interface
(A_Gen_T
) /=
13918 Is_Protected_Interface
(Act_T
)
13919 or else Is_Synchronized_Interface
(A_Gen_T
) /=
13920 Is_Synchronized_Interface
(Act_T
)
13923 ("actual for interface& does not match (RM 12.5.5(4))",
13926 end Validate_Interface_Type_Instance
;
13928 ------------------------------------
13929 -- Validate_Private_Type_Instance --
13930 ------------------------------------
13932 procedure Validate_Private_Type_Instance
is
13934 if Is_Limited_Type
(Act_T
)
13935 and then not Is_Limited_Type
(A_Gen_T
)
13937 if In_Instance
then
13941 ("actual for non-limited & cannot be a limited type", Actual
,
13943 Explain_Limited_Type
(Act_T
, Actual
);
13944 Abandon_Instantiation
(Actual
);
13947 elsif Known_To_Have_Preelab_Init
(A_Gen_T
)
13948 and then not Has_Preelaborable_Initialization
(Act_T
)
13951 ("actual for & must have preelaborable initialization", Actual
,
13954 elsif not Is_Definite_Subtype
(Act_T
)
13955 and then Is_Definite_Subtype
(A_Gen_T
)
13956 and then Ada_Version
>= Ada_95
13959 ("actual for & must be a definite subtype", Actual
, Gen_T
);
13961 elsif not Is_Tagged_Type
(Act_T
)
13962 and then Is_Tagged_Type
(A_Gen_T
)
13965 ("actual for & must be a tagged type", Actual
, Gen_T
);
13968 Validate_Discriminated_Formal_Type
;
13970 end Validate_Private_Type_Instance
;
13972 -- Start of processing for Instantiate_Type
13975 if Get_Instance_Of
(A_Gen_T
) /= A_Gen_T
then
13976 Error_Msg_N
("duplicate instantiation of generic type", Actual
);
13977 return New_List
(Error
);
13979 elsif not Is_Entity_Name
(Actual
)
13980 or else not Is_Type
(Entity
(Actual
))
13983 ("expect valid subtype mark to instantiate &", Actual
, Gen_T
);
13984 Abandon_Instantiation
(Actual
);
13987 Act_T
:= Entity
(Actual
);
13989 -- Ada 2005 (AI-216): An Unchecked_Union subtype shall only be passed
13990 -- as a generic actual parameter if the corresponding formal type
13991 -- does not have a known_discriminant_part, or is a formal derived
13992 -- type that is an Unchecked_Union type.
13994 if Is_Unchecked_Union
(Base_Type
(Act_T
)) then
13995 if not Has_Discriminants
(A_Gen_T
)
13996 or else (Is_Derived_Type
(A_Gen_T
)
13997 and then Is_Unchecked_Union
(A_Gen_T
))
14001 Error_Msg_N
("unchecked union cannot be the actual for a "
14002 & "discriminated formal type", Act_T
);
14007 -- Deal with fixed/floating restrictions
14009 if Is_Floating_Point_Type
(Act_T
) then
14010 Check_Restriction
(No_Floating_Point
, Actual
);
14011 elsif Is_Fixed_Point_Type
(Act_T
) then
14012 Check_Restriction
(No_Fixed_Point
, Actual
);
14015 -- Deal with error of using incomplete type as generic actual.
14016 -- This includes limited views of a type, even if the non-limited
14017 -- view may be available.
14019 if Ekind
(Act_T
) = E_Incomplete_Type
14020 or else (Is_Class_Wide_Type
(Act_T
)
14021 and then Ekind
(Root_Type
(Act_T
)) = E_Incomplete_Type
)
14023 -- If the formal is an incomplete type, the actual can be
14024 -- incomplete as well, but if an actual incomplete type has
14025 -- a full view, then we'll retrieve that.
14027 if Ekind
(A_Gen_T
) = E_Incomplete_Type
14028 and then not Present
(Full_View
(Act_T
))
14032 elsif Is_Class_Wide_Type
(Act_T
)
14033 or else No
(Full_View
(Act_T
))
14035 Error_Msg_N
("premature use of incomplete type", Actual
);
14036 Abandon_Instantiation
(Actual
);
14039 Act_T
:= Full_View
(Act_T
);
14040 Set_Entity
(Actual
, Act_T
);
14042 if Has_Private_Component
(Act_T
) then
14044 ("premature use of type with private component", Actual
);
14048 -- Deal with error of premature use of private type as generic actual
14050 elsif Is_Private_Type
(Act_T
)
14051 and then Is_Private_Type
(Base_Type
(Act_T
))
14052 and then not Is_Generic_Type
(Act_T
)
14053 and then not Is_Derived_Type
(Act_T
)
14054 and then No
(Full_View
(Root_Type
(Act_T
)))
14056 -- If the formal is an incomplete type, the actual can be
14057 -- private or incomplete as well.
14059 if Ekind
(A_Gen_T
) = E_Incomplete_Type
then
14062 Error_Msg_N
("premature use of private type", Actual
);
14065 elsif Has_Private_Component
(Act_T
) then
14067 ("premature use of type with private component", Actual
);
14070 Set_Instance_Of
(A_Gen_T
, Act_T
);
14072 -- If the type is generic, the class-wide type may also be used
14074 if Is_Tagged_Type
(A_Gen_T
)
14075 and then Is_Tagged_Type
(Act_T
)
14076 and then not Is_Class_Wide_Type
(A_Gen_T
)
14078 Set_Instance_Of
(Class_Wide_Type
(A_Gen_T
),
14079 Class_Wide_Type
(Act_T
));
14082 if not Is_Abstract_Type
(A_Gen_T
)
14083 and then Is_Abstract_Type
(Act_T
)
14086 ("actual of non-abstract formal cannot be abstract", Actual
);
14089 -- A generic scalar type is a first subtype for which we generate
14090 -- an anonymous base type. Indicate that the instance of this base
14091 -- is the base type of the actual.
14093 if Is_Scalar_Type
(A_Gen_T
) then
14094 Set_Instance_Of
(Etype
(A_Gen_T
), Etype
(Act_T
));
14098 Check_Shared_Variable_Control_Aspects
;
14100 if Error_Posted
(Act_T
) then
14103 case Nkind
(Def
) is
14104 when N_Formal_Private_Type_Definition
=>
14105 Validate_Private_Type_Instance
;
14107 when N_Formal_Incomplete_Type_Definition
=>
14108 Validate_Incomplete_Type_Instance
;
14110 when N_Formal_Derived_Type_Definition
=>
14111 Validate_Derived_Type_Instance
;
14113 when N_Formal_Discrete_Type_Definition
=>
14114 if not Is_Discrete_Type
(Act_T
) then
14116 ("expect discrete type in instantiation of&",
14118 Abandon_Instantiation
(Actual
);
14121 Diagnose_Predicated_Actual
;
14123 when N_Formal_Signed_Integer_Type_Definition
=>
14124 if not Is_Signed_Integer_Type
(Act_T
) then
14126 ("expect signed integer type in instantiation of&",
14128 Abandon_Instantiation
(Actual
);
14131 Diagnose_Predicated_Actual
;
14133 when N_Formal_Modular_Type_Definition
=>
14134 if not Is_Modular_Integer_Type
(Act_T
) then
14136 ("expect modular type in instantiation of &",
14138 Abandon_Instantiation
(Actual
);
14141 Diagnose_Predicated_Actual
;
14143 when N_Formal_Floating_Point_Definition
=>
14144 if not Is_Floating_Point_Type
(Act_T
) then
14146 ("expect float type in instantiation of &", Actual
, Gen_T
);
14147 Abandon_Instantiation
(Actual
);
14150 when N_Formal_Ordinary_Fixed_Point_Definition
=>
14151 if not Is_Ordinary_Fixed_Point_Type
(Act_T
) then
14153 ("expect ordinary fixed point type in instantiation of &",
14155 Abandon_Instantiation
(Actual
);
14158 when N_Formal_Decimal_Fixed_Point_Definition
=>
14159 if not Is_Decimal_Fixed_Point_Type
(Act_T
) then
14161 ("expect decimal type in instantiation of &",
14163 Abandon_Instantiation
(Actual
);
14166 when N_Array_Type_Definition
=>
14167 Validate_Array_Type_Instance
;
14169 when N_Access_To_Object_Definition
=>
14170 Validate_Access_Type_Instance
;
14172 when N_Access_Function_Definition
14173 | N_Access_Procedure_Definition
14175 Validate_Access_Subprogram_Instance
;
14177 when N_Record_Definition
=>
14178 Validate_Interface_Type_Instance
;
14180 when N_Derived_Type_Definition
=>
14181 Validate_Derived_Interface_Type_Instance
;
14184 raise Program_Error
;
14188 Subt
:= New_Copy
(Gen_T
);
14190 -- Use adjusted sloc of subtype name as the location for other nodes in
14191 -- the subtype declaration.
14193 Loc
:= Sloc
(Subt
);
14196 Make_Subtype_Declaration
(Loc
,
14197 Defining_Identifier
=> Subt
,
14198 Subtype_Indication
=> New_Occurrence_Of
(Act_T
, Loc
));
14200 -- Record whether the actual is private at this point, so that
14201 -- Check_Generic_Actuals can restore its proper view before the
14202 -- semantic analysis of the instance.
14204 if Is_Private_Type
(Act_T
) then
14205 Set_Has_Private_View
(Subtype_Indication
(Decl_Node
));
14208 -- In Ada 2012 the actual may be a limited view. Indicate that
14209 -- the local subtype must be treated as such.
14211 if From_Limited_With
(Act_T
) then
14212 Mutate_Ekind
(Subt
, E_Incomplete_Subtype
);
14213 Set_From_Limited_With
(Subt
);
14216 Decl_Nodes
:= New_List
(Decl_Node
);
14218 -- Flag actual derived types so their elaboration produces the
14219 -- appropriate renamings for the primitive operations of the ancestor.
14220 -- Flag actual for formal private types as well, to determine whether
14221 -- operations in the private part may override inherited operations.
14222 -- If the formal has an interface list, the ancestor is not the
14223 -- parent, but the analyzed formal that includes the interface
14224 -- operations of all its progenitors.
14226 -- Same treatment for formal private types, so we can check whether the
14227 -- type is tagged limited when validating derivations in the private
14228 -- part. (See AI05-096).
14230 if Nkind
(Def
) = N_Formal_Derived_Type_Definition
then
14231 if Present
(Interface_List
(Def
)) then
14232 Set_Generic_Parent_Type
(Decl_Node
, A_Gen_T
);
14234 Set_Generic_Parent_Type
(Decl_Node
, Ancestor
);
14237 elsif Nkind
(Def
) in N_Formal_Private_Type_Definition
14238 | N_Formal_Incomplete_Type_Definition
14240 Set_Generic_Parent_Type
(Decl_Node
, A_Gen_T
);
14243 -- If the actual is a synchronized type that implements an interface,
14244 -- the primitive operations are attached to the corresponding record,
14245 -- and we have to treat it as an additional generic actual, so that its
14246 -- primitive operations become visible in the instance. The task or
14247 -- protected type itself does not carry primitive operations.
14249 if Is_Concurrent_Type
(Act_T
)
14250 and then Is_Tagged_Type
(Act_T
)
14251 and then Present
(Corresponding_Record_Type
(Act_T
))
14252 and then Present
(Ancestor
)
14253 and then Is_Interface
(Ancestor
)
14256 Corr_Rec
: constant Entity_Id
:=
14257 Corresponding_Record_Type
(Act_T
);
14258 New_Corr
: Entity_Id
;
14259 Corr_Decl
: Node_Id
;
14262 New_Corr
:= Make_Temporary
(Loc
, 'S');
14264 Make_Subtype_Declaration
(Loc
,
14265 Defining_Identifier
=> New_Corr
,
14266 Subtype_Indication
=>
14267 New_Occurrence_Of
(Corr_Rec
, Loc
));
14268 Append_To
(Decl_Nodes
, Corr_Decl
);
14270 if Ekind
(Act_T
) = E_Task_Type
then
14271 Mutate_Ekind
(Subt
, E_Task_Subtype
);
14273 Mutate_Ekind
(Subt
, E_Protected_Subtype
);
14276 Set_Corresponding_Record_Type
(Subt
, Corr_Rec
);
14277 Set_Generic_Parent_Type
(Corr_Decl
, Ancestor
);
14278 Set_Generic_Parent_Type
(Decl_Node
, Empty
);
14282 -- For a floating-point type, capture dimension info if any, because
14283 -- the generated subtype declaration does not come from source and
14284 -- will not process dimensions.
14286 if Is_Floating_Point_Type
(Act_T
) then
14287 Copy_Dimensions
(Act_T
, Subt
);
14291 end Instantiate_Type
;
14293 -----------------------------
14294 -- Is_Abbreviated_Instance --
14295 -----------------------------
14297 function Is_Abbreviated_Instance
(E
: Entity_Id
) return Boolean is
14299 return Ekind
(E
) = E_Package
14300 and then Present
(Hidden_In_Formal_Instance
(E
));
14301 end Is_Abbreviated_Instance
;
14303 ---------------------
14304 -- Is_In_Main_Unit --
14305 ---------------------
14307 function Is_In_Main_Unit
(N
: Node_Id
) return Boolean is
14308 Unum
: constant Unit_Number_Type
:= Get_Source_Unit
(N
);
14309 Current_Unit
: Node_Id
;
14312 if Unum
= Main_Unit
then
14315 -- If the current unit is a subunit then it is either the main unit or
14316 -- is being compiled as part of the main unit.
14318 elsif Nkind
(N
) = N_Compilation_Unit
then
14319 return Nkind
(Unit
(N
)) = N_Subunit
;
14322 Current_Unit
:= Parent
(N
);
14323 while Present
(Current_Unit
)
14324 and then Nkind
(Current_Unit
) /= N_Compilation_Unit
14326 Current_Unit
:= Parent
(Current_Unit
);
14329 -- The instantiation node is in the main unit, or else the current node
14330 -- (perhaps as the result of nested instantiations) is in the main unit,
14331 -- or in the declaration of the main unit, which in this last case must
14335 Current_Unit
= Cunit
(Main_Unit
)
14336 or else Current_Unit
= Library_Unit
(Cunit
(Main_Unit
))
14337 or else (Present
(Current_Unit
)
14338 and then Present
(Library_Unit
(Current_Unit
))
14339 and then Is_In_Main_Unit
(Library_Unit
(Current_Unit
)));
14340 end Is_In_Main_Unit
;
14342 ----------------------------
14343 -- Load_Parent_Of_Generic --
14344 ----------------------------
14346 procedure Load_Parent_Of_Generic
14349 Body_Optional
: Boolean := False)
14351 Comp_Unit
: constant Node_Id
:= Cunit
(Get_Source_Unit
(Spec
));
14352 Saved_Style_Check
: constant Boolean := Style_Check
;
14353 Saved_Warnings
: constant Warning_Record
:= Save_Warnings
;
14354 True_Parent
: Node_Id
;
14355 Inst_Node
: Node_Id
;
14357 Previous_Instances
: constant Elist_Id
:= New_Elmt_List
;
14359 procedure Collect_Previous_Instances
(Decls
: List_Id
);
14360 -- Collect all instantiations in the given list of declarations, that
14361 -- precede the generic that we need to load. If the bodies of these
14362 -- instantiations are available, we must analyze them, to ensure that
14363 -- the public symbols generated are the same when the unit is compiled
14364 -- to generate code, and when it is compiled in the context of a unit
14365 -- that needs a particular nested instance. This process is applied to
14366 -- both package and subprogram instances.
14368 --------------------------------
14369 -- Collect_Previous_Instances --
14370 --------------------------------
14372 procedure Collect_Previous_Instances
(Decls
: List_Id
) is
14376 Decl
:= First
(Decls
);
14377 while Present
(Decl
) loop
14378 if Sloc
(Decl
) >= Sloc
(Inst_Node
) then
14381 -- If Decl is an instantiation, then record it as requiring
14382 -- instantiation of the corresponding body, except if it is an
14383 -- abbreviated instantiation generated internally for conformance
14384 -- checking purposes only for the case of a formal package
14385 -- declared without a box (see Instantiate_Formal_Package). Such
14386 -- an instantiation does not generate any code (the actual code
14387 -- comes from actual) and thus does not need to be analyzed here.
14388 -- If the instantiation appears with a generic package body it is
14389 -- not analyzed here either.
14391 elsif Nkind
(Decl
) = N_Package_Instantiation
14392 and then not Is_Abbreviated_Instance
(Defining_Entity
(Decl
))
14394 Append_Elmt
(Decl
, Previous_Instances
);
14396 -- For a subprogram instantiation, omit instantiations intrinsic
14397 -- operations (Unchecked_Conversions, etc.) that have no bodies.
14399 elsif Nkind
(Decl
) in N_Function_Instantiation
14400 | N_Procedure_Instantiation
14401 and then not Is_Intrinsic_Subprogram
(Entity
(Name
(Decl
)))
14403 Append_Elmt
(Decl
, Previous_Instances
);
14405 elsif Nkind
(Decl
) = N_Package_Declaration
then
14406 Collect_Previous_Instances
14407 (Visible_Declarations
(Specification
(Decl
)));
14408 Collect_Previous_Instances
14409 (Private_Declarations
(Specification
(Decl
)));
14411 -- Previous non-generic bodies may contain instances as well
14413 elsif Nkind
(Decl
) = N_Package_Body
14414 and then Ekind
(Corresponding_Spec
(Decl
)) /= E_Generic_Package
14416 Collect_Previous_Instances
(Declarations
(Decl
));
14418 elsif Nkind
(Decl
) = N_Subprogram_Body
14419 and then not Acts_As_Spec
(Decl
)
14420 and then not Is_Generic_Subprogram
(Corresponding_Spec
(Decl
))
14422 Collect_Previous_Instances
(Declarations
(Decl
));
14427 end Collect_Previous_Instances
;
14429 -- Start of processing for Load_Parent_Of_Generic
14432 if not In_Same_Source_Unit
(N
, Spec
)
14433 or else Nkind
(Unit
(Comp_Unit
)) = N_Package_Declaration
14434 or else (Nkind
(Unit
(Comp_Unit
)) = N_Package_Body
14435 and then not Is_In_Main_Unit
(Spec
))
14437 -- Find body of parent of spec, and analyze it. A special case arises
14438 -- when the parent is an instantiation, that is to say when we are
14439 -- currently instantiating a nested generic. In that case, there is
14440 -- no separate file for the body of the enclosing instance. Instead,
14441 -- the enclosing body must be instantiated as if it were a pending
14442 -- instantiation, in order to produce the body for the nested generic
14443 -- we require now. Note that in that case the generic may be defined
14444 -- in a package body, the instance defined in the same package body,
14445 -- and the original enclosing body may not be in the main unit.
14447 Inst_Node
:= Empty
;
14449 True_Parent
:= Parent
(Spec
);
14450 while Present
(True_Parent
)
14451 and then Nkind
(True_Parent
) /= N_Compilation_Unit
14453 if Nkind
(True_Parent
) = N_Package_Declaration
14455 Nkind
(Original_Node
(True_Parent
)) = N_Package_Instantiation
14457 -- Parent is a compilation unit that is an instantiation, and
14458 -- instantiation node has been replaced with package decl.
14460 Inst_Node
:= Original_Node
(True_Parent
);
14463 elsif Nkind
(True_Parent
) = N_Package_Declaration
14464 and then Nkind
(Parent
(True_Parent
)) = N_Compilation_Unit
14466 Nkind
(Unit
(Parent
(True_Parent
))) = N_Package_Instantiation
14468 -- Parent is a compilation unit that is an instantiation, but
14469 -- instantiation node has not been replaced with package decl.
14471 Inst_Node
:= Unit
(Parent
(True_Parent
));
14474 elsif Nkind
(True_Parent
) = N_Package_Declaration
14475 and then Nkind
(Parent
(True_Parent
)) /= N_Compilation_Unit
14476 and then Present
(Generic_Parent
(Specification
(True_Parent
)))
14478 -- Parent is an instantiation within another specification.
14479 -- Declaration for instance has been inserted before original
14480 -- instantiation node. A direct link would be preferable?
14482 Inst_Node
:= Next
(True_Parent
);
14483 while Present
(Inst_Node
)
14484 and then Nkind
(Inst_Node
) /= N_Package_Instantiation
14489 -- If the instance appears within a generic, and the generic
14490 -- unit is defined within a formal package of the enclosing
14491 -- generic, there is no generic body available, and none
14492 -- needed. A more precise test should be used ???
14494 if No
(Inst_Node
) then
14500 -- If an ancestor of the generic comes from a formal package
14501 -- there is no source for the ancestor body. This is detected
14502 -- by examining the scope of the ancestor and its declaration.
14503 -- The body, if any is needed, will be available when the
14504 -- current unit (containing a formal package) is instantiated.
14506 elsif Nkind
(True_Parent
) = N_Package_Specification
14507 and then Present
(Generic_Parent
(True_Parent
))
14509 (Original_Node
(Unit_Declaration_Node
14510 (Scope
(Generic_Parent
(True_Parent
)))))
14511 = N_Formal_Package_Declaration
14516 True_Parent
:= Parent
(True_Parent
);
14520 -- Case where we are currently instantiating a nested generic
14522 if Present
(Inst_Node
) then
14523 if Nkind
(Parent
(True_Parent
)) = N_Compilation_Unit
then
14525 -- Instantiation node and declaration of instantiated package
14526 -- were exchanged when only the declaration was needed.
14527 -- Restore instantiation node before proceeding with body.
14529 Set_Unit
(Parent
(True_Parent
), Inst_Node
);
14532 -- Now complete instantiation of enclosing body, if it appears in
14533 -- some other unit. If it appears in the current unit, the body
14534 -- will have been instantiated already.
14536 if No
(Corresponding_Body
(Instance_Spec
(Inst_Node
))) then
14538 -- We need to determine the expander mode to instantiate the
14539 -- enclosing body. Because the generic body we need may use
14540 -- global entities declared in the enclosing package (including
14541 -- aggregates) it is in general necessary to compile this body
14542 -- with expansion enabled, except if we are within a generic
14543 -- package, in which case the usual generic rule applies.
14546 Exp_Status
: Boolean := True;
14550 -- Loop through scopes looking for generic package
14552 Scop
:= Scope
(Defining_Entity
(Instance_Spec
(Inst_Node
)));
14553 while Present
(Scop
)
14554 and then Scop
/= Standard_Standard
14556 if Ekind
(Scop
) = E_Generic_Package
then
14557 Exp_Status
:= False;
14561 Scop
:= Scope
(Scop
);
14564 -- Collect previous instantiations in the unit that contains
14565 -- the desired generic.
14567 if Nkind
(Parent
(True_Parent
)) /= N_Compilation_Unit
14568 and then not Body_Optional
14572 Info
: Pending_Body_Info
;
14576 Par
:= Parent
(Inst_Node
);
14577 while Present
(Par
) loop
14578 exit when Nkind
(Parent
(Par
)) = N_Compilation_Unit
;
14579 Par
:= Parent
(Par
);
14582 pragma Assert
(Present
(Par
));
14584 if Nkind
(Par
) = N_Package_Body
then
14585 Collect_Previous_Instances
(Declarations
(Par
));
14587 elsif Nkind
(Par
) = N_Package_Declaration
then
14588 Collect_Previous_Instances
14589 (Visible_Declarations
(Specification
(Par
)));
14590 Collect_Previous_Instances
14591 (Private_Declarations
(Specification
(Par
)));
14594 -- Enclosing unit is a subprogram body. In this
14595 -- case all instance bodies are processed in order
14596 -- and there is no need to collect them separately.
14601 Decl
:= First_Elmt
(Previous_Instances
);
14602 while Present
(Decl
) loop
14605 Instance_Spec
(Node
(Decl
)),
14606 Config_Switches
=> Save_Config_Switches
,
14607 Current_Sem_Unit
=>
14608 Get_Code_Unit
(Sloc
(Node
(Decl
))),
14609 Expander_Status
=> Exp_Status
,
14610 Inst_Node
=> Node
(Decl
),
14611 Local_Suppress_Stack_Top
=>
14612 Local_Suppress_Stack_Top
,
14613 Scope_Suppress
=> Scope_Suppress
,
14614 Warnings
=> Save_Warnings
);
14616 -- Package instance
14618 if Nkind
(Node
(Decl
)) = N_Package_Instantiation
14620 Instantiate_Package_Body
14621 (Info
, Body_Optional
=> True);
14623 -- Subprogram instance
14626 -- The instance_spec is in the wrapper package,
14627 -- usually followed by its local renaming
14628 -- declaration. See Build_Subprogram_Renaming
14629 -- for details. If the instance carries aspects,
14630 -- these result in the corresponding pragmas,
14631 -- inserted after the subprogram declaration.
14632 -- They must be skipped as well when retrieving
14633 -- the desired spec. Some of them may have been
14634 -- rewritten as null statements.
14635 -- A direct link would be more robust ???
14639 (Last
(Visible_Declarations
14640 (Specification
(Info
.Act_Decl
))));
14642 while Nkind
(Decl
) in
14645 N_Subprogram_Renaming_Declaration
14647 Decl
:= Prev
(Decl
);
14650 Info
.Act_Decl
:= Decl
;
14653 Instantiate_Subprogram_Body
14654 (Info
, Body_Optional
=> True);
14662 Instantiate_Package_Body
14664 ((Act_Decl
=> True_Parent
,
14665 Config_Switches
=> Save_Config_Switches
,
14666 Current_Sem_Unit
=>
14667 Get_Code_Unit
(Sloc
(Inst_Node
)),
14668 Expander_Status
=> Exp_Status
,
14669 Inst_Node
=> Inst_Node
,
14670 Local_Suppress_Stack_Top
=> Local_Suppress_Stack_Top
,
14671 Scope_Suppress
=> Scope_Suppress
,
14672 Warnings
=> Save_Warnings
)),
14673 Body_Optional
=> Body_Optional
);
14677 -- Case where we are not instantiating a nested generic
14680 Opt
.Style_Check
:= False;
14681 Expander_Mode_Save_And_Set
(True);
14682 Load_Needed_Body
(Comp_Unit
, OK
);
14683 Opt
.Style_Check
:= Saved_Style_Check
;
14684 Restore_Warnings
(Saved_Warnings
);
14685 Expander_Mode_Restore
;
14688 and then Unit_Requires_Body
(Defining_Entity
(Spec
))
14689 and then not Body_Optional
14692 Bname
: constant Unit_Name_Type
:=
14693 Get_Body_Name
(Get_Unit_Name
(Unit
(Comp_Unit
)));
14696 -- In CodePeer mode, the missing body may make the analysis
14697 -- incomplete, but we do not treat it as fatal.
14699 if CodePeer_Mode
then
14703 Error_Msg_Unit_1
:= Bname
;
14704 Error_Msg_N
("this instantiation requires$!", N
);
14705 Error_Msg_File_1
:=
14706 Get_File_Name
(Bname
, Subunit
=> False);
14707 Error_Msg_N
("\but file{ was not found!", N
);
14708 raise Unrecoverable_Error
;
14715 -- If loading parent of the generic caused an instantiation circularity,
14716 -- we abandon compilation at this point, because otherwise in some cases
14717 -- we get into trouble with infinite recursions after this point.
14719 if Circularity_Detected
then
14720 raise Unrecoverable_Error
;
14722 end Load_Parent_Of_Generic
;
14724 ---------------------------------
14725 -- Map_Formal_Package_Entities --
14726 ---------------------------------
14728 procedure Map_Formal_Package_Entities
(Form
: Entity_Id
; Act
: Entity_Id
) is
14733 Set_Instance_Of
(Form
, Act
);
14735 -- Traverse formal and actual package to map the corresponding entities.
14736 -- We skip over internal entities that may be generated during semantic
14737 -- analysis, and find the matching entities by name, given that they
14738 -- must appear in the same order.
14740 E1
:= First_Entity
(Form
);
14741 E2
:= First_Entity
(Act
);
14742 while Present
(E1
) and then E1
/= First_Private_Entity
(Form
) loop
14743 -- Could this test be a single condition??? Seems like it could, and
14744 -- isn't FPE (Form) a constant anyway???
14746 if not Is_Internal
(E1
)
14747 and then Present
(Parent
(E1
))
14748 and then not Is_Class_Wide_Type
(E1
)
14749 and then not Is_Internal_Name
(Chars
(E1
))
14751 while Present
(E2
) and then Chars
(E2
) /= Chars
(E1
) loop
14758 Set_Instance_Of
(E1
, E2
);
14760 if Is_Type
(E1
) and then Is_Tagged_Type
(E2
) then
14761 Set_Instance_Of
(Class_Wide_Type
(E1
), Class_Wide_Type
(E2
));
14764 if Is_Constrained
(E1
) then
14765 Set_Instance_Of
(Base_Type
(E1
), Base_Type
(E2
));
14768 if Ekind
(E1
) = E_Package
and then No
(Renamed_Entity
(E1
)) then
14769 Map_Formal_Package_Entities
(E1
, E2
);
14776 end Map_Formal_Package_Entities
;
14778 -----------------------
14779 -- Move_Freeze_Nodes --
14780 -----------------------
14782 procedure Move_Freeze_Nodes
14783 (Out_Of
: Entity_Id
;
14788 Next_Decl
: Node_Id
;
14789 Next_Node
: Node_Id
:= After
;
14792 function Is_Outer_Type
(T
: Entity_Id
) return Boolean;
14793 -- Check whether entity is declared in a scope external to that of the
14796 -------------------
14797 -- Is_Outer_Type --
14798 -------------------
14800 function Is_Outer_Type
(T
: Entity_Id
) return Boolean is
14801 Scop
: Entity_Id
:= Scope
(T
);
14804 if Scope_Depth
(Scop
) < Scope_Depth
(Out_Of
) then
14808 while Scop
/= Standard_Standard
loop
14809 if Scop
= Out_Of
then
14812 Scop
:= Scope
(Scop
);
14820 -- Start of processing for Move_Freeze_Nodes
14827 -- First remove the freeze nodes that may appear before all other
14831 while Present
(Decl
)
14832 and then Nkind
(Decl
) = N_Freeze_Entity
14833 and then Is_Outer_Type
(Entity
(Decl
))
14835 Decl
:= Remove_Head
(L
);
14836 Insert_After
(Next_Node
, Decl
);
14837 Set_Analyzed
(Decl
, False);
14842 -- Next scan the list of declarations and remove each freeze node that
14843 -- appears ahead of the current node.
14845 while Present
(Decl
) loop
14846 while Present
(Next
(Decl
))
14847 and then Nkind
(Next
(Decl
)) = N_Freeze_Entity
14848 and then Is_Outer_Type
(Entity
(Next
(Decl
)))
14850 Next_Decl
:= Remove_Next
(Decl
);
14851 Insert_After
(Next_Node
, Next_Decl
);
14852 Set_Analyzed
(Next_Decl
, False);
14853 Next_Node
:= Next_Decl
;
14856 -- If the declaration is a nested package or concurrent type, then
14857 -- recurse. Nested generic packages will have been processed from the
14860 case Nkind
(Decl
) is
14861 when N_Package_Declaration
=>
14862 Spec
:= Specification
(Decl
);
14864 when N_Task_Type_Declaration
=>
14865 Spec
:= Task_Definition
(Decl
);
14867 when N_Protected_Type_Declaration
=>
14868 Spec
:= Protected_Definition
(Decl
);
14874 if Present
(Spec
) then
14875 Move_Freeze_Nodes
(Out_Of
, Next_Node
, Visible_Declarations
(Spec
));
14876 Move_Freeze_Nodes
(Out_Of
, Next_Node
, Private_Declarations
(Spec
));
14881 end Move_Freeze_Nodes
;
14887 function Next_Assoc
(E
: Assoc_Ptr
) return Assoc_Ptr
is
14889 return Generic_Renamings
.Table
(E
).Next_In_HTable
;
14892 ------------------------
14893 -- Preanalyze_Actuals --
14894 ------------------------
14896 procedure Preanalyze_Actuals
(N
: Node_Id
; Inst
: Entity_Id
:= Empty
) is
14897 procedure Perform_Appropriate_Analysis
(N
: Node_Id
);
14898 -- Determine if the actuals we are analyzing come from a generic
14899 -- instantiation that is a library unit and dispatch accordingly.
14901 ----------------------------------
14902 -- Perform_Appropriate_Analysis --
14903 ----------------------------------
14905 procedure Perform_Appropriate_Analysis
(N
: Node_Id
) is
14907 -- When we have a library instantiation we cannot allow any expansion
14908 -- to occur, since there may be no place to put it. Instead, in that
14909 -- case we perform a preanalysis of the actual.
14911 if Present
(Inst
) and then Is_Compilation_Unit
(Inst
) then
14916 end Perform_Appropriate_Analysis
;
14920 Errs
: constant Nat
:= Serious_Errors_Detected
;
14925 Cur
: Entity_Id
:= Empty
;
14926 -- Current homograph of the instance name
14928 Vis
: Boolean := False;
14929 -- Saved visibility status of the current homograph
14931 -- Start of processing for Preanalyze_Actuals
14934 Assoc
:= First
(Generic_Associations
(N
));
14936 -- If the instance is a child unit, its name may hide an outer homonym,
14937 -- so make it invisible to perform name resolution on the actuals.
14939 if Nkind
(Defining_Unit_Name
(N
)) = N_Defining_Program_Unit_Name
14941 (Current_Entity
(Defining_Identifier
(Defining_Unit_Name
(N
))))
14943 Cur
:= Current_Entity
(Defining_Identifier
(Defining_Unit_Name
(N
)));
14945 if Is_Compilation_Unit
(Cur
) then
14946 Vis
:= Is_Immediately_Visible
(Cur
);
14947 Set_Is_Immediately_Visible
(Cur
, False);
14953 while Present
(Assoc
) loop
14954 if Nkind
(Assoc
) /= N_Others_Choice
then
14955 Act
:= Explicit_Generic_Actual_Parameter
(Assoc
);
14957 -- Within a nested instantiation, a defaulted actual is an empty
14958 -- association, so nothing to analyze. If the subprogram actual
14959 -- is an attribute, analyze prefix only, because actual is not a
14960 -- complete attribute reference.
14962 -- If actual is an allocator, analyze expression only. The full
14963 -- analysis can generate code, and if instance is a compilation
14964 -- unit we have to wait until the package instance is installed
14965 -- to have a proper place to insert this code.
14967 -- String literals may be operators, but at this point we do not
14968 -- know whether the actual is a formal subprogram or a string.
14973 elsif Nkind
(Act
) = N_Attribute_Reference
then
14974 Perform_Appropriate_Analysis
(Prefix
(Act
));
14976 elsif Nkind
(Act
) = N_Explicit_Dereference
then
14977 Perform_Appropriate_Analysis
(Prefix
(Act
));
14979 elsif Nkind
(Act
) = N_Allocator
then
14981 Expr
: constant Node_Id
:= Expression
(Act
);
14984 if Nkind
(Expr
) = N_Subtype_Indication
then
14985 Perform_Appropriate_Analysis
(Subtype_Mark
(Expr
));
14987 -- Analyze separately each discriminant constraint, when
14988 -- given with a named association.
14994 Constr
:= First
(Constraints
(Constraint
(Expr
)));
14995 while Present
(Constr
) loop
14996 if Nkind
(Constr
) = N_Discriminant_Association
then
14997 Perform_Appropriate_Analysis
14998 (Expression
(Constr
));
15000 Perform_Appropriate_Analysis
(Constr
);
15008 Perform_Appropriate_Analysis
(Expr
);
15012 elsif Nkind
(Act
) /= N_Operator_Symbol
then
15013 Perform_Appropriate_Analysis
(Act
);
15015 -- Within a package instance, mark actuals that are limited
15016 -- views, so their use can be moved to the body of the
15019 if Is_Entity_Name
(Act
)
15020 and then Is_Type
(Entity
(Act
))
15021 and then From_Limited_With
(Entity
(Act
))
15022 and then Present
(Inst
)
15024 Append_Elmt
(Entity
(Act
), Incomplete_Actuals
(Inst
));
15028 if Errs
/= Serious_Errors_Detected
then
15030 -- Do a minimal analysis of the generic, to prevent spurious
15031 -- warnings complaining about the generic being unreferenced,
15032 -- before abandoning the instantiation.
15034 Perform_Appropriate_Analysis
(Name
(N
));
15036 if Is_Entity_Name
(Name
(N
))
15037 and then Etype
(Name
(N
)) /= Any_Type
15039 Generate_Reference
(Entity
(Name
(N
)), Name
(N
));
15040 Set_Is_Instantiated
(Entity
(Name
(N
)));
15043 if Present
(Cur
) then
15045 -- For the case of a child instance hiding an outer homonym,
15046 -- provide additional warning which might explain the error.
15048 Set_Is_Immediately_Visible
(Cur
, Vis
);
15050 ("& hides outer unit with the same name??",
15051 N
, Defining_Unit_Name
(N
));
15054 Abandon_Instantiation
(Act
);
15061 if Present
(Cur
) then
15062 Set_Is_Immediately_Visible
(Cur
, Vis
);
15064 end Preanalyze_Actuals
;
15066 -------------------------------
15067 -- Provide_Completing_Bodies --
15068 -------------------------------
15070 procedure Provide_Completing_Bodies
(N
: Node_Id
) is
15071 procedure Build_Completing_Body
(Subp_Decl
: Node_Id
);
15072 -- Generate the completing body for subprogram declaration Subp_Decl
15074 procedure Provide_Completing_Bodies_In
(Decls
: List_Id
);
15075 -- Generating completing bodies for all subprograms found in declarative
15078 ---------------------------
15079 -- Build_Completing_Body --
15080 ---------------------------
15082 procedure Build_Completing_Body
(Subp_Decl
: Node_Id
) is
15083 Loc
: constant Source_Ptr
:= Sloc
(Subp_Decl
);
15084 Subp_Id
: constant Entity_Id
:= Defining_Entity
(Subp_Decl
);
15088 -- Nothing to do if the subprogram already has a completing body
15090 if Present
(Corresponding_Body
(Subp_Decl
)) then
15093 -- Mark the function as having a valid return statement even though
15094 -- the body contains a single raise statement.
15096 elsif Ekind
(Subp_Id
) = E_Function
then
15097 Set_Return_Present
(Subp_Id
);
15100 -- Clone the specification to obtain new entities and reset the only
15103 Spec
:= Copy_Subprogram_Spec
(Specification
(Subp_Decl
));
15104 Set_Generic_Parent
(Spec
, Empty
);
15107 -- function Func ... return ... is
15109 -- procedure Proc ... is
15111 -- raise Program_Error with "access before elaboration";
15114 Insert_After_And_Analyze
(Subp_Decl
,
15115 Make_Subprogram_Body
(Loc
,
15116 Specification
=> Spec
,
15117 Declarations
=> New_List
,
15118 Handled_Statement_Sequence
=>
15119 Make_Handled_Sequence_Of_Statements
(Loc
,
15120 Statements
=> New_List
(
15121 Make_Raise_Program_Error
(Loc
,
15122 Reason
=> PE_Access_Before_Elaboration
)))));
15123 end Build_Completing_Body
;
15125 ----------------------------------
15126 -- Provide_Completing_Bodies_In --
15127 ----------------------------------
15129 procedure Provide_Completing_Bodies_In
(Decls
: List_Id
) is
15133 if Present
(Decls
) then
15134 Decl
:= First
(Decls
);
15135 while Present
(Decl
) loop
15136 Provide_Completing_Bodies
(Decl
);
15140 end Provide_Completing_Bodies_In
;
15146 -- Start of processing for Provide_Completing_Bodies
15149 if Nkind
(N
) = N_Package_Declaration
then
15150 Spec
:= Specification
(N
);
15152 Push_Scope
(Defining_Entity
(N
));
15153 Provide_Completing_Bodies_In
(Visible_Declarations
(Spec
));
15154 Provide_Completing_Bodies_In
(Private_Declarations
(Spec
));
15157 elsif Nkind
(N
) = N_Subprogram_Declaration
then
15158 Build_Completing_Body
(N
);
15160 end Provide_Completing_Bodies
;
15162 -------------------
15163 -- Remove_Parent --
15164 -------------------
15166 procedure Remove_Parent
(In_Body
: Boolean := False) is
15167 S
: Entity_Id
:= Current_Scope
;
15168 -- S is the scope containing the instantiation just completed. The scope
15169 -- stack contains the parent instances of the instantiation, followed by
15178 -- After child instantiation is complete, remove from scope stack the
15179 -- extra copy of the current scope, and then remove parent instances.
15181 if not In_Body
then
15184 while Current_Scope
/= S
loop
15185 P
:= Current_Scope
;
15186 End_Package_Scope
(Current_Scope
);
15188 if In_Open_Scopes
(P
) then
15189 E
:= First_Entity
(P
);
15190 while Present
(E
) loop
15191 Set_Is_Immediately_Visible
(E
, True);
15195 -- If instantiation is declared in a block, it is the enclosing
15196 -- scope that might be a parent instance. Note that only one
15197 -- block can be involved, because the parent instances have
15198 -- been installed within it.
15200 if Ekind
(P
) = E_Block
then
15201 Cur_P
:= Scope
(P
);
15206 if Is_Generic_Instance
(Cur_P
) and then P
/= Current_Scope
then
15207 -- We are within an instance of some sibling. Retain
15208 -- visibility of parent, for proper subsequent cleanup, and
15209 -- reinstall private declarations as well.
15211 Set_In_Private_Part
(P
);
15212 Install_Private_Declarations
(P
);
15215 -- If the ultimate parent is a top-level unit recorded in
15216 -- Instance_Parent_Unit, then reset its visibility to what it was
15217 -- before instantiation. (It's not clear what the purpose is of
15218 -- testing whether Scope (P) is In_Open_Scopes, but that test was
15219 -- present before the ultimate parent test was added.???)
15221 elsif not In_Open_Scopes
(Scope
(P
))
15222 or else (P
= Instance_Parent_Unit
15223 and then not Parent_Unit_Visible
)
15225 Set_Is_Immediately_Visible
(P
, False);
15227 -- If the current scope is itself an instantiation of a generic
15228 -- nested within P, and we are in the private part of body of this
15229 -- instantiation, restore the full views of P, that were removed
15230 -- in End_Package_Scope above. This obscure case can occur when a
15231 -- subunit of a generic contains an instance of a child unit of
15232 -- its generic parent unit.
15234 elsif S
= Current_Scope
and then Is_Generic_Instance
(S
)
15235 and then (In_Package_Body
(S
) or else In_Private_Part
(S
))
15238 Par
: constant Entity_Id
:=
15239 Generic_Parent
(Package_Specification
(S
));
15242 and then P
= Scope
(Par
)
15244 Set_In_Private_Part
(P
);
15245 Install_Private_Declarations
(P
);
15251 -- Reset visibility of entities in the enclosing scope
15253 Set_Is_Hidden_Open_Scope
(Current_Scope
, False);
15255 Hidden
:= First_Elmt
(Hidden_Entities
);
15256 while Present
(Hidden
) loop
15257 Set_Is_Immediately_Visible
(Node
(Hidden
), True);
15258 Next_Elmt
(Hidden
);
15262 -- Each body is analyzed separately, and there is no context that
15263 -- needs preserving from one body instance to the next, so remove all
15264 -- parent scopes that have been installed.
15266 while Present
(S
) loop
15267 End_Package_Scope
(S
);
15268 Set_Is_Immediately_Visible
(S
, False);
15269 S
:= Current_Scope
;
15270 exit when S
= Standard_Standard
;
15275 -----------------------------------
15276 -- Requires_Conformance_Checking --
15277 -----------------------------------
15279 function Requires_Conformance_Checking
(N
: Node_Id
) return Boolean is
15281 -- No conformance checking required if the generic actual part is empty,
15282 -- or is a box or an others_clause (necessarily with a box).
15284 return Present
(Generic_Associations
(N
))
15285 and then not Box_Present
(N
)
15286 and then Nkind
(First
(Generic_Associations
(N
))) /= N_Others_Choice
;
15287 end Requires_Conformance_Checking
;
15293 procedure Restore_Env
is
15294 Saved
: Instance_Env
renames Instance_Envs
.Table
(Instance_Envs
.Last
);
15297 if No
(Current_Instantiated_Parent
.Act_Id
) then
15298 -- Restore environment after subprogram inlining
15300 Restore_Private_Views
(Empty
);
15303 Current_Instantiated_Parent
:= Saved
.Instantiated_Parent
;
15304 Exchanged_Views
:= Saved
.Exchanged_Views
;
15305 Hidden_Entities
:= Saved
.Hidden_Entities
;
15306 Current_Sem_Unit
:= Saved
.Current_Sem_Unit
;
15307 Parent_Unit_Visible
:= Saved
.Parent_Unit_Visible
;
15308 Instance_Parent_Unit
:= Saved
.Instance_Parent_Unit
;
15310 Restore_Config_Switches
(Saved
.Switches
);
15312 Instance_Envs
.Decrement_Last
;
15315 ---------------------------
15316 -- Restore_Private_Views --
15317 ---------------------------
15319 procedure Restore_Private_Views
15320 (Pack_Id
: Entity_Id
;
15321 Is_Package
: Boolean := True)
15326 Dep_Elmt
: Elmt_Id
;
15329 procedure Restore_Nested_Formal
(Formal
: Entity_Id
);
15330 -- Hide the generic formals of formal packages declared with box which
15331 -- were reachable in the current instantiation.
15333 ---------------------------
15334 -- Restore_Nested_Formal --
15335 ---------------------------
15337 procedure Restore_Nested_Formal
(Formal
: Entity_Id
) is
15338 pragma Assert
(Ekind
(Formal
) = E_Package
);
15341 if Present
(Renamed_Entity
(Formal
))
15342 and then Denotes_Formal_Package
(Renamed_Entity
(Formal
), True)
15346 elsif Present
(Associated_Formal_Package
(Formal
)) then
15347 Ent
:= First_Entity
(Formal
);
15348 while Present
(Ent
) loop
15349 exit when Ekind
(Ent
) = E_Package
15350 and then Renamed_Entity
(Ent
) = Renamed_Entity
(Formal
);
15352 Set_Is_Hidden
(Ent
);
15353 Set_Is_Potentially_Use_Visible
(Ent
, False);
15355 -- If package, then recurse
15357 if Ekind
(Ent
) = E_Package
then
15358 Restore_Nested_Formal
(Ent
);
15364 end Restore_Nested_Formal
;
15366 -- Start of processing for Restore_Private_Views
15369 M
:= First_Elmt
(Exchanged_Views
);
15370 while Present
(M
) loop
15373 -- Subtypes of types whose views have been exchanged, and that are
15374 -- defined within the instance, were not on the Private_Dependents
15375 -- list on entry to the instance, so they have to be exchanged
15376 -- explicitly now, in order to remain consistent with the view of the
15379 if Ekind
(Typ
) in E_Private_Type
15380 | E_Limited_Private_Type
15381 | E_Record_Type_With_Private
15383 Dep_Elmt
:= First_Elmt
(Private_Dependents
(Typ
));
15384 while Present
(Dep_Elmt
) loop
15385 Dep_Typ
:= Node
(Dep_Elmt
);
15387 if Scope
(Dep_Typ
) = Pack_Id
15388 and then Present
(Full_View
(Dep_Typ
))
15390 Replace_Elmt
(Dep_Elmt
, Full_View
(Dep_Typ
));
15391 Exchange_Declarations
(Dep_Typ
);
15394 Next_Elmt
(Dep_Elmt
);
15398 Exchange_Declarations
(Node
(M
));
15402 if No
(Pack_Id
) then
15406 -- Make the generic formal parameters private, and make the formal types
15407 -- into subtypes of the actuals again.
15409 E
:= First_Entity
(Pack_Id
);
15410 while Present
(E
) loop
15411 Set_Is_Hidden
(E
, True);
15414 and then Nkind
(Parent
(E
)) = N_Subtype_Declaration
15416 -- Always preserve the flag Is_Generic_Actual_Type for GNATprove,
15417 -- as it is needed to identify the subtype with the type it
15418 -- renames, when there are conversions between access types
15421 if GNATprove_Mode
then
15424 -- If the actual for E is itself a generic actual type from
15425 -- an enclosing instance, E is still a generic actual type
15426 -- outside of the current instance. This matter when resolving
15427 -- an overloaded call that may be ambiguous in the enclosing
15428 -- instance, when two of its actuals coincide.
15430 elsif Is_Entity_Name
(Subtype_Indication
(Parent
(E
)))
15431 and then Is_Generic_Actual_Type
15432 (Entity
(Subtype_Indication
(Parent
(E
))))
15436 Set_Is_Generic_Actual_Type
(E
, False);
15438 -- It might seem reasonable to clear the Is_Generic_Actual_Type
15439 -- flag also on the Full_View if the type is private, since it
15440 -- was set also on this Full_View. However, this flag is relied
15441 -- upon by Covers to spot "types exported from instantiations"
15442 -- which are implicit Full_Views built for instantiations made
15443 -- on private types and we get type mismatches if we do it when
15444 -- the block exchanging the declarations below triggers ???
15446 -- if Is_Private_Type (E) and then Present (Full_View (E)) then
15447 -- Set_Is_Generic_Actual_Type (Full_View (E), False);
15451 -- An unusual case of aliasing: the actual may also be directly
15452 -- visible in the generic, and be private there, while it is fully
15453 -- visible in the context of the instance. The internal subtype
15454 -- is private in the instance but has full visibility like its
15455 -- parent in the enclosing scope. This enforces the invariant that
15456 -- the privacy status of all private dependents of a type coincide
15457 -- with that of the parent type. This can only happen when a
15458 -- generic child unit is instantiated within a sibling.
15460 if Is_Private_Type
(E
)
15461 and then not Is_Private_Type
(Etype
(E
))
15463 Exchange_Declarations
(E
);
15466 elsif Ekind
(E
) = E_Package
then
15468 -- The end of the renaming list is the renaming of the generic
15469 -- package itself. If the instance is a subprogram, all entities
15470 -- in the corresponding package are renamings. If this entity is
15471 -- a formal package, make its own formals private as well. The
15472 -- actual in this case is itself the renaming of an instantiation.
15473 -- If the entity is not a package renaming, it is the entity
15474 -- created to validate formal package actuals: ignore it.
15476 -- If the actual is itself a formal package for the enclosing
15477 -- generic, or the actual for such a formal package, it remains
15478 -- visible on exit from the instance, and therefore nothing needs
15479 -- to be done either, except to keep it accessible.
15481 if Is_Package
and then Renamed_Entity
(E
) = Pack_Id
then
15484 elsif Nkind
(Parent
(E
)) /= N_Package_Renaming_Declaration
then
15488 Denotes_Formal_Package
(Renamed_Entity
(E
), True, Pack_Id
)
15490 Set_Is_Hidden
(E
, False);
15494 Act_P
: constant Entity_Id
:= Renamed_Entity
(E
);
15498 Id
:= First_Entity
(Act_P
);
15500 and then Id
/= First_Private_Entity
(Act_P
)
15502 exit when Ekind
(Id
) = E_Package
15503 and then Renamed_Entity
(Id
) = Act_P
;
15505 Set_Is_Hidden
(Id
, True);
15506 Set_Is_Potentially_Use_Visible
(Id
, In_Use
(Act_P
));
15508 if Ekind
(Id
) = E_Package
then
15509 Restore_Nested_Formal
(Id
);
15520 end Restore_Private_Views
;
15527 (Gen_Unit
: Entity_Id
;
15528 Act_Unit
: Entity_Id
)
15532 Set_Instance_Env
(Gen_Unit
, Act_Unit
);
15535 ----------------------------
15536 -- Save_Global_References --
15537 ----------------------------
15539 procedure Save_Global_References
(Templ
: Node_Id
) is
15541 -- ??? it is horrible to use global variables in highly recursive code
15544 -- The entity of the current associated node
15546 Gen_Scope
: Entity_Id
;
15547 -- The scope of the generic for which references are being saved
15550 -- The current associated node
15552 function Is_Global
(E
: Entity_Id
) return Boolean;
15553 -- Check whether entity is defined outside of generic unit. Examine the
15554 -- scope of an entity, and the scope of the scope, etc, until we find
15555 -- either Standard, in which case the entity is global, or the generic
15556 -- unit itself, which indicates that the entity is local. If the entity
15557 -- is the generic unit itself, as in the case of a recursive call, or
15558 -- the enclosing generic unit, if different from the current scope, then
15559 -- it is local as well, because it will be replaced at the point of
15560 -- instantiation. On the other hand, if it is a reference to a child
15561 -- unit of a common ancestor, which appears in an instantiation, it is
15562 -- global because it is used to denote a specific compilation unit at
15563 -- the time the instantiations will be analyzed.
15565 procedure Qualify_Universal_Operands
15567 Func_Call
: Node_Id
);
15568 -- Op denotes a binary or unary operator in generic template Templ. Node
15569 -- Func_Call is the function call alternative of the operator within the
15570 -- the analyzed copy of the template. Change each operand which yields a
15571 -- universal type by wrapping it into a qualified expression
15573 -- Actual_Typ'(Operand)
15575 -- where Actual_Typ is the type of corresponding actual parameter of
15576 -- Operand in Func_Call.
15578 procedure Reset_Entity
(N
: Node_Id
);
15579 -- Save semantic information on global entity so that it is not resolved
15580 -- again at instantiation time.
15582 procedure Save_Entity_Descendants
(N
: Node_Id
);
15583 -- Apply Save_Global_References to the two syntactic descendants of
15584 -- non-terminal nodes that carry an Associated_Node and are processed
15585 -- through Reset_Entity. Once the global entity (if any) has been
15586 -- captured together with its type, only two syntactic descendants need
15587 -- to be traversed to complete the processing of the tree rooted at N.
15588 -- This applies to Selected_Components, Expanded_Names, and to Operator
15589 -- nodes. N can also be a character literal, identifier, or operator
15590 -- symbol node, but the call has no effect in these cases.
15592 procedure Save_Global_Defaults
(N1
: Node_Id
; N2
: Node_Id
);
15593 -- Default actuals in nested instances must be handled specially
15594 -- because there is no link to them from the original tree. When an
15595 -- actual subprogram is given by a default, we add an explicit generic
15596 -- association for it in the instantiation node. When we save the
15597 -- global references on the name of the instance, we recover the list
15598 -- of generic associations, and add an explicit one to the original
15599 -- generic tree, through which a global actual can be preserved.
15600 -- Similarly, if a child unit is instantiated within a sibling, in the
15601 -- context of the parent, we must preserve the identifier of the parent
15602 -- so that it can be properly resolved in a subsequent instantiation.
15604 procedure Save_Global_Descendant
(D
: Union_Id
);
15605 -- Apply Save_References recursively to the descendants of node D
15607 procedure Save_References
(N
: Node_Id
);
15608 -- This is the recursive procedure that does the work, once the
15609 -- enclosing generic scope has been established.
15615 function Is_Global
(E
: Entity_Id
) return Boolean is
15618 function Is_Instance_Node
(Decl
: Node_Id
) return Boolean;
15619 -- Determine whether the parent node of a reference to a child unit
15620 -- denotes an instantiation or a formal package, in which case the
15621 -- reference to the child unit is global, even if it appears within
15622 -- the current scope (e.g. when the instance appears within the body
15623 -- of an ancestor).
15625 ----------------------
15626 -- Is_Instance_Node --
15627 ----------------------
15629 function Is_Instance_Node
(Decl
: Node_Id
) return Boolean is
15631 return Nkind
(Decl
) in N_Generic_Instantiation
15633 Nkind
(Original_Node
(Decl
)) = N_Formal_Package_Declaration
;
15634 end Is_Instance_Node
;
15636 -- Start of processing for Is_Global
15639 if E
= Gen_Scope
then
15642 elsif E
= Standard_Standard
then
15645 -- E should be an entity, but it is not always
15647 elsif Nkind
(E
) not in N_Entity
then
15650 elsif Nkind
(E
) /= N_Expanded_Name
15651 and then Is_Child_Unit
(E
)
15652 and then (Is_Instance_Node
(Parent
(N2
))
15653 or else (Nkind
(Parent
(N2
)) = N_Expanded_Name
15654 and then N2
= Selector_Name
(Parent
(N2
))
15656 Is_Instance_Node
(Parent
(Parent
(N2
)))))
15661 -- E may be an expanded name - typically an operator - in which
15662 -- case we must find its enclosing scope since expanded names
15663 -- don't have corresponding scopes.
15665 if Nkind
(E
) = N_Expanded_Name
then
15666 Se
:= Find_Enclosing_Scope
(E
);
15668 -- Otherwise, E is an entity and will have Scope set
15674 while Se
/= Gen_Scope
loop
15675 if Se
= Standard_Standard
then
15686 --------------------------------
15687 -- Qualify_Universal_Operands --
15688 --------------------------------
15690 procedure Qualify_Universal_Operands
15692 Func_Call
: Node_Id
)
15694 procedure Qualify_Operand
(Opnd
: Node_Id
; Actual
: Node_Id
);
15695 -- Rewrite operand Opnd as a qualified expression of the form
15697 -- Actual_Typ'(Opnd)
15699 -- where Actual is the corresponding actual parameter of Opnd in
15700 -- function call Func_Call.
15702 function Qualify_Type
15704 Typ
: Entity_Id
) return Node_Id
;
15705 -- Qualify type Typ by creating a selected component of the form
15707 -- Scope_Of_Typ.Typ
15709 ---------------------
15710 -- Qualify_Operand --
15711 ---------------------
15713 procedure Qualify_Operand
(Opnd
: Node_Id
; Actual
: Node_Id
) is
15714 Loc
: constant Source_Ptr
:= Sloc
(Opnd
);
15715 Typ
: constant Entity_Id
:= Etype
(Actual
);
15720 -- Qualify the operand when it is of a universal type. Note that
15721 -- the template is unanalyzed and it is not possible to directly
15722 -- query the type. This transformation is not done when the type
15723 -- of the actual is internally generated because the type will be
15724 -- regenerated in the instance.
15726 if Yields_Universal_Type
(Opnd
)
15727 and then Comes_From_Source
(Typ
)
15728 and then not Is_Hidden
(Typ
)
15730 -- The type of the actual may be a global reference. Save this
15731 -- information by creating a reference to it.
15733 if Is_Global
(Typ
) then
15734 Mark
:= New_Occurrence_Of
(Typ
, Loc
);
15736 -- Otherwise rely on resolution to find the proper type within
15740 Mark
:= Qualify_Type
(Loc
, Typ
);
15744 Make_Qualified_Expression
(Loc
,
15745 Subtype_Mark
=> Mark
,
15746 Expression
=> Relocate_Node
(Opnd
));
15748 -- Mark the qualification to distinguish it from other source
15749 -- constructs and signal the instantiation mechanism that this
15750 -- node requires special processing. See Copy_Generic_Node for
15753 Set_Is_Qualified_Universal_Literal
(Qual
);
15755 Rewrite
(Opnd
, Qual
);
15757 end Qualify_Operand
;
15763 function Qualify_Type
15765 Typ
: Entity_Id
) return Node_Id
15767 Scop
: constant Entity_Id
:= Scope
(Typ
);
15771 Result
:= Make_Identifier
(Loc
, Chars
(Typ
));
15773 if Present
(Scop
) and then not Is_Generic_Unit
(Scop
) then
15775 Make_Selected_Component
(Loc
,
15776 Prefix
=> Make_Identifier
(Loc
, Chars
(Scop
)),
15777 Selector_Name
=> Result
);
15785 Actuals
: constant List_Id
:= Parameter_Associations
(Func_Call
);
15787 -- Start of processing for Qualify_Universal_Operands
15790 if Nkind
(Op
) in N_Binary_Op
then
15791 Qualify_Operand
(Left_Opnd
(Op
), First
(Actuals
));
15792 Qualify_Operand
(Right_Opnd
(Op
), Next
(First
(Actuals
)));
15794 elsif Nkind
(Op
) in N_Unary_Op
then
15795 Qualify_Operand
(Right_Opnd
(Op
), First
(Actuals
));
15797 end Qualify_Universal_Operands
;
15803 procedure Reset_Entity
(N
: Node_Id
) is
15804 procedure Set_Global_Type
(N
: Node_Id
; N2
: Node_Id
);
15805 -- If the type of N2 is global to the generic unit, save the type in
15806 -- the generic node. Just as we perform name capture for explicit
15807 -- references within the generic, we must capture the global types
15808 -- of local entities because they may participate in resolution in
15811 function Top_Ancestor
(E
: Entity_Id
) return Entity_Id
;
15812 -- Find the ultimate ancestor of the current unit. If it is not a
15813 -- generic unit, then the name of the current unit in the prefix of
15814 -- an expanded name must be replaced with its generic homonym to
15815 -- ensure that it will be properly resolved in an instance.
15817 ---------------------
15818 -- Set_Global_Type --
15819 ---------------------
15821 procedure Set_Global_Type
(N
: Node_Id
; N2
: Node_Id
) is
15822 Typ
: constant Entity_Id
:= Etype
(N2
);
15825 Set_Etype
(N
, Typ
);
15827 -- If the entity of N is not the associated node, this is a
15828 -- nested generic and it has an associated node as well, whose
15829 -- type is already the full view (see below). Indicate that the
15830 -- original node has a private view.
15832 if Entity
(N
) /= N2
and then Has_Private_View
(Entity
(N
)) then
15833 Set_Has_Private_View
(N
);
15836 -- If not a private type, nothing else to do
15838 if not Is_Private_Type
(Typ
) then
15841 -- If it is a derivation of a private type in a context where no
15842 -- full view is needed, nothing to do either.
15844 elsif No
(Full_View
(Typ
)) and then Typ
/= Etype
(Typ
) then
15847 -- Otherwise mark the type for flipping and use the full view when
15851 Set_Has_Private_View
(N
);
15853 if Present
(Full_View
(Typ
)) then
15854 Set_Etype
(N2
, Full_View
(Typ
));
15858 if Is_Floating_Point_Type
(Typ
)
15859 and then Has_Dimension_System
(Typ
)
15861 Copy_Dimensions
(N2
, N
);
15863 end Set_Global_Type
;
15869 function Top_Ancestor
(E
: Entity_Id
) return Entity_Id
is
15874 while Is_Child_Unit
(Par
) loop
15875 Par
:= Scope
(Par
);
15881 -- Start of processing for Reset_Entity
15884 N2
:= Get_Associated_Node
(N
);
15887 if Present
(E
) then
15889 -- If the node is an entry call to an entry in an enclosing task,
15890 -- it is rewritten as a selected component. No global entity to
15891 -- preserve in this case, since the expansion will be redone in
15894 if Nkind
(E
) not in N_Entity
then
15895 Set_Associated_Node
(N
, Empty
);
15896 Set_Etype
(N
, Empty
);
15900 -- If the entity is an itype created as a subtype of an access
15901 -- type with a null exclusion restore source entity for proper
15902 -- visibility. The itype will be created anew in the instance.
15905 and then Ekind
(E
) = E_Access_Subtype
15906 and then Is_Entity_Name
(N
)
15907 and then Chars
(Etype
(E
)) = Chars
(N
)
15910 Set_Entity
(N2
, E
);
15914 if Is_Global
(E
) then
15915 Set_Global_Type
(N
, N2
);
15917 elsif Nkind
(N
) = N_Op_Concat
15918 and then Is_Generic_Type
(Etype
(N2
))
15919 and then (Base_Type
(Etype
(Right_Opnd
(N2
))) = Etype
(N2
)
15921 Base_Type
(Etype
(Left_Opnd
(N2
))) = Etype
(N2
))
15922 and then Is_Intrinsic_Subprogram
(E
)
15926 -- Entity is local. Mark generic node as unresolved. Note that now
15927 -- it does not have an entity.
15930 Set_Associated_Node
(N
, Empty
);
15931 Set_Etype
(N
, Empty
);
15934 if Nkind
(Parent
(N
)) in N_Generic_Instantiation
15935 and then N
= Name
(Parent
(N
))
15937 Save_Global_Defaults
(Parent
(N
), Parent
(N2
));
15940 elsif Nkind
(Parent
(N
)) = N_Selected_Component
15941 and then Nkind
(Parent
(N2
)) = N_Expanded_Name
15943 -- In case of previous errors, the tree might be malformed
15945 if No
(Entity
(Parent
(N2
))) then
15948 elsif Is_Global
(Entity
(Parent
(N2
))) then
15949 Change_Selected_Component_To_Expanded_Name
(Parent
(N
));
15950 Set_Associated_Node
(Parent
(N
), Parent
(N2
));
15951 Set_Global_Type
(Parent
(N
), Parent
(N2
));
15952 Save_Entity_Descendants
(N
);
15954 -- If this is a reference to the current generic entity, replace
15955 -- by the name of the generic homonym of the current package. This
15956 -- is because in an instantiation Par.P.Q will not resolve to the
15957 -- name of the instance, whose enclosing scope is not necessarily
15958 -- Par. We use the generic homonym rather that the name of the
15959 -- generic itself because it may be hidden by a local declaration.
15961 elsif In_Open_Scopes
(Entity
(Parent
(N2
)))
15963 Is_Generic_Unit
(Top_Ancestor
(Entity
(Prefix
(Parent
(N2
)))))
15965 if Ekind
(Entity
(Parent
(N2
))) = E_Generic_Package
then
15966 Rewrite
(Parent
(N
),
15967 Make_Identifier
(Sloc
(N
),
15969 Chars
(Generic_Homonym
(Entity
(Parent
(N2
))))));
15971 Rewrite
(Parent
(N
),
15972 Make_Identifier
(Sloc
(N
),
15973 Chars
=> Chars
(Selector_Name
(Parent
(N2
)))));
15977 if Nkind
(Parent
(Parent
(N
))) in N_Generic_Instantiation
15978 and then Parent
(N
) = Name
(Parent
(Parent
(N
)))
15980 Save_Global_Defaults
15981 (Parent
(Parent
(N
)), Parent
(Parent
(N2
)));
15984 -- A selected component may denote a static constant that has been
15985 -- folded. If the static constant is global to the generic, capture
15986 -- its value. Otherwise the folding will happen in any instantiation.
15988 elsif Nkind
(Parent
(N
)) = N_Selected_Component
15989 and then Nkind
(Parent
(N2
)) in N_Integer_Literal | N_Real_Literal
15991 if Present
(Entity
(Original_Node
(Parent
(N2
))))
15992 and then Is_Global
(Entity
(Original_Node
(Parent
(N2
))))
15994 Rewrite
(Parent
(N
), New_Copy
(Parent
(N2
)));
15995 Set_Analyzed
(Parent
(N
), False);
15998 -- A selected component may be transformed into a parameterless
15999 -- function call. If the called entity is global, rewrite the node
16000 -- appropriately, i.e. as an extended name for the global entity.
16002 elsif Nkind
(Parent
(N
)) = N_Selected_Component
16003 and then Nkind
(Parent
(N2
)) = N_Function_Call
16004 and then N
= Selector_Name
(Parent
(N
))
16006 if No
(Parameter_Associations
(Parent
(N2
))) then
16007 if Is_Global
(Entity
(Name
(Parent
(N2
)))) then
16008 Change_Selected_Component_To_Expanded_Name
(Parent
(N
));
16009 Set_Associated_Node
(Parent
(N
), Name
(Parent
(N2
)));
16010 Set_Global_Type
(Parent
(N
), Name
(Parent
(N2
)));
16011 Save_Entity_Descendants
(N
);
16014 Set_Is_Prefixed_Call
(Parent
(N
));
16015 Set_Associated_Node
(N
, Empty
);
16016 Set_Etype
(N
, Empty
);
16019 -- In Ada 2005, X.F may be a call to a primitive operation,
16020 -- rewritten as F (X). This rewriting will be done again in an
16021 -- instance, so keep the original node. Global entities will be
16022 -- captured as for other constructs. Indicate that this must
16023 -- resolve as a call, to prevent accidental overloading in the
16024 -- instance, if both a component and a primitive operation appear
16028 Set_Is_Prefixed_Call
(Parent
(N
));
16031 -- Entity is local. Reset in generic unit, so that node is resolved
16032 -- anew at the point of instantiation.
16035 Set_Associated_Node
(N
, Empty
);
16036 Set_Etype
(N
, Empty
);
16040 -----------------------------
16041 -- Save_Entity_Descendants --
16042 -----------------------------
16044 procedure Save_Entity_Descendants
(N
: Node_Id
) is
16047 when N_Binary_Op
=>
16048 Save_Global_Descendant
(Union_Id
(Left_Opnd
(N
)));
16049 Save_Global_Descendant
(Union_Id
(Right_Opnd
(N
)));
16052 Save_Global_Descendant
(Union_Id
(Right_Opnd
(N
)));
16054 when N_Expanded_Name
16055 | N_Selected_Component
16057 Save_Global_Descendant
(Union_Id
(Prefix
(N
)));
16058 Save_Global_Descendant
(Union_Id
(Selector_Name
(N
)));
16060 when N_Character_Literal
16062 | N_Operator_Symbol
16067 raise Program_Error
;
16069 end Save_Entity_Descendants
;
16071 --------------------------
16072 -- Save_Global_Defaults --
16073 --------------------------
16075 procedure Save_Global_Defaults
(N1
: Node_Id
; N2
: Node_Id
) is
16076 Loc
: constant Source_Ptr
:= Sloc
(N1
);
16077 Assoc2
: constant List_Id
:= Generic_Associations
(N2
);
16078 Gen_Id
: constant Entity_Id
:= Get_Generic_Entity
(N2
);
16085 Actual
: Entity_Id
;
16088 Assoc1
:= Generic_Associations
(N1
);
16090 if Present
(Assoc1
) then
16091 Act1
:= First
(Assoc1
);
16094 Set_Generic_Associations
(N1
, New_List
);
16095 Assoc1
:= Generic_Associations
(N1
);
16098 if Present
(Assoc2
) then
16099 Act2
:= First
(Assoc2
);
16104 while Present
(Act1
) and then Present
(Act2
) loop
16109 -- Find the associations added for default subprograms
16111 if Present
(Act2
) then
16112 while Nkind
(Act2
) /= N_Generic_Association
16113 or else No
(Entity
(Selector_Name
(Act2
)))
16114 or else not Is_Overloadable
(Entity
(Selector_Name
(Act2
)))
16119 -- Add a similar association if the default is global. The
16120 -- renaming declaration for the actual has been analyzed, and
16121 -- its alias is the program it renames. Link the actual in the
16122 -- original generic tree with the node in the analyzed tree.
16124 while Present
(Act2
) loop
16125 Subp
:= Entity
(Selector_Name
(Act2
));
16126 Def
:= Explicit_Generic_Actual_Parameter
(Act2
);
16128 -- Following test is defence against rubbish errors
16130 if No
(Alias
(Subp
)) then
16134 -- Retrieve the resolved actual from the renaming declaration
16135 -- created for the instantiated formal.
16137 Actual
:= Entity
(Name
(Parent
(Parent
(Subp
))));
16138 Set_Entity
(Def
, Actual
);
16139 Set_Etype
(Def
, Etype
(Actual
));
16141 if Is_Global
(Actual
) then
16143 Make_Generic_Association
(Loc
,
16145 New_Occurrence_Of
(Subp
, Loc
),
16146 Explicit_Generic_Actual_Parameter
=>
16147 New_Occurrence_Of
(Actual
, Loc
));
16149 Set_Associated_Node
16150 (Explicit_Generic_Actual_Parameter
(Ndec
), Def
);
16152 Append
(Ndec
, Assoc1
);
16154 -- If there are other defaults, add a dummy association in case
16155 -- there are other defaulted formals with the same name.
16157 elsif Present
(Next
(Act2
)) then
16159 Make_Generic_Association
(Loc
,
16161 New_Occurrence_Of
(Subp
, Loc
),
16162 Explicit_Generic_Actual_Parameter
=> Empty
);
16164 Append
(Ndec
, Assoc1
);
16171 if Nkind
(Name
(N1
)) = N_Identifier
16172 and then Is_Child_Unit
(Gen_Id
)
16173 and then Is_Global
(Gen_Id
)
16174 and then Is_Generic_Unit
(Scope
(Gen_Id
))
16175 and then In_Open_Scopes
(Scope
(Gen_Id
))
16177 -- This is an instantiation of a child unit within a sibling, so
16178 -- that the generic parent is in scope. An eventual instance must
16179 -- occur within the scope of an instance of the parent. Make name
16180 -- in instance into an expanded name, to preserve the identifier
16181 -- of the parent, so it can be resolved subsequently.
16183 Rewrite
(Name
(N2
),
16184 Make_Expanded_Name
(Loc
,
16185 Chars
=> Chars
(Gen_Id
),
16186 Prefix
=> New_Occurrence_Of
(Scope
(Gen_Id
), Loc
),
16187 Selector_Name
=> New_Occurrence_Of
(Gen_Id
, Loc
)));
16188 Set_Entity
(Name
(N2
), Gen_Id
);
16190 Rewrite
(Name
(N1
),
16191 Make_Expanded_Name
(Loc
,
16192 Chars
=> Chars
(Gen_Id
),
16193 Prefix
=> New_Occurrence_Of
(Scope
(Gen_Id
), Loc
),
16194 Selector_Name
=> New_Occurrence_Of
(Gen_Id
, Loc
)));
16196 Set_Associated_Node
(Name
(N1
), Name
(N2
));
16197 Set_Associated_Node
(Prefix
(Name
(N1
)), Empty
);
16198 Set_Associated_Node
16199 (Selector_Name
(Name
(N1
)), Selector_Name
(Name
(N2
)));
16200 Set_Etype
(Name
(N1
), Etype
(Gen_Id
));
16202 end Save_Global_Defaults
;
16204 ----------------------------
16205 -- Save_Global_Descendant --
16206 ----------------------------
16208 procedure Save_Global_Descendant
(D
: Union_Id
) is
16212 if D
in Node_Range
then
16213 if D
= Union_Id
(Empty
) then
16216 elsif Nkind
(Node_Id
(D
)) /= N_Compilation_Unit
then
16217 Save_References
(Node_Id
(D
));
16220 elsif D
in List_Range
then
16221 pragma Assert
(D
/= Union_Id
(No_List
));
16222 -- Because No_List = Empty, which is in Node_Range above
16224 N1
:= First
(List_Id
(D
));
16225 while Present
(N1
) loop
16226 Save_References
(N1
);
16230 -- Element list or other non-node field, nothing to do
16235 end Save_Global_Descendant
;
16237 ---------------------
16238 -- Save_References --
16239 ---------------------
16241 -- This is the recursive procedure that does the work once the enclosing
16242 -- generic scope has been established. We have to treat specially a
16243 -- number of node rewritings that are required by semantic processing
16244 -- and which change the kind of nodes in the generic copy: typically
16245 -- constant-folding, replacing an operator node by a string literal, or
16246 -- a selected component by an expanded name. In each of those cases, the
16247 -- transformation is propagated to the generic unit.
16249 procedure Save_References
(N
: Node_Id
) is
16250 Loc
: constant Source_Ptr
:= Sloc
(N
);
16252 function Requires_Delayed_Save
(Nod
: Node_Id
) return Boolean;
16253 -- Determine whether arbitrary node Nod requires delayed capture of
16254 -- global references within its aspect specifications.
16256 procedure Save_References_In_Aggregate
(N
: Node_Id
);
16257 -- Save all global references in [extension] aggregate node N
16259 procedure Save_References_In_Char_Lit_Or_Op_Symbol
(N
: Node_Id
);
16260 -- Save all global references in a character literal or operator
16261 -- symbol denoted by N.
16263 procedure Save_References_In_Descendants
(N
: Node_Id
);
16264 -- Save all global references in all descendants of node N
16266 procedure Save_References_In_Identifier
(N
: Node_Id
);
16267 -- Save all global references in identifier node N
16269 procedure Save_References_In_Operator
(N
: Node_Id
);
16270 -- Save all global references in operator node N
16272 procedure Save_References_In_Pragma
(Prag
: Node_Id
);
16273 -- Save all global references found within the expression of pragma
16276 ---------------------------
16277 -- Requires_Delayed_Save --
16278 ---------------------------
16280 function Requires_Delayed_Save
(Nod
: Node_Id
) return Boolean is
16282 -- Generic packages and subprograms require delayed capture of
16283 -- global references within their aspects due to the timing of
16284 -- annotation analysis.
16286 if Nkind
(Nod
) in N_Generic_Package_Declaration
16287 | N_Generic_Subprogram_Declaration
16289 | N_Package_Body_Stub
16290 | N_Subprogram_Body
16291 | N_Subprogram_Body_Stub
16293 -- Since the capture of global references is done on the
16294 -- unanalyzed generic template, there is no information around
16295 -- to infer the context. Use the Associated_Entity linkages to
16296 -- peek into the analyzed generic copy and determine what the
16297 -- template corresponds to.
16299 if Nod
= Templ
then
16301 Is_Generic_Declaration_Or_Body
16302 (Unit_Declaration_Node
16303 (Associated_Entity
(Defining_Entity
(Nod
))));
16305 -- Otherwise the generic unit being processed is not the top
16306 -- level template. It is safe to capture of global references
16307 -- within the generic unit because at this point the top level
16308 -- copy is fully analyzed.
16314 -- Otherwise capture the global references without interference
16319 end Requires_Delayed_Save
;
16321 ----------------------------------
16322 -- Save_References_In_Aggregate --
16323 ----------------------------------
16325 procedure Save_References_In_Aggregate
(N
: Node_Id
) is
16327 Qual
: Node_Id
:= Empty
;
16328 Typ
: Entity_Id
:= Empty
;
16331 N2
:= Get_Associated_Node
(N
);
16333 if Present
(N2
) then
16336 -- In an instance within a generic, use the name of the actual
16337 -- and not the original generic parameter. If the actual is
16338 -- global in the current generic it must be preserved for its
16341 if Parent_Kind
(Typ
) = N_Subtype_Declaration
16342 and then Present
(Generic_Parent_Type
(Parent
(Typ
)))
16344 Typ
:= Base_Type
(Typ
);
16345 Set_Etype
(N2
, Typ
);
16349 if No
(N2
) or else No
(Typ
) or else not Is_Global
(Typ
) then
16350 Set_Associated_Node
(N
, Empty
);
16352 -- If the aggregate is an actual in a call, it has been
16353 -- resolved in the current context, to some local type. The
16354 -- enclosing call may have been disambiguated by the aggregate,
16355 -- and this disambiguation might fail at instantiation time
16356 -- because the type to which the aggregate did resolve is not
16357 -- preserved. In order to preserve some of this information,
16358 -- wrap the aggregate in a qualified expression, using the id
16359 -- of its type. For further disambiguation we qualify the type
16360 -- name with its scope (if visible and not hidden by a local
16361 -- homograph) because both id's will have corresponding
16362 -- entities in an instance. This resolves most of the problems
16363 -- with missing type information on aggregates in instances.
16366 and then Nkind
(N2
) = Nkind
(N
)
16367 and then Nkind
(Parent
(N2
)) in N_Subprogram_Call
16368 and then Present
(Typ
)
16369 and then Comes_From_Source
(Typ
)
16371 Nam
:= Make_Identifier
(Loc
, Chars
(Typ
));
16373 if Is_Immediately_Visible
(Scope
(Typ
))
16375 (not In_Open_Scopes
(Scope
(Typ
))
16376 or else Current_Entity
(Scope
(Typ
)) = Scope
(Typ
))
16379 Make_Selected_Component
(Loc
,
16381 Make_Identifier
(Loc
, Chars
(Scope
(Typ
))),
16382 Selector_Name
=> Nam
);
16386 Make_Qualified_Expression
(Loc
,
16387 Subtype_Mark
=> Nam
,
16388 Expression
=> Relocate_Node
(N
));
16392 if Nkind
(N
) = N_Aggregate
then
16393 Save_Global_Descendant
(Union_Id
(Aggregate_Bounds
(N
)));
16395 elsif Nkind
(N
) = N_Extension_Aggregate
then
16396 Save_Global_Descendant
(Union_Id
(Ancestor_Part
(N
)));
16399 pragma Assert
(False);
16402 Save_Global_Descendant
(Union_Id
(Expressions
(N
)));
16403 Save_Global_Descendant
(Union_Id
(Component_Associations
(N
)));
16404 Save_Global_Descendant
(Union_Id
(Etype
(N
)));
16406 if Present
(Qual
) then
16409 end Save_References_In_Aggregate
;
16411 ----------------------------------------------
16412 -- Save_References_In_Char_Lit_Or_Op_Symbol --
16413 ----------------------------------------------
16415 procedure Save_References_In_Char_Lit_Or_Op_Symbol
(N
: Node_Id
) is
16417 if Nkind
(N
) = Nkind
(Get_Associated_Node
(N
)) then
16420 elsif Nkind
(N
) = N_Operator_Symbol
16421 and then Nkind
(Get_Associated_Node
(N
)) = N_String_Literal
16423 Change_Operator_Symbol_To_String_Literal
(N
);
16425 end Save_References_In_Char_Lit_Or_Op_Symbol
;
16427 ------------------------------------
16428 -- Save_References_In_Descendants --
16429 ------------------------------------
16431 procedure Save_References_In_Descendants
(N
: Node_Id
) is
16432 procedure Walk
is new Walk_Sinfo_Fields
(Save_Global_Descendant
);
16435 end Save_References_In_Descendants
;
16437 -----------------------------------
16438 -- Save_References_In_Identifier --
16439 -----------------------------------
16441 procedure Save_References_In_Identifier
(N
: Node_Id
) is
16443 -- The node did not undergo a transformation
16445 if Nkind
(N
) = Nkind
(Get_Associated_Node
(N
)) then
16446 -- If this is a discriminant reference, always save it.
16447 -- It is used in the instance to find the corresponding
16448 -- discriminant positionally rather than by name.
16450 Set_Original_Discriminant
16451 (N
, Original_Discriminant
(Get_Associated_Node
(N
)));
16455 -- The analysis of the generic copy transformed the identifier
16456 -- into another construct. Propagate the changes to the template.
16459 N2
:= Get_Associated_Node
(N
);
16461 -- The identifier denotes a call to a parameterless function.
16462 -- Mark the node as resolved when the function is external.
16464 if Nkind
(N2
) = N_Function_Call
then
16465 E
:= Entity
(Name
(N2
));
16467 if Present
(E
) and then Is_Global
(E
) then
16468 Set_Etype
(N
, Etype
(N2
));
16470 Set_Associated_Node
(N
, Empty
);
16471 Set_Etype
(N
, Empty
);
16474 -- The identifier denotes a named number that was constant
16475 -- folded. Preserve the original name for ASIS and undo the
16476 -- constant folding which will be repeated in the instance.
16477 -- Is this still needed???
16479 elsif Nkind
(N2
) in N_Integer_Literal | N_Real_Literal
16480 and then Is_Entity_Name
(Original_Node
(N2
))
16482 Set_Associated_Node
(N
, Original_Node
(N2
));
16485 -- The identifier resolved to a string literal. Propagate this
16486 -- information to the generic template.
16488 elsif Nkind
(N2
) = N_String_Literal
then
16489 Rewrite
(N
, New_Copy
(N2
));
16491 -- The identifier is rewritten as a dereference if it is the
16492 -- prefix of an implicit dereference. Preserve the original
16493 -- tree as the analysis of the instance will expand the node
16494 -- again, but preserve the resolved entity if it is global.
16496 elsif Nkind
(N2
) = N_Explicit_Dereference
then
16497 if Is_Entity_Name
(Prefix
(N2
))
16498 and then Present
(Entity
(Prefix
(N2
)))
16499 and then Is_Global
(Entity
(Prefix
(N2
)))
16501 Set_Associated_Node
(N
, Prefix
(N2
));
16503 elsif Nkind
(Prefix
(N2
)) = N_Function_Call
16504 and then Present
(Entity
(Name
(Prefix
(N2
))))
16505 and then Is_Global
(Entity
(Name
(Prefix
(N2
))))
16508 Make_Explicit_Dereference
(Loc
,
16510 Make_Function_Call
(Loc
,
16513 (Entity
(Name
(Prefix
(N2
))), Loc
))));
16516 Set_Associated_Node
(N
, Empty
);
16517 Set_Etype
(N
, Empty
);
16520 -- The subtype mark of a nominally unconstrained object is
16521 -- rewritten as a subtype indication using the bounds of the
16522 -- expression. Recover the original subtype mark.
16524 elsif Nkind
(N2
) = N_Subtype_Indication
16525 and then Is_Entity_Name
(Original_Node
(N2
))
16527 Set_Associated_Node
(N
, Original_Node
(N2
));
16531 end Save_References_In_Identifier
;
16533 ---------------------------------
16534 -- Save_References_In_Operator --
16535 ---------------------------------
16537 procedure Save_References_In_Operator
(N
: Node_Id
) is
16539 -- The node did not undergo a transformation
16541 if Nkind
(N
) = Nkind
(Get_Associated_Node
(N
)) then
16542 if Nkind
(N
) = N_Op_Concat
then
16543 Set_Is_Component_Left_Opnd
(N
,
16544 Is_Component_Left_Opnd
(Get_Associated_Node
(N
)));
16546 Set_Is_Component_Right_Opnd
(N
,
16547 Is_Component_Right_Opnd
(Get_Associated_Node
(N
)));
16552 -- The analysis of the generic copy transformed the operator into
16553 -- some other construct. Propagate the changes to the template if
16557 N2
:= Get_Associated_Node
(N
);
16559 -- The operator resoved to a function call
16561 if Nkind
(N2
) = N_Function_Call
then
16563 -- Add explicit qualifications in the generic template for
16564 -- all operands of universal type. This aids resolution by
16565 -- preserving the actual type of a literal or an attribute
16566 -- that yields a universal result.
16568 Qualify_Universal_Operands
(N
, N2
);
16570 E
:= Entity
(Name
(N2
));
16572 if Present
(E
) and then Is_Global
(E
) then
16573 Set_Etype
(N
, Etype
(N2
));
16575 Set_Associated_Node
(N
, Empty
);
16576 Set_Etype
(N
, Empty
);
16579 -- The operator was folded into a literal
16581 elsif Nkind
(N2
) in N_Integer_Literal
16585 if Present
(Original_Node
(N2
))
16586 and then Nkind
(Original_Node
(N2
)) = Nkind
(N
)
16588 -- Operation was constant-folded. Whenever possible,
16589 -- recover semantic information from unfolded node.
16590 -- This was initially done for ASIS but is apparently
16591 -- needed also for e.g. compiling a-nbnbin.adb.
16593 Set_Associated_Node
(N
, Original_Node
(N2
));
16595 if Nkind
(N
) = N_Op_Concat
then
16596 Set_Is_Component_Left_Opnd
(N
,
16597 Is_Component_Left_Opnd
(Get_Associated_Node
(N
)));
16598 Set_Is_Component_Right_Opnd
(N
,
16599 Is_Component_Right_Opnd
(Get_Associated_Node
(N
)));
16604 -- Propagate the constant folding back to the template
16607 Rewrite
(N
, New_Copy
(N2
));
16608 Set_Analyzed
(N
, False);
16611 -- The operator was folded into an enumeration literal. Retain
16612 -- the entity to avoid spurious ambiguities if it is overloaded
16613 -- at the point of instantiation or inlining.
16615 elsif Nkind
(N2
) = N_Identifier
16616 and then Ekind
(Entity
(N2
)) = E_Enumeration_Literal
16618 Rewrite
(N
, New_Copy
(N2
));
16619 Set_Analyzed
(N
, False);
16623 -- Complete the operands check if node has not been constant
16626 if Nkind
(N
) in N_Op
then
16627 Save_Entity_Descendants
(N
);
16629 end Save_References_In_Operator
;
16631 -------------------------------
16632 -- Save_References_In_Pragma --
16633 -------------------------------
16635 procedure Save_References_In_Pragma
(Prag
: Node_Id
) is
16637 Do_Save
: Boolean := True;
16640 -- Do not save global references in pragmas generated from aspects
16641 -- because the pragmas will be regenerated at instantiation time.
16643 if From_Aspect_Specification
(Prag
) then
16646 -- The capture of global references within contract-related source
16647 -- pragmas associated with generic packages, subprograms or their
16648 -- respective bodies must be delayed due to timing of annotation
16649 -- analysis. Global references are still captured in routine
16650 -- Save_Global_References_In_Contract.
16652 elsif Is_Generic_Contract_Pragma
(Prag
) and then Prag
/= Templ
then
16653 if Is_Package_Contract_Annotation
(Prag
) then
16654 Context
:= Find_Related_Package_Or_Body
(Prag
);
16656 pragma Assert
(Is_Subprogram_Contract_Annotation
(Prag
));
16657 Context
:= Find_Related_Declaration_Or_Body
(Prag
);
16660 -- The use of Original_Node accounts for the case when the
16661 -- related context is generic template.
16663 if Requires_Delayed_Save
(Original_Node
(Context
)) then
16668 -- For all other cases, save all global references within the
16669 -- descendants, but skip the following semantic fields:
16670 -- Next_Pragma, Corresponding_Aspect, Next_Rep_Item.
16673 Save_Global_Descendant
16674 (Union_Id
(Pragma_Argument_Associations
(N
)));
16675 Save_Global_Descendant
(Union_Id
(Pragma_Identifier
(N
)));
16677 end Save_References_In_Pragma
;
16679 -- Start of processing for Save_References
16687 elsif Nkind
(N
) in N_Aggregate | N_Extension_Aggregate
then
16688 Save_References_In_Aggregate
(N
);
16690 -- Character literals, operator symbols
16692 elsif Nkind
(N
) in N_Character_Literal | N_Operator_Symbol
then
16693 Save_References_In_Char_Lit_Or_Op_Symbol
(N
);
16695 -- Defining identifiers
16697 elsif Nkind
(N
) in N_Entity
then
16702 elsif Nkind
(N
) = N_Identifier
then
16703 Save_References_In_Identifier
(N
);
16707 elsif Nkind
(N
) in N_Op
then
16708 Save_References_In_Operator
(N
);
16712 elsif Nkind
(N
) = N_Pragma
then
16713 Save_References_In_Pragma
(N
);
16716 Save_References_In_Descendants
(N
);
16719 -- Save all global references found within the aspect specifications
16720 -- of the related node.
16722 if Permits_Aspect_Specifications
(N
) and then Has_Aspects
(N
) then
16724 -- The capture of global references within aspects associated with
16725 -- generic packages, subprograms or their bodies must be delayed
16726 -- due to timing of annotation analysis. Global references are
16727 -- still captured in routine Save_Global_References_In_Contract.
16729 if Requires_Delayed_Save
(N
) then
16732 -- Otherwise save all global references within the aspects
16735 Save_Global_References_In_Aspects
(N
);
16738 end Save_References
;
16740 -- Start of processing for Save_Global_References
16743 Gen_Scope
:= Current_Scope
;
16745 -- If the generic unit is a child unit, references to entities in the
16746 -- parent are treated as local, because they will be resolved anew in
16747 -- the context of the instance of the parent.
16749 while Is_Child_Unit
(Gen_Scope
)
16750 and then Ekind
(Scope
(Gen_Scope
)) = E_Generic_Package
16752 Gen_Scope
:= Scope
(Gen_Scope
);
16755 Save_References
(Templ
);
16756 end Save_Global_References
;
16758 ---------------------------------------
16759 -- Save_Global_References_In_Aspects --
16760 ---------------------------------------
16762 procedure Save_Global_References_In_Aspects
(N
: Node_Id
) is
16767 Asp
:= First
(Aspect_Specifications
(N
));
16768 while Present
(Asp
) loop
16769 Expr
:= Expression
(Asp
);
16771 if Present
(Expr
) then
16772 Save_Global_References
(Expr
);
16777 end Save_Global_References_In_Aspects
;
16779 ------------------------------------------
16780 -- Set_Copied_Sloc_For_Inherited_Pragma --
16781 ------------------------------------------
16783 procedure Set_Copied_Sloc_For_Inherited_Pragma
16788 Create_Instantiation_Source
(N
, E
,
16789 Inlined_Body
=> False,
16790 Inherited_Pragma
=> True,
16791 Factor
=> S_Adjustment
);
16792 end Set_Copied_Sloc_For_Inherited_Pragma
;
16794 --------------------------------------
16795 -- Set_Copied_Sloc_For_Inlined_Body --
16796 --------------------------------------
16798 procedure Set_Copied_Sloc_For_Inlined_Body
(N
: Node_Id
; E
: Entity_Id
) is
16800 Create_Instantiation_Source
(N
, E
,
16801 Inlined_Body
=> True,
16802 Inherited_Pragma
=> False,
16803 Factor
=> S_Adjustment
);
16804 end Set_Copied_Sloc_For_Inlined_Body
;
16806 ---------------------
16807 -- Set_Instance_Of --
16808 ---------------------
16810 procedure Set_Instance_Of
(A
: Entity_Id
; B
: Entity_Id
) is
16812 Generic_Renamings
.Table
(Generic_Renamings
.Last
) := (A
, B
, Assoc_Null
);
16813 Generic_Renamings_HTable
.Set
(Generic_Renamings
.Last
);
16814 Generic_Renamings
.Increment_Last
;
16815 end Set_Instance_Of
;
16817 --------------------
16818 -- Set_Next_Assoc --
16819 --------------------
16821 procedure Set_Next_Assoc
(E
: Assoc_Ptr
; Next
: Assoc_Ptr
) is
16823 Generic_Renamings
.Table
(E
).Next_In_HTable
:= Next
;
16824 end Set_Next_Assoc
;
16826 -------------------
16827 -- Start_Generic --
16828 -------------------
16830 procedure Start_Generic
is
16832 -- ??? More things could be factored out in this routine.
16833 -- Should probably be done at a later stage.
16835 Generic_Flags
.Append
(Inside_A_Generic
);
16836 Inside_A_Generic
:= True;
16838 Expander_Mode_Save_And_Set
(False);
16841 ----------------------
16842 -- Set_Instance_Env --
16843 ----------------------
16845 -- WARNING: This routine manages SPARK regions
16847 procedure Set_Instance_Env
16848 (Gen_Unit
: Entity_Id
;
16849 Act_Unit
: Entity_Id
)
16851 Saved_AE
: constant Boolean := Assertions_Enabled
;
16852 Saved_CPL
: constant Node_Id
:= Check_Policy_List
;
16853 Saved_DEC
: constant Boolean := Dynamic_Elaboration_Checks
;
16854 Saved_SM
: constant SPARK_Mode_Type
:= SPARK_Mode
;
16855 Saved_SMP
: constant Node_Id
:= SPARK_Mode_Pragma
;
16858 -- Regardless of the current mode, predefined units are analyzed in the
16859 -- most current Ada mode, and earlier version Ada checks do not apply
16860 -- to predefined units. Nothing needs to be done for non-internal units.
16861 -- These are always analyzed in the current mode.
16863 if In_Internal_Unit
(Gen_Unit
) then
16865 -- The following call resets all configuration attributes to default
16866 -- or the xxx_Config versions of the attributes when the current sem
16867 -- unit is the main unit. At the same time, internal units must also
16868 -- inherit certain configuration attributes from their context. It
16869 -- is unclear what these two sets are.
16871 Set_Config_Switches
(True, Current_Sem_Unit
= Main_Unit
);
16873 -- Reinstall relevant configuration attributes of the context
16875 Assertions_Enabled
:= Saved_AE
;
16876 Check_Policy_List
:= Saved_CPL
;
16877 Dynamic_Elaboration_Checks
:= Saved_DEC
;
16879 Install_SPARK_Mode
(Saved_SM
, Saved_SMP
);
16882 Current_Instantiated_Parent
:=
16883 (Gen_Id
=> Gen_Unit
,
16884 Act_Id
=> Act_Unit
,
16885 Next_In_HTable
=> Assoc_Null
);
16886 end Set_Instance_Env
;
16892 procedure Switch_View
(T
: Entity_Id
) is
16893 BT
: constant Entity_Id
:= Base_Type
(T
);
16894 Priv_Elmt
: Elmt_Id
:= No_Elmt
;
16895 Priv_Sub
: Entity_Id
;
16898 -- T may be private but its base type may have been exchanged through
16899 -- some other occurrence, in which case there is nothing to switch
16900 -- besides T itself. Note that a private dependent subtype of a private
16901 -- type might not have been switched even if the base type has been,
16902 -- because of the last branch of Check_Private_View (see comment there).
16904 if not Is_Private_Type
(BT
) then
16905 Prepend_Elmt
(Full_View
(T
), Exchanged_Views
);
16906 Exchange_Declarations
(T
);
16910 Priv_Elmt
:= First_Elmt
(Private_Dependents
(BT
));
16912 if Present
(Full_View
(BT
)) then
16913 Prepend_Elmt
(Full_View
(BT
), Exchanged_Views
);
16914 Exchange_Declarations
(BT
);
16917 while Present
(Priv_Elmt
) loop
16918 Priv_Sub
:= Node
(Priv_Elmt
);
16920 if Present
(Full_View
(Priv_Sub
)) then
16921 Prepend_Elmt
(Full_View
(Priv_Sub
), Exchanged_Views
);
16922 Exchange_Declarations
(Priv_Sub
);
16925 Next_Elmt
(Priv_Elmt
);
16933 function True_Parent
(N
: Node_Id
) return Node_Id
is
16935 if Nkind
(Parent
(N
)) = N_Subunit
then
16936 return Parent
(Corresponding_Stub
(Parent
(N
)));
16942 -----------------------------
16943 -- Valid_Default_Attribute --
16944 -----------------------------
16946 procedure Valid_Default_Attribute
(Nam
: Entity_Id
; Def
: Node_Id
) is
16947 Attr_Id
: constant Attribute_Id
:=
16948 Get_Attribute_Id
(Attribute_Name
(Def
));
16949 T
: constant Entity_Id
:= Entity
(Prefix
(Def
));
16950 Is_Fun
: constant Boolean := (Ekind
(Nam
) = E_Function
);
16956 if No
(T
) or else T
= Any_Id
then
16961 F
:= First_Formal
(Nam
);
16962 while Present
(F
) loop
16963 Num_F
:= Num_F
+ 1;
16968 when Attribute_Adjacent
16969 | Attribute_Ceiling
16970 | Attribute_Copy_Sign
16972 | Attribute_Fraction
16973 | Attribute_Machine
16975 | Attribute_Remainder
16976 | Attribute_Rounding
16977 | Attribute_Unbiased_Rounding
16981 and then Is_Floating_Point_Type
(T
);
16983 when Attribute_Image
16987 | Attribute_Wide_Image
16988 | Attribute_Wide_Value
16990 OK
:= Is_Fun
and then Num_F
= 1 and then Is_Scalar_Type
(T
);
16995 OK
:= Is_Fun
and then Num_F
= 2 and then Is_Scalar_Type
(T
);
16997 when Attribute_Input
=>
16998 OK
:= (Is_Fun
and then Num_F
= 1);
17000 when Attribute_Output
17001 | Attribute_Put_Image
17005 OK
:= not Is_Fun
and then Num_F
= 2;
17013 ("attribute reference has wrong profile for subprogram", Def
);
17015 end Valid_Default_Attribute
;
17017 ----------------------------------
17018 -- Validate_Formal_Type_Default --
17019 ----------------------------------
17021 procedure Validate_Formal_Type_Default
(Decl
: Node_Id
) is
17022 Default
: constant Node_Id
:=
17023 Default_Subtype_Mark
(Original_Node
(Decl
));
17024 Formal
: constant Entity_Id
:= Defining_Identifier
(Decl
);
17026 Def_Sub
: Entity_Id
; -- Default subtype mark
17027 Type_Def
: Node_Id
;
17029 procedure Check_Discriminated_Formal
;
17030 -- Check that discriminants of default for private or incomplete
17031 -- type match those of formal type.
17033 function Reference_Formal
(N
: Node_Id
) return Traverse_Result
;
17034 -- Check whether formal type definition mentions a previous formal
17035 -- type of the same generic.
17037 ----------------------
17038 -- Reference_Formal --
17039 ----------------------
17041 function Reference_Formal
(N
: Node_Id
) return Traverse_Result
is
17043 if Is_Entity_Name
(N
)
17044 and then Scope
(Entity
(N
)) = Current_Scope
17050 end Reference_Formal
;
17052 function Depends_On_Other_Formals
is
17053 new Traverse_Func
(Reference_Formal
);
17055 function Default_Subtype_Matches
17056 (Gen_T
, Def_T
: Entity_Id
) return Boolean;
17058 procedure Validate_Array_Type_Default
;
17059 -- Verify that dimension, indices, and component types of default
17060 -- are compatible with formal array type definition.
17062 procedure Validate_Derived_Type_Default
;
17063 -- Verify that ancestor and progenitor types match.
17065 ---------------------------------
17066 -- Check_Discriminated_Formal --
17067 ---------------------------------
17069 procedure Check_Discriminated_Formal
is
17070 Formal_Discr
: Entity_Id
;
17071 Actual_Discr
: Entity_Id
;
17072 Formal_Subt
: Entity_Id
;
17075 if Has_Discriminants
(Formal
) then
17076 if not Has_Discriminants
(Def_Sub
) then
17078 ("default for & must have discriminants", Default
, Formal
);
17080 elsif Is_Constrained
(Def_Sub
) then
17082 ("default for & must be unconstrained", Default
, Formal
);
17085 Formal_Discr
:= First_Discriminant
(Formal
);
17086 Actual_Discr
:= First_Discriminant
(Def_Sub
);
17087 while Formal_Discr
/= Empty
loop
17088 if Actual_Discr
= Empty
then
17090 ("discriminants on Formal do not match formal",
17094 Formal_Subt
:= Etype
(Formal_Discr
);
17096 -- Access discriminants match if designated types do
17098 if Ekind
(Base_Type
(Formal_Subt
)) = E_Anonymous_Access_Type
17099 and then (Ekind
(Base_Type
(Etype
(Actual_Discr
)))) =
17100 E_Anonymous_Access_Type
17102 Designated_Type
(Base_Type
(Formal_Subt
)) =
17103 Designated_Type
(Base_Type
(Etype
(Actual_Discr
)))
17107 elsif Base_Type
(Formal_Subt
) /=
17108 Base_Type
(Etype
(Actual_Discr
))
17111 ("types of discriminants of default must match formal",
17114 elsif not Subtypes_Statically_Match
17115 (Formal_Subt
, Etype
(Actual_Discr
))
17116 and then Ada_Version
>= Ada_95
17119 ("subtypes of discriminants of default "
17120 & "must match formal",
17124 Next_Discriminant
(Formal_Discr
);
17125 Next_Discriminant
(Actual_Discr
);
17128 if Actual_Discr
/= Empty
then
17130 ("discriminants on default do not match formal",
17135 end Check_Discriminated_Formal
;
17137 ---------------------------
17138 -- Default_Subtype_Matches --
17139 ---------------------------
17141 function Default_Subtype_Matches
17142 (Gen_T
, Def_T
: Entity_Id
) return Boolean
17145 -- Check that the base types, root types (when dealing with class
17146 -- wide types), or designated types (when dealing with anonymous
17147 -- access types) of Gen_T and Def_T are statically matching subtypes.
17149 return (Base_Type
(Gen_T
) = Base_Type
(Def_T
)
17150 and then Subtypes_Statically_Match
(Gen_T
, Def_T
))
17152 or else (Is_Class_Wide_Type
(Gen_T
)
17153 and then Is_Class_Wide_Type
(Def_T
)
17154 and then Default_Subtype_Matches
17155 (Root_Type
(Gen_T
), Root_Type
(Def_T
)))
17157 or else (Is_Anonymous_Access_Type
(Gen_T
)
17158 and then Ekind
(Def_T
) = Ekind
(Gen_T
)
17159 and then Subtypes_Statically_Match
17160 (Designated_Type
(Gen_T
), Designated_Type
(Def_T
)));
17162 end Default_Subtype_Matches
;
17164 ----------------------------------
17165 -- Validate_Array_Type_Default --
17166 ----------------------------------
17168 procedure Validate_Array_Type_Default
is
17172 if not Is_Array_Type
(Def_Sub
) then
17173 Error_Msg_NE
("default for& must be an array type ",
17177 elsif Number_Dimensions
(Def_Sub
) /= Number_Dimensions
(Formal
)
17178 or else Is_Constrained
(Def_Sub
) /=
17179 Is_Constrained
(Formal
)
17181 Error_Msg_NE
("default array type does not match&",
17186 I1
:= First_Index
(Formal
);
17187 I2
:= First_Index
(Def_Sub
);
17188 for J
in 1 .. Number_Dimensions
(Formal
) loop
17190 -- If the indexes of the actual were given by a subtype_mark,
17191 -- the index was transformed into a range attribute. Retrieve
17192 -- the original type mark for checking.
17194 if Is_Entity_Name
(Original_Node
(I2
)) then
17195 T2
:= Entity
(Original_Node
(I2
));
17200 if not Subtypes_Statically_Match
(Etype
(I1
), T2
) then
17202 ("index types of default do not match those of formal &",
17210 if not Default_Subtype_Matches
17211 (Component_Type
(Formal
), Component_Type
(Def_Sub
))
17214 ("component subtype of default does not match that of formal &",
17218 if Has_Aliased_Components
(Formal
)
17219 and then not Has_Aliased_Components
(Default
)
17222 ("default must have aliased components to match formal type &",
17225 end Validate_Array_Type_Default
;
17227 -----------------------------------
17228 -- Validate_Derived_Type_Default --
17229 -----------------------------------
17231 procedure Validate_Derived_Type_Default
is
17233 if not Is_Ancestor
(Etype
(Formal
), Def_Sub
) then
17234 Error_Msg_NE
("default must be a descendent of&",
17235 Default
, Etype
(Formal
));
17238 if Has_Interfaces
(Formal
) then
17239 if not Has_Interfaces
(Def_Sub
) then
17241 ("default must implement all interfaces of formal&",
17246 Act_Iface_List
: Elist_Id
;
17248 Iface_Ent
: Entity_Id
;
17251 Iface
:= First
(Abstract_Interface_List
(Formal
));
17252 Collect_Interfaces
(Def_Sub
, Act_Iface_List
);
17254 while Present
(Iface
) loop
17255 Iface_Ent
:= Entity
(Iface
);
17257 if Is_Ancestor
(Iface_Ent
, Def_Sub
)
17258 or else Is_Progenitor
(Iface_Ent
, Def_Sub
)
17264 ("Default must implement interface&",
17265 Default
, Etype
(Iface
));
17273 end Validate_Derived_Type_Default
;
17275 -- Start of processing for Validate_Formal_Type_Default
17279 if not Is_Entity_Name
(Default
)
17280 or else not Is_Type
(Entity
(Default
))
17283 ("Expect type name for default of formal type", Default
);
17286 Def_Sub
:= Entity
(Default
);
17289 -- Formal derived_type declarations are transformed into full
17290 -- type declarations or Private_Type_Extensions for ease of processing.
17292 if Nkind
(Decl
) = N_Full_Type_Declaration
then
17293 Type_Def
:= Type_Definition
(Decl
);
17295 elsif Nkind
(Decl
) = N_Private_Extension_Declaration
then
17296 Type_Def
:= Subtype_Indication
(Decl
);
17299 Type_Def
:= Formal_Type_Definition
(Decl
);
17302 if Depends_On_Other_Formals
(Type_Def
) = Abandon
17303 and then Scope
(Def_Sub
) /= Current_Scope
17305 Error_Msg_N
("default of formal type that depends on "
17306 & "other formals must be a previous formal type", Default
);
17309 elsif Def_Sub
= Formal
then
17311 ("default for formal type cannot be formal itsef", Default
);
17315 case Nkind
(Type_Def
) is
17317 when N_Formal_Private_Type_Definition
=>
17318 if (Is_Abstract_Type
(Formal
)
17319 and then not Is_Abstract_Type
(Def_Sub
))
17320 or else (Is_Limited_Type
(Formal
)
17321 and then not Is_Limited_Type
(Def_Sub
))
17324 ("default for private type$ does not match",
17328 Check_Discriminated_Formal
;
17330 when N_Formal_Derived_Type_Definition
=>
17331 Check_Discriminated_Formal
;
17332 Validate_Derived_Type_Default
;
17334 when N_Formal_Incomplete_Type_Definition
=>
17335 if Is_Tagged_Type
(Formal
)
17336 and then not Is_Tagged_Type
(Def_Sub
)
17339 ("default for & must be a tagged type", Default
, Formal
);
17342 Check_Discriminated_Formal
;
17344 when N_Formal_Discrete_Type_Definition
=>
17345 if not Is_Discrete_Type
(Def_Sub
) then
17346 Error_Msg_NE
("default for& must be a discrete type",
17350 when N_Formal_Signed_Integer_Type_Definition
=>
17351 if not Is_Integer_Type
(Def_Sub
) then
17352 Error_Msg_NE
("default for& must be a discrete type",
17356 when N_Formal_Modular_Type_Definition
=>
17357 if not Is_Modular_Integer_Type
(Def_Sub
) then
17358 Error_Msg_NE
("default for& must be a modular_integer Type",
17362 when N_Formal_Floating_Point_Definition
=>
17363 if not Is_Floating_Point_Type
(Def_Sub
) then
17364 Error_Msg_NE
("default for& must be a floating_point type",
17368 when N_Formal_Ordinary_Fixed_Point_Definition
=>
17369 if not Is_Ordinary_Fixed_Point_Type
(Def_Sub
) then
17370 Error_Msg_NE
("default for& must be "
17371 & "an ordinary_fixed_point type ",
17375 when N_Formal_Decimal_Fixed_Point_Definition
=>
17376 if not Is_Decimal_Fixed_Point_Type
(Def_Sub
) then
17377 Error_Msg_NE
("default for& must be "
17378 & "an Decimal_fixed_point type ",
17382 when N_Array_Type_Definition
=>
17383 Validate_Array_Type_Default
;
17385 when N_Access_Function_Definition |
17386 N_Access_Procedure_Definition
=>
17387 if Ekind
(Def_Sub
) /= E_Access_Subprogram_Type
then
17388 Error_Msg_NE
("default for& must be an Access_To_Subprogram",
17391 Check_Subtype_Conformant
17392 (Designated_Type
(Formal
), Designated_Type
(Def_Sub
));
17394 when N_Access_To_Object_Definition
=>
17395 if not Is_Access_Object_Type
(Def_Sub
) then
17396 Error_Msg_NE
("default for& must be an Access_To_Object",
17399 elsif not Default_Subtype_Matches
17400 (Designated_Type
(Formal
), Designated_Type
(Def_Sub
))
17402 Error_Msg_NE
("designated type of defaul does not match "
17403 & "designated type of formal type",
17407 when N_Record_Definition
=> -- Formal interface type
17408 if not Is_Interface
(Def_Sub
) then
17410 ("default for formal interface type must be an interface",
17413 elsif Is_Limited_Type
(Def_Sub
) /= Is_Limited_Type
(Formal
)
17414 or else Is_Task_Interface
(Formal
) /= Is_Task_Interface
(Def_Sub
)
17415 or else Is_Protected_Interface
(Formal
) /=
17416 Is_Protected_Interface
(Def_Sub
)
17417 or else Is_Synchronized_Interface
(Formal
) /=
17418 Is_Synchronized_Interface
(Def_Sub
)
17421 ("default for interface& does not match", Def_Sub
, Formal
);
17424 when N_Derived_Type_Definition
=>
17425 Validate_Derived_Type_Default
;
17427 when N_Identifier
=> -- case of a private extension
17428 Validate_Derived_Type_Default
;
17434 raise Program_Error
;
17436 end Validate_Formal_Type_Default
;