1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
9 -- Copyright (C) 1992-2020, Free Software Foundation, Inc. --
11 -- GNAT is free software; you can redistribute it and/or modify it under --
12 -- terms of the GNU General Public License as published by the Free Soft- --
13 -- ware Foundation; either version 3, or (at your option) any later ver- --
14 -- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
15 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
16 -- or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License --
17 -- for more details. You should have received a copy of the GNU General --
18 -- Public License distributed with GNAT; see file COPYING3. If not, go to --
19 -- http://www.gnu.org/licenses for a complete copy of the license. --
21 -- GNAT was originally developed by the GNAT team at New York University. --
22 -- Extensive contributions were provided by Ada Core Technologies Inc. --
24 ------------------------------------------------------------------------------
26 with Aspects; use Aspects;
27 with Atree; use Atree;
28 with Contracts; use Contracts;
29 with Einfo; use Einfo;
30 with Elists; use Elists;
31 with Errout; use Errout;
32 with Expander; use Expander;
33 with Fname; use Fname;
34 with Fname.UF; use Fname.UF;
35 with Freeze; use Freeze;
36 with Ghost; use Ghost;
37 with Itypes; use Itypes;
39 with Lib.Load; use Lib.Load;
40 with Lib.Xref; use Lib.Xref;
41 with Nlists; use Nlists;
42 with Namet; use Namet;
43 with Nmake; use Nmake;
45 with Rident; use Rident;
46 with Restrict; use Restrict;
47 with Rtsfind; use Rtsfind;
49 with Sem_Aux; use Sem_Aux;
50 with Sem_Cat; use Sem_Cat;
51 with Sem_Ch3; use Sem_Ch3;
52 with Sem_Ch6; use Sem_Ch6;
53 with Sem_Ch7; use Sem_Ch7;
54 with Sem_Ch8; use Sem_Ch8;
55 with Sem_Ch10; use Sem_Ch10;
56 with Sem_Ch13; use Sem_Ch13;
57 with Sem_Dim; use Sem_Dim;
58 with Sem_Disp; use Sem_Disp;
59 with Sem_Elab; use Sem_Elab;
60 with Sem_Elim; use Sem_Elim;
61 with Sem_Eval; use Sem_Eval;
62 with Sem_Prag; use Sem_Prag;
63 with Sem_Res; use Sem_Res;
64 with Sem_Type; use Sem_Type;
65 with Sem_Util; use Sem_Util;
66 with Sem_Warn; use Sem_Warn;
67 with Stand; use Stand;
68 with Sinfo; use Sinfo;
69 with Sinfo.CN; use Sinfo.CN;
70 with Sinput; use Sinput;
71 with Sinput.L; use Sinput.L;
72 with Snames; use Snames;
73 with Stringt; use Stringt;
74 with Uname; use Uname;
76 with Tbuild; use Tbuild;
77 with Uintp; use Uintp;
78 with Urealp; use Urealp;
79 with Warnsw; use Warnsw;
83 package body Sem_Ch12 is
85 ----------------------------------------------------------
86 -- Implementation of Generic Analysis and Instantiation --
87 ----------------------------------------------------------
89 -- GNAT implements generics by macro expansion. No attempt is made to share
90 -- generic instantiations (for now). Analysis of a generic definition does
91 -- not perform any expansion action, but the expander must be called on the
92 -- tree for each instantiation, because the expansion may of course depend
93 -- on the generic actuals. All of this is best achieved as follows:
95 -- a) Semantic analysis of a generic unit is performed on a copy of the
96 -- tree for the generic unit. All tree modifications that follow analysis
97 -- do not affect the original tree. Links are kept between the original
98 -- tree and the copy, in order to recognize non-local references within
99 -- the generic, and propagate them to each instance (recall that name
100 -- resolution is done on the generic declaration: generics are not really
101 -- macros). This is summarized in the following diagram:
103 -- .-----------. .----------.
104 -- | semantic |<--------------| generic |
106 -- | |==============>| |
107 -- |___________| global |__________|
118 -- b) Each instantiation copies the original tree, and inserts into it a
119 -- series of declarations that describe the mapping between generic formals
120 -- and actuals. For example, a generic In OUT parameter is an object
121 -- renaming of the corresponding actual, etc. Generic IN parameters are
122 -- constant declarations.
124 -- c) In order to give the right visibility for these renamings, we use
125 -- a different scheme for package and subprogram instantiations. For
126 -- packages, the list of renamings is inserted into the package
127 -- specification, before the visible declarations of the package. The
128 -- renamings are analyzed before any of the text of the instance, and are
129 -- thus visible at the right place. Furthermore, outside of the instance,
130 -- the generic parameters are visible and denote their corresponding
133 -- For subprograms, we create a container package to hold the renamings
134 -- and the subprogram instance itself. Analysis of the package makes the
135 -- renaming declarations visible to the subprogram. After analyzing the
136 -- package, the defining entity for the subprogram is touched-up so that
137 -- it appears declared in the current scope, and not inside the container
140 -- If the instantiation is a compilation unit, the container package is
141 -- given the same name as the subprogram instance. This ensures that
142 -- the elaboration procedure called by the binder, using the compilation
143 -- unit name, calls in fact the elaboration procedure for the package.
145 -- Not surprisingly, private types complicate this approach. By saving in
146 -- the original generic object the non-local references, we guarantee that
147 -- the proper entities are referenced at the point of instantiation.
148 -- However, for private types, this by itself does not insure that the
149 -- proper VIEW of the entity is used (the full type may be visible at the
150 -- point of generic definition, but not at instantiation, or vice-versa).
151 -- In order to reference the proper view, we special-case any reference
152 -- to private types in the generic object, by saving both views, one in
153 -- the generic and one in the semantic copy. At time of instantiation, we
154 -- check whether the two views are consistent, and exchange declarations if
155 -- necessary, in order to restore the correct visibility. Similarly, if
156 -- the instance view is private when the generic view was not, we perform
157 -- the exchange. After completing the instantiation, we restore the
158 -- current visibility. The flag Has_Private_View marks identifiers in the
159 -- the generic unit that require checking.
161 -- Visibility within nested generic units requires special handling.
162 -- Consider the following scheme:
164 -- type Global is ... -- outside of generic unit.
168 -- type Semi_Global is ... -- global to inner.
171 -- procedure inner (X1 : Global; X2 : Semi_Global);
173 -- procedure in2 is new inner (...); -- 4
176 -- package New_Outer is new Outer (...); -- 2
177 -- procedure New_Inner is new New_Outer.Inner (...); -- 3
179 -- The semantic analysis of Outer captures all occurrences of Global.
180 -- The semantic analysis of Inner (at 1) captures both occurrences of
181 -- Global and Semi_Global.
183 -- At point 2 (instantiation of Outer), we also produce a generic copy
184 -- of Inner, even though Inner is, at that point, not being instantiated.
185 -- (This is just part of the semantic analysis of New_Outer).
187 -- Critically, references to Global within Inner must be preserved, while
188 -- references to Semi_Global should not preserved, because they must now
189 -- resolve to an entity within New_Outer. To distinguish between these, we
190 -- use a global variable, Current_Instantiated_Parent, which is set when
191 -- performing a generic copy during instantiation (at 2). This variable is
192 -- used when performing a generic copy that is not an instantiation, but
193 -- that is nested within one, as the occurrence of 1 within 2. The analysis
194 -- of a nested generic only preserves references that are global to the
195 -- enclosing Current_Instantiated_Parent. We use the Scope_Depth value to
196 -- determine whether a reference is external to the given parent.
198 -- The instantiation at point 3 requires no special treatment. The method
199 -- works as well for further nestings of generic units, but of course the
200 -- variable Current_Instantiated_Parent must be stacked because nested
201 -- instantiations can occur, e.g. the occurrence of 4 within 2.
203 -- The instantiation of package and subprogram bodies is handled in a
204 -- similar manner, except that it is delayed until after semantic
205 -- analysis is complete. In this fashion complex cross-dependencies
206 -- between several package declarations and bodies containing generics
207 -- can be compiled which otherwise would diagnose spurious circularities.
209 -- For example, it is possible to compile two packages A and B that
210 -- have the following structure:
212 -- package A is package B is
213 -- generic ... generic ...
214 -- package G_A is package G_B is
217 -- package body A is package body B is
218 -- package N_B is new G_B (..) package N_A is new G_A (..)
220 -- The table Pending_Instantiations in package Inline is used to keep
221 -- track of body instantiations that are delayed in this manner. Inline
222 -- handles the actual calls to do the body instantiations. This activity
223 -- is part of Inline, since the processing occurs at the same point, and
224 -- for essentially the same reason, as the handling of inlined routines.
226 ----------------------------------------------
227 -- Detection of Instantiation Circularities --
228 ----------------------------------------------
230 -- If we have a chain of instantiations that is circular, this is static
231 -- error which must be detected at compile time. The detection of these
232 -- circularities is carried out at the point that we insert a generic
233 -- instance spec or body. If there is a circularity, then the analysis of
234 -- the offending spec or body will eventually result in trying to load the
235 -- same unit again, and we detect this problem as we analyze the package
236 -- instantiation for the second time.
238 -- At least in some cases after we have detected the circularity, we get
239 -- into trouble if we try to keep going. The following flag is set if a
240 -- circularity is detected, and used to abandon compilation after the
241 -- messages have been posted.
243 Circularity_Detected : Boolean := False;
244 -- It should really be reset upon encountering a new main unit, but in
245 -- practice we do not use multiple main units so this is not critical.
247 -----------------------------------------
248 -- Implementation of Generic Contracts --
249 -----------------------------------------
251 -- A "contract" is a collection of aspects and pragmas that either verify a
252 -- property of a construct at runtime or classify the data flow to and from
253 -- the construct in some fashion.
255 -- Generic packages, subprograms and their respective bodies may be subject
256 -- to the following contract-related aspects or pragmas collectively known
259 -- package subprogram [body]
260 -- Abstract_State Contract_Cases
261 -- Initial_Condition Depends
262 -- Initializes Extensions_Visible
265 -- Refined_State Post_Class
275 -- Most package contract annotations utilize forward references to classify
276 -- data declared within the package [body]. Subprogram annotations then use
277 -- the classifications to further refine them. These inter dependencies are
278 -- problematic with respect to the implementation of generics because their
279 -- analysis, capture of global references and instantiation does not mesh
280 -- well with the existing mechanism.
282 -- 1) Analysis of generic contracts is carried out the same way non-generic
283 -- contracts are analyzed:
285 -- 1.1) General rule - a contract is analyzed after all related aspects
286 -- and pragmas are analyzed. This is done by routines
288 -- Analyze_Package_Body_Contract
289 -- Analyze_Package_Contract
290 -- Analyze_Subprogram_Body_Contract
291 -- Analyze_Subprogram_Contract
293 -- 1.2) Compilation unit - the contract is analyzed after Pragmas_After
296 -- 1.3) Compilation unit body - the contract is analyzed at the end of
297 -- the body declaration list.
299 -- 1.4) Package - the contract is analyzed at the end of the private or
300 -- visible declarations, prior to analyzing the contracts of any nested
301 -- packages or subprograms.
303 -- 1.5) Package body - the contract is analyzed at the end of the body
304 -- declaration list, prior to analyzing the contracts of any nested
305 -- packages or subprograms.
307 -- 1.6) Subprogram - if the subprogram is declared inside a block, a
308 -- package or a subprogram, then its contract is analyzed at the end of
309 -- the enclosing declarations, otherwise the subprogram is a compilation
312 -- 1.7) Subprogram body - if the subprogram body is declared inside a
313 -- block, a package body or a subprogram body, then its contract is
314 -- analyzed at the end of the enclosing declarations, otherwise the
315 -- subprogram is a compilation unit 1.3).
317 -- 2) Capture of global references within contracts is done after capturing
318 -- global references within the generic template. There are two reasons for
319 -- this delay - pragma annotations are not part of the generic template in
320 -- the case of a generic subprogram declaration, and analysis of contracts
323 -- Contract-related source pragmas within generic templates are prepared
324 -- for delayed capture of global references by routine
326 -- Create_Generic_Contract
328 -- The routine associates these pragmas with the contract of the template.
329 -- In the case of a generic subprogram declaration, the routine creates
330 -- generic templates for the pragmas declared after the subprogram because
331 -- they are not part of the template.
333 -- generic -- template starts
334 -- procedure Gen_Proc (Input : Integer); -- template ends
335 -- pragma Precondition (Input > 0); -- requires own template
337 -- 2.1) The capture of global references with aspect specifications and
338 -- source pragmas that apply to a generic unit must be suppressed when
339 -- the generic template is being processed because the contracts have not
340 -- been analyzed yet. Any attempts to capture global references at that
341 -- point will destroy the Associated_Node linkages and leave the template
342 -- undecorated. This delay is controlled by routine
344 -- Requires_Delayed_Save
346 -- 2.2) The real capture of global references within a contract is done
347 -- after the contract has been analyzed, by routine
349 -- Save_Global_References_In_Contract
351 -- 3) The instantiation of a generic contract occurs as part of the
352 -- instantiation of the contract owner. Generic subprogram declarations
353 -- require additional processing when the contract is specified by pragmas
354 -- because the pragmas are not part of the generic template. This is done
357 -- Instantiate_Subprogram_Contract
359 --------------------------------------------------
360 -- Formal packages and partial parameterization --
361 --------------------------------------------------
363 -- When compiling a generic, a formal package is a local instantiation. If
364 -- declared with a box, its generic formals are visible in the enclosing
365 -- generic. If declared with a partial list of actuals, those actuals that
366 -- are defaulted (covered by an Others clause, or given an explicit box
367 -- initialization) are also visible in the enclosing generic, while those
368 -- that have a corresponding actual are not.
370 -- In our source model of instantiation, the same visibility must be
371 -- present in the spec and body of an instance: the names of the formals
372 -- that are defaulted must be made visible within the instance, and made
373 -- invisible (hidden) after the instantiation is complete, so that they
374 -- are not accessible outside of the instance.
376 -- In a generic, a formal package is treated like a special instantiation.
377 -- Our Ada 95 compiler handled formals with and without box in different
378 -- ways. With partial parameterization, we use a single model for both.
379 -- We create a package declaration that consists of the specification of
380 -- the generic package, and a set of declarations that map the actuals
381 -- into local renamings, just as we do for bona fide instantiations. For
382 -- defaulted parameters and formals with a box, we copy directly the
383 -- declarations of the formals into this local package. The result is a
384 -- package whose visible declarations may include generic formals. This
385 -- package is only used for type checking and visibility analysis, and
386 -- never reaches the back end, so it can freely violate the placement
387 -- rules for generic formal declarations.
389 -- The list of declarations (renamings and copies of formals) is built
390 -- by Analyze_Associations, just as for regular instantiations.
392 -- At the point of instantiation, conformance checking must be applied only
393 -- to those parameters that were specified in the formals. We perform this
394 -- checking by creating another internal instantiation, this one including
395 -- only the renamings and the formals (the rest of the package spec is not
396 -- relevant to conformance checking). We can then traverse two lists: the
397 -- list of actuals in the instance that corresponds to the formal package,
398 -- and the list of actuals produced for this bogus instantiation. We apply
399 -- the conformance rules to those actuals that are not defaulted, i.e.
400 -- which still appear as generic formals.
402 -- When we compile an instance body we must make the right parameters
403 -- visible again. The predicate Is_Generic_Formal indicates which of the
404 -- formals should have its Is_Hidden flag reset.
406 -----------------------
407 -- Local subprograms --
408 -----------------------
410 procedure Abandon_Instantiation (N : Node_Id);
411 pragma No_Return (Abandon_Instantiation);
412 -- Posts an error message "instantiation abandoned" at the indicated node
413 -- and then raises the exception Instantiation_Error to do it.
415 procedure Analyze_Formal_Array_Type
416 (T : in out Entity_Id;
418 -- A formal array type is treated like an array type declaration, and
419 -- invokes Array_Type_Declaration (sem_ch3) whose first parameter is
420 -- in-out, because in the case of an anonymous type the entity is
421 -- actually created in the procedure.
423 -- The following procedures treat other kinds of formal parameters
425 procedure Analyze_Formal_Derived_Interface_Type
430 procedure Analyze_Formal_Derived_Type
435 procedure Analyze_Formal_Interface_Type
440 -- The following subprograms create abbreviated declarations for formal
441 -- scalar types. We introduce an anonymous base of the proper class for
442 -- each of them, and define the formals as constrained first subtypes of
443 -- their bases. The bounds are expressions that are non-static in the
446 procedure Analyze_Formal_Decimal_Fixed_Point_Type
447 (T : Entity_Id; Def : Node_Id);
448 procedure Analyze_Formal_Discrete_Type (T : Entity_Id; Def : Node_Id);
449 procedure Analyze_Formal_Floating_Type (T : Entity_Id; Def : Node_Id);
450 procedure Analyze_Formal_Signed_Integer_Type (T : Entity_Id; Def : Node_Id);
451 procedure Analyze_Formal_Modular_Type (T : Entity_Id; Def : Node_Id);
452 procedure Analyze_Formal_Ordinary_Fixed_Point_Type
453 (T : Entity_Id; Def : Node_Id);
455 procedure Analyze_Formal_Private_Type
459 -- Creates a new private type, which does not require completion
461 procedure Analyze_Formal_Incomplete_Type (T : Entity_Id; Def : Node_Id);
462 -- Ada 2012: Creates a new incomplete type whose actual does not freeze
464 procedure Analyze_Generic_Formal_Part (N : Node_Id);
465 -- Analyze generic formal part
467 procedure Analyze_Generic_Access_Type (T : Entity_Id; Def : Node_Id);
468 -- Create a new access type with the given designated type
470 function Analyze_Associations
473 F_Copy : List_Id) return List_Id;
474 -- At instantiation time, build the list of associations between formals
475 -- and actuals. Each association becomes a renaming declaration for the
476 -- formal entity. F_Copy is the analyzed list of formals in the generic
477 -- copy. It is used to apply legality checks to the actuals. I_Node is the
478 -- instantiation node itself.
480 procedure Analyze_Subprogram_Instantiation
484 procedure Build_Instance_Compilation_Unit_Nodes
488 -- This procedure is used in the case where the generic instance of a
489 -- subprogram body or package body is a library unit. In this case, the
490 -- original library unit node for the generic instantiation must be
491 -- replaced by the resulting generic body, and a link made to a new
492 -- compilation unit node for the generic declaration. The argument N is
493 -- the original generic instantiation. Act_Body and Act_Decl are the body
494 -- and declaration of the instance (either package body and declaration
495 -- nodes or subprogram body and declaration nodes depending on the case).
496 -- On return, the node N has been rewritten with the actual body.
498 function Build_Subprogram_Decl_Wrapper
499 (Formal_Subp : Entity_Id) return Node_Id;
500 -- Ada 2020 allows formal subprograms to carry pre/postconditions.
501 -- At the point of instantiation these contracts apply to uses of
502 -- the actual subprogram. This is implemented by creating wrapper
503 -- subprograms instead of the renamings previously used to link
504 -- formal subprograms and the corresponding actuals. If the actual
505 -- is not an entity (e.g. an attribute reference) a renaming is
506 -- created to handle the expansion of the attribute.
508 function Build_Subprogram_Body_Wrapper
509 (Formal_Subp : Entity_Id;
510 Actual_Name : Node_Id) return Node_Id;
511 -- The body of the wrapper is a call to the actual, with the generated
512 -- pre/postconditon checks added.
514 procedure Check_Access_Definition (N : Node_Id);
515 -- Subsidiary routine to null exclusion processing. Perform an assertion
516 -- check on Ada version and the presence of an access definition in N.
518 procedure Check_Formal_Packages (P_Id : Entity_Id);
519 -- Apply the following to all formal packages in generic associations.
520 -- Restore the visibility of the formals of the instance that are not
521 -- defaulted (see RM 12.7 (10)). Remove the anonymous package declaration
522 -- created for formal instances that are not defaulted.
524 procedure Check_Formal_Package_Instance
525 (Formal_Pack : Entity_Id;
526 Actual_Pack : Entity_Id);
527 -- Verify that the actuals of the actual instance match the actuals of
528 -- the template for a formal package that is not declared with a box.
530 procedure Check_Forward_Instantiation (Decl : Node_Id);
531 -- If the generic is a local entity and the corresponding body has not
532 -- been seen yet, flag enclosing packages to indicate that it will be
533 -- elaborated after the generic body. Subprograms declared in the same
534 -- package cannot be inlined by the front end because front-end inlining
535 -- requires a strict linear order of elaboration.
537 function Check_Hidden_Primitives (Assoc_List : List_Id) return Elist_Id;
538 -- Check if some association between formals and actuals requires to make
539 -- visible primitives of a tagged type, and make those primitives visible.
540 -- Return the list of primitives whose visibility is modified (to restore
541 -- their visibility later through Restore_Hidden_Primitives). If no
542 -- candidate is found then return No_Elist.
544 procedure Check_Hidden_Child_Unit
546 Gen_Unit : Entity_Id;
547 Act_Decl_Id : Entity_Id);
548 -- If the generic unit is an implicit child instance within a parent
549 -- instance, we need to make an explicit test that it is not hidden by
550 -- a child instance of the same name and parent.
552 procedure Check_Generic_Actuals
553 (Instance : Entity_Id;
554 Is_Formal_Box : Boolean);
555 -- Similar to previous one. Check the actuals in the instantiation,
556 -- whose views can change between the point of instantiation and the point
557 -- of instantiation of the body. In addition, mark the generic renamings
558 -- as generic actuals, so that they are not compatible with other actuals.
559 -- Recurse on an actual that is a formal package whose declaration has
562 function Contains_Instance_Of
565 N : Node_Id) return Boolean;
566 -- Inner is instantiated within the generic Outer. Check whether Inner
567 -- directly or indirectly contains an instance of Outer or of one of its
568 -- parents, in the case of a subunit. Each generic unit holds a list of
569 -- the entities instantiated within (at any depth). This procedure
570 -- determines whether the set of such lists contains a cycle, i.e. an
571 -- illegal circular instantiation.
573 function Denotes_Formal_Package
575 On_Exit : Boolean := False;
576 Instance : Entity_Id := Empty) return Boolean;
577 -- Returns True if E is a formal package of an enclosing generic, or
578 -- the actual for such a formal in an enclosing instantiation. If such
579 -- a package is used as a formal in an nested generic, or as an actual
580 -- in a nested instantiation, the visibility of ITS formals should not
581 -- be modified. When called from within Restore_Private_Views, the flag
582 -- On_Exit is true, to indicate that the search for a possible enclosing
583 -- instance should ignore the current one. In that case Instance denotes
584 -- the declaration for which this is an actual. This declaration may be
585 -- an instantiation in the source, or the internal instantiation that
586 -- corresponds to the actual for a formal package.
588 function Earlier (N1, N2 : Node_Id) return Boolean;
589 -- Yields True if N1 and N2 appear in the same compilation unit,
590 -- ignoring subunits, and if N1 is to the left of N2 in a left-to-right
591 -- traversal of the tree for the unit. Used to determine the placement
592 -- of freeze nodes for instance bodies that may depend on other instances.
594 function Find_Actual_Type
596 Gen_Type : Entity_Id) return Entity_Id;
597 -- When validating the actual types of a child instance, check whether
598 -- the formal is a formal type of the parent unit, and retrieve the current
599 -- actual for it. Typ is the entity in the analyzed formal type declaration
600 -- (component or index type of an array type, or designated type of an
601 -- access formal) and Gen_Type is the enclosing analyzed formal array
602 -- or access type. The desired actual may be a formal of a parent, or may
603 -- be declared in a formal package of a parent. In both cases it is a
604 -- generic actual type because it appears within a visible instance.
605 -- Finally, it may be declared in a parent unit without being a formal
606 -- of that unit, in which case it must be retrieved by visibility.
607 -- Ambiguities may still arise if two homonyms are declared in two formal
608 -- packages, and the prefix of the formal type may be needed to resolve
609 -- the ambiguity in the instance ???
611 procedure Freeze_Subprogram_Body
612 (Inst_Node : Node_Id;
614 Pack_Id : Entity_Id);
615 -- The generic body may appear textually after the instance, including
616 -- in the proper body of a stub, or within a different package instance.
617 -- Given that the instance can only be elaborated after the generic, we
618 -- place freeze_nodes for the instance and/or for packages that may enclose
619 -- the instance and the generic, so that the back-end can establish the
620 -- proper order of elaboration.
622 function Get_Associated_Node (N : Node_Id) return Node_Id;
623 -- In order to propagate semantic information back from the analyzed copy
624 -- to the original generic, we maintain links between selected nodes in the
625 -- generic and their corresponding copies. At the end of generic analysis,
626 -- the routine Save_Global_References traverses the generic tree, examines
627 -- the semantic information, and preserves the links to those nodes that
628 -- contain global information. At instantiation, the information from the
629 -- associated node is placed on the new copy, so that name resolution is
632 -- Three kinds of source nodes have associated nodes:
634 -- a) those that can reference (denote) entities, that is identifiers,
635 -- character literals, expanded_names, operator symbols, operators,
636 -- and attribute reference nodes. These nodes have an Entity field
637 -- and are the set of nodes that are in N_Has_Entity.
639 -- b) aggregates (N_Aggregate and N_Extension_Aggregate)
641 -- c) selected components (N_Selected_Component)
643 -- For the first class, the associated node preserves the entity if it is
644 -- global. If the generic contains nested instantiations, the associated
645 -- node itself has been recopied, and a chain of them must be followed.
647 -- For aggregates, the associated node allows retrieval of the type, which
648 -- may otherwise not appear in the generic. The view of this type may be
649 -- different between generic and instantiation, and the full view can be
650 -- installed before the instantiation is analyzed. For aggregates of type
651 -- extensions, the same view exchange may have to be performed for some of
652 -- the ancestor types, if their view is private at the point of
655 -- Nodes that are selected components in the parse tree may be rewritten
656 -- as expanded names after resolution, and must be treated as potential
657 -- entity holders, which is why they also have an Associated_Node.
659 -- Nodes that do not come from source, such as freeze nodes, do not appear
660 -- in the generic tree, and need not have an associated node.
662 -- The associated node is stored in the Associated_Node field. Note that
663 -- this field overlaps Entity, which is fine, because the whole point is
664 -- that we don't need or want the normal Entity field in this situation.
666 function Has_Been_Exchanged (E : Entity_Id) return Boolean;
667 -- Traverse the Exchanged_Views list to see if a type was private
668 -- and has already been flipped during this phase of instantiation.
670 function Has_Contracts (Decl : Node_Id) return Boolean;
671 -- Determine whether a formal subprogram has a Pre- or Postcondition,
672 -- in which case a subprogram wrapper has to be built for the actual.
674 procedure Hide_Current_Scope;
675 -- When instantiating a generic child unit, the parent context must be
676 -- present, but the instance and all entities that may be generated
677 -- must be inserted in the current scope. We leave the current scope
678 -- on the stack, but make its entities invisible to avoid visibility
679 -- problems. This is reversed at the end of the instantiation. This is
680 -- not done for the instantiation of the bodies, which only require the
681 -- instances of the generic parents to be in scope.
683 function In_Main_Context (E : Entity_Id) return Boolean;
684 -- Check whether an instantiation is in the context of the main unit.
685 -- Used to determine whether its body should be elaborated to allow
686 -- front-end inlining.
688 procedure Inherit_Context (Gen_Decl : Node_Id; Inst : Node_Id);
689 -- Add the context clause of the unit containing a generic unit to a
690 -- compilation unit that is, or contains, an instantiation.
693 -- Establish environment for subsequent instantiation. Separated from
694 -- Save_Env because data-structures for visibility handling must be
695 -- initialized before call to Check_Generic_Child_Unit.
697 procedure Inline_Instance_Body
699 Gen_Unit : Entity_Id;
701 -- If front-end inlining is requested, instantiate the package body,
702 -- and preserve the visibility of its compilation unit, to insure
703 -- that successive instantiations succeed.
705 procedure Insert_Freeze_Node_For_Instance
708 -- N denotes a package or a subprogram instantiation and F_Node is the
709 -- associated freeze node. Insert the freeze node before the first source
710 -- body which follows immediately after N. If no such body is found, the
711 -- freeze node is inserted at the end of the declarative region which
714 procedure Install_Body
719 -- If the instantiation happens textually before the body of the generic,
720 -- the instantiation of the body must be analyzed after the generic body,
721 -- and not at the point of instantiation. Such early instantiations can
722 -- happen if the generic and the instance appear in a package declaration
723 -- because the generic body can only appear in the corresponding package
724 -- body. Early instantiations can also appear if generic, instance and
725 -- body are all in the declarative part of a subprogram or entry. Entities
726 -- of packages that are early instantiations are delayed, and their freeze
727 -- node appears after the generic body. This rather complex machinery is
728 -- needed when nested instantiations are present, because the source does
729 -- not carry any indication of where the corresponding instance bodies must
730 -- be installed and frozen.
732 procedure Install_Formal_Packages (Par : Entity_Id);
733 -- Install the visible part of any formal of the parent that is a formal
734 -- package. Note that for the case of a formal package with a box, this
735 -- includes the formal part of the formal package (12.7(10/2)).
737 procedure Install_Hidden_Primitives
738 (Prims_List : in out Elist_Id;
741 -- Remove suffix 'P' from hidden primitives of Act_T to match the
742 -- visibility of primitives of Gen_T. The list of primitives to which
743 -- the suffix is removed is added to Prims_List to restore them later.
745 procedure Install_Parent (P : Entity_Id; In_Body : Boolean := False);
746 -- When compiling an instance of a child unit the parent (which is
747 -- itself an instance) is an enclosing scope that must be made
748 -- immediately visible. This procedure is also used to install the non-
749 -- generic parent of a generic child unit when compiling its body, so
750 -- that full views of types in the parent are made visible.
752 -- The functions Instantiate_XXX perform various legality checks and build
753 -- the declarations for instantiated generic parameters. In all of these
754 -- Formal is the entity in the generic unit, Actual is the entity of
755 -- expression in the generic associations, and Analyzed_Formal is the
756 -- formal in the generic copy, which contains the semantic information to
757 -- be used to validate the actual.
759 function Instantiate_Object
762 Analyzed_Formal : Node_Id) return List_Id;
764 function Instantiate_Type
767 Analyzed_Formal : Node_Id;
768 Actual_Decls : List_Id) return List_Id;
770 function Instantiate_Formal_Subprogram
773 Analyzed_Formal : Node_Id) return Node_Id;
775 function Instantiate_Formal_Package
778 Analyzed_Formal : Node_Id) return List_Id;
779 -- If the formal package is declared with a box, special visibility rules
780 -- apply to its formals: they are in the visible part of the package. This
781 -- is true in the declarative region of the formal package, that is to say
782 -- in the enclosing generic or instantiation. For an instantiation, the
783 -- parameters of the formal package are made visible in an explicit step.
784 -- Furthermore, if the actual has a visible USE clause, these formals must
785 -- be made potentially use-visible as well. On exit from the enclosing
786 -- instantiation, the reverse must be done.
788 -- For a formal package declared without a box, there are conformance rules
789 -- that apply to the actuals in the generic declaration and the actuals of
790 -- the actual package in the enclosing instantiation. The simplest way to
791 -- apply these rules is to repeat the instantiation of the formal package
792 -- in the context of the enclosing instance, and compare the generic
793 -- associations of this instantiation with those of the actual package.
794 -- This internal instantiation only needs to contain the renamings of the
795 -- formals: the visible and private declarations themselves need not be
798 -- In Ada 2005, the formal package may be only partially parameterized.
799 -- In that case the visibility step must make visible those actuals whose
800 -- corresponding formals were given with a box. A final complication
801 -- involves inherited operations from formal derived types, which must
802 -- be visible if the type is.
804 function Is_In_Main_Unit (N : Node_Id) return Boolean;
805 -- Test if given node is in the main unit
807 procedure Load_Parent_Of_Generic
810 Body_Optional : Boolean := False);
811 -- If the generic appears in a separate non-generic library unit, load the
812 -- corresponding body to retrieve the body of the generic. N is the node
813 -- for the generic instantiation, Spec is the generic package declaration.
815 -- Body_Optional is a flag that indicates that the body is being loaded to
816 -- ensure that temporaries are generated consistently when there are other
817 -- instances in the current declarative part that precede the one being
818 -- loaded. In that case a missing body is acceptable.
820 procedure Map_Formal_Package_Entities (Form : Entity_Id; Act : Entity_Id);
821 -- Within the generic part, entities in the formal package are
822 -- visible. To validate subsequent type declarations, indicate
823 -- the correspondence between the entities in the analyzed formal,
824 -- and the entities in the actual package. There are three packages
825 -- involved in the instantiation of a formal package: the parent
826 -- generic P1 which appears in the generic declaration, the fake
827 -- instantiation P2 which appears in the analyzed generic, and whose
828 -- visible entities may be used in subsequent formals, and the actual
829 -- P3 in the instance. To validate subsequent formals, me indicate
830 -- that the entities in P2 are mapped into those of P3. The mapping of
831 -- entities has to be done recursively for nested packages.
833 procedure Move_Freeze_Nodes
837 -- Freeze nodes can be generated in the analysis of a generic unit, but
838 -- will not be seen by the back-end. It is necessary to move those nodes
839 -- to the enclosing scope if they freeze an outer entity. We place them
840 -- at the end of the enclosing generic package, which is semantically
843 procedure Preanalyze_Actuals (N : Node_Id; Inst : Entity_Id := Empty);
844 -- Analyze actuals to perform name resolution. Full resolution is done
845 -- later, when the expected types are known, but names have to be captured
846 -- before installing parents of generics, that are not visible for the
847 -- actuals themselves.
849 -- If Inst is present, it is the entity of the package instance. This
850 -- entity is marked as having a limited_view actual when some actual is
851 -- a limited view. This is used to place the instance body properly.
853 procedure Provide_Completing_Bodies (N : Node_Id);
854 -- Generate completing bodies for all subprograms found within package or
855 -- subprogram declaration N.
857 procedure Remove_Parent (In_Body : Boolean := False);
858 -- Reverse effect after instantiation of child is complete
860 procedure Restore_Hidden_Primitives (Prims_List : in out Elist_Id);
861 -- Restore suffix 'P' to primitives of Prims_List and leave Prims_List
864 procedure Set_Instance_Env
865 (Gen_Unit : Entity_Id;
866 Act_Unit : Entity_Id);
867 -- Save current instance on saved environment, to be used to determine
868 -- the global status of entities in nested instances. Part of Save_Env.
869 -- called after verifying that the generic unit is legal for the instance,
870 -- The procedure also examines whether the generic unit is a predefined
871 -- unit, in order to set configuration switches accordingly. As a result
872 -- the procedure must be called after analyzing and freezing the actuals.
874 procedure Set_Instance_Of (A : Entity_Id; B : Entity_Id);
875 -- Associate analyzed generic parameter with corresponding instance. Used
876 -- for semantic checks at instantiation time.
878 function True_Parent (N : Node_Id) return Node_Id;
879 -- For a subunit, return parent of corresponding stub, else return
882 procedure Valid_Default_Attribute (Nam : Entity_Id; Def : Node_Id);
883 -- Verify that an attribute that appears as the default for a formal
884 -- subprogram is a function or procedure with the correct profile.
886 -------------------------------------------
887 -- Data Structures for Generic Renamings --
888 -------------------------------------------
890 -- The map Generic_Renamings associates generic entities with their
891 -- corresponding actuals. Currently used to validate type instances. It
892 -- will eventually be used for all generic parameters to eliminate the
893 -- need for overload resolution in the instance.
895 type Assoc_Ptr is new Int;
897 Assoc_Null : constant Assoc_Ptr := -1;
902 Next_In_HTable : Assoc_Ptr;
905 package Generic_Renamings is new Table.Table
906 (Table_Component_Type => Assoc,
907 Table_Index_Type => Assoc_Ptr,
908 Table_Low_Bound => 0,
910 Table_Increment => 100,
911 Table_Name => "Generic_Renamings");
913 -- Variable to hold enclosing instantiation. When the environment is
914 -- saved for a subprogram inlining, the corresponding Act_Id is empty.
916 Current_Instantiated_Parent : Assoc := (Empty, Empty, Assoc_Null);
918 -- Hash table for associations
920 HTable_Size : constant := 37;
921 type HTable_Range is range 0 .. HTable_Size - 1;
923 procedure Set_Next_Assoc (E : Assoc_Ptr; Next : Assoc_Ptr);
924 function Next_Assoc (E : Assoc_Ptr) return Assoc_Ptr;
925 function Get_Gen_Id (E : Assoc_Ptr) return Entity_Id;
926 function Hash (F : Entity_Id) return HTable_Range;
928 package Generic_Renamings_HTable is new GNAT.HTable.Static_HTable (
929 Header_Num => HTable_Range,
931 Elmt_Ptr => Assoc_Ptr,
932 Null_Ptr => Assoc_Null,
933 Set_Next => Set_Next_Assoc,
936 Get_Key => Get_Gen_Id,
940 Exchanged_Views : Elist_Id;
941 -- This list holds the private views that have been exchanged during
942 -- instantiation to restore the visibility of the generic declaration.
943 -- (see comments above). After instantiation, the current visibility is
944 -- reestablished by means of a traversal of this list.
946 Hidden_Entities : Elist_Id;
947 -- This list holds the entities of the current scope that are removed
948 -- from immediate visibility when instantiating a child unit. Their
949 -- visibility is restored in Remove_Parent.
951 -- Because instantiations can be recursive, the following must be saved
952 -- on entry and restored on exit from an instantiation (spec or body).
953 -- This is done by the two procedures Save_Env and Restore_Env. For
954 -- package and subprogram instantiations (but not for the body instances)
955 -- the action of Save_Env is done in two steps: Init_Env is called before
956 -- Check_Generic_Child_Unit, because setting the parent instances requires
957 -- that the visibility data structures be properly initialized. Once the
958 -- generic is unit is validated, Set_Instance_Env completes Save_Env.
960 Parent_Unit_Visible : Boolean := False;
961 -- Parent_Unit_Visible is used when the generic is a child unit, and
962 -- indicates whether the ultimate parent of the generic is visible in the
963 -- instantiation environment. It is used to reset the visibility of the
964 -- parent at the end of the instantiation (see Remove_Parent).
966 Instance_Parent_Unit : Entity_Id := Empty;
967 -- This records the ultimate parent unit of an instance of a generic
968 -- child unit and is used in conjunction with Parent_Unit_Visible to
969 -- indicate the unit to which the Parent_Unit_Visible flag corresponds.
971 type Instance_Env is record
972 Instantiated_Parent : Assoc;
973 Exchanged_Views : Elist_Id;
974 Hidden_Entities : Elist_Id;
975 Current_Sem_Unit : Unit_Number_Type;
976 Parent_Unit_Visible : Boolean := False;
977 Instance_Parent_Unit : Entity_Id := Empty;
978 Switches : Config_Switches_Type;
981 package Instance_Envs is new Table.Table (
982 Table_Component_Type => Instance_Env,
983 Table_Index_Type => Int,
984 Table_Low_Bound => 0,
986 Table_Increment => 100,
987 Table_Name => "Instance_Envs");
989 procedure Restore_Private_Views
990 (Pack_Id : Entity_Id;
991 Is_Package : Boolean := True);
992 -- Restore the private views of external types, and unmark the generic
993 -- renamings of actuals, so that they become compatible subtypes again.
994 -- For subprograms, Pack_Id is the package constructed to hold the
997 procedure Switch_View (T : Entity_Id);
998 -- Switch the partial and full views of a type and its private
999 -- dependents (i.e. its subtypes and derived types).
1001 ------------------------------------
1002 -- Structures for Error Reporting --
1003 ------------------------------------
1005 Instantiation_Node : Node_Id;
1006 -- Used by subprograms that validate instantiation of formal parameters
1007 -- where there might be no actual on which to place the error message.
1008 -- Also used to locate the instantiation node for generic subunits.
1010 Instantiation_Error : exception;
1011 -- When there is a semantic error in the generic parameter matching,
1012 -- there is no point in continuing the instantiation, because the
1013 -- number of cascaded errors is unpredictable. This exception aborts
1014 -- the instantiation process altogether.
1016 S_Adjustment : Sloc_Adjustment;
1017 -- Offset created for each node in an instantiation, in order to keep
1018 -- track of the source position of the instantiation in each of its nodes.
1019 -- A subsequent semantic error or warning on a construct of the instance
1020 -- points to both places: the original generic node, and the point of
1021 -- instantiation. See Sinput and Sinput.L for additional details.
1023 ------------------------------------------------------------
1024 -- Data structure for keeping track when inside a Generic --
1025 ------------------------------------------------------------
1027 -- The following table is used to save values of the Inside_A_Generic
1028 -- flag (see spec of Sem) when they are saved by Start_Generic.
1030 package Generic_Flags is new Table.Table (
1031 Table_Component_Type => Boolean,
1032 Table_Index_Type => Int,
1033 Table_Low_Bound => 0,
1034 Table_Initial => 32,
1035 Table_Increment => 200,
1036 Table_Name => "Generic_Flags");
1038 ---------------------------
1039 -- Abandon_Instantiation --
1040 ---------------------------
1042 procedure Abandon_Instantiation (N : Node_Id) is
1044 Error_Msg_N ("\instantiation abandoned!", N);
1045 raise Instantiation_Error;
1046 end Abandon_Instantiation;
1048 ----------------------------------
1049 -- Adjust_Inherited_Pragma_Sloc --
1050 ----------------------------------
1052 procedure Adjust_Inherited_Pragma_Sloc (N : Node_Id) is
1054 Adjust_Instantiation_Sloc (N, S_Adjustment);
1055 end Adjust_Inherited_Pragma_Sloc;
1057 --------------------------
1058 -- Analyze_Associations --
1059 --------------------------
1061 function Analyze_Associations
1064 F_Copy : List_Id) return List_Id
1066 Actuals_To_Freeze : constant Elist_Id := New_Elmt_List;
1067 Assoc_List : constant List_Id := New_List;
1068 Default_Actuals : constant List_Id := New_List;
1069 Gen_Unit : constant Entity_Id :=
1070 Defining_Entity (Parent (F_Copy));
1074 Analyzed_Formal : Node_Id;
1075 First_Named : Node_Id := Empty;
1079 Saved_Formal : Node_Id;
1081 Default_Formals : constant List_Id := New_List;
1082 -- If an Others_Choice is present, some of the formals may be defaulted.
1083 -- To simplify the treatment of visibility in an instance, we introduce
1084 -- individual defaults for each such formal. These defaults are
1085 -- appended to the list of associations and replace the Others_Choice.
1087 Found_Assoc : Node_Id;
1088 -- Association for the current formal being match. Empty if there are
1089 -- no remaining actuals, or if there is no named association with the
1090 -- name of the formal.
1092 Is_Named_Assoc : Boolean;
1093 Num_Matched : Nat := 0;
1094 Num_Actuals : Nat := 0;
1096 Others_Present : Boolean := False;
1097 Others_Choice : Node_Id := Empty;
1098 -- In Ada 2005, indicates partial parameterization of a formal
1099 -- package. As usual an other association must be last in the list.
1101 procedure Build_Subprogram_Wrappers;
1102 -- Ada 2020: AI12-0272 introduces pre/postconditions for formal
1103 -- subprograms. The implementation of making the formal into a renaming
1104 -- of the actual does not work, given that subprogram renaming cannot
1105 -- carry aspect specifications. Instead we must create subprogram
1106 -- wrappers whose body is a call to the actual, and whose declaration
1107 -- carries the aspects of the formal.
1109 procedure Check_Fixed_Point_Actual (Actual : Node_Id);
1110 -- Warn if an actual fixed-point type has user-defined arithmetic
1111 -- operations, but there is no corresponding formal in the generic,
1112 -- in which case the predefined operations will be used. This merits
1113 -- a warning because of the special semantics of fixed point ops.
1115 procedure Check_Overloaded_Formal_Subprogram (Formal : Entity_Id);
1116 -- Apply RM 12.3(9): if a formal subprogram is overloaded, the instance
1117 -- cannot have a named association for it. AI05-0025 extends this rule
1118 -- to formals of formal packages by AI05-0025, and it also applies to
1119 -- box-initialized formals.
1121 function Has_Fully_Defined_Profile (Subp : Entity_Id) return Boolean;
1122 -- Determine whether the parameter types and the return type of Subp
1123 -- are fully defined at the point of instantiation.
1125 function Matching_Actual
1127 A_F : Entity_Id) return Node_Id;
1128 -- Find actual that corresponds to a given a formal parameter. If the
1129 -- actuals are positional, return the next one, if any. If the actuals
1130 -- are named, scan the parameter associations to find the right one.
1131 -- A_F is the corresponding entity in the analyzed generic, which is
1132 -- placed on the selector name.
1134 -- In Ada 2005, a named association may be given with a box, in which
1135 -- case Matching_Actual sets Found_Assoc to the generic association,
1136 -- but return Empty for the actual itself. In this case the code below
1137 -- creates a corresponding declaration for the formal.
1139 function Partial_Parameterization return Boolean;
1140 -- Ada 2005: if no match is found for a given formal, check if the
1141 -- association for it includes a box, or whether the associations
1142 -- include an Others clause.
1144 procedure Process_Default (F : Entity_Id);
1145 -- Add a copy of the declaration of generic formal F to the list of
1146 -- associations, and add an explicit box association for F if there
1147 -- is none yet, and the default comes from an Others_Choice.
1149 function Renames_Standard_Subprogram (Subp : Entity_Id) return Boolean;
1150 -- Determine whether Subp renames one of the subprograms defined in the
1151 -- generated package Standard.
1153 procedure Set_Analyzed_Formal;
1154 -- Find the node in the generic copy that corresponds to a given formal.
1155 -- The semantic information on this node is used to perform legality
1156 -- checks on the actuals. Because semantic analysis can introduce some
1157 -- anonymous entities or modify the declaration node itself, the
1158 -- correspondence between the two lists is not one-one. In addition to
1159 -- anonymous types, the presence a formal equality will introduce an
1160 -- implicit declaration for the corresponding inequality.
1162 -----------------------------------------
1163 -- procedure Build_Subprogram_Wrappers --
1164 -----------------------------------------
1166 procedure Build_Subprogram_Wrappers is
1167 Formal : constant Entity_Id :=
1168 Defining_Unit_Name (Specification (Analyzed_Formal));
1169 Aspect_Spec : Node_Id;
1170 Decl_Node : Node_Id;
1171 Actual_Name : Node_Id;
1174 -- Create declaration for wrapper subprogram
1175 -- The actual can be overloaded, in which case it will be
1176 -- resolved when the call in the wrapper body is analyzed.
1177 -- We attach the possible interpretations of the actual to
1178 -- the name to be used in the call in the wrapper body.
1180 if Is_Entity_Name (Match) then
1181 Actual_Name := New_Occurrence_Of (Entity (Match), Sloc (Match));
1183 if Is_Overloaded (Match) then
1184 Save_Interps (Match, Actual_Name);
1188 -- Use renaming declaration created when analyzing actual.
1189 -- This may be incomplete if there are several formal
1190 -- subprograms whose actual is an attribute ???
1193 Renaming_Decl : constant Node_Id := Last (Assoc_List);
1196 Actual_Name := New_Occurrence_Of
1197 (Defining_Entity (Renaming_Decl), Sloc (Match));
1198 Set_Etype (Actual_Name, Get_Instance_Of (Etype (Formal)));
1202 Decl_Node := Build_Subprogram_Decl_Wrapper (Formal);
1204 -- Transfer aspect specifications from formal subprogram to wrapper
1206 Set_Aspect_Specifications (Decl_Node,
1207 New_Copy_List_Tree (Aspect_Specifications (Analyzed_Formal)));
1209 Aspect_Spec := First (Aspect_Specifications (Decl_Node));
1210 while Present (Aspect_Spec) loop
1211 Set_Analyzed (Aspect_Spec, False);
1215 Append_To (Assoc_List, Decl_Node);
1217 -- Create corresponding body, and append it to association list
1218 -- that appears at the head of the declarations in the instance.
1219 -- The subprogram may be called in the analysis of subsequent
1222 Append_To (Assoc_List,
1223 Build_Subprogram_Body_Wrapper (Formal, Actual_Name));
1224 end Build_Subprogram_Wrappers;
1226 ----------------------------------------
1227 -- Check_Overloaded_Formal_Subprogram --
1228 ----------------------------------------
1230 procedure Check_Overloaded_Formal_Subprogram (Formal : Entity_Id) is
1231 Temp_Formal : Entity_Id;
1234 Temp_Formal := First (Formals);
1235 while Present (Temp_Formal) loop
1236 if Nkind (Temp_Formal) in N_Formal_Subprogram_Declaration
1237 and then Temp_Formal /= Formal
1239 Chars (Defining_Unit_Name (Specification (Formal))) =
1240 Chars (Defining_Unit_Name (Specification (Temp_Formal)))
1242 if Present (Found_Assoc) then
1244 ("named association not allowed for overloaded formal",
1249 ("named association not allowed for overloaded formal",
1253 Abandon_Instantiation (Instantiation_Node);
1258 end Check_Overloaded_Formal_Subprogram;
1260 -------------------------------
1261 -- Check_Fixed_Point_Actual --
1262 -------------------------------
1264 procedure Check_Fixed_Point_Actual (Actual : Node_Id) is
1265 Typ : constant Entity_Id := Entity (Actual);
1266 Prims : constant Elist_Id := Collect_Primitive_Operations (Typ);
1272 -- Locate primitive operations of the type that are arithmetic
1275 Elem := First_Elmt (Prims);
1276 while Present (Elem) loop
1277 if Nkind (Node (Elem)) = N_Defining_Operator_Symbol then
1279 -- Check whether the generic unit has a formal subprogram of
1280 -- the same name. This does not check types but is good enough
1281 -- to justify a warning.
1283 Formal := First_Non_Pragma (Formals);
1284 Op := Alias (Node (Elem));
1286 while Present (Formal) loop
1287 if Nkind (Formal) = N_Formal_Concrete_Subprogram_Declaration
1288 and then Chars (Defining_Entity (Formal)) =
1293 elsif Nkind (Formal) = N_Formal_Package_Declaration then
1299 -- Locate corresponding actual, and check whether it
1300 -- includes a fixed-point type.
1302 Assoc := First (Assoc_List);
1303 while Present (Assoc) loop
1305 Nkind (Assoc) = N_Package_Renaming_Declaration
1306 and then Chars (Defining_Unit_Name (Assoc)) =
1307 Chars (Defining_Identifier (Formal));
1312 if Present (Assoc) then
1314 -- If formal package declares a fixed-point type,
1315 -- and the user-defined operator is derived from
1316 -- a generic instance package, the fixed-point type
1317 -- does not use the corresponding predefined op.
1319 Ent := First_Entity (Entity (Name (Assoc)));
1320 while Present (Ent) loop
1321 if Is_Fixed_Point_Type (Ent)
1322 and then Present (Op)
1323 and then Is_Generic_Instance (Scope (Op))
1338 Error_Msg_Sloc := Sloc (Node (Elem));
1340 ("?instance uses predefined operation, not primitive "
1341 & "operation&#", Actual, Node (Elem));
1347 end Check_Fixed_Point_Actual;
1349 -------------------------------
1350 -- Has_Fully_Defined_Profile --
1351 -------------------------------
1353 function Has_Fully_Defined_Profile (Subp : Entity_Id) return Boolean is
1354 function Is_Fully_Defined_Type (Typ : Entity_Id) return Boolean;
1355 -- Determine whethet type Typ is fully defined
1357 ---------------------------
1358 -- Is_Fully_Defined_Type --
1359 ---------------------------
1361 function Is_Fully_Defined_Type (Typ : Entity_Id) return Boolean is
1363 -- A private type without a full view is not fully defined
1365 if Is_Private_Type (Typ)
1366 and then No (Full_View (Typ))
1370 -- An incomplete type is never fully defined
1372 elsif Is_Incomplete_Type (Typ) then
1375 -- All other types are fully defined
1380 end Is_Fully_Defined_Type;
1382 -- Local declarations
1386 -- Start of processing for Has_Fully_Defined_Profile
1389 -- Check the parameters
1391 Param := First_Formal (Subp);
1392 while Present (Param) loop
1393 if not Is_Fully_Defined_Type (Etype (Param)) then
1397 Next_Formal (Param);
1400 -- Check the return type
1402 return Is_Fully_Defined_Type (Etype (Subp));
1403 end Has_Fully_Defined_Profile;
1405 ---------------------
1406 -- Matching_Actual --
1407 ---------------------
1409 function Matching_Actual
1411 A_F : Entity_Id) return Node_Id
1417 Is_Named_Assoc := False;
1419 -- End of list of purely positional parameters
1421 if No (Actual) or else Nkind (Actual) = N_Others_Choice then
1422 Found_Assoc := Empty;
1425 -- Case of positional parameter corresponding to current formal
1427 elsif No (Selector_Name (Actual)) then
1428 Found_Assoc := Actual;
1429 Act := Explicit_Generic_Actual_Parameter (Actual);
1430 Num_Matched := Num_Matched + 1;
1433 -- Otherwise scan list of named actuals to find the one with the
1434 -- desired name. All remaining actuals have explicit names.
1437 Is_Named_Assoc := True;
1438 Found_Assoc := Empty;
1442 while Present (Actual) loop
1443 if Nkind (Actual) = N_Others_Choice then
1444 Found_Assoc := Empty;
1447 elsif Chars (Selector_Name (Actual)) = Chars (F) then
1448 Set_Entity (Selector_Name (Actual), A_F);
1449 Set_Etype (Selector_Name (Actual), Etype (A_F));
1450 Generate_Reference (A_F, Selector_Name (Actual));
1452 Found_Assoc := Actual;
1453 Act := Explicit_Generic_Actual_Parameter (Actual);
1454 Num_Matched := Num_Matched + 1;
1462 -- Reset for subsequent searches. In most cases the named
1463 -- associations are in order. If they are not, we reorder them
1464 -- to avoid scanning twice the same actual. This is not just a
1465 -- question of efficiency: there may be multiple defaults with
1466 -- boxes that have the same name. In a nested instantiation we
1467 -- insert actuals for those defaults, and cannot rely on their
1468 -- names to disambiguate them.
1470 if Actual = First_Named then
1473 elsif Present (Actual) then
1474 Insert_Before (First_Named, Remove_Next (Prev));
1477 Actual := First_Named;
1480 if Is_Entity_Name (Act) and then Present (Entity (Act)) then
1481 Set_Used_As_Generic_Actual (Entity (Act));
1485 end Matching_Actual;
1487 ------------------------------
1488 -- Partial_Parameterization --
1489 ------------------------------
1491 function Partial_Parameterization return Boolean is
1493 return Others_Present
1494 or else (Present (Found_Assoc) and then Box_Present (Found_Assoc));
1495 end Partial_Parameterization;
1497 ---------------------
1498 -- Process_Default --
1499 ---------------------
1501 procedure Process_Default (F : Entity_Id) is
1502 Loc : constant Source_Ptr := Sloc (I_Node);
1503 F_Id : constant Entity_Id := Defining_Entity (F);
1509 -- Append copy of formal declaration to associations, and create new
1510 -- defining identifier for it.
1512 Decl := New_Copy_Tree (F);
1513 Id := Make_Defining_Identifier (Sloc (F_Id), Chars (F_Id));
1515 if Nkind (F) in N_Formal_Subprogram_Declaration then
1516 Set_Defining_Unit_Name (Specification (Decl), Id);
1519 Set_Defining_Identifier (Decl, Id);
1522 Append (Decl, Assoc_List);
1524 if No (Found_Assoc) then
1526 Make_Generic_Association (Loc,
1528 New_Occurrence_Of (Id, Loc),
1529 Explicit_Generic_Actual_Parameter => Empty);
1530 Set_Box_Present (Default);
1531 Append (Default, Default_Formals);
1533 end Process_Default;
1535 ---------------------------------
1536 -- Renames_Standard_Subprogram --
1537 ---------------------------------
1539 function Renames_Standard_Subprogram (Subp : Entity_Id) return Boolean is
1544 while Present (Id) loop
1545 if Scope (Id) = Standard_Standard then
1553 end Renames_Standard_Subprogram;
1555 -------------------------
1556 -- Set_Analyzed_Formal --
1557 -------------------------
1559 procedure Set_Analyzed_Formal is
1563 while Present (Analyzed_Formal) loop
1564 Kind := Nkind (Analyzed_Formal);
1566 case Nkind (Formal) is
1567 when N_Formal_Subprogram_Declaration =>
1568 exit when Kind in N_Formal_Subprogram_Declaration
1571 (Defining_Unit_Name (Specification (Formal))) =
1573 (Defining_Unit_Name (Specification (Analyzed_Formal)));
1575 when N_Formal_Package_Declaration =>
1576 exit when Nkind_In (Kind, N_Formal_Package_Declaration,
1577 N_Generic_Package_Declaration,
1578 N_Package_Declaration);
1580 when N_Use_Package_Clause
1587 -- Skip freeze nodes, and nodes inserted to replace
1588 -- unrecognized pragmas.
1591 Kind not in N_Formal_Subprogram_Declaration
1592 and then not Nkind_In (Kind, N_Subprogram_Declaration,
1596 and then Chars (Defining_Identifier (Formal)) =
1597 Chars (Defining_Identifier (Analyzed_Formal));
1600 Next (Analyzed_Formal);
1602 end Set_Analyzed_Formal;
1604 -- Start of processing for Analyze_Associations
1607 Actuals := Generic_Associations (I_Node);
1609 if Present (Actuals) then
1611 -- Check for an Others choice, indicating a partial parameterization
1612 -- for a formal package.
1614 Actual := First (Actuals);
1615 while Present (Actual) loop
1616 if Nkind (Actual) = N_Others_Choice then
1617 Others_Present := True;
1618 Others_Choice := Actual;
1620 if Present (Next (Actual)) then
1621 Error_Msg_N ("others must be last association", Actual);
1624 -- This subprogram is used both for formal packages and for
1625 -- instantiations. For the latter, associations must all be
1628 if Nkind (I_Node) /= N_Formal_Package_Declaration
1629 and then Comes_From_Source (I_Node)
1632 ("others association not allowed in an instance",
1636 -- In any case, nothing to do after the others association
1640 elsif Box_Present (Actual)
1641 and then Comes_From_Source (I_Node)
1642 and then Nkind (I_Node) /= N_Formal_Package_Declaration
1645 ("box association not allowed in an instance", Actual);
1651 -- If named associations are present, save first named association
1652 -- (it may of course be Empty) to facilitate subsequent name search.
1654 First_Named := First (Actuals);
1655 while Present (First_Named)
1656 and then Nkind (First_Named) /= N_Others_Choice
1657 and then No (Selector_Name (First_Named))
1659 Num_Actuals := Num_Actuals + 1;
1664 Named := First_Named;
1665 while Present (Named) loop
1666 if Nkind (Named) /= N_Others_Choice
1667 and then No (Selector_Name (Named))
1669 Error_Msg_N ("invalid positional actual after named one", Named);
1670 Abandon_Instantiation (Named);
1673 -- A named association may lack an actual parameter, if it was
1674 -- introduced for a default subprogram that turns out to be local
1675 -- to the outer instantiation. If it has a box association it must
1676 -- correspond to some formal in the generic.
1678 if Nkind (Named) /= N_Others_Choice
1679 and then (Present (Explicit_Generic_Actual_Parameter (Named))
1680 or else Box_Present (Named))
1682 Num_Actuals := Num_Actuals + 1;
1688 if Present (Formals) then
1689 Formal := First_Non_Pragma (Formals);
1690 Analyzed_Formal := First_Non_Pragma (F_Copy);
1692 if Present (Actuals) then
1693 Actual := First (Actuals);
1695 -- All formals should have default values
1701 while Present (Formal) loop
1702 Set_Analyzed_Formal;
1703 Saved_Formal := Next_Non_Pragma (Formal);
1705 case Nkind (Formal) is
1706 when N_Formal_Object_Declaration =>
1709 (Defining_Identifier (Formal),
1710 Defining_Identifier (Analyzed_Formal));
1712 if No (Match) and then Partial_Parameterization then
1713 Process_Default (Formal);
1717 (Instantiate_Object (Formal, Match, Analyzed_Formal),
1720 -- For a defaulted in_parameter, create an entry in the
1721 -- the list of defaulted actuals, for GNATProve use. Do
1722 -- not included these defaults for an instance nested
1723 -- within a generic, because the defaults are also used
1724 -- in the analysis of the enclosing generic, and only
1725 -- defaulted subprograms are relevant there.
1727 if No (Match) and then not Inside_A_Generic then
1728 Append_To (Default_Actuals,
1729 Make_Generic_Association (Sloc (I_Node),
1732 (Defining_Identifier (Formal), Sloc (I_Node)),
1733 Explicit_Generic_Actual_Parameter =>
1734 New_Copy_Tree (Default_Expression (Formal))));
1738 -- If the object is a call to an expression function, this
1739 -- is a freezing point for it.
1741 if Is_Entity_Name (Match)
1742 and then Present (Entity (Match))
1744 (Original_Node (Unit_Declaration_Node (Entity (Match))))
1745 = N_Expression_Function
1747 Append_Elmt (Entity (Match), Actuals_To_Freeze);
1750 when N_Formal_Type_Declaration =>
1753 (Defining_Identifier (Formal),
1754 Defining_Identifier (Analyzed_Formal));
1757 if Partial_Parameterization then
1758 Process_Default (Formal);
1761 Error_Msg_Sloc := Sloc (Gen_Unit);
1764 Instantiation_Node, Defining_Identifier (Formal));
1766 ("\in instantiation of & declared#",
1767 Instantiation_Node, Gen_Unit);
1768 Abandon_Instantiation (Instantiation_Node);
1775 (Formal, Match, Analyzed_Formal, Assoc_List),
1778 -- Warn when an actual is a fixed-point with user-
1779 -- defined promitives. The warning is superfluous
1780 -- if the formal is private, because there can be
1781 -- no arithmetic operations in the generic so there
1782 -- no danger of confusion.
1784 if Is_Fixed_Point_Type (Entity (Match))
1785 and then not Is_Private_Type
1786 (Defining_Identifier (Analyzed_Formal))
1788 Check_Fixed_Point_Actual (Match);
1791 -- An instantiation is a freeze point for the actuals,
1792 -- unless this is a rewritten formal package, or the
1793 -- formal is an Ada 2012 formal incomplete type.
1795 if Nkind (I_Node) = N_Formal_Package_Declaration
1797 (Ada_Version >= Ada_2012
1799 Ekind (Defining_Identifier (Analyzed_Formal)) =
1805 Append_Elmt (Entity (Match), Actuals_To_Freeze);
1809 -- A remote access-to-class-wide type is not a legal actual
1810 -- for a generic formal of an access type (E.2.2(17/2)).
1811 -- In GNAT an exception to this rule is introduced when
1812 -- the formal is marked as remote using implementation
1813 -- defined aspect/pragma Remote_Access_Type. In that case
1814 -- the actual must be remote as well.
1816 -- If the current instantiation is the construction of a
1817 -- local copy for a formal package the actuals may be
1818 -- defaulted, and there is no matching actual to check.
1820 if Nkind (Analyzed_Formal) = N_Formal_Type_Declaration
1822 Nkind (Formal_Type_Definition (Analyzed_Formal)) =
1823 N_Access_To_Object_Definition
1824 and then Present (Match)
1827 Formal_Ent : constant Entity_Id :=
1828 Defining_Identifier (Analyzed_Formal);
1830 if Is_Remote_Access_To_Class_Wide_Type (Entity (Match))
1831 = Is_Remote_Types (Formal_Ent)
1833 -- Remoteness of formal and actual match
1837 elsif Is_Remote_Types (Formal_Ent) then
1839 -- Remote formal, non-remote actual
1842 ("actual for& must be remote", Match, Formal_Ent);
1845 -- Non-remote formal, remote actual
1848 ("actual for& may not be remote",
1854 when N_Formal_Subprogram_Declaration =>
1857 (Defining_Unit_Name (Specification (Formal)),
1858 Defining_Unit_Name (Specification (Analyzed_Formal)));
1860 -- If the formal subprogram has the same name as another
1861 -- formal subprogram of the generic, then a named
1862 -- association is illegal (12.3(9)). Exclude named
1863 -- associations that are generated for a nested instance.
1866 and then Is_Named_Assoc
1867 and then Comes_From_Source (Found_Assoc)
1869 Check_Overloaded_Formal_Subprogram (Formal);
1872 -- If there is no corresponding actual, this may be case
1873 -- of partial parameterization, or else the formal has a
1874 -- default or a box.
1876 if No (Match) and then Partial_Parameterization then
1877 Process_Default (Formal);
1879 if Nkind (I_Node) = N_Formal_Package_Declaration then
1880 Check_Overloaded_Formal_Subprogram (Formal);
1884 Append_To (Assoc_List,
1885 Instantiate_Formal_Subprogram
1886 (Formal, Match, Analyzed_Formal));
1888 -- If formal subprogram has contracts, create wrappers
1889 -- for it. This is an expansion activity that cannot
1890 -- take place e.g. within an enclosing generic unit.
1892 if Has_Contracts (Analyzed_Formal)
1893 and then Expander_Active
1895 Build_Subprogram_Wrappers;
1898 -- An instantiation is a freeze point for the actuals,
1899 -- unless this is a rewritten formal package.
1901 if Nkind (I_Node) /= N_Formal_Package_Declaration
1902 and then Nkind (Match) = N_Identifier
1903 and then Is_Subprogram (Entity (Match))
1905 -- The actual subprogram may rename a routine defined
1906 -- in Standard. Avoid freezing such renamings because
1907 -- subprograms coming from Standard cannot be frozen.
1910 not Renames_Standard_Subprogram (Entity (Match))
1912 -- If the actual subprogram comes from a different
1913 -- unit, it is already frozen, either by a body in
1914 -- that unit or by the end of the declarative part
1915 -- of the unit. This check avoids the freezing of
1916 -- subprograms defined in Standard which are used
1917 -- as generic actuals.
1919 and then In_Same_Code_Unit (Entity (Match), I_Node)
1920 and then Has_Fully_Defined_Profile (Entity (Match))
1922 -- Mark the subprogram as having a delayed freeze
1923 -- since this may be an out-of-order action.
1925 Set_Has_Delayed_Freeze (Entity (Match));
1926 Append_Elmt (Entity (Match), Actuals_To_Freeze);
1930 -- If this is a nested generic, preserve default for later
1931 -- instantiations. We do this as well for GNATProve use,
1932 -- so that the list of generic associations is complete.
1934 if No (Match) and then Box_Present (Formal) then
1936 Subp : constant Entity_Id :=
1938 (Specification (Last (Assoc_List)));
1941 Append_To (Default_Actuals,
1942 Make_Generic_Association (Sloc (I_Node),
1944 New_Occurrence_Of (Subp, Sloc (I_Node)),
1945 Explicit_Generic_Actual_Parameter =>
1946 New_Occurrence_Of (Subp, Sloc (I_Node))));
1950 when N_Formal_Package_Declaration =>
1953 (Defining_Identifier (Formal),
1954 Defining_Identifier (Original_Node (Analyzed_Formal)));
1957 if Partial_Parameterization then
1958 Process_Default (Formal);
1961 Error_Msg_Sloc := Sloc (Gen_Unit);
1964 Instantiation_Node, Defining_Identifier (Formal));
1966 ("\in instantiation of & declared#",
1967 Instantiation_Node, Gen_Unit);
1969 Abandon_Instantiation (Instantiation_Node);
1975 (Instantiate_Formal_Package
1976 (Formal, Match, Analyzed_Formal),
1979 -- Determine whether the actual package needs an explicit
1980 -- freeze node. This is only the case if the actual is
1981 -- declared in the same unit and has a body. Normally
1982 -- packages do not have explicit freeze nodes, and gigi
1983 -- only uses them to elaborate entities in a package
1986 Explicit_Freeze_Check : declare
1987 Actual : constant Entity_Id := Entity (Match);
1988 Gen_Par : Entity_Id;
1990 Needs_Freezing : Boolean;
1993 procedure Check_Generic_Parent;
1994 -- The actual may be an instantiation of a unit
1995 -- declared in a previous instantiation. If that
1996 -- one is also in the current compilation, it must
1997 -- itself be frozen before the actual. The actual
1998 -- may be an instantiation of a generic child unit,
1999 -- in which case the same applies to the instance
2000 -- of the parent which must be frozen before the
2002 -- Should this itself be recursive ???
2004 --------------------------
2005 -- Check_Generic_Parent --
2006 --------------------------
2008 procedure Check_Generic_Parent is
2009 Inst : constant Node_Id :=
2010 Next (Unit_Declaration_Node (Actual));
2016 if Nkind (Parent (Actual)) = N_Package_Specification
2018 Par := Scope (Generic_Parent (Parent (Actual)));
2020 if Is_Generic_Instance (Par) then
2023 -- If the actual is a child generic unit, check
2024 -- whether the instantiation of the parent is
2025 -- also local and must also be frozen now. We
2026 -- must retrieve the instance node to locate the
2027 -- parent instance if any.
2029 elsif Ekind (Par) = E_Generic_Package
2030 and then Is_Child_Unit (Gen_Par)
2031 and then Ekind (Scope (Gen_Par)) =
2034 if Nkind (Inst) = N_Package_Instantiation
2035 and then Nkind (Name (Inst)) =
2038 -- Retrieve entity of parent instance
2040 Par := Entity (Prefix (Name (Inst)));
2049 and then Is_Generic_Instance (Par)
2050 and then Scope (Par) = Current_Scope
2052 (No (Freeze_Node (Par))
2054 not Is_List_Member (Freeze_Node (Par)))
2056 Set_Has_Delayed_Freeze (Par);
2057 Append_Elmt (Par, Actuals_To_Freeze);
2059 end Check_Generic_Parent;
2061 -- Start of processing for Explicit_Freeze_Check
2064 if Present (Renamed_Entity (Actual)) then
2066 Generic_Parent (Specification
2067 (Unit_Declaration_Node
2068 (Renamed_Entity (Actual))));
2071 Generic_Parent (Specification
2072 (Unit_Declaration_Node (Actual)));
2075 if not Expander_Active
2076 or else not Has_Completion (Actual)
2077 or else not In_Same_Source_Unit (I_Node, Actual)
2078 or else Is_Frozen (Actual)
2080 (Present (Renamed_Entity (Actual))
2082 not In_Same_Source_Unit
2083 (I_Node, (Renamed_Entity (Actual))))
2088 -- Finally we want to exclude such freeze nodes
2089 -- from statement sequences, which freeze
2090 -- everything before them.
2091 -- Is this strictly necessary ???
2093 Needs_Freezing := True;
2096 while Present (S) loop
2097 if Ekind_In (S, E_Block,
2102 Needs_Freezing := False;
2109 if Needs_Freezing then
2110 Check_Generic_Parent;
2112 -- If the actual is a renaming of a proper
2113 -- instance of the formal package, indicate
2114 -- that it is the instance that must be frozen.
2116 if Nkind (Parent (Actual)) =
2117 N_Package_Renaming_Declaration
2119 Set_Has_Delayed_Freeze
2120 (Renamed_Entity (Actual));
2122 (Renamed_Entity (Actual),
2125 Set_Has_Delayed_Freeze (Actual);
2126 Append_Elmt (Actual, Actuals_To_Freeze);
2130 end Explicit_Freeze_Check;
2133 -- For use type and use package appearing in the generic part,
2134 -- we have already copied them, so we can just move them where
2135 -- they belong (we mustn't recopy them since this would mess up
2136 -- the Sloc values).
2138 when N_Use_Package_Clause
2141 if Nkind (Original_Node (I_Node)) =
2142 N_Formal_Package_Declaration
2144 Append (New_Copy_Tree (Formal), Assoc_List);
2147 Append (Formal, Assoc_List);
2151 raise Program_Error;
2154 Formal := Saved_Formal;
2155 Next_Non_Pragma (Analyzed_Formal);
2158 if Num_Actuals > Num_Matched then
2159 Error_Msg_Sloc := Sloc (Gen_Unit);
2161 if Present (Selector_Name (Actual)) then
2163 ("unmatched actual &", Actual, Selector_Name (Actual));
2165 ("\in instantiation of & declared#", Actual, Gen_Unit);
2168 ("unmatched actual in instantiation of & declared#",
2173 elsif Present (Actuals) then
2175 ("too many actuals in generic instantiation", Instantiation_Node);
2178 -- An instantiation freezes all generic actuals. The only exceptions
2179 -- to this are incomplete types and subprograms which are not fully
2180 -- defined at the point of instantiation.
2183 Elmt : Elmt_Id := First_Elmt (Actuals_To_Freeze);
2185 while Present (Elmt) loop
2186 Freeze_Before (I_Node, Node (Elmt));
2191 -- If there are default subprograms, normalize the tree by adding
2192 -- explicit associations for them. This is required if the instance
2193 -- appears within a generic.
2195 if not Is_Empty_List (Default_Actuals) then
2200 Default := First (Default_Actuals);
2201 while Present (Default) loop
2202 Mark_Rewrite_Insertion (Default);
2206 if No (Actuals) then
2207 Set_Generic_Associations (I_Node, Default_Actuals);
2209 Append_List_To (Actuals, Default_Actuals);
2214 -- If this is a formal package, normalize the parameter list by adding
2215 -- explicit box associations for the formals that are covered by an
2218 if not Is_Empty_List (Default_Formals) then
2219 Append_List (Default_Formals, Formals);
2223 end Analyze_Associations;
2225 -------------------------------
2226 -- Analyze_Formal_Array_Type --
2227 -------------------------------
2229 procedure Analyze_Formal_Array_Type
2230 (T : in out Entity_Id;
2236 -- Treated like a non-generic array declaration, with additional
2241 if Nkind (Def) = N_Constrained_Array_Definition then
2242 DSS := First (Discrete_Subtype_Definitions (Def));
2243 while Present (DSS) loop
2244 if Nkind_In (DSS, N_Subtype_Indication,
2246 N_Attribute_Reference)
2248 Error_Msg_N ("only a subtype mark is allowed in a formal", DSS);
2255 Array_Type_Declaration (T, Def);
2256 Set_Is_Generic_Type (Base_Type (T));
2258 if Ekind (Component_Type (T)) = E_Incomplete_Type
2259 and then No (Full_View (Component_Type (T)))
2261 Error_Msg_N ("premature usage of incomplete type", Def);
2263 -- Check that range constraint is not allowed on the component type
2264 -- of a generic formal array type (AARM 12.5.3(3))
2266 elsif Is_Internal (Component_Type (T))
2267 and then Present (Subtype_Indication (Component_Definition (Def)))
2268 and then Nkind (Original_Node
2269 (Subtype_Indication (Component_Definition (Def)))) =
2270 N_Subtype_Indication
2273 ("in a formal, a subtype indication can only be "
2274 & "a subtype mark (RM 12.5.3(3))",
2275 Subtype_Indication (Component_Definition (Def)));
2278 end Analyze_Formal_Array_Type;
2280 ---------------------------------------------
2281 -- Analyze_Formal_Decimal_Fixed_Point_Type --
2282 ---------------------------------------------
2284 -- As for other generic types, we create a valid type representation with
2285 -- legal but arbitrary attributes, whose values are never considered
2286 -- static. For all scalar types we introduce an anonymous base type, with
2287 -- the same attributes. We choose the corresponding integer type to be
2288 -- Standard_Integer.
2289 -- Here and in other similar routines, the Sloc of the generated internal
2290 -- type must be the same as the sloc of the defining identifier of the
2291 -- formal type declaration, to provide proper source navigation.
2293 procedure Analyze_Formal_Decimal_Fixed_Point_Type
2297 Loc : constant Source_Ptr := Sloc (Def);
2299 Base : constant Entity_Id :=
2301 (E_Decimal_Fixed_Point_Type,
2303 Sloc (Defining_Identifier (Parent (Def))), 'G');
2305 Int_Base : constant Entity_Id := Standard_Integer;
2306 Delta_Val : constant Ureal := Ureal_1;
2307 Digs_Val : constant Uint := Uint_6;
2309 function Make_Dummy_Bound return Node_Id;
2310 -- Return a properly typed universal real literal to use as a bound
2312 ----------------------
2313 -- Make_Dummy_Bound --
2314 ----------------------
2316 function Make_Dummy_Bound return Node_Id is
2317 Bound : constant Node_Id := Make_Real_Literal (Loc, Ureal_1);
2319 Set_Etype (Bound, Universal_Real);
2321 end Make_Dummy_Bound;
2323 -- Start of processing for Analyze_Formal_Decimal_Fixed_Point_Type
2328 Set_Etype (Base, Base);
2329 Set_Size_Info (Base, Int_Base);
2330 Set_RM_Size (Base, RM_Size (Int_Base));
2331 Set_First_Rep_Item (Base, First_Rep_Item (Int_Base));
2332 Set_Digits_Value (Base, Digs_Val);
2333 Set_Delta_Value (Base, Delta_Val);
2334 Set_Small_Value (Base, Delta_Val);
2335 Set_Scalar_Range (Base,
2337 Low_Bound => Make_Dummy_Bound,
2338 High_Bound => Make_Dummy_Bound));
2340 Set_Is_Generic_Type (Base);
2341 Set_Parent (Base, Parent (Def));
2343 Set_Ekind (T, E_Decimal_Fixed_Point_Subtype);
2344 Set_Etype (T, Base);
2345 Set_Size_Info (T, Int_Base);
2346 Set_RM_Size (T, RM_Size (Int_Base));
2347 Set_First_Rep_Item (T, First_Rep_Item (Int_Base));
2348 Set_Digits_Value (T, Digs_Val);
2349 Set_Delta_Value (T, Delta_Val);
2350 Set_Small_Value (T, Delta_Val);
2351 Set_Scalar_Range (T, Scalar_Range (Base));
2352 Set_Is_Constrained (T);
2354 Check_Restriction (No_Fixed_Point, Def);
2355 end Analyze_Formal_Decimal_Fixed_Point_Type;
2357 -------------------------------------------
2358 -- Analyze_Formal_Derived_Interface_Type --
2359 -------------------------------------------
2361 procedure Analyze_Formal_Derived_Interface_Type
2366 Loc : constant Source_Ptr := Sloc (Def);
2369 -- Rewrite as a type declaration of a derived type. This ensures that
2370 -- the interface list and primitive operations are properly captured.
2373 Make_Full_Type_Declaration (Loc,
2374 Defining_Identifier => T,
2375 Type_Definition => Def));
2377 Set_Is_Generic_Type (T);
2378 end Analyze_Formal_Derived_Interface_Type;
2380 ---------------------------------
2381 -- Analyze_Formal_Derived_Type --
2382 ---------------------------------
2384 procedure Analyze_Formal_Derived_Type
2389 Loc : constant Source_Ptr := Sloc (Def);
2390 Unk_Disc : constant Boolean := Unknown_Discriminants_Present (N);
2394 Set_Is_Generic_Type (T);
2396 if Private_Present (Def) then
2398 Make_Private_Extension_Declaration (Loc,
2399 Defining_Identifier => T,
2400 Discriminant_Specifications => Discriminant_Specifications (N),
2401 Unknown_Discriminants_Present => Unk_Disc,
2402 Subtype_Indication => Subtype_Mark (Def),
2403 Interface_List => Interface_List (Def));
2405 Set_Abstract_Present (New_N, Abstract_Present (Def));
2406 Set_Limited_Present (New_N, Limited_Present (Def));
2407 Set_Synchronized_Present (New_N, Synchronized_Present (Def));
2411 Make_Full_Type_Declaration (Loc,
2412 Defining_Identifier => T,
2413 Discriminant_Specifications =>
2414 Discriminant_Specifications (Parent (T)),
2416 Make_Derived_Type_Definition (Loc,
2417 Subtype_Indication => Subtype_Mark (Def)));
2419 Set_Abstract_Present
2420 (Type_Definition (New_N), Abstract_Present (Def));
2422 (Type_Definition (New_N), Limited_Present (Def));
2429 if not Is_Composite_Type (T) then
2431 ("unknown discriminants not allowed for elementary types", N);
2433 Set_Has_Unknown_Discriminants (T);
2434 Set_Is_Constrained (T, False);
2438 -- If the parent type has a known size, so does the formal, which makes
2439 -- legal representation clauses that involve the formal.
2441 Set_Size_Known_At_Compile_Time
2442 (T, Size_Known_At_Compile_Time (Entity (Subtype_Mark (Def))));
2443 end Analyze_Formal_Derived_Type;
2445 ----------------------------------
2446 -- Analyze_Formal_Discrete_Type --
2447 ----------------------------------
2449 -- The operations defined for a discrete types are those of an enumeration
2450 -- type. The size is set to an arbitrary value, for use in analyzing the
2453 procedure Analyze_Formal_Discrete_Type (T : Entity_Id; Def : Node_Id) is
2454 Loc : constant Source_Ptr := Sloc (Def);
2458 Base : constant Entity_Id :=
2460 (E_Floating_Point_Type, Current_Scope,
2461 Sloc (Defining_Identifier (Parent (Def))), 'G');
2465 Set_Ekind (T, E_Enumeration_Subtype);
2466 Set_Etype (T, Base);
2469 Set_Is_Generic_Type (T);
2470 Set_Is_Constrained (T);
2472 -- For semantic analysis, the bounds of the type must be set to some
2473 -- non-static value. The simplest is to create attribute nodes for those
2474 -- bounds, that refer to the type itself. These bounds are never
2475 -- analyzed but serve as place-holders.
2478 Make_Attribute_Reference (Loc,
2479 Attribute_Name => Name_First,
2480 Prefix => New_Occurrence_Of (T, Loc));
2484 Make_Attribute_Reference (Loc,
2485 Attribute_Name => Name_Last,
2486 Prefix => New_Occurrence_Of (T, Loc));
2489 Set_Scalar_Range (T,
2494 Set_Ekind (Base, E_Enumeration_Type);
2495 Set_Etype (Base, Base);
2496 Init_Size (Base, 8);
2497 Init_Alignment (Base);
2498 Set_Is_Generic_Type (Base);
2499 Set_Scalar_Range (Base, Scalar_Range (T));
2500 Set_Parent (Base, Parent (Def));
2501 end Analyze_Formal_Discrete_Type;
2503 ----------------------------------
2504 -- Analyze_Formal_Floating_Type --
2505 ---------------------------------
2507 procedure Analyze_Formal_Floating_Type (T : Entity_Id; Def : Node_Id) is
2508 Base : constant Entity_Id :=
2510 (E_Floating_Point_Type, Current_Scope,
2511 Sloc (Defining_Identifier (Parent (Def))), 'G');
2514 -- The various semantic attributes are taken from the predefined type
2515 -- Float, just so that all of them are initialized. Their values are
2516 -- never used because no constant folding or expansion takes place in
2517 -- the generic itself.
2520 Set_Ekind (T, E_Floating_Point_Subtype);
2521 Set_Etype (T, Base);
2522 Set_Size_Info (T, (Standard_Float));
2523 Set_RM_Size (T, RM_Size (Standard_Float));
2524 Set_Digits_Value (T, Digits_Value (Standard_Float));
2525 Set_Scalar_Range (T, Scalar_Range (Standard_Float));
2526 Set_Is_Constrained (T);
2528 Set_Is_Generic_Type (Base);
2529 Set_Etype (Base, Base);
2530 Set_Size_Info (Base, (Standard_Float));
2531 Set_RM_Size (Base, RM_Size (Standard_Float));
2532 Set_Digits_Value (Base, Digits_Value (Standard_Float));
2533 Set_Scalar_Range (Base, Scalar_Range (Standard_Float));
2534 Set_Parent (Base, Parent (Def));
2536 Check_Restriction (No_Floating_Point, Def);
2537 end Analyze_Formal_Floating_Type;
2539 -----------------------------------
2540 -- Analyze_Formal_Interface_Type;--
2541 -----------------------------------
2543 procedure Analyze_Formal_Interface_Type
2548 Loc : constant Source_Ptr := Sloc (N);
2553 Make_Full_Type_Declaration (Loc,
2554 Defining_Identifier => T,
2555 Type_Definition => Def);
2559 Set_Is_Generic_Type (T);
2560 end Analyze_Formal_Interface_Type;
2562 ---------------------------------
2563 -- Analyze_Formal_Modular_Type --
2564 ---------------------------------
2566 procedure Analyze_Formal_Modular_Type (T : Entity_Id; Def : Node_Id) is
2568 -- Apart from their entity kind, generic modular types are treated like
2569 -- signed integer types, and have the same attributes.
2571 Analyze_Formal_Signed_Integer_Type (T, Def);
2572 Set_Ekind (T, E_Modular_Integer_Subtype);
2573 Set_Ekind (Etype (T), E_Modular_Integer_Type);
2575 end Analyze_Formal_Modular_Type;
2577 ---------------------------------------
2578 -- Analyze_Formal_Object_Declaration --
2579 ---------------------------------------
2581 procedure Analyze_Formal_Object_Declaration (N : Node_Id) is
2582 E : constant Node_Id := Default_Expression (N);
2583 Id : constant Node_Id := Defining_Identifier (N);
2590 -- Determine the mode of the formal object
2592 if Out_Present (N) then
2593 K := E_Generic_In_Out_Parameter;
2595 if not In_Present (N) then
2596 Error_Msg_N ("formal generic objects cannot have mode OUT", N);
2600 K := E_Generic_In_Parameter;
2603 if Present (Subtype_Mark (N)) then
2604 Find_Type (Subtype_Mark (N));
2605 T := Entity (Subtype_Mark (N));
2607 -- Verify that there is no redundant null exclusion
2609 if Null_Exclusion_Present (N) then
2610 if not Is_Access_Type (T) then
2612 ("null exclusion can only apply to an access type", N);
2614 elsif Can_Never_Be_Null (T) then
2616 ("`NOT NULL` not allowed (& already excludes null)", N, T);
2620 -- Ada 2005 (AI-423): Formal object with an access definition
2623 Check_Access_Definition (N);
2624 T := Access_Definition
2626 N => Access_Definition (N));
2629 if Ekind (T) = E_Incomplete_Type then
2631 Error_Node : Node_Id;
2634 if Present (Subtype_Mark (N)) then
2635 Error_Node := Subtype_Mark (N);
2637 Check_Access_Definition (N);
2638 Error_Node := Access_Definition (N);
2641 Error_Msg_N ("premature usage of incomplete type", Error_Node);
2645 if K = E_Generic_In_Parameter then
2647 -- Ada 2005 (AI-287): Limited aggregates allowed in generic formals
2649 if Ada_Version < Ada_2005 and then Is_Limited_Type (T) then
2651 ("generic formal of mode IN must not be of limited type", N);
2652 Explain_Limited_Type (T, N);
2655 if Is_Abstract_Type (T) then
2657 ("generic formal of mode IN must not be of abstract type", N);
2661 Preanalyze_Spec_Expression (E, T);
2663 if Is_Limited_Type (T) and then not OK_For_Limited_Init (T, E) then
2665 ("initialization not allowed for limited types", E);
2666 Explain_Limited_Type (T, E);
2673 -- Case of generic IN OUT parameter
2676 -- If the formal has an unconstrained type, construct its actual
2677 -- subtype, as is done for subprogram formals. In this fashion, all
2678 -- its uses can refer to specific bounds.
2683 if (Is_Array_Type (T) and then not Is_Constrained (T))
2684 or else (Ekind (T) = E_Record_Type and then Has_Discriminants (T))
2687 Non_Freezing_Ref : constant Node_Id :=
2688 New_Occurrence_Of (Id, Sloc (Id));
2692 -- Make sure the actual subtype doesn't generate bogus freezing
2694 Set_Must_Not_Freeze (Non_Freezing_Ref);
2695 Decl := Build_Actual_Subtype (T, Non_Freezing_Ref);
2696 Insert_Before_And_Analyze (N, Decl);
2697 Set_Actual_Subtype (Id, Defining_Identifier (Decl));
2700 Set_Actual_Subtype (Id, T);
2705 ("initialization not allowed for `IN OUT` formals", N);
2709 if Has_Aspects (N) then
2710 Analyze_Aspect_Specifications (N, Id);
2712 end Analyze_Formal_Object_Declaration;
2714 ----------------------------------------------
2715 -- Analyze_Formal_Ordinary_Fixed_Point_Type --
2716 ----------------------------------------------
2718 procedure Analyze_Formal_Ordinary_Fixed_Point_Type
2722 Loc : constant Source_Ptr := Sloc (Def);
2723 Base : constant Entity_Id :=
2725 (E_Ordinary_Fixed_Point_Type, Current_Scope,
2726 Sloc (Defining_Identifier (Parent (Def))), 'G');
2729 -- The semantic attributes are set for completeness only, their values
2730 -- will never be used, since all properties of the type are non-static.
2733 Set_Ekind (T, E_Ordinary_Fixed_Point_Subtype);
2734 Set_Etype (T, Base);
2735 Set_Size_Info (T, Standard_Integer);
2736 Set_RM_Size (T, RM_Size (Standard_Integer));
2737 Set_Small_Value (T, Ureal_1);
2738 Set_Delta_Value (T, Ureal_1);
2739 Set_Scalar_Range (T,
2741 Low_Bound => Make_Real_Literal (Loc, Ureal_1),
2742 High_Bound => Make_Real_Literal (Loc, Ureal_1)));
2743 Set_Is_Constrained (T);
2745 Set_Is_Generic_Type (Base);
2746 Set_Etype (Base, Base);
2747 Set_Size_Info (Base, Standard_Integer);
2748 Set_RM_Size (Base, RM_Size (Standard_Integer));
2749 Set_Small_Value (Base, Ureal_1);
2750 Set_Delta_Value (Base, Ureal_1);
2751 Set_Scalar_Range (Base, Scalar_Range (T));
2752 Set_Parent (Base, Parent (Def));
2754 Check_Restriction (No_Fixed_Point, Def);
2755 end Analyze_Formal_Ordinary_Fixed_Point_Type;
2757 ----------------------------------------
2758 -- Analyze_Formal_Package_Declaration --
2759 ----------------------------------------
2761 procedure Analyze_Formal_Package_Declaration (N : Node_Id) is
2762 Gen_Id : constant Node_Id := Name (N);
2763 Loc : constant Source_Ptr := Sloc (N);
2764 Pack_Id : constant Entity_Id := Defining_Identifier (N);
2767 Gen_Unit : Entity_Id;
2770 Vis_Prims_List : Elist_Id := No_Elist;
2771 -- List of primitives made temporarily visible in the instantiation
2772 -- to match the visibility of the formal type.
2774 function Build_Local_Package return Node_Id;
2775 -- The formal package is rewritten so that its parameters are replaced
2776 -- with corresponding declarations. For parameters with bona fide
2777 -- associations these declarations are created by Analyze_Associations
2778 -- as for a regular instantiation. For boxed parameters, we preserve
2779 -- the formal declarations and analyze them, in order to introduce
2780 -- entities of the right kind in the environment of the formal.
2782 -------------------------
2783 -- Build_Local_Package --
2784 -------------------------
2786 function Build_Local_Package return Node_Id is
2788 Pack_Decl : Node_Id;
2791 -- Within the formal, the name of the generic package is a renaming
2792 -- of the formal (as for a regular instantiation).
2795 Make_Package_Declaration (Loc,
2798 (Specification (Original_Node (Gen_Decl)),
2799 Empty, Instantiating => True));
2802 Make_Package_Renaming_Declaration (Loc,
2803 Defining_Unit_Name =>
2804 Make_Defining_Identifier (Loc, Chars (Gen_Unit)),
2805 Name => New_Occurrence_Of (Formal, Loc));
2807 if Nkind (Gen_Id) = N_Identifier
2808 and then Chars (Gen_Id) = Chars (Pack_Id)
2811 ("& is hidden within declaration of instance", Gen_Id, Gen_Unit);
2814 -- If the formal is declared with a box, or with an others choice,
2815 -- create corresponding declarations for all entities in the formal
2816 -- part, so that names with the proper types are available in the
2817 -- specification of the formal package.
2819 -- On the other hand, if there are no associations, then all the
2820 -- formals must have defaults, and this will be checked by the
2821 -- call to Analyze_Associations.
2824 or else Nkind (First (Generic_Associations (N))) = N_Others_Choice
2827 Formal_Decl : Node_Id;
2830 -- TBA : for a formal package, need to recurse ???
2835 (Generic_Formal_Declarations (Original_Node (Gen_Decl)));
2836 while Present (Formal_Decl) loop
2840 (Formal_Decl, Empty, Instantiating => True));
2845 -- If generic associations are present, use Analyze_Associations to
2846 -- create the proper renaming declarations.
2850 Act_Tree : constant Node_Id :=
2852 (Original_Node (Gen_Decl), Empty,
2853 Instantiating => True);
2856 Generic_Renamings.Set_Last (0);
2857 Generic_Renamings_HTable.Reset;
2858 Instantiation_Node := N;
2861 Analyze_Associations
2862 (I_Node => Original_Node (N),
2863 Formals => Generic_Formal_Declarations (Act_Tree),
2864 F_Copy => Generic_Formal_Declarations (Gen_Decl));
2866 Vis_Prims_List := Check_Hidden_Primitives (Decls);
2870 Append (Renaming, To => Decls);
2872 -- Add generated declarations ahead of local declarations in
2875 if No (Visible_Declarations (Specification (Pack_Decl))) then
2876 Set_Visible_Declarations (Specification (Pack_Decl), Decls);
2879 (First (Visible_Declarations (Specification (Pack_Decl))),
2884 end Build_Local_Package;
2888 Save_ISMP : constant Boolean := Ignore_SPARK_Mode_Pragmas_In_Instance;
2889 -- Save flag Ignore_SPARK_Mode_Pragmas_In_Instance for restore on exit
2891 Associations : Boolean := True;
2893 Parent_Installed : Boolean := False;
2894 Parent_Instance : Entity_Id;
2895 Renaming_In_Par : Entity_Id;
2897 -- Start of processing for Analyze_Formal_Package_Declaration
2900 Check_Text_IO_Special_Unit (Gen_Id);
2903 Check_Generic_Child_Unit (Gen_Id, Parent_Installed);
2904 Gen_Unit := Entity (Gen_Id);
2906 -- Check for a formal package that is a package renaming
2908 if Present (Renamed_Object (Gen_Unit)) then
2910 -- Indicate that unit is used, before replacing it with renamed
2911 -- entity for use below.
2913 if In_Extended_Main_Source_Unit (N) then
2914 Set_Is_Instantiated (Gen_Unit);
2915 Generate_Reference (Gen_Unit, N);
2918 Gen_Unit := Renamed_Object (Gen_Unit);
2921 if Ekind (Gen_Unit) /= E_Generic_Package then
2922 Error_Msg_N ("expect generic package name", Gen_Id);
2926 elsif Gen_Unit = Current_Scope then
2928 ("generic package cannot be used as a formal package of itself",
2933 elsif In_Open_Scopes (Gen_Unit) then
2934 if Is_Compilation_Unit (Gen_Unit)
2935 and then Is_Child_Unit (Current_Scope)
2937 -- Special-case the error when the formal is a parent, and
2938 -- continue analysis to minimize cascaded errors.
2941 ("generic parent cannot be used as formal package of a child "
2946 ("generic package cannot be used as a formal package within "
2947 & "itself", Gen_Id);
2953 -- Check that name of formal package does not hide name of generic,
2954 -- or its leading prefix. This check must be done separately because
2955 -- the name of the generic has already been analyzed.
2958 Gen_Name : Entity_Id;
2962 while Nkind (Gen_Name) = N_Expanded_Name loop
2963 Gen_Name := Prefix (Gen_Name);
2966 if Chars (Gen_Name) = Chars (Pack_Id) then
2968 ("& is hidden within declaration of formal package",
2974 or else No (Generic_Associations (N))
2975 or else Nkind (First (Generic_Associations (N))) = N_Others_Choice
2977 Associations := False;
2980 -- If there are no generic associations, the generic parameters appear
2981 -- as local entities and are instantiated like them. We copy the generic
2982 -- package declaration as if it were an instantiation, and analyze it
2983 -- like a regular package, except that we treat the formals as
2984 -- additional visible components.
2986 Gen_Decl := Unit_Declaration_Node (Gen_Unit);
2988 if In_Extended_Main_Source_Unit (N) then
2989 Set_Is_Instantiated (Gen_Unit);
2990 Generate_Reference (Gen_Unit, N);
2993 Formal := New_Copy (Pack_Id);
2994 Create_Instantiation_Source (N, Gen_Unit, S_Adjustment);
2996 -- Make local generic without formals. The formals will be replaced with
2997 -- internal declarations.
3000 New_N := Build_Local_Package;
3002 -- If there are errors in the parameter list, Analyze_Associations
3003 -- raises Instantiation_Error. Patch the declaration to prevent further
3004 -- exception propagation.
3007 when Instantiation_Error =>
3008 Enter_Name (Formal);
3009 Set_Ekind (Formal, E_Variable);
3010 Set_Etype (Formal, Any_Type);
3011 Restore_Hidden_Primitives (Vis_Prims_List);
3013 if Parent_Installed then
3021 Set_Defining_Unit_Name (Specification (New_N), Formal);
3022 Set_Generic_Parent (Specification (N), Gen_Unit);
3023 Set_Instance_Env (Gen_Unit, Formal);
3024 Set_Is_Generic_Instance (Formal);
3026 Enter_Name (Formal);
3027 Set_Ekind (Formal, E_Package);
3028 Set_Etype (Formal, Standard_Void_Type);
3029 Set_Inner_Instances (Formal, New_Elmt_List);
3031 -- It is unclear that any aspects can apply to a formal package
3032 -- declaration, given that they look like a hidden conformance
3033 -- requirement on the corresponding actual. However, Abstract_State
3034 -- must be treated specially because it generates declarations that
3035 -- must appear before other declarations in the specification and
3036 -- must be analyzed at once.
3038 if Present (Aspect_Specifications (Gen_Decl)) then
3039 if No (Aspect_Specifications (N)) then
3040 Set_Aspect_Specifications (N, New_List);
3041 Set_Has_Aspects (N);
3045 ASN : Node_Id := First (Aspect_Specifications (Gen_Decl));
3049 while Present (ASN) loop
3050 if Get_Aspect_Id (ASN) = Aspect_Abstract_State then
3052 Copy_Generic_Node (ASN, Empty, Instantiating => True);
3053 Set_Entity (New_A, Formal);
3054 Set_Analyzed (New_A, False);
3055 Append (New_A, Aspect_Specifications (N));
3056 Analyze_Aspect_Specifications (N, Formal);
3065 Push_Scope (Formal);
3067 -- Manually set the SPARK_Mode from the context because the package
3068 -- declaration is never analyzed.
3070 Set_SPARK_Pragma (Formal, SPARK_Mode_Pragma);
3071 Set_SPARK_Aux_Pragma (Formal, SPARK_Mode_Pragma);
3072 Set_SPARK_Pragma_Inherited (Formal);
3073 Set_SPARK_Aux_Pragma_Inherited (Formal);
3075 if Is_Child_Unit (Gen_Unit) and then Parent_Installed then
3077 -- Similarly, we have to make the name of the formal visible in the
3078 -- parent instance, to resolve properly fully qualified names that
3079 -- may appear in the generic unit. The parent instance has been
3080 -- placed on the scope stack ahead of the current scope.
3082 Parent_Instance := Scope_Stack.Table (Scope_Stack.Last - 1).Entity;
3085 Make_Defining_Identifier (Loc, Chars (Gen_Unit));
3086 Set_Ekind (Renaming_In_Par, E_Package);
3087 Set_Etype (Renaming_In_Par, Standard_Void_Type);
3088 Set_Scope (Renaming_In_Par, Parent_Instance);
3089 Set_Parent (Renaming_In_Par, Parent (Formal));
3090 Set_Renamed_Object (Renaming_In_Par, Formal);
3091 Append_Entity (Renaming_In_Par, Parent_Instance);
3094 -- A formal package declaration behaves as a package instantiation with
3095 -- respect to SPARK_Mode "off". If the annotation is "off" or altogether
3096 -- missing, set the global flag which signals Analyze_Pragma to ingnore
3097 -- all SPARK_Mode pragmas within the generic_package_name.
3099 if SPARK_Mode /= On then
3100 Ignore_SPARK_Mode_Pragmas_In_Instance := True;
3102 -- Mark the formal spec in case the body is instantiated at a later
3103 -- pass. This preserves the original context in effect for the body.
3105 Set_Ignore_SPARK_Mode_Pragmas (Formal);
3108 Analyze (Specification (N));
3110 -- The formals for which associations are provided are not visible
3111 -- outside of the formal package. The others are still declared by a
3112 -- formal parameter declaration.
3114 -- If there are no associations, the only local entity to hide is the
3115 -- generated package renaming itself.
3121 E := First_Entity (Formal);
3122 while Present (E) loop
3123 if Associations and then not Is_Generic_Formal (E) then
3127 if Ekind (E) = E_Package and then Renamed_Entity (E) = Formal then
3136 End_Package_Scope (Formal);
3137 Restore_Hidden_Primitives (Vis_Prims_List);
3139 if Parent_Installed then
3145 -- Inside the generic unit, the formal package is a regular package, but
3146 -- no body is needed for it. Note that after instantiation, the defining
3147 -- unit name we need is in the new tree and not in the original (see
3148 -- Package_Instantiation). A generic formal package is an instance, and
3149 -- can be used as an actual for an inner instance.
3151 Set_Has_Completion (Formal, True);
3153 -- Add semantic information to the original defining identifier.
3155 Set_Ekind (Pack_Id, E_Package);
3156 Set_Etype (Pack_Id, Standard_Void_Type);
3157 Set_Scope (Pack_Id, Scope (Formal));
3158 Set_Has_Completion (Pack_Id, True);
3161 if Has_Aspects (N) then
3162 -- Unclear that any other aspects may appear here, snalyze them
3163 -- for completion, given that the grammar allows their appearance.
3165 Analyze_Aspect_Specifications (N, Pack_Id);
3168 Ignore_SPARK_Mode_Pragmas_In_Instance := Save_ISMP;
3169 end Analyze_Formal_Package_Declaration;
3171 ---------------------------------
3172 -- Analyze_Formal_Private_Type --
3173 ---------------------------------
3175 procedure Analyze_Formal_Private_Type
3181 New_Private_Type (N, T, Def);
3183 -- Set the size to an arbitrary but legal value
3185 Set_Size_Info (T, Standard_Integer);
3186 Set_RM_Size (T, RM_Size (Standard_Integer));
3187 end Analyze_Formal_Private_Type;
3189 ------------------------------------
3190 -- Analyze_Formal_Incomplete_Type --
3191 ------------------------------------
3193 procedure Analyze_Formal_Incomplete_Type
3199 Set_Ekind (T, E_Incomplete_Type);
3201 Set_Private_Dependents (T, New_Elmt_List);
3203 if Tagged_Present (Def) then
3204 Set_Is_Tagged_Type (T);
3205 Make_Class_Wide_Type (T);
3206 Set_Direct_Primitive_Operations (T, New_Elmt_List);
3208 end Analyze_Formal_Incomplete_Type;
3210 ----------------------------------------
3211 -- Analyze_Formal_Signed_Integer_Type --
3212 ----------------------------------------
3214 procedure Analyze_Formal_Signed_Integer_Type
3218 Base : constant Entity_Id :=
3220 (E_Signed_Integer_Type,
3222 Sloc (Defining_Identifier (Parent (Def))), 'G');
3227 Set_Ekind (T, E_Signed_Integer_Subtype);
3228 Set_Etype (T, Base);
3229 Set_Size_Info (T, Standard_Integer);
3230 Set_RM_Size (T, RM_Size (Standard_Integer));
3231 Set_Scalar_Range (T, Scalar_Range (Standard_Integer));
3232 Set_Is_Constrained (T);
3234 Set_Is_Generic_Type (Base);
3235 Set_Size_Info (Base, Standard_Integer);
3236 Set_RM_Size (Base, RM_Size (Standard_Integer));
3237 Set_Etype (Base, Base);
3238 Set_Scalar_Range (Base, Scalar_Range (Standard_Integer));
3239 Set_Parent (Base, Parent (Def));
3240 end Analyze_Formal_Signed_Integer_Type;
3242 -------------------------------------------
3243 -- Analyze_Formal_Subprogram_Declaration --
3244 -------------------------------------------
3246 procedure Analyze_Formal_Subprogram_Declaration (N : Node_Id) is
3247 Spec : constant Node_Id := Specification (N);
3248 Def : constant Node_Id := Default_Name (N);
3249 Nam : constant Entity_Id := Defining_Unit_Name (Spec);
3257 if Nkind (Nam) = N_Defining_Program_Unit_Name then
3258 Error_Msg_N ("name of formal subprogram must be a direct name", Nam);
3262 Analyze_Subprogram_Declaration (N);
3263 Set_Is_Formal_Subprogram (Nam);
3264 Set_Has_Completion (Nam);
3266 if Nkind (N) = N_Formal_Abstract_Subprogram_Declaration then
3267 Set_Is_Abstract_Subprogram (Nam);
3269 Set_Is_Dispatching_Operation (Nam);
3271 -- A formal abstract procedure cannot have a null default
3272 -- (RM 12.6(4.1/2)).
3274 if Nkind (Spec) = N_Procedure_Specification
3275 and then Null_Present (Spec)
3278 ("a formal abstract subprogram cannot default to null", Spec);
3282 Ctrl_Type : constant Entity_Id := Find_Dispatching_Type (Nam);
3284 if No (Ctrl_Type) then
3286 ("abstract formal subprogram must have a controlling type",
3289 elsif Ada_Version >= Ada_2012
3290 and then Is_Incomplete_Type (Ctrl_Type)
3293 ("controlling type of abstract formal subprogram cannot "
3294 & "be incomplete type", N, Ctrl_Type);
3297 Check_Controlling_Formals (Ctrl_Type, Nam);
3302 -- Default name is resolved at the point of instantiation
3304 if Box_Present (N) then
3307 -- Else default is bound at the point of generic declaration
3309 elsif Present (Def) then
3310 if Nkind (Def) = N_Operator_Symbol then
3311 Find_Direct_Name (Def);
3313 elsif Nkind (Def) /= N_Attribute_Reference then
3317 -- For an attribute reference, analyze the prefix and verify
3318 -- that it has the proper profile for the subprogram.
3320 Analyze (Prefix (Def));
3321 Valid_Default_Attribute (Nam, Def);
3325 -- Default name may be overloaded, in which case the interpretation
3326 -- with the correct profile must be selected, as for a renaming.
3327 -- If the definition is an indexed component, it must denote a
3328 -- member of an entry family. If it is a selected component, it
3329 -- can be a protected operation.
3331 if Etype (Def) = Any_Type then
3334 elsif Nkind (Def) = N_Selected_Component then
3335 if not Is_Overloadable (Entity (Selector_Name (Def))) then
3336 Error_Msg_N ("expect valid subprogram name as default", Def);
3339 elsif Nkind (Def) = N_Indexed_Component then
3340 if Is_Entity_Name (Prefix (Def)) then
3341 if Ekind (Entity (Prefix (Def))) /= E_Entry_Family then
3342 Error_Msg_N ("expect valid subprogram name as default", Def);
3345 elsif Nkind (Prefix (Def)) = N_Selected_Component then
3346 if Ekind (Entity (Selector_Name (Prefix (Def)))) /=
3349 Error_Msg_N ("expect valid subprogram name as default", Def);
3353 Error_Msg_N ("expect valid subprogram name as default", Def);
3357 elsif Nkind (Def) = N_Character_Literal then
3359 -- Needs some type checks: subprogram should be parameterless???
3361 Resolve (Def, (Etype (Nam)));
3363 elsif not Is_Entity_Name (Def)
3364 or else not Is_Overloadable (Entity (Def))
3366 Error_Msg_N ("expect valid subprogram name as default", Def);
3369 elsif not Is_Overloaded (Def) then
3370 Subp := Entity (Def);
3373 Error_Msg_N ("premature usage of formal subprogram", Def);
3375 elsif not Entity_Matches_Spec (Subp, Nam) then
3376 Error_Msg_N ("no visible entity matches specification", Def);
3379 -- More than one interpretation, so disambiguate as for a renaming
3384 I1 : Interp_Index := 0;
3390 Get_First_Interp (Def, I, It);
3391 while Present (It.Nam) loop
3392 if Entity_Matches_Spec (It.Nam, Nam) then
3393 if Subp /= Any_Id then
3394 It1 := Disambiguate (Def, I1, I, Etype (Subp));
3396 if It1 = No_Interp then
3397 Error_Msg_N ("ambiguous default subprogram", Def);
3410 Get_Next_Interp (I, It);
3414 if Subp /= Any_Id then
3416 -- Subprogram found, generate reference to it
3418 Set_Entity (Def, Subp);
3419 Generate_Reference (Subp, Def);
3422 Error_Msg_N ("premature usage of formal subprogram", Def);
3424 elsif Ekind (Subp) /= E_Operator then
3425 Check_Mode_Conformant (Subp, Nam);
3429 Error_Msg_N ("no visible subprogram matches specification", N);
3435 if Has_Aspects (N) then
3436 Analyze_Aspect_Specifications (N, Nam);
3439 end Analyze_Formal_Subprogram_Declaration;
3441 -------------------------------------
3442 -- Analyze_Formal_Type_Declaration --
3443 -------------------------------------
3445 procedure Analyze_Formal_Type_Declaration (N : Node_Id) is
3446 Def : constant Node_Id := Formal_Type_Definition (N);
3450 T := Defining_Identifier (N);
3452 if Present (Discriminant_Specifications (N))
3453 and then Nkind (Def) /= N_Formal_Private_Type_Definition
3456 ("discriminants not allowed for this formal type", T);
3459 -- Enter the new name, and branch to specific routine
3462 when N_Formal_Private_Type_Definition =>
3463 Analyze_Formal_Private_Type (N, T, Def);
3465 when N_Formal_Derived_Type_Definition =>
3466 Analyze_Formal_Derived_Type (N, T, Def);
3468 when N_Formal_Incomplete_Type_Definition =>
3469 Analyze_Formal_Incomplete_Type (T, Def);
3471 when N_Formal_Discrete_Type_Definition =>
3472 Analyze_Formal_Discrete_Type (T, Def);
3474 when N_Formal_Signed_Integer_Type_Definition =>
3475 Analyze_Formal_Signed_Integer_Type (T, Def);
3477 when N_Formal_Modular_Type_Definition =>
3478 Analyze_Formal_Modular_Type (T, Def);
3480 when N_Formal_Floating_Point_Definition =>
3481 Analyze_Formal_Floating_Type (T, Def);
3483 when N_Formal_Ordinary_Fixed_Point_Definition =>
3484 Analyze_Formal_Ordinary_Fixed_Point_Type (T, Def);
3486 when N_Formal_Decimal_Fixed_Point_Definition =>
3487 Analyze_Formal_Decimal_Fixed_Point_Type (T, Def);
3489 when N_Array_Type_Definition =>
3490 Analyze_Formal_Array_Type (T, Def);
3492 when N_Access_Function_Definition
3493 | N_Access_Procedure_Definition
3494 | N_Access_To_Object_Definition
3496 Analyze_Generic_Access_Type (T, Def);
3498 -- Ada 2005: a interface declaration is encoded as an abstract
3499 -- record declaration or a abstract type derivation.
3501 when N_Record_Definition =>
3502 Analyze_Formal_Interface_Type (N, T, Def);
3504 when N_Derived_Type_Definition =>
3505 Analyze_Formal_Derived_Interface_Type (N, T, Def);
3511 raise Program_Error;
3514 -- A formal type declaration declares a type and its first
3517 Set_Is_Generic_Type (T);
3518 Set_Is_First_Subtype (T);
3520 if Has_Aspects (N) then
3521 Analyze_Aspect_Specifications (N, T);
3523 end Analyze_Formal_Type_Declaration;
3525 ------------------------------------
3526 -- Analyze_Function_Instantiation --
3527 ------------------------------------
3529 procedure Analyze_Function_Instantiation (N : Node_Id) is
3531 Analyze_Subprogram_Instantiation (N, E_Function);
3532 end Analyze_Function_Instantiation;
3534 ---------------------------------
3535 -- Analyze_Generic_Access_Type --
3536 ---------------------------------
3538 procedure Analyze_Generic_Access_Type (T : Entity_Id; Def : Node_Id) is
3542 if Nkind (Def) = N_Access_To_Object_Definition then
3543 Access_Type_Declaration (T, Def);
3545 if Is_Incomplete_Or_Private_Type (Designated_Type (T))
3546 and then No (Full_View (Designated_Type (T)))
3547 and then not Is_Generic_Type (Designated_Type (T))
3549 Error_Msg_N ("premature usage of incomplete type", Def);
3551 elsif not Is_Entity_Name (Subtype_Indication (Def)) then
3553 ("only a subtype mark is allowed in a formal", Def);
3557 Access_Subprogram_Declaration (T, Def);
3559 end Analyze_Generic_Access_Type;
3561 ---------------------------------
3562 -- Analyze_Generic_Formal_Part --
3563 ---------------------------------
3565 procedure Analyze_Generic_Formal_Part (N : Node_Id) is
3566 Gen_Parm_Decl : Node_Id;
3569 -- The generic formals are processed in the scope of the generic unit,
3570 -- where they are immediately visible. The scope is installed by the
3573 Gen_Parm_Decl := First (Generic_Formal_Declarations (N));
3574 while Present (Gen_Parm_Decl) loop
3575 Analyze (Gen_Parm_Decl);
3576 Next (Gen_Parm_Decl);
3579 Generate_Reference_To_Generic_Formals (Current_Scope);
3581 -- For Ada 2020, some formal parameters can carry aspects, which must
3582 -- be name-resolved at the end of the list of formal parameters (which
3583 -- has the semantics of a declaration list).
3585 Analyze_Contracts (Generic_Formal_Declarations (N));
3586 end Analyze_Generic_Formal_Part;
3588 ------------------------------------------
3589 -- Analyze_Generic_Package_Declaration --
3590 ------------------------------------------
3592 procedure Analyze_Generic_Package_Declaration (N : Node_Id) is
3593 Decls : constant List_Id := Visible_Declarations (Specification (N));
3594 Loc : constant Source_Ptr := Sloc (N);
3600 Save_Parent : Node_Id;
3603 -- A generic may grant access to its private enclosing context depending
3604 -- on the placement of its corresponding body. From elaboration point of
3605 -- view, the flow of execution may enter this private context, and then
3606 -- reach an external unit, thus producing a dependency on that external
3607 -- unit. For such a path to be properly discovered and encoded in the
3608 -- ALI file of the main unit, let the ABE mechanism process the body of
3609 -- the main unit, and encode all relevant invocation constructs and the
3610 -- relations between them.
3612 Mark_Save_Invocation_Graph_Of_Body;
3614 -- We introduce a renaming of the enclosing package, to have a usable
3615 -- entity as the prefix of an expanded name for a local entity of the
3616 -- form Par.P.Q, where P is the generic package. This is because a local
3617 -- entity named P may hide it, so that the usual visibility rules in
3618 -- the instance will not resolve properly.
3621 Make_Package_Renaming_Declaration (Loc,
3622 Defining_Unit_Name =>
3623 Make_Defining_Identifier (Loc,
3624 Chars => New_External_Name (Chars (Defining_Entity (N)), "GH")),
3626 Make_Identifier (Loc, Chars (Defining_Entity (N))));
3628 -- The declaration is inserted before other declarations, but before
3629 -- pragmas that may be library-unit pragmas and must appear before other
3630 -- declarations. The pragma Compile_Time_Error is not in this class, and
3631 -- may contain an expression that includes such a qualified name, so the
3632 -- renaming declaration must appear before it.
3634 -- Are there other pragmas that require this special handling ???
3636 if Present (Decls) then
3637 Decl := First (Decls);
3638 while Present (Decl)
3639 and then Nkind (Decl) = N_Pragma
3640 and then Get_Pragma_Id (Decl) /= Pragma_Compile_Time_Error
3645 if Present (Decl) then
3646 Insert_Before (Decl, Renaming);
3648 Append (Renaming, Visible_Declarations (Specification (N)));
3652 Set_Visible_Declarations (Specification (N), New_List (Renaming));
3655 -- Create copy of generic unit, and save for instantiation. If the unit
3656 -- is a child unit, do not copy the specifications for the parent, which
3657 -- are not part of the generic tree.
3659 Save_Parent := Parent_Spec (N);
3660 Set_Parent_Spec (N, Empty);
3662 New_N := Copy_Generic_Node (N, Empty, Instantiating => False);
3663 Set_Parent_Spec (New_N, Save_Parent);
3666 -- Once the contents of the generic copy and the template are swapped,
3667 -- do the same for their respective aspect specifications.
3669 Exchange_Aspects (N, New_N);
3671 -- Collect all contract-related source pragmas found within the template
3672 -- and attach them to the contract of the package spec. This contract is
3673 -- used in the capture of global references within annotations.
3675 Create_Generic_Contract (N);
3677 Id := Defining_Entity (N);
3678 Generate_Definition (Id);
3680 -- Expansion is not applied to generic units
3685 Set_Ekind (Id, E_Generic_Package);
3686 Set_Etype (Id, Standard_Void_Type);
3688 -- Set SPARK_Mode from context
3690 Set_SPARK_Pragma (Id, SPARK_Mode_Pragma);
3691 Set_SPARK_Aux_Pragma (Id, SPARK_Mode_Pragma);
3692 Set_SPARK_Pragma_Inherited (Id);
3693 Set_SPARK_Aux_Pragma_Inherited (Id);
3695 -- Preserve relevant elaboration-related attributes of the context which
3696 -- are no longer available or very expensive to recompute once analysis,
3697 -- resolution, and expansion are over.
3699 Mark_Elaboration_Attributes
3704 -- Analyze aspects now, so that generated pragmas appear in the
3705 -- declarations before building and analyzing the generic copy.
3707 if Has_Aspects (N) then
3708 Analyze_Aspect_Specifications (N, Id);
3712 Enter_Generic_Scope (Id);
3713 Set_Inner_Instances (Id, New_Elmt_List);
3715 Set_Categorization_From_Pragmas (N);
3716 Set_Is_Pure (Id, Is_Pure (Current_Scope));
3718 -- Link the declaration of the generic homonym in the generic copy to
3719 -- the package it renames, so that it is always resolved properly.
3721 Set_Generic_Homonym (Id, Defining_Unit_Name (Renaming));
3722 Set_Entity (Associated_Node (Name (Renaming)), Id);
3724 -- For a library unit, we have reconstructed the entity for the unit,
3725 -- and must reset it in the library tables.
3727 if Nkind (Parent (N)) = N_Compilation_Unit then
3728 Set_Cunit_Entity (Current_Sem_Unit, Id);
3731 Analyze_Generic_Formal_Part (N);
3733 -- After processing the generic formals, analysis proceeds as for a
3734 -- non-generic package.
3736 Analyze (Specification (N));
3738 Validate_Categorization_Dependency (N, Id);
3742 End_Package_Scope (Id);
3743 Exit_Generic_Scope (Id);
3745 -- If the generic appears within a package unit, the body of that unit
3746 -- has to be present for instantiation and inlining.
3748 if Nkind (Unit (Cunit (Current_Sem_Unit))) = N_Package_Declaration then
3749 Set_Body_Needed_For_Inlining
3750 (Defining_Entity (Unit (Cunit (Current_Sem_Unit))));
3753 if Nkind (Parent (N)) /= N_Compilation_Unit then
3754 Move_Freeze_Nodes (Id, N, Visible_Declarations (Specification (N)));
3755 Move_Freeze_Nodes (Id, N, Private_Declarations (Specification (N)));
3756 Move_Freeze_Nodes (Id, N, Generic_Formal_Declarations (N));
3759 Set_Body_Required (Parent (N), Unit_Requires_Body (Id));
3760 Validate_RT_RAT_Component (N);
3762 -- If this is a spec without a body, check that generic parameters
3765 if not Body_Required (Parent (N)) then
3766 Check_References (Id);
3770 -- If there is a specified storage pool in the context, create an
3771 -- aspect on the package declaration, so that it is used in any
3772 -- instance that does not override it.
3774 if Present (Default_Pool) then
3780 Make_Aspect_Specification (Loc,
3781 Identifier => Make_Identifier (Loc, Name_Default_Storage_Pool),
3782 Expression => New_Copy (Default_Pool));
3784 if No (Aspect_Specifications (Specification (N))) then
3785 Set_Aspect_Specifications (Specification (N), New_List (ASN));
3787 Append (ASN, Aspect_Specifications (Specification (N)));
3791 end Analyze_Generic_Package_Declaration;
3793 --------------------------------------------
3794 -- Analyze_Generic_Subprogram_Declaration --
3795 --------------------------------------------
3797 procedure Analyze_Generic_Subprogram_Declaration (N : Node_Id) is
3801 Result_Type : Entity_Id;
3802 Save_Parent : Node_Id;
3807 -- A generic may grant access to its private enclosing context depending
3808 -- on the placement of its corresponding body. From elaboration point of
3809 -- view, the flow of execution may enter this private context, and then
3810 -- reach an external unit, thus producing a dependency on that external
3811 -- unit. For such a path to be properly discovered and encoded in the
3812 -- ALI file of the main unit, let the ABE mechanism process the body of
3813 -- the main unit, and encode all relevant invocation constructs and the
3814 -- relations between them.
3816 Mark_Save_Invocation_Graph_Of_Body;
3818 -- Create copy of generic unit, and save for instantiation. If the unit
3819 -- is a child unit, do not copy the specifications for the parent, which
3820 -- are not part of the generic tree.
3822 Save_Parent := Parent_Spec (N);
3823 Set_Parent_Spec (N, Empty);
3825 New_N := Copy_Generic_Node (N, Empty, Instantiating => False);
3826 Set_Parent_Spec (New_N, Save_Parent);
3829 -- Once the contents of the generic copy and the template are swapped,
3830 -- do the same for their respective aspect specifications.
3832 Exchange_Aspects (N, New_N);
3834 -- Collect all contract-related source pragmas found within the template
3835 -- and attach them to the contract of the subprogram spec. This contract
3836 -- is used in the capture of global references within annotations.
3838 Create_Generic_Contract (N);
3840 Spec := Specification (N);
3841 Id := Defining_Entity (Spec);
3842 Generate_Definition (Id);
3844 if Nkind (Id) = N_Defining_Operator_Symbol then
3846 ("operator symbol not allowed for generic subprogram", Id);
3852 Set_Scope_Depth_Value (Id, Scope_Depth (Current_Scope) + 1);
3854 -- Analyze the aspects of the generic copy to ensure that all generated
3855 -- pragmas (if any) perform their semantic effects.
3857 if Has_Aspects (N) then
3858 Analyze_Aspect_Specifications (N, Id);
3862 Enter_Generic_Scope (Id);
3863 Set_Inner_Instances (Id, New_Elmt_List);
3864 Set_Is_Pure (Id, Is_Pure (Current_Scope));
3866 Analyze_Generic_Formal_Part (N);
3868 if Nkind (Spec) = N_Function_Specification then
3869 Set_Ekind (Id, E_Generic_Function);
3871 Set_Ekind (Id, E_Generic_Procedure);
3874 -- Set SPARK_Mode from context
3876 Set_SPARK_Pragma (Id, SPARK_Mode_Pragma);
3877 Set_SPARK_Pragma_Inherited (Id);
3879 -- Preserve relevant elaboration-related attributes of the context which
3880 -- are no longer available or very expensive to recompute once analysis,
3881 -- resolution, and expansion are over.
3883 Mark_Elaboration_Attributes
3888 Formals := Parameter_Specifications (Spec);
3890 if Present (Formals) then
3891 Process_Formals (Formals, Spec);
3894 if Nkind (Spec) = N_Function_Specification then
3895 if Nkind (Result_Definition (Spec)) = N_Access_Definition then
3896 Result_Type := Access_Definition (Spec, Result_Definition (Spec));
3897 Set_Etype (Id, Result_Type);
3899 -- Check restriction imposed by AI05-073: a generic function
3900 -- cannot return an abstract type or an access to such.
3902 -- This is a binding interpretation should it apply to earlier
3903 -- versions of Ada as well as Ada 2012???
3905 if Is_Abstract_Type (Designated_Type (Result_Type))
3906 and then Ada_Version >= Ada_2012
3909 ("generic function cannot have an access result "
3910 & "that designates an abstract type", Spec);
3914 Find_Type (Result_Definition (Spec));
3915 Typ := Entity (Result_Definition (Spec));
3917 if Is_Abstract_Type (Typ)
3918 and then Ada_Version >= Ada_2012
3921 ("generic function cannot have abstract result type", Spec);
3924 -- If a null exclusion is imposed on the result type, then create
3925 -- a null-excluding itype (an access subtype) and use it as the
3926 -- function's Etype.
3928 if Is_Access_Type (Typ)
3929 and then Null_Exclusion_Present (Spec)
3932 Create_Null_Excluding_Itype
3934 Related_Nod => Spec,
3935 Scope_Id => Defining_Unit_Name (Spec)));
3937 Set_Etype (Id, Typ);
3942 Set_Etype (Id, Standard_Void_Type);
3945 -- For a library unit, we have reconstructed the entity for the unit,
3946 -- and must reset it in the library tables. We also make sure that
3947 -- Body_Required is set properly in the original compilation unit node.
3949 if Nkind (Parent (N)) = N_Compilation_Unit then
3950 Set_Cunit_Entity (Current_Sem_Unit, Id);
3951 Set_Body_Required (Parent (N), Unit_Requires_Body (Id));
3954 -- If the generic appears within a package unit, the body of that unit
3955 -- has to be present for instantiation and inlining.
3957 if Nkind (Unit (Cunit (Current_Sem_Unit))) = N_Package_Declaration
3958 and then Unit_Requires_Body (Id)
3960 Set_Body_Needed_For_Inlining
3961 (Defining_Entity (Unit (Cunit (Current_Sem_Unit))));
3964 Set_Categorization_From_Pragmas (N);
3965 Validate_Categorization_Dependency (N, Id);
3967 -- Capture all global references that occur within the profile of the
3968 -- generic subprogram. Aspects are not part of this processing because
3969 -- they must be delayed. If processed now, Save_Global_References will
3970 -- destroy the Associated_Node links and prevent the capture of global
3971 -- references when the contract of the generic subprogram is analyzed.
3973 Save_Global_References (Original_Node (N));
3977 Exit_Generic_Scope (Id);
3978 Generate_Reference_To_Formals (Id);
3980 List_Inherited_Pre_Post_Aspects (Id);
3981 end Analyze_Generic_Subprogram_Declaration;
3983 -----------------------------------
3984 -- Analyze_Package_Instantiation --
3985 -----------------------------------
3987 -- WARNING: This routine manages Ghost and SPARK regions. Return statements
3988 -- must be replaced by gotos which jump to the end of the routine in order
3989 -- to restore the Ghost and SPARK modes.
3991 procedure Analyze_Package_Instantiation (N : Node_Id) is
3992 Has_Inline_Always : Boolean := False;
3993 -- Set if the generic unit contains any subprograms with Inline_Always.
3994 -- Only relevant when back-end inlining is not enabled.
3996 function Might_Inline_Subp (Gen_Unit : Entity_Id) return Boolean;
3997 -- Return True if inlining is active and Gen_Unit contains inlined
3998 -- subprograms. In this case, we may either instantiate the body when
3999 -- front-end inlining is enabled, or add a pending instantiation when
4000 -- back-end inlining is enabled. In the former case, this may cause
4001 -- superfluous instantiations, but in either case we need to perform
4002 -- the instantiation of the body in the context of the instance and
4003 -- not in that of the point of inlining.
4005 function Needs_Body_Instantiated (Gen_Unit : Entity_Id) return Boolean;
4006 -- Return True if Gen_Unit needs to have its body instantiated in the
4007 -- context of N. This in particular excludes generic contexts.
4009 -----------------------
4010 -- Might_Inline_Subp --
4011 -----------------------
4013 function Might_Inline_Subp (Gen_Unit : Entity_Id) return Boolean is
4017 if Inline_Processing_Required then
4018 -- No need to recompute the answer if we know it is positive
4019 -- and back-end inlining is enabled.
4021 if Is_Inlined (Gen_Unit) and then Back_End_Inlining then
4025 E := First_Entity (Gen_Unit);
4026 while Present (E) loop
4027 if Is_Subprogram (E) and then Is_Inlined (E) then
4028 -- Remember if there are any subprograms with Inline_Always
4030 if Has_Pragma_Inline_Always (E) then
4031 Has_Inline_Always := True;
4034 Set_Is_Inlined (Gen_Unit);
4043 end Might_Inline_Subp;
4045 -------------------------------
4046 -- Needs_Body_Instantiated --
4047 -------------------------------
4049 function Needs_Body_Instantiated (Gen_Unit : Entity_Id) return Boolean is
4051 -- No need to instantiate bodies in generic units
4053 if Is_Generic_Unit (Cunit_Entity (Main_Unit)) then
4057 -- If the instantiation is in the main unit, then the body is needed
4059 if Is_In_Main_Unit (N) then
4063 -- If not, then again no need to instantiate bodies in generic units
4065 if Is_Generic_Unit (Cunit_Entity (Get_Code_Unit (N))) then
4069 -- Here we have a special handling for back-end inlining: if inline
4070 -- processing is required, then we unconditionally want to have the
4071 -- body instantiated. The reason is that Might_Inline_Subp does not
4072 -- catch all the cases (as it does not recurse into nested packages)
4073 -- so this avoids the need to patch things up afterwards. Moreover,
4074 -- these instantiations are only performed on demand when back-end
4075 -- inlining is enabled, so this causes very little extra work.
4077 if Inline_Processing_Required and then Back_End_Inlining then
4081 -- We want to have the bodies instantiated in non-main units if
4082 -- they might contribute inlined subprograms.
4084 return Might_Inline_Subp (Gen_Unit);
4085 end Needs_Body_Instantiated;
4087 -- Local declarations
4089 Gen_Id : constant Node_Id := Name (N);
4090 Inst_Id : constant Entity_Id := Defining_Entity (N);
4091 Is_Actual_Pack : constant Boolean := Is_Internal (Inst_Id);
4092 Loc : constant Source_Ptr := Sloc (N);
4094 Saved_GM : constant Ghost_Mode_Type := Ghost_Mode;
4095 Saved_IGR : constant Node_Id := Ignored_Ghost_Region;
4096 Saved_ISMP : constant Boolean :=
4097 Ignore_SPARK_Mode_Pragmas_In_Instance;
4098 Saved_SM : constant SPARK_Mode_Type := SPARK_Mode;
4099 Saved_SMP : constant Node_Id := SPARK_Mode_Pragma;
4100 -- Save the Ghost and SPARK mode-related data to restore on exit
4102 Saved_Style_Check : constant Boolean := Style_Check;
4103 -- Save style check mode for restore on exit
4106 Act_Decl_Name : Node_Id;
4107 Act_Decl_Id : Entity_Id;
4110 Env_Installed : Boolean := False;
4113 Gen_Unit : Entity_Id;
4114 Inline_Now : Boolean := False;
4115 Needs_Body : Boolean;
4116 Parent_Installed : Boolean := False;
4117 Renaming_List : List_Id;
4118 Unit_Renaming : Node_Id;
4120 Vis_Prims_List : Elist_Id := No_Elist;
4121 -- List of primitives made temporarily visible in the instantiation
4122 -- to match the visibility of the formal type
4124 -- Start of processing for Analyze_Package_Instantiation
4127 -- Preserve relevant elaboration-related attributes of the context which
4128 -- are no longer available or very expensive to recompute once analysis,
4129 -- resolution, and expansion are over.
4131 Mark_Elaboration_Attributes
4138 -- Very first thing: check for Text_IO special unit in case we are
4139 -- instantiating one of the children of [[Wide_]Wide_]Text_IO.
4141 Check_Text_IO_Special_Unit (Name (N));
4143 -- Make node global for error reporting
4145 Instantiation_Node := N;
4147 -- Case of instantiation of a generic package
4149 if Nkind (N) = N_Package_Instantiation then
4150 Act_Decl_Id := New_Copy (Defining_Entity (N));
4151 Set_Comes_From_Source (Act_Decl_Id, True);
4153 if Nkind (Defining_Unit_Name (N)) = N_Defining_Program_Unit_Name then
4155 Make_Defining_Program_Unit_Name (Loc,
4157 New_Copy_Tree (Name (Defining_Unit_Name (N))),
4158 Defining_Identifier => Act_Decl_Id);
4160 Act_Decl_Name := Act_Decl_Id;
4163 -- Case of instantiation of a formal package
4166 Act_Decl_Id := Defining_Identifier (N);
4167 Act_Decl_Name := Act_Decl_Id;
4170 Generate_Definition (Act_Decl_Id);
4171 Set_Ekind (Act_Decl_Id, E_Package);
4173 -- Initialize list of incomplete actuals before analysis
4175 Set_Incomplete_Actuals (Act_Decl_Id, New_Elmt_List);
4177 Preanalyze_Actuals (N, Act_Decl_Id);
4179 -- Turn off style checking in instances. If the check is enabled on the
4180 -- generic unit, a warning in an instance would just be noise. If not
4181 -- enabled on the generic, then a warning in an instance is just wrong.
4182 -- This must be done after analyzing the actuals, which do come from
4183 -- source and are subject to style checking.
4185 Style_Check := False;
4188 Env_Installed := True;
4190 -- Reset renaming map for formal types. The mapping is established
4191 -- when analyzing the generic associations, but some mappings are
4192 -- inherited from formal packages of parent units, and these are
4193 -- constructed when the parents are installed.
4195 Generic_Renamings.Set_Last (0);
4196 Generic_Renamings_HTable.Reset;
4198 Check_Generic_Child_Unit (Gen_Id, Parent_Installed);
4199 Gen_Unit := Entity (Gen_Id);
4201 -- A package instantiation is Ghost when it is subject to pragma Ghost
4202 -- or the generic template is Ghost. Set the mode now to ensure that
4203 -- any nodes generated during analysis and expansion are marked as
4206 Mark_And_Set_Ghost_Instantiation (N, Gen_Unit);
4208 -- Verify that it is the name of a generic package
4210 -- A visibility glitch: if the instance is a child unit and the generic
4211 -- is the generic unit of a parent instance (i.e. both the parent and
4212 -- the child units are instances of the same package) the name now
4213 -- denotes the renaming within the parent, not the intended generic
4214 -- unit. See if there is a homonym that is the desired generic. The
4215 -- renaming declaration must be visible inside the instance of the
4216 -- child, but not when analyzing the name in the instantiation itself.
4218 if Ekind (Gen_Unit) = E_Package
4219 and then Present (Renamed_Entity (Gen_Unit))
4220 and then In_Open_Scopes (Renamed_Entity (Gen_Unit))
4221 and then Is_Generic_Instance (Renamed_Entity (Gen_Unit))
4222 and then Present (Homonym (Gen_Unit))
4224 Gen_Unit := Homonym (Gen_Unit);
4227 if Etype (Gen_Unit) = Any_Type then
4231 elsif Ekind (Gen_Unit) /= E_Generic_Package then
4233 -- Ada 2005 (AI-50217): Cannot use instance in limited with_clause
4235 if From_Limited_With (Gen_Unit) then
4237 ("cannot instantiate a limited withed package", Gen_Id);
4240 ("& is not the name of a generic package", Gen_Id, Gen_Unit);
4247 if In_Extended_Main_Source_Unit (N) then
4248 Set_Is_Instantiated (Gen_Unit);
4249 Generate_Reference (Gen_Unit, N);
4251 if Present (Renamed_Object (Gen_Unit)) then
4252 Set_Is_Instantiated (Renamed_Object (Gen_Unit));
4253 Generate_Reference (Renamed_Object (Gen_Unit), N);
4257 if Nkind (Gen_Id) = N_Identifier
4258 and then Chars (Gen_Unit) = Chars (Defining_Entity (N))
4261 ("& is hidden within declaration of instance", Gen_Id, Gen_Unit);
4263 elsif Nkind (Gen_Id) = N_Expanded_Name
4264 and then Is_Child_Unit (Gen_Unit)
4265 and then Nkind (Prefix (Gen_Id)) = N_Identifier
4266 and then Chars (Act_Decl_Id) = Chars (Prefix (Gen_Id))
4269 ("& is hidden within declaration of instance ", Prefix (Gen_Id));
4272 Set_Entity (Gen_Id, Gen_Unit);
4274 -- If generic is a renaming, get original generic unit
4276 if Present (Renamed_Object (Gen_Unit))
4277 and then Ekind (Renamed_Object (Gen_Unit)) = E_Generic_Package
4279 Gen_Unit := Renamed_Object (Gen_Unit);
4282 -- Verify that there are no circular instantiations
4284 if In_Open_Scopes (Gen_Unit) then
4285 Error_Msg_NE ("instantiation of & within itself", N, Gen_Unit);
4289 elsif Contains_Instance_Of (Gen_Unit, Current_Scope, Gen_Id) then
4290 Error_Msg_Node_2 := Current_Scope;
4292 ("circular Instantiation: & instantiated in &!", N, Gen_Unit);
4293 Circularity_Detected := True;
4298 Set_Ekind (Inst_Id, E_Package);
4299 Set_Scope (Inst_Id, Current_Scope);
4301 -- If the context of the instance is subject to SPARK_Mode "off" or
4302 -- the annotation is altogether missing, set the global flag which
4303 -- signals Analyze_Pragma to ignore all SPARK_Mode pragmas within
4306 if SPARK_Mode /= On then
4307 Ignore_SPARK_Mode_Pragmas_In_Instance := True;
4309 -- Mark the instance spec in case the body is instantiated at a
4310 -- later pass. This preserves the original context in effect for
4313 Set_Ignore_SPARK_Mode_Pragmas (Act_Decl_Id);
4316 Gen_Decl := Unit_Declaration_Node (Gen_Unit);
4317 Gen_Spec := Specification (Gen_Decl);
4319 -- Initialize renamings map, for error checking, and the list that
4320 -- holds private entities whose views have changed between generic
4321 -- definition and instantiation. If this is the instance created to
4322 -- validate an actual package, the instantiation environment is that
4323 -- of the enclosing instance.
4325 Create_Instantiation_Source (N, Gen_Unit, S_Adjustment);
4327 -- Copy original generic tree, to produce text for instantiation
4331 (Original_Node (Gen_Decl), Empty, Instantiating => True);
4333 Act_Spec := Specification (Act_Tree);
4335 -- If this is the instance created to validate an actual package,
4336 -- only the formals matter, do not examine the package spec itself.
4338 if Is_Actual_Pack then
4339 Set_Visible_Declarations (Act_Spec, New_List);
4340 Set_Private_Declarations (Act_Spec, New_List);
4344 Analyze_Associations
4346 Formals => Generic_Formal_Declarations (Act_Tree),
4347 F_Copy => Generic_Formal_Declarations (Gen_Decl));
4349 Vis_Prims_List := Check_Hidden_Primitives (Renaming_List);
4351 Set_Instance_Env (Gen_Unit, Act_Decl_Id);
4352 Set_Defining_Unit_Name (Act_Spec, Act_Decl_Name);
4353 Set_Is_Generic_Instance (Act_Decl_Id);
4354 Set_Generic_Parent (Act_Spec, Gen_Unit);
4356 -- References to the generic in its own declaration or its body are
4357 -- references to the instance. Add a renaming declaration for the
4358 -- generic unit itself. This declaration, as well as the renaming
4359 -- declarations for the generic formals, must remain private to the
4360 -- unit: the formals, because this is the language semantics, and
4361 -- the unit because its use is an artifact of the implementation.
4364 Make_Package_Renaming_Declaration (Loc,
4365 Defining_Unit_Name =>
4366 Make_Defining_Identifier (Loc, Chars (Gen_Unit)),
4367 Name => New_Occurrence_Of (Act_Decl_Id, Loc));
4369 Append (Unit_Renaming, Renaming_List);
4371 -- The renaming declarations are the first local declarations of the
4374 if Is_Non_Empty_List (Visible_Declarations (Act_Spec)) then
4376 (First (Visible_Declarations (Act_Spec)), Renaming_List);
4378 Set_Visible_Declarations (Act_Spec, Renaming_List);
4381 Act_Decl := Make_Package_Declaration (Loc, Specification => Act_Spec);
4383 -- Propagate the aspect specifications from the package declaration
4384 -- template to the instantiated version of the package declaration.
4386 if Has_Aspects (Act_Tree) then
4387 Set_Aspect_Specifications (Act_Decl,
4388 New_Copy_List_Tree (Aspect_Specifications (Act_Tree)));
4391 -- The generic may have a generated Default_Storage_Pool aspect,
4392 -- set at the point of generic declaration. If the instance has
4393 -- that aspect, it overrides the one inherited from the generic.
4395 if Has_Aspects (Gen_Spec) then
4396 if No (Aspect_Specifications (N)) then
4397 Set_Aspect_Specifications (N,
4399 (Aspect_Specifications (Gen_Spec))));
4403 Inherited_Aspects : constant List_Id :=
4405 (Aspect_Specifications (Gen_Spec));
4409 Pool_Present : Boolean := False;
4412 ASN1 := First (Aspect_Specifications (N));
4413 while Present (ASN1) loop
4414 if Chars (Identifier (ASN1)) =
4415 Name_Default_Storage_Pool
4417 Pool_Present := True;
4424 if Pool_Present then
4426 -- If generic carries a default storage pool, remove it
4427 -- in favor of the instance one.
4429 ASN2 := First (Inherited_Aspects);
4430 while Present (ASN2) loop
4431 if Chars (Identifier (ASN2)) =
4432 Name_Default_Storage_Pool
4443 (Aspect_Specifications (N), Inherited_Aspects);
4448 -- Save the instantiation node for a subsequent instantiation of the
4449 -- body if there is one and it needs to be instantiated here.
4451 -- We instantiate the body only if we are generating code, or if we
4452 -- are generating cross-reference information, or for GNATprove use.
4455 Enclosing_Body_Present : Boolean := False;
4456 -- If the generic unit is not a compilation unit, then a body may
4457 -- be present in its parent even if none is required. We create a
4458 -- tentative pending instantiation for the body, which will be
4459 -- discarded if none is actually present.
4464 if Scope (Gen_Unit) /= Standard_Standard
4465 and then not Is_Child_Unit (Gen_Unit)
4467 Scop := Scope (Gen_Unit);
4468 while Present (Scop) and then Scop /= Standard_Standard loop
4469 if Unit_Requires_Body (Scop) then
4470 Enclosing_Body_Present := True;
4473 elsif In_Open_Scopes (Scop)
4474 and then In_Package_Body (Scop)
4476 Enclosing_Body_Present := True;
4480 exit when Is_Compilation_Unit (Scop);
4481 Scop := Scope (Scop);
4485 -- If front-end inlining is enabled or there are any subprograms
4486 -- marked with Inline_Always, and this is a unit for which code
4487 -- will be generated, we instantiate the body at once.
4489 -- This is done if the instance is not the main unit, and if the
4490 -- generic is not a child unit of another generic, to avoid scope
4491 -- problems and the reinstallation of parent instances.
4494 and then (not Is_Child_Unit (Gen_Unit)
4495 or else not Is_Generic_Unit (Scope (Gen_Unit)))
4496 and then Might_Inline_Subp (Gen_Unit)
4497 and then not Is_Actual_Pack
4499 if not Back_End_Inlining
4500 and then (Front_End_Inlining or else Has_Inline_Always)
4501 and then (Is_In_Main_Unit (N)
4502 or else In_Main_Context (Current_Scope))
4503 and then Nkind (Parent (N)) /= N_Compilation_Unit
4507 -- In configurable_run_time mode we force the inlining of
4508 -- predefined subprograms marked Inline_Always, to minimize
4509 -- the use of the run-time library.
4511 elsif In_Predefined_Unit (Gen_Decl)
4512 and then Configurable_Run_Time_Mode
4513 and then Nkind (Parent (N)) /= N_Compilation_Unit
4518 -- If the current scope is itself an instance within a child
4519 -- unit, there will be duplications in the scope stack, and the
4520 -- unstacking mechanism in Inline_Instance_Body will fail.
4521 -- This loses some rare cases of optimization, and might be
4522 -- improved some day, if we can find a proper abstraction for
4523 -- "the complete compilation context" that can be saved and
4526 if Is_Generic_Instance (Current_Scope) then
4528 Curr_Unit : constant Entity_Id :=
4529 Cunit_Entity (Current_Sem_Unit);
4531 if Curr_Unit /= Current_Scope
4532 and then Is_Child_Unit (Curr_Unit)
4534 Inline_Now := False;
4541 (Unit_Requires_Body (Gen_Unit)
4542 or else Enclosing_Body_Present
4543 or else Present (Corresponding_Body (Gen_Decl)))
4544 and then Needs_Body_Instantiated (Gen_Unit)
4545 and then not Is_Actual_Pack
4546 and then not Inline_Now
4547 and then (Operating_Mode = Generate_Code
4548 or else (Operating_Mode = Check_Semantics
4549 and then GNATprove_Mode));
4551 -- If front-end inlining is enabled or there are any subprograms
4552 -- marked with Inline_Always, do not instantiate body when within
4553 -- a generic context.
4555 if not Back_End_Inlining
4556 and then (Front_End_Inlining or else Has_Inline_Always)
4557 and then not Expander_Active
4559 Needs_Body := False;
4562 -- If the current context is generic, and the package being
4563 -- instantiated is declared within a formal package, there is no
4564 -- body to instantiate until the enclosing generic is instantiated
4565 -- and there is an actual for the formal package. If the formal
4566 -- package has parameters, we build a regular package instance for
4567 -- it, that precedes the original formal package declaration.
4569 if In_Open_Scopes (Scope (Scope (Gen_Unit))) then
4571 Decl : constant Node_Id :=
4573 (Unit_Declaration_Node (Scope (Gen_Unit)));
4575 if Nkind (Decl) = N_Formal_Package_Declaration
4576 or else (Nkind (Decl) = N_Package_Declaration
4577 and then Is_List_Member (Decl)
4578 and then Present (Next (Decl))
4580 Nkind (Next (Decl)) =
4581 N_Formal_Package_Declaration)
4583 Needs_Body := False;
4589 -- For RCI unit calling stubs, we omit the instance body if the
4590 -- instance is the RCI library unit itself.
4592 -- However there is a special case for nested instances: in this case
4593 -- we do generate the instance body, as it might be required, e.g.
4594 -- because it provides stream attributes for some type used in the
4595 -- profile of a remote subprogram. This is consistent with 12.3(12),
4596 -- which indicates that the instance body occurs at the place of the
4597 -- instantiation, and thus is part of the RCI declaration, which is
4598 -- present on all client partitions (this is E.2.3(18)).
4600 -- Note that AI12-0002 may make it illegal at some point to have
4601 -- stream attributes defined in an RCI unit, in which case this
4602 -- special case will become unnecessary. In the meantime, there
4603 -- is known application code in production that depends on this
4604 -- being possible, so we definitely cannot eliminate the body in
4605 -- the case of nested instances for the time being.
4607 -- When we generate a nested instance body, calling stubs for any
4608 -- relevant subprogram will be inserted immediately after the
4609 -- subprogram declarations, and will take precedence over the
4610 -- subsequent (original) body. (The stub and original body will be
4611 -- complete homographs, but this is permitted in an instance).
4612 -- (Could we do better and remove the original body???)
4614 if Distribution_Stub_Mode = Generate_Caller_Stub_Body
4615 and then Comes_From_Source (N)
4616 and then Nkind (Parent (N)) = N_Compilation_Unit
4618 Needs_Body := False;
4622 -- Indicate that the enclosing scopes contain an instantiation,
4623 -- and that cleanup actions should be delayed until after the
4624 -- instance body is expanded.
4626 Check_Forward_Instantiation (Gen_Decl);
4627 if Nkind (N) = N_Package_Instantiation then
4629 Enclosing_Master : Entity_Id;
4632 -- Loop to search enclosing masters
4634 Enclosing_Master := Current_Scope;
4635 Scope_Loop : while Enclosing_Master /= Standard_Standard loop
4636 if Ekind (Enclosing_Master) = E_Package then
4637 if Is_Compilation_Unit (Enclosing_Master) then
4638 if In_Package_Body (Enclosing_Master) then
4639 Set_Delay_Subprogram_Descriptors
4640 (Body_Entity (Enclosing_Master));
4642 Set_Delay_Subprogram_Descriptors
4649 Enclosing_Master := Scope (Enclosing_Master);
4652 elsif Is_Generic_Unit (Enclosing_Master)
4653 or else Ekind (Enclosing_Master) = E_Void
4655 -- Cleanup actions will eventually be performed on the
4656 -- enclosing subprogram or package instance, if any.
4657 -- Enclosing scope is void in the formal part of a
4658 -- generic subprogram.
4663 if Ekind (Enclosing_Master) = E_Entry
4665 Ekind (Scope (Enclosing_Master)) = E_Protected_Type
4667 if not Expander_Active then
4671 Protected_Body_Subprogram (Enclosing_Master);
4675 Set_Delay_Cleanups (Enclosing_Master);
4677 while Ekind (Enclosing_Master) = E_Block loop
4678 Enclosing_Master := Scope (Enclosing_Master);
4681 if Is_Subprogram (Enclosing_Master) then
4682 Set_Delay_Subprogram_Descriptors (Enclosing_Master);
4684 elsif Is_Task_Type (Enclosing_Master) then
4686 TBP : constant Node_Id :=
4687 Get_Task_Body_Procedure
4690 if Present (TBP) then
4691 Set_Delay_Subprogram_Descriptors (TBP);
4692 Set_Delay_Cleanups (TBP);
4699 end loop Scope_Loop;
4702 -- Make entry in table
4704 Add_Pending_Instantiation (N, Act_Decl);
4708 Set_Categorization_From_Pragmas (Act_Decl);
4710 if Parent_Installed then
4714 Set_Instance_Spec (N, Act_Decl);
4716 -- If not a compilation unit, insert the package declaration before
4717 -- the original instantiation node.
4719 if Nkind (Parent (N)) /= N_Compilation_Unit then
4720 Mark_Rewrite_Insertion (Act_Decl);
4721 Insert_Before (N, Act_Decl);
4723 if Has_Aspects (N) then
4724 Analyze_Aspect_Specifications (N, Act_Decl_Id);
4726 -- The pragma created for a Default_Storage_Pool aspect must
4727 -- appear ahead of the declarations in the instance spec.
4728 -- Analysis has placed it after the instance node, so remove
4729 -- it and reinsert it properly now.
4732 ASN : constant Node_Id := First (Aspect_Specifications (N));
4733 A_Name : constant Name_Id := Chars (Identifier (ASN));
4737 if A_Name = Name_Default_Storage_Pool then
4738 if No (Visible_Declarations (Act_Spec)) then
4739 Set_Visible_Declarations (Act_Spec, New_List);
4743 while Present (Decl) loop
4744 if Nkind (Decl) = N_Pragma then
4746 Prepend (Decl, Visible_Declarations (Act_Spec));
4758 -- For an instantiation that is a compilation unit, place
4759 -- declaration on current node so context is complete for analysis
4760 -- (including nested instantiations). If this is the main unit,
4761 -- the declaration eventually replaces the instantiation node.
4762 -- If the instance body is created later, it replaces the
4763 -- instance node, and the declaration is attached to it
4764 -- (see Build_Instance_Compilation_Unit_Nodes).
4767 if Cunit_Entity (Current_Sem_Unit) = Defining_Entity (N) then
4769 -- The entity for the current unit is the newly created one,
4770 -- and all semantic information is attached to it.
4772 Set_Cunit_Entity (Current_Sem_Unit, Act_Decl_Id);
4774 -- If this is the main unit, replace the main entity as well
4776 if Current_Sem_Unit = Main_Unit then
4777 Main_Unit_Entity := Act_Decl_Id;
4781 Set_Unit (Parent (N), Act_Decl);
4782 Set_Parent_Spec (Act_Decl, Parent_Spec (N));
4783 Set_Package_Instantiation (Act_Decl_Id, N);
4785 -- Process aspect specifications of the instance node, if any, to
4786 -- take into account categorization pragmas before analyzing the
4789 if Has_Aspects (N) then
4790 Analyze_Aspect_Specifications (N, Act_Decl_Id);
4794 Set_Unit (Parent (N), N);
4795 Set_Body_Required (Parent (N), False);
4797 -- We never need elaboration checks on instantiations, since by
4798 -- definition, the body instantiation is elaborated at the same
4799 -- time as the spec instantiation.
4801 if Legacy_Elaboration_Checks then
4802 Set_Kill_Elaboration_Checks (Act_Decl_Id);
4803 Set_Suppress_Elaboration_Warnings (Act_Decl_Id);
4807 if Legacy_Elaboration_Checks then
4808 Check_Elab_Instantiation (N);
4811 -- Save the scenario for later examination by the ABE Processing
4814 Record_Elaboration_Scenario (N);
4816 -- The instantiation results in a guaranteed ABE
4818 if Is_Known_Guaranteed_ABE (N) and then Needs_Body then
4819 -- Do not instantiate the corresponding body because gigi cannot
4820 -- handle certain types of premature instantiations.
4822 Remove_Dead_Instance (N);
4824 -- Create completing bodies for all subprogram declarations since
4825 -- their real bodies will not be instantiated.
4827 Provide_Completing_Bodies (Instance_Spec (N));
4830 Check_Hidden_Child_Unit (N, Gen_Unit, Act_Decl_Id);
4832 Set_First_Private_Entity (Defining_Unit_Name (Unit_Renaming),
4833 First_Private_Entity (Act_Decl_Id));
4835 -- If the instantiation will receive a body, the unit will be
4836 -- transformed into a package body, and receive its own elaboration
4837 -- entity. Otherwise, the nature of the unit is now a package
4840 if Nkind (Parent (N)) = N_Compilation_Unit
4841 and then not Needs_Body
4843 Rewrite (N, Act_Decl);
4846 if Present (Corresponding_Body (Gen_Decl))
4847 or else Unit_Requires_Body (Gen_Unit)
4849 Set_Has_Completion (Act_Decl_Id);
4852 Check_Formal_Packages (Act_Decl_Id);
4854 Restore_Hidden_Primitives (Vis_Prims_List);
4855 Restore_Private_Views (Act_Decl_Id);
4857 Inherit_Context (Gen_Decl, N);
4859 if Parent_Installed then
4864 Env_Installed := False;
4867 Validate_Categorization_Dependency (N, Act_Decl_Id);
4869 -- There used to be a check here to prevent instantiations in local
4870 -- contexts if the No_Local_Allocators restriction was active. This
4871 -- check was removed by a binding interpretation in AI-95-00130/07,
4872 -- but we retain the code for documentation purposes.
4874 -- if Ekind (Act_Decl_Id) /= E_Void
4875 -- and then not Is_Library_Level_Entity (Act_Decl_Id)
4877 -- Check_Restriction (No_Local_Allocators, N);
4881 Inline_Instance_Body (N, Gen_Unit, Act_Decl);
4884 -- Check that if N is an instantiation of System.Dim_Float_IO or
4885 -- System.Dim_Integer_IO, the formal type has a dimension system.
4887 if Nkind (N) = N_Package_Instantiation
4888 and then Is_Dim_IO_Package_Instantiation (N)
4891 Assoc : constant Node_Id := First (Generic_Associations (N));
4893 if not Has_Dimension_System
4894 (Etype (Explicit_Generic_Actual_Parameter (Assoc)))
4896 Error_Msg_N ("type with a dimension system expected", Assoc);
4902 if Has_Aspects (N) and then Nkind (Parent (N)) /= N_Compilation_Unit then
4903 Analyze_Aspect_Specifications (N, Act_Decl_Id);
4906 Ignore_SPARK_Mode_Pragmas_In_Instance := Saved_ISMP;
4907 Restore_Ghost_Region (Saved_GM, Saved_IGR);
4908 Restore_SPARK_Mode (Saved_SM, Saved_SMP);
4909 Style_Check := Saved_Style_Check;
4912 when Instantiation_Error =>
4913 if Parent_Installed then
4917 if Env_Installed then
4921 Ignore_SPARK_Mode_Pragmas_In_Instance := Saved_ISMP;
4922 Restore_Ghost_Region (Saved_GM, Saved_IGR);
4923 Restore_SPARK_Mode (Saved_SM, Saved_SMP);
4924 Style_Check := Saved_Style_Check;
4925 end Analyze_Package_Instantiation;
4927 --------------------------
4928 -- Inline_Instance_Body --
4929 --------------------------
4931 -- WARNING: This routine manages SPARK regions. Return statements must be
4932 -- replaced by gotos which jump to the end of the routine and restore the
4935 procedure Inline_Instance_Body
4937 Gen_Unit : Entity_Id;
4940 Config_Attrs : constant Config_Switches_Type := Save_Config_Switches;
4942 Curr_Comp : constant Node_Id := Cunit (Current_Sem_Unit);
4943 Curr_Unit : constant Entity_Id := Cunit_Entity (Current_Sem_Unit);
4944 Gen_Comp : constant Entity_Id :=
4945 Cunit_Entity (Get_Source_Unit (Gen_Unit));
4947 Scope_Stack_Depth : constant Pos :=
4948 Scope_Stack.Last - Scope_Stack.First + 1;
4950 Inner_Scopes : array (1 .. Scope_Stack_Depth) of Entity_Id;
4951 Instances : array (1 .. Scope_Stack_Depth) of Entity_Id;
4952 Use_Clauses : array (1 .. Scope_Stack_Depth) of Node_Id;
4954 Curr_Scope : Entity_Id := Empty;
4955 List : Elist_Id := No_Elist; -- init to avoid warning
4956 N_Instances : Nat := 0;
4957 Num_Inner : Nat := 0;
4958 Num_Scopes : Nat := 0;
4959 Removed : Boolean := False;
4964 -- Case of generic unit defined in another unit. We must remove the
4965 -- complete context of the current unit to install that of the generic.
4967 if Gen_Comp /= Cunit_Entity (Current_Sem_Unit) then
4969 -- Add some comments for the following two loops ???
4972 while Present (S) and then S /= Standard_Standard loop
4974 Num_Scopes := Num_Scopes + 1;
4976 Use_Clauses (Num_Scopes) :=
4978 (Scope_Stack.Last - Num_Scopes + 1).
4980 End_Use_Clauses (Use_Clauses (Num_Scopes));
4982 exit when Scope_Stack.Last - Num_Scopes + 1 = Scope_Stack.First
4983 or else Scope_Stack.Table
4984 (Scope_Stack.Last - Num_Scopes).Entity = Scope (S);
4987 exit when Is_Generic_Instance (S)
4988 and then (In_Package_Body (S)
4989 or else Ekind (S) = E_Procedure
4990 or else Ekind (S) = E_Function);
4994 Vis := Is_Immediately_Visible (Gen_Comp);
4996 -- Find and save all enclosing instances
5001 and then S /= Standard_Standard
5003 if Is_Generic_Instance (S) then
5004 N_Instances := N_Instances + 1;
5005 Instances (N_Instances) := S;
5007 exit when In_Package_Body (S);
5013 -- Remove context of current compilation unit, unless we are within a
5014 -- nested package instantiation, in which case the context has been
5015 -- removed previously.
5017 -- If current scope is the body of a child unit, remove context of
5018 -- spec as well. If an enclosing scope is an instance body, the
5019 -- context has already been removed, but the entities in the body
5020 -- must be made invisible as well.
5023 while Present (S) and then S /= Standard_Standard loop
5024 if Is_Generic_Instance (S)
5025 and then (In_Package_Body (S)
5026 or else Ekind_In (S, E_Procedure, E_Function))
5028 -- We still have to remove the entities of the enclosing
5029 -- instance from direct visibility.
5034 E := First_Entity (S);
5035 while Present (E) loop
5036 Set_Is_Immediately_Visible (E, False);
5045 or else (Ekind (Curr_Unit) = E_Package_Body
5046 and then S = Spec_Entity (Curr_Unit))
5047 or else (Ekind (Curr_Unit) = E_Subprogram_Body
5048 and then S = Corresponding_Spec
5049 (Unit_Declaration_Node (Curr_Unit)))
5053 -- Remove entities in current scopes from visibility, so that
5054 -- instance body is compiled in a clean environment.
5056 List := Save_Scope_Stack (Handle_Use => False);
5058 if Is_Child_Unit (S) then
5060 -- Remove child unit from stack, as well as inner scopes.
5061 -- Removing the context of a child unit removes parent units
5064 while Current_Scope /= S loop
5065 Num_Inner := Num_Inner + 1;
5066 Inner_Scopes (Num_Inner) := Current_Scope;
5071 Remove_Context (Curr_Comp);
5075 Remove_Context (Curr_Comp);
5078 if Ekind (Curr_Unit) = E_Package_Body then
5079 Remove_Context (Library_Unit (Curr_Comp));
5086 pragma Assert (Num_Inner < Num_Scopes);
5088 Push_Scope (Standard_Standard);
5089 Scope_Stack.Table (Scope_Stack.Last).Is_Active_Stack_Base := True;
5091 -- The inlined package body is analyzed with the configuration state
5092 -- of the context prior to the scope manipulations performed above.
5094 -- ??? shouldn't this also use the warning state of the context prior
5095 -- to the scope manipulations?
5097 Instantiate_Package_Body
5099 ((Act_Decl => Act_Decl,
5100 Config_Switches => Config_Attrs,
5101 Current_Sem_Unit => Current_Sem_Unit,
5102 Expander_Status => Expander_Active,
5104 Local_Suppress_Stack_Top => Local_Suppress_Stack_Top,
5105 Scope_Suppress => Scope_Suppress,
5106 Warnings => Save_Warnings)),
5107 Inlined_Body => True);
5113 Set_Is_Immediately_Visible (Gen_Comp, Vis);
5115 -- Reset Generic_Instance flag so that use clauses can be installed
5116 -- in the proper order. (See Use_One_Package for effect of enclosing
5117 -- instances on processing of use clauses).
5119 for J in 1 .. N_Instances loop
5120 Set_Is_Generic_Instance (Instances (J), False);
5124 Install_Context (Curr_Comp, Chain => False);
5126 if Present (Curr_Scope)
5127 and then Is_Child_Unit (Curr_Scope)
5129 Push_Scope (Curr_Scope);
5130 Set_Is_Immediately_Visible (Curr_Scope);
5132 -- Finally, restore inner scopes as well
5134 for J in reverse 1 .. Num_Inner loop
5135 Push_Scope (Inner_Scopes (J));
5139 Restore_Scope_Stack (List, Handle_Use => False);
5141 if Present (Curr_Scope)
5143 (In_Private_Part (Curr_Scope)
5144 or else In_Package_Body (Curr_Scope))
5146 -- Install private declaration of ancestor units, which are
5147 -- currently available. Restore_Scope_Stack and Install_Context
5148 -- only install the visible part of parents.
5153 Par := Scope (Curr_Scope);
5154 while (Present (Par)) and then Par /= Standard_Standard loop
5155 Install_Private_Declarations (Par);
5162 -- Restore use clauses. For a child unit, use clauses in the parents
5163 -- are restored when installing the context, so only those in inner
5164 -- scopes (and those local to the child unit itself) need to be
5165 -- installed explicitly.
5167 if Is_Child_Unit (Curr_Unit) and then Removed then
5168 for J in reverse 1 .. Num_Inner + 1 loop
5169 Scope_Stack.Table (Scope_Stack.Last - J + 1).First_Use_Clause :=
5171 Install_Use_Clauses (Use_Clauses (J));
5175 for J in reverse 1 .. Num_Scopes loop
5176 Scope_Stack.Table (Scope_Stack.Last - J + 1).First_Use_Clause :=
5178 Install_Use_Clauses (Use_Clauses (J));
5182 -- Restore status of instances. If one of them is a body, make its
5183 -- local entities visible again.
5190 for J in 1 .. N_Instances loop
5191 Inst := Instances (J);
5192 Set_Is_Generic_Instance (Inst, True);
5194 if In_Package_Body (Inst)
5195 or else Ekind_In (S, E_Procedure, E_Function)
5197 E := First_Entity (Instances (J));
5198 while Present (E) loop
5199 Set_Is_Immediately_Visible (E);
5206 -- If generic unit is in current unit, current context is correct. Note
5207 -- that the context is guaranteed to carry the correct SPARK_Mode as no
5208 -- enclosing scopes were removed.
5211 Instantiate_Package_Body
5213 ((Act_Decl => Act_Decl,
5214 Config_Switches => Save_Config_Switches,
5215 Current_Sem_Unit => Current_Sem_Unit,
5216 Expander_Status => Expander_Active,
5218 Local_Suppress_Stack_Top => Local_Suppress_Stack_Top,
5219 Scope_Suppress => Scope_Suppress,
5220 Warnings => Save_Warnings)),
5221 Inlined_Body => True);
5223 end Inline_Instance_Body;
5225 -------------------------------------
5226 -- Analyze_Procedure_Instantiation --
5227 -------------------------------------
5229 procedure Analyze_Procedure_Instantiation (N : Node_Id) is
5231 Analyze_Subprogram_Instantiation (N, E_Procedure);
5232 end Analyze_Procedure_Instantiation;
5234 -----------------------------------
5235 -- Need_Subprogram_Instance_Body --
5236 -----------------------------------
5238 function Need_Subprogram_Instance_Body
5240 Subp : Entity_Id) return Boolean
5242 function Is_Inlined_Or_Child_Of_Inlined (E : Entity_Id) return Boolean;
5243 -- Return True if E is an inlined subprogram, an inlined renaming or a
5244 -- subprogram nested in an inlined subprogram. The inlining machinery
5245 -- totally disregards nested subprograms since it considers that they
5246 -- will always be compiled if the parent is (see Inline.Is_Nested).
5248 ------------------------------------
5249 -- Is_Inlined_Or_Child_Of_Inlined --
5250 ------------------------------------
5252 function Is_Inlined_Or_Child_Of_Inlined (E : Entity_Id) return Boolean is
5256 if Is_Inlined (E) or else Is_Inlined (Alias (E)) then
5261 while Scop /= Standard_Standard loop
5262 if Ekind (Scop) in Subprogram_Kind and then Is_Inlined (Scop) then
5266 Scop := Scope (Scop);
5270 end Is_Inlined_Or_Child_Of_Inlined;
5273 -- Must be in the main unit or inlined (or child of inlined)
5275 if (Is_In_Main_Unit (N) or else Is_Inlined_Or_Child_Of_Inlined (Subp))
5277 -- Must be generating code or analyzing code in GNATprove mode
5279 and then (Operating_Mode = Generate_Code
5280 or else (Operating_Mode = Check_Semantics
5281 and then GNATprove_Mode))
5283 -- The body is needed when generating code (full expansion) and in
5284 -- in GNATprove mode (special expansion) for formal verification of
5287 and then (Expander_Active or GNATprove_Mode)
5289 -- No point in inlining if ABE is inevitable
5291 and then not Is_Known_Guaranteed_ABE (N)
5293 -- Or if subprogram is eliminated
5295 and then not Is_Eliminated (Subp)
5297 Add_Pending_Instantiation (N, Unit_Declaration_Node (Subp));
5300 -- Here if not inlined, or we ignore the inlining
5305 end Need_Subprogram_Instance_Body;
5307 --------------------------------------
5308 -- Analyze_Subprogram_Instantiation --
5309 --------------------------------------
5311 -- WARNING: This routine manages Ghost and SPARK regions. Return statements
5312 -- must be replaced by gotos which jump to the end of the routine in order
5313 -- to restore the Ghost and SPARK modes.
5315 procedure Analyze_Subprogram_Instantiation
5319 Errs : constant Nat := Serious_Errors_Detected;
5320 Gen_Id : constant Node_Id := Name (N);
5321 Inst_Id : constant Entity_Id := Defining_Entity (N);
5322 Anon_Id : constant Entity_Id :=
5323 Make_Defining_Identifier (Sloc (Inst_Id),
5324 Chars => New_External_Name (Chars (Inst_Id), 'R'));
5325 Loc : constant Source_Ptr := Sloc (N);
5327 Act_Decl_Id : Entity_Id := Empty; -- init to avoid warning
5332 Env_Installed : Boolean := False;
5333 Gen_Unit : Entity_Id;
5335 Pack_Id : Entity_Id;
5336 Parent_Installed : Boolean := False;
5338 Renaming_List : List_Id;
5339 -- The list of declarations that link formals and actuals of the
5340 -- instance. These are subtype declarations for formal types, and
5341 -- renaming declarations for other formals. The subprogram declaration
5342 -- for the instance is then appended to the list, and the last item on
5343 -- the list is the renaming declaration for the instance.
5345 procedure Analyze_Instance_And_Renamings;
5346 -- The instance must be analyzed in a context that includes the mappings
5347 -- of generic parameters into actuals. We create a package declaration
5348 -- for this purpose, and a subprogram with an internal name within the
5349 -- package. The subprogram instance is simply an alias for the internal
5350 -- subprogram, declared in the current scope.
5352 procedure Build_Subprogram_Renaming;
5353 -- If the subprogram is recursive, there are occurrences of the name of
5354 -- the generic within the body, which must resolve to the current
5355 -- instance. We add a renaming declaration after the declaration, which
5356 -- is available in the instance body, as well as in the analysis of
5357 -- aspects that appear in the generic. This renaming declaration is
5358 -- inserted after the instance declaration which it renames.
5360 ------------------------------------
5361 -- Analyze_Instance_And_Renamings --
5362 ------------------------------------
5364 procedure Analyze_Instance_And_Renamings is
5365 Def_Ent : constant Entity_Id := Defining_Entity (N);
5366 Pack_Decl : Node_Id;
5369 if Nkind (Parent (N)) = N_Compilation_Unit then
5371 -- For the case of a compilation unit, the container package has
5372 -- the same name as the instantiation, to insure that the binder
5373 -- calls the elaboration procedure with the right name. Copy the
5374 -- entity of the instance, which may have compilation level flags
5375 -- (e.g. Is_Child_Unit) set.
5377 Pack_Id := New_Copy (Def_Ent);
5380 -- Otherwise we use the name of the instantiation concatenated
5381 -- with its source position to ensure uniqueness if there are
5382 -- several instantiations with the same name.
5385 Make_Defining_Identifier (Loc,
5386 Chars => New_External_Name
5387 (Related_Id => Chars (Def_Ent),
5389 Suffix_Index => Source_Offset (Sloc (Def_Ent))));
5393 Make_Package_Declaration (Loc,
5394 Specification => Make_Package_Specification (Loc,
5395 Defining_Unit_Name => Pack_Id,
5396 Visible_Declarations => Renaming_List,
5397 End_Label => Empty));
5399 Set_Instance_Spec (N, Pack_Decl);
5400 Set_Is_Generic_Instance (Pack_Id);
5401 Set_Debug_Info_Needed (Pack_Id);
5403 -- Case of not a compilation unit
5405 if Nkind (Parent (N)) /= N_Compilation_Unit then
5406 Mark_Rewrite_Insertion (Pack_Decl);
5407 Insert_Before (N, Pack_Decl);
5408 Set_Has_Completion (Pack_Id);
5410 -- Case of an instantiation that is a compilation unit
5412 -- Place declaration on current node so context is complete for
5413 -- analysis (including nested instantiations), and for use in a
5414 -- context_clause (see Analyze_With_Clause).
5417 Set_Unit (Parent (N), Pack_Decl);
5418 Set_Parent_Spec (Pack_Decl, Parent_Spec (N));
5421 Analyze (Pack_Decl);
5422 Check_Formal_Packages (Pack_Id);
5424 -- Body of the enclosing package is supplied when instantiating the
5425 -- subprogram body, after semantic analysis is completed.
5427 if Nkind (Parent (N)) = N_Compilation_Unit then
5429 -- Remove package itself from visibility, so it does not
5430 -- conflict with subprogram.
5432 Set_Name_Entity_Id (Chars (Pack_Id), Homonym (Pack_Id));
5434 -- Set name and scope of internal subprogram so that the proper
5435 -- external name will be generated. The proper scope is the scope
5436 -- of the wrapper package. We need to generate debugging info for
5437 -- the internal subprogram, so set flag accordingly.
5439 Set_Chars (Anon_Id, Chars (Defining_Entity (N)));
5440 Set_Scope (Anon_Id, Scope (Pack_Id));
5442 -- Mark wrapper package as referenced, to avoid spurious warnings
5443 -- if the instantiation appears in various with_ clauses of
5444 -- subunits of the main unit.
5446 Set_Referenced (Pack_Id);
5449 Set_Is_Generic_Instance (Anon_Id);
5450 Set_Debug_Info_Needed (Anon_Id);
5451 Act_Decl_Id := New_Copy (Anon_Id);
5453 Set_Parent (Act_Decl_Id, Parent (Anon_Id));
5454 Set_Chars (Act_Decl_Id, Chars (Defining_Entity (N)));
5455 Set_Sloc (Act_Decl_Id, Sloc (Defining_Entity (N)));
5457 -- Subprogram instance comes from source only if generic does
5459 Set_Comes_From_Source (Act_Decl_Id, Comes_From_Source (Gen_Unit));
5461 -- If the instance is a child unit, mark the Id accordingly. Mark
5462 -- the anonymous entity as well, which is the real subprogram and
5463 -- which is used when the instance appears in a context clause.
5464 -- Similarly, propagate the Is_Eliminated flag to handle properly
5465 -- nested eliminated subprograms.
5467 Set_Is_Child_Unit (Act_Decl_Id, Is_Child_Unit (Defining_Entity (N)));
5468 Set_Is_Child_Unit (Anon_Id, Is_Child_Unit (Defining_Entity (N)));
5469 New_Overloaded_Entity (Act_Decl_Id);
5470 Check_Eliminated (Act_Decl_Id);
5471 Set_Is_Eliminated (Anon_Id, Is_Eliminated (Act_Decl_Id));
5473 if Nkind (Parent (N)) = N_Compilation_Unit then
5475 -- In compilation unit case, kill elaboration checks on the
5476 -- instantiation, since they are never needed - the body is
5477 -- instantiated at the same point as the spec.
5479 if Legacy_Elaboration_Checks then
5480 Set_Kill_Elaboration_Checks (Act_Decl_Id);
5481 Set_Suppress_Elaboration_Warnings (Act_Decl_Id);
5484 Set_Is_Compilation_Unit (Anon_Id);
5485 Set_Cunit_Entity (Current_Sem_Unit, Pack_Id);
5488 -- The instance is not a freezing point for the new subprogram.
5489 -- The anonymous subprogram may have a freeze node, created for
5490 -- some delayed aspects. This freeze node must not be inherited
5491 -- by the visible subprogram entity.
5493 Set_Is_Frozen (Act_Decl_Id, False);
5494 Set_Freeze_Node (Act_Decl_Id, Empty);
5496 if Nkind (Defining_Entity (N)) = N_Defining_Operator_Symbol then
5497 Valid_Operator_Definition (Act_Decl_Id);
5500 Set_Alias (Act_Decl_Id, Anon_Id);
5501 Set_Has_Completion (Act_Decl_Id);
5502 Set_Related_Instance (Pack_Id, Act_Decl_Id);
5504 if Nkind (Parent (N)) = N_Compilation_Unit then
5505 Set_Body_Required (Parent (N), False);
5507 end Analyze_Instance_And_Renamings;
5509 -------------------------------
5510 -- Build_Subprogram_Renaming --
5511 -------------------------------
5513 procedure Build_Subprogram_Renaming is
5514 Renaming_Decl : Node_Id;
5515 Unit_Renaming : Node_Id;
5519 Make_Subprogram_Renaming_Declaration (Loc,
5522 (Specification (Original_Node (Gen_Decl)),
5524 Instantiating => True),
5525 Name => New_Occurrence_Of (Anon_Id, Loc));
5527 -- The generic may be a child unit. The renaming needs an identifier
5528 -- with the proper name.
5530 Set_Defining_Unit_Name (Specification (Unit_Renaming),
5531 Make_Defining_Identifier (Loc, Chars (Gen_Unit)));
5533 -- If there is a formal subprogram with the same name as the unit
5534 -- itself, do not add this renaming declaration, to prevent
5535 -- ambiguities when there is a call with that name in the body.
5536 -- This is a partial and ugly fix for one ACATS test. ???
5538 Renaming_Decl := First (Renaming_List);
5539 while Present (Renaming_Decl) loop
5540 if Nkind (Renaming_Decl) = N_Subprogram_Renaming_Declaration
5542 Chars (Defining_Entity (Renaming_Decl)) = Chars (Gen_Unit)
5547 Next (Renaming_Decl);
5550 if No (Renaming_Decl) then
5551 Append (Unit_Renaming, Renaming_List);
5553 end Build_Subprogram_Renaming;
5557 Saved_GM : constant Ghost_Mode_Type := Ghost_Mode;
5558 Saved_IGR : constant Node_Id := Ignored_Ghost_Region;
5559 Saved_ISMP : constant Boolean :=
5560 Ignore_SPARK_Mode_Pragmas_In_Instance;
5561 Saved_SM : constant SPARK_Mode_Type := SPARK_Mode;
5562 Saved_SMP : constant Node_Id := SPARK_Mode_Pragma;
5563 -- Save the Ghost and SPARK mode-related data to restore on exit
5565 Vis_Prims_List : Elist_Id := No_Elist;
5566 -- List of primitives made temporarily visible in the instantiation
5567 -- to match the visibility of the formal type
5569 -- Start of processing for Analyze_Subprogram_Instantiation
5572 -- Preserve relevant elaboration-related attributes of the context which
5573 -- are no longer available or very expensive to recompute once analysis,
5574 -- resolution, and expansion are over.
5576 Mark_Elaboration_Attributes
5583 -- Very first thing: check for special Text_IO unit in case we are
5584 -- instantiating one of the children of [[Wide_]Wide_]Text_IO. Of course
5585 -- such an instantiation is bogus (these are packages, not subprograms),
5586 -- but we get a better error message if we do this.
5588 Check_Text_IO_Special_Unit (Gen_Id);
5590 -- Make node global for error reporting
5592 Instantiation_Node := N;
5594 -- For package instantiations we turn off style checks, because they
5595 -- will have been emitted in the generic. For subprogram instantiations
5596 -- we want to apply at least the check on overriding indicators so we
5597 -- do not modify the style check status.
5599 -- The renaming declarations for the actuals do not come from source and
5600 -- will not generate spurious warnings.
5602 Preanalyze_Actuals (N);
5605 Env_Installed := True;
5606 Check_Generic_Child_Unit (Gen_Id, Parent_Installed);
5607 Gen_Unit := Entity (Gen_Id);
5609 -- A subprogram instantiation is Ghost when it is subject to pragma
5610 -- Ghost or the generic template is Ghost. Set the mode now to ensure
5611 -- that any nodes generated during analysis and expansion are marked as
5614 Mark_And_Set_Ghost_Instantiation (N, Gen_Unit);
5616 Generate_Reference (Gen_Unit, Gen_Id);
5618 if Nkind (Gen_Id) = N_Identifier
5619 and then Chars (Gen_Unit) = Chars (Defining_Entity (N))
5622 ("& is hidden within declaration of instance", Gen_Id, Gen_Unit);
5625 if Etype (Gen_Unit) = Any_Type then
5630 -- Verify that it is a generic subprogram of the right kind, and that
5631 -- it does not lead to a circular instantiation.
5633 if K = E_Procedure and then Ekind (Gen_Unit) /= E_Generic_Procedure then
5635 ("& is not the name of a generic procedure", Gen_Id, Gen_Unit);
5637 elsif K = E_Function and then Ekind (Gen_Unit) /= E_Generic_Function then
5639 ("& is not the name of a generic function", Gen_Id, Gen_Unit);
5641 elsif In_Open_Scopes (Gen_Unit) then
5642 Error_Msg_NE ("instantiation of & within itself", N, Gen_Unit);
5645 Set_Ekind (Inst_Id, K);
5646 Set_Scope (Inst_Id, Current_Scope);
5648 Set_Entity (Gen_Id, Gen_Unit);
5649 Set_Is_Instantiated (Gen_Unit);
5651 if In_Extended_Main_Source_Unit (N) then
5652 Generate_Reference (Gen_Unit, N);
5655 -- If renaming, get original unit
5657 if Present (Renamed_Object (Gen_Unit))
5658 and then Ekind_In (Renamed_Object (Gen_Unit), E_Generic_Procedure,
5661 Gen_Unit := Renamed_Object (Gen_Unit);
5662 Set_Is_Instantiated (Gen_Unit);
5663 Generate_Reference (Gen_Unit, N);
5666 if Contains_Instance_Of (Gen_Unit, Current_Scope, Gen_Id) then
5667 Error_Msg_Node_2 := Current_Scope;
5669 ("circular Instantiation: & instantiated in &!", N, Gen_Unit);
5670 Circularity_Detected := True;
5671 Restore_Hidden_Primitives (Vis_Prims_List);
5675 Gen_Decl := Unit_Declaration_Node (Gen_Unit);
5677 -- Initialize renamings map, for error checking
5679 Generic_Renamings.Set_Last (0);
5680 Generic_Renamings_HTable.Reset;
5682 Create_Instantiation_Source (N, Gen_Unit, S_Adjustment);
5684 -- Copy original generic tree, to produce text for instantiation
5688 (Original_Node (Gen_Decl), Empty, Instantiating => True);
5690 -- Inherit overriding indicator from instance node
5692 Act_Spec := Specification (Act_Tree);
5693 Set_Must_Override (Act_Spec, Must_Override (N));
5694 Set_Must_Not_Override (Act_Spec, Must_Not_Override (N));
5697 Analyze_Associations
5699 Formals => Generic_Formal_Declarations (Act_Tree),
5700 F_Copy => Generic_Formal_Declarations (Gen_Decl));
5702 Vis_Prims_List := Check_Hidden_Primitives (Renaming_List);
5704 -- The subprogram itself cannot contain a nested instance, so the
5705 -- current parent is left empty.
5707 Set_Instance_Env (Gen_Unit, Empty);
5709 -- Build the subprogram declaration, which does not appear in the
5710 -- generic template, and give it a sloc consistent with that of the
5713 Set_Defining_Unit_Name (Act_Spec, Anon_Id);
5714 Set_Generic_Parent (Act_Spec, Gen_Unit);
5716 Make_Subprogram_Declaration (Sloc (Act_Spec),
5717 Specification => Act_Spec);
5719 -- The aspects have been copied previously, but they have to be
5720 -- linked explicitly to the new subprogram declaration. Explicit
5721 -- pre/postconditions on the instance are analyzed below, in a
5724 Move_Aspects (Act_Tree, To => Act_Decl);
5725 Set_Categorization_From_Pragmas (Act_Decl);
5727 if Parent_Installed then
5731 Append (Act_Decl, Renaming_List);
5733 -- Contract-related source pragmas that follow a generic subprogram
5734 -- must be instantiated explicitly because they are not part of the
5735 -- subprogram template.
5737 Instantiate_Subprogram_Contract
5738 (Original_Node (Gen_Decl), Renaming_List);
5740 Build_Subprogram_Renaming;
5742 -- If the context of the instance is subject to SPARK_Mode "off" or
5743 -- the annotation is altogether missing, set the global flag which
5744 -- signals Analyze_Pragma to ignore all SPARK_Mode pragmas within
5745 -- the instance. This should be done prior to analyzing the instance.
5747 if SPARK_Mode /= On then
5748 Ignore_SPARK_Mode_Pragmas_In_Instance := True;
5751 -- If the context of an instance is not subject to SPARK_Mode "off",
5752 -- and the generic spec is subject to an explicit SPARK_Mode pragma,
5753 -- the latter should be the one applicable to the instance.
5755 if not Ignore_SPARK_Mode_Pragmas_In_Instance
5756 and then Saved_SM /= Off
5757 and then Present (SPARK_Pragma (Gen_Unit))
5759 Set_SPARK_Mode (Gen_Unit);
5762 Analyze_Instance_And_Renamings;
5764 -- Restore SPARK_Mode from the context after analysis of the package
5765 -- declaration, so that the SPARK_Mode on the generic spec does not
5766 -- apply to the pending instance for the instance body.
5768 if not Ignore_SPARK_Mode_Pragmas_In_Instance
5769 and then Saved_SM /= Off
5770 and then Present (SPARK_Pragma (Gen_Unit))
5772 Restore_SPARK_Mode (Saved_SM, Saved_SMP);
5775 -- If the generic is marked Import (Intrinsic), then so is the
5776 -- instance. This indicates that there is no body to instantiate. If
5777 -- generic is marked inline, so it the instance, and the anonymous
5778 -- subprogram it renames. If inlined, or else if inlining is enabled
5779 -- for the compilation, we generate the instance body even if it is
5780 -- not within the main unit.
5782 if Is_Intrinsic_Subprogram (Gen_Unit) then
5783 Set_Is_Intrinsic_Subprogram (Anon_Id);
5784 Set_Is_Intrinsic_Subprogram (Act_Decl_Id);
5786 if Chars (Gen_Unit) = Name_Unchecked_Conversion then
5787 Validate_Unchecked_Conversion (N, Act_Decl_Id);
5791 -- Inherit convention from generic unit. Intrinsic convention, as for
5792 -- an instance of unchecked conversion, is not inherited because an
5793 -- explicit Ada instance has been created.
5795 if Has_Convention_Pragma (Gen_Unit)
5796 and then Convention (Gen_Unit) /= Convention_Intrinsic
5798 Set_Convention (Act_Decl_Id, Convention (Gen_Unit));
5799 Set_Is_Exported (Act_Decl_Id, Is_Exported (Gen_Unit));
5802 Generate_Definition (Act_Decl_Id);
5804 -- Inherit all inlining-related flags which apply to the generic in
5805 -- the subprogram and its declaration.
5807 Set_Is_Inlined (Act_Decl_Id, Is_Inlined (Gen_Unit));
5808 Set_Is_Inlined (Anon_Id, Is_Inlined (Gen_Unit));
5810 Set_Has_Pragma_Inline (Act_Decl_Id, Has_Pragma_Inline (Gen_Unit));
5811 Set_Has_Pragma_Inline (Anon_Id, Has_Pragma_Inline (Gen_Unit));
5813 Set_Has_Pragma_Inline_Always
5814 (Act_Decl_Id, Has_Pragma_Inline_Always (Gen_Unit));
5815 Set_Has_Pragma_Inline_Always
5816 (Anon_Id, Has_Pragma_Inline_Always (Gen_Unit));
5818 Set_Has_Pragma_No_Inline
5819 (Act_Decl_Id, Has_Pragma_No_Inline (Gen_Unit));
5820 Set_Has_Pragma_No_Inline
5821 (Anon_Id, Has_Pragma_No_Inline (Gen_Unit));
5823 -- Propagate No_Return if pragma applied to generic unit. This must
5824 -- be done explicitly because pragma does not appear in generic
5825 -- declaration (unlike the aspect case).
5827 if No_Return (Gen_Unit) then
5828 Set_No_Return (Act_Decl_Id);
5829 Set_No_Return (Anon_Id);
5832 -- Mark both the instance spec and the anonymous package in case the
5833 -- body is instantiated at a later pass. This preserves the original
5834 -- context in effect for the body.
5836 if SPARK_Mode /= On then
5837 Set_Ignore_SPARK_Mode_Pragmas (Act_Decl_Id);
5838 Set_Ignore_SPARK_Mode_Pragmas (Anon_Id);
5841 if Legacy_Elaboration_Checks
5842 and then not Is_Intrinsic_Subprogram (Gen_Unit)
5844 Check_Elab_Instantiation (N);
5847 -- Save the scenario for later examination by the ABE Processing
5850 Record_Elaboration_Scenario (N);
5852 -- The instantiation results in a guaranteed ABE. Create a completing
5853 -- body for the subprogram declaration because the real body will not
5856 if Is_Known_Guaranteed_ABE (N) then
5857 Provide_Completing_Bodies (Instance_Spec (N));
5860 if Is_Dispatching_Operation (Act_Decl_Id)
5861 and then Ada_Version >= Ada_2005
5867 Formal := First_Formal (Act_Decl_Id);
5868 while Present (Formal) loop
5869 if Ekind (Etype (Formal)) = E_Anonymous_Access_Type
5870 and then Is_Controlling_Formal (Formal)
5871 and then not Can_Never_Be_Null (Formal)
5874 ("access parameter& is controlling,", N, Formal);
5876 ("\corresponding parameter of & must be explicitly "
5877 & "null-excluding", N, Gen_Id);
5880 Next_Formal (Formal);
5885 Check_Hidden_Child_Unit (N, Gen_Unit, Act_Decl_Id);
5887 Validate_Categorization_Dependency (N, Act_Decl_Id);
5889 if not Is_Intrinsic_Subprogram (Act_Decl_Id) then
5890 Inherit_Context (Gen_Decl, N);
5892 Restore_Private_Views (Pack_Id, False);
5894 -- If the context requires a full instantiation, mark node for
5895 -- subsequent construction of the body.
5897 if Need_Subprogram_Instance_Body (N, Act_Decl_Id) then
5898 Check_Forward_Instantiation (Gen_Decl);
5900 -- The wrapper package is always delayed, because it does not
5901 -- constitute a freeze point, but to insure that the freeze node
5902 -- is placed properly, it is created directly when instantiating
5903 -- the body (otherwise the freeze node might appear to early for
5904 -- nested instantiations).
5906 elsif Nkind (Parent (N)) = N_Compilation_Unit then
5907 Rewrite (N, Unit (Parent (N)));
5908 Set_Unit (Parent (N), N);
5911 -- Replace instance node for library-level instantiations of
5912 -- intrinsic subprograms.
5914 elsif Nkind (Parent (N)) = N_Compilation_Unit then
5915 Rewrite (N, Unit (Parent (N)));
5916 Set_Unit (Parent (N), N);
5919 if Parent_Installed then
5923 Restore_Hidden_Primitives (Vis_Prims_List);
5925 Env_Installed := False;
5926 Generic_Renamings.Set_Last (0);
5927 Generic_Renamings_HTable.Reset;
5931 -- Analyze aspects in declaration if no errors appear in the instance.
5933 if Has_Aspects (N) and then Serious_Errors_Detected = Errs then
5934 Analyze_Aspect_Specifications (N, Act_Decl_Id);
5937 Ignore_SPARK_Mode_Pragmas_In_Instance := Saved_ISMP;
5938 Restore_Ghost_Region (Saved_GM, Saved_IGR);
5939 Restore_SPARK_Mode (Saved_SM, Saved_SMP);
5942 when Instantiation_Error =>
5943 if Parent_Installed then
5947 if Env_Installed then
5951 Ignore_SPARK_Mode_Pragmas_In_Instance := Saved_ISMP;
5952 Restore_Ghost_Region (Saved_GM, Saved_IGR);
5953 Restore_SPARK_Mode (Saved_SM, Saved_SMP);
5954 end Analyze_Subprogram_Instantiation;
5956 -------------------------
5957 -- Get_Associated_Node --
5958 -------------------------
5960 function Get_Associated_Node (N : Node_Id) return Node_Id is
5964 Assoc := Associated_Node (N);
5966 if Nkind (Assoc) /= Nkind (N) then
5969 elsif Nkind_In (Assoc, N_Aggregate, N_Extension_Aggregate) then
5973 -- If the node is part of an inner generic, it may itself have been
5974 -- remapped into a further generic copy. Associated_Node is otherwise
5975 -- used for the entity of the node, and will be of a different node
5976 -- kind, or else N has been rewritten as a literal or function call.
5978 while Present (Associated_Node (Assoc))
5979 and then Nkind (Associated_Node (Assoc)) = Nkind (Assoc)
5981 Assoc := Associated_Node (Assoc);
5984 -- Follow an additional link in case the final node was rewritten.
5985 -- This can only happen with nested generic units.
5987 if (Nkind (Assoc) = N_Identifier or else Nkind (Assoc) in N_Op)
5988 and then Present (Associated_Node (Assoc))
5989 and then (Nkind_In (Associated_Node (Assoc), N_Function_Call,
5990 N_Explicit_Dereference,
5995 Assoc := Associated_Node (Assoc);
5998 -- An additional special case: an unconstrained type in an object
5999 -- declaration may have been rewritten as a local subtype constrained
6000 -- by the expression in the declaration. We need to recover the
6001 -- original entity, which may be global.
6003 if Present (Original_Node (Assoc))
6004 and then Nkind (Parent (N)) = N_Object_Declaration
6006 Assoc := Original_Node (Assoc);
6011 end Get_Associated_Node;
6013 ----------------------------
6014 -- Build_Function_Wrapper --
6015 ----------------------------
6017 function Build_Function_Wrapper
6018 (Formal_Subp : Entity_Id;
6019 Actual_Subp : Entity_Id) return Node_Id
6021 Loc : constant Source_Ptr := Sloc (Current_Scope);
6022 Ret_Type : constant Entity_Id := Get_Instance_Of (Etype (Formal_Subp));
6025 Func_Name : Node_Id;
6027 Parm_Type : Node_Id;
6028 Profile : List_Id := New_List;
6035 Func_Name := New_Occurrence_Of (Actual_Subp, Loc);
6037 Func := Make_Defining_Identifier (Loc, Chars (Formal_Subp));
6038 Set_Ekind (Func, E_Function);
6039 Set_Is_Generic_Actual_Subprogram (Func);
6041 Actuals := New_List;
6042 Profile := New_List;
6044 Act_F := First_Formal (Actual_Subp);
6045 Form_F := First_Formal (Formal_Subp);
6046 while Present (Form_F) loop
6048 -- Create new formal for profile of wrapper, and add a reference
6049 -- to it in the list of actuals for the enclosing call. The name
6050 -- must be that of the formal in the formal subprogram, because
6051 -- calls to it in the generic body may use named associations.
6053 New_F := Make_Defining_Identifier (Loc, Chars (Form_F));
6056 New_Occurrence_Of (Get_Instance_Of (Etype (Form_F)), Loc);
6059 Make_Parameter_Specification (Loc,
6060 Defining_Identifier => New_F,
6061 Parameter_Type => Parm_Type));
6063 Append_To (Actuals, New_Occurrence_Of (New_F, Loc));
6064 Next_Formal (Form_F);
6066 if Present (Act_F) then
6067 Next_Formal (Act_F);
6072 Make_Function_Specification (Loc,
6073 Defining_Unit_Name => Func,
6074 Parameter_Specifications => Profile,
6075 Result_Definition => New_Occurrence_Of (Ret_Type, Loc));
6078 Make_Expression_Function (Loc,
6079 Specification => Spec,
6081 Make_Function_Call (Loc,
6083 Parameter_Associations => Actuals));
6086 end Build_Function_Wrapper;
6088 ----------------------------
6089 -- Build_Operator_Wrapper --
6090 ----------------------------
6092 function Build_Operator_Wrapper
6093 (Formal_Subp : Entity_Id;
6094 Actual_Subp : Entity_Id) return Node_Id
6096 Loc : constant Source_Ptr := Sloc (Current_Scope);
6097 Ret_Type : constant Entity_Id :=
6098 Get_Instance_Of (Etype (Formal_Subp));
6099 Op_Type : constant Entity_Id :=
6100 Get_Instance_Of (Etype (First_Formal (Formal_Subp)));
6101 Is_Binary : constant Boolean :=
6102 Present (Next_Formal (First_Formal (Formal_Subp)));
6105 Expr : Node_Id := Empty;
6113 Op_Name := Chars (Actual_Subp);
6115 -- Create entities for wrapper function and its formals
6117 F1 := Make_Temporary (Loc, 'A');
6118 F2 := Make_Temporary (Loc, 'B');
6119 L := New_Occurrence_Of (F1, Loc);
6120 R := New_Occurrence_Of (F2, Loc);
6122 Func := Make_Defining_Identifier (Loc, Chars (Formal_Subp));
6123 Set_Ekind (Func, E_Function);
6124 Set_Is_Generic_Actual_Subprogram (Func);
6127 Make_Function_Specification (Loc,
6128 Defining_Unit_Name => Func,
6129 Parameter_Specifications => New_List (
6130 Make_Parameter_Specification (Loc,
6131 Defining_Identifier => F1,
6132 Parameter_Type => New_Occurrence_Of (Op_Type, Loc))),
6133 Result_Definition => New_Occurrence_Of (Ret_Type, Loc));
6136 Append_To (Parameter_Specifications (Spec),
6137 Make_Parameter_Specification (Loc,
6138 Defining_Identifier => F2,
6139 Parameter_Type => New_Occurrence_Of (Op_Type, Loc)));
6142 -- Build expression as a function call, or as an operator node
6143 -- that corresponds to the name of the actual, starting with
6144 -- binary operators.
6146 if Op_Name not in Any_Operator_Name then
6148 Make_Function_Call (Loc,
6150 New_Occurrence_Of (Actual_Subp, Loc),
6151 Parameter_Associations => New_List (L));
6154 Append_To (Parameter_Associations (Expr), R);
6159 elsif Is_Binary then
6160 if Op_Name = Name_Op_And then
6161 Expr := Make_Op_And (Loc, Left_Opnd => L, Right_Opnd => R);
6162 elsif Op_Name = Name_Op_Or then
6163 Expr := Make_Op_Or (Loc, Left_Opnd => L, Right_Opnd => R);
6164 elsif Op_Name = Name_Op_Xor then
6165 Expr := Make_Op_Xor (Loc, Left_Opnd => L, Right_Opnd => R);
6166 elsif Op_Name = Name_Op_Eq then
6167 Expr := Make_Op_Eq (Loc, Left_Opnd => L, Right_Opnd => R);
6168 elsif Op_Name = Name_Op_Ne then
6169 Expr := Make_Op_Ne (Loc, Left_Opnd => L, Right_Opnd => R);
6170 elsif Op_Name = Name_Op_Le then
6171 Expr := Make_Op_Le (Loc, Left_Opnd => L, Right_Opnd => R);
6172 elsif Op_Name = Name_Op_Gt then
6173 Expr := Make_Op_Gt (Loc, Left_Opnd => L, Right_Opnd => R);
6174 elsif Op_Name = Name_Op_Ge then
6175 Expr := Make_Op_Ge (Loc, Left_Opnd => L, Right_Opnd => R);
6176 elsif Op_Name = Name_Op_Lt then
6177 Expr := Make_Op_Lt (Loc, Left_Opnd => L, Right_Opnd => R);
6178 elsif Op_Name = Name_Op_Add then
6179 Expr := Make_Op_Add (Loc, Left_Opnd => L, Right_Opnd => R);
6180 elsif Op_Name = Name_Op_Subtract then
6181 Expr := Make_Op_Subtract (Loc, Left_Opnd => L, Right_Opnd => R);
6182 elsif Op_Name = Name_Op_Concat then
6183 Expr := Make_Op_Concat (Loc, Left_Opnd => L, Right_Opnd => R);
6184 elsif Op_Name = Name_Op_Multiply then
6185 Expr := Make_Op_Multiply (Loc, Left_Opnd => L, Right_Opnd => R);
6186 elsif Op_Name = Name_Op_Divide then
6187 Expr := Make_Op_Divide (Loc, Left_Opnd => L, Right_Opnd => R);
6188 elsif Op_Name = Name_Op_Mod then
6189 Expr := Make_Op_Mod (Loc, Left_Opnd => L, Right_Opnd => R);
6190 elsif Op_Name = Name_Op_Rem then
6191 Expr := Make_Op_Rem (Loc, Left_Opnd => L, Right_Opnd => R);
6192 elsif Op_Name = Name_Op_Expon then
6193 Expr := Make_Op_Expon (Loc, Left_Opnd => L, Right_Opnd => R);
6199 if Op_Name = Name_Op_Add then
6200 Expr := Make_Op_Plus (Loc, Right_Opnd => L);
6201 elsif Op_Name = Name_Op_Subtract then
6202 Expr := Make_Op_Minus (Loc, Right_Opnd => L);
6203 elsif Op_Name = Name_Op_Abs then
6204 Expr := Make_Op_Abs (Loc, Right_Opnd => L);
6205 elsif Op_Name = Name_Op_Not then
6206 Expr := Make_Op_Not (Loc, Right_Opnd => L);
6211 Make_Expression_Function (Loc,
6212 Specification => Spec,
6213 Expression => Expr);
6216 end Build_Operator_Wrapper;
6218 -----------------------------------
6219 -- Build_Subprogram_Decl_Wrapper --
6220 -----------------------------------
6222 function Build_Subprogram_Decl_Wrapper
6223 (Formal_Subp : Entity_Id) return Node_Id
6225 Loc : constant Source_Ptr := Sloc (Current_Scope);
6226 Ret_Type : constant Entity_Id := Get_Instance_Of (Etype (Formal_Subp));
6229 Parm_Spec : Node_Id;
6230 Profile : List_Id := New_List;
6237 Subp := Make_Defining_Identifier (Loc, Chars (Formal_Subp));
6238 Set_Ekind (Subp, Ekind (Formal_Subp));
6239 Set_Is_Generic_Actual_Subprogram (Subp);
6241 Profile := Parameter_Specifications (
6243 (Specification (Unit_Declaration_Node (Formal_Subp))));
6245 Form_F := First_Formal (Formal_Subp);
6246 Parm_Spec := First (Profile);
6248 -- Create new entities for the formals. Reset entities so that
6249 -- parameter types are properly resolved when wrapper declaration
6252 while Present (Parm_Spec) loop
6253 New_F := Make_Defining_Identifier (Loc, Chars (Form_F));
6254 Set_Defining_Identifier (Parm_Spec, New_F);
6255 Set_Entity (Parameter_Type (Parm_Spec), Empty);
6257 Next_Formal (Form_F);
6260 if Ret_Type = Standard_Void_Type then
6262 Make_Procedure_Specification (Loc,
6263 Defining_Unit_Name => Subp,
6264 Parameter_Specifications => Profile);
6267 Make_Function_Specification (Loc,
6268 Defining_Unit_Name => Subp,
6269 Parameter_Specifications => Profile,
6270 Result_Definition => New_Occurrence_Of (Ret_Type, Loc));
6274 Make_Subprogram_Declaration (Loc, Specification => Spec);
6277 end Build_Subprogram_Decl_Wrapper;
6279 -----------------------------------
6280 -- Build_Subprogram_Body_Wrapper --
6281 -----------------------------------
6283 function Build_Subprogram_Body_Wrapper
6284 (Formal_Subp : Entity_Id;
6285 Actual_Name : Node_Id) return Node_Id
6287 Loc : constant Source_Ptr := Sloc (Current_Scope);
6288 Ret_Type : constant Entity_Id := Get_Instance_Of (Etype (Formal_Subp));
6289 Spec_Node : constant Node_Id :=
6291 (Build_Subprogram_Decl_Wrapper (Formal_Subp));
6294 Body_Node : Node_Id;
6297 Actuals := New_List;
6298 Act := First (Parameter_Specifications (Spec_Node));
6300 while Present (Act) loop
6302 Make_Identifier (Loc, Chars (Defining_Identifier (Act))));
6306 if Ret_Type = Standard_Void_Type then
6307 Stmt := Make_Procedure_Call_Statement (Loc,
6308 Name => Actual_Name,
6309 Parameter_Associations => Actuals);
6312 Stmt := Make_Simple_Return_Statement (Loc,
6314 Make_Function_Call (Loc,
6315 Name => Actual_Name,
6316 Parameter_Associations => Actuals));
6319 Body_Node := Make_Subprogram_Body (Loc,
6320 Specification => Spec_Node,
6321 Declarations => New_List,
6322 Handled_Statement_Sequence =>
6323 Make_Handled_Sequence_Of_Statements (Loc,
6324 Statements => New_List (Stmt)));
6327 end Build_Subprogram_Body_Wrapper;
6329 -------------------------------------------
6330 -- Build_Instance_Compilation_Unit_Nodes --
6331 -------------------------------------------
6333 procedure Build_Instance_Compilation_Unit_Nodes
6338 Decl_Cunit : Node_Id;
6339 Body_Cunit : Node_Id;
6341 New_Main : constant Entity_Id := Defining_Entity (Act_Decl);
6342 Old_Main : constant Entity_Id := Cunit_Entity (Main_Unit);
6345 -- A new compilation unit node is built for the instance declaration
6348 Make_Compilation_Unit (Sloc (N),
6349 Context_Items => Empty_List,
6351 Aux_Decls_Node => Make_Compilation_Unit_Aux (Sloc (N)));
6353 Set_Parent_Spec (Act_Decl, Parent_Spec (N));
6355 -- The new compilation unit is linked to its body, but both share the
6356 -- same file, so we do not set Body_Required on the new unit so as not
6357 -- to create a spurious dependency on a non-existent body in the ali.
6358 -- This simplifies CodePeer unit traversal.
6360 -- We use the original instantiation compilation unit as the resulting
6361 -- compilation unit of the instance, since this is the main unit.
6363 Rewrite (N, Act_Body);
6365 -- Propagate the aspect specifications from the package body template to
6366 -- the instantiated version of the package body.
6368 if Has_Aspects (Act_Body) then
6369 Set_Aspect_Specifications
6370 (N, New_Copy_List_Tree (Aspect_Specifications (Act_Body)));
6373 Body_Cunit := Parent (N);
6375 -- The two compilation unit nodes are linked by the Library_Unit field
6377 Set_Library_Unit (Decl_Cunit, Body_Cunit);
6378 Set_Library_Unit (Body_Cunit, Decl_Cunit);
6380 -- Preserve the private nature of the package if needed
6382 Set_Private_Present (Decl_Cunit, Private_Present (Body_Cunit));
6384 -- If the instance is not the main unit, its context, categorization
6385 -- and elaboration entity are not relevant to the compilation.
6387 if Body_Cunit /= Cunit (Main_Unit) then
6388 Make_Instance_Unit (Body_Cunit, In_Main => False);
6392 -- The context clause items on the instantiation, which are now attached
6393 -- to the body compilation unit (since the body overwrote the original
6394 -- instantiation node), semantically belong on the spec, so copy them
6395 -- there. It's harmless to leave them on the body as well. In fact one
6396 -- could argue that they belong in both places.
6398 Citem := First (Context_Items (Body_Cunit));
6399 while Present (Citem) loop
6400 Append (New_Copy (Citem), Context_Items (Decl_Cunit));
6404 -- Propagate categorization flags on packages, so that they appear in
6405 -- the ali file for the spec of the unit.
6407 if Ekind (New_Main) = E_Package then
6408 Set_Is_Pure (Old_Main, Is_Pure (New_Main));
6409 Set_Is_Preelaborated (Old_Main, Is_Preelaborated (New_Main));
6410 Set_Is_Remote_Types (Old_Main, Is_Remote_Types (New_Main));
6411 Set_Is_Shared_Passive (Old_Main, Is_Shared_Passive (New_Main));
6412 Set_Is_Remote_Call_Interface
6413 (Old_Main, Is_Remote_Call_Interface (New_Main));
6416 -- Make entry in Units table, so that binder can generate call to
6417 -- elaboration procedure for body, if any.
6419 Make_Instance_Unit (Body_Cunit, In_Main => True);
6420 Main_Unit_Entity := New_Main;
6421 Set_Cunit_Entity (Main_Unit, Main_Unit_Entity);
6423 -- Build elaboration entity, since the instance may certainly generate
6424 -- elaboration code requiring a flag for protection.
6426 Build_Elaboration_Entity (Decl_Cunit, New_Main);
6427 end Build_Instance_Compilation_Unit_Nodes;
6429 -----------------------------
6430 -- Check_Access_Definition --
6431 -----------------------------
6433 procedure Check_Access_Definition (N : Node_Id) is
6436 (Ada_Version >= Ada_2005 and then Present (Access_Definition (N)));
6438 end Check_Access_Definition;
6440 -----------------------------------
6441 -- Check_Formal_Package_Instance --
6442 -----------------------------------
6444 -- If the formal has specific parameters, they must match those of the
6445 -- actual. Both of them are instances, and the renaming declarations for
6446 -- their formal parameters appear in the same order in both. The analyzed
6447 -- formal has been analyzed in the context of the current instance.
6449 procedure Check_Formal_Package_Instance
6450 (Formal_Pack : Entity_Id;
6451 Actual_Pack : Entity_Id)
6453 E1 : Entity_Id := First_Entity (Actual_Pack);
6454 E2 : Entity_Id := First_Entity (Formal_Pack);
6455 Prev_E1 : Entity_Id;
6460 procedure Check_Mismatch (B : Boolean);
6461 -- Common error routine for mismatch between the parameters of the
6462 -- actual instance and those of the formal package.
6464 function Is_Defaulted (Param : Entity_Id) return Boolean;
6465 -- If the formal package has partly box-initialized formals, skip
6466 -- conformance check for these formals. Previously the code assumed
6467 -- that box initialization for a formal package applied to all its
6468 -- formal parameters.
6470 function Same_Instantiated_Constant (E1, E2 : Entity_Id) return Boolean;
6471 -- The formal may come from a nested formal package, and the actual may
6472 -- have been constant-folded. To determine whether the two denote the
6473 -- same entity we may have to traverse several definitions to recover
6474 -- the ultimate entity that they refer to.
6476 function Same_Instantiated_Function (E1, E2 : Entity_Id) return Boolean;
6477 -- The formal and the actual must be identical, but if both are
6478 -- given by attributes they end up renaming different generated bodies,
6479 -- and we must verify that the attributes themselves match.
6481 function Same_Instantiated_Variable (E1, E2 : Entity_Id) return Boolean;
6482 -- Similarly, if the formal comes from a nested formal package, the
6483 -- actual may designate the formal through multiple renamings, which
6484 -- have to be followed to determine the original variable in question.
6486 --------------------
6487 -- Check_Mismatch --
6488 --------------------
6490 procedure Check_Mismatch (B : Boolean) is
6491 -- A Formal_Type_Declaration for a derived private type is rewritten
6492 -- as a private extension decl. (see Analyze_Formal_Derived_Type),
6493 -- which is why we examine the original node.
6495 Kind : constant Node_Kind := Nkind (Original_Node (Parent (E2)));
6498 if Kind = N_Formal_Type_Declaration then
6501 elsif Nkind_In (Kind, N_Formal_Object_Declaration,
6502 N_Formal_Package_Declaration)
6503 or else Kind in N_Formal_Subprogram_Declaration
6507 -- Ada 2012: If both formal and actual are incomplete types they
6510 elsif Is_Incomplete_Type (E1) and then Is_Incomplete_Type (E2) then
6515 ("actual for & in actual instance does not match formal",
6516 Parent (Actual_Pack), E1);
6524 function Is_Defaulted (Param : Entity_Id) return Boolean is
6529 First (Generic_Associations (Parent
6530 (Associated_Formal_Package (Actual_Pack))));
6532 while Present (Assoc) loop
6533 if Nkind (Assoc) = N_Others_Choice then
6536 elsif Nkind (Assoc) = N_Generic_Association
6537 and then Chars (Selector_Name (Assoc)) = Chars (Param)
6539 return Box_Present (Assoc);
6548 --------------------------------
6549 -- Same_Instantiated_Constant --
6550 --------------------------------
6552 function Same_Instantiated_Constant
6553 (E1, E2 : Entity_Id) return Boolean
6559 while Present (Ent) loop
6563 elsif Ekind (Ent) /= E_Constant then
6566 elsif Is_Entity_Name (Constant_Value (Ent)) then
6567 if Entity (Constant_Value (Ent)) = E1 then
6570 Ent := Entity (Constant_Value (Ent));
6573 -- The actual may be a constant that has been folded. Recover
6576 elsif Is_Entity_Name (Original_Node (Constant_Value (Ent))) then
6577 Ent := Entity (Original_Node (Constant_Value (Ent)));
6585 end Same_Instantiated_Constant;
6587 --------------------------------
6588 -- Same_Instantiated_Function --
6589 --------------------------------
6591 function Same_Instantiated_Function
6592 (E1, E2 : Entity_Id) return Boolean
6596 if Alias (E1) = Alias (E2) then
6599 elsif Present (Alias (E2)) then
6600 U1 := Original_Node (Unit_Declaration_Node (E1));
6601 U2 := Original_Node (Unit_Declaration_Node (Alias (E2)));
6603 return Nkind (U1) = N_Subprogram_Renaming_Declaration
6604 and then Nkind (Name (U1)) = N_Attribute_Reference
6606 and then Nkind (U2) = N_Subprogram_Renaming_Declaration
6607 and then Nkind (Name (U2)) = N_Attribute_Reference
6610 Attribute_Name (Name (U1)) = Attribute_Name (Name (U2));
6614 end Same_Instantiated_Function;
6616 --------------------------------
6617 -- Same_Instantiated_Variable --
6618 --------------------------------
6620 function Same_Instantiated_Variable
6621 (E1, E2 : Entity_Id) return Boolean
6623 function Original_Entity (E : Entity_Id) return Entity_Id;
6624 -- Follow chain of renamings to the ultimate ancestor
6626 ---------------------
6627 -- Original_Entity --
6628 ---------------------
6630 function Original_Entity (E : Entity_Id) return Entity_Id is
6635 while Nkind (Parent (Orig)) = N_Object_Renaming_Declaration
6636 and then Present (Renamed_Object (Orig))
6637 and then Is_Entity_Name (Renamed_Object (Orig))
6639 Orig := Entity (Renamed_Object (Orig));
6643 end Original_Entity;
6645 -- Start of processing for Same_Instantiated_Variable
6648 return Ekind (E1) = Ekind (E2)
6649 and then Original_Entity (E1) = Original_Entity (E2);
6650 end Same_Instantiated_Variable;
6652 -- Start of processing for Check_Formal_Package_Instance
6656 while Present (E1) and then Present (E2) loop
6657 exit when Ekind (E1) = E_Package
6658 and then Renamed_Entity (E1) = Renamed_Entity (Actual_Pack);
6660 -- If the formal is the renaming of the formal package, this
6661 -- is the end of its formal part, which may occur before the
6662 -- end of the formal part in the actual in the presence of
6663 -- defaulted parameters in the formal package.
6665 exit when Nkind (Parent (E2)) = N_Package_Renaming_Declaration
6666 and then Renamed_Entity (E2) = Scope (E2);
6668 -- The analysis of the actual may generate additional internal
6669 -- entities. If the formal is defaulted, there is no corresponding
6670 -- analysis and the internal entities must be skipped, until we
6671 -- find corresponding entities again.
6673 if Comes_From_Source (E2)
6674 and then not Comes_From_Source (E1)
6675 and then Chars (E1) /= Chars (E2)
6677 while Present (E1) and then Chars (E1) /= Chars (E2) loop
6685 -- Entities may be declared without full declaration, such as
6686 -- itypes and predefined operators (concatenation for arrays, eg).
6687 -- Skip it and keep the formal entity to find a later match for it.
6689 elsif No (Parent (E2)) and then Ekind (E1) /= Ekind (E2) then
6693 -- If the formal entity comes from a formal declaration, it was
6694 -- defaulted in the formal package, and no check is needed on it.
6696 elsif Nkind_In (Original_Node (Parent (E2)),
6697 N_Formal_Object_Declaration,
6698 N_Formal_Type_Declaration)
6700 -- If the formal is a tagged type the corresponding class-wide
6701 -- type has been generated as well, and it must be skipped.
6703 if Is_Type (E2) and then Is_Tagged_Type (E2) then
6709 -- Ditto for defaulted formal subprograms.
6711 elsif Is_Overloadable (E1)
6712 and then Nkind (Unit_Declaration_Node (E2)) in
6713 N_Formal_Subprogram_Declaration
6717 elsif Is_Defaulted (E1) then
6720 elsif Is_Type (E1) then
6722 -- Subtypes must statically match. E1, E2 are the local entities
6723 -- that are subtypes of the actuals. Itypes generated for other
6724 -- parameters need not be checked, the check will be performed
6725 -- on the parameters themselves.
6727 -- If E2 is a formal type declaration, it is a defaulted parameter
6728 -- and needs no checking.
6730 if not Is_Itype (E1) and then not Is_Itype (E2) then
6733 or else Etype (E1) /= Etype (E2)
6734 or else not Subtypes_Statically_Match (E1, E2));
6737 elsif Ekind (E1) = E_Constant then
6739 -- IN parameters must denote the same static value, or the same
6740 -- constant, or the literal null.
6742 Expr1 := Expression (Parent (E1));
6744 if Ekind (E2) /= E_Constant then
6745 Check_Mismatch (True);
6748 Expr2 := Expression (Parent (E2));
6751 if Is_OK_Static_Expression (Expr1) then
6752 if not Is_OK_Static_Expression (Expr2) then
6753 Check_Mismatch (True);
6755 elsif Is_Discrete_Type (Etype (E1)) then
6757 V1 : constant Uint := Expr_Value (Expr1);
6758 V2 : constant Uint := Expr_Value (Expr2);
6760 Check_Mismatch (V1 /= V2);
6763 elsif Is_Real_Type (Etype (E1)) then
6765 V1 : constant Ureal := Expr_Value_R (Expr1);
6766 V2 : constant Ureal := Expr_Value_R (Expr2);
6768 Check_Mismatch (V1 /= V2);
6771 elsif Is_String_Type (Etype (E1))
6772 and then Nkind (Expr1) = N_String_Literal
6774 if Nkind (Expr2) /= N_String_Literal then
6775 Check_Mismatch (True);
6778 (not String_Equal (Strval (Expr1), Strval (Expr2)));
6782 elsif Is_Entity_Name (Expr1) then
6783 if Is_Entity_Name (Expr2) then
6784 if Entity (Expr1) = Entity (Expr2) then
6788 (not Same_Instantiated_Constant
6789 (Entity (Expr1), Entity (Expr2)));
6793 Check_Mismatch (True);
6796 elsif Is_Entity_Name (Original_Node (Expr1))
6797 and then Is_Entity_Name (Expr2)
6798 and then Same_Instantiated_Constant
6799 (Entity (Original_Node (Expr1)), Entity (Expr2))
6803 elsif Nkind (Expr1) = N_Null then
6804 Check_Mismatch (Nkind (Expr1) /= N_Null);
6807 Check_Mismatch (True);
6810 elsif Ekind (E1) = E_Variable then
6811 Check_Mismatch (not Same_Instantiated_Variable (E1, E2));
6813 elsif Ekind (E1) = E_Package then
6815 (Ekind (E1) /= Ekind (E2)
6816 or else (Present (Renamed_Object (E2))
6817 and then Renamed_Object (E1) /=
6818 Renamed_Object (E2)));
6820 elsif Is_Overloadable (E1) then
6821 -- Verify that the actual subprograms match. Note that actuals
6822 -- that are attributes are rewritten as subprograms. If the
6823 -- subprogram in the formal package is defaulted, no check is
6824 -- needed. Note that this can only happen in Ada 2005 when the
6825 -- formal package can be partially parameterized.
6827 if Nkind (Unit_Declaration_Node (E1)) =
6828 N_Subprogram_Renaming_Declaration
6829 and then From_Default (Unit_Declaration_Node (E1))
6833 -- If the formal package has an "others" box association that
6834 -- covers this formal, there is no need for a check either.
6836 elsif Nkind (Unit_Declaration_Node (E2)) in
6837 N_Formal_Subprogram_Declaration
6838 and then Box_Present (Unit_Declaration_Node (E2))
6842 -- No check needed if subprogram is a defaulted null procedure
6844 elsif No (Alias (E2))
6845 and then Ekind (E2) = E_Procedure
6847 Null_Present (Specification (Unit_Declaration_Node (E2)))
6851 -- Otherwise the actual in the formal and the actual in the
6852 -- instantiation of the formal must match, up to renamings.
6856 (Ekind (E2) /= Ekind (E1)
6857 or else not Same_Instantiated_Function (E1, E2));
6861 raise Program_Error;
6869 end Check_Formal_Package_Instance;
6871 ---------------------------
6872 -- Check_Formal_Packages --
6873 ---------------------------
6875 procedure Check_Formal_Packages (P_Id : Entity_Id) is
6877 Formal_P : Entity_Id;
6878 Formal_Decl : Node_Id;
6880 -- Iterate through the declarations in the instance, looking for package
6881 -- renaming declarations that denote instances of formal packages. Stop
6882 -- when we find the renaming of the current package itself. The
6883 -- declaration for a formal package without a box is followed by an
6884 -- internal entity that repeats the instantiation.
6886 E := First_Entity (P_Id);
6887 while Present (E) loop
6888 if Ekind (E) = E_Package then
6889 if Renamed_Object (E) = P_Id then
6892 elsif Nkind (Parent (E)) /= N_Package_Renaming_Declaration then
6896 Formal_Decl := Parent (Associated_Formal_Package (E));
6898 -- Nothing to check if the formal has a box or an others_clause
6899 -- (necessarily with a box), or no associations altogether
6901 if Box_Present (Formal_Decl)
6902 or else No (Generic_Associations (Formal_Decl))
6906 elsif Nkind (First (Generic_Associations (Formal_Decl))) =
6909 -- The internal validating package was generated but formal
6910 -- and instance are known to be compatible.
6912 Formal_P := Next_Entity (E);
6913 Remove (Unit_Declaration_Node (Formal_P));
6916 Formal_P := Next_Entity (E);
6918 -- If the instance is within an enclosing instance body
6919 -- there is no need to verify the legality of current formal
6920 -- packages because they were legal in the generic body.
6921 -- This optimization may be applicable elsewhere, and it
6922 -- also removes spurious errors that may arise with
6923 -- on-the-fly inlining and confusion between private and
6926 if not In_Instance_Body then
6927 Check_Formal_Package_Instance (Formal_P, E);
6930 -- Restore the visibility of formals of the formal instance
6931 -- that are not defaulted, and are hidden within the current
6932 -- generic. These formals may be visible within an enclosing
6938 Elmt := First_Elmt (Hidden_In_Formal_Instance (Formal_P));
6939 while Present (Elmt) loop
6940 Set_Is_Hidden (Node (Elmt), False);
6945 -- After checking, remove the internal validating package.
6946 -- It is only needed for semantic checks, and as it may
6947 -- contain generic formal declarations it should not reach
6950 Remove (Unit_Declaration_Node (Formal_P));
6957 end Check_Formal_Packages;
6959 ---------------------------------
6960 -- Check_Forward_Instantiation --
6961 ---------------------------------
6963 procedure Check_Forward_Instantiation (Decl : Node_Id) is
6965 Gen_Comp : Entity_Id := Cunit_Entity (Get_Source_Unit (Decl));
6968 -- The instantiation appears before the generic body if we are in the
6969 -- scope of the unit containing the generic, either in its spec or in
6970 -- the package body, and before the generic body.
6972 if Ekind (Gen_Comp) = E_Package_Body then
6973 Gen_Comp := Spec_Entity (Gen_Comp);
6976 if In_Open_Scopes (Gen_Comp)
6977 and then No (Corresponding_Body (Decl))
6982 and then not Is_Compilation_Unit (S)
6983 and then not Is_Child_Unit (S)
6985 if Ekind (S) = E_Package then
6986 Set_Has_Forward_Instantiation (S);
6992 end Check_Forward_Instantiation;
6994 ---------------------------
6995 -- Check_Generic_Actuals --
6996 ---------------------------
6998 -- The visibility of the actuals may be different between the point of
6999 -- generic instantiation and the instantiation of the body.
7001 procedure Check_Generic_Actuals
7002 (Instance : Entity_Id;
7003 Is_Formal_Box : Boolean)
7009 E := First_Entity (Instance);
7010 while Present (E) loop
7012 and then Nkind (Parent (E)) = N_Subtype_Declaration
7013 and then Scope (Etype (E)) /= Instance
7014 and then Is_Entity_Name (Subtype_Indication (Parent (E)))
7016 -- Restore the proper view of the actual from the information
7017 -- saved earlier by Instantiate_Type.
7019 Check_Private_View (Subtype_Indication (Parent (E)));
7021 -- If the actual is itself the formal of a parent instance,
7022 -- then also restore the proper view of its actual and so on.
7023 -- That's necessary for nested instantiations of the form
7026 -- type Component is private;
7027 -- type Array_Type is array (Positive range <>) of Component;
7030 -- when the outermost actuals have inconsistent views, because
7031 -- the Component_Type of Array_Type of the inner instantiations
7032 -- is the actual of Component of the outermost one and not that
7033 -- of the corresponding inner instantiations.
7035 Astype := Ancestor_Subtype (E);
7036 while Present (Astype)
7037 and then Nkind (Parent (Astype)) = N_Subtype_Declaration
7038 and then Present (Generic_Parent_Type (Parent (Astype)))
7039 and then Is_Entity_Name (Subtype_Indication (Parent (Astype)))
7041 Check_Private_View (Subtype_Indication (Parent (Astype)));
7042 Astype := Ancestor_Subtype (Astype);
7045 Set_Is_Generic_Actual_Type (E);
7047 if Is_Private_Type (E) and then Present (Full_View (E)) then
7048 Set_Is_Generic_Actual_Type (Full_View (E));
7051 Set_Is_Hidden (E, False);
7052 Set_Is_Potentially_Use_Visible (E, In_Use (Instance));
7054 -- We constructed the generic actual type as a subtype of the
7055 -- supplied type. This means that it normally would not inherit
7056 -- subtype specific attributes of the actual, which is wrong for
7057 -- the generic case.
7059 Astype := Ancestor_Subtype (E);
7063 -- This can happen when E is an itype that is the full view of
7064 -- a private type completed, e.g. with a constrained array. In
7065 -- that case, use the first subtype, which will carry size
7066 -- information. The base type itself is unconstrained and will
7069 Astype := First_Subtype (E);
7072 Set_Size_Info (E, (Astype));
7073 Set_RM_Size (E, RM_Size (Astype));
7074 Set_First_Rep_Item (E, First_Rep_Item (Astype));
7076 if Is_Discrete_Or_Fixed_Point_Type (E) then
7077 Set_RM_Size (E, RM_Size (Astype));
7080 elsif Ekind (E) = E_Package then
7082 -- If this is the renaming for the current instance, we're done.
7083 -- Otherwise it is a formal package. If the corresponding formal
7084 -- was declared with a box, the (instantiations of the) generic
7085 -- formal part are also visible. Otherwise, ignore the entity
7086 -- created to validate the actuals.
7088 if Renamed_Object (E) = Instance then
7091 elsif Nkind (Parent (E)) /= N_Package_Renaming_Declaration then
7094 -- The visibility of a formal of an enclosing generic is already
7097 elsif Denotes_Formal_Package (E) then
7100 elsif Present (Associated_Formal_Package (E))
7101 and then not Is_Generic_Formal (E)
7103 if Box_Present (Parent (Associated_Formal_Package (E))) then
7104 Check_Generic_Actuals (Renamed_Object (E), True);
7107 Check_Generic_Actuals (Renamed_Object (E), False);
7110 Set_Is_Hidden (E, False);
7113 -- If this is a subprogram instance (in a wrapper package) the
7114 -- actual is fully visible.
7116 elsif Is_Wrapper_Package (Instance) then
7117 Set_Is_Hidden (E, False);
7119 -- If the formal package is declared with a box, or if the formal
7120 -- parameter is defaulted, it is visible in the body.
7122 elsif Is_Formal_Box or else Is_Visible_Formal (E) then
7123 Set_Is_Hidden (E, False);
7126 if Ekind (E) = E_Constant then
7128 -- If the type of the actual is a private type declared in the
7129 -- enclosing scope of the generic unit, the body of the generic
7130 -- sees the full view of the type (because it has to appear in
7131 -- the corresponding package body). If the type is private now,
7132 -- exchange views to restore the proper visiblity in the instance.
7135 Typ : constant Entity_Id := Base_Type (Etype (E));
7136 -- The type of the actual
7141 Parent_Scope : Entity_Id;
7142 -- The enclosing scope of the generic unit
7145 if Is_Wrapper_Package (Instance) then
7149 (Unit_Declaration_Node
7150 (Related_Instance (Instance))));
7153 Generic_Parent (Package_Specification (Instance));
7156 Parent_Scope := Scope (Gen_Id);
7158 -- The exchange is only needed if the generic is defined
7159 -- within a package which is not a common ancestor of the
7160 -- scope of the instance, and is not already in scope.
7162 if Is_Private_Type (Typ)
7163 and then Scope (Typ) = Parent_Scope
7164 and then Scope (Instance) /= Parent_Scope
7165 and then Ekind (Parent_Scope) = E_Package
7166 and then not Is_Child_Unit (Gen_Id)
7170 -- If the type of the entity is a subtype, it may also have
7171 -- to be made visible, together with the base type of its
7172 -- full view, after exchange.
7174 if Is_Private_Type (Etype (E)) then
7175 Switch_View (Etype (E));
7176 Switch_View (Base_Type (Etype (E)));
7184 end Check_Generic_Actuals;
7186 ------------------------------
7187 -- Check_Generic_Child_Unit --
7188 ------------------------------
7190 procedure Check_Generic_Child_Unit
7192 Parent_Installed : in out Boolean)
7194 Loc : constant Source_Ptr := Sloc (Gen_Id);
7195 Gen_Par : Entity_Id := Empty;
7197 Inst_Par : Entity_Id;
7200 function Find_Generic_Child
7202 Id : Node_Id) return Entity_Id;
7203 -- Search generic parent for possible child unit with the given name
7205 function In_Enclosing_Instance return Boolean;
7206 -- Within an instance of the parent, the child unit may be denoted by
7207 -- a simple name, or an abbreviated expanded name. Examine enclosing
7208 -- scopes to locate a possible parent instantiation.
7210 ------------------------
7211 -- Find_Generic_Child --
7212 ------------------------
7214 function Find_Generic_Child
7216 Id : Node_Id) return Entity_Id
7221 -- If entity of name is already set, instance has already been
7222 -- resolved, e.g. in an enclosing instantiation.
7224 if Present (Entity (Id)) then
7225 if Scope (Entity (Id)) = Scop then
7232 E := First_Entity (Scop);
7233 while Present (E) loop
7234 if Chars (E) = Chars (Id)
7235 and then Is_Child_Unit (E)
7237 if Is_Child_Unit (E)
7238 and then not Is_Visible_Lib_Unit (E)
7241 ("generic child unit& is not visible", Gen_Id, E);
7253 end Find_Generic_Child;
7255 ---------------------------
7256 -- In_Enclosing_Instance --
7257 ---------------------------
7259 function In_Enclosing_Instance return Boolean is
7260 Enclosing_Instance : Node_Id;
7261 Instance_Decl : Node_Id;
7264 -- We do not inline any call that contains instantiations, except
7265 -- for instantiations of Unchecked_Conversion, so if we are within
7266 -- an inlined body the current instance does not require parents.
7268 if In_Inlined_Body then
7269 pragma Assert (Chars (Gen_Id) = Name_Unchecked_Conversion);
7273 -- Loop to check enclosing scopes
7275 Enclosing_Instance := Current_Scope;
7276 while Present (Enclosing_Instance) loop
7277 Instance_Decl := Unit_Declaration_Node (Enclosing_Instance);
7279 if Ekind (Enclosing_Instance) = E_Package
7280 and then Is_Generic_Instance (Enclosing_Instance)
7282 (Generic_Parent (Specification (Instance_Decl)))
7284 -- Check whether the generic we are looking for is a child of
7287 E := Find_Generic_Child
7288 (Generic_Parent (Specification (Instance_Decl)), Gen_Id);
7289 exit when Present (E);
7295 Enclosing_Instance := Scope (Enclosing_Instance);
7307 Make_Expanded_Name (Loc,
7309 Prefix => New_Occurrence_Of (Enclosing_Instance, Loc),
7310 Selector_Name => New_Occurrence_Of (E, Loc)));
7312 Set_Entity (Gen_Id, E);
7313 Set_Etype (Gen_Id, Etype (E));
7314 Parent_Installed := False; -- Already in scope.
7317 end In_Enclosing_Instance;
7319 -- Start of processing for Check_Generic_Child_Unit
7322 -- If the name of the generic is given by a selected component, it may
7323 -- be the name of a generic child unit, and the prefix is the name of an
7324 -- instance of the parent, in which case the child unit must be visible.
7325 -- If this instance is not in scope, it must be placed there and removed
7326 -- after instantiation, because what is being instantiated is not the
7327 -- original child, but the corresponding child present in the instance
7330 -- If the child is instantiated within the parent, it can be given by
7331 -- a simple name. In this case the instance is already in scope, but
7332 -- the child generic must be recovered from the generic parent as well.
7334 if Nkind (Gen_Id) = N_Selected_Component then
7335 S := Selector_Name (Gen_Id);
7336 Analyze (Prefix (Gen_Id));
7337 Inst_Par := Entity (Prefix (Gen_Id));
7339 if Ekind (Inst_Par) = E_Package
7340 and then Present (Renamed_Object (Inst_Par))
7342 Inst_Par := Renamed_Object (Inst_Par);
7345 if Ekind (Inst_Par) = E_Package then
7346 if Nkind (Parent (Inst_Par)) = N_Package_Specification then
7347 Gen_Par := Generic_Parent (Parent (Inst_Par));
7349 elsif Nkind (Parent (Inst_Par)) = N_Defining_Program_Unit_Name
7351 Nkind (Parent (Parent (Inst_Par))) = N_Package_Specification
7353 Gen_Par := Generic_Parent (Parent (Parent (Inst_Par)));
7356 elsif Ekind (Inst_Par) = E_Generic_Package
7357 and then Nkind (Parent (Gen_Id)) = N_Formal_Package_Declaration
7359 -- A formal package may be a real child package, and not the
7360 -- implicit instance within a parent. In this case the child is
7361 -- not visible and has to be retrieved explicitly as well.
7363 Gen_Par := Inst_Par;
7366 if Present (Gen_Par) then
7368 -- The prefix denotes an instantiation. The entity itself may be a
7369 -- nested generic, or a child unit.
7371 E := Find_Generic_Child (Gen_Par, S);
7374 Change_Selected_Component_To_Expanded_Name (Gen_Id);
7375 Set_Entity (Gen_Id, E);
7376 Set_Etype (Gen_Id, Etype (E));
7378 Set_Etype (S, Etype (E));
7380 -- Indicate that this is a reference to the parent
7382 if In_Extended_Main_Source_Unit (Gen_Id) then
7383 Set_Is_Instantiated (Inst_Par);
7386 -- A common mistake is to replicate the naming scheme of a
7387 -- hierarchy by instantiating a generic child directly, rather
7388 -- than the implicit child in a parent instance:
7390 -- generic .. package Gpar is ..
7391 -- generic .. package Gpar.Child is ..
7392 -- package Par is new Gpar ();
7395 -- package Par.Child is new Gpar.Child ();
7396 -- rather than Par.Child
7398 -- In this case the instantiation is within Par, which is an
7399 -- instance, but Gpar does not denote Par because we are not IN
7400 -- the instance of Gpar, so this is illegal. The test below
7401 -- recognizes this particular case.
7403 if Is_Child_Unit (E)
7404 and then not Comes_From_Source (Entity (Prefix (Gen_Id)))
7405 and then (not In_Instance
7406 or else Nkind (Parent (Parent (Gen_Id))) =
7410 ("prefix of generic child unit must be instance of parent",
7414 if not In_Open_Scopes (Inst_Par)
7415 and then Nkind (Parent (Gen_Id)) not in
7416 N_Generic_Renaming_Declaration
7418 Install_Parent (Inst_Par);
7419 Parent_Installed := True;
7421 elsif In_Open_Scopes (Inst_Par) then
7423 -- If the parent is already installed, install the actuals
7424 -- for its formal packages. This is necessary when the child
7425 -- instance is a child of the parent instance: in this case,
7426 -- the parent is placed on the scope stack but the formal
7427 -- packages are not made visible.
7429 Install_Formal_Packages (Inst_Par);
7433 -- If the generic parent does not contain an entity that
7434 -- corresponds to the selector, the instance doesn't either.
7435 -- Analyzing the node will yield the appropriate error message.
7436 -- If the entity is not a child unit, then it is an inner
7437 -- generic in the parent.
7445 if Is_Child_Unit (Entity (Gen_Id))
7447 Nkind (Parent (Gen_Id)) not in N_Generic_Renaming_Declaration
7448 and then not In_Open_Scopes (Inst_Par)
7450 Install_Parent (Inst_Par);
7451 Parent_Installed := True;
7453 -- The generic unit may be the renaming of the implicit child
7454 -- present in an instance. In that case the parent instance is
7455 -- obtained from the name of the renamed entity.
7457 elsif Ekind (Entity (Gen_Id)) = E_Generic_Package
7458 and then Present (Renamed_Entity (Entity (Gen_Id)))
7459 and then Is_Child_Unit (Renamed_Entity (Entity (Gen_Id)))
7462 Renamed_Package : constant Node_Id :=
7463 Name (Parent (Entity (Gen_Id)));
7465 if Nkind (Renamed_Package) = N_Expanded_Name then
7466 Inst_Par := Entity (Prefix (Renamed_Package));
7467 Install_Parent (Inst_Par);
7468 Parent_Installed := True;
7474 elsif Nkind (Gen_Id) = N_Expanded_Name then
7476 -- Entity already present, analyze prefix, whose meaning may be an
7477 -- instance in the current context. If it is an instance of a
7478 -- relative within another, the proper parent may still have to be
7479 -- installed, if they are not of the same generation.
7481 Analyze (Prefix (Gen_Id));
7483 -- Prevent cascaded errors
7485 if Etype (Prefix (Gen_Id)) = Any_Type then
7489 -- In the unlikely case that a local declaration hides the name of
7490 -- the parent package, locate it on the homonym chain. If the context
7491 -- is an instance of the parent, the renaming entity is flagged as
7494 Inst_Par := Entity (Prefix (Gen_Id));
7495 while Present (Inst_Par)
7496 and then not Is_Package_Or_Generic_Package (Inst_Par)
7498 Inst_Par := Homonym (Inst_Par);
7501 pragma Assert (Present (Inst_Par));
7502 Set_Entity (Prefix (Gen_Id), Inst_Par);
7504 if In_Enclosing_Instance then
7507 elsif Present (Entity (Gen_Id))
7508 and then Is_Child_Unit (Entity (Gen_Id))
7509 and then not In_Open_Scopes (Inst_Par)
7511 Install_Parent (Inst_Par);
7512 Parent_Installed := True;
7515 elsif In_Enclosing_Instance then
7517 -- The child unit is found in some enclosing scope
7524 -- If this is the renaming of the implicit child in a parent
7525 -- instance, recover the parent name and install it.
7527 if Is_Entity_Name (Gen_Id) then
7528 E := Entity (Gen_Id);
7530 if Is_Generic_Unit (E)
7531 and then Nkind (Parent (E)) in N_Generic_Renaming_Declaration
7532 and then Is_Child_Unit (Renamed_Object (E))
7533 and then Is_Generic_Unit (Scope (Renamed_Object (E)))
7534 and then Nkind (Name (Parent (E))) = N_Expanded_Name
7536 Rewrite (Gen_Id, New_Copy_Tree (Name (Parent (E))));
7537 Inst_Par := Entity (Prefix (Gen_Id));
7539 if not In_Open_Scopes (Inst_Par) then
7540 Install_Parent (Inst_Par);
7541 Parent_Installed := True;
7544 -- If it is a child unit of a non-generic parent, it may be
7545 -- use-visible and given by a direct name. Install parent as
7548 elsif Is_Generic_Unit (E)
7549 and then Is_Child_Unit (E)
7551 Nkind (Parent (Gen_Id)) not in N_Generic_Renaming_Declaration
7552 and then not Is_Generic_Unit (Scope (E))
7554 if not In_Open_Scopes (Scope (E)) then
7555 Install_Parent (Scope (E));
7556 Parent_Installed := True;
7561 end Check_Generic_Child_Unit;
7563 -----------------------------
7564 -- Check_Hidden_Child_Unit --
7565 -----------------------------
7567 procedure Check_Hidden_Child_Unit
7569 Gen_Unit : Entity_Id;
7570 Act_Decl_Id : Entity_Id)
7572 Gen_Id : constant Node_Id := Name (N);
7575 if Is_Child_Unit (Gen_Unit)
7576 and then Is_Child_Unit (Act_Decl_Id)
7577 and then Nkind (Gen_Id) = N_Expanded_Name
7578 and then Entity (Prefix (Gen_Id)) = Scope (Act_Decl_Id)
7579 and then Chars (Gen_Unit) = Chars (Act_Decl_Id)
7581 Error_Msg_Node_2 := Scope (Act_Decl_Id);
7583 ("generic unit & is implicitly declared in &",
7584 Defining_Unit_Name (N), Gen_Unit);
7585 Error_Msg_N ("\instance must have different name",
7586 Defining_Unit_Name (N));
7588 end Check_Hidden_Child_Unit;
7590 ------------------------
7591 -- Check_Private_View --
7592 ------------------------
7594 procedure Check_Private_View (N : Node_Id) is
7595 T : constant Entity_Id := Etype (N);
7599 -- Exchange views if the type was not private in the generic but is
7600 -- private at the point of instantiation. Do not exchange views if
7601 -- the scope of the type is in scope. This can happen if both generic
7602 -- and instance are sibling units, or if type is defined in a parent.
7603 -- In this case the visibility of the type will be correct for all
7607 BT := Base_Type (T);
7609 if Is_Private_Type (T)
7610 and then not Has_Private_View (N)
7611 and then Present (Full_View (T))
7612 and then not In_Open_Scopes (Scope (T))
7614 -- In the generic, the full declaration was visible
7618 elsif Has_Private_View (N)
7619 and then not Is_Private_Type (T)
7620 and then not Has_Been_Exchanged (T)
7621 and then (not In_Open_Scopes (Scope (T))
7622 or else Nkind (Parent (N)) = N_Subtype_Declaration)
7624 -- In the generic, only the private declaration was visible
7626 -- If the type appears in a subtype declaration, the subtype in
7627 -- instance must have a view compatible with that of its parent,
7628 -- which must be exchanged (see corresponding code in Restore_
7629 -- Private_Views) so we make an exception to the open scope rule.
7631 Prepend_Elmt (T, Exchanged_Views);
7632 Exchange_Declarations (Etype (Get_Associated_Node (N)));
7634 -- Finally, a non-private subtype may have a private base type, which
7635 -- must be exchanged for consistency. This can happen when a package
7636 -- body is instantiated, when the scope stack is empty but in fact
7637 -- the subtype and the base type are declared in an enclosing scope.
7639 -- Note that in this case we introduce an inconsistency in the view
7640 -- set, because we switch the base type BT, but there could be some
7641 -- private dependent subtypes of BT which remain unswitched. Such
7642 -- subtypes might need to be switched at a later point (see specific
7643 -- provision for that case in Switch_View).
7645 elsif not Is_Private_Type (T)
7646 and then not Has_Private_View (N)
7647 and then Is_Private_Type (BT)
7648 and then Present (Full_View (BT))
7649 and then not Is_Generic_Type (BT)
7650 and then not In_Open_Scopes (BT)
7652 Prepend_Elmt (Full_View (BT), Exchanged_Views);
7653 Exchange_Declarations (BT);
7656 end Check_Private_View;
7658 -----------------------------
7659 -- Check_Hidden_Primitives --
7660 -----------------------------
7662 function Check_Hidden_Primitives (Assoc_List : List_Id) return Elist_Id is
7665 Result : Elist_Id := No_Elist;
7668 if No (Assoc_List) then
7672 -- Traverse the list of associations between formals and actuals
7673 -- searching for renamings of tagged types
7675 Actual := First (Assoc_List);
7676 while Present (Actual) loop
7677 if Nkind (Actual) = N_Subtype_Declaration then
7678 Gen_T := Generic_Parent_Type (Actual);
7680 if Present (Gen_T) and then Is_Tagged_Type (Gen_T) then
7682 -- Traverse the list of primitives of the actual types
7683 -- searching for hidden primitives that are visible in the
7684 -- corresponding generic formal; leave them visible and
7685 -- append them to Result to restore their decoration later.
7687 Install_Hidden_Primitives
7688 (Prims_List => Result,
7690 Act_T => Entity (Subtype_Indication (Actual)));
7698 end Check_Hidden_Primitives;
7700 --------------------------
7701 -- Contains_Instance_Of --
7702 --------------------------
7704 function Contains_Instance_Of
7707 N : Node_Id) return Boolean
7715 -- Verify that there are no circular instantiations. We check whether
7716 -- the unit contains an instance of the current scope or some enclosing
7717 -- scope (in case one of the instances appears in a subunit). Longer
7718 -- circularities involving subunits might seem too pathological to
7719 -- consider, but they were not too pathological for the authors of
7720 -- DEC bc30vsq, so we loop over all enclosing scopes, and mark all
7721 -- enclosing generic scopes as containing an instance.
7724 -- Within a generic subprogram body, the scope is not generic, to
7725 -- allow for recursive subprograms. Use the declaration to determine
7726 -- whether this is a generic unit.
7728 if Ekind (Scop) = E_Generic_Package
7729 or else (Is_Subprogram (Scop)
7730 and then Nkind (Unit_Declaration_Node (Scop)) =
7731 N_Generic_Subprogram_Declaration)
7733 Elmt := First_Elmt (Inner_Instances (Inner));
7735 while Present (Elmt) loop
7736 if Node (Elmt) = Scop then
7737 Error_Msg_Node_2 := Inner;
7739 ("circular Instantiation: & instantiated within &!",
7743 elsif Node (Elmt) = Inner then
7746 elsif Contains_Instance_Of (Node (Elmt), Scop, N) then
7747 Error_Msg_Node_2 := Inner;
7749 ("circular Instantiation: & instantiated within &!",
7757 -- Indicate that Inner is being instantiated within Scop
7759 Append_Elmt (Inner, Inner_Instances (Scop));
7762 if Scop = Standard_Standard then
7765 Scop := Scope (Scop);
7770 end Contains_Instance_Of;
7772 -----------------------
7773 -- Copy_Generic_Node --
7774 -----------------------
7776 function Copy_Generic_Node
7778 Parent_Id : Node_Id;
7779 Instantiating : Boolean) return Node_Id
7784 function Copy_Generic_Descendant (D : Union_Id) return Union_Id;
7785 -- Check the given value of one of the Fields referenced by the current
7786 -- node to determine whether to copy it recursively. The field may hold
7787 -- a Node_Id, a List_Id, or an Elist_Id, or a plain value (Sloc, Uint,
7788 -- Char) in which case it need not be copied.
7790 procedure Copy_Descendants;
7791 -- Common utility for various nodes
7793 function Copy_Generic_Elist (E : Elist_Id) return Elist_Id;
7794 -- Make copy of element list
7796 function Copy_Generic_List
7798 Parent_Id : Node_Id) return List_Id;
7799 -- Apply Copy_Node recursively to the members of a node list
7801 function In_Defining_Unit_Name (Nam : Node_Id) return Boolean;
7802 -- True if an identifier is part of the defining program unit name of
7804 -- Consider removing this subprogram now that ASIS no longer uses it.
7806 ----------------------
7807 -- Copy_Descendants --
7808 ----------------------
7810 procedure Copy_Descendants is
7811 use Atree.Unchecked_Access;
7812 -- This code section is part of the implementation of an untyped
7813 -- tree traversal, so it needs direct access to node fields.
7816 Set_Field1 (New_N, Copy_Generic_Descendant (Field1 (N)));
7817 Set_Field2 (New_N, Copy_Generic_Descendant (Field2 (N)));
7818 Set_Field3 (New_N, Copy_Generic_Descendant (Field3 (N)));
7819 Set_Field4 (New_N, Copy_Generic_Descendant (Field4 (N)));
7820 Set_Field5 (New_N, Copy_Generic_Descendant (Field5 (N)));
7821 end Copy_Descendants;
7823 -----------------------------
7824 -- Copy_Generic_Descendant --
7825 -----------------------------
7827 function Copy_Generic_Descendant (D : Union_Id) return Union_Id is
7829 if D = Union_Id (Empty) then
7832 elsif D in Node_Range then
7834 (Copy_Generic_Node (Node_Id (D), New_N, Instantiating));
7836 elsif D in List_Range then
7837 return Union_Id (Copy_Generic_List (List_Id (D), New_N));
7839 elsif D in Elist_Range then
7840 return Union_Id (Copy_Generic_Elist (Elist_Id (D)));
7842 -- Nothing else is copyable (e.g. Uint values), return as is
7847 end Copy_Generic_Descendant;
7849 ------------------------
7850 -- Copy_Generic_Elist --
7851 ------------------------
7853 function Copy_Generic_Elist (E : Elist_Id) return Elist_Id is
7860 M := First_Elmt (E);
7861 while Present (M) loop
7863 (Copy_Generic_Node (Node (M), Empty, Instantiating), L);
7872 end Copy_Generic_Elist;
7874 -----------------------
7875 -- Copy_Generic_List --
7876 -----------------------
7878 function Copy_Generic_List
7880 Parent_Id : Node_Id) return List_Id
7888 Set_Parent (New_L, Parent_Id);
7891 while Present (N) loop
7892 Append (Copy_Generic_Node (N, Empty, Instantiating), New_L);
7901 end Copy_Generic_List;
7903 ---------------------------
7904 -- In_Defining_Unit_Name --
7905 ---------------------------
7907 function In_Defining_Unit_Name (Nam : Node_Id) return Boolean is
7910 Present (Parent (Nam))
7911 and then (Nkind (Parent (Nam)) = N_Defining_Program_Unit_Name
7913 (Nkind (Parent (Nam)) = N_Expanded_Name
7914 and then In_Defining_Unit_Name (Parent (Nam))));
7915 end In_Defining_Unit_Name;
7917 -- Start of processing for Copy_Generic_Node
7924 New_N := New_Copy (N);
7926 -- Copy aspects if present
7928 if Has_Aspects (N) then
7929 Set_Has_Aspects (New_N, False);
7930 Set_Aspect_Specifications
7931 (New_N, Copy_Generic_List (Aspect_Specifications (N), Parent_Id));
7934 -- If we are instantiating, we want to adjust the sloc based on the
7935 -- current S_Adjustment. However, if this is the root node of a subunit,
7936 -- we need to defer that adjustment to below (see "elsif Instantiating
7937 -- and Was_Stub"), so it comes after Create_Instantiation_Source has
7938 -- computed the adjustment.
7941 and then not (Nkind (N) in N_Proper_Body
7942 and then Was_Originally_Stub (N))
7944 Adjust_Instantiation_Sloc (New_N, S_Adjustment);
7947 if not Is_List_Member (N) then
7948 Set_Parent (New_N, Parent_Id);
7951 -- Special casing for identifiers and other entity names and operators
7953 if Nkind_In (New_N, N_Character_Literal,
7957 or else Nkind (New_N) in N_Op
7959 if not Instantiating then
7961 -- Link both nodes in order to assign subsequently the entity of
7962 -- the copy to the original node, in case this is a global
7965 Set_Associated_Node (N, New_N);
7967 -- If we are within an instantiation, this is a nested generic
7968 -- that has already been analyzed at the point of definition.
7969 -- We must preserve references that were global to the enclosing
7970 -- parent at that point. Other occurrences, whether global or
7971 -- local to the current generic, must be resolved anew, so we
7972 -- reset the entity in the generic copy. A global reference has a
7973 -- smaller depth than the parent, or else the same depth in case
7974 -- both are distinct compilation units.
7976 -- A child unit is implicitly declared within the enclosing parent
7977 -- but is in fact global to it, and must be preserved.
7979 -- It is also possible for Current_Instantiated_Parent to be
7980 -- defined, and for this not to be a nested generic, namely if
7981 -- the unit is loaded through Rtsfind. In that case, the entity of
7982 -- New_N is only a link to the associated node, and not a defining
7985 -- The entities for parent units in the defining_program_unit of a
7986 -- generic child unit are established when the context of the unit
7987 -- is first analyzed, before the generic copy is made. They are
7988 -- preserved in the copy for use in e.g. ASIS queries.
7990 Ent := Entity (New_N);
7992 if No (Current_Instantiated_Parent.Gen_Id) then
7994 or else Nkind (Ent) /= N_Defining_Identifier
7995 or else not In_Defining_Unit_Name (N)
7997 Set_Associated_Node (New_N, Empty);
8002 not Nkind_In (Ent, N_Defining_Identifier,
8003 N_Defining_Character_Literal,
8004 N_Defining_Operator_Symbol)
8005 or else No (Scope (Ent))
8007 (Scope (Ent) = Current_Instantiated_Parent.Gen_Id
8008 and then not Is_Child_Unit (Ent))
8010 (Scope_Depth (Scope (Ent)) >
8011 Scope_Depth (Current_Instantiated_Parent.Gen_Id)
8013 Get_Source_Unit (Ent) =
8014 Get_Source_Unit (Current_Instantiated_Parent.Gen_Id))
8016 Set_Associated_Node (New_N, Empty);
8019 -- Case of instantiating identifier or some other name or operator
8022 -- If the associated node is still defined, the entity in it
8023 -- is global, and must be copied to the instance. If this copy
8024 -- is being made for a body to inline, it is applied to an
8025 -- instantiated tree, and the entity is already present and
8026 -- must be also preserved.
8029 Assoc : constant Node_Id := Get_Associated_Node (N);
8032 if Present (Assoc) then
8033 if Nkind (Assoc) = Nkind (N) then
8034 Set_Entity (New_N, Entity (Assoc));
8035 Check_Private_View (N);
8037 -- Here we deal with a very peculiar case for which the
8038 -- Has_Private_View mechanism is not sufficient, because
8039 -- the reference to the type is implicit in the tree,
8040 -- that is to say, it's not referenced from a node but
8041 -- only from another type, namely through Component_Type.
8045 -- type Pt is private;
8048 -- type Ft is array (Positive range <>) of Pt;
8050 -- procedure Check (F1, F2 : Ft; Lt : Boolean);
8054 -- type Pt is new Boolean;
8057 -- package body P is
8058 -- package body G is
8059 -- procedure Check (F1, F2 : Ft; Lt : Boolean) is
8061 -- if (F1 < F2) /= Lt then
8068 -- type Arr is array (Positive range <>) of P.Pt;
8070 -- package Inst is new P.G (Arr);
8072 -- Pt is a global type for the generic package G and it
8073 -- is not referenced in its body, but only as component
8074 -- type of Ft, which is a local type. This means that no
8075 -- references to Pt or Ft are seen during the copy of the
8076 -- body, the only reference to Pt being seen is when the
8077 -- actuals are checked by Check_Generic_Actuals, but Pt
8078 -- is still private at this point. In the end, the views
8079 -- of Pt are not switched in the body and, therefore, the
8080 -- array comparison is rejected because the component is
8083 -- Adding e.g. a dummy variable of type Pt in the body is
8084 -- sufficient to make everything work, so we generate an
8085 -- artificial reference to Pt on the fly and thus force
8086 -- the switching of views on the grounds that, if the
8087 -- comparison was accepted during the semantic analysis
8088 -- of the generic, this means that the component cannot
8089 -- have been private (see Sem_Type.Valid_Comparison_Arg).
8091 if Nkind (Assoc) in N_Op_Compare
8092 and then Present (Etype (Left_Opnd (Assoc)))
8093 and then Is_Array_Type (Etype (Left_Opnd (Assoc)))
8094 and then Present (Etype (Right_Opnd (Assoc)))
8095 and then Is_Array_Type (Etype (Right_Opnd (Assoc)))
8098 Ltyp : constant Entity_Id :=
8099 Etype (Left_Opnd (Assoc));
8100 Rtyp : constant Entity_Id :=
8101 Etype (Right_Opnd (Assoc));
8103 if Is_Private_Type (Component_Type (Ltyp)) then
8105 (New_Occurrence_Of (Component_Type (Ltyp),
8108 if Is_Private_Type (Component_Type (Rtyp)) then
8110 (New_Occurrence_Of (Component_Type (Rtyp),
8115 -- Here is a similar case, for the Designated_Type of an
8116 -- access type that is present as target type in a type
8117 -- conversion from another access type. In this case, if
8118 -- the base types of the designated types are different
8119 -- and the conversion was accepted during the semantic
8120 -- analysis of the generic, this means that the target
8121 -- type cannot have been private (see Valid_Conversion).
8123 elsif Nkind (Assoc) = N_Identifier
8124 and then Nkind (Parent (Assoc)) = N_Type_Conversion
8125 and then Subtype_Mark (Parent (Assoc)) = Assoc
8126 and then Present (Etype (Assoc))
8127 and then Is_Access_Type (Etype (Assoc))
8128 and then Present (Etype (Expression (Parent (Assoc))))
8130 Is_Access_Type (Etype (Expression (Parent (Assoc))))
8133 Targ_Desig : constant Entity_Id :=
8134 Designated_Type (Etype (Assoc));
8135 Expr_Desig : constant Entity_Id :=
8137 (Etype (Expression (Parent (Assoc))));
8139 if Base_Type (Targ_Desig) /= Base_Type (Expr_Desig)
8140 and then Is_Private_Type (Targ_Desig)
8143 (New_Occurrence_Of (Targ_Desig, Sloc (N)));
8148 -- The node is a reference to a global type and acts as the
8149 -- subtype mark of a qualified expression created in order
8150 -- to aid resolution of accidental overloading in instances.
8151 -- Since N is a reference to a type, the Associated_Node of
8152 -- N denotes an entity rather than another identifier. See
8153 -- Qualify_Universal_Operands for details.
8155 elsif Nkind (N) = N_Identifier
8156 and then Nkind (Parent (N)) = N_Qualified_Expression
8157 and then Subtype_Mark (Parent (N)) = N
8158 and then Is_Qualified_Universal_Literal (Parent (N))
8160 Set_Entity (New_N, Assoc);
8162 -- The name in the call may be a selected component if the
8163 -- call has not been analyzed yet, as may be the case for
8164 -- pre/post conditions in a generic unit.
8166 elsif Nkind (Assoc) = N_Function_Call
8167 and then Is_Entity_Name (Name (Assoc))
8169 Set_Entity (New_N, Entity (Name (Assoc)));
8171 elsif Nkind_In (Assoc, N_Defining_Identifier,
8172 N_Defining_Character_Literal,
8173 N_Defining_Operator_Symbol)
8174 and then Expander_Active
8176 -- Inlining case: we are copying a tree that contains
8177 -- global entities, which are preserved in the copy to be
8178 -- used for subsequent inlining.
8183 Set_Entity (New_N, Empty);
8189 -- For expanded name, we must copy the Prefix and Selector_Name
8191 if Nkind (N) = N_Expanded_Name then
8193 (New_N, Copy_Generic_Node (Prefix (N), New_N, Instantiating));
8195 Set_Selector_Name (New_N,
8196 Copy_Generic_Node (Selector_Name (N), New_N, Instantiating));
8198 -- For operators, copy the operands
8200 elsif Nkind (N) in N_Op then
8201 if Nkind (N) in N_Binary_Op then
8202 Set_Left_Opnd (New_N,
8203 Copy_Generic_Node (Left_Opnd (N), New_N, Instantiating));
8206 Set_Right_Opnd (New_N,
8207 Copy_Generic_Node (Right_Opnd (N), New_N, Instantiating));
8210 -- Establish a link between an entity from the generic template and the
8211 -- corresponding entity in the generic copy to be analyzed.
8213 elsif Nkind (N) in N_Entity then
8214 if not Instantiating then
8215 Set_Associated_Entity (N, New_N);
8218 -- Clear any existing link the copy may inherit from the replicated
8219 -- generic template entity.
8221 Set_Associated_Entity (New_N, Empty);
8223 -- Special casing for stubs
8225 elsif Nkind (N) in N_Body_Stub then
8227 -- In any case, we must copy the specification or defining
8228 -- identifier as appropriate.
8230 if Nkind (N) = N_Subprogram_Body_Stub then
8231 Set_Specification (New_N,
8232 Copy_Generic_Node (Specification (N), New_N, Instantiating));
8235 Set_Defining_Identifier (New_N,
8237 (Defining_Identifier (N), New_N, Instantiating));
8240 -- If we are not instantiating, then this is where we load and
8241 -- analyze subunits, i.e. at the point where the stub occurs. A
8242 -- more permissive system might defer this analysis to the point
8243 -- of instantiation, but this seems too complicated for now.
8245 if not Instantiating then
8247 Subunit_Name : constant Unit_Name_Type := Get_Unit_Name (N);
8249 Unum : Unit_Number_Type;
8253 -- Make sure that, if it is a subunit of the main unit that is
8254 -- preprocessed and if -gnateG is specified, the preprocessed
8255 -- file will be written.
8257 Lib.Analysing_Subunit_Of_Main :=
8258 Lib.In_Extended_Main_Source_Unit (N);
8261 (Load_Name => Subunit_Name,
8265 Lib.Analysing_Subunit_Of_Main := False;
8267 -- If the proper body is not found, a warning message will be
8268 -- emitted when analyzing the stub, or later at the point of
8269 -- instantiation. Here we just leave the stub as is.
8271 if Unum = No_Unit then
8272 Subunits_Missing := True;
8273 goto Subunit_Not_Found;
8276 Subunit := Cunit (Unum);
8278 if Nkind (Unit (Subunit)) /= N_Subunit then
8280 ("found child unit instead of expected SEPARATE subunit",
8282 Error_Msg_Sloc := Sloc (N);
8283 Error_Msg_N ("\to complete stub #", Subunit);
8284 goto Subunit_Not_Found;
8287 -- We must create a generic copy of the subunit, in order to
8288 -- perform semantic analysis on it, and we must replace the
8289 -- stub in the original generic unit with the subunit, in order
8290 -- to preserve non-local references within.
8292 -- Only the proper body needs to be copied. Library_Unit and
8293 -- context clause are simply inherited by the generic copy.
8294 -- Note that the copy (which may be recursive if there are
8295 -- nested subunits) must be done first, before attaching it to
8296 -- the enclosing generic.
8300 (Proper_Body (Unit (Subunit)),
8301 Empty, Instantiating => False);
8303 -- Now place the original proper body in the original generic
8304 -- unit. This is a body, not a compilation unit.
8306 Rewrite (N, Proper_Body (Unit (Subunit)));
8307 Set_Is_Compilation_Unit (Defining_Entity (N), False);
8308 Set_Was_Originally_Stub (N);
8310 -- Finally replace the body of the subunit with its copy, and
8311 -- make this new subunit into the library unit of the generic
8312 -- copy, which does not have stubs any longer.
8314 Set_Proper_Body (Unit (Subunit), New_Body);
8315 Set_Library_Unit (New_N, Subunit);
8316 Inherit_Context (Unit (Subunit), N);
8319 -- If we are instantiating, this must be an error case, since
8320 -- otherwise we would have replaced the stub node by the proper body
8321 -- that corresponds. So just ignore it in the copy (i.e. we have
8322 -- copied it, and that is good enough).
8328 <<Subunit_Not_Found>> null;
8330 -- If the node is a compilation unit, it is the subunit of a stub, which
8331 -- has been loaded already (see code below). In this case, the library
8332 -- unit field of N points to the parent unit (which is a compilation
8333 -- unit) and need not (and cannot) be copied.
8335 -- When the proper body of the stub is analyzed, the library_unit link
8336 -- is used to establish the proper context (see sem_ch10).
8338 -- The other fields of a compilation unit are copied as usual
8340 elsif Nkind (N) = N_Compilation_Unit then
8342 -- This code can only be executed when not instantiating, because in
8343 -- the copy made for an instantiation, the compilation unit node has
8344 -- disappeared at the point that a stub is replaced by its proper
8347 pragma Assert (not Instantiating);
8349 Set_Context_Items (New_N,
8350 Copy_Generic_List (Context_Items (N), New_N));
8353 Copy_Generic_Node (Unit (N), New_N, Instantiating => False));
8355 Set_First_Inlined_Subprogram (New_N,
8357 (First_Inlined_Subprogram (N), New_N, Instantiating => False));
8362 (Aux_Decls_Node (N), New_N, Instantiating => False));
8364 -- For an assignment node, the assignment is known to be semantically
8365 -- legal if we are instantiating the template. This avoids incorrect
8366 -- diagnostics in generated code.
8368 elsif Nkind (N) = N_Assignment_Statement then
8370 -- Copy name and expression fields in usual manner
8373 Copy_Generic_Node (Name (N), New_N, Instantiating));
8375 Set_Expression (New_N,
8376 Copy_Generic_Node (Expression (N), New_N, Instantiating));
8378 if Instantiating then
8379 Set_Assignment_OK (Name (New_N), True);
8382 elsif Nkind_In (N, N_Aggregate, N_Extension_Aggregate) then
8383 if not Instantiating then
8384 Set_Associated_Node (N, New_N);
8387 if Present (Get_Associated_Node (N))
8388 and then Nkind (Get_Associated_Node (N)) = Nkind (N)
8390 -- In the generic the aggregate has some composite type. If at
8391 -- the point of instantiation the type has a private view,
8392 -- install the full view (and that of its ancestors, if any).
8395 T : Entity_Id := (Etype (Get_Associated_Node (New_N)));
8399 if Present (T) and then Is_Private_Type (T) then
8404 and then Is_Tagged_Type (T)
8405 and then Is_Derived_Type (T)
8407 Rt := Root_Type (T);
8412 if Is_Private_Type (T) then
8423 -- Do not copy the associated node, which points to the generic copy
8424 -- of the aggregate.
8427 use Atree.Unchecked_Access;
8428 -- This code section is part of the implementation of an untyped
8429 -- tree traversal, so it needs direct access to node fields.
8432 Set_Field1 (New_N, Copy_Generic_Descendant (Field1 (N)));
8433 Set_Field2 (New_N, Copy_Generic_Descendant (Field2 (N)));
8434 Set_Field3 (New_N, Copy_Generic_Descendant (Field3 (N)));
8435 Set_Field5 (New_N, Copy_Generic_Descendant (Field5 (N)));
8438 -- Allocators do not have an identifier denoting the access type, so we
8439 -- must locate it through the expression to check whether the views are
8442 elsif Nkind (N) = N_Allocator
8443 and then Nkind (Expression (N)) = N_Qualified_Expression
8444 and then Is_Entity_Name (Subtype_Mark (Expression (N)))
8445 and then Instantiating
8448 T : constant Node_Id :=
8449 Get_Associated_Node (Subtype_Mark (Expression (N)));
8455 -- Retrieve the allocator node in the generic copy
8457 Acc_T := Etype (Parent (Parent (T)));
8459 if Present (Acc_T) and then Is_Private_Type (Acc_T) then
8460 Switch_View (Acc_T);
8467 -- For a proper body, we must catch the case of a proper body that
8468 -- replaces a stub. This represents the point at which a separate
8469 -- compilation unit, and hence template file, may be referenced, so we
8470 -- must make a new source instantiation entry for the template of the
8471 -- subunit, and ensure that all nodes in the subunit are adjusted using
8472 -- this new source instantiation entry.
8474 elsif Nkind (N) in N_Proper_Body then
8476 Save_Adjustment : constant Sloc_Adjustment := S_Adjustment;
8478 if Instantiating and then Was_Originally_Stub (N) then
8479 Create_Instantiation_Source
8480 (Instantiation_Node,
8481 Defining_Entity (N),
8484 Adjust_Instantiation_Sloc (New_N, S_Adjustment);
8487 -- Now copy the fields of the proper body, using the new
8488 -- adjustment factor if one was needed as per test above.
8492 -- Restore the original adjustment factor
8494 S_Adjustment := Save_Adjustment;
8497 elsif Nkind (N) = N_Pragma and then Instantiating then
8499 -- Do not copy Comment or Ident pragmas their content is relevant to
8500 -- the generic unit, not to the instantiating unit.
8502 if Nam_In (Pragma_Name_Unmapped (N), Name_Comment, Name_Ident) then
8503 New_N := Make_Null_Statement (Sloc (N));
8505 -- Do not copy pragmas generated from aspects because the pragmas do
8506 -- not carry any semantic information, plus they will be regenerated
8509 -- However, generating C we need to copy them since postconditions
8510 -- are inlined by the front end, and the front-end inlining machinery
8511 -- relies on this routine to perform inlining.
8513 elsif From_Aspect_Specification (N)
8514 and then not Modify_Tree_For_C
8516 New_N := Make_Null_Statement (Sloc (N));
8522 elsif Nkind_In (N, N_Integer_Literal, N_Real_Literal) then
8524 -- No descendant fields need traversing
8528 elsif Nkind (N) = N_String_Literal
8529 and then Present (Etype (N))
8530 and then Instantiating
8532 -- If the string is declared in an outer scope, the string_literal
8533 -- subtype created for it may have the wrong scope. Force reanalysis
8534 -- of the constant to generate a new itype in the proper context.
8536 Set_Etype (New_N, Empty);
8537 Set_Analyzed (New_N, False);
8539 -- For the remaining nodes, copy their descendants recursively
8544 if Instantiating and then Nkind (N) = N_Subprogram_Body then
8545 Set_Generic_Parent (Specification (New_N), N);
8547 -- Should preserve Corresponding_Spec??? (12.3(14))
8551 -- Propagate dimensions if present, so that they are reflected in the
8554 if Nkind (N) in N_Has_Etype
8555 and then (Nkind (N) in N_Op or else Is_Entity_Name (N))
8556 and then Present (Etype (N))
8557 and then Is_Floating_Point_Type (Etype (N))
8558 and then Has_Dimension_System (Etype (N))
8560 Copy_Dimensions (N, New_N);
8564 end Copy_Generic_Node;
8566 ----------------------------
8567 -- Denotes_Formal_Package --
8568 ----------------------------
8570 function Denotes_Formal_Package
8572 On_Exit : Boolean := False;
8573 Instance : Entity_Id := Empty) return Boolean
8576 Scop : constant Entity_Id := Scope (Pack);
8579 function Is_Actual_Of_Previous_Formal (P : Entity_Id) return Boolean;
8580 -- The package in question may be an actual for a previous formal
8581 -- package P of the current instance, so examine its actuals as well.
8582 -- This must be recursive over other formal packages.
8584 ----------------------------------
8585 -- Is_Actual_Of_Previous_Formal --
8586 ----------------------------------
8588 function Is_Actual_Of_Previous_Formal (P : Entity_Id) return Boolean is
8592 E1 := First_Entity (P);
8593 while Present (E1) and then E1 /= Instance loop
8594 if Ekind (E1) = E_Package
8595 and then Nkind (Parent (E1)) = N_Package_Renaming_Declaration
8597 if Renamed_Object (E1) = Pack then
8600 elsif E1 = P or else Renamed_Object (E1) = P then
8603 elsif Is_Actual_Of_Previous_Formal (E1) then
8612 end Is_Actual_Of_Previous_Formal;
8614 -- Start of processing for Denotes_Formal_Package
8620 (Instance_Envs.Last).Instantiated_Parent.Act_Id;
8622 Par := Current_Instantiated_Parent.Act_Id;
8625 if Ekind (Scop) = E_Generic_Package
8626 or else Nkind (Unit_Declaration_Node (Scop)) =
8627 N_Generic_Subprogram_Declaration
8631 elsif Nkind (Original_Node (Unit_Declaration_Node (Pack))) =
8632 N_Formal_Package_Declaration
8640 -- Check whether this package is associated with a formal package of
8641 -- the enclosing instantiation. Iterate over the list of renamings.
8643 E := First_Entity (Par);
8644 while Present (E) loop
8645 if Ekind (E) /= E_Package
8646 or else Nkind (Parent (E)) /= N_Package_Renaming_Declaration
8650 elsif Renamed_Object (E) = Par then
8653 elsif Renamed_Object (E) = Pack then
8656 elsif Is_Actual_Of_Previous_Formal (E) then
8666 end Denotes_Formal_Package;
8672 procedure End_Generic is
8674 -- ??? More things could be factored out in this routine. Should
8675 -- probably be done at a later stage.
8677 Inside_A_Generic := Generic_Flags.Table (Generic_Flags.Last);
8678 Generic_Flags.Decrement_Last;
8680 Expander_Mode_Restore;
8687 function Earlier (N1, N2 : Node_Id) return Boolean is
8688 procedure Find_Depth (P : in out Node_Id; D : in out Integer);
8689 -- Find distance from given node to enclosing compilation unit
8695 procedure Find_Depth (P : in out Node_Id; D : in out Integer) is
8698 and then Nkind (P) /= N_Compilation_Unit
8700 P := True_Parent (P);
8705 -- Local declarations
8714 -- Start of processing for Earlier
8717 Find_Depth (P1, D1);
8718 Find_Depth (P2, D2);
8728 P1 := True_Parent (P1);
8733 P2 := True_Parent (P2);
8737 -- At this point P1 and P2 are at the same distance from the root.
8738 -- We examine their parents until we find a common declarative list.
8739 -- If we reach the root, N1 and N2 do not descend from the same
8740 -- declarative list (e.g. one is nested in the declarative part and
8741 -- the other is in a block in the statement part) and the earlier
8742 -- one is already frozen.
8744 while not Is_List_Member (P1)
8745 or else not Is_List_Member (P2)
8746 or else List_Containing (P1) /= List_Containing (P2)
8748 P1 := True_Parent (P1);
8749 P2 := True_Parent (P2);
8751 if Nkind (Parent (P1)) = N_Subunit then
8752 P1 := Corresponding_Stub (Parent (P1));
8755 if Nkind (Parent (P2)) = N_Subunit then
8756 P2 := Corresponding_Stub (Parent (P2));
8764 -- Expanded code usually shares the source location of the original
8765 -- construct it was generated for. This however may not necessarily
8766 -- reflect the true location of the code within the tree.
8768 -- Before comparing the slocs of the two nodes, make sure that we are
8769 -- working with correct source locations. Assume that P1 is to the left
8770 -- of P2. If either one does not come from source, traverse the common
8771 -- list heading towards the other node and locate the first source
8775 -- ----+===+===+--------------+===+===+----
8776 -- expanded code expanded code
8778 if not Comes_From_Source (P1) then
8779 while Present (P1) loop
8781 -- Neither P2 nor a source statement were located during the
8782 -- search. If we reach the end of the list, then P1 does not
8783 -- occur earlier than P2.
8786 -- start --- P2 ----- P1 --- end
8788 if No (Next (P1)) then
8791 -- We encounter P2 while going to the right of the list. This
8792 -- means that P1 does indeed appear earlier.
8795 -- start --- P1 ===== P2 --- end
8796 -- expanded code in between
8801 -- No need to look any further since we have located a source
8804 elsif Comes_From_Source (P1) then
8814 if not Comes_From_Source (P2) then
8815 while Present (P2) loop
8817 -- Neither P1 nor a source statement were located during the
8818 -- search. If we reach the start of the list, then P1 does not
8819 -- occur earlier than P2.
8822 -- start --- P2 --- P1 --- end
8824 if No (Prev (P2)) then
8827 -- We encounter P1 while going to the left of the list. This
8828 -- means that P1 does indeed appear earlier.
8831 -- start --- P1 ===== P2 --- end
8832 -- expanded code in between
8837 -- No need to look any further since we have located a source
8840 elsif Comes_From_Source (P2) then
8850 -- At this point either both nodes came from source or we approximated
8851 -- their source locations through neighboring source statements.
8853 T1 := Top_Level_Location (Sloc (P1));
8854 T2 := Top_Level_Location (Sloc (P2));
8856 -- When two nodes come from the same instance, they have identical top
8857 -- level locations. To determine proper relation within the tree, check
8858 -- their locations within the template.
8861 return Sloc (P1) < Sloc (P2);
8863 -- The two nodes either come from unrelated instances or do not come
8864 -- from instantiated code at all.
8871 ----------------------
8872 -- Find_Actual_Type --
8873 ----------------------
8875 function Find_Actual_Type
8877 Gen_Type : Entity_Id) return Entity_Id
8879 Gen_Scope : constant Entity_Id := Scope (Gen_Type);
8883 -- Special processing only applies to child units
8885 if not Is_Child_Unit (Gen_Scope) then
8886 return Get_Instance_Of (Typ);
8888 -- If designated or component type is itself a formal of the child unit,
8889 -- its instance is available.
8891 elsif Scope (Typ) = Gen_Scope then
8892 return Get_Instance_Of (Typ);
8894 -- If the array or access type is not declared in the parent unit,
8895 -- no special processing needed.
8897 elsif not Is_Generic_Type (Typ)
8898 and then Scope (Gen_Scope) /= Scope (Typ)
8900 return Get_Instance_Of (Typ);
8902 -- Otherwise, retrieve designated or component type by visibility
8905 T := Current_Entity (Typ);
8906 while Present (T) loop
8907 if In_Open_Scopes (Scope (T)) then
8909 elsif Is_Generic_Actual_Type (T) then
8918 end Find_Actual_Type;
8920 ----------------------------
8921 -- Freeze_Subprogram_Body --
8922 ----------------------------
8924 procedure Freeze_Subprogram_Body
8925 (Inst_Node : Node_Id;
8927 Pack_Id : Entity_Id)
8929 Gen_Unit : constant Entity_Id := Get_Generic_Entity (Inst_Node);
8930 Par : constant Entity_Id := Scope (Gen_Unit);
8936 function Enclosing_Package_Body (N : Node_Id) return Node_Id;
8937 -- Find innermost package body that encloses the given node, and which
8938 -- is not a compilation unit. Freeze nodes for the instance, or for its
8939 -- enclosing body, may be inserted after the enclosing_body of the
8940 -- generic unit. Used to determine proper placement of freeze node for
8941 -- both package and subprogram instances.
8943 function Package_Freeze_Node (B : Node_Id) return Node_Id;
8944 -- Find entity for given package body, and locate or create a freeze
8947 ----------------------------
8948 -- Enclosing_Package_Body --
8949 ----------------------------
8951 function Enclosing_Package_Body (N : Node_Id) return Node_Id is
8957 and then Nkind (Parent (P)) /= N_Compilation_Unit
8959 if Nkind (P) = N_Package_Body then
8960 if Nkind (Parent (P)) = N_Subunit then
8961 return Corresponding_Stub (Parent (P));
8967 P := True_Parent (P);
8971 end Enclosing_Package_Body;
8973 -------------------------
8974 -- Package_Freeze_Node --
8975 -------------------------
8977 function Package_Freeze_Node (B : Node_Id) return Node_Id is
8981 if Nkind (B) = N_Package_Body then
8982 Id := Corresponding_Spec (B);
8983 else pragma Assert (Nkind (B) = N_Package_Body_Stub);
8984 Id := Corresponding_Spec (Proper_Body (Unit (Library_Unit (B))));
8987 Ensure_Freeze_Node (Id);
8988 return Freeze_Node (Id);
8989 end Package_Freeze_Node;
8991 -- Start of processing for Freeze_Subprogram_Body
8994 -- If the instance and the generic body appear within the same unit, and
8995 -- the instance precedes the generic, the freeze node for the instance
8996 -- must appear after that of the generic. If the generic is nested
8997 -- within another instance I2, then current instance must be frozen
8998 -- after I2. In both cases, the freeze nodes are those of enclosing
8999 -- packages. Otherwise, the freeze node is placed at the end of the
9000 -- current declarative part.
9002 Enc_G := Enclosing_Package_Body (Gen_Body);
9003 Enc_I := Enclosing_Package_Body (Inst_Node);
9004 Ensure_Freeze_Node (Pack_Id);
9005 F_Node := Freeze_Node (Pack_Id);
9007 if Is_Generic_Instance (Par)
9008 and then Present (Freeze_Node (Par))
9009 and then In_Same_Declarative_Part
9010 (Parent (Freeze_Node (Par)), Inst_Node)
9012 -- The parent was a premature instantiation. Insert freeze node at
9013 -- the end the current declarative part.
9015 if Is_Known_Guaranteed_ABE (Get_Unit_Instantiation_Node (Par)) then
9016 Insert_Freeze_Node_For_Instance (Inst_Node, F_Node);
9018 -- Handle the following case:
9020 -- package Parent_Inst is new ...
9023 -- procedure P ... -- this body freezes Parent_Inst
9025 -- package Inst is new ...
9027 -- In this particular scenario, the freeze node for Inst must be
9028 -- inserted in the same manner as that of Parent_Inst - before the
9029 -- next source body or at the end of the declarative list (body not
9030 -- available). If body P did not exist and Parent_Inst was frozen
9031 -- after Inst, either by a body following Inst or at the end of the
9032 -- declarative region, the freeze node for Inst must be inserted
9033 -- after that of Parent_Inst. This relation is established by
9034 -- comparing the Slocs of Parent_Inst freeze node and Inst.
9036 elsif List_Containing (Get_Unit_Instantiation_Node (Par)) =
9037 List_Containing (Inst_Node)
9038 and then Sloc (Freeze_Node (Par)) < Sloc (Inst_Node)
9040 Insert_Freeze_Node_For_Instance (Inst_Node, F_Node);
9043 Insert_After (Freeze_Node (Par), F_Node);
9046 -- The body enclosing the instance should be frozen after the body that
9047 -- includes the generic, because the body of the instance may make
9048 -- references to entities therein. If the two are not in the same
9049 -- declarative part, or if the one enclosing the instance is frozen
9050 -- already, freeze the instance at the end of the current declarative
9053 elsif Is_Generic_Instance (Par)
9054 and then Present (Freeze_Node (Par))
9055 and then Present (Enc_I)
9057 if In_Same_Declarative_Part (Parent (Freeze_Node (Par)), Enc_I)
9059 (Nkind (Enc_I) = N_Package_Body
9060 and then In_Same_Declarative_Part
9061 (Parent (Freeze_Node (Par)), Parent (Enc_I)))
9063 -- The enclosing package may contain several instances. Rather
9064 -- than computing the earliest point at which to insert its freeze
9065 -- node, we place it at the end of the declarative part of the
9066 -- parent of the generic.
9068 Insert_Freeze_Node_For_Instance
9069 (Freeze_Node (Par), Package_Freeze_Node (Enc_I));
9072 Insert_Freeze_Node_For_Instance (Inst_Node, F_Node);
9074 elsif Present (Enc_G)
9075 and then Present (Enc_I)
9076 and then Enc_G /= Enc_I
9077 and then Earlier (Inst_Node, Gen_Body)
9079 if Nkind (Enc_G) = N_Package_Body then
9081 Corresponding_Spec (Enc_G);
9082 else pragma Assert (Nkind (Enc_G) = N_Package_Body_Stub);
9084 Corresponding_Spec (Proper_Body (Unit (Library_Unit (Enc_G))));
9087 -- Freeze package that encloses instance, and place node after the
9088 -- package that encloses generic. If enclosing package is already
9089 -- frozen we have to assume it is at the proper place. This may be a
9090 -- potential ABE that requires dynamic checking. Do not add a freeze
9091 -- node if the package that encloses the generic is inside the body
9092 -- that encloses the instance, because the freeze node would be in
9093 -- the wrong scope. Additional contortions needed if the bodies are
9094 -- within a subunit.
9097 Enclosing_Body : Node_Id;
9100 if Nkind (Enc_I) = N_Package_Body_Stub then
9101 Enclosing_Body := Proper_Body (Unit (Library_Unit (Enc_I)));
9103 Enclosing_Body := Enc_I;
9106 if Parent (List_Containing (Enc_G)) /= Enclosing_Body then
9107 Insert_Freeze_Node_For_Instance
9108 (Enc_G, Package_Freeze_Node (Enc_I));
9112 -- Freeze enclosing subunit before instance
9114 Ensure_Freeze_Node (E_G_Id);
9116 if not Is_List_Member (Freeze_Node (E_G_Id)) then
9117 Insert_After (Enc_G, Freeze_Node (E_G_Id));
9120 Insert_Freeze_Node_For_Instance (Inst_Node, F_Node);
9123 -- If none of the above, insert freeze node at the end of the current
9124 -- declarative part.
9126 Insert_Freeze_Node_For_Instance (Inst_Node, F_Node);
9128 end Freeze_Subprogram_Body;
9134 function Get_Gen_Id (E : Assoc_Ptr) return Entity_Id is
9136 return Generic_Renamings.Table (E).Gen_Id;
9139 ---------------------
9140 -- Get_Instance_Of --
9141 ---------------------
9143 function Get_Instance_Of (A : Entity_Id) return Entity_Id is
9144 Res : constant Assoc_Ptr := Generic_Renamings_HTable.Get (A);
9147 if Res /= Assoc_Null then
9148 return Generic_Renamings.Table (Res).Act_Id;
9151 -- On exit, entity is not instantiated: not a generic parameter, or
9152 -- else parameter of an inner generic unit.
9156 end Get_Instance_Of;
9158 ---------------------------------
9159 -- Get_Unit_Instantiation_Node --
9160 ---------------------------------
9162 function Get_Unit_Instantiation_Node (A : Entity_Id) return Node_Id is
9163 Decl : Node_Id := Unit_Declaration_Node (A);
9167 -- If the Package_Instantiation attribute has been set on the package
9168 -- entity, then use it directly when it (or its Original_Node) refers
9169 -- to an N_Package_Instantiation node. In principle it should be
9170 -- possible to have this field set in all cases, which should be
9171 -- investigated, and would allow this function to be significantly
9174 Inst := Package_Instantiation (A);
9176 if Present (Inst) then
9177 if Nkind (Inst) = N_Package_Instantiation then
9180 elsif Nkind (Original_Node (Inst)) = N_Package_Instantiation then
9181 return Original_Node (Inst);
9185 -- If the instantiation is a compilation unit that does not need body
9186 -- then the instantiation node has been rewritten as a package
9187 -- declaration for the instance, and we return the original node.
9189 -- If it is a compilation unit and the instance node has not been
9190 -- rewritten, then it is still the unit of the compilation. Finally, if
9191 -- a body is present, this is a parent of the main unit whose body has
9192 -- been compiled for inlining purposes, and the instantiation node has
9193 -- been rewritten with the instance body.
9195 -- Otherwise the instantiation node appears after the declaration. If
9196 -- the entity is a formal package, the declaration may have been
9197 -- rewritten as a generic declaration (in the case of a formal with box)
9198 -- or left as a formal package declaration if it has actuals, and is
9199 -- found with a forward search.
9201 if Nkind (Parent (Decl)) = N_Compilation_Unit then
9202 if Nkind (Decl) = N_Package_Declaration
9203 and then Present (Corresponding_Body (Decl))
9205 Decl := Unit_Declaration_Node (Corresponding_Body (Decl));
9208 if Nkind (Original_Node (Decl)) in N_Generic_Instantiation then
9209 return Original_Node (Decl);
9211 return Unit (Parent (Decl));
9214 elsif Nkind (Decl) = N_Package_Declaration
9215 and then Nkind (Original_Node (Decl)) = N_Formal_Package_Declaration
9217 return Original_Node (Decl);
9220 Inst := Next (Decl);
9221 while not Nkind_In (Inst, N_Formal_Package_Declaration,
9222 N_Function_Instantiation,
9223 N_Package_Instantiation,
9224 N_Procedure_Instantiation)
9231 end Get_Unit_Instantiation_Node;
9233 ------------------------
9234 -- Has_Been_Exchanged --
9235 ------------------------
9237 function Has_Been_Exchanged (E : Entity_Id) return Boolean is
9241 Next := First_Elmt (Exchanged_Views);
9242 while Present (Next) loop
9243 if Full_View (Node (Next)) = E then
9251 end Has_Been_Exchanged;
9257 function Has_Contracts (Decl : Node_Id) return Boolean is
9258 A_List : constant List_Id := Aspect_Specifications (Decl);
9265 A_Spec := First (A_List);
9266 while Present (A_Spec) loop
9267 A_Id := Get_Aspect_Id (A_Spec);
9268 if A_Id = Aspect_Pre or else A_Id = Aspect_Post then
9283 function Hash (F : Entity_Id) return HTable_Range is
9285 return HTable_Range (F mod HTable_Size);
9288 ------------------------
9289 -- Hide_Current_Scope --
9290 ------------------------
9292 procedure Hide_Current_Scope is
9293 C : constant Entity_Id := Current_Scope;
9297 Set_Is_Hidden_Open_Scope (C);
9299 E := First_Entity (C);
9300 while Present (E) loop
9301 if Is_Immediately_Visible (E) then
9302 Set_Is_Immediately_Visible (E, False);
9303 Append_Elmt (E, Hidden_Entities);
9309 -- Make the scope name invisible as well. This is necessary, but might
9310 -- conflict with calls to Rtsfind later on, in case the scope is a
9311 -- predefined one. There is no clean solution to this problem, so for
9312 -- now we depend on the user not redefining Standard itself in one of
9313 -- the parent units.
9315 if Is_Immediately_Visible (C) and then C /= Standard_Standard then
9316 Set_Is_Immediately_Visible (C, False);
9317 Append_Elmt (C, Hidden_Entities);
9320 end Hide_Current_Scope;
9326 procedure Init_Env is
9327 Saved : Instance_Env;
9330 Saved.Instantiated_Parent := Current_Instantiated_Parent;
9331 Saved.Exchanged_Views := Exchanged_Views;
9332 Saved.Hidden_Entities := Hidden_Entities;
9333 Saved.Current_Sem_Unit := Current_Sem_Unit;
9334 Saved.Parent_Unit_Visible := Parent_Unit_Visible;
9335 Saved.Instance_Parent_Unit := Instance_Parent_Unit;
9337 -- Save configuration switches. These may be reset if the unit is a
9338 -- predefined unit, and the current mode is not Ada 2005.
9340 Saved.Switches := Save_Config_Switches;
9342 Instance_Envs.Append (Saved);
9344 Exchanged_Views := New_Elmt_List;
9345 Hidden_Entities := New_Elmt_List;
9347 -- Make dummy entry for Instantiated parent. If generic unit is legal,
9348 -- this is set properly in Set_Instance_Env.
9350 Current_Instantiated_Parent :=
9351 (Current_Scope, Current_Scope, Assoc_Null);
9354 ---------------------
9355 -- In_Main_Context --
9356 ---------------------
9358 function In_Main_Context (E : Entity_Id) return Boolean is
9364 if not Is_Compilation_Unit (E)
9365 or else Ekind (E) /= E_Package
9366 or else In_Private_Part (E)
9371 Context := Context_Items (Cunit (Main_Unit));
9373 Clause := First (Context);
9374 while Present (Clause) loop
9375 if Nkind (Clause) = N_With_Clause then
9376 Nam := Name (Clause);
9378 -- If the current scope is part of the context of the main unit,
9379 -- analysis of the corresponding with_clause is not complete, and
9380 -- the entity is not set. We use the Chars field directly, which
9381 -- might produce false positives in rare cases, but guarantees
9382 -- that we produce all the instance bodies we will need.
9384 if (Is_Entity_Name (Nam) and then Chars (Nam) = Chars (E))
9385 or else (Nkind (Nam) = N_Selected_Component
9386 and then Chars (Selector_Name (Nam)) = Chars (E))
9396 end In_Main_Context;
9398 ---------------------
9399 -- Inherit_Context --
9400 ---------------------
9402 procedure Inherit_Context (Gen_Decl : Node_Id; Inst : Node_Id) is
9403 Current_Context : List_Id;
9404 Current_Unit : Node_Id;
9413 if Nkind (Parent (Gen_Decl)) = N_Compilation_Unit then
9415 -- The inherited context is attached to the enclosing compilation
9416 -- unit. This is either the main unit, or the declaration for the
9417 -- main unit (in case the instantiation appears within the package
9418 -- declaration and the main unit is its body).
9420 Current_Unit := Parent (Inst);
9421 while Present (Current_Unit)
9422 and then Nkind (Current_Unit) /= N_Compilation_Unit
9424 Current_Unit := Parent (Current_Unit);
9427 Current_Context := Context_Items (Current_Unit);
9429 Item := First (Context_Items (Parent (Gen_Decl)));
9430 while Present (Item) loop
9431 if Nkind (Item) = N_With_Clause then
9432 Lib_Unit := Library_Unit (Item);
9434 -- Take care to prevent direct cyclic with's
9436 if Lib_Unit /= Current_Unit then
9438 -- Do not add a unit if it is already in the context
9440 Clause := First (Current_Context);
9442 while Present (Clause) loop
9443 if Nkind (Clause) = N_With_Clause
9444 and then Library_Unit (Clause) = Lib_Unit
9454 New_I := New_Copy (Item);
9455 Set_Implicit_With (New_I);
9457 Append (New_I, Current_Context);
9465 end Inherit_Context;
9471 procedure Initialize is
9473 Generic_Renamings.Init;
9476 Generic_Renamings_HTable.Reset;
9477 Circularity_Detected := False;
9478 Exchanged_Views := No_Elist;
9479 Hidden_Entities := No_Elist;
9482 -------------------------------------
9483 -- Insert_Freeze_Node_For_Instance --
9484 -------------------------------------
9486 procedure Insert_Freeze_Node_For_Instance
9495 function Enclosing_Body (N : Node_Id) return Node_Id;
9496 -- Find enclosing package or subprogram body, if any. Freeze node may
9497 -- be placed at end of current declarative list if previous instance
9498 -- and current one have different enclosing bodies.
9500 function Previous_Instance (Gen : Entity_Id) return Entity_Id;
9501 -- Find the local instance, if any, that declares the generic that is
9502 -- being instantiated. If present, the freeze node for this instance
9503 -- must follow the freeze node for the previous instance.
9505 --------------------
9506 -- Enclosing_Body --
9507 --------------------
9509 function Enclosing_Body (N : Node_Id) return Node_Id is
9515 and then Nkind (Parent (P)) /= N_Compilation_Unit
9517 if Nkind_In (P, N_Package_Body, N_Subprogram_Body) then
9518 if Nkind (Parent (P)) = N_Subunit then
9519 return Corresponding_Stub (Parent (P));
9525 P := True_Parent (P);
9531 -----------------------
9532 -- Previous_Instance --
9533 -----------------------
9535 function Previous_Instance (Gen : Entity_Id) return Entity_Id is
9540 while Present (S) and then S /= Standard_Standard loop
9541 if Is_Generic_Instance (S)
9542 and then In_Same_Source_Unit (S, N)
9551 end Previous_Instance;
9553 -- Start of processing for Insert_Freeze_Node_For_Instance
9556 if not Is_List_Member (F_Node) then
9558 Decls := List_Containing (N);
9559 Inst := Entity (F_Node);
9560 Par_N := Parent (Decls);
9562 -- When processing a subprogram instantiation, utilize the actual
9563 -- subprogram instantiation rather than its package wrapper as it
9564 -- carries all the context information.
9566 if Is_Wrapper_Package (Inst) then
9567 Inst := Related_Instance (Inst);
9570 -- If this is a package instance, check whether the generic is
9571 -- declared in a previous instance and the current instance is
9572 -- not within the previous one.
9574 if Present (Generic_Parent (Parent (Inst)))
9575 and then Is_In_Main_Unit (N)
9578 Enclosing_N : constant Node_Id := Enclosing_Body (N);
9579 Par_I : constant Entity_Id :=
9581 (Generic_Parent (Parent (Inst)));
9586 and then Earlier (N, Freeze_Node (Par_I))
9588 Scop := Scope (Inst);
9590 -- If the current instance is within the one that contains
9591 -- the generic, the freeze node for the current one must
9592 -- appear in the current declarative part. Ditto, if the
9593 -- current instance is within another package instance or
9594 -- within a body that does not enclose the current instance.
9595 -- In these three cases the freeze node of the previous
9596 -- instance is not relevant.
9598 while Present (Scop) and then Scop /= Standard_Standard loop
9599 exit when Scop = Par_I
9601 (Is_Generic_Instance (Scop)
9602 and then Scope_Depth (Scop) > Scope_Depth (Par_I));
9603 Scop := Scope (Scop);
9606 -- Previous instance encloses current instance
9608 if Scop = Par_I then
9611 -- If the next node is a source body we must freeze in
9612 -- the current scope as well.
9614 elsif Present (Next (N))
9615 and then Nkind_In (Next (N), N_Subprogram_Body,
9617 and then Comes_From_Source (Next (N))
9621 -- Current instance is within an unrelated instance
9623 elsif Is_Generic_Instance (Scop) then
9626 -- Current instance is within an unrelated body
9628 elsif Present (Enclosing_N)
9629 and then Enclosing_N /= Enclosing_Body (Par_I)
9634 Insert_After (Freeze_Node (Par_I), F_Node);
9641 -- When the instantiation occurs in a package declaration, append the
9642 -- freeze node to the private declarations (if any).
9644 if Nkind (Par_N) = N_Package_Specification
9645 and then Decls = Visible_Declarations (Par_N)
9646 and then Present (Private_Declarations (Par_N))
9647 and then not Is_Empty_List (Private_Declarations (Par_N))
9649 Decls := Private_Declarations (Par_N);
9650 Decl := First (Decls);
9653 -- Determine the proper freeze point of a package instantiation. We
9654 -- adhere to the general rule of a package or subprogram body causing
9655 -- freezing of anything before it in the same declarative region. In
9656 -- this case, the proper freeze point of a package instantiation is
9657 -- before the first source body which follows, or before a stub. This
9658 -- ensures that entities coming from the instance are already frozen
9659 -- and usable in source bodies.
9661 if Nkind (Par_N) /= N_Package_Declaration
9662 and then Ekind (Inst) = E_Package
9663 and then Is_Generic_Instance (Inst)
9665 not In_Same_Source_Unit (Generic_Parent (Parent (Inst)), Inst)
9667 while Present (Decl) loop
9668 if (Nkind (Decl) in N_Unit_Body
9670 Nkind (Decl) in N_Body_Stub)
9671 and then Comes_From_Source (Decl)
9673 Insert_Before (Decl, F_Node);
9681 -- In a package declaration, or if no previous body, insert at end
9684 Set_Sloc (F_Node, Sloc (Last (Decls)));
9685 Insert_After (Last (Decls), F_Node);
9687 end Insert_Freeze_Node_For_Instance;
9693 procedure Install_Body
9694 (Act_Body : Node_Id;
9699 function In_Same_Scope (Gen_Id, Act_Id : Node_Id) return Boolean;
9700 -- Check if the generic definition and the instantiation come from
9701 -- a common scope, in which case the instance must be frozen after
9702 -- the generic body.
9704 function True_Sloc (N, Act_Unit : Node_Id) return Source_Ptr;
9705 -- If the instance is nested inside a generic unit, the Sloc of the
9706 -- instance indicates the place of the original definition, not the
9707 -- point of the current enclosing instance. Pending a better usage of
9708 -- Slocs to indicate instantiation places, we determine the place of
9709 -- origin of a node by finding the maximum sloc of any ancestor node.
9710 -- Why is this not equivalent to Top_Level_Location ???
9716 function In_Same_Scope (Gen_Id, Act_Id : Node_Id) return Boolean is
9717 Act_Scop : Entity_Id := Scope (Act_Id);
9718 Gen_Scop : Entity_Id := Scope (Gen_Id);
9721 while Act_Scop /= Standard_Standard
9722 and then Gen_Scop /= Standard_Standard
9724 if Act_Scop = Gen_Scop then
9728 Act_Scop := Scope (Act_Scop);
9729 Gen_Scop := Scope (Gen_Scop);
9739 function True_Sloc (N, Act_Unit : Node_Id) return Source_Ptr is
9746 while Present (N1) and then N1 /= Act_Unit loop
9747 if Sloc (N1) > Res then
9757 Act_Id : constant Entity_Id := Corresponding_Spec (Act_Body);
9758 Act_Unit : constant Node_Id := Unit (Cunit (Get_Source_Unit (N)));
9759 Gen_Id : constant Entity_Id := Corresponding_Spec (Gen_Body);
9760 Par : constant Entity_Id := Scope (Gen_Id);
9761 Gen_Unit : constant Node_Id :=
9762 Unit (Cunit (Get_Source_Unit (Gen_Decl)));
9764 Body_Unit : Node_Id;
9766 Must_Delay : Boolean;
9767 Orig_Body : Node_Id := Gen_Body;
9769 -- Start of processing for Install_Body
9772 -- Handle first the case of an instance with incomplete actual types.
9773 -- The instance body cannot be placed after the declaration because
9774 -- full views have not been seen yet. Any use of the non-limited views
9775 -- in the instance body requires the presence of a regular with_clause
9776 -- in the enclosing unit, and will fail if this with_clause is missing.
9777 -- We place the instance body at the beginning of the enclosing body,
9778 -- which is the unit being compiled. The freeze node for the instance
9779 -- is then placed after the instance body.
9781 if not Is_Empty_Elmt_List (Incomplete_Actuals (Act_Id))
9782 and then Expander_Active
9783 and then Ekind (Scope (Act_Id)) = E_Package
9786 Scop : constant Entity_Id := Scope (Act_Id);
9787 Body_Id : constant Node_Id :=
9788 Corresponding_Body (Unit_Declaration_Node (Scop));
9791 Ensure_Freeze_Node (Act_Id);
9792 F_Node := Freeze_Node (Act_Id);
9793 if Present (Body_Id) then
9794 Set_Is_Frozen (Act_Id, False);
9795 Prepend (Act_Body, Declarations (Parent (Body_Id)));
9796 if Is_List_Member (F_Node) then
9800 Insert_After (Act_Body, F_Node);
9806 -- If the body is a subunit, the freeze point is the corresponding stub
9807 -- in the current compilation, not the subunit itself.
9809 if Nkind (Parent (Gen_Body)) = N_Subunit then
9810 Orig_Body := Corresponding_Stub (Parent (Gen_Body));
9812 Orig_Body := Gen_Body;
9815 Body_Unit := Unit (Cunit (Get_Source_Unit (Orig_Body)));
9817 -- If the instantiation and the generic definition appear in the same
9818 -- package declaration, this is an early instantiation. If they appear
9819 -- in the same declarative part, it is an early instantiation only if
9820 -- the generic body appears textually later, and the generic body is
9821 -- also in the main unit.
9823 -- If instance is nested within a subprogram, and the generic body
9824 -- is not, the instance is delayed because the enclosing body is. If
9825 -- instance and body are within the same scope, or the same subprogram
9826 -- body, indicate explicitly that the instance is delayed.
9829 (Gen_Unit = Act_Unit
9830 and then (Nkind_In (Gen_Unit, N_Generic_Package_Declaration,
9831 N_Package_Declaration)
9832 or else (Gen_Unit = Body_Unit
9833 and then True_Sloc (N, Act_Unit) <
9835 and then Is_In_Main_Unit (Original_Node (Gen_Unit))
9836 and then In_Same_Scope (Gen_Id, Act_Id));
9838 -- If this is an early instantiation, the freeze node is placed after
9839 -- the generic body. Otherwise, if the generic appears in an instance,
9840 -- we cannot freeze the current instance until the outer one is frozen.
9841 -- This is only relevant if the current instance is nested within some
9842 -- inner scope not itself within the outer instance. If this scope is
9843 -- a package body in the same declarative part as the outer instance,
9844 -- then that body needs to be frozen after the outer instance. Finally,
9845 -- if no delay is needed, we place the freeze node at the end of the
9846 -- current declarative part.
9849 and then (No (Freeze_Node (Act_Id))
9850 or else not Is_List_Member (Freeze_Node (Act_Id)))
9852 Ensure_Freeze_Node (Act_Id);
9853 F_Node := Freeze_Node (Act_Id);
9856 Insert_After (Orig_Body, F_Node);
9858 elsif Is_Generic_Instance (Par)
9859 and then Present (Freeze_Node (Par))
9860 and then Scope (Act_Id) /= Par
9862 -- Freeze instance of inner generic after instance of enclosing
9865 if In_Same_Declarative_Part (Parent (Freeze_Node (Par)), N) then
9867 -- Handle the following case:
9869 -- package Parent_Inst is new ...
9872 -- procedure P ... -- this body freezes Parent_Inst
9874 -- package Inst is new ...
9876 -- In this particular scenario, the freeze node for Inst must
9877 -- be inserted in the same manner as that of Parent_Inst,
9878 -- before the next source body or at the end of the declarative
9879 -- list (body not available). If body P did not exist and
9880 -- Parent_Inst was frozen after Inst, either by a body
9881 -- following Inst or at the end of the declarative region,
9882 -- the freeze node for Inst must be inserted after that of
9883 -- Parent_Inst. This relation is established by comparing
9884 -- the Slocs of Parent_Inst freeze node and Inst.
9885 -- We examine the parents of the enclosing lists to handle
9886 -- the case where the parent instance is in the visible part
9887 -- of a package declaration, and the inner instance is in
9888 -- the corresponding private part.
9890 if Parent (List_Containing (Get_Unit_Instantiation_Node (Par)))
9891 = Parent (List_Containing (N))
9892 and then Sloc (Freeze_Node (Par)) < Sloc (N)
9894 Insert_Freeze_Node_For_Instance (N, F_Node);
9896 Insert_After (Freeze_Node (Par), F_Node);
9899 -- Freeze package enclosing instance of inner generic after
9900 -- instance of enclosing generic.
9902 elsif Nkind_In (Parent (N), N_Package_Body, N_Subprogram_Body)
9903 and then In_Same_Declarative_Part
9904 (Parent (Freeze_Node (Par)), Parent (N))
9907 Enclosing : Entity_Id;
9910 Enclosing := Corresponding_Spec (Parent (N));
9912 if No (Enclosing) then
9913 Enclosing := Defining_Entity (Parent (N));
9916 Insert_Freeze_Node_For_Instance (N, F_Node);
9917 Ensure_Freeze_Node (Enclosing);
9919 if not Is_List_Member (Freeze_Node (Enclosing)) then
9921 -- The enclosing context is a subunit, insert the freeze
9922 -- node after the stub.
9924 if Nkind (Parent (Parent (N))) = N_Subunit then
9925 Insert_Freeze_Node_For_Instance
9926 (Corresponding_Stub (Parent (Parent (N))),
9927 Freeze_Node (Enclosing));
9929 -- The enclosing context is a package with a stub body
9930 -- which has already been replaced by the real body.
9931 -- Insert the freeze node after the actual body.
9933 elsif Ekind (Enclosing) = E_Package
9934 and then Present (Body_Entity (Enclosing))
9935 and then Was_Originally_Stub
9936 (Parent (Body_Entity (Enclosing)))
9938 Insert_Freeze_Node_For_Instance
9939 (Parent (Body_Entity (Enclosing)),
9940 Freeze_Node (Enclosing));
9942 -- The parent instance has been frozen before the body of
9943 -- the enclosing package, insert the freeze node after
9946 elsif List_Containing (Freeze_Node (Par)) =
9947 List_Containing (Parent (N))
9948 and then Sloc (Freeze_Node (Par)) < Sloc (Parent (N))
9950 Insert_Freeze_Node_For_Instance
9951 (Parent (N), Freeze_Node (Enclosing));
9955 (Freeze_Node (Par), Freeze_Node (Enclosing));
9961 Insert_Freeze_Node_For_Instance (N, F_Node);
9965 Insert_Freeze_Node_For_Instance (N, F_Node);
9969 Set_Is_Frozen (Act_Id);
9970 Insert_Before (N, Act_Body);
9971 Mark_Rewrite_Insertion (Act_Body);
9974 -----------------------------
9975 -- Install_Formal_Packages --
9976 -----------------------------
9978 procedure Install_Formal_Packages (Par : Entity_Id) is
9981 Gen_E : Entity_Id := Empty;
9984 E := First_Entity (Par);
9986 -- If we are installing an instance parent, locate the formal packages
9987 -- of its generic parent.
9989 if Is_Generic_Instance (Par) then
9990 Gen := Generic_Parent (Package_Specification (Par));
9991 Gen_E := First_Entity (Gen);
9994 while Present (E) loop
9995 if Ekind (E) = E_Package
9996 and then Nkind (Parent (E)) = N_Package_Renaming_Declaration
9998 -- If this is the renaming for the parent instance, done
10000 if Renamed_Object (E) = Par then
10003 -- The visibility of a formal of an enclosing generic is already
10006 elsif Denotes_Formal_Package (E) then
10009 elsif Present (Associated_Formal_Package (E)) then
10010 Check_Generic_Actuals (Renamed_Object (E), True);
10011 Set_Is_Hidden (E, False);
10013 -- Find formal package in generic unit that corresponds to
10014 -- (instance of) formal package in instance.
10016 while Present (Gen_E) and then Chars (Gen_E) /= Chars (E) loop
10017 Next_Entity (Gen_E);
10020 if Present (Gen_E) then
10021 Map_Formal_Package_Entities (Gen_E, E);
10028 if Present (Gen_E) then
10029 Next_Entity (Gen_E);
10032 end Install_Formal_Packages;
10034 --------------------
10035 -- Install_Parent --
10036 --------------------
10038 procedure Install_Parent (P : Entity_Id; In_Body : Boolean := False) is
10039 Ancestors : constant Elist_Id := New_Elmt_List;
10040 S : constant Entity_Id := Current_Scope;
10041 Inst_Par : Entity_Id;
10042 First_Par : Entity_Id;
10043 Inst_Node : Node_Id;
10044 Gen_Par : Entity_Id;
10045 First_Gen : Entity_Id;
10048 procedure Install_Noninstance_Specs (Par : Entity_Id);
10049 -- Install the scopes of noninstance parent units ending with Par
10051 procedure Install_Spec (Par : Entity_Id);
10052 -- The child unit is within the declarative part of the parent, so the
10053 -- declarations within the parent are immediately visible.
10055 -------------------------------
10056 -- Install_Noninstance_Specs --
10057 -------------------------------
10059 procedure Install_Noninstance_Specs (Par : Entity_Id) is
10062 and then Par /= Standard_Standard
10063 and then not In_Open_Scopes (Par)
10065 Install_Noninstance_Specs (Scope (Par));
10066 Install_Spec (Par);
10068 end Install_Noninstance_Specs;
10074 procedure Install_Spec (Par : Entity_Id) is
10075 Spec : constant Node_Id := Package_Specification (Par);
10078 -- If this parent of the child instance is a top-level unit,
10079 -- then record the unit and its visibility for later resetting in
10080 -- Remove_Parent. We exclude units that are generic instances, as we
10081 -- only want to record this information for the ultimate top-level
10082 -- noninstance parent (is that always correct???).
10084 if Scope (Par) = Standard_Standard
10085 and then not Is_Generic_Instance (Par)
10087 Parent_Unit_Visible := Is_Immediately_Visible (Par);
10088 Instance_Parent_Unit := Par;
10091 -- Open the parent scope and make it and its declarations visible.
10092 -- If this point is not within a body, then only the visible
10093 -- declarations should be made visible, and installation of the
10094 -- private declarations is deferred until the appropriate point
10095 -- within analysis of the spec being instantiated (see the handling
10096 -- of parent visibility in Analyze_Package_Specification). This is
10097 -- relaxed in the case where the parent unit is Ada.Tags, to avoid
10098 -- private view problems that occur when compiling instantiations of
10099 -- a generic child of that package (Generic_Dispatching_Constructor).
10100 -- If the instance freezes a tagged type, inlinings of operations
10101 -- from Ada.Tags may need the full view of type Tag. If inlining took
10102 -- proper account of establishing visibility of inlined subprograms'
10103 -- parents then it should be possible to remove this
10104 -- special check. ???
10107 Set_Is_Immediately_Visible (Par);
10108 Install_Visible_Declarations (Par);
10109 Set_Use (Visible_Declarations (Spec));
10111 if In_Body or else Is_RTU (Par, Ada_Tags) then
10112 Install_Private_Declarations (Par);
10113 Set_Use (Private_Declarations (Spec));
10117 -- Start of processing for Install_Parent
10120 -- We need to install the parent instance to compile the instantiation
10121 -- of the child, but the child instance must appear in the current
10122 -- scope. Given that we cannot place the parent above the current scope
10123 -- in the scope stack, we duplicate the current scope and unstack both
10124 -- after the instantiation is complete.
10126 -- If the parent is itself the instantiation of a child unit, we must
10127 -- also stack the instantiation of its parent, and so on. Each such
10128 -- ancestor is the prefix of the name in a prior instantiation.
10130 -- If this is a nested instance, the parent unit itself resolves to
10131 -- a renaming of the parent instance, whose declaration we need.
10133 -- Finally, the parent may be a generic (not an instance) when the
10134 -- child unit appears as a formal package.
10138 if Present (Renamed_Entity (Inst_Par)) then
10139 Inst_Par := Renamed_Entity (Inst_Par);
10142 First_Par := Inst_Par;
10144 Gen_Par := Generic_Parent (Package_Specification (Inst_Par));
10146 First_Gen := Gen_Par;
10148 while Present (Gen_Par) and then Is_Child_Unit (Gen_Par) loop
10150 -- Load grandparent instance as well
10152 Inst_Node := Get_Unit_Instantiation_Node (Inst_Par);
10154 if Nkind (Name (Inst_Node)) = N_Expanded_Name then
10155 Inst_Par := Entity (Prefix (Name (Inst_Node)));
10157 if Present (Renamed_Entity (Inst_Par)) then
10158 Inst_Par := Renamed_Entity (Inst_Par);
10161 Gen_Par := Generic_Parent (Package_Specification (Inst_Par));
10163 if Present (Gen_Par) then
10164 Prepend_Elmt (Inst_Par, Ancestors);
10167 -- Parent is not the name of an instantiation
10169 Install_Noninstance_Specs (Inst_Par);
10180 if Present (First_Gen) then
10181 Append_Elmt (First_Par, Ancestors);
10183 Install_Noninstance_Specs (First_Par);
10186 if not Is_Empty_Elmt_List (Ancestors) then
10187 Elmt := First_Elmt (Ancestors);
10188 while Present (Elmt) loop
10189 Install_Spec (Node (Elmt));
10190 Install_Formal_Packages (Node (Elmt));
10195 if not In_Body then
10198 end Install_Parent;
10200 -------------------------------
10201 -- Install_Hidden_Primitives --
10202 -------------------------------
10204 procedure Install_Hidden_Primitives
10205 (Prims_List : in out Elist_Id;
10210 List : Elist_Id := No_Elist;
10211 Prim_G_Elmt : Elmt_Id;
10212 Prim_A_Elmt : Elmt_Id;
10217 -- No action needed in case of serious errors because we cannot trust
10218 -- in the order of primitives
10220 if Serious_Errors_Detected > 0 then
10223 -- No action possible if we don't have available the list of primitive
10227 or else not Is_Record_Type (Gen_T)
10228 or else not Is_Tagged_Type (Gen_T)
10229 or else not Is_Record_Type (Act_T)
10230 or else not Is_Tagged_Type (Act_T)
10234 -- There is no need to handle interface types since their primitives
10235 -- cannot be hidden
10237 elsif Is_Interface (Gen_T) then
10241 Prim_G_Elmt := First_Elmt (Primitive_Operations (Gen_T));
10243 if not Is_Class_Wide_Type (Act_T) then
10244 Prim_A_Elmt := First_Elmt (Primitive_Operations (Act_T));
10246 Prim_A_Elmt := First_Elmt (Primitive_Operations (Root_Type (Act_T)));
10250 -- Skip predefined primitives in the generic formal
10252 while Present (Prim_G_Elmt)
10253 and then Is_Predefined_Dispatching_Operation (Node (Prim_G_Elmt))
10255 Next_Elmt (Prim_G_Elmt);
10258 -- Skip predefined primitives in the generic actual
10260 while Present (Prim_A_Elmt)
10261 and then Is_Predefined_Dispatching_Operation (Node (Prim_A_Elmt))
10263 Next_Elmt (Prim_A_Elmt);
10266 exit when No (Prim_G_Elmt) or else No (Prim_A_Elmt);
10268 Prim_G := Node (Prim_G_Elmt);
10269 Prim_A := Node (Prim_A_Elmt);
10271 -- There is no need to handle interface primitives because their
10272 -- primitives are not hidden
10274 exit when Present (Interface_Alias (Prim_G));
10276 -- Here we install one hidden primitive
10278 if Chars (Prim_G) /= Chars (Prim_A)
10279 and then Has_Suffix (Prim_A, 'P')
10280 and then Remove_Suffix (Prim_A, 'P') = Chars (Prim_G)
10282 Set_Chars (Prim_A, Chars (Prim_G));
10283 Append_New_Elmt (Prim_A, To => List);
10286 Next_Elmt (Prim_A_Elmt);
10287 Next_Elmt (Prim_G_Elmt);
10290 -- Append the elements to the list of temporarily visible primitives
10291 -- avoiding duplicates.
10293 if Present (List) then
10294 if No (Prims_List) then
10295 Prims_List := New_Elmt_List;
10298 Elmt := First_Elmt (List);
10299 while Present (Elmt) loop
10300 Append_Unique_Elmt (Node (Elmt), Prims_List);
10304 end Install_Hidden_Primitives;
10306 -------------------------------
10307 -- Restore_Hidden_Primitives --
10308 -------------------------------
10310 procedure Restore_Hidden_Primitives (Prims_List : in out Elist_Id) is
10311 Prim_Elmt : Elmt_Id;
10315 if Prims_List /= No_Elist then
10316 Prim_Elmt := First_Elmt (Prims_List);
10317 while Present (Prim_Elmt) loop
10318 Prim := Node (Prim_Elmt);
10319 Set_Chars (Prim, Add_Suffix (Prim, 'P'));
10320 Next_Elmt (Prim_Elmt);
10323 Prims_List := No_Elist;
10325 end Restore_Hidden_Primitives;
10327 --------------------------------
10328 -- Instantiate_Formal_Package --
10329 --------------------------------
10331 function Instantiate_Formal_Package
10334 Analyzed_Formal : Node_Id) return List_Id
10336 Loc : constant Source_Ptr := Sloc (Actual);
10337 Hidden_Formals : constant Elist_Id := New_Elmt_List;
10338 Actual_Pack : Entity_Id;
10339 Formal_Pack : Entity_Id;
10340 Gen_Parent : Entity_Id;
10343 Parent_Spec : Node_Id;
10345 procedure Find_Matching_Actual
10347 Act : in out Entity_Id);
10348 -- We need to associate each formal entity in the formal package with
10349 -- the corresponding entity in the actual package. The actual package
10350 -- has been analyzed and possibly expanded, and as a result there is
10351 -- no one-to-one correspondence between the two lists (for example,
10352 -- the actual may include subtypes, itypes, and inherited primitive
10353 -- operations, interspersed among the renaming declarations for the
10354 -- actuals). We retrieve the corresponding actual by name because each
10355 -- actual has the same name as the formal, and they do appear in the
10358 function Get_Formal_Entity (N : Node_Id) return Entity_Id;
10359 -- Retrieve entity of defining entity of generic formal parameter.
10360 -- Only the declarations of formals need to be considered when
10361 -- linking them to actuals, but the declarative list may include
10362 -- internal entities generated during analysis, and those are ignored.
10364 procedure Match_Formal_Entity
10365 (Formal_Node : Node_Id;
10366 Formal_Ent : Entity_Id;
10367 Actual_Ent : Entity_Id);
10368 -- Associates the formal entity with the actual. In the case where
10369 -- Formal_Ent is a formal package, this procedure iterates through all
10370 -- of its formals and enters associations between the actuals occurring
10371 -- in the formal package's corresponding actual package (given by
10372 -- Actual_Ent) and the formal package's formal parameters. This
10373 -- procedure recurses if any of the parameters is itself a package.
10375 function Is_Instance_Of
10376 (Act_Spec : Entity_Id;
10377 Gen_Anc : Entity_Id) return Boolean;
10378 -- The actual can be an instantiation of a generic within another
10379 -- instance, in which case there is no direct link from it to the
10380 -- original generic ancestor. In that case, we recognize that the
10381 -- ultimate ancestor is the same by examining names and scopes.
10383 procedure Process_Nested_Formal (Formal : Entity_Id);
10384 -- If the current formal is declared with a box, its own formals are
10385 -- visible in the instance, as they were in the generic, and their
10386 -- Hidden flag must be reset. If some of these formals are themselves
10387 -- packages declared with a box, the processing must be recursive.
10389 --------------------------
10390 -- Find_Matching_Actual --
10391 --------------------------
10393 procedure Find_Matching_Actual
10395 Act : in out Entity_Id)
10397 Formal_Ent : Entity_Id;
10400 case Nkind (Original_Node (F)) is
10401 when N_Formal_Object_Declaration
10402 | N_Formal_Type_Declaration
10404 Formal_Ent := Defining_Identifier (F);
10406 while Chars (Act) /= Chars (Formal_Ent) loop
10410 when N_Formal_Package_Declaration
10411 | N_Formal_Subprogram_Declaration
10412 | N_Generic_Package_Declaration
10413 | N_Package_Declaration
10415 Formal_Ent := Defining_Entity (F);
10417 while Chars (Act) /= Chars (Formal_Ent) loop
10422 raise Program_Error;
10424 end Find_Matching_Actual;
10426 -------------------------
10427 -- Match_Formal_Entity --
10428 -------------------------
10430 procedure Match_Formal_Entity
10431 (Formal_Node : Node_Id;
10432 Formal_Ent : Entity_Id;
10433 Actual_Ent : Entity_Id)
10435 Act_Pkg : Entity_Id;
10438 Set_Instance_Of (Formal_Ent, Actual_Ent);
10440 if Ekind (Actual_Ent) = E_Package then
10442 -- Record associations for each parameter
10444 Act_Pkg := Actual_Ent;
10447 A_Ent : Entity_Id := First_Entity (Act_Pkg);
10451 Gen_Decl : Node_Id;
10453 Actual : Entity_Id;
10456 -- Retrieve the actual given in the formal package declaration
10458 Actual := Entity (Name (Original_Node (Formal_Node)));
10460 -- The actual in the formal package declaration may be a
10461 -- renamed generic package, in which case we want to retrieve
10462 -- the original generic in order to traverse its formal part.
10464 if Present (Renamed_Entity (Actual)) then
10465 Gen_Decl := Unit_Declaration_Node (Renamed_Entity (Actual));
10467 Gen_Decl := Unit_Declaration_Node (Actual);
10470 Formals := Generic_Formal_Declarations (Gen_Decl);
10472 if Present (Formals) then
10473 F_Node := First_Non_Pragma (Formals);
10478 while Present (A_Ent)
10479 and then Present (F_Node)
10480 and then A_Ent /= First_Private_Entity (Act_Pkg)
10482 F_Ent := Get_Formal_Entity (F_Node);
10484 if Present (F_Ent) then
10486 -- This is a formal of the original package. Record
10487 -- association and recurse.
10489 Find_Matching_Actual (F_Node, A_Ent);
10490 Match_Formal_Entity (F_Node, F_Ent, A_Ent);
10491 Next_Entity (A_Ent);
10494 Next_Non_Pragma (F_Node);
10498 end Match_Formal_Entity;
10500 -----------------------
10501 -- Get_Formal_Entity --
10502 -----------------------
10504 function Get_Formal_Entity (N : Node_Id) return Entity_Id is
10505 Kind : constant Node_Kind := Nkind (Original_Node (N));
10508 when N_Formal_Object_Declaration =>
10509 return Defining_Identifier (N);
10511 when N_Formal_Type_Declaration =>
10512 return Defining_Identifier (N);
10514 when N_Formal_Subprogram_Declaration =>
10515 return Defining_Unit_Name (Specification (N));
10517 when N_Formal_Package_Declaration =>
10518 return Defining_Identifier (Original_Node (N));
10520 when N_Generic_Package_Declaration =>
10521 return Defining_Identifier (Original_Node (N));
10523 -- All other declarations are introduced by semantic analysis and
10524 -- have no match in the actual.
10529 end Get_Formal_Entity;
10531 --------------------
10532 -- Is_Instance_Of --
10533 --------------------
10535 function Is_Instance_Of
10536 (Act_Spec : Entity_Id;
10537 Gen_Anc : Entity_Id) return Boolean
10539 Gen_Par : constant Entity_Id := Generic_Parent (Act_Spec);
10542 if No (Gen_Par) then
10545 -- Simplest case: the generic parent of the actual is the formal
10547 elsif Gen_Par = Gen_Anc then
10550 elsif Chars (Gen_Par) /= Chars (Gen_Anc) then
10553 -- The actual may be obtained through several instantiations. Its
10554 -- scope must itself be an instance of a generic declared in the
10555 -- same scope as the formal. Any other case is detected above.
10557 elsif not Is_Generic_Instance (Scope (Gen_Par)) then
10561 return Generic_Parent (Parent (Scope (Gen_Par))) = Scope (Gen_Anc);
10563 end Is_Instance_Of;
10565 ---------------------------
10566 -- Process_Nested_Formal --
10567 ---------------------------
10569 procedure Process_Nested_Formal (Formal : Entity_Id) is
10573 if Present (Associated_Formal_Package (Formal))
10574 and then Box_Present (Parent (Associated_Formal_Package (Formal)))
10576 Ent := First_Entity (Formal);
10577 while Present (Ent) loop
10578 Set_Is_Hidden (Ent, False);
10579 Set_Is_Visible_Formal (Ent);
10580 Set_Is_Potentially_Use_Visible
10581 (Ent, Is_Potentially_Use_Visible (Formal));
10583 if Ekind (Ent) = E_Package then
10584 exit when Renamed_Entity (Ent) = Renamed_Entity (Formal);
10585 Process_Nested_Formal (Ent);
10591 end Process_Nested_Formal;
10593 -- Start of processing for Instantiate_Formal_Package
10598 -- The actual must be a package instance, or else a current instance
10599 -- such as a parent generic within the body of a generic child.
10601 if not Is_Entity_Name (Actual)
10602 or else not Is_Package_Or_Generic_Package (Entity (Actual))
10605 ("expect package instance to instantiate formal", Actual);
10606 Abandon_Instantiation (Actual);
10607 raise Program_Error;
10610 Actual_Pack := Entity (Actual);
10611 Set_Is_Instantiated (Actual_Pack);
10613 -- The actual may be a renamed package, or an outer generic formal
10614 -- package whose instantiation is converted into a renaming.
10616 if Present (Renamed_Object (Actual_Pack)) then
10617 Actual_Pack := Renamed_Object (Actual_Pack);
10620 if Nkind (Analyzed_Formal) = N_Formal_Package_Declaration then
10621 Gen_Parent := Get_Instance_Of (Entity (Name (Analyzed_Formal)));
10622 Formal_Pack := Defining_Identifier (Analyzed_Formal);
10625 Generic_Parent (Specification (Analyzed_Formal));
10627 Defining_Unit_Name (Specification (Analyzed_Formal));
10630 if Nkind (Parent (Actual_Pack)) = N_Defining_Program_Unit_Name then
10631 Parent_Spec := Package_Specification (Actual_Pack);
10633 Parent_Spec := Parent (Actual_Pack);
10636 if Gen_Parent = Any_Id then
10638 ("previous error in declaration of formal package", Actual);
10639 Abandon_Instantiation (Actual);
10641 elsif Is_Instance_Of (Parent_Spec, Get_Instance_Of (Gen_Parent)) then
10644 -- If this is the current instance of an enclosing generic, that unit
10645 -- is the generic package we need.
10647 elsif In_Open_Scopes (Actual_Pack)
10648 and then Ekind (Actual_Pack) = E_Generic_Package
10654 ("actual parameter must be instance of&", Actual, Gen_Parent);
10655 Abandon_Instantiation (Actual);
10658 Set_Instance_Of (Defining_Identifier (Formal), Actual_Pack);
10659 Map_Formal_Package_Entities (Formal_Pack, Actual_Pack);
10662 Make_Package_Renaming_Declaration (Loc,
10663 Defining_Unit_Name => New_Copy (Defining_Identifier (Formal)),
10664 Name => New_Occurrence_Of (Actual_Pack, Loc));
10666 Set_Associated_Formal_Package
10667 (Defining_Unit_Name (Nod), Defining_Identifier (Formal));
10668 Decls := New_List (Nod);
10670 -- If the formal F has a box, then the generic declarations are
10671 -- visible in the generic G. In an instance of G, the corresponding
10672 -- entities in the actual for F (which are the actuals for the
10673 -- instantiation of the generic that F denotes) must also be made
10674 -- visible for analysis of the current instance. On exit from the
10675 -- current instance, those entities are made private again. If the
10676 -- actual is currently in use, these entities are also use-visible.
10678 -- The loop through the actual entities also steps through the formal
10679 -- entities and enters associations from formals to actuals into the
10680 -- renaming map. This is necessary to properly handle checking of
10681 -- actual parameter associations for later formals that depend on
10682 -- actuals declared in the formal package.
10684 -- In Ada 2005, partial parameterization requires that we make
10685 -- visible the actuals corresponding to formals that were defaulted
10686 -- in the formal package. There formals are identified because they
10687 -- remain formal generics within the formal package, rather than
10688 -- being renamings of the actuals supplied.
10691 Gen_Decl : constant Node_Id :=
10692 Unit_Declaration_Node (Gen_Parent);
10693 Formals : constant List_Id :=
10694 Generic_Formal_Declarations (Gen_Decl);
10696 Actual_Ent : Entity_Id;
10697 Actual_Of_Formal : Node_Id;
10698 Formal_Node : Node_Id;
10699 Formal_Ent : Entity_Id;
10702 if Present (Formals) then
10703 Formal_Node := First_Non_Pragma (Formals);
10705 Formal_Node := Empty;
10708 Actual_Ent := First_Entity (Actual_Pack);
10709 Actual_Of_Formal :=
10710 First (Visible_Declarations (Specification (Analyzed_Formal)));
10711 while Present (Actual_Ent)
10712 and then Actual_Ent /= First_Private_Entity (Actual_Pack)
10714 if Present (Formal_Node) then
10715 Formal_Ent := Get_Formal_Entity (Formal_Node);
10717 if Present (Formal_Ent) then
10718 Find_Matching_Actual (Formal_Node, Actual_Ent);
10719 Match_Formal_Entity (Formal_Node, Formal_Ent, Actual_Ent);
10721 -- We iterate at the same time over the actuals of the
10722 -- local package created for the formal, to determine
10723 -- which one of the formals of the original generic were
10724 -- defaulted in the formal. The corresponding actual
10725 -- entities are visible in the enclosing instance.
10727 if Box_Present (Formal)
10729 (Present (Actual_Of_Formal)
10732 (Get_Formal_Entity (Actual_Of_Formal)))
10734 Set_Is_Hidden (Actual_Ent, False);
10735 Set_Is_Visible_Formal (Actual_Ent);
10736 Set_Is_Potentially_Use_Visible
10737 (Actual_Ent, In_Use (Actual_Pack));
10739 if Ekind (Actual_Ent) = E_Package then
10740 Process_Nested_Formal (Actual_Ent);
10744 if not Is_Hidden (Actual_Ent) then
10745 Append_Elmt (Actual_Ent, Hidden_Formals);
10748 Set_Is_Hidden (Actual_Ent);
10749 Set_Is_Potentially_Use_Visible (Actual_Ent, False);
10753 Next_Non_Pragma (Formal_Node);
10754 Next (Actual_Of_Formal);
10757 -- No further formals to match, but the generic part may
10758 -- contain inherited operation that are not hidden in the
10759 -- enclosing instance.
10761 Next_Entity (Actual_Ent);
10765 -- Inherited subprograms generated by formal derived types are
10766 -- also visible if the types are.
10768 Actual_Ent := First_Entity (Actual_Pack);
10769 while Present (Actual_Ent)
10770 and then Actual_Ent /= First_Private_Entity (Actual_Pack)
10772 if Is_Overloadable (Actual_Ent)
10774 Nkind (Parent (Actual_Ent)) = N_Subtype_Declaration
10776 not Is_Hidden (Defining_Identifier (Parent (Actual_Ent)))
10778 Set_Is_Hidden (Actual_Ent, False);
10779 Set_Is_Potentially_Use_Visible
10780 (Actual_Ent, In_Use (Actual_Pack));
10783 Next_Entity (Actual_Ent);
10786 -- No conformance to check if the generic has no formal parameters
10787 -- and the formal package has no generic associations.
10789 if Is_Empty_List (Formals)
10791 (Box_Present (Formal)
10792 or else No (Generic_Associations (Formal)))
10798 -- If the formal is not declared with a box, reanalyze it as an
10799 -- abbreviated instantiation, to verify the matching rules of 12.7.
10800 -- The actual checks are performed after the generic associations
10801 -- have been analyzed, to guarantee the same visibility for this
10802 -- instantiation and for the actuals.
10804 -- In Ada 2005, the generic associations for the formal can include
10805 -- defaulted parameters. These are ignored during check. This
10806 -- internal instantiation is removed from the tree after conformance
10807 -- checking, because it contains formal declarations for those
10808 -- defaulted parameters, and those should not reach the back-end.
10810 if not Box_Present (Formal) then
10812 I_Pack : constant Entity_Id :=
10813 Make_Temporary (Sloc (Actual), 'P');
10816 Set_Is_Internal (I_Pack);
10817 Set_Ekind (I_Pack, E_Package);
10818 Set_Hidden_In_Formal_Instance (I_Pack, Hidden_Formals);
10821 Make_Package_Instantiation (Sloc (Actual),
10822 Defining_Unit_Name => I_Pack,
10825 (Get_Instance_Of (Gen_Parent), Sloc (Actual)),
10826 Generic_Associations => Generic_Associations (Formal)));
10832 end Instantiate_Formal_Package;
10834 -----------------------------------
10835 -- Instantiate_Formal_Subprogram --
10836 -----------------------------------
10838 function Instantiate_Formal_Subprogram
10841 Analyzed_Formal : Node_Id) return Node_Id
10843 Analyzed_S : constant Entity_Id :=
10844 Defining_Unit_Name (Specification (Analyzed_Formal));
10845 Formal_Sub : constant Entity_Id :=
10846 Defining_Unit_Name (Specification (Formal));
10848 function From_Parent_Scope (Subp : Entity_Id) return Boolean;
10849 -- If the generic is a child unit, the parent has been installed on the
10850 -- scope stack, but a default subprogram cannot resolve to something
10851 -- on the parent because that parent is not really part of the visible
10852 -- context (it is there to resolve explicit local entities). If the
10853 -- default has resolved in this way, we remove the entity from immediate
10854 -- visibility and analyze the node again to emit an error message or
10855 -- find another visible candidate.
10857 procedure Valid_Actual_Subprogram (Act : Node_Id);
10858 -- Perform legality check and raise exception on failure
10860 -----------------------
10861 -- From_Parent_Scope --
10862 -----------------------
10864 function From_Parent_Scope (Subp : Entity_Id) return Boolean is
10865 Gen_Scope : Node_Id;
10868 Gen_Scope := Scope (Analyzed_S);
10869 while Present (Gen_Scope) and then Is_Child_Unit (Gen_Scope) loop
10870 if Scope (Subp) = Scope (Gen_Scope) then
10874 Gen_Scope := Scope (Gen_Scope);
10878 end From_Parent_Scope;
10880 -----------------------------
10881 -- Valid_Actual_Subprogram --
10882 -----------------------------
10884 procedure Valid_Actual_Subprogram (Act : Node_Id) is
10888 if Is_Entity_Name (Act) then
10889 Act_E := Entity (Act);
10891 elsif Nkind (Act) = N_Selected_Component
10892 and then Is_Entity_Name (Selector_Name (Act))
10894 Act_E := Entity (Selector_Name (Act));
10900 if (Present (Act_E) and then Is_Overloadable (Act_E))
10901 or else Nkind_In (Act, N_Attribute_Reference,
10902 N_Indexed_Component,
10903 N_Character_Literal,
10904 N_Explicit_Dereference)
10910 ("expect subprogram or entry name in instantiation of &",
10911 Instantiation_Node, Formal_Sub);
10912 Abandon_Instantiation (Instantiation_Node);
10913 end Valid_Actual_Subprogram;
10917 Decl_Node : Node_Id;
10920 New_Spec : Node_Id;
10921 New_Subp : Entity_Id;
10923 -- Start of processing for Instantiate_Formal_Subprogram
10926 New_Spec := New_Copy_Tree (Specification (Formal));
10928 -- The tree copy has created the proper instantiation sloc for the
10929 -- new specification. Use this location for all other constructed
10932 Loc := Sloc (Defining_Unit_Name (New_Spec));
10934 -- Create new entity for the actual (New_Copy_Tree does not), and
10935 -- indicate that it is an actual.
10937 -- If the actual is not an entity and the formal includes aspect
10938 -- specifications for contracts, we create an internal name for
10939 -- the renaming declaration. The constructed wrapper contains a
10940 -- call to the entity in the renaming.
10942 if Ada_Version >= Ada_2020
10943 and then Present (Aspect_Specifications (Analyzed_Formal))
10945 New_Subp := Make_Temporary (Sloc (Actual), 'S');
10946 Set_Defining_Unit_Name (New_Spec, New_Subp);
10948 New_Subp := Make_Defining_Identifier (Loc, Chars (Formal_Sub));
10951 Set_Ekind (New_Subp, Ekind (Analyzed_S));
10952 Set_Is_Generic_Actual_Subprogram (New_Subp);
10953 Set_Defining_Unit_Name (New_Spec, New_Subp);
10955 -- Create new entities for the each of the formals in the specification
10956 -- of the renaming declaration built for the actual.
10958 if Present (Parameter_Specifications (New_Spec)) then
10964 F := First (Parameter_Specifications (New_Spec));
10965 while Present (F) loop
10966 F_Id := Defining_Identifier (F);
10968 Set_Defining_Identifier (F,
10969 Make_Defining_Identifier (Sloc (F_Id), Chars (F_Id)));
10975 -- Find entity of actual. If the actual is an attribute reference, it
10976 -- cannot be resolved here (its formal is missing) but is handled
10977 -- instead in Attribute_Renaming. If the actual is overloaded, it is
10978 -- fully resolved subsequently, when the renaming declaration for the
10979 -- formal is analyzed. If it is an explicit dereference, resolve the
10980 -- prefix but not the actual itself, to prevent interpretation as call.
10982 if Present (Actual) then
10983 Loc := Sloc (Actual);
10984 Set_Sloc (New_Spec, Loc);
10986 if Nkind (Actual) = N_Operator_Symbol then
10987 Find_Direct_Name (Actual);
10989 elsif Nkind (Actual) = N_Explicit_Dereference then
10990 Analyze (Prefix (Actual));
10992 elsif Nkind (Actual) /= N_Attribute_Reference then
10996 Valid_Actual_Subprogram (Actual);
10999 elsif Present (Default_Name (Formal)) then
11000 if not Nkind_In (Default_Name (Formal), N_Attribute_Reference,
11001 N_Selected_Component,
11002 N_Indexed_Component,
11003 N_Character_Literal)
11004 and then Present (Entity (Default_Name (Formal)))
11006 Nam := New_Occurrence_Of (Entity (Default_Name (Formal)), Loc);
11008 Nam := New_Copy (Default_Name (Formal));
11009 Set_Sloc (Nam, Loc);
11012 elsif Box_Present (Formal) then
11014 -- Actual is resolved at the point of instantiation. Create an
11015 -- identifier or operator with the same name as the formal.
11017 if Nkind (Formal_Sub) = N_Defining_Operator_Symbol then
11019 Make_Operator_Symbol (Loc,
11020 Chars => Chars (Formal_Sub),
11021 Strval => No_String);
11023 Nam := Make_Identifier (Loc, Chars (Formal_Sub));
11026 elsif Nkind (Specification (Formal)) = N_Procedure_Specification
11027 and then Null_Present (Specification (Formal))
11029 -- Generate null body for procedure, for use in the instance
11032 Make_Subprogram_Body (Loc,
11033 Specification => New_Spec,
11034 Declarations => New_List,
11035 Handled_Statement_Sequence =>
11036 Make_Handled_Sequence_Of_Statements (Loc,
11037 Statements => New_List (Make_Null_Statement (Loc))));
11039 -- RM 12.6 (16 2/2): The procedure has convention Intrinsic
11041 Set_Convention (Defining_Unit_Name (New_Spec), Convention_Intrinsic);
11043 -- Eliminate the calls to it when optimization is enabled
11045 Set_Is_Inlined (Defining_Unit_Name (New_Spec));
11049 Error_Msg_Sloc := Sloc (Scope (Analyzed_S));
11051 ("missing actual&", Instantiation_Node, Formal_Sub);
11053 ("\in instantiation of & declared#",
11054 Instantiation_Node, Scope (Analyzed_S));
11055 Abandon_Instantiation (Instantiation_Node);
11059 Make_Subprogram_Renaming_Declaration (Loc,
11060 Specification => New_Spec,
11063 -- If we do not have an actual and the formal specified <> then set to
11064 -- get proper default.
11066 if No (Actual) and then Box_Present (Formal) then
11067 Set_From_Default (Decl_Node);
11070 -- Gather possible interpretations for the actual before analyzing the
11071 -- instance. If overloaded, it will be resolved when analyzing the
11072 -- renaming declaration.
11074 if Box_Present (Formal) and then No (Actual) then
11077 if Is_Child_Unit (Scope (Analyzed_S))
11078 and then Present (Entity (Nam))
11080 if not Is_Overloaded (Nam) then
11081 if From_Parent_Scope (Entity (Nam)) then
11082 Set_Is_Immediately_Visible (Entity (Nam), False);
11083 Set_Entity (Nam, Empty);
11084 Set_Etype (Nam, Empty);
11087 Set_Is_Immediately_Visible (Entity (Nam));
11096 Get_First_Interp (Nam, I, It);
11097 while Present (It.Nam) loop
11098 if From_Parent_Scope (It.Nam) then
11102 Get_Next_Interp (I, It);
11109 -- The generic instantiation freezes the actual. This can only be done
11110 -- once the actual is resolved, in the analysis of the renaming
11111 -- declaration. To make the formal subprogram entity available, we set
11112 -- Corresponding_Formal_Spec to point to the formal subprogram entity.
11113 -- This is also needed in Analyze_Subprogram_Renaming for the processing
11114 -- of formal abstract subprograms.
11116 Set_Corresponding_Formal_Spec (Decl_Node, Analyzed_S);
11118 -- We cannot analyze the renaming declaration, and thus find the actual,
11119 -- until all the actuals are assembled in the instance. For subsequent
11120 -- checks of other actuals, indicate the node that will hold the
11121 -- instance of this formal.
11123 Set_Instance_Of (Analyzed_S, Nam);
11125 if Nkind (Actual) = N_Selected_Component
11126 and then Is_Task_Type (Etype (Prefix (Actual)))
11127 and then not Is_Frozen (Etype (Prefix (Actual)))
11129 -- The renaming declaration will create a body, which must appear
11130 -- outside of the instantiation, We move the renaming declaration
11131 -- out of the instance, and create an additional renaming inside,
11132 -- to prevent freezing anomalies.
11135 Anon_Id : constant Entity_Id := Make_Temporary (Loc, 'E');
11138 Set_Defining_Unit_Name (New_Spec, Anon_Id);
11139 Insert_Before (Instantiation_Node, Decl_Node);
11140 Analyze (Decl_Node);
11142 -- Now create renaming within the instance
11145 Make_Subprogram_Renaming_Declaration (Loc,
11146 Specification => New_Copy_Tree (New_Spec),
11147 Name => New_Occurrence_Of (Anon_Id, Loc));
11149 Set_Defining_Unit_Name (Specification (Decl_Node),
11150 Make_Defining_Identifier (Loc, Chars (Formal_Sub)));
11155 end Instantiate_Formal_Subprogram;
11157 ------------------------
11158 -- Instantiate_Object --
11159 ------------------------
11161 function Instantiate_Object
11164 Analyzed_Formal : Node_Id) return List_Id
11166 Gen_Obj : constant Entity_Id := Defining_Identifier (Formal);
11167 A_Gen_Obj : constant Entity_Id :=
11168 Defining_Identifier (Analyzed_Formal);
11169 Acc_Def : Node_Id := Empty;
11170 Act_Assoc : constant Node_Id := Parent (Actual);
11171 Actual_Decl : Node_Id := Empty;
11172 Decl_Node : Node_Id;
11175 List : constant List_Id := New_List;
11176 Loc : constant Source_Ptr := Sloc (Actual);
11177 Orig_Ftyp : constant Entity_Id := Etype (A_Gen_Obj);
11178 Subt_Decl : Node_Id := Empty;
11179 Subt_Mark : Node_Id := Empty;
11181 -- Start of processing for Instantiate_Object
11184 -- Formal may be an anonymous access
11186 if Present (Subtype_Mark (Formal)) then
11187 Subt_Mark := Subtype_Mark (Formal);
11189 Check_Access_Definition (Formal);
11190 Acc_Def := Access_Definition (Formal);
11193 -- Sloc for error message on missing actual
11195 Error_Msg_Sloc := Sloc (Scope (A_Gen_Obj));
11197 if Get_Instance_Of (Gen_Obj) /= Gen_Obj then
11198 Error_Msg_N ("duplicate instantiation of generic parameter", Actual);
11201 Set_Parent (List, Parent (Actual));
11205 if Out_Present (Formal) then
11207 -- An IN OUT generic actual must be a name. The instantiation is a
11208 -- renaming declaration. The actual is the name being renamed. We
11209 -- use the actual directly, rather than a copy, because it is not
11210 -- used further in the list of actuals, and because a copy or a use
11211 -- of relocate_node is incorrect if the instance is nested within a
11212 -- generic. In order to simplify e.g. ASIS queries, the
11213 -- Generic_Parent field links the declaration to the generic
11216 if No (Actual) then
11218 ("missing actual &",
11219 Instantiation_Node, Gen_Obj);
11221 ("\in instantiation of & declared#",
11222 Instantiation_Node, Scope (A_Gen_Obj));
11223 Abandon_Instantiation (Instantiation_Node);
11226 if Present (Subt_Mark) then
11228 Make_Object_Renaming_Declaration (Loc,
11229 Defining_Identifier => New_Copy (Gen_Obj),
11230 Subtype_Mark => New_Copy_Tree (Subt_Mark),
11233 else pragma Assert (Present (Acc_Def));
11235 Make_Object_Renaming_Declaration (Loc,
11236 Defining_Identifier => New_Copy (Gen_Obj),
11237 Access_Definition => New_Copy_Tree (Acc_Def),
11241 Set_Corresponding_Generic_Association (Decl_Node, Act_Assoc);
11243 -- The analysis of the actual may produce Insert_Action nodes, so
11244 -- the declaration must have a context in which to attach them.
11246 Append (Decl_Node, List);
11249 -- Return if the analysis of the actual reported some error
11251 if Etype (Actual) = Any_Type then
11255 -- This check is performed here because Analyze_Object_Renaming will
11256 -- not check it when Comes_From_Source is False. Note though that the
11257 -- check for the actual being the name of an object will be performed
11258 -- in Analyze_Object_Renaming.
11260 if Is_Object_Reference (Actual)
11261 and then Is_Dependent_Component_Of_Mutable_Object (Actual)
11264 ("illegal discriminant-dependent component for in out parameter",
11268 -- The actual has to be resolved in order to check that it is a
11269 -- variable (due to cases such as F (1), where F returns access to
11270 -- an array, and for overloaded prefixes).
11272 Ftyp := Get_Instance_Of (Etype (A_Gen_Obj));
11274 -- If the type of the formal is not itself a formal, and the current
11275 -- unit is a child unit, the formal type must be declared in a
11276 -- parent, and must be retrieved by visibility.
11278 if Ftyp = Orig_Ftyp
11279 and then Is_Generic_Unit (Scope (Ftyp))
11280 and then Is_Child_Unit (Scope (A_Gen_Obj))
11283 Temp : constant Node_Id :=
11284 New_Copy_Tree (Subtype_Mark (Analyzed_Formal));
11286 Set_Entity (Temp, Empty);
11288 Ftyp := Entity (Temp);
11292 if Is_Private_Type (Ftyp)
11293 and then not Is_Private_Type (Etype (Actual))
11294 and then (Base_Type (Full_View (Ftyp)) = Base_Type (Etype (Actual))
11295 or else Base_Type (Etype (Actual)) = Ftyp)
11297 -- If the actual has the type of the full view of the formal, or
11298 -- else a non-private subtype of the formal, then the visibility
11299 -- of the formal type has changed. Add to the actuals a subtype
11300 -- declaration that will force the exchange of views in the body
11301 -- of the instance as well.
11304 Make_Subtype_Declaration (Loc,
11305 Defining_Identifier => Make_Temporary (Loc, 'P'),
11306 Subtype_Indication => New_Occurrence_Of (Ftyp, Loc));
11308 Prepend (Subt_Decl, List);
11310 Prepend_Elmt (Full_View (Ftyp), Exchanged_Views);
11311 Exchange_Declarations (Ftyp);
11314 Resolve (Actual, Ftyp);
11316 if not Denotes_Variable (Actual) then
11317 Error_Msg_NE ("actual for& must be a variable", Actual, Gen_Obj);
11319 elsif Base_Type (Ftyp) /= Base_Type (Etype (Actual)) then
11321 -- Ada 2005 (AI-423): For a generic formal object of mode in out,
11322 -- the type of the actual shall resolve to a specific anonymous
11325 if Ada_Version < Ada_2005
11326 or else Ekind (Base_Type (Ftyp)) not in Anonymous_Access_Kind
11327 or else Ekind (Base_Type (Etype (Actual)))
11328 not in Anonymous_Access_Kind
11331 ("type of actual does not match type of&", Actual, Gen_Obj);
11335 Note_Possible_Modification (Actual, Sure => True);
11337 -- Check for instantiation with atomic/volatile object actual for
11338 -- nonatomic/nonvolatile formal (RM C.6 (12)).
11340 if Is_Atomic_Object (Actual) and then not Is_Atomic (Orig_Ftyp) then
11342 ("cannot instantiate nonatomic formal & of mode in out",
11344 Error_Msg_N ("\with atomic object actual (RM C.6(12))", Actual);
11346 elsif Is_Volatile_Object (Actual) and then not Is_Volatile (Orig_Ftyp)
11349 ("cannot instantiate nonvolatile formal & of mode in out",
11351 Error_Msg_N ("\with volatile object actual (RM C.6(12))", Actual);
11354 -- Check for instantiation on nonatomic subcomponent of an atomic
11355 -- object in Ada 2020 (RM C.6 (13)).
11357 if Ada_Version >= Ada_2020
11358 and then Is_Subcomponent_Of_Atomic_Object (Actual)
11359 and then not Is_Atomic_Object (Actual)
11362 ("cannot instantiate formal & of mode in out with actual",
11365 ("\nonatomic subcomponent of atomic object (RM C.6(13))",
11369 -- Check actual/formal compatibility with respect to the four
11370 -- volatility refinement aspects.
11373 Actual_Obj : Entity_Id;
11374 N : Node_Id := Actual;
11376 -- Similar to Sem_Util.Get_Enclosing_Object, but treat
11377 -- pointer dereference like component selection.
11379 if Is_Entity_Name (N) then
11380 Actual_Obj := Entity (N);
11385 when N_Indexed_Component
11386 | N_Selected_Component
11388 | N_Explicit_Dereference
11392 when N_Type_Conversion =>
11393 N := Expression (N);
11396 Actual_Obj := Etype (N);
11401 Check_Volatility_Compatibility
11402 (Actual_Obj, A_Gen_Obj, "actual object",
11403 "its corresponding formal object of mode in out",
11404 Srcpos_Bearer => Actual);
11407 -- Formal in-parameter
11410 -- The instantiation of a generic formal in-parameter is constant
11411 -- declaration. The actual is the expression for that declaration.
11412 -- Its type is a full copy of the type of the formal. This may be
11413 -- an access to subprogram, for which we need to generate entities
11414 -- for the formals in the new signature.
11416 if Present (Actual) then
11417 if Present (Subt_Mark) then
11418 Def := New_Copy_Tree (Subt_Mark);
11420 pragma Assert (Present (Acc_Def));
11421 Def := New_Copy_Tree (Acc_Def);
11425 Make_Object_Declaration (Loc,
11426 Defining_Identifier => New_Copy (Gen_Obj),
11427 Constant_Present => True,
11428 Null_Exclusion_Present => Null_Exclusion_Present (Formal),
11429 Object_Definition => Def,
11430 Expression => Actual);
11432 Set_Corresponding_Generic_Association (Decl_Node, Act_Assoc);
11434 -- A generic formal object of a tagged type is defined to be
11435 -- aliased so the new constant must also be treated as aliased.
11437 if Is_Tagged_Type (Etype (A_Gen_Obj)) then
11438 Set_Aliased_Present (Decl_Node);
11441 Append (Decl_Node, List);
11443 -- No need to repeat (pre-)analysis of some expression nodes
11444 -- already handled in Preanalyze_Actuals.
11446 if Nkind (Actual) /= N_Allocator then
11449 -- Return if the analysis of the actual reported some error
11451 if Etype (Actual) = Any_Type then
11457 Formal_Type : constant Entity_Id := Etype (A_Gen_Obj);
11461 Typ := Get_Instance_Of (Formal_Type);
11463 -- If the actual appears in the current or an enclosing scope,
11464 -- use its type directly. This is relevant if it has an actual
11465 -- subtype that is distinct from its nominal one. This cannot
11466 -- be done in general because the type of the actual may
11467 -- depend on other actuals, and only be fully determined when
11468 -- the enclosing instance is analyzed.
11470 if Present (Etype (Actual))
11471 and then Is_Constr_Subt_For_U_Nominal (Etype (Actual))
11473 Freeze_Before (Instantiation_Node, Etype (Actual));
11475 Freeze_Before (Instantiation_Node, Typ);
11478 -- If the actual is an aggregate, perform name resolution on
11479 -- its components (the analysis of an aggregate does not do it)
11480 -- to capture local names that may be hidden if the generic is
11483 if Nkind (Actual) = N_Aggregate then
11484 Preanalyze_And_Resolve (Actual, Typ);
11487 if Is_Limited_Type (Typ)
11488 and then not OK_For_Limited_Init (Typ, Actual)
11491 ("initialization not allowed for limited types", Actual);
11492 Explain_Limited_Type (Typ, Actual);
11496 elsif Present (Default_Expression (Formal)) then
11498 -- Use default to construct declaration
11500 if Present (Subt_Mark) then
11501 Def := New_Copy (Subt_Mark);
11503 pragma Assert (Present (Acc_Def));
11504 Def := New_Copy_Tree (Acc_Def);
11508 Make_Object_Declaration (Sloc (Formal),
11509 Defining_Identifier => New_Copy (Gen_Obj),
11510 Constant_Present => True,
11511 Null_Exclusion_Present => Null_Exclusion_Present (Formal),
11512 Object_Definition => Def,
11513 Expression => New_Copy_Tree
11514 (Default_Expression (Formal)));
11516 Set_Corresponding_Generic_Association
11517 (Decl_Node, Expression (Decl_Node));
11519 Append (Decl_Node, List);
11520 Set_Analyzed (Expression (Decl_Node), False);
11523 Error_Msg_NE ("missing actual&", Instantiation_Node, Gen_Obj);
11524 Error_Msg_NE ("\in instantiation of & declared#",
11525 Instantiation_Node, Scope (A_Gen_Obj));
11527 if Is_Scalar_Type (Etype (A_Gen_Obj)) then
11529 -- Create dummy constant declaration so that instance can be
11530 -- analyzed, to minimize cascaded visibility errors.
11532 if Present (Subt_Mark) then
11534 else pragma Assert (Present (Acc_Def));
11539 Make_Object_Declaration (Loc,
11540 Defining_Identifier => New_Copy (Gen_Obj),
11541 Constant_Present => True,
11542 Null_Exclusion_Present => Null_Exclusion_Present (Formal),
11543 Object_Definition => New_Copy (Def),
11545 Make_Attribute_Reference (Sloc (Gen_Obj),
11546 Attribute_Name => Name_First,
11547 Prefix => New_Copy (Def)));
11549 Append (Decl_Node, List);
11552 Abandon_Instantiation (Instantiation_Node);
11557 if Nkind (Actual) in N_Has_Entity then
11558 Actual_Decl := Parent (Entity (Actual));
11561 -- Ada 2005 (AI-423) refined by AI12-0287:
11562 -- For an object_renaming_declaration with a null_exclusion or an
11563 -- access_definition that has a null_exclusion, the subtype of the
11564 -- object_name shall exclude null. In addition, if the
11565 -- object_renaming_declaration occurs within the body of a generic unit
11566 -- G or within the body of a generic unit declared within the
11567 -- declarative region of generic unit G, then:
11568 -- * if the object_name statically denotes a generic formal object of
11569 -- mode in out of G, then the declaration of that object shall have a
11571 -- * if the object_name statically denotes a call of a generic formal
11572 -- function of G, then the declaration of the result of that function
11573 -- shall have a null_exclusion.
11575 if Ada_Version >= Ada_2005
11576 and then Present (Actual_Decl)
11577 and then Nkind_In (Actual_Decl, N_Formal_Object_Declaration,
11578 N_Object_Declaration)
11579 and then Nkind (Analyzed_Formal) = N_Formal_Object_Declaration
11580 and then not Has_Null_Exclusion (Actual_Decl)
11581 and then Has_Null_Exclusion (Analyzed_Formal)
11582 and then Ekind (Defining_Identifier (Analyzed_Formal))
11583 = E_Generic_In_Out_Parameter
11584 and then ((In_Generic_Scope (Entity (Actual))
11585 and then In_Package_Body (Scope (Entity (Actual))))
11586 or else not Can_Never_Be_Null (Etype (Actual)))
11588 Error_Msg_Sloc := Sloc (Analyzed_Formal);
11590 ("actual must exclude null to match generic formal#", Actual);
11593 -- An effectively volatile object cannot be used as an actual in a
11594 -- generic instantiation (SPARK RM 7.1.3(7)). The following check is
11595 -- relevant only when SPARK_Mode is on as it is not a standard Ada
11596 -- legality rule, and also verifies that the actual is an object.
11599 and then Present (Actual)
11600 and then Is_Object_Reference (Actual)
11601 and then Is_Effectively_Volatile_Object (Actual)
11602 and then not Is_Effectively_Volatile (A_Gen_Obj)
11605 ("volatile object cannot act as actual in generic instantiation",
11610 end Instantiate_Object;
11612 ------------------------------
11613 -- Instantiate_Package_Body --
11614 ------------------------------
11616 -- WARNING: This routine manages Ghost and SPARK regions. Return statements
11617 -- must be replaced by gotos which jump to the end of the routine in order
11618 -- to restore the Ghost and SPARK modes.
11620 procedure Instantiate_Package_Body
11621 (Body_Info : Pending_Body_Info;
11622 Inlined_Body : Boolean := False;
11623 Body_Optional : Boolean := False)
11625 Act_Decl : constant Node_Id := Body_Info.Act_Decl;
11626 Act_Decl_Id : constant Entity_Id := Defining_Entity (Act_Decl);
11627 Act_Spec : constant Node_Id := Specification (Act_Decl);
11628 Inst_Node : constant Node_Id := Body_Info.Inst_Node;
11629 Gen_Id : constant Node_Id := Name (Inst_Node);
11630 Gen_Unit : constant Entity_Id := Get_Generic_Entity (Inst_Node);
11631 Gen_Decl : constant Node_Id := Unit_Declaration_Node (Gen_Unit);
11632 Loc : constant Source_Ptr := Sloc (Inst_Node);
11634 procedure Check_Initialized_Types;
11635 -- In a generic package body, an entity of a generic private type may
11636 -- appear uninitialized. This is suspicious, unless the actual is a
11637 -- fully initialized type.
11639 -----------------------------
11640 -- Check_Initialized_Types --
11641 -----------------------------
11643 procedure Check_Initialized_Types is
11645 Formal : Entity_Id;
11646 Actual : Entity_Id;
11647 Uninit_Var : Entity_Id;
11650 Decl := First (Generic_Formal_Declarations (Gen_Decl));
11651 while Present (Decl) loop
11652 Uninit_Var := Empty;
11654 if Nkind (Decl) = N_Private_Extension_Declaration then
11655 Uninit_Var := Uninitialized_Variable (Decl);
11657 elsif Nkind (Decl) = N_Formal_Type_Declaration
11658 and then Nkind (Formal_Type_Definition (Decl)) =
11659 N_Formal_Private_Type_Definition
11662 Uninitialized_Variable (Formal_Type_Definition (Decl));
11665 if Present (Uninit_Var) then
11666 Formal := Defining_Identifier (Decl);
11667 Actual := First_Entity (Act_Decl_Id);
11669 -- For each formal there is a subtype declaration that renames
11670 -- the actual and has the same name as the formal. Locate the
11671 -- formal for warning message about uninitialized variables
11672 -- in the generic, for which the actual type should be a fully
11673 -- initialized type.
11675 while Present (Actual) loop
11676 exit when Ekind (Actual) = E_Package
11677 and then Present (Renamed_Object (Actual));
11679 if Chars (Actual) = Chars (Formal)
11680 and then not Is_Scalar_Type (Actual)
11681 and then not Is_Fully_Initialized_Type (Actual)
11682 and then Warn_On_No_Value_Assigned
11684 Error_Msg_Node_2 := Formal;
11686 ("generic unit has uninitialized variable& of "
11687 & "formal private type &?v?", Actual, Uninit_Var);
11689 ("actual type for& should be fully initialized type?v?",
11694 Next_Entity (Actual);
11700 end Check_Initialized_Types;
11704 -- The following constants capture the context prior to instantiating
11705 -- the package body.
11707 Saved_CS : constant Config_Switches_Type := Save_Config_Switches;
11708 Saved_GM : constant Ghost_Mode_Type := Ghost_Mode;
11709 Saved_IGR : constant Node_Id := Ignored_Ghost_Region;
11710 Saved_ISMP : constant Boolean :=
11711 Ignore_SPARK_Mode_Pragmas_In_Instance;
11712 Saved_LSST : constant Suppress_Stack_Entry_Ptr :=
11713 Local_Suppress_Stack_Top;
11714 Saved_SC : constant Boolean := Style_Check;
11715 Saved_SM : constant SPARK_Mode_Type := SPARK_Mode;
11716 Saved_SMP : constant Node_Id := SPARK_Mode_Pragma;
11717 Saved_SS : constant Suppress_Record := Scope_Suppress;
11718 Saved_Warn : constant Warning_Record := Save_Warnings;
11720 Act_Body : Node_Id;
11721 Act_Body_Id : Entity_Id;
11722 Act_Body_Name : Node_Id;
11723 Gen_Body : Node_Id;
11724 Gen_Body_Id : Node_Id;
11725 Par_Ent : Entity_Id := Empty;
11726 Par_Installed : Boolean := False;
11727 Par_Vis : Boolean := False;
11729 Vis_Prims_List : Elist_Id := No_Elist;
11730 -- List of primitives made temporarily visible in the instantiation
11731 -- to match the visibility of the formal type.
11733 -- Start of processing for Instantiate_Package_Body
11736 Gen_Body_Id := Corresponding_Body (Gen_Decl);
11738 -- The instance body may already have been processed, as the parent of
11739 -- another instance that is inlined (Load_Parent_Of_Generic).
11741 if Present (Corresponding_Body (Instance_Spec (Inst_Node))) then
11745 -- The package being instantiated may be subject to pragma Ghost. Set
11746 -- the mode now to ensure that any nodes generated during instantiation
11747 -- are properly marked as Ghost.
11749 Set_Ghost_Mode (Act_Decl_Id);
11751 Expander_Mode_Save_And_Set (Body_Info.Expander_Status);
11753 -- Re-establish the state of information on which checks are suppressed.
11754 -- This information was set in Body_Info at the point of instantiation,
11755 -- and now we restore it so that the instance is compiled using the
11756 -- check status at the instantiation (RM 11.5(7.2/2), AI95-00224-01).
11758 Local_Suppress_Stack_Top := Body_Info.Local_Suppress_Stack_Top;
11759 Scope_Suppress := Body_Info.Scope_Suppress;
11761 Restore_Config_Switches (Body_Info.Config_Switches);
11762 Restore_Warnings (Body_Info.Warnings);
11764 if No (Gen_Body_Id) then
11766 -- Do not look for parent of generic body if none is required.
11767 -- This may happen when the routine is called as part of the
11768 -- Pending_Instantiations processing, when nested instances
11769 -- may precede the one generated from the main unit.
11771 if not Unit_Requires_Body (Defining_Entity (Gen_Decl))
11772 and then Body_Optional
11776 Load_Parent_Of_Generic
11777 (Inst_Node, Specification (Gen_Decl), Body_Optional);
11779 -- Surprisingly enough, loading the body of the parent can cause
11780 -- the body to be instantiated and the double instantiation needs
11781 -- to be prevented in order to avoid giving bogus semantic errors.
11783 -- This case can occur because of the Collect_Previous_Instances
11784 -- machinery of Load_Parent_Of_Generic, which will instantiate
11785 -- bodies that are deemed to be ahead of the body of the parent
11786 -- in the compilation unit. But the relative position of these
11787 -- bodies is computed using the mere comparison of their Sloc.
11789 -- Now suppose that you have two generic packages G and H, with
11790 -- G containing a mere instantiation of H:
11796 -- package Nested_G is
11807 -- package My_H is new H;
11811 -- and a third package Q instantiating G and Nested_G:
11817 -- package My_G is new G;
11819 -- package My_Nested_G is new My_G.My_H.Nested_G;
11823 -- The body to be instantiated is that of My_Nested_G and its
11824 -- parent is the instance My_G.My_H. This latter instantiation
11825 -- is done when My_G is analyzed, i.e. after the declarations
11826 -- of My_G and My_Nested_G have been parsed; as a result, the
11827 -- Sloc of My_G.My_H is greater than the Sloc of My_Nested_G.
11829 -- Therefore loading the body of My_G.My_H will cause the body
11830 -- of My_Nested_G to be instantiated because it is deemed to be
11831 -- ahead of My_G.My_H. This means that Load_Parent_Of_Generic
11832 -- will again be invoked on My_G.My_H, but this time with the
11833 -- Collect_Previous_Instances machinery disabled, so there is
11834 -- no endless mutual recursion and things are done in order.
11836 if Present (Corresponding_Body (Instance_Spec (Inst_Node))) then
11840 Gen_Body_Id := Corresponding_Body (Gen_Decl);
11844 -- Establish global variable for sloc adjustment and for error recovery
11845 -- In the case of an instance body for an instantiation with actuals
11846 -- from a limited view, the instance body is placed at the beginning
11847 -- of the enclosing package body: use the body entity as the source
11848 -- location for nodes of the instance body.
11850 if not Is_Empty_Elmt_List (Incomplete_Actuals (Act_Decl_Id)) then
11852 Scop : constant Entity_Id := Scope (Act_Decl_Id);
11853 Body_Id : constant Node_Id :=
11854 Corresponding_Body (Unit_Declaration_Node (Scop));
11857 Instantiation_Node := Body_Id;
11860 Instantiation_Node := Inst_Node;
11863 if Present (Gen_Body_Id) then
11864 Save_Env (Gen_Unit, Act_Decl_Id);
11865 Style_Check := False;
11867 -- If the context of the instance is subject to SPARK_Mode "off", the
11868 -- annotation is missing, or the body is instantiated at a later pass
11869 -- and its spec ignored SPARK_Mode pragma, set the global flag which
11870 -- signals Analyze_Pragma to ignore all SPARK_Mode pragmas within the
11873 if SPARK_Mode /= On
11874 or else Ignore_SPARK_Mode_Pragmas (Act_Decl_Id)
11876 Ignore_SPARK_Mode_Pragmas_In_Instance := True;
11879 Current_Sem_Unit := Body_Info.Current_Sem_Unit;
11880 Gen_Body := Unit_Declaration_Node (Gen_Body_Id);
11882 Create_Instantiation_Source
11883 (Inst_Node, Gen_Body_Id, S_Adjustment);
11887 (Original_Node (Gen_Body), Empty, Instantiating => True);
11889 -- Create proper (possibly qualified) defining name for the body, to
11890 -- correspond to the one in the spec.
11893 Make_Defining_Identifier (Sloc (Act_Decl_Id), Chars (Act_Decl_Id));
11894 Set_Comes_From_Source (Act_Body_Id, Comes_From_Source (Act_Decl_Id));
11896 -- Some attributes of spec entity are not inherited by body entity
11898 Set_Handler_Records (Act_Body_Id, No_List);
11900 if Nkind (Defining_Unit_Name (Act_Spec)) =
11901 N_Defining_Program_Unit_Name
11904 Make_Defining_Program_Unit_Name (Loc,
11906 New_Copy_Tree (Name (Defining_Unit_Name (Act_Spec))),
11907 Defining_Identifier => Act_Body_Id);
11909 Act_Body_Name := Act_Body_Id;
11912 Set_Defining_Unit_Name (Act_Body, Act_Body_Name);
11914 Set_Corresponding_Spec (Act_Body, Act_Decl_Id);
11915 Check_Generic_Actuals (Act_Decl_Id, False);
11916 Check_Initialized_Types;
11918 -- Install primitives hidden at the point of the instantiation but
11919 -- visible when processing the generic formals
11925 E := First_Entity (Act_Decl_Id);
11926 while Present (E) loop
11928 and then not Is_Itype (E)
11929 and then Is_Generic_Actual_Type (E)
11930 and then Is_Tagged_Type (E)
11932 Install_Hidden_Primitives
11933 (Prims_List => Vis_Prims_List,
11934 Gen_T => Generic_Parent_Type (Parent (E)),
11942 -- If it is a child unit, make the parent instance (which is an
11943 -- instance of the parent of the generic) visible. The parent
11944 -- instance is the prefix of the name of the generic unit.
11946 if Ekind (Scope (Gen_Unit)) = E_Generic_Package
11947 and then Nkind (Gen_Id) = N_Expanded_Name
11949 Par_Ent := Entity (Prefix (Gen_Id));
11950 Par_Vis := Is_Immediately_Visible (Par_Ent);
11951 Install_Parent (Par_Ent, In_Body => True);
11952 Par_Installed := True;
11954 elsif Is_Child_Unit (Gen_Unit) then
11955 Par_Ent := Scope (Gen_Unit);
11956 Par_Vis := Is_Immediately_Visible (Par_Ent);
11957 Install_Parent (Par_Ent, In_Body => True);
11958 Par_Installed := True;
11961 -- If the instantiation is a library unit, and this is the main unit,
11962 -- then build the resulting compilation unit nodes for the instance.
11963 -- If this is a compilation unit but it is not the main unit, then it
11964 -- is the body of a unit in the context, that is being compiled
11965 -- because it is encloses some inlined unit or another generic unit
11966 -- being instantiated. In that case, this body is not part of the
11967 -- current compilation, and is not attached to the tree, but its
11968 -- parent must be set for analysis.
11970 if Nkind (Parent (Inst_Node)) = N_Compilation_Unit then
11972 -- Replace instance node with body of instance, and create new
11973 -- node for corresponding instance declaration.
11975 Build_Instance_Compilation_Unit_Nodes
11976 (Inst_Node, Act_Body, Act_Decl);
11977 Analyze (Inst_Node);
11979 if Parent (Inst_Node) = Cunit (Main_Unit) then
11981 -- If the instance is a child unit itself, then set the scope
11982 -- of the expanded body to be the parent of the instantiation
11983 -- (ensuring that the fully qualified name will be generated
11984 -- for the elaboration subprogram).
11986 if Nkind (Defining_Unit_Name (Act_Spec)) =
11987 N_Defining_Program_Unit_Name
11989 Set_Scope (Defining_Entity (Inst_Node), Scope (Act_Decl_Id));
11993 -- Case where instantiation is not a library unit
11996 -- If this is an early instantiation, i.e. appears textually
11997 -- before the corresponding body and must be elaborated first,
11998 -- indicate that the body instance is to be delayed.
12000 Install_Body (Act_Body, Inst_Node, Gen_Body, Gen_Decl);
12001 Analyze (Act_Body);
12004 Inherit_Context (Gen_Body, Inst_Node);
12006 -- Remove the parent instances if they have been placed on the scope
12007 -- stack to compile the body.
12009 if Par_Installed then
12010 Remove_Parent (In_Body => True);
12012 -- Restore the previous visibility of the parent
12014 Set_Is_Immediately_Visible (Par_Ent, Par_Vis);
12017 Restore_Hidden_Primitives (Vis_Prims_List);
12018 Restore_Private_Views (Act_Decl_Id);
12020 -- Remove the current unit from visibility if this is an instance
12021 -- that is not elaborated on the fly for inlining purposes.
12023 if not Inlined_Body then
12024 Set_Is_Immediately_Visible (Act_Decl_Id, False);
12029 -- If we have no body, and the unit requires a body, then complain. This
12030 -- complaint is suppressed if we have detected other errors (since a
12031 -- common reason for missing the body is that it had errors).
12032 -- In CodePeer mode, a warning has been emitted already, no need for
12033 -- further messages.
12035 elsif Unit_Requires_Body (Gen_Unit)
12036 and then not Body_Optional
12038 if CodePeer_Mode then
12041 elsif Serious_Errors_Detected = 0 then
12043 ("cannot find body of generic package &", Inst_Node, Gen_Unit);
12045 -- Don't attempt to perform any cleanup actions if some other error
12046 -- was already detected, since this can cause blowups.
12052 -- Case of package that does not need a body
12055 -- If the instantiation of the declaration is a library unit, rewrite
12056 -- the original package instantiation as a package declaration in the
12057 -- compilation unit node.
12059 if Nkind (Parent (Inst_Node)) = N_Compilation_Unit then
12060 Set_Parent_Spec (Act_Decl, Parent_Spec (Inst_Node));
12061 Rewrite (Inst_Node, Act_Decl);
12063 -- Generate elaboration entity, in case spec has elaboration code.
12064 -- This cannot be done when the instance is analyzed, because it
12065 -- is not known yet whether the body exists.
12067 Set_Elaboration_Entity_Required (Act_Decl_Id, False);
12068 Build_Elaboration_Entity (Parent (Inst_Node), Act_Decl_Id);
12070 -- If the instantiation is not a library unit, then append the
12071 -- declaration to the list of implicitly generated entities, unless
12072 -- it is already a list member which means that it was already
12075 elsif not Is_List_Member (Act_Decl) then
12076 Mark_Rewrite_Insertion (Act_Decl);
12077 Insert_Before (Inst_Node, Act_Decl);
12083 -- Restore the context that was in effect prior to instantiating the
12086 Ignore_SPARK_Mode_Pragmas_In_Instance := Saved_ISMP;
12087 Local_Suppress_Stack_Top := Saved_LSST;
12088 Scope_Suppress := Saved_SS;
12089 Style_Check := Saved_SC;
12091 Expander_Mode_Restore;
12092 Restore_Config_Switches (Saved_CS);
12093 Restore_Ghost_Region (Saved_GM, Saved_IGR);
12094 Restore_SPARK_Mode (Saved_SM, Saved_SMP);
12095 Restore_Warnings (Saved_Warn);
12096 end Instantiate_Package_Body;
12098 ---------------------------------
12099 -- Instantiate_Subprogram_Body --
12100 ---------------------------------
12102 -- WARNING: This routine manages Ghost and SPARK regions. Return statements
12103 -- must be replaced by gotos which jump to the end of the routine in order
12104 -- to restore the Ghost and SPARK modes.
12106 procedure Instantiate_Subprogram_Body
12107 (Body_Info : Pending_Body_Info;
12108 Body_Optional : Boolean := False)
12110 Act_Decl : constant Node_Id := Body_Info.Act_Decl;
12111 Act_Decl_Id : constant Entity_Id := Defining_Entity (Act_Decl);
12112 Inst_Node : constant Node_Id := Body_Info.Inst_Node;
12113 Gen_Id : constant Node_Id := Name (Inst_Node);
12114 Gen_Unit : constant Entity_Id := Get_Generic_Entity (Inst_Node);
12115 Gen_Decl : constant Node_Id := Unit_Declaration_Node (Gen_Unit);
12116 Loc : constant Source_Ptr := Sloc (Inst_Node);
12117 Pack_Id : constant Entity_Id :=
12118 Defining_Unit_Name (Parent (Act_Decl));
12120 -- The following constants capture the context prior to instantiating
12121 -- the subprogram body.
12123 Saved_CS : constant Config_Switches_Type := Save_Config_Switches;
12124 Saved_GM : constant Ghost_Mode_Type := Ghost_Mode;
12125 Saved_IGR : constant Node_Id := Ignored_Ghost_Region;
12126 Saved_ISMP : constant Boolean :=
12127 Ignore_SPARK_Mode_Pragmas_In_Instance;
12128 Saved_LSST : constant Suppress_Stack_Entry_Ptr :=
12129 Local_Suppress_Stack_Top;
12130 Saved_SC : constant Boolean := Style_Check;
12131 Saved_SM : constant SPARK_Mode_Type := SPARK_Mode;
12132 Saved_SMP : constant Node_Id := SPARK_Mode_Pragma;
12133 Saved_SS : constant Suppress_Record := Scope_Suppress;
12134 Saved_Warn : constant Warning_Record := Save_Warnings;
12136 Act_Body : Node_Id;
12137 Act_Body_Id : Entity_Id;
12138 Gen_Body : Node_Id;
12139 Gen_Body_Id : Node_Id;
12140 Pack_Body : Node_Id;
12141 Par_Ent : Entity_Id := Empty;
12142 Par_Installed : Boolean := False;
12143 Par_Vis : Boolean := False;
12144 Ret_Expr : Node_Id;
12147 Gen_Body_Id := Corresponding_Body (Gen_Decl);
12149 -- Subprogram body may have been created already because of an inline
12150 -- pragma, or because of multiple elaborations of the enclosing package
12151 -- when several instances of the subprogram appear in the main unit.
12153 if Present (Corresponding_Body (Act_Decl)) then
12157 -- The subprogram being instantiated may be subject to pragma Ghost. Set
12158 -- the mode now to ensure that any nodes generated during instantiation
12159 -- are properly marked as Ghost.
12161 Set_Ghost_Mode (Act_Decl_Id);
12163 Expander_Mode_Save_And_Set (Body_Info.Expander_Status);
12165 -- Re-establish the state of information on which checks are suppressed.
12166 -- This information was set in Body_Info at the point of instantiation,
12167 -- and now we restore it so that the instance is compiled using the
12168 -- check status at the instantiation (RM 11.5(7.2/2), AI95-00224-01).
12170 Local_Suppress_Stack_Top := Body_Info.Local_Suppress_Stack_Top;
12171 Scope_Suppress := Body_Info.Scope_Suppress;
12173 Restore_Config_Switches (Body_Info.Config_Switches);
12174 Restore_Warnings (Body_Info.Warnings);
12176 if No (Gen_Body_Id) then
12178 -- For imported generic subprogram, no body to compile, complete
12179 -- the spec entity appropriately.
12181 if Is_Imported (Gen_Unit) then
12182 Set_Is_Imported (Act_Decl_Id);
12183 Set_First_Rep_Item (Act_Decl_Id, First_Rep_Item (Gen_Unit));
12184 Set_Interface_Name (Act_Decl_Id, Interface_Name (Gen_Unit));
12185 Set_Convention (Act_Decl_Id, Convention (Gen_Unit));
12186 Set_Has_Completion (Act_Decl_Id);
12189 -- For other cases, compile the body
12192 Load_Parent_Of_Generic
12193 (Inst_Node, Specification (Gen_Decl), Body_Optional);
12194 Gen_Body_Id := Corresponding_Body (Gen_Decl);
12198 Instantiation_Node := Inst_Node;
12200 if Present (Gen_Body_Id) then
12201 Gen_Body := Unit_Declaration_Node (Gen_Body_Id);
12203 if Nkind (Gen_Body) = N_Subprogram_Body_Stub then
12205 -- Either body is not present, or context is non-expanding, as
12206 -- when compiling a subunit. Mark the instance as completed, and
12207 -- diagnose a missing body when needed.
12210 and then Operating_Mode = Generate_Code
12212 Error_Msg_N ("missing proper body for instantiation", Gen_Body);
12215 Set_Has_Completion (Act_Decl_Id);
12219 Save_Env (Gen_Unit, Act_Decl_Id);
12220 Style_Check := False;
12222 -- If the context of the instance is subject to SPARK_Mode "off", the
12223 -- annotation is missing, or the body is instantiated at a later pass
12224 -- and its spec ignored SPARK_Mode pragma, set the global flag which
12225 -- signals Analyze_Pragma to ignore all SPARK_Mode pragmas within the
12228 if SPARK_Mode /= On
12229 or else Ignore_SPARK_Mode_Pragmas (Act_Decl_Id)
12231 Ignore_SPARK_Mode_Pragmas_In_Instance := True;
12234 -- If the context of an instance is not subject to SPARK_Mode "off",
12235 -- and the generic body is subject to an explicit SPARK_Mode pragma,
12236 -- the latter should be the one applicable to the instance.
12238 if not Ignore_SPARK_Mode_Pragmas_In_Instance
12239 and then SPARK_Mode /= Off
12240 and then Present (SPARK_Pragma (Gen_Body_Id))
12242 Set_SPARK_Mode (Gen_Body_Id);
12245 Current_Sem_Unit := Body_Info.Current_Sem_Unit;
12246 Create_Instantiation_Source
12253 (Original_Node (Gen_Body), Empty, Instantiating => True);
12255 -- Create proper defining name for the body, to correspond to the one
12259 Make_Defining_Identifier (Sloc (Act_Decl_Id), Chars (Act_Decl_Id));
12261 Set_Comes_From_Source (Act_Body_Id, Comes_From_Source (Act_Decl_Id));
12262 Set_Defining_Unit_Name (Specification (Act_Body), Act_Body_Id);
12264 Set_Corresponding_Spec (Act_Body, Act_Decl_Id);
12265 Set_Has_Completion (Act_Decl_Id);
12266 Check_Generic_Actuals (Pack_Id, False);
12268 -- Generate a reference to link the visible subprogram instance to
12269 -- the generic body, which for navigation purposes is the only
12270 -- available source for the instance.
12273 (Related_Instance (Pack_Id),
12274 Gen_Body_Id, 'b', Set_Ref => False, Force => True);
12276 -- If it is a child unit, make the parent instance (which is an
12277 -- instance of the parent of the generic) visible. The parent
12278 -- instance is the prefix of the name of the generic unit.
12280 if Ekind (Scope (Gen_Unit)) = E_Generic_Package
12281 and then Nkind (Gen_Id) = N_Expanded_Name
12283 Par_Ent := Entity (Prefix (Gen_Id));
12284 Par_Vis := Is_Immediately_Visible (Par_Ent);
12285 Install_Parent (Par_Ent, In_Body => True);
12286 Par_Installed := True;
12288 elsif Is_Child_Unit (Gen_Unit) then
12289 Par_Ent := Scope (Gen_Unit);
12290 Par_Vis := Is_Immediately_Visible (Par_Ent);
12291 Install_Parent (Par_Ent, In_Body => True);
12292 Par_Installed := True;
12295 -- Subprogram body is placed in the body of wrapper package,
12296 -- whose spec contains the subprogram declaration as well as
12297 -- the renaming declarations for the generic parameters.
12300 Make_Package_Body (Loc,
12301 Defining_Unit_Name => New_Copy (Pack_Id),
12302 Declarations => New_List (Act_Body));
12304 Set_Corresponding_Spec (Pack_Body, Pack_Id);
12306 -- If the instantiation is a library unit, then build resulting
12307 -- compilation unit nodes for the instance. The declaration of
12308 -- the enclosing package is the grandparent of the subprogram
12309 -- declaration. First replace the instantiation node as the unit
12310 -- of the corresponding compilation.
12312 if Nkind (Parent (Inst_Node)) = N_Compilation_Unit then
12313 if Parent (Inst_Node) = Cunit (Main_Unit) then
12314 Set_Unit (Parent (Inst_Node), Inst_Node);
12315 Build_Instance_Compilation_Unit_Nodes
12316 (Inst_Node, Pack_Body, Parent (Parent (Act_Decl)));
12317 Analyze (Inst_Node);
12319 Set_Parent (Pack_Body, Parent (Inst_Node));
12320 Analyze (Pack_Body);
12324 Insert_Before (Inst_Node, Pack_Body);
12325 Mark_Rewrite_Insertion (Pack_Body);
12326 Analyze (Pack_Body);
12328 if Expander_Active then
12329 Freeze_Subprogram_Body (Inst_Node, Gen_Body, Pack_Id);
12333 Inherit_Context (Gen_Body, Inst_Node);
12335 Restore_Private_Views (Pack_Id, False);
12337 if Par_Installed then
12338 Remove_Parent (In_Body => True);
12340 -- Restore the previous visibility of the parent
12342 Set_Is_Immediately_Visible (Par_Ent, Par_Vis);
12347 -- Body not found. Error was emitted already. If there were no previous
12348 -- errors, this may be an instance whose scope is a premature instance.
12349 -- In that case we must insure that the (legal) program does raise
12350 -- program error if executed. We generate a subprogram body for this
12351 -- purpose. See DEC ac30vso.
12353 -- Should not reference proprietary DEC tests in comments ???
12355 elsif Serious_Errors_Detected = 0
12356 and then Nkind (Parent (Inst_Node)) /= N_Compilation_Unit
12358 if Body_Optional then
12361 elsif Ekind (Act_Decl_Id) = E_Procedure then
12363 Make_Subprogram_Body (Loc,
12365 Make_Procedure_Specification (Loc,
12366 Defining_Unit_Name =>
12367 Make_Defining_Identifier (Loc, Chars (Act_Decl_Id)),
12368 Parameter_Specifications =>
12370 (Parameter_Specifications (Parent (Act_Decl_Id)))),
12372 Declarations => Empty_List,
12373 Handled_Statement_Sequence =>
12374 Make_Handled_Sequence_Of_Statements (Loc,
12375 Statements => New_List (
12376 Make_Raise_Program_Error (Loc,
12377 Reason => PE_Access_Before_Elaboration))));
12381 Make_Raise_Program_Error (Loc,
12382 Reason => PE_Access_Before_Elaboration);
12384 Set_Etype (Ret_Expr, (Etype (Act_Decl_Id)));
12385 Set_Analyzed (Ret_Expr);
12388 Make_Subprogram_Body (Loc,
12390 Make_Function_Specification (Loc,
12391 Defining_Unit_Name =>
12392 Make_Defining_Identifier (Loc, Chars (Act_Decl_Id)),
12393 Parameter_Specifications =>
12395 (Parameter_Specifications (Parent (Act_Decl_Id))),
12396 Result_Definition =>
12397 New_Occurrence_Of (Etype (Act_Decl_Id), Loc)),
12399 Declarations => Empty_List,
12400 Handled_Statement_Sequence =>
12401 Make_Handled_Sequence_Of_Statements (Loc,
12402 Statements => New_List (
12403 Make_Simple_Return_Statement (Loc, Ret_Expr))));
12407 Make_Package_Body (Loc,
12408 Defining_Unit_Name => New_Copy (Pack_Id),
12409 Declarations => New_List (Act_Body));
12411 Insert_After (Inst_Node, Pack_Body);
12412 Set_Corresponding_Spec (Pack_Body, Pack_Id);
12413 Analyze (Pack_Body);
12418 -- Restore the context that was in effect prior to instantiating the
12419 -- subprogram body.
12421 Ignore_SPARK_Mode_Pragmas_In_Instance := Saved_ISMP;
12422 Local_Suppress_Stack_Top := Saved_LSST;
12423 Scope_Suppress := Saved_SS;
12424 Style_Check := Saved_SC;
12426 Expander_Mode_Restore;
12427 Restore_Config_Switches (Saved_CS);
12428 Restore_Ghost_Region (Saved_GM, Saved_IGR);
12429 Restore_SPARK_Mode (Saved_SM, Saved_SMP);
12430 Restore_Warnings (Saved_Warn);
12431 end Instantiate_Subprogram_Body;
12433 ----------------------
12434 -- Instantiate_Type --
12435 ----------------------
12437 function Instantiate_Type
12440 Analyzed_Formal : Node_Id;
12441 Actual_Decls : List_Id) return List_Id
12443 A_Gen_T : constant Entity_Id :=
12444 Defining_Identifier (Analyzed_Formal);
12445 Def : constant Node_Id := Formal_Type_Definition (Formal);
12446 Gen_T : constant Entity_Id := Defining_Identifier (Formal);
12448 Ancestor : Entity_Id := Empty;
12449 Decl_Node : Node_Id;
12450 Decl_Nodes : List_Id;
12454 procedure Check_Shared_Variable_Control_Aspects;
12455 -- Ada 2020: Verify that shared variable control aspects (RM C.6)
12456 -- that may be specified for a formal type are obeyed by the actual.
12458 procedure Diagnose_Predicated_Actual;
12459 -- There are a number of constructs in which a discrete type with
12460 -- predicates is illegal, e.g. as an index in an array type declaration.
12461 -- If a generic type is used is such a construct in a generic package
12462 -- declaration, it carries the flag No_Predicate_On_Actual. it is part
12463 -- of the generic contract that the actual cannot have predicates.
12465 procedure Validate_Array_Type_Instance;
12466 procedure Validate_Access_Subprogram_Instance;
12467 procedure Validate_Access_Type_Instance;
12468 procedure Validate_Derived_Type_Instance;
12469 procedure Validate_Derived_Interface_Type_Instance;
12470 procedure Validate_Discriminated_Formal_Type;
12471 procedure Validate_Interface_Type_Instance;
12472 procedure Validate_Private_Type_Instance;
12473 procedure Validate_Incomplete_Type_Instance;
12474 -- These procedures perform validation tests for the named case.
12475 -- Validate_Discriminated_Formal_Type is shared by formal private
12476 -- types and Ada 2012 formal incomplete types.
12478 function Subtypes_Match (Gen_T, Act_T : Entity_Id) return Boolean;
12479 -- Check that base types are the same and that the subtypes match
12480 -- statically. Used in several of the above.
12482 --------------------------------------------
12483 -- Check_Shared_Variable_Control_Aspects --
12484 --------------------------------------------
12486 -- Ada 2020: Verify that shared variable control aspects (RM C.6)
12487 -- that may be specified for the formal are obeyed by the actual.
12488 -- If the formal is a derived type the aspect specifications must match.
12489 -- NOTE: AI12-0282 implies that matching of aspects is required between
12490 -- formal and actual in all cases, but this is too restrictive.
12491 -- In particular it violates a language design rule: a limited private
12492 -- indefinite formal can be matched by any actual. The current code
12493 -- reflects an older and more permissive version of RM C.6 (12/5).
12495 procedure Check_Shared_Variable_Control_Aspects is
12497 if Ada_Version >= Ada_2020 then
12498 if Is_Atomic (A_Gen_T) and then not Is_Atomic (Act_T) then
12500 ("actual for& must have Atomic aspect", Actual, A_Gen_T);
12502 elsif Is_Derived_Type (A_Gen_T)
12503 and then Is_Atomic (A_Gen_T) /= Is_Atomic (Act_T)
12506 ("actual for& has different Atomic aspect", Actual, A_Gen_T);
12509 if Is_Volatile (A_Gen_T) and then not Is_Volatile (Act_T) then
12511 ("actual for& has different Volatile aspect",
12514 elsif Is_Derived_Type (A_Gen_T)
12515 and then Is_Volatile (A_Gen_T) /= Is_Volatile (Act_T)
12518 ("actual for& has different Volatile aspect",
12522 -- We assume that an array type whose atomic component type
12523 -- is Atomic is equivalent to an array type with the explicit
12524 -- aspect Has_Atomic_Components. This is a reasonable inference
12525 -- from the intent of AI12-0282, and makes it legal to use an
12526 -- actual that does not have the identical aspect as the formal.
12527 -- Ditto for volatile components.
12530 Actual_Atomic_Comp : constant Boolean :=
12531 Has_Atomic_Components (Act_T)
12532 or else (Is_Array_Type (Act_T)
12533 and then Is_Atomic (Component_Type (Act_T)));
12535 if Has_Atomic_Components (A_Gen_T) /= Actual_Atomic_Comp then
12537 ("formal and actual for& must agree on atomic components",
12543 Actual_Volatile_Comp : constant Boolean :=
12544 Has_Volatile_Components (Act_T)
12545 or else (Is_Array_Type (Act_T)
12546 and then Is_Volatile (Component_Type (Act_T)));
12548 if Has_Volatile_Components (A_Gen_T) /= Actual_Volatile_Comp
12551 ("actual for& must have volatile components",
12556 -- The following two aspects do not require exact matching,
12557 -- but only one-way agreement. See RM C.6.
12559 if Is_Independent (A_Gen_T) and then not Is_Independent (Act_T)
12562 ("actual for& must have Independent aspect specified",
12566 if Has_Independent_Components (A_Gen_T)
12567 and then not Has_Independent_Components (Act_T)
12570 ("actual for& must have Independent_Components specified",
12574 -- Check actual/formal compatibility with respect to the four
12575 -- volatility refinement aspects.
12577 Check_Volatility_Compatibility
12579 "actual type", "its corresponding formal type",
12580 Srcpos_Bearer => Act_T);
12582 end Check_Shared_Variable_Control_Aspects;
12584 ---------------------------------
12585 -- Diagnose_Predicated_Actual --
12586 ---------------------------------
12588 procedure Diagnose_Predicated_Actual is
12590 if No_Predicate_On_Actual (A_Gen_T)
12591 and then Has_Predicates (Act_T)
12594 ("actual for& cannot be a type with predicate",
12595 Instantiation_Node, A_Gen_T);
12597 elsif No_Dynamic_Predicate_On_Actual (A_Gen_T)
12598 and then Has_Predicates (Act_T)
12599 and then not Has_Static_Predicate_Aspect (Act_T)
12602 ("actual for& cannot be a type with a dynamic predicate",
12603 Instantiation_Node, A_Gen_T);
12605 end Diagnose_Predicated_Actual;
12607 --------------------
12608 -- Subtypes_Match --
12609 --------------------
12611 function Subtypes_Match (Gen_T, Act_T : Entity_Id) return Boolean is
12612 T : constant Entity_Id := Get_Instance_Of (Gen_T);
12615 -- Some detailed comments would be useful here ???
12617 return ((Base_Type (T) = Act_T
12618 or else Base_Type (T) = Base_Type (Act_T))
12619 and then Subtypes_Statically_Match (T, Act_T))
12621 or else (Is_Class_Wide_Type (Gen_T)
12622 and then Is_Class_Wide_Type (Act_T)
12623 and then Subtypes_Match
12624 (Get_Instance_Of (Root_Type (Gen_T)),
12625 Root_Type (Act_T)))
12628 (Ekind_In (Gen_T, E_Anonymous_Access_Subprogram_Type,
12629 E_Anonymous_Access_Type)
12630 and then Ekind (Act_T) = Ekind (Gen_T)
12631 and then Subtypes_Statically_Match
12632 (Designated_Type (Gen_T), Designated_Type (Act_T)));
12633 end Subtypes_Match;
12635 -----------------------------------------
12636 -- Validate_Access_Subprogram_Instance --
12637 -----------------------------------------
12639 procedure Validate_Access_Subprogram_Instance is
12641 if not Is_Access_Type (Act_T)
12642 or else Ekind (Designated_Type (Act_T)) /= E_Subprogram_Type
12645 ("expect access type in instantiation of &", Actual, Gen_T);
12646 Abandon_Instantiation (Actual);
12649 -- According to AI05-288, actuals for access_to_subprograms must be
12650 -- subtype conformant with the generic formal. Previous to AI05-288
12651 -- only mode conformance was required.
12653 -- This is a binding interpretation that applies to previous versions
12654 -- of the language, no need to maintain previous weaker checks.
12656 Check_Subtype_Conformant
12657 (Designated_Type (Act_T),
12658 Designated_Type (A_Gen_T),
12662 if Ekind (Base_Type (Act_T)) = E_Access_Protected_Subprogram_Type then
12663 if Ekind (A_Gen_T) = E_Access_Subprogram_Type then
12665 ("protected access type not allowed for formal &",
12669 elsif Ekind (A_Gen_T) = E_Access_Protected_Subprogram_Type then
12671 ("expect protected access type for formal &",
12675 -- If the formal has a specified convention (which in most cases
12676 -- will be StdCall) verify that the actual has the same convention.
12678 if Has_Convention_Pragma (A_Gen_T)
12679 and then Convention (A_Gen_T) /= Convention (Act_T)
12681 Error_Msg_Name_1 := Get_Convention_Name (Convention (A_Gen_T));
12683 ("actual for formal & must have convention %", Actual, Gen_T);
12686 if Can_Never_Be_Null (A_Gen_T) /= Can_Never_Be_Null (Act_T) then
12688 ("non null exclusion of actual and formal & do not match",
12691 end Validate_Access_Subprogram_Instance;
12693 -----------------------------------
12694 -- Validate_Access_Type_Instance --
12695 -----------------------------------
12697 procedure Validate_Access_Type_Instance is
12698 Desig_Type : constant Entity_Id :=
12699 Find_Actual_Type (Designated_Type (A_Gen_T), A_Gen_T);
12700 Desig_Act : Entity_Id;
12703 if not Is_Access_Type (Act_T) then
12705 ("expect access type in instantiation of &", Actual, Gen_T);
12706 Abandon_Instantiation (Actual);
12709 if Is_Access_Constant (A_Gen_T) then
12710 if not Is_Access_Constant (Act_T) then
12712 ("actual type must be access-to-constant type", Actual);
12713 Abandon_Instantiation (Actual);
12716 if Is_Access_Constant (Act_T) then
12718 ("actual type must be access-to-variable type", Actual);
12719 Abandon_Instantiation (Actual);
12721 elsif Ekind (A_Gen_T) = E_General_Access_Type
12722 and then Ekind (Base_Type (Act_T)) /= E_General_Access_Type
12724 Error_Msg_N -- CODEFIX
12725 ("actual must be general access type!", Actual);
12726 Error_Msg_NE -- CODEFIX
12727 ("add ALL to }!", Actual, Act_T);
12728 Abandon_Instantiation (Actual);
12732 -- The designated subtypes, that is to say the subtypes introduced
12733 -- by an access type declaration (and not by a subtype declaration)
12736 Desig_Act := Designated_Type (Base_Type (Act_T));
12738 -- The designated type may have been introduced through a limited_
12739 -- with clause, in which case retrieve the non-limited view. This
12740 -- applies to incomplete types as well as to class-wide types.
12742 if From_Limited_With (Desig_Act) then
12743 Desig_Act := Available_View (Desig_Act);
12746 if not Subtypes_Match (Desig_Type, Desig_Act) then
12748 ("designated type of actual does not match that of formal &",
12751 if not Predicates_Match (Desig_Type, Desig_Act) then
12752 Error_Msg_N ("\predicates do not match", Actual);
12755 Abandon_Instantiation (Actual);
12757 elsif Is_Access_Type (Designated_Type (Act_T))
12758 and then Is_Constrained (Designated_Type (Designated_Type (Act_T)))
12760 Is_Constrained (Designated_Type (Desig_Type))
12763 ("designated type of actual does not match that of formal &",
12766 if not Predicates_Match (Desig_Type, Desig_Act) then
12767 Error_Msg_N ("\predicates do not match", Actual);
12770 Abandon_Instantiation (Actual);
12773 -- Ada 2005: null-exclusion indicators of the two types must agree
12775 if Can_Never_Be_Null (A_Gen_T) /= Can_Never_Be_Null (Act_T) then
12777 ("non null exclusion of actual and formal & do not match",
12780 end Validate_Access_Type_Instance;
12782 ----------------------------------
12783 -- Validate_Array_Type_Instance --
12784 ----------------------------------
12786 procedure Validate_Array_Type_Instance is
12791 function Formal_Dimensions return Nat;
12792 -- Count number of dimensions in array type formal
12794 -----------------------
12795 -- Formal_Dimensions --
12796 -----------------------
12798 function Formal_Dimensions return Nat is
12803 if Nkind (Def) = N_Constrained_Array_Definition then
12804 Index := First (Discrete_Subtype_Definitions (Def));
12806 Index := First (Subtype_Marks (Def));
12809 while Present (Index) loop
12811 Next_Index (Index);
12815 end Formal_Dimensions;
12817 -- Start of processing for Validate_Array_Type_Instance
12820 if not Is_Array_Type (Act_T) then
12822 ("expect array type in instantiation of &", Actual, Gen_T);
12823 Abandon_Instantiation (Actual);
12825 elsif Nkind (Def) = N_Constrained_Array_Definition then
12826 if not (Is_Constrained (Act_T)) then
12828 ("expect constrained array in instantiation of &",
12830 Abandon_Instantiation (Actual);
12834 if Is_Constrained (Act_T) then
12836 ("expect unconstrained array in instantiation of &",
12838 Abandon_Instantiation (Actual);
12842 if Formal_Dimensions /= Number_Dimensions (Act_T) then
12844 ("dimensions of actual do not match formal &", Actual, Gen_T);
12845 Abandon_Instantiation (Actual);
12848 I1 := First_Index (A_Gen_T);
12849 I2 := First_Index (Act_T);
12850 for J in 1 .. Formal_Dimensions loop
12852 -- If the indexes of the actual were given by a subtype_mark,
12853 -- the index was transformed into a range attribute. Retrieve
12854 -- the original type mark for checking.
12856 if Is_Entity_Name (Original_Node (I2)) then
12857 T2 := Entity (Original_Node (I2));
12862 if not Subtypes_Match
12863 (Find_Actual_Type (Etype (I1), A_Gen_T), T2)
12866 ("index types of actual do not match those of formal &",
12868 Abandon_Instantiation (Actual);
12875 -- Check matching subtypes. Note that there are complex visibility
12876 -- issues when the generic is a child unit and some aspect of the
12877 -- generic type is declared in a parent unit of the generic. We do
12878 -- the test to handle this special case only after a direct check
12879 -- for static matching has failed. The case where both the component
12880 -- type and the array type are separate formals, and the component
12881 -- type is a private view may also require special checking in
12882 -- Subtypes_Match. Finally, we assume that a child instance where
12883 -- the component type comes from a formal of a parent instance is
12884 -- correct because the generic was correct. A more precise check
12885 -- seems too complex to install???
12888 (Component_Type (A_Gen_T), Component_Type (Act_T))
12891 (Find_Actual_Type (Component_Type (A_Gen_T), A_Gen_T),
12892 Component_Type (Act_T))
12894 (not Inside_A_Generic
12895 and then Is_Child_Unit (Scope (Component_Type (A_Gen_T))))
12900 ("component subtype of actual does not match that of formal &",
12902 Abandon_Instantiation (Actual);
12905 if Has_Aliased_Components (A_Gen_T)
12906 and then not Has_Aliased_Components (Act_T)
12909 ("actual must have aliased components to match formal type &",
12912 end Validate_Array_Type_Instance;
12914 -----------------------------------------------
12915 -- Validate_Derived_Interface_Type_Instance --
12916 -----------------------------------------------
12918 procedure Validate_Derived_Interface_Type_Instance is
12919 Par : constant Entity_Id := Entity (Subtype_Indication (Def));
12923 -- First apply interface instance checks
12925 Validate_Interface_Type_Instance;
12927 -- Verify that immediate parent interface is an ancestor of
12931 and then not Interface_Present_In_Ancestor (Act_T, Par)
12934 ("interface actual must include progenitor&", Actual, Par);
12937 -- Now verify that the actual includes all other ancestors of
12940 Elmt := First_Elmt (Interfaces (A_Gen_T));
12941 while Present (Elmt) loop
12942 if not Interface_Present_In_Ancestor
12943 (Act_T, Get_Instance_Of (Node (Elmt)))
12946 ("interface actual must include progenitor&",
12947 Actual, Node (Elmt));
12952 end Validate_Derived_Interface_Type_Instance;
12954 ------------------------------------
12955 -- Validate_Derived_Type_Instance --
12956 ------------------------------------
12958 procedure Validate_Derived_Type_Instance is
12959 Actual_Discr : Entity_Id;
12960 Ancestor_Discr : Entity_Id;
12963 -- Verify that the actual includes the progenitors of the formal,
12964 -- if any. The formal may depend on previous formals and their
12965 -- instance, so we must examine instance of interfaces if present.
12966 -- The actual may be an extension of an interface, in which case
12967 -- it does not appear in the interface list, so this must be
12968 -- checked separately.
12970 if Present (Interface_List (Def)) then
12971 if not Has_Interfaces (Act_T) then
12973 ("actual must implement all interfaces of formal&",
12978 Act_Iface_List : Elist_Id;
12980 Iface_Ent : Entity_Id;
12982 function Instance_Exists (I : Entity_Id) return Boolean;
12983 -- If the interface entity is declared in a generic unit,
12984 -- this can only be legal if we are within an instantiation
12985 -- of a child of that generic. There is currently no
12986 -- mechanism to relate an interface declared within a
12987 -- generic to the corresponding interface in an instance,
12988 -- so we traverse the list of interfaces of the actual,
12989 -- looking for a name match.
12991 ---------------------
12992 -- Instance_Exists --
12993 ---------------------
12995 function Instance_Exists (I : Entity_Id) return Boolean is
12996 Iface_Elmt : Elmt_Id;
12999 Iface_Elmt := First_Elmt (Act_Iface_List);
13000 while Present (Iface_Elmt) loop
13001 if Is_Generic_Instance (Scope (Node (Iface_Elmt)))
13002 and then Chars (Node (Iface_Elmt)) = Chars (I)
13007 Next_Elmt (Iface_Elmt);
13011 end Instance_Exists;
13014 Iface := First (Abstract_Interface_List (A_Gen_T));
13015 Collect_Interfaces (Act_T, Act_Iface_List);
13017 while Present (Iface) loop
13018 Iface_Ent := Get_Instance_Of (Entity (Iface));
13020 if Is_Ancestor (Iface_Ent, Act_T)
13021 or else Is_Progenitor (Iface_Ent, Act_T)
13025 elsif Ekind (Scope (Iface_Ent)) = E_Generic_Package
13026 and then Instance_Exists (Iface_Ent)
13031 Error_Msg_Name_1 := Chars (Act_T);
13033 ("Actual% must implement interface&",
13034 Actual, Etype (Iface));
13043 -- If the parent type in the generic declaration is itself a previous
13044 -- formal type, then it is local to the generic and absent from the
13045 -- analyzed generic definition. In that case the ancestor is the
13046 -- instance of the formal (which must have been instantiated
13047 -- previously), unless the ancestor is itself a formal derived type.
13048 -- In this latter case (which is the subject of Corrigendum 8652/0038
13049 -- (AI-202) the ancestor of the formals is the ancestor of its
13050 -- parent. Otherwise, the analyzed generic carries the parent type.
13051 -- If the parent type is defined in a previous formal package, then
13052 -- the scope of that formal package is that of the generic type
13053 -- itself, and it has already been mapped into the corresponding type
13054 -- in the actual package.
13056 -- Common case: parent type defined outside of the generic
13058 if Is_Entity_Name (Subtype_Mark (Def))
13059 and then Present (Entity (Subtype_Mark (Def)))
13061 Ancestor := Get_Instance_Of (Entity (Subtype_Mark (Def)));
13063 -- Check whether parent is defined in a previous formal package
13066 Scope (Scope (Base_Type (Etype (A_Gen_T)))) = Scope (A_Gen_T)
13069 Get_Instance_Of (Base_Type (Etype (A_Gen_T)));
13071 -- The type may be a local derivation, or a type extension of a
13072 -- previous formal, or of a formal of a parent package.
13074 elsif Is_Derived_Type (Get_Instance_Of (A_Gen_T))
13076 Ekind (Get_Instance_Of (A_Gen_T)) = E_Record_Type_With_Private
13078 -- Check whether the parent is another derived formal type in the
13079 -- same generic unit.
13081 if Etype (A_Gen_T) /= A_Gen_T
13082 and then Is_Generic_Type (Etype (A_Gen_T))
13083 and then Scope (Etype (A_Gen_T)) = Scope (A_Gen_T)
13084 and then Etype (Etype (A_Gen_T)) /= Etype (A_Gen_T)
13086 -- Locate ancestor of parent from the subtype declaration
13087 -- created for the actual.
13093 Decl := First (Actual_Decls);
13094 while Present (Decl) loop
13095 if Nkind (Decl) = N_Subtype_Declaration
13096 and then Chars (Defining_Identifier (Decl)) =
13097 Chars (Etype (A_Gen_T))
13099 Ancestor := Generic_Parent_Type (Decl);
13107 pragma Assert (Present (Ancestor));
13109 -- The ancestor itself may be a previous formal that has been
13112 Ancestor := Get_Instance_Of (Ancestor);
13116 Get_Instance_Of (Base_Type (Get_Instance_Of (A_Gen_T)));
13119 -- Check whether parent is a previous formal of the current generic
13121 elsif Is_Derived_Type (A_Gen_T)
13122 and then Is_Generic_Type (Etype (A_Gen_T))
13123 and then Scope (A_Gen_T) = Scope (Etype (A_Gen_T))
13125 Ancestor := Get_Instance_Of (First_Subtype (Etype (A_Gen_T)));
13127 -- An unusual case: the actual is a type declared in a parent unit,
13128 -- but is not a formal type so there is no instance_of for it.
13129 -- Retrieve it by analyzing the record extension.
13131 elsif Is_Child_Unit (Scope (A_Gen_T))
13132 and then In_Open_Scopes (Scope (Act_T))
13133 and then Is_Generic_Instance (Scope (Act_T))
13135 Analyze (Subtype_Mark (Def));
13136 Ancestor := Entity (Subtype_Mark (Def));
13139 Ancestor := Get_Instance_Of (Etype (Base_Type (A_Gen_T)));
13142 -- If the formal derived type has pragma Preelaborable_Initialization
13143 -- then the actual type must have preelaborable initialization.
13145 if Known_To_Have_Preelab_Init (A_Gen_T)
13146 and then not Has_Preelaborable_Initialization (Act_T)
13149 ("actual for & must have preelaborable initialization",
13153 -- Ada 2005 (AI-251)
13155 if Ada_Version >= Ada_2005 and then Is_Interface (Ancestor) then
13156 if not Interface_Present_In_Ancestor (Act_T, Ancestor) then
13158 ("(Ada 2005) expected type implementing & in instantiation",
13162 -- Finally verify that the (instance of) the ancestor is an ancestor
13165 elsif not Is_Ancestor (Base_Type (Ancestor), Act_T) then
13167 ("expect type derived from & in instantiation",
13168 Actual, First_Subtype (Ancestor));
13169 Abandon_Instantiation (Actual);
13172 -- Ada 2005 (AI-443): Synchronized formal derived type checks. Note
13173 -- that the formal type declaration has been rewritten as a private
13176 if Ada_Version >= Ada_2005
13177 and then Nkind (Parent (A_Gen_T)) = N_Private_Extension_Declaration
13178 and then Synchronized_Present (Parent (A_Gen_T))
13180 -- The actual must be a synchronized tagged type
13182 if not Is_Tagged_Type (Act_T) then
13184 ("actual of synchronized type must be tagged", Actual);
13185 Abandon_Instantiation (Actual);
13187 elsif Nkind (Parent (Act_T)) = N_Full_Type_Declaration
13188 and then Nkind (Type_Definition (Parent (Act_T))) =
13189 N_Derived_Type_Definition
13190 and then not Synchronized_Present
13191 (Type_Definition (Parent (Act_T)))
13194 ("actual of synchronized type must be synchronized", Actual);
13195 Abandon_Instantiation (Actual);
13199 -- Perform atomic/volatile checks (RM C.6(12)). Note that AI05-0218-1
13200 -- removes the second instance of the phrase "or allow pass by copy".
13202 -- For Ada 2020, the aspect may be specified explicitly for the
13203 -- formal regardless of whether an ancestor obeys it.
13205 if Is_Atomic (Act_T)
13206 and then not Is_Atomic (Ancestor)
13207 and then not Is_Atomic (A_Gen_T)
13210 ("cannot have atomic actual type for non-atomic formal type",
13213 elsif Is_Volatile (Act_T)
13214 and then not Is_Volatile (Ancestor)
13215 and then not Is_Volatile (A_Gen_T)
13218 ("cannot have volatile actual type for non-volatile formal type",
13222 -- It should not be necessary to check for unknown discriminants on
13223 -- Formal, but for some reason Has_Unknown_Discriminants is false for
13224 -- A_Gen_T, so Is_Definite_Subtype incorrectly returns True. This
13225 -- needs fixing. ???
13227 if Is_Definite_Subtype (A_Gen_T)
13228 and then not Unknown_Discriminants_Present (Formal)
13229 and then not Is_Definite_Subtype (Act_T)
13231 Error_Msg_N ("actual subtype must be constrained", Actual);
13232 Abandon_Instantiation (Actual);
13235 if not Unknown_Discriminants_Present (Formal) then
13236 if Is_Constrained (Ancestor) then
13237 if not Is_Constrained (Act_T) then
13238 Error_Msg_N ("actual subtype must be constrained", Actual);
13239 Abandon_Instantiation (Actual);
13242 -- Ancestor is unconstrained, Check if generic formal and actual
13243 -- agree on constrainedness. The check only applies to array types
13244 -- and discriminated types.
13246 elsif Is_Constrained (Act_T) then
13247 if Ekind (Ancestor) = E_Access_Type
13248 or else (not Is_Constrained (A_Gen_T)
13249 and then Is_Composite_Type (A_Gen_T))
13251 Error_Msg_N ("actual subtype must be unconstrained", Actual);
13252 Abandon_Instantiation (Actual);
13255 -- A class-wide type is only allowed if the formal has unknown
13258 elsif Is_Class_Wide_Type (Act_T)
13259 and then not Has_Unknown_Discriminants (Ancestor)
13262 ("actual for & cannot be a class-wide type", Actual, Gen_T);
13263 Abandon_Instantiation (Actual);
13265 -- Otherwise, the formal and actual must have the same number
13266 -- of discriminants and each discriminant of the actual must
13267 -- correspond to a discriminant of the formal.
13269 elsif Has_Discriminants (Act_T)
13270 and then not Has_Unknown_Discriminants (Act_T)
13271 and then Has_Discriminants (Ancestor)
13273 Actual_Discr := First_Discriminant (Act_T);
13274 Ancestor_Discr := First_Discriminant (Ancestor);
13275 while Present (Actual_Discr)
13276 and then Present (Ancestor_Discr)
13278 if Base_Type (Act_T) /= Base_Type (Ancestor) and then
13279 No (Corresponding_Discriminant (Actual_Discr))
13282 ("discriminant & does not correspond "
13283 & "to ancestor discriminant", Actual, Actual_Discr);
13284 Abandon_Instantiation (Actual);
13287 Next_Discriminant (Actual_Discr);
13288 Next_Discriminant (Ancestor_Discr);
13291 if Present (Actual_Discr) or else Present (Ancestor_Discr) then
13293 ("actual for & must have same number of discriminants",
13295 Abandon_Instantiation (Actual);
13298 -- This case should be caught by the earlier check for
13299 -- constrainedness, but the check here is added for completeness.
13301 elsif Has_Discriminants (Act_T)
13302 and then not Has_Unknown_Discriminants (Act_T)
13305 ("actual for & must not have discriminants", Actual, Gen_T);
13306 Abandon_Instantiation (Actual);
13308 elsif Has_Discriminants (Ancestor) then
13310 ("actual for & must have known discriminants", Actual, Gen_T);
13311 Abandon_Instantiation (Actual);
13314 if not Subtypes_Statically_Compatible
13315 (Act_T, Ancestor, Formal_Derived_Matching => True)
13318 ("constraint on actual is incompatible with formal", Actual);
13319 Abandon_Instantiation (Actual);
13323 -- If the formal and actual types are abstract, check that there
13324 -- are no abstract primitives of the actual type that correspond to
13325 -- nonabstract primitives of the formal type (second sentence of
13328 if Is_Abstract_Type (A_Gen_T) and then Is_Abstract_Type (Act_T) then
13329 Check_Abstract_Primitives : declare
13330 Gen_Prims : constant Elist_Id :=
13331 Primitive_Operations (A_Gen_T);
13332 Gen_Elmt : Elmt_Id;
13333 Gen_Subp : Entity_Id;
13334 Anc_Subp : Entity_Id;
13335 Anc_Formal : Entity_Id;
13336 Anc_F_Type : Entity_Id;
13338 Act_Prims : constant Elist_Id := Primitive_Operations (Act_T);
13339 Act_Elmt : Elmt_Id;
13340 Act_Subp : Entity_Id;
13341 Act_Formal : Entity_Id;
13342 Act_F_Type : Entity_Id;
13344 Subprograms_Correspond : Boolean;
13346 function Is_Tagged_Ancestor (T1, T2 : Entity_Id) return Boolean;
13347 -- Returns true if T2 is derived directly or indirectly from
13348 -- T1, including derivations from interfaces. T1 and T2 are
13349 -- required to be specific tagged base types.
13351 ------------------------
13352 -- Is_Tagged_Ancestor --
13353 ------------------------
13355 function Is_Tagged_Ancestor (T1, T2 : Entity_Id) return Boolean
13357 Intfc_Elmt : Elmt_Id;
13360 -- The predicate is satisfied if the types are the same
13365 -- If we've reached the top of the derivation chain then
13366 -- we know that T1 is not an ancestor of T2.
13368 elsif Etype (T2) = T2 then
13371 -- Proceed to check T2's immediate parent
13373 elsif Is_Ancestor (T1, Base_Type (Etype (T2))) then
13376 -- Finally, check to see if T1 is an ancestor of any of T2's
13380 Intfc_Elmt := First_Elmt (Interfaces (T2));
13381 while Present (Intfc_Elmt) loop
13382 if Is_Ancestor (T1, Node (Intfc_Elmt)) then
13386 Next_Elmt (Intfc_Elmt);
13391 end Is_Tagged_Ancestor;
13393 -- Start of processing for Check_Abstract_Primitives
13396 -- Loop over all of the formal derived type's primitives
13398 Gen_Elmt := First_Elmt (Gen_Prims);
13399 while Present (Gen_Elmt) loop
13400 Gen_Subp := Node (Gen_Elmt);
13402 -- If the primitive of the formal is not abstract, then
13403 -- determine whether there is a corresponding primitive of
13404 -- the actual type that's abstract.
13406 if not Is_Abstract_Subprogram (Gen_Subp) then
13407 Act_Elmt := First_Elmt (Act_Prims);
13408 while Present (Act_Elmt) loop
13409 Act_Subp := Node (Act_Elmt);
13411 -- If we find an abstract primitive of the actual,
13412 -- then we need to test whether it corresponds to the
13413 -- subprogram from which the generic formal primitive
13416 if Is_Abstract_Subprogram (Act_Subp) then
13417 Anc_Subp := Alias (Gen_Subp);
13419 -- Test whether we have a corresponding primitive
13420 -- by comparing names, kinds, formal types, and
13423 if Chars (Anc_Subp) = Chars (Act_Subp)
13424 and then Ekind (Anc_Subp) = Ekind (Act_Subp)
13426 Anc_Formal := First_Formal (Anc_Subp);
13427 Act_Formal := First_Formal (Act_Subp);
13428 while Present (Anc_Formal)
13429 and then Present (Act_Formal)
13431 Anc_F_Type := Etype (Anc_Formal);
13432 Act_F_Type := Etype (Act_Formal);
13434 if Ekind (Anc_F_Type) =
13435 E_Anonymous_Access_Type
13437 Anc_F_Type := Designated_Type (Anc_F_Type);
13439 if Ekind (Act_F_Type) =
13440 E_Anonymous_Access_Type
13443 Designated_Type (Act_F_Type);
13449 Ekind (Act_F_Type) = E_Anonymous_Access_Type
13454 Anc_F_Type := Base_Type (Anc_F_Type);
13455 Act_F_Type := Base_Type (Act_F_Type);
13457 -- If the formal is controlling, then the
13458 -- the type of the actual primitive's formal
13459 -- must be derived directly or indirectly
13460 -- from the type of the ancestor primitive's
13463 if Is_Controlling_Formal (Anc_Formal) then
13464 if not Is_Tagged_Ancestor
13465 (Anc_F_Type, Act_F_Type)
13470 -- Otherwise the types of the formals must
13473 elsif Anc_F_Type /= Act_F_Type then
13477 Next_Entity (Anc_Formal);
13478 Next_Entity (Act_Formal);
13481 -- If we traversed through all of the formals
13482 -- then so far the subprograms correspond, so
13483 -- now check that any result types correspond.
13485 if No (Anc_Formal) and then No (Act_Formal) then
13486 Subprograms_Correspond := True;
13488 if Ekind (Act_Subp) = E_Function then
13489 Anc_F_Type := Etype (Anc_Subp);
13490 Act_F_Type := Etype (Act_Subp);
13492 if Ekind (Anc_F_Type) =
13493 E_Anonymous_Access_Type
13496 Designated_Type (Anc_F_Type);
13498 if Ekind (Act_F_Type) =
13499 E_Anonymous_Access_Type
13502 Designated_Type (Act_F_Type);
13504 Subprograms_Correspond := False;
13509 = E_Anonymous_Access_Type
13511 Subprograms_Correspond := False;
13514 Anc_F_Type := Base_Type (Anc_F_Type);
13515 Act_F_Type := Base_Type (Act_F_Type);
13517 -- Now either the result types must be
13518 -- the same or, if the result type is
13519 -- controlling, the result type of the
13520 -- actual primitive must descend from the
13521 -- result type of the ancestor primitive.
13523 if Subprograms_Correspond
13524 and then Anc_F_Type /= Act_F_Type
13526 Has_Controlling_Result (Anc_Subp)
13527 and then not Is_Tagged_Ancestor
13528 (Anc_F_Type, Act_F_Type)
13530 Subprograms_Correspond := False;
13534 -- Found a matching subprogram belonging to
13535 -- formal ancestor type, so actual subprogram
13536 -- corresponds and this violates 3.9.3(9).
13538 if Subprograms_Correspond then
13540 ("abstract subprogram & overrides "
13541 & "nonabstract subprogram of ancestor",
13548 Next_Elmt (Act_Elmt);
13552 Next_Elmt (Gen_Elmt);
13554 end Check_Abstract_Primitives;
13557 -- Verify that limitedness matches. If parent is a limited
13558 -- interface then the generic formal is not unless declared
13559 -- explicitly so. If not declared limited, the actual cannot be
13560 -- limited (see AI05-0087).
13562 if Is_Limited_Type (Act_T) and then not Is_Limited_Type (A_Gen_T) then
13563 if not In_Instance then
13565 ("actual for non-limited & cannot be a limited type",
13567 Explain_Limited_Type (Act_T, Actual);
13568 Abandon_Instantiation (Actual);
13572 -- Check for AI12-0036
13575 Formal_Is_Private_Extension : constant Boolean :=
13576 Nkind (Parent (A_Gen_T)) = N_Private_Extension_Declaration;
13578 Actual_Is_Tagged : constant Boolean := Is_Tagged_Type (Act_T);
13581 if Actual_Is_Tagged /= Formal_Is_Private_Extension then
13582 if not In_Instance then
13583 if Actual_Is_Tagged then
13585 ("actual for & cannot be a tagged type", Actual, Gen_T);
13588 ("actual for & must be a tagged type", Actual, Gen_T);
13591 Abandon_Instantiation (Actual);
13595 end Validate_Derived_Type_Instance;
13597 ----------------------------------------
13598 -- Validate_Discriminated_Formal_Type --
13599 ----------------------------------------
13601 procedure Validate_Discriminated_Formal_Type is
13602 Formal_Discr : Entity_Id;
13603 Actual_Discr : Entity_Id;
13604 Formal_Subt : Entity_Id;
13607 if Has_Discriminants (A_Gen_T) then
13608 if not Has_Discriminants (Act_T) then
13610 ("actual for & must have discriminants", Actual, Gen_T);
13611 Abandon_Instantiation (Actual);
13613 elsif Is_Constrained (Act_T) then
13615 ("actual for & must be unconstrained", Actual, Gen_T);
13616 Abandon_Instantiation (Actual);
13619 Formal_Discr := First_Discriminant (A_Gen_T);
13620 Actual_Discr := First_Discriminant (Act_T);
13621 while Formal_Discr /= Empty loop
13622 if Actual_Discr = Empty then
13624 ("discriminants on actual do not match formal",
13626 Abandon_Instantiation (Actual);
13629 Formal_Subt := Get_Instance_Of (Etype (Formal_Discr));
13631 -- Access discriminants match if designated types do
13633 if Ekind (Base_Type (Formal_Subt)) = E_Anonymous_Access_Type
13634 and then (Ekind (Base_Type (Etype (Actual_Discr)))) =
13635 E_Anonymous_Access_Type
13638 (Designated_Type (Base_Type (Formal_Subt))) =
13639 Designated_Type (Base_Type (Etype (Actual_Discr)))
13643 elsif Base_Type (Formal_Subt) /=
13644 Base_Type (Etype (Actual_Discr))
13647 ("types of actual discriminants must match formal",
13649 Abandon_Instantiation (Actual);
13651 elsif not Subtypes_Statically_Match
13652 (Formal_Subt, Etype (Actual_Discr))
13653 and then Ada_Version >= Ada_95
13656 ("subtypes of actual discriminants must match formal",
13658 Abandon_Instantiation (Actual);
13661 Next_Discriminant (Formal_Discr);
13662 Next_Discriminant (Actual_Discr);
13665 if Actual_Discr /= Empty then
13667 ("discriminants on actual do not match formal",
13669 Abandon_Instantiation (Actual);
13673 end Validate_Discriminated_Formal_Type;
13675 ---------------------------------------
13676 -- Validate_Incomplete_Type_Instance --
13677 ---------------------------------------
13679 procedure Validate_Incomplete_Type_Instance is
13681 if not Is_Tagged_Type (Act_T)
13682 and then Is_Tagged_Type (A_Gen_T)
13685 ("actual for & must be a tagged type", Actual, Gen_T);
13688 Validate_Discriminated_Formal_Type;
13689 end Validate_Incomplete_Type_Instance;
13691 --------------------------------------
13692 -- Validate_Interface_Type_Instance --
13693 --------------------------------------
13695 procedure Validate_Interface_Type_Instance is
13697 if not Is_Interface (Act_T) then
13699 ("actual for formal interface type must be an interface",
13702 elsif Is_Limited_Type (Act_T) /= Is_Limited_Type (A_Gen_T)
13703 or else Is_Task_Interface (A_Gen_T) /= Is_Task_Interface (Act_T)
13704 or else Is_Protected_Interface (A_Gen_T) /=
13705 Is_Protected_Interface (Act_T)
13706 or else Is_Synchronized_Interface (A_Gen_T) /=
13707 Is_Synchronized_Interface (Act_T)
13710 ("actual for interface& does not match (RM 12.5.5(4))",
13713 end Validate_Interface_Type_Instance;
13715 ------------------------------------
13716 -- Validate_Private_Type_Instance --
13717 ------------------------------------
13719 procedure Validate_Private_Type_Instance is
13721 if Is_Limited_Type (Act_T)
13722 and then not Is_Limited_Type (A_Gen_T)
13724 if In_Instance then
13728 ("actual for non-limited & cannot be a limited type", Actual,
13730 Explain_Limited_Type (Act_T, Actual);
13731 Abandon_Instantiation (Actual);
13734 elsif Known_To_Have_Preelab_Init (A_Gen_T)
13735 and then not Has_Preelaborable_Initialization (Act_T)
13738 ("actual for & must have preelaborable initialization", Actual,
13741 elsif not Is_Definite_Subtype (Act_T)
13742 and then Is_Definite_Subtype (A_Gen_T)
13743 and then Ada_Version >= Ada_95
13746 ("actual for & must be a definite subtype", Actual, Gen_T);
13748 elsif not Is_Tagged_Type (Act_T)
13749 and then Is_Tagged_Type (A_Gen_T)
13752 ("actual for & must be a tagged type", Actual, Gen_T);
13755 Validate_Discriminated_Formal_Type;
13757 end Validate_Private_Type_Instance;
13759 -- Start of processing for Instantiate_Type
13762 if Get_Instance_Of (A_Gen_T) /= A_Gen_T then
13763 Error_Msg_N ("duplicate instantiation of generic type", Actual);
13764 return New_List (Error);
13766 elsif not Is_Entity_Name (Actual)
13767 or else not Is_Type (Entity (Actual))
13770 ("expect valid subtype mark to instantiate &", Actual, Gen_T);
13771 Abandon_Instantiation (Actual);
13774 Act_T := Entity (Actual);
13776 -- Ada 2005 (AI-216): An Unchecked_Union subtype shall only be passed
13777 -- as a generic actual parameter if the corresponding formal type
13778 -- does not have a known_discriminant_part, or is a formal derived
13779 -- type that is an Unchecked_Union type.
13781 if Is_Unchecked_Union (Base_Type (Act_T)) then
13782 if not Has_Discriminants (A_Gen_T)
13783 or else (Is_Derived_Type (A_Gen_T)
13784 and then Is_Unchecked_Union (A_Gen_T))
13788 Error_Msg_N ("unchecked union cannot be the actual for a "
13789 & "discriminated formal type", Act_T);
13794 -- Deal with fixed/floating restrictions
13796 if Is_Floating_Point_Type (Act_T) then
13797 Check_Restriction (No_Floating_Point, Actual);
13798 elsif Is_Fixed_Point_Type (Act_T) then
13799 Check_Restriction (No_Fixed_Point, Actual);
13802 -- Deal with error of using incomplete type as generic actual.
13803 -- This includes limited views of a type, even if the non-limited
13804 -- view may be available.
13806 if Ekind (Act_T) = E_Incomplete_Type
13807 or else (Is_Class_Wide_Type (Act_T)
13808 and then Ekind (Root_Type (Act_T)) = E_Incomplete_Type)
13810 -- If the formal is an incomplete type, the actual can be
13811 -- incomplete as well.
13813 if Ekind (A_Gen_T) = E_Incomplete_Type then
13816 elsif Is_Class_Wide_Type (Act_T)
13817 or else No (Full_View (Act_T))
13819 Error_Msg_N ("premature use of incomplete type", Actual);
13820 Abandon_Instantiation (Actual);
13822 Act_T := Full_View (Act_T);
13823 Set_Entity (Actual, Act_T);
13825 if Has_Private_Component (Act_T) then
13827 ("premature use of type with private component", Actual);
13831 -- Deal with error of premature use of private type as generic actual
13833 elsif Is_Private_Type (Act_T)
13834 and then Is_Private_Type (Base_Type (Act_T))
13835 and then not Is_Generic_Type (Act_T)
13836 and then not Is_Derived_Type (Act_T)
13837 and then No (Full_View (Root_Type (Act_T)))
13839 -- If the formal is an incomplete type, the actual can be
13840 -- private or incomplete as well.
13842 if Ekind (A_Gen_T) = E_Incomplete_Type then
13845 Error_Msg_N ("premature use of private type", Actual);
13848 elsif Has_Private_Component (Act_T) then
13850 ("premature use of type with private component", Actual);
13853 Set_Instance_Of (A_Gen_T, Act_T);
13855 -- If the type is generic, the class-wide type may also be used
13857 if Is_Tagged_Type (A_Gen_T)
13858 and then Is_Tagged_Type (Act_T)
13859 and then not Is_Class_Wide_Type (A_Gen_T)
13861 Set_Instance_Of (Class_Wide_Type (A_Gen_T),
13862 Class_Wide_Type (Act_T));
13865 if not Is_Abstract_Type (A_Gen_T)
13866 and then Is_Abstract_Type (Act_T)
13869 ("actual of non-abstract formal cannot be abstract", Actual);
13872 -- A generic scalar type is a first subtype for which we generate
13873 -- an anonymous base type. Indicate that the instance of this base
13874 -- is the base type of the actual.
13876 if Is_Scalar_Type (A_Gen_T) then
13877 Set_Instance_Of (Etype (A_Gen_T), Etype (Act_T));
13881 Check_Shared_Variable_Control_Aspects;
13883 if Error_Posted (Act_T) then
13886 case Nkind (Def) is
13887 when N_Formal_Private_Type_Definition =>
13888 Validate_Private_Type_Instance;
13890 when N_Formal_Incomplete_Type_Definition =>
13891 Validate_Incomplete_Type_Instance;
13893 when N_Formal_Derived_Type_Definition =>
13894 Validate_Derived_Type_Instance;
13896 when N_Formal_Discrete_Type_Definition =>
13897 if not Is_Discrete_Type (Act_T) then
13899 ("expect discrete type in instantiation of&",
13901 Abandon_Instantiation (Actual);
13904 Diagnose_Predicated_Actual;
13906 when N_Formal_Signed_Integer_Type_Definition =>
13907 if not Is_Signed_Integer_Type (Act_T) then
13909 ("expect signed integer type in instantiation of&",
13911 Abandon_Instantiation (Actual);
13914 Diagnose_Predicated_Actual;
13916 when N_Formal_Modular_Type_Definition =>
13917 if not Is_Modular_Integer_Type (Act_T) then
13919 ("expect modular type in instantiation of &",
13921 Abandon_Instantiation (Actual);
13924 Diagnose_Predicated_Actual;
13926 when N_Formal_Floating_Point_Definition =>
13927 if not Is_Floating_Point_Type (Act_T) then
13929 ("expect float type in instantiation of &", Actual, Gen_T);
13930 Abandon_Instantiation (Actual);
13933 when N_Formal_Ordinary_Fixed_Point_Definition =>
13934 if not Is_Ordinary_Fixed_Point_Type (Act_T) then
13936 ("expect ordinary fixed point type in instantiation of &",
13938 Abandon_Instantiation (Actual);
13941 when N_Formal_Decimal_Fixed_Point_Definition =>
13942 if not Is_Decimal_Fixed_Point_Type (Act_T) then
13944 ("expect decimal type in instantiation of &",
13946 Abandon_Instantiation (Actual);
13949 when N_Array_Type_Definition =>
13950 Validate_Array_Type_Instance;
13952 when N_Access_To_Object_Definition =>
13953 Validate_Access_Type_Instance;
13955 when N_Access_Function_Definition
13956 | N_Access_Procedure_Definition
13958 Validate_Access_Subprogram_Instance;
13960 when N_Record_Definition =>
13961 Validate_Interface_Type_Instance;
13963 when N_Derived_Type_Definition =>
13964 Validate_Derived_Interface_Type_Instance;
13967 raise Program_Error;
13971 Subt := New_Copy (Gen_T);
13973 -- Use adjusted sloc of subtype name as the location for other nodes in
13974 -- the subtype declaration.
13976 Loc := Sloc (Subt);
13979 Make_Subtype_Declaration (Loc,
13980 Defining_Identifier => Subt,
13981 Subtype_Indication => New_Occurrence_Of (Act_T, Loc));
13983 -- Record whether the actual is private at this point, so that
13984 -- Check_Generic_Actuals can restore its proper view before the
13985 -- semantic analysis of the instance.
13987 if Is_Private_Type (Act_T) then
13988 Set_Has_Private_View (Subtype_Indication (Decl_Node));
13991 -- In Ada 2012 the actual may be a limited view. Indicate that
13992 -- the local subtype must be treated as such.
13994 if From_Limited_With (Act_T) then
13995 Set_Ekind (Subt, E_Incomplete_Subtype);
13996 Set_From_Limited_With (Subt);
13999 Decl_Nodes := New_List (Decl_Node);
14001 -- Flag actual derived types so their elaboration produces the
14002 -- appropriate renamings for the primitive operations of the ancestor.
14003 -- Flag actual for formal private types as well, to determine whether
14004 -- operations in the private part may override inherited operations.
14005 -- If the formal has an interface list, the ancestor is not the
14006 -- parent, but the analyzed formal that includes the interface
14007 -- operations of all its progenitors.
14009 -- Same treatment for formal private types, so we can check whether the
14010 -- type is tagged limited when validating derivations in the private
14011 -- part. (See AI05-096).
14013 if Nkind (Def) = N_Formal_Derived_Type_Definition then
14014 if Present (Interface_List (Def)) then
14015 Set_Generic_Parent_Type (Decl_Node, A_Gen_T);
14017 Set_Generic_Parent_Type (Decl_Node, Ancestor);
14020 elsif Nkind_In (Def, N_Formal_Private_Type_Definition,
14021 N_Formal_Incomplete_Type_Definition)
14023 Set_Generic_Parent_Type (Decl_Node, A_Gen_T);
14026 -- If the actual is a synchronized type that implements an interface,
14027 -- the primitive operations are attached to the corresponding record,
14028 -- and we have to treat it as an additional generic actual, so that its
14029 -- primitive operations become visible in the instance. The task or
14030 -- protected type itself does not carry primitive operations.
14032 if Is_Concurrent_Type (Act_T)
14033 and then Is_Tagged_Type (Act_T)
14034 and then Present (Corresponding_Record_Type (Act_T))
14035 and then Present (Ancestor)
14036 and then Is_Interface (Ancestor)
14039 Corr_Rec : constant Entity_Id :=
14040 Corresponding_Record_Type (Act_T);
14041 New_Corr : Entity_Id;
14042 Corr_Decl : Node_Id;
14045 New_Corr := Make_Temporary (Loc, 'S');
14047 Make_Subtype_Declaration (Loc,
14048 Defining_Identifier => New_Corr,
14049 Subtype_Indication =>
14050 New_Occurrence_Of (Corr_Rec, Loc));
14051 Append_To (Decl_Nodes, Corr_Decl);
14053 if Ekind (Act_T) = E_Task_Type then
14054 Set_Ekind (Subt, E_Task_Subtype);
14056 Set_Ekind (Subt, E_Protected_Subtype);
14059 Set_Corresponding_Record_Type (Subt, Corr_Rec);
14060 Set_Generic_Parent_Type (Corr_Decl, Ancestor);
14061 Set_Generic_Parent_Type (Decl_Node, Empty);
14065 -- For a floating-point type, capture dimension info if any, because
14066 -- the generated subtype declaration does not come from source and
14067 -- will not process dimensions.
14069 if Is_Floating_Point_Type (Act_T) then
14070 Copy_Dimensions (Act_T, Subt);
14074 end Instantiate_Type;
14076 ---------------------
14077 -- Is_In_Main_Unit --
14078 ---------------------
14080 function Is_In_Main_Unit (N : Node_Id) return Boolean is
14081 Unum : constant Unit_Number_Type := Get_Source_Unit (N);
14082 Current_Unit : Node_Id;
14085 if Unum = Main_Unit then
14088 -- If the current unit is a subunit then it is either the main unit or
14089 -- is being compiled as part of the main unit.
14091 elsif Nkind (N) = N_Compilation_Unit then
14092 return Nkind (Unit (N)) = N_Subunit;
14095 Current_Unit := Parent (N);
14096 while Present (Current_Unit)
14097 and then Nkind (Current_Unit) /= N_Compilation_Unit
14099 Current_Unit := Parent (Current_Unit);
14102 -- The instantiation node is in the main unit, or else the current node
14103 -- (perhaps as the result of nested instantiations) is in the main unit,
14104 -- or in the declaration of the main unit, which in this last case must
14108 Current_Unit = Cunit (Main_Unit)
14109 or else Current_Unit = Library_Unit (Cunit (Main_Unit))
14110 or else (Present (Current_Unit)
14111 and then Present (Library_Unit (Current_Unit))
14112 and then Is_In_Main_Unit (Library_Unit (Current_Unit)));
14113 end Is_In_Main_Unit;
14115 ----------------------------
14116 -- Load_Parent_Of_Generic --
14117 ----------------------------
14119 procedure Load_Parent_Of_Generic
14122 Body_Optional : Boolean := False)
14124 Comp_Unit : constant Node_Id := Cunit (Get_Source_Unit (Spec));
14125 Saved_Style_Check : constant Boolean := Style_Check;
14126 Saved_Warnings : constant Warning_Record := Save_Warnings;
14127 True_Parent : Node_Id;
14128 Inst_Node : Node_Id;
14130 Previous_Instances : constant Elist_Id := New_Elmt_List;
14132 procedure Collect_Previous_Instances (Decls : List_Id);
14133 -- Collect all instantiations in the given list of declarations, that
14134 -- precede the generic that we need to load. If the bodies of these
14135 -- instantiations are available, we must analyze them, to ensure that
14136 -- the public symbols generated are the same when the unit is compiled
14137 -- to generate code, and when it is compiled in the context of a unit
14138 -- that needs a particular nested instance. This process is applied to
14139 -- both package and subprogram instances.
14141 --------------------------------
14142 -- Collect_Previous_Instances --
14143 --------------------------------
14145 procedure Collect_Previous_Instances (Decls : List_Id) is
14149 Decl := First (Decls);
14150 while Present (Decl) loop
14151 if Sloc (Decl) >= Sloc (Inst_Node) then
14154 -- If Decl is an instantiation, then record it as requiring
14155 -- instantiation of the corresponding body, except if it is an
14156 -- abbreviated instantiation generated internally for conformance
14157 -- checking purposes only for the case of a formal package
14158 -- declared without a box (see Instantiate_Formal_Package). Such
14159 -- an instantiation does not generate any code (the actual code
14160 -- comes from actual) and thus does not need to be analyzed here.
14161 -- If the instantiation appears with a generic package body it is
14162 -- not analyzed here either.
14164 elsif Nkind (Decl) = N_Package_Instantiation
14165 and then not Is_Internal (Defining_Entity (Decl))
14167 Append_Elmt (Decl, Previous_Instances);
14169 -- For a subprogram instantiation, omit instantiations intrinsic
14170 -- operations (Unchecked_Conversions, etc.) that have no bodies.
14172 elsif Nkind_In (Decl, N_Function_Instantiation,
14173 N_Procedure_Instantiation)
14174 and then not Is_Intrinsic_Subprogram (Entity (Name (Decl)))
14176 Append_Elmt (Decl, Previous_Instances);
14178 elsif Nkind (Decl) = N_Package_Declaration then
14179 Collect_Previous_Instances
14180 (Visible_Declarations (Specification (Decl)));
14181 Collect_Previous_Instances
14182 (Private_Declarations (Specification (Decl)));
14184 -- Previous non-generic bodies may contain instances as well
14186 elsif Nkind (Decl) = N_Package_Body
14187 and then Ekind (Corresponding_Spec (Decl)) /= E_Generic_Package
14189 Collect_Previous_Instances (Declarations (Decl));
14191 elsif Nkind (Decl) = N_Subprogram_Body
14192 and then not Acts_As_Spec (Decl)
14193 and then not Is_Generic_Subprogram (Corresponding_Spec (Decl))
14195 Collect_Previous_Instances (Declarations (Decl));
14200 end Collect_Previous_Instances;
14202 -- Start of processing for Load_Parent_Of_Generic
14205 if not In_Same_Source_Unit (N, Spec)
14206 or else Nkind (Unit (Comp_Unit)) = N_Package_Declaration
14207 or else (Nkind (Unit (Comp_Unit)) = N_Package_Body
14208 and then not Is_In_Main_Unit (Spec))
14210 -- Find body of parent of spec, and analyze it. A special case arises
14211 -- when the parent is an instantiation, that is to say when we are
14212 -- currently instantiating a nested generic. In that case, there is
14213 -- no separate file for the body of the enclosing instance. Instead,
14214 -- the enclosing body must be instantiated as if it were a pending
14215 -- instantiation, in order to produce the body for the nested generic
14216 -- we require now. Note that in that case the generic may be defined
14217 -- in a package body, the instance defined in the same package body,
14218 -- and the original enclosing body may not be in the main unit.
14220 Inst_Node := Empty;
14222 True_Parent := Parent (Spec);
14223 while Present (True_Parent)
14224 and then Nkind (True_Parent) /= N_Compilation_Unit
14226 if Nkind (True_Parent) = N_Package_Declaration
14228 Nkind (Original_Node (True_Parent)) = N_Package_Instantiation
14230 -- Parent is a compilation unit that is an instantiation, and
14231 -- instantiation node has been replaced with package decl.
14233 Inst_Node := Original_Node (True_Parent);
14236 elsif Nkind (True_Parent) = N_Package_Declaration
14237 and then Nkind (Parent (True_Parent)) = N_Compilation_Unit
14239 Nkind (Unit (Parent (True_Parent))) = N_Package_Instantiation
14241 -- Parent is a compilation unit that is an instantiation, but
14242 -- instantiation node has not been replaced with package decl.
14244 Inst_Node := Unit (Parent (True_Parent));
14247 elsif Nkind (True_Parent) = N_Package_Declaration
14248 and then Nkind (Parent (True_Parent)) /= N_Compilation_Unit
14249 and then Present (Generic_Parent (Specification (True_Parent)))
14251 -- Parent is an instantiation within another specification.
14252 -- Declaration for instance has been inserted before original
14253 -- instantiation node. A direct link would be preferable?
14255 Inst_Node := Next (True_Parent);
14256 while Present (Inst_Node)
14257 and then Nkind (Inst_Node) /= N_Package_Instantiation
14262 -- If the instance appears within a generic, and the generic
14263 -- unit is defined within a formal package of the enclosing
14264 -- generic, there is no generic body available, and none
14265 -- needed. A more precise test should be used ???
14267 if No (Inst_Node) then
14274 True_Parent := Parent (True_Parent);
14278 -- Case where we are currently instantiating a nested generic
14280 if Present (Inst_Node) then
14281 if Nkind (Parent (True_Parent)) = N_Compilation_Unit then
14283 -- Instantiation node and declaration of instantiated package
14284 -- were exchanged when only the declaration was needed.
14285 -- Restore instantiation node before proceeding with body.
14287 Set_Unit (Parent (True_Parent), Inst_Node);
14290 -- Now complete instantiation of enclosing body, if it appears in
14291 -- some other unit. If it appears in the current unit, the body
14292 -- will have been instantiated already.
14294 if No (Corresponding_Body (Instance_Spec (Inst_Node))) then
14296 -- We need to determine the expander mode to instantiate the
14297 -- enclosing body. Because the generic body we need may use
14298 -- global entities declared in the enclosing package (including
14299 -- aggregates) it is in general necessary to compile this body
14300 -- with expansion enabled, except if we are within a generic
14301 -- package, in which case the usual generic rule applies.
14304 Exp_Status : Boolean := True;
14308 -- Loop through scopes looking for generic package
14310 Scop := Scope (Defining_Entity (Instance_Spec (Inst_Node)));
14311 while Present (Scop)
14312 and then Scop /= Standard_Standard
14314 if Ekind (Scop) = E_Generic_Package then
14315 Exp_Status := False;
14319 Scop := Scope (Scop);
14322 -- Collect previous instantiations in the unit that contains
14323 -- the desired generic.
14325 if Nkind (Parent (True_Parent)) /= N_Compilation_Unit
14326 and then not Body_Optional
14330 Info : Pending_Body_Info;
14334 Par := Parent (Inst_Node);
14335 while Present (Par) loop
14336 exit when Nkind (Parent (Par)) = N_Compilation_Unit;
14337 Par := Parent (Par);
14340 pragma Assert (Present (Par));
14342 if Nkind (Par) = N_Package_Body then
14343 Collect_Previous_Instances (Declarations (Par));
14345 elsif Nkind (Par) = N_Package_Declaration then
14346 Collect_Previous_Instances
14347 (Visible_Declarations (Specification (Par)));
14348 Collect_Previous_Instances
14349 (Private_Declarations (Specification (Par)));
14352 -- Enclosing unit is a subprogram body. In this
14353 -- case all instance bodies are processed in order
14354 -- and there is no need to collect them separately.
14359 Decl := First_Elmt (Previous_Instances);
14360 while Present (Decl) loop
14363 Instance_Spec (Node (Decl)),
14364 Config_Switches => Save_Config_Switches,
14365 Current_Sem_Unit =>
14366 Get_Code_Unit (Sloc (Node (Decl))),
14367 Expander_Status => Exp_Status,
14368 Inst_Node => Node (Decl),
14369 Local_Suppress_Stack_Top =>
14370 Local_Suppress_Stack_Top,
14371 Scope_Suppress => Scope_Suppress,
14372 Warnings => Save_Warnings);
14374 -- Package instance
14376 if Nkind (Node (Decl)) = N_Package_Instantiation
14378 Instantiate_Package_Body
14379 (Info, Body_Optional => True);
14381 -- Subprogram instance
14384 -- The instance_spec is in the wrapper package,
14385 -- usually followed by its local renaming
14386 -- declaration. See Build_Subprogram_Renaming
14387 -- for details. If the instance carries aspects,
14388 -- these result in the corresponding pragmas,
14389 -- inserted after the subprogram declaration.
14390 -- They must be skipped as well when retrieving
14391 -- the desired spec. Some of them may have been
14392 -- rewritten as null statements.
14393 -- A direct link would be more robust ???
14397 (Last (Visible_Declarations
14398 (Specification (Info.Act_Decl))));
14400 while Nkind_In (Decl,
14403 N_Subprogram_Renaming_Declaration)
14405 Decl := Prev (Decl);
14408 Info.Act_Decl := Decl;
14411 Instantiate_Subprogram_Body
14412 (Info, Body_Optional => True);
14420 Instantiate_Package_Body
14422 ((Act_Decl => True_Parent,
14423 Config_Switches => Save_Config_Switches,
14424 Current_Sem_Unit =>
14425 Get_Code_Unit (Sloc (Inst_Node)),
14426 Expander_Status => Exp_Status,
14427 Inst_Node => Inst_Node,
14428 Local_Suppress_Stack_Top => Local_Suppress_Stack_Top,
14429 Scope_Suppress => Scope_Suppress,
14430 Warnings => Save_Warnings)),
14431 Body_Optional => Body_Optional);
14435 -- Case where we are not instantiating a nested generic
14438 Opt.Style_Check := False;
14439 Expander_Mode_Save_And_Set (True);
14440 Load_Needed_Body (Comp_Unit, OK);
14441 Opt.Style_Check := Saved_Style_Check;
14442 Restore_Warnings (Saved_Warnings);
14443 Expander_Mode_Restore;
14446 and then Unit_Requires_Body (Defining_Entity (Spec))
14447 and then not Body_Optional
14450 Bname : constant Unit_Name_Type :=
14451 Get_Body_Name (Get_Unit_Name (Unit (Comp_Unit)));
14454 -- In CodePeer mode, the missing body may make the analysis
14455 -- incomplete, but we do not treat it as fatal.
14457 if CodePeer_Mode then
14461 Error_Msg_Unit_1 := Bname;
14462 Error_Msg_N ("this instantiation requires$!", N);
14463 Error_Msg_File_1 :=
14464 Get_File_Name (Bname, Subunit => False);
14465 Error_Msg_N ("\but file{ was not found!", N);
14466 raise Unrecoverable_Error;
14473 -- If loading parent of the generic caused an instantiation circularity,
14474 -- we abandon compilation at this point, because otherwise in some cases
14475 -- we get into trouble with infinite recursions after this point.
14477 if Circularity_Detected then
14478 raise Unrecoverable_Error;
14480 end Load_Parent_Of_Generic;
14482 ---------------------------------
14483 -- Map_Formal_Package_Entities --
14484 ---------------------------------
14486 procedure Map_Formal_Package_Entities (Form : Entity_Id; Act : Entity_Id) is
14491 Set_Instance_Of (Form, Act);
14493 -- Traverse formal and actual package to map the corresponding entities.
14494 -- We skip over internal entities that may be generated during semantic
14495 -- analysis, and find the matching entities by name, given that they
14496 -- must appear in the same order.
14498 E1 := First_Entity (Form);
14499 E2 := First_Entity (Act);
14500 while Present (E1) and then E1 /= First_Private_Entity (Form) loop
14501 -- Could this test be a single condition??? Seems like it could, and
14502 -- isn't FPE (Form) a constant anyway???
14504 if not Is_Internal (E1)
14505 and then Present (Parent (E1))
14506 and then not Is_Class_Wide_Type (E1)
14507 and then not Is_Internal_Name (Chars (E1))
14509 while Present (E2) and then Chars (E2) /= Chars (E1) loop
14516 Set_Instance_Of (E1, E2);
14518 if Is_Type (E1) and then Is_Tagged_Type (E2) then
14519 Set_Instance_Of (Class_Wide_Type (E1), Class_Wide_Type (E2));
14522 if Is_Constrained (E1) then
14523 Set_Instance_Of (Base_Type (E1), Base_Type (E2));
14526 if Ekind (E1) = E_Package and then No (Renamed_Object (E1)) then
14527 Map_Formal_Package_Entities (E1, E2);
14534 end Map_Formal_Package_Entities;
14536 -----------------------
14537 -- Move_Freeze_Nodes --
14538 -----------------------
14540 procedure Move_Freeze_Nodes
14541 (Out_Of : Entity_Id;
14546 Next_Decl : Node_Id;
14547 Next_Node : Node_Id := After;
14550 function Is_Outer_Type (T : Entity_Id) return Boolean;
14551 -- Check whether entity is declared in a scope external to that of the
14554 -------------------
14555 -- Is_Outer_Type --
14556 -------------------
14558 function Is_Outer_Type (T : Entity_Id) return Boolean is
14559 Scop : Entity_Id := Scope (T);
14562 if Scope_Depth (Scop) < Scope_Depth (Out_Of) then
14566 while Scop /= Standard_Standard loop
14567 if Scop = Out_Of then
14570 Scop := Scope (Scop);
14578 -- Start of processing for Move_Freeze_Nodes
14585 -- First remove the freeze nodes that may appear before all other
14589 while Present (Decl)
14590 and then Nkind (Decl) = N_Freeze_Entity
14591 and then Is_Outer_Type (Entity (Decl))
14593 Decl := Remove_Head (L);
14594 Insert_After (Next_Node, Decl);
14595 Set_Analyzed (Decl, False);
14600 -- Next scan the list of declarations and remove each freeze node that
14601 -- appears ahead of the current node.
14603 while Present (Decl) loop
14604 while Present (Next (Decl))
14605 and then Nkind (Next (Decl)) = N_Freeze_Entity
14606 and then Is_Outer_Type (Entity (Next (Decl)))
14608 Next_Decl := Remove_Next (Decl);
14609 Insert_After (Next_Node, Next_Decl);
14610 Set_Analyzed (Next_Decl, False);
14611 Next_Node := Next_Decl;
14614 -- If the declaration is a nested package or concurrent type, then
14615 -- recurse. Nested generic packages will have been processed from the
14618 case Nkind (Decl) is
14619 when N_Package_Declaration =>
14620 Spec := Specification (Decl);
14622 when N_Task_Type_Declaration =>
14623 Spec := Task_Definition (Decl);
14625 when N_Protected_Type_Declaration =>
14626 Spec := Protected_Definition (Decl);
14632 if Present (Spec) then
14633 Move_Freeze_Nodes (Out_Of, Next_Node, Visible_Declarations (Spec));
14634 Move_Freeze_Nodes (Out_Of, Next_Node, Private_Declarations (Spec));
14639 end Move_Freeze_Nodes;
14645 function Next_Assoc (E : Assoc_Ptr) return Assoc_Ptr is
14647 return Generic_Renamings.Table (E).Next_In_HTable;
14650 ------------------------
14651 -- Preanalyze_Actuals --
14652 ------------------------
14654 procedure Preanalyze_Actuals (N : Node_Id; Inst : Entity_Id := Empty) is
14655 procedure Perform_Appropriate_Analysis (N : Node_Id);
14656 -- Determine if the actuals we are analyzing come from a generic
14657 -- instantiation that is a library unit and dispatch accordingly.
14659 ----------------------------------
14660 -- Perform_Appropriate_Analysis --
14661 ----------------------------------
14663 procedure Perform_Appropriate_Analysis (N : Node_Id) is
14665 -- When we have a library instantiation we cannot allow any expansion
14666 -- to occur, since there may be no place to put it. Instead, in that
14667 -- case we perform a preanalysis of the actual.
14669 if Present (Inst) and then Is_Compilation_Unit (Inst) then
14674 end Perform_Appropriate_Analysis;
14678 Errs : constant Nat := Serious_Errors_Detected;
14683 Cur : Entity_Id := Empty;
14684 -- Current homograph of the instance name
14686 Vis : Boolean := False;
14687 -- Saved visibility status of the current homograph
14689 -- Start of processing for Preanalyze_Actuals
14692 Assoc := First (Generic_Associations (N));
14694 -- If the instance is a child unit, its name may hide an outer homonym,
14695 -- so make it invisible to perform name resolution on the actuals.
14697 if Nkind (Defining_Unit_Name (N)) = N_Defining_Program_Unit_Name
14699 (Current_Entity (Defining_Identifier (Defining_Unit_Name (N))))
14701 Cur := Current_Entity (Defining_Identifier (Defining_Unit_Name (N)));
14703 if Is_Compilation_Unit (Cur) then
14704 Vis := Is_Immediately_Visible (Cur);
14705 Set_Is_Immediately_Visible (Cur, False);
14711 while Present (Assoc) loop
14712 if Nkind (Assoc) /= N_Others_Choice then
14713 Act := Explicit_Generic_Actual_Parameter (Assoc);
14715 -- Within a nested instantiation, a defaulted actual is an empty
14716 -- association, so nothing to analyze. If the subprogram actual
14717 -- is an attribute, analyze prefix only, because actual is not a
14718 -- complete attribute reference.
14720 -- If actual is an allocator, analyze expression only. The full
14721 -- analysis can generate code, and if instance is a compilation
14722 -- unit we have to wait until the package instance is installed
14723 -- to have a proper place to insert this code.
14725 -- String literals may be operators, but at this point we do not
14726 -- know whether the actual is a formal subprogram or a string.
14731 elsif Nkind (Act) = N_Attribute_Reference then
14732 Perform_Appropriate_Analysis (Prefix (Act));
14734 elsif Nkind (Act) = N_Explicit_Dereference then
14735 Perform_Appropriate_Analysis (Prefix (Act));
14737 elsif Nkind (Act) = N_Allocator then
14739 Expr : constant Node_Id := Expression (Act);
14742 if Nkind (Expr) = N_Subtype_Indication then
14743 Perform_Appropriate_Analysis (Subtype_Mark (Expr));
14745 -- Analyze separately each discriminant constraint, when
14746 -- given with a named association.
14752 Constr := First (Constraints (Constraint (Expr)));
14753 while Present (Constr) loop
14754 if Nkind (Constr) = N_Discriminant_Association then
14755 Perform_Appropriate_Analysis
14756 (Expression (Constr));
14758 Perform_Appropriate_Analysis (Constr);
14766 Perform_Appropriate_Analysis (Expr);
14770 elsif Nkind (Act) /= N_Operator_Symbol then
14771 Perform_Appropriate_Analysis (Act);
14773 -- Within a package instance, mark actuals that are limited
14774 -- views, so their use can be moved to the body of the
14777 if Is_Entity_Name (Act)
14778 and then Is_Type (Entity (Act))
14779 and then From_Limited_With (Entity (Act))
14780 and then Present (Inst)
14782 Append_Elmt (Entity (Act), Incomplete_Actuals (Inst));
14786 if Errs /= Serious_Errors_Detected then
14788 -- Do a minimal analysis of the generic, to prevent spurious
14789 -- warnings complaining about the generic being unreferenced,
14790 -- before abandoning the instantiation.
14792 Perform_Appropriate_Analysis (Name (N));
14794 if Is_Entity_Name (Name (N))
14795 and then Etype (Name (N)) /= Any_Type
14797 Generate_Reference (Entity (Name (N)), Name (N));
14798 Set_Is_Instantiated (Entity (Name (N)));
14801 if Present (Cur) then
14803 -- For the case of a child instance hiding an outer homonym,
14804 -- provide additional warning which might explain the error.
14806 Set_Is_Immediately_Visible (Cur, Vis);
14808 ("& hides outer unit with the same name??",
14809 N, Defining_Unit_Name (N));
14812 Abandon_Instantiation (Act);
14819 if Present (Cur) then
14820 Set_Is_Immediately_Visible (Cur, Vis);
14822 end Preanalyze_Actuals;
14824 -------------------------------
14825 -- Provide_Completing_Bodies --
14826 -------------------------------
14828 procedure Provide_Completing_Bodies (N : Node_Id) is
14829 procedure Build_Completing_Body (Subp_Decl : Node_Id);
14830 -- Generate the completing body for subprogram declaration Subp_Decl
14832 procedure Provide_Completing_Bodies_In (Decls : List_Id);
14833 -- Generating completing bodies for all subprograms found in declarative
14836 ---------------------------
14837 -- Build_Completing_Body --
14838 ---------------------------
14840 procedure Build_Completing_Body (Subp_Decl : Node_Id) is
14841 Loc : constant Source_Ptr := Sloc (Subp_Decl);
14842 Subp_Id : constant Entity_Id := Defining_Entity (Subp_Decl);
14846 -- Nothing to do if the subprogram already has a completing body
14848 if Present (Corresponding_Body (Subp_Decl)) then
14851 -- Mark the function as having a valid return statement even though
14852 -- the body contains a single raise statement.
14854 elsif Ekind (Subp_Id) = E_Function then
14855 Set_Return_Present (Subp_Id);
14858 -- Clone the specification to obtain new entities and reset the only
14861 Spec := Copy_Subprogram_Spec (Specification (Subp_Decl));
14862 Set_Generic_Parent (Spec, Empty);
14865 -- function Func ... return ... is
14867 -- procedure Proc ... is
14869 -- raise Program_Error with "access before elaboration";
14872 Insert_After_And_Analyze (Subp_Decl,
14873 Make_Subprogram_Body (Loc,
14874 Specification => Spec,
14875 Declarations => New_List,
14876 Handled_Statement_Sequence =>
14877 Make_Handled_Sequence_Of_Statements (Loc,
14878 Statements => New_List (
14879 Make_Raise_Program_Error (Loc,
14880 Reason => PE_Access_Before_Elaboration)))));
14881 end Build_Completing_Body;
14883 ----------------------------------
14884 -- Provide_Completing_Bodies_In --
14885 ----------------------------------
14887 procedure Provide_Completing_Bodies_In (Decls : List_Id) is
14891 if Present (Decls) then
14892 Decl := First (Decls);
14893 while Present (Decl) loop
14894 Provide_Completing_Bodies (Decl);
14898 end Provide_Completing_Bodies_In;
14904 -- Start of processing for Provide_Completing_Bodies
14907 if Nkind (N) = N_Package_Declaration then
14908 Spec := Specification (N);
14910 Push_Scope (Defining_Entity (N));
14911 Provide_Completing_Bodies_In (Visible_Declarations (Spec));
14912 Provide_Completing_Bodies_In (Private_Declarations (Spec));
14915 elsif Nkind (N) = N_Subprogram_Declaration then
14916 Build_Completing_Body (N);
14918 end Provide_Completing_Bodies;
14920 -------------------
14921 -- Remove_Parent --
14922 -------------------
14924 procedure Remove_Parent (In_Body : Boolean := False) is
14925 S : Entity_Id := Current_Scope;
14926 -- S is the scope containing the instantiation just completed. The scope
14927 -- stack contains the parent instances of the instantiation, followed by
14936 -- After child instantiation is complete, remove from scope stack the
14937 -- extra copy of the current scope, and then remove parent instances.
14939 if not In_Body then
14942 while Current_Scope /= S loop
14943 P := Current_Scope;
14944 End_Package_Scope (Current_Scope);
14946 if In_Open_Scopes (P) then
14947 E := First_Entity (P);
14948 while Present (E) loop
14949 Set_Is_Immediately_Visible (E, True);
14953 -- If instantiation is declared in a block, it is the enclosing
14954 -- scope that might be a parent instance. Note that only one
14955 -- block can be involved, because the parent instances have
14956 -- been installed within it.
14958 if Ekind (P) = E_Block then
14959 Cur_P := Scope (P);
14964 if Is_Generic_Instance (Cur_P) and then P /= Current_Scope then
14965 -- We are within an instance of some sibling. Retain
14966 -- visibility of parent, for proper subsequent cleanup, and
14967 -- reinstall private declarations as well.
14969 Set_In_Private_Part (P);
14970 Install_Private_Declarations (P);
14973 -- If the ultimate parent is a top-level unit recorded in
14974 -- Instance_Parent_Unit, then reset its visibility to what it was
14975 -- before instantiation. (It's not clear what the purpose is of
14976 -- testing whether Scope (P) is In_Open_Scopes, but that test was
14977 -- present before the ultimate parent test was added.???)
14979 elsif not In_Open_Scopes (Scope (P))
14980 or else (P = Instance_Parent_Unit
14981 and then not Parent_Unit_Visible)
14983 Set_Is_Immediately_Visible (P, False);
14985 -- If the current scope is itself an instantiation of a generic
14986 -- nested within P, and we are in the private part of body of this
14987 -- instantiation, restore the full views of P, that were removed
14988 -- in End_Package_Scope above. This obscure case can occur when a
14989 -- subunit of a generic contains an instance of a child unit of
14990 -- its generic parent unit.
14992 elsif S = Current_Scope and then Is_Generic_Instance (S) then
14994 Par : constant Entity_Id :=
14995 Generic_Parent (Package_Specification (S));
14998 and then P = Scope (Par)
14999 and then (In_Package_Body (S) or else In_Private_Part (S))
15001 Set_In_Private_Part (P);
15002 Install_Private_Declarations (P);
15008 -- Reset visibility of entities in the enclosing scope
15010 Set_Is_Hidden_Open_Scope (Current_Scope, False);
15012 Hidden := First_Elmt (Hidden_Entities);
15013 while Present (Hidden) loop
15014 Set_Is_Immediately_Visible (Node (Hidden), True);
15015 Next_Elmt (Hidden);
15019 -- Each body is analyzed separately, and there is no context that
15020 -- needs preserving from one body instance to the next, so remove all
15021 -- parent scopes that have been installed.
15023 while Present (S) loop
15024 End_Package_Scope (S);
15025 Set_Is_Immediately_Visible (S, False);
15026 S := Current_Scope;
15027 exit when S = Standard_Standard;
15036 procedure Restore_Env is
15037 Saved : Instance_Env renames Instance_Envs.Table (Instance_Envs.Last);
15040 if No (Current_Instantiated_Parent.Act_Id) then
15041 -- Restore environment after subprogram inlining
15043 Restore_Private_Views (Empty);
15046 Current_Instantiated_Parent := Saved.Instantiated_Parent;
15047 Exchanged_Views := Saved.Exchanged_Views;
15048 Hidden_Entities := Saved.Hidden_Entities;
15049 Current_Sem_Unit := Saved.Current_Sem_Unit;
15050 Parent_Unit_Visible := Saved.Parent_Unit_Visible;
15051 Instance_Parent_Unit := Saved.Instance_Parent_Unit;
15053 Restore_Config_Switches (Saved.Switches);
15055 Instance_Envs.Decrement_Last;
15058 ---------------------------
15059 -- Restore_Private_Views --
15060 ---------------------------
15062 procedure Restore_Private_Views
15063 (Pack_Id : Entity_Id;
15064 Is_Package : Boolean := True)
15069 Dep_Elmt : Elmt_Id;
15072 procedure Restore_Nested_Formal (Formal : Entity_Id);
15073 -- Hide the generic formals of formal packages declared with box which
15074 -- were reachable in the current instantiation.
15076 ---------------------------
15077 -- Restore_Nested_Formal --
15078 ---------------------------
15080 procedure Restore_Nested_Formal (Formal : Entity_Id) is
15084 if Present (Renamed_Object (Formal))
15085 and then Denotes_Formal_Package (Renamed_Object (Formal), True)
15089 elsif Present (Associated_Formal_Package (Formal)) then
15090 Ent := First_Entity (Formal);
15091 while Present (Ent) loop
15092 exit when Ekind (Ent) = E_Package
15093 and then Renamed_Entity (Ent) = Renamed_Entity (Formal);
15095 Set_Is_Hidden (Ent);
15096 Set_Is_Potentially_Use_Visible (Ent, False);
15098 -- If package, then recurse
15100 if Ekind (Ent) = E_Package then
15101 Restore_Nested_Formal (Ent);
15107 end Restore_Nested_Formal;
15109 -- Start of processing for Restore_Private_Views
15112 M := First_Elmt (Exchanged_Views);
15113 while Present (M) loop
15116 -- Subtypes of types whose views have been exchanged, and that are
15117 -- defined within the instance, were not on the Private_Dependents
15118 -- list on entry to the instance, so they have to be exchanged
15119 -- explicitly now, in order to remain consistent with the view of the
15122 if Ekind_In (Typ, E_Private_Type,
15123 E_Limited_Private_Type,
15124 E_Record_Type_With_Private)
15126 Dep_Elmt := First_Elmt (Private_Dependents (Typ));
15127 while Present (Dep_Elmt) loop
15128 Dep_Typ := Node (Dep_Elmt);
15130 if Scope (Dep_Typ) = Pack_Id
15131 and then Present (Full_View (Dep_Typ))
15133 Replace_Elmt (Dep_Elmt, Full_View (Dep_Typ));
15134 Exchange_Declarations (Dep_Typ);
15137 Next_Elmt (Dep_Elmt);
15141 Exchange_Declarations (Node (M));
15145 if No (Pack_Id) then
15149 -- Make the generic formal parameters private, and make the formal types
15150 -- into subtypes of the actuals again.
15152 E := First_Entity (Pack_Id);
15153 while Present (E) loop
15154 Set_Is_Hidden (E, True);
15157 and then Nkind (Parent (E)) = N_Subtype_Declaration
15159 -- If the actual for E is itself a generic actual type from
15160 -- an enclosing instance, E is still a generic actual type
15161 -- outside of the current instance. This matter when resolving
15162 -- an overloaded call that may be ambiguous in the enclosing
15163 -- instance, when two of its actuals coincide.
15165 if Is_Entity_Name (Subtype_Indication (Parent (E)))
15166 and then Is_Generic_Actual_Type
15167 (Entity (Subtype_Indication (Parent (E))))
15171 Set_Is_Generic_Actual_Type (E, False);
15173 -- It might seem reasonable to clear the Is_Generic_Actual_Type
15174 -- flag also on the Full_View if the type is private, since it
15175 -- was set also on this Full_View. However, this flag is relied
15176 -- upon by Covers to spot "types exported from instantiations"
15177 -- which are implicit Full_Views built for instantiations made
15178 -- on private types and we get type mismatches if we do it when
15179 -- the block exchanging the declarations below triggers ???
15181 -- if Is_Private_Type (E) and then Present (Full_View (E)) then
15182 -- Set_Is_Generic_Actual_Type (Full_View (E), False);
15186 -- An unusual case of aliasing: the actual may also be directly
15187 -- visible in the generic, and be private there, while it is fully
15188 -- visible in the context of the instance. The internal subtype
15189 -- is private in the instance but has full visibility like its
15190 -- parent in the enclosing scope. This enforces the invariant that
15191 -- the privacy status of all private dependents of a type coincide
15192 -- with that of the parent type. This can only happen when a
15193 -- generic child unit is instantiated within a sibling.
15195 if Is_Private_Type (E)
15196 and then not Is_Private_Type (Etype (E))
15198 Exchange_Declarations (E);
15201 elsif Ekind (E) = E_Package then
15203 -- The end of the renaming list is the renaming of the generic
15204 -- package itself. If the instance is a subprogram, all entities
15205 -- in the corresponding package are renamings. If this entity is
15206 -- a formal package, make its own formals private as well. The
15207 -- actual in this case is itself the renaming of an instantiation.
15208 -- If the entity is not a package renaming, it is the entity
15209 -- created to validate formal package actuals: ignore it.
15211 -- If the actual is itself a formal package for the enclosing
15212 -- generic, or the actual for such a formal package, it remains
15213 -- visible on exit from the instance, and therefore nothing needs
15214 -- to be done either, except to keep it accessible.
15216 if Is_Package and then Renamed_Object (E) = Pack_Id then
15219 elsif Nkind (Parent (E)) /= N_Package_Renaming_Declaration then
15223 Denotes_Formal_Package (Renamed_Object (E), True, Pack_Id)
15225 Set_Is_Hidden (E, False);
15229 Act_P : constant Entity_Id := Renamed_Object (E);
15233 Id := First_Entity (Act_P);
15235 and then Id /= First_Private_Entity (Act_P)
15237 exit when Ekind (Id) = E_Package
15238 and then Renamed_Object (Id) = Act_P;
15240 Set_Is_Hidden (Id, True);
15241 Set_Is_Potentially_Use_Visible (Id, In_Use (Act_P));
15243 if Ekind (Id) = E_Package then
15244 Restore_Nested_Formal (Id);
15255 end Restore_Private_Views;
15262 (Gen_Unit : Entity_Id;
15263 Act_Unit : Entity_Id)
15267 Set_Instance_Env (Gen_Unit, Act_Unit);
15270 ----------------------------
15271 -- Save_Global_References --
15272 ----------------------------
15274 procedure Save_Global_References (Templ : Node_Id) is
15276 -- ??? it is horrible to use global variables in highly recursive code
15279 -- The entity of the current associated node
15281 Gen_Scope : Entity_Id;
15282 -- The scope of the generic for which references are being saved
15285 -- The current associated node
15287 function Is_Global (E : Entity_Id) return Boolean;
15288 -- Check whether entity is defined outside of generic unit. Examine the
15289 -- scope of an entity, and the scope of the scope, etc, until we find
15290 -- either Standard, in which case the entity is global, or the generic
15291 -- unit itself, which indicates that the entity is local. If the entity
15292 -- is the generic unit itself, as in the case of a recursive call, or
15293 -- the enclosing generic unit, if different from the current scope, then
15294 -- it is local as well, because it will be replaced at the point of
15295 -- instantiation. On the other hand, if it is a reference to a child
15296 -- unit of a common ancestor, which appears in an instantiation, it is
15297 -- global because it is used to denote a specific compilation unit at
15298 -- the time the instantiations will be analyzed.
15300 procedure Qualify_Universal_Operands
15302 Func_Call : Node_Id);
15303 -- Op denotes a binary or unary operator in generic template Templ. Node
15304 -- Func_Call is the function call alternative of the operator within the
15305 -- the analyzed copy of the template. Change each operand which yields a
15306 -- universal type by wrapping it into a qualified expression
15308 -- Actual_Typ'(Operand)
15310 -- where Actual_Typ is the type of corresponding actual parameter of
15311 -- Operand in Func_Call.
15313 procedure Reset_Entity (N : Node_Id);
15314 -- Save semantic information on global entity so that it is not resolved
15315 -- again at instantiation time.
15317 procedure Save_Entity_Descendants (N : Node_Id);
15318 -- Apply Save_Global_References to the two syntactic descendants of
15319 -- non-terminal nodes that carry an Associated_Node and are processed
15320 -- through Reset_Entity. Once the global entity (if any) has been
15321 -- captured together with its type, only two syntactic descendants need
15322 -- to be traversed to complete the processing of the tree rooted at N.
15323 -- This applies to Selected_Components, Expanded_Names, and to Operator
15324 -- nodes. N can also be a character literal, identifier, or operator
15325 -- symbol node, but the call has no effect in these cases.
15327 procedure Save_Global_Defaults (N1 : Node_Id; N2 : Node_Id);
15328 -- Default actuals in nested instances must be handled specially
15329 -- because there is no link to them from the original tree. When an
15330 -- actual subprogram is given by a default, we add an explicit generic
15331 -- association for it in the instantiation node. When we save the
15332 -- global references on the name of the instance, we recover the list
15333 -- of generic associations, and add an explicit one to the original
15334 -- generic tree, through which a global actual can be preserved.
15335 -- Similarly, if a child unit is instantiated within a sibling, in the
15336 -- context of the parent, we must preserve the identifier of the parent
15337 -- so that it can be properly resolved in a subsequent instantiation.
15339 procedure Save_Global_Descendant (D : Union_Id);
15340 -- Apply Save_References recursively to the descendants of node D
15342 procedure Save_References (N : Node_Id);
15343 -- This is the recursive procedure that does the work, once the
15344 -- enclosing generic scope has been established.
15350 function Is_Global (E : Entity_Id) return Boolean is
15353 function Is_Instance_Node (Decl : Node_Id) return Boolean;
15354 -- Determine whether the parent node of a reference to a child unit
15355 -- denotes an instantiation or a formal package, in which case the
15356 -- reference to the child unit is global, even if it appears within
15357 -- the current scope (e.g. when the instance appears within the body
15358 -- of an ancestor).
15360 ----------------------
15361 -- Is_Instance_Node --
15362 ----------------------
15364 function Is_Instance_Node (Decl : Node_Id) return Boolean is
15366 return Nkind (Decl) in N_Generic_Instantiation
15368 Nkind (Original_Node (Decl)) = N_Formal_Package_Declaration;
15369 end Is_Instance_Node;
15371 -- Start of processing for Is_Global
15374 if E = Gen_Scope then
15377 elsif E = Standard_Standard then
15380 elsif Is_Child_Unit (E)
15381 and then (Is_Instance_Node (Parent (N2))
15382 or else (Nkind (Parent (N2)) = N_Expanded_Name
15383 and then N2 = Selector_Name (Parent (N2))
15385 Is_Instance_Node (Parent (Parent (N2)))))
15391 while Se /= Gen_Scope loop
15392 if Se = Standard_Standard then
15403 --------------------------------
15404 -- Qualify_Universal_Operands --
15405 --------------------------------
15407 procedure Qualify_Universal_Operands
15409 Func_Call : Node_Id)
15411 procedure Qualify_Operand (Opnd : Node_Id; Actual : Node_Id);
15412 -- Rewrite operand Opnd as a qualified expression of the form
15414 -- Actual_Typ'(Opnd)
15416 -- where Actual is the corresponding actual parameter of Opnd in
15417 -- function call Func_Call.
15419 function Qualify_Type
15421 Typ : Entity_Id) return Node_Id;
15422 -- Qualify type Typ by creating a selected component of the form
15424 -- Scope_Of_Typ.Typ
15426 ---------------------
15427 -- Qualify_Operand --
15428 ---------------------
15430 procedure Qualify_Operand (Opnd : Node_Id; Actual : Node_Id) is
15431 Loc : constant Source_Ptr := Sloc (Opnd);
15432 Typ : constant Entity_Id := Etype (Actual);
15437 -- Qualify the operand when it is of a universal type. Note that
15438 -- the template is unanalyzed and it is not possible to directly
15439 -- query the type. This transformation is not done when the type
15440 -- of the actual is internally generated because the type will be
15441 -- regenerated in the instance.
15443 if Yields_Universal_Type (Opnd)
15444 and then Comes_From_Source (Typ)
15445 and then not Is_Hidden (Typ)
15447 -- The type of the actual may be a global reference. Save this
15448 -- information by creating a reference to it.
15450 if Is_Global (Typ) then
15451 Mark := New_Occurrence_Of (Typ, Loc);
15453 -- Otherwise rely on resolution to find the proper type within
15457 Mark := Qualify_Type (Loc, Typ);
15461 Make_Qualified_Expression (Loc,
15462 Subtype_Mark => Mark,
15463 Expression => Relocate_Node (Opnd));
15465 -- Mark the qualification to distinguish it from other source
15466 -- constructs and signal the instantiation mechanism that this
15467 -- node requires special processing. See Copy_Generic_Node for
15470 Set_Is_Qualified_Universal_Literal (Qual);
15472 Rewrite (Opnd, Qual);
15474 end Qualify_Operand;
15480 function Qualify_Type
15482 Typ : Entity_Id) return Node_Id
15484 Scop : constant Entity_Id := Scope (Typ);
15488 Result := Make_Identifier (Loc, Chars (Typ));
15490 if Present (Scop) and then not Is_Generic_Unit (Scop) then
15492 Make_Selected_Component (Loc,
15493 Prefix => Make_Identifier (Loc, Chars (Scop)),
15494 Selector_Name => Result);
15502 Actuals : constant List_Id := Parameter_Associations (Func_Call);
15504 -- Start of processing for Qualify_Universal_Operands
15507 if Nkind (Op) in N_Binary_Op then
15508 Qualify_Operand (Left_Opnd (Op), First (Actuals));
15509 Qualify_Operand (Right_Opnd (Op), Next (First (Actuals)));
15511 elsif Nkind (Op) in N_Unary_Op then
15512 Qualify_Operand (Right_Opnd (Op), First (Actuals));
15514 end Qualify_Universal_Operands;
15520 procedure Reset_Entity (N : Node_Id) is
15521 procedure Set_Global_Type (N : Node_Id; N2 : Node_Id);
15522 -- If the type of N2 is global to the generic unit, save the type in
15523 -- the generic node. Just as we perform name capture for explicit
15524 -- references within the generic, we must capture the global types
15525 -- of local entities because they may participate in resolution in
15528 function Top_Ancestor (E : Entity_Id) return Entity_Id;
15529 -- Find the ultimate ancestor of the current unit. If it is not a
15530 -- generic unit, then the name of the current unit in the prefix of
15531 -- an expanded name must be replaced with its generic homonym to
15532 -- ensure that it will be properly resolved in an instance.
15534 ---------------------
15535 -- Set_Global_Type --
15536 ---------------------
15538 procedure Set_Global_Type (N : Node_Id; N2 : Node_Id) is
15539 Typ : constant Entity_Id := Etype (N2);
15542 Set_Etype (N, Typ);
15544 -- If the entity of N is not the associated node, this is a
15545 -- nested generic and it has an associated node as well, whose
15546 -- type is already the full view (see below). Indicate that the
15547 -- original node has a private view.
15549 if Entity (N) /= N2 and then Has_Private_View (Entity (N)) then
15550 Set_Has_Private_View (N);
15553 -- If not a private type, nothing else to do
15555 if not Is_Private_Type (Typ) then
15558 -- If it is a derivation of a private type in a context where no
15559 -- full view is needed, nothing to do either.
15561 elsif No (Full_View (Typ)) and then Typ /= Etype (Typ) then
15564 -- Otherwise mark the type for flipping and use the full view when
15568 Set_Has_Private_View (N);
15570 if Present (Full_View (Typ)) then
15571 Set_Etype (N2, Full_View (Typ));
15575 if Is_Floating_Point_Type (Typ)
15576 and then Has_Dimension_System (Typ)
15578 Copy_Dimensions (N2, N);
15580 end Set_Global_Type;
15586 function Top_Ancestor (E : Entity_Id) return Entity_Id is
15591 while Is_Child_Unit (Par) loop
15592 Par := Scope (Par);
15598 -- Start of processing for Reset_Entity
15601 N2 := Get_Associated_Node (N);
15604 if Present (E) then
15606 -- If the node is an entry call to an entry in an enclosing task,
15607 -- it is rewritten as a selected component. No global entity to
15608 -- preserve in this case, since the expansion will be redone in
15611 if not Nkind_In (E, N_Defining_Character_Literal,
15612 N_Defining_Identifier,
15613 N_Defining_Operator_Symbol)
15615 Set_Associated_Node (N, Empty);
15616 Set_Etype (N, Empty);
15620 -- If the entity is an itype created as a subtype of an access
15621 -- type with a null exclusion restore source entity for proper
15622 -- visibility. The itype will be created anew in the instance.
15625 and then Ekind (E) = E_Access_Subtype
15626 and then Is_Entity_Name (N)
15627 and then Chars (Etype (E)) = Chars (N)
15630 Set_Entity (N2, E);
15634 if Is_Global (E) then
15635 Set_Global_Type (N, N2);
15637 elsif Nkind (N) = N_Op_Concat
15638 and then Is_Generic_Type (Etype (N2))
15639 and then (Base_Type (Etype (Right_Opnd (N2))) = Etype (N2)
15641 Base_Type (Etype (Left_Opnd (N2))) = Etype (N2))
15642 and then Is_Intrinsic_Subprogram (E)
15646 -- Entity is local. Mark generic node as unresolved. Note that now
15647 -- it does not have an entity.
15650 Set_Associated_Node (N, Empty);
15651 Set_Etype (N, Empty);
15654 if Nkind (Parent (N)) in N_Generic_Instantiation
15655 and then N = Name (Parent (N))
15657 Save_Global_Defaults (Parent (N), Parent (N2));
15660 elsif Nkind (Parent (N)) = N_Selected_Component
15661 and then Nkind (Parent (N2)) = N_Expanded_Name
15663 if Is_Global (Entity (Parent (N2))) then
15664 Change_Selected_Component_To_Expanded_Name (Parent (N));
15665 Set_Associated_Node (Parent (N), Parent (N2));
15666 Set_Global_Type (Parent (N), Parent (N2));
15667 Save_Entity_Descendants (N);
15669 -- If this is a reference to the current generic entity, replace
15670 -- by the name of the generic homonym of the current package. This
15671 -- is because in an instantiation Par.P.Q will not resolve to the
15672 -- name of the instance, whose enclosing scope is not necessarily
15673 -- Par. We use the generic homonym rather that the name of the
15674 -- generic itself because it may be hidden by a local declaration.
15676 elsif In_Open_Scopes (Entity (Parent (N2)))
15678 Is_Generic_Unit (Top_Ancestor (Entity (Prefix (Parent (N2)))))
15680 if Ekind (Entity (Parent (N2))) = E_Generic_Package then
15681 Rewrite (Parent (N),
15682 Make_Identifier (Sloc (N),
15684 Chars (Generic_Homonym (Entity (Parent (N2))))));
15686 Rewrite (Parent (N),
15687 Make_Identifier (Sloc (N),
15688 Chars => Chars (Selector_Name (Parent (N2)))));
15692 if Nkind (Parent (Parent (N))) in N_Generic_Instantiation
15693 and then Parent (N) = Name (Parent (Parent (N)))
15695 Save_Global_Defaults
15696 (Parent (Parent (N)), Parent (Parent (N2)));
15699 -- A selected component may denote a static constant that has been
15700 -- folded. If the static constant is global to the generic, capture
15701 -- its value. Otherwise the folding will happen in any instantiation.
15703 elsif Nkind (Parent (N)) = N_Selected_Component
15704 and then Nkind_In (Parent (N2), N_Integer_Literal, N_Real_Literal)
15706 if Present (Entity (Original_Node (Parent (N2))))
15707 and then Is_Global (Entity (Original_Node (Parent (N2))))
15709 Rewrite (Parent (N), New_Copy (Parent (N2)));
15710 Set_Analyzed (Parent (N), False);
15713 -- A selected component may be transformed into a parameterless
15714 -- function call. If the called entity is global, rewrite the node
15715 -- appropriately, i.e. as an extended name for the global entity.
15717 elsif Nkind (Parent (N)) = N_Selected_Component
15718 and then Nkind (Parent (N2)) = N_Function_Call
15719 and then N = Selector_Name (Parent (N))
15721 if No (Parameter_Associations (Parent (N2))) then
15722 if Is_Global (Entity (Name (Parent (N2)))) then
15723 Change_Selected_Component_To_Expanded_Name (Parent (N));
15724 Set_Associated_Node (Parent (N), Name (Parent (N2)));
15725 Set_Global_Type (Parent (N), Name (Parent (N2)));
15726 Save_Entity_Descendants (N);
15729 Set_Is_Prefixed_Call (Parent (N));
15730 Set_Associated_Node (N, Empty);
15731 Set_Etype (N, Empty);
15734 -- In Ada 2005, X.F may be a call to a primitive operation,
15735 -- rewritten as F (X). This rewriting will be done again in an
15736 -- instance, so keep the original node. Global entities will be
15737 -- captured as for other constructs. Indicate that this must
15738 -- resolve as a call, to prevent accidental overloading in the
15739 -- instance, if both a component and a primitive operation appear
15743 Set_Is_Prefixed_Call (Parent (N));
15746 -- Entity is local. Reset in generic unit, so that node is resolved
15747 -- anew at the point of instantiation.
15750 Set_Associated_Node (N, Empty);
15751 Set_Etype (N, Empty);
15755 -----------------------------
15756 -- Save_Entity_Descendants --
15757 -----------------------------
15759 procedure Save_Entity_Descendants (N : Node_Id) is
15762 when N_Binary_Op =>
15763 Save_Global_Descendant (Union_Id (Left_Opnd (N)));
15764 Save_Global_Descendant (Union_Id (Right_Opnd (N)));
15767 Save_Global_Descendant (Union_Id (Right_Opnd (N)));
15769 when N_Expanded_Name
15770 | N_Selected_Component
15772 Save_Global_Descendant (Union_Id (Prefix (N)));
15773 Save_Global_Descendant (Union_Id (Selector_Name (N)));
15775 when N_Character_Literal
15777 | N_Operator_Symbol
15782 raise Program_Error;
15784 end Save_Entity_Descendants;
15786 --------------------------
15787 -- Save_Global_Defaults --
15788 --------------------------
15790 procedure Save_Global_Defaults (N1 : Node_Id; N2 : Node_Id) is
15791 Loc : constant Source_Ptr := Sloc (N1);
15792 Assoc2 : constant List_Id := Generic_Associations (N2);
15793 Gen_Id : constant Entity_Id := Get_Generic_Entity (N2);
15800 Actual : Entity_Id;
15803 Assoc1 := Generic_Associations (N1);
15805 if Present (Assoc1) then
15806 Act1 := First (Assoc1);
15809 Set_Generic_Associations (N1, New_List);
15810 Assoc1 := Generic_Associations (N1);
15813 if Present (Assoc2) then
15814 Act2 := First (Assoc2);
15819 while Present (Act1) and then Present (Act2) loop
15824 -- Find the associations added for default subprograms
15826 if Present (Act2) then
15827 while Nkind (Act2) /= N_Generic_Association
15828 or else No (Entity (Selector_Name (Act2)))
15829 or else not Is_Overloadable (Entity (Selector_Name (Act2)))
15834 -- Add a similar association if the default is global. The
15835 -- renaming declaration for the actual has been analyzed, and
15836 -- its alias is the program it renames. Link the actual in the
15837 -- original generic tree with the node in the analyzed tree.
15839 while Present (Act2) loop
15840 Subp := Entity (Selector_Name (Act2));
15841 Def := Explicit_Generic_Actual_Parameter (Act2);
15843 -- Following test is defence against rubbish errors
15845 if No (Alias (Subp)) then
15849 -- Retrieve the resolved actual from the renaming declaration
15850 -- created for the instantiated formal.
15852 Actual := Entity (Name (Parent (Parent (Subp))));
15853 Set_Entity (Def, Actual);
15854 Set_Etype (Def, Etype (Actual));
15856 if Is_Global (Actual) then
15858 Make_Generic_Association (Loc,
15860 New_Occurrence_Of (Subp, Loc),
15861 Explicit_Generic_Actual_Parameter =>
15862 New_Occurrence_Of (Actual, Loc));
15864 Set_Associated_Node
15865 (Explicit_Generic_Actual_Parameter (Ndec), Def);
15867 Append (Ndec, Assoc1);
15869 -- If there are other defaults, add a dummy association in case
15870 -- there are other defaulted formals with the same name.
15872 elsif Present (Next (Act2)) then
15874 Make_Generic_Association (Loc,
15876 New_Occurrence_Of (Subp, Loc),
15877 Explicit_Generic_Actual_Parameter => Empty);
15879 Append (Ndec, Assoc1);
15886 if Nkind (Name (N1)) = N_Identifier
15887 and then Is_Child_Unit (Gen_Id)
15888 and then Is_Global (Gen_Id)
15889 and then Is_Generic_Unit (Scope (Gen_Id))
15890 and then In_Open_Scopes (Scope (Gen_Id))
15892 -- This is an instantiation of a child unit within a sibling, so
15893 -- that the generic parent is in scope. An eventual instance must
15894 -- occur within the scope of an instance of the parent. Make name
15895 -- in instance into an expanded name, to preserve the identifier
15896 -- of the parent, so it can be resolved subsequently.
15898 Rewrite (Name (N2),
15899 Make_Expanded_Name (Loc,
15900 Chars => Chars (Gen_Id),
15901 Prefix => New_Occurrence_Of (Scope (Gen_Id), Loc),
15902 Selector_Name => New_Occurrence_Of (Gen_Id, Loc)));
15903 Set_Entity (Name (N2), Gen_Id);
15905 Rewrite (Name (N1),
15906 Make_Expanded_Name (Loc,
15907 Chars => Chars (Gen_Id),
15908 Prefix => New_Occurrence_Of (Scope (Gen_Id), Loc),
15909 Selector_Name => New_Occurrence_Of (Gen_Id, Loc)));
15911 Set_Associated_Node (Name (N1), Name (N2));
15912 Set_Associated_Node (Prefix (Name (N1)), Empty);
15913 Set_Associated_Node
15914 (Selector_Name (Name (N1)), Selector_Name (Name (N2)));
15915 Set_Etype (Name (N1), Etype (Gen_Id));
15917 end Save_Global_Defaults;
15919 ----------------------------
15920 -- Save_Global_Descendant --
15921 ----------------------------
15923 procedure Save_Global_Descendant (D : Union_Id) is
15927 if D in Node_Range then
15928 if D = Union_Id (Empty) then
15931 elsif Nkind (Node_Id (D)) /= N_Compilation_Unit then
15932 Save_References (Node_Id (D));
15935 elsif D in List_Range then
15936 pragma Assert (D /= Union_Id (No_List));
15937 -- Because No_List = Empty, which is in Node_Range above
15939 if Is_Empty_List (List_Id (D)) then
15943 N1 := First (List_Id (D));
15944 while Present (N1) loop
15945 Save_References (N1);
15950 -- Element list or other non-node field, nothing to do
15955 end Save_Global_Descendant;
15957 ---------------------
15958 -- Save_References --
15959 ---------------------
15961 -- This is the recursive procedure that does the work once the enclosing
15962 -- generic scope has been established. We have to treat specially a
15963 -- number of node rewritings that are required by semantic processing
15964 -- and which change the kind of nodes in the generic copy: typically
15965 -- constant-folding, replacing an operator node by a string literal, or
15966 -- a selected component by an expanded name. In each of those cases, the
15967 -- transformation is propagated to the generic unit.
15969 procedure Save_References (N : Node_Id) is
15970 Loc : constant Source_Ptr := Sloc (N);
15972 function Requires_Delayed_Save (Nod : Node_Id) return Boolean;
15973 -- Determine whether arbitrary node Nod requires delayed capture of
15974 -- global references within its aspect specifications.
15976 procedure Save_References_In_Aggregate (N : Node_Id);
15977 -- Save all global references in [extension] aggregate node N
15979 procedure Save_References_In_Char_Lit_Or_Op_Symbol (N : Node_Id);
15980 -- Save all global references in a character literal or operator
15981 -- symbol denoted by N.
15983 procedure Save_References_In_Descendants (N : Node_Id);
15984 -- Save all global references in all descendants of node N
15986 procedure Save_References_In_Identifier (N : Node_Id);
15987 -- Save all global references in identifier node N
15989 procedure Save_References_In_Operator (N : Node_Id);
15990 -- Save all global references in operator node N
15992 procedure Save_References_In_Pragma (Prag : Node_Id);
15993 -- Save all global references found within the expression of pragma
15996 ---------------------------
15997 -- Requires_Delayed_Save --
15998 ---------------------------
16000 function Requires_Delayed_Save (Nod : Node_Id) return Boolean is
16002 -- Generic packages and subprograms require delayed capture of
16003 -- global references within their aspects due to the timing of
16004 -- annotation analysis.
16006 if Nkind_In (Nod, N_Generic_Package_Declaration,
16007 N_Generic_Subprogram_Declaration,
16009 N_Package_Body_Stub,
16011 N_Subprogram_Body_Stub)
16013 -- Since the capture of global references is done on the
16014 -- unanalyzed generic template, there is no information around
16015 -- to infer the context. Use the Associated_Entity linkages to
16016 -- peek into the analyzed generic copy and determine what the
16017 -- template corresponds to.
16019 if Nod = Templ then
16021 Is_Generic_Declaration_Or_Body
16022 (Unit_Declaration_Node
16023 (Associated_Entity (Defining_Entity (Nod))));
16025 -- Otherwise the generic unit being processed is not the top
16026 -- level template. It is safe to capture of global references
16027 -- within the generic unit because at this point the top level
16028 -- copy is fully analyzed.
16034 -- Otherwise capture the global references without interference
16039 end Requires_Delayed_Save;
16041 ----------------------------------
16042 -- Save_References_In_Aggregate --
16043 ----------------------------------
16045 procedure Save_References_In_Aggregate (N : Node_Id) is
16047 Qual : Node_Id := Empty;
16048 Typ : Entity_Id := Empty;
16050 use Atree.Unchecked_Access;
16051 -- This code section is part of implementing an untyped tree
16052 -- traversal, so it needs direct access to node fields.
16055 N2 := Get_Associated_Node (N);
16057 if Present (N2) then
16060 -- In an instance within a generic, use the name of the actual
16061 -- and not the original generic parameter. If the actual is
16062 -- global in the current generic it must be preserved for its
16065 if Nkind (Parent (Typ)) = N_Subtype_Declaration
16066 and then Present (Generic_Parent_Type (Parent (Typ)))
16068 Typ := Base_Type (Typ);
16069 Set_Etype (N2, Typ);
16073 if No (N2) or else No (Typ) or else not Is_Global (Typ) then
16074 Set_Associated_Node (N, Empty);
16076 -- If the aggregate is an actual in a call, it has been
16077 -- resolved in the current context, to some local type. The
16078 -- enclosing call may have been disambiguated by the aggregate,
16079 -- and this disambiguation might fail at instantiation time
16080 -- because the type to which the aggregate did resolve is not
16081 -- preserved. In order to preserve some of this information,
16082 -- wrap the aggregate in a qualified expression, using the id
16083 -- of its type. For further disambiguation we qualify the type
16084 -- name with its scope (if visible and not hidden by a local
16085 -- homograph) because both id's will have corresponding
16086 -- entities in an instance. This resolves most of the problems
16087 -- with missing type information on aggregates in instances.
16090 and then Nkind (N2) = Nkind (N)
16091 and then Nkind (Parent (N2)) in N_Subprogram_Call
16092 and then Present (Typ)
16093 and then Comes_From_Source (Typ)
16095 Nam := Make_Identifier (Loc, Chars (Typ));
16097 if Is_Immediately_Visible (Scope (Typ))
16099 (not In_Open_Scopes (Scope (Typ))
16100 or else Current_Entity (Scope (Typ)) = Scope (Typ))
16103 Make_Selected_Component (Loc,
16105 Make_Identifier (Loc, Chars (Scope (Typ))),
16106 Selector_Name => Nam);
16110 Make_Qualified_Expression (Loc,
16111 Subtype_Mark => Nam,
16112 Expression => Relocate_Node (N));
16116 Save_Global_Descendant (Field1 (N));
16117 Save_Global_Descendant (Field2 (N));
16118 Save_Global_Descendant (Field3 (N));
16119 Save_Global_Descendant (Field5 (N));
16121 if Present (Qual) then
16124 end Save_References_In_Aggregate;
16126 ----------------------------------------------
16127 -- Save_References_In_Char_Lit_Or_Op_Symbol --
16128 ----------------------------------------------
16130 procedure Save_References_In_Char_Lit_Or_Op_Symbol (N : Node_Id) is
16132 if Nkind (N) = Nkind (Get_Associated_Node (N)) then
16135 elsif Nkind (N) = N_Operator_Symbol
16136 and then Nkind (Get_Associated_Node (N)) = N_String_Literal
16138 Change_Operator_Symbol_To_String_Literal (N);
16140 end Save_References_In_Char_Lit_Or_Op_Symbol;
16142 ------------------------------------
16143 -- Save_References_In_Descendants --
16144 ------------------------------------
16146 procedure Save_References_In_Descendants (N : Node_Id) is
16147 use Atree.Unchecked_Access;
16148 -- This code section is part of implementing an untyped tree
16149 -- traversal, so it needs direct access to node fields.
16152 Save_Global_Descendant (Field1 (N));
16153 Save_Global_Descendant (Field2 (N));
16154 Save_Global_Descendant (Field3 (N));
16155 Save_Global_Descendant (Field4 (N));
16156 Save_Global_Descendant (Field5 (N));
16157 end Save_References_In_Descendants;
16159 -----------------------------------
16160 -- Save_References_In_Identifier --
16161 -----------------------------------
16163 procedure Save_References_In_Identifier (N : Node_Id) is
16165 -- The node did not undergo a transformation
16167 if Nkind (N) = Nkind (Get_Associated_Node (N)) then
16168 -- If this is a discriminant reference, always save it.
16169 -- It is used in the instance to find the corresponding
16170 -- discriminant positionally rather than by name.
16172 Set_Original_Discriminant
16173 (N, Original_Discriminant (Get_Associated_Node (N)));
16177 -- The analysis of the generic copy transformed the identifier
16178 -- into another construct. Propagate the changes to the template.
16181 N2 := Get_Associated_Node (N);
16183 -- The identifier denotes a call to a parameterless function.
16184 -- Mark the node as resolved when the function is external.
16186 if Nkind (N2) = N_Function_Call then
16187 E := Entity (Name (N2));
16189 if Present (E) and then Is_Global (E) then
16190 Set_Etype (N, Etype (N2));
16192 Set_Associated_Node (N, Empty);
16193 Set_Etype (N, Empty);
16196 -- The identifier denotes a named number that was constant
16197 -- folded. Preserve the original name for ASIS and undo the
16198 -- constant folding which will be repeated in the instance.
16199 -- Is this still needed???
16201 elsif Nkind_In (N2, N_Integer_Literal, N_Real_Literal)
16202 and then Is_Entity_Name (Original_Node (N2))
16204 Set_Associated_Node (N, Original_Node (N2));
16207 -- The identifier resolved to a string literal. Propagate this
16208 -- information to the generic template.
16210 elsif Nkind (N2) = N_String_Literal then
16211 Rewrite (N, New_Copy (N2));
16213 -- The identifier is rewritten as a dereference if it is the
16214 -- prefix of an implicit dereference. Preserve the original
16215 -- tree as the analysis of the instance will expand the node
16216 -- again, but preserve the resolved entity if it is global.
16218 elsif Nkind (N2) = N_Explicit_Dereference then
16219 if Is_Entity_Name (Prefix (N2))
16220 and then Present (Entity (Prefix (N2)))
16221 and then Is_Global (Entity (Prefix (N2)))
16223 Set_Associated_Node (N, Prefix (N2));
16225 elsif Nkind (Prefix (N2)) = N_Function_Call
16226 and then Present (Entity (Name (Prefix (N2))))
16227 and then Is_Global (Entity (Name (Prefix (N2))))
16230 Make_Explicit_Dereference (Loc,
16232 Make_Function_Call (Loc,
16235 (Entity (Name (Prefix (N2))), Loc))));
16238 Set_Associated_Node (N, Empty);
16239 Set_Etype (N, Empty);
16242 -- The subtype mark of a nominally unconstrained object is
16243 -- rewritten as a subtype indication using the bounds of the
16244 -- expression. Recover the original subtype mark.
16246 elsif Nkind (N2) = N_Subtype_Indication
16247 and then Is_Entity_Name (Original_Node (N2))
16249 Set_Associated_Node (N, Original_Node (N2));
16253 end Save_References_In_Identifier;
16255 ---------------------------------
16256 -- Save_References_In_Operator --
16257 ---------------------------------
16259 procedure Save_References_In_Operator (N : Node_Id) is
16261 -- The node did not undergo a transformation
16263 if Nkind (N) = Nkind (Get_Associated_Node (N)) then
16264 if Nkind (N) = N_Op_Concat then
16265 Set_Is_Component_Left_Opnd (N,
16266 Is_Component_Left_Opnd (Get_Associated_Node (N)));
16268 Set_Is_Component_Right_Opnd (N,
16269 Is_Component_Right_Opnd (Get_Associated_Node (N)));
16274 -- The analysis of the generic copy transformed the operator into
16275 -- some other construct. Propagate the changes to the template if
16279 N2 := Get_Associated_Node (N);
16281 -- The operator resoved to a function call
16283 if Nkind (N2) = N_Function_Call then
16285 -- Add explicit qualifications in the generic template for
16286 -- all operands of universal type. This aids resolution by
16287 -- preserving the actual type of a literal or an attribute
16288 -- that yields a universal result.
16290 Qualify_Universal_Operands (N, N2);
16292 E := Entity (Name (N2));
16294 if Present (E) and then Is_Global (E) then
16295 Set_Etype (N, Etype (N2));
16297 Set_Associated_Node (N, Empty);
16298 Set_Etype (N, Empty);
16301 -- The operator was folded into a literal
16303 elsif Nkind_In (N2, N_Integer_Literal,
16307 if Present (Original_Node (N2))
16308 and then Nkind (Original_Node (N2)) = Nkind (N)
16310 -- Operation was constant-folded. Whenever possible,
16311 -- recover semantic information from unfolded node.
16312 -- This was initially done for ASIS but is apparently
16313 -- needed also for e.g. compiling a-nbnbin.adb.
16315 Set_Associated_Node (N, Original_Node (N2));
16317 if Nkind (N) = N_Op_Concat then
16318 Set_Is_Component_Left_Opnd (N,
16319 Is_Component_Left_Opnd (Get_Associated_Node (N)));
16320 Set_Is_Component_Right_Opnd (N,
16321 Is_Component_Right_Opnd (Get_Associated_Node (N)));
16326 -- Propagate the constant folding back to the template
16329 Rewrite (N, New_Copy (N2));
16330 Set_Analyzed (N, False);
16333 -- The operator was folded into an enumeration literal. Retain
16334 -- the entity to avoid spurious ambiguities if it is overloaded
16335 -- at the point of instantiation or inlining.
16337 elsif Nkind (N2) = N_Identifier
16338 and then Ekind (Entity (N2)) = E_Enumeration_Literal
16340 Rewrite (N, New_Copy (N2));
16341 Set_Analyzed (N, False);
16345 -- Complete the operands check if node has not been constant
16348 if Nkind (N) in N_Op then
16349 Save_Entity_Descendants (N);
16351 end Save_References_In_Operator;
16353 -------------------------------
16354 -- Save_References_In_Pragma --
16355 -------------------------------
16357 procedure Save_References_In_Pragma (Prag : Node_Id) is
16359 Do_Save : Boolean := True;
16361 use Atree.Unchecked_Access;
16362 -- This code section is part of implementing an untyped tree
16363 -- traversal, so it needs direct access to node fields.
16366 -- Do not save global references in pragmas generated from aspects
16367 -- because the pragmas will be regenerated at instantiation time.
16369 if From_Aspect_Specification (Prag) then
16372 -- The capture of global references within contract-related source
16373 -- pragmas associated with generic packages, subprograms or their
16374 -- respective bodies must be delayed due to timing of annotation
16375 -- analysis. Global references are still captured in routine
16376 -- Save_Global_References_In_Contract.
16378 elsif Is_Generic_Contract_Pragma (Prag) and then Prag /= Templ then
16379 if Is_Package_Contract_Annotation (Prag) then
16380 Context := Find_Related_Package_Or_Body (Prag);
16382 pragma Assert (Is_Subprogram_Contract_Annotation (Prag));
16383 Context := Find_Related_Declaration_Or_Body (Prag);
16386 -- The use of Original_Node accounts for the case when the
16387 -- related context is generic template.
16389 if Requires_Delayed_Save (Original_Node (Context)) then
16394 -- For all other cases, save all global references within the
16395 -- descendants, but skip the following semantic fields:
16397 -- Field1 - Next_Pragma
16398 -- Field3 - Corresponding_Aspect
16399 -- Field5 - Next_Rep_Item
16402 Save_Global_Descendant (Field2 (Prag));
16403 Save_Global_Descendant (Field4 (Prag));
16405 end Save_References_In_Pragma;
16407 -- Start of processing for Save_References
16415 elsif Nkind_In (N, N_Aggregate, N_Extension_Aggregate) then
16416 Save_References_In_Aggregate (N);
16418 -- Character literals, operator symbols
16420 elsif Nkind_In (N, N_Character_Literal, N_Operator_Symbol) then
16421 Save_References_In_Char_Lit_Or_Op_Symbol (N);
16423 -- Defining identifiers
16425 elsif Nkind (N) in N_Entity then
16430 elsif Nkind (N) = N_Identifier then
16431 Save_References_In_Identifier (N);
16435 elsif Nkind (N) in N_Op then
16436 Save_References_In_Operator (N);
16440 elsif Nkind (N) = N_Pragma then
16441 Save_References_In_Pragma (N);
16444 Save_References_In_Descendants (N);
16447 -- Save all global references found within the aspect specifications
16448 -- of the related node.
16450 if Permits_Aspect_Specifications (N) and then Has_Aspects (N) then
16452 -- The capture of global references within aspects associated with
16453 -- generic packages, subprograms or their bodies must be delayed
16454 -- due to timing of annotation analysis. Global references are
16455 -- still captured in routine Save_Global_References_In_Contract.
16457 if Requires_Delayed_Save (N) then
16460 -- Otherwise save all global references within the aspects
16463 Save_Global_References_In_Aspects (N);
16466 end Save_References;
16468 -- Start of processing for Save_Global_References
16471 Gen_Scope := Current_Scope;
16473 -- If the generic unit is a child unit, references to entities in the
16474 -- parent are treated as local, because they will be resolved anew in
16475 -- the context of the instance of the parent.
16477 while Is_Child_Unit (Gen_Scope)
16478 and then Ekind (Scope (Gen_Scope)) = E_Generic_Package
16480 Gen_Scope := Scope (Gen_Scope);
16483 Save_References (Templ);
16484 end Save_Global_References;
16486 ---------------------------------------
16487 -- Save_Global_References_In_Aspects --
16488 ---------------------------------------
16490 procedure Save_Global_References_In_Aspects (N : Node_Id) is
16495 Asp := First (Aspect_Specifications (N));
16496 while Present (Asp) loop
16497 Expr := Expression (Asp);
16499 if Present (Expr) then
16500 Save_Global_References (Expr);
16505 end Save_Global_References_In_Aspects;
16507 ------------------------------------------
16508 -- Set_Copied_Sloc_For_Inherited_Pragma --
16509 ------------------------------------------
16511 procedure Set_Copied_Sloc_For_Inherited_Pragma
16516 Create_Instantiation_Source (N, E,
16517 Inlined_Body => False,
16518 Inherited_Pragma => True,
16519 Factor => S_Adjustment);
16520 end Set_Copied_Sloc_For_Inherited_Pragma;
16522 --------------------------------------
16523 -- Set_Copied_Sloc_For_Inlined_Body --
16524 --------------------------------------
16526 procedure Set_Copied_Sloc_For_Inlined_Body (N : Node_Id; E : Entity_Id) is
16528 Create_Instantiation_Source (N, E,
16529 Inlined_Body => True,
16530 Inherited_Pragma => False,
16531 Factor => S_Adjustment);
16532 end Set_Copied_Sloc_For_Inlined_Body;
16534 ---------------------
16535 -- Set_Instance_Of --
16536 ---------------------
16538 procedure Set_Instance_Of (A : Entity_Id; B : Entity_Id) is
16540 Generic_Renamings.Table (Generic_Renamings.Last) := (A, B, Assoc_Null);
16541 Generic_Renamings_HTable.Set (Generic_Renamings.Last);
16542 Generic_Renamings.Increment_Last;
16543 end Set_Instance_Of;
16545 --------------------
16546 -- Set_Next_Assoc --
16547 --------------------
16549 procedure Set_Next_Assoc (E : Assoc_Ptr; Next : Assoc_Ptr) is
16551 Generic_Renamings.Table (E).Next_In_HTable := Next;
16552 end Set_Next_Assoc;
16554 -------------------
16555 -- Start_Generic --
16556 -------------------
16558 procedure Start_Generic is
16560 -- ??? More things could be factored out in this routine.
16561 -- Should probably be done at a later stage.
16563 Generic_Flags.Append (Inside_A_Generic);
16564 Inside_A_Generic := True;
16566 Expander_Mode_Save_And_Set (False);
16569 ----------------------
16570 -- Set_Instance_Env --
16571 ----------------------
16573 -- WARNING: This routine manages SPARK regions
16575 procedure Set_Instance_Env
16576 (Gen_Unit : Entity_Id;
16577 Act_Unit : Entity_Id)
16579 Saved_AE : constant Boolean := Assertions_Enabled;
16580 Saved_CPL : constant Node_Id := Check_Policy_List;
16581 Saved_DEC : constant Boolean := Dynamic_Elaboration_Checks;
16582 Saved_SM : constant SPARK_Mode_Type := SPARK_Mode;
16583 Saved_SMP : constant Node_Id := SPARK_Mode_Pragma;
16586 -- Regardless of the current mode, predefined units are analyzed in the
16587 -- most current Ada mode, and earlier version Ada checks do not apply
16588 -- to predefined units. Nothing needs to be done for non-internal units.
16589 -- These are always analyzed in the current mode.
16591 if In_Internal_Unit (Gen_Unit) then
16593 -- The following call resets all configuration attributes to default
16594 -- or the xxx_Config versions of the attributes when the current sem
16595 -- unit is the main unit. At the same time, internal units must also
16596 -- inherit certain configuration attributes from their context. It
16597 -- is unclear what these two sets are.
16599 Set_Config_Switches (True, Current_Sem_Unit = Main_Unit);
16601 -- Reinstall relevant configuration attributes of the context
16603 Assertions_Enabled := Saved_AE;
16604 Check_Policy_List := Saved_CPL;
16605 Dynamic_Elaboration_Checks := Saved_DEC;
16607 Install_SPARK_Mode (Saved_SM, Saved_SMP);
16610 Current_Instantiated_Parent :=
16611 (Gen_Id => Gen_Unit,
16612 Act_Id => Act_Unit,
16613 Next_In_HTable => Assoc_Null);
16614 end Set_Instance_Env;
16620 procedure Switch_View (T : Entity_Id) is
16621 BT : constant Entity_Id := Base_Type (T);
16622 Priv_Elmt : Elmt_Id := No_Elmt;
16623 Priv_Sub : Entity_Id;
16626 -- T may be private but its base type may have been exchanged through
16627 -- some other occurrence, in which case there is nothing to switch
16628 -- besides T itself. Note that a private dependent subtype of a private
16629 -- type might not have been switched even if the base type has been,
16630 -- because of the last branch of Check_Private_View (see comment there).
16632 if not Is_Private_Type (BT) then
16633 Prepend_Elmt (Full_View (T), Exchanged_Views);
16634 Exchange_Declarations (T);
16638 Priv_Elmt := First_Elmt (Private_Dependents (BT));
16640 if Present (Full_View (BT)) then
16641 Prepend_Elmt (Full_View (BT), Exchanged_Views);
16642 Exchange_Declarations (BT);
16645 while Present (Priv_Elmt) loop
16646 Priv_Sub := (Node (Priv_Elmt));
16648 -- We avoid flipping the subtype if the Etype of its full view is
16649 -- private because this would result in a malformed subtype. This
16650 -- occurs when the Etype of the subtype full view is the full view of
16651 -- the base type (and since the base types were just switched, the
16652 -- subtype is pointing to the wrong view). This is currently the case
16653 -- for tagged record types, access types (maybe more?) and needs to
16654 -- be resolved. ???
16656 if Present (Full_View (Priv_Sub))
16657 and then not Is_Private_Type (Etype (Full_View (Priv_Sub)))
16659 Prepend_Elmt (Full_View (Priv_Sub), Exchanged_Views);
16660 Exchange_Declarations (Priv_Sub);
16663 Next_Elmt (Priv_Elmt);
16671 function True_Parent (N : Node_Id) return Node_Id is
16673 if Nkind (Parent (N)) = N_Subunit then
16674 return Parent (Corresponding_Stub (Parent (N)));
16680 -----------------------------
16681 -- Valid_Default_Attribute --
16682 -----------------------------
16684 procedure Valid_Default_Attribute (Nam : Entity_Id; Def : Node_Id) is
16685 Attr_Id : constant Attribute_Id :=
16686 Get_Attribute_Id (Attribute_Name (Def));
16687 T : constant Entity_Id := Entity (Prefix (Def));
16688 Is_Fun : constant Boolean := (Ekind (Nam) = E_Function);
16694 if No (T) or else T = Any_Id then
16699 F := First_Formal (Nam);
16700 while Present (F) loop
16701 Num_F := Num_F + 1;
16706 when Attribute_Adjacent
16707 | Attribute_Ceiling
16708 | Attribute_Copy_Sign
16710 | Attribute_Fraction
16711 | Attribute_Machine
16713 | Attribute_Remainder
16714 | Attribute_Rounding
16715 | Attribute_Unbiased_Rounding
16719 and then Is_Floating_Point_Type (T);
16721 when Attribute_Image
16725 | Attribute_Wide_Image
16726 | Attribute_Wide_Value
16728 OK := Is_Fun and then Num_F = 1 and then Is_Scalar_Type (T);
16733 OK := Is_Fun and then Num_F = 2 and then Is_Scalar_Type (T);
16735 when Attribute_Input =>
16736 OK := (Is_Fun and then Num_F = 1);
16738 when Attribute_Output
16739 | Attribute_Put_Image
16743 OK := not Is_Fun and then Num_F = 2;
16751 ("attribute reference has wrong profile for subprogram", Def);
16753 end Valid_Default_Attribute;