1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
9 -- Copyright (C) 1992-2024, 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 Debug; use Debug;
30 with Einfo; use Einfo;
31 with Einfo.Entities; use Einfo.Entities;
32 with Einfo.Utils; use Einfo.Utils;
33 with Elists; use Elists;
34 with Errout; use Errout;
35 with Expander; use Expander;
36 with Fname; use Fname;
37 with Fname.UF; use Fname.UF;
38 with Freeze; use Freeze;
39 with Ghost; use Ghost;
40 with Itypes; use Itypes;
42 with Lib.Load; use Lib.Load;
43 with Lib.Xref; use Lib.Xref;
44 with Mutably_Tagged; use Mutably_Tagged;
45 with Nlists; use Nlists;
46 with Namet; use Namet;
47 with Nmake; use Nmake;
49 with Rident; use Rident;
50 with Restrict; use Restrict;
51 with Rtsfind; use Rtsfind;
53 with Sem_Aux; use Sem_Aux;
54 with Sem_Cat; use Sem_Cat;
55 with Sem_Ch3; use Sem_Ch3;
56 with Sem_Ch6; use Sem_Ch6;
57 with Sem_Ch7; use Sem_Ch7;
58 with Sem_Ch8; use Sem_Ch8;
59 with Sem_Ch10; use Sem_Ch10;
60 with Sem_Ch13; use Sem_Ch13;
61 with Sem_Dim; use Sem_Dim;
62 with Sem_Disp; use Sem_Disp;
63 with Sem_Elab; use Sem_Elab;
64 with Sem_Elim; use Sem_Elim;
65 with Sem_Eval; use Sem_Eval;
66 with Sem_Prag; use Sem_Prag;
67 with Sem_Res; use Sem_Res;
68 with Sem_Type; use Sem_Type;
69 with Sem_Util; use Sem_Util;
70 with Sem_Warn; use Sem_Warn;
71 with Stand; use Stand;
72 with Sinfo; use Sinfo;
73 with Sinfo.Nodes; use Sinfo.Nodes;
74 with Sinfo.Utils; use Sinfo.Utils;
75 with Sinfo.CN; use Sinfo.CN;
76 with Sinput; use Sinput;
77 with Sinput.L; use Sinput.L;
78 with Snames; use Snames;
79 with Stringt; use Stringt;
80 with Uname; use Uname;
82 with Tbuild; use Tbuild;
83 with Uintp; use Uintp;
84 with Urealp; use Urealp;
85 with Warnsw; use Warnsw;
89 package body Sem_Ch12 is
91 ----------------------------------------------------------
92 -- Implementation of Generic Analysis and Instantiation --
93 ----------------------------------------------------------
95 -- GNAT implements generics by macro expansion. No attempt is made to share
96 -- generic instantiations (for now). Analysis of a generic definition does
97 -- not perform any expansion action, but the expander must be called on the
98 -- tree for each instantiation, because the expansion may of course depend
99 -- on the generic actuals. All of this is best achieved as follows:
101 -- a) Semantic analysis of a generic unit is performed on a copy of the
102 -- tree for the generic unit. All tree modifications that follow analysis
103 -- do not affect the original tree. Links are kept between the original
104 -- tree and the copy, in order to recognize non-local references within
105 -- the generic, and propagate them to each instance (recall that name
106 -- resolution is done on the generic declaration: generics are not really
107 -- macros). This is summarized in the following diagram:
109 -- .-----------. .----------.
110 -- | semantic |<--------------| generic |
112 -- | |==============>| |
113 -- |___________| global |__________|
124 -- b) Each instantiation copies the original tree, and inserts into it a
125 -- series of declarations that describe the mapping between generic formals
126 -- and actuals. For example, a generic In OUT parameter is an object
127 -- renaming of the corresponding actual, etc. Generic IN parameters are
128 -- constant declarations.
130 -- c) In order to give the right visibility for these renamings, we use
131 -- a different scheme for package and subprogram instantiations. For
132 -- packages, the list of renamings is inserted into the package
133 -- specification, before the visible declarations of the package. The
134 -- renamings are analyzed before any of the text of the instance, and are
135 -- thus visible at the right place. Furthermore, outside of the instance,
136 -- the generic parameters are visible and denote their corresponding
139 -- For subprograms, we create a container package to hold the renamings
140 -- and the subprogram instance itself. Analysis of the package makes the
141 -- renaming declarations visible to the subprogram. After analyzing the
142 -- package, the defining entity for the subprogram is touched-up so that
143 -- it appears declared in the current scope, and not inside the container
146 -- If the instantiation is a compilation unit, the container package is
147 -- given the same name as the subprogram instance. This ensures that
148 -- the elaboration procedure called by the binder, using the compilation
149 -- unit name, calls in fact the elaboration procedure for the package.
151 -- Not surprisingly, private types complicate this approach. By saving in
152 -- the original generic object the non-local references, we guarantee that
153 -- the proper entities are referenced at the point of instantiation.
154 -- However, for private types, this by itself does not insure that the
155 -- proper VIEW of the entity is used (the full type may be visible at the
156 -- point of generic definition, but not at instantiation, or vice-versa).
157 -- In order to reference the proper view, we special-case any reference
158 -- to private types in the generic object, by saving both views, one in
159 -- the generic and one in the semantic copy. At time of instantiation, we
160 -- check whether the two views are consistent, and exchange declarations if
161 -- necessary, in order to restore the correct visibility. Similarly, if
162 -- the instance view is private when the generic view was not, we perform
163 -- the exchange. After completing the instantiation, we restore the
164 -- current visibility. The flag Has_Private_View marks identifiers in the
165 -- the generic unit that require checking.
167 -- Visibility within nested generic units requires special handling.
168 -- Consider the following scheme:
170 -- type Global is ... -- outside of generic unit.
174 -- type Semi_Global is ... -- global to inner.
177 -- procedure inner (X1 : Global; X2 : Semi_Global);
179 -- procedure in2 is new inner (...); -- 4
182 -- package New_Outer is new Outer (...); -- 2
183 -- procedure New_Inner is new New_Outer.Inner (...); -- 3
185 -- The semantic analysis of Outer captures all occurrences of Global.
186 -- The semantic analysis of Inner (at 1) captures both occurrences of
187 -- Global and Semi_Global.
189 -- At point 2 (instantiation of Outer), we also produce a generic copy
190 -- of Inner, even though Inner is, at that point, not being instantiated.
191 -- (This is just part of the semantic analysis of New_Outer).
193 -- Critically, references to Global within Inner must be preserved, while
194 -- references to Semi_Global should not be preserved, because they must now
195 -- resolve to an entity within New_Outer. To distinguish between these, we
196 -- use a global variable, Current_Instantiated_Parent, which is set when
197 -- performing a generic copy during instantiation (at 2). This variable is
198 -- used when performing a generic copy that is not an instantiation, but
199 -- that is nested within one, as the occurrence of 1 within 2. The analysis
200 -- of a nested generic only preserves references that are global to the
201 -- enclosing Current_Instantiated_Parent. We use the Scope_Depth value to
202 -- determine whether a reference is external to the given parent.
204 -- The instantiation at point 3 requires no special treatment. The method
205 -- works as well for further nestings of generic units, but of course the
206 -- variable Current_Instantiated_Parent must be stacked because nested
207 -- instantiations can occur, e.g. the occurrence of 4 within 2.
209 -- The instantiation of package and subprogram bodies is handled in a
210 -- similar manner, except that it is delayed until after semantic
211 -- analysis is complete. In this fashion complex cross-dependencies
212 -- between several package declarations and bodies containing generics
213 -- can be compiled which otherwise would diagnose spurious circularities.
215 -- For example, it is possible to compile two packages A and B that
216 -- have the following structure:
218 -- package A is package B is
219 -- generic ... generic ...
220 -- package G_A is package G_B is
223 -- package body A is package body B is
224 -- package N_B is new G_B (..) package N_A is new G_A (..)
226 -- The table Pending_Instantiations in package Inline is used to keep
227 -- track of body instantiations that are delayed in this manner. Inline
228 -- handles the actual calls to do the body instantiations. This activity
229 -- is part of Inline, since the processing occurs at the same point, and
230 -- for essentially the same reason, as the handling of inlined routines.
232 ----------------------------------------------
233 -- Detection of Instantiation Circularities --
234 ----------------------------------------------
236 -- If we have a chain of instantiations that is circular, this is static
237 -- error which must be detected at compile time. The detection of these
238 -- circularities is carried out at the point that we insert a generic
239 -- instance spec or body. If there is a circularity, then the analysis of
240 -- the offending spec or body will eventually result in trying to load the
241 -- same unit again, and we detect this problem as we analyze the package
242 -- instantiation for the second time.
244 -- At least in some cases after we have detected the circularity, we get
245 -- into trouble if we try to keep going. The following flag is set if a
246 -- circularity is detected, and used to abandon compilation after the
247 -- messages have been posted.
249 Circularity_Detected : Boolean := False;
250 -- It should really be reset upon encountering a new main unit, but in
251 -- practice we do not use multiple main units so this is not critical.
253 -----------------------------------------
254 -- Implementation of Generic Contracts --
255 -----------------------------------------
257 -- A "contract" is a collection of aspects and pragmas that either verify a
258 -- property of a construct at runtime or classify the data flow to and from
259 -- the construct in some fashion.
261 -- Generic packages, subprograms and their respective bodies may be subject
262 -- to the following contract-related aspects or pragmas collectively known
265 -- package subprogram [body]
266 -- Abstract_State Always_Terminates
267 -- Initial_Condition Contract_Cases
268 -- Initializes Depends
270 -- Extensions_Visible
273 -- Refined_State Post_Class
281 -- Subprogram_Variant
284 -- Most package contract annotations utilize forward references to classify
285 -- data declared within the package [body]. Subprogram annotations then use
286 -- the classifications to further refine them. These inter dependencies are
287 -- problematic with respect to the implementation of generics because their
288 -- analysis, capture of global references and instantiation does not mesh
289 -- well with the existing mechanism.
291 -- 1) Analysis of generic contracts is carried out the same way non-generic
292 -- contracts are analyzed:
294 -- 1.1) General rule - a contract is analyzed after all related aspects
295 -- and pragmas are analyzed. This is done by routines
297 -- Analyze_Package_Body_Contract
298 -- Analyze_Package_Contract
299 -- Analyze_Subprogram_Body_Contract
300 -- Analyze_Subprogram_Contract
302 -- 1.2) Compilation unit - the contract is analyzed after Pragmas_After
305 -- 1.3) Compilation unit body - the contract is analyzed at the end of
306 -- the body declaration list.
308 -- 1.4) Package - the contract is analyzed at the end of the private or
309 -- visible declarations, prior to analyzing the contracts of any nested
310 -- packages or subprograms.
312 -- 1.5) Package body - the contract is analyzed at the end of the body
313 -- declaration list, prior to analyzing the contracts of any nested
314 -- packages or subprograms.
316 -- 1.6) Subprogram - if the subprogram is declared inside a block, a
317 -- package or a subprogram, then its contract is analyzed at the end of
318 -- the enclosing declarations, otherwise the subprogram is a compilation
321 -- 1.7) Subprogram body - if the subprogram body is declared inside a
322 -- block, a package body or a subprogram body, then its contract is
323 -- analyzed at the end of the enclosing declarations, otherwise the
324 -- subprogram is a compilation unit 1.3).
326 -- 2) Capture of global references within contracts is done after capturing
327 -- global references within the generic template. There are two reasons for
328 -- this delay - pragma annotations are not part of the generic template in
329 -- the case of a generic subprogram declaration, and analysis of contracts
332 -- Contract-related source pragmas within generic templates are prepared
333 -- for delayed capture of global references by routine
335 -- Create_Generic_Contract
337 -- The routine associates these pragmas with the contract of the template.
338 -- In the case of a generic subprogram declaration, the routine creates
339 -- generic templates for the pragmas declared after the subprogram because
340 -- they are not part of the template.
342 -- generic -- template starts
343 -- procedure Gen_Proc (Input : Integer); -- template ends
344 -- pragma Precondition (Input > 0); -- requires own template
346 -- 2.1) The capture of global references with aspect specifications and
347 -- source pragmas that apply to a generic unit must be suppressed when
348 -- the generic template is being processed because the contracts have not
349 -- been analyzed yet. Any attempts to capture global references at that
350 -- point will destroy the Associated_Node linkages and leave the template
351 -- undecorated. This delay is controlled by routine
353 -- Requires_Delayed_Save
355 -- 2.2) The real capture of global references within a contract is done
356 -- after the contract has been analyzed, by routine
358 -- Save_Global_References_In_Contract
360 -- 3) The instantiation of a generic contract occurs as part of the
361 -- instantiation of the contract owner. Generic subprogram declarations
362 -- require additional processing when the contract is specified by pragmas
363 -- because the pragmas are not part of the generic template. This is done
366 -- Instantiate_Subprogram_Contract
368 --------------------------------------------------
369 -- Formal packages and partial parameterization --
370 --------------------------------------------------
372 -- When compiling a generic, a formal package is a local instantiation. If
373 -- declared with a box, its generic formals are visible in the enclosing
374 -- generic. If declared with a partial list of actuals, those actuals that
375 -- are defaulted (covered by an Others clause, or given an explicit box
376 -- initialization) are also visible in the enclosing generic, while those
377 -- that have a corresponding actual are not.
379 -- In our source model of instantiation, the same visibility must be
380 -- present in the spec and body of an instance: the names of the formals
381 -- that are defaulted must be made visible within the instance, and made
382 -- invisible (hidden) after the instantiation is complete, so that they
383 -- are not accessible outside of the instance.
385 -- In a generic, a formal package is treated like a special instantiation.
386 -- Our Ada 95 compiler handled formals with and without box in different
387 -- ways. With partial parameterization, we use a single model for both.
388 -- We create a package declaration that consists of the specification of
389 -- the generic package, and a set of declarations that map the actuals
390 -- into local renamings, just as we do for bona fide instantiations. For
391 -- defaulted parameters and formals with a box, we copy directly the
392 -- declarations of the formals into this local package. The result is a
393 -- package whose visible declarations may include generic formals. This
394 -- package is only used for type checking and visibility analysis, and
395 -- never reaches the back end, so it can freely violate the placement
396 -- rules for generic formal declarations.
398 -- The list of declarations (renamings and copies of formals) is built
399 -- by Analyze_Associations, just as for regular instantiations.
401 -- At the point of instantiation, conformance checking must be applied only
402 -- to those parameters that were specified in the formals. We perform this
403 -- checking by creating another internal instantiation, this one including
404 -- only the renamings and the formals (the rest of the package spec is not
405 -- relevant to conformance checking). We can then traverse two lists: the
406 -- list of actuals in the instance that corresponds to the formal package,
407 -- and the list of actuals produced for this bogus instantiation. We apply
408 -- the conformance rules to those actuals that are not defaulted, i.e.
409 -- which still appear as generic formals.
411 -- When we compile an instance body we must make the right parameters
412 -- visible again. The predicate Is_Generic_Formal indicates which of the
413 -- formals should have its Is_Hidden flag reset.
415 -----------------------
416 -- Local subprograms --
417 -----------------------
419 procedure Abandon_Instantiation (N : Node_Id);
420 pragma No_Return (Abandon_Instantiation);
421 -- Posts an error message "instantiation abandoned" at the indicated node
422 -- and then raises the exception Instantiation_Error to do it.
424 procedure Analyze_Formal_Array_Type
425 (T : in out Entity_Id;
427 -- A formal array type is treated like an array type declaration, and
428 -- invokes Array_Type_Declaration (sem_ch3) whose first parameter is
429 -- in-out, because in the case of an anonymous type the entity is
430 -- actually created in the procedure.
432 -- The following procedures treat other kinds of formal parameters
434 procedure Analyze_Formal_Derived_Interface_Type
439 procedure Analyze_Formal_Derived_Type
444 procedure Analyze_Formal_Interface_Type
449 -- The following subprograms create abbreviated declarations for formal
450 -- scalar types. We introduce an anonymous base of the proper class for
451 -- each of them, and define the formals as constrained first subtypes of
452 -- their bases. The bounds are expressions that are non-static in the
455 procedure Analyze_Formal_Decimal_Fixed_Point_Type
456 (T : Entity_Id; Def : Node_Id);
457 procedure Analyze_Formal_Discrete_Type (T : Entity_Id; Def : Node_Id);
458 procedure Analyze_Formal_Floating_Type (T : Entity_Id; Def : Node_Id);
459 procedure Analyze_Formal_Signed_Integer_Type (T : Entity_Id; Def : Node_Id);
460 procedure Analyze_Formal_Modular_Type (T : Entity_Id; Def : Node_Id);
461 procedure Analyze_Formal_Ordinary_Fixed_Point_Type
462 (T : Entity_Id; Def : Node_Id);
464 procedure Analyze_Formal_Private_Type
468 -- Creates a new private type, which does not require completion
470 procedure Analyze_Formal_Incomplete_Type (T : Entity_Id; Def : Node_Id);
471 -- Ada 2012: Creates a new incomplete type whose actual does not freeze
473 procedure Analyze_Generic_Formal_Part (N : Node_Id);
474 -- Analyze generic formal part
476 procedure Analyze_Generic_Access_Type (T : Entity_Id; Def : Node_Id);
477 -- Create a new access type with the given designated type
479 function Analyze_Associations
482 F_Copy : List_Id) return List_Id;
483 -- At instantiation time, build the list of associations between formals
484 -- and actuals. Each association becomes a renaming declaration for the
485 -- formal entity. F_Copy is the analyzed list of formals in the generic
486 -- copy. It is used to apply legality checks to the actuals. I_Node is the
487 -- instantiation node.
489 procedure Analyze_Subprogram_Instantiation
493 procedure Build_Instance_Compilation_Unit_Nodes
497 -- This procedure is used in the case where the generic instance of a
498 -- subprogram body or package body is a library unit. In this case, the
499 -- original library unit node for the generic instantiation must be
500 -- replaced by the resulting generic body, and a link made to a new
501 -- compilation unit node for the generic declaration. The argument N is
502 -- the original generic instantiation. Act_Body and Act_Decl are the body
503 -- and declaration of the instance (either package body and declaration
504 -- nodes or subprogram body and declaration nodes depending on the case).
505 -- On return, the node N has been rewritten with the actual body.
507 function Build_Subprogram_Decl_Wrapper
508 (Formal_Subp : Entity_Id) return Node_Id;
509 -- Ada 2022 allows formal subprograms to carry pre/postconditions.
510 -- At the point of instantiation these contracts apply to uses of
511 -- the actual subprogram. This is implemented by creating wrapper
512 -- subprograms instead of the renamings previously used to link
513 -- formal subprograms and the corresponding actuals. If the actual
514 -- is not an entity (e.g. an attribute reference) a renaming is
515 -- created to handle the expansion of the attribute.
517 function Build_Subprogram_Body_Wrapper
518 (Formal_Subp : Entity_Id;
519 Actual_Name : Node_Id) return Node_Id;
520 -- The body of the wrapper is a call to the actual, with the generated
521 -- pre/postconditon checks added.
523 procedure Build_Subprogram_Wrappers
524 (Match, Analyzed_Formal : Node_Id; Renamings : List_Id);
525 -- Ada 2022: AI12-0272 introduces pre/postconditions for formal
526 -- subprograms. The implementation of making the formal into a renaming
527 -- of the actual does not work, given that subprogram renaming cannot
528 -- carry aspect specifications. Instead we must create subprogram
529 -- wrappers whose body is a call to the actual, and whose declaration
530 -- carries the aspects of the formal.
531 -- The wrapper declaration and body are appended to Renamings.
532 -- ???But renaming declarations CAN have aspects specs,
533 -- and that was true from the start (see AI05-0183-1).
535 procedure Check_Abbreviated_Instance
537 Parent_Installed : in out Boolean);
538 -- If the name of the generic unit in an abbreviated instantiation is an
539 -- expanded name, then the prefix may be an instance and the selector may
540 -- designate a child unit. If the parent is installed as a result of this
541 -- call, then Parent_Installed is set True, otherwise Parent_Installed is
542 -- unchanged by the call.
544 -- This routine needs to be called for declaration nodes of formal objects,
545 -- types and subprograms to check whether they are the copy, present in the
546 -- visible part of the abbreviated instantiation of formal packages, of the
547 -- declaration node of their corresponding formal parameter in the template
548 -- of the formal package, as specified by RM 12.7(10/2), so as to establish
549 -- the proper context for their analysis.
551 procedure Check_Access_Definition (N : Node_Id);
552 -- Subsidiary routine to null exclusion processing. Perform an assertion
553 -- check on Ada version and the presence of an access definition in N.
555 procedure Check_Formal_Packages (P_Id : Entity_Id);
556 -- Apply the following to all formal packages in generic associations.
557 -- Restore the visibility of the formals of the instance that are not
558 -- defaulted (see RM 12.7 (10)). Remove the anonymous package declaration
559 -- created for formal instances that are not defaulted.
561 procedure Check_Formal_Package_Instance
562 (Formal_Pack : Entity_Id;
563 Actual_Pack : Entity_Id);
564 -- Verify that the actuals of the actual instance match the actuals of
565 -- the template for a formal package that is not declared with a box.
567 procedure Check_Forward_Instantiation (Decl : Node_Id);
568 -- If the generic is a local entity and the corresponding body has not
569 -- been seen yet, flag enclosing packages to indicate that it will be
570 -- elaborated after the generic body. Subprograms declared in the same
571 -- package cannot be inlined by the front end because front-end inlining
572 -- requires a strict linear order of elaboration.
574 function Check_Hidden_Primitives (Renamings : List_Id) return Elist_Id;
575 -- Check if some association between formals and actuals requires to make
576 -- visible primitives of a tagged type, and make those primitives visible.
577 -- Return the list of primitives whose visibility is modified (to restore
578 -- their visibility later through Restore_Hidden_Primitives). If no
579 -- candidate is found then return No_Elist.
581 procedure Check_Hidden_Child_Unit
583 Gen_Unit : Entity_Id;
584 Act_Decl_Id : Entity_Id);
585 -- If the generic unit is an implicit child instance within a parent
586 -- instance, we need to make an explicit test that it is not hidden by
587 -- a child instance of the same name and parent.
589 procedure Check_Generic_Actuals
590 (Instance : Entity_Id;
591 Is_Formal_Box : Boolean);
592 -- Similar to previous one. Check the actuals in the instantiation,
593 -- whose views can change between the point of instantiation and the point
594 -- of instantiation of the body. In addition, mark the generic renamings
595 -- as generic actuals, so that they are not compatible with other actuals.
596 -- Recurse on an actual that is a formal package whose declaration has
599 function Component_Type_For_Private_View (T : Entity_Id) return Entity_Id;
600 -- Return the component type of array type T, with the following addition:
601 -- if this component type itself is an array type which has not been first
602 -- declared as private, then recurse on it. This makes it possible to deal
603 -- with arrays of arrays the same way as multi-dimensional arrays in the
604 -- mechanism handling private views.
606 function Contains_Instance_Of
609 N : Node_Id) return Boolean;
610 -- Inner is instantiated within the generic Outer. Check whether Inner
611 -- directly or indirectly contains an instance of Outer or of one of its
612 -- parents, in the case of a subunit. Each generic unit holds a list of
613 -- the entities instantiated within (at any depth). This procedure
614 -- determines whether the set of such lists contains a cycle, i.e. an
615 -- illegal circular instantiation.
617 function Denotes_Formal_Package
619 On_Exit : Boolean := False;
620 Instance : Entity_Id := Empty) return Boolean;
621 -- Returns True if E is a formal package of an enclosing generic, or
622 -- the actual for such a formal in an enclosing instantiation. If such
623 -- a package is used as a formal in an nested generic, or as an actual
624 -- in a nested instantiation, the visibility of ITS formals should not
625 -- be modified. When called from within Restore_Private_Views, the flag
626 -- On_Exit is true, to indicate that the search for a possible enclosing
627 -- instance should ignore the current one. In that case Instance denotes
628 -- the declaration for which this is an actual. This declaration may be
629 -- an instantiation in the source, or the internal instantiation that
630 -- corresponds to the actual for a formal package.
632 function Earlier (N1, N2 : Node_Id) return Boolean;
633 -- Yields True if N1 and N2 appear in the same compilation unit,
634 -- ignoring subunits, and if N1 is to the left of N2 in a left-to-right
635 -- traversal of the tree for the unit. Used to determine the placement
636 -- of freeze nodes for instance bodies that may depend on other instances.
638 function Find_Actual_Type
640 Gen_Type : Entity_Id) return Entity_Id;
641 -- When validating the actual types of a child instance, check whether
642 -- the formal is a formal type of the parent unit, and retrieve the current
643 -- actual for it. Typ is the entity in the analyzed formal type declaration
644 -- (component or index type of an array type, or designated type of an
645 -- access formal) and Gen_Type is the enclosing analyzed formal array
646 -- or access type. The desired actual may be a formal of a parent, or may
647 -- be declared in a formal package of a parent. In both cases it is a
648 -- generic actual type because it appears within a visible instance.
649 -- Finally, it may be declared in a parent unit without being a formal
650 -- of that unit, in which case it must be retrieved by visibility.
651 -- Ambiguities may still arise if two homonyms are declared in two formal
652 -- packages, and the prefix of the formal type may be needed to resolve
653 -- the ambiguity in the instance ???
655 procedure Freeze_Package_Instance
660 -- If the instantiation happens textually before the body of the generic,
661 -- the instantiation of the body must be analyzed after the generic body,
662 -- and not at the point of instantiation. Such early instantiations can
663 -- happen if the generic and the instance appear in a package declaration
664 -- because the generic body can only appear in the corresponding package
665 -- body. Early instantiations can also appear if generic, instance and
666 -- body are all in the declarative part of a subprogram or entry. Entities
667 -- of packages that are early instantiations are delayed, and their freeze
668 -- node appears after the generic body. This rather complex machinery is
669 -- needed when nested instantiations are present, because the source does
670 -- not carry any indication of where the corresponding instance bodies must
671 -- be installed and frozen.
673 procedure Freeze_Subprogram_Instance
676 Pack_Id : Entity_Id);
677 -- The generic body may appear textually after the instance, including
678 -- in the proper body of a stub, or within a different package instance.
679 -- Given that the instance can only be elaborated after the generic, we
680 -- place freeze nodes for the instance and/or for packages that may enclose
681 -- the instance and the generic, so that the back-end can establish the
682 -- proper order of elaboration.
684 function Get_Associated_Entity (Id : Entity_Id) return Entity_Id;
685 -- Similar to Get_Associated_Node below, but for entities
687 function Get_Associated_Node (N : Node_Id) return Node_Id;
688 -- In order to propagate semantic information back from the analyzed copy
689 -- to the original generic, we maintain links between selected nodes in the
690 -- generic and their corresponding copies. At the end of generic analysis,
691 -- the routine Save_Global_References traverses the generic tree, examines
692 -- the semantic information, and preserves the links to those nodes that
693 -- contain global information. At instantiation, the information from the
694 -- associated node is placed on the new copy, so that name resolution is
697 -- Three kinds of source nodes have associated nodes:
699 -- a) those that can reference (denote) entities, that is identifiers,
700 -- character literals, expanded_names, operator symbols, operators,
701 -- and attribute reference nodes. These nodes have an Entity field
702 -- and are the set of nodes that are in N_Has_Entity.
704 -- b) aggregates (N_Aggregate and N_Extension_Aggregate)
706 -- c) selected components (N_Selected_Component)
708 -- For the first class, the associated node preserves the entity if it is
709 -- global. If the generic contains nested instantiations, the associated
710 -- node itself has been recopied, and a chain of them must be followed.
712 -- For aggregates, the associated node allows retrieval of the type, which
713 -- may otherwise not appear in the generic. The view of this type may be
714 -- different between generic and instantiation, and the full view can be
715 -- installed before the instantiation is analyzed. For aggregates of type
716 -- extensions, the same view exchange may have to be performed for some of
717 -- the ancestor types, if their view is private at the point of
720 -- Nodes that are selected components in the parse tree may be rewritten
721 -- as expanded names after resolution, and must be treated as potential
722 -- entity holders, which is why they also have an Associated_Node.
724 -- Nodes that do not come from source, such as freeze nodes, do not appear
725 -- in the generic tree, and need not have an associated node.
727 -- The associated node is stored in the Associated_Node field. Note that
728 -- this field overlaps Entity, which is fine, because the whole point is
729 -- that we don't need or want the normal Entity field in this situation.
731 function Has_Been_Exchanged (E : Entity_Id) return Boolean;
732 -- Traverse the Exchanged_Views list to see if a type was private
733 -- and has already been flipped during this phase of instantiation.
735 function Has_Contracts (Decl : Node_Id) return Boolean;
736 -- Determine whether a formal subprogram has a Pre- or Postcondition,
737 -- in which case a subprogram wrapper has to be built for the actual.
739 function Has_Fully_Defined_Profile (Subp : Entity_Id) return Boolean;
740 -- Determine whether the parameter types and the return type of Subp
741 -- are fully defined at the point of instantiation.
743 function Has_Null_Default (N : Node_Id) return Boolean is
744 (Nkind (N) in N_Formal_Subprogram_Declaration
745 and then Nkind (Specification (N)) = N_Procedure_Specification
746 and then Null_Present (Specification (N)));
747 -- True if N is the declaration of a formal procedure with "is null"
750 procedure Hide_Current_Scope;
751 -- When instantiating a generic child unit, the parent context must be
752 -- present, but the instance and all entities that may be generated
753 -- must be inserted in the current scope. We leave the current scope
754 -- on the stack, but make its entities invisible to avoid visibility
755 -- problems. This is reversed at the end of the instantiation. This is
756 -- not done for the instantiation of the bodies, which only require the
757 -- instances of the generic parents to be in scope.
759 function In_Main_Context (E : Entity_Id) return Boolean;
760 -- Check whether an instantiation is in the context of the main unit.
761 -- Used to determine whether its body should be elaborated to allow
762 -- front-end inlining.
764 procedure Inherit_Context (Gen_Decl : Node_Id; Inst : Node_Id);
765 -- Add the context clause of the unit containing a generic unit to a
766 -- compilation unit that is, or contains, an instantiation.
769 -- Establish environment for subsequent instantiation. Separated from
770 -- Save_Env because data-structures for visibility handling must be
771 -- initialized before call to Check_Generic_Child_Unit.
773 procedure Inline_Instance_Body
775 Gen_Unit : Entity_Id;
777 -- If front-end inlining is requested, instantiate the package body,
778 -- and preserve the visibility of its compilation unit, to insure
779 -- that successive instantiations succeed.
781 procedure Insert_Freeze_Node_For_Instance
784 -- N denotes a package or a subprogram instantiation and F_Node is the
785 -- associated freeze node. Insert the freeze node before the first source
786 -- body which follows immediately after N. If no such body is found, the
787 -- freeze node is inserted at the end of the declarative region which
788 -- contains N, unless the instantiation is done in a package spec that is
789 -- not at library level, in which case it is inserted at the outer level.
790 -- This can also be invoked to insert the freeze node of a package that
791 -- encloses an instantiation, in which case N may denote an arbitrary node.
793 procedure Install_Formal_Packages (Par : Entity_Id);
794 -- Install the visible part of any formal of the parent that is a formal
795 -- package. Note that for the case of a formal package with a box, this
796 -- includes the formal part of the formal package (12.7(10/2)).
798 procedure Install_Hidden_Primitives
799 (Prims_List : in out Elist_Id;
802 -- Remove suffix 'P' from hidden primitives of Act_T to match the
803 -- visibility of primitives of Gen_T. The list of primitives to which
804 -- the suffix is removed is added to Prims_List to restore them later.
806 procedure Install_Parent (P : Entity_Id; In_Body : Boolean := False);
807 -- When compiling an instance of a child unit the parent (which is
808 -- itself an instance) is an enclosing scope that must be made
809 -- immediately visible. This procedure is also used to install the non-
810 -- generic parent of a generic child unit when compiling its body, so
811 -- that full views of types in the parent are made visible.
813 -- The functions Instantiate_... perform various legality checks and build
814 -- the declarations for instantiated generic parameters. In all of these
815 -- Formal is the entity in the generic unit, Actual is the entity or
816 -- expression in the generic associations, and Analyzed_Formal is the
817 -- formal in the generic copy, which contains the semantic information to
818 -- be used to validate the actual.
820 function Instantiate_Object
823 Analyzed_Formal : Node_Id) return List_Id;
825 function Instantiate_Type
828 Analyzed_Formal : Node_Id;
829 Actual_Decls : List_Id) return List_Id;
830 -- Actual_Decls is the list of renamings being built; this is used for
831 -- formal derived types, to determine whether the parent type is another
832 -- formal derived type in the same generic unit.
833 -- Note that the call site appends the result of this function onto
834 -- the same list it is passing to Actual_Decls.
836 function Instantiate_Formal_Subprogram
839 Analyzed_Formal : Node_Id) return Node_Id;
841 function Instantiate_Formal_Package
844 Analyzed_Formal : Node_Id) return List_Id;
845 -- If the formal package is declared with a box, special visibility rules
846 -- apply to its formals: they are in the visible part of the package. This
847 -- is true in the declarative region of the formal package, that is to say
848 -- in the enclosing generic or instantiation. For an instantiation, the
849 -- parameters of the formal package are made visible in an explicit step.
850 -- Furthermore, if the actual has a visible USE clause, these formals must
851 -- be made potentially use-visible as well. On exit from the enclosing
852 -- instantiation, the reverse must be done.
854 -- For a formal package declared without a box, there are conformance rules
855 -- that apply to the actuals in the generic declaration and the actuals of
856 -- the actual package in the enclosing instantiation. The simplest way to
857 -- apply these rules is to repeat the instantiation of the formal package
858 -- in the context of the enclosing instance, and compare the generic
859 -- associations of this instantiation with those of the actual package.
860 -- This internal instantiation only needs to contain the renamings of the
861 -- formals: the visible and private declarations themselves need not be
864 -- In Ada 2005, the formal package may be only partially parameterized.
865 -- In that case the visibility step must make visible those actuals whose
866 -- corresponding formals were given with a box. A final complication
867 -- involves inherited operations from formal derived types, which must
868 -- be visible if the type is.
870 function Is_In_Main_Unit (N : Node_Id) return Boolean;
871 -- Test if given node is in the main unit
873 procedure Load_Parent_Of_Generic
876 Body_Optional : Boolean := False);
877 -- If the generic appears in a separate non-generic library unit, load the
878 -- corresponding body to retrieve the body of the generic. N is the node
879 -- for the generic instantiation, Spec is the generic package declaration.
881 -- Body_Optional is a flag that indicates that the body is being loaded to
882 -- ensure that temporaries are generated consistently when there are other
883 -- instances in the current declarative part that precede the one being
884 -- loaded. In that case a missing body is acceptable.
886 procedure Map_Formal_Package_Entities (Form : Entity_Id; Act : Entity_Id);
887 -- Within the generic part, entities in the formal package are
888 -- visible. To validate subsequent type declarations, indicate
889 -- the correspondence between the entities in the analyzed formal,
890 -- and the entities in the actual package. There are three packages
891 -- involved in the instantiation of a formal package: the parent
892 -- generic P1 which appears in the generic declaration, the fake
893 -- instantiation P2 which appears in the analyzed generic, and whose
894 -- visible entities may be used in subsequent formals, and the actual
895 -- P3 in the instance. To validate subsequent formals, me indicate
896 -- that the entities in P2 are mapped into those of P3. The mapping of
897 -- entities has to be done recursively for nested packages.
899 procedure Move_Freeze_Nodes
903 -- Freeze nodes can be generated in the analysis of a generic unit, but
904 -- will not be seen by the back-end. It is necessary to move those nodes
905 -- to the enclosing scope if they freeze an outer entity. We place them
906 -- at the end of the enclosing generic package, which is semantically
909 procedure Preanalyze_Actuals (N : Node_Id; Inst : Entity_Id := Empty);
910 -- Analyze actuals to perform name resolution. Full resolution is done
911 -- later, when the expected types are known, but names have to be captured
912 -- before installing parents of generics, that are not visible for the
913 -- actuals themselves.
915 -- If Inst is present, it is the entity of the package instance. This
916 -- entity is marked as having a limited_view actual when some actual is
917 -- a limited view. This is used to place the instance body properly.
919 procedure Provide_Completing_Bodies (N : Node_Id);
920 -- Generate completing bodies for all subprograms found within package or
921 -- subprogram declaration N.
923 procedure Remove_Parent (In_Body : Boolean := False);
924 -- Reverse effect after instantiation of child is complete
926 function Renames_Standard_Subprogram (Subp : Entity_Id) return Boolean;
927 -- Determine whether Subp renames one of the subprograms defined in the
928 -- generated package Standard.
930 function Requires_Conformance_Checking (N : Node_Id) return Boolean;
931 -- Determine whether the formal package declaration N requires conformance
932 -- checking with actuals in instantiations.
934 procedure Restore_Hidden_Primitives (Prims_List : in out Elist_Id);
935 -- Restore suffix 'P' to primitives of Prims_List and leave Prims_List
938 procedure Set_Instance_Env
939 (Gen_Unit : Entity_Id;
940 Act_Unit : Entity_Id);
941 -- Save current instance on saved environment, to be used to determine
942 -- the global status of entities in nested instances. Part of Save_Env.
943 -- called after verifying that the generic unit is legal for the instance,
944 -- The procedure also examines whether the generic unit is a predefined
945 -- unit, in order to set configuration switches accordingly. As a result
946 -- the procedure must be called after analyzing and freezing the actuals.
948 procedure Set_Instance_Of (A : Entity_Id; B : Entity_Id);
949 -- Associate analyzed generic parameter with corresponding instance. Used
950 -- for semantic checks at instantiation time.
952 function True_Parent (N : Node_Id) return Node_Id;
953 -- For a subunit, return parent of corresponding stub, else return
956 procedure Valid_Default_Attribute (Nam : Entity_Id; Def : Node_Id);
957 -- Verify that an attribute that appears as the default for a formal
958 -- subprogram is a function or procedure with the correct profile.
960 procedure Validate_Formal_Type_Default (Decl : Node_Id);
961 -- Ada_2022 AI12-205: if a default subtype_mark is present, verify
962 -- that it is the name of a type in the same class as the formal.
963 -- The treatment parallels what is done in Instantiate_Type but differs
964 -- in a few ways so that this machinery cannot be reused as is: on one
965 -- hand there are no visibility issues for a default, because it is
966 -- analyzed in the same context as the formal type definition; on the
967 -- other hand the check needs to take into acount the use of a previous
968 -- formal type in the current formal type definition (see details in
971 -------------------------------------------
972 -- Data Structures for Generic Renamings --
973 -------------------------------------------
975 -- The map Generic_Renamings associates generic entities with their
976 -- corresponding actuals. Currently used to validate type instances. It
977 -- will eventually be used for all generic parameters to eliminate the
978 -- need for overload resolution in the instance.
980 type Assoc_Ptr is new Int;
982 Assoc_Null : constant Assoc_Ptr := -1;
987 Next_In_HTable : Assoc_Ptr;
990 package Generic_Renamings is new Table.Table
991 (Table_Component_Type => Assoc,
992 Table_Index_Type => Assoc_Ptr,
993 Table_Low_Bound => 0,
995 Table_Increment => 100,
996 Table_Name => "Generic_Renamings");
998 -- Variable to hold enclosing instantiation. When the environment is
999 -- saved for a subprogram inlining, the corresponding Act_Id is empty.
1001 Current_Instantiated_Parent : Assoc := (Empty, Empty, Assoc_Null);
1003 -- Hash table for associations
1005 HTable_Size : constant := 37;
1006 type HTable_Range is range 0 .. HTable_Size - 1;
1008 procedure Set_Next_Assoc (E : Assoc_Ptr; Next : Assoc_Ptr);
1009 function Next_Assoc (E : Assoc_Ptr) return Assoc_Ptr;
1010 function Get_Gen_Id (E : Assoc_Ptr) return Entity_Id;
1011 function Hash (F : Entity_Id) return HTable_Range;
1013 package Generic_Renamings_HTable is new GNAT.HTable.Static_HTable (
1014 Header_Num => HTable_Range,
1016 Elmt_Ptr => Assoc_Ptr,
1017 Null_Ptr => Assoc_Null,
1018 Set_Next => Set_Next_Assoc,
1021 Get_Key => Get_Gen_Id,
1025 Exchanged_Views : Elist_Id;
1026 -- This list holds the private views that have been exchanged during
1027 -- instantiation to restore the visibility of the generic declaration.
1028 -- (see comments above). After instantiation, the current visibility is
1029 -- reestablished by means of a traversal of this list.
1031 Hidden_Entities : Elist_Id;
1032 -- This list holds the entities of the current scope that are removed
1033 -- from immediate visibility when instantiating a child unit. Their
1034 -- visibility is restored in Remove_Parent.
1036 -- Because instantiations can be recursive, the following must be saved
1037 -- on entry and restored on exit from an instantiation (spec or body).
1038 -- This is done by the two procedures Save_Env and Restore_Env. For
1039 -- package and subprogram instantiations (but not for the body instances)
1040 -- the action of Save_Env is done in two steps: Init_Env is called before
1041 -- Check_Generic_Child_Unit, because setting the parent instances requires
1042 -- that the visibility data structures be properly initialized. Once the
1043 -- generic is unit is validated, Set_Instance_Env completes Save_Env.
1045 Parent_Unit_Visible : Boolean := False;
1046 -- Parent_Unit_Visible is used when the generic is a child unit, and
1047 -- indicates whether the ultimate parent of the generic is visible in the
1048 -- instantiation environment. It is used to reset the visibility of the
1049 -- parent at the end of the instantiation (see Remove_Parent).
1051 Instance_Parent_Unit : Entity_Id := Empty;
1052 -- This records the ultimate parent unit of an instance of a generic
1053 -- child unit and is used in conjunction with Parent_Unit_Visible to
1054 -- indicate the unit to which the Parent_Unit_Visible flag corresponds.
1056 type Instance_Env is record
1057 Instantiated_Parent : Assoc;
1058 Exchanged_Views : Elist_Id;
1059 Hidden_Entities : Elist_Id;
1060 Current_Sem_Unit : Unit_Number_Type;
1061 Parent_Unit_Visible : Boolean := False;
1062 Instance_Parent_Unit : Entity_Id := Empty;
1063 Switches : Config_Switches_Type;
1066 package Instance_Envs is new Table.Table (
1067 Table_Component_Type => Instance_Env,
1068 Table_Index_Type => Int,
1069 Table_Low_Bound => 0,
1070 Table_Initial => 32,
1071 Table_Increment => 100,
1072 Table_Name => "Instance_Envs");
1074 procedure Restore_Private_Views
1075 (Pack_Id : Entity_Id;
1076 Is_Package : Boolean := True);
1077 -- Restore the private views of external types, and unmark the generic
1078 -- renamings of actuals, so that they become compatible subtypes again.
1079 -- For subprograms, Pack_Id is the package constructed to hold the
1082 procedure Switch_View (T : Entity_Id);
1083 -- Switch the partial and full views of a type and its private
1084 -- dependents (i.e. its subtypes and derived types).
1086 ------------------------------------
1087 -- Structures for Error Reporting --
1088 ------------------------------------
1090 Instantiation_Node : Node_Id;
1091 -- Used by subprograms that validate instantiation of formal parameters
1092 -- where there might be no actual on which to place the error message.
1093 -- Also used to locate the instantiation node for generic subunits.
1095 Instantiation_Error : exception;
1096 -- When there is a semantic error in the generic parameter matching,
1097 -- there is no point in continuing the instantiation, because the
1098 -- number of cascaded errors is unpredictable. This exception aborts
1099 -- the instantiation process altogether.
1101 S_Adjustment : Sloc_Adjustment;
1102 -- Offset created for each node in an instantiation, in order to keep
1103 -- track of the source position of the instantiation in each of its nodes.
1104 -- A subsequent semantic error or warning on a construct of the instance
1105 -- points to both places: the original generic node, and the point of
1106 -- instantiation. See Sinput and Sinput.L for additional details.
1108 ------------------------------------------------------------
1109 -- Data structure for keeping track when inside a Generic --
1110 ------------------------------------------------------------
1112 -- The following table is used to save values of the Inside_A_Generic
1113 -- flag (see spec of Sem) when they are saved by Start_Generic.
1115 package Generic_Flags is new Table.Table (
1116 Table_Component_Type => Boolean,
1117 Table_Index_Type => Int,
1118 Table_Low_Bound => 0,
1119 Table_Initial => 32,
1120 Table_Increment => 200,
1121 Table_Name => "Generic_Flags");
1127 package Associations is
1132 -- Used when the "formal" is a use clause; there is no corresponding
1135 -- Used for "is <>" as a subprogram default
1137 -- Used for explicit "name => <>" and "others => <>" in formal
1140 -- Name or expression or ....
1141 -- Used for an explicit_generic_actual_parameter, and also for the
1142 -- default_expression of an in-mode formal, the default_subtype_mark
1143 -- of a formal type, and the default_name of a formal subprogram.
1145 -- Used for "is null" as a subprogram default.
1147 -- Used for "is (expression)" as a subprogram default,
1148 -- which is a language extension (and is different from "is name"
1149 -- without parentheses).
1151 -- Used for the dummy associations that are created in
1152 -- Save_Global_Defaults. These have Explicit_Generic_Actual_Parameter
1153 -- = Empty and Box_Present = False
1155 -- ???We wouldn't need this enumeration type if we created new node
1156 -- kinds for N_Box_Subp_Default, N_Box_Actual, N_Null_Default, and
1157 -- N_Exp_Func_Default.
1159 type Generic_Actual_Rec (Kind : Actual_Kind := None) is record
1160 -- Representation of one generic actual parameter
1162 when None | None_Use_Clause | Box_Subp_Default | Box_Actual |
1163 Null_Default | Dummy_Assoc =>
1165 when Name_Exp | Exp_Func_Default =>
1170 type Actual_Origin_Enum is
1171 (None, From_Explicit_Actual, From_Default, From_Inference,
1173 -- Indication of where the Actual came from -- explicitly in the
1174 -- instantiation, inferred from some other type, or defaulted.
1176 type Inference_Reason is
1177 -- Reason an actual type corresponding to a formal type was (or could
1178 -- be) inferred from the actual type corresponding to another formal
1180 (Designated_Type, -- designated type from formal access
1181 Index_Type, -- index type from formal array
1182 Component_Type, -- component type from formal array
1183 Discriminant_Type); -- discriminant type from formal discriminated
1185 function Image (Reason : Inference_Reason) return String is
1187 when Designated_Type => "designated type",
1188 when Index_Type => "index type",
1189 when Component_Type => "component type",
1190 when Discriminant_Type => "discriminant type");
1192 type Assoc_Index is new Pos;
1193 subtype Assoc_Count is Assoc_Index'Base range 0 .. Assoc_Index'Last;
1195 type Assoc_Rec is record
1196 -- Association between a single formal/actual pair. But we store both
1197 -- the unanalyzed and analyzed formal.
1199 Un_Formal, An_Formal : Node_Id; -- unanalyzed and analyzed formals
1200 -- An_Formal is the node in the generic copy that corresponds to
1201 -- Un_Formal. The semantic information on this node is used to
1202 -- perform legality checks on the actuals. Because semantic analysis
1203 -- can introduce some anonymous entities or modify the declaration
1204 -- node itself, the correspondence between the two lists is not
1205 -- one-one. In addition to anonymous types, a formal "=" will
1206 -- introduce an implicit equal and opposite "/=".
1208 Explicit_Assoc : Opt_N_Generic_Association_Id;
1209 -- Explicit association, if any, from the source or generated.
1211 Actual : Generic_Actual_Rec;
1212 -- Generic actual parameter corresponding to Un_Formal/An_Formal,
1213 -- possibly from defaults or others/boxes.
1215 Actual_Origin : Actual_Origin_Enum;
1216 -- Reason why Actual was set; where it came from
1218 Info_Inferred_Actual : Opt_Type_Kind_Id;
1219 -- An inferred actual is always a type entity, not a box, and not
1220 -- something like T'Base. This is used only for messages and
1221 -- assertions. It contains the type that was, or could have been,
1224 Inferred_From : Assoc_Index;
1225 -- Index of a later Assoc_Rec in the same Gen_Assocs_Rec from which
1226 -- this one was inferred, or could be inferred.
1227 -- Valid only if Info_Inferred_Actual is present.
1229 Reason : Inference_Reason;
1230 -- Reason the type was inferred, or could have been inferred.
1231 -- Valid only if Info_Inferred_Actual is present.
1232 end record; -- Assoc_Rec
1234 type Assoc_Array is array (Assoc_Index range <>) of Assoc_Rec;
1235 -- One element for each formal and (if legal) for each corresponding
1238 type Gen_Assocs_Rec (Num_Assocs : Assoc_Count) is record
1239 -- Representation of formal/actual matching. Num_Assocs
1240 -- is the number of formals and (if legal) the number
1242 Gen_Unit : Entity_Id;
1243 -- the generic unit being instantiated
1244 Others_Present : Boolean;
1245 -- True if "others => <>" (only for formal packages)
1246 Assocs : Assoc_Array (1 .. Num_Assocs);
1247 -- One for each formal/actual pair; defaulted and inferred actuals
1251 function Match_Assocs
1252 (I_Node : Node_Id; Formals : List_Id; F_Copy : List_Id)
1253 return Gen_Assocs_Rec;
1254 -- I_Node is the instantiation node. Formals is the list of unanalyzed
1255 -- formals. F_Copy is the analyzed list of formals in the generic copy.
1256 -- Return a Gen_Assocs_Rec with formals, explicit actuals, and default
1257 -- actuals filled in. Check legality rules related to formal/actual
1260 procedure Note_Potential_Inference
1261 (I_Node : Node_Id; Gen_Assocs : Gen_Assocs_Rec);
1262 -- If -gnatd_I, print "info:" messages about type inference that could
1267 procedure Analyze_One_Association
1268 (I_Node : Node_Id; -- instantiation node
1269 Assoc : Associations.Assoc_Rec;
1270 -- Logical 'in out' parameters:
1271 Result_Renamings : List_Id;
1272 Default_Actuals : List_Id;
1273 Actuals_To_Freeze : Elist_Id);
1274 -- Called by Analyze_Associations for each association. The renamings
1275 -- are appended onto Result_Renamings. Defaulted actuals are appended
1276 -- onto Default_Actuals, and actuals that require freezing are
1277 -- appended onto Actuals_To_Freeze.
1279 procedure Check_Fixed_Point_Warning
1280 (Gen_Assocs : Associations.Gen_Assocs_Rec;
1281 Renamings : List_Id);
1282 -- Warn if any actual is a fixed-point type that has user-defined
1283 -- arithmetic operators, but there is no corresponding formal in the
1284 -- generic, in which case the predefined operators will be used. This
1285 -- merits a warning because of the special semantics of fixed point
1286 -- operators. However, do not warn if the formal is private, because there
1287 -- can be no arithmetic operators in the generic so there no danger of
1294 package body Associations is
1297 with procedure Action (F : Node_Id; Index : Assoc_Index);
1298 procedure Formal_Iter (Formals : List_Id);
1299 -- Iterate through the unanalyzed formals, calling Action for each one.
1300 -- Skip pragmas, but do not skip use clauses.
1302 function Num_Formals (Formals : List_Id) return Assoc_Count;
1303 -- Note: does not include pragmas that occur in the Formals list;
1304 -- it does include use clauses.
1307 with procedure Action (F : Node_Id; Index : Assoc_Index);
1308 procedure An_Formal_Iter (An_Formals : List_Id);
1309 -- Iterate through the analyzed formals, calling Action for each one
1310 -- that corresponds to an unanalyzed formal. This should call Action
1311 -- exactly the same number of times that Formal_Iter calls its Action.
1312 -- Skip pragmas, but do not skip use clauses. Skip extraneous
1313 -- analyzed formals in cases where there are multiple ones
1314 -- corresponding to a particular unanalyzed one.
1316 function Num_An_Formals (F_Copy : List_Id) return Assoc_Count;
1317 -- Number of analyzed formals that correspond directly to unanalyzed
1318 -- formals. There are all sorts of other things in F_Copy, which
1321 procedure Check_Box (I_Node, Actual : Node_Id);
1322 -- Check for errors in "others => <>" and "Name => <>"
1324 function Default (Un_Formal : Node_Id) return Generic_Actual_Rec;
1325 -- Return the default for a given formal, which can be a name,
1326 -- expression, box, etc.
1328 procedure Match_Positional
1329 (Src_Assoc : in out Node_Id; Assoc : in out Assoc_Rec);
1330 -- Called by Match_Assocs to match one positional parameter association.
1331 -- If the current formal (in Assoc) is not a use clause, then there is a
1332 -- match, and we set Assoc.Actual and move Src_Assoc to the next one.
1334 procedure Match_Named
1335 (Src_Assoc : Node_Id; Assoc : in out Assoc_Rec;
1336 Found : in out Boolean);
1337 -- Called by Match_Assocs to match one named parameter association.
1338 -- If the current formal (in Assoc) is not a use clause, and the
1339 -- selector name matches the formal name, then there is a match,
1340 -- and we set Assoc.Actual. We also set the Selector_Name to denote
1341 -- the matched formal, and set Found to True.
1343 procedure Inference_Msg
1344 (Gen_Unit : Entity_Id;
1345 Inferred_To, Inferred_From : Assoc_Rec;
1346 Was_Inferred : Boolean);
1347 -- If Was_Inferred is True, this prints out an "info:" message
1348 -- showing the inference.
1349 -- If Was_Inferred is False, the message says that it could have
1353 (Gen_Assocs : Gen_Assocs_Rec; F : Entity_Id) return Assoc_Index;
1354 -- Return the index of F in Gen_Assocs.Assocs, which must be
1357 procedure Maybe_Infer_One
1358 (Gen_Assocs : in out Gen_Assocs_Rec;
1359 FF, AA : N_Entity_Id; Inferred_From : Assoc_Index;
1360 Reason : Inference_Reason);
1361 -- If it makes sense to infer that formal FF is associated with
1362 -- actual AA, then do so.
1364 procedure Infer_From_Access
1365 (Gen_Assocs : in out Gen_Assocs_Rec;
1366 Index : Assoc_Index;
1368 A_Full : Entity_Id);
1369 -- Try to infer the designated type
1371 procedure Infer_From_Array
1372 (Gen_Assocs : in out Gen_Assocs_Rec;
1373 Index : Assoc_Index;
1375 A_Full : Entity_Id);
1376 -- Try to infer the index and component types
1378 procedure Infer_From_Discriminated
1379 (Gen_Assocs : in out Gen_Assocs_Rec;
1380 Index : Assoc_Index;
1382 A_Full : Entity_Id);
1383 -- Try to infer the types of discriminants
1385 procedure Infer_Actuals (Gen_Assocs : in out Gen_Assocs_Rec);
1386 -- Called by Match_Assocs after processing explicit and defaulted
1387 -- parameters to infer any that are still missing.
1393 -- Formal_Iter is straightforward; An_Formal_Iter is not.
1395 procedure Formal_Iter (Formals : List_Id) is
1396 F : Node_Id := First (Formals);
1397 Index : Assoc_Index := 1;
1399 while Present (F) loop
1401 when N_Formal_Object_Declaration
1402 | N_Formal_Type_Declaration
1403 | N_Formal_Subprogram_Declaration
1404 | N_Formal_Package_Declaration
1405 | N_Use_Package_Clause
1413 raise Program_Error;
1424 function Num_Formals (Formals : List_Id) return Assoc_Count is
1425 Result : Assoc_Count := 0;
1426 procedure Action (Ignore_F : Node_Id; Ignore : Assoc_Index);
1427 procedure Action (Ignore_F : Node_Id; Ignore : Assoc_Index) is
1429 Result := Result + 1;
1431 procedure Iter is new Formal_Iter (Action);
1437 --------------------
1438 -- An_Formal_Iter --
1439 --------------------
1441 procedure An_Formal_Iter (An_Formals : List_Id) is
1442 F : Node_Id := First (An_Formals);
1443 Index : Assoc_Index := 1;
1445 -- The correspondence between unanalyzed and analyzed formals is not
1446 -- one-one; hence this needs to do some fancy footwork to skip some
1447 -- items in the analyzed formals list. In each case where multiple
1448 -- items in An_Formals correspond to a particular unanalyzed formal,
1449 -- we must pick the "main" one.
1451 while Present (F) loop
1453 when N_Use_Package_Clause | N_Use_Type_Clause =>
1457 when N_Formal_Object_Declaration
1458 | N_Formal_Type_Declaration
1459 | N_Formal_Subprogram_Declaration
1460 | N_Package_Declaration
1461 | N_Full_Type_Declaration
1462 | N_Private_Type_Declaration
1463 | N_Private_Extension_Declaration
1465 if Is_Internal_Name (Chars (Defining_Entity (F))) then
1471 -- Skip full type of derived type
1473 if Nkind (F) = N_Full_Type_Declaration
1474 and then Nkind (Type_Definition (F)) =
1475 N_Derived_Type_Definition
1476 and then Present (Next (F))
1477 and then Nkind (Next (F)) = N_Full_Type_Declaration
1478 and then Chars (Defining_Identifier (F)) =
1479 Chars (Defining_Identifier (Next (F)))
1485 when N_Subtype_Declaration =>
1486 if Nkind (Original_Node (F)) in N_Formal_Type_Declaration
1489 (not Is_Internal_Name (Chars (Defining_Entity (F))));
1493 elsif Nkind (Original_Node (F)) in N_Full_Type_Declaration
1497 -- subtype of a formal object
1500 (Nkind (Next (F)) = N_Formal_Object_Declaration);
1506 when N_Formal_Package_Declaration =>
1507 -- If there were no errors, this would have been transformed
1508 -- into an N_Package_Declaration.
1510 Check_Error_Detected;
1511 pragma Assert (Error_Posted (F));
1512 Abandon_Instantiation (Instantiation_Node);
1515 raise Program_Error;
1522 --------------------
1523 -- Num_An_Formals --
1524 --------------------
1526 function Num_An_Formals (F_Copy : List_Id) return Assoc_Count is
1527 Result : Assoc_Count := 0;
1528 procedure Action (Ignore_F : Node_Id; Ignore : Assoc_Index);
1529 procedure Action (Ignore_F : Node_Id; Ignore : Assoc_Index) is
1531 Result := Result + 1;
1533 procedure Iter is new An_Formal_Iter (Action);
1543 procedure Check_Box (I_Node, Actual : Node_Id) is
1545 -- "... => <>" is allowed only in formal packages, not old-fashioned
1548 if Nkind (I_Node) /= N_Formal_Package_Declaration
1549 and then Comes_From_Source (I_Node)
1551 if Actual in N_Others_Choice_Id then
1553 ("OTHERS association not allowed in an instance", Actual);
1554 elsif Box_Present (Actual) then
1556 ("box association not allowed in an instance", Actual);
1560 -- "others => <>" must come last
1562 if Actual in N_Others_Choice_Id
1563 and then Present (Next (Actual))
1566 ("OTHERS must be last association", Actual);
1574 function Default (Un_Formal : Node_Id) return Generic_Actual_Rec is
1576 return Result : Generic_Actual_Rec do
1577 case Nkind (Un_Formal) is
1578 when N_Formal_Object_Declaration =>
1579 if Present (Default_Expression (Un_Formal)) then
1580 Result := (Name_Exp, Default_Expression (Un_Formal));
1582 when N_Formal_Type_Declaration =>
1583 if Present (Default_Subtype_Mark (Un_Formal)) then
1584 Result := (Name_Exp, Default_Subtype_Mark (Un_Formal));
1586 when N_Formal_Subprogram_Declaration =>
1587 if Present (Default_Name (Un_Formal)) then
1588 pragma Assert (Result.Kind = None);
1589 Result := (Name_Exp, Default_Name (Un_Formal));
1592 if Box_Present (Un_Formal) then
1593 pragma Assert (Result.Kind = None);
1594 Result := (Kind => Box_Subp_Default);
1597 if Present (Expression (Un_Formal)) then
1598 pragma Assert (Result.Kind = None);
1599 Result := (Exp_Func_Default, Expression (Un_Formal));
1602 if Has_Null_Default (Un_Formal) then
1603 pragma Assert (Result.Kind = None);
1604 Result := (Kind => Null_Default);
1607 when N_Formal_Package_Declaration => null;
1609 when others => raise Program_Error;
1612 (if Result.Kind in Name_Exp | Exp_Func_Default then
1613 Present (Result.Name_Exp));
1617 ----------------------
1618 -- Match_Positional --
1619 ----------------------
1621 procedure Match_Positional
1622 (Src_Assoc : in out Node_Id; Assoc : in out Assoc_Rec) is
1624 if Nkind (Assoc.Un_Formal) not in
1625 N_Use_Package_Clause | N_Use_Type_Clause
1627 pragma Assert (No (Assoc.Explicit_Assoc));
1628 pragma Assert (Assoc.Actual.Kind = None);
1629 Assoc.Explicit_Assoc := Src_Assoc;
1631 -- A "<>" without "name =>" is illegal syntax
1633 if Box_Present (Src_Assoc) then
1634 Assoc.Actual := (Kind => Box_Actual);
1635 if False then -- ???
1636 -- Disable this for now, because we have various
1637 -- code that needs to be updated.
1639 ("box requires named notation", Src_Assoc);
1644 Explicit_Generic_Actual_Parameter (Src_Assoc));
1645 pragma Assert (Present (Assoc.Actual.Name_Exp));
1647 Assoc.Actual_Origin := From_Explicit_Actual;
1651 end Match_Positional;
1657 procedure Match_Named
1658 (Src_Assoc : Node_Id; Assoc : in out Assoc_Rec;
1659 Found : in out Boolean) is
1661 if Nkind (Assoc.Un_Formal) not in
1662 N_Use_Package_Clause | N_Use_Type_Clause
1663 and then Chars (Selector_Name (Src_Assoc)) =
1664 Chars (Defining_Entity (Assoc.Un_Formal))
1666 if Found then -- second formal with the same name
1667 pragma Assert (Comes_From_Source (Src_Assoc));
1669 ("named association not allowed for " &
1670 "overloaded formal", Src_Assoc);
1671 Abandon_Instantiation (Instantiation_Node);
1674 if Assoc.Actual.Kind /= None then
1675 if Comes_From_Source (Src_Assoc) then
1677 ("duplicate actual for &",
1678 Src_Assoc, Selector_Name (Src_Assoc));
1681 Assoc.Explicit_Assoc := Src_Assoc;
1682 if Box_Present (Src_Assoc) then
1683 Assoc.Actual := (Kind => Box_Actual);
1686 if No (Explicit_Generic_Actual_Parameter (Src_Assoc)) then
1687 Assoc.Actual := (Kind => Dummy_Assoc);
1691 Explicit_Generic_Actual_Parameter (Src_Assoc));
1694 -- Set Entity (etc.) of the selector name:
1697 A_F : constant Entity_Id :=
1698 Defining_Entity (Assoc.An_Formal);
1699 Orig_F : constant Node_Id :=
1700 Original_Node (Assoc.An_Formal);
1701 Sel : constant Node_Id :=
1702 Selector_Name (Assoc.Explicit_Assoc);
1704 Set_Entity (Sel, A_F);
1705 Set_Etype (Sel, Etype (A_F));
1707 if Nkind (Orig_F) = N_Formal_Package_Declaration then
1708 Generate_Reference (Defining_Identifier (Orig_F), Sel);
1709 -- ???Original_Node makes no sense, but we're
1710 -- preserving the old behavior.
1712 Generate_Reference (A_F, Sel);
1717 Assoc.Actual_Origin := From_Explicit_Actual;
1727 function Match_Assocs
1728 (I_Node : Node_Id; Formals : List_Id; F_Copy : List_Id)
1729 return Gen_Assocs_Rec
1731 Src_Assocs : constant List_Id := Generic_Associations (I_Node);
1732 Gen_Unit : constant Entity_Id := Defining_Entity (Parent (F_Copy));
1735 (Num_An_Formals (F_Copy) = Num_Formals (Formals)
1736 or else Serious_Errors_Detected > 0);
1738 return Result : Gen_Assocs_Rec (Num_Assocs => Num_Formals (Formals))
1740 Result.Gen_Unit := Gen_Unit;
1741 Result.Others_Present := False;
1743 -- Loop through the unanalyzed formals:
1746 procedure Set_Formal (F : Node_Id; Index : Assoc_Index);
1747 -- Initialize one Assoc_Rec so the formal is set.
1748 -- Use a dummy assoc for use clauses.
1750 procedure Set_Formal (F : Node_Id; Index : Assoc_Index) is
1751 Assoc : Assoc_Rec renames Result.Assocs (Index);
1753 if Nkind (F) in N_Use_Package_Clause | N_Use_Type_Clause then
1757 Explicit_Assoc => Empty,
1758 Actual => (Kind => None_Use_Clause),
1759 Actual_Origin => None,
1760 Info_Inferred_Actual => Empty,
1761 Inferred_From => <>,
1767 Explicit_Assoc => Empty,
1769 Actual_Origin => None,
1770 Info_Inferred_Actual => Empty,
1771 Inferred_From => <>,
1775 procedure Iter is new Formal_Iter (Set_Formal);
1780 -- Loop through the analyzed copy of the formals:
1783 procedure Set_An_Formal (F : Node_Id; Index : Assoc_Index);
1784 procedure Set_An_Formal (F : Node_Id; Index : Assoc_Index) is
1785 Assoc : Assoc_Rec renames Result.Assocs (Index);
1787 Assoc.An_Formal := F;
1788 if Nkind (F) in N_Use_Package_Clause | N_Use_Type_Clause then
1790 (Nkind (Assoc.Un_Formal) = Nkind (Assoc.An_Formal));
1793 case Nkind (Assoc.Un_Formal) is
1794 when N_Formal_Object_Declaration
1795 | N_Formal_Subprogram_Declaration
1798 (Nkind (Assoc.Un_Formal) =
1799 Nkind (Assoc.An_Formal));
1801 when N_Formal_Type_Declaration =>
1803 (Nkind (Original_Node (Assoc.An_Formal)) =
1804 N_Formal_Type_Declaration);
1806 (Nkind (Assoc.An_Formal) in
1807 N_Formal_Type_Declaration
1808 | N_Full_Type_Declaration
1809 | N_Private_Type_Declaration
1810 | N_Private_Extension_Declaration
1811 | N_Subtype_Declaration);
1813 when N_Formal_Package_Declaration =>
1815 (Nkind (Original_Node (Assoc.An_Formal)) =
1816 N_Formal_Package_Declaration);
1818 (Nkind (Assoc.An_Formal) = N_Package_Declaration);
1820 when others => pragma Assert (False);
1824 (Chars (Defining_Entity (Assoc.Un_Formal)) =
1825 Chars (Defining_Entity (Assoc.An_Formal)));
1829 procedure Iter is new An_Formal_Iter (Set_An_Formal);
1832 (Num_An_Formals (F_Copy) = Result.Assocs'Last
1833 or else Serious_Errors_Detected > 0);
1837 -- Loop through actual source associations:
1840 Src_Assoc : Node_Id := First (Src_Assocs);
1841 -- Generic association from the source
1843 function Positional return Boolean is
1844 (Present (Src_Assoc)
1845 and then Src_Assoc not in N_Others_Choice_Id
1846 and then No (Selector_Name (Src_Assoc)));
1847 -- True if Src_Assoc is position; i.e. not named and not others
1849 -- Loop through positional actuals:
1851 for Index in Result.Assocs'Range loop
1852 exit when not Positional;
1853 Match_Positional (Src_Assoc, Result.Assocs (Index));
1857 Error_Msg_Sloc := Sloc (Gen_Unit);
1859 ("unmatched actual in instantiation of & declared#",
1860 Src_Assoc, Gen_Unit);
1862 -- Loop through named actuals and "others => <>":
1864 while Present (Src_Assoc) loop
1865 Check_Box (I_Node, Src_Assoc);
1866 if Src_Assoc in N_Others_Choice_Id then
1867 Result.Others_Present := True;
1873 ("invalid positional actual after named one",
1876 -- For actual "X => ...", find formal whose name is X.
1877 -- Complain if X has already been specified (could be
1878 -- by a positional association, or by a previous named
1879 -- one). Also complain if there's more than one X.
1880 -- See RM-12.3(9/3) and 12.7(4.1/3).
1881 -- However, this rule does not apply to generated
1882 -- code,because for nested instances, we routinely
1883 -- generate things like:
1884 -- X => ..., X => ...
1885 -- where the first one refers to the first formal X,
1886 -- and the second one refers to the second formal X,
1887 -- and so on. (The X's are formal subprograms in this
1891 Found : Boolean := False;
1893 for Index in Result.Assocs'Range loop
1895 (Src_Assoc, Result.Assocs (Index), Found);
1897 and then not Comes_From_Source (Src_Assoc);
1900 if not Found and then Comes_From_Source (Src_Assoc)
1902 Error_Msg_Sloc := Sloc (Gen_Unit);
1904 ("unmatched actual &",
1905 Src_Assoc, Selector_Name (Src_Assoc));
1907 ("\in instantiation of & declared#",
1908 Src_Assoc, Gen_Unit);
1918 -- Fill in defaults. For each formal F with no associated actual,
1919 -- if there is "others => <>", set the actual to "F => <>".
1920 -- Otherwise, if the formal has a default, set the actual to
1921 -- "F => default". Otherwise leave it Empty.
1922 -- (If Empty, it could be inferred, or it could be an error).
1924 for Index in Result.Assocs'Range loop
1926 Assoc : Assoc_Rec renames Result.Assocs (Index);
1928 if Assoc.Actual.Kind = None then
1929 pragma Assert (No (Assoc.Explicit_Assoc));
1930 if Result.Others_Present then
1931 Assoc.Actual := (Kind => Box_Actual);
1932 Assoc.Actual_Origin := From_Others_Box;
1934 Assoc.Actual := Default (Assoc.Un_Formal);
1935 if Assoc.Actual.Kind /= None then
1936 Assoc.Actual_Origin := From_Default;
1943 if Nkind (I_Node) /= N_Formal_Package_Declaration then
1944 Infer_Actuals (Gen_Assocs => Result);
1947 -- Check for missing actuals
1949 for Index in Result.Assocs'Range loop
1950 if Result.Assocs (Index).Actual.Kind = None then
1951 Error_Msg_Sloc := Sloc (Gen_Unit);
1953 ("missing actual &",
1955 Defining_Entity (Result.Assocs (Index).Un_Formal));
1957 ("\in instantiation of & declared#",
1958 Instantiation_Node, Gen_Unit);
1959 Abandon_Instantiation (Instantiation_Node);
1969 procedure Inference_Msg
1970 (Gen_Unit : Entity_Id;
1971 Inferred_To, Inferred_From : Assoc_Rec;
1972 Was_Inferred : Boolean)
1974 pragma Assert (Debug_Flag_Underscore_II); -- This is only for -gnatd_I
1976 Was : constant String := (if Was_Inferred then "" else "could have ");
1978 -- "if True" below to leave out some verbosity for now:
1979 Inst : constant String :=
1981 else " gen: " & Get_Name_String (Chars (Gen_Unit)));
1982 Decl : constant String := (if True then "" else " declared # ");
1984 R : constant String := " (" & Image (Inferred_To.Reason) & ")";
1986 Mess : constant String :=
1987 "info: " & Was & "inferred `% ='> &`" & Decl & Inst & R;
1988 Mess_2 : constant String :=
1989 "info: `% ='> ...`";
1991 Error_Msg_Name_1 := Chars (Defining_Entity (Inferred_To.An_Formal));
1992 Error_Msg_Sloc := Sloc (Inferred_To.Info_Inferred_Actual);
1993 if not In_Instance then
1994 if Debug_Flag_Underscore_II then
1996 (Mess, Inferred_From.Actual.Name_Exp,
1997 Inferred_To.Info_Inferred_Actual);
1999 Chars (Defining_Identifier (Inferred_From.An_Formal));
2000 Error_Msg_N (Mess_2, Inferred_From.Actual.Name_Exp);
2005 ------------------------------
2006 -- Note_Potential_Inference --
2007 ------------------------------
2009 procedure Note_Potential_Inference
2010 (I_Node : Node_Id; Gen_Assocs : Gen_Assocs_Rec)
2013 if not Debug_Flag_Underscore_II or else Serious_Errors_Detected > 0
2018 for Index in Gen_Assocs.Assocs'Range loop
2020 Assoc : Assoc_Rec renames Gen_Assocs.Assocs (Index);
2022 if Assoc.Actual_Origin = From_Explicit_Actual
2023 and then Present (Assoc.Info_Inferred_Actual)
2024 and then In_Extended_Main_Source_Unit (I_Node)
2025 and then not In_Internal_Unit (I_Node)
2028 (Gen_Assocs.Gen_Unit,
2029 Inferred_To => Assoc,
2030 Inferred_From => Gen_Assocs.Assocs (Assoc.Inferred_From),
2031 Was_Inferred => False);
2035 end Note_Potential_Inference;
2042 (Gen_Assocs : Gen_Assocs_Rec; F : Entity_Id) return Assoc_Index
2045 for Index in Gen_Assocs.Assocs'Range loop
2046 if Defining_Entity (Gen_Assocs.Assocs (Index).An_Formal) = F then
2051 raise Program_Error; -- it must be present
2054 ---------------------
2055 -- Maybe_Infer_One --
2056 ---------------------
2058 procedure Maybe_Infer_One
2059 (Gen_Assocs : in out Gen_Assocs_Rec;
2060 FF, AA : N_Entity_Id; Inferred_From : Assoc_Index;
2061 Reason : Inference_Reason)
2064 if not (Is_Generic_Type (FF)
2065 and then Scope (FF) = Gen_Assocs.Gen_Unit)
2067 return; -- no inference if not a formal type of this generic
2070 if Is_Internal_Name (Chars (FF)) or else Is_Itype (AA) then
2071 return; -- no inference if internally generated
2075 Index : constant Assoc_Index := Find_Assoc (Gen_Assocs, FF);
2076 Assoc : Assoc_Rec renames Gen_Assocs.Assocs (Index);
2077 pragma Assert (Defining_Entity (Assoc.An_Formal) = FF);
2079 From_Actual : constant Node_Id :=
2080 Gen_Assocs.Assocs (Inferred_From).Actual.Name_Exp;
2083 Assoc.Info_Inferred_Actual := AA;
2084 Assoc.Inferred_From := Inferred_From;
2085 Assoc.Reason := Reason;
2087 if Assoc.Actual.Kind = None then
2089 (Name_Exp, New_Occurrence_Of (AA, Sloc (From_Actual)));
2090 Assoc.Actual_Origin := From_Inference;
2092 Error_Msg_GNAT_Extension
2093 ("type inference of generic parameters",
2094 Sloc (From_Actual));
2096 if Debug_Flag_Underscore_II then
2098 (Gen_Assocs.Gen_Unit,
2099 Inferred_To => Assoc,
2100 Inferred_From => Gen_Assocs.Assocs (Assoc.Inferred_From),
2101 Was_Inferred => True);
2105 end Maybe_Infer_One;
2111 procedure Infer_From_Access
2112 (Gen_Assocs : in out Gen_Assocs_Rec;
2113 Index : Assoc_Index;
2118 if Ekind (A_Full) in Access_Kind then
2120 FF : constant Entity_Id :=
2121 Designated_Type (Defining_Entity (F));
2122 AA : constant Entity_Id := Designated_Type (A_Full);
2128 Inferred_From => Index,
2129 Reason => Designated_Type);
2132 end Infer_From_Access;
2134 procedure Infer_From_Array
2135 (Gen_Assocs : in out Gen_Assocs_Rec;
2136 Index : Assoc_Index;
2141 if Ekind (A_Full) in Array_Kind then
2143 F_Index_Type : Opt_N_Is_Index_Id :=
2144 First_Index (Defining_Entity (F));
2145 A_Index_Type : Opt_N_Is_Index_Id :=
2146 First_Index (A_Full);
2148 while Present (F_Index_Type) and then Present (A_Index_Type)
2152 Etype (F_Index_Type),
2153 Etype (A_Index_Type),
2154 Inferred_From => Index,
2155 Reason => Index_Type);
2157 Next_Index (F_Index_Type);
2158 Next_Index (A_Index_Type);
2163 F_Comp_Type : constant Type_Kind_Id :=
2164 Component_Type (Defining_Entity (F));
2165 A_Comp_Type : constant Type_Kind_Id :=
2166 Component_Type (A_Full);
2172 Inferred_From => Index,
2173 Reason => Component_Type);
2176 end Infer_From_Array;
2178 procedure Infer_From_Discriminated
2179 (Gen_Assocs : in out Gen_Assocs_Rec;
2180 Index : Assoc_Index;
2185 if Has_Discriminants (Defining_Entity (F))
2186 and then Present (A_Full)
2187 and then Has_Discriminants (A_Full)
2188 and then Number_Discriminants (A_Full) =
2189 Number_Discriminants (Defining_Entity (F))
2192 F_Discrim : Node_Id := First_Discriminant (Defining_Entity (F));
2193 A_Discrim : Node_Id := First_Discriminant (A_Full);
2195 while Present (F_Discrim) loop
2200 Inferred_From => Index,
2201 Reason => Discriminant_Type);
2203 Next_Discriminant (F_Discrim);
2204 Next_Discriminant (A_Discrim);
2206 pragma Assert (No (A_Discrim)); -- same number as F_Discrim
2209 end Infer_From_Discriminated;
2211 procedure Infer_Actuals (Gen_Assocs : in out Gen_Assocs_Rec) is
2212 -- Note that we can infer FROM defaults, but we cannot infer TO a
2213 -- parameter that has a default. We can also infer from inferred
2216 -- We don't need to check that multiple inferences get the same
2217 -- answer; the second one will get a type mismatch or nonstatically
2220 -- This code needs to be robust, in the sense of tolerating illegal
2221 -- code, because we have not yet checked all legality rules. For
2222 -- example, if a formal type F has a discriminant whose type is
2223 -- another formal type, then we want to infer the type of the
2224 -- discriminant from the actual for F. That actual must have
2225 -- discriminants, but we have not checked that rule yet, so we
2226 -- need to tolerate an actual for F that has no discriminants.
2229 -- For each parameter, check whether we can infer FROM that one TO
2232 -- Process the parameters in reverse order, because the inferred type
2233 -- always comes before the parameter it is inferred from. This
2234 -- ensures that we can do the inference in one pass, including in
2235 -- cases where an inferred type leads to another inferred type.
2236 -- For example, an array type that allows us to infer the component
2237 -- type, which is an access type that allows us to infer the
2238 -- designated type. The reverse loop implies that we will see the
2239 -- array type, then the access type, then the designated type.
2241 for Index in reverse Gen_Assocs.Assocs'Range loop -- NB: "reverse"
2242 if Gen_Assocs.Assocs (Index).Actual.Kind = Name_Exp then
2244 F : constant Node_Id := Gen_Assocs.Assocs (Index).An_Formal;
2245 A_E : constant Node_Id :=
2246 Gen_Assocs.Assocs (Index).Actual.Name_Exp;
2247 A_Full : Entity_Id := Empty;
2249 if Nkind (A_E) in N_Has_Entity then
2250 A_Full := Entity (A_E);
2253 and then Ekind (A_Full) in Incomplete_Kind
2254 and then Present (Full_View (A_Full))
2256 A_Full := Full_View (A_Full);
2260 if Nkind (F) = N_Formal_Type_Declaration
2261 and then Present (A_Full)
2263 case Ekind (Defining_Entity (F)) is
2264 when E_Access_Type | E_General_Access_Type =>
2265 Infer_From_Access (Gen_Assocs, Index, F, A_Full);
2267 when E_Access_Subtype
2268 | E_Access_Attribute_Type
2270 | E_Anonymous_Access_Type =>
2271 raise Program_Error;
2273 when E_Array_Type | E_Array_Subtype =>
2274 Infer_From_Array (Gen_Assocs, Index, F, A_Full);
2276 when E_String_Literal_Subtype =>
2277 raise Program_Error;
2283 Infer_From_Discriminated (Gen_Assocs, Index, F, A_Full);
2292 ---------------------------
2293 -- Abandon_Instantiation --
2294 ---------------------------
2296 procedure Abandon_Instantiation (N : Node_Id) is
2298 Error_Msg_N ("\instantiation abandoned!", N);
2299 raise Instantiation_Error;
2300 end Abandon_Instantiation;
2302 ----------------------------------
2303 -- Adjust_Inherited_Pragma_Sloc --
2304 ----------------------------------
2306 procedure Adjust_Inherited_Pragma_Sloc (N : Node_Id) is
2308 Adjust_Instantiation_Sloc (N, S_Adjustment);
2309 end Adjust_Inherited_Pragma_Sloc;
2311 --------------------------
2312 -- Analyze_Associations --
2313 --------------------------
2315 function Analyze_Associations
2318 F_Copy : List_Id) return List_Id
2322 Result_Renamings : constant List_Id := New_List;
2323 -- To be returned. Includes "renamings" broadly interpreted
2324 -- (e.g. subtypes are used for types).
2326 Actuals_To_Freeze : constant Elist_Id := New_Elmt_List;
2327 Default_Actuals : constant List_Id := New_List;
2329 Gen_Assocs : constant Gen_Assocs_Rec :=
2330 Match_Assocs (I_Node, Formals, F_Copy);
2333 for Matching_Actual_Index in Gen_Assocs.Assocs'Range loop
2335 Assoc : Assoc_Rec renames
2336 Gen_Assocs.Assocs (Matching_Actual_Index);
2338 if Nkind (Assoc.Un_Formal) = N_Formal_Package_Declaration
2339 and then Error_Posted (Assoc.An_Formal)
2341 -- Restrict this to N_Formal_Package_Declaration,
2342 -- because otherwise we miss errors.
2343 Abandon_Instantiation (Instantiation_Node);
2346 if Nkind (Assoc.Un_Formal) in
2347 N_Use_Package_Clause | N_Use_Type_Clause
2349 -- Copy the use clause to where it belongs:
2350 Append (New_Copy_Tree (Assoc.Un_Formal), Result_Renamings);
2353 Analyze_One_Association
2355 Result_Renamings, Default_Actuals, Actuals_To_Freeze);
2360 -- An instantiation freezes all generic actuals, except for incomplete
2361 -- types and subprograms that are not fully defined at the point of
2365 Elmt : Elmt_Id := First_Elmt (Actuals_To_Freeze);
2367 while Present (Elmt) loop
2368 Freeze_Before (I_Node, Node (Elmt));
2373 -- If there are defaults, normalize the tree by adding explicit
2374 -- associations for them. This is required if the instance appears
2375 -- within a generic.
2377 if not Is_Empty_List (Default_Actuals) then
2382 Default := First (Default_Actuals);
2383 while Present (Default) loop
2384 Mark_Rewrite_Insertion (Default);
2388 if No (Generic_Associations (I_Node)) then
2389 Set_Generic_Associations (I_Node, Default_Actuals);
2391 Append_List_To (Generic_Associations (I_Node), Default_Actuals);
2396 Note_Potential_Inference (I_Node, Gen_Assocs);
2398 Check_Fixed_Point_Warning (Gen_Assocs, Result_Renamings);
2400 return Result_Renamings;
2401 end Analyze_Associations;
2403 -----------------------------
2404 -- Analyze_One_Association --
2405 -----------------------------
2407 procedure Analyze_One_Association
2409 Assoc : Associations.Assoc_Rec;
2410 -- Logical 'in out' parameters:
2411 Result_Renamings : List_Id;
2412 Default_Actuals : List_Id;
2413 Actuals_To_Freeze : Elist_Id)
2417 procedure Process_Box_Actual (Formal : Node_Id);
2418 -- Called for "Formal => <>", and also if "Formal => ..." is missing,
2419 -- but there is "others => <>". Add a copy of the declaration of the
2420 -- generic formal to the Result_Renamings.
2422 ------------------------
2423 -- Process_Box_Actual --
2424 ------------------------
2426 procedure Process_Box_Actual (Formal : Node_Id) is
2427 pragma Assert (Assoc.Actual.Kind = Box_Actual);
2428 F_Id : constant Entity_Id := Defining_Entity (Formal);
2429 Decl : constant Node_Id := New_Copy_Tree (Formal);
2430 Id : constant Entity_Id :=
2431 Make_Defining_Identifier (Sloc (F_Id), Chars (F_Id));
2433 if Nkind (Formal) in N_Formal_Subprogram_Declaration then
2434 Set_Defining_Unit_Name (Specification (Decl), Id);
2437 Set_Defining_Identifier (Decl, Id);
2440 Append (Decl, Result_Renamings);
2441 end Process_Box_Actual;
2445 -- Start of processing for Analyze_One_Association
2448 pragma Assert (Assoc.Actual_Origin /= None);
2450 if Assoc.Actual_Origin = From_Explicit_Actual
2451 and then Assoc.Actual.Kind = Name_Exp
2453 Match := Assoc.Actual.Name_Exp;
2455 if Is_Entity_Name (Match) and then Present (Entity (Match)) then
2456 Set_Used_As_Generic_Actual (Entity (Match));
2462 case Nkind (Assoc.Un_Formal) is
2463 when N_Formal_Object_Declaration =>
2464 if Assoc.Actual.Kind = Box_Actual then
2465 Process_Box_Actual (Assoc.Un_Formal);
2469 (Instantiate_Object (Assoc.Un_Formal, Match, Assoc.An_Formal),
2472 -- GNATprove: For a defaulted in-mode parameter, create
2473 -- an entry in the list of defaulted actuals, for
2474 -- GNATprove use. Do not include these defaults for an
2475 -- instance nested within a generic, because the defaults
2476 -- are also used in the analysis of the enclosing
2477 -- generic, and only defaulted subprograms are relevant
2480 if No (Match) and then not Inside_A_Generic then
2481 Append_To (Default_Actuals,
2482 Make_Generic_Association (Sloc (I_Node),
2485 (Defining_Identifier
2486 (Assoc.Un_Formal), Sloc (I_Node)),
2487 Explicit_Generic_Actual_Parameter =>
2488 New_Copy_Tree (Default_Expression (Assoc.Un_Formal))));
2492 -- If the object is a call to an expression function, this
2493 -- is a freezing point for it.
2495 if Is_Entity_Name (Match)
2496 and then Present (Entity (Match))
2498 (Original_Node (Unit_Declaration_Node (Entity (Match))))
2499 = N_Expression_Function
2501 Append_Elmt (Entity (Match), Actuals_To_Freeze);
2504 when N_Formal_Type_Declaration =>
2505 if Assoc.Actual.Kind = Box_Actual then
2506 Process_Box_Actual (Assoc.Un_Formal);
2508 elsif No (Match) then
2509 -- No explicit actual; try default
2511 if Present (Default_Subtype_Mark (Assoc.Un_Formal)) then
2512 Match := New_Copy (Default_Subtype_Mark (Assoc.Un_Formal));
2515 (Assoc.Un_Formal, Match, Assoc.An_Formal,
2518 Append_Elmt (Entity (Match), Actuals_To_Freeze);
2520 -- No explicit actual and no default; must be inference
2523 pragma Assert (Assoc.Actual_Origin = From_Inference);
2525 Match := Assoc.Actual.Name_Exp;
2533 Append_Elmt (Entity (Match), Actuals_To_Freeze);
2539 -- Rewrite mutably tagged types to be their class-wide
2542 if Ekind (Etype (Match)) /= E_Void
2543 and then Is_Mutably_Tagged_Type (Etype (Match))
2545 Rewrite (Match, New_Occurrence_Of
2546 (Class_Wide_Equivalent_Type
2547 (Etype (Match)), Sloc (Match)));
2553 (Assoc.Un_Formal, Match, Assoc.An_Formal,
2557 -- An instantiation is a freeze point for the actuals,
2558 -- unless this is a rewritten formal package, or the
2559 -- formal is an Ada 2012 formal incomplete type.
2561 if Nkind (I_Node) = N_Formal_Package_Declaration
2563 (Ada_Version >= Ada_2012
2565 Ekind (Defining_Identifier (Assoc.An_Formal)) =
2571 Append_Elmt (Entity (Match), Actuals_To_Freeze);
2575 -- A remote access-to-class-wide type is not a legal actual
2576 -- for a generic formal of an access type (E.2.2(17/2)).
2577 -- In GNAT an exception to this rule is introduced when
2578 -- the formal is marked as remote using implementation
2579 -- defined aspect/pragma Remote_Access_Type. In that case
2580 -- the actual must be remote as well.
2582 -- If the current instantiation is the construction of a
2583 -- local copy for a formal package the actuals may be
2584 -- defaulted, and there is no matching actual to check.
2586 if Nkind (Assoc.An_Formal) = N_Formal_Type_Declaration
2588 Nkind (Formal_Type_Definition (Assoc.An_Formal)) =
2589 N_Access_To_Object_Definition
2590 and then Present (Match)
2593 Formal_Ent : constant Entity_Id :=
2594 Defining_Identifier (Assoc.An_Formal);
2596 if Is_Remote_Access_To_Class_Wide_Type (Entity (Match))
2597 = Is_Remote_Types (Formal_Ent)
2599 -- Remoteness of formal and actual match
2603 elsif Is_Remote_Types (Formal_Ent) then
2605 -- Remote formal, non-remote actual
2608 ("actual for& must be remote", Match, Formal_Ent);
2611 -- Non-remote formal, remote actual
2614 ("actual for& may not be remote",
2620 when N_Formal_Subprogram_Declaration =>
2621 -- If there is no corresponding actual, this may be case
2622 -- of partial parameterization, or else the formal has a
2623 -- default or a box.
2625 if Assoc.Actual.Kind = Box_Actual then
2626 Process_Box_Actual (Assoc.Un_Formal);
2629 Append_To (Result_Renamings,
2630 Instantiate_Formal_Subprogram
2631 (Assoc.Un_Formal, Match, Assoc.An_Formal));
2633 -- If formal subprogram has contracts, create wrappers
2634 -- for it. This is an expansion activity that cannot
2635 -- take place e.g. within an enclosing generic unit.
2637 if Has_Contracts (Assoc.An_Formal)
2638 and then (Expander_Active or GNATprove_Mode)
2640 Build_Subprogram_Wrappers
2641 (Match, Assoc.An_Formal, Result_Renamings);
2644 -- An instantiation is a freeze point for the actuals,
2645 -- unless this is a rewritten formal package.
2647 if Nkind (I_Node) /= N_Formal_Package_Declaration
2648 and then Nkind (Match) = N_Identifier
2649 and then Is_Subprogram (Entity (Match))
2651 -- The actual subprogram may rename a routine defined
2652 -- in Standard. Avoid freezing such renamings because
2653 -- subprograms coming from Standard cannot be frozen.
2656 not Renames_Standard_Subprogram (Entity (Match))
2658 -- If the actual subprogram comes from a different
2659 -- unit, it is already frozen, either by a body in
2660 -- that unit or by the end of the declarative part
2661 -- of the unit. This check avoids the freezing of
2662 -- subprograms defined in Standard which are used
2663 -- as generic actuals.
2665 and then In_Same_Code_Unit (Entity (Match), I_Node)
2666 and then Has_Fully_Defined_Profile (Entity (Match))
2668 -- Mark the subprogram as having a delayed freeze
2669 -- since this may be an out-of-order action.
2671 Set_Has_Delayed_Freeze (Entity (Match));
2672 Append_Elmt (Entity (Match), Actuals_To_Freeze);
2676 -- If this is a nested generic, preserve default for later
2677 -- instantiations. We do this as well for GNATprove use,
2678 -- so that the list of generic associations is complete.
2680 if No (Match) and then Box_Present (Assoc.Un_Formal) then
2682 Subp : constant Entity_Id :=
2684 (Specification (Last (Result_Renamings)));
2687 Append_To (Default_Actuals,
2688 Make_Generic_Association (Sloc (I_Node),
2690 New_Occurrence_Of (Subp, Sloc (I_Node)),
2691 Explicit_Generic_Actual_Parameter =>
2692 New_Occurrence_Of (Subp, Sloc (I_Node))));
2696 when N_Formal_Package_Declaration =>
2697 if Assoc.Actual.Kind = Box_Actual then
2698 Process_Box_Actual (Assoc.Un_Formal);
2703 (Instantiate_Formal_Package
2704 (Assoc.Un_Formal, Match, Assoc.An_Formal),
2707 -- Determine whether the actual package needs an explicit
2708 -- freeze node. This is only the case if the actual is
2709 -- declared in the same unit and has a body. Normally
2710 -- packages do not have explicit freeze nodes, and gigi
2711 -- only uses them to elaborate entities in a package
2714 Explicit_Freeze_Check : declare
2715 Actual : constant Entity_Id := Entity (Match);
2716 Gen_Par : Entity_Id;
2718 Needs_Freezing : Boolean;
2721 procedure Check_Generic_Parent;
2722 -- The actual may be an instantiation of a unit
2723 -- declared in a previous instantiation. If that
2724 -- one is also in the current compilation, it must
2725 -- itself be frozen before the actual. The actual
2726 -- may be an instantiation of a generic child unit,
2727 -- in which case the same applies to the instance
2728 -- of the parent which must be frozen before the
2730 -- Should this itself be recursive ???
2732 --------------------------
2733 -- Check_Generic_Parent --
2734 --------------------------
2736 procedure Check_Generic_Parent is
2737 Inst : constant Node_Id :=
2738 Get_Unit_Instantiation_Node (Actual);
2744 if Nkind (Parent (Actual)) = N_Package_Specification
2746 Par := Scope (Generic_Parent (Parent (Actual)));
2748 if Is_Generic_Instance (Par) then
2751 -- If the actual is a child generic unit, check
2752 -- whether the instantiation of the parent is
2753 -- also local and must also be frozen now. We
2754 -- must retrieve the instance node to locate the
2755 -- parent instance if any.
2757 elsif Ekind (Par) = E_Generic_Package
2758 and then Is_Child_Unit (Gen_Par)
2759 and then Ekind (Scope (Gen_Par)) =
2762 if Nkind (Inst) = N_Package_Instantiation
2763 and then Nkind (Name (Inst)) =
2766 -- Retrieve entity of parent instance
2768 Par := Entity (Prefix (Name (Inst)));
2777 and then Is_Generic_Instance (Par)
2778 and then Scope (Par) = Current_Scope
2780 (No (Freeze_Node (Par))
2782 not Is_List_Member (Freeze_Node (Par)))
2784 Set_Has_Delayed_Freeze (Par);
2785 Append_Elmt (Par, Actuals_To_Freeze);
2787 end Check_Generic_Parent;
2789 -- Start of processing for Explicit_Freeze_Check
2792 if Present (Renamed_Entity (Actual)) then
2794 Generic_Parent (Specification
2795 (Unit_Declaration_Node
2796 (Renamed_Entity (Actual))));
2799 Generic_Parent (Specification
2800 (Unit_Declaration_Node (Actual)));
2803 if not Expander_Active
2804 or else not Has_Completion (Actual)
2805 or else not In_Same_Source_Unit (I_Node, Actual)
2806 or else Is_Frozen (Actual)
2808 (Present (Renamed_Entity (Actual))
2810 not In_Same_Source_Unit
2811 (I_Node, (Renamed_Entity (Actual))))
2816 -- Finally we want to exclude such freeze nodes
2817 -- from statement sequences, which freeze
2818 -- everything before them.
2819 -- Is this strictly necessary ???
2821 Needs_Freezing := True;
2823 P := Parent (I_Node);
2824 while Nkind (P) /= N_Compilation_Unit loop
2825 if Nkind (P) = N_Handled_Sequence_Of_Statements
2827 Needs_Freezing := False;
2834 if Needs_Freezing then
2835 Check_Generic_Parent;
2837 -- If the actual is a renaming of a proper
2838 -- instance of the formal package, indicate
2839 -- that it is the instance that must be frozen.
2841 if Nkind (Parent (Actual)) =
2842 N_Package_Renaming_Declaration
2844 Set_Has_Delayed_Freeze
2845 (Renamed_Entity (Actual));
2847 (Renamed_Entity (Actual),
2850 Set_Has_Delayed_Freeze (Actual);
2851 Append_Elmt (Actual, Actuals_To_Freeze);
2855 end Explicit_Freeze_Check;
2859 raise Program_Error;
2862 -- Check for correct use of Ghost entities in generic
2863 -- instantiations (SPARK RM 6.9(10)).
2865 Check_Ghost_Context_In_Generic_Association
2867 Formal => Defining_Entity (Assoc.An_Formal));
2868 end Analyze_One_Association;
2870 -------------------------------
2871 -- Analyze_Formal_Array_Type --
2872 -------------------------------
2874 procedure Analyze_Formal_Array_Type
2875 (T : in out Entity_Id;
2881 -- Treated like a non-generic array declaration, with additional
2886 if Nkind (Def) = N_Constrained_Array_Definition then
2887 DSS := First (Discrete_Subtype_Definitions (Def));
2888 while Present (DSS) loop
2889 if Nkind (DSS) in N_Subtype_Indication
2891 | N_Attribute_Reference
2893 Error_Msg_N ("only a subtype mark is allowed in a formal", DSS);
2900 Array_Type_Declaration (T, Def);
2901 Set_Is_Generic_Type (Base_Type (T));
2903 if Ekind (Component_Type (T)) = E_Incomplete_Type
2904 and then No (Full_View (Component_Type (T)))
2906 Error_Msg_N ("premature usage of incomplete type", Def);
2908 -- Check that range constraint is not allowed on the component type
2909 -- of a generic formal array type (AARM 12.5.3(3))
2911 elsif Is_Internal (Component_Type (T))
2912 and then Present (Subtype_Indication (Component_Definition (Def)))
2913 and then Nkind (Original_Node
2914 (Subtype_Indication (Component_Definition (Def)))) =
2915 N_Subtype_Indication
2918 ("in a formal, a subtype indication can only be "
2919 & "a subtype mark (RM 12.5.3(3))",
2920 Subtype_Indication (Component_Definition (Def)));
2923 end Analyze_Formal_Array_Type;
2925 ---------------------------------------------
2926 -- Analyze_Formal_Decimal_Fixed_Point_Type --
2927 ---------------------------------------------
2929 -- As for other generic types, we create a valid type representation with
2930 -- legal but arbitrary attributes, whose values are never considered
2931 -- static. For all scalar types we introduce an anonymous base type, with
2932 -- the same attributes. We choose the corresponding integer type to be
2933 -- Standard_Integer.
2934 -- Here and in other similar routines, the Sloc of the generated internal
2935 -- type must be the same as the sloc of the defining identifier of the
2936 -- formal type declaration, to provide proper source navigation.
2938 procedure Analyze_Formal_Decimal_Fixed_Point_Type
2942 Loc : constant Source_Ptr := Sloc (Def);
2944 Base : constant Entity_Id :=
2946 (E_Decimal_Fixed_Point_Type,
2948 Sloc (Defining_Identifier (Parent (Def))), 'G');
2950 Int_Base : constant Entity_Id := Standard_Integer;
2951 Delta_Val : constant Ureal := Ureal_1;
2952 Digs_Val : constant Uint := Uint_6;
2954 function Make_Dummy_Bound return Node_Id;
2955 -- Return a properly typed universal real literal to use as a bound
2957 ----------------------
2958 -- Make_Dummy_Bound --
2959 ----------------------
2961 function Make_Dummy_Bound return Node_Id is
2962 Bound : constant Node_Id := Make_Real_Literal (Loc, Ureal_1);
2964 Set_Etype (Bound, Universal_Real);
2966 end Make_Dummy_Bound;
2968 -- Start of processing for Analyze_Formal_Decimal_Fixed_Point_Type
2973 Set_Etype (Base, Base);
2974 Set_Size_Info (Base, Int_Base);
2975 Set_RM_Size (Base, RM_Size (Int_Base));
2976 Set_First_Rep_Item (Base, First_Rep_Item (Int_Base));
2977 Set_Digits_Value (Base, Digs_Val);
2978 Set_Delta_Value (Base, Delta_Val);
2979 Set_Small_Value (Base, Delta_Val);
2980 Set_Scalar_Range (Base,
2982 Low_Bound => Make_Dummy_Bound,
2983 High_Bound => Make_Dummy_Bound));
2985 Set_Is_Generic_Type (Base);
2986 Set_Parent (Base, Parent (Def));
2988 Mutate_Ekind (T, E_Decimal_Fixed_Point_Subtype);
2989 Set_Etype (T, Base);
2990 Set_Size_Info (T, Int_Base);
2991 Set_RM_Size (T, RM_Size (Int_Base));
2992 Set_First_Rep_Item (T, First_Rep_Item (Int_Base));
2993 Set_Digits_Value (T, Digs_Val);
2994 Set_Delta_Value (T, Delta_Val);
2995 Set_Small_Value (T, Delta_Val);
2996 Set_Scalar_Range (T, Scalar_Range (Base));
2997 Set_Is_Constrained (T);
2999 Check_Restriction (No_Fixed_Point, Def);
3000 end Analyze_Formal_Decimal_Fixed_Point_Type;
3002 -------------------------------------------
3003 -- Analyze_Formal_Derived_Interface_Type --
3004 -------------------------------------------
3006 procedure Analyze_Formal_Derived_Interface_Type
3011 Loc : constant Source_Ptr := Sloc (Def);
3014 -- Rewrite as a type declaration of a derived type. This ensures that
3015 -- the interface list and primitive operations are properly captured.
3018 Make_Full_Type_Declaration (Loc,
3019 Defining_Identifier => T,
3020 Type_Definition => Def));
3022 -- Keep the aspects from the original node
3024 Move_Aspects (Original_Node (N), N);
3027 Set_Is_Generic_Type (T);
3028 end Analyze_Formal_Derived_Interface_Type;
3030 ---------------------------------
3031 -- Analyze_Formal_Derived_Type --
3032 ---------------------------------
3034 procedure Analyze_Formal_Derived_Type
3039 Loc : constant Source_Ptr := Sloc (Def);
3040 Unk_Disc : constant Boolean := Unknown_Discriminants_Present (N);
3044 Set_Is_Generic_Type (T);
3046 if Private_Present (Def) then
3048 Make_Private_Extension_Declaration (Loc,
3049 Defining_Identifier => T,
3050 Discriminant_Specifications => Discriminant_Specifications (N),
3051 Unknown_Discriminants_Present => Unk_Disc,
3052 Subtype_Indication => Subtype_Mark (Def),
3053 Interface_List => Interface_List (Def));
3055 Set_Abstract_Present (New_N, Abstract_Present (Def));
3056 Set_Limited_Present (New_N, Limited_Present (Def));
3057 Set_Synchronized_Present (New_N, Synchronized_Present (Def));
3061 Make_Full_Type_Declaration (Loc,
3062 Defining_Identifier => T,
3063 Discriminant_Specifications =>
3064 Discriminant_Specifications (Parent (T)),
3066 Make_Derived_Type_Definition (Loc,
3067 Subtype_Indication => Subtype_Mark (Def)));
3069 Set_Abstract_Present
3070 (Type_Definition (New_N), Abstract_Present (Def));
3072 (Type_Definition (New_N), Limited_Present (Def));
3077 -- Keep the aspects from the original node
3079 Move_Aspects (Original_Node (N), N);
3084 if not Is_Composite_Type (T) then
3086 ("unknown discriminants not allowed for elementary types", N);
3088 Set_Has_Unknown_Discriminants (T);
3089 Set_Is_Constrained (T, False);
3093 if Subtype_Mark (Def) <= Empty_Or_Error then
3094 pragma Assert (Serious_Errors_Detected > 0);
3095 -- avoid passing bad argument to Entity
3099 -- If the parent type has a known size, so does the formal, which makes
3100 -- legal representation clauses that involve the formal.
3102 Set_Size_Known_At_Compile_Time
3103 (T, Size_Known_At_Compile_Time (Entity (Subtype_Mark (Def))));
3104 end Analyze_Formal_Derived_Type;
3106 ----------------------------------
3107 -- Analyze_Formal_Discrete_Type --
3108 ----------------------------------
3110 -- The operations defined for a discrete types are those of an enumeration
3111 -- type. The size is set to an arbitrary value, for use in analyzing the
3114 procedure Analyze_Formal_Discrete_Type (T : Entity_Id; Def : Node_Id) is
3115 Loc : constant Source_Ptr := Sloc (Def);
3119 Base : constant Entity_Id :=
3121 (E_Floating_Point_Type, Current_Scope,
3122 Sloc (Defining_Identifier (Parent (Def))), 'G');
3126 Mutate_Ekind (T, E_Enumeration_Subtype);
3127 Set_Etype (T, Base);
3129 Reinit_Alignment (T);
3130 Set_Is_Generic_Type (T);
3131 Set_Is_Constrained (T);
3133 -- For semantic analysis, the bounds of the type must be set to some
3134 -- non-static value. The simplest is to create attribute nodes for those
3135 -- bounds, that refer to the type itself. These bounds are never
3136 -- analyzed but serve as place-holders.
3139 Make_Attribute_Reference (Loc,
3140 Attribute_Name => Name_First,
3141 Prefix => New_Occurrence_Of (T, Loc));
3145 Make_Attribute_Reference (Loc,
3146 Attribute_Name => Name_Last,
3147 Prefix => New_Occurrence_Of (T, Loc));
3150 Set_Scalar_Range (T,
3155 Mutate_Ekind (Base, E_Enumeration_Type);
3156 Set_Etype (Base, Base);
3157 Init_Size (Base, 8);
3158 Reinit_Alignment (Base);
3159 Set_Is_Generic_Type (Base);
3160 Set_Scalar_Range (Base, Scalar_Range (T));
3161 Set_Parent (Base, Parent (Def));
3162 end Analyze_Formal_Discrete_Type;
3164 ----------------------------------
3165 -- Analyze_Formal_Floating_Type --
3166 ---------------------------------
3168 procedure Analyze_Formal_Floating_Type (T : Entity_Id; Def : Node_Id) is
3169 Base : constant Entity_Id :=
3171 (E_Floating_Point_Type, Current_Scope,
3172 Sloc (Defining_Identifier (Parent (Def))), 'G');
3175 -- The various semantic attributes are taken from the predefined type
3176 -- Float, just so that all of them are initialized. Their values are
3177 -- never used because no constant folding or expansion takes place in
3178 -- the generic itself.
3181 Mutate_Ekind (T, E_Floating_Point_Subtype);
3182 Set_Etype (T, Base);
3183 Set_Size_Info (T, (Standard_Float));
3184 Set_RM_Size (T, RM_Size (Standard_Float));
3185 Set_Digits_Value (T, Digits_Value (Standard_Float));
3186 Set_Scalar_Range (T, Scalar_Range (Standard_Float));
3187 Set_Is_Constrained (T);
3189 Set_Is_Generic_Type (Base);
3190 Set_Etype (Base, Base);
3191 Set_Size_Info (Base, (Standard_Float));
3192 Set_RM_Size (Base, RM_Size (Standard_Float));
3193 Set_Digits_Value (Base, Digits_Value (Standard_Float));
3194 Set_Scalar_Range (Base, Scalar_Range (Standard_Float));
3195 Set_Parent (Base, Parent (Def));
3197 Check_Restriction (No_Floating_Point, Def);
3198 end Analyze_Formal_Floating_Type;
3200 -----------------------------------
3201 -- Analyze_Formal_Interface_Type;--
3202 -----------------------------------
3204 procedure Analyze_Formal_Interface_Type
3209 Loc : constant Source_Ptr := Sloc (N);
3214 Make_Full_Type_Declaration (Loc,
3215 Defining_Identifier => T,
3216 Type_Definition => Def);
3220 -- Keep the aspects from the original node
3222 Move_Aspects (Original_Node (N), N);
3225 Set_Is_Generic_Type (T);
3226 end Analyze_Formal_Interface_Type;
3228 ---------------------------------
3229 -- Analyze_Formal_Modular_Type --
3230 ---------------------------------
3232 procedure Analyze_Formal_Modular_Type (T : Entity_Id; Def : Node_Id) is
3234 -- Apart from their entity kind, generic modular types are treated like
3235 -- signed integer types, and have the same attributes.
3237 Analyze_Formal_Signed_Integer_Type (T, Def);
3238 Mutate_Ekind (T, E_Modular_Integer_Subtype);
3239 Mutate_Ekind (Etype (T), E_Modular_Integer_Type);
3241 end Analyze_Formal_Modular_Type;
3243 ---------------------------------------
3244 -- Analyze_Formal_Object_Declaration --
3245 ---------------------------------------
3247 procedure Analyze_Formal_Object_Declaration (N : Node_Id) is
3248 E : constant Node_Id := Default_Expression (N);
3249 Id : constant Node_Id := Defining_Identifier (N);
3252 Parent_Installed : Boolean := False;
3258 Check_Abbreviated_Instance (Parent (N), Parent_Installed);
3260 -- Determine the mode of the formal object
3262 if Out_Present (N) then
3263 K := E_Generic_In_Out_Parameter;
3265 if not In_Present (N) then
3266 Error_Msg_N ("formal generic objects cannot have mode OUT", N);
3270 K := E_Generic_In_Parameter;
3273 if Present (Subtype_Mark (N)) then
3274 Find_Type (Subtype_Mark (N));
3275 T := Entity (Subtype_Mark (N));
3277 -- Verify that there is no redundant null exclusion
3279 if Null_Exclusion_Present (N) then
3280 if not Is_Access_Type (T) then
3282 ("null exclusion can only apply to an access type", N);
3284 elsif Can_Never_Be_Null (T) then
3286 ("`NOT NULL` not allowed (& already excludes null)", N, T);
3290 -- Ada 2005 (AI-423): Formal object with an access definition
3293 Check_Access_Definition (N);
3294 T := Access_Definition
3296 N => Access_Definition (N));
3299 if Ekind (T) = E_Incomplete_Type then
3301 Error_Node : Node_Id;
3304 if Present (Subtype_Mark (N)) then
3305 Error_Node := Subtype_Mark (N);
3307 Check_Access_Definition (N);
3308 Error_Node := Access_Definition (N);
3311 Error_Msg_N ("premature usage of incomplete type", Error_Node);
3315 if K = E_Generic_In_Parameter then
3317 -- Ada 2005 (AI-287): Limited aggregates allowed in generic formals
3319 if Ada_Version < Ada_2005 and then Is_Limited_Type (T) then
3321 ("generic formal of mode IN must not be of limited type", N);
3322 Explain_Limited_Type (T, N);
3325 if Is_Abstract_Type (T) then
3327 ("generic formal of mode IN must not be of abstract type", N);
3331 Preanalyze_Spec_Expression (E, T);
3333 -- The default for a ghost generic formal IN parameter of
3334 -- access-to-variable type should be a ghost object (SPARK
3337 if Is_Access_Variable (T) then
3338 Check_Ghost_Formal_Variable
3341 Is_Default => True);
3344 if Is_Limited_Type (T) and then not OK_For_Limited_Init (T, E) then
3346 ("initialization not allowed for limited types", E);
3347 Explain_Limited_Type (T, E);
3351 Mutate_Ekind (Id, K);
3354 -- Case of generic IN OUT parameter
3357 -- If the formal has an unconstrained type, construct its actual
3358 -- subtype, as is done for subprogram formals. In this fashion, all
3359 -- its uses can refer to specific bounds.
3361 Mutate_Ekind (Id, K);
3364 if (Is_Array_Type (T) and then not Is_Constrained (T))
3365 or else (Ekind (T) = E_Record_Type and then Has_Discriminants (T))
3368 Non_Freezing_Ref : constant Node_Id :=
3369 New_Occurrence_Of (Id, Sloc (Id));
3373 -- Make sure the actual subtype doesn't generate bogus freezing
3375 Set_Must_Not_Freeze (Non_Freezing_Ref);
3376 Decl := Build_Actual_Subtype (T, Non_Freezing_Ref);
3377 Insert_Before_And_Analyze (N, Decl);
3378 Set_Actual_Subtype (Id, Defining_Identifier (Decl));
3381 Set_Actual_Subtype (Id, T);
3386 ("initialization not allowed for `IN OUT` formals", N);
3390 Analyze_Aspect_Specifications (N, Id);
3392 if Parent_Installed then
3395 end Analyze_Formal_Object_Declaration;
3397 ----------------------------------------------
3398 -- Analyze_Formal_Ordinary_Fixed_Point_Type --
3399 ----------------------------------------------
3401 procedure Analyze_Formal_Ordinary_Fixed_Point_Type
3405 Loc : constant Source_Ptr := Sloc (Def);
3406 Base : constant Entity_Id :=
3408 (E_Ordinary_Fixed_Point_Type, Current_Scope,
3409 Sloc (Defining_Identifier (Parent (Def))), 'G');
3412 -- The semantic attributes are set for completeness only, their values
3413 -- will never be used, since all properties of the type are non-static.
3416 Mutate_Ekind (T, E_Ordinary_Fixed_Point_Subtype);
3417 Set_Etype (T, Base);
3418 Set_Size_Info (T, Standard_Integer);
3419 Set_RM_Size (T, RM_Size (Standard_Integer));
3420 Set_Small_Value (T, Ureal_1);
3421 Set_Delta_Value (T, Ureal_1);
3422 Set_Scalar_Range (T,
3424 Low_Bound => Make_Real_Literal (Loc, Ureal_1),
3425 High_Bound => Make_Real_Literal (Loc, Ureal_1)));
3426 Set_Is_Constrained (T);
3428 Set_Is_Generic_Type (Base);
3429 Set_Etype (Base, Base);
3430 Set_Size_Info (Base, Standard_Integer);
3431 Set_RM_Size (Base, RM_Size (Standard_Integer));
3432 Set_Small_Value (Base, Ureal_1);
3433 Set_Delta_Value (Base, Ureal_1);
3434 Set_Scalar_Range (Base, Scalar_Range (T));
3435 Set_Parent (Base, Parent (Def));
3437 Check_Restriction (No_Fixed_Point, Def);
3438 end Analyze_Formal_Ordinary_Fixed_Point_Type;
3440 ----------------------------------------
3441 -- Analyze_Formal_Package_Declaration --
3442 ----------------------------------------
3444 procedure Analyze_Formal_Package_Declaration (N : Node_Id) is
3445 Gen_Id : constant Node_Id := Name (N);
3446 Loc : constant Source_Ptr := Sloc (N);
3447 Pack_Id : constant Entity_Id := Defining_Identifier (N);
3450 Gen_Unit : Entity_Id;
3453 Vis_Prims_List : Elist_Id := No_Elist;
3454 -- List of primitives made temporarily visible in the instantiation
3455 -- to match the visibility of the formal type.
3457 function Build_Local_Package return Node_Id;
3458 -- The formal package is rewritten so that its parameters are replaced
3459 -- with corresponding declarations. For parameters with bona fide
3460 -- associations these declarations are created by Analyze_Associations
3461 -- as for a regular instantiation. For boxed parameters, we preserve
3462 -- the formal declarations and analyze them, in order to introduce
3463 -- entities of the right kind in the environment of the formal.
3465 -------------------------
3466 -- Build_Local_Package --
3467 -------------------------
3469 function Build_Local_Package return Node_Id is
3471 Pack_Decl : Node_Id;
3474 -- Within the formal, the name of the generic package is a renaming
3475 -- of the formal (as for a regular instantiation).
3478 Make_Package_Declaration (Loc,
3481 (Specification (Original_Node (Gen_Decl)),
3482 Empty, Instantiating => True));
3485 Make_Package_Renaming_Declaration (Loc,
3486 Defining_Unit_Name =>
3487 Make_Defining_Identifier (Loc, Chars (Gen_Unit)),
3488 Name => New_Occurrence_Of (Formal, Loc));
3490 if Nkind (Gen_Id) = N_Identifier
3491 and then Chars (Gen_Id) = Chars (Pack_Id)
3494 ("& is hidden within declaration of instance", Gen_Id, Gen_Unit);
3497 -- If the formal is declared with a box, or with an others choice,
3498 -- create corresponding declarations for all entities in the formal
3499 -- part, so that names with the proper types are available in the
3500 -- specification of the formal package.
3502 -- On the other hand, if there are no associations (as in "new G;"),
3503 -- then all the formals must have defaults, and this will be checked
3504 -- by the call to Analyze_Associations.
3507 or else Nkind (First (Generic_Associations (N))) = N_Others_Choice
3510 Formal_Decl : Node_Id;
3513 -- TBA : for a formal package, need to recurse ???
3518 (Generic_Formal_Declarations (Original_Node (Gen_Decl)));
3519 while Present (Formal_Decl) loop
3523 (Formal_Decl, Empty, Instantiating => True));
3528 -- If generic associations are present, use Analyze_Associations to
3529 -- create the proper renaming declarations.
3533 Act_Tree : constant Node_Id :=
3535 (Original_Node (Gen_Decl), Empty,
3536 Instantiating => True);
3539 Generic_Renamings.Set_Last (0);
3540 Generic_Renamings_HTable.Reset;
3541 Instantiation_Node := N;
3544 Analyze_Associations
3545 (I_Node => Original_Node (N),
3546 Formals => Generic_Formal_Declarations (Act_Tree),
3547 F_Copy => Generic_Formal_Declarations (Gen_Decl));
3549 Vis_Prims_List := Check_Hidden_Primitives (Decls);
3553 Append (Renaming, To => Decls);
3555 -- Add generated declarations ahead of local declarations in
3558 if No (Visible_Declarations (Specification (Pack_Decl))) then
3559 Set_Visible_Declarations (Specification (Pack_Decl), Decls);
3562 (First (Visible_Declarations (Specification (Pack_Decl))),
3567 end Build_Local_Package;
3571 Save_ISMP : constant Boolean := Ignore_SPARK_Mode_Pragmas_In_Instance;
3572 -- Save flag Ignore_SPARK_Mode_Pragmas_In_Instance for restore on exit
3574 Associations : Boolean := True;
3576 Parent_Installed : Boolean := False;
3577 Parent_Instance : Entity_Id;
3578 Renaming_In_Par : Entity_Id;
3580 -- Start of processing for Analyze_Formal_Package_Declaration
3583 Check_Text_IO_Special_Unit (Gen_Id);
3586 Check_Generic_Child_Unit (Gen_Id, Parent_Installed);
3587 Gen_Unit := Entity (Gen_Id);
3589 -- Check for a formal package that is a package renaming
3591 if Present (Renamed_Entity (Gen_Unit)) then
3593 -- Indicate that unit is used, before replacing it with renamed
3594 -- entity for use below.
3596 if In_Extended_Main_Source_Unit (N) then
3597 Set_Is_Instantiated (Gen_Unit);
3598 Generate_Reference (Gen_Unit, N);
3601 Gen_Unit := Renamed_Entity (Gen_Unit);
3604 if Ekind (Gen_Unit) /= E_Generic_Package then
3605 Error_Msg_N ("expect generic package name", Gen_Id);
3609 elsif Gen_Unit = Current_Scope then
3611 ("generic package cannot be used as a formal package of itself",
3616 elsif In_Open_Scopes (Gen_Unit) then
3617 if Is_Compilation_Unit (Gen_Unit)
3618 and then Is_Child_Unit (Current_Scope)
3620 -- Special-case the error when the formal is a parent, and
3621 -- continue analysis to minimize cascaded errors.
3624 ("generic parent cannot be used as formal package of a child "
3629 ("generic package cannot be used as a formal package within "
3630 & "itself", Gen_Id);
3636 -- Check that name of formal package does not hide name of generic,
3637 -- or its leading prefix. This check must be done separately because
3638 -- the name of the generic has already been analyzed.
3641 Gen_Name : Entity_Id;
3645 while Nkind (Gen_Name) = N_Expanded_Name loop
3646 Gen_Name := Prefix (Gen_Name);
3649 if Chars (Gen_Name) = Chars (Pack_Id) then
3651 ("& is hidden within declaration of formal package",
3657 or else No (Generic_Associations (N))
3658 or else Nkind (First (Generic_Associations (N))) = N_Others_Choice
3660 Associations := False;
3663 -- If there are no generic associations, the generic parameters appear
3664 -- as local entities and are instantiated like them. We copy the generic
3665 -- package declaration as if it were an instantiation, and analyze it
3666 -- like a regular package, except that we treat the formals as
3667 -- additional visible components.
3669 Gen_Decl := Unit_Declaration_Node (Gen_Unit);
3671 if In_Extended_Main_Source_Unit (N) then
3672 Set_Is_Instantiated (Gen_Unit);
3673 Generate_Reference (Gen_Unit, N);
3676 Formal := New_Copy (Pack_Id);
3677 Create_Instantiation_Source (N, Gen_Unit, S_Adjustment);
3679 -- Make local generic without formals. The formals will be replaced with
3680 -- internal declarations.
3683 New_N := Build_Local_Package;
3685 -- If there are errors in the parameter list, Analyze_Associations
3686 -- raises Instantiation_Error. Patch the declaration to prevent further
3687 -- exception propagation.
3690 when Instantiation_Error =>
3691 Enter_Name (Formal);
3692 Mutate_Ekind (Formal, E_Variable);
3693 Set_Etype (Formal, Any_Type);
3694 Restore_Hidden_Primitives (Vis_Prims_List);
3696 if Parent_Installed then
3704 Set_Defining_Unit_Name (Specification (New_N), Formal);
3705 Set_Generic_Parent (Specification (N), Gen_Unit);
3706 Set_Instance_Env (Gen_Unit, Formal);
3707 Set_Is_Generic_Instance (Formal);
3709 Enter_Name (Formal);
3710 Mutate_Ekind (Formal, E_Package);
3711 Set_Etype (Formal, Standard_Void_Type);
3712 Set_Inner_Instances (Formal, New_Elmt_List);
3714 -- It is unclear that any aspects can apply to a formal package
3715 -- declaration, given that they look like a hidden conformance
3716 -- requirement on the corresponding actual. However, Abstract_State
3717 -- must be treated specially because it generates declarations that
3718 -- must appear before other declarations in the specification and
3719 -- must be analyzed at once.
3721 if Present (Aspect_Specifications (Gen_Decl)) then
3722 if No (Aspect_Specifications (N)) then
3723 Set_Aspect_Specifications (N, New_List);
3727 ASN : Node_Id := First (Aspect_Specifications (Gen_Decl));
3731 while Present (ASN) loop
3732 if Get_Aspect_Id (ASN) = Aspect_Abstract_State then
3734 Copy_Generic_Node (ASN, Empty, Instantiating => True);
3735 Set_Entity (New_A, Formal);
3736 Set_Analyzed (New_A, False);
3737 Append (New_A, Aspect_Specifications (N));
3738 Analyze_Aspect_Specifications (N, Formal);
3747 Push_Scope (Formal);
3749 -- Manually set the SPARK_Mode from the context because the package
3750 -- declaration is never analyzed.
3752 Set_SPARK_Pragma (Formal, SPARK_Mode_Pragma);
3753 Set_SPARK_Aux_Pragma (Formal, SPARK_Mode_Pragma);
3754 Set_SPARK_Pragma_Inherited (Formal);
3755 Set_SPARK_Aux_Pragma_Inherited (Formal);
3757 if Is_Child_Unit (Gen_Unit) and then Parent_Installed then
3759 -- Similarly, we have to make the name of the formal visible in the
3760 -- parent instance, to resolve properly fully qualified names that
3761 -- may appear in the generic unit. The parent instance has been
3762 -- placed on the scope stack ahead of the current scope.
3764 Parent_Instance := Scope_Stack.Table (Scope_Stack.Last - 1).Entity;
3767 Make_Defining_Identifier (Loc, Chars (Gen_Unit));
3768 Mutate_Ekind (Renaming_In_Par, E_Package);
3769 Set_Is_Not_Self_Hidden (Renaming_In_Par);
3770 Set_Etype (Renaming_In_Par, Standard_Void_Type);
3771 Set_Scope (Renaming_In_Par, Parent_Instance);
3772 Set_Parent (Renaming_In_Par, Parent (Formal));
3773 Set_Renamed_Entity (Renaming_In_Par, Formal);
3774 Append_Entity (Renaming_In_Par, Parent_Instance);
3777 -- A formal package declaration behaves as a package instantiation with
3778 -- respect to SPARK_Mode "off". If the annotation is "off" or altogether
3779 -- missing, set the global flag which signals Analyze_Pragma to ingnore
3780 -- all SPARK_Mode pragmas within the generic_package_name.
3782 if SPARK_Mode /= On then
3783 Ignore_SPARK_Mode_Pragmas_In_Instance := True;
3785 -- Mark the formal spec in case the body is instantiated at a later
3786 -- pass. This preserves the original context in effect for the body.
3788 Set_Ignore_SPARK_Mode_Pragmas (Formal);
3791 Analyze (Specification (N));
3793 -- The formals for which associations are provided are not visible
3794 -- outside of the formal package. The others are still declared by a
3795 -- formal parameter declaration.
3797 -- If there are no associations, the only local entity to hide is the
3798 -- generated package renaming itself.
3804 E := First_Entity (Formal);
3805 while Present (E) loop
3806 if Associations and then not Is_Generic_Formal (E) then
3810 if Ekind (E) = E_Package and then Renamed_Entity (E) = Formal then
3819 End_Package_Scope (Formal);
3820 Restore_Hidden_Primitives (Vis_Prims_List);
3822 if Parent_Installed then
3828 -- Inside the generic unit, the formal package is a regular package, but
3829 -- no body is needed for it. Note that after instantiation, the defining
3830 -- unit name we need is in the new tree and not in the original (see
3831 -- Package_Instantiation). A generic formal package is an instance, and
3832 -- can be used as an actual for an inner instance.
3834 Set_Has_Completion (Formal, True);
3836 -- Add semantic information to the original defining identifier.
3838 Mutate_Ekind (Pack_Id, E_Package);
3839 Set_Etype (Pack_Id, Standard_Void_Type);
3840 Set_Scope (Pack_Id, Scope (Formal));
3841 Set_Has_Completion (Pack_Id, True);
3844 -- Unclear that any other aspects may appear here, analyze them
3845 -- for completion, given that the grammar allows their appearance.
3847 Analyze_Aspect_Specifications (N, Pack_Id);
3849 Ignore_SPARK_Mode_Pragmas_In_Instance := Save_ISMP;
3850 end Analyze_Formal_Package_Declaration;
3852 ---------------------------------
3853 -- Analyze_Formal_Private_Type --
3854 ---------------------------------
3856 procedure Analyze_Formal_Private_Type
3862 New_Private_Type (N, T, Def);
3864 -- Set the size to an arbitrary but legal value
3866 Set_Size_Info (T, Standard_Integer);
3867 Set_RM_Size (T, RM_Size (Standard_Integer));
3868 end Analyze_Formal_Private_Type;
3870 ------------------------------------
3871 -- Analyze_Formal_Incomplete_Type --
3872 ------------------------------------
3874 procedure Analyze_Formal_Incomplete_Type
3880 Mutate_Ekind (T, E_Incomplete_Type);
3882 Set_Private_Dependents (T, New_Elmt_List);
3884 if Tagged_Present (Def) then
3885 Set_Is_Tagged_Type (T);
3886 Make_Class_Wide_Type (T);
3887 Set_Direct_Primitive_Operations (T, New_Elmt_List);
3889 end Analyze_Formal_Incomplete_Type;
3891 ----------------------------------------
3892 -- Analyze_Formal_Signed_Integer_Type --
3893 ----------------------------------------
3895 procedure Analyze_Formal_Signed_Integer_Type
3899 Base : constant Entity_Id :=
3901 (E_Signed_Integer_Type,
3903 Sloc (Defining_Identifier (Parent (Def))), 'G');
3908 Mutate_Ekind (T, E_Signed_Integer_Subtype);
3909 Set_Etype (T, Base);
3910 Set_Size_Info (T, Standard_Integer);
3911 Set_RM_Size (T, RM_Size (Standard_Integer));
3912 Set_Scalar_Range (T, Scalar_Range (Standard_Integer));
3913 Set_Is_Constrained (T);
3915 Set_Is_Generic_Type (Base);
3916 Set_Size_Info (Base, Standard_Integer);
3917 Set_RM_Size (Base, RM_Size (Standard_Integer));
3918 Set_Etype (Base, Base);
3919 Set_Scalar_Range (Base, Scalar_Range (Standard_Integer));
3920 Set_Parent (Base, Parent (Def));
3921 end Analyze_Formal_Signed_Integer_Type;
3923 -------------------------------------------
3924 -- Analyze_Formal_Subprogram_Declaration --
3925 -------------------------------------------
3927 procedure Analyze_Formal_Subprogram_Declaration (N : Node_Id) is
3928 Spec : constant Node_Id := Specification (N);
3929 Def : constant Node_Id := Default_Name (N);
3930 Expr : constant Node_Id := Expression (N);
3931 Nam : constant Entity_Id := Defining_Unit_Name (Spec);
3933 Parent_Installed : Boolean := False;
3941 if Nkind (Nam) = N_Defining_Program_Unit_Name then
3942 Error_Msg_N ("name of formal subprogram must be a direct name", Nam);
3946 Check_Abbreviated_Instance (Parent (N), Parent_Installed);
3948 Analyze_Subprogram_Declaration (N);
3949 Set_Is_Formal_Subprogram (Nam);
3950 Set_Has_Completion (Nam);
3952 if Nkind (N) = N_Formal_Abstract_Subprogram_Declaration then
3953 Set_Is_Abstract_Subprogram (Nam);
3955 Set_Is_Dispatching_Operation (Nam);
3957 -- A formal abstract procedure cannot have a null default
3958 -- (RM 12.6(4.1/2)).
3960 if Has_Null_Default (N) then
3962 ("a formal abstract subprogram cannot default to null", Spec);
3965 -- A formal abstract function cannot have an expression default
3966 -- (expression defaults are allowed for nonabstract formal functions
3967 -- when extensions are enabled).
3969 if Nkind (Spec) = N_Function_Specification
3970 and then Present (Expr)
3973 ("a formal abstract subprogram cannot default to an expression",
3978 Ctrl_Type : constant Entity_Id := Find_Dispatching_Type (Nam);
3980 if No (Ctrl_Type) then
3982 ("abstract formal subprogram must have a controlling type",
3985 elsif Ada_Version >= Ada_2012
3986 and then Is_Incomplete_Type (Ctrl_Type)
3989 ("controlling type of abstract formal subprogram cannot "
3990 & "be incomplete type", N, Ctrl_Type);
3993 Check_Controlling_Formals (Ctrl_Type, Nam);
3998 -- Default name is resolved at the point of instantiation
4000 if Box_Present (N) then
4003 -- Default name is bound at the point of generic declaration
4005 elsif Present (Def) then
4006 if Nkind (Def) = N_Operator_Symbol then
4007 Find_Direct_Name (Def);
4009 elsif Nkind (Def) /= N_Attribute_Reference then
4013 -- For an attribute reference, analyze the prefix and verify
4014 -- that it has the proper profile for the subprogram.
4016 Analyze (Prefix (Def));
4017 Valid_Default_Attribute (Nam, Def);
4021 -- The default for a ghost generic formal procedure should be a ghost
4022 -- procedure (SPARK RM 6.9(13)).
4024 if Ekind (Nam) = E_Procedure then
4026 Def_E : Entity_Id := Empty;
4028 if Nkind (Def) in N_Has_Entity then
4029 Def_E := Entity (Def);
4032 Check_Ghost_Formal_Procedure_Or_Package
4036 Is_Default => True);
4040 -- Default name may be overloaded, in which case the interpretation
4041 -- with the correct profile must be selected, as for a renaming.
4042 -- If the definition is an indexed component, it must denote a
4043 -- member of an entry family. If it is a selected component, it
4044 -- can be a protected operation.
4046 if Etype (Def) = Any_Type then
4049 elsif Nkind (Def) = N_Selected_Component then
4050 if not Is_Overloadable (Entity (Selector_Name (Def))) then
4051 Error_Msg_N ("expect valid subprogram name as default", Def);
4054 elsif Nkind (Def) = N_Indexed_Component then
4055 if Is_Entity_Name (Prefix (Def)) then
4056 if Ekind (Entity (Prefix (Def))) /= E_Entry_Family then
4057 Error_Msg_N ("expect valid subprogram name as default", Def);
4060 elsif Nkind (Prefix (Def)) = N_Selected_Component then
4061 if Ekind (Entity (Selector_Name (Prefix (Def)))) /=
4064 Error_Msg_N ("expect valid subprogram name as default", Def);
4068 Error_Msg_N ("expect valid subprogram name as default", Def);
4072 elsif Nkind (Def) = N_Character_Literal then
4074 -- Needs some type checks: subprogram should be parameterless???
4076 Resolve (Def, (Etype (Nam)));
4078 elsif not Is_Entity_Name (Def)
4079 or else not Is_Overloadable (Entity (Def))
4081 Error_Msg_N ("expect valid subprogram name as default", Def);
4084 elsif not Is_Overloaded (Def) then
4085 Subp := Entity (Def);
4088 Error_Msg_N ("premature usage of formal subprogram", Def);
4090 elsif not Entity_Matches_Spec (Subp, Nam) then
4091 Error_Msg_N ("no visible entity matches specification", Def);
4094 -- More than one interpretation, so disambiguate as for a renaming
4099 I1 : Interp_Index := 0;
4105 Get_First_Interp (Def, I, It);
4106 while Present (It.Nam) loop
4107 if Entity_Matches_Spec (It.Nam, Nam) then
4108 if Subp /= Any_Id then
4109 It1 := Disambiguate (Def, I1, I, Etype (Subp));
4111 if It1 = No_Interp then
4112 Error_Msg_N ("ambiguous default subprogram", Def);
4125 Get_Next_Interp (I, It);
4129 if Subp /= Any_Id then
4131 -- Subprogram found, generate reference to it
4133 Set_Entity (Def, Subp);
4134 Generate_Reference (Subp, Def);
4137 Error_Msg_N ("premature usage of formal subprogram", Def);
4139 elsif Ekind (Subp) /= E_Operator then
4140 Check_Mode_Conformant (Subp, Nam);
4144 Error_Msg_N ("no visible subprogram matches specification", N);
4148 -- When extensions are enabled, an expression can be given as default
4149 -- for a formal function. The expression must be of the function result
4150 -- type and can reference formal parameters of the function.
4152 elsif Present (Expr) then
4154 Install_Formals (Nam);
4155 Preanalyze_Spec_Expression (Expr, Etype (Nam));
4160 Analyze_Aspect_Specifications (N, Nam);
4162 if Parent_Installed then
4165 end Analyze_Formal_Subprogram_Declaration;
4167 -------------------------------------
4168 -- Analyze_Formal_Type_Declaration --
4169 -------------------------------------
4171 procedure Analyze_Formal_Type_Declaration (N : Node_Id) is
4172 Def : constant Node_Id := Formal_Type_Definition (N);
4174 Parent_Installed : Boolean := False;
4178 T := Defining_Identifier (N);
4180 if Present (Discriminant_Specifications (N))
4181 and then Nkind (Def) /= N_Formal_Private_Type_Definition
4184 ("discriminants not allowed for this formal type", T);
4187 Check_Abbreviated_Instance (Parent (N), Parent_Installed);
4189 -- Enter the new name, and branch to specific routine
4192 when N_Formal_Private_Type_Definition =>
4193 Analyze_Formal_Private_Type (N, T, Def);
4195 when N_Formal_Derived_Type_Definition =>
4196 Analyze_Formal_Derived_Type (N, T, Def);
4198 when N_Formal_Incomplete_Type_Definition =>
4199 Analyze_Formal_Incomplete_Type (T, Def);
4201 when N_Formal_Discrete_Type_Definition =>
4202 Analyze_Formal_Discrete_Type (T, Def);
4204 when N_Formal_Signed_Integer_Type_Definition =>
4205 Analyze_Formal_Signed_Integer_Type (T, Def);
4207 when N_Formal_Modular_Type_Definition =>
4208 Analyze_Formal_Modular_Type (T, Def);
4210 when N_Formal_Floating_Point_Definition =>
4211 Analyze_Formal_Floating_Type (T, Def);
4213 when N_Formal_Ordinary_Fixed_Point_Definition =>
4214 Analyze_Formal_Ordinary_Fixed_Point_Type (T, Def);
4216 when N_Formal_Decimal_Fixed_Point_Definition =>
4217 Analyze_Formal_Decimal_Fixed_Point_Type (T, Def);
4219 when N_Array_Type_Definition =>
4220 Analyze_Formal_Array_Type (T, Def);
4222 when N_Access_Function_Definition
4223 | N_Access_Procedure_Definition
4224 | N_Access_To_Object_Definition
4226 Analyze_Generic_Access_Type (T, Def);
4228 -- Ada 2005: a interface declaration is encoded as an abstract
4229 -- record declaration or a abstract type derivation.
4231 when N_Record_Definition =>
4232 Analyze_Formal_Interface_Type (N, T, Def);
4234 when N_Derived_Type_Definition =>
4235 Analyze_Formal_Derived_Interface_Type (N, T, Def);
4241 raise Program_Error;
4244 -- A formal type declaration declares a type and its first
4247 Set_Is_Generic_Type (T);
4248 Set_Is_First_Subtype (T);
4250 if Present (Default_Subtype_Mark (Original_Node (N))) then
4251 Validate_Formal_Type_Default (N);
4254 Analyze_Aspect_Specifications (N, T);
4256 if Parent_Installed then
4259 end Analyze_Formal_Type_Declaration;
4261 ------------------------------------
4262 -- Analyze_Function_Instantiation --
4263 ------------------------------------
4265 procedure Analyze_Function_Instantiation (N : Node_Id) is
4267 Analyze_Subprogram_Instantiation (N, E_Function);
4268 end Analyze_Function_Instantiation;
4270 ---------------------------------
4271 -- Analyze_Generic_Access_Type --
4272 ---------------------------------
4274 procedure Analyze_Generic_Access_Type (T : Entity_Id; Def : Node_Id) is
4278 if Nkind (Def) = N_Access_To_Object_Definition then
4279 Access_Type_Declaration (T, Def);
4281 if Is_Incomplete_Or_Private_Type (Designated_Type (T))
4282 and then No (Full_View (Designated_Type (T)))
4283 and then not Is_Generic_Type (Designated_Type (T))
4285 Error_Msg_N ("premature usage of incomplete type", Def);
4287 elsif not Is_Entity_Name (Subtype_Indication (Def)) then
4289 ("only a subtype mark is allowed in a formal", Def);
4293 Access_Subprogram_Declaration (T, Def);
4295 end Analyze_Generic_Access_Type;
4297 ---------------------------------
4298 -- Analyze_Generic_Formal_Part --
4299 ---------------------------------
4301 procedure Analyze_Generic_Formal_Part (N : Node_Id) is
4302 Gen_Parm_Decl : Node_Id;
4305 -- The generic formals are processed in the scope of the generic unit,
4306 -- where they are immediately visible. The scope is installed by the
4309 Gen_Parm_Decl := First (Generic_Formal_Declarations (N));
4310 while Present (Gen_Parm_Decl) loop
4311 Analyze (Gen_Parm_Decl);
4312 Next (Gen_Parm_Decl);
4315 Generate_Reference_To_Generic_Formals (Current_Scope);
4317 -- For Ada 2022, some formal parameters can carry aspects, which must
4318 -- be name-resolved at the end of the list of formal parameters (which
4319 -- has the semantics of a declaration list).
4321 Analyze_Contracts (Generic_Formal_Declarations (N));
4322 end Analyze_Generic_Formal_Part;
4324 ------------------------------------------
4325 -- Analyze_Generic_Package_Declaration --
4326 ------------------------------------------
4328 procedure Analyze_Generic_Package_Declaration (N : Node_Id) is
4329 Decls : constant List_Id := Visible_Declarations (Specification (N));
4330 Loc : constant Source_Ptr := Sloc (N);
4336 Save_Parent : Node_Id;
4339 -- A generic may grant access to its private enclosing context depending
4340 -- on the placement of its corresponding body. From elaboration point of
4341 -- view, the flow of execution may enter this private context, and then
4342 -- reach an external unit, thus producing a dependency on that external
4343 -- unit. For such a path to be properly discovered and encoded in the
4344 -- ALI file of the main unit, let the ABE mechanism process the body of
4345 -- the main unit, and encode all relevant invocation constructs and the
4346 -- relations between them.
4348 Mark_Save_Invocation_Graph_Of_Body;
4350 -- We introduce a renaming of the enclosing package, to have a usable
4351 -- entity as the prefix of an expanded name for a local entity of the
4352 -- form Par.P.Q, where P is the generic package. This is because a local
4353 -- entity named P may hide it, so that the usual visibility rules in
4354 -- the instance will not resolve properly.
4357 Make_Package_Renaming_Declaration (Loc,
4358 Defining_Unit_Name =>
4359 Make_Defining_Identifier (Loc,
4360 Chars => New_External_Name (Chars (Defining_Entity (N)), "GH")),
4362 Make_Identifier (Loc, Chars (Defining_Entity (N))));
4364 -- The declaration is inserted before other declarations, but before
4365 -- pragmas that may be library-unit pragmas and must appear before other
4366 -- declarations. The pragma Compile_Time_Error is not in this class, and
4367 -- may contain an expression that includes such a qualified name, so the
4368 -- renaming declaration must appear before it.
4370 -- Are there other pragmas that require this special handling ???
4372 if Present (Decls) then
4373 Decl := First (Decls);
4374 while Present (Decl)
4375 and then Nkind (Decl) = N_Pragma
4376 and then Get_Pragma_Id (Decl) /= Pragma_Compile_Time_Error
4381 if Present (Decl) then
4382 Insert_Before (Decl, Renaming);
4384 Append (Renaming, Visible_Declarations (Specification (N)));
4388 Set_Visible_Declarations (Specification (N), New_List (Renaming));
4391 -- Create copy of generic unit, and save for instantiation. If the unit
4392 -- is a child unit, do not copy the specifications for the parent, which
4393 -- are not part of the generic tree.
4395 Save_Parent := Parent_Spec (N);
4396 Set_Parent_Spec (N, Empty);
4398 New_N := Copy_Generic_Node (N, Empty, Instantiating => False);
4399 Set_Parent_Spec (New_N, Save_Parent);
4402 -- Collect all contract-related source pragmas found within the template
4403 -- and attach them to the contract of the package spec. This contract is
4404 -- used in the capture of global references within annotations.
4406 Create_Generic_Contract (N);
4408 Id := Defining_Entity (N);
4409 Generate_Definition (Id);
4411 -- Expansion is not applied to generic units
4416 Mutate_Ekind (Id, E_Generic_Package);
4417 Set_Is_Not_Self_Hidden (Id);
4418 Set_Etype (Id, Standard_Void_Type);
4420 -- Set SPARK_Mode from context
4422 Set_SPARK_Pragma (Id, SPARK_Mode_Pragma);
4423 Set_SPARK_Aux_Pragma (Id, SPARK_Mode_Pragma);
4424 Set_SPARK_Pragma_Inherited (Id);
4425 Set_SPARK_Aux_Pragma_Inherited (Id);
4427 -- Preserve relevant elaboration-related attributes of the context which
4428 -- are no longer available or very expensive to recompute once analysis,
4429 -- resolution, and expansion are over.
4431 Mark_Elaboration_Attributes
4436 -- Analyze aspects now, so that generated pragmas appear in the
4437 -- declarations before building and analyzing the generic copy.
4439 Analyze_Aspect_Specifications (N, Id);
4442 Enter_Generic_Scope (Id);
4443 Set_Inner_Instances (Id, New_Elmt_List);
4445 Set_Categorization_From_Pragmas (N);
4446 Set_Is_Pure (Id, Is_Pure (Current_Scope));
4448 -- Link the declaration of the generic homonym in the generic copy to
4449 -- the package it renames, so that it is always resolved properly.
4451 Set_Generic_Homonym (Id, Defining_Unit_Name (Renaming));
4452 Set_Entity (Associated_Node (Name (Renaming)), Id);
4454 -- For a library unit, we have reconstructed the entity for the unit,
4455 -- and must reset it in the library tables.
4457 if Nkind (Parent (N)) = N_Compilation_Unit then
4458 Set_Cunit_Entity (Current_Sem_Unit, Id);
4461 Analyze_Generic_Formal_Part (N);
4463 -- After processing the generic formals, analysis proceeds as for a
4464 -- non-generic package.
4466 Analyze (Specification (N));
4468 Validate_Categorization_Dependency (N, Id);
4472 End_Package_Scope (Id);
4473 Exit_Generic_Scope (Id);
4475 -- If the generic appears within a package unit, the body of that unit
4476 -- has to be present for instantiation and inlining.
4478 if Nkind (Unit (Cunit (Current_Sem_Unit))) = N_Package_Declaration then
4479 Set_Body_Needed_For_Inlining
4480 (Defining_Entity (Unit (Cunit (Current_Sem_Unit))));
4483 if Nkind (Parent (N)) /= N_Compilation_Unit then
4484 Move_Freeze_Nodes (Id, N, Visible_Declarations (Specification (N)));
4485 Move_Freeze_Nodes (Id, N, Private_Declarations (Specification (N)));
4486 Move_Freeze_Nodes (Id, N, Generic_Formal_Declarations (N));
4489 Set_Body_Required (Parent (N), Unit_Requires_Body (Id));
4490 Validate_RT_RAT_Component (N);
4492 -- If this is a spec without a body, check that generic parameters
4495 if not Body_Required (Parent (N)) then
4496 Check_References (Id);
4500 -- If there is a specified storage pool in the context, create an
4501 -- aspect on the package declaration, so that it is used in any
4502 -- instance that does not override it.
4504 if Present (Default_Pool) then
4510 Make_Aspect_Specification (Loc,
4511 Identifier => Make_Identifier (Loc, Name_Default_Storage_Pool),
4512 Expression => New_Copy (Default_Pool));
4514 if No (Aspect_Specifications (Specification (N))) then
4515 Set_Aspect_Specifications (Specification (N), New_List (ASN));
4517 Append (ASN, Aspect_Specifications (Specification (N)));
4521 end Analyze_Generic_Package_Declaration;
4523 --------------------------------------------
4524 -- Analyze_Generic_Subprogram_Declaration --
4525 --------------------------------------------
4527 procedure Analyze_Generic_Subprogram_Declaration (N : Node_Id) is
4531 Result_Type : Entity_Id;
4532 Save_Parent : Node_Id;
4537 -- A generic may grant access to its private enclosing context depending
4538 -- on the placement of its corresponding body. From elaboration point of
4539 -- view, the flow of execution may enter this private context, and then
4540 -- reach an external unit, thus producing a dependency on that external
4541 -- unit. For such a path to be properly discovered and encoded in the
4542 -- ALI file of the main unit, let the ABE mechanism process the body of
4543 -- the main unit, and encode all relevant invocation constructs and the
4544 -- relations between them.
4546 Mark_Save_Invocation_Graph_Of_Body;
4548 -- Create copy of generic unit, and save for instantiation. If the unit
4549 -- is a child unit, do not copy the specifications for the parent, which
4550 -- are not part of the generic tree.
4552 Save_Parent := Parent_Spec (N);
4553 Set_Parent_Spec (N, Empty);
4555 New_N := Copy_Generic_Node (N, Empty, Instantiating => False);
4556 Set_Parent_Spec (New_N, Save_Parent);
4559 -- Collect all contract-related source pragmas found within the template
4560 -- and attach them to the contract of the subprogram spec. This contract
4561 -- is used in the capture of global references within annotations.
4563 Create_Generic_Contract (N);
4565 Spec := Specification (N);
4566 Id := Defining_Entity (Spec);
4567 Generate_Definition (Id);
4569 if Nkind (Id) = N_Defining_Operator_Symbol then
4571 ("operator symbol not allowed for generic subprogram", Id);
4577 Set_Scope_Depth_Value (Id, Scope_Depth (Current_Scope) + 1);
4580 Enter_Generic_Scope (Id);
4581 Set_Inner_Instances (Id, New_Elmt_List);
4582 Set_Is_Pure (Id, Is_Pure (Current_Scope));
4584 Analyze_Generic_Formal_Part (N);
4586 if Nkind (Spec) = N_Function_Specification then
4587 Mutate_Ekind (Id, E_Generic_Function);
4589 Mutate_Ekind (Id, E_Generic_Procedure);
4592 -- Set SPARK_Mode from context
4594 Set_SPARK_Pragma (Id, SPARK_Mode_Pragma);
4595 Set_SPARK_Pragma_Inherited (Id);
4597 -- Preserve relevant elaboration-related attributes of the context which
4598 -- are no longer available or very expensive to recompute once analysis,
4599 -- resolution, and expansion are over.
4601 Mark_Elaboration_Attributes
4606 Formals := Parameter_Specifications (Spec);
4608 if Present (Formals) then
4609 Process_Formals (Formals, Spec);
4612 if Nkind (Spec) = N_Function_Specification then
4613 if Nkind (Result_Definition (Spec)) = N_Access_Definition then
4614 Result_Type := Access_Definition (Spec, Result_Definition (Spec));
4615 Set_Etype (Id, Result_Type);
4617 -- Check restriction imposed by AI05-073: a generic function
4618 -- cannot return an abstract type or an access to such.
4620 if Is_Abstract_Type (Designated_Type (Result_Type)) then
4622 ("generic function cannot have an access result "
4623 & "that designates an abstract type", Spec);
4627 Find_Type (Result_Definition (Spec));
4628 Typ := Entity (Result_Definition (Spec));
4630 if Is_Abstract_Type (Typ)
4631 and then Ada_Version >= Ada_2012
4634 ("generic function cannot have abstract result type", Spec);
4637 -- If a null exclusion is imposed on the result type, then create
4638 -- a null-excluding itype (an access subtype) and use it as the
4639 -- function's Etype.
4641 if Is_Access_Type (Typ)
4642 and then Null_Exclusion_Present (Spec)
4645 Create_Null_Excluding_Itype
4647 Related_Nod => Spec,
4648 Scope_Id => Defining_Unit_Name (Spec)));
4650 Set_Etype (Id, Typ);
4655 Set_Etype (Id, Standard_Void_Type);
4658 Set_Is_Not_Self_Hidden (Id);
4660 -- Analyze the aspects of the generic copy to ensure that all generated
4661 -- pragmas (if any) perform their semantic effects.
4663 Analyze_Aspect_Specifications (N, Id);
4665 -- For a library unit, we have reconstructed the entity for the unit,
4666 -- and must reset it in the library tables. We also make sure that
4667 -- Body_Required is set properly in the original compilation unit node.
4669 if Nkind (Parent (N)) = N_Compilation_Unit then
4670 Set_Cunit_Entity (Current_Sem_Unit, Id);
4671 Set_Body_Required (Parent (N), Unit_Requires_Body (Id));
4674 -- If the generic appears within a package unit, the body of that unit
4675 -- has to be present for instantiation and inlining.
4677 if Nkind (Unit (Cunit (Current_Sem_Unit))) = N_Package_Declaration
4678 and then Unit_Requires_Body (Id)
4680 Set_Body_Needed_For_Inlining
4681 (Defining_Entity (Unit (Cunit (Current_Sem_Unit))));
4684 Set_Categorization_From_Pragmas (N);
4685 Validate_Categorization_Dependency (N, Id);
4687 -- Capture all global references that occur within the profile of the
4688 -- generic subprogram. Aspects are not part of this processing because
4689 -- they must be delayed. If processed now, Save_Global_References will
4690 -- destroy the Associated_Node links and prevent the capture of global
4691 -- references when the contract of the generic subprogram is analyzed.
4693 Save_Global_References (Original_Node (N));
4697 Exit_Generic_Scope (Id);
4698 Generate_Reference_To_Formals (Id);
4700 List_Inherited_Pre_Post_Aspects (Id);
4701 end Analyze_Generic_Subprogram_Declaration;
4703 -----------------------------------
4704 -- Analyze_Package_Instantiation --
4705 -----------------------------------
4707 -- WARNING: This routine manages Ghost and SPARK regions. Return statements
4708 -- must be replaced by gotos which jump to the end of the routine in order
4709 -- to restore the Ghost and SPARK modes.
4711 procedure Analyze_Package_Instantiation (N : Node_Id) is
4712 Has_Inline_Always : Boolean := False;
4713 -- Set if the generic unit contains any subprograms with Inline_Always.
4714 -- Only relevant when back-end inlining is not enabled.
4716 function Might_Inline_Subp (Gen_Unit : Entity_Id) return Boolean;
4717 -- Return True if inlining is active and Gen_Unit contains inlined
4718 -- subprograms. In this case, we may either instantiate the body when
4719 -- front-end inlining is enabled, or add a pending instantiation when
4720 -- back-end inlining is enabled. In the former case, this may cause
4721 -- superfluous instantiations, but in either case we need to perform
4722 -- the instantiation of the body in the context of the instance and
4723 -- not in that of the point of inlining.
4725 function Needs_Body_Instantiated (Gen_Unit : Entity_Id) return Boolean;
4726 -- Return True if Gen_Unit needs to have its body instantiated in the
4727 -- context of N. This in particular excludes generic contexts.
4729 -----------------------
4730 -- Might_Inline_Subp --
4731 -----------------------
4733 function Might_Inline_Subp (Gen_Unit : Entity_Id) return Boolean is
4737 if Inline_Processing_Required then
4738 -- No need to recompute the answer if we know it is positive
4739 -- and back-end inlining is enabled.
4741 if Is_Inlined (Gen_Unit) and then Back_End_Inlining then
4745 E := First_Entity (Gen_Unit);
4746 while Present (E) loop
4747 if Is_Subprogram (E) and then Is_Inlined (E) then
4748 -- Remember if there are any subprograms with Inline_Always
4750 if Has_Pragma_Inline_Always (E) then
4751 Has_Inline_Always := True;
4754 Set_Is_Inlined (Gen_Unit);
4763 end Might_Inline_Subp;
4765 -------------------------------
4766 -- Needs_Body_Instantiated --
4767 -------------------------------
4769 function Needs_Body_Instantiated (Gen_Unit : Entity_Id) return Boolean is
4771 -- No need to instantiate bodies in generic units
4773 if Is_Generic_Unit (Cunit_Entity (Main_Unit)) then
4777 -- If the instantiation is in the main unit, then the body is needed
4779 if Is_In_Main_Unit (N) then
4783 -- In GNATprove mode, never instantiate bodies outside of the main
4784 -- unit, as it does not use frontend/backend inlining in the way that
4785 -- GNAT does, so does not benefit from such instantiations. On the
4786 -- contrary, such instantiations may bring artificial constraints,
4787 -- as for example such bodies may require preprocessing.
4789 if GNATprove_Mode then
4793 -- If not, then again no need to instantiate bodies in generic units
4795 if Is_Generic_Unit (Cunit_Entity (Get_Code_Unit (N))) then
4799 -- Here we have a special handling for back-end inlining: if inline
4800 -- processing is required, then we unconditionally want to have the
4801 -- body instantiated. The reason is that Might_Inline_Subp does not
4802 -- catch all the cases (as it does not recurse into nested packages)
4803 -- so this avoids the need to patch things up afterwards. Moreover,
4804 -- these instantiations are only performed on demand when back-end
4805 -- inlining is enabled, so this causes very little extra work.
4807 if Inline_Processing_Required and then Back_End_Inlining then
4811 -- We want to have the bodies instantiated in non-main units if
4812 -- they might contribute inlined subprograms.
4814 return Might_Inline_Subp (Gen_Unit);
4815 end Needs_Body_Instantiated;
4817 -- Local declarations
4819 Gen_Id : constant Node_Id := Name (N);
4820 Inst_Id : constant Entity_Id := Defining_Entity (N);
4821 Is_Actual_Pack : constant Boolean := Is_Internal (Inst_Id);
4822 Loc : constant Source_Ptr := Sloc (N);
4824 Saved_GM : constant Ghost_Mode_Type := Ghost_Mode;
4825 Saved_IGR : constant Node_Id := Ignored_Ghost_Region;
4826 Saved_ISMP : constant Boolean :=
4827 Ignore_SPARK_Mode_Pragmas_In_Instance;
4828 Saved_SM : constant SPARK_Mode_Type := SPARK_Mode;
4829 Saved_SMP : constant Node_Id := SPARK_Mode_Pragma;
4830 -- Save the Ghost and SPARK mode-related data to restore on exit
4832 Saved_Style_Check : constant Boolean := Style_Check;
4833 -- Save style check mode for restore on exit
4836 Act_Decl_Name : Node_Id;
4837 Act_Decl_Id : Entity_Id;
4840 Env_Installed : Boolean := False;
4843 Gen_Unit : Entity_Id;
4844 Inline_Now : Boolean := False;
4845 Needs_Body : Boolean;
4846 Parent_Installed : Boolean := False;
4847 Renamings : List_Id;
4848 Unit_Renaming : Node_Id;
4850 Vis_Prims_List : Elist_Id := No_Elist;
4851 -- List of primitives made temporarily visible in the instantiation
4852 -- to match the visibility of the formal type
4854 -- Start of processing for Analyze_Package_Instantiation
4857 -- Preserve relevant elaboration-related attributes of the context which
4858 -- are no longer available or very expensive to recompute once analysis,
4859 -- resolution, and expansion are over.
4861 Mark_Elaboration_Attributes
4868 -- Very first thing: check for Text_IO special unit in case we are
4869 -- instantiating one of the children of [[Wide_]Wide_]Text_IO.
4871 Check_Text_IO_Special_Unit (Name (N));
4873 -- Make node global for error reporting
4875 Instantiation_Node := N;
4877 -- Case of instantiation of a generic package
4879 if Nkind (N) = N_Package_Instantiation then
4880 Act_Decl_Id := New_Copy (Defining_Entity (N));
4882 if Nkind (Defining_Unit_Name (N)) = N_Defining_Program_Unit_Name then
4884 Make_Defining_Program_Unit_Name (Loc,
4886 New_Copy_Tree (Name (Defining_Unit_Name (N))),
4887 Defining_Identifier => Act_Decl_Id);
4889 Act_Decl_Name := Act_Decl_Id;
4892 -- Case of instantiation of a formal package
4895 Act_Decl_Id := Defining_Identifier (N);
4896 Act_Decl_Name := Act_Decl_Id;
4899 Generate_Definition (Act_Decl_Id);
4900 Mutate_Ekind (Act_Decl_Id, E_Package);
4901 Set_Is_Not_Self_Hidden (Act_Decl_Id);
4903 -- Initialize list of incomplete actuals before analysis
4905 Set_Incomplete_Actuals (Act_Decl_Id, New_Elmt_List);
4907 Preanalyze_Actuals (N, Act_Decl_Id);
4909 -- Turn off style checking in instances. If the check is enabled on the
4910 -- generic unit, a warning in an instance would just be noise. If not
4911 -- enabled on the generic, then a warning in an instance is just wrong.
4912 -- This must be done after analyzing the actuals, which do come from
4913 -- source and are subject to style checking.
4915 Style_Check := False;
4918 Env_Installed := True;
4920 -- Reset renaming map for formal types. The mapping is established
4921 -- when analyzing the generic associations, but some mappings are
4922 -- inherited from formal packages of parent units, and these are
4923 -- constructed when the parents are installed.
4925 Generic_Renamings.Set_Last (0);
4926 Generic_Renamings_HTable.Reset;
4928 -- Except for an abbreviated instance created to check a formal package,
4929 -- install the parent if this is a generic child unit.
4931 if not Is_Abbreviated_Instance (Inst_Id) then
4932 Check_Generic_Child_Unit (Gen_Id, Parent_Installed);
4935 Gen_Unit := Entity (Gen_Id);
4937 -- A package instantiation is Ghost when it is subject to pragma Ghost
4938 -- or the generic template is Ghost. Set the mode now to ensure that
4939 -- any nodes generated during analysis and expansion are marked as
4942 Mark_And_Set_Ghost_Instantiation (N, Gen_Unit);
4944 -- Verify that it is the name of a generic package
4946 -- A visibility glitch: if the instance is a child unit and the generic
4947 -- is the generic unit of a parent instance (i.e. both the parent and
4948 -- the child units are instances of the same package) the name now
4949 -- denotes the renaming within the parent, not the intended generic
4950 -- unit. See if there is a homonym that is the desired generic. The
4951 -- renaming declaration must be visible inside the instance of the
4952 -- child, but not when analyzing the name in the instantiation itself.
4954 if Ekind (Gen_Unit) = E_Package
4955 and then Present (Renamed_Entity (Gen_Unit))
4956 and then In_Open_Scopes (Renamed_Entity (Gen_Unit))
4957 and then Is_Generic_Instance (Renamed_Entity (Gen_Unit))
4958 and then Present (Homonym (Gen_Unit))
4960 Gen_Unit := Homonym (Gen_Unit);
4963 if Etype (Gen_Unit) = Any_Type then
4967 elsif Ekind (Gen_Unit) /= E_Generic_Package then
4969 -- Ada 2005 (AI-50217): Cannot use instance in limited with_clause
4971 if From_Limited_With (Gen_Unit) then
4973 ("cannot instantiate a limited withed package", Gen_Id);
4976 ("& is not the name of a generic package", Gen_Id, Gen_Unit);
4983 if In_Extended_Main_Source_Unit (N) then
4984 Set_Is_Instantiated (Gen_Unit);
4985 Generate_Reference (Gen_Unit, N);
4987 if Present (Renamed_Entity (Gen_Unit)) then
4988 Set_Is_Instantiated (Renamed_Entity (Gen_Unit));
4989 Generate_Reference (Renamed_Entity (Gen_Unit), N);
4993 if Nkind (Gen_Id) = N_Identifier
4994 and then Chars (Gen_Unit) = Chars (Defining_Entity (N))
4997 ("& is hidden within declaration of instance", Gen_Id, Gen_Unit);
4999 elsif Nkind (Gen_Id) = N_Expanded_Name
5000 and then Is_Child_Unit (Gen_Unit)
5001 and then Nkind (Prefix (Gen_Id)) = N_Identifier
5002 and then Chars (Act_Decl_Id) = Chars (Prefix (Gen_Id))
5005 ("& is hidden within declaration of instance", Prefix (Gen_Id));
5008 Set_Entity (Gen_Id, Gen_Unit);
5010 -- If generic is a renaming, get original generic unit
5012 if Present (Renamed_Entity (Gen_Unit))
5013 and then Ekind (Renamed_Entity (Gen_Unit)) = E_Generic_Package
5015 Gen_Unit := Renamed_Entity (Gen_Unit);
5018 -- Verify that there are no circular instantiations
5020 if In_Open_Scopes (Gen_Unit) then
5021 Error_Msg_NE ("instantiation of & within itself", N, Gen_Unit);
5025 elsif Contains_Instance_Of (Gen_Unit, Current_Scope, Gen_Id) then
5026 Error_Msg_Node_2 := Current_Scope;
5028 ("circular instantiation: & instantiated in &!", N, Gen_Unit);
5029 Circularity_Detected := True;
5034 Mutate_Ekind (Inst_Id, E_Package);
5035 Set_Scope (Inst_Id, Current_Scope);
5037 -- If the context of the instance is subject to SPARK_Mode "off" or
5038 -- the annotation is altogether missing, set the global flag which
5039 -- signals Analyze_Pragma to ignore all SPARK_Mode pragmas within
5042 if SPARK_Mode /= On then
5043 Ignore_SPARK_Mode_Pragmas_In_Instance := True;
5045 -- Mark the instance spec in case the body is instantiated at a
5046 -- later pass. This preserves the original context in effect for
5049 Set_Ignore_SPARK_Mode_Pragmas (Act_Decl_Id);
5052 Gen_Decl := Unit_Declaration_Node (Gen_Unit);
5053 Gen_Spec := Specification (Gen_Decl);
5055 -- Initialize renamings map, for error checking, and the list that
5056 -- holds private entities whose views have changed between generic
5057 -- definition and instantiation. If this is the instance created to
5058 -- validate an actual package, the instantiation environment is that
5059 -- of the enclosing instance.
5061 Create_Instantiation_Source (N, Gen_Unit, S_Adjustment);
5063 -- Copy original generic tree, to produce text for instantiation
5067 (Original_Node (Gen_Decl), Empty, Instantiating => True);
5069 Act_Spec := Specification (Act_Tree);
5071 -- If this is the instance created to validate an actual package,
5072 -- only the formals matter, do not examine the package spec itself.
5074 if Is_Actual_Pack then
5075 Set_Visible_Declarations (Act_Spec, New_List);
5076 Set_Private_Declarations (Act_Spec, New_List);
5080 Analyze_Associations
5082 Formals => Generic_Formal_Declarations (Act_Tree),
5083 F_Copy => Generic_Formal_Declarations (Gen_Decl));
5085 Vis_Prims_List := Check_Hidden_Primitives (Renamings);
5087 Set_Instance_Env (Gen_Unit, Act_Decl_Id);
5088 Set_Defining_Unit_Name (Act_Spec, Act_Decl_Name);
5089 Set_Is_Generic_Instance (Act_Decl_Id);
5090 Set_Generic_Parent (Act_Spec, Gen_Unit);
5092 -- References to the generic in its own declaration or its body are
5093 -- references to the instance. Add a renaming declaration for the
5094 -- generic unit itself. This declaration, as well as the renaming
5095 -- declarations for the generic formals, must remain private to the
5096 -- unit: the formals, because this is the language semantics, and
5097 -- the unit because its use is an artifact of the implementation.
5100 Make_Package_Renaming_Declaration (Loc,
5101 Defining_Unit_Name =>
5102 Make_Defining_Identifier (Loc, Chars (Gen_Unit)),
5103 Name => New_Occurrence_Of (Act_Decl_Id, Loc));
5105 Append (Unit_Renaming, Renamings);
5107 -- The renaming declarations are the first local declarations of the
5110 if Is_Non_Empty_List (Visible_Declarations (Act_Spec)) then
5112 (First (Visible_Declarations (Act_Spec)), Renamings);
5114 Set_Visible_Declarations (Act_Spec, Renamings);
5117 Act_Decl := Make_Package_Declaration (Loc, Specification => Act_Spec);
5119 -- Propagate the aspect specifications from the package declaration
5120 -- template to the instantiated version of the package declaration.
5122 if Has_Aspects (Act_Tree) then
5123 Set_Aspect_Specifications (Act_Decl,
5124 New_Copy_List_Tree (Aspect_Specifications (Act_Tree)));
5127 -- The generic may have a generated Default_Storage_Pool aspect,
5128 -- set at the point of generic declaration. If the instance has
5129 -- that aspect, it overrides the one inherited from the generic.
5131 if Has_Aspects (Gen_Spec) then
5132 if No (Aspect_Specifications (N)) then
5133 Set_Aspect_Specifications (N,
5135 (Aspect_Specifications (Gen_Spec))));
5139 Inherited_Aspects : constant List_Id :=
5141 (Aspect_Specifications (Gen_Spec));
5145 Pool_Present : Boolean := False;
5148 ASN1 := First (Aspect_Specifications (N));
5149 while Present (ASN1) loop
5150 if Chars (Identifier (ASN1)) =
5151 Name_Default_Storage_Pool
5153 Pool_Present := True;
5160 if Pool_Present then
5162 -- If generic carries a default storage pool, remove it
5163 -- in favor of the instance one.
5165 ASN2 := First (Inherited_Aspects);
5166 while Present (ASN2) loop
5167 if Chars (Identifier (ASN2)) =
5168 Name_Default_Storage_Pool
5179 (Aspect_Specifications (N), Inherited_Aspects);
5184 -- Save the instantiation node for a subsequent instantiation of the
5185 -- body if there is one and it needs to be instantiated here.
5187 -- We instantiate the body only if we are generating code, or if we
5188 -- are generating cross-reference information, or for GNATprove use.
5191 Enclosing_Body_Present : Boolean := False;
5192 -- If the generic unit is not a compilation unit, then a body may
5193 -- be present in its parent even if none is required. We create a
5194 -- tentative pending instantiation for the body, which will be
5195 -- discarded if none is actually present.
5200 if Scope (Gen_Unit) /= Standard_Standard
5201 and then not Is_Child_Unit (Gen_Unit)
5203 Scop := Scope (Gen_Unit);
5204 while Present (Scop) and then Scop /= Standard_Standard loop
5205 if Unit_Requires_Body (Scop) then
5206 Enclosing_Body_Present := True;
5209 elsif In_Open_Scopes (Scop)
5210 and then In_Package_Body (Scop)
5212 Enclosing_Body_Present := True;
5216 exit when Is_Compilation_Unit (Scop);
5217 Scop := Scope (Scop);
5221 -- If front-end inlining is enabled or there are any subprograms
5222 -- marked with Inline_Always, and this is a unit for which code
5223 -- will be generated, we instantiate the body at once.
5225 -- This is done if the instance is not the main unit, and if the
5226 -- generic is not a child unit of another generic, to avoid scope
5227 -- problems and the reinstallation of parent instances.
5230 and then (not Is_Child_Unit (Gen_Unit)
5231 or else not Is_Generic_Unit (Scope (Gen_Unit)))
5232 and then Might_Inline_Subp (Gen_Unit)
5233 and then not Is_Actual_Pack
5235 if not Back_End_Inlining
5236 and then (Front_End_Inlining or else Has_Inline_Always)
5237 and then (Is_In_Main_Unit (N)
5238 or else In_Main_Context (Current_Scope))
5239 and then Nkind (Parent (N)) /= N_Compilation_Unit
5243 -- In configurable_run_time mode we force the inlining of
5244 -- predefined subprograms marked Inline_Always, to minimize
5245 -- the use of the run-time library.
5247 elsif In_Predefined_Unit (Gen_Decl)
5248 and then Configurable_Run_Time_Mode
5249 and then Nkind (Parent (N)) /= N_Compilation_Unit
5254 -- If the current scope is itself an instance within a child
5255 -- unit, there will be duplications in the scope stack, and the
5256 -- unstacking mechanism in Inline_Instance_Body will fail.
5257 -- This loses some rare cases of optimization.
5259 if Is_Generic_Instance (Current_Scope) then
5261 Curr_Unit : constant Entity_Id :=
5262 Cunit_Entity (Current_Sem_Unit);
5264 if Curr_Unit /= Current_Scope
5265 and then Is_Child_Unit (Curr_Unit)
5267 Inline_Now := False;
5274 (Unit_Requires_Body (Gen_Unit)
5275 or else Enclosing_Body_Present
5276 or else Present (Corresponding_Body (Gen_Decl)))
5277 and then Needs_Body_Instantiated (Gen_Unit)
5278 and then not Is_Actual_Pack
5279 and then not Inline_Now
5280 and then (Operating_Mode = Generate_Code
5281 or else (Operating_Mode = Check_Semantics
5282 and then GNATprove_Mode));
5284 -- If front-end inlining is enabled or there are any subprograms
5285 -- marked with Inline_Always, do not instantiate body when within
5286 -- a generic context.
5288 if not Back_End_Inlining
5289 and then (Front_End_Inlining or else Has_Inline_Always)
5290 and then not Expander_Active
5292 Needs_Body := False;
5295 -- If the current context is generic, and the package being
5296 -- instantiated is declared within a formal package, there is no
5297 -- body to instantiate until the enclosing generic is instantiated
5298 -- and there is an actual for the formal package. If the formal
5299 -- package has parameters, we build a regular package instance for
5300 -- it, that precedes the original formal package declaration.
5302 if In_Open_Scopes (Scope (Scope (Gen_Unit))) then
5304 Decl : constant Node_Id :=
5306 (Unit_Declaration_Node (Scope (Gen_Unit)));
5308 if Nkind (Decl) = N_Formal_Package_Declaration
5309 or else (Nkind (Decl) = N_Package_Declaration
5310 and then Is_List_Member (Decl)
5311 and then Present (Next (Decl))
5313 Nkind (Next (Decl)) =
5314 N_Formal_Package_Declaration)
5316 Needs_Body := False;
5322 -- For RCI unit calling stubs, we omit the instance body if the
5323 -- instance is the RCI library unit itself.
5325 -- However there is a special case for nested instances: in this case
5326 -- we do generate the instance body, as it might be required, e.g.
5327 -- because it provides stream attributes for some type used in the
5328 -- profile of a remote subprogram. This is consistent with 12.3(12),
5329 -- which indicates that the instance body occurs at the place of the
5330 -- instantiation, and thus is part of the RCI declaration, which is
5331 -- present on all client partitions (this is E.2.3(18)).
5333 -- Note that AI12-0002 may make it illegal at some point to have
5334 -- stream attributes defined in an RCI unit, in which case this
5335 -- special case will become unnecessary. In the meantime, there
5336 -- is known application code in production that depends on this
5337 -- being possible, so we definitely cannot eliminate the body in
5338 -- the case of nested instances for the time being.
5340 -- When we generate a nested instance body, calling stubs for any
5341 -- relevant subprogram will be inserted immediately after the
5342 -- subprogram declarations, and will take precedence over the
5343 -- subsequent (original) body. (The stub and original body will be
5344 -- complete homographs, but this is permitted in an instance).
5345 -- (Could we do better and remove the original body???)
5347 if Distribution_Stub_Mode = Generate_Caller_Stub_Body
5348 and then Comes_From_Source (N)
5349 and then Nkind (Parent (N)) = N_Compilation_Unit
5351 Needs_Body := False;
5354 -- If the context requires a full instantiation, set things up for
5355 -- subsequent construction of the body.
5359 Fin_Scop, S : Entity_Id;
5362 Check_Forward_Instantiation (Gen_Decl);
5366 -- For a package instantiation that is not a compilation unit,
5367 -- indicate that cleanup actions of the innermost enclosing
5368 -- scope for which they are generated should be delayed until
5369 -- after the package body is instantiated.
5371 if Nkind (N) = N_Package_Instantiation
5372 and then not Is_Compilation_Unit (Act_Decl_Id)
5376 while S /= Standard_Standard loop
5377 -- Cleanup actions are not generated within generic units
5378 -- or in the formal part of generic units.
5380 if not Expander_Active then
5383 -- For package scopes, cleanup actions are generated only
5384 -- for compilation units, for spec and body separately.
5386 elsif Ekind (S) = E_Package then
5387 if Is_Compilation_Unit (S) then
5388 if In_Package_Body (S) then
5389 Fin_Scop := Body_Entity (S);
5394 Set_Delay_Cleanups (Fin_Scop);
5401 -- Cleanup actions are generated for all dynamic scopes
5405 Set_Delay_Cleanups (Fin_Scop);
5411 Add_Pending_Instantiation (N, Act_Decl, Fin_Scop);
5415 Set_Categorization_From_Pragmas (Act_Decl);
5417 if Parent_Installed then
5421 Set_Instance_Spec (N, Act_Decl);
5423 -- If not a compilation unit, insert the package declaration before
5424 -- the original instantiation node.
5426 if Nkind (Parent (N)) /= N_Compilation_Unit then
5427 Mark_Rewrite_Insertion (Act_Decl);
5428 Insert_Before (N, Act_Decl);
5430 if Has_Aspects (N) then
5431 Analyze_Aspect_Specifications (N, Act_Decl_Id);
5433 -- The pragma created for a Default_Storage_Pool aspect must
5434 -- appear ahead of the declarations in the instance spec.
5435 -- Analysis has placed it after the instance node, so remove
5436 -- it and reinsert it properly now.
5439 ASN : constant Node_Id := First (Aspect_Specifications (N));
5440 A_Name : constant Name_Id := Chars (Identifier (ASN));
5444 if A_Name = Name_Default_Storage_Pool then
5445 if No (Visible_Declarations (Act_Spec)) then
5446 Set_Visible_Declarations (Act_Spec, New_List);
5450 while Present (Decl) loop
5451 if Nkind (Decl) = N_Pragma then
5453 Prepend (Decl, Visible_Declarations (Act_Spec));
5465 -- For an instantiation that is a compilation unit, place
5466 -- declaration on current node so context is complete for analysis
5467 -- (including nested instantiations). If this is the main unit,
5468 -- the declaration eventually replaces the instantiation node.
5469 -- If the instance body is created later, it replaces the
5470 -- instance node, and the declaration is attached to it
5471 -- (see Build_Instance_Compilation_Unit_Nodes).
5474 if Cunit_Entity (Current_Sem_Unit) = Defining_Entity (N) then
5476 -- The entity for the current unit is the newly created one,
5477 -- and all semantic information is attached to it.
5479 Set_Cunit_Entity (Current_Sem_Unit, Act_Decl_Id);
5481 -- If this is the main unit, replace the main entity as well
5483 if Current_Sem_Unit = Main_Unit then
5484 Main_Unit_Entity := Act_Decl_Id;
5488 Set_Unit (Parent (N), Act_Decl);
5489 Set_Parent_Spec (Act_Decl, Parent_Spec (N));
5490 Set_Package_Instantiation (Act_Decl_Id, N);
5492 -- Process aspect specifications of the instance node, if any, to
5493 -- take into account categorization pragmas before analyzing the
5496 Analyze_Aspect_Specifications (N, Act_Decl_Id);
5499 Set_Unit (Parent (N), N);
5500 Set_Body_Required (Parent (N), False);
5502 -- We never need elaboration checks on instantiations, since by
5503 -- definition, the body instantiation is elaborated at the same
5504 -- time as the spec instantiation.
5506 if Legacy_Elaboration_Checks then
5507 Set_Kill_Elaboration_Checks (Act_Decl_Id);
5508 Set_Suppress_Elaboration_Warnings (Act_Decl_Id);
5512 if Legacy_Elaboration_Checks then
5513 Check_Elab_Instantiation (N);
5516 -- Save the scenario for later examination by the ABE Processing
5519 Record_Elaboration_Scenario (N);
5521 -- The instantiation results in a guaranteed ABE
5523 if Is_Known_Guaranteed_ABE (N) and then Needs_Body then
5524 -- Do not instantiate the corresponding body because gigi cannot
5525 -- handle certain types of premature instantiations.
5527 Remove_Dead_Instance (N);
5529 -- Create completing bodies for all subprogram declarations since
5530 -- their real bodies will not be instantiated.
5532 Provide_Completing_Bodies (Instance_Spec (N));
5535 Check_Hidden_Child_Unit (N, Gen_Unit, Act_Decl_Id);
5537 Set_First_Private_Entity (Defining_Unit_Name (Unit_Renaming),
5538 First_Private_Entity (Act_Decl_Id));
5540 -- If the instantiation needs a body, the unit will be turned into
5541 -- a package body and receive its own elaboration entity. Otherwise,
5542 -- the nature of the unit is now a package declaration.
5544 -- Note that the below rewriting means that Act_Decl, which has been
5545 -- analyzed and expanded, will be re-expanded as the rewritten N.
5547 if Nkind (Parent (N)) = N_Compilation_Unit
5548 and then not Needs_Body
5550 Rewrite (N, Act_Decl);
5553 if Present (Corresponding_Body (Gen_Decl))
5554 or else Unit_Requires_Body (Gen_Unit)
5556 Set_Has_Completion (Act_Decl_Id);
5559 Check_Formal_Packages (Act_Decl_Id);
5561 Restore_Hidden_Primitives (Vis_Prims_List);
5562 Restore_Private_Views (Act_Decl_Id);
5564 Inherit_Context (Gen_Decl, N);
5566 if Parent_Installed then
5571 Env_Installed := False;
5574 Validate_Categorization_Dependency (N, Act_Decl_Id);
5576 -- There used to be a check here to prevent instantiations in local
5577 -- contexts if the No_Local_Allocators restriction was active. This
5578 -- check was removed by a binding interpretation in AI-95-00130/07,
5579 -- but we retain the code for documentation purposes.
5581 -- if Ekind (Act_Decl_Id) /= E_Void
5582 -- and then not Is_Library_Level_Entity (Act_Decl_Id)
5584 -- Check_Restriction (No_Local_Allocators, N);
5588 Inline_Instance_Body (N, Gen_Unit, Act_Decl);
5591 -- Check that if N is an instantiation of System.Dim_Float_IO or
5592 -- System.Dim_Integer_IO, the formal type has a dimension system.
5594 if Nkind (N) = N_Package_Instantiation
5595 and then Is_Dim_IO_Package_Instantiation (N)
5598 Assoc : constant Node_Id := First (Generic_Associations (N));
5600 if not Has_Dimension_System
5601 (Etype (Explicit_Generic_Actual_Parameter (Assoc)))
5603 Error_Msg_N ("type with a dimension system expected", Assoc);
5609 if Nkind (Parent (N)) /= N_Compilation_Unit then
5610 Analyze_Aspect_Specifications (N, Act_Decl_Id);
5613 Ignore_SPARK_Mode_Pragmas_In_Instance := Saved_ISMP;
5614 Restore_Ghost_Region (Saved_GM, Saved_IGR);
5615 Restore_SPARK_Mode (Saved_SM, Saved_SMP);
5616 Style_Check := Saved_Style_Check;
5619 when Instantiation_Error =>
5620 if Parent_Installed then
5624 if Env_Installed then
5628 Ignore_SPARK_Mode_Pragmas_In_Instance := Saved_ISMP;
5629 Restore_Ghost_Region (Saved_GM, Saved_IGR);
5630 Restore_SPARK_Mode (Saved_SM, Saved_SMP);
5631 Style_Check := Saved_Style_Check;
5632 end Analyze_Package_Instantiation;
5634 --------------------------
5635 -- Inline_Instance_Body --
5636 --------------------------
5638 -- WARNING: This routine manages SPARK regions. Return statements must be
5639 -- replaced by gotos which jump to the end of the routine and restore the
5642 procedure Inline_Instance_Body
5644 Gen_Unit : Entity_Id;
5647 Config_Attrs : constant Config_Switches_Type := Save_Config_Switches;
5649 Curr_Comp : constant Node_Id := Cunit (Current_Sem_Unit);
5650 Curr_Unit : constant Entity_Id := Cunit_Entity (Current_Sem_Unit);
5651 Gen_Comp : constant Entity_Id :=
5652 Cunit_Entity (Get_Source_Unit (Gen_Unit));
5654 Scope_Stack_Depth : constant Pos :=
5655 Scope_Stack.Last - Scope_Stack.First + 1;
5657 Inner_Scopes : array (1 .. Scope_Stack_Depth) of Entity_Id;
5658 Instances : array (1 .. Scope_Stack_Depth) of Entity_Id;
5659 Use_Clauses : array (1 .. Scope_Stack_Depth) of Node_Id;
5661 Curr_Scope : Entity_Id := Empty;
5662 List : Elist_Id := No_Elist; -- init to avoid warning
5663 N_Instances : Nat := 0;
5664 Num_Inner : Nat := 0;
5665 Num_Scopes : Nat := 0;
5666 Removed : Boolean := False;
5671 -- Case of generic unit defined in another unit. We must remove the
5672 -- complete context of the current unit to install that of the generic.
5674 if Gen_Comp /= Cunit_Entity (Current_Sem_Unit) then
5676 -- Loop through enclosing scopes until we reach a generic instance,
5677 -- package body, or subprogram.
5680 while Present (S) and then S /= Standard_Standard loop
5682 -- Save use clauses from enclosing scopes into Use_Clauses
5685 Num_Scopes := Num_Scopes + 1;
5687 Use_Clauses (Num_Scopes) :=
5689 (Scope_Stack.Last - Num_Scopes + 1).First_Use_Clause);
5690 End_Use_Clauses (Use_Clauses (Num_Scopes));
5692 exit when Scope_Stack.Last - Num_Scopes + 1 = Scope_Stack.First
5693 or else Scope_Stack.Table
5694 (Scope_Stack.Last - Num_Scopes).Entity = Scope (S);
5697 exit when Is_Generic_Instance (S)
5698 and then (In_Package_Body (S)
5699 or else Ekind (S) = E_Procedure
5700 or else Ekind (S) = E_Function);
5704 Vis := Is_Immediately_Visible (Gen_Comp);
5706 -- Find and save all enclosing instances
5711 and then S /= Standard_Standard
5713 if Is_Generic_Instance (S) then
5714 N_Instances := N_Instances + 1;
5715 Instances (N_Instances) := S;
5717 exit when In_Package_Body (S);
5723 -- Remove context of current compilation unit, unless we are within a
5724 -- nested package instantiation, in which case the context has been
5725 -- removed previously.
5727 -- If current scope is the body of a child unit, remove context of
5728 -- spec as well. If an enclosing scope is an instance body, the
5729 -- context has already been removed, but the entities in the body
5730 -- must be made invisible as well.
5733 while Present (S) and then S /= Standard_Standard loop
5734 if Is_Generic_Instance (S)
5735 and then (In_Package_Body (S)
5736 or else Ekind (S) in E_Procedure | E_Function)
5738 -- We still have to remove the entities of the enclosing
5739 -- instance from direct visibility.
5744 E := First_Entity (S);
5745 while Present (E) loop
5746 Set_Is_Immediately_Visible (E, False);
5755 or else (Ekind (Curr_Unit) = E_Package_Body
5756 and then S = Spec_Entity (Curr_Unit))
5757 or else (Ekind (Curr_Unit) = E_Subprogram_Body
5758 and then S = Corresponding_Spec
5759 (Unit_Declaration_Node (Curr_Unit)))
5763 -- Remove entities in current scopes from visibility, so that
5764 -- instance body is compiled in a clean environment.
5766 List := Save_Scope_Stack (Handle_Use => False);
5768 if Is_Child_Unit (S) then
5770 -- Remove child unit from stack, as well as inner scopes.
5771 -- Removing the context of a child unit removes parent units
5774 while Current_Scope /= S loop
5775 Num_Inner := Num_Inner + 1;
5776 Inner_Scopes (Num_Inner) := Current_Scope;
5781 Remove_Context (Curr_Comp);
5785 Remove_Context (Curr_Comp);
5788 if Ekind (Curr_Unit) = E_Package_Body then
5789 Remove_Context (Library_Unit (Curr_Comp));
5796 pragma Assert (Num_Inner < Num_Scopes);
5798 Push_Scope (Standard_Standard);
5799 Scope_Stack.Table (Scope_Stack.Last).Is_Active_Stack_Base := True;
5801 -- The inlined package body is analyzed with the configuration state
5802 -- of the context prior to the scope manipulations performed above.
5804 -- ??? shouldn't this also use the warning state of the context prior
5805 -- to the scope manipulations?
5807 Instantiate_Package_Body
5810 Act_Decl => Act_Decl,
5812 Config_Switches => Config_Attrs,
5813 Current_Sem_Unit => Current_Sem_Unit,
5814 Expander_Status => Expander_Active,
5815 Local_Suppress_Stack_Top => Local_Suppress_Stack_Top,
5816 Scope_Suppress => Scope_Suppress,
5817 Warnings => Save_Warnings)),
5818 Inlined_Body => True);
5824 Set_Is_Immediately_Visible (Gen_Comp, Vis);
5826 -- Reset Generic_Instance flag so that use clauses can be installed
5827 -- in the proper order. (See Use_One_Package for effect of enclosing
5828 -- instances on processing of use clauses).
5830 for J in 1 .. N_Instances loop
5831 Set_Is_Generic_Instance (Instances (J), False);
5835 Install_Context (Curr_Comp, Chain => False);
5837 if Present (Curr_Scope)
5838 and then Is_Child_Unit (Curr_Scope)
5840 Push_Scope (Curr_Scope);
5841 Set_Is_Immediately_Visible (Curr_Scope);
5843 -- Finally, restore inner scopes as well
5845 for J in reverse 1 .. Num_Inner loop
5846 Push_Scope (Inner_Scopes (J));
5850 Restore_Scope_Stack (List, Handle_Use => False);
5852 if Present (Curr_Scope)
5854 (In_Private_Part (Curr_Scope)
5855 or else In_Package_Body (Curr_Scope))
5857 -- Install private declaration of ancestor units, which are
5858 -- currently available. Restore_Scope_Stack and Install_Context
5859 -- only install the visible part of parents.
5864 Par := Scope (Curr_Scope);
5865 while Present (Par) and then Par /= Standard_Standard loop
5866 Install_Private_Declarations (Par);
5873 -- Restore use clauses. For a child unit, use clauses in the parents
5874 -- are restored when installing the context, so only those in inner
5875 -- scopes (and those local to the child unit itself) need to be
5876 -- installed explicitly.
5878 if Is_Child_Unit (Curr_Unit) and then Removed then
5879 for J in reverse 1 .. Num_Inner + 1 loop
5880 Scope_Stack.Table (Scope_Stack.Last - J + 1).First_Use_Clause :=
5882 Install_Use_Clauses (Use_Clauses (J));
5886 for J in reverse 1 .. Num_Scopes loop
5887 Scope_Stack.Table (Scope_Stack.Last - J + 1).First_Use_Clause :=
5889 Install_Use_Clauses (Use_Clauses (J));
5893 -- Restore status of instances. If one of them is a body, make its
5894 -- local entities visible again.
5901 for J in 1 .. N_Instances loop
5902 Inst := Instances (J);
5903 Set_Is_Generic_Instance (Inst, True);
5905 if In_Package_Body (Inst)
5906 or else Ekind (S) in E_Procedure | E_Function
5908 E := First_Entity (Instances (J));
5909 while Present (E) loop
5910 Set_Is_Immediately_Visible (E);
5917 -- If generic unit is in current unit, current context is correct. Note
5918 -- that the context is guaranteed to carry the correct SPARK_Mode as no
5919 -- enclosing scopes were removed.
5922 Instantiate_Package_Body
5925 Act_Decl => Act_Decl,
5927 Config_Switches => Save_Config_Switches,
5928 Current_Sem_Unit => Current_Sem_Unit,
5929 Expander_Status => Expander_Active,
5930 Local_Suppress_Stack_Top => Local_Suppress_Stack_Top,
5931 Scope_Suppress => Scope_Suppress,
5932 Warnings => Save_Warnings)),
5933 Inlined_Body => True);
5935 end Inline_Instance_Body;
5937 -------------------------------------
5938 -- Analyze_Procedure_Instantiation --
5939 -------------------------------------
5941 procedure Analyze_Procedure_Instantiation (N : Node_Id) is
5943 Analyze_Subprogram_Instantiation (N, E_Procedure);
5944 end Analyze_Procedure_Instantiation;
5946 -----------------------------------
5947 -- Need_Subprogram_Instance_Body --
5948 -----------------------------------
5950 function Need_Subprogram_Instance_Body
5952 Subp : Entity_Id) return Boolean
5954 function Is_Inlined_Or_Child_Of_Inlined (E : Entity_Id) return Boolean;
5955 -- Return True if E is an inlined subprogram, an inlined renaming or a
5956 -- subprogram nested in an inlined subprogram. The inlining machinery
5957 -- totally disregards nested subprograms since it considers that they
5958 -- will always be compiled if the parent is (see Inline.Is_Nested).
5960 ------------------------------------
5961 -- Is_Inlined_Or_Child_Of_Inlined --
5962 ------------------------------------
5964 function Is_Inlined_Or_Child_Of_Inlined (E : Entity_Id) return Boolean is
5968 if Is_Inlined (E) or else Is_Inlined (Alias (E)) then
5973 while Scop /= Standard_Standard loop
5974 if Is_Subprogram (Scop) and then Is_Inlined (Scop) then
5978 Scop := Scope (Scop);
5982 end Is_Inlined_Or_Child_Of_Inlined;
5984 -- Start of processing for Need_Subprogram_Instance_Body
5987 -- Must be in the main unit or inlined (or child of inlined)
5989 if (Is_In_Main_Unit (N) or else Is_Inlined_Or_Child_Of_Inlined (Subp))
5991 -- Must be generating code or analyzing code in GNATprove mode
5993 and then (Operating_Mode = Generate_Code
5994 or else (Operating_Mode = Check_Semantics
5995 and then GNATprove_Mode))
5997 -- The body is needed when generating code (full expansion) and in
5998 -- in GNATprove mode (special expansion) for formal verification of
6001 and then (Expander_Active or GNATprove_Mode)
6003 -- No point in inlining if ABE is inevitable
6005 and then not Is_Known_Guaranteed_ABE (N)
6007 -- Or if subprogram is eliminated
6009 and then not Is_Eliminated (Subp)
6011 Add_Pending_Instantiation (N, Unit_Declaration_Node (Subp));
6014 -- Here if not inlined, or we ignore the inlining
6019 end Need_Subprogram_Instance_Body;
6021 --------------------------------------
6022 -- Analyze_Subprogram_Instantiation --
6023 --------------------------------------
6025 -- WARNING: This routine manages Ghost and SPARK regions. Return statements
6026 -- must be replaced by gotos which jump to the end of the routine in order
6027 -- to restore the Ghost and SPARK modes.
6029 procedure Analyze_Subprogram_Instantiation
6033 Errs : constant Nat := Serious_Errors_Detected;
6034 Gen_Id : constant Node_Id := Name (N);
6035 Inst_Id : constant Entity_Id := Defining_Entity (N);
6036 Anon_Id : constant Entity_Id :=
6037 Make_Defining_Identifier (Sloc (Inst_Id),
6038 Chars => New_External_Name (Chars (Inst_Id), 'R'));
6039 Loc : constant Source_Ptr := Sloc (N);
6041 Act_Decl_Id : Entity_Id := Empty; -- init to avoid warning
6046 Env_Installed : Boolean := False;
6047 Gen_Unit : Entity_Id;
6049 Pack_Id : Entity_Id;
6050 Parent_Installed : Boolean := False;
6052 Renamings : List_Id;
6053 -- The list of declarations that link formals and actuals of the
6054 -- instance. These are subtype declarations for formal types, and
6055 -- renaming declarations for other formals. The subprogram declaration
6056 -- for the instance is then appended to the list, and the last item on
6057 -- the list is the renaming declaration for the instance.
6059 procedure Analyze_Instance_And_Renamings;
6060 -- The instance must be analyzed in a context that includes the mappings
6061 -- of generic parameters into actuals. We create a package declaration
6062 -- for this purpose, and a subprogram with an internal name within the
6063 -- package. The subprogram instance is simply an alias for the internal
6064 -- subprogram, declared in the current scope.
6066 procedure Build_Subprogram_Renaming;
6067 -- If the subprogram is recursive, there are occurrences of the name of
6068 -- the generic within the body, which must resolve to the current
6069 -- instance. We add a renaming declaration after the declaration, which
6070 -- is available in the instance body, as well as in the analysis of
6071 -- aspects that appear in the generic. This renaming declaration is
6072 -- inserted after the instance declaration which it renames.
6074 ------------------------------------
6075 -- Analyze_Instance_And_Renamings --
6076 ------------------------------------
6078 procedure Analyze_Instance_And_Renamings is
6079 Def_Ent : constant Entity_Id := Defining_Entity (N);
6080 Pack_Decl : Node_Id;
6083 if Nkind (Parent (N)) = N_Compilation_Unit then
6085 -- For the case of a compilation unit, the container package has
6086 -- the same name as the instantiation, to insure that the binder
6087 -- calls the elaboration procedure with the right name. Copy the
6088 -- entity of the instance, which may have compilation level flags
6089 -- (e.g. Is_Child_Unit) set.
6091 Pack_Id := New_Copy (Def_Ent);
6094 -- Otherwise we use the name of the instantiation concatenated
6095 -- with its source position to ensure uniqueness if there are
6096 -- several instantiations with the same name.
6099 Make_Defining_Identifier (Loc,
6100 Chars => New_External_Name
6101 (Related_Id => Chars (Def_Ent),
6103 Suffix_Index => Source_Offset (Sloc (Def_Ent))));
6107 Make_Package_Declaration (Loc,
6108 Specification => Make_Package_Specification (Loc,
6109 Defining_Unit_Name => Pack_Id,
6110 Visible_Declarations => Renamings,
6111 End_Label => Empty));
6113 Set_Instance_Spec (N, Pack_Decl);
6114 Set_Is_Generic_Instance (Pack_Id);
6115 Set_Debug_Info_Needed (Pack_Id);
6117 -- Case of not a compilation unit
6119 if Nkind (Parent (N)) /= N_Compilation_Unit then
6120 Mark_Rewrite_Insertion (Pack_Decl);
6121 Insert_Before (N, Pack_Decl);
6122 Set_Has_Completion (Pack_Id);
6124 -- Case of an instantiation that is a compilation unit
6126 -- Place declaration on current node so context is complete for
6127 -- analysis (including nested instantiations), and for use in a
6128 -- context_clause (see Analyze_With_Clause).
6131 Set_Unit (Parent (N), Pack_Decl);
6132 Set_Parent_Spec (Pack_Decl, Parent_Spec (N));
6135 Analyze (Pack_Decl);
6136 Check_Formal_Packages (Pack_Id);
6138 -- Body of the enclosing package is supplied when instantiating the
6139 -- subprogram body, after semantic analysis is completed.
6141 if Nkind (Parent (N)) = N_Compilation_Unit then
6143 -- Remove package itself from visibility, so it does not
6144 -- conflict with subprogram.
6146 Set_Name_Entity_Id (Chars (Pack_Id), Homonym (Pack_Id));
6148 -- Set name and scope of internal subprogram so that the proper
6149 -- external name will be generated. The proper scope is the scope
6150 -- of the wrapper package. We need to generate debugging info for
6151 -- the internal subprogram, so set flag accordingly.
6153 Set_Chars (Anon_Id, Chars (Defining_Entity (N)));
6154 Set_Scope (Anon_Id, Scope (Pack_Id));
6156 -- Mark wrapper package as referenced, to avoid spurious warnings
6157 -- if the instantiation appears in various with_ clauses of
6158 -- subunits of the main unit.
6160 Set_Referenced (Pack_Id);
6163 Set_Is_Generic_Instance (Anon_Id);
6164 Set_Debug_Info_Needed (Anon_Id);
6165 Act_Decl_Id := New_Copy (Anon_Id);
6167 Set_Parent (Act_Decl_Id, Parent (Anon_Id));
6168 Set_Chars (Act_Decl_Id, Chars (Defining_Entity (N)));
6169 Set_Sloc (Act_Decl_Id, Sloc (Defining_Entity (N)));
6171 -- Subprogram instance comes from source only if generic does
6173 Preserve_Comes_From_Source (Act_Decl_Id, Gen_Unit);
6175 -- If the instance is a child unit, mark the Id accordingly. Mark
6176 -- the anonymous entity as well, which is the real subprogram and
6177 -- which is used when the instance appears in a context clause.
6178 -- Similarly, propagate the Is_Eliminated flag to handle properly
6179 -- nested eliminated subprograms.
6181 Set_Is_Child_Unit (Act_Decl_Id, Is_Child_Unit (Defining_Entity (N)));
6182 Set_Is_Child_Unit (Anon_Id, Is_Child_Unit (Defining_Entity (N)));
6183 New_Overloaded_Entity (Act_Decl_Id);
6184 Check_Eliminated (Act_Decl_Id);
6185 Set_Is_Eliminated (Anon_Id, Is_Eliminated (Act_Decl_Id));
6187 if Nkind (Parent (N)) = N_Compilation_Unit then
6189 -- In compilation unit case, kill elaboration checks on the
6190 -- instantiation, since they are never needed - the body is
6191 -- instantiated at the same point as the spec.
6193 if Legacy_Elaboration_Checks then
6194 Set_Kill_Elaboration_Checks (Act_Decl_Id);
6195 Set_Suppress_Elaboration_Warnings (Act_Decl_Id);
6198 Set_Is_Compilation_Unit (Anon_Id);
6199 Set_Cunit_Entity (Current_Sem_Unit, Pack_Id);
6202 -- The instance is not a freezing point for the new subprogram.
6203 -- The anonymous subprogram may have a freeze node, created for
6204 -- some delayed aspects. This freeze node must not be inherited
6205 -- by the visible subprogram entity.
6207 Set_Is_Frozen (Act_Decl_Id, False);
6208 Set_Freeze_Node (Act_Decl_Id, Empty);
6210 if Nkind (Defining_Entity (N)) = N_Defining_Operator_Symbol then
6211 Valid_Operator_Definition (Act_Decl_Id);
6214 Set_Alias (Act_Decl_Id, Anon_Id);
6215 Set_Has_Completion (Act_Decl_Id);
6216 Set_Related_Instance (Pack_Id, Act_Decl_Id);
6218 if Nkind (Parent (N)) = N_Compilation_Unit then
6219 Set_Body_Required (Parent (N), False);
6221 end Analyze_Instance_And_Renamings;
6223 -------------------------------
6224 -- Build_Subprogram_Renaming --
6225 -------------------------------
6227 procedure Build_Subprogram_Renaming is
6228 Renaming_Decl : Node_Id;
6229 Unit_Renaming : Node_Id;
6233 Make_Subprogram_Renaming_Declaration (Loc,
6236 (Specification (Original_Node (Gen_Decl)),
6238 Instantiating => True),
6239 Name => New_Occurrence_Of (Anon_Id, Loc));
6241 -- The generic may be a child unit. The renaming needs an identifier
6242 -- with the proper name.
6244 Set_Defining_Unit_Name (Specification (Unit_Renaming),
6245 Make_Defining_Identifier (Loc, Chars (Gen_Unit)));
6247 -- If there is a formal subprogram with the same name as the unit
6248 -- itself, do not add this renaming declaration, to prevent
6249 -- ambiguities when there is a call with that name in the body.
6251 Renaming_Decl := First (Renamings);
6252 while Present (Renaming_Decl) loop
6253 if Nkind (Renaming_Decl) = N_Subprogram_Renaming_Declaration
6255 Chars (Defining_Entity (Renaming_Decl)) = Chars (Gen_Unit)
6260 Next (Renaming_Decl);
6263 if No (Renaming_Decl) then
6264 Append (Unit_Renaming, Renamings);
6266 end Build_Subprogram_Renaming;
6270 Saved_GM : constant Ghost_Mode_Type := Ghost_Mode;
6271 Saved_IGR : constant Node_Id := Ignored_Ghost_Region;
6272 Saved_ISMP : constant Boolean :=
6273 Ignore_SPARK_Mode_Pragmas_In_Instance;
6274 Saved_SM : constant SPARK_Mode_Type := SPARK_Mode;
6275 Saved_SMP : constant Node_Id := SPARK_Mode_Pragma;
6276 -- Save the Ghost and SPARK mode-related data to restore on exit
6278 Vis_Prims_List : Elist_Id := No_Elist;
6279 -- List of primitives made temporarily visible in the instantiation
6280 -- to match the visibility of the formal type
6282 -- Start of processing for Analyze_Subprogram_Instantiation
6285 -- Preserve relevant elaboration-related attributes of the context which
6286 -- are no longer available or very expensive to recompute once analysis,
6287 -- resolution, and expansion are over.
6289 Mark_Elaboration_Attributes
6296 -- Very first thing: check for special Text_IO unit in case we are
6297 -- instantiating one of the children of [[Wide_]Wide_]Text_IO. Of course
6298 -- such an instantiation is bogus (these are packages, not subprograms),
6299 -- but we get a better error message if we do this.
6301 Check_Text_IO_Special_Unit (Gen_Id);
6303 -- Make node global for error reporting
6305 Instantiation_Node := N;
6307 -- For package instantiations we turn off style checks, because they
6308 -- will have been emitted in the generic. For subprogram instantiations
6309 -- we want to apply at least the check on overriding indicators so we
6310 -- do not modify the style check status.
6312 -- The renaming declarations for the actuals do not come from source and
6313 -- will not generate spurious warnings.
6315 Preanalyze_Actuals (N);
6318 Env_Installed := True;
6319 Check_Generic_Child_Unit (Gen_Id, Parent_Installed);
6320 Gen_Unit := Entity (Gen_Id);
6322 -- A subprogram instantiation is Ghost when it is subject to pragma
6323 -- Ghost or the generic template is Ghost. Set the mode now to ensure
6324 -- that any nodes generated during analysis and expansion are marked as
6327 Mark_And_Set_Ghost_Instantiation (N, Gen_Unit);
6329 Generate_Reference (Gen_Unit, Gen_Id);
6331 if Nkind (Gen_Id) = N_Identifier
6332 and then Chars (Gen_Unit) = Chars (Defining_Entity (N))
6335 ("& is hidden within declaration of instance", Gen_Id, Gen_Unit);
6338 if Etype (Gen_Unit) = Any_Type then
6343 -- Verify that it is a generic subprogram of the right kind, and that
6344 -- it does not lead to a circular instantiation.
6346 if K = E_Procedure and then Ekind (Gen_Unit) /= E_Generic_Procedure then
6348 ("& is not the name of a generic procedure", Gen_Id, Gen_Unit);
6350 elsif K = E_Function and then Ekind (Gen_Unit) /= E_Generic_Function then
6352 ("& is not the name of a generic function", Gen_Id, Gen_Unit);
6354 elsif In_Open_Scopes (Gen_Unit) then
6355 Error_Msg_NE ("instantiation of & within itself", N, Gen_Unit);
6358 Mutate_Ekind (Inst_Id, K);
6359 Set_Scope (Inst_Id, Current_Scope);
6361 Set_Entity (Gen_Id, Gen_Unit);
6363 if In_Extended_Main_Source_Unit (N) then
6364 Set_Is_Instantiated (Gen_Unit);
6365 Generate_Reference (Gen_Unit, N);
6368 -- If renaming, get original unit
6370 if Present (Renamed_Entity (Gen_Unit))
6371 and then Is_Generic_Subprogram (Renamed_Entity (Gen_Unit))
6373 Gen_Unit := Renamed_Entity (Gen_Unit);
6374 Set_Is_Instantiated (Gen_Unit);
6375 Generate_Reference (Gen_Unit, N);
6378 if Contains_Instance_Of (Gen_Unit, Current_Scope, Gen_Id) then
6379 Error_Msg_Node_2 := Current_Scope;
6381 ("circular instantiation: & instantiated in &!", N, Gen_Unit);
6382 Circularity_Detected := True;
6383 Restore_Hidden_Primitives (Vis_Prims_List);
6387 Gen_Decl := Unit_Declaration_Node (Gen_Unit);
6389 -- Initialize renamings map, for error checking
6391 Generic_Renamings.Set_Last (0);
6392 Generic_Renamings_HTable.Reset;
6394 Create_Instantiation_Source (N, Gen_Unit, S_Adjustment);
6396 -- Copy original generic tree, to produce text for instantiation
6400 (Original_Node (Gen_Decl), Empty, Instantiating => True);
6402 -- Inherit overriding indicator from instance node
6404 Act_Spec := Specification (Act_Tree);
6405 Set_Must_Override (Act_Spec, Must_Override (N));
6406 Set_Must_Not_Override (Act_Spec, Must_Not_Override (N));
6409 Analyze_Associations
6411 Formals => Generic_Formal_Declarations (Act_Tree),
6412 F_Copy => Generic_Formal_Declarations (Gen_Decl));
6414 Vis_Prims_List := Check_Hidden_Primitives (Renamings);
6416 -- The subprogram itself cannot contain a nested instance, so the
6417 -- current parent is left empty.
6419 Set_Instance_Env (Gen_Unit, Empty);
6421 -- Build the subprogram declaration, which does not appear in the
6422 -- generic template, and give it a sloc consistent with that of the
6425 Set_Defining_Unit_Name (Act_Spec, Anon_Id);
6426 Set_Generic_Parent (Act_Spec, Gen_Unit);
6428 Make_Subprogram_Declaration (Sloc (Act_Spec),
6429 Specification => Act_Spec);
6431 -- The aspects have been copied previously, but they have to be
6432 -- linked explicitly to the new subprogram declaration. Explicit
6433 -- pre/postconditions on the instance are analyzed below, in a
6436 Move_Aspects (Act_Tree, To => Act_Decl);
6437 Set_Categorization_From_Pragmas (Act_Decl);
6439 if Parent_Installed then
6443 Append (Act_Decl, Renamings);
6445 -- Contract-related source pragmas that follow a generic subprogram
6446 -- must be instantiated explicitly because they are not part of the
6447 -- subprogram template.
6449 Instantiate_Subprogram_Contract
6450 (Original_Node (Gen_Decl), Renamings);
6452 Build_Subprogram_Renaming;
6454 -- If the context of the instance is subject to SPARK_Mode "off" or
6455 -- the annotation is altogether missing, set the global flag which
6456 -- signals Analyze_Pragma to ignore all SPARK_Mode pragmas within
6457 -- the instance. This should be done prior to analyzing the instance.
6459 if SPARK_Mode /= On then
6460 Ignore_SPARK_Mode_Pragmas_In_Instance := True;
6463 -- If the context of an instance is not subject to SPARK_Mode "off",
6464 -- and the generic spec is subject to an explicit SPARK_Mode pragma,
6465 -- the latter should be the one applicable to the instance.
6467 if not Ignore_SPARK_Mode_Pragmas_In_Instance
6468 and then Saved_SM /= Off
6469 and then Present (SPARK_Pragma (Gen_Unit))
6471 Set_SPARK_Mode (Gen_Unit);
6474 -- Need to mark Anon_Id intrinsic before calling
6475 -- Analyze_Instance_And_Renamings because this flag may be propagated
6478 if Is_Intrinsic_Subprogram (Gen_Unit) then
6479 Set_Is_Intrinsic_Subprogram (Anon_Id);
6480 Set_Interface_Name (Anon_Id, Interface_Name (Gen_Unit));
6483 Analyze_Instance_And_Renamings;
6485 -- Restore SPARK_Mode from the context after analysis of the package
6486 -- declaration, so that the SPARK_Mode on the generic spec does not
6487 -- apply to the pending instance for the instance body.
6489 if not Ignore_SPARK_Mode_Pragmas_In_Instance
6490 and then Saved_SM /= Off
6491 and then Present (SPARK_Pragma (Gen_Unit))
6493 Restore_SPARK_Mode (Saved_SM, Saved_SMP);
6496 -- If the generic is marked Import (Intrinsic), then so is the
6497 -- instance; this indicates that there is no body to instantiate.
6498 -- We also copy the interface name in case this is handled by the
6499 -- back-end and deal with an instance of unchecked conversion.
6501 if Is_Intrinsic_Subprogram (Gen_Unit) then
6502 Set_Is_Intrinsic_Subprogram (Act_Decl_Id);
6503 Set_Interface_Name (Act_Decl_Id, Interface_Name (Gen_Unit));
6505 if Chars (Gen_Unit) = Name_Unchecked_Conversion then
6506 Validate_Unchecked_Conversion (N, Act_Decl_Id);
6510 -- Inherit convention from generic unit. Intrinsic convention, as for
6511 -- an instance of unchecked conversion, is not inherited because an
6512 -- explicit Ada instance has been created.
6514 if Has_Convention_Pragma (Gen_Unit)
6515 and then Convention (Gen_Unit) /= Convention_Intrinsic
6517 Set_Convention (Act_Decl_Id, Convention (Gen_Unit));
6518 Set_Is_Exported (Act_Decl_Id, Is_Exported (Gen_Unit));
6521 Generate_Definition (Act_Decl_Id);
6523 -- Inherit all inlining-related flags which apply to the generic in
6524 -- the subprogram and its declaration.
6526 Set_Is_Inlined (Act_Decl_Id, Is_Inlined (Gen_Unit));
6527 Set_Is_Inlined (Anon_Id, Is_Inlined (Gen_Unit));
6529 Set_Has_Pragma_Inline (Act_Decl_Id, Has_Pragma_Inline (Gen_Unit));
6530 Set_Has_Pragma_Inline (Anon_Id, Has_Pragma_Inline (Gen_Unit));
6532 Set_Has_Pragma_Inline_Always
6533 (Act_Decl_Id, Has_Pragma_Inline_Always (Gen_Unit));
6534 Set_Has_Pragma_Inline_Always
6535 (Anon_Id, Has_Pragma_Inline_Always (Gen_Unit));
6537 Set_Has_Pragma_No_Inline
6538 (Act_Decl_Id, Has_Pragma_No_Inline (Gen_Unit));
6539 Set_Has_Pragma_No_Inline
6540 (Anon_Id, Has_Pragma_No_Inline (Gen_Unit));
6542 -- Propagate No_Raise if pragma applied to generic unit. This must
6543 -- be done explicitly because the pragma does not appear in generic
6544 -- declarations (unlike the aspect).
6546 if No_Raise (Gen_Unit) then
6547 Set_No_Raise (Act_Decl_Id);
6548 Set_No_Raise (Anon_Id);
6551 -- Likewise for No_Return
6553 if No_Return (Gen_Unit) then
6554 Set_No_Return (Act_Decl_Id);
6555 Set_No_Return (Anon_Id);
6558 -- Mark both the instance spec and the anonymous package in case the
6559 -- body is instantiated at a later pass. This preserves the original
6560 -- context in effect for the body.
6562 if SPARK_Mode /= On then
6563 Set_Ignore_SPARK_Mode_Pragmas (Act_Decl_Id);
6564 Set_Ignore_SPARK_Mode_Pragmas (Anon_Id);
6567 if Legacy_Elaboration_Checks
6568 and then not Is_Intrinsic_Subprogram (Gen_Unit)
6570 Check_Elab_Instantiation (N);
6573 -- Save the scenario for later examination by the ABE Processing
6576 Record_Elaboration_Scenario (N);
6578 -- The instantiation results in a guaranteed ABE. Create a completing
6579 -- body for the subprogram declaration because the real body will not
6582 if Is_Known_Guaranteed_ABE (N) then
6583 Provide_Completing_Bodies (Instance_Spec (N));
6586 if Is_Dispatching_Operation (Act_Decl_Id)
6587 and then Ada_Version >= Ada_2005
6593 Formal := First_Formal (Act_Decl_Id);
6594 while Present (Formal) loop
6595 if Ekind (Etype (Formal)) = E_Anonymous_Access_Type
6596 and then Is_Controlling_Formal (Formal)
6597 and then not Can_Never_Be_Null (Formal)
6600 ("access parameter& is controlling,", N, Formal);
6602 ("\corresponding parameter of & must be explicitly "
6603 & "null-excluding", N, Gen_Id);
6606 Next_Formal (Formal);
6611 Check_Hidden_Child_Unit (N, Gen_Unit, Act_Decl_Id);
6613 Validate_Categorization_Dependency (N, Act_Decl_Id);
6615 if not Is_Intrinsic_Subprogram (Act_Decl_Id) then
6616 Inherit_Context (Gen_Decl, N);
6618 Restore_Private_Views (Pack_Id, False);
6620 -- If the context requires a full instantiation, mark node for
6621 -- subsequent construction of the body.
6623 if Need_Subprogram_Instance_Body (N, Act_Decl_Id) then
6624 Check_Forward_Instantiation (Gen_Decl);
6626 -- The wrapper package is always delayed, because it does not
6627 -- constitute a freeze point, but to insure that the freeze node
6628 -- is placed properly, it is created directly when instantiating
6629 -- the body (otherwise the freeze node might appear to early for
6630 -- nested instantiations).
6632 elsif Nkind (Parent (N)) = N_Compilation_Unit then
6633 Rewrite (N, Unit (Parent (N)));
6634 Set_Unit (Parent (N), N);
6637 -- Replace instance node for library-level instantiations of
6638 -- intrinsic subprograms.
6640 elsif Nkind (Parent (N)) = N_Compilation_Unit then
6641 Rewrite (N, Unit (Parent (N)));
6642 Set_Unit (Parent (N), N);
6645 if Parent_Installed then
6649 Restore_Hidden_Primitives (Vis_Prims_List);
6651 Env_Installed := False;
6652 Generic_Renamings.Set_Last (0);
6653 Generic_Renamings_HTable.Reset;
6657 -- Analyze aspects in declaration if no errors appear in the instance.
6659 if Has_Aspects (N) and then Serious_Errors_Detected = Errs then
6660 Analyze_Aspect_Specifications (N, Act_Decl_Id);
6663 Ignore_SPARK_Mode_Pragmas_In_Instance := Saved_ISMP;
6664 Restore_Ghost_Region (Saved_GM, Saved_IGR);
6665 Restore_SPARK_Mode (Saved_SM, Saved_SMP);
6668 when Instantiation_Error =>
6669 if Parent_Installed then
6673 if Env_Installed then
6677 Ignore_SPARK_Mode_Pragmas_In_Instance := Saved_ISMP;
6678 Restore_Ghost_Region (Saved_GM, Saved_IGR);
6679 Restore_SPARK_Mode (Saved_SM, Saved_SMP);
6680 end Analyze_Subprogram_Instantiation;
6682 ---------------------------
6683 -- Get_Associated_Entity --
6684 ---------------------------
6686 function Get_Associated_Entity (Id : Entity_Id) return Entity_Id is
6690 Assoc := Associated_Entity (Id);
6692 if Present (Assoc) then
6693 while Present (Associated_Entity (Assoc)) loop
6694 Assoc := Associated_Entity (Assoc);
6699 end Get_Associated_Entity;
6701 -------------------------
6702 -- Get_Associated_Node --
6703 -------------------------
6705 function Get_Associated_Node (N : Node_Id) return Node_Id is
6709 Assoc := Associated_Node (N);
6711 if Nkind (Assoc) /= Nkind (N) then
6714 elsif Nkind (Assoc) in N_Aggregate | N_Extension_Aggregate then
6718 -- If the node is part of an inner generic, it may itself have been
6719 -- remapped into a further generic copy. Associated_Node is otherwise
6720 -- used for the entity of the node, and will be of a different node
6721 -- kind, or else N has been rewritten as a literal or function call.
6723 while Present (Associated_Node (Assoc))
6724 and then Nkind (Associated_Node (Assoc)) = Nkind (Assoc)
6726 Assoc := Associated_Node (Assoc);
6729 -- Follow an additional link in case the final node was rewritten.
6730 -- This can only happen with nested generic units.
6732 if (Nkind (Assoc) = N_Identifier or else Nkind (Assoc) in N_Op)
6733 and then Present (Associated_Node (Assoc))
6734 and then Nkind (Associated_Node (Assoc)) in N_Function_Call
6735 | N_Explicit_Dereference
6740 Assoc := Associated_Node (Assoc);
6743 -- An additional special case: an unconstrained type in an object
6744 -- declaration may have been rewritten as a local subtype constrained
6745 -- by the expression in the declaration. We need to recover the
6746 -- original entity, which may be global.
6748 if Present (Original_Node (Assoc))
6749 and then Nkind (Parent (N)) = N_Object_Declaration
6751 Assoc := Original_Node (Assoc);
6756 end Get_Associated_Node;
6758 -----------------------------------
6759 -- Build_Subprogram_Decl_Wrapper --
6760 -----------------------------------
6762 function Build_Subprogram_Decl_Wrapper
6763 (Formal_Subp : Entity_Id) return Node_Id
6765 Loc : constant Source_Ptr := Sloc (Current_Scope);
6766 Ret_Type : constant Entity_Id := Get_Instance_Of (Etype (Formal_Subp));
6769 Parm_Spec : Node_Id;
6770 Profile : List_Id := New_List;
6776 Subp := Make_Defining_Identifier (Loc, Chars (Formal_Subp));
6777 Mutate_Ekind (Subp, Ekind (Formal_Subp));
6778 Set_Is_Generic_Actual_Subprogram (Subp);
6780 Profile := Parameter_Specifications (
6782 (Specification (Unit_Declaration_Node (Formal_Subp))));
6784 Form_F := First_Formal (Formal_Subp);
6785 Parm_Spec := First (Profile);
6787 -- Create new entities for the formals. Reset entities so that
6788 -- parameter types are properly resolved when wrapper declaration
6791 while Present (Parm_Spec) loop
6792 New_F := Make_Defining_Identifier (Loc, Chars (Form_F));
6793 Set_Defining_Identifier (Parm_Spec, New_F);
6794 Set_Entity (Parameter_Type (Parm_Spec), Empty);
6796 Next_Formal (Form_F);
6799 if Ret_Type = Standard_Void_Type then
6801 Make_Procedure_Specification (Loc,
6802 Defining_Unit_Name => Subp,
6803 Parameter_Specifications => Profile);
6806 Make_Function_Specification (Loc,
6807 Defining_Unit_Name => Subp,
6808 Parameter_Specifications => Profile,
6809 Result_Definition => New_Occurrence_Of (Ret_Type, Loc));
6813 Make_Subprogram_Declaration (Loc, Specification => Spec);
6816 end Build_Subprogram_Decl_Wrapper;
6818 -----------------------------------
6819 -- Build_Subprogram_Body_Wrapper --
6820 -----------------------------------
6822 function Build_Subprogram_Body_Wrapper
6823 (Formal_Subp : Entity_Id;
6824 Actual_Name : Node_Id) return Node_Id
6826 Loc : constant Source_Ptr := Sloc (Current_Scope);
6827 Ret_Type : constant Entity_Id := Get_Instance_Of (Etype (Formal_Subp));
6828 Spec_Node : constant Node_Id :=
6830 (Build_Subprogram_Decl_Wrapper (Formal_Subp));
6833 Body_Node : Node_Id;
6836 Actuals := New_List;
6837 Act := First (Parameter_Specifications (Spec_Node));
6839 while Present (Act) loop
6841 Make_Identifier (Loc, Chars (Defining_Identifier (Act))));
6845 if Ret_Type = Standard_Void_Type then
6846 Stmt := Make_Procedure_Call_Statement (Loc,
6847 Name => Actual_Name,
6848 Parameter_Associations => Actuals);
6851 Stmt := Make_Simple_Return_Statement (Loc,
6853 Make_Function_Call (Loc,
6854 Name => Actual_Name,
6855 Parameter_Associations => Actuals));
6858 Body_Node := Make_Subprogram_Body (Loc,
6859 Specification => Spec_Node,
6860 Declarations => New_List,
6861 Handled_Statement_Sequence =>
6862 Make_Handled_Sequence_Of_Statements (Loc,
6863 Statements => New_List (Stmt)));
6866 end Build_Subprogram_Body_Wrapper;
6868 -------------------------------
6869 -- Build_Subprogram_Wrappers --
6870 -------------------------------
6872 procedure Build_Subprogram_Wrappers
6873 (Match, Analyzed_Formal : Node_Id; Renamings : List_Id)
6875 function Adjust_Aspect_Sloc (N : Node_Id) return Traverse_Result;
6876 -- Adjust Sloc so that errors will be reported on the instance rather
6877 -- than the generic.
6879 ------------------------
6880 -- Adjust_Aspect_Sloc --
6881 ------------------------
6883 function Adjust_Aspect_Sloc (N : Node_Id) return Traverse_Result is
6885 Adjust_Instantiation_Sloc (N, S_Adjustment);
6887 end Adjust_Aspect_Sloc;
6889 procedure Adjust_Aspect_Slocs is new
6890 Traverse_Proc (Adjust_Aspect_Sloc);
6892 Formal : constant Entity_Id :=
6893 Defining_Unit_Name (Specification (Analyzed_Formal));
6894 Aspect_Spec : Node_Id;
6895 Decl_Node : Node_Id;
6896 Actual_Name : Node_Id;
6898 -- Start of processing for Build_Subprogram_Wrappers
6901 -- Create declaration for wrapper subprogram.
6902 -- The actual can be overloaded, in which case it will be
6903 -- resolved when the call in the wrapper body is analyzed.
6904 -- We attach the possible interpretations of the actual to
6905 -- the name to be used in the call in the wrapper body.
6907 if Is_Entity_Name (Match) then
6908 Actual_Name := New_Occurrence_Of (Entity (Match), Sloc (Match));
6910 if Is_Overloaded (Match) then
6911 Save_Interps (Match, Actual_Name);
6915 -- Use renaming declaration created when analyzing actual.
6916 -- This may be incomplete if there are several formal
6917 -- subprograms whose actual is an attribute ???
6920 Renaming_Decl : constant Node_Id := Last (Renamings);
6923 Actual_Name := New_Occurrence_Of
6924 (Defining_Entity (Renaming_Decl), Sloc (Match));
6925 Set_Etype (Actual_Name, Get_Instance_Of (Etype (Formal)));
6929 Decl_Node := Build_Subprogram_Decl_Wrapper (Formal);
6931 -- Transfer aspect specifications from formal subprogram to wrapper
6933 Set_Aspect_Specifications (Decl_Node,
6934 New_Copy_List_Tree (Aspect_Specifications (Analyzed_Formal)));
6936 Aspect_Spec := First (Aspect_Specifications (Decl_Node));
6937 while Present (Aspect_Spec) loop
6938 Adjust_Aspect_Slocs (Aspect_Spec);
6939 Set_Analyzed (Aspect_Spec, False);
6943 Append_To (Renamings, Decl_Node);
6945 -- Create corresponding body, and append it to association list
6946 -- that appears at the head of the declarations in the instance.
6947 -- The subprogram may be called in the analysis of subsequent
6950 Append_To (Renamings,
6951 Build_Subprogram_Body_Wrapper (Formal, Actual_Name));
6952 end Build_Subprogram_Wrappers;
6954 -------------------------------------------
6955 -- Build_Instance_Compilation_Unit_Nodes --
6956 -------------------------------------------
6958 procedure Build_Instance_Compilation_Unit_Nodes
6963 Decl_Cunit : Node_Id;
6964 Body_Cunit : Node_Id;
6966 New_Main : constant Entity_Id := Defining_Entity (Act_Decl);
6967 Old_Main : constant Entity_Id := Cunit_Entity (Main_Unit);
6970 -- A new compilation unit node is built for the instance declaration.
6971 -- It relocates the auxiliary declaration node from the compilation unit
6972 -- where the instance appeared, so that declarations that originally
6973 -- followed the instance will be attached to the spec compilation unit.
6976 Make_Compilation_Unit (Sloc (N),
6977 Context_Items => Empty_List,
6979 Aux_Decls_Node => Relocate_Node (Aux_Decls_Node (Parent (N))));
6981 Set_Parent_Spec (Act_Decl, Parent_Spec (N));
6983 -- The new compilation unit is linked to its body, but both share the
6984 -- same file, so we do not set Body_Required on the new unit so as not
6985 -- to create a spurious dependency on a non-existent body in the ali.
6986 -- This simplifies CodePeer unit traversal.
6988 -- We use the original instantiation compilation unit as the resulting
6989 -- compilation unit of the instance, since this is the main unit.
6991 Rewrite (N, Act_Body);
6993 Body_Cunit := Parent (N);
6995 -- The two compilation unit nodes are linked by the Library_Unit field
6997 Set_Library_Unit (Decl_Cunit, Body_Cunit);
6998 Set_Library_Unit (Body_Cunit, Decl_Cunit);
7000 -- Preserve the private nature of the package if needed
7002 Set_Private_Present (Decl_Cunit, Private_Present (Body_Cunit));
7004 -- If the instance is not the main unit, its context, categorization
7005 -- and elaboration entity are not relevant to the compilation.
7007 if Body_Cunit /= Cunit (Main_Unit) then
7008 Make_Instance_Unit (Body_Cunit, In_Main => False);
7012 -- The context clause items on the instantiation, which are now attached
7013 -- to the body compilation unit (since the body overwrote the original
7014 -- instantiation node), semantically belong on the spec, so copy them
7015 -- there. It's harmless to leave them on the body as well. In fact one
7016 -- could argue that they belong in both places.
7018 Citem := First (Context_Items (Body_Cunit));
7019 while Present (Citem) loop
7020 Append (New_Copy (Citem), Context_Items (Decl_Cunit));
7024 -- Propagate categorization flags on packages, so that they appear in
7025 -- the ali file for the spec of the unit.
7027 if Ekind (New_Main) = E_Package then
7028 Set_Is_Pure (Old_Main, Is_Pure (New_Main));
7029 Set_Is_Preelaborated (Old_Main, Is_Preelaborated (New_Main));
7030 Set_Is_Remote_Types (Old_Main, Is_Remote_Types (New_Main));
7031 Set_Is_Shared_Passive (Old_Main, Is_Shared_Passive (New_Main));
7032 Set_Is_Remote_Call_Interface
7033 (Old_Main, Is_Remote_Call_Interface (New_Main));
7036 -- Make entry in Units table, so that binder can generate call to
7037 -- elaboration procedure for body, if any.
7039 Make_Instance_Unit (Body_Cunit, In_Main => True);
7040 Main_Unit_Entity := New_Main;
7041 Set_Cunit_Entity (Main_Unit, Main_Unit_Entity);
7043 -- Build elaboration entity, since the instance may certainly generate
7044 -- elaboration code requiring a flag for protection.
7046 Build_Elaboration_Entity (Decl_Cunit, New_Main);
7047 end Build_Instance_Compilation_Unit_Nodes;
7049 --------------------------------
7050 -- Check_Abbreviated_Instance --
7051 --------------------------------
7053 procedure Check_Abbreviated_Instance
7055 Parent_Installed : in out Boolean)
7057 Inst_Node : Node_Id;
7060 if Nkind (N) = N_Package_Specification
7061 and then Is_Abbreviated_Instance (Defining_Entity (N))
7063 Inst_Node := Get_Unit_Instantiation_Node (Defining_Entity (N));
7064 Check_Generic_Child_Unit (Name (Inst_Node), Parent_Installed);
7066 end Check_Abbreviated_Instance;
7068 -----------------------------
7069 -- Check_Access_Definition --
7070 -----------------------------
7072 procedure Check_Access_Definition (N : Node_Id) is
7075 (Ada_Version >= Ada_2005 and then Present (Access_Definition (N)));
7077 end Check_Access_Definition;
7079 -----------------------------------
7080 -- Check_Formal_Package_Instance --
7081 -----------------------------------
7083 -- If the formal has specific parameters, they must match those of the
7084 -- actual. Both of them are instances, and the renaming declarations for
7085 -- their formal parameters appear in the same order in both. The analyzed
7086 -- formal has been analyzed in the context of the current instance.
7088 procedure Check_Formal_Package_Instance
7089 (Formal_Pack : Entity_Id;
7090 Actual_Pack : Entity_Id)
7092 E1 : Entity_Id := First_Entity (Actual_Pack);
7093 E2 : Entity_Id := First_Entity (Formal_Pack);
7094 Prev_E1 : Entity_Id;
7099 procedure Check_Mismatch (B : Boolean);
7100 -- Common error routine for mismatch between the parameters of the
7101 -- actual instance and those of the formal package.
7103 function Is_Defaulted (Param : Entity_Id) return Boolean;
7104 -- If the formal package has partly box-initialized formals, skip
7105 -- conformance check for these formals. Previously the code assumed
7106 -- that box initialization for a formal package applied to all its
7107 -- formal parameters.
7109 function Same_Instantiated_Constant (E1, E2 : Entity_Id) return Boolean;
7110 -- The formal may come from a nested formal package, and the actual may
7111 -- have been constant-folded. To determine whether the two denote the
7112 -- same entity we may have to traverse several definitions to recover
7113 -- the ultimate entity that they refer to.
7115 function Same_Instantiated_Function (E1, E2 : Entity_Id) return Boolean;
7116 -- The formal and the actual must be identical, but if both are
7117 -- given by attributes they end up renaming different generated bodies,
7118 -- and we must verify that the attributes themselves match.
7120 function Same_Instantiated_Variable (E1, E2 : Entity_Id) return Boolean;
7121 -- Similarly, if the formal comes from a nested formal package, the
7122 -- actual may designate the formal through multiple renamings, which
7123 -- have to be followed to determine the original variable in question.
7125 --------------------
7126 -- Check_Mismatch --
7127 --------------------
7129 procedure Check_Mismatch (B : Boolean) is
7130 -- A Formal_Type_Declaration for a derived private type is rewritten
7131 -- as a private extension decl. (see Analyze_Formal_Derived_Type),
7132 -- which is why we examine the original node.
7134 Kind : constant Node_Kind := Nkind (Original_Node (Parent (E2)));
7137 if Kind = N_Formal_Type_Declaration then
7140 elsif Kind in N_Formal_Object_Declaration
7141 | N_Formal_Package_Declaration
7142 | N_Formal_Subprogram_Declaration
7146 -- Ada 2012: If both formal and actual are incomplete types they
7149 elsif Is_Incomplete_Type (E1) and then Is_Incomplete_Type (E2) then
7154 ("actual for & in actual instance does not match formal",
7155 Parent (Actual_Pack), E1);
7163 function Is_Defaulted (Param : Entity_Id) return Boolean is
7168 First (Generic_Associations (Parent
7169 (Associated_Formal_Package (Actual_Pack))));
7171 while Present (Assoc) loop
7172 if Nkind (Assoc) = N_Others_Choice then
7175 elsif Nkind (Assoc) = N_Generic_Association
7176 and then Chars (Selector_Name (Assoc)) = Chars (Param)
7178 return Box_Present (Assoc);
7187 --------------------------------
7188 -- Same_Instantiated_Constant --
7189 --------------------------------
7191 function Same_Instantiated_Constant
7192 (E1, E2 : Entity_Id) return Boolean
7198 while Present (Ent) loop
7202 elsif Ekind (Ent) /= E_Constant then
7205 elsif Is_Entity_Name (Constant_Value (Ent)) then
7206 if Entity (Constant_Value (Ent)) = E1 then
7209 Ent := Entity (Constant_Value (Ent));
7212 -- The actual may be a constant that has been folded. Recover
7215 elsif Is_Entity_Name (Original_Node (Constant_Value (Ent))) then
7216 Ent := Entity (Original_Node (Constant_Value (Ent)));
7224 end Same_Instantiated_Constant;
7226 --------------------------------
7227 -- Same_Instantiated_Function --
7228 --------------------------------
7230 function Same_Instantiated_Function
7231 (E1, E2 : Entity_Id) return Boolean
7235 if Alias (E1) = Alias (E2) then
7238 elsif Present (Alias (E2)) then
7239 U1 := Original_Node (Unit_Declaration_Node (E1));
7240 U2 := Original_Node (Unit_Declaration_Node (Alias (E2)));
7242 return Nkind (U1) = N_Subprogram_Renaming_Declaration
7243 and then Nkind (Name (U1)) = N_Attribute_Reference
7245 and then Nkind (U2) = N_Subprogram_Renaming_Declaration
7246 and then Nkind (Name (U2)) = N_Attribute_Reference
7249 Attribute_Name (Name (U1)) = Attribute_Name (Name (U2));
7253 end Same_Instantiated_Function;
7255 --------------------------------
7256 -- Same_Instantiated_Variable --
7257 --------------------------------
7259 function Same_Instantiated_Variable
7260 (E1, E2 : Entity_Id) return Boolean
7262 function Original_Entity (E : Entity_Id) return Entity_Id;
7263 -- Follow chain of renamings to the ultimate ancestor
7265 ---------------------
7266 -- Original_Entity --
7267 ---------------------
7269 function Original_Entity (E : Entity_Id) return Entity_Id is
7274 while Nkind (Parent (Orig)) = N_Object_Renaming_Declaration
7275 and then Present (Renamed_Object (Orig))
7276 and then Is_Entity_Name (Renamed_Object (Orig))
7278 Orig := Entity (Renamed_Object (Orig));
7282 end Original_Entity;
7284 -- Start of processing for Same_Instantiated_Variable
7287 return Ekind (E1) = Ekind (E2)
7288 and then Original_Entity (E1) = Original_Entity (E2);
7289 end Same_Instantiated_Variable;
7291 -- Start of processing for Check_Formal_Package_Instance
7295 while Present (E1) and then Present (E2) loop
7296 exit when Ekind (E1) = E_Package
7297 and then Renamed_Entity (E1) = Renamed_Entity (Actual_Pack);
7299 -- If the formal is the renaming of the formal package, this
7300 -- is the end of its formal part, which may occur before the
7301 -- end of the formal part in the actual in the presence of
7302 -- defaulted parameters in the formal package.
7304 exit when Nkind (Parent (E2)) = N_Package_Renaming_Declaration
7305 and then Renamed_Entity (E2) = Scope (E2);
7307 -- The analysis of the actual may generate additional internal
7308 -- entities. If the formal is defaulted, there is no corresponding
7309 -- analysis and the internal entities must be skipped, until we
7310 -- find corresponding entities again.
7312 if Comes_From_Source (E2)
7313 and then not Comes_From_Source (E1)
7314 and then Chars (E1) /= Chars (E2)
7316 while Present (E1) and then Chars (E1) /= Chars (E2) loop
7324 -- Entities may be declared without full declaration, such as
7325 -- itypes and predefined operators (concatenation for arrays, eg).
7326 -- Skip it and keep the formal entity to find a later match for it.
7328 elsif No (Parent (E2)) and then Ekind (E1) /= Ekind (E2) then
7332 -- If the formal entity comes from a formal declaration, it was
7333 -- defaulted in the formal package, and no check is needed on it.
7335 elsif Nkind (Original_Node (Parent (E2))) in
7336 N_Formal_Object_Declaration | N_Formal_Type_Declaration
7338 -- If the formal is a tagged type the corresponding class-wide
7339 -- type has been generated as well, and it must be skipped.
7341 if Is_Type (E2) and then Is_Tagged_Type (E2) then
7347 -- Ditto for defaulted formal subprograms.
7349 elsif Is_Overloadable (E1)
7350 and then Nkind (Unit_Declaration_Node (E2)) in
7351 N_Formal_Subprogram_Declaration
7355 elsif Is_Defaulted (E1) then
7358 elsif Is_Type (E1) then
7360 -- Subtypes must statically match. E1, E2 are the local entities
7361 -- that are subtypes of the actuals. Itypes generated for other
7362 -- parameters need not be checked, the check will be performed
7363 -- on the parameters themselves.
7365 -- If E2 is a formal type declaration, it is a defaulted parameter
7366 -- and needs no checking.
7368 if not Is_Itype (E1) and then not Is_Itype (E2) then
7371 or else Etype (E1) /= Etype (E2)
7372 or else not Subtypes_Statically_Match (E1, E2));
7375 elsif Ekind (E1) = E_Constant then
7377 -- IN parameters must denote the same static value, or the same
7378 -- constant, or the literal null.
7380 Expr1 := Expression (Parent (E1));
7382 if Ekind (E2) /= E_Constant then
7383 Check_Mismatch (True);
7386 Expr2 := Expression (Parent (E2));
7389 if Is_OK_Static_Expression (Expr1) then
7390 if not Is_OK_Static_Expression (Expr2) then
7391 Check_Mismatch (True);
7393 elsif Is_Discrete_Type (Etype (E1)) then
7395 V1 : constant Uint := Expr_Value (Expr1);
7396 V2 : constant Uint := Expr_Value (Expr2);
7398 Check_Mismatch (V1 /= V2);
7401 elsif Is_Real_Type (Etype (E1)) then
7403 V1 : constant Ureal := Expr_Value_R (Expr1);
7404 V2 : constant Ureal := Expr_Value_R (Expr2);
7406 Check_Mismatch (V1 /= V2);
7409 elsif Is_String_Type (Etype (E1))
7410 and then Nkind (Expr1) = N_String_Literal
7412 if Nkind (Expr2) /= N_String_Literal then
7413 Check_Mismatch (True);
7416 (not String_Equal (Strval (Expr1), Strval (Expr2)));
7420 elsif Is_Entity_Name (Expr1) then
7421 if Is_Entity_Name (Expr2) then
7422 if Entity (Expr1) = Entity (Expr2) then
7426 (not Same_Instantiated_Constant
7427 (Entity (Expr1), Entity (Expr2)));
7431 Check_Mismatch (True);
7434 elsif Is_Entity_Name (Original_Node (Expr1))
7435 and then Is_Entity_Name (Expr2)
7436 and then Same_Instantiated_Constant
7437 (Entity (Original_Node (Expr1)), Entity (Expr2))
7441 elsif Nkind (Expr1) = N_Null then
7442 Check_Mismatch (Nkind (Expr1) /= N_Null);
7445 Check_Mismatch (True);
7448 elsif Ekind (E1) = E_Variable then
7449 Check_Mismatch (not Same_Instantiated_Variable (E1, E2));
7451 elsif Ekind (E1) = E_Package then
7453 (Ekind (E1) /= Ekind (E2)
7454 or else (Present (Renamed_Entity (E2))
7455 and then Renamed_Entity (E1) /=
7456 Renamed_Entity (E2)));
7458 elsif Is_Overloadable (E1) then
7459 -- Verify that the actual subprograms match. Note that actuals
7460 -- that are attributes are rewritten as subprograms. If the
7461 -- subprogram in the formal package is defaulted, no check is
7462 -- needed. Note that this can only happen in Ada 2005 when the
7463 -- formal package can be partially parameterized.
7465 if Nkind (Unit_Declaration_Node (E1)) =
7466 N_Subprogram_Renaming_Declaration
7467 and then From_Default (Unit_Declaration_Node (E1))
7471 -- If the formal package has an "others" box association that
7472 -- covers this formal, there is no need for a check either.
7474 elsif Nkind (Unit_Declaration_Node (E2)) in
7475 N_Formal_Subprogram_Declaration
7476 and then Box_Present (Unit_Declaration_Node (E2))
7480 -- No check needed if subprogram is a defaulted null procedure
7482 elsif No (Alias (E2))
7483 and then Ekind (E2) = E_Procedure
7485 Null_Present (Specification (Unit_Declaration_Node (E2)))
7489 -- Otherwise the actual in the formal and the actual in the
7490 -- instantiation of the formal must match, up to renamings.
7494 (Ekind (E2) /= Ekind (E1)
7495 or else not Same_Instantiated_Function (E1, E2));
7499 raise Program_Error;
7507 end Check_Formal_Package_Instance;
7509 -------------------------------
7510 -- Check_Fixed_Point_Warning --
7511 -------------------------------
7513 procedure Check_Fixed_Point_Warning
7514 (Gen_Assocs : Associations.Gen_Assocs_Rec;
7515 Renamings : List_Id)
7519 for Type_Index in Gen_Assocs.Assocs'Range loop
7521 Assoc : Assoc_Rec renames Gen_Assocs.Assocs (Type_Index);
7523 if Nkind (Assoc.An_Formal) = N_Formal_Type_Declaration
7524 and then Is_Fixed_Point_Type (Defining_Entity (Assoc.An_Formal))
7525 and then Assoc.Actual.Kind = Name_Exp
7528 Typ : constant Entity_Id := Entity (Assoc.Actual.Name_Exp);
7529 pragma Assert (Is_Fixed_Point_Type (Typ));
7531 Prims : constant Elist_Id :=
7532 Collect_Primitive_Operations (Typ);
7533 Elem : Elmt_Id := First_Elmt (Prims);
7537 -- Locate primitive operations of the type that are
7538 -- arithmetic operations.
7540 while Present (Elem) loop
7541 if Nkind (Node (Elem)) = N_Defining_Operator_Symbol then
7543 -- Check whether the generic unit has a formal
7544 -- subprogram of the same name. This does not check
7545 -- types but is good enough to justify a warning.
7547 Op := Alias (Node (Elem));
7549 for Op_Index in Type_Index + 1 ..
7550 Gen_Assocs.Assocs'Last
7552 Formal := Gen_Assocs.Assocs (Op_Index).Un_Formal;
7555 N_Formal_Concrete_Subprogram_Declaration
7556 and then Chars (Defining_Entity (Formal)) =
7561 elsif Nkind (Formal) = N_Formal_Package_Declaration
7568 -- Locate corresponding actual, and check
7569 -- whether it includes a fixed-point type.
7571 Assoc := First (Renamings);
7572 while Present (Assoc) loop
7575 N_Package_Renaming_Declaration
7577 Chars (Defining_Unit_Name (Assoc)) =
7578 Chars (Defining_Identifier (Formal));
7583 if Present (Assoc) then
7584 -- If the formal package declares a
7585 -- fixed-point type, and the user-defined
7586 -- operator is derived from a generic
7587 -- instance package, the fixed-point type
7588 -- does not use the corresponding
7592 First_Entity (Entity (Name (Assoc)));
7593 while Present (Ent) loop
7594 if Is_Fixed_Point_Type (Ent)
7595 and then Present (Op)
7597 Is_Generic_Instance (Scope (Op))
7609 Error_Msg_Sloc := Sloc (Node (Elem));
7611 ("?instance uses predefined, not primitive, " &
7613 Assoc.Actual.Name_Exp, Node (Elem));
7623 end Check_Fixed_Point_Warning;
7625 ---------------------------
7626 -- Check_Formal_Packages --
7627 ---------------------------
7629 procedure Check_Formal_Packages (P_Id : Entity_Id) is
7631 Formal_P : Entity_Id;
7632 Formal_Decl : Node_Id;
7635 -- Iterate through the declarations in the instance, looking for package
7636 -- renaming declarations that denote instances of formal packages, until
7637 -- we find the renaming of the current package itself. The declaration
7638 -- of a formal package that requires conformance checking is followed by
7639 -- an internal entity that is the abbreviated instance.
7641 E := First_Entity (P_Id);
7642 while Present (E) loop
7643 if Ekind (E) = E_Package then
7644 exit when Renamed_Entity (E) = P_Id;
7646 if Nkind (Parent (E)) = N_Package_Renaming_Declaration then
7647 Formal_Decl := Parent (Associated_Formal_Package (E));
7649 if Requires_Conformance_Checking (Formal_Decl) then
7650 Formal_P := Next_Entity (E);
7652 -- If the instance is within an enclosing instance body
7653 -- there is no need to verify the legality of current formal
7654 -- packages because they were legal in the generic body.
7655 -- This optimization may be applicable elsewhere, and it
7656 -- also removes spurious errors that may arise with
7657 -- on-the-fly inlining and confusion between private and
7660 if not In_Instance_Body then
7661 Check_Formal_Package_Instance (Formal_P, E);
7664 -- Restore the visibility of formals of the formal instance
7665 -- that are not defaulted, and are hidden within the current
7666 -- generic. These formals may be visible within an enclosing
7672 Elmt := First_Elmt (Hidden_In_Formal_Instance (Formal_P));
7673 while Present (Elmt) loop
7674 Set_Is_Hidden (Node (Elmt), False);
7679 -- After checking, remove the internal validating package.
7680 -- It is only needed for semantic checks, and as it may
7681 -- contain generic formal declarations it should not reach
7684 Remove (Unit_Declaration_Node (Formal_P));
7691 end Check_Formal_Packages;
7693 ---------------------------------
7694 -- Check_Forward_Instantiation --
7695 ---------------------------------
7697 procedure Check_Forward_Instantiation (Decl : Node_Id) is
7699 Gen_Comp : Entity_Id := Cunit_Entity (Get_Source_Unit (Decl));
7702 -- The instantiation appears before the generic body if we are in the
7703 -- scope of the unit containing the generic, either in its spec or in
7704 -- the package body, and before the generic body.
7706 if Ekind (Gen_Comp) = E_Package_Body then
7707 Gen_Comp := Spec_Entity (Gen_Comp);
7710 if In_Open_Scopes (Gen_Comp)
7711 and then No (Corresponding_Body (Decl))
7716 and then not Is_Compilation_Unit (S)
7717 and then not Is_Child_Unit (S)
7719 if Ekind (S) = E_Package then
7720 Set_Has_Forward_Instantiation (S);
7726 end Check_Forward_Instantiation;
7728 ---------------------------
7729 -- Check_Generic_Actuals --
7730 ---------------------------
7732 -- The visibility of the actuals may be different between the point of
7733 -- generic instantiation and the instantiation of the body.
7735 procedure Check_Generic_Actuals
7736 (Instance : Entity_Id;
7737 Is_Formal_Box : Boolean)
7739 Gen_Id : constant Entity_Id :=
7740 (if Is_Generic_Unit (Instance) then
7742 elsif Is_Wrapper_Package (Instance) then
7745 (Unit_Declaration_Node (Related_Instance (Instance))))
7747 Generic_Parent (Package_Specification (Instance)));
7750 Parent_Scope : constant Entity_Id := Scope (Gen_Id);
7751 -- The enclosing scope of the generic unit
7753 procedure Check_Actual_Type (Typ : Entity_Id);
7754 -- If the type of the actual is a private type declared in the enclosing
7755 -- scope of the generic, either directly or through packages nested in
7756 -- bodies, but not a derived type of a private type declared elsewhere,
7757 -- then the body of the generic sees the full view of the type because
7758 -- it has to appear in the package body. If the type is private now then
7759 -- exchange views to restore the proper visibility in the instance.
7761 -----------------------
7762 -- Check_Actual_Type --
7763 -----------------------
7765 procedure Check_Actual_Type (Typ : Entity_Id) is
7766 Btyp : constant Entity_Id := Base_Type (Typ);
7768 function Scope_Within_Body_Or_Same
7770 Outer : Entity_Id) return Boolean;
7771 -- Determine whether scope Inner is within the body of scope Outer
7772 -- or is Outer itself.
7774 -------------------------------
7775 -- Scope_Within_Body_Or_Same --
7776 -------------------------------
7778 function Scope_Within_Body_Or_Same
7780 Outer : Entity_Id) return Boolean
7782 Curr : Entity_Id := Inner;
7785 while Curr /= Standard_Standard loop
7786 if Curr = Outer then
7789 elsif Is_Package_Body_Entity (Curr) then
7790 Curr := Scope (Curr);
7798 end Scope_Within_Body_Or_Same;
7800 -- Start of processing for Check_Actual_Type
7803 -- The exchange is only needed if the generic is defined
7804 -- within a package which is not a common ancestor of the
7805 -- scope of the instance, and is not already in scope.
7807 if Is_Private_Type (Btyp)
7808 and then not Has_Private_Ancestor (Btyp)
7809 and then Ekind (Parent_Scope) in E_Package | E_Generic_Package
7810 and then Scope_Within_Body_Or_Same (Parent_Scope, Scope (Btyp))
7811 and then Parent_Scope /= Scope (Instance)
7812 and then not Is_Child_Unit (Gen_Id)
7816 -- If the type of the entity is a subtype, it may also have
7817 -- to be made visible, together with the base type of its
7818 -- full view, after exchange.
7820 if Is_Private_Type (Typ) then
7822 Switch_View (Base_Type (Typ));
7825 end Check_Actual_Type;
7833 -- Start of processing for Check_Generic_Actuals
7836 E := First_Entity (Instance);
7837 while Present (E) loop
7839 and then Nkind (Parent (E)) = N_Subtype_Declaration
7840 and then Scope (Etype (E)) /= Instance
7841 and then Is_Entity_Name (Subtype_Indication (Parent (E)))
7844 Indic : constant Node_Id := Subtype_Indication (Parent (E));
7847 -- Restore the proper view of the actual from the information
7848 -- saved earlier by Instantiate_Type.
7850 Check_Private_View (Indic);
7852 -- If this view is an array type, check its component type.
7853 -- This handles the case of an array type whose component
7854 -- type is private, used as the actual in an instantiation
7855 -- of a generic construct declared in the same package as
7856 -- the component type and taking an array type with this
7857 -- component type as formal type parameter.
7859 if Is_Array_Type (Etype (Indic)) then
7861 (Component_Type_For_Private_View (Etype (Indic)));
7865 -- If the actual is itself the formal of a parent instance,
7866 -- then also restore the proper view of its actual and so on.
7867 -- That's necessary for nested instantiations of the form
7870 -- type Component is private;
7871 -- type Array_Type is array (Positive range <>) of Component;
7874 -- when the outermost actuals have inconsistent views, because
7875 -- the Component_Type of Array_Type of the inner instantiations
7876 -- is the actual of Component of the outermost one and not that
7877 -- of the corresponding inner instantiations.
7879 Astype := Ancestor_Subtype (E);
7880 while Present (Astype)
7881 and then Nkind (Parent (Astype)) = N_Subtype_Declaration
7882 and then Present (Generic_Parent_Type (Parent (Astype)))
7883 and then Is_Entity_Name (Subtype_Indication (Parent (Astype)))
7885 Check_Private_View (Subtype_Indication (Parent (Astype)));
7886 Astype := Ancestor_Subtype (Astype);
7889 Set_Is_Generic_Actual_Type (E);
7891 if Is_Private_Type (E) and then Present (Full_View (E)) then
7892 Set_Is_Generic_Actual_Type (Full_View (E));
7895 Set_Is_Hidden (E, False);
7896 Set_Is_Potentially_Use_Visible (E, In_Use (Instance));
7898 -- We constructed the generic actual type as a subtype of the
7899 -- supplied type. This means that it normally would not inherit
7900 -- subtype specific attributes of the actual, which is wrong for
7901 -- the generic case.
7903 Astype := Ancestor_Subtype (E);
7907 -- This can happen when E is an itype that is the full view of
7908 -- a private type completed, e.g. with a constrained array. In
7909 -- that case, use the first subtype, which will carry size
7910 -- information. The base type itself is unconstrained and will
7913 Astype := First_Subtype (E);
7916 Set_Size_Info (E, Astype);
7917 Copy_RM_Size (To => E, From => Astype);
7918 Set_First_Rep_Item (E, First_Rep_Item (Astype));
7920 if Is_Discrete_Or_Fixed_Point_Type (E) then
7921 Set_RM_Size (E, RM_Size (Astype));
7924 elsif Ekind (E) = E_Package then
7926 -- If this is the renaming for the current instance, we're done.
7927 -- Otherwise it is a formal package. If the corresponding formal
7928 -- was declared with a box, the (instantiations of the) generic
7929 -- formal part are also visible. Otherwise, ignore the entity
7930 -- created to validate the actuals.
7932 if Renamed_Entity (E) = Instance then
7935 elsif Nkind (Parent (E)) /= N_Package_Renaming_Declaration then
7938 -- The visibility of a formal of an enclosing generic is already
7941 elsif Denotes_Formal_Package (E) then
7944 elsif Present (Associated_Formal_Package (E))
7945 and then not Is_Generic_Formal (E)
7947 Check_Generic_Actuals
7948 (Renamed_Entity (E),
7950 Box_Present (Parent (Associated_Formal_Package (E))));
7952 Set_Is_Hidden (E, False);
7955 -- If this is a subprogram instance (in a wrapper package) the
7956 -- actual is fully visible.
7958 elsif Is_Wrapper_Package (Instance) then
7959 Set_Is_Hidden (E, False);
7961 -- If the formal package is declared with a box, or if the formal
7962 -- parameter is defaulted, it is visible in the body.
7964 elsif Is_Formal_Box or else Is_Visible_Formal (E) then
7965 Set_Is_Hidden (E, False);
7968 -- Check directly the type of the actual objects, including the
7969 -- component type for array types.
7971 if Ekind (E) in E_Constant | E_Variable then
7972 Check_Actual_Type (Etype (E));
7974 if Is_Array_Type (Etype (E)) then
7975 Check_Actual_Type (Component_Type (Etype (E)));
7978 -- As well as the type of formal parameters of actual subprograms
7980 elsif Ekind (E) in E_Function | E_Procedure
7981 and then Is_Generic_Actual_Subprogram (E)
7982 and then Present (Alias (E))
7984 Formal := First_Formal (Alias (E));
7985 while Present (Formal) loop
7986 Check_Actual_Type (Etype (Formal));
7987 Next_Formal (Formal);
7993 end Check_Generic_Actuals;
7995 ------------------------------
7996 -- Check_Generic_Child_Unit --
7997 ------------------------------
7999 procedure Check_Generic_Child_Unit
8001 Parent_Installed : in out Boolean)
8003 Loc : constant Source_Ptr := Sloc (Gen_Id);
8004 Gen_Par : Entity_Id := Empty;
8006 Inst_Par : Entity_Id := Empty;
8009 function Find_Generic_Child
8011 Id : Node_Id) return Entity_Id;
8012 -- Search generic parent for possible child unit with the given name
8014 function In_Enclosing_Instance return Boolean;
8015 -- Within an instance of the parent, the child unit may be denoted by
8016 -- a simple name, or an abbreviated expanded name. Examine enclosing
8017 -- scopes to locate a possible parent instantiation.
8019 ------------------------
8020 -- Find_Generic_Child --
8021 ------------------------
8023 function Find_Generic_Child
8025 Id : Node_Id) return Entity_Id
8030 -- If entity of name is already set, instance has already been
8031 -- resolved, e.g. in an enclosing instantiation.
8033 if Present (Entity (Id)) then
8034 if Scope (Entity (Id)) = Scop then
8041 E := First_Entity (Scop);
8042 while Present (E) loop
8043 if Chars (E) = Chars (Id)
8044 and then Is_Child_Unit (E)
8046 if Is_Child_Unit (E)
8047 and then not Is_Visible_Lib_Unit (E)
8050 ("generic child unit& is not visible", Gen_Id, E);
8062 end Find_Generic_Child;
8064 ---------------------------
8065 -- In_Enclosing_Instance --
8066 ---------------------------
8068 function In_Enclosing_Instance return Boolean is
8069 Enclosing_Instance : Node_Id;
8070 Instance_Decl : Node_Id;
8073 -- We do not inline any call that contains instantiations, except
8074 -- for instantiations of Unchecked_Conversion, so if we are within
8075 -- an inlined body the current instance does not require parents.
8077 if In_Inlined_Body then
8078 pragma Assert (Chars (Gen_Id) = Name_Unchecked_Conversion);
8082 -- Loop to check enclosing scopes
8084 Enclosing_Instance := Current_Scope;
8085 while Present (Enclosing_Instance) loop
8086 Instance_Decl := Unit_Declaration_Node (Enclosing_Instance);
8088 if Ekind (Enclosing_Instance) = E_Package
8089 and then Is_Generic_Instance (Enclosing_Instance)
8091 (Generic_Parent (Specification (Instance_Decl)))
8093 -- Check whether the generic we are looking for is a child of
8096 E := Find_Generic_Child
8097 (Generic_Parent (Specification (Instance_Decl)), Gen_Id);
8098 exit when Present (E);
8104 Enclosing_Instance := Scope (Enclosing_Instance);
8116 Make_Expanded_Name (Loc,
8118 Prefix => New_Occurrence_Of (Enclosing_Instance, Loc),
8119 Selector_Name => New_Occurrence_Of (E, Loc)));
8121 Set_Entity (Gen_Id, E);
8122 Set_Etype (Gen_Id, Etype (E));
8123 Parent_Installed := False; -- Already in scope.
8126 end In_Enclosing_Instance;
8128 -- Start of processing for Check_Generic_Child_Unit
8131 -- If the name of the generic is given by a selected component, it may
8132 -- be the name of a generic child unit, and the prefix is the name of an
8133 -- instance of the parent, in which case the child unit must be visible.
8134 -- If this instance is not in scope, it must be placed there and removed
8135 -- after instantiation, because what is being instantiated is not the
8136 -- original child, but the corresponding child present in the instance
8139 -- If the child is instantiated within the parent, it can be given by
8140 -- a simple name. In this case the instance is already in scope, but
8141 -- the child generic must be recovered from the generic parent as well.
8143 if Nkind (Gen_Id) = N_Selected_Component then
8144 S := Selector_Name (Gen_Id);
8145 Analyze (Prefix (Gen_Id));
8146 Inst_Par := Entity (Prefix (Gen_Id));
8148 if Ekind (Inst_Par) = E_Package
8149 and then Present (Renamed_Entity (Inst_Par))
8151 Inst_Par := Renamed_Entity (Inst_Par);
8154 if Ekind (Inst_Par) = E_Package then
8155 if Nkind (Parent (Inst_Par)) = N_Package_Specification then
8156 Gen_Par := Generic_Parent (Parent (Inst_Par));
8158 elsif Nkind (Parent (Inst_Par)) = N_Defining_Program_Unit_Name
8160 Nkind (Parent (Parent (Inst_Par))) = N_Package_Specification
8162 Gen_Par := Generic_Parent (Parent (Parent (Inst_Par)));
8165 elsif Ekind (Inst_Par) = E_Generic_Package
8166 and then Nkind (Parent (Gen_Id)) = N_Formal_Package_Declaration
8168 -- A formal package may be a real child package, and not the
8169 -- implicit instance within a parent. In this case the child is
8170 -- not visible and has to be retrieved explicitly as well.
8172 Gen_Par := Inst_Par;
8175 if Present (Gen_Par) then
8177 -- The prefix denotes an instantiation. The entity itself may be a
8178 -- nested generic, or a child unit.
8180 E := Find_Generic_Child (Gen_Par, S);
8183 Change_Selected_Component_To_Expanded_Name (Gen_Id);
8184 Set_Entity (Gen_Id, E);
8185 Set_Etype (Gen_Id, Etype (E));
8187 Set_Etype (S, Etype (E));
8189 -- Indicate that this is a reference to the parent
8191 if In_Extended_Main_Source_Unit (Gen_Id) then
8192 Set_Is_Instantiated (Inst_Par);
8195 -- A common mistake is to replicate the naming scheme of a
8196 -- hierarchy by instantiating a generic child directly, rather
8197 -- than the implicit child in a parent instance:
8199 -- generic .. package Gpar is ..
8200 -- generic .. package Gpar.Child is ..
8201 -- package Par is new Gpar ();
8204 -- package Par.Child is new Gpar.Child ();
8205 -- rather than Par.Child
8207 -- In this case the instantiation is within Par, which is an
8208 -- instance, but Gpar does not denote Par because we are not IN
8209 -- the instance of Gpar, so this is illegal. The test below
8210 -- recognizes this particular case.
8213 -- We want to reject the final instantiation in
8214 -- generic package G1 is end G1;
8215 -- generic package G1.G2 is end G1.G2;
8216 -- with G1; package I1 is new G1;
8217 -- with G1.G2; package I1.I2 is new G1.G2;
8218 -- because the use of G1.G2 should instead be either
8219 -- I1.G2 or simply G2. However, the tree that is built
8220 -- in this case is wrong. In the expanded copy
8221 -- of G2, we need (and therefore generate) a renaming
8222 -- package G1 renames I1;
8223 -- but this renaming should not participate in resolving
8224 -- this occurrence of the name "G1.G2"; unfortunately,
8225 -- it does. Rather than correct this error, we compensate
8226 -- for it in this function.
8228 -- We also perform another adjustment here. If we are
8229 -- currently inside a generic package, then that
8230 -- generic package needs to be treated as a package.
8231 -- For example, if a generic Aaa declares a nested generic
8232 -- Bbb (perhaps as a child unit) then Aaa can also legally
8233 -- declare an instance of Aaa.Bbb.
8235 function Adjusted_Inst_Par_Ekind return Entity_Kind;
8237 -----------------------------
8238 -- Adjusted_Inst_Par_Ekind --
8239 -----------------------------
8241 function Adjusted_Inst_Par_Ekind return Entity_Kind is
8242 Prefix_Entity : Entity_Id;
8243 Inst_Par_GP : Node_Id;
8244 Inst_Par_Parent : Node_Id := Parent (Inst_Par);
8246 if Nkind (Inst_Par_Parent) = N_Defining_Program_Unit_Name
8248 Inst_Par_Parent := Parent (Inst_Par_Parent);
8251 Inst_Par_GP := Generic_Parent (Inst_Par_Parent);
8253 if Nkind (Gen_Id) = N_Expanded_Name
8254 and then Present (Inst_Par_GP)
8255 and then Ekind (Inst_Par_GP) = E_Generic_Package
8257 Prefix_Entity := Entity (Prefix (Gen_Id));
8259 if Present (Prefix_Entity)
8260 and then not Comes_From_Source (Prefix_Entity)
8261 and then Nkind (Parent (Prefix_Entity)) =
8262 N_Package_Renaming_Declaration
8263 and then Chars (Prefix_Entity) = Chars (Inst_Par_GP)
8265 return E_Generic_Package;
8269 if Ekind (Inst_Par) = E_Generic_Package
8270 and then In_Open_Scopes (Inst_Par)
8272 -- If we are inside a generic package then
8273 -- treat it as a package.
8278 return Ekind (Inst_Par);
8279 end Adjusted_Inst_Par_Ekind;
8282 if Is_Child_Unit (E)
8283 and then (No (Inst_Par)
8284 or else Adjusted_Inst_Par_Ekind =
8286 and then (not In_Instance
8287 or else Nkind (Parent (Parent (Gen_Id))) =
8291 ("prefix of generic child unit must be " &
8292 "instance of parent",
8297 if not In_Open_Scopes (Inst_Par)
8298 and then Nkind (Parent (Gen_Id)) not in
8299 N_Generic_Renaming_Declaration
8301 Install_Parent (Inst_Par);
8302 Parent_Installed := True;
8304 elsif In_Open_Scopes (Inst_Par) then
8306 -- If the parent is already installed, install the actuals
8307 -- for its formal packages. This is necessary when the child
8308 -- instance is a child of the parent instance: in this case,
8309 -- the parent is placed on the scope stack but the formal
8310 -- packages are not made visible.
8312 Install_Formal_Packages (Inst_Par);
8316 -- If the generic parent does not contain an entity that
8317 -- corresponds to the selector, the instance doesn't either.
8318 -- Analyzing the node will yield the appropriate error message.
8319 -- If the entity is not a child unit, then it is an inner
8320 -- generic in the parent.
8328 if Is_Child_Unit (Entity (Gen_Id))
8330 Nkind (Parent (Gen_Id)) not in N_Generic_Renaming_Declaration
8331 and then not In_Open_Scopes (Inst_Par)
8333 Install_Parent (Inst_Par);
8334 Parent_Installed := True;
8336 -- The generic unit may be the renaming of the implicit child
8337 -- present in an instance. In that case the parent instance is
8338 -- obtained from the name of the renamed entity.
8340 elsif Ekind (Entity (Gen_Id)) = E_Generic_Package
8341 and then Present (Renamed_Entity (Entity (Gen_Id)))
8342 and then Is_Child_Unit (Renamed_Entity (Entity (Gen_Id)))
8345 Renamed_Package : constant Node_Id :=
8346 Name (Parent (Entity (Gen_Id)));
8348 if Nkind (Renamed_Package) = N_Expanded_Name then
8349 Inst_Par := Entity (Prefix (Renamed_Package));
8350 Install_Parent (Inst_Par);
8351 Parent_Installed := True;
8357 elsif Nkind (Gen_Id) = N_Expanded_Name then
8359 -- Entity already present, analyze prefix, whose meaning may be an
8360 -- instance in the current context. If it is an instance of a
8361 -- relative within another, the proper parent may still have to be
8362 -- installed, if they are not of the same generation.
8364 Analyze (Prefix (Gen_Id));
8366 -- Prevent cascaded errors
8368 if Etype (Prefix (Gen_Id)) = Any_Type then
8372 -- In the unlikely case that a local declaration hides the name of
8373 -- the parent package, locate it on the homonym chain. If the context
8374 -- is an instance of the parent, the renaming entity is flagged as
8377 Inst_Par := Entity (Prefix (Gen_Id));
8378 while Present (Inst_Par)
8379 and then not Is_Package_Or_Generic_Package (Inst_Par)
8381 Inst_Par := Homonym (Inst_Par);
8384 pragma Assert (Present (Inst_Par));
8385 Set_Entity (Prefix (Gen_Id), Inst_Par);
8387 if In_Enclosing_Instance then
8390 elsif Present (Entity (Gen_Id))
8391 and then No (Renamed_Entity (Entity (Gen_Id)))
8392 and then Is_Child_Unit (Entity (Gen_Id))
8393 and then not In_Open_Scopes (Inst_Par)
8395 Install_Parent (Inst_Par);
8396 Parent_Installed := True;
8398 -- Handle renaming of generic child unit
8400 elsif Present (Entity (Gen_Id))
8401 and then Present (Renamed_Entity (Entity (Gen_Id)))
8402 and then Is_Child_Unit (Renamed_Entity (Entity (Gen_Id)))
8409 -- The entity of the renamed generic child unit does not
8410 -- have any reference to the instantiated parent. In order to
8411 -- locate it we traverse the scope containing the renaming
8412 -- declaration; the instance of the parent is available in
8413 -- the prefix of the renaming declaration. For example:
8416 -- package Inst_Par is new ...
8417 -- generic package Ren_Child renames Ins_Par.Child;
8422 -- package Inst_Child is new A.Ren_Child;
8425 E := First_Entity (Entity (Prefix (Gen_Id)));
8426 while Present (E) loop
8427 if not Is_Object (E)
8428 and then Present (Renamed_Entity (E))
8430 Renamed_Entity (E) = Renamed_Entity (Entity (Gen_Id))
8432 Ren_Decl := Parent (E);
8433 Inst_Par := Entity (Prefix (Name (Ren_Decl)));
8435 if not In_Open_Scopes (Inst_Par) then
8436 Install_Parent (Inst_Par);
8437 Parent_Installed := True;
8443 E := Next_Entity (E);
8448 elsif In_Enclosing_Instance then
8450 -- The child unit is found in some enclosing scope
8457 -- If this is the renaming of the implicit child in a parent
8458 -- instance, recover the parent name and install it.
8460 if Is_Entity_Name (Gen_Id) then
8461 E := Entity (Gen_Id);
8463 if Is_Generic_Unit (E)
8464 and then Nkind (Parent (E)) in N_Generic_Renaming_Declaration
8465 and then Is_Child_Unit (Renamed_Entity (E))
8466 and then Is_Generic_Unit (Scope (Renamed_Entity (E)))
8467 and then Nkind (Name (Parent (E))) = N_Expanded_Name
8469 Rewrite (Gen_Id, New_Copy_Tree (Name (Parent (E))));
8470 Inst_Par := Entity (Prefix (Gen_Id));
8472 if not In_Open_Scopes (Inst_Par) then
8473 Install_Parent (Inst_Par);
8474 Parent_Installed := True;
8477 -- If it is a child unit of a non-generic parent, it may be
8478 -- use-visible and given by a direct name. Install parent as
8481 elsif Is_Generic_Unit (E)
8482 and then Is_Child_Unit (E)
8484 Nkind (Parent (Gen_Id)) not in N_Generic_Renaming_Declaration
8485 and then not Is_Generic_Unit (Scope (E))
8487 if not In_Open_Scopes (Scope (E)) then
8488 Install_Parent (Scope (E));
8489 Parent_Installed := True;
8494 end Check_Generic_Child_Unit;
8496 -----------------------------
8497 -- Check_Hidden_Child_Unit --
8498 -----------------------------
8500 procedure Check_Hidden_Child_Unit
8502 Gen_Unit : Entity_Id;
8503 Act_Decl_Id : Entity_Id)
8505 Gen_Id : constant Node_Id := Name (N);
8508 if Is_Child_Unit (Gen_Unit)
8509 and then Is_Child_Unit (Act_Decl_Id)
8510 and then Nkind (Gen_Id) = N_Expanded_Name
8511 and then Entity (Prefix (Gen_Id)) = Scope (Act_Decl_Id)
8512 and then Chars (Gen_Unit) = Chars (Act_Decl_Id)
8514 Error_Msg_Node_2 := Scope (Act_Decl_Id);
8516 ("generic unit & is implicitly declared in &",
8517 Defining_Unit_Name (N), Gen_Unit);
8518 Error_Msg_N ("\instance must have different name",
8519 Defining_Unit_Name (N));
8521 end Check_Hidden_Child_Unit;
8523 ------------------------
8524 -- Check_Private_View --
8525 ------------------------
8527 procedure Check_Private_View (N : Node_Id) is
8528 Comparison : constant Boolean := Nkind (N) in N_Op_Compare;
8529 Typ : constant Entity_Id :=
8530 (if Comparison then Compare_Type (N) else Etype (N));
8532 procedure Check_Private_Type (T : Entity_Id; Private_View : Boolean);
8533 -- Check that the available view of T matches Private_View and, if not,
8534 -- switch the view of T or of its base type.
8536 procedure Check_Private_Type (T : Entity_Id; Private_View : Boolean) is
8537 BT : constant Entity_Id := Base_Type (T);
8540 -- If the full declaration was not visible in the generic, stop here
8542 if Private_View then
8546 -- Exchange views if the type was not private in the generic but is
8547 -- private at the point of instantiation. Do not exchange views if
8548 -- the scope of the type is in scope. This can happen if both generic
8549 -- and instance are sibling units, or if type is defined in a parent.
8550 -- In this case the visibility of the type will be correct for all
8553 if Is_Private_Type (T)
8554 and then Present (Full_View (T))
8555 and then not In_Open_Scopes (Scope (T))
8559 -- Finally, a nonprivate subtype may have a private base type, which
8560 -- must be exchanged for consistency. This can happen when a package
8561 -- body is instantiated, when the scope stack is empty but in fact
8562 -- the subtype and the base type are declared in an enclosing scope.
8564 -- Note that in this case we introduce an inconsistency in the view
8565 -- set, because we switch the base type BT, but there could be some
8566 -- private dependent subtypes of BT which remain unswitched. Such
8567 -- subtypes might need to be switched at a later point (see specific
8568 -- provision for that case in Switch_View).
8570 elsif not Is_Private_Type (T)
8571 and then Is_Private_Type (BT)
8572 and then Present (Full_View (BT))
8573 and then not In_Open_Scopes (BT)
8575 Prepend_Elmt (Full_View (BT), Exchanged_Views);
8576 Exchange_Declarations (BT);
8578 end Check_Private_Type;
8580 -- Start of processing for Check_Private_View
8583 if Present (Typ) then
8584 -- If the type appears in a subtype declaration, the subtype in
8585 -- instance must have a view compatible with that of its parent,
8586 -- which must be exchanged (see corresponding code in Restore_
8587 -- Private_Views) so we make an exception to the open scope rule
8588 -- implemented by Check_Private_Type above.
8590 if Has_Private_View (N)
8591 and then not Is_Private_Type (Typ)
8592 and then not Has_Been_Exchanged (Typ)
8593 and then (not In_Open_Scopes (Scope (Typ))
8594 or else Nkind (Parent (N)) = N_Subtype_Declaration)
8597 Assoc : constant Node_Id := Get_Associated_Node (N);
8600 -- In the generic, only the private declaration was visible
8602 Prepend_Elmt (Typ, Exchanged_Views);
8603 Exchange_Declarations
8604 (if Comparison then Compare_Type (Assoc) else Etype (Assoc));
8607 -- Check that the available views of Typ match their respective flag.
8608 -- Note that the type of a visible discriminant is never private.
8611 Check_Private_Type (Typ, Has_Private_View (N));
8613 if Is_Access_Type (Typ) then
8615 (Designated_Type (Typ), Has_Secondary_Private_View (N));
8617 elsif Is_Array_Type (Typ) then
8619 (Component_Type_For_Private_View (Typ),
8620 Has_Secondary_Private_View (N));
8622 elsif (Is_Record_Type (Typ) or else Is_Concurrent_Type (Typ))
8623 and then Has_Discriminants (Typ)
8629 Disc := First_Discriminant (Typ);
8630 while Present (Disc) loop
8631 Check_Private_Type (Etype (Disc), False);
8632 Next_Discriminant (Disc);
8638 end Check_Private_View;
8640 -----------------------------
8641 -- Check_Hidden_Primitives --
8642 -----------------------------
8644 function Check_Hidden_Primitives (Renamings : List_Id) return Elist_Id is
8647 Result : Elist_Id := No_Elist;
8650 if No (Renamings) then
8654 -- Traverse the list of associations between formals and actuals
8655 -- searching for renamings of tagged types
8657 Actual := First (Renamings);
8658 while Present (Actual) loop
8659 if Nkind (Actual) = N_Subtype_Declaration then
8660 Gen_T := Generic_Parent_Type (Actual);
8662 if Present (Gen_T) and then Is_Tagged_Type (Gen_T) then
8664 -- Traverse the list of primitives of the actual types
8665 -- searching for hidden primitives that are visible in the
8666 -- corresponding generic formal; leave them visible and
8667 -- append them to Result to restore their decoration later.
8669 Install_Hidden_Primitives
8670 (Prims_List => Result,
8672 Act_T => Entity (Subtype_Indication (Actual)));
8680 end Check_Hidden_Primitives;
8682 -------------------------------------
8683 -- Component_Type_For_Private_View --
8684 -------------------------------------
8686 function Component_Type_For_Private_View (T : Entity_Id) return Entity_Id is
8687 Typ : constant Entity_Id := Component_Type (T);
8690 if Is_Array_Type (Typ) and then not Has_Private_Declaration (Typ) then
8691 return Component_Type_For_Private_View (Typ);
8695 end Component_Type_For_Private_View;
8697 --------------------------
8698 -- Contains_Instance_Of --
8699 --------------------------
8701 function Contains_Instance_Of
8704 N : Node_Id) return Boolean
8712 -- Verify that there are no circular instantiations. We check whether
8713 -- the unit contains an instance of the current scope or some enclosing
8714 -- scope (in case one of the instances appears in a subunit). Longer
8715 -- circularities involving subunits might seem too pathological to
8716 -- consider, but they were not too pathological for the authors of
8717 -- DEC bc30vsq, so we loop over all enclosing scopes, and mark all
8718 -- enclosing generic scopes as containing an instance.
8721 -- Within a generic subprogram body, the scope is not generic, to
8722 -- allow for recursive subprograms. Use the declaration to determine
8723 -- whether this is a generic unit.
8725 if Ekind (Scop) = E_Generic_Package
8726 or else (Is_Subprogram (Scop)
8727 and then Nkind (Unit_Declaration_Node (Scop)) =
8728 N_Generic_Subprogram_Declaration)
8730 Elmt := First_Elmt (Inner_Instances (Inner));
8732 while Present (Elmt) loop
8733 if Node (Elmt) = Scop then
8734 Error_Msg_Node_2 := Inner;
8736 ("circular instantiation: & instantiated within &!",
8740 elsif Node (Elmt) = Inner then
8743 elsif Contains_Instance_Of (Node (Elmt), Scop, N) then
8744 Error_Msg_Node_2 := Inner;
8746 ("circular instantiation: & instantiated within &!",
8754 -- Indicate that Inner is being instantiated within Scop
8756 Append_Elmt (Inner, Inner_Instances (Scop));
8759 if Scop = Standard_Standard then
8762 Scop := Scope (Scop);
8767 end Contains_Instance_Of;
8769 -----------------------
8770 -- Copy_Generic_Node --
8771 -----------------------
8773 function Copy_Generic_Node
8775 Parent_Id : Node_Id;
8776 Instantiating : Boolean) return Node_Id
8781 function Copy_Generic_Descendant (D : Union_Id) return Union_Id;
8782 -- Check the given value of one of the Fields referenced by the current
8783 -- node to determine whether to copy it recursively. The field may hold
8784 -- a Node_Id, a List_Id, or an Elist_Id, or a plain value (Sloc, Uint,
8785 -- Char) in which case it need not be copied.
8787 procedure Copy_Descendants;
8788 -- Common utility for various nodes
8790 function Copy_Generic_Elist (E : Elist_Id) return Elist_Id;
8791 -- Make copy of element list
8793 function Copy_Generic_List
8795 Parent_Id : Node_Id) return List_Id;
8796 -- Apply Copy_Generic_Node recursively to the members of a node list
8798 function In_Defining_Unit_Name (Nam : Node_Id) return Boolean;
8799 -- True if an identifier is part of the defining program unit name of
8801 -- Consider removing this subprogram now that ASIS no longer uses it.
8803 ----------------------
8804 -- Copy_Descendants --
8805 ----------------------
8807 procedure Copy_Descendants is
8808 procedure Walk is new
8809 Walk_Sinfo_Fields_Pairwise (Copy_Generic_Descendant);
8812 end Copy_Descendants;
8814 -----------------------------
8815 -- Copy_Generic_Descendant --
8816 -----------------------------
8818 function Copy_Generic_Descendant (D : Union_Id) return Union_Id is
8820 if D = Union_Id (Empty) then
8823 elsif D in Node_Range then
8825 (Copy_Generic_Node (Node_Id (D), New_N, Instantiating));
8827 elsif D in List_Range then
8828 return Union_Id (Copy_Generic_List (List_Id (D), New_N));
8830 elsif D in Elist_Range then
8831 return Union_Id (Copy_Generic_Elist (Elist_Id (D)));
8833 -- Nothing else is copyable (e.g. Uint values), return as is
8838 end Copy_Generic_Descendant;
8840 ------------------------
8841 -- Copy_Generic_Elist --
8842 ------------------------
8844 function Copy_Generic_Elist (E : Elist_Id) return Elist_Id is
8851 M := First_Elmt (E);
8852 while Present (M) loop
8854 (Copy_Generic_Node (Node (M), Empty, Instantiating), L);
8863 end Copy_Generic_Elist;
8865 -----------------------
8866 -- Copy_Generic_List --
8867 -----------------------
8869 function Copy_Generic_List
8871 Parent_Id : Node_Id) return List_Id
8879 Set_Parent (New_L, Parent_Id);
8882 while Present (N) loop
8883 Append (Copy_Generic_Node (N, Empty, Instantiating), New_L);
8892 end Copy_Generic_List;
8894 ---------------------------
8895 -- In_Defining_Unit_Name --
8896 ---------------------------
8898 function In_Defining_Unit_Name (Nam : Node_Id) return Boolean is
8901 Present (Parent (Nam))
8902 and then (Nkind (Parent (Nam)) = N_Defining_Program_Unit_Name
8904 (Nkind (Parent (Nam)) = N_Expanded_Name
8905 and then In_Defining_Unit_Name (Parent (Nam))));
8906 end In_Defining_Unit_Name;
8908 -- Start of processing for Copy_Generic_Node
8915 New_N := New_Copy (N);
8917 -- If we are instantiating, we want to adjust the sloc based on the
8918 -- current S_Adjustment. However, if this is the root node of a subunit,
8919 -- we need to defer that adjustment to below (see "elsif Instantiating
8920 -- and Was_Stub"), so it comes after Create_Instantiation_Source has
8921 -- computed the adjustment.
8924 and then not (Nkind (N) in N_Proper_Body
8925 and then Was_Originally_Stub (N))
8927 Adjust_Instantiation_Sloc (New_N, S_Adjustment);
8930 if not Is_List_Member (N) then
8931 Set_Parent (New_N, Parent_Id);
8934 -- Special casing for identifiers and other entity names and operators
8936 if Nkind (N) in N_Character_Literal
8942 if not Instantiating then
8944 -- Link both nodes in order to assign subsequently the entity of
8945 -- the copy to the original node, in case this is a global
8948 Set_Associated_Node (N, New_N);
8950 -- If we are within an instantiation, this is a nested generic
8951 -- that has already been analyzed at the point of definition.
8952 -- We must preserve references that were global to the enclosing
8953 -- parent at that point. Other occurrences, whether global or
8954 -- local to the current generic, must be resolved anew, so we
8955 -- reset the entity in the generic copy. A global reference has a
8956 -- smaller depth than the parent, or else the same depth in case
8957 -- both are distinct compilation units.
8959 -- A child unit is implicitly declared within the enclosing parent
8960 -- but is in fact global to it, and must be preserved.
8962 -- It is also possible for Current_Instantiated_Parent to be
8963 -- defined, and for this not to be a nested generic, namely if
8964 -- the unit is loaded through Rtsfind. In that case, the entity of
8965 -- New_N is only a link to the associated node, and not a defining
8968 -- The entities for parent units in the defining_program_unit of a
8969 -- generic child unit are established when the context of the unit
8970 -- is first analyzed, before the generic copy is made. They are
8971 -- preserved in the copy for use in e.g. ASIS queries.
8973 Ent := Entity (New_N);
8975 if No (Current_Instantiated_Parent.Gen_Id) then
8977 or else Nkind (Ent) /= N_Defining_Identifier
8978 or else not In_Defining_Unit_Name (N)
8980 Set_Associated_Node (New_N, Empty);
8984 or else Nkind (Ent) not in N_Entity
8985 or else No (Scope (Ent))
8987 (Scope (Ent) = Current_Instantiated_Parent.Gen_Id
8988 and then not Is_Child_Unit (Ent))
8990 (Scope_Depth_Set (Scope (Ent))
8992 Scope_Depth (Scope (Ent)) >
8993 Scope_Depth (Current_Instantiated_Parent.Gen_Id)
8995 Get_Source_Unit (Ent) =
8996 Get_Source_Unit (Current_Instantiated_Parent.Gen_Id))
8998 Set_Associated_Node (New_N, Empty);
9001 -- Case of instantiating identifier or some other name or operator
9004 -- If the associated node is still defined, the entity in it
9005 -- is global, and must be copied to the instance. If this copy
9006 -- is being made for a body to inline, it is applied to an
9007 -- instantiated tree, and the entity is already present and
9008 -- must be also preserved.
9011 Assoc : constant Node_Id := Get_Associated_Node (N);
9014 if Present (Assoc) then
9015 if Nkind (Assoc) = Nkind (N) then
9016 Set_Entity (New_N, Entity (Assoc));
9017 Check_Private_View (N);
9019 -- The node is a reference to a global type and acts as the
9020 -- subtype mark of a qualified expression created in order
9021 -- to aid resolution of accidental overloading in instances.
9022 -- Since N is a reference to a type, the Associated_Node of
9023 -- N denotes an entity rather than another identifier. See
9024 -- Qualify_Universal_Operands for details.
9026 elsif Nkind (N) = N_Identifier
9027 and then Nkind (Parent (N)) = N_Qualified_Expression
9028 and then Subtype_Mark (Parent (N)) = N
9029 and then Is_Qualified_Universal_Literal (Parent (N))
9031 Set_Entity (New_N, Assoc);
9033 -- Cope with the rewriting into expanded name that may have
9034 -- occurred in between, e.g. in Check_Generic_Child_Unit for
9035 -- generic renaming declarations.
9037 elsif Nkind (Assoc) = N_Expanded_Name then
9038 Rewrite (N, New_Copy_Tree (Assoc));
9039 Set_Associated_Node (N, Assoc);
9040 return Copy_Generic_Node (N, Parent_Id, Instantiating);
9042 -- The name in the call may be a selected component if the
9043 -- call has not been analyzed yet, as may be the case for
9044 -- pre/post conditions in a generic unit.
9046 elsif Nkind (Assoc) = N_Function_Call
9047 and then Is_Entity_Name (Name (Assoc))
9049 Set_Entity (New_N, Entity (Name (Assoc)));
9050 Check_Private_View (N);
9052 elsif Nkind (Assoc) in N_Entity
9053 and then (Expander_Active
9054 or else (GNATprove_Mode
9055 and then not In_Spec_Expression
9056 and then not Inside_A_Generic))
9058 -- Inlining case: we are copying a tree that contains
9059 -- global entities, which are preserved in the copy to be
9060 -- used for subsequent inlining.
9065 Set_Entity (New_N, Empty);
9071 -- For expanded name, we must copy the Prefix and Selector_Name
9073 if Nkind (N) = N_Expanded_Name then
9075 (New_N, Copy_Generic_Node (Prefix (N), New_N, Instantiating));
9077 Set_Selector_Name (New_N,
9078 Copy_Generic_Node (Selector_Name (N), New_N, Instantiating));
9080 -- For operators, copy the operands
9082 elsif Nkind (N) in N_Op then
9083 if Nkind (N) in N_Binary_Op then
9084 Set_Left_Opnd (New_N,
9085 Copy_Generic_Node (Left_Opnd (N), New_N, Instantiating));
9088 Set_Right_Opnd (New_N,
9089 Copy_Generic_Node (Right_Opnd (N), New_N, Instantiating));
9092 -- Establish a link between an entity from the generic template and the
9093 -- corresponding entity in the generic copy to be analyzed.
9095 elsif Nkind (N) in N_Entity then
9096 if not Instantiating then
9097 Set_Associated_Entity (N, New_N);
9100 -- Clear any existing link the copy may inherit from the replicated
9101 -- generic template entity.
9103 Set_Associated_Entity (New_N, Empty);
9105 -- Special casing for stubs
9107 elsif Nkind (N) in N_Body_Stub then
9109 -- In any case, we must copy the specification or defining
9110 -- identifier as appropriate.
9112 if Nkind (N) = N_Subprogram_Body_Stub then
9113 Set_Specification (New_N,
9114 Copy_Generic_Node (Specification (N), New_N, Instantiating));
9117 Set_Defining_Identifier (New_N,
9119 (Defining_Identifier (N), New_N, Instantiating));
9122 -- If we are not instantiating, then this is where we load and
9123 -- analyze subunits, i.e. at the point where the stub occurs. A
9124 -- more permissive system might defer this analysis to the point
9125 -- of instantiation, but this seems too complicated for now.
9127 if not Instantiating then
9129 Subunit_Name : constant Unit_Name_Type := Get_Unit_Name (N);
9131 Unum : Unit_Number_Type;
9135 -- Make sure that, if it is a subunit of the main unit that is
9136 -- preprocessed and if -gnateG is specified, the preprocessed
9137 -- file will be written.
9139 Lib.Analysing_Subunit_Of_Main :=
9140 Lib.In_Extended_Main_Source_Unit (N);
9143 (Load_Name => Subunit_Name,
9147 Lib.Analysing_Subunit_Of_Main := False;
9149 -- If the proper body is not found, a warning message will be
9150 -- emitted when analyzing the stub, or later at the point of
9151 -- instantiation. Here we just leave the stub as is.
9153 if Unum = No_Unit then
9154 Subunits_Missing := True;
9155 goto Subunit_Not_Found;
9158 Subunit := Cunit (Unum);
9160 if Nkind (Unit (Subunit)) /= N_Subunit then
9162 ("found child unit instead of expected SEPARATE subunit",
9164 Error_Msg_Sloc := Sloc (N);
9165 Error_Msg_N ("\to complete stub #", Subunit);
9166 goto Subunit_Not_Found;
9169 -- We must create a generic copy of the subunit, in order to
9170 -- perform semantic analysis on it, and we must replace the
9171 -- stub in the original generic unit with the subunit, in order
9172 -- to preserve non-local references within.
9174 -- Only the proper body needs to be copied. Library_Unit and
9175 -- context clause are simply inherited by the generic copy.
9176 -- Note that the copy (which may be recursive if there are
9177 -- nested subunits) must be done first, before attaching it to
9178 -- the enclosing generic.
9182 (Proper_Body (Unit (Subunit)),
9183 Empty, Instantiating => False);
9185 -- Now place the original proper body in the original generic
9186 -- unit. This is a body, not a compilation unit.
9188 Rewrite (N, Proper_Body (Unit (Subunit)));
9189 Set_Is_Compilation_Unit (Defining_Entity (N), False);
9190 Set_Was_Originally_Stub (N);
9192 -- Finally replace the body of the subunit with its copy, and
9193 -- make this new subunit into the library unit of the generic
9194 -- copy, which does not have stubs any longer.
9196 Set_Proper_Body (Unit (Subunit), New_Body);
9197 Set_Library_Unit (New_N, Subunit);
9198 Inherit_Context (Unit (Subunit), N);
9201 -- If we are instantiating, this must be an error case, since
9202 -- otherwise we would have replaced the stub node by the proper body
9203 -- that corresponds. So just ignore it in the copy (i.e. we have
9204 -- copied it, and that is good enough).
9210 <<Subunit_Not_Found>> null;
9212 -- If the node is a compilation unit, it is the subunit of a stub, which
9213 -- has been loaded already (see code below). In this case, the library
9214 -- unit field of N points to the parent unit (which is a compilation
9215 -- unit) and need not (and cannot) be copied.
9217 -- When the proper body of the stub is analyzed, the library_unit link
9218 -- is used to establish the proper context (see sem_ch10).
9220 -- The other fields of a compilation unit are copied as usual
9222 elsif Nkind (N) = N_Compilation_Unit then
9224 -- This code can only be executed when not instantiating, because in
9225 -- the copy made for an instantiation, the compilation unit node has
9226 -- disappeared at the point that a stub is replaced by its proper
9229 pragma Assert (not Instantiating);
9231 Set_Context_Items (New_N,
9232 Copy_Generic_List (Context_Items (N), New_N));
9235 Copy_Generic_Node (Unit (N), New_N, Instantiating => False));
9237 Set_First_Inlined_Subprogram (New_N,
9239 (First_Inlined_Subprogram (N), New_N, Instantiating => False));
9244 (Aux_Decls_Node (N), New_N, Instantiating => False));
9246 -- For an assignment node, the assignment is known to be semantically
9247 -- legal if we are instantiating the template. This avoids incorrect
9248 -- diagnostics in generated code.
9250 elsif Nkind (N) = N_Assignment_Statement then
9252 -- Copy name and expression fields in usual manner
9255 Copy_Generic_Node (Name (N), New_N, Instantiating));
9257 Set_Expression (New_N,
9258 Copy_Generic_Node (Expression (N), New_N, Instantiating));
9260 if Instantiating then
9261 Set_Assignment_OK (Name (New_N), True);
9264 elsif Nkind (N) in N_Aggregate | N_Extension_Aggregate then
9265 if not Instantiating then
9266 Set_Associated_Node (N, New_N);
9269 -- If, in the generic, the aggregate has a global composite type
9270 -- and, at the point of instantiation, the type has a private view
9271 -- then install the full view.
9274 Assoc : constant Node_Id := Get_Associated_Node (N);
9278 and then Nkind (Assoc) = Nkind (N)
9279 and then Present (Etype (Assoc))
9280 and then Is_Private_Type (Etype (Assoc))
9282 Switch_View (Etype (Assoc));
9286 -- Moreover, for a full aggregate, if the type is a derived tagged
9287 -- type and has a global ancestor, then also restore the full view
9288 -- of this ancestor and do so up to the root type. Beware that the
9289 -- Ancestor_Type field is overloaded, so test that it's an entity.
9291 if Nkind (N) = N_Aggregate
9292 and then Present (Ancestor_Type (N))
9293 and then Nkind (Ancestor_Type (N)) in N_Entity
9296 Root_Typ : constant Entity_Id :=
9297 Root_Type (Ancestor_Type (N));
9299 Typ : Entity_Id := Ancestor_Type (N);
9303 if Is_Private_Type (Typ) then
9307 exit when Typ = Root_Typ;
9315 -- Do not copy the associated node, which points to the generic copy
9316 -- of the aggregate.
9318 if Nkind (N) = N_Aggregate then
9319 Set_Aggregate_Bounds
9321 Node_Id (Copy_Generic_Descendant
9322 (Union_Id (Aggregate_Bounds (N)))));
9324 elsif Nkind (N) = N_Extension_Aggregate then
9327 Node_Id (Copy_Generic_Descendant
9328 (Union_Id (Ancestor_Part (N)))));
9331 pragma Assert (False);
9336 List_Id (Copy_Generic_Descendant (Union_Id (Expressions (N)))));
9337 Set_Component_Associations
9339 List_Id (Copy_Generic_Descendant
9340 (Union_Id (Component_Associations (N)))));
9342 (New_N, Node_Id (Copy_Generic_Descendant (Union_Id (Etype (N)))));
9344 -- Allocators do not have an identifier denoting the access type, so we
9345 -- must locate it through the expression to check whether the views are
9348 elsif Nkind (N) = N_Allocator
9349 and then Nkind (Expression (N)) = N_Qualified_Expression
9350 and then Is_Entity_Name (Subtype_Mark (Expression (N)))
9351 and then Instantiating
9354 T : constant Node_Id :=
9355 Get_Associated_Node (Subtype_Mark (Expression (N)));
9361 -- Retrieve the allocator node in the generic copy
9363 Acc_T := Etype (Parent (Parent (T)));
9365 if Present (Acc_T) and then Is_Private_Type (Acc_T) then
9366 Switch_View (Acc_T);
9373 -- Loop parameter specifications do not have an identifier denoting the
9374 -- index type, so we must locate it through the defining identifier to
9375 -- check whether the views are consistent.
9377 elsif Nkind (N) = N_Loop_Parameter_Specification
9378 and then Instantiating
9381 Id : constant Entity_Id :=
9382 Get_Associated_Entity (Defining_Identifier (N));
9384 Index_T : Entity_Id;
9387 if Present (Id) and then Present (Etype (Id)) then
9388 Index_T := First_Subtype (Etype (Id));
9390 if Present (Index_T) and then Is_Private_Type (Index_T) then
9391 Switch_View (Index_T);
9398 -- For a proper body, we must catch the case of a proper body that
9399 -- replaces a stub. This represents the point at which a separate
9400 -- compilation unit, and hence template file, may be referenced, so we
9401 -- must make a new source instantiation entry for the template of the
9402 -- subunit, and ensure that all nodes in the subunit are adjusted using
9403 -- this new source instantiation entry.
9405 elsif Nkind (N) in N_Proper_Body then
9407 Save_Adjustment : constant Sloc_Adjustment := S_Adjustment;
9409 if Instantiating and then Was_Originally_Stub (N) then
9410 Create_Instantiation_Source
9411 (Instantiation_Node,
9412 Defining_Entity (N),
9415 Adjust_Instantiation_Sloc (New_N, S_Adjustment);
9418 -- Now copy the fields of the proper body, using the new
9419 -- adjustment factor if one was needed as per test above.
9423 -- Restore the original adjustment factor
9425 S_Adjustment := Save_Adjustment;
9428 elsif Nkind (N) = N_Pragma and then Instantiating then
9430 -- Do not copy Comment or Ident pragmas their content is relevant to
9431 -- the generic unit, not to the instantiating unit.
9433 if Pragma_Name_Unmapped (N) in Name_Comment | Name_Ident then
9434 New_N := Make_Null_Statement (Sloc (N));
9436 -- Do not copy pragmas generated from aspects because the pragmas do
9437 -- not carry any semantic information, plus they will be regenerated
9440 -- However, generating C we need to copy them since postconditions
9441 -- are inlined by the front end, and the front-end inlining machinery
9442 -- relies on this routine to perform inlining.
9444 elsif From_Aspect_Specification (N) then
9445 New_N := Make_Null_Statement (Sloc (N));
9451 elsif Nkind (N) in N_Integer_Literal | N_Real_Literal then
9453 -- No descendant fields need traversing
9457 elsif Nkind (N) = N_String_Literal
9458 and then Present (Etype (N))
9459 and then Instantiating
9461 -- If the string is declared in an outer scope, the string_literal
9462 -- subtype created for it may have the wrong scope. Force reanalysis
9463 -- of the constant to generate a new itype in the proper context.
9465 Set_Etype (New_N, Empty);
9466 Set_Analyzed (New_N, False);
9468 -- For the remaining nodes, copy their descendants recursively
9473 if Instantiating and then Nkind (N) = N_Subprogram_Body then
9474 Set_Generic_Parent (Specification (New_N), N);
9476 -- Should preserve Corresponding_Spec??? (12.3(14))
9480 -- Propagate dimensions if present, so that they are reflected in the
9483 if Nkind (N) in N_Has_Etype
9484 and then (Nkind (N) in N_Op or else Is_Entity_Name (N))
9485 and then Present (Etype (N))
9486 and then Is_Floating_Point_Type (Etype (N))
9487 and then Has_Dimension_System (Etype (N))
9489 Copy_Dimensions (N, New_N);
9493 end Copy_Generic_Node;
9495 ----------------------------
9496 -- Denotes_Formal_Package --
9497 ----------------------------
9499 function Denotes_Formal_Package
9501 On_Exit : Boolean := False;
9502 Instance : Entity_Id := Empty) return Boolean
9505 Scop : constant Entity_Id := Scope (Pack);
9508 function Is_Actual_Of_Previous_Formal (P : Entity_Id) return Boolean;
9509 -- The package in question may be an actual for a previous formal
9510 -- package P of the current instance, so examine its actuals as well.
9511 -- This must be recursive over other formal packages.
9513 ----------------------------------
9514 -- Is_Actual_Of_Previous_Formal --
9515 ----------------------------------
9517 function Is_Actual_Of_Previous_Formal (P : Entity_Id) return Boolean is
9521 E1 := First_Entity (P);
9522 while Present (E1) and then E1 /= Instance loop
9523 if Ekind (E1) = E_Package
9524 and then Nkind (Parent (E1)) = N_Package_Renaming_Declaration
9526 if Renamed_Entity (E1) = Pack then
9529 elsif E1 = P or else Renamed_Entity (E1) = P then
9532 elsif Is_Actual_Of_Previous_Formal (E1) then
9541 end Is_Actual_Of_Previous_Formal;
9543 -- Start of processing for Denotes_Formal_Package
9549 (Instance_Envs.Last).Instantiated_Parent.Act_Id;
9551 Par := Current_Instantiated_Parent.Act_Id;
9554 if Ekind (Scop) = E_Generic_Package
9555 or else Nkind (Unit_Declaration_Node (Scop)) =
9556 N_Generic_Subprogram_Declaration
9560 elsif Nkind (Original_Node (Unit_Declaration_Node (Pack))) =
9561 N_Formal_Package_Declaration
9569 -- Check whether this package is associated with a formal package of
9570 -- the enclosing instantiation. Iterate over the list of renamings.
9572 E := First_Entity (Par);
9573 while Present (E) loop
9574 if Ekind (E) /= E_Package
9575 or else Nkind (Parent (E)) /= N_Package_Renaming_Declaration
9579 elsif Renamed_Entity (E) = Par then
9582 elsif Renamed_Entity (E) = Pack then
9585 elsif Is_Actual_Of_Previous_Formal (E) then
9595 end Denotes_Formal_Package;
9601 procedure End_Generic is
9603 -- ??? More things could be factored out in this routine. Should
9604 -- probably be done at a later stage.
9606 Inside_A_Generic := Generic_Flags.Table (Generic_Flags.Last);
9607 Generic_Flags.Decrement_Last;
9609 Expander_Mode_Restore;
9616 function Earlier (N1, N2 : Node_Id) return Boolean is
9617 procedure Find_Depth (P : in out Node_Id; D : in out Integer);
9618 -- Find distance from given node to enclosing compilation unit
9624 procedure Find_Depth (P : in out Node_Id; D : in out Integer) is
9627 and then Nkind (P) /= N_Compilation_Unit
9629 P := True_Parent (P);
9634 -- Local declarations
9643 -- Start of processing for Earlier
9646 Find_Depth (P1, D1);
9647 Find_Depth (P2, D2);
9657 P1 := True_Parent (P1);
9662 P2 := True_Parent (P2);
9666 -- At this point P1 and P2 are at the same distance from the root.
9667 -- We examine their parents until we find a common declarative list.
9668 -- If we reach the root, N1 and N2 do not descend from the same
9669 -- declarative list (e.g. one is nested in the declarative part and
9670 -- the other is in a block in the statement part) and the earlier
9671 -- one is already frozen.
9673 while not Is_List_Member (P1)
9674 or else not Is_List_Member (P2)
9675 or else not In_Same_List (P1, P2)
9677 P1 := True_Parent (P1);
9678 P2 := True_Parent (P2);
9680 if Nkind (Parent (P1)) = N_Subunit then
9681 P1 := Corresponding_Stub (Parent (P1));
9684 if Nkind (Parent (P2)) = N_Subunit then
9685 P2 := Corresponding_Stub (Parent (P2));
9693 -- Expanded code usually shares the source location of the original
9694 -- construct it was generated for. This however may not necessarily
9695 -- reflect the true location of the code within the tree.
9697 -- Before comparing the slocs of the two nodes, make sure that we are
9698 -- working with correct source locations. Assume that P1 is to the left
9699 -- of P2. If either one does not come from source, traverse the common
9700 -- list heading towards the other node and locate the first source
9704 -- ----+===+===+--------------+===+===+----
9705 -- expanded code expanded code
9707 if not Comes_From_Source (P1) then
9708 while Present (P1) loop
9710 -- Neither P2 nor a source statement were located during the
9711 -- search. If we reach the end of the list, then P1 does not
9712 -- occur earlier than P2.
9715 -- start --- P2 ----- P1 --- end
9717 if No (Next (P1)) then
9720 -- We encounter P2 while going to the right of the list. This
9721 -- means that P1 does indeed appear earlier.
9724 -- start --- P1 ===== P2 --- end
9725 -- expanded code in between
9730 -- No need to look any further since we have located a source
9733 elsif Comes_From_Source (P1) then
9743 if not Comes_From_Source (P2) then
9744 while Present (P2) loop
9746 -- Neither P1 nor a source statement were located during the
9747 -- search. If we reach the start of the list, then P1 does not
9748 -- occur earlier than P2.
9751 -- start --- P2 --- P1 --- end
9753 if No (Prev (P2)) then
9756 -- We encounter P1 while going to the left of the list. This
9757 -- means that P1 does indeed appear earlier.
9760 -- start --- P1 ===== P2 --- end
9761 -- expanded code in between
9766 -- No need to look any further since we have located a source
9769 elsif Comes_From_Source (P2) then
9779 -- At this point either both nodes came from source or we approximated
9780 -- their source locations through neighboring source statements.
9782 T1 := Top_Level_Location (Sloc (P1));
9783 T2 := Top_Level_Location (Sloc (P2));
9785 -- When two nodes come from the same instance, they have identical top
9786 -- level locations. To determine proper relation within the tree, check
9787 -- their locations within the template.
9790 return Sloc (P1) < Sloc (P2);
9792 -- The two nodes either come from unrelated instances or do not come
9793 -- from instantiated code at all.
9800 ----------------------
9801 -- Find_Actual_Type --
9802 ----------------------
9804 function Find_Actual_Type
9806 Gen_Type : Entity_Id) return Entity_Id
9808 Gen_Scope : constant Entity_Id := Scope (Gen_Type);
9812 -- Special processing only applies to child units
9814 if not Is_Child_Unit (Gen_Scope) then
9815 return Get_Instance_Of (Typ);
9817 -- If designated or component type is itself a formal of the child unit,
9818 -- its instance is available.
9820 elsif Scope (Typ) = Gen_Scope then
9821 return Get_Instance_Of (Typ);
9823 -- If the array or access type is not declared in the parent unit,
9824 -- no special processing needed.
9826 elsif not Is_Generic_Type (Typ)
9827 and then Scope (Gen_Scope) /= Scope (Typ)
9829 return Get_Instance_Of (Typ);
9831 -- Otherwise, retrieve designated or component type by visibility
9834 T := Current_Entity (Typ);
9835 while Present (T) loop
9836 if In_Open_Scopes (Scope (T)) then
9838 elsif Is_Generic_Actual_Type (T) then
9847 end Find_Actual_Type;
9849 -----------------------------
9850 -- Freeze_Package_Instance --
9851 -----------------------------
9853 procedure Freeze_Package_Instance
9859 function In_Same_Scope (Gen_Id, Act_Id : Node_Id) return Boolean;
9860 -- Check if the generic definition and the instantiation come from
9861 -- a common scope, in which case the instance must be frozen after
9862 -- the generic body.
9864 function True_Sloc (N, Act_Unit : Node_Id) return Source_Ptr;
9865 -- If the instance is nested inside a generic unit, the Sloc of the
9866 -- instance indicates the place of the original definition, not the
9867 -- point of the current enclosing instance. Pending a better usage of
9868 -- Slocs to indicate instantiation places, we determine the place of
9869 -- origin of a node by finding the maximum sloc of any ancestor node.
9871 -- Why is this not equivalent to Top_Level_Location ???
9877 function In_Same_Scope (Gen_Id, Act_Id : Node_Id) return Boolean is
9878 Act_Scop : Entity_Id := Scope (Act_Id);
9879 Gen_Scop : Entity_Id := Scope (Gen_Id);
9882 while Act_Scop /= Standard_Standard
9883 and then Gen_Scop /= Standard_Standard
9885 if Act_Scop = Gen_Scop then
9889 Act_Scop := Scope (Act_Scop);
9890 Gen_Scop := Scope (Gen_Scop);
9900 function True_Sloc (N, Act_Unit : Node_Id) return Source_Ptr is
9907 while Present (N1) and then N1 /= Act_Unit loop
9908 if Sloc (N1) > Res then
9920 Gen_Id : constant Entity_Id := Get_Generic_Entity (N);
9921 Par_Id : constant Entity_Id := Scope (Gen_Id);
9922 Act_Unit : constant Node_Id := Unit (Cunit (Get_Source_Unit (N)));
9923 Gen_Unit : constant Node_Id :=
9924 Unit (Cunit (Get_Source_Unit (Gen_Decl)));
9926 Body_Unit : Node_Id;
9928 Must_Delay : Boolean;
9929 Orig_Body : Node_Id;
9931 -- Start of processing for Freeze_Package_Instance
9934 -- If the body is a subunit, the freeze point is the corresponding stub
9935 -- in the current compilation, not the subunit itself.
9937 if Nkind (Parent (Gen_Body)) = N_Subunit then
9938 Orig_Body := Corresponding_Stub (Parent (Gen_Body));
9940 Orig_Body := Gen_Body;
9943 Body_Unit := Unit (Cunit (Get_Source_Unit (Orig_Body)));
9945 -- If the instantiation and the generic definition appear in the same
9946 -- package declaration, this is an early instantiation. If they appear
9947 -- in the same declarative part, it is an early instantiation only if
9948 -- the generic body appears textually later, and the generic body is
9949 -- also in the main unit.
9951 -- If instance is nested within a subprogram, and the generic body
9952 -- is not, the instance is delayed because the enclosing body is. If
9953 -- instance and body are within the same scope, or the same subprogram
9954 -- body, indicate explicitly that the instance is delayed.
9957 (Gen_Unit = Act_Unit
9958 and then (Nkind (Gen_Unit) in N_Generic_Package_Declaration
9959 | N_Package_Declaration
9960 or else (Gen_Unit = Body_Unit
9962 True_Sloc (N, Act_Unit) < Sloc (Orig_Body)))
9963 and then Is_In_Main_Unit (Original_Node (Gen_Unit))
9964 and then In_Same_Scope (Gen_Id, Act_Id));
9966 -- If this is an early instantiation, the freeze node is placed after
9967 -- the generic body. Otherwise, if the generic appears in an instance,
9968 -- we cannot freeze the current instance until the outer one is frozen.
9969 -- This is only relevant if the current instance is nested within some
9970 -- inner scope not itself within the outer instance. If this scope is
9971 -- a package body in the same declarative part as the outer instance,
9972 -- then that body needs to be frozen after the outer instance. Finally,
9973 -- if no delay is needed, we place the freeze node at the end of the
9974 -- current declarative part.
9976 if No (Freeze_Node (Act_Id))
9977 or else not Is_List_Member (Freeze_Node (Act_Id))
9979 Ensure_Freeze_Node (Act_Id);
9980 F_Node := Freeze_Node (Act_Id);
9983 Insert_After (Orig_Body, F_Node);
9985 elsif Is_Generic_Instance (Par_Id)
9986 and then Present (Freeze_Node (Par_Id))
9987 and then Scope (Act_Id) /= Par_Id
9989 -- Freeze instance of inner generic after instance of enclosing
9992 if In_Same_Declarative_Part (Parent (Freeze_Node (Par_Id)), N) then
9994 -- Handle the following case:
9996 -- package Parent_Inst is new ...
9997 -- freeze Parent_Inst []
9999 -- procedure P ... -- this body freezes Parent_Inst
10001 -- package Inst is new ...
10003 -- In this particular scenario, the freeze node for Inst must
10004 -- be inserted in the same manner as that of Parent_Inst,
10005 -- before the next source body or at the end of the declarative
10006 -- list (body not available). If body P did not exist and
10007 -- Parent_Inst was frozen after Inst, either by a body
10008 -- following Inst or at the end of the declarative region,
10009 -- the freeze node for Inst must be inserted after that of
10010 -- Parent_Inst. This relation is established by comparing
10011 -- the Slocs of Parent_Inst freeze node and Inst.
10012 -- We examine the parents of the enclosing lists to handle
10013 -- the case where the parent instance is in the visible part
10014 -- of a package declaration, and the inner instance is in
10015 -- the corresponding private part.
10017 if Parent (List_Containing (Freeze_Node (Par_Id)))
10018 = Parent (List_Containing (N))
10019 and then Sloc (Freeze_Node (Par_Id)) <= Sloc (N)
10021 Insert_Freeze_Node_For_Instance (N, F_Node);
10023 Insert_After (Freeze_Node (Par_Id), F_Node);
10026 -- Freeze package enclosing instance of inner generic after
10027 -- instance of enclosing generic.
10029 elsif Nkind (Parent (N)) in N_Package_Body | N_Subprogram_Body
10030 and then In_Same_Declarative_Part
10031 (Parent (Freeze_Node (Par_Id)), Parent (N))
10034 Enclosing : Entity_Id;
10037 Enclosing := Corresponding_Spec (Parent (N));
10039 if No (Enclosing) then
10040 Enclosing := Defining_Entity (Parent (N));
10043 Insert_Freeze_Node_For_Instance (N, F_Node);
10044 Ensure_Freeze_Node (Enclosing);
10046 if not Is_List_Member (Freeze_Node (Enclosing)) then
10048 -- The enclosing context is a subunit, insert the freeze
10049 -- node after the stub.
10051 if Nkind (Parent (Parent (N))) = N_Subunit then
10052 Insert_Freeze_Node_For_Instance
10053 (Corresponding_Stub (Parent (Parent (N))),
10054 Freeze_Node (Enclosing));
10056 -- The enclosing context is a package with a stub body
10057 -- which has already been replaced by the real body.
10058 -- Insert the freeze node after the actual body.
10060 elsif Ekind (Enclosing) = E_Package
10061 and then Present (Body_Entity (Enclosing))
10062 and then Was_Originally_Stub
10063 (Parent (Body_Entity (Enclosing)))
10065 Insert_Freeze_Node_For_Instance
10066 (Parent (Body_Entity (Enclosing)),
10067 Freeze_Node (Enclosing));
10069 -- The parent instance has been frozen before the body of
10070 -- the enclosing package, insert the freeze node after
10073 elsif In_Same_List (Freeze_Node (Par_Id), Parent (N))
10075 Sloc (Freeze_Node (Par_Id)) <= Sloc (Parent (N))
10077 Insert_Freeze_Node_For_Instance
10078 (Parent (N), Freeze_Node (Enclosing));
10082 (Freeze_Node (Par_Id), Freeze_Node (Enclosing));
10088 Insert_Freeze_Node_For_Instance (N, F_Node);
10092 Insert_Freeze_Node_For_Instance (N, F_Node);
10095 end Freeze_Package_Instance;
10097 --------------------------------
10098 -- Freeze_Subprogram_Instance --
10099 --------------------------------
10101 procedure Freeze_Subprogram_Instance
10103 Gen_Body : Node_Id;
10104 Pack_Id : Entity_Id)
10106 function Enclosing_Package_Body (N : Node_Id) return Node_Id;
10107 -- Find innermost package body that encloses the given node, and which
10108 -- is not a compilation unit. Freeze nodes for the instance, or for its
10109 -- enclosing body, may be inserted after the enclosing_body of the
10110 -- generic unit. Used to determine proper placement of freeze node for
10111 -- both package and subprogram instances.
10113 function Package_Freeze_Node (B : Node_Id) return Node_Id;
10114 -- Find entity for given package body, and locate or create a freeze
10117 ----------------------------
10118 -- Enclosing_Package_Body --
10119 ----------------------------
10121 function Enclosing_Package_Body (N : Node_Id) return Node_Id is
10127 and then Nkind (Parent (P)) /= N_Compilation_Unit
10129 if Nkind (P) = N_Package_Body then
10130 if Nkind (Parent (P)) = N_Subunit then
10131 return Corresponding_Stub (Parent (P));
10137 P := True_Parent (P);
10141 end Enclosing_Package_Body;
10143 -------------------------
10144 -- Package_Freeze_Node --
10145 -------------------------
10147 function Package_Freeze_Node (B : Node_Id) return Node_Id is
10151 if Nkind (B) = N_Package_Body then
10152 Id := Corresponding_Spec (B);
10153 else pragma Assert (Nkind (B) = N_Package_Body_Stub);
10154 Id := Corresponding_Spec (Proper_Body (Unit (Library_Unit (B))));
10157 Ensure_Freeze_Node (Id);
10158 return Freeze_Node (Id);
10159 end Package_Freeze_Node;
10163 Enc_G : constant Node_Id := Enclosing_Package_Body (Gen_Body);
10164 Enc_N : constant Node_Id := Enclosing_Package_Body (N);
10165 Par_Id : constant Entity_Id := Scope (Get_Generic_Entity (N));
10170 -- Start of processing for Freeze_Subprogram_Instance
10173 -- If the instance and the generic body appear within the same unit, and
10174 -- the instance precedes the generic, the freeze node for the instance
10175 -- must appear after that of the generic. If the generic is nested
10176 -- within another instance I2, then current instance must be frozen
10177 -- after I2. In both cases, the freeze nodes are those of enclosing
10178 -- packages. Otherwise, the freeze node is placed at the end of the
10179 -- current declarative part.
10181 Ensure_Freeze_Node (Pack_Id);
10182 F_Node := Freeze_Node (Pack_Id);
10184 if Is_Generic_Instance (Par_Id)
10185 and then Present (Freeze_Node (Par_Id))
10186 and then In_Same_Declarative_Part (Parent (Freeze_Node (Par_Id)), N)
10188 -- The parent was a premature instantiation. Insert freeze node at
10189 -- the end the current declarative part.
10191 if Is_Known_Guaranteed_ABE (Get_Unit_Instantiation_Node (Par_Id)) then
10192 Insert_Freeze_Node_For_Instance (N, F_Node);
10194 -- Handle the following case:
10196 -- package Parent_Inst is new ...
10197 -- freeze Parent_Inst []
10199 -- procedure P ... -- this body freezes Parent_Inst
10201 -- procedure Inst is new ...
10203 -- In this particular scenario, the freeze node for Inst must be
10204 -- inserted in the same manner as that of Parent_Inst - before the
10205 -- next source body or at the end of the declarative list (body not
10206 -- available). If body P did not exist and Parent_Inst was frozen
10207 -- after Inst, either by a body following Inst or at the end of the
10208 -- declarative region, the freeze node for Inst must be inserted
10209 -- after that of Parent_Inst. This relation is established by
10210 -- comparing the Slocs of Parent_Inst freeze node and Inst.
10212 elsif In_Same_List (Freeze_Node (Par_Id), N)
10213 and then Sloc (Freeze_Node (Par_Id)) <= Sloc (N)
10215 Insert_Freeze_Node_For_Instance (N, F_Node);
10218 Insert_After (Freeze_Node (Par_Id), F_Node);
10221 -- The body enclosing the instance should be frozen after the body that
10222 -- includes the generic, because the body of the instance may make
10223 -- references to entities therein. If the two are not in the same
10224 -- declarative part, or if the one enclosing the instance is frozen
10225 -- already, freeze the instance at the end of the current declarative
10228 elsif Is_Generic_Instance (Par_Id)
10229 and then Present (Freeze_Node (Par_Id))
10230 and then Present (Enc_N)
10232 if In_Same_Declarative_Part (Parent (Freeze_Node (Par_Id)), Enc_N)
10234 -- The enclosing package may contain several instances. Rather
10235 -- than computing the earliest point at which to insert its freeze
10236 -- node, we place it at the end of the declarative part of the
10237 -- parent of the generic.
10239 Insert_Freeze_Node_For_Instance
10240 (Freeze_Node (Par_Id), Package_Freeze_Node (Enc_N));
10243 Insert_Freeze_Node_For_Instance (N, F_Node);
10245 elsif Present (Enc_G)
10246 and then Present (Enc_N)
10247 and then Enc_G /= Enc_N
10248 and then Earlier (N, Gen_Body)
10250 -- Freeze package that encloses instance, and place node after the
10251 -- package that encloses generic. If enclosing package is already
10252 -- frozen we have to assume it is at the proper place. This may be a
10253 -- potential ABE that requires dynamic checking. Do not add a freeze
10254 -- node if the package that encloses the generic is inside the body
10255 -- that encloses the instance, because the freeze node would be in
10256 -- the wrong scope. Additional contortions needed if the bodies are
10257 -- within a subunit.
10260 Enclosing_Body : Node_Id;
10263 if Nkind (Enc_N) = N_Package_Body_Stub then
10264 Enclosing_Body := Proper_Body (Unit (Library_Unit (Enc_N)));
10266 Enclosing_Body := Enc_N;
10269 if Parent (List_Containing (Enc_G)) /= Enclosing_Body then
10270 Insert_Freeze_Node_For_Instance
10271 (Enc_G, Package_Freeze_Node (Enc_N));
10275 -- Freeze enclosing subunit before instance
10277 Enc_G_F := Package_Freeze_Node (Enc_G);
10279 if not Is_List_Member (Enc_G_F) then
10280 Insert_After (Enc_G, Enc_G_F);
10283 Insert_Freeze_Node_For_Instance (N, F_Node);
10286 -- If none of the above, insert freeze node at the end of the current
10287 -- declarative part.
10289 Insert_Freeze_Node_For_Instance (N, F_Node);
10291 end Freeze_Subprogram_Instance;
10297 function Get_Gen_Id (E : Assoc_Ptr) return Entity_Id is
10299 return Generic_Renamings.Table (E).Gen_Id;
10302 ---------------------
10303 -- Get_Instance_Of --
10304 ---------------------
10306 function Get_Instance_Of (A : Entity_Id) return Entity_Id is
10307 Res : constant Assoc_Ptr := Generic_Renamings_HTable.Get (A);
10310 if Res /= Assoc_Null then
10311 return Generic_Renamings.Table (Res).Act_Id;
10314 -- On exit, entity is not instantiated: not a generic parameter, or
10315 -- else parameter of an inner generic unit.
10319 end Get_Instance_Of;
10321 ---------------------------------
10322 -- Get_Unit_Instantiation_Node --
10323 ---------------------------------
10325 function Get_Unit_Instantiation_Node (A : Entity_Id) return Node_Id is
10326 Decl : Node_Id := Unit_Declaration_Node (A);
10330 -- If the Package_Instantiation attribute has been set on the package
10331 -- entity, then use it directly when it (or its Original_Node) refers
10332 -- to an N_Package_Instantiation node. In principle it should be
10333 -- possible to have this field set in all cases, which should be
10334 -- investigated, and would allow this function to be significantly
10337 Inst := Package_Instantiation (A);
10339 if Present (Inst) then
10340 if Nkind (Inst) = N_Package_Instantiation then
10343 elsif Nkind (Original_Node (Inst)) = N_Package_Instantiation then
10344 return Original_Node (Inst);
10348 -- If the instantiation is a compilation unit that does not need body
10349 -- then the instantiation node has been rewritten as a package
10350 -- declaration for the instance, and we return the original node.
10352 -- If it is a compilation unit and the instance node has not been
10353 -- rewritten, then it is still the unit of the compilation. Finally, if
10354 -- a body is present, this is a parent of the main unit whose body has
10355 -- been compiled for inlining purposes, and the instantiation node has
10356 -- been rewritten with the instance body.
10358 -- Otherwise the instantiation node appears after the declaration. If
10359 -- the entity is a formal package, the declaration may have been
10360 -- rewritten as a generic declaration (in the case of a formal with box)
10361 -- or left as a formal package declaration if it has actuals, and is
10362 -- found with a forward search.
10364 if Nkind (Parent (Decl)) = N_Compilation_Unit then
10365 if Nkind (Decl) = N_Package_Declaration
10366 and then Present (Corresponding_Body (Decl))
10368 Decl := Unit_Declaration_Node (Corresponding_Body (Decl));
10371 if Nkind (Original_Node (Decl)) in N_Generic_Instantiation then
10372 return Original_Node (Decl);
10374 return Unit (Parent (Decl));
10377 elsif Nkind (Decl) = N_Package_Declaration
10378 and then Nkind (Original_Node (Decl)) = N_Formal_Package_Declaration
10380 return Original_Node (Decl);
10383 Inst := Next (Decl);
10384 while Nkind (Inst) not in N_Formal_Package_Declaration
10385 | N_Function_Instantiation
10386 | N_Package_Instantiation
10387 | N_Procedure_Instantiation
10394 end Get_Unit_Instantiation_Node;
10396 ------------------------
10397 -- Has_Been_Exchanged --
10398 ------------------------
10400 function Has_Been_Exchanged (E : Entity_Id) return Boolean is
10404 Next := First_Elmt (Exchanged_Views);
10405 while Present (Next) loop
10406 if Full_View (Node (Next)) = E then
10414 end Has_Been_Exchanged;
10416 -------------------
10417 -- Has_Contracts --
10418 -------------------
10420 function Has_Contracts (Decl : Node_Id) return Boolean is
10421 A_List : constant List_Id := Aspect_Specifications (Decl);
10425 A_Spec := First (A_List);
10426 while Present (A_Spec) loop
10427 A_Id := Get_Aspect_Id (A_Spec);
10428 if A_Id = Aspect_Pre or else A_Id = Aspect_Post then
10438 -------------------------------
10439 -- Has_Fully_Defined_Profile --
10440 -------------------------------
10442 function Has_Fully_Defined_Profile (Subp : Entity_Id) return Boolean is
10443 function Is_Fully_Defined_Type (Typ : Entity_Id) return Boolean;
10444 -- Determine whethet type Typ is fully defined
10446 ---------------------------
10447 -- Is_Fully_Defined_Type --
10448 ---------------------------
10450 function Is_Fully_Defined_Type (Typ : Entity_Id) return Boolean is
10452 -- A private type without a full view is not fully defined
10454 if Is_Private_Type (Typ)
10455 and then No (Full_View (Typ))
10459 -- An incomplete type is never fully defined
10461 elsif Is_Incomplete_Type (Typ) then
10464 -- All other types are fully defined
10469 end Is_Fully_Defined_Type;
10471 -- Local declarations
10475 -- Start of processing for Has_Fully_Defined_Profile
10478 -- Check the parameters
10480 Param := First_Formal (Subp);
10481 while Present (Param) loop
10482 if not Is_Fully_Defined_Type (Etype (Param)) then
10486 Next_Formal (Param);
10489 -- Check the return type
10491 return Is_Fully_Defined_Type (Etype (Subp));
10492 end Has_Fully_Defined_Profile;
10498 function Hash (F : Entity_Id) return HTable_Range is
10500 return HTable_Range (F mod HTable_Size);
10503 ------------------------
10504 -- Hide_Current_Scope --
10505 ------------------------
10507 procedure Hide_Current_Scope is
10508 C : constant Entity_Id := Current_Scope;
10512 Set_Is_Hidden_Open_Scope (C);
10514 E := First_Entity (C);
10515 while Present (E) loop
10516 if Is_Immediately_Visible (E) then
10517 Set_Is_Immediately_Visible (E, False);
10518 Append_Elmt (E, Hidden_Entities);
10524 -- Make the scope name invisible as well. This is necessary, but might
10525 -- conflict with calls to Rtsfind later on, in case the scope is a
10526 -- predefined one. There is no clean solution to this problem, so for
10527 -- now we depend on the user not redefining Standard itself in one of
10528 -- the parent units.
10530 if Is_Immediately_Visible (C) and then C /= Standard_Standard then
10531 Set_Is_Immediately_Visible (C, False);
10532 Append_Elmt (C, Hidden_Entities);
10535 end Hide_Current_Scope;
10541 procedure Init_Env is
10542 Saved : Instance_Env;
10545 Saved.Instantiated_Parent := Current_Instantiated_Parent;
10546 Saved.Exchanged_Views := Exchanged_Views;
10547 Saved.Hidden_Entities := Hidden_Entities;
10548 Saved.Current_Sem_Unit := Current_Sem_Unit;
10549 Saved.Parent_Unit_Visible := Parent_Unit_Visible;
10550 Saved.Instance_Parent_Unit := Instance_Parent_Unit;
10552 -- Save configuration switches. These may be reset if the unit is a
10553 -- predefined unit, and the current mode is not Ada 2005.
10555 Saved.Switches := Save_Config_Switches;
10557 Instance_Envs.Append (Saved);
10559 Exchanged_Views := New_Elmt_List;
10560 Hidden_Entities := New_Elmt_List;
10562 -- Make dummy entry for Instantiated parent. If generic unit is legal,
10563 -- this is set properly in Set_Instance_Env.
10565 Current_Instantiated_Parent :=
10566 (Current_Scope, Current_Scope, Assoc_Null);
10569 ---------------------
10570 -- In_Main_Context --
10571 ---------------------
10573 function In_Main_Context (E : Entity_Id) return Boolean is
10579 if not Is_Compilation_Unit (E)
10580 or else Ekind (E) /= E_Package
10581 or else In_Private_Part (E)
10586 Context := Context_Items (Cunit (Main_Unit));
10588 Clause := First (Context);
10589 while Present (Clause) loop
10590 if Nkind (Clause) = N_With_Clause then
10591 Nam := Name (Clause);
10593 -- If the current scope is part of the context of the main unit,
10594 -- analysis of the corresponding with_clause is not complete, and
10595 -- the entity is not set. We use the Chars field directly, which
10596 -- might produce false positives in rare cases, but guarantees
10597 -- that we produce all the instance bodies we will need.
10599 if (Is_Entity_Name (Nam) and then Chars (Nam) = Chars (E))
10600 or else (Nkind (Nam) = N_Selected_Component
10601 and then Chars (Selector_Name (Nam)) = Chars (E))
10611 end In_Main_Context;
10613 ---------------------
10614 -- Inherit_Context --
10615 ---------------------
10617 procedure Inherit_Context (Gen_Decl : Node_Id; Inst : Node_Id) is
10618 Current_Context : List_Id;
10619 Current_Unit : Node_Id;
10625 Lib_Unit : Node_Id;
10628 if Nkind (Parent (Gen_Decl)) = N_Compilation_Unit then
10630 -- The inherited context is attached to the enclosing compilation
10631 -- unit. This is either the main unit, or the declaration for the
10632 -- main unit (in case the instantiation appears within the package
10633 -- declaration and the main unit is its body).
10635 Current_Unit := Parent (Inst);
10636 while Present (Current_Unit)
10637 and then Nkind (Current_Unit) /= N_Compilation_Unit
10639 Current_Unit := Parent (Current_Unit);
10642 Current_Context := Context_Items (Current_Unit);
10644 Item := First (Context_Items (Parent (Gen_Decl)));
10645 while Present (Item) loop
10646 if Nkind (Item) = N_With_Clause then
10647 Lib_Unit := Library_Unit (Item);
10649 -- Take care to prevent direct cyclic with's
10651 if Lib_Unit /= Current_Unit then
10653 -- Do not add a unit if it is already in the context
10655 Clause := First (Current_Context);
10657 while Present (Clause) loop
10658 if Nkind (Clause) = N_With_Clause
10659 and then Library_Unit (Clause) = Lib_Unit
10669 New_I := New_Copy (Item);
10670 Set_Implicit_With (New_I);
10672 Append (New_I, Current_Context);
10680 end Inherit_Context;
10686 procedure Initialize is
10688 Generic_Renamings.Init;
10689 Instance_Envs.Init;
10690 Generic_Flags.Init;
10691 Generic_Renamings_HTable.Reset;
10692 Circularity_Detected := False;
10693 Exchanged_Views := No_Elist;
10694 Hidden_Entities := No_Elist;
10697 -------------------------------------
10698 -- Insert_Freeze_Node_For_Instance --
10699 -------------------------------------
10701 procedure Insert_Freeze_Node_For_Instance
10705 function Enclosing_Body (N : Node_Id) return Node_Id;
10706 -- Find enclosing package or subprogram body, if any. Freeze node may
10707 -- be placed at end of current declarative list if previous instance
10708 -- and current one have different enclosing bodies.
10710 function Previous_Instance (Gen : Entity_Id) return Entity_Id;
10711 -- Find the local instance, if any, that declares the generic that is
10712 -- being instantiated. If present, the freeze node for this instance
10713 -- must follow the freeze node for the previous instance.
10715 --------------------
10716 -- Enclosing_Body --
10717 --------------------
10719 function Enclosing_Body (N : Node_Id) return Node_Id is
10725 and then Nkind (Parent (P)) /= N_Compilation_Unit
10727 if Nkind (P) in N_Package_Body | N_Subprogram_Body then
10728 if Nkind (Parent (P)) = N_Subunit then
10729 return Corresponding_Stub (Parent (P));
10735 P := True_Parent (P);
10739 end Enclosing_Body;
10741 -----------------------
10742 -- Previous_Instance --
10743 -----------------------
10745 function Previous_Instance (Gen : Entity_Id) return Entity_Id is
10750 while Present (S) and then S /= Standard_Standard loop
10751 if Is_Generic_Instance (S)
10752 and then In_Same_Source_Unit (S, N)
10761 end Previous_Instance;
10768 Origin : Entity_Id;
10769 Par_Inst : Node_Id;
10772 -- Start of processing for Insert_Freeze_Node_For_Instance
10775 -- Nothing to do if the freeze node has already been inserted
10777 if Is_List_Member (F_Node) then
10781 Inst := Entity (F_Node);
10783 -- When processing a subprogram instantiation, utilize the actual
10784 -- subprogram instantiation rather than its package wrapper as it
10785 -- carries all the context information.
10787 if Is_Wrapper_Package (Inst) then
10788 Inst := Related_Instance (Inst);
10791 Par_Inst := Parent (Inst);
10793 -- If this is a package instance, check whether the generic is declared
10794 -- in a previous instance and the current instance is not within the
10797 if Present (Generic_Parent (Par_Inst)) and then Is_In_Main_Unit (N) then
10799 Enclosing_N : constant Node_Id := Enclosing_Body (N);
10800 Par_I : constant Entity_Id :=
10801 Previous_Instance (Generic_Parent (Par_Inst));
10805 if Present (Par_I) and then Earlier (N, Freeze_Node (Par_I)) then
10806 Scop := Scope (Inst);
10808 -- If the current instance is within the one that contains
10809 -- the generic, the freeze node for the current one must
10810 -- appear in the current declarative part. Ditto, if the
10811 -- current instance is within another package instance or
10812 -- within a body that does not enclose the current instance.
10813 -- In these three cases the freeze node of the previous
10814 -- instance is not relevant.
10816 while Present (Scop) and then Scop /= Standard_Standard loop
10817 exit when Scop = Par_I
10819 (Is_Generic_Instance (Scop)
10820 and then Scope_Depth (Scop) > Scope_Depth (Par_I));
10821 Scop := Scope (Scop);
10824 -- Previous instance encloses current instance
10826 if Scop = Par_I then
10829 -- If the next node is a source body we must freeze in the
10830 -- current scope as well.
10832 elsif Present (Next (N))
10833 and then Nkind (Next (N)) in N_Subprogram_Body
10835 and then Comes_From_Source (Next (N))
10839 -- Current instance is within an unrelated instance
10841 elsif Is_Generic_Instance (Scop) then
10844 -- Current instance is within an unrelated body
10846 elsif Present (Enclosing_N)
10847 and then Enclosing_N /= Enclosing_Body (Par_I)
10852 Insert_After (Freeze_Node (Par_I), F_Node);
10860 Decls := List_Containing (N);
10861 Par_N := Parent (Decls);
10864 -- Determine the proper freeze point of an instantiation
10866 if Is_Generic_Instance (Inst) then
10868 -- When the instantiation occurs in a package spec, append the
10869 -- freeze node to the private declarations (if any).
10871 if Nkind (Par_N) = N_Package_Specification
10872 and then Decls = Visible_Declarations (Par_N)
10873 and then not Is_Empty_List (Private_Declarations (Par_N))
10875 Decls := Private_Declarations (Par_N);
10876 Decl := First (Decls);
10879 -- We adhere to the general rule of a package or subprogram body
10880 -- causing freezing of anything before it in the same declarative
10881 -- region. In this respect, the proper freeze point of a package
10882 -- instantiation is before the first source body which follows, or
10883 -- before a stub. This ensures that entities from the instance are
10884 -- already frozen and therefore usable in source bodies.
10886 if Nkind (Par_N) /= N_Package_Declaration
10888 not In_Same_Source_Unit (Generic_Parent (Par_Inst), Inst)
10890 while Present (Decl) loop
10891 if ((Nkind (Decl) in N_Unit_Body
10893 Nkind (Decl) in N_Body_Stub)
10894 and then Comes_From_Source (Decl))
10895 or else (Present (Origin)
10896 and then Nkind (Decl) in N_Generic_Instantiation
10897 and then Instance_Spec (Decl) /= Origin)
10899 Set_Sloc (F_Node, Sloc (Decl));
10900 Insert_Before (Decl, F_Node);
10908 -- When the instantiation occurs in a package spec and there is
10909 -- no source body which follows, and the package has a body but
10910 -- is delayed, then insert immediately before its freeze node.
10912 if Nkind (Par_N) = N_Package_Specification
10913 and then Present (Corresponding_Body (Parent (Par_N)))
10914 and then Present (Freeze_Node (Defining_Entity (Par_N)))
10916 Set_Sloc (F_Node, Sloc (Freeze_Node (Defining_Entity (Par_N))));
10917 Insert_Before (Freeze_Node (Defining_Entity (Par_N)), F_Node);
10920 -- When the instantiation occurs in a package spec and there is
10921 -- no source body which follows, not even of the package itself,
10922 -- then insert into the declaration list of the outer level, but
10923 -- do not jump over following instantiations in this list because
10924 -- they may have a body that has not materialized yet, see above.
10926 elsif Nkind (Par_N) = N_Package_Specification
10927 and then No (Corresponding_Body (Parent (Par_N)))
10928 and then Is_List_Member (Parent (Par_N))
10930 Decl := Parent (Par_N);
10931 Decls := List_Containing (Decl);
10932 Par_N := Parent (Decls);
10935 -- In a package declaration, or if no source body which follows
10936 -- and at library level, then insert at end of list.
10944 -- Insert and adjust the Sloc of the freeze node
10946 Set_Sloc (F_Node, Sloc (Last (Decls)));
10947 Insert_After (Last (Decls), F_Node);
10948 end Insert_Freeze_Node_For_Instance;
10950 -----------------------------
10951 -- Install_Formal_Packages --
10952 -----------------------------
10954 procedure Install_Formal_Packages (Par : Entity_Id) is
10957 Gen_E : Entity_Id := Empty;
10960 E := First_Entity (Par);
10962 -- If we are installing an instance parent, locate the formal packages
10963 -- of its generic parent.
10965 if Is_Generic_Instance (Par) then
10966 Gen := Generic_Parent (Package_Specification (Par));
10967 Gen_E := First_Entity (Gen);
10970 while Present (E) loop
10971 if Ekind (E) = E_Package
10972 and then Nkind (Parent (E)) = N_Package_Renaming_Declaration
10974 -- If this is the renaming for the parent instance, done
10976 if Renamed_Entity (E) = Par then
10979 -- The visibility of a formal of an enclosing generic is already
10982 elsif Denotes_Formal_Package (E) then
10985 elsif Present (Associated_Formal_Package (E)) then
10986 Check_Generic_Actuals (Renamed_Entity (E), True);
10987 Set_Is_Hidden (E, False);
10989 -- Find formal package in generic unit that corresponds to
10990 -- (instance of) formal package in instance.
10992 while Present (Gen_E) and then Chars (Gen_E) /= Chars (E) loop
10993 Next_Entity (Gen_E);
10996 if Present (Gen_E) then
10997 Map_Formal_Package_Entities (Gen_E, E);
11004 if Present (Gen_E) then
11005 Next_Entity (Gen_E);
11008 end Install_Formal_Packages;
11010 --------------------
11011 -- Install_Parent --
11012 --------------------
11014 procedure Install_Parent (P : Entity_Id; In_Body : Boolean := False) is
11015 Ancestors : constant Elist_Id := New_Elmt_List;
11016 S : constant Entity_Id := Current_Scope;
11017 Inst_Par : Entity_Id;
11018 First_Par : Entity_Id;
11019 Inst_Node : Node_Id;
11020 Gen_Par : Entity_Id;
11021 First_Gen : Entity_Id;
11024 procedure Install_Noninstance_Specs (Par : Entity_Id);
11025 -- Install the scopes of noninstance parent units ending with Par
11027 procedure Install_Spec (Par : Entity_Id);
11028 -- The child unit is within the declarative part of the parent, so the
11029 -- declarations within the parent are immediately visible.
11031 -------------------------------
11032 -- Install_Noninstance_Specs --
11033 -------------------------------
11035 procedure Install_Noninstance_Specs (Par : Entity_Id) is
11038 and then Par /= Standard_Standard
11039 and then not In_Open_Scopes (Par)
11041 Install_Noninstance_Specs (Scope (Par));
11042 Install_Spec (Par);
11044 end Install_Noninstance_Specs;
11050 procedure Install_Spec (Par : Entity_Id) is
11051 Spec : constant Node_Id := Package_Specification (Par);
11054 -- If this parent of the child instance is a top-level unit,
11055 -- then record the unit and its visibility for later resetting in
11056 -- Remove_Parent. We exclude units that are generic instances, as we
11057 -- only want to record this information for the ultimate top-level
11058 -- noninstance parent (is that always correct???).
11060 if Scope (Par) = Standard_Standard
11061 and then not Is_Generic_Instance (Par)
11063 Parent_Unit_Visible := Is_Immediately_Visible (Par);
11064 Instance_Parent_Unit := Par;
11067 -- Open the parent scope and make it and its declarations visible.
11068 -- If this point is not within a body, then only the visible
11069 -- declarations should be made visible, and installation of the
11070 -- private declarations is deferred until the appropriate point
11071 -- within analysis of the spec being instantiated (see the handling
11072 -- of parent visibility in Analyze_Package_Specification). This is
11073 -- relaxed in the case where the parent unit is Ada.Tags, to avoid
11074 -- private view problems that occur when compiling instantiations of
11075 -- a generic child of that package (Generic_Dispatching_Constructor).
11076 -- If the instance freezes a tagged type, inlinings of operations
11077 -- from Ada.Tags may need the full view of type Tag. If inlining took
11078 -- proper account of establishing visibility of inlined subprograms'
11079 -- parents then it should be possible to remove this
11080 -- special check. ???
11083 Set_Is_Immediately_Visible (Par);
11084 Install_Visible_Declarations (Par);
11085 Set_Use (Visible_Declarations (Spec));
11087 if In_Body or else Is_RTU (Par, Ada_Tags) then
11088 Install_Private_Declarations (Par);
11089 Set_Use (Private_Declarations (Spec));
11093 -- Start of processing for Install_Parent
11096 -- We need to install the parent instance to compile the instantiation
11097 -- of the child, but the child instance must appear in the current
11098 -- scope. Given that we cannot place the parent above the current scope
11099 -- in the scope stack, we duplicate the current scope and unstack both
11100 -- after the instantiation is complete.
11102 -- If the parent is itself the instantiation of a child unit, we must
11103 -- also stack the instantiation of its parent, and so on. Each such
11104 -- ancestor is the prefix of the name in a prior instantiation.
11106 -- If this is a nested instance, the parent unit itself resolves to
11107 -- a renaming of the parent instance, whose declaration we need.
11109 -- Finally, the parent may be a generic (not an instance) when the
11110 -- child unit appears as a formal package.
11114 if Present (Renamed_Entity (Inst_Par)) then
11115 Inst_Par := Renamed_Entity (Inst_Par);
11118 First_Par := Inst_Par;
11120 Gen_Par := Generic_Parent (Package_Specification (Inst_Par));
11122 First_Gen := Gen_Par;
11124 while Present (Gen_Par) and then Is_Child_Unit (Gen_Par) loop
11126 -- Load grandparent instance as well
11128 Inst_Node := Get_Unit_Instantiation_Node (Inst_Par);
11130 if Nkind (Name (Inst_Node)) = N_Expanded_Name then
11131 Inst_Par := Entity (Prefix (Name (Inst_Node)));
11133 if Present (Renamed_Entity (Inst_Par)) then
11134 Inst_Par := Renamed_Entity (Inst_Par);
11137 Gen_Par := Generic_Parent (Package_Specification (Inst_Par));
11139 if Present (Gen_Par) then
11140 Prepend_Elmt (Inst_Par, Ancestors);
11143 -- Parent is not the name of an instantiation
11145 Install_Noninstance_Specs (Inst_Par);
11156 if Present (First_Gen) then
11157 Append_Elmt (First_Par, Ancestors);
11159 Install_Noninstance_Specs (First_Par);
11162 if not Is_Empty_Elmt_List (Ancestors) then
11163 Elmt := First_Elmt (Ancestors);
11164 while Present (Elmt) loop
11165 Install_Spec (Node (Elmt));
11166 Install_Formal_Packages (Node (Elmt));
11171 if not In_Body then
11174 end Install_Parent;
11176 -------------------------------
11177 -- Install_Hidden_Primitives --
11178 -------------------------------
11180 procedure Install_Hidden_Primitives
11181 (Prims_List : in out Elist_Id;
11186 List : Elist_Id := No_Elist;
11187 Prim_G_Elmt : Elmt_Id;
11188 Prim_A_Elmt : Elmt_Id;
11193 -- No action needed in case of serious errors because we cannot trust
11194 -- in the order of primitives
11196 if Serious_Errors_Detected > 0 then
11199 -- No action possible if we don't have available the list of primitive
11203 or else not Is_Record_Type (Gen_T)
11204 or else not Is_Tagged_Type (Gen_T)
11205 or else not Is_Record_Type (Act_T)
11206 or else not Is_Tagged_Type (Act_T)
11210 -- There is no need to handle interface types since their primitives
11211 -- cannot be hidden
11213 elsif Is_Interface (Gen_T) then
11217 Prim_G_Elmt := First_Elmt (Primitive_Operations (Gen_T));
11219 if not Is_Class_Wide_Type (Act_T) then
11220 Prim_A_Elmt := First_Elmt (Primitive_Operations (Act_T));
11222 Prim_A_Elmt := First_Elmt (Primitive_Operations (Root_Type (Act_T)));
11226 -- Skip predefined primitives in the generic formal
11228 while Present (Prim_G_Elmt)
11229 and then Is_Predefined_Dispatching_Operation (Node (Prim_G_Elmt))
11231 Next_Elmt (Prim_G_Elmt);
11234 -- Skip predefined primitives in the generic actual
11236 while Present (Prim_A_Elmt)
11237 and then Is_Predefined_Dispatching_Operation (Node (Prim_A_Elmt))
11239 Next_Elmt (Prim_A_Elmt);
11242 exit when No (Prim_G_Elmt) or else No (Prim_A_Elmt);
11244 Prim_G := Node (Prim_G_Elmt);
11245 Prim_A := Node (Prim_A_Elmt);
11247 -- There is no need to handle interface primitives because their
11248 -- primitives are not hidden
11250 exit when Present (Interface_Alias (Prim_G));
11252 -- Here we install one hidden primitive
11254 if Chars (Prim_G) /= Chars (Prim_A)
11255 and then Has_Suffix (Prim_A, 'P')
11256 and then Remove_Suffix (Prim_A, 'P') = Chars (Prim_G)
11258 Set_Chars (Prim_A, Chars (Prim_G));
11259 Append_New_Elmt (Prim_A, To => List);
11262 Next_Elmt (Prim_A_Elmt);
11263 Next_Elmt (Prim_G_Elmt);
11266 -- Append the elements to the list of temporarily visible primitives
11267 -- avoiding duplicates.
11269 if Present (List) then
11270 if No (Prims_List) then
11271 Prims_List := New_Elmt_List;
11274 Elmt := First_Elmt (List);
11275 while Present (Elmt) loop
11276 Append_Unique_Elmt (Node (Elmt), Prims_List);
11280 end Install_Hidden_Primitives;
11282 ---------------------------------
11283 -- Renames_Standard_Subprogram --
11284 ---------------------------------
11286 function Renames_Standard_Subprogram (Subp : Entity_Id) return Boolean is
11290 Id := Alias (Subp);
11291 while Present (Id) loop
11292 if Scope (Id) = Standard_Standard then
11300 end Renames_Standard_Subprogram;
11302 -------------------------------
11303 -- Restore_Hidden_Primitives --
11304 -------------------------------
11306 procedure Restore_Hidden_Primitives (Prims_List : in out Elist_Id) is
11307 Prim_Elmt : Elmt_Id;
11311 if Present (Prims_List) then
11312 Prim_Elmt := First_Elmt (Prims_List);
11313 while Present (Prim_Elmt) loop
11314 Prim := Node (Prim_Elmt);
11315 Set_Chars (Prim, Add_Suffix (Prim, 'P'));
11316 Next_Elmt (Prim_Elmt);
11319 Prims_List := No_Elist;
11321 end Restore_Hidden_Primitives;
11323 --------------------------------
11324 -- Instantiate_Formal_Package --
11325 --------------------------------
11327 function Instantiate_Formal_Package
11330 Analyzed_Formal : Node_Id) return List_Id
11332 Loc : constant Source_Ptr := Sloc (Actual);
11333 Hidden_Formals : constant Elist_Id := New_Elmt_List;
11335 Actual_Pack : Entity_Id;
11336 Formal_Pack : Entity_Id;
11337 Gen_Parent : Entity_Id;
11340 Parent_Spec : Node_Id;
11342 procedure Find_Matching_Actual
11344 Act : in out Entity_Id);
11345 -- We need to associate each formal entity in the formal package with
11346 -- the corresponding entity in the actual package. The actual package
11347 -- has been analyzed and possibly expanded, and as a result there is
11348 -- no one-to-one correspondence between the two lists (for example,
11349 -- the actual may include subtypes, itypes, and inherited primitive
11350 -- operations, interspersed among the renaming declarations for the
11351 -- actuals). We retrieve the corresponding actual by name because each
11352 -- actual has the same name as the formal, and they do appear in the
11355 function Get_Formal_Entity (N : Node_Id) return Entity_Id;
11356 -- Retrieve entity of defining entity of generic formal parameter.
11357 -- Only the declarations of formals need to be considered when
11358 -- linking them to actuals, but the declarative list may include
11359 -- internal entities generated during analysis, and those are ignored.
11361 procedure Match_Formal_Entity
11362 (Formal_Node : Node_Id;
11363 Formal_Ent : Entity_Id;
11364 Actual_Ent : Entity_Id);
11365 -- Associates the formal entity with the actual. In the case where
11366 -- Formal_Ent is a formal package, this procedure iterates through all
11367 -- of its formals and enters associations between the actuals occurring
11368 -- in the formal package's corresponding actual package (given by
11369 -- Actual_Ent) and the formal package's formal parameters. This
11370 -- procedure recurses if any of the parameters is itself a package.
11372 function Is_Instance_Of
11373 (Act_Spec : Entity_Id;
11374 Gen_Anc : Entity_Id) return Boolean;
11375 -- The actual can be an instantiation of a generic within another
11376 -- instance, in which case there is no direct link from it to the
11377 -- original generic ancestor. In that case, we recognize that the
11378 -- ultimate ancestor is the same by examining names and scopes.
11380 procedure Process_Nested_Formal (Formal : Entity_Id);
11381 -- If the current formal is declared with a box, its own formals are
11382 -- visible in the instance, as they were in the generic, and their
11383 -- Hidden flag must be reset. If some of these formals are themselves
11384 -- packages declared with a box, the processing must be recursive.
11386 --------------------------
11387 -- Find_Matching_Actual --
11388 --------------------------
11390 procedure Find_Matching_Actual
11392 Act : in out Entity_Id)
11394 Formal_Ent : Entity_Id;
11397 case Nkind (Original_Node (F)) is
11398 when N_Formal_Object_Declaration
11399 | N_Formal_Type_Declaration
11401 Formal_Ent := Defining_Identifier (F);
11403 while Present (Act)
11404 and then Chars (Act) /= Chars (Formal_Ent)
11409 when N_Formal_Package_Declaration
11410 | N_Formal_Subprogram_Declaration
11411 | N_Generic_Package_Declaration
11412 | N_Package_Declaration
11414 Formal_Ent := Defining_Entity (F);
11416 while Present (Act)
11417 and then Chars (Act) /= Chars (Formal_Ent)
11423 raise Program_Error;
11425 end Find_Matching_Actual;
11427 -------------------------
11428 -- Match_Formal_Entity --
11429 -------------------------
11431 procedure Match_Formal_Entity
11432 (Formal_Node : Node_Id;
11433 Formal_Ent : Entity_Id;
11434 Actual_Ent : Entity_Id)
11436 Act_Pkg : Entity_Id;
11439 Set_Instance_Of (Formal_Ent, Actual_Ent);
11441 if Ekind (Actual_Ent) = E_Package then
11443 -- Record associations for each parameter
11445 Act_Pkg := Actual_Ent;
11448 A_Ent : Entity_Id := First_Entity (Act_Pkg);
11452 Gen_Decl : Node_Id;
11454 Actual : Entity_Id;
11457 -- Retrieve the actual given in the formal package declaration
11459 Actual := Entity (Name (Original_Node (Formal_Node)));
11461 -- The actual in the formal package declaration may be a
11462 -- renamed generic package, in which case we want to retrieve
11463 -- the original generic in order to traverse its formal part.
11465 if Present (Renamed_Entity (Actual)) then
11466 Gen_Decl := Unit_Declaration_Node (Renamed_Entity (Actual));
11468 Gen_Decl := Unit_Declaration_Node (Actual);
11471 Formals := Generic_Formal_Declarations (Gen_Decl);
11473 if Present (Formals) then
11474 F_Node := First_Non_Pragma (Formals);
11479 while Present (A_Ent)
11480 and then Present (F_Node)
11481 and then A_Ent /= First_Private_Entity (Act_Pkg)
11483 F_Ent := Get_Formal_Entity (F_Node);
11485 if Present (F_Ent) then
11487 -- This is a formal of the original package. Record
11488 -- association and recurse.
11490 Find_Matching_Actual (F_Node, A_Ent);
11491 Match_Formal_Entity (F_Node, F_Ent, A_Ent);
11492 Next_Entity (A_Ent);
11495 Next_Non_Pragma (F_Node);
11499 end Match_Formal_Entity;
11501 -----------------------
11502 -- Get_Formal_Entity --
11503 -----------------------
11505 function Get_Formal_Entity (N : Node_Id) return Entity_Id is
11506 Kind : constant Node_Kind := Nkind (Original_Node (N));
11509 when N_Formal_Object_Declaration =>
11510 return Defining_Identifier (N);
11512 when N_Formal_Type_Declaration =>
11513 return Defining_Identifier (N);
11515 when N_Formal_Subprogram_Declaration =>
11516 return Defining_Unit_Name (Specification (N));
11518 when N_Formal_Package_Declaration =>
11519 return Defining_Identifier (Original_Node (N));
11521 when N_Generic_Package_Declaration =>
11522 return Defining_Identifier (Original_Node (N));
11524 -- All other declarations are introduced by semantic analysis and
11525 -- have no match in the actual.
11530 end Get_Formal_Entity;
11532 --------------------
11533 -- Is_Instance_Of --
11534 --------------------
11536 function Is_Instance_Of
11537 (Act_Spec : Entity_Id;
11538 Gen_Anc : Entity_Id) return Boolean
11540 Gen_Par : constant Entity_Id := Generic_Parent (Act_Spec);
11543 if No (Gen_Par) then
11546 -- Simplest case: the generic parent of the actual is the formal
11548 elsif Gen_Par = Gen_Anc then
11551 elsif Chars (Gen_Par) /= Chars (Gen_Anc) then
11554 -- The actual may be obtained through several instantiations. Its
11555 -- scope must itself be an instance of a generic declared in the
11556 -- same scope as the formal. Any other case is detected above.
11558 elsif not Is_Generic_Instance (Scope (Gen_Par)) then
11562 return Generic_Parent (Parent (Scope (Gen_Par))) = Scope (Gen_Anc);
11564 end Is_Instance_Of;
11566 ---------------------------
11567 -- Process_Nested_Formal --
11568 ---------------------------
11570 procedure Process_Nested_Formal (Formal : Entity_Id) is
11574 if Present (Associated_Formal_Package (Formal))
11575 and then Box_Present (Parent (Associated_Formal_Package (Formal)))
11577 Ent := First_Entity (Formal);
11578 while Present (Ent) loop
11579 Set_Is_Hidden (Ent, False);
11580 Set_Is_Visible_Formal (Ent);
11581 Set_Is_Potentially_Use_Visible
11582 (Ent, Is_Potentially_Use_Visible (Formal));
11584 if Ekind (Ent) = E_Package then
11585 exit when Renamed_Entity (Ent) = Renamed_Entity (Formal);
11586 Process_Nested_Formal (Ent);
11592 end Process_Nested_Formal;
11594 -- Start of processing for Instantiate_Formal_Package
11599 -- The actual must be a package instance, or else a current instance
11600 -- such as a parent generic within the body of a generic child.
11602 if not Is_Entity_Name (Actual)
11603 or else not Is_Package_Or_Generic_Package (Entity (Actual))
11606 ("expect package instance to instantiate formal", Actual);
11607 Abandon_Instantiation (Actual);
11610 Actual_Pack := Entity (Actual);
11611 Set_Is_Instantiated (Actual_Pack);
11613 -- The actual may be a renamed package, or an outer generic formal
11614 -- package whose instantiation is converted into a renaming.
11616 if Present (Renamed_Entity (Actual_Pack)) then
11617 Actual_Pack := Renamed_Entity (Actual_Pack);
11620 -- The analyzed formal is expected to be the result of the rewriting
11621 -- of the formal package into a regular package by analysis.
11623 pragma Assert (Nkind (Analyzed_Formal) = N_Package_Declaration
11624 and then Nkind (Original_Node (Analyzed_Formal)) =
11625 N_Formal_Package_Declaration);
11627 Gen_Parent := Generic_Parent (Specification (Analyzed_Formal));
11628 Formal_Pack := Defining_Unit_Name (Specification (Analyzed_Formal));
11630 -- The actual for a ghost generic formal package should be a ghost
11631 -- package (SPARK RM 6.9(14)).
11633 Check_Ghost_Formal_Procedure_Or_Package
11635 Actual => Actual_Pack,
11636 Formal => Formal_Pack);
11638 if Nkind (Parent (Actual_Pack)) = N_Defining_Program_Unit_Name then
11639 Parent_Spec := Package_Specification (Actual_Pack);
11641 Parent_Spec := Parent (Actual_Pack);
11644 if Gen_Parent = Any_Id then
11646 ("previous error in declaration of formal package", Actual);
11647 Abandon_Instantiation (Actual);
11649 elsif Is_Instance_Of (Parent_Spec, Get_Instance_Of (Gen_Parent)) then
11652 -- If this is the current instance of an enclosing generic, that unit
11653 -- is the generic package we need.
11655 elsif In_Open_Scopes (Actual_Pack)
11656 and then Ekind (Actual_Pack) = E_Generic_Package
11662 ("actual parameter must be instance of&", Actual, Gen_Parent);
11663 Abandon_Instantiation (Actual);
11666 Set_Instance_Of (Defining_Identifier (Formal), Actual_Pack);
11667 Map_Formal_Package_Entities (Formal_Pack, Actual_Pack);
11670 Make_Package_Renaming_Declaration (Loc,
11671 Defining_Unit_Name => New_Copy (Defining_Identifier (Formal)),
11672 Name => New_Occurrence_Of (Actual_Pack, Loc));
11674 Set_Associated_Formal_Package
11675 (Defining_Unit_Name (Nod), Defining_Identifier (Formal));
11676 Decls := New_List (Nod);
11678 -- If the formal F has a box, then the generic declarations are
11679 -- visible in the generic G. In an instance of G, the corresponding
11680 -- entities in the actual for F (which are the actuals for the
11681 -- instantiation of the generic that F denotes) must also be made
11682 -- visible for analysis of the current instance. On exit from the
11683 -- current instance, those entities are made private again. If the
11684 -- actual is currently in use, these entities are also use-visible.
11686 -- The loop through the actual entities also steps through the formal
11687 -- entities and enters associations from formals to actuals into the
11688 -- renaming map. This is necessary to properly handle checking of
11689 -- actual parameter associations for later formals that depend on
11690 -- actuals declared in the formal package.
11692 -- In Ada 2005, partial parameterization requires that we make
11693 -- visible the actuals corresponding to formals that were defaulted
11694 -- in the formal package. There formals are identified because they
11695 -- remain formal generics within the formal package, rather than
11696 -- being renamings of the actuals supplied.
11699 Gen_Decl : constant Node_Id :=
11700 Unit_Declaration_Node (Gen_Parent);
11701 Formals : constant List_Id :=
11702 Generic_Formal_Declarations (Gen_Decl);
11704 Actual_Ent : Entity_Id;
11705 Actual_Of_Formal : Node_Id;
11706 Formal_Node : Node_Id;
11707 Formal_Ent : Entity_Id;
11710 if Present (Formals) then
11711 Formal_Node := First_Non_Pragma (Formals);
11713 Formal_Node := Empty;
11716 Actual_Ent := First_Entity (Actual_Pack);
11717 Actual_Of_Formal :=
11718 First (Visible_Declarations (Specification (Analyzed_Formal)));
11719 while Present (Actual_Ent)
11720 and then Actual_Ent /= First_Private_Entity (Actual_Pack)
11722 if Present (Formal_Node) then
11723 Formal_Ent := Get_Formal_Entity (Formal_Node);
11725 if Present (Formal_Ent) then
11726 Find_Matching_Actual (Formal_Node, Actual_Ent);
11727 Match_Formal_Entity (Formal_Node, Formal_Ent, Actual_Ent);
11729 -- We iterate at the same time over the actuals of the
11730 -- local package created for the formal, to determine
11731 -- which one of the formals of the original generic were
11732 -- defaulted in the formal. The corresponding actual
11733 -- entities are visible in the enclosing instance.
11735 if Box_Present (Formal)
11737 (Present (Actual_Of_Formal)
11740 (Get_Formal_Entity (Actual_Of_Formal)))
11742 Set_Is_Hidden (Actual_Ent, False);
11743 Set_Is_Visible_Formal (Actual_Ent);
11744 Set_Is_Potentially_Use_Visible
11745 (Actual_Ent, In_Use (Actual_Pack));
11747 if Ekind (Actual_Ent) = E_Package then
11748 Process_Nested_Formal (Actual_Ent);
11752 if not Is_Hidden (Actual_Ent) then
11753 Append_Elmt (Actual_Ent, Hidden_Formals);
11756 Set_Is_Hidden (Actual_Ent);
11757 Set_Is_Potentially_Use_Visible (Actual_Ent, False);
11761 Next_Non_Pragma (Formal_Node);
11762 Next (Actual_Of_Formal);
11764 -- A formal subprogram may be overloaded, so advance in
11765 -- the list of actuals to make sure we do not match two
11766 -- successive formals to the same actual. This is only
11767 -- relevant for overloadable entities, others have
11770 if Is_Overloadable (Actual_Ent) then
11771 Next_Entity (Actual_Ent);
11775 -- No further formals to match, but the generic part may
11776 -- contain inherited operation that are not hidden in the
11777 -- enclosing instance.
11779 Next_Entity (Actual_Ent);
11783 -- Inherited subprograms generated by formal derived types are
11784 -- also visible if the types are.
11786 Actual_Ent := First_Entity (Actual_Pack);
11787 while Present (Actual_Ent)
11788 and then Actual_Ent /= First_Private_Entity (Actual_Pack)
11790 if Is_Overloadable (Actual_Ent)
11792 Nkind (Parent (Actual_Ent)) = N_Subtype_Declaration
11794 not Is_Hidden (Defining_Identifier (Parent (Actual_Ent)))
11796 Set_Is_Hidden (Actual_Ent, False);
11797 Set_Is_Potentially_Use_Visible
11798 (Actual_Ent, In_Use (Actual_Pack));
11801 Next_Entity (Actual_Ent);
11805 -- If the formal requires conformance checking, reanalyze it as an
11806 -- abbreviated instantiation, to verify the matching rules of 12.7.
11807 -- The actual checks are performed after the generic associations
11808 -- have been analyzed, to guarantee the same visibility for this
11809 -- instantiation and for the actuals.
11811 -- In Ada 2005, the generic associations for the formal can include
11812 -- defaulted parameters. These are ignored during check. This
11813 -- internal instantiation is removed from the tree after conformance
11814 -- checking, because it contains formal declarations for those
11815 -- defaulted parameters, and those should not reach the back-end.
11817 if Requires_Conformance_Checking (Formal) then
11819 I_Pack : constant Entity_Id := Make_Temporary (Loc, 'P');
11822 Set_Is_Internal (I_Pack);
11823 Mutate_Ekind (I_Pack, E_Package);
11825 -- Insert the package into the list of its hidden entities so
11826 -- that the list is not empty for Is_Abbreviated_Instance.
11828 Append_Elmt (I_Pack, Hidden_Formals);
11830 Set_Hidden_In_Formal_Instance (I_Pack, Hidden_Formals);
11832 -- If the generic is a child unit, Check_Generic_Child_Unit
11833 -- needs its original name in case it is qualified.
11835 if Is_Child_Unit (Gen_Parent) then
11837 New_Copy_Tree (Name (Original_Node (Analyzed_Formal)));
11838 pragma Assert (Entity (I_Nam) = Gen_Parent
11839 or else Renamed_Entity (Entity (I_Nam)) = Gen_Parent);
11843 New_Occurrence_Of (Get_Instance_Of (Gen_Parent), Loc);
11847 Make_Package_Instantiation (Loc,
11848 Defining_Unit_Name => I_Pack,
11850 Generic_Associations => Generic_Associations (Formal)));
11856 end Instantiate_Formal_Package;
11858 -----------------------------------
11859 -- Instantiate_Formal_Subprogram --
11860 -----------------------------------
11862 function Instantiate_Formal_Subprogram
11865 Analyzed_Formal : Node_Id) return Node_Id
11867 Analyzed_S : constant Entity_Id :=
11868 Defining_Unit_Name (Specification (Analyzed_Formal));
11869 Formal_Sub : constant Entity_Id :=
11870 Defining_Unit_Name (Specification (Formal));
11872 function From_Parent_Scope (Subp : Entity_Id) return Boolean;
11873 -- If the generic is a child unit, the parent has been installed on the
11874 -- scope stack, but a default subprogram cannot resolve to something
11875 -- on the parent because that parent is not really part of the visible
11876 -- context (it is there to resolve explicit local entities). If the
11877 -- default has resolved in this way, we remove the entity from immediate
11878 -- visibility and analyze the node again to emit an error message or
11879 -- find another visible candidate.
11881 procedure Valid_Actual_Subprogram (Act : Node_Id);
11882 -- Perform legality check and raise exception on failure
11884 -----------------------
11885 -- From_Parent_Scope --
11886 -----------------------
11888 function From_Parent_Scope (Subp : Entity_Id) return Boolean is
11889 Gen_Scope : Node_Id;
11892 Gen_Scope := Scope (Analyzed_S);
11893 while Present (Gen_Scope) and then Is_Child_Unit (Gen_Scope) loop
11894 if Scope (Subp) = Scope (Gen_Scope) then
11898 Gen_Scope := Scope (Gen_Scope);
11902 end From_Parent_Scope;
11904 -----------------------------
11905 -- Valid_Actual_Subprogram --
11906 -----------------------------
11908 procedure Valid_Actual_Subprogram (Act : Node_Id) is
11912 if Is_Entity_Name (Act) then
11913 Act_E := Entity (Act);
11915 elsif Nkind (Act) = N_Selected_Component
11916 and then Is_Entity_Name (Selector_Name (Act))
11918 Act_E := Entity (Selector_Name (Act));
11924 -- The actual for a ghost generic formal procedure should be a ghost
11925 -- procedure (SPARK RM 6.9(14)).
11928 and then Ekind (Act_E) = E_Procedure
11930 Check_Ghost_Formal_Procedure_Or_Package
11933 Formal => Analyzed_S);
11936 if (Present (Act_E) and then Is_Overloadable (Act_E))
11937 or else Nkind (Act) in N_Attribute_Reference
11938 | N_Indexed_Component
11939 | N_Character_Literal
11940 | N_Explicit_Dereference
11946 ("expect subprogram or entry name in instantiation of &",
11947 Instantiation_Node, Formal_Sub);
11948 Abandon_Instantiation (Instantiation_Node);
11949 end Valid_Actual_Subprogram;
11953 Decl_Node : Node_Id;
11956 New_Spec : Node_Id;
11957 New_Subp : Entity_Id;
11959 -- Start of processing for Instantiate_Formal_Subprogram
11962 New_Spec := New_Copy_Tree (Specification (Formal));
11964 -- The tree copy has created the proper instantiation sloc for the
11965 -- new specification. Use this location for all other constructed
11968 Loc := Sloc (Defining_Unit_Name (New_Spec));
11970 -- Create new entity for the actual (New_Copy_Tree does not), and
11971 -- indicate that it is an actual.
11973 -- If the actual is not an entity (i.e. an attribute reference)
11974 -- and the formal includes aspect specifications for contracts,
11975 -- we create an internal name for the renaming declaration. The
11976 -- constructed wrapper contains a call to the entity in the renaming.
11977 -- This is an expansion activity, as is the wrapper creation.
11979 if Ada_Version >= Ada_2022
11980 and then Has_Contracts (Analyzed_Formal)
11981 and then not Is_Entity_Name (Actual)
11982 and then Expander_Active
11984 New_Subp := Make_Temporary (Sloc (Actual), 'S');
11986 New_Subp := Make_Defining_Identifier (Loc, Chars (Formal_Sub));
11989 Mutate_Ekind (New_Subp, Ekind (Analyzed_S));
11990 Set_Is_Generic_Actual_Subprogram (New_Subp);
11991 Set_Defining_Unit_Name (New_Spec, New_Subp);
11993 -- Create new entities for the each of the formals in the specification
11994 -- of the renaming declaration built for the actual.
11996 if Present (Parameter_Specifications (New_Spec)) then
12002 F := First (Parameter_Specifications (New_Spec));
12003 while Present (F) loop
12004 F_Id := Defining_Identifier (F);
12006 Set_Defining_Identifier (F,
12007 Make_Defining_Identifier (Sloc (F_Id), Chars (F_Id)));
12013 -- Find entity of actual. If the actual is an attribute reference, it
12014 -- cannot be resolved here (its formal is missing) but is handled
12015 -- instead in Attribute_Renaming. If the actual is overloaded, it is
12016 -- fully resolved subsequently, when the renaming declaration for the
12017 -- formal is analyzed. If it is an explicit dereference, resolve the
12018 -- prefix but not the actual itself, to prevent interpretation as call.
12020 if Present (Actual) then
12021 Loc := Sloc (Actual);
12022 Set_Sloc (New_Spec, Loc);
12024 if Nkind (Actual) = N_Operator_Symbol then
12025 Find_Direct_Name (Actual);
12027 elsif Nkind (Actual) = N_Explicit_Dereference then
12028 Analyze (Prefix (Actual));
12030 elsif Nkind (Actual) /= N_Attribute_Reference then
12034 Valid_Actual_Subprogram (Actual);
12037 elsif Present (Default_Name (Formal)) then
12038 if Nkind (Default_Name (Formal)) not in N_Attribute_Reference
12039 | N_Selected_Component
12040 | N_Indexed_Component
12041 | N_Character_Literal
12042 and then Present (Entity (Default_Name (Formal)))
12044 Nam := New_Occurrence_Of (Entity (Default_Name (Formal)), Loc);
12046 Nam := New_Copy (Default_Name (Formal));
12047 Set_Sloc (Nam, Loc);
12050 elsif Box_Present (Formal) then
12052 -- Actual is resolved at the point of instantiation. Create an
12053 -- identifier or operator with the same name as the formal.
12055 if Nkind (Formal_Sub) = N_Defining_Operator_Symbol then
12057 Make_Operator_Symbol (Loc,
12058 Chars => Chars (Formal_Sub),
12059 Strval => No_String);
12061 Nam := Make_Identifier (Loc, Chars (Formal_Sub));
12064 elsif Has_Null_Default (Formal) then
12065 -- Generate null body for procedure, for use in the instance
12068 Make_Subprogram_Body (Loc,
12069 Specification => New_Spec,
12070 Declarations => New_List,
12071 Handled_Statement_Sequence =>
12072 Make_Handled_Sequence_Of_Statements (Loc,
12073 Statements => New_List (Make_Null_Statement (Loc))));
12075 -- RM 12.6 (16.2/2): The procedure has convention Intrinsic
12077 Set_Convention (Defining_Unit_Name (New_Spec), Convention_Intrinsic);
12079 Copy_Ghost_Aspect (Formal, To => Decl_Node);
12081 -- Eliminate the calls to it when optimization is enabled
12083 Set_Is_Inlined (Defining_Unit_Name (New_Spec));
12086 -- Handle case of a formal function with an expression default (allowed
12087 -- when extensions are enabled).
12089 elsif Nkind (Specification (Formal)) = N_Function_Specification
12090 and then Present (Expression (Formal))
12092 -- Generate body for function, for use in the instance
12095 Expr : constant Node_Id := New_Copy (Expression (Formal));
12096 Stmt : constant Node_Id := Make_Simple_Return_Statement (Loc);
12098 Set_Sloc (Expr, Loc);
12099 Set_Expression (Stmt, Expr);
12102 Make_Subprogram_Body (Loc,
12103 Specification => New_Spec,
12104 Declarations => New_List,
12105 Handled_Statement_Sequence =>
12106 Make_Handled_Sequence_Of_Statements (Loc,
12107 Statements => New_List (Stmt)));
12110 -- RM 12.6 (16.2/2): Like a null procedure default, the function
12111 -- has convention Intrinsic.
12113 Set_Convention (Defining_Unit_Name (New_Spec), Convention_Intrinsic);
12115 -- Inline calls to it when optimization is enabled
12117 Set_Is_Inlined (Defining_Unit_Name (New_Spec));
12121 pragma Assert (False);
12125 Make_Subprogram_Renaming_Declaration (Loc,
12126 Specification => New_Spec,
12129 -- If we do not have an actual and the formal specified <> then set to
12130 -- get proper default.
12132 if No (Actual) and then Box_Present (Formal) then
12133 Set_From_Default (Decl_Node);
12136 -- Gather possible interpretations for the actual before analyzing the
12137 -- instance. If overloaded, it will be resolved when analyzing the
12138 -- renaming declaration.
12140 if Box_Present (Formal) and then No (Actual) then
12143 if Is_Child_Unit (Scope (Analyzed_S))
12144 and then Present (Entity (Nam))
12146 if not Is_Overloaded (Nam) then
12147 if From_Parent_Scope (Entity (Nam)) then
12148 Set_Is_Immediately_Visible (Entity (Nam), False);
12149 Set_Entity (Nam, Empty);
12150 Set_Etype (Nam, Empty);
12153 Set_Is_Immediately_Visible (Entity (Nam));
12162 Get_First_Interp (Nam, I, It);
12163 while Present (It.Nam) loop
12164 if From_Parent_Scope (It.Nam) then
12168 Get_Next_Interp (I, It);
12175 -- The generic instantiation freezes the actual. This can only be done
12176 -- once the actual is resolved, in the analysis of the renaming
12177 -- declaration. To make the formal subprogram entity available, we set
12178 -- Corresponding_Formal_Spec to point to the formal subprogram entity.
12179 -- This is also needed in Analyze_Subprogram_Renaming for the processing
12180 -- of formal abstract subprograms.
12182 Set_Corresponding_Formal_Spec (Decl_Node, Analyzed_S);
12184 -- We cannot analyze the renaming declaration, and thus find the actual,
12185 -- until all the actuals are assembled in the instance. For subsequent
12186 -- checks of other actuals, indicate the node that will hold the
12187 -- instance of this formal.
12189 Set_Instance_Of (Analyzed_S, Nam);
12191 if Nkind (Actual) = N_Selected_Component
12192 and then Is_Task_Type (Etype (Prefix (Actual)))
12193 and then not Is_Frozen (Etype (Prefix (Actual)))
12195 -- The renaming declaration will create a body, which must appear
12196 -- outside of the instantiation, We move the renaming declaration
12197 -- out of the instance, and create an additional renaming inside,
12198 -- to prevent freezing anomalies.
12201 Anon_Id : constant Entity_Id := Make_Temporary (Loc, 'E');
12204 Set_Defining_Unit_Name (New_Spec, Anon_Id);
12205 Insert_Before (Instantiation_Node, Decl_Node);
12206 Analyze (Decl_Node);
12208 -- Now create renaming within the instance
12211 Make_Subprogram_Renaming_Declaration (Loc,
12212 Specification => New_Copy_Tree (New_Spec),
12213 Name => New_Occurrence_Of (Anon_Id, Loc));
12215 Set_Defining_Unit_Name (Specification (Decl_Node),
12216 Make_Defining_Identifier (Loc, Chars (Formal_Sub)));
12221 end Instantiate_Formal_Subprogram;
12223 ------------------------
12224 -- Instantiate_Object --
12225 ------------------------
12227 function Instantiate_Object
12230 Analyzed_Formal : Node_Id) return List_Id
12232 Gen_Obj : constant Entity_Id := Defining_Identifier (Formal);
12233 A_Gen_Obj : constant Entity_Id :=
12234 Defining_Identifier (Analyzed_Formal);
12235 Acc_Def : Node_Id := Empty;
12236 Act_Assoc : constant Node_Id :=
12237 (if No (Actual) then Empty else Parent (Actual));
12238 Actual_Decl : Node_Id := Empty;
12239 Decl_Node : Node_Id;
12242 List : constant List_Id := New_List;
12243 Loc : constant Source_Ptr := Sloc (Actual);
12244 Orig_Ftyp : constant Entity_Id := Etype (A_Gen_Obj);
12245 Subt_Decl : Node_Id := Empty;
12246 Subt_Mark : Node_Id := Empty;
12248 -- Start of processing for Instantiate_Object
12251 -- Formal may be an anonymous access
12253 if Present (Subtype_Mark (Formal)) then
12254 Subt_Mark := Subtype_Mark (Formal);
12256 Check_Access_Definition (Formal);
12257 Acc_Def := Access_Definition (Formal);
12260 Set_Parent (List, Act_Assoc);
12264 if Out_Present (Formal) then
12266 -- An IN OUT generic actual must be a name. The instantiation is a
12267 -- renaming declaration. The actual is the name being renamed. We
12268 -- use the actual directly, rather than a copy, because it is not
12269 -- used further in the list of actuals, and because a copy or a use
12270 -- of Relocate_Node is incorrect if the instance is nested within a
12271 -- generic. In order to simplify e.g. ASIS queries, the
12272 -- Generic_Parent field links the declaration to the generic
12275 if Present (Subt_Mark) then
12277 Make_Object_Renaming_Declaration (Loc,
12278 Defining_Identifier => New_Copy (Gen_Obj),
12279 Subtype_Mark => New_Copy_Tree (Subt_Mark),
12282 else pragma Assert (Present (Acc_Def));
12284 Make_Object_Renaming_Declaration (Loc,
12285 Defining_Identifier => New_Copy (Gen_Obj),
12286 Access_Definition => New_Copy_Tree (Acc_Def),
12290 Set_Corresponding_Generic_Association (Decl_Node, Act_Assoc);
12292 -- The analysis of the actual may produce Insert_Action nodes, so
12293 -- the declaration must have a context in which to attach them.
12295 Append (Decl_Node, List);
12298 -- Return if the analysis of the actual reported some error
12300 if Etype (Actual) = Any_Type then
12304 -- This check is performed here because Analyze_Object_Renaming will
12305 -- not check it when Comes_From_Source is False. Note though that the
12306 -- check for the actual being the name of an object will be performed
12307 -- in Analyze_Object_Renaming.
12309 if Is_Object_Reference (Actual)
12310 and then Is_Dependent_Component_Of_Mutable_Object (Actual)
12313 ("illegal discriminant-dependent component for in out parameter",
12315 elsif Depends_On_Mutably_Tagged_Ext_Comp (Actual) then
12317 ("illegal mutably tagged component for in out parameter",
12321 -- The actual has to be resolved in order to check that it is a
12322 -- variable (due to cases such as F (1), where F returns access to
12323 -- an array, and for overloaded prefixes).
12325 Ftyp := Get_Instance_Of (Etype (A_Gen_Obj));
12327 -- If the type of the formal is not itself a formal, and the current
12328 -- unit is a child unit, the formal type must be declared in a
12329 -- parent, and must be retrieved by visibility.
12331 if Ftyp = Orig_Ftyp
12332 and then Is_Generic_Unit (Scope (Ftyp))
12333 and then Is_Child_Unit (Scope (A_Gen_Obj))
12336 Temp : constant Node_Id :=
12337 New_Copy_Tree (Subtype_Mark (Analyzed_Formal));
12339 Set_Entity (Temp, Empty);
12341 Ftyp := Entity (Temp);
12345 if Is_Private_Type (Ftyp)
12346 and then not Is_Private_Type (Etype (Actual))
12347 and then (Base_Type (Full_View (Ftyp)) = Base_Type (Etype (Actual))
12348 or else Base_Type (Etype (Actual)) = Ftyp)
12350 -- If the actual has the type of the full view of the formal, or
12351 -- else a non-private subtype of the formal, then the visibility
12352 -- of the formal type has changed. Add to the actuals a subtype
12353 -- declaration that will force the exchange of views in the body
12354 -- of the instance as well.
12357 Make_Subtype_Declaration (Loc,
12358 Defining_Identifier => Make_Temporary (Loc, 'P'),
12359 Subtype_Indication => New_Occurrence_Of (Ftyp, Loc));
12361 Prepend (Subt_Decl, List);
12363 Prepend_Elmt (Full_View (Ftyp), Exchanged_Views);
12364 Exchange_Declarations (Ftyp);
12367 Resolve (Actual, Ftyp);
12369 if not Denotes_Variable (Actual) then
12370 Error_Msg_NE ("actual for& must be a variable", Actual, Gen_Obj);
12372 elsif Base_Type (Ftyp) /= Base_Type (Etype (Actual)) then
12374 -- Ada 2005 (AI-423): For a generic formal object of mode in out,
12375 -- the type of the actual shall resolve to a specific anonymous
12378 if Ada_Version < Ada_2005
12379 or else not Is_Anonymous_Access_Type (Base_Type (Ftyp))
12380 or else not Is_Anonymous_Access_Type (Base_Type (Etype (Actual)))
12383 ("type of actual does not match type of&", Actual, Gen_Obj);
12387 Note_Possible_Modification (Actual, Sure => True);
12389 -- Check for instantiation with atomic/volatile/VFA object actual for
12390 -- nonatomic/nonvolatile/nonVFA formal (RM C.6 (12)).
12392 if Is_Atomic_Object (Actual) and then not Is_Atomic (Orig_Ftyp) then
12394 ("cannot instantiate nonatomic formal & of mode in out",
12396 Error_Msg_N ("\with atomic object actual (RM C.6(12))", Actual);
12398 elsif Is_Volatile_Object_Ref (Actual)
12399 and then not Is_Volatile (Orig_Ftyp)
12402 ("cannot instantiate nonvolatile formal & of mode in out",
12404 Error_Msg_N ("\with volatile object actual (RM C.6(12))", Actual);
12406 elsif Is_Volatile_Full_Access_Object_Ref (Actual)
12407 and then not Is_Volatile_Full_Access (Orig_Ftyp)
12410 ("cannot instantiate nonfull access formal & of mode in out",
12413 ("\with full access object actual (RM C.6(12))", Actual);
12416 -- Check for instantiation on nonatomic subcomponent of a full access
12417 -- object in Ada 2022 (RM C.6 (12)).
12419 if Ada_Version >= Ada_2022
12420 and then Is_Subcomponent_Of_Full_Access_Object (Actual)
12421 and then not Is_Atomic_Object (Actual)
12424 ("cannot instantiate formal & of mode in out with actual",
12427 ("\nonatomic subcomponent of full access object (RM C.6(12))",
12431 -- The actual for a ghost generic formal IN OUT parameter should be a
12432 -- ghost object (SPARK RM 6.9(14)).
12434 Check_Ghost_Formal_Variable
12436 Formal => A_Gen_Obj);
12438 -- Formal in-mode parameter
12441 -- The instantiation of a generic formal in-mode parameter is a
12442 -- constant declaration. The actual is the expression for that
12443 -- declaration. Its type is a full copy of the type of the
12444 -- formal. This may be an access to subprogram, for which we need
12445 -- to generate entities for the formals in the new signature.
12447 if Present (Actual) then
12448 if Present (Subt_Mark) then
12449 Def := New_Copy_Tree (Subt_Mark);
12451 pragma Assert (Present (Acc_Def));
12452 Def := New_Copy_Tree (Acc_Def);
12456 Make_Object_Declaration (Loc,
12457 Defining_Identifier => New_Copy (Gen_Obj),
12458 Constant_Present => True,
12459 Null_Exclusion_Present => Null_Exclusion_Present (Formal),
12460 Object_Definition => Def,
12461 Expression => Actual);
12463 Copy_Ghost_Aspect (Formal, To => Decl_Node);
12464 Set_Corresponding_Generic_Association (Decl_Node, Act_Assoc);
12466 -- A generic formal object of a tagged type is defined to be
12467 -- aliased so the new constant must also be treated as aliased.
12469 if Is_Tagged_Type (Etype (A_Gen_Obj)) then
12470 Set_Aliased_Present (Decl_Node);
12473 Append (Decl_Node, List);
12475 -- The actual for a ghost generic formal IN parameter of
12476 -- access-to-variable type should be a ghost object (SPARK
12479 if Is_Access_Variable (Etype (A_Gen_Obj)) then
12480 Check_Ghost_Formal_Variable
12482 Formal => A_Gen_Obj);
12485 -- No need to repeat (pre-)analysis of some expression nodes
12486 -- already handled in Preanalyze_Actuals.
12488 if Nkind (Actual) /= N_Allocator then
12491 -- Return if the analysis of the actual reported some error
12493 if Etype (Actual) = Any_Type then
12499 Formal_Type : constant Entity_Id := Etype (A_Gen_Obj);
12503 Typ := Get_Instance_Of (Formal_Type);
12505 -- If the actual appears in the current or an enclosing scope,
12506 -- use its type directly. This is relevant if it has an actual
12507 -- subtype that is distinct from its nominal one. This cannot
12508 -- be done in general because the type of the actual may
12509 -- depend on other actuals, and only be fully determined when
12510 -- the enclosing instance is analyzed.
12512 if Present (Etype (Actual))
12513 and then Is_Constr_Subt_For_U_Nominal (Etype (Actual))
12515 Freeze_Before (Instantiation_Node, Etype (Actual));
12517 Freeze_Before (Instantiation_Node, Typ);
12520 -- If the actual is an aggregate, perform name resolution on
12521 -- its components (the analysis of an aggregate does not do it)
12522 -- to capture local names that may be hidden if the generic is
12525 if Nkind (Actual) = N_Aggregate then
12526 Preanalyze_And_Resolve (Actual, Typ);
12529 if Is_Limited_Type (Typ)
12530 and then not OK_For_Limited_Init (Typ, Actual)
12533 ("initialization not allowed for limited types", Actual);
12534 Explain_Limited_Type (Typ, Actual);
12538 elsif Present (Default_Expression (Formal)) then
12540 -- Use default to construct declaration
12542 if Present (Subt_Mark) then
12543 Def := New_Copy_Tree (Subt_Mark);
12545 pragma Assert (Present (Acc_Def));
12546 Def := New_Copy_Tree (Acc_Def);
12550 Make_Object_Declaration (Sloc (Formal),
12551 Defining_Identifier => New_Copy (Gen_Obj),
12552 Constant_Present => True,
12553 Null_Exclusion_Present => Null_Exclusion_Present (Formal),
12554 Object_Definition => Def,
12555 Expression => New_Copy_Tree
12556 (Default_Expression (Formal)));
12558 Copy_Ghost_Aspect (Formal, To => Decl_Node);
12559 Set_Corresponding_Generic_Association
12560 (Decl_Node, Expression (Decl_Node));
12562 Append (Decl_Node, List);
12563 Set_Analyzed (Expression (Decl_Node), False);
12566 pragma Assert (False);
12570 if Nkind (Actual) in N_Has_Entity
12571 and then Present (Entity (Actual))
12573 Actual_Decl := Parent (Entity (Actual));
12576 -- Ada 2005 (AI-423) refined by AI12-0287:
12577 -- For an object_renaming_declaration with a null_exclusion or an
12578 -- access_definition that has a null_exclusion, the subtype of the
12579 -- object_name shall exclude null. In addition, if the
12580 -- object_renaming_declaration occurs within the body of a generic unit
12581 -- G or within the body of a generic unit declared within the
12582 -- declarative region of generic unit G, then:
12583 -- * if the object_name statically denotes a generic formal object of
12584 -- mode in out of G, then the declaration of that object shall have a
12586 -- * if the object_name statically denotes a call of a generic formal
12587 -- function of G, then the declaration of the result of that function
12588 -- shall have a null_exclusion.
12590 if Ada_Version >= Ada_2005
12591 and then Present (Actual_Decl)
12592 and then Nkind (Actual_Decl) in N_Formal_Object_Declaration
12593 | N_Object_Declaration
12594 and then Nkind (Analyzed_Formal) = N_Formal_Object_Declaration
12595 and then not Has_Null_Exclusion (Actual_Decl)
12596 and then Has_Null_Exclusion (Analyzed_Formal)
12597 and then Ekind (Defining_Identifier (Analyzed_Formal))
12598 = E_Generic_In_Out_Parameter
12599 and then ((In_Generic_Scope (Entity (Actual))
12600 and then In_Package_Body (Scope (Entity (Actual))))
12601 or else not Can_Never_Be_Null (Etype (Actual)))
12603 Error_Msg_Sloc := Sloc (Analyzed_Formal);
12605 ("actual must exclude null to match generic formal#", Actual);
12609 end Instantiate_Object;
12611 ------------------------------
12612 -- Instantiate_Package_Body --
12613 ------------------------------
12615 -- WARNING: This routine manages Ghost and SPARK regions. Return statements
12616 -- must be replaced by gotos which jump to the end of the routine in order
12617 -- to restore the Ghost and SPARK modes.
12619 procedure Instantiate_Package_Body
12620 (Body_Info : Pending_Body_Info;
12621 Inlined_Body : Boolean := False;
12622 Body_Optional : Boolean := False)
12624 Act_Decl : constant Node_Id := Body_Info.Act_Decl;
12625 Act_Decl_Id : constant Entity_Id := Defining_Entity (Act_Decl);
12626 Act_Spec : constant Node_Id := Specification (Act_Decl);
12627 Ctx_Parents : Elist_Id := No_Elist;
12628 Ctx_Top : Int := 0;
12629 Inst_Node : constant Node_Id := Body_Info.Inst_Node;
12630 Gen_Id : constant Node_Id := Name (Inst_Node);
12631 Gen_Unit : constant Entity_Id := Get_Generic_Entity (Inst_Node);
12632 Gen_Decl : constant Node_Id := Unit_Declaration_Node (Gen_Unit);
12633 Loc : constant Source_Ptr := Sloc (Inst_Node);
12635 procedure Check_Initialized_Types;
12636 -- In a generic package body, an entity of a generic private type may
12637 -- appear uninitialized. This is suspicious, unless the actual is a
12638 -- fully initialized type.
12640 procedure Install_Parents_Of_Generic_Context
12641 (Inst_Scope : Entity_Id;
12642 Ctx_Parents : out Elist_Id);
12643 -- Inst_Scope is the scope where the instance appears within; when it
12644 -- appears within a generic child package G, this routine collects and
12645 -- installs the enclosing packages of G in the scopes stack; installed
12646 -- packages are returned in Ctx_Parents.
12648 procedure Remove_Parents_Of_Generic_Context (Ctx_Parents : Elist_Id);
12649 -- Reverse effect after instantiation is complete
12651 -----------------------------
12652 -- Check_Initialized_Types --
12653 -----------------------------
12655 procedure Check_Initialized_Types is
12657 Formal : Entity_Id;
12658 Actual : Entity_Id;
12659 Uninit_Var : Entity_Id;
12662 Decl := First (Generic_Formal_Declarations (Gen_Decl));
12663 while Present (Decl) loop
12664 Uninit_Var := Empty;
12666 if Nkind (Decl) = N_Private_Extension_Declaration then
12667 Uninit_Var := Uninitialized_Variable (Decl);
12669 elsif Nkind (Decl) = N_Formal_Type_Declaration
12670 and then Nkind (Formal_Type_Definition (Decl)) =
12671 N_Formal_Private_Type_Definition
12674 Uninitialized_Variable (Formal_Type_Definition (Decl));
12677 if Present (Uninit_Var) then
12678 Formal := Defining_Identifier (Decl);
12679 Actual := First_Entity (Act_Decl_Id);
12681 -- For each formal there is a subtype declaration that renames
12682 -- the actual and has the same name as the formal. Locate the
12683 -- formal for warning message about uninitialized variables
12684 -- in the generic, for which the actual type should be a fully
12685 -- initialized type.
12687 while Present (Actual) loop
12688 exit when Ekind (Actual) = E_Package
12689 and then Present (Renamed_Entity (Actual));
12691 if Chars (Actual) = Chars (Formal)
12692 and then not Is_Scalar_Type (Actual)
12693 and then not Is_Fully_Initialized_Type (Actual)
12694 and then Warn_On_No_Value_Assigned
12696 Error_Msg_Node_2 := Formal;
12698 ("generic unit has uninitialized variable& of "
12699 & "formal private type &?v?", Actual, Uninit_Var);
12701 ("actual type for& should be fully initialized type?v?",
12706 Next_Entity (Actual);
12712 end Check_Initialized_Types;
12714 ----------------------------------------
12715 -- Install_Parents_Of_Generic_Context --
12716 ----------------------------------------
12718 procedure Install_Parents_Of_Generic_Context
12719 (Inst_Scope : Entity_Id;
12720 Ctx_Parents : out Elist_Id)
12726 Ctx_Parents := New_Elmt_List;
12728 -- Collect context parents (ie. parents where the instantiation
12729 -- appears within).
12732 while S /= Standard_Standard loop
12733 Prepend_Elmt (S, Ctx_Parents);
12737 -- Install enclosing parents
12739 Elmt := First_Elmt (Ctx_Parents);
12740 while Present (Elmt) loop
12741 Push_Scope (Node (Elmt));
12742 Set_Is_Immediately_Visible (Node (Elmt));
12745 end Install_Parents_Of_Generic_Context;
12747 ---------------------------------------
12748 -- Remove_Parents_Of_Generic_Context --
12749 ---------------------------------------
12751 procedure Remove_Parents_Of_Generic_Context (Ctx_Parents : Elist_Id) is
12755 -- Traverse Ctx_Parents in LIFO order to check the removed scopes
12757 Elmt := Last_Elmt (Ctx_Parents);
12758 while Present (Elmt) loop
12759 pragma Assert (Current_Scope = Node (Elmt));
12760 Set_Is_Immediately_Visible (Current_Scope, False);
12763 Remove_Last_Elmt (Ctx_Parents);
12764 Elmt := Last_Elmt (Ctx_Parents);
12766 end Remove_Parents_Of_Generic_Context;
12770 -- The following constants capture the context prior to instantiating
12771 -- the package body.
12773 Saved_CS : constant Config_Switches_Type := Save_Config_Switches;
12774 Saved_GM : constant Ghost_Mode_Type := Ghost_Mode;
12775 Saved_IGR : constant Node_Id := Ignored_Ghost_Region;
12776 Saved_ISMP : constant Boolean :=
12777 Ignore_SPARK_Mode_Pragmas_In_Instance;
12778 Saved_LSST : constant Suppress_Stack_Entry_Ptr :=
12779 Local_Suppress_Stack_Top;
12780 Saved_SC : constant Boolean := Style_Check;
12781 Saved_SM : constant SPARK_Mode_Type := SPARK_Mode;
12782 Saved_SMP : constant Node_Id := SPARK_Mode_Pragma;
12783 Saved_SS : constant Suppress_Record := Scope_Suppress;
12784 Saved_Warn : constant Warnings_State := Save_Warnings;
12786 Act_Body : Node_Id;
12787 Act_Body_Id : Entity_Id;
12788 Act_Body_Name : Node_Id;
12789 Gen_Body : Node_Id;
12790 Gen_Body_Id : Node_Id;
12791 Par_Ent : Entity_Id := Empty;
12792 Par_Installed : Boolean := False;
12793 Par_Vis : Boolean := False;
12795 Scope_Check_Id : Entity_Id;
12796 Scope_Check_Last : Nat;
12797 -- Value of Current_Scope before calls to Install_Parents; used to check
12798 -- that scopes are correctly removed after instantiation.
12800 Vis_Prims_List : Elist_Id := No_Elist;
12801 -- List of primitives made temporarily visible in the instantiation
12802 -- to match the visibility of the formal type.
12804 -- Start of processing for Instantiate_Package_Body
12807 Gen_Body_Id := Corresponding_Body (Gen_Decl);
12809 -- The instance body may already have been processed, as the parent of
12810 -- another instance that is inlined (Load_Parent_Of_Generic).
12812 if Present (Corresponding_Body (Instance_Spec (Inst_Node))) then
12816 Expander_Mode_Save_And_Set (Body_Info.Expander_Status);
12818 -- Re-establish the state of information on which checks are suppressed.
12819 -- This information was set in Body_Info at the point of instantiation,
12820 -- and now we restore it so that the instance is compiled using the
12821 -- check status at the instantiation (RM 11.5(7.2/2), AI95-00224-01).
12823 Local_Suppress_Stack_Top := Body_Info.Local_Suppress_Stack_Top;
12824 Scope_Suppress := Body_Info.Scope_Suppress;
12826 Restore_Config_Switches (Body_Info.Config_Switches);
12827 Restore_Warnings (Body_Info.Warnings);
12829 if No (Gen_Body_Id) then
12831 -- Do not look for parent of generic body if none is required.
12832 -- This may happen when the routine is called as part of the
12833 -- Pending_Instantiations processing, when nested instances
12834 -- may precede the one generated from the main unit.
12836 if not Unit_Requires_Body (Defining_Entity (Gen_Decl))
12837 and then Body_Optional
12841 Load_Parent_Of_Generic
12842 (Inst_Node, Specification (Gen_Decl), Body_Optional);
12844 -- Surprisingly enough, loading the body of the parent can cause
12845 -- the body to be instantiated and the double instantiation needs
12846 -- to be prevented in order to avoid giving bogus semantic errors.
12848 -- This case can occur because of the Collect_Previous_Instances
12849 -- machinery of Load_Parent_Of_Generic, which will instantiate
12850 -- bodies that are deemed to be ahead of the body of the parent
12851 -- in the compilation unit. But the relative position of these
12852 -- bodies is computed using the mere comparison of their Sloc.
12854 -- Now suppose that you have two generic packages G and H, with
12855 -- G containing a mere instantiation of H:
12861 -- package Nested_G is
12872 -- package My_H is new H;
12876 -- and a third package Q instantiating G and Nested_G:
12882 -- package My_G is new G;
12884 -- package My_Nested_G is new My_G.My_H.Nested_G;
12888 -- The body to be instantiated is that of My_Nested_G and its
12889 -- parent is the instance My_G.My_H. This latter instantiation
12890 -- is done when My_G is analyzed, i.e. after the declarations
12891 -- of My_G and My_Nested_G have been parsed; as a result, the
12892 -- Sloc of My_G.My_H is greater than the Sloc of My_Nested_G.
12894 -- Therefore loading the body of My_G.My_H will cause the body
12895 -- of My_Nested_G to be instantiated because it is deemed to be
12896 -- ahead of My_G.My_H. This means that Load_Parent_Of_Generic
12897 -- will again be invoked on My_G.My_H, but this time with the
12898 -- Collect_Previous_Instances machinery disabled, so there is
12899 -- no endless mutual recursion and things are done in order.
12901 if Present (Corresponding_Body (Instance_Spec (Inst_Node))) then
12905 Gen_Body_Id := Corresponding_Body (Gen_Decl);
12909 -- Establish global variable for sloc adjustment and for error recovery
12910 -- In the case of an instance body for an instantiation with actuals
12911 -- from a limited view, the instance body is placed at the beginning
12912 -- of the enclosing package body: use the body entity as the source
12913 -- location for nodes of the instance body.
12915 if not Is_Empty_Elmt_List (Incomplete_Actuals (Act_Decl_Id)) then
12917 Scop : constant Entity_Id := Scope (Act_Decl_Id);
12918 Body_Id : constant Node_Id :=
12919 Corresponding_Body (Unit_Declaration_Node (Scop));
12922 Instantiation_Node := Body_Id;
12925 Instantiation_Node := Inst_Node;
12928 -- The package being instantiated may be subject to pragma Ghost. Set
12929 -- the mode now to ensure that any nodes generated during instantiation
12930 -- are properly marked as Ghost.
12932 Set_Ghost_Mode (Act_Decl_Id);
12934 if Present (Gen_Body_Id) then
12935 Save_Env (Gen_Unit, Act_Decl_Id);
12936 Style_Check := False;
12938 -- If the context of the instance is subject to SPARK_Mode "off", the
12939 -- annotation is missing, or the body is instantiated at a later pass
12940 -- and its spec ignored SPARK_Mode pragma, set the global flag which
12941 -- signals Analyze_Pragma to ignore all SPARK_Mode pragmas within the
12944 if SPARK_Mode /= On
12945 or else Ignore_SPARK_Mode_Pragmas (Act_Decl_Id)
12947 Ignore_SPARK_Mode_Pragmas_In_Instance := True;
12950 Current_Sem_Unit := Body_Info.Current_Sem_Unit;
12951 Gen_Body := Unit_Declaration_Node (Gen_Body_Id);
12953 Create_Instantiation_Source
12954 (Inst_Node, Gen_Body_Id, S_Adjustment);
12958 (Original_Node (Gen_Body), Empty, Instantiating => True);
12960 -- Create proper (possibly qualified) defining name for the body, to
12961 -- correspond to the one in the spec.
12964 Make_Defining_Identifier (Sloc (Act_Decl_Id), Chars (Act_Decl_Id));
12965 Preserve_Comes_From_Source (Act_Body_Id, Act_Decl_Id);
12967 -- Some attributes of spec entity are not inherited by body entity
12969 Set_Handler_Records (Act_Body_Id, No_List);
12971 if Nkind (Defining_Unit_Name (Act_Spec)) =
12972 N_Defining_Program_Unit_Name
12975 Make_Defining_Program_Unit_Name (Loc,
12977 New_Copy_Tree (Name (Defining_Unit_Name (Act_Spec))),
12978 Defining_Identifier => Act_Body_Id);
12980 Act_Body_Name := Act_Body_Id;
12983 Set_Defining_Unit_Name (Act_Body, Act_Body_Name);
12985 Set_Corresponding_Spec (Act_Body, Act_Decl_Id);
12986 Check_Generic_Actuals (Act_Decl_Id, False);
12987 Check_Initialized_Types;
12989 -- Install primitives hidden at the point of the instantiation but
12990 -- visible when processing the generic formals
12996 E := First_Entity (Act_Decl_Id);
12997 while Present (E) loop
12999 and then not Is_Itype (E)
13000 and then Is_Generic_Actual_Type (E)
13001 and then Is_Tagged_Type (E)
13003 Install_Hidden_Primitives
13004 (Prims_List => Vis_Prims_List,
13005 Gen_T => Generic_Parent_Type (Parent (E)),
13013 Scope_Check_Id := Current_Scope;
13014 Scope_Check_Last := Scope_Stack.Last;
13016 -- If the instantiation appears within a generic child some actual
13017 -- parameter may be the current instance of the enclosing generic
13021 Inst_Scope : constant Entity_Id := Scope (Act_Decl_Id);
13024 if Is_Child_Unit (Inst_Scope)
13025 and then Ekind (Inst_Scope) = E_Generic_Package
13026 and then Present (Generic_Associations (Inst_Node))
13028 Install_Parents_Of_Generic_Context (Inst_Scope, Ctx_Parents);
13030 -- Hide them from visibility; required to avoid conflicts
13031 -- installing the parent instance.
13033 if Present (Ctx_Parents) then
13034 Push_Scope (Standard_Standard);
13035 Ctx_Top := Scope_Stack.Last;
13036 Scope_Stack.Table (Ctx_Top).Is_Active_Stack_Base := True;
13041 -- If it is a child unit, make the parent instance (which is an
13042 -- instance of the parent of the generic) visible.
13044 -- 1) The child unit's parent is an explicit parent instance (the
13045 -- prefix of the name of the generic unit):
13047 -- package Child_Package is new Parent_Instance.Child_Unit;
13049 -- 2) The child unit's parent is an implicit parent instance (e.g.
13050 -- when instantiating a sibling package):
13053 -- package Parent.Second_Child is
13057 -- package Parent.First_Child is
13058 -- package Sibling_Package is new Second_Child;
13060 -- 3) The child unit's parent is not an instance, so the scope is
13061 -- simply the one of the unit.
13063 if Ekind (Scope (Gen_Unit)) = E_Generic_Package
13064 and then Nkind (Gen_Id) = N_Expanded_Name
13066 Par_Ent := Entity (Prefix (Gen_Id));
13068 elsif Ekind (Scope (Gen_Unit)) = E_Generic_Package
13069 and then Ekind (Scope (Act_Decl_Id)) = E_Package
13070 and then Is_Generic_Instance (Scope (Act_Decl_Id))
13072 (Name (Get_Unit_Instantiation_Node
13073 (Scope (Act_Decl_Id)))) = N_Expanded_Name
13076 (Prefix (Name (Get_Unit_Instantiation_Node
13077 (Scope (Act_Decl_Id)))));
13079 elsif Is_Child_Unit (Gen_Unit) then
13080 Par_Ent := Scope (Gen_Unit);
13083 if Present (Par_Ent) then
13084 Par_Vis := Is_Immediately_Visible (Par_Ent);
13085 Install_Parent (Par_Ent, In_Body => True);
13086 Par_Installed := True;
13089 -- If the instantiation is a library unit, and this is the main unit,
13090 -- then build the resulting compilation unit nodes for the instance.
13091 -- If this is a compilation unit but it is not the main unit, then it
13092 -- is the body of a unit in the context, that is being compiled
13093 -- because it is encloses some inlined unit or another generic unit
13094 -- being instantiated. In that case, this body is not part of the
13095 -- current compilation, and is not attached to the tree, but its
13096 -- parent must be set for analysis.
13098 if Nkind (Parent (Inst_Node)) = N_Compilation_Unit then
13100 -- Replace instance node with body of instance, and create new
13101 -- node for corresponding instance declaration.
13103 Build_Instance_Compilation_Unit_Nodes
13104 (Inst_Node, Act_Body, Act_Decl);
13106 -- If the instantiation appears within a generic child package
13107 -- enable visibility of current instance of enclosing generic
13110 if Present (Ctx_Parents) then
13111 Scope_Stack.Table (Ctx_Top).Is_Active_Stack_Base := False;
13112 Analyze (Inst_Node);
13113 Scope_Stack.Table (Ctx_Top).Is_Active_Stack_Base := True;
13115 Analyze (Inst_Node);
13118 if Parent (Inst_Node) = Cunit (Main_Unit) then
13120 -- If the instance is a child unit itself, then set the scope
13121 -- of the expanded body to be the parent of the instantiation
13122 -- (ensuring that the fully qualified name will be generated
13123 -- for the elaboration subprogram).
13125 if Nkind (Defining_Unit_Name (Act_Spec)) =
13126 N_Defining_Program_Unit_Name
13128 Set_Scope (Defining_Entity (Inst_Node), Scope (Act_Decl_Id));
13132 -- Case where instantiation is not a library unit
13135 -- Handle the case of an instance with incomplete actual types.
13136 -- The instance body cannot be placed just after the declaration
13137 -- because full views have not been seen yet. Any use of the non-
13138 -- limited views in the instance body requires the presence of a
13139 -- regular with_clause in the enclosing unit. Therefore we place
13140 -- the instance body at the beginning of the enclosing body, and
13141 -- the freeze node for the instance is then placed after the body.
13143 if not Is_Empty_Elmt_List (Incomplete_Actuals (Act_Decl_Id))
13144 and then Ekind (Scope (Act_Decl_Id)) = E_Package
13147 Scop : constant Entity_Id := Scope (Act_Decl_Id);
13148 Body_Id : constant Node_Id :=
13149 Corresponding_Body (Unit_Declaration_Node (Scop));
13154 pragma Assert (Present (Body_Id));
13156 Prepend (Act_Body, Declarations (Parent (Body_Id)));
13158 if Expander_Active then
13159 Ensure_Freeze_Node (Act_Decl_Id);
13160 F_Node := Freeze_Node (Act_Decl_Id);
13161 Set_Is_Frozen (Act_Decl_Id, False);
13162 if Is_List_Member (F_Node) then
13166 Insert_After (Act_Body, F_Node);
13171 Insert_Before (Inst_Node, Act_Body);
13172 Mark_Rewrite_Insertion (Act_Body);
13174 -- Insert the freeze node for the instance if need be
13176 if Expander_Active then
13177 Freeze_Package_Instance
13178 (Inst_Node, Gen_Body, Gen_Decl, Act_Decl_Id);
13179 Set_Is_Frozen (Act_Decl_Id);
13183 -- If the instantiation appears within a generic child package
13184 -- enable visibility of current instance of enclosing generic
13187 if Present (Ctx_Parents) then
13188 Scope_Stack.Table (Ctx_Top).Is_Active_Stack_Base := False;
13189 Analyze (Act_Body);
13190 Scope_Stack.Table (Ctx_Top).Is_Active_Stack_Base := True;
13192 Analyze (Act_Body);
13196 Inherit_Context (Gen_Body, Inst_Node);
13198 if Par_Installed then
13199 Remove_Parent (In_Body => True);
13201 -- Restore the previous visibility of the parent
13203 Set_Is_Immediately_Visible (Par_Ent, Par_Vis);
13206 -- Remove the parent instances if they have been placed on the scope
13207 -- stack to compile the body.
13209 if Present (Ctx_Parents) then
13210 pragma Assert (Scope_Stack.Last = Ctx_Top
13211 and then Current_Scope = Standard_Standard);
13214 Remove_Parents_Of_Generic_Context (Ctx_Parents);
13217 pragma Assert (Current_Scope = Scope_Check_Id);
13218 pragma Assert (Scope_Stack.Last = Scope_Check_Last);
13220 Restore_Hidden_Primitives (Vis_Prims_List);
13222 -- Restore the private views that were made visible when the body of
13223 -- the instantiation was created. Note that, in the case where one of
13224 -- these private views is declared in the parent, there is a nesting
13225 -- issue with the calls to Install_Parent and Remove_Parent made in
13226 -- between above with In_Body set to True, because these calls also
13227 -- want to swap and restore this private view respectively. In this
13228 -- case, the call to Install_Parent does nothing, but the call to
13229 -- Remove_Parent does restore the private view, thus undercutting the
13230 -- call to Restore_Private_Views. That's OK under the condition that
13231 -- the two mechanisms swap exactly the same entities, in particular
13232 -- the private entities dependent on the primary private entities.
13234 Restore_Private_Views (Act_Decl_Id);
13236 -- Remove the current unit from visibility if this is an instance
13237 -- that is not elaborated on the fly for inlining purposes.
13239 if not Inlined_Body then
13240 Set_Is_Immediately_Visible (Act_Decl_Id, False);
13245 -- If we have no body, and the unit requires a body, then complain. This
13246 -- complaint is suppressed if we have detected other errors (since a
13247 -- common reason for missing the body is that it had errors).
13248 -- In CodePeer mode, a warning has been emitted already, no need for
13249 -- further messages.
13251 elsif Unit_Requires_Body (Gen_Unit)
13252 and then not Body_Optional
13254 if CodePeer_Mode then
13257 elsif Serious_Errors_Detected = 0 then
13259 ("cannot find body of generic package &", Inst_Node, Gen_Unit);
13261 -- Don't attempt to perform any cleanup actions if some other error
13262 -- was already detected, since this can cause blowups.
13268 -- Case of package that does not need a body
13271 -- If the instantiation of the declaration is a library unit, rewrite
13272 -- the original package instantiation as a package declaration in the
13273 -- compilation unit node.
13275 if Nkind (Parent (Inst_Node)) = N_Compilation_Unit then
13276 Set_Parent_Spec (Act_Decl, Parent_Spec (Inst_Node));
13277 Rewrite (Inst_Node, Act_Decl);
13279 -- Generate elaboration entity, in case spec has elaboration code.
13280 -- This cannot be done when the instance is analyzed, because it
13281 -- is not known yet whether the body exists.
13283 Set_Elaboration_Entity_Required (Act_Decl_Id, False);
13284 Build_Elaboration_Entity (Parent (Inst_Node), Act_Decl_Id);
13286 -- If the instantiation is not a library unit, then append the
13287 -- declaration to the list of implicitly generated entities, unless
13288 -- it is already a list member which means that it was already
13291 elsif not Is_List_Member (Act_Decl) then
13292 Mark_Rewrite_Insertion (Act_Decl);
13293 Insert_Before (Inst_Node, Act_Decl);
13299 -- Restore the context that was in effect prior to instantiating the
13302 Ignore_SPARK_Mode_Pragmas_In_Instance := Saved_ISMP;
13303 Local_Suppress_Stack_Top := Saved_LSST;
13304 Scope_Suppress := Saved_SS;
13305 Style_Check := Saved_SC;
13307 Expander_Mode_Restore;
13308 Restore_Config_Switches (Saved_CS);
13309 Restore_Ghost_Region (Saved_GM, Saved_IGR);
13310 Restore_SPARK_Mode (Saved_SM, Saved_SMP);
13311 Restore_Warnings (Saved_Warn);
13312 end Instantiate_Package_Body;
13314 ---------------------------------
13315 -- Instantiate_Subprogram_Body --
13316 ---------------------------------
13318 -- WARNING: This routine manages Ghost and SPARK regions. Return statements
13319 -- must be replaced by gotos which jump to the end of the routine in order
13320 -- to restore the Ghost and SPARK modes.
13322 procedure Instantiate_Subprogram_Body
13323 (Body_Info : Pending_Body_Info;
13324 Body_Optional : Boolean := False)
13326 Act_Decl : constant Node_Id := Body_Info.Act_Decl;
13327 Act_Decl_Id : constant Entity_Id := Defining_Entity (Act_Decl);
13328 Inst_Node : constant Node_Id := Body_Info.Inst_Node;
13329 Gen_Id : constant Node_Id := Name (Inst_Node);
13330 Gen_Unit : constant Entity_Id := Get_Generic_Entity (Inst_Node);
13331 Gen_Decl : constant Node_Id := Unit_Declaration_Node (Gen_Unit);
13332 Loc : constant Source_Ptr := Sloc (Inst_Node);
13333 Pack_Id : constant Entity_Id :=
13334 Defining_Unit_Name (Parent (Act_Decl));
13336 -- The following constants capture the context prior to instantiating
13337 -- the subprogram body.
13339 Saved_CS : constant Config_Switches_Type := Save_Config_Switches;
13340 Saved_GM : constant Ghost_Mode_Type := Ghost_Mode;
13341 Saved_IGR : constant Node_Id := Ignored_Ghost_Region;
13342 Saved_ISMP : constant Boolean :=
13343 Ignore_SPARK_Mode_Pragmas_In_Instance;
13344 Saved_LSST : constant Suppress_Stack_Entry_Ptr :=
13345 Local_Suppress_Stack_Top;
13346 Saved_SC : constant Boolean := Style_Check;
13347 Saved_SM : constant SPARK_Mode_Type := SPARK_Mode;
13348 Saved_SMP : constant Node_Id := SPARK_Mode_Pragma;
13349 Saved_SS : constant Suppress_Record := Scope_Suppress;
13350 Saved_Warn : constant Warnings_State := Save_Warnings;
13352 Act_Body : Node_Id;
13353 Act_Body_Id : Entity_Id;
13354 Gen_Body : Node_Id;
13355 Gen_Body_Id : Node_Id;
13356 Pack_Body : Node_Id;
13357 Par_Ent : Entity_Id := Empty;
13358 Par_Installed : Boolean := False;
13359 Par_Vis : Boolean := False;
13360 Ret_Expr : Node_Id;
13363 Gen_Body_Id := Corresponding_Body (Gen_Decl);
13365 -- Subprogram body may have been created already because of an inline
13366 -- pragma, or because of multiple elaborations of the enclosing package
13367 -- when several instances of the subprogram appear in the main unit.
13369 if Present (Corresponding_Body (Act_Decl)) then
13373 Expander_Mode_Save_And_Set (Body_Info.Expander_Status);
13375 -- Re-establish the state of information on which checks are suppressed.
13376 -- This information was set in Body_Info at the point of instantiation,
13377 -- and now we restore it so that the instance is compiled using the
13378 -- check status at the instantiation (RM 11.5(7.2/2), AI95-00224-01).
13380 Local_Suppress_Stack_Top := Body_Info.Local_Suppress_Stack_Top;
13381 Scope_Suppress := Body_Info.Scope_Suppress;
13383 Restore_Config_Switches (Body_Info.Config_Switches);
13384 Restore_Warnings (Body_Info.Warnings);
13386 if No (Gen_Body_Id) then
13388 -- For imported generic subprogram, no body to compile, complete
13389 -- the spec entity appropriately.
13391 if Is_Imported (Gen_Unit) then
13392 Set_Is_Imported (Act_Decl_Id);
13393 Set_First_Rep_Item (Act_Decl_Id, First_Rep_Item (Gen_Unit));
13394 Set_Interface_Name (Act_Decl_Id, Interface_Name (Gen_Unit));
13395 Set_Convention (Act_Decl_Id, Convention (Gen_Unit));
13396 Set_Has_Completion (Act_Decl_Id);
13399 -- For other cases, compile the body
13402 Load_Parent_Of_Generic
13403 (Inst_Node, Specification (Gen_Decl), Body_Optional);
13404 Gen_Body_Id := Corresponding_Body (Gen_Decl);
13408 Instantiation_Node := Inst_Node;
13410 -- The subprogram being instantiated may be subject to pragma Ghost. Set
13411 -- the mode now to ensure that any nodes generated during instantiation
13412 -- are properly marked as Ghost.
13414 Set_Ghost_Mode (Act_Decl_Id);
13416 if Present (Gen_Body_Id) then
13417 Gen_Body := Unit_Declaration_Node (Gen_Body_Id);
13419 if Nkind (Gen_Body) = N_Subprogram_Body_Stub then
13421 -- Either body is not present, or context is non-expanding, as
13422 -- when compiling a subunit. Mark the instance as completed, and
13423 -- diagnose a missing body when needed.
13426 and then Operating_Mode = Generate_Code
13428 Error_Msg_N ("missing proper body for instantiation", Gen_Body);
13431 Set_Has_Completion (Act_Decl_Id);
13435 Save_Env (Gen_Unit, Act_Decl_Id);
13436 Style_Check := False;
13438 -- If the context of the instance is subject to SPARK_Mode "off", the
13439 -- annotation is missing, or the body is instantiated at a later pass
13440 -- and its spec ignored SPARK_Mode pragma, set the global flag which
13441 -- signals Analyze_Pragma to ignore all SPARK_Mode pragmas within the
13444 if SPARK_Mode /= On
13445 or else Ignore_SPARK_Mode_Pragmas (Act_Decl_Id)
13447 Ignore_SPARK_Mode_Pragmas_In_Instance := True;
13450 -- If the context of an instance is not subject to SPARK_Mode "off",
13451 -- and the generic body is subject to an explicit SPARK_Mode pragma,
13452 -- the latter should be the one applicable to the instance.
13454 if not Ignore_SPARK_Mode_Pragmas_In_Instance
13455 and then SPARK_Mode /= Off
13456 and then Present (SPARK_Pragma (Gen_Body_Id))
13458 Set_SPARK_Mode (Gen_Body_Id);
13461 Current_Sem_Unit := Body_Info.Current_Sem_Unit;
13462 Create_Instantiation_Source
13469 (Original_Node (Gen_Body), Empty, Instantiating => True);
13471 -- Create proper defining name for the body, to correspond to the one
13475 Make_Defining_Identifier (Sloc (Act_Decl_Id), Chars (Act_Decl_Id));
13477 Preserve_Comes_From_Source (Act_Body_Id, Act_Decl_Id);
13478 Set_Defining_Unit_Name (Specification (Act_Body), Act_Body_Id);
13480 Set_Corresponding_Spec (Act_Body, Act_Decl_Id);
13481 Set_Has_Completion (Act_Decl_Id);
13482 Check_Generic_Actuals (Pack_Id, False);
13484 -- Generate a reference to link the visible subprogram instance to
13485 -- the generic body, which for navigation purposes is the only
13486 -- available source for the instance.
13489 (Related_Instance (Pack_Id),
13490 Gen_Body_Id, 'b', Set_Ref => False, Force => True);
13492 -- If it is a child unit, make the parent instance (which is an
13493 -- instance of the parent of the generic) visible. The parent
13494 -- instance is the prefix of the name of the generic unit.
13496 if Ekind (Scope (Gen_Unit)) = E_Generic_Package
13497 and then Nkind (Gen_Id) = N_Expanded_Name
13499 Par_Ent := Entity (Prefix (Gen_Id));
13500 elsif Is_Child_Unit (Gen_Unit) then
13501 Par_Ent := Scope (Gen_Unit);
13504 if Present (Par_Ent) then
13505 Par_Vis := Is_Immediately_Visible (Par_Ent);
13506 Install_Parent (Par_Ent, In_Body => True);
13507 Par_Installed := True;
13510 -- Subprogram body is placed in the body of wrapper package,
13511 -- whose spec contains the subprogram declaration as well as
13512 -- the renaming declarations for the generic parameters.
13515 Make_Package_Body (Loc,
13516 Defining_Unit_Name => New_Copy (Pack_Id),
13517 Declarations => New_List (Act_Body));
13519 Set_Corresponding_Spec (Pack_Body, Pack_Id);
13521 -- If the instantiation is a library unit, then build resulting
13522 -- compilation unit nodes for the instance. The declaration of
13523 -- the enclosing package is the grandparent of the subprogram
13524 -- declaration. First replace the instantiation node as the unit
13525 -- of the corresponding compilation.
13527 if Nkind (Parent (Inst_Node)) = N_Compilation_Unit then
13528 if Parent (Inst_Node) = Cunit (Main_Unit) then
13529 Set_Unit (Parent (Inst_Node), Inst_Node);
13530 Build_Instance_Compilation_Unit_Nodes
13531 (Inst_Node, Pack_Body, Parent (Parent (Act_Decl)));
13532 Analyze (Inst_Node);
13534 Set_Parent (Pack_Body, Parent (Inst_Node));
13535 Analyze (Pack_Body);
13539 Insert_Before (Inst_Node, Pack_Body);
13540 Mark_Rewrite_Insertion (Pack_Body);
13542 -- Insert the freeze node for the instance if need be
13544 if Expander_Active then
13545 Freeze_Subprogram_Instance (Inst_Node, Gen_Body, Pack_Id);
13548 Analyze (Pack_Body);
13551 Inherit_Context (Gen_Body, Inst_Node);
13553 Restore_Private_Views (Pack_Id, False);
13555 if Par_Installed then
13556 Remove_Parent (In_Body => True);
13558 -- Restore the previous visibility of the parent
13560 Set_Is_Immediately_Visible (Par_Ent, Par_Vis);
13565 -- Body not found. Error was emitted already. If there were no previous
13566 -- errors, this may be an instance whose scope is a premature instance.
13567 -- In that case we must insure that the (legal) program does raise
13568 -- program error if executed. We generate a subprogram body for this
13571 elsif Serious_Errors_Detected = 0
13572 and then Nkind (Parent (Inst_Node)) /= N_Compilation_Unit
13574 if Body_Optional then
13577 elsif Ekind (Act_Decl_Id) = E_Procedure then
13579 Make_Subprogram_Body (Loc,
13581 Make_Procedure_Specification (Loc,
13582 Defining_Unit_Name =>
13583 Make_Defining_Identifier (Loc, Chars (Act_Decl_Id)),
13584 Parameter_Specifications =>
13586 (Parameter_Specifications (Parent (Act_Decl_Id)))),
13588 Declarations => Empty_List,
13589 Handled_Statement_Sequence =>
13590 Make_Handled_Sequence_Of_Statements (Loc,
13591 Statements => New_List (
13592 Make_Raise_Program_Error (Loc,
13593 Reason => PE_Access_Before_Elaboration))));
13597 Make_Raise_Program_Error (Loc,
13598 Reason => PE_Access_Before_Elaboration);
13600 Set_Etype (Ret_Expr, (Etype (Act_Decl_Id)));
13601 Set_Analyzed (Ret_Expr);
13604 Make_Subprogram_Body (Loc,
13606 Make_Function_Specification (Loc,
13607 Defining_Unit_Name =>
13608 Make_Defining_Identifier (Loc, Chars (Act_Decl_Id)),
13609 Parameter_Specifications =>
13611 (Parameter_Specifications (Parent (Act_Decl_Id))),
13612 Result_Definition =>
13613 New_Occurrence_Of (Etype (Act_Decl_Id), Loc)),
13615 Declarations => Empty_List,
13616 Handled_Statement_Sequence =>
13617 Make_Handled_Sequence_Of_Statements (Loc,
13618 Statements => New_List (
13619 Make_Simple_Return_Statement (Loc, Ret_Expr))));
13623 Make_Package_Body (Loc,
13624 Defining_Unit_Name => New_Copy (Pack_Id),
13625 Declarations => New_List (Act_Body));
13627 Insert_After (Inst_Node, Pack_Body);
13628 Set_Corresponding_Spec (Pack_Body, Pack_Id);
13629 Analyze (Pack_Body);
13634 -- Restore the context that was in effect prior to instantiating the
13635 -- subprogram body.
13637 Ignore_SPARK_Mode_Pragmas_In_Instance := Saved_ISMP;
13638 Local_Suppress_Stack_Top := Saved_LSST;
13639 Scope_Suppress := Saved_SS;
13640 Style_Check := Saved_SC;
13642 Expander_Mode_Restore;
13643 Restore_Config_Switches (Saved_CS);
13644 Restore_Ghost_Region (Saved_GM, Saved_IGR);
13645 Restore_SPARK_Mode (Saved_SM, Saved_SMP);
13646 Restore_Warnings (Saved_Warn);
13647 end Instantiate_Subprogram_Body;
13649 ----------------------
13650 -- Instantiate_Type --
13651 ----------------------
13653 function Instantiate_Type
13656 Analyzed_Formal : Node_Id;
13657 Actual_Decls : List_Id) return List_Id
13659 A_Gen_T : constant Entity_Id :=
13660 Defining_Identifier (Analyzed_Formal);
13661 Def : constant Node_Id := Formal_Type_Definition (Formal);
13662 Gen_T : constant Entity_Id := Defining_Identifier (Formal);
13664 Ancestor : Entity_Id := Empty;
13665 Decl_Node : Node_Id;
13666 Decl_Nodes : List_Id; -- result
13670 procedure Check_Shared_Variable_Control_Aspects;
13671 -- Ada 2022: Verify that shared variable control aspects (RM C.6)
13672 -- that may be specified for a formal type are obeyed by the actual.
13674 procedure Diagnose_Predicated_Actual;
13675 -- There are a number of constructs in which a discrete type with
13676 -- predicates is illegal, e.g. as an index in an array type declaration.
13677 -- If a generic type is used is such a construct in a generic package
13678 -- declaration, it carries the flag No_Predicate_On_Actual. It is part
13679 -- of the generic contract that the actual cannot have predicates.
13681 function Subtypes_Match (Gen_T, Act_T : Entity_Id) return Boolean;
13682 -- Check that base types are the same and that the subtypes match
13683 -- statically. Used in several of the validation subprograms for
13684 -- actuals in instantiations.
13686 procedure Validate_Array_Type_Instance;
13687 procedure Validate_Access_Subprogram_Instance;
13688 procedure Validate_Access_Type_Instance;
13689 procedure Validate_Derived_Type_Instance;
13690 procedure Validate_Derived_Interface_Type_Instance;
13691 procedure Validate_Discriminated_Formal_Type;
13692 procedure Validate_Interface_Type_Instance;
13693 procedure Validate_Private_Type_Instance;
13694 procedure Validate_Incomplete_Type_Instance;
13695 -- These procedures perform validation tests for the named case.
13696 -- Validate_Discriminated_Formal_Type is shared by formal private
13697 -- types and Ada 2012 formal incomplete types.
13699 --------------------------------------------
13700 -- Check_Shared_Variable_Control_Aspects --
13701 --------------------------------------------
13703 -- Ada 2022: Verify that shared variable control aspects (RM C.6)
13704 -- that may be specified for the formal are obeyed by the actual.
13705 -- If the formal is a derived type the aspect specifications must match.
13706 -- NOTE: AI12-0282 implies that matching of aspects is required between
13707 -- formal and actual in all cases, but this is too restrictive.
13708 -- In particular it violates a language design rule: a limited private
13709 -- indefinite formal can be matched by any actual. The current code
13710 -- reflects an older and more permissive version of RM C.6 (12/5).
13712 procedure Check_Shared_Variable_Control_Aspects is
13714 if Ada_Version >= Ada_2022 then
13715 if Is_Atomic (A_Gen_T) and then not Is_Atomic (Act_T) then
13717 ("actual for& must have Atomic aspect", Actual, A_Gen_T);
13719 elsif Is_Derived_Type (A_Gen_T)
13720 and then Is_Atomic (A_Gen_T) /= Is_Atomic (Act_T)
13723 ("actual for& has different Atomic aspect", Actual, A_Gen_T);
13726 if Is_Volatile (A_Gen_T) and then not Is_Volatile (Act_T) then
13728 ("actual for& must have Volatile aspect",
13731 elsif Is_Derived_Type (A_Gen_T)
13732 and then Is_Volatile (A_Gen_T) /= Is_Volatile (Act_T)
13735 ("actual for& has different Volatile aspect",
13739 -- We assume that an array type whose atomic component type
13740 -- is Atomic is equivalent to an array type with the explicit
13741 -- aspect Has_Atomic_Components. This is a reasonable inference
13742 -- from the intent of AI12-0282, and makes it legal to use an
13743 -- actual that does not have the identical aspect as the formal.
13744 -- Ditto for volatile components.
13747 Actual_Atomic_Comp : constant Boolean :=
13748 Has_Atomic_Components (Act_T)
13749 or else (Is_Array_Type (Act_T)
13750 and then Is_Atomic (Component_Type (Act_T)));
13752 if Has_Atomic_Components (A_Gen_T) /= Actual_Atomic_Comp then
13754 ("formal and actual for& must agree on atomic components",
13760 Actual_Volatile_Comp : constant Boolean :=
13761 Has_Volatile_Components (Act_T)
13762 or else (Is_Array_Type (Act_T)
13763 and then Is_Volatile (Component_Type (Act_T)));
13765 if Has_Volatile_Components (A_Gen_T) /= Actual_Volatile_Comp
13768 ("actual for& must have volatile components",
13773 -- The following two aspects do not require exact matching,
13774 -- but only one-way agreement. See RM C.6.
13776 if Is_Independent (A_Gen_T) and then not Is_Independent (Act_T)
13779 ("actual for& must have Independent aspect specified",
13783 if Has_Independent_Components (A_Gen_T)
13784 and then not Has_Independent_Components (Act_T)
13787 ("actual for& must have Independent_Components specified",
13791 end Check_Shared_Variable_Control_Aspects;
13793 ---------------------------------
13794 -- Diagnose_Predicated_Actual --
13795 ---------------------------------
13797 procedure Diagnose_Predicated_Actual is
13799 if No_Predicate_On_Actual (A_Gen_T)
13800 and then Has_Predicates (Act_T)
13803 ("actual for& cannot be a type with predicate",
13804 Instantiation_Node, A_Gen_T);
13806 elsif No_Dynamic_Predicate_On_Actual (A_Gen_T)
13807 and then Has_Predicates (Act_T)
13808 and then not Has_Static_Predicate_Aspect (Act_T)
13811 ("actual for& cannot be a type with a dynamic predicate",
13812 Instantiation_Node, A_Gen_T);
13814 end Diagnose_Predicated_Actual;
13816 --------------------
13817 -- Subtypes_Match --
13818 --------------------
13820 function Subtypes_Match (Gen_T, Act_T : Entity_Id) return Boolean is
13821 T : constant Entity_Id := Get_Instance_Of (Gen_T);
13824 -- Check that the base types, root types (when dealing with class
13825 -- wide types), or designated types (when dealing with anonymous
13826 -- access types) of Gen_T and Act_T are statically matching subtypes.
13828 return (Base_Type (Base_Type (T)) = Base_Type (Act_T)
13829 and then Subtypes_Statically_Match (T, Act_T))
13831 or else (Is_Class_Wide_Type (Gen_T)
13832 and then Is_Class_Wide_Type (Act_T)
13833 and then Subtypes_Match
13834 (Get_Instance_Of (Root_Type (Gen_T)),
13835 Root_Type (Act_T)))
13837 or else (Is_Anonymous_Access_Type (Gen_T)
13838 and then Ekind (Act_T) = Ekind (Gen_T)
13839 and then Subtypes_Statically_Match
13840 (Designated_Type (Gen_T), Designated_Type (Act_T)));
13841 end Subtypes_Match;
13843 -----------------------------------------
13844 -- Validate_Access_Subprogram_Instance --
13845 -----------------------------------------
13847 procedure Validate_Access_Subprogram_Instance is
13849 if not Is_Access_Type (Act_T)
13850 or else Ekind (Designated_Type (Act_T)) /= E_Subprogram_Type
13853 ("expect access type in instantiation of &", Actual, Gen_T);
13854 Abandon_Instantiation (Actual);
13857 -- According to AI05-288, actuals for access_to_subprograms must be
13858 -- subtype conformant with the generic formal. Previous to AI05-288
13859 -- only mode conformance was required.
13861 -- This is a binding interpretation that applies to previous versions
13862 -- of the language, no need to maintain previous weaker checks.
13864 Check_Subtype_Conformant
13865 (Designated_Type (Act_T),
13866 Designated_Type (A_Gen_T),
13870 if Ekind (Base_Type (Act_T)) = E_Access_Protected_Subprogram_Type then
13871 if Ekind (A_Gen_T) = E_Access_Subprogram_Type then
13873 ("protected access type not allowed for formal &",
13877 elsif Ekind (A_Gen_T) = E_Access_Protected_Subprogram_Type then
13879 ("expect protected access type for formal &",
13883 -- If the formal has a specified convention (which in most cases
13884 -- will be StdCall) verify that the actual has the same convention.
13886 if Has_Convention_Pragma (A_Gen_T)
13887 and then Convention (A_Gen_T) /= Convention (Act_T)
13889 Error_Msg_Name_1 := Get_Convention_Name (Convention (A_Gen_T));
13891 ("actual for formal & must have convention %", Actual, Gen_T);
13894 if Can_Never_Be_Null (A_Gen_T) /= Can_Never_Be_Null (Act_T) then
13896 ("non null exclusion of actual and formal & do not match",
13899 end Validate_Access_Subprogram_Instance;
13901 -----------------------------------
13902 -- Validate_Access_Type_Instance --
13903 -----------------------------------
13905 procedure Validate_Access_Type_Instance is
13906 Desig_Type : constant Entity_Id :=
13907 Find_Actual_Type (Designated_Type (A_Gen_T), A_Gen_T);
13908 Desig_Act : Entity_Id;
13911 if not Is_Access_Type (Act_T) then
13913 ("expect access type in instantiation of &", Actual, Gen_T);
13914 Abandon_Instantiation (Actual);
13917 if Is_Access_Constant (A_Gen_T) then
13918 if not Is_Access_Constant (Act_T) then
13920 ("actual type must be access-to-constant type", Actual);
13921 Abandon_Instantiation (Actual);
13924 if Is_Access_Constant (Act_T) then
13926 ("actual type must be access-to-variable type", Actual);
13927 Abandon_Instantiation (Actual);
13929 elsif Ekind (A_Gen_T) = E_General_Access_Type
13930 and then Ekind (Base_Type (Act_T)) /= E_General_Access_Type
13933 ("actual must be general access type!", Actual);
13934 Error_Msg_NE -- CODEFIX
13935 ("\add ALL to }!", Actual, Act_T);
13936 Abandon_Instantiation (Actual);
13940 -- The designated subtypes, that is to say the subtypes introduced
13941 -- by an access type declaration (and not by a subtype declaration)
13944 Desig_Act := Designated_Type (Base_Type (Act_T));
13946 -- The designated type may have been introduced through a limited_
13947 -- with clause, in which case retrieve the non-limited view. This
13948 -- applies to incomplete types as well as to class-wide types.
13950 if From_Limited_With (Desig_Act) then
13951 Desig_Act := Available_View (Desig_Act);
13954 if not Subtypes_Match (Desig_Type, Desig_Act) then
13956 ("designated type of actual does not match that of formal &",
13959 if not Predicates_Match (Desig_Type, Desig_Act) then
13960 Error_Msg_N ("\predicates do not match", Actual);
13963 Abandon_Instantiation (Actual);
13966 -- Ada 2005: null-exclusion indicators of the two types must agree
13968 if Can_Never_Be_Null (A_Gen_T) /= Can_Never_Be_Null (Act_T) then
13970 ("non null exclusion of actual and formal & do not match",
13973 end Validate_Access_Type_Instance;
13975 ----------------------------------
13976 -- Validate_Array_Type_Instance --
13977 ----------------------------------
13979 procedure Validate_Array_Type_Instance is
13984 function Formal_Dimensions return Nat;
13985 -- Count number of dimensions in array type formal
13987 -----------------------
13988 -- Formal_Dimensions --
13989 -----------------------
13991 function Formal_Dimensions return Nat is
13996 if Nkind (Def) = N_Constrained_Array_Definition then
13997 Index := First (Discrete_Subtype_Definitions (Def));
13999 Index := First (Subtype_Marks (Def));
14002 while Present (Index) loop
14008 end Formal_Dimensions;
14010 -- Start of processing for Validate_Array_Type_Instance
14013 if not Is_Array_Type (Act_T) then
14015 ("expect array type in instantiation of &", Actual, Gen_T);
14016 Abandon_Instantiation (Actual);
14018 elsif Nkind (Def) = N_Constrained_Array_Definition then
14019 if not Is_Constrained (Act_T) then
14021 ("expect constrained array in instantiation of &",
14023 Abandon_Instantiation (Actual);
14027 if Is_Constrained (Act_T) then
14029 ("expect unconstrained array in instantiation of &",
14031 Abandon_Instantiation (Actual);
14035 if Formal_Dimensions /= Number_Dimensions (Act_T) then
14037 ("dimensions of actual do not match formal &", Actual, Gen_T);
14038 Abandon_Instantiation (Actual);
14041 I1 := First_Index (A_Gen_T);
14042 I2 := First_Index (Act_T);
14043 for J in 1 .. Formal_Dimensions loop
14045 -- If the indexes of the actual were given by a subtype_mark,
14046 -- the index was transformed into a range attribute. Retrieve
14047 -- the original type mark for checking.
14049 if Is_Entity_Name (Original_Node (I2)) then
14050 T2 := Entity (Original_Node (I2));
14055 if not Subtypes_Match
14056 (Find_Actual_Type (Etype (I1), A_Gen_T), T2)
14059 ("index types of actual do not match those of formal &",
14061 Abandon_Instantiation (Actual);
14068 -- Check matching subtypes. Note that there are complex visibility
14069 -- issues when the generic is a child unit and some aspect of the
14070 -- generic type is declared in a parent unit of the generic. We do
14071 -- the test to handle this special case only after a direct check
14072 -- for static matching has failed. The case where both the component
14073 -- type and the array type are separate formals, and the component
14074 -- type is a private view may also require special checking in
14075 -- Subtypes_Match. Finally, we assume that a child instance where
14076 -- the component type comes from a formal of a parent instance is
14077 -- correct because the generic was correct. A more precise check
14078 -- seems too complex to install???
14081 (Component_Type (A_Gen_T), Component_Type (Act_T))
14084 (Find_Actual_Type (Component_Type (A_Gen_T), A_Gen_T),
14085 Component_Type (Act_T))
14087 (not Inside_A_Generic
14088 and then Is_Child_Unit (Scope (Component_Type (A_Gen_T))))
14093 ("component subtype of actual does not match that of formal &",
14095 Abandon_Instantiation (Actual);
14098 if Has_Aliased_Components (A_Gen_T)
14099 and then not Has_Aliased_Components (Act_T)
14102 ("actual must have aliased components to match formal type &",
14105 end Validate_Array_Type_Instance;
14107 -----------------------------------------------
14108 -- Validate_Derived_Interface_Type_Instance --
14109 -----------------------------------------------
14111 procedure Validate_Derived_Interface_Type_Instance is
14112 Par : constant Entity_Id := Entity (Subtype_Indication (Def));
14116 -- First apply interface instance checks
14118 Validate_Interface_Type_Instance;
14120 -- Verify that immediate parent interface is an ancestor of
14124 and then not Interface_Present_In_Ancestor (Act_T, Par)
14127 ("interface actual must include progenitor&", Actual, Par);
14130 -- Now verify that the actual includes all other ancestors of
14133 Elmt := First_Elmt (Interfaces (A_Gen_T));
14134 while Present (Elmt) loop
14135 if not Interface_Present_In_Ancestor
14136 (Act_T, Get_Instance_Of (Node (Elmt)))
14139 ("interface actual must include progenitor&",
14140 Actual, Node (Elmt));
14145 end Validate_Derived_Interface_Type_Instance;
14147 ------------------------------------
14148 -- Validate_Derived_Type_Instance --
14149 ------------------------------------
14151 procedure Validate_Derived_Type_Instance is
14152 Actual_Discr : Entity_Id;
14153 Ancestor_Discr : Entity_Id;
14156 -- Verify that the actual includes the progenitors of the formal,
14157 -- if any. The formal may depend on previous formals and their
14158 -- instance, so we must examine instance of interfaces if present.
14159 -- The actual may be an extension of an interface, in which case
14160 -- it does not appear in the interface list, so this must be
14161 -- checked separately.
14163 if Present (Interface_List (Def)) then
14164 if not Has_Interfaces (Act_T) then
14166 ("actual must implement all interfaces of formal&",
14171 Act_Iface_List : Elist_Id;
14173 Iface_Ent : Entity_Id;
14175 function Instance_Exists (I : Entity_Id) return Boolean;
14176 -- If the interface entity is declared in a generic unit,
14177 -- this can only be legal if we are within an instantiation
14178 -- of a child of that generic. There is currently no
14179 -- mechanism to relate an interface declared within a
14180 -- generic to the corresponding interface in an instance,
14181 -- so we traverse the list of interfaces of the actual,
14182 -- looking for a name match.
14184 ---------------------
14185 -- Instance_Exists --
14186 ---------------------
14188 function Instance_Exists (I : Entity_Id) return Boolean is
14189 Iface_Elmt : Elmt_Id;
14192 Iface_Elmt := First_Elmt (Act_Iface_List);
14193 while Present (Iface_Elmt) loop
14194 if Is_Generic_Instance (Scope (Node (Iface_Elmt)))
14195 and then Chars (Node (Iface_Elmt)) = Chars (I)
14200 Next_Elmt (Iface_Elmt);
14204 end Instance_Exists;
14207 Iface := First (Abstract_Interface_List (A_Gen_T));
14208 Collect_Interfaces (Act_T, Act_Iface_List);
14210 while Present (Iface) loop
14211 Iface_Ent := Get_Instance_Of (Entity (Iface));
14213 if Is_Ancestor (Iface_Ent, Act_T)
14214 or else Is_Progenitor (Iface_Ent, Act_T)
14218 elsif Ekind (Scope (Iface_Ent)) = E_Generic_Package
14219 and then Instance_Exists (Iface_Ent)
14224 Error_Msg_Name_1 := Chars (Act_T);
14226 ("actual% must implement interface&",
14227 Actual, Etype (Iface));
14236 -- If the parent type in the generic declaration is itself a previous
14237 -- formal type, then it is local to the generic and absent from the
14238 -- analyzed generic definition. In that case the ancestor is the
14239 -- instance of the formal (which must have been instantiated
14240 -- previously), unless the ancestor is itself a formal derived type.
14241 -- In this latter case (which is the subject of Corrigendum 8652/0038
14242 -- (AI-202) the ancestor of the formals is the ancestor of its
14243 -- parent. Otherwise, the analyzed generic carries the parent type.
14244 -- If the parent type is defined in a previous formal package, then
14245 -- the scope of that formal package is that of the generic type
14246 -- itself, and it has already been mapped into the corresponding type
14247 -- in the actual package.
14249 -- Common case: parent type defined outside of the generic
14251 if Is_Entity_Name (Subtype_Mark (Def))
14252 and then Present (Entity (Subtype_Mark (Def)))
14254 Ancestor := Get_Instance_Of (Entity (Subtype_Mark (Def)));
14256 -- Check whether parent is defined in a previous formal package
14259 Scope (Scope (Base_Type (Etype (A_Gen_T)))) = Scope (A_Gen_T)
14262 Get_Instance_Of (Base_Type (Etype (A_Gen_T)));
14264 -- The type may be a local derivation, or a type extension of a
14265 -- previous formal, or of a formal of a parent package.
14267 elsif Is_Derived_Type (Get_Instance_Of (A_Gen_T))
14269 Ekind (Get_Instance_Of (A_Gen_T)) = E_Record_Type_With_Private
14271 -- Check whether the parent is another formal derived type in the
14272 -- same generic unit.
14274 if Etype (A_Gen_T) /= A_Gen_T
14275 and then Is_Generic_Type (Etype (A_Gen_T))
14276 and then Scope (Etype (A_Gen_T)) = Scope (A_Gen_T)
14277 and then Etype (Etype (A_Gen_T)) /= Etype (A_Gen_T)
14279 -- Locate ancestor of parent from the subtype declaration
14280 -- created for the actual.
14286 Decl := First (Actual_Decls);
14287 while Present (Decl) loop
14288 if Nkind (Decl) = N_Subtype_Declaration
14289 and then Chars (Defining_Identifier (Decl)) =
14290 Chars (Etype (A_Gen_T))
14292 Ancestor := Generic_Parent_Type (Decl);
14300 pragma Assert (Present (Ancestor));
14302 -- The ancestor itself may be a previous formal that has been
14305 Ancestor := Get_Instance_Of (Ancestor);
14308 Ancestor := Get_Instance_Of (Etype (Get_Instance_Of (A_Gen_T)));
14311 -- Check whether parent is a previous formal of the current generic
14313 elsif Is_Derived_Type (A_Gen_T)
14314 and then Is_Generic_Type (Etype (A_Gen_T))
14315 and then Scope (A_Gen_T) = Scope (Etype (A_Gen_T))
14317 Ancestor := Get_Instance_Of (First_Subtype (Etype (A_Gen_T)));
14319 -- An unusual case: the actual is a type declared in a parent unit,
14320 -- but is not a formal type so there is no instance_of for it.
14321 -- Retrieve it by analyzing the record extension.
14323 elsif Is_Child_Unit (Scope (A_Gen_T))
14324 and then In_Open_Scopes (Scope (Act_T))
14325 and then Is_Generic_Instance (Scope (Act_T))
14327 Analyze (Subtype_Mark (Def));
14328 Ancestor := Entity (Subtype_Mark (Def));
14331 Ancestor := Get_Instance_Of (Etype (Base_Type (A_Gen_T)));
14334 -- If the formal derived type has pragma Preelaborable_Initialization
14335 -- then the actual type must have preelaborable initialization.
14337 if Known_To_Have_Preelab_Init (A_Gen_T)
14338 and then not Has_Preelaborable_Initialization (Act_T)
14341 ("actual for & must have preelaborable initialization",
14345 -- Ada 2005 (AI-251)
14347 if Ada_Version >= Ada_2005 and then Is_Interface (Ancestor) then
14348 if not Interface_Present_In_Ancestor (Act_T, Ancestor) then
14350 ("(Ada 2005) expected type implementing & in instantiation",
14354 -- Finally verify that the (instance of) the ancestor is an ancestor
14357 elsif not Is_Ancestor (Base_Type (Ancestor), Act_T) then
14359 ("expect type derived from & in instantiation",
14360 Actual, First_Subtype (Ancestor));
14361 Abandon_Instantiation (Actual);
14364 -- Ada 2005 (AI-443): Synchronized formal derived type checks. Note
14365 -- that the formal type declaration has been rewritten as a private
14368 if Ada_Version >= Ada_2005
14369 and then Nkind (Parent (A_Gen_T)) = N_Private_Extension_Declaration
14370 and then Synchronized_Present (Parent (A_Gen_T))
14372 -- The actual must be a synchronized tagged type
14374 if not Is_Tagged_Type (Act_T) then
14376 ("actual of synchronized type must be tagged", Actual);
14377 Abandon_Instantiation (Actual);
14379 elsif Nkind (Parent (Act_T)) = N_Full_Type_Declaration
14380 and then Nkind (Type_Definition (Parent (Act_T))) =
14381 N_Derived_Type_Definition
14382 and then not Synchronized_Present
14383 (Type_Definition (Parent (Act_T)))
14386 ("actual of synchronized type must be synchronized", Actual);
14387 Abandon_Instantiation (Actual);
14391 -- Perform atomic/volatile checks (RM C.6(12)). Note that AI05-0218-1
14392 -- removes the second instance of the phrase "or allow pass by copy".
14394 -- For Ada 2022, the aspect may be specified explicitly for the
14395 -- formal regardless of whether an ancestor obeys it.
14397 if Is_Atomic (Act_T)
14398 and then not Is_Atomic (Ancestor)
14399 and then not Is_Atomic (A_Gen_T)
14402 ("cannot have atomic actual type for non-atomic formal type",
14405 elsif Is_Volatile (Act_T)
14406 and then not Is_Volatile (Ancestor)
14407 and then not Is_Volatile (A_Gen_T)
14410 ("cannot have volatile actual type for non-volatile formal type",
14414 -- It should not be necessary to check for unknown discriminants on
14415 -- Formal, but for some reason Has_Unknown_Discriminants is false for
14416 -- A_Gen_T, so Is_Definite_Subtype incorrectly returns True. This
14417 -- needs fixing. ???
14419 if Is_Definite_Subtype (A_Gen_T)
14420 and then not Unknown_Discriminants_Present (Formal)
14421 and then not Is_Definite_Subtype (Act_T)
14423 Error_Msg_N ("actual subtype must be constrained", Actual);
14424 Abandon_Instantiation (Actual);
14427 if not Unknown_Discriminants_Present (Formal) then
14428 if Is_Constrained (Ancestor) then
14429 if not Is_Constrained (Act_T) then
14430 Error_Msg_N ("actual subtype must be constrained", Actual);
14431 Abandon_Instantiation (Actual);
14434 -- Ancestor is unconstrained, Check if generic formal and actual
14435 -- agree on constrainedness. The check only applies to array types
14436 -- and discriminated types.
14438 elsif Is_Constrained (Act_T) then
14439 if Ekind (Ancestor) = E_Access_Type
14440 or else (not Is_Constrained (A_Gen_T)
14441 and then Is_Composite_Type (A_Gen_T))
14443 Error_Msg_N ("actual subtype must be unconstrained", Actual);
14444 Abandon_Instantiation (Actual);
14447 -- A class-wide type is only allowed if the formal has unknown
14450 elsif Is_Class_Wide_Type (Act_T)
14451 and then not Has_Unknown_Discriminants (Ancestor)
14454 ("actual for & cannot be a class-wide type", Actual, Gen_T);
14455 Abandon_Instantiation (Actual);
14457 -- Otherwise, the formal and actual must have the same number
14458 -- of discriminants and each discriminant of the actual must
14459 -- correspond to a discriminant of the formal.
14461 elsif Has_Discriminants (Act_T)
14462 and then not Has_Unknown_Discriminants (Act_T)
14463 and then Has_Discriminants (Ancestor)
14465 Actual_Discr := First_Discriminant (Act_T);
14466 Ancestor_Discr := First_Discriminant (Ancestor);
14467 while Present (Actual_Discr)
14468 and then Present (Ancestor_Discr)
14470 if Base_Type (Act_T) /= Base_Type (Ancestor) and then
14471 No (Corresponding_Discriminant (Actual_Discr))
14474 ("discriminant & does not correspond "
14475 & "to ancestor discriminant", Actual, Actual_Discr);
14476 Abandon_Instantiation (Actual);
14479 Next_Discriminant (Actual_Discr);
14480 Next_Discriminant (Ancestor_Discr);
14483 if Present (Actual_Discr) or else Present (Ancestor_Discr) then
14485 ("actual for & must have same number of discriminants",
14487 Abandon_Instantiation (Actual);
14490 -- This case should be caught by the earlier check for
14491 -- constrainedness, but the check here is added for completeness.
14493 elsif Has_Discriminants (Act_T)
14494 and then not Has_Unknown_Discriminants (Act_T)
14497 ("actual for & must not have discriminants", Actual, Gen_T);
14498 Abandon_Instantiation (Actual);
14500 elsif Has_Discriminants (Ancestor) then
14502 ("actual for & must have known discriminants", Actual, Gen_T);
14503 Abandon_Instantiation (Actual);
14506 if not Subtypes_Statically_Compatible
14507 (Act_T, Ancestor, Formal_Derived_Matching => True)
14510 ("actual for & must be statically compatible with ancestor",
14513 if not Predicates_Compatible (Act_T, Ancestor) then
14515 ("\predicate on actual is not compatible with ancestor",
14519 Abandon_Instantiation (Actual);
14523 -- If the formal and actual types are abstract, check that there
14524 -- are no abstract primitives of the actual type that correspond to
14525 -- nonabstract primitives of the formal type (second sentence of
14528 if Is_Abstract_Type (A_Gen_T) and then Is_Abstract_Type (Act_T) then
14529 Check_Abstract_Primitives : declare
14530 Gen_Prims : constant Elist_Id :=
14531 Primitive_Operations (A_Gen_T);
14532 Gen_Elmt : Elmt_Id;
14533 Gen_Subp : Entity_Id;
14534 Anc_Subp : Entity_Id;
14535 Anc_Formal : Entity_Id;
14536 Anc_F_Type : Entity_Id;
14538 Act_Prims : constant Elist_Id := Primitive_Operations (Act_T);
14539 Act_Elmt : Elmt_Id;
14540 Act_Subp : Entity_Id;
14541 Act_Formal : Entity_Id;
14542 Act_F_Type : Entity_Id;
14544 Subprograms_Correspond : Boolean;
14546 function Is_Tagged_Ancestor (T1, T2 : Entity_Id) return Boolean;
14547 -- Returns true if T2 is derived directly or indirectly from
14548 -- T1, including derivations from interfaces. T1 and T2 are
14549 -- required to be specific tagged base types.
14551 ------------------------
14552 -- Is_Tagged_Ancestor --
14553 ------------------------
14555 function Is_Tagged_Ancestor (T1, T2 : Entity_Id) return Boolean
14557 Intfc_Elmt : Elmt_Id;
14560 -- The predicate is satisfied if the types are the same
14565 -- If we've reached the top of the derivation chain then
14566 -- we know that T1 is not an ancestor of T2.
14568 elsif Etype (T2) = T2 then
14571 -- Proceed to check T2's immediate parent
14573 elsif Is_Ancestor (T1, Base_Type (Etype (T2))) then
14576 -- Finally, check to see if T1 is an ancestor of any of T2's
14580 Intfc_Elmt := First_Elmt (Interfaces (T2));
14581 while Present (Intfc_Elmt) loop
14582 if Is_Ancestor (T1, Node (Intfc_Elmt)) then
14586 Next_Elmt (Intfc_Elmt);
14591 end Is_Tagged_Ancestor;
14593 -- Start of processing for Check_Abstract_Primitives
14596 -- Loop over all of the formal derived type's primitives
14598 Gen_Elmt := First_Elmt (Gen_Prims);
14599 while Present (Gen_Elmt) loop
14600 Gen_Subp := Node (Gen_Elmt);
14602 -- If the primitive of the formal is not abstract, then
14603 -- determine whether there is a corresponding primitive of
14604 -- the actual type that's abstract.
14606 if not Is_Abstract_Subprogram (Gen_Subp) then
14607 Act_Elmt := First_Elmt (Act_Prims);
14608 while Present (Act_Elmt) loop
14609 Act_Subp := Node (Act_Elmt);
14611 -- If we find an abstract primitive of the actual,
14612 -- then we need to test whether it corresponds to the
14613 -- subprogram from which the generic formal primitive
14616 if Is_Abstract_Subprogram (Act_Subp) then
14617 Anc_Subp := Alias (Gen_Subp);
14619 -- Test whether we have a corresponding primitive
14620 -- by comparing names, kinds, formal types, and
14623 if Chars (Anc_Subp) = Chars (Act_Subp)
14624 and then Ekind (Anc_Subp) = Ekind (Act_Subp)
14626 Anc_Formal := First_Formal (Anc_Subp);
14627 Act_Formal := First_Formal (Act_Subp);
14628 while Present (Anc_Formal)
14629 and then Present (Act_Formal)
14631 Anc_F_Type := Etype (Anc_Formal);
14632 Act_F_Type := Etype (Act_Formal);
14634 if Ekind (Anc_F_Type) =
14635 E_Anonymous_Access_Type
14637 Anc_F_Type := Designated_Type (Anc_F_Type);
14639 if Ekind (Act_F_Type) =
14640 E_Anonymous_Access_Type
14643 Designated_Type (Act_F_Type);
14649 Ekind (Act_F_Type) = E_Anonymous_Access_Type
14654 Anc_F_Type := Base_Type (Anc_F_Type);
14655 Act_F_Type := Base_Type (Act_F_Type);
14657 -- If the formal is controlling, then the
14658 -- the type of the actual primitive's formal
14659 -- must be derived directly or indirectly
14660 -- from the type of the ancestor primitive's
14663 if Is_Controlling_Formal (Anc_Formal) then
14664 if not Is_Tagged_Ancestor
14665 (Anc_F_Type, Act_F_Type)
14670 -- Otherwise the types of the formals must
14673 elsif Anc_F_Type /= Act_F_Type then
14677 Next_Formal (Anc_Formal);
14678 Next_Formal (Act_Formal);
14681 -- If we traversed through all of the formals
14682 -- then so far the subprograms correspond, so
14683 -- now check that any result types correspond.
14685 if No (Anc_Formal) and then No (Act_Formal) then
14686 Subprograms_Correspond := True;
14688 if Ekind (Act_Subp) = E_Function then
14689 Anc_F_Type := Etype (Anc_Subp);
14690 Act_F_Type := Etype (Act_Subp);
14692 if Ekind (Anc_F_Type) =
14693 E_Anonymous_Access_Type
14696 Designated_Type (Anc_F_Type);
14698 if Ekind (Act_F_Type) =
14699 E_Anonymous_Access_Type
14702 Designated_Type (Act_F_Type);
14704 Subprograms_Correspond := False;
14709 = E_Anonymous_Access_Type
14711 Subprograms_Correspond := False;
14714 Anc_F_Type := Base_Type (Anc_F_Type);
14715 Act_F_Type := Base_Type (Act_F_Type);
14717 -- Now either the result types must be
14718 -- the same or, if the result type is
14719 -- controlling, the result type of the
14720 -- actual primitive must descend from the
14721 -- result type of the ancestor primitive.
14723 if Subprograms_Correspond
14724 and then Anc_F_Type /= Act_F_Type
14726 Has_Controlling_Result (Anc_Subp)
14727 and then not Is_Tagged_Ancestor
14728 (Anc_F_Type, Act_F_Type)
14730 Subprograms_Correspond := False;
14734 -- Found a matching subprogram belonging to
14735 -- formal ancestor type, so actual subprogram
14736 -- corresponds and this violates 3.9.3(9).
14738 if Subprograms_Correspond then
14740 ("abstract subprogram & overrides "
14741 & "nonabstract subprogram of ancestor",
14748 Next_Elmt (Act_Elmt);
14752 Next_Elmt (Gen_Elmt);
14754 end Check_Abstract_Primitives;
14757 -- Verify that limitedness matches. If parent is a limited
14758 -- interface then the generic formal is not unless declared
14759 -- explicitly so. If not declared limited, the actual cannot be
14760 -- limited (see AI05-0087).
14762 if Is_Limited_Type (Act_T) and then not Is_Limited_Type (A_Gen_T) then
14763 if not In_Instance then
14765 ("actual for non-limited & cannot be a limited type",
14767 Explain_Limited_Type (Act_T, Actual);
14768 Abandon_Instantiation (Actual);
14772 -- Check for AI12-0036
14775 Formal_Is_Private_Extension : constant Boolean :=
14776 Nkind (Parent (A_Gen_T)) = N_Private_Extension_Declaration;
14778 Actual_Is_Tagged : constant Boolean := Is_Tagged_Type (Act_T);
14781 if Actual_Is_Tagged /= Formal_Is_Private_Extension then
14782 if not In_Instance then
14783 if Actual_Is_Tagged then
14785 ("actual for & cannot be a tagged type", Actual, Gen_T);
14788 ("actual for & must be a tagged type", Actual, Gen_T);
14791 Abandon_Instantiation (Actual);
14795 end Validate_Derived_Type_Instance;
14797 ----------------------------------------
14798 -- Validate_Discriminated_Formal_Type --
14799 ----------------------------------------
14801 procedure Validate_Discriminated_Formal_Type is
14802 Formal_Discr : Entity_Id;
14803 Actual_Discr : Entity_Id;
14804 Formal_Subt : Entity_Id;
14807 if Has_Discriminants (A_Gen_T) then
14808 if not Has_Discriminants (Act_T) then
14810 ("actual for & must have discriminants", Actual, Gen_T);
14811 Abandon_Instantiation (Actual);
14813 elsif Is_Constrained (Act_T) then
14815 ("actual for & must be unconstrained", Actual, Gen_T);
14816 Abandon_Instantiation (Actual);
14819 Formal_Discr := First_Discriminant (A_Gen_T);
14820 Actual_Discr := First_Discriminant (Act_T);
14821 while Formal_Discr /= Empty loop
14822 if Actual_Discr = Empty then
14824 ("discriminants on actual do not match formal",
14826 Abandon_Instantiation (Actual);
14829 Formal_Subt := Get_Instance_Of (Etype (Formal_Discr));
14831 -- Access discriminants match if designated types do
14833 if Ekind (Base_Type (Formal_Subt)) = E_Anonymous_Access_Type
14834 and then (Ekind (Base_Type (Etype (Actual_Discr)))) =
14835 E_Anonymous_Access_Type
14839 (Designated_Type (Base_Type (Formal_Subt))),
14840 Designated_Type (Base_Type (Etype (Actual_Discr))))
14844 elsif Base_Type (Formal_Subt) /=
14845 Base_Type (Etype (Actual_Discr))
14848 ("types of actual discriminants must match formal",
14850 Abandon_Instantiation (Actual);
14852 elsif not Subtypes_Statically_Match
14853 (Formal_Subt, Etype (Actual_Discr))
14854 and then Ada_Version >= Ada_95
14857 ("subtypes of actual discriminants must match formal",
14859 Abandon_Instantiation (Actual);
14862 Next_Discriminant (Formal_Discr);
14863 Next_Discriminant (Actual_Discr);
14866 if Actual_Discr /= Empty then
14868 ("discriminants on actual do not match formal",
14870 Abandon_Instantiation (Actual);
14874 end Validate_Discriminated_Formal_Type;
14876 ---------------------------------------
14877 -- Validate_Incomplete_Type_Instance --
14878 ---------------------------------------
14880 procedure Validate_Incomplete_Type_Instance is
14882 if not Is_Tagged_Type (Act_T)
14883 and then Is_Tagged_Type (A_Gen_T)
14886 ("actual for & must be a tagged type", Actual, Gen_T);
14889 Validate_Discriminated_Formal_Type;
14890 end Validate_Incomplete_Type_Instance;
14892 --------------------------------------
14893 -- Validate_Interface_Type_Instance --
14894 --------------------------------------
14896 procedure Validate_Interface_Type_Instance is
14898 if not Is_Interface (Act_T) then
14900 ("actual for formal interface type must be an interface",
14903 elsif Is_Limited_Type (Act_T) /= Is_Limited_Type (A_Gen_T)
14904 or else Is_Task_Interface (A_Gen_T) /= Is_Task_Interface (Act_T)
14905 or else Is_Protected_Interface (A_Gen_T) /=
14906 Is_Protected_Interface (Act_T)
14907 or else Is_Synchronized_Interface (A_Gen_T) /=
14908 Is_Synchronized_Interface (Act_T)
14911 ("actual for interface& does not match (RM 12.5.5(4))",
14914 end Validate_Interface_Type_Instance;
14916 ------------------------------------
14917 -- Validate_Private_Type_Instance --
14918 ------------------------------------
14920 procedure Validate_Private_Type_Instance is
14922 if Is_Limited_Type (Act_T)
14923 and then not Is_Limited_Type (A_Gen_T)
14925 if In_Instance then
14929 ("actual for non-limited & cannot be a limited type", Actual,
14931 Explain_Limited_Type (Act_T, Actual);
14932 Abandon_Instantiation (Actual);
14935 elsif Known_To_Have_Preelab_Init (A_Gen_T)
14936 and then not Has_Preelaborable_Initialization (Act_T)
14939 ("actual for & must have preelaborable initialization", Actual,
14942 elsif not Is_Definite_Subtype (Act_T)
14943 and then Is_Definite_Subtype (A_Gen_T)
14944 and then No (Class_Wide_Equivalent_Type (Act_T))
14945 and then Ada_Version >= Ada_95
14948 ("actual for & must be a definite subtype", Actual, Gen_T);
14950 elsif not Is_Tagged_Type (Act_T)
14951 and then Is_Tagged_Type (A_Gen_T)
14954 ("actual for & must be a tagged type", Actual, Gen_T);
14956 -- For generic formal tagged types with the First_Controlling_Param
14957 -- aspect, ensure that the actual type also has this aspect.
14959 elsif Is_Tagged_Type (Act_T)
14960 and then Is_Tagged_Type (A_Gen_T)
14961 and then not Has_First_Controlling_Parameter_Aspect (Act_T)
14962 and then Has_First_Controlling_Parameter_Aspect (A_Gen_T)
14965 ("actual for & must be a 'First_'Controlling_'Parameter tagged "
14966 & "type", Actual, Gen_T);
14969 Validate_Discriminated_Formal_Type;
14971 end Validate_Private_Type_Instance;
14973 -- Start of processing for Instantiate_Type
14976 if not Is_Entity_Name (Actual)
14977 or else not Is_Type (Entity (Actual))
14980 ("expect valid subtype mark to instantiate &", Actual, Gen_T);
14981 Abandon_Instantiation (Actual);
14984 Act_T := Entity (Actual);
14986 -- Obtain the class-wide equivalent type and use it for the
14987 -- instantiation instead of a mutably tagged type.
14989 if Present (Class_Wide_Equivalent_Type (Act_T)) then
14990 Act_T := Class_Wide_Equivalent_Type (Act_T);
14993 -- Ada 2005 (AI-216): An Unchecked_Union subtype shall only be passed
14994 -- as a generic actual parameter if the corresponding formal type
14995 -- does not have a known_discriminant_part, or is a formal derived
14996 -- type that is an Unchecked_Union type.
14998 if Is_Unchecked_Union (Base_Type (Act_T)) then
14999 if not Has_Discriminants (A_Gen_T)
15000 or else (Is_Derived_Type (A_Gen_T)
15001 and then Is_Unchecked_Union (A_Gen_T))
15005 Error_Msg_N ("unchecked union cannot be the actual for a "
15006 & "discriminated formal type", Act_T);
15011 -- Deal with fixed/floating restrictions
15013 if Is_Floating_Point_Type (Act_T) then
15014 Check_Restriction (No_Floating_Point, Actual);
15015 elsif Is_Fixed_Point_Type (Act_T) then
15016 Check_Restriction (No_Fixed_Point, Actual);
15019 -- Deal with error of using incomplete type as generic actual.
15020 -- This includes limited views of a type, even if the non-limited
15021 -- view may be available.
15023 if Ekind (Act_T) = E_Incomplete_Type
15024 or else (Is_Class_Wide_Type (Act_T)
15025 and then Ekind (Root_Type (Act_T)) = E_Incomplete_Type)
15027 -- If the formal is an incomplete type, the actual can be
15028 -- incomplete as well, but if an actual incomplete type has
15029 -- a full view, then we'll retrieve that.
15031 if Ekind (A_Gen_T) = E_Incomplete_Type
15032 and then No (Full_View (Act_T))
15036 elsif Is_Class_Wide_Type (Act_T)
15037 or else No (Full_View (Act_T))
15039 Error_Msg_N ("premature use of incomplete type", Actual);
15040 Abandon_Instantiation (Actual);
15043 Act_T := Full_View (Act_T);
15044 Set_Entity (Actual, Act_T);
15046 if Has_Private_Component (Act_T) then
15048 ("premature use of type with private component", Actual);
15052 -- Deal with error of premature use of private type as generic actual,
15053 -- which is allowed for incomplete formals.
15055 elsif Ekind (A_Gen_T) /= E_Incomplete_Type then
15056 if Is_Private_Type (Act_T)
15057 and then Is_Private_Type (Base_Type (Act_T))
15058 and then not Is_Generic_Type (Act_T)
15059 and then not Is_Derived_Type (Act_T)
15060 and then No (Full_View (Root_Type (Act_T)))
15062 Error_Msg_N ("premature use of private type", Actual);
15064 elsif Has_Private_Component (Act_T) then
15066 ("premature use of type with private component", Actual);
15070 Set_Instance_Of (A_Gen_T, Act_T);
15072 -- If the type is generic, the class-wide type may also be used
15074 if Is_Tagged_Type (A_Gen_T)
15075 and then Is_Tagged_Type (Act_T)
15076 and then not Is_Class_Wide_Type (A_Gen_T)
15078 Set_Instance_Of (Class_Wide_Type (A_Gen_T),
15079 Class_Wide_Type (Act_T));
15082 if not Is_Abstract_Type (A_Gen_T)
15083 and then Is_Abstract_Type (Act_T)
15086 ("actual of non-abstract formal cannot be abstract", Actual);
15089 -- A generic scalar type is a first subtype for which we generate
15090 -- an anonymous base type. Indicate that the instance of this base
15091 -- is the base type of the actual.
15093 if Is_Scalar_Type (A_Gen_T) then
15094 Set_Instance_Of (Etype (A_Gen_T), Etype (Act_T));
15097 Check_Shared_Variable_Control_Aspects;
15099 if not Error_Posted (Act_T) then
15100 case Nkind (Def) is
15101 when N_Formal_Private_Type_Definition =>
15102 Validate_Private_Type_Instance;
15104 when N_Formal_Incomplete_Type_Definition =>
15105 Validate_Incomplete_Type_Instance;
15107 when N_Formal_Derived_Type_Definition =>
15108 Validate_Derived_Type_Instance;
15110 when N_Formal_Discrete_Type_Definition =>
15111 if not Is_Discrete_Type (Act_T) then
15113 ("expect discrete type in instantiation of&",
15115 Abandon_Instantiation (Actual);
15118 Diagnose_Predicated_Actual;
15120 when N_Formal_Signed_Integer_Type_Definition =>
15121 if not Is_Signed_Integer_Type (Act_T) then
15123 ("expect signed integer type in instantiation of&",
15125 Abandon_Instantiation (Actual);
15128 Diagnose_Predicated_Actual;
15130 when N_Formal_Modular_Type_Definition =>
15131 if not Is_Modular_Integer_Type (Act_T) then
15133 ("expect modular type in instantiation of &",
15135 Abandon_Instantiation (Actual);
15138 Diagnose_Predicated_Actual;
15140 when N_Formal_Floating_Point_Definition =>
15141 if not Is_Floating_Point_Type (Act_T) then
15143 ("expect float type in instantiation of &", Actual, Gen_T);
15144 Abandon_Instantiation (Actual);
15147 when N_Formal_Ordinary_Fixed_Point_Definition =>
15148 if not Is_Ordinary_Fixed_Point_Type (Act_T) then
15150 ("expect ordinary fixed point type in instantiation of &",
15152 Abandon_Instantiation (Actual);
15155 when N_Formal_Decimal_Fixed_Point_Definition =>
15156 if not Is_Decimal_Fixed_Point_Type (Act_T) then
15158 ("expect decimal type in instantiation of &",
15160 Abandon_Instantiation (Actual);
15163 when N_Array_Type_Definition =>
15164 Validate_Array_Type_Instance;
15166 when N_Access_To_Object_Definition =>
15167 Validate_Access_Type_Instance;
15169 when N_Access_Function_Definition
15170 | N_Access_Procedure_Definition
15172 Validate_Access_Subprogram_Instance;
15174 when N_Record_Definition =>
15175 Validate_Interface_Type_Instance;
15177 when N_Derived_Type_Definition =>
15178 Validate_Derived_Interface_Type_Instance;
15181 raise Program_Error;
15185 Subt := New_Copy (Gen_T);
15187 -- Use adjusted sloc of subtype name as the location for other nodes in
15188 -- the subtype declaration.
15190 Loc := Sloc (Subt);
15193 Make_Subtype_Declaration (Loc,
15194 Defining_Identifier => Subt,
15195 Subtype_Indication => New_Occurrence_Of (Act_T, Loc));
15197 Copy_Ghost_Aspect (Formal, To => Decl_Node);
15199 -- Record whether the actual is private at this point, so that
15200 -- Check_Generic_Actuals can restore its proper view before the
15201 -- semantic analysis of the instance.
15203 if Is_Private_Type (Act_T) then
15204 Set_Has_Private_View (Subtype_Indication (Decl_Node));
15206 elsif (Is_Access_Type (Act_T)
15207 and then Is_Private_Type (Designated_Type (Act_T)))
15208 or else (Is_Array_Type (Act_T)
15210 Is_Private_Type (Component_Type_For_Private_View (Act_T)))
15212 Set_Has_Secondary_Private_View (Subtype_Indication (Decl_Node));
15215 -- In Ada 2012 the actual may be a limited view. Indicate that
15216 -- the local subtype must be treated as such.
15218 if From_Limited_With (Act_T) then
15219 Mutate_Ekind (Subt, E_Incomplete_Subtype);
15220 Set_From_Limited_With (Subt);
15223 Decl_Nodes := New_List (Decl_Node);
15225 -- Flag actual derived types so their elaboration produces the
15226 -- appropriate renamings for the primitive operations of the ancestor.
15227 -- Flag actual for formal private types as well, to determine whether
15228 -- operations in the private part may override inherited operations.
15229 -- If the formal has an interface list, the ancestor is not the
15230 -- parent, but the analyzed formal that includes the interface
15231 -- operations of all its progenitors.
15233 -- Same treatment for formal private types, so we can check whether the
15234 -- type is tagged limited when validating derivations in the private
15235 -- part. (See AI05-096).
15237 if Nkind (Def) = N_Formal_Derived_Type_Definition then
15238 if Present (Interface_List (Def)) then
15239 Set_Generic_Parent_Type (Decl_Node, A_Gen_T);
15241 Set_Generic_Parent_Type (Decl_Node, Ancestor);
15244 elsif Nkind (Def) in N_Formal_Private_Type_Definition
15245 | N_Formal_Incomplete_Type_Definition
15247 Set_Generic_Parent_Type (Decl_Node, A_Gen_T);
15250 -- If the actual is a synchronized type that implements an interface,
15251 -- the primitive operations are attached to the corresponding record,
15252 -- and we have to treat it as an additional generic actual, so that its
15253 -- primitive operations become visible in the instance. The task or
15254 -- protected type itself does not carry primitive operations.
15256 if Is_Concurrent_Type (Act_T)
15257 and then Is_Tagged_Type (Act_T)
15258 and then Present (Corresponding_Record_Type (Act_T))
15259 and then Present (Ancestor)
15260 and then Is_Interface (Ancestor)
15263 Corr_Rec : constant Entity_Id :=
15264 Corresponding_Record_Type (Act_T);
15265 New_Corr : Entity_Id;
15266 Corr_Decl : Node_Id;
15269 New_Corr := Make_Temporary (Loc, 'S');
15271 Make_Subtype_Declaration (Loc,
15272 Defining_Identifier => New_Corr,
15273 Subtype_Indication =>
15274 New_Occurrence_Of (Corr_Rec, Loc));
15275 Append_To (Decl_Nodes, Corr_Decl);
15277 if Ekind (Act_T) = E_Task_Type then
15278 Mutate_Ekind (Subt, E_Task_Subtype);
15280 Mutate_Ekind (Subt, E_Protected_Subtype);
15283 Set_Corresponding_Record_Type (Subt, Corr_Rec);
15284 Set_Generic_Parent_Type (Corr_Decl, Ancestor);
15285 Set_Generic_Parent_Type (Decl_Node, Empty);
15289 -- For a floating-point type, capture dimension info if any, because
15290 -- the generated subtype declaration does not come from source and
15291 -- will not process dimensions.
15293 if Is_Floating_Point_Type (Act_T) then
15294 Copy_Dimensions (Act_T, Subt);
15298 end Instantiate_Type;
15300 -----------------------------
15301 -- Is_Abbreviated_Instance --
15302 -----------------------------
15304 function Is_Abbreviated_Instance (E : Entity_Id) return Boolean is
15306 return Ekind (E) = E_Package
15307 and then Present (Hidden_In_Formal_Instance (E));
15308 end Is_Abbreviated_Instance;
15310 ---------------------
15311 -- Is_In_Main_Unit --
15312 ---------------------
15314 function Is_In_Main_Unit (N : Node_Id) return Boolean is
15315 Unum : constant Unit_Number_Type := Get_Source_Unit (N);
15316 Current_Unit : Node_Id;
15319 if Unum = Main_Unit then
15322 -- If the current unit is a subunit then it is either the main unit or
15323 -- is being compiled as part of the main unit.
15325 elsif Nkind (N) = N_Compilation_Unit then
15326 return Nkind (Unit (N)) = N_Subunit;
15329 Current_Unit := Parent (N);
15330 while Present (Current_Unit)
15331 and then Nkind (Current_Unit) /= N_Compilation_Unit
15333 Current_Unit := Parent (Current_Unit);
15336 -- The instantiation node is in the main unit, or else the current node
15337 -- (perhaps as the result of nested instantiations) is in the main unit,
15338 -- or in the declaration of the main unit, which in this last case must
15342 Current_Unit = Cunit (Main_Unit)
15343 or else Current_Unit = Library_Unit (Cunit (Main_Unit))
15344 or else (Present (Current_Unit)
15345 and then Present (Library_Unit (Current_Unit))
15346 and then Is_In_Main_Unit (Library_Unit (Current_Unit)));
15347 end Is_In_Main_Unit;
15349 ----------------------------
15350 -- Load_Parent_Of_Generic --
15351 ----------------------------
15353 procedure Load_Parent_Of_Generic
15356 Body_Optional : Boolean := False)
15358 Comp_Unit : constant Node_Id := Cunit (Get_Source_Unit (Spec));
15359 Saved_Style_Check : constant Boolean := Style_Check;
15360 Saved_Warn : constant Warnings_State := Save_Warnings;
15361 True_Parent : Node_Id;
15362 Inst_Node : Node_Id;
15364 Previous_Instances : constant Elist_Id := New_Elmt_List;
15366 procedure Collect_Previous_Instances (Decls : List_Id);
15367 -- Collect all instantiations in the given list of declarations, that
15368 -- precede the generic that we need to load. If the bodies of these
15369 -- instantiations are available, we must analyze them, to ensure that
15370 -- the public symbols generated are the same when the unit is compiled
15371 -- to generate code, and when it is compiled in the context of a unit
15372 -- that needs a particular nested instance. This process is applied to
15373 -- both package and subprogram instances.
15375 --------------------------------
15376 -- Collect_Previous_Instances --
15377 --------------------------------
15379 procedure Collect_Previous_Instances (Decls : List_Id) is
15383 Decl := First (Decls);
15384 while Present (Decl) loop
15385 if Sloc (Decl) >= Sloc (Inst_Node) then
15388 -- If Decl is an instantiation, then record it as requiring
15389 -- instantiation of the corresponding body, except if it is an
15390 -- abbreviated instantiation generated internally for conformance
15391 -- checking purposes only for the case of a formal package
15392 -- declared without a box (see Instantiate_Formal_Package). Such
15393 -- an instantiation does not generate any code (the actual code
15394 -- comes from actual) and thus does not need to be analyzed here.
15395 -- If the instantiation appears with a generic package body it is
15396 -- not analyzed here either.
15398 elsif Nkind (Decl) = N_Package_Instantiation
15399 and then not Is_Abbreviated_Instance (Defining_Entity (Decl))
15401 Append_Elmt (Decl, Previous_Instances);
15403 -- For a subprogram instantiation, omit instantiations intrinsic
15404 -- operations (Unchecked_Conversions, etc.) that have no bodies.
15406 elsif Nkind (Decl) in N_Function_Instantiation
15407 | N_Procedure_Instantiation
15408 and then not Is_Intrinsic_Subprogram (Entity (Name (Decl)))
15410 Append_Elmt (Decl, Previous_Instances);
15412 elsif Nkind (Decl) = N_Package_Declaration then
15413 Collect_Previous_Instances
15414 (Visible_Declarations (Specification (Decl)));
15415 Collect_Previous_Instances
15416 (Private_Declarations (Specification (Decl)));
15418 -- Previous non-generic bodies may contain instances as well
15420 elsif Nkind (Decl) = N_Package_Body
15421 and then Ekind (Corresponding_Spec (Decl)) /= E_Generic_Package
15423 Collect_Previous_Instances (Declarations (Decl));
15425 elsif Nkind (Decl) = N_Subprogram_Body
15426 and then not Acts_As_Spec (Decl)
15427 and then not Is_Generic_Subprogram (Corresponding_Spec (Decl))
15429 Collect_Previous_Instances (Declarations (Decl));
15434 end Collect_Previous_Instances;
15436 -- Start of processing for Load_Parent_Of_Generic
15439 if not In_Same_Source_Unit (N, Spec)
15440 or else Nkind (Unit (Comp_Unit)) = N_Package_Declaration
15441 or else (Nkind (Unit (Comp_Unit)) = N_Package_Body
15442 and then not Is_In_Main_Unit (Spec))
15444 -- Find body of parent of spec, and analyze it. A special case arises
15445 -- when the parent is an instantiation, that is to say when we are
15446 -- currently instantiating a nested generic. In that case, there is
15447 -- no separate file for the body of the enclosing instance. Instead,
15448 -- the enclosing body must be instantiated as if it were a pending
15449 -- instantiation, in order to produce the body for the nested generic
15450 -- we require now. Note that in that case the generic may be defined
15451 -- in a package body, the instance defined in the same package body,
15452 -- and the original enclosing body may not be in the main unit.
15454 Inst_Node := Empty;
15456 True_Parent := Parent (Spec);
15457 while Present (True_Parent)
15458 and then Nkind (True_Parent) /= N_Compilation_Unit
15460 if Nkind (True_Parent) = N_Package_Declaration
15462 Nkind (Original_Node (True_Parent)) = N_Package_Instantiation
15464 -- Parent is a compilation unit that is an instantiation, and
15465 -- instantiation node has been replaced with package decl.
15467 Inst_Node := Original_Node (True_Parent);
15470 elsif Nkind (True_Parent) = N_Package_Declaration
15471 and then Nkind (Parent (True_Parent)) = N_Compilation_Unit
15473 Nkind (Unit (Parent (True_Parent))) = N_Package_Instantiation
15475 -- Parent is a compilation unit that is an instantiation, but
15476 -- instantiation node has not been replaced with package decl.
15478 Inst_Node := Unit (Parent (True_Parent));
15481 elsif Nkind (True_Parent) = N_Package_Declaration
15482 and then Nkind (Parent (True_Parent)) /= N_Compilation_Unit
15483 and then Present (Generic_Parent (Specification (True_Parent)))
15485 -- Parent is an instantiation within another specification.
15486 -- Declaration for instance has been inserted before original
15487 -- instantiation node. A direct link would be preferable?
15489 Inst_Node := Next (True_Parent);
15490 while Present (Inst_Node)
15491 and then Nkind (Inst_Node) /= N_Package_Instantiation
15496 -- If the instance appears within a generic, and the generic
15497 -- unit is defined within a formal package of the enclosing
15498 -- generic, there is no generic body available, and none
15499 -- needed. A more precise test should be used ???
15501 if No (Inst_Node) then
15507 -- If an ancestor of the generic comes from a formal package
15508 -- there is no source for the ancestor body. This is detected
15509 -- by examining the scope of the ancestor and its declaration.
15510 -- The body, if any is needed, will be available when the
15511 -- current unit (containing a formal package) is instantiated.
15513 elsif Nkind (True_Parent) = N_Package_Specification
15514 and then Present (Generic_Parent (True_Parent))
15516 (Original_Node (Unit_Declaration_Node
15517 (Scope (Generic_Parent (True_Parent)))))
15518 = N_Formal_Package_Declaration
15523 True_Parent := Parent (True_Parent);
15527 -- Case where we are currently instantiating a nested generic
15529 if Present (Inst_Node) then
15530 if Nkind (Parent (True_Parent)) = N_Compilation_Unit then
15532 -- Instantiation node and declaration of instantiated package
15533 -- were exchanged when only the declaration was needed.
15534 -- Restore instantiation node before proceeding with body.
15536 Set_Unit (Parent (True_Parent), Inst_Node);
15539 -- Now complete instantiation of enclosing body, if it appears in
15540 -- some other unit. If it appears in the current unit, the body
15541 -- will have been instantiated already.
15543 if No (Corresponding_Body (Instance_Spec (Inst_Node))) then
15545 -- We need to determine the expander mode to instantiate the
15546 -- enclosing body. Because the generic body we need may use
15547 -- global entities declared in the enclosing package (including
15548 -- aggregates) it is in general necessary to compile this body
15549 -- with expansion enabled, except if we are within a generic
15550 -- package, in which case the usual generic rule applies.
15553 Exp_Status : Boolean := True;
15557 -- Loop through scopes looking for generic package
15559 Scop := Scope (Defining_Entity (Instance_Spec (Inst_Node)));
15560 while Present (Scop)
15561 and then Scop /= Standard_Standard
15563 if Ekind (Scop) = E_Generic_Package then
15564 Exp_Status := False;
15568 Scop := Scope (Scop);
15571 -- Collect previous instantiations in the unit that contains
15572 -- the desired generic.
15574 if Nkind (Parent (True_Parent)) /= N_Compilation_Unit
15575 and then not Body_Optional
15579 Info : Pending_Body_Info;
15583 Par := Parent (Inst_Node);
15584 while Present (Par) loop
15585 exit when Nkind (Parent (Par)) = N_Compilation_Unit;
15586 Par := Parent (Par);
15589 pragma Assert (Present (Par));
15591 if Nkind (Par) = N_Package_Body then
15592 Collect_Previous_Instances (Declarations (Par));
15594 elsif Nkind (Par) = N_Package_Declaration then
15595 Collect_Previous_Instances
15596 (Visible_Declarations (Specification (Par)));
15597 Collect_Previous_Instances
15598 (Private_Declarations (Specification (Par)));
15601 -- Enclosing unit is a subprogram body. In this
15602 -- case all instance bodies are processed in order
15603 -- and there is no need to collect them separately.
15608 Decl := First_Elmt (Previous_Instances);
15609 while Present (Decl) loop
15611 (Inst_Node => Node (Decl),
15613 Instance_Spec (Node (Decl)),
15615 Config_Switches => Save_Config_Switches,
15616 Current_Sem_Unit =>
15617 Get_Code_Unit (Sloc (Node (Decl))),
15618 Expander_Status => Exp_Status,
15619 Local_Suppress_Stack_Top =>
15620 Local_Suppress_Stack_Top,
15621 Scope_Suppress => Scope_Suppress,
15622 Warnings => Save_Warnings);
15624 -- Package instance
15626 if Nkind (Node (Decl)) = N_Package_Instantiation
15628 Instantiate_Package_Body
15629 (Info, Body_Optional => True);
15631 -- Subprogram instance
15634 -- The instance_spec is in the wrapper package,
15635 -- usually followed by its local renaming
15636 -- declaration. See Build_Subprogram_Renaming
15637 -- for details. If the instance carries aspects,
15638 -- these result in the corresponding pragmas,
15639 -- inserted after the subprogram declaration.
15640 -- They must be skipped as well when retrieving
15641 -- the desired spec. Some of them may have been
15642 -- rewritten as null statements.
15643 -- A direct link would be more robust ???
15647 (Last (Visible_Declarations
15648 (Specification (Info.Act_Decl))));
15650 while Nkind (Decl) in
15653 N_Subprogram_Renaming_Declaration
15655 Decl := Prev (Decl);
15658 Info.Act_Decl := Decl;
15661 Instantiate_Subprogram_Body
15662 (Info, Body_Optional => True);
15670 Instantiate_Package_Body
15672 ((Inst_Node => Inst_Node,
15673 Act_Decl => True_Parent,
15675 Config_Switches => Save_Config_Switches,
15676 Current_Sem_Unit =>
15677 Get_Code_Unit (Sloc (Inst_Node)),
15678 Expander_Status => Exp_Status,
15679 Local_Suppress_Stack_Top => Local_Suppress_Stack_Top,
15680 Scope_Suppress => Scope_Suppress,
15681 Warnings => Save_Warnings)),
15682 Body_Optional => Body_Optional);
15686 -- Case where we are not instantiating a nested generic
15689 Opt.Style_Check := False;
15690 Expander_Mode_Save_And_Set (True);
15691 Load_Needed_Body (Comp_Unit, OK);
15692 Opt.Style_Check := Saved_Style_Check;
15693 Restore_Warnings (Saved_Warn);
15694 Expander_Mode_Restore;
15697 and then Unit_Requires_Body (Defining_Entity (Spec))
15698 and then not Body_Optional
15701 Bname : constant Unit_Name_Type :=
15702 Get_Body_Name (Get_Unit_Name (Unit (Comp_Unit)));
15705 -- In CodePeer mode, the missing body may make the analysis
15706 -- incomplete, but we do not treat it as fatal.
15708 if CodePeer_Mode then
15712 Error_Msg_Unit_1 := Bname;
15713 Error_Msg_N ("this instantiation requires$!", N);
15714 Error_Msg_File_1 :=
15715 Get_File_Name (Bname, Subunit => False);
15716 Error_Msg_N ("\but file{ was not found!", N);
15717 raise Unrecoverable_Error;
15724 -- If loading parent of the generic caused an instantiation circularity,
15725 -- we abandon compilation at this point, because otherwise in some cases
15726 -- we get into trouble with infinite recursions after this point.
15728 if Circularity_Detected then
15729 raise Unrecoverable_Error;
15731 end Load_Parent_Of_Generic;
15733 ---------------------------------
15734 -- Map_Formal_Package_Entities --
15735 ---------------------------------
15737 procedure Map_Formal_Package_Entities (Form : Entity_Id; Act : Entity_Id) is
15742 Set_Instance_Of (Form, Act);
15744 -- Traverse formal and actual package to map the corresponding entities.
15745 -- We skip over internal entities that may be generated during semantic
15746 -- analysis, and find the matching entities by name, given that they
15747 -- must appear in the same order.
15749 E1 := First_Entity (Form);
15750 E2 := First_Entity (Act);
15751 while Present (E1) and then E1 /= First_Private_Entity (Form) loop
15752 -- Could this test be a single condition??? Seems like it could, and
15753 -- isn't FPE (Form) a constant anyway???
15755 if not Is_Internal (E1)
15756 and then Present (Parent (E1))
15757 and then not Is_Class_Wide_Type (E1)
15758 and then not Is_Internal_Name (Chars (E1))
15760 while Present (E2) and then Chars (E2) /= Chars (E1) loop
15767 Set_Instance_Of (E1, E2);
15769 if Is_Type (E1) and then Is_Tagged_Type (E2) then
15770 Set_Instance_Of (Class_Wide_Type (E1), Class_Wide_Type (E2));
15773 if Is_Constrained (E1) then
15774 Set_Instance_Of (Base_Type (E1), Base_Type (E2));
15777 if Ekind (E1) = E_Package and then No (Renamed_Entity (E1)) then
15778 Map_Formal_Package_Entities (E1, E2);
15785 end Map_Formal_Package_Entities;
15787 -----------------------
15788 -- Move_Freeze_Nodes --
15789 -----------------------
15791 procedure Move_Freeze_Nodes
15792 (Out_Of : Entity_Id;
15797 Next_Decl : Node_Id;
15798 Next_Node : Node_Id := After;
15801 function Is_Outer_Type (T : Entity_Id) return Boolean;
15802 -- Check whether entity is declared in a scope external to that of the
15805 -------------------
15806 -- Is_Outer_Type --
15807 -------------------
15809 function Is_Outer_Type (T : Entity_Id) return Boolean is
15810 Scop : Entity_Id := Scope (T);
15813 if Scope_Depth (Scop) < Scope_Depth (Out_Of) then
15817 while Scop /= Standard_Standard loop
15818 if Scop = Out_Of then
15821 Scop := Scope (Scop);
15829 -- Start of processing for Move_Freeze_Nodes
15836 -- First remove the freeze nodes that may appear before all other
15840 while Present (Decl)
15841 and then Nkind (Decl) = N_Freeze_Entity
15842 and then Is_Outer_Type (Entity (Decl))
15844 Decl := Remove_Head (L);
15845 Insert_After (Next_Node, Decl);
15846 Set_Analyzed (Decl, False);
15851 -- Next scan the list of declarations and remove each freeze node that
15852 -- appears ahead of the current node.
15854 while Present (Decl) loop
15855 while Present (Next (Decl))
15856 and then Nkind (Next (Decl)) = N_Freeze_Entity
15857 and then Is_Outer_Type (Entity (Next (Decl)))
15859 Next_Decl := Remove_Next (Decl);
15860 Insert_After (Next_Node, Next_Decl);
15861 Set_Analyzed (Next_Decl, False);
15862 Next_Node := Next_Decl;
15865 -- If the declaration is a nested package or concurrent type, then
15866 -- recurse. Nested generic packages will have been processed from the
15869 case Nkind (Decl) is
15870 when N_Package_Declaration =>
15871 Spec := Specification (Decl);
15873 when N_Task_Type_Declaration =>
15874 Spec := Task_Definition (Decl);
15876 when N_Protected_Type_Declaration =>
15877 Spec := Protected_Definition (Decl);
15883 if Present (Spec) then
15884 Move_Freeze_Nodes (Out_Of, Next_Node, Visible_Declarations (Spec));
15885 Move_Freeze_Nodes (Out_Of, Next_Node, Private_Declarations (Spec));
15890 end Move_Freeze_Nodes;
15896 function Next_Assoc (E : Assoc_Ptr) return Assoc_Ptr is
15898 return Generic_Renamings.Table (E).Next_In_HTable;
15901 ------------------------
15902 -- Preanalyze_Actuals --
15903 ------------------------
15905 procedure Preanalyze_Actuals (N : Node_Id; Inst : Entity_Id := Empty) is
15906 procedure Perform_Appropriate_Analysis (N : Node_Id);
15907 -- Determine if the actuals we are analyzing come from a generic
15908 -- instantiation that is a library unit and dispatch accordingly.
15910 ----------------------------------
15911 -- Perform_Appropriate_Analysis --
15912 ----------------------------------
15914 procedure Perform_Appropriate_Analysis (N : Node_Id) is
15916 -- When we have a library instantiation we cannot allow any expansion
15917 -- to occur, since there may be no place to put it. Instead, in that
15918 -- case we perform a preanalysis of the actual.
15920 if Present (Inst) and then Is_Compilation_Unit (Inst) then
15925 end Perform_Appropriate_Analysis;
15929 Errs : constant Nat := Serious_Errors_Detected;
15934 Cur : Entity_Id := Empty;
15935 -- Current homograph of the instance name
15937 Vis : Boolean := False;
15938 -- Saved visibility status of the current homograph
15940 -- Start of processing for Preanalyze_Actuals
15943 Assoc := First (Generic_Associations (N));
15945 -- If the instance is a child unit, its name may hide an outer homonym,
15946 -- so make it invisible to perform name resolution on the actuals.
15948 if Nkind (Defining_Unit_Name (N)) = N_Defining_Program_Unit_Name
15950 (Current_Entity (Defining_Identifier (Defining_Unit_Name (N))))
15952 Cur := Current_Entity (Defining_Identifier (Defining_Unit_Name (N)));
15954 if Is_Compilation_Unit (Cur) then
15955 Vis := Is_Immediately_Visible (Cur);
15956 Set_Is_Immediately_Visible (Cur, False);
15962 while Present (Assoc) loop
15963 if Nkind (Assoc) /= N_Others_Choice then
15964 Act := Explicit_Generic_Actual_Parameter (Assoc);
15966 -- Within a nested instantiation, a defaulted actual is an empty
15967 -- association, so nothing to analyze. If the subprogram actual
15968 -- is an attribute, analyze prefix only, because actual is not a
15969 -- complete attribute reference.
15971 -- If actual is an allocator, analyze expression only. The full
15972 -- analysis can generate code, and if instance is a compilation
15973 -- unit we have to wait until the package instance is installed
15974 -- to have a proper place to insert this code.
15976 -- String literals may be operators, but at this point we do not
15977 -- know whether the actual is a formal subprogram or a string.
15982 elsif Nkind (Act) = N_Attribute_Reference then
15983 Perform_Appropriate_Analysis (Prefix (Act));
15985 elsif Nkind (Act) = N_Explicit_Dereference then
15986 Perform_Appropriate_Analysis (Prefix (Act));
15988 elsif Nkind (Act) = N_Allocator then
15990 Expr : constant Node_Id := Expression (Act);
15993 if Nkind (Expr) = N_Subtype_Indication then
15994 Perform_Appropriate_Analysis (Subtype_Mark (Expr));
15996 -- Analyze separately each discriminant constraint, when
15997 -- given with a named association.
16003 Constr := First (Constraints (Constraint (Expr)));
16004 while Present (Constr) loop
16005 if Nkind (Constr) = N_Discriminant_Association then
16006 Perform_Appropriate_Analysis
16007 (Expression (Constr));
16009 Perform_Appropriate_Analysis (Constr);
16017 Perform_Appropriate_Analysis (Expr);
16021 elsif Nkind (Act) /= N_Operator_Symbol then
16022 Perform_Appropriate_Analysis (Act);
16024 -- Within a package instance, mark actuals that are limited
16025 -- views, so their use can be moved to the body of the
16028 if Is_Entity_Name (Act)
16029 and then Is_Type (Entity (Act))
16030 and then From_Limited_With (Entity (Act))
16031 and then Present (Inst)
16033 Append_Elmt (Entity (Act), Incomplete_Actuals (Inst));
16037 if Errs /= Serious_Errors_Detected then
16039 -- Do a minimal analysis of the generic, to prevent spurious
16040 -- warnings complaining about the generic being unreferenced,
16041 -- before abandoning the instantiation.
16043 Perform_Appropriate_Analysis (Name (N));
16045 if Is_Entity_Name (Name (N))
16046 and then Etype (Name (N)) /= Any_Type
16048 Generate_Reference (Entity (Name (N)), Name (N));
16049 Set_Is_Instantiated (Entity (Name (N)));
16052 if Present (Cur) then
16054 -- For the case of a child instance hiding an outer homonym,
16055 -- provide additional warning which might explain the error.
16057 Set_Is_Immediately_Visible (Cur, Vis);
16059 ("& hides outer unit with the same name??",
16060 N, Defining_Unit_Name (N));
16063 Abandon_Instantiation (Act);
16070 if Present (Cur) then
16071 Set_Is_Immediately_Visible (Cur, Vis);
16073 end Preanalyze_Actuals;
16075 -------------------------------
16076 -- Provide_Completing_Bodies --
16077 -------------------------------
16079 procedure Provide_Completing_Bodies (N : Node_Id) is
16080 procedure Build_Completing_Body (Subp_Decl : Node_Id);
16081 -- Generate the completing body for subprogram declaration Subp_Decl
16083 procedure Provide_Completing_Bodies_In (Decls : List_Id);
16084 -- Generating completing bodies for all subprograms found in declarative
16087 ---------------------------
16088 -- Build_Completing_Body --
16089 ---------------------------
16091 procedure Build_Completing_Body (Subp_Decl : Node_Id) is
16092 Loc : constant Source_Ptr := Sloc (Subp_Decl);
16093 Subp_Id : constant Entity_Id := Defining_Entity (Subp_Decl);
16097 -- Nothing to do if the subprogram already has a completing body
16099 if Present (Corresponding_Body (Subp_Decl)) then
16102 -- Mark the function as having a valid return statement even though
16103 -- the body contains a single raise statement.
16105 elsif Ekind (Subp_Id) = E_Function then
16106 Set_Return_Present (Subp_Id);
16109 -- Clone the specification to obtain new entities and reset the only
16112 Spec := Copy_Subprogram_Spec (Specification (Subp_Decl));
16113 Set_Generic_Parent (Spec, Empty);
16116 -- function Func ... return ... is
16118 -- procedure Proc ... is
16120 -- raise Program_Error with "access before elaboration";
16123 Insert_After_And_Analyze (Subp_Decl,
16124 Make_Subprogram_Body (Loc,
16125 Specification => Spec,
16126 Declarations => New_List,
16127 Handled_Statement_Sequence =>
16128 Make_Handled_Sequence_Of_Statements (Loc,
16129 Statements => New_List (
16130 Make_Raise_Program_Error (Loc,
16131 Reason => PE_Access_Before_Elaboration)))));
16132 end Build_Completing_Body;
16134 ----------------------------------
16135 -- Provide_Completing_Bodies_In --
16136 ----------------------------------
16138 procedure Provide_Completing_Bodies_In (Decls : List_Id) is
16142 if Present (Decls) then
16143 Decl := First (Decls);
16144 while Present (Decl) loop
16145 Provide_Completing_Bodies (Decl);
16149 end Provide_Completing_Bodies_In;
16155 -- Start of processing for Provide_Completing_Bodies
16158 if Nkind (N) = N_Package_Declaration then
16159 Spec := Specification (N);
16161 Push_Scope (Defining_Entity (N));
16162 Provide_Completing_Bodies_In (Visible_Declarations (Spec));
16163 Provide_Completing_Bodies_In (Private_Declarations (Spec));
16166 elsif Nkind (N) = N_Subprogram_Declaration then
16167 Build_Completing_Body (N);
16169 end Provide_Completing_Bodies;
16171 -------------------
16172 -- Remove_Parent --
16173 -------------------
16175 procedure Remove_Parent (In_Body : Boolean := False) is
16176 S : Entity_Id := Current_Scope;
16177 -- S is the scope containing the instantiation just completed. The scope
16178 -- stack contains the parent instances of the instantiation, followed by
16187 -- After child instantiation is complete, remove from scope stack the
16188 -- extra copy of the current scope, and then remove parent instances.
16190 if not In_Body then
16193 while Current_Scope /= S loop
16194 P := Current_Scope;
16195 End_Package_Scope (Current_Scope);
16197 if In_Open_Scopes (P) then
16198 E := First_Entity (P);
16199 while Present (E) loop
16200 Set_Is_Immediately_Visible (E, True);
16204 -- If instantiation is declared in a block, it is the enclosing
16205 -- scope that might be a parent instance. Note that only one
16206 -- block can be involved, because the parent instances have
16207 -- been installed within it.
16209 if Ekind (P) = E_Block then
16210 Cur_P := Scope (P);
16215 if Is_Generic_Instance (Cur_P) and then P /= Current_Scope then
16216 -- We are within an instance of some sibling. Retain
16217 -- visibility of parent, for proper subsequent cleanup, and
16218 -- reinstall private declarations as well.
16220 Set_In_Private_Part (P);
16221 Install_Private_Declarations (P);
16224 -- If the ultimate parent is a top-level unit recorded in
16225 -- Instance_Parent_Unit, then reset its visibility to what it was
16226 -- before instantiation. (It's not clear what the purpose is of
16227 -- testing whether Scope (P) is In_Open_Scopes, but that test was
16228 -- present before the ultimate parent test was added.???)
16230 elsif not In_Open_Scopes (Scope (P))
16231 or else (P = Instance_Parent_Unit
16232 and then not Parent_Unit_Visible)
16234 Set_Is_Immediately_Visible (P, False);
16236 -- If the current scope is itself an instantiation of a generic
16237 -- nested within P, and we are in the private part of body of this
16238 -- instantiation, restore the full views of P, that were removed
16239 -- in End_Package_Scope above. This obscure case can occur when a
16240 -- subunit of a generic contains an instance of a child unit of
16241 -- its generic parent unit.
16243 elsif S = Current_Scope and then Is_Generic_Instance (S)
16244 and then (In_Package_Body (S) or else In_Private_Part (S))
16247 Par : constant Entity_Id :=
16248 Generic_Parent (Package_Specification (S));
16251 and then P = Scope (Par)
16253 Set_In_Private_Part (P);
16254 Install_Private_Declarations (P);
16260 -- Reset visibility of entities in the enclosing scope
16262 Set_Is_Hidden_Open_Scope (Current_Scope, False);
16264 Hidden := First_Elmt (Hidden_Entities);
16265 while Present (Hidden) loop
16266 Set_Is_Immediately_Visible (Node (Hidden), True);
16267 Next_Elmt (Hidden);
16271 -- Each body is analyzed separately, and there is no context that
16272 -- needs preserving from one body instance to the next, so remove all
16273 -- parent scopes that have been installed.
16275 while Present (S) loop
16276 End_Package_Scope (S);
16277 Set_Is_Immediately_Visible (S, False);
16278 S := Current_Scope;
16279 exit when S = Standard_Standard;
16284 -----------------------------------
16285 -- Requires_Conformance_Checking --
16286 -----------------------------------
16288 function Requires_Conformance_Checking (N : Node_Id) return Boolean is
16290 -- No conformance checking required if the generic actual part is empty,
16291 -- or is a box or an others_clause (necessarily with a box).
16293 return Present (Generic_Associations (N))
16294 and then not Box_Present (N)
16295 and then Nkind (First (Generic_Associations (N))) /= N_Others_Choice;
16296 end Requires_Conformance_Checking;
16302 procedure Restore_Env is
16303 Saved : Instance_Env renames Instance_Envs.Table (Instance_Envs.Last);
16306 if No (Current_Instantiated_Parent.Act_Id) then
16307 -- Restore environment after subprogram inlining
16309 Restore_Private_Views (Empty);
16312 Current_Instantiated_Parent := Saved.Instantiated_Parent;
16313 Exchanged_Views := Saved.Exchanged_Views;
16314 Hidden_Entities := Saved.Hidden_Entities;
16315 Current_Sem_Unit := Saved.Current_Sem_Unit;
16316 Parent_Unit_Visible := Saved.Parent_Unit_Visible;
16317 Instance_Parent_Unit := Saved.Instance_Parent_Unit;
16319 Restore_Config_Switches (Saved.Switches);
16321 Instance_Envs.Decrement_Last;
16324 ---------------------------
16325 -- Restore_Private_Views --
16326 ---------------------------
16328 procedure Restore_Private_Views
16329 (Pack_Id : Entity_Id;
16330 Is_Package : Boolean := True)
16335 Dep_Elmt : Elmt_Id;
16338 procedure Restore_Nested_Formal (Formal : Entity_Id);
16339 -- Hide the generic formals of formal packages declared with box which
16340 -- were reachable in the current instantiation.
16342 ---------------------------
16343 -- Restore_Nested_Formal --
16344 ---------------------------
16346 procedure Restore_Nested_Formal (Formal : Entity_Id) is
16347 pragma Assert (Ekind (Formal) = E_Package);
16350 if Present (Renamed_Entity (Formal))
16351 and then Denotes_Formal_Package (Renamed_Entity (Formal), True)
16355 elsif Present (Associated_Formal_Package (Formal)) then
16356 Ent := First_Entity (Formal);
16357 while Present (Ent) loop
16358 exit when Ekind (Ent) = E_Package
16359 and then Renamed_Entity (Ent) = Renamed_Entity (Formal);
16361 Set_Is_Hidden (Ent);
16362 Set_Is_Potentially_Use_Visible (Ent, False);
16364 -- If package, then recurse
16366 if Ekind (Ent) = E_Package then
16367 Restore_Nested_Formal (Ent);
16373 end Restore_Nested_Formal;
16375 -- Start of processing for Restore_Private_Views
16378 M := First_Elmt (Exchanged_Views);
16379 while Present (M) loop
16382 -- Subtypes of types whose views have been exchanged, and that are
16383 -- defined within the instance, were not on the Private_Dependents
16384 -- list on entry to the instance, so they have to be exchanged
16385 -- explicitly now, in order to remain consistent with the view of the
16388 if Ekind (Typ) in E_Private_Type
16389 | E_Limited_Private_Type
16390 | E_Record_Type_With_Private
16392 Dep_Elmt := First_Elmt (Private_Dependents (Typ));
16393 while Present (Dep_Elmt) loop
16394 Dep_Typ := Node (Dep_Elmt);
16396 if Scope (Dep_Typ) = Pack_Id
16397 and then Present (Full_View (Dep_Typ))
16399 Replace_Elmt (Dep_Elmt, Full_View (Dep_Typ));
16400 Exchange_Declarations (Dep_Typ);
16403 Next_Elmt (Dep_Elmt);
16407 Exchange_Declarations (Typ);
16411 if No (Pack_Id) then
16415 -- Make the generic formal parameters private, and make the formal types
16416 -- into subtypes of the actuals again.
16418 E := First_Entity (Pack_Id);
16419 while Present (E) loop
16420 Set_Is_Hidden (E, True);
16423 and then Nkind (Parent (E)) = N_Subtype_Declaration
16425 -- Always preserve the flag Is_Generic_Actual_Type for GNATprove,
16426 -- as it is needed to identify the subtype with the type it
16427 -- renames, when there are conversions between access types
16430 if GNATprove_Mode then
16433 -- If the actual for E is itself a generic actual type from
16434 -- an enclosing instance, E is still a generic actual type
16435 -- outside of the current instance. This matter when resolving
16436 -- an overloaded call that may be ambiguous in the enclosing
16437 -- instance, when two of its actuals coincide.
16439 elsif Is_Entity_Name (Subtype_Indication (Parent (E)))
16440 and then Is_Generic_Actual_Type
16441 (Entity (Subtype_Indication (Parent (E))))
16445 Set_Is_Generic_Actual_Type (E, False);
16447 -- It might seem reasonable to clear the Is_Generic_Actual_Type
16448 -- flag also on the Full_View if the type is private, since it
16449 -- was set also on this Full_View. However, this flag is relied
16450 -- upon by Covers to spot "types exported from instantiations"
16451 -- which are implicit Full_Views built for instantiations made
16452 -- on private types and we get type mismatches if we do it when
16453 -- the block exchanging the declarations below triggers ???
16455 -- if Is_Private_Type (E) and then Present (Full_View (E)) then
16456 -- Set_Is_Generic_Actual_Type (Full_View (E), False);
16460 -- An unusual case of aliasing: the actual may also be directly
16461 -- visible in the generic, and be private there, while it is fully
16462 -- visible in the context of the instance. The internal subtype
16463 -- is private in the instance but has full visibility like its
16464 -- parent in the enclosing scope. This enforces the invariant that
16465 -- the privacy status of all private dependents of a type coincide
16466 -- with that of the parent type. This can only happen when a
16467 -- generic child unit is instantiated within a sibling.
16469 if Is_Private_Type (E)
16470 and then not Is_Private_Type (Etype (E))
16472 Exchange_Declarations (E);
16475 elsif Ekind (E) = E_Package then
16477 -- The end of the renaming list is the renaming of the generic
16478 -- package itself. If the instance is a subprogram, all entities
16479 -- in the corresponding package are renamings. If this entity is
16480 -- a formal package, make its own formals private as well. The
16481 -- actual in this case is itself the renaming of an instantiation.
16482 -- If the entity is not a package renaming, it is the entity
16483 -- created to validate formal package actuals: ignore it.
16485 -- If the actual is itself a formal package for the enclosing
16486 -- generic, or the actual for such a formal package, it remains
16487 -- visible on exit from the instance, and therefore nothing needs
16488 -- to be done either, except to keep it accessible.
16490 if Is_Package and then Renamed_Entity (E) = Pack_Id then
16493 elsif Nkind (Parent (E)) /= N_Package_Renaming_Declaration then
16497 Denotes_Formal_Package (Renamed_Entity (E), True, Pack_Id)
16499 Set_Is_Hidden (E, False);
16503 Act_P : constant Entity_Id := Renamed_Entity (E);
16507 Id := First_Entity (Act_P);
16509 and then Id /= First_Private_Entity (Act_P)
16511 exit when Ekind (Id) = E_Package
16512 and then Renamed_Entity (Id) = Act_P;
16514 Set_Is_Hidden (Id, True);
16515 Set_Is_Potentially_Use_Visible (Id, In_Use (Act_P));
16517 if Ekind (Id) = E_Package then
16518 Restore_Nested_Formal (Id);
16529 end Restore_Private_Views;
16536 (Gen_Unit : Entity_Id;
16537 Act_Unit : Entity_Id)
16541 Set_Instance_Env (Gen_Unit, Act_Unit);
16544 ----------------------------
16545 -- Save_Global_References --
16546 ----------------------------
16548 procedure Save_Global_References (Templ : Node_Id) is
16550 -- ??? it is horrible to use global variables in highly recursive code
16553 -- The entity of the current associated node
16555 Gen_Scope : Entity_Id;
16556 -- The scope of the generic for which references are being saved
16559 -- The current associated node
16561 function Is_Global (E : Entity_Id) return Boolean;
16562 -- Check whether entity is defined outside of generic unit. Examine the
16563 -- scope of an entity, and the scope of the scope, etc, until we find
16564 -- either Standard, in which case the entity is global, or the generic
16565 -- unit itself, which indicates that the entity is local. If the entity
16566 -- is the generic unit itself, as in the case of a recursive call, or
16567 -- the enclosing generic unit, if different from the current scope, then
16568 -- it is local as well, because it will be replaced at the point of
16569 -- instantiation. On the other hand, if it is a reference to a child
16570 -- unit of a common ancestor, which appears in an instantiation, it is
16571 -- global because it is used to denote a specific compilation unit at
16572 -- the time the instantiations will be analyzed.
16574 procedure Qualify_Universal_Operands
16576 Func_Call : Node_Id);
16577 -- Op denotes a binary or unary operator in generic template Templ. Node
16578 -- Func_Call is the function call alternative of the operator within the
16579 -- the analyzed copy of the template. Change each operand which yields a
16580 -- universal type by wrapping it into a qualified expression
16582 -- Actual_Typ'(Operand)
16584 -- where Actual_Typ is the type of corresponding actual parameter of
16585 -- Operand in Func_Call.
16587 procedure Reset_Entity (N : Node_Id);
16588 -- Save semantic information on global entity so that it is not resolved
16589 -- again at instantiation time.
16591 procedure Save_Entity_Descendants (N : Node_Id);
16592 -- Apply Save_Global_References to the two syntactic descendants of
16593 -- non-terminal nodes that carry an Associated_Node and are processed
16594 -- through Reset_Entity. Once the global entity (if any) has been
16595 -- captured together with its type, only two syntactic descendants need
16596 -- to be traversed to complete the processing of the tree rooted at N.
16597 -- This applies to Selected_Components, Expanded_Names, and to Operator
16598 -- nodes. N can also be a character literal, identifier, or operator
16599 -- symbol node, but the call has no effect in these cases.
16601 procedure Save_Global_Defaults (N1 : Node_Id; N2 : Node_Id);
16602 -- Default actuals in nested instances must be handled specially
16603 -- because there is no link to them from the original tree. When an
16604 -- actual subprogram is given by a default, we add an explicit generic
16605 -- association for it in the instantiation node. When we save the
16606 -- global references on the name of the instance, we recover the list
16607 -- of generic associations, and add an explicit one to the original
16608 -- generic tree, through which a global actual can be preserved.
16609 -- Similarly, if a child unit is instantiated within a sibling, in the
16610 -- context of the parent, we must preserve the identifier of the parent
16611 -- so that it can be properly resolved in a subsequent instantiation.
16613 procedure Save_Global_Descendant (D : Union_Id);
16614 -- Apply Save_References recursively to the descendants of node D
16616 procedure Save_References (N : Node_Id);
16617 -- This is the recursive procedure that does the work, once the
16618 -- enclosing generic scope has been established.
16620 procedure Set_Global_Type (N : Node_Id; N2 : Node_Id);
16621 -- If the type of N2 is global to the generic unit, save the type in
16622 -- the generic node. Just as we perform name capture for explicit
16623 -- references within the generic, we must capture the global types
16624 -- of local entities because they may participate in resolution in
16631 function Is_Global (E : Entity_Id) return Boolean is
16634 function Is_Instance_Node (Decl : Node_Id) return Boolean;
16635 -- Determine whether the parent node of a reference to a child unit
16636 -- denotes an instantiation or a formal package, in which case the
16637 -- reference to the child unit is global, even if it appears within
16638 -- the current scope (e.g. when the instance appears within the body
16639 -- of an ancestor).
16641 ----------------------
16642 -- Is_Instance_Node --
16643 ----------------------
16645 function Is_Instance_Node (Decl : Node_Id) return Boolean is
16647 return Nkind (Decl) in N_Generic_Instantiation
16649 Nkind (Original_Node (Decl)) = N_Formal_Package_Declaration;
16650 end Is_Instance_Node;
16652 -- Start of processing for Is_Global
16655 if E = Gen_Scope then
16658 elsif E = Standard_Standard then
16661 -- E should be an entity, but it is not always
16663 elsif Nkind (E) not in N_Entity then
16666 elsif Nkind (E) /= N_Expanded_Name
16667 and then Is_Child_Unit (E)
16668 and then (Is_Instance_Node (Parent (N2))
16669 or else (Nkind (Parent (N2)) = N_Expanded_Name
16670 and then N2 = Selector_Name (Parent (N2))
16672 Is_Instance_Node (Parent (Parent (N2)))))
16677 -- E may be an expanded name - typically an operator - in which
16678 -- case we must find its enclosing scope since expanded names
16679 -- don't have corresponding scopes.
16681 if Nkind (E) = N_Expanded_Name then
16682 Se := Find_Enclosing_Scope (E);
16684 -- Otherwise, E is an entity and will have Scope set
16690 while Se /= Gen_Scope loop
16691 if Se = Standard_Standard then
16702 --------------------------------
16703 -- Qualify_Universal_Operands --
16704 --------------------------------
16706 procedure Qualify_Universal_Operands
16708 Func_Call : Node_Id)
16710 procedure Qualify_Operand (Opnd : Node_Id; Actual : Node_Id);
16711 -- Rewrite operand Opnd as a qualified expression of the form
16713 -- Actual_Typ'(Opnd)
16715 -- where Actual is the corresponding actual parameter of Opnd in
16716 -- function call Func_Call.
16718 function Qualify_Type
16720 Typ : Entity_Id) return Node_Id;
16721 -- Qualify type Typ by creating a selected component of the form
16723 -- Scope_Of_Typ.Typ
16725 ---------------------
16726 -- Qualify_Operand --
16727 ---------------------
16729 procedure Qualify_Operand (Opnd : Node_Id; Actual : Node_Id) is
16730 Loc : constant Source_Ptr := Sloc (Opnd);
16731 Typ : constant Entity_Id := Etype (Actual);
16736 -- Qualify the operand when it is of a universal type. Note that
16737 -- the template is unanalyzed and it is not possible to directly
16738 -- query the type. This transformation is not done when the type
16739 -- of the actual is internally generated because the type will be
16740 -- regenerated in the instance.
16742 if Yields_Universal_Type (Opnd)
16743 and then Comes_From_Source (Typ)
16744 and then not Is_Hidden (Typ)
16746 -- The type of the actual may be a global reference. Save this
16747 -- information by creating a reference to it.
16749 if Is_Global (Typ) then
16750 Mark := New_Occurrence_Of (Typ, Loc);
16752 -- Otherwise rely on resolution to find the proper type within
16756 Mark := Qualify_Type (Loc, Typ);
16760 Make_Qualified_Expression (Loc,
16761 Subtype_Mark => Mark,
16762 Expression => Relocate_Node (Opnd));
16764 -- Mark the qualification to distinguish it from other source
16765 -- constructs and signal the instantiation mechanism that this
16766 -- node requires special processing. See Copy_Generic_Node for
16769 Set_Is_Qualified_Universal_Literal (Qual);
16771 Rewrite (Opnd, Qual);
16773 end Qualify_Operand;
16779 function Qualify_Type
16781 Typ : Entity_Id) return Node_Id
16783 Scop : constant Entity_Id := Scope (Typ);
16787 Result := Make_Identifier (Loc, Chars (Typ));
16789 if Present (Scop) and then not Is_Generic_Unit (Scop) then
16791 Make_Selected_Component (Loc,
16792 Prefix => Make_Identifier (Loc, Chars (Scop)),
16793 Selector_Name => Result);
16801 Actuals : constant List_Id := Parameter_Associations (Func_Call);
16803 -- Start of processing for Qualify_Universal_Operands
16806 if Nkind (Op) in N_Binary_Op then
16807 Qualify_Operand (Left_Opnd (Op), First (Actuals));
16808 Qualify_Operand (Right_Opnd (Op), Next (First (Actuals)));
16810 elsif Nkind (Op) in N_Unary_Op then
16811 Qualify_Operand (Right_Opnd (Op), First (Actuals));
16813 end Qualify_Universal_Operands;
16819 procedure Reset_Entity (N : Node_Id) is
16820 function Top_Ancestor (E : Entity_Id) return Entity_Id;
16821 -- Find the ultimate ancestor of the current unit. If it is not a
16822 -- generic unit, then the name of the current unit in the prefix of
16823 -- an expanded name must be replaced with its generic homonym to
16824 -- ensure that it will be properly resolved in an instance.
16830 function Top_Ancestor (E : Entity_Id) return Entity_Id is
16835 while Is_Child_Unit (Par) loop
16836 Par := Scope (Par);
16842 -- Start of processing for Reset_Entity
16845 N2 := Get_Associated_Node (N);
16848 if Present (E) then
16850 -- If the node is an entry call to an entry in an enclosing task,
16851 -- it is rewritten as a selected component. No global entity to
16852 -- preserve in this case, since the expansion will be redone in
16855 if Nkind (E) not in N_Entity then
16856 Set_Associated_Node (N, Empty);
16857 Set_Etype (N, Empty);
16861 -- If the entity is an itype created as a subtype of an access
16862 -- type with a null exclusion restore source entity for proper
16863 -- visibility. The itype will be created anew in the instance.
16866 and then Ekind (E) = E_Access_Subtype
16867 and then Is_Entity_Name (N)
16868 and then Chars (Etype (E)) = Chars (N)
16871 Set_Entity (N2, E);
16875 if Is_Global (E) then
16876 Set_Global_Type (N, N2);
16878 elsif Nkind (N) = N_Op_Concat
16879 and then Is_Generic_Type (Etype (N2))
16880 and then (Base_Type (Etype (Right_Opnd (N2))) = Etype (N2)
16882 Base_Type (Etype (Left_Opnd (N2))) = Etype (N2))
16883 and then Is_Intrinsic_Subprogram (E)
16887 -- Entity is local. Mark generic node as unresolved. Note that now
16888 -- it does not have an entity.
16891 Set_Associated_Node (N, Empty);
16892 Set_Etype (N, Empty);
16895 if Nkind (Parent (N)) in N_Generic_Instantiation
16896 and then N = Name (Parent (N))
16898 Save_Global_Defaults (Parent (N), Parent (N2));
16901 elsif Nkind (Parent (N)) = N_Selected_Component
16902 and then Nkind (Parent (N2)) = N_Expanded_Name
16904 -- In case of previous errors, the tree might be malformed
16906 if No (Entity (Parent (N2))) then
16909 elsif Is_Global (Entity (Parent (N2))) then
16910 Change_Selected_Component_To_Expanded_Name (Parent (N));
16911 Set_Associated_Node (Parent (N), Parent (N2));
16912 Set_Global_Type (Parent (N), Parent (N2));
16913 Save_Entity_Descendants (N);
16915 -- If this is a reference to the current generic entity, replace
16916 -- by the name of the generic homonym of the current package. This
16917 -- is because in an instantiation Par.P.Q will not resolve to the
16918 -- name of the instance, whose enclosing scope is not necessarily
16919 -- Par. We use the generic homonym rather that the name of the
16920 -- generic itself because it may be hidden by a local declaration.
16922 elsif In_Open_Scopes (Entity (Parent (N2)))
16924 Is_Generic_Unit (Top_Ancestor (Entity (Prefix (Parent (N2)))))
16926 if Ekind (Entity (Parent (N2))) = E_Generic_Package then
16927 Rewrite (Parent (N),
16928 Make_Identifier (Sloc (N),
16930 Chars (Generic_Homonym (Entity (Parent (N2))))));
16932 Rewrite (Parent (N),
16933 Make_Identifier (Sloc (N),
16934 Chars => Chars (Selector_Name (Parent (N2)))));
16938 if Nkind (Parent (Parent (N))) in N_Generic_Instantiation
16939 and then Parent (N) = Name (Parent (Parent (N)))
16941 Save_Global_Defaults
16942 (Parent (Parent (N)), Parent (Parent (N2)));
16945 -- A selected component may denote a static constant that has been
16946 -- folded. If the static constant is global to the generic, capture
16947 -- its value. Otherwise the folding will happen in any instantiation.
16949 elsif Nkind (Parent (N)) = N_Selected_Component
16950 and then Nkind (Parent (N2)) in N_Integer_Literal | N_Real_Literal
16952 if Present (Entity (Original_Node (Parent (N2))))
16953 and then Is_Global (Entity (Original_Node (Parent (N2))))
16955 Rewrite (Parent (N), New_Copy (Parent (N2)));
16956 Set_Analyzed (Parent (N), False);
16959 -- A selected component may be transformed into a parameterless
16960 -- function call. If the called entity is global, rewrite the node
16961 -- appropriately, i.e. as an extended name for the global entity.
16963 elsif Nkind (Parent (N)) = N_Selected_Component
16964 and then Nkind (Parent (N2)) = N_Function_Call
16965 and then N = Selector_Name (Parent (N))
16967 if No (Parameter_Associations (Parent (N2))) then
16968 if Is_Global (Entity (Name (Parent (N2)))) then
16969 Change_Selected_Component_To_Expanded_Name (Parent (N));
16970 Set_Associated_Node (Parent (N), Name (Parent (N2)));
16971 Set_Global_Type (Parent (N), Name (Parent (N2)));
16972 Save_Entity_Descendants (N);
16975 Set_Is_Prefixed_Call (Parent (N));
16976 Set_Associated_Node (N, Empty);
16977 Set_Etype (N, Empty);
16980 -- In Ada 2005, X.F may be a call to a primitive operation,
16981 -- rewritten as F (X). This rewriting will be done again in an
16982 -- instance, so keep the original node. Global entities will be
16983 -- captured as for other constructs. Indicate that this must
16984 -- resolve as a call, to prevent accidental overloading in the
16985 -- instance, if both a component and a primitive operation appear
16989 Set_Is_Prefixed_Call (Parent (N));
16992 -- Entity is local. Reset in generic unit, so that node is resolved
16993 -- anew at the point of instantiation.
16996 Set_Associated_Node (N, Empty);
16997 Set_Etype (N, Empty);
17001 -----------------------------
17002 -- Save_Entity_Descendants --
17003 -----------------------------
17005 procedure Save_Entity_Descendants (N : Node_Id) is
17008 when N_Binary_Op =>
17009 Save_Global_Descendant (Union_Id (Left_Opnd (N)));
17010 Save_Global_Descendant (Union_Id (Right_Opnd (N)));
17013 Save_Global_Descendant (Union_Id (Right_Opnd (N)));
17015 when N_Expanded_Name
17016 | N_Selected_Component
17018 Save_Global_Descendant (Union_Id (Prefix (N)));
17019 Save_Global_Descendant (Union_Id (Selector_Name (N)));
17021 when N_Character_Literal
17023 | N_Operator_Symbol
17028 raise Program_Error;
17030 end Save_Entity_Descendants;
17032 --------------------------
17033 -- Save_Global_Defaults --
17034 --------------------------
17036 procedure Save_Global_Defaults (N1 : Node_Id; N2 : Node_Id) is
17037 Loc : constant Source_Ptr := Sloc (N1);
17038 Assoc2 : constant List_Id := Generic_Associations (N2);
17039 Gen_Id : constant Entity_Id := Get_Generic_Entity (N2);
17046 Actual : Entity_Id;
17049 Assoc1 := Generic_Associations (N1);
17051 if Present (Assoc1) then
17052 Act1 := First (Assoc1);
17055 Set_Generic_Associations (N1, New_List);
17056 Assoc1 := Generic_Associations (N1);
17059 if Present (Assoc2) then
17060 Act2 := First (Assoc2);
17065 while Present (Act1) and then Present (Act2) loop
17070 -- Find the associations added for default subprograms
17072 if Present (Act2) then
17073 while Nkind (Act2) /= N_Generic_Association
17074 or else No (Entity (Selector_Name (Act2)))
17075 or else not Is_Overloadable (Entity (Selector_Name (Act2)))
17080 -- Add a similar association if the default is global. The
17081 -- renaming declaration for the actual has been analyzed, and
17082 -- its alias is the program it renames. Link the actual in the
17083 -- original generic tree with the node in the analyzed tree.
17085 while Present (Act2) loop
17086 Subp := Entity (Selector_Name (Act2));
17087 Def := Explicit_Generic_Actual_Parameter (Act2);
17089 -- Following test is defence against rubbish errors
17091 if No (Alias (Subp)) then
17095 -- Retrieve the resolved actual from the renaming declaration
17096 -- created for the instantiated formal.
17098 Actual := Entity (Name (Parent (Parent (Subp))));
17099 Set_Entity (Def, Actual);
17100 Set_Etype (Def, Etype (Actual));
17102 if Is_Global (Actual) then
17104 Make_Generic_Association (Loc,
17106 New_Occurrence_Of (Subp, Loc),
17107 Explicit_Generic_Actual_Parameter =>
17108 New_Occurrence_Of (Actual, Loc));
17110 Set_Associated_Node
17111 (Explicit_Generic_Actual_Parameter (Ndec), Def);
17113 Append (Ndec, Assoc1);
17115 -- If there are other defaults, add a dummy association in case
17116 -- there are other defaulted formals with the same name.
17117 -- Note that we are creating an N_Generic_Association with
17118 -- neither Explicit_Generic_Actual_Parameter nor Box_Present.
17120 elsif Present (Next (Act2)) then
17122 Make_Generic_Association (Loc,
17123 Selector_Name => New_Occurrence_Of (Subp, Loc),
17124 Explicit_Generic_Actual_Parameter => Empty);
17126 Append (Ndec, Assoc1);
17133 if Nkind (Name (N1)) = N_Identifier
17134 and then Is_Child_Unit (Gen_Id)
17135 and then Is_Global (Gen_Id)
17136 and then Is_Generic_Unit (Scope (Gen_Id))
17137 and then In_Open_Scopes (Scope (Gen_Id))
17139 -- This is an instantiation of a child unit within a sibling, so
17140 -- that the generic parent is in scope. An eventual instance must
17141 -- occur within the scope of an instance of the parent. Make name
17142 -- in instance into an expanded name, to preserve the identifier
17143 -- of the parent, so it can be resolved subsequently.
17145 Rewrite (Name (N2),
17146 Make_Expanded_Name (Loc,
17147 Chars => Chars (Gen_Id),
17148 Prefix => New_Occurrence_Of (Scope (Gen_Id), Loc),
17149 Selector_Name => New_Occurrence_Of (Gen_Id, Loc)));
17150 Set_Entity (Name (N2), Gen_Id);
17152 Rewrite (Name (N1),
17153 Make_Expanded_Name (Loc,
17154 Chars => Chars (Gen_Id),
17155 Prefix => New_Occurrence_Of (Scope (Gen_Id), Loc),
17156 Selector_Name => New_Occurrence_Of (Gen_Id, Loc)));
17158 Set_Associated_Node (Name (N1), Name (N2));
17159 Set_Associated_Node (Prefix (Name (N1)), Empty);
17160 Set_Associated_Node
17161 (Selector_Name (Name (N1)), Selector_Name (Name (N2)));
17162 Set_Etype (Name (N1), Etype (Gen_Id));
17164 end Save_Global_Defaults;
17166 ----------------------------
17167 -- Save_Global_Descendant --
17168 ----------------------------
17170 procedure Save_Global_Descendant (D : Union_Id) is
17174 if D in Node_Range then
17175 if D = Union_Id (Empty) then
17178 elsif Nkind (Node_Id (D)) /= N_Compilation_Unit then
17179 Save_References (Node_Id (D));
17182 elsif D in List_Range then
17183 pragma Assert (D /= Union_Id (No_List));
17184 -- Because No_List = Empty, which is in Node_Range above
17186 N1 := First (List_Id (D));
17187 while Present (N1) loop
17188 Save_References (N1);
17192 -- Element list or other non-node field, nothing to do
17197 end Save_Global_Descendant;
17199 ---------------------
17200 -- Save_References --
17201 ---------------------
17203 -- This is the recursive procedure that does the work once the enclosing
17204 -- generic scope has been established. We have to treat specially a
17205 -- number of node rewritings that are required by semantic processing
17206 -- and which change the kind of nodes in the generic copy: typically
17207 -- constant-folding, replacing an operator node by a string literal, or
17208 -- a selected component by an expanded name. In each of those cases, the
17209 -- transformation is propagated to the generic unit.
17211 procedure Save_References (N : Node_Id) is
17212 Loc : constant Source_Ptr := Sloc (N);
17214 function Requires_Delayed_Save (Nod : Node_Id) return Boolean;
17215 -- Determine whether arbitrary node Nod requires delayed capture of
17216 -- global references within its aspect specifications.
17218 procedure Save_References_In_Aggregate (N : Node_Id);
17219 -- Save all global references in [extension] aggregate node N
17221 procedure Save_References_In_Char_Lit_Or_Op_Symbol (N : Node_Id);
17222 -- Save all global references in a character literal or operator
17223 -- symbol denoted by N.
17225 procedure Save_References_In_Descendants (N : Node_Id);
17226 -- Save all global references in all descendants of node N
17228 procedure Save_References_In_Identifier (N : Node_Id);
17229 -- Save all global references in identifier node N
17231 procedure Save_References_In_Operator (N : Node_Id);
17232 -- Save all global references in operator node N
17234 procedure Save_References_In_Pragma (Prag : Node_Id);
17235 -- Save all global references found within the expression of pragma
17238 ---------------------------
17239 -- Requires_Delayed_Save --
17240 ---------------------------
17242 function Requires_Delayed_Save (Nod : Node_Id) return Boolean is
17244 -- Generic packages and subprograms require delayed capture of
17245 -- global references within their aspects due to the timing of
17246 -- annotation analysis.
17248 if Nkind (Nod) in N_Generic_Package_Declaration
17249 | N_Generic_Subprogram_Declaration
17251 | N_Package_Body_Stub
17252 | N_Subprogram_Body
17253 | N_Subprogram_Body_Stub
17255 -- Since the capture of global references is done on the
17256 -- unanalyzed generic template, there is no information around
17257 -- to infer the context. Use the Associated_Entity linkages to
17258 -- peek into the analyzed generic copy and determine what the
17259 -- template corresponds to.
17261 if Nod = Templ then
17263 Is_Generic_Declaration_Or_Body
17264 (Unit_Declaration_Node
17265 (Get_Associated_Entity (Defining_Entity (Nod))));
17267 -- Otherwise the generic unit being processed is not the top
17268 -- level template. It is safe to capture of global references
17269 -- within the generic unit because at this point the top level
17270 -- copy is fully analyzed.
17276 -- Otherwise capture the global references without interference
17281 end Requires_Delayed_Save;
17283 ----------------------------------
17284 -- Save_References_In_Aggregate --
17285 ----------------------------------
17287 procedure Save_References_In_Aggregate (N : Node_Id) is
17289 Qual : Node_Id := Empty;
17290 Typ : Entity_Id := Empty;
17293 N2 := Get_Associated_Node (N);
17295 if Present (N2) then
17298 -- In an instance within a generic, use the name of the actual
17299 -- and not the original generic parameter. If the actual is
17300 -- global in the current generic it must be preserved for its
17303 if Parent_Kind (Typ) = N_Subtype_Declaration
17304 and then Present (Generic_Parent_Type (Parent (Typ)))
17306 Typ := Base_Type (Typ);
17307 Set_Etype (N2, Typ);
17311 if No (N2) or else No (Typ) or else not Is_Global (Typ) then
17312 Set_Associated_Node (N, Empty);
17314 -- For a full aggregate, if the type is local but is a derived
17315 -- tagged type of a global ancestor, we will need to have the
17316 -- full view of this global ancestor available in the instance
17317 -- in order to analyze the full aggregate.
17320 and then Nkind (N2) = N_Aggregate
17321 and then Present (Typ)
17322 and then Is_Tagged_Type (Typ)
17323 and then Is_Derived_Type (Typ)
17326 Root_Typ : constant Entity_Id := Root_Type (Typ);
17328 Parent_Typ : Entity_Id := Typ;
17332 Parent_Typ := Etype (Parent_Typ);
17334 if Is_Global (Parent_Typ) then
17335 Set_Ancestor_Type (N, Parent_Typ);
17339 exit when Parent_Typ = Root_Typ;
17344 -- If the aggregate is an actual in a call, it has been
17345 -- resolved in the current context, to some local type. The
17346 -- enclosing call may have been disambiguated by the aggregate,
17347 -- and this disambiguation might fail at instantiation time
17348 -- because the type to which the aggregate did resolve is not
17349 -- preserved. In order to preserve some of this information,
17350 -- wrap the aggregate in a qualified expression, using the id
17351 -- of its type. For further disambiguation we qualify the type
17352 -- name with its scope (if visible and not hidden by a local
17353 -- homograph) because both id's will have corresponding
17354 -- entities in an instance. This resolves most of the problems
17355 -- with missing type information on aggregates in instances.
17358 and then Nkind (N2) = Nkind (N)
17359 and then Nkind (Parent (N2)) in N_Subprogram_Call
17360 and then Present (Typ)
17361 and then Comes_From_Source (Typ)
17363 Nam := Make_Identifier (Loc, Chars (Typ));
17365 if Is_Immediately_Visible (Scope (Typ))
17367 (not In_Open_Scopes (Scope (Typ))
17368 or else Current_Entity (Scope (Typ)) = Scope (Typ))
17371 Make_Selected_Component (Loc,
17373 Make_Identifier (Loc, Chars (Scope (Typ))),
17374 Selector_Name => Nam);
17378 Make_Qualified_Expression (Loc,
17379 Subtype_Mark => Nam,
17380 Expression => Relocate_Node (N));
17383 -- For a full aggregate, if the type is global and a derived
17384 -- tagged type, we will also need to have the full view of its
17385 -- ancestor available in the instance in order to analyze the
17389 and then Nkind (N2) = N_Aggregate
17390 and then Present (Typ)
17391 and then Is_Tagged_Type (Typ)
17392 and then Is_Derived_Type (Typ)
17394 Set_Ancestor_Type (N, Etype (Typ));
17397 if Nkind (N) = N_Aggregate then
17398 Save_Global_Descendant (Union_Id (Aggregate_Bounds (N)));
17400 elsif Nkind (N) = N_Extension_Aggregate then
17401 Save_Global_Descendant (Union_Id (Ancestor_Part (N)));
17404 pragma Assert (False);
17407 Save_Global_Descendant (Union_Id (Expressions (N)));
17408 Save_Global_Descendant (Union_Id (Component_Associations (N)));
17409 Save_Global_Descendant (Union_Id (Etype (N)));
17411 if Present (Qual) then
17414 end Save_References_In_Aggregate;
17416 ----------------------------------------------
17417 -- Save_References_In_Char_Lit_Or_Op_Symbol --
17418 ----------------------------------------------
17420 procedure Save_References_In_Char_Lit_Or_Op_Symbol (N : Node_Id) is
17422 if Nkind (N) = Nkind (Get_Associated_Node (N)) then
17425 elsif Nkind (N) = N_Operator_Symbol
17426 and then Nkind (Get_Associated_Node (N)) = N_String_Literal
17428 Change_Operator_Symbol_To_String_Literal (N);
17430 end Save_References_In_Char_Lit_Or_Op_Symbol;
17432 ------------------------------------
17433 -- Save_References_In_Descendants --
17434 ------------------------------------
17436 procedure Save_References_In_Descendants (N : Node_Id) is
17437 procedure Walk is new Walk_Sinfo_Fields (Save_Global_Descendant);
17440 end Save_References_In_Descendants;
17442 -----------------------------------
17443 -- Save_References_In_Identifier --
17444 -----------------------------------
17446 procedure Save_References_In_Identifier (N : Node_Id) is
17448 -- The node did not undergo a transformation
17450 if Nkind (N) = Nkind (Get_Associated_Node (N)) then
17451 -- If this is a discriminant reference, always save it.
17452 -- It is used in the instance to find the corresponding
17453 -- discriminant positionally rather than by name.
17455 Set_Original_Discriminant
17456 (N, Original_Discriminant (Get_Associated_Node (N)));
17460 -- The analysis of the generic copy transformed the identifier
17461 -- into another construct. Propagate the changes to the template.
17464 N2 := Get_Associated_Node (N);
17466 -- The identifier denotes a call to a parameterless function.
17467 -- Mark the node as resolved when the function is external.
17469 if Nkind (N2) = N_Function_Call then
17470 E := Entity (Name (N2));
17472 if Present (E) and then Is_Global (E) then
17473 Set_Global_Type (N, N2);
17475 Set_Associated_Node (N, Empty);
17476 Set_Etype (N, Empty);
17479 -- The identifier denotes a named number that was constant
17480 -- folded. Preserve the original name for ASIS and undo the
17481 -- constant folding which will be repeated in the instance.
17482 -- Is this still needed???
17484 elsif Nkind (N2) in N_Integer_Literal | N_Real_Literal
17485 and then Is_Entity_Name (Original_Node (N2))
17487 Set_Associated_Node (N, Original_Node (N2));
17490 -- The identifier resolved to a string literal. Propagate this
17491 -- information to the generic template.
17493 elsif Nkind (N2) = N_String_Literal then
17494 Rewrite (N, New_Copy (N2));
17496 -- The identifier is rewritten as a dereference if it is the
17497 -- prefix of an implicit dereference. Preserve the original
17498 -- tree as the analysis of the instance will expand the node
17499 -- again, but preserve the resolved entity if it is global.
17501 elsif Nkind (N2) = N_Explicit_Dereference then
17502 if Is_Entity_Name (Prefix (N2))
17503 and then Present (Entity (Prefix (N2)))
17504 and then Is_Global (Entity (Prefix (N2)))
17506 Set_Associated_Node (N, Prefix (N2));
17507 Set_Global_Type (N, Prefix (N2));
17509 elsif Nkind (Prefix (N2)) = N_Function_Call
17510 and then Is_Entity_Name (Name (Prefix (N2)))
17511 and then Present (Entity (Name (Prefix (N2))))
17512 and then Is_Global (Entity (Name (Prefix (N2))))
17515 Make_Explicit_Dereference (Loc,
17517 Make_Function_Call (Loc,
17520 (Entity (Name (Prefix (N2))), Loc))));
17521 Set_Associated_Node
17522 (Name (Prefix (N)), Name (Prefix (N2)));
17523 Set_Global_Type (Name (Prefix (N)), Name (Prefix (N2)));
17526 Set_Associated_Node (N, Empty);
17527 Set_Etype (N, Empty);
17530 -- The subtype mark of a nominally unconstrained object is
17531 -- rewritten as a subtype indication using the bounds of the
17532 -- expression. Recover the original subtype mark.
17534 elsif Nkind (N2) = N_Subtype_Indication
17535 and then Is_Entity_Name (Original_Node (N2))
17537 Set_Associated_Node (N, Original_Node (N2));
17541 end Save_References_In_Identifier;
17543 ---------------------------------
17544 -- Save_References_In_Operator --
17545 ---------------------------------
17547 procedure Save_References_In_Operator (N : Node_Id) is
17549 N2 := Get_Associated_Node (N);
17551 -- The node did not undergo a transformation
17553 if Nkind (N) = Nkind (N2) then
17554 if Nkind (N) = N_Op_Concat then
17555 Set_Is_Component_Left_Opnd
17556 (N, Is_Component_Left_Opnd (N2));
17557 Set_Is_Component_Right_Opnd
17558 (N, Is_Component_Right_Opnd (N2));
17563 -- The analysis of the generic copy transformed the operator into
17564 -- some other construct. Propagate the changes to the template if
17568 -- The operator resoved to a function call
17570 if Nkind (N2) = N_Function_Call then
17572 -- Add explicit qualifications in the generic template for
17573 -- all operands of universal type. This aids resolution by
17574 -- preserving the actual type of a literal or an attribute
17575 -- that yields a universal result.
17577 Qualify_Universal_Operands (N, N2);
17579 E := Entity (Name (N2));
17581 if Present (E) and then Is_Global (E) then
17582 Set_Global_Type (N, N2);
17584 Set_Associated_Node (N, Empty);
17585 Set_Etype (N, Empty);
17588 -- The operator was folded into a literal
17590 elsif Nkind (N2) in N_Integer_Literal
17594 if Present (Original_Node (N2))
17595 and then Nkind (Original_Node (N2)) = Nkind (N)
17597 -- Operation was constant-folded. Whenever possible,
17598 -- recover semantic information from unfolded node.
17599 -- This was initially done for ASIS but is apparently
17600 -- needed also for e.g. compiling a-nbnbin.adb.
17602 Set_Associated_Node (N, Original_Node (N2));
17604 if Nkind (N) = N_Op_Concat then
17605 Set_Is_Component_Left_Opnd (N,
17606 Is_Component_Left_Opnd (Get_Associated_Node (N)));
17607 Set_Is_Component_Right_Opnd (N,
17608 Is_Component_Right_Opnd (Get_Associated_Node (N)));
17613 -- Propagate the constant folding back to the template
17616 Rewrite (N, New_Copy (N2));
17617 Set_Analyzed (N, False);
17620 -- The operator was folded into an enumeration literal. Retain
17621 -- the entity to avoid spurious ambiguities if it is overloaded
17622 -- at the point of instantiation or inlining.
17624 elsif Nkind (N2) = N_Identifier
17625 and then Ekind (Entity (N2)) = E_Enumeration_Literal
17627 Rewrite (N, New_Copy (N2));
17628 Set_Analyzed (N, False);
17632 -- Complete the operands check if node has not been constant
17635 if Nkind (N) in N_Op then
17636 Save_Entity_Descendants (N);
17638 end Save_References_In_Operator;
17640 -------------------------------
17641 -- Save_References_In_Pragma --
17642 -------------------------------
17644 procedure Save_References_In_Pragma (Prag : Node_Id) is
17646 Do_Save : Boolean := True;
17649 -- Do not save global references in pragmas generated from aspects
17650 -- because the pragmas will be regenerated at instantiation time.
17652 if From_Aspect_Specification (Prag) then
17655 -- The capture of global references within contract-related source
17656 -- pragmas associated with generic packages, subprograms or their
17657 -- respective bodies must be delayed due to timing of annotation
17658 -- analysis. Global references are still captured in routine
17659 -- Save_Global_References_In_Contract.
17661 elsif Is_Generic_Contract_Pragma (Prag) and then Prag /= Templ then
17662 if Is_Package_Contract_Annotation (Prag) then
17663 Context := Find_Related_Package_Or_Body (Prag);
17665 pragma Assert (Is_Subprogram_Contract_Annotation (Prag));
17666 Context := Find_Related_Declaration_Or_Body (Prag);
17669 -- The use of Original_Node accounts for the case when the
17670 -- related context is generic template.
17672 if Requires_Delayed_Save (Original_Node (Context)) then
17677 -- For all other cases, save all global references within the
17678 -- descendants, but skip the following semantic fields:
17679 -- Next_Pragma, Corresponding_Aspect, Next_Rep_Item.
17682 Save_Global_Descendant
17683 (Union_Id (Pragma_Argument_Associations (N)));
17684 Save_Global_Descendant (Union_Id (Pragma_Identifier (N)));
17686 end Save_References_In_Pragma;
17688 -- Start of processing for Save_References
17696 elsif Nkind (N) in N_Aggregate | N_Extension_Aggregate then
17697 Save_References_In_Aggregate (N);
17699 -- Character literals, operator symbols
17701 elsif Nkind (N) in N_Character_Literal | N_Operator_Symbol then
17702 Save_References_In_Char_Lit_Or_Op_Symbol (N);
17704 -- Defining identifiers
17706 elsif Nkind (N) in N_Entity then
17711 elsif Nkind (N) = N_Identifier then
17712 Save_References_In_Identifier (N);
17716 elsif Nkind (N) in N_Op then
17717 Save_References_In_Operator (N);
17721 elsif Nkind (N) = N_Pragma then
17722 Save_References_In_Pragma (N);
17724 elsif Nkind (N) = N_Aspect_Specification then
17726 P : constant Node_Id := Parent (N);
17730 if Permits_Aspect_Specifications (P) then
17732 -- The capture of global references within aspects
17733 -- associated with generic packages, subprograms or
17734 -- their bodies must be delayed due to timing of
17735 -- annotation analysis. Global references are still
17736 -- captured in routine Save_Global_References_In_Contract.
17738 if Requires_Delayed_Save (Original_Node (P)) then
17741 -- Otherwise save all global references within the
17745 Expr := Expression (N);
17747 if Present (Expr) then
17748 Save_Global_References (Expr);
17754 -- Do not walk the node pointed to by Label_Construct twice
17756 elsif Nkind (N) = N_Implicit_Label_Declaration then
17760 Save_References_In_Descendants (N);
17763 end Save_References;
17765 ---------------------
17766 -- Set_Global_Type --
17767 ---------------------
17769 procedure Set_Global_Type (N : Node_Id; N2 : Node_Id) is
17770 Comparison : constant Boolean := Nkind (N2) in N_Op_Compare;
17771 Typ : constant Entity_Id :=
17772 (if Comparison then Compare_Type (N2) else Etype (N2));
17775 -- For a comparison (or equality) operator, the Etype is Boolean, so
17776 -- it is always global. But the type subject to the Has_Private_View
17777 -- processing is the Compare_Type, so we must specifically check it.
17780 Set_Etype (N, Etype (N2));
17782 if not Is_Global (Typ) then
17786 Set_Compare_Type (N, Typ);
17789 Set_Etype (N, Typ);
17792 -- If the entity of N is not the associated node, this is a
17793 -- nested generic and it has an associated node as well, whose
17794 -- type is already the full view (see below). Indicate that the
17795 -- original node has a private view.
17797 if Entity (N) /= N2 then
17798 if Has_Private_View (Entity (N)) then
17799 Set_Has_Private_View (N);
17802 if Has_Secondary_Private_View (Entity (N)) then
17803 Set_Has_Secondary_Private_View (N);
17807 -- If not a private type, deal with a secondary private view
17809 if not Is_Private_Type (Typ) then
17810 if (Is_Access_Type (Typ)
17811 and then Is_Private_Type (Designated_Type (Typ)))
17812 or else (Is_Array_Type (Typ)
17814 Is_Private_Type (Component_Type_For_Private_View (Typ)))
17816 Set_Has_Secondary_Private_View (N);
17819 -- If it is a derivation of a private type in a context where no
17820 -- full view is needed, nothing to do either.
17822 elsif No (Full_View (Typ)) and then Typ /= Etype (Typ) then
17825 -- Otherwise mark the type for flipping and set the full view on N2
17826 -- when available, which is necessary for Check_Private_View to swap
17827 -- back the views in case the full declaration of Typ is visible in
17828 -- the instantiation context. Note that this will be problematic if
17829 -- N2 is re-analyzed later, e.g. if it's a default value in a call.
17832 Set_Has_Private_View (N);
17834 if Present (Full_View (Typ)) then
17836 Set_Compare_Type (N2, Full_View (Typ));
17838 Set_Etype (N2, Full_View (Typ));
17843 if Is_Floating_Point_Type (Typ)
17844 and then Has_Dimension_System (Typ)
17846 Copy_Dimensions (N2, N);
17848 end Set_Global_Type;
17850 -- Start of processing for Save_Global_References
17853 Gen_Scope := Current_Scope;
17855 -- If the generic unit is a child unit, references to entities in the
17856 -- parent are treated as local, because they will be resolved anew in
17857 -- the context of the instance of the parent.
17859 while Is_Child_Unit (Gen_Scope)
17860 and then Ekind (Scope (Gen_Scope)) = E_Generic_Package
17862 Gen_Scope := Scope (Gen_Scope);
17865 Save_References (Templ);
17866 end Save_Global_References;
17868 ---------------------------------------
17869 -- Save_Global_References_In_Aspects --
17870 ---------------------------------------
17872 procedure Save_Global_References_In_Aspects (N : Node_Id) is
17877 Asp := First (Aspect_Specifications (N));
17878 while Present (Asp) loop
17879 Expr := Expression (Asp);
17881 if Present (Expr) then
17882 Save_Global_References (Expr);
17887 end Save_Global_References_In_Aspects;
17889 ------------------------------------------
17890 -- Set_Copied_Sloc_For_Inherited_Pragma --
17891 ------------------------------------------
17893 procedure Set_Copied_Sloc_For_Inherited_Pragma
17898 Create_Instantiation_Source (N, E,
17899 Inlined_Body => False,
17900 Inherited_Pragma => True,
17901 Factor => S_Adjustment);
17902 end Set_Copied_Sloc_For_Inherited_Pragma;
17904 --------------------------------------
17905 -- Set_Copied_Sloc_For_Inlined_Body --
17906 --------------------------------------
17908 procedure Set_Copied_Sloc_For_Inlined_Body (N : Node_Id; E : Entity_Id) is
17910 Create_Instantiation_Source (N, E,
17911 Inlined_Body => True,
17912 Inherited_Pragma => False,
17913 Factor => S_Adjustment);
17914 end Set_Copied_Sloc_For_Inlined_Body;
17916 ---------------------
17917 -- Set_Instance_Of --
17918 ---------------------
17920 procedure Set_Instance_Of (A : Entity_Id; B : Entity_Id) is
17922 Generic_Renamings.Table (Generic_Renamings.Last) := (A, B, Assoc_Null);
17923 Generic_Renamings_HTable.Set (Generic_Renamings.Last);
17924 Generic_Renamings.Increment_Last;
17925 end Set_Instance_Of;
17927 --------------------
17928 -- Set_Next_Assoc --
17929 --------------------
17931 procedure Set_Next_Assoc (E : Assoc_Ptr; Next : Assoc_Ptr) is
17933 Generic_Renamings.Table (E).Next_In_HTable := Next;
17934 end Set_Next_Assoc;
17936 -------------------
17937 -- Start_Generic --
17938 -------------------
17940 procedure Start_Generic is
17942 -- ??? More things could be factored out in this routine.
17943 -- Should probably be done at a later stage.
17945 Generic_Flags.Append (Inside_A_Generic);
17946 Inside_A_Generic := True;
17948 Expander_Mode_Save_And_Set (False);
17951 ----------------------
17952 -- Set_Instance_Env --
17953 ----------------------
17955 -- WARNING: This routine manages SPARK regions
17957 procedure Set_Instance_Env
17958 (Gen_Unit : Entity_Id;
17959 Act_Unit : Entity_Id)
17961 Saved_AE : constant Boolean := Assertions_Enabled;
17962 Saved_CPL : constant Node_Id := Check_Policy_List;
17963 Saved_DEC : constant Boolean := Dynamic_Elaboration_Checks;
17964 Saved_SM : constant SPARK_Mode_Type := SPARK_Mode;
17965 Saved_SMP : constant Node_Id := SPARK_Mode_Pragma;
17968 -- Regardless of the current mode, predefined units are analyzed in the
17969 -- most current Ada mode, and earlier version Ada checks do not apply
17970 -- to predefined units. Nothing needs to be done for non-internal units.
17971 -- These are always analyzed in the current mode.
17973 if In_Internal_Unit (Gen_Unit) then
17975 -- The following call resets all configuration attributes to default
17976 -- or the xxx_Config versions of the attributes when the current sem
17977 -- unit is the main unit. At the same time, internal units must also
17978 -- inherit certain configuration attributes from their context. It
17979 -- is unclear what these two sets are.
17981 Set_Config_Switches (True, Current_Sem_Unit = Main_Unit);
17983 -- Reinstall relevant configuration attributes of the context
17985 Assertions_Enabled := Saved_AE;
17986 Check_Policy_List := Saved_CPL;
17987 Dynamic_Elaboration_Checks := Saved_DEC;
17989 Install_SPARK_Mode (Saved_SM, Saved_SMP);
17992 Current_Instantiated_Parent :=
17993 (Gen_Id => Gen_Unit,
17994 Act_Id => Act_Unit,
17995 Next_In_HTable => Assoc_Null);
17996 end Set_Instance_Env;
18002 procedure Switch_View (T : Entity_Id) is
18003 BT : constant Entity_Id := Base_Type (T);
18004 Priv_Elmt : Elmt_Id := No_Elmt;
18005 Priv_Sub : Entity_Id;
18008 -- T may be private but its base type may have been exchanged through
18009 -- some other occurrence, in which case there is nothing to switch
18010 -- besides T itself. Note that a private dependent subtype of a private
18011 -- type might not have been switched even if the base type has been,
18012 -- because of the last branch of Check_Private_View (see comment there).
18014 if not Is_Private_Type (BT) then
18015 Prepend_Elmt (Full_View (T), Exchanged_Views);
18016 Exchange_Declarations (T);
18020 Priv_Elmt := First_Elmt (Private_Dependents (BT));
18022 if Present (Full_View (BT)) then
18023 Prepend_Elmt (Full_View (BT), Exchanged_Views);
18024 Exchange_Declarations (BT);
18027 while Present (Priv_Elmt) loop
18028 Priv_Sub := Node (Priv_Elmt);
18030 if Present (Full_View (Priv_Sub)) then
18031 Prepend_Elmt (Full_View (Priv_Sub), Exchanged_Views);
18032 Exchange_Declarations (Priv_Sub);
18035 Next_Elmt (Priv_Elmt);
18043 function True_Parent (N : Node_Id) return Node_Id is
18045 if Nkind (Parent (N)) = N_Subunit then
18046 return Parent (Corresponding_Stub (Parent (N)));
18052 -----------------------------
18053 -- Valid_Default_Attribute --
18054 -----------------------------
18056 procedure Valid_Default_Attribute (Nam : Entity_Id; Def : Node_Id) is
18057 Attr_Id : constant Attribute_Id :=
18058 Get_Attribute_Id (Attribute_Name (Def));
18059 T : constant Entity_Id := Entity (Prefix (Def));
18060 Is_Fun : constant Boolean := (Ekind (Nam) = E_Function);
18066 if No (T) or else T = Any_Id then
18071 F := First_Formal (Nam);
18072 while Present (F) loop
18073 Num_F := Num_F + 1;
18078 when Attribute_Adjacent
18079 | Attribute_Ceiling
18080 | Attribute_Copy_Sign
18082 | Attribute_Fraction
18083 | Attribute_Machine
18085 | Attribute_Remainder
18086 | Attribute_Rounding
18087 | Attribute_Unbiased_Rounding
18091 and then Is_Floating_Point_Type (T);
18093 when Attribute_Image
18097 | Attribute_Wide_Image
18098 | Attribute_Wide_Value
18100 OK := Is_Fun and then Num_F = 1 and then Is_Scalar_Type (T);
18105 OK := Is_Fun and then Num_F = 2 and then Is_Scalar_Type (T);
18107 when Attribute_Input =>
18108 OK := (Is_Fun and then Num_F = 1);
18110 when Attribute_Output
18111 | Attribute_Put_Image
18115 OK := not Is_Fun and then Num_F = 2;
18123 ("attribute reference has wrong profile for subprogram", Def);
18125 end Valid_Default_Attribute;
18127 ----------------------------------
18128 -- Validate_Formal_Type_Default --
18129 ----------------------------------
18131 procedure Validate_Formal_Type_Default (Decl : Node_Id) is
18132 Default : constant Node_Id :=
18133 Default_Subtype_Mark (Original_Node (Decl));
18134 Formal : constant Entity_Id := Defining_Identifier (Decl);
18136 Def_Sub : Entity_Id; -- Default subtype mark
18137 Type_Def : Node_Id;
18139 procedure Check_Discriminated_Formal;
18140 -- Check that discriminants of default for private or incomplete
18141 -- type match those of formal type.
18143 function Reference_Formal (N : Node_Id) return Traverse_Result;
18144 -- Check whether formal type definition mentions a previous formal
18145 -- type of the same generic.
18147 ----------------------
18148 -- Reference_Formal --
18149 ----------------------
18151 function Reference_Formal (N : Node_Id) return Traverse_Result is
18153 if Is_Entity_Name (N)
18154 and then Scope (Entity (N)) = Current_Scope
18160 end Reference_Formal;
18162 function Depends_On_Other_Formals is
18163 new Traverse_Func (Reference_Formal);
18165 function Default_Subtype_Matches
18166 (Gen_T, Def_T : Entity_Id) return Boolean;
18168 procedure Validate_Array_Type_Default;
18169 -- Verify that dimension, indices, and component types of default
18170 -- are compatible with formal array type definition.
18172 procedure Validate_Derived_Type_Default;
18173 -- Verify that ancestor and progenitor types match.
18175 ---------------------------------
18176 -- Check_Discriminated_Formal --
18177 ---------------------------------
18179 procedure Check_Discriminated_Formal is
18180 Formal_Discr : Entity_Id;
18181 Actual_Discr : Entity_Id;
18182 Formal_Subt : Entity_Id;
18185 if Has_Discriminants (Formal) then
18186 if not Has_Discriminants (Def_Sub) then
18188 ("default for & must have discriminants", Default, Formal);
18190 elsif Is_Constrained (Def_Sub) then
18192 ("default for & must be unconstrained", Default, Formal);
18195 Formal_Discr := First_Discriminant (Formal);
18196 Actual_Discr := First_Discriminant (Def_Sub);
18197 while Formal_Discr /= Empty loop
18198 if Actual_Discr = Empty then
18200 ("discriminants on Formal do not match formal",
18204 Formal_Subt := Etype (Formal_Discr);
18206 -- Access discriminants match if designated types do
18208 if Ekind (Base_Type (Formal_Subt)) = E_Anonymous_Access_Type
18209 and then (Ekind (Base_Type (Etype (Actual_Discr)))) =
18210 E_Anonymous_Access_Type
18213 (Designated_Type (Base_Type (Formal_Subt))) =
18215 (Designated_Type (Base_Type (Etype (Actual_Discr))))
18217 Subtypes_Statically_Match
18218 (Designated_Type (Base_Type (Formal_Subt)),
18219 Designated_Type (Base_Type (Etype (Actual_Discr))))
18223 elsif Base_Type (Formal_Subt) /=
18224 Base_Type (Etype (Actual_Discr))
18227 ("types of discriminants of default must match formal",
18230 elsif not Subtypes_Statically_Match
18231 (Formal_Subt, Etype (Actual_Discr))
18232 and then Ada_Version >= Ada_95
18235 ("subtypes of discriminants of default "
18236 & "must match formal",
18240 Next_Discriminant (Formal_Discr);
18241 Next_Discriminant (Actual_Discr);
18244 if Actual_Discr /= Empty then
18246 ("discriminants on default do not match formal",
18251 end Check_Discriminated_Formal;
18253 ---------------------------
18254 -- Default_Subtype_Matches --
18255 ---------------------------
18257 function Default_Subtype_Matches
18258 (Gen_T, Def_T : Entity_Id) return Boolean
18261 -- Check that the base types, root types (when dealing with class
18262 -- wide types), or designated types (when dealing with anonymous
18263 -- access types) of Gen_T and Def_T are statically matching subtypes.
18265 return (Base_Type (Gen_T) = Base_Type (Def_T)
18266 and then Subtypes_Statically_Match (Gen_T, Def_T))
18268 or else (Is_Class_Wide_Type (Gen_T)
18269 and then Is_Class_Wide_Type (Def_T)
18270 and then Default_Subtype_Matches
18271 (Root_Type (Gen_T), Root_Type (Def_T)))
18273 or else (Is_Anonymous_Access_Type (Gen_T)
18274 and then Ekind (Def_T) = Ekind (Gen_T)
18275 and then Subtypes_Statically_Match
18276 (Designated_Type (Gen_T), Designated_Type (Def_T)));
18278 end Default_Subtype_Matches;
18280 ----------------------------------
18281 -- Validate_Array_Type_Default --
18282 ----------------------------------
18284 procedure Validate_Array_Type_Default is
18288 if not Is_Array_Type (Def_Sub) then
18289 Error_Msg_NE ("default for& must be an array type ",
18293 elsif Number_Dimensions (Def_Sub) /= Number_Dimensions (Formal)
18294 or else Is_Constrained (Def_Sub) /=
18295 Is_Constrained (Formal)
18297 Error_Msg_NE ("default array type does not match&",
18302 I1 := First_Index (Formal);
18303 I2 := First_Index (Def_Sub);
18304 for J in 1 .. Number_Dimensions (Formal) loop
18306 -- If the indexes of the actual were given by a subtype_mark,
18307 -- the index was transformed into a range attribute. Retrieve
18308 -- the original type mark for checking.
18310 if Is_Entity_Name (Original_Node (I2)) then
18311 T2 := Entity (Original_Node (I2));
18316 if not Subtypes_Statically_Match (Etype (I1), T2) then
18318 ("index types of default do not match those of formal &",
18326 if not Default_Subtype_Matches
18327 (Component_Type (Formal), Component_Type (Def_Sub))
18330 ("component subtype of default does not match that of formal &",
18334 if Has_Aliased_Components (Formal)
18335 and then not Has_Aliased_Components (Default)
18338 ("default must have aliased components to match formal type &",
18341 end Validate_Array_Type_Default;
18343 -----------------------------------
18344 -- Validate_Derived_Type_Default --
18345 -----------------------------------
18347 procedure Validate_Derived_Type_Default is
18349 if not Is_Ancestor (Etype (Formal), Def_Sub) then
18350 Error_Msg_NE ("default must be a descendent of&",
18351 Default, Etype (Formal));
18354 if Has_Interfaces (Formal) then
18355 if not Has_Interfaces (Def_Sub) then
18357 ("default must implement all interfaces of formal&",
18363 Iface_Ent : Entity_Id;
18366 Iface := First (Abstract_Interface_List (Formal));
18368 while Present (Iface) loop
18369 Iface_Ent := Entity (Iface);
18371 if Is_Ancestor (Iface_Ent, Def_Sub)
18372 or else Is_Progenitor (Iface_Ent, Def_Sub)
18378 ("Default must implement interface&",
18379 Default, Etype (Iface));
18387 end Validate_Derived_Type_Default;
18389 -- Start of processing for Validate_Formal_Type_Default
18393 if not Is_Entity_Name (Default)
18394 or else not Is_Type (Entity (Default))
18397 ("Expect type name for default of formal type", Default);
18400 Def_Sub := Entity (Default);
18403 -- Formal derived_type declarations are transformed into full
18404 -- type declarations or Private_Type_Extensions for ease of processing.
18406 if Nkind (Decl) = N_Full_Type_Declaration then
18407 Type_Def := Type_Definition (Decl);
18409 elsif Nkind (Decl) = N_Private_Extension_Declaration then
18410 Type_Def := Subtype_Indication (Decl);
18413 Type_Def := Formal_Type_Definition (Decl);
18416 if Depends_On_Other_Formals (Type_Def) = Abandon
18417 and then Scope (Def_Sub) /= Current_Scope
18419 Error_Msg_N ("default of formal type that depends on "
18420 & "other formals must be a previous formal type", Default);
18423 elsif Def_Sub = Formal then
18425 ("default for formal type cannot be formal itsef", Default);
18429 case Nkind (Type_Def) is
18431 when N_Formal_Private_Type_Definition =>
18432 if (Is_Abstract_Type (Formal)
18433 and then not Is_Abstract_Type (Def_Sub))
18434 or else (Is_Limited_Type (Formal)
18435 and then not Is_Limited_Type (Def_Sub))
18438 ("default for private type$ does not match",
18442 Check_Discriminated_Formal;
18444 when N_Formal_Derived_Type_Definition =>
18445 Check_Discriminated_Formal;
18446 Validate_Derived_Type_Default;
18448 when N_Formal_Incomplete_Type_Definition =>
18449 if Is_Tagged_Type (Formal)
18450 and then not Is_Tagged_Type (Def_Sub)
18453 ("default for & must be a tagged type", Default, Formal);
18456 Check_Discriminated_Formal;
18458 when N_Formal_Discrete_Type_Definition =>
18459 if not Is_Discrete_Type (Def_Sub) then
18460 Error_Msg_NE ("default for& must be a discrete type",
18464 when N_Formal_Signed_Integer_Type_Definition =>
18465 if not Is_Integer_Type (Def_Sub) then
18466 Error_Msg_NE ("default for& must be a discrete type",
18470 when N_Formal_Modular_Type_Definition =>
18471 if not Is_Modular_Integer_Type (Def_Sub) then
18472 Error_Msg_NE ("default for& must be a modular_integer Type",
18476 when N_Formal_Floating_Point_Definition =>
18477 if not Is_Floating_Point_Type (Def_Sub) then
18478 Error_Msg_NE ("default for& must be a floating_point type",
18482 when N_Formal_Ordinary_Fixed_Point_Definition =>
18483 if not Is_Ordinary_Fixed_Point_Type (Def_Sub) then
18484 Error_Msg_NE ("default for& must be "
18485 & "an ordinary_fixed_point type ",
18489 when N_Formal_Decimal_Fixed_Point_Definition =>
18490 if not Is_Decimal_Fixed_Point_Type (Def_Sub) then
18491 Error_Msg_NE ("default for& must be "
18492 & "an Decimal_fixed_point type ",
18496 when N_Array_Type_Definition =>
18497 Validate_Array_Type_Default;
18499 when N_Access_Function_Definition |
18500 N_Access_Procedure_Definition =>
18501 if Ekind (Def_Sub) /= E_Access_Subprogram_Type then
18502 Error_Msg_NE ("default for& must be an Access_To_Subprogram",
18505 Check_Subtype_Conformant
18506 (Designated_Type (Formal), Designated_Type (Def_Sub));
18508 when N_Access_To_Object_Definition =>
18509 if not Is_Access_Object_Type (Def_Sub) then
18510 Error_Msg_NE ("default for& must be an Access_To_Object",
18513 elsif not Default_Subtype_Matches
18514 (Designated_Type (Formal), Designated_Type (Def_Sub))
18516 Error_Msg_NE ("designated type of defaul does not match "
18517 & "designated type of formal type",
18521 when N_Record_Definition => -- Formal interface type
18522 if not Is_Interface (Def_Sub) then
18524 ("default for formal interface type must be an interface",
18527 elsif Is_Limited_Type (Def_Sub) /= Is_Limited_Type (Formal)
18528 or else Is_Task_Interface (Formal) /= Is_Task_Interface (Def_Sub)
18529 or else Is_Protected_Interface (Formal) /=
18530 Is_Protected_Interface (Def_Sub)
18531 or else Is_Synchronized_Interface (Formal) /=
18532 Is_Synchronized_Interface (Def_Sub)
18535 ("default for interface& does not match", Def_Sub, Formal);
18538 when N_Derived_Type_Definition =>
18539 Validate_Derived_Type_Default;
18541 when N_Identifier => -- case of a private extension
18542 Validate_Derived_Type_Default;
18548 raise Program_Error;
18550 end Validate_Formal_Type_Default;
18552 package body Instance_Context is
18554 --------------------
18555 -- Save_And_Reset --
18556 --------------------
18558 function Save_And_Reset return Context is
18560 return Result : Context (0 .. Integer (Generic_Renamings.Last)) do
18561 for Index in Result'Range loop
18563 Indexed_Assoc : Assoc renames Generic_Renamings.Table
18564 (Assoc_Ptr (Index));
18565 Result_Pair : Binding_Pair renames Result (Index);
18567 -- If we have called Increment_Last but have not yet
18568 -- initialized the new last element of the table, then
18569 -- that last element might be invalid. Saving and
18570 -- restoring (especially restoring, it turns out) invalid
18571 -- values can result in exceptions if predicate checking
18572 -- is enabled, so replace invalid values with Empty.
18574 if Indexed_Assoc.Gen_Id'Valid then
18575 Result_Pair.Formal_Id := Indexed_Assoc.Gen_Id;
18577 pragma Assert (Index = Result'Last);
18578 Result_Pair.Formal_Id := Empty;
18581 if Indexed_Assoc.Act_Id'Valid then
18582 Result_Pair.Actual_Id := Indexed_Assoc.Act_Id;
18584 pragma Assert (Index = Result'Last);
18585 Result_Pair.Actual_Id := Empty;
18590 Generic_Renamings.Init;
18591 Generic_Renamings.Set_Last (-1);
18592 Generic_Renamings_HTable.Reset;
18594 end Save_And_Reset;
18600 procedure Restore (Saved : Context) is
18602 Generic_Renamings.Init;
18603 Generic_Renamings.Set_Last (0);
18604 Generic_Renamings_HTable.Reset;
18605 Generic_Renamings.Increment_Last;
18606 for Pair of Saved loop
18607 Set_Instance_Of (Pair.Formal_Id, Pair.Actual_Id);
18609 Generic_Renamings.Decrement_Last;
18612 end Instance_Context;