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 procedure Check_Access_Definition (N : Node_Id);
499 -- Subsidiary routine to null exclusion processing. Perform an assertion
500 -- check on Ada version and the presence of an access definition in N.
502 procedure Check_Formal_Packages (P_Id : Entity_Id);
503 -- Apply the following to all formal packages in generic associations.
504 -- Restore the visibility of the formals of the instance that are not
505 -- defaulted (see RM 12.7 (10)). Remove the anonymous package declaration
506 -- created for formal instances that are not defaulted.
508 procedure Check_Formal_Package_Instance
509 (Formal_Pack : Entity_Id;
510 Actual_Pack : Entity_Id);
511 -- Verify that the actuals of the actual instance match the actuals of
512 -- the template for a formal package that is not declared with a box.
514 procedure Check_Forward_Instantiation (Decl : Node_Id);
515 -- If the generic is a local entity and the corresponding body has not
516 -- been seen yet, flag enclosing packages to indicate that it will be
517 -- elaborated after the generic body. Subprograms declared in the same
518 -- package cannot be inlined by the front end because front-end inlining
519 -- requires a strict linear order of elaboration.
521 function Check_Hidden_Primitives (Assoc_List : List_Id) return Elist_Id;
522 -- Check if some association between formals and actuals requires to make
523 -- visible primitives of a tagged type, and make those primitives visible.
524 -- Return the list of primitives whose visibility is modified (to restore
525 -- their visibility later through Restore_Hidden_Primitives). If no
526 -- candidate is found then return No_Elist.
528 procedure Check_Hidden_Child_Unit
530 Gen_Unit : Entity_Id;
531 Act_Decl_Id : Entity_Id);
532 -- If the generic unit is an implicit child instance within a parent
533 -- instance, we need to make an explicit test that it is not hidden by
534 -- a child instance of the same name and parent.
536 procedure Check_Generic_Actuals
537 (Instance : Entity_Id;
538 Is_Formal_Box : Boolean);
539 -- Similar to previous one. Check the actuals in the instantiation,
540 -- whose views can change between the point of instantiation and the point
541 -- of instantiation of the body. In addition, mark the generic renamings
542 -- as generic actuals, so that they are not compatible with other actuals.
543 -- Recurse on an actual that is a formal package whose declaration has
546 function Contains_Instance_Of
549 N : Node_Id) return Boolean;
550 -- Inner is instantiated within the generic Outer. Check whether Inner
551 -- directly or indirectly contains an instance of Outer or of one of its
552 -- parents, in the case of a subunit. Each generic unit holds a list of
553 -- the entities instantiated within (at any depth). This procedure
554 -- determines whether the set of such lists contains a cycle, i.e. an
555 -- illegal circular instantiation.
557 function Denotes_Formal_Package
559 On_Exit : Boolean := False;
560 Instance : Entity_Id := Empty) return Boolean;
561 -- Returns True if E is a formal package of an enclosing generic, or
562 -- the actual for such a formal in an enclosing instantiation. If such
563 -- a package is used as a formal in an nested generic, or as an actual
564 -- in a nested instantiation, the visibility of ITS formals should not
565 -- be modified. When called from within Restore_Private_Views, the flag
566 -- On_Exit is true, to indicate that the search for a possible enclosing
567 -- instance should ignore the current one. In that case Instance denotes
568 -- the declaration for which this is an actual. This declaration may be
569 -- an instantiation in the source, or the internal instantiation that
570 -- corresponds to the actual for a formal package.
572 function Earlier (N1, N2 : Node_Id) return Boolean;
573 -- Yields True if N1 and N2 appear in the same compilation unit,
574 -- ignoring subunits, and if N1 is to the left of N2 in a left-to-right
575 -- traversal of the tree for the unit. Used to determine the placement
576 -- of freeze nodes for instance bodies that may depend on other instances.
578 function Find_Actual_Type
580 Gen_Type : Entity_Id) return Entity_Id;
581 -- When validating the actual types of a child instance, check whether
582 -- the formal is a formal type of the parent unit, and retrieve the current
583 -- actual for it. Typ is the entity in the analyzed formal type declaration
584 -- (component or index type of an array type, or designated type of an
585 -- access formal) and Gen_Type is the enclosing analyzed formal array
586 -- or access type. The desired actual may be a formal of a parent, or may
587 -- be declared in a formal package of a parent. In both cases it is a
588 -- generic actual type because it appears within a visible instance.
589 -- Finally, it may be declared in a parent unit without being a formal
590 -- of that unit, in which case it must be retrieved by visibility.
591 -- Ambiguities may still arise if two homonyms are declared in two formal
592 -- packages, and the prefix of the formal type may be needed to resolve
593 -- the ambiguity in the instance ???
595 procedure Freeze_Subprogram_Body
596 (Inst_Node : Node_Id;
598 Pack_Id : Entity_Id);
599 -- The generic body may appear textually after the instance, including
600 -- in the proper body of a stub, or within a different package instance.
601 -- Given that the instance can only be elaborated after the generic, we
602 -- place freeze_nodes for the instance and/or for packages that may enclose
603 -- the instance and the generic, so that the back-end can establish the
604 -- proper order of elaboration.
606 function Get_Associated_Node (N : Node_Id) return Node_Id;
607 -- In order to propagate semantic information back from the analyzed copy
608 -- to the original generic, we maintain links between selected nodes in the
609 -- generic and their corresponding copies. At the end of generic analysis,
610 -- the routine Save_Global_References traverses the generic tree, examines
611 -- the semantic information, and preserves the links to those nodes that
612 -- contain global information. At instantiation, the information from the
613 -- associated node is placed on the new copy, so that name resolution is
616 -- Three kinds of source nodes have associated nodes:
618 -- a) those that can reference (denote) entities, that is identifiers,
619 -- character literals, expanded_names, operator symbols, operators,
620 -- and attribute reference nodes. These nodes have an Entity field
621 -- and are the set of nodes that are in N_Has_Entity.
623 -- b) aggregates (N_Aggregate and N_Extension_Aggregate)
625 -- c) selected components (N_Selected_Component)
627 -- For the first class, the associated node preserves the entity if it is
628 -- global. If the generic contains nested instantiations, the associated
629 -- node itself has been recopied, and a chain of them must be followed.
631 -- For aggregates, the associated node allows retrieval of the type, which
632 -- may otherwise not appear in the generic. The view of this type may be
633 -- different between generic and instantiation, and the full view can be
634 -- installed before the instantiation is analyzed. For aggregates of type
635 -- extensions, the same view exchange may have to be performed for some of
636 -- the ancestor types, if their view is private at the point of
639 -- Nodes that are selected components in the parse tree may be rewritten
640 -- as expanded names after resolution, and must be treated as potential
641 -- entity holders, which is why they also have an Associated_Node.
643 -- Nodes that do not come from source, such as freeze nodes, do not appear
644 -- in the generic tree, and need not have an associated node.
646 -- The associated node is stored in the Associated_Node field. Note that
647 -- this field overlaps Entity, which is fine, because the whole point is
648 -- that we don't need or want the normal Entity field in this situation.
650 function Has_Been_Exchanged (E : Entity_Id) return Boolean;
651 -- Traverse the Exchanged_Views list to see if a type was private
652 -- and has already been flipped during this phase of instantiation.
654 procedure Hide_Current_Scope;
655 -- When instantiating a generic child unit, the parent context must be
656 -- present, but the instance and all entities that may be generated
657 -- must be inserted in the current scope. We leave the current scope
658 -- on the stack, but make its entities invisible to avoid visibility
659 -- problems. This is reversed at the end of the instantiation. This is
660 -- not done for the instantiation of the bodies, which only require the
661 -- instances of the generic parents to be in scope.
663 function In_Main_Context (E : Entity_Id) return Boolean;
664 -- Check whether an instantiation is in the context of the main unit.
665 -- Used to determine whether its body should be elaborated to allow
666 -- front-end inlining.
668 procedure Inherit_Context (Gen_Decl : Node_Id; Inst : Node_Id);
669 -- Add the context clause of the unit containing a generic unit to a
670 -- compilation unit that is, or contains, an instantiation.
673 -- Establish environment for subsequent instantiation. Separated from
674 -- Save_Env because data-structures for visibility handling must be
675 -- initialized before call to Check_Generic_Child_Unit.
677 procedure Inline_Instance_Body
679 Gen_Unit : Entity_Id;
681 -- If front-end inlining is requested, instantiate the package body,
682 -- and preserve the visibility of its compilation unit, to insure
683 -- that successive instantiations succeed.
685 procedure Insert_Freeze_Node_For_Instance
688 -- N denotes a package or a subprogram instantiation and F_Node is the
689 -- associated freeze node. Insert the freeze node before the first source
690 -- body which follows immediately after N. If no such body is found, the
691 -- freeze node is inserted at the end of the declarative region which
694 procedure Install_Body
699 -- If the instantiation happens textually before the body of the generic,
700 -- the instantiation of the body must be analyzed after the generic body,
701 -- and not at the point of instantiation. Such early instantiations can
702 -- happen if the generic and the instance appear in a package declaration
703 -- because the generic body can only appear in the corresponding package
704 -- body. Early instantiations can also appear if generic, instance and
705 -- body are all in the declarative part of a subprogram or entry. Entities
706 -- of packages that are early instantiations are delayed, and their freeze
707 -- node appears after the generic body. This rather complex machinery is
708 -- needed when nested instantiations are present, because the source does
709 -- not carry any indication of where the corresponding instance bodies must
710 -- be installed and frozen.
712 procedure Install_Formal_Packages (Par : Entity_Id);
713 -- Install the visible part of any formal of the parent that is a formal
714 -- package. Note that for the case of a formal package with a box, this
715 -- includes the formal part of the formal package (12.7(10/2)).
717 procedure Install_Hidden_Primitives
718 (Prims_List : in out Elist_Id;
721 -- Remove suffix 'P' from hidden primitives of Act_T to match the
722 -- visibility of primitives of Gen_T. The list of primitives to which
723 -- the suffix is removed is added to Prims_List to restore them later.
725 procedure Install_Parent (P : Entity_Id; In_Body : Boolean := False);
726 -- When compiling an instance of a child unit the parent (which is
727 -- itself an instance) is an enclosing scope that must be made
728 -- immediately visible. This procedure is also used to install the non-
729 -- generic parent of a generic child unit when compiling its body, so
730 -- that full views of types in the parent are made visible.
732 -- The functions Instantiate_XXX perform various legality checks and build
733 -- the declarations for instantiated generic parameters. In all of these
734 -- Formal is the entity in the generic unit, Actual is the entity of
735 -- expression in the generic associations, and Analyzed_Formal is the
736 -- formal in the generic copy, which contains the semantic information to
737 -- be used to validate the actual.
739 function Instantiate_Object
742 Analyzed_Formal : Node_Id) return List_Id;
744 function Instantiate_Type
747 Analyzed_Formal : Node_Id;
748 Actual_Decls : List_Id) return List_Id;
750 function Instantiate_Formal_Subprogram
753 Analyzed_Formal : Node_Id) return Node_Id;
755 function Instantiate_Formal_Package
758 Analyzed_Formal : Node_Id) return List_Id;
759 -- If the formal package is declared with a box, special visibility rules
760 -- apply to its formals: they are in the visible part of the package. This
761 -- is true in the declarative region of the formal package, that is to say
762 -- in the enclosing generic or instantiation. For an instantiation, the
763 -- parameters of the formal package are made visible in an explicit step.
764 -- Furthermore, if the actual has a visible USE clause, these formals must
765 -- be made potentially use-visible as well. On exit from the enclosing
766 -- instantiation, the reverse must be done.
768 -- For a formal package declared without a box, there are conformance rules
769 -- that apply to the actuals in the generic declaration and the actuals of
770 -- the actual package in the enclosing instantiation. The simplest way to
771 -- apply these rules is to repeat the instantiation of the formal package
772 -- in the context of the enclosing instance, and compare the generic
773 -- associations of this instantiation with those of the actual package.
774 -- This internal instantiation only needs to contain the renamings of the
775 -- formals: the visible and private declarations themselves need not be
778 -- In Ada 2005, the formal package may be only partially parameterized.
779 -- In that case the visibility step must make visible those actuals whose
780 -- corresponding formals were given with a box. A final complication
781 -- involves inherited operations from formal derived types, which must
782 -- be visible if the type is.
784 function Is_In_Main_Unit (N : Node_Id) return Boolean;
785 -- Test if given node is in the main unit
787 procedure Load_Parent_Of_Generic
790 Body_Optional : Boolean := False);
791 -- If the generic appears in a separate non-generic library unit, load the
792 -- corresponding body to retrieve the body of the generic. N is the node
793 -- for the generic instantiation, Spec is the generic package declaration.
795 -- Body_Optional is a flag that indicates that the body is being loaded to
796 -- ensure that temporaries are generated consistently when there are other
797 -- instances in the current declarative part that precede the one being
798 -- loaded. In that case a missing body is acceptable.
800 procedure Map_Formal_Package_Entities (Form : Entity_Id; Act : Entity_Id);
801 -- Within the generic part, entities in the formal package are
802 -- visible. To validate subsequent type declarations, indicate
803 -- the correspondence between the entities in the analyzed formal,
804 -- and the entities in the actual package. There are three packages
805 -- involved in the instantiation of a formal package: the parent
806 -- generic P1 which appears in the generic declaration, the fake
807 -- instantiation P2 which appears in the analyzed generic, and whose
808 -- visible entities may be used in subsequent formals, and the actual
809 -- P3 in the instance. To validate subsequent formals, me indicate
810 -- that the entities in P2 are mapped into those of P3. The mapping of
811 -- entities has to be done recursively for nested packages.
813 procedure Move_Freeze_Nodes
817 -- Freeze nodes can be generated in the analysis of a generic unit, but
818 -- will not be seen by the back-end. It is necessary to move those nodes
819 -- to the enclosing scope if they freeze an outer entity. We place them
820 -- at the end of the enclosing generic package, which is semantically
823 procedure Preanalyze_Actuals (N : Node_Id; Inst : Entity_Id := Empty);
824 -- Analyze actuals to perform name resolution. Full resolution is done
825 -- later, when the expected types are known, but names have to be captured
826 -- before installing parents of generics, that are not visible for the
827 -- actuals themselves.
829 -- If Inst is present, it is the entity of the package instance. This
830 -- entity is marked as having a limited_view actual when some actual is
831 -- a limited view. This is used to place the instance body properly.
833 procedure Provide_Completing_Bodies (N : Node_Id);
834 -- Generate completing bodies for all subprograms found within package or
835 -- subprogram declaration N.
837 procedure Remove_Parent (In_Body : Boolean := False);
838 -- Reverse effect after instantiation of child is complete
840 procedure Restore_Hidden_Primitives (Prims_List : in out Elist_Id);
841 -- Restore suffix 'P' to primitives of Prims_List and leave Prims_List
844 procedure Set_Instance_Env
845 (Gen_Unit : Entity_Id;
846 Act_Unit : Entity_Id);
847 -- Save current instance on saved environment, to be used to determine
848 -- the global status of entities in nested instances. Part of Save_Env.
849 -- called after verifying that the generic unit is legal for the instance,
850 -- The procedure also examines whether the generic unit is a predefined
851 -- unit, in order to set configuration switches accordingly. As a result
852 -- the procedure must be called after analyzing and freezing the actuals.
854 procedure Set_Instance_Of (A : Entity_Id; B : Entity_Id);
855 -- Associate analyzed generic parameter with corresponding instance. Used
856 -- for semantic checks at instantiation time.
858 function True_Parent (N : Node_Id) return Node_Id;
859 -- For a subunit, return parent of corresponding stub, else return
862 procedure Valid_Default_Attribute (Nam : Entity_Id; Def : Node_Id);
863 -- Verify that an attribute that appears as the default for a formal
864 -- subprogram is a function or procedure with the correct profile.
866 -------------------------------------------
867 -- Data Structures for Generic Renamings --
868 -------------------------------------------
870 -- The map Generic_Renamings associates generic entities with their
871 -- corresponding actuals. Currently used to validate type instances. It
872 -- will eventually be used for all generic parameters to eliminate the
873 -- need for overload resolution in the instance.
875 type Assoc_Ptr is new Int;
877 Assoc_Null : constant Assoc_Ptr := -1;
882 Next_In_HTable : Assoc_Ptr;
885 package Generic_Renamings is new Table.Table
886 (Table_Component_Type => Assoc,
887 Table_Index_Type => Assoc_Ptr,
888 Table_Low_Bound => 0,
890 Table_Increment => 100,
891 Table_Name => "Generic_Renamings");
893 -- Variable to hold enclosing instantiation. When the environment is
894 -- saved for a subprogram inlining, the corresponding Act_Id is empty.
896 Current_Instantiated_Parent : Assoc := (Empty, Empty, Assoc_Null);
898 -- Hash table for associations
900 HTable_Size : constant := 37;
901 type HTable_Range is range 0 .. HTable_Size - 1;
903 procedure Set_Next_Assoc (E : Assoc_Ptr; Next : Assoc_Ptr);
904 function Next_Assoc (E : Assoc_Ptr) return Assoc_Ptr;
905 function Get_Gen_Id (E : Assoc_Ptr) return Entity_Id;
906 function Hash (F : Entity_Id) return HTable_Range;
908 package Generic_Renamings_HTable is new GNAT.HTable.Static_HTable (
909 Header_Num => HTable_Range,
911 Elmt_Ptr => Assoc_Ptr,
912 Null_Ptr => Assoc_Null,
913 Set_Next => Set_Next_Assoc,
916 Get_Key => Get_Gen_Id,
920 Exchanged_Views : Elist_Id;
921 -- This list holds the private views that have been exchanged during
922 -- instantiation to restore the visibility of the generic declaration.
923 -- (see comments above). After instantiation, the current visibility is
924 -- reestablished by means of a traversal of this list.
926 Hidden_Entities : Elist_Id;
927 -- This list holds the entities of the current scope that are removed
928 -- from immediate visibility when instantiating a child unit. Their
929 -- visibility is restored in Remove_Parent.
931 -- Because instantiations can be recursive, the following must be saved
932 -- on entry and restored on exit from an instantiation (spec or body).
933 -- This is done by the two procedures Save_Env and Restore_Env. For
934 -- package and subprogram instantiations (but not for the body instances)
935 -- the action of Save_Env is done in two steps: Init_Env is called before
936 -- Check_Generic_Child_Unit, because setting the parent instances requires
937 -- that the visibility data structures be properly initialized. Once the
938 -- generic is unit is validated, Set_Instance_Env completes Save_Env.
940 Parent_Unit_Visible : Boolean := False;
941 -- Parent_Unit_Visible is used when the generic is a child unit, and
942 -- indicates whether the ultimate parent of the generic is visible in the
943 -- instantiation environment. It is used to reset the visibility of the
944 -- parent at the end of the instantiation (see Remove_Parent).
946 Instance_Parent_Unit : Entity_Id := Empty;
947 -- This records the ultimate parent unit of an instance of a generic
948 -- child unit and is used in conjunction with Parent_Unit_Visible to
949 -- indicate the unit to which the Parent_Unit_Visible flag corresponds.
951 type Instance_Env is record
952 Instantiated_Parent : Assoc;
953 Exchanged_Views : Elist_Id;
954 Hidden_Entities : Elist_Id;
955 Current_Sem_Unit : Unit_Number_Type;
956 Parent_Unit_Visible : Boolean := False;
957 Instance_Parent_Unit : Entity_Id := Empty;
958 Switches : Config_Switches_Type;
961 package Instance_Envs is new Table.Table (
962 Table_Component_Type => Instance_Env,
963 Table_Index_Type => Int,
964 Table_Low_Bound => 0,
966 Table_Increment => 100,
967 Table_Name => "Instance_Envs");
969 procedure Restore_Private_Views
970 (Pack_Id : Entity_Id;
971 Is_Package : Boolean := True);
972 -- Restore the private views of external types, and unmark the generic
973 -- renamings of actuals, so that they become compatible subtypes again.
974 -- For subprograms, Pack_Id is the package constructed to hold the
977 procedure Switch_View (T : Entity_Id);
978 -- Switch the partial and full views of a type and its private
979 -- dependents (i.e. its subtypes and derived types).
981 ------------------------------------
982 -- Structures for Error Reporting --
983 ------------------------------------
985 Instantiation_Node : Node_Id;
986 -- Used by subprograms that validate instantiation of formal parameters
987 -- where there might be no actual on which to place the error message.
988 -- Also used to locate the instantiation node for generic subunits.
990 Instantiation_Error : exception;
991 -- When there is a semantic error in the generic parameter matching,
992 -- there is no point in continuing the instantiation, because the
993 -- number of cascaded errors is unpredictable. This exception aborts
994 -- the instantiation process altogether.
996 S_Adjustment : Sloc_Adjustment;
997 -- Offset created for each node in an instantiation, in order to keep
998 -- track of the source position of the instantiation in each of its nodes.
999 -- A subsequent semantic error or warning on a construct of the instance
1000 -- points to both places: the original generic node, and the point of
1001 -- instantiation. See Sinput and Sinput.L for additional details.
1003 ------------------------------------------------------------
1004 -- Data structure for keeping track when inside a Generic --
1005 ------------------------------------------------------------
1007 -- The following table is used to save values of the Inside_A_Generic
1008 -- flag (see spec of Sem) when they are saved by Start_Generic.
1010 package Generic_Flags is new Table.Table (
1011 Table_Component_Type => Boolean,
1012 Table_Index_Type => Int,
1013 Table_Low_Bound => 0,
1014 Table_Initial => 32,
1015 Table_Increment => 200,
1016 Table_Name => "Generic_Flags");
1018 ---------------------------
1019 -- Abandon_Instantiation --
1020 ---------------------------
1022 procedure Abandon_Instantiation (N : Node_Id) is
1024 Error_Msg_N ("\instantiation abandoned!", N);
1025 raise Instantiation_Error;
1026 end Abandon_Instantiation;
1028 ----------------------------------
1029 -- Adjust_Inherited_Pragma_Sloc --
1030 ----------------------------------
1032 procedure Adjust_Inherited_Pragma_Sloc (N : Node_Id) is
1034 Adjust_Instantiation_Sloc (N, S_Adjustment);
1035 end Adjust_Inherited_Pragma_Sloc;
1037 --------------------------
1038 -- Analyze_Associations --
1039 --------------------------
1041 function Analyze_Associations
1044 F_Copy : List_Id) return List_Id
1046 Actuals_To_Freeze : constant Elist_Id := New_Elmt_List;
1047 Assoc_List : constant List_Id := New_List;
1048 Default_Actuals : constant List_Id := New_List;
1049 Gen_Unit : constant Entity_Id :=
1050 Defining_Entity (Parent (F_Copy));
1054 Analyzed_Formal : Node_Id;
1055 First_Named : Node_Id := Empty;
1059 Saved_Formal : Node_Id;
1061 Default_Formals : constant List_Id := New_List;
1062 -- If an Others_Choice is present, some of the formals may be defaulted.
1063 -- To simplify the treatment of visibility in an instance, we introduce
1064 -- individual defaults for each such formal. These defaults are
1065 -- appended to the list of associations and replace the Others_Choice.
1067 Found_Assoc : Node_Id;
1068 -- Association for the current formal being match. Empty if there are
1069 -- no remaining actuals, or if there is no named association with the
1070 -- name of the formal.
1072 Is_Named_Assoc : Boolean;
1073 Num_Matched : Nat := 0;
1074 Num_Actuals : Nat := 0;
1076 Others_Present : Boolean := False;
1077 Others_Choice : Node_Id := Empty;
1078 -- In Ada 2005, indicates partial parameterization of a formal
1079 -- package. As usual an other association must be last in the list.
1081 procedure Check_Fixed_Point_Actual (Actual : Node_Id);
1082 -- Warn if an actual fixed-point type has user-defined arithmetic
1083 -- operations, but there is no corresponding formal in the generic,
1084 -- in which case the predefined operations will be used. This merits
1085 -- a warning because of the special semantics of fixed point ops.
1087 procedure Check_Overloaded_Formal_Subprogram (Formal : Entity_Id);
1088 -- Apply RM 12.3(9): if a formal subprogram is overloaded, the instance
1089 -- cannot have a named association for it. AI05-0025 extends this rule
1090 -- to formals of formal packages by AI05-0025, and it also applies to
1091 -- box-initialized formals.
1093 function Has_Fully_Defined_Profile (Subp : Entity_Id) return Boolean;
1094 -- Determine whether the parameter types and the return type of Subp
1095 -- are fully defined at the point of instantiation.
1097 function Matching_Actual
1099 A_F : Entity_Id) return Node_Id;
1100 -- Find actual that corresponds to a given a formal parameter. If the
1101 -- actuals are positional, return the next one, if any. If the actuals
1102 -- are named, scan the parameter associations to find the right one.
1103 -- A_F is the corresponding entity in the analyzed generic, which is
1104 -- placed on the selector name.
1106 -- In Ada 2005, a named association may be given with a box, in which
1107 -- case Matching_Actual sets Found_Assoc to the generic association,
1108 -- but return Empty for the actual itself. In this case the code below
1109 -- creates a corresponding declaration for the formal.
1111 function Partial_Parameterization return Boolean;
1112 -- Ada 2005: if no match is found for a given formal, check if the
1113 -- association for it includes a box, or whether the associations
1114 -- include an Others clause.
1116 procedure Process_Default (F : Entity_Id);
1117 -- Add a copy of the declaration of generic formal F to the list of
1118 -- associations, and add an explicit box association for F if there
1119 -- is none yet, and the default comes from an Others_Choice.
1121 function Renames_Standard_Subprogram (Subp : Entity_Id) return Boolean;
1122 -- Determine whether Subp renames one of the subprograms defined in the
1123 -- generated package Standard.
1125 procedure Set_Analyzed_Formal;
1126 -- Find the node in the generic copy that corresponds to a given formal.
1127 -- The semantic information on this node is used to perform legality
1128 -- checks on the actuals. Because semantic analysis can introduce some
1129 -- anonymous entities or modify the declaration node itself, the
1130 -- correspondence between the two lists is not one-one. In addition to
1131 -- anonymous types, the presence a formal equality will introduce an
1132 -- implicit declaration for the corresponding inequality.
1134 ----------------------------------------
1135 -- Check_Overloaded_Formal_Subprogram --
1136 ----------------------------------------
1138 procedure Check_Overloaded_Formal_Subprogram (Formal : Entity_Id) is
1139 Temp_Formal : Entity_Id;
1142 Temp_Formal := First (Formals);
1143 while Present (Temp_Formal) loop
1144 if Nkind (Temp_Formal) in N_Formal_Subprogram_Declaration
1145 and then Temp_Formal /= Formal
1147 Chars (Defining_Unit_Name (Specification (Formal))) =
1148 Chars (Defining_Unit_Name (Specification (Temp_Formal)))
1150 if Present (Found_Assoc) then
1152 ("named association not allowed for overloaded formal",
1157 ("named association not allowed for overloaded formal",
1161 Abandon_Instantiation (Instantiation_Node);
1166 end Check_Overloaded_Formal_Subprogram;
1168 -------------------------------
1169 -- Check_Fixed_Point_Actual --
1170 -------------------------------
1172 procedure Check_Fixed_Point_Actual (Actual : Node_Id) is
1173 Typ : constant Entity_Id := Entity (Actual);
1174 Prims : constant Elist_Id := Collect_Primitive_Operations (Typ);
1180 -- Locate primitive operations of the type that are arithmetic
1183 Elem := First_Elmt (Prims);
1184 while Present (Elem) loop
1185 if Nkind (Node (Elem)) = N_Defining_Operator_Symbol then
1187 -- Check whether the generic unit has a formal subprogram of
1188 -- the same name. This does not check types but is good enough
1189 -- to justify a warning.
1191 Formal := First_Non_Pragma (Formals);
1192 Op := Alias (Node (Elem));
1194 while Present (Formal) loop
1195 if Nkind (Formal) = N_Formal_Concrete_Subprogram_Declaration
1196 and then Chars (Defining_Entity (Formal)) =
1201 elsif Nkind (Formal) = N_Formal_Package_Declaration then
1207 -- Locate corresponding actual, and check whether it
1208 -- includes a fixed-point type.
1210 Assoc := First (Assoc_List);
1211 while Present (Assoc) loop
1213 Nkind (Assoc) = N_Package_Renaming_Declaration
1214 and then Chars (Defining_Unit_Name (Assoc)) =
1215 Chars (Defining_Identifier (Formal));
1220 if Present (Assoc) then
1222 -- If formal package declares a fixed-point type,
1223 -- and the user-defined operator is derived from
1224 -- a generic instance package, the fixed-point type
1225 -- does not use the corresponding predefined op.
1227 Ent := First_Entity (Entity (Name (Assoc)));
1228 while Present (Ent) loop
1229 if Is_Fixed_Point_Type (Ent)
1230 and then Present (Op)
1231 and then Is_Generic_Instance (Scope (Op))
1246 Error_Msg_Sloc := Sloc (Node (Elem));
1248 ("?instance uses predefined operation, not primitive "
1249 & "operation&#", Actual, Node (Elem));
1255 end Check_Fixed_Point_Actual;
1257 -------------------------------
1258 -- Has_Fully_Defined_Profile --
1259 -------------------------------
1261 function Has_Fully_Defined_Profile (Subp : Entity_Id) return Boolean is
1262 function Is_Fully_Defined_Type (Typ : Entity_Id) return Boolean;
1263 -- Determine whethet type Typ is fully defined
1265 ---------------------------
1266 -- Is_Fully_Defined_Type --
1267 ---------------------------
1269 function Is_Fully_Defined_Type (Typ : Entity_Id) return Boolean is
1271 -- A private type without a full view is not fully defined
1273 if Is_Private_Type (Typ)
1274 and then No (Full_View (Typ))
1278 -- An incomplete type is never fully defined
1280 elsif Is_Incomplete_Type (Typ) then
1283 -- All other types are fully defined
1288 end Is_Fully_Defined_Type;
1290 -- Local declarations
1294 -- Start of processing for Has_Fully_Defined_Profile
1297 -- Check the parameters
1299 Param := First_Formal (Subp);
1300 while Present (Param) loop
1301 if not Is_Fully_Defined_Type (Etype (Param)) then
1305 Next_Formal (Param);
1308 -- Check the return type
1310 return Is_Fully_Defined_Type (Etype (Subp));
1311 end Has_Fully_Defined_Profile;
1313 ---------------------
1314 -- Matching_Actual --
1315 ---------------------
1317 function Matching_Actual
1319 A_F : Entity_Id) return Node_Id
1325 Is_Named_Assoc := False;
1327 -- End of list of purely positional parameters
1329 if No (Actual) or else Nkind (Actual) = N_Others_Choice then
1330 Found_Assoc := Empty;
1333 -- Case of positional parameter corresponding to current formal
1335 elsif No (Selector_Name (Actual)) then
1336 Found_Assoc := Actual;
1337 Act := Explicit_Generic_Actual_Parameter (Actual);
1338 Num_Matched := Num_Matched + 1;
1341 -- Otherwise scan list of named actuals to find the one with the
1342 -- desired name. All remaining actuals have explicit names.
1345 Is_Named_Assoc := True;
1346 Found_Assoc := Empty;
1350 while Present (Actual) loop
1351 if Nkind (Actual) = N_Others_Choice then
1352 Found_Assoc := Empty;
1355 elsif Chars (Selector_Name (Actual)) = Chars (F) then
1356 Set_Entity (Selector_Name (Actual), A_F);
1357 Set_Etype (Selector_Name (Actual), Etype (A_F));
1358 Generate_Reference (A_F, Selector_Name (Actual));
1360 Found_Assoc := Actual;
1361 Act := Explicit_Generic_Actual_Parameter (Actual);
1362 Num_Matched := Num_Matched + 1;
1370 -- Reset for subsequent searches. In most cases the named
1371 -- associations are in order. If they are not, we reorder them
1372 -- to avoid scanning twice the same actual. This is not just a
1373 -- question of efficiency: there may be multiple defaults with
1374 -- boxes that have the same name. In a nested instantiation we
1375 -- insert actuals for those defaults, and cannot rely on their
1376 -- names to disambiguate them.
1378 if Actual = First_Named then
1381 elsif Present (Actual) then
1382 Insert_Before (First_Named, Remove_Next (Prev));
1385 Actual := First_Named;
1388 if Is_Entity_Name (Act) and then Present (Entity (Act)) then
1389 Set_Used_As_Generic_Actual (Entity (Act));
1393 end Matching_Actual;
1395 ------------------------------
1396 -- Partial_Parameterization --
1397 ------------------------------
1399 function Partial_Parameterization return Boolean is
1401 return Others_Present
1402 or else (Present (Found_Assoc) and then Box_Present (Found_Assoc));
1403 end Partial_Parameterization;
1405 ---------------------
1406 -- Process_Default --
1407 ---------------------
1409 procedure Process_Default (F : Entity_Id) is
1410 Loc : constant Source_Ptr := Sloc (I_Node);
1411 F_Id : constant Entity_Id := Defining_Entity (F);
1417 -- Append copy of formal declaration to associations, and create new
1418 -- defining identifier for it.
1420 Decl := New_Copy_Tree (F);
1421 Id := Make_Defining_Identifier (Sloc (F_Id), Chars (F_Id));
1423 if Nkind (F) in N_Formal_Subprogram_Declaration then
1424 Set_Defining_Unit_Name (Specification (Decl), Id);
1427 Set_Defining_Identifier (Decl, Id);
1430 Append (Decl, Assoc_List);
1432 if No (Found_Assoc) then
1434 Make_Generic_Association (Loc,
1436 New_Occurrence_Of (Id, Loc),
1437 Explicit_Generic_Actual_Parameter => Empty);
1438 Set_Box_Present (Default);
1439 Append (Default, Default_Formals);
1441 end Process_Default;
1443 ---------------------------------
1444 -- Renames_Standard_Subprogram --
1445 ---------------------------------
1447 function Renames_Standard_Subprogram (Subp : Entity_Id) return Boolean is
1452 while Present (Id) loop
1453 if Scope (Id) = Standard_Standard then
1461 end Renames_Standard_Subprogram;
1463 -------------------------
1464 -- Set_Analyzed_Formal --
1465 -------------------------
1467 procedure Set_Analyzed_Formal is
1471 while Present (Analyzed_Formal) loop
1472 Kind := Nkind (Analyzed_Formal);
1474 case Nkind (Formal) is
1475 when N_Formal_Subprogram_Declaration =>
1476 exit when Kind in N_Formal_Subprogram_Declaration
1479 (Defining_Unit_Name (Specification (Formal))) =
1481 (Defining_Unit_Name (Specification (Analyzed_Formal)));
1483 when N_Formal_Package_Declaration =>
1484 exit when Nkind_In (Kind, N_Formal_Package_Declaration,
1485 N_Generic_Package_Declaration,
1486 N_Package_Declaration);
1488 when N_Use_Package_Clause
1495 -- Skip freeze nodes, and nodes inserted to replace
1496 -- unrecognized pragmas.
1499 Kind not in N_Formal_Subprogram_Declaration
1500 and then not Nkind_In (Kind, N_Subprogram_Declaration,
1504 and then Chars (Defining_Identifier (Formal)) =
1505 Chars (Defining_Identifier (Analyzed_Formal));
1508 Next (Analyzed_Formal);
1510 end Set_Analyzed_Formal;
1512 -- Start of processing for Analyze_Associations
1515 Actuals := Generic_Associations (I_Node);
1517 if Present (Actuals) then
1519 -- Check for an Others choice, indicating a partial parameterization
1520 -- for a formal package.
1522 Actual := First (Actuals);
1523 while Present (Actual) loop
1524 if Nkind (Actual) = N_Others_Choice then
1525 Others_Present := True;
1526 Others_Choice := Actual;
1528 if Present (Next (Actual)) then
1529 Error_Msg_N ("others must be last association", Actual);
1532 -- This subprogram is used both for formal packages and for
1533 -- instantiations. For the latter, associations must all be
1536 if Nkind (I_Node) /= N_Formal_Package_Declaration
1537 and then Comes_From_Source (I_Node)
1540 ("others association not allowed in an instance",
1544 -- In any case, nothing to do after the others association
1548 elsif Box_Present (Actual)
1549 and then Comes_From_Source (I_Node)
1550 and then Nkind (I_Node) /= N_Formal_Package_Declaration
1553 ("box association not allowed in an instance", Actual);
1559 -- If named associations are present, save first named association
1560 -- (it may of course be Empty) to facilitate subsequent name search.
1562 First_Named := First (Actuals);
1563 while Present (First_Named)
1564 and then Nkind (First_Named) /= N_Others_Choice
1565 and then No (Selector_Name (First_Named))
1567 Num_Actuals := Num_Actuals + 1;
1572 Named := First_Named;
1573 while Present (Named) loop
1574 if Nkind (Named) /= N_Others_Choice
1575 and then No (Selector_Name (Named))
1577 Error_Msg_N ("invalid positional actual after named one", Named);
1578 Abandon_Instantiation (Named);
1581 -- A named association may lack an actual parameter, if it was
1582 -- introduced for a default subprogram that turns out to be local
1583 -- to the outer instantiation. If it has a box association it must
1584 -- correspond to some formal in the generic.
1586 if Nkind (Named) /= N_Others_Choice
1587 and then (Present (Explicit_Generic_Actual_Parameter (Named))
1588 or else Box_Present (Named))
1590 Num_Actuals := Num_Actuals + 1;
1596 if Present (Formals) then
1597 Formal := First_Non_Pragma (Formals);
1598 Analyzed_Formal := First_Non_Pragma (F_Copy);
1600 if Present (Actuals) then
1601 Actual := First (Actuals);
1603 -- All formals should have default values
1609 while Present (Formal) loop
1610 Set_Analyzed_Formal;
1611 Saved_Formal := Next_Non_Pragma (Formal);
1613 case Nkind (Formal) is
1614 when N_Formal_Object_Declaration =>
1617 (Defining_Identifier (Formal),
1618 Defining_Identifier (Analyzed_Formal));
1620 if No (Match) and then Partial_Parameterization then
1621 Process_Default (Formal);
1625 (Instantiate_Object (Formal, Match, Analyzed_Formal),
1628 -- For a defaulted in_parameter, create an entry in the
1629 -- the list of defaulted actuals, for GNATProve use. Do
1630 -- not included these defaults for an instance nested
1631 -- within a generic, because the defaults are also used
1632 -- in the analysis of the enclosing generic, and only
1633 -- defaulted subprograms are relevant there.
1635 if No (Match) and then not Inside_A_Generic then
1636 Append_To (Default_Actuals,
1637 Make_Generic_Association (Sloc (I_Node),
1640 (Defining_Identifier (Formal), Sloc (I_Node)),
1641 Explicit_Generic_Actual_Parameter =>
1642 New_Copy_Tree (Default_Expression (Formal))));
1646 -- If the object is a call to an expression function, this
1647 -- is a freezing point for it.
1649 if Is_Entity_Name (Match)
1650 and then Present (Entity (Match))
1652 (Original_Node (Unit_Declaration_Node (Entity (Match))))
1653 = N_Expression_Function
1655 Append_Elmt (Entity (Match), Actuals_To_Freeze);
1658 when N_Formal_Type_Declaration =>
1661 (Defining_Identifier (Formal),
1662 Defining_Identifier (Analyzed_Formal));
1665 if Partial_Parameterization then
1666 Process_Default (Formal);
1669 Error_Msg_Sloc := Sloc (Gen_Unit);
1672 Instantiation_Node, Defining_Identifier (Formal));
1674 ("\in instantiation of & declared#",
1675 Instantiation_Node, Gen_Unit);
1676 Abandon_Instantiation (Instantiation_Node);
1683 (Formal, Match, Analyzed_Formal, Assoc_List),
1686 -- Warn when an actual is a fixed-point with user-
1687 -- defined promitives. The warning is superfluous
1688 -- if the fornal is private, because there can be
1689 -- no arithmetic operations in the generic so there
1690 -- no danger of confusion.
1692 if Is_Fixed_Point_Type (Entity (Match))
1693 and then not Is_Private_Type
1694 (Defining_Identifier (Analyzed_Formal))
1696 Check_Fixed_Point_Actual (Match);
1699 -- An instantiation is a freeze point for the actuals,
1700 -- unless this is a rewritten formal package, or the
1701 -- formal is an Ada 2012 formal incomplete type.
1703 if Nkind (I_Node) = N_Formal_Package_Declaration
1705 (Ada_Version >= Ada_2012
1707 Ekind (Defining_Identifier (Analyzed_Formal)) =
1713 Append_Elmt (Entity (Match), Actuals_To_Freeze);
1717 -- A remote access-to-class-wide type is not a legal actual
1718 -- for a generic formal of an access type (E.2.2(17/2)).
1719 -- In GNAT an exception to this rule is introduced when
1720 -- the formal is marked as remote using implementation
1721 -- defined aspect/pragma Remote_Access_Type. In that case
1722 -- the actual must be remote as well.
1724 -- If the current instantiation is the construction of a
1725 -- local copy for a formal package the actuals may be
1726 -- defaulted, and there is no matching actual to check.
1728 if Nkind (Analyzed_Formal) = N_Formal_Type_Declaration
1730 Nkind (Formal_Type_Definition (Analyzed_Formal)) =
1731 N_Access_To_Object_Definition
1732 and then Present (Match)
1735 Formal_Ent : constant Entity_Id :=
1736 Defining_Identifier (Analyzed_Formal);
1738 if Is_Remote_Access_To_Class_Wide_Type (Entity (Match))
1739 = Is_Remote_Types (Formal_Ent)
1741 -- Remoteness of formal and actual match
1745 elsif Is_Remote_Types (Formal_Ent) then
1747 -- Remote formal, non-remote actual
1750 ("actual for& must be remote", Match, Formal_Ent);
1753 -- Non-remote formal, remote actual
1756 ("actual for& may not be remote",
1762 when N_Formal_Subprogram_Declaration =>
1765 (Defining_Unit_Name (Specification (Formal)),
1766 Defining_Unit_Name (Specification (Analyzed_Formal)));
1768 -- If the formal subprogram has the same name as another
1769 -- formal subprogram of the generic, then a named
1770 -- association is illegal (12.3(9)). Exclude named
1771 -- associations that are generated for a nested instance.
1774 and then Is_Named_Assoc
1775 and then Comes_From_Source (Found_Assoc)
1777 Check_Overloaded_Formal_Subprogram (Formal);
1780 -- If there is no corresponding actual, this may be case
1781 -- of partial parameterization, or else the formal has a
1782 -- default or a box.
1784 if No (Match) and then Partial_Parameterization then
1785 Process_Default (Formal);
1787 if Nkind (I_Node) = N_Formal_Package_Declaration then
1788 Check_Overloaded_Formal_Subprogram (Formal);
1792 Append_To (Assoc_List,
1793 Instantiate_Formal_Subprogram
1794 (Formal, Match, Analyzed_Formal));
1796 -- An instantiation is a freeze point for the actuals,
1797 -- unless this is a rewritten formal package.
1799 if Nkind (I_Node) /= N_Formal_Package_Declaration
1800 and then Nkind (Match) = N_Identifier
1801 and then Is_Subprogram (Entity (Match))
1803 -- The actual subprogram may rename a routine defined
1804 -- in Standard. Avoid freezing such renamings because
1805 -- subprograms coming from Standard cannot be frozen.
1808 not Renames_Standard_Subprogram (Entity (Match))
1810 -- If the actual subprogram comes from a different
1811 -- unit, it is already frozen, either by a body in
1812 -- that unit or by the end of the declarative part
1813 -- of the unit. This check avoids the freezing of
1814 -- subprograms defined in Standard which are used
1815 -- as generic actuals.
1817 and then In_Same_Code_Unit (Entity (Match), I_Node)
1818 and then Has_Fully_Defined_Profile (Entity (Match))
1820 -- Mark the subprogram as having a delayed freeze
1821 -- since this may be an out-of-order action.
1823 Set_Has_Delayed_Freeze (Entity (Match));
1824 Append_Elmt (Entity (Match), Actuals_To_Freeze);
1828 -- If this is a nested generic, preserve default for later
1829 -- instantiations. We do this as well for GNATProve use,
1830 -- so that the list of generic associations is complete.
1832 if No (Match) and then Box_Present (Formal) then
1834 Subp : constant Entity_Id :=
1836 (Specification (Last (Assoc_List)));
1839 Append_To (Default_Actuals,
1840 Make_Generic_Association (Sloc (I_Node),
1842 New_Occurrence_Of (Subp, Sloc (I_Node)),
1843 Explicit_Generic_Actual_Parameter =>
1844 New_Occurrence_Of (Subp, Sloc (I_Node))));
1848 when N_Formal_Package_Declaration =>
1851 (Defining_Identifier (Formal),
1852 Defining_Identifier (Original_Node (Analyzed_Formal)));
1855 if Partial_Parameterization then
1856 Process_Default (Formal);
1859 Error_Msg_Sloc := Sloc (Gen_Unit);
1862 Instantiation_Node, Defining_Identifier (Formal));
1864 ("\in instantiation of & declared#",
1865 Instantiation_Node, Gen_Unit);
1867 Abandon_Instantiation (Instantiation_Node);
1873 (Instantiate_Formal_Package
1874 (Formal, Match, Analyzed_Formal),
1877 -- Determine whether the actual package needs an explicit
1878 -- freeze node. This is only the case if the actual is
1879 -- declared in the same unit and has a body. Normally
1880 -- packages do not have explicit freeze nodes, and gigi
1881 -- only uses them to elaborate entities in a package
1884 Explicit_Freeze_Check : declare
1885 Actual : constant Entity_Id := Entity (Match);
1886 Gen_Par : Entity_Id;
1888 Needs_Freezing : Boolean;
1891 procedure Check_Generic_Parent;
1892 -- The actual may be an instantiation of a unit
1893 -- declared in a previous instantiation. If that
1894 -- one is also in the current compilation, it must
1895 -- itself be frozen before the actual. The actual
1896 -- may be an instantiation of a generic child unit,
1897 -- in which case the same applies to the instance
1898 -- of the parent which must be frozen before the
1900 -- Should this itself be recursive ???
1902 --------------------------
1903 -- Check_Generic_Parent --
1904 --------------------------
1906 procedure Check_Generic_Parent is
1907 Inst : constant Node_Id :=
1908 Next (Unit_Declaration_Node (Actual));
1914 if Nkind (Parent (Actual)) = N_Package_Specification
1916 Par := Scope (Generic_Parent (Parent (Actual)));
1918 if Is_Generic_Instance (Par) then
1921 -- If the actual is a child generic unit, check
1922 -- whether the instantiation of the parent is
1923 -- also local and must also be frozen now. We
1924 -- must retrieve the instance node to locate the
1925 -- parent instance if any.
1927 elsif Ekind (Par) = E_Generic_Package
1928 and then Is_Child_Unit (Gen_Par)
1929 and then Ekind (Scope (Gen_Par)) =
1932 if Nkind (Inst) = N_Package_Instantiation
1933 and then Nkind (Name (Inst)) =
1936 -- Retrieve entity of parent instance
1938 Par := Entity (Prefix (Name (Inst)));
1947 and then Is_Generic_Instance (Par)
1948 and then Scope (Par) = Current_Scope
1950 (No (Freeze_Node (Par))
1952 not Is_List_Member (Freeze_Node (Par)))
1954 Set_Has_Delayed_Freeze (Par);
1955 Append_Elmt (Par, Actuals_To_Freeze);
1957 end Check_Generic_Parent;
1959 -- Start of processing for Explicit_Freeze_Check
1962 if Present (Renamed_Entity (Actual)) then
1964 Generic_Parent (Specification
1965 (Unit_Declaration_Node
1966 (Renamed_Entity (Actual))));
1969 Generic_Parent (Specification
1970 (Unit_Declaration_Node (Actual)));
1973 if not Expander_Active
1974 or else not Has_Completion (Actual)
1975 or else not In_Same_Source_Unit (I_Node, Actual)
1976 or else Is_Frozen (Actual)
1978 (Present (Renamed_Entity (Actual))
1980 not In_Same_Source_Unit
1981 (I_Node, (Renamed_Entity (Actual))))
1986 -- Finally we want to exclude such freeze nodes
1987 -- from statement sequences, which freeze
1988 -- everything before them.
1989 -- Is this strictly necessary ???
1991 Needs_Freezing := True;
1994 while Present (S) loop
1995 if Ekind_In (S, E_Block,
2000 Needs_Freezing := False;
2007 if Needs_Freezing then
2008 Check_Generic_Parent;
2010 -- If the actual is a renaming of a proper
2011 -- instance of the formal package, indicate
2012 -- that it is the instance that must be frozen.
2014 if Nkind (Parent (Actual)) =
2015 N_Package_Renaming_Declaration
2017 Set_Has_Delayed_Freeze
2018 (Renamed_Entity (Actual));
2020 (Renamed_Entity (Actual),
2023 Set_Has_Delayed_Freeze (Actual);
2024 Append_Elmt (Actual, Actuals_To_Freeze);
2028 end Explicit_Freeze_Check;
2031 -- For use type and use package appearing in the generic part,
2032 -- we have already copied them, so we can just move them where
2033 -- they belong (we mustn't recopy them since this would mess up
2034 -- the Sloc values).
2036 when N_Use_Package_Clause
2039 if Nkind (Original_Node (I_Node)) =
2040 N_Formal_Package_Declaration
2042 Append (New_Copy_Tree (Formal), Assoc_List);
2045 Append (Formal, Assoc_List);
2049 raise Program_Error;
2052 Formal := Saved_Formal;
2053 Next_Non_Pragma (Analyzed_Formal);
2056 if Num_Actuals > Num_Matched then
2057 Error_Msg_Sloc := Sloc (Gen_Unit);
2059 if Present (Selector_Name (Actual)) then
2061 ("unmatched actual &", Actual, Selector_Name (Actual));
2063 ("\in instantiation of & declared#", Actual, Gen_Unit);
2066 ("unmatched actual in instantiation of & declared#",
2071 elsif Present (Actuals) then
2073 ("too many actuals in generic instantiation", Instantiation_Node);
2076 -- An instantiation freezes all generic actuals. The only exceptions
2077 -- to this are incomplete types and subprograms which are not fully
2078 -- defined at the point of instantiation.
2081 Elmt : Elmt_Id := First_Elmt (Actuals_To_Freeze);
2083 while Present (Elmt) loop
2084 Freeze_Before (I_Node, Node (Elmt));
2089 -- If there are default subprograms, normalize the tree by adding
2090 -- explicit associations for them. This is required if the instance
2091 -- appears within a generic.
2093 if not Is_Empty_List (Default_Actuals) then
2098 Default := First (Default_Actuals);
2099 while Present (Default) loop
2100 Mark_Rewrite_Insertion (Default);
2104 if No (Actuals) then
2105 Set_Generic_Associations (I_Node, Default_Actuals);
2107 Append_List_To (Actuals, Default_Actuals);
2112 -- If this is a formal package, normalize the parameter list by adding
2113 -- explicit box associations for the formals that are covered by an
2116 if not Is_Empty_List (Default_Formals) then
2117 Append_List (Default_Formals, Formals);
2121 end Analyze_Associations;
2123 -------------------------------
2124 -- Analyze_Formal_Array_Type --
2125 -------------------------------
2127 procedure Analyze_Formal_Array_Type
2128 (T : in out Entity_Id;
2134 -- Treated like a non-generic array declaration, with additional
2139 if Nkind (Def) = N_Constrained_Array_Definition then
2140 DSS := First (Discrete_Subtype_Definitions (Def));
2141 while Present (DSS) loop
2142 if Nkind_In (DSS, N_Subtype_Indication,
2144 N_Attribute_Reference)
2146 Error_Msg_N ("only a subtype mark is allowed in a formal", DSS);
2153 Array_Type_Declaration (T, Def);
2154 Set_Is_Generic_Type (Base_Type (T));
2156 if Ekind (Component_Type (T)) = E_Incomplete_Type
2157 and then No (Full_View (Component_Type (T)))
2159 Error_Msg_N ("premature usage of incomplete type", Def);
2161 -- Check that range constraint is not allowed on the component type
2162 -- of a generic formal array type (AARM 12.5.3(3))
2164 elsif Is_Internal (Component_Type (T))
2165 and then Present (Subtype_Indication (Component_Definition (Def)))
2166 and then Nkind (Original_Node
2167 (Subtype_Indication (Component_Definition (Def)))) =
2168 N_Subtype_Indication
2171 ("in a formal, a subtype indication can only be "
2172 & "a subtype mark (RM 12.5.3(3))",
2173 Subtype_Indication (Component_Definition (Def)));
2176 end Analyze_Formal_Array_Type;
2178 ---------------------------------------------
2179 -- Analyze_Formal_Decimal_Fixed_Point_Type --
2180 ---------------------------------------------
2182 -- As for other generic types, we create a valid type representation with
2183 -- legal but arbitrary attributes, whose values are never considered
2184 -- static. For all scalar types we introduce an anonymous base type, with
2185 -- the same attributes. We choose the corresponding integer type to be
2186 -- Standard_Integer.
2187 -- Here and in other similar routines, the Sloc of the generated internal
2188 -- type must be the same as the sloc of the defining identifier of the
2189 -- formal type declaration, to provide proper source navigation.
2191 procedure Analyze_Formal_Decimal_Fixed_Point_Type
2195 Loc : constant Source_Ptr := Sloc (Def);
2197 Base : constant Entity_Id :=
2199 (E_Decimal_Fixed_Point_Type,
2201 Sloc (Defining_Identifier (Parent (Def))), 'G');
2203 Int_Base : constant Entity_Id := Standard_Integer;
2204 Delta_Val : constant Ureal := Ureal_1;
2205 Digs_Val : constant Uint := Uint_6;
2207 function Make_Dummy_Bound return Node_Id;
2208 -- Return a properly typed universal real literal to use as a bound
2210 ----------------------
2211 -- Make_Dummy_Bound --
2212 ----------------------
2214 function Make_Dummy_Bound return Node_Id is
2215 Bound : constant Node_Id := Make_Real_Literal (Loc, Ureal_1);
2217 Set_Etype (Bound, Universal_Real);
2219 end Make_Dummy_Bound;
2221 -- Start of processing for Analyze_Formal_Decimal_Fixed_Point_Type
2226 Set_Etype (Base, Base);
2227 Set_Size_Info (Base, Int_Base);
2228 Set_RM_Size (Base, RM_Size (Int_Base));
2229 Set_First_Rep_Item (Base, First_Rep_Item (Int_Base));
2230 Set_Digits_Value (Base, Digs_Val);
2231 Set_Delta_Value (Base, Delta_Val);
2232 Set_Small_Value (Base, Delta_Val);
2233 Set_Scalar_Range (Base,
2235 Low_Bound => Make_Dummy_Bound,
2236 High_Bound => Make_Dummy_Bound));
2238 Set_Is_Generic_Type (Base);
2239 Set_Parent (Base, Parent (Def));
2241 Set_Ekind (T, E_Decimal_Fixed_Point_Subtype);
2242 Set_Etype (T, Base);
2243 Set_Size_Info (T, Int_Base);
2244 Set_RM_Size (T, RM_Size (Int_Base));
2245 Set_First_Rep_Item (T, First_Rep_Item (Int_Base));
2246 Set_Digits_Value (T, Digs_Val);
2247 Set_Delta_Value (T, Delta_Val);
2248 Set_Small_Value (T, Delta_Val);
2249 Set_Scalar_Range (T, Scalar_Range (Base));
2250 Set_Is_Constrained (T);
2252 Check_Restriction (No_Fixed_Point, Def);
2253 end Analyze_Formal_Decimal_Fixed_Point_Type;
2255 -------------------------------------------
2256 -- Analyze_Formal_Derived_Interface_Type --
2257 -------------------------------------------
2259 procedure Analyze_Formal_Derived_Interface_Type
2264 Loc : constant Source_Ptr := Sloc (Def);
2267 -- Rewrite as a type declaration of a derived type. This ensures that
2268 -- the interface list and primitive operations are properly captured.
2271 Make_Full_Type_Declaration (Loc,
2272 Defining_Identifier => T,
2273 Type_Definition => Def));
2275 Set_Is_Generic_Type (T);
2276 end Analyze_Formal_Derived_Interface_Type;
2278 ---------------------------------
2279 -- Analyze_Formal_Derived_Type --
2280 ---------------------------------
2282 procedure Analyze_Formal_Derived_Type
2287 Loc : constant Source_Ptr := Sloc (Def);
2288 Unk_Disc : constant Boolean := Unknown_Discriminants_Present (N);
2292 Set_Is_Generic_Type (T);
2294 if Private_Present (Def) then
2296 Make_Private_Extension_Declaration (Loc,
2297 Defining_Identifier => T,
2298 Discriminant_Specifications => Discriminant_Specifications (N),
2299 Unknown_Discriminants_Present => Unk_Disc,
2300 Subtype_Indication => Subtype_Mark (Def),
2301 Interface_List => Interface_List (Def));
2303 Set_Abstract_Present (New_N, Abstract_Present (Def));
2304 Set_Limited_Present (New_N, Limited_Present (Def));
2305 Set_Synchronized_Present (New_N, Synchronized_Present (Def));
2309 Make_Full_Type_Declaration (Loc,
2310 Defining_Identifier => T,
2311 Discriminant_Specifications =>
2312 Discriminant_Specifications (Parent (T)),
2314 Make_Derived_Type_Definition (Loc,
2315 Subtype_Indication => Subtype_Mark (Def)));
2317 Set_Abstract_Present
2318 (Type_Definition (New_N), Abstract_Present (Def));
2320 (Type_Definition (New_N), Limited_Present (Def));
2327 if not Is_Composite_Type (T) then
2329 ("unknown discriminants not allowed for elementary types", N);
2331 Set_Has_Unknown_Discriminants (T);
2332 Set_Is_Constrained (T, False);
2336 -- If the parent type has a known size, so does the formal, which makes
2337 -- legal representation clauses that involve the formal.
2339 Set_Size_Known_At_Compile_Time
2340 (T, Size_Known_At_Compile_Time (Entity (Subtype_Mark (Def))));
2341 end Analyze_Formal_Derived_Type;
2343 ----------------------------------
2344 -- Analyze_Formal_Discrete_Type --
2345 ----------------------------------
2347 -- The operations defined for a discrete types are those of an enumeration
2348 -- type. The size is set to an arbitrary value, for use in analyzing the
2351 procedure Analyze_Formal_Discrete_Type (T : Entity_Id; Def : Node_Id) is
2352 Loc : constant Source_Ptr := Sloc (Def);
2356 Base : constant Entity_Id :=
2358 (E_Floating_Point_Type, Current_Scope,
2359 Sloc (Defining_Identifier (Parent (Def))), 'G');
2363 Set_Ekind (T, E_Enumeration_Subtype);
2364 Set_Etype (T, Base);
2367 Set_Is_Generic_Type (T);
2368 Set_Is_Constrained (T);
2370 -- For semantic analysis, the bounds of the type must be set to some
2371 -- non-static value. The simplest is to create attribute nodes for those
2372 -- bounds, that refer to the type itself. These bounds are never
2373 -- analyzed but serve as place-holders.
2376 Make_Attribute_Reference (Loc,
2377 Attribute_Name => Name_First,
2378 Prefix => New_Occurrence_Of (T, Loc));
2382 Make_Attribute_Reference (Loc,
2383 Attribute_Name => Name_Last,
2384 Prefix => New_Occurrence_Of (T, Loc));
2387 Set_Scalar_Range (T,
2392 Set_Ekind (Base, E_Enumeration_Type);
2393 Set_Etype (Base, Base);
2394 Init_Size (Base, 8);
2395 Init_Alignment (Base);
2396 Set_Is_Generic_Type (Base);
2397 Set_Scalar_Range (Base, Scalar_Range (T));
2398 Set_Parent (Base, Parent (Def));
2399 end Analyze_Formal_Discrete_Type;
2401 ----------------------------------
2402 -- Analyze_Formal_Floating_Type --
2403 ---------------------------------
2405 procedure Analyze_Formal_Floating_Type (T : Entity_Id; Def : Node_Id) is
2406 Base : constant Entity_Id :=
2408 (E_Floating_Point_Type, Current_Scope,
2409 Sloc (Defining_Identifier (Parent (Def))), 'G');
2412 -- The various semantic attributes are taken from the predefined type
2413 -- Float, just so that all of them are initialized. Their values are
2414 -- never used because no constant folding or expansion takes place in
2415 -- the generic itself.
2418 Set_Ekind (T, E_Floating_Point_Subtype);
2419 Set_Etype (T, Base);
2420 Set_Size_Info (T, (Standard_Float));
2421 Set_RM_Size (T, RM_Size (Standard_Float));
2422 Set_Digits_Value (T, Digits_Value (Standard_Float));
2423 Set_Scalar_Range (T, Scalar_Range (Standard_Float));
2424 Set_Is_Constrained (T);
2426 Set_Is_Generic_Type (Base);
2427 Set_Etype (Base, Base);
2428 Set_Size_Info (Base, (Standard_Float));
2429 Set_RM_Size (Base, RM_Size (Standard_Float));
2430 Set_Digits_Value (Base, Digits_Value (Standard_Float));
2431 Set_Scalar_Range (Base, Scalar_Range (Standard_Float));
2432 Set_Parent (Base, Parent (Def));
2434 Check_Restriction (No_Floating_Point, Def);
2435 end Analyze_Formal_Floating_Type;
2437 -----------------------------------
2438 -- Analyze_Formal_Interface_Type;--
2439 -----------------------------------
2441 procedure Analyze_Formal_Interface_Type
2446 Loc : constant Source_Ptr := Sloc (N);
2451 Make_Full_Type_Declaration (Loc,
2452 Defining_Identifier => T,
2453 Type_Definition => Def);
2457 Set_Is_Generic_Type (T);
2458 end Analyze_Formal_Interface_Type;
2460 ---------------------------------
2461 -- Analyze_Formal_Modular_Type --
2462 ---------------------------------
2464 procedure Analyze_Formal_Modular_Type (T : Entity_Id; Def : Node_Id) is
2466 -- Apart from their entity kind, generic modular types are treated like
2467 -- signed integer types, and have the same attributes.
2469 Analyze_Formal_Signed_Integer_Type (T, Def);
2470 Set_Ekind (T, E_Modular_Integer_Subtype);
2471 Set_Ekind (Etype (T), E_Modular_Integer_Type);
2473 end Analyze_Formal_Modular_Type;
2475 ---------------------------------------
2476 -- Analyze_Formal_Object_Declaration --
2477 ---------------------------------------
2479 procedure Analyze_Formal_Object_Declaration (N : Node_Id) is
2480 E : constant Node_Id := Default_Expression (N);
2481 Id : constant Node_Id := Defining_Identifier (N);
2488 -- Determine the mode of the formal object
2490 if Out_Present (N) then
2491 K := E_Generic_In_Out_Parameter;
2493 if not In_Present (N) then
2494 Error_Msg_N ("formal generic objects cannot have mode OUT", N);
2498 K := E_Generic_In_Parameter;
2501 if Present (Subtype_Mark (N)) then
2502 Find_Type (Subtype_Mark (N));
2503 T := Entity (Subtype_Mark (N));
2505 -- Verify that there is no redundant null exclusion
2507 if Null_Exclusion_Present (N) then
2508 if not Is_Access_Type (T) then
2510 ("null exclusion can only apply to an access type", N);
2512 elsif Can_Never_Be_Null (T) then
2514 ("`NOT NULL` not allowed (& already excludes null)", N, T);
2518 -- Ada 2005 (AI-423): Formal object with an access definition
2521 Check_Access_Definition (N);
2522 T := Access_Definition
2524 N => Access_Definition (N));
2527 if Ekind (T) = E_Incomplete_Type then
2529 Error_Node : Node_Id;
2532 if Present (Subtype_Mark (N)) then
2533 Error_Node := Subtype_Mark (N);
2535 Check_Access_Definition (N);
2536 Error_Node := Access_Definition (N);
2539 Error_Msg_N ("premature usage of incomplete type", Error_Node);
2543 if K = E_Generic_In_Parameter then
2545 -- Ada 2005 (AI-287): Limited aggregates allowed in generic formals
2547 if Ada_Version < Ada_2005 and then Is_Limited_Type (T) then
2549 ("generic formal of mode IN must not be of limited type", N);
2550 Explain_Limited_Type (T, N);
2553 if Is_Abstract_Type (T) then
2555 ("generic formal of mode IN must not be of abstract type", N);
2559 Preanalyze_Spec_Expression (E, T);
2561 if Is_Limited_Type (T) and then not OK_For_Limited_Init (T, E) then
2563 ("initialization not allowed for limited types", E);
2564 Explain_Limited_Type (T, E);
2571 -- Case of generic IN OUT parameter
2574 -- If the formal has an unconstrained type, construct its actual
2575 -- subtype, as is done for subprogram formals. In this fashion, all
2576 -- its uses can refer to specific bounds.
2581 if (Is_Array_Type (T) and then not Is_Constrained (T))
2582 or else (Ekind (T) = E_Record_Type and then Has_Discriminants (T))
2585 Non_Freezing_Ref : constant Node_Id :=
2586 New_Occurrence_Of (Id, Sloc (Id));
2590 -- Make sure the actual subtype doesn't generate bogus freezing
2592 Set_Must_Not_Freeze (Non_Freezing_Ref);
2593 Decl := Build_Actual_Subtype (T, Non_Freezing_Ref);
2594 Insert_Before_And_Analyze (N, Decl);
2595 Set_Actual_Subtype (Id, Defining_Identifier (Decl));
2598 Set_Actual_Subtype (Id, T);
2603 ("initialization not allowed for `IN OUT` formals", N);
2607 if Has_Aspects (N) then
2608 Analyze_Aspect_Specifications (N, Id);
2610 end Analyze_Formal_Object_Declaration;
2612 ----------------------------------------------
2613 -- Analyze_Formal_Ordinary_Fixed_Point_Type --
2614 ----------------------------------------------
2616 procedure Analyze_Formal_Ordinary_Fixed_Point_Type
2620 Loc : constant Source_Ptr := Sloc (Def);
2621 Base : constant Entity_Id :=
2623 (E_Ordinary_Fixed_Point_Type, Current_Scope,
2624 Sloc (Defining_Identifier (Parent (Def))), 'G');
2627 -- The semantic attributes are set for completeness only, their values
2628 -- will never be used, since all properties of the type are non-static.
2631 Set_Ekind (T, E_Ordinary_Fixed_Point_Subtype);
2632 Set_Etype (T, Base);
2633 Set_Size_Info (T, Standard_Integer);
2634 Set_RM_Size (T, RM_Size (Standard_Integer));
2635 Set_Small_Value (T, Ureal_1);
2636 Set_Delta_Value (T, Ureal_1);
2637 Set_Scalar_Range (T,
2639 Low_Bound => Make_Real_Literal (Loc, Ureal_1),
2640 High_Bound => Make_Real_Literal (Loc, Ureal_1)));
2641 Set_Is_Constrained (T);
2643 Set_Is_Generic_Type (Base);
2644 Set_Etype (Base, Base);
2645 Set_Size_Info (Base, Standard_Integer);
2646 Set_RM_Size (Base, RM_Size (Standard_Integer));
2647 Set_Small_Value (Base, Ureal_1);
2648 Set_Delta_Value (Base, Ureal_1);
2649 Set_Scalar_Range (Base, Scalar_Range (T));
2650 Set_Parent (Base, Parent (Def));
2652 Check_Restriction (No_Fixed_Point, Def);
2653 end Analyze_Formal_Ordinary_Fixed_Point_Type;
2655 ----------------------------------------
2656 -- Analyze_Formal_Package_Declaration --
2657 ----------------------------------------
2659 procedure Analyze_Formal_Package_Declaration (N : Node_Id) is
2660 Gen_Id : constant Node_Id := Name (N);
2661 Loc : constant Source_Ptr := Sloc (N);
2662 Pack_Id : constant Entity_Id := Defining_Identifier (N);
2665 Gen_Unit : Entity_Id;
2668 Vis_Prims_List : Elist_Id := No_Elist;
2669 -- List of primitives made temporarily visible in the instantiation
2670 -- to match the visibility of the formal type.
2672 function Build_Local_Package return Node_Id;
2673 -- The formal package is rewritten so that its parameters are replaced
2674 -- with corresponding declarations. For parameters with bona fide
2675 -- associations these declarations are created by Analyze_Associations
2676 -- as for a regular instantiation. For boxed parameters, we preserve
2677 -- the formal declarations and analyze them, in order to introduce
2678 -- entities of the right kind in the environment of the formal.
2680 -------------------------
2681 -- Build_Local_Package --
2682 -------------------------
2684 function Build_Local_Package return Node_Id is
2686 Pack_Decl : Node_Id;
2689 -- Within the formal, the name of the generic package is a renaming
2690 -- of the formal (as for a regular instantiation).
2693 Make_Package_Declaration (Loc,
2696 (Specification (Original_Node (Gen_Decl)),
2697 Empty, Instantiating => True));
2700 Make_Package_Renaming_Declaration (Loc,
2701 Defining_Unit_Name =>
2702 Make_Defining_Identifier (Loc, Chars (Gen_Unit)),
2703 Name => New_Occurrence_Of (Formal, Loc));
2705 if Nkind (Gen_Id) = N_Identifier
2706 and then Chars (Gen_Id) = Chars (Pack_Id)
2709 ("& is hidden within declaration of instance", Gen_Id, Gen_Unit);
2712 -- If the formal is declared with a box, or with an others choice,
2713 -- create corresponding declarations for all entities in the formal
2714 -- part, so that names with the proper types are available in the
2715 -- specification of the formal package.
2717 -- On the other hand, if there are no associations, then all the
2718 -- formals must have defaults, and this will be checked by the
2719 -- call to Analyze_Associations.
2722 or else Nkind (First (Generic_Associations (N))) = N_Others_Choice
2725 Formal_Decl : Node_Id;
2728 -- TBA : for a formal package, need to recurse ???
2733 (Generic_Formal_Declarations (Original_Node (Gen_Decl)));
2734 while Present (Formal_Decl) loop
2738 (Formal_Decl, Empty, Instantiating => True));
2743 -- If generic associations are present, use Analyze_Associations to
2744 -- create the proper renaming declarations.
2748 Act_Tree : constant Node_Id :=
2750 (Original_Node (Gen_Decl), Empty,
2751 Instantiating => True);
2754 Generic_Renamings.Set_Last (0);
2755 Generic_Renamings_HTable.Reset;
2756 Instantiation_Node := N;
2759 Analyze_Associations
2760 (I_Node => Original_Node (N),
2761 Formals => Generic_Formal_Declarations (Act_Tree),
2762 F_Copy => Generic_Formal_Declarations (Gen_Decl));
2764 Vis_Prims_List := Check_Hidden_Primitives (Decls);
2768 Append (Renaming, To => Decls);
2770 -- Add generated declarations ahead of local declarations in
2773 if No (Visible_Declarations (Specification (Pack_Decl))) then
2774 Set_Visible_Declarations (Specification (Pack_Decl), Decls);
2777 (First (Visible_Declarations (Specification (Pack_Decl))),
2782 end Build_Local_Package;
2786 Save_ISMP : constant Boolean := Ignore_SPARK_Mode_Pragmas_In_Instance;
2787 -- Save flag Ignore_SPARK_Mode_Pragmas_In_Instance for restore on exit
2789 Associations : Boolean := True;
2791 Parent_Installed : Boolean := False;
2792 Parent_Instance : Entity_Id;
2793 Renaming_In_Par : Entity_Id;
2795 -- Start of processing for Analyze_Formal_Package_Declaration
2798 Check_Text_IO_Special_Unit (Gen_Id);
2801 Check_Generic_Child_Unit (Gen_Id, Parent_Installed);
2802 Gen_Unit := Entity (Gen_Id);
2804 -- Check for a formal package that is a package renaming
2806 if Present (Renamed_Object (Gen_Unit)) then
2808 -- Indicate that unit is used, before replacing it with renamed
2809 -- entity for use below.
2811 if In_Extended_Main_Source_Unit (N) then
2812 Set_Is_Instantiated (Gen_Unit);
2813 Generate_Reference (Gen_Unit, N);
2816 Gen_Unit := Renamed_Object (Gen_Unit);
2819 if Ekind (Gen_Unit) /= E_Generic_Package then
2820 Error_Msg_N ("expect generic package name", Gen_Id);
2824 elsif Gen_Unit = Current_Scope then
2826 ("generic package cannot be used as a formal package of itself",
2831 elsif In_Open_Scopes (Gen_Unit) then
2832 if Is_Compilation_Unit (Gen_Unit)
2833 and then Is_Child_Unit (Current_Scope)
2835 -- Special-case the error when the formal is a parent, and
2836 -- continue analysis to minimize cascaded errors.
2839 ("generic parent cannot be used as formal package of a child "
2844 ("generic package cannot be used as a formal package within "
2845 & "itself", Gen_Id);
2851 -- Check that name of formal package does not hide name of generic,
2852 -- or its leading prefix. This check must be done separately because
2853 -- the name of the generic has already been analyzed.
2856 Gen_Name : Entity_Id;
2860 while Nkind (Gen_Name) = N_Expanded_Name loop
2861 Gen_Name := Prefix (Gen_Name);
2864 if Chars (Gen_Name) = Chars (Pack_Id) then
2866 ("& is hidden within declaration of formal package",
2872 or else No (Generic_Associations (N))
2873 or else Nkind (First (Generic_Associations (N))) = N_Others_Choice
2875 Associations := False;
2878 -- If there are no generic associations, the generic parameters appear
2879 -- as local entities and are instantiated like them. We copy the generic
2880 -- package declaration as if it were an instantiation, and analyze it
2881 -- like a regular package, except that we treat the formals as
2882 -- additional visible components.
2884 Gen_Decl := Unit_Declaration_Node (Gen_Unit);
2886 if In_Extended_Main_Source_Unit (N) then
2887 Set_Is_Instantiated (Gen_Unit);
2888 Generate_Reference (Gen_Unit, N);
2891 Formal := New_Copy (Pack_Id);
2892 Create_Instantiation_Source (N, Gen_Unit, S_Adjustment);
2894 -- Make local generic without formals. The formals will be replaced with
2895 -- internal declarations.
2898 New_N := Build_Local_Package;
2900 -- If there are errors in the parameter list, Analyze_Associations
2901 -- raises Instantiation_Error. Patch the declaration to prevent further
2902 -- exception propagation.
2905 when Instantiation_Error =>
2906 Enter_Name (Formal);
2907 Set_Ekind (Formal, E_Variable);
2908 Set_Etype (Formal, Any_Type);
2909 Restore_Hidden_Primitives (Vis_Prims_List);
2911 if Parent_Installed then
2919 Set_Defining_Unit_Name (Specification (New_N), Formal);
2920 Set_Generic_Parent (Specification (N), Gen_Unit);
2921 Set_Instance_Env (Gen_Unit, Formal);
2922 Set_Is_Generic_Instance (Formal);
2924 Enter_Name (Formal);
2925 Set_Ekind (Formal, E_Package);
2926 Set_Etype (Formal, Standard_Void_Type);
2927 Set_Inner_Instances (Formal, New_Elmt_List);
2929 -- It is unclear that any aspects can apply to a formal package
2930 -- declaration, given that they look like a hidden conformance
2931 -- requirement on the corresponding actual. However, Abstract_State
2932 -- must be treated specially because it generates declarations that
2933 -- must appear before other declarations in the specification and
2934 -- must be analyzed at once.
2936 if Present (Aspect_Specifications (Gen_Decl)) then
2937 if No (Aspect_Specifications (N)) then
2938 Set_Aspect_Specifications (N, New_List);
2939 Set_Has_Aspects (N);
2943 ASN : Node_Id := First (Aspect_Specifications (Gen_Decl));
2947 while Present (ASN) loop
2948 if Get_Aspect_Id (ASN) = Aspect_Abstract_State then
2950 Copy_Generic_Node (ASN, Empty, Instantiating => True);
2951 Set_Entity (New_A, Formal);
2952 Set_Analyzed (New_A, False);
2953 Append (New_A, Aspect_Specifications (N));
2954 Analyze_Aspect_Specifications (N, Formal);
2963 Push_Scope (Formal);
2965 -- Manually set the SPARK_Mode from the context because the package
2966 -- declaration is never analyzed.
2968 Set_SPARK_Pragma (Formal, SPARK_Mode_Pragma);
2969 Set_SPARK_Aux_Pragma (Formal, SPARK_Mode_Pragma);
2970 Set_SPARK_Pragma_Inherited (Formal);
2971 Set_SPARK_Aux_Pragma_Inherited (Formal);
2973 if Is_Child_Unit (Gen_Unit) and then Parent_Installed then
2975 -- Similarly, we have to make the name of the formal visible in the
2976 -- parent instance, to resolve properly fully qualified names that
2977 -- may appear in the generic unit. The parent instance has been
2978 -- placed on the scope stack ahead of the current scope.
2980 Parent_Instance := Scope_Stack.Table (Scope_Stack.Last - 1).Entity;
2983 Make_Defining_Identifier (Loc, Chars (Gen_Unit));
2984 Set_Ekind (Renaming_In_Par, E_Package);
2985 Set_Etype (Renaming_In_Par, Standard_Void_Type);
2986 Set_Scope (Renaming_In_Par, Parent_Instance);
2987 Set_Parent (Renaming_In_Par, Parent (Formal));
2988 Set_Renamed_Object (Renaming_In_Par, Formal);
2989 Append_Entity (Renaming_In_Par, Parent_Instance);
2992 -- A formal package declaration behaves as a package instantiation with
2993 -- respect to SPARK_Mode "off". If the annotation is "off" or altogether
2994 -- missing, set the global flag which signals Analyze_Pragma to ingnore
2995 -- all SPARK_Mode pragmas within the generic_package_name.
2997 if SPARK_Mode /= On then
2998 Ignore_SPARK_Mode_Pragmas_In_Instance := True;
3000 -- Mark the formal spec in case the body is instantiated at a later
3001 -- pass. This preserves the original context in effect for the body.
3003 Set_Ignore_SPARK_Mode_Pragmas (Formal);
3006 Analyze (Specification (N));
3008 -- The formals for which associations are provided are not visible
3009 -- outside of the formal package. The others are still declared by a
3010 -- formal parameter declaration.
3012 -- If there are no associations, the only local entity to hide is the
3013 -- generated package renaming itself.
3019 E := First_Entity (Formal);
3020 while Present (E) loop
3021 if Associations and then not Is_Generic_Formal (E) then
3025 if Ekind (E) = E_Package and then Renamed_Entity (E) = Formal then
3034 End_Package_Scope (Formal);
3035 Restore_Hidden_Primitives (Vis_Prims_List);
3037 if Parent_Installed then
3043 -- Inside the generic unit, the formal package is a regular package, but
3044 -- no body is needed for it. Note that after instantiation, the defining
3045 -- unit name we need is in the new tree and not in the original (see
3046 -- Package_Instantiation). A generic formal package is an instance, and
3047 -- can be used as an actual for an inner instance.
3049 Set_Has_Completion (Formal, True);
3051 -- Add semantic information to the original defining identifier.
3053 Set_Ekind (Pack_Id, E_Package);
3054 Set_Etype (Pack_Id, Standard_Void_Type);
3055 Set_Scope (Pack_Id, Scope (Formal));
3056 Set_Has_Completion (Pack_Id, True);
3059 if Has_Aspects (N) then
3060 -- Unclear that any other aspects may appear here, snalyze them
3061 -- for completion, given that the grammar allows their appearance.
3063 Analyze_Aspect_Specifications (N, Pack_Id);
3066 Ignore_SPARK_Mode_Pragmas_In_Instance := Save_ISMP;
3067 end Analyze_Formal_Package_Declaration;
3069 ---------------------------------
3070 -- Analyze_Formal_Private_Type --
3071 ---------------------------------
3073 procedure Analyze_Formal_Private_Type
3079 New_Private_Type (N, T, Def);
3081 -- Set the size to an arbitrary but legal value
3083 Set_Size_Info (T, Standard_Integer);
3084 Set_RM_Size (T, RM_Size (Standard_Integer));
3085 end Analyze_Formal_Private_Type;
3087 ------------------------------------
3088 -- Analyze_Formal_Incomplete_Type --
3089 ------------------------------------
3091 procedure Analyze_Formal_Incomplete_Type
3097 Set_Ekind (T, E_Incomplete_Type);
3099 Set_Private_Dependents (T, New_Elmt_List);
3101 if Tagged_Present (Def) then
3102 Set_Is_Tagged_Type (T);
3103 Make_Class_Wide_Type (T);
3104 Set_Direct_Primitive_Operations (T, New_Elmt_List);
3106 end Analyze_Formal_Incomplete_Type;
3108 ----------------------------------------
3109 -- Analyze_Formal_Signed_Integer_Type --
3110 ----------------------------------------
3112 procedure Analyze_Formal_Signed_Integer_Type
3116 Base : constant Entity_Id :=
3118 (E_Signed_Integer_Type,
3120 Sloc (Defining_Identifier (Parent (Def))), 'G');
3125 Set_Ekind (T, E_Signed_Integer_Subtype);
3126 Set_Etype (T, Base);
3127 Set_Size_Info (T, Standard_Integer);
3128 Set_RM_Size (T, RM_Size (Standard_Integer));
3129 Set_Scalar_Range (T, Scalar_Range (Standard_Integer));
3130 Set_Is_Constrained (T);
3132 Set_Is_Generic_Type (Base);
3133 Set_Size_Info (Base, Standard_Integer);
3134 Set_RM_Size (Base, RM_Size (Standard_Integer));
3135 Set_Etype (Base, Base);
3136 Set_Scalar_Range (Base, Scalar_Range (Standard_Integer));
3137 Set_Parent (Base, Parent (Def));
3138 end Analyze_Formal_Signed_Integer_Type;
3140 -------------------------------------------
3141 -- Analyze_Formal_Subprogram_Declaration --
3142 -------------------------------------------
3144 procedure Analyze_Formal_Subprogram_Declaration (N : Node_Id) is
3145 Spec : constant Node_Id := Specification (N);
3146 Def : constant Node_Id := Default_Name (N);
3147 Nam : constant Entity_Id := Defining_Unit_Name (Spec);
3155 if Nkind (Nam) = N_Defining_Program_Unit_Name then
3156 Error_Msg_N ("name of formal subprogram must be a direct name", Nam);
3160 Analyze_Subprogram_Declaration (N);
3161 Set_Is_Formal_Subprogram (Nam);
3162 Set_Has_Completion (Nam);
3164 if Nkind (N) = N_Formal_Abstract_Subprogram_Declaration then
3165 Set_Is_Abstract_Subprogram (Nam);
3167 Set_Is_Dispatching_Operation (Nam);
3169 -- A formal abstract procedure cannot have a null default
3170 -- (RM 12.6(4.1/2)).
3172 if Nkind (Spec) = N_Procedure_Specification
3173 and then Null_Present (Spec)
3176 ("a formal abstract subprogram cannot default to null", Spec);
3180 Ctrl_Type : constant Entity_Id := Find_Dispatching_Type (Nam);
3182 if No (Ctrl_Type) then
3184 ("abstract formal subprogram must have a controlling type",
3187 elsif Ada_Version >= Ada_2012
3188 and then Is_Incomplete_Type (Ctrl_Type)
3191 ("controlling type of abstract formal subprogram cannot "
3192 & "be incomplete type", N, Ctrl_Type);
3195 Check_Controlling_Formals (Ctrl_Type, Nam);
3200 -- Default name is resolved at the point of instantiation
3202 if Box_Present (N) then
3205 -- Else default is bound at the point of generic declaration
3207 elsif Present (Def) then
3208 if Nkind (Def) = N_Operator_Symbol then
3209 Find_Direct_Name (Def);
3211 elsif Nkind (Def) /= N_Attribute_Reference then
3215 -- For an attribute reference, analyze the prefix and verify
3216 -- that it has the proper profile for the subprogram.
3218 Analyze (Prefix (Def));
3219 Valid_Default_Attribute (Nam, Def);
3223 -- Default name may be overloaded, in which case the interpretation
3224 -- with the correct profile must be selected, as for a renaming.
3225 -- If the definition is an indexed component, it must denote a
3226 -- member of an entry family. If it is a selected component, it
3227 -- can be a protected operation.
3229 if Etype (Def) = Any_Type then
3232 elsif Nkind (Def) = N_Selected_Component then
3233 if not Is_Overloadable (Entity (Selector_Name (Def))) then
3234 Error_Msg_N ("expect valid subprogram name as default", Def);
3237 elsif Nkind (Def) = N_Indexed_Component then
3238 if Is_Entity_Name (Prefix (Def)) then
3239 if Ekind (Entity (Prefix (Def))) /= E_Entry_Family then
3240 Error_Msg_N ("expect valid subprogram name as default", Def);
3243 elsif Nkind (Prefix (Def)) = N_Selected_Component then
3244 if Ekind (Entity (Selector_Name (Prefix (Def)))) /=
3247 Error_Msg_N ("expect valid subprogram name as default", Def);
3251 Error_Msg_N ("expect valid subprogram name as default", Def);
3255 elsif Nkind (Def) = N_Character_Literal then
3257 -- Needs some type checks: subprogram should be parameterless???
3259 Resolve (Def, (Etype (Nam)));
3261 elsif not Is_Entity_Name (Def)
3262 or else not Is_Overloadable (Entity (Def))
3264 Error_Msg_N ("expect valid subprogram name as default", Def);
3267 elsif not Is_Overloaded (Def) then
3268 Subp := Entity (Def);
3271 Error_Msg_N ("premature usage of formal subprogram", Def);
3273 elsif not Entity_Matches_Spec (Subp, Nam) then
3274 Error_Msg_N ("no visible entity matches specification", Def);
3277 -- More than one interpretation, so disambiguate as for a renaming
3282 I1 : Interp_Index := 0;
3288 Get_First_Interp (Def, I, It);
3289 while Present (It.Nam) loop
3290 if Entity_Matches_Spec (It.Nam, Nam) then
3291 if Subp /= Any_Id then
3292 It1 := Disambiguate (Def, I1, I, Etype (Subp));
3294 if It1 = No_Interp then
3295 Error_Msg_N ("ambiguous default subprogram", Def);
3308 Get_Next_Interp (I, It);
3312 if Subp /= Any_Id then
3314 -- Subprogram found, generate reference to it
3316 Set_Entity (Def, Subp);
3317 Generate_Reference (Subp, Def);
3320 Error_Msg_N ("premature usage of formal subprogram", Def);
3322 elsif Ekind (Subp) /= E_Operator then
3323 Check_Mode_Conformant (Subp, Nam);
3327 Error_Msg_N ("no visible subprogram matches specification", N);
3333 if Has_Aspects (N) then
3334 Analyze_Aspect_Specifications (N, Nam);
3337 end Analyze_Formal_Subprogram_Declaration;
3339 -------------------------------------
3340 -- Analyze_Formal_Type_Declaration --
3341 -------------------------------------
3343 procedure Analyze_Formal_Type_Declaration (N : Node_Id) is
3344 Def : constant Node_Id := Formal_Type_Definition (N);
3348 T := Defining_Identifier (N);
3350 if Present (Discriminant_Specifications (N))
3351 and then Nkind (Def) /= N_Formal_Private_Type_Definition
3354 ("discriminants not allowed for this formal type", T);
3357 -- Enter the new name, and branch to specific routine
3360 when N_Formal_Private_Type_Definition =>
3361 Analyze_Formal_Private_Type (N, T, Def);
3363 when N_Formal_Derived_Type_Definition =>
3364 Analyze_Formal_Derived_Type (N, T, Def);
3366 when N_Formal_Incomplete_Type_Definition =>
3367 Analyze_Formal_Incomplete_Type (T, Def);
3369 when N_Formal_Discrete_Type_Definition =>
3370 Analyze_Formal_Discrete_Type (T, Def);
3372 when N_Formal_Signed_Integer_Type_Definition =>
3373 Analyze_Formal_Signed_Integer_Type (T, Def);
3375 when N_Formal_Modular_Type_Definition =>
3376 Analyze_Formal_Modular_Type (T, Def);
3378 when N_Formal_Floating_Point_Definition =>
3379 Analyze_Formal_Floating_Type (T, Def);
3381 when N_Formal_Ordinary_Fixed_Point_Definition =>
3382 Analyze_Formal_Ordinary_Fixed_Point_Type (T, Def);
3384 when N_Formal_Decimal_Fixed_Point_Definition =>
3385 Analyze_Formal_Decimal_Fixed_Point_Type (T, Def);
3387 when N_Array_Type_Definition =>
3388 Analyze_Formal_Array_Type (T, Def);
3390 when N_Access_Function_Definition
3391 | N_Access_Procedure_Definition
3392 | N_Access_To_Object_Definition
3394 Analyze_Generic_Access_Type (T, Def);
3396 -- Ada 2005: a interface declaration is encoded as an abstract
3397 -- record declaration or a abstract type derivation.
3399 when N_Record_Definition =>
3400 Analyze_Formal_Interface_Type (N, T, Def);
3402 when N_Derived_Type_Definition =>
3403 Analyze_Formal_Derived_Interface_Type (N, T, Def);
3409 raise Program_Error;
3412 -- A formal type declaration declares a type and its first
3415 Set_Is_Generic_Type (T);
3416 Set_Is_First_Subtype (T);
3418 if Has_Aspects (N) then
3419 Analyze_Aspect_Specifications (N, T);
3421 end Analyze_Formal_Type_Declaration;
3423 ------------------------------------
3424 -- Analyze_Function_Instantiation --
3425 ------------------------------------
3427 procedure Analyze_Function_Instantiation (N : Node_Id) is
3429 Analyze_Subprogram_Instantiation (N, E_Function);
3430 end Analyze_Function_Instantiation;
3432 ---------------------------------
3433 -- Analyze_Generic_Access_Type --
3434 ---------------------------------
3436 procedure Analyze_Generic_Access_Type (T : Entity_Id; Def : Node_Id) is
3440 if Nkind (Def) = N_Access_To_Object_Definition then
3441 Access_Type_Declaration (T, Def);
3443 if Is_Incomplete_Or_Private_Type (Designated_Type (T))
3444 and then No (Full_View (Designated_Type (T)))
3445 and then not Is_Generic_Type (Designated_Type (T))
3447 Error_Msg_N ("premature usage of incomplete type", Def);
3449 elsif not Is_Entity_Name (Subtype_Indication (Def)) then
3451 ("only a subtype mark is allowed in a formal", Def);
3455 Access_Subprogram_Declaration (T, Def);
3457 end Analyze_Generic_Access_Type;
3459 ---------------------------------
3460 -- Analyze_Generic_Formal_Part --
3461 ---------------------------------
3463 procedure Analyze_Generic_Formal_Part (N : Node_Id) is
3464 Gen_Parm_Decl : Node_Id;
3467 -- The generic formals are processed in the scope of the generic unit,
3468 -- where they are immediately visible. The scope is installed by the
3471 Gen_Parm_Decl := First (Generic_Formal_Declarations (N));
3472 while Present (Gen_Parm_Decl) loop
3473 Analyze (Gen_Parm_Decl);
3474 Next (Gen_Parm_Decl);
3477 Generate_Reference_To_Generic_Formals (Current_Scope);
3478 end Analyze_Generic_Formal_Part;
3480 ------------------------------------------
3481 -- Analyze_Generic_Package_Declaration --
3482 ------------------------------------------
3484 procedure Analyze_Generic_Package_Declaration (N : Node_Id) is
3485 Decls : constant List_Id := Visible_Declarations (Specification (N));
3486 Loc : constant Source_Ptr := Sloc (N);
3492 Save_Parent : Node_Id;
3495 Check_SPARK_05_Restriction ("generic is not allowed", N);
3497 -- A generic may grant access to its private enclosing context depending
3498 -- on the placement of its corresponding body. From elaboration point of
3499 -- view, the flow of execution may enter this private context, and then
3500 -- reach an external unit, thus producing a dependency on that external
3501 -- unit. For such a path to be properly discovered and encoded in the
3502 -- ALI file of the main unit, let the ABE mechanism process the body of
3503 -- the main unit, and encode all relevant invocation constructs and the
3504 -- relations between them.
3506 Mark_Save_Invocation_Graph_Of_Body;
3508 -- We introduce a renaming of the enclosing package, to have a usable
3509 -- entity as the prefix of an expanded name for a local entity of the
3510 -- form Par.P.Q, where P is the generic package. This is because a local
3511 -- entity named P may hide it, so that the usual visibility rules in
3512 -- the instance will not resolve properly.
3515 Make_Package_Renaming_Declaration (Loc,
3516 Defining_Unit_Name =>
3517 Make_Defining_Identifier (Loc,
3518 Chars => New_External_Name (Chars (Defining_Entity (N)), "GH")),
3520 Make_Identifier (Loc, Chars (Defining_Entity (N))));
3522 -- The declaration is inserted before other declarations, but before
3523 -- pragmas that may be library-unit pragmas and must appear before other
3524 -- declarations. The pragma Compile_Time_Error is not in this class, and
3525 -- may contain an expression that includes such a qualified name, so the
3526 -- renaming declaration must appear before it.
3528 -- Are there other pragmas that require this special handling ???
3530 if Present (Decls) then
3531 Decl := First (Decls);
3532 while Present (Decl)
3533 and then Nkind (Decl) = N_Pragma
3534 and then Get_Pragma_Id (Decl) /= Pragma_Compile_Time_Error
3539 if Present (Decl) then
3540 Insert_Before (Decl, Renaming);
3542 Append (Renaming, Visible_Declarations (Specification (N)));
3546 Set_Visible_Declarations (Specification (N), New_List (Renaming));
3549 -- Create copy of generic unit, and save for instantiation. If the unit
3550 -- is a child unit, do not copy the specifications for the parent, which
3551 -- are not part of the generic tree.
3553 Save_Parent := Parent_Spec (N);
3554 Set_Parent_Spec (N, Empty);
3556 New_N := Copy_Generic_Node (N, Empty, Instantiating => False);
3557 Set_Parent_Spec (New_N, Save_Parent);
3560 -- Once the contents of the generic copy and the template are swapped,
3561 -- do the same for their respective aspect specifications.
3563 Exchange_Aspects (N, New_N);
3565 -- Collect all contract-related source pragmas found within the template
3566 -- and attach them to the contract of the package spec. This contract is
3567 -- used in the capture of global references within annotations.
3569 Create_Generic_Contract (N);
3571 Id := Defining_Entity (N);
3572 Generate_Definition (Id);
3574 -- Expansion is not applied to generic units
3579 Set_Ekind (Id, E_Generic_Package);
3580 Set_Etype (Id, Standard_Void_Type);
3582 -- Set SPARK_Mode from context
3584 Set_SPARK_Pragma (Id, SPARK_Mode_Pragma);
3585 Set_SPARK_Aux_Pragma (Id, SPARK_Mode_Pragma);
3586 Set_SPARK_Pragma_Inherited (Id);
3587 Set_SPARK_Aux_Pragma_Inherited (Id);
3589 -- Preserve relevant elaboration-related attributes of the context which
3590 -- are no longer available or very expensive to recompute once analysis,
3591 -- resolution, and expansion are over.
3593 Mark_Elaboration_Attributes
3598 -- Analyze aspects now, so that generated pragmas appear in the
3599 -- declarations before building and analyzing the generic copy.
3601 if Has_Aspects (N) then
3602 Analyze_Aspect_Specifications (N, Id);
3606 Enter_Generic_Scope (Id);
3607 Set_Inner_Instances (Id, New_Elmt_List);
3609 Set_Categorization_From_Pragmas (N);
3610 Set_Is_Pure (Id, Is_Pure (Current_Scope));
3612 -- Link the declaration of the generic homonym in the generic copy to
3613 -- the package it renames, so that it is always resolved properly.
3615 Set_Generic_Homonym (Id, Defining_Unit_Name (Renaming));
3616 Set_Entity (Associated_Node (Name (Renaming)), Id);
3618 -- For a library unit, we have reconstructed the entity for the unit,
3619 -- and must reset it in the library tables.
3621 if Nkind (Parent (N)) = N_Compilation_Unit then
3622 Set_Cunit_Entity (Current_Sem_Unit, Id);
3625 Analyze_Generic_Formal_Part (N);
3627 -- After processing the generic formals, analysis proceeds as for a
3628 -- non-generic package.
3630 Analyze (Specification (N));
3632 Validate_Categorization_Dependency (N, Id);
3636 End_Package_Scope (Id);
3637 Exit_Generic_Scope (Id);
3639 -- If the generic appears within a package unit, the body of that unit
3640 -- has to be present for instantiation and inlining.
3642 if Nkind (Unit (Cunit (Current_Sem_Unit))) = N_Package_Declaration then
3643 Set_Body_Needed_For_Inlining
3644 (Defining_Entity (Unit (Cunit (Current_Sem_Unit))));
3647 if Nkind (Parent (N)) /= N_Compilation_Unit then
3648 Move_Freeze_Nodes (Id, N, Visible_Declarations (Specification (N)));
3649 Move_Freeze_Nodes (Id, N, Private_Declarations (Specification (N)));
3650 Move_Freeze_Nodes (Id, N, Generic_Formal_Declarations (N));
3653 Set_Body_Required (Parent (N), Unit_Requires_Body (Id));
3654 Validate_RT_RAT_Component (N);
3656 -- If this is a spec without a body, check that generic parameters
3659 if not Body_Required (Parent (N)) then
3660 Check_References (Id);
3664 -- If there is a specified storage pool in the context, create an
3665 -- aspect on the package declaration, so that it is used in any
3666 -- instance that does not override it.
3668 if Present (Default_Pool) then
3674 Make_Aspect_Specification (Loc,
3675 Identifier => Make_Identifier (Loc, Name_Default_Storage_Pool),
3676 Expression => New_Copy (Default_Pool));
3678 if No (Aspect_Specifications (Specification (N))) then
3679 Set_Aspect_Specifications (Specification (N), New_List (ASN));
3681 Append (ASN, Aspect_Specifications (Specification (N)));
3685 end Analyze_Generic_Package_Declaration;
3687 --------------------------------------------
3688 -- Analyze_Generic_Subprogram_Declaration --
3689 --------------------------------------------
3691 procedure Analyze_Generic_Subprogram_Declaration (N : Node_Id) is
3695 Result_Type : Entity_Id;
3696 Save_Parent : Node_Id;
3701 Check_SPARK_05_Restriction ("generic is not allowed", N);
3703 -- A generic may grant access to its private enclosing context depending
3704 -- on the placement of its corresponding body. From elaboration point of
3705 -- view, the flow of execution may enter this private context, and then
3706 -- reach an external unit, thus producing a dependency on that external
3707 -- unit. For such a path to be properly discovered and encoded in the
3708 -- ALI file of the main unit, let the ABE mechanism process the body of
3709 -- the main unit, and encode all relevant invocation constructs and the
3710 -- relations between them.
3712 Mark_Save_Invocation_Graph_Of_Body;
3714 -- Create copy of generic unit, and save for instantiation. If the unit
3715 -- is a child unit, do not copy the specifications for the parent, which
3716 -- are not part of the generic tree.
3718 Save_Parent := Parent_Spec (N);
3719 Set_Parent_Spec (N, Empty);
3721 New_N := Copy_Generic_Node (N, Empty, Instantiating => False);
3722 Set_Parent_Spec (New_N, Save_Parent);
3725 -- Once the contents of the generic copy and the template are swapped,
3726 -- do the same for their respective aspect specifications.
3728 Exchange_Aspects (N, New_N);
3730 -- Collect all contract-related source pragmas found within the template
3731 -- and attach them to the contract of the subprogram spec. This contract
3732 -- is used in the capture of global references within annotations.
3734 Create_Generic_Contract (N);
3736 Spec := Specification (N);
3737 Id := Defining_Entity (Spec);
3738 Generate_Definition (Id);
3740 if Nkind (Id) = N_Defining_Operator_Symbol then
3742 ("operator symbol not allowed for generic subprogram", Id);
3748 Set_Scope_Depth_Value (Id, Scope_Depth (Current_Scope) + 1);
3750 -- Analyze the aspects of the generic copy to ensure that all generated
3751 -- pragmas (if any) perform their semantic effects.
3753 if Has_Aspects (N) then
3754 Analyze_Aspect_Specifications (N, Id);
3758 Enter_Generic_Scope (Id);
3759 Set_Inner_Instances (Id, New_Elmt_List);
3760 Set_Is_Pure (Id, Is_Pure (Current_Scope));
3762 Analyze_Generic_Formal_Part (N);
3764 if Nkind (Spec) = N_Function_Specification then
3765 Set_Ekind (Id, E_Generic_Function);
3767 Set_Ekind (Id, E_Generic_Procedure);
3770 -- Set SPARK_Mode from context
3772 Set_SPARK_Pragma (Id, SPARK_Mode_Pragma);
3773 Set_SPARK_Pragma_Inherited (Id);
3775 -- Preserve relevant elaboration-related attributes of the context which
3776 -- are no longer available or very expensive to recompute once analysis,
3777 -- resolution, and expansion are over.
3779 Mark_Elaboration_Attributes
3784 Formals := Parameter_Specifications (Spec);
3786 if Present (Formals) then
3787 Process_Formals (Formals, Spec);
3790 if Nkind (Spec) = N_Function_Specification then
3791 if Nkind (Result_Definition (Spec)) = N_Access_Definition then
3792 Result_Type := Access_Definition (Spec, Result_Definition (Spec));
3793 Set_Etype (Id, Result_Type);
3795 -- Check restriction imposed by AI05-073: a generic function
3796 -- cannot return an abstract type or an access to such.
3798 -- This is a binding interpretation should it apply to earlier
3799 -- versions of Ada as well as Ada 2012???
3801 if Is_Abstract_Type (Designated_Type (Result_Type))
3802 and then Ada_Version >= Ada_2012
3805 ("generic function cannot have an access result "
3806 & "that designates an abstract type", Spec);
3810 Find_Type (Result_Definition (Spec));
3811 Typ := Entity (Result_Definition (Spec));
3813 if Is_Abstract_Type (Typ)
3814 and then Ada_Version >= Ada_2012
3817 ("generic function cannot have abstract result type", Spec);
3820 -- If a null exclusion is imposed on the result type, then create
3821 -- a null-excluding itype (an access subtype) and use it as the
3822 -- function's Etype.
3824 if Is_Access_Type (Typ)
3825 and then Null_Exclusion_Present (Spec)
3828 Create_Null_Excluding_Itype
3830 Related_Nod => Spec,
3831 Scope_Id => Defining_Unit_Name (Spec)));
3833 Set_Etype (Id, Typ);
3838 Set_Etype (Id, Standard_Void_Type);
3841 -- For a library unit, we have reconstructed the entity for the unit,
3842 -- and must reset it in the library tables. We also make sure that
3843 -- Body_Required is set properly in the original compilation unit node.
3845 if Nkind (Parent (N)) = N_Compilation_Unit then
3846 Set_Cunit_Entity (Current_Sem_Unit, Id);
3847 Set_Body_Required (Parent (N), Unit_Requires_Body (Id));
3850 -- If the generic appears within a package unit, the body of that unit
3851 -- has to be present for instantiation and inlining.
3853 if Nkind (Unit (Cunit (Current_Sem_Unit))) = N_Package_Declaration
3854 and then Unit_Requires_Body (Id)
3856 Set_Body_Needed_For_Inlining
3857 (Defining_Entity (Unit (Cunit (Current_Sem_Unit))));
3860 Set_Categorization_From_Pragmas (N);
3861 Validate_Categorization_Dependency (N, Id);
3863 -- Capture all global references that occur within the profile of the
3864 -- generic subprogram. Aspects are not part of this processing because
3865 -- they must be delayed. If processed now, Save_Global_References will
3866 -- destroy the Associated_Node links and prevent the capture of global
3867 -- references when the contract of the generic subprogram is analyzed.
3869 Save_Global_References (Original_Node (N));
3873 Exit_Generic_Scope (Id);
3874 Generate_Reference_To_Formals (Id);
3876 List_Inherited_Pre_Post_Aspects (Id);
3877 end Analyze_Generic_Subprogram_Declaration;
3879 -----------------------------------
3880 -- Analyze_Package_Instantiation --
3881 -----------------------------------
3883 -- WARNING: This routine manages Ghost and SPARK regions. Return statements
3884 -- must be replaced by gotos which jump to the end of the routine in order
3885 -- to restore the Ghost and SPARK modes.
3887 procedure Analyze_Package_Instantiation (N : Node_Id) is
3888 Has_Inline_Always : Boolean := False;
3889 -- Set if the generic unit contains any subprograms with Inline_Always.
3890 -- Only relevant when back-end inlining is not enabled.
3892 function Might_Inline_Subp (Gen_Unit : Entity_Id) return Boolean;
3893 -- Return True if inlining is active and Gen_Unit contains inlined
3894 -- subprograms. In this case, we may either instantiate the body when
3895 -- front-end inlining is enabled, or add a pending instantiation when
3896 -- back-end inlining is enabled. In the former case, this may cause
3897 -- superfluous instantiations, but in either case we need to perform
3898 -- the instantiation of the body in the context of the instance and
3899 -- not in that of the point of inlining.
3901 function Needs_Body_Instantiated (Gen_Unit : Entity_Id) return Boolean;
3902 -- Return True if Gen_Unit needs to have its body instantiated in the
3903 -- context of N. This in particular excludes generic contexts.
3905 -----------------------
3906 -- Might_Inline_Subp --
3907 -----------------------
3909 function Might_Inline_Subp (Gen_Unit : Entity_Id) return Boolean is
3913 if Inline_Processing_Required then
3914 -- No need to recompute the answer if we know it is positive
3915 -- and back-end inlining is enabled.
3917 if Is_Inlined (Gen_Unit) and then Back_End_Inlining then
3921 E := First_Entity (Gen_Unit);
3922 while Present (E) loop
3923 if Is_Subprogram (E) and then Is_Inlined (E) then
3924 -- Remember if there are any subprograms with Inline_Always
3926 if Has_Pragma_Inline_Always (E) then
3927 Has_Inline_Always := True;
3930 Set_Is_Inlined (Gen_Unit);
3939 end Might_Inline_Subp;
3941 -------------------------------
3942 -- Needs_Body_Instantiated --
3943 -------------------------------
3945 function Needs_Body_Instantiated (Gen_Unit : Entity_Id) return Boolean is
3947 -- No need to instantiate bodies in generic units
3949 if Is_Generic_Unit (Cunit_Entity (Main_Unit)) then
3953 -- If the instantiation is in the main unit, then the body is needed
3955 if Is_In_Main_Unit (N) then
3959 -- If not, then again no need to instantiate bodies in generic units
3961 if Is_Generic_Unit (Cunit_Entity (Get_Code_Unit (N))) then
3965 -- Here we have a special handling for back-end inlining: if inline
3966 -- processing is required, then we unconditionally want to have the
3967 -- body instantiated. The reason is that Might_Inline_Subp does not
3968 -- catch all the cases (as it does not recurse into nested packages)
3969 -- so this avoids the need to patch things up afterwards. Moreover,
3970 -- these instantiations are only performed on demand when back-end
3971 -- inlining is enabled, so this causes very little extra work.
3973 if Inline_Processing_Required and then Back_End_Inlining then
3977 -- We want to have the bodies instantiated in non-main units if
3978 -- they might contribute inlined subprograms.
3980 return Might_Inline_Subp (Gen_Unit);
3981 end Needs_Body_Instantiated;
3983 -- Local declarations
3985 Gen_Id : constant Node_Id := Name (N);
3986 Inst_Id : constant Entity_Id := Defining_Entity (N);
3987 Is_Actual_Pack : constant Boolean := Is_Internal (Inst_Id);
3988 Loc : constant Source_Ptr := Sloc (N);
3990 Saved_GM : constant Ghost_Mode_Type := Ghost_Mode;
3991 Saved_IGR : constant Node_Id := Ignored_Ghost_Region;
3992 Saved_ISMP : constant Boolean :=
3993 Ignore_SPARK_Mode_Pragmas_In_Instance;
3994 Saved_SM : constant SPARK_Mode_Type := SPARK_Mode;
3995 Saved_SMP : constant Node_Id := SPARK_Mode_Pragma;
3996 -- Save the Ghost and SPARK mode-related data to restore on exit
3998 Saved_Style_Check : constant Boolean := Style_Check;
3999 -- Save style check mode for restore on exit
4002 Act_Decl_Name : Node_Id;
4003 Act_Decl_Id : Entity_Id;
4006 Env_Installed : Boolean := False;
4009 Gen_Unit : Entity_Id;
4010 Inline_Now : Boolean := False;
4011 Needs_Body : Boolean;
4012 Parent_Installed : Boolean := False;
4013 Renaming_List : List_Id;
4014 Unit_Renaming : Node_Id;
4016 Vis_Prims_List : Elist_Id := No_Elist;
4017 -- List of primitives made temporarily visible in the instantiation
4018 -- to match the visibility of the formal type
4020 -- Start of processing for Analyze_Package_Instantiation
4023 -- Preserve relevant elaboration-related attributes of the context which
4024 -- are no longer available or very expensive to recompute once analysis,
4025 -- resolution, and expansion are over.
4027 Mark_Elaboration_Attributes
4034 Check_SPARK_05_Restriction ("generic is not allowed", N);
4036 -- Very first thing: check for Text_IO special unit in case we are
4037 -- instantiating one of the children of [[Wide_]Wide_]Text_IO.
4039 Check_Text_IO_Special_Unit (Name (N));
4041 -- Make node global for error reporting
4043 Instantiation_Node := N;
4045 -- Case of instantiation of a generic package
4047 if Nkind (N) = N_Package_Instantiation then
4048 Act_Decl_Id := New_Copy (Defining_Entity (N));
4049 Set_Comes_From_Source (Act_Decl_Id, True);
4051 if Nkind (Defining_Unit_Name (N)) = N_Defining_Program_Unit_Name then
4053 Make_Defining_Program_Unit_Name (Loc,
4055 New_Copy_Tree (Name (Defining_Unit_Name (N))),
4056 Defining_Identifier => Act_Decl_Id);
4058 Act_Decl_Name := Act_Decl_Id;
4061 -- Case of instantiation of a formal package
4064 Act_Decl_Id := Defining_Identifier (N);
4065 Act_Decl_Name := Act_Decl_Id;
4068 Generate_Definition (Act_Decl_Id);
4069 Set_Ekind (Act_Decl_Id, E_Package);
4071 -- Initialize list of incomplete actuals before analysis
4073 Set_Incomplete_Actuals (Act_Decl_Id, New_Elmt_List);
4075 Preanalyze_Actuals (N, Act_Decl_Id);
4077 -- Turn off style checking in instances. If the check is enabled on the
4078 -- generic unit, a warning in an instance would just be noise. If not
4079 -- enabled on the generic, then a warning in an instance is just wrong.
4080 -- This must be done after analyzing the actuals, which do come from
4081 -- source and are subject to style checking.
4083 Style_Check := False;
4086 Env_Installed := True;
4088 -- Reset renaming map for formal types. The mapping is established
4089 -- when analyzing the generic associations, but some mappings are
4090 -- inherited from formal packages of parent units, and these are
4091 -- constructed when the parents are installed.
4093 Generic_Renamings.Set_Last (0);
4094 Generic_Renamings_HTable.Reset;
4096 Check_Generic_Child_Unit (Gen_Id, Parent_Installed);
4097 Gen_Unit := Entity (Gen_Id);
4099 -- A package instantiation is Ghost when it is subject to pragma Ghost
4100 -- or the generic template is Ghost. Set the mode now to ensure that
4101 -- any nodes generated during analysis and expansion are marked as
4104 Mark_And_Set_Ghost_Instantiation (N, Gen_Unit);
4106 -- Verify that it is the name of a generic package
4108 -- A visibility glitch: if the instance is a child unit and the generic
4109 -- is the generic unit of a parent instance (i.e. both the parent and
4110 -- the child units are instances of the same package) the name now
4111 -- denotes the renaming within the parent, not the intended generic
4112 -- unit. See if there is a homonym that is the desired generic. The
4113 -- renaming declaration must be visible inside the instance of the
4114 -- child, but not when analyzing the name in the instantiation itself.
4116 if Ekind (Gen_Unit) = E_Package
4117 and then Present (Renamed_Entity (Gen_Unit))
4118 and then In_Open_Scopes (Renamed_Entity (Gen_Unit))
4119 and then Is_Generic_Instance (Renamed_Entity (Gen_Unit))
4120 and then Present (Homonym (Gen_Unit))
4122 Gen_Unit := Homonym (Gen_Unit);
4125 if Etype (Gen_Unit) = Any_Type then
4129 elsif Ekind (Gen_Unit) /= E_Generic_Package then
4131 -- Ada 2005 (AI-50217): Cannot use instance in limited with_clause
4133 if From_Limited_With (Gen_Unit) then
4135 ("cannot instantiate a limited withed package", Gen_Id);
4138 ("& is not the name of a generic package", Gen_Id, Gen_Unit);
4145 if In_Extended_Main_Source_Unit (N) then
4146 Set_Is_Instantiated (Gen_Unit);
4147 Generate_Reference (Gen_Unit, N);
4149 if Present (Renamed_Object (Gen_Unit)) then
4150 Set_Is_Instantiated (Renamed_Object (Gen_Unit));
4151 Generate_Reference (Renamed_Object (Gen_Unit), N);
4155 if Nkind (Gen_Id) = N_Identifier
4156 and then Chars (Gen_Unit) = Chars (Defining_Entity (N))
4159 ("& is hidden within declaration of instance", Gen_Id, Gen_Unit);
4161 elsif Nkind (Gen_Id) = N_Expanded_Name
4162 and then Is_Child_Unit (Gen_Unit)
4163 and then Nkind (Prefix (Gen_Id)) = N_Identifier
4164 and then Chars (Act_Decl_Id) = Chars (Prefix (Gen_Id))
4167 ("& is hidden within declaration of instance ", Prefix (Gen_Id));
4170 Set_Entity (Gen_Id, Gen_Unit);
4172 -- If generic is a renaming, get original generic unit
4174 if Present (Renamed_Object (Gen_Unit))
4175 and then Ekind (Renamed_Object (Gen_Unit)) = E_Generic_Package
4177 Gen_Unit := Renamed_Object (Gen_Unit);
4180 -- Verify that there are no circular instantiations
4182 if In_Open_Scopes (Gen_Unit) then
4183 Error_Msg_NE ("instantiation of & within itself", N, Gen_Unit);
4187 elsif Contains_Instance_Of (Gen_Unit, Current_Scope, Gen_Id) then
4188 Error_Msg_Node_2 := Current_Scope;
4190 ("circular Instantiation: & instantiated in &!", N, Gen_Unit);
4191 Circularity_Detected := True;
4196 Set_Ekind (Inst_Id, E_Package);
4197 Set_Scope (Inst_Id, Current_Scope);
4199 -- If the context of the instance is subject to SPARK_Mode "off" or
4200 -- the annotation is altogether missing, set the global flag which
4201 -- signals Analyze_Pragma to ignore all SPARK_Mode pragmas within
4204 if SPARK_Mode /= On then
4205 Ignore_SPARK_Mode_Pragmas_In_Instance := True;
4207 -- Mark the instance spec in case the body is instantiated at a
4208 -- later pass. This preserves the original context in effect for
4211 Set_Ignore_SPARK_Mode_Pragmas (Act_Decl_Id);
4214 Gen_Decl := Unit_Declaration_Node (Gen_Unit);
4215 Gen_Spec := Specification (Gen_Decl);
4217 -- Initialize renamings map, for error checking, and the list that
4218 -- holds private entities whose views have changed between generic
4219 -- definition and instantiation. If this is the instance created to
4220 -- validate an actual package, the instantiation environment is that
4221 -- of the enclosing instance.
4223 Create_Instantiation_Source (N, Gen_Unit, S_Adjustment);
4225 -- Copy original generic tree, to produce text for instantiation
4229 (Original_Node (Gen_Decl), Empty, Instantiating => True);
4231 Act_Spec := Specification (Act_Tree);
4233 -- If this is the instance created to validate an actual package,
4234 -- only the formals matter, do not examine the package spec itself.
4236 if Is_Actual_Pack then
4237 Set_Visible_Declarations (Act_Spec, New_List);
4238 Set_Private_Declarations (Act_Spec, New_List);
4242 Analyze_Associations
4244 Formals => Generic_Formal_Declarations (Act_Tree),
4245 F_Copy => Generic_Formal_Declarations (Gen_Decl));
4247 Vis_Prims_List := Check_Hidden_Primitives (Renaming_List);
4249 Set_Instance_Env (Gen_Unit, Act_Decl_Id);
4250 Set_Defining_Unit_Name (Act_Spec, Act_Decl_Name);
4251 Set_Is_Generic_Instance (Act_Decl_Id);
4252 Set_Generic_Parent (Act_Spec, Gen_Unit);
4254 -- References to the generic in its own declaration or its body are
4255 -- references to the instance. Add a renaming declaration for the
4256 -- generic unit itself. This declaration, as well as the renaming
4257 -- declarations for the generic formals, must remain private to the
4258 -- unit: the formals, because this is the language semantics, and
4259 -- the unit because its use is an artifact of the implementation.
4262 Make_Package_Renaming_Declaration (Loc,
4263 Defining_Unit_Name =>
4264 Make_Defining_Identifier (Loc, Chars (Gen_Unit)),
4265 Name => New_Occurrence_Of (Act_Decl_Id, Loc));
4267 Append (Unit_Renaming, Renaming_List);
4269 -- The renaming declarations are the first local declarations of the
4272 if Is_Non_Empty_List (Visible_Declarations (Act_Spec)) then
4274 (First (Visible_Declarations (Act_Spec)), Renaming_List);
4276 Set_Visible_Declarations (Act_Spec, Renaming_List);
4279 Act_Decl := Make_Package_Declaration (Loc, Specification => Act_Spec);
4281 -- Propagate the aspect specifications from the package declaration
4282 -- template to the instantiated version of the package declaration.
4284 if Has_Aspects (Act_Tree) then
4285 Set_Aspect_Specifications (Act_Decl,
4286 New_Copy_List_Tree (Aspect_Specifications (Act_Tree)));
4289 -- The generic may have a generated Default_Storage_Pool aspect,
4290 -- set at the point of generic declaration. If the instance has
4291 -- that aspect, it overrides the one inherited from the generic.
4293 if Has_Aspects (Gen_Spec) then
4294 if No (Aspect_Specifications (N)) then
4295 Set_Aspect_Specifications (N,
4297 (Aspect_Specifications (Gen_Spec))));
4301 Inherited_Aspects : constant List_Id :=
4303 (Aspect_Specifications (Gen_Spec));
4307 Pool_Present : Boolean := False;
4310 ASN1 := First (Aspect_Specifications (N));
4311 while Present (ASN1) loop
4312 if Chars (Identifier (ASN1)) =
4313 Name_Default_Storage_Pool
4315 Pool_Present := True;
4322 if Pool_Present then
4324 -- If generic carries a default storage pool, remove it
4325 -- in favor of the instance one.
4327 ASN2 := First (Inherited_Aspects);
4328 while Present (ASN2) loop
4329 if Chars (Identifier (ASN2)) =
4330 Name_Default_Storage_Pool
4341 (Aspect_Specifications (N), Inherited_Aspects);
4346 -- Save the instantiation node for a subsequent instantiation of the
4347 -- body if there is one and it needs to be instantiated here.
4349 -- We instantiate the body only if we are generating code, or if we
4350 -- are generating cross-reference information, or for GNATprove use.
4353 Enclosing_Body_Present : Boolean := False;
4354 -- If the generic unit is not a compilation unit, then a body may
4355 -- be present in its parent even if none is required. We create a
4356 -- tentative pending instantiation for the body, which will be
4357 -- discarded if none is actually present.
4362 if Scope (Gen_Unit) /= Standard_Standard
4363 and then not Is_Child_Unit (Gen_Unit)
4365 Scop := Scope (Gen_Unit);
4366 while Present (Scop) and then Scop /= Standard_Standard loop
4367 if Unit_Requires_Body (Scop) then
4368 Enclosing_Body_Present := True;
4371 elsif In_Open_Scopes (Scop)
4372 and then In_Package_Body (Scop)
4374 Enclosing_Body_Present := True;
4378 exit when Is_Compilation_Unit (Scop);
4379 Scop := Scope (Scop);
4383 -- If front-end inlining is enabled or there are any subprograms
4384 -- marked with Inline_Always, and this is a unit for which code
4385 -- will be generated, we instantiate the body at once.
4387 -- This is done if the instance is not the main unit, and if the
4388 -- generic is not a child unit of another generic, to avoid scope
4389 -- problems and the reinstallation of parent instances.
4392 and then (not Is_Child_Unit (Gen_Unit)
4393 or else not Is_Generic_Unit (Scope (Gen_Unit)))
4394 and then Might_Inline_Subp (Gen_Unit)
4395 and then not Is_Actual_Pack
4397 if not Back_End_Inlining
4398 and then (Front_End_Inlining or else Has_Inline_Always)
4399 and then (Is_In_Main_Unit (N)
4400 or else In_Main_Context (Current_Scope))
4401 and then Nkind (Parent (N)) /= N_Compilation_Unit
4405 -- In configurable_run_time mode we force the inlining of
4406 -- predefined subprograms marked Inline_Always, to minimize
4407 -- the use of the run-time library.
4409 elsif In_Predefined_Unit (Gen_Decl)
4410 and then Configurable_Run_Time_Mode
4411 and then Nkind (Parent (N)) /= N_Compilation_Unit
4416 -- If the current scope is itself an instance within a child
4417 -- unit, there will be duplications in the scope stack, and the
4418 -- unstacking mechanism in Inline_Instance_Body will fail.
4419 -- This loses some rare cases of optimization, and might be
4420 -- improved some day, if we can find a proper abstraction for
4421 -- "the complete compilation context" that can be saved and
4424 if Is_Generic_Instance (Current_Scope) then
4426 Curr_Unit : constant Entity_Id :=
4427 Cunit_Entity (Current_Sem_Unit);
4429 if Curr_Unit /= Current_Scope
4430 and then Is_Child_Unit (Curr_Unit)
4432 Inline_Now := False;
4439 (Unit_Requires_Body (Gen_Unit)
4440 or else Enclosing_Body_Present
4441 or else Present (Corresponding_Body (Gen_Decl)))
4442 and then Needs_Body_Instantiated (Gen_Unit)
4443 and then not Is_Actual_Pack
4444 and then not Inline_Now
4445 and then (Operating_Mode = Generate_Code
4446 or else (Operating_Mode = Check_Semantics
4447 and then GNATprove_Mode));
4449 -- If front-end inlining is enabled or there are any subprograms
4450 -- marked with Inline_Always, do not instantiate body when within
4451 -- a generic context.
4453 if not Back_End_Inlining
4454 and then (Front_End_Inlining or else Has_Inline_Always)
4455 and then not Expander_Active
4457 Needs_Body := False;
4460 -- If the current context is generic, and the package being
4461 -- instantiated is declared within a formal package, there is no
4462 -- body to instantiate until the enclosing generic is instantiated
4463 -- and there is an actual for the formal package. If the formal
4464 -- package has parameters, we build a regular package instance for
4465 -- it, that precedes the original formal package declaration.
4467 if In_Open_Scopes (Scope (Scope (Gen_Unit))) then
4469 Decl : constant Node_Id :=
4471 (Unit_Declaration_Node (Scope (Gen_Unit)));
4473 if Nkind (Decl) = N_Formal_Package_Declaration
4474 or else (Nkind (Decl) = N_Package_Declaration
4475 and then Is_List_Member (Decl)
4476 and then Present (Next (Decl))
4478 Nkind (Next (Decl)) =
4479 N_Formal_Package_Declaration)
4481 Needs_Body := False;
4487 -- For RCI unit calling stubs, we omit the instance body if the
4488 -- instance is the RCI library unit itself.
4490 -- However there is a special case for nested instances: in this case
4491 -- we do generate the instance body, as it might be required, e.g.
4492 -- because it provides stream attributes for some type used in the
4493 -- profile of a remote subprogram. This is consistent with 12.3(12),
4494 -- which indicates that the instance body occurs at the place of the
4495 -- instantiation, and thus is part of the RCI declaration, which is
4496 -- present on all client partitions (this is E.2.3(18)).
4498 -- Note that AI12-0002 may make it illegal at some point to have
4499 -- stream attributes defined in an RCI unit, in which case this
4500 -- special case will become unnecessary. In the meantime, there
4501 -- is known application code in production that depends on this
4502 -- being possible, so we definitely cannot eliminate the body in
4503 -- the case of nested instances for the time being.
4505 -- When we generate a nested instance body, calling stubs for any
4506 -- relevant subprogram will be inserted immediately after the
4507 -- subprogram declarations, and will take precedence over the
4508 -- subsequent (original) body. (The stub and original body will be
4509 -- complete homographs, but this is permitted in an instance).
4510 -- (Could we do better and remove the original body???)
4512 if Distribution_Stub_Mode = Generate_Caller_Stub_Body
4513 and then Comes_From_Source (N)
4514 and then Nkind (Parent (N)) = N_Compilation_Unit
4516 Needs_Body := False;
4520 -- Indicate that the enclosing scopes contain an instantiation,
4521 -- and that cleanup actions should be delayed until after the
4522 -- instance body is expanded.
4524 Check_Forward_Instantiation (Gen_Decl);
4525 if Nkind (N) = N_Package_Instantiation then
4527 Enclosing_Master : Entity_Id;
4530 -- Loop to search enclosing masters
4532 Enclosing_Master := Current_Scope;
4533 Scope_Loop : while Enclosing_Master /= Standard_Standard loop
4534 if Ekind (Enclosing_Master) = E_Package then
4535 if Is_Compilation_Unit (Enclosing_Master) then
4536 if In_Package_Body (Enclosing_Master) then
4537 Set_Delay_Subprogram_Descriptors
4538 (Body_Entity (Enclosing_Master));
4540 Set_Delay_Subprogram_Descriptors
4547 Enclosing_Master := Scope (Enclosing_Master);
4550 elsif Is_Generic_Unit (Enclosing_Master)
4551 or else Ekind (Enclosing_Master) = E_Void
4553 -- Cleanup actions will eventually be performed on the
4554 -- enclosing subprogram or package instance, if any.
4555 -- Enclosing scope is void in the formal part of a
4556 -- generic subprogram.
4561 if Ekind (Enclosing_Master) = E_Entry
4563 Ekind (Scope (Enclosing_Master)) = E_Protected_Type
4565 if not Expander_Active then
4569 Protected_Body_Subprogram (Enclosing_Master);
4573 Set_Delay_Cleanups (Enclosing_Master);
4575 while Ekind (Enclosing_Master) = E_Block loop
4576 Enclosing_Master := Scope (Enclosing_Master);
4579 if Is_Subprogram (Enclosing_Master) then
4580 Set_Delay_Subprogram_Descriptors (Enclosing_Master);
4582 elsif Is_Task_Type (Enclosing_Master) then
4584 TBP : constant Node_Id :=
4585 Get_Task_Body_Procedure
4588 if Present (TBP) then
4589 Set_Delay_Subprogram_Descriptors (TBP);
4590 Set_Delay_Cleanups (TBP);
4597 end loop Scope_Loop;
4600 -- Make entry in table
4602 Add_Pending_Instantiation (N, Act_Decl);
4606 Set_Categorization_From_Pragmas (Act_Decl);
4608 if Parent_Installed then
4612 Set_Instance_Spec (N, Act_Decl);
4614 -- If not a compilation unit, insert the package declaration before
4615 -- the original instantiation node.
4617 if Nkind (Parent (N)) /= N_Compilation_Unit then
4618 Mark_Rewrite_Insertion (Act_Decl);
4619 Insert_Before (N, Act_Decl);
4621 if Has_Aspects (N) then
4622 Analyze_Aspect_Specifications (N, Act_Decl_Id);
4624 -- The pragma created for a Default_Storage_Pool aspect must
4625 -- appear ahead of the declarations in the instance spec.
4626 -- Analysis has placed it after the instance node, so remove
4627 -- it and reinsert it properly now.
4630 ASN : constant Node_Id := First (Aspect_Specifications (N));
4631 A_Name : constant Name_Id := Chars (Identifier (ASN));
4635 if A_Name = Name_Default_Storage_Pool then
4636 if No (Visible_Declarations (Act_Spec)) then
4637 Set_Visible_Declarations (Act_Spec, New_List);
4641 while Present (Decl) loop
4642 if Nkind (Decl) = N_Pragma then
4644 Prepend (Decl, Visible_Declarations (Act_Spec));
4656 -- For an instantiation that is a compilation unit, place
4657 -- declaration on current node so context is complete for analysis
4658 -- (including nested instantiations). If this is the main unit,
4659 -- the declaration eventually replaces the instantiation node.
4660 -- If the instance body is created later, it replaces the
4661 -- instance node, and the declaration is attached to it
4662 -- (see Build_Instance_Compilation_Unit_Nodes).
4665 if Cunit_Entity (Current_Sem_Unit) = Defining_Entity (N) then
4667 -- The entity for the current unit is the newly created one,
4668 -- and all semantic information is attached to it.
4670 Set_Cunit_Entity (Current_Sem_Unit, Act_Decl_Id);
4672 -- If this is the main unit, replace the main entity as well
4674 if Current_Sem_Unit = Main_Unit then
4675 Main_Unit_Entity := Act_Decl_Id;
4679 Set_Unit (Parent (N), Act_Decl);
4680 Set_Parent_Spec (Act_Decl, Parent_Spec (N));
4681 Set_Package_Instantiation (Act_Decl_Id, N);
4683 -- Process aspect specifications of the instance node, if any, to
4684 -- take into account categorization pragmas before analyzing the
4687 if Has_Aspects (N) then
4688 Analyze_Aspect_Specifications (N, Act_Decl_Id);
4692 Set_Unit (Parent (N), N);
4693 Set_Body_Required (Parent (N), False);
4695 -- We never need elaboration checks on instantiations, since by
4696 -- definition, the body instantiation is elaborated at the same
4697 -- time as the spec instantiation.
4699 if Legacy_Elaboration_Checks then
4700 Set_Kill_Elaboration_Checks (Act_Decl_Id);
4701 Set_Suppress_Elaboration_Warnings (Act_Decl_Id);
4705 if Legacy_Elaboration_Checks then
4706 Check_Elab_Instantiation (N);
4709 -- Save the scenario for later examination by the ABE Processing
4712 Record_Elaboration_Scenario (N);
4714 -- The instantiation results in a guaranteed ABE
4716 if Is_Known_Guaranteed_ABE (N) and then Needs_Body then
4717 -- Do not instantiate the corresponding body because gigi cannot
4718 -- handle certain types of premature instantiations.
4720 Remove_Dead_Instance (N);
4722 -- Create completing bodies for all subprogram declarations since
4723 -- their real bodies will not be instantiated.
4725 Provide_Completing_Bodies (Instance_Spec (N));
4728 Check_Hidden_Child_Unit (N, Gen_Unit, Act_Decl_Id);
4730 Set_First_Private_Entity (Defining_Unit_Name (Unit_Renaming),
4731 First_Private_Entity (Act_Decl_Id));
4733 -- If the instantiation will receive a body, the unit will be
4734 -- transformed into a package body, and receive its own elaboration
4735 -- entity. Otherwise, the nature of the unit is now a package
4738 if Nkind (Parent (N)) = N_Compilation_Unit
4739 and then not Needs_Body
4741 Rewrite (N, Act_Decl);
4744 if Present (Corresponding_Body (Gen_Decl))
4745 or else Unit_Requires_Body (Gen_Unit)
4747 Set_Has_Completion (Act_Decl_Id);
4750 Check_Formal_Packages (Act_Decl_Id);
4752 Restore_Hidden_Primitives (Vis_Prims_List);
4753 Restore_Private_Views (Act_Decl_Id);
4755 Inherit_Context (Gen_Decl, N);
4757 if Parent_Installed then
4762 Env_Installed := False;
4765 Validate_Categorization_Dependency (N, Act_Decl_Id);
4767 -- There used to be a check here to prevent instantiations in local
4768 -- contexts if the No_Local_Allocators restriction was active. This
4769 -- check was removed by a binding interpretation in AI-95-00130/07,
4770 -- but we retain the code for documentation purposes.
4772 -- if Ekind (Act_Decl_Id) /= E_Void
4773 -- and then not Is_Library_Level_Entity (Act_Decl_Id)
4775 -- Check_Restriction (No_Local_Allocators, N);
4779 Inline_Instance_Body (N, Gen_Unit, Act_Decl);
4782 -- Check that if N is an instantiation of System.Dim_Float_IO or
4783 -- System.Dim_Integer_IO, the formal type has a dimension system.
4785 if Nkind (N) = N_Package_Instantiation
4786 and then Is_Dim_IO_Package_Instantiation (N)
4789 Assoc : constant Node_Id := First (Generic_Associations (N));
4791 if not Has_Dimension_System
4792 (Etype (Explicit_Generic_Actual_Parameter (Assoc)))
4794 Error_Msg_N ("type with a dimension system expected", Assoc);
4800 if Has_Aspects (N) and then Nkind (Parent (N)) /= N_Compilation_Unit then
4801 Analyze_Aspect_Specifications (N, Act_Decl_Id);
4804 Ignore_SPARK_Mode_Pragmas_In_Instance := Saved_ISMP;
4805 Restore_Ghost_Region (Saved_GM, Saved_IGR);
4806 Restore_SPARK_Mode (Saved_SM, Saved_SMP);
4807 Style_Check := Saved_Style_Check;
4810 when Instantiation_Error =>
4811 if Parent_Installed then
4815 if Env_Installed then
4819 Ignore_SPARK_Mode_Pragmas_In_Instance := Saved_ISMP;
4820 Restore_Ghost_Region (Saved_GM, Saved_IGR);
4821 Restore_SPARK_Mode (Saved_SM, Saved_SMP);
4822 Style_Check := Saved_Style_Check;
4823 end Analyze_Package_Instantiation;
4825 --------------------------
4826 -- Inline_Instance_Body --
4827 --------------------------
4829 -- WARNING: This routine manages SPARK regions. Return statements must be
4830 -- replaced by gotos which jump to the end of the routine and restore the
4833 procedure Inline_Instance_Body
4835 Gen_Unit : Entity_Id;
4838 Config_Attrs : constant Config_Switches_Type := Save_Config_Switches;
4840 Curr_Comp : constant Node_Id := Cunit (Current_Sem_Unit);
4841 Curr_Unit : constant Entity_Id := Cunit_Entity (Current_Sem_Unit);
4842 Gen_Comp : constant Entity_Id :=
4843 Cunit_Entity (Get_Source_Unit (Gen_Unit));
4845 Scope_Stack_Depth : constant Pos :=
4846 Scope_Stack.Last - Scope_Stack.First + 1;
4848 Inner_Scopes : array (1 .. Scope_Stack_Depth) of Entity_Id;
4849 Instances : array (1 .. Scope_Stack_Depth) of Entity_Id;
4850 Use_Clauses : array (1 .. Scope_Stack_Depth) of Node_Id;
4852 Curr_Scope : Entity_Id := Empty;
4853 List : Elist_Id := No_Elist; -- init to avoid warning
4854 N_Instances : Nat := 0;
4855 Num_Inner : Nat := 0;
4856 Num_Scopes : Nat := 0;
4857 Removed : Boolean := False;
4862 -- Case of generic unit defined in another unit. We must remove the
4863 -- complete context of the current unit to install that of the generic.
4865 if Gen_Comp /= Cunit_Entity (Current_Sem_Unit) then
4867 -- Add some comments for the following two loops ???
4870 while Present (S) and then S /= Standard_Standard loop
4872 Num_Scopes := Num_Scopes + 1;
4874 Use_Clauses (Num_Scopes) :=
4876 (Scope_Stack.Last - Num_Scopes + 1).
4878 End_Use_Clauses (Use_Clauses (Num_Scopes));
4880 exit when Scope_Stack.Last - Num_Scopes + 1 = Scope_Stack.First
4881 or else Scope_Stack.Table
4882 (Scope_Stack.Last - Num_Scopes).Entity = Scope (S);
4885 exit when Is_Generic_Instance (S)
4886 and then (In_Package_Body (S)
4887 or else Ekind (S) = E_Procedure
4888 or else Ekind (S) = E_Function);
4892 Vis := Is_Immediately_Visible (Gen_Comp);
4894 -- Find and save all enclosing instances
4899 and then S /= Standard_Standard
4901 if Is_Generic_Instance (S) then
4902 N_Instances := N_Instances + 1;
4903 Instances (N_Instances) := S;
4905 exit when In_Package_Body (S);
4911 -- Remove context of current compilation unit, unless we are within a
4912 -- nested package instantiation, in which case the context has been
4913 -- removed previously.
4915 -- If current scope is the body of a child unit, remove context of
4916 -- spec as well. If an enclosing scope is an instance body, the
4917 -- context has already been removed, but the entities in the body
4918 -- must be made invisible as well.
4921 while Present (S) and then S /= Standard_Standard loop
4922 if Is_Generic_Instance (S)
4923 and then (In_Package_Body (S)
4924 or else Ekind_In (S, E_Procedure, E_Function))
4926 -- We still have to remove the entities of the enclosing
4927 -- instance from direct visibility.
4932 E := First_Entity (S);
4933 while Present (E) loop
4934 Set_Is_Immediately_Visible (E, False);
4943 or else (Ekind (Curr_Unit) = E_Package_Body
4944 and then S = Spec_Entity (Curr_Unit))
4945 or else (Ekind (Curr_Unit) = E_Subprogram_Body
4946 and then S = Corresponding_Spec
4947 (Unit_Declaration_Node (Curr_Unit)))
4951 -- Remove entities in current scopes from visibility, so that
4952 -- instance body is compiled in a clean environment.
4954 List := Save_Scope_Stack (Handle_Use => False);
4956 if Is_Child_Unit (S) then
4958 -- Remove child unit from stack, as well as inner scopes.
4959 -- Removing the context of a child unit removes parent units
4962 while Current_Scope /= S loop
4963 Num_Inner := Num_Inner + 1;
4964 Inner_Scopes (Num_Inner) := Current_Scope;
4969 Remove_Context (Curr_Comp);
4973 Remove_Context (Curr_Comp);
4976 if Ekind (Curr_Unit) = E_Package_Body then
4977 Remove_Context (Library_Unit (Curr_Comp));
4984 pragma Assert (Num_Inner < Num_Scopes);
4986 Push_Scope (Standard_Standard);
4987 Scope_Stack.Table (Scope_Stack.Last).Is_Active_Stack_Base := True;
4989 -- The inlined package body is analyzed with the configuration state
4990 -- of the context prior to the scope manipulations performed above.
4992 -- ??? shouldn't this also use the warning state of the context prior
4993 -- to the scope manipulations?
4995 Instantiate_Package_Body
4997 ((Act_Decl => Act_Decl,
4998 Config_Switches => Config_Attrs,
4999 Current_Sem_Unit => Current_Sem_Unit,
5000 Expander_Status => Expander_Active,
5002 Local_Suppress_Stack_Top => Local_Suppress_Stack_Top,
5003 Scope_Suppress => Scope_Suppress,
5004 Warnings => Save_Warnings)),
5005 Inlined_Body => True);
5011 Set_Is_Immediately_Visible (Gen_Comp, Vis);
5013 -- Reset Generic_Instance flag so that use clauses can be installed
5014 -- in the proper order. (See Use_One_Package for effect of enclosing
5015 -- instances on processing of use clauses).
5017 for J in 1 .. N_Instances loop
5018 Set_Is_Generic_Instance (Instances (J), False);
5022 Install_Context (Curr_Comp, Chain => False);
5024 if Present (Curr_Scope)
5025 and then Is_Child_Unit (Curr_Scope)
5027 Push_Scope (Curr_Scope);
5028 Set_Is_Immediately_Visible (Curr_Scope);
5030 -- Finally, restore inner scopes as well
5032 for J in reverse 1 .. Num_Inner loop
5033 Push_Scope (Inner_Scopes (J));
5037 Restore_Scope_Stack (List, Handle_Use => False);
5039 if Present (Curr_Scope)
5041 (In_Private_Part (Curr_Scope)
5042 or else In_Package_Body (Curr_Scope))
5044 -- Install private declaration of ancestor units, which are
5045 -- currently available. Restore_Scope_Stack and Install_Context
5046 -- only install the visible part of parents.
5051 Par := Scope (Curr_Scope);
5052 while (Present (Par)) and then Par /= Standard_Standard loop
5053 Install_Private_Declarations (Par);
5060 -- Restore use clauses. For a child unit, use clauses in the parents
5061 -- are restored when installing the context, so only those in inner
5062 -- scopes (and those local to the child unit itself) need to be
5063 -- installed explicitly.
5065 if Is_Child_Unit (Curr_Unit) and then Removed then
5066 for J in reverse 1 .. Num_Inner + 1 loop
5067 Scope_Stack.Table (Scope_Stack.Last - J + 1).First_Use_Clause :=
5069 Install_Use_Clauses (Use_Clauses (J));
5073 for J in reverse 1 .. Num_Scopes loop
5074 Scope_Stack.Table (Scope_Stack.Last - J + 1).First_Use_Clause :=
5076 Install_Use_Clauses (Use_Clauses (J));
5080 -- Restore status of instances. If one of them is a body, make its
5081 -- local entities visible again.
5088 for J in 1 .. N_Instances loop
5089 Inst := Instances (J);
5090 Set_Is_Generic_Instance (Inst, True);
5092 if In_Package_Body (Inst)
5093 or else Ekind_In (S, E_Procedure, E_Function)
5095 E := First_Entity (Instances (J));
5096 while Present (E) loop
5097 Set_Is_Immediately_Visible (E);
5104 -- If generic unit is in current unit, current context is correct. Note
5105 -- that the context is guaranteed to carry the correct SPARK_Mode as no
5106 -- enclosing scopes were removed.
5109 Instantiate_Package_Body
5111 ((Act_Decl => Act_Decl,
5112 Config_Switches => Save_Config_Switches,
5113 Current_Sem_Unit => Current_Sem_Unit,
5114 Expander_Status => Expander_Active,
5116 Local_Suppress_Stack_Top => Local_Suppress_Stack_Top,
5117 Scope_Suppress => Scope_Suppress,
5118 Warnings => Save_Warnings)),
5119 Inlined_Body => True);
5121 end Inline_Instance_Body;
5123 -------------------------------------
5124 -- Analyze_Procedure_Instantiation --
5125 -------------------------------------
5127 procedure Analyze_Procedure_Instantiation (N : Node_Id) is
5129 Analyze_Subprogram_Instantiation (N, E_Procedure);
5130 end Analyze_Procedure_Instantiation;
5132 -----------------------------------
5133 -- Need_Subprogram_Instance_Body --
5134 -----------------------------------
5136 function Need_Subprogram_Instance_Body
5138 Subp : Entity_Id) return Boolean
5140 function Is_Inlined_Or_Child_Of_Inlined (E : Entity_Id) return Boolean;
5141 -- Return True if E is an inlined subprogram, an inlined renaming or a
5142 -- subprogram nested in an inlined subprogram. The inlining machinery
5143 -- totally disregards nested subprograms since it considers that they
5144 -- will always be compiled if the parent is (see Inline.Is_Nested).
5146 ------------------------------------
5147 -- Is_Inlined_Or_Child_Of_Inlined --
5148 ------------------------------------
5150 function Is_Inlined_Or_Child_Of_Inlined (E : Entity_Id) return Boolean is
5154 if Is_Inlined (E) or else Is_Inlined (Alias (E)) then
5159 while Scop /= Standard_Standard loop
5160 if Ekind (Scop) in Subprogram_Kind and then Is_Inlined (Scop) then
5164 Scop := Scope (Scop);
5168 end Is_Inlined_Or_Child_Of_Inlined;
5171 -- Must be in the main unit or inlined (or child of inlined)
5173 if (Is_In_Main_Unit (N) or else Is_Inlined_Or_Child_Of_Inlined (Subp))
5175 -- Must be generating code or analyzing code in GNATprove mode
5177 and then (Operating_Mode = Generate_Code
5178 or else (Operating_Mode = Check_Semantics
5179 and then GNATprove_Mode))
5181 -- The body is needed when generating code (full expansion) and in
5182 -- in GNATprove mode (special expansion) for formal verification of
5185 and then (Expander_Active or GNATprove_Mode)
5187 -- No point in inlining if ABE is inevitable
5189 and then not Is_Known_Guaranteed_ABE (N)
5191 -- Or if subprogram is eliminated
5193 and then not Is_Eliminated (Subp)
5195 Add_Pending_Instantiation (N, Unit_Declaration_Node (Subp));
5198 -- Here if not inlined, or we ignore the inlining
5203 end Need_Subprogram_Instance_Body;
5205 --------------------------------------
5206 -- Analyze_Subprogram_Instantiation --
5207 --------------------------------------
5209 -- WARNING: This routine manages Ghost and SPARK regions. Return statements
5210 -- must be replaced by gotos which jump to the end of the routine in order
5211 -- to restore the Ghost and SPARK modes.
5213 procedure Analyze_Subprogram_Instantiation
5217 Errs : constant Nat := Serious_Errors_Detected;
5218 Gen_Id : constant Node_Id := Name (N);
5219 Inst_Id : constant Entity_Id := Defining_Entity (N);
5220 Anon_Id : constant Entity_Id :=
5221 Make_Defining_Identifier (Sloc (Inst_Id),
5222 Chars => New_External_Name (Chars (Inst_Id), 'R'));
5223 Loc : constant Source_Ptr := Sloc (N);
5225 Act_Decl_Id : Entity_Id := Empty; -- init to avoid warning
5230 Env_Installed : Boolean := False;
5231 Gen_Unit : Entity_Id;
5233 Pack_Id : Entity_Id;
5234 Parent_Installed : Boolean := False;
5236 Renaming_List : List_Id;
5237 -- The list of declarations that link formals and actuals of the
5238 -- instance. These are subtype declarations for formal types, and
5239 -- renaming declarations for other formals. The subprogram declaration
5240 -- for the instance is then appended to the list, and the last item on
5241 -- the list is the renaming declaration for the instance.
5243 procedure Analyze_Instance_And_Renamings;
5244 -- The instance must be analyzed in a context that includes the mappings
5245 -- of generic parameters into actuals. We create a package declaration
5246 -- for this purpose, and a subprogram with an internal name within the
5247 -- package. The subprogram instance is simply an alias for the internal
5248 -- subprogram, declared in the current scope.
5250 procedure Build_Subprogram_Renaming;
5251 -- If the subprogram is recursive, there are occurrences of the name of
5252 -- the generic within the body, which must resolve to the current
5253 -- instance. We add a renaming declaration after the declaration, which
5254 -- is available in the instance body, as well as in the analysis of
5255 -- aspects that appear in the generic. This renaming declaration is
5256 -- inserted after the instance declaration which it renames.
5258 ------------------------------------
5259 -- Analyze_Instance_And_Renamings --
5260 ------------------------------------
5262 procedure Analyze_Instance_And_Renamings is
5263 Def_Ent : constant Entity_Id := Defining_Entity (N);
5264 Pack_Decl : Node_Id;
5267 if Nkind (Parent (N)) = N_Compilation_Unit then
5269 -- For the case of a compilation unit, the container package has
5270 -- the same name as the instantiation, to insure that the binder
5271 -- calls the elaboration procedure with the right name. Copy the
5272 -- entity of the instance, which may have compilation level flags
5273 -- (e.g. Is_Child_Unit) set.
5275 Pack_Id := New_Copy (Def_Ent);
5278 -- Otherwise we use the name of the instantiation concatenated
5279 -- with its source position to ensure uniqueness if there are
5280 -- several instantiations with the same name.
5283 Make_Defining_Identifier (Loc,
5284 Chars => New_External_Name
5285 (Related_Id => Chars (Def_Ent),
5287 Suffix_Index => Source_Offset (Sloc (Def_Ent))));
5291 Make_Package_Declaration (Loc,
5292 Specification => Make_Package_Specification (Loc,
5293 Defining_Unit_Name => Pack_Id,
5294 Visible_Declarations => Renaming_List,
5295 End_Label => Empty));
5297 Set_Instance_Spec (N, Pack_Decl);
5298 Set_Is_Generic_Instance (Pack_Id);
5299 Set_Debug_Info_Needed (Pack_Id);
5301 -- Case of not a compilation unit
5303 if Nkind (Parent (N)) /= N_Compilation_Unit then
5304 Mark_Rewrite_Insertion (Pack_Decl);
5305 Insert_Before (N, Pack_Decl);
5306 Set_Has_Completion (Pack_Id);
5308 -- Case of an instantiation that is a compilation unit
5310 -- Place declaration on current node so context is complete for
5311 -- analysis (including nested instantiations), and for use in a
5312 -- context_clause (see Analyze_With_Clause).
5315 Set_Unit (Parent (N), Pack_Decl);
5316 Set_Parent_Spec (Pack_Decl, Parent_Spec (N));
5319 Analyze (Pack_Decl);
5320 Check_Formal_Packages (Pack_Id);
5322 -- Body of the enclosing package is supplied when instantiating the
5323 -- subprogram body, after semantic analysis is completed.
5325 if Nkind (Parent (N)) = N_Compilation_Unit then
5327 -- Remove package itself from visibility, so it does not
5328 -- conflict with subprogram.
5330 Set_Name_Entity_Id (Chars (Pack_Id), Homonym (Pack_Id));
5332 -- Set name and scope of internal subprogram so that the proper
5333 -- external name will be generated. The proper scope is the scope
5334 -- of the wrapper package. We need to generate debugging info for
5335 -- the internal subprogram, so set flag accordingly.
5337 Set_Chars (Anon_Id, Chars (Defining_Entity (N)));
5338 Set_Scope (Anon_Id, Scope (Pack_Id));
5340 -- Mark wrapper package as referenced, to avoid spurious warnings
5341 -- if the instantiation appears in various with_ clauses of
5342 -- subunits of the main unit.
5344 Set_Referenced (Pack_Id);
5347 Set_Is_Generic_Instance (Anon_Id);
5348 Set_Debug_Info_Needed (Anon_Id);
5349 Act_Decl_Id := New_Copy (Anon_Id);
5351 Set_Parent (Act_Decl_Id, Parent (Anon_Id));
5352 Set_Chars (Act_Decl_Id, Chars (Defining_Entity (N)));
5353 Set_Sloc (Act_Decl_Id, Sloc (Defining_Entity (N)));
5355 -- Subprogram instance comes from source only if generic does
5357 Set_Comes_From_Source (Act_Decl_Id, Comes_From_Source (Gen_Unit));
5359 -- If the instance is a child unit, mark the Id accordingly. Mark
5360 -- the anonymous entity as well, which is the real subprogram and
5361 -- which is used when the instance appears in a context clause.
5362 -- Similarly, propagate the Is_Eliminated flag to handle properly
5363 -- nested eliminated subprograms.
5365 Set_Is_Child_Unit (Act_Decl_Id, Is_Child_Unit (Defining_Entity (N)));
5366 Set_Is_Child_Unit (Anon_Id, Is_Child_Unit (Defining_Entity (N)));
5367 New_Overloaded_Entity (Act_Decl_Id);
5368 Check_Eliminated (Act_Decl_Id);
5369 Set_Is_Eliminated (Anon_Id, Is_Eliminated (Act_Decl_Id));
5371 if Nkind (Parent (N)) = N_Compilation_Unit then
5373 -- In compilation unit case, kill elaboration checks on the
5374 -- instantiation, since they are never needed - the body is
5375 -- instantiated at the same point as the spec.
5377 if Legacy_Elaboration_Checks then
5378 Set_Kill_Elaboration_Checks (Act_Decl_Id);
5379 Set_Suppress_Elaboration_Warnings (Act_Decl_Id);
5382 Set_Is_Compilation_Unit (Anon_Id);
5383 Set_Cunit_Entity (Current_Sem_Unit, Pack_Id);
5386 -- The instance is not a freezing point for the new subprogram.
5387 -- The anonymous subprogram may have a freeze node, created for
5388 -- some delayed aspects. This freeze node must not be inherited
5389 -- by the visible subprogram entity.
5391 Set_Is_Frozen (Act_Decl_Id, False);
5392 Set_Freeze_Node (Act_Decl_Id, Empty);
5394 if Nkind (Defining_Entity (N)) = N_Defining_Operator_Symbol then
5395 Valid_Operator_Definition (Act_Decl_Id);
5398 Set_Alias (Act_Decl_Id, Anon_Id);
5399 Set_Has_Completion (Act_Decl_Id);
5400 Set_Related_Instance (Pack_Id, Act_Decl_Id);
5402 if Nkind (Parent (N)) = N_Compilation_Unit then
5403 Set_Body_Required (Parent (N), False);
5405 end Analyze_Instance_And_Renamings;
5407 -------------------------------
5408 -- Build_Subprogram_Renaming --
5409 -------------------------------
5411 procedure Build_Subprogram_Renaming is
5412 Renaming_Decl : Node_Id;
5413 Unit_Renaming : Node_Id;
5417 Make_Subprogram_Renaming_Declaration (Loc,
5420 (Specification (Original_Node (Gen_Decl)),
5422 Instantiating => True),
5423 Name => New_Occurrence_Of (Anon_Id, Loc));
5425 -- The generic may be a child unit. The renaming needs an identifier
5426 -- with the proper name.
5428 Set_Defining_Unit_Name (Specification (Unit_Renaming),
5429 Make_Defining_Identifier (Loc, Chars (Gen_Unit)));
5431 -- If there is a formal subprogram with the same name as the unit
5432 -- itself, do not add this renaming declaration, to prevent
5433 -- ambiguities when there is a call with that name in the body.
5434 -- This is a partial and ugly fix for one ACATS test. ???
5436 Renaming_Decl := First (Renaming_List);
5437 while Present (Renaming_Decl) loop
5438 if Nkind (Renaming_Decl) = N_Subprogram_Renaming_Declaration
5440 Chars (Defining_Entity (Renaming_Decl)) = Chars (Gen_Unit)
5445 Next (Renaming_Decl);
5448 if No (Renaming_Decl) then
5449 Append (Unit_Renaming, Renaming_List);
5451 end Build_Subprogram_Renaming;
5455 Saved_GM : constant Ghost_Mode_Type := Ghost_Mode;
5456 Saved_IGR : constant Node_Id := Ignored_Ghost_Region;
5457 Saved_ISMP : constant Boolean :=
5458 Ignore_SPARK_Mode_Pragmas_In_Instance;
5459 Saved_SM : constant SPARK_Mode_Type := SPARK_Mode;
5460 Saved_SMP : constant Node_Id := SPARK_Mode_Pragma;
5461 -- Save the Ghost and SPARK mode-related data to restore on exit
5463 Vis_Prims_List : Elist_Id := No_Elist;
5464 -- List of primitives made temporarily visible in the instantiation
5465 -- to match the visibility of the formal type
5467 -- Start of processing for Analyze_Subprogram_Instantiation
5470 -- Preserve relevant elaboration-related attributes of the context which
5471 -- are no longer available or very expensive to recompute once analysis,
5472 -- resolution, and expansion are over.
5474 Mark_Elaboration_Attributes
5481 Check_SPARK_05_Restriction ("generic is not allowed", N);
5483 -- Very first thing: check for special Text_IO unit in case we are
5484 -- instantiating one of the children of [[Wide_]Wide_]Text_IO. Of course
5485 -- such an instantiation is bogus (these are packages, not subprograms),
5486 -- but we get a better error message if we do this.
5488 Check_Text_IO_Special_Unit (Gen_Id);
5490 -- Make node global for error reporting
5492 Instantiation_Node := N;
5494 -- For package instantiations we turn off style checks, because they
5495 -- will have been emitted in the generic. For subprogram instantiations
5496 -- we want to apply at least the check on overriding indicators so we
5497 -- do not modify the style check status.
5499 -- The renaming declarations for the actuals do not come from source and
5500 -- will not generate spurious warnings.
5502 Preanalyze_Actuals (N);
5505 Env_Installed := True;
5506 Check_Generic_Child_Unit (Gen_Id, Parent_Installed);
5507 Gen_Unit := Entity (Gen_Id);
5509 -- A subprogram instantiation is Ghost when it is subject to pragma
5510 -- Ghost or the generic template is Ghost. Set the mode now to ensure
5511 -- that any nodes generated during analysis and expansion are marked as
5514 Mark_And_Set_Ghost_Instantiation (N, Gen_Unit);
5516 Generate_Reference (Gen_Unit, Gen_Id);
5518 if Nkind (Gen_Id) = N_Identifier
5519 and then Chars (Gen_Unit) = Chars (Defining_Entity (N))
5522 ("& is hidden within declaration of instance", Gen_Id, Gen_Unit);
5525 if Etype (Gen_Unit) = Any_Type then
5530 -- Verify that it is a generic subprogram of the right kind, and that
5531 -- it does not lead to a circular instantiation.
5533 if K = E_Procedure and then Ekind (Gen_Unit) /= E_Generic_Procedure then
5535 ("& is not the name of a generic procedure", Gen_Id, Gen_Unit);
5537 elsif K = E_Function and then Ekind (Gen_Unit) /= E_Generic_Function then
5539 ("& is not the name of a generic function", Gen_Id, Gen_Unit);
5541 elsif In_Open_Scopes (Gen_Unit) then
5542 Error_Msg_NE ("instantiation of & within itself", N, Gen_Unit);
5545 Set_Ekind (Inst_Id, K);
5546 Set_Scope (Inst_Id, Current_Scope);
5548 Set_Entity (Gen_Id, Gen_Unit);
5549 Set_Is_Instantiated (Gen_Unit);
5551 if In_Extended_Main_Source_Unit (N) then
5552 Generate_Reference (Gen_Unit, N);
5555 -- If renaming, get original unit
5557 if Present (Renamed_Object (Gen_Unit))
5558 and then Ekind_In (Renamed_Object (Gen_Unit), E_Generic_Procedure,
5561 Gen_Unit := Renamed_Object (Gen_Unit);
5562 Set_Is_Instantiated (Gen_Unit);
5563 Generate_Reference (Gen_Unit, N);
5566 if Contains_Instance_Of (Gen_Unit, Current_Scope, Gen_Id) then
5567 Error_Msg_Node_2 := Current_Scope;
5569 ("circular Instantiation: & instantiated in &!", N, Gen_Unit);
5570 Circularity_Detected := True;
5571 Restore_Hidden_Primitives (Vis_Prims_List);
5575 Gen_Decl := Unit_Declaration_Node (Gen_Unit);
5577 -- Initialize renamings map, for error checking
5579 Generic_Renamings.Set_Last (0);
5580 Generic_Renamings_HTable.Reset;
5582 Create_Instantiation_Source (N, Gen_Unit, S_Adjustment);
5584 -- Copy original generic tree, to produce text for instantiation
5588 (Original_Node (Gen_Decl), Empty, Instantiating => True);
5590 -- Inherit overriding indicator from instance node
5592 Act_Spec := Specification (Act_Tree);
5593 Set_Must_Override (Act_Spec, Must_Override (N));
5594 Set_Must_Not_Override (Act_Spec, Must_Not_Override (N));
5597 Analyze_Associations
5599 Formals => Generic_Formal_Declarations (Act_Tree),
5600 F_Copy => Generic_Formal_Declarations (Gen_Decl));
5602 Vis_Prims_List := Check_Hidden_Primitives (Renaming_List);
5604 -- The subprogram itself cannot contain a nested instance, so the
5605 -- current parent is left empty.
5607 Set_Instance_Env (Gen_Unit, Empty);
5609 -- Build the subprogram declaration, which does not appear in the
5610 -- generic template, and give it a sloc consistent with that of the
5613 Set_Defining_Unit_Name (Act_Spec, Anon_Id);
5614 Set_Generic_Parent (Act_Spec, Gen_Unit);
5616 Make_Subprogram_Declaration (Sloc (Act_Spec),
5617 Specification => Act_Spec);
5619 -- The aspects have been copied previously, but they have to be
5620 -- linked explicitly to the new subprogram declaration. Explicit
5621 -- pre/postconditions on the instance are analyzed below, in a
5624 Move_Aspects (Act_Tree, To => Act_Decl);
5625 Set_Categorization_From_Pragmas (Act_Decl);
5627 if Parent_Installed then
5631 Append (Act_Decl, Renaming_List);
5633 -- Contract-related source pragmas that follow a generic subprogram
5634 -- must be instantiated explicitly because they are not part of the
5635 -- subprogram template.
5637 Instantiate_Subprogram_Contract
5638 (Original_Node (Gen_Decl), Renaming_List);
5640 Build_Subprogram_Renaming;
5642 -- If the context of the instance is subject to SPARK_Mode "off" or
5643 -- the annotation is altogether missing, set the global flag which
5644 -- signals Analyze_Pragma to ignore all SPARK_Mode pragmas within
5645 -- the instance. This should be done prior to analyzing the instance.
5647 if SPARK_Mode /= On then
5648 Ignore_SPARK_Mode_Pragmas_In_Instance := True;
5651 -- If the context of an instance is not subject to SPARK_Mode "off",
5652 -- and the generic spec is subject to an explicit SPARK_Mode pragma,
5653 -- the latter should be the one applicable to the instance.
5655 if not Ignore_SPARK_Mode_Pragmas_In_Instance
5656 and then Saved_SM /= Off
5657 and then Present (SPARK_Pragma (Gen_Unit))
5659 Set_SPARK_Mode (Gen_Unit);
5662 Analyze_Instance_And_Renamings;
5664 -- Restore SPARK_Mode from the context after analysis of the package
5665 -- declaration, so that the SPARK_Mode on the generic spec does not
5666 -- apply to the pending instance for the instance body.
5668 if not Ignore_SPARK_Mode_Pragmas_In_Instance
5669 and then Saved_SM /= Off
5670 and then Present (SPARK_Pragma (Gen_Unit))
5672 Restore_SPARK_Mode (Saved_SM, Saved_SMP);
5675 -- If the generic is marked Import (Intrinsic), then so is the
5676 -- instance. This indicates that there is no body to instantiate. If
5677 -- generic is marked inline, so it the instance, and the anonymous
5678 -- subprogram it renames. If inlined, or else if inlining is enabled
5679 -- for the compilation, we generate the instance body even if it is
5680 -- not within the main unit.
5682 if Is_Intrinsic_Subprogram (Gen_Unit) then
5683 Set_Is_Intrinsic_Subprogram (Anon_Id);
5684 Set_Is_Intrinsic_Subprogram (Act_Decl_Id);
5686 if Chars (Gen_Unit) = Name_Unchecked_Conversion then
5687 Validate_Unchecked_Conversion (N, Act_Decl_Id);
5691 -- Inherit convention from generic unit. Intrinsic convention, as for
5692 -- an instance of unchecked conversion, is not inherited because an
5693 -- explicit Ada instance has been created.
5695 if Has_Convention_Pragma (Gen_Unit)
5696 and then Convention (Gen_Unit) /= Convention_Intrinsic
5698 Set_Convention (Act_Decl_Id, Convention (Gen_Unit));
5699 Set_Is_Exported (Act_Decl_Id, Is_Exported (Gen_Unit));
5702 Generate_Definition (Act_Decl_Id);
5704 -- Inherit all inlining-related flags which apply to the generic in
5705 -- the subprogram and its declaration.
5707 Set_Is_Inlined (Act_Decl_Id, Is_Inlined (Gen_Unit));
5708 Set_Is_Inlined (Anon_Id, Is_Inlined (Gen_Unit));
5710 Set_Has_Pragma_Inline (Act_Decl_Id, Has_Pragma_Inline (Gen_Unit));
5711 Set_Has_Pragma_Inline (Anon_Id, Has_Pragma_Inline (Gen_Unit));
5713 Set_Has_Pragma_Inline_Always
5714 (Act_Decl_Id, Has_Pragma_Inline_Always (Gen_Unit));
5715 Set_Has_Pragma_Inline_Always
5716 (Anon_Id, Has_Pragma_Inline_Always (Gen_Unit));
5718 Set_Has_Pragma_No_Inline
5719 (Act_Decl_Id, Has_Pragma_No_Inline (Gen_Unit));
5720 Set_Has_Pragma_No_Inline
5721 (Anon_Id, Has_Pragma_No_Inline (Gen_Unit));
5723 -- Propagate No_Return if pragma applied to generic unit. This must
5724 -- be done explicitly because pragma does not appear in generic
5725 -- declaration (unlike the aspect case).
5727 if No_Return (Gen_Unit) then
5728 Set_No_Return (Act_Decl_Id);
5729 Set_No_Return (Anon_Id);
5732 -- Mark both the instance spec and the anonymous package in case the
5733 -- body is instantiated at a later pass. This preserves the original
5734 -- context in effect for the body.
5736 if SPARK_Mode /= On then
5737 Set_Ignore_SPARK_Mode_Pragmas (Act_Decl_Id);
5738 Set_Ignore_SPARK_Mode_Pragmas (Anon_Id);
5741 if Legacy_Elaboration_Checks
5742 and then not Is_Intrinsic_Subprogram (Gen_Unit)
5744 Check_Elab_Instantiation (N);
5747 -- Save the scenario for later examination by the ABE Processing
5750 Record_Elaboration_Scenario (N);
5752 -- The instantiation results in a guaranteed ABE. Create a completing
5753 -- body for the subprogram declaration because the real body will not
5756 if Is_Known_Guaranteed_ABE (N) then
5757 Provide_Completing_Bodies (Instance_Spec (N));
5760 if Is_Dispatching_Operation (Act_Decl_Id)
5761 and then Ada_Version >= Ada_2005
5767 Formal := First_Formal (Act_Decl_Id);
5768 while Present (Formal) loop
5769 if Ekind (Etype (Formal)) = E_Anonymous_Access_Type
5770 and then Is_Controlling_Formal (Formal)
5771 and then not Can_Never_Be_Null (Formal)
5774 ("access parameter& is controlling,", N, Formal);
5776 ("\corresponding parameter of & must be explicitly "
5777 & "null-excluding", N, Gen_Id);
5780 Next_Formal (Formal);
5785 Check_Hidden_Child_Unit (N, Gen_Unit, Act_Decl_Id);
5787 Validate_Categorization_Dependency (N, Act_Decl_Id);
5789 if not Is_Intrinsic_Subprogram (Act_Decl_Id) then
5790 Inherit_Context (Gen_Decl, N);
5792 Restore_Private_Views (Pack_Id, False);
5794 -- If the context requires a full instantiation, mark node for
5795 -- subsequent construction of the body.
5797 if Need_Subprogram_Instance_Body (N, Act_Decl_Id) then
5798 Check_Forward_Instantiation (Gen_Decl);
5800 -- The wrapper package is always delayed, because it does not
5801 -- constitute a freeze point, but to insure that the freeze node
5802 -- is placed properly, it is created directly when instantiating
5803 -- the body (otherwise the freeze node might appear to early for
5804 -- nested instantiations).
5806 elsif Nkind (Parent (N)) = N_Compilation_Unit then
5807 Rewrite (N, Unit (Parent (N)));
5808 Set_Unit (Parent (N), N);
5811 -- Replace instance node for library-level instantiations of
5812 -- intrinsic subprograms.
5814 elsif Nkind (Parent (N)) = N_Compilation_Unit then
5815 Rewrite (N, Unit (Parent (N)));
5816 Set_Unit (Parent (N), N);
5819 if Parent_Installed then
5823 Restore_Hidden_Primitives (Vis_Prims_List);
5825 Env_Installed := False;
5826 Generic_Renamings.Set_Last (0);
5827 Generic_Renamings_HTable.Reset;
5831 -- Analyze aspects in declaration if no errors appear in the instance.
5833 if Has_Aspects (N) and then Serious_Errors_Detected = Errs then
5834 Analyze_Aspect_Specifications (N, Act_Decl_Id);
5837 Ignore_SPARK_Mode_Pragmas_In_Instance := Saved_ISMP;
5838 Restore_Ghost_Region (Saved_GM, Saved_IGR);
5839 Restore_SPARK_Mode (Saved_SM, Saved_SMP);
5842 when Instantiation_Error =>
5843 if Parent_Installed then
5847 if Env_Installed then
5851 Ignore_SPARK_Mode_Pragmas_In_Instance := Saved_ISMP;
5852 Restore_Ghost_Region (Saved_GM, Saved_IGR);
5853 Restore_SPARK_Mode (Saved_SM, Saved_SMP);
5854 end Analyze_Subprogram_Instantiation;
5856 -------------------------
5857 -- Get_Associated_Node --
5858 -------------------------
5860 function Get_Associated_Node (N : Node_Id) return Node_Id is
5864 Assoc := Associated_Node (N);
5866 if Nkind (Assoc) /= Nkind (N) then
5869 elsif Nkind_In (Assoc, N_Aggregate, N_Extension_Aggregate) then
5873 -- If the node is part of an inner generic, it may itself have been
5874 -- remapped into a further generic copy. Associated_Node is otherwise
5875 -- used for the entity of the node, and will be of a different node
5876 -- kind, or else N has been rewritten as a literal or function call.
5878 while Present (Associated_Node (Assoc))
5879 and then Nkind (Associated_Node (Assoc)) = Nkind (Assoc)
5881 Assoc := Associated_Node (Assoc);
5884 -- Follow an additional link in case the final node was rewritten.
5885 -- This can only happen with nested generic units.
5887 if (Nkind (Assoc) = N_Identifier or else Nkind (Assoc) in N_Op)
5888 and then Present (Associated_Node (Assoc))
5889 and then (Nkind_In (Associated_Node (Assoc), N_Function_Call,
5890 N_Explicit_Dereference,
5895 Assoc := Associated_Node (Assoc);
5898 -- An additional special case: an unconstrained type in an object
5899 -- declaration may have been rewritten as a local subtype constrained
5900 -- by the expression in the declaration. We need to recover the
5901 -- original entity, which may be global.
5903 if Present (Original_Node (Assoc))
5904 and then Nkind (Parent (N)) = N_Object_Declaration
5906 Assoc := Original_Node (Assoc);
5911 end Get_Associated_Node;
5913 ----------------------------
5914 -- Build_Function_Wrapper --
5915 ----------------------------
5917 function Build_Function_Wrapper
5918 (Formal_Subp : Entity_Id;
5919 Actual_Subp : Entity_Id) return Node_Id
5921 Loc : constant Source_Ptr := Sloc (Current_Scope);
5922 Ret_Type : constant Entity_Id := Get_Instance_Of (Etype (Formal_Subp));
5925 Func_Name : Node_Id;
5927 Parm_Type : Node_Id;
5928 Profile : List_Id := New_List;
5935 Func_Name := New_Occurrence_Of (Actual_Subp, Loc);
5937 Func := Make_Defining_Identifier (Loc, Chars (Formal_Subp));
5938 Set_Ekind (Func, E_Function);
5939 Set_Is_Generic_Actual_Subprogram (Func);
5941 Actuals := New_List;
5942 Profile := New_List;
5944 Act_F := First_Formal (Actual_Subp);
5945 Form_F := First_Formal (Formal_Subp);
5946 while Present (Form_F) loop
5948 -- Create new formal for profile of wrapper, and add a reference
5949 -- to it in the list of actuals for the enclosing call. The name
5950 -- must be that of the formal in the formal subprogram, because
5951 -- calls to it in the generic body may use named associations.
5953 New_F := Make_Defining_Identifier (Loc, Chars (Form_F));
5956 New_Occurrence_Of (Get_Instance_Of (Etype (Form_F)), Loc);
5959 Make_Parameter_Specification (Loc,
5960 Defining_Identifier => New_F,
5961 Parameter_Type => Parm_Type));
5963 Append_To (Actuals, New_Occurrence_Of (New_F, Loc));
5964 Next_Formal (Form_F);
5966 if Present (Act_F) then
5967 Next_Formal (Act_F);
5972 Make_Function_Specification (Loc,
5973 Defining_Unit_Name => Func,
5974 Parameter_Specifications => Profile,
5975 Result_Definition => New_Occurrence_Of (Ret_Type, Loc));
5978 Make_Expression_Function (Loc,
5979 Specification => Spec,
5981 Make_Function_Call (Loc,
5983 Parameter_Associations => Actuals));
5986 end Build_Function_Wrapper;
5988 ----------------------------
5989 -- Build_Operator_Wrapper --
5990 ----------------------------
5992 function Build_Operator_Wrapper
5993 (Formal_Subp : Entity_Id;
5994 Actual_Subp : Entity_Id) return Node_Id
5996 Loc : constant Source_Ptr := Sloc (Current_Scope);
5997 Ret_Type : constant Entity_Id :=
5998 Get_Instance_Of (Etype (Formal_Subp));
5999 Op_Type : constant Entity_Id :=
6000 Get_Instance_Of (Etype (First_Formal (Formal_Subp)));
6001 Is_Binary : constant Boolean :=
6002 Present (Next_Formal (First_Formal (Formal_Subp)));
6005 Expr : Node_Id := Empty;
6013 Op_Name := Chars (Actual_Subp);
6015 -- Create entities for wrapper function and its formals
6017 F1 := Make_Temporary (Loc, 'A');
6018 F2 := Make_Temporary (Loc, 'B');
6019 L := New_Occurrence_Of (F1, Loc);
6020 R := New_Occurrence_Of (F2, Loc);
6022 Func := Make_Defining_Identifier (Loc, Chars (Formal_Subp));
6023 Set_Ekind (Func, E_Function);
6024 Set_Is_Generic_Actual_Subprogram (Func);
6027 Make_Function_Specification (Loc,
6028 Defining_Unit_Name => Func,
6029 Parameter_Specifications => New_List (
6030 Make_Parameter_Specification (Loc,
6031 Defining_Identifier => F1,
6032 Parameter_Type => New_Occurrence_Of (Op_Type, Loc))),
6033 Result_Definition => New_Occurrence_Of (Ret_Type, Loc));
6036 Append_To (Parameter_Specifications (Spec),
6037 Make_Parameter_Specification (Loc,
6038 Defining_Identifier => F2,
6039 Parameter_Type => New_Occurrence_Of (Op_Type, Loc)));
6042 -- Build expression as a function call, or as an operator node
6043 -- that corresponds to the name of the actual, starting with
6044 -- binary operators.
6046 if Op_Name not in Any_Operator_Name then
6048 Make_Function_Call (Loc,
6050 New_Occurrence_Of (Actual_Subp, Loc),
6051 Parameter_Associations => New_List (L));
6054 Append_To (Parameter_Associations (Expr), R);
6059 elsif Is_Binary then
6060 if Op_Name = Name_Op_And then
6061 Expr := Make_Op_And (Loc, Left_Opnd => L, Right_Opnd => R);
6062 elsif Op_Name = Name_Op_Or then
6063 Expr := Make_Op_Or (Loc, Left_Opnd => L, Right_Opnd => R);
6064 elsif Op_Name = Name_Op_Xor then
6065 Expr := Make_Op_Xor (Loc, Left_Opnd => L, Right_Opnd => R);
6066 elsif Op_Name = Name_Op_Eq then
6067 Expr := Make_Op_Eq (Loc, Left_Opnd => L, Right_Opnd => R);
6068 elsif Op_Name = Name_Op_Ne then
6069 Expr := Make_Op_Ne (Loc, Left_Opnd => L, Right_Opnd => R);
6070 elsif Op_Name = Name_Op_Le then
6071 Expr := Make_Op_Le (Loc, Left_Opnd => L, Right_Opnd => R);
6072 elsif Op_Name = Name_Op_Gt then
6073 Expr := Make_Op_Gt (Loc, Left_Opnd => L, Right_Opnd => R);
6074 elsif Op_Name = Name_Op_Ge then
6075 Expr := Make_Op_Ge (Loc, Left_Opnd => L, Right_Opnd => R);
6076 elsif Op_Name = Name_Op_Lt then
6077 Expr := Make_Op_Lt (Loc, Left_Opnd => L, Right_Opnd => R);
6078 elsif Op_Name = Name_Op_Add then
6079 Expr := Make_Op_Add (Loc, Left_Opnd => L, Right_Opnd => R);
6080 elsif Op_Name = Name_Op_Subtract then
6081 Expr := Make_Op_Subtract (Loc, Left_Opnd => L, Right_Opnd => R);
6082 elsif Op_Name = Name_Op_Concat then
6083 Expr := Make_Op_Concat (Loc, Left_Opnd => L, Right_Opnd => R);
6084 elsif Op_Name = Name_Op_Multiply then
6085 Expr := Make_Op_Multiply (Loc, Left_Opnd => L, Right_Opnd => R);
6086 elsif Op_Name = Name_Op_Divide then
6087 Expr := Make_Op_Divide (Loc, Left_Opnd => L, Right_Opnd => R);
6088 elsif Op_Name = Name_Op_Mod then
6089 Expr := Make_Op_Mod (Loc, Left_Opnd => L, Right_Opnd => R);
6090 elsif Op_Name = Name_Op_Rem then
6091 Expr := Make_Op_Rem (Loc, Left_Opnd => L, Right_Opnd => R);
6092 elsif Op_Name = Name_Op_Expon then
6093 Expr := Make_Op_Expon (Loc, Left_Opnd => L, Right_Opnd => R);
6099 if Op_Name = Name_Op_Add then
6100 Expr := Make_Op_Plus (Loc, Right_Opnd => L);
6101 elsif Op_Name = Name_Op_Subtract then
6102 Expr := Make_Op_Minus (Loc, Right_Opnd => L);
6103 elsif Op_Name = Name_Op_Abs then
6104 Expr := Make_Op_Abs (Loc, Right_Opnd => L);
6105 elsif Op_Name = Name_Op_Not then
6106 Expr := Make_Op_Not (Loc, Right_Opnd => L);
6111 Make_Expression_Function (Loc,
6112 Specification => Spec,
6113 Expression => Expr);
6116 end Build_Operator_Wrapper;
6118 -------------------------------------------
6119 -- Build_Instance_Compilation_Unit_Nodes --
6120 -------------------------------------------
6122 procedure Build_Instance_Compilation_Unit_Nodes
6127 Decl_Cunit : Node_Id;
6128 Body_Cunit : Node_Id;
6130 New_Main : constant Entity_Id := Defining_Entity (Act_Decl);
6131 Old_Main : constant Entity_Id := Cunit_Entity (Main_Unit);
6134 -- A new compilation unit node is built for the instance declaration
6137 Make_Compilation_Unit (Sloc (N),
6138 Context_Items => Empty_List,
6140 Aux_Decls_Node => Make_Compilation_Unit_Aux (Sloc (N)));
6142 Set_Parent_Spec (Act_Decl, Parent_Spec (N));
6144 -- The new compilation unit is linked to its body, but both share the
6145 -- same file, so we do not set Body_Required on the new unit so as not
6146 -- to create a spurious dependency on a non-existent body in the ali.
6147 -- This simplifies CodePeer unit traversal.
6149 -- We use the original instantiation compilation unit as the resulting
6150 -- compilation unit of the instance, since this is the main unit.
6152 Rewrite (N, Act_Body);
6154 -- Propagate the aspect specifications from the package body template to
6155 -- the instantiated version of the package body.
6157 if Has_Aspects (Act_Body) then
6158 Set_Aspect_Specifications
6159 (N, New_Copy_List_Tree (Aspect_Specifications (Act_Body)));
6162 Body_Cunit := Parent (N);
6164 -- The two compilation unit nodes are linked by the Library_Unit field
6166 Set_Library_Unit (Decl_Cunit, Body_Cunit);
6167 Set_Library_Unit (Body_Cunit, Decl_Cunit);
6169 -- Preserve the private nature of the package if needed
6171 Set_Private_Present (Decl_Cunit, Private_Present (Body_Cunit));
6173 -- If the instance is not the main unit, its context, categorization
6174 -- and elaboration entity are not relevant to the compilation.
6176 if Body_Cunit /= Cunit (Main_Unit) then
6177 Make_Instance_Unit (Body_Cunit, In_Main => False);
6181 -- The context clause items on the instantiation, which are now attached
6182 -- to the body compilation unit (since the body overwrote the original
6183 -- instantiation node), semantically belong on the spec, so copy them
6184 -- there. It's harmless to leave them on the body as well. In fact one
6185 -- could argue that they belong in both places.
6187 Citem := First (Context_Items (Body_Cunit));
6188 while Present (Citem) loop
6189 Append (New_Copy (Citem), Context_Items (Decl_Cunit));
6193 -- Propagate categorization flags on packages, so that they appear in
6194 -- the ali file for the spec of the unit.
6196 if Ekind (New_Main) = E_Package then
6197 Set_Is_Pure (Old_Main, Is_Pure (New_Main));
6198 Set_Is_Preelaborated (Old_Main, Is_Preelaborated (New_Main));
6199 Set_Is_Remote_Types (Old_Main, Is_Remote_Types (New_Main));
6200 Set_Is_Shared_Passive (Old_Main, Is_Shared_Passive (New_Main));
6201 Set_Is_Remote_Call_Interface
6202 (Old_Main, Is_Remote_Call_Interface (New_Main));
6205 -- Make entry in Units table, so that binder can generate call to
6206 -- elaboration procedure for body, if any.
6208 Make_Instance_Unit (Body_Cunit, In_Main => True);
6209 Main_Unit_Entity := New_Main;
6210 Set_Cunit_Entity (Main_Unit, Main_Unit_Entity);
6212 -- Build elaboration entity, since the instance may certainly generate
6213 -- elaboration code requiring a flag for protection.
6215 Build_Elaboration_Entity (Decl_Cunit, New_Main);
6216 end Build_Instance_Compilation_Unit_Nodes;
6218 -----------------------------
6219 -- Check_Access_Definition --
6220 -----------------------------
6222 procedure Check_Access_Definition (N : Node_Id) is
6225 (Ada_Version >= Ada_2005 and then Present (Access_Definition (N)));
6227 end Check_Access_Definition;
6229 -----------------------------------
6230 -- Check_Formal_Package_Instance --
6231 -----------------------------------
6233 -- If the formal has specific parameters, they must match those of the
6234 -- actual. Both of them are instances, and the renaming declarations for
6235 -- their formal parameters appear in the same order in both. The analyzed
6236 -- formal has been analyzed in the context of the current instance.
6238 procedure Check_Formal_Package_Instance
6239 (Formal_Pack : Entity_Id;
6240 Actual_Pack : Entity_Id)
6242 E1 : Entity_Id := First_Entity (Actual_Pack);
6243 E2 : Entity_Id := First_Entity (Formal_Pack);
6244 Prev_E1 : Entity_Id;
6249 procedure Check_Mismatch (B : Boolean);
6250 -- Common error routine for mismatch between the parameters of the
6251 -- actual instance and those of the formal package.
6253 function Is_Defaulted (Param : Entity_Id) return Boolean;
6254 -- If the formal package has partly box-initialized formals, skip
6255 -- conformance check for these formals. Previously the code assumed
6256 -- that box initialization for a formal package applied to all its
6257 -- formal parameters.
6259 function Same_Instantiated_Constant (E1, E2 : Entity_Id) return Boolean;
6260 -- The formal may come from a nested formal package, and the actual may
6261 -- have been constant-folded. To determine whether the two denote the
6262 -- same entity we may have to traverse several definitions to recover
6263 -- the ultimate entity that they refer to.
6265 function Same_Instantiated_Function (E1, E2 : Entity_Id) return Boolean;
6266 -- The formal and the actual must be identical, but if both are
6267 -- given by attributes they end up renaming different generated bodies,
6268 -- and we must verify that the attributes themselves match.
6270 function Same_Instantiated_Variable (E1, E2 : Entity_Id) return Boolean;
6271 -- Similarly, if the formal comes from a nested formal package, the
6272 -- actual may designate the formal through multiple renamings, which
6273 -- have to be followed to determine the original variable in question.
6275 --------------------
6276 -- Check_Mismatch --
6277 --------------------
6279 procedure Check_Mismatch (B : Boolean) is
6280 -- A Formal_Type_Declaration for a derived private type is rewritten
6281 -- as a private extension decl. (see Analyze_Formal_Derived_Type),
6282 -- which is why we examine the original node.
6284 Kind : constant Node_Kind := Nkind (Original_Node (Parent (E2)));
6287 if Kind = N_Formal_Type_Declaration then
6290 elsif Nkind_In (Kind, N_Formal_Object_Declaration,
6291 N_Formal_Package_Declaration)
6292 or else Kind in N_Formal_Subprogram_Declaration
6296 -- Ada 2012: If both formal and actual are incomplete types they
6299 elsif Is_Incomplete_Type (E1) and then Is_Incomplete_Type (E2) then
6304 ("actual for & in actual instance does not match formal",
6305 Parent (Actual_Pack), E1);
6313 function Is_Defaulted (Param : Entity_Id) return Boolean is
6318 First (Generic_Associations (Parent
6319 (Associated_Formal_Package (Actual_Pack))));
6321 while Present (Assoc) loop
6322 if Nkind (Assoc) = N_Others_Choice then
6325 elsif Nkind (Assoc) = N_Generic_Association
6326 and then Chars (Selector_Name (Assoc)) = Chars (Param)
6328 return Box_Present (Assoc);
6337 --------------------------------
6338 -- Same_Instantiated_Constant --
6339 --------------------------------
6341 function Same_Instantiated_Constant
6342 (E1, E2 : Entity_Id) return Boolean
6348 while Present (Ent) loop
6352 elsif Ekind (Ent) /= E_Constant then
6355 elsif Is_Entity_Name (Constant_Value (Ent)) then
6356 if Entity (Constant_Value (Ent)) = E1 then
6359 Ent := Entity (Constant_Value (Ent));
6362 -- The actual may be a constant that has been folded. Recover
6365 elsif Is_Entity_Name (Original_Node (Constant_Value (Ent))) then
6366 Ent := Entity (Original_Node (Constant_Value (Ent)));
6374 end Same_Instantiated_Constant;
6376 --------------------------------
6377 -- Same_Instantiated_Function --
6378 --------------------------------
6380 function Same_Instantiated_Function
6381 (E1, E2 : Entity_Id) return Boolean
6385 if Alias (E1) = Alias (E2) then
6388 elsif Present (Alias (E2)) then
6389 U1 := Original_Node (Unit_Declaration_Node (E1));
6390 U2 := Original_Node (Unit_Declaration_Node (Alias (E2)));
6392 return Nkind (U1) = N_Subprogram_Renaming_Declaration
6393 and then Nkind (Name (U1)) = N_Attribute_Reference
6395 and then Nkind (U2) = N_Subprogram_Renaming_Declaration
6396 and then Nkind (Name (U2)) = N_Attribute_Reference
6399 Attribute_Name (Name (U1)) = Attribute_Name (Name (U2));
6403 end Same_Instantiated_Function;
6405 --------------------------------
6406 -- Same_Instantiated_Variable --
6407 --------------------------------
6409 function Same_Instantiated_Variable
6410 (E1, E2 : Entity_Id) return Boolean
6412 function Original_Entity (E : Entity_Id) return Entity_Id;
6413 -- Follow chain of renamings to the ultimate ancestor
6415 ---------------------
6416 -- Original_Entity --
6417 ---------------------
6419 function Original_Entity (E : Entity_Id) return Entity_Id is
6424 while Nkind (Parent (Orig)) = N_Object_Renaming_Declaration
6425 and then Present (Renamed_Object (Orig))
6426 and then Is_Entity_Name (Renamed_Object (Orig))
6428 Orig := Entity (Renamed_Object (Orig));
6432 end Original_Entity;
6434 -- Start of processing for Same_Instantiated_Variable
6437 return Ekind (E1) = Ekind (E2)
6438 and then Original_Entity (E1) = Original_Entity (E2);
6439 end Same_Instantiated_Variable;
6441 -- Start of processing for Check_Formal_Package_Instance
6445 while Present (E1) and then Present (E2) loop
6446 exit when Ekind (E1) = E_Package
6447 and then Renamed_Entity (E1) = Renamed_Entity (Actual_Pack);
6449 -- If the formal is the renaming of the formal package, this
6450 -- is the end of its formal part, which may occur before the
6451 -- end of the formal part in the actual in the presence of
6452 -- defaulted parameters in the formal package.
6454 exit when Nkind (Parent (E2)) = N_Package_Renaming_Declaration
6455 and then Renamed_Entity (E2) = Scope (E2);
6457 -- The analysis of the actual may generate additional internal
6458 -- entities. If the formal is defaulted, there is no corresponding
6459 -- analysis and the internal entities must be skipped, until we
6460 -- find corresponding entities again.
6462 if Comes_From_Source (E2)
6463 and then not Comes_From_Source (E1)
6464 and then Chars (E1) /= Chars (E2)
6466 while Present (E1) and then Chars (E1) /= Chars (E2) loop
6474 -- Entities may be declared without full declaration, such as
6475 -- itypes and predefined operators (concatenation for arrays, eg).
6476 -- Skip it and keep the formal entity to find a later match for it.
6478 elsif No (Parent (E2)) and then Ekind (E1) /= Ekind (E2) then
6482 -- If the formal entity comes from a formal declaration, it was
6483 -- defaulted in the formal package, and no check is needed on it.
6485 elsif Nkind_In (Original_Node (Parent (E2)),
6486 N_Formal_Object_Declaration,
6487 N_Formal_Type_Declaration)
6489 -- If the formal is a tagged type the corresponding class-wide
6490 -- type has been generated as well, and it must be skipped.
6492 if Is_Type (E2) and then Is_Tagged_Type (E2) then
6498 -- Ditto for defaulted formal subprograms.
6500 elsif Is_Overloadable (E1)
6501 and then Nkind (Unit_Declaration_Node (E2)) in
6502 N_Formal_Subprogram_Declaration
6506 elsif Is_Defaulted (E1) then
6509 elsif Is_Type (E1) then
6511 -- Subtypes must statically match. E1, E2 are the local entities
6512 -- that are subtypes of the actuals. Itypes generated for other
6513 -- parameters need not be checked, the check will be performed
6514 -- on the parameters themselves.
6516 -- If E2 is a formal type declaration, it is a defaulted parameter
6517 -- and needs no checking.
6519 if not Is_Itype (E1) and then not Is_Itype (E2) then
6522 or else Etype (E1) /= Etype (E2)
6523 or else not Subtypes_Statically_Match (E1, E2));
6526 elsif Ekind (E1) = E_Constant then
6528 -- IN parameters must denote the same static value, or the same
6529 -- constant, or the literal null.
6531 Expr1 := Expression (Parent (E1));
6533 if Ekind (E2) /= E_Constant then
6534 Check_Mismatch (True);
6537 Expr2 := Expression (Parent (E2));
6540 if Is_OK_Static_Expression (Expr1) then
6541 if not Is_OK_Static_Expression (Expr2) then
6542 Check_Mismatch (True);
6544 elsif Is_Discrete_Type (Etype (E1)) then
6546 V1 : constant Uint := Expr_Value (Expr1);
6547 V2 : constant Uint := Expr_Value (Expr2);
6549 Check_Mismatch (V1 /= V2);
6552 elsif Is_Real_Type (Etype (E1)) then
6554 V1 : constant Ureal := Expr_Value_R (Expr1);
6555 V2 : constant Ureal := Expr_Value_R (Expr2);
6557 Check_Mismatch (V1 /= V2);
6560 elsif Is_String_Type (Etype (E1))
6561 and then Nkind (Expr1) = N_String_Literal
6563 if Nkind (Expr2) /= N_String_Literal then
6564 Check_Mismatch (True);
6567 (not String_Equal (Strval (Expr1), Strval (Expr2)));
6571 elsif Is_Entity_Name (Expr1) then
6572 if Is_Entity_Name (Expr2) then
6573 if Entity (Expr1) = Entity (Expr2) then
6577 (not Same_Instantiated_Constant
6578 (Entity (Expr1), Entity (Expr2)));
6582 Check_Mismatch (True);
6585 elsif Is_Entity_Name (Original_Node (Expr1))
6586 and then Is_Entity_Name (Expr2)
6587 and then Same_Instantiated_Constant
6588 (Entity (Original_Node (Expr1)), Entity (Expr2))
6592 elsif Nkind (Expr1) = N_Null then
6593 Check_Mismatch (Nkind (Expr1) /= N_Null);
6596 Check_Mismatch (True);
6599 elsif Ekind (E1) = E_Variable then
6600 Check_Mismatch (not Same_Instantiated_Variable (E1, E2));
6602 elsif Ekind (E1) = E_Package then
6604 (Ekind (E1) /= Ekind (E2)
6605 or else (Present (Renamed_Object (E2))
6606 and then Renamed_Object (E1) /=
6607 Renamed_Object (E2)));
6609 elsif Is_Overloadable (E1) then
6610 -- Verify that the actual subprograms match. Note that actuals
6611 -- that are attributes are rewritten as subprograms. If the
6612 -- subprogram in the formal package is defaulted, no check is
6613 -- needed. Note that this can only happen in Ada 2005 when the
6614 -- formal package can be partially parameterized.
6616 if Nkind (Unit_Declaration_Node (E1)) =
6617 N_Subprogram_Renaming_Declaration
6618 and then From_Default (Unit_Declaration_Node (E1))
6622 -- If the formal package has an "others" box association that
6623 -- covers this formal, there is no need for a check either.
6625 elsif Nkind (Unit_Declaration_Node (E2)) in
6626 N_Formal_Subprogram_Declaration
6627 and then Box_Present (Unit_Declaration_Node (E2))
6631 -- No check needed if subprogram is a defaulted null procedure
6633 elsif No (Alias (E2))
6634 and then Ekind (E2) = E_Procedure
6636 Null_Present (Specification (Unit_Declaration_Node (E2)))
6640 -- Otherwise the actual in the formal and the actual in the
6641 -- instantiation of the formal must match, up to renamings.
6645 (Ekind (E2) /= Ekind (E1)
6646 or else not Same_Instantiated_Function (E1, E2));
6650 raise Program_Error;
6658 end Check_Formal_Package_Instance;
6660 ---------------------------
6661 -- Check_Formal_Packages --
6662 ---------------------------
6664 procedure Check_Formal_Packages (P_Id : Entity_Id) is
6666 Formal_P : Entity_Id;
6667 Formal_Decl : Node_Id;
6669 -- Iterate through the declarations in the instance, looking for package
6670 -- renaming declarations that denote instances of formal packages. Stop
6671 -- when we find the renaming of the current package itself. The
6672 -- declaration for a formal package without a box is followed by an
6673 -- internal entity that repeats the instantiation.
6675 E := First_Entity (P_Id);
6676 while Present (E) loop
6677 if Ekind (E) = E_Package then
6678 if Renamed_Object (E) = P_Id then
6681 elsif Nkind (Parent (E)) /= N_Package_Renaming_Declaration then
6685 Formal_Decl := Parent (Associated_Formal_Package (E));
6687 -- Nothing to check if the formal has a box or an others_clause
6688 -- (necessarily with a box), or no associations altogether
6690 if Box_Present (Formal_Decl)
6691 or else No (Generic_Associations (Formal_Decl))
6695 elsif Nkind (First (Generic_Associations (Formal_Decl))) =
6698 -- The internal validating package was generated but formal
6699 -- and instance are known to be compatible.
6701 Formal_P := Next_Entity (E);
6702 Remove (Unit_Declaration_Node (Formal_P));
6705 Formal_P := Next_Entity (E);
6707 -- If the instance is within an enclosing instance body
6708 -- there is no need to verify the legality of current formal
6709 -- packages because they were legal in the generic body.
6710 -- This optimization may be applicable elsewhere, and it
6711 -- also removes spurious errors that may arise with
6712 -- on-the-fly inlining and confusion between private and
6715 if not In_Instance_Body then
6716 Check_Formal_Package_Instance (Formal_P, E);
6719 -- Restore the visibility of formals of the formal instance
6720 -- that are not defaulted, and are hidden within the current
6721 -- generic. These formals may be visible within an enclosing
6727 Elmt := First_Elmt (Hidden_In_Formal_Instance (Formal_P));
6728 while Present (Elmt) loop
6729 Set_Is_Hidden (Node (Elmt), False);
6734 -- After checking, remove the internal validating package.
6735 -- It is only needed for semantic checks, and as it may
6736 -- contain generic formal declarations it should not reach
6739 Remove (Unit_Declaration_Node (Formal_P));
6746 end Check_Formal_Packages;
6748 ---------------------------------
6749 -- Check_Forward_Instantiation --
6750 ---------------------------------
6752 procedure Check_Forward_Instantiation (Decl : Node_Id) is
6754 Gen_Comp : Entity_Id := Cunit_Entity (Get_Source_Unit (Decl));
6757 -- The instantiation appears before the generic body if we are in the
6758 -- scope of the unit containing the generic, either in its spec or in
6759 -- the package body, and before the generic body.
6761 if Ekind (Gen_Comp) = E_Package_Body then
6762 Gen_Comp := Spec_Entity (Gen_Comp);
6765 if In_Open_Scopes (Gen_Comp)
6766 and then No (Corresponding_Body (Decl))
6771 and then not Is_Compilation_Unit (S)
6772 and then not Is_Child_Unit (S)
6774 if Ekind (S) = E_Package then
6775 Set_Has_Forward_Instantiation (S);
6781 end Check_Forward_Instantiation;
6783 ---------------------------
6784 -- Check_Generic_Actuals --
6785 ---------------------------
6787 -- The visibility of the actuals may be different between the point of
6788 -- generic instantiation and the instantiation of the body.
6790 procedure Check_Generic_Actuals
6791 (Instance : Entity_Id;
6792 Is_Formal_Box : Boolean)
6798 E := First_Entity (Instance);
6799 while Present (E) loop
6801 and then Nkind (Parent (E)) = N_Subtype_Declaration
6802 and then Scope (Etype (E)) /= Instance
6803 and then Is_Entity_Name (Subtype_Indication (Parent (E)))
6805 -- Restore the proper view of the actual from the information
6806 -- saved earlier by Instantiate_Type.
6808 Check_Private_View (Subtype_Indication (Parent (E)));
6810 -- If the actual is itself the formal of a parent instance,
6811 -- then also restore the proper view of its actual and so on.
6812 -- That's necessary for nested instantiations of the form
6815 -- type Component is private;
6816 -- type Array_Type is array (Positive range <>) of Component;
6819 -- when the outermost actuals have inconsistent views, because
6820 -- the Component_Type of Array_Type of the inner instantiations
6821 -- is the actual of Component of the outermost one and not that
6822 -- of the corresponding inner instantiations.
6824 Astype := Ancestor_Subtype (E);
6825 while Present (Astype)
6826 and then Nkind (Parent (Astype)) = N_Subtype_Declaration
6827 and then Present (Generic_Parent_Type (Parent (Astype)))
6828 and then Is_Entity_Name (Subtype_Indication (Parent (Astype)))
6830 Check_Private_View (Subtype_Indication (Parent (Astype)));
6831 Astype := Ancestor_Subtype (Astype);
6834 Set_Is_Generic_Actual_Type (E);
6836 if Is_Private_Type (E) and then Present (Full_View (E)) then
6837 Set_Is_Generic_Actual_Type (Full_View (E));
6840 Set_Is_Hidden (E, False);
6841 Set_Is_Potentially_Use_Visible (E, In_Use (Instance));
6843 -- We constructed the generic actual type as a subtype of the
6844 -- supplied type. This means that it normally would not inherit
6845 -- subtype specific attributes of the actual, which is wrong for
6846 -- the generic case.
6848 Astype := Ancestor_Subtype (E);
6852 -- This can happen when E is an itype that is the full view of
6853 -- a private type completed, e.g. with a constrained array. In
6854 -- that case, use the first subtype, which will carry size
6855 -- information. The base type itself is unconstrained and will
6858 Astype := First_Subtype (E);
6861 Set_Size_Info (E, (Astype));
6862 Set_RM_Size (E, RM_Size (Astype));
6863 Set_First_Rep_Item (E, First_Rep_Item (Astype));
6865 if Is_Discrete_Or_Fixed_Point_Type (E) then
6866 Set_RM_Size (E, RM_Size (Astype));
6869 elsif Ekind (E) = E_Package then
6871 -- If this is the renaming for the current instance, we're done.
6872 -- Otherwise it is a formal package. If the corresponding formal
6873 -- was declared with a box, the (instantiations of the) generic
6874 -- formal part are also visible. Otherwise, ignore the entity
6875 -- created to validate the actuals.
6877 if Renamed_Object (E) = Instance then
6880 elsif Nkind (Parent (E)) /= N_Package_Renaming_Declaration then
6883 -- The visibility of a formal of an enclosing generic is already
6886 elsif Denotes_Formal_Package (E) then
6889 elsif Present (Associated_Formal_Package (E))
6890 and then not Is_Generic_Formal (E)
6892 if Box_Present (Parent (Associated_Formal_Package (E))) then
6893 Check_Generic_Actuals (Renamed_Object (E), True);
6896 Check_Generic_Actuals (Renamed_Object (E), False);
6899 Set_Is_Hidden (E, False);
6902 -- If this is a subprogram instance (in a wrapper package) the
6903 -- actual is fully visible.
6905 elsif Is_Wrapper_Package (Instance) then
6906 Set_Is_Hidden (E, False);
6908 -- If the formal package is declared with a box, or if the formal
6909 -- parameter is defaulted, it is visible in the body.
6911 elsif Is_Formal_Box or else Is_Visible_Formal (E) then
6912 Set_Is_Hidden (E, False);
6915 if Ekind (E) = E_Constant then
6917 -- If the type of the actual is a private type declared in the
6918 -- enclosing scope of the generic unit, the body of the generic
6919 -- sees the full view of the type (because it has to appear in
6920 -- the corresponding package body). If the type is private now,
6921 -- exchange views to restore the proper visiblity in the instance.
6924 Typ : constant Entity_Id := Base_Type (Etype (E));
6925 -- The type of the actual
6930 Parent_Scope : Entity_Id;
6931 -- The enclosing scope of the generic unit
6934 if Is_Wrapper_Package (Instance) then
6938 (Unit_Declaration_Node
6939 (Related_Instance (Instance))));
6942 Generic_Parent (Package_Specification (Instance));
6945 Parent_Scope := Scope (Gen_Id);
6947 -- The exchange is only needed if the generic is defined
6948 -- within a package which is not a common ancestor of the
6949 -- scope of the instance, and is not already in scope.
6951 if Is_Private_Type (Typ)
6952 and then Scope (Typ) = Parent_Scope
6953 and then Scope (Instance) /= Parent_Scope
6954 and then Ekind (Parent_Scope) = E_Package
6955 and then not Is_Child_Unit (Gen_Id)
6959 -- If the type of the entity is a subtype, it may also have
6960 -- to be made visible, together with the base type of its
6961 -- full view, after exchange.
6963 if Is_Private_Type (Etype (E)) then
6964 Switch_View (Etype (E));
6965 Switch_View (Base_Type (Etype (E)));
6973 end Check_Generic_Actuals;
6975 ------------------------------
6976 -- Check_Generic_Child_Unit --
6977 ------------------------------
6979 procedure Check_Generic_Child_Unit
6981 Parent_Installed : in out Boolean)
6983 Loc : constant Source_Ptr := Sloc (Gen_Id);
6984 Gen_Par : Entity_Id := Empty;
6986 Inst_Par : Entity_Id;
6989 function Find_Generic_Child
6991 Id : Node_Id) return Entity_Id;
6992 -- Search generic parent for possible child unit with the given name
6994 function In_Enclosing_Instance return Boolean;
6995 -- Within an instance of the parent, the child unit may be denoted by
6996 -- a simple name, or an abbreviated expanded name. Examine enclosing
6997 -- scopes to locate a possible parent instantiation.
6999 ------------------------
7000 -- Find_Generic_Child --
7001 ------------------------
7003 function Find_Generic_Child
7005 Id : Node_Id) return Entity_Id
7010 -- If entity of name is already set, instance has already been
7011 -- resolved, e.g. in an enclosing instantiation.
7013 if Present (Entity (Id)) then
7014 if Scope (Entity (Id)) = Scop then
7021 E := First_Entity (Scop);
7022 while Present (E) loop
7023 if Chars (E) = Chars (Id)
7024 and then Is_Child_Unit (E)
7026 if Is_Child_Unit (E)
7027 and then not Is_Visible_Lib_Unit (E)
7030 ("generic child unit& is not visible", Gen_Id, E);
7042 end Find_Generic_Child;
7044 ---------------------------
7045 -- In_Enclosing_Instance --
7046 ---------------------------
7048 function In_Enclosing_Instance return Boolean is
7049 Enclosing_Instance : Node_Id;
7050 Instance_Decl : Node_Id;
7053 -- We do not inline any call that contains instantiations, except
7054 -- for instantiations of Unchecked_Conversion, so if we are within
7055 -- an inlined body the current instance does not require parents.
7057 if In_Inlined_Body then
7058 pragma Assert (Chars (Gen_Id) = Name_Unchecked_Conversion);
7062 -- Loop to check enclosing scopes
7064 Enclosing_Instance := Current_Scope;
7065 while Present (Enclosing_Instance) loop
7066 Instance_Decl := Unit_Declaration_Node (Enclosing_Instance);
7068 if Ekind (Enclosing_Instance) = E_Package
7069 and then Is_Generic_Instance (Enclosing_Instance)
7071 (Generic_Parent (Specification (Instance_Decl)))
7073 -- Check whether the generic we are looking for is a child of
7076 E := Find_Generic_Child
7077 (Generic_Parent (Specification (Instance_Decl)), Gen_Id);
7078 exit when Present (E);
7084 Enclosing_Instance := Scope (Enclosing_Instance);
7096 Make_Expanded_Name (Loc,
7098 Prefix => New_Occurrence_Of (Enclosing_Instance, Loc),
7099 Selector_Name => New_Occurrence_Of (E, Loc)));
7101 Set_Entity (Gen_Id, E);
7102 Set_Etype (Gen_Id, Etype (E));
7103 Parent_Installed := False; -- Already in scope.
7106 end In_Enclosing_Instance;
7108 -- Start of processing for Check_Generic_Child_Unit
7111 -- If the name of the generic is given by a selected component, it may
7112 -- be the name of a generic child unit, and the prefix is the name of an
7113 -- instance of the parent, in which case the child unit must be visible.
7114 -- If this instance is not in scope, it must be placed there and removed
7115 -- after instantiation, because what is being instantiated is not the
7116 -- original child, but the corresponding child present in the instance
7119 -- If the child is instantiated within the parent, it can be given by
7120 -- a simple name. In this case the instance is already in scope, but
7121 -- the child generic must be recovered from the generic parent as well.
7123 if Nkind (Gen_Id) = N_Selected_Component then
7124 S := Selector_Name (Gen_Id);
7125 Analyze (Prefix (Gen_Id));
7126 Inst_Par := Entity (Prefix (Gen_Id));
7128 if Ekind (Inst_Par) = E_Package
7129 and then Present (Renamed_Object (Inst_Par))
7131 Inst_Par := Renamed_Object (Inst_Par);
7134 if Ekind (Inst_Par) = E_Package then
7135 if Nkind (Parent (Inst_Par)) = N_Package_Specification then
7136 Gen_Par := Generic_Parent (Parent (Inst_Par));
7138 elsif Nkind (Parent (Inst_Par)) = N_Defining_Program_Unit_Name
7140 Nkind (Parent (Parent (Inst_Par))) = N_Package_Specification
7142 Gen_Par := Generic_Parent (Parent (Parent (Inst_Par)));
7145 elsif Ekind (Inst_Par) = E_Generic_Package
7146 and then Nkind (Parent (Gen_Id)) = N_Formal_Package_Declaration
7148 -- A formal package may be a real child package, and not the
7149 -- implicit instance within a parent. In this case the child is
7150 -- not visible and has to be retrieved explicitly as well.
7152 Gen_Par := Inst_Par;
7155 if Present (Gen_Par) then
7157 -- The prefix denotes an instantiation. The entity itself may be a
7158 -- nested generic, or a child unit.
7160 E := Find_Generic_Child (Gen_Par, S);
7163 Change_Selected_Component_To_Expanded_Name (Gen_Id);
7164 Set_Entity (Gen_Id, E);
7165 Set_Etype (Gen_Id, Etype (E));
7167 Set_Etype (S, Etype (E));
7169 -- Indicate that this is a reference to the parent
7171 if In_Extended_Main_Source_Unit (Gen_Id) then
7172 Set_Is_Instantiated (Inst_Par);
7175 -- A common mistake is to replicate the naming scheme of a
7176 -- hierarchy by instantiating a generic child directly, rather
7177 -- than the implicit child in a parent instance:
7179 -- generic .. package Gpar is ..
7180 -- generic .. package Gpar.Child is ..
7181 -- package Par is new Gpar ();
7184 -- package Par.Child is new Gpar.Child ();
7185 -- rather than Par.Child
7187 -- In this case the instantiation is within Par, which is an
7188 -- instance, but Gpar does not denote Par because we are not IN
7189 -- the instance of Gpar, so this is illegal. The test below
7190 -- recognizes this particular case.
7192 if Is_Child_Unit (E)
7193 and then not Comes_From_Source (Entity (Prefix (Gen_Id)))
7194 and then (not In_Instance
7195 or else Nkind (Parent (Parent (Gen_Id))) =
7199 ("prefix of generic child unit must be instance of parent",
7203 if not In_Open_Scopes (Inst_Par)
7204 and then Nkind (Parent (Gen_Id)) not in
7205 N_Generic_Renaming_Declaration
7207 Install_Parent (Inst_Par);
7208 Parent_Installed := True;
7210 elsif In_Open_Scopes (Inst_Par) then
7212 -- If the parent is already installed, install the actuals
7213 -- for its formal packages. This is necessary when the child
7214 -- instance is a child of the parent instance: in this case,
7215 -- the parent is placed on the scope stack but the formal
7216 -- packages are not made visible.
7218 Install_Formal_Packages (Inst_Par);
7222 -- If the generic parent does not contain an entity that
7223 -- corresponds to the selector, the instance doesn't either.
7224 -- Analyzing the node will yield the appropriate error message.
7225 -- If the entity is not a child unit, then it is an inner
7226 -- generic in the parent.
7234 if Is_Child_Unit (Entity (Gen_Id))
7236 Nkind (Parent (Gen_Id)) not in N_Generic_Renaming_Declaration
7237 and then not In_Open_Scopes (Inst_Par)
7239 Install_Parent (Inst_Par);
7240 Parent_Installed := True;
7242 -- The generic unit may be the renaming of the implicit child
7243 -- present in an instance. In that case the parent instance is
7244 -- obtained from the name of the renamed entity.
7246 elsif Ekind (Entity (Gen_Id)) = E_Generic_Package
7247 and then Present (Renamed_Entity (Entity (Gen_Id)))
7248 and then Is_Child_Unit (Renamed_Entity (Entity (Gen_Id)))
7251 Renamed_Package : constant Node_Id :=
7252 Name (Parent (Entity (Gen_Id)));
7254 if Nkind (Renamed_Package) = N_Expanded_Name then
7255 Inst_Par := Entity (Prefix (Renamed_Package));
7256 Install_Parent (Inst_Par);
7257 Parent_Installed := True;
7263 elsif Nkind (Gen_Id) = N_Expanded_Name then
7265 -- Entity already present, analyze prefix, whose meaning may be an
7266 -- instance in the current context. If it is an instance of a
7267 -- relative within another, the proper parent may still have to be
7268 -- installed, if they are not of the same generation.
7270 Analyze (Prefix (Gen_Id));
7272 -- Prevent cascaded errors
7274 if Etype (Prefix (Gen_Id)) = Any_Type then
7278 -- In the unlikely case that a local declaration hides the name of
7279 -- the parent package, locate it on the homonym chain. If the context
7280 -- is an instance of the parent, the renaming entity is flagged as
7283 Inst_Par := Entity (Prefix (Gen_Id));
7284 while Present (Inst_Par)
7285 and then not Is_Package_Or_Generic_Package (Inst_Par)
7287 Inst_Par := Homonym (Inst_Par);
7290 pragma Assert (Present (Inst_Par));
7291 Set_Entity (Prefix (Gen_Id), Inst_Par);
7293 if In_Enclosing_Instance then
7296 elsif Present (Entity (Gen_Id))
7297 and then Is_Child_Unit (Entity (Gen_Id))
7298 and then not In_Open_Scopes (Inst_Par)
7300 Install_Parent (Inst_Par);
7301 Parent_Installed := True;
7304 elsif In_Enclosing_Instance then
7306 -- The child unit is found in some enclosing scope
7313 -- If this is the renaming of the implicit child in a parent
7314 -- instance, recover the parent name and install it.
7316 if Is_Entity_Name (Gen_Id) then
7317 E := Entity (Gen_Id);
7319 if Is_Generic_Unit (E)
7320 and then Nkind (Parent (E)) in N_Generic_Renaming_Declaration
7321 and then Is_Child_Unit (Renamed_Object (E))
7322 and then Is_Generic_Unit (Scope (Renamed_Object (E)))
7323 and then Nkind (Name (Parent (E))) = N_Expanded_Name
7325 Rewrite (Gen_Id, New_Copy_Tree (Name (Parent (E))));
7326 Inst_Par := Entity (Prefix (Gen_Id));
7328 if not In_Open_Scopes (Inst_Par) then
7329 Install_Parent (Inst_Par);
7330 Parent_Installed := True;
7333 -- If it is a child unit of a non-generic parent, it may be
7334 -- use-visible and given by a direct name. Install parent as
7337 elsif Is_Generic_Unit (E)
7338 and then Is_Child_Unit (E)
7340 Nkind (Parent (Gen_Id)) not in N_Generic_Renaming_Declaration
7341 and then not Is_Generic_Unit (Scope (E))
7343 if not In_Open_Scopes (Scope (E)) then
7344 Install_Parent (Scope (E));
7345 Parent_Installed := True;
7350 end Check_Generic_Child_Unit;
7352 -----------------------------
7353 -- Check_Hidden_Child_Unit --
7354 -----------------------------
7356 procedure Check_Hidden_Child_Unit
7358 Gen_Unit : Entity_Id;
7359 Act_Decl_Id : Entity_Id)
7361 Gen_Id : constant Node_Id := Name (N);
7364 if Is_Child_Unit (Gen_Unit)
7365 and then Is_Child_Unit (Act_Decl_Id)
7366 and then Nkind (Gen_Id) = N_Expanded_Name
7367 and then Entity (Prefix (Gen_Id)) = Scope (Act_Decl_Id)
7368 and then Chars (Gen_Unit) = Chars (Act_Decl_Id)
7370 Error_Msg_Node_2 := Scope (Act_Decl_Id);
7372 ("generic unit & is implicitly declared in &",
7373 Defining_Unit_Name (N), Gen_Unit);
7374 Error_Msg_N ("\instance must have different name",
7375 Defining_Unit_Name (N));
7377 end Check_Hidden_Child_Unit;
7379 ------------------------
7380 -- Check_Private_View --
7381 ------------------------
7383 procedure Check_Private_View (N : Node_Id) is
7384 T : constant Entity_Id := Etype (N);
7388 -- Exchange views if the type was not private in the generic but is
7389 -- private at the point of instantiation. Do not exchange views if
7390 -- the scope of the type is in scope. This can happen if both generic
7391 -- and instance are sibling units, or if type is defined in a parent.
7392 -- In this case the visibility of the type will be correct for all
7396 BT := Base_Type (T);
7398 if Is_Private_Type (T)
7399 and then not Has_Private_View (N)
7400 and then Present (Full_View (T))
7401 and then not In_Open_Scopes (Scope (T))
7403 -- In the generic, the full declaration was visible
7407 elsif Has_Private_View (N)
7408 and then not Is_Private_Type (T)
7409 and then not Has_Been_Exchanged (T)
7410 and then (not In_Open_Scopes (Scope (T))
7411 or else Nkind (Parent (N)) = N_Subtype_Declaration)
7413 -- In the generic, only the private declaration was visible
7415 -- If the type appears in a subtype declaration, the subtype in
7416 -- instance must have a view compatible with that of its parent,
7417 -- which must be exchanged (see corresponding code in Restore_
7418 -- Private_Views) so we make an exception to the open scope rule.
7420 Prepend_Elmt (T, Exchanged_Views);
7421 Exchange_Declarations (Etype (Get_Associated_Node (N)));
7423 -- Finally, a non-private subtype may have a private base type, which
7424 -- must be exchanged for consistency. This can happen when a package
7425 -- body is instantiated, when the scope stack is empty but in fact
7426 -- the subtype and the base type are declared in an enclosing scope.
7428 -- Note that in this case we introduce an inconsistency in the view
7429 -- set, because we switch the base type BT, but there could be some
7430 -- private dependent subtypes of BT which remain unswitched. Such
7431 -- subtypes might need to be switched at a later point (see specific
7432 -- provision for that case in Switch_View).
7434 elsif not Is_Private_Type (T)
7435 and then not Has_Private_View (N)
7436 and then Is_Private_Type (BT)
7437 and then Present (Full_View (BT))
7438 and then not Is_Generic_Type (BT)
7439 and then not In_Open_Scopes (BT)
7441 Prepend_Elmt (Full_View (BT), Exchanged_Views);
7442 Exchange_Declarations (BT);
7445 end Check_Private_View;
7447 -----------------------------
7448 -- Check_Hidden_Primitives --
7449 -----------------------------
7451 function Check_Hidden_Primitives (Assoc_List : List_Id) return Elist_Id is
7454 Result : Elist_Id := No_Elist;
7457 if No (Assoc_List) then
7461 -- Traverse the list of associations between formals and actuals
7462 -- searching for renamings of tagged types
7464 Actual := First (Assoc_List);
7465 while Present (Actual) loop
7466 if Nkind (Actual) = N_Subtype_Declaration then
7467 Gen_T := Generic_Parent_Type (Actual);
7469 if Present (Gen_T) and then Is_Tagged_Type (Gen_T) then
7471 -- Traverse the list of primitives of the actual types
7472 -- searching for hidden primitives that are visible in the
7473 -- corresponding generic formal; leave them visible and
7474 -- append them to Result to restore their decoration later.
7476 Install_Hidden_Primitives
7477 (Prims_List => Result,
7479 Act_T => Entity (Subtype_Indication (Actual)));
7487 end Check_Hidden_Primitives;
7489 --------------------------
7490 -- Contains_Instance_Of --
7491 --------------------------
7493 function Contains_Instance_Of
7496 N : Node_Id) return Boolean
7504 -- Verify that there are no circular instantiations. We check whether
7505 -- the unit contains an instance of the current scope or some enclosing
7506 -- scope (in case one of the instances appears in a subunit). Longer
7507 -- circularities involving subunits might seem too pathological to
7508 -- consider, but they were not too pathological for the authors of
7509 -- DEC bc30vsq, so we loop over all enclosing scopes, and mark all
7510 -- enclosing generic scopes as containing an instance.
7513 -- Within a generic subprogram body, the scope is not generic, to
7514 -- allow for recursive subprograms. Use the declaration to determine
7515 -- whether this is a generic unit.
7517 if Ekind (Scop) = E_Generic_Package
7518 or else (Is_Subprogram (Scop)
7519 and then Nkind (Unit_Declaration_Node (Scop)) =
7520 N_Generic_Subprogram_Declaration)
7522 Elmt := First_Elmt (Inner_Instances (Inner));
7524 while Present (Elmt) loop
7525 if Node (Elmt) = Scop then
7526 Error_Msg_Node_2 := Inner;
7528 ("circular Instantiation: & instantiated within &!",
7532 elsif Node (Elmt) = Inner then
7535 elsif Contains_Instance_Of (Node (Elmt), Scop, N) then
7536 Error_Msg_Node_2 := Inner;
7538 ("circular Instantiation: & instantiated within &!",
7546 -- Indicate that Inner is being instantiated within Scop
7548 Append_Elmt (Inner, Inner_Instances (Scop));
7551 if Scop = Standard_Standard then
7554 Scop := Scope (Scop);
7559 end Contains_Instance_Of;
7561 -----------------------
7562 -- Copy_Generic_Node --
7563 -----------------------
7565 function Copy_Generic_Node
7567 Parent_Id : Node_Id;
7568 Instantiating : Boolean) return Node_Id
7573 function Copy_Generic_Descendant (D : Union_Id) return Union_Id;
7574 -- Check the given value of one of the Fields referenced by the current
7575 -- node to determine whether to copy it recursively. The field may hold
7576 -- a Node_Id, a List_Id, or an Elist_Id, or a plain value (Sloc, Uint,
7577 -- Char) in which case it need not be copied.
7579 procedure Copy_Descendants;
7580 -- Common utility for various nodes
7582 function Copy_Generic_Elist (E : Elist_Id) return Elist_Id;
7583 -- Make copy of element list
7585 function Copy_Generic_List
7587 Parent_Id : Node_Id) return List_Id;
7588 -- Apply Copy_Node recursively to the members of a node list
7590 function In_Defining_Unit_Name (Nam : Node_Id) return Boolean;
7591 -- True if an identifier is part of the defining program unit name of
7593 -- Consider removing this subprogram now that ASIS no longer uses it.
7595 ----------------------
7596 -- Copy_Descendants --
7597 ----------------------
7599 procedure Copy_Descendants is
7600 use Atree.Unchecked_Access;
7601 -- This code section is part of the implementation of an untyped
7602 -- tree traversal, so it needs direct access to node fields.
7605 Set_Field1 (New_N, Copy_Generic_Descendant (Field1 (N)));
7606 Set_Field2 (New_N, Copy_Generic_Descendant (Field2 (N)));
7607 Set_Field3 (New_N, Copy_Generic_Descendant (Field3 (N)));
7608 Set_Field4 (New_N, Copy_Generic_Descendant (Field4 (N)));
7609 Set_Field5 (New_N, Copy_Generic_Descendant (Field5 (N)));
7610 end Copy_Descendants;
7612 -----------------------------
7613 -- Copy_Generic_Descendant --
7614 -----------------------------
7616 function Copy_Generic_Descendant (D : Union_Id) return Union_Id is
7618 if D = Union_Id (Empty) then
7621 elsif D in Node_Range then
7623 (Copy_Generic_Node (Node_Id (D), New_N, Instantiating));
7625 elsif D in List_Range then
7626 return Union_Id (Copy_Generic_List (List_Id (D), New_N));
7628 elsif D in Elist_Range then
7629 return Union_Id (Copy_Generic_Elist (Elist_Id (D)));
7631 -- Nothing else is copyable (e.g. Uint values), return as is
7636 end Copy_Generic_Descendant;
7638 ------------------------
7639 -- Copy_Generic_Elist --
7640 ------------------------
7642 function Copy_Generic_Elist (E : Elist_Id) return Elist_Id is
7649 M := First_Elmt (E);
7650 while Present (M) loop
7652 (Copy_Generic_Node (Node (M), Empty, Instantiating), L);
7661 end Copy_Generic_Elist;
7663 -----------------------
7664 -- Copy_Generic_List --
7665 -----------------------
7667 function Copy_Generic_List
7669 Parent_Id : Node_Id) return List_Id
7677 Set_Parent (New_L, Parent_Id);
7680 while Present (N) loop
7681 Append (Copy_Generic_Node (N, Empty, Instantiating), New_L);
7690 end Copy_Generic_List;
7692 ---------------------------
7693 -- In_Defining_Unit_Name --
7694 ---------------------------
7696 function In_Defining_Unit_Name (Nam : Node_Id) return Boolean is
7699 Present (Parent (Nam))
7700 and then (Nkind (Parent (Nam)) = N_Defining_Program_Unit_Name
7702 (Nkind (Parent (Nam)) = N_Expanded_Name
7703 and then In_Defining_Unit_Name (Parent (Nam))));
7704 end In_Defining_Unit_Name;
7706 -- Start of processing for Copy_Generic_Node
7713 New_N := New_Copy (N);
7715 -- Copy aspects if present
7717 if Has_Aspects (N) then
7718 Set_Has_Aspects (New_N, False);
7719 Set_Aspect_Specifications
7720 (New_N, Copy_Generic_List (Aspect_Specifications (N), Parent_Id));
7723 -- If we are instantiating, we want to adjust the sloc based on the
7724 -- current S_Adjustment. However, if this is the root node of a subunit,
7725 -- we need to defer that adjustment to below (see "elsif Instantiating
7726 -- and Was_Stub"), so it comes after Create_Instantiation_Source has
7727 -- computed the adjustment.
7730 and then not (Nkind (N) in N_Proper_Body
7731 and then Was_Originally_Stub (N))
7733 Adjust_Instantiation_Sloc (New_N, S_Adjustment);
7736 if not Is_List_Member (N) then
7737 Set_Parent (New_N, Parent_Id);
7740 -- Special casing for identifiers and other entity names and operators
7742 if Nkind_In (New_N, N_Character_Literal,
7746 or else Nkind (New_N) in N_Op
7748 if not Instantiating then
7750 -- Link both nodes in order to assign subsequently the entity of
7751 -- the copy to the original node, in case this is a global
7754 Set_Associated_Node (N, New_N);
7756 -- If we are within an instantiation, this is a nested generic
7757 -- that has already been analyzed at the point of definition.
7758 -- We must preserve references that were global to the enclosing
7759 -- parent at that point. Other occurrences, whether global or
7760 -- local to the current generic, must be resolved anew, so we
7761 -- reset the entity in the generic copy. A global reference has a
7762 -- smaller depth than the parent, or else the same depth in case
7763 -- both are distinct compilation units.
7765 -- A child unit is implicitly declared within the enclosing parent
7766 -- but is in fact global to it, and must be preserved.
7768 -- It is also possible for Current_Instantiated_Parent to be
7769 -- defined, and for this not to be a nested generic, namely if
7770 -- the unit is loaded through Rtsfind. In that case, the entity of
7771 -- New_N is only a link to the associated node, and not a defining
7774 -- The entities for parent units in the defining_program_unit of a
7775 -- generic child unit are established when the context of the unit
7776 -- is first analyzed, before the generic copy is made. They are
7777 -- preserved in the copy for use in e.g. ASIS queries.
7779 Ent := Entity (New_N);
7781 if No (Current_Instantiated_Parent.Gen_Id) then
7783 or else Nkind (Ent) /= N_Defining_Identifier
7784 or else not In_Defining_Unit_Name (N)
7786 Set_Associated_Node (New_N, Empty);
7791 not Nkind_In (Ent, N_Defining_Identifier,
7792 N_Defining_Character_Literal,
7793 N_Defining_Operator_Symbol)
7794 or else No (Scope (Ent))
7796 (Scope (Ent) = Current_Instantiated_Parent.Gen_Id
7797 and then not Is_Child_Unit (Ent))
7799 (Scope_Depth (Scope (Ent)) >
7800 Scope_Depth (Current_Instantiated_Parent.Gen_Id)
7802 Get_Source_Unit (Ent) =
7803 Get_Source_Unit (Current_Instantiated_Parent.Gen_Id))
7805 Set_Associated_Node (New_N, Empty);
7808 -- Case of instantiating identifier or some other name or operator
7811 -- If the associated node is still defined, the entity in it
7812 -- is global, and must be copied to the instance. If this copy
7813 -- is being made for a body to inline, it is applied to an
7814 -- instantiated tree, and the entity is already present and
7815 -- must be also preserved.
7818 Assoc : constant Node_Id := Get_Associated_Node (N);
7821 if Present (Assoc) then
7822 if Nkind (Assoc) = Nkind (N) then
7823 Set_Entity (New_N, Entity (Assoc));
7824 Check_Private_View (N);
7826 -- Here we deal with a very peculiar case for which the
7827 -- Has_Private_View mechanism is not sufficient, because
7828 -- the reference to the type is implicit in the tree,
7829 -- that is to say, it's not referenced from a node but
7830 -- only from another type, namely through Component_Type.
7834 -- type Pt is private;
7837 -- type Ft is array (Positive range <>) of Pt;
7839 -- procedure Check (F1, F2 : Ft; Lt : Boolean);
7843 -- type Pt is new Boolean;
7846 -- package body P is
7847 -- package body G is
7848 -- procedure Check (F1, F2 : Ft; Lt : Boolean) is
7850 -- if (F1 < F2) /= Lt then
7857 -- type Arr is array (Positive range <>) of P.Pt;
7859 -- package Inst is new P.G (Arr);
7861 -- Pt is a global type for the generic package G and it
7862 -- is not referenced in its body, but only as component
7863 -- type of Ft, which is a local type. This means that no
7864 -- references to Pt or Ft are seen during the copy of the
7865 -- body, the only reference to Pt being seen is when the
7866 -- actuals are checked by Check_Generic_Actuals, but Pt
7867 -- is still private at this point. In the end, the views
7868 -- of Pt are not switched in the body and, therefore, the
7869 -- array comparison is rejected because the component is
7872 -- Adding e.g. a dummy variable of type Pt in the body is
7873 -- sufficient to make everything work, so we generate an
7874 -- artificial reference to Pt on the fly and thus force
7875 -- the switching of views on the grounds that, if the
7876 -- comparison was accepted during the semantic analysis
7877 -- of the generic, this means that the component cannot
7878 -- have been private (see Sem_Type.Valid_Comparison_Arg).
7880 if Nkind (Assoc) in N_Op_Compare
7881 and then Present (Etype (Left_Opnd (Assoc)))
7882 and then Is_Array_Type (Etype (Left_Opnd (Assoc)))
7883 and then Present (Etype (Right_Opnd (Assoc)))
7884 and then Is_Array_Type (Etype (Right_Opnd (Assoc)))
7887 Ltyp : constant Entity_Id :=
7888 Etype (Left_Opnd (Assoc));
7889 Rtyp : constant Entity_Id :=
7890 Etype (Right_Opnd (Assoc));
7892 if Is_Private_Type (Component_Type (Ltyp)) then
7894 (New_Occurrence_Of (Component_Type (Ltyp),
7897 if Is_Private_Type (Component_Type (Rtyp)) then
7899 (New_Occurrence_Of (Component_Type (Rtyp),
7905 -- The node is a reference to a global type and acts as the
7906 -- subtype mark of a qualified expression created in order
7907 -- to aid resolution of accidental overloading in instances.
7908 -- Since N is a reference to a type, the Associated_Node of
7909 -- N denotes an entity rather than another identifier. See
7910 -- Qualify_Universal_Operands for details.
7912 elsif Nkind (N) = N_Identifier
7913 and then Nkind (Parent (N)) = N_Qualified_Expression
7914 and then Subtype_Mark (Parent (N)) = N
7915 and then Is_Qualified_Universal_Literal (Parent (N))
7917 Set_Entity (New_N, Assoc);
7919 -- The name in the call may be a selected component if the
7920 -- call has not been analyzed yet, as may be the case for
7921 -- pre/post conditions in a generic unit.
7923 elsif Nkind (Assoc) = N_Function_Call
7924 and then Is_Entity_Name (Name (Assoc))
7926 Set_Entity (New_N, Entity (Name (Assoc)));
7928 elsif Nkind_In (Assoc, N_Defining_Identifier,
7929 N_Defining_Character_Literal,
7930 N_Defining_Operator_Symbol)
7931 and then Expander_Active
7933 -- Inlining case: we are copying a tree that contains
7934 -- global entities, which are preserved in the copy to be
7935 -- used for subsequent inlining.
7940 Set_Entity (New_N, Empty);
7946 -- For expanded name, we must copy the Prefix and Selector_Name
7948 if Nkind (N) = N_Expanded_Name then
7950 (New_N, Copy_Generic_Node (Prefix (N), New_N, Instantiating));
7952 Set_Selector_Name (New_N,
7953 Copy_Generic_Node (Selector_Name (N), New_N, Instantiating));
7955 -- For operators, copy the operands
7957 elsif Nkind (N) in N_Op then
7958 if Nkind (N) in N_Binary_Op then
7959 Set_Left_Opnd (New_N,
7960 Copy_Generic_Node (Left_Opnd (N), New_N, Instantiating));
7963 Set_Right_Opnd (New_N,
7964 Copy_Generic_Node (Right_Opnd (N), New_N, Instantiating));
7967 -- Establish a link between an entity from the generic template and the
7968 -- corresponding entity in the generic copy to be analyzed.
7970 elsif Nkind (N) in N_Entity then
7971 if not Instantiating then
7972 Set_Associated_Entity (N, New_N);
7975 -- Clear any existing link the copy may inherit from the replicated
7976 -- generic template entity.
7978 Set_Associated_Entity (New_N, Empty);
7980 -- Special casing for stubs
7982 elsif Nkind (N) in N_Body_Stub then
7984 -- In any case, we must copy the specification or defining
7985 -- identifier as appropriate.
7987 if Nkind (N) = N_Subprogram_Body_Stub then
7988 Set_Specification (New_N,
7989 Copy_Generic_Node (Specification (N), New_N, Instantiating));
7992 Set_Defining_Identifier (New_N,
7994 (Defining_Identifier (N), New_N, Instantiating));
7997 -- If we are not instantiating, then this is where we load and
7998 -- analyze subunits, i.e. at the point where the stub occurs. A
7999 -- more permissive system might defer this analysis to the point
8000 -- of instantiation, but this seems too complicated for now.
8002 if not Instantiating then
8004 Subunit_Name : constant Unit_Name_Type := Get_Unit_Name (N);
8006 Unum : Unit_Number_Type;
8010 -- Make sure that, if it is a subunit of the main unit that is
8011 -- preprocessed and if -gnateG is specified, the preprocessed
8012 -- file will be written.
8014 Lib.Analysing_Subunit_Of_Main :=
8015 Lib.In_Extended_Main_Source_Unit (N);
8018 (Load_Name => Subunit_Name,
8022 Lib.Analysing_Subunit_Of_Main := False;
8024 -- If the proper body is not found, a warning message will be
8025 -- emitted when analyzing the stub, or later at the point of
8026 -- instantiation. Here we just leave the stub as is.
8028 if Unum = No_Unit then
8029 Subunits_Missing := True;
8030 goto Subunit_Not_Found;
8033 Subunit := Cunit (Unum);
8035 if Nkind (Unit (Subunit)) /= N_Subunit then
8037 ("found child unit instead of expected SEPARATE subunit",
8039 Error_Msg_Sloc := Sloc (N);
8040 Error_Msg_N ("\to complete stub #", Subunit);
8041 goto Subunit_Not_Found;
8044 -- We must create a generic copy of the subunit, in order to
8045 -- perform semantic analysis on it, and we must replace the
8046 -- stub in the original generic unit with the subunit, in order
8047 -- to preserve non-local references within.
8049 -- Only the proper body needs to be copied. Library_Unit and
8050 -- context clause are simply inherited by the generic copy.
8051 -- Note that the copy (which may be recursive if there are
8052 -- nested subunits) must be done first, before attaching it to
8053 -- the enclosing generic.
8057 (Proper_Body (Unit (Subunit)),
8058 Empty, Instantiating => False);
8060 -- Now place the original proper body in the original generic
8061 -- unit. This is a body, not a compilation unit.
8063 Rewrite (N, Proper_Body (Unit (Subunit)));
8064 Set_Is_Compilation_Unit (Defining_Entity (N), False);
8065 Set_Was_Originally_Stub (N);
8067 -- Finally replace the body of the subunit with its copy, and
8068 -- make this new subunit into the library unit of the generic
8069 -- copy, which does not have stubs any longer.
8071 Set_Proper_Body (Unit (Subunit), New_Body);
8072 Set_Library_Unit (New_N, Subunit);
8073 Inherit_Context (Unit (Subunit), N);
8076 -- If we are instantiating, this must be an error case, since
8077 -- otherwise we would have replaced the stub node by the proper body
8078 -- that corresponds. So just ignore it in the copy (i.e. we have
8079 -- copied it, and that is good enough).
8085 <<Subunit_Not_Found>> null;
8087 -- If the node is a compilation unit, it is the subunit of a stub, which
8088 -- has been loaded already (see code below). In this case, the library
8089 -- unit field of N points to the parent unit (which is a compilation
8090 -- unit) and need not (and cannot) be copied.
8092 -- When the proper body of the stub is analyzed, the library_unit link
8093 -- is used to establish the proper context (see sem_ch10).
8095 -- The other fields of a compilation unit are copied as usual
8097 elsif Nkind (N) = N_Compilation_Unit then
8099 -- This code can only be executed when not instantiating, because in
8100 -- the copy made for an instantiation, the compilation unit node has
8101 -- disappeared at the point that a stub is replaced by its proper
8104 pragma Assert (not Instantiating);
8106 Set_Context_Items (New_N,
8107 Copy_Generic_List (Context_Items (N), New_N));
8110 Copy_Generic_Node (Unit (N), New_N, Instantiating => False));
8112 Set_First_Inlined_Subprogram (New_N,
8114 (First_Inlined_Subprogram (N), New_N, Instantiating => False));
8119 (Aux_Decls_Node (N), New_N, Instantiating => False));
8121 -- For an assignment node, the assignment is known to be semantically
8122 -- legal if we are instantiating the template. This avoids incorrect
8123 -- diagnostics in generated code.
8125 elsif Nkind (N) = N_Assignment_Statement then
8127 -- Copy name and expression fields in usual manner
8130 Copy_Generic_Node (Name (N), New_N, Instantiating));
8132 Set_Expression (New_N,
8133 Copy_Generic_Node (Expression (N), New_N, Instantiating));
8135 if Instantiating then
8136 Set_Assignment_OK (Name (New_N), True);
8139 elsif Nkind_In (N, N_Aggregate, N_Extension_Aggregate) then
8140 if not Instantiating then
8141 Set_Associated_Node (N, New_N);
8144 if Present (Get_Associated_Node (N))
8145 and then Nkind (Get_Associated_Node (N)) = Nkind (N)
8147 -- In the generic the aggregate has some composite type. If at
8148 -- the point of instantiation the type has a private view,
8149 -- install the full view (and that of its ancestors, if any).
8152 T : Entity_Id := (Etype (Get_Associated_Node (New_N)));
8156 if Present (T) and then Is_Private_Type (T) then
8161 and then Is_Tagged_Type (T)
8162 and then Is_Derived_Type (T)
8164 Rt := Root_Type (T);
8169 if Is_Private_Type (T) then
8180 -- Do not copy the associated node, which points to the generic copy
8181 -- of the aggregate.
8184 use Atree.Unchecked_Access;
8185 -- This code section is part of the implementation of an untyped
8186 -- tree traversal, so it needs direct access to node fields.
8189 Set_Field1 (New_N, Copy_Generic_Descendant (Field1 (N)));
8190 Set_Field2 (New_N, Copy_Generic_Descendant (Field2 (N)));
8191 Set_Field3 (New_N, Copy_Generic_Descendant (Field3 (N)));
8192 Set_Field5 (New_N, Copy_Generic_Descendant (Field5 (N)));
8195 -- Allocators do not have an identifier denoting the access type, so we
8196 -- must locate it through the expression to check whether the views are
8199 elsif Nkind (N) = N_Allocator
8200 and then Nkind (Expression (N)) = N_Qualified_Expression
8201 and then Is_Entity_Name (Subtype_Mark (Expression (N)))
8202 and then Instantiating
8205 T : constant Node_Id :=
8206 Get_Associated_Node (Subtype_Mark (Expression (N)));
8212 -- Retrieve the allocator node in the generic copy
8214 Acc_T := Etype (Parent (Parent (T)));
8216 if Present (Acc_T) and then Is_Private_Type (Acc_T) then
8217 Switch_View (Acc_T);
8224 -- For a proper body, we must catch the case of a proper body that
8225 -- replaces a stub. This represents the point at which a separate
8226 -- compilation unit, and hence template file, may be referenced, so we
8227 -- must make a new source instantiation entry for the template of the
8228 -- subunit, and ensure that all nodes in the subunit are adjusted using
8229 -- this new source instantiation entry.
8231 elsif Nkind (N) in N_Proper_Body then
8233 Save_Adjustment : constant Sloc_Adjustment := S_Adjustment;
8235 if Instantiating and then Was_Originally_Stub (N) then
8236 Create_Instantiation_Source
8237 (Instantiation_Node,
8238 Defining_Entity (N),
8241 Adjust_Instantiation_Sloc (New_N, S_Adjustment);
8244 -- Now copy the fields of the proper body, using the new
8245 -- adjustment factor if one was needed as per test above.
8249 -- Restore the original adjustment factor
8251 S_Adjustment := Save_Adjustment;
8254 elsif Nkind (N) = N_Pragma and then Instantiating then
8256 -- Do not copy Comment or Ident pragmas their content is relevant to
8257 -- the generic unit, not to the instantiating unit.
8259 if Nam_In (Pragma_Name_Unmapped (N), Name_Comment, Name_Ident) then
8260 New_N := Make_Null_Statement (Sloc (N));
8262 -- Do not copy pragmas generated from aspects because the pragmas do
8263 -- not carry any semantic information, plus they will be regenerated
8266 -- However, generating C we need to copy them since postconditions
8267 -- are inlined by the front end, and the front-end inlining machinery
8268 -- relies on this routine to perform inlining.
8270 elsif From_Aspect_Specification (N)
8271 and then not Modify_Tree_For_C
8273 New_N := Make_Null_Statement (Sloc (N));
8279 elsif Nkind_In (N, N_Integer_Literal, N_Real_Literal) then
8281 -- No descendant fields need traversing
8285 elsif Nkind (N) = N_String_Literal
8286 and then Present (Etype (N))
8287 and then Instantiating
8289 -- If the string is declared in an outer scope, the string_literal
8290 -- subtype created for it may have the wrong scope. Force reanalysis
8291 -- of the constant to generate a new itype in the proper context.
8293 Set_Etype (New_N, Empty);
8294 Set_Analyzed (New_N, False);
8296 -- For the remaining nodes, copy their descendants recursively
8301 if Instantiating and then Nkind (N) = N_Subprogram_Body then
8302 Set_Generic_Parent (Specification (New_N), N);
8304 -- Should preserve Corresponding_Spec??? (12.3(14))
8308 -- Propagate dimensions if present, so that they are reflected in the
8311 if Nkind (N) in N_Has_Etype
8312 and then (Nkind (N) in N_Op or else Is_Entity_Name (N))
8313 and then Present (Etype (N))
8314 and then Is_Floating_Point_Type (Etype (N))
8315 and then Has_Dimension_System (Etype (N))
8317 Copy_Dimensions (N, New_N);
8321 end Copy_Generic_Node;
8323 ----------------------------
8324 -- Denotes_Formal_Package --
8325 ----------------------------
8327 function Denotes_Formal_Package
8329 On_Exit : Boolean := False;
8330 Instance : Entity_Id := Empty) return Boolean
8333 Scop : constant Entity_Id := Scope (Pack);
8336 function Is_Actual_Of_Previous_Formal (P : Entity_Id) return Boolean;
8337 -- The package in question may be an actual for a previous formal
8338 -- package P of the current instance, so examine its actuals as well.
8339 -- This must be recursive over other formal packages.
8341 ----------------------------------
8342 -- Is_Actual_Of_Previous_Formal --
8343 ----------------------------------
8345 function Is_Actual_Of_Previous_Formal (P : Entity_Id) return Boolean is
8349 E1 := First_Entity (P);
8350 while Present (E1) and then E1 /= Instance loop
8351 if Ekind (E1) = E_Package
8352 and then Nkind (Parent (E1)) = N_Package_Renaming_Declaration
8354 if Renamed_Object (E1) = Pack then
8357 elsif E1 = P or else Renamed_Object (E1) = P then
8360 elsif Is_Actual_Of_Previous_Formal (E1) then
8369 end Is_Actual_Of_Previous_Formal;
8371 -- Start of processing for Denotes_Formal_Package
8377 (Instance_Envs.Last).Instantiated_Parent.Act_Id;
8379 Par := Current_Instantiated_Parent.Act_Id;
8382 if Ekind (Scop) = E_Generic_Package
8383 or else Nkind (Unit_Declaration_Node (Scop)) =
8384 N_Generic_Subprogram_Declaration
8388 elsif Nkind (Original_Node (Unit_Declaration_Node (Pack))) =
8389 N_Formal_Package_Declaration
8397 -- Check whether this package is associated with a formal package of
8398 -- the enclosing instantiation. Iterate over the list of renamings.
8400 E := First_Entity (Par);
8401 while Present (E) loop
8402 if Ekind (E) /= E_Package
8403 or else Nkind (Parent (E)) /= N_Package_Renaming_Declaration
8407 elsif Renamed_Object (E) = Par then
8410 elsif Renamed_Object (E) = Pack then
8413 elsif Is_Actual_Of_Previous_Formal (E) then
8423 end Denotes_Formal_Package;
8429 procedure End_Generic is
8431 -- ??? More things could be factored out in this routine. Should
8432 -- probably be done at a later stage.
8434 Inside_A_Generic := Generic_Flags.Table (Generic_Flags.Last);
8435 Generic_Flags.Decrement_Last;
8437 Expander_Mode_Restore;
8444 function Earlier (N1, N2 : Node_Id) return Boolean is
8445 procedure Find_Depth (P : in out Node_Id; D : in out Integer);
8446 -- Find distance from given node to enclosing compilation unit
8452 procedure Find_Depth (P : in out Node_Id; D : in out Integer) is
8455 and then Nkind (P) /= N_Compilation_Unit
8457 P := True_Parent (P);
8462 -- Local declarations
8471 -- Start of processing for Earlier
8474 Find_Depth (P1, D1);
8475 Find_Depth (P2, D2);
8485 P1 := True_Parent (P1);
8490 P2 := True_Parent (P2);
8494 -- At this point P1 and P2 are at the same distance from the root.
8495 -- We examine their parents until we find a common declarative list.
8496 -- If we reach the root, N1 and N2 do not descend from the same
8497 -- declarative list (e.g. one is nested in the declarative part and
8498 -- the other is in a block in the statement part) and the earlier
8499 -- one is already frozen.
8501 while not Is_List_Member (P1)
8502 or else not Is_List_Member (P2)
8503 or else List_Containing (P1) /= List_Containing (P2)
8505 P1 := True_Parent (P1);
8506 P2 := True_Parent (P2);
8508 if Nkind (Parent (P1)) = N_Subunit then
8509 P1 := Corresponding_Stub (Parent (P1));
8512 if Nkind (Parent (P2)) = N_Subunit then
8513 P2 := Corresponding_Stub (Parent (P2));
8521 -- Expanded code usually shares the source location of the original
8522 -- construct it was generated for. This however may not necessarily
8523 -- reflect the true location of the code within the tree.
8525 -- Before comparing the slocs of the two nodes, make sure that we are
8526 -- working with correct source locations. Assume that P1 is to the left
8527 -- of P2. If either one does not come from source, traverse the common
8528 -- list heading towards the other node and locate the first source
8532 -- ----+===+===+--------------+===+===+----
8533 -- expanded code expanded code
8535 if not Comes_From_Source (P1) then
8536 while Present (P1) loop
8538 -- Neither P2 nor a source statement were located during the
8539 -- search. If we reach the end of the list, then P1 does not
8540 -- occur earlier than P2.
8543 -- start --- P2 ----- P1 --- end
8545 if No (Next (P1)) then
8548 -- We encounter P2 while going to the right of the list. This
8549 -- means that P1 does indeed appear earlier.
8552 -- start --- P1 ===== P2 --- end
8553 -- expanded code in between
8558 -- No need to look any further since we have located a source
8561 elsif Comes_From_Source (P1) then
8571 if not Comes_From_Source (P2) then
8572 while Present (P2) loop
8574 -- Neither P1 nor a source statement were located during the
8575 -- search. If we reach the start of the list, then P1 does not
8576 -- occur earlier than P2.
8579 -- start --- P2 --- P1 --- end
8581 if No (Prev (P2)) then
8584 -- We encounter P1 while going to the left of the list. This
8585 -- means that P1 does indeed appear earlier.
8588 -- start --- P1 ===== P2 --- end
8589 -- expanded code in between
8594 -- No need to look any further since we have located a source
8597 elsif Comes_From_Source (P2) then
8607 -- At this point either both nodes came from source or we approximated
8608 -- their source locations through neighboring source statements.
8610 T1 := Top_Level_Location (Sloc (P1));
8611 T2 := Top_Level_Location (Sloc (P2));
8613 -- When two nodes come from the same instance, they have identical top
8614 -- level locations. To determine proper relation within the tree, check
8615 -- their locations within the template.
8618 return Sloc (P1) < Sloc (P2);
8620 -- The two nodes either come from unrelated instances or do not come
8621 -- from instantiated code at all.
8628 ----------------------
8629 -- Find_Actual_Type --
8630 ----------------------
8632 function Find_Actual_Type
8634 Gen_Type : Entity_Id) return Entity_Id
8636 Gen_Scope : constant Entity_Id := Scope (Gen_Type);
8640 -- Special processing only applies to child units
8642 if not Is_Child_Unit (Gen_Scope) then
8643 return Get_Instance_Of (Typ);
8645 -- If designated or component type is itself a formal of the child unit,
8646 -- its instance is available.
8648 elsif Scope (Typ) = Gen_Scope then
8649 return Get_Instance_Of (Typ);
8651 -- If the array or access type is not declared in the parent unit,
8652 -- no special processing needed.
8654 elsif not Is_Generic_Type (Typ)
8655 and then Scope (Gen_Scope) /= Scope (Typ)
8657 return Get_Instance_Of (Typ);
8659 -- Otherwise, retrieve designated or component type by visibility
8662 T := Current_Entity (Typ);
8663 while Present (T) loop
8664 if In_Open_Scopes (Scope (T)) then
8666 elsif Is_Generic_Actual_Type (T) then
8675 end Find_Actual_Type;
8677 ----------------------------
8678 -- Freeze_Subprogram_Body --
8679 ----------------------------
8681 procedure Freeze_Subprogram_Body
8682 (Inst_Node : Node_Id;
8684 Pack_Id : Entity_Id)
8686 Gen_Unit : constant Entity_Id := Get_Generic_Entity (Inst_Node);
8687 Par : constant Entity_Id := Scope (Gen_Unit);
8693 function Enclosing_Package_Body (N : Node_Id) return Node_Id;
8694 -- Find innermost package body that encloses the given node, and which
8695 -- is not a compilation unit. Freeze nodes for the instance, or for its
8696 -- enclosing body, may be inserted after the enclosing_body of the
8697 -- generic unit. Used to determine proper placement of freeze node for
8698 -- both package and subprogram instances.
8700 function Package_Freeze_Node (B : Node_Id) return Node_Id;
8701 -- Find entity for given package body, and locate or create a freeze
8704 ----------------------------
8705 -- Enclosing_Package_Body --
8706 ----------------------------
8708 function Enclosing_Package_Body (N : Node_Id) return Node_Id is
8714 and then Nkind (Parent (P)) /= N_Compilation_Unit
8716 if Nkind (P) = N_Package_Body then
8717 if Nkind (Parent (P)) = N_Subunit then
8718 return Corresponding_Stub (Parent (P));
8724 P := True_Parent (P);
8728 end Enclosing_Package_Body;
8730 -------------------------
8731 -- Package_Freeze_Node --
8732 -------------------------
8734 function Package_Freeze_Node (B : Node_Id) return Node_Id is
8738 if Nkind (B) = N_Package_Body then
8739 Id := Corresponding_Spec (B);
8740 else pragma Assert (Nkind (B) = N_Package_Body_Stub);
8741 Id := Corresponding_Spec (Proper_Body (Unit (Library_Unit (B))));
8744 Ensure_Freeze_Node (Id);
8745 return Freeze_Node (Id);
8746 end Package_Freeze_Node;
8748 -- Start of processing for Freeze_Subprogram_Body
8751 -- If the instance and the generic body appear within the same unit, and
8752 -- the instance precedes the generic, the freeze node for the instance
8753 -- must appear after that of the generic. If the generic is nested
8754 -- within another instance I2, then current instance must be frozen
8755 -- after I2. In both cases, the freeze nodes are those of enclosing
8756 -- packages. Otherwise, the freeze node is placed at the end of the
8757 -- current declarative part.
8759 Enc_G := Enclosing_Package_Body (Gen_Body);
8760 Enc_I := Enclosing_Package_Body (Inst_Node);
8761 Ensure_Freeze_Node (Pack_Id);
8762 F_Node := Freeze_Node (Pack_Id);
8764 if Is_Generic_Instance (Par)
8765 and then Present (Freeze_Node (Par))
8766 and then In_Same_Declarative_Part
8767 (Parent (Freeze_Node (Par)), Inst_Node)
8769 -- The parent was a premature instantiation. Insert freeze node at
8770 -- the end the current declarative part.
8772 if Is_Known_Guaranteed_ABE (Get_Unit_Instantiation_Node (Par)) then
8773 Insert_Freeze_Node_For_Instance (Inst_Node, F_Node);
8775 -- Handle the following case:
8777 -- package Parent_Inst is new ...
8780 -- procedure P ... -- this body freezes Parent_Inst
8782 -- package Inst is new ...
8784 -- In this particular scenario, the freeze node for Inst must be
8785 -- inserted in the same manner as that of Parent_Inst - before the
8786 -- next source body or at the end of the declarative list (body not
8787 -- available). If body P did not exist and Parent_Inst was frozen
8788 -- after Inst, either by a body following Inst or at the end of the
8789 -- declarative region, the freeze node for Inst must be inserted
8790 -- after that of Parent_Inst. This relation is established by
8791 -- comparing the Slocs of Parent_Inst freeze node and Inst.
8793 elsif List_Containing (Get_Unit_Instantiation_Node (Par)) =
8794 List_Containing (Inst_Node)
8795 and then Sloc (Freeze_Node (Par)) < Sloc (Inst_Node)
8797 Insert_Freeze_Node_For_Instance (Inst_Node, F_Node);
8800 Insert_After (Freeze_Node (Par), F_Node);
8803 -- The body enclosing the instance should be frozen after the body that
8804 -- includes the generic, because the body of the instance may make
8805 -- references to entities therein. If the two are not in the same
8806 -- declarative part, or if the one enclosing the instance is frozen
8807 -- already, freeze the instance at the end of the current declarative
8810 elsif Is_Generic_Instance (Par)
8811 and then Present (Freeze_Node (Par))
8812 and then Present (Enc_I)
8814 if In_Same_Declarative_Part (Parent (Freeze_Node (Par)), Enc_I)
8816 (Nkind (Enc_I) = N_Package_Body
8817 and then In_Same_Declarative_Part
8818 (Parent (Freeze_Node (Par)), Parent (Enc_I)))
8820 -- The enclosing package may contain several instances. Rather
8821 -- than computing the earliest point at which to insert its freeze
8822 -- node, we place it at the end of the declarative part of the
8823 -- parent of the generic.
8825 Insert_Freeze_Node_For_Instance
8826 (Freeze_Node (Par), Package_Freeze_Node (Enc_I));
8829 Insert_Freeze_Node_For_Instance (Inst_Node, F_Node);
8831 elsif Present (Enc_G)
8832 and then Present (Enc_I)
8833 and then Enc_G /= Enc_I
8834 and then Earlier (Inst_Node, Gen_Body)
8836 if Nkind (Enc_G) = N_Package_Body then
8838 Corresponding_Spec (Enc_G);
8839 else pragma Assert (Nkind (Enc_G) = N_Package_Body_Stub);
8841 Corresponding_Spec (Proper_Body (Unit (Library_Unit (Enc_G))));
8844 -- Freeze package that encloses instance, and place node after the
8845 -- package that encloses generic. If enclosing package is already
8846 -- frozen we have to assume it is at the proper place. This may be a
8847 -- potential ABE that requires dynamic checking. Do not add a freeze
8848 -- node if the package that encloses the generic is inside the body
8849 -- that encloses the instance, because the freeze node would be in
8850 -- the wrong scope. Additional contortions needed if the bodies are
8851 -- within a subunit.
8854 Enclosing_Body : Node_Id;
8857 if Nkind (Enc_I) = N_Package_Body_Stub then
8858 Enclosing_Body := Proper_Body (Unit (Library_Unit (Enc_I)));
8860 Enclosing_Body := Enc_I;
8863 if Parent (List_Containing (Enc_G)) /= Enclosing_Body then
8864 Insert_Freeze_Node_For_Instance
8865 (Enc_G, Package_Freeze_Node (Enc_I));
8869 -- Freeze enclosing subunit before instance
8871 Ensure_Freeze_Node (E_G_Id);
8873 if not Is_List_Member (Freeze_Node (E_G_Id)) then
8874 Insert_After (Enc_G, Freeze_Node (E_G_Id));
8877 Insert_Freeze_Node_For_Instance (Inst_Node, F_Node);
8880 -- If none of the above, insert freeze node at the end of the current
8881 -- declarative part.
8883 Insert_Freeze_Node_For_Instance (Inst_Node, F_Node);
8885 end Freeze_Subprogram_Body;
8891 function Get_Gen_Id (E : Assoc_Ptr) return Entity_Id is
8893 return Generic_Renamings.Table (E).Gen_Id;
8896 ---------------------
8897 -- Get_Instance_Of --
8898 ---------------------
8900 function Get_Instance_Of (A : Entity_Id) return Entity_Id is
8901 Res : constant Assoc_Ptr := Generic_Renamings_HTable.Get (A);
8904 if Res /= Assoc_Null then
8905 return Generic_Renamings.Table (Res).Act_Id;
8908 -- On exit, entity is not instantiated: not a generic parameter, or
8909 -- else parameter of an inner generic unit.
8913 end Get_Instance_Of;
8915 ---------------------------------
8916 -- Get_Unit_Instantiation_Node --
8917 ---------------------------------
8919 function Get_Unit_Instantiation_Node (A : Entity_Id) return Node_Id is
8920 Decl : Node_Id := Unit_Declaration_Node (A);
8924 -- If the Package_Instantiation attribute has been set on the package
8925 -- entity, then use it directly when it (or its Original_Node) refers
8926 -- to an N_Package_Instantiation node. In principle it should be
8927 -- possible to have this field set in all cases, which should be
8928 -- investigated, and would allow this function to be significantly
8931 Inst := Package_Instantiation (A);
8933 if Present (Inst) then
8934 if Nkind (Inst) = N_Package_Instantiation then
8937 elsif Nkind (Original_Node (Inst)) = N_Package_Instantiation then
8938 return Original_Node (Inst);
8942 -- If the instantiation is a compilation unit that does not need body
8943 -- then the instantiation node has been rewritten as a package
8944 -- declaration for the instance, and we return the original node.
8946 -- If it is a compilation unit and the instance node has not been
8947 -- rewritten, then it is still the unit of the compilation. Finally, if
8948 -- a body is present, this is a parent of the main unit whose body has
8949 -- been compiled for inlining purposes, and the instantiation node has
8950 -- been rewritten with the instance body.
8952 -- Otherwise the instantiation node appears after the declaration. If
8953 -- the entity is a formal package, the declaration may have been
8954 -- rewritten as a generic declaration (in the case of a formal with box)
8955 -- or left as a formal package declaration if it has actuals, and is
8956 -- found with a forward search.
8958 if Nkind (Parent (Decl)) = N_Compilation_Unit then
8959 if Nkind (Decl) = N_Package_Declaration
8960 and then Present (Corresponding_Body (Decl))
8962 Decl := Unit_Declaration_Node (Corresponding_Body (Decl));
8965 if Nkind (Original_Node (Decl)) in N_Generic_Instantiation then
8966 return Original_Node (Decl);
8968 return Unit (Parent (Decl));
8971 elsif Nkind (Decl) = N_Package_Declaration
8972 and then Nkind (Original_Node (Decl)) = N_Formal_Package_Declaration
8974 return Original_Node (Decl);
8977 Inst := Next (Decl);
8978 while not Nkind_In (Inst, N_Formal_Package_Declaration,
8979 N_Function_Instantiation,
8980 N_Package_Instantiation,
8981 N_Procedure_Instantiation)
8988 end Get_Unit_Instantiation_Node;
8990 ------------------------
8991 -- Has_Been_Exchanged --
8992 ------------------------
8994 function Has_Been_Exchanged (E : Entity_Id) return Boolean is
8998 Next := First_Elmt (Exchanged_Views);
8999 while Present (Next) loop
9000 if Full_View (Node (Next)) = E then
9008 end Has_Been_Exchanged;
9014 function Hash (F : Entity_Id) return HTable_Range is
9016 return HTable_Range (F mod HTable_Size);
9019 ------------------------
9020 -- Hide_Current_Scope --
9021 ------------------------
9023 procedure Hide_Current_Scope is
9024 C : constant Entity_Id := Current_Scope;
9028 Set_Is_Hidden_Open_Scope (C);
9030 E := First_Entity (C);
9031 while Present (E) loop
9032 if Is_Immediately_Visible (E) then
9033 Set_Is_Immediately_Visible (E, False);
9034 Append_Elmt (E, Hidden_Entities);
9040 -- Make the scope name invisible as well. This is necessary, but might
9041 -- conflict with calls to Rtsfind later on, in case the scope is a
9042 -- predefined one. There is no clean solution to this problem, so for
9043 -- now we depend on the user not redefining Standard itself in one of
9044 -- the parent units.
9046 if Is_Immediately_Visible (C) and then C /= Standard_Standard then
9047 Set_Is_Immediately_Visible (C, False);
9048 Append_Elmt (C, Hidden_Entities);
9051 end Hide_Current_Scope;
9057 procedure Init_Env is
9058 Saved : Instance_Env;
9061 Saved.Instantiated_Parent := Current_Instantiated_Parent;
9062 Saved.Exchanged_Views := Exchanged_Views;
9063 Saved.Hidden_Entities := Hidden_Entities;
9064 Saved.Current_Sem_Unit := Current_Sem_Unit;
9065 Saved.Parent_Unit_Visible := Parent_Unit_Visible;
9066 Saved.Instance_Parent_Unit := Instance_Parent_Unit;
9068 -- Save configuration switches. These may be reset if the unit is a
9069 -- predefined unit, and the current mode is not Ada 2005.
9071 Saved.Switches := Save_Config_Switches;
9073 Instance_Envs.Append (Saved);
9075 Exchanged_Views := New_Elmt_List;
9076 Hidden_Entities := New_Elmt_List;
9078 -- Make dummy entry for Instantiated parent. If generic unit is legal,
9079 -- this is set properly in Set_Instance_Env.
9081 Current_Instantiated_Parent :=
9082 (Current_Scope, Current_Scope, Assoc_Null);
9085 ---------------------
9086 -- In_Main_Context --
9087 ---------------------
9089 function In_Main_Context (E : Entity_Id) return Boolean is
9095 if not Is_Compilation_Unit (E)
9096 or else Ekind (E) /= E_Package
9097 or else In_Private_Part (E)
9102 Context := Context_Items (Cunit (Main_Unit));
9104 Clause := First (Context);
9105 while Present (Clause) loop
9106 if Nkind (Clause) = N_With_Clause then
9107 Nam := Name (Clause);
9109 -- If the current scope is part of the context of the main unit,
9110 -- analysis of the corresponding with_clause is not complete, and
9111 -- the entity is not set. We use the Chars field directly, which
9112 -- might produce false positives in rare cases, but guarantees
9113 -- that we produce all the instance bodies we will need.
9115 if (Is_Entity_Name (Nam) and then Chars (Nam) = Chars (E))
9116 or else (Nkind (Nam) = N_Selected_Component
9117 and then Chars (Selector_Name (Nam)) = Chars (E))
9127 end In_Main_Context;
9129 ---------------------
9130 -- Inherit_Context --
9131 ---------------------
9133 procedure Inherit_Context (Gen_Decl : Node_Id; Inst : Node_Id) is
9134 Current_Context : List_Id;
9135 Current_Unit : Node_Id;
9144 if Nkind (Parent (Gen_Decl)) = N_Compilation_Unit then
9146 -- The inherited context is attached to the enclosing compilation
9147 -- unit. This is either the main unit, or the declaration for the
9148 -- main unit (in case the instantiation appears within the package
9149 -- declaration and the main unit is its body).
9151 Current_Unit := Parent (Inst);
9152 while Present (Current_Unit)
9153 and then Nkind (Current_Unit) /= N_Compilation_Unit
9155 Current_Unit := Parent (Current_Unit);
9158 Current_Context := Context_Items (Current_Unit);
9160 Item := First (Context_Items (Parent (Gen_Decl)));
9161 while Present (Item) loop
9162 if Nkind (Item) = N_With_Clause then
9163 Lib_Unit := Library_Unit (Item);
9165 -- Take care to prevent direct cyclic with's
9167 if Lib_Unit /= Current_Unit then
9169 -- Do not add a unit if it is already in the context
9171 Clause := First (Current_Context);
9173 while Present (Clause) loop
9174 if Nkind (Clause) = N_With_Clause
9175 and then Library_Unit (Clause) = Lib_Unit
9185 New_I := New_Copy (Item);
9186 Set_Implicit_With (New_I);
9188 Append (New_I, Current_Context);
9196 end Inherit_Context;
9202 procedure Initialize is
9204 Generic_Renamings.Init;
9207 Generic_Renamings_HTable.Reset;
9208 Circularity_Detected := False;
9209 Exchanged_Views := No_Elist;
9210 Hidden_Entities := No_Elist;
9213 -------------------------------------
9214 -- Insert_Freeze_Node_For_Instance --
9215 -------------------------------------
9217 procedure Insert_Freeze_Node_For_Instance
9226 function Enclosing_Body (N : Node_Id) return Node_Id;
9227 -- Find enclosing package or subprogram body, if any. Freeze node may
9228 -- be placed at end of current declarative list if previous instance
9229 -- and current one have different enclosing bodies.
9231 function Previous_Instance (Gen : Entity_Id) return Entity_Id;
9232 -- Find the local instance, if any, that declares the generic that is
9233 -- being instantiated. If present, the freeze node for this instance
9234 -- must follow the freeze node for the previous instance.
9236 --------------------
9237 -- Enclosing_Body --
9238 --------------------
9240 function Enclosing_Body (N : Node_Id) return Node_Id is
9246 and then Nkind (Parent (P)) /= N_Compilation_Unit
9248 if Nkind_In (P, N_Package_Body, N_Subprogram_Body) then
9249 if Nkind (Parent (P)) = N_Subunit then
9250 return Corresponding_Stub (Parent (P));
9256 P := True_Parent (P);
9262 -----------------------
9263 -- Previous_Instance --
9264 -----------------------
9266 function Previous_Instance (Gen : Entity_Id) return Entity_Id is
9271 while Present (S) and then S /= Standard_Standard loop
9272 if Is_Generic_Instance (S)
9273 and then In_Same_Source_Unit (S, N)
9282 end Previous_Instance;
9284 -- Start of processing for Insert_Freeze_Node_For_Instance
9287 if not Is_List_Member (F_Node) then
9289 Decls := List_Containing (N);
9290 Inst := Entity (F_Node);
9291 Par_N := Parent (Decls);
9293 -- When processing a subprogram instantiation, utilize the actual
9294 -- subprogram instantiation rather than its package wrapper as it
9295 -- carries all the context information.
9297 if Is_Wrapper_Package (Inst) then
9298 Inst := Related_Instance (Inst);
9301 -- If this is a package instance, check whether the generic is
9302 -- declared in a previous instance and the current instance is
9303 -- not within the previous one.
9305 if Present (Generic_Parent (Parent (Inst)))
9306 and then Is_In_Main_Unit (N)
9309 Enclosing_N : constant Node_Id := Enclosing_Body (N);
9310 Par_I : constant Entity_Id :=
9312 (Generic_Parent (Parent (Inst)));
9317 and then Earlier (N, Freeze_Node (Par_I))
9319 Scop := Scope (Inst);
9321 -- If the current instance is within the one that contains
9322 -- the generic, the freeze node for the current one must
9323 -- appear in the current declarative part. Ditto, if the
9324 -- current instance is within another package instance or
9325 -- within a body that does not enclose the current instance.
9326 -- In these three cases the freeze node of the previous
9327 -- instance is not relevant.
9329 while Present (Scop) and then Scop /= Standard_Standard loop
9330 exit when Scop = Par_I
9332 (Is_Generic_Instance (Scop)
9333 and then Scope_Depth (Scop) > Scope_Depth (Par_I));
9334 Scop := Scope (Scop);
9337 -- Previous instance encloses current instance
9339 if Scop = Par_I then
9342 -- If the next node is a source body we must freeze in
9343 -- the current scope as well.
9345 elsif Present (Next (N))
9346 and then Nkind_In (Next (N), N_Subprogram_Body,
9348 and then Comes_From_Source (Next (N))
9352 -- Current instance is within an unrelated instance
9354 elsif Is_Generic_Instance (Scop) then
9357 -- Current instance is within an unrelated body
9359 elsif Present (Enclosing_N)
9360 and then Enclosing_N /= Enclosing_Body (Par_I)
9365 Insert_After (Freeze_Node (Par_I), F_Node);
9372 -- When the instantiation occurs in a package declaration, append the
9373 -- freeze node to the private declarations (if any).
9375 if Nkind (Par_N) = N_Package_Specification
9376 and then Decls = Visible_Declarations (Par_N)
9377 and then Present (Private_Declarations (Par_N))
9378 and then not Is_Empty_List (Private_Declarations (Par_N))
9380 Decls := Private_Declarations (Par_N);
9381 Decl := First (Decls);
9384 -- Determine the proper freeze point of a package instantiation. We
9385 -- adhere to the general rule of a package or subprogram body causing
9386 -- freezing of anything before it in the same declarative region. In
9387 -- this case, the proper freeze point of a package instantiation is
9388 -- before the first source body which follows, or before a stub. This
9389 -- ensures that entities coming from the instance are already frozen
9390 -- and usable in source bodies.
9392 if Nkind (Par_N) /= N_Package_Declaration
9393 and then Ekind (Inst) = E_Package
9394 and then Is_Generic_Instance (Inst)
9396 not In_Same_Source_Unit (Generic_Parent (Parent (Inst)), Inst)
9398 while Present (Decl) loop
9399 if (Nkind (Decl) in N_Unit_Body
9401 Nkind (Decl) in N_Body_Stub)
9402 and then Comes_From_Source (Decl)
9404 Insert_Before (Decl, F_Node);
9412 -- In a package declaration, or if no previous body, insert at end
9415 Set_Sloc (F_Node, Sloc (Last (Decls)));
9416 Insert_After (Last (Decls), F_Node);
9418 end Insert_Freeze_Node_For_Instance;
9424 procedure Install_Body
9425 (Act_Body : Node_Id;
9430 function In_Same_Scope (Gen_Id, Act_Id : Node_Id) return Boolean;
9431 -- Check if the generic definition and the instantiation come from
9432 -- a common scope, in which case the instance must be frozen after
9433 -- the generic body.
9435 function True_Sloc (N, Act_Unit : Node_Id) return Source_Ptr;
9436 -- If the instance is nested inside a generic unit, the Sloc of the
9437 -- instance indicates the place of the original definition, not the
9438 -- point of the current enclosing instance. Pending a better usage of
9439 -- Slocs to indicate instantiation places, we determine the place of
9440 -- origin of a node by finding the maximum sloc of any ancestor node.
9441 -- Why is this not equivalent to Top_Level_Location ???
9447 function In_Same_Scope (Gen_Id, Act_Id : Node_Id) return Boolean is
9448 Act_Scop : Entity_Id := Scope (Act_Id);
9449 Gen_Scop : Entity_Id := Scope (Gen_Id);
9452 while Act_Scop /= Standard_Standard
9453 and then Gen_Scop /= Standard_Standard
9455 if Act_Scop = Gen_Scop then
9459 Act_Scop := Scope (Act_Scop);
9460 Gen_Scop := Scope (Gen_Scop);
9470 function True_Sloc (N, Act_Unit : Node_Id) return Source_Ptr is
9477 while Present (N1) and then N1 /= Act_Unit loop
9478 if Sloc (N1) > Res then
9488 Act_Id : constant Entity_Id := Corresponding_Spec (Act_Body);
9489 Act_Unit : constant Node_Id := Unit (Cunit (Get_Source_Unit (N)));
9490 Gen_Id : constant Entity_Id := Corresponding_Spec (Gen_Body);
9491 Par : constant Entity_Id := Scope (Gen_Id);
9492 Gen_Unit : constant Node_Id :=
9493 Unit (Cunit (Get_Source_Unit (Gen_Decl)));
9495 Body_Unit : Node_Id;
9497 Must_Delay : Boolean;
9498 Orig_Body : Node_Id := Gen_Body;
9500 -- Start of processing for Install_Body
9503 -- Handle first the case of an instance with incomplete actual types.
9504 -- The instance body cannot be placed after the declaration because
9505 -- full views have not been seen yet. Any use of the non-limited views
9506 -- in the instance body requires the presence of a regular with_clause
9507 -- in the enclosing unit, and will fail if this with_clause is missing.
9508 -- We place the instance body at the beginning of the enclosing body,
9509 -- which is the unit being compiled. The freeze node for the instance
9510 -- is then placed after the instance body.
9512 if not Is_Empty_Elmt_List (Incomplete_Actuals (Act_Id))
9513 and then Expander_Active
9514 and then Ekind (Scope (Act_Id)) = E_Package
9517 Scop : constant Entity_Id := Scope (Act_Id);
9518 Body_Id : constant Node_Id :=
9519 Corresponding_Body (Unit_Declaration_Node (Scop));
9522 Ensure_Freeze_Node (Act_Id);
9523 F_Node := Freeze_Node (Act_Id);
9524 if Present (Body_Id) then
9525 Set_Is_Frozen (Act_Id, False);
9526 Prepend (Act_Body, Declarations (Parent (Body_Id)));
9527 if Is_List_Member (F_Node) then
9531 Insert_After (Act_Body, F_Node);
9537 -- If the body is a subunit, the freeze point is the corresponding stub
9538 -- in the current compilation, not the subunit itself.
9540 if Nkind (Parent (Gen_Body)) = N_Subunit then
9541 Orig_Body := Corresponding_Stub (Parent (Gen_Body));
9543 Orig_Body := Gen_Body;
9546 Body_Unit := Unit (Cunit (Get_Source_Unit (Orig_Body)));
9548 -- If the instantiation and the generic definition appear in the same
9549 -- package declaration, this is an early instantiation. If they appear
9550 -- in the same declarative part, it is an early instantiation only if
9551 -- the generic body appears textually later, and the generic body is
9552 -- also in the main unit.
9554 -- If instance is nested within a subprogram, and the generic body
9555 -- is not, the instance is delayed because the enclosing body is. If
9556 -- instance and body are within the same scope, or the same subprogram
9557 -- body, indicate explicitly that the instance is delayed.
9560 (Gen_Unit = Act_Unit
9561 and then (Nkind_In (Gen_Unit, N_Generic_Package_Declaration,
9562 N_Package_Declaration)
9563 or else (Gen_Unit = Body_Unit
9564 and then True_Sloc (N, Act_Unit) <
9566 and then Is_In_Main_Unit (Original_Node (Gen_Unit))
9567 and then In_Same_Scope (Gen_Id, Act_Id));
9569 -- If this is an early instantiation, the freeze node is placed after
9570 -- the generic body. Otherwise, if the generic appears in an instance,
9571 -- we cannot freeze the current instance until the outer one is frozen.
9572 -- This is only relevant if the current instance is nested within some
9573 -- inner scope not itself within the outer instance. If this scope is
9574 -- a package body in the same declarative part as the outer instance,
9575 -- then that body needs to be frozen after the outer instance. Finally,
9576 -- if no delay is needed, we place the freeze node at the end of the
9577 -- current declarative part.
9580 and then (No (Freeze_Node (Act_Id))
9581 or else not Is_List_Member (Freeze_Node (Act_Id)))
9583 Ensure_Freeze_Node (Act_Id);
9584 F_Node := Freeze_Node (Act_Id);
9587 Insert_After (Orig_Body, F_Node);
9589 elsif Is_Generic_Instance (Par)
9590 and then Present (Freeze_Node (Par))
9591 and then Scope (Act_Id) /= Par
9593 -- Freeze instance of inner generic after instance of enclosing
9596 if In_Same_Declarative_Part (Parent (Freeze_Node (Par)), N) then
9598 -- Handle the following case:
9600 -- package Parent_Inst is new ...
9603 -- procedure P ... -- this body freezes Parent_Inst
9605 -- package Inst is new ...
9607 -- In this particular scenario, the freeze node for Inst must
9608 -- be inserted in the same manner as that of Parent_Inst,
9609 -- before the next source body or at the end of the declarative
9610 -- list (body not available). If body P did not exist and
9611 -- Parent_Inst was frozen after Inst, either by a body
9612 -- following Inst or at the end of the declarative region,
9613 -- the freeze node for Inst must be inserted after that of
9614 -- Parent_Inst. This relation is established by comparing
9615 -- the Slocs of Parent_Inst freeze node and Inst.
9616 -- We examine the parents of the enclosing lists to handle
9617 -- the case where the parent instance is in the visible part
9618 -- of a package declaration, and the inner instance is in
9619 -- the corresponding private part.
9621 if Parent (List_Containing (Get_Unit_Instantiation_Node (Par)))
9622 = Parent (List_Containing (N))
9623 and then Sloc (Freeze_Node (Par)) < Sloc (N)
9625 Insert_Freeze_Node_For_Instance (N, F_Node);
9627 Insert_After (Freeze_Node (Par), F_Node);
9630 -- Freeze package enclosing instance of inner generic after
9631 -- instance of enclosing generic.
9633 elsif Nkind_In (Parent (N), N_Package_Body, N_Subprogram_Body)
9634 and then In_Same_Declarative_Part
9635 (Parent (Freeze_Node (Par)), Parent (N))
9638 Enclosing : Entity_Id;
9641 Enclosing := Corresponding_Spec (Parent (N));
9643 if No (Enclosing) then
9644 Enclosing := Defining_Entity (Parent (N));
9647 Insert_Freeze_Node_For_Instance (N, F_Node);
9648 Ensure_Freeze_Node (Enclosing);
9650 if not Is_List_Member (Freeze_Node (Enclosing)) then
9652 -- The enclosing context is a subunit, insert the freeze
9653 -- node after the stub.
9655 if Nkind (Parent (Parent (N))) = N_Subunit then
9656 Insert_Freeze_Node_For_Instance
9657 (Corresponding_Stub (Parent (Parent (N))),
9658 Freeze_Node (Enclosing));
9660 -- The enclosing context is a package with a stub body
9661 -- which has already been replaced by the real body.
9662 -- Insert the freeze node after the actual body.
9664 elsif Ekind (Enclosing) = E_Package
9665 and then Present (Body_Entity (Enclosing))
9666 and then Was_Originally_Stub
9667 (Parent (Body_Entity (Enclosing)))
9669 Insert_Freeze_Node_For_Instance
9670 (Parent (Body_Entity (Enclosing)),
9671 Freeze_Node (Enclosing));
9673 -- The parent instance has been frozen before the body of
9674 -- the enclosing package, insert the freeze node after
9677 elsif List_Containing (Freeze_Node (Par)) =
9678 List_Containing (Parent (N))
9679 and then Sloc (Freeze_Node (Par)) < Sloc (Parent (N))
9681 Insert_Freeze_Node_For_Instance
9682 (Parent (N), Freeze_Node (Enclosing));
9686 (Freeze_Node (Par), Freeze_Node (Enclosing));
9692 Insert_Freeze_Node_For_Instance (N, F_Node);
9696 Insert_Freeze_Node_For_Instance (N, F_Node);
9700 Set_Is_Frozen (Act_Id);
9701 Insert_Before (N, Act_Body);
9702 Mark_Rewrite_Insertion (Act_Body);
9705 -----------------------------
9706 -- Install_Formal_Packages --
9707 -----------------------------
9709 procedure Install_Formal_Packages (Par : Entity_Id) is
9712 Gen_E : Entity_Id := Empty;
9715 E := First_Entity (Par);
9717 -- If we are installing an instance parent, locate the formal packages
9718 -- of its generic parent.
9720 if Is_Generic_Instance (Par) then
9721 Gen := Generic_Parent (Package_Specification (Par));
9722 Gen_E := First_Entity (Gen);
9725 while Present (E) loop
9726 if Ekind (E) = E_Package
9727 and then Nkind (Parent (E)) = N_Package_Renaming_Declaration
9729 -- If this is the renaming for the parent instance, done
9731 if Renamed_Object (E) = Par then
9734 -- The visibility of a formal of an enclosing generic is already
9737 elsif Denotes_Formal_Package (E) then
9740 elsif Present (Associated_Formal_Package (E)) then
9741 Check_Generic_Actuals (Renamed_Object (E), True);
9742 Set_Is_Hidden (E, False);
9744 -- Find formal package in generic unit that corresponds to
9745 -- (instance of) formal package in instance.
9747 while Present (Gen_E) and then Chars (Gen_E) /= Chars (E) loop
9748 Next_Entity (Gen_E);
9751 if Present (Gen_E) then
9752 Map_Formal_Package_Entities (Gen_E, E);
9759 if Present (Gen_E) then
9760 Next_Entity (Gen_E);
9763 end Install_Formal_Packages;
9765 --------------------
9766 -- Install_Parent --
9767 --------------------
9769 procedure Install_Parent (P : Entity_Id; In_Body : Boolean := False) is
9770 Ancestors : constant Elist_Id := New_Elmt_List;
9771 S : constant Entity_Id := Current_Scope;
9772 Inst_Par : Entity_Id;
9773 First_Par : Entity_Id;
9774 Inst_Node : Node_Id;
9775 Gen_Par : Entity_Id;
9776 First_Gen : Entity_Id;
9779 procedure Install_Noninstance_Specs (Par : Entity_Id);
9780 -- Install the scopes of noninstance parent units ending with Par
9782 procedure Install_Spec (Par : Entity_Id);
9783 -- The child unit is within the declarative part of the parent, so the
9784 -- declarations within the parent are immediately visible.
9786 -------------------------------
9787 -- Install_Noninstance_Specs --
9788 -------------------------------
9790 procedure Install_Noninstance_Specs (Par : Entity_Id) is
9793 and then Par /= Standard_Standard
9794 and then not In_Open_Scopes (Par)
9796 Install_Noninstance_Specs (Scope (Par));
9799 end Install_Noninstance_Specs;
9805 procedure Install_Spec (Par : Entity_Id) is
9806 Spec : constant Node_Id := Package_Specification (Par);
9809 -- If this parent of the child instance is a top-level unit,
9810 -- then record the unit and its visibility for later resetting in
9811 -- Remove_Parent. We exclude units that are generic instances, as we
9812 -- only want to record this information for the ultimate top-level
9813 -- noninstance parent (is that always correct???).
9815 if Scope (Par) = Standard_Standard
9816 and then not Is_Generic_Instance (Par)
9818 Parent_Unit_Visible := Is_Immediately_Visible (Par);
9819 Instance_Parent_Unit := Par;
9822 -- Open the parent scope and make it and its declarations visible.
9823 -- If this point is not within a body, then only the visible
9824 -- declarations should be made visible, and installation of the
9825 -- private declarations is deferred until the appropriate point
9826 -- within analysis of the spec being instantiated (see the handling
9827 -- of parent visibility in Analyze_Package_Specification). This is
9828 -- relaxed in the case where the parent unit is Ada.Tags, to avoid
9829 -- private view problems that occur when compiling instantiations of
9830 -- a generic child of that package (Generic_Dispatching_Constructor).
9831 -- If the instance freezes a tagged type, inlinings of operations
9832 -- from Ada.Tags may need the full view of type Tag. If inlining took
9833 -- proper account of establishing visibility of inlined subprograms'
9834 -- parents then it should be possible to remove this
9835 -- special check. ???
9838 Set_Is_Immediately_Visible (Par);
9839 Install_Visible_Declarations (Par);
9840 Set_Use (Visible_Declarations (Spec));
9842 if In_Body or else Is_RTU (Par, Ada_Tags) then
9843 Install_Private_Declarations (Par);
9844 Set_Use (Private_Declarations (Spec));
9848 -- Start of processing for Install_Parent
9851 -- We need to install the parent instance to compile the instantiation
9852 -- of the child, but the child instance must appear in the current
9853 -- scope. Given that we cannot place the parent above the current scope
9854 -- in the scope stack, we duplicate the current scope and unstack both
9855 -- after the instantiation is complete.
9857 -- If the parent is itself the instantiation of a child unit, we must
9858 -- also stack the instantiation of its parent, and so on. Each such
9859 -- ancestor is the prefix of the name in a prior instantiation.
9861 -- If this is a nested instance, the parent unit itself resolves to
9862 -- a renaming of the parent instance, whose declaration we need.
9864 -- Finally, the parent may be a generic (not an instance) when the
9865 -- child unit appears as a formal package.
9869 if Present (Renamed_Entity (Inst_Par)) then
9870 Inst_Par := Renamed_Entity (Inst_Par);
9873 First_Par := Inst_Par;
9875 Gen_Par := Generic_Parent (Package_Specification (Inst_Par));
9877 First_Gen := Gen_Par;
9879 while Present (Gen_Par) and then Is_Child_Unit (Gen_Par) loop
9881 -- Load grandparent instance as well
9883 Inst_Node := Get_Unit_Instantiation_Node (Inst_Par);
9885 if Nkind (Name (Inst_Node)) = N_Expanded_Name then
9886 Inst_Par := Entity (Prefix (Name (Inst_Node)));
9888 if Present (Renamed_Entity (Inst_Par)) then
9889 Inst_Par := Renamed_Entity (Inst_Par);
9892 Gen_Par := Generic_Parent (Package_Specification (Inst_Par));
9894 if Present (Gen_Par) then
9895 Prepend_Elmt (Inst_Par, Ancestors);
9898 -- Parent is not the name of an instantiation
9900 Install_Noninstance_Specs (Inst_Par);
9911 if Present (First_Gen) then
9912 Append_Elmt (First_Par, Ancestors);
9914 Install_Noninstance_Specs (First_Par);
9917 if not Is_Empty_Elmt_List (Ancestors) then
9918 Elmt := First_Elmt (Ancestors);
9919 while Present (Elmt) loop
9920 Install_Spec (Node (Elmt));
9921 Install_Formal_Packages (Node (Elmt));
9931 -------------------------------
9932 -- Install_Hidden_Primitives --
9933 -------------------------------
9935 procedure Install_Hidden_Primitives
9936 (Prims_List : in out Elist_Id;
9941 List : Elist_Id := No_Elist;
9942 Prim_G_Elmt : Elmt_Id;
9943 Prim_A_Elmt : Elmt_Id;
9948 -- No action needed in case of serious errors because we cannot trust
9949 -- in the order of primitives
9951 if Serious_Errors_Detected > 0 then
9954 -- No action possible if we don't have available the list of primitive
9958 or else not Is_Record_Type (Gen_T)
9959 or else not Is_Tagged_Type (Gen_T)
9960 or else not Is_Record_Type (Act_T)
9961 or else not Is_Tagged_Type (Act_T)
9965 -- There is no need to handle interface types since their primitives
9968 elsif Is_Interface (Gen_T) then
9972 Prim_G_Elmt := First_Elmt (Primitive_Operations (Gen_T));
9974 if not Is_Class_Wide_Type (Act_T) then
9975 Prim_A_Elmt := First_Elmt (Primitive_Operations (Act_T));
9977 Prim_A_Elmt := First_Elmt (Primitive_Operations (Root_Type (Act_T)));
9981 -- Skip predefined primitives in the generic formal
9983 while Present (Prim_G_Elmt)
9984 and then Is_Predefined_Dispatching_Operation (Node (Prim_G_Elmt))
9986 Next_Elmt (Prim_G_Elmt);
9989 -- Skip predefined primitives in the generic actual
9991 while Present (Prim_A_Elmt)
9992 and then Is_Predefined_Dispatching_Operation (Node (Prim_A_Elmt))
9994 Next_Elmt (Prim_A_Elmt);
9997 exit when No (Prim_G_Elmt) or else No (Prim_A_Elmt);
9999 Prim_G := Node (Prim_G_Elmt);
10000 Prim_A := Node (Prim_A_Elmt);
10002 -- There is no need to handle interface primitives because their
10003 -- primitives are not hidden
10005 exit when Present (Interface_Alias (Prim_G));
10007 -- Here we install one hidden primitive
10009 if Chars (Prim_G) /= Chars (Prim_A)
10010 and then Has_Suffix (Prim_A, 'P')
10011 and then Remove_Suffix (Prim_A, 'P') = Chars (Prim_G)
10013 Set_Chars (Prim_A, Chars (Prim_G));
10014 Append_New_Elmt (Prim_A, To => List);
10017 Next_Elmt (Prim_A_Elmt);
10018 Next_Elmt (Prim_G_Elmt);
10021 -- Append the elements to the list of temporarily visible primitives
10022 -- avoiding duplicates.
10024 if Present (List) then
10025 if No (Prims_List) then
10026 Prims_List := New_Elmt_List;
10029 Elmt := First_Elmt (List);
10030 while Present (Elmt) loop
10031 Append_Unique_Elmt (Node (Elmt), Prims_List);
10035 end Install_Hidden_Primitives;
10037 -------------------------------
10038 -- Restore_Hidden_Primitives --
10039 -------------------------------
10041 procedure Restore_Hidden_Primitives (Prims_List : in out Elist_Id) is
10042 Prim_Elmt : Elmt_Id;
10046 if Prims_List /= No_Elist then
10047 Prim_Elmt := First_Elmt (Prims_List);
10048 while Present (Prim_Elmt) loop
10049 Prim := Node (Prim_Elmt);
10050 Set_Chars (Prim, Add_Suffix (Prim, 'P'));
10051 Next_Elmt (Prim_Elmt);
10054 Prims_List := No_Elist;
10056 end Restore_Hidden_Primitives;
10058 --------------------------------
10059 -- Instantiate_Formal_Package --
10060 --------------------------------
10062 function Instantiate_Formal_Package
10065 Analyzed_Formal : Node_Id) return List_Id
10067 Loc : constant Source_Ptr := Sloc (Actual);
10068 Hidden_Formals : constant Elist_Id := New_Elmt_List;
10069 Actual_Pack : Entity_Id;
10070 Formal_Pack : Entity_Id;
10071 Gen_Parent : Entity_Id;
10074 Parent_Spec : Node_Id;
10076 procedure Find_Matching_Actual
10078 Act : in out Entity_Id);
10079 -- We need to associate each formal entity in the formal package with
10080 -- the corresponding entity in the actual package. The actual package
10081 -- has been analyzed and possibly expanded, and as a result there is
10082 -- no one-to-one correspondence between the two lists (for example,
10083 -- the actual may include subtypes, itypes, and inherited primitive
10084 -- operations, interspersed among the renaming declarations for the
10085 -- actuals). We retrieve the corresponding actual by name because each
10086 -- actual has the same name as the formal, and they do appear in the
10089 function Get_Formal_Entity (N : Node_Id) return Entity_Id;
10090 -- Retrieve entity of defining entity of generic formal parameter.
10091 -- Only the declarations of formals need to be considered when
10092 -- linking them to actuals, but the declarative list may include
10093 -- internal entities generated during analysis, and those are ignored.
10095 procedure Match_Formal_Entity
10096 (Formal_Node : Node_Id;
10097 Formal_Ent : Entity_Id;
10098 Actual_Ent : Entity_Id);
10099 -- Associates the formal entity with the actual. In the case where
10100 -- Formal_Ent is a formal package, this procedure iterates through all
10101 -- of its formals and enters associations between the actuals occurring
10102 -- in the formal package's corresponding actual package (given by
10103 -- Actual_Ent) and the formal package's formal parameters. This
10104 -- procedure recurses if any of the parameters is itself a package.
10106 function Is_Instance_Of
10107 (Act_Spec : Entity_Id;
10108 Gen_Anc : Entity_Id) return Boolean;
10109 -- The actual can be an instantiation of a generic within another
10110 -- instance, in which case there is no direct link from it to the
10111 -- original generic ancestor. In that case, we recognize that the
10112 -- ultimate ancestor is the same by examining names and scopes.
10114 procedure Process_Nested_Formal (Formal : Entity_Id);
10115 -- If the current formal is declared with a box, its own formals are
10116 -- visible in the instance, as they were in the generic, and their
10117 -- Hidden flag must be reset. If some of these formals are themselves
10118 -- packages declared with a box, the processing must be recursive.
10120 --------------------------
10121 -- Find_Matching_Actual --
10122 --------------------------
10124 procedure Find_Matching_Actual
10126 Act : in out Entity_Id)
10128 Formal_Ent : Entity_Id;
10131 case Nkind (Original_Node (F)) is
10132 when N_Formal_Object_Declaration
10133 | N_Formal_Type_Declaration
10135 Formal_Ent := Defining_Identifier (F);
10137 while Chars (Act) /= Chars (Formal_Ent) loop
10141 when N_Formal_Package_Declaration
10142 | N_Formal_Subprogram_Declaration
10143 | N_Generic_Package_Declaration
10144 | N_Package_Declaration
10146 Formal_Ent := Defining_Entity (F);
10148 while Chars (Act) /= Chars (Formal_Ent) loop
10153 raise Program_Error;
10155 end Find_Matching_Actual;
10157 -------------------------
10158 -- Match_Formal_Entity --
10159 -------------------------
10161 procedure Match_Formal_Entity
10162 (Formal_Node : Node_Id;
10163 Formal_Ent : Entity_Id;
10164 Actual_Ent : Entity_Id)
10166 Act_Pkg : Entity_Id;
10169 Set_Instance_Of (Formal_Ent, Actual_Ent);
10171 if Ekind (Actual_Ent) = E_Package then
10173 -- Record associations for each parameter
10175 Act_Pkg := Actual_Ent;
10178 A_Ent : Entity_Id := First_Entity (Act_Pkg);
10182 Gen_Decl : Node_Id;
10184 Actual : Entity_Id;
10187 -- Retrieve the actual given in the formal package declaration
10189 Actual := Entity (Name (Original_Node (Formal_Node)));
10191 -- The actual in the formal package declaration may be a
10192 -- renamed generic package, in which case we want to retrieve
10193 -- the original generic in order to traverse its formal part.
10195 if Present (Renamed_Entity (Actual)) then
10196 Gen_Decl := Unit_Declaration_Node (Renamed_Entity (Actual));
10198 Gen_Decl := Unit_Declaration_Node (Actual);
10201 Formals := Generic_Formal_Declarations (Gen_Decl);
10203 if Present (Formals) then
10204 F_Node := First_Non_Pragma (Formals);
10209 while Present (A_Ent)
10210 and then Present (F_Node)
10211 and then A_Ent /= First_Private_Entity (Act_Pkg)
10213 F_Ent := Get_Formal_Entity (F_Node);
10215 if Present (F_Ent) then
10217 -- This is a formal of the original package. Record
10218 -- association and recurse.
10220 Find_Matching_Actual (F_Node, A_Ent);
10221 Match_Formal_Entity (F_Node, F_Ent, A_Ent);
10222 Next_Entity (A_Ent);
10225 Next_Non_Pragma (F_Node);
10229 end Match_Formal_Entity;
10231 -----------------------
10232 -- Get_Formal_Entity --
10233 -----------------------
10235 function Get_Formal_Entity (N : Node_Id) return Entity_Id is
10236 Kind : constant Node_Kind := Nkind (Original_Node (N));
10239 when N_Formal_Object_Declaration =>
10240 return Defining_Identifier (N);
10242 when N_Formal_Type_Declaration =>
10243 return Defining_Identifier (N);
10245 when N_Formal_Subprogram_Declaration =>
10246 return Defining_Unit_Name (Specification (N));
10248 when N_Formal_Package_Declaration =>
10249 return Defining_Identifier (Original_Node (N));
10251 when N_Generic_Package_Declaration =>
10252 return Defining_Identifier (Original_Node (N));
10254 -- All other declarations are introduced by semantic analysis and
10255 -- have no match in the actual.
10260 end Get_Formal_Entity;
10262 --------------------
10263 -- Is_Instance_Of --
10264 --------------------
10266 function Is_Instance_Of
10267 (Act_Spec : Entity_Id;
10268 Gen_Anc : Entity_Id) return Boolean
10270 Gen_Par : constant Entity_Id := Generic_Parent (Act_Spec);
10273 if No (Gen_Par) then
10276 -- Simplest case: the generic parent of the actual is the formal
10278 elsif Gen_Par = Gen_Anc then
10281 elsif Chars (Gen_Par) /= Chars (Gen_Anc) then
10284 -- The actual may be obtained through several instantiations. Its
10285 -- scope must itself be an instance of a generic declared in the
10286 -- same scope as the formal. Any other case is detected above.
10288 elsif not Is_Generic_Instance (Scope (Gen_Par)) then
10292 return Generic_Parent (Parent (Scope (Gen_Par))) = Scope (Gen_Anc);
10294 end Is_Instance_Of;
10296 ---------------------------
10297 -- Process_Nested_Formal --
10298 ---------------------------
10300 procedure Process_Nested_Formal (Formal : Entity_Id) is
10304 if Present (Associated_Formal_Package (Formal))
10305 and then Box_Present (Parent (Associated_Formal_Package (Formal)))
10307 Ent := First_Entity (Formal);
10308 while Present (Ent) loop
10309 Set_Is_Hidden (Ent, False);
10310 Set_Is_Visible_Formal (Ent);
10311 Set_Is_Potentially_Use_Visible
10312 (Ent, Is_Potentially_Use_Visible (Formal));
10314 if Ekind (Ent) = E_Package then
10315 exit when Renamed_Entity (Ent) = Renamed_Entity (Formal);
10316 Process_Nested_Formal (Ent);
10322 end Process_Nested_Formal;
10324 -- Start of processing for Instantiate_Formal_Package
10329 -- The actual must be a package instance, or else a current instance
10330 -- such as a parent generic within the body of a generic child.
10332 if not Is_Entity_Name (Actual)
10333 or else not Is_Package_Or_Generic_Package (Entity (Actual))
10336 ("expect package instance to instantiate formal", Actual);
10337 Abandon_Instantiation (Actual);
10338 raise Program_Error;
10341 Actual_Pack := Entity (Actual);
10342 Set_Is_Instantiated (Actual_Pack);
10344 -- The actual may be a renamed package, or an outer generic formal
10345 -- package whose instantiation is converted into a renaming.
10347 if Present (Renamed_Object (Actual_Pack)) then
10348 Actual_Pack := Renamed_Object (Actual_Pack);
10351 if Nkind (Analyzed_Formal) = N_Formal_Package_Declaration then
10352 Gen_Parent := Get_Instance_Of (Entity (Name (Analyzed_Formal)));
10353 Formal_Pack := Defining_Identifier (Analyzed_Formal);
10356 Generic_Parent (Specification (Analyzed_Formal));
10358 Defining_Unit_Name (Specification (Analyzed_Formal));
10361 if Nkind (Parent (Actual_Pack)) = N_Defining_Program_Unit_Name then
10362 Parent_Spec := Package_Specification (Actual_Pack);
10364 Parent_Spec := Parent (Actual_Pack);
10367 if Gen_Parent = Any_Id then
10369 ("previous error in declaration of formal package", Actual);
10370 Abandon_Instantiation (Actual);
10372 elsif Is_Instance_Of (Parent_Spec, Get_Instance_Of (Gen_Parent)) then
10375 -- If this is the current instance of an enclosing generic, that unit
10376 -- is the generic package we need.
10378 elsif In_Open_Scopes (Actual_Pack)
10379 and then Ekind (Actual_Pack) = E_Generic_Package
10385 ("actual parameter must be instance of&", Actual, Gen_Parent);
10386 Abandon_Instantiation (Actual);
10389 Set_Instance_Of (Defining_Identifier (Formal), Actual_Pack);
10390 Map_Formal_Package_Entities (Formal_Pack, Actual_Pack);
10393 Make_Package_Renaming_Declaration (Loc,
10394 Defining_Unit_Name => New_Copy (Defining_Identifier (Formal)),
10395 Name => New_Occurrence_Of (Actual_Pack, Loc));
10397 Set_Associated_Formal_Package
10398 (Defining_Unit_Name (Nod), Defining_Identifier (Formal));
10399 Decls := New_List (Nod);
10401 -- If the formal F has a box, then the generic declarations are
10402 -- visible in the generic G. In an instance of G, the corresponding
10403 -- entities in the actual for F (which are the actuals for the
10404 -- instantiation of the generic that F denotes) must also be made
10405 -- visible for analysis of the current instance. On exit from the
10406 -- current instance, those entities are made private again. If the
10407 -- actual is currently in use, these entities are also use-visible.
10409 -- The loop through the actual entities also steps through the formal
10410 -- entities and enters associations from formals to actuals into the
10411 -- renaming map. This is necessary to properly handle checking of
10412 -- actual parameter associations for later formals that depend on
10413 -- actuals declared in the formal package.
10415 -- In Ada 2005, partial parameterization requires that we make
10416 -- visible the actuals corresponding to formals that were defaulted
10417 -- in the formal package. There formals are identified because they
10418 -- remain formal generics within the formal package, rather than
10419 -- being renamings of the actuals supplied.
10422 Gen_Decl : constant Node_Id :=
10423 Unit_Declaration_Node (Gen_Parent);
10424 Formals : constant List_Id :=
10425 Generic_Formal_Declarations (Gen_Decl);
10427 Actual_Ent : Entity_Id;
10428 Actual_Of_Formal : Node_Id;
10429 Formal_Node : Node_Id;
10430 Formal_Ent : Entity_Id;
10433 if Present (Formals) then
10434 Formal_Node := First_Non_Pragma (Formals);
10436 Formal_Node := Empty;
10439 Actual_Ent := First_Entity (Actual_Pack);
10440 Actual_Of_Formal :=
10441 First (Visible_Declarations (Specification (Analyzed_Formal)));
10442 while Present (Actual_Ent)
10443 and then Actual_Ent /= First_Private_Entity (Actual_Pack)
10445 if Present (Formal_Node) then
10446 Formal_Ent := Get_Formal_Entity (Formal_Node);
10448 if Present (Formal_Ent) then
10449 Find_Matching_Actual (Formal_Node, Actual_Ent);
10450 Match_Formal_Entity (Formal_Node, Formal_Ent, Actual_Ent);
10452 -- We iterate at the same time over the actuals of the
10453 -- local package created for the formal, to determine
10454 -- which one of the formals of the original generic were
10455 -- defaulted in the formal. The corresponding actual
10456 -- entities are visible in the enclosing instance.
10458 if Box_Present (Formal)
10460 (Present (Actual_Of_Formal)
10463 (Get_Formal_Entity (Actual_Of_Formal)))
10465 Set_Is_Hidden (Actual_Ent, False);
10466 Set_Is_Visible_Formal (Actual_Ent);
10467 Set_Is_Potentially_Use_Visible
10468 (Actual_Ent, In_Use (Actual_Pack));
10470 if Ekind (Actual_Ent) = E_Package then
10471 Process_Nested_Formal (Actual_Ent);
10475 if not Is_Hidden (Actual_Ent) then
10476 Append_Elmt (Actual_Ent, Hidden_Formals);
10479 Set_Is_Hidden (Actual_Ent);
10480 Set_Is_Potentially_Use_Visible (Actual_Ent, False);
10484 Next_Non_Pragma (Formal_Node);
10485 Next (Actual_Of_Formal);
10488 -- No further formals to match, but the generic part may
10489 -- contain inherited operation that are not hidden in the
10490 -- enclosing instance.
10492 Next_Entity (Actual_Ent);
10496 -- Inherited subprograms generated by formal derived types are
10497 -- also visible if the types are.
10499 Actual_Ent := First_Entity (Actual_Pack);
10500 while Present (Actual_Ent)
10501 and then Actual_Ent /= First_Private_Entity (Actual_Pack)
10503 if Is_Overloadable (Actual_Ent)
10505 Nkind (Parent (Actual_Ent)) = N_Subtype_Declaration
10507 not Is_Hidden (Defining_Identifier (Parent (Actual_Ent)))
10509 Set_Is_Hidden (Actual_Ent, False);
10510 Set_Is_Potentially_Use_Visible
10511 (Actual_Ent, In_Use (Actual_Pack));
10514 Next_Entity (Actual_Ent);
10517 -- No conformance to check if the generic has no formal parameters
10518 -- and the formal package has no generic associations.
10520 if Is_Empty_List (Formals)
10522 (Box_Present (Formal)
10523 or else No (Generic_Associations (Formal)))
10529 -- If the formal is not declared with a box, reanalyze it as an
10530 -- abbreviated instantiation, to verify the matching rules of 12.7.
10531 -- The actual checks are performed after the generic associations
10532 -- have been analyzed, to guarantee the same visibility for this
10533 -- instantiation and for the actuals.
10535 -- In Ada 2005, the generic associations for the formal can include
10536 -- defaulted parameters. These are ignored during check. This
10537 -- internal instantiation is removed from the tree after conformance
10538 -- checking, because it contains formal declarations for those
10539 -- defaulted parameters, and those should not reach the back-end.
10541 if not Box_Present (Formal) then
10543 I_Pack : constant Entity_Id :=
10544 Make_Temporary (Sloc (Actual), 'P');
10547 Set_Is_Internal (I_Pack);
10548 Set_Ekind (I_Pack, E_Package);
10549 Set_Hidden_In_Formal_Instance (I_Pack, Hidden_Formals);
10552 Make_Package_Instantiation (Sloc (Actual),
10553 Defining_Unit_Name => I_Pack,
10556 (Get_Instance_Of (Gen_Parent), Sloc (Actual)),
10557 Generic_Associations => Generic_Associations (Formal)));
10563 end Instantiate_Formal_Package;
10565 -----------------------------------
10566 -- Instantiate_Formal_Subprogram --
10567 -----------------------------------
10569 function Instantiate_Formal_Subprogram
10572 Analyzed_Formal : Node_Id) return Node_Id
10574 Analyzed_S : constant Entity_Id :=
10575 Defining_Unit_Name (Specification (Analyzed_Formal));
10576 Formal_Sub : constant Entity_Id :=
10577 Defining_Unit_Name (Specification (Formal));
10579 function From_Parent_Scope (Subp : Entity_Id) return Boolean;
10580 -- If the generic is a child unit, the parent has been installed on the
10581 -- scope stack, but a default subprogram cannot resolve to something
10582 -- on the parent because that parent is not really part of the visible
10583 -- context (it is there to resolve explicit local entities). If the
10584 -- default has resolved in this way, we remove the entity from immediate
10585 -- visibility and analyze the node again to emit an error message or
10586 -- find another visible candidate.
10588 procedure Valid_Actual_Subprogram (Act : Node_Id);
10589 -- Perform legality check and raise exception on failure
10591 -----------------------
10592 -- From_Parent_Scope --
10593 -----------------------
10595 function From_Parent_Scope (Subp : Entity_Id) return Boolean is
10596 Gen_Scope : Node_Id;
10599 Gen_Scope := Scope (Analyzed_S);
10600 while Present (Gen_Scope) and then Is_Child_Unit (Gen_Scope) loop
10601 if Scope (Subp) = Scope (Gen_Scope) then
10605 Gen_Scope := Scope (Gen_Scope);
10609 end From_Parent_Scope;
10611 -----------------------------
10612 -- Valid_Actual_Subprogram --
10613 -----------------------------
10615 procedure Valid_Actual_Subprogram (Act : Node_Id) is
10619 if Is_Entity_Name (Act) then
10620 Act_E := Entity (Act);
10622 elsif Nkind (Act) = N_Selected_Component
10623 and then Is_Entity_Name (Selector_Name (Act))
10625 Act_E := Entity (Selector_Name (Act));
10631 if (Present (Act_E) and then Is_Overloadable (Act_E))
10632 or else Nkind_In (Act, N_Attribute_Reference,
10633 N_Indexed_Component,
10634 N_Character_Literal,
10635 N_Explicit_Dereference)
10641 ("expect subprogram or entry name in instantiation of &",
10642 Instantiation_Node, Formal_Sub);
10643 Abandon_Instantiation (Instantiation_Node);
10644 end Valid_Actual_Subprogram;
10648 Decl_Node : Node_Id;
10651 New_Spec : Node_Id;
10652 New_Subp : Entity_Id;
10654 -- Start of processing for Instantiate_Formal_Subprogram
10657 New_Spec := New_Copy_Tree (Specification (Formal));
10659 -- The tree copy has created the proper instantiation sloc for the
10660 -- new specification. Use this location for all other constructed
10663 Loc := Sloc (Defining_Unit_Name (New_Spec));
10665 -- Create new entity for the actual (New_Copy_Tree does not), and
10666 -- indicate that it is an actual.
10668 New_Subp := Make_Defining_Identifier (Loc, Chars (Formal_Sub));
10669 Set_Ekind (New_Subp, Ekind (Analyzed_S));
10670 Set_Is_Generic_Actual_Subprogram (New_Subp);
10671 Set_Defining_Unit_Name (New_Spec, New_Subp);
10673 -- Create new entities for the each of the formals in the specification
10674 -- of the renaming declaration built for the actual.
10676 if Present (Parameter_Specifications (New_Spec)) then
10682 F := First (Parameter_Specifications (New_Spec));
10683 while Present (F) loop
10684 F_Id := Defining_Identifier (F);
10686 Set_Defining_Identifier (F,
10687 Make_Defining_Identifier (Sloc (F_Id), Chars (F_Id)));
10693 -- Find entity of actual. If the actual is an attribute reference, it
10694 -- cannot be resolved here (its formal is missing) but is handled
10695 -- instead in Attribute_Renaming. If the actual is overloaded, it is
10696 -- fully resolved subsequently, when the renaming declaration for the
10697 -- formal is analyzed. If it is an explicit dereference, resolve the
10698 -- prefix but not the actual itself, to prevent interpretation as call.
10700 if Present (Actual) then
10701 Loc := Sloc (Actual);
10702 Set_Sloc (New_Spec, Loc);
10704 if Nkind (Actual) = N_Operator_Symbol then
10705 Find_Direct_Name (Actual);
10707 elsif Nkind (Actual) = N_Explicit_Dereference then
10708 Analyze (Prefix (Actual));
10710 elsif Nkind (Actual) /= N_Attribute_Reference then
10714 Valid_Actual_Subprogram (Actual);
10717 elsif Present (Default_Name (Formal)) then
10718 if not Nkind_In (Default_Name (Formal), N_Attribute_Reference,
10719 N_Selected_Component,
10720 N_Indexed_Component,
10721 N_Character_Literal)
10722 and then Present (Entity (Default_Name (Formal)))
10724 Nam := New_Occurrence_Of (Entity (Default_Name (Formal)), Loc);
10726 Nam := New_Copy (Default_Name (Formal));
10727 Set_Sloc (Nam, Loc);
10730 elsif Box_Present (Formal) then
10732 -- Actual is resolved at the point of instantiation. Create an
10733 -- identifier or operator with the same name as the formal.
10735 if Nkind (Formal_Sub) = N_Defining_Operator_Symbol then
10737 Make_Operator_Symbol (Loc,
10738 Chars => Chars (Formal_Sub),
10739 Strval => No_String);
10741 Nam := Make_Identifier (Loc, Chars (Formal_Sub));
10744 elsif Nkind (Specification (Formal)) = N_Procedure_Specification
10745 and then Null_Present (Specification (Formal))
10747 -- Generate null body for procedure, for use in the instance
10750 Make_Subprogram_Body (Loc,
10751 Specification => New_Spec,
10752 Declarations => New_List,
10753 Handled_Statement_Sequence =>
10754 Make_Handled_Sequence_Of_Statements (Loc,
10755 Statements => New_List (Make_Null_Statement (Loc))));
10757 Set_Is_Intrinsic_Subprogram (Defining_Unit_Name (New_Spec));
10761 Error_Msg_Sloc := Sloc (Scope (Analyzed_S));
10763 ("missing actual&", Instantiation_Node, Formal_Sub);
10765 ("\in instantiation of & declared#",
10766 Instantiation_Node, Scope (Analyzed_S));
10767 Abandon_Instantiation (Instantiation_Node);
10771 Make_Subprogram_Renaming_Declaration (Loc,
10772 Specification => New_Spec,
10775 -- If we do not have an actual and the formal specified <> then set to
10776 -- get proper default.
10778 if No (Actual) and then Box_Present (Formal) then
10779 Set_From_Default (Decl_Node);
10782 -- Gather possible interpretations for the actual before analyzing the
10783 -- instance. If overloaded, it will be resolved when analyzing the
10784 -- renaming declaration.
10786 if Box_Present (Formal) and then No (Actual) then
10789 if Is_Child_Unit (Scope (Analyzed_S))
10790 and then Present (Entity (Nam))
10792 if not Is_Overloaded (Nam) then
10793 if From_Parent_Scope (Entity (Nam)) then
10794 Set_Is_Immediately_Visible (Entity (Nam), False);
10795 Set_Entity (Nam, Empty);
10796 Set_Etype (Nam, Empty);
10799 Set_Is_Immediately_Visible (Entity (Nam));
10808 Get_First_Interp (Nam, I, It);
10809 while Present (It.Nam) loop
10810 if From_Parent_Scope (It.Nam) then
10814 Get_Next_Interp (I, It);
10821 -- The generic instantiation freezes the actual. This can only be done
10822 -- once the actual is resolved, in the analysis of the renaming
10823 -- declaration. To make the formal subprogram entity available, we set
10824 -- Corresponding_Formal_Spec to point to the formal subprogram entity.
10825 -- This is also needed in Analyze_Subprogram_Renaming for the processing
10826 -- of formal abstract subprograms.
10828 Set_Corresponding_Formal_Spec (Decl_Node, Analyzed_S);
10830 -- We cannot analyze the renaming declaration, and thus find the actual,
10831 -- until all the actuals are assembled in the instance. For subsequent
10832 -- checks of other actuals, indicate the node that will hold the
10833 -- instance of this formal.
10835 Set_Instance_Of (Analyzed_S, Nam);
10837 if Nkind (Actual) = N_Selected_Component
10838 and then Is_Task_Type (Etype (Prefix (Actual)))
10839 and then not Is_Frozen (Etype (Prefix (Actual)))
10841 -- The renaming declaration will create a body, which must appear
10842 -- outside of the instantiation, We move the renaming declaration
10843 -- out of the instance, and create an additional renaming inside,
10844 -- to prevent freezing anomalies.
10847 Anon_Id : constant Entity_Id := Make_Temporary (Loc, 'E');
10850 Set_Defining_Unit_Name (New_Spec, Anon_Id);
10851 Insert_Before (Instantiation_Node, Decl_Node);
10852 Analyze (Decl_Node);
10854 -- Now create renaming within the instance
10857 Make_Subprogram_Renaming_Declaration (Loc,
10858 Specification => New_Copy_Tree (New_Spec),
10859 Name => New_Occurrence_Of (Anon_Id, Loc));
10861 Set_Defining_Unit_Name (Specification (Decl_Node),
10862 Make_Defining_Identifier (Loc, Chars (Formal_Sub)));
10867 end Instantiate_Formal_Subprogram;
10869 ------------------------
10870 -- Instantiate_Object --
10871 ------------------------
10873 function Instantiate_Object
10876 Analyzed_Formal : Node_Id) return List_Id
10878 Gen_Obj : constant Entity_Id := Defining_Identifier (Formal);
10879 A_Gen_Obj : constant Entity_Id :=
10880 Defining_Identifier (Analyzed_Formal);
10881 Acc_Def : Node_Id := Empty;
10882 Act_Assoc : constant Node_Id := Parent (Actual);
10883 Actual_Decl : Node_Id := Empty;
10884 Decl_Node : Node_Id;
10887 List : constant List_Id := New_List;
10888 Loc : constant Source_Ptr := Sloc (Actual);
10889 Orig_Ftyp : constant Entity_Id := Etype (A_Gen_Obj);
10890 Subt_Decl : Node_Id := Empty;
10891 Subt_Mark : Node_Id := Empty;
10893 -- Start of processing for Instantiate_Object
10896 -- Formal may be an anonymous access
10898 if Present (Subtype_Mark (Formal)) then
10899 Subt_Mark := Subtype_Mark (Formal);
10901 Check_Access_Definition (Formal);
10902 Acc_Def := Access_Definition (Formal);
10905 -- Sloc for error message on missing actual
10907 Error_Msg_Sloc := Sloc (Scope (A_Gen_Obj));
10909 if Get_Instance_Of (Gen_Obj) /= Gen_Obj then
10910 Error_Msg_N ("duplicate instantiation of generic parameter", Actual);
10913 Set_Parent (List, Parent (Actual));
10917 if Out_Present (Formal) then
10919 -- An IN OUT generic actual must be a name. The instantiation is a
10920 -- renaming declaration. The actual is the name being renamed. We
10921 -- use the actual directly, rather than a copy, because it is not
10922 -- used further in the list of actuals, and because a copy or a use
10923 -- of relocate_node is incorrect if the instance is nested within a
10924 -- generic. In order to simplify e.g. ASIS queries, the
10925 -- Generic_Parent field links the declaration to the generic
10928 if No (Actual) then
10930 ("missing actual &",
10931 Instantiation_Node, Gen_Obj);
10933 ("\in instantiation of & declared#",
10934 Instantiation_Node, Scope (A_Gen_Obj));
10935 Abandon_Instantiation (Instantiation_Node);
10938 if Present (Subt_Mark) then
10940 Make_Object_Renaming_Declaration (Loc,
10941 Defining_Identifier => New_Copy (Gen_Obj),
10942 Subtype_Mark => New_Copy_Tree (Subt_Mark),
10945 else pragma Assert (Present (Acc_Def));
10947 Make_Object_Renaming_Declaration (Loc,
10948 Defining_Identifier => New_Copy (Gen_Obj),
10949 Access_Definition => New_Copy_Tree (Acc_Def),
10953 Set_Corresponding_Generic_Association (Decl_Node, Act_Assoc);
10955 -- The analysis of the actual may produce Insert_Action nodes, so
10956 -- the declaration must have a context in which to attach them.
10958 Append (Decl_Node, List);
10961 -- Return if the analysis of the actual reported some error
10963 if Etype (Actual) = Any_Type then
10967 -- This check is performed here because Analyze_Object_Renaming will
10968 -- not check it when Comes_From_Source is False. Note though that the
10969 -- check for the actual being the name of an object will be performed
10970 -- in Analyze_Object_Renaming.
10972 if Is_Object_Reference (Actual)
10973 and then Is_Dependent_Component_Of_Mutable_Object (Actual)
10976 ("illegal discriminant-dependent component for in out parameter",
10980 -- The actual has to be resolved in order to check that it is a
10981 -- variable (due to cases such as F (1), where F returns access to
10982 -- an array, and for overloaded prefixes).
10984 Ftyp := Get_Instance_Of (Etype (A_Gen_Obj));
10986 -- If the type of the formal is not itself a formal, and the current
10987 -- unit is a child unit, the formal type must be declared in a
10988 -- parent, and must be retrieved by visibility.
10990 if Ftyp = Orig_Ftyp
10991 and then Is_Generic_Unit (Scope (Ftyp))
10992 and then Is_Child_Unit (Scope (A_Gen_Obj))
10995 Temp : constant Node_Id :=
10996 New_Copy_Tree (Subtype_Mark (Analyzed_Formal));
10998 Set_Entity (Temp, Empty);
11000 Ftyp := Entity (Temp);
11004 if Is_Private_Type (Ftyp)
11005 and then not Is_Private_Type (Etype (Actual))
11006 and then (Base_Type (Full_View (Ftyp)) = Base_Type (Etype (Actual))
11007 or else Base_Type (Etype (Actual)) = Ftyp)
11009 -- If the actual has the type of the full view of the formal, or
11010 -- else a non-private subtype of the formal, then the visibility
11011 -- of the formal type has changed. Add to the actuals a subtype
11012 -- declaration that will force the exchange of views in the body
11013 -- of the instance as well.
11016 Make_Subtype_Declaration (Loc,
11017 Defining_Identifier => Make_Temporary (Loc, 'P'),
11018 Subtype_Indication => New_Occurrence_Of (Ftyp, Loc));
11020 Prepend (Subt_Decl, List);
11022 Prepend_Elmt (Full_View (Ftyp), Exchanged_Views);
11023 Exchange_Declarations (Ftyp);
11026 Resolve (Actual, Ftyp);
11028 if not Denotes_Variable (Actual) then
11029 Error_Msg_NE ("actual for& must be a variable", Actual, Gen_Obj);
11031 elsif Base_Type (Ftyp) /= Base_Type (Etype (Actual)) then
11033 -- Ada 2005 (AI-423): For a generic formal object of mode in out,
11034 -- the type of the actual shall resolve to a specific anonymous
11037 if Ada_Version < Ada_2005
11038 or else Ekind (Base_Type (Ftyp)) /=
11039 E_Anonymous_Access_Type
11040 or else Ekind (Base_Type (Etype (Actual))) /=
11041 E_Anonymous_Access_Type
11044 ("type of actual does not match type of&", Actual, Gen_Obj);
11048 Note_Possible_Modification (Actual, Sure => True);
11050 -- Check for instantiation with atomic/volatile object actual for
11051 -- nonatomic/nonvolatile formal (RM C.6 (12)).
11053 if Is_Atomic_Object (Actual) and then not Is_Atomic (Orig_Ftyp) then
11055 ("cannot instantiate nonatomic formal & of mode in out",
11057 Error_Msg_N ("\with atomic object actual (RM C.6(12))", Actual);
11059 elsif Is_Volatile_Object (Actual) and then not Is_Volatile (Orig_Ftyp)
11062 ("cannot instantiate nonvolatile formal & of mode in out",
11064 Error_Msg_N ("\with volatile object actual (RM C.6(12))", Actual);
11067 -- Check for instantiation on nonatomic subcomponent of an atomic
11068 -- object in Ada 2020 (RM C.6 (13)).
11070 if Ada_Version >= Ada_2020
11071 and then Is_Subcomponent_Of_Atomic_Object (Actual)
11072 and then not Is_Atomic_Object (Actual)
11075 ("cannot instantiate formal & of mode in out with actual",
11078 ("\nonatomic subcomponent of atomic object (RM C.6(13))",
11082 -- Formal in-parameter
11085 -- The instantiation of a generic formal in-parameter is constant
11086 -- declaration. The actual is the expression for that declaration.
11087 -- Its type is a full copy of the type of the formal. This may be
11088 -- an access to subprogram, for which we need to generate entities
11089 -- for the formals in the new signature.
11091 if Present (Actual) then
11092 if Present (Subt_Mark) then
11093 Def := New_Copy_Tree (Subt_Mark);
11095 pragma Assert (Present (Acc_Def));
11096 Def := New_Copy_Tree (Acc_Def);
11100 Make_Object_Declaration (Loc,
11101 Defining_Identifier => New_Copy (Gen_Obj),
11102 Constant_Present => True,
11103 Null_Exclusion_Present => Null_Exclusion_Present (Formal),
11104 Object_Definition => Def,
11105 Expression => Actual);
11107 Set_Corresponding_Generic_Association (Decl_Node, Act_Assoc);
11109 -- A generic formal object of a tagged type is defined to be
11110 -- aliased so the new constant must also be treated as aliased.
11112 if Is_Tagged_Type (Etype (A_Gen_Obj)) then
11113 Set_Aliased_Present (Decl_Node);
11116 Append (Decl_Node, List);
11118 -- No need to repeat (pre-)analysis of some expression nodes
11119 -- already handled in Preanalyze_Actuals.
11121 if Nkind (Actual) /= N_Allocator then
11124 -- Return if the analysis of the actual reported some error
11126 if Etype (Actual) = Any_Type then
11132 Formal_Type : constant Entity_Id := Etype (A_Gen_Obj);
11136 Typ := Get_Instance_Of (Formal_Type);
11138 -- If the actual appears in the current or an enclosing scope,
11139 -- use its type directly. This is relevant if it has an actual
11140 -- subtype that is distinct from its nominal one. This cannot
11141 -- be done in general because the type of the actual may
11142 -- depend on other actuals, and only be fully determined when
11143 -- the enclosing instance is analyzed.
11145 if Present (Etype (Actual))
11146 and then Is_Constr_Subt_For_U_Nominal (Etype (Actual))
11148 Freeze_Before (Instantiation_Node, Etype (Actual));
11150 Freeze_Before (Instantiation_Node, Typ);
11153 -- If the actual is an aggregate, perform name resolution on
11154 -- its components (the analysis of an aggregate does not do it)
11155 -- to capture local names that may be hidden if the generic is
11158 if Nkind (Actual) = N_Aggregate then
11159 Preanalyze_And_Resolve (Actual, Typ);
11162 if Is_Limited_Type (Typ)
11163 and then not OK_For_Limited_Init (Typ, Actual)
11166 ("initialization not allowed for limited types", Actual);
11167 Explain_Limited_Type (Typ, Actual);
11171 elsif Present (Default_Expression (Formal)) then
11173 -- Use default to construct declaration
11175 if Present (Subt_Mark) then
11176 Def := New_Copy (Subt_Mark);
11178 pragma Assert (Present (Acc_Def));
11179 Def := New_Copy_Tree (Acc_Def);
11183 Make_Object_Declaration (Sloc (Formal),
11184 Defining_Identifier => New_Copy (Gen_Obj),
11185 Constant_Present => True,
11186 Null_Exclusion_Present => Null_Exclusion_Present (Formal),
11187 Object_Definition => Def,
11188 Expression => New_Copy_Tree
11189 (Default_Expression (Formal)));
11191 Set_Corresponding_Generic_Association
11192 (Decl_Node, Expression (Decl_Node));
11194 Append (Decl_Node, List);
11195 Set_Analyzed (Expression (Decl_Node), False);
11198 Error_Msg_NE ("missing actual&", Instantiation_Node, Gen_Obj);
11199 Error_Msg_NE ("\in instantiation of & declared#",
11200 Instantiation_Node, Scope (A_Gen_Obj));
11202 if Is_Scalar_Type (Etype (A_Gen_Obj)) then
11204 -- Create dummy constant declaration so that instance can be
11205 -- analyzed, to minimize cascaded visibility errors.
11207 if Present (Subt_Mark) then
11209 else pragma Assert (Present (Acc_Def));
11214 Make_Object_Declaration (Loc,
11215 Defining_Identifier => New_Copy (Gen_Obj),
11216 Constant_Present => True,
11217 Null_Exclusion_Present => Null_Exclusion_Present (Formal),
11218 Object_Definition => New_Copy (Def),
11220 Make_Attribute_Reference (Sloc (Gen_Obj),
11221 Attribute_Name => Name_First,
11222 Prefix => New_Copy (Def)));
11224 Append (Decl_Node, List);
11227 Abandon_Instantiation (Instantiation_Node);
11232 if Nkind (Actual) in N_Has_Entity then
11233 Actual_Decl := Parent (Entity (Actual));
11236 -- Ada 2005 (AI-423): For a formal object declaration with a null
11237 -- exclusion or an access definition that has a null exclusion: If the
11238 -- actual matching the formal object declaration denotes a generic
11239 -- formal object of another generic unit G, and the instantiation
11240 -- containing the actual occurs within the body of G or within the body
11241 -- of a generic unit declared within the declarative region of G, then
11242 -- the declaration of the formal object of G must have a null exclusion.
11243 -- Otherwise, the subtype of the actual matching the formal object
11244 -- declaration shall exclude null.
11246 if Ada_Version >= Ada_2005
11247 and then Present (Actual_Decl)
11248 and then Nkind_In (Actual_Decl, N_Formal_Object_Declaration,
11249 N_Object_Declaration)
11250 and then Nkind (Analyzed_Formal) = N_Formal_Object_Declaration
11251 and then not Has_Null_Exclusion (Actual_Decl)
11252 and then Has_Null_Exclusion (Analyzed_Formal)
11254 Error_Msg_Sloc := Sloc (Analyzed_Formal);
11256 ("actual must exclude null to match generic formal#", Actual);
11259 -- An effectively volatile object cannot be used as an actual in a
11260 -- generic instantiation (SPARK RM 7.1.3(7)). The following check is
11261 -- relevant only when SPARK_Mode is on as it is not a standard Ada
11262 -- legality rule, and also verifies that the actual is an object.
11265 and then Present (Actual)
11266 and then Is_Object_Reference (Actual)
11267 and then Is_Effectively_Volatile_Object (Actual)
11270 ("volatile object cannot act as actual in generic instantiation",
11275 end Instantiate_Object;
11277 ------------------------------
11278 -- Instantiate_Package_Body --
11279 ------------------------------
11281 -- WARNING: This routine manages Ghost and SPARK regions. Return statements
11282 -- must be replaced by gotos which jump to the end of the routine in order
11283 -- to restore the Ghost and SPARK modes.
11285 procedure Instantiate_Package_Body
11286 (Body_Info : Pending_Body_Info;
11287 Inlined_Body : Boolean := False;
11288 Body_Optional : Boolean := False)
11290 Act_Decl : constant Node_Id := Body_Info.Act_Decl;
11291 Act_Decl_Id : constant Entity_Id := Defining_Entity (Act_Decl);
11292 Act_Spec : constant Node_Id := Specification (Act_Decl);
11293 Inst_Node : constant Node_Id := Body_Info.Inst_Node;
11294 Gen_Id : constant Node_Id := Name (Inst_Node);
11295 Gen_Unit : constant Entity_Id := Get_Generic_Entity (Inst_Node);
11296 Gen_Decl : constant Node_Id := Unit_Declaration_Node (Gen_Unit);
11297 Loc : constant Source_Ptr := Sloc (Inst_Node);
11299 procedure Check_Initialized_Types;
11300 -- In a generic package body, an entity of a generic private type may
11301 -- appear uninitialized. This is suspicious, unless the actual is a
11302 -- fully initialized type.
11304 -----------------------------
11305 -- Check_Initialized_Types --
11306 -----------------------------
11308 procedure Check_Initialized_Types is
11310 Formal : Entity_Id;
11311 Actual : Entity_Id;
11312 Uninit_Var : Entity_Id;
11315 Decl := First (Generic_Formal_Declarations (Gen_Decl));
11316 while Present (Decl) loop
11317 Uninit_Var := Empty;
11319 if Nkind (Decl) = N_Private_Extension_Declaration then
11320 Uninit_Var := Uninitialized_Variable (Decl);
11322 elsif Nkind (Decl) = N_Formal_Type_Declaration
11323 and then Nkind (Formal_Type_Definition (Decl)) =
11324 N_Formal_Private_Type_Definition
11327 Uninitialized_Variable (Formal_Type_Definition (Decl));
11330 if Present (Uninit_Var) then
11331 Formal := Defining_Identifier (Decl);
11332 Actual := First_Entity (Act_Decl_Id);
11334 -- For each formal there is a subtype declaration that renames
11335 -- the actual and has the same name as the formal. Locate the
11336 -- formal for warning message about uninitialized variables
11337 -- in the generic, for which the actual type should be a fully
11338 -- initialized type.
11340 while Present (Actual) loop
11341 exit when Ekind (Actual) = E_Package
11342 and then Present (Renamed_Object (Actual));
11344 if Chars (Actual) = Chars (Formal)
11345 and then not Is_Scalar_Type (Actual)
11346 and then not Is_Fully_Initialized_Type (Actual)
11347 and then Warn_On_No_Value_Assigned
11349 Error_Msg_Node_2 := Formal;
11351 ("generic unit has uninitialized variable& of "
11352 & "formal private type &?v?", Actual, Uninit_Var);
11354 ("actual type for& should be fully initialized type?v?",
11359 Next_Entity (Actual);
11365 end Check_Initialized_Types;
11369 -- The following constants capture the context prior to instantiating
11370 -- the package body.
11372 Saved_CS : constant Config_Switches_Type := Save_Config_Switches;
11373 Saved_GM : constant Ghost_Mode_Type := Ghost_Mode;
11374 Saved_IGR : constant Node_Id := Ignored_Ghost_Region;
11375 Saved_ISMP : constant Boolean :=
11376 Ignore_SPARK_Mode_Pragmas_In_Instance;
11377 Saved_LSST : constant Suppress_Stack_Entry_Ptr :=
11378 Local_Suppress_Stack_Top;
11379 Saved_SC : constant Boolean := Style_Check;
11380 Saved_SM : constant SPARK_Mode_Type := SPARK_Mode;
11381 Saved_SMP : constant Node_Id := SPARK_Mode_Pragma;
11382 Saved_SS : constant Suppress_Record := Scope_Suppress;
11383 Saved_Warn : constant Warning_Record := Save_Warnings;
11385 Act_Body : Node_Id;
11386 Act_Body_Id : Entity_Id;
11387 Act_Body_Name : Node_Id;
11388 Gen_Body : Node_Id;
11389 Gen_Body_Id : Node_Id;
11390 Par_Ent : Entity_Id := Empty;
11391 Par_Installed : Boolean := False;
11392 Par_Vis : Boolean := False;
11394 Vis_Prims_List : Elist_Id := No_Elist;
11395 -- List of primitives made temporarily visible in the instantiation
11396 -- to match the visibility of the formal type.
11398 -- Start of processing for Instantiate_Package_Body
11401 Gen_Body_Id := Corresponding_Body (Gen_Decl);
11403 -- The instance body may already have been processed, as the parent of
11404 -- another instance that is inlined (Load_Parent_Of_Generic).
11406 if Present (Corresponding_Body (Instance_Spec (Inst_Node))) then
11410 -- The package being instantiated may be subject to pragma Ghost. Set
11411 -- the mode now to ensure that any nodes generated during instantiation
11412 -- are properly marked as Ghost.
11414 Set_Ghost_Mode (Act_Decl_Id);
11416 Expander_Mode_Save_And_Set (Body_Info.Expander_Status);
11418 -- Re-establish the state of information on which checks are suppressed.
11419 -- This information was set in Body_Info at the point of instantiation,
11420 -- and now we restore it so that the instance is compiled using the
11421 -- check status at the instantiation (RM 11.5(7.2/2), AI95-00224-01).
11423 Local_Suppress_Stack_Top := Body_Info.Local_Suppress_Stack_Top;
11424 Scope_Suppress := Body_Info.Scope_Suppress;
11426 Restore_Config_Switches (Body_Info.Config_Switches);
11427 Restore_Warnings (Body_Info.Warnings);
11429 if No (Gen_Body_Id) then
11431 -- Do not look for parent of generic body if none is required.
11432 -- This may happen when the routine is called as part of the
11433 -- Pending_Instantiations processing, when nested instances
11434 -- may precede the one generated from the main unit.
11436 if not Unit_Requires_Body (Defining_Entity (Gen_Decl))
11437 and then Body_Optional
11441 Load_Parent_Of_Generic
11442 (Inst_Node, Specification (Gen_Decl), Body_Optional);
11444 -- Surprisingly enough, loading the body of the parent can cause
11445 -- the body to be instantiated and the double instantiation needs
11446 -- to be prevented in order to avoid giving bogus semantic errors.
11448 -- This case can occur because of the Collect_Previous_Instances
11449 -- machinery of Load_Parent_Of_Generic, which will instantiate
11450 -- bodies that are deemed to be ahead of the body of the parent
11451 -- in the compilation unit. But the relative position of these
11452 -- bodies is computed using the mere comparison of their Sloc.
11454 -- Now suppose that you have two generic packages G and H, with
11455 -- G containing a mere instantiation of H:
11461 -- package Nested_G is
11472 -- package My_H is new H;
11476 -- and a third package Q instantiating G and Nested_G:
11482 -- package My_G is new G;
11484 -- package My_Nested_G is new My_G.My_H.Nested_G;
11488 -- The body to be instantiated is that of My_Nested_G and its
11489 -- parent is the instance My_G.My_H. This latter instantiation
11490 -- is done when My_G is analyzed, i.e. after the declarations
11491 -- of My_G and My_Nested_G have been parsed; as a result, the
11492 -- Sloc of My_G.My_H is greater than the Sloc of My_Nested_G.
11494 -- Therefore loading the body of My_G.My_H will cause the body
11495 -- of My_Nested_G to be instantiated because it is deemed to be
11496 -- ahead of My_G.My_H. This means that Load_Parent_Of_Generic
11497 -- will again be invoked on My_G.My_H, but this time with the
11498 -- Collect_Previous_Instances machinery disabled, so there is
11499 -- no endless mutual recursion and things are done in order.
11501 if Present (Corresponding_Body (Instance_Spec (Inst_Node))) then
11505 Gen_Body_Id := Corresponding_Body (Gen_Decl);
11509 -- Establish global variable for sloc adjustment and for error recovery
11510 -- In the case of an instance body for an instantiation with actuals
11511 -- from a limited view, the instance body is placed at the beginning
11512 -- of the enclosing package body: use the body entity as the source
11513 -- location for nodes of the instance body.
11515 if not Is_Empty_Elmt_List (Incomplete_Actuals (Act_Decl_Id)) then
11517 Scop : constant Entity_Id := Scope (Act_Decl_Id);
11518 Body_Id : constant Node_Id :=
11519 Corresponding_Body (Unit_Declaration_Node (Scop));
11522 Instantiation_Node := Body_Id;
11525 Instantiation_Node := Inst_Node;
11528 if Present (Gen_Body_Id) then
11529 Save_Env (Gen_Unit, Act_Decl_Id);
11530 Style_Check := False;
11532 -- If the context of the instance is subject to SPARK_Mode "off", the
11533 -- annotation is missing, or the body is instantiated at a later pass
11534 -- and its spec ignored SPARK_Mode pragma, set the global flag which
11535 -- signals Analyze_Pragma to ignore all SPARK_Mode pragmas within the
11538 if SPARK_Mode /= On
11539 or else Ignore_SPARK_Mode_Pragmas (Act_Decl_Id)
11541 Ignore_SPARK_Mode_Pragmas_In_Instance := True;
11544 Current_Sem_Unit := Body_Info.Current_Sem_Unit;
11545 Gen_Body := Unit_Declaration_Node (Gen_Body_Id);
11547 Create_Instantiation_Source
11548 (Inst_Node, Gen_Body_Id, S_Adjustment);
11552 (Original_Node (Gen_Body), Empty, Instantiating => True);
11554 -- Create proper (possibly qualified) defining name for the body, to
11555 -- correspond to the one in the spec.
11558 Make_Defining_Identifier (Sloc (Act_Decl_Id), Chars (Act_Decl_Id));
11559 Set_Comes_From_Source (Act_Body_Id, Comes_From_Source (Act_Decl_Id));
11561 -- Some attributes of spec entity are not inherited by body entity
11563 Set_Handler_Records (Act_Body_Id, No_List);
11565 if Nkind (Defining_Unit_Name (Act_Spec)) =
11566 N_Defining_Program_Unit_Name
11569 Make_Defining_Program_Unit_Name (Loc,
11571 New_Copy_Tree (Name (Defining_Unit_Name (Act_Spec))),
11572 Defining_Identifier => Act_Body_Id);
11574 Act_Body_Name := Act_Body_Id;
11577 Set_Defining_Unit_Name (Act_Body, Act_Body_Name);
11579 Set_Corresponding_Spec (Act_Body, Act_Decl_Id);
11580 Check_Generic_Actuals (Act_Decl_Id, False);
11581 Check_Initialized_Types;
11583 -- Install primitives hidden at the point of the instantiation but
11584 -- visible when processing the generic formals
11590 E := First_Entity (Act_Decl_Id);
11591 while Present (E) loop
11593 and then not Is_Itype (E)
11594 and then Is_Generic_Actual_Type (E)
11595 and then Is_Tagged_Type (E)
11597 Install_Hidden_Primitives
11598 (Prims_List => Vis_Prims_List,
11599 Gen_T => Generic_Parent_Type (Parent (E)),
11607 -- If it is a child unit, make the parent instance (which is an
11608 -- instance of the parent of the generic) visible. The parent
11609 -- instance is the prefix of the name of the generic unit.
11611 if Ekind (Scope (Gen_Unit)) = E_Generic_Package
11612 and then Nkind (Gen_Id) = N_Expanded_Name
11614 Par_Ent := Entity (Prefix (Gen_Id));
11615 Par_Vis := Is_Immediately_Visible (Par_Ent);
11616 Install_Parent (Par_Ent, In_Body => True);
11617 Par_Installed := True;
11619 elsif Is_Child_Unit (Gen_Unit) then
11620 Par_Ent := Scope (Gen_Unit);
11621 Par_Vis := Is_Immediately_Visible (Par_Ent);
11622 Install_Parent (Par_Ent, In_Body => True);
11623 Par_Installed := True;
11626 -- If the instantiation is a library unit, and this is the main unit,
11627 -- then build the resulting compilation unit nodes for the instance.
11628 -- If this is a compilation unit but it is not the main unit, then it
11629 -- is the body of a unit in the context, that is being compiled
11630 -- because it is encloses some inlined unit or another generic unit
11631 -- being instantiated. In that case, this body is not part of the
11632 -- current compilation, and is not attached to the tree, but its
11633 -- parent must be set for analysis.
11635 if Nkind (Parent (Inst_Node)) = N_Compilation_Unit then
11637 -- Replace instance node with body of instance, and create new
11638 -- node for corresponding instance declaration.
11640 Build_Instance_Compilation_Unit_Nodes
11641 (Inst_Node, Act_Body, Act_Decl);
11642 Analyze (Inst_Node);
11644 if Parent (Inst_Node) = Cunit (Main_Unit) then
11646 -- If the instance is a child unit itself, then set the scope
11647 -- of the expanded body to be the parent of the instantiation
11648 -- (ensuring that the fully qualified name will be generated
11649 -- for the elaboration subprogram).
11651 if Nkind (Defining_Unit_Name (Act_Spec)) =
11652 N_Defining_Program_Unit_Name
11654 Set_Scope (Defining_Entity (Inst_Node), Scope (Act_Decl_Id));
11658 -- Case where instantiation is not a library unit
11661 -- If this is an early instantiation, i.e. appears textually
11662 -- before the corresponding body and must be elaborated first,
11663 -- indicate that the body instance is to be delayed.
11665 Install_Body (Act_Body, Inst_Node, Gen_Body, Gen_Decl);
11666 Analyze (Act_Body);
11669 Inherit_Context (Gen_Body, Inst_Node);
11671 -- Remove the parent instances if they have been placed on the scope
11672 -- stack to compile the body.
11674 if Par_Installed then
11675 Remove_Parent (In_Body => True);
11677 -- Restore the previous visibility of the parent
11679 Set_Is_Immediately_Visible (Par_Ent, Par_Vis);
11682 Restore_Hidden_Primitives (Vis_Prims_List);
11683 Restore_Private_Views (Act_Decl_Id);
11685 -- Remove the current unit from visibility if this is an instance
11686 -- that is not elaborated on the fly for inlining purposes.
11688 if not Inlined_Body then
11689 Set_Is_Immediately_Visible (Act_Decl_Id, False);
11694 -- If we have no body, and the unit requires a body, then complain. This
11695 -- complaint is suppressed if we have detected other errors (since a
11696 -- common reason for missing the body is that it had errors).
11697 -- In CodePeer mode, a warning has been emitted already, no need for
11698 -- further messages.
11700 elsif Unit_Requires_Body (Gen_Unit)
11701 and then not Body_Optional
11703 if CodePeer_Mode then
11706 elsif Serious_Errors_Detected = 0 then
11708 ("cannot find body of generic package &", Inst_Node, Gen_Unit);
11710 -- Don't attempt to perform any cleanup actions if some other error
11711 -- was already detected, since this can cause blowups.
11717 -- Case of package that does not need a body
11720 -- If the instantiation of the declaration is a library unit, rewrite
11721 -- the original package instantiation as a package declaration in the
11722 -- compilation unit node.
11724 if Nkind (Parent (Inst_Node)) = N_Compilation_Unit then
11725 Set_Parent_Spec (Act_Decl, Parent_Spec (Inst_Node));
11726 Rewrite (Inst_Node, Act_Decl);
11728 -- Generate elaboration entity, in case spec has elaboration code.
11729 -- This cannot be done when the instance is analyzed, because it
11730 -- is not known yet whether the body exists.
11732 Set_Elaboration_Entity_Required (Act_Decl_Id, False);
11733 Build_Elaboration_Entity (Parent (Inst_Node), Act_Decl_Id);
11735 -- If the instantiation is not a library unit, then append the
11736 -- declaration to the list of implicitly generated entities, unless
11737 -- it is already a list member which means that it was already
11740 elsif not Is_List_Member (Act_Decl) then
11741 Mark_Rewrite_Insertion (Act_Decl);
11742 Insert_Before (Inst_Node, Act_Decl);
11748 -- Restore the context that was in effect prior to instantiating the
11751 Ignore_SPARK_Mode_Pragmas_In_Instance := Saved_ISMP;
11752 Local_Suppress_Stack_Top := Saved_LSST;
11753 Scope_Suppress := Saved_SS;
11754 Style_Check := Saved_SC;
11756 Expander_Mode_Restore;
11757 Restore_Config_Switches (Saved_CS);
11758 Restore_Ghost_Region (Saved_GM, Saved_IGR);
11759 Restore_SPARK_Mode (Saved_SM, Saved_SMP);
11760 Restore_Warnings (Saved_Warn);
11761 end Instantiate_Package_Body;
11763 ---------------------------------
11764 -- Instantiate_Subprogram_Body --
11765 ---------------------------------
11767 -- WARNING: This routine manages Ghost and SPARK regions. Return statements
11768 -- must be replaced by gotos which jump to the end of the routine in order
11769 -- to restore the Ghost and SPARK modes.
11771 procedure Instantiate_Subprogram_Body
11772 (Body_Info : Pending_Body_Info;
11773 Body_Optional : Boolean := False)
11775 Act_Decl : constant Node_Id := Body_Info.Act_Decl;
11776 Act_Decl_Id : constant Entity_Id := Defining_Entity (Act_Decl);
11777 Inst_Node : constant Node_Id := Body_Info.Inst_Node;
11778 Gen_Id : constant Node_Id := Name (Inst_Node);
11779 Gen_Unit : constant Entity_Id := Get_Generic_Entity (Inst_Node);
11780 Gen_Decl : constant Node_Id := Unit_Declaration_Node (Gen_Unit);
11781 Loc : constant Source_Ptr := Sloc (Inst_Node);
11782 Pack_Id : constant Entity_Id :=
11783 Defining_Unit_Name (Parent (Act_Decl));
11785 -- The following constants capture the context prior to instantiating
11786 -- the subprogram body.
11788 Saved_CS : constant Config_Switches_Type := Save_Config_Switches;
11789 Saved_GM : constant Ghost_Mode_Type := Ghost_Mode;
11790 Saved_IGR : constant Node_Id := Ignored_Ghost_Region;
11791 Saved_ISMP : constant Boolean :=
11792 Ignore_SPARK_Mode_Pragmas_In_Instance;
11793 Saved_LSST : constant Suppress_Stack_Entry_Ptr :=
11794 Local_Suppress_Stack_Top;
11795 Saved_SC : constant Boolean := Style_Check;
11796 Saved_SM : constant SPARK_Mode_Type := SPARK_Mode;
11797 Saved_SMP : constant Node_Id := SPARK_Mode_Pragma;
11798 Saved_SS : constant Suppress_Record := Scope_Suppress;
11799 Saved_Warn : constant Warning_Record := Save_Warnings;
11801 Act_Body : Node_Id;
11802 Act_Body_Id : Entity_Id;
11803 Gen_Body : Node_Id;
11804 Gen_Body_Id : Node_Id;
11805 Pack_Body : Node_Id;
11806 Par_Ent : Entity_Id := Empty;
11807 Par_Installed : Boolean := False;
11808 Par_Vis : Boolean := False;
11809 Ret_Expr : Node_Id;
11812 Gen_Body_Id := Corresponding_Body (Gen_Decl);
11814 -- Subprogram body may have been created already because of an inline
11815 -- pragma, or because of multiple elaborations of the enclosing package
11816 -- when several instances of the subprogram appear in the main unit.
11818 if Present (Corresponding_Body (Act_Decl)) then
11822 -- The subprogram being instantiated may be subject to pragma Ghost. Set
11823 -- the mode now to ensure that any nodes generated during instantiation
11824 -- are properly marked as Ghost.
11826 Set_Ghost_Mode (Act_Decl_Id);
11828 Expander_Mode_Save_And_Set (Body_Info.Expander_Status);
11830 -- Re-establish the state of information on which checks are suppressed.
11831 -- This information was set in Body_Info at the point of instantiation,
11832 -- and now we restore it so that the instance is compiled using the
11833 -- check status at the instantiation (RM 11.5(7.2/2), AI95-00224-01).
11835 Local_Suppress_Stack_Top := Body_Info.Local_Suppress_Stack_Top;
11836 Scope_Suppress := Body_Info.Scope_Suppress;
11838 Restore_Config_Switches (Body_Info.Config_Switches);
11839 Restore_Warnings (Body_Info.Warnings);
11841 if No (Gen_Body_Id) then
11843 -- For imported generic subprogram, no body to compile, complete
11844 -- the spec entity appropriately.
11846 if Is_Imported (Gen_Unit) then
11847 Set_Is_Imported (Act_Decl_Id);
11848 Set_First_Rep_Item (Act_Decl_Id, First_Rep_Item (Gen_Unit));
11849 Set_Interface_Name (Act_Decl_Id, Interface_Name (Gen_Unit));
11850 Set_Convention (Act_Decl_Id, Convention (Gen_Unit));
11851 Set_Has_Completion (Act_Decl_Id);
11854 -- For other cases, compile the body
11857 Load_Parent_Of_Generic
11858 (Inst_Node, Specification (Gen_Decl), Body_Optional);
11859 Gen_Body_Id := Corresponding_Body (Gen_Decl);
11863 Instantiation_Node := Inst_Node;
11865 if Present (Gen_Body_Id) then
11866 Gen_Body := Unit_Declaration_Node (Gen_Body_Id);
11868 if Nkind (Gen_Body) = N_Subprogram_Body_Stub then
11870 -- Either body is not present, or context is non-expanding, as
11871 -- when compiling a subunit. Mark the instance as completed, and
11872 -- diagnose a missing body when needed.
11875 and then Operating_Mode = Generate_Code
11877 Error_Msg_N ("missing proper body for instantiation", Gen_Body);
11880 Set_Has_Completion (Act_Decl_Id);
11884 Save_Env (Gen_Unit, Act_Decl_Id);
11885 Style_Check := False;
11887 -- If the context of the instance is subject to SPARK_Mode "off", the
11888 -- annotation is missing, or the body is instantiated at a later pass
11889 -- and its spec ignored SPARK_Mode pragma, set the global flag which
11890 -- signals Analyze_Pragma to ignore all SPARK_Mode pragmas within the
11893 if SPARK_Mode /= On
11894 or else Ignore_SPARK_Mode_Pragmas (Act_Decl_Id)
11896 Ignore_SPARK_Mode_Pragmas_In_Instance := True;
11899 -- If the context of an instance is not subject to SPARK_Mode "off",
11900 -- and the generic body is subject to an explicit SPARK_Mode pragma,
11901 -- the latter should be the one applicable to the instance.
11903 if not Ignore_SPARK_Mode_Pragmas_In_Instance
11904 and then SPARK_Mode /= Off
11905 and then Present (SPARK_Pragma (Gen_Body_Id))
11907 Set_SPARK_Mode (Gen_Body_Id);
11910 Current_Sem_Unit := Body_Info.Current_Sem_Unit;
11911 Create_Instantiation_Source
11918 (Original_Node (Gen_Body), Empty, Instantiating => True);
11920 -- Create proper defining name for the body, to correspond to the one
11924 Make_Defining_Identifier (Sloc (Act_Decl_Id), Chars (Act_Decl_Id));
11926 Set_Comes_From_Source (Act_Body_Id, Comes_From_Source (Act_Decl_Id));
11927 Set_Defining_Unit_Name (Specification (Act_Body), Act_Body_Id);
11929 Set_Corresponding_Spec (Act_Body, Act_Decl_Id);
11930 Set_Has_Completion (Act_Decl_Id);
11931 Check_Generic_Actuals (Pack_Id, False);
11933 -- Generate a reference to link the visible subprogram instance to
11934 -- the generic body, which for navigation purposes is the only
11935 -- available source for the instance.
11938 (Related_Instance (Pack_Id),
11939 Gen_Body_Id, 'b', Set_Ref => False, Force => True);
11941 -- If it is a child unit, make the parent instance (which is an
11942 -- instance of the parent of the generic) visible. The parent
11943 -- instance is the prefix of the name of the generic unit.
11945 if Ekind (Scope (Gen_Unit)) = E_Generic_Package
11946 and then Nkind (Gen_Id) = N_Expanded_Name
11948 Par_Ent := Entity (Prefix (Gen_Id));
11949 Par_Vis := Is_Immediately_Visible (Par_Ent);
11950 Install_Parent (Par_Ent, In_Body => True);
11951 Par_Installed := True;
11953 elsif Is_Child_Unit (Gen_Unit) then
11954 Par_Ent := Scope (Gen_Unit);
11955 Par_Vis := Is_Immediately_Visible (Par_Ent);
11956 Install_Parent (Par_Ent, In_Body => True);
11957 Par_Installed := True;
11960 -- Subprogram body is placed in the body of wrapper package,
11961 -- whose spec contains the subprogram declaration as well as
11962 -- the renaming declarations for the generic parameters.
11965 Make_Package_Body (Loc,
11966 Defining_Unit_Name => New_Copy (Pack_Id),
11967 Declarations => New_List (Act_Body));
11969 Set_Corresponding_Spec (Pack_Body, Pack_Id);
11971 -- If the instantiation is a library unit, then build resulting
11972 -- compilation unit nodes for the instance. The declaration of
11973 -- the enclosing package is the grandparent of the subprogram
11974 -- declaration. First replace the instantiation node as the unit
11975 -- of the corresponding compilation.
11977 if Nkind (Parent (Inst_Node)) = N_Compilation_Unit then
11978 if Parent (Inst_Node) = Cunit (Main_Unit) then
11979 Set_Unit (Parent (Inst_Node), Inst_Node);
11980 Build_Instance_Compilation_Unit_Nodes
11981 (Inst_Node, Pack_Body, Parent (Parent (Act_Decl)));
11982 Analyze (Inst_Node);
11984 Set_Parent (Pack_Body, Parent (Inst_Node));
11985 Analyze (Pack_Body);
11989 Insert_Before (Inst_Node, Pack_Body);
11990 Mark_Rewrite_Insertion (Pack_Body);
11991 Analyze (Pack_Body);
11993 if Expander_Active then
11994 Freeze_Subprogram_Body (Inst_Node, Gen_Body, Pack_Id);
11998 Inherit_Context (Gen_Body, Inst_Node);
12000 Restore_Private_Views (Pack_Id, False);
12002 if Par_Installed then
12003 Remove_Parent (In_Body => True);
12005 -- Restore the previous visibility of the parent
12007 Set_Is_Immediately_Visible (Par_Ent, Par_Vis);
12012 -- Body not found. Error was emitted already. If there were no previous
12013 -- errors, this may be an instance whose scope is a premature instance.
12014 -- In that case we must insure that the (legal) program does raise
12015 -- program error if executed. We generate a subprogram body for this
12016 -- purpose. See DEC ac30vso.
12018 -- Should not reference proprietary DEC tests in comments ???
12020 elsif Serious_Errors_Detected = 0
12021 and then Nkind (Parent (Inst_Node)) /= N_Compilation_Unit
12023 if Body_Optional then
12026 elsif Ekind (Act_Decl_Id) = E_Procedure then
12028 Make_Subprogram_Body (Loc,
12030 Make_Procedure_Specification (Loc,
12031 Defining_Unit_Name =>
12032 Make_Defining_Identifier (Loc, Chars (Act_Decl_Id)),
12033 Parameter_Specifications =>
12035 (Parameter_Specifications (Parent (Act_Decl_Id)))),
12037 Declarations => Empty_List,
12038 Handled_Statement_Sequence =>
12039 Make_Handled_Sequence_Of_Statements (Loc,
12040 Statements => New_List (
12041 Make_Raise_Program_Error (Loc,
12042 Reason => PE_Access_Before_Elaboration))));
12046 Make_Raise_Program_Error (Loc,
12047 Reason => PE_Access_Before_Elaboration);
12049 Set_Etype (Ret_Expr, (Etype (Act_Decl_Id)));
12050 Set_Analyzed (Ret_Expr);
12053 Make_Subprogram_Body (Loc,
12055 Make_Function_Specification (Loc,
12056 Defining_Unit_Name =>
12057 Make_Defining_Identifier (Loc, Chars (Act_Decl_Id)),
12058 Parameter_Specifications =>
12060 (Parameter_Specifications (Parent (Act_Decl_Id))),
12061 Result_Definition =>
12062 New_Occurrence_Of (Etype (Act_Decl_Id), Loc)),
12064 Declarations => Empty_List,
12065 Handled_Statement_Sequence =>
12066 Make_Handled_Sequence_Of_Statements (Loc,
12067 Statements => New_List (
12068 Make_Simple_Return_Statement (Loc, Ret_Expr))));
12072 Make_Package_Body (Loc,
12073 Defining_Unit_Name => New_Copy (Pack_Id),
12074 Declarations => New_List (Act_Body));
12076 Insert_After (Inst_Node, Pack_Body);
12077 Set_Corresponding_Spec (Pack_Body, Pack_Id);
12078 Analyze (Pack_Body);
12083 -- Restore the context that was in effect prior to instantiating the
12084 -- subprogram body.
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_Subprogram_Body;
12098 ----------------------
12099 -- Instantiate_Type --
12100 ----------------------
12102 function Instantiate_Type
12105 Analyzed_Formal : Node_Id;
12106 Actual_Decls : List_Id) return List_Id
12108 A_Gen_T : constant Entity_Id :=
12109 Defining_Identifier (Analyzed_Formal);
12110 Def : constant Node_Id := Formal_Type_Definition (Formal);
12111 Gen_T : constant Entity_Id := Defining_Identifier (Formal);
12113 Ancestor : Entity_Id := Empty;
12114 Decl_Node : Node_Id;
12115 Decl_Nodes : List_Id;
12119 procedure Check_Shared_Variable_Control_Aspects;
12120 -- Ada_2020: Verify that shared variable control aspects (RM C.6)
12121 -- that may be specified for a formal type are obeyed by the actual.
12123 procedure Diagnose_Predicated_Actual;
12124 -- There are a number of constructs in which a discrete type with
12125 -- predicates is illegal, e.g. as an index in an array type declaration.
12126 -- If a generic type is used is such a construct in a generic package
12127 -- declaration, it carries the flag No_Predicate_On_Actual. it is part
12128 -- of the generic contract that the actual cannot have predicates.
12130 procedure Validate_Array_Type_Instance;
12131 procedure Validate_Access_Subprogram_Instance;
12132 procedure Validate_Access_Type_Instance;
12133 procedure Validate_Derived_Type_Instance;
12134 procedure Validate_Derived_Interface_Type_Instance;
12135 procedure Validate_Discriminated_Formal_Type;
12136 procedure Validate_Interface_Type_Instance;
12137 procedure Validate_Private_Type_Instance;
12138 procedure Validate_Incomplete_Type_Instance;
12139 -- These procedures perform validation tests for the named case.
12140 -- Validate_Discriminated_Formal_Type is shared by formal private
12141 -- types and Ada 2012 formal incomplete types.
12143 function Subtypes_Match (Gen_T, Act_T : Entity_Id) return Boolean;
12144 -- Check that base types are the same and that the subtypes match
12145 -- statically. Used in several of the above.
12147 --------------------------------------------
12148 -- Check_Shared_Variable_Control_Aspects --
12149 --------------------------------------------
12151 -- Ada_2020: Verify that shared variable control aspects (RM C.6)
12152 -- that may be specified for the formal are obeyed by the actual.
12154 procedure Check_Shared_Variable_Control_Aspects is
12156 if Ada_Version >= Ada_2020 then
12157 if Is_Atomic (A_Gen_T) and then not Is_Atomic (Act_T) then
12159 ("actual for& must be an atomic type", Actual, A_Gen_T);
12162 if Is_Volatile (A_Gen_T) and then not Is_Volatile (Act_T) then
12164 ("actual for& must be a Volatile type", Actual, A_Gen_T);
12168 Is_Independent (A_Gen_T) and then not Is_Independent (Act_T)
12171 ("actual for& must be an Independent type", Actual, A_Gen_T);
12174 -- We assume that an array type whose atomic component type
12175 -- is Atomic is equivalent to an array type with the explicit
12176 -- aspect Has_Atomic_Components. This is a reasonable inference
12177 -- from the intent of AI12-0282, and makes it legal to use an
12178 -- actual that does not have the identical aspect as the formal.
12180 if Has_Atomic_Components (A_Gen_T)
12181 and then not Has_Atomic_Components (Act_T)
12183 if Is_Array_Type (Act_T)
12184 and then Is_Atomic (Component_Type (Act_T))
12190 ("actual for& must have atomic components",
12195 if Has_Independent_Components (A_Gen_T)
12196 and then not Has_Independent_Components (Act_T)
12199 ("actual for& must have independent components",
12203 if Has_Volatile_Components (A_Gen_T)
12204 and then not Has_Volatile_Components (Act_T)
12206 if Is_Array_Type (Act_T)
12207 and then Is_Volatile (Component_Type (Act_T))
12213 ("actual for& must have volatile components",
12218 end Check_Shared_Variable_Control_Aspects;
12220 ---------------------------------
12221 -- Diagnose_Predicated_Actual --
12222 ---------------------------------
12224 procedure Diagnose_Predicated_Actual is
12226 if No_Predicate_On_Actual (A_Gen_T)
12227 and then Has_Predicates (Act_T)
12230 ("actual for& cannot be a type with predicate",
12231 Instantiation_Node, A_Gen_T);
12233 elsif No_Dynamic_Predicate_On_Actual (A_Gen_T)
12234 and then Has_Predicates (Act_T)
12235 and then not Has_Static_Predicate_Aspect (Act_T)
12238 ("actual for& cannot be a type with a dynamic predicate",
12239 Instantiation_Node, A_Gen_T);
12241 end Diagnose_Predicated_Actual;
12243 --------------------
12244 -- Subtypes_Match --
12245 --------------------
12247 function Subtypes_Match (Gen_T, Act_T : Entity_Id) return Boolean is
12248 T : constant Entity_Id := Get_Instance_Of (Gen_T);
12251 -- Some detailed comments would be useful here ???
12253 return ((Base_Type (T) = Act_T
12254 or else Base_Type (T) = Base_Type (Act_T))
12255 and then Subtypes_Statically_Match (T, Act_T))
12257 or else (Is_Class_Wide_Type (Gen_T)
12258 and then Is_Class_Wide_Type (Act_T)
12259 and then Subtypes_Match
12260 (Get_Instance_Of (Root_Type (Gen_T)),
12261 Root_Type (Act_T)))
12264 (Ekind_In (Gen_T, E_Anonymous_Access_Subprogram_Type,
12265 E_Anonymous_Access_Type)
12266 and then Ekind (Act_T) = Ekind (Gen_T)
12267 and then Subtypes_Statically_Match
12268 (Designated_Type (Gen_T), Designated_Type (Act_T)));
12269 end Subtypes_Match;
12271 -----------------------------------------
12272 -- Validate_Access_Subprogram_Instance --
12273 -----------------------------------------
12275 procedure Validate_Access_Subprogram_Instance is
12277 if not Is_Access_Type (Act_T)
12278 or else Ekind (Designated_Type (Act_T)) /= E_Subprogram_Type
12281 ("expect access type in instantiation of &", Actual, Gen_T);
12282 Abandon_Instantiation (Actual);
12285 -- According to AI05-288, actuals for access_to_subprograms must be
12286 -- subtype conformant with the generic formal. Previous to AI05-288
12287 -- only mode conformance was required.
12289 -- This is a binding interpretation that applies to previous versions
12290 -- of the language, no need to maintain previous weaker checks.
12292 Check_Subtype_Conformant
12293 (Designated_Type (Act_T),
12294 Designated_Type (A_Gen_T),
12298 if Ekind (Base_Type (Act_T)) = E_Access_Protected_Subprogram_Type then
12299 if Ekind (A_Gen_T) = E_Access_Subprogram_Type then
12301 ("protected access type not allowed for formal &",
12305 elsif Ekind (A_Gen_T) = E_Access_Protected_Subprogram_Type then
12307 ("expect protected access type for formal &",
12311 -- If the formal has a specified convention (which in most cases
12312 -- will be StdCall) verify that the actual has the same convention.
12314 if Has_Convention_Pragma (A_Gen_T)
12315 and then Convention (A_Gen_T) /= Convention (Act_T)
12317 Error_Msg_Name_1 := Get_Convention_Name (Convention (A_Gen_T));
12319 ("actual for formal & must have convention %", Actual, Gen_T);
12322 if Can_Never_Be_Null (A_Gen_T) /= Can_Never_Be_Null (Act_T) then
12324 ("non null exclusion of actual and formal & do not match",
12327 end Validate_Access_Subprogram_Instance;
12329 -----------------------------------
12330 -- Validate_Access_Type_Instance --
12331 -----------------------------------
12333 procedure Validate_Access_Type_Instance is
12334 Desig_Type : constant Entity_Id :=
12335 Find_Actual_Type (Designated_Type (A_Gen_T), A_Gen_T);
12336 Desig_Act : Entity_Id;
12339 if not Is_Access_Type (Act_T) then
12341 ("expect access type in instantiation of &", Actual, Gen_T);
12342 Abandon_Instantiation (Actual);
12345 if Is_Access_Constant (A_Gen_T) then
12346 if not Is_Access_Constant (Act_T) then
12348 ("actual type must be access-to-constant type", Actual);
12349 Abandon_Instantiation (Actual);
12352 if Is_Access_Constant (Act_T) then
12354 ("actual type must be access-to-variable type", Actual);
12355 Abandon_Instantiation (Actual);
12357 elsif Ekind (A_Gen_T) = E_General_Access_Type
12358 and then Ekind (Base_Type (Act_T)) /= E_General_Access_Type
12360 Error_Msg_N -- CODEFIX
12361 ("actual must be general access type!", Actual);
12362 Error_Msg_NE -- CODEFIX
12363 ("add ALL to }!", Actual, Act_T);
12364 Abandon_Instantiation (Actual);
12368 -- The designated subtypes, that is to say the subtypes introduced
12369 -- by an access type declaration (and not by a subtype declaration)
12372 Desig_Act := Designated_Type (Base_Type (Act_T));
12374 -- The designated type may have been introduced through a limited_
12375 -- with clause, in which case retrieve the non-limited view. This
12376 -- applies to incomplete types as well as to class-wide types.
12378 if From_Limited_With (Desig_Act) then
12379 Desig_Act := Available_View (Desig_Act);
12382 if not Subtypes_Match (Desig_Type, Desig_Act) then
12384 ("designated type of actual does not match that of formal &",
12387 if not Predicates_Match (Desig_Type, Desig_Act) then
12388 Error_Msg_N ("\predicates do not match", Actual);
12391 Abandon_Instantiation (Actual);
12393 elsif Is_Access_Type (Designated_Type (Act_T))
12394 and then Is_Constrained (Designated_Type (Designated_Type (Act_T)))
12396 Is_Constrained (Designated_Type (Desig_Type))
12399 ("designated type of actual does not match that of formal &",
12402 if not Predicates_Match (Desig_Type, Desig_Act) then
12403 Error_Msg_N ("\predicates do not match", Actual);
12406 Abandon_Instantiation (Actual);
12409 -- Ada 2005: null-exclusion indicators of the two types must agree
12411 if Can_Never_Be_Null (A_Gen_T) /= Can_Never_Be_Null (Act_T) then
12413 ("non null exclusion of actual and formal & do not match",
12416 end Validate_Access_Type_Instance;
12418 ----------------------------------
12419 -- Validate_Array_Type_Instance --
12420 ----------------------------------
12422 procedure Validate_Array_Type_Instance is
12427 function Formal_Dimensions return Nat;
12428 -- Count number of dimensions in array type formal
12430 -----------------------
12431 -- Formal_Dimensions --
12432 -----------------------
12434 function Formal_Dimensions return Nat is
12439 if Nkind (Def) = N_Constrained_Array_Definition then
12440 Index := First (Discrete_Subtype_Definitions (Def));
12442 Index := First (Subtype_Marks (Def));
12445 while Present (Index) loop
12447 Next_Index (Index);
12451 end Formal_Dimensions;
12453 -- Start of processing for Validate_Array_Type_Instance
12456 if not Is_Array_Type (Act_T) then
12458 ("expect array type in instantiation of &", Actual, Gen_T);
12459 Abandon_Instantiation (Actual);
12461 elsif Nkind (Def) = N_Constrained_Array_Definition then
12462 if not (Is_Constrained (Act_T)) then
12464 ("expect constrained array in instantiation of &",
12466 Abandon_Instantiation (Actual);
12470 if Is_Constrained (Act_T) then
12472 ("expect unconstrained array in instantiation of &",
12474 Abandon_Instantiation (Actual);
12478 if Formal_Dimensions /= Number_Dimensions (Act_T) then
12480 ("dimensions of actual do not match formal &", Actual, Gen_T);
12481 Abandon_Instantiation (Actual);
12484 I1 := First_Index (A_Gen_T);
12485 I2 := First_Index (Act_T);
12486 for J in 1 .. Formal_Dimensions loop
12488 -- If the indexes of the actual were given by a subtype_mark,
12489 -- the index was transformed into a range attribute. Retrieve
12490 -- the original type mark for checking.
12492 if Is_Entity_Name (Original_Node (I2)) then
12493 T2 := Entity (Original_Node (I2));
12498 if not Subtypes_Match
12499 (Find_Actual_Type (Etype (I1), A_Gen_T), T2)
12502 ("index types of actual do not match those of formal &",
12504 Abandon_Instantiation (Actual);
12511 -- Check matching subtypes. Note that there are complex visibility
12512 -- issues when the generic is a child unit and some aspect of the
12513 -- generic type is declared in a parent unit of the generic. We do
12514 -- the test to handle this special case only after a direct check
12515 -- for static matching has failed. The case where both the component
12516 -- type and the array type are separate formals, and the component
12517 -- type is a private view may also require special checking in
12518 -- Subtypes_Match. Finally, we assume that a child instance where
12519 -- the component type comes from a formal of a parent instance is
12520 -- correct because the generic was correct. A more precise check
12521 -- seems too complex to install???
12524 (Component_Type (A_Gen_T), Component_Type (Act_T))
12527 (Find_Actual_Type (Component_Type (A_Gen_T), A_Gen_T),
12528 Component_Type (Act_T))
12530 (not Inside_A_Generic
12531 and then Is_Child_Unit (Scope (Component_Type (A_Gen_T))))
12536 ("component subtype of actual does not match that of formal &",
12538 Abandon_Instantiation (Actual);
12541 if Has_Aliased_Components (A_Gen_T)
12542 and then not Has_Aliased_Components (Act_T)
12545 ("actual must have aliased components to match formal type &",
12548 end Validate_Array_Type_Instance;
12550 -----------------------------------------------
12551 -- Validate_Derived_Interface_Type_Instance --
12552 -----------------------------------------------
12554 procedure Validate_Derived_Interface_Type_Instance is
12555 Par : constant Entity_Id := Entity (Subtype_Indication (Def));
12559 -- First apply interface instance checks
12561 Validate_Interface_Type_Instance;
12563 -- Verify that immediate parent interface is an ancestor of
12567 and then not Interface_Present_In_Ancestor (Act_T, Par)
12570 ("interface actual must include progenitor&", Actual, Par);
12573 -- Now verify that the actual includes all other ancestors of
12576 Elmt := First_Elmt (Interfaces (A_Gen_T));
12577 while Present (Elmt) loop
12578 if not Interface_Present_In_Ancestor
12579 (Act_T, Get_Instance_Of (Node (Elmt)))
12582 ("interface actual must include progenitor&",
12583 Actual, Node (Elmt));
12588 end Validate_Derived_Interface_Type_Instance;
12590 ------------------------------------
12591 -- Validate_Derived_Type_Instance --
12592 ------------------------------------
12594 procedure Validate_Derived_Type_Instance is
12595 Actual_Discr : Entity_Id;
12596 Ancestor_Discr : Entity_Id;
12599 -- Verify that the actual includes the progenitors of the formal,
12600 -- if any. The formal may depend on previous formals and their
12601 -- instance, so we must examine instance of interfaces if present.
12602 -- The actual may be an extension of an interface, in which case
12603 -- it does not appear in the interface list, so this must be
12604 -- checked separately.
12606 if Present (Interface_List (Def)) then
12607 if not Has_Interfaces (Act_T) then
12609 ("actual must implement all interfaces of formal&",
12614 Act_Iface_List : Elist_Id;
12616 Iface_Ent : Entity_Id;
12618 function Instance_Exists (I : Entity_Id) return Boolean;
12619 -- If the interface entity is declared in a generic unit,
12620 -- this can only be legal if we are within an instantiation
12621 -- of a child of that generic. There is currently no
12622 -- mechanism to relate an interface declared within a
12623 -- generic to the corresponding interface in an instance,
12624 -- so we traverse the list of interfaces of the actual,
12625 -- looking for a name match.
12627 ---------------------
12628 -- Instance_Exists --
12629 ---------------------
12631 function Instance_Exists (I : Entity_Id) return Boolean is
12632 Iface_Elmt : Elmt_Id;
12635 Iface_Elmt := First_Elmt (Act_Iface_List);
12636 while Present (Iface_Elmt) loop
12637 if Is_Generic_Instance (Scope (Node (Iface_Elmt)))
12638 and then Chars (Node (Iface_Elmt)) = Chars (I)
12643 Next_Elmt (Iface_Elmt);
12647 end Instance_Exists;
12650 Iface := First (Abstract_Interface_List (A_Gen_T));
12651 Collect_Interfaces (Act_T, Act_Iface_List);
12653 while Present (Iface) loop
12654 Iface_Ent := Get_Instance_Of (Entity (Iface));
12656 if Is_Ancestor (Iface_Ent, Act_T)
12657 or else Is_Progenitor (Iface_Ent, Act_T)
12661 elsif Ekind (Scope (Iface_Ent)) = E_Generic_Package
12662 and then Instance_Exists (Iface_Ent)
12667 Error_Msg_Name_1 := Chars (Act_T);
12669 ("Actual% must implement interface&",
12670 Actual, Etype (Iface));
12679 -- If the parent type in the generic declaration is itself a previous
12680 -- formal type, then it is local to the generic and absent from the
12681 -- analyzed generic definition. In that case the ancestor is the
12682 -- instance of the formal (which must have been instantiated
12683 -- previously), unless the ancestor is itself a formal derived type.
12684 -- In this latter case (which is the subject of Corrigendum 8652/0038
12685 -- (AI-202) the ancestor of the formals is the ancestor of its
12686 -- parent. Otherwise, the analyzed generic carries the parent type.
12687 -- If the parent type is defined in a previous formal package, then
12688 -- the scope of that formal package is that of the generic type
12689 -- itself, and it has already been mapped into the corresponding type
12690 -- in the actual package.
12692 -- Common case: parent type defined outside of the generic
12694 if Is_Entity_Name (Subtype_Mark (Def))
12695 and then Present (Entity (Subtype_Mark (Def)))
12697 Ancestor := Get_Instance_Of (Entity (Subtype_Mark (Def)));
12699 -- Check whether parent is defined in a previous formal package
12702 Scope (Scope (Base_Type (Etype (A_Gen_T)))) = Scope (A_Gen_T)
12705 Get_Instance_Of (Base_Type (Etype (A_Gen_T)));
12707 -- The type may be a local derivation, or a type extension of a
12708 -- previous formal, or of a formal of a parent package.
12710 elsif Is_Derived_Type (Get_Instance_Of (A_Gen_T))
12712 Ekind (Get_Instance_Of (A_Gen_T)) = E_Record_Type_With_Private
12714 -- Check whether the parent is another derived formal type in the
12715 -- same generic unit.
12717 if Etype (A_Gen_T) /= A_Gen_T
12718 and then Is_Generic_Type (Etype (A_Gen_T))
12719 and then Scope (Etype (A_Gen_T)) = Scope (A_Gen_T)
12720 and then Etype (Etype (A_Gen_T)) /= Etype (A_Gen_T)
12722 -- Locate ancestor of parent from the subtype declaration
12723 -- created for the actual.
12729 Decl := First (Actual_Decls);
12730 while Present (Decl) loop
12731 if Nkind (Decl) = N_Subtype_Declaration
12732 and then Chars (Defining_Identifier (Decl)) =
12733 Chars (Etype (A_Gen_T))
12735 Ancestor := Generic_Parent_Type (Decl);
12743 pragma Assert (Present (Ancestor));
12745 -- The ancestor itself may be a previous formal that has been
12748 Ancestor := Get_Instance_Of (Ancestor);
12752 Get_Instance_Of (Base_Type (Get_Instance_Of (A_Gen_T)));
12755 -- Check whether parent is a previous formal of the current generic
12757 elsif Is_Derived_Type (A_Gen_T)
12758 and then Is_Generic_Type (Etype (A_Gen_T))
12759 and then Scope (A_Gen_T) = Scope (Etype (A_Gen_T))
12761 Ancestor := Get_Instance_Of (First_Subtype (Etype (A_Gen_T)));
12763 -- An unusual case: the actual is a type declared in a parent unit,
12764 -- but is not a formal type so there is no instance_of for it.
12765 -- Retrieve it by analyzing the record extension.
12767 elsif Is_Child_Unit (Scope (A_Gen_T))
12768 and then In_Open_Scopes (Scope (Act_T))
12769 and then Is_Generic_Instance (Scope (Act_T))
12771 Analyze (Subtype_Mark (Def));
12772 Ancestor := Entity (Subtype_Mark (Def));
12775 Ancestor := Get_Instance_Of (Etype (Base_Type (A_Gen_T)));
12778 -- If the formal derived type has pragma Preelaborable_Initialization
12779 -- then the actual type must have preelaborable initialization.
12781 if Known_To_Have_Preelab_Init (A_Gen_T)
12782 and then not Has_Preelaborable_Initialization (Act_T)
12785 ("actual for & must have preelaborable initialization",
12789 -- Ada 2005 (AI-251)
12791 if Ada_Version >= Ada_2005 and then Is_Interface (Ancestor) then
12792 if not Interface_Present_In_Ancestor (Act_T, Ancestor) then
12794 ("(Ada 2005) expected type implementing & in instantiation",
12798 -- Finally verify that the (instance of) the ancestor is an ancestor
12801 elsif not Is_Ancestor (Base_Type (Ancestor), Act_T) then
12803 ("expect type derived from & in instantiation",
12804 Actual, First_Subtype (Ancestor));
12805 Abandon_Instantiation (Actual);
12808 -- Ada 2005 (AI-443): Synchronized formal derived type checks. Note
12809 -- that the formal type declaration has been rewritten as a private
12812 if Ada_Version >= Ada_2005
12813 and then Nkind (Parent (A_Gen_T)) = N_Private_Extension_Declaration
12814 and then Synchronized_Present (Parent (A_Gen_T))
12816 -- The actual must be a synchronized tagged type
12818 if not Is_Tagged_Type (Act_T) then
12820 ("actual of synchronized type must be tagged", Actual);
12821 Abandon_Instantiation (Actual);
12823 elsif Nkind (Parent (Act_T)) = N_Full_Type_Declaration
12824 and then Nkind (Type_Definition (Parent (Act_T))) =
12825 N_Derived_Type_Definition
12826 and then not Synchronized_Present
12827 (Type_Definition (Parent (Act_T)))
12830 ("actual of synchronized type must be synchronized", Actual);
12831 Abandon_Instantiation (Actual);
12835 -- Perform atomic/volatile checks (RM C.6(12)). Note that AI05-0218-1
12836 -- removes the second instance of the phrase "or allow pass by copy".
12838 -- In Ada_2020 the aspect may be specified explicitly for the formal
12839 -- regardless of whether an ancestor obeys it.
12841 if Is_Atomic (Act_T)
12842 and then not Is_Atomic (Ancestor)
12843 and then not Is_Atomic (A_Gen_T)
12846 ("cannot have atomic actual type for non-atomic formal type",
12849 elsif Is_Volatile (Act_T)
12850 and then not Is_Volatile (Ancestor)
12851 and then not Is_Volatile (A_Gen_T)
12854 ("cannot have volatile actual type for non-volatile formal type",
12858 -- It should not be necessary to check for unknown discriminants on
12859 -- Formal, but for some reason Has_Unknown_Discriminants is false for
12860 -- A_Gen_T, so Is_Definite_Subtype incorrectly returns True. This
12861 -- needs fixing. ???
12863 if Is_Definite_Subtype (A_Gen_T)
12864 and then not Unknown_Discriminants_Present (Formal)
12865 and then not Is_Definite_Subtype (Act_T)
12867 Error_Msg_N ("actual subtype must be constrained", Actual);
12868 Abandon_Instantiation (Actual);
12871 if not Unknown_Discriminants_Present (Formal) then
12872 if Is_Constrained (Ancestor) then
12873 if not Is_Constrained (Act_T) then
12874 Error_Msg_N ("actual subtype must be constrained", Actual);
12875 Abandon_Instantiation (Actual);
12878 -- Ancestor is unconstrained, Check if generic formal and actual
12879 -- agree on constrainedness. The check only applies to array types
12880 -- and discriminated types.
12882 elsif Is_Constrained (Act_T) then
12883 if Ekind (Ancestor) = E_Access_Type
12884 or else (not Is_Constrained (A_Gen_T)
12885 and then Is_Composite_Type (A_Gen_T))
12887 Error_Msg_N ("actual subtype must be unconstrained", Actual);
12888 Abandon_Instantiation (Actual);
12891 -- A class-wide type is only allowed if the formal has unknown
12894 elsif Is_Class_Wide_Type (Act_T)
12895 and then not Has_Unknown_Discriminants (Ancestor)
12898 ("actual for & cannot be a class-wide type", Actual, Gen_T);
12899 Abandon_Instantiation (Actual);
12901 -- Otherwise, the formal and actual must have the same number
12902 -- of discriminants and each discriminant of the actual must
12903 -- correspond to a discriminant of the formal.
12905 elsif Has_Discriminants (Act_T)
12906 and then not Has_Unknown_Discriminants (Act_T)
12907 and then Has_Discriminants (Ancestor)
12909 Actual_Discr := First_Discriminant (Act_T);
12910 Ancestor_Discr := First_Discriminant (Ancestor);
12911 while Present (Actual_Discr)
12912 and then Present (Ancestor_Discr)
12914 if Base_Type (Act_T) /= Base_Type (Ancestor) and then
12915 No (Corresponding_Discriminant (Actual_Discr))
12918 ("discriminant & does not correspond "
12919 & "to ancestor discriminant", Actual, Actual_Discr);
12920 Abandon_Instantiation (Actual);
12923 Next_Discriminant (Actual_Discr);
12924 Next_Discriminant (Ancestor_Discr);
12927 if Present (Actual_Discr) or else Present (Ancestor_Discr) then
12929 ("actual for & must have same number of discriminants",
12931 Abandon_Instantiation (Actual);
12934 -- This case should be caught by the earlier check for
12935 -- constrainedness, but the check here is added for completeness.
12937 elsif Has_Discriminants (Act_T)
12938 and then not Has_Unknown_Discriminants (Act_T)
12941 ("actual for & must not have discriminants", Actual, Gen_T);
12942 Abandon_Instantiation (Actual);
12944 elsif Has_Discriminants (Ancestor) then
12946 ("actual for & must have known discriminants", Actual, Gen_T);
12947 Abandon_Instantiation (Actual);
12950 if not Subtypes_Statically_Compatible
12951 (Act_T, Ancestor, Formal_Derived_Matching => True)
12954 ("constraint on actual is incompatible with formal", Actual);
12955 Abandon_Instantiation (Actual);
12959 -- If the formal and actual types are abstract, check that there
12960 -- are no abstract primitives of the actual type that correspond to
12961 -- nonabstract primitives of the formal type (second sentence of
12964 if Is_Abstract_Type (A_Gen_T) and then Is_Abstract_Type (Act_T) then
12965 Check_Abstract_Primitives : declare
12966 Gen_Prims : constant Elist_Id :=
12967 Primitive_Operations (A_Gen_T);
12968 Gen_Elmt : Elmt_Id;
12969 Gen_Subp : Entity_Id;
12970 Anc_Subp : Entity_Id;
12971 Anc_Formal : Entity_Id;
12972 Anc_F_Type : Entity_Id;
12974 Act_Prims : constant Elist_Id := Primitive_Operations (Act_T);
12975 Act_Elmt : Elmt_Id;
12976 Act_Subp : Entity_Id;
12977 Act_Formal : Entity_Id;
12978 Act_F_Type : Entity_Id;
12980 Subprograms_Correspond : Boolean;
12982 function Is_Tagged_Ancestor (T1, T2 : Entity_Id) return Boolean;
12983 -- Returns true if T2 is derived directly or indirectly from
12984 -- T1, including derivations from interfaces. T1 and T2 are
12985 -- required to be specific tagged base types.
12987 ------------------------
12988 -- Is_Tagged_Ancestor --
12989 ------------------------
12991 function Is_Tagged_Ancestor (T1, T2 : Entity_Id) return Boolean
12993 Intfc_Elmt : Elmt_Id;
12996 -- The predicate is satisfied if the types are the same
13001 -- If we've reached the top of the derivation chain then
13002 -- we know that T1 is not an ancestor of T2.
13004 elsif Etype (T2) = T2 then
13007 -- Proceed to check T2's immediate parent
13009 elsif Is_Ancestor (T1, Base_Type (Etype (T2))) then
13012 -- Finally, check to see if T1 is an ancestor of any of T2's
13016 Intfc_Elmt := First_Elmt (Interfaces (T2));
13017 while Present (Intfc_Elmt) loop
13018 if Is_Ancestor (T1, Node (Intfc_Elmt)) then
13022 Next_Elmt (Intfc_Elmt);
13027 end Is_Tagged_Ancestor;
13029 -- Start of processing for Check_Abstract_Primitives
13032 -- Loop over all of the formal derived type's primitives
13034 Gen_Elmt := First_Elmt (Gen_Prims);
13035 while Present (Gen_Elmt) loop
13036 Gen_Subp := Node (Gen_Elmt);
13038 -- If the primitive of the formal is not abstract, then
13039 -- determine whether there is a corresponding primitive of
13040 -- the actual type that's abstract.
13042 if not Is_Abstract_Subprogram (Gen_Subp) then
13043 Act_Elmt := First_Elmt (Act_Prims);
13044 while Present (Act_Elmt) loop
13045 Act_Subp := Node (Act_Elmt);
13047 -- If we find an abstract primitive of the actual,
13048 -- then we need to test whether it corresponds to the
13049 -- subprogram from which the generic formal primitive
13052 if Is_Abstract_Subprogram (Act_Subp) then
13053 Anc_Subp := Alias (Gen_Subp);
13055 -- Test whether we have a corresponding primitive
13056 -- by comparing names, kinds, formal types, and
13059 if Chars (Anc_Subp) = Chars (Act_Subp)
13060 and then Ekind (Anc_Subp) = Ekind (Act_Subp)
13062 Anc_Formal := First_Formal (Anc_Subp);
13063 Act_Formal := First_Formal (Act_Subp);
13064 while Present (Anc_Formal)
13065 and then Present (Act_Formal)
13067 Anc_F_Type := Etype (Anc_Formal);
13068 Act_F_Type := Etype (Act_Formal);
13070 if Ekind (Anc_F_Type) =
13071 E_Anonymous_Access_Type
13073 Anc_F_Type := Designated_Type (Anc_F_Type);
13075 if Ekind (Act_F_Type) =
13076 E_Anonymous_Access_Type
13079 Designated_Type (Act_F_Type);
13085 Ekind (Act_F_Type) = E_Anonymous_Access_Type
13090 Anc_F_Type := Base_Type (Anc_F_Type);
13091 Act_F_Type := Base_Type (Act_F_Type);
13093 -- If the formal is controlling, then the
13094 -- the type of the actual primitive's formal
13095 -- must be derived directly or indirectly
13096 -- from the type of the ancestor primitive's
13099 if Is_Controlling_Formal (Anc_Formal) then
13100 if not Is_Tagged_Ancestor
13101 (Anc_F_Type, Act_F_Type)
13106 -- Otherwise the types of the formals must
13109 elsif Anc_F_Type /= Act_F_Type then
13113 Next_Entity (Anc_Formal);
13114 Next_Entity (Act_Formal);
13117 -- If we traversed through all of the formals
13118 -- then so far the subprograms correspond, so
13119 -- now check that any result types correspond.
13121 if No (Anc_Formal) and then No (Act_Formal) then
13122 Subprograms_Correspond := True;
13124 if Ekind (Act_Subp) = E_Function then
13125 Anc_F_Type := Etype (Anc_Subp);
13126 Act_F_Type := Etype (Act_Subp);
13128 if Ekind (Anc_F_Type) =
13129 E_Anonymous_Access_Type
13132 Designated_Type (Anc_F_Type);
13134 if Ekind (Act_F_Type) =
13135 E_Anonymous_Access_Type
13138 Designated_Type (Act_F_Type);
13140 Subprograms_Correspond := False;
13145 = E_Anonymous_Access_Type
13147 Subprograms_Correspond := False;
13150 Anc_F_Type := Base_Type (Anc_F_Type);
13151 Act_F_Type := Base_Type (Act_F_Type);
13153 -- Now either the result types must be
13154 -- the same or, if the result type is
13155 -- controlling, the result type of the
13156 -- actual primitive must descend from the
13157 -- result type of the ancestor primitive.
13159 if Subprograms_Correspond
13160 and then Anc_F_Type /= Act_F_Type
13162 Has_Controlling_Result (Anc_Subp)
13163 and then not Is_Tagged_Ancestor
13164 (Anc_F_Type, Act_F_Type)
13166 Subprograms_Correspond := False;
13170 -- Found a matching subprogram belonging to
13171 -- formal ancestor type, so actual subprogram
13172 -- corresponds and this violates 3.9.3(9).
13174 if Subprograms_Correspond then
13176 ("abstract subprogram & overrides "
13177 & "nonabstract subprogram of ancestor",
13184 Next_Elmt (Act_Elmt);
13188 Next_Elmt (Gen_Elmt);
13190 end Check_Abstract_Primitives;
13193 -- Verify that limitedness matches. If parent is a limited
13194 -- interface then the generic formal is not unless declared
13195 -- explicitly so. If not declared limited, the actual cannot be
13196 -- limited (see AI05-0087).
13198 -- Even though this AI is a binding interpretation, we enable the
13199 -- check only in Ada 2012 mode, because this improper construct
13200 -- shows up in user code and in existing B-tests.
13202 if Is_Limited_Type (Act_T)
13203 and then not Is_Limited_Type (A_Gen_T)
13204 and then Ada_Version >= Ada_2012
13206 if In_Instance then
13210 ("actual for non-limited & cannot be a limited type",
13212 Explain_Limited_Type (Act_T, Actual);
13213 Abandon_Instantiation (Actual);
13217 -- Don't check Ada_Version here (for now) because AI12-0036 is
13218 -- a binding interpretation; this decision may be reversed if
13219 -- the situation turns out to be similar to that of the preceding
13220 -- Is_Limited_Type test (see preceding comment).
13223 Formal_Is_Private_Extension : constant Boolean :=
13224 Nkind (Parent (A_Gen_T)) = N_Private_Extension_Declaration;
13226 Actual_Is_Tagged : constant Boolean := Is_Tagged_Type (Act_T);
13228 if Actual_Is_Tagged /= Formal_Is_Private_Extension then
13229 if In_Instance then
13232 if Actual_Is_Tagged then
13234 ("actual for & cannot be a tagged type",
13238 ("actual for & must be a tagged type",
13241 Abandon_Instantiation (Actual);
13245 end Validate_Derived_Type_Instance;
13247 ----------------------------------------
13248 -- Validate_Discriminated_Formal_Type --
13249 ----------------------------------------
13251 procedure Validate_Discriminated_Formal_Type is
13252 Formal_Discr : Entity_Id;
13253 Actual_Discr : Entity_Id;
13254 Formal_Subt : Entity_Id;
13257 if Has_Discriminants (A_Gen_T) then
13258 if not Has_Discriminants (Act_T) then
13260 ("actual for & must have discriminants", Actual, Gen_T);
13261 Abandon_Instantiation (Actual);
13263 elsif Is_Constrained (Act_T) then
13265 ("actual for & must be unconstrained", Actual, Gen_T);
13266 Abandon_Instantiation (Actual);
13269 Formal_Discr := First_Discriminant (A_Gen_T);
13270 Actual_Discr := First_Discriminant (Act_T);
13271 while Formal_Discr /= Empty loop
13272 if Actual_Discr = Empty then
13274 ("discriminants on actual do not match formal",
13276 Abandon_Instantiation (Actual);
13279 Formal_Subt := Get_Instance_Of (Etype (Formal_Discr));
13281 -- Access discriminants match if designated types do
13283 if Ekind (Base_Type (Formal_Subt)) = E_Anonymous_Access_Type
13284 and then (Ekind (Base_Type (Etype (Actual_Discr)))) =
13285 E_Anonymous_Access_Type
13288 (Designated_Type (Base_Type (Formal_Subt))) =
13289 Designated_Type (Base_Type (Etype (Actual_Discr)))
13293 elsif Base_Type (Formal_Subt) /=
13294 Base_Type (Etype (Actual_Discr))
13297 ("types of actual discriminants must match formal",
13299 Abandon_Instantiation (Actual);
13301 elsif not Subtypes_Statically_Match
13302 (Formal_Subt, Etype (Actual_Discr))
13303 and then Ada_Version >= Ada_95
13306 ("subtypes of actual discriminants must match formal",
13308 Abandon_Instantiation (Actual);
13311 Next_Discriminant (Formal_Discr);
13312 Next_Discriminant (Actual_Discr);
13315 if Actual_Discr /= Empty then
13317 ("discriminants on actual do not match formal",
13319 Abandon_Instantiation (Actual);
13323 end Validate_Discriminated_Formal_Type;
13325 ---------------------------------------
13326 -- Validate_Incomplete_Type_Instance --
13327 ---------------------------------------
13329 procedure Validate_Incomplete_Type_Instance is
13331 if not Is_Tagged_Type (Act_T)
13332 and then Is_Tagged_Type (A_Gen_T)
13335 ("actual for & must be a tagged type", Actual, Gen_T);
13338 Validate_Discriminated_Formal_Type;
13339 end Validate_Incomplete_Type_Instance;
13341 --------------------------------------
13342 -- Validate_Interface_Type_Instance --
13343 --------------------------------------
13345 procedure Validate_Interface_Type_Instance is
13347 if not Is_Interface (Act_T) then
13349 ("actual for formal interface type must be an interface",
13352 elsif Is_Limited_Type (Act_T) /= Is_Limited_Type (A_Gen_T)
13353 or else Is_Task_Interface (A_Gen_T) /= Is_Task_Interface (Act_T)
13354 or else Is_Protected_Interface (A_Gen_T) /=
13355 Is_Protected_Interface (Act_T)
13356 or else Is_Synchronized_Interface (A_Gen_T) /=
13357 Is_Synchronized_Interface (Act_T)
13360 ("actual for interface& does not match (RM 12.5.5(4))",
13363 end Validate_Interface_Type_Instance;
13365 ------------------------------------
13366 -- Validate_Private_Type_Instance --
13367 ------------------------------------
13369 procedure Validate_Private_Type_Instance is
13371 if Is_Limited_Type (Act_T)
13372 and then not Is_Limited_Type (A_Gen_T)
13374 if In_Instance then
13378 ("actual for non-limited & cannot be a limited type", Actual,
13380 Explain_Limited_Type (Act_T, Actual);
13381 Abandon_Instantiation (Actual);
13384 elsif Known_To_Have_Preelab_Init (A_Gen_T)
13385 and then not Has_Preelaborable_Initialization (Act_T)
13388 ("actual for & must have preelaborable initialization", Actual,
13391 elsif not Is_Definite_Subtype (Act_T)
13392 and then Is_Definite_Subtype (A_Gen_T)
13393 and then Ada_Version >= Ada_95
13396 ("actual for & must be a definite subtype", Actual, Gen_T);
13398 elsif not Is_Tagged_Type (Act_T)
13399 and then Is_Tagged_Type (A_Gen_T)
13402 ("actual for & must be a tagged type", Actual, Gen_T);
13405 Validate_Discriminated_Formal_Type;
13407 end Validate_Private_Type_Instance;
13409 -- Start of processing for Instantiate_Type
13412 if Get_Instance_Of (A_Gen_T) /= A_Gen_T then
13413 Error_Msg_N ("duplicate instantiation of generic type", Actual);
13414 return New_List (Error);
13416 elsif not Is_Entity_Name (Actual)
13417 or else not Is_Type (Entity (Actual))
13420 ("expect valid subtype mark to instantiate &", Actual, Gen_T);
13421 Abandon_Instantiation (Actual);
13424 Act_T := Entity (Actual);
13426 -- Ada 2005 (AI-216): An Unchecked_Union subtype shall only be passed
13427 -- as a generic actual parameter if the corresponding formal type
13428 -- does not have a known_discriminant_part, or is a formal derived
13429 -- type that is an Unchecked_Union type.
13431 if Is_Unchecked_Union (Base_Type (Act_T)) then
13432 if not Has_Discriminants (A_Gen_T)
13433 or else (Is_Derived_Type (A_Gen_T)
13434 and then Is_Unchecked_Union (A_Gen_T))
13438 Error_Msg_N ("unchecked union cannot be the actual for a "
13439 & "discriminated formal type", Act_T);
13444 -- Deal with fixed/floating restrictions
13446 if Is_Floating_Point_Type (Act_T) then
13447 Check_Restriction (No_Floating_Point, Actual);
13448 elsif Is_Fixed_Point_Type (Act_T) then
13449 Check_Restriction (No_Fixed_Point, Actual);
13452 -- Deal with error of using incomplete type as generic actual.
13453 -- This includes limited views of a type, even if the non-limited
13454 -- view may be available.
13456 if Ekind (Act_T) = E_Incomplete_Type
13457 or else (Is_Class_Wide_Type (Act_T)
13458 and then Ekind (Root_Type (Act_T)) = E_Incomplete_Type)
13460 -- If the formal is an incomplete type, the actual can be
13461 -- incomplete as well.
13463 if Ekind (A_Gen_T) = E_Incomplete_Type then
13466 elsif Is_Class_Wide_Type (Act_T)
13467 or else No (Full_View (Act_T))
13469 Error_Msg_N ("premature use of incomplete type", Actual);
13470 Abandon_Instantiation (Actual);
13472 Act_T := Full_View (Act_T);
13473 Set_Entity (Actual, Act_T);
13475 if Has_Private_Component (Act_T) then
13477 ("premature use of type with private component", Actual);
13481 -- Deal with error of premature use of private type as generic actual
13483 elsif Is_Private_Type (Act_T)
13484 and then Is_Private_Type (Base_Type (Act_T))
13485 and then not Is_Generic_Type (Act_T)
13486 and then not Is_Derived_Type (Act_T)
13487 and then No (Full_View (Root_Type (Act_T)))
13489 -- If the formal is an incomplete type, the actual can be
13490 -- private or incomplete as well.
13492 if Ekind (A_Gen_T) = E_Incomplete_Type then
13495 Error_Msg_N ("premature use of private type", Actual);
13498 elsif Has_Private_Component (Act_T) then
13500 ("premature use of type with private component", Actual);
13503 Set_Instance_Of (A_Gen_T, Act_T);
13505 -- If the type is generic, the class-wide type may also be used
13507 if Is_Tagged_Type (A_Gen_T)
13508 and then Is_Tagged_Type (Act_T)
13509 and then not Is_Class_Wide_Type (A_Gen_T)
13511 Set_Instance_Of (Class_Wide_Type (A_Gen_T),
13512 Class_Wide_Type (Act_T));
13515 if not Is_Abstract_Type (A_Gen_T)
13516 and then Is_Abstract_Type (Act_T)
13519 ("actual of non-abstract formal cannot be abstract", Actual);
13522 -- A generic scalar type is a first subtype for which we generate
13523 -- an anonymous base type. Indicate that the instance of this base
13524 -- is the base type of the actual.
13526 if Is_Scalar_Type (A_Gen_T) then
13527 Set_Instance_Of (Etype (A_Gen_T), Etype (Act_T));
13531 Check_Shared_Variable_Control_Aspects;
13533 if Error_Posted (Act_T) then
13536 case Nkind (Def) is
13537 when N_Formal_Private_Type_Definition =>
13538 Validate_Private_Type_Instance;
13540 when N_Formal_Incomplete_Type_Definition =>
13541 Validate_Incomplete_Type_Instance;
13543 when N_Formal_Derived_Type_Definition =>
13544 Validate_Derived_Type_Instance;
13546 when N_Formal_Discrete_Type_Definition =>
13547 if not Is_Discrete_Type (Act_T) then
13549 ("expect discrete type in instantiation of&",
13551 Abandon_Instantiation (Actual);
13554 Diagnose_Predicated_Actual;
13556 when N_Formal_Signed_Integer_Type_Definition =>
13557 if not Is_Signed_Integer_Type (Act_T) then
13559 ("expect signed integer type in instantiation of&",
13561 Abandon_Instantiation (Actual);
13564 Diagnose_Predicated_Actual;
13566 when N_Formal_Modular_Type_Definition =>
13567 if not Is_Modular_Integer_Type (Act_T) then
13569 ("expect modular type in instantiation of &",
13571 Abandon_Instantiation (Actual);
13574 Diagnose_Predicated_Actual;
13576 when N_Formal_Floating_Point_Definition =>
13577 if not Is_Floating_Point_Type (Act_T) then
13579 ("expect float type in instantiation of &", Actual, Gen_T);
13580 Abandon_Instantiation (Actual);
13583 when N_Formal_Ordinary_Fixed_Point_Definition =>
13584 if not Is_Ordinary_Fixed_Point_Type (Act_T) then
13586 ("expect ordinary fixed point type in instantiation of &",
13588 Abandon_Instantiation (Actual);
13591 when N_Formal_Decimal_Fixed_Point_Definition =>
13592 if not Is_Decimal_Fixed_Point_Type (Act_T) then
13594 ("expect decimal type in instantiation of &",
13596 Abandon_Instantiation (Actual);
13599 when N_Array_Type_Definition =>
13600 Validate_Array_Type_Instance;
13602 when N_Access_To_Object_Definition =>
13603 Validate_Access_Type_Instance;
13605 when N_Access_Function_Definition
13606 | N_Access_Procedure_Definition
13608 Validate_Access_Subprogram_Instance;
13610 when N_Record_Definition =>
13611 Validate_Interface_Type_Instance;
13613 when N_Derived_Type_Definition =>
13614 Validate_Derived_Interface_Type_Instance;
13617 raise Program_Error;
13621 Subt := New_Copy (Gen_T);
13623 -- Use adjusted sloc of subtype name as the location for other nodes in
13624 -- the subtype declaration.
13626 Loc := Sloc (Subt);
13629 Make_Subtype_Declaration (Loc,
13630 Defining_Identifier => Subt,
13631 Subtype_Indication => New_Occurrence_Of (Act_T, Loc));
13633 -- Record whether the actual is private at this point, so that
13634 -- Check_Generic_Actuals can restore its proper view before the
13635 -- semantic analysis of the instance.
13637 if Is_Private_Type (Act_T) then
13638 Set_Has_Private_View (Subtype_Indication (Decl_Node));
13641 -- In Ada 2012 the actual may be a limited view. Indicate that
13642 -- the local subtype must be treated as such.
13644 if From_Limited_With (Act_T) then
13645 Set_Ekind (Subt, E_Incomplete_Subtype);
13646 Set_From_Limited_With (Subt);
13649 Decl_Nodes := New_List (Decl_Node);
13651 -- Flag actual derived types so their elaboration produces the
13652 -- appropriate renamings for the primitive operations of the ancestor.
13653 -- Flag actual for formal private types as well, to determine whether
13654 -- operations in the private part may override inherited operations.
13655 -- If the formal has an interface list, the ancestor is not the
13656 -- parent, but the analyzed formal that includes the interface
13657 -- operations of all its progenitors.
13659 -- Same treatment for formal private types, so we can check whether the
13660 -- type is tagged limited when validating derivations in the private
13661 -- part. (See AI05-096).
13663 if Nkind (Def) = N_Formal_Derived_Type_Definition then
13664 if Present (Interface_List (Def)) then
13665 Set_Generic_Parent_Type (Decl_Node, A_Gen_T);
13667 Set_Generic_Parent_Type (Decl_Node, Ancestor);
13670 elsif Nkind_In (Def, N_Formal_Private_Type_Definition,
13671 N_Formal_Incomplete_Type_Definition)
13673 Set_Generic_Parent_Type (Decl_Node, A_Gen_T);
13676 -- If the actual is a synchronized type that implements an interface,
13677 -- the primitive operations are attached to the corresponding record,
13678 -- and we have to treat it as an additional generic actual, so that its
13679 -- primitive operations become visible in the instance. The task or
13680 -- protected type itself does not carry primitive operations.
13682 if Is_Concurrent_Type (Act_T)
13683 and then Is_Tagged_Type (Act_T)
13684 and then Present (Corresponding_Record_Type (Act_T))
13685 and then Present (Ancestor)
13686 and then Is_Interface (Ancestor)
13689 Corr_Rec : constant Entity_Id :=
13690 Corresponding_Record_Type (Act_T);
13691 New_Corr : Entity_Id;
13692 Corr_Decl : Node_Id;
13695 New_Corr := Make_Temporary (Loc, 'S');
13697 Make_Subtype_Declaration (Loc,
13698 Defining_Identifier => New_Corr,
13699 Subtype_Indication =>
13700 New_Occurrence_Of (Corr_Rec, Loc));
13701 Append_To (Decl_Nodes, Corr_Decl);
13703 if Ekind (Act_T) = E_Task_Type then
13704 Set_Ekind (Subt, E_Task_Subtype);
13706 Set_Ekind (Subt, E_Protected_Subtype);
13709 Set_Corresponding_Record_Type (Subt, Corr_Rec);
13710 Set_Generic_Parent_Type (Corr_Decl, Ancestor);
13711 Set_Generic_Parent_Type (Decl_Node, Empty);
13715 -- For a floating-point type, capture dimension info if any, because
13716 -- the generated subtype declaration does not come from source and
13717 -- will not process dimensions.
13719 if Is_Floating_Point_Type (Act_T) then
13720 Copy_Dimensions (Act_T, Subt);
13724 end Instantiate_Type;
13726 ---------------------
13727 -- Is_In_Main_Unit --
13728 ---------------------
13730 function Is_In_Main_Unit (N : Node_Id) return Boolean is
13731 Unum : constant Unit_Number_Type := Get_Source_Unit (N);
13732 Current_Unit : Node_Id;
13735 if Unum = Main_Unit then
13738 -- If the current unit is a subunit then it is either the main unit or
13739 -- is being compiled as part of the main unit.
13741 elsif Nkind (N) = N_Compilation_Unit then
13742 return Nkind (Unit (N)) = N_Subunit;
13745 Current_Unit := Parent (N);
13746 while Present (Current_Unit)
13747 and then Nkind (Current_Unit) /= N_Compilation_Unit
13749 Current_Unit := Parent (Current_Unit);
13752 -- The instantiation node is in the main unit, or else the current node
13753 -- (perhaps as the result of nested instantiations) is in the main unit,
13754 -- or in the declaration of the main unit, which in this last case must
13758 Current_Unit = Cunit (Main_Unit)
13759 or else Current_Unit = Library_Unit (Cunit (Main_Unit))
13760 or else (Present (Current_Unit)
13761 and then Present (Library_Unit (Current_Unit))
13762 and then Is_In_Main_Unit (Library_Unit (Current_Unit)));
13763 end Is_In_Main_Unit;
13765 ----------------------------
13766 -- Load_Parent_Of_Generic --
13767 ----------------------------
13769 procedure Load_Parent_Of_Generic
13772 Body_Optional : Boolean := False)
13774 Comp_Unit : constant Node_Id := Cunit (Get_Source_Unit (Spec));
13775 Saved_Style_Check : constant Boolean := Style_Check;
13776 Saved_Warnings : constant Warning_Record := Save_Warnings;
13777 True_Parent : Node_Id;
13778 Inst_Node : Node_Id;
13780 Previous_Instances : constant Elist_Id := New_Elmt_List;
13782 procedure Collect_Previous_Instances (Decls : List_Id);
13783 -- Collect all instantiations in the given list of declarations, that
13784 -- precede the generic that we need to load. If the bodies of these
13785 -- instantiations are available, we must analyze them, to ensure that
13786 -- the public symbols generated are the same when the unit is compiled
13787 -- to generate code, and when it is compiled in the context of a unit
13788 -- that needs a particular nested instance. This process is applied to
13789 -- both package and subprogram instances.
13791 --------------------------------
13792 -- Collect_Previous_Instances --
13793 --------------------------------
13795 procedure Collect_Previous_Instances (Decls : List_Id) is
13799 Decl := First (Decls);
13800 while Present (Decl) loop
13801 if Sloc (Decl) >= Sloc (Inst_Node) then
13804 -- If Decl is an instantiation, then record it as requiring
13805 -- instantiation of the corresponding body, except if it is an
13806 -- abbreviated instantiation generated internally for conformance
13807 -- checking purposes only for the case of a formal package
13808 -- declared without a box (see Instantiate_Formal_Package). Such
13809 -- an instantiation does not generate any code (the actual code
13810 -- comes from actual) and thus does not need to be analyzed here.
13811 -- If the instantiation appears with a generic package body it is
13812 -- not analyzed here either.
13814 elsif Nkind (Decl) = N_Package_Instantiation
13815 and then not Is_Internal (Defining_Entity (Decl))
13817 Append_Elmt (Decl, Previous_Instances);
13819 -- For a subprogram instantiation, omit instantiations intrinsic
13820 -- operations (Unchecked_Conversions, etc.) that have no bodies.
13822 elsif Nkind_In (Decl, N_Function_Instantiation,
13823 N_Procedure_Instantiation)
13824 and then not Is_Intrinsic_Subprogram (Entity (Name (Decl)))
13826 Append_Elmt (Decl, Previous_Instances);
13828 elsif Nkind (Decl) = N_Package_Declaration then
13829 Collect_Previous_Instances
13830 (Visible_Declarations (Specification (Decl)));
13831 Collect_Previous_Instances
13832 (Private_Declarations (Specification (Decl)));
13834 -- Previous non-generic bodies may contain instances as well
13836 elsif Nkind (Decl) = N_Package_Body
13837 and then Ekind (Corresponding_Spec (Decl)) /= E_Generic_Package
13839 Collect_Previous_Instances (Declarations (Decl));
13841 elsif Nkind (Decl) = N_Subprogram_Body
13842 and then not Acts_As_Spec (Decl)
13843 and then not Is_Generic_Subprogram (Corresponding_Spec (Decl))
13845 Collect_Previous_Instances (Declarations (Decl));
13850 end Collect_Previous_Instances;
13852 -- Start of processing for Load_Parent_Of_Generic
13855 if not In_Same_Source_Unit (N, Spec)
13856 or else Nkind (Unit (Comp_Unit)) = N_Package_Declaration
13857 or else (Nkind (Unit (Comp_Unit)) = N_Package_Body
13858 and then not Is_In_Main_Unit (Spec))
13860 -- Find body of parent of spec, and analyze it. A special case arises
13861 -- when the parent is an instantiation, that is to say when we are
13862 -- currently instantiating a nested generic. In that case, there is
13863 -- no separate file for the body of the enclosing instance. Instead,
13864 -- the enclosing body must be instantiated as if it were a pending
13865 -- instantiation, in order to produce the body for the nested generic
13866 -- we require now. Note that in that case the generic may be defined
13867 -- in a package body, the instance defined in the same package body,
13868 -- and the original enclosing body may not be in the main unit.
13870 Inst_Node := Empty;
13872 True_Parent := Parent (Spec);
13873 while Present (True_Parent)
13874 and then Nkind (True_Parent) /= N_Compilation_Unit
13876 if Nkind (True_Parent) = N_Package_Declaration
13878 Nkind (Original_Node (True_Parent)) = N_Package_Instantiation
13880 -- Parent is a compilation unit that is an instantiation, and
13881 -- instantiation node has been replaced with package decl.
13883 Inst_Node := Original_Node (True_Parent);
13886 elsif Nkind (True_Parent) = N_Package_Declaration
13887 and then Nkind (Parent (True_Parent)) = N_Compilation_Unit
13889 Nkind (Unit (Parent (True_Parent))) = N_Package_Instantiation
13891 -- Parent is a compilation unit that is an instantiation, but
13892 -- instantiation node has not been replaced with package decl.
13894 Inst_Node := Unit (Parent (True_Parent));
13897 elsif Nkind (True_Parent) = N_Package_Declaration
13898 and then Nkind (Parent (True_Parent)) /= N_Compilation_Unit
13899 and then Present (Generic_Parent (Specification (True_Parent)))
13901 -- Parent is an instantiation within another specification.
13902 -- Declaration for instance has been inserted before original
13903 -- instantiation node. A direct link would be preferable?
13905 Inst_Node := Next (True_Parent);
13906 while Present (Inst_Node)
13907 and then Nkind (Inst_Node) /= N_Package_Instantiation
13912 -- If the instance appears within a generic, and the generic
13913 -- unit is defined within a formal package of the enclosing
13914 -- generic, there is no generic body available, and none
13915 -- needed. A more precise test should be used ???
13917 if No (Inst_Node) then
13924 True_Parent := Parent (True_Parent);
13928 -- Case where we are currently instantiating a nested generic
13930 if Present (Inst_Node) then
13931 if Nkind (Parent (True_Parent)) = N_Compilation_Unit then
13933 -- Instantiation node and declaration of instantiated package
13934 -- were exchanged when only the declaration was needed.
13935 -- Restore instantiation node before proceeding with body.
13937 Set_Unit (Parent (True_Parent), Inst_Node);
13940 -- Now complete instantiation of enclosing body, if it appears in
13941 -- some other unit. If it appears in the current unit, the body
13942 -- will have been instantiated already.
13944 if No (Corresponding_Body (Instance_Spec (Inst_Node))) then
13946 -- We need to determine the expander mode to instantiate the
13947 -- enclosing body. Because the generic body we need may use
13948 -- global entities declared in the enclosing package (including
13949 -- aggregates) it is in general necessary to compile this body
13950 -- with expansion enabled, except if we are within a generic
13951 -- package, in which case the usual generic rule applies.
13954 Exp_Status : Boolean := True;
13958 -- Loop through scopes looking for generic package
13960 Scop := Scope (Defining_Entity (Instance_Spec (Inst_Node)));
13961 while Present (Scop)
13962 and then Scop /= Standard_Standard
13964 if Ekind (Scop) = E_Generic_Package then
13965 Exp_Status := False;
13969 Scop := Scope (Scop);
13972 -- Collect previous instantiations in the unit that contains
13973 -- the desired generic.
13975 if Nkind (Parent (True_Parent)) /= N_Compilation_Unit
13976 and then not Body_Optional
13980 Info : Pending_Body_Info;
13984 Par := Parent (Inst_Node);
13985 while Present (Par) loop
13986 exit when Nkind (Parent (Par)) = N_Compilation_Unit;
13987 Par := Parent (Par);
13990 pragma Assert (Present (Par));
13992 if Nkind (Par) = N_Package_Body then
13993 Collect_Previous_Instances (Declarations (Par));
13995 elsif Nkind (Par) = N_Package_Declaration then
13996 Collect_Previous_Instances
13997 (Visible_Declarations (Specification (Par)));
13998 Collect_Previous_Instances
13999 (Private_Declarations (Specification (Par)));
14002 -- Enclosing unit is a subprogram body. In this
14003 -- case all instance bodies are processed in order
14004 -- and there is no need to collect them separately.
14009 Decl := First_Elmt (Previous_Instances);
14010 while Present (Decl) loop
14013 Instance_Spec (Node (Decl)),
14014 Config_Switches => Save_Config_Switches,
14015 Current_Sem_Unit =>
14016 Get_Code_Unit (Sloc (Node (Decl))),
14017 Expander_Status => Exp_Status,
14018 Inst_Node => Node (Decl),
14019 Local_Suppress_Stack_Top =>
14020 Local_Suppress_Stack_Top,
14021 Scope_Suppress => Scope_Suppress,
14022 Warnings => Save_Warnings);
14024 -- Package instance
14026 if Nkind (Node (Decl)) = N_Package_Instantiation
14028 Instantiate_Package_Body
14029 (Info, Body_Optional => True);
14031 -- Subprogram instance
14034 -- The instance_spec is in the wrapper package,
14035 -- usually followed by its local renaming
14036 -- declaration. See Build_Subprogram_Renaming
14037 -- for details. If the instance carries aspects,
14038 -- these result in the corresponding pragmas,
14039 -- inserted after the subprogram declaration.
14040 -- They must be skipped as well when retrieving
14041 -- the desired spec. Some of them may have been
14042 -- rewritten as null statements.
14043 -- A direct link would be more robust ???
14047 (Last (Visible_Declarations
14048 (Specification (Info.Act_Decl))));
14050 while Nkind_In (Decl,
14053 N_Subprogram_Renaming_Declaration)
14055 Decl := Prev (Decl);
14058 Info.Act_Decl := Decl;
14061 Instantiate_Subprogram_Body
14062 (Info, Body_Optional => True);
14070 Instantiate_Package_Body
14072 ((Act_Decl => True_Parent,
14073 Config_Switches => Save_Config_Switches,
14074 Current_Sem_Unit =>
14075 Get_Code_Unit (Sloc (Inst_Node)),
14076 Expander_Status => Exp_Status,
14077 Inst_Node => Inst_Node,
14078 Local_Suppress_Stack_Top => Local_Suppress_Stack_Top,
14079 Scope_Suppress => Scope_Suppress,
14080 Warnings => Save_Warnings)),
14081 Body_Optional => Body_Optional);
14085 -- Case where we are not instantiating a nested generic
14088 Opt.Style_Check := False;
14089 Expander_Mode_Save_And_Set (True);
14090 Load_Needed_Body (Comp_Unit, OK);
14091 Opt.Style_Check := Saved_Style_Check;
14092 Restore_Warnings (Saved_Warnings);
14093 Expander_Mode_Restore;
14096 and then Unit_Requires_Body (Defining_Entity (Spec))
14097 and then not Body_Optional
14100 Bname : constant Unit_Name_Type :=
14101 Get_Body_Name (Get_Unit_Name (Unit (Comp_Unit)));
14104 -- In CodePeer mode, the missing body may make the analysis
14105 -- incomplete, but we do not treat it as fatal.
14107 if CodePeer_Mode then
14111 Error_Msg_Unit_1 := Bname;
14112 Error_Msg_N ("this instantiation requires$!", N);
14113 Error_Msg_File_1 :=
14114 Get_File_Name (Bname, Subunit => False);
14115 Error_Msg_N ("\but file{ was not found!", N);
14116 raise Unrecoverable_Error;
14123 -- If loading parent of the generic caused an instantiation circularity,
14124 -- we abandon compilation at this point, because otherwise in some cases
14125 -- we get into trouble with infinite recursions after this point.
14127 if Circularity_Detected then
14128 raise Unrecoverable_Error;
14130 end Load_Parent_Of_Generic;
14132 ---------------------------------
14133 -- Map_Formal_Package_Entities --
14134 ---------------------------------
14136 procedure Map_Formal_Package_Entities (Form : Entity_Id; Act : Entity_Id) is
14141 Set_Instance_Of (Form, Act);
14143 -- Traverse formal and actual package to map the corresponding entities.
14144 -- We skip over internal entities that may be generated during semantic
14145 -- analysis, and find the matching entities by name, given that they
14146 -- must appear in the same order.
14148 E1 := First_Entity (Form);
14149 E2 := First_Entity (Act);
14150 while Present (E1) and then E1 /= First_Private_Entity (Form) loop
14151 -- Could this test be a single condition??? Seems like it could, and
14152 -- isn't FPE (Form) a constant anyway???
14154 if not Is_Internal (E1)
14155 and then Present (Parent (E1))
14156 and then not Is_Class_Wide_Type (E1)
14157 and then not Is_Internal_Name (Chars (E1))
14159 while Present (E2) and then Chars (E2) /= Chars (E1) loop
14166 Set_Instance_Of (E1, E2);
14168 if Is_Type (E1) and then Is_Tagged_Type (E2) then
14169 Set_Instance_Of (Class_Wide_Type (E1), Class_Wide_Type (E2));
14172 if Is_Constrained (E1) then
14173 Set_Instance_Of (Base_Type (E1), Base_Type (E2));
14176 if Ekind (E1) = E_Package and then No (Renamed_Object (E1)) then
14177 Map_Formal_Package_Entities (E1, E2);
14184 end Map_Formal_Package_Entities;
14186 -----------------------
14187 -- Move_Freeze_Nodes --
14188 -----------------------
14190 procedure Move_Freeze_Nodes
14191 (Out_Of : Entity_Id;
14196 Next_Decl : Node_Id;
14197 Next_Node : Node_Id := After;
14200 function Is_Outer_Type (T : Entity_Id) return Boolean;
14201 -- Check whether entity is declared in a scope external to that of the
14204 -------------------
14205 -- Is_Outer_Type --
14206 -------------------
14208 function Is_Outer_Type (T : Entity_Id) return Boolean is
14209 Scop : Entity_Id := Scope (T);
14212 if Scope_Depth (Scop) < Scope_Depth (Out_Of) then
14216 while Scop /= Standard_Standard loop
14217 if Scop = Out_Of then
14220 Scop := Scope (Scop);
14228 -- Start of processing for Move_Freeze_Nodes
14235 -- First remove the freeze nodes that may appear before all other
14239 while Present (Decl)
14240 and then Nkind (Decl) = N_Freeze_Entity
14241 and then Is_Outer_Type (Entity (Decl))
14243 Decl := Remove_Head (L);
14244 Insert_After (Next_Node, Decl);
14245 Set_Analyzed (Decl, False);
14250 -- Next scan the list of declarations and remove each freeze node that
14251 -- appears ahead of the current node.
14253 while Present (Decl) loop
14254 while Present (Next (Decl))
14255 and then Nkind (Next (Decl)) = N_Freeze_Entity
14256 and then Is_Outer_Type (Entity (Next (Decl)))
14258 Next_Decl := Remove_Next (Decl);
14259 Insert_After (Next_Node, Next_Decl);
14260 Set_Analyzed (Next_Decl, False);
14261 Next_Node := Next_Decl;
14264 -- If the declaration is a nested package or concurrent type, then
14265 -- recurse. Nested generic packages will have been processed from the
14268 case Nkind (Decl) is
14269 when N_Package_Declaration =>
14270 Spec := Specification (Decl);
14272 when N_Task_Type_Declaration =>
14273 Spec := Task_Definition (Decl);
14275 when N_Protected_Type_Declaration =>
14276 Spec := Protected_Definition (Decl);
14282 if Present (Spec) then
14283 Move_Freeze_Nodes (Out_Of, Next_Node, Visible_Declarations (Spec));
14284 Move_Freeze_Nodes (Out_Of, Next_Node, Private_Declarations (Spec));
14289 end Move_Freeze_Nodes;
14295 function Next_Assoc (E : Assoc_Ptr) return Assoc_Ptr is
14297 return Generic_Renamings.Table (E).Next_In_HTable;
14300 ------------------------
14301 -- Preanalyze_Actuals --
14302 ------------------------
14304 procedure Preanalyze_Actuals (N : Node_Id; Inst : Entity_Id := Empty) is
14305 procedure Perform_Appropriate_Analysis (N : Node_Id);
14306 -- Determine if the actuals we are analyzing come from a generic
14307 -- instantiation that is a library unit and dispatch accordingly.
14309 ----------------------------------
14310 -- Perform_Appropriate_Analysis --
14311 ----------------------------------
14313 procedure Perform_Appropriate_Analysis (N : Node_Id) is
14315 -- When we have a library instantiation we cannot allow any expansion
14316 -- to occur, since there may be no place to put it. Instead, in that
14317 -- case we perform a preanalysis of the actual.
14319 if Present (Inst) and then Is_Compilation_Unit (Inst) then
14324 end Perform_Appropriate_Analysis;
14328 Errs : constant Nat := Serious_Errors_Detected;
14333 Cur : Entity_Id := Empty;
14334 -- Current homograph of the instance name
14336 Vis : Boolean := False;
14337 -- Saved visibility status of the current homograph
14339 -- Start of processing for Preanalyze_Actuals
14342 Assoc := First (Generic_Associations (N));
14344 -- If the instance is a child unit, its name may hide an outer homonym,
14345 -- so make it invisible to perform name resolution on the actuals.
14347 if Nkind (Defining_Unit_Name (N)) = N_Defining_Program_Unit_Name
14349 (Current_Entity (Defining_Identifier (Defining_Unit_Name (N))))
14351 Cur := Current_Entity (Defining_Identifier (Defining_Unit_Name (N)));
14353 if Is_Compilation_Unit (Cur) then
14354 Vis := Is_Immediately_Visible (Cur);
14355 Set_Is_Immediately_Visible (Cur, False);
14361 while Present (Assoc) loop
14362 if Nkind (Assoc) /= N_Others_Choice then
14363 Act := Explicit_Generic_Actual_Parameter (Assoc);
14365 -- Within a nested instantiation, a defaulted actual is an empty
14366 -- association, so nothing to analyze. If the subprogram actual
14367 -- is an attribute, analyze prefix only, because actual is not a
14368 -- complete attribute reference.
14370 -- If actual is an allocator, analyze expression only. The full
14371 -- analysis can generate code, and if instance is a compilation
14372 -- unit we have to wait until the package instance is installed
14373 -- to have a proper place to insert this code.
14375 -- String literals may be operators, but at this point we do not
14376 -- know whether the actual is a formal subprogram or a string.
14381 elsif Nkind (Act) = N_Attribute_Reference then
14382 Perform_Appropriate_Analysis (Prefix (Act));
14384 elsif Nkind (Act) = N_Explicit_Dereference then
14385 Perform_Appropriate_Analysis (Prefix (Act));
14387 elsif Nkind (Act) = N_Allocator then
14389 Expr : constant Node_Id := Expression (Act);
14392 if Nkind (Expr) = N_Subtype_Indication then
14393 Perform_Appropriate_Analysis (Subtype_Mark (Expr));
14395 -- Analyze separately each discriminant constraint, when
14396 -- given with a named association.
14402 Constr := First (Constraints (Constraint (Expr)));
14403 while Present (Constr) loop
14404 if Nkind (Constr) = N_Discriminant_Association then
14405 Perform_Appropriate_Analysis
14406 (Expression (Constr));
14408 Perform_Appropriate_Analysis (Constr);
14416 Perform_Appropriate_Analysis (Expr);
14420 elsif Nkind (Act) /= N_Operator_Symbol then
14421 Perform_Appropriate_Analysis (Act);
14423 -- Within a package instance, mark actuals that are limited
14424 -- views, so their use can be moved to the body of the
14427 if Is_Entity_Name (Act)
14428 and then Is_Type (Entity (Act))
14429 and then From_Limited_With (Entity (Act))
14430 and then Present (Inst)
14432 Append_Elmt (Entity (Act), Incomplete_Actuals (Inst));
14436 if Errs /= Serious_Errors_Detected then
14438 -- Do a minimal analysis of the generic, to prevent spurious
14439 -- warnings complaining about the generic being unreferenced,
14440 -- before abandoning the instantiation.
14442 Perform_Appropriate_Analysis (Name (N));
14444 if Is_Entity_Name (Name (N))
14445 and then Etype (Name (N)) /= Any_Type
14447 Generate_Reference (Entity (Name (N)), Name (N));
14448 Set_Is_Instantiated (Entity (Name (N)));
14451 if Present (Cur) then
14453 -- For the case of a child instance hiding an outer homonym,
14454 -- provide additional warning which might explain the error.
14456 Set_Is_Immediately_Visible (Cur, Vis);
14458 ("& hides outer unit with the same name??",
14459 N, Defining_Unit_Name (N));
14462 Abandon_Instantiation (Act);
14469 if Present (Cur) then
14470 Set_Is_Immediately_Visible (Cur, Vis);
14472 end Preanalyze_Actuals;
14474 -------------------------------
14475 -- Provide_Completing_Bodies --
14476 -------------------------------
14478 procedure Provide_Completing_Bodies (N : Node_Id) is
14479 procedure Build_Completing_Body (Subp_Decl : Node_Id);
14480 -- Generate the completing body for subprogram declaration Subp_Decl
14482 procedure Provide_Completing_Bodies_In (Decls : List_Id);
14483 -- Generating completing bodies for all subprograms found in declarative
14486 ---------------------------
14487 -- Build_Completing_Body --
14488 ---------------------------
14490 procedure Build_Completing_Body (Subp_Decl : Node_Id) is
14491 Loc : constant Source_Ptr := Sloc (Subp_Decl);
14492 Subp_Id : constant Entity_Id := Defining_Entity (Subp_Decl);
14496 -- Nothing to do if the subprogram already has a completing body
14498 if Present (Corresponding_Body (Subp_Decl)) then
14501 -- Mark the function as having a valid return statement even though
14502 -- the body contains a single raise statement.
14504 elsif Ekind (Subp_Id) = E_Function then
14505 Set_Return_Present (Subp_Id);
14508 -- Clone the specification to obtain new entities and reset the only
14511 Spec := Copy_Subprogram_Spec (Specification (Subp_Decl));
14512 Set_Generic_Parent (Spec, Empty);
14515 -- function Func ... return ... is
14517 -- procedure Proc ... is
14519 -- raise Program_Error with "access before elaboration";
14522 Insert_After_And_Analyze (Subp_Decl,
14523 Make_Subprogram_Body (Loc,
14524 Specification => Spec,
14525 Declarations => New_List,
14526 Handled_Statement_Sequence =>
14527 Make_Handled_Sequence_Of_Statements (Loc,
14528 Statements => New_List (
14529 Make_Raise_Program_Error (Loc,
14530 Reason => PE_Access_Before_Elaboration)))));
14531 end Build_Completing_Body;
14533 ----------------------------------
14534 -- Provide_Completing_Bodies_In --
14535 ----------------------------------
14537 procedure Provide_Completing_Bodies_In (Decls : List_Id) is
14541 if Present (Decls) then
14542 Decl := First (Decls);
14543 while Present (Decl) loop
14544 Provide_Completing_Bodies (Decl);
14548 end Provide_Completing_Bodies_In;
14554 -- Start of processing for Provide_Completing_Bodies
14557 if Nkind (N) = N_Package_Declaration then
14558 Spec := Specification (N);
14560 Push_Scope (Defining_Entity (N));
14561 Provide_Completing_Bodies_In (Visible_Declarations (Spec));
14562 Provide_Completing_Bodies_In (Private_Declarations (Spec));
14565 elsif Nkind (N) = N_Subprogram_Declaration then
14566 Build_Completing_Body (N);
14568 end Provide_Completing_Bodies;
14570 -------------------
14571 -- Remove_Parent --
14572 -------------------
14574 procedure Remove_Parent (In_Body : Boolean := False) is
14575 S : Entity_Id := Current_Scope;
14576 -- S is the scope containing the instantiation just completed. The scope
14577 -- stack contains the parent instances of the instantiation, followed by
14586 -- After child instantiation is complete, remove from scope stack the
14587 -- extra copy of the current scope, and then remove parent instances.
14589 if not In_Body then
14592 while Current_Scope /= S loop
14593 P := Current_Scope;
14594 End_Package_Scope (Current_Scope);
14596 if In_Open_Scopes (P) then
14597 E := First_Entity (P);
14598 while Present (E) loop
14599 Set_Is_Immediately_Visible (E, True);
14603 -- If instantiation is declared in a block, it is the enclosing
14604 -- scope that might be a parent instance. Note that only one
14605 -- block can be involved, because the parent instances have
14606 -- been installed within it.
14608 if Ekind (P) = E_Block then
14609 Cur_P := Scope (P);
14614 if Is_Generic_Instance (Cur_P) and then P /= Current_Scope then
14615 -- We are within an instance of some sibling. Retain
14616 -- visibility of parent, for proper subsequent cleanup, and
14617 -- reinstall private declarations as well.
14619 Set_In_Private_Part (P);
14620 Install_Private_Declarations (P);
14623 -- If the ultimate parent is a top-level unit recorded in
14624 -- Instance_Parent_Unit, then reset its visibility to what it was
14625 -- before instantiation. (It's not clear what the purpose is of
14626 -- testing whether Scope (P) is In_Open_Scopes, but that test was
14627 -- present before the ultimate parent test was added.???)
14629 elsif not In_Open_Scopes (Scope (P))
14630 or else (P = Instance_Parent_Unit
14631 and then not Parent_Unit_Visible)
14633 Set_Is_Immediately_Visible (P, False);
14635 -- If the current scope is itself an instantiation of a generic
14636 -- nested within P, and we are in the private part of body of this
14637 -- instantiation, restore the full views of P, that were removed
14638 -- in End_Package_Scope above. This obscure case can occur when a
14639 -- subunit of a generic contains an instance of a child unit of
14640 -- its generic parent unit.
14642 elsif S = Current_Scope and then Is_Generic_Instance (S) then
14644 Par : constant Entity_Id :=
14645 Generic_Parent (Package_Specification (S));
14648 and then P = Scope (Par)
14649 and then (In_Package_Body (S) or else In_Private_Part (S))
14651 Set_In_Private_Part (P);
14652 Install_Private_Declarations (P);
14658 -- Reset visibility of entities in the enclosing scope
14660 Set_Is_Hidden_Open_Scope (Current_Scope, False);
14662 Hidden := First_Elmt (Hidden_Entities);
14663 while Present (Hidden) loop
14664 Set_Is_Immediately_Visible (Node (Hidden), True);
14665 Next_Elmt (Hidden);
14669 -- Each body is analyzed separately, and there is no context that
14670 -- needs preserving from one body instance to the next, so remove all
14671 -- parent scopes that have been installed.
14673 while Present (S) loop
14674 End_Package_Scope (S);
14675 Set_Is_Immediately_Visible (S, False);
14676 S := Current_Scope;
14677 exit when S = Standard_Standard;
14686 procedure Restore_Env is
14687 Saved : Instance_Env renames Instance_Envs.Table (Instance_Envs.Last);
14690 if No (Current_Instantiated_Parent.Act_Id) then
14691 -- Restore environment after subprogram inlining
14693 Restore_Private_Views (Empty);
14696 Current_Instantiated_Parent := Saved.Instantiated_Parent;
14697 Exchanged_Views := Saved.Exchanged_Views;
14698 Hidden_Entities := Saved.Hidden_Entities;
14699 Current_Sem_Unit := Saved.Current_Sem_Unit;
14700 Parent_Unit_Visible := Saved.Parent_Unit_Visible;
14701 Instance_Parent_Unit := Saved.Instance_Parent_Unit;
14703 Restore_Config_Switches (Saved.Switches);
14705 Instance_Envs.Decrement_Last;
14708 ---------------------------
14709 -- Restore_Private_Views --
14710 ---------------------------
14712 procedure Restore_Private_Views
14713 (Pack_Id : Entity_Id;
14714 Is_Package : Boolean := True)
14719 Dep_Elmt : Elmt_Id;
14722 procedure Restore_Nested_Formal (Formal : Entity_Id);
14723 -- Hide the generic formals of formal packages declared with box which
14724 -- were reachable in the current instantiation.
14726 ---------------------------
14727 -- Restore_Nested_Formal --
14728 ---------------------------
14730 procedure Restore_Nested_Formal (Formal : Entity_Id) is
14734 if Present (Renamed_Object (Formal))
14735 and then Denotes_Formal_Package (Renamed_Object (Formal), True)
14739 elsif Present (Associated_Formal_Package (Formal)) then
14740 Ent := First_Entity (Formal);
14741 while Present (Ent) loop
14742 exit when Ekind (Ent) = E_Package
14743 and then Renamed_Entity (Ent) = Renamed_Entity (Formal);
14745 Set_Is_Hidden (Ent);
14746 Set_Is_Potentially_Use_Visible (Ent, False);
14748 -- If package, then recurse
14750 if Ekind (Ent) = E_Package then
14751 Restore_Nested_Formal (Ent);
14757 end Restore_Nested_Formal;
14759 -- Start of processing for Restore_Private_Views
14762 M := First_Elmt (Exchanged_Views);
14763 while Present (M) loop
14766 -- Subtypes of types whose views have been exchanged, and that are
14767 -- defined within the instance, were not on the Private_Dependents
14768 -- list on entry to the instance, so they have to be exchanged
14769 -- explicitly now, in order to remain consistent with the view of the
14772 if Ekind_In (Typ, E_Private_Type,
14773 E_Limited_Private_Type,
14774 E_Record_Type_With_Private)
14776 Dep_Elmt := First_Elmt (Private_Dependents (Typ));
14777 while Present (Dep_Elmt) loop
14778 Dep_Typ := Node (Dep_Elmt);
14780 if Scope (Dep_Typ) = Pack_Id
14781 and then Present (Full_View (Dep_Typ))
14783 Replace_Elmt (Dep_Elmt, Full_View (Dep_Typ));
14784 Exchange_Declarations (Dep_Typ);
14787 Next_Elmt (Dep_Elmt);
14791 Exchange_Declarations (Node (M));
14795 if No (Pack_Id) then
14799 -- Make the generic formal parameters private, and make the formal types
14800 -- into subtypes of the actuals again.
14802 E := First_Entity (Pack_Id);
14803 while Present (E) loop
14804 Set_Is_Hidden (E, True);
14807 and then Nkind (Parent (E)) = N_Subtype_Declaration
14809 -- If the actual for E is itself a generic actual type from
14810 -- an enclosing instance, E is still a generic actual type
14811 -- outside of the current instance. This matter when resolving
14812 -- an overloaded call that may be ambiguous in the enclosing
14813 -- instance, when two of its actuals coincide.
14815 if Is_Entity_Name (Subtype_Indication (Parent (E)))
14816 and then Is_Generic_Actual_Type
14817 (Entity (Subtype_Indication (Parent (E))))
14821 Set_Is_Generic_Actual_Type (E, False);
14823 -- It might seem reasonable to clear the Is_Generic_Actual_Type
14824 -- flag also on the Full_View if the type is private, since it
14825 -- was set also on this Full_View. However, this flag is relied
14826 -- upon by Covers to spot "types exported from instantiations"
14827 -- which are implicit Full_Views built for instantiations made
14828 -- on private types and we get type mismatches if we do it when
14829 -- the block exchanging the declarations below triggers ???
14831 -- if Is_Private_Type (E) and then Present (Full_View (E)) then
14832 -- Set_Is_Generic_Actual_Type (Full_View (E), False);
14836 -- An unusual case of aliasing: the actual may also be directly
14837 -- visible in the generic, and be private there, while it is fully
14838 -- visible in the context of the instance. The internal subtype
14839 -- is private in the instance but has full visibility like its
14840 -- parent in the enclosing scope. This enforces the invariant that
14841 -- the privacy status of all private dependents of a type coincide
14842 -- with that of the parent type. This can only happen when a
14843 -- generic child unit is instantiated within a sibling.
14845 if Is_Private_Type (E)
14846 and then not Is_Private_Type (Etype (E))
14848 Exchange_Declarations (E);
14851 elsif Ekind (E) = E_Package then
14853 -- The end of the renaming list is the renaming of the generic
14854 -- package itself. If the instance is a subprogram, all entities
14855 -- in the corresponding package are renamings. If this entity is
14856 -- a formal package, make its own formals private as well. The
14857 -- actual in this case is itself the renaming of an instantiation.
14858 -- If the entity is not a package renaming, it is the entity
14859 -- created to validate formal package actuals: ignore it.
14861 -- If the actual is itself a formal package for the enclosing
14862 -- generic, or the actual for such a formal package, it remains
14863 -- visible on exit from the instance, and therefore nothing needs
14864 -- to be done either, except to keep it accessible.
14866 if Is_Package and then Renamed_Object (E) = Pack_Id then
14869 elsif Nkind (Parent (E)) /= N_Package_Renaming_Declaration then
14873 Denotes_Formal_Package (Renamed_Object (E), True, Pack_Id)
14875 Set_Is_Hidden (E, False);
14879 Act_P : constant Entity_Id := Renamed_Object (E);
14883 Id := First_Entity (Act_P);
14885 and then Id /= First_Private_Entity (Act_P)
14887 exit when Ekind (Id) = E_Package
14888 and then Renamed_Object (Id) = Act_P;
14890 Set_Is_Hidden (Id, True);
14891 Set_Is_Potentially_Use_Visible (Id, In_Use (Act_P));
14893 if Ekind (Id) = E_Package then
14894 Restore_Nested_Formal (Id);
14905 end Restore_Private_Views;
14912 (Gen_Unit : Entity_Id;
14913 Act_Unit : Entity_Id)
14917 Set_Instance_Env (Gen_Unit, Act_Unit);
14920 ----------------------------
14921 -- Save_Global_References --
14922 ----------------------------
14924 procedure Save_Global_References (Templ : Node_Id) is
14926 -- ??? it is horrible to use global variables in highly recursive code
14929 -- The entity of the current associated node
14931 Gen_Scope : Entity_Id;
14932 -- The scope of the generic for which references are being saved
14935 -- The current associated node
14937 function Is_Global (E : Entity_Id) return Boolean;
14938 -- Check whether entity is defined outside of generic unit. Examine the
14939 -- scope of an entity, and the scope of the scope, etc, until we find
14940 -- either Standard, in which case the entity is global, or the generic
14941 -- unit itself, which indicates that the entity is local. If the entity
14942 -- is the generic unit itself, as in the case of a recursive call, or
14943 -- the enclosing generic unit, if different from the current scope, then
14944 -- it is local as well, because it will be replaced at the point of
14945 -- instantiation. On the other hand, if it is a reference to a child
14946 -- unit of a common ancestor, which appears in an instantiation, it is
14947 -- global because it is used to denote a specific compilation unit at
14948 -- the time the instantiations will be analyzed.
14950 procedure Qualify_Universal_Operands
14952 Func_Call : Node_Id);
14953 -- Op denotes a binary or unary operator in generic template Templ. Node
14954 -- Func_Call is the function call alternative of the operator within the
14955 -- the analyzed copy of the template. Change each operand which yields a
14956 -- universal type by wrapping it into a qualified expression
14958 -- Actual_Typ'(Operand)
14960 -- where Actual_Typ is the type of corresponding actual parameter of
14961 -- Operand in Func_Call.
14963 procedure Reset_Entity (N : Node_Id);
14964 -- Save semantic information on global entity so that it is not resolved
14965 -- again at instantiation time.
14967 procedure Save_Entity_Descendants (N : Node_Id);
14968 -- Apply Save_Global_References to the two syntactic descendants of
14969 -- non-terminal nodes that carry an Associated_Node and are processed
14970 -- through Reset_Entity. Once the global entity (if any) has been
14971 -- captured together with its type, only two syntactic descendants need
14972 -- to be traversed to complete the processing of the tree rooted at N.
14973 -- This applies to Selected_Components, Expanded_Names, and to Operator
14974 -- nodes. N can also be a character literal, identifier, or operator
14975 -- symbol node, but the call has no effect in these cases.
14977 procedure Save_Global_Defaults (N1 : Node_Id; N2 : Node_Id);
14978 -- Default actuals in nested instances must be handled specially
14979 -- because there is no link to them from the original tree. When an
14980 -- actual subprogram is given by a default, we add an explicit generic
14981 -- association for it in the instantiation node. When we save the
14982 -- global references on the name of the instance, we recover the list
14983 -- of generic associations, and add an explicit one to the original
14984 -- generic tree, through which a global actual can be preserved.
14985 -- Similarly, if a child unit is instantiated within a sibling, in the
14986 -- context of the parent, we must preserve the identifier of the parent
14987 -- so that it can be properly resolved in a subsequent instantiation.
14989 procedure Save_Global_Descendant (D : Union_Id);
14990 -- Apply Save_References recursively to the descendants of node D
14992 procedure Save_References (N : Node_Id);
14993 -- This is the recursive procedure that does the work, once the
14994 -- enclosing generic scope has been established.
15000 function Is_Global (E : Entity_Id) return Boolean is
15003 function Is_Instance_Node (Decl : Node_Id) return Boolean;
15004 -- Determine whether the parent node of a reference to a child unit
15005 -- denotes an instantiation or a formal package, in which case the
15006 -- reference to the child unit is global, even if it appears within
15007 -- the current scope (e.g. when the instance appears within the body
15008 -- of an ancestor).
15010 ----------------------
15011 -- Is_Instance_Node --
15012 ----------------------
15014 function Is_Instance_Node (Decl : Node_Id) return Boolean is
15016 return Nkind (Decl) in N_Generic_Instantiation
15018 Nkind (Original_Node (Decl)) = N_Formal_Package_Declaration;
15019 end Is_Instance_Node;
15021 -- Start of processing for Is_Global
15024 if E = Gen_Scope then
15027 elsif E = Standard_Standard then
15030 elsif Is_Child_Unit (E)
15031 and then (Is_Instance_Node (Parent (N2))
15032 or else (Nkind (Parent (N2)) = N_Expanded_Name
15033 and then N2 = Selector_Name (Parent (N2))
15035 Is_Instance_Node (Parent (Parent (N2)))))
15041 while Se /= Gen_Scope loop
15042 if Se = Standard_Standard then
15053 --------------------------------
15054 -- Qualify_Universal_Operands --
15055 --------------------------------
15057 procedure Qualify_Universal_Operands
15059 Func_Call : Node_Id)
15061 procedure Qualify_Operand (Opnd : Node_Id; Actual : Node_Id);
15062 -- Rewrite operand Opnd as a qualified expression of the form
15064 -- Actual_Typ'(Opnd)
15066 -- where Actual is the corresponding actual parameter of Opnd in
15067 -- function call Func_Call.
15069 function Qualify_Type
15071 Typ : Entity_Id) return Node_Id;
15072 -- Qualify type Typ by creating a selected component of the form
15074 -- Scope_Of_Typ.Typ
15076 ---------------------
15077 -- Qualify_Operand --
15078 ---------------------
15080 procedure Qualify_Operand (Opnd : Node_Id; Actual : Node_Id) is
15081 Loc : constant Source_Ptr := Sloc (Opnd);
15082 Typ : constant Entity_Id := Etype (Actual);
15087 -- Qualify the operand when it is of a universal type. Note that
15088 -- the template is unanalyzed and it is not possible to directly
15089 -- query the type. This transformation is not done when the type
15090 -- of the actual is internally generated because the type will be
15091 -- regenerated in the instance.
15093 if Yields_Universal_Type (Opnd)
15094 and then Comes_From_Source (Typ)
15095 and then not Is_Hidden (Typ)
15097 -- The type of the actual may be a global reference. Save this
15098 -- information by creating a reference to it.
15100 if Is_Global (Typ) then
15101 Mark := New_Occurrence_Of (Typ, Loc);
15103 -- Otherwise rely on resolution to find the proper type within
15107 Mark := Qualify_Type (Loc, Typ);
15111 Make_Qualified_Expression (Loc,
15112 Subtype_Mark => Mark,
15113 Expression => Relocate_Node (Opnd));
15115 -- Mark the qualification to distinguish it from other source
15116 -- constructs and signal the instantiation mechanism that this
15117 -- node requires special processing. See Copy_Generic_Node for
15120 Set_Is_Qualified_Universal_Literal (Qual);
15122 Rewrite (Opnd, Qual);
15124 end Qualify_Operand;
15130 function Qualify_Type
15132 Typ : Entity_Id) return Node_Id
15134 Scop : constant Entity_Id := Scope (Typ);
15138 Result := Make_Identifier (Loc, Chars (Typ));
15140 if Present (Scop) and then not Is_Generic_Unit (Scop) then
15142 Make_Selected_Component (Loc,
15143 Prefix => Make_Identifier (Loc, Chars (Scop)),
15144 Selector_Name => Result);
15152 Actuals : constant List_Id := Parameter_Associations (Func_Call);
15154 -- Start of processing for Qualify_Universal_Operands
15157 if Nkind (Op) in N_Binary_Op then
15158 Qualify_Operand (Left_Opnd (Op), First (Actuals));
15159 Qualify_Operand (Right_Opnd (Op), Next (First (Actuals)));
15161 elsif Nkind (Op) in N_Unary_Op then
15162 Qualify_Operand (Right_Opnd (Op), First (Actuals));
15164 end Qualify_Universal_Operands;
15170 procedure Reset_Entity (N : Node_Id) is
15171 procedure Set_Global_Type (N : Node_Id; N2 : Node_Id);
15172 -- If the type of N2 is global to the generic unit, save the type in
15173 -- the generic node. Just as we perform name capture for explicit
15174 -- references within the generic, we must capture the global types
15175 -- of local entities because they may participate in resolution in
15178 function Top_Ancestor (E : Entity_Id) return Entity_Id;
15179 -- Find the ultimate ancestor of the current unit. If it is not a
15180 -- generic unit, then the name of the current unit in the prefix of
15181 -- an expanded name must be replaced with its generic homonym to
15182 -- ensure that it will be properly resolved in an instance.
15184 ---------------------
15185 -- Set_Global_Type --
15186 ---------------------
15188 procedure Set_Global_Type (N : Node_Id; N2 : Node_Id) is
15189 Typ : constant Entity_Id := Etype (N2);
15192 Set_Etype (N, Typ);
15194 -- If the entity of N is not the associated node, this is a
15195 -- nested generic and it has an associated node as well, whose
15196 -- type is already the full view (see below). Indicate that the
15197 -- original node has a private view.
15199 if Entity (N) /= N2 and then Has_Private_View (Entity (N)) then
15200 Set_Has_Private_View (N);
15203 -- If not a private type, nothing else to do
15205 if not Is_Private_Type (Typ) then
15208 -- If it is a derivation of a private type in a context where no
15209 -- full view is needed, nothing to do either.
15211 elsif No (Full_View (Typ)) and then Typ /= Etype (Typ) then
15214 -- Otherwise mark the type for flipping and use the full view when
15218 Set_Has_Private_View (N);
15220 if Present (Full_View (Typ)) then
15221 Set_Etype (N2, Full_View (Typ));
15225 if Is_Floating_Point_Type (Typ)
15226 and then Has_Dimension_System (Typ)
15228 Copy_Dimensions (N2, N);
15230 end Set_Global_Type;
15236 function Top_Ancestor (E : Entity_Id) return Entity_Id is
15241 while Is_Child_Unit (Par) loop
15242 Par := Scope (Par);
15248 -- Start of processing for Reset_Entity
15251 N2 := Get_Associated_Node (N);
15254 if Present (E) then
15256 -- If the node is an entry call to an entry in an enclosing task,
15257 -- it is rewritten as a selected component. No global entity to
15258 -- preserve in this case, since the expansion will be redone in
15261 if not Nkind_In (E, N_Defining_Character_Literal,
15262 N_Defining_Identifier,
15263 N_Defining_Operator_Symbol)
15265 Set_Associated_Node (N, Empty);
15266 Set_Etype (N, Empty);
15270 -- If the entity is an itype created as a subtype of an access
15271 -- type with a null exclusion restore source entity for proper
15272 -- visibility. The itype will be created anew in the instance.
15275 and then Ekind (E) = E_Access_Subtype
15276 and then Is_Entity_Name (N)
15277 and then Chars (Etype (E)) = Chars (N)
15280 Set_Entity (N2, E);
15284 if Is_Global (E) then
15285 Set_Global_Type (N, N2);
15287 elsif Nkind (N) = N_Op_Concat
15288 and then Is_Generic_Type (Etype (N2))
15289 and then (Base_Type (Etype (Right_Opnd (N2))) = Etype (N2)
15291 Base_Type (Etype (Left_Opnd (N2))) = Etype (N2))
15292 and then Is_Intrinsic_Subprogram (E)
15296 -- Entity is local. Mark generic node as unresolved. Note that now
15297 -- it does not have an entity.
15300 Set_Associated_Node (N, Empty);
15301 Set_Etype (N, Empty);
15304 if Nkind (Parent (N)) in N_Generic_Instantiation
15305 and then N = Name (Parent (N))
15307 Save_Global_Defaults (Parent (N), Parent (N2));
15310 elsif Nkind (Parent (N)) = N_Selected_Component
15311 and then Nkind (Parent (N2)) = N_Expanded_Name
15313 if Is_Global (Entity (Parent (N2))) then
15314 Change_Selected_Component_To_Expanded_Name (Parent (N));
15315 Set_Associated_Node (Parent (N), Parent (N2));
15316 Set_Global_Type (Parent (N), Parent (N2));
15317 Save_Entity_Descendants (N);
15319 -- If this is a reference to the current generic entity, replace
15320 -- by the name of the generic homonym of the current package. This
15321 -- is because in an instantiation Par.P.Q will not resolve to the
15322 -- name of the instance, whose enclosing scope is not necessarily
15323 -- Par. We use the generic homonym rather that the name of the
15324 -- generic itself because it may be hidden by a local declaration.
15326 elsif In_Open_Scopes (Entity (Parent (N2)))
15328 Is_Generic_Unit (Top_Ancestor (Entity (Prefix (Parent (N2)))))
15330 if Ekind (Entity (Parent (N2))) = E_Generic_Package then
15331 Rewrite (Parent (N),
15332 Make_Identifier (Sloc (N),
15334 Chars (Generic_Homonym (Entity (Parent (N2))))));
15336 Rewrite (Parent (N),
15337 Make_Identifier (Sloc (N),
15338 Chars => Chars (Selector_Name (Parent (N2)))));
15342 if Nkind (Parent (Parent (N))) in N_Generic_Instantiation
15343 and then Parent (N) = Name (Parent (Parent (N)))
15345 Save_Global_Defaults
15346 (Parent (Parent (N)), Parent (Parent (N2)));
15349 -- A selected component may denote a static constant that has been
15350 -- folded. If the static constant is global to the generic, capture
15351 -- its value. Otherwise the folding will happen in any instantiation.
15353 elsif Nkind (Parent (N)) = N_Selected_Component
15354 and then Nkind_In (Parent (N2), N_Integer_Literal, N_Real_Literal)
15356 if Present (Entity (Original_Node (Parent (N2))))
15357 and then Is_Global (Entity (Original_Node (Parent (N2))))
15359 Rewrite (Parent (N), New_Copy (Parent (N2)));
15360 Set_Analyzed (Parent (N), False);
15363 -- A selected component may be transformed into a parameterless
15364 -- function call. If the called entity is global, rewrite the node
15365 -- appropriately, i.e. as an extended name for the global entity.
15367 elsif Nkind (Parent (N)) = N_Selected_Component
15368 and then Nkind (Parent (N2)) = N_Function_Call
15369 and then N = Selector_Name (Parent (N))
15371 if No (Parameter_Associations (Parent (N2))) then
15372 if Is_Global (Entity (Name (Parent (N2)))) then
15373 Change_Selected_Component_To_Expanded_Name (Parent (N));
15374 Set_Associated_Node (Parent (N), Name (Parent (N2)));
15375 Set_Global_Type (Parent (N), Name (Parent (N2)));
15376 Save_Entity_Descendants (N);
15379 Set_Is_Prefixed_Call (Parent (N));
15380 Set_Associated_Node (N, Empty);
15381 Set_Etype (N, Empty);
15384 -- In Ada 2005, X.F may be a call to a primitive operation,
15385 -- rewritten as F (X). This rewriting will be done again in an
15386 -- instance, so keep the original node. Global entities will be
15387 -- captured as for other constructs. Indicate that this must
15388 -- resolve as a call, to prevent accidental overloading in the
15389 -- instance, if both a component and a primitive operation appear
15393 Set_Is_Prefixed_Call (Parent (N));
15396 -- Entity is local. Reset in generic unit, so that node is resolved
15397 -- anew at the point of instantiation.
15400 Set_Associated_Node (N, Empty);
15401 Set_Etype (N, Empty);
15405 -----------------------------
15406 -- Save_Entity_Descendants --
15407 -----------------------------
15409 procedure Save_Entity_Descendants (N : Node_Id) is
15412 when N_Binary_Op =>
15413 Save_Global_Descendant (Union_Id (Left_Opnd (N)));
15414 Save_Global_Descendant (Union_Id (Right_Opnd (N)));
15417 Save_Global_Descendant (Union_Id (Right_Opnd (N)));
15419 when N_Expanded_Name
15420 | N_Selected_Component
15422 Save_Global_Descendant (Union_Id (Prefix (N)));
15423 Save_Global_Descendant (Union_Id (Selector_Name (N)));
15425 when N_Character_Literal
15427 | N_Operator_Symbol
15432 raise Program_Error;
15434 end Save_Entity_Descendants;
15436 --------------------------
15437 -- Save_Global_Defaults --
15438 --------------------------
15440 procedure Save_Global_Defaults (N1 : Node_Id; N2 : Node_Id) is
15441 Loc : constant Source_Ptr := Sloc (N1);
15442 Assoc2 : constant List_Id := Generic_Associations (N2);
15443 Gen_Id : constant Entity_Id := Get_Generic_Entity (N2);
15450 Actual : Entity_Id;
15453 Assoc1 := Generic_Associations (N1);
15455 if Present (Assoc1) then
15456 Act1 := First (Assoc1);
15459 Set_Generic_Associations (N1, New_List);
15460 Assoc1 := Generic_Associations (N1);
15463 if Present (Assoc2) then
15464 Act2 := First (Assoc2);
15469 while Present (Act1) and then Present (Act2) loop
15474 -- Find the associations added for default subprograms
15476 if Present (Act2) then
15477 while Nkind (Act2) /= N_Generic_Association
15478 or else No (Entity (Selector_Name (Act2)))
15479 or else not Is_Overloadable (Entity (Selector_Name (Act2)))
15484 -- Add a similar association if the default is global. The
15485 -- renaming declaration for the actual has been analyzed, and
15486 -- its alias is the program it renames. Link the actual in the
15487 -- original generic tree with the node in the analyzed tree.
15489 while Present (Act2) loop
15490 Subp := Entity (Selector_Name (Act2));
15491 Def := Explicit_Generic_Actual_Parameter (Act2);
15493 -- Following test is defence against rubbish errors
15495 if No (Alias (Subp)) then
15499 -- Retrieve the resolved actual from the renaming declaration
15500 -- created for the instantiated formal.
15502 Actual := Entity (Name (Parent (Parent (Subp))));
15503 Set_Entity (Def, Actual);
15504 Set_Etype (Def, Etype (Actual));
15506 if Is_Global (Actual) then
15508 Make_Generic_Association (Loc,
15510 New_Occurrence_Of (Subp, Loc),
15511 Explicit_Generic_Actual_Parameter =>
15512 New_Occurrence_Of (Actual, Loc));
15514 Set_Associated_Node
15515 (Explicit_Generic_Actual_Parameter (Ndec), Def);
15517 Append (Ndec, Assoc1);
15519 -- If there are other defaults, add a dummy association in case
15520 -- there are other defaulted formals with the same name.
15522 elsif Present (Next (Act2)) then
15524 Make_Generic_Association (Loc,
15526 New_Occurrence_Of (Subp, Loc),
15527 Explicit_Generic_Actual_Parameter => Empty);
15529 Append (Ndec, Assoc1);
15536 if Nkind (Name (N1)) = N_Identifier
15537 and then Is_Child_Unit (Gen_Id)
15538 and then Is_Global (Gen_Id)
15539 and then Is_Generic_Unit (Scope (Gen_Id))
15540 and then In_Open_Scopes (Scope (Gen_Id))
15542 -- This is an instantiation of a child unit within a sibling, so
15543 -- that the generic parent is in scope. An eventual instance must
15544 -- occur within the scope of an instance of the parent. Make name
15545 -- in instance into an expanded name, to preserve the identifier
15546 -- of the parent, so it can be resolved subsequently.
15548 Rewrite (Name (N2),
15549 Make_Expanded_Name (Loc,
15550 Chars => Chars (Gen_Id),
15551 Prefix => New_Occurrence_Of (Scope (Gen_Id), Loc),
15552 Selector_Name => New_Occurrence_Of (Gen_Id, Loc)));
15553 Set_Entity (Name (N2), Gen_Id);
15555 Rewrite (Name (N1),
15556 Make_Expanded_Name (Loc,
15557 Chars => Chars (Gen_Id),
15558 Prefix => New_Occurrence_Of (Scope (Gen_Id), Loc),
15559 Selector_Name => New_Occurrence_Of (Gen_Id, Loc)));
15561 Set_Associated_Node (Name (N1), Name (N2));
15562 Set_Associated_Node (Prefix (Name (N1)), Empty);
15563 Set_Associated_Node
15564 (Selector_Name (Name (N1)), Selector_Name (Name (N2)));
15565 Set_Etype (Name (N1), Etype (Gen_Id));
15567 end Save_Global_Defaults;
15569 ----------------------------
15570 -- Save_Global_Descendant --
15571 ----------------------------
15573 procedure Save_Global_Descendant (D : Union_Id) is
15577 if D in Node_Range then
15578 if D = Union_Id (Empty) then
15581 elsif Nkind (Node_Id (D)) /= N_Compilation_Unit then
15582 Save_References (Node_Id (D));
15585 elsif D in List_Range then
15586 pragma Assert (D /= Union_Id (No_List));
15587 -- Because No_List = Empty, which is in Node_Range above
15589 if Is_Empty_List (List_Id (D)) then
15593 N1 := First (List_Id (D));
15594 while Present (N1) loop
15595 Save_References (N1);
15600 -- Element list or other non-node field, nothing to do
15605 end Save_Global_Descendant;
15607 ---------------------
15608 -- Save_References --
15609 ---------------------
15611 -- This is the recursive procedure that does the work once the enclosing
15612 -- generic scope has been established. We have to treat specially a
15613 -- number of node rewritings that are required by semantic processing
15614 -- and which change the kind of nodes in the generic copy: typically
15615 -- constant-folding, replacing an operator node by a string literal, or
15616 -- a selected component by an expanded name. In each of those cases, the
15617 -- transformation is propagated to the generic unit.
15619 procedure Save_References (N : Node_Id) is
15620 Loc : constant Source_Ptr := Sloc (N);
15622 function Requires_Delayed_Save (Nod : Node_Id) return Boolean;
15623 -- Determine whether arbitrary node Nod requires delayed capture of
15624 -- global references within its aspect specifications.
15626 procedure Save_References_In_Aggregate (N : Node_Id);
15627 -- Save all global references in [extension] aggregate node N
15629 procedure Save_References_In_Char_Lit_Or_Op_Symbol (N : Node_Id);
15630 -- Save all global references in a character literal or operator
15631 -- symbol denoted by N.
15633 procedure Save_References_In_Descendants (N : Node_Id);
15634 -- Save all global references in all descendants of node N
15636 procedure Save_References_In_Identifier (N : Node_Id);
15637 -- Save all global references in identifier node N
15639 procedure Save_References_In_Operator (N : Node_Id);
15640 -- Save all global references in operator node N
15642 procedure Save_References_In_Pragma (Prag : Node_Id);
15643 -- Save all global references found within the expression of pragma
15646 ---------------------------
15647 -- Requires_Delayed_Save --
15648 ---------------------------
15650 function Requires_Delayed_Save (Nod : Node_Id) return Boolean is
15652 -- Generic packages and subprograms require delayed capture of
15653 -- global references within their aspects due to the timing of
15654 -- annotation analysis.
15656 if Nkind_In (Nod, N_Generic_Package_Declaration,
15657 N_Generic_Subprogram_Declaration,
15659 N_Package_Body_Stub,
15661 N_Subprogram_Body_Stub)
15663 -- Since the capture of global references is done on the
15664 -- unanalyzed generic template, there is no information around
15665 -- to infer the context. Use the Associated_Entity linkages to
15666 -- peek into the analyzed generic copy and determine what the
15667 -- template corresponds to.
15669 if Nod = Templ then
15671 Is_Generic_Declaration_Or_Body
15672 (Unit_Declaration_Node
15673 (Associated_Entity (Defining_Entity (Nod))));
15675 -- Otherwise the generic unit being processed is not the top
15676 -- level template. It is safe to capture of global references
15677 -- within the generic unit because at this point the top level
15678 -- copy is fully analyzed.
15684 -- Otherwise capture the global references without interference
15689 end Requires_Delayed_Save;
15691 ----------------------------------
15692 -- Save_References_In_Aggregate --
15693 ----------------------------------
15695 procedure Save_References_In_Aggregate (N : Node_Id) is
15697 Qual : Node_Id := Empty;
15698 Typ : Entity_Id := Empty;
15700 use Atree.Unchecked_Access;
15701 -- This code section is part of implementing an untyped tree
15702 -- traversal, so it needs direct access to node fields.
15705 N2 := Get_Associated_Node (N);
15707 if Present (N2) then
15710 -- In an instance within a generic, use the name of the actual
15711 -- and not the original generic parameter. If the actual is
15712 -- global in the current generic it must be preserved for its
15715 if Nkind (Parent (Typ)) = N_Subtype_Declaration
15716 and then Present (Generic_Parent_Type (Parent (Typ)))
15718 Typ := Base_Type (Typ);
15719 Set_Etype (N2, Typ);
15723 if No (N2) or else No (Typ) or else not Is_Global (Typ) then
15724 Set_Associated_Node (N, Empty);
15726 -- If the aggregate is an actual in a call, it has been
15727 -- resolved in the current context, to some local type. The
15728 -- enclosing call may have been disambiguated by the aggregate,
15729 -- and this disambiguation might fail at instantiation time
15730 -- because the type to which the aggregate did resolve is not
15731 -- preserved. In order to preserve some of this information,
15732 -- wrap the aggregate in a qualified expression, using the id
15733 -- of its type. For further disambiguation we qualify the type
15734 -- name with its scope (if visible and not hidden by a local
15735 -- homograph) because both id's will have corresponding
15736 -- entities in an instance. This resolves most of the problems
15737 -- with missing type information on aggregates in instances.
15740 and then Nkind (N2) = Nkind (N)
15741 and then Nkind (Parent (N2)) in N_Subprogram_Call
15742 and then Present (Typ)
15743 and then Comes_From_Source (Typ)
15745 Nam := Make_Identifier (Loc, Chars (Typ));
15747 if Is_Immediately_Visible (Scope (Typ))
15749 (not In_Open_Scopes (Scope (Typ))
15750 or else Current_Entity (Scope (Typ)) = Scope (Typ))
15753 Make_Selected_Component (Loc,
15755 Make_Identifier (Loc, Chars (Scope (Typ))),
15756 Selector_Name => Nam);
15760 Make_Qualified_Expression (Loc,
15761 Subtype_Mark => Nam,
15762 Expression => Relocate_Node (N));
15766 Save_Global_Descendant (Field1 (N));
15767 Save_Global_Descendant (Field2 (N));
15768 Save_Global_Descendant (Field3 (N));
15769 Save_Global_Descendant (Field5 (N));
15771 if Present (Qual) then
15774 end Save_References_In_Aggregate;
15776 ----------------------------------------------
15777 -- Save_References_In_Char_Lit_Or_Op_Symbol --
15778 ----------------------------------------------
15780 procedure Save_References_In_Char_Lit_Or_Op_Symbol (N : Node_Id) is
15782 if Nkind (N) = Nkind (Get_Associated_Node (N)) then
15785 elsif Nkind (N) = N_Operator_Symbol
15786 and then Nkind (Get_Associated_Node (N)) = N_String_Literal
15788 Change_Operator_Symbol_To_String_Literal (N);
15790 end Save_References_In_Char_Lit_Or_Op_Symbol;
15792 ------------------------------------
15793 -- Save_References_In_Descendants --
15794 ------------------------------------
15796 procedure Save_References_In_Descendants (N : Node_Id) is
15797 use Atree.Unchecked_Access;
15798 -- This code section is part of implementing an untyped tree
15799 -- traversal, so it needs direct access to node fields.
15802 Save_Global_Descendant (Field1 (N));
15803 Save_Global_Descendant (Field2 (N));
15804 Save_Global_Descendant (Field3 (N));
15805 Save_Global_Descendant (Field4 (N));
15806 Save_Global_Descendant (Field5 (N));
15807 end Save_References_In_Descendants;
15809 -----------------------------------
15810 -- Save_References_In_Identifier --
15811 -----------------------------------
15813 procedure Save_References_In_Identifier (N : Node_Id) is
15815 -- The node did not undergo a transformation
15817 if Nkind (N) = Nkind (Get_Associated_Node (N)) then
15818 -- If this is a discriminant reference, always save it.
15819 -- It is used in the instance to find the corresponding
15820 -- discriminant positionally rather than by name.
15822 Set_Original_Discriminant
15823 (N, Original_Discriminant (Get_Associated_Node (N)));
15827 -- The analysis of the generic copy transformed the identifier
15828 -- into another construct. Propagate the changes to the template.
15831 N2 := Get_Associated_Node (N);
15833 -- The identifier denotes a call to a parameterless function.
15834 -- Mark the node as resolved when the function is external.
15836 if Nkind (N2) = N_Function_Call then
15837 E := Entity (Name (N2));
15839 if Present (E) and then Is_Global (E) then
15840 Set_Etype (N, Etype (N2));
15842 Set_Associated_Node (N, Empty);
15843 Set_Etype (N, Empty);
15846 -- The identifier denotes a named number that was constant
15847 -- folded. Preserve the original name for ASIS and undo the
15848 -- constant folding which will be repeated in the instance.
15849 -- Is this still needed???
15851 elsif Nkind_In (N2, N_Integer_Literal, N_Real_Literal)
15852 and then Is_Entity_Name (Original_Node (N2))
15854 Set_Associated_Node (N, Original_Node (N2));
15857 -- The identifier resolved to a string literal. Propagate this
15858 -- information to the generic template.
15860 elsif Nkind (N2) = N_String_Literal then
15861 Rewrite (N, New_Copy (N2));
15863 -- The identifier is rewritten as a dereference if it is the
15864 -- prefix of an implicit dereference. Preserve the original
15865 -- tree as the analysis of the instance will expand the node
15866 -- again, but preserve the resolved entity if it is global.
15868 elsif Nkind (N2) = N_Explicit_Dereference then
15869 if Is_Entity_Name (Prefix (N2))
15870 and then Present (Entity (Prefix (N2)))
15871 and then Is_Global (Entity (Prefix (N2)))
15873 Set_Associated_Node (N, Prefix (N2));
15875 elsif Nkind (Prefix (N2)) = N_Function_Call
15876 and then Present (Entity (Name (Prefix (N2))))
15877 and then Is_Global (Entity (Name (Prefix (N2))))
15880 Make_Explicit_Dereference (Loc,
15882 Make_Function_Call (Loc,
15885 (Entity (Name (Prefix (N2))), Loc))));
15888 Set_Associated_Node (N, Empty);
15889 Set_Etype (N, Empty);
15892 -- The subtype mark of a nominally unconstrained object is
15893 -- rewritten as a subtype indication using the bounds of the
15894 -- expression. Recover the original subtype mark.
15896 elsif Nkind (N2) = N_Subtype_Indication
15897 and then Is_Entity_Name (Original_Node (N2))
15899 Set_Associated_Node (N, Original_Node (N2));
15903 end Save_References_In_Identifier;
15905 ---------------------------------
15906 -- Save_References_In_Operator --
15907 ---------------------------------
15909 procedure Save_References_In_Operator (N : Node_Id) is
15911 -- The node did not undergo a transformation
15913 if Nkind (N) = Nkind (Get_Associated_Node (N)) then
15914 if Nkind (N) = N_Op_Concat then
15915 Set_Is_Component_Left_Opnd (N,
15916 Is_Component_Left_Opnd (Get_Associated_Node (N)));
15918 Set_Is_Component_Right_Opnd (N,
15919 Is_Component_Right_Opnd (Get_Associated_Node (N)));
15924 -- The analysis of the generic copy transformed the operator into
15925 -- some other construct. Propagate the changes to the template if
15929 N2 := Get_Associated_Node (N);
15931 -- The operator resoved to a function call
15933 if Nkind (N2) = N_Function_Call then
15935 -- Add explicit qualifications in the generic template for
15936 -- all operands of universal type. This aids resolution by
15937 -- preserving the actual type of a literal or an attribute
15938 -- that yields a universal result.
15940 Qualify_Universal_Operands (N, N2);
15942 E := Entity (Name (N2));
15944 if Present (E) and then Is_Global (E) then
15945 Set_Etype (N, Etype (N2));
15947 Set_Associated_Node (N, Empty);
15948 Set_Etype (N, Empty);
15951 -- The operator was folded into a literal
15953 elsif Nkind_In (N2, N_Integer_Literal,
15957 if Present (Original_Node (N2))
15958 and then Nkind (Original_Node (N2)) = Nkind (N)
15960 -- Operation was constant-folded. Whenever possible,
15961 -- recover semantic information from unfolded node.
15962 -- This was initially done for ASIS but is apparently
15963 -- needed also for e.g. compiling a-nbnbin.adb.
15965 Set_Associated_Node (N, Original_Node (N2));
15967 if Nkind (N) = N_Op_Concat then
15968 Set_Is_Component_Left_Opnd (N,
15969 Is_Component_Left_Opnd (Get_Associated_Node (N)));
15970 Set_Is_Component_Right_Opnd (N,
15971 Is_Component_Right_Opnd (Get_Associated_Node (N)));
15976 -- Propagate the constant folding back to the template
15979 Rewrite (N, New_Copy (N2));
15980 Set_Analyzed (N, False);
15983 -- The operator was folded into an enumeration literal. Retain
15984 -- the entity to avoid spurious ambiguities if it is overloaded
15985 -- at the point of instantiation or inlining.
15987 elsif Nkind (N2) = N_Identifier
15988 and then Ekind (Entity (N2)) = E_Enumeration_Literal
15990 Rewrite (N, New_Copy (N2));
15991 Set_Analyzed (N, False);
15995 -- Complete the operands check if node has not been constant
15998 if Nkind (N) in N_Op then
15999 Save_Entity_Descendants (N);
16001 end Save_References_In_Operator;
16003 -------------------------------
16004 -- Save_References_In_Pragma --
16005 -------------------------------
16007 procedure Save_References_In_Pragma (Prag : Node_Id) is
16009 Do_Save : Boolean := True;
16011 use Atree.Unchecked_Access;
16012 -- This code section is part of implementing an untyped tree
16013 -- traversal, so it needs direct access to node fields.
16016 -- Do not save global references in pragmas generated from aspects
16017 -- because the pragmas will be regenerated at instantiation time.
16019 if From_Aspect_Specification (Prag) then
16022 -- The capture of global references within contract-related source
16023 -- pragmas associated with generic packages, subprograms or their
16024 -- respective bodies must be delayed due to timing of annotation
16025 -- analysis. Global references are still captured in routine
16026 -- Save_Global_References_In_Contract.
16028 elsif Is_Generic_Contract_Pragma (Prag) and then Prag /= Templ then
16029 if Is_Package_Contract_Annotation (Prag) then
16030 Context := Find_Related_Package_Or_Body (Prag);
16032 pragma Assert (Is_Subprogram_Contract_Annotation (Prag));
16033 Context := Find_Related_Declaration_Or_Body (Prag);
16036 -- The use of Original_Node accounts for the case when the
16037 -- related context is generic template.
16039 if Requires_Delayed_Save (Original_Node (Context)) then
16044 -- For all other cases, save all global references within the
16045 -- descendants, but skip the following semantic fields:
16047 -- Field1 - Next_Pragma
16048 -- Field3 - Corresponding_Aspect
16049 -- Field5 - Next_Rep_Item
16052 Save_Global_Descendant (Field2 (Prag));
16053 Save_Global_Descendant (Field4 (Prag));
16055 end Save_References_In_Pragma;
16057 -- Start of processing for Save_References
16065 elsif Nkind_In (N, N_Aggregate, N_Extension_Aggregate) then
16066 Save_References_In_Aggregate (N);
16068 -- Character literals, operator symbols
16070 elsif Nkind_In (N, N_Character_Literal, N_Operator_Symbol) then
16071 Save_References_In_Char_Lit_Or_Op_Symbol (N);
16073 -- Defining identifiers
16075 elsif Nkind (N) in N_Entity then
16080 elsif Nkind (N) = N_Identifier then
16081 Save_References_In_Identifier (N);
16085 elsif Nkind (N) in N_Op then
16086 Save_References_In_Operator (N);
16090 elsif Nkind (N) = N_Pragma then
16091 Save_References_In_Pragma (N);
16094 Save_References_In_Descendants (N);
16097 -- Save all global references found within the aspect specifications
16098 -- of the related node.
16100 if Permits_Aspect_Specifications (N) and then Has_Aspects (N) then
16102 -- The capture of global references within aspects associated with
16103 -- generic packages, subprograms or their bodies must be delayed
16104 -- due to timing of annotation analysis. Global references are
16105 -- still captured in routine Save_Global_References_In_Contract.
16107 if Requires_Delayed_Save (N) then
16110 -- Otherwise save all global references within the aspects
16113 Save_Global_References_In_Aspects (N);
16116 end Save_References;
16118 -- Start of processing for Save_Global_References
16121 Gen_Scope := Current_Scope;
16123 -- If the generic unit is a child unit, references to entities in the
16124 -- parent are treated as local, because they will be resolved anew in
16125 -- the context of the instance of the parent.
16127 while Is_Child_Unit (Gen_Scope)
16128 and then Ekind (Scope (Gen_Scope)) = E_Generic_Package
16130 Gen_Scope := Scope (Gen_Scope);
16133 Save_References (Templ);
16134 end Save_Global_References;
16136 ---------------------------------------
16137 -- Save_Global_References_In_Aspects --
16138 ---------------------------------------
16140 procedure Save_Global_References_In_Aspects (N : Node_Id) is
16145 Asp := First (Aspect_Specifications (N));
16146 while Present (Asp) loop
16147 Expr := Expression (Asp);
16149 if Present (Expr) then
16150 Save_Global_References (Expr);
16155 end Save_Global_References_In_Aspects;
16157 ------------------------------------------
16158 -- Set_Copied_Sloc_For_Inherited_Pragma --
16159 ------------------------------------------
16161 procedure Set_Copied_Sloc_For_Inherited_Pragma
16166 Create_Instantiation_Source (N, E,
16167 Inlined_Body => False,
16168 Inherited_Pragma => True,
16169 Factor => S_Adjustment);
16170 end Set_Copied_Sloc_For_Inherited_Pragma;
16172 --------------------------------------
16173 -- Set_Copied_Sloc_For_Inlined_Body --
16174 --------------------------------------
16176 procedure Set_Copied_Sloc_For_Inlined_Body (N : Node_Id; E : Entity_Id) is
16178 Create_Instantiation_Source (N, E,
16179 Inlined_Body => True,
16180 Inherited_Pragma => False,
16181 Factor => S_Adjustment);
16182 end Set_Copied_Sloc_For_Inlined_Body;
16184 ---------------------
16185 -- Set_Instance_Of --
16186 ---------------------
16188 procedure Set_Instance_Of (A : Entity_Id; B : Entity_Id) is
16190 Generic_Renamings.Table (Generic_Renamings.Last) := (A, B, Assoc_Null);
16191 Generic_Renamings_HTable.Set (Generic_Renamings.Last);
16192 Generic_Renamings.Increment_Last;
16193 end Set_Instance_Of;
16195 --------------------
16196 -- Set_Next_Assoc --
16197 --------------------
16199 procedure Set_Next_Assoc (E : Assoc_Ptr; Next : Assoc_Ptr) is
16201 Generic_Renamings.Table (E).Next_In_HTable := Next;
16202 end Set_Next_Assoc;
16204 -------------------
16205 -- Start_Generic --
16206 -------------------
16208 procedure Start_Generic is
16210 -- ??? More things could be factored out in this routine.
16211 -- Should probably be done at a later stage.
16213 Generic_Flags.Append (Inside_A_Generic);
16214 Inside_A_Generic := True;
16216 Expander_Mode_Save_And_Set (False);
16219 ----------------------
16220 -- Set_Instance_Env --
16221 ----------------------
16223 -- WARNING: This routine manages SPARK regions
16225 procedure Set_Instance_Env
16226 (Gen_Unit : Entity_Id;
16227 Act_Unit : Entity_Id)
16229 Saved_AE : constant Boolean := Assertions_Enabled;
16230 Saved_CPL : constant Node_Id := Check_Policy_List;
16231 Saved_DEC : constant Boolean := Dynamic_Elaboration_Checks;
16232 Saved_SM : constant SPARK_Mode_Type := SPARK_Mode;
16233 Saved_SMP : constant Node_Id := SPARK_Mode_Pragma;
16236 -- Regardless of the current mode, predefined units are analyzed in the
16237 -- most current Ada mode, and earlier version Ada checks do not apply
16238 -- to predefined units. Nothing needs to be done for non-internal units.
16239 -- These are always analyzed in the current mode.
16241 if In_Internal_Unit (Gen_Unit) then
16243 -- The following call resets all configuration attributes to default
16244 -- or the xxx_Config versions of the attributes when the current sem
16245 -- unit is the main unit. At the same time, internal units must also
16246 -- inherit certain configuration attributes from their context. It
16247 -- is unclear what these two sets are.
16249 Set_Config_Switches (True, Current_Sem_Unit = Main_Unit);
16251 -- Reinstall relevant configuration attributes of the context
16253 Assertions_Enabled := Saved_AE;
16254 Check_Policy_List := Saved_CPL;
16255 Dynamic_Elaboration_Checks := Saved_DEC;
16257 Install_SPARK_Mode (Saved_SM, Saved_SMP);
16260 Current_Instantiated_Parent :=
16261 (Gen_Id => Gen_Unit,
16262 Act_Id => Act_Unit,
16263 Next_In_HTable => Assoc_Null);
16264 end Set_Instance_Env;
16270 procedure Switch_View (T : Entity_Id) is
16271 BT : constant Entity_Id := Base_Type (T);
16272 Priv_Elmt : Elmt_Id := No_Elmt;
16273 Priv_Sub : Entity_Id;
16276 -- T may be private but its base type may have been exchanged through
16277 -- some other occurrence, in which case there is nothing to switch
16278 -- besides T itself. Note that a private dependent subtype of a private
16279 -- type might not have been switched even if the base type has been,
16280 -- because of the last branch of Check_Private_View (see comment there).
16282 if not Is_Private_Type (BT) then
16283 Prepend_Elmt (Full_View (T), Exchanged_Views);
16284 Exchange_Declarations (T);
16288 Priv_Elmt := First_Elmt (Private_Dependents (BT));
16290 if Present (Full_View (BT)) then
16291 Prepend_Elmt (Full_View (BT), Exchanged_Views);
16292 Exchange_Declarations (BT);
16295 while Present (Priv_Elmt) loop
16296 Priv_Sub := (Node (Priv_Elmt));
16298 -- We avoid flipping the subtype if the Etype of its full view is
16299 -- private because this would result in a malformed subtype. This
16300 -- occurs when the Etype of the subtype full view is the full view of
16301 -- the base type (and since the base types were just switched, the
16302 -- subtype is pointing to the wrong view). This is currently the case
16303 -- for tagged record types, access types (maybe more?) and needs to
16304 -- be resolved. ???
16306 if Present (Full_View (Priv_Sub))
16307 and then not Is_Private_Type (Etype (Full_View (Priv_Sub)))
16309 Prepend_Elmt (Full_View (Priv_Sub), Exchanged_Views);
16310 Exchange_Declarations (Priv_Sub);
16313 Next_Elmt (Priv_Elmt);
16321 function True_Parent (N : Node_Id) return Node_Id is
16323 if Nkind (Parent (N)) = N_Subunit then
16324 return Parent (Corresponding_Stub (Parent (N)));
16330 -----------------------------
16331 -- Valid_Default_Attribute --
16332 -----------------------------
16334 procedure Valid_Default_Attribute (Nam : Entity_Id; Def : Node_Id) is
16335 Attr_Id : constant Attribute_Id :=
16336 Get_Attribute_Id (Attribute_Name (Def));
16337 T : constant Entity_Id := Entity (Prefix (Def));
16338 Is_Fun : constant Boolean := (Ekind (Nam) = E_Function);
16344 if No (T) or else T = Any_Id then
16349 F := First_Formal (Nam);
16350 while Present (F) loop
16351 Num_F := Num_F + 1;
16356 when Attribute_Adjacent
16357 | Attribute_Ceiling
16358 | Attribute_Copy_Sign
16360 | Attribute_Fraction
16361 | Attribute_Machine
16363 | Attribute_Remainder
16364 | Attribute_Rounding
16365 | Attribute_Unbiased_Rounding
16369 and then Is_Floating_Point_Type (T);
16371 when Attribute_Image
16375 | Attribute_Wide_Image
16376 | Attribute_Wide_Value
16378 OK := Is_Fun and then Num_F = 1 and then Is_Scalar_Type (T);
16383 OK := Is_Fun and then Num_F = 2 and then Is_Scalar_Type (T);
16385 when Attribute_Input =>
16386 OK := (Is_Fun and then Num_F = 1);
16388 when Attribute_Output
16392 OK := not Is_Fun and then Num_F = 2;
16400 ("attribute reference has wrong profile for subprogram", Def);
16402 end Valid_Default_Attribute;